libfcoe.c 36 KB

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