mic_main.c 13 KB

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