fcoe_ctlr.c 79 KB

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