fcoe_ctlr.c 74 KB

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