fcoe.c 47 KB

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