libfcoe.c 48 KB

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