fcoe_sw.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496
  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. 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. skb_queue_head_init(&fc->fcoe_pending_queue);
  163. fc->fcoe_pending_queue_active = 0;
  164. /* setup Source Mac Address */
  165. memcpy(fc->ctl_src_addr, fc->real_dev->dev_addr,
  166. fc->real_dev->addr_len);
  167. wwnn = fcoe_wwn_from_mac(fc->real_dev->dev_addr, 1, 0);
  168. fc_set_wwnn(lp, wwnn);
  169. /* XXX - 3rd arg needs to be vlan id */
  170. wwpn = fcoe_wwn_from_mac(fc->real_dev->dev_addr, 2, 0);
  171. fc_set_wwpn(lp, wwpn);
  172. /*
  173. * Add FCoE MAC address as second unicast MAC address
  174. * or enter promiscuous mode if not capable of listening
  175. * for multiple unicast MACs.
  176. */
  177. rtnl_lock();
  178. memcpy(flogi_maddr, (u8[6]) FC_FCOE_FLOGI_MAC, ETH_ALEN);
  179. dev_unicast_add(fc->real_dev, flogi_maddr, ETH_ALEN);
  180. rtnl_unlock();
  181. /*
  182. * setup the receive function from ethernet driver
  183. * on the ethertype for the given device
  184. */
  185. fc->fcoe_packet_type.func = fcoe_rcv;
  186. fc->fcoe_packet_type.type = __constant_htons(ETH_P_FCOE);
  187. fc->fcoe_packet_type.dev = fc->real_dev;
  188. dev_add_pack(&fc->fcoe_packet_type);
  189. return 0;
  190. }
  191. /**
  192. * fcoe_sw_shost_config() - Sets up fc_lport->host
  193. * @lp : ptr to the fc_lport
  194. * @shost : ptr to the associated scsi host
  195. * @dev : device associated to scsi host
  196. *
  197. * Must be called after fcoe_sw_lport_config() and fcoe_sw_netdev_config()
  198. *
  199. * Returns : 0 for success
  200. */
  201. static int fcoe_sw_shost_config(struct fc_lport *lp, struct Scsi_Host *shost,
  202. struct device *dev)
  203. {
  204. int rc = 0;
  205. /* lport scsi host config */
  206. lp->host = shost;
  207. lp->host->max_lun = FCOE_MAX_LUN;
  208. lp->host->max_id = FCOE_MAX_FCP_TARGET;
  209. lp->host->max_channel = 0;
  210. lp->host->transportt = scsi_transport_fcoe_sw;
  211. /* add the new host to the SCSI-ml */
  212. rc = scsi_add_host(lp->host, dev);
  213. if (rc) {
  214. FC_DBG("fcoe_sw_shost_config:error on scsi_add_host\n");
  215. return rc;
  216. }
  217. sprintf(fc_host_symbolic_name(lp->host), "%s v%s over %s",
  218. FCOE_SW_NAME, FCOE_SW_VERSION,
  219. fcoe_netdev(lp)->name);
  220. return 0;
  221. }
  222. /**
  223. * fcoe_sw_em_config() - allocates em for this lport
  224. * @lp: the port that em is to allocated for
  225. *
  226. * Returns : 0 on success
  227. */
  228. static inline int fcoe_sw_em_config(struct fc_lport *lp)
  229. {
  230. BUG_ON(lp->emp);
  231. lp->emp = fc_exch_mgr_alloc(lp, FC_CLASS_3,
  232. FCOE_MIN_XID, FCOE_MAX_XID);
  233. if (!lp->emp)
  234. return -ENOMEM;
  235. return 0;
  236. }
  237. /**
  238. * fcoe_sw_destroy() - FCoE software HBA tear-down function
  239. * @netdev: ptr to the associated net_device
  240. *
  241. * Returns: 0 if link is OK for use by FCoE.
  242. */
  243. static int fcoe_sw_destroy(struct net_device *netdev)
  244. {
  245. int cpu;
  246. struct fc_lport *lp = NULL;
  247. struct fcoe_softc *fc;
  248. u8 flogi_maddr[ETH_ALEN];
  249. BUG_ON(!netdev);
  250. printk(KERN_DEBUG "fcoe_sw_destroy:interface on %s\n",
  251. netdev->name);
  252. lp = fcoe_hostlist_lookup(netdev);
  253. if (!lp)
  254. return -ENODEV;
  255. fc = lport_priv(lp);
  256. /* Logout of the fabric */
  257. fc_fabric_logoff(lp);
  258. /* Remove the instance from fcoe's list */
  259. fcoe_hostlist_remove(lp);
  260. /* Don't listen for Ethernet packets anymore */
  261. dev_remove_pack(&fc->fcoe_packet_type);
  262. /* Cleanup the fc_lport */
  263. fc_lport_destroy(lp);
  264. fc_fcp_destroy(lp);
  265. /* Detach from the scsi-ml */
  266. fc_remove_host(lp->host);
  267. scsi_remove_host(lp->host);
  268. /* There are no more rports or I/O, free the EM */
  269. if (lp->emp)
  270. fc_exch_mgr_free(lp->emp);
  271. /* Delete secondary MAC addresses */
  272. rtnl_lock();
  273. memcpy(flogi_maddr, (u8[6]) FC_FCOE_FLOGI_MAC, ETH_ALEN);
  274. dev_unicast_delete(fc->real_dev, flogi_maddr, ETH_ALEN);
  275. if (compare_ether_addr(fc->data_src_addr, (u8[6]) { 0 }))
  276. dev_unicast_delete(fc->real_dev, fc->data_src_addr, ETH_ALEN);
  277. rtnl_unlock();
  278. /* Free the per-CPU revieve threads */
  279. fcoe_percpu_clean(lp);
  280. /* Free existing skbs */
  281. fcoe_clean_pending_queue(lp);
  282. /* Free memory used by statistical counters */
  283. for_each_online_cpu(cpu)
  284. kfree(lp->dev_stats[cpu]);
  285. /* Release the net_device and Scsi_Host */
  286. dev_put(fc->real_dev);
  287. scsi_host_put(lp->host);
  288. return 0;
  289. }
  290. static struct libfc_function_template fcoe_sw_libfc_fcn_templ = {
  291. .frame_send = fcoe_xmit,
  292. };
  293. /**
  294. * fcoe_sw_create() - this function creates the fcoe interface
  295. * @netdev: pointer the associated netdevice
  296. *
  297. * Creates fc_lport struct and scsi_host for lport, configures lport
  298. * and starts fabric login.
  299. *
  300. * Returns : 0 on success
  301. */
  302. static int fcoe_sw_create(struct net_device *netdev)
  303. {
  304. int rc;
  305. struct fc_lport *lp = NULL;
  306. struct fcoe_softc *fc;
  307. struct Scsi_Host *shost;
  308. BUG_ON(!netdev);
  309. printk(KERN_DEBUG "fcoe_sw_create:interface on %s\n",
  310. netdev->name);
  311. lp = fcoe_hostlist_lookup(netdev);
  312. if (lp)
  313. return -EEXIST;
  314. shost = fcoe_host_alloc(&fcoe_sw_shost_template,
  315. sizeof(struct fcoe_softc));
  316. if (!shost) {
  317. FC_DBG("Could not allocate host structure\n");
  318. return -ENOMEM;
  319. }
  320. lp = shost_priv(shost);
  321. fc = lport_priv(lp);
  322. /* configure fc_lport, e.g., em */
  323. rc = fcoe_sw_lport_config(lp);
  324. if (rc) {
  325. FC_DBG("Could not configure lport\n");
  326. goto out_host_put;
  327. }
  328. /* configure lport network properties */
  329. rc = fcoe_sw_netdev_config(lp, netdev);
  330. if (rc) {
  331. FC_DBG("Could not configure netdev for lport\n");
  332. goto out_host_put;
  333. }
  334. /* configure lport scsi host properties */
  335. rc = fcoe_sw_shost_config(lp, shost, &netdev->dev);
  336. if (rc) {
  337. FC_DBG("Could not configure shost for lport\n");
  338. goto out_host_put;
  339. }
  340. /* lport exch manager allocation */
  341. rc = fcoe_sw_em_config(lp);
  342. if (rc) {
  343. FC_DBG("Could not configure em for lport\n");
  344. goto out_host_put;
  345. }
  346. /* Initialize the library */
  347. rc = fcoe_libfc_config(lp, &fcoe_sw_libfc_fcn_templ);
  348. if (rc) {
  349. FC_DBG("Could not configure libfc for lport!\n");
  350. goto out_lp_destroy;
  351. }
  352. /* add to lports list */
  353. fcoe_hostlist_add(lp);
  354. lp->boot_time = jiffies;
  355. fc_fabric_login(lp);
  356. dev_hold(netdev);
  357. return rc;
  358. out_lp_destroy:
  359. fc_exch_mgr_free(lp->emp); /* Free the EM */
  360. out_host_put:
  361. scsi_host_put(lp->host);
  362. return rc;
  363. }
  364. /**
  365. * fcoe_sw_match() - The FCoE SW transport match function
  366. *
  367. * Returns : false always
  368. */
  369. static bool fcoe_sw_match(struct net_device *netdev)
  370. {
  371. /* FIXME - for sw transport, always return false */
  372. return false;
  373. }
  374. /* the sw hba fcoe transport */
  375. struct fcoe_transport fcoe_sw_transport = {
  376. .name = "fcoesw",
  377. .create = fcoe_sw_create,
  378. .destroy = fcoe_sw_destroy,
  379. .match = fcoe_sw_match,
  380. .vendor = 0x0,
  381. .device = 0xffff,
  382. };
  383. /**
  384. * fcoe_sw_init() - Registers fcoe_sw_transport
  385. *
  386. * Returns : 0 on success
  387. */
  388. int __init fcoe_sw_init(void)
  389. {
  390. /* attach to scsi transport */
  391. scsi_transport_fcoe_sw =
  392. fc_attach_transport(&fcoe_sw_transport_function);
  393. if (!scsi_transport_fcoe_sw) {
  394. printk(KERN_ERR "fcoe_sw_init:fc_attach_transport() failed\n");
  395. return -ENODEV;
  396. }
  397. mutex_init(&fcoe_sw_transport.devlock);
  398. INIT_LIST_HEAD(&fcoe_sw_transport.devlist);
  399. /* register sw transport */
  400. fcoe_transport_register(&fcoe_sw_transport);
  401. return 0;
  402. }
  403. /**
  404. * fcoe_sw_exit() - Unregisters fcoe_sw_transport
  405. *
  406. * Returns : 0 on success
  407. */
  408. int __exit fcoe_sw_exit(void)
  409. {
  410. /* dettach the transport */
  411. fc_release_transport(scsi_transport_fcoe_sw);
  412. fcoe_transport_unregister(&fcoe_sw_transport);
  413. return 0;
  414. }