fnic_main.c 26 KB

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