libfcoe.c 41 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512
  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/libfc.h>
  41. #include <scsi/libfcoe.h>
  42. MODULE_AUTHOR("Open-FCoE.org");
  43. MODULE_DESCRIPTION("FIP discovery protocol support for FCoE HBAs");
  44. MODULE_LICENSE("GPL v2");
  45. #define FCOE_CTLR_MIN_FKA 500 /* min keep alive (mS) */
  46. #define FCOE_CTLR_DEF_FKA FIP_DEF_FKA /* default keep alive (mS) */
  47. static void fcoe_ctlr_timeout(unsigned long);
  48. static void fcoe_ctlr_timer_work(struct work_struct *);
  49. static void fcoe_ctlr_recv_work(struct work_struct *);
  50. static u8 fcoe_all_fcfs[ETH_ALEN] = FIP_ALL_FCF_MACS;
  51. unsigned int libfcoe_debug_logging;
  52. module_param_named(debug_logging, libfcoe_debug_logging, int, S_IRUGO|S_IWUSR);
  53. MODULE_PARM_DESC(debug_logging, "a bit mask of logging levels");
  54. #define LIBFCOE_LOGGING 0x01 /* General logging, not categorized */
  55. #define LIBFCOE_FIP_LOGGING 0x02 /* FIP logging */
  56. #define LIBFCOE_CHECK_LOGGING(LEVEL, CMD) \
  57. do { \
  58. if (unlikely(libfcoe_debug_logging & LEVEL)) \
  59. do { \
  60. CMD; \
  61. } while (0); \
  62. } while (0)
  63. #define LIBFCOE_DBG(fmt, args...) \
  64. LIBFCOE_CHECK_LOGGING(LIBFCOE_LOGGING, \
  65. printk(KERN_INFO "libfcoe: " fmt, ##args);)
  66. #define LIBFCOE_FIP_DBG(fip, fmt, args...) \
  67. LIBFCOE_CHECK_LOGGING(LIBFCOE_FIP_LOGGING, \
  68. printk(KERN_INFO "host%d: fip: " fmt, \
  69. (fip)->lp->host->host_no, ##args);)
  70. /**
  71. * fcoe_ctlr_mtu_valid() - Check if a FCF's MTU is valid
  72. * @fcf: The FCF to check
  73. *
  74. * Return non-zero if FCF fcoe_size has been validated.
  75. */
  76. static inline int fcoe_ctlr_mtu_valid(const struct fcoe_fcf *fcf)
  77. {
  78. return (fcf->flags & FIP_FL_SOL) != 0;
  79. }
  80. /**
  81. * fcoe_ctlr_fcf_usable() - Check if a FCF is usable
  82. * @fcf: The FCF to check
  83. *
  84. * Return non-zero if the FCF is usable.
  85. */
  86. static inline int fcoe_ctlr_fcf_usable(struct fcoe_fcf *fcf)
  87. {
  88. u16 flags = FIP_FL_SOL | FIP_FL_AVAIL;
  89. return (fcf->flags & flags) == flags;
  90. }
  91. /**
  92. * fcoe_ctlr_init() - Initialize the FCoE Controller instance
  93. * @fip: The FCoE controller to initialize
  94. */
  95. void fcoe_ctlr_init(struct fcoe_ctlr *fip)
  96. {
  97. fip->state = FIP_ST_LINK_WAIT;
  98. fip->mode = FIP_ST_AUTO;
  99. INIT_LIST_HEAD(&fip->fcfs);
  100. spin_lock_init(&fip->lock);
  101. fip->flogi_oxid = FC_XID_UNKNOWN;
  102. setup_timer(&fip->timer, fcoe_ctlr_timeout, (unsigned long)fip);
  103. INIT_WORK(&fip->timer_work, fcoe_ctlr_timer_work);
  104. INIT_WORK(&fip->recv_work, fcoe_ctlr_recv_work);
  105. skb_queue_head_init(&fip->fip_recv_list);
  106. }
  107. EXPORT_SYMBOL(fcoe_ctlr_init);
  108. /**
  109. * fcoe_ctlr_reset_fcfs() - Reset and free all FCFs for a controller
  110. * @fip: The FCoE controller whose FCFs are to be reset
  111. *
  112. * Called with &fcoe_ctlr lock held.
  113. */
  114. static void fcoe_ctlr_reset_fcfs(struct fcoe_ctlr *fip)
  115. {
  116. struct fcoe_fcf *fcf;
  117. struct fcoe_fcf *next;
  118. fip->sel_fcf = NULL;
  119. list_for_each_entry_safe(fcf, next, &fip->fcfs, list) {
  120. list_del(&fcf->list);
  121. kfree(fcf);
  122. }
  123. fip->fcf_count = 0;
  124. fip->sel_time = 0;
  125. }
  126. /**
  127. * fcoe_ctlr_destroy() - Disable and tear down a FCoE controller
  128. * @fip: The FCoE controller to tear down
  129. *
  130. * This is called by FCoE drivers before freeing the &fcoe_ctlr.
  131. *
  132. * The receive handler will have been deleted before this to guarantee
  133. * that no more recv_work will be scheduled.
  134. *
  135. * The timer routine will simply return once we set FIP_ST_DISABLED.
  136. * This guarantees that no further timeouts or work will be scheduled.
  137. */
  138. void fcoe_ctlr_destroy(struct fcoe_ctlr *fip)
  139. {
  140. cancel_work_sync(&fip->recv_work);
  141. skb_queue_purge(&fip->fip_recv_list);
  142. spin_lock_bh(&fip->lock);
  143. fip->state = FIP_ST_DISABLED;
  144. fcoe_ctlr_reset_fcfs(fip);
  145. spin_unlock_bh(&fip->lock);
  146. del_timer_sync(&fip->timer);
  147. cancel_work_sync(&fip->timer_work);
  148. }
  149. EXPORT_SYMBOL(fcoe_ctlr_destroy);
  150. /**
  151. * fcoe_ctlr_fcoe_size() - Return the maximum FCoE size required for VN_Port
  152. * @fip: The FCoE controller to get the maximum FCoE size from
  153. *
  154. * Returns the maximum packet size including the FCoE header and trailer,
  155. * but not including any Ethernet or VLAN headers.
  156. */
  157. static inline u32 fcoe_ctlr_fcoe_size(struct fcoe_ctlr *fip)
  158. {
  159. /*
  160. * Determine the max FCoE frame size allowed, including
  161. * FCoE header and trailer.
  162. * Note: lp->mfs is currently the payload size, not the frame size.
  163. */
  164. return fip->lp->mfs + sizeof(struct fc_frame_header) +
  165. sizeof(struct fcoe_hdr) + sizeof(struct fcoe_crc_eof);
  166. }
  167. /**
  168. * fcoe_ctlr_solicit() - Send a FIP solicitation
  169. * @fip: The FCoE controller to send the solicitation on
  170. * @fcf: The destination FCF (if NULL, a multicast solicitation is sent)
  171. */
  172. static void fcoe_ctlr_solicit(struct fcoe_ctlr *fip, struct fcoe_fcf *fcf)
  173. {
  174. struct sk_buff *skb;
  175. struct fip_sol {
  176. struct ethhdr eth;
  177. struct fip_header fip;
  178. struct {
  179. struct fip_mac_desc mac;
  180. struct fip_wwn_desc wwnn;
  181. struct fip_size_desc size;
  182. } __attribute__((packed)) desc;
  183. } __attribute__((packed)) *sol;
  184. u32 fcoe_size;
  185. skb = dev_alloc_skb(sizeof(*sol));
  186. if (!skb)
  187. return;
  188. sol = (struct fip_sol *)skb->data;
  189. memset(sol, 0, sizeof(*sol));
  190. memcpy(sol->eth.h_dest, fcf ? fcf->fcf_mac : fcoe_all_fcfs, ETH_ALEN);
  191. memcpy(sol->eth.h_source, fip->ctl_src_addr, ETH_ALEN);
  192. sol->eth.h_proto = htons(ETH_P_FIP);
  193. sol->fip.fip_ver = FIP_VER_ENCAPS(FIP_VER);
  194. sol->fip.fip_op = htons(FIP_OP_DISC);
  195. sol->fip.fip_subcode = FIP_SC_SOL;
  196. sol->fip.fip_dl_len = htons(sizeof(sol->desc) / FIP_BPW);
  197. sol->fip.fip_flags = htons(FIP_FL_FPMA);
  198. if (fip->spma)
  199. sol->fip.fip_flags |= htons(FIP_FL_SPMA);
  200. sol->desc.mac.fd_desc.fip_dtype = FIP_DT_MAC;
  201. sol->desc.mac.fd_desc.fip_dlen = sizeof(sol->desc.mac) / FIP_BPW;
  202. memcpy(sol->desc.mac.fd_mac, fip->ctl_src_addr, ETH_ALEN);
  203. sol->desc.wwnn.fd_desc.fip_dtype = FIP_DT_NAME;
  204. sol->desc.wwnn.fd_desc.fip_dlen = sizeof(sol->desc.wwnn) / FIP_BPW;
  205. put_unaligned_be64(fip->lp->wwnn, &sol->desc.wwnn.fd_wwn);
  206. fcoe_size = fcoe_ctlr_fcoe_size(fip);
  207. sol->desc.size.fd_desc.fip_dtype = FIP_DT_FCOE_SIZE;
  208. sol->desc.size.fd_desc.fip_dlen = sizeof(sol->desc.size) / FIP_BPW;
  209. sol->desc.size.fd_size = htons(fcoe_size);
  210. skb_put(skb, sizeof(*sol));
  211. skb->protocol = htons(ETH_P_FIP);
  212. skb_reset_mac_header(skb);
  213. skb_reset_network_header(skb);
  214. fip->send(fip, skb);
  215. if (!fcf)
  216. fip->sol_time = jiffies;
  217. }
  218. /**
  219. * fcoe_ctlr_link_up() - Start FCoE controller
  220. * @fip: The FCoE controller to start
  221. *
  222. * Called from the LLD when the network link is ready.
  223. */
  224. void fcoe_ctlr_link_up(struct fcoe_ctlr *fip)
  225. {
  226. spin_lock_bh(&fip->lock);
  227. if (fip->state == FIP_ST_NON_FIP || fip->state == FIP_ST_AUTO) {
  228. spin_unlock_bh(&fip->lock);
  229. fc_linkup(fip->lp);
  230. } else if (fip->state == FIP_ST_LINK_WAIT) {
  231. fip->state = fip->mode;
  232. spin_unlock_bh(&fip->lock);
  233. if (fip->state == FIP_ST_AUTO)
  234. LIBFCOE_FIP_DBG(fip, "%s", "setting AUTO mode.\n");
  235. fc_linkup(fip->lp);
  236. fcoe_ctlr_solicit(fip, NULL);
  237. } else
  238. spin_unlock_bh(&fip->lock);
  239. }
  240. EXPORT_SYMBOL(fcoe_ctlr_link_up);
  241. /**
  242. * fcoe_ctlr_reset() - Reset a FCoE controller
  243. * @fip: The FCoE controller to reset
  244. */
  245. static void fcoe_ctlr_reset(struct fcoe_ctlr *fip)
  246. {
  247. fcoe_ctlr_reset_fcfs(fip);
  248. del_timer(&fip->timer);
  249. fip->ctlr_ka_time = 0;
  250. fip->port_ka_time = 0;
  251. fip->sol_time = 0;
  252. fip->flogi_oxid = FC_XID_UNKNOWN;
  253. fip->map_dest = 0;
  254. }
  255. /**
  256. * fcoe_ctlr_link_down() - Stop a FCoE controller
  257. * @fip: The FCoE controller to be stopped
  258. *
  259. * Returns non-zero if the link was up and now isn't.
  260. *
  261. * Called from the LLD when the network link is not ready.
  262. * There may be multiple calls while the link is down.
  263. */
  264. int fcoe_ctlr_link_down(struct fcoe_ctlr *fip)
  265. {
  266. int link_dropped;
  267. LIBFCOE_FIP_DBG(fip, "link down.\n");
  268. spin_lock_bh(&fip->lock);
  269. fcoe_ctlr_reset(fip);
  270. link_dropped = fip->state != FIP_ST_LINK_WAIT;
  271. fip->state = FIP_ST_LINK_WAIT;
  272. spin_unlock_bh(&fip->lock);
  273. if (link_dropped)
  274. fc_linkdown(fip->lp);
  275. return link_dropped;
  276. }
  277. EXPORT_SYMBOL(fcoe_ctlr_link_down);
  278. /**
  279. * fcoe_ctlr_send_keep_alive() - Send a keep-alive to the selected FCF
  280. * @fip: The FCoE controller to send the FKA on
  281. * @lport: libfc fc_lport to send from
  282. * @ports: 0 for controller keep-alive, 1 for port keep-alive
  283. * @sa: The source MAC address
  284. *
  285. * A controller keep-alive is sent every fka_period (typically 8 seconds).
  286. * The source MAC is the native MAC address.
  287. *
  288. * A port keep-alive is sent every 90 seconds while logged in.
  289. * The source MAC is the assigned mapped source address.
  290. * The destination is the FCF's F-port.
  291. */
  292. static void fcoe_ctlr_send_keep_alive(struct fcoe_ctlr *fip,
  293. struct fc_lport *lport,
  294. int ports, u8 *sa)
  295. {
  296. struct sk_buff *skb;
  297. struct fip_kal {
  298. struct ethhdr eth;
  299. struct fip_header fip;
  300. struct fip_mac_desc mac;
  301. } __attribute__((packed)) *kal;
  302. struct fip_vn_desc *vn;
  303. u32 len;
  304. struct fc_lport *lp;
  305. struct fcoe_fcf *fcf;
  306. fcf = fip->sel_fcf;
  307. lp = fip->lp;
  308. if (!fcf || (ports && !lp->port_id))
  309. return;
  310. len = sizeof(*kal) + ports * sizeof(*vn);
  311. skb = dev_alloc_skb(len);
  312. if (!skb)
  313. return;
  314. kal = (struct fip_kal *)skb->data;
  315. memset(kal, 0, len);
  316. memcpy(kal->eth.h_dest, fcf->fcf_mac, ETH_ALEN);
  317. memcpy(kal->eth.h_source, sa, ETH_ALEN);
  318. kal->eth.h_proto = htons(ETH_P_FIP);
  319. kal->fip.fip_ver = FIP_VER_ENCAPS(FIP_VER);
  320. kal->fip.fip_op = htons(FIP_OP_CTRL);
  321. kal->fip.fip_subcode = FIP_SC_KEEP_ALIVE;
  322. kal->fip.fip_dl_len = htons((sizeof(kal->mac) +
  323. ports * sizeof(*vn)) / FIP_BPW);
  324. kal->fip.fip_flags = htons(FIP_FL_FPMA);
  325. if (fip->spma)
  326. kal->fip.fip_flags |= htons(FIP_FL_SPMA);
  327. kal->mac.fd_desc.fip_dtype = FIP_DT_MAC;
  328. kal->mac.fd_desc.fip_dlen = sizeof(kal->mac) / FIP_BPW;
  329. memcpy(kal->mac.fd_mac, fip->ctl_src_addr, ETH_ALEN);
  330. if (ports) {
  331. vn = (struct fip_vn_desc *)(kal + 1);
  332. vn->fd_desc.fip_dtype = FIP_DT_VN_ID;
  333. vn->fd_desc.fip_dlen = sizeof(*vn) / FIP_BPW;
  334. memcpy(vn->fd_mac, fip->get_src_addr(lport), ETH_ALEN);
  335. hton24(vn->fd_fc_id, lport->port_id);
  336. put_unaligned_be64(lport->wwpn, &vn->fd_wwpn);
  337. }
  338. skb_put(skb, len);
  339. skb->protocol = htons(ETH_P_FIP);
  340. skb_reset_mac_header(skb);
  341. skb_reset_network_header(skb);
  342. fip->send(fip, skb);
  343. }
  344. /**
  345. * fcoe_ctlr_encaps() - Encapsulate an ELS frame for FIP, without sending it
  346. * @fip: The FCoE controller for the ELS frame
  347. * @dtype: The FIP descriptor type for the frame
  348. * @skb: The FCoE ELS frame including FC header but no FCoE headers
  349. *
  350. * Returns non-zero error code on failure.
  351. *
  352. * The caller must check that the length is a multiple of 4.
  353. *
  354. * The @skb must have enough headroom (28 bytes) and tailroom (8 bytes).
  355. * Headroom includes the FIP encapsulation description, FIP header, and
  356. * Ethernet header. The tailroom is for the FIP MAC descriptor.
  357. */
  358. static int fcoe_ctlr_encaps(struct fcoe_ctlr *fip, struct fc_lport *lport,
  359. u8 dtype, struct sk_buff *skb)
  360. {
  361. struct fip_encaps_head {
  362. struct ethhdr eth;
  363. struct fip_header fip;
  364. struct fip_encaps encaps;
  365. } __attribute__((packed)) *cap;
  366. struct fip_mac_desc *mac;
  367. struct fcoe_fcf *fcf;
  368. size_t dlen;
  369. u16 fip_flags;
  370. fcf = fip->sel_fcf;
  371. if (!fcf)
  372. return -ENODEV;
  373. /* set flags according to both FCF and lport's capability on SPMA */
  374. fip_flags = fcf->flags;
  375. fip_flags &= fip->spma ? FIP_FL_SPMA | FIP_FL_FPMA : FIP_FL_FPMA;
  376. if (!fip_flags)
  377. return -ENODEV;
  378. dlen = sizeof(struct fip_encaps) + skb->len; /* len before push */
  379. cap = (struct fip_encaps_head *)skb_push(skb, sizeof(*cap));
  380. memset(cap, 0, sizeof(*cap));
  381. memcpy(cap->eth.h_dest, fcf->fcf_mac, ETH_ALEN);
  382. memcpy(cap->eth.h_source, fip->ctl_src_addr, ETH_ALEN);
  383. cap->eth.h_proto = htons(ETH_P_FIP);
  384. cap->fip.fip_ver = FIP_VER_ENCAPS(FIP_VER);
  385. cap->fip.fip_op = htons(FIP_OP_LS);
  386. cap->fip.fip_subcode = FIP_SC_REQ;
  387. cap->fip.fip_dl_len = htons((dlen + sizeof(*mac)) / FIP_BPW);
  388. cap->fip.fip_flags = htons(fip_flags);
  389. cap->encaps.fd_desc.fip_dtype = dtype;
  390. cap->encaps.fd_desc.fip_dlen = dlen / FIP_BPW;
  391. mac = (struct fip_mac_desc *)skb_put(skb, sizeof(*mac));
  392. memset(mac, 0, sizeof(*mac));
  393. mac->fd_desc.fip_dtype = FIP_DT_MAC;
  394. mac->fd_desc.fip_dlen = sizeof(*mac) / FIP_BPW;
  395. if (dtype != FIP_DT_FLOGI && dtype != FIP_DT_FDISC) {
  396. memcpy(mac->fd_mac, fip->get_src_addr(lport), ETH_ALEN);
  397. } else if (fip_flags & FIP_FL_SPMA) {
  398. LIBFCOE_FIP_DBG(fip, "FLOGI/FDISC sent with SPMA\n");
  399. memcpy(mac->fd_mac, fip->ctl_src_addr, ETH_ALEN);
  400. } else {
  401. LIBFCOE_FIP_DBG(fip, "FLOGI/FDISC sent with FPMA\n");
  402. /* FPMA only FLOGI must leave the MAC desc set to all 0s */
  403. }
  404. skb->protocol = htons(ETH_P_FIP);
  405. skb_reset_mac_header(skb);
  406. skb_reset_network_header(skb);
  407. return 0;
  408. }
  409. /**
  410. * fcoe_ctlr_els_send() - Send an ELS frame encapsulated by FIP if appropriate.
  411. * @fip: FCoE controller.
  412. * @lport: libfc fc_lport to send from
  413. * @skb: FCoE ELS frame including FC header but no FCoE headers.
  414. *
  415. * Returns a non-zero error code if the frame should not be sent.
  416. * Returns zero if the caller should send the frame with FCoE encapsulation.
  417. *
  418. * The caller must check that the length is a multiple of 4.
  419. * The SKB must have enough headroom (28 bytes) and tailroom (8 bytes).
  420. */
  421. int fcoe_ctlr_els_send(struct fcoe_ctlr *fip, struct fc_lport *lport,
  422. struct sk_buff *skb)
  423. {
  424. struct fc_frame_header *fh;
  425. u16 old_xid;
  426. u8 op;
  427. u8 mac[ETH_ALEN];
  428. fh = (struct fc_frame_header *)skb->data;
  429. op = *(u8 *)(fh + 1);
  430. if (op == ELS_FLOGI) {
  431. old_xid = fip->flogi_oxid;
  432. fip->flogi_oxid = ntohs(fh->fh_ox_id);
  433. if (fip->state == FIP_ST_AUTO) {
  434. if (old_xid == FC_XID_UNKNOWN)
  435. fip->flogi_count = 0;
  436. fip->flogi_count++;
  437. if (fip->flogi_count < 3)
  438. goto drop;
  439. fip->map_dest = 1;
  440. return 0;
  441. }
  442. if (fip->state == FIP_ST_NON_FIP)
  443. fip->map_dest = 1;
  444. }
  445. if (fip->state == FIP_ST_NON_FIP)
  446. return 0;
  447. if (!fip->sel_fcf)
  448. goto drop;
  449. switch (op) {
  450. case ELS_FLOGI:
  451. op = FIP_DT_FLOGI;
  452. break;
  453. case ELS_FDISC:
  454. if (ntoh24(fh->fh_s_id))
  455. return 0;
  456. op = FIP_DT_FDISC;
  457. break;
  458. case ELS_LOGO:
  459. if (fip->state != FIP_ST_ENABLED)
  460. return 0;
  461. if (ntoh24(fh->fh_d_id) != FC_FID_FLOGI)
  462. return 0;
  463. op = FIP_DT_LOGO;
  464. break;
  465. case ELS_LS_ACC:
  466. if (fip->flogi_oxid == FC_XID_UNKNOWN)
  467. return 0;
  468. if (!ntoh24(fh->fh_s_id))
  469. return 0;
  470. if (fip->state == FIP_ST_AUTO)
  471. return 0;
  472. /*
  473. * Here we must've gotten an SID by accepting an FLOGI
  474. * from a point-to-point connection. Switch to using
  475. * the source mac based on the SID. The destination
  476. * MAC in this case would have been set by receving the
  477. * FLOGI.
  478. */
  479. fip->flogi_oxid = FC_XID_UNKNOWN;
  480. fc_fcoe_set_mac(mac, fh->fh_d_id);
  481. fip->update_mac(lport, mac);
  482. return 0;
  483. default:
  484. if (fip->state != FIP_ST_ENABLED)
  485. goto drop;
  486. return 0;
  487. }
  488. if (fcoe_ctlr_encaps(fip, lport, op, skb))
  489. goto drop;
  490. fip->send(fip, skb);
  491. return -EINPROGRESS;
  492. drop:
  493. kfree_skb(skb);
  494. return -EINVAL;
  495. }
  496. EXPORT_SYMBOL(fcoe_ctlr_els_send);
  497. /**
  498. * fcoe_ctlr_age_fcfs() - Reset and free all old FCFs for a controller
  499. * @fip: The FCoE controller to free FCFs on
  500. *
  501. * Called with lock held and preemption disabled.
  502. *
  503. * An FCF is considered old if we have missed two advertisements.
  504. * That is, there have been no valid advertisement from it for 2.5
  505. * times its keep-alive period.
  506. *
  507. * In addition, determine the time when an FCF selection can occur.
  508. *
  509. * Also, increment the MissDiscAdvCount when no advertisement is received
  510. * for the corresponding FCF for 1.5 * FKA_ADV_PERIOD (FC-BB-5 LESB).
  511. *
  512. * Returns the time in jiffies for the next call.
  513. */
  514. static unsigned long fcoe_ctlr_age_fcfs(struct fcoe_ctlr *fip)
  515. {
  516. struct fcoe_fcf *fcf;
  517. struct fcoe_fcf *next;
  518. unsigned long next_timer = jiffies + msecs_to_jiffies(FIP_VN_KA_PERIOD);
  519. unsigned long deadline;
  520. unsigned long sel_time = 0;
  521. struct fcoe_dev_stats *stats;
  522. list_for_each_entry_safe(fcf, next, &fip->fcfs, list) {
  523. deadline = fcf->time + fcf->fka_period + fcf->fka_period / 2;
  524. if (fip->sel_fcf == fcf) {
  525. if (time_after(jiffies, deadline)) {
  526. stats = per_cpu_ptr(fip->lp->dev_stats,
  527. smp_processor_id());
  528. stats->MissDiscAdvCount++;
  529. printk(KERN_INFO "libfcoe: host%d: "
  530. "Missing Discovery Advertisement "
  531. "for fab %16.16llx count %lld\n",
  532. fip->lp->host->host_no, fcf->fabric_name,
  533. stats->MissDiscAdvCount);
  534. } else if (time_after(next_timer, deadline))
  535. next_timer = deadline;
  536. }
  537. deadline += fcf->fka_period;
  538. if (time_after_eq(jiffies, deadline)) {
  539. if (fip->sel_fcf == fcf)
  540. fip->sel_fcf = NULL;
  541. list_del(&fcf->list);
  542. WARN_ON(!fip->fcf_count);
  543. fip->fcf_count--;
  544. kfree(fcf);
  545. stats = per_cpu_ptr(fip->lp->dev_stats,
  546. smp_processor_id());
  547. stats->VLinkFailureCount++;
  548. } else {
  549. if (time_after(next_timer, deadline))
  550. next_timer = deadline;
  551. if (fcoe_ctlr_mtu_valid(fcf) &&
  552. (!sel_time || time_before(sel_time, fcf->time)))
  553. sel_time = fcf->time;
  554. }
  555. }
  556. if (sel_time && !fip->sel_fcf && !fip->sel_time) {
  557. sel_time += msecs_to_jiffies(FCOE_CTLR_START_DELAY);
  558. fip->sel_time = sel_time;
  559. }
  560. return next_timer;
  561. }
  562. /**
  563. * fcoe_ctlr_parse_adv() - Decode a FIP advertisement into a new FCF entry
  564. * @fip: The FCoE controller receiving the advertisement
  565. * @skb: The received FIP advertisement frame
  566. * @fcf: The resulting FCF entry
  567. *
  568. * Returns zero on a valid parsed advertisement,
  569. * otherwise returns non zero value.
  570. */
  571. static int fcoe_ctlr_parse_adv(struct fcoe_ctlr *fip,
  572. struct sk_buff *skb, struct fcoe_fcf *fcf)
  573. {
  574. struct fip_header *fiph;
  575. struct fip_desc *desc = NULL;
  576. struct fip_wwn_desc *wwn;
  577. struct fip_fab_desc *fab;
  578. struct fip_fka_desc *fka;
  579. unsigned long t;
  580. size_t rlen;
  581. size_t dlen;
  582. u32 desc_mask;
  583. memset(fcf, 0, sizeof(*fcf));
  584. fcf->fka_period = msecs_to_jiffies(FCOE_CTLR_DEF_FKA);
  585. fiph = (struct fip_header *)skb->data;
  586. fcf->flags = ntohs(fiph->fip_flags);
  587. /*
  588. * mask of required descriptors. validating each one clears its bit.
  589. */
  590. desc_mask = BIT(FIP_DT_PRI) | BIT(FIP_DT_MAC) | BIT(FIP_DT_NAME) |
  591. BIT(FIP_DT_FAB) | BIT(FIP_DT_FKA);
  592. rlen = ntohs(fiph->fip_dl_len) * 4;
  593. if (rlen + sizeof(*fiph) > skb->len)
  594. return -EINVAL;
  595. desc = (struct fip_desc *)(fiph + 1);
  596. while (rlen > 0) {
  597. dlen = desc->fip_dlen * FIP_BPW;
  598. if (dlen < sizeof(*desc) || dlen > rlen)
  599. return -EINVAL;
  600. /* Drop Adv if there are duplicate critical descriptors */
  601. if ((desc->fip_dtype < 32) &&
  602. !(desc_mask & 1U << desc->fip_dtype)) {
  603. LIBFCOE_FIP_DBG(fip, "Duplicate Critical "
  604. "Descriptors in FIP adv\n");
  605. return -EINVAL;
  606. }
  607. switch (desc->fip_dtype) {
  608. case FIP_DT_PRI:
  609. if (dlen != sizeof(struct fip_pri_desc))
  610. goto len_err;
  611. fcf->pri = ((struct fip_pri_desc *)desc)->fd_pri;
  612. desc_mask &= ~BIT(FIP_DT_PRI);
  613. break;
  614. case FIP_DT_MAC:
  615. if (dlen != sizeof(struct fip_mac_desc))
  616. goto len_err;
  617. memcpy(fcf->fcf_mac,
  618. ((struct fip_mac_desc *)desc)->fd_mac,
  619. ETH_ALEN);
  620. if (!is_valid_ether_addr(fcf->fcf_mac)) {
  621. LIBFCOE_FIP_DBG(fip, "Invalid MAC address "
  622. "in FIP adv\n");
  623. return -EINVAL;
  624. }
  625. desc_mask &= ~BIT(FIP_DT_MAC);
  626. break;
  627. case FIP_DT_NAME:
  628. if (dlen != sizeof(struct fip_wwn_desc))
  629. goto len_err;
  630. wwn = (struct fip_wwn_desc *)desc;
  631. fcf->switch_name = get_unaligned_be64(&wwn->fd_wwn);
  632. desc_mask &= ~BIT(FIP_DT_NAME);
  633. break;
  634. case FIP_DT_FAB:
  635. if (dlen != sizeof(struct fip_fab_desc))
  636. goto len_err;
  637. fab = (struct fip_fab_desc *)desc;
  638. fcf->fabric_name = get_unaligned_be64(&fab->fd_wwn);
  639. fcf->vfid = ntohs(fab->fd_vfid);
  640. fcf->fc_map = ntoh24(fab->fd_map);
  641. desc_mask &= ~BIT(FIP_DT_FAB);
  642. break;
  643. case FIP_DT_FKA:
  644. if (dlen != sizeof(struct fip_fka_desc))
  645. goto len_err;
  646. fka = (struct fip_fka_desc *)desc;
  647. if (fka->fd_flags & FIP_FKA_ADV_D)
  648. fcf->fd_flags = 1;
  649. t = ntohl(fka->fd_fka_period);
  650. if (t >= FCOE_CTLR_MIN_FKA)
  651. fcf->fka_period = msecs_to_jiffies(t);
  652. desc_mask &= ~BIT(FIP_DT_FKA);
  653. break;
  654. case FIP_DT_MAP_OUI:
  655. case FIP_DT_FCOE_SIZE:
  656. case FIP_DT_FLOGI:
  657. case FIP_DT_FDISC:
  658. case FIP_DT_LOGO:
  659. case FIP_DT_ELP:
  660. default:
  661. LIBFCOE_FIP_DBG(fip, "unexpected descriptor type %x "
  662. "in FIP adv\n", desc->fip_dtype);
  663. /* standard says ignore unknown descriptors >= 128 */
  664. if (desc->fip_dtype < FIP_DT_VENDOR_BASE)
  665. return -EINVAL;
  666. break;
  667. }
  668. desc = (struct fip_desc *)((char *)desc + dlen);
  669. rlen -= dlen;
  670. }
  671. if (!fcf->fc_map || (fcf->fc_map & 0x10000))
  672. return -EINVAL;
  673. if (!fcf->switch_name || !fcf->fabric_name)
  674. return -EINVAL;
  675. if (desc_mask) {
  676. LIBFCOE_FIP_DBG(fip, "adv missing descriptors mask %x\n",
  677. desc_mask);
  678. return -EINVAL;
  679. }
  680. return 0;
  681. len_err:
  682. LIBFCOE_FIP_DBG(fip, "FIP length error in descriptor type %x len %zu\n",
  683. desc->fip_dtype, dlen);
  684. return -EINVAL;
  685. }
  686. /**
  687. * fcoe_ctlr_recv_adv() - Handle an incoming advertisement
  688. * @fip: The FCoE controller receiving the advertisement
  689. * @skb: The received FIP packet
  690. */
  691. static void fcoe_ctlr_recv_adv(struct fcoe_ctlr *fip, struct sk_buff *skb)
  692. {
  693. struct fcoe_fcf *fcf;
  694. struct fcoe_fcf new;
  695. struct fcoe_fcf *found;
  696. unsigned long sol_tov = msecs_to_jiffies(FCOE_CTRL_SOL_TOV);
  697. int first = 0;
  698. int mtu_valid;
  699. if (fcoe_ctlr_parse_adv(fip, skb, &new))
  700. return;
  701. spin_lock_bh(&fip->lock);
  702. first = list_empty(&fip->fcfs);
  703. found = NULL;
  704. list_for_each_entry(fcf, &fip->fcfs, list) {
  705. if (fcf->switch_name == new.switch_name &&
  706. fcf->fabric_name == new.fabric_name &&
  707. fcf->fc_map == new.fc_map &&
  708. compare_ether_addr(fcf->fcf_mac, new.fcf_mac) == 0) {
  709. found = fcf;
  710. break;
  711. }
  712. }
  713. if (!found) {
  714. if (fip->fcf_count >= FCOE_CTLR_FCF_LIMIT)
  715. goto out;
  716. fcf = kmalloc(sizeof(*fcf), GFP_ATOMIC);
  717. if (!fcf)
  718. goto out;
  719. fip->fcf_count++;
  720. memcpy(fcf, &new, sizeof(new));
  721. list_add(&fcf->list, &fip->fcfs);
  722. } else {
  723. /*
  724. * Update the FCF's keep-alive descriptor flags.
  725. * Other flag changes from new advertisements are
  726. * ignored after a solicited advertisement is
  727. * received and the FCF is selectable (usable).
  728. */
  729. fcf->fd_flags = new.fd_flags;
  730. if (!fcoe_ctlr_fcf_usable(fcf))
  731. fcf->flags = new.flags;
  732. if (fcf == fip->sel_fcf && !fcf->fd_flags) {
  733. fip->ctlr_ka_time -= fcf->fka_period;
  734. fip->ctlr_ka_time += new.fka_period;
  735. if (time_before(fip->ctlr_ka_time, fip->timer.expires))
  736. mod_timer(&fip->timer, fip->ctlr_ka_time);
  737. }
  738. fcf->fka_period = new.fka_period;
  739. memcpy(fcf->fcf_mac, new.fcf_mac, ETH_ALEN);
  740. }
  741. mtu_valid = fcoe_ctlr_mtu_valid(fcf);
  742. fcf->time = jiffies;
  743. if (!found) {
  744. LIBFCOE_FIP_DBG(fip, "New FCF for fab %16.16llx "
  745. "map %x val %d\n",
  746. fcf->fabric_name, fcf->fc_map, mtu_valid);
  747. }
  748. /*
  749. * If this advertisement is not solicited and our max receive size
  750. * hasn't been verified, send a solicited advertisement.
  751. */
  752. if (!mtu_valid)
  753. fcoe_ctlr_solicit(fip, fcf);
  754. /*
  755. * If its been a while since we did a solicit, and this is
  756. * the first advertisement we've received, do a multicast
  757. * solicitation to gather as many advertisements as we can
  758. * before selection occurs.
  759. */
  760. if (first && time_after(jiffies, fip->sol_time + sol_tov))
  761. fcoe_ctlr_solicit(fip, NULL);
  762. /*
  763. * If this is the first validated FCF, note the time and
  764. * set a timer to trigger selection.
  765. */
  766. if (mtu_valid && !fip->sel_fcf && fcoe_ctlr_fcf_usable(fcf)) {
  767. fip->sel_time = jiffies +
  768. msecs_to_jiffies(FCOE_CTLR_START_DELAY);
  769. if (!timer_pending(&fip->timer) ||
  770. time_before(fip->sel_time, fip->timer.expires))
  771. mod_timer(&fip->timer, fip->sel_time);
  772. }
  773. out:
  774. spin_unlock_bh(&fip->lock);
  775. }
  776. /**
  777. * fcoe_ctlr_recv_els() - Handle an incoming FIP encapsulated ELS frame
  778. * @fip: The FCoE controller which received the packet
  779. * @skb: The received FIP packet
  780. */
  781. static void fcoe_ctlr_recv_els(struct fcoe_ctlr *fip, struct sk_buff *skb)
  782. {
  783. struct fc_lport *lport = fip->lp;
  784. struct fip_header *fiph;
  785. struct fc_frame *fp = (struct fc_frame *)skb;
  786. struct fc_frame_header *fh = NULL;
  787. struct fip_desc *desc;
  788. struct fip_encaps *els;
  789. struct fcoe_dev_stats *stats;
  790. enum fip_desc_type els_dtype = 0;
  791. u8 els_op;
  792. u8 sub;
  793. u8 granted_mac[ETH_ALEN] = { 0 };
  794. size_t els_len = 0;
  795. size_t rlen;
  796. size_t dlen;
  797. u32 desc_mask = 0;
  798. u32 desc_cnt = 0;
  799. fiph = (struct fip_header *)skb->data;
  800. sub = fiph->fip_subcode;
  801. if (sub != FIP_SC_REQ && sub != FIP_SC_REP)
  802. goto drop;
  803. rlen = ntohs(fiph->fip_dl_len) * 4;
  804. if (rlen + sizeof(*fiph) > skb->len)
  805. goto drop;
  806. desc = (struct fip_desc *)(fiph + 1);
  807. while (rlen > 0) {
  808. desc_cnt++;
  809. dlen = desc->fip_dlen * FIP_BPW;
  810. if (dlen < sizeof(*desc) || dlen > rlen)
  811. goto drop;
  812. /* Drop ELS if there are duplicate critical descriptors */
  813. if (desc->fip_dtype < 32) {
  814. if (desc_mask & 1U << desc->fip_dtype) {
  815. LIBFCOE_FIP_DBG(fip, "Duplicate Critical "
  816. "Descriptors in FIP ELS\n");
  817. goto drop;
  818. }
  819. desc_mask |= (1 << desc->fip_dtype);
  820. }
  821. switch (desc->fip_dtype) {
  822. case FIP_DT_MAC:
  823. if (desc_cnt == 1) {
  824. LIBFCOE_FIP_DBG(fip, "FIP descriptors "
  825. "received out of order\n");
  826. goto drop;
  827. }
  828. if (dlen != sizeof(struct fip_mac_desc))
  829. goto len_err;
  830. memcpy(granted_mac,
  831. ((struct fip_mac_desc *)desc)->fd_mac,
  832. ETH_ALEN);
  833. if (!is_valid_ether_addr(granted_mac)) {
  834. LIBFCOE_FIP_DBG(fip, "Invalid MAC address "
  835. "in FIP ELS\n");
  836. goto drop;
  837. }
  838. memcpy(fr_cb(fp)->granted_mac, granted_mac, ETH_ALEN);
  839. break;
  840. case FIP_DT_FLOGI:
  841. case FIP_DT_FDISC:
  842. case FIP_DT_LOGO:
  843. case FIP_DT_ELP:
  844. if (desc_cnt != 1) {
  845. LIBFCOE_FIP_DBG(fip, "FIP descriptors "
  846. "received out of order\n");
  847. goto drop;
  848. }
  849. if (fh)
  850. goto drop;
  851. if (dlen < sizeof(*els) + sizeof(*fh) + 1)
  852. goto len_err;
  853. els_len = dlen - sizeof(*els);
  854. els = (struct fip_encaps *)desc;
  855. fh = (struct fc_frame_header *)(els + 1);
  856. els_dtype = desc->fip_dtype;
  857. break;
  858. default:
  859. LIBFCOE_FIP_DBG(fip, "unexpected descriptor type %x "
  860. "in FIP adv\n", desc->fip_dtype);
  861. /* standard says ignore unknown descriptors >= 128 */
  862. if (desc->fip_dtype < FIP_DT_VENDOR_BASE)
  863. goto drop;
  864. if (desc_cnt <= 2) {
  865. LIBFCOE_FIP_DBG(fip, "FIP descriptors "
  866. "received out of order\n");
  867. goto drop;
  868. }
  869. break;
  870. }
  871. desc = (struct fip_desc *)((char *)desc + dlen);
  872. rlen -= dlen;
  873. }
  874. if (!fh)
  875. goto drop;
  876. els_op = *(u8 *)(fh + 1);
  877. if (els_dtype == FIP_DT_FLOGI && sub == FIP_SC_REP &&
  878. fip->flogi_oxid == ntohs(fh->fh_ox_id) &&
  879. els_op == ELS_LS_ACC && is_valid_ether_addr(granted_mac))
  880. fip->flogi_oxid = FC_XID_UNKNOWN;
  881. if ((desc_cnt == 0) || ((els_op != ELS_LS_RJT) &&
  882. (!(1U << FIP_DT_MAC & desc_mask)))) {
  883. LIBFCOE_FIP_DBG(fip, "Missing critical descriptors "
  884. "in FIP ELS\n");
  885. goto drop;
  886. }
  887. /*
  888. * Convert skb into an fc_frame containing only the ELS.
  889. */
  890. skb_pull(skb, (u8 *)fh - skb->data);
  891. skb_trim(skb, els_len);
  892. fp = (struct fc_frame *)skb;
  893. fc_frame_init(fp);
  894. fr_sof(fp) = FC_SOF_I3;
  895. fr_eof(fp) = FC_EOF_T;
  896. fr_dev(fp) = lport;
  897. stats = per_cpu_ptr(lport->dev_stats, get_cpu());
  898. stats->RxFrames++;
  899. stats->RxWords += skb->len / FIP_BPW;
  900. put_cpu();
  901. fc_exch_recv(lport, fp);
  902. return;
  903. len_err:
  904. LIBFCOE_FIP_DBG(fip, "FIP length error in descriptor type %x len %zu\n",
  905. desc->fip_dtype, dlen);
  906. drop:
  907. kfree_skb(skb);
  908. }
  909. /**
  910. * fcoe_ctlr_recv_els() - Handle an incoming link reset frame
  911. * @fip: The FCoE controller that received the frame
  912. * @fh: The received FIP header
  913. *
  914. * There may be multiple VN_Port descriptors.
  915. * The overall length has already been checked.
  916. */
  917. static void fcoe_ctlr_recv_clr_vlink(struct fcoe_ctlr *fip,
  918. struct fip_header *fh)
  919. {
  920. struct fip_desc *desc;
  921. struct fip_mac_desc *mp;
  922. struct fip_wwn_desc *wp;
  923. struct fip_vn_desc *vp;
  924. size_t rlen;
  925. size_t dlen;
  926. struct fcoe_fcf *fcf = fip->sel_fcf;
  927. struct fc_lport *lport = fip->lp;
  928. struct fc_lport *vn_port = NULL;
  929. u32 desc_mask;
  930. int is_vn_port = 0;
  931. LIBFCOE_FIP_DBG(fip, "Clear Virtual Link received\n");
  932. if (!fcf || !lport->port_id)
  933. return;
  934. /*
  935. * mask of required descriptors. Validating each one clears its bit.
  936. */
  937. desc_mask = BIT(FIP_DT_MAC) | BIT(FIP_DT_NAME) | BIT(FIP_DT_VN_ID);
  938. rlen = ntohs(fh->fip_dl_len) * FIP_BPW;
  939. desc = (struct fip_desc *)(fh + 1);
  940. while (rlen >= sizeof(*desc)) {
  941. dlen = desc->fip_dlen * FIP_BPW;
  942. if (dlen > rlen)
  943. return;
  944. /* Drop CVL if there are duplicate critical descriptors */
  945. if ((desc->fip_dtype < 32) &&
  946. !(desc_mask & 1U << desc->fip_dtype)) {
  947. LIBFCOE_FIP_DBG(fip, "Duplicate Critical "
  948. "Descriptors in FIP CVL\n");
  949. return;
  950. }
  951. switch (desc->fip_dtype) {
  952. case FIP_DT_MAC:
  953. mp = (struct fip_mac_desc *)desc;
  954. if (dlen < sizeof(*mp))
  955. return;
  956. if (compare_ether_addr(mp->fd_mac, fcf->fcf_mac))
  957. return;
  958. desc_mask &= ~BIT(FIP_DT_MAC);
  959. break;
  960. case FIP_DT_NAME:
  961. wp = (struct fip_wwn_desc *)desc;
  962. if (dlen < sizeof(*wp))
  963. return;
  964. if (get_unaligned_be64(&wp->fd_wwn) != fcf->switch_name)
  965. return;
  966. desc_mask &= ~BIT(FIP_DT_NAME);
  967. break;
  968. case FIP_DT_VN_ID:
  969. vp = (struct fip_vn_desc *)desc;
  970. if (dlen < sizeof(*vp))
  971. return;
  972. if (compare_ether_addr(vp->fd_mac,
  973. fip->get_src_addr(lport)) == 0 &&
  974. get_unaligned_be64(&vp->fd_wwpn) == lport->wwpn &&
  975. ntoh24(vp->fd_fc_id) == lport->port_id) {
  976. desc_mask &= ~BIT(FIP_DT_VN_ID);
  977. break;
  978. }
  979. /* check if clr_vlink is for NPIV port */
  980. mutex_lock(&lport->lp_mutex);
  981. list_for_each_entry(vn_port, &lport->vports, list) {
  982. if (compare_ether_addr(vp->fd_mac,
  983. fip->get_src_addr(vn_port)) == 0 &&
  984. (get_unaligned_be64(&vp->fd_wwpn)
  985. == vn_port->wwpn) &&
  986. (ntoh24(vp->fd_fc_id) ==
  987. fc_host_port_id(vn_port->host))) {
  988. desc_mask &= ~BIT(FIP_DT_VN_ID);
  989. is_vn_port = 1;
  990. break;
  991. }
  992. }
  993. mutex_unlock(&lport->lp_mutex);
  994. break;
  995. default:
  996. /* standard says ignore unknown descriptors >= 128 */
  997. if (desc->fip_dtype < FIP_DT_VENDOR_BASE)
  998. return;
  999. break;
  1000. }
  1001. desc = (struct fip_desc *)((char *)desc + dlen);
  1002. rlen -= dlen;
  1003. }
  1004. /*
  1005. * reset only if all required descriptors were present and valid.
  1006. */
  1007. if (desc_mask) {
  1008. LIBFCOE_FIP_DBG(fip, "missing descriptors mask %x\n",
  1009. desc_mask);
  1010. } else {
  1011. LIBFCOE_FIP_DBG(fip, "performing Clear Virtual Link\n");
  1012. if (is_vn_port)
  1013. fc_lport_reset(vn_port);
  1014. else {
  1015. spin_lock_bh(&fip->lock);
  1016. per_cpu_ptr(lport->dev_stats,
  1017. smp_processor_id())->VLinkFailureCount++;
  1018. fcoe_ctlr_reset(fip);
  1019. spin_unlock_bh(&fip->lock);
  1020. fc_lport_reset(fip->lp);
  1021. fcoe_ctlr_solicit(fip, NULL);
  1022. }
  1023. }
  1024. }
  1025. /**
  1026. * fcoe_ctlr_recv() - Receive a FIP packet
  1027. * @fip: The FCoE controller that received the packet
  1028. * @skb: The received FIP packet
  1029. *
  1030. * This may be called from either NET_RX_SOFTIRQ or IRQ.
  1031. */
  1032. void fcoe_ctlr_recv(struct fcoe_ctlr *fip, struct sk_buff *skb)
  1033. {
  1034. skb_queue_tail(&fip->fip_recv_list, skb);
  1035. schedule_work(&fip->recv_work);
  1036. }
  1037. EXPORT_SYMBOL(fcoe_ctlr_recv);
  1038. /**
  1039. * fcoe_ctlr_recv_handler() - Receive a FIP frame
  1040. * @fip: The FCoE controller that received the frame
  1041. * @skb: The received FIP frame
  1042. *
  1043. * Returns non-zero if the frame is dropped.
  1044. */
  1045. static int fcoe_ctlr_recv_handler(struct fcoe_ctlr *fip, struct sk_buff *skb)
  1046. {
  1047. struct fip_header *fiph;
  1048. struct ethhdr *eh;
  1049. enum fip_state state;
  1050. u16 op;
  1051. u8 sub;
  1052. if (skb_linearize(skb))
  1053. goto drop;
  1054. if (skb->len < sizeof(*fiph))
  1055. goto drop;
  1056. eh = eth_hdr(skb);
  1057. if (compare_ether_addr(eh->h_dest, fip->ctl_src_addr) &&
  1058. compare_ether_addr(eh->h_dest, FIP_ALL_ENODE_MACS))
  1059. goto drop;
  1060. fiph = (struct fip_header *)skb->data;
  1061. op = ntohs(fiph->fip_op);
  1062. sub = fiph->fip_subcode;
  1063. if (FIP_VER_DECAPS(fiph->fip_ver) != FIP_VER)
  1064. goto drop;
  1065. if (ntohs(fiph->fip_dl_len) * FIP_BPW + sizeof(*fiph) > skb->len)
  1066. goto drop;
  1067. spin_lock_bh(&fip->lock);
  1068. state = fip->state;
  1069. if (state == FIP_ST_AUTO) {
  1070. fip->map_dest = 0;
  1071. fip->state = FIP_ST_ENABLED;
  1072. state = FIP_ST_ENABLED;
  1073. LIBFCOE_FIP_DBG(fip, "Using FIP mode\n");
  1074. }
  1075. spin_unlock_bh(&fip->lock);
  1076. if (state != FIP_ST_ENABLED)
  1077. goto drop;
  1078. if (op == FIP_OP_LS) {
  1079. fcoe_ctlr_recv_els(fip, skb); /* consumes skb */
  1080. return 0;
  1081. }
  1082. if (op == FIP_OP_DISC && sub == FIP_SC_ADV)
  1083. fcoe_ctlr_recv_adv(fip, skb);
  1084. else if (op == FIP_OP_CTRL && sub == FIP_SC_CLR_VLINK)
  1085. fcoe_ctlr_recv_clr_vlink(fip, fiph);
  1086. kfree_skb(skb);
  1087. return 0;
  1088. drop:
  1089. kfree_skb(skb);
  1090. return -1;
  1091. }
  1092. /**
  1093. * fcoe_ctlr_select() - Select the best FCF (if possible)
  1094. * @fip: The FCoE controller
  1095. *
  1096. * If there are conflicting advertisements, no FCF can be chosen.
  1097. *
  1098. * Called with lock held.
  1099. */
  1100. static void fcoe_ctlr_select(struct fcoe_ctlr *fip)
  1101. {
  1102. struct fcoe_fcf *fcf;
  1103. struct fcoe_fcf *best = NULL;
  1104. list_for_each_entry(fcf, &fip->fcfs, list) {
  1105. LIBFCOE_FIP_DBG(fip, "consider FCF for fab %16.16llx "
  1106. "VFID %d map %x val %d\n",
  1107. fcf->fabric_name, fcf->vfid,
  1108. fcf->fc_map, fcoe_ctlr_mtu_valid(fcf));
  1109. if (!fcoe_ctlr_fcf_usable(fcf)) {
  1110. LIBFCOE_FIP_DBG(fip, "FCF for fab %16.16llx "
  1111. "map %x %svalid %savailable\n",
  1112. fcf->fabric_name, fcf->fc_map,
  1113. (fcf->flags & FIP_FL_SOL) ? "" : "in",
  1114. (fcf->flags & FIP_FL_AVAIL) ?
  1115. "" : "un");
  1116. continue;
  1117. }
  1118. if (!best) {
  1119. best = fcf;
  1120. continue;
  1121. }
  1122. if (fcf->fabric_name != best->fabric_name ||
  1123. fcf->vfid != best->vfid ||
  1124. fcf->fc_map != best->fc_map) {
  1125. LIBFCOE_FIP_DBG(fip, "Conflicting fabric, VFID, "
  1126. "or FC-MAP\n");
  1127. return;
  1128. }
  1129. if (fcf->pri < best->pri)
  1130. best = fcf;
  1131. }
  1132. fip->sel_fcf = best;
  1133. }
  1134. /**
  1135. * fcoe_ctlr_timeout() - FIP timeout handler
  1136. * @arg: The FCoE controller that timed out
  1137. *
  1138. * Ages FCFs. Triggers FCF selection if possible. Sends keep-alives.
  1139. */
  1140. static void fcoe_ctlr_timeout(unsigned long arg)
  1141. {
  1142. struct fcoe_ctlr *fip = (struct fcoe_ctlr *)arg;
  1143. struct fcoe_fcf *sel;
  1144. struct fcoe_fcf *fcf;
  1145. unsigned long next_timer;
  1146. spin_lock_bh(&fip->lock);
  1147. if (fip->state == FIP_ST_DISABLED) {
  1148. spin_unlock_bh(&fip->lock);
  1149. return;
  1150. }
  1151. fcf = fip->sel_fcf;
  1152. next_timer = fcoe_ctlr_age_fcfs(fip);
  1153. sel = fip->sel_fcf;
  1154. if (!sel && fip->sel_time) {
  1155. if (time_after_eq(jiffies, fip->sel_time)) {
  1156. fcoe_ctlr_select(fip);
  1157. sel = fip->sel_fcf;
  1158. fip->sel_time = 0;
  1159. } else if (time_after(next_timer, fip->sel_time))
  1160. next_timer = fip->sel_time;
  1161. }
  1162. if (sel != fcf) {
  1163. fcf = sel; /* the old FCF may have been freed */
  1164. if (sel) {
  1165. printk(KERN_INFO "libfcoe: host%d: FIP selected "
  1166. "Fibre-Channel Forwarder MAC %pM\n",
  1167. fip->lp->host->host_no, sel->fcf_mac);
  1168. memcpy(fip->dest_addr, sel->fcf_mac, ETH_ALEN);
  1169. fip->port_ka_time = jiffies +
  1170. msecs_to_jiffies(FIP_VN_KA_PERIOD);
  1171. fip->ctlr_ka_time = jiffies + sel->fka_period;
  1172. if (time_after(next_timer, fip->ctlr_ka_time))
  1173. next_timer = fip->ctlr_ka_time;
  1174. } else {
  1175. printk(KERN_NOTICE "libfcoe: host%d: "
  1176. "FIP Fibre-Channel Forwarder timed out. "
  1177. "Starting FCF discovery.\n",
  1178. fip->lp->host->host_no);
  1179. fip->reset_req = 1;
  1180. schedule_work(&fip->timer_work);
  1181. }
  1182. }
  1183. if (sel && !sel->fd_flags) {
  1184. if (time_after_eq(jiffies, fip->ctlr_ka_time)) {
  1185. fip->ctlr_ka_time = jiffies + sel->fka_period;
  1186. fip->send_ctlr_ka = 1;
  1187. }
  1188. if (time_after(next_timer, fip->ctlr_ka_time))
  1189. next_timer = fip->ctlr_ka_time;
  1190. if (time_after_eq(jiffies, fip->port_ka_time)) {
  1191. fip->port_ka_time = jiffies +
  1192. msecs_to_jiffies(FIP_VN_KA_PERIOD);
  1193. fip->send_port_ka = 1;
  1194. }
  1195. if (time_after(next_timer, fip->port_ka_time))
  1196. next_timer = fip->port_ka_time;
  1197. }
  1198. if (!list_empty(&fip->fcfs))
  1199. mod_timer(&fip->timer, next_timer);
  1200. if (fip->send_ctlr_ka || fip->send_port_ka)
  1201. schedule_work(&fip->timer_work);
  1202. spin_unlock_bh(&fip->lock);
  1203. }
  1204. /**
  1205. * fcoe_ctlr_timer_work() - Worker thread function for timer work
  1206. * @work: Handle to a FCoE controller
  1207. *
  1208. * Sends keep-alives and resets which must not
  1209. * be called from the timer directly, since they use a mutex.
  1210. */
  1211. static void fcoe_ctlr_timer_work(struct work_struct *work)
  1212. {
  1213. struct fcoe_ctlr *fip;
  1214. struct fc_lport *vport;
  1215. u8 *mac;
  1216. int reset;
  1217. fip = container_of(work, struct fcoe_ctlr, timer_work);
  1218. spin_lock_bh(&fip->lock);
  1219. reset = fip->reset_req;
  1220. fip->reset_req = 0;
  1221. spin_unlock_bh(&fip->lock);
  1222. if (reset) {
  1223. fc_lport_reset(fip->lp);
  1224. /* restart things with a solicitation */
  1225. fcoe_ctlr_solicit(fip, NULL);
  1226. }
  1227. if (fip->send_ctlr_ka) {
  1228. fip->send_ctlr_ka = 0;
  1229. fcoe_ctlr_send_keep_alive(fip, NULL, 0, fip->ctl_src_addr);
  1230. }
  1231. if (fip->send_port_ka) {
  1232. fip->send_port_ka = 0;
  1233. mutex_lock(&fip->lp->lp_mutex);
  1234. mac = fip->get_src_addr(fip->lp);
  1235. fcoe_ctlr_send_keep_alive(fip, fip->lp, 1, mac);
  1236. list_for_each_entry(vport, &fip->lp->vports, list) {
  1237. mac = fip->get_src_addr(vport);
  1238. fcoe_ctlr_send_keep_alive(fip, vport, 1, mac);
  1239. }
  1240. mutex_unlock(&fip->lp->lp_mutex);
  1241. }
  1242. }
  1243. /**
  1244. * fcoe_ctlr_recv_work() - Worker thread function for receiving FIP frames
  1245. * @recv_work: Handle to a FCoE controller
  1246. */
  1247. static void fcoe_ctlr_recv_work(struct work_struct *recv_work)
  1248. {
  1249. struct fcoe_ctlr *fip;
  1250. struct sk_buff *skb;
  1251. fip = container_of(recv_work, struct fcoe_ctlr, recv_work);
  1252. while ((skb = skb_dequeue(&fip->fip_recv_list)))
  1253. fcoe_ctlr_recv_handler(fip, skb);
  1254. }
  1255. /**
  1256. * fcoe_ctlr_recv_flogi() - Snoop pre-FIP receipt of FLOGI response
  1257. * @fip: The FCoE controller
  1258. * @fp: The FC frame to snoop
  1259. *
  1260. * Snoop potential response to FLOGI or even incoming FLOGI.
  1261. *
  1262. * The caller has checked that we are waiting for login as indicated
  1263. * by fip->flogi_oxid != FC_XID_UNKNOWN.
  1264. *
  1265. * The caller is responsible for freeing the frame.
  1266. * Fill in the granted_mac address.
  1267. *
  1268. * Return non-zero if the frame should not be delivered to libfc.
  1269. */
  1270. int fcoe_ctlr_recv_flogi(struct fcoe_ctlr *fip, struct fc_lport *lport,
  1271. struct fc_frame *fp)
  1272. {
  1273. struct fc_frame_header *fh;
  1274. u8 op;
  1275. u8 *sa;
  1276. sa = eth_hdr(&fp->skb)->h_source;
  1277. fh = fc_frame_header_get(fp);
  1278. if (fh->fh_type != FC_TYPE_ELS)
  1279. return 0;
  1280. op = fc_frame_payload_op(fp);
  1281. if (op == ELS_LS_ACC && fh->fh_r_ctl == FC_RCTL_ELS_REP &&
  1282. fip->flogi_oxid == ntohs(fh->fh_ox_id)) {
  1283. spin_lock_bh(&fip->lock);
  1284. if (fip->state != FIP_ST_AUTO && fip->state != FIP_ST_NON_FIP) {
  1285. spin_unlock_bh(&fip->lock);
  1286. return -EINVAL;
  1287. }
  1288. fip->state = FIP_ST_NON_FIP;
  1289. LIBFCOE_FIP_DBG(fip,
  1290. "received FLOGI LS_ACC using non-FIP mode\n");
  1291. /*
  1292. * FLOGI accepted.
  1293. * If the src mac addr is FC_OUI-based, then we mark the
  1294. * address_mode flag to use FC_OUI-based Ethernet DA.
  1295. * Otherwise we use the FCoE gateway addr
  1296. */
  1297. if (!compare_ether_addr(sa, (u8[6])FC_FCOE_FLOGI_MAC)) {
  1298. fip->map_dest = 1;
  1299. } else {
  1300. memcpy(fip->dest_addr, sa, ETH_ALEN);
  1301. fip->map_dest = 0;
  1302. }
  1303. fip->flogi_oxid = FC_XID_UNKNOWN;
  1304. spin_unlock_bh(&fip->lock);
  1305. fc_fcoe_set_mac(fr_cb(fp)->granted_mac, fh->fh_d_id);
  1306. } else if (op == ELS_FLOGI && fh->fh_r_ctl == FC_RCTL_ELS_REQ && sa) {
  1307. /*
  1308. * Save source MAC for point-to-point responses.
  1309. */
  1310. spin_lock_bh(&fip->lock);
  1311. if (fip->state == FIP_ST_AUTO || fip->state == FIP_ST_NON_FIP) {
  1312. memcpy(fip->dest_addr, sa, ETH_ALEN);
  1313. fip->map_dest = 0;
  1314. if (fip->state == FIP_ST_AUTO)
  1315. LIBFCOE_FIP_DBG(fip, "received non-FIP FLOGI. "
  1316. "Setting non-FIP mode\n");
  1317. fip->state = FIP_ST_NON_FIP;
  1318. }
  1319. spin_unlock_bh(&fip->lock);
  1320. }
  1321. return 0;
  1322. }
  1323. EXPORT_SYMBOL(fcoe_ctlr_recv_flogi);
  1324. /**
  1325. * fcoe_wwn_from_mac() - Converts a 48-bit IEEE MAC address to a 64-bit FC WWN
  1326. * @mac: The MAC address to convert
  1327. * @scheme: The scheme to use when converting
  1328. * @port: The port indicator for converting
  1329. *
  1330. * Returns: u64 fc world wide name
  1331. */
  1332. u64 fcoe_wwn_from_mac(unsigned char mac[MAX_ADDR_LEN],
  1333. unsigned int scheme, unsigned int port)
  1334. {
  1335. u64 wwn;
  1336. u64 host_mac;
  1337. /* The MAC is in NO, so flip only the low 48 bits */
  1338. host_mac = ((u64) mac[0] << 40) |
  1339. ((u64) mac[1] << 32) |
  1340. ((u64) mac[2] << 24) |
  1341. ((u64) mac[3] << 16) |
  1342. ((u64) mac[4] << 8) |
  1343. (u64) mac[5];
  1344. WARN_ON(host_mac >= (1ULL << 48));
  1345. wwn = host_mac | ((u64) scheme << 60);
  1346. switch (scheme) {
  1347. case 1:
  1348. WARN_ON(port != 0);
  1349. break;
  1350. case 2:
  1351. WARN_ON(port >= 0xfff);
  1352. wwn |= (u64) port << 48;
  1353. break;
  1354. default:
  1355. WARN_ON(1);
  1356. break;
  1357. }
  1358. return wwn;
  1359. }
  1360. EXPORT_SYMBOL_GPL(fcoe_wwn_from_mac);
  1361. /**
  1362. * fcoe_libfc_config() - Sets up libfc related properties for local port
  1363. * @lp: The local port to configure libfc for
  1364. * @tt: The libfc function template
  1365. *
  1366. * Returns : 0 for success
  1367. */
  1368. int fcoe_libfc_config(struct fc_lport *lport,
  1369. struct libfc_function_template *tt)
  1370. {
  1371. /* Set the function pointers set by the LLDD */
  1372. memcpy(&lport->tt, tt, sizeof(*tt));
  1373. if (fc_fcp_init(lport))
  1374. return -ENOMEM;
  1375. fc_exch_init(lport);
  1376. fc_elsct_init(lport);
  1377. fc_lport_init(lport);
  1378. fc_rport_init(lport);
  1379. fc_disc_init(lport);
  1380. return 0;
  1381. }
  1382. EXPORT_SYMBOL_GPL(fcoe_libfc_config);