fcoe_ctlr.c 72 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682
  1. /*
  2. * Copyright (c) 2008-2009 Cisco Systems, Inc. All rights reserved.
  3. * Copyright (c) 2009 Intel Corporation. All rights reserved.
  4. *
  5. * This program is free software; you can redistribute it and/or modify it
  6. * under the terms and conditions of the GNU General Public License,
  7. * version 2, as published by the Free Software Foundation.
  8. *
  9. * This program is distributed in the hope it will be useful, but WITHOUT
  10. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  12. * more details.
  13. *
  14. * You should have received a copy of the GNU General Public License along with
  15. * this program; if not, write to the Free Software Foundation, Inc.,
  16. * 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
  17. *
  18. * Maintained at www.Open-FCoE.org
  19. */
  20. #include <linux/types.h>
  21. #include <linux/module.h>
  22. #include <linux/kernel.h>
  23. #include <linux/list.h>
  24. #include <linux/spinlock.h>
  25. #include <linux/timer.h>
  26. #include <linux/netdevice.h>
  27. #include <linux/etherdevice.h>
  28. #include <linux/ethtool.h>
  29. #include <linux/if_ether.h>
  30. #include <linux/if_vlan.h>
  31. #include <linux/errno.h>
  32. #include <linux/bitops.h>
  33. #include <linux/slab.h>
  34. #include <net/rtnetlink.h>
  35. #include <scsi/fc/fc_els.h>
  36. #include <scsi/fc/fc_fs.h>
  37. #include <scsi/fc/fc_fip.h>
  38. #include <scsi/fc/fc_encaps.h>
  39. #include <scsi/fc/fc_fcoe.h>
  40. #include <scsi/fc/fc_fcp.h>
  41. #include <scsi/libfc.h>
  42. #include <scsi/libfcoe.h>
  43. #include "libfcoe.h"
  44. #define FCOE_CTLR_MIN_FKA 500 /* min keep alive (mS) */
  45. #define FCOE_CTLR_DEF_FKA FIP_DEF_FKA /* default keep alive (mS) */
  46. static void fcoe_ctlr_timeout(unsigned long);
  47. static void fcoe_ctlr_timer_work(struct work_struct *);
  48. static void fcoe_ctlr_recv_work(struct work_struct *);
  49. static int fcoe_ctlr_flogi_retry(struct fcoe_ctlr *);
  50. static void fcoe_ctlr_vn_start(struct fcoe_ctlr *);
  51. static int fcoe_ctlr_vn_recv(struct fcoe_ctlr *, struct sk_buff *);
  52. static void fcoe_ctlr_vn_timeout(struct fcoe_ctlr *);
  53. static int fcoe_ctlr_vn_lookup(struct fcoe_ctlr *, u32, u8 *);
  54. static u8 fcoe_all_fcfs[ETH_ALEN] = FIP_ALL_FCF_MACS;
  55. static u8 fcoe_all_enode[ETH_ALEN] = FIP_ALL_ENODE_MACS;
  56. static u8 fcoe_all_vn2vn[ETH_ALEN] = FIP_ALL_VN2VN_MACS;
  57. static u8 fcoe_all_p2p[ETH_ALEN] = FIP_ALL_P2P_MACS;
  58. static const char * const fcoe_ctlr_states[] = {
  59. [FIP_ST_DISABLED] = "DISABLED",
  60. [FIP_ST_LINK_WAIT] = "LINK_WAIT",
  61. [FIP_ST_AUTO] = "AUTO",
  62. [FIP_ST_NON_FIP] = "NON_FIP",
  63. [FIP_ST_ENABLED] = "ENABLED",
  64. [FIP_ST_VNMP_START] = "VNMP_START",
  65. [FIP_ST_VNMP_PROBE1] = "VNMP_PROBE1",
  66. [FIP_ST_VNMP_PROBE2] = "VNMP_PROBE2",
  67. [FIP_ST_VNMP_CLAIM] = "VNMP_CLAIM",
  68. [FIP_ST_VNMP_UP] = "VNMP_UP",
  69. };
  70. static const char *fcoe_ctlr_state(enum fip_state state)
  71. {
  72. const char *cp = "unknown";
  73. if (state < ARRAY_SIZE(fcoe_ctlr_states))
  74. cp = fcoe_ctlr_states[state];
  75. if (!cp)
  76. cp = "unknown";
  77. return cp;
  78. }
  79. /**
  80. * fcoe_ctlr_set_state() - Set and do debug printing for the new FIP state.
  81. * @fip: The FCoE controller
  82. * @state: The new state
  83. */
  84. static void fcoe_ctlr_set_state(struct fcoe_ctlr *fip, enum fip_state state)
  85. {
  86. if (state == fip->state)
  87. return;
  88. if (fip->lp)
  89. LIBFCOE_FIP_DBG(fip, "state %s -> %s\n",
  90. fcoe_ctlr_state(fip->state), fcoe_ctlr_state(state));
  91. fip->state = state;
  92. }
  93. /**
  94. * fcoe_ctlr_mtu_valid() - Check if a FCF's MTU is valid
  95. * @fcf: The FCF to check
  96. *
  97. * Return non-zero if FCF fcoe_size has been validated.
  98. */
  99. static inline int fcoe_ctlr_mtu_valid(const struct fcoe_fcf *fcf)
  100. {
  101. return (fcf->flags & FIP_FL_SOL) != 0;
  102. }
  103. /**
  104. * fcoe_ctlr_fcf_usable() - Check if a FCF is usable
  105. * @fcf: The FCF to check
  106. *
  107. * Return non-zero if the FCF is usable.
  108. */
  109. static inline int fcoe_ctlr_fcf_usable(struct fcoe_fcf *fcf)
  110. {
  111. u16 flags = FIP_FL_SOL | FIP_FL_AVAIL;
  112. return (fcf->flags & flags) == flags;
  113. }
  114. /**
  115. * fcoe_ctlr_map_dest() - Set flag and OUI for mapping destination addresses
  116. * @fip: The FCoE controller
  117. */
  118. static void fcoe_ctlr_map_dest(struct fcoe_ctlr *fip)
  119. {
  120. if (fip->mode == FIP_MODE_VN2VN)
  121. hton24(fip->dest_addr, FIP_VN_FC_MAP);
  122. else
  123. hton24(fip->dest_addr, FIP_DEF_FC_MAP);
  124. hton24(fip->dest_addr + 3, 0);
  125. fip->map_dest = 1;
  126. }
  127. /**
  128. * fcoe_ctlr_init() - Initialize the FCoE Controller instance
  129. * @fip: The FCoE controller to initialize
  130. */
  131. void fcoe_ctlr_init(struct fcoe_ctlr *fip, enum fip_state mode)
  132. {
  133. fcoe_ctlr_set_state(fip, FIP_ST_LINK_WAIT);
  134. fip->mode = mode;
  135. INIT_LIST_HEAD(&fip->fcfs);
  136. mutex_init(&fip->ctlr_mutex);
  137. spin_lock_init(&fip->ctlr_lock);
  138. fip->flogi_oxid = FC_XID_UNKNOWN;
  139. setup_timer(&fip->timer, fcoe_ctlr_timeout, (unsigned long)fip);
  140. INIT_WORK(&fip->timer_work, fcoe_ctlr_timer_work);
  141. INIT_WORK(&fip->recv_work, fcoe_ctlr_recv_work);
  142. skb_queue_head_init(&fip->fip_recv_list);
  143. }
  144. EXPORT_SYMBOL(fcoe_ctlr_init);
  145. /**
  146. * fcoe_ctlr_reset_fcfs() - Reset and free all FCFs for a controller
  147. * @fip: The FCoE controller whose FCFs are to be reset
  148. *
  149. * Called with &fcoe_ctlr lock held.
  150. */
  151. static void fcoe_ctlr_reset_fcfs(struct fcoe_ctlr *fip)
  152. {
  153. struct fcoe_fcf *fcf;
  154. struct fcoe_fcf *next;
  155. fip->sel_fcf = NULL;
  156. list_for_each_entry_safe(fcf, next, &fip->fcfs, list) {
  157. list_del(&fcf->list);
  158. kfree(fcf);
  159. }
  160. fip->fcf_count = 0;
  161. fip->sel_time = 0;
  162. }
  163. /**
  164. * fcoe_ctlr_destroy() - Disable and tear down a FCoE controller
  165. * @fip: The FCoE controller to tear down
  166. *
  167. * This is called by FCoE drivers before freeing the &fcoe_ctlr.
  168. *
  169. * The receive handler will have been deleted before this to guarantee
  170. * that no more recv_work will be scheduled.
  171. *
  172. * The timer routine will simply return once we set FIP_ST_DISABLED.
  173. * This guarantees that no further timeouts or work will be scheduled.
  174. */
  175. void fcoe_ctlr_destroy(struct fcoe_ctlr *fip)
  176. {
  177. cancel_work_sync(&fip->recv_work);
  178. skb_queue_purge(&fip->fip_recv_list);
  179. mutex_lock(&fip->ctlr_mutex);
  180. fcoe_ctlr_set_state(fip, FIP_ST_DISABLED);
  181. fcoe_ctlr_reset_fcfs(fip);
  182. mutex_unlock(&fip->ctlr_mutex);
  183. del_timer_sync(&fip->timer);
  184. cancel_work_sync(&fip->timer_work);
  185. }
  186. EXPORT_SYMBOL(fcoe_ctlr_destroy);
  187. /**
  188. * fcoe_ctlr_announce() - announce new FCF selection
  189. * @fip: The FCoE controller
  190. *
  191. * Also sets the destination MAC for FCoE and control packets
  192. *
  193. * Called with neither ctlr_mutex nor ctlr_lock held.
  194. */
  195. static void fcoe_ctlr_announce(struct fcoe_ctlr *fip)
  196. {
  197. struct fcoe_fcf *sel;
  198. struct fcoe_fcf *fcf;
  199. mutex_lock(&fip->ctlr_mutex);
  200. spin_lock_bh(&fip->ctlr_lock);
  201. kfree_skb(fip->flogi_req);
  202. fip->flogi_req = NULL;
  203. list_for_each_entry(fcf, &fip->fcfs, list)
  204. fcf->flogi_sent = 0;
  205. spin_unlock_bh(&fip->ctlr_lock);
  206. sel = fip->sel_fcf;
  207. if (sel && !compare_ether_addr(sel->fcf_mac, fip->dest_addr))
  208. goto unlock;
  209. if (!is_zero_ether_addr(fip->dest_addr)) {
  210. printk(KERN_NOTICE "libfcoe: host%d: "
  211. "FIP Fibre-Channel Forwarder MAC %pM deselected\n",
  212. fip->lp->host->host_no, fip->dest_addr);
  213. memset(fip->dest_addr, 0, ETH_ALEN);
  214. }
  215. if (sel) {
  216. printk(KERN_INFO "libfcoe: host%d: FIP selected "
  217. "Fibre-Channel Forwarder MAC %pM\n",
  218. fip->lp->host->host_no, sel->fcf_mac);
  219. memcpy(fip->dest_addr, sel->fcf_mac, ETH_ALEN);
  220. fip->map_dest = 0;
  221. }
  222. unlock:
  223. mutex_unlock(&fip->ctlr_mutex);
  224. }
  225. /**
  226. * fcoe_ctlr_fcoe_size() - Return the maximum FCoE size required for VN_Port
  227. * @fip: The FCoE controller to get the maximum FCoE size from
  228. *
  229. * Returns the maximum packet size including the FCoE header and trailer,
  230. * but not including any Ethernet or VLAN headers.
  231. */
  232. static inline u32 fcoe_ctlr_fcoe_size(struct fcoe_ctlr *fip)
  233. {
  234. /*
  235. * Determine the max FCoE frame size allowed, including
  236. * FCoE header and trailer.
  237. * Note: lp->mfs is currently the payload size, not the frame size.
  238. */
  239. return fip->lp->mfs + sizeof(struct fc_frame_header) +
  240. sizeof(struct fcoe_hdr) + sizeof(struct fcoe_crc_eof);
  241. }
  242. /**
  243. * fcoe_ctlr_solicit() - Send a FIP solicitation
  244. * @fip: The FCoE controller to send the solicitation on
  245. * @fcf: The destination FCF (if NULL, a multicast solicitation is sent)
  246. */
  247. static void fcoe_ctlr_solicit(struct fcoe_ctlr *fip, struct fcoe_fcf *fcf)
  248. {
  249. struct sk_buff *skb;
  250. struct fip_sol {
  251. struct ethhdr eth;
  252. struct fip_header fip;
  253. struct {
  254. struct fip_mac_desc mac;
  255. struct fip_wwn_desc wwnn;
  256. struct fip_size_desc size;
  257. } __packed desc;
  258. } __packed * sol;
  259. u32 fcoe_size;
  260. skb = dev_alloc_skb(sizeof(*sol));
  261. if (!skb)
  262. return;
  263. sol = (struct fip_sol *)skb->data;
  264. memset(sol, 0, sizeof(*sol));
  265. memcpy(sol->eth.h_dest, fcf ? fcf->fcf_mac : fcoe_all_fcfs, ETH_ALEN);
  266. memcpy(sol->eth.h_source, fip->ctl_src_addr, ETH_ALEN);
  267. sol->eth.h_proto = htons(ETH_P_FIP);
  268. sol->fip.fip_ver = FIP_VER_ENCAPS(FIP_VER);
  269. sol->fip.fip_op = htons(FIP_OP_DISC);
  270. sol->fip.fip_subcode = FIP_SC_SOL;
  271. sol->fip.fip_dl_len = htons(sizeof(sol->desc) / FIP_BPW);
  272. sol->fip.fip_flags = htons(FIP_FL_FPMA);
  273. if (fip->spma)
  274. sol->fip.fip_flags |= htons(FIP_FL_SPMA);
  275. sol->desc.mac.fd_desc.fip_dtype = FIP_DT_MAC;
  276. sol->desc.mac.fd_desc.fip_dlen = sizeof(sol->desc.mac) / FIP_BPW;
  277. memcpy(sol->desc.mac.fd_mac, fip->ctl_src_addr, ETH_ALEN);
  278. sol->desc.wwnn.fd_desc.fip_dtype = FIP_DT_NAME;
  279. sol->desc.wwnn.fd_desc.fip_dlen = sizeof(sol->desc.wwnn) / FIP_BPW;
  280. put_unaligned_be64(fip->lp->wwnn, &sol->desc.wwnn.fd_wwn);
  281. fcoe_size = fcoe_ctlr_fcoe_size(fip);
  282. sol->desc.size.fd_desc.fip_dtype = FIP_DT_FCOE_SIZE;
  283. sol->desc.size.fd_desc.fip_dlen = sizeof(sol->desc.size) / FIP_BPW;
  284. sol->desc.size.fd_size = htons(fcoe_size);
  285. skb_put(skb, sizeof(*sol));
  286. skb->protocol = htons(ETH_P_FIP);
  287. skb_reset_mac_header(skb);
  288. skb_reset_network_header(skb);
  289. fip->send(fip, skb);
  290. if (!fcf)
  291. fip->sol_time = jiffies;
  292. }
  293. /**
  294. * fcoe_ctlr_link_up() - Start FCoE controller
  295. * @fip: The FCoE controller to start
  296. *
  297. * Called from the LLD when the network link is ready.
  298. */
  299. void fcoe_ctlr_link_up(struct fcoe_ctlr *fip)
  300. {
  301. mutex_lock(&fip->ctlr_mutex);
  302. if (fip->state == FIP_ST_NON_FIP || fip->state == FIP_ST_AUTO) {
  303. mutex_unlock(&fip->ctlr_mutex);
  304. fc_linkup(fip->lp);
  305. } else if (fip->state == FIP_ST_LINK_WAIT) {
  306. fcoe_ctlr_set_state(fip, fip->mode);
  307. switch (fip->mode) {
  308. default:
  309. LIBFCOE_FIP_DBG(fip, "invalid mode %d\n", fip->mode);
  310. /* fall-through */
  311. case FIP_MODE_AUTO:
  312. LIBFCOE_FIP_DBG(fip, "%s", "setting AUTO mode.\n");
  313. /* fall-through */
  314. case FIP_MODE_FABRIC:
  315. case FIP_MODE_NON_FIP:
  316. mutex_unlock(&fip->ctlr_mutex);
  317. fc_linkup(fip->lp);
  318. fcoe_ctlr_solicit(fip, NULL);
  319. break;
  320. case FIP_MODE_VN2VN:
  321. fcoe_ctlr_vn_start(fip);
  322. mutex_unlock(&fip->ctlr_mutex);
  323. fc_linkup(fip->lp);
  324. break;
  325. }
  326. } else
  327. mutex_unlock(&fip->ctlr_mutex);
  328. }
  329. EXPORT_SYMBOL(fcoe_ctlr_link_up);
  330. /**
  331. * fcoe_ctlr_reset() - Reset a FCoE controller
  332. * @fip: The FCoE controller to reset
  333. */
  334. static void fcoe_ctlr_reset(struct fcoe_ctlr *fip)
  335. {
  336. fcoe_ctlr_reset_fcfs(fip);
  337. del_timer(&fip->timer);
  338. fip->ctlr_ka_time = 0;
  339. fip->port_ka_time = 0;
  340. fip->sol_time = 0;
  341. fip->flogi_oxid = FC_XID_UNKNOWN;
  342. fcoe_ctlr_map_dest(fip);
  343. }
  344. /**
  345. * fcoe_ctlr_link_down() - Stop a FCoE controller
  346. * @fip: The FCoE controller to be stopped
  347. *
  348. * Returns non-zero if the link was up and now isn't.
  349. *
  350. * Called from the LLD when the network link is not ready.
  351. * There may be multiple calls while the link is down.
  352. */
  353. int fcoe_ctlr_link_down(struct fcoe_ctlr *fip)
  354. {
  355. int link_dropped;
  356. LIBFCOE_FIP_DBG(fip, "link down.\n");
  357. mutex_lock(&fip->ctlr_mutex);
  358. fcoe_ctlr_reset(fip);
  359. link_dropped = fip->state != FIP_ST_LINK_WAIT;
  360. fcoe_ctlr_set_state(fip, FIP_ST_LINK_WAIT);
  361. mutex_unlock(&fip->ctlr_mutex);
  362. if (link_dropped)
  363. fc_linkdown(fip->lp);
  364. return link_dropped;
  365. }
  366. EXPORT_SYMBOL(fcoe_ctlr_link_down);
  367. /**
  368. * fcoe_ctlr_send_keep_alive() - Send a keep-alive to the selected FCF
  369. * @fip: The FCoE controller to send the FKA on
  370. * @lport: libfc fc_lport to send from
  371. * @ports: 0 for controller keep-alive, 1 for port keep-alive
  372. * @sa: The source MAC address
  373. *
  374. * A controller keep-alive is sent every fka_period (typically 8 seconds).
  375. * The source MAC is the native MAC address.
  376. *
  377. * A port keep-alive is sent every 90 seconds while logged in.
  378. * The source MAC is the assigned mapped source address.
  379. * The destination is the FCF's F-port.
  380. */
  381. static void fcoe_ctlr_send_keep_alive(struct fcoe_ctlr *fip,
  382. struct fc_lport *lport,
  383. int ports, u8 *sa)
  384. {
  385. struct sk_buff *skb;
  386. struct fip_kal {
  387. struct ethhdr eth;
  388. struct fip_header fip;
  389. struct fip_mac_desc mac;
  390. } __packed * kal;
  391. struct fip_vn_desc *vn;
  392. u32 len;
  393. struct fc_lport *lp;
  394. struct fcoe_fcf *fcf;
  395. fcf = fip->sel_fcf;
  396. lp = fip->lp;
  397. if (!fcf || (ports && !lp->port_id))
  398. return;
  399. len = sizeof(*kal) + ports * sizeof(*vn);
  400. skb = dev_alloc_skb(len);
  401. if (!skb)
  402. return;
  403. kal = (struct fip_kal *)skb->data;
  404. memset(kal, 0, len);
  405. memcpy(kal->eth.h_dest, fcf->fcf_mac, ETH_ALEN);
  406. memcpy(kal->eth.h_source, sa, ETH_ALEN);
  407. kal->eth.h_proto = htons(ETH_P_FIP);
  408. kal->fip.fip_ver = FIP_VER_ENCAPS(FIP_VER);
  409. kal->fip.fip_op = htons(FIP_OP_CTRL);
  410. kal->fip.fip_subcode = FIP_SC_KEEP_ALIVE;
  411. kal->fip.fip_dl_len = htons((sizeof(kal->mac) +
  412. ports * sizeof(*vn)) / FIP_BPW);
  413. kal->fip.fip_flags = htons(FIP_FL_FPMA);
  414. if (fip->spma)
  415. kal->fip.fip_flags |= htons(FIP_FL_SPMA);
  416. kal->mac.fd_desc.fip_dtype = FIP_DT_MAC;
  417. kal->mac.fd_desc.fip_dlen = sizeof(kal->mac) / FIP_BPW;
  418. memcpy(kal->mac.fd_mac, fip->ctl_src_addr, ETH_ALEN);
  419. if (ports) {
  420. vn = (struct fip_vn_desc *)(kal + 1);
  421. vn->fd_desc.fip_dtype = FIP_DT_VN_ID;
  422. vn->fd_desc.fip_dlen = sizeof(*vn) / FIP_BPW;
  423. memcpy(vn->fd_mac, fip->get_src_addr(lport), ETH_ALEN);
  424. hton24(vn->fd_fc_id, lport->port_id);
  425. put_unaligned_be64(lport->wwpn, &vn->fd_wwpn);
  426. }
  427. skb_put(skb, len);
  428. skb->protocol = htons(ETH_P_FIP);
  429. skb_reset_mac_header(skb);
  430. skb_reset_network_header(skb);
  431. fip->send(fip, skb);
  432. }
  433. /**
  434. * fcoe_ctlr_encaps() - Encapsulate an ELS frame for FIP, without sending it
  435. * @fip: The FCoE controller for the ELS frame
  436. * @dtype: The FIP descriptor type for the frame
  437. * @skb: The FCoE ELS frame including FC header but no FCoE headers
  438. * @d_id: The destination port ID.
  439. *
  440. * Returns non-zero error code on failure.
  441. *
  442. * The caller must check that the length is a multiple of 4.
  443. *
  444. * The @skb must have enough headroom (28 bytes) and tailroom (8 bytes).
  445. * Headroom includes the FIP encapsulation description, FIP header, and
  446. * Ethernet header. The tailroom is for the FIP MAC descriptor.
  447. */
  448. static int fcoe_ctlr_encaps(struct fcoe_ctlr *fip, struct fc_lport *lport,
  449. u8 dtype, struct sk_buff *skb, u32 d_id)
  450. {
  451. struct fip_encaps_head {
  452. struct ethhdr eth;
  453. struct fip_header fip;
  454. struct fip_encaps encaps;
  455. } __packed * cap;
  456. struct fc_frame_header *fh;
  457. struct fip_mac_desc *mac;
  458. struct fcoe_fcf *fcf;
  459. size_t dlen;
  460. u16 fip_flags;
  461. u8 op;
  462. fh = (struct fc_frame_header *)skb->data;
  463. op = *(u8 *)(fh + 1);
  464. dlen = sizeof(struct fip_encaps) + skb->len; /* len before push */
  465. cap = (struct fip_encaps_head *)skb_push(skb, sizeof(*cap));
  466. memset(cap, 0, sizeof(*cap));
  467. if (lport->point_to_multipoint) {
  468. if (fcoe_ctlr_vn_lookup(fip, d_id, cap->eth.h_dest))
  469. return -ENODEV;
  470. fip_flags = 0;
  471. } else {
  472. fcf = fip->sel_fcf;
  473. if (!fcf)
  474. return -ENODEV;
  475. fip_flags = fcf->flags;
  476. fip_flags &= fip->spma ? FIP_FL_SPMA | FIP_FL_FPMA :
  477. FIP_FL_FPMA;
  478. if (!fip_flags)
  479. return -ENODEV;
  480. memcpy(cap->eth.h_dest, fcf->fcf_mac, ETH_ALEN);
  481. }
  482. memcpy(cap->eth.h_source, fip->ctl_src_addr, ETH_ALEN);
  483. cap->eth.h_proto = htons(ETH_P_FIP);
  484. cap->fip.fip_ver = FIP_VER_ENCAPS(FIP_VER);
  485. cap->fip.fip_op = htons(FIP_OP_LS);
  486. if (op == ELS_LS_ACC || op == ELS_LS_RJT)
  487. cap->fip.fip_subcode = FIP_SC_REP;
  488. else
  489. cap->fip.fip_subcode = FIP_SC_REQ;
  490. cap->fip.fip_flags = htons(fip_flags);
  491. cap->encaps.fd_desc.fip_dtype = dtype;
  492. cap->encaps.fd_desc.fip_dlen = dlen / FIP_BPW;
  493. if (op != ELS_LS_RJT) {
  494. dlen += sizeof(*mac);
  495. mac = (struct fip_mac_desc *)skb_put(skb, sizeof(*mac));
  496. memset(mac, 0, sizeof(*mac));
  497. mac->fd_desc.fip_dtype = FIP_DT_MAC;
  498. mac->fd_desc.fip_dlen = sizeof(*mac) / FIP_BPW;
  499. if (dtype != FIP_DT_FLOGI && dtype != FIP_DT_FDISC) {
  500. memcpy(mac->fd_mac, fip->get_src_addr(lport), ETH_ALEN);
  501. } else if (fip->mode == FIP_MODE_VN2VN) {
  502. hton24(mac->fd_mac, FIP_VN_FC_MAP);
  503. hton24(mac->fd_mac + 3, fip->port_id);
  504. } else if (fip_flags & FIP_FL_SPMA) {
  505. LIBFCOE_FIP_DBG(fip, "FLOGI/FDISC sent with SPMA\n");
  506. memcpy(mac->fd_mac, fip->ctl_src_addr, ETH_ALEN);
  507. } else {
  508. LIBFCOE_FIP_DBG(fip, "FLOGI/FDISC sent with FPMA\n");
  509. /* FPMA only FLOGI. Must leave the MAC desc zeroed. */
  510. }
  511. }
  512. cap->fip.fip_dl_len = htons(dlen / FIP_BPW);
  513. skb->protocol = htons(ETH_P_FIP);
  514. skb_reset_mac_header(skb);
  515. skb_reset_network_header(skb);
  516. return 0;
  517. }
  518. /**
  519. * fcoe_ctlr_els_send() - Send an ELS frame encapsulated by FIP if appropriate.
  520. * @fip: FCoE controller.
  521. * @lport: libfc fc_lport to send from
  522. * @skb: FCoE ELS frame including FC header but no FCoE headers.
  523. *
  524. * Returns a non-zero error code if the frame should not be sent.
  525. * Returns zero if the caller should send the frame with FCoE encapsulation.
  526. *
  527. * The caller must check that the length is a multiple of 4.
  528. * The SKB must have enough headroom (28 bytes) and tailroom (8 bytes).
  529. * The the skb must also be an fc_frame.
  530. *
  531. * This is called from the lower-level driver with spinlocks held,
  532. * so we must not take a mutex here.
  533. */
  534. int fcoe_ctlr_els_send(struct fcoe_ctlr *fip, struct fc_lport *lport,
  535. struct sk_buff *skb)
  536. {
  537. struct fc_frame *fp;
  538. struct fc_frame_header *fh;
  539. u16 old_xid;
  540. u8 op;
  541. u8 mac[ETH_ALEN];
  542. fp = container_of(skb, struct fc_frame, skb);
  543. fh = (struct fc_frame_header *)skb->data;
  544. op = *(u8 *)(fh + 1);
  545. if (op == ELS_FLOGI && fip->mode != FIP_MODE_VN2VN) {
  546. old_xid = fip->flogi_oxid;
  547. fip->flogi_oxid = ntohs(fh->fh_ox_id);
  548. if (fip->state == FIP_ST_AUTO) {
  549. if (old_xid == FC_XID_UNKNOWN)
  550. fip->flogi_count = 0;
  551. fip->flogi_count++;
  552. if (fip->flogi_count < 3)
  553. goto drop;
  554. fcoe_ctlr_map_dest(fip);
  555. return 0;
  556. }
  557. if (fip->state == FIP_ST_NON_FIP)
  558. fcoe_ctlr_map_dest(fip);
  559. }
  560. if (fip->state == FIP_ST_NON_FIP)
  561. return 0;
  562. if (!fip->sel_fcf && fip->mode != FIP_MODE_VN2VN)
  563. goto drop;
  564. switch (op) {
  565. case ELS_FLOGI:
  566. op = FIP_DT_FLOGI;
  567. if (fip->mode == FIP_MODE_VN2VN)
  568. break;
  569. spin_lock_bh(&fip->ctlr_lock);
  570. kfree_skb(fip->flogi_req);
  571. fip->flogi_req = skb;
  572. fip->flogi_req_send = 1;
  573. spin_unlock_bh(&fip->ctlr_lock);
  574. schedule_work(&fip->timer_work);
  575. return -EINPROGRESS;
  576. case ELS_FDISC:
  577. if (ntoh24(fh->fh_s_id))
  578. return 0;
  579. op = FIP_DT_FDISC;
  580. break;
  581. case ELS_LOGO:
  582. if (fip->mode == FIP_MODE_VN2VN) {
  583. if (fip->state != FIP_ST_VNMP_UP)
  584. return -EINVAL;
  585. if (ntoh24(fh->fh_d_id) == FC_FID_FLOGI)
  586. return -EINVAL;
  587. } else {
  588. if (fip->state != FIP_ST_ENABLED)
  589. return 0;
  590. if (ntoh24(fh->fh_d_id) != FC_FID_FLOGI)
  591. return 0;
  592. }
  593. op = FIP_DT_LOGO;
  594. break;
  595. case ELS_LS_ACC:
  596. /*
  597. * If non-FIP, we may have gotten an SID by accepting an FLOGI
  598. * from a point-to-point connection. Switch to using
  599. * the source mac based on the SID. The destination
  600. * MAC in this case would have been set by receving the
  601. * FLOGI.
  602. */
  603. if (fip->state == FIP_ST_NON_FIP) {
  604. if (fip->flogi_oxid == FC_XID_UNKNOWN)
  605. return 0;
  606. fip->flogi_oxid = FC_XID_UNKNOWN;
  607. fc_fcoe_set_mac(mac, fh->fh_d_id);
  608. fip->update_mac(lport, mac);
  609. }
  610. /* fall through */
  611. case ELS_LS_RJT:
  612. op = fr_encaps(fp);
  613. if (op)
  614. break;
  615. return 0;
  616. default:
  617. if (fip->state != FIP_ST_ENABLED &&
  618. fip->state != FIP_ST_VNMP_UP)
  619. goto drop;
  620. return 0;
  621. }
  622. LIBFCOE_FIP_DBG(fip, "els_send op %u d_id %x\n",
  623. op, ntoh24(fh->fh_d_id));
  624. if (fcoe_ctlr_encaps(fip, lport, op, skb, ntoh24(fh->fh_d_id)))
  625. goto drop;
  626. fip->send(fip, skb);
  627. return -EINPROGRESS;
  628. drop:
  629. kfree_skb(skb);
  630. return -EINVAL;
  631. }
  632. EXPORT_SYMBOL(fcoe_ctlr_els_send);
  633. /**
  634. * fcoe_ctlr_age_fcfs() - Reset and free all old FCFs for a controller
  635. * @fip: The FCoE controller to free FCFs on
  636. *
  637. * Called with lock held and preemption disabled.
  638. *
  639. * An FCF is considered old if we have missed two advertisements.
  640. * That is, there have been no valid advertisement from it for 2.5
  641. * times its keep-alive period.
  642. *
  643. * In addition, determine the time when an FCF selection can occur.
  644. *
  645. * Also, increment the MissDiscAdvCount when no advertisement is received
  646. * for the corresponding FCF for 1.5 * FKA_ADV_PERIOD (FC-BB-5 LESB).
  647. *
  648. * Returns the time in jiffies for the next call.
  649. */
  650. static unsigned long fcoe_ctlr_age_fcfs(struct fcoe_ctlr *fip)
  651. {
  652. struct fcoe_fcf *fcf;
  653. struct fcoe_fcf *next;
  654. unsigned long next_timer = jiffies + msecs_to_jiffies(FIP_VN_KA_PERIOD);
  655. unsigned long deadline;
  656. unsigned long sel_time = 0;
  657. struct fcoe_dev_stats *stats;
  658. stats = per_cpu_ptr(fip->lp->dev_stats, get_cpu());
  659. list_for_each_entry_safe(fcf, next, &fip->fcfs, list) {
  660. deadline = fcf->time + fcf->fka_period + fcf->fka_period / 2;
  661. if (fip->sel_fcf == fcf) {
  662. if (time_after(jiffies, deadline)) {
  663. stats->MissDiscAdvCount++;
  664. printk(KERN_INFO "libfcoe: host%d: "
  665. "Missing Discovery Advertisement "
  666. "for fab %16.16llx count %lld\n",
  667. fip->lp->host->host_no, fcf->fabric_name,
  668. stats->MissDiscAdvCount);
  669. } else if (time_after(next_timer, deadline))
  670. next_timer = deadline;
  671. }
  672. deadline += fcf->fka_period;
  673. if (time_after_eq(jiffies, deadline)) {
  674. if (fip->sel_fcf == fcf)
  675. fip->sel_fcf = NULL;
  676. list_del(&fcf->list);
  677. WARN_ON(!fip->fcf_count);
  678. fip->fcf_count--;
  679. kfree(fcf);
  680. stats->VLinkFailureCount++;
  681. } else {
  682. if (time_after(next_timer, deadline))
  683. next_timer = deadline;
  684. if (fcoe_ctlr_mtu_valid(fcf) &&
  685. (!sel_time || time_before(sel_time, fcf->time)))
  686. sel_time = fcf->time;
  687. }
  688. }
  689. put_cpu();
  690. if (sel_time && !fip->sel_fcf && !fip->sel_time) {
  691. sel_time += msecs_to_jiffies(FCOE_CTLR_START_DELAY);
  692. fip->sel_time = sel_time;
  693. }
  694. return next_timer;
  695. }
  696. /**
  697. * fcoe_ctlr_parse_adv() - Decode a FIP advertisement into a new FCF entry
  698. * @fip: The FCoE controller receiving the advertisement
  699. * @skb: The received FIP advertisement frame
  700. * @fcf: The resulting FCF entry
  701. *
  702. * Returns zero on a valid parsed advertisement,
  703. * otherwise returns non zero value.
  704. */
  705. static int fcoe_ctlr_parse_adv(struct fcoe_ctlr *fip,
  706. struct sk_buff *skb, struct fcoe_fcf *fcf)
  707. {
  708. struct fip_header *fiph;
  709. struct fip_desc *desc = NULL;
  710. struct fip_wwn_desc *wwn;
  711. struct fip_fab_desc *fab;
  712. struct fip_fka_desc *fka;
  713. unsigned long t;
  714. size_t rlen;
  715. size_t dlen;
  716. u32 desc_mask;
  717. memset(fcf, 0, sizeof(*fcf));
  718. fcf->fka_period = msecs_to_jiffies(FCOE_CTLR_DEF_FKA);
  719. fiph = (struct fip_header *)skb->data;
  720. fcf->flags = ntohs(fiph->fip_flags);
  721. /*
  722. * mask of required descriptors. validating each one clears its bit.
  723. */
  724. desc_mask = BIT(FIP_DT_PRI) | BIT(FIP_DT_MAC) | BIT(FIP_DT_NAME) |
  725. BIT(FIP_DT_FAB) | BIT(FIP_DT_FKA);
  726. rlen = ntohs(fiph->fip_dl_len) * 4;
  727. if (rlen + sizeof(*fiph) > skb->len)
  728. return -EINVAL;
  729. desc = (struct fip_desc *)(fiph + 1);
  730. while (rlen > 0) {
  731. dlen = desc->fip_dlen * FIP_BPW;
  732. if (dlen < sizeof(*desc) || dlen > rlen)
  733. return -EINVAL;
  734. /* Drop Adv if there are duplicate critical descriptors */
  735. if ((desc->fip_dtype < 32) &&
  736. !(desc_mask & 1U << desc->fip_dtype)) {
  737. LIBFCOE_FIP_DBG(fip, "Duplicate Critical "
  738. "Descriptors in FIP adv\n");
  739. return -EINVAL;
  740. }
  741. switch (desc->fip_dtype) {
  742. case FIP_DT_PRI:
  743. if (dlen != sizeof(struct fip_pri_desc))
  744. goto len_err;
  745. fcf->pri = ((struct fip_pri_desc *)desc)->fd_pri;
  746. desc_mask &= ~BIT(FIP_DT_PRI);
  747. break;
  748. case FIP_DT_MAC:
  749. if (dlen != sizeof(struct fip_mac_desc))
  750. goto len_err;
  751. memcpy(fcf->fcf_mac,
  752. ((struct fip_mac_desc *)desc)->fd_mac,
  753. ETH_ALEN);
  754. if (!is_valid_ether_addr(fcf->fcf_mac)) {
  755. LIBFCOE_FIP_DBG(fip,
  756. "Invalid MAC addr %pM in FIP adv\n",
  757. fcf->fcf_mac);
  758. return -EINVAL;
  759. }
  760. desc_mask &= ~BIT(FIP_DT_MAC);
  761. break;
  762. case FIP_DT_NAME:
  763. if (dlen != sizeof(struct fip_wwn_desc))
  764. goto len_err;
  765. wwn = (struct fip_wwn_desc *)desc;
  766. fcf->switch_name = get_unaligned_be64(&wwn->fd_wwn);
  767. desc_mask &= ~BIT(FIP_DT_NAME);
  768. break;
  769. case FIP_DT_FAB:
  770. if (dlen != sizeof(struct fip_fab_desc))
  771. goto len_err;
  772. fab = (struct fip_fab_desc *)desc;
  773. fcf->fabric_name = get_unaligned_be64(&fab->fd_wwn);
  774. fcf->vfid = ntohs(fab->fd_vfid);
  775. fcf->fc_map = ntoh24(fab->fd_map);
  776. desc_mask &= ~BIT(FIP_DT_FAB);
  777. break;
  778. case FIP_DT_FKA:
  779. if (dlen != sizeof(struct fip_fka_desc))
  780. goto len_err;
  781. fka = (struct fip_fka_desc *)desc;
  782. if (fka->fd_flags & FIP_FKA_ADV_D)
  783. fcf->fd_flags = 1;
  784. t = ntohl(fka->fd_fka_period);
  785. if (t >= FCOE_CTLR_MIN_FKA)
  786. fcf->fka_period = msecs_to_jiffies(t);
  787. desc_mask &= ~BIT(FIP_DT_FKA);
  788. break;
  789. case FIP_DT_MAP_OUI:
  790. case FIP_DT_FCOE_SIZE:
  791. case FIP_DT_FLOGI:
  792. case FIP_DT_FDISC:
  793. case FIP_DT_LOGO:
  794. case FIP_DT_ELP:
  795. default:
  796. LIBFCOE_FIP_DBG(fip, "unexpected descriptor type %x "
  797. "in FIP adv\n", desc->fip_dtype);
  798. /* standard says ignore unknown descriptors >= 128 */
  799. if (desc->fip_dtype < FIP_DT_VENDOR_BASE)
  800. return -EINVAL;
  801. break;
  802. }
  803. desc = (struct fip_desc *)((char *)desc + dlen);
  804. rlen -= dlen;
  805. }
  806. if (!fcf->fc_map || (fcf->fc_map & 0x10000))
  807. return -EINVAL;
  808. if (!fcf->switch_name)
  809. return -EINVAL;
  810. if (desc_mask) {
  811. LIBFCOE_FIP_DBG(fip, "adv missing descriptors mask %x\n",
  812. desc_mask);
  813. return -EINVAL;
  814. }
  815. return 0;
  816. len_err:
  817. LIBFCOE_FIP_DBG(fip, "FIP length error in descriptor type %x len %zu\n",
  818. desc->fip_dtype, dlen);
  819. return -EINVAL;
  820. }
  821. /**
  822. * fcoe_ctlr_recv_adv() - Handle an incoming advertisement
  823. * @fip: The FCoE controller receiving the advertisement
  824. * @skb: The received FIP packet
  825. */
  826. static void fcoe_ctlr_recv_adv(struct fcoe_ctlr *fip, struct sk_buff *skb)
  827. {
  828. struct fcoe_fcf *fcf;
  829. struct fcoe_fcf new;
  830. struct fcoe_fcf *found;
  831. unsigned long sol_tov = msecs_to_jiffies(FCOE_CTRL_SOL_TOV);
  832. int first = 0;
  833. int mtu_valid;
  834. if (fcoe_ctlr_parse_adv(fip, skb, &new))
  835. return;
  836. mutex_lock(&fip->ctlr_mutex);
  837. first = list_empty(&fip->fcfs);
  838. found = NULL;
  839. list_for_each_entry(fcf, &fip->fcfs, list) {
  840. if (fcf->switch_name == new.switch_name &&
  841. fcf->fabric_name == new.fabric_name &&
  842. fcf->fc_map == new.fc_map &&
  843. compare_ether_addr(fcf->fcf_mac, new.fcf_mac) == 0) {
  844. found = fcf;
  845. break;
  846. }
  847. }
  848. if (!found) {
  849. if (fip->fcf_count >= FCOE_CTLR_FCF_LIMIT)
  850. goto out;
  851. fcf = kmalloc(sizeof(*fcf), GFP_ATOMIC);
  852. if (!fcf)
  853. goto out;
  854. fip->fcf_count++;
  855. memcpy(fcf, &new, sizeof(new));
  856. list_add(&fcf->list, &fip->fcfs);
  857. } else {
  858. /*
  859. * Update the FCF's keep-alive descriptor flags.
  860. * Other flag changes from new advertisements are
  861. * ignored after a solicited advertisement is
  862. * received and the FCF is selectable (usable).
  863. */
  864. fcf->fd_flags = new.fd_flags;
  865. if (!fcoe_ctlr_fcf_usable(fcf))
  866. fcf->flags = new.flags;
  867. if (fcf == fip->sel_fcf && !fcf->fd_flags) {
  868. fip->ctlr_ka_time -= fcf->fka_period;
  869. fip->ctlr_ka_time += new.fka_period;
  870. if (time_before(fip->ctlr_ka_time, fip->timer.expires))
  871. mod_timer(&fip->timer, fip->ctlr_ka_time);
  872. }
  873. fcf->fka_period = new.fka_period;
  874. memcpy(fcf->fcf_mac, new.fcf_mac, ETH_ALEN);
  875. }
  876. mtu_valid = fcoe_ctlr_mtu_valid(fcf);
  877. fcf->time = jiffies;
  878. if (!found)
  879. LIBFCOE_FIP_DBG(fip, "New FCF fab %16.16llx mac %pM\n",
  880. fcf->fabric_name, fcf->fcf_mac);
  881. /*
  882. * If this advertisement is not solicited and our max receive size
  883. * hasn't been verified, send a solicited advertisement.
  884. */
  885. if (!mtu_valid)
  886. fcoe_ctlr_solicit(fip, fcf);
  887. /*
  888. * If its been a while since we did a solicit, and this is
  889. * the first advertisement we've received, do a multicast
  890. * solicitation to gather as many advertisements as we can
  891. * before selection occurs.
  892. */
  893. if (first && time_after(jiffies, fip->sol_time + sol_tov))
  894. fcoe_ctlr_solicit(fip, NULL);
  895. /*
  896. * Put this FCF at the head of the list for priority among equals.
  897. * This helps in the case of an NPV switch which insists we use
  898. * the FCF that answers multicast solicitations, not the others that
  899. * are sending periodic multicast advertisements.
  900. */
  901. if (mtu_valid) {
  902. list_del(&fcf->list);
  903. list_add(&fcf->list, &fip->fcfs);
  904. }
  905. /*
  906. * If this is the first validated FCF, note the time and
  907. * set a timer to trigger selection.
  908. */
  909. if (mtu_valid && !fip->sel_fcf && fcoe_ctlr_fcf_usable(fcf)) {
  910. fip->sel_time = jiffies +
  911. msecs_to_jiffies(FCOE_CTLR_START_DELAY);
  912. if (!timer_pending(&fip->timer) ||
  913. time_before(fip->sel_time, fip->timer.expires))
  914. mod_timer(&fip->timer, fip->sel_time);
  915. }
  916. out:
  917. mutex_unlock(&fip->ctlr_mutex);
  918. }
  919. /**
  920. * fcoe_ctlr_recv_els() - Handle an incoming FIP encapsulated ELS frame
  921. * @fip: The FCoE controller which received the packet
  922. * @skb: The received FIP packet
  923. */
  924. static void fcoe_ctlr_recv_els(struct fcoe_ctlr *fip, struct sk_buff *skb)
  925. {
  926. struct fc_lport *lport = fip->lp;
  927. struct fip_header *fiph;
  928. struct fc_frame *fp = (struct fc_frame *)skb;
  929. struct fc_frame_header *fh = NULL;
  930. struct fip_desc *desc;
  931. struct fip_encaps *els;
  932. struct fcoe_dev_stats *stats;
  933. enum fip_desc_type els_dtype = 0;
  934. u8 els_op;
  935. u8 sub;
  936. u8 granted_mac[ETH_ALEN] = { 0 };
  937. size_t els_len = 0;
  938. size_t rlen;
  939. size_t dlen;
  940. u32 desc_mask = 0;
  941. u32 desc_cnt = 0;
  942. fiph = (struct fip_header *)skb->data;
  943. sub = fiph->fip_subcode;
  944. if (sub != FIP_SC_REQ && sub != FIP_SC_REP)
  945. goto drop;
  946. rlen = ntohs(fiph->fip_dl_len) * 4;
  947. if (rlen + sizeof(*fiph) > skb->len)
  948. goto drop;
  949. desc = (struct fip_desc *)(fiph + 1);
  950. while (rlen > 0) {
  951. desc_cnt++;
  952. dlen = desc->fip_dlen * FIP_BPW;
  953. if (dlen < sizeof(*desc) || dlen > rlen)
  954. goto drop;
  955. /* Drop ELS if there are duplicate critical descriptors */
  956. if (desc->fip_dtype < 32) {
  957. if (desc_mask & 1U << desc->fip_dtype) {
  958. LIBFCOE_FIP_DBG(fip, "Duplicate Critical "
  959. "Descriptors in FIP ELS\n");
  960. goto drop;
  961. }
  962. desc_mask |= (1 << desc->fip_dtype);
  963. }
  964. switch (desc->fip_dtype) {
  965. case FIP_DT_MAC:
  966. if (desc_cnt == 1) {
  967. LIBFCOE_FIP_DBG(fip, "FIP descriptors "
  968. "received out of order\n");
  969. goto drop;
  970. }
  971. if (dlen != sizeof(struct fip_mac_desc))
  972. goto len_err;
  973. memcpy(granted_mac,
  974. ((struct fip_mac_desc *)desc)->fd_mac,
  975. ETH_ALEN);
  976. break;
  977. case FIP_DT_FLOGI:
  978. case FIP_DT_FDISC:
  979. case FIP_DT_LOGO:
  980. case FIP_DT_ELP:
  981. if (desc_cnt != 1) {
  982. LIBFCOE_FIP_DBG(fip, "FIP descriptors "
  983. "received out of order\n");
  984. goto drop;
  985. }
  986. if (fh)
  987. goto drop;
  988. if (dlen < sizeof(*els) + sizeof(*fh) + 1)
  989. goto len_err;
  990. els_len = dlen - sizeof(*els);
  991. els = (struct fip_encaps *)desc;
  992. fh = (struct fc_frame_header *)(els + 1);
  993. els_dtype = desc->fip_dtype;
  994. break;
  995. default:
  996. LIBFCOE_FIP_DBG(fip, "unexpected descriptor type %x "
  997. "in FIP adv\n", desc->fip_dtype);
  998. /* standard says ignore unknown descriptors >= 128 */
  999. if (desc->fip_dtype < FIP_DT_VENDOR_BASE)
  1000. goto drop;
  1001. if (desc_cnt <= 2) {
  1002. LIBFCOE_FIP_DBG(fip, "FIP descriptors "
  1003. "received out of order\n");
  1004. goto drop;
  1005. }
  1006. break;
  1007. }
  1008. desc = (struct fip_desc *)((char *)desc + dlen);
  1009. rlen -= dlen;
  1010. }
  1011. if (!fh)
  1012. goto drop;
  1013. els_op = *(u8 *)(fh + 1);
  1014. if ((els_dtype == FIP_DT_FLOGI || els_dtype == FIP_DT_FDISC) &&
  1015. sub == FIP_SC_REP && fip->mode != FIP_MODE_VN2VN) {
  1016. if (els_op == ELS_LS_ACC) {
  1017. if (!is_valid_ether_addr(granted_mac)) {
  1018. LIBFCOE_FIP_DBG(fip,
  1019. "Invalid MAC address %pM in FIP ELS\n",
  1020. granted_mac);
  1021. goto drop;
  1022. }
  1023. memcpy(fr_cb(fp)->granted_mac, granted_mac, ETH_ALEN);
  1024. if (fip->flogi_oxid == ntohs(fh->fh_ox_id)) {
  1025. fip->flogi_oxid = FC_XID_UNKNOWN;
  1026. if (els_dtype == FIP_DT_FLOGI)
  1027. fcoe_ctlr_announce(fip);
  1028. }
  1029. } else if (els_dtype == FIP_DT_FLOGI &&
  1030. !fcoe_ctlr_flogi_retry(fip))
  1031. goto drop; /* retrying FLOGI so drop reject */
  1032. }
  1033. if ((desc_cnt == 0) || ((els_op != ELS_LS_RJT) &&
  1034. (!(1U << FIP_DT_MAC & desc_mask)))) {
  1035. LIBFCOE_FIP_DBG(fip, "Missing critical descriptors "
  1036. "in FIP ELS\n");
  1037. goto drop;
  1038. }
  1039. /*
  1040. * Convert skb into an fc_frame containing only the ELS.
  1041. */
  1042. skb_pull(skb, (u8 *)fh - skb->data);
  1043. skb_trim(skb, els_len);
  1044. fp = (struct fc_frame *)skb;
  1045. fc_frame_init(fp);
  1046. fr_sof(fp) = FC_SOF_I3;
  1047. fr_eof(fp) = FC_EOF_T;
  1048. fr_dev(fp) = lport;
  1049. fr_encaps(fp) = els_dtype;
  1050. stats = per_cpu_ptr(lport->dev_stats, get_cpu());
  1051. stats->RxFrames++;
  1052. stats->RxWords += skb->len / FIP_BPW;
  1053. put_cpu();
  1054. fc_exch_recv(lport, fp);
  1055. return;
  1056. len_err:
  1057. LIBFCOE_FIP_DBG(fip, "FIP length error in descriptor type %x len %zu\n",
  1058. desc->fip_dtype, dlen);
  1059. drop:
  1060. kfree_skb(skb);
  1061. }
  1062. /**
  1063. * fcoe_ctlr_recv_els() - Handle an incoming link reset frame
  1064. * @fip: The FCoE controller that received the frame
  1065. * @fh: The received FIP header
  1066. *
  1067. * There may be multiple VN_Port descriptors.
  1068. * The overall length has already been checked.
  1069. */
  1070. static void fcoe_ctlr_recv_clr_vlink(struct fcoe_ctlr *fip,
  1071. struct fip_header *fh)
  1072. {
  1073. struct fip_desc *desc;
  1074. struct fip_mac_desc *mp;
  1075. struct fip_wwn_desc *wp;
  1076. struct fip_vn_desc *vp;
  1077. size_t rlen;
  1078. size_t dlen;
  1079. struct fcoe_fcf *fcf = fip->sel_fcf;
  1080. struct fc_lport *lport = fip->lp;
  1081. struct fc_lport *vn_port = NULL;
  1082. u32 desc_mask;
  1083. int is_vn_port = 0;
  1084. LIBFCOE_FIP_DBG(fip, "Clear Virtual Link received\n");
  1085. if (!fcf || !lport->port_id)
  1086. return;
  1087. /*
  1088. * mask of required descriptors. Validating each one clears its bit.
  1089. */
  1090. desc_mask = BIT(FIP_DT_MAC) | BIT(FIP_DT_NAME) | BIT(FIP_DT_VN_ID);
  1091. rlen = ntohs(fh->fip_dl_len) * FIP_BPW;
  1092. desc = (struct fip_desc *)(fh + 1);
  1093. while (rlen >= sizeof(*desc)) {
  1094. dlen = desc->fip_dlen * FIP_BPW;
  1095. if (dlen > rlen)
  1096. return;
  1097. /* Drop CVL if there are duplicate critical descriptors */
  1098. if ((desc->fip_dtype < 32) &&
  1099. !(desc_mask & 1U << desc->fip_dtype)) {
  1100. LIBFCOE_FIP_DBG(fip, "Duplicate Critical "
  1101. "Descriptors in FIP CVL\n");
  1102. return;
  1103. }
  1104. switch (desc->fip_dtype) {
  1105. case FIP_DT_MAC:
  1106. mp = (struct fip_mac_desc *)desc;
  1107. if (dlen < sizeof(*mp))
  1108. return;
  1109. if (compare_ether_addr(mp->fd_mac, fcf->fcf_mac))
  1110. return;
  1111. desc_mask &= ~BIT(FIP_DT_MAC);
  1112. break;
  1113. case FIP_DT_NAME:
  1114. wp = (struct fip_wwn_desc *)desc;
  1115. if (dlen < sizeof(*wp))
  1116. return;
  1117. if (get_unaligned_be64(&wp->fd_wwn) != fcf->switch_name)
  1118. return;
  1119. desc_mask &= ~BIT(FIP_DT_NAME);
  1120. break;
  1121. case FIP_DT_VN_ID:
  1122. vp = (struct fip_vn_desc *)desc;
  1123. if (dlen < sizeof(*vp))
  1124. return;
  1125. if (compare_ether_addr(vp->fd_mac,
  1126. fip->get_src_addr(lport)) == 0 &&
  1127. get_unaligned_be64(&vp->fd_wwpn) == lport->wwpn &&
  1128. ntoh24(vp->fd_fc_id) == lport->port_id) {
  1129. desc_mask &= ~BIT(FIP_DT_VN_ID);
  1130. break;
  1131. }
  1132. /* check if clr_vlink is for NPIV port */
  1133. mutex_lock(&lport->lp_mutex);
  1134. list_for_each_entry(vn_port, &lport->vports, list) {
  1135. if (compare_ether_addr(vp->fd_mac,
  1136. fip->get_src_addr(vn_port)) == 0 &&
  1137. (get_unaligned_be64(&vp->fd_wwpn)
  1138. == vn_port->wwpn) &&
  1139. (ntoh24(vp->fd_fc_id) ==
  1140. fc_host_port_id(vn_port->host))) {
  1141. desc_mask &= ~BIT(FIP_DT_VN_ID);
  1142. is_vn_port = 1;
  1143. break;
  1144. }
  1145. }
  1146. mutex_unlock(&lport->lp_mutex);
  1147. break;
  1148. default:
  1149. /* standard says ignore unknown descriptors >= 128 */
  1150. if (desc->fip_dtype < FIP_DT_VENDOR_BASE)
  1151. return;
  1152. break;
  1153. }
  1154. desc = (struct fip_desc *)((char *)desc + dlen);
  1155. rlen -= dlen;
  1156. }
  1157. /*
  1158. * reset only if all required descriptors were present and valid.
  1159. */
  1160. if (desc_mask) {
  1161. LIBFCOE_FIP_DBG(fip, "missing descriptors mask %x\n",
  1162. desc_mask);
  1163. } else {
  1164. LIBFCOE_FIP_DBG(fip, "performing Clear Virtual Link\n");
  1165. if (is_vn_port)
  1166. fc_lport_reset(vn_port);
  1167. else {
  1168. mutex_lock(&fip->ctlr_mutex);
  1169. per_cpu_ptr(lport->dev_stats,
  1170. get_cpu())->VLinkFailureCount++;
  1171. put_cpu();
  1172. fcoe_ctlr_reset(fip);
  1173. mutex_unlock(&fip->ctlr_mutex);
  1174. fc_lport_reset(fip->lp);
  1175. fcoe_ctlr_solicit(fip, NULL);
  1176. }
  1177. }
  1178. }
  1179. /**
  1180. * fcoe_ctlr_recv() - Receive a FIP packet
  1181. * @fip: The FCoE controller that received the packet
  1182. * @skb: The received FIP packet
  1183. *
  1184. * This may be called from either NET_RX_SOFTIRQ or IRQ.
  1185. */
  1186. void fcoe_ctlr_recv(struct fcoe_ctlr *fip, struct sk_buff *skb)
  1187. {
  1188. skb_queue_tail(&fip->fip_recv_list, skb);
  1189. schedule_work(&fip->recv_work);
  1190. }
  1191. EXPORT_SYMBOL(fcoe_ctlr_recv);
  1192. /**
  1193. * fcoe_ctlr_recv_handler() - Receive a FIP frame
  1194. * @fip: The FCoE controller that received the frame
  1195. * @skb: The received FIP frame
  1196. *
  1197. * Returns non-zero if the frame is dropped.
  1198. */
  1199. static int fcoe_ctlr_recv_handler(struct fcoe_ctlr *fip, struct sk_buff *skb)
  1200. {
  1201. struct fip_header *fiph;
  1202. struct ethhdr *eh;
  1203. enum fip_state state;
  1204. u16 op;
  1205. u8 sub;
  1206. if (skb_linearize(skb))
  1207. goto drop;
  1208. if (skb->len < sizeof(*fiph))
  1209. goto drop;
  1210. eh = eth_hdr(skb);
  1211. if (fip->mode == FIP_MODE_VN2VN) {
  1212. if (compare_ether_addr(eh->h_dest, fip->ctl_src_addr) &&
  1213. compare_ether_addr(eh->h_dest, fcoe_all_vn2vn) &&
  1214. compare_ether_addr(eh->h_dest, fcoe_all_p2p))
  1215. goto drop;
  1216. } else if (compare_ether_addr(eh->h_dest, fip->ctl_src_addr) &&
  1217. compare_ether_addr(eh->h_dest, fcoe_all_enode))
  1218. goto drop;
  1219. fiph = (struct fip_header *)skb->data;
  1220. op = ntohs(fiph->fip_op);
  1221. sub = fiph->fip_subcode;
  1222. if (FIP_VER_DECAPS(fiph->fip_ver) != FIP_VER)
  1223. goto drop;
  1224. if (ntohs(fiph->fip_dl_len) * FIP_BPW + sizeof(*fiph) > skb->len)
  1225. goto drop;
  1226. mutex_lock(&fip->ctlr_mutex);
  1227. state = fip->state;
  1228. if (state == FIP_ST_AUTO) {
  1229. fip->map_dest = 0;
  1230. fcoe_ctlr_set_state(fip, FIP_ST_ENABLED);
  1231. state = FIP_ST_ENABLED;
  1232. LIBFCOE_FIP_DBG(fip, "Using FIP mode\n");
  1233. }
  1234. mutex_unlock(&fip->ctlr_mutex);
  1235. if (fip->mode == FIP_MODE_VN2VN && op == FIP_OP_VN2VN)
  1236. return fcoe_ctlr_vn_recv(fip, skb);
  1237. if (state != FIP_ST_ENABLED && state != FIP_ST_VNMP_UP &&
  1238. state != FIP_ST_VNMP_CLAIM)
  1239. goto drop;
  1240. if (op == FIP_OP_LS) {
  1241. fcoe_ctlr_recv_els(fip, skb); /* consumes skb */
  1242. return 0;
  1243. }
  1244. if (state != FIP_ST_ENABLED)
  1245. goto drop;
  1246. if (op == FIP_OP_DISC && sub == FIP_SC_ADV)
  1247. fcoe_ctlr_recv_adv(fip, skb);
  1248. else if (op == FIP_OP_CTRL && sub == FIP_SC_CLR_VLINK)
  1249. fcoe_ctlr_recv_clr_vlink(fip, fiph);
  1250. kfree_skb(skb);
  1251. return 0;
  1252. drop:
  1253. kfree_skb(skb);
  1254. return -1;
  1255. }
  1256. /**
  1257. * fcoe_ctlr_select() - Select the best FCF (if possible)
  1258. * @fip: The FCoE controller
  1259. *
  1260. * Returns the selected FCF, or NULL if none are usable.
  1261. *
  1262. * If there are conflicting advertisements, no FCF can be chosen.
  1263. *
  1264. * If there is already a selected FCF, this will choose a better one or
  1265. * an equivalent one that hasn't already been sent a FLOGI.
  1266. *
  1267. * Called with lock held.
  1268. */
  1269. static struct fcoe_fcf *fcoe_ctlr_select(struct fcoe_ctlr *fip)
  1270. {
  1271. struct fcoe_fcf *fcf;
  1272. struct fcoe_fcf *best = fip->sel_fcf;
  1273. struct fcoe_fcf *first;
  1274. first = list_first_entry(&fip->fcfs, struct fcoe_fcf, list);
  1275. list_for_each_entry(fcf, &fip->fcfs, list) {
  1276. LIBFCOE_FIP_DBG(fip, "consider FCF fab %16.16llx "
  1277. "VFID %d mac %pM map %x val %d "
  1278. "sent %u pri %u\n",
  1279. fcf->fabric_name, fcf->vfid, fcf->fcf_mac,
  1280. fcf->fc_map, fcoe_ctlr_mtu_valid(fcf),
  1281. fcf->flogi_sent, fcf->pri);
  1282. if (fcf->fabric_name != first->fabric_name ||
  1283. fcf->vfid != first->vfid ||
  1284. fcf->fc_map != first->fc_map) {
  1285. LIBFCOE_FIP_DBG(fip, "Conflicting fabric, VFID, "
  1286. "or FC-MAP\n");
  1287. return NULL;
  1288. }
  1289. if (fcf->flogi_sent)
  1290. continue;
  1291. if (!fcoe_ctlr_fcf_usable(fcf)) {
  1292. LIBFCOE_FIP_DBG(fip, "FCF for fab %16.16llx "
  1293. "map %x %svalid %savailable\n",
  1294. fcf->fabric_name, fcf->fc_map,
  1295. (fcf->flags & FIP_FL_SOL) ? "" : "in",
  1296. (fcf->flags & FIP_FL_AVAIL) ?
  1297. "" : "un");
  1298. continue;
  1299. }
  1300. if (!best || fcf->pri < best->pri || best->flogi_sent)
  1301. best = fcf;
  1302. }
  1303. fip->sel_fcf = best;
  1304. if (best) {
  1305. LIBFCOE_FIP_DBG(fip, "using FCF mac %pM\n", best->fcf_mac);
  1306. fip->port_ka_time = jiffies +
  1307. msecs_to_jiffies(FIP_VN_KA_PERIOD);
  1308. fip->ctlr_ka_time = jiffies + best->fka_period;
  1309. if (time_before(fip->ctlr_ka_time, fip->timer.expires))
  1310. mod_timer(&fip->timer, fip->ctlr_ka_time);
  1311. }
  1312. return best;
  1313. }
  1314. /**
  1315. * fcoe_ctlr_flogi_send_locked() - send FIP-encapsulated FLOGI to current FCF
  1316. * @fip: The FCoE controller
  1317. *
  1318. * Returns non-zero error if it could not be sent.
  1319. *
  1320. * Called with ctlr_mutex and ctlr_lock held.
  1321. * Caller must verify that fip->sel_fcf is not NULL.
  1322. */
  1323. static int fcoe_ctlr_flogi_send_locked(struct fcoe_ctlr *fip)
  1324. {
  1325. struct sk_buff *skb;
  1326. struct sk_buff *skb_orig;
  1327. struct fc_frame_header *fh;
  1328. int error;
  1329. skb_orig = fip->flogi_req;
  1330. if (!skb_orig)
  1331. return -EINVAL;
  1332. /*
  1333. * Clone and send the FLOGI request. If clone fails, use original.
  1334. */
  1335. skb = skb_clone(skb_orig, GFP_ATOMIC);
  1336. if (!skb) {
  1337. skb = skb_orig;
  1338. fip->flogi_req = NULL;
  1339. }
  1340. fh = (struct fc_frame_header *)skb->data;
  1341. error = fcoe_ctlr_encaps(fip, fip->lp, FIP_DT_FLOGI, skb,
  1342. ntoh24(fh->fh_d_id));
  1343. if (error) {
  1344. kfree_skb(skb);
  1345. return error;
  1346. }
  1347. fip->send(fip, skb);
  1348. fip->sel_fcf->flogi_sent = 1;
  1349. return 0;
  1350. }
  1351. /**
  1352. * fcoe_ctlr_flogi_retry() - resend FLOGI request to a new FCF if possible
  1353. * @fip: The FCoE controller
  1354. *
  1355. * Returns non-zero error code if there's no FLOGI request to retry or
  1356. * no alternate FCF available.
  1357. */
  1358. static int fcoe_ctlr_flogi_retry(struct fcoe_ctlr *fip)
  1359. {
  1360. struct fcoe_fcf *fcf;
  1361. int error;
  1362. mutex_lock(&fip->ctlr_mutex);
  1363. spin_lock_bh(&fip->ctlr_lock);
  1364. LIBFCOE_FIP_DBG(fip, "re-sending FLOGI - reselect\n");
  1365. fcf = fcoe_ctlr_select(fip);
  1366. if (!fcf || fcf->flogi_sent) {
  1367. kfree_skb(fip->flogi_req);
  1368. fip->flogi_req = NULL;
  1369. error = -ENOENT;
  1370. } else {
  1371. fcoe_ctlr_solicit(fip, NULL);
  1372. error = fcoe_ctlr_flogi_send_locked(fip);
  1373. }
  1374. spin_unlock_bh(&fip->ctlr_lock);
  1375. mutex_unlock(&fip->ctlr_mutex);
  1376. return error;
  1377. }
  1378. /**
  1379. * fcoe_ctlr_flogi_send() - Handle sending of FIP FLOGI.
  1380. * @fip: The FCoE controller that timed out
  1381. *
  1382. * Done here because fcoe_ctlr_els_send() can't get mutex.
  1383. *
  1384. * Called with ctlr_mutex held. The caller must not hold ctlr_lock.
  1385. */
  1386. static void fcoe_ctlr_flogi_send(struct fcoe_ctlr *fip)
  1387. {
  1388. struct fcoe_fcf *fcf;
  1389. spin_lock_bh(&fip->ctlr_lock);
  1390. fcf = fip->sel_fcf;
  1391. if (!fcf || !fip->flogi_req_send)
  1392. goto unlock;
  1393. LIBFCOE_FIP_DBG(fip, "sending FLOGI\n");
  1394. /*
  1395. * If this FLOGI is being sent due to a timeout retry
  1396. * to the same FCF as before, select a different FCF if possible.
  1397. */
  1398. if (fcf->flogi_sent) {
  1399. LIBFCOE_FIP_DBG(fip, "sending FLOGI - reselect\n");
  1400. fcf = fcoe_ctlr_select(fip);
  1401. if (!fcf || fcf->flogi_sent) {
  1402. LIBFCOE_FIP_DBG(fip, "sending FLOGI - clearing\n");
  1403. list_for_each_entry(fcf, &fip->fcfs, list)
  1404. fcf->flogi_sent = 0;
  1405. fcf = fcoe_ctlr_select(fip);
  1406. }
  1407. }
  1408. if (fcf) {
  1409. fcoe_ctlr_flogi_send_locked(fip);
  1410. fip->flogi_req_send = 0;
  1411. } else /* XXX */
  1412. LIBFCOE_FIP_DBG(fip, "No FCF selected - defer send\n");
  1413. unlock:
  1414. spin_unlock_bh(&fip->ctlr_lock);
  1415. }
  1416. /**
  1417. * fcoe_ctlr_timeout() - FIP timeout handler
  1418. * @arg: The FCoE controller that timed out
  1419. */
  1420. static void fcoe_ctlr_timeout(unsigned long arg)
  1421. {
  1422. struct fcoe_ctlr *fip = (struct fcoe_ctlr *)arg;
  1423. schedule_work(&fip->timer_work);
  1424. }
  1425. /**
  1426. * fcoe_ctlr_timer_work() - Worker thread function for timer work
  1427. * @work: Handle to a FCoE controller
  1428. *
  1429. * Ages FCFs. Triggers FCF selection if possible.
  1430. * Sends keep-alives and resets.
  1431. */
  1432. static void fcoe_ctlr_timer_work(struct work_struct *work)
  1433. {
  1434. struct fcoe_ctlr *fip;
  1435. struct fc_lport *vport;
  1436. u8 *mac;
  1437. u8 reset = 0;
  1438. u8 send_ctlr_ka = 0;
  1439. u8 send_port_ka = 0;
  1440. struct fcoe_fcf *sel;
  1441. struct fcoe_fcf *fcf;
  1442. unsigned long next_timer;
  1443. fip = container_of(work, struct fcoe_ctlr, timer_work);
  1444. if (fip->mode == FIP_MODE_VN2VN)
  1445. return fcoe_ctlr_vn_timeout(fip);
  1446. mutex_lock(&fip->ctlr_mutex);
  1447. if (fip->state == FIP_ST_DISABLED) {
  1448. mutex_unlock(&fip->ctlr_mutex);
  1449. return;
  1450. }
  1451. fcf = fip->sel_fcf;
  1452. next_timer = fcoe_ctlr_age_fcfs(fip);
  1453. sel = fip->sel_fcf;
  1454. if (!sel && fip->sel_time) {
  1455. if (time_after_eq(jiffies, fip->sel_time)) {
  1456. sel = fcoe_ctlr_select(fip);
  1457. fip->sel_time = 0;
  1458. } else if (time_after(next_timer, fip->sel_time))
  1459. next_timer = fip->sel_time;
  1460. }
  1461. if (sel && fip->flogi_req_send)
  1462. fcoe_ctlr_flogi_send(fip);
  1463. else if (!sel && fcf)
  1464. reset = 1;
  1465. if (sel && !sel->fd_flags) {
  1466. if (time_after_eq(jiffies, fip->ctlr_ka_time)) {
  1467. fip->ctlr_ka_time = jiffies + sel->fka_period;
  1468. send_ctlr_ka = 1;
  1469. }
  1470. if (time_after(next_timer, fip->ctlr_ka_time))
  1471. next_timer = fip->ctlr_ka_time;
  1472. if (time_after_eq(jiffies, fip->port_ka_time)) {
  1473. fip->port_ka_time = jiffies +
  1474. msecs_to_jiffies(FIP_VN_KA_PERIOD);
  1475. send_port_ka = 1;
  1476. }
  1477. if (time_after(next_timer, fip->port_ka_time))
  1478. next_timer = fip->port_ka_time;
  1479. }
  1480. if (!list_empty(&fip->fcfs))
  1481. mod_timer(&fip->timer, next_timer);
  1482. mutex_unlock(&fip->ctlr_mutex);
  1483. if (reset) {
  1484. fc_lport_reset(fip->lp);
  1485. /* restart things with a solicitation */
  1486. fcoe_ctlr_solicit(fip, NULL);
  1487. }
  1488. if (send_ctlr_ka)
  1489. fcoe_ctlr_send_keep_alive(fip, NULL, 0, fip->ctl_src_addr);
  1490. if (send_port_ka) {
  1491. mutex_lock(&fip->lp->lp_mutex);
  1492. mac = fip->get_src_addr(fip->lp);
  1493. fcoe_ctlr_send_keep_alive(fip, fip->lp, 1, mac);
  1494. list_for_each_entry(vport, &fip->lp->vports, list) {
  1495. mac = fip->get_src_addr(vport);
  1496. fcoe_ctlr_send_keep_alive(fip, vport, 1, mac);
  1497. }
  1498. mutex_unlock(&fip->lp->lp_mutex);
  1499. }
  1500. }
  1501. /**
  1502. * fcoe_ctlr_recv_work() - Worker thread function for receiving FIP frames
  1503. * @recv_work: Handle to a FCoE controller
  1504. */
  1505. static void fcoe_ctlr_recv_work(struct work_struct *recv_work)
  1506. {
  1507. struct fcoe_ctlr *fip;
  1508. struct sk_buff *skb;
  1509. fip = container_of(recv_work, struct fcoe_ctlr, recv_work);
  1510. while ((skb = skb_dequeue(&fip->fip_recv_list)))
  1511. fcoe_ctlr_recv_handler(fip, skb);
  1512. }
  1513. /**
  1514. * fcoe_ctlr_recv_flogi() - Snoop pre-FIP receipt of FLOGI response
  1515. * @fip: The FCoE controller
  1516. * @fp: The FC frame to snoop
  1517. *
  1518. * Snoop potential response to FLOGI or even incoming FLOGI.
  1519. *
  1520. * The caller has checked that we are waiting for login as indicated
  1521. * by fip->flogi_oxid != FC_XID_UNKNOWN.
  1522. *
  1523. * The caller is responsible for freeing the frame.
  1524. * Fill in the granted_mac address.
  1525. *
  1526. * Return non-zero if the frame should not be delivered to libfc.
  1527. */
  1528. int fcoe_ctlr_recv_flogi(struct fcoe_ctlr *fip, struct fc_lport *lport,
  1529. struct fc_frame *fp)
  1530. {
  1531. struct fc_frame_header *fh;
  1532. u8 op;
  1533. u8 *sa;
  1534. sa = eth_hdr(&fp->skb)->h_source;
  1535. fh = fc_frame_header_get(fp);
  1536. if (fh->fh_type != FC_TYPE_ELS)
  1537. return 0;
  1538. op = fc_frame_payload_op(fp);
  1539. if (op == ELS_LS_ACC && fh->fh_r_ctl == FC_RCTL_ELS_REP &&
  1540. fip->flogi_oxid == ntohs(fh->fh_ox_id)) {
  1541. mutex_lock(&fip->ctlr_mutex);
  1542. if (fip->state != FIP_ST_AUTO && fip->state != FIP_ST_NON_FIP) {
  1543. mutex_unlock(&fip->ctlr_mutex);
  1544. return -EINVAL;
  1545. }
  1546. fcoe_ctlr_set_state(fip, FIP_ST_NON_FIP);
  1547. LIBFCOE_FIP_DBG(fip,
  1548. "received FLOGI LS_ACC using non-FIP mode\n");
  1549. /*
  1550. * FLOGI accepted.
  1551. * If the src mac addr is FC_OUI-based, then we mark the
  1552. * address_mode flag to use FC_OUI-based Ethernet DA.
  1553. * Otherwise we use the FCoE gateway addr
  1554. */
  1555. if (!compare_ether_addr(sa, (u8[6])FC_FCOE_FLOGI_MAC)) {
  1556. fcoe_ctlr_map_dest(fip);
  1557. } else {
  1558. memcpy(fip->dest_addr, sa, ETH_ALEN);
  1559. fip->map_dest = 0;
  1560. }
  1561. fip->flogi_oxid = FC_XID_UNKNOWN;
  1562. mutex_unlock(&fip->ctlr_mutex);
  1563. fc_fcoe_set_mac(fr_cb(fp)->granted_mac, fh->fh_d_id);
  1564. } else if (op == ELS_FLOGI && fh->fh_r_ctl == FC_RCTL_ELS_REQ && sa) {
  1565. /*
  1566. * Save source MAC for point-to-point responses.
  1567. */
  1568. mutex_lock(&fip->ctlr_mutex);
  1569. if (fip->state == FIP_ST_AUTO || fip->state == FIP_ST_NON_FIP) {
  1570. memcpy(fip->dest_addr, sa, ETH_ALEN);
  1571. fip->map_dest = 0;
  1572. if (fip->state == FIP_ST_AUTO)
  1573. LIBFCOE_FIP_DBG(fip, "received non-FIP FLOGI. "
  1574. "Setting non-FIP mode\n");
  1575. fcoe_ctlr_set_state(fip, FIP_ST_NON_FIP);
  1576. }
  1577. mutex_unlock(&fip->ctlr_mutex);
  1578. }
  1579. return 0;
  1580. }
  1581. EXPORT_SYMBOL(fcoe_ctlr_recv_flogi);
  1582. /**
  1583. * fcoe_wwn_from_mac() - Converts a 48-bit IEEE MAC address to a 64-bit FC WWN
  1584. * @mac: The MAC address to convert
  1585. * @scheme: The scheme to use when converting
  1586. * @port: The port indicator for converting
  1587. *
  1588. * Returns: u64 fc world wide name
  1589. */
  1590. u64 fcoe_wwn_from_mac(unsigned char mac[MAX_ADDR_LEN],
  1591. unsigned int scheme, unsigned int port)
  1592. {
  1593. u64 wwn;
  1594. u64 host_mac;
  1595. /* The MAC is in NO, so flip only the low 48 bits */
  1596. host_mac = ((u64) mac[0] << 40) |
  1597. ((u64) mac[1] << 32) |
  1598. ((u64) mac[2] << 24) |
  1599. ((u64) mac[3] << 16) |
  1600. ((u64) mac[4] << 8) |
  1601. (u64) mac[5];
  1602. WARN_ON(host_mac >= (1ULL << 48));
  1603. wwn = host_mac | ((u64) scheme << 60);
  1604. switch (scheme) {
  1605. case 1:
  1606. WARN_ON(port != 0);
  1607. break;
  1608. case 2:
  1609. WARN_ON(port >= 0xfff);
  1610. wwn |= (u64) port << 48;
  1611. break;
  1612. default:
  1613. WARN_ON(1);
  1614. break;
  1615. }
  1616. return wwn;
  1617. }
  1618. EXPORT_SYMBOL_GPL(fcoe_wwn_from_mac);
  1619. /**
  1620. * fcoe_ctlr_rport() - return the fcoe_rport for a given fc_rport_priv
  1621. * @rdata: libfc remote port
  1622. */
  1623. static inline struct fcoe_rport *fcoe_ctlr_rport(struct fc_rport_priv *rdata)
  1624. {
  1625. return (struct fcoe_rport *)(rdata + 1);
  1626. }
  1627. /**
  1628. * fcoe_ctlr_vn_send() - Send a FIP VN2VN Probe Request or Reply.
  1629. * @fip: The FCoE controller
  1630. * @sub: sub-opcode for probe request, reply, or advertisement.
  1631. * @dest: The destination Ethernet MAC address
  1632. * @min_len: minimum size of the Ethernet payload to be sent
  1633. */
  1634. static void fcoe_ctlr_vn_send(struct fcoe_ctlr *fip,
  1635. enum fip_vn2vn_subcode sub,
  1636. const u8 *dest, size_t min_len)
  1637. {
  1638. struct sk_buff *skb;
  1639. struct fip_frame {
  1640. struct ethhdr eth;
  1641. struct fip_header fip;
  1642. struct fip_mac_desc mac;
  1643. struct fip_wwn_desc wwnn;
  1644. struct fip_vn_desc vn;
  1645. } __packed * frame;
  1646. struct fip_fc4_feat *ff;
  1647. struct fip_size_desc *size;
  1648. u32 fcp_feat;
  1649. size_t len;
  1650. size_t dlen;
  1651. len = sizeof(*frame);
  1652. dlen = 0;
  1653. if (sub == FIP_SC_VN_CLAIM_NOTIFY || sub == FIP_SC_VN_CLAIM_REP) {
  1654. dlen = sizeof(struct fip_fc4_feat) +
  1655. sizeof(struct fip_size_desc);
  1656. len += dlen;
  1657. }
  1658. dlen += sizeof(frame->mac) + sizeof(frame->wwnn) + sizeof(frame->vn);
  1659. len = max(len, min_len + sizeof(struct ethhdr));
  1660. skb = dev_alloc_skb(len);
  1661. if (!skb)
  1662. return;
  1663. frame = (struct fip_frame *)skb->data;
  1664. memset(frame, 0, len);
  1665. memcpy(frame->eth.h_dest, dest, ETH_ALEN);
  1666. memcpy(frame->eth.h_source, fip->ctl_src_addr, ETH_ALEN);
  1667. frame->eth.h_proto = htons(ETH_P_FIP);
  1668. frame->fip.fip_ver = FIP_VER_ENCAPS(FIP_VER);
  1669. frame->fip.fip_op = htons(FIP_OP_VN2VN);
  1670. frame->fip.fip_subcode = sub;
  1671. frame->fip.fip_dl_len = htons(dlen / FIP_BPW);
  1672. frame->mac.fd_desc.fip_dtype = FIP_DT_MAC;
  1673. frame->mac.fd_desc.fip_dlen = sizeof(frame->mac) / FIP_BPW;
  1674. memcpy(frame->mac.fd_mac, fip->ctl_src_addr, ETH_ALEN);
  1675. frame->wwnn.fd_desc.fip_dtype = FIP_DT_NAME;
  1676. frame->wwnn.fd_desc.fip_dlen = sizeof(frame->wwnn) / FIP_BPW;
  1677. put_unaligned_be64(fip->lp->wwnn, &frame->wwnn.fd_wwn);
  1678. frame->vn.fd_desc.fip_dtype = FIP_DT_VN_ID;
  1679. frame->vn.fd_desc.fip_dlen = sizeof(frame->vn) / FIP_BPW;
  1680. hton24(frame->vn.fd_mac, FIP_VN_FC_MAP);
  1681. hton24(frame->vn.fd_mac + 3, fip->port_id);
  1682. hton24(frame->vn.fd_fc_id, fip->port_id);
  1683. put_unaligned_be64(fip->lp->wwpn, &frame->vn.fd_wwpn);
  1684. /*
  1685. * For claims, add FC-4 features.
  1686. * TBD: Add interface to get fc-4 types and features from libfc.
  1687. */
  1688. if (sub == FIP_SC_VN_CLAIM_NOTIFY || sub == FIP_SC_VN_CLAIM_REP) {
  1689. ff = (struct fip_fc4_feat *)(frame + 1);
  1690. ff->fd_desc.fip_dtype = FIP_DT_FC4F;
  1691. ff->fd_desc.fip_dlen = sizeof(*ff) / FIP_BPW;
  1692. ff->fd_fts = fip->lp->fcts;
  1693. fcp_feat = 0;
  1694. if (fip->lp->service_params & FCP_SPPF_INIT_FCN)
  1695. fcp_feat |= FCP_FEAT_INIT;
  1696. if (fip->lp->service_params & FCP_SPPF_TARG_FCN)
  1697. fcp_feat |= FCP_FEAT_TARG;
  1698. fcp_feat <<= (FC_TYPE_FCP * 4) % 32;
  1699. ff->fd_ff.fd_feat[FC_TYPE_FCP * 4 / 32] = htonl(fcp_feat);
  1700. size = (struct fip_size_desc *)(ff + 1);
  1701. size->fd_desc.fip_dtype = FIP_DT_FCOE_SIZE;
  1702. size->fd_desc.fip_dlen = sizeof(*size) / FIP_BPW;
  1703. size->fd_size = htons(fcoe_ctlr_fcoe_size(fip));
  1704. }
  1705. skb_put(skb, len);
  1706. skb->protocol = htons(ETH_P_FIP);
  1707. skb_reset_mac_header(skb);
  1708. skb_reset_network_header(skb);
  1709. fip->send(fip, skb);
  1710. }
  1711. /**
  1712. * fcoe_ctlr_vn_rport_callback - Event handler for rport events.
  1713. * @lport: The lport which is receiving the event
  1714. * @rdata: remote port private data
  1715. * @event: The event that occured
  1716. *
  1717. * Locking Note: The rport lock must not be held when calling this function.
  1718. */
  1719. static void fcoe_ctlr_vn_rport_callback(struct fc_lport *lport,
  1720. struct fc_rport_priv *rdata,
  1721. enum fc_rport_event event)
  1722. {
  1723. struct fcoe_ctlr *fip = lport->disc.priv;
  1724. struct fcoe_rport *frport = fcoe_ctlr_rport(rdata);
  1725. LIBFCOE_FIP_DBG(fip, "vn_rport_callback %x event %d\n",
  1726. rdata->ids.port_id, event);
  1727. mutex_lock(&fip->ctlr_mutex);
  1728. switch (event) {
  1729. case RPORT_EV_READY:
  1730. frport->login_count = 0;
  1731. break;
  1732. case RPORT_EV_LOGO:
  1733. case RPORT_EV_FAILED:
  1734. case RPORT_EV_STOP:
  1735. frport->login_count++;
  1736. if (frport->login_count > FCOE_CTLR_VN2VN_LOGIN_LIMIT) {
  1737. LIBFCOE_FIP_DBG(fip,
  1738. "rport FLOGI limited port_id %6.6x\n",
  1739. rdata->ids.port_id);
  1740. lport->tt.rport_logoff(rdata);
  1741. }
  1742. break;
  1743. default:
  1744. break;
  1745. }
  1746. mutex_unlock(&fip->ctlr_mutex);
  1747. }
  1748. static struct fc_rport_operations fcoe_ctlr_vn_rport_ops = {
  1749. .event_callback = fcoe_ctlr_vn_rport_callback,
  1750. };
  1751. /**
  1752. * fcoe_ctlr_disc_stop_locked() - stop discovery in VN2VN mode
  1753. * @fip: The FCoE controller
  1754. *
  1755. * Called with ctlr_mutex held.
  1756. */
  1757. static void fcoe_ctlr_disc_stop_locked(struct fc_lport *lport)
  1758. {
  1759. mutex_lock(&lport->disc.disc_mutex);
  1760. lport->disc.disc_callback = NULL;
  1761. mutex_unlock(&lport->disc.disc_mutex);
  1762. }
  1763. /**
  1764. * fcoe_ctlr_disc_stop() - stop discovery in VN2VN mode
  1765. * @fip: The FCoE controller
  1766. *
  1767. * Called through the local port template for discovery.
  1768. * Called without the ctlr_mutex held.
  1769. */
  1770. static void fcoe_ctlr_disc_stop(struct fc_lport *lport)
  1771. {
  1772. struct fcoe_ctlr *fip = lport->disc.priv;
  1773. mutex_lock(&fip->ctlr_mutex);
  1774. fcoe_ctlr_disc_stop_locked(lport);
  1775. mutex_unlock(&fip->ctlr_mutex);
  1776. }
  1777. /**
  1778. * fcoe_ctlr_disc_stop_final() - stop discovery for shutdown in VN2VN mode
  1779. * @fip: The FCoE controller
  1780. *
  1781. * Called through the local port template for discovery.
  1782. * Called without the ctlr_mutex held.
  1783. */
  1784. static void fcoe_ctlr_disc_stop_final(struct fc_lport *lport)
  1785. {
  1786. fcoe_ctlr_disc_stop(lport);
  1787. lport->tt.rport_flush_queue();
  1788. synchronize_rcu();
  1789. }
  1790. /**
  1791. * fcoe_ctlr_vn_restart() - VN2VN probe restart with new port_id
  1792. * @fip: The FCoE controller
  1793. *
  1794. * Called with fcoe_ctlr lock held.
  1795. */
  1796. static void fcoe_ctlr_vn_restart(struct fcoe_ctlr *fip)
  1797. {
  1798. unsigned long wait;
  1799. u32 port_id;
  1800. fcoe_ctlr_disc_stop_locked(fip->lp);
  1801. /*
  1802. * Get proposed port ID.
  1803. * If this is the first try after link up, use any previous port_id.
  1804. * If there was none, use the low bits of the port_name.
  1805. * On subsequent tries, get the next random one.
  1806. * Don't use reserved IDs, use another non-zero value, just as random.
  1807. */
  1808. port_id = fip->port_id;
  1809. if (fip->probe_tries)
  1810. port_id = prandom32(&fip->rnd_state) & 0xffff;
  1811. else if (!port_id)
  1812. port_id = fip->lp->wwpn & 0xffff;
  1813. if (!port_id || port_id == 0xffff)
  1814. port_id = 1;
  1815. fip->port_id = port_id;
  1816. if (fip->probe_tries < FIP_VN_RLIM_COUNT) {
  1817. fip->probe_tries++;
  1818. wait = random32() % FIP_VN_PROBE_WAIT;
  1819. } else
  1820. wait = FIP_VN_RLIM_INT;
  1821. mod_timer(&fip->timer, jiffies + msecs_to_jiffies(wait));
  1822. fcoe_ctlr_set_state(fip, FIP_ST_VNMP_START);
  1823. }
  1824. /**
  1825. * fcoe_ctlr_vn_start() - Start in VN2VN mode
  1826. * @fip: The FCoE controller
  1827. *
  1828. * Called with fcoe_ctlr lock held.
  1829. */
  1830. static void fcoe_ctlr_vn_start(struct fcoe_ctlr *fip)
  1831. {
  1832. fip->probe_tries = 0;
  1833. prandom32_seed(&fip->rnd_state, fip->lp->wwpn);
  1834. fcoe_ctlr_vn_restart(fip);
  1835. }
  1836. /**
  1837. * fcoe_ctlr_vn_parse - parse probe request or response
  1838. * @fip: The FCoE controller
  1839. * @skb: incoming packet
  1840. * @rdata: buffer for resulting parsed VN entry plus fcoe_rport
  1841. *
  1842. * Returns non-zero error number on error.
  1843. * Does not consume the packet.
  1844. */
  1845. static int fcoe_ctlr_vn_parse(struct fcoe_ctlr *fip,
  1846. struct sk_buff *skb,
  1847. struct fc_rport_priv *rdata)
  1848. {
  1849. struct fip_header *fiph;
  1850. struct fip_desc *desc = NULL;
  1851. struct fip_mac_desc *macd = NULL;
  1852. struct fip_wwn_desc *wwn = NULL;
  1853. struct fip_vn_desc *vn = NULL;
  1854. struct fip_size_desc *size = NULL;
  1855. struct fcoe_rport *frport;
  1856. size_t rlen;
  1857. size_t dlen;
  1858. u32 desc_mask = 0;
  1859. u32 dtype;
  1860. u8 sub;
  1861. memset(rdata, 0, sizeof(*rdata) + sizeof(*frport));
  1862. frport = fcoe_ctlr_rport(rdata);
  1863. fiph = (struct fip_header *)skb->data;
  1864. frport->flags = ntohs(fiph->fip_flags);
  1865. sub = fiph->fip_subcode;
  1866. switch (sub) {
  1867. case FIP_SC_VN_PROBE_REQ:
  1868. case FIP_SC_VN_PROBE_REP:
  1869. case FIP_SC_VN_BEACON:
  1870. desc_mask = BIT(FIP_DT_MAC) | BIT(FIP_DT_NAME) |
  1871. BIT(FIP_DT_VN_ID);
  1872. break;
  1873. case FIP_SC_VN_CLAIM_NOTIFY:
  1874. case FIP_SC_VN_CLAIM_REP:
  1875. desc_mask = BIT(FIP_DT_MAC) | BIT(FIP_DT_NAME) |
  1876. BIT(FIP_DT_VN_ID) | BIT(FIP_DT_FC4F) |
  1877. BIT(FIP_DT_FCOE_SIZE);
  1878. break;
  1879. default:
  1880. LIBFCOE_FIP_DBG(fip, "vn_parse unknown subcode %u\n", sub);
  1881. return -EINVAL;
  1882. }
  1883. rlen = ntohs(fiph->fip_dl_len) * 4;
  1884. if (rlen + sizeof(*fiph) > skb->len)
  1885. return -EINVAL;
  1886. desc = (struct fip_desc *)(fiph + 1);
  1887. while (rlen > 0) {
  1888. dlen = desc->fip_dlen * FIP_BPW;
  1889. if (dlen < sizeof(*desc) || dlen > rlen)
  1890. return -EINVAL;
  1891. dtype = desc->fip_dtype;
  1892. if (dtype < 32) {
  1893. if (!(desc_mask & BIT(dtype))) {
  1894. LIBFCOE_FIP_DBG(fip,
  1895. "unexpected or duplicated desc "
  1896. "desc type %u in "
  1897. "FIP VN2VN subtype %u\n",
  1898. dtype, sub);
  1899. return -EINVAL;
  1900. }
  1901. desc_mask &= ~BIT(dtype);
  1902. }
  1903. switch (dtype) {
  1904. case FIP_DT_MAC:
  1905. if (dlen != sizeof(struct fip_mac_desc))
  1906. goto len_err;
  1907. macd = (struct fip_mac_desc *)desc;
  1908. if (!is_valid_ether_addr(macd->fd_mac)) {
  1909. LIBFCOE_FIP_DBG(fip,
  1910. "Invalid MAC addr %pM in FIP VN2VN\n",
  1911. macd->fd_mac);
  1912. return -EINVAL;
  1913. }
  1914. memcpy(frport->enode_mac, macd->fd_mac, ETH_ALEN);
  1915. break;
  1916. case FIP_DT_NAME:
  1917. if (dlen != sizeof(struct fip_wwn_desc))
  1918. goto len_err;
  1919. wwn = (struct fip_wwn_desc *)desc;
  1920. rdata->ids.node_name = get_unaligned_be64(&wwn->fd_wwn);
  1921. break;
  1922. case FIP_DT_VN_ID:
  1923. if (dlen != sizeof(struct fip_vn_desc))
  1924. goto len_err;
  1925. vn = (struct fip_vn_desc *)desc;
  1926. memcpy(frport->vn_mac, vn->fd_mac, ETH_ALEN);
  1927. rdata->ids.port_id = ntoh24(vn->fd_fc_id);
  1928. rdata->ids.port_name = get_unaligned_be64(&vn->fd_wwpn);
  1929. break;
  1930. case FIP_DT_FC4F:
  1931. if (dlen != sizeof(struct fip_fc4_feat))
  1932. goto len_err;
  1933. break;
  1934. case FIP_DT_FCOE_SIZE:
  1935. if (dlen != sizeof(struct fip_size_desc))
  1936. goto len_err;
  1937. size = (struct fip_size_desc *)desc;
  1938. frport->fcoe_len = ntohs(size->fd_size);
  1939. break;
  1940. default:
  1941. LIBFCOE_FIP_DBG(fip, "unexpected descriptor type %x "
  1942. "in FIP probe\n", dtype);
  1943. /* standard says ignore unknown descriptors >= 128 */
  1944. if (dtype < FIP_DT_VENDOR_BASE)
  1945. return -EINVAL;
  1946. break;
  1947. }
  1948. desc = (struct fip_desc *)((char *)desc + dlen);
  1949. rlen -= dlen;
  1950. }
  1951. return 0;
  1952. len_err:
  1953. LIBFCOE_FIP_DBG(fip, "FIP length error in descriptor type %x len %zu\n",
  1954. dtype, dlen);
  1955. return -EINVAL;
  1956. }
  1957. /**
  1958. * fcoe_ctlr_vn_send_claim() - send multicast FIP VN2VN Claim Notification.
  1959. * @fip: The FCoE controller
  1960. *
  1961. * Called with ctlr_mutex held.
  1962. */
  1963. static void fcoe_ctlr_vn_send_claim(struct fcoe_ctlr *fip)
  1964. {
  1965. fcoe_ctlr_vn_send(fip, FIP_SC_VN_CLAIM_NOTIFY, fcoe_all_vn2vn, 0);
  1966. fip->sol_time = jiffies;
  1967. }
  1968. /**
  1969. * fcoe_ctlr_vn_probe_req() - handle incoming VN2VN probe request.
  1970. * @fip: The FCoE controller
  1971. * @rdata: parsed remote port with frport from the probe request
  1972. *
  1973. * Called with ctlr_mutex held.
  1974. */
  1975. static void fcoe_ctlr_vn_probe_req(struct fcoe_ctlr *fip,
  1976. struct fc_rport_priv *rdata)
  1977. {
  1978. struct fcoe_rport *frport = fcoe_ctlr_rport(rdata);
  1979. if (rdata->ids.port_id != fip->port_id)
  1980. return;
  1981. switch (fip->state) {
  1982. case FIP_ST_VNMP_CLAIM:
  1983. case FIP_ST_VNMP_UP:
  1984. fcoe_ctlr_vn_send(fip, FIP_SC_VN_PROBE_REP,
  1985. frport->enode_mac, 0);
  1986. break;
  1987. case FIP_ST_VNMP_PROBE1:
  1988. case FIP_ST_VNMP_PROBE2:
  1989. /*
  1990. * Decide whether to reply to the Probe.
  1991. * Our selected address is never a "recorded" one, so
  1992. * only reply if our WWPN is greater and the
  1993. * Probe's REC bit is not set.
  1994. * If we don't reply, we will change our address.
  1995. */
  1996. if (fip->lp->wwpn > rdata->ids.port_name &&
  1997. !(frport->flags & FIP_FL_REC_OR_P2P)) {
  1998. fcoe_ctlr_vn_send(fip, FIP_SC_VN_PROBE_REP,
  1999. frport->enode_mac, 0);
  2000. break;
  2001. }
  2002. /* fall through */
  2003. case FIP_ST_VNMP_START:
  2004. fcoe_ctlr_vn_restart(fip);
  2005. break;
  2006. default:
  2007. break;
  2008. }
  2009. }
  2010. /**
  2011. * fcoe_ctlr_vn_probe_reply() - handle incoming VN2VN probe reply.
  2012. * @fip: The FCoE controller
  2013. * @rdata: parsed remote port with frport from the probe request
  2014. *
  2015. * Called with ctlr_mutex held.
  2016. */
  2017. static void fcoe_ctlr_vn_probe_reply(struct fcoe_ctlr *fip,
  2018. struct fc_rport_priv *rdata)
  2019. {
  2020. if (rdata->ids.port_id != fip->port_id)
  2021. return;
  2022. switch (fip->state) {
  2023. case FIP_ST_VNMP_START:
  2024. case FIP_ST_VNMP_PROBE1:
  2025. case FIP_ST_VNMP_PROBE2:
  2026. case FIP_ST_VNMP_CLAIM:
  2027. fcoe_ctlr_vn_restart(fip);
  2028. break;
  2029. case FIP_ST_VNMP_UP:
  2030. fcoe_ctlr_vn_send_claim(fip);
  2031. break;
  2032. default:
  2033. break;
  2034. }
  2035. }
  2036. /**
  2037. * fcoe_ctlr_vn_add() - Add a VN2VN entry to the list, based on a claim reply.
  2038. * @fip: The FCoE controller
  2039. * @new: newly-parsed remote port with frport as a template for new rdata
  2040. *
  2041. * Called with ctlr_mutex held.
  2042. */
  2043. static void fcoe_ctlr_vn_add(struct fcoe_ctlr *fip, struct fc_rport_priv *new)
  2044. {
  2045. struct fc_lport *lport = fip->lp;
  2046. struct fc_rport_priv *rdata;
  2047. struct fc_rport_identifiers *ids;
  2048. struct fcoe_rport *frport;
  2049. u32 port_id;
  2050. port_id = new->ids.port_id;
  2051. if (port_id == fip->port_id)
  2052. return;
  2053. mutex_lock(&lport->disc.disc_mutex);
  2054. rdata = lport->tt.rport_create(lport, port_id);
  2055. if (!rdata) {
  2056. mutex_unlock(&lport->disc.disc_mutex);
  2057. return;
  2058. }
  2059. rdata->ops = &fcoe_ctlr_vn_rport_ops;
  2060. rdata->disc_id = lport->disc.disc_id;
  2061. ids = &rdata->ids;
  2062. if ((ids->port_name != -1 && ids->port_name != new->ids.port_name) ||
  2063. (ids->node_name != -1 && ids->node_name != new->ids.node_name))
  2064. lport->tt.rport_logoff(rdata);
  2065. ids->port_name = new->ids.port_name;
  2066. ids->node_name = new->ids.node_name;
  2067. mutex_unlock(&lport->disc.disc_mutex);
  2068. frport = fcoe_ctlr_rport(rdata);
  2069. LIBFCOE_FIP_DBG(fip, "vn_add rport %6.6x %s\n",
  2070. port_id, frport->fcoe_len ? "old" : "new");
  2071. *frport = *fcoe_ctlr_rport(new);
  2072. frport->time = 0;
  2073. }
  2074. /**
  2075. * fcoe_ctlr_vn_lookup() - Find VN remote port's MAC address
  2076. * @fip: The FCoE controller
  2077. * @port_id: The port_id of the remote VN_node
  2078. * @mac: buffer which will hold the VN_NODE destination MAC address, if found.
  2079. *
  2080. * Returns non-zero error if no remote port found.
  2081. */
  2082. static int fcoe_ctlr_vn_lookup(struct fcoe_ctlr *fip, u32 port_id, u8 *mac)
  2083. {
  2084. struct fc_lport *lport = fip->lp;
  2085. struct fc_rport_priv *rdata;
  2086. struct fcoe_rport *frport;
  2087. int ret = -1;
  2088. rcu_read_lock();
  2089. rdata = lport->tt.rport_lookup(lport, port_id);
  2090. if (rdata) {
  2091. frport = fcoe_ctlr_rport(rdata);
  2092. memcpy(mac, frport->enode_mac, ETH_ALEN);
  2093. ret = 0;
  2094. }
  2095. rcu_read_unlock();
  2096. return ret;
  2097. }
  2098. /**
  2099. * fcoe_ctlr_vn_claim_notify() - handle received FIP VN2VN Claim Notification
  2100. * @fip: The FCoE controller
  2101. * @new: newly-parsed remote port with frport as a template for new rdata
  2102. *
  2103. * Called with ctlr_mutex held.
  2104. */
  2105. static void fcoe_ctlr_vn_claim_notify(struct fcoe_ctlr *fip,
  2106. struct fc_rport_priv *new)
  2107. {
  2108. struct fcoe_rport *frport = fcoe_ctlr_rport(new);
  2109. if (frport->flags & FIP_FL_REC_OR_P2P) {
  2110. fcoe_ctlr_vn_send(fip, FIP_SC_VN_PROBE_REQ, fcoe_all_vn2vn, 0);
  2111. return;
  2112. }
  2113. switch (fip->state) {
  2114. case FIP_ST_VNMP_START:
  2115. case FIP_ST_VNMP_PROBE1:
  2116. case FIP_ST_VNMP_PROBE2:
  2117. if (new->ids.port_id == fip->port_id)
  2118. fcoe_ctlr_vn_restart(fip);
  2119. break;
  2120. case FIP_ST_VNMP_CLAIM:
  2121. case FIP_ST_VNMP_UP:
  2122. if (new->ids.port_id == fip->port_id) {
  2123. if (new->ids.port_name > fip->lp->wwpn) {
  2124. fcoe_ctlr_vn_restart(fip);
  2125. break;
  2126. }
  2127. fcoe_ctlr_vn_send_claim(fip);
  2128. break;
  2129. }
  2130. fcoe_ctlr_vn_send(fip, FIP_SC_VN_CLAIM_REP, frport->enode_mac,
  2131. min((u32)frport->fcoe_len,
  2132. fcoe_ctlr_fcoe_size(fip)));
  2133. fcoe_ctlr_vn_add(fip, new);
  2134. break;
  2135. default:
  2136. break;
  2137. }
  2138. }
  2139. /**
  2140. * fcoe_ctlr_vn_claim_resp() - handle received Claim Response
  2141. * @fip: The FCoE controller that received the frame
  2142. * @new: newly-parsed remote port with frport from the Claim Response
  2143. *
  2144. * Called with ctlr_mutex held.
  2145. */
  2146. static void fcoe_ctlr_vn_claim_resp(struct fcoe_ctlr *fip,
  2147. struct fc_rport_priv *new)
  2148. {
  2149. LIBFCOE_FIP_DBG(fip, "claim resp from from rport %x - state %s\n",
  2150. new->ids.port_id, fcoe_ctlr_state(fip->state));
  2151. if (fip->state == FIP_ST_VNMP_UP || fip->state == FIP_ST_VNMP_CLAIM)
  2152. fcoe_ctlr_vn_add(fip, new);
  2153. }
  2154. /**
  2155. * fcoe_ctlr_vn_beacon() - handle received beacon.
  2156. * @fip: The FCoE controller that received the frame
  2157. * @new: newly-parsed remote port with frport from the Beacon
  2158. *
  2159. * Called with ctlr_mutex held.
  2160. */
  2161. static void fcoe_ctlr_vn_beacon(struct fcoe_ctlr *fip,
  2162. struct fc_rport_priv *new)
  2163. {
  2164. struct fc_lport *lport = fip->lp;
  2165. struct fc_rport_priv *rdata;
  2166. struct fcoe_rport *frport;
  2167. frport = fcoe_ctlr_rport(new);
  2168. if (frport->flags & FIP_FL_REC_OR_P2P) {
  2169. fcoe_ctlr_vn_send(fip, FIP_SC_VN_PROBE_REQ, fcoe_all_vn2vn, 0);
  2170. return;
  2171. }
  2172. mutex_lock(&lport->disc.disc_mutex);
  2173. rdata = lport->tt.rport_lookup(lport, new->ids.port_id);
  2174. if (rdata)
  2175. kref_get(&rdata->kref);
  2176. mutex_unlock(&lport->disc.disc_mutex);
  2177. if (rdata) {
  2178. if (rdata->ids.node_name == new->ids.node_name &&
  2179. rdata->ids.port_name == new->ids.port_name) {
  2180. frport = fcoe_ctlr_rport(rdata);
  2181. if (!frport->time && fip->state == FIP_ST_VNMP_UP)
  2182. lport->tt.rport_login(rdata);
  2183. frport->time = jiffies;
  2184. }
  2185. kref_put(&rdata->kref, lport->tt.rport_destroy);
  2186. return;
  2187. }
  2188. if (fip->state != FIP_ST_VNMP_UP)
  2189. return;
  2190. /*
  2191. * Beacon from a new neighbor.
  2192. * Send a claim notify if one hasn't been sent recently.
  2193. * Don't add the neighbor yet.
  2194. */
  2195. LIBFCOE_FIP_DBG(fip, "beacon from new rport %x. sending claim notify\n",
  2196. new->ids.port_id);
  2197. if (time_after(jiffies,
  2198. fip->sol_time + msecs_to_jiffies(FIP_VN_ANN_WAIT)))
  2199. fcoe_ctlr_vn_send_claim(fip);
  2200. }
  2201. /**
  2202. * fcoe_ctlr_vn_age() - Check for VN_ports without recent beacons
  2203. * @fip: The FCoE controller
  2204. *
  2205. * Called with ctlr_mutex held.
  2206. * Called only in state FIP_ST_VNMP_UP.
  2207. * Returns the soonest time for next age-out or a time far in the future.
  2208. */
  2209. static unsigned long fcoe_ctlr_vn_age(struct fcoe_ctlr *fip)
  2210. {
  2211. struct fc_lport *lport = fip->lp;
  2212. struct fc_rport_priv *rdata;
  2213. struct fcoe_rport *frport;
  2214. unsigned long next_time;
  2215. unsigned long deadline;
  2216. next_time = jiffies + msecs_to_jiffies(FIP_VN_BEACON_INT * 10);
  2217. mutex_lock(&lport->disc.disc_mutex);
  2218. list_for_each_entry_rcu(rdata, &lport->disc.rports, peers) {
  2219. frport = fcoe_ctlr_rport(rdata);
  2220. if (!frport->time)
  2221. continue;
  2222. deadline = frport->time +
  2223. msecs_to_jiffies(FIP_VN_BEACON_INT * 25 / 10);
  2224. if (time_after_eq(jiffies, deadline)) {
  2225. frport->time = 0;
  2226. LIBFCOE_FIP_DBG(fip,
  2227. "port %16.16llx fc_id %6.6x beacon expired\n",
  2228. rdata->ids.port_name, rdata->ids.port_id);
  2229. lport->tt.rport_logoff(rdata);
  2230. } else if (time_before(deadline, next_time))
  2231. next_time = deadline;
  2232. }
  2233. mutex_unlock(&lport->disc.disc_mutex);
  2234. return next_time;
  2235. }
  2236. /**
  2237. * fcoe_ctlr_vn_recv() - Receive a FIP frame
  2238. * @fip: The FCoE controller that received the frame
  2239. * @skb: The received FIP frame
  2240. *
  2241. * Returns non-zero if the frame is dropped.
  2242. * Always consumes the frame.
  2243. */
  2244. static int fcoe_ctlr_vn_recv(struct fcoe_ctlr *fip, struct sk_buff *skb)
  2245. {
  2246. struct fip_header *fiph;
  2247. enum fip_vn2vn_subcode sub;
  2248. struct {
  2249. struct fc_rport_priv rdata;
  2250. struct fcoe_rport frport;
  2251. } buf;
  2252. int rc;
  2253. fiph = (struct fip_header *)skb->data;
  2254. sub = fiph->fip_subcode;
  2255. rc = fcoe_ctlr_vn_parse(fip, skb, &buf.rdata);
  2256. if (rc) {
  2257. LIBFCOE_FIP_DBG(fip, "vn_recv vn_parse error %d\n", rc);
  2258. goto drop;
  2259. }
  2260. mutex_lock(&fip->ctlr_mutex);
  2261. switch (sub) {
  2262. case FIP_SC_VN_PROBE_REQ:
  2263. fcoe_ctlr_vn_probe_req(fip, &buf.rdata);
  2264. break;
  2265. case FIP_SC_VN_PROBE_REP:
  2266. fcoe_ctlr_vn_probe_reply(fip, &buf.rdata);
  2267. break;
  2268. case FIP_SC_VN_CLAIM_NOTIFY:
  2269. fcoe_ctlr_vn_claim_notify(fip, &buf.rdata);
  2270. break;
  2271. case FIP_SC_VN_CLAIM_REP:
  2272. fcoe_ctlr_vn_claim_resp(fip, &buf.rdata);
  2273. break;
  2274. case FIP_SC_VN_BEACON:
  2275. fcoe_ctlr_vn_beacon(fip, &buf.rdata);
  2276. break;
  2277. default:
  2278. LIBFCOE_FIP_DBG(fip, "vn_recv unknown subcode %d\n", sub);
  2279. rc = -1;
  2280. break;
  2281. }
  2282. mutex_unlock(&fip->ctlr_mutex);
  2283. drop:
  2284. kfree_skb(skb);
  2285. return rc;
  2286. }
  2287. /**
  2288. * fcoe_ctlr_disc_recv - discovery receive handler for VN2VN mode.
  2289. * @lport: The local port
  2290. * @fp: The received frame
  2291. *
  2292. * This should never be called since we don't see RSCNs or other
  2293. * fabric-generated ELSes.
  2294. */
  2295. static void fcoe_ctlr_disc_recv(struct fc_lport *lport, struct fc_frame *fp)
  2296. {
  2297. struct fc_seq_els_data rjt_data;
  2298. rjt_data.reason = ELS_RJT_UNSUP;
  2299. rjt_data.explan = ELS_EXPL_NONE;
  2300. lport->tt.seq_els_rsp_send(fp, ELS_LS_RJT, &rjt_data);
  2301. fc_frame_free(fp);
  2302. }
  2303. /**
  2304. * fcoe_ctlr_disc_recv - start discovery for VN2VN mode.
  2305. * @fip: The FCoE controller
  2306. *
  2307. * This sets a flag indicating that remote ports should be created
  2308. * and started for the peers we discover. We use the disc_callback
  2309. * pointer as that flag. Peers already discovered are created here.
  2310. *
  2311. * The lport lock is held during this call. The callback must be done
  2312. * later, without holding either the lport or discovery locks.
  2313. * The fcoe_ctlr lock may also be held during this call.
  2314. */
  2315. static void fcoe_ctlr_disc_start(void (*callback)(struct fc_lport *,
  2316. enum fc_disc_event),
  2317. struct fc_lport *lport)
  2318. {
  2319. struct fc_disc *disc = &lport->disc;
  2320. struct fcoe_ctlr *fip = disc->priv;
  2321. mutex_lock(&disc->disc_mutex);
  2322. disc->disc_callback = callback;
  2323. disc->disc_id = (disc->disc_id + 2) | 1;
  2324. disc->pending = 1;
  2325. schedule_work(&fip->timer_work);
  2326. mutex_unlock(&disc->disc_mutex);
  2327. }
  2328. /**
  2329. * fcoe_ctlr_vn_disc() - report FIP VN_port discovery results after claim state.
  2330. * @fip: The FCoE controller
  2331. *
  2332. * Starts the FLOGI and PLOGI login process to each discovered rport for which
  2333. * we've received at least one beacon.
  2334. * Performs the discovery complete callback.
  2335. */
  2336. static void fcoe_ctlr_vn_disc(struct fcoe_ctlr *fip)
  2337. {
  2338. struct fc_lport *lport = fip->lp;
  2339. struct fc_disc *disc = &lport->disc;
  2340. struct fc_rport_priv *rdata;
  2341. struct fcoe_rport *frport;
  2342. void (*callback)(struct fc_lport *, enum fc_disc_event);
  2343. mutex_lock(&disc->disc_mutex);
  2344. callback = disc->pending ? disc->disc_callback : NULL;
  2345. disc->pending = 0;
  2346. list_for_each_entry_rcu(rdata, &disc->rports, peers) {
  2347. frport = fcoe_ctlr_rport(rdata);
  2348. if (frport->time)
  2349. lport->tt.rport_login(rdata);
  2350. }
  2351. mutex_unlock(&disc->disc_mutex);
  2352. if (callback)
  2353. callback(lport, DISC_EV_SUCCESS);
  2354. }
  2355. /**
  2356. * fcoe_ctlr_vn_timeout - timer work function for VN2VN mode.
  2357. * @fip: The FCoE controller
  2358. */
  2359. static void fcoe_ctlr_vn_timeout(struct fcoe_ctlr *fip)
  2360. {
  2361. unsigned long next_time;
  2362. u8 mac[ETH_ALEN];
  2363. u32 new_port_id = 0;
  2364. mutex_lock(&fip->ctlr_mutex);
  2365. switch (fip->state) {
  2366. case FIP_ST_VNMP_START:
  2367. fcoe_ctlr_set_state(fip, FIP_ST_VNMP_PROBE1);
  2368. fcoe_ctlr_vn_send(fip, FIP_SC_VN_PROBE_REQ, fcoe_all_vn2vn, 0);
  2369. next_time = jiffies + msecs_to_jiffies(FIP_VN_PROBE_WAIT);
  2370. break;
  2371. case FIP_ST_VNMP_PROBE1:
  2372. fcoe_ctlr_set_state(fip, FIP_ST_VNMP_PROBE2);
  2373. fcoe_ctlr_vn_send(fip, FIP_SC_VN_PROBE_REQ, fcoe_all_vn2vn, 0);
  2374. next_time = jiffies + msecs_to_jiffies(FIP_VN_ANN_WAIT);
  2375. break;
  2376. case FIP_ST_VNMP_PROBE2:
  2377. fcoe_ctlr_set_state(fip, FIP_ST_VNMP_CLAIM);
  2378. new_port_id = fip->port_id;
  2379. hton24(mac, FIP_VN_FC_MAP);
  2380. hton24(mac + 3, new_port_id);
  2381. fcoe_ctlr_map_dest(fip);
  2382. fip->update_mac(fip->lp, mac);
  2383. fcoe_ctlr_vn_send_claim(fip);
  2384. next_time = jiffies + msecs_to_jiffies(FIP_VN_ANN_WAIT);
  2385. break;
  2386. case FIP_ST_VNMP_CLAIM:
  2387. /*
  2388. * This may be invoked either by starting discovery so don't
  2389. * go to the next state unless it's been long enough.
  2390. */
  2391. next_time = fip->sol_time + msecs_to_jiffies(FIP_VN_ANN_WAIT);
  2392. if (time_after_eq(jiffies, next_time)) {
  2393. fcoe_ctlr_set_state(fip, FIP_ST_VNMP_UP);
  2394. fcoe_ctlr_vn_send(fip, FIP_SC_VN_BEACON,
  2395. fcoe_all_vn2vn, 0);
  2396. next_time = jiffies + msecs_to_jiffies(FIP_VN_ANN_WAIT);
  2397. fip->port_ka_time = next_time;
  2398. }
  2399. fcoe_ctlr_vn_disc(fip);
  2400. break;
  2401. case FIP_ST_VNMP_UP:
  2402. next_time = fcoe_ctlr_vn_age(fip);
  2403. if (time_after_eq(jiffies, fip->port_ka_time)) {
  2404. fcoe_ctlr_vn_send(fip, FIP_SC_VN_BEACON,
  2405. fcoe_all_vn2vn, 0);
  2406. fip->port_ka_time = jiffies +
  2407. msecs_to_jiffies(FIP_VN_BEACON_INT +
  2408. (random32() % FIP_VN_BEACON_FUZZ));
  2409. }
  2410. if (time_before(fip->port_ka_time, next_time))
  2411. next_time = fip->port_ka_time;
  2412. break;
  2413. case FIP_ST_LINK_WAIT:
  2414. goto unlock;
  2415. default:
  2416. WARN(1, "unexpected state %d\n", fip->state);
  2417. goto unlock;
  2418. }
  2419. mod_timer(&fip->timer, next_time);
  2420. unlock:
  2421. mutex_unlock(&fip->ctlr_mutex);
  2422. /* If port ID is new, notify local port after dropping ctlr_mutex */
  2423. if (new_port_id)
  2424. fc_lport_set_local_id(fip->lp, new_port_id);
  2425. }
  2426. /**
  2427. * fcoe_libfc_config() - Sets up libfc related properties for local port
  2428. * @lp: The local port to configure libfc for
  2429. * @fip: The FCoE controller in use by the local port
  2430. * @tt: The libfc function template
  2431. * @init_fcp: If non-zero, the FCP portion of libfc should be initialized
  2432. *
  2433. * Returns : 0 for success
  2434. */
  2435. int fcoe_libfc_config(struct fc_lport *lport, struct fcoe_ctlr *fip,
  2436. const struct libfc_function_template *tt, int init_fcp)
  2437. {
  2438. /* Set the function pointers set by the LLDD */
  2439. memcpy(&lport->tt, tt, sizeof(*tt));
  2440. if (init_fcp && fc_fcp_init(lport))
  2441. return -ENOMEM;
  2442. fc_exch_init(lport);
  2443. fc_elsct_init(lport);
  2444. fc_lport_init(lport);
  2445. if (fip->mode == FIP_MODE_VN2VN)
  2446. lport->rport_priv_size = sizeof(struct fcoe_rport);
  2447. fc_rport_init(lport);
  2448. if (fip->mode == FIP_MODE_VN2VN) {
  2449. lport->point_to_multipoint = 1;
  2450. lport->tt.disc_recv_req = fcoe_ctlr_disc_recv;
  2451. lport->tt.disc_start = fcoe_ctlr_disc_start;
  2452. lport->tt.disc_stop = fcoe_ctlr_disc_stop;
  2453. lport->tt.disc_stop_final = fcoe_ctlr_disc_stop_final;
  2454. mutex_init(&lport->disc.disc_mutex);
  2455. INIT_LIST_HEAD(&lport->disc.rports);
  2456. lport->disc.priv = fip;
  2457. } else {
  2458. fc_disc_init(lport);
  2459. }
  2460. return 0;
  2461. }
  2462. EXPORT_SYMBOL_GPL(fcoe_libfc_config);