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