fcoe_sw.c 14 KB

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