fcoe_ctlr.c 88 KB

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