fcoe_ctlr.c 80 KB

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