fcoe_sw.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523
  1. /*
  2. * Copyright(c) 2007 - 2008 Intel Corporation. All rights reserved.
  3. *
  4. * This program is free software; you can redistribute it and/or modify it
  5. * under the terms and conditions of the GNU General Public License,
  6. * version 2, as published by the Free Software Foundation.
  7. *
  8. * This program is distributed in the hope it will be useful, but WITHOUT
  9. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  10. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  11. * more details.
  12. *
  13. * You should have received a copy of the GNU General Public License along with
  14. * this program; if not, write to the Free Software Foundation, Inc.,
  15. * 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
  16. *
  17. * Maintained at www.Open-FCoE.org
  18. */
  19. #include <linux/module.h>
  20. #include <linux/version.h>
  21. #include <linux/kernel.h>
  22. #include <linux/pci.h>
  23. #include <linux/init.h>
  24. #include <linux/spinlock.h>
  25. #include <linux/netdevice.h>
  26. #include <linux/etherdevice.h>
  27. #include <linux/if_vlan.h>
  28. #include <net/rtnetlink.h>
  29. #include <scsi/fc/fc_els.h>
  30. #include <scsi/fc/fc_encaps.h>
  31. #include <scsi/fc/fc_fs.h>
  32. #include <scsi/scsi_transport.h>
  33. #include <scsi/scsi_transport_fc.h>
  34. #include <scsi/libfc.h>
  35. #include <scsi/libfcoe.h>
  36. #define FCOE_SW_VERSION "0.1"
  37. #define FCOE_SW_NAME "fcoesw"
  38. #define FCOE_SW_VENDOR "Open-FCoE.org"
  39. #define FCOE_MAX_LUN 255
  40. #define FCOE_MAX_FCP_TARGET 256
  41. #define FCOE_MAX_OUTSTANDING_COMMANDS 1024
  42. #define FCOE_MIN_XID 0x0001 /* the min xid supported by fcoe_sw */
  43. #define FCOE_MAX_XID 0x07ef /* the max xid supported by fcoe_sw */
  44. static struct scsi_transport_template *scsi_transport_fcoe_sw;
  45. struct fc_function_template fcoe_sw_transport_function = {
  46. .show_host_node_name = 1,
  47. .show_host_port_name = 1,
  48. .show_host_supported_classes = 1,
  49. .show_host_supported_fc4s = 1,
  50. .show_host_active_fc4s = 1,
  51. .show_host_maxframe_size = 1,
  52. .show_host_port_id = 1,
  53. .show_host_supported_speeds = 1,
  54. .get_host_speed = fc_get_host_speed,
  55. .show_host_speed = 1,
  56. .show_host_port_type = 1,
  57. .get_host_port_state = fc_get_host_port_state,
  58. .show_host_port_state = 1,
  59. .show_host_symbolic_name = 1,
  60. .dd_fcrport_size = sizeof(struct fc_rport_libfc_priv),
  61. .show_rport_maxframe_size = 1,
  62. .show_rport_supported_classes = 1,
  63. .show_host_fabric_name = 1,
  64. .show_starget_node_name = 1,
  65. .show_starget_port_name = 1,
  66. .show_starget_port_id = 1,
  67. .set_rport_dev_loss_tmo = fc_set_rport_loss_tmo,
  68. .show_rport_dev_loss_tmo = 1,
  69. .get_fc_host_stats = fc_get_host_stats,
  70. .issue_fc_host_lip = fcoe_reset,
  71. .terminate_rport_io = fc_rport_terminate_io,
  72. };
  73. static struct scsi_host_template fcoe_sw_shost_template = {
  74. .module = THIS_MODULE,
  75. .name = "FCoE Driver",
  76. .proc_name = FCOE_SW_NAME,
  77. .queuecommand = fc_queuecommand,
  78. .eh_abort_handler = fc_eh_abort,
  79. .eh_device_reset_handler = fc_eh_device_reset,
  80. .eh_host_reset_handler = fc_eh_host_reset,
  81. .slave_alloc = fc_slave_alloc,
  82. .change_queue_depth = fc_change_queue_depth,
  83. .change_queue_type = fc_change_queue_type,
  84. .this_id = -1,
  85. .cmd_per_lun = 32,
  86. .can_queue = FCOE_MAX_OUTSTANDING_COMMANDS,
  87. .use_clustering = ENABLE_CLUSTERING,
  88. .sg_tablesize = SG_ALL,
  89. .max_sectors = 0xffff,
  90. };
  91. /**
  92. * fcoe_sw_lport_config() - sets up the fc_lport
  93. * @lp: ptr to the fc_lport
  94. * @shost: ptr to the parent scsi host
  95. *
  96. * Returns: 0 for success
  97. */
  98. static int fcoe_sw_lport_config(struct fc_lport *lp)
  99. {
  100. lp->link_up = 0;
  101. lp->qfull = 0;
  102. lp->max_retry_count = 3;
  103. lp->e_d_tov = 2 * 1000; /* FC-FS default */
  104. lp->r_a_tov = 2 * 2 * 1000;
  105. lp->service_params = (FCP_SPPF_INIT_FCN | FCP_SPPF_RD_XRDY_DIS |
  106. FCP_SPPF_RETRY | FCP_SPPF_CONF_COMPL);
  107. fc_lport_init_stats(lp);
  108. /* lport fc_lport related configuration */
  109. fc_lport_config(lp);
  110. /* offload related configuration */
  111. lp->crc_offload = 0;
  112. lp->seq_offload = 0;
  113. lp->lro_enabled = 0;
  114. lp->lro_xid = 0;
  115. lp->lso_max = 0;
  116. return 0;
  117. }
  118. /**
  119. * fcoe_sw_netdev_config() - Set up netdev for SW FCoE
  120. * @lp : ptr to the fc_lport
  121. * @netdev : ptr to the associated netdevice struct
  122. *
  123. * Must be called after fcoe_sw_lport_config() as it will use lport mutex
  124. *
  125. * Returns : 0 for success
  126. */
  127. static int fcoe_sw_netdev_config(struct fc_lport *lp, struct net_device *netdev)
  128. {
  129. u32 mfs;
  130. u64 wwnn, wwpn;
  131. struct fcoe_softc *fc;
  132. u8 flogi_maddr[ETH_ALEN];
  133. /* Setup lport private data to point to fcoe softc */
  134. fc = lport_priv(lp);
  135. fc->lp = lp;
  136. fc->real_dev = netdev;
  137. fc->phys_dev = netdev;
  138. /* Require support for get_pauseparam ethtool op. */
  139. if (netdev->priv_flags & IFF_802_1Q_VLAN)
  140. fc->phys_dev = vlan_dev_real_dev(netdev);
  141. /* Do not support for bonding device */
  142. if ((fc->real_dev->priv_flags & IFF_MASTER_ALB) ||
  143. (fc->real_dev->priv_flags & IFF_SLAVE_INACTIVE) ||
  144. (fc->real_dev->priv_flags & IFF_MASTER_8023AD)) {
  145. return -EOPNOTSUPP;
  146. }
  147. /*
  148. * Determine max frame size based on underlying device and optional
  149. * user-configured limit. If the MFS is too low, fcoe_link_ok()
  150. * will return 0, so do this first.
  151. */
  152. mfs = fc->real_dev->mtu - (sizeof(struct fcoe_hdr) +
  153. sizeof(struct fcoe_crc_eof));
  154. if (fc_set_mfs(lp, mfs))
  155. return -EINVAL;
  156. if (!fcoe_link_ok(lp))
  157. lp->link_up = 1;
  158. /* offload features support */
  159. if (fc->real_dev->features & NETIF_F_SG)
  160. lp->sg_supp = 1;
  161. #ifdef NETIF_F_FCOE_CRC
  162. if (netdev->features & NETIF_F_FCOE_CRC) {
  163. lp->crc_offload = 1;
  164. printk(KERN_DEBUG "fcoe:%s supports FCCRC offload\n",
  165. netdev->name);
  166. }
  167. #endif
  168. #ifdef NETIF_F_FSO
  169. if (netdev->features & NETIF_F_FSO) {
  170. lp->seq_offload = 1;
  171. lp->lso_max = netdev->gso_max_size;
  172. printk(KERN_DEBUG "fcoe:%s supports LSO for max len 0x%x\n",
  173. netdev->name, lp->lso_max);
  174. }
  175. #endif
  176. if (netdev->fcoe_ddp_xid) {
  177. lp->lro_enabled = 1;
  178. lp->lro_xid = netdev->fcoe_ddp_xid;
  179. printk(KERN_DEBUG "fcoe:%s supports LRO for max xid 0x%x\n",
  180. netdev->name, lp->lro_xid);
  181. }
  182. skb_queue_head_init(&fc->fcoe_pending_queue);
  183. fc->fcoe_pending_queue_active = 0;
  184. /* setup Source Mac Address */
  185. memcpy(fc->ctl_src_addr, fc->real_dev->dev_addr,
  186. fc->real_dev->addr_len);
  187. wwnn = fcoe_wwn_from_mac(fc->real_dev->dev_addr, 1, 0);
  188. fc_set_wwnn(lp, wwnn);
  189. /* XXX - 3rd arg needs to be vlan id */
  190. wwpn = fcoe_wwn_from_mac(fc->real_dev->dev_addr, 2, 0);
  191. fc_set_wwpn(lp, wwpn);
  192. /*
  193. * Add FCoE MAC address as second unicast MAC address
  194. * or enter promiscuous mode if not capable of listening
  195. * for multiple unicast MACs.
  196. */
  197. rtnl_lock();
  198. memcpy(flogi_maddr, (u8[6]) FC_FCOE_FLOGI_MAC, ETH_ALEN);
  199. dev_unicast_add(fc->real_dev, flogi_maddr, ETH_ALEN);
  200. rtnl_unlock();
  201. /*
  202. * setup the receive function from ethernet driver
  203. * on the ethertype for the given device
  204. */
  205. fc->fcoe_packet_type.func = fcoe_rcv;
  206. fc->fcoe_packet_type.type = __constant_htons(ETH_P_FCOE);
  207. fc->fcoe_packet_type.dev = fc->real_dev;
  208. dev_add_pack(&fc->fcoe_packet_type);
  209. return 0;
  210. }
  211. /**
  212. * fcoe_sw_shost_config() - Sets up fc_lport->host
  213. * @lp : ptr to the fc_lport
  214. * @shost : ptr to the associated scsi host
  215. * @dev : device associated to scsi host
  216. *
  217. * Must be called after fcoe_sw_lport_config() and fcoe_sw_netdev_config()
  218. *
  219. * Returns : 0 for success
  220. */
  221. static int fcoe_sw_shost_config(struct fc_lport *lp, struct Scsi_Host *shost,
  222. struct device *dev)
  223. {
  224. int rc = 0;
  225. /* lport scsi host config */
  226. lp->host = shost;
  227. lp->host->max_lun = FCOE_MAX_LUN;
  228. lp->host->max_id = FCOE_MAX_FCP_TARGET;
  229. lp->host->max_channel = 0;
  230. lp->host->transportt = scsi_transport_fcoe_sw;
  231. /* add the new host to the SCSI-ml */
  232. rc = scsi_add_host(lp->host, dev);
  233. if (rc) {
  234. FC_DBG("fcoe_sw_shost_config:error on scsi_add_host\n");
  235. return rc;
  236. }
  237. sprintf(fc_host_symbolic_name(lp->host), "%s v%s over %s",
  238. FCOE_SW_NAME, FCOE_SW_VERSION,
  239. fcoe_netdev(lp)->name);
  240. return 0;
  241. }
  242. /**
  243. * fcoe_sw_em_config() - allocates em for this lport
  244. * @lp: the port that em is to allocated for
  245. *
  246. * Returns : 0 on success
  247. */
  248. static inline int fcoe_sw_em_config(struct fc_lport *lp)
  249. {
  250. BUG_ON(lp->emp);
  251. lp->emp = fc_exch_mgr_alloc(lp, FC_CLASS_3,
  252. FCOE_MIN_XID, FCOE_MAX_XID);
  253. if (!lp->emp)
  254. return -ENOMEM;
  255. return 0;
  256. }
  257. /**
  258. * fcoe_sw_destroy() - FCoE software HBA tear-down function
  259. * @netdev: ptr to the associated net_device
  260. *
  261. * Returns: 0 if link is OK for use by FCoE.
  262. */
  263. int fcoe_sw_destroy(struct net_device *netdev)
  264. {
  265. struct fc_lport *lp = NULL;
  266. struct fcoe_softc *fc;
  267. u8 flogi_maddr[ETH_ALEN];
  268. BUG_ON(!netdev);
  269. printk(KERN_DEBUG "fcoe_sw_destroy:interface on %s\n",
  270. netdev->name);
  271. lp = fcoe_hostlist_lookup(netdev);
  272. if (!lp)
  273. return -ENODEV;
  274. fc = lport_priv(lp);
  275. /* Logout of the fabric */
  276. fc_fabric_logoff(lp);
  277. /* Remove the instance from fcoe's list */
  278. fcoe_hostlist_remove(lp);
  279. /* Don't listen for Ethernet packets anymore */
  280. dev_remove_pack(&fc->fcoe_packet_type);
  281. /* Cleanup the fc_lport */
  282. fc_lport_destroy(lp);
  283. fc_fcp_destroy(lp);
  284. /* Detach from the scsi-ml */
  285. fc_remove_host(lp->host);
  286. scsi_remove_host(lp->host);
  287. /* There are no more rports or I/O, free the EM */
  288. if (lp->emp)
  289. fc_exch_mgr_free(lp->emp);
  290. /* Delete secondary MAC addresses */
  291. rtnl_lock();
  292. memcpy(flogi_maddr, (u8[6]) FC_FCOE_FLOGI_MAC, ETH_ALEN);
  293. dev_unicast_delete(fc->real_dev, flogi_maddr, ETH_ALEN);
  294. if (compare_ether_addr(fc->data_src_addr, (u8[6]) { 0 }))
  295. dev_unicast_delete(fc->real_dev, fc->data_src_addr, ETH_ALEN);
  296. rtnl_unlock();
  297. /* Free the per-CPU revieve threads */
  298. fcoe_percpu_clean(lp);
  299. /* Free existing skbs */
  300. fcoe_clean_pending_queue(lp);
  301. /* Free memory used by statistical counters */
  302. fc_lport_free_stats(lp);
  303. /* Release the net_device and Scsi_Host */
  304. dev_put(fc->real_dev);
  305. scsi_host_put(lp->host);
  306. return 0;
  307. }
  308. /*
  309. * fcoe_sw_ddp_setup - calls LLD's ddp_setup through net_device
  310. * @lp: the corresponding fc_lport
  311. * @xid: the exchange id for this ddp transfer
  312. * @sgl: the scatterlist describing this transfer
  313. * @sgc: number of sg items
  314. *
  315. * Returns : 0 no ddp
  316. */
  317. static int fcoe_sw_ddp_setup(struct fc_lport *lp, u16 xid,
  318. struct scatterlist *sgl, unsigned int sgc)
  319. {
  320. struct net_device *n = fcoe_netdev(lp);
  321. if (n->netdev_ops && n->netdev_ops->ndo_fcoe_ddp_setup)
  322. return n->netdev_ops->ndo_fcoe_ddp_setup(n, xid, sgl, sgc);
  323. return 0;
  324. }
  325. /*
  326. * fcoe_sw_ddp_done - calls LLD's ddp_done through net_device
  327. * @lp: the corresponding fc_lport
  328. * @xid: the exchange id for this ddp transfer
  329. *
  330. * Returns : the length of data that have been completed by ddp
  331. */
  332. static int fcoe_sw_ddp_done(struct fc_lport *lp, u16 xid)
  333. {
  334. struct net_device *n = fcoe_netdev(lp);
  335. if (n->netdev_ops && n->netdev_ops->ndo_fcoe_ddp_done)
  336. return n->netdev_ops->ndo_fcoe_ddp_done(n, xid);
  337. return 0;
  338. }
  339. static struct libfc_function_template fcoe_sw_libfc_fcn_templ = {
  340. .frame_send = fcoe_xmit,
  341. .ddp_setup = fcoe_sw_ddp_setup,
  342. .ddp_done = fcoe_sw_ddp_done,
  343. };
  344. /**
  345. * fcoe_sw_create() - this function creates the fcoe interface
  346. * @netdev: pointer the associated netdevice
  347. *
  348. * Creates fc_lport struct and scsi_host for lport, configures lport
  349. * and starts fabric login.
  350. *
  351. * Returns : 0 on success
  352. */
  353. int fcoe_sw_create(struct net_device *netdev)
  354. {
  355. int rc;
  356. struct fc_lport *lp = NULL;
  357. struct fcoe_softc *fc;
  358. struct Scsi_Host *shost;
  359. BUG_ON(!netdev);
  360. printk(KERN_DEBUG "fcoe_sw_create:interface on %s\n",
  361. netdev->name);
  362. lp = fcoe_hostlist_lookup(netdev);
  363. if (lp)
  364. return -EEXIST;
  365. shost = fcoe_host_alloc(&fcoe_sw_shost_template,
  366. sizeof(struct fcoe_softc));
  367. if (!shost) {
  368. FC_DBG("Could not allocate host structure\n");
  369. return -ENOMEM;
  370. }
  371. lp = shost_priv(shost);
  372. fc = lport_priv(lp);
  373. /* configure fc_lport, e.g., em */
  374. rc = fcoe_sw_lport_config(lp);
  375. if (rc) {
  376. FC_DBG("Could not configure lport\n");
  377. goto out_host_put;
  378. }
  379. /* configure lport network properties */
  380. rc = fcoe_sw_netdev_config(lp, netdev);
  381. if (rc) {
  382. FC_DBG("Could not configure netdev for lport\n");
  383. goto out_host_put;
  384. }
  385. /* configure lport scsi host properties */
  386. rc = fcoe_sw_shost_config(lp, shost, &netdev->dev);
  387. if (rc) {
  388. FC_DBG("Could not configure shost for lport\n");
  389. goto out_host_put;
  390. }
  391. /* lport exch manager allocation */
  392. rc = fcoe_sw_em_config(lp);
  393. if (rc) {
  394. FC_DBG("Could not configure em for lport\n");
  395. goto out_host_put;
  396. }
  397. /* Initialize the library */
  398. rc = fcoe_libfc_config(lp, &fcoe_sw_libfc_fcn_templ);
  399. if (rc) {
  400. FC_DBG("Could not configure libfc for lport!\n");
  401. goto out_lp_destroy;
  402. }
  403. /* add to lports list */
  404. fcoe_hostlist_add(lp);
  405. lp->boot_time = jiffies;
  406. fc_fabric_login(lp);
  407. dev_hold(netdev);
  408. return rc;
  409. out_lp_destroy:
  410. fc_exch_mgr_free(lp->emp); /* Free the EM */
  411. out_host_put:
  412. scsi_host_put(lp->host);
  413. return rc;
  414. }
  415. /**
  416. * fcoe_sw_init() - attach to scsi transport
  417. *
  418. * Returns : 0 on success
  419. */
  420. int __init fcoe_sw_init(void)
  421. {
  422. /* attach to scsi transport */
  423. scsi_transport_fcoe_sw =
  424. fc_attach_transport(&fcoe_sw_transport_function);
  425. if (!scsi_transport_fcoe_sw) {
  426. printk(KERN_ERR "fcoe_sw_init:fc_attach_transport() failed\n");
  427. return -ENODEV;
  428. }
  429. return 0;
  430. }
  431. /**
  432. * fcoe_sw_exit() - detach from scsi transport
  433. *
  434. * Returns : 0 on success
  435. */
  436. int __exit fcoe_sw_exit(void)
  437. {
  438. fc_release_transport(scsi_transport_fcoe_sw);
  439. return 0;
  440. }