pci-me.c 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393
  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. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  17. #include <linux/module.h>
  18. #include <linux/moduleparam.h>
  19. #include <linux/kernel.h>
  20. #include <linux/device.h>
  21. #include <linux/fs.h>
  22. #include <linux/errno.h>
  23. #include <linux/types.h>
  24. #include <linux/fcntl.h>
  25. #include <linux/aio.h>
  26. #include <linux/pci.h>
  27. #include <linux/poll.h>
  28. #include <linux/init.h>
  29. #include <linux/ioctl.h>
  30. #include <linux/cdev.h>
  31. #include <linux/sched.h>
  32. #include <linux/uuid.h>
  33. #include <linux/compat.h>
  34. #include <linux/jiffies.h>
  35. #include <linux/interrupt.h>
  36. #include <linux/miscdevice.h>
  37. #include <linux/mei.h>
  38. #include "mei_dev.h"
  39. #include "hw-me.h"
  40. #include "client.h"
  41. /* AMT device is a singleton on the platform */
  42. static struct pci_dev *mei_pdev;
  43. /* mei_pci_tbl - PCI Device ID Table */
  44. static DEFINE_PCI_DEVICE_TABLE(mei_pci_tbl) = {
  45. {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MEI_DEV_ID_82946GZ)},
  46. {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MEI_DEV_ID_82G35)},
  47. {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MEI_DEV_ID_82Q965)},
  48. {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MEI_DEV_ID_82G965)},
  49. {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MEI_DEV_ID_82GM965)},
  50. {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MEI_DEV_ID_82GME965)},
  51. {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MEI_DEV_ID_ICH9_82Q35)},
  52. {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MEI_DEV_ID_ICH9_82G33)},
  53. {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MEI_DEV_ID_ICH9_82Q33)},
  54. {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MEI_DEV_ID_ICH9_82X38)},
  55. {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MEI_DEV_ID_ICH9_3200)},
  56. {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MEI_DEV_ID_ICH9_6)},
  57. {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MEI_DEV_ID_ICH9_7)},
  58. {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MEI_DEV_ID_ICH9_8)},
  59. {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MEI_DEV_ID_ICH9_9)},
  60. {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MEI_DEV_ID_ICH9_10)},
  61. {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MEI_DEV_ID_ICH9M_1)},
  62. {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MEI_DEV_ID_ICH9M_2)},
  63. {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MEI_DEV_ID_ICH9M_3)},
  64. {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MEI_DEV_ID_ICH9M_4)},
  65. {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MEI_DEV_ID_ICH10_1)},
  66. {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MEI_DEV_ID_ICH10_2)},
  67. {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MEI_DEV_ID_ICH10_3)},
  68. {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MEI_DEV_ID_ICH10_4)},
  69. {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MEI_DEV_ID_IBXPK_1)},
  70. {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MEI_DEV_ID_IBXPK_2)},
  71. {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MEI_DEV_ID_CPT_1)},
  72. {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MEI_DEV_ID_PBG_1)},
  73. {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MEI_DEV_ID_PPT_1)},
  74. {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MEI_DEV_ID_PPT_2)},
  75. {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MEI_DEV_ID_PPT_3)},
  76. {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MEI_DEV_ID_LPT)},
  77. {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MEI_DEV_ID_LPT_LP)},
  78. /* required last entry */
  79. {0, }
  80. };
  81. MODULE_DEVICE_TABLE(pci, mei_pci_tbl);
  82. static DEFINE_MUTEX(mei_mutex);
  83. /**
  84. * mei_quirk_probe - probe for devices that doesn't valid ME interface
  85. * @pdev: PCI device structure
  86. * @ent: entry into pci_device_table
  87. *
  88. * returns true if ME Interface is valid, false otherwise
  89. */
  90. static bool mei_quirk_probe(struct pci_dev *pdev,
  91. const struct pci_device_id *ent)
  92. {
  93. u32 reg;
  94. if (ent->device == MEI_DEV_ID_PBG_1) {
  95. pci_read_config_dword(pdev, 0x48, &reg);
  96. /* make sure that bit 9 is up and bit 10 is down */
  97. if ((reg & 0x600) == 0x200) {
  98. dev_info(&pdev->dev, "Device doesn't have valid ME Interface\n");
  99. return false;
  100. }
  101. }
  102. return true;
  103. }
  104. /**
  105. * mei_probe - Device Initialization Routine
  106. *
  107. * @pdev: PCI device structure
  108. * @ent: entry in kcs_pci_tbl
  109. *
  110. * returns 0 on success, <0 on failure.
  111. */
  112. static int mei_probe(struct pci_dev *pdev,
  113. const struct pci_device_id *ent)
  114. {
  115. struct mei_device *dev;
  116. int err;
  117. mutex_lock(&mei_mutex);
  118. if (!mei_quirk_probe(pdev, ent)) {
  119. err = -ENODEV;
  120. goto end;
  121. }
  122. if (mei_pdev) {
  123. err = -EEXIST;
  124. goto end;
  125. }
  126. /* enable pci dev */
  127. err = pci_enable_device(pdev);
  128. if (err) {
  129. dev_err(&pdev->dev, "failed to enable pci device.\n");
  130. goto end;
  131. }
  132. /* set PCI host mastering */
  133. pci_set_master(pdev);
  134. /* pci request regions for mei driver */
  135. err = pci_request_regions(pdev, KBUILD_MODNAME);
  136. if (err) {
  137. dev_err(&pdev->dev, "failed to get pci regions.\n");
  138. goto disable_device;
  139. }
  140. /* allocates and initializes the mei dev structure */
  141. dev = mei_device_init(pdev);
  142. if (!dev) {
  143. err = -ENOMEM;
  144. goto release_regions;
  145. }
  146. /* mapping IO device memory */
  147. dev->mem_addr = pci_iomap(pdev, 0, 0);
  148. if (!dev->mem_addr) {
  149. dev_err(&pdev->dev, "mapping I/O device memory failure.\n");
  150. err = -ENOMEM;
  151. goto free_device;
  152. }
  153. pci_enable_msi(pdev);
  154. /* request and enable interrupt */
  155. if (pci_dev_msi_enabled(pdev))
  156. err = request_threaded_irq(pdev->irq,
  157. NULL,
  158. mei_interrupt_thread_handler,
  159. IRQF_ONESHOT, KBUILD_MODNAME, dev);
  160. else
  161. err = request_threaded_irq(pdev->irq,
  162. mei_interrupt_quick_handler,
  163. mei_interrupt_thread_handler,
  164. IRQF_SHARED, KBUILD_MODNAME, dev);
  165. if (err) {
  166. dev_err(&pdev->dev, "request_threaded_irq failure. irq = %d\n",
  167. pdev->irq);
  168. goto disable_msi;
  169. }
  170. if (mei_hw_init(dev)) {
  171. dev_err(&pdev->dev, "init hw failure.\n");
  172. err = -ENODEV;
  173. goto release_irq;
  174. }
  175. err = mei_register(&pdev->dev);
  176. if (err)
  177. goto release_irq;
  178. mei_pdev = pdev;
  179. pci_set_drvdata(pdev, dev);
  180. schedule_delayed_work(&dev->timer_work, HZ);
  181. mutex_unlock(&mei_mutex);
  182. pr_debug("initialization successful.\n");
  183. return 0;
  184. release_irq:
  185. mei_disable_interrupts(dev);
  186. flush_scheduled_work();
  187. free_irq(pdev->irq, dev);
  188. disable_msi:
  189. pci_disable_msi(pdev);
  190. pci_iounmap(pdev, dev->mem_addr);
  191. free_device:
  192. kfree(dev);
  193. release_regions:
  194. pci_release_regions(pdev);
  195. disable_device:
  196. pci_disable_device(pdev);
  197. end:
  198. mutex_unlock(&mei_mutex);
  199. dev_err(&pdev->dev, "initialization failed.\n");
  200. return err;
  201. }
  202. /**
  203. * mei_remove - Device Removal Routine
  204. *
  205. * @pdev: PCI device structure
  206. *
  207. * mei_remove is called by the PCI subsystem to alert the driver
  208. * that it should release a PCI device.
  209. */
  210. static void mei_remove(struct pci_dev *pdev)
  211. {
  212. struct mei_device *dev;
  213. if (mei_pdev != pdev)
  214. return;
  215. dev = pci_get_drvdata(pdev);
  216. if (!dev)
  217. return;
  218. mutex_lock(&dev->device_lock);
  219. cancel_delayed_work(&dev->timer_work);
  220. mei_wd_stop(dev);
  221. mei_pdev = NULL;
  222. if (dev->iamthif_cl.state == MEI_FILE_CONNECTED) {
  223. dev->iamthif_cl.state = MEI_FILE_DISCONNECTING;
  224. mei_cl_disconnect(&dev->iamthif_cl);
  225. }
  226. if (dev->wd_cl.state == MEI_FILE_CONNECTED) {
  227. dev->wd_cl.state = MEI_FILE_DISCONNECTING;
  228. mei_cl_disconnect(&dev->wd_cl);
  229. }
  230. /* Unregistering watchdog device */
  231. mei_watchdog_unregister(dev);
  232. /* remove entry if already in list */
  233. dev_dbg(&pdev->dev, "list del iamthif and wd file list.\n");
  234. if (dev->open_handle_count > 0)
  235. dev->open_handle_count--;
  236. mei_cl_unlink(&dev->wd_cl);
  237. if (dev->open_handle_count > 0)
  238. dev->open_handle_count--;
  239. mei_cl_unlink(&dev->iamthif_cl);
  240. dev->iamthif_current_cb = NULL;
  241. dev->me_clients_num = 0;
  242. mutex_unlock(&dev->device_lock);
  243. flush_scheduled_work();
  244. /* disable interrupts */
  245. mei_disable_interrupts(dev);
  246. free_irq(pdev->irq, dev);
  247. pci_disable_msi(pdev);
  248. pci_set_drvdata(pdev, NULL);
  249. if (dev->mem_addr)
  250. pci_iounmap(pdev, dev->mem_addr);
  251. kfree(dev);
  252. pci_release_regions(pdev);
  253. pci_disable_device(pdev);
  254. mei_deregister();
  255. }
  256. #ifdef CONFIG_PM
  257. static int mei_pci_suspend(struct device *device)
  258. {
  259. struct pci_dev *pdev = to_pci_dev(device);
  260. struct mei_device *dev = pci_get_drvdata(pdev);
  261. int err;
  262. if (!dev)
  263. return -ENODEV;
  264. mutex_lock(&dev->device_lock);
  265. cancel_delayed_work(&dev->timer_work);
  266. /* Stop watchdog if exists */
  267. err = mei_wd_stop(dev);
  268. /* Set new mei state */
  269. if (dev->dev_state == MEI_DEV_ENABLED ||
  270. dev->dev_state == MEI_DEV_RECOVERING_FROM_RESET) {
  271. dev->dev_state = MEI_DEV_POWER_DOWN;
  272. mei_reset(dev, 0);
  273. }
  274. mutex_unlock(&dev->device_lock);
  275. free_irq(pdev->irq, dev);
  276. pci_disable_msi(pdev);
  277. return err;
  278. }
  279. static int mei_pci_resume(struct device *device)
  280. {
  281. struct pci_dev *pdev = to_pci_dev(device);
  282. struct mei_device *dev;
  283. int err;
  284. dev = pci_get_drvdata(pdev);
  285. if (!dev)
  286. return -ENODEV;
  287. pci_enable_msi(pdev);
  288. /* request and enable interrupt */
  289. if (pci_dev_msi_enabled(pdev))
  290. err = request_threaded_irq(pdev->irq,
  291. NULL,
  292. mei_interrupt_thread_handler,
  293. IRQF_ONESHOT, KBUILD_MODNAME, dev);
  294. else
  295. err = request_threaded_irq(pdev->irq,
  296. mei_interrupt_quick_handler,
  297. mei_interrupt_thread_handler,
  298. IRQF_SHARED, KBUILD_MODNAME, dev);
  299. if (err) {
  300. dev_err(&pdev->dev, "request_threaded_irq failed: irq = %d.\n",
  301. pdev->irq);
  302. return err;
  303. }
  304. mutex_lock(&dev->device_lock);
  305. dev->dev_state = MEI_DEV_POWER_UP;
  306. mei_reset(dev, 1);
  307. mutex_unlock(&dev->device_lock);
  308. /* Start timer if stopped in suspend */
  309. schedule_delayed_work(&dev->timer_work, HZ);
  310. return err;
  311. }
  312. static SIMPLE_DEV_PM_OPS(mei_pm_ops, mei_pci_suspend, mei_pci_resume);
  313. #define MEI_PM_OPS (&mei_pm_ops)
  314. #else
  315. #define MEI_PM_OPS NULL
  316. #endif /* CONFIG_PM */
  317. /*
  318. * PCI driver structure
  319. */
  320. static struct pci_driver mei_driver = {
  321. .name = KBUILD_MODNAME,
  322. .id_table = mei_pci_tbl,
  323. .probe = mei_probe,
  324. .remove = mei_remove,
  325. .shutdown = mei_remove,
  326. .driver.pm = MEI_PM_OPS,
  327. };
  328. module_pci_driver(mei_driver);
  329. MODULE_AUTHOR("Intel Corporation");
  330. MODULE_DESCRIPTION("Intel(R) Management Engine Interface");
  331. MODULE_LICENSE("GPL v2");