mic_main.c 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  1. /*
  2. * Intel MIC Platform Software Stack (MPSS)
  3. *
  4. * Copyright(c) 2013 Intel Corporation.
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License, version 2, as
  8. * published by the Free Software Foundation.
  9. *
  10. * This program is distributed in the hope that it will be useful, but
  11. * WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  13. * General Public License for more details.
  14. *
  15. * The full GNU General Public License is included in this distribution in
  16. * the file called "COPYING".
  17. *
  18. * Intel MIC Host driver.
  19. *
  20. * Global TODO's across the driver to be added after initial base
  21. * patches are accepted upstream:
  22. * 1) Enable DMA support.
  23. * 2) Enable per vring interrupt support.
  24. */
  25. #include <linux/fs.h>
  26. #include <linux/module.h>
  27. #include <linux/pci.h>
  28. #include "../common/mic_device.h"
  29. #include "mic_device.h"
  30. #include "mic_x100.h"
  31. #include "mic_smpt.h"
  32. static const char mic_driver_name[] = "mic";
  33. static DEFINE_PCI_DEVICE_TABLE(mic_pci_tbl) = {
  34. {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MIC_X100_PCI_DEVICE_2250)},
  35. {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MIC_X100_PCI_DEVICE_2251)},
  36. {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MIC_X100_PCI_DEVICE_2252)},
  37. {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MIC_X100_PCI_DEVICE_2253)},
  38. {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MIC_X100_PCI_DEVICE_2254)},
  39. {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MIC_X100_PCI_DEVICE_2255)},
  40. {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MIC_X100_PCI_DEVICE_2256)},
  41. {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MIC_X100_PCI_DEVICE_2257)},
  42. {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MIC_X100_PCI_DEVICE_2258)},
  43. {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MIC_X100_PCI_DEVICE_2259)},
  44. {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MIC_X100_PCI_DEVICE_225a)},
  45. {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MIC_X100_PCI_DEVICE_225b)},
  46. {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MIC_X100_PCI_DEVICE_225c)},
  47. {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MIC_X100_PCI_DEVICE_225d)},
  48. {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MIC_X100_PCI_DEVICE_225e)},
  49. /* required last entry */
  50. { 0, }
  51. };
  52. MODULE_DEVICE_TABLE(pci, mic_pci_tbl);
  53. /* ID allocator for MIC devices */
  54. static struct ida g_mic_ida;
  55. /* Class of MIC devices for sysfs accessibility. */
  56. static struct class *g_mic_class;
  57. /* Base device node number for MIC devices */
  58. static dev_t g_mic_devno;
  59. /**
  60. * mic_ops_init: Initialize HW specific operation tables.
  61. *
  62. * @mdev: pointer to mic_device instance
  63. *
  64. * returns none.
  65. */
  66. static void mic_ops_init(struct mic_device *mdev)
  67. {
  68. switch (mdev->family) {
  69. case MIC_FAMILY_X100:
  70. mdev->ops = &mic_x100_ops;
  71. mdev->intr_ops = &mic_x100_intr_ops;
  72. mdev->smpt_ops = &mic_x100_smpt_ops;
  73. break;
  74. default:
  75. break;
  76. }
  77. }
  78. /**
  79. * mic_get_family - Determine hardware family to which this MIC belongs.
  80. *
  81. * @pdev: The pci device structure
  82. *
  83. * returns family.
  84. */
  85. static enum mic_hw_family mic_get_family(struct pci_dev *pdev)
  86. {
  87. enum mic_hw_family family;
  88. switch (pdev->device) {
  89. case MIC_X100_PCI_DEVICE_2250:
  90. case MIC_X100_PCI_DEVICE_2251:
  91. case MIC_X100_PCI_DEVICE_2252:
  92. case MIC_X100_PCI_DEVICE_2253:
  93. case MIC_X100_PCI_DEVICE_2254:
  94. case MIC_X100_PCI_DEVICE_2255:
  95. case MIC_X100_PCI_DEVICE_2256:
  96. case MIC_X100_PCI_DEVICE_2257:
  97. case MIC_X100_PCI_DEVICE_2258:
  98. case MIC_X100_PCI_DEVICE_2259:
  99. case MIC_X100_PCI_DEVICE_225a:
  100. case MIC_X100_PCI_DEVICE_225b:
  101. case MIC_X100_PCI_DEVICE_225c:
  102. case MIC_X100_PCI_DEVICE_225d:
  103. case MIC_X100_PCI_DEVICE_225e:
  104. family = MIC_FAMILY_X100;
  105. break;
  106. default:
  107. family = MIC_FAMILY_UNKNOWN;
  108. break;
  109. }
  110. return family;
  111. }
  112. /**
  113. * mic_device_init - Allocates and initializes the MIC device structure
  114. *
  115. * @mdev: pointer to mic_device instance
  116. * @pdev: The pci device structure
  117. *
  118. * returns none.
  119. */
  120. static void
  121. mic_device_init(struct mic_device *mdev, struct pci_dev *pdev)
  122. {
  123. mdev->family = mic_get_family(pdev);
  124. mdev->stepping = pdev->revision;
  125. mic_ops_init(mdev);
  126. mic_sysfs_init(mdev);
  127. mutex_init(&mdev->mic_mutex);
  128. mdev->irq_info.next_avail_src = 0;
  129. }
  130. /**
  131. * mic_probe - Device Initialization Routine
  132. *
  133. * @pdev: PCI device structure
  134. * @ent: entry in mic_pci_tbl
  135. *
  136. * returns 0 on success, < 0 on failure.
  137. */
  138. static int mic_probe(struct pci_dev *pdev,
  139. const struct pci_device_id *ent)
  140. {
  141. int rc;
  142. struct mic_device *mdev;
  143. mdev = kzalloc(sizeof(*mdev), GFP_KERNEL);
  144. if (!mdev) {
  145. rc = -ENOMEM;
  146. dev_err(&pdev->dev, "mdev kmalloc failed rc %d\n", rc);
  147. goto mdev_alloc_fail;
  148. }
  149. mdev->id = ida_simple_get(&g_mic_ida, 0, MIC_MAX_NUM_DEVS, GFP_KERNEL);
  150. if (mdev->id < 0) {
  151. rc = mdev->id;
  152. dev_err(&pdev->dev, "ida_simple_get failed rc %d\n", rc);
  153. goto ida_fail;
  154. }
  155. mic_device_init(mdev, pdev);
  156. rc = pci_enable_device(pdev);
  157. if (rc) {
  158. dev_err(&pdev->dev, "failed to enable pci device.\n");
  159. goto ida_remove;
  160. }
  161. pci_set_master(pdev);
  162. rc = pci_request_regions(pdev, mic_driver_name);
  163. if (rc) {
  164. dev_err(&pdev->dev, "failed to get pci regions.\n");
  165. goto disable_device;
  166. }
  167. rc = pci_set_dma_mask(pdev, DMA_BIT_MASK(64));
  168. if (rc) {
  169. dev_err(&pdev->dev, "Cannot set DMA mask\n");
  170. goto release_regions;
  171. }
  172. mdev->mmio.pa = pci_resource_start(pdev, mdev->ops->mmio_bar);
  173. mdev->mmio.len = pci_resource_len(pdev, mdev->ops->mmio_bar);
  174. mdev->mmio.va = pci_ioremap_bar(pdev, mdev->ops->mmio_bar);
  175. if (!mdev->mmio.va) {
  176. dev_err(&pdev->dev, "Cannot remap MMIO BAR\n");
  177. rc = -EIO;
  178. goto release_regions;
  179. }
  180. mdev->aper.pa = pci_resource_start(pdev, mdev->ops->aper_bar);
  181. mdev->aper.len = pci_resource_len(pdev, mdev->ops->aper_bar);
  182. mdev->aper.va = ioremap_wc(mdev->aper.pa, mdev->aper.len);
  183. if (!mdev->aper.va) {
  184. dev_err(&pdev->dev, "Cannot remap Aperture BAR\n");
  185. rc = -EIO;
  186. goto unmap_mmio;
  187. }
  188. mdev->intr_ops->intr_init(mdev);
  189. rc = mic_setup_interrupts(mdev, pdev);
  190. if (rc) {
  191. dev_err(&pdev->dev, "mic_setup_interrupts failed %d\n", rc);
  192. goto unmap_aper;
  193. }
  194. rc = mic_smpt_init(mdev);
  195. if (rc) {
  196. dev_err(&pdev->dev, "smpt_init failed %d\n", rc);
  197. goto free_interrupts;
  198. }
  199. pci_set_drvdata(pdev, mdev);
  200. mdev->sdev = device_create_with_groups(g_mic_class, &pdev->dev,
  201. MKDEV(MAJOR(g_mic_devno), mdev->id), NULL,
  202. mdev->attr_group, "mic%d", mdev->id);
  203. if (IS_ERR(mdev->sdev)) {
  204. rc = PTR_ERR(mdev->sdev);
  205. dev_err(&pdev->dev,
  206. "device_create_with_groups failed rc %d\n", rc);
  207. goto smpt_uninit;
  208. }
  209. return 0;
  210. smpt_uninit:
  211. mic_smpt_uninit(mdev);
  212. free_interrupts:
  213. mic_free_interrupts(mdev, pdev);
  214. unmap_aper:
  215. iounmap(mdev->aper.va);
  216. unmap_mmio:
  217. iounmap(mdev->mmio.va);
  218. release_regions:
  219. pci_release_regions(pdev);
  220. disable_device:
  221. pci_disable_device(pdev);
  222. ida_remove:
  223. ida_simple_remove(&g_mic_ida, mdev->id);
  224. ida_fail:
  225. kfree(mdev);
  226. mdev_alloc_fail:
  227. dev_err(&pdev->dev, "Probe failed rc %d\n", rc);
  228. return rc;
  229. }
  230. /**
  231. * mic_remove - Device Removal Routine
  232. * mic_remove is called by the PCI subsystem to alert the driver
  233. * that it should release a PCI device.
  234. *
  235. * @pdev: PCI device structure
  236. */
  237. static void mic_remove(struct pci_dev *pdev)
  238. {
  239. struct mic_device *mdev;
  240. mdev = pci_get_drvdata(pdev);
  241. if (!mdev)
  242. return;
  243. device_destroy(g_mic_class, MKDEV(MAJOR(g_mic_devno), mdev->id));
  244. mic_smpt_uninit(mdev);
  245. mic_free_interrupts(mdev, pdev);
  246. iounmap(mdev->mmio.va);
  247. iounmap(mdev->aper.va);
  248. pci_release_regions(pdev);
  249. pci_disable_device(pdev);
  250. ida_simple_remove(&g_mic_ida, mdev->id);
  251. kfree(mdev);
  252. }
  253. static struct pci_driver mic_driver = {
  254. .name = mic_driver_name,
  255. .id_table = mic_pci_tbl,
  256. .probe = mic_probe,
  257. .remove = mic_remove
  258. };
  259. static int __init mic_init(void)
  260. {
  261. int ret;
  262. ret = alloc_chrdev_region(&g_mic_devno, 0,
  263. MIC_MAX_NUM_DEVS, mic_driver_name);
  264. if (ret) {
  265. pr_err("alloc_chrdev_region failed ret %d\n", ret);
  266. goto error;
  267. }
  268. g_mic_class = class_create(THIS_MODULE, mic_driver_name);
  269. if (IS_ERR(g_mic_class)) {
  270. ret = PTR_ERR(g_mic_class);
  271. pr_err("class_create failed ret %d\n", ret);
  272. goto cleanup_chrdev;
  273. }
  274. ida_init(&g_mic_ida);
  275. ret = pci_register_driver(&mic_driver);
  276. if (ret) {
  277. pr_err("pci_register_driver failed ret %d\n", ret);
  278. goto class_destroy;
  279. }
  280. return ret;
  281. class_destroy:
  282. class_destroy(g_mic_class);
  283. cleanup_chrdev:
  284. unregister_chrdev_region(g_mic_devno, MIC_MAX_NUM_DEVS);
  285. error:
  286. return ret;
  287. }
  288. static void __exit mic_exit(void)
  289. {
  290. pci_unregister_driver(&mic_driver);
  291. ida_destroy(&g_mic_ida);
  292. class_destroy(g_mic_class);
  293. unregister_chrdev_region(g_mic_devno, MIC_MAX_NUM_DEVS);
  294. }
  295. module_init(mic_init);
  296. module_exit(mic_exit);
  297. MODULE_AUTHOR("Intel Corporation");
  298. MODULE_DESCRIPTION("Intel(R) MIC X100 Host driver");
  299. MODULE_LICENSE("GPL v2");