fcoe.c 45 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864
  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/spinlock.h>
  22. #include <linux/netdevice.h>
  23. #include <linux/etherdevice.h>
  24. #include <linux/ethtool.h>
  25. #include <linux/if_ether.h>
  26. #include <linux/if_vlan.h>
  27. #include <linux/crc32.h>
  28. #include <linux/cpu.h>
  29. #include <linux/fs.h>
  30. #include <linux/sysfs.h>
  31. #include <linux/ctype.h>
  32. #include <scsi/scsi_tcq.h>
  33. #include <scsi/scsicam.h>
  34. #include <scsi/scsi_transport.h>
  35. #include <scsi/scsi_transport_fc.h>
  36. #include <net/rtnetlink.h>
  37. #include <scsi/fc/fc_encaps.h>
  38. #include <scsi/fc/fc_fip.h>
  39. #include <scsi/libfc.h>
  40. #include <scsi/fc_frame.h>
  41. #include <scsi/libfcoe.h>
  42. #include "fcoe.h"
  43. static int debug_fcoe;
  44. MODULE_AUTHOR("Open-FCoE.org");
  45. MODULE_DESCRIPTION("FCoE");
  46. MODULE_LICENSE("GPL v2");
  47. /* fcoe host list */
  48. LIST_HEAD(fcoe_hostlist);
  49. DEFINE_RWLOCK(fcoe_hostlist_lock);
  50. DEFINE_TIMER(fcoe_timer, NULL, 0, 0);
  51. DEFINE_PER_CPU(struct fcoe_percpu_s, fcoe_percpu);
  52. /* Function Prototypes */
  53. static int fcoe_reset(struct Scsi_Host *shost);
  54. static int fcoe_xmit(struct fc_lport *, struct fc_frame *);
  55. static int fcoe_rcv(struct sk_buff *, struct net_device *,
  56. struct packet_type *, struct net_device *);
  57. static int fcoe_percpu_receive_thread(void *arg);
  58. static void fcoe_clean_pending_queue(struct fc_lport *lp);
  59. static void fcoe_percpu_clean(struct fc_lport *lp);
  60. static int fcoe_link_ok(struct fc_lport *lp);
  61. static struct fc_lport *fcoe_hostlist_lookup(const struct net_device *);
  62. static int fcoe_hostlist_add(const struct fc_lport *);
  63. static int fcoe_hostlist_remove(const struct fc_lport *);
  64. static int fcoe_check_wait_queue(struct fc_lport *);
  65. static int fcoe_device_notification(struct notifier_block *, ulong, void *);
  66. static void fcoe_dev_setup(void);
  67. static void fcoe_dev_cleanup(void);
  68. /* notification function from net device */
  69. static struct notifier_block fcoe_notifier = {
  70. .notifier_call = fcoe_device_notification,
  71. };
  72. static struct scsi_transport_template *scsi_transport_fcoe_sw;
  73. struct fc_function_template fcoe_transport_function = {
  74. .show_host_node_name = 1,
  75. .show_host_port_name = 1,
  76. .show_host_supported_classes = 1,
  77. .show_host_supported_fc4s = 1,
  78. .show_host_active_fc4s = 1,
  79. .show_host_maxframe_size = 1,
  80. .show_host_port_id = 1,
  81. .show_host_supported_speeds = 1,
  82. .get_host_speed = fc_get_host_speed,
  83. .show_host_speed = 1,
  84. .show_host_port_type = 1,
  85. .get_host_port_state = fc_get_host_port_state,
  86. .show_host_port_state = 1,
  87. .show_host_symbolic_name = 1,
  88. .dd_fcrport_size = sizeof(struct fc_rport_libfc_priv),
  89. .show_rport_maxframe_size = 1,
  90. .show_rport_supported_classes = 1,
  91. .show_host_fabric_name = 1,
  92. .show_starget_node_name = 1,
  93. .show_starget_port_name = 1,
  94. .show_starget_port_id = 1,
  95. .set_rport_dev_loss_tmo = fc_set_rport_loss_tmo,
  96. .show_rport_dev_loss_tmo = 1,
  97. .get_fc_host_stats = fc_get_host_stats,
  98. .issue_fc_host_lip = fcoe_reset,
  99. .terminate_rport_io = fc_rport_terminate_io,
  100. };
  101. static struct scsi_host_template fcoe_shost_template = {
  102. .module = THIS_MODULE,
  103. .name = "FCoE Driver",
  104. .proc_name = FCOE_NAME,
  105. .queuecommand = fc_queuecommand,
  106. .eh_abort_handler = fc_eh_abort,
  107. .eh_device_reset_handler = fc_eh_device_reset,
  108. .eh_host_reset_handler = fc_eh_host_reset,
  109. .slave_alloc = fc_slave_alloc,
  110. .change_queue_depth = fc_change_queue_depth,
  111. .change_queue_type = fc_change_queue_type,
  112. .this_id = -1,
  113. .cmd_per_lun = 32,
  114. .can_queue = FCOE_MAX_OUTSTANDING_COMMANDS,
  115. .use_clustering = ENABLE_CLUSTERING,
  116. .sg_tablesize = SG_ALL,
  117. .max_sectors = 0xffff,
  118. };
  119. /**
  120. * fcoe_lport_config() - sets up the fc_lport
  121. * @lp: ptr to the fc_lport
  122. *
  123. * Returns: 0 for success
  124. */
  125. static int fcoe_lport_config(struct fc_lport *lp)
  126. {
  127. lp->link_up = 0;
  128. lp->qfull = 0;
  129. lp->max_retry_count = 3;
  130. lp->max_rport_retry_count = 3;
  131. lp->e_d_tov = 2 * 1000; /* FC-FS default */
  132. lp->r_a_tov = 2 * 2 * 1000;
  133. lp->service_params = (FCP_SPPF_INIT_FCN | FCP_SPPF_RD_XRDY_DIS |
  134. FCP_SPPF_RETRY | FCP_SPPF_CONF_COMPL);
  135. fc_lport_init_stats(lp);
  136. /* lport fc_lport related configuration */
  137. fc_lport_config(lp);
  138. /* offload related configuration */
  139. lp->crc_offload = 0;
  140. lp->seq_offload = 0;
  141. lp->lro_enabled = 0;
  142. lp->lro_xid = 0;
  143. lp->lso_max = 0;
  144. return 0;
  145. }
  146. /**
  147. * fcoe_netdev_config() - Set up netdev for SW FCoE
  148. * @lp : ptr to the fc_lport
  149. * @netdev : ptr to the associated netdevice struct
  150. *
  151. * Must be called after fcoe_lport_config() as it will use lport mutex
  152. *
  153. * Returns : 0 for success
  154. */
  155. static int fcoe_netdev_config(struct fc_lport *lp, struct net_device *netdev)
  156. {
  157. u32 mfs;
  158. u64 wwnn, wwpn;
  159. struct fcoe_softc *fc;
  160. u8 flogi_maddr[ETH_ALEN];
  161. /* Setup lport private data to point to fcoe softc */
  162. fc = lport_priv(lp);
  163. fc->ctlr.lp = lp;
  164. fc->real_dev = netdev;
  165. fc->phys_dev = netdev;
  166. /* Require support for get_pauseparam ethtool op. */
  167. if (netdev->priv_flags & IFF_802_1Q_VLAN)
  168. fc->phys_dev = vlan_dev_real_dev(netdev);
  169. /* Do not support for bonding device */
  170. if ((fc->real_dev->priv_flags & IFF_MASTER_ALB) ||
  171. (fc->real_dev->priv_flags & IFF_SLAVE_INACTIVE) ||
  172. (fc->real_dev->priv_flags & IFF_MASTER_8023AD)) {
  173. return -EOPNOTSUPP;
  174. }
  175. /*
  176. * Determine max frame size based on underlying device and optional
  177. * user-configured limit. If the MFS is too low, fcoe_link_ok()
  178. * will return 0, so do this first.
  179. */
  180. mfs = fc->real_dev->mtu - (sizeof(struct fcoe_hdr) +
  181. sizeof(struct fcoe_crc_eof));
  182. if (fc_set_mfs(lp, mfs))
  183. return -EINVAL;
  184. /* offload features support */
  185. if (fc->real_dev->features & NETIF_F_SG)
  186. lp->sg_supp = 1;
  187. #ifdef NETIF_F_FCOE_CRC
  188. if (netdev->features & NETIF_F_FCOE_CRC) {
  189. lp->crc_offload = 1;
  190. printk(KERN_DEBUG "fcoe:%s supports FCCRC offload\n",
  191. netdev->name);
  192. }
  193. #endif
  194. #ifdef NETIF_F_FSO
  195. if (netdev->features & NETIF_F_FSO) {
  196. lp->seq_offload = 1;
  197. lp->lso_max = netdev->gso_max_size;
  198. printk(KERN_DEBUG "fcoe:%s supports LSO for max len 0x%x\n",
  199. netdev->name, lp->lso_max);
  200. }
  201. #endif
  202. if (netdev->fcoe_ddp_xid) {
  203. lp->lro_enabled = 1;
  204. lp->lro_xid = netdev->fcoe_ddp_xid;
  205. printk(KERN_DEBUG "fcoe:%s supports LRO for max xid 0x%x\n",
  206. netdev->name, lp->lro_xid);
  207. }
  208. skb_queue_head_init(&fc->fcoe_pending_queue);
  209. fc->fcoe_pending_queue_active = 0;
  210. /* setup Source Mac Address */
  211. memcpy(fc->ctlr.ctl_src_addr, fc->real_dev->dev_addr,
  212. fc->real_dev->addr_len);
  213. wwnn = fcoe_wwn_from_mac(fc->real_dev->dev_addr, 1, 0);
  214. fc_set_wwnn(lp, wwnn);
  215. /* XXX - 3rd arg needs to be vlan id */
  216. wwpn = fcoe_wwn_from_mac(fc->real_dev->dev_addr, 2, 0);
  217. fc_set_wwpn(lp, wwpn);
  218. /*
  219. * Add FCoE MAC address as second unicast MAC address
  220. * or enter promiscuous mode if not capable of listening
  221. * for multiple unicast MACs.
  222. */
  223. rtnl_lock();
  224. memcpy(flogi_maddr, (u8[6]) FC_FCOE_FLOGI_MAC, ETH_ALEN);
  225. dev_unicast_add(fc->real_dev, flogi_maddr, ETH_ALEN);
  226. dev_mc_add(fc->real_dev, FIP_ALL_ENODE_MACS, ETH_ALEN, 0);
  227. rtnl_unlock();
  228. /*
  229. * setup the receive function from ethernet driver
  230. * on the ethertype for the given device
  231. */
  232. fc->fcoe_packet_type.func = fcoe_rcv;
  233. fc->fcoe_packet_type.type = __constant_htons(ETH_P_FCOE);
  234. fc->fcoe_packet_type.dev = fc->real_dev;
  235. dev_add_pack(&fc->fcoe_packet_type);
  236. return 0;
  237. }
  238. /**
  239. * fcoe_shost_config() - Sets up fc_lport->host
  240. * @lp : ptr to the fc_lport
  241. * @shost : ptr to the associated scsi host
  242. * @dev : device associated to scsi host
  243. *
  244. * Must be called after fcoe_lport_config() and fcoe_netdev_config()
  245. *
  246. * Returns : 0 for success
  247. */
  248. static int fcoe_shost_config(struct fc_lport *lp, struct Scsi_Host *shost,
  249. struct device *dev)
  250. {
  251. int rc = 0;
  252. /* lport scsi host config */
  253. lp->host = shost;
  254. lp->host->max_lun = FCOE_MAX_LUN;
  255. lp->host->max_id = FCOE_MAX_FCP_TARGET;
  256. lp->host->max_channel = 0;
  257. lp->host->transportt = scsi_transport_fcoe_sw;
  258. /* add the new host to the SCSI-ml */
  259. rc = scsi_add_host(lp->host, dev);
  260. if (rc) {
  261. FC_DBG("fcoe_shost_config:error on scsi_add_host\n");
  262. return rc;
  263. }
  264. sprintf(fc_host_symbolic_name(lp->host), "%s v%s over %s",
  265. FCOE_NAME, FCOE_VERSION,
  266. fcoe_netdev(lp)->name);
  267. return 0;
  268. }
  269. /**
  270. * fcoe_em_config() - allocates em for this lport
  271. * @lp: the port that em is to allocated for
  272. *
  273. * Returns : 0 on success
  274. */
  275. static inline int fcoe_em_config(struct fc_lport *lp)
  276. {
  277. BUG_ON(lp->emp);
  278. lp->emp = fc_exch_mgr_alloc(lp, FC_CLASS_3,
  279. FCOE_MIN_XID, FCOE_MAX_XID);
  280. if (!lp->emp)
  281. return -ENOMEM;
  282. return 0;
  283. }
  284. /**
  285. * fcoe_if_destroy() - FCoE software HBA tear-down function
  286. * @netdev: ptr to the associated net_device
  287. *
  288. * Returns: 0 if link is OK for use by FCoE.
  289. */
  290. static int fcoe_if_destroy(struct net_device *netdev)
  291. {
  292. struct fc_lport *lp = NULL;
  293. struct fcoe_softc *fc;
  294. u8 flogi_maddr[ETH_ALEN];
  295. BUG_ON(!netdev);
  296. printk(KERN_DEBUG "fcoe_if_destroy:interface on %s\n",
  297. netdev->name);
  298. lp = fcoe_hostlist_lookup(netdev);
  299. if (!lp)
  300. return -ENODEV;
  301. fc = lport_priv(lp);
  302. /* Logout of the fabric */
  303. fc_fabric_logoff(lp);
  304. /* Remove the instance from fcoe's list */
  305. fcoe_hostlist_remove(lp);
  306. /* Don't listen for Ethernet packets anymore */
  307. dev_remove_pack(&fc->fcoe_packet_type);
  308. dev_remove_pack(&fc->fip_packet_type);
  309. fcoe_ctlr_destroy(&fc->ctlr);
  310. /* Cleanup the fc_lport */
  311. fc_lport_destroy(lp);
  312. fc_fcp_destroy(lp);
  313. /* Detach from the scsi-ml */
  314. fc_remove_host(lp->host);
  315. scsi_remove_host(lp->host);
  316. /* There are no more rports or I/O, free the EM */
  317. if (lp->emp)
  318. fc_exch_mgr_free(lp->emp);
  319. /* Delete secondary MAC addresses */
  320. rtnl_lock();
  321. memcpy(flogi_maddr, (u8[6]) FC_FCOE_FLOGI_MAC, ETH_ALEN);
  322. dev_unicast_delete(fc->real_dev, flogi_maddr, ETH_ALEN);
  323. if (!is_zero_ether_addr(fc->ctlr.data_src_addr))
  324. dev_unicast_delete(fc->real_dev,
  325. fc->ctlr.data_src_addr, ETH_ALEN);
  326. dev_mc_delete(fc->real_dev, FIP_ALL_ENODE_MACS, ETH_ALEN, 0);
  327. rtnl_unlock();
  328. /* Free the per-CPU receive threads */
  329. fcoe_percpu_clean(lp);
  330. /* Free existing skbs */
  331. fcoe_clean_pending_queue(lp);
  332. /* Free memory used by statistical counters */
  333. fc_lport_free_stats(lp);
  334. /* Release the net_device and Scsi_Host */
  335. dev_put(fc->real_dev);
  336. scsi_host_put(lp->host);
  337. return 0;
  338. }
  339. /*
  340. * fcoe_ddp_setup - calls LLD's ddp_setup through net_device
  341. * @lp: the corresponding fc_lport
  342. * @xid: the exchange id for this ddp transfer
  343. * @sgl: the scatterlist describing this transfer
  344. * @sgc: number of sg items
  345. *
  346. * Returns : 0 no ddp
  347. */
  348. static int fcoe_ddp_setup(struct fc_lport *lp, u16 xid,
  349. struct scatterlist *sgl, unsigned int sgc)
  350. {
  351. struct net_device *n = fcoe_netdev(lp);
  352. if (n->netdev_ops && n->netdev_ops->ndo_fcoe_ddp_setup)
  353. return n->netdev_ops->ndo_fcoe_ddp_setup(n, xid, sgl, sgc);
  354. return 0;
  355. }
  356. /*
  357. * fcoe_ddp_done - calls LLD's ddp_done through net_device
  358. * @lp: the corresponding fc_lport
  359. * @xid: the exchange id for this ddp transfer
  360. *
  361. * Returns : the length of data that have been completed by ddp
  362. */
  363. static int fcoe_ddp_done(struct fc_lport *lp, u16 xid)
  364. {
  365. struct net_device *n = fcoe_netdev(lp);
  366. if (n->netdev_ops && n->netdev_ops->ndo_fcoe_ddp_done)
  367. return n->netdev_ops->ndo_fcoe_ddp_done(n, xid);
  368. return 0;
  369. }
  370. static struct libfc_function_template fcoe_libfc_fcn_templ = {
  371. .frame_send = fcoe_xmit,
  372. .ddp_setup = fcoe_ddp_setup,
  373. .ddp_done = fcoe_ddp_done,
  374. };
  375. /**
  376. * fcoe_fip_recv - handle a received FIP frame.
  377. * @skb: the receive skb
  378. * @dev: associated &net_device
  379. * @ptype: the &packet_type structure which was used to register this handler.
  380. * @orig_dev: original receive &net_device, in case @dev is a bond.
  381. *
  382. * Returns: 0 for success
  383. */
  384. static int fcoe_fip_recv(struct sk_buff *skb, struct net_device *dev,
  385. struct packet_type *ptype,
  386. struct net_device *orig_dev)
  387. {
  388. struct fcoe_softc *fc;
  389. fc = container_of(ptype, struct fcoe_softc, fip_packet_type);
  390. fcoe_ctlr_recv(&fc->ctlr, skb);
  391. return 0;
  392. }
  393. /**
  394. * fcoe_fip_send() - send an Ethernet-encapsulated FIP frame.
  395. * @fip: FCoE controller.
  396. * @skb: FIP Packet.
  397. */
  398. static void fcoe_fip_send(struct fcoe_ctlr *fip, struct sk_buff *skb)
  399. {
  400. skb->dev = fcoe_from_ctlr(fip)->real_dev;
  401. dev_queue_xmit(skb);
  402. }
  403. /**
  404. * fcoe_update_src_mac() - Update Ethernet MAC filters.
  405. * @fip: FCoE controller.
  406. * @old: Unicast MAC address to delete if the MAC is non-zero.
  407. * @new: Unicast MAC address to add.
  408. *
  409. * Remove any previously-set unicast MAC filter.
  410. * Add secondary FCoE MAC address filter for our OUI.
  411. */
  412. static void fcoe_update_src_mac(struct fcoe_ctlr *fip, u8 *old, u8 *new)
  413. {
  414. struct fcoe_softc *fc;
  415. fc = fcoe_from_ctlr(fip);
  416. rtnl_lock();
  417. if (!is_zero_ether_addr(old))
  418. dev_unicast_delete(fc->real_dev, old, ETH_ALEN);
  419. dev_unicast_add(fc->real_dev, new, ETH_ALEN);
  420. rtnl_unlock();
  421. }
  422. /**
  423. * fcoe_if_create() - this function creates the fcoe interface
  424. * @netdev: pointer the associated netdevice
  425. *
  426. * Creates fc_lport struct and scsi_host for lport, configures lport
  427. * and starts fabric login.
  428. *
  429. * Returns : 0 on success
  430. */
  431. static int fcoe_if_create(struct net_device *netdev)
  432. {
  433. int rc;
  434. struct fc_lport *lp = NULL;
  435. struct fcoe_softc *fc;
  436. struct Scsi_Host *shost;
  437. BUG_ON(!netdev);
  438. printk(KERN_DEBUG "fcoe_if_create:interface on %s\n",
  439. netdev->name);
  440. lp = fcoe_hostlist_lookup(netdev);
  441. if (lp)
  442. return -EEXIST;
  443. shost = libfc_host_alloc(&fcoe_shost_template,
  444. sizeof(struct fcoe_softc));
  445. if (!shost) {
  446. FC_DBG("Could not allocate host structure\n");
  447. return -ENOMEM;
  448. }
  449. lp = shost_priv(shost);
  450. fc = lport_priv(lp);
  451. /* configure fc_lport, e.g., em */
  452. rc = fcoe_lport_config(lp);
  453. if (rc) {
  454. FC_DBG("Could not configure lport\n");
  455. goto out_host_put;
  456. }
  457. /* configure lport network properties */
  458. rc = fcoe_netdev_config(lp, netdev);
  459. if (rc) {
  460. FC_DBG("Could not configure netdev for lport\n");
  461. goto out_host_put;
  462. }
  463. /*
  464. * Initialize FIP.
  465. */
  466. fcoe_ctlr_init(&fc->ctlr);
  467. fc->ctlr.send = fcoe_fip_send;
  468. fc->ctlr.update_mac = fcoe_update_src_mac;
  469. fc->fip_packet_type.func = fcoe_fip_recv;
  470. fc->fip_packet_type.type = htons(ETH_P_FIP);
  471. fc->fip_packet_type.dev = fc->real_dev;
  472. dev_add_pack(&fc->fip_packet_type);
  473. /* configure lport scsi host properties */
  474. rc = fcoe_shost_config(lp, shost, &netdev->dev);
  475. if (rc) {
  476. FC_DBG("Could not configure shost for lport\n");
  477. goto out_host_put;
  478. }
  479. /* lport exch manager allocation */
  480. rc = fcoe_em_config(lp);
  481. if (rc) {
  482. FC_DBG("Could not configure em for lport\n");
  483. goto out_host_put;
  484. }
  485. /* Initialize the library */
  486. rc = fcoe_libfc_config(lp, &fcoe_libfc_fcn_templ);
  487. if (rc) {
  488. FC_DBG("Could not configure libfc for lport!\n");
  489. goto out_lp_destroy;
  490. }
  491. /* add to lports list */
  492. fcoe_hostlist_add(lp);
  493. lp->boot_time = jiffies;
  494. fc_fabric_login(lp);
  495. if (!fcoe_link_ok(lp))
  496. fcoe_ctlr_link_up(&fc->ctlr);
  497. dev_hold(netdev);
  498. return rc;
  499. out_lp_destroy:
  500. fc_exch_mgr_free(lp->emp); /* Free the EM */
  501. out_host_put:
  502. scsi_host_put(lp->host);
  503. return rc;
  504. }
  505. /**
  506. * fcoe_if_init() - attach to scsi transport
  507. *
  508. * Returns : 0 on success
  509. */
  510. static int __init fcoe_if_init(void)
  511. {
  512. /* attach to scsi transport */
  513. scsi_transport_fcoe_sw =
  514. fc_attach_transport(&fcoe_transport_function);
  515. if (!scsi_transport_fcoe_sw) {
  516. printk(KERN_ERR "fcoe_init:fc_attach_transport() failed\n");
  517. return -ENODEV;
  518. }
  519. return 0;
  520. }
  521. /**
  522. * fcoe_if_exit() - detach from scsi transport
  523. *
  524. * Returns : 0 on success
  525. */
  526. int __exit fcoe_if_exit(void)
  527. {
  528. fc_release_transport(scsi_transport_fcoe_sw);
  529. return 0;
  530. }
  531. /**
  532. * fcoe_percpu_thread_create() - Create a receive thread for an online cpu
  533. * @cpu: cpu index for the online cpu
  534. */
  535. static void fcoe_percpu_thread_create(unsigned int cpu)
  536. {
  537. struct fcoe_percpu_s *p;
  538. struct task_struct *thread;
  539. p = &per_cpu(fcoe_percpu, cpu);
  540. thread = kthread_create(fcoe_percpu_receive_thread,
  541. (void *)p, "fcoethread/%d", cpu);
  542. if (likely(!IS_ERR(p->thread))) {
  543. kthread_bind(thread, cpu);
  544. wake_up_process(thread);
  545. spin_lock_bh(&p->fcoe_rx_list.lock);
  546. p->thread = thread;
  547. spin_unlock_bh(&p->fcoe_rx_list.lock);
  548. }
  549. }
  550. /**
  551. * fcoe_percpu_thread_destroy() - removes the rx thread for the given cpu
  552. * @cpu: cpu index the rx thread is to be removed
  553. *
  554. * Destroys a per-CPU Rx thread. Any pending skbs are moved to the
  555. * current CPU's Rx thread. If the thread being destroyed is bound to
  556. * the CPU processing this context the skbs will be freed.
  557. */
  558. static void fcoe_percpu_thread_destroy(unsigned int cpu)
  559. {
  560. struct fcoe_percpu_s *p;
  561. struct task_struct *thread;
  562. struct page *crc_eof;
  563. struct sk_buff *skb;
  564. #ifdef CONFIG_SMP
  565. struct fcoe_percpu_s *p0;
  566. unsigned targ_cpu = smp_processor_id();
  567. #endif /* CONFIG_SMP */
  568. printk(KERN_DEBUG "fcoe: Destroying receive thread for CPU %d\n", cpu);
  569. /* Prevent any new skbs from being queued for this CPU. */
  570. p = &per_cpu(fcoe_percpu, cpu);
  571. spin_lock_bh(&p->fcoe_rx_list.lock);
  572. thread = p->thread;
  573. p->thread = NULL;
  574. crc_eof = p->crc_eof_page;
  575. p->crc_eof_page = NULL;
  576. p->crc_eof_offset = 0;
  577. spin_unlock_bh(&p->fcoe_rx_list.lock);
  578. #ifdef CONFIG_SMP
  579. /*
  580. * Don't bother moving the skb's if this context is running
  581. * on the same CPU that is having its thread destroyed. This
  582. * can easily happen when the module is removed.
  583. */
  584. if (cpu != targ_cpu) {
  585. p0 = &per_cpu(fcoe_percpu, targ_cpu);
  586. spin_lock_bh(&p0->fcoe_rx_list.lock);
  587. if (p0->thread) {
  588. FC_DBG("Moving frames from CPU %d to CPU %d\n",
  589. cpu, targ_cpu);
  590. while ((skb = __skb_dequeue(&p->fcoe_rx_list)) != NULL)
  591. __skb_queue_tail(&p0->fcoe_rx_list, skb);
  592. spin_unlock_bh(&p0->fcoe_rx_list.lock);
  593. } else {
  594. /*
  595. * The targeted CPU is not initialized and cannot accept
  596. * new skbs. Unlock the targeted CPU and drop the skbs
  597. * on the CPU that is going offline.
  598. */
  599. while ((skb = __skb_dequeue(&p->fcoe_rx_list)) != NULL)
  600. kfree_skb(skb);
  601. spin_unlock_bh(&p0->fcoe_rx_list.lock);
  602. }
  603. } else {
  604. /*
  605. * This scenario occurs when the module is being removed
  606. * and all threads are being destroyed. skbs will continue
  607. * to be shifted from the CPU thread that is being removed
  608. * to the CPU thread associated with the CPU that is processing
  609. * the module removal. Once there is only one CPU Rx thread it
  610. * will reach this case and we will drop all skbs and later
  611. * stop the thread.
  612. */
  613. spin_lock_bh(&p->fcoe_rx_list.lock);
  614. while ((skb = __skb_dequeue(&p->fcoe_rx_list)) != NULL)
  615. kfree_skb(skb);
  616. spin_unlock_bh(&p->fcoe_rx_list.lock);
  617. }
  618. #else
  619. /*
  620. * This a non-SMP scenario where the singular Rx thread is
  621. * being removed. Free all skbs and stop the thread.
  622. */
  623. spin_lock_bh(&p->fcoe_rx_list.lock);
  624. while ((skb = __skb_dequeue(&p->fcoe_rx_list)) != NULL)
  625. kfree_skb(skb);
  626. spin_unlock_bh(&p->fcoe_rx_list.lock);
  627. #endif
  628. if (thread)
  629. kthread_stop(thread);
  630. if (crc_eof)
  631. put_page(crc_eof);
  632. }
  633. /**
  634. * fcoe_cpu_callback() - fcoe cpu hotplug event callback
  635. * @nfb: callback data block
  636. * @action: event triggering the callback
  637. * @hcpu: index for the cpu of this event
  638. *
  639. * This creates or destroys per cpu data for fcoe
  640. *
  641. * Returns NOTIFY_OK always.
  642. */
  643. static int fcoe_cpu_callback(struct notifier_block *nfb,
  644. unsigned long action, void *hcpu)
  645. {
  646. unsigned cpu = (unsigned long)hcpu;
  647. switch (action) {
  648. case CPU_ONLINE:
  649. case CPU_ONLINE_FROZEN:
  650. FC_DBG("CPU %x online: Create Rx thread\n", cpu);
  651. fcoe_percpu_thread_create(cpu);
  652. break;
  653. case CPU_DEAD:
  654. case CPU_DEAD_FROZEN:
  655. FC_DBG("CPU %x offline: Remove Rx thread\n", cpu);
  656. fcoe_percpu_thread_destroy(cpu);
  657. break;
  658. default:
  659. break;
  660. }
  661. return NOTIFY_OK;
  662. }
  663. static struct notifier_block fcoe_cpu_notifier = {
  664. .notifier_call = fcoe_cpu_callback,
  665. };
  666. /**
  667. * fcoe_rcv() - this is the fcoe receive function called by NET_RX_SOFTIRQ
  668. * @skb: the receive skb
  669. * @dev: associated net device
  670. * @ptype: context
  671. * @olddev: last device
  672. *
  673. * this function will receive the packet and build fc frame and pass it up
  674. *
  675. * Returns: 0 for success
  676. */
  677. int fcoe_rcv(struct sk_buff *skb, struct net_device *dev,
  678. struct packet_type *ptype, struct net_device *olddev)
  679. {
  680. struct fc_lport *lp;
  681. struct fcoe_rcv_info *fr;
  682. struct fcoe_softc *fc;
  683. struct fc_frame_header *fh;
  684. struct fcoe_percpu_s *fps;
  685. unsigned short oxid;
  686. unsigned int cpu = 0;
  687. fc = container_of(ptype, struct fcoe_softc, fcoe_packet_type);
  688. lp = fc->ctlr.lp;
  689. if (unlikely(lp == NULL)) {
  690. FC_DBG("cannot find hba structure");
  691. goto err2;
  692. }
  693. if (!lp->link_up)
  694. goto err2;
  695. if (unlikely(debug_fcoe)) {
  696. FC_DBG("skb_info: len:%d data_len:%d head:%p data:%p tail:%p "
  697. "end:%p sum:%d dev:%s", skb->len, skb->data_len,
  698. skb->head, skb->data, skb_tail_pointer(skb),
  699. skb_end_pointer(skb), skb->csum,
  700. skb->dev ? skb->dev->name : "<NULL>");
  701. }
  702. /* check for FCOE packet type */
  703. if (unlikely(eth_hdr(skb)->h_proto != htons(ETH_P_FCOE))) {
  704. FC_DBG("wrong FC type frame");
  705. goto err;
  706. }
  707. /*
  708. * Check for minimum frame length, and make sure required FCoE
  709. * and FC headers are pulled into the linear data area.
  710. */
  711. if (unlikely((skb->len < FCOE_MIN_FRAME) ||
  712. !pskb_may_pull(skb, FCOE_HEADER_LEN)))
  713. goto err;
  714. skb_set_transport_header(skb, sizeof(struct fcoe_hdr));
  715. fh = (struct fc_frame_header *) skb_transport_header(skb);
  716. oxid = ntohs(fh->fh_ox_id);
  717. fr = fcoe_dev_from_skb(skb);
  718. fr->fr_dev = lp;
  719. fr->ptype = ptype;
  720. #ifdef CONFIG_SMP
  721. /*
  722. * The incoming frame exchange id(oxid) is ANDed with num of online
  723. * cpu bits to get cpu and then this cpu is used for selecting
  724. * a per cpu kernel thread from fcoe_percpu.
  725. */
  726. cpu = oxid & (num_online_cpus() - 1);
  727. #endif
  728. fps = &per_cpu(fcoe_percpu, cpu);
  729. spin_lock_bh(&fps->fcoe_rx_list.lock);
  730. if (unlikely(!fps->thread)) {
  731. /*
  732. * The targeted CPU is not ready, let's target
  733. * the first CPU now. For non-SMP systems this
  734. * will check the same CPU twice.
  735. */
  736. FC_DBG("CPU is online, but no receive thread ready "
  737. "for incoming skb- using first online CPU.\n");
  738. spin_unlock_bh(&fps->fcoe_rx_list.lock);
  739. cpu = first_cpu(cpu_online_map);
  740. fps = &per_cpu(fcoe_percpu, cpu);
  741. spin_lock_bh(&fps->fcoe_rx_list.lock);
  742. if (!fps->thread) {
  743. spin_unlock_bh(&fps->fcoe_rx_list.lock);
  744. goto err;
  745. }
  746. }
  747. /*
  748. * We now have a valid CPU that we're targeting for
  749. * this skb. We also have this receive thread locked,
  750. * so we're free to queue skbs into it's queue.
  751. */
  752. __skb_queue_tail(&fps->fcoe_rx_list, skb);
  753. if (fps->fcoe_rx_list.qlen == 1)
  754. wake_up_process(fps->thread);
  755. spin_unlock_bh(&fps->fcoe_rx_list.lock);
  756. return 0;
  757. err:
  758. fc_lport_get_stats(lp)->ErrorFrames++;
  759. err2:
  760. kfree_skb(skb);
  761. return -1;
  762. }
  763. /**
  764. * fcoe_start_io() - pass to netdev to start xmit for fcoe
  765. * @skb: the skb to be xmitted
  766. *
  767. * Returns: 0 for success
  768. */
  769. static inline int fcoe_start_io(struct sk_buff *skb)
  770. {
  771. int rc;
  772. skb_get(skb);
  773. rc = dev_queue_xmit(skb);
  774. if (rc != 0)
  775. return rc;
  776. kfree_skb(skb);
  777. return 0;
  778. }
  779. /**
  780. * fcoe_get_paged_crc_eof() - in case we need to alloc a page for crc_eof
  781. * @skb: the skb to be xmitted
  782. * @tlen: total len
  783. *
  784. * Returns: 0 for success
  785. */
  786. static int fcoe_get_paged_crc_eof(struct sk_buff *skb, int tlen)
  787. {
  788. struct fcoe_percpu_s *fps;
  789. struct page *page;
  790. fps = &get_cpu_var(fcoe_percpu);
  791. page = fps->crc_eof_page;
  792. if (!page) {
  793. page = alloc_page(GFP_ATOMIC);
  794. if (!page) {
  795. put_cpu_var(fcoe_percpu);
  796. return -ENOMEM;
  797. }
  798. fps->crc_eof_page = page;
  799. fps->crc_eof_offset = 0;
  800. }
  801. get_page(page);
  802. skb_fill_page_desc(skb, skb_shinfo(skb)->nr_frags, page,
  803. fps->crc_eof_offset, tlen);
  804. skb->len += tlen;
  805. skb->data_len += tlen;
  806. skb->truesize += tlen;
  807. fps->crc_eof_offset += sizeof(struct fcoe_crc_eof);
  808. if (fps->crc_eof_offset >= PAGE_SIZE) {
  809. fps->crc_eof_page = NULL;
  810. fps->crc_eof_offset = 0;
  811. put_page(page);
  812. }
  813. put_cpu_var(fcoe_percpu);
  814. return 0;
  815. }
  816. /**
  817. * fcoe_fc_crc() - calculates FC CRC in this fcoe skb
  818. * @fp: the fc_frame containing data to be checksummed
  819. *
  820. * This uses crc32() to calculate the crc for fc frame
  821. * Return : 32 bit crc
  822. */
  823. u32 fcoe_fc_crc(struct fc_frame *fp)
  824. {
  825. struct sk_buff *skb = fp_skb(fp);
  826. struct skb_frag_struct *frag;
  827. unsigned char *data;
  828. unsigned long off, len, clen;
  829. u32 crc;
  830. unsigned i;
  831. crc = crc32(~0, skb->data, skb_headlen(skb));
  832. for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
  833. frag = &skb_shinfo(skb)->frags[i];
  834. off = frag->page_offset;
  835. len = frag->size;
  836. while (len > 0) {
  837. clen = min(len, PAGE_SIZE - (off & ~PAGE_MASK));
  838. data = kmap_atomic(frag->page + (off >> PAGE_SHIFT),
  839. KM_SKB_DATA_SOFTIRQ);
  840. crc = crc32(crc, data + (off & ~PAGE_MASK), clen);
  841. kunmap_atomic(data, KM_SKB_DATA_SOFTIRQ);
  842. off += clen;
  843. len -= clen;
  844. }
  845. }
  846. return crc;
  847. }
  848. /**
  849. * fcoe_xmit() - FCoE frame transmit function
  850. * @lp: the associated local port
  851. * @fp: the fc_frame to be transmitted
  852. *
  853. * Return : 0 for success
  854. */
  855. int fcoe_xmit(struct fc_lport *lp, struct fc_frame *fp)
  856. {
  857. int wlen, rc = 0;
  858. u32 crc;
  859. struct ethhdr *eh;
  860. struct fcoe_crc_eof *cp;
  861. struct sk_buff *skb;
  862. struct fcoe_dev_stats *stats;
  863. struct fc_frame_header *fh;
  864. unsigned int hlen; /* header length implies the version */
  865. unsigned int tlen; /* trailer length */
  866. unsigned int elen; /* eth header, may include vlan */
  867. struct fcoe_softc *fc;
  868. u8 sof, eof;
  869. struct fcoe_hdr *hp;
  870. WARN_ON((fr_len(fp) % sizeof(u32)) != 0);
  871. fc = lport_priv(lp);
  872. fh = fc_frame_header_get(fp);
  873. skb = fp_skb(fp);
  874. wlen = skb->len / FCOE_WORD_TO_BYTE;
  875. if (!lp->link_up) {
  876. kfree_skb(skb);
  877. return 0;
  878. }
  879. if (unlikely(fh->fh_r_ctl == FC_RCTL_ELS_REQ) &&
  880. fcoe_ctlr_els_send(&fc->ctlr, skb))
  881. return 0;
  882. sof = fr_sof(fp);
  883. eof = fr_eof(fp);
  884. elen = (fc->real_dev->priv_flags & IFF_802_1Q_VLAN) ?
  885. sizeof(struct vlan_ethhdr) : sizeof(struct ethhdr);
  886. hlen = sizeof(struct fcoe_hdr);
  887. tlen = sizeof(struct fcoe_crc_eof);
  888. wlen = (skb->len - tlen + sizeof(crc)) / FCOE_WORD_TO_BYTE;
  889. /* crc offload */
  890. if (likely(lp->crc_offload)) {
  891. skb->ip_summed = CHECKSUM_PARTIAL;
  892. skb->csum_start = skb_headroom(skb);
  893. skb->csum_offset = skb->len;
  894. crc = 0;
  895. } else {
  896. skb->ip_summed = CHECKSUM_NONE;
  897. crc = fcoe_fc_crc(fp);
  898. }
  899. /* copy fc crc and eof to the skb buff */
  900. if (skb_is_nonlinear(skb)) {
  901. skb_frag_t *frag;
  902. if (fcoe_get_paged_crc_eof(skb, tlen)) {
  903. kfree_skb(skb);
  904. return -ENOMEM;
  905. }
  906. frag = &skb_shinfo(skb)->frags[skb_shinfo(skb)->nr_frags - 1];
  907. cp = kmap_atomic(frag->page, KM_SKB_DATA_SOFTIRQ)
  908. + frag->page_offset;
  909. } else {
  910. cp = (struct fcoe_crc_eof *)skb_put(skb, tlen);
  911. }
  912. memset(cp, 0, sizeof(*cp));
  913. cp->fcoe_eof = eof;
  914. cp->fcoe_crc32 = cpu_to_le32(~crc);
  915. if (skb_is_nonlinear(skb)) {
  916. kunmap_atomic(cp, KM_SKB_DATA_SOFTIRQ);
  917. cp = NULL;
  918. }
  919. /* adjust skb network/transport offsets to match mac/fcoe/fc */
  920. skb_push(skb, elen + hlen);
  921. skb_reset_mac_header(skb);
  922. skb_reset_network_header(skb);
  923. skb->mac_len = elen;
  924. skb->protocol = htons(ETH_P_FCOE);
  925. skb->dev = fc->real_dev;
  926. /* fill up mac and fcoe headers */
  927. eh = eth_hdr(skb);
  928. eh->h_proto = htons(ETH_P_FCOE);
  929. if (fc->ctlr.map_dest)
  930. fc_fcoe_set_mac(eh->h_dest, fh->fh_d_id);
  931. else
  932. /* insert GW address */
  933. memcpy(eh->h_dest, fc->ctlr.dest_addr, ETH_ALEN);
  934. if (unlikely(fc->ctlr.flogi_oxid != FC_XID_UNKNOWN))
  935. memcpy(eh->h_source, fc->ctlr.ctl_src_addr, ETH_ALEN);
  936. else
  937. memcpy(eh->h_source, fc->ctlr.data_src_addr, ETH_ALEN);
  938. hp = (struct fcoe_hdr *)(eh + 1);
  939. memset(hp, 0, sizeof(*hp));
  940. if (FC_FCOE_VER)
  941. FC_FCOE_ENCAPS_VER(hp, FC_FCOE_VER);
  942. hp->fcoe_sof = sof;
  943. #ifdef NETIF_F_FSO
  944. /* fcoe lso, mss is in max_payload which is non-zero for FCP data */
  945. if (lp->seq_offload && fr_max_payload(fp)) {
  946. skb_shinfo(skb)->gso_type = SKB_GSO_FCOE;
  947. skb_shinfo(skb)->gso_size = fr_max_payload(fp);
  948. } else {
  949. skb_shinfo(skb)->gso_type = 0;
  950. skb_shinfo(skb)->gso_size = 0;
  951. }
  952. #endif
  953. /* update tx stats: regardless if LLD fails */
  954. stats = fc_lport_get_stats(lp);
  955. stats->TxFrames++;
  956. stats->TxWords += wlen;
  957. /* send down to lld */
  958. fr_dev(fp) = lp;
  959. if (fc->fcoe_pending_queue.qlen)
  960. rc = fcoe_check_wait_queue(lp);
  961. if (rc == 0)
  962. rc = fcoe_start_io(skb);
  963. if (rc) {
  964. spin_lock_bh(&fc->fcoe_pending_queue.lock);
  965. __skb_queue_tail(&fc->fcoe_pending_queue, skb);
  966. spin_unlock_bh(&fc->fcoe_pending_queue.lock);
  967. if (fc->fcoe_pending_queue.qlen > FCOE_MAX_QUEUE_DEPTH)
  968. lp->qfull = 1;
  969. }
  970. return 0;
  971. }
  972. /**
  973. * fcoe_percpu_receive_thread() - recv thread per cpu
  974. * @arg: ptr to the fcoe per cpu struct
  975. *
  976. * Return: 0 for success
  977. */
  978. int fcoe_percpu_receive_thread(void *arg)
  979. {
  980. struct fcoe_percpu_s *p = arg;
  981. u32 fr_len;
  982. struct fc_lport *lp;
  983. struct fcoe_rcv_info *fr;
  984. struct fcoe_dev_stats *stats;
  985. struct fc_frame_header *fh;
  986. struct sk_buff *skb;
  987. struct fcoe_crc_eof crc_eof;
  988. struct fc_frame *fp;
  989. u8 *mac = NULL;
  990. struct fcoe_softc *fc;
  991. struct fcoe_hdr *hp;
  992. set_user_nice(current, -20);
  993. while (!kthread_should_stop()) {
  994. spin_lock_bh(&p->fcoe_rx_list.lock);
  995. while ((skb = __skb_dequeue(&p->fcoe_rx_list)) == NULL) {
  996. set_current_state(TASK_INTERRUPTIBLE);
  997. spin_unlock_bh(&p->fcoe_rx_list.lock);
  998. schedule();
  999. set_current_state(TASK_RUNNING);
  1000. if (kthread_should_stop())
  1001. return 0;
  1002. spin_lock_bh(&p->fcoe_rx_list.lock);
  1003. }
  1004. spin_unlock_bh(&p->fcoe_rx_list.lock);
  1005. fr = fcoe_dev_from_skb(skb);
  1006. lp = fr->fr_dev;
  1007. if (unlikely(lp == NULL)) {
  1008. FC_DBG("invalid HBA Structure");
  1009. kfree_skb(skb);
  1010. continue;
  1011. }
  1012. if (unlikely(debug_fcoe)) {
  1013. FC_DBG("skb_info: len:%d data_len:%d head:%p data:%p "
  1014. "tail:%p end:%p sum:%d dev:%s",
  1015. skb->len, skb->data_len,
  1016. skb->head, skb->data, skb_tail_pointer(skb),
  1017. skb_end_pointer(skb), skb->csum,
  1018. skb->dev ? skb->dev->name : "<NULL>");
  1019. }
  1020. /*
  1021. * Save source MAC address before discarding header.
  1022. */
  1023. fc = lport_priv(lp);
  1024. if (skb_is_nonlinear(skb))
  1025. skb_linearize(skb); /* not ideal */
  1026. mac = eth_hdr(skb)->h_source;
  1027. /*
  1028. * Frame length checks and setting up the header pointers
  1029. * was done in fcoe_rcv already.
  1030. */
  1031. hp = (struct fcoe_hdr *) skb_network_header(skb);
  1032. fh = (struct fc_frame_header *) skb_transport_header(skb);
  1033. stats = fc_lport_get_stats(lp);
  1034. if (unlikely(FC_FCOE_DECAPS_VER(hp) != FC_FCOE_VER)) {
  1035. if (stats->ErrorFrames < 5)
  1036. printk(KERN_WARNING "FCoE version "
  1037. "mismatch: The frame has "
  1038. "version %x, but the "
  1039. "initiator supports version "
  1040. "%x\n", FC_FCOE_DECAPS_VER(hp),
  1041. FC_FCOE_VER);
  1042. stats->ErrorFrames++;
  1043. kfree_skb(skb);
  1044. continue;
  1045. }
  1046. skb_pull(skb, sizeof(struct fcoe_hdr));
  1047. fr_len = skb->len - sizeof(struct fcoe_crc_eof);
  1048. stats->RxFrames++;
  1049. stats->RxWords += fr_len / FCOE_WORD_TO_BYTE;
  1050. fp = (struct fc_frame *)skb;
  1051. fc_frame_init(fp);
  1052. fr_dev(fp) = lp;
  1053. fr_sof(fp) = hp->fcoe_sof;
  1054. /* Copy out the CRC and EOF trailer for access */
  1055. if (skb_copy_bits(skb, fr_len, &crc_eof, sizeof(crc_eof))) {
  1056. kfree_skb(skb);
  1057. continue;
  1058. }
  1059. fr_eof(fp) = crc_eof.fcoe_eof;
  1060. fr_crc(fp) = crc_eof.fcoe_crc32;
  1061. if (pskb_trim(skb, fr_len)) {
  1062. kfree_skb(skb);
  1063. continue;
  1064. }
  1065. /*
  1066. * We only check CRC if no offload is available and if it is
  1067. * it's solicited data, in which case, the FCP layer would
  1068. * check it during the copy.
  1069. */
  1070. if (lp->crc_offload && skb->ip_summed == CHECKSUM_UNNECESSARY)
  1071. fr_flags(fp) &= ~FCPHF_CRC_UNCHECKED;
  1072. else
  1073. fr_flags(fp) |= FCPHF_CRC_UNCHECKED;
  1074. fh = fc_frame_header_get(fp);
  1075. if (fh->fh_r_ctl == FC_RCTL_DD_SOL_DATA &&
  1076. fh->fh_type == FC_TYPE_FCP) {
  1077. fc_exch_recv(lp, lp->emp, fp);
  1078. continue;
  1079. }
  1080. if (fr_flags(fp) & FCPHF_CRC_UNCHECKED) {
  1081. if (le32_to_cpu(fr_crc(fp)) !=
  1082. ~crc32(~0, skb->data, fr_len)) {
  1083. if (debug_fcoe || stats->InvalidCRCCount < 5)
  1084. printk(KERN_WARNING "fcoe: dropping "
  1085. "frame with CRC error\n");
  1086. stats->InvalidCRCCount++;
  1087. stats->ErrorFrames++;
  1088. fc_frame_free(fp);
  1089. continue;
  1090. }
  1091. fr_flags(fp) &= ~FCPHF_CRC_UNCHECKED;
  1092. }
  1093. if (unlikely(fc->ctlr.flogi_oxid != FC_XID_UNKNOWN) &&
  1094. fcoe_ctlr_recv_flogi(&fc->ctlr, fp, mac)) {
  1095. fc_frame_free(fp);
  1096. continue;
  1097. }
  1098. fc_exch_recv(lp, lp->emp, fp);
  1099. }
  1100. return 0;
  1101. }
  1102. /**
  1103. * fcoe_watchdog() - fcoe timer callback
  1104. * @vp:
  1105. *
  1106. * This checks the pending queue length for fcoe and set lport qfull
  1107. * if the FCOE_MAX_QUEUE_DEPTH is reached. This is done for all fc_lport on the
  1108. * fcoe_hostlist.
  1109. *
  1110. * Returns: 0 for success
  1111. */
  1112. void fcoe_watchdog(ulong vp)
  1113. {
  1114. struct fcoe_softc *fc;
  1115. read_lock(&fcoe_hostlist_lock);
  1116. list_for_each_entry(fc, &fcoe_hostlist, list) {
  1117. if (fc->ctlr.lp)
  1118. fcoe_check_wait_queue(fc->ctlr.lp);
  1119. }
  1120. read_unlock(&fcoe_hostlist_lock);
  1121. fcoe_timer.expires = jiffies + (1 * HZ);
  1122. add_timer(&fcoe_timer);
  1123. }
  1124. /**
  1125. * fcoe_check_wait_queue() - attempt to clear the transmit backlog
  1126. * @lp: the fc_lport
  1127. *
  1128. * This empties the wait_queue, dequeue the head of the wait_queue queue
  1129. * and calls fcoe_start_io() for each packet, if all skb have been
  1130. * transmitted, return qlen or -1 if a error occurs, then restore
  1131. * wait_queue and try again later.
  1132. *
  1133. * The wait_queue is used when the skb transmit fails. skb will go
  1134. * in the wait_queue which will be emptied by the timer function or
  1135. * by the next skb transmit.
  1136. *
  1137. * Returns: 0 for success
  1138. */
  1139. static int fcoe_check_wait_queue(struct fc_lport *lp)
  1140. {
  1141. struct fcoe_softc *fc = lport_priv(lp);
  1142. struct sk_buff *skb;
  1143. int rc = -1;
  1144. spin_lock_bh(&fc->fcoe_pending_queue.lock);
  1145. if (fc->fcoe_pending_queue_active)
  1146. goto out;
  1147. fc->fcoe_pending_queue_active = 1;
  1148. while (fc->fcoe_pending_queue.qlen) {
  1149. /* keep qlen > 0 until fcoe_start_io succeeds */
  1150. fc->fcoe_pending_queue.qlen++;
  1151. skb = __skb_dequeue(&fc->fcoe_pending_queue);
  1152. spin_unlock_bh(&fc->fcoe_pending_queue.lock);
  1153. rc = fcoe_start_io(skb);
  1154. spin_lock_bh(&fc->fcoe_pending_queue.lock);
  1155. if (rc) {
  1156. __skb_queue_head(&fc->fcoe_pending_queue, skb);
  1157. /* undo temporary increment above */
  1158. fc->fcoe_pending_queue.qlen--;
  1159. break;
  1160. }
  1161. /* undo temporary increment above */
  1162. fc->fcoe_pending_queue.qlen--;
  1163. }
  1164. if (fc->fcoe_pending_queue.qlen < FCOE_LOW_QUEUE_DEPTH)
  1165. lp->qfull = 0;
  1166. fc->fcoe_pending_queue_active = 0;
  1167. rc = fc->fcoe_pending_queue.qlen;
  1168. out:
  1169. spin_unlock_bh(&fc->fcoe_pending_queue.lock);
  1170. return rc;
  1171. }
  1172. /**
  1173. * fcoe_dev_setup() - setup link change notification interface
  1174. */
  1175. static void fcoe_dev_setup(void)
  1176. {
  1177. register_netdevice_notifier(&fcoe_notifier);
  1178. }
  1179. /**
  1180. * fcoe_dev_cleanup() - cleanup link change notification interface
  1181. */
  1182. static void fcoe_dev_cleanup(void)
  1183. {
  1184. unregister_netdevice_notifier(&fcoe_notifier);
  1185. }
  1186. /**
  1187. * fcoe_device_notification() - netdev event notification callback
  1188. * @notifier: context of the notification
  1189. * @event: type of event
  1190. * @ptr: fixed array for output parsed ifname
  1191. *
  1192. * This function is called by the ethernet driver in case of link change event
  1193. *
  1194. * Returns: 0 for success
  1195. */
  1196. static int fcoe_device_notification(struct notifier_block *notifier,
  1197. ulong event, void *ptr)
  1198. {
  1199. struct fc_lport *lp = NULL;
  1200. struct net_device *real_dev = ptr;
  1201. struct fcoe_softc *fc;
  1202. struct fcoe_dev_stats *stats;
  1203. u32 link_possible = 1;
  1204. u32 mfs;
  1205. int rc = NOTIFY_OK;
  1206. read_lock(&fcoe_hostlist_lock);
  1207. list_for_each_entry(fc, &fcoe_hostlist, list) {
  1208. if (fc->real_dev == real_dev) {
  1209. lp = fc->ctlr.lp;
  1210. break;
  1211. }
  1212. }
  1213. read_unlock(&fcoe_hostlist_lock);
  1214. if (lp == NULL) {
  1215. rc = NOTIFY_DONE;
  1216. goto out;
  1217. }
  1218. switch (event) {
  1219. case NETDEV_DOWN:
  1220. case NETDEV_GOING_DOWN:
  1221. link_possible = 0;
  1222. break;
  1223. case NETDEV_UP:
  1224. case NETDEV_CHANGE:
  1225. break;
  1226. case NETDEV_CHANGEMTU:
  1227. mfs = fc->real_dev->mtu -
  1228. (sizeof(struct fcoe_hdr) +
  1229. sizeof(struct fcoe_crc_eof));
  1230. if (mfs >= FC_MIN_MAX_FRAME)
  1231. fc_set_mfs(lp, mfs);
  1232. break;
  1233. case NETDEV_REGISTER:
  1234. break;
  1235. default:
  1236. FC_DBG("Unknown event %ld from netdev netlink\n", event);
  1237. }
  1238. if (link_possible && !fcoe_link_ok(lp))
  1239. fcoe_ctlr_link_up(&fc->ctlr);
  1240. else if (fcoe_ctlr_link_down(&fc->ctlr)) {
  1241. stats = fc_lport_get_stats(lp);
  1242. stats->LinkFailureCount++;
  1243. fcoe_clean_pending_queue(lp);
  1244. }
  1245. out:
  1246. return rc;
  1247. }
  1248. /**
  1249. * fcoe_if_to_netdev() - parse a name buffer to get netdev
  1250. * @buffer: incoming buffer to be copied
  1251. *
  1252. * Returns: NULL or ptr to net_device
  1253. */
  1254. static struct net_device *fcoe_if_to_netdev(const char *buffer)
  1255. {
  1256. char *cp;
  1257. char ifname[IFNAMSIZ + 2];
  1258. if (buffer) {
  1259. strlcpy(ifname, buffer, IFNAMSIZ);
  1260. cp = ifname + strlen(ifname);
  1261. while (--cp >= ifname && *cp == '\n')
  1262. *cp = '\0';
  1263. return dev_get_by_name(&init_net, ifname);
  1264. }
  1265. return NULL;
  1266. }
  1267. /**
  1268. * fcoe_netdev_to_module_owner() - finds out the driver module of the netdev
  1269. * @netdev: the target netdev
  1270. *
  1271. * Returns: ptr to the struct module, NULL for failure
  1272. */
  1273. static struct module *
  1274. fcoe_netdev_to_module_owner(const struct net_device *netdev)
  1275. {
  1276. struct device *dev;
  1277. if (!netdev)
  1278. return NULL;
  1279. dev = netdev->dev.parent;
  1280. if (!dev)
  1281. return NULL;
  1282. if (!dev->driver)
  1283. return NULL;
  1284. return dev->driver->owner;
  1285. }
  1286. /**
  1287. * fcoe_ethdrv_get() - Hold the Ethernet driver
  1288. * @netdev: the target netdev
  1289. *
  1290. * Holds the Ethernet driver module by try_module_get() for
  1291. * the corresponding netdev.
  1292. *
  1293. * Returns: 0 for success
  1294. */
  1295. static int fcoe_ethdrv_get(const struct net_device *netdev)
  1296. {
  1297. struct module *owner;
  1298. owner = fcoe_netdev_to_module_owner(netdev);
  1299. if (owner) {
  1300. printk(KERN_DEBUG "fcoe:hold driver module %s for %s\n",
  1301. module_name(owner), netdev->name);
  1302. return try_module_get(owner);
  1303. }
  1304. return -ENODEV;
  1305. }
  1306. /**
  1307. * fcoe_ethdrv_put() - Release the Ethernet driver
  1308. * @netdev: the target netdev
  1309. *
  1310. * Releases the Ethernet driver module by module_put for
  1311. * the corresponding netdev.
  1312. *
  1313. * Returns: 0 for success
  1314. */
  1315. static int fcoe_ethdrv_put(const struct net_device *netdev)
  1316. {
  1317. struct module *owner;
  1318. owner = fcoe_netdev_to_module_owner(netdev);
  1319. if (owner) {
  1320. printk(KERN_DEBUG "fcoe:release driver module %s for %s\n",
  1321. module_name(owner), netdev->name);
  1322. module_put(owner);
  1323. return 0;
  1324. }
  1325. return -ENODEV;
  1326. }
  1327. /**
  1328. * fcoe_destroy() - handles the destroy from sysfs
  1329. * @buffer: expected to be an eth if name
  1330. * @kp: associated kernel param
  1331. *
  1332. * Returns: 0 for success
  1333. */
  1334. static int fcoe_destroy(const char *buffer, struct kernel_param *kp)
  1335. {
  1336. int rc;
  1337. struct net_device *netdev;
  1338. netdev = fcoe_if_to_netdev(buffer);
  1339. if (!netdev) {
  1340. rc = -ENODEV;
  1341. goto out_nodev;
  1342. }
  1343. /* look for existing lport */
  1344. if (!fcoe_hostlist_lookup(netdev)) {
  1345. rc = -ENODEV;
  1346. goto out_putdev;
  1347. }
  1348. rc = fcoe_if_destroy(netdev);
  1349. if (rc) {
  1350. printk(KERN_ERR "fcoe: fcoe_if_destroy(%s) failed\n",
  1351. netdev->name);
  1352. rc = -EIO;
  1353. goto out_putdev;
  1354. }
  1355. fcoe_ethdrv_put(netdev);
  1356. rc = 0;
  1357. out_putdev:
  1358. dev_put(netdev);
  1359. out_nodev:
  1360. return rc;
  1361. }
  1362. /**
  1363. * fcoe_create() - Handles the create call from sysfs
  1364. * @buffer: expected to be an eth if name
  1365. * @kp: associated kernel param
  1366. *
  1367. * Returns: 0 for success
  1368. */
  1369. static int fcoe_create(const char *buffer, struct kernel_param *kp)
  1370. {
  1371. int rc;
  1372. struct net_device *netdev;
  1373. netdev = fcoe_if_to_netdev(buffer);
  1374. if (!netdev) {
  1375. rc = -ENODEV;
  1376. goto out_nodev;
  1377. }
  1378. /* look for existing lport */
  1379. if (fcoe_hostlist_lookup(netdev)) {
  1380. rc = -EEXIST;
  1381. goto out_putdev;
  1382. }
  1383. fcoe_ethdrv_get(netdev);
  1384. rc = fcoe_if_create(netdev);
  1385. if (rc) {
  1386. printk(KERN_ERR "fcoe: fcoe_if_create(%s) failed\n",
  1387. netdev->name);
  1388. fcoe_ethdrv_put(netdev);
  1389. rc = -EIO;
  1390. goto out_putdev;
  1391. }
  1392. rc = 0;
  1393. out_putdev:
  1394. dev_put(netdev);
  1395. out_nodev:
  1396. return rc;
  1397. }
  1398. module_param_call(create, fcoe_create, NULL, NULL, S_IWUSR);
  1399. __MODULE_PARM_TYPE(create, "string");
  1400. MODULE_PARM_DESC(create, "Create fcoe port using net device passed in.");
  1401. module_param_call(destroy, fcoe_destroy, NULL, NULL, S_IWUSR);
  1402. __MODULE_PARM_TYPE(destroy, "string");
  1403. MODULE_PARM_DESC(destroy, "Destroy fcoe port");
  1404. /**
  1405. * fcoe_link_ok() - Check if link is ok for the fc_lport
  1406. * @lp: ptr to the fc_lport
  1407. *
  1408. * Any permanently-disqualifying conditions have been previously checked.
  1409. * This also updates the speed setting, which may change with link for 100/1000.
  1410. *
  1411. * This function should probably be checking for PAUSE support at some point
  1412. * in the future. Currently Per-priority-pause is not determinable using
  1413. * ethtool, so we shouldn't be restrictive until that problem is resolved.
  1414. *
  1415. * Returns: 0 if link is OK for use by FCoE.
  1416. *
  1417. */
  1418. int fcoe_link_ok(struct fc_lport *lp)
  1419. {
  1420. struct fcoe_softc *fc = lport_priv(lp);
  1421. struct net_device *dev = fc->real_dev;
  1422. struct ethtool_cmd ecmd = { ETHTOOL_GSET };
  1423. int rc = 0;
  1424. if ((dev->flags & IFF_UP) && netif_carrier_ok(dev)) {
  1425. dev = fc->phys_dev;
  1426. if (dev->ethtool_ops->get_settings) {
  1427. dev->ethtool_ops->get_settings(dev, &ecmd);
  1428. lp->link_supported_speeds &=
  1429. ~(FC_PORTSPEED_1GBIT | FC_PORTSPEED_10GBIT);
  1430. if (ecmd.supported & (SUPPORTED_1000baseT_Half |
  1431. SUPPORTED_1000baseT_Full))
  1432. lp->link_supported_speeds |= FC_PORTSPEED_1GBIT;
  1433. if (ecmd.supported & SUPPORTED_10000baseT_Full)
  1434. lp->link_supported_speeds |=
  1435. FC_PORTSPEED_10GBIT;
  1436. if (ecmd.speed == SPEED_1000)
  1437. lp->link_speed = FC_PORTSPEED_1GBIT;
  1438. if (ecmd.speed == SPEED_10000)
  1439. lp->link_speed = FC_PORTSPEED_10GBIT;
  1440. }
  1441. } else
  1442. rc = -1;
  1443. return rc;
  1444. }
  1445. /**
  1446. * fcoe_percpu_clean() - Clear the pending skbs for an lport
  1447. * @lp: the fc_lport
  1448. */
  1449. void fcoe_percpu_clean(struct fc_lport *lp)
  1450. {
  1451. struct fcoe_percpu_s *pp;
  1452. struct fcoe_rcv_info *fr;
  1453. struct sk_buff_head *list;
  1454. struct sk_buff *skb, *next;
  1455. struct sk_buff *head;
  1456. unsigned int cpu;
  1457. for_each_possible_cpu(cpu) {
  1458. pp = &per_cpu(fcoe_percpu, cpu);
  1459. spin_lock_bh(&pp->fcoe_rx_list.lock);
  1460. list = &pp->fcoe_rx_list;
  1461. head = list->next;
  1462. for (skb = head; skb != (struct sk_buff *)list;
  1463. skb = next) {
  1464. next = skb->next;
  1465. fr = fcoe_dev_from_skb(skb);
  1466. if (fr->fr_dev == lp) {
  1467. __skb_unlink(skb, list);
  1468. kfree_skb(skb);
  1469. }
  1470. }
  1471. spin_unlock_bh(&pp->fcoe_rx_list.lock);
  1472. }
  1473. }
  1474. /**
  1475. * fcoe_clean_pending_queue() - Dequeue a skb and free it
  1476. * @lp: the corresponding fc_lport
  1477. *
  1478. * Returns: none
  1479. */
  1480. void fcoe_clean_pending_queue(struct fc_lport *lp)
  1481. {
  1482. struct fcoe_softc *fc = lport_priv(lp);
  1483. struct sk_buff *skb;
  1484. spin_lock_bh(&fc->fcoe_pending_queue.lock);
  1485. while ((skb = __skb_dequeue(&fc->fcoe_pending_queue)) != NULL) {
  1486. spin_unlock_bh(&fc->fcoe_pending_queue.lock);
  1487. kfree_skb(skb);
  1488. spin_lock_bh(&fc->fcoe_pending_queue.lock);
  1489. }
  1490. spin_unlock_bh(&fc->fcoe_pending_queue.lock);
  1491. }
  1492. /**
  1493. * fcoe_reset() - Resets the fcoe
  1494. * @shost: shost the reset is from
  1495. *
  1496. * Returns: always 0
  1497. */
  1498. int fcoe_reset(struct Scsi_Host *shost)
  1499. {
  1500. struct fc_lport *lport = shost_priv(shost);
  1501. fc_lport_reset(lport);
  1502. return 0;
  1503. }
  1504. /**
  1505. * fcoe_hostlist_lookup_softc() - find the corresponding lport by a given device
  1506. * @dev: this is currently ptr to net_device
  1507. *
  1508. * Returns: NULL or the located fcoe_softc
  1509. */
  1510. static struct fcoe_softc *
  1511. fcoe_hostlist_lookup_softc(const struct net_device *dev)
  1512. {
  1513. struct fcoe_softc *fc;
  1514. read_lock(&fcoe_hostlist_lock);
  1515. list_for_each_entry(fc, &fcoe_hostlist, list) {
  1516. if (fc->real_dev == dev) {
  1517. read_unlock(&fcoe_hostlist_lock);
  1518. return fc;
  1519. }
  1520. }
  1521. read_unlock(&fcoe_hostlist_lock);
  1522. return NULL;
  1523. }
  1524. /**
  1525. * fcoe_hostlist_lookup() - Find the corresponding lport by netdev
  1526. * @netdev: ptr to net_device
  1527. *
  1528. * Returns: 0 for success
  1529. */
  1530. struct fc_lport *fcoe_hostlist_lookup(const struct net_device *netdev)
  1531. {
  1532. struct fcoe_softc *fc;
  1533. fc = fcoe_hostlist_lookup_softc(netdev);
  1534. return (fc) ? fc->ctlr.lp : NULL;
  1535. }
  1536. /**
  1537. * fcoe_hostlist_add() - Add a lport to lports list
  1538. * @lp: ptr to the fc_lport to be added
  1539. *
  1540. * Returns: 0 for success
  1541. */
  1542. int fcoe_hostlist_add(const struct fc_lport *lp)
  1543. {
  1544. struct fcoe_softc *fc;
  1545. fc = fcoe_hostlist_lookup_softc(fcoe_netdev(lp));
  1546. if (!fc) {
  1547. fc = lport_priv(lp);
  1548. write_lock_bh(&fcoe_hostlist_lock);
  1549. list_add_tail(&fc->list, &fcoe_hostlist);
  1550. write_unlock_bh(&fcoe_hostlist_lock);
  1551. }
  1552. return 0;
  1553. }
  1554. /**
  1555. * fcoe_hostlist_remove() - remove a lport from lports list
  1556. * @lp: ptr to the fc_lport to be removed
  1557. *
  1558. * Returns: 0 for success
  1559. */
  1560. int fcoe_hostlist_remove(const struct fc_lport *lp)
  1561. {
  1562. struct fcoe_softc *fc;
  1563. fc = fcoe_hostlist_lookup_softc(fcoe_netdev(lp));
  1564. BUG_ON(!fc);
  1565. write_lock_bh(&fcoe_hostlist_lock);
  1566. list_del(&fc->list);
  1567. write_unlock_bh(&fcoe_hostlist_lock);
  1568. return 0;
  1569. }
  1570. /**
  1571. * fcoe_init() - fcoe module loading initialization
  1572. *
  1573. * Returns 0 on success, negative on failure
  1574. */
  1575. static int __init fcoe_init(void)
  1576. {
  1577. unsigned int cpu;
  1578. int rc = 0;
  1579. struct fcoe_percpu_s *p;
  1580. INIT_LIST_HEAD(&fcoe_hostlist);
  1581. rwlock_init(&fcoe_hostlist_lock);
  1582. for_each_possible_cpu(cpu) {
  1583. p = &per_cpu(fcoe_percpu, cpu);
  1584. skb_queue_head_init(&p->fcoe_rx_list);
  1585. }
  1586. for_each_online_cpu(cpu)
  1587. fcoe_percpu_thread_create(cpu);
  1588. /* Initialize per CPU interrupt thread */
  1589. rc = register_hotcpu_notifier(&fcoe_cpu_notifier);
  1590. if (rc)
  1591. goto out_free;
  1592. /* Setup link change notification */
  1593. fcoe_dev_setup();
  1594. setup_timer(&fcoe_timer, fcoe_watchdog, 0);
  1595. mod_timer(&fcoe_timer, jiffies + (10 * HZ));
  1596. fcoe_if_init();
  1597. return 0;
  1598. out_free:
  1599. for_each_online_cpu(cpu) {
  1600. fcoe_percpu_thread_destroy(cpu);
  1601. }
  1602. return rc;
  1603. }
  1604. module_init(fcoe_init);
  1605. /**
  1606. * fcoe_exit() - fcoe module unloading cleanup
  1607. *
  1608. * Returns 0 on success, negative on failure
  1609. */
  1610. static void __exit fcoe_exit(void)
  1611. {
  1612. unsigned int cpu;
  1613. struct fcoe_softc *fc, *tmp;
  1614. fcoe_dev_cleanup();
  1615. /* Stop the timer */
  1616. del_timer_sync(&fcoe_timer);
  1617. /* releases the associated fcoe hosts */
  1618. list_for_each_entry_safe(fc, tmp, &fcoe_hostlist, list)
  1619. fcoe_if_destroy(fc->real_dev);
  1620. unregister_hotcpu_notifier(&fcoe_cpu_notifier);
  1621. for_each_online_cpu(cpu) {
  1622. fcoe_percpu_thread_destroy(cpu);
  1623. }
  1624. /* detach from scsi transport */
  1625. fcoe_if_exit();
  1626. }
  1627. module_exit(fcoe_exit);