snic_main.c 24 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045
  1. /*
  2. * Copyright 2014 Cisco Systems, Inc. All rights reserved.
  3. *
  4. * This program is free software; you may redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation; version 2 of the License.
  7. *
  8. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  9. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  10. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  11. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
  12. * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  13. * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  14. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  15. * SOFTWARE.
  16. */
  17. #include <linux/module.h>
  18. #include <linux/mempool.h>
  19. #include <linux/string.h>
  20. #include <linux/slab.h>
  21. #include <linux/errno.h>
  22. #include <linux/init.h>
  23. #include <linux/pci.h>
  24. #include <linux/skbuff.h>
  25. #include <linux/interrupt.h>
  26. #include <linux/spinlock.h>
  27. #include <linux/workqueue.h>
  28. #include <scsi/scsi_host.h>
  29. #include <scsi/scsi_tcq.h>
  30. #include "snic.h"
  31. #include "snic_fwint.h"
  32. #define PCI_DEVICE_ID_CISCO_SNIC 0x0046
  33. /* Supported devices by snic module */
  34. static struct pci_device_id snic_id_table[] = {
  35. {PCI_DEVICE(0x1137, PCI_DEVICE_ID_CISCO_SNIC) },
  36. { 0, } /* end of table */
  37. };
  38. unsigned int snic_log_level = 0x0;
  39. module_param(snic_log_level, int, S_IRUGO|S_IWUSR);
  40. MODULE_PARM_DESC(snic_log_level, "bitmask for snic logging levels");
  41. #ifdef CONFIG_SCSI_SNIC_DEBUG_FS
  42. unsigned int snic_trace_max_pages = 16;
  43. module_param(snic_trace_max_pages, uint, S_IRUGO|S_IWUSR);
  44. MODULE_PARM_DESC(snic_trace_max_pages,
  45. "Total allocated memory pages for snic trace buffer");
  46. #endif
  47. unsigned int snic_max_qdepth = SNIC_DFLT_QUEUE_DEPTH;
  48. module_param(snic_max_qdepth, uint, S_IRUGO | S_IWUSR);
  49. MODULE_PARM_DESC(snic_max_qdepth, "Queue depth to report for each LUN");
  50. /*
  51. * snic_slave_alloc : callback function to SCSI Mid Layer, called on
  52. * scsi device initialization.
  53. */
  54. static int
  55. snic_slave_alloc(struct scsi_device *sdev)
  56. {
  57. struct snic_tgt *tgt = starget_to_tgt(scsi_target(sdev));
  58. if (!tgt || snic_tgt_chkready(tgt))
  59. return -ENXIO;
  60. return 0;
  61. }
  62. /*
  63. * snic_slave_configure : callback function to SCSI Mid Layer, called on
  64. * scsi device initialization.
  65. */
  66. static int
  67. snic_slave_configure(struct scsi_device *sdev)
  68. {
  69. struct snic *snic = shost_priv(sdev->host);
  70. u32 qdepth = 0, max_ios = 0;
  71. int tmo = SNIC_DFLT_CMD_TIMEOUT * HZ;
  72. /* Set Queue Depth */
  73. max_ios = snic_max_qdepth;
  74. qdepth = min_t(u32, max_ios, SNIC_MAX_QUEUE_DEPTH);
  75. scsi_change_queue_depth(sdev, qdepth);
  76. if (snic->fwinfo.io_tmo > 1)
  77. tmo = snic->fwinfo.io_tmo * HZ;
  78. /* FW requires extended timeouts */
  79. blk_queue_rq_timeout(sdev->request_queue, tmo);
  80. return 0;
  81. }
  82. static int
  83. snic_change_queue_depth(struct scsi_device *sdev, int qdepth)
  84. {
  85. int qsz = 0;
  86. qsz = min_t(u32, qdepth, SNIC_MAX_QUEUE_DEPTH);
  87. scsi_change_queue_depth(sdev, qsz);
  88. SNIC_INFO("QDepth Changed to %d\n", sdev->queue_depth);
  89. return sdev->queue_depth;
  90. }
  91. static struct scsi_host_template snic_host_template = {
  92. .module = THIS_MODULE,
  93. .name = SNIC_DRV_NAME,
  94. .queuecommand = snic_queuecommand,
  95. .eh_abort_handler = snic_abort_cmd,
  96. .eh_device_reset_handler = snic_device_reset,
  97. .eh_host_reset_handler = snic_host_reset,
  98. .slave_alloc = snic_slave_alloc,
  99. .slave_configure = snic_slave_configure,
  100. .change_queue_depth = snic_change_queue_depth,
  101. .this_id = -1,
  102. .cmd_per_lun = SNIC_DFLT_QUEUE_DEPTH,
  103. .can_queue = SNIC_MAX_IO_REQ,
  104. .use_clustering = ENABLE_CLUSTERING,
  105. .sg_tablesize = SNIC_MAX_SG_DESC_CNT,
  106. .max_sectors = 0x800,
  107. .shost_attrs = snic_attrs,
  108. .use_blk_tags = 1,
  109. .track_queue_depth = 1,
  110. .cmd_size = sizeof(struct snic_internal_io_state),
  111. .proc_name = "snic_scsi",
  112. };
  113. /*
  114. * snic_handle_link_event : Handles link events such as link up/down/error
  115. */
  116. void
  117. snic_handle_link_event(struct snic *snic)
  118. {
  119. unsigned long flags;
  120. spin_lock_irqsave(&snic->snic_lock, flags);
  121. if (snic->stop_link_events) {
  122. spin_unlock_irqrestore(&snic->snic_lock, flags);
  123. return;
  124. }
  125. spin_unlock_irqrestore(&snic->snic_lock, flags);
  126. queue_work(snic_glob->event_q, &snic->link_work);
  127. } /* end of snic_handle_link_event */
  128. /*
  129. * snic_notify_set : sets notification area
  130. * This notification area is to receive events from fw
  131. * Note: snic supports only MSIX interrupts, in which we can just call
  132. * svnic_dev_notify_set directly
  133. */
  134. static int
  135. snic_notify_set(struct snic *snic)
  136. {
  137. int ret = 0;
  138. enum vnic_dev_intr_mode intr_mode;
  139. intr_mode = svnic_dev_get_intr_mode(snic->vdev);
  140. if (intr_mode == VNIC_DEV_INTR_MODE_MSIX) {
  141. ret = svnic_dev_notify_set(snic->vdev, SNIC_MSIX_ERR_NOTIFY);
  142. } else {
  143. SNIC_HOST_ERR(snic->shost,
  144. "Interrupt mode should be setup before devcmd notify set %d\n",
  145. intr_mode);
  146. ret = -1;
  147. }
  148. return ret;
  149. } /* end of snic_notify_set */
  150. /*
  151. * snic_dev_wait : polls vnic open status.
  152. */
  153. static int
  154. snic_dev_wait(struct vnic_dev *vdev,
  155. int (*start)(struct vnic_dev *, int),
  156. int (*finished)(struct vnic_dev *, int *),
  157. int arg)
  158. {
  159. unsigned long time;
  160. int ret, done;
  161. int retry_cnt = 0;
  162. ret = start(vdev, arg);
  163. if (ret)
  164. return ret;
  165. /*
  166. * Wait for func to complete...2 seconds max.
  167. *
  168. * Sometimes schedule_timeout_uninterruptible take long time
  169. * to wakeup, which results skipping retry. The retry counter
  170. * ensures to retry at least two times.
  171. */
  172. time = jiffies + (HZ * 2);
  173. do {
  174. ret = finished(vdev, &done);
  175. if (ret)
  176. return ret;
  177. if (done)
  178. return 0;
  179. schedule_timeout_uninterruptible(HZ/10);
  180. ++retry_cnt;
  181. } while (time_after(time, jiffies) || (retry_cnt < 3));
  182. return -ETIMEDOUT;
  183. } /* end of snic_dev_wait */
  184. /*
  185. * snic_cleanup: called by snic_remove
  186. * Stops the snic device, masks all interrupts, Completed CQ entries are
  187. * drained. Posted WQ/RQ/Copy-WQ entries are cleanup
  188. */
  189. static int
  190. snic_cleanup(struct snic *snic)
  191. {
  192. unsigned int i;
  193. int ret;
  194. svnic_dev_disable(snic->vdev);
  195. for (i = 0; i < snic->intr_count; i++)
  196. svnic_intr_mask(&snic->intr[i]);
  197. for (i = 0; i < snic->wq_count; i++) {
  198. ret = svnic_wq_disable(&snic->wq[i]);
  199. if (ret)
  200. return ret;
  201. }
  202. /* Clean up completed IOs */
  203. snic_fwcq_cmpl_handler(snic, -1);
  204. snic_wq_cmpl_handler(snic, -1);
  205. /* Clean up the IOs that have not completed */
  206. for (i = 0; i < snic->wq_count; i++)
  207. svnic_wq_clean(&snic->wq[i], snic_free_wq_buf);
  208. for (i = 0; i < snic->cq_count; i++)
  209. svnic_cq_clean(&snic->cq[i]);
  210. for (i = 0; i < snic->intr_count; i++)
  211. svnic_intr_clean(&snic->intr[i]);
  212. /* Cleanup snic specific requests */
  213. snic_free_all_untagged_reqs(snic);
  214. /* Cleanup Pending SCSI commands */
  215. snic_shutdown_scsi_cleanup(snic);
  216. for (i = 0; i < SNIC_REQ_MAX_CACHES; i++)
  217. mempool_destroy(snic->req_pool[i]);
  218. return 0;
  219. } /* end of snic_cleanup */
  220. static void
  221. snic_iounmap(struct snic *snic)
  222. {
  223. if (snic->bar0.vaddr)
  224. iounmap(snic->bar0.vaddr);
  225. }
  226. /*
  227. * snic_vdev_open_done : polls for svnic_dev_open cmd completion.
  228. */
  229. static int
  230. snic_vdev_open_done(struct vnic_dev *vdev, int *done)
  231. {
  232. struct snic *snic = svnic_dev_priv(vdev);
  233. int ret;
  234. int nretries = 5;
  235. do {
  236. ret = svnic_dev_open_done(vdev, done);
  237. if (ret == 0)
  238. break;
  239. SNIC_HOST_INFO(snic->shost, "VNIC_DEV_OPEN Timedout.\n");
  240. } while (nretries--);
  241. return ret;
  242. } /* end of snic_vdev_open_done */
  243. /*
  244. * snic_add_host : registers scsi host with ML
  245. */
  246. static int
  247. snic_add_host(struct Scsi_Host *shost, struct pci_dev *pdev)
  248. {
  249. int ret = 0;
  250. ret = scsi_add_host(shost, &pdev->dev);
  251. if (ret) {
  252. SNIC_HOST_ERR(shost,
  253. "snic: scsi_add_host failed. %d\n",
  254. ret);
  255. return ret;
  256. }
  257. SNIC_BUG_ON(shost->work_q != NULL);
  258. snprintf(shost->work_q_name, sizeof(shost->work_q_name), "scsi_wq_%d",
  259. shost->host_no);
  260. shost->work_q = create_singlethread_workqueue(shost->work_q_name);
  261. if (!shost->work_q) {
  262. SNIC_HOST_ERR(shost, "Failed to Create ScsiHost wq.\n");
  263. ret = -ENOMEM;
  264. }
  265. return ret;
  266. } /* end of snic_add_host */
  267. static void
  268. snic_del_host(struct Scsi_Host *shost)
  269. {
  270. if (!shost->work_q)
  271. return;
  272. destroy_workqueue(shost->work_q);
  273. shost->work_q = NULL;
  274. scsi_remove_host(shost);
  275. }
  276. int
  277. snic_get_state(struct snic *snic)
  278. {
  279. return atomic_read(&snic->state);
  280. }
  281. void
  282. snic_set_state(struct snic *snic, enum snic_state state)
  283. {
  284. SNIC_HOST_INFO(snic->shost, "snic state change from %s to %s\n",
  285. snic_state_to_str(snic_get_state(snic)),
  286. snic_state_to_str(state));
  287. atomic_set(&snic->state, state);
  288. }
  289. /*
  290. * snic_probe : Initialize the snic interface.
  291. */
  292. static int
  293. snic_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
  294. {
  295. struct Scsi_Host *shost;
  296. struct snic *snic;
  297. mempool_t *pool;
  298. unsigned long flags;
  299. u32 max_ios = 0;
  300. int ret, i;
  301. /* Device Information */
  302. SNIC_INFO("snic device %4x:%4x:%4x:%4x: ",
  303. pdev->vendor, pdev->device, pdev->subsystem_vendor,
  304. pdev->subsystem_device);
  305. SNIC_INFO("snic device bus %x: slot %x: fn %x\n",
  306. pdev->bus->number, PCI_SLOT(pdev->devfn),
  307. PCI_FUNC(pdev->devfn));
  308. /*
  309. * Allocate SCSI Host and setup association between host, and snic
  310. */
  311. shost = scsi_host_alloc(&snic_host_template, sizeof(struct snic));
  312. if (!shost) {
  313. SNIC_ERR("Unable to alloc scsi_host\n");
  314. ret = -ENOMEM;
  315. goto prob_end;
  316. }
  317. snic = shost_priv(shost);
  318. snic->shost = shost;
  319. snprintf(snic->name, sizeof(snic->name) - 1, "%s%d", SNIC_DRV_NAME,
  320. shost->host_no);
  321. SNIC_HOST_INFO(shost,
  322. "snic%d = %p shost = %p device bus %x: slot %x: fn %x\n",
  323. shost->host_no, snic, shost, pdev->bus->number,
  324. PCI_SLOT(pdev->devfn), PCI_FUNC(pdev->devfn));
  325. #ifdef CONFIG_SCSI_SNIC_DEBUG_FS
  326. /* Per snic debugfs init */
  327. ret = snic_stats_debugfs_init(snic);
  328. if (ret) {
  329. SNIC_HOST_ERR(snic->shost,
  330. "Failed to initialize debugfs stats\n");
  331. snic_stats_debugfs_remove(snic);
  332. }
  333. #endif
  334. /* Setup PCI Resources */
  335. pci_set_drvdata(pdev, snic);
  336. snic->pdev = pdev;
  337. ret = pci_enable_device(pdev);
  338. if (ret) {
  339. SNIC_HOST_ERR(shost,
  340. "Cannot enable PCI Resources, aborting : %d\n",
  341. ret);
  342. goto err_free_snic;
  343. }
  344. ret = pci_request_regions(pdev, SNIC_DRV_NAME);
  345. if (ret) {
  346. SNIC_HOST_ERR(shost,
  347. "Cannot obtain PCI Resources, aborting : %d\n",
  348. ret);
  349. goto err_pci_disable;
  350. }
  351. pci_set_master(pdev);
  352. /*
  353. * Query PCI Controller on system for DMA addressing
  354. * limitation for the device. Try 43-bit first, and
  355. * fail to 32-bit.
  356. */
  357. ret = pci_set_dma_mask(pdev, DMA_BIT_MASK(43));
  358. if (ret) {
  359. ret = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
  360. if (ret) {
  361. SNIC_HOST_ERR(shost,
  362. "No Usable DMA Configuration, aborting %d\n",
  363. ret);
  364. goto err_rel_regions;
  365. }
  366. ret = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(32));
  367. if (ret) {
  368. SNIC_HOST_ERR(shost,
  369. "Unable to obtain 32-bit DMA for consistent allocations, aborting: %d\n",
  370. ret);
  371. goto err_rel_regions;
  372. }
  373. } else {
  374. ret = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(43));
  375. if (ret) {
  376. SNIC_HOST_ERR(shost,
  377. "Unable to obtain 43-bit DMA for consistent allocations. aborting: %d\n",
  378. ret);
  379. goto err_rel_regions;
  380. }
  381. }
  382. /* Map vNIC resources from BAR0 */
  383. if (!(pci_resource_flags(pdev, 0) & IORESOURCE_MEM)) {
  384. SNIC_HOST_ERR(shost, "BAR0 not memory mappable aborting.\n");
  385. ret = -ENODEV;
  386. goto err_rel_regions;
  387. }
  388. snic->bar0.vaddr = pci_iomap(pdev, 0, 0);
  389. if (!snic->bar0.vaddr) {
  390. SNIC_HOST_ERR(shost,
  391. "Cannot memory map BAR0 res hdr aborting.\n");
  392. ret = -ENODEV;
  393. goto err_rel_regions;
  394. }
  395. snic->bar0.bus_addr = pci_resource_start(pdev, 0);
  396. snic->bar0.len = pci_resource_len(pdev, 0);
  397. SNIC_BUG_ON(snic->bar0.bus_addr == 0);
  398. /* Devcmd2 Resource Allocation and Initialization */
  399. snic->vdev = svnic_dev_alloc_discover(NULL, snic, pdev, &snic->bar0, 1);
  400. if (!snic->vdev) {
  401. SNIC_HOST_ERR(shost, "vNIC Resource Discovery Failed.\n");
  402. ret = -ENODEV;
  403. goto err_iounmap;
  404. }
  405. ret = svnic_dev_cmd_init(snic->vdev, 0);
  406. if (ret) {
  407. SNIC_HOST_INFO(shost, "Devcmd2 Init Failed. err = %d\n", ret);
  408. goto err_vnic_unreg;
  409. }
  410. ret = snic_dev_wait(snic->vdev, svnic_dev_open, snic_vdev_open_done, 0);
  411. if (ret) {
  412. SNIC_HOST_ERR(shost,
  413. "vNIC dev open failed, aborting. %d\n",
  414. ret);
  415. goto err_vnic_unreg;
  416. }
  417. ret = svnic_dev_init(snic->vdev, 0);
  418. if (ret) {
  419. SNIC_HOST_ERR(shost,
  420. "vNIC dev init failed. aborting. %d\n",
  421. ret);
  422. goto err_dev_close;
  423. }
  424. /* Get vNIC information */
  425. ret = snic_get_vnic_config(snic);
  426. if (ret) {
  427. SNIC_HOST_ERR(shost,
  428. "Get vNIC configuration failed, aborting. %d\n",
  429. ret);
  430. goto err_dev_close;
  431. }
  432. /* Configure Maximum Outstanding IO reqs */
  433. max_ios = snic->config.io_throttle_count;
  434. if (max_ios != SNIC_UCSM_DFLT_THROTTLE_CNT_BLD)
  435. shost->can_queue = min_t(u32, SNIC_MAX_IO_REQ,
  436. max_t(u32, SNIC_MIN_IO_REQ, max_ios));
  437. snic->max_tag_id = shost->can_queue;
  438. ret = scsi_init_shared_tag_map(shost, snic->max_tag_id);
  439. if (ret) {
  440. SNIC_HOST_ERR(shost,
  441. "Unable to alloc shared tag map. %d\n",
  442. ret);
  443. goto err_dev_close;
  444. }
  445. shost->max_lun = snic->config.luns_per_tgt;
  446. shost->max_id = SNIC_MAX_TARGET;
  447. shost->max_cmd_len = MAX_COMMAND_SIZE; /*defined in scsi_cmnd.h*/
  448. snic_get_res_counts(snic);
  449. /*
  450. * Assumption: Only MSIx is supported
  451. */
  452. ret = snic_set_intr_mode(snic);
  453. if (ret) {
  454. SNIC_HOST_ERR(shost,
  455. "Failed to set intr mode aborting. %d\n",
  456. ret);
  457. goto err_dev_close;
  458. }
  459. ret = snic_alloc_vnic_res(snic);
  460. if (ret) {
  461. SNIC_HOST_ERR(shost,
  462. "Failed to alloc vNIC resources aborting. %d\n",
  463. ret);
  464. goto err_clear_intr;
  465. }
  466. /* Initialize specific lists */
  467. INIT_LIST_HEAD(&snic->list);
  468. /*
  469. * spl_cmd_list for maintaining snic specific cmds
  470. * such as EXCH_VER_REQ, REPORT_TARGETS etc
  471. */
  472. INIT_LIST_HEAD(&snic->spl_cmd_list);
  473. spin_lock_init(&snic->spl_cmd_lock);
  474. /* initialize all snic locks */
  475. spin_lock_init(&snic->snic_lock);
  476. for (i = 0; i < SNIC_WQ_MAX; i++)
  477. spin_lock_init(&snic->wq_lock[i]);
  478. for (i = 0; i < SNIC_IO_LOCKS; i++)
  479. spin_lock_init(&snic->io_req_lock[i]);
  480. pool = mempool_create_slab_pool(2,
  481. snic_glob->req_cache[SNIC_REQ_CACHE_DFLT_SGL]);
  482. if (!pool) {
  483. SNIC_HOST_ERR(shost, "dflt sgl pool creation failed\n");
  484. goto err_free_res;
  485. }
  486. snic->req_pool[SNIC_REQ_CACHE_DFLT_SGL] = pool;
  487. pool = mempool_create_slab_pool(2,
  488. snic_glob->req_cache[SNIC_REQ_CACHE_MAX_SGL]);
  489. if (!pool) {
  490. SNIC_HOST_ERR(shost, "max sgl pool creation failed\n");
  491. goto err_free_dflt_sgl_pool;
  492. }
  493. snic->req_pool[SNIC_REQ_CACHE_MAX_SGL] = pool;
  494. pool = mempool_create_slab_pool(2,
  495. snic_glob->req_cache[SNIC_REQ_TM_CACHE]);
  496. if (!pool) {
  497. SNIC_HOST_ERR(shost, "snic tmreq info pool creation failed.\n");
  498. goto err_free_max_sgl_pool;
  499. }
  500. snic->req_pool[SNIC_REQ_TM_CACHE] = pool;
  501. /* Initialize snic state */
  502. atomic_set(&snic->state, SNIC_INIT);
  503. atomic_set(&snic->ios_inflight, 0);
  504. /* Setup notification buffer area */
  505. ret = snic_notify_set(snic);
  506. if (ret) {
  507. SNIC_HOST_ERR(shost,
  508. "Failed to alloc notify buffer aborting. %d\n",
  509. ret);
  510. goto err_free_tmreq_pool;
  511. }
  512. /*
  513. * Initialization done with PCI system, hardware, firmware.
  514. * Add shost to SCSI
  515. */
  516. ret = snic_add_host(shost, pdev);
  517. if (ret) {
  518. SNIC_HOST_ERR(shost,
  519. "Adding scsi host Failed ... exiting. %d\n",
  520. ret);
  521. goto err_notify_unset;
  522. }
  523. spin_lock_irqsave(&snic_glob->snic_list_lock, flags);
  524. list_add_tail(&snic->list, &snic_glob->snic_list);
  525. spin_unlock_irqrestore(&snic_glob->snic_list_lock, flags);
  526. snic_disc_init(&snic->disc);
  527. INIT_WORK(&snic->tgt_work, snic_handle_tgt_disc);
  528. INIT_WORK(&snic->disc_work, snic_handle_disc);
  529. INIT_WORK(&snic->link_work, snic_handle_link);
  530. /* Enable all queues */
  531. for (i = 0; i < snic->wq_count; i++)
  532. svnic_wq_enable(&snic->wq[i]);
  533. ret = svnic_dev_enable_wait(snic->vdev);
  534. if (ret) {
  535. SNIC_HOST_ERR(shost,
  536. "vNIC dev enable failed w/ error %d\n",
  537. ret);
  538. goto err_vdev_enable;
  539. }
  540. ret = snic_request_intr(snic);
  541. if (ret) {
  542. SNIC_HOST_ERR(shost, "Unable to request irq. %d\n", ret);
  543. goto err_req_intr;
  544. }
  545. for (i = 0; i < snic->intr_count; i++)
  546. svnic_intr_unmask(&snic->intr[i]);
  547. snic_set_state(snic, SNIC_ONLINE);
  548. /* Get snic params */
  549. ret = snic_get_conf(snic);
  550. if (ret) {
  551. SNIC_HOST_ERR(shost,
  552. "Failed to get snic io config from FW w err %d\n",
  553. ret);
  554. goto err_get_conf;
  555. }
  556. ret = snic_disc_start(snic);
  557. if (ret) {
  558. SNIC_HOST_ERR(shost, "snic_probe:Discovery Failed w err = %d\n",
  559. ret);
  560. goto err_get_conf;
  561. }
  562. SNIC_HOST_INFO(shost, "SNIC Device Probe Successful.\n");
  563. return 0;
  564. err_get_conf:
  565. snic_free_all_untagged_reqs(snic);
  566. for (i = 0; i < snic->intr_count; i++)
  567. svnic_intr_mask(&snic->intr[i]);
  568. snic_free_intr(snic);
  569. err_req_intr:
  570. svnic_dev_disable(snic->vdev);
  571. err_vdev_enable:
  572. for (i = 0; i < snic->wq_count; i++) {
  573. int rc = 0;
  574. rc = svnic_wq_disable(&snic->wq[i]);
  575. if (rc) {
  576. SNIC_HOST_ERR(shost,
  577. "WQ Disable Failed w/ err = %d\n", rc);
  578. break;
  579. }
  580. }
  581. snic_del_host(snic->shost);
  582. err_notify_unset:
  583. svnic_dev_notify_unset(snic->vdev);
  584. err_free_tmreq_pool:
  585. mempool_destroy(snic->req_pool[SNIC_REQ_TM_CACHE]);
  586. err_free_max_sgl_pool:
  587. mempool_destroy(snic->req_pool[SNIC_REQ_CACHE_MAX_SGL]);
  588. err_free_dflt_sgl_pool:
  589. mempool_destroy(snic->req_pool[SNIC_REQ_CACHE_DFLT_SGL]);
  590. err_free_res:
  591. snic_free_vnic_res(snic);
  592. err_clear_intr:
  593. snic_clear_intr_mode(snic);
  594. err_dev_close:
  595. svnic_dev_close(snic->vdev);
  596. err_vnic_unreg:
  597. svnic_dev_unregister(snic->vdev);
  598. err_iounmap:
  599. snic_iounmap(snic);
  600. err_rel_regions:
  601. pci_release_regions(pdev);
  602. err_pci_disable:
  603. pci_disable_device(pdev);
  604. err_free_snic:
  605. #ifdef CONFIG_SCSI_SNIC_DEBUG_FS
  606. snic_stats_debugfs_remove(snic);
  607. #endif
  608. scsi_host_put(shost);
  609. pci_set_drvdata(pdev, NULL);
  610. prob_end:
  611. SNIC_INFO("sNIC device : bus %d: slot %d: fn %d Registration Failed.\n",
  612. pdev->bus->number, PCI_SLOT(pdev->devfn),
  613. PCI_FUNC(pdev->devfn));
  614. return ret;
  615. } /* end of snic_probe */
  616. /*
  617. * snic_remove : invoked on unbinding the interface to cleanup the
  618. * resources allocated in snic_probe on initialization.
  619. */
  620. static void
  621. snic_remove(struct pci_dev *pdev)
  622. {
  623. struct snic *snic = pci_get_drvdata(pdev);
  624. unsigned long flags;
  625. if (!snic) {
  626. SNIC_INFO("sNIC dev: bus %d slot %d fn %d snic inst is null.\n",
  627. pdev->bus->number, PCI_SLOT(pdev->devfn),
  628. PCI_FUNC(pdev->devfn));
  629. return;
  630. }
  631. /*
  632. * Mark state so that the workqueue thread stops forwarding
  633. * received frames and link events. ISR and other threads
  634. * that can queue work items will also stop creating work
  635. * items on the snic workqueue
  636. */
  637. snic_set_state(snic, SNIC_OFFLINE);
  638. spin_lock_irqsave(&snic->snic_lock, flags);
  639. snic->stop_link_events = 1;
  640. spin_unlock_irqrestore(&snic->snic_lock, flags);
  641. flush_workqueue(snic_glob->event_q);
  642. snic_disc_term(snic);
  643. spin_lock_irqsave(&snic->snic_lock, flags);
  644. snic->in_remove = 1;
  645. spin_unlock_irqrestore(&snic->snic_lock, flags);
  646. /*
  647. * This stops the snic device, masks all interrupts, Completed
  648. * CQ entries are drained. Posted WQ/RQ/Copy-WQ entries are
  649. * cleanup
  650. */
  651. snic_cleanup(snic);
  652. spin_lock_irqsave(&snic_glob->snic_list_lock, flags);
  653. list_del(&snic->list);
  654. spin_unlock_irqrestore(&snic_glob->snic_list_lock, flags);
  655. snic_tgt_del_all(snic);
  656. #ifdef CONFIG_SCSI_SNIC_DEBUG_FS
  657. snic_stats_debugfs_remove(snic);
  658. #endif
  659. snic_del_host(snic->shost);
  660. svnic_dev_notify_unset(snic->vdev);
  661. snic_free_intr(snic);
  662. snic_free_vnic_res(snic);
  663. snic_clear_intr_mode(snic);
  664. svnic_dev_close(snic->vdev);
  665. svnic_dev_unregister(snic->vdev);
  666. snic_iounmap(snic);
  667. pci_release_regions(pdev);
  668. pci_disable_device(pdev);
  669. pci_set_drvdata(pdev, NULL);
  670. /* this frees Scsi_Host and snic memory (continuous chunk) */
  671. scsi_host_put(snic->shost);
  672. } /* end of snic_remove */
  673. struct snic_global *snic_glob;
  674. /*
  675. * snic_global_data_init: Initialize SNIC Global Data
  676. * Notes: All the global lists, variables should be part of global data
  677. * this helps in debugging.
  678. */
  679. static int
  680. snic_global_data_init(void)
  681. {
  682. int ret = 0;
  683. struct kmem_cache *cachep;
  684. ssize_t len = 0;
  685. snic_glob = kzalloc(sizeof(*snic_glob), GFP_KERNEL);
  686. if (!snic_glob) {
  687. SNIC_ERR("Failed to allocate Global Context.\n");
  688. ret = -ENOMEM;
  689. goto gdi_end;
  690. }
  691. #ifdef CONFIG_SCSI_SNIC_DEBUG_FS
  692. /* Debugfs related Initialization */
  693. /* Create debugfs entries for snic */
  694. ret = snic_debugfs_init();
  695. if (ret < 0) {
  696. SNIC_ERR("Failed to create sysfs dir for tracing and stats.\n");
  697. snic_debugfs_term();
  698. /* continue even if it fails */
  699. }
  700. /* Trace related Initialization */
  701. /* Allocate memory for trace buffer */
  702. ret = snic_trc_init();
  703. if (ret < 0) {
  704. SNIC_ERR("Trace buffer init failed, SNIC tracing disabled\n");
  705. snic_trc_free();
  706. /* continue even if it fails */
  707. }
  708. #endif
  709. INIT_LIST_HEAD(&snic_glob->snic_list);
  710. spin_lock_init(&snic_glob->snic_list_lock);
  711. /* Create a cache for allocation of snic_host_req+default size ESGLs */
  712. len = sizeof(struct snic_req_info);
  713. len += sizeof(struct snic_host_req) + sizeof(struct snic_dflt_sgl);
  714. cachep = kmem_cache_create("snic_req_dfltsgl", len, SNIC_SG_DESC_ALIGN,
  715. SLAB_HWCACHE_ALIGN, NULL);
  716. if (!cachep) {
  717. SNIC_ERR("Failed to create snic default sgl slab\n");
  718. ret = -ENOMEM;
  719. goto err_dflt_req_slab;
  720. }
  721. snic_glob->req_cache[SNIC_REQ_CACHE_DFLT_SGL] = cachep;
  722. /* Create a cache for allocation of max size Extended SGLs */
  723. len = sizeof(struct snic_req_info);
  724. len += sizeof(struct snic_host_req) + sizeof(struct snic_max_sgl);
  725. cachep = kmem_cache_create("snic_req_maxsgl", len, SNIC_SG_DESC_ALIGN,
  726. SLAB_HWCACHE_ALIGN, NULL);
  727. if (!cachep) {
  728. SNIC_ERR("Failed to create snic max sgl slab\n");
  729. ret = -ENOMEM;
  730. goto err_max_req_slab;
  731. }
  732. snic_glob->req_cache[SNIC_REQ_CACHE_MAX_SGL] = cachep;
  733. len = sizeof(struct snic_host_req);
  734. cachep = kmem_cache_create("snic_req_maxsgl", len, SNIC_SG_DESC_ALIGN,
  735. SLAB_HWCACHE_ALIGN, NULL);
  736. if (!cachep) {
  737. SNIC_ERR("Failed to create snic tm req slab\n");
  738. ret = -ENOMEM;
  739. goto err_tmreq_slab;
  740. }
  741. snic_glob->req_cache[SNIC_REQ_TM_CACHE] = cachep;
  742. /* snic_event queue */
  743. snic_glob->event_q = create_singlethread_workqueue("snic_event_wq");
  744. if (!snic_glob->event_q) {
  745. SNIC_ERR("snic event queue create failed\n");
  746. ret = -ENOMEM;
  747. goto err_eventq;
  748. }
  749. return ret;
  750. err_eventq:
  751. kmem_cache_destroy(snic_glob->req_cache[SNIC_REQ_TM_CACHE]);
  752. err_tmreq_slab:
  753. kmem_cache_destroy(snic_glob->req_cache[SNIC_REQ_CACHE_MAX_SGL]);
  754. err_max_req_slab:
  755. kmem_cache_destroy(snic_glob->req_cache[SNIC_REQ_CACHE_DFLT_SGL]);
  756. err_dflt_req_slab:
  757. #ifdef CONFIG_SCSI_SNIC_DEBUG_FS
  758. snic_trc_free();
  759. snic_debugfs_term();
  760. #endif
  761. kfree(snic_glob);
  762. snic_glob = NULL;
  763. gdi_end:
  764. return ret;
  765. } /* end of snic_glob_init */
  766. /*
  767. * snic_global_data_cleanup : Frees SNIC Global Data
  768. */
  769. static void
  770. snic_global_data_cleanup(void)
  771. {
  772. SNIC_BUG_ON(snic_glob == NULL);
  773. destroy_workqueue(snic_glob->event_q);
  774. kmem_cache_destroy(snic_glob->req_cache[SNIC_REQ_TM_CACHE]);
  775. kmem_cache_destroy(snic_glob->req_cache[SNIC_REQ_CACHE_MAX_SGL]);
  776. kmem_cache_destroy(snic_glob->req_cache[SNIC_REQ_CACHE_DFLT_SGL]);
  777. #ifdef CONFIG_SCSI_SNIC_DEBUG_FS
  778. /* Freeing Trace Resources */
  779. snic_trc_free();
  780. /* Freeing Debugfs Resources */
  781. snic_debugfs_term();
  782. #endif
  783. kfree(snic_glob);
  784. snic_glob = NULL;
  785. } /* end of snic_glob_cleanup */
  786. static struct pci_driver snic_driver = {
  787. .name = SNIC_DRV_NAME,
  788. .id_table = snic_id_table,
  789. .probe = snic_probe,
  790. .remove = snic_remove,
  791. };
  792. static int __init
  793. snic_init_module(void)
  794. {
  795. int ret = 0;
  796. #ifndef __x86_64__
  797. SNIC_INFO("SNIC Driver is supported only for x86_64 platforms!\n");
  798. add_taint(TAINT_CPU_OUT_OF_SPEC, LOCKDEP_STILL_OK);
  799. #endif
  800. SNIC_INFO("%s, ver %s\n", SNIC_DRV_DESCRIPTION, SNIC_DRV_VERSION);
  801. ret = snic_global_data_init();
  802. if (ret) {
  803. SNIC_ERR("Failed to Initialize Global Data.\n");
  804. return ret;
  805. }
  806. ret = pci_register_driver(&snic_driver);
  807. if (ret < 0) {
  808. SNIC_ERR("PCI driver register error\n");
  809. goto err_pci_reg;
  810. }
  811. return ret;
  812. err_pci_reg:
  813. snic_global_data_cleanup();
  814. return ret;
  815. }
  816. static void __exit
  817. snic_cleanup_module(void)
  818. {
  819. pci_unregister_driver(&snic_driver);
  820. snic_global_data_cleanup();
  821. }
  822. module_init(snic_init_module);
  823. module_exit(snic_cleanup_module);
  824. MODULE_LICENSE("GPL v2");
  825. MODULE_DESCRIPTION(SNIC_DRV_DESCRIPTION);
  826. MODULE_VERSION(SNIC_DRV_VERSION);
  827. MODULE_DEVICE_TABLE(pci, snic_id_table);
  828. MODULE_AUTHOR("Narsimhulu Musini <nmusini@cisco.com>, "
  829. "Sesidhar Baddela <sebaddel@cisco.com>");