mic_main.c 14 KB

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