pci-me.c 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338
  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. /* mei_pci_tbl - PCI Device ID Table */
  42. static DEFINE_PCI_DEVICE_TABLE(mei_me_pci_tbl) = {
  43. {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MEI_DEV_ID_82946GZ)},
  44. {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MEI_DEV_ID_82G35)},
  45. {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MEI_DEV_ID_82Q965)},
  46. {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MEI_DEV_ID_82G965)},
  47. {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MEI_DEV_ID_82GM965)},
  48. {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MEI_DEV_ID_82GME965)},
  49. {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MEI_DEV_ID_ICH9_82Q35)},
  50. {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MEI_DEV_ID_ICH9_82G33)},
  51. {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MEI_DEV_ID_ICH9_82Q33)},
  52. {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MEI_DEV_ID_ICH9_82X38)},
  53. {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MEI_DEV_ID_ICH9_3200)},
  54. {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MEI_DEV_ID_ICH9_6)},
  55. {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MEI_DEV_ID_ICH9_7)},
  56. {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MEI_DEV_ID_ICH9_8)},
  57. {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MEI_DEV_ID_ICH9_9)},
  58. {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MEI_DEV_ID_ICH9_10)},
  59. {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MEI_DEV_ID_ICH9M_1)},
  60. {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MEI_DEV_ID_ICH9M_2)},
  61. {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MEI_DEV_ID_ICH9M_3)},
  62. {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MEI_DEV_ID_ICH9M_4)},
  63. {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MEI_DEV_ID_ICH10_1)},
  64. {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MEI_DEV_ID_ICH10_2)},
  65. {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MEI_DEV_ID_ICH10_3)},
  66. {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MEI_DEV_ID_ICH10_4)},
  67. {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MEI_DEV_ID_IBXPK_1)},
  68. {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MEI_DEV_ID_IBXPK_2)},
  69. {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MEI_DEV_ID_CPT_1)},
  70. {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MEI_DEV_ID_PBG_1)},
  71. {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MEI_DEV_ID_PPT_1)},
  72. {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MEI_DEV_ID_PPT_2)},
  73. {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MEI_DEV_ID_PPT_3)},
  74. {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MEI_DEV_ID_LPT)},
  75. {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MEI_DEV_ID_LPT_LP)},
  76. /* required last entry */
  77. {0, }
  78. };
  79. MODULE_DEVICE_TABLE(pci, mei_me_pci_tbl);
  80. /**
  81. * mei_quirk_probe - probe for devices that doesn't valid ME interface
  82. *
  83. * @pdev: PCI device structure
  84. * @ent: entry into pci_device_table
  85. *
  86. * returns true if ME Interface is valid, false otherwise
  87. */
  88. static bool mei_me_quirk_probe(struct pci_dev *pdev,
  89. const struct pci_device_id *ent)
  90. {
  91. u32 reg;
  92. if (ent->device == MEI_DEV_ID_PBG_1) {
  93. pci_read_config_dword(pdev, 0x48, &reg);
  94. /* make sure that bit 9 is up and bit 10 is down */
  95. if ((reg & 0x600) == 0x200) {
  96. dev_info(&pdev->dev, "Device doesn't have valid ME Interface\n");
  97. return false;
  98. }
  99. }
  100. return true;
  101. }
  102. /**
  103. * mei_probe - Device Initialization Routine
  104. *
  105. * @pdev: PCI device structure
  106. * @ent: entry in kcs_pci_tbl
  107. *
  108. * returns 0 on success, <0 on failure.
  109. */
  110. static int mei_me_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
  111. {
  112. struct mei_device *dev;
  113. struct mei_me_hw *hw;
  114. int err;
  115. if (!mei_me_quirk_probe(pdev, ent)) {
  116. err = -ENODEV;
  117. goto end;
  118. }
  119. /* enable pci dev */
  120. err = pci_enable_device(pdev);
  121. if (err) {
  122. dev_err(&pdev->dev, "failed to enable pci device.\n");
  123. goto end;
  124. }
  125. /* set PCI host mastering */
  126. pci_set_master(pdev);
  127. /* pci request regions for mei driver */
  128. err = pci_request_regions(pdev, KBUILD_MODNAME);
  129. if (err) {
  130. dev_err(&pdev->dev, "failed to get pci regions.\n");
  131. goto disable_device;
  132. }
  133. /* allocates and initializes the mei dev structure */
  134. dev = mei_me_dev_init(pdev);
  135. if (!dev) {
  136. err = -ENOMEM;
  137. goto release_regions;
  138. }
  139. hw = to_me_hw(dev);
  140. /* mapping IO device memory */
  141. hw->mem_addr = pci_iomap(pdev, 0, 0);
  142. if (!hw->mem_addr) {
  143. dev_err(&pdev->dev, "mapping I/O device memory failure.\n");
  144. err = -ENOMEM;
  145. goto free_device;
  146. }
  147. pci_enable_msi(pdev);
  148. /* request and enable interrupt */
  149. if (pci_dev_msi_enabled(pdev))
  150. err = request_threaded_irq(pdev->irq,
  151. NULL,
  152. mei_me_irq_thread_handler,
  153. IRQF_ONESHOT, KBUILD_MODNAME, dev);
  154. else
  155. err = request_threaded_irq(pdev->irq,
  156. mei_me_irq_quick_handler,
  157. mei_me_irq_thread_handler,
  158. IRQF_SHARED, KBUILD_MODNAME, dev);
  159. if (err) {
  160. dev_err(&pdev->dev, "request_threaded_irq failure. irq = %d\n",
  161. pdev->irq);
  162. goto disable_msi;
  163. }
  164. if (mei_start(dev)) {
  165. dev_err(&pdev->dev, "init hw failure.\n");
  166. err = -ENODEV;
  167. goto release_irq;
  168. }
  169. err = mei_register(dev);
  170. if (err)
  171. goto release_irq;
  172. pci_set_drvdata(pdev, dev);
  173. schedule_delayed_work(&dev->timer_work, HZ);
  174. pr_debug("initialization successful.\n");
  175. return 0;
  176. release_irq:
  177. mei_disable_interrupts(dev);
  178. flush_scheduled_work();
  179. free_irq(pdev->irq, dev);
  180. disable_msi:
  181. pci_disable_msi(pdev);
  182. pci_iounmap(pdev, hw->mem_addr);
  183. free_device:
  184. kfree(dev);
  185. release_regions:
  186. pci_release_regions(pdev);
  187. disable_device:
  188. pci_disable_device(pdev);
  189. end:
  190. dev_err(&pdev->dev, "initialization failed.\n");
  191. return err;
  192. }
  193. /**
  194. * mei_remove - Device Removal Routine
  195. *
  196. * @pdev: PCI device structure
  197. *
  198. * mei_remove is called by the PCI subsystem to alert the driver
  199. * that it should release a PCI device.
  200. */
  201. static void mei_me_remove(struct pci_dev *pdev)
  202. {
  203. struct mei_device *dev;
  204. struct mei_me_hw *hw;
  205. dev = pci_get_drvdata(pdev);
  206. if (!dev)
  207. return;
  208. hw = to_me_hw(dev);
  209. dev_err(&pdev->dev, "stop\n");
  210. mei_stop(dev);
  211. /* disable interrupts */
  212. mei_disable_interrupts(dev);
  213. free_irq(pdev->irq, dev);
  214. pci_disable_msi(pdev);
  215. if (hw->mem_addr)
  216. pci_iounmap(pdev, hw->mem_addr);
  217. mei_deregister(dev);
  218. kfree(dev);
  219. pci_release_regions(pdev);
  220. pci_disable_device(pdev);
  221. }
  222. #ifdef CONFIG_PM
  223. static int mei_me_pci_suspend(struct device *device)
  224. {
  225. struct pci_dev *pdev = to_pci_dev(device);
  226. struct mei_device *dev = pci_get_drvdata(pdev);
  227. if (!dev)
  228. return -ENODEV;
  229. dev_err(&pdev->dev, "suspend\n");
  230. mei_stop(dev);
  231. mei_disable_interrupts(dev);
  232. free_irq(pdev->irq, dev);
  233. pci_disable_msi(pdev);
  234. return 0;
  235. }
  236. static int mei_me_pci_resume(struct device *device)
  237. {
  238. struct pci_dev *pdev = to_pci_dev(device);
  239. struct mei_device *dev;
  240. int err;
  241. dev = pci_get_drvdata(pdev);
  242. if (!dev)
  243. return -ENODEV;
  244. pci_enable_msi(pdev);
  245. /* request and enable interrupt */
  246. if (pci_dev_msi_enabled(pdev))
  247. err = request_threaded_irq(pdev->irq,
  248. NULL,
  249. mei_me_irq_thread_handler,
  250. IRQF_ONESHOT, KBUILD_MODNAME, dev);
  251. else
  252. err = request_threaded_irq(pdev->irq,
  253. mei_me_irq_quick_handler,
  254. mei_me_irq_thread_handler,
  255. IRQF_SHARED, KBUILD_MODNAME, dev);
  256. if (err) {
  257. dev_err(&pdev->dev, "request_threaded_irq failed: irq = %d.\n",
  258. pdev->irq);
  259. return err;
  260. }
  261. mutex_lock(&dev->device_lock);
  262. dev->dev_state = MEI_DEV_POWER_UP;
  263. mei_clear_interrupts(dev);
  264. mei_reset(dev, 1);
  265. mutex_unlock(&dev->device_lock);
  266. /* Start timer if stopped in suspend */
  267. schedule_delayed_work(&dev->timer_work, HZ);
  268. return err;
  269. }
  270. static SIMPLE_DEV_PM_OPS(mei_me_pm_ops, mei_me_pci_suspend, mei_me_pci_resume);
  271. #define MEI_ME_PM_OPS (&mei_me_pm_ops)
  272. #else
  273. #define MEI_ME_PM_OPS NULL
  274. #endif /* CONFIG_PM */
  275. /*
  276. * PCI driver structure
  277. */
  278. static struct pci_driver mei_me_driver = {
  279. .name = KBUILD_MODNAME,
  280. .id_table = mei_me_pci_tbl,
  281. .probe = mei_me_probe,
  282. .remove = mei_me_remove,
  283. .shutdown = mei_me_remove,
  284. .driver.pm = MEI_ME_PM_OPS,
  285. };
  286. module_pci_driver(mei_me_driver);
  287. MODULE_AUTHOR("Intel Corporation");
  288. MODULE_DESCRIPTION("Intel(R) Management Engine Interface");
  289. MODULE_LICENSE("GPL v2");