fnic_main.c 27 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006
  1. /*
  2. * Copyright 2008 Cisco Systems, Inc. All rights reserved.
  3. * Copyright 2007 Nuova Systems, Inc. All rights reserved.
  4. *
  5. * This program is free software; you may redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; version 2 of the License.
  8. *
  9. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  10. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  11. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  12. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
  13. * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  14. * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  15. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  16. * SOFTWARE.
  17. */
  18. #include <linux/module.h>
  19. #include <linux/mempool.h>
  20. #include <linux/string.h>
  21. #include <linux/slab.h>
  22. #include <linux/errno.h>
  23. #include <linux/init.h>
  24. #include <linux/pci.h>
  25. #include <linux/skbuff.h>
  26. #include <linux/interrupt.h>
  27. #include <linux/spinlock.h>
  28. #include <linux/workqueue.h>
  29. #include <linux/if_ether.h>
  30. #include <scsi/fc/fc_fip.h>
  31. #include <scsi/scsi_host.h>
  32. #include <scsi/scsi_transport.h>
  33. #include <scsi/scsi_transport_fc.h>
  34. #include <scsi/scsi_tcq.h>
  35. #include <scsi/libfc.h>
  36. #include <scsi/fc_frame.h>
  37. #include "vnic_dev.h"
  38. #include "vnic_intr.h"
  39. #include "vnic_stats.h"
  40. #include "fnic_io.h"
  41. #include "fnic_fip.h"
  42. #include "fnic.h"
  43. #define PCI_DEVICE_ID_CISCO_FNIC 0x0045
  44. /* Timer to poll notification area for events. Used for MSI interrupts */
  45. #define FNIC_NOTIFY_TIMER_PERIOD (2 * HZ)
  46. static struct kmem_cache *fnic_sgl_cache[FNIC_SGL_NUM_CACHES];
  47. static struct kmem_cache *fnic_io_req_cache;
  48. LIST_HEAD(fnic_list);
  49. DEFINE_SPINLOCK(fnic_list_lock);
  50. /* Supported devices by fnic module */
  51. static struct pci_device_id fnic_id_table[] = {
  52. { PCI_DEVICE(PCI_VENDOR_ID_CISCO, PCI_DEVICE_ID_CISCO_FNIC) },
  53. { 0, }
  54. };
  55. MODULE_DESCRIPTION(DRV_DESCRIPTION);
  56. MODULE_AUTHOR("Abhijeet Joglekar <abjoglek@cisco.com>, "
  57. "Joseph R. Eykholt <jeykholt@cisco.com>");
  58. MODULE_LICENSE("GPL v2");
  59. MODULE_VERSION(DRV_VERSION);
  60. MODULE_DEVICE_TABLE(pci, fnic_id_table);
  61. unsigned int fnic_log_level;
  62. module_param(fnic_log_level, int, S_IRUGO|S_IWUSR);
  63. MODULE_PARM_DESC(fnic_log_level, "bit mask of fnic logging levels");
  64. unsigned int fnic_trace_max_pages = 16;
  65. module_param(fnic_trace_max_pages, uint, S_IRUGO|S_IWUSR);
  66. MODULE_PARM_DESC(fnic_trace_max_pages, "Total allocated memory pages "
  67. "for fnic trace buffer");
  68. static struct libfc_function_template fnic_transport_template = {
  69. .frame_send = fnic_send,
  70. .lport_set_port_id = fnic_set_port_id,
  71. .fcp_abort_io = fnic_empty_scsi_cleanup,
  72. .fcp_cleanup = fnic_empty_scsi_cleanup,
  73. .exch_mgr_reset = fnic_exch_mgr_reset
  74. };
  75. static int fnic_slave_alloc(struct scsi_device *sdev)
  76. {
  77. struct fc_rport *rport = starget_to_rport(scsi_target(sdev));
  78. sdev->tagged_supported = 1;
  79. if (!rport || fc_remote_port_chkready(rport))
  80. return -ENXIO;
  81. scsi_activate_tcq(sdev, FNIC_DFLT_QUEUE_DEPTH);
  82. return 0;
  83. }
  84. static struct scsi_host_template fnic_host_template = {
  85. .module = THIS_MODULE,
  86. .name = DRV_NAME,
  87. .queuecommand = fnic_queuecommand,
  88. .eh_abort_handler = fnic_abort_cmd,
  89. .eh_device_reset_handler = fnic_device_reset,
  90. .eh_host_reset_handler = fnic_host_reset,
  91. .slave_alloc = fnic_slave_alloc,
  92. .change_queue_depth = fc_change_queue_depth,
  93. .change_queue_type = fc_change_queue_type,
  94. .this_id = -1,
  95. .cmd_per_lun = 3,
  96. .can_queue = FNIC_MAX_IO_REQ,
  97. .use_clustering = ENABLE_CLUSTERING,
  98. .sg_tablesize = FNIC_MAX_SG_DESC_CNT,
  99. .max_sectors = 0xffff,
  100. .shost_attrs = fnic_attrs,
  101. };
  102. static void
  103. fnic_set_rport_dev_loss_tmo(struct fc_rport *rport, u32 timeout)
  104. {
  105. if (timeout)
  106. rport->dev_loss_tmo = timeout;
  107. else
  108. rport->dev_loss_tmo = 1;
  109. }
  110. static void fnic_get_host_speed(struct Scsi_Host *shost);
  111. static struct scsi_transport_template *fnic_fc_transport;
  112. static struct fc_host_statistics *fnic_get_stats(struct Scsi_Host *);
  113. static struct fc_function_template fnic_fc_functions = {
  114. .show_host_node_name = 1,
  115. .show_host_port_name = 1,
  116. .show_host_supported_classes = 1,
  117. .show_host_supported_fc4s = 1,
  118. .show_host_active_fc4s = 1,
  119. .show_host_maxframe_size = 1,
  120. .show_host_port_id = 1,
  121. .show_host_supported_speeds = 1,
  122. .get_host_speed = fnic_get_host_speed,
  123. .show_host_speed = 1,
  124. .show_host_port_type = 1,
  125. .get_host_port_state = fc_get_host_port_state,
  126. .show_host_port_state = 1,
  127. .show_host_symbolic_name = 1,
  128. .show_rport_maxframe_size = 1,
  129. .show_rport_supported_classes = 1,
  130. .show_host_fabric_name = 1,
  131. .show_starget_node_name = 1,
  132. .show_starget_port_name = 1,
  133. .show_starget_port_id = 1,
  134. .show_rport_dev_loss_tmo = 1,
  135. .set_rport_dev_loss_tmo = fnic_set_rport_dev_loss_tmo,
  136. .issue_fc_host_lip = fnic_reset,
  137. .get_fc_host_stats = fnic_get_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 10000:
  150. fc_host_speed(shost) = FC_PORTSPEED_10GBIT;
  151. break;
  152. default:
  153. fc_host_speed(shost) = FC_PORTSPEED_10GBIT;
  154. break;
  155. }
  156. }
  157. static struct fc_host_statistics *fnic_get_stats(struct Scsi_Host *host)
  158. {
  159. int ret;
  160. struct fc_lport *lp = shost_priv(host);
  161. struct fnic *fnic = lport_priv(lp);
  162. struct fc_host_statistics *stats = &lp->host_stats;
  163. struct vnic_stats *vs;
  164. unsigned long flags;
  165. if (time_before(jiffies, fnic->stats_time + HZ / FNIC_STATS_RATE_LIMIT))
  166. return stats;
  167. fnic->stats_time = jiffies;
  168. spin_lock_irqsave(&fnic->fnic_lock, flags);
  169. ret = vnic_dev_stats_dump(fnic->vdev, &fnic->stats);
  170. spin_unlock_irqrestore(&fnic->fnic_lock, flags);
  171. if (ret) {
  172. FNIC_MAIN_DBG(KERN_DEBUG, fnic->lport->host,
  173. "fnic: Get vnic stats failed"
  174. " 0x%x", ret);
  175. return stats;
  176. }
  177. vs = fnic->stats;
  178. stats->tx_frames = vs->tx.tx_unicast_frames_ok;
  179. stats->tx_words = vs->tx.tx_unicast_bytes_ok / 4;
  180. stats->rx_frames = vs->rx.rx_unicast_frames_ok;
  181. stats->rx_words = vs->rx.rx_unicast_bytes_ok / 4;
  182. stats->error_frames = vs->tx.tx_errors + vs->rx.rx_errors;
  183. stats->dumped_frames = vs->tx.tx_drops + vs->rx.rx_drop;
  184. stats->invalid_crc_count = vs->rx.rx_crc_errors;
  185. stats->seconds_since_last_reset = (jiffies - lp->boot_time) / HZ;
  186. stats->fcp_input_megabytes = div_u64(fnic->fcp_input_bytes, 1000000);
  187. stats->fcp_output_megabytes = div_u64(fnic->fcp_output_bytes, 1000000);
  188. return stats;
  189. }
  190. void fnic_log_q_error(struct fnic *fnic)
  191. {
  192. unsigned int i;
  193. u32 error_status;
  194. for (i = 0; i < fnic->raw_wq_count; i++) {
  195. error_status = ioread32(&fnic->wq[i].ctrl->error_status);
  196. if (error_status)
  197. shost_printk(KERN_ERR, fnic->lport->host,
  198. "WQ[%d] error_status"
  199. " %d\n", i, error_status);
  200. }
  201. for (i = 0; i < fnic->rq_count; i++) {
  202. error_status = ioread32(&fnic->rq[i].ctrl->error_status);
  203. if (error_status)
  204. shost_printk(KERN_ERR, fnic->lport->host,
  205. "RQ[%d] error_status"
  206. " %d\n", i, error_status);
  207. }
  208. for (i = 0; i < fnic->wq_copy_count; i++) {
  209. error_status = ioread32(&fnic->wq_copy[i].ctrl->error_status);
  210. if (error_status)
  211. shost_printk(KERN_ERR, fnic->lport->host,
  212. "CWQ[%d] error_status"
  213. " %d\n", i, error_status);
  214. }
  215. }
  216. void fnic_handle_link_event(struct fnic *fnic)
  217. {
  218. unsigned long flags;
  219. spin_lock_irqsave(&fnic->fnic_lock, flags);
  220. if (fnic->stop_rx_link_events) {
  221. spin_unlock_irqrestore(&fnic->fnic_lock, flags);
  222. return;
  223. }
  224. spin_unlock_irqrestore(&fnic->fnic_lock, flags);
  225. queue_work(fnic_event_queue, &fnic->link_work);
  226. }
  227. static int fnic_notify_set(struct fnic *fnic)
  228. {
  229. int err;
  230. switch (vnic_dev_get_intr_mode(fnic->vdev)) {
  231. case VNIC_DEV_INTR_MODE_INTX:
  232. err = vnic_dev_notify_set(fnic->vdev, FNIC_INTX_NOTIFY);
  233. break;
  234. case VNIC_DEV_INTR_MODE_MSI:
  235. err = vnic_dev_notify_set(fnic->vdev, -1);
  236. break;
  237. case VNIC_DEV_INTR_MODE_MSIX:
  238. err = vnic_dev_notify_set(fnic->vdev, FNIC_MSIX_ERR_NOTIFY);
  239. break;
  240. default:
  241. shost_printk(KERN_ERR, fnic->lport->host,
  242. "Interrupt mode should be set up"
  243. " before devcmd notify set %d\n",
  244. vnic_dev_get_intr_mode(fnic->vdev));
  245. err = -1;
  246. break;
  247. }
  248. return err;
  249. }
  250. static void fnic_notify_timer(unsigned long data)
  251. {
  252. struct fnic *fnic = (struct fnic *)data;
  253. fnic_handle_link_event(fnic);
  254. mod_timer(&fnic->notify_timer,
  255. round_jiffies(jiffies + FNIC_NOTIFY_TIMER_PERIOD));
  256. }
  257. static void fnic_fip_notify_timer(unsigned long data)
  258. {
  259. struct fnic *fnic = (struct fnic *)data;
  260. fnic_handle_fip_timer(fnic);
  261. }
  262. static void fnic_notify_timer_start(struct fnic *fnic)
  263. {
  264. switch (vnic_dev_get_intr_mode(fnic->vdev)) {
  265. case VNIC_DEV_INTR_MODE_MSI:
  266. /*
  267. * Schedule first timeout immediately. The driver is
  268. * initiatialized and ready to look for link up notification
  269. */
  270. mod_timer(&fnic->notify_timer, jiffies);
  271. break;
  272. default:
  273. /* Using intr for notification for INTx/MSI-X */
  274. break;
  275. };
  276. }
  277. static int fnic_dev_wait(struct vnic_dev *vdev,
  278. int (*start)(struct vnic_dev *, int),
  279. int (*finished)(struct vnic_dev *, int *),
  280. int arg)
  281. {
  282. unsigned long time;
  283. int done;
  284. int err;
  285. err = start(vdev, arg);
  286. if (err)
  287. return err;
  288. /* Wait for func to complete...2 seconds max */
  289. time = jiffies + (HZ * 2);
  290. do {
  291. err = finished(vdev, &done);
  292. if (err)
  293. return err;
  294. if (done)
  295. return 0;
  296. schedule_timeout_uninterruptible(HZ / 10);
  297. } while (time_after(time, jiffies));
  298. return -ETIMEDOUT;
  299. }
  300. static int fnic_cleanup(struct fnic *fnic)
  301. {
  302. unsigned int i;
  303. int err;
  304. vnic_dev_disable(fnic->vdev);
  305. for (i = 0; i < fnic->intr_count; i++)
  306. vnic_intr_mask(&fnic->intr[i]);
  307. for (i = 0; i < fnic->rq_count; i++) {
  308. err = vnic_rq_disable(&fnic->rq[i]);
  309. if (err)
  310. return err;
  311. }
  312. for (i = 0; i < fnic->raw_wq_count; i++) {
  313. err = vnic_wq_disable(&fnic->wq[i]);
  314. if (err)
  315. return err;
  316. }
  317. for (i = 0; i < fnic->wq_copy_count; i++) {
  318. err = vnic_wq_copy_disable(&fnic->wq_copy[i]);
  319. if (err)
  320. return err;
  321. }
  322. /* Clean up completed IOs and FCS frames */
  323. fnic_wq_copy_cmpl_handler(fnic, -1);
  324. fnic_wq_cmpl_handler(fnic, -1);
  325. fnic_rq_cmpl_handler(fnic, -1);
  326. /* Clean up the IOs and FCS frames that have not completed */
  327. for (i = 0; i < fnic->raw_wq_count; i++)
  328. vnic_wq_clean(&fnic->wq[i], fnic_free_wq_buf);
  329. for (i = 0; i < fnic->rq_count; i++)
  330. vnic_rq_clean(&fnic->rq[i], fnic_free_rq_buf);
  331. for (i = 0; i < fnic->wq_copy_count; i++)
  332. vnic_wq_copy_clean(&fnic->wq_copy[i],
  333. fnic_wq_copy_cleanup_handler);
  334. for (i = 0; i < fnic->cq_count; i++)
  335. vnic_cq_clean(&fnic->cq[i]);
  336. for (i = 0; i < fnic->intr_count; i++)
  337. vnic_intr_clean(&fnic->intr[i]);
  338. mempool_destroy(fnic->io_req_pool);
  339. for (i = 0; i < FNIC_SGL_NUM_CACHES; i++)
  340. mempool_destroy(fnic->io_sgl_pool[i]);
  341. return 0;
  342. }
  343. static void fnic_iounmap(struct fnic *fnic)
  344. {
  345. if (fnic->bar0.vaddr)
  346. iounmap(fnic->bar0.vaddr);
  347. }
  348. /**
  349. * fnic_get_mac() - get assigned data MAC address for FIP code.
  350. * @lport: local port.
  351. */
  352. static u8 *fnic_get_mac(struct fc_lport *lport)
  353. {
  354. struct fnic *fnic = lport_priv(lport);
  355. return fnic->data_src_addr;
  356. }
  357. static void fnic_set_vlan(struct fnic *fnic, u16 vlan_id)
  358. {
  359. u16 old_vlan;
  360. old_vlan = vnic_dev_set_default_vlan(fnic->vdev, vlan_id);
  361. }
  362. static int fnic_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
  363. {
  364. struct Scsi_Host *host;
  365. struct fc_lport *lp;
  366. struct fnic *fnic;
  367. mempool_t *pool;
  368. int err;
  369. int i;
  370. unsigned long flags;
  371. /*
  372. * Allocate SCSI Host and set up association between host,
  373. * local port, and fnic
  374. */
  375. lp = libfc_host_alloc(&fnic_host_template, sizeof(struct fnic));
  376. if (!lp) {
  377. printk(KERN_ERR PFX "Unable to alloc libfc local port\n");
  378. err = -ENOMEM;
  379. goto err_out;
  380. }
  381. host = lp->host;
  382. fnic = lport_priv(lp);
  383. fnic->lport = lp;
  384. fnic->ctlr.lp = lp;
  385. snprintf(fnic->name, sizeof(fnic->name) - 1, "%s%d", DRV_NAME,
  386. host->host_no);
  387. host->transportt = fnic_fc_transport;
  388. err = scsi_init_shared_tag_map(host, FNIC_MAX_IO_REQ);
  389. if (err) {
  390. shost_printk(KERN_ERR, fnic->lport->host,
  391. "Unable to alloc shared tag map\n");
  392. goto err_out_free_hba;
  393. }
  394. /* Setup PCI resources */
  395. pci_set_drvdata(pdev, fnic);
  396. fnic->pdev = pdev;
  397. err = pci_enable_device(pdev);
  398. if (err) {
  399. shost_printk(KERN_ERR, fnic->lport->host,
  400. "Cannot enable PCI device, aborting.\n");
  401. goto err_out_free_hba;
  402. }
  403. err = pci_request_regions(pdev, DRV_NAME);
  404. if (err) {
  405. shost_printk(KERN_ERR, fnic->lport->host,
  406. "Cannot enable PCI resources, aborting\n");
  407. goto err_out_disable_device;
  408. }
  409. pci_set_master(pdev);
  410. /* Query PCI controller on system for DMA addressing
  411. * limitation for the device. Try 40-bit first, and
  412. * fail to 32-bit.
  413. */
  414. err = pci_set_dma_mask(pdev, DMA_BIT_MASK(40));
  415. if (err) {
  416. err = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
  417. if (err) {
  418. shost_printk(KERN_ERR, fnic->lport->host,
  419. "No usable DMA configuration "
  420. "aborting\n");
  421. goto err_out_release_regions;
  422. }
  423. err = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(32));
  424. if (err) {
  425. shost_printk(KERN_ERR, fnic->lport->host,
  426. "Unable to obtain 32-bit DMA "
  427. "for consistent allocations, aborting.\n");
  428. goto err_out_release_regions;
  429. }
  430. } else {
  431. err = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(40));
  432. if (err) {
  433. shost_printk(KERN_ERR, fnic->lport->host,
  434. "Unable to obtain 40-bit DMA "
  435. "for consistent allocations, aborting.\n");
  436. goto err_out_release_regions;
  437. }
  438. }
  439. /* Map vNIC resources from BAR0 */
  440. if (!(pci_resource_flags(pdev, 0) & IORESOURCE_MEM)) {
  441. shost_printk(KERN_ERR, fnic->lport->host,
  442. "BAR0 not memory-map'able, aborting.\n");
  443. err = -ENODEV;
  444. goto err_out_release_regions;
  445. }
  446. fnic->bar0.vaddr = pci_iomap(pdev, 0, 0);
  447. fnic->bar0.bus_addr = pci_resource_start(pdev, 0);
  448. fnic->bar0.len = pci_resource_len(pdev, 0);
  449. if (!fnic->bar0.vaddr) {
  450. shost_printk(KERN_ERR, fnic->lport->host,
  451. "Cannot memory-map BAR0 res hdr, "
  452. "aborting.\n");
  453. err = -ENODEV;
  454. goto err_out_release_regions;
  455. }
  456. fnic->vdev = vnic_dev_register(NULL, fnic, pdev, &fnic->bar0);
  457. if (!fnic->vdev) {
  458. shost_printk(KERN_ERR, fnic->lport->host,
  459. "vNIC registration failed, "
  460. "aborting.\n");
  461. err = -ENODEV;
  462. goto err_out_iounmap;
  463. }
  464. err = fnic_dev_wait(fnic->vdev, vnic_dev_open,
  465. vnic_dev_open_done, 0);
  466. if (err) {
  467. shost_printk(KERN_ERR, fnic->lport->host,
  468. "vNIC dev open failed, aborting.\n");
  469. goto err_out_vnic_unregister;
  470. }
  471. err = vnic_dev_init(fnic->vdev, 0);
  472. if (err) {
  473. shost_printk(KERN_ERR, fnic->lport->host,
  474. "vNIC dev init failed, aborting.\n");
  475. goto err_out_dev_close;
  476. }
  477. err = vnic_dev_mac_addr(fnic->vdev, fnic->ctlr.ctl_src_addr);
  478. if (err) {
  479. shost_printk(KERN_ERR, fnic->lport->host,
  480. "vNIC get MAC addr failed \n");
  481. goto err_out_dev_close;
  482. }
  483. /* set data_src for point-to-point mode and to keep it non-zero */
  484. memcpy(fnic->data_src_addr, fnic->ctlr.ctl_src_addr, ETH_ALEN);
  485. /* Get vNIC configuration */
  486. err = fnic_get_vnic_config(fnic);
  487. if (err) {
  488. shost_printk(KERN_ERR, fnic->lport->host,
  489. "Get vNIC configuration failed, "
  490. "aborting.\n");
  491. goto err_out_dev_close;
  492. }
  493. host->max_lun = fnic->config.luns_per_tgt;
  494. host->max_id = FNIC_MAX_FCP_TARGET;
  495. host->max_cmd_len = FCOE_MAX_CMD_LEN;
  496. fnic_get_res_counts(fnic);
  497. err = fnic_set_intr_mode(fnic);
  498. if (err) {
  499. shost_printk(KERN_ERR, fnic->lport->host,
  500. "Failed to set intr mode, "
  501. "aborting.\n");
  502. goto err_out_dev_close;
  503. }
  504. err = fnic_alloc_vnic_resources(fnic);
  505. if (err) {
  506. shost_printk(KERN_ERR, fnic->lport->host,
  507. "Failed to alloc vNIC resources, "
  508. "aborting.\n");
  509. goto err_out_clear_intr;
  510. }
  511. /* initialize all fnic locks */
  512. spin_lock_init(&fnic->fnic_lock);
  513. for (i = 0; i < FNIC_WQ_MAX; i++)
  514. spin_lock_init(&fnic->wq_lock[i]);
  515. for (i = 0; i < FNIC_WQ_COPY_MAX; i++) {
  516. spin_lock_init(&fnic->wq_copy_lock[i]);
  517. fnic->wq_copy_desc_low[i] = DESC_CLEAN_LOW_WATERMARK;
  518. fnic->fw_ack_recd[i] = 0;
  519. fnic->fw_ack_index[i] = -1;
  520. }
  521. for (i = 0; i < FNIC_IO_LOCKS; i++)
  522. spin_lock_init(&fnic->io_req_lock[i]);
  523. fnic->io_req_pool = mempool_create_slab_pool(2, fnic_io_req_cache);
  524. if (!fnic->io_req_pool)
  525. goto err_out_free_resources;
  526. pool = mempool_create_slab_pool(2, fnic_sgl_cache[FNIC_SGL_CACHE_DFLT]);
  527. if (!pool)
  528. goto err_out_free_ioreq_pool;
  529. fnic->io_sgl_pool[FNIC_SGL_CACHE_DFLT] = pool;
  530. pool = mempool_create_slab_pool(2, fnic_sgl_cache[FNIC_SGL_CACHE_MAX]);
  531. if (!pool)
  532. goto err_out_free_dflt_pool;
  533. fnic->io_sgl_pool[FNIC_SGL_CACHE_MAX] = pool;
  534. /* setup vlan config, hw inserts vlan header */
  535. fnic->vlan_hw_insert = 1;
  536. fnic->vlan_id = 0;
  537. /* Initialize the FIP fcoe_ctrl struct */
  538. fnic->ctlr.send = fnic_eth_send;
  539. fnic->ctlr.update_mac = fnic_update_mac;
  540. fnic->ctlr.get_src_addr = fnic_get_mac;
  541. if (fnic->config.flags & VFCF_FIP_CAPABLE) {
  542. shost_printk(KERN_INFO, fnic->lport->host,
  543. "firmware supports FIP\n");
  544. /* enable directed and multicast */
  545. vnic_dev_packet_filter(fnic->vdev, 1, 1, 0, 0, 0);
  546. vnic_dev_add_addr(fnic->vdev, FIP_ALL_ENODE_MACS);
  547. vnic_dev_add_addr(fnic->vdev, fnic->ctlr.ctl_src_addr);
  548. fnic->set_vlan = fnic_set_vlan;
  549. fcoe_ctlr_init(&fnic->ctlr, FIP_MODE_AUTO);
  550. setup_timer(&fnic->fip_timer, fnic_fip_notify_timer,
  551. (unsigned long)fnic);
  552. spin_lock_init(&fnic->vlans_lock);
  553. INIT_WORK(&fnic->fip_frame_work, fnic_handle_fip_frame);
  554. INIT_WORK(&fnic->event_work, fnic_handle_event);
  555. skb_queue_head_init(&fnic->fip_frame_queue);
  556. INIT_LIST_HEAD(&fnic->evlist);
  557. INIT_LIST_HEAD(&fnic->vlans);
  558. } else {
  559. shost_printk(KERN_INFO, fnic->lport->host,
  560. "firmware uses non-FIP mode\n");
  561. fcoe_ctlr_init(&fnic->ctlr, FIP_MODE_NON_FIP);
  562. }
  563. fnic->state = FNIC_IN_FC_MODE;
  564. atomic_set(&fnic->in_flight, 0);
  565. fnic->state_flags = FNIC_FLAGS_NONE;
  566. /* Enable hardware stripping of vlan header on ingress */
  567. fnic_set_nic_config(fnic, 0, 0, 0, 0, 0, 0, 1);
  568. /* Setup notification buffer area */
  569. err = fnic_notify_set(fnic);
  570. if (err) {
  571. shost_printk(KERN_ERR, fnic->lport->host,
  572. "Failed to alloc notify buffer, aborting.\n");
  573. goto err_out_free_max_pool;
  574. }
  575. /* Setup notify timer when using MSI interrupts */
  576. if (vnic_dev_get_intr_mode(fnic->vdev) == VNIC_DEV_INTR_MODE_MSI)
  577. setup_timer(&fnic->notify_timer,
  578. fnic_notify_timer, (unsigned long)fnic);
  579. /* allocate RQ buffers and post them to RQ*/
  580. for (i = 0; i < fnic->rq_count; i++) {
  581. err = vnic_rq_fill(&fnic->rq[i], fnic_alloc_rq_frame);
  582. if (err) {
  583. shost_printk(KERN_ERR, fnic->lport->host,
  584. "fnic_alloc_rq_frame can't alloc "
  585. "frame\n");
  586. goto err_out_free_rq_buf;
  587. }
  588. }
  589. /*
  590. * Initialization done with PCI system, hardware, firmware.
  591. * Add host to SCSI
  592. */
  593. err = scsi_add_host(lp->host, &pdev->dev);
  594. if (err) {
  595. shost_printk(KERN_ERR, fnic->lport->host,
  596. "fnic: scsi_add_host failed...exiting\n");
  597. goto err_out_free_rq_buf;
  598. }
  599. /* Start local port initiatialization */
  600. lp->link_up = 0;
  601. lp->max_retry_count = fnic->config.flogi_retries;
  602. lp->max_rport_retry_count = fnic->config.plogi_retries;
  603. lp->service_params = (FCP_SPPF_INIT_FCN | FCP_SPPF_RD_XRDY_DIS |
  604. FCP_SPPF_CONF_COMPL);
  605. if (fnic->config.flags & VFCF_FCP_SEQ_LVL_ERR)
  606. lp->service_params |= FCP_SPPF_RETRY;
  607. lp->boot_time = jiffies;
  608. lp->e_d_tov = fnic->config.ed_tov;
  609. lp->r_a_tov = fnic->config.ra_tov;
  610. lp->link_supported_speeds = FC_PORTSPEED_10GBIT;
  611. fc_set_wwnn(lp, fnic->config.node_wwn);
  612. fc_set_wwpn(lp, fnic->config.port_wwn);
  613. fcoe_libfc_config(lp, &fnic->ctlr, &fnic_transport_template, 0);
  614. if (!fc_exch_mgr_alloc(lp, FC_CLASS_3, FCPIO_HOST_EXCH_RANGE_START,
  615. FCPIO_HOST_EXCH_RANGE_END, NULL)) {
  616. err = -ENOMEM;
  617. goto err_out_remove_scsi_host;
  618. }
  619. fc_lport_init_stats(lp);
  620. fc_lport_config(lp);
  621. if (fc_set_mfs(lp, fnic->config.maxdatafieldsize +
  622. sizeof(struct fc_frame_header))) {
  623. err = -EINVAL;
  624. goto err_out_free_exch_mgr;
  625. }
  626. fc_host_maxframe_size(lp->host) = lp->mfs;
  627. fc_host_dev_loss_tmo(lp->host) = fnic->config.port_down_timeout / 1000;
  628. sprintf(fc_host_symbolic_name(lp->host),
  629. DRV_NAME " v" DRV_VERSION " over %s", fnic->name);
  630. spin_lock_irqsave(&fnic_list_lock, flags);
  631. list_add_tail(&fnic->list, &fnic_list);
  632. spin_unlock_irqrestore(&fnic_list_lock, flags);
  633. INIT_WORK(&fnic->link_work, fnic_handle_link);
  634. INIT_WORK(&fnic->frame_work, fnic_handle_frame);
  635. skb_queue_head_init(&fnic->frame_queue);
  636. skb_queue_head_init(&fnic->tx_queue);
  637. /* Enable all queues */
  638. for (i = 0; i < fnic->raw_wq_count; i++)
  639. vnic_wq_enable(&fnic->wq[i]);
  640. for (i = 0; i < fnic->rq_count; i++)
  641. vnic_rq_enable(&fnic->rq[i]);
  642. for (i = 0; i < fnic->wq_copy_count; i++)
  643. vnic_wq_copy_enable(&fnic->wq_copy[i]);
  644. fc_fabric_login(lp);
  645. vnic_dev_enable(fnic->vdev);
  646. err = fnic_request_intr(fnic);
  647. if (err) {
  648. shost_printk(KERN_ERR, fnic->lport->host,
  649. "Unable to request irq.\n");
  650. goto err_out_free_exch_mgr;
  651. }
  652. for (i = 0; i < fnic->intr_count; i++)
  653. vnic_intr_unmask(&fnic->intr[i]);
  654. fnic_notify_timer_start(fnic);
  655. return 0;
  656. err_out_free_exch_mgr:
  657. fc_exch_mgr_free(lp);
  658. err_out_remove_scsi_host:
  659. fc_remove_host(lp->host);
  660. scsi_remove_host(lp->host);
  661. err_out_free_rq_buf:
  662. for (i = 0; i < fnic->rq_count; i++)
  663. vnic_rq_clean(&fnic->rq[i], fnic_free_rq_buf);
  664. vnic_dev_notify_unset(fnic->vdev);
  665. err_out_free_max_pool:
  666. mempool_destroy(fnic->io_sgl_pool[FNIC_SGL_CACHE_MAX]);
  667. err_out_free_dflt_pool:
  668. mempool_destroy(fnic->io_sgl_pool[FNIC_SGL_CACHE_DFLT]);
  669. err_out_free_ioreq_pool:
  670. mempool_destroy(fnic->io_req_pool);
  671. err_out_free_resources:
  672. fnic_free_vnic_resources(fnic);
  673. err_out_clear_intr:
  674. fnic_clear_intr_mode(fnic);
  675. err_out_dev_close:
  676. vnic_dev_close(fnic->vdev);
  677. err_out_vnic_unregister:
  678. vnic_dev_unregister(fnic->vdev);
  679. err_out_iounmap:
  680. fnic_iounmap(fnic);
  681. err_out_release_regions:
  682. pci_release_regions(pdev);
  683. err_out_disable_device:
  684. pci_disable_device(pdev);
  685. err_out_free_hba:
  686. scsi_host_put(lp->host);
  687. err_out:
  688. return err;
  689. }
  690. static void fnic_remove(struct pci_dev *pdev)
  691. {
  692. struct fnic *fnic = pci_get_drvdata(pdev);
  693. struct fc_lport *lp = fnic->lport;
  694. unsigned long flags;
  695. /*
  696. * Mark state so that the workqueue thread stops forwarding
  697. * received frames and link events to the local port. ISR and
  698. * other threads that can queue work items will also stop
  699. * creating work items on the fnic workqueue
  700. */
  701. spin_lock_irqsave(&fnic->fnic_lock, flags);
  702. fnic->stop_rx_link_events = 1;
  703. spin_unlock_irqrestore(&fnic->fnic_lock, flags);
  704. if (vnic_dev_get_intr_mode(fnic->vdev) == VNIC_DEV_INTR_MODE_MSI)
  705. del_timer_sync(&fnic->notify_timer);
  706. /*
  707. * Flush the fnic event queue. After this call, there should
  708. * be no event queued for this fnic device in the workqueue
  709. */
  710. flush_workqueue(fnic_event_queue);
  711. skb_queue_purge(&fnic->frame_queue);
  712. skb_queue_purge(&fnic->tx_queue);
  713. if (fnic->config.flags & VFCF_FIP_CAPABLE) {
  714. del_timer_sync(&fnic->fip_timer);
  715. skb_queue_purge(&fnic->fip_frame_queue);
  716. fnic_fcoe_reset_vlans(fnic);
  717. fnic_fcoe_evlist_free(fnic);
  718. }
  719. /*
  720. * Log off the fabric. This stops all remote ports, dns port,
  721. * logs off the fabric. This flushes all rport, disc, lport work
  722. * before returning
  723. */
  724. fc_fabric_logoff(fnic->lport);
  725. spin_lock_irqsave(&fnic->fnic_lock, flags);
  726. fnic->in_remove = 1;
  727. spin_unlock_irqrestore(&fnic->fnic_lock, flags);
  728. fcoe_ctlr_destroy(&fnic->ctlr);
  729. fc_lport_destroy(lp);
  730. /*
  731. * This stops the fnic device, masks all interrupts. Completed
  732. * CQ entries are drained. Posted WQ/RQ/Copy-WQ entries are
  733. * cleaned up
  734. */
  735. fnic_cleanup(fnic);
  736. BUG_ON(!skb_queue_empty(&fnic->frame_queue));
  737. BUG_ON(!skb_queue_empty(&fnic->tx_queue));
  738. spin_lock_irqsave(&fnic_list_lock, flags);
  739. list_del(&fnic->list);
  740. spin_unlock_irqrestore(&fnic_list_lock, flags);
  741. fc_remove_host(fnic->lport->host);
  742. scsi_remove_host(fnic->lport->host);
  743. fc_exch_mgr_free(fnic->lport);
  744. vnic_dev_notify_unset(fnic->vdev);
  745. fnic_free_intr(fnic);
  746. fnic_free_vnic_resources(fnic);
  747. fnic_clear_intr_mode(fnic);
  748. vnic_dev_close(fnic->vdev);
  749. vnic_dev_unregister(fnic->vdev);
  750. fnic_iounmap(fnic);
  751. pci_release_regions(pdev);
  752. pci_disable_device(pdev);
  753. pci_set_drvdata(pdev, NULL);
  754. scsi_host_put(lp->host);
  755. }
  756. static struct pci_driver fnic_driver = {
  757. .name = DRV_NAME,
  758. .id_table = fnic_id_table,
  759. .probe = fnic_probe,
  760. .remove = fnic_remove,
  761. };
  762. static int __init fnic_init_module(void)
  763. {
  764. size_t len;
  765. int err = 0;
  766. printk(KERN_INFO PFX "%s, ver %s\n", DRV_DESCRIPTION, DRV_VERSION);
  767. /* Allocate memory for trace buffer */
  768. err = fnic_trace_buf_init();
  769. if (err < 0) {
  770. printk(KERN_ERR PFX "Trace buffer initialization Failed "
  771. "Fnic Tracing utility is disabled\n");
  772. fnic_trace_free();
  773. }
  774. /* Create a cache for allocation of default size sgls */
  775. len = sizeof(struct fnic_dflt_sgl_list);
  776. fnic_sgl_cache[FNIC_SGL_CACHE_DFLT] = kmem_cache_create
  777. ("fnic_sgl_dflt", len + FNIC_SG_DESC_ALIGN, FNIC_SG_DESC_ALIGN,
  778. SLAB_HWCACHE_ALIGN,
  779. NULL);
  780. if (!fnic_sgl_cache[FNIC_SGL_CACHE_DFLT]) {
  781. printk(KERN_ERR PFX "failed to create fnic dflt sgl slab\n");
  782. err = -ENOMEM;
  783. goto err_create_fnic_sgl_slab_dflt;
  784. }
  785. /* Create a cache for allocation of max size sgls*/
  786. len = sizeof(struct fnic_sgl_list);
  787. fnic_sgl_cache[FNIC_SGL_CACHE_MAX] = kmem_cache_create
  788. ("fnic_sgl_max", len + FNIC_SG_DESC_ALIGN, FNIC_SG_DESC_ALIGN,
  789. SLAB_HWCACHE_ALIGN,
  790. NULL);
  791. if (!fnic_sgl_cache[FNIC_SGL_CACHE_MAX]) {
  792. printk(KERN_ERR PFX "failed to create fnic max sgl slab\n");
  793. err = -ENOMEM;
  794. goto err_create_fnic_sgl_slab_max;
  795. }
  796. /* Create a cache of io_req structs for use via mempool */
  797. fnic_io_req_cache = kmem_cache_create("fnic_io_req",
  798. sizeof(struct fnic_io_req),
  799. 0, SLAB_HWCACHE_ALIGN, NULL);
  800. if (!fnic_io_req_cache) {
  801. printk(KERN_ERR PFX "failed to create fnic io_req slab\n");
  802. err = -ENOMEM;
  803. goto err_create_fnic_ioreq_slab;
  804. }
  805. fnic_event_queue = create_singlethread_workqueue("fnic_event_wq");
  806. if (!fnic_event_queue) {
  807. printk(KERN_ERR PFX "fnic work queue create failed\n");
  808. err = -ENOMEM;
  809. goto err_create_fnic_workq;
  810. }
  811. spin_lock_init(&fnic_list_lock);
  812. INIT_LIST_HEAD(&fnic_list);
  813. fnic_fip_queue = create_singlethread_workqueue("fnic_fip_q");
  814. if (!fnic_fip_queue) {
  815. printk(KERN_ERR PFX "fnic FIP work queue create failed\n");
  816. err = -ENOMEM;
  817. goto err_create_fip_workq;
  818. }
  819. fnic_fc_transport = fc_attach_transport(&fnic_fc_functions);
  820. if (!fnic_fc_transport) {
  821. printk(KERN_ERR PFX "fc_attach_transport error\n");
  822. err = -ENOMEM;
  823. goto err_fc_transport;
  824. }
  825. /* register the driver with PCI system */
  826. err = pci_register_driver(&fnic_driver);
  827. if (err < 0) {
  828. printk(KERN_ERR PFX "pci register error\n");
  829. goto err_pci_register;
  830. }
  831. return err;
  832. err_pci_register:
  833. fc_release_transport(fnic_fc_transport);
  834. err_fc_transport:
  835. destroy_workqueue(fnic_fip_queue);
  836. err_create_fip_workq:
  837. destroy_workqueue(fnic_event_queue);
  838. err_create_fnic_workq:
  839. kmem_cache_destroy(fnic_io_req_cache);
  840. err_create_fnic_ioreq_slab:
  841. kmem_cache_destroy(fnic_sgl_cache[FNIC_SGL_CACHE_MAX]);
  842. err_create_fnic_sgl_slab_max:
  843. kmem_cache_destroy(fnic_sgl_cache[FNIC_SGL_CACHE_DFLT]);
  844. err_create_fnic_sgl_slab_dflt:
  845. fnic_trace_free();
  846. return err;
  847. }
  848. static void __exit fnic_cleanup_module(void)
  849. {
  850. pci_unregister_driver(&fnic_driver);
  851. destroy_workqueue(fnic_event_queue);
  852. if (fnic_fip_queue) {
  853. flush_workqueue(fnic_fip_queue);
  854. destroy_workqueue(fnic_fip_queue);
  855. }
  856. kmem_cache_destroy(fnic_sgl_cache[FNIC_SGL_CACHE_MAX]);
  857. kmem_cache_destroy(fnic_sgl_cache[FNIC_SGL_CACHE_DFLT]);
  858. kmem_cache_destroy(fnic_io_req_cache);
  859. fc_release_transport(fnic_fc_transport);
  860. fnic_trace_free();
  861. }
  862. module_init(fnic_init_module);
  863. module_exit(fnic_cleanup_module);