libfcoe.c 35 KB

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