mic_main.c 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  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/idr.h>
  27. #include <linux/module.h>
  28. #include <linux/pci.h>
  29. #include "../common/mic_device.h"
  30. #include "mic_device.h"
  31. #include "mic_x100.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. break;
  72. default:
  73. break;
  74. }
  75. }
  76. /**
  77. * mic_get_family - Determine hardware family to which this MIC belongs.
  78. *
  79. * @pdev: The pci device structure
  80. *
  81. * returns family.
  82. */
  83. static enum mic_hw_family mic_get_family(struct pci_dev *pdev)
  84. {
  85. enum mic_hw_family family;
  86. switch (pdev->device) {
  87. case MIC_X100_PCI_DEVICE_2250:
  88. case MIC_X100_PCI_DEVICE_2251:
  89. case MIC_X100_PCI_DEVICE_2252:
  90. case MIC_X100_PCI_DEVICE_2253:
  91. case MIC_X100_PCI_DEVICE_2254:
  92. case MIC_X100_PCI_DEVICE_2255:
  93. case MIC_X100_PCI_DEVICE_2256:
  94. case MIC_X100_PCI_DEVICE_2257:
  95. case MIC_X100_PCI_DEVICE_2258:
  96. case MIC_X100_PCI_DEVICE_2259:
  97. case MIC_X100_PCI_DEVICE_225a:
  98. case MIC_X100_PCI_DEVICE_225b:
  99. case MIC_X100_PCI_DEVICE_225c:
  100. case MIC_X100_PCI_DEVICE_225d:
  101. case MIC_X100_PCI_DEVICE_225e:
  102. family = MIC_FAMILY_X100;
  103. break;
  104. default:
  105. family = MIC_FAMILY_UNKNOWN;
  106. break;
  107. }
  108. return family;
  109. }
  110. /**
  111. * mic_device_init - Allocates and initializes the MIC device structure
  112. *
  113. * @mdev: pointer to mic_device instance
  114. * @pdev: The pci device structure
  115. *
  116. * returns none.
  117. */
  118. static void
  119. mic_device_init(struct mic_device *mdev, struct pci_dev *pdev)
  120. {
  121. mdev->family = mic_get_family(pdev);
  122. mdev->stepping = pdev->revision;
  123. mic_ops_init(mdev);
  124. mic_sysfs_init(mdev);
  125. }
  126. /**
  127. * mic_probe - Device Initialization Routine
  128. *
  129. * @pdev: PCI device structure
  130. * @ent: entry in mic_pci_tbl
  131. *
  132. * returns 0 on success, < 0 on failure.
  133. */
  134. static int mic_probe(struct pci_dev *pdev,
  135. const struct pci_device_id *ent)
  136. {
  137. int rc;
  138. struct mic_device *mdev;
  139. mdev = kzalloc(sizeof(*mdev), GFP_KERNEL);
  140. if (!mdev) {
  141. rc = -ENOMEM;
  142. dev_err(&pdev->dev, "mdev kmalloc failed rc %d\n", rc);
  143. goto mdev_alloc_fail;
  144. }
  145. mdev->id = ida_simple_get(&g_mic_ida, 0, MIC_MAX_NUM_DEVS, GFP_KERNEL);
  146. if (mdev->id < 0) {
  147. rc = mdev->id;
  148. dev_err(&pdev->dev, "ida_simple_get failed rc %d\n", rc);
  149. goto ida_fail;
  150. }
  151. mic_device_init(mdev, pdev);
  152. rc = pci_enable_device(pdev);
  153. if (rc) {
  154. dev_err(&pdev->dev, "failed to enable pci device.\n");
  155. goto ida_remove;
  156. }
  157. pci_set_master(pdev);
  158. rc = pci_request_regions(pdev, mic_driver_name);
  159. if (rc) {
  160. dev_err(&pdev->dev, "failed to get pci regions.\n");
  161. goto disable_device;
  162. }
  163. rc = pci_set_dma_mask(pdev, DMA_BIT_MASK(64));
  164. if (rc) {
  165. dev_err(&pdev->dev, "Cannot set DMA mask\n");
  166. goto release_regions;
  167. }
  168. mdev->mmio.pa = pci_resource_start(pdev, mdev->ops->mmio_bar);
  169. mdev->mmio.len = pci_resource_len(pdev, mdev->ops->mmio_bar);
  170. mdev->mmio.va = pci_ioremap_bar(pdev, mdev->ops->mmio_bar);
  171. if (!mdev->mmio.va) {
  172. dev_err(&pdev->dev, "Cannot remap MMIO BAR\n");
  173. rc = -EIO;
  174. goto release_regions;
  175. }
  176. mdev->aper.pa = pci_resource_start(pdev, mdev->ops->aper_bar);
  177. mdev->aper.len = pci_resource_len(pdev, mdev->ops->aper_bar);
  178. mdev->aper.va = ioremap_wc(mdev->aper.pa, mdev->aper.len);
  179. if (!mdev->aper.va) {
  180. dev_err(&pdev->dev, "Cannot remap Aperture BAR\n");
  181. rc = -EIO;
  182. goto unmap_mmio;
  183. }
  184. pci_set_drvdata(pdev, mdev);
  185. mdev->sdev = device_create_with_groups(g_mic_class, &pdev->dev,
  186. MKDEV(MAJOR(g_mic_devno), mdev->id), NULL,
  187. mdev->attr_group, "mic%d", mdev->id);
  188. if (IS_ERR(mdev->sdev)) {
  189. rc = PTR_ERR(mdev->sdev);
  190. dev_err(&pdev->dev,
  191. "device_create_with_groups failed rc %d\n", rc);
  192. goto unmap_aper;
  193. }
  194. return 0;
  195. unmap_aper:
  196. iounmap(mdev->aper.va);
  197. unmap_mmio:
  198. iounmap(mdev->mmio.va);
  199. release_regions:
  200. pci_release_regions(pdev);
  201. disable_device:
  202. pci_disable_device(pdev);
  203. ida_remove:
  204. ida_simple_remove(&g_mic_ida, mdev->id);
  205. ida_fail:
  206. kfree(mdev);
  207. mdev_alloc_fail:
  208. dev_err(&pdev->dev, "Probe failed rc %d\n", rc);
  209. return rc;
  210. }
  211. /**
  212. * mic_remove - Device Removal Routine
  213. * mic_remove is called by the PCI subsystem to alert the driver
  214. * that it should release a PCI device.
  215. *
  216. * @pdev: PCI device structure
  217. */
  218. static void mic_remove(struct pci_dev *pdev)
  219. {
  220. struct mic_device *mdev;
  221. mdev = pci_get_drvdata(pdev);
  222. if (!mdev)
  223. return;
  224. device_destroy(g_mic_class, MKDEV(MAJOR(g_mic_devno), mdev->id));
  225. iounmap(mdev->mmio.va);
  226. iounmap(mdev->aper.va);
  227. pci_release_regions(pdev);
  228. pci_disable_device(pdev);
  229. ida_simple_remove(&g_mic_ida, mdev->id);
  230. kfree(mdev);
  231. }
  232. static struct pci_driver mic_driver = {
  233. .name = mic_driver_name,
  234. .id_table = mic_pci_tbl,
  235. .probe = mic_probe,
  236. .remove = mic_remove
  237. };
  238. static int __init mic_init(void)
  239. {
  240. int ret;
  241. ret = alloc_chrdev_region(&g_mic_devno, 0,
  242. MIC_MAX_NUM_DEVS, mic_driver_name);
  243. if (ret) {
  244. pr_err("alloc_chrdev_region failed ret %d\n", ret);
  245. goto error;
  246. }
  247. g_mic_class = class_create(THIS_MODULE, mic_driver_name);
  248. if (IS_ERR(g_mic_class)) {
  249. ret = PTR_ERR(g_mic_class);
  250. pr_err("class_create failed ret %d\n", ret);
  251. goto cleanup_chrdev;
  252. }
  253. ida_init(&g_mic_ida);
  254. ret = pci_register_driver(&mic_driver);
  255. if (ret) {
  256. pr_err("pci_register_driver failed ret %d\n", ret);
  257. goto class_destroy;
  258. }
  259. return ret;
  260. class_destroy:
  261. class_destroy(g_mic_class);
  262. cleanup_chrdev:
  263. unregister_chrdev_region(g_mic_devno, MIC_MAX_NUM_DEVS);
  264. error:
  265. return ret;
  266. }
  267. static void __exit mic_exit(void)
  268. {
  269. pci_unregister_driver(&mic_driver);
  270. ida_destroy(&g_mic_ida);
  271. class_destroy(g_mic_class);
  272. unregister_chrdev_region(g_mic_devno, MIC_MAX_NUM_DEVS);
  273. }
  274. module_init(mic_init);
  275. module_exit(mic_exit);
  276. MODULE_AUTHOR("Intel Corporation");
  277. MODULE_DESCRIPTION("Intel(R) MIC X100 Host driver");
  278. MODULE_LICENSE("GPL v2");