fcoe_ctlr.c 73 KB

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