thp: transparent hugepage vmstat
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / scsi / fcoe / libfcoe.c
blob625c6be25396d79bbfffb192d4e1bdb7cbd7aad9
1 /*
2 * Copyright (c) 2008-2009 Cisco Systems, Inc. All rights reserved.
3 * Copyright (c) 2009 Intel Corporation. All rights reserved.
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.
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.
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.
18 * Maintained at www.Open-FCoE.org
21 #include <linux/types.h>
22 #include <linux/module.h>
23 #include <linux/kernel.h>
24 #include <linux/list.h>
25 #include <linux/spinlock.h>
26 #include <linux/timer.h>
27 #include <linux/netdevice.h>
28 #include <linux/etherdevice.h>
29 #include <linux/ethtool.h>
30 #include <linux/if_ether.h>
31 #include <linux/if_vlan.h>
32 #include <linux/errno.h>
33 #include <linux/bitops.h>
34 #include <linux/slab.h>
35 #include <net/rtnetlink.h>
37 #include <scsi/fc/fc_els.h>
38 #include <scsi/fc/fc_fs.h>
39 #include <scsi/fc/fc_fip.h>
40 #include <scsi/fc/fc_encaps.h>
41 #include <scsi/fc/fc_fcoe.h>
42 #include <scsi/fc/fc_fcp.h>
44 #include <scsi/libfc.h>
45 #include <scsi/libfcoe.h>
47 MODULE_AUTHOR("Open-FCoE.org");
48 MODULE_DESCRIPTION("FIP discovery protocol support for FCoE HBAs");
49 MODULE_LICENSE("GPL v2");
51 #define FCOE_CTLR_MIN_FKA 500 /* min keep alive (mS) */
52 #define FCOE_CTLR_DEF_FKA FIP_DEF_FKA /* default keep alive (mS) */
54 static void fcoe_ctlr_timeout(unsigned long);
55 static void fcoe_ctlr_timer_work(struct work_struct *);
56 static void fcoe_ctlr_recv_work(struct work_struct *);
57 static int fcoe_ctlr_flogi_retry(struct fcoe_ctlr *);
59 static void fcoe_ctlr_vn_start(struct fcoe_ctlr *);
60 static int fcoe_ctlr_vn_recv(struct fcoe_ctlr *, struct sk_buff *);
61 static void fcoe_ctlr_vn_timeout(struct fcoe_ctlr *);
62 static int fcoe_ctlr_vn_lookup(struct fcoe_ctlr *, u32, u8 *);
64 static u8 fcoe_all_fcfs[ETH_ALEN] = FIP_ALL_FCF_MACS;
65 static u8 fcoe_all_enode[ETH_ALEN] = FIP_ALL_ENODE_MACS;
66 static u8 fcoe_all_vn2vn[ETH_ALEN] = FIP_ALL_VN2VN_MACS;
67 static u8 fcoe_all_p2p[ETH_ALEN] = FIP_ALL_P2P_MACS;
69 unsigned int libfcoe_debug_logging;
70 module_param_named(debug_logging, libfcoe_debug_logging, int, S_IRUGO|S_IWUSR);
71 MODULE_PARM_DESC(debug_logging, "a bit mask of logging levels");
73 #define LIBFCOE_LOGGING 0x01 /* General logging, not categorized */
74 #define LIBFCOE_FIP_LOGGING 0x02 /* FIP logging */
76 #define LIBFCOE_CHECK_LOGGING(LEVEL, CMD) \
77 do { \
78 if (unlikely(libfcoe_debug_logging & LEVEL)) \
79 do { \
80 CMD; \
81 } while (0); \
82 } while (0)
84 #define LIBFCOE_DBG(fmt, args...) \
85 LIBFCOE_CHECK_LOGGING(LIBFCOE_LOGGING, \
86 printk(KERN_INFO "libfcoe: " fmt, ##args);)
88 #define LIBFCOE_FIP_DBG(fip, fmt, args...) \
89 LIBFCOE_CHECK_LOGGING(LIBFCOE_FIP_LOGGING, \
90 printk(KERN_INFO "host%d: fip: " fmt, \
91 (fip)->lp->host->host_no, ##args);)
93 static const char *fcoe_ctlr_states[] = {
94 [FIP_ST_DISABLED] = "DISABLED",
95 [FIP_ST_LINK_WAIT] = "LINK_WAIT",
96 [FIP_ST_AUTO] = "AUTO",
97 [FIP_ST_NON_FIP] = "NON_FIP",
98 [FIP_ST_ENABLED] = "ENABLED",
99 [FIP_ST_VNMP_START] = "VNMP_START",
100 [FIP_ST_VNMP_PROBE1] = "VNMP_PROBE1",
101 [FIP_ST_VNMP_PROBE2] = "VNMP_PROBE2",
102 [FIP_ST_VNMP_CLAIM] = "VNMP_CLAIM",
103 [FIP_ST_VNMP_UP] = "VNMP_UP",
106 static const char *fcoe_ctlr_state(enum fip_state state)
108 const char *cp = "unknown";
110 if (state < ARRAY_SIZE(fcoe_ctlr_states))
111 cp = fcoe_ctlr_states[state];
112 if (!cp)
113 cp = "unknown";
114 return cp;
118 * fcoe_ctlr_set_state() - Set and do debug printing for the new FIP state.
119 * @fip: The FCoE controller
120 * @state: The new state
122 static void fcoe_ctlr_set_state(struct fcoe_ctlr *fip, enum fip_state state)
124 if (state == fip->state)
125 return;
126 if (fip->lp)
127 LIBFCOE_FIP_DBG(fip, "state %s -> %s\n",
128 fcoe_ctlr_state(fip->state), fcoe_ctlr_state(state));
129 fip->state = state;
133 * fcoe_ctlr_mtu_valid() - Check if a FCF's MTU is valid
134 * @fcf: The FCF to check
136 * Return non-zero if FCF fcoe_size has been validated.
138 static inline int fcoe_ctlr_mtu_valid(const struct fcoe_fcf *fcf)
140 return (fcf->flags & FIP_FL_SOL) != 0;
144 * fcoe_ctlr_fcf_usable() - Check if a FCF is usable
145 * @fcf: The FCF to check
147 * Return non-zero if the FCF is usable.
149 static inline int fcoe_ctlr_fcf_usable(struct fcoe_fcf *fcf)
151 u16 flags = FIP_FL_SOL | FIP_FL_AVAIL;
153 return (fcf->flags & flags) == flags;
157 * fcoe_ctlr_map_dest() - Set flag and OUI for mapping destination addresses
158 * @fip: The FCoE controller
160 static void fcoe_ctlr_map_dest(struct fcoe_ctlr *fip)
162 if (fip->mode == FIP_MODE_VN2VN)
163 hton24(fip->dest_addr, FIP_VN_FC_MAP);
164 else
165 hton24(fip->dest_addr, FIP_DEF_FC_MAP);
166 hton24(fip->dest_addr + 3, 0);
167 fip->map_dest = 1;
171 * fcoe_ctlr_init() - Initialize the FCoE Controller instance
172 * @fip: The FCoE controller to initialize
174 void fcoe_ctlr_init(struct fcoe_ctlr *fip, enum fip_state mode)
176 fcoe_ctlr_set_state(fip, FIP_ST_LINK_WAIT);
177 fip->mode = mode;
178 INIT_LIST_HEAD(&fip->fcfs);
179 mutex_init(&fip->ctlr_mutex);
180 spin_lock_init(&fip->ctlr_lock);
181 fip->flogi_oxid = FC_XID_UNKNOWN;
182 setup_timer(&fip->timer, fcoe_ctlr_timeout, (unsigned long)fip);
183 INIT_WORK(&fip->timer_work, fcoe_ctlr_timer_work);
184 INIT_WORK(&fip->recv_work, fcoe_ctlr_recv_work);
185 skb_queue_head_init(&fip->fip_recv_list);
187 EXPORT_SYMBOL(fcoe_ctlr_init);
190 * fcoe_ctlr_reset_fcfs() - Reset and free all FCFs for a controller
191 * @fip: The FCoE controller whose FCFs are to be reset
193 * Called with &fcoe_ctlr lock held.
195 static void fcoe_ctlr_reset_fcfs(struct fcoe_ctlr *fip)
197 struct fcoe_fcf *fcf;
198 struct fcoe_fcf *next;
200 fip->sel_fcf = NULL;
201 list_for_each_entry_safe(fcf, next, &fip->fcfs, list) {
202 list_del(&fcf->list);
203 kfree(fcf);
205 fip->fcf_count = 0;
206 fip->sel_time = 0;
210 * fcoe_ctlr_destroy() - Disable and tear down a FCoE controller
211 * @fip: The FCoE controller to tear down
213 * This is called by FCoE drivers before freeing the &fcoe_ctlr.
215 * The receive handler will have been deleted before this to guarantee
216 * that no more recv_work will be scheduled.
218 * The timer routine will simply return once we set FIP_ST_DISABLED.
219 * This guarantees that no further timeouts or work will be scheduled.
221 void fcoe_ctlr_destroy(struct fcoe_ctlr *fip)
223 cancel_work_sync(&fip->recv_work);
224 skb_queue_purge(&fip->fip_recv_list);
226 mutex_lock(&fip->ctlr_mutex);
227 fcoe_ctlr_set_state(fip, FIP_ST_DISABLED);
228 fcoe_ctlr_reset_fcfs(fip);
229 mutex_unlock(&fip->ctlr_mutex);
230 del_timer_sync(&fip->timer);
231 cancel_work_sync(&fip->timer_work);
233 EXPORT_SYMBOL(fcoe_ctlr_destroy);
236 * fcoe_ctlr_announce() - announce new FCF selection
237 * @fip: The FCoE controller
239 * Also sets the destination MAC for FCoE and control packets
241 * Called with neither ctlr_mutex nor ctlr_lock held.
243 static void fcoe_ctlr_announce(struct fcoe_ctlr *fip)
245 struct fcoe_fcf *sel;
246 struct fcoe_fcf *fcf;
248 mutex_lock(&fip->ctlr_mutex);
249 spin_lock_bh(&fip->ctlr_lock);
251 kfree_skb(fip->flogi_req);
252 fip->flogi_req = NULL;
253 list_for_each_entry(fcf, &fip->fcfs, list)
254 fcf->flogi_sent = 0;
256 spin_unlock_bh(&fip->ctlr_lock);
257 sel = fip->sel_fcf;
259 if (sel && !compare_ether_addr(sel->fcf_mac, fip->dest_addr))
260 goto unlock;
261 if (!is_zero_ether_addr(fip->dest_addr)) {
262 printk(KERN_NOTICE "libfcoe: host%d: "
263 "FIP Fibre-Channel Forwarder MAC %pM deselected\n",
264 fip->lp->host->host_no, fip->dest_addr);
265 memset(fip->dest_addr, 0, ETH_ALEN);
267 if (sel) {
268 printk(KERN_INFO "libfcoe: host%d: FIP selected "
269 "Fibre-Channel Forwarder MAC %pM\n",
270 fip->lp->host->host_no, sel->fcf_mac);
271 memcpy(fip->dest_addr, sel->fcf_mac, ETH_ALEN);
272 fip->map_dest = 0;
274 unlock:
275 mutex_unlock(&fip->ctlr_mutex);
279 * fcoe_ctlr_fcoe_size() - Return the maximum FCoE size required for VN_Port
280 * @fip: The FCoE controller to get the maximum FCoE size from
282 * Returns the maximum packet size including the FCoE header and trailer,
283 * but not including any Ethernet or VLAN headers.
285 static inline u32 fcoe_ctlr_fcoe_size(struct fcoe_ctlr *fip)
288 * Determine the max FCoE frame size allowed, including
289 * FCoE header and trailer.
290 * Note: lp->mfs is currently the payload size, not the frame size.
292 return fip->lp->mfs + sizeof(struct fc_frame_header) +
293 sizeof(struct fcoe_hdr) + sizeof(struct fcoe_crc_eof);
297 * fcoe_ctlr_solicit() - Send a FIP solicitation
298 * @fip: The FCoE controller to send the solicitation on
299 * @fcf: The destination FCF (if NULL, a multicast solicitation is sent)
301 static void fcoe_ctlr_solicit(struct fcoe_ctlr *fip, struct fcoe_fcf *fcf)
303 struct sk_buff *skb;
304 struct fip_sol {
305 struct ethhdr eth;
306 struct fip_header fip;
307 struct {
308 struct fip_mac_desc mac;
309 struct fip_wwn_desc wwnn;
310 struct fip_size_desc size;
311 } __attribute__((packed)) desc;
312 } __attribute__((packed)) *sol;
313 u32 fcoe_size;
315 skb = dev_alloc_skb(sizeof(*sol));
316 if (!skb)
317 return;
319 sol = (struct fip_sol *)skb->data;
321 memset(sol, 0, sizeof(*sol));
322 memcpy(sol->eth.h_dest, fcf ? fcf->fcf_mac : fcoe_all_fcfs, ETH_ALEN);
323 memcpy(sol->eth.h_source, fip->ctl_src_addr, ETH_ALEN);
324 sol->eth.h_proto = htons(ETH_P_FIP);
326 sol->fip.fip_ver = FIP_VER_ENCAPS(FIP_VER);
327 sol->fip.fip_op = htons(FIP_OP_DISC);
328 sol->fip.fip_subcode = FIP_SC_SOL;
329 sol->fip.fip_dl_len = htons(sizeof(sol->desc) / FIP_BPW);
330 sol->fip.fip_flags = htons(FIP_FL_FPMA);
331 if (fip->spma)
332 sol->fip.fip_flags |= htons(FIP_FL_SPMA);
334 sol->desc.mac.fd_desc.fip_dtype = FIP_DT_MAC;
335 sol->desc.mac.fd_desc.fip_dlen = sizeof(sol->desc.mac) / FIP_BPW;
336 memcpy(sol->desc.mac.fd_mac, fip->ctl_src_addr, ETH_ALEN);
338 sol->desc.wwnn.fd_desc.fip_dtype = FIP_DT_NAME;
339 sol->desc.wwnn.fd_desc.fip_dlen = sizeof(sol->desc.wwnn) / FIP_BPW;
340 put_unaligned_be64(fip->lp->wwnn, &sol->desc.wwnn.fd_wwn);
342 fcoe_size = fcoe_ctlr_fcoe_size(fip);
343 sol->desc.size.fd_desc.fip_dtype = FIP_DT_FCOE_SIZE;
344 sol->desc.size.fd_desc.fip_dlen = sizeof(sol->desc.size) / FIP_BPW;
345 sol->desc.size.fd_size = htons(fcoe_size);
347 skb_put(skb, sizeof(*sol));
348 skb->protocol = htons(ETH_P_FIP);
349 skb_reset_mac_header(skb);
350 skb_reset_network_header(skb);
351 fip->send(fip, skb);
353 if (!fcf)
354 fip->sol_time = jiffies;
358 * fcoe_ctlr_link_up() - Start FCoE controller
359 * @fip: The FCoE controller to start
361 * Called from the LLD when the network link is ready.
363 void fcoe_ctlr_link_up(struct fcoe_ctlr *fip)
365 mutex_lock(&fip->ctlr_mutex);
366 if (fip->state == FIP_ST_NON_FIP || fip->state == FIP_ST_AUTO) {
367 mutex_unlock(&fip->ctlr_mutex);
368 fc_linkup(fip->lp);
369 } else if (fip->state == FIP_ST_LINK_WAIT) {
370 fcoe_ctlr_set_state(fip, fip->mode);
371 switch (fip->mode) {
372 default:
373 LIBFCOE_FIP_DBG(fip, "invalid mode %d\n", fip->mode);
374 /* fall-through */
375 case FIP_MODE_AUTO:
376 LIBFCOE_FIP_DBG(fip, "%s", "setting AUTO mode.\n");
377 /* fall-through */
378 case FIP_MODE_FABRIC:
379 case FIP_MODE_NON_FIP:
380 mutex_unlock(&fip->ctlr_mutex);
381 fc_linkup(fip->lp);
382 fcoe_ctlr_solicit(fip, NULL);
383 break;
384 case FIP_MODE_VN2VN:
385 fcoe_ctlr_vn_start(fip);
386 mutex_unlock(&fip->ctlr_mutex);
387 fc_linkup(fip->lp);
388 break;
390 } else
391 mutex_unlock(&fip->ctlr_mutex);
393 EXPORT_SYMBOL(fcoe_ctlr_link_up);
396 * fcoe_ctlr_reset() - Reset a FCoE controller
397 * @fip: The FCoE controller to reset
399 static void fcoe_ctlr_reset(struct fcoe_ctlr *fip)
401 fcoe_ctlr_reset_fcfs(fip);
402 del_timer(&fip->timer);
403 fip->ctlr_ka_time = 0;
404 fip->port_ka_time = 0;
405 fip->sol_time = 0;
406 fip->flogi_oxid = FC_XID_UNKNOWN;
407 fcoe_ctlr_map_dest(fip);
411 * fcoe_ctlr_link_down() - Stop a FCoE controller
412 * @fip: The FCoE controller to be stopped
414 * Returns non-zero if the link was up and now isn't.
416 * Called from the LLD when the network link is not ready.
417 * There may be multiple calls while the link is down.
419 int fcoe_ctlr_link_down(struct fcoe_ctlr *fip)
421 int link_dropped;
423 LIBFCOE_FIP_DBG(fip, "link down.\n");
424 mutex_lock(&fip->ctlr_mutex);
425 fcoe_ctlr_reset(fip);
426 link_dropped = fip->state != FIP_ST_LINK_WAIT;
427 fcoe_ctlr_set_state(fip, FIP_ST_LINK_WAIT);
428 mutex_unlock(&fip->ctlr_mutex);
430 if (link_dropped)
431 fc_linkdown(fip->lp);
432 return link_dropped;
434 EXPORT_SYMBOL(fcoe_ctlr_link_down);
437 * fcoe_ctlr_send_keep_alive() - Send a keep-alive to the selected FCF
438 * @fip: The FCoE controller to send the FKA on
439 * @lport: libfc fc_lport to send from
440 * @ports: 0 for controller keep-alive, 1 for port keep-alive
441 * @sa: The source MAC address
443 * A controller keep-alive is sent every fka_period (typically 8 seconds).
444 * The source MAC is the native MAC address.
446 * A port keep-alive is sent every 90 seconds while logged in.
447 * The source MAC is the assigned mapped source address.
448 * The destination is the FCF's F-port.
450 static void fcoe_ctlr_send_keep_alive(struct fcoe_ctlr *fip,
451 struct fc_lport *lport,
452 int ports, u8 *sa)
454 struct sk_buff *skb;
455 struct fip_kal {
456 struct ethhdr eth;
457 struct fip_header fip;
458 struct fip_mac_desc mac;
459 } __attribute__((packed)) *kal;
460 struct fip_vn_desc *vn;
461 u32 len;
462 struct fc_lport *lp;
463 struct fcoe_fcf *fcf;
465 fcf = fip->sel_fcf;
466 lp = fip->lp;
467 if (!fcf || (ports && !lp->port_id))
468 return;
470 len = sizeof(*kal) + ports * sizeof(*vn);
471 skb = dev_alloc_skb(len);
472 if (!skb)
473 return;
475 kal = (struct fip_kal *)skb->data;
476 memset(kal, 0, len);
477 memcpy(kal->eth.h_dest, fcf->fcf_mac, ETH_ALEN);
478 memcpy(kal->eth.h_source, sa, ETH_ALEN);
479 kal->eth.h_proto = htons(ETH_P_FIP);
481 kal->fip.fip_ver = FIP_VER_ENCAPS(FIP_VER);
482 kal->fip.fip_op = htons(FIP_OP_CTRL);
483 kal->fip.fip_subcode = FIP_SC_KEEP_ALIVE;
484 kal->fip.fip_dl_len = htons((sizeof(kal->mac) +
485 ports * sizeof(*vn)) / FIP_BPW);
486 kal->fip.fip_flags = htons(FIP_FL_FPMA);
487 if (fip->spma)
488 kal->fip.fip_flags |= htons(FIP_FL_SPMA);
490 kal->mac.fd_desc.fip_dtype = FIP_DT_MAC;
491 kal->mac.fd_desc.fip_dlen = sizeof(kal->mac) / FIP_BPW;
492 memcpy(kal->mac.fd_mac, fip->ctl_src_addr, ETH_ALEN);
493 if (ports) {
494 vn = (struct fip_vn_desc *)(kal + 1);
495 vn->fd_desc.fip_dtype = FIP_DT_VN_ID;
496 vn->fd_desc.fip_dlen = sizeof(*vn) / FIP_BPW;
497 memcpy(vn->fd_mac, fip->get_src_addr(lport), ETH_ALEN);
498 hton24(vn->fd_fc_id, lport->port_id);
499 put_unaligned_be64(lport->wwpn, &vn->fd_wwpn);
501 skb_put(skb, len);
502 skb->protocol = htons(ETH_P_FIP);
503 skb_reset_mac_header(skb);
504 skb_reset_network_header(skb);
505 fip->send(fip, skb);
509 * fcoe_ctlr_encaps() - Encapsulate an ELS frame for FIP, without sending it
510 * @fip: The FCoE controller for the ELS frame
511 * @dtype: The FIP descriptor type for the frame
512 * @skb: The FCoE ELS frame including FC header but no FCoE headers
513 * @d_id: The destination port ID.
515 * Returns non-zero error code on failure.
517 * The caller must check that the length is a multiple of 4.
519 * The @skb must have enough headroom (28 bytes) and tailroom (8 bytes).
520 * Headroom includes the FIP encapsulation description, FIP header, and
521 * Ethernet header. The tailroom is for the FIP MAC descriptor.
523 static int fcoe_ctlr_encaps(struct fcoe_ctlr *fip, struct fc_lport *lport,
524 u8 dtype, struct sk_buff *skb, u32 d_id)
526 struct fip_encaps_head {
527 struct ethhdr eth;
528 struct fip_header fip;
529 struct fip_encaps encaps;
530 } __attribute__((packed)) *cap;
531 struct fc_frame_header *fh;
532 struct fip_mac_desc *mac;
533 struct fcoe_fcf *fcf;
534 size_t dlen;
535 u16 fip_flags;
536 u8 op;
538 fh = (struct fc_frame_header *)skb->data;
539 op = *(u8 *)(fh + 1);
540 dlen = sizeof(struct fip_encaps) + skb->len; /* len before push */
541 cap = (struct fip_encaps_head *)skb_push(skb, sizeof(*cap));
542 memset(cap, 0, sizeof(*cap));
544 if (lport->point_to_multipoint) {
545 if (fcoe_ctlr_vn_lookup(fip, d_id, cap->eth.h_dest))
546 return -ENODEV;
547 fip_flags = 0;
548 } else {
549 fcf = fip->sel_fcf;
550 if (!fcf)
551 return -ENODEV;
552 fip_flags = fcf->flags;
553 fip_flags &= fip->spma ? FIP_FL_SPMA | FIP_FL_FPMA :
554 FIP_FL_FPMA;
555 if (!fip_flags)
556 return -ENODEV;
557 memcpy(cap->eth.h_dest, fcf->fcf_mac, ETH_ALEN);
559 memcpy(cap->eth.h_source, fip->ctl_src_addr, ETH_ALEN);
560 cap->eth.h_proto = htons(ETH_P_FIP);
562 cap->fip.fip_ver = FIP_VER_ENCAPS(FIP_VER);
563 cap->fip.fip_op = htons(FIP_OP_LS);
564 if (op == ELS_LS_ACC || op == ELS_LS_RJT)
565 cap->fip.fip_subcode = FIP_SC_REP;
566 else
567 cap->fip.fip_subcode = FIP_SC_REQ;
568 cap->fip.fip_flags = htons(fip_flags);
570 cap->encaps.fd_desc.fip_dtype = dtype;
571 cap->encaps.fd_desc.fip_dlen = dlen / FIP_BPW;
573 if (op != ELS_LS_RJT) {
574 dlen += sizeof(*mac);
575 mac = (struct fip_mac_desc *)skb_put(skb, sizeof(*mac));
576 memset(mac, 0, sizeof(*mac));
577 mac->fd_desc.fip_dtype = FIP_DT_MAC;
578 mac->fd_desc.fip_dlen = sizeof(*mac) / FIP_BPW;
579 if (dtype != FIP_DT_FLOGI && dtype != FIP_DT_FDISC) {
580 memcpy(mac->fd_mac, fip->get_src_addr(lport), ETH_ALEN);
581 } else if (fip->mode == FIP_MODE_VN2VN) {
582 hton24(mac->fd_mac, FIP_VN_FC_MAP);
583 hton24(mac->fd_mac + 3, fip->port_id);
584 } else if (fip_flags & FIP_FL_SPMA) {
585 LIBFCOE_FIP_DBG(fip, "FLOGI/FDISC sent with SPMA\n");
586 memcpy(mac->fd_mac, fip->ctl_src_addr, ETH_ALEN);
587 } else {
588 LIBFCOE_FIP_DBG(fip, "FLOGI/FDISC sent with FPMA\n");
589 /* FPMA only FLOGI. Must leave the MAC desc zeroed. */
592 cap->fip.fip_dl_len = htons(dlen / FIP_BPW);
594 skb->protocol = htons(ETH_P_FIP);
595 skb_reset_mac_header(skb);
596 skb_reset_network_header(skb);
597 return 0;
601 * fcoe_ctlr_els_send() - Send an ELS frame encapsulated by FIP if appropriate.
602 * @fip: FCoE controller.
603 * @lport: libfc fc_lport to send from
604 * @skb: FCoE ELS frame including FC header but no FCoE headers.
606 * Returns a non-zero error code if the frame should not be sent.
607 * Returns zero if the caller should send the frame with FCoE encapsulation.
609 * The caller must check that the length is a multiple of 4.
610 * The SKB must have enough headroom (28 bytes) and tailroom (8 bytes).
611 * The the skb must also be an fc_frame.
613 * This is called from the lower-level driver with spinlocks held,
614 * so we must not take a mutex here.
616 int fcoe_ctlr_els_send(struct fcoe_ctlr *fip, struct fc_lport *lport,
617 struct sk_buff *skb)
619 struct fc_frame *fp;
620 struct fc_frame_header *fh;
621 u16 old_xid;
622 u8 op;
623 u8 mac[ETH_ALEN];
625 fp = container_of(skb, struct fc_frame, skb);
626 fh = (struct fc_frame_header *)skb->data;
627 op = *(u8 *)(fh + 1);
629 if (op == ELS_FLOGI && fip->mode != FIP_MODE_VN2VN) {
630 old_xid = fip->flogi_oxid;
631 fip->flogi_oxid = ntohs(fh->fh_ox_id);
632 if (fip->state == FIP_ST_AUTO) {
633 if (old_xid == FC_XID_UNKNOWN)
634 fip->flogi_count = 0;
635 fip->flogi_count++;
636 if (fip->flogi_count < 3)
637 goto drop;
638 fcoe_ctlr_map_dest(fip);
639 return 0;
641 if (fip->state == FIP_ST_NON_FIP)
642 fcoe_ctlr_map_dest(fip);
645 if (fip->state == FIP_ST_NON_FIP)
646 return 0;
647 if (!fip->sel_fcf && fip->mode != FIP_MODE_VN2VN)
648 goto drop;
649 switch (op) {
650 case ELS_FLOGI:
651 op = FIP_DT_FLOGI;
652 if (fip->mode == FIP_MODE_VN2VN)
653 break;
654 spin_lock_bh(&fip->ctlr_lock);
655 kfree_skb(fip->flogi_req);
656 fip->flogi_req = skb;
657 fip->flogi_req_send = 1;
658 spin_unlock_bh(&fip->ctlr_lock);
659 schedule_work(&fip->timer_work);
660 return -EINPROGRESS;
661 case ELS_FDISC:
662 if (ntoh24(fh->fh_s_id))
663 return 0;
664 op = FIP_DT_FDISC;
665 break;
666 case ELS_LOGO:
667 if (fip->mode == FIP_MODE_VN2VN) {
668 if (fip->state != FIP_ST_VNMP_UP)
669 return -EINVAL;
670 if (ntoh24(fh->fh_d_id) == FC_FID_FLOGI)
671 return -EINVAL;
672 } else {
673 if (fip->state != FIP_ST_ENABLED)
674 return 0;
675 if (ntoh24(fh->fh_d_id) != FC_FID_FLOGI)
676 return 0;
678 op = FIP_DT_LOGO;
679 break;
680 case ELS_LS_ACC:
682 * If non-FIP, we may have gotten an SID by accepting an FLOGI
683 * from a point-to-point connection. Switch to using
684 * the source mac based on the SID. The destination
685 * MAC in this case would have been set by receving the
686 * FLOGI.
688 if (fip->state == FIP_ST_NON_FIP) {
689 if (fip->flogi_oxid == FC_XID_UNKNOWN)
690 return 0;
691 fip->flogi_oxid = FC_XID_UNKNOWN;
692 fc_fcoe_set_mac(mac, fh->fh_d_id);
693 fip->update_mac(lport, mac);
695 /* fall through */
696 case ELS_LS_RJT:
697 op = fr_encaps(fp);
698 if (op)
699 break;
700 return 0;
701 default:
702 if (fip->state != FIP_ST_ENABLED &&
703 fip->state != FIP_ST_VNMP_UP)
704 goto drop;
705 return 0;
707 LIBFCOE_FIP_DBG(fip, "els_send op %u d_id %x\n",
708 op, ntoh24(fh->fh_d_id));
709 if (fcoe_ctlr_encaps(fip, lport, op, skb, ntoh24(fh->fh_d_id)))
710 goto drop;
711 fip->send(fip, skb);
712 return -EINPROGRESS;
713 drop:
714 kfree_skb(skb);
715 return -EINVAL;
717 EXPORT_SYMBOL(fcoe_ctlr_els_send);
720 * fcoe_ctlr_age_fcfs() - Reset and free all old FCFs for a controller
721 * @fip: The FCoE controller to free FCFs on
723 * Called with lock held and preemption disabled.
725 * An FCF is considered old if we have missed two advertisements.
726 * That is, there have been no valid advertisement from it for 2.5
727 * times its keep-alive period.
729 * In addition, determine the time when an FCF selection can occur.
731 * Also, increment the MissDiscAdvCount when no advertisement is received
732 * for the corresponding FCF for 1.5 * FKA_ADV_PERIOD (FC-BB-5 LESB).
734 * Returns the time in jiffies for the next call.
736 static unsigned long fcoe_ctlr_age_fcfs(struct fcoe_ctlr *fip)
738 struct fcoe_fcf *fcf;
739 struct fcoe_fcf *next;
740 unsigned long next_timer = jiffies + msecs_to_jiffies(FIP_VN_KA_PERIOD);
741 unsigned long deadline;
742 unsigned long sel_time = 0;
743 struct fcoe_dev_stats *stats;
745 stats = per_cpu_ptr(fip->lp->dev_stats, get_cpu());
747 list_for_each_entry_safe(fcf, next, &fip->fcfs, list) {
748 deadline = fcf->time + fcf->fka_period + fcf->fka_period / 2;
749 if (fip->sel_fcf == fcf) {
750 if (time_after(jiffies, deadline)) {
751 stats->MissDiscAdvCount++;
752 printk(KERN_INFO "libfcoe: host%d: "
753 "Missing Discovery Advertisement "
754 "for fab %16.16llx count %lld\n",
755 fip->lp->host->host_no, fcf->fabric_name,
756 stats->MissDiscAdvCount);
757 } else if (time_after(next_timer, deadline))
758 next_timer = deadline;
761 deadline += fcf->fka_period;
762 if (time_after_eq(jiffies, deadline)) {
763 if (fip->sel_fcf == fcf)
764 fip->sel_fcf = NULL;
765 list_del(&fcf->list);
766 WARN_ON(!fip->fcf_count);
767 fip->fcf_count--;
768 kfree(fcf);
769 stats->VLinkFailureCount++;
770 } else {
771 if (time_after(next_timer, deadline))
772 next_timer = deadline;
773 if (fcoe_ctlr_mtu_valid(fcf) &&
774 (!sel_time || time_before(sel_time, fcf->time)))
775 sel_time = fcf->time;
778 put_cpu();
779 if (sel_time && !fip->sel_fcf && !fip->sel_time) {
780 sel_time += msecs_to_jiffies(FCOE_CTLR_START_DELAY);
781 fip->sel_time = sel_time;
784 return next_timer;
788 * fcoe_ctlr_parse_adv() - Decode a FIP advertisement into a new FCF entry
789 * @fip: The FCoE controller receiving the advertisement
790 * @skb: The received FIP advertisement frame
791 * @fcf: The resulting FCF entry
793 * Returns zero on a valid parsed advertisement,
794 * otherwise returns non zero value.
796 static int fcoe_ctlr_parse_adv(struct fcoe_ctlr *fip,
797 struct sk_buff *skb, struct fcoe_fcf *fcf)
799 struct fip_header *fiph;
800 struct fip_desc *desc = NULL;
801 struct fip_wwn_desc *wwn;
802 struct fip_fab_desc *fab;
803 struct fip_fka_desc *fka;
804 unsigned long t;
805 size_t rlen;
806 size_t dlen;
807 u32 desc_mask;
809 memset(fcf, 0, sizeof(*fcf));
810 fcf->fka_period = msecs_to_jiffies(FCOE_CTLR_DEF_FKA);
812 fiph = (struct fip_header *)skb->data;
813 fcf->flags = ntohs(fiph->fip_flags);
816 * mask of required descriptors. validating each one clears its bit.
818 desc_mask = BIT(FIP_DT_PRI) | BIT(FIP_DT_MAC) | BIT(FIP_DT_NAME) |
819 BIT(FIP_DT_FAB) | BIT(FIP_DT_FKA);
821 rlen = ntohs(fiph->fip_dl_len) * 4;
822 if (rlen + sizeof(*fiph) > skb->len)
823 return -EINVAL;
825 desc = (struct fip_desc *)(fiph + 1);
826 while (rlen > 0) {
827 dlen = desc->fip_dlen * FIP_BPW;
828 if (dlen < sizeof(*desc) || dlen > rlen)
829 return -EINVAL;
830 /* Drop Adv if there are duplicate critical descriptors */
831 if ((desc->fip_dtype < 32) &&
832 !(desc_mask & 1U << desc->fip_dtype)) {
833 LIBFCOE_FIP_DBG(fip, "Duplicate Critical "
834 "Descriptors in FIP adv\n");
835 return -EINVAL;
837 switch (desc->fip_dtype) {
838 case FIP_DT_PRI:
839 if (dlen != sizeof(struct fip_pri_desc))
840 goto len_err;
841 fcf->pri = ((struct fip_pri_desc *)desc)->fd_pri;
842 desc_mask &= ~BIT(FIP_DT_PRI);
843 break;
844 case FIP_DT_MAC:
845 if (dlen != sizeof(struct fip_mac_desc))
846 goto len_err;
847 memcpy(fcf->fcf_mac,
848 ((struct fip_mac_desc *)desc)->fd_mac,
849 ETH_ALEN);
850 if (!is_valid_ether_addr(fcf->fcf_mac)) {
851 LIBFCOE_FIP_DBG(fip,
852 "Invalid MAC addr %pM in FIP adv\n",
853 fcf->fcf_mac);
854 return -EINVAL;
856 desc_mask &= ~BIT(FIP_DT_MAC);
857 break;
858 case FIP_DT_NAME:
859 if (dlen != sizeof(struct fip_wwn_desc))
860 goto len_err;
861 wwn = (struct fip_wwn_desc *)desc;
862 fcf->switch_name = get_unaligned_be64(&wwn->fd_wwn);
863 desc_mask &= ~BIT(FIP_DT_NAME);
864 break;
865 case FIP_DT_FAB:
866 if (dlen != sizeof(struct fip_fab_desc))
867 goto len_err;
868 fab = (struct fip_fab_desc *)desc;
869 fcf->fabric_name = get_unaligned_be64(&fab->fd_wwn);
870 fcf->vfid = ntohs(fab->fd_vfid);
871 fcf->fc_map = ntoh24(fab->fd_map);
872 desc_mask &= ~BIT(FIP_DT_FAB);
873 break;
874 case FIP_DT_FKA:
875 if (dlen != sizeof(struct fip_fka_desc))
876 goto len_err;
877 fka = (struct fip_fka_desc *)desc;
878 if (fka->fd_flags & FIP_FKA_ADV_D)
879 fcf->fd_flags = 1;
880 t = ntohl(fka->fd_fka_period);
881 if (t >= FCOE_CTLR_MIN_FKA)
882 fcf->fka_period = msecs_to_jiffies(t);
883 desc_mask &= ~BIT(FIP_DT_FKA);
884 break;
885 case FIP_DT_MAP_OUI:
886 case FIP_DT_FCOE_SIZE:
887 case FIP_DT_FLOGI:
888 case FIP_DT_FDISC:
889 case FIP_DT_LOGO:
890 case FIP_DT_ELP:
891 default:
892 LIBFCOE_FIP_DBG(fip, "unexpected descriptor type %x "
893 "in FIP adv\n", desc->fip_dtype);
894 /* standard says ignore unknown descriptors >= 128 */
895 if (desc->fip_dtype < FIP_DT_VENDOR_BASE)
896 return -EINVAL;
897 break;
899 desc = (struct fip_desc *)((char *)desc + dlen);
900 rlen -= dlen;
902 if (!fcf->fc_map || (fcf->fc_map & 0x10000))
903 return -EINVAL;
904 if (!fcf->switch_name)
905 return -EINVAL;
906 if (desc_mask) {
907 LIBFCOE_FIP_DBG(fip, "adv missing descriptors mask %x\n",
908 desc_mask);
909 return -EINVAL;
911 return 0;
913 len_err:
914 LIBFCOE_FIP_DBG(fip, "FIP length error in descriptor type %x len %zu\n",
915 desc->fip_dtype, dlen);
916 return -EINVAL;
920 * fcoe_ctlr_recv_adv() - Handle an incoming advertisement
921 * @fip: The FCoE controller receiving the advertisement
922 * @skb: The received FIP packet
924 static void fcoe_ctlr_recv_adv(struct fcoe_ctlr *fip, struct sk_buff *skb)
926 struct fcoe_fcf *fcf;
927 struct fcoe_fcf new;
928 struct fcoe_fcf *found;
929 unsigned long sol_tov = msecs_to_jiffies(FCOE_CTRL_SOL_TOV);
930 int first = 0;
931 int mtu_valid;
933 if (fcoe_ctlr_parse_adv(fip, skb, &new))
934 return;
936 mutex_lock(&fip->ctlr_mutex);
937 first = list_empty(&fip->fcfs);
938 found = NULL;
939 list_for_each_entry(fcf, &fip->fcfs, list) {
940 if (fcf->switch_name == new.switch_name &&
941 fcf->fabric_name == new.fabric_name &&
942 fcf->fc_map == new.fc_map &&
943 compare_ether_addr(fcf->fcf_mac, new.fcf_mac) == 0) {
944 found = fcf;
945 break;
948 if (!found) {
949 if (fip->fcf_count >= FCOE_CTLR_FCF_LIMIT)
950 goto out;
952 fcf = kmalloc(sizeof(*fcf), GFP_ATOMIC);
953 if (!fcf)
954 goto out;
956 fip->fcf_count++;
957 memcpy(fcf, &new, sizeof(new));
958 list_add(&fcf->list, &fip->fcfs);
959 } else {
961 * Update the FCF's keep-alive descriptor flags.
962 * Other flag changes from new advertisements are
963 * ignored after a solicited advertisement is
964 * received and the FCF is selectable (usable).
966 fcf->fd_flags = new.fd_flags;
967 if (!fcoe_ctlr_fcf_usable(fcf))
968 fcf->flags = new.flags;
970 if (fcf == fip->sel_fcf && !fcf->fd_flags) {
971 fip->ctlr_ka_time -= fcf->fka_period;
972 fip->ctlr_ka_time += new.fka_period;
973 if (time_before(fip->ctlr_ka_time, fip->timer.expires))
974 mod_timer(&fip->timer, fip->ctlr_ka_time);
976 fcf->fka_period = new.fka_period;
977 memcpy(fcf->fcf_mac, new.fcf_mac, ETH_ALEN);
979 mtu_valid = fcoe_ctlr_mtu_valid(fcf);
980 fcf->time = jiffies;
981 if (!found)
982 LIBFCOE_FIP_DBG(fip, "New FCF fab %16.16llx mac %pM\n",
983 fcf->fabric_name, fcf->fcf_mac);
986 * If this advertisement is not solicited and our max receive size
987 * hasn't been verified, send a solicited advertisement.
989 if (!mtu_valid)
990 fcoe_ctlr_solicit(fip, fcf);
993 * If its been a while since we did a solicit, and this is
994 * the first advertisement we've received, do a multicast
995 * solicitation to gather as many advertisements as we can
996 * before selection occurs.
998 if (first && time_after(jiffies, fip->sol_time + sol_tov))
999 fcoe_ctlr_solicit(fip, NULL);
1002 * Put this FCF at the head of the list for priority among equals.
1003 * This helps in the case of an NPV switch which insists we use
1004 * the FCF that answers multicast solicitations, not the others that
1005 * are sending periodic multicast advertisements.
1007 if (mtu_valid) {
1008 list_del(&fcf->list);
1009 list_add(&fcf->list, &fip->fcfs);
1013 * If this is the first validated FCF, note the time and
1014 * set a timer to trigger selection.
1016 if (mtu_valid && !fip->sel_fcf && fcoe_ctlr_fcf_usable(fcf)) {
1017 fip->sel_time = jiffies +
1018 msecs_to_jiffies(FCOE_CTLR_START_DELAY);
1019 if (!timer_pending(&fip->timer) ||
1020 time_before(fip->sel_time, fip->timer.expires))
1021 mod_timer(&fip->timer, fip->sel_time);
1023 out:
1024 mutex_unlock(&fip->ctlr_mutex);
1028 * fcoe_ctlr_recv_els() - Handle an incoming FIP encapsulated ELS frame
1029 * @fip: The FCoE controller which received the packet
1030 * @skb: The received FIP packet
1032 static void fcoe_ctlr_recv_els(struct fcoe_ctlr *fip, struct sk_buff *skb)
1034 struct fc_lport *lport = fip->lp;
1035 struct fip_header *fiph;
1036 struct fc_frame *fp = (struct fc_frame *)skb;
1037 struct fc_frame_header *fh = NULL;
1038 struct fip_desc *desc;
1039 struct fip_encaps *els;
1040 struct fcoe_dev_stats *stats;
1041 enum fip_desc_type els_dtype = 0;
1042 u8 els_op;
1043 u8 sub;
1044 u8 granted_mac[ETH_ALEN] = { 0 };
1045 size_t els_len = 0;
1046 size_t rlen;
1047 size_t dlen;
1048 u32 desc_mask = 0;
1049 u32 desc_cnt = 0;
1051 fiph = (struct fip_header *)skb->data;
1052 sub = fiph->fip_subcode;
1053 if (sub != FIP_SC_REQ && sub != FIP_SC_REP)
1054 goto drop;
1056 rlen = ntohs(fiph->fip_dl_len) * 4;
1057 if (rlen + sizeof(*fiph) > skb->len)
1058 goto drop;
1060 desc = (struct fip_desc *)(fiph + 1);
1061 while (rlen > 0) {
1062 desc_cnt++;
1063 dlen = desc->fip_dlen * FIP_BPW;
1064 if (dlen < sizeof(*desc) || dlen > rlen)
1065 goto drop;
1066 /* Drop ELS if there are duplicate critical descriptors */
1067 if (desc->fip_dtype < 32) {
1068 if (desc_mask & 1U << desc->fip_dtype) {
1069 LIBFCOE_FIP_DBG(fip, "Duplicate Critical "
1070 "Descriptors in FIP ELS\n");
1071 goto drop;
1073 desc_mask |= (1 << desc->fip_dtype);
1075 switch (desc->fip_dtype) {
1076 case FIP_DT_MAC:
1077 if (desc_cnt == 1) {
1078 LIBFCOE_FIP_DBG(fip, "FIP descriptors "
1079 "received out of order\n");
1080 goto drop;
1083 if (dlen != sizeof(struct fip_mac_desc))
1084 goto len_err;
1085 memcpy(granted_mac,
1086 ((struct fip_mac_desc *)desc)->fd_mac,
1087 ETH_ALEN);
1088 break;
1089 case FIP_DT_FLOGI:
1090 case FIP_DT_FDISC:
1091 case FIP_DT_LOGO:
1092 case FIP_DT_ELP:
1093 if (desc_cnt != 1) {
1094 LIBFCOE_FIP_DBG(fip, "FIP descriptors "
1095 "received out of order\n");
1096 goto drop;
1098 if (fh)
1099 goto drop;
1100 if (dlen < sizeof(*els) + sizeof(*fh) + 1)
1101 goto len_err;
1102 els_len = dlen - sizeof(*els);
1103 els = (struct fip_encaps *)desc;
1104 fh = (struct fc_frame_header *)(els + 1);
1105 els_dtype = desc->fip_dtype;
1106 break;
1107 default:
1108 LIBFCOE_FIP_DBG(fip, "unexpected descriptor type %x "
1109 "in FIP adv\n", desc->fip_dtype);
1110 /* standard says ignore unknown descriptors >= 128 */
1111 if (desc->fip_dtype < FIP_DT_VENDOR_BASE)
1112 goto drop;
1113 if (desc_cnt <= 2) {
1114 LIBFCOE_FIP_DBG(fip, "FIP descriptors "
1115 "received out of order\n");
1116 goto drop;
1118 break;
1120 desc = (struct fip_desc *)((char *)desc + dlen);
1121 rlen -= dlen;
1124 if (!fh)
1125 goto drop;
1126 els_op = *(u8 *)(fh + 1);
1128 if ((els_dtype == FIP_DT_FLOGI || els_dtype == FIP_DT_FDISC) &&
1129 sub == FIP_SC_REP && fip->mode != FIP_MODE_VN2VN) {
1130 if (els_op == ELS_LS_ACC) {
1131 if (!is_valid_ether_addr(granted_mac)) {
1132 LIBFCOE_FIP_DBG(fip,
1133 "Invalid MAC address %pM in FIP ELS\n",
1134 granted_mac);
1135 goto drop;
1137 memcpy(fr_cb(fp)->granted_mac, granted_mac, ETH_ALEN);
1139 if (fip->flogi_oxid == ntohs(fh->fh_ox_id)) {
1140 fip->flogi_oxid = FC_XID_UNKNOWN;
1141 if (els_dtype == FIP_DT_FLOGI)
1142 fcoe_ctlr_announce(fip);
1144 } else if (els_dtype == FIP_DT_FLOGI &&
1145 !fcoe_ctlr_flogi_retry(fip))
1146 goto drop; /* retrying FLOGI so drop reject */
1149 if ((desc_cnt == 0) || ((els_op != ELS_LS_RJT) &&
1150 (!(1U << FIP_DT_MAC & desc_mask)))) {
1151 LIBFCOE_FIP_DBG(fip, "Missing critical descriptors "
1152 "in FIP ELS\n");
1153 goto drop;
1157 * Convert skb into an fc_frame containing only the ELS.
1159 skb_pull(skb, (u8 *)fh - skb->data);
1160 skb_trim(skb, els_len);
1161 fp = (struct fc_frame *)skb;
1162 fc_frame_init(fp);
1163 fr_sof(fp) = FC_SOF_I3;
1164 fr_eof(fp) = FC_EOF_T;
1165 fr_dev(fp) = lport;
1166 fr_encaps(fp) = els_dtype;
1168 stats = per_cpu_ptr(lport->dev_stats, get_cpu());
1169 stats->RxFrames++;
1170 stats->RxWords += skb->len / FIP_BPW;
1171 put_cpu();
1173 fc_exch_recv(lport, fp);
1174 return;
1176 len_err:
1177 LIBFCOE_FIP_DBG(fip, "FIP length error in descriptor type %x len %zu\n",
1178 desc->fip_dtype, dlen);
1179 drop:
1180 kfree_skb(skb);
1184 * fcoe_ctlr_recv_els() - Handle an incoming link reset frame
1185 * @fip: The FCoE controller that received the frame
1186 * @fh: The received FIP header
1188 * There may be multiple VN_Port descriptors.
1189 * The overall length has already been checked.
1191 static void fcoe_ctlr_recv_clr_vlink(struct fcoe_ctlr *fip,
1192 struct fip_header *fh)
1194 struct fip_desc *desc;
1195 struct fip_mac_desc *mp;
1196 struct fip_wwn_desc *wp;
1197 struct fip_vn_desc *vp;
1198 size_t rlen;
1199 size_t dlen;
1200 struct fcoe_fcf *fcf = fip->sel_fcf;
1201 struct fc_lport *lport = fip->lp;
1202 struct fc_lport *vn_port = NULL;
1203 u32 desc_mask;
1204 int is_vn_port = 0;
1206 LIBFCOE_FIP_DBG(fip, "Clear Virtual Link received\n");
1208 if (!fcf || !lport->port_id)
1209 return;
1212 * mask of required descriptors. Validating each one clears its bit.
1214 desc_mask = BIT(FIP_DT_MAC) | BIT(FIP_DT_NAME) | BIT(FIP_DT_VN_ID);
1216 rlen = ntohs(fh->fip_dl_len) * FIP_BPW;
1217 desc = (struct fip_desc *)(fh + 1);
1218 while (rlen >= sizeof(*desc)) {
1219 dlen = desc->fip_dlen * FIP_BPW;
1220 if (dlen > rlen)
1221 return;
1222 /* Drop CVL if there are duplicate critical descriptors */
1223 if ((desc->fip_dtype < 32) &&
1224 !(desc_mask & 1U << desc->fip_dtype)) {
1225 LIBFCOE_FIP_DBG(fip, "Duplicate Critical "
1226 "Descriptors in FIP CVL\n");
1227 return;
1229 switch (desc->fip_dtype) {
1230 case FIP_DT_MAC:
1231 mp = (struct fip_mac_desc *)desc;
1232 if (dlen < sizeof(*mp))
1233 return;
1234 if (compare_ether_addr(mp->fd_mac, fcf->fcf_mac))
1235 return;
1236 desc_mask &= ~BIT(FIP_DT_MAC);
1237 break;
1238 case FIP_DT_NAME:
1239 wp = (struct fip_wwn_desc *)desc;
1240 if (dlen < sizeof(*wp))
1241 return;
1242 if (get_unaligned_be64(&wp->fd_wwn) != fcf->switch_name)
1243 return;
1244 desc_mask &= ~BIT(FIP_DT_NAME);
1245 break;
1246 case FIP_DT_VN_ID:
1247 vp = (struct fip_vn_desc *)desc;
1248 if (dlen < sizeof(*vp))
1249 return;
1250 if (compare_ether_addr(vp->fd_mac,
1251 fip->get_src_addr(lport)) == 0 &&
1252 get_unaligned_be64(&vp->fd_wwpn) == lport->wwpn &&
1253 ntoh24(vp->fd_fc_id) == lport->port_id) {
1254 desc_mask &= ~BIT(FIP_DT_VN_ID);
1255 break;
1257 /* check if clr_vlink is for NPIV port */
1258 mutex_lock(&lport->lp_mutex);
1259 list_for_each_entry(vn_port, &lport->vports, list) {
1260 if (compare_ether_addr(vp->fd_mac,
1261 fip->get_src_addr(vn_port)) == 0 &&
1262 (get_unaligned_be64(&vp->fd_wwpn)
1263 == vn_port->wwpn) &&
1264 (ntoh24(vp->fd_fc_id) ==
1265 fc_host_port_id(vn_port->host))) {
1266 desc_mask &= ~BIT(FIP_DT_VN_ID);
1267 is_vn_port = 1;
1268 break;
1271 mutex_unlock(&lport->lp_mutex);
1273 break;
1274 default:
1275 /* standard says ignore unknown descriptors >= 128 */
1276 if (desc->fip_dtype < FIP_DT_VENDOR_BASE)
1277 return;
1278 break;
1280 desc = (struct fip_desc *)((char *)desc + dlen);
1281 rlen -= dlen;
1285 * reset only if all required descriptors were present and valid.
1287 if (desc_mask) {
1288 LIBFCOE_FIP_DBG(fip, "missing descriptors mask %x\n",
1289 desc_mask);
1290 } else {
1291 LIBFCOE_FIP_DBG(fip, "performing Clear Virtual Link\n");
1293 if (is_vn_port)
1294 fc_lport_reset(vn_port);
1295 else {
1296 mutex_lock(&fip->ctlr_mutex);
1297 per_cpu_ptr(lport->dev_stats,
1298 get_cpu())->VLinkFailureCount++;
1299 put_cpu();
1300 fcoe_ctlr_reset(fip);
1301 mutex_unlock(&fip->ctlr_mutex);
1303 fc_lport_reset(fip->lp);
1304 fcoe_ctlr_solicit(fip, NULL);
1310 * fcoe_ctlr_recv() - Receive a FIP packet
1311 * @fip: The FCoE controller that received the packet
1312 * @skb: The received FIP packet
1314 * This may be called from either NET_RX_SOFTIRQ or IRQ.
1316 void fcoe_ctlr_recv(struct fcoe_ctlr *fip, struct sk_buff *skb)
1318 skb_queue_tail(&fip->fip_recv_list, skb);
1319 schedule_work(&fip->recv_work);
1321 EXPORT_SYMBOL(fcoe_ctlr_recv);
1324 * fcoe_ctlr_recv_handler() - Receive a FIP frame
1325 * @fip: The FCoE controller that received the frame
1326 * @skb: The received FIP frame
1328 * Returns non-zero if the frame is dropped.
1330 static int fcoe_ctlr_recv_handler(struct fcoe_ctlr *fip, struct sk_buff *skb)
1332 struct fip_header *fiph;
1333 struct ethhdr *eh;
1334 enum fip_state state;
1335 u16 op;
1336 u8 sub;
1338 if (skb_linearize(skb))
1339 goto drop;
1340 if (skb->len < sizeof(*fiph))
1341 goto drop;
1342 eh = eth_hdr(skb);
1343 if (fip->mode == FIP_MODE_VN2VN) {
1344 if (compare_ether_addr(eh->h_dest, fip->ctl_src_addr) &&
1345 compare_ether_addr(eh->h_dest, fcoe_all_vn2vn) &&
1346 compare_ether_addr(eh->h_dest, fcoe_all_p2p))
1347 goto drop;
1348 } else if (compare_ether_addr(eh->h_dest, fip->ctl_src_addr) &&
1349 compare_ether_addr(eh->h_dest, fcoe_all_enode))
1350 goto drop;
1351 fiph = (struct fip_header *)skb->data;
1352 op = ntohs(fiph->fip_op);
1353 sub = fiph->fip_subcode;
1355 if (FIP_VER_DECAPS(fiph->fip_ver) != FIP_VER)
1356 goto drop;
1357 if (ntohs(fiph->fip_dl_len) * FIP_BPW + sizeof(*fiph) > skb->len)
1358 goto drop;
1360 mutex_lock(&fip->ctlr_mutex);
1361 state = fip->state;
1362 if (state == FIP_ST_AUTO) {
1363 fip->map_dest = 0;
1364 fcoe_ctlr_set_state(fip, FIP_ST_ENABLED);
1365 state = FIP_ST_ENABLED;
1366 LIBFCOE_FIP_DBG(fip, "Using FIP mode\n");
1368 mutex_unlock(&fip->ctlr_mutex);
1370 if (fip->mode == FIP_MODE_VN2VN && op == FIP_OP_VN2VN)
1371 return fcoe_ctlr_vn_recv(fip, skb);
1373 if (state != FIP_ST_ENABLED && state != FIP_ST_VNMP_UP &&
1374 state != FIP_ST_VNMP_CLAIM)
1375 goto drop;
1377 if (op == FIP_OP_LS) {
1378 fcoe_ctlr_recv_els(fip, skb); /* consumes skb */
1379 return 0;
1382 if (state != FIP_ST_ENABLED)
1383 goto drop;
1385 if (op == FIP_OP_DISC && sub == FIP_SC_ADV)
1386 fcoe_ctlr_recv_adv(fip, skb);
1387 else if (op == FIP_OP_CTRL && sub == FIP_SC_CLR_VLINK)
1388 fcoe_ctlr_recv_clr_vlink(fip, fiph);
1389 kfree_skb(skb);
1390 return 0;
1391 drop:
1392 kfree_skb(skb);
1393 return -1;
1397 * fcoe_ctlr_select() - Select the best FCF (if possible)
1398 * @fip: The FCoE controller
1400 * Returns the selected FCF, or NULL if none are usable.
1402 * If there are conflicting advertisements, no FCF can be chosen.
1404 * If there is already a selected FCF, this will choose a better one or
1405 * an equivalent one that hasn't already been sent a FLOGI.
1407 * Called with lock held.
1409 static struct fcoe_fcf *fcoe_ctlr_select(struct fcoe_ctlr *fip)
1411 struct fcoe_fcf *fcf;
1412 struct fcoe_fcf *best = fip->sel_fcf;
1413 struct fcoe_fcf *first;
1415 first = list_first_entry(&fip->fcfs, struct fcoe_fcf, list);
1417 list_for_each_entry(fcf, &fip->fcfs, list) {
1418 LIBFCOE_FIP_DBG(fip, "consider FCF fab %16.16llx "
1419 "VFID %d mac %pM map %x val %d "
1420 "sent %u pri %u\n",
1421 fcf->fabric_name, fcf->vfid, fcf->fcf_mac,
1422 fcf->fc_map, fcoe_ctlr_mtu_valid(fcf),
1423 fcf->flogi_sent, fcf->pri);
1424 if (fcf->fabric_name != first->fabric_name ||
1425 fcf->vfid != first->vfid ||
1426 fcf->fc_map != first->fc_map) {
1427 LIBFCOE_FIP_DBG(fip, "Conflicting fabric, VFID, "
1428 "or FC-MAP\n");
1429 return NULL;
1431 if (fcf->flogi_sent)
1432 continue;
1433 if (!fcoe_ctlr_fcf_usable(fcf)) {
1434 LIBFCOE_FIP_DBG(fip, "FCF for fab %16.16llx "
1435 "map %x %svalid %savailable\n",
1436 fcf->fabric_name, fcf->fc_map,
1437 (fcf->flags & FIP_FL_SOL) ? "" : "in",
1438 (fcf->flags & FIP_FL_AVAIL) ?
1439 "" : "un");
1440 continue;
1442 if (!best || fcf->pri < best->pri || best->flogi_sent)
1443 best = fcf;
1445 fip->sel_fcf = best;
1446 if (best) {
1447 LIBFCOE_FIP_DBG(fip, "using FCF mac %pM\n", best->fcf_mac);
1448 fip->port_ka_time = jiffies +
1449 msecs_to_jiffies(FIP_VN_KA_PERIOD);
1450 fip->ctlr_ka_time = jiffies + best->fka_period;
1451 if (time_before(fip->ctlr_ka_time, fip->timer.expires))
1452 mod_timer(&fip->timer, fip->ctlr_ka_time);
1454 return best;
1458 * fcoe_ctlr_flogi_send_locked() - send FIP-encapsulated FLOGI to current FCF
1459 * @fip: The FCoE controller
1461 * Returns non-zero error if it could not be sent.
1463 * Called with ctlr_mutex and ctlr_lock held.
1464 * Caller must verify that fip->sel_fcf is not NULL.
1466 static int fcoe_ctlr_flogi_send_locked(struct fcoe_ctlr *fip)
1468 struct sk_buff *skb;
1469 struct sk_buff *skb_orig;
1470 struct fc_frame_header *fh;
1471 int error;
1473 skb_orig = fip->flogi_req;
1474 if (!skb_orig)
1475 return -EINVAL;
1478 * Clone and send the FLOGI request. If clone fails, use original.
1480 skb = skb_clone(skb_orig, GFP_ATOMIC);
1481 if (!skb) {
1482 skb = skb_orig;
1483 fip->flogi_req = NULL;
1485 fh = (struct fc_frame_header *)skb->data;
1486 error = fcoe_ctlr_encaps(fip, fip->lp, FIP_DT_FLOGI, skb,
1487 ntoh24(fh->fh_d_id));
1488 if (error) {
1489 kfree_skb(skb);
1490 return error;
1492 fip->send(fip, skb);
1493 fip->sel_fcf->flogi_sent = 1;
1494 return 0;
1498 * fcoe_ctlr_flogi_retry() - resend FLOGI request to a new FCF if possible
1499 * @fip: The FCoE controller
1501 * Returns non-zero error code if there's no FLOGI request to retry or
1502 * no alternate FCF available.
1504 static int fcoe_ctlr_flogi_retry(struct fcoe_ctlr *fip)
1506 struct fcoe_fcf *fcf;
1507 int error;
1509 mutex_lock(&fip->ctlr_mutex);
1510 spin_lock_bh(&fip->ctlr_lock);
1511 LIBFCOE_FIP_DBG(fip, "re-sending FLOGI - reselect\n");
1512 fcf = fcoe_ctlr_select(fip);
1513 if (!fcf || fcf->flogi_sent) {
1514 kfree_skb(fip->flogi_req);
1515 fip->flogi_req = NULL;
1516 error = -ENOENT;
1517 } else {
1518 fcoe_ctlr_solicit(fip, NULL);
1519 error = fcoe_ctlr_flogi_send_locked(fip);
1521 spin_unlock_bh(&fip->ctlr_lock);
1522 mutex_unlock(&fip->ctlr_mutex);
1523 return error;
1528 * fcoe_ctlr_flogi_send() - Handle sending of FIP FLOGI.
1529 * @fip: The FCoE controller that timed out
1531 * Done here because fcoe_ctlr_els_send() can't get mutex.
1533 * Called with ctlr_mutex held. The caller must not hold ctlr_lock.
1535 static void fcoe_ctlr_flogi_send(struct fcoe_ctlr *fip)
1537 struct fcoe_fcf *fcf;
1539 spin_lock_bh(&fip->ctlr_lock);
1540 fcf = fip->sel_fcf;
1541 if (!fcf || !fip->flogi_req_send)
1542 goto unlock;
1544 LIBFCOE_FIP_DBG(fip, "sending FLOGI\n");
1547 * If this FLOGI is being sent due to a timeout retry
1548 * to the same FCF as before, select a different FCF if possible.
1550 if (fcf->flogi_sent) {
1551 LIBFCOE_FIP_DBG(fip, "sending FLOGI - reselect\n");
1552 fcf = fcoe_ctlr_select(fip);
1553 if (!fcf || fcf->flogi_sent) {
1554 LIBFCOE_FIP_DBG(fip, "sending FLOGI - clearing\n");
1555 list_for_each_entry(fcf, &fip->fcfs, list)
1556 fcf->flogi_sent = 0;
1557 fcf = fcoe_ctlr_select(fip);
1560 if (fcf) {
1561 fcoe_ctlr_flogi_send_locked(fip);
1562 fip->flogi_req_send = 0;
1563 } else /* XXX */
1564 LIBFCOE_FIP_DBG(fip, "No FCF selected - defer send\n");
1565 unlock:
1566 spin_unlock_bh(&fip->ctlr_lock);
1570 * fcoe_ctlr_timeout() - FIP timeout handler
1571 * @arg: The FCoE controller that timed out
1573 static void fcoe_ctlr_timeout(unsigned long arg)
1575 struct fcoe_ctlr *fip = (struct fcoe_ctlr *)arg;
1577 schedule_work(&fip->timer_work);
1581 * fcoe_ctlr_timer_work() - Worker thread function for timer work
1582 * @work: Handle to a FCoE controller
1584 * Ages FCFs. Triggers FCF selection if possible.
1585 * Sends keep-alives and resets.
1587 static void fcoe_ctlr_timer_work(struct work_struct *work)
1589 struct fcoe_ctlr *fip;
1590 struct fc_lport *vport;
1591 u8 *mac;
1592 u8 reset = 0;
1593 u8 send_ctlr_ka = 0;
1594 u8 send_port_ka = 0;
1595 struct fcoe_fcf *sel;
1596 struct fcoe_fcf *fcf;
1597 unsigned long next_timer;
1599 fip = container_of(work, struct fcoe_ctlr, timer_work);
1600 if (fip->mode == FIP_MODE_VN2VN)
1601 return fcoe_ctlr_vn_timeout(fip);
1602 mutex_lock(&fip->ctlr_mutex);
1603 if (fip->state == FIP_ST_DISABLED) {
1604 mutex_unlock(&fip->ctlr_mutex);
1605 return;
1608 fcf = fip->sel_fcf;
1609 next_timer = fcoe_ctlr_age_fcfs(fip);
1611 sel = fip->sel_fcf;
1612 if (!sel && fip->sel_time) {
1613 if (time_after_eq(jiffies, fip->sel_time)) {
1614 sel = fcoe_ctlr_select(fip);
1615 fip->sel_time = 0;
1616 } else if (time_after(next_timer, fip->sel_time))
1617 next_timer = fip->sel_time;
1620 if (sel && fip->flogi_req_send)
1621 fcoe_ctlr_flogi_send(fip);
1622 else if (!sel && fcf)
1623 reset = 1;
1625 if (sel && !sel->fd_flags) {
1626 if (time_after_eq(jiffies, fip->ctlr_ka_time)) {
1627 fip->ctlr_ka_time = jiffies + sel->fka_period;
1628 send_ctlr_ka = 1;
1630 if (time_after(next_timer, fip->ctlr_ka_time))
1631 next_timer = fip->ctlr_ka_time;
1633 if (time_after_eq(jiffies, fip->port_ka_time)) {
1634 fip->port_ka_time = jiffies +
1635 msecs_to_jiffies(FIP_VN_KA_PERIOD);
1636 send_port_ka = 1;
1638 if (time_after(next_timer, fip->port_ka_time))
1639 next_timer = fip->port_ka_time;
1641 if (!list_empty(&fip->fcfs))
1642 mod_timer(&fip->timer, next_timer);
1643 mutex_unlock(&fip->ctlr_mutex);
1645 if (reset) {
1646 fc_lport_reset(fip->lp);
1647 /* restart things with a solicitation */
1648 fcoe_ctlr_solicit(fip, NULL);
1651 if (send_ctlr_ka)
1652 fcoe_ctlr_send_keep_alive(fip, NULL, 0, fip->ctl_src_addr);
1654 if (send_port_ka) {
1655 mutex_lock(&fip->lp->lp_mutex);
1656 mac = fip->get_src_addr(fip->lp);
1657 fcoe_ctlr_send_keep_alive(fip, fip->lp, 1, mac);
1658 list_for_each_entry(vport, &fip->lp->vports, list) {
1659 mac = fip->get_src_addr(vport);
1660 fcoe_ctlr_send_keep_alive(fip, vport, 1, mac);
1662 mutex_unlock(&fip->lp->lp_mutex);
1667 * fcoe_ctlr_recv_work() - Worker thread function for receiving FIP frames
1668 * @recv_work: Handle to a FCoE controller
1670 static void fcoe_ctlr_recv_work(struct work_struct *recv_work)
1672 struct fcoe_ctlr *fip;
1673 struct sk_buff *skb;
1675 fip = container_of(recv_work, struct fcoe_ctlr, recv_work);
1676 while ((skb = skb_dequeue(&fip->fip_recv_list)))
1677 fcoe_ctlr_recv_handler(fip, skb);
1681 * fcoe_ctlr_recv_flogi() - Snoop pre-FIP receipt of FLOGI response
1682 * @fip: The FCoE controller
1683 * @fp: The FC frame to snoop
1685 * Snoop potential response to FLOGI or even incoming FLOGI.
1687 * The caller has checked that we are waiting for login as indicated
1688 * by fip->flogi_oxid != FC_XID_UNKNOWN.
1690 * The caller is responsible for freeing the frame.
1691 * Fill in the granted_mac address.
1693 * Return non-zero if the frame should not be delivered to libfc.
1695 int fcoe_ctlr_recv_flogi(struct fcoe_ctlr *fip, struct fc_lport *lport,
1696 struct fc_frame *fp)
1698 struct fc_frame_header *fh;
1699 u8 op;
1700 u8 *sa;
1702 sa = eth_hdr(&fp->skb)->h_source;
1703 fh = fc_frame_header_get(fp);
1704 if (fh->fh_type != FC_TYPE_ELS)
1705 return 0;
1707 op = fc_frame_payload_op(fp);
1708 if (op == ELS_LS_ACC && fh->fh_r_ctl == FC_RCTL_ELS_REP &&
1709 fip->flogi_oxid == ntohs(fh->fh_ox_id)) {
1711 mutex_lock(&fip->ctlr_mutex);
1712 if (fip->state != FIP_ST_AUTO && fip->state != FIP_ST_NON_FIP) {
1713 mutex_unlock(&fip->ctlr_mutex);
1714 return -EINVAL;
1716 fcoe_ctlr_set_state(fip, FIP_ST_NON_FIP);
1717 LIBFCOE_FIP_DBG(fip,
1718 "received FLOGI LS_ACC using non-FIP mode\n");
1721 * FLOGI accepted.
1722 * If the src mac addr is FC_OUI-based, then we mark the
1723 * address_mode flag to use FC_OUI-based Ethernet DA.
1724 * Otherwise we use the FCoE gateway addr
1726 if (!compare_ether_addr(sa, (u8[6])FC_FCOE_FLOGI_MAC)) {
1727 fcoe_ctlr_map_dest(fip);
1728 } else {
1729 memcpy(fip->dest_addr, sa, ETH_ALEN);
1730 fip->map_dest = 0;
1732 fip->flogi_oxid = FC_XID_UNKNOWN;
1733 mutex_unlock(&fip->ctlr_mutex);
1734 fc_fcoe_set_mac(fr_cb(fp)->granted_mac, fh->fh_d_id);
1735 } else if (op == ELS_FLOGI && fh->fh_r_ctl == FC_RCTL_ELS_REQ && sa) {
1737 * Save source MAC for point-to-point responses.
1739 mutex_lock(&fip->ctlr_mutex);
1740 if (fip->state == FIP_ST_AUTO || fip->state == FIP_ST_NON_FIP) {
1741 memcpy(fip->dest_addr, sa, ETH_ALEN);
1742 fip->map_dest = 0;
1743 if (fip->state == FIP_ST_AUTO)
1744 LIBFCOE_FIP_DBG(fip, "received non-FIP FLOGI. "
1745 "Setting non-FIP mode\n");
1746 fcoe_ctlr_set_state(fip, FIP_ST_NON_FIP);
1748 mutex_unlock(&fip->ctlr_mutex);
1750 return 0;
1752 EXPORT_SYMBOL(fcoe_ctlr_recv_flogi);
1755 * fcoe_wwn_from_mac() - Converts a 48-bit IEEE MAC address to a 64-bit FC WWN
1756 * @mac: The MAC address to convert
1757 * @scheme: The scheme to use when converting
1758 * @port: The port indicator for converting
1760 * Returns: u64 fc world wide name
1762 u64 fcoe_wwn_from_mac(unsigned char mac[MAX_ADDR_LEN],
1763 unsigned int scheme, unsigned int port)
1765 u64 wwn;
1766 u64 host_mac;
1768 /* The MAC is in NO, so flip only the low 48 bits */
1769 host_mac = ((u64) mac[0] << 40) |
1770 ((u64) mac[1] << 32) |
1771 ((u64) mac[2] << 24) |
1772 ((u64) mac[3] << 16) |
1773 ((u64) mac[4] << 8) |
1774 (u64) mac[5];
1776 WARN_ON(host_mac >= (1ULL << 48));
1777 wwn = host_mac | ((u64) scheme << 60);
1778 switch (scheme) {
1779 case 1:
1780 WARN_ON(port != 0);
1781 break;
1782 case 2:
1783 WARN_ON(port >= 0xfff);
1784 wwn |= (u64) port << 48;
1785 break;
1786 default:
1787 WARN_ON(1);
1788 break;
1791 return wwn;
1793 EXPORT_SYMBOL_GPL(fcoe_wwn_from_mac);
1796 * fcoe_ctlr_rport() - return the fcoe_rport for a given fc_rport_priv
1797 * @rdata: libfc remote port
1799 static inline struct fcoe_rport *fcoe_ctlr_rport(struct fc_rport_priv *rdata)
1801 return (struct fcoe_rport *)(rdata + 1);
1805 * fcoe_ctlr_vn_send() - Send a FIP VN2VN Probe Request or Reply.
1806 * @fip: The FCoE controller
1807 * @sub: sub-opcode for probe request, reply, or advertisement.
1808 * @dest: The destination Ethernet MAC address
1809 * @min_len: minimum size of the Ethernet payload to be sent
1811 static void fcoe_ctlr_vn_send(struct fcoe_ctlr *fip,
1812 enum fip_vn2vn_subcode sub,
1813 const u8 *dest, size_t min_len)
1815 struct sk_buff *skb;
1816 struct fip_frame {
1817 struct ethhdr eth;
1818 struct fip_header fip;
1819 struct fip_mac_desc mac;
1820 struct fip_wwn_desc wwnn;
1821 struct fip_vn_desc vn;
1822 } __attribute__((packed)) *frame;
1823 struct fip_fc4_feat *ff;
1824 struct fip_size_desc *size;
1825 u32 fcp_feat;
1826 size_t len;
1827 size_t dlen;
1829 len = sizeof(*frame);
1830 dlen = 0;
1831 if (sub == FIP_SC_VN_CLAIM_NOTIFY || sub == FIP_SC_VN_CLAIM_REP) {
1832 dlen = sizeof(struct fip_fc4_feat) +
1833 sizeof(struct fip_size_desc);
1834 len += dlen;
1836 dlen += sizeof(frame->mac) + sizeof(frame->wwnn) + sizeof(frame->vn);
1837 len = max(len, min_len + sizeof(struct ethhdr));
1839 skb = dev_alloc_skb(len);
1840 if (!skb)
1841 return;
1843 frame = (struct fip_frame *)skb->data;
1844 memset(frame, 0, len);
1845 memcpy(frame->eth.h_dest, dest, ETH_ALEN);
1846 memcpy(frame->eth.h_source, fip->ctl_src_addr, ETH_ALEN);
1847 frame->eth.h_proto = htons(ETH_P_FIP);
1849 frame->fip.fip_ver = FIP_VER_ENCAPS(FIP_VER);
1850 frame->fip.fip_op = htons(FIP_OP_VN2VN);
1851 frame->fip.fip_subcode = sub;
1852 frame->fip.fip_dl_len = htons(dlen / FIP_BPW);
1854 frame->mac.fd_desc.fip_dtype = FIP_DT_MAC;
1855 frame->mac.fd_desc.fip_dlen = sizeof(frame->mac) / FIP_BPW;
1856 memcpy(frame->mac.fd_mac, fip->ctl_src_addr, ETH_ALEN);
1858 frame->wwnn.fd_desc.fip_dtype = FIP_DT_NAME;
1859 frame->wwnn.fd_desc.fip_dlen = sizeof(frame->wwnn) / FIP_BPW;
1860 put_unaligned_be64(fip->lp->wwnn, &frame->wwnn.fd_wwn);
1862 frame->vn.fd_desc.fip_dtype = FIP_DT_VN_ID;
1863 frame->vn.fd_desc.fip_dlen = sizeof(frame->vn) / FIP_BPW;
1864 hton24(frame->vn.fd_mac, FIP_VN_FC_MAP);
1865 hton24(frame->vn.fd_mac + 3, fip->port_id);
1866 hton24(frame->vn.fd_fc_id, fip->port_id);
1867 put_unaligned_be64(fip->lp->wwpn, &frame->vn.fd_wwpn);
1870 * For claims, add FC-4 features.
1871 * TBD: Add interface to get fc-4 types and features from libfc.
1873 if (sub == FIP_SC_VN_CLAIM_NOTIFY || sub == FIP_SC_VN_CLAIM_REP) {
1874 ff = (struct fip_fc4_feat *)(frame + 1);
1875 ff->fd_desc.fip_dtype = FIP_DT_FC4F;
1876 ff->fd_desc.fip_dlen = sizeof(*ff) / FIP_BPW;
1877 ff->fd_fts = fip->lp->fcts;
1879 fcp_feat = 0;
1880 if (fip->lp->service_params & FCP_SPPF_INIT_FCN)
1881 fcp_feat |= FCP_FEAT_INIT;
1882 if (fip->lp->service_params & FCP_SPPF_TARG_FCN)
1883 fcp_feat |= FCP_FEAT_TARG;
1884 fcp_feat <<= (FC_TYPE_FCP * 4) % 32;
1885 ff->fd_ff.fd_feat[FC_TYPE_FCP * 4 / 32] = htonl(fcp_feat);
1887 size = (struct fip_size_desc *)(ff + 1);
1888 size->fd_desc.fip_dtype = FIP_DT_FCOE_SIZE;
1889 size->fd_desc.fip_dlen = sizeof(*size) / FIP_BPW;
1890 size->fd_size = htons(fcoe_ctlr_fcoe_size(fip));
1893 skb_put(skb, len);
1894 skb->protocol = htons(ETH_P_FIP);
1895 skb_reset_mac_header(skb);
1896 skb_reset_network_header(skb);
1898 fip->send(fip, skb);
1902 * fcoe_ctlr_vn_rport_callback - Event handler for rport events.
1903 * @lport: The lport which is receiving the event
1904 * @rdata: remote port private data
1905 * @event: The event that occured
1907 * Locking Note: The rport lock must not be held when calling this function.
1909 static void fcoe_ctlr_vn_rport_callback(struct fc_lport *lport,
1910 struct fc_rport_priv *rdata,
1911 enum fc_rport_event event)
1913 struct fcoe_ctlr *fip = lport->disc.priv;
1914 struct fcoe_rport *frport = fcoe_ctlr_rport(rdata);
1916 LIBFCOE_FIP_DBG(fip, "vn_rport_callback %x event %d\n",
1917 rdata->ids.port_id, event);
1919 mutex_lock(&fip->ctlr_mutex);
1920 switch (event) {
1921 case RPORT_EV_READY:
1922 frport->login_count = 0;
1923 break;
1924 case RPORT_EV_LOGO:
1925 case RPORT_EV_FAILED:
1926 case RPORT_EV_STOP:
1927 frport->login_count++;
1928 if (frport->login_count > FCOE_CTLR_VN2VN_LOGIN_LIMIT) {
1929 LIBFCOE_FIP_DBG(fip,
1930 "rport FLOGI limited port_id %6.6x\n",
1931 rdata->ids.port_id);
1932 lport->tt.rport_logoff(rdata);
1934 break;
1935 default:
1936 break;
1938 mutex_unlock(&fip->ctlr_mutex);
1941 static struct fc_rport_operations fcoe_ctlr_vn_rport_ops = {
1942 .event_callback = fcoe_ctlr_vn_rport_callback,
1946 * fcoe_ctlr_disc_stop_locked() - stop discovery in VN2VN mode
1947 * @fip: The FCoE controller
1949 * Called with ctlr_mutex held.
1951 static void fcoe_ctlr_disc_stop_locked(struct fc_lport *lport)
1953 mutex_lock(&lport->disc.disc_mutex);
1954 lport->disc.disc_callback = NULL;
1955 mutex_unlock(&lport->disc.disc_mutex);
1959 * fcoe_ctlr_disc_stop() - stop discovery in VN2VN mode
1960 * @fip: The FCoE controller
1962 * Called through the local port template for discovery.
1963 * Called without the ctlr_mutex held.
1965 static void fcoe_ctlr_disc_stop(struct fc_lport *lport)
1967 struct fcoe_ctlr *fip = lport->disc.priv;
1969 mutex_lock(&fip->ctlr_mutex);
1970 fcoe_ctlr_disc_stop_locked(lport);
1971 mutex_unlock(&fip->ctlr_mutex);
1975 * fcoe_ctlr_disc_stop_final() - stop discovery for shutdown in VN2VN mode
1976 * @fip: The FCoE controller
1978 * Called through the local port template for discovery.
1979 * Called without the ctlr_mutex held.
1981 static void fcoe_ctlr_disc_stop_final(struct fc_lport *lport)
1983 fcoe_ctlr_disc_stop(lport);
1984 lport->tt.rport_flush_queue();
1985 synchronize_rcu();
1989 * fcoe_ctlr_vn_restart() - VN2VN probe restart with new port_id
1990 * @fip: The FCoE controller
1992 * Called with fcoe_ctlr lock held.
1994 static void fcoe_ctlr_vn_restart(struct fcoe_ctlr *fip)
1996 unsigned long wait;
1997 u32 port_id;
1999 fcoe_ctlr_disc_stop_locked(fip->lp);
2002 * Get proposed port ID.
2003 * If this is the first try after link up, use any previous port_id.
2004 * If there was none, use the low bits of the port_name.
2005 * On subsequent tries, get the next random one.
2006 * Don't use reserved IDs, use another non-zero value, just as random.
2008 port_id = fip->port_id;
2009 if (fip->probe_tries)
2010 port_id = prandom32(&fip->rnd_state) & 0xffff;
2011 else if (!port_id)
2012 port_id = fip->lp->wwpn & 0xffff;
2013 if (!port_id || port_id == 0xffff)
2014 port_id = 1;
2015 fip->port_id = port_id;
2017 if (fip->probe_tries < FIP_VN_RLIM_COUNT) {
2018 fip->probe_tries++;
2019 wait = random32() % FIP_VN_PROBE_WAIT;
2020 } else
2021 wait = FIP_VN_RLIM_INT;
2022 mod_timer(&fip->timer, jiffies + msecs_to_jiffies(wait));
2023 fcoe_ctlr_set_state(fip, FIP_ST_VNMP_START);
2027 * fcoe_ctlr_vn_start() - Start in VN2VN mode
2028 * @fip: The FCoE controller
2030 * Called with fcoe_ctlr lock held.
2032 static void fcoe_ctlr_vn_start(struct fcoe_ctlr *fip)
2034 fip->probe_tries = 0;
2035 prandom32_seed(&fip->rnd_state, fip->lp->wwpn);
2036 fcoe_ctlr_vn_restart(fip);
2040 * fcoe_ctlr_vn_parse - parse probe request or response
2041 * @fip: The FCoE controller
2042 * @skb: incoming packet
2043 * @rdata: buffer for resulting parsed VN entry plus fcoe_rport
2045 * Returns non-zero error number on error.
2046 * Does not consume the packet.
2048 static int fcoe_ctlr_vn_parse(struct fcoe_ctlr *fip,
2049 struct sk_buff *skb,
2050 struct fc_rport_priv *rdata)
2052 struct fip_header *fiph;
2053 struct fip_desc *desc = NULL;
2054 struct fip_mac_desc *macd = NULL;
2055 struct fip_wwn_desc *wwn = NULL;
2056 struct fip_vn_desc *vn = NULL;
2057 struct fip_size_desc *size = NULL;
2058 struct fcoe_rport *frport;
2059 size_t rlen;
2060 size_t dlen;
2061 u32 desc_mask = 0;
2062 u32 dtype;
2063 u8 sub;
2065 memset(rdata, 0, sizeof(*rdata) + sizeof(*frport));
2066 frport = fcoe_ctlr_rport(rdata);
2068 fiph = (struct fip_header *)skb->data;
2069 frport->flags = ntohs(fiph->fip_flags);
2071 sub = fiph->fip_subcode;
2072 switch (sub) {
2073 case FIP_SC_VN_PROBE_REQ:
2074 case FIP_SC_VN_PROBE_REP:
2075 case FIP_SC_VN_BEACON:
2076 desc_mask = BIT(FIP_DT_MAC) | BIT(FIP_DT_NAME) |
2077 BIT(FIP_DT_VN_ID);
2078 break;
2079 case FIP_SC_VN_CLAIM_NOTIFY:
2080 case FIP_SC_VN_CLAIM_REP:
2081 desc_mask = BIT(FIP_DT_MAC) | BIT(FIP_DT_NAME) |
2082 BIT(FIP_DT_VN_ID) | BIT(FIP_DT_FC4F) |
2083 BIT(FIP_DT_FCOE_SIZE);
2084 break;
2085 default:
2086 LIBFCOE_FIP_DBG(fip, "vn_parse unknown subcode %u\n", sub);
2087 return -EINVAL;
2090 rlen = ntohs(fiph->fip_dl_len) * 4;
2091 if (rlen + sizeof(*fiph) > skb->len)
2092 return -EINVAL;
2094 desc = (struct fip_desc *)(fiph + 1);
2095 while (rlen > 0) {
2096 dlen = desc->fip_dlen * FIP_BPW;
2097 if (dlen < sizeof(*desc) || dlen > rlen)
2098 return -EINVAL;
2100 dtype = desc->fip_dtype;
2101 if (dtype < 32) {
2102 if (!(desc_mask & BIT(dtype))) {
2103 LIBFCOE_FIP_DBG(fip,
2104 "unexpected or duplicated desc "
2105 "desc type %u in "
2106 "FIP VN2VN subtype %u\n",
2107 dtype, sub);
2108 return -EINVAL;
2110 desc_mask &= ~BIT(dtype);
2113 switch (dtype) {
2114 case FIP_DT_MAC:
2115 if (dlen != sizeof(struct fip_mac_desc))
2116 goto len_err;
2117 macd = (struct fip_mac_desc *)desc;
2118 if (!is_valid_ether_addr(macd->fd_mac)) {
2119 LIBFCOE_FIP_DBG(fip,
2120 "Invalid MAC addr %pM in FIP VN2VN\n",
2121 macd->fd_mac);
2122 return -EINVAL;
2124 memcpy(frport->enode_mac, macd->fd_mac, ETH_ALEN);
2125 break;
2126 case FIP_DT_NAME:
2127 if (dlen != sizeof(struct fip_wwn_desc))
2128 goto len_err;
2129 wwn = (struct fip_wwn_desc *)desc;
2130 rdata->ids.node_name = get_unaligned_be64(&wwn->fd_wwn);
2131 break;
2132 case FIP_DT_VN_ID:
2133 if (dlen != sizeof(struct fip_vn_desc))
2134 goto len_err;
2135 vn = (struct fip_vn_desc *)desc;
2136 memcpy(frport->vn_mac, vn->fd_mac, ETH_ALEN);
2137 rdata->ids.port_id = ntoh24(vn->fd_fc_id);
2138 rdata->ids.port_name = get_unaligned_be64(&vn->fd_wwpn);
2139 break;
2140 case FIP_DT_FC4F:
2141 if (dlen != sizeof(struct fip_fc4_feat))
2142 goto len_err;
2143 break;
2144 case FIP_DT_FCOE_SIZE:
2145 if (dlen != sizeof(struct fip_size_desc))
2146 goto len_err;
2147 size = (struct fip_size_desc *)desc;
2148 frport->fcoe_len = ntohs(size->fd_size);
2149 break;
2150 default:
2151 LIBFCOE_FIP_DBG(fip, "unexpected descriptor type %x "
2152 "in FIP probe\n", dtype);
2153 /* standard says ignore unknown descriptors >= 128 */
2154 if (dtype < FIP_DT_VENDOR_BASE)
2155 return -EINVAL;
2156 break;
2158 desc = (struct fip_desc *)((char *)desc + dlen);
2159 rlen -= dlen;
2161 return 0;
2163 len_err:
2164 LIBFCOE_FIP_DBG(fip, "FIP length error in descriptor type %x len %zu\n",
2165 dtype, dlen);
2166 return -EINVAL;
2170 * fcoe_ctlr_vn_send_claim() - send multicast FIP VN2VN Claim Notification.
2171 * @fip: The FCoE controller
2173 * Called with ctlr_mutex held.
2175 static void fcoe_ctlr_vn_send_claim(struct fcoe_ctlr *fip)
2177 fcoe_ctlr_vn_send(fip, FIP_SC_VN_CLAIM_NOTIFY, fcoe_all_vn2vn, 0);
2178 fip->sol_time = jiffies;
2182 * fcoe_ctlr_vn_probe_req() - handle incoming VN2VN probe request.
2183 * @fip: The FCoE controller
2184 * @rdata: parsed remote port with frport from the probe request
2186 * Called with ctlr_mutex held.
2188 static void fcoe_ctlr_vn_probe_req(struct fcoe_ctlr *fip,
2189 struct fc_rport_priv *rdata)
2191 struct fcoe_rport *frport = fcoe_ctlr_rport(rdata);
2193 if (rdata->ids.port_id != fip->port_id)
2194 return;
2196 switch (fip->state) {
2197 case FIP_ST_VNMP_CLAIM:
2198 case FIP_ST_VNMP_UP:
2199 fcoe_ctlr_vn_send(fip, FIP_SC_VN_PROBE_REP,
2200 frport->enode_mac, 0);
2201 break;
2202 case FIP_ST_VNMP_PROBE1:
2203 case FIP_ST_VNMP_PROBE2:
2205 * Decide whether to reply to the Probe.
2206 * Our selected address is never a "recorded" one, so
2207 * only reply if our WWPN is greater and the
2208 * Probe's REC bit is not set.
2209 * If we don't reply, we will change our address.
2211 if (fip->lp->wwpn > rdata->ids.port_name &&
2212 !(frport->flags & FIP_FL_REC_OR_P2P)) {
2213 fcoe_ctlr_vn_send(fip, FIP_SC_VN_PROBE_REP,
2214 frport->enode_mac, 0);
2215 break;
2217 /* fall through */
2218 case FIP_ST_VNMP_START:
2219 fcoe_ctlr_vn_restart(fip);
2220 break;
2221 default:
2222 break;
2227 * fcoe_ctlr_vn_probe_reply() - handle incoming VN2VN probe reply.
2228 * @fip: The FCoE controller
2229 * @rdata: parsed remote port with frport from the probe request
2231 * Called with ctlr_mutex held.
2233 static void fcoe_ctlr_vn_probe_reply(struct fcoe_ctlr *fip,
2234 struct fc_rport_priv *rdata)
2236 if (rdata->ids.port_id != fip->port_id)
2237 return;
2238 switch (fip->state) {
2239 case FIP_ST_VNMP_START:
2240 case FIP_ST_VNMP_PROBE1:
2241 case FIP_ST_VNMP_PROBE2:
2242 case FIP_ST_VNMP_CLAIM:
2243 fcoe_ctlr_vn_restart(fip);
2244 break;
2245 case FIP_ST_VNMP_UP:
2246 fcoe_ctlr_vn_send_claim(fip);
2247 break;
2248 default:
2249 break;
2254 * fcoe_ctlr_vn_add() - Add a VN2VN entry to the list, based on a claim reply.
2255 * @fip: The FCoE controller
2256 * @new: newly-parsed remote port with frport as a template for new rdata
2258 * Called with ctlr_mutex held.
2260 static void fcoe_ctlr_vn_add(struct fcoe_ctlr *fip, struct fc_rport_priv *new)
2262 struct fc_lport *lport = fip->lp;
2263 struct fc_rport_priv *rdata;
2264 struct fc_rport_identifiers *ids;
2265 struct fcoe_rport *frport;
2266 u32 port_id;
2268 port_id = new->ids.port_id;
2269 if (port_id == fip->port_id)
2270 return;
2272 mutex_lock(&lport->disc.disc_mutex);
2273 rdata = lport->tt.rport_create(lport, port_id);
2274 if (!rdata) {
2275 mutex_unlock(&lport->disc.disc_mutex);
2276 return;
2279 rdata->ops = &fcoe_ctlr_vn_rport_ops;
2280 rdata->disc_id = lport->disc.disc_id;
2282 ids = &rdata->ids;
2283 if ((ids->port_name != -1 && ids->port_name != new->ids.port_name) ||
2284 (ids->node_name != -1 && ids->node_name != new->ids.node_name))
2285 lport->tt.rport_logoff(rdata);
2286 ids->port_name = new->ids.port_name;
2287 ids->node_name = new->ids.node_name;
2288 mutex_unlock(&lport->disc.disc_mutex);
2290 frport = fcoe_ctlr_rport(rdata);
2291 LIBFCOE_FIP_DBG(fip, "vn_add rport %6.6x %s\n",
2292 port_id, frport->fcoe_len ? "old" : "new");
2293 *frport = *fcoe_ctlr_rport(new);
2294 frport->time = 0;
2298 * fcoe_ctlr_vn_lookup() - Find VN remote port's MAC address
2299 * @fip: The FCoE controller
2300 * @port_id: The port_id of the remote VN_node
2301 * @mac: buffer which will hold the VN_NODE destination MAC address, if found.
2303 * Returns non-zero error if no remote port found.
2305 static int fcoe_ctlr_vn_lookup(struct fcoe_ctlr *fip, u32 port_id, u8 *mac)
2307 struct fc_lport *lport = fip->lp;
2308 struct fc_rport_priv *rdata;
2309 struct fcoe_rport *frport;
2310 int ret = -1;
2312 rcu_read_lock();
2313 rdata = lport->tt.rport_lookup(lport, port_id);
2314 if (rdata) {
2315 frport = fcoe_ctlr_rport(rdata);
2316 memcpy(mac, frport->enode_mac, ETH_ALEN);
2317 ret = 0;
2319 rcu_read_unlock();
2320 return ret;
2324 * fcoe_ctlr_vn_claim_notify() - handle received FIP VN2VN Claim Notification
2325 * @fip: The FCoE controller
2326 * @new: newly-parsed remote port with frport as a template for new rdata
2328 * Called with ctlr_mutex held.
2330 static void fcoe_ctlr_vn_claim_notify(struct fcoe_ctlr *fip,
2331 struct fc_rport_priv *new)
2333 struct fcoe_rport *frport = fcoe_ctlr_rport(new);
2335 if (frport->flags & FIP_FL_REC_OR_P2P) {
2336 fcoe_ctlr_vn_send(fip, FIP_SC_VN_PROBE_REQ, fcoe_all_vn2vn, 0);
2337 return;
2339 switch (fip->state) {
2340 case FIP_ST_VNMP_START:
2341 case FIP_ST_VNMP_PROBE1:
2342 case FIP_ST_VNMP_PROBE2:
2343 if (new->ids.port_id == fip->port_id)
2344 fcoe_ctlr_vn_restart(fip);
2345 break;
2346 case FIP_ST_VNMP_CLAIM:
2347 case FIP_ST_VNMP_UP:
2348 if (new->ids.port_id == fip->port_id) {
2349 if (new->ids.port_name > fip->lp->wwpn) {
2350 fcoe_ctlr_vn_restart(fip);
2351 break;
2353 fcoe_ctlr_vn_send_claim(fip);
2354 break;
2356 fcoe_ctlr_vn_send(fip, FIP_SC_VN_CLAIM_REP, frport->enode_mac,
2357 min((u32)frport->fcoe_len,
2358 fcoe_ctlr_fcoe_size(fip)));
2359 fcoe_ctlr_vn_add(fip, new);
2360 break;
2361 default:
2362 break;
2367 * fcoe_ctlr_vn_claim_resp() - handle received Claim Response
2368 * @fip: The FCoE controller that received the frame
2369 * @new: newly-parsed remote port with frport from the Claim Response
2371 * Called with ctlr_mutex held.
2373 static void fcoe_ctlr_vn_claim_resp(struct fcoe_ctlr *fip,
2374 struct fc_rport_priv *new)
2376 LIBFCOE_FIP_DBG(fip, "claim resp from from rport %x - state %s\n",
2377 new->ids.port_id, fcoe_ctlr_state(fip->state));
2378 if (fip->state == FIP_ST_VNMP_UP || fip->state == FIP_ST_VNMP_CLAIM)
2379 fcoe_ctlr_vn_add(fip, new);
2383 * fcoe_ctlr_vn_beacon() - handle received beacon.
2384 * @fip: The FCoE controller that received the frame
2385 * @new: newly-parsed remote port with frport from the Beacon
2387 * Called with ctlr_mutex held.
2389 static void fcoe_ctlr_vn_beacon(struct fcoe_ctlr *fip,
2390 struct fc_rport_priv *new)
2392 struct fc_lport *lport = fip->lp;
2393 struct fc_rport_priv *rdata;
2394 struct fcoe_rport *frport;
2396 frport = fcoe_ctlr_rport(new);
2397 if (frport->flags & FIP_FL_REC_OR_P2P) {
2398 fcoe_ctlr_vn_send(fip, FIP_SC_VN_PROBE_REQ, fcoe_all_vn2vn, 0);
2399 return;
2401 mutex_lock(&lport->disc.disc_mutex);
2402 rdata = lport->tt.rport_lookup(lport, new->ids.port_id);
2403 if (rdata)
2404 kref_get(&rdata->kref);
2405 mutex_unlock(&lport->disc.disc_mutex);
2406 if (rdata) {
2407 if (rdata->ids.node_name == new->ids.node_name &&
2408 rdata->ids.port_name == new->ids.port_name) {
2409 frport = fcoe_ctlr_rport(rdata);
2410 if (!frport->time && fip->state == FIP_ST_VNMP_UP)
2411 lport->tt.rport_login(rdata);
2412 frport->time = jiffies;
2414 kref_put(&rdata->kref, lport->tt.rport_destroy);
2415 return;
2417 if (fip->state != FIP_ST_VNMP_UP)
2418 return;
2421 * Beacon from a new neighbor.
2422 * Send a claim notify if one hasn't been sent recently.
2423 * Don't add the neighbor yet.
2425 LIBFCOE_FIP_DBG(fip, "beacon from new rport %x. sending claim notify\n",
2426 new->ids.port_id);
2427 if (time_after(jiffies,
2428 fip->sol_time + msecs_to_jiffies(FIP_VN_ANN_WAIT)))
2429 fcoe_ctlr_vn_send_claim(fip);
2433 * fcoe_ctlr_vn_age() - Check for VN_ports without recent beacons
2434 * @fip: The FCoE controller
2436 * Called with ctlr_mutex held.
2437 * Called only in state FIP_ST_VNMP_UP.
2438 * Returns the soonest time for next age-out or a time far in the future.
2440 static unsigned long fcoe_ctlr_vn_age(struct fcoe_ctlr *fip)
2442 struct fc_lport *lport = fip->lp;
2443 struct fc_rport_priv *rdata;
2444 struct fcoe_rport *frport;
2445 unsigned long next_time;
2446 unsigned long deadline;
2448 next_time = jiffies + msecs_to_jiffies(FIP_VN_BEACON_INT * 10);
2449 mutex_lock(&lport->disc.disc_mutex);
2450 list_for_each_entry_rcu(rdata, &lport->disc.rports, peers) {
2451 frport = fcoe_ctlr_rport(rdata);
2452 if (!frport->time)
2453 continue;
2454 deadline = frport->time +
2455 msecs_to_jiffies(FIP_VN_BEACON_INT * 25 / 10);
2456 if (time_after_eq(jiffies, deadline)) {
2457 frport->time = 0;
2458 LIBFCOE_FIP_DBG(fip,
2459 "port %16.16llx fc_id %6.6x beacon expired\n",
2460 rdata->ids.port_name, rdata->ids.port_id);
2461 lport->tt.rport_logoff(rdata);
2462 } else if (time_before(deadline, next_time))
2463 next_time = deadline;
2465 mutex_unlock(&lport->disc.disc_mutex);
2466 return next_time;
2470 * fcoe_ctlr_vn_recv() - Receive a FIP frame
2471 * @fip: The FCoE controller that received the frame
2472 * @skb: The received FIP frame
2474 * Returns non-zero if the frame is dropped.
2475 * Always consumes the frame.
2477 static int fcoe_ctlr_vn_recv(struct fcoe_ctlr *fip, struct sk_buff *skb)
2479 struct fip_header *fiph;
2480 enum fip_vn2vn_subcode sub;
2481 struct {
2482 struct fc_rport_priv rdata;
2483 struct fcoe_rport frport;
2484 } buf;
2485 int rc;
2487 fiph = (struct fip_header *)skb->data;
2488 sub = fiph->fip_subcode;
2490 rc = fcoe_ctlr_vn_parse(fip, skb, &buf.rdata);
2491 if (rc) {
2492 LIBFCOE_FIP_DBG(fip, "vn_recv vn_parse error %d\n", rc);
2493 goto drop;
2496 mutex_lock(&fip->ctlr_mutex);
2497 switch (sub) {
2498 case FIP_SC_VN_PROBE_REQ:
2499 fcoe_ctlr_vn_probe_req(fip, &buf.rdata);
2500 break;
2501 case FIP_SC_VN_PROBE_REP:
2502 fcoe_ctlr_vn_probe_reply(fip, &buf.rdata);
2503 break;
2504 case FIP_SC_VN_CLAIM_NOTIFY:
2505 fcoe_ctlr_vn_claim_notify(fip, &buf.rdata);
2506 break;
2507 case FIP_SC_VN_CLAIM_REP:
2508 fcoe_ctlr_vn_claim_resp(fip, &buf.rdata);
2509 break;
2510 case FIP_SC_VN_BEACON:
2511 fcoe_ctlr_vn_beacon(fip, &buf.rdata);
2512 break;
2513 default:
2514 LIBFCOE_FIP_DBG(fip, "vn_recv unknown subcode %d\n", sub);
2515 rc = -1;
2516 break;
2518 mutex_unlock(&fip->ctlr_mutex);
2519 drop:
2520 kfree_skb(skb);
2521 return rc;
2525 * fcoe_ctlr_disc_recv - discovery receive handler for VN2VN mode.
2526 * @lport: The local port
2527 * @fp: The received frame
2529 * This should never be called since we don't see RSCNs or other
2530 * fabric-generated ELSes.
2532 static void fcoe_ctlr_disc_recv(struct fc_lport *lport, struct fc_frame *fp)
2534 struct fc_seq_els_data rjt_data;
2536 rjt_data.reason = ELS_RJT_UNSUP;
2537 rjt_data.explan = ELS_EXPL_NONE;
2538 lport->tt.seq_els_rsp_send(fp, ELS_LS_RJT, &rjt_data);
2539 fc_frame_free(fp);
2543 * fcoe_ctlr_disc_recv - start discovery for VN2VN mode.
2544 * @fip: The FCoE controller
2546 * This sets a flag indicating that remote ports should be created
2547 * and started for the peers we discover. We use the disc_callback
2548 * pointer as that flag. Peers already discovered are created here.
2550 * The lport lock is held during this call. The callback must be done
2551 * later, without holding either the lport or discovery locks.
2552 * The fcoe_ctlr lock may also be held during this call.
2554 static void fcoe_ctlr_disc_start(void (*callback)(struct fc_lport *,
2555 enum fc_disc_event),
2556 struct fc_lport *lport)
2558 struct fc_disc *disc = &lport->disc;
2559 struct fcoe_ctlr *fip = disc->priv;
2561 mutex_lock(&disc->disc_mutex);
2562 disc->disc_callback = callback;
2563 disc->disc_id = (disc->disc_id + 2) | 1;
2564 disc->pending = 1;
2565 schedule_work(&fip->timer_work);
2566 mutex_unlock(&disc->disc_mutex);
2570 * fcoe_ctlr_vn_disc() - report FIP VN_port discovery results after claim state.
2571 * @fip: The FCoE controller
2573 * Starts the FLOGI and PLOGI login process to each discovered rport for which
2574 * we've received at least one beacon.
2575 * Performs the discovery complete callback.
2577 static void fcoe_ctlr_vn_disc(struct fcoe_ctlr *fip)
2579 struct fc_lport *lport = fip->lp;
2580 struct fc_disc *disc = &lport->disc;
2581 struct fc_rport_priv *rdata;
2582 struct fcoe_rport *frport;
2583 void (*callback)(struct fc_lport *, enum fc_disc_event);
2585 mutex_lock(&disc->disc_mutex);
2586 callback = disc->pending ? disc->disc_callback : NULL;
2587 disc->pending = 0;
2588 list_for_each_entry_rcu(rdata, &disc->rports, peers) {
2589 frport = fcoe_ctlr_rport(rdata);
2590 if (frport->time)
2591 lport->tt.rport_login(rdata);
2593 mutex_unlock(&disc->disc_mutex);
2594 if (callback)
2595 callback(lport, DISC_EV_SUCCESS);
2599 * fcoe_ctlr_vn_timeout - timer work function for VN2VN mode.
2600 * @fip: The FCoE controller
2602 static void fcoe_ctlr_vn_timeout(struct fcoe_ctlr *fip)
2604 unsigned long next_time;
2605 u8 mac[ETH_ALEN];
2606 u32 new_port_id = 0;
2608 mutex_lock(&fip->ctlr_mutex);
2609 switch (fip->state) {
2610 case FIP_ST_VNMP_START:
2611 fcoe_ctlr_set_state(fip, FIP_ST_VNMP_PROBE1);
2612 fcoe_ctlr_vn_send(fip, FIP_SC_VN_PROBE_REQ, fcoe_all_vn2vn, 0);
2613 next_time = jiffies + msecs_to_jiffies(FIP_VN_PROBE_WAIT);
2614 break;
2615 case FIP_ST_VNMP_PROBE1:
2616 fcoe_ctlr_set_state(fip, FIP_ST_VNMP_PROBE2);
2617 fcoe_ctlr_vn_send(fip, FIP_SC_VN_PROBE_REQ, fcoe_all_vn2vn, 0);
2618 next_time = jiffies + msecs_to_jiffies(FIP_VN_ANN_WAIT);
2619 break;
2620 case FIP_ST_VNMP_PROBE2:
2621 fcoe_ctlr_set_state(fip, FIP_ST_VNMP_CLAIM);
2622 new_port_id = fip->port_id;
2623 hton24(mac, FIP_VN_FC_MAP);
2624 hton24(mac + 3, new_port_id);
2625 fcoe_ctlr_map_dest(fip);
2626 fip->update_mac(fip->lp, mac);
2627 fcoe_ctlr_vn_send_claim(fip);
2628 next_time = jiffies + msecs_to_jiffies(FIP_VN_ANN_WAIT);
2629 break;
2630 case FIP_ST_VNMP_CLAIM:
2632 * This may be invoked either by starting discovery so don't
2633 * go to the next state unless it's been long enough.
2635 next_time = fip->sol_time + msecs_to_jiffies(FIP_VN_ANN_WAIT);
2636 if (time_after_eq(jiffies, next_time)) {
2637 fcoe_ctlr_set_state(fip, FIP_ST_VNMP_UP);
2638 fcoe_ctlr_vn_send(fip, FIP_SC_VN_BEACON,
2639 fcoe_all_vn2vn, 0);
2640 next_time = jiffies + msecs_to_jiffies(FIP_VN_ANN_WAIT);
2641 fip->port_ka_time = next_time;
2643 fcoe_ctlr_vn_disc(fip);
2644 break;
2645 case FIP_ST_VNMP_UP:
2646 next_time = fcoe_ctlr_vn_age(fip);
2647 if (time_after_eq(jiffies, fip->port_ka_time)) {
2648 fcoe_ctlr_vn_send(fip, FIP_SC_VN_BEACON,
2649 fcoe_all_vn2vn, 0);
2650 fip->port_ka_time = jiffies +
2651 msecs_to_jiffies(FIP_VN_BEACON_INT +
2652 (random32() % FIP_VN_BEACON_FUZZ));
2654 if (time_before(fip->port_ka_time, next_time))
2655 next_time = fip->port_ka_time;
2656 break;
2657 case FIP_ST_LINK_WAIT:
2658 goto unlock;
2659 default:
2660 WARN(1, "unexpected state %d\n", fip->state);
2661 goto unlock;
2663 mod_timer(&fip->timer, next_time);
2664 unlock:
2665 mutex_unlock(&fip->ctlr_mutex);
2667 /* If port ID is new, notify local port after dropping ctlr_mutex */
2668 if (new_port_id)
2669 fc_lport_set_local_id(fip->lp, new_port_id);
2673 * fcoe_libfc_config() - Sets up libfc related properties for local port
2674 * @lp: The local port to configure libfc for
2675 * @fip: The FCoE controller in use by the local port
2676 * @tt: The libfc function template
2677 * @init_fcp: If non-zero, the FCP portion of libfc should be initialized
2679 * Returns : 0 for success
2681 int fcoe_libfc_config(struct fc_lport *lport, struct fcoe_ctlr *fip,
2682 const struct libfc_function_template *tt, int init_fcp)
2684 /* Set the function pointers set by the LLDD */
2685 memcpy(&lport->tt, tt, sizeof(*tt));
2686 if (init_fcp && fc_fcp_init(lport))
2687 return -ENOMEM;
2688 fc_exch_init(lport);
2689 fc_elsct_init(lport);
2690 fc_lport_init(lport);
2691 if (fip->mode == FIP_MODE_VN2VN)
2692 lport->rport_priv_size = sizeof(struct fcoe_rport);
2693 fc_rport_init(lport);
2694 if (fip->mode == FIP_MODE_VN2VN) {
2695 lport->point_to_multipoint = 1;
2696 lport->tt.disc_recv_req = fcoe_ctlr_disc_recv;
2697 lport->tt.disc_start = fcoe_ctlr_disc_start;
2698 lport->tt.disc_stop = fcoe_ctlr_disc_stop;
2699 lport->tt.disc_stop_final = fcoe_ctlr_disc_stop_final;
2700 mutex_init(&lport->disc.disc_mutex);
2701 INIT_LIST_HEAD(&lport->disc.rports);
2702 lport->disc.priv = fip;
2703 } else {
2704 fc_disc_init(lport);
2706 return 0;
2708 EXPORT_SYMBOL_GPL(fcoe_libfc_config);