fcoe_ctlr.c 74 KB

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