fcoe_ctlr.c 88 KB

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