fcoe_ctlr.c 74 KB

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