fcoe.c 46 KB

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