fnic_main.c 32 KB

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