pci-me.c 8.8 KB

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