fcoe_ctlr.c 78 KB

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