libfcoe.c 36 KB

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