libfcoe.c 36 KB

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