libfcoe.c 69 KB

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