fcoe_ctlr.c 88 KB

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