Merge remote-tracking branch 'lsk/v3.10/topic/gator' into linux-linaro-lsk
[firefly-linux-kernel-4.4.55.git] / drivers / misc / mei / pci-me.c
1 /*
2  *
3  * Intel Management Engine Interface (Intel MEI) Linux driver
4  * Copyright (c) 2003-2012, Intel Corporation.
5  *
6  * This program is free software; you can redistribute it and/or modify it
7  * under the terms and conditions of the GNU General Public License,
8  * version 2, as published by the Free Software Foundation.
9  *
10  * This program is distributed in the hope it will be useful, but WITHOUT
11  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
13  * more details.
14  *
15  */
16
17 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
18
19 #include <linux/module.h>
20 #include <linux/moduleparam.h>
21 #include <linux/kernel.h>
22 #include <linux/device.h>
23 #include <linux/fs.h>
24 #include <linux/errno.h>
25 #include <linux/types.h>
26 #include <linux/fcntl.h>
27 #include <linux/aio.h>
28 #include <linux/pci.h>
29 #include <linux/poll.h>
30 #include <linux/init.h>
31 #include <linux/ioctl.h>
32 #include <linux/cdev.h>
33 #include <linux/sched.h>
34 #include <linux/uuid.h>
35 #include <linux/compat.h>
36 #include <linux/jiffies.h>
37 #include <linux/interrupt.h>
38 #include <linux/miscdevice.h>
39
40 #include <linux/mei.h>
41
42 #include "mei_dev.h"
43 #include "hw-me.h"
44 #include "client.h"
45
46 /* AMT device is a singleton on the platform */
47 static struct pci_dev *mei_pdev;
48
49 /* mei_pci_tbl - PCI Device ID Table */
50 static DEFINE_PCI_DEVICE_TABLE(mei_me_pci_tbl) = {
51         {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MEI_DEV_ID_82946GZ)},
52         {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MEI_DEV_ID_82G35)},
53         {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MEI_DEV_ID_82Q965)},
54         {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MEI_DEV_ID_82G965)},
55         {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MEI_DEV_ID_82GM965)},
56         {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MEI_DEV_ID_82GME965)},
57         {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MEI_DEV_ID_ICH9_82Q35)},
58         {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MEI_DEV_ID_ICH9_82G33)},
59         {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MEI_DEV_ID_ICH9_82Q33)},
60         {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MEI_DEV_ID_ICH9_82X38)},
61         {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MEI_DEV_ID_ICH9_3200)},
62         {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MEI_DEV_ID_ICH9_6)},
63         {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MEI_DEV_ID_ICH9_7)},
64         {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MEI_DEV_ID_ICH9_8)},
65         {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MEI_DEV_ID_ICH9_9)},
66         {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MEI_DEV_ID_ICH9_10)},
67         {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MEI_DEV_ID_ICH9M_1)},
68         {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MEI_DEV_ID_ICH9M_2)},
69         {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MEI_DEV_ID_ICH9M_3)},
70         {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MEI_DEV_ID_ICH9M_4)},
71         {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MEI_DEV_ID_ICH10_1)},
72         {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MEI_DEV_ID_ICH10_2)},
73         {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MEI_DEV_ID_ICH10_3)},
74         {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MEI_DEV_ID_ICH10_4)},
75         {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MEI_DEV_ID_IBXPK_1)},
76         {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MEI_DEV_ID_IBXPK_2)},
77         {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MEI_DEV_ID_CPT_1)},
78         {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MEI_DEV_ID_PBG_1)},
79         {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MEI_DEV_ID_PPT_1)},
80         {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MEI_DEV_ID_PPT_2)},
81         {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MEI_DEV_ID_PPT_3)},
82         {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MEI_DEV_ID_LPT_H)},
83         {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MEI_DEV_ID_LPT_W)},
84         {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MEI_DEV_ID_LPT_LP)},
85         {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MEI_DEV_ID_LPT_HR)},
86         {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MEI_DEV_ID_WPT_LP)},
87
88         /* required last entry */
89         {0, }
90 };
91
92 MODULE_DEVICE_TABLE(pci, mei_me_pci_tbl);
93
94 static DEFINE_MUTEX(mei_mutex);
95
96 /**
97  * mei_quirk_probe - probe for devices that doesn't valid ME interface
98  *
99  * @pdev: PCI device structure
100  * @ent: entry into pci_device_table
101  *
102  * returns true if ME Interface is valid, false otherwise
103  */
104 static bool mei_me_quirk_probe(struct pci_dev *pdev,
105                                 const struct pci_device_id *ent)
106 {
107         u32 reg;
108         /* Cougar Point || Patsburg */
109         if (ent->device == MEI_DEV_ID_CPT_1 ||
110             ent->device == MEI_DEV_ID_PBG_1) {
111                 pci_read_config_dword(pdev, PCI_CFG_HFS_2, &reg);
112                 /* make sure that bit 9 (NM) is up and bit 10 (DM) is down */
113                 if ((reg & 0x600) == 0x200)
114                         goto no_mei;
115         }
116
117         /* Lynx Point */
118         if (ent->device == MEI_DEV_ID_LPT_H  ||
119             ent->device == MEI_DEV_ID_LPT_W  ||
120             ent->device == MEI_DEV_ID_LPT_HR) {
121                 /* Read ME FW Status check for SPS Firmware */
122                 pci_read_config_dword(pdev, PCI_CFG_HFS_1, &reg);
123                 /* if bits [19:16] = 15, running SPS Firmware */
124                 if ((reg & 0xf0000) == 0xf0000)
125                         goto no_mei;
126         }
127
128         return true;
129
130 no_mei:
131         dev_info(&pdev->dev, "Device doesn't have valid ME Interface\n");
132         return false;
133 }
134 /**
135  * mei_probe - Device Initialization Routine
136  *
137  * @pdev: PCI device structure
138  * @ent: entry in kcs_pci_tbl
139  *
140  * returns 0 on success, <0 on failure.
141  */
142 static int mei_me_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
143 {
144         struct mei_device *dev;
145         struct mei_me_hw *hw;
146         int err;
147
148         mutex_lock(&mei_mutex);
149
150         if (!mei_me_quirk_probe(pdev, ent)) {
151                 err = -ENODEV;
152                 goto end;
153         }
154
155         if (mei_pdev) {
156                 err = -EEXIST;
157                 goto end;
158         }
159         /* enable pci dev */
160         err = pci_enable_device(pdev);
161         if (err) {
162                 dev_err(&pdev->dev, "failed to enable pci device.\n");
163                 goto end;
164         }
165         /* set PCI host mastering  */
166         pci_set_master(pdev);
167         /* pci request regions for mei driver */
168         err = pci_request_regions(pdev, KBUILD_MODNAME);
169         if (err) {
170                 dev_err(&pdev->dev, "failed to get pci regions.\n");
171                 goto disable_device;
172         }
173         /* allocates and initializes the mei dev structure */
174         dev = mei_me_dev_init(pdev);
175         if (!dev) {
176                 err = -ENOMEM;
177                 goto release_regions;
178         }
179         hw = to_me_hw(dev);
180         /* mapping  IO device memory */
181         hw->mem_addr = pci_iomap(pdev, 0, 0);
182         if (!hw->mem_addr) {
183                 dev_err(&pdev->dev, "mapping I/O device memory failure.\n");
184                 err = -ENOMEM;
185                 goto free_device;
186         }
187         pci_enable_msi(pdev);
188
189          /* request and enable interrupt */
190         if (pci_dev_msi_enabled(pdev))
191                 err = request_threaded_irq(pdev->irq,
192                         NULL,
193                         mei_me_irq_thread_handler,
194                         IRQF_ONESHOT, KBUILD_MODNAME, dev);
195         else
196                 err = request_threaded_irq(pdev->irq,
197                         mei_me_irq_quick_handler,
198                         mei_me_irq_thread_handler,
199                         IRQF_SHARED, KBUILD_MODNAME, dev);
200
201         if (err) {
202                 dev_err(&pdev->dev, "request_threaded_irq failure. irq = %d\n",
203                        pdev->irq);
204                 goto disable_msi;
205         }
206
207         if (mei_start(dev)) {
208                 dev_err(&pdev->dev, "init hw failure.\n");
209                 err = -ENODEV;
210                 goto release_irq;
211         }
212
213         err = mei_register(dev);
214         if (err)
215                 goto release_irq;
216
217         mei_pdev = pdev;
218         pci_set_drvdata(pdev, dev);
219
220         schedule_delayed_work(&dev->timer_work, HZ);
221
222         mutex_unlock(&mei_mutex);
223
224         pr_debug("initialization successful.\n");
225
226         return 0;
227
228 release_irq:
229         mei_disable_interrupts(dev);
230         flush_scheduled_work();
231         free_irq(pdev->irq, dev);
232 disable_msi:
233         pci_disable_msi(pdev);
234         pci_iounmap(pdev, hw->mem_addr);
235 free_device:
236         kfree(dev);
237 release_regions:
238         pci_release_regions(pdev);
239 disable_device:
240         pci_disable_device(pdev);
241 end:
242         mutex_unlock(&mei_mutex);
243         dev_err(&pdev->dev, "initialization failed.\n");
244         return err;
245 }
246
247 /**
248  * mei_remove - Device Removal Routine
249  *
250  * @pdev: PCI device structure
251  *
252  * mei_remove is called by the PCI subsystem to alert the driver
253  * that it should release a PCI device.
254  */
255 static void mei_me_remove(struct pci_dev *pdev)
256 {
257         struct mei_device *dev;
258         struct mei_me_hw *hw;
259
260         if (mei_pdev != pdev)
261                 return;
262
263         dev = pci_get_drvdata(pdev);
264         if (!dev)
265                 return;
266
267         hw = to_me_hw(dev);
268
269
270         dev_err(&pdev->dev, "stop\n");
271         mei_stop(dev);
272
273         mei_pdev = NULL;
274
275         /* disable interrupts */
276         mei_disable_interrupts(dev);
277
278         free_irq(pdev->irq, dev);
279         pci_disable_msi(pdev);
280         pci_set_drvdata(pdev, NULL);
281
282         if (hw->mem_addr)
283                 pci_iounmap(pdev, hw->mem_addr);
284
285         mei_deregister(dev);
286
287         kfree(dev);
288
289         pci_release_regions(pdev);
290         pci_disable_device(pdev);
291
292
293 }
294 #ifdef CONFIG_PM
295 static int mei_me_pci_suspend(struct device *device)
296 {
297         struct pci_dev *pdev = to_pci_dev(device);
298         struct mei_device *dev = pci_get_drvdata(pdev);
299
300         if (!dev)
301                 return -ENODEV;
302
303         dev_err(&pdev->dev, "suspend\n");
304
305         mei_stop(dev);
306
307         mei_disable_interrupts(dev);
308
309         free_irq(pdev->irq, dev);
310         pci_disable_msi(pdev);
311
312         return 0;
313 }
314
315 static int mei_me_pci_resume(struct device *device)
316 {
317         struct pci_dev *pdev = to_pci_dev(device);
318         struct mei_device *dev;
319         int err;
320
321         dev = pci_get_drvdata(pdev);
322         if (!dev)
323                 return -ENODEV;
324
325         pci_enable_msi(pdev);
326
327         /* request and enable interrupt */
328         if (pci_dev_msi_enabled(pdev))
329                 err = request_threaded_irq(pdev->irq,
330                         NULL,
331                         mei_me_irq_thread_handler,
332                         IRQF_ONESHOT, KBUILD_MODNAME, dev);
333         else
334                 err = request_threaded_irq(pdev->irq,
335                         mei_me_irq_quick_handler,
336                         mei_me_irq_thread_handler,
337                         IRQF_SHARED, KBUILD_MODNAME, dev);
338
339         if (err) {
340                 dev_err(&pdev->dev, "request_threaded_irq failed: irq = %d.\n",
341                                 pdev->irq);
342                 return err;
343         }
344
345         mutex_lock(&dev->device_lock);
346         dev->dev_state = MEI_DEV_POWER_UP;
347         mei_clear_interrupts(dev);
348         mei_reset(dev, 1);
349         mutex_unlock(&dev->device_lock);
350
351         /* Start timer if stopped in suspend */
352         schedule_delayed_work(&dev->timer_work, HZ);
353
354         return err;
355 }
356 static SIMPLE_DEV_PM_OPS(mei_me_pm_ops, mei_me_pci_suspend, mei_me_pci_resume);
357 #define MEI_ME_PM_OPS   (&mei_me_pm_ops)
358 #else
359 #define MEI_ME_PM_OPS   NULL
360 #endif /* CONFIG_PM */
361 /*
362  *  PCI driver structure
363  */
364 static struct pci_driver mei_me_driver = {
365         .name = KBUILD_MODNAME,
366         .id_table = mei_me_pci_tbl,
367         .probe = mei_me_probe,
368         .remove = mei_me_remove,
369         .shutdown = mei_me_remove,
370         .driver.pm = MEI_ME_PM_OPS,
371 };
372
373 module_pci_driver(mei_me_driver);
374
375 MODULE_AUTHOR("Intel Corporation");
376 MODULE_DESCRIPTION("Intel(R) Management Engine Interface");
377 MODULE_LICENSE("GPL v2");