blob: 1b3844e823792a0893ee9b200cbc7a681ee7025f [file] [log] [blame]
Tomas Winkler2703d4b2013-02-06 14:06:39 +02001/*
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
Tomas Winkler2703d4b2013-02-06 14:06:39 +020046/* mei_pci_tbl - PCI Device ID Table */
Tomas Winklerb68301e2013-03-27 16:58:29 +020047static DEFINE_PCI_DEVICE_TABLE(mei_me_pci_tbl) = {
Tomas Winkler2703d4b2013-02-06 14:06:39 +020048 {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MEI_DEV_ID_82946GZ)},
49 {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MEI_DEV_ID_82G35)},
50 {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MEI_DEV_ID_82Q965)},
51 {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MEI_DEV_ID_82G965)},
52 {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MEI_DEV_ID_82GM965)},
53 {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MEI_DEV_ID_82GME965)},
54 {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MEI_DEV_ID_ICH9_82Q35)},
55 {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MEI_DEV_ID_ICH9_82G33)},
56 {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MEI_DEV_ID_ICH9_82Q33)},
57 {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MEI_DEV_ID_ICH9_82X38)},
58 {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MEI_DEV_ID_ICH9_3200)},
59 {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MEI_DEV_ID_ICH9_6)},
60 {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MEI_DEV_ID_ICH9_7)},
61 {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MEI_DEV_ID_ICH9_8)},
62 {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MEI_DEV_ID_ICH9_9)},
63 {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MEI_DEV_ID_ICH9_10)},
64 {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MEI_DEV_ID_ICH9M_1)},
65 {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MEI_DEV_ID_ICH9M_2)},
66 {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MEI_DEV_ID_ICH9M_3)},
67 {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MEI_DEV_ID_ICH9M_4)},
68 {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MEI_DEV_ID_ICH10_1)},
69 {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MEI_DEV_ID_ICH10_2)},
70 {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MEI_DEV_ID_ICH10_3)},
71 {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MEI_DEV_ID_ICH10_4)},
72 {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MEI_DEV_ID_IBXPK_1)},
73 {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MEI_DEV_ID_IBXPK_2)},
74 {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MEI_DEV_ID_CPT_1)},
75 {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MEI_DEV_ID_PBG_1)},
76 {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MEI_DEV_ID_PPT_1)},
77 {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MEI_DEV_ID_PPT_2)},
78 {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MEI_DEV_ID_PPT_3)},
79 {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MEI_DEV_ID_LPT)},
80 {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MEI_DEV_ID_LPT_LP)},
81
82 /* required last entry */
83 {0, }
84};
85
Tomas Winklerb68301e2013-03-27 16:58:29 +020086MODULE_DEVICE_TABLE(pci, mei_me_pci_tbl);
Tomas Winkler2703d4b2013-02-06 14:06:39 +020087
Tomas Winkler2703d4b2013-02-06 14:06:39 +020088/**
89 * mei_quirk_probe - probe for devices that doesn't valid ME interface
Masanari Iida393b1482013-04-05 01:05:05 +090090 *
Tomas Winkler2703d4b2013-02-06 14:06:39 +020091 * @pdev: PCI device structure
92 * @ent: entry into pci_device_table
93 *
94 * returns true if ME Interface is valid, false otherwise
95 */
Tomas Winklerb68301e2013-03-27 16:58:29 +020096static bool mei_me_quirk_probe(struct pci_dev *pdev,
Tomas Winkler2703d4b2013-02-06 14:06:39 +020097 const struct pci_device_id *ent)
98{
99 u32 reg;
100 if (ent->device == MEI_DEV_ID_PBG_1) {
101 pci_read_config_dword(pdev, 0x48, &reg);
102 /* make sure that bit 9 is up and bit 10 is down */
103 if ((reg & 0x600) == 0x200) {
104 dev_info(&pdev->dev, "Device doesn't have valid ME Interface\n");
105 return false;
106 }
107 }
108 return true;
109}
110/**
111 * mei_probe - Device Initialization Routine
112 *
113 * @pdev: PCI device structure
114 * @ent: entry in kcs_pci_tbl
115 *
116 * returns 0 on success, <0 on failure.
117 */
Tomas Winklerb68301e2013-03-27 16:58:29 +0200118static int mei_me_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
Tomas Winkler2703d4b2013-02-06 14:06:39 +0200119{
120 struct mei_device *dev;
Tomas Winkler52c34562013-02-06 14:06:40 +0200121 struct mei_me_hw *hw;
Tomas Winkler2703d4b2013-02-06 14:06:39 +0200122 int err;
123
Tomas Winkler2703d4b2013-02-06 14:06:39 +0200124
Tomas Winklerb68301e2013-03-27 16:58:29 +0200125 if (!mei_me_quirk_probe(pdev, ent)) {
Tomas Winkler2703d4b2013-02-06 14:06:39 +0200126 err = -ENODEV;
127 goto end;
128 }
129
Tomas Winkler2703d4b2013-02-06 14:06:39 +0200130 /* enable pci dev */
131 err = pci_enable_device(pdev);
132 if (err) {
133 dev_err(&pdev->dev, "failed to enable pci device.\n");
134 goto end;
135 }
136 /* set PCI host mastering */
137 pci_set_master(pdev);
138 /* pci request regions for mei driver */
139 err = pci_request_regions(pdev, KBUILD_MODNAME);
140 if (err) {
141 dev_err(&pdev->dev, "failed to get pci regions.\n");
142 goto disable_device;
143 }
144 /* allocates and initializes the mei dev structure */
Tomas Winkler52c34562013-02-06 14:06:40 +0200145 dev = mei_me_dev_init(pdev);
Tomas Winkler2703d4b2013-02-06 14:06:39 +0200146 if (!dev) {
147 err = -ENOMEM;
148 goto release_regions;
149 }
Tomas Winkler52c34562013-02-06 14:06:40 +0200150 hw = to_me_hw(dev);
Tomas Winkler2703d4b2013-02-06 14:06:39 +0200151 /* mapping IO device memory */
Tomas Winkler52c34562013-02-06 14:06:40 +0200152 hw->mem_addr = pci_iomap(pdev, 0, 0);
153 if (!hw->mem_addr) {
Tomas Winkler2703d4b2013-02-06 14:06:39 +0200154 dev_err(&pdev->dev, "mapping I/O device memory failure.\n");
155 err = -ENOMEM;
156 goto free_device;
157 }
158 pci_enable_msi(pdev);
159
160 /* request and enable interrupt */
161 if (pci_dev_msi_enabled(pdev))
162 err = request_threaded_irq(pdev->irq,
163 NULL,
Tomas Winkler06ecd642013-02-06 14:06:42 +0200164 mei_me_irq_thread_handler,
Tomas Winkler2703d4b2013-02-06 14:06:39 +0200165 IRQF_ONESHOT, KBUILD_MODNAME, dev);
166 else
167 err = request_threaded_irq(pdev->irq,
Tomas Winkler06ecd642013-02-06 14:06:42 +0200168 mei_me_irq_quick_handler,
169 mei_me_irq_thread_handler,
Tomas Winkler2703d4b2013-02-06 14:06:39 +0200170 IRQF_SHARED, KBUILD_MODNAME, dev);
171
172 if (err) {
173 dev_err(&pdev->dev, "request_threaded_irq failure. irq = %d\n",
174 pdev->irq);
175 goto disable_msi;
176 }
177
Tomas Winklerc4d589b2013-03-27 16:58:28 +0200178 if (mei_start(dev)) {
Tomas Winkler2703d4b2013-02-06 14:06:39 +0200179 dev_err(&pdev->dev, "init hw failure.\n");
180 err = -ENODEV;
181 goto release_irq;
182 }
183
Tomas Winkler30e53bb2013-04-05 22:10:34 +0300184 err = mei_register(dev);
Tomas Winkler2703d4b2013-02-06 14:06:39 +0200185 if (err)
186 goto release_irq;
187
Tomas Winkler2703d4b2013-02-06 14:06:39 +0200188 pci_set_drvdata(pdev, dev);
189
Tomas Winkler2703d4b2013-02-06 14:06:39 +0200190 schedule_delayed_work(&dev->timer_work, HZ);
191
Tomas Winkler2703d4b2013-02-06 14:06:39 +0200192 pr_debug("initialization successful.\n");
193
194 return 0;
195
196release_irq:
197 mei_disable_interrupts(dev);
198 flush_scheduled_work();
199 free_irq(pdev->irq, dev);
200disable_msi:
201 pci_disable_msi(pdev);
Tomas Winkler52c34562013-02-06 14:06:40 +0200202 pci_iounmap(pdev, hw->mem_addr);
Tomas Winkler2703d4b2013-02-06 14:06:39 +0200203free_device:
204 kfree(dev);
205release_regions:
206 pci_release_regions(pdev);
207disable_device:
208 pci_disable_device(pdev);
209end:
Tomas Winkler2703d4b2013-02-06 14:06:39 +0200210 dev_err(&pdev->dev, "initialization failed.\n");
211 return err;
212}
213
214/**
215 * mei_remove - Device Removal Routine
216 *
217 * @pdev: PCI device structure
218 *
219 * mei_remove is called by the PCI subsystem to alert the driver
220 * that it should release a PCI device.
221 */
Tomas Winklerb68301e2013-03-27 16:58:29 +0200222static void mei_me_remove(struct pci_dev *pdev)
Tomas Winkler2703d4b2013-02-06 14:06:39 +0200223{
224 struct mei_device *dev;
Tomas Winkler52c34562013-02-06 14:06:40 +0200225 struct mei_me_hw *hw;
Tomas Winkler2703d4b2013-02-06 14:06:39 +0200226
Tomas Winkler2703d4b2013-02-06 14:06:39 +0200227 dev = pci_get_drvdata(pdev);
228 if (!dev)
229 return;
230
Tomas Winkler52c34562013-02-06 14:06:40 +0200231 hw = to_me_hw(dev);
232
Tomas Winkler2703d4b2013-02-06 14:06:39 +0200233
Tomas Winkler7cb035d2013-03-10 13:56:08 +0200234 dev_err(&pdev->dev, "stop\n");
235 mei_stop(dev);
Tomas Winkler2703d4b2013-02-06 14:06:39 +0200236
Tomas Winkler2703d4b2013-02-06 14:06:39 +0200237 /* disable interrupts */
238 mei_disable_interrupts(dev);
239
240 free_irq(pdev->irq, dev);
241 pci_disable_msi(pdev);
242 pci_set_drvdata(pdev, NULL);
243
Tomas Winkler52c34562013-02-06 14:06:40 +0200244 if (hw->mem_addr)
245 pci_iounmap(pdev, hw->mem_addr);
Tomas Winkler2703d4b2013-02-06 14:06:39 +0200246
Tomas Winkler30e53bb2013-04-05 22:10:34 +0300247 mei_deregister(dev);
248
Tomas Winkler2703d4b2013-02-06 14:06:39 +0200249 kfree(dev);
250
251 pci_release_regions(pdev);
252 pci_disable_device(pdev);
253
Tomas Winkler2703d4b2013-02-06 14:06:39 +0200254
255}
256#ifdef CONFIG_PM
Tomas Winklerb68301e2013-03-27 16:58:29 +0200257static int mei_me_pci_suspend(struct device *device)
Tomas Winkler2703d4b2013-02-06 14:06:39 +0200258{
259 struct pci_dev *pdev = to_pci_dev(device);
260 struct mei_device *dev = pci_get_drvdata(pdev);
Tomas Winkler2703d4b2013-02-06 14:06:39 +0200261
262 if (!dev)
263 return -ENODEV;
Tomas Winkler2703d4b2013-02-06 14:06:39 +0200264
Tomas Winkler7cb035d2013-03-10 13:56:08 +0200265 dev_err(&pdev->dev, "suspend\n");
Tomas Winkler2703d4b2013-02-06 14:06:39 +0200266
Tomas Winkler7cb035d2013-03-10 13:56:08 +0200267 mei_stop(dev);
268
269 mei_disable_interrupts(dev);
Tomas Winkler2703d4b2013-02-06 14:06:39 +0200270
271 free_irq(pdev->irq, dev);
272 pci_disable_msi(pdev);
273
Tomas Winkler7cb035d2013-03-10 13:56:08 +0200274 return 0;
Tomas Winkler2703d4b2013-02-06 14:06:39 +0200275}
276
Tomas Winklerb68301e2013-03-27 16:58:29 +0200277static int mei_me_pci_resume(struct device *device)
Tomas Winkler2703d4b2013-02-06 14:06:39 +0200278{
279 struct pci_dev *pdev = to_pci_dev(device);
280 struct mei_device *dev;
281 int err;
282
283 dev = pci_get_drvdata(pdev);
284 if (!dev)
285 return -ENODEV;
286
287 pci_enable_msi(pdev);
288
289 /* request and enable interrupt */
290 if (pci_dev_msi_enabled(pdev))
291 err = request_threaded_irq(pdev->irq,
292 NULL,
Tomas Winkler06ecd642013-02-06 14:06:42 +0200293 mei_me_irq_thread_handler,
Tomas Winkler2703d4b2013-02-06 14:06:39 +0200294 IRQF_ONESHOT, KBUILD_MODNAME, dev);
295 else
296 err = request_threaded_irq(pdev->irq,
Tomas Winkler06ecd642013-02-06 14:06:42 +0200297 mei_me_irq_quick_handler,
298 mei_me_irq_thread_handler,
Tomas Winkler2703d4b2013-02-06 14:06:39 +0200299 IRQF_SHARED, KBUILD_MODNAME, dev);
300
301 if (err) {
302 dev_err(&pdev->dev, "request_threaded_irq failed: irq = %d.\n",
303 pdev->irq);
304 return err;
305 }
306
307 mutex_lock(&dev->device_lock);
308 dev->dev_state = MEI_DEV_POWER_UP;
Tomas Winkler42f132f2013-06-05 10:51:13 +0300309 mei_clear_interrupts(dev);
Tomas Winkler2703d4b2013-02-06 14:06:39 +0200310 mei_reset(dev, 1);
311 mutex_unlock(&dev->device_lock);
312
313 /* Start timer if stopped in suspend */
314 schedule_delayed_work(&dev->timer_work, HZ);
315
316 return err;
317}
Tomas Winklerb68301e2013-03-27 16:58:29 +0200318static SIMPLE_DEV_PM_OPS(mei_me_pm_ops, mei_me_pci_suspend, mei_me_pci_resume);
319#define MEI_ME_PM_OPS (&mei_me_pm_ops)
Tomas Winkler2703d4b2013-02-06 14:06:39 +0200320#else
Tomas Winklerb68301e2013-03-27 16:58:29 +0200321#define MEI_ME_PM_OPS NULL
Tomas Winkler2703d4b2013-02-06 14:06:39 +0200322#endif /* CONFIG_PM */
323/*
324 * PCI driver structure
325 */
Tomas Winklerb68301e2013-03-27 16:58:29 +0200326static struct pci_driver mei_me_driver = {
Tomas Winkler2703d4b2013-02-06 14:06:39 +0200327 .name = KBUILD_MODNAME,
Tomas Winklerb68301e2013-03-27 16:58:29 +0200328 .id_table = mei_me_pci_tbl,
329 .probe = mei_me_probe,
330 .remove = mei_me_remove,
331 .shutdown = mei_me_remove,
332 .driver.pm = MEI_ME_PM_OPS,
Tomas Winkler2703d4b2013-02-06 14:06:39 +0200333};
334
Tomas Winklerb68301e2013-03-27 16:58:29 +0200335module_pci_driver(mei_me_driver);
Tomas Winkler2703d4b2013-02-06 14:06:39 +0200336
337MODULE_AUTHOR("Intel Corporation");
338MODULE_DESCRIPTION("Intel(R) Management Engine Interface");
339MODULE_LICENSE("GPL v2");