fcoe_ctlr.c 72 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680
  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 is_vn_port = 0;
  1082. LIBFCOE_FIP_DBG(fip, "Clear Virtual Link received\n");
  1083. if (!fcf || !lport->port_id)
  1084. return;
  1085. /*
  1086. * mask of required descriptors. Validating each one clears its bit.
  1087. */
  1088. desc_mask = BIT(FIP_DT_MAC) | BIT(FIP_DT_NAME) | BIT(FIP_DT_VN_ID);
  1089. rlen = ntohs(fh->fip_dl_len) * FIP_BPW;
  1090. desc = (struct fip_desc *)(fh + 1);
  1091. while (rlen >= sizeof(*desc)) {
  1092. dlen = desc->fip_dlen * FIP_BPW;
  1093. if (dlen > rlen)
  1094. return;
  1095. /* Drop CVL if there are duplicate critical descriptors */
  1096. if ((desc->fip_dtype < 32) &&
  1097. !(desc_mask & 1U << desc->fip_dtype)) {
  1098. LIBFCOE_FIP_DBG(fip, "Duplicate Critical "
  1099. "Descriptors in FIP CVL\n");
  1100. return;
  1101. }
  1102. switch (desc->fip_dtype) {
  1103. case FIP_DT_MAC:
  1104. mp = (struct fip_mac_desc *)desc;
  1105. if (dlen < sizeof(*mp))
  1106. return;
  1107. if (compare_ether_addr(mp->fd_mac, fcf->fcf_mac))
  1108. return;
  1109. desc_mask &= ~BIT(FIP_DT_MAC);
  1110. break;
  1111. case FIP_DT_NAME:
  1112. wp = (struct fip_wwn_desc *)desc;
  1113. if (dlen < sizeof(*wp))
  1114. return;
  1115. if (get_unaligned_be64(&wp->fd_wwn) != fcf->switch_name)
  1116. return;
  1117. desc_mask &= ~BIT(FIP_DT_NAME);
  1118. break;
  1119. case FIP_DT_VN_ID:
  1120. vp = (struct fip_vn_desc *)desc;
  1121. if (dlen < sizeof(*vp))
  1122. return;
  1123. if (compare_ether_addr(vp->fd_mac,
  1124. fip->get_src_addr(lport)) == 0 &&
  1125. get_unaligned_be64(&vp->fd_wwpn) == lport->wwpn &&
  1126. ntoh24(vp->fd_fc_id) == lport->port_id) {
  1127. desc_mask &= ~BIT(FIP_DT_VN_ID);
  1128. break;
  1129. }
  1130. /* check if clr_vlink is for NPIV port */
  1131. mutex_lock(&lport->lp_mutex);
  1132. list_for_each_entry(vn_port, &lport->vports, list) {
  1133. if (compare_ether_addr(vp->fd_mac,
  1134. fip->get_src_addr(vn_port)) == 0 &&
  1135. (get_unaligned_be64(&vp->fd_wwpn)
  1136. == vn_port->wwpn) &&
  1137. (ntoh24(vp->fd_fc_id) ==
  1138. fc_host_port_id(vn_port->host))) {
  1139. desc_mask &= ~BIT(FIP_DT_VN_ID);
  1140. is_vn_port = 1;
  1141. break;
  1142. }
  1143. }
  1144. mutex_unlock(&lport->lp_mutex);
  1145. break;
  1146. default:
  1147. /* standard says ignore unknown descriptors >= 128 */
  1148. if (desc->fip_dtype < FIP_DT_VENDOR_BASE)
  1149. return;
  1150. break;
  1151. }
  1152. desc = (struct fip_desc *)((char *)desc + dlen);
  1153. rlen -= dlen;
  1154. }
  1155. /*
  1156. * reset only if all required descriptors were present and valid.
  1157. */
  1158. if (desc_mask) {
  1159. LIBFCOE_FIP_DBG(fip, "missing descriptors mask %x\n",
  1160. desc_mask);
  1161. } else {
  1162. LIBFCOE_FIP_DBG(fip, "performing Clear Virtual Link\n");
  1163. if (is_vn_port)
  1164. fc_lport_reset(vn_port);
  1165. else {
  1166. mutex_lock(&fip->ctlr_mutex);
  1167. per_cpu_ptr(lport->dev_stats,
  1168. get_cpu())->VLinkFailureCount++;
  1169. put_cpu();
  1170. fcoe_ctlr_reset(fip);
  1171. mutex_unlock(&fip->ctlr_mutex);
  1172. fc_lport_reset(fip->lp);
  1173. fcoe_ctlr_solicit(fip, NULL);
  1174. }
  1175. }
  1176. }
  1177. /**
  1178. * fcoe_ctlr_recv() - Receive a FIP packet
  1179. * @fip: The FCoE controller that received the packet
  1180. * @skb: The received FIP packet
  1181. *
  1182. * This may be called from either NET_RX_SOFTIRQ or IRQ.
  1183. */
  1184. void fcoe_ctlr_recv(struct fcoe_ctlr *fip, struct sk_buff *skb)
  1185. {
  1186. skb_queue_tail(&fip->fip_recv_list, skb);
  1187. schedule_work(&fip->recv_work);
  1188. }
  1189. EXPORT_SYMBOL(fcoe_ctlr_recv);
  1190. /**
  1191. * fcoe_ctlr_recv_handler() - Receive a FIP frame
  1192. * @fip: The FCoE controller that received the frame
  1193. * @skb: The received FIP frame
  1194. *
  1195. * Returns non-zero if the frame is dropped.
  1196. */
  1197. static int fcoe_ctlr_recv_handler(struct fcoe_ctlr *fip, struct sk_buff *skb)
  1198. {
  1199. struct fip_header *fiph;
  1200. struct ethhdr *eh;
  1201. enum fip_state state;
  1202. u16 op;
  1203. u8 sub;
  1204. if (skb_linearize(skb))
  1205. goto drop;
  1206. if (skb->len < sizeof(*fiph))
  1207. goto drop;
  1208. eh = eth_hdr(skb);
  1209. if (fip->mode == FIP_MODE_VN2VN) {
  1210. if (compare_ether_addr(eh->h_dest, fip->ctl_src_addr) &&
  1211. compare_ether_addr(eh->h_dest, fcoe_all_vn2vn) &&
  1212. compare_ether_addr(eh->h_dest, fcoe_all_p2p))
  1213. goto drop;
  1214. } else if (compare_ether_addr(eh->h_dest, fip->ctl_src_addr) &&
  1215. compare_ether_addr(eh->h_dest, fcoe_all_enode))
  1216. goto drop;
  1217. fiph = (struct fip_header *)skb->data;
  1218. op = ntohs(fiph->fip_op);
  1219. sub = fiph->fip_subcode;
  1220. if (FIP_VER_DECAPS(fiph->fip_ver) != FIP_VER)
  1221. goto drop;
  1222. if (ntohs(fiph->fip_dl_len) * FIP_BPW + sizeof(*fiph) > skb->len)
  1223. goto drop;
  1224. mutex_lock(&fip->ctlr_mutex);
  1225. state = fip->state;
  1226. if (state == FIP_ST_AUTO) {
  1227. fip->map_dest = 0;
  1228. fcoe_ctlr_set_state(fip, FIP_ST_ENABLED);
  1229. state = FIP_ST_ENABLED;
  1230. LIBFCOE_FIP_DBG(fip, "Using FIP mode\n");
  1231. }
  1232. mutex_unlock(&fip->ctlr_mutex);
  1233. if (fip->mode == FIP_MODE_VN2VN && op == FIP_OP_VN2VN)
  1234. return fcoe_ctlr_vn_recv(fip, skb);
  1235. if (state != FIP_ST_ENABLED && state != FIP_ST_VNMP_UP &&
  1236. state != FIP_ST_VNMP_CLAIM)
  1237. goto drop;
  1238. if (op == FIP_OP_LS) {
  1239. fcoe_ctlr_recv_els(fip, skb); /* consumes skb */
  1240. return 0;
  1241. }
  1242. if (state != FIP_ST_ENABLED)
  1243. goto drop;
  1244. if (op == FIP_OP_DISC && sub == FIP_SC_ADV)
  1245. fcoe_ctlr_recv_adv(fip, skb);
  1246. else if (op == FIP_OP_CTRL && sub == FIP_SC_CLR_VLINK)
  1247. fcoe_ctlr_recv_clr_vlink(fip, fiph);
  1248. kfree_skb(skb);
  1249. return 0;
  1250. drop:
  1251. kfree_skb(skb);
  1252. return -1;
  1253. }
  1254. /**
  1255. * fcoe_ctlr_select() - Select the best FCF (if possible)
  1256. * @fip: The FCoE controller
  1257. *
  1258. * Returns the selected FCF, or NULL if none are usable.
  1259. *
  1260. * If there are conflicting advertisements, no FCF can be chosen.
  1261. *
  1262. * If there is already a selected FCF, this will choose a better one or
  1263. * an equivalent one that hasn't already been sent a FLOGI.
  1264. *
  1265. * Called with lock held.
  1266. */
  1267. static struct fcoe_fcf *fcoe_ctlr_select(struct fcoe_ctlr *fip)
  1268. {
  1269. struct fcoe_fcf *fcf;
  1270. struct fcoe_fcf *best = fip->sel_fcf;
  1271. struct fcoe_fcf *first;
  1272. first = list_first_entry(&fip->fcfs, struct fcoe_fcf, list);
  1273. list_for_each_entry(fcf, &fip->fcfs, list) {
  1274. LIBFCOE_FIP_DBG(fip, "consider FCF fab %16.16llx "
  1275. "VFID %d mac %pM map %x val %d "
  1276. "sent %u pri %u\n",
  1277. fcf->fabric_name, fcf->vfid, fcf->fcf_mac,
  1278. fcf->fc_map, fcoe_ctlr_mtu_valid(fcf),
  1279. fcf->flogi_sent, fcf->pri);
  1280. if (fcf->fabric_name != first->fabric_name ||
  1281. fcf->vfid != first->vfid ||
  1282. fcf->fc_map != first->fc_map) {
  1283. LIBFCOE_FIP_DBG(fip, "Conflicting fabric, VFID, "
  1284. "or FC-MAP\n");
  1285. return NULL;
  1286. }
  1287. if (fcf->flogi_sent)
  1288. continue;
  1289. if (!fcoe_ctlr_fcf_usable(fcf)) {
  1290. LIBFCOE_FIP_DBG(fip, "FCF for fab %16.16llx "
  1291. "map %x %svalid %savailable\n",
  1292. fcf->fabric_name, fcf->fc_map,
  1293. (fcf->flags & FIP_FL_SOL) ? "" : "in",
  1294. (fcf->flags & FIP_FL_AVAIL) ?
  1295. "" : "un");
  1296. continue;
  1297. }
  1298. if (!best || fcf->pri < best->pri || best->flogi_sent)
  1299. best = fcf;
  1300. }
  1301. fip->sel_fcf = best;
  1302. if (best) {
  1303. LIBFCOE_FIP_DBG(fip, "using FCF mac %pM\n", best->fcf_mac);
  1304. fip->port_ka_time = jiffies +
  1305. msecs_to_jiffies(FIP_VN_KA_PERIOD);
  1306. fip->ctlr_ka_time = jiffies + best->fka_period;
  1307. if (time_before(fip->ctlr_ka_time, fip->timer.expires))
  1308. mod_timer(&fip->timer, fip->ctlr_ka_time);
  1309. }
  1310. return best;
  1311. }
  1312. /**
  1313. * fcoe_ctlr_flogi_send_locked() - send FIP-encapsulated FLOGI to current FCF
  1314. * @fip: The FCoE controller
  1315. *
  1316. * Returns non-zero error if it could not be sent.
  1317. *
  1318. * Called with ctlr_mutex and ctlr_lock held.
  1319. * Caller must verify that fip->sel_fcf is not NULL.
  1320. */
  1321. static int fcoe_ctlr_flogi_send_locked(struct fcoe_ctlr *fip)
  1322. {
  1323. struct sk_buff *skb;
  1324. struct sk_buff *skb_orig;
  1325. struct fc_frame_header *fh;
  1326. int error;
  1327. skb_orig = fip->flogi_req;
  1328. if (!skb_orig)
  1329. return -EINVAL;
  1330. /*
  1331. * Clone and send the FLOGI request. If clone fails, use original.
  1332. */
  1333. skb = skb_clone(skb_orig, GFP_ATOMIC);
  1334. if (!skb) {
  1335. skb = skb_orig;
  1336. fip->flogi_req = NULL;
  1337. }
  1338. fh = (struct fc_frame_header *)skb->data;
  1339. error = fcoe_ctlr_encaps(fip, fip->lp, FIP_DT_FLOGI, skb,
  1340. ntoh24(fh->fh_d_id));
  1341. if (error) {
  1342. kfree_skb(skb);
  1343. return error;
  1344. }
  1345. fip->send(fip, skb);
  1346. fip->sel_fcf->flogi_sent = 1;
  1347. return 0;
  1348. }
  1349. /**
  1350. * fcoe_ctlr_flogi_retry() - resend FLOGI request to a new FCF if possible
  1351. * @fip: The FCoE controller
  1352. *
  1353. * Returns non-zero error code if there's no FLOGI request to retry or
  1354. * no alternate FCF available.
  1355. */
  1356. static int fcoe_ctlr_flogi_retry(struct fcoe_ctlr *fip)
  1357. {
  1358. struct fcoe_fcf *fcf;
  1359. int error;
  1360. mutex_lock(&fip->ctlr_mutex);
  1361. spin_lock_bh(&fip->ctlr_lock);
  1362. LIBFCOE_FIP_DBG(fip, "re-sending FLOGI - reselect\n");
  1363. fcf = fcoe_ctlr_select(fip);
  1364. if (!fcf || fcf->flogi_sent) {
  1365. kfree_skb(fip->flogi_req);
  1366. fip->flogi_req = NULL;
  1367. error = -ENOENT;
  1368. } else {
  1369. fcoe_ctlr_solicit(fip, NULL);
  1370. error = fcoe_ctlr_flogi_send_locked(fip);
  1371. }
  1372. spin_unlock_bh(&fip->ctlr_lock);
  1373. mutex_unlock(&fip->ctlr_mutex);
  1374. return error;
  1375. }
  1376. /**
  1377. * fcoe_ctlr_flogi_send() - Handle sending of FIP FLOGI.
  1378. * @fip: The FCoE controller that timed out
  1379. *
  1380. * Done here because fcoe_ctlr_els_send() can't get mutex.
  1381. *
  1382. * Called with ctlr_mutex held. The caller must not hold ctlr_lock.
  1383. */
  1384. static void fcoe_ctlr_flogi_send(struct fcoe_ctlr *fip)
  1385. {
  1386. struct fcoe_fcf *fcf;
  1387. spin_lock_bh(&fip->ctlr_lock);
  1388. fcf = fip->sel_fcf;
  1389. if (!fcf || !fip->flogi_req_send)
  1390. goto unlock;
  1391. LIBFCOE_FIP_DBG(fip, "sending FLOGI\n");
  1392. /*
  1393. * If this FLOGI is being sent due to a timeout retry
  1394. * to the same FCF as before, select a different FCF if possible.
  1395. */
  1396. if (fcf->flogi_sent) {
  1397. LIBFCOE_FIP_DBG(fip, "sending FLOGI - reselect\n");
  1398. fcf = fcoe_ctlr_select(fip);
  1399. if (!fcf || fcf->flogi_sent) {
  1400. LIBFCOE_FIP_DBG(fip, "sending FLOGI - clearing\n");
  1401. list_for_each_entry(fcf, &fip->fcfs, list)
  1402. fcf->flogi_sent = 0;
  1403. fcf = fcoe_ctlr_select(fip);
  1404. }
  1405. }
  1406. if (fcf) {
  1407. fcoe_ctlr_flogi_send_locked(fip);
  1408. fip->flogi_req_send = 0;
  1409. } else /* XXX */
  1410. LIBFCOE_FIP_DBG(fip, "No FCF selected - defer send\n");
  1411. unlock:
  1412. spin_unlock_bh(&fip->ctlr_lock);
  1413. }
  1414. /**
  1415. * fcoe_ctlr_timeout() - FIP timeout handler
  1416. * @arg: The FCoE controller that timed out
  1417. */
  1418. static void fcoe_ctlr_timeout(unsigned long arg)
  1419. {
  1420. struct fcoe_ctlr *fip = (struct fcoe_ctlr *)arg;
  1421. schedule_work(&fip->timer_work);
  1422. }
  1423. /**
  1424. * fcoe_ctlr_timer_work() - Worker thread function for timer work
  1425. * @work: Handle to a FCoE controller
  1426. *
  1427. * Ages FCFs. Triggers FCF selection if possible.
  1428. * Sends keep-alives and resets.
  1429. */
  1430. static void fcoe_ctlr_timer_work(struct work_struct *work)
  1431. {
  1432. struct fcoe_ctlr *fip;
  1433. struct fc_lport *vport;
  1434. u8 *mac;
  1435. u8 reset = 0;
  1436. u8 send_ctlr_ka = 0;
  1437. u8 send_port_ka = 0;
  1438. struct fcoe_fcf *sel;
  1439. struct fcoe_fcf *fcf;
  1440. unsigned long next_timer;
  1441. fip = container_of(work, struct fcoe_ctlr, timer_work);
  1442. if (fip->mode == FIP_MODE_VN2VN)
  1443. return fcoe_ctlr_vn_timeout(fip);
  1444. mutex_lock(&fip->ctlr_mutex);
  1445. if (fip->state == FIP_ST_DISABLED) {
  1446. mutex_unlock(&fip->ctlr_mutex);
  1447. return;
  1448. }
  1449. fcf = fip->sel_fcf;
  1450. next_timer = fcoe_ctlr_age_fcfs(fip);
  1451. sel = fip->sel_fcf;
  1452. if (!sel && fip->sel_time) {
  1453. if (time_after_eq(jiffies, fip->sel_time)) {
  1454. sel = fcoe_ctlr_select(fip);
  1455. fip->sel_time = 0;
  1456. } else if (time_after(next_timer, fip->sel_time))
  1457. next_timer = fip->sel_time;
  1458. }
  1459. if (sel && fip->flogi_req_send)
  1460. fcoe_ctlr_flogi_send(fip);
  1461. else if (!sel && fcf)
  1462. reset = 1;
  1463. if (sel && !sel->fd_flags) {
  1464. if (time_after_eq(jiffies, fip->ctlr_ka_time)) {
  1465. fip->ctlr_ka_time = jiffies + sel->fka_period;
  1466. send_ctlr_ka = 1;
  1467. }
  1468. if (time_after(next_timer, fip->ctlr_ka_time))
  1469. next_timer = fip->ctlr_ka_time;
  1470. if (time_after_eq(jiffies, fip->port_ka_time)) {
  1471. fip->port_ka_time = jiffies +
  1472. msecs_to_jiffies(FIP_VN_KA_PERIOD);
  1473. send_port_ka = 1;
  1474. }
  1475. if (time_after(next_timer, fip->port_ka_time))
  1476. next_timer = fip->port_ka_time;
  1477. }
  1478. if (!list_empty(&fip->fcfs))
  1479. mod_timer(&fip->timer, next_timer);
  1480. mutex_unlock(&fip->ctlr_mutex);
  1481. if (reset) {
  1482. fc_lport_reset(fip->lp);
  1483. /* restart things with a solicitation */
  1484. fcoe_ctlr_solicit(fip, NULL);
  1485. }
  1486. if (send_ctlr_ka)
  1487. fcoe_ctlr_send_keep_alive(fip, NULL, 0, fip->ctl_src_addr);
  1488. if (send_port_ka) {
  1489. mutex_lock(&fip->lp->lp_mutex);
  1490. mac = fip->get_src_addr(fip->lp);
  1491. fcoe_ctlr_send_keep_alive(fip, fip->lp, 1, mac);
  1492. list_for_each_entry(vport, &fip->lp->vports, list) {
  1493. mac = fip->get_src_addr(vport);
  1494. fcoe_ctlr_send_keep_alive(fip, vport, 1, mac);
  1495. }
  1496. mutex_unlock(&fip->lp->lp_mutex);
  1497. }
  1498. }
  1499. /**
  1500. * fcoe_ctlr_recv_work() - Worker thread function for receiving FIP frames
  1501. * @recv_work: Handle to a FCoE controller
  1502. */
  1503. static void fcoe_ctlr_recv_work(struct work_struct *recv_work)
  1504. {
  1505. struct fcoe_ctlr *fip;
  1506. struct sk_buff *skb;
  1507. fip = container_of(recv_work, struct fcoe_ctlr, recv_work);
  1508. while ((skb = skb_dequeue(&fip->fip_recv_list)))
  1509. fcoe_ctlr_recv_handler(fip, skb);
  1510. }
  1511. /**
  1512. * fcoe_ctlr_recv_flogi() - Snoop pre-FIP receipt of FLOGI response
  1513. * @fip: The FCoE controller
  1514. * @fp: The FC frame to snoop
  1515. *
  1516. * Snoop potential response to FLOGI or even incoming FLOGI.
  1517. *
  1518. * The caller has checked that we are waiting for login as indicated
  1519. * by fip->flogi_oxid != FC_XID_UNKNOWN.
  1520. *
  1521. * The caller is responsible for freeing the frame.
  1522. * Fill in the granted_mac address.
  1523. *
  1524. * Return non-zero if the frame should not be delivered to libfc.
  1525. */
  1526. int fcoe_ctlr_recv_flogi(struct fcoe_ctlr *fip, struct fc_lport *lport,
  1527. struct fc_frame *fp)
  1528. {
  1529. struct fc_frame_header *fh;
  1530. u8 op;
  1531. u8 *sa;
  1532. sa = eth_hdr(&fp->skb)->h_source;
  1533. fh = fc_frame_header_get(fp);
  1534. if (fh->fh_type != FC_TYPE_ELS)
  1535. return 0;
  1536. op = fc_frame_payload_op(fp);
  1537. if (op == ELS_LS_ACC && fh->fh_r_ctl == FC_RCTL_ELS_REP &&
  1538. fip->flogi_oxid == ntohs(fh->fh_ox_id)) {
  1539. mutex_lock(&fip->ctlr_mutex);
  1540. if (fip->state != FIP_ST_AUTO && fip->state != FIP_ST_NON_FIP) {
  1541. mutex_unlock(&fip->ctlr_mutex);
  1542. return -EINVAL;
  1543. }
  1544. fcoe_ctlr_set_state(fip, FIP_ST_NON_FIP);
  1545. LIBFCOE_FIP_DBG(fip,
  1546. "received FLOGI LS_ACC using non-FIP mode\n");
  1547. /*
  1548. * FLOGI accepted.
  1549. * If the src mac addr is FC_OUI-based, then we mark the
  1550. * address_mode flag to use FC_OUI-based Ethernet DA.
  1551. * Otherwise we use the FCoE gateway addr
  1552. */
  1553. if (!compare_ether_addr(sa, (u8[6])FC_FCOE_FLOGI_MAC)) {
  1554. fcoe_ctlr_map_dest(fip);
  1555. } else {
  1556. memcpy(fip->dest_addr, sa, ETH_ALEN);
  1557. fip->map_dest = 0;
  1558. }
  1559. fip->flogi_oxid = FC_XID_UNKNOWN;
  1560. mutex_unlock(&fip->ctlr_mutex);
  1561. fc_fcoe_set_mac(fr_cb(fp)->granted_mac, fh->fh_d_id);
  1562. } else if (op == ELS_FLOGI && fh->fh_r_ctl == FC_RCTL_ELS_REQ && sa) {
  1563. /*
  1564. * Save source MAC for point-to-point responses.
  1565. */
  1566. mutex_lock(&fip->ctlr_mutex);
  1567. if (fip->state == FIP_ST_AUTO || fip->state == FIP_ST_NON_FIP) {
  1568. memcpy(fip->dest_addr, sa, ETH_ALEN);
  1569. fip->map_dest = 0;
  1570. if (fip->state == FIP_ST_AUTO)
  1571. LIBFCOE_FIP_DBG(fip, "received non-FIP FLOGI. "
  1572. "Setting non-FIP mode\n");
  1573. fcoe_ctlr_set_state(fip, FIP_ST_NON_FIP);
  1574. }
  1575. mutex_unlock(&fip->ctlr_mutex);
  1576. }
  1577. return 0;
  1578. }
  1579. EXPORT_SYMBOL(fcoe_ctlr_recv_flogi);
  1580. /**
  1581. * fcoe_wwn_from_mac() - Converts a 48-bit IEEE MAC address to a 64-bit FC WWN
  1582. * @mac: The MAC address to convert
  1583. * @scheme: The scheme to use when converting
  1584. * @port: The port indicator for converting
  1585. *
  1586. * Returns: u64 fc world wide name
  1587. */
  1588. u64 fcoe_wwn_from_mac(unsigned char mac[MAX_ADDR_LEN],
  1589. unsigned int scheme, unsigned int port)
  1590. {
  1591. u64 wwn;
  1592. u64 host_mac;
  1593. /* The MAC is in NO, so flip only the low 48 bits */
  1594. host_mac = ((u64) mac[0] << 40) |
  1595. ((u64) mac[1] << 32) |
  1596. ((u64) mac[2] << 24) |
  1597. ((u64) mac[3] << 16) |
  1598. ((u64) mac[4] << 8) |
  1599. (u64) mac[5];
  1600. WARN_ON(host_mac >= (1ULL << 48));
  1601. wwn = host_mac | ((u64) scheme << 60);
  1602. switch (scheme) {
  1603. case 1:
  1604. WARN_ON(port != 0);
  1605. break;
  1606. case 2:
  1607. WARN_ON(port >= 0xfff);
  1608. wwn |= (u64) port << 48;
  1609. break;
  1610. default:
  1611. WARN_ON(1);
  1612. break;
  1613. }
  1614. return wwn;
  1615. }
  1616. EXPORT_SYMBOL_GPL(fcoe_wwn_from_mac);
  1617. /**
  1618. * fcoe_ctlr_rport() - return the fcoe_rport for a given fc_rport_priv
  1619. * @rdata: libfc remote port
  1620. */
  1621. static inline struct fcoe_rport *fcoe_ctlr_rport(struct fc_rport_priv *rdata)
  1622. {
  1623. return (struct fcoe_rport *)(rdata + 1);
  1624. }
  1625. /**
  1626. * fcoe_ctlr_vn_send() - Send a FIP VN2VN Probe Request or Reply.
  1627. * @fip: The FCoE controller
  1628. * @sub: sub-opcode for probe request, reply, or advertisement.
  1629. * @dest: The destination Ethernet MAC address
  1630. * @min_len: minimum size of the Ethernet payload to be sent
  1631. */
  1632. static void fcoe_ctlr_vn_send(struct fcoe_ctlr *fip,
  1633. enum fip_vn2vn_subcode sub,
  1634. const u8 *dest, size_t min_len)
  1635. {
  1636. struct sk_buff *skb;
  1637. struct fip_frame {
  1638. struct ethhdr eth;
  1639. struct fip_header fip;
  1640. struct fip_mac_desc mac;
  1641. struct fip_wwn_desc wwnn;
  1642. struct fip_vn_desc vn;
  1643. } __packed * frame;
  1644. struct fip_fc4_feat *ff;
  1645. struct fip_size_desc *size;
  1646. u32 fcp_feat;
  1647. size_t len;
  1648. size_t dlen;
  1649. len = sizeof(*frame);
  1650. dlen = 0;
  1651. if (sub == FIP_SC_VN_CLAIM_NOTIFY || sub == FIP_SC_VN_CLAIM_REP) {
  1652. dlen = sizeof(struct fip_fc4_feat) +
  1653. sizeof(struct fip_size_desc);
  1654. len += dlen;
  1655. }
  1656. dlen += sizeof(frame->mac) + sizeof(frame->wwnn) + sizeof(frame->vn);
  1657. len = max(len, min_len + sizeof(struct ethhdr));
  1658. skb = dev_alloc_skb(len);
  1659. if (!skb)
  1660. return;
  1661. frame = (struct fip_frame *)skb->data;
  1662. memset(frame, 0, len);
  1663. memcpy(frame->eth.h_dest, dest, ETH_ALEN);
  1664. memcpy(frame->eth.h_source, fip->ctl_src_addr, ETH_ALEN);
  1665. frame->eth.h_proto = htons(ETH_P_FIP);
  1666. frame->fip.fip_ver = FIP_VER_ENCAPS(FIP_VER);
  1667. frame->fip.fip_op = htons(FIP_OP_VN2VN);
  1668. frame->fip.fip_subcode = sub;
  1669. frame->fip.fip_dl_len = htons(dlen / FIP_BPW);
  1670. frame->mac.fd_desc.fip_dtype = FIP_DT_MAC;
  1671. frame->mac.fd_desc.fip_dlen = sizeof(frame->mac) / FIP_BPW;
  1672. memcpy(frame->mac.fd_mac, fip->ctl_src_addr, ETH_ALEN);
  1673. frame->wwnn.fd_desc.fip_dtype = FIP_DT_NAME;
  1674. frame->wwnn.fd_desc.fip_dlen = sizeof(frame->wwnn) / FIP_BPW;
  1675. put_unaligned_be64(fip->lp->wwnn, &frame->wwnn.fd_wwn);
  1676. frame->vn.fd_desc.fip_dtype = FIP_DT_VN_ID;
  1677. frame->vn.fd_desc.fip_dlen = sizeof(frame->vn) / FIP_BPW;
  1678. hton24(frame->vn.fd_mac, FIP_VN_FC_MAP);
  1679. hton24(frame->vn.fd_mac + 3, fip->port_id);
  1680. hton24(frame->vn.fd_fc_id, fip->port_id);
  1681. put_unaligned_be64(fip->lp->wwpn, &frame->vn.fd_wwpn);
  1682. /*
  1683. * For claims, add FC-4 features.
  1684. * TBD: Add interface to get fc-4 types and features from libfc.
  1685. */
  1686. if (sub == FIP_SC_VN_CLAIM_NOTIFY || sub == FIP_SC_VN_CLAIM_REP) {
  1687. ff = (struct fip_fc4_feat *)(frame + 1);
  1688. ff->fd_desc.fip_dtype = FIP_DT_FC4F;
  1689. ff->fd_desc.fip_dlen = sizeof(*ff) / FIP_BPW;
  1690. ff->fd_fts = fip->lp->fcts;
  1691. fcp_feat = 0;
  1692. if (fip->lp->service_params & FCP_SPPF_INIT_FCN)
  1693. fcp_feat |= FCP_FEAT_INIT;
  1694. if (fip->lp->service_params & FCP_SPPF_TARG_FCN)
  1695. fcp_feat |= FCP_FEAT_TARG;
  1696. fcp_feat <<= (FC_TYPE_FCP * 4) % 32;
  1697. ff->fd_ff.fd_feat[FC_TYPE_FCP * 4 / 32] = htonl(fcp_feat);
  1698. size = (struct fip_size_desc *)(ff + 1);
  1699. size->fd_desc.fip_dtype = FIP_DT_FCOE_SIZE;
  1700. size->fd_desc.fip_dlen = sizeof(*size) / FIP_BPW;
  1701. size->fd_size = htons(fcoe_ctlr_fcoe_size(fip));
  1702. }
  1703. skb_put(skb, len);
  1704. skb->protocol = htons(ETH_P_FIP);
  1705. skb_reset_mac_header(skb);
  1706. skb_reset_network_header(skb);
  1707. fip->send(fip, skb);
  1708. }
  1709. /**
  1710. * fcoe_ctlr_vn_rport_callback - Event handler for rport events.
  1711. * @lport: The lport which is receiving the event
  1712. * @rdata: remote port private data
  1713. * @event: The event that occurred
  1714. *
  1715. * Locking Note: The rport lock must not be held when calling this function.
  1716. */
  1717. static void fcoe_ctlr_vn_rport_callback(struct fc_lport *lport,
  1718. struct fc_rport_priv *rdata,
  1719. enum fc_rport_event event)
  1720. {
  1721. struct fcoe_ctlr *fip = lport->disc.priv;
  1722. struct fcoe_rport *frport = fcoe_ctlr_rport(rdata);
  1723. LIBFCOE_FIP_DBG(fip, "vn_rport_callback %x event %d\n",
  1724. rdata->ids.port_id, event);
  1725. mutex_lock(&fip->ctlr_mutex);
  1726. switch (event) {
  1727. case RPORT_EV_READY:
  1728. frport->login_count = 0;
  1729. break;
  1730. case RPORT_EV_LOGO:
  1731. case RPORT_EV_FAILED:
  1732. case RPORT_EV_STOP:
  1733. frport->login_count++;
  1734. if (frport->login_count > FCOE_CTLR_VN2VN_LOGIN_LIMIT) {
  1735. LIBFCOE_FIP_DBG(fip,
  1736. "rport FLOGI limited port_id %6.6x\n",
  1737. rdata->ids.port_id);
  1738. lport->tt.rport_logoff(rdata);
  1739. }
  1740. break;
  1741. default:
  1742. break;
  1743. }
  1744. mutex_unlock(&fip->ctlr_mutex);
  1745. }
  1746. static struct fc_rport_operations fcoe_ctlr_vn_rport_ops = {
  1747. .event_callback = fcoe_ctlr_vn_rport_callback,
  1748. };
  1749. /**
  1750. * fcoe_ctlr_disc_stop_locked() - stop discovery in VN2VN mode
  1751. * @fip: The FCoE controller
  1752. *
  1753. * Called with ctlr_mutex held.
  1754. */
  1755. static void fcoe_ctlr_disc_stop_locked(struct fc_lport *lport)
  1756. {
  1757. mutex_lock(&lport->disc.disc_mutex);
  1758. lport->disc.disc_callback = NULL;
  1759. mutex_unlock(&lport->disc.disc_mutex);
  1760. }
  1761. /**
  1762. * fcoe_ctlr_disc_stop() - stop discovery in VN2VN mode
  1763. * @fip: The FCoE controller
  1764. *
  1765. * Called through the local port template for discovery.
  1766. * Called without the ctlr_mutex held.
  1767. */
  1768. static void fcoe_ctlr_disc_stop(struct fc_lport *lport)
  1769. {
  1770. struct fcoe_ctlr *fip = lport->disc.priv;
  1771. mutex_lock(&fip->ctlr_mutex);
  1772. fcoe_ctlr_disc_stop_locked(lport);
  1773. mutex_unlock(&fip->ctlr_mutex);
  1774. }
  1775. /**
  1776. * fcoe_ctlr_disc_stop_final() - stop discovery for shutdown in VN2VN mode
  1777. * @fip: The FCoE controller
  1778. *
  1779. * Called through the local port template for discovery.
  1780. * Called without the ctlr_mutex held.
  1781. */
  1782. static void fcoe_ctlr_disc_stop_final(struct fc_lport *lport)
  1783. {
  1784. fcoe_ctlr_disc_stop(lport);
  1785. lport->tt.rport_flush_queue();
  1786. synchronize_rcu();
  1787. }
  1788. /**
  1789. * fcoe_ctlr_vn_restart() - VN2VN probe restart with new port_id
  1790. * @fip: The FCoE controller
  1791. *
  1792. * Called with fcoe_ctlr lock held.
  1793. */
  1794. static void fcoe_ctlr_vn_restart(struct fcoe_ctlr *fip)
  1795. {
  1796. unsigned long wait;
  1797. u32 port_id;
  1798. fcoe_ctlr_disc_stop_locked(fip->lp);
  1799. /*
  1800. * Get proposed port ID.
  1801. * If this is the first try after link up, use any previous port_id.
  1802. * If there was none, use the low bits of the port_name.
  1803. * On subsequent tries, get the next random one.
  1804. * Don't use reserved IDs, use another non-zero value, just as random.
  1805. */
  1806. port_id = fip->port_id;
  1807. if (fip->probe_tries)
  1808. port_id = prandom32(&fip->rnd_state) & 0xffff;
  1809. else if (!port_id)
  1810. port_id = fip->lp->wwpn & 0xffff;
  1811. if (!port_id || port_id == 0xffff)
  1812. port_id = 1;
  1813. fip->port_id = port_id;
  1814. if (fip->probe_tries < FIP_VN_RLIM_COUNT) {
  1815. fip->probe_tries++;
  1816. wait = random32() % FIP_VN_PROBE_WAIT;
  1817. } else
  1818. wait = FIP_VN_RLIM_INT;
  1819. mod_timer(&fip->timer, jiffies + msecs_to_jiffies(wait));
  1820. fcoe_ctlr_set_state(fip, FIP_ST_VNMP_START);
  1821. }
  1822. /**
  1823. * fcoe_ctlr_vn_start() - Start in VN2VN mode
  1824. * @fip: The FCoE controller
  1825. *
  1826. * Called with fcoe_ctlr lock held.
  1827. */
  1828. static void fcoe_ctlr_vn_start(struct fcoe_ctlr *fip)
  1829. {
  1830. fip->probe_tries = 0;
  1831. prandom32_seed(&fip->rnd_state, fip->lp->wwpn);
  1832. fcoe_ctlr_vn_restart(fip);
  1833. }
  1834. /**
  1835. * fcoe_ctlr_vn_parse - parse probe request or response
  1836. * @fip: The FCoE controller
  1837. * @skb: incoming packet
  1838. * @rdata: buffer for resulting parsed VN entry plus fcoe_rport
  1839. *
  1840. * Returns non-zero error number on error.
  1841. * Does not consume the packet.
  1842. */
  1843. static int fcoe_ctlr_vn_parse(struct fcoe_ctlr *fip,
  1844. struct sk_buff *skb,
  1845. struct fc_rport_priv *rdata)
  1846. {
  1847. struct fip_header *fiph;
  1848. struct fip_desc *desc = NULL;
  1849. struct fip_mac_desc *macd = NULL;
  1850. struct fip_wwn_desc *wwn = NULL;
  1851. struct fip_vn_desc *vn = NULL;
  1852. struct fip_size_desc *size = NULL;
  1853. struct fcoe_rport *frport;
  1854. size_t rlen;
  1855. size_t dlen;
  1856. u32 desc_mask = 0;
  1857. u32 dtype;
  1858. u8 sub;
  1859. memset(rdata, 0, sizeof(*rdata) + sizeof(*frport));
  1860. frport = fcoe_ctlr_rport(rdata);
  1861. fiph = (struct fip_header *)skb->data;
  1862. frport->flags = ntohs(fiph->fip_flags);
  1863. sub = fiph->fip_subcode;
  1864. switch (sub) {
  1865. case FIP_SC_VN_PROBE_REQ:
  1866. case FIP_SC_VN_PROBE_REP:
  1867. case FIP_SC_VN_BEACON:
  1868. desc_mask = BIT(FIP_DT_MAC) | BIT(FIP_DT_NAME) |
  1869. BIT(FIP_DT_VN_ID);
  1870. break;
  1871. case FIP_SC_VN_CLAIM_NOTIFY:
  1872. case FIP_SC_VN_CLAIM_REP:
  1873. desc_mask = BIT(FIP_DT_MAC) | BIT(FIP_DT_NAME) |
  1874. BIT(FIP_DT_VN_ID) | BIT(FIP_DT_FC4F) |
  1875. BIT(FIP_DT_FCOE_SIZE);
  1876. break;
  1877. default:
  1878. LIBFCOE_FIP_DBG(fip, "vn_parse unknown subcode %u\n", sub);
  1879. return -EINVAL;
  1880. }
  1881. rlen = ntohs(fiph->fip_dl_len) * 4;
  1882. if (rlen + sizeof(*fiph) > skb->len)
  1883. return -EINVAL;
  1884. desc = (struct fip_desc *)(fiph + 1);
  1885. while (rlen > 0) {
  1886. dlen = desc->fip_dlen * FIP_BPW;
  1887. if (dlen < sizeof(*desc) || dlen > rlen)
  1888. return -EINVAL;
  1889. dtype = desc->fip_dtype;
  1890. if (dtype < 32) {
  1891. if (!(desc_mask & BIT(dtype))) {
  1892. LIBFCOE_FIP_DBG(fip,
  1893. "unexpected or duplicated desc "
  1894. "desc type %u in "
  1895. "FIP VN2VN subtype %u\n",
  1896. dtype, sub);
  1897. return -EINVAL;
  1898. }
  1899. desc_mask &= ~BIT(dtype);
  1900. }
  1901. switch (dtype) {
  1902. case FIP_DT_MAC:
  1903. if (dlen != sizeof(struct fip_mac_desc))
  1904. goto len_err;
  1905. macd = (struct fip_mac_desc *)desc;
  1906. if (!is_valid_ether_addr(macd->fd_mac)) {
  1907. LIBFCOE_FIP_DBG(fip,
  1908. "Invalid MAC addr %pM in FIP VN2VN\n",
  1909. macd->fd_mac);
  1910. return -EINVAL;
  1911. }
  1912. memcpy(frport->enode_mac, macd->fd_mac, ETH_ALEN);
  1913. break;
  1914. case FIP_DT_NAME:
  1915. if (dlen != sizeof(struct fip_wwn_desc))
  1916. goto len_err;
  1917. wwn = (struct fip_wwn_desc *)desc;
  1918. rdata->ids.node_name = get_unaligned_be64(&wwn->fd_wwn);
  1919. break;
  1920. case FIP_DT_VN_ID:
  1921. if (dlen != sizeof(struct fip_vn_desc))
  1922. goto len_err;
  1923. vn = (struct fip_vn_desc *)desc;
  1924. memcpy(frport->vn_mac, vn->fd_mac, ETH_ALEN);
  1925. rdata->ids.port_id = ntoh24(vn->fd_fc_id);
  1926. rdata->ids.port_name = get_unaligned_be64(&vn->fd_wwpn);
  1927. break;
  1928. case FIP_DT_FC4F:
  1929. if (dlen != sizeof(struct fip_fc4_feat))
  1930. goto len_err;
  1931. break;
  1932. case FIP_DT_FCOE_SIZE:
  1933. if (dlen != sizeof(struct fip_size_desc))
  1934. goto len_err;
  1935. size = (struct fip_size_desc *)desc;
  1936. frport->fcoe_len = ntohs(size->fd_size);
  1937. break;
  1938. default:
  1939. LIBFCOE_FIP_DBG(fip, "unexpected descriptor type %x "
  1940. "in FIP probe\n", dtype);
  1941. /* standard says ignore unknown descriptors >= 128 */
  1942. if (dtype < FIP_DT_VENDOR_BASE)
  1943. return -EINVAL;
  1944. break;
  1945. }
  1946. desc = (struct fip_desc *)((char *)desc + dlen);
  1947. rlen -= dlen;
  1948. }
  1949. return 0;
  1950. len_err:
  1951. LIBFCOE_FIP_DBG(fip, "FIP length error in descriptor type %x len %zu\n",
  1952. dtype, dlen);
  1953. return -EINVAL;
  1954. }
  1955. /**
  1956. * fcoe_ctlr_vn_send_claim() - send multicast FIP VN2VN Claim Notification.
  1957. * @fip: The FCoE controller
  1958. *
  1959. * Called with ctlr_mutex held.
  1960. */
  1961. static void fcoe_ctlr_vn_send_claim(struct fcoe_ctlr *fip)
  1962. {
  1963. fcoe_ctlr_vn_send(fip, FIP_SC_VN_CLAIM_NOTIFY, fcoe_all_vn2vn, 0);
  1964. fip->sol_time = jiffies;
  1965. }
  1966. /**
  1967. * fcoe_ctlr_vn_probe_req() - handle incoming VN2VN probe request.
  1968. * @fip: The FCoE controller
  1969. * @rdata: parsed remote port with frport from the probe request
  1970. *
  1971. * Called with ctlr_mutex held.
  1972. */
  1973. static void fcoe_ctlr_vn_probe_req(struct fcoe_ctlr *fip,
  1974. struct fc_rport_priv *rdata)
  1975. {
  1976. struct fcoe_rport *frport = fcoe_ctlr_rport(rdata);
  1977. if (rdata->ids.port_id != fip->port_id)
  1978. return;
  1979. switch (fip->state) {
  1980. case FIP_ST_VNMP_CLAIM:
  1981. case FIP_ST_VNMP_UP:
  1982. fcoe_ctlr_vn_send(fip, FIP_SC_VN_PROBE_REP,
  1983. frport->enode_mac, 0);
  1984. break;
  1985. case FIP_ST_VNMP_PROBE1:
  1986. case FIP_ST_VNMP_PROBE2:
  1987. /*
  1988. * Decide whether to reply to the Probe.
  1989. * Our selected address is never a "recorded" one, so
  1990. * only reply if our WWPN is greater and the
  1991. * Probe's REC bit is not set.
  1992. * If we don't reply, we will change our address.
  1993. */
  1994. if (fip->lp->wwpn > rdata->ids.port_name &&
  1995. !(frport->flags & FIP_FL_REC_OR_P2P)) {
  1996. fcoe_ctlr_vn_send(fip, FIP_SC_VN_PROBE_REP,
  1997. frport->enode_mac, 0);
  1998. break;
  1999. }
  2000. /* fall through */
  2001. case FIP_ST_VNMP_START:
  2002. fcoe_ctlr_vn_restart(fip);
  2003. break;
  2004. default:
  2005. break;
  2006. }
  2007. }
  2008. /**
  2009. * fcoe_ctlr_vn_probe_reply() - handle incoming VN2VN probe reply.
  2010. * @fip: The FCoE controller
  2011. * @rdata: parsed remote port with frport from the probe request
  2012. *
  2013. * Called with ctlr_mutex held.
  2014. */
  2015. static void fcoe_ctlr_vn_probe_reply(struct fcoe_ctlr *fip,
  2016. struct fc_rport_priv *rdata)
  2017. {
  2018. if (rdata->ids.port_id != fip->port_id)
  2019. return;
  2020. switch (fip->state) {
  2021. case FIP_ST_VNMP_START:
  2022. case FIP_ST_VNMP_PROBE1:
  2023. case FIP_ST_VNMP_PROBE2:
  2024. case FIP_ST_VNMP_CLAIM:
  2025. fcoe_ctlr_vn_restart(fip);
  2026. break;
  2027. case FIP_ST_VNMP_UP:
  2028. fcoe_ctlr_vn_send_claim(fip);
  2029. break;
  2030. default:
  2031. break;
  2032. }
  2033. }
  2034. /**
  2035. * fcoe_ctlr_vn_add() - Add a VN2VN entry to the list, based on a claim reply.
  2036. * @fip: The FCoE controller
  2037. * @new: newly-parsed remote port with frport as a template for new rdata
  2038. *
  2039. * Called with ctlr_mutex held.
  2040. */
  2041. static void fcoe_ctlr_vn_add(struct fcoe_ctlr *fip, struct fc_rport_priv *new)
  2042. {
  2043. struct fc_lport *lport = fip->lp;
  2044. struct fc_rport_priv *rdata;
  2045. struct fc_rport_identifiers *ids;
  2046. struct fcoe_rport *frport;
  2047. u32 port_id;
  2048. port_id = new->ids.port_id;
  2049. if (port_id == fip->port_id)
  2050. return;
  2051. mutex_lock(&lport->disc.disc_mutex);
  2052. rdata = lport->tt.rport_create(lport, port_id);
  2053. if (!rdata) {
  2054. mutex_unlock(&lport->disc.disc_mutex);
  2055. return;
  2056. }
  2057. rdata->ops = &fcoe_ctlr_vn_rport_ops;
  2058. rdata->disc_id = lport->disc.disc_id;
  2059. ids = &rdata->ids;
  2060. if ((ids->port_name != -1 && ids->port_name != new->ids.port_name) ||
  2061. (ids->node_name != -1 && ids->node_name != new->ids.node_name))
  2062. lport->tt.rport_logoff(rdata);
  2063. ids->port_name = new->ids.port_name;
  2064. ids->node_name = new->ids.node_name;
  2065. mutex_unlock(&lport->disc.disc_mutex);
  2066. frport = fcoe_ctlr_rport(rdata);
  2067. LIBFCOE_FIP_DBG(fip, "vn_add rport %6.6x %s\n",
  2068. port_id, frport->fcoe_len ? "old" : "new");
  2069. *frport = *fcoe_ctlr_rport(new);
  2070. frport->time = 0;
  2071. }
  2072. /**
  2073. * fcoe_ctlr_vn_lookup() - Find VN remote port's MAC address
  2074. * @fip: The FCoE controller
  2075. * @port_id: The port_id of the remote VN_node
  2076. * @mac: buffer which will hold the VN_NODE destination MAC address, if found.
  2077. *
  2078. * Returns non-zero error if no remote port found.
  2079. */
  2080. static int fcoe_ctlr_vn_lookup(struct fcoe_ctlr *fip, u32 port_id, u8 *mac)
  2081. {
  2082. struct fc_lport *lport = fip->lp;
  2083. struct fc_rport_priv *rdata;
  2084. struct fcoe_rport *frport;
  2085. int ret = -1;
  2086. rcu_read_lock();
  2087. rdata = lport->tt.rport_lookup(lport, port_id);
  2088. if (rdata) {
  2089. frport = fcoe_ctlr_rport(rdata);
  2090. memcpy(mac, frport->enode_mac, ETH_ALEN);
  2091. ret = 0;
  2092. }
  2093. rcu_read_unlock();
  2094. return ret;
  2095. }
  2096. /**
  2097. * fcoe_ctlr_vn_claim_notify() - handle received FIP VN2VN Claim Notification
  2098. * @fip: The FCoE controller
  2099. * @new: newly-parsed remote port with frport as a template for new rdata
  2100. *
  2101. * Called with ctlr_mutex held.
  2102. */
  2103. static void fcoe_ctlr_vn_claim_notify(struct fcoe_ctlr *fip,
  2104. struct fc_rport_priv *new)
  2105. {
  2106. struct fcoe_rport *frport = fcoe_ctlr_rport(new);
  2107. if (frport->flags & FIP_FL_REC_OR_P2P) {
  2108. fcoe_ctlr_vn_send(fip, FIP_SC_VN_PROBE_REQ, fcoe_all_vn2vn, 0);
  2109. return;
  2110. }
  2111. switch (fip->state) {
  2112. case FIP_ST_VNMP_START:
  2113. case FIP_ST_VNMP_PROBE1:
  2114. case FIP_ST_VNMP_PROBE2:
  2115. if (new->ids.port_id == fip->port_id)
  2116. fcoe_ctlr_vn_restart(fip);
  2117. break;
  2118. case FIP_ST_VNMP_CLAIM:
  2119. case FIP_ST_VNMP_UP:
  2120. if (new->ids.port_id == fip->port_id) {
  2121. if (new->ids.port_name > fip->lp->wwpn) {
  2122. fcoe_ctlr_vn_restart(fip);
  2123. break;
  2124. }
  2125. fcoe_ctlr_vn_send_claim(fip);
  2126. break;
  2127. }
  2128. fcoe_ctlr_vn_send(fip, FIP_SC_VN_CLAIM_REP, frport->enode_mac,
  2129. min((u32)frport->fcoe_len,
  2130. fcoe_ctlr_fcoe_size(fip)));
  2131. fcoe_ctlr_vn_add(fip, new);
  2132. break;
  2133. default:
  2134. break;
  2135. }
  2136. }
  2137. /**
  2138. * fcoe_ctlr_vn_claim_resp() - handle received Claim Response
  2139. * @fip: The FCoE controller that received the frame
  2140. * @new: newly-parsed remote port with frport from the Claim Response
  2141. *
  2142. * Called with ctlr_mutex held.
  2143. */
  2144. static void fcoe_ctlr_vn_claim_resp(struct fcoe_ctlr *fip,
  2145. struct fc_rport_priv *new)
  2146. {
  2147. LIBFCOE_FIP_DBG(fip, "claim resp from from rport %x - state %s\n",
  2148. new->ids.port_id, fcoe_ctlr_state(fip->state));
  2149. if (fip->state == FIP_ST_VNMP_UP || fip->state == FIP_ST_VNMP_CLAIM)
  2150. fcoe_ctlr_vn_add(fip, new);
  2151. }
  2152. /**
  2153. * fcoe_ctlr_vn_beacon() - handle received beacon.
  2154. * @fip: The FCoE controller that received the frame
  2155. * @new: newly-parsed remote port with frport from the Beacon
  2156. *
  2157. * Called with ctlr_mutex held.
  2158. */
  2159. static void fcoe_ctlr_vn_beacon(struct fcoe_ctlr *fip,
  2160. struct fc_rport_priv *new)
  2161. {
  2162. struct fc_lport *lport = fip->lp;
  2163. struct fc_rport_priv *rdata;
  2164. struct fcoe_rport *frport;
  2165. frport = fcoe_ctlr_rport(new);
  2166. if (frport->flags & FIP_FL_REC_OR_P2P) {
  2167. fcoe_ctlr_vn_send(fip, FIP_SC_VN_PROBE_REQ, fcoe_all_vn2vn, 0);
  2168. return;
  2169. }
  2170. mutex_lock(&lport->disc.disc_mutex);
  2171. rdata = lport->tt.rport_lookup(lport, new->ids.port_id);
  2172. if (rdata)
  2173. kref_get(&rdata->kref);
  2174. mutex_unlock(&lport->disc.disc_mutex);
  2175. if (rdata) {
  2176. if (rdata->ids.node_name == new->ids.node_name &&
  2177. rdata->ids.port_name == new->ids.port_name) {
  2178. frport = fcoe_ctlr_rport(rdata);
  2179. if (!frport->time && fip->state == FIP_ST_VNMP_UP)
  2180. lport->tt.rport_login(rdata);
  2181. frport->time = jiffies;
  2182. }
  2183. kref_put(&rdata->kref, lport->tt.rport_destroy);
  2184. return;
  2185. }
  2186. if (fip->state != FIP_ST_VNMP_UP)
  2187. return;
  2188. /*
  2189. * Beacon from a new neighbor.
  2190. * Send a claim notify if one hasn't been sent recently.
  2191. * Don't add the neighbor yet.
  2192. */
  2193. LIBFCOE_FIP_DBG(fip, "beacon from new rport %x. sending claim notify\n",
  2194. new->ids.port_id);
  2195. if (time_after(jiffies,
  2196. fip->sol_time + msecs_to_jiffies(FIP_VN_ANN_WAIT)))
  2197. fcoe_ctlr_vn_send_claim(fip);
  2198. }
  2199. /**
  2200. * fcoe_ctlr_vn_age() - Check for VN_ports without recent beacons
  2201. * @fip: The FCoE controller
  2202. *
  2203. * Called with ctlr_mutex held.
  2204. * Called only in state FIP_ST_VNMP_UP.
  2205. * Returns the soonest time for next age-out or a time far in the future.
  2206. */
  2207. static unsigned long fcoe_ctlr_vn_age(struct fcoe_ctlr *fip)
  2208. {
  2209. struct fc_lport *lport = fip->lp;
  2210. struct fc_rport_priv *rdata;
  2211. struct fcoe_rport *frport;
  2212. unsigned long next_time;
  2213. unsigned long deadline;
  2214. next_time = jiffies + msecs_to_jiffies(FIP_VN_BEACON_INT * 10);
  2215. mutex_lock(&lport->disc.disc_mutex);
  2216. list_for_each_entry_rcu(rdata, &lport->disc.rports, peers) {
  2217. frport = fcoe_ctlr_rport(rdata);
  2218. if (!frport->time)
  2219. continue;
  2220. deadline = frport->time +
  2221. msecs_to_jiffies(FIP_VN_BEACON_INT * 25 / 10);
  2222. if (time_after_eq(jiffies, deadline)) {
  2223. frport->time = 0;
  2224. LIBFCOE_FIP_DBG(fip,
  2225. "port %16.16llx fc_id %6.6x beacon expired\n",
  2226. rdata->ids.port_name, rdata->ids.port_id);
  2227. lport->tt.rport_logoff(rdata);
  2228. } else if (time_before(deadline, next_time))
  2229. next_time = deadline;
  2230. }
  2231. mutex_unlock(&lport->disc.disc_mutex);
  2232. return next_time;
  2233. }
  2234. /**
  2235. * fcoe_ctlr_vn_recv() - Receive a FIP frame
  2236. * @fip: The FCoE controller that received the frame
  2237. * @skb: The received FIP frame
  2238. *
  2239. * Returns non-zero if the frame is dropped.
  2240. * Always consumes the frame.
  2241. */
  2242. static int fcoe_ctlr_vn_recv(struct fcoe_ctlr *fip, struct sk_buff *skb)
  2243. {
  2244. struct fip_header *fiph;
  2245. enum fip_vn2vn_subcode sub;
  2246. struct {
  2247. struct fc_rport_priv rdata;
  2248. struct fcoe_rport frport;
  2249. } buf;
  2250. int rc;
  2251. fiph = (struct fip_header *)skb->data;
  2252. sub = fiph->fip_subcode;
  2253. rc = fcoe_ctlr_vn_parse(fip, skb, &buf.rdata);
  2254. if (rc) {
  2255. LIBFCOE_FIP_DBG(fip, "vn_recv vn_parse error %d\n", rc);
  2256. goto drop;
  2257. }
  2258. mutex_lock(&fip->ctlr_mutex);
  2259. switch (sub) {
  2260. case FIP_SC_VN_PROBE_REQ:
  2261. fcoe_ctlr_vn_probe_req(fip, &buf.rdata);
  2262. break;
  2263. case FIP_SC_VN_PROBE_REP:
  2264. fcoe_ctlr_vn_probe_reply(fip, &buf.rdata);
  2265. break;
  2266. case FIP_SC_VN_CLAIM_NOTIFY:
  2267. fcoe_ctlr_vn_claim_notify(fip, &buf.rdata);
  2268. break;
  2269. case FIP_SC_VN_CLAIM_REP:
  2270. fcoe_ctlr_vn_claim_resp(fip, &buf.rdata);
  2271. break;
  2272. case FIP_SC_VN_BEACON:
  2273. fcoe_ctlr_vn_beacon(fip, &buf.rdata);
  2274. break;
  2275. default:
  2276. LIBFCOE_FIP_DBG(fip, "vn_recv unknown subcode %d\n", sub);
  2277. rc = -1;
  2278. break;
  2279. }
  2280. mutex_unlock(&fip->ctlr_mutex);
  2281. drop:
  2282. kfree_skb(skb);
  2283. return rc;
  2284. }
  2285. /**
  2286. * fcoe_ctlr_disc_recv - discovery receive handler for VN2VN mode.
  2287. * @lport: The local port
  2288. * @fp: The received frame
  2289. *
  2290. * This should never be called since we don't see RSCNs or other
  2291. * fabric-generated ELSes.
  2292. */
  2293. static void fcoe_ctlr_disc_recv(struct fc_lport *lport, struct fc_frame *fp)
  2294. {
  2295. struct fc_seq_els_data rjt_data;
  2296. rjt_data.reason = ELS_RJT_UNSUP;
  2297. rjt_data.explan = ELS_EXPL_NONE;
  2298. lport->tt.seq_els_rsp_send(fp, ELS_LS_RJT, &rjt_data);
  2299. fc_frame_free(fp);
  2300. }
  2301. /**
  2302. * fcoe_ctlr_disc_recv - start discovery for VN2VN mode.
  2303. * @fip: The FCoE controller
  2304. *
  2305. * This sets a flag indicating that remote ports should be created
  2306. * and started for the peers we discover. We use the disc_callback
  2307. * pointer as that flag. Peers already discovered are created here.
  2308. *
  2309. * The lport lock is held during this call. The callback must be done
  2310. * later, without holding either the lport or discovery locks.
  2311. * The fcoe_ctlr lock may also be held during this call.
  2312. */
  2313. static void fcoe_ctlr_disc_start(void (*callback)(struct fc_lport *,
  2314. enum fc_disc_event),
  2315. struct fc_lport *lport)
  2316. {
  2317. struct fc_disc *disc = &lport->disc;
  2318. struct fcoe_ctlr *fip = disc->priv;
  2319. mutex_lock(&disc->disc_mutex);
  2320. disc->disc_callback = callback;
  2321. disc->disc_id = (disc->disc_id + 2) | 1;
  2322. disc->pending = 1;
  2323. schedule_work(&fip->timer_work);
  2324. mutex_unlock(&disc->disc_mutex);
  2325. }
  2326. /**
  2327. * fcoe_ctlr_vn_disc() - report FIP VN_port discovery results after claim state.
  2328. * @fip: The FCoE controller
  2329. *
  2330. * Starts the FLOGI and PLOGI login process to each discovered rport for which
  2331. * we've received at least one beacon.
  2332. * Performs the discovery complete callback.
  2333. */
  2334. static void fcoe_ctlr_vn_disc(struct fcoe_ctlr *fip)
  2335. {
  2336. struct fc_lport *lport = fip->lp;
  2337. struct fc_disc *disc = &lport->disc;
  2338. struct fc_rport_priv *rdata;
  2339. struct fcoe_rport *frport;
  2340. void (*callback)(struct fc_lport *, enum fc_disc_event);
  2341. mutex_lock(&disc->disc_mutex);
  2342. callback = disc->pending ? disc->disc_callback : NULL;
  2343. disc->pending = 0;
  2344. list_for_each_entry_rcu(rdata, &disc->rports, peers) {
  2345. frport = fcoe_ctlr_rport(rdata);
  2346. if (frport->time)
  2347. lport->tt.rport_login(rdata);
  2348. }
  2349. mutex_unlock(&disc->disc_mutex);
  2350. if (callback)
  2351. callback(lport, DISC_EV_SUCCESS);
  2352. }
  2353. /**
  2354. * fcoe_ctlr_vn_timeout - timer work function for VN2VN mode.
  2355. * @fip: The FCoE controller
  2356. */
  2357. static void fcoe_ctlr_vn_timeout(struct fcoe_ctlr *fip)
  2358. {
  2359. unsigned long next_time;
  2360. u8 mac[ETH_ALEN];
  2361. u32 new_port_id = 0;
  2362. mutex_lock(&fip->ctlr_mutex);
  2363. switch (fip->state) {
  2364. case FIP_ST_VNMP_START:
  2365. fcoe_ctlr_set_state(fip, FIP_ST_VNMP_PROBE1);
  2366. fcoe_ctlr_vn_send(fip, FIP_SC_VN_PROBE_REQ, fcoe_all_vn2vn, 0);
  2367. next_time = jiffies + msecs_to_jiffies(FIP_VN_PROBE_WAIT);
  2368. break;
  2369. case FIP_ST_VNMP_PROBE1:
  2370. fcoe_ctlr_set_state(fip, FIP_ST_VNMP_PROBE2);
  2371. fcoe_ctlr_vn_send(fip, FIP_SC_VN_PROBE_REQ, fcoe_all_vn2vn, 0);
  2372. next_time = jiffies + msecs_to_jiffies(FIP_VN_ANN_WAIT);
  2373. break;
  2374. case FIP_ST_VNMP_PROBE2:
  2375. fcoe_ctlr_set_state(fip, FIP_ST_VNMP_CLAIM);
  2376. new_port_id = fip->port_id;
  2377. hton24(mac, FIP_VN_FC_MAP);
  2378. hton24(mac + 3, new_port_id);
  2379. fcoe_ctlr_map_dest(fip);
  2380. fip->update_mac(fip->lp, mac);
  2381. fcoe_ctlr_vn_send_claim(fip);
  2382. next_time = jiffies + msecs_to_jiffies(FIP_VN_ANN_WAIT);
  2383. break;
  2384. case FIP_ST_VNMP_CLAIM:
  2385. /*
  2386. * This may be invoked either by starting discovery so don't
  2387. * go to the next state unless it's been long enough.
  2388. */
  2389. next_time = fip->sol_time + msecs_to_jiffies(FIP_VN_ANN_WAIT);
  2390. if (time_after_eq(jiffies, next_time)) {
  2391. fcoe_ctlr_set_state(fip, FIP_ST_VNMP_UP);
  2392. fcoe_ctlr_vn_send(fip, FIP_SC_VN_BEACON,
  2393. fcoe_all_vn2vn, 0);
  2394. next_time = jiffies + msecs_to_jiffies(FIP_VN_ANN_WAIT);
  2395. fip->port_ka_time = next_time;
  2396. }
  2397. fcoe_ctlr_vn_disc(fip);
  2398. break;
  2399. case FIP_ST_VNMP_UP:
  2400. next_time = fcoe_ctlr_vn_age(fip);
  2401. if (time_after_eq(jiffies, fip->port_ka_time)) {
  2402. fcoe_ctlr_vn_send(fip, FIP_SC_VN_BEACON,
  2403. fcoe_all_vn2vn, 0);
  2404. fip->port_ka_time = jiffies +
  2405. msecs_to_jiffies(FIP_VN_BEACON_INT +
  2406. (random32() % FIP_VN_BEACON_FUZZ));
  2407. }
  2408. if (time_before(fip->port_ka_time, next_time))
  2409. next_time = fip->port_ka_time;
  2410. break;
  2411. case FIP_ST_LINK_WAIT:
  2412. goto unlock;
  2413. default:
  2414. WARN(1, "unexpected state %d\n", fip->state);
  2415. goto unlock;
  2416. }
  2417. mod_timer(&fip->timer, next_time);
  2418. unlock:
  2419. mutex_unlock(&fip->ctlr_mutex);
  2420. /* If port ID is new, notify local port after dropping ctlr_mutex */
  2421. if (new_port_id)
  2422. fc_lport_set_local_id(fip->lp, new_port_id);
  2423. }
  2424. /**
  2425. * fcoe_libfc_config() - Sets up libfc related properties for local port
  2426. * @lp: The local port to configure libfc for
  2427. * @fip: The FCoE controller in use by the local port
  2428. * @tt: The libfc function template
  2429. * @init_fcp: If non-zero, the FCP portion of libfc should be initialized
  2430. *
  2431. * Returns : 0 for success
  2432. */
  2433. int fcoe_libfc_config(struct fc_lport *lport, struct fcoe_ctlr *fip,
  2434. const struct libfc_function_template *tt, int init_fcp)
  2435. {
  2436. /* Set the function pointers set by the LLDD */
  2437. memcpy(&lport->tt, tt, sizeof(*tt));
  2438. if (init_fcp && fc_fcp_init(lport))
  2439. return -ENOMEM;
  2440. fc_exch_init(lport);
  2441. fc_elsct_init(lport);
  2442. fc_lport_init(lport);
  2443. if (fip->mode == FIP_MODE_VN2VN)
  2444. lport->rport_priv_size = sizeof(struct fcoe_rport);
  2445. fc_rport_init(lport);
  2446. if (fip->mode == FIP_MODE_VN2VN) {
  2447. lport->point_to_multipoint = 1;
  2448. lport->tt.disc_recv_req = fcoe_ctlr_disc_recv;
  2449. lport->tt.disc_start = fcoe_ctlr_disc_start;
  2450. lport->tt.disc_stop = fcoe_ctlr_disc_stop;
  2451. lport->tt.disc_stop_final = fcoe_ctlr_disc_stop_final;
  2452. mutex_init(&lport->disc.disc_mutex);
  2453. INIT_LIST_HEAD(&lport->disc.rports);
  2454. lport->disc.priv = fip;
  2455. } else {
  2456. fc_disc_init(lport);
  2457. }
  2458. return 0;
  2459. }
  2460. EXPORT_SYMBOL_GPL(fcoe_libfc_config);