fnic_main.c 27 KB

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