fnic_main.c 26 KB

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