fcoe_ctlr.c 85 KB

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