fcoe_ctlr.c 88 KB

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