fcoe_ctlr.c 78 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892
  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 fc_stats *stats;
  717. INIT_LIST_HEAD(&del_list);
  718. stats = per_cpu_ptr(fip->lp->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_fcf *sel;
  1006. struct fc_stats *stats;
  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->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. /*
  1178. * We are yet to select best FCF, but we got CVL in the
  1179. * meantime. reset the ctlr and let it rediscover the FCF
  1180. */
  1181. mutex_lock(&fip->ctlr_mutex);
  1182. fcoe_ctlr_reset(fip);
  1183. mutex_unlock(&fip->ctlr_mutex);
  1184. return;
  1185. }
  1186. /*
  1187. * mask of required descriptors. Validating each one clears its bit.
  1188. */
  1189. desc_mask = BIT(FIP_DT_MAC) | BIT(FIP_DT_NAME);
  1190. rlen = ntohs(fh->fip_dl_len) * FIP_BPW;
  1191. desc = (struct fip_desc *)(fh + 1);
  1192. /*
  1193. * Actually need to subtract 'sizeof(*mp) - sizeof(*wp)' from 'rlen'
  1194. * before determining max Vx_Port descriptor but a buggy FCF could have
  1195. * omited either or both MAC Address and Name Identifier descriptors
  1196. */
  1197. num_vlink_desc = rlen / sizeof(*vp);
  1198. if (num_vlink_desc)
  1199. vlink_desc_arr = kmalloc(sizeof(vp) * num_vlink_desc,
  1200. GFP_ATOMIC);
  1201. if (!vlink_desc_arr)
  1202. return;
  1203. num_vlink_desc = 0;
  1204. while (rlen >= sizeof(*desc)) {
  1205. dlen = desc->fip_dlen * FIP_BPW;
  1206. if (dlen > rlen)
  1207. goto err;
  1208. /* Drop CVL if there are duplicate critical descriptors */
  1209. if ((desc->fip_dtype < 32) &&
  1210. (desc->fip_dtype != FIP_DT_VN_ID) &&
  1211. !(desc_mask & 1U << desc->fip_dtype)) {
  1212. LIBFCOE_FIP_DBG(fip, "Duplicate Critical "
  1213. "Descriptors in FIP CVL\n");
  1214. goto err;
  1215. }
  1216. switch (desc->fip_dtype) {
  1217. case FIP_DT_MAC:
  1218. mp = (struct fip_mac_desc *)desc;
  1219. if (dlen < sizeof(*mp))
  1220. goto err;
  1221. if (compare_ether_addr(mp->fd_mac, fcf->fcf_mac))
  1222. goto err;
  1223. desc_mask &= ~BIT(FIP_DT_MAC);
  1224. break;
  1225. case FIP_DT_NAME:
  1226. wp = (struct fip_wwn_desc *)desc;
  1227. if (dlen < sizeof(*wp))
  1228. goto err;
  1229. if (get_unaligned_be64(&wp->fd_wwn) != fcf->switch_name)
  1230. goto err;
  1231. desc_mask &= ~BIT(FIP_DT_NAME);
  1232. break;
  1233. case FIP_DT_VN_ID:
  1234. vp = (struct fip_vn_desc *)desc;
  1235. if (dlen < sizeof(*vp))
  1236. goto err;
  1237. vlink_desc_arr[num_vlink_desc++] = vp;
  1238. vn_port = fc_vport_id_lookup(lport,
  1239. ntoh24(vp->fd_fc_id));
  1240. if (vn_port && (vn_port == lport)) {
  1241. mutex_lock(&fip->ctlr_mutex);
  1242. per_cpu_ptr(lport->stats,
  1243. get_cpu())->VLinkFailureCount++;
  1244. put_cpu();
  1245. fcoe_ctlr_reset(fip);
  1246. mutex_unlock(&fip->ctlr_mutex);
  1247. }
  1248. break;
  1249. default:
  1250. /* standard says ignore unknown descriptors >= 128 */
  1251. if (desc->fip_dtype < FIP_DT_VENDOR_BASE)
  1252. goto err;
  1253. break;
  1254. }
  1255. desc = (struct fip_desc *)((char *)desc + dlen);
  1256. rlen -= dlen;
  1257. }
  1258. /*
  1259. * reset only if all required descriptors were present and valid.
  1260. */
  1261. if (desc_mask)
  1262. LIBFCOE_FIP_DBG(fip, "missing descriptors mask %x\n",
  1263. desc_mask);
  1264. else if (!num_vlink_desc) {
  1265. LIBFCOE_FIP_DBG(fip, "CVL: no Vx_Port descriptor found\n");
  1266. /*
  1267. * No Vx_Port description. Clear all NPIV ports,
  1268. * followed by physical port
  1269. */
  1270. mutex_lock(&fip->ctlr_mutex);
  1271. per_cpu_ptr(lport->stats, get_cpu())->VLinkFailureCount++;
  1272. put_cpu();
  1273. fcoe_ctlr_reset(fip);
  1274. mutex_unlock(&fip->ctlr_mutex);
  1275. mutex_lock(&lport->lp_mutex);
  1276. list_for_each_entry(vn_port, &lport->vports, list)
  1277. fc_lport_reset(vn_port);
  1278. mutex_unlock(&lport->lp_mutex);
  1279. fc_lport_reset(fip->lp);
  1280. fcoe_ctlr_solicit(fip, NULL);
  1281. } else {
  1282. int i;
  1283. LIBFCOE_FIP_DBG(fip, "performing Clear Virtual Link\n");
  1284. for (i = 0; i < num_vlink_desc; i++) {
  1285. vp = vlink_desc_arr[i];
  1286. vn_port = fc_vport_id_lookup(lport,
  1287. ntoh24(vp->fd_fc_id));
  1288. if (!vn_port)
  1289. continue;
  1290. /*
  1291. * 'port_id' is already validated, check MAC address and
  1292. * wwpn
  1293. */
  1294. if (compare_ether_addr(fip->get_src_addr(vn_port),
  1295. vp->fd_mac) != 0 ||
  1296. get_unaligned_be64(&vp->fd_wwpn) !=
  1297. vn_port->wwpn)
  1298. continue;
  1299. if (vn_port == lport)
  1300. /*
  1301. * Physical port, defer processing till all
  1302. * listed NPIV ports are cleared
  1303. */
  1304. reset_phys_port = 1;
  1305. else /* NPIV port */
  1306. fc_lport_reset(vn_port);
  1307. }
  1308. if (reset_phys_port) {
  1309. fc_lport_reset(fip->lp);
  1310. fcoe_ctlr_solicit(fip, NULL);
  1311. }
  1312. }
  1313. err:
  1314. kfree(vlink_desc_arr);
  1315. }
  1316. /**
  1317. * fcoe_ctlr_recv() - Receive a FIP packet
  1318. * @fip: The FCoE controller that received the packet
  1319. * @skb: The received FIP packet
  1320. *
  1321. * This may be called from either NET_RX_SOFTIRQ or IRQ.
  1322. */
  1323. void fcoe_ctlr_recv(struct fcoe_ctlr *fip, struct sk_buff *skb)
  1324. {
  1325. skb_queue_tail(&fip->fip_recv_list, skb);
  1326. schedule_work(&fip->recv_work);
  1327. }
  1328. EXPORT_SYMBOL(fcoe_ctlr_recv);
  1329. /**
  1330. * fcoe_ctlr_recv_handler() - Receive a FIP frame
  1331. * @fip: The FCoE controller that received the frame
  1332. * @skb: The received FIP frame
  1333. *
  1334. * Returns non-zero if the frame is dropped.
  1335. */
  1336. static int fcoe_ctlr_recv_handler(struct fcoe_ctlr *fip, struct sk_buff *skb)
  1337. {
  1338. struct fip_header *fiph;
  1339. struct ethhdr *eh;
  1340. enum fip_state state;
  1341. u16 op;
  1342. u8 sub;
  1343. if (skb_linearize(skb))
  1344. goto drop;
  1345. if (skb->len < sizeof(*fiph))
  1346. goto drop;
  1347. eh = eth_hdr(skb);
  1348. if (fip->mode == FIP_MODE_VN2VN) {
  1349. if (compare_ether_addr(eh->h_dest, fip->ctl_src_addr) &&
  1350. compare_ether_addr(eh->h_dest, fcoe_all_vn2vn) &&
  1351. compare_ether_addr(eh->h_dest, fcoe_all_p2p))
  1352. goto drop;
  1353. } else if (compare_ether_addr(eh->h_dest, fip->ctl_src_addr) &&
  1354. compare_ether_addr(eh->h_dest, fcoe_all_enode))
  1355. goto drop;
  1356. fiph = (struct fip_header *)skb->data;
  1357. op = ntohs(fiph->fip_op);
  1358. sub = fiph->fip_subcode;
  1359. if (FIP_VER_DECAPS(fiph->fip_ver) != FIP_VER)
  1360. goto drop;
  1361. if (ntohs(fiph->fip_dl_len) * FIP_BPW + sizeof(*fiph) > skb->len)
  1362. goto drop;
  1363. mutex_lock(&fip->ctlr_mutex);
  1364. state = fip->state;
  1365. if (state == FIP_ST_AUTO) {
  1366. fip->map_dest = 0;
  1367. fcoe_ctlr_set_state(fip, FIP_ST_ENABLED);
  1368. state = FIP_ST_ENABLED;
  1369. LIBFCOE_FIP_DBG(fip, "Using FIP mode\n");
  1370. }
  1371. mutex_unlock(&fip->ctlr_mutex);
  1372. if (fip->mode == FIP_MODE_VN2VN && op == FIP_OP_VN2VN)
  1373. return fcoe_ctlr_vn_recv(fip, skb);
  1374. if (state != FIP_ST_ENABLED && state != FIP_ST_VNMP_UP &&
  1375. state != FIP_ST_VNMP_CLAIM)
  1376. goto drop;
  1377. if (op == FIP_OP_LS) {
  1378. fcoe_ctlr_recv_els(fip, skb); /* consumes skb */
  1379. return 0;
  1380. }
  1381. if (state != FIP_ST_ENABLED)
  1382. goto drop;
  1383. if (op == FIP_OP_DISC && sub == FIP_SC_ADV)
  1384. fcoe_ctlr_recv_adv(fip, skb);
  1385. else if (op == FIP_OP_CTRL && sub == FIP_SC_CLR_VLINK)
  1386. fcoe_ctlr_recv_clr_vlink(fip, fiph);
  1387. kfree_skb(skb);
  1388. return 0;
  1389. drop:
  1390. kfree_skb(skb);
  1391. return -1;
  1392. }
  1393. /**
  1394. * fcoe_ctlr_select() - Select the best FCF (if possible)
  1395. * @fip: The FCoE controller
  1396. *
  1397. * Returns the selected FCF, or NULL if none are usable.
  1398. *
  1399. * If there are conflicting advertisements, no FCF can be chosen.
  1400. *
  1401. * If there is already a selected FCF, this will choose a better one or
  1402. * an equivalent one that hasn't already been sent a FLOGI.
  1403. *
  1404. * Called with lock held.
  1405. */
  1406. static struct fcoe_fcf *fcoe_ctlr_select(struct fcoe_ctlr *fip)
  1407. {
  1408. struct fcoe_fcf *fcf;
  1409. struct fcoe_fcf *best = fip->sel_fcf;
  1410. struct fcoe_fcf *first;
  1411. first = list_first_entry(&fip->fcfs, struct fcoe_fcf, list);
  1412. list_for_each_entry(fcf, &fip->fcfs, list) {
  1413. LIBFCOE_FIP_DBG(fip, "consider FCF fab %16.16llx "
  1414. "VFID %d mac %pM map %x val %d "
  1415. "sent %u pri %u\n",
  1416. fcf->fabric_name, fcf->vfid, fcf->fcf_mac,
  1417. fcf->fc_map, fcoe_ctlr_mtu_valid(fcf),
  1418. fcf->flogi_sent, fcf->pri);
  1419. if (!fcoe_ctlr_fcf_usable(fcf)) {
  1420. LIBFCOE_FIP_DBG(fip, "FCF for fab %16.16llx "
  1421. "map %x %svalid %savailable\n",
  1422. fcf->fabric_name, fcf->fc_map,
  1423. (fcf->flags & FIP_FL_SOL) ? "" : "in",
  1424. (fcf->flags & FIP_FL_AVAIL) ?
  1425. "" : "un");
  1426. continue;
  1427. }
  1428. if (fcf->fabric_name != first->fabric_name ||
  1429. fcf->vfid != first->vfid ||
  1430. fcf->fc_map != first->fc_map) {
  1431. LIBFCOE_FIP_DBG(fip, "Conflicting fabric, VFID, "
  1432. "or FC-MAP\n");
  1433. return NULL;
  1434. }
  1435. if (fcf->flogi_sent)
  1436. continue;
  1437. if (!best || fcf->pri < best->pri || best->flogi_sent)
  1438. best = fcf;
  1439. }
  1440. fip->sel_fcf = best;
  1441. if (best) {
  1442. LIBFCOE_FIP_DBG(fip, "using FCF mac %pM\n", best->fcf_mac);
  1443. fip->port_ka_time = jiffies +
  1444. msecs_to_jiffies(FIP_VN_KA_PERIOD);
  1445. fip->ctlr_ka_time = jiffies + best->fka_period;
  1446. if (time_before(fip->ctlr_ka_time, fip->timer.expires))
  1447. mod_timer(&fip->timer, fip->ctlr_ka_time);
  1448. }
  1449. return best;
  1450. }
  1451. /**
  1452. * fcoe_ctlr_flogi_send_locked() - send FIP-encapsulated FLOGI to current FCF
  1453. * @fip: The FCoE controller
  1454. *
  1455. * Returns non-zero error if it could not be sent.
  1456. *
  1457. * Called with ctlr_mutex and ctlr_lock held.
  1458. * Caller must verify that fip->sel_fcf is not NULL.
  1459. */
  1460. static int fcoe_ctlr_flogi_send_locked(struct fcoe_ctlr *fip)
  1461. {
  1462. struct sk_buff *skb;
  1463. struct sk_buff *skb_orig;
  1464. struct fc_frame_header *fh;
  1465. int error;
  1466. skb_orig = fip->flogi_req;
  1467. if (!skb_orig)
  1468. return -EINVAL;
  1469. /*
  1470. * Clone and send the FLOGI request. If clone fails, use original.
  1471. */
  1472. skb = skb_clone(skb_orig, GFP_ATOMIC);
  1473. if (!skb) {
  1474. skb = skb_orig;
  1475. fip->flogi_req = NULL;
  1476. }
  1477. fh = (struct fc_frame_header *)skb->data;
  1478. error = fcoe_ctlr_encaps(fip, fip->lp, FIP_DT_FLOGI, skb,
  1479. ntoh24(fh->fh_d_id));
  1480. if (error) {
  1481. kfree_skb(skb);
  1482. return error;
  1483. }
  1484. fip->send(fip, skb);
  1485. fip->sel_fcf->flogi_sent = 1;
  1486. return 0;
  1487. }
  1488. /**
  1489. * fcoe_ctlr_flogi_retry() - resend FLOGI request to a new FCF if possible
  1490. * @fip: The FCoE controller
  1491. *
  1492. * Returns non-zero error code if there's no FLOGI request to retry or
  1493. * no alternate FCF available.
  1494. */
  1495. static int fcoe_ctlr_flogi_retry(struct fcoe_ctlr *fip)
  1496. {
  1497. struct fcoe_fcf *fcf;
  1498. int error;
  1499. mutex_lock(&fip->ctlr_mutex);
  1500. spin_lock_bh(&fip->ctlr_lock);
  1501. LIBFCOE_FIP_DBG(fip, "re-sending FLOGI - reselect\n");
  1502. fcf = fcoe_ctlr_select(fip);
  1503. if (!fcf || fcf->flogi_sent) {
  1504. kfree_skb(fip->flogi_req);
  1505. fip->flogi_req = NULL;
  1506. error = -ENOENT;
  1507. } else {
  1508. fcoe_ctlr_solicit(fip, NULL);
  1509. error = fcoe_ctlr_flogi_send_locked(fip);
  1510. }
  1511. spin_unlock_bh(&fip->ctlr_lock);
  1512. mutex_unlock(&fip->ctlr_mutex);
  1513. return error;
  1514. }
  1515. /**
  1516. * fcoe_ctlr_flogi_send() - Handle sending of FIP FLOGI.
  1517. * @fip: The FCoE controller that timed out
  1518. *
  1519. * Done here because fcoe_ctlr_els_send() can't get mutex.
  1520. *
  1521. * Called with ctlr_mutex held. The caller must not hold ctlr_lock.
  1522. */
  1523. static void fcoe_ctlr_flogi_send(struct fcoe_ctlr *fip)
  1524. {
  1525. struct fcoe_fcf *fcf;
  1526. spin_lock_bh(&fip->ctlr_lock);
  1527. fcf = fip->sel_fcf;
  1528. if (!fcf || !fip->flogi_req_send)
  1529. goto unlock;
  1530. LIBFCOE_FIP_DBG(fip, "sending FLOGI\n");
  1531. /*
  1532. * If this FLOGI is being sent due to a timeout retry
  1533. * to the same FCF as before, select a different FCF if possible.
  1534. */
  1535. if (fcf->flogi_sent) {
  1536. LIBFCOE_FIP_DBG(fip, "sending FLOGI - reselect\n");
  1537. fcf = fcoe_ctlr_select(fip);
  1538. if (!fcf || fcf->flogi_sent) {
  1539. LIBFCOE_FIP_DBG(fip, "sending FLOGI - clearing\n");
  1540. list_for_each_entry(fcf, &fip->fcfs, list)
  1541. fcf->flogi_sent = 0;
  1542. fcf = fcoe_ctlr_select(fip);
  1543. }
  1544. }
  1545. if (fcf) {
  1546. fcoe_ctlr_flogi_send_locked(fip);
  1547. fip->flogi_req_send = 0;
  1548. } else /* XXX */
  1549. LIBFCOE_FIP_DBG(fip, "No FCF selected - defer send\n");
  1550. unlock:
  1551. spin_unlock_bh(&fip->ctlr_lock);
  1552. }
  1553. /**
  1554. * fcoe_ctlr_timeout() - FIP timeout handler
  1555. * @arg: The FCoE controller that timed out
  1556. */
  1557. static void fcoe_ctlr_timeout(unsigned long arg)
  1558. {
  1559. struct fcoe_ctlr *fip = (struct fcoe_ctlr *)arg;
  1560. schedule_work(&fip->timer_work);
  1561. }
  1562. /**
  1563. * fcoe_ctlr_timer_work() - Worker thread function for timer work
  1564. * @work: Handle to a FCoE controller
  1565. *
  1566. * Ages FCFs. Triggers FCF selection if possible.
  1567. * Sends keep-alives and resets.
  1568. */
  1569. static void fcoe_ctlr_timer_work(struct work_struct *work)
  1570. {
  1571. struct fcoe_ctlr *fip;
  1572. struct fc_lport *vport;
  1573. u8 *mac;
  1574. u8 reset = 0;
  1575. u8 send_ctlr_ka = 0;
  1576. u8 send_port_ka = 0;
  1577. struct fcoe_fcf *sel;
  1578. struct fcoe_fcf *fcf;
  1579. unsigned long next_timer;
  1580. fip = container_of(work, struct fcoe_ctlr, timer_work);
  1581. if (fip->mode == FIP_MODE_VN2VN)
  1582. return fcoe_ctlr_vn_timeout(fip);
  1583. mutex_lock(&fip->ctlr_mutex);
  1584. if (fip->state == FIP_ST_DISABLED) {
  1585. mutex_unlock(&fip->ctlr_mutex);
  1586. return;
  1587. }
  1588. fcf = fip->sel_fcf;
  1589. next_timer = fcoe_ctlr_age_fcfs(fip);
  1590. sel = fip->sel_fcf;
  1591. if (!sel && fip->sel_time) {
  1592. if (time_after_eq(jiffies, fip->sel_time)) {
  1593. sel = fcoe_ctlr_select(fip);
  1594. fip->sel_time = 0;
  1595. } else if (time_after(next_timer, fip->sel_time))
  1596. next_timer = fip->sel_time;
  1597. }
  1598. if (sel && fip->flogi_req_send)
  1599. fcoe_ctlr_flogi_send(fip);
  1600. else if (!sel && fcf)
  1601. reset = 1;
  1602. if (sel && !sel->fd_flags) {
  1603. if (time_after_eq(jiffies, fip->ctlr_ka_time)) {
  1604. fip->ctlr_ka_time = jiffies + sel->fka_period;
  1605. send_ctlr_ka = 1;
  1606. }
  1607. if (time_after(next_timer, fip->ctlr_ka_time))
  1608. next_timer = fip->ctlr_ka_time;
  1609. if (time_after_eq(jiffies, fip->port_ka_time)) {
  1610. fip->port_ka_time = jiffies +
  1611. msecs_to_jiffies(FIP_VN_KA_PERIOD);
  1612. send_port_ka = 1;
  1613. }
  1614. if (time_after(next_timer, fip->port_ka_time))
  1615. next_timer = fip->port_ka_time;
  1616. }
  1617. if (!list_empty(&fip->fcfs))
  1618. mod_timer(&fip->timer, next_timer);
  1619. mutex_unlock(&fip->ctlr_mutex);
  1620. if (reset) {
  1621. fc_lport_reset(fip->lp);
  1622. /* restart things with a solicitation */
  1623. fcoe_ctlr_solicit(fip, NULL);
  1624. }
  1625. if (send_ctlr_ka)
  1626. fcoe_ctlr_send_keep_alive(fip, NULL, 0, fip->ctl_src_addr);
  1627. if (send_port_ka) {
  1628. mutex_lock(&fip->lp->lp_mutex);
  1629. mac = fip->get_src_addr(fip->lp);
  1630. fcoe_ctlr_send_keep_alive(fip, fip->lp, 1, mac);
  1631. list_for_each_entry(vport, &fip->lp->vports, list) {
  1632. mac = fip->get_src_addr(vport);
  1633. fcoe_ctlr_send_keep_alive(fip, vport, 1, mac);
  1634. }
  1635. mutex_unlock(&fip->lp->lp_mutex);
  1636. }
  1637. }
  1638. /**
  1639. * fcoe_ctlr_recv_work() - Worker thread function for receiving FIP frames
  1640. * @recv_work: Handle to a FCoE controller
  1641. */
  1642. static void fcoe_ctlr_recv_work(struct work_struct *recv_work)
  1643. {
  1644. struct fcoe_ctlr *fip;
  1645. struct sk_buff *skb;
  1646. fip = container_of(recv_work, struct fcoe_ctlr, recv_work);
  1647. while ((skb = skb_dequeue(&fip->fip_recv_list)))
  1648. fcoe_ctlr_recv_handler(fip, skb);
  1649. }
  1650. /**
  1651. * fcoe_ctlr_recv_flogi() - Snoop pre-FIP receipt of FLOGI response
  1652. * @fip: The FCoE controller
  1653. * @fp: The FC frame to snoop
  1654. *
  1655. * Snoop potential response to FLOGI or even incoming FLOGI.
  1656. *
  1657. * The caller has checked that we are waiting for login as indicated
  1658. * by fip->flogi_oxid != FC_XID_UNKNOWN.
  1659. *
  1660. * The caller is responsible for freeing the frame.
  1661. * Fill in the granted_mac address.
  1662. *
  1663. * Return non-zero if the frame should not be delivered to libfc.
  1664. */
  1665. int fcoe_ctlr_recv_flogi(struct fcoe_ctlr *fip, struct fc_lport *lport,
  1666. struct fc_frame *fp)
  1667. {
  1668. struct fc_frame_header *fh;
  1669. u8 op;
  1670. u8 *sa;
  1671. sa = eth_hdr(&fp->skb)->h_source;
  1672. fh = fc_frame_header_get(fp);
  1673. if (fh->fh_type != FC_TYPE_ELS)
  1674. return 0;
  1675. op = fc_frame_payload_op(fp);
  1676. if (op == ELS_LS_ACC && fh->fh_r_ctl == FC_RCTL_ELS_REP &&
  1677. fip->flogi_oxid == ntohs(fh->fh_ox_id)) {
  1678. mutex_lock(&fip->ctlr_mutex);
  1679. if (fip->state != FIP_ST_AUTO && fip->state != FIP_ST_NON_FIP) {
  1680. mutex_unlock(&fip->ctlr_mutex);
  1681. return -EINVAL;
  1682. }
  1683. fcoe_ctlr_set_state(fip, FIP_ST_NON_FIP);
  1684. LIBFCOE_FIP_DBG(fip,
  1685. "received FLOGI LS_ACC using non-FIP mode\n");
  1686. /*
  1687. * FLOGI accepted.
  1688. * If the src mac addr is FC_OUI-based, then we mark the
  1689. * address_mode flag to use FC_OUI-based Ethernet DA.
  1690. * Otherwise we use the FCoE gateway addr
  1691. */
  1692. if (!compare_ether_addr(sa, (u8[6])FC_FCOE_FLOGI_MAC)) {
  1693. fcoe_ctlr_map_dest(fip);
  1694. } else {
  1695. memcpy(fip->dest_addr, sa, ETH_ALEN);
  1696. fip->map_dest = 0;
  1697. }
  1698. fip->flogi_oxid = FC_XID_UNKNOWN;
  1699. mutex_unlock(&fip->ctlr_mutex);
  1700. fc_fcoe_set_mac(fr_cb(fp)->granted_mac, fh->fh_d_id);
  1701. } else if (op == ELS_FLOGI && fh->fh_r_ctl == FC_RCTL_ELS_REQ && sa) {
  1702. /*
  1703. * Save source MAC for point-to-point responses.
  1704. */
  1705. mutex_lock(&fip->ctlr_mutex);
  1706. if (fip->state == FIP_ST_AUTO || fip->state == FIP_ST_NON_FIP) {
  1707. memcpy(fip->dest_addr, sa, ETH_ALEN);
  1708. fip->map_dest = 0;
  1709. if (fip->state == FIP_ST_AUTO)
  1710. LIBFCOE_FIP_DBG(fip, "received non-FIP FLOGI. "
  1711. "Setting non-FIP mode\n");
  1712. fcoe_ctlr_set_state(fip, FIP_ST_NON_FIP);
  1713. }
  1714. mutex_unlock(&fip->ctlr_mutex);
  1715. }
  1716. return 0;
  1717. }
  1718. EXPORT_SYMBOL(fcoe_ctlr_recv_flogi);
  1719. /**
  1720. * fcoe_wwn_from_mac() - Converts a 48-bit IEEE MAC address to a 64-bit FC WWN
  1721. * @mac: The MAC address to convert
  1722. * @scheme: The scheme to use when converting
  1723. * @port: The port indicator for converting
  1724. *
  1725. * Returns: u64 fc world wide name
  1726. */
  1727. u64 fcoe_wwn_from_mac(unsigned char mac[MAX_ADDR_LEN],
  1728. unsigned int scheme, unsigned int port)
  1729. {
  1730. u64 wwn;
  1731. u64 host_mac;
  1732. /* The MAC is in NO, so flip only the low 48 bits */
  1733. host_mac = ((u64) mac[0] << 40) |
  1734. ((u64) mac[1] << 32) |
  1735. ((u64) mac[2] << 24) |
  1736. ((u64) mac[3] << 16) |
  1737. ((u64) mac[4] << 8) |
  1738. (u64) mac[5];
  1739. WARN_ON(host_mac >= (1ULL << 48));
  1740. wwn = host_mac | ((u64) scheme << 60);
  1741. switch (scheme) {
  1742. case 1:
  1743. WARN_ON(port != 0);
  1744. break;
  1745. case 2:
  1746. WARN_ON(port >= 0xfff);
  1747. wwn |= (u64) port << 48;
  1748. break;
  1749. default:
  1750. WARN_ON(1);
  1751. break;
  1752. }
  1753. return wwn;
  1754. }
  1755. EXPORT_SYMBOL_GPL(fcoe_wwn_from_mac);
  1756. /**
  1757. * fcoe_ctlr_rport() - return the fcoe_rport for a given fc_rport_priv
  1758. * @rdata: libfc remote port
  1759. */
  1760. static inline struct fcoe_rport *fcoe_ctlr_rport(struct fc_rport_priv *rdata)
  1761. {
  1762. return (struct fcoe_rport *)(rdata + 1);
  1763. }
  1764. /**
  1765. * fcoe_ctlr_vn_send() - Send a FIP VN2VN Probe Request or Reply.
  1766. * @fip: The FCoE controller
  1767. * @sub: sub-opcode for probe request, reply, or advertisement.
  1768. * @dest: The destination Ethernet MAC address
  1769. * @min_len: minimum size of the Ethernet payload to be sent
  1770. */
  1771. static void fcoe_ctlr_vn_send(struct fcoe_ctlr *fip,
  1772. enum fip_vn2vn_subcode sub,
  1773. const u8 *dest, size_t min_len)
  1774. {
  1775. struct sk_buff *skb;
  1776. struct fip_frame {
  1777. struct ethhdr eth;
  1778. struct fip_header fip;
  1779. struct fip_mac_desc mac;
  1780. struct fip_wwn_desc wwnn;
  1781. struct fip_vn_desc vn;
  1782. } __packed * frame;
  1783. struct fip_fc4_feat *ff;
  1784. struct fip_size_desc *size;
  1785. u32 fcp_feat;
  1786. size_t len;
  1787. size_t dlen;
  1788. len = sizeof(*frame);
  1789. dlen = 0;
  1790. if (sub == FIP_SC_VN_CLAIM_NOTIFY || sub == FIP_SC_VN_CLAIM_REP) {
  1791. dlen = sizeof(struct fip_fc4_feat) +
  1792. sizeof(struct fip_size_desc);
  1793. len += dlen;
  1794. }
  1795. dlen += sizeof(frame->mac) + sizeof(frame->wwnn) + sizeof(frame->vn);
  1796. len = max(len, min_len + sizeof(struct ethhdr));
  1797. skb = dev_alloc_skb(len);
  1798. if (!skb)
  1799. return;
  1800. frame = (struct fip_frame *)skb->data;
  1801. memset(frame, 0, len);
  1802. memcpy(frame->eth.h_dest, dest, ETH_ALEN);
  1803. if (sub == FIP_SC_VN_BEACON) {
  1804. hton24(frame->eth.h_source, FIP_VN_FC_MAP);
  1805. hton24(frame->eth.h_source + 3, fip->port_id);
  1806. } else {
  1807. memcpy(frame->eth.h_source, fip->ctl_src_addr, ETH_ALEN);
  1808. }
  1809. frame->eth.h_proto = htons(ETH_P_FIP);
  1810. frame->fip.fip_ver = FIP_VER_ENCAPS(FIP_VER);
  1811. frame->fip.fip_op = htons(FIP_OP_VN2VN);
  1812. frame->fip.fip_subcode = sub;
  1813. frame->fip.fip_dl_len = htons(dlen / FIP_BPW);
  1814. frame->mac.fd_desc.fip_dtype = FIP_DT_MAC;
  1815. frame->mac.fd_desc.fip_dlen = sizeof(frame->mac) / FIP_BPW;
  1816. memcpy(frame->mac.fd_mac, fip->ctl_src_addr, ETH_ALEN);
  1817. frame->wwnn.fd_desc.fip_dtype = FIP_DT_NAME;
  1818. frame->wwnn.fd_desc.fip_dlen = sizeof(frame->wwnn) / FIP_BPW;
  1819. put_unaligned_be64(fip->lp->wwnn, &frame->wwnn.fd_wwn);
  1820. frame->vn.fd_desc.fip_dtype = FIP_DT_VN_ID;
  1821. frame->vn.fd_desc.fip_dlen = sizeof(frame->vn) / FIP_BPW;
  1822. hton24(frame->vn.fd_mac, FIP_VN_FC_MAP);
  1823. hton24(frame->vn.fd_mac + 3, fip->port_id);
  1824. hton24(frame->vn.fd_fc_id, fip->port_id);
  1825. put_unaligned_be64(fip->lp->wwpn, &frame->vn.fd_wwpn);
  1826. /*
  1827. * For claims, add FC-4 features.
  1828. * TBD: Add interface to get fc-4 types and features from libfc.
  1829. */
  1830. if (sub == FIP_SC_VN_CLAIM_NOTIFY || sub == FIP_SC_VN_CLAIM_REP) {
  1831. ff = (struct fip_fc4_feat *)(frame + 1);
  1832. ff->fd_desc.fip_dtype = FIP_DT_FC4F;
  1833. ff->fd_desc.fip_dlen = sizeof(*ff) / FIP_BPW;
  1834. ff->fd_fts = fip->lp->fcts;
  1835. fcp_feat = 0;
  1836. if (fip->lp->service_params & FCP_SPPF_INIT_FCN)
  1837. fcp_feat |= FCP_FEAT_INIT;
  1838. if (fip->lp->service_params & FCP_SPPF_TARG_FCN)
  1839. fcp_feat |= FCP_FEAT_TARG;
  1840. fcp_feat <<= (FC_TYPE_FCP * 4) % 32;
  1841. ff->fd_ff.fd_feat[FC_TYPE_FCP * 4 / 32] = htonl(fcp_feat);
  1842. size = (struct fip_size_desc *)(ff + 1);
  1843. size->fd_desc.fip_dtype = FIP_DT_FCOE_SIZE;
  1844. size->fd_desc.fip_dlen = sizeof(*size) / FIP_BPW;
  1845. size->fd_size = htons(fcoe_ctlr_fcoe_size(fip));
  1846. }
  1847. skb_put(skb, len);
  1848. skb->protocol = htons(ETH_P_FIP);
  1849. skb->priority = fip->priority;
  1850. skb_reset_mac_header(skb);
  1851. skb_reset_network_header(skb);
  1852. fip->send(fip, skb);
  1853. }
  1854. /**
  1855. * fcoe_ctlr_vn_rport_callback - Event handler for rport events.
  1856. * @lport: The lport which is receiving the event
  1857. * @rdata: remote port private data
  1858. * @event: The event that occurred
  1859. *
  1860. * Locking Note: The rport lock must not be held when calling this function.
  1861. */
  1862. static void fcoe_ctlr_vn_rport_callback(struct fc_lport *lport,
  1863. struct fc_rport_priv *rdata,
  1864. enum fc_rport_event event)
  1865. {
  1866. struct fcoe_ctlr *fip = lport->disc.priv;
  1867. struct fcoe_rport *frport = fcoe_ctlr_rport(rdata);
  1868. LIBFCOE_FIP_DBG(fip, "vn_rport_callback %x event %d\n",
  1869. rdata->ids.port_id, event);
  1870. mutex_lock(&fip->ctlr_mutex);
  1871. switch (event) {
  1872. case RPORT_EV_READY:
  1873. frport->login_count = 0;
  1874. break;
  1875. case RPORT_EV_LOGO:
  1876. case RPORT_EV_FAILED:
  1877. case RPORT_EV_STOP:
  1878. frport->login_count++;
  1879. if (frport->login_count > FCOE_CTLR_VN2VN_LOGIN_LIMIT) {
  1880. LIBFCOE_FIP_DBG(fip,
  1881. "rport FLOGI limited port_id %6.6x\n",
  1882. rdata->ids.port_id);
  1883. lport->tt.rport_logoff(rdata);
  1884. }
  1885. break;
  1886. default:
  1887. break;
  1888. }
  1889. mutex_unlock(&fip->ctlr_mutex);
  1890. }
  1891. static struct fc_rport_operations fcoe_ctlr_vn_rport_ops = {
  1892. .event_callback = fcoe_ctlr_vn_rport_callback,
  1893. };
  1894. /**
  1895. * fcoe_ctlr_disc_stop_locked() - stop discovery in VN2VN mode
  1896. * @fip: The FCoE controller
  1897. *
  1898. * Called with ctlr_mutex held.
  1899. */
  1900. static void fcoe_ctlr_disc_stop_locked(struct fc_lport *lport)
  1901. {
  1902. mutex_lock(&lport->disc.disc_mutex);
  1903. lport->disc.disc_callback = NULL;
  1904. mutex_unlock(&lport->disc.disc_mutex);
  1905. }
  1906. /**
  1907. * fcoe_ctlr_disc_stop() - stop discovery in VN2VN mode
  1908. * @fip: The FCoE controller
  1909. *
  1910. * Called through the local port template for discovery.
  1911. * Called without the ctlr_mutex held.
  1912. */
  1913. static void fcoe_ctlr_disc_stop(struct fc_lport *lport)
  1914. {
  1915. struct fcoe_ctlr *fip = lport->disc.priv;
  1916. mutex_lock(&fip->ctlr_mutex);
  1917. fcoe_ctlr_disc_stop_locked(lport);
  1918. mutex_unlock(&fip->ctlr_mutex);
  1919. }
  1920. /**
  1921. * fcoe_ctlr_disc_stop_final() - stop discovery for shutdown in VN2VN mode
  1922. * @fip: The FCoE controller
  1923. *
  1924. * Called through the local port template for discovery.
  1925. * Called without the ctlr_mutex held.
  1926. */
  1927. static void fcoe_ctlr_disc_stop_final(struct fc_lport *lport)
  1928. {
  1929. fcoe_ctlr_disc_stop(lport);
  1930. lport->tt.rport_flush_queue();
  1931. synchronize_rcu();
  1932. }
  1933. /**
  1934. * fcoe_ctlr_vn_restart() - VN2VN probe restart with new port_id
  1935. * @fip: The FCoE controller
  1936. *
  1937. * Called with fcoe_ctlr lock held.
  1938. */
  1939. static void fcoe_ctlr_vn_restart(struct fcoe_ctlr *fip)
  1940. {
  1941. unsigned long wait;
  1942. u32 port_id;
  1943. fcoe_ctlr_disc_stop_locked(fip->lp);
  1944. /*
  1945. * Get proposed port ID.
  1946. * If this is the first try after link up, use any previous port_id.
  1947. * If there was none, use the low bits of the port_name.
  1948. * On subsequent tries, get the next random one.
  1949. * Don't use reserved IDs, use another non-zero value, just as random.
  1950. */
  1951. port_id = fip->port_id;
  1952. if (fip->probe_tries)
  1953. port_id = prandom_u32_state(&fip->rnd_state) & 0xffff;
  1954. else if (!port_id)
  1955. port_id = fip->lp->wwpn & 0xffff;
  1956. if (!port_id || port_id == 0xffff)
  1957. port_id = 1;
  1958. fip->port_id = port_id;
  1959. if (fip->probe_tries < FIP_VN_RLIM_COUNT) {
  1960. fip->probe_tries++;
  1961. wait = random32() % FIP_VN_PROBE_WAIT;
  1962. } else
  1963. wait = FIP_VN_RLIM_INT;
  1964. mod_timer(&fip->timer, jiffies + msecs_to_jiffies(wait));
  1965. fcoe_ctlr_set_state(fip, FIP_ST_VNMP_START);
  1966. }
  1967. /**
  1968. * fcoe_ctlr_vn_start() - Start in VN2VN mode
  1969. * @fip: The FCoE controller
  1970. *
  1971. * Called with fcoe_ctlr lock held.
  1972. */
  1973. static void fcoe_ctlr_vn_start(struct fcoe_ctlr *fip)
  1974. {
  1975. fip->probe_tries = 0;
  1976. prandom_seed_state(&fip->rnd_state, fip->lp->wwpn);
  1977. fcoe_ctlr_vn_restart(fip);
  1978. }
  1979. /**
  1980. * fcoe_ctlr_vn_parse - parse probe request or response
  1981. * @fip: The FCoE controller
  1982. * @skb: incoming packet
  1983. * @rdata: buffer for resulting parsed VN entry plus fcoe_rport
  1984. *
  1985. * Returns non-zero error number on error.
  1986. * Does not consume the packet.
  1987. */
  1988. static int fcoe_ctlr_vn_parse(struct fcoe_ctlr *fip,
  1989. struct sk_buff *skb,
  1990. struct fc_rport_priv *rdata)
  1991. {
  1992. struct fip_header *fiph;
  1993. struct fip_desc *desc = NULL;
  1994. struct fip_mac_desc *macd = NULL;
  1995. struct fip_wwn_desc *wwn = NULL;
  1996. struct fip_vn_desc *vn = NULL;
  1997. struct fip_size_desc *size = NULL;
  1998. struct fcoe_rport *frport;
  1999. size_t rlen;
  2000. size_t dlen;
  2001. u32 desc_mask = 0;
  2002. u32 dtype;
  2003. u8 sub;
  2004. memset(rdata, 0, sizeof(*rdata) + sizeof(*frport));
  2005. frport = fcoe_ctlr_rport(rdata);
  2006. fiph = (struct fip_header *)skb->data;
  2007. frport->flags = ntohs(fiph->fip_flags);
  2008. sub = fiph->fip_subcode;
  2009. switch (sub) {
  2010. case FIP_SC_VN_PROBE_REQ:
  2011. case FIP_SC_VN_PROBE_REP:
  2012. case FIP_SC_VN_BEACON:
  2013. desc_mask = BIT(FIP_DT_MAC) | BIT(FIP_DT_NAME) |
  2014. BIT(FIP_DT_VN_ID);
  2015. break;
  2016. case FIP_SC_VN_CLAIM_NOTIFY:
  2017. case FIP_SC_VN_CLAIM_REP:
  2018. desc_mask = BIT(FIP_DT_MAC) | BIT(FIP_DT_NAME) |
  2019. BIT(FIP_DT_VN_ID) | BIT(FIP_DT_FC4F) |
  2020. BIT(FIP_DT_FCOE_SIZE);
  2021. break;
  2022. default:
  2023. LIBFCOE_FIP_DBG(fip, "vn_parse unknown subcode %u\n", sub);
  2024. return -EINVAL;
  2025. }
  2026. rlen = ntohs(fiph->fip_dl_len) * 4;
  2027. if (rlen + sizeof(*fiph) > skb->len)
  2028. return -EINVAL;
  2029. desc = (struct fip_desc *)(fiph + 1);
  2030. while (rlen > 0) {
  2031. dlen = desc->fip_dlen * FIP_BPW;
  2032. if (dlen < sizeof(*desc) || dlen > rlen)
  2033. return -EINVAL;
  2034. dtype = desc->fip_dtype;
  2035. if (dtype < 32) {
  2036. if (!(desc_mask & BIT(dtype))) {
  2037. LIBFCOE_FIP_DBG(fip,
  2038. "unexpected or duplicated desc "
  2039. "desc type %u in "
  2040. "FIP VN2VN subtype %u\n",
  2041. dtype, sub);
  2042. return -EINVAL;
  2043. }
  2044. desc_mask &= ~BIT(dtype);
  2045. }
  2046. switch (dtype) {
  2047. case FIP_DT_MAC:
  2048. if (dlen != sizeof(struct fip_mac_desc))
  2049. goto len_err;
  2050. macd = (struct fip_mac_desc *)desc;
  2051. if (!is_valid_ether_addr(macd->fd_mac)) {
  2052. LIBFCOE_FIP_DBG(fip,
  2053. "Invalid MAC addr %pM in FIP VN2VN\n",
  2054. macd->fd_mac);
  2055. return -EINVAL;
  2056. }
  2057. memcpy(frport->enode_mac, macd->fd_mac, ETH_ALEN);
  2058. break;
  2059. case FIP_DT_NAME:
  2060. if (dlen != sizeof(struct fip_wwn_desc))
  2061. goto len_err;
  2062. wwn = (struct fip_wwn_desc *)desc;
  2063. rdata->ids.node_name = get_unaligned_be64(&wwn->fd_wwn);
  2064. break;
  2065. case FIP_DT_VN_ID:
  2066. if (dlen != sizeof(struct fip_vn_desc))
  2067. goto len_err;
  2068. vn = (struct fip_vn_desc *)desc;
  2069. memcpy(frport->vn_mac, vn->fd_mac, ETH_ALEN);
  2070. rdata->ids.port_id = ntoh24(vn->fd_fc_id);
  2071. rdata->ids.port_name = get_unaligned_be64(&vn->fd_wwpn);
  2072. break;
  2073. case FIP_DT_FC4F:
  2074. if (dlen != sizeof(struct fip_fc4_feat))
  2075. goto len_err;
  2076. break;
  2077. case FIP_DT_FCOE_SIZE:
  2078. if (dlen != sizeof(struct fip_size_desc))
  2079. goto len_err;
  2080. size = (struct fip_size_desc *)desc;
  2081. frport->fcoe_len = ntohs(size->fd_size);
  2082. break;
  2083. default:
  2084. LIBFCOE_FIP_DBG(fip, "unexpected descriptor type %x "
  2085. "in FIP probe\n", dtype);
  2086. /* standard says ignore unknown descriptors >= 128 */
  2087. if (dtype < FIP_DT_VENDOR_BASE)
  2088. return -EINVAL;
  2089. break;
  2090. }
  2091. desc = (struct fip_desc *)((char *)desc + dlen);
  2092. rlen -= dlen;
  2093. }
  2094. return 0;
  2095. len_err:
  2096. LIBFCOE_FIP_DBG(fip, "FIP length error in descriptor type %x len %zu\n",
  2097. dtype, dlen);
  2098. return -EINVAL;
  2099. }
  2100. /**
  2101. * fcoe_ctlr_vn_send_claim() - send multicast FIP VN2VN Claim Notification.
  2102. * @fip: The FCoE controller
  2103. *
  2104. * Called with ctlr_mutex held.
  2105. */
  2106. static void fcoe_ctlr_vn_send_claim(struct fcoe_ctlr *fip)
  2107. {
  2108. fcoe_ctlr_vn_send(fip, FIP_SC_VN_CLAIM_NOTIFY, fcoe_all_vn2vn, 0);
  2109. fip->sol_time = jiffies;
  2110. }
  2111. /**
  2112. * fcoe_ctlr_vn_probe_req() - handle incoming VN2VN probe request.
  2113. * @fip: The FCoE controller
  2114. * @rdata: parsed remote port with frport from the probe request
  2115. *
  2116. * Called with ctlr_mutex held.
  2117. */
  2118. static void fcoe_ctlr_vn_probe_req(struct fcoe_ctlr *fip,
  2119. struct fc_rport_priv *rdata)
  2120. {
  2121. struct fcoe_rport *frport = fcoe_ctlr_rport(rdata);
  2122. if (rdata->ids.port_id != fip->port_id)
  2123. return;
  2124. switch (fip->state) {
  2125. case FIP_ST_VNMP_CLAIM:
  2126. case FIP_ST_VNMP_UP:
  2127. fcoe_ctlr_vn_send(fip, FIP_SC_VN_PROBE_REP,
  2128. frport->enode_mac, 0);
  2129. break;
  2130. case FIP_ST_VNMP_PROBE1:
  2131. case FIP_ST_VNMP_PROBE2:
  2132. /*
  2133. * Decide whether to reply to the Probe.
  2134. * Our selected address is never a "recorded" one, so
  2135. * only reply if our WWPN is greater and the
  2136. * Probe's REC bit is not set.
  2137. * If we don't reply, we will change our address.
  2138. */
  2139. if (fip->lp->wwpn > rdata->ids.port_name &&
  2140. !(frport->flags & FIP_FL_REC_OR_P2P)) {
  2141. fcoe_ctlr_vn_send(fip, FIP_SC_VN_PROBE_REP,
  2142. frport->enode_mac, 0);
  2143. break;
  2144. }
  2145. /* fall through */
  2146. case FIP_ST_VNMP_START:
  2147. fcoe_ctlr_vn_restart(fip);
  2148. break;
  2149. default:
  2150. break;
  2151. }
  2152. }
  2153. /**
  2154. * fcoe_ctlr_vn_probe_reply() - handle incoming VN2VN probe reply.
  2155. * @fip: The FCoE controller
  2156. * @rdata: parsed remote port with frport from the probe request
  2157. *
  2158. * Called with ctlr_mutex held.
  2159. */
  2160. static void fcoe_ctlr_vn_probe_reply(struct fcoe_ctlr *fip,
  2161. struct fc_rport_priv *rdata)
  2162. {
  2163. if (rdata->ids.port_id != fip->port_id)
  2164. return;
  2165. switch (fip->state) {
  2166. case FIP_ST_VNMP_START:
  2167. case FIP_ST_VNMP_PROBE1:
  2168. case FIP_ST_VNMP_PROBE2:
  2169. case FIP_ST_VNMP_CLAIM:
  2170. fcoe_ctlr_vn_restart(fip);
  2171. break;
  2172. case FIP_ST_VNMP_UP:
  2173. fcoe_ctlr_vn_send_claim(fip);
  2174. break;
  2175. default:
  2176. break;
  2177. }
  2178. }
  2179. /**
  2180. * fcoe_ctlr_vn_add() - Add a VN2VN entry to the list, based on a claim reply.
  2181. * @fip: The FCoE controller
  2182. * @new: newly-parsed remote port with frport as a template for new rdata
  2183. *
  2184. * Called with ctlr_mutex held.
  2185. */
  2186. static void fcoe_ctlr_vn_add(struct fcoe_ctlr *fip, struct fc_rport_priv *new)
  2187. {
  2188. struct fc_lport *lport = fip->lp;
  2189. struct fc_rport_priv *rdata;
  2190. struct fc_rport_identifiers *ids;
  2191. struct fcoe_rport *frport;
  2192. u32 port_id;
  2193. port_id = new->ids.port_id;
  2194. if (port_id == fip->port_id)
  2195. return;
  2196. mutex_lock(&lport->disc.disc_mutex);
  2197. rdata = lport->tt.rport_create(lport, port_id);
  2198. if (!rdata) {
  2199. mutex_unlock(&lport->disc.disc_mutex);
  2200. return;
  2201. }
  2202. rdata->ops = &fcoe_ctlr_vn_rport_ops;
  2203. rdata->disc_id = lport->disc.disc_id;
  2204. ids = &rdata->ids;
  2205. if ((ids->port_name != -1 && ids->port_name != new->ids.port_name) ||
  2206. (ids->node_name != -1 && ids->node_name != new->ids.node_name))
  2207. lport->tt.rport_logoff(rdata);
  2208. ids->port_name = new->ids.port_name;
  2209. ids->node_name = new->ids.node_name;
  2210. mutex_unlock(&lport->disc.disc_mutex);
  2211. frport = fcoe_ctlr_rport(rdata);
  2212. LIBFCOE_FIP_DBG(fip, "vn_add rport %6.6x %s\n",
  2213. port_id, frport->fcoe_len ? "old" : "new");
  2214. *frport = *fcoe_ctlr_rport(new);
  2215. frport->time = 0;
  2216. }
  2217. /**
  2218. * fcoe_ctlr_vn_lookup() - Find VN remote port's MAC address
  2219. * @fip: The FCoE controller
  2220. * @port_id: The port_id of the remote VN_node
  2221. * @mac: buffer which will hold the VN_NODE destination MAC address, if found.
  2222. *
  2223. * Returns non-zero error if no remote port found.
  2224. */
  2225. static int fcoe_ctlr_vn_lookup(struct fcoe_ctlr *fip, u32 port_id, u8 *mac)
  2226. {
  2227. struct fc_lport *lport = fip->lp;
  2228. struct fc_rport_priv *rdata;
  2229. struct fcoe_rport *frport;
  2230. int ret = -1;
  2231. rcu_read_lock();
  2232. rdata = lport->tt.rport_lookup(lport, port_id);
  2233. if (rdata) {
  2234. frport = fcoe_ctlr_rport(rdata);
  2235. memcpy(mac, frport->enode_mac, ETH_ALEN);
  2236. ret = 0;
  2237. }
  2238. rcu_read_unlock();
  2239. return ret;
  2240. }
  2241. /**
  2242. * fcoe_ctlr_vn_claim_notify() - handle received FIP VN2VN Claim Notification
  2243. * @fip: The FCoE controller
  2244. * @new: newly-parsed remote port with frport as a template for new rdata
  2245. *
  2246. * Called with ctlr_mutex held.
  2247. */
  2248. static void fcoe_ctlr_vn_claim_notify(struct fcoe_ctlr *fip,
  2249. struct fc_rport_priv *new)
  2250. {
  2251. struct fcoe_rport *frport = fcoe_ctlr_rport(new);
  2252. if (frport->flags & FIP_FL_REC_OR_P2P) {
  2253. fcoe_ctlr_vn_send(fip, FIP_SC_VN_PROBE_REQ, fcoe_all_vn2vn, 0);
  2254. return;
  2255. }
  2256. switch (fip->state) {
  2257. case FIP_ST_VNMP_START:
  2258. case FIP_ST_VNMP_PROBE1:
  2259. case FIP_ST_VNMP_PROBE2:
  2260. if (new->ids.port_id == fip->port_id)
  2261. fcoe_ctlr_vn_restart(fip);
  2262. break;
  2263. case FIP_ST_VNMP_CLAIM:
  2264. case FIP_ST_VNMP_UP:
  2265. if (new->ids.port_id == fip->port_id) {
  2266. if (new->ids.port_name > fip->lp->wwpn) {
  2267. fcoe_ctlr_vn_restart(fip);
  2268. break;
  2269. }
  2270. fcoe_ctlr_vn_send_claim(fip);
  2271. break;
  2272. }
  2273. fcoe_ctlr_vn_send(fip, FIP_SC_VN_CLAIM_REP, frport->enode_mac,
  2274. min((u32)frport->fcoe_len,
  2275. fcoe_ctlr_fcoe_size(fip)));
  2276. fcoe_ctlr_vn_add(fip, new);
  2277. break;
  2278. default:
  2279. break;
  2280. }
  2281. }
  2282. /**
  2283. * fcoe_ctlr_vn_claim_resp() - handle received Claim Response
  2284. * @fip: The FCoE controller that received the frame
  2285. * @new: newly-parsed remote port with frport from the Claim Response
  2286. *
  2287. * Called with ctlr_mutex held.
  2288. */
  2289. static void fcoe_ctlr_vn_claim_resp(struct fcoe_ctlr *fip,
  2290. struct fc_rport_priv *new)
  2291. {
  2292. LIBFCOE_FIP_DBG(fip, "claim resp from from rport %x - state %s\n",
  2293. new->ids.port_id, fcoe_ctlr_state(fip->state));
  2294. if (fip->state == FIP_ST_VNMP_UP || fip->state == FIP_ST_VNMP_CLAIM)
  2295. fcoe_ctlr_vn_add(fip, new);
  2296. }
  2297. /**
  2298. * fcoe_ctlr_vn_beacon() - handle received beacon.
  2299. * @fip: The FCoE controller that received the frame
  2300. * @new: newly-parsed remote port with frport from the Beacon
  2301. *
  2302. * Called with ctlr_mutex held.
  2303. */
  2304. static void fcoe_ctlr_vn_beacon(struct fcoe_ctlr *fip,
  2305. struct fc_rport_priv *new)
  2306. {
  2307. struct fc_lport *lport = fip->lp;
  2308. struct fc_rport_priv *rdata;
  2309. struct fcoe_rport *frport;
  2310. frport = fcoe_ctlr_rport(new);
  2311. if (frport->flags & FIP_FL_REC_OR_P2P) {
  2312. fcoe_ctlr_vn_send(fip, FIP_SC_VN_PROBE_REQ, fcoe_all_vn2vn, 0);
  2313. return;
  2314. }
  2315. mutex_lock(&lport->disc.disc_mutex);
  2316. rdata = lport->tt.rport_lookup(lport, new->ids.port_id);
  2317. if (rdata)
  2318. kref_get(&rdata->kref);
  2319. mutex_unlock(&lport->disc.disc_mutex);
  2320. if (rdata) {
  2321. if (rdata->ids.node_name == new->ids.node_name &&
  2322. rdata->ids.port_name == new->ids.port_name) {
  2323. frport = fcoe_ctlr_rport(rdata);
  2324. if (!frport->time && fip->state == FIP_ST_VNMP_UP)
  2325. lport->tt.rport_login(rdata);
  2326. frport->time = jiffies;
  2327. }
  2328. kref_put(&rdata->kref, lport->tt.rport_destroy);
  2329. return;
  2330. }
  2331. if (fip->state != FIP_ST_VNMP_UP)
  2332. return;
  2333. /*
  2334. * Beacon from a new neighbor.
  2335. * Send a claim notify if one hasn't been sent recently.
  2336. * Don't add the neighbor yet.
  2337. */
  2338. LIBFCOE_FIP_DBG(fip, "beacon from new rport %x. sending claim notify\n",
  2339. new->ids.port_id);
  2340. if (time_after(jiffies,
  2341. fip->sol_time + msecs_to_jiffies(FIP_VN_ANN_WAIT)))
  2342. fcoe_ctlr_vn_send_claim(fip);
  2343. }
  2344. /**
  2345. * fcoe_ctlr_vn_age() - Check for VN_ports without recent beacons
  2346. * @fip: The FCoE controller
  2347. *
  2348. * Called with ctlr_mutex held.
  2349. * Called only in state FIP_ST_VNMP_UP.
  2350. * Returns the soonest time for next age-out or a time far in the future.
  2351. */
  2352. static unsigned long fcoe_ctlr_vn_age(struct fcoe_ctlr *fip)
  2353. {
  2354. struct fc_lport *lport = fip->lp;
  2355. struct fc_rport_priv *rdata;
  2356. struct fcoe_rport *frport;
  2357. unsigned long next_time;
  2358. unsigned long deadline;
  2359. next_time = jiffies + msecs_to_jiffies(FIP_VN_BEACON_INT * 10);
  2360. mutex_lock(&lport->disc.disc_mutex);
  2361. list_for_each_entry_rcu(rdata, &lport->disc.rports, peers) {
  2362. frport = fcoe_ctlr_rport(rdata);
  2363. if (!frport->time)
  2364. continue;
  2365. deadline = frport->time +
  2366. msecs_to_jiffies(FIP_VN_BEACON_INT * 25 / 10);
  2367. if (time_after_eq(jiffies, deadline)) {
  2368. frport->time = 0;
  2369. LIBFCOE_FIP_DBG(fip,
  2370. "port %16.16llx fc_id %6.6x beacon expired\n",
  2371. rdata->ids.port_name, rdata->ids.port_id);
  2372. lport->tt.rport_logoff(rdata);
  2373. } else if (time_before(deadline, next_time))
  2374. next_time = deadline;
  2375. }
  2376. mutex_unlock(&lport->disc.disc_mutex);
  2377. return next_time;
  2378. }
  2379. /**
  2380. * fcoe_ctlr_vn_recv() - Receive a FIP frame
  2381. * @fip: The FCoE controller that received the frame
  2382. * @skb: The received FIP frame
  2383. *
  2384. * Returns non-zero if the frame is dropped.
  2385. * Always consumes the frame.
  2386. */
  2387. static int fcoe_ctlr_vn_recv(struct fcoe_ctlr *fip, struct sk_buff *skb)
  2388. {
  2389. struct fip_header *fiph;
  2390. enum fip_vn2vn_subcode sub;
  2391. struct {
  2392. struct fc_rport_priv rdata;
  2393. struct fcoe_rport frport;
  2394. } buf;
  2395. int rc;
  2396. fiph = (struct fip_header *)skb->data;
  2397. sub = fiph->fip_subcode;
  2398. rc = fcoe_ctlr_vn_parse(fip, skb, &buf.rdata);
  2399. if (rc) {
  2400. LIBFCOE_FIP_DBG(fip, "vn_recv vn_parse error %d\n", rc);
  2401. goto drop;
  2402. }
  2403. mutex_lock(&fip->ctlr_mutex);
  2404. switch (sub) {
  2405. case FIP_SC_VN_PROBE_REQ:
  2406. fcoe_ctlr_vn_probe_req(fip, &buf.rdata);
  2407. break;
  2408. case FIP_SC_VN_PROBE_REP:
  2409. fcoe_ctlr_vn_probe_reply(fip, &buf.rdata);
  2410. break;
  2411. case FIP_SC_VN_CLAIM_NOTIFY:
  2412. fcoe_ctlr_vn_claim_notify(fip, &buf.rdata);
  2413. break;
  2414. case FIP_SC_VN_CLAIM_REP:
  2415. fcoe_ctlr_vn_claim_resp(fip, &buf.rdata);
  2416. break;
  2417. case FIP_SC_VN_BEACON:
  2418. fcoe_ctlr_vn_beacon(fip, &buf.rdata);
  2419. break;
  2420. default:
  2421. LIBFCOE_FIP_DBG(fip, "vn_recv unknown subcode %d\n", sub);
  2422. rc = -1;
  2423. break;
  2424. }
  2425. mutex_unlock(&fip->ctlr_mutex);
  2426. drop:
  2427. kfree_skb(skb);
  2428. return rc;
  2429. }
  2430. /**
  2431. * fcoe_ctlr_disc_recv - discovery receive handler for VN2VN mode.
  2432. * @lport: The local port
  2433. * @fp: The received frame
  2434. *
  2435. * This should never be called since we don't see RSCNs or other
  2436. * fabric-generated ELSes.
  2437. */
  2438. static void fcoe_ctlr_disc_recv(struct fc_lport *lport, struct fc_frame *fp)
  2439. {
  2440. struct fc_seq_els_data rjt_data;
  2441. rjt_data.reason = ELS_RJT_UNSUP;
  2442. rjt_data.explan = ELS_EXPL_NONE;
  2443. lport->tt.seq_els_rsp_send(fp, ELS_LS_RJT, &rjt_data);
  2444. fc_frame_free(fp);
  2445. }
  2446. /**
  2447. * fcoe_ctlr_disc_recv - start discovery for VN2VN mode.
  2448. * @fip: The FCoE controller
  2449. *
  2450. * This sets a flag indicating that remote ports should be created
  2451. * and started for the peers we discover. We use the disc_callback
  2452. * pointer as that flag. Peers already discovered are created here.
  2453. *
  2454. * The lport lock is held during this call. The callback must be done
  2455. * later, without holding either the lport or discovery locks.
  2456. * The fcoe_ctlr lock may also be held during this call.
  2457. */
  2458. static void fcoe_ctlr_disc_start(void (*callback)(struct fc_lport *,
  2459. enum fc_disc_event),
  2460. struct fc_lport *lport)
  2461. {
  2462. struct fc_disc *disc = &lport->disc;
  2463. struct fcoe_ctlr *fip = disc->priv;
  2464. mutex_lock(&disc->disc_mutex);
  2465. disc->disc_callback = callback;
  2466. disc->disc_id = (disc->disc_id + 2) | 1;
  2467. disc->pending = 1;
  2468. schedule_work(&fip->timer_work);
  2469. mutex_unlock(&disc->disc_mutex);
  2470. }
  2471. /**
  2472. * fcoe_ctlr_vn_disc() - report FIP VN_port discovery results after claim state.
  2473. * @fip: The FCoE controller
  2474. *
  2475. * Starts the FLOGI and PLOGI login process to each discovered rport for which
  2476. * we've received at least one beacon.
  2477. * Performs the discovery complete callback.
  2478. */
  2479. static void fcoe_ctlr_vn_disc(struct fcoe_ctlr *fip)
  2480. {
  2481. struct fc_lport *lport = fip->lp;
  2482. struct fc_disc *disc = &lport->disc;
  2483. struct fc_rport_priv *rdata;
  2484. struct fcoe_rport *frport;
  2485. void (*callback)(struct fc_lport *, enum fc_disc_event);
  2486. mutex_lock(&disc->disc_mutex);
  2487. callback = disc->pending ? disc->disc_callback : NULL;
  2488. disc->pending = 0;
  2489. list_for_each_entry_rcu(rdata, &disc->rports, peers) {
  2490. frport = fcoe_ctlr_rport(rdata);
  2491. if (frport->time)
  2492. lport->tt.rport_login(rdata);
  2493. }
  2494. mutex_unlock(&disc->disc_mutex);
  2495. if (callback)
  2496. callback(lport, DISC_EV_SUCCESS);
  2497. }
  2498. /**
  2499. * fcoe_ctlr_vn_timeout - timer work function for VN2VN mode.
  2500. * @fip: The FCoE controller
  2501. */
  2502. static void fcoe_ctlr_vn_timeout(struct fcoe_ctlr *fip)
  2503. {
  2504. unsigned long next_time;
  2505. u8 mac[ETH_ALEN];
  2506. u32 new_port_id = 0;
  2507. mutex_lock(&fip->ctlr_mutex);
  2508. switch (fip->state) {
  2509. case FIP_ST_VNMP_START:
  2510. fcoe_ctlr_set_state(fip, FIP_ST_VNMP_PROBE1);
  2511. fcoe_ctlr_vn_send(fip, FIP_SC_VN_PROBE_REQ, fcoe_all_vn2vn, 0);
  2512. next_time = jiffies + msecs_to_jiffies(FIP_VN_PROBE_WAIT);
  2513. break;
  2514. case FIP_ST_VNMP_PROBE1:
  2515. fcoe_ctlr_set_state(fip, FIP_ST_VNMP_PROBE2);
  2516. fcoe_ctlr_vn_send(fip, FIP_SC_VN_PROBE_REQ, fcoe_all_vn2vn, 0);
  2517. next_time = jiffies + msecs_to_jiffies(FIP_VN_ANN_WAIT);
  2518. break;
  2519. case FIP_ST_VNMP_PROBE2:
  2520. fcoe_ctlr_set_state(fip, FIP_ST_VNMP_CLAIM);
  2521. new_port_id = fip->port_id;
  2522. hton24(mac, FIP_VN_FC_MAP);
  2523. hton24(mac + 3, new_port_id);
  2524. fcoe_ctlr_map_dest(fip);
  2525. fip->update_mac(fip->lp, mac);
  2526. fcoe_ctlr_vn_send_claim(fip);
  2527. next_time = jiffies + msecs_to_jiffies(FIP_VN_ANN_WAIT);
  2528. break;
  2529. case FIP_ST_VNMP_CLAIM:
  2530. /*
  2531. * This may be invoked either by starting discovery so don't
  2532. * go to the next state unless it's been long enough.
  2533. */
  2534. next_time = fip->sol_time + msecs_to_jiffies(FIP_VN_ANN_WAIT);
  2535. if (time_after_eq(jiffies, next_time)) {
  2536. fcoe_ctlr_set_state(fip, FIP_ST_VNMP_UP);
  2537. fcoe_ctlr_vn_send(fip, FIP_SC_VN_BEACON,
  2538. fcoe_all_vn2vn, 0);
  2539. next_time = jiffies + msecs_to_jiffies(FIP_VN_ANN_WAIT);
  2540. fip->port_ka_time = next_time;
  2541. }
  2542. fcoe_ctlr_vn_disc(fip);
  2543. break;
  2544. case FIP_ST_VNMP_UP:
  2545. next_time = fcoe_ctlr_vn_age(fip);
  2546. if (time_after_eq(jiffies, fip->port_ka_time)) {
  2547. fcoe_ctlr_vn_send(fip, FIP_SC_VN_BEACON,
  2548. fcoe_all_vn2vn, 0);
  2549. fip->port_ka_time = jiffies +
  2550. msecs_to_jiffies(FIP_VN_BEACON_INT +
  2551. (random32() % FIP_VN_BEACON_FUZZ));
  2552. }
  2553. if (time_before(fip->port_ka_time, next_time))
  2554. next_time = fip->port_ka_time;
  2555. break;
  2556. case FIP_ST_LINK_WAIT:
  2557. goto unlock;
  2558. default:
  2559. WARN(1, "unexpected state %d\n", fip->state);
  2560. goto unlock;
  2561. }
  2562. mod_timer(&fip->timer, next_time);
  2563. unlock:
  2564. mutex_unlock(&fip->ctlr_mutex);
  2565. /* If port ID is new, notify local port after dropping ctlr_mutex */
  2566. if (new_port_id)
  2567. fc_lport_set_local_id(fip->lp, new_port_id);
  2568. }
  2569. /**
  2570. * fcoe_libfc_config() - Sets up libfc related properties for local port
  2571. * @lport: The local port to configure libfc for
  2572. * @fip: The FCoE controller in use by the local port
  2573. * @tt: The libfc function template
  2574. * @init_fcp: If non-zero, the FCP portion of libfc should be initialized
  2575. *
  2576. * Returns : 0 for success
  2577. */
  2578. int fcoe_libfc_config(struct fc_lport *lport, struct fcoe_ctlr *fip,
  2579. const struct libfc_function_template *tt, int init_fcp)
  2580. {
  2581. /* Set the function pointers set by the LLDD */
  2582. memcpy(&lport->tt, tt, sizeof(*tt));
  2583. if (init_fcp && fc_fcp_init(lport))
  2584. return -ENOMEM;
  2585. fc_exch_init(lport);
  2586. fc_elsct_init(lport);
  2587. fc_lport_init(lport);
  2588. if (fip->mode == FIP_MODE_VN2VN)
  2589. lport->rport_priv_size = sizeof(struct fcoe_rport);
  2590. fc_rport_init(lport);
  2591. if (fip->mode == FIP_MODE_VN2VN) {
  2592. lport->point_to_multipoint = 1;
  2593. lport->tt.disc_recv_req = fcoe_ctlr_disc_recv;
  2594. lport->tt.disc_start = fcoe_ctlr_disc_start;
  2595. lport->tt.disc_stop = fcoe_ctlr_disc_stop;
  2596. lport->tt.disc_stop_final = fcoe_ctlr_disc_stop_final;
  2597. mutex_init(&lport->disc.disc_mutex);
  2598. INIT_LIST_HEAD(&lport->disc.rports);
  2599. lport->disc.priv = fip;
  2600. } else {
  2601. fc_disc_init(lport);
  2602. }
  2603. return 0;
  2604. }
  2605. EXPORT_SYMBOL_GPL(fcoe_libfc_config);
  2606. void fcoe_fcf_get_selected(struct fcoe_fcf_device *fcf_dev)
  2607. {
  2608. struct fcoe_ctlr_device *ctlr_dev = fcoe_fcf_dev_to_ctlr_dev(fcf_dev);
  2609. struct fcoe_ctlr *fip = fcoe_ctlr_device_priv(ctlr_dev);
  2610. struct fcoe_fcf *fcf;
  2611. mutex_lock(&fip->ctlr_mutex);
  2612. mutex_lock(&ctlr_dev->lock);
  2613. fcf = fcoe_fcf_device_priv(fcf_dev);
  2614. if (fcf)
  2615. fcf_dev->selected = (fcf == fip->sel_fcf) ? 1 : 0;
  2616. else
  2617. fcf_dev->selected = 0;
  2618. mutex_unlock(&ctlr_dev->lock);
  2619. mutex_unlock(&fip->ctlr_mutex);
  2620. }
  2621. EXPORT_SYMBOL(fcoe_fcf_get_selected);
  2622. void fcoe_ctlr_set_fip_mode(struct fcoe_ctlr_device *ctlr_dev)
  2623. {
  2624. struct fcoe_ctlr *ctlr = fcoe_ctlr_device_priv(ctlr_dev);
  2625. mutex_lock(&ctlr->ctlr_mutex);
  2626. switch (ctlr_dev->mode) {
  2627. case FIP_CONN_TYPE_VN2VN:
  2628. ctlr->mode = FIP_MODE_VN2VN;
  2629. break;
  2630. case FIP_CONN_TYPE_FABRIC:
  2631. default:
  2632. ctlr->mode = FIP_MODE_FABRIC;
  2633. break;
  2634. }
  2635. mutex_unlock(&ctlr->ctlr_mutex);
  2636. }
  2637. EXPORT_SYMBOL(fcoe_ctlr_set_fip_mode);