libfcoe.c 73 KB

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