libfcoe.c 38 KB

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