fnic_main.c 34 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright 2008 Cisco Systems, Inc. All rights reserved.
  4. * Copyright 2007 Nuova Systems, Inc. All rights reserved.
  5. */
  6. #include <linux/module.h>
  7. #include <linux/mempool.h>
  8. #include <linux/string.h>
  9. #include <linux/slab.h>
  10. #include <linux/errno.h>
  11. #include <linux/init.h>
  12. #include <linux/pci.h>
  13. #include <linux/skbuff.h>
  14. #include <linux/interrupt.h>
  15. #include <linux/irq.h>
  16. #include <linux/spinlock.h>
  17. #include <linux/workqueue.h>
  18. #include <linux/if_ether.h>
  19. #include <linux/blk-mq-pci.h>
  20. #include <scsi/fc/fc_fip.h>
  21. #include <scsi/scsi_host.h>
  22. #include <scsi/scsi_transport.h>
  23. #include <scsi/scsi_transport_fc.h>
  24. #include <scsi/scsi_tcq.h>
  25. #include <scsi/libfc.h>
  26. #include <scsi/fc_frame.h>
  27. #include "vnic_dev.h"
  28. #include "vnic_intr.h"
  29. #include "vnic_stats.h"
  30. #include "fnic_io.h"
  31. #include "fnic_fip.h"
  32. #include "fnic.h"
  33. #define PCI_DEVICE_ID_CISCO_FNIC 0x0045
  34. /* Timer to poll notification area for events. Used for MSI interrupts */
  35. #define FNIC_NOTIFY_TIMER_PERIOD (2 * HZ)
  36. static struct kmem_cache *fnic_sgl_cache[FNIC_SGL_NUM_CACHES];
  37. static struct kmem_cache *fnic_io_req_cache;
  38. static LIST_HEAD(fnic_list);
  39. static DEFINE_SPINLOCK(fnic_list_lock);
  40. static DEFINE_IDA(fnic_ida);
  41. /* Supported devices by fnic module */
  42. static struct pci_device_id fnic_id_table[] = {
  43. { PCI_DEVICE(PCI_VENDOR_ID_CISCO, PCI_DEVICE_ID_CISCO_FNIC) },
  44. { 0, }
  45. };
  46. MODULE_DESCRIPTION(DRV_DESCRIPTION);
  47. MODULE_AUTHOR("Abhijeet Joglekar <abjoglek@cisco.com>, "
  48. "Joseph R. Eykholt <jeykholt@cisco.com>");
  49. MODULE_LICENSE("GPL v2");
  50. MODULE_VERSION(DRV_VERSION);
  51. MODULE_DEVICE_TABLE(pci, fnic_id_table);
  52. unsigned int fnic_log_level;
  53. module_param(fnic_log_level, int, S_IRUGO|S_IWUSR);
  54. MODULE_PARM_DESC(fnic_log_level, "bit mask of fnic logging levels");
  55. unsigned int io_completions = FNIC_DFLT_IO_COMPLETIONS;
  56. module_param(io_completions, int, S_IRUGO|S_IWUSR);
  57. MODULE_PARM_DESC(io_completions, "Max CQ entries to process at a time");
  58. unsigned int fnic_trace_max_pages = 16;
  59. module_param(fnic_trace_max_pages, uint, S_IRUGO|S_IWUSR);
  60. MODULE_PARM_DESC(fnic_trace_max_pages, "Total allocated memory pages "
  61. "for fnic trace buffer");
  62. unsigned int fnic_fc_trace_max_pages = 64;
  63. module_param(fnic_fc_trace_max_pages, uint, S_IRUGO|S_IWUSR);
  64. MODULE_PARM_DESC(fnic_fc_trace_max_pages,
  65. "Total allocated memory pages for fc trace buffer");
  66. static unsigned int fnic_max_qdepth = FNIC_DFLT_QUEUE_DEPTH;
  67. module_param(fnic_max_qdepth, uint, S_IRUGO|S_IWUSR);
  68. MODULE_PARM_DESC(fnic_max_qdepth, "Queue depth to report for each LUN");
  69. static struct libfc_function_template fnic_transport_template = {
  70. .frame_send = fnic_send,
  71. .lport_set_port_id = fnic_set_port_id,
  72. .fcp_abort_io = fnic_empty_scsi_cleanup,
  73. .fcp_cleanup = fnic_empty_scsi_cleanup,
  74. .exch_mgr_reset = fnic_exch_mgr_reset
  75. };
  76. static int fnic_slave_alloc(struct scsi_device *sdev)
  77. {
  78. struct fc_rport *rport = starget_to_rport(scsi_target(sdev));
  79. if (!rport || fc_remote_port_chkready(rport))
  80. return -ENXIO;
  81. scsi_change_queue_depth(sdev, fnic_max_qdepth);
  82. return 0;
  83. }
  84. static const struct scsi_host_template fnic_host_template = {
  85. .module = THIS_MODULE,
  86. .name = DRV_NAME,
  87. .queuecommand = fnic_queuecommand,
  88. .eh_timed_out = fc_eh_timed_out,
  89. .eh_abort_handler = fnic_abort_cmd,
  90. .eh_device_reset_handler = fnic_device_reset,
  91. .eh_host_reset_handler = fnic_host_reset,
  92. .slave_alloc = fnic_slave_alloc,
  93. .change_queue_depth = scsi_change_queue_depth,
  94. .this_id = -1,
  95. .cmd_per_lun = 3,
  96. .can_queue = FNIC_DFLT_IO_REQ,
  97. .sg_tablesize = FNIC_MAX_SG_DESC_CNT,
  98. .max_sectors = 0xffff,
  99. .shost_groups = fnic_host_groups,
  100. .track_queue_depth = 1,
  101. .cmd_size = sizeof(struct fnic_cmd_priv),
  102. .map_queues = fnic_mq_map_queues_cpus,
  103. };
  104. static void
  105. fnic_set_rport_dev_loss_tmo(struct fc_rport *rport, u32 timeout)
  106. {
  107. if (timeout)
  108. rport->dev_loss_tmo = timeout;
  109. else
  110. rport->dev_loss_tmo = 1;
  111. }
  112. static void fnic_get_host_speed(struct Scsi_Host *shost);
  113. static struct scsi_transport_template *fnic_fc_transport;
  114. static struct fc_host_statistics *fnic_get_stats(struct Scsi_Host *);
  115. static void fnic_reset_host_stats(struct Scsi_Host *);
  116. static struct fc_function_template fnic_fc_functions = {
  117. .show_host_node_name = 1,
  118. .show_host_port_name = 1,
  119. .show_host_supported_classes = 1,
  120. .show_host_supported_fc4s = 1,
  121. .show_host_active_fc4s = 1,
  122. .show_host_maxframe_size = 1,
  123. .show_host_port_id = 1,
  124. .show_host_supported_speeds = 1,
  125. .get_host_speed = fnic_get_host_speed,
  126. .show_host_speed = 1,
  127. .show_host_port_type = 1,
  128. .get_host_port_state = fc_get_host_port_state,
  129. .show_host_port_state = 1,
  130. .show_host_symbolic_name = 1,
  131. .show_rport_maxframe_size = 1,
  132. .show_rport_supported_classes = 1,
  133. .show_host_fabric_name = 1,
  134. .show_starget_node_name = 1,
  135. .show_starget_port_name = 1,
  136. .show_starget_port_id = 1,
  137. .show_rport_dev_loss_tmo = 1,
  138. .set_rport_dev_loss_tmo = fnic_set_rport_dev_loss_tmo,
  139. .issue_fc_host_lip = fnic_reset,
  140. .get_fc_host_stats = fnic_get_stats,
  141. .reset_fc_host_stats = fnic_reset_host_stats,
  142. .dd_fcrport_size = sizeof(struct fc_rport_libfc_priv),
  143. .terminate_rport_io = fnic_terminate_rport_io,
  144. .bsg_request = fc_lport_bsg_request,
  145. };
  146. static void fnic_get_host_speed(struct Scsi_Host *shost)
  147. {
  148. struct fc_lport *lp = shost_priv(shost);
  149. struct fnic *fnic = lport_priv(lp);
  150. u32 port_speed = vnic_dev_port_speed(fnic->vdev);
  151. /* Add in other values as they get defined in fw */
  152. switch (port_speed) {
  153. case DCEM_PORTSPEED_10G:
  154. fc_host_speed(shost) = FC_PORTSPEED_10GBIT;
  155. break;
  156. case DCEM_PORTSPEED_20G:
  157. fc_host_speed(shost) = FC_PORTSPEED_20GBIT;
  158. break;
  159. case DCEM_PORTSPEED_25G:
  160. fc_host_speed(shost) = FC_PORTSPEED_25GBIT;
  161. break;
  162. case DCEM_PORTSPEED_40G:
  163. case DCEM_PORTSPEED_4x10G:
  164. fc_host_speed(shost) = FC_PORTSPEED_40GBIT;
  165. break;
  166. case DCEM_PORTSPEED_100G:
  167. fc_host_speed(shost) = FC_PORTSPEED_100GBIT;
  168. break;
  169. default:
  170. fc_host_speed(shost) = FC_PORTSPEED_UNKNOWN;
  171. break;
  172. }
  173. }
  174. static struct fc_host_statistics *fnic_get_stats(struct Scsi_Host *host)
  175. {
  176. int ret;
  177. struct fc_lport *lp = shost_priv(host);
  178. struct fnic *fnic = lport_priv(lp);
  179. struct fc_host_statistics *stats = &lp->host_stats;
  180. struct vnic_stats *vs;
  181. unsigned long flags;
  182. if (time_before(jiffies, fnic->stats_time + HZ / FNIC_STATS_RATE_LIMIT))
  183. return stats;
  184. fnic->stats_time = jiffies;
  185. spin_lock_irqsave(&fnic->fnic_lock, flags);
  186. ret = vnic_dev_stats_dump(fnic->vdev, &fnic->stats);
  187. spin_unlock_irqrestore(&fnic->fnic_lock, flags);
  188. if (ret) {
  189. FNIC_MAIN_DBG(KERN_DEBUG, fnic->lport->host, fnic->fnic_num,
  190. "fnic: Get vnic stats failed"
  191. " 0x%x", ret);
  192. return stats;
  193. }
  194. vs = fnic->stats;
  195. stats->tx_frames = vs->tx.tx_unicast_frames_ok;
  196. stats->tx_words = vs->tx.tx_unicast_bytes_ok / 4;
  197. stats->rx_frames = vs->rx.rx_unicast_frames_ok;
  198. stats->rx_words = vs->rx.rx_unicast_bytes_ok / 4;
  199. stats->error_frames = vs->tx.tx_errors + vs->rx.rx_errors;
  200. stats->dumped_frames = vs->tx.tx_drops + vs->rx.rx_drop;
  201. stats->invalid_crc_count = vs->rx.rx_crc_errors;
  202. stats->seconds_since_last_reset =
  203. (jiffies - fnic->stats_reset_time) / HZ;
  204. stats->fcp_input_megabytes = div_u64(fnic->fcp_input_bytes, 1000000);
  205. stats->fcp_output_megabytes = div_u64(fnic->fcp_output_bytes, 1000000);
  206. return stats;
  207. }
  208. /*
  209. * fnic_dump_fchost_stats
  210. * note : dumps fc_statistics into system logs
  211. */
  212. void fnic_dump_fchost_stats(struct Scsi_Host *host,
  213. struct fc_host_statistics *stats)
  214. {
  215. FNIC_MAIN_NOTE(KERN_NOTICE, host,
  216. "fnic: seconds since last reset = %llu\n",
  217. stats->seconds_since_last_reset);
  218. FNIC_MAIN_NOTE(KERN_NOTICE, host,
  219. "fnic: tx frames = %llu\n",
  220. stats->tx_frames);
  221. FNIC_MAIN_NOTE(KERN_NOTICE, host,
  222. "fnic: tx words = %llu\n",
  223. stats->tx_words);
  224. FNIC_MAIN_NOTE(KERN_NOTICE, host,
  225. "fnic: rx frames = %llu\n",
  226. stats->rx_frames);
  227. FNIC_MAIN_NOTE(KERN_NOTICE, host,
  228. "fnic: rx words = %llu\n",
  229. stats->rx_words);
  230. FNIC_MAIN_NOTE(KERN_NOTICE, host,
  231. "fnic: lip count = %llu\n",
  232. stats->lip_count);
  233. FNIC_MAIN_NOTE(KERN_NOTICE, host,
  234. "fnic: nos count = %llu\n",
  235. stats->nos_count);
  236. FNIC_MAIN_NOTE(KERN_NOTICE, host,
  237. "fnic: error frames = %llu\n",
  238. stats->error_frames);
  239. FNIC_MAIN_NOTE(KERN_NOTICE, host,
  240. "fnic: dumped frames = %llu\n",
  241. stats->dumped_frames);
  242. FNIC_MAIN_NOTE(KERN_NOTICE, host,
  243. "fnic: link failure count = %llu\n",
  244. stats->link_failure_count);
  245. FNIC_MAIN_NOTE(KERN_NOTICE, host,
  246. "fnic: loss of sync count = %llu\n",
  247. stats->loss_of_sync_count);
  248. FNIC_MAIN_NOTE(KERN_NOTICE, host,
  249. "fnic: loss of signal count = %llu\n",
  250. stats->loss_of_signal_count);
  251. FNIC_MAIN_NOTE(KERN_NOTICE, host,
  252. "fnic: prim seq protocol err count = %llu\n",
  253. stats->prim_seq_protocol_err_count);
  254. FNIC_MAIN_NOTE(KERN_NOTICE, host,
  255. "fnic: invalid tx word count= %llu\n",
  256. stats->invalid_tx_word_count);
  257. FNIC_MAIN_NOTE(KERN_NOTICE, host,
  258. "fnic: invalid crc count = %llu\n",
  259. stats->invalid_crc_count);
  260. FNIC_MAIN_NOTE(KERN_NOTICE, host,
  261. "fnic: fcp input requests = %llu\n",
  262. stats->fcp_input_requests);
  263. FNIC_MAIN_NOTE(KERN_NOTICE, host,
  264. "fnic: fcp output requests = %llu\n",
  265. stats->fcp_output_requests);
  266. FNIC_MAIN_NOTE(KERN_NOTICE, host,
  267. "fnic: fcp control requests = %llu\n",
  268. stats->fcp_control_requests);
  269. FNIC_MAIN_NOTE(KERN_NOTICE, host,
  270. "fnic: fcp input megabytes = %llu\n",
  271. stats->fcp_input_megabytes);
  272. FNIC_MAIN_NOTE(KERN_NOTICE, host,
  273. "fnic: fcp output megabytes = %llu\n",
  274. stats->fcp_output_megabytes);
  275. return;
  276. }
  277. /*
  278. * fnic_reset_host_stats : clears host stats
  279. * note : called when reset_statistics set under sysfs dir
  280. */
  281. static void fnic_reset_host_stats(struct Scsi_Host *host)
  282. {
  283. int ret;
  284. struct fc_lport *lp = shost_priv(host);
  285. struct fnic *fnic = lport_priv(lp);
  286. struct fc_host_statistics *stats;
  287. unsigned long flags;
  288. /* dump current stats, before clearing them */
  289. stats = fnic_get_stats(host);
  290. fnic_dump_fchost_stats(host, stats);
  291. spin_lock_irqsave(&fnic->fnic_lock, flags);
  292. ret = vnic_dev_stats_clear(fnic->vdev);
  293. spin_unlock_irqrestore(&fnic->fnic_lock, flags);
  294. if (ret) {
  295. FNIC_MAIN_DBG(KERN_DEBUG, fnic->lport->host, fnic->fnic_num,
  296. "fnic: Reset vnic stats failed"
  297. " 0x%x", ret);
  298. return;
  299. }
  300. fnic->stats_reset_time = jiffies;
  301. memset(stats, 0, sizeof(*stats));
  302. return;
  303. }
  304. void fnic_log_q_error(struct fnic *fnic)
  305. {
  306. unsigned int i;
  307. u32 error_status;
  308. for (i = 0; i < fnic->raw_wq_count; i++) {
  309. error_status = ioread32(&fnic->wq[i].ctrl->error_status);
  310. if (error_status)
  311. shost_printk(KERN_ERR, fnic->lport->host,
  312. "WQ[%d] error_status"
  313. " %d\n", i, error_status);
  314. }
  315. for (i = 0; i < fnic->rq_count; i++) {
  316. error_status = ioread32(&fnic->rq[i].ctrl->error_status);
  317. if (error_status)
  318. shost_printk(KERN_ERR, fnic->lport->host,
  319. "RQ[%d] error_status"
  320. " %d\n", i, error_status);
  321. }
  322. for (i = 0; i < fnic->wq_copy_count; i++) {
  323. error_status = ioread32(&fnic->hw_copy_wq[i].ctrl->error_status);
  324. if (error_status)
  325. shost_printk(KERN_ERR, fnic->lport->host,
  326. "CWQ[%d] error_status"
  327. " %d\n", i, error_status);
  328. }
  329. }
  330. void fnic_handle_link_event(struct fnic *fnic)
  331. {
  332. unsigned long flags;
  333. spin_lock_irqsave(&fnic->fnic_lock, flags);
  334. if (fnic->stop_rx_link_events) {
  335. spin_unlock_irqrestore(&fnic->fnic_lock, flags);
  336. return;
  337. }
  338. spin_unlock_irqrestore(&fnic->fnic_lock, flags);
  339. queue_work(fnic_event_queue, &fnic->link_work);
  340. }
  341. static int fnic_notify_set(struct fnic *fnic)
  342. {
  343. int err;
  344. switch (vnic_dev_get_intr_mode(fnic->vdev)) {
  345. case VNIC_DEV_INTR_MODE_INTX:
  346. err = vnic_dev_notify_set(fnic->vdev, FNIC_INTX_NOTIFY);
  347. break;
  348. case VNIC_DEV_INTR_MODE_MSI:
  349. err = vnic_dev_notify_set(fnic->vdev, -1);
  350. break;
  351. case VNIC_DEV_INTR_MODE_MSIX:
  352. err = vnic_dev_notify_set(fnic->vdev, fnic->wq_copy_count + fnic->copy_wq_base);
  353. break;
  354. default:
  355. shost_printk(KERN_ERR, fnic->lport->host,
  356. "Interrupt mode should be set up"
  357. " before devcmd notify set %d\n",
  358. vnic_dev_get_intr_mode(fnic->vdev));
  359. err = -1;
  360. break;
  361. }
  362. return err;
  363. }
  364. static void fnic_notify_timer(struct timer_list *t)
  365. {
  366. struct fnic *fnic = from_timer(fnic, t, notify_timer);
  367. fnic_handle_link_event(fnic);
  368. mod_timer(&fnic->notify_timer,
  369. round_jiffies(jiffies + FNIC_NOTIFY_TIMER_PERIOD));
  370. }
  371. static void fnic_fip_notify_timer(struct timer_list *t)
  372. {
  373. struct fnic *fnic = from_timer(fnic, t, fip_timer);
  374. fnic_handle_fip_timer(fnic);
  375. }
  376. static void fnic_notify_timer_start(struct fnic *fnic)
  377. {
  378. switch (vnic_dev_get_intr_mode(fnic->vdev)) {
  379. case VNIC_DEV_INTR_MODE_MSI:
  380. /*
  381. * Schedule first timeout immediately. The driver is
  382. * initiatialized and ready to look for link up notification
  383. */
  384. mod_timer(&fnic->notify_timer, jiffies);
  385. break;
  386. default:
  387. /* Using intr for notification for INTx/MSI-X */
  388. break;
  389. }
  390. }
  391. static int fnic_dev_wait(struct vnic_dev *vdev,
  392. int (*start)(struct vnic_dev *, int),
  393. int (*finished)(struct vnic_dev *, int *),
  394. int arg)
  395. {
  396. unsigned long time;
  397. int done;
  398. int err;
  399. int count;
  400. count = 0;
  401. err = start(vdev, arg);
  402. if (err)
  403. return err;
  404. /* Wait for func to complete.
  405. * Sometime schedule_timeout_uninterruptible take long time
  406. * to wake up so we do not retry as we are only waiting for
  407. * 2 seconds in while loop. By adding count, we make sure
  408. * we try atleast three times before returning -ETIMEDOUT
  409. */
  410. time = jiffies + (HZ * 2);
  411. do {
  412. err = finished(vdev, &done);
  413. count++;
  414. if (err)
  415. return err;
  416. if (done)
  417. return 0;
  418. schedule_timeout_uninterruptible(HZ / 10);
  419. } while (time_after(time, jiffies) || (count < 3));
  420. return -ETIMEDOUT;
  421. }
  422. static int fnic_cleanup(struct fnic *fnic)
  423. {
  424. unsigned int i;
  425. int err;
  426. int raw_wq_rq_counts;
  427. vnic_dev_disable(fnic->vdev);
  428. for (i = 0; i < fnic->intr_count; i++)
  429. vnic_intr_mask(&fnic->intr[i]);
  430. for (i = 0; i < fnic->rq_count; i++) {
  431. err = vnic_rq_disable(&fnic->rq[i]);
  432. if (err)
  433. return err;
  434. }
  435. for (i = 0; i < fnic->raw_wq_count; i++) {
  436. err = vnic_wq_disable(&fnic->wq[i]);
  437. if (err)
  438. return err;
  439. }
  440. for (i = 0; i < fnic->wq_copy_count; i++) {
  441. err = vnic_wq_copy_disable(&fnic->hw_copy_wq[i]);
  442. if (err)
  443. return err;
  444. raw_wq_rq_counts = fnic->raw_wq_count + fnic->rq_count;
  445. fnic_wq_copy_cmpl_handler(fnic, -1, i + raw_wq_rq_counts);
  446. }
  447. /* Clean up completed IOs and FCS frames */
  448. fnic_wq_cmpl_handler(fnic, -1);
  449. fnic_rq_cmpl_handler(fnic, -1);
  450. /* Clean up the IOs and FCS frames that have not completed */
  451. for (i = 0; i < fnic->raw_wq_count; i++)
  452. vnic_wq_clean(&fnic->wq[i], fnic_free_wq_buf);
  453. for (i = 0; i < fnic->rq_count; i++)
  454. vnic_rq_clean(&fnic->rq[i], fnic_free_rq_buf);
  455. for (i = 0; i < fnic->wq_copy_count; i++)
  456. vnic_wq_copy_clean(&fnic->hw_copy_wq[i],
  457. fnic_wq_copy_cleanup_handler);
  458. for (i = 0; i < fnic->cq_count; i++)
  459. vnic_cq_clean(&fnic->cq[i]);
  460. for (i = 0; i < fnic->intr_count; i++)
  461. vnic_intr_clean(&fnic->intr[i]);
  462. mempool_destroy(fnic->io_req_pool);
  463. for (i = 0; i < FNIC_SGL_NUM_CACHES; i++)
  464. mempool_destroy(fnic->io_sgl_pool[i]);
  465. return 0;
  466. }
  467. static void fnic_iounmap(struct fnic *fnic)
  468. {
  469. if (fnic->bar0.vaddr)
  470. iounmap(fnic->bar0.vaddr);
  471. }
  472. /**
  473. * fnic_get_mac() - get assigned data MAC address for FIP code.
  474. * @lport: local port.
  475. */
  476. static u8 *fnic_get_mac(struct fc_lport *lport)
  477. {
  478. struct fnic *fnic = lport_priv(lport);
  479. return fnic->data_src_addr;
  480. }
  481. static void fnic_set_vlan(struct fnic *fnic, u16 vlan_id)
  482. {
  483. vnic_dev_set_default_vlan(fnic->vdev, vlan_id);
  484. }
  485. static int fnic_scsi_drv_init(struct fnic *fnic)
  486. {
  487. struct Scsi_Host *host = fnic->lport->host;
  488. /* Configure maximum outstanding IO reqs*/
  489. if (fnic->config.io_throttle_count != FNIC_UCSM_DFLT_THROTTLE_CNT_BLD)
  490. host->can_queue = min_t(u32, FNIC_MAX_IO_REQ,
  491. max_t(u32, FNIC_MIN_IO_REQ,
  492. fnic->config.io_throttle_count));
  493. fnic->fnic_max_tag_id = host->can_queue;
  494. host->max_lun = fnic->config.luns_per_tgt;
  495. host->max_id = FNIC_MAX_FCP_TARGET;
  496. host->max_cmd_len = FCOE_MAX_CMD_LEN;
  497. host->nr_hw_queues = fnic->wq_copy_count;
  498. shost_printk(KERN_INFO, host,
  499. "fnic: can_queue: %d max_lun: %llu",
  500. host->can_queue, host->max_lun);
  501. shost_printk(KERN_INFO, host,
  502. "fnic: max_id: %d max_cmd_len: %d nr_hw_queues: %d",
  503. host->max_id, host->max_cmd_len, host->nr_hw_queues);
  504. return 0;
  505. }
  506. void fnic_mq_map_queues_cpus(struct Scsi_Host *host)
  507. {
  508. struct fc_lport *lp = shost_priv(host);
  509. struct fnic *fnic = lport_priv(lp);
  510. struct pci_dev *l_pdev = fnic->pdev;
  511. int intr_mode = fnic->config.intr_mode;
  512. struct blk_mq_queue_map *qmap = &host->tag_set.map[HCTX_TYPE_DEFAULT];
  513. if (intr_mode == VNIC_DEV_INTR_MODE_MSI || intr_mode == VNIC_DEV_INTR_MODE_INTX) {
  514. FNIC_MAIN_DBG(KERN_ERR, fnic->lport->host, fnic->fnic_num,
  515. "intr_mode is not msix\n");
  516. return;
  517. }
  518. FNIC_MAIN_DBG(KERN_INFO, fnic->lport->host, fnic->fnic_num,
  519. "qmap->nr_queues: %d\n", qmap->nr_queues);
  520. if (l_pdev == NULL) {
  521. FNIC_MAIN_DBG(KERN_ERR, fnic->lport->host, fnic->fnic_num,
  522. "l_pdev is null\n");
  523. return;
  524. }
  525. blk_mq_pci_map_queues(qmap, l_pdev, FNIC_PCI_OFFSET);
  526. }
  527. static int fnic_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
  528. {
  529. struct Scsi_Host *host;
  530. struct fc_lport *lp;
  531. struct fnic *fnic;
  532. mempool_t *pool;
  533. int err = 0;
  534. int fnic_id = 0;
  535. int i;
  536. unsigned long flags;
  537. int hwq;
  538. /*
  539. * Allocate SCSI Host and set up association between host,
  540. * local port, and fnic
  541. */
  542. lp = libfc_host_alloc(&fnic_host_template, sizeof(struct fnic));
  543. if (!lp) {
  544. printk(KERN_ERR PFX "Unable to alloc libfc local port\n");
  545. err = -ENOMEM;
  546. goto err_out;
  547. }
  548. host = lp->host;
  549. fnic = lport_priv(lp);
  550. fnic_id = ida_alloc(&fnic_ida, GFP_KERNEL);
  551. if (fnic_id < 0) {
  552. pr_err("Unable to alloc fnic ID\n");
  553. err = fnic_id;
  554. goto err_out_ida_alloc;
  555. }
  556. fnic->lport = lp;
  557. fnic->ctlr.lp = lp;
  558. fnic->link_events = 0;
  559. fnic->pdev = pdev;
  560. snprintf(fnic->name, sizeof(fnic->name) - 1, "%s%d", DRV_NAME,
  561. host->host_no);
  562. host->transportt = fnic_fc_transport;
  563. fnic->fnic_num = fnic_id;
  564. fnic_stats_debugfs_init(fnic);
  565. err = pci_enable_device(pdev);
  566. if (err) {
  567. shost_printk(KERN_ERR, fnic->lport->host,
  568. "Cannot enable PCI device, aborting.\n");
  569. goto err_out_free_hba;
  570. }
  571. err = pci_request_regions(pdev, DRV_NAME);
  572. if (err) {
  573. shost_printk(KERN_ERR, fnic->lport->host,
  574. "Cannot enable PCI resources, aborting\n");
  575. goto err_out_disable_device;
  576. }
  577. pci_set_master(pdev);
  578. /* Query PCI controller on system for DMA addressing
  579. * limitation for the device. Try 47-bit first, and
  580. * fail to 32-bit. Cisco VIC supports 47 bits only.
  581. */
  582. err = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(47));
  583. if (err) {
  584. err = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32));
  585. if (err) {
  586. shost_printk(KERN_ERR, fnic->lport->host,
  587. "No usable DMA configuration "
  588. "aborting\n");
  589. goto err_out_release_regions;
  590. }
  591. }
  592. /* Map vNIC resources from BAR0 */
  593. if (!(pci_resource_flags(pdev, 0) & IORESOURCE_MEM)) {
  594. shost_printk(KERN_ERR, fnic->lport->host,
  595. "BAR0 not memory-map'able, aborting.\n");
  596. err = -ENODEV;
  597. goto err_out_release_regions;
  598. }
  599. fnic->bar0.vaddr = pci_iomap(pdev, 0, 0);
  600. fnic->bar0.bus_addr = pci_resource_start(pdev, 0);
  601. fnic->bar0.len = pci_resource_len(pdev, 0);
  602. if (!fnic->bar0.vaddr) {
  603. shost_printk(KERN_ERR, fnic->lport->host,
  604. "Cannot memory-map BAR0 res hdr, "
  605. "aborting.\n");
  606. err = -ENODEV;
  607. goto err_out_release_regions;
  608. }
  609. fnic->vdev = vnic_dev_register(NULL, fnic, pdev, &fnic->bar0);
  610. if (!fnic->vdev) {
  611. shost_printk(KERN_ERR, fnic->lport->host,
  612. "vNIC registration failed, "
  613. "aborting.\n");
  614. err = -ENODEV;
  615. goto err_out_iounmap;
  616. }
  617. err = vnic_dev_cmd_init(fnic->vdev);
  618. if (err) {
  619. shost_printk(KERN_ERR, fnic->lport->host,
  620. "vnic_dev_cmd_init() returns %d, aborting\n",
  621. err);
  622. goto err_out_vnic_unregister;
  623. }
  624. err = fnic_dev_wait(fnic->vdev, vnic_dev_open,
  625. vnic_dev_open_done, CMD_OPENF_RQ_ENABLE_THEN_POST);
  626. if (err) {
  627. shost_printk(KERN_ERR, fnic->lport->host,
  628. "vNIC dev open failed, aborting.\n");
  629. goto err_out_dev_cmd_deinit;
  630. }
  631. err = vnic_dev_init(fnic->vdev, 0);
  632. if (err) {
  633. shost_printk(KERN_ERR, fnic->lport->host,
  634. "vNIC dev init failed, aborting.\n");
  635. goto err_out_dev_close;
  636. }
  637. err = vnic_dev_mac_addr(fnic->vdev, fnic->ctlr.ctl_src_addr);
  638. if (err) {
  639. shost_printk(KERN_ERR, fnic->lport->host,
  640. "vNIC get MAC addr failed \n");
  641. goto err_out_dev_close;
  642. }
  643. /* set data_src for point-to-point mode and to keep it non-zero */
  644. memcpy(fnic->data_src_addr, fnic->ctlr.ctl_src_addr, ETH_ALEN);
  645. /* Get vNIC configuration */
  646. err = fnic_get_vnic_config(fnic);
  647. if (err) {
  648. shost_printk(KERN_ERR, fnic->lport->host,
  649. "Get vNIC configuration failed, "
  650. "aborting.\n");
  651. goto err_out_dev_close;
  652. }
  653. /* Setup PCI resources */
  654. pci_set_drvdata(pdev, fnic);
  655. fnic_get_res_counts(fnic);
  656. err = fnic_set_intr_mode(fnic);
  657. if (err) {
  658. shost_printk(KERN_ERR, fnic->lport->host,
  659. "Failed to set intr mode, "
  660. "aborting.\n");
  661. goto err_out_dev_close;
  662. }
  663. err = fnic_alloc_vnic_resources(fnic);
  664. if (err) {
  665. shost_printk(KERN_ERR, fnic->lport->host,
  666. "Failed to alloc vNIC resources, "
  667. "aborting.\n");
  668. goto err_out_clear_intr;
  669. }
  670. fnic_scsi_drv_init(fnic);
  671. for (hwq = 0; hwq < fnic->wq_copy_count; hwq++) {
  672. fnic->sw_copy_wq[hwq].ioreq_table_size = fnic->fnic_max_tag_id;
  673. fnic->sw_copy_wq[hwq].io_req_table =
  674. kzalloc((fnic->sw_copy_wq[hwq].ioreq_table_size + 1) *
  675. sizeof(struct fnic_io_req *), GFP_KERNEL);
  676. }
  677. shost_printk(KERN_INFO, fnic->lport->host, "fnic copy wqs: %d, Q0 ioreq table size: %d\n",
  678. fnic->wq_copy_count, fnic->sw_copy_wq[0].ioreq_table_size);
  679. /* initialize all fnic locks */
  680. spin_lock_init(&fnic->fnic_lock);
  681. for (i = 0; i < FNIC_WQ_MAX; i++)
  682. spin_lock_init(&fnic->wq_lock[i]);
  683. for (i = 0; i < FNIC_WQ_COPY_MAX; i++) {
  684. spin_lock_init(&fnic->wq_copy_lock[i]);
  685. fnic->wq_copy_desc_low[i] = DESC_CLEAN_LOW_WATERMARK;
  686. fnic->fw_ack_recd[i] = 0;
  687. fnic->fw_ack_index[i] = -1;
  688. }
  689. err = -ENOMEM;
  690. fnic->io_req_pool = mempool_create_slab_pool(2, fnic_io_req_cache);
  691. if (!fnic->io_req_pool)
  692. goto err_out_free_resources;
  693. pool = mempool_create_slab_pool(2, fnic_sgl_cache[FNIC_SGL_CACHE_DFLT]);
  694. if (!pool)
  695. goto err_out_free_ioreq_pool;
  696. fnic->io_sgl_pool[FNIC_SGL_CACHE_DFLT] = pool;
  697. pool = mempool_create_slab_pool(2, fnic_sgl_cache[FNIC_SGL_CACHE_MAX]);
  698. if (!pool)
  699. goto err_out_free_dflt_pool;
  700. fnic->io_sgl_pool[FNIC_SGL_CACHE_MAX] = pool;
  701. /* setup vlan config, hw inserts vlan header */
  702. fnic->vlan_hw_insert = 1;
  703. fnic->vlan_id = 0;
  704. /* Initialize the FIP fcoe_ctrl struct */
  705. fnic->ctlr.send = fnic_eth_send;
  706. fnic->ctlr.update_mac = fnic_update_mac;
  707. fnic->ctlr.get_src_addr = fnic_get_mac;
  708. if (fnic->config.flags & VFCF_FIP_CAPABLE) {
  709. shost_printk(KERN_INFO, fnic->lport->host,
  710. "firmware supports FIP\n");
  711. /* enable directed and multicast */
  712. vnic_dev_packet_filter(fnic->vdev, 1, 1, 0, 0, 0);
  713. vnic_dev_add_addr(fnic->vdev, FIP_ALL_ENODE_MACS);
  714. vnic_dev_add_addr(fnic->vdev, fnic->ctlr.ctl_src_addr);
  715. fnic->set_vlan = fnic_set_vlan;
  716. fcoe_ctlr_init(&fnic->ctlr, FIP_MODE_AUTO);
  717. timer_setup(&fnic->fip_timer, fnic_fip_notify_timer, 0);
  718. spin_lock_init(&fnic->vlans_lock);
  719. INIT_WORK(&fnic->fip_frame_work, fnic_handle_fip_frame);
  720. INIT_WORK(&fnic->event_work, fnic_handle_event);
  721. skb_queue_head_init(&fnic->fip_frame_queue);
  722. INIT_LIST_HEAD(&fnic->evlist);
  723. INIT_LIST_HEAD(&fnic->vlans);
  724. } else {
  725. shost_printk(KERN_INFO, fnic->lport->host,
  726. "firmware uses non-FIP mode\n");
  727. fcoe_ctlr_init(&fnic->ctlr, FIP_MODE_NON_FIP);
  728. fnic->ctlr.state = FIP_ST_NON_FIP;
  729. }
  730. fnic->state = FNIC_IN_FC_MODE;
  731. atomic_set(&fnic->in_flight, 0);
  732. fnic->state_flags = FNIC_FLAGS_NONE;
  733. /* Enable hardware stripping of vlan header on ingress */
  734. fnic_set_nic_config(fnic, 0, 0, 0, 0, 0, 0, 1);
  735. /* Setup notification buffer area */
  736. err = fnic_notify_set(fnic);
  737. if (err) {
  738. shost_printk(KERN_ERR, fnic->lport->host,
  739. "Failed to alloc notify buffer, aborting.\n");
  740. goto err_out_free_max_pool;
  741. }
  742. /* Setup notify timer when using MSI interrupts */
  743. if (vnic_dev_get_intr_mode(fnic->vdev) == VNIC_DEV_INTR_MODE_MSI)
  744. timer_setup(&fnic->notify_timer, fnic_notify_timer, 0);
  745. /* allocate RQ buffers and post them to RQ*/
  746. for (i = 0; i < fnic->rq_count; i++) {
  747. err = vnic_rq_fill(&fnic->rq[i], fnic_alloc_rq_frame);
  748. if (err) {
  749. shost_printk(KERN_ERR, fnic->lport->host,
  750. "fnic_alloc_rq_frame can't alloc "
  751. "frame\n");
  752. goto err_out_rq_buf;
  753. }
  754. }
  755. /* Enable all queues */
  756. for (i = 0; i < fnic->raw_wq_count; i++)
  757. vnic_wq_enable(&fnic->wq[i]);
  758. for (i = 0; i < fnic->rq_count; i++) {
  759. if (!ioread32(&fnic->rq[i].ctrl->enable))
  760. vnic_rq_enable(&fnic->rq[i]);
  761. }
  762. for (i = 0; i < fnic->wq_copy_count; i++)
  763. vnic_wq_copy_enable(&fnic->hw_copy_wq[i]);
  764. err = fnic_request_intr(fnic);
  765. if (err) {
  766. shost_printk(KERN_ERR, fnic->lport->host,
  767. "Unable to request irq.\n");
  768. goto err_out_request_intr;
  769. }
  770. /*
  771. * Initialization done with PCI system, hardware, firmware.
  772. * Add host to SCSI
  773. */
  774. err = scsi_add_host(lp->host, &pdev->dev);
  775. if (err) {
  776. shost_printk(KERN_ERR, fnic->lport->host,
  777. "fnic: scsi_add_host failed...exiting\n");
  778. goto err_out_scsi_add_host;
  779. }
  780. /* Start local port initiatialization */
  781. lp->link_up = 0;
  782. lp->max_retry_count = fnic->config.flogi_retries;
  783. lp->max_rport_retry_count = fnic->config.plogi_retries;
  784. lp->service_params = (FCP_SPPF_INIT_FCN | FCP_SPPF_RD_XRDY_DIS |
  785. FCP_SPPF_CONF_COMPL);
  786. if (fnic->config.flags & VFCF_FCP_SEQ_LVL_ERR)
  787. lp->service_params |= FCP_SPPF_RETRY;
  788. lp->boot_time = jiffies;
  789. lp->e_d_tov = fnic->config.ed_tov;
  790. lp->r_a_tov = fnic->config.ra_tov;
  791. lp->link_supported_speeds = FC_PORTSPEED_10GBIT;
  792. fc_set_wwnn(lp, fnic->config.node_wwn);
  793. fc_set_wwpn(lp, fnic->config.port_wwn);
  794. fcoe_libfc_config(lp, &fnic->ctlr, &fnic_transport_template, 0);
  795. if (!fc_exch_mgr_alloc(lp, FC_CLASS_3, FCPIO_HOST_EXCH_RANGE_START,
  796. FCPIO_HOST_EXCH_RANGE_END, NULL)) {
  797. err = -ENOMEM;
  798. goto err_out_fc_exch_mgr_alloc;
  799. }
  800. fc_lport_init_stats(lp);
  801. fnic->stats_reset_time = jiffies;
  802. fc_lport_config(lp);
  803. if (fc_set_mfs(lp, fnic->config.maxdatafieldsize +
  804. sizeof(struct fc_frame_header))) {
  805. err = -EINVAL;
  806. goto err_out_free_exch_mgr;
  807. }
  808. fc_host_maxframe_size(lp->host) = lp->mfs;
  809. fc_host_dev_loss_tmo(lp->host) = fnic->config.port_down_timeout / 1000;
  810. sprintf(fc_host_symbolic_name(lp->host),
  811. DRV_NAME " v" DRV_VERSION " over %s", fnic->name);
  812. spin_lock_irqsave(&fnic_list_lock, flags);
  813. list_add_tail(&fnic->list, &fnic_list);
  814. spin_unlock_irqrestore(&fnic_list_lock, flags);
  815. INIT_WORK(&fnic->link_work, fnic_handle_link);
  816. INIT_WORK(&fnic->frame_work, fnic_handle_frame);
  817. INIT_WORK(&fnic->flush_work, fnic_flush_tx);
  818. skb_queue_head_init(&fnic->frame_queue);
  819. skb_queue_head_init(&fnic->tx_queue);
  820. fc_fabric_login(lp);
  821. vnic_dev_enable(fnic->vdev);
  822. for (i = 0; i < fnic->intr_count; i++)
  823. vnic_intr_unmask(&fnic->intr[i]);
  824. fnic_notify_timer_start(fnic);
  825. return 0;
  826. err_out_free_exch_mgr:
  827. fc_exch_mgr_free(lp);
  828. err_out_fc_exch_mgr_alloc:
  829. fc_remove_host(lp->host);
  830. scsi_remove_host(lp->host);
  831. err_out_scsi_add_host:
  832. fnic_free_intr(fnic);
  833. err_out_request_intr:
  834. for (i = 0; i < fnic->rq_count; i++)
  835. vnic_rq_clean(&fnic->rq[i], fnic_free_rq_buf);
  836. err_out_rq_buf:
  837. vnic_dev_notify_unset(fnic->vdev);
  838. err_out_free_max_pool:
  839. mempool_destroy(fnic->io_sgl_pool[FNIC_SGL_CACHE_MAX]);
  840. err_out_free_dflt_pool:
  841. mempool_destroy(fnic->io_sgl_pool[FNIC_SGL_CACHE_DFLT]);
  842. err_out_free_ioreq_pool:
  843. mempool_destroy(fnic->io_req_pool);
  844. err_out_free_resources:
  845. for (hwq = 0; hwq < fnic->wq_copy_count; hwq++)
  846. kfree(fnic->sw_copy_wq[hwq].io_req_table);
  847. fnic_free_vnic_resources(fnic);
  848. err_out_clear_intr:
  849. fnic_clear_intr_mode(fnic);
  850. err_out_dev_close:
  851. vnic_dev_close(fnic->vdev);
  852. err_out_dev_cmd_deinit:
  853. err_out_vnic_unregister:
  854. vnic_dev_unregister(fnic->vdev);
  855. err_out_iounmap:
  856. fnic_iounmap(fnic);
  857. err_out_release_regions:
  858. pci_release_regions(pdev);
  859. err_out_disable_device:
  860. pci_disable_device(pdev);
  861. err_out_free_hba:
  862. fnic_stats_debugfs_remove(fnic);
  863. ida_free(&fnic_ida, fnic->fnic_num);
  864. err_out_ida_alloc:
  865. scsi_host_put(lp->host);
  866. err_out:
  867. return err;
  868. }
  869. static void fnic_remove(struct pci_dev *pdev)
  870. {
  871. struct fnic *fnic = pci_get_drvdata(pdev);
  872. struct fc_lport *lp = fnic->lport;
  873. unsigned long flags;
  874. int hwq;
  875. /*
  876. * Mark state so that the workqueue thread stops forwarding
  877. * received frames and link events to the local port. ISR and
  878. * other threads that can queue work items will also stop
  879. * creating work items on the fnic workqueue
  880. */
  881. spin_lock_irqsave(&fnic->fnic_lock, flags);
  882. fnic->stop_rx_link_events = 1;
  883. spin_unlock_irqrestore(&fnic->fnic_lock, flags);
  884. if (vnic_dev_get_intr_mode(fnic->vdev) == VNIC_DEV_INTR_MODE_MSI)
  885. del_timer_sync(&fnic->notify_timer);
  886. /*
  887. * Flush the fnic event queue. After this call, there should
  888. * be no event queued for this fnic device in the workqueue
  889. */
  890. flush_workqueue(fnic_event_queue);
  891. skb_queue_purge(&fnic->frame_queue);
  892. skb_queue_purge(&fnic->tx_queue);
  893. if (fnic->config.flags & VFCF_FIP_CAPABLE) {
  894. del_timer_sync(&fnic->fip_timer);
  895. skb_queue_purge(&fnic->fip_frame_queue);
  896. fnic_fcoe_reset_vlans(fnic);
  897. fnic_fcoe_evlist_free(fnic);
  898. }
  899. /*
  900. * Log off the fabric. This stops all remote ports, dns port,
  901. * logs off the fabric. This flushes all rport, disc, lport work
  902. * before returning
  903. */
  904. fc_fabric_logoff(fnic->lport);
  905. spin_lock_irqsave(&fnic->fnic_lock, flags);
  906. fnic->in_remove = 1;
  907. spin_unlock_irqrestore(&fnic->fnic_lock, flags);
  908. fcoe_ctlr_destroy(&fnic->ctlr);
  909. fc_lport_destroy(lp);
  910. fnic_stats_debugfs_remove(fnic);
  911. /*
  912. * This stops the fnic device, masks all interrupts. Completed
  913. * CQ entries are drained. Posted WQ/RQ/Copy-WQ entries are
  914. * cleaned up
  915. */
  916. fnic_cleanup(fnic);
  917. BUG_ON(!skb_queue_empty(&fnic->frame_queue));
  918. BUG_ON(!skb_queue_empty(&fnic->tx_queue));
  919. spin_lock_irqsave(&fnic_list_lock, flags);
  920. list_del(&fnic->list);
  921. spin_unlock_irqrestore(&fnic_list_lock, flags);
  922. fc_remove_host(fnic->lport->host);
  923. scsi_remove_host(fnic->lport->host);
  924. for (hwq = 0; hwq < fnic->wq_copy_count; hwq++)
  925. kfree(fnic->sw_copy_wq[hwq].io_req_table);
  926. fc_exch_mgr_free(fnic->lport);
  927. vnic_dev_notify_unset(fnic->vdev);
  928. fnic_free_intr(fnic);
  929. fnic_free_vnic_resources(fnic);
  930. fnic_clear_intr_mode(fnic);
  931. vnic_dev_close(fnic->vdev);
  932. vnic_dev_unregister(fnic->vdev);
  933. fnic_iounmap(fnic);
  934. pci_release_regions(pdev);
  935. pci_disable_device(pdev);
  936. ida_free(&fnic_ida, fnic->fnic_num);
  937. scsi_host_put(lp->host);
  938. }
  939. static struct pci_driver fnic_driver = {
  940. .name = DRV_NAME,
  941. .id_table = fnic_id_table,
  942. .probe = fnic_probe,
  943. .remove = fnic_remove,
  944. };
  945. static int __init fnic_init_module(void)
  946. {
  947. size_t len;
  948. int err = 0;
  949. printk(KERN_INFO PFX "%s, ver %s\n", DRV_DESCRIPTION, DRV_VERSION);
  950. /* Create debugfs entries for fnic */
  951. err = fnic_debugfs_init();
  952. if (err < 0) {
  953. printk(KERN_ERR PFX "Failed to create fnic directory "
  954. "for tracing and stats logging\n");
  955. fnic_debugfs_terminate();
  956. }
  957. /* Allocate memory for trace buffer */
  958. err = fnic_trace_buf_init();
  959. if (err < 0) {
  960. printk(KERN_ERR PFX
  961. "Trace buffer initialization Failed. "
  962. "Fnic Tracing utility is disabled\n");
  963. fnic_trace_free();
  964. }
  965. /* Allocate memory for fc trace buffer */
  966. err = fnic_fc_trace_init();
  967. if (err < 0) {
  968. printk(KERN_ERR PFX "FC trace buffer initialization Failed "
  969. "FC frame tracing utility is disabled\n");
  970. fnic_fc_trace_free();
  971. }
  972. /* Create a cache for allocation of default size sgls */
  973. len = sizeof(struct fnic_dflt_sgl_list);
  974. fnic_sgl_cache[FNIC_SGL_CACHE_DFLT] = kmem_cache_create
  975. ("fnic_sgl_dflt", len + FNIC_SG_DESC_ALIGN, FNIC_SG_DESC_ALIGN,
  976. SLAB_HWCACHE_ALIGN,
  977. NULL);
  978. if (!fnic_sgl_cache[FNIC_SGL_CACHE_DFLT]) {
  979. printk(KERN_ERR PFX "failed to create fnic dflt sgl slab\n");
  980. err = -ENOMEM;
  981. goto err_create_fnic_sgl_slab_dflt;
  982. }
  983. /* Create a cache for allocation of max size sgls*/
  984. len = sizeof(struct fnic_sgl_list);
  985. fnic_sgl_cache[FNIC_SGL_CACHE_MAX] = kmem_cache_create
  986. ("fnic_sgl_max", len + FNIC_SG_DESC_ALIGN, FNIC_SG_DESC_ALIGN,
  987. SLAB_HWCACHE_ALIGN,
  988. NULL);
  989. if (!fnic_sgl_cache[FNIC_SGL_CACHE_MAX]) {
  990. printk(KERN_ERR PFX "failed to create fnic max sgl slab\n");
  991. err = -ENOMEM;
  992. goto err_create_fnic_sgl_slab_max;
  993. }
  994. /* Create a cache of io_req structs for use via mempool */
  995. fnic_io_req_cache = kmem_cache_create("fnic_io_req",
  996. sizeof(struct fnic_io_req),
  997. 0, SLAB_HWCACHE_ALIGN, NULL);
  998. if (!fnic_io_req_cache) {
  999. printk(KERN_ERR PFX "failed to create fnic io_req slab\n");
  1000. err = -ENOMEM;
  1001. goto err_create_fnic_ioreq_slab;
  1002. }
  1003. fnic_event_queue =
  1004. alloc_ordered_workqueue("%s", WQ_MEM_RECLAIM, "fnic_event_wq");
  1005. if (!fnic_event_queue) {
  1006. printk(KERN_ERR PFX "fnic work queue create failed\n");
  1007. err = -ENOMEM;
  1008. goto err_create_fnic_workq;
  1009. }
  1010. fnic_fip_queue =
  1011. alloc_ordered_workqueue("%s", WQ_MEM_RECLAIM, "fnic_fip_q");
  1012. if (!fnic_fip_queue) {
  1013. printk(KERN_ERR PFX "fnic FIP work queue create failed\n");
  1014. err = -ENOMEM;
  1015. goto err_create_fip_workq;
  1016. }
  1017. fnic_fc_transport = fc_attach_transport(&fnic_fc_functions);
  1018. if (!fnic_fc_transport) {
  1019. printk(KERN_ERR PFX "fc_attach_transport error\n");
  1020. err = -ENOMEM;
  1021. goto err_fc_transport;
  1022. }
  1023. /* register the driver with PCI system */
  1024. err = pci_register_driver(&fnic_driver);
  1025. if (err < 0) {
  1026. printk(KERN_ERR PFX "pci register error\n");
  1027. goto err_pci_register;
  1028. }
  1029. return err;
  1030. err_pci_register:
  1031. fc_release_transport(fnic_fc_transport);
  1032. err_fc_transport:
  1033. destroy_workqueue(fnic_fip_queue);
  1034. err_create_fip_workq:
  1035. destroy_workqueue(fnic_event_queue);
  1036. err_create_fnic_workq:
  1037. kmem_cache_destroy(fnic_io_req_cache);
  1038. err_create_fnic_ioreq_slab:
  1039. kmem_cache_destroy(fnic_sgl_cache[FNIC_SGL_CACHE_MAX]);
  1040. err_create_fnic_sgl_slab_max:
  1041. kmem_cache_destroy(fnic_sgl_cache[FNIC_SGL_CACHE_DFLT]);
  1042. err_create_fnic_sgl_slab_dflt:
  1043. fnic_trace_free();
  1044. fnic_fc_trace_free();
  1045. fnic_debugfs_terminate();
  1046. return err;
  1047. }
  1048. static void __exit fnic_cleanup_module(void)
  1049. {
  1050. pci_unregister_driver(&fnic_driver);
  1051. destroy_workqueue(fnic_event_queue);
  1052. if (fnic_fip_queue)
  1053. destroy_workqueue(fnic_fip_queue);
  1054. kmem_cache_destroy(fnic_sgl_cache[FNIC_SGL_CACHE_MAX]);
  1055. kmem_cache_destroy(fnic_sgl_cache[FNIC_SGL_CACHE_DFLT]);
  1056. kmem_cache_destroy(fnic_io_req_cache);
  1057. fc_release_transport(fnic_fc_transport);
  1058. fnic_trace_free();
  1059. fnic_fc_trace_free();
  1060. fnic_debugfs_terminate();
  1061. ida_destroy(&fnic_ida);
  1062. }
  1063. module_init(fnic_init_module);
  1064. module_exit(fnic_cleanup_module);