blob: df1750651bf8895240dab92da382a01a5e483c91 [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
46/* AMT device is a singleton on the platform */
47static struct pci_dev *mei_pdev;
48
49/* mei_pci_tbl - PCI Device ID Table */
50static DEFINE_PCI_DEVICE_TABLE(mei_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)},
83 {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MEI_DEV_ID_LPT_LP)},
84
85 /* required last entry */
86 {0, }
87};
88
89MODULE_DEVICE_TABLE(pci, mei_pci_tbl);
90
91static DEFINE_MUTEX(mei_mutex);
92
Tomas Winkler2703d4b2013-02-06 14:06:39 +020093/**
94 * mei_quirk_probe - probe for devices that doesn't valid ME interface
Masanari Iida393b1482013-04-05 01:05:05 +090095 *
Tomas Winkler2703d4b2013-02-06 14:06:39 +020096 * @pdev: PCI device structure
97 * @ent: entry into pci_device_table
98 *
99 * returns true if ME Interface is valid, false otherwise
100 */
101static bool mei_quirk_probe(struct pci_dev *pdev,
102 const struct pci_device_id *ent)
103{
104 u32 reg;
105 if (ent->device == MEI_DEV_ID_PBG_1) {
106 pci_read_config_dword(pdev, 0x48, &reg);
107 /* make sure that bit 9 is up and bit 10 is down */
108 if ((reg & 0x600) == 0x200) {
109 dev_info(&pdev->dev, "Device doesn't have valid ME Interface\n");
110 return false;
111 }
112 }
113 return true;
114}
115/**
116 * mei_probe - Device Initialization Routine
117 *
118 * @pdev: PCI device structure
119 * @ent: entry in kcs_pci_tbl
120 *
121 * returns 0 on success, <0 on failure.
122 */
Tomas Winkler52c34562013-02-06 14:06:40 +0200123static int mei_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
Tomas Winkler2703d4b2013-02-06 14:06:39 +0200124{
125 struct mei_device *dev;
Tomas Winkler52c34562013-02-06 14:06:40 +0200126 struct mei_me_hw *hw;
Tomas Winkler2703d4b2013-02-06 14:06:39 +0200127 int err;
128
129 mutex_lock(&mei_mutex);
130
131 if (!mei_quirk_probe(pdev, ent)) {
132 err = -ENODEV;
133 goto end;
134 }
135
136 if (mei_pdev) {
137 err = -EEXIST;
138 goto end;
139 }
140 /* enable pci dev */
141 err = pci_enable_device(pdev);
142 if (err) {
143 dev_err(&pdev->dev, "failed to enable pci device.\n");
144 goto end;
145 }
146 /* set PCI host mastering */
147 pci_set_master(pdev);
148 /* pci request regions for mei driver */
149 err = pci_request_regions(pdev, KBUILD_MODNAME);
150 if (err) {
151 dev_err(&pdev->dev, "failed to get pci regions.\n");
152 goto disable_device;
153 }
154 /* allocates and initializes the mei dev structure */
Tomas Winkler52c34562013-02-06 14:06:40 +0200155 dev = mei_me_dev_init(pdev);
Tomas Winkler2703d4b2013-02-06 14:06:39 +0200156 if (!dev) {
157 err = -ENOMEM;
158 goto release_regions;
159 }
Tomas Winkler52c34562013-02-06 14:06:40 +0200160 hw = to_me_hw(dev);
Tomas Winkler2703d4b2013-02-06 14:06:39 +0200161 /* mapping IO device memory */
Tomas Winkler52c34562013-02-06 14:06:40 +0200162 hw->mem_addr = pci_iomap(pdev, 0, 0);
163 if (!hw->mem_addr) {
Tomas Winkler2703d4b2013-02-06 14:06:39 +0200164 dev_err(&pdev->dev, "mapping I/O device memory failure.\n");
165 err = -ENOMEM;
166 goto free_device;
167 }
168 pci_enable_msi(pdev);
169
170 /* request and enable interrupt */
171 if (pci_dev_msi_enabled(pdev))
172 err = request_threaded_irq(pdev->irq,
173 NULL,
Tomas Winkler06ecd642013-02-06 14:06:42 +0200174 mei_me_irq_thread_handler,
Tomas Winkler2703d4b2013-02-06 14:06:39 +0200175 IRQF_ONESHOT, KBUILD_MODNAME, dev);
176 else
177 err = request_threaded_irq(pdev->irq,
Tomas Winkler06ecd642013-02-06 14:06:42 +0200178 mei_me_irq_quick_handler,
179 mei_me_irq_thread_handler,
Tomas Winkler2703d4b2013-02-06 14:06:39 +0200180 IRQF_SHARED, KBUILD_MODNAME, dev);
181
182 if (err) {
183 dev_err(&pdev->dev, "request_threaded_irq failure. irq = %d\n",
184 pdev->irq);
185 goto disable_msi;
186 }
187
188 if (mei_hw_init(dev)) {
189 dev_err(&pdev->dev, "init hw failure.\n");
190 err = -ENODEV;
191 goto release_irq;
192 }
193
194 err = mei_register(&pdev->dev);
195 if (err)
196 goto release_irq;
197
198 mei_pdev = pdev;
199 pci_set_drvdata(pdev, dev);
200
201
202 schedule_delayed_work(&dev->timer_work, HZ);
203
204 mutex_unlock(&mei_mutex);
205
206 pr_debug("initialization successful.\n");
207
208 return 0;
209
210release_irq:
211 mei_disable_interrupts(dev);
212 flush_scheduled_work();
213 free_irq(pdev->irq, dev);
214disable_msi:
215 pci_disable_msi(pdev);
Tomas Winkler52c34562013-02-06 14:06:40 +0200216 pci_iounmap(pdev, hw->mem_addr);
Tomas Winkler2703d4b2013-02-06 14:06:39 +0200217free_device:
218 kfree(dev);
219release_regions:
220 pci_release_regions(pdev);
221disable_device:
222 pci_disable_device(pdev);
223end:
224 mutex_unlock(&mei_mutex);
225 dev_err(&pdev->dev, "initialization failed.\n");
226 return err;
227}
228
229/**
230 * mei_remove - Device Removal Routine
231 *
232 * @pdev: PCI device structure
233 *
234 * mei_remove is called by the PCI subsystem to alert the driver
235 * that it should release a PCI device.
236 */
237static void mei_remove(struct pci_dev *pdev)
238{
239 struct mei_device *dev;
Tomas Winkler52c34562013-02-06 14:06:40 +0200240 struct mei_me_hw *hw;
Tomas Winkler2703d4b2013-02-06 14:06:39 +0200241
242 if (mei_pdev != pdev)
243 return;
244
245 dev = pci_get_drvdata(pdev);
246 if (!dev)
247 return;
248
Tomas Winkler52c34562013-02-06 14:06:40 +0200249 hw = to_me_hw(dev);
250
Tomas Winkler2703d4b2013-02-06 14:06:39 +0200251 mutex_lock(&dev->device_lock);
252
253 cancel_delayed_work(&dev->timer_work);
254
255 mei_wd_stop(dev);
256
257 mei_pdev = NULL;
258
259 if (dev->iamthif_cl.state == MEI_FILE_CONNECTED) {
260 dev->iamthif_cl.state = MEI_FILE_DISCONNECTING;
261 mei_cl_disconnect(&dev->iamthif_cl);
262 }
263 if (dev->wd_cl.state == MEI_FILE_CONNECTED) {
264 dev->wd_cl.state = MEI_FILE_DISCONNECTING;
265 mei_cl_disconnect(&dev->wd_cl);
266 }
267
268 /* Unregistering watchdog device */
269 mei_watchdog_unregister(dev);
270
271 /* remove entry if already in list */
272 dev_dbg(&pdev->dev, "list del iamthif and wd file list.\n");
273
274 if (dev->open_handle_count > 0)
275 dev->open_handle_count--;
276 mei_cl_unlink(&dev->wd_cl);
277
278 if (dev->open_handle_count > 0)
279 dev->open_handle_count--;
280 mei_cl_unlink(&dev->iamthif_cl);
281
282 dev->iamthif_current_cb = NULL;
283 dev->me_clients_num = 0;
284
285 mutex_unlock(&dev->device_lock);
286
287 flush_scheduled_work();
288
289 /* disable interrupts */
290 mei_disable_interrupts(dev);
291
292 free_irq(pdev->irq, dev);
293 pci_disable_msi(pdev);
294 pci_set_drvdata(pdev, NULL);
295
Tomas Winkler52c34562013-02-06 14:06:40 +0200296 if (hw->mem_addr)
297 pci_iounmap(pdev, hw->mem_addr);
Tomas Winkler2703d4b2013-02-06 14:06:39 +0200298
299 kfree(dev);
300
301 pci_release_regions(pdev);
302 pci_disable_device(pdev);
303
304 mei_deregister();
305
306}
307#ifdef CONFIG_PM
308static int mei_pci_suspend(struct device *device)
309{
310 struct pci_dev *pdev = to_pci_dev(device);
311 struct mei_device *dev = pci_get_drvdata(pdev);
312 int err;
313
314 if (!dev)
315 return -ENODEV;
316 mutex_lock(&dev->device_lock);
317
318 cancel_delayed_work(&dev->timer_work);
319
320 /* Stop watchdog if exists */
321 err = mei_wd_stop(dev);
322 /* Set new mei state */
323 if (dev->dev_state == MEI_DEV_ENABLED ||
324 dev->dev_state == MEI_DEV_RECOVERING_FROM_RESET) {
325 dev->dev_state = MEI_DEV_POWER_DOWN;
326 mei_reset(dev, 0);
327 }
328 mutex_unlock(&dev->device_lock);
329
330 free_irq(pdev->irq, dev);
331 pci_disable_msi(pdev);
332
333 return err;
334}
335
336static int mei_pci_resume(struct device *device)
337{
338 struct pci_dev *pdev = to_pci_dev(device);
339 struct mei_device *dev;
340 int err;
341
342 dev = pci_get_drvdata(pdev);
343 if (!dev)
344 return -ENODEV;
345
346 pci_enable_msi(pdev);
347
348 /* request and enable interrupt */
349 if (pci_dev_msi_enabled(pdev))
350 err = request_threaded_irq(pdev->irq,
351 NULL,
Tomas Winkler06ecd642013-02-06 14:06:42 +0200352 mei_me_irq_thread_handler,
Tomas Winkler2703d4b2013-02-06 14:06:39 +0200353 IRQF_ONESHOT, KBUILD_MODNAME, dev);
354 else
355 err = request_threaded_irq(pdev->irq,
Tomas Winkler06ecd642013-02-06 14:06:42 +0200356 mei_me_irq_quick_handler,
357 mei_me_irq_thread_handler,
Tomas Winkler2703d4b2013-02-06 14:06:39 +0200358 IRQF_SHARED, KBUILD_MODNAME, dev);
359
360 if (err) {
361 dev_err(&pdev->dev, "request_threaded_irq failed: irq = %d.\n",
362 pdev->irq);
363 return err;
364 }
365
366 mutex_lock(&dev->device_lock);
367 dev->dev_state = MEI_DEV_POWER_UP;
368 mei_reset(dev, 1);
369 mutex_unlock(&dev->device_lock);
370
371 /* Start timer if stopped in suspend */
372 schedule_delayed_work(&dev->timer_work, HZ);
373
374 return err;
375}
376static SIMPLE_DEV_PM_OPS(mei_pm_ops, mei_pci_suspend, mei_pci_resume);
377#define MEI_PM_OPS (&mei_pm_ops)
378#else
379#define MEI_PM_OPS NULL
380#endif /* CONFIG_PM */
381/*
382 * PCI driver structure
383 */
384static struct pci_driver mei_driver = {
385 .name = KBUILD_MODNAME,
386 .id_table = mei_pci_tbl,
387 .probe = mei_probe,
388 .remove = mei_remove,
389 .shutdown = mei_remove,
390 .driver.pm = MEI_PM_OPS,
391};
392
393module_pci_driver(mei_driver);
394
395MODULE_AUTHOR("Intel Corporation");
396MODULE_DESCRIPTION("Intel(R) Management Engine Interface");
397MODULE_LICENSE("GPL v2");