fcoe_ctlr.c 78 KB

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