fnic_main.c 25 KB

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