371c65ae6be6331554361687484aa63a797881ed
[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         if (ent->device == MEI_DEV_ID_PBG_1) {
109                 pci_read_config_dword(pdev, 0x48, &reg);
110                 /* make sure that bit 9 is up and bit 10 is down */
111                 if ((reg & 0x600) == 0x200) {
112                         dev_info(&pdev->dev, "Device doesn't have valid ME Interface\n");
113                         return false;
114                 }
115         }
116         return true;
117 }
118 /**
119  * mei_probe - Device Initialization Routine
120  *
121  * @pdev: PCI device structure
122  * @ent: entry in kcs_pci_tbl
123  *
124  * returns 0 on success, <0 on failure.
125  */
126 static int mei_me_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
127 {
128         struct mei_device *dev;
129         struct mei_me_hw *hw;
130         int err;
131
132         mutex_lock(&mei_mutex);
133
134         if (!mei_me_quirk_probe(pdev, ent)) {
135                 err = -ENODEV;
136                 goto end;
137         }
138
139         if (mei_pdev) {
140                 err = -EEXIST;
141                 goto end;
142         }
143         /* enable pci dev */
144         err = pci_enable_device(pdev);
145         if (err) {
146                 dev_err(&pdev->dev, "failed to enable pci device.\n");
147                 goto end;
148         }
149         /* set PCI host mastering  */
150         pci_set_master(pdev);
151         /* pci request regions for mei driver */
152         err = pci_request_regions(pdev, KBUILD_MODNAME);
153         if (err) {
154                 dev_err(&pdev->dev, "failed to get pci regions.\n");
155                 goto disable_device;
156         }
157         /* allocates and initializes the mei dev structure */
158         dev = mei_me_dev_init(pdev);
159         if (!dev) {
160                 err = -ENOMEM;
161                 goto release_regions;
162         }
163         hw = to_me_hw(dev);
164         /* mapping  IO device memory */
165         hw->mem_addr = pci_iomap(pdev, 0, 0);
166         if (!hw->mem_addr) {
167                 dev_err(&pdev->dev, "mapping I/O device memory failure.\n");
168                 err = -ENOMEM;
169                 goto free_device;
170         }
171         pci_enable_msi(pdev);
172
173          /* request and enable interrupt */
174         if (pci_dev_msi_enabled(pdev))
175                 err = request_threaded_irq(pdev->irq,
176                         NULL,
177                         mei_me_irq_thread_handler,
178                         IRQF_ONESHOT, KBUILD_MODNAME, dev);
179         else
180                 err = request_threaded_irq(pdev->irq,
181                         mei_me_irq_quick_handler,
182                         mei_me_irq_thread_handler,
183                         IRQF_SHARED, KBUILD_MODNAME, dev);
184
185         if (err) {
186                 dev_err(&pdev->dev, "request_threaded_irq failure. irq = %d\n",
187                        pdev->irq);
188                 goto disable_msi;
189         }
190
191         if (mei_start(dev)) {
192                 dev_err(&pdev->dev, "init hw failure.\n");
193                 err = -ENODEV;
194                 goto release_irq;
195         }
196
197         err = mei_register(dev);
198         if (err)
199                 goto release_irq;
200
201         mei_pdev = pdev;
202         pci_set_drvdata(pdev, dev);
203
204         schedule_delayed_work(&dev->timer_work, HZ);
205
206         mutex_unlock(&mei_mutex);
207
208         pr_debug("initialization successful.\n");
209
210         return 0;
211
212 release_irq:
213         mei_disable_interrupts(dev);
214         flush_scheduled_work();
215         free_irq(pdev->irq, dev);
216 disable_msi:
217         pci_disable_msi(pdev);
218         pci_iounmap(pdev, hw->mem_addr);
219 free_device:
220         kfree(dev);
221 release_regions:
222         pci_release_regions(pdev);
223 disable_device:
224         pci_disable_device(pdev);
225 end:
226         mutex_unlock(&mei_mutex);
227         dev_err(&pdev->dev, "initialization failed.\n");
228         return err;
229 }
230
231 /**
232  * mei_remove - Device Removal Routine
233  *
234  * @pdev: PCI device structure
235  *
236  * mei_remove is called by the PCI subsystem to alert the driver
237  * that it should release a PCI device.
238  */
239 static void mei_me_remove(struct pci_dev *pdev)
240 {
241         struct mei_device *dev;
242         struct mei_me_hw *hw;
243
244         if (mei_pdev != pdev)
245                 return;
246
247         dev = pci_get_drvdata(pdev);
248         if (!dev)
249                 return;
250
251         hw = to_me_hw(dev);
252
253
254         dev_err(&pdev->dev, "stop\n");
255         mei_stop(dev);
256
257         mei_pdev = NULL;
258
259         /* disable interrupts */
260         mei_disable_interrupts(dev);
261
262         free_irq(pdev->irq, dev);
263         pci_disable_msi(pdev);
264         pci_set_drvdata(pdev, NULL);
265
266         if (hw->mem_addr)
267                 pci_iounmap(pdev, hw->mem_addr);
268
269         mei_deregister(dev);
270
271         kfree(dev);
272
273         pci_release_regions(pdev);
274         pci_disable_device(pdev);
275
276
277 }
278 #ifdef CONFIG_PM
279 static int mei_me_pci_suspend(struct device *device)
280 {
281         struct pci_dev *pdev = to_pci_dev(device);
282         struct mei_device *dev = pci_get_drvdata(pdev);
283
284         if (!dev)
285                 return -ENODEV;
286
287         dev_err(&pdev->dev, "suspend\n");
288
289         mei_stop(dev);
290
291         mei_disable_interrupts(dev);
292
293         free_irq(pdev->irq, dev);
294         pci_disable_msi(pdev);
295
296         return 0;
297 }
298
299 static int mei_me_pci_resume(struct device *device)
300 {
301         struct pci_dev *pdev = to_pci_dev(device);
302         struct mei_device *dev;
303         int err;
304
305         dev = pci_get_drvdata(pdev);
306         if (!dev)
307                 return -ENODEV;
308
309         pci_enable_msi(pdev);
310
311         /* request and enable interrupt */
312         if (pci_dev_msi_enabled(pdev))
313                 err = request_threaded_irq(pdev->irq,
314                         NULL,
315                         mei_me_irq_thread_handler,
316                         IRQF_ONESHOT, KBUILD_MODNAME, dev);
317         else
318                 err = request_threaded_irq(pdev->irq,
319                         mei_me_irq_quick_handler,
320                         mei_me_irq_thread_handler,
321                         IRQF_SHARED, KBUILD_MODNAME, dev);
322
323         if (err) {
324                 dev_err(&pdev->dev, "request_threaded_irq failed: irq = %d.\n",
325                                 pdev->irq);
326                 return err;
327         }
328
329         mutex_lock(&dev->device_lock);
330         dev->dev_state = MEI_DEV_POWER_UP;
331         mei_clear_interrupts(dev);
332         mei_reset(dev, 1);
333         mutex_unlock(&dev->device_lock);
334
335         /* Start timer if stopped in suspend */
336         schedule_delayed_work(&dev->timer_work, HZ);
337
338         return err;
339 }
340 static SIMPLE_DEV_PM_OPS(mei_me_pm_ops, mei_me_pci_suspend, mei_me_pci_resume);
341 #define MEI_ME_PM_OPS   (&mei_me_pm_ops)
342 #else
343 #define MEI_ME_PM_OPS   NULL
344 #endif /* CONFIG_PM */
345 /*
346  *  PCI driver structure
347  */
348 static struct pci_driver mei_me_driver = {
349         .name = KBUILD_MODNAME,
350         .id_table = mei_me_pci_tbl,
351         .probe = mei_me_probe,
352         .remove = mei_me_remove,
353         .shutdown = mei_me_remove,
354         .driver.pm = MEI_ME_PM_OPS,
355 };
356
357 module_pci_driver(mei_me_driver);
358
359 MODULE_AUTHOR("Intel Corporation");
360 MODULE_DESCRIPTION("Intel(R) Management Engine Interface");
361 MODULE_LICENSE("GPL v2");