mic_main.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478
  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 <linux/poll.h>
  29. #include <linux/mic_common.h>
  30. #include "../common/mic_device.h"
  31. #include "mic_device.h"
  32. #include "mic_x100.h"
  33. #include "mic_smpt.h"
  34. #include "mic_fops.h"
  35. #include "mic_virtio.h"
  36. static const char mic_driver_name[] = "mic";
  37. static DEFINE_PCI_DEVICE_TABLE(mic_pci_tbl) = {
  38. {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MIC_X100_PCI_DEVICE_2250)},
  39. {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MIC_X100_PCI_DEVICE_2251)},
  40. {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MIC_X100_PCI_DEVICE_2252)},
  41. {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MIC_X100_PCI_DEVICE_2253)},
  42. {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MIC_X100_PCI_DEVICE_2254)},
  43. {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MIC_X100_PCI_DEVICE_2255)},
  44. {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MIC_X100_PCI_DEVICE_2256)},
  45. {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MIC_X100_PCI_DEVICE_2257)},
  46. {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MIC_X100_PCI_DEVICE_2258)},
  47. {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MIC_X100_PCI_DEVICE_2259)},
  48. {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MIC_X100_PCI_DEVICE_225a)},
  49. {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MIC_X100_PCI_DEVICE_225b)},
  50. {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MIC_X100_PCI_DEVICE_225c)},
  51. {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MIC_X100_PCI_DEVICE_225d)},
  52. {PCI_DEVICE(PCI_VENDOR_ID_INTEL, MIC_X100_PCI_DEVICE_225e)},
  53. /* required last entry */
  54. { 0, }
  55. };
  56. MODULE_DEVICE_TABLE(pci, mic_pci_tbl);
  57. /* ID allocator for MIC devices */
  58. static struct ida g_mic_ida;
  59. /* Class of MIC devices for sysfs accessibility. */
  60. static struct class *g_mic_class;
  61. /* Base device node number for MIC devices */
  62. static dev_t g_mic_devno;
  63. static const struct file_operations mic_fops = {
  64. .open = mic_open,
  65. .release = mic_release,
  66. .unlocked_ioctl = mic_ioctl,
  67. .poll = mic_poll,
  68. .mmap = mic_mmap,
  69. .owner = THIS_MODULE,
  70. };
  71. /* Initialize the device page */
  72. static int mic_dp_init(struct mic_device *mdev)
  73. {
  74. mdev->dp = kzalloc(MIC_DP_SIZE, GFP_KERNEL);
  75. if (!mdev->dp) {
  76. dev_err(mdev->sdev->parent, "%s %d err %d\n",
  77. __func__, __LINE__, -ENOMEM);
  78. return -ENOMEM;
  79. }
  80. mdev->dp_dma_addr = mic_map_single(mdev,
  81. mdev->dp, MIC_DP_SIZE);
  82. if (mic_map_error(mdev->dp_dma_addr)) {
  83. kfree(mdev->dp);
  84. dev_err(mdev->sdev->parent, "%s %d err %d\n",
  85. __func__, __LINE__, -ENOMEM);
  86. return -ENOMEM;
  87. }
  88. mdev->ops->write_spad(mdev, MIC_DPLO_SPAD, mdev->dp_dma_addr);
  89. mdev->ops->write_spad(mdev, MIC_DPHI_SPAD, mdev->dp_dma_addr >> 32);
  90. return 0;
  91. }
  92. /* Uninitialize the device page */
  93. static void mic_dp_uninit(struct mic_device *mdev)
  94. {
  95. mic_unmap_single(mdev, mdev->dp_dma_addr, MIC_DP_SIZE);
  96. kfree(mdev->dp);
  97. }
  98. /**
  99. * mic_shutdown_db - Shutdown doorbell interrupt handler.
  100. */
  101. static irqreturn_t mic_shutdown_db(int irq, void *data)
  102. {
  103. struct mic_device *mdev = data;
  104. struct mic_bootparam *bootparam = mdev->dp;
  105. mdev->ops->ack_interrupt(mdev);
  106. switch (bootparam->shutdown_status) {
  107. case MIC_HALTED:
  108. case MIC_POWER_OFF:
  109. case MIC_RESTART:
  110. /* Fall through */
  111. case MIC_CRASHED:
  112. schedule_work(&mdev->shutdown_work);
  113. break;
  114. default:
  115. break;
  116. };
  117. return IRQ_HANDLED;
  118. }
  119. /**
  120. * mic_ops_init: Initialize HW specific operation tables.
  121. *
  122. * @mdev: pointer to mic_device instance
  123. *
  124. * returns none.
  125. */
  126. static void mic_ops_init(struct mic_device *mdev)
  127. {
  128. switch (mdev->family) {
  129. case MIC_FAMILY_X100:
  130. mdev->ops = &mic_x100_ops;
  131. mdev->intr_ops = &mic_x100_intr_ops;
  132. mdev->smpt_ops = &mic_x100_smpt_ops;
  133. break;
  134. default:
  135. break;
  136. }
  137. }
  138. /**
  139. * mic_get_family - Determine hardware family to which this MIC belongs.
  140. *
  141. * @pdev: The pci device structure
  142. *
  143. * returns family.
  144. */
  145. static enum mic_hw_family mic_get_family(struct pci_dev *pdev)
  146. {
  147. enum mic_hw_family family;
  148. switch (pdev->device) {
  149. case MIC_X100_PCI_DEVICE_2250:
  150. case MIC_X100_PCI_DEVICE_2251:
  151. case MIC_X100_PCI_DEVICE_2252:
  152. case MIC_X100_PCI_DEVICE_2253:
  153. case MIC_X100_PCI_DEVICE_2254:
  154. case MIC_X100_PCI_DEVICE_2255:
  155. case MIC_X100_PCI_DEVICE_2256:
  156. case MIC_X100_PCI_DEVICE_2257:
  157. case MIC_X100_PCI_DEVICE_2258:
  158. case MIC_X100_PCI_DEVICE_2259:
  159. case MIC_X100_PCI_DEVICE_225a:
  160. case MIC_X100_PCI_DEVICE_225b:
  161. case MIC_X100_PCI_DEVICE_225c:
  162. case MIC_X100_PCI_DEVICE_225d:
  163. case MIC_X100_PCI_DEVICE_225e:
  164. family = MIC_FAMILY_X100;
  165. break;
  166. default:
  167. family = MIC_FAMILY_UNKNOWN;
  168. break;
  169. }
  170. return family;
  171. }
  172. /**
  173. * mic_device_init - Allocates and initializes the MIC device structure
  174. *
  175. * @mdev: pointer to mic_device instance
  176. * @pdev: The pci device structure
  177. *
  178. * returns none.
  179. */
  180. static void
  181. mic_device_init(struct mic_device *mdev, struct pci_dev *pdev)
  182. {
  183. mdev->family = mic_get_family(pdev);
  184. mdev->stepping = pdev->revision;
  185. mic_ops_init(mdev);
  186. mic_sysfs_init(mdev);
  187. mutex_init(&mdev->mic_mutex);
  188. mdev->irq_info.next_avail_src = 0;
  189. INIT_WORK(&mdev->reset_trigger_work, mic_reset_trigger_work);
  190. INIT_WORK(&mdev->shutdown_work, mic_shutdown_work);
  191. INIT_LIST_HEAD(&mdev->vdev_list);
  192. }
  193. /**
  194. * mic_device_uninit - Frees resources allocated during mic_device_init(..)
  195. *
  196. * @mdev: pointer to mic_device instance
  197. *
  198. * returns none
  199. */
  200. static void mic_device_uninit(struct mic_device *mdev)
  201. {
  202. /* The cmdline sysfs entry might have allocated cmdline */
  203. kfree(mdev->cmdline);
  204. kfree(mdev->firmware);
  205. kfree(mdev->ramdisk);
  206. kfree(mdev->bootmode);
  207. flush_work(&mdev->reset_trigger_work);
  208. flush_work(&mdev->shutdown_work);
  209. }
  210. /**
  211. * mic_probe - Device Initialization Routine
  212. *
  213. * @pdev: PCI device structure
  214. * @ent: entry in mic_pci_tbl
  215. *
  216. * returns 0 on success, < 0 on failure.
  217. */
  218. static int mic_probe(struct pci_dev *pdev,
  219. const struct pci_device_id *ent)
  220. {
  221. int rc;
  222. struct mic_device *mdev;
  223. mdev = kzalloc(sizeof(*mdev), GFP_KERNEL);
  224. if (!mdev) {
  225. rc = -ENOMEM;
  226. dev_err(&pdev->dev, "mdev kmalloc failed rc %d\n", rc);
  227. goto mdev_alloc_fail;
  228. }
  229. mdev->id = ida_simple_get(&g_mic_ida, 0, MIC_MAX_NUM_DEVS, GFP_KERNEL);
  230. if (mdev->id < 0) {
  231. rc = mdev->id;
  232. dev_err(&pdev->dev, "ida_simple_get failed rc %d\n", rc);
  233. goto ida_fail;
  234. }
  235. mic_device_init(mdev, pdev);
  236. rc = pci_enable_device(pdev);
  237. if (rc) {
  238. dev_err(&pdev->dev, "failed to enable pci device.\n");
  239. goto uninit_device;
  240. }
  241. pci_set_master(pdev);
  242. rc = pci_request_regions(pdev, mic_driver_name);
  243. if (rc) {
  244. dev_err(&pdev->dev, "failed to get pci regions.\n");
  245. goto disable_device;
  246. }
  247. rc = pci_set_dma_mask(pdev, DMA_BIT_MASK(64));
  248. if (rc) {
  249. dev_err(&pdev->dev, "Cannot set DMA mask\n");
  250. goto release_regions;
  251. }
  252. mdev->mmio.pa = pci_resource_start(pdev, mdev->ops->mmio_bar);
  253. mdev->mmio.len = pci_resource_len(pdev, mdev->ops->mmio_bar);
  254. mdev->mmio.va = pci_ioremap_bar(pdev, mdev->ops->mmio_bar);
  255. if (!mdev->mmio.va) {
  256. dev_err(&pdev->dev, "Cannot remap MMIO BAR\n");
  257. rc = -EIO;
  258. goto release_regions;
  259. }
  260. mdev->aper.pa = pci_resource_start(pdev, mdev->ops->aper_bar);
  261. mdev->aper.len = pci_resource_len(pdev, mdev->ops->aper_bar);
  262. mdev->aper.va = ioremap_wc(mdev->aper.pa, mdev->aper.len);
  263. if (!mdev->aper.va) {
  264. dev_err(&pdev->dev, "Cannot remap Aperture BAR\n");
  265. rc = -EIO;
  266. goto unmap_mmio;
  267. }
  268. mdev->intr_ops->intr_init(mdev);
  269. rc = mic_setup_interrupts(mdev, pdev);
  270. if (rc) {
  271. dev_err(&pdev->dev, "mic_setup_interrupts failed %d\n", rc);
  272. goto unmap_aper;
  273. }
  274. rc = mic_smpt_init(mdev);
  275. if (rc) {
  276. dev_err(&pdev->dev, "smpt_init failed %d\n", rc);
  277. goto free_interrupts;
  278. }
  279. pci_set_drvdata(pdev, mdev);
  280. mdev->sdev = device_create_with_groups(g_mic_class, &pdev->dev,
  281. MKDEV(MAJOR(g_mic_devno), mdev->id), NULL,
  282. mdev->attr_group, "mic%d", mdev->id);
  283. if (IS_ERR(mdev->sdev)) {
  284. rc = PTR_ERR(mdev->sdev);
  285. dev_err(&pdev->dev,
  286. "device_create_with_groups failed rc %d\n", rc);
  287. goto smpt_uninit;
  288. }
  289. mdev->state_sysfs = sysfs_get_dirent(mdev->sdev->kobj.sd,
  290. NULL, "state");
  291. if (!mdev->state_sysfs) {
  292. rc = -ENODEV;
  293. dev_err(&pdev->dev, "sysfs_get_dirent failed rc %d\n", rc);
  294. goto destroy_device;
  295. }
  296. rc = mic_dp_init(mdev);
  297. if (rc) {
  298. dev_err(&pdev->dev, "mic_dp_init failed rc %d\n", rc);
  299. goto sysfs_put;
  300. }
  301. mutex_lock(&mdev->mic_mutex);
  302. mdev->shutdown_db = mic_next_db(mdev);
  303. mdev->shutdown_cookie = mic_request_irq(mdev, mic_shutdown_db,
  304. "shutdown-interrupt", mdev, mdev->shutdown_db, MIC_INTR_DB);
  305. if (IS_ERR(mdev->shutdown_cookie)) {
  306. rc = PTR_ERR(mdev->shutdown_cookie);
  307. mutex_unlock(&mdev->mic_mutex);
  308. goto dp_uninit;
  309. }
  310. mutex_unlock(&mdev->mic_mutex);
  311. mic_bootparam_init(mdev);
  312. mic_create_debug_dir(mdev);
  313. cdev_init(&mdev->cdev, &mic_fops);
  314. mdev->cdev.owner = THIS_MODULE;
  315. rc = cdev_add(&mdev->cdev, MKDEV(MAJOR(g_mic_devno), mdev->id), 1);
  316. if (rc) {
  317. dev_err(&pdev->dev, "cdev_add err id %d rc %d\n", mdev->id, rc);
  318. goto cleanup_debug_dir;
  319. }
  320. return 0;
  321. cleanup_debug_dir:
  322. mic_delete_debug_dir(mdev);
  323. mutex_lock(&mdev->mic_mutex);
  324. mic_free_irq(mdev, mdev->shutdown_cookie, mdev);
  325. mutex_unlock(&mdev->mic_mutex);
  326. dp_uninit:
  327. mic_dp_uninit(mdev);
  328. sysfs_put:
  329. sysfs_put(mdev->state_sysfs);
  330. destroy_device:
  331. device_destroy(g_mic_class, MKDEV(MAJOR(g_mic_devno), mdev->id));
  332. smpt_uninit:
  333. mic_smpt_uninit(mdev);
  334. free_interrupts:
  335. mic_free_interrupts(mdev, pdev);
  336. unmap_aper:
  337. iounmap(mdev->aper.va);
  338. unmap_mmio:
  339. iounmap(mdev->mmio.va);
  340. release_regions:
  341. pci_release_regions(pdev);
  342. disable_device:
  343. pci_disable_device(pdev);
  344. uninit_device:
  345. mic_device_uninit(mdev);
  346. ida_simple_remove(&g_mic_ida, mdev->id);
  347. ida_fail:
  348. kfree(mdev);
  349. mdev_alloc_fail:
  350. dev_err(&pdev->dev, "Probe failed rc %d\n", rc);
  351. return rc;
  352. }
  353. /**
  354. * mic_remove - Device Removal Routine
  355. * mic_remove is called by the PCI subsystem to alert the driver
  356. * that it should release a PCI device.
  357. *
  358. * @pdev: PCI device structure
  359. */
  360. static void mic_remove(struct pci_dev *pdev)
  361. {
  362. struct mic_device *mdev;
  363. mdev = pci_get_drvdata(pdev);
  364. if (!mdev)
  365. return;
  366. mic_stop(mdev, false);
  367. cdev_del(&mdev->cdev);
  368. mic_delete_debug_dir(mdev);
  369. mutex_lock(&mdev->mic_mutex);
  370. mic_free_irq(mdev, mdev->shutdown_cookie, mdev);
  371. mutex_unlock(&mdev->mic_mutex);
  372. flush_work(&mdev->shutdown_work);
  373. mic_dp_uninit(mdev);
  374. sysfs_put(mdev->state_sysfs);
  375. device_destroy(g_mic_class, MKDEV(MAJOR(g_mic_devno), mdev->id));
  376. mic_smpt_uninit(mdev);
  377. mic_free_interrupts(mdev, pdev);
  378. iounmap(mdev->mmio.va);
  379. iounmap(mdev->aper.va);
  380. mic_device_uninit(mdev);
  381. pci_release_regions(pdev);
  382. pci_disable_device(pdev);
  383. ida_simple_remove(&g_mic_ida, mdev->id);
  384. kfree(mdev);
  385. }
  386. static struct pci_driver mic_driver = {
  387. .name = mic_driver_name,
  388. .id_table = mic_pci_tbl,
  389. .probe = mic_probe,
  390. .remove = mic_remove
  391. };
  392. static int __init mic_init(void)
  393. {
  394. int ret;
  395. ret = alloc_chrdev_region(&g_mic_devno, 0,
  396. MIC_MAX_NUM_DEVS, mic_driver_name);
  397. if (ret) {
  398. pr_err("alloc_chrdev_region failed ret %d\n", ret);
  399. goto error;
  400. }
  401. g_mic_class = class_create(THIS_MODULE, mic_driver_name);
  402. if (IS_ERR(g_mic_class)) {
  403. ret = PTR_ERR(g_mic_class);
  404. pr_err("class_create failed ret %d\n", ret);
  405. goto cleanup_chrdev;
  406. }
  407. mic_init_debugfs();
  408. ida_init(&g_mic_ida);
  409. ret = pci_register_driver(&mic_driver);
  410. if (ret) {
  411. pr_err("pci_register_driver failed ret %d\n", ret);
  412. goto cleanup_debugfs;
  413. }
  414. return ret;
  415. cleanup_debugfs:
  416. mic_exit_debugfs();
  417. class_destroy(g_mic_class);
  418. cleanup_chrdev:
  419. unregister_chrdev_region(g_mic_devno, MIC_MAX_NUM_DEVS);
  420. error:
  421. return ret;
  422. }
  423. static void __exit mic_exit(void)
  424. {
  425. pci_unregister_driver(&mic_driver);
  426. ida_destroy(&g_mic_ida);
  427. mic_exit_debugfs();
  428. class_destroy(g_mic_class);
  429. unregister_chrdev_region(g_mic_devno, MIC_MAX_NUM_DEVS);
  430. }
  431. module_init(mic_init);
  432. module_exit(mic_exit);
  433. MODULE_AUTHOR("Intel Corporation");
  434. MODULE_DESCRIPTION("Intel(R) MIC X100 Host driver");
  435. MODULE_LICENSE("GPL v2");