libfcoe.c 68 KB

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