fcoe_sw.c 14 KB

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