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
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>
49 #define FCOE_CTLR_MIN_FKA 500 /* min keep alive (mS) */
50 #define FCOE_CTLR_DEF_FKA FIP_DEF_FKA /* default keep alive (mS) */
52 static void fcoe_ctlr_timeout(struct timer_list *);
53 static void fcoe_ctlr_timer_work(struct work_struct *);
54 static void fcoe_ctlr_recv_work(struct work_struct *);
55 static int fcoe_ctlr_flogi_retry(struct fcoe_ctlr *);
57 static void fcoe_ctlr_vn_start(struct fcoe_ctlr *);
58 static int fcoe_ctlr_vn_recv(struct fcoe_ctlr *, struct sk_buff *);
59 static void fcoe_ctlr_vn_timeout(struct fcoe_ctlr *);
60 static int fcoe_ctlr_vn_lookup(struct fcoe_ctlr *, u32, u8 *);
62 static int fcoe_ctlr_vlan_recv(struct fcoe_ctlr *, struct sk_buff *);
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 static const char * const fcoe_ctlr_states[] = {
70 [FIP_ST_DISABLED] = "DISABLED",
71 [FIP_ST_LINK_WAIT] = "LINK_WAIT",
72 [FIP_ST_AUTO] = "AUTO",
73 [FIP_ST_NON_FIP] = "NON_FIP",
74 [FIP_ST_ENABLED] = "ENABLED",
75 [FIP_ST_VNMP_START] = "VNMP_START",
76 [FIP_ST_VNMP_PROBE1] = "VNMP_PROBE1",
77 [FIP_ST_VNMP_PROBE2] = "VNMP_PROBE2",
78 [FIP_ST_VNMP_CLAIM] = "VNMP_CLAIM",
79 [FIP_ST_VNMP_UP] = "VNMP_UP",
82 static const char *fcoe_ctlr_state(enum fip_state state)
84 const char *cp = "unknown";
86 if (state < ARRAY_SIZE(fcoe_ctlr_states))
87 cp = fcoe_ctlr_states[state];
94 * fcoe_ctlr_set_state() - Set and do debug printing for the new FIP state.
95 * @fip: The FCoE controller
96 * @state: The new state
98 static void fcoe_ctlr_set_state(struct fcoe_ctlr *fip, enum fip_state state)
100 if (state == fip->state)
103 LIBFCOE_FIP_DBG(fip, "state %s -> %s\n",
104 fcoe_ctlr_state(fip->state), fcoe_ctlr_state(state));
109 * fcoe_ctlr_mtu_valid() - Check if a FCF's MTU is valid
110 * @fcf: The FCF to check
112 * Return non-zero if FCF fcoe_size has been validated.
114 static inline int fcoe_ctlr_mtu_valid(const struct fcoe_fcf *fcf)
116 return (fcf->flags & FIP_FL_SOL) != 0;
120 * fcoe_ctlr_fcf_usable() - Check if a FCF is usable
121 * @fcf: The FCF to check
123 * Return non-zero if the FCF is usable.
125 static inline int fcoe_ctlr_fcf_usable(struct fcoe_fcf *fcf)
127 u16 flags = FIP_FL_SOL | FIP_FL_AVAIL;
129 return (fcf->flags & flags) == flags;
133 * fcoe_ctlr_map_dest() - Set flag and OUI for mapping destination addresses
134 * @fip: The FCoE controller
136 static void fcoe_ctlr_map_dest(struct fcoe_ctlr *fip)
138 if (fip->mode == FIP_MODE_VN2VN)
139 hton24(fip->dest_addr, FIP_VN_FC_MAP);
141 hton24(fip->dest_addr, FIP_DEF_FC_MAP);
142 hton24(fip->dest_addr + 3, 0);
147 * fcoe_ctlr_init() - Initialize the FCoE Controller instance
148 * @fip: The FCoE controller to initialize
150 void fcoe_ctlr_init(struct fcoe_ctlr *fip, enum fip_mode mode)
152 fcoe_ctlr_set_state(fip, FIP_ST_LINK_WAIT);
154 fip->fip_resp = false;
155 INIT_LIST_HEAD(&fip->fcfs);
156 mutex_init(&fip->ctlr_mutex);
157 spin_lock_init(&fip->ctlr_lock);
158 fip->flogi_oxid = FC_XID_UNKNOWN;
159 timer_setup(&fip->timer, fcoe_ctlr_timeout, 0);
160 INIT_WORK(&fip->timer_work, fcoe_ctlr_timer_work);
161 INIT_WORK(&fip->recv_work, fcoe_ctlr_recv_work);
162 skb_queue_head_init(&fip->fip_recv_list);
164 EXPORT_SYMBOL(fcoe_ctlr_init);
167 * fcoe_sysfs_fcf_add() - Add a fcoe_fcf{,_device} to a fcoe_ctlr{,_device}
168 * @new: The newly discovered FCF
170 * Called with fip->ctlr_mutex held
172 static int fcoe_sysfs_fcf_add(struct fcoe_fcf *new)
174 struct fcoe_ctlr *fip = new->fip;
175 struct fcoe_ctlr_device *ctlr_dev;
176 struct fcoe_fcf_device *temp, *fcf_dev;
179 LIBFCOE_FIP_DBG(fip, "New FCF fab %16.16llx mac %pM\n",
180 new->fabric_name, new->fcf_mac);
182 temp = kzalloc(sizeof(*temp), GFP_KERNEL);
186 temp->fabric_name = new->fabric_name;
187 temp->switch_name = new->switch_name;
188 temp->fc_map = new->fc_map;
189 temp->vfid = new->vfid;
190 memcpy(temp->mac, new->fcf_mac, ETH_ALEN);
191 temp->priority = new->pri;
192 temp->fka_period = new->fka_period;
193 temp->selected = 0; /* default to unselected */
196 * If ctlr_dev doesn't exist then it means we're a libfcoe user
197 * who doesn't use fcoe_syfs and didn't allocate a fcoe_ctlr_device.
198 * fnic would be an example of a driver with this behavior. In this
199 * case we want to add the fcoe_fcf to the fcoe_ctlr list, but we
200 * don't want to make sysfs changes.
203 ctlr_dev = fcoe_ctlr_to_ctlr_dev(fip);
205 mutex_lock(&ctlr_dev->lock);
206 fcf_dev = fcoe_fcf_device_add(ctlr_dev, temp);
207 if (unlikely(!fcf_dev)) {
209 mutex_unlock(&ctlr_dev->lock);
214 * The fcoe_sysfs layer can return a CONNECTED fcf that
215 * has a priv (fcf was never deleted) or a CONNECTED fcf
216 * that doesn't have a priv (fcf was deleted). However,
217 * libfcoe will always delete FCFs before trying to add
218 * them. This is ensured because both recv_adv and
219 * age_fcfs are protected by the the fcoe_ctlr's mutex.
220 * This means that we should never get a FCF with a
221 * non-NULL priv pointer.
223 BUG_ON(fcf_dev->priv);
226 new->fcf_dev = fcf_dev;
227 mutex_unlock(&ctlr_dev->lock);
230 list_add(&new->list, &fip->fcfs);
240 * fcoe_sysfs_fcf_del() - Remove a fcoe_fcf{,_device} to a fcoe_ctlr{,_device}
241 * @new: The FCF to be removed
243 * Called with fip->ctlr_mutex held
245 static void fcoe_sysfs_fcf_del(struct fcoe_fcf *new)
247 struct fcoe_ctlr *fip = new->fip;
248 struct fcoe_ctlr_device *cdev;
249 struct fcoe_fcf_device *fcf_dev;
251 list_del(&new->list);
255 * If ctlr_dev doesn't exist then it means we're a libfcoe user
256 * who doesn't use fcoe_syfs and didn't allocate a fcoe_ctlr_device
257 * or a fcoe_fcf_device.
259 * fnic would be an example of a driver with this behavior. In this
260 * case we want to remove the fcoe_fcf from the fcoe_ctlr list (above),
261 * but we don't want to make sysfs changes.
263 cdev = fcoe_ctlr_to_ctlr_dev(fip);
265 mutex_lock(&cdev->lock);
266 fcf_dev = fcoe_fcf_to_fcf_dev(new);
269 fcoe_fcf_device_delete(fcf_dev);
270 mutex_unlock(&cdev->lock);
276 * fcoe_ctlr_reset_fcfs() - Reset and free all FCFs for a controller
277 * @fip: The FCoE controller whose FCFs are to be reset
279 * Called with &fcoe_ctlr lock held.
281 static void fcoe_ctlr_reset_fcfs(struct fcoe_ctlr *fip)
283 struct fcoe_fcf *fcf;
284 struct fcoe_fcf *next;
287 list_for_each_entry_safe(fcf, next, &fip->fcfs, list) {
288 fcoe_sysfs_fcf_del(fcf);
290 WARN_ON(fip->fcf_count);
296 * fcoe_ctlr_destroy() - Disable and tear down a FCoE controller
297 * @fip: The FCoE controller to tear down
299 * This is called by FCoE drivers before freeing the &fcoe_ctlr.
301 * The receive handler will have been deleted before this to guarantee
302 * that no more recv_work will be scheduled.
304 * The timer routine will simply return once we set FIP_ST_DISABLED.
305 * This guarantees that no further timeouts or work will be scheduled.
307 void fcoe_ctlr_destroy(struct fcoe_ctlr *fip)
309 cancel_work_sync(&fip->recv_work);
310 skb_queue_purge(&fip->fip_recv_list);
312 mutex_lock(&fip->ctlr_mutex);
313 fcoe_ctlr_set_state(fip, FIP_ST_DISABLED);
314 fcoe_ctlr_reset_fcfs(fip);
315 mutex_unlock(&fip->ctlr_mutex);
316 del_timer_sync(&fip->timer);
317 cancel_work_sync(&fip->timer_work);
319 EXPORT_SYMBOL(fcoe_ctlr_destroy);
322 * fcoe_ctlr_announce() - announce new FCF selection
323 * @fip: The FCoE controller
325 * Also sets the destination MAC for FCoE and control packets
327 * Called with neither ctlr_mutex nor ctlr_lock held.
329 static void fcoe_ctlr_announce(struct fcoe_ctlr *fip)
331 struct fcoe_fcf *sel;
332 struct fcoe_fcf *fcf;
335 mutex_lock(&fip->ctlr_mutex);
336 spin_lock_irqsave(&fip->ctlr_lock, flags);
338 kfree_skb(fip->flogi_req);
339 fip->flogi_req = NULL;
340 list_for_each_entry(fcf, &fip->fcfs, list)
343 spin_unlock_irqrestore(&fip->ctlr_lock, flags);
346 if (sel && ether_addr_equal(sel->fcf_mac, fip->dest_addr))
348 if (!is_zero_ether_addr(fip->dest_addr)) {
349 printk(KERN_NOTICE "libfcoe: host%d: "
350 "FIP Fibre-Channel Forwarder MAC %pM deselected\n",
351 fip->lp->host->host_no, fip->dest_addr);
352 memset(fip->dest_addr, 0, ETH_ALEN);
355 printk(KERN_INFO "libfcoe: host%d: FIP selected "
356 "Fibre-Channel Forwarder MAC %pM\n",
357 fip->lp->host->host_no, sel->fcf_mac);
358 memcpy(fip->dest_addr, sel->fcoe_mac, ETH_ALEN);
362 mutex_unlock(&fip->ctlr_mutex);
366 * fcoe_ctlr_fcoe_size() - Return the maximum FCoE size required for VN_Port
367 * @fip: The FCoE controller to get the maximum FCoE size from
369 * Returns the maximum packet size including the FCoE header and trailer,
370 * but not including any Ethernet or VLAN headers.
372 static inline u32 fcoe_ctlr_fcoe_size(struct fcoe_ctlr *fip)
375 * Determine the max FCoE frame size allowed, including
376 * FCoE header and trailer.
377 * Note: lp->mfs is currently the payload size, not the frame size.
379 return fip->lp->mfs + sizeof(struct fc_frame_header) +
380 sizeof(struct fcoe_hdr) + sizeof(struct fcoe_crc_eof);
384 * fcoe_ctlr_solicit() - Send a FIP solicitation
385 * @fip: The FCoE controller to send the solicitation on
386 * @fcf: The destination FCF (if NULL, a multicast solicitation is sent)
388 static void fcoe_ctlr_solicit(struct fcoe_ctlr *fip, struct fcoe_fcf *fcf)
393 struct fip_header fip;
395 struct fip_mac_desc mac;
396 struct fip_wwn_desc wwnn;
397 struct fip_size_desc size;
402 skb = dev_alloc_skb(sizeof(*sol));
406 sol = (struct fip_sol *)skb->data;
408 memset(sol, 0, sizeof(*sol));
409 memcpy(sol->eth.h_dest, fcf ? fcf->fcf_mac : fcoe_all_fcfs, ETH_ALEN);
410 memcpy(sol->eth.h_source, fip->ctl_src_addr, ETH_ALEN);
411 sol->eth.h_proto = htons(ETH_P_FIP);
413 sol->fip.fip_ver = FIP_VER_ENCAPS(FIP_VER);
414 sol->fip.fip_op = htons(FIP_OP_DISC);
415 sol->fip.fip_subcode = FIP_SC_SOL;
416 sol->fip.fip_dl_len = htons(sizeof(sol->desc) / FIP_BPW);
417 sol->fip.fip_flags = htons(FIP_FL_FPMA);
419 sol->fip.fip_flags |= htons(FIP_FL_SPMA);
421 sol->desc.mac.fd_desc.fip_dtype = FIP_DT_MAC;
422 sol->desc.mac.fd_desc.fip_dlen = sizeof(sol->desc.mac) / FIP_BPW;
423 memcpy(sol->desc.mac.fd_mac, fip->ctl_src_addr, ETH_ALEN);
425 sol->desc.wwnn.fd_desc.fip_dtype = FIP_DT_NAME;
426 sol->desc.wwnn.fd_desc.fip_dlen = sizeof(sol->desc.wwnn) / FIP_BPW;
427 put_unaligned_be64(fip->lp->wwnn, &sol->desc.wwnn.fd_wwn);
429 fcoe_size = fcoe_ctlr_fcoe_size(fip);
430 sol->desc.size.fd_desc.fip_dtype = FIP_DT_FCOE_SIZE;
431 sol->desc.size.fd_desc.fip_dlen = sizeof(sol->desc.size) / FIP_BPW;
432 sol->desc.size.fd_size = htons(fcoe_size);
434 skb_put(skb, sizeof(*sol));
435 skb->protocol = htons(ETH_P_FIP);
436 skb->priority = fip->priority;
437 skb_reset_mac_header(skb);
438 skb_reset_network_header(skb);
442 fip->sol_time = jiffies;
446 * fcoe_ctlr_link_up() - Start FCoE controller
447 * @fip: The FCoE controller to start
449 * Called from the LLD when the network link is ready.
451 void fcoe_ctlr_link_up(struct fcoe_ctlr *fip)
453 mutex_lock(&fip->ctlr_mutex);
454 if (fip->state == FIP_ST_NON_FIP || fip->state == FIP_ST_AUTO) {
455 mutex_unlock(&fip->ctlr_mutex);
457 } else if (fip->state == FIP_ST_LINK_WAIT) {
458 if (fip->mode == FIP_MODE_NON_FIP)
459 fcoe_ctlr_set_state(fip, FIP_ST_NON_FIP);
461 fcoe_ctlr_set_state(fip, FIP_ST_AUTO);
464 LIBFCOE_FIP_DBG(fip, "invalid mode %d\n", fip->mode);
467 LIBFCOE_FIP_DBG(fip, "%s", "setting AUTO mode.\n");
469 case FIP_MODE_FABRIC:
470 case FIP_MODE_NON_FIP:
471 mutex_unlock(&fip->ctlr_mutex);
473 fcoe_ctlr_solicit(fip, NULL);
476 fcoe_ctlr_vn_start(fip);
477 mutex_unlock(&fip->ctlr_mutex);
482 mutex_unlock(&fip->ctlr_mutex);
484 EXPORT_SYMBOL(fcoe_ctlr_link_up);
487 * fcoe_ctlr_reset() - Reset a FCoE controller
488 * @fip: The FCoE controller to reset
490 static void fcoe_ctlr_reset(struct fcoe_ctlr *fip)
492 fcoe_ctlr_reset_fcfs(fip);
493 del_timer(&fip->timer);
494 fip->ctlr_ka_time = 0;
495 fip->port_ka_time = 0;
497 fip->flogi_oxid = FC_XID_UNKNOWN;
498 fcoe_ctlr_map_dest(fip);
502 * fcoe_ctlr_link_down() - Stop a FCoE controller
503 * @fip: The FCoE controller to be stopped
505 * Returns non-zero if the link was up and now isn't.
507 * Called from the LLD when the network link is not ready.
508 * There may be multiple calls while the link is down.
510 int fcoe_ctlr_link_down(struct fcoe_ctlr *fip)
514 LIBFCOE_FIP_DBG(fip, "link down.\n");
515 mutex_lock(&fip->ctlr_mutex);
516 fcoe_ctlr_reset(fip);
517 link_dropped = fip->state != FIP_ST_LINK_WAIT;
518 fcoe_ctlr_set_state(fip, FIP_ST_LINK_WAIT);
519 mutex_unlock(&fip->ctlr_mutex);
522 fc_linkdown(fip->lp);
525 EXPORT_SYMBOL(fcoe_ctlr_link_down);
528 * fcoe_ctlr_send_keep_alive() - Send a keep-alive to the selected FCF
529 * @fip: The FCoE controller to send the FKA on
530 * @lport: libfc fc_lport to send from
531 * @ports: 0 for controller keep-alive, 1 for port keep-alive
532 * @sa: The source MAC address
534 * A controller keep-alive is sent every fka_period (typically 8 seconds).
535 * The source MAC is the native MAC address.
537 * A port keep-alive is sent every 90 seconds while logged in.
538 * The source MAC is the assigned mapped source address.
539 * The destination is the FCF's F-port.
541 static void fcoe_ctlr_send_keep_alive(struct fcoe_ctlr *fip,
542 struct fc_lport *lport,
548 struct fip_header fip;
549 struct fip_mac_desc mac;
551 struct fip_vn_desc *vn;
554 struct fcoe_fcf *fcf;
558 if (!fcf || (ports && !lp->port_id))
561 len = sizeof(*kal) + ports * sizeof(*vn);
562 skb = dev_alloc_skb(len);
566 kal = (struct fip_kal *)skb->data;
568 memcpy(kal->eth.h_dest, fcf->fcf_mac, ETH_ALEN);
569 memcpy(kal->eth.h_source, sa, ETH_ALEN);
570 kal->eth.h_proto = htons(ETH_P_FIP);
572 kal->fip.fip_ver = FIP_VER_ENCAPS(FIP_VER);
573 kal->fip.fip_op = htons(FIP_OP_CTRL);
574 kal->fip.fip_subcode = FIP_SC_KEEP_ALIVE;
575 kal->fip.fip_dl_len = htons((sizeof(kal->mac) +
576 ports * sizeof(*vn)) / FIP_BPW);
577 kal->fip.fip_flags = htons(FIP_FL_FPMA);
579 kal->fip.fip_flags |= htons(FIP_FL_SPMA);
581 kal->mac.fd_desc.fip_dtype = FIP_DT_MAC;
582 kal->mac.fd_desc.fip_dlen = sizeof(kal->mac) / FIP_BPW;
583 memcpy(kal->mac.fd_mac, fip->ctl_src_addr, ETH_ALEN);
585 vn = (struct fip_vn_desc *)(kal + 1);
586 vn->fd_desc.fip_dtype = FIP_DT_VN_ID;
587 vn->fd_desc.fip_dlen = sizeof(*vn) / FIP_BPW;
588 memcpy(vn->fd_mac, fip->get_src_addr(lport), ETH_ALEN);
589 hton24(vn->fd_fc_id, lport->port_id);
590 put_unaligned_be64(lport->wwpn, &vn->fd_wwpn);
593 skb->protocol = htons(ETH_P_FIP);
594 skb->priority = fip->priority;
595 skb_reset_mac_header(skb);
596 skb_reset_network_header(skb);
601 * fcoe_ctlr_encaps() - Encapsulate an ELS frame for FIP, without sending it
602 * @fip: The FCoE controller for the ELS frame
603 * @dtype: The FIP descriptor type for the frame
604 * @skb: The FCoE ELS frame including FC header but no FCoE headers
605 * @d_id: The destination port ID.
607 * Returns non-zero error code on failure.
609 * The caller must check that the length is a multiple of 4.
611 * The @skb must have enough headroom (28 bytes) and tailroom (8 bytes).
612 * Headroom includes the FIP encapsulation description, FIP header, and
613 * Ethernet header. The tailroom is for the FIP MAC descriptor.
615 static int fcoe_ctlr_encaps(struct fcoe_ctlr *fip, struct fc_lport *lport,
616 u8 dtype, struct sk_buff *skb, u32 d_id)
618 struct fip_encaps_head {
620 struct fip_header fip;
621 struct fip_encaps encaps;
623 struct fc_frame_header *fh;
624 struct fip_mac_desc *mac;
625 struct fcoe_fcf *fcf;
630 fh = (struct fc_frame_header *)skb->data;
631 op = *(u8 *)(fh + 1);
632 dlen = sizeof(struct fip_encaps) + skb->len; /* len before push */
633 cap = skb_push(skb, sizeof(*cap));
634 memset(cap, 0, sizeof(*cap));
636 if (lport->point_to_multipoint) {
637 if (fcoe_ctlr_vn_lookup(fip, d_id, cap->eth.h_dest))
644 fip_flags = fcf->flags;
645 fip_flags &= fip->spma ? FIP_FL_SPMA | FIP_FL_FPMA :
649 memcpy(cap->eth.h_dest, fcf->fcf_mac, ETH_ALEN);
651 memcpy(cap->eth.h_source, fip->ctl_src_addr, ETH_ALEN);
652 cap->eth.h_proto = htons(ETH_P_FIP);
654 cap->fip.fip_ver = FIP_VER_ENCAPS(FIP_VER);
655 cap->fip.fip_op = htons(FIP_OP_LS);
656 if (op == ELS_LS_ACC || op == ELS_LS_RJT)
657 cap->fip.fip_subcode = FIP_SC_REP;
659 cap->fip.fip_subcode = FIP_SC_REQ;
660 cap->fip.fip_flags = htons(fip_flags);
662 cap->encaps.fd_desc.fip_dtype = dtype;
663 cap->encaps.fd_desc.fip_dlen = dlen / FIP_BPW;
665 if (op != ELS_LS_RJT) {
666 dlen += sizeof(*mac);
667 mac = skb_put_zero(skb, sizeof(*mac));
668 mac->fd_desc.fip_dtype = FIP_DT_MAC;
669 mac->fd_desc.fip_dlen = sizeof(*mac) / FIP_BPW;
670 if (dtype != FIP_DT_FLOGI && dtype != FIP_DT_FDISC) {
671 memcpy(mac->fd_mac, fip->get_src_addr(lport), ETH_ALEN);
672 } else if (fip->mode == FIP_MODE_VN2VN) {
673 hton24(mac->fd_mac, FIP_VN_FC_MAP);
674 hton24(mac->fd_mac + 3, fip->port_id);
675 } else if (fip_flags & FIP_FL_SPMA) {
676 LIBFCOE_FIP_DBG(fip, "FLOGI/FDISC sent with SPMA\n");
677 memcpy(mac->fd_mac, fip->ctl_src_addr, ETH_ALEN);
679 LIBFCOE_FIP_DBG(fip, "FLOGI/FDISC sent with FPMA\n");
680 /* FPMA only FLOGI. Must leave the MAC desc zeroed. */
683 cap->fip.fip_dl_len = htons(dlen / FIP_BPW);
685 skb->protocol = htons(ETH_P_FIP);
686 skb->priority = fip->priority;
687 skb_reset_mac_header(skb);
688 skb_reset_network_header(skb);
693 * fcoe_ctlr_els_send() - Send an ELS frame encapsulated by FIP if appropriate.
694 * @fip: FCoE controller.
695 * @lport: libfc fc_lport to send from
696 * @skb: FCoE ELS frame including FC header but no FCoE headers.
698 * Returns a non-zero error code if the frame should not be sent.
699 * Returns zero if the caller should send the frame with FCoE encapsulation.
701 * The caller must check that the length is a multiple of 4.
702 * The SKB must have enough headroom (28 bytes) and tailroom (8 bytes).
703 * The the skb must also be an fc_frame.
705 * This is called from the lower-level driver with spinlocks held,
706 * so we must not take a mutex here.
708 int fcoe_ctlr_els_send(struct fcoe_ctlr *fip, struct fc_lport *lport,
712 struct fc_frame_header *fh;
718 fp = container_of(skb, struct fc_frame, skb);
719 fh = (struct fc_frame_header *)skb->data;
720 op = *(u8 *)(fh + 1);
722 if (op == ELS_FLOGI && fip->mode != FIP_MODE_VN2VN) {
723 old_xid = fip->flogi_oxid;
724 fip->flogi_oxid = ntohs(fh->fh_ox_id);
725 if (fip->state == FIP_ST_AUTO) {
726 if (old_xid == FC_XID_UNKNOWN)
727 fip->flogi_count = 0;
729 if (fip->flogi_count < 3)
731 fcoe_ctlr_map_dest(fip);
734 if (fip->state == FIP_ST_NON_FIP)
735 fcoe_ctlr_map_dest(fip);
738 if (fip->state == FIP_ST_NON_FIP)
740 if (!fip->sel_fcf && fip->mode != FIP_MODE_VN2VN)
745 if (fip->mode == FIP_MODE_VN2VN)
747 spin_lock_irqsave(&fip->ctlr_lock, flags);
748 kfree_skb(fip->flogi_req);
749 fip->flogi_req = skb;
750 fip->flogi_req_send = 1;
751 spin_unlock_irqrestore(&fip->ctlr_lock, flags);
752 schedule_work(&fip->timer_work);
755 if (ntoh24(fh->fh_s_id))
760 if (fip->mode == FIP_MODE_VN2VN) {
761 if (fip->state != FIP_ST_VNMP_UP)
763 if (ntoh24(fh->fh_d_id) == FC_FID_FLOGI)
766 if (fip->state != FIP_ST_ENABLED)
768 if (ntoh24(fh->fh_d_id) != FC_FID_FLOGI)
775 * If non-FIP, we may have gotten an SID by accepting an FLOGI
776 * from a point-to-point connection. Switch to using
777 * the source mac based on the SID. The destination
778 * MAC in this case would have been set by receiving the
781 if (fip->state == FIP_ST_NON_FIP) {
782 if (fip->flogi_oxid == FC_XID_UNKNOWN)
784 fip->flogi_oxid = FC_XID_UNKNOWN;
785 fc_fcoe_set_mac(mac, fh->fh_d_id);
786 fip->update_mac(lport, mac);
795 if (fip->state != FIP_ST_ENABLED &&
796 fip->state != FIP_ST_VNMP_UP)
800 LIBFCOE_FIP_DBG(fip, "els_send op %u d_id %x\n",
801 op, ntoh24(fh->fh_d_id));
802 if (fcoe_ctlr_encaps(fip, lport, op, skb, ntoh24(fh->fh_d_id)))
807 LIBFCOE_FIP_DBG(fip, "drop els_send op %u d_id %x\n",
808 op, ntoh24(fh->fh_d_id));
812 EXPORT_SYMBOL(fcoe_ctlr_els_send);
815 * fcoe_ctlr_age_fcfs() - Reset and free all old FCFs for a controller
816 * @fip: The FCoE controller to free FCFs on
818 * Called with lock held and preemption disabled.
820 * An FCF is considered old if we have missed two advertisements.
821 * That is, there have been no valid advertisement from it for 2.5
822 * times its keep-alive period.
824 * In addition, determine the time when an FCF selection can occur.
826 * Also, increment the MissDiscAdvCount when no advertisement is received
827 * for the corresponding FCF for 1.5 * FKA_ADV_PERIOD (FC-BB-5 LESB).
829 * Returns the time in jiffies for the next call.
831 static unsigned long fcoe_ctlr_age_fcfs(struct fcoe_ctlr *fip)
833 struct fcoe_fcf *fcf;
834 struct fcoe_fcf *next;
835 unsigned long next_timer = jiffies + msecs_to_jiffies(FIP_VN_KA_PERIOD);
836 unsigned long deadline;
837 unsigned long sel_time = 0;
838 struct list_head del_list;
839 struct fc_stats *stats;
841 INIT_LIST_HEAD(&del_list);
843 stats = per_cpu_ptr(fip->lp->stats, get_cpu());
845 list_for_each_entry_safe(fcf, next, &fip->fcfs, list) {
846 deadline = fcf->time + fcf->fka_period + fcf->fka_period / 2;
847 if (fip->sel_fcf == fcf) {
848 if (time_after(jiffies, deadline)) {
849 stats->MissDiscAdvCount++;
850 printk(KERN_INFO "libfcoe: host%d: "
851 "Missing Discovery Advertisement "
852 "for fab %16.16llx count %lld\n",
853 fip->lp->host->host_no, fcf->fabric_name,
854 stats->MissDiscAdvCount);
855 } else if (time_after(next_timer, deadline))
856 next_timer = deadline;
859 deadline += fcf->fka_period;
860 if (time_after_eq(jiffies, deadline)) {
861 if (fip->sel_fcf == fcf)
864 * Move to delete list so we can call
865 * fcoe_sysfs_fcf_del (which can sleep)
866 * after the put_cpu().
868 list_del(&fcf->list);
869 list_add(&fcf->list, &del_list);
870 stats->VLinkFailureCount++;
872 if (time_after(next_timer, deadline))
873 next_timer = deadline;
874 if (fcoe_ctlr_mtu_valid(fcf) &&
875 (!sel_time || time_before(sel_time, fcf->time)))
876 sel_time = fcf->time;
881 list_for_each_entry_safe(fcf, next, &del_list, list) {
882 /* Removes fcf from current list */
883 fcoe_sysfs_fcf_del(fcf);
886 if (sel_time && !fip->sel_fcf && !fip->sel_time) {
887 sel_time += msecs_to_jiffies(FCOE_CTLR_START_DELAY);
888 fip->sel_time = sel_time;
895 * fcoe_ctlr_parse_adv() - Decode a FIP advertisement into a new FCF entry
896 * @fip: The FCoE controller receiving the advertisement
897 * @skb: The received FIP advertisement frame
898 * @fcf: The resulting FCF entry
900 * Returns zero on a valid parsed advertisement,
901 * otherwise returns non zero value.
903 static int fcoe_ctlr_parse_adv(struct fcoe_ctlr *fip,
904 struct sk_buff *skb, struct fcoe_fcf *fcf)
906 struct fip_header *fiph;
907 struct fip_desc *desc = NULL;
908 struct fip_wwn_desc *wwn;
909 struct fip_fab_desc *fab;
910 struct fip_fka_desc *fka;
916 memset(fcf, 0, sizeof(*fcf));
917 fcf->fka_period = msecs_to_jiffies(FCOE_CTLR_DEF_FKA);
919 fiph = (struct fip_header *)skb->data;
920 fcf->flags = ntohs(fiph->fip_flags);
923 * mask of required descriptors. validating each one clears its bit.
925 desc_mask = BIT(FIP_DT_PRI) | BIT(FIP_DT_MAC) | BIT(FIP_DT_NAME) |
926 BIT(FIP_DT_FAB) | BIT(FIP_DT_FKA);
928 rlen = ntohs(fiph->fip_dl_len) * 4;
929 if (rlen + sizeof(*fiph) > skb->len)
932 desc = (struct fip_desc *)(fiph + 1);
934 dlen = desc->fip_dlen * FIP_BPW;
935 if (dlen < sizeof(*desc) || dlen > rlen)
937 /* Drop Adv if there are duplicate critical descriptors */
938 if ((desc->fip_dtype < 32) &&
939 !(desc_mask & 1U << desc->fip_dtype)) {
940 LIBFCOE_FIP_DBG(fip, "Duplicate Critical "
941 "Descriptors in FIP adv\n");
944 switch (desc->fip_dtype) {
946 if (dlen != sizeof(struct fip_pri_desc))
948 fcf->pri = ((struct fip_pri_desc *)desc)->fd_pri;
949 desc_mask &= ~BIT(FIP_DT_PRI);
952 if (dlen != sizeof(struct fip_mac_desc))
955 ((struct fip_mac_desc *)desc)->fd_mac,
957 memcpy(fcf->fcoe_mac, fcf->fcf_mac, ETH_ALEN);
958 if (!is_valid_ether_addr(fcf->fcf_mac)) {
960 "Invalid MAC addr %pM in FIP adv\n",
964 desc_mask &= ~BIT(FIP_DT_MAC);
967 if (dlen != sizeof(struct fip_wwn_desc))
969 wwn = (struct fip_wwn_desc *)desc;
970 fcf->switch_name = get_unaligned_be64(&wwn->fd_wwn);
971 desc_mask &= ~BIT(FIP_DT_NAME);
974 if (dlen != sizeof(struct fip_fab_desc))
976 fab = (struct fip_fab_desc *)desc;
977 fcf->fabric_name = get_unaligned_be64(&fab->fd_wwn);
978 fcf->vfid = ntohs(fab->fd_vfid);
979 fcf->fc_map = ntoh24(fab->fd_map);
980 desc_mask &= ~BIT(FIP_DT_FAB);
983 if (dlen != sizeof(struct fip_fka_desc))
985 fka = (struct fip_fka_desc *)desc;
986 if (fka->fd_flags & FIP_FKA_ADV_D)
988 t = ntohl(fka->fd_fka_period);
989 if (t >= FCOE_CTLR_MIN_FKA)
990 fcf->fka_period = msecs_to_jiffies(t);
991 desc_mask &= ~BIT(FIP_DT_FKA);
994 case FIP_DT_FCOE_SIZE:
1000 LIBFCOE_FIP_DBG(fip, "unexpected descriptor type %x "
1001 "in FIP adv\n", desc->fip_dtype);
1002 /* standard says ignore unknown descriptors >= 128 */
1003 if (desc->fip_dtype < FIP_DT_NON_CRITICAL)
1007 desc = (struct fip_desc *)((char *)desc + dlen);
1010 if (!fcf->fc_map || (fcf->fc_map & 0x10000))
1012 if (!fcf->switch_name)
1015 LIBFCOE_FIP_DBG(fip, "adv missing descriptors mask %x\n",
1022 LIBFCOE_FIP_DBG(fip, "FIP length error in descriptor type %x len %zu\n",
1023 desc->fip_dtype, dlen);
1028 * fcoe_ctlr_recv_adv() - Handle an incoming advertisement
1029 * @fip: The FCoE controller receiving the advertisement
1030 * @skb: The received FIP packet
1032 static void fcoe_ctlr_recv_adv(struct fcoe_ctlr *fip, struct sk_buff *skb)
1034 struct fcoe_fcf *fcf;
1035 struct fcoe_fcf new;
1036 unsigned long sol_tov = msecs_to_jiffies(FCOE_CTRL_SOL_TOV);
1042 if (fcoe_ctlr_parse_adv(fip, skb, &new))
1045 mutex_lock(&fip->ctlr_mutex);
1046 first = list_empty(&fip->fcfs);
1047 list_for_each_entry(fcf, &fip->fcfs, list) {
1048 if (fcf->switch_name == new.switch_name &&
1049 fcf->fabric_name == new.fabric_name &&
1050 fcf->fc_map == new.fc_map &&
1051 ether_addr_equal(fcf->fcf_mac, new.fcf_mac)) {
1057 if (fip->fcf_count >= FCOE_CTLR_FCF_LIMIT)
1060 fcf = kmalloc(sizeof(*fcf), GFP_ATOMIC);
1064 memcpy(fcf, &new, sizeof(new));
1066 rc = fcoe_sysfs_fcf_add(fcf);
1068 printk(KERN_ERR "Failed to allocate sysfs instance "
1069 "for FCF, fab %16.16llx mac %pM\n",
1070 new.fabric_name, new.fcf_mac);
1076 * Update the FCF's keep-alive descriptor flags.
1077 * Other flag changes from new advertisements are
1078 * ignored after a solicited advertisement is
1079 * received and the FCF is selectable (usable).
1081 fcf->fd_flags = new.fd_flags;
1082 if (!fcoe_ctlr_fcf_usable(fcf))
1083 fcf->flags = new.flags;
1085 if (fcf == fip->sel_fcf && !fcf->fd_flags) {
1086 fip->ctlr_ka_time -= fcf->fka_period;
1087 fip->ctlr_ka_time += new.fka_period;
1088 if (time_before(fip->ctlr_ka_time, fip->timer.expires))
1089 mod_timer(&fip->timer, fip->ctlr_ka_time);
1091 fcf->fka_period = new.fka_period;
1092 memcpy(fcf->fcf_mac, new.fcf_mac, ETH_ALEN);
1095 mtu_valid = fcoe_ctlr_mtu_valid(fcf);
1096 fcf->time = jiffies;
1098 LIBFCOE_FIP_DBG(fip, "New FCF fab %16.16llx mac %pM\n",
1099 fcf->fabric_name, fcf->fcf_mac);
1102 * If this advertisement is not solicited and our max receive size
1103 * hasn't been verified, send a solicited advertisement.
1106 fcoe_ctlr_solicit(fip, fcf);
1109 * If its been a while since we did a solicit, and this is
1110 * the first advertisement we've received, do a multicast
1111 * solicitation to gather as many advertisements as we can
1112 * before selection occurs.
1114 if (first && time_after(jiffies, fip->sol_time + sol_tov))
1115 fcoe_ctlr_solicit(fip, NULL);
1118 * Put this FCF at the head of the list for priority among equals.
1119 * This helps in the case of an NPV switch which insists we use
1120 * the FCF that answers multicast solicitations, not the others that
1121 * are sending periodic multicast advertisements.
1124 list_move(&fcf->list, &fip->fcfs);
1127 * If this is the first validated FCF, note the time and
1128 * set a timer to trigger selection.
1130 if (mtu_valid && !fip->sel_fcf && !fip->sel_time &&
1131 fcoe_ctlr_fcf_usable(fcf)) {
1132 fip->sel_time = jiffies +
1133 msecs_to_jiffies(FCOE_CTLR_START_DELAY);
1134 if (!timer_pending(&fip->timer) ||
1135 time_before(fip->sel_time, fip->timer.expires))
1136 mod_timer(&fip->timer, fip->sel_time);
1140 mutex_unlock(&fip->ctlr_mutex);
1144 * fcoe_ctlr_recv_els() - Handle an incoming FIP encapsulated ELS frame
1145 * @fip: The FCoE controller which received the packet
1146 * @skb: The received FIP packet
1148 static void fcoe_ctlr_recv_els(struct fcoe_ctlr *fip, struct sk_buff *skb)
1150 struct fc_lport *lport = fip->lp;
1151 struct fip_header *fiph;
1152 struct fc_frame *fp = (struct fc_frame *)skb;
1153 struct fc_frame_header *fh = NULL;
1154 struct fip_desc *desc;
1155 struct fip_encaps *els;
1156 struct fcoe_fcf *sel;
1157 struct fc_stats *stats;
1158 enum fip_desc_type els_dtype = 0;
1161 u8 granted_mac[ETH_ALEN] = { 0 };
1168 fiph = (struct fip_header *)skb->data;
1169 sub = fiph->fip_subcode;
1170 if (sub != FIP_SC_REQ && sub != FIP_SC_REP)
1173 rlen = ntohs(fiph->fip_dl_len) * 4;
1174 if (rlen + sizeof(*fiph) > skb->len)
1177 desc = (struct fip_desc *)(fiph + 1);
1180 dlen = desc->fip_dlen * FIP_BPW;
1181 if (dlen < sizeof(*desc) || dlen > rlen)
1183 /* Drop ELS if there are duplicate critical descriptors */
1184 if (desc->fip_dtype < 32) {
1185 if ((desc->fip_dtype != FIP_DT_MAC) &&
1186 (desc_mask & 1U << desc->fip_dtype)) {
1187 LIBFCOE_FIP_DBG(fip, "Duplicate Critical "
1188 "Descriptors in FIP ELS\n");
1191 desc_mask |= (1 << desc->fip_dtype);
1193 switch (desc->fip_dtype) {
1196 if (desc_cnt == 1) {
1197 LIBFCOE_FIP_DBG(fip, "FIP descriptors "
1198 "received out of order\n");
1202 * Some switch implementations send two MAC descriptors,
1203 * with first MAC(granted_mac) being the FPMA, and the
1204 * second one(fcoe_mac) is used as destination address
1205 * for sending/receiving FCoE packets. FIP traffic is
1206 * sent using fip_mac. For regular switches, both
1207 * fip_mac and fcoe_mac would be the same.
1211 ((struct fip_mac_desc *)desc)->fd_mac,
1214 if (dlen != sizeof(struct fip_mac_desc))
1217 if ((desc_cnt == 3) && (sel))
1218 memcpy(sel->fcoe_mac,
1219 ((struct fip_mac_desc *)desc)->fd_mac,
1226 if (desc_cnt != 1) {
1227 LIBFCOE_FIP_DBG(fip, "FIP descriptors "
1228 "received out of order\n");
1233 if (dlen < sizeof(*els) + sizeof(*fh) + 1)
1235 els_len = dlen - sizeof(*els);
1236 els = (struct fip_encaps *)desc;
1237 fh = (struct fc_frame_header *)(els + 1);
1238 els_dtype = desc->fip_dtype;
1241 LIBFCOE_FIP_DBG(fip, "unexpected descriptor type %x "
1242 "in FIP adv\n", desc->fip_dtype);
1243 /* standard says ignore unknown descriptors >= 128 */
1244 if (desc->fip_dtype < FIP_DT_NON_CRITICAL)
1246 if (desc_cnt <= 2) {
1247 LIBFCOE_FIP_DBG(fip, "FIP descriptors "
1248 "received out of order\n");
1253 desc = (struct fip_desc *)((char *)desc + dlen);
1259 els_op = *(u8 *)(fh + 1);
1261 if ((els_dtype == FIP_DT_FLOGI || els_dtype == FIP_DT_FDISC) &&
1262 sub == FIP_SC_REP && fip->mode != FIP_MODE_VN2VN) {
1263 if (els_op == ELS_LS_ACC) {
1264 if (!is_valid_ether_addr(granted_mac)) {
1265 LIBFCOE_FIP_DBG(fip,
1266 "Invalid MAC address %pM in FIP ELS\n",
1270 memcpy(fr_cb(fp)->granted_mac, granted_mac, ETH_ALEN);
1272 if (fip->flogi_oxid == ntohs(fh->fh_ox_id)) {
1273 fip->flogi_oxid = FC_XID_UNKNOWN;
1274 if (els_dtype == FIP_DT_FLOGI)
1275 fcoe_ctlr_announce(fip);
1277 } else if (els_dtype == FIP_DT_FLOGI &&
1278 !fcoe_ctlr_flogi_retry(fip))
1279 goto drop; /* retrying FLOGI so drop reject */
1282 if ((desc_cnt == 0) || ((els_op != ELS_LS_RJT) &&
1283 (!(1U << FIP_DT_MAC & desc_mask)))) {
1284 LIBFCOE_FIP_DBG(fip, "Missing critical descriptors "
1290 * Convert skb into an fc_frame containing only the ELS.
1292 skb_pull(skb, (u8 *)fh - skb->data);
1293 skb_trim(skb, els_len);
1294 fp = (struct fc_frame *)skb;
1296 fr_sof(fp) = FC_SOF_I3;
1297 fr_eof(fp) = FC_EOF_T;
1299 fr_encaps(fp) = els_dtype;
1301 stats = per_cpu_ptr(lport->stats, get_cpu());
1303 stats->RxWords += skb->len / FIP_BPW;
1306 fc_exch_recv(lport, fp);
1310 LIBFCOE_FIP_DBG(fip, "FIP length error in descriptor type %x len %zu\n",
1311 desc->fip_dtype, dlen);
1317 * fcoe_ctlr_recv_els() - Handle an incoming link reset frame
1318 * @fip: The FCoE controller that received the frame
1319 * @fh: The received FIP header
1321 * There may be multiple VN_Port descriptors.
1322 * The overall length has already been checked.
1324 static void fcoe_ctlr_recv_clr_vlink(struct fcoe_ctlr *fip,
1325 struct sk_buff *skb)
1327 struct fip_desc *desc;
1328 struct fip_mac_desc *mp;
1329 struct fip_wwn_desc *wp;
1330 struct fip_vn_desc *vp;
1333 struct fcoe_fcf *fcf = fip->sel_fcf;
1334 struct fc_lport *lport = fip->lp;
1335 struct fc_lport *vn_port = NULL;
1338 int reset_phys_port = 0;
1339 struct fip_vn_desc **vlink_desc_arr = NULL;
1340 struct fip_header *fh = (struct fip_header *)skb->data;
1341 struct ethhdr *eh = eth_hdr(skb);
1343 LIBFCOE_FIP_DBG(fip, "Clear Virtual Link received\n");
1347 * We are yet to select best FCF, but we got CVL in the
1348 * meantime. reset the ctlr and let it rediscover the FCF
1350 LIBFCOE_FIP_DBG(fip, "Resetting fcoe_ctlr as FCF has not been "
1352 mutex_lock(&fip->ctlr_mutex);
1353 fcoe_ctlr_reset(fip);
1354 mutex_unlock(&fip->ctlr_mutex);
1359 * If we've selected an FCF check that the CVL is from there to avoid
1360 * processing CVLs from an unexpected source. If it is from an
1361 * unexpected source drop it on the floor.
1363 if (!ether_addr_equal(eh->h_source, fcf->fcf_mac)) {
1364 LIBFCOE_FIP_DBG(fip, "Dropping CVL due to source address "
1365 "mismatch with FCF src=%pM\n", eh->h_source);
1370 * If we haven't logged into the fabric but receive a CVL we should
1371 * reset everything and go back to solicitation.
1373 if (!lport->port_id) {
1374 LIBFCOE_FIP_DBG(fip, "lport not logged in, resoliciting\n");
1375 mutex_lock(&fip->ctlr_mutex);
1376 fcoe_ctlr_reset(fip);
1377 mutex_unlock(&fip->ctlr_mutex);
1378 fc_lport_reset(fip->lp);
1379 fcoe_ctlr_solicit(fip, NULL);
1384 * mask of required descriptors. Validating each one clears its bit.
1386 desc_mask = BIT(FIP_DT_MAC) | BIT(FIP_DT_NAME);
1388 rlen = ntohs(fh->fip_dl_len) * FIP_BPW;
1389 desc = (struct fip_desc *)(fh + 1);
1392 * Actually need to subtract 'sizeof(*mp) - sizeof(*wp)' from 'rlen'
1393 * before determining max Vx_Port descriptor but a buggy FCF could have
1394 * omitted either or both MAC Address and Name Identifier descriptors
1396 num_vlink_desc = rlen / sizeof(*vp);
1398 vlink_desc_arr = kmalloc_array(num_vlink_desc, sizeof(vp),
1400 if (!vlink_desc_arr)
1404 while (rlen >= sizeof(*desc)) {
1405 dlen = desc->fip_dlen * FIP_BPW;
1408 /* Drop CVL if there are duplicate critical descriptors */
1409 if ((desc->fip_dtype < 32) &&
1410 (desc->fip_dtype != FIP_DT_VN_ID) &&
1411 !(desc_mask & 1U << desc->fip_dtype)) {
1412 LIBFCOE_FIP_DBG(fip, "Duplicate Critical "
1413 "Descriptors in FIP CVL\n");
1416 switch (desc->fip_dtype) {
1418 mp = (struct fip_mac_desc *)desc;
1419 if (dlen < sizeof(*mp))
1421 if (!ether_addr_equal(mp->fd_mac, fcf->fcf_mac))
1423 desc_mask &= ~BIT(FIP_DT_MAC);
1426 wp = (struct fip_wwn_desc *)desc;
1427 if (dlen < sizeof(*wp))
1429 if (get_unaligned_be64(&wp->fd_wwn) != fcf->switch_name)
1431 desc_mask &= ~BIT(FIP_DT_NAME);
1434 vp = (struct fip_vn_desc *)desc;
1435 if (dlen < sizeof(*vp))
1437 vlink_desc_arr[num_vlink_desc++] = vp;
1438 vn_port = fc_vport_id_lookup(lport,
1439 ntoh24(vp->fd_fc_id));
1440 if (vn_port && (vn_port == lport)) {
1441 mutex_lock(&fip->ctlr_mutex);
1442 per_cpu_ptr(lport->stats,
1443 get_cpu())->VLinkFailureCount++;
1445 fcoe_ctlr_reset(fip);
1446 mutex_unlock(&fip->ctlr_mutex);
1450 /* standard says ignore unknown descriptors >= 128 */
1451 if (desc->fip_dtype < FIP_DT_NON_CRITICAL)
1455 desc = (struct fip_desc *)((char *)desc + dlen);
1460 * reset only if all required descriptors were present and valid.
1463 LIBFCOE_FIP_DBG(fip, "missing descriptors mask %x\n",
1465 else if (!num_vlink_desc) {
1466 LIBFCOE_FIP_DBG(fip, "CVL: no Vx_Port descriptor found\n");
1468 * No Vx_Port description. Clear all NPIV ports,
1469 * followed by physical port
1471 mutex_lock(&fip->ctlr_mutex);
1472 per_cpu_ptr(lport->stats, get_cpu())->VLinkFailureCount++;
1474 fcoe_ctlr_reset(fip);
1475 mutex_unlock(&fip->ctlr_mutex);
1477 mutex_lock(&lport->lp_mutex);
1478 list_for_each_entry(vn_port, &lport->vports, list)
1479 fc_lport_reset(vn_port);
1480 mutex_unlock(&lport->lp_mutex);
1482 fc_lport_reset(fip->lp);
1483 fcoe_ctlr_solicit(fip, NULL);
1487 LIBFCOE_FIP_DBG(fip, "performing Clear Virtual Link\n");
1488 for (i = 0; i < num_vlink_desc; i++) {
1489 vp = vlink_desc_arr[i];
1490 vn_port = fc_vport_id_lookup(lport,
1491 ntoh24(vp->fd_fc_id));
1496 * 'port_id' is already validated, check MAC address and
1499 if (!ether_addr_equal(fip->get_src_addr(vn_port),
1501 get_unaligned_be64(&vp->fd_wwpn) !=
1505 if (vn_port == lport)
1507 * Physical port, defer processing till all
1508 * listed NPIV ports are cleared
1510 reset_phys_port = 1;
1511 else /* NPIV port */
1512 fc_lport_reset(vn_port);
1515 if (reset_phys_port) {
1516 fc_lport_reset(fip->lp);
1517 fcoe_ctlr_solicit(fip, NULL);
1522 kfree(vlink_desc_arr);
1526 * fcoe_ctlr_recv() - Receive a FIP packet
1527 * @fip: The FCoE controller that received the packet
1528 * @skb: The received FIP packet
1530 * This may be called from either NET_RX_SOFTIRQ or IRQ.
1532 void fcoe_ctlr_recv(struct fcoe_ctlr *fip, struct sk_buff *skb)
1534 skb = skb_share_check(skb, GFP_ATOMIC);
1537 skb_queue_tail(&fip->fip_recv_list, skb);
1538 schedule_work(&fip->recv_work);
1540 EXPORT_SYMBOL(fcoe_ctlr_recv);
1543 * fcoe_ctlr_recv_handler() - Receive a FIP frame
1544 * @fip: The FCoE controller that received the frame
1545 * @skb: The received FIP frame
1547 * Returns non-zero if the frame is dropped.
1549 static int fcoe_ctlr_recv_handler(struct fcoe_ctlr *fip, struct sk_buff *skb)
1551 struct fip_header *fiph;
1553 enum fip_state state;
1554 bool fip_vlan_resp = false;
1558 if (skb_linearize(skb))
1560 if (skb->len < sizeof(*fiph))
1563 if (fip->mode == FIP_MODE_VN2VN) {
1564 if (!ether_addr_equal(eh->h_dest, fip->ctl_src_addr) &&
1565 !ether_addr_equal(eh->h_dest, fcoe_all_vn2vn) &&
1566 !ether_addr_equal(eh->h_dest, fcoe_all_p2p))
1568 } else if (!ether_addr_equal(eh->h_dest, fip->ctl_src_addr) &&
1569 !ether_addr_equal(eh->h_dest, fcoe_all_enode))
1571 fiph = (struct fip_header *)skb->data;
1572 op = ntohs(fiph->fip_op);
1573 sub = fiph->fip_subcode;
1575 if (FIP_VER_DECAPS(fiph->fip_ver) != FIP_VER)
1577 if (ntohs(fiph->fip_dl_len) * FIP_BPW + sizeof(*fiph) > skb->len)
1580 mutex_lock(&fip->ctlr_mutex);
1582 if (state == FIP_ST_AUTO) {
1584 fcoe_ctlr_set_state(fip, FIP_ST_ENABLED);
1585 state = FIP_ST_ENABLED;
1586 LIBFCOE_FIP_DBG(fip, "Using FIP mode\n");
1588 fip_vlan_resp = fip->fip_resp;
1589 mutex_unlock(&fip->ctlr_mutex);
1591 if (fip->mode == FIP_MODE_VN2VN && op == FIP_OP_VN2VN)
1592 return fcoe_ctlr_vn_recv(fip, skb);
1594 if (fip_vlan_resp && op == FIP_OP_VLAN) {
1595 LIBFCOE_FIP_DBG(fip, "fip vlan discovery\n");
1596 return fcoe_ctlr_vlan_recv(fip, skb);
1599 if (state != FIP_ST_ENABLED && state != FIP_ST_VNMP_UP &&
1600 state != FIP_ST_VNMP_CLAIM)
1603 if (op == FIP_OP_LS) {
1604 fcoe_ctlr_recv_els(fip, skb); /* consumes skb */
1608 if (state != FIP_ST_ENABLED)
1611 if (op == FIP_OP_DISC && sub == FIP_SC_ADV)
1612 fcoe_ctlr_recv_adv(fip, skb);
1613 else if (op == FIP_OP_CTRL && sub == FIP_SC_CLR_VLINK)
1614 fcoe_ctlr_recv_clr_vlink(fip, skb);
1623 * fcoe_ctlr_select() - Select the best FCF (if possible)
1624 * @fip: The FCoE controller
1626 * Returns the selected FCF, or NULL if none are usable.
1628 * If there are conflicting advertisements, no FCF can be chosen.
1630 * If there is already a selected FCF, this will choose a better one or
1631 * an equivalent one that hasn't already been sent a FLOGI.
1633 * Called with lock held.
1635 static struct fcoe_fcf *fcoe_ctlr_select(struct fcoe_ctlr *fip)
1637 struct fcoe_fcf *fcf;
1638 struct fcoe_fcf *best = fip->sel_fcf;
1640 list_for_each_entry(fcf, &fip->fcfs, list) {
1641 LIBFCOE_FIP_DBG(fip, "consider FCF fab %16.16llx "
1642 "VFID %d mac %pM map %x val %d "
1644 fcf->fabric_name, fcf->vfid, fcf->fcf_mac,
1645 fcf->fc_map, fcoe_ctlr_mtu_valid(fcf),
1646 fcf->flogi_sent, fcf->pri);
1647 if (!fcoe_ctlr_fcf_usable(fcf)) {
1648 LIBFCOE_FIP_DBG(fip, "FCF for fab %16.16llx "
1649 "map %x %svalid %savailable\n",
1650 fcf->fabric_name, fcf->fc_map,
1651 (fcf->flags & FIP_FL_SOL) ? "" : "in",
1652 (fcf->flags & FIP_FL_AVAIL) ?
1656 if (!best || fcf->pri < best->pri || best->flogi_sent)
1658 if (fcf->fabric_name != best->fabric_name ||
1659 fcf->vfid != best->vfid ||
1660 fcf->fc_map != best->fc_map) {
1661 LIBFCOE_FIP_DBG(fip, "Conflicting fabric, VFID, "
1666 fip->sel_fcf = best;
1668 LIBFCOE_FIP_DBG(fip, "using FCF mac %pM\n", best->fcf_mac);
1669 fip->port_ka_time = jiffies +
1670 msecs_to_jiffies(FIP_VN_KA_PERIOD);
1671 fip->ctlr_ka_time = jiffies + best->fka_period;
1672 if (time_before(fip->ctlr_ka_time, fip->timer.expires))
1673 mod_timer(&fip->timer, fip->ctlr_ka_time);
1679 * fcoe_ctlr_flogi_send_locked() - send FIP-encapsulated FLOGI to current FCF
1680 * @fip: The FCoE controller
1682 * Returns non-zero error if it could not be sent.
1684 * Called with ctlr_mutex and ctlr_lock held.
1685 * Caller must verify that fip->sel_fcf is not NULL.
1687 static int fcoe_ctlr_flogi_send_locked(struct fcoe_ctlr *fip)
1689 struct sk_buff *skb;
1690 struct sk_buff *skb_orig;
1691 struct fc_frame_header *fh;
1694 skb_orig = fip->flogi_req;
1699 * Clone and send the FLOGI request. If clone fails, use original.
1701 skb = skb_clone(skb_orig, GFP_ATOMIC);
1704 fip->flogi_req = NULL;
1706 fh = (struct fc_frame_header *)skb->data;
1707 error = fcoe_ctlr_encaps(fip, fip->lp, FIP_DT_FLOGI, skb,
1708 ntoh24(fh->fh_d_id));
1713 fip->send(fip, skb);
1714 fip->sel_fcf->flogi_sent = 1;
1719 * fcoe_ctlr_flogi_retry() - resend FLOGI request to a new FCF if possible
1720 * @fip: The FCoE controller
1722 * Returns non-zero error code if there's no FLOGI request to retry or
1723 * no alternate FCF available.
1725 static int fcoe_ctlr_flogi_retry(struct fcoe_ctlr *fip)
1727 struct fcoe_fcf *fcf;
1728 unsigned long flags;
1731 mutex_lock(&fip->ctlr_mutex);
1732 spin_lock_irqsave(&fip->ctlr_lock, flags);
1733 LIBFCOE_FIP_DBG(fip, "re-sending FLOGI - reselect\n");
1734 fcf = fcoe_ctlr_select(fip);
1735 if (!fcf || fcf->flogi_sent) {
1736 kfree_skb(fip->flogi_req);
1737 fip->flogi_req = NULL;
1740 fcoe_ctlr_solicit(fip, NULL);
1741 error = fcoe_ctlr_flogi_send_locked(fip);
1743 spin_unlock_irqrestore(&fip->ctlr_lock, flags);
1744 mutex_unlock(&fip->ctlr_mutex);
1750 * fcoe_ctlr_flogi_send() - Handle sending of FIP FLOGI.
1751 * @fip: The FCoE controller that timed out
1753 * Done here because fcoe_ctlr_els_send() can't get mutex.
1755 * Called with ctlr_mutex held. The caller must not hold ctlr_lock.
1757 static void fcoe_ctlr_flogi_send(struct fcoe_ctlr *fip)
1759 struct fcoe_fcf *fcf;
1760 unsigned long flags;
1762 spin_lock_irqsave(&fip->ctlr_lock, flags);
1764 if (!fcf || !fip->flogi_req_send)
1767 LIBFCOE_FIP_DBG(fip, "sending FLOGI\n");
1770 * If this FLOGI is being sent due to a timeout retry
1771 * to the same FCF as before, select a different FCF if possible.
1773 if (fcf->flogi_sent) {
1774 LIBFCOE_FIP_DBG(fip, "sending FLOGI - reselect\n");
1775 fcf = fcoe_ctlr_select(fip);
1776 if (!fcf || fcf->flogi_sent) {
1777 LIBFCOE_FIP_DBG(fip, "sending FLOGI - clearing\n");
1778 list_for_each_entry(fcf, &fip->fcfs, list)
1779 fcf->flogi_sent = 0;
1780 fcf = fcoe_ctlr_select(fip);
1784 fcoe_ctlr_flogi_send_locked(fip);
1785 fip->flogi_req_send = 0;
1787 LIBFCOE_FIP_DBG(fip, "No FCF selected - defer send\n");
1789 spin_unlock_irqrestore(&fip->ctlr_lock, flags);
1793 * fcoe_ctlr_timeout() - FIP timeout handler
1794 * @arg: The FCoE controller that timed out
1796 static void fcoe_ctlr_timeout(struct timer_list *t)
1798 struct fcoe_ctlr *fip = from_timer(fip, t, timer);
1800 schedule_work(&fip->timer_work);
1804 * fcoe_ctlr_timer_work() - Worker thread function for timer work
1805 * @work: Handle to a FCoE controller
1807 * Ages FCFs. Triggers FCF selection if possible.
1808 * Sends keep-alives and resets.
1810 static void fcoe_ctlr_timer_work(struct work_struct *work)
1812 struct fcoe_ctlr *fip;
1813 struct fc_lport *vport;
1816 u8 send_ctlr_ka = 0;
1817 u8 send_port_ka = 0;
1818 struct fcoe_fcf *sel;
1819 struct fcoe_fcf *fcf;
1820 unsigned long next_timer;
1822 fip = container_of(work, struct fcoe_ctlr, timer_work);
1823 if (fip->mode == FIP_MODE_VN2VN)
1824 return fcoe_ctlr_vn_timeout(fip);
1825 mutex_lock(&fip->ctlr_mutex);
1826 if (fip->state == FIP_ST_DISABLED) {
1827 mutex_unlock(&fip->ctlr_mutex);
1832 next_timer = fcoe_ctlr_age_fcfs(fip);
1835 if (!sel && fip->sel_time) {
1836 if (time_after_eq(jiffies, fip->sel_time)) {
1837 sel = fcoe_ctlr_select(fip);
1839 } else if (time_after(next_timer, fip->sel_time))
1840 next_timer = fip->sel_time;
1843 if (sel && fip->flogi_req_send)
1844 fcoe_ctlr_flogi_send(fip);
1845 else if (!sel && fcf)
1848 if (sel && !sel->fd_flags) {
1849 if (time_after_eq(jiffies, fip->ctlr_ka_time)) {
1850 fip->ctlr_ka_time = jiffies + sel->fka_period;
1853 if (time_after(next_timer, fip->ctlr_ka_time))
1854 next_timer = fip->ctlr_ka_time;
1856 if (time_after_eq(jiffies, fip->port_ka_time)) {
1857 fip->port_ka_time = jiffies +
1858 msecs_to_jiffies(FIP_VN_KA_PERIOD);
1861 if (time_after(next_timer, fip->port_ka_time))
1862 next_timer = fip->port_ka_time;
1864 if (!list_empty(&fip->fcfs))
1865 mod_timer(&fip->timer, next_timer);
1866 mutex_unlock(&fip->ctlr_mutex);
1869 fc_lport_reset(fip->lp);
1870 /* restart things with a solicitation */
1871 fcoe_ctlr_solicit(fip, NULL);
1875 fcoe_ctlr_send_keep_alive(fip, NULL, 0, fip->ctl_src_addr);
1878 mutex_lock(&fip->lp->lp_mutex);
1879 mac = fip->get_src_addr(fip->lp);
1880 fcoe_ctlr_send_keep_alive(fip, fip->lp, 1, mac);
1881 list_for_each_entry(vport, &fip->lp->vports, list) {
1882 mac = fip->get_src_addr(vport);
1883 fcoe_ctlr_send_keep_alive(fip, vport, 1, mac);
1885 mutex_unlock(&fip->lp->lp_mutex);
1890 * fcoe_ctlr_recv_work() - Worker thread function for receiving FIP frames
1891 * @recv_work: Handle to a FCoE controller
1893 static void fcoe_ctlr_recv_work(struct work_struct *recv_work)
1895 struct fcoe_ctlr *fip;
1896 struct sk_buff *skb;
1898 fip = container_of(recv_work, struct fcoe_ctlr, recv_work);
1899 while ((skb = skb_dequeue(&fip->fip_recv_list)))
1900 fcoe_ctlr_recv_handler(fip, skb);
1904 * fcoe_ctlr_recv_flogi() - Snoop pre-FIP receipt of FLOGI response
1905 * @fip: The FCoE controller
1906 * @fp: The FC frame to snoop
1908 * Snoop potential response to FLOGI or even incoming FLOGI.
1910 * The caller has checked that we are waiting for login as indicated
1911 * by fip->flogi_oxid != FC_XID_UNKNOWN.
1913 * The caller is responsible for freeing the frame.
1914 * Fill in the granted_mac address.
1916 * Return non-zero if the frame should not be delivered to libfc.
1918 int fcoe_ctlr_recv_flogi(struct fcoe_ctlr *fip, struct fc_lport *lport,
1919 struct fc_frame *fp)
1921 struct fc_frame_header *fh;
1925 sa = eth_hdr(&fp->skb)->h_source;
1926 fh = fc_frame_header_get(fp);
1927 if (fh->fh_type != FC_TYPE_ELS)
1930 op = fc_frame_payload_op(fp);
1931 if (op == ELS_LS_ACC && fh->fh_r_ctl == FC_RCTL_ELS_REP &&
1932 fip->flogi_oxid == ntohs(fh->fh_ox_id)) {
1934 mutex_lock(&fip->ctlr_mutex);
1935 if (fip->state != FIP_ST_AUTO && fip->state != FIP_ST_NON_FIP) {
1936 mutex_unlock(&fip->ctlr_mutex);
1939 fcoe_ctlr_set_state(fip, FIP_ST_NON_FIP);
1940 LIBFCOE_FIP_DBG(fip,
1941 "received FLOGI LS_ACC using non-FIP mode\n");
1945 * If the src mac addr is FC_OUI-based, then we mark the
1946 * address_mode flag to use FC_OUI-based Ethernet DA.
1947 * Otherwise we use the FCoE gateway addr
1949 if (ether_addr_equal(sa, (u8[6])FC_FCOE_FLOGI_MAC)) {
1950 fcoe_ctlr_map_dest(fip);
1952 memcpy(fip->dest_addr, sa, ETH_ALEN);
1955 fip->flogi_oxid = FC_XID_UNKNOWN;
1956 mutex_unlock(&fip->ctlr_mutex);
1957 fc_fcoe_set_mac(fr_cb(fp)->granted_mac, fh->fh_d_id);
1958 } else if (op == ELS_FLOGI && fh->fh_r_ctl == FC_RCTL_ELS_REQ && sa) {
1960 * Save source MAC for point-to-point responses.
1962 mutex_lock(&fip->ctlr_mutex);
1963 if (fip->state == FIP_ST_AUTO || fip->state == FIP_ST_NON_FIP) {
1964 memcpy(fip->dest_addr, sa, ETH_ALEN);
1966 if (fip->state == FIP_ST_AUTO)
1967 LIBFCOE_FIP_DBG(fip, "received non-FIP FLOGI. "
1968 "Setting non-FIP mode\n");
1969 fcoe_ctlr_set_state(fip, FIP_ST_NON_FIP);
1971 mutex_unlock(&fip->ctlr_mutex);
1975 EXPORT_SYMBOL(fcoe_ctlr_recv_flogi);
1978 * fcoe_wwn_from_mac() - Converts a 48-bit IEEE MAC address to a 64-bit FC WWN
1979 * @mac: The MAC address to convert
1980 * @scheme: The scheme to use when converting
1981 * @port: The port indicator for converting
1983 * Returns: u64 fc world wide name
1985 u64 fcoe_wwn_from_mac(unsigned char mac[ETH_ALEN],
1986 unsigned int scheme, unsigned int port)
1991 /* The MAC is in NO, so flip only the low 48 bits */
1992 host_mac = ((u64) mac[0] << 40) |
1993 ((u64) mac[1] << 32) |
1994 ((u64) mac[2] << 24) |
1995 ((u64) mac[3] << 16) |
1996 ((u64) mac[4] << 8) |
1999 WARN_ON(host_mac >= (1ULL << 48));
2000 wwn = host_mac | ((u64) scheme << 60);
2006 WARN_ON(port >= 0xfff);
2007 wwn |= (u64) port << 48;
2016 EXPORT_SYMBOL_GPL(fcoe_wwn_from_mac);
2019 * fcoe_ctlr_rport() - return the fcoe_rport for a given fc_rport_priv
2020 * @rdata: libfc remote port
2022 static inline struct fcoe_rport *fcoe_ctlr_rport(struct fc_rport_priv *rdata)
2024 return container_of(rdata, struct fcoe_rport, rdata);
2028 * fcoe_ctlr_vn_send() - Send a FIP VN2VN Probe Request or Reply.
2029 * @fip: The FCoE controller
2030 * @sub: sub-opcode for probe request, reply, or advertisement.
2031 * @dest: The destination Ethernet MAC address
2032 * @min_len: minimum size of the Ethernet payload to be sent
2034 static void fcoe_ctlr_vn_send(struct fcoe_ctlr *fip,
2035 enum fip_vn2vn_subcode sub,
2036 const u8 *dest, size_t min_len)
2038 struct sk_buff *skb;
2039 struct fip_vn2vn_probe_frame {
2041 struct fip_header fip;
2042 struct fip_mac_desc mac;
2043 struct fip_wwn_desc wwnn;
2044 struct fip_vn_desc vn;
2046 struct fip_fc4_feat *ff;
2047 struct fip_size_desc *size;
2052 len = sizeof(*frame);
2054 if (sub == FIP_SC_VN_CLAIM_NOTIFY || sub == FIP_SC_VN_CLAIM_REP) {
2055 dlen = sizeof(struct fip_fc4_feat) +
2056 sizeof(struct fip_size_desc);
2059 dlen += sizeof(frame->mac) + sizeof(frame->wwnn) + sizeof(frame->vn);
2060 len = max(len, min_len + sizeof(struct ethhdr));
2062 skb = dev_alloc_skb(len);
2066 frame = (struct fip_vn2vn_probe_frame *)skb->data;
2067 memset(frame, 0, len);
2068 memcpy(frame->eth.h_dest, dest, ETH_ALEN);
2070 if (sub == FIP_SC_VN_BEACON) {
2071 hton24(frame->eth.h_source, FIP_VN_FC_MAP);
2072 hton24(frame->eth.h_source + 3, fip->port_id);
2074 memcpy(frame->eth.h_source, fip->ctl_src_addr, ETH_ALEN);
2076 frame->eth.h_proto = htons(ETH_P_FIP);
2078 frame->fip.fip_ver = FIP_VER_ENCAPS(FIP_VER);
2079 frame->fip.fip_op = htons(FIP_OP_VN2VN);
2080 frame->fip.fip_subcode = sub;
2081 frame->fip.fip_dl_len = htons(dlen / FIP_BPW);
2083 frame->mac.fd_desc.fip_dtype = FIP_DT_MAC;
2084 frame->mac.fd_desc.fip_dlen = sizeof(frame->mac) / FIP_BPW;
2085 memcpy(frame->mac.fd_mac, fip->ctl_src_addr, ETH_ALEN);
2087 frame->wwnn.fd_desc.fip_dtype = FIP_DT_NAME;
2088 frame->wwnn.fd_desc.fip_dlen = sizeof(frame->wwnn) / FIP_BPW;
2089 put_unaligned_be64(fip->lp->wwnn, &frame->wwnn.fd_wwn);
2091 frame->vn.fd_desc.fip_dtype = FIP_DT_VN_ID;
2092 frame->vn.fd_desc.fip_dlen = sizeof(frame->vn) / FIP_BPW;
2093 hton24(frame->vn.fd_mac, FIP_VN_FC_MAP);
2094 hton24(frame->vn.fd_mac + 3, fip->port_id);
2095 hton24(frame->vn.fd_fc_id, fip->port_id);
2096 put_unaligned_be64(fip->lp->wwpn, &frame->vn.fd_wwpn);
2099 * For claims, add FC-4 features.
2100 * TBD: Add interface to get fc-4 types and features from libfc.
2102 if (sub == FIP_SC_VN_CLAIM_NOTIFY || sub == FIP_SC_VN_CLAIM_REP) {
2103 ff = (struct fip_fc4_feat *)(frame + 1);
2104 ff->fd_desc.fip_dtype = FIP_DT_FC4F;
2105 ff->fd_desc.fip_dlen = sizeof(*ff) / FIP_BPW;
2106 ff->fd_fts = fip->lp->fcts;
2109 if (fip->lp->service_params & FCP_SPPF_INIT_FCN)
2110 fcp_feat |= FCP_FEAT_INIT;
2111 if (fip->lp->service_params & FCP_SPPF_TARG_FCN)
2112 fcp_feat |= FCP_FEAT_TARG;
2113 fcp_feat <<= (FC_TYPE_FCP * 4) % 32;
2114 ff->fd_ff.fd_feat[FC_TYPE_FCP * 4 / 32] = htonl(fcp_feat);
2116 size = (struct fip_size_desc *)(ff + 1);
2117 size->fd_desc.fip_dtype = FIP_DT_FCOE_SIZE;
2118 size->fd_desc.fip_dlen = sizeof(*size) / FIP_BPW;
2119 size->fd_size = htons(fcoe_ctlr_fcoe_size(fip));
2123 skb->protocol = htons(ETH_P_FIP);
2124 skb->priority = fip->priority;
2125 skb_reset_mac_header(skb);
2126 skb_reset_network_header(skb);
2128 fip->send(fip, skb);
2132 * fcoe_ctlr_vn_rport_callback - Event handler for rport events.
2133 * @lport: The lport which is receiving the event
2134 * @rdata: remote port private data
2135 * @event: The event that occurred
2137 * Locking Note: The rport lock must not be held when calling this function.
2139 static void fcoe_ctlr_vn_rport_callback(struct fc_lport *lport,
2140 struct fc_rport_priv *rdata,
2141 enum fc_rport_event event)
2143 struct fcoe_ctlr *fip = lport->disc.priv;
2144 struct fcoe_rport *frport = fcoe_ctlr_rport(rdata);
2146 LIBFCOE_FIP_DBG(fip, "vn_rport_callback %x event %d\n",
2147 rdata->ids.port_id, event);
2149 mutex_lock(&fip->ctlr_mutex);
2151 case RPORT_EV_READY:
2152 frport->login_count = 0;
2155 case RPORT_EV_FAILED:
2157 frport->login_count++;
2158 if (frport->login_count > FCOE_CTLR_VN2VN_LOGIN_LIMIT) {
2159 LIBFCOE_FIP_DBG(fip,
2160 "rport FLOGI limited port_id %6.6x\n",
2161 rdata->ids.port_id);
2162 fc_rport_logoff(rdata);
2168 mutex_unlock(&fip->ctlr_mutex);
2171 static struct fc_rport_operations fcoe_ctlr_vn_rport_ops = {
2172 .event_callback = fcoe_ctlr_vn_rport_callback,
2176 * fcoe_ctlr_disc_stop_locked() - stop discovery in VN2VN mode
2177 * @fip: The FCoE controller
2179 * Called with ctlr_mutex held.
2181 static void fcoe_ctlr_disc_stop_locked(struct fc_lport *lport)
2183 struct fc_rport_priv *rdata;
2185 mutex_lock(&lport->disc.disc_mutex);
2186 list_for_each_entry_rcu(rdata, &lport->disc.rports, peers) {
2187 if (kref_get_unless_zero(&rdata->kref)) {
2188 fc_rport_logoff(rdata);
2189 kref_put(&rdata->kref, fc_rport_destroy);
2192 lport->disc.disc_callback = NULL;
2193 mutex_unlock(&lport->disc.disc_mutex);
2197 * fcoe_ctlr_disc_stop() - stop discovery in VN2VN mode
2198 * @fip: The FCoE controller
2200 * Called through the local port template for discovery.
2201 * Called without the ctlr_mutex held.
2203 static void fcoe_ctlr_disc_stop(struct fc_lport *lport)
2205 struct fcoe_ctlr *fip = lport->disc.priv;
2207 mutex_lock(&fip->ctlr_mutex);
2208 fcoe_ctlr_disc_stop_locked(lport);
2209 mutex_unlock(&fip->ctlr_mutex);
2213 * fcoe_ctlr_disc_stop_final() - stop discovery for shutdown in VN2VN mode
2214 * @fip: The FCoE controller
2216 * Called through the local port template for discovery.
2217 * Called without the ctlr_mutex held.
2219 static void fcoe_ctlr_disc_stop_final(struct fc_lport *lport)
2221 fcoe_ctlr_disc_stop(lport);
2222 fc_rport_flush_queue();
2227 * fcoe_ctlr_vn_restart() - VN2VN probe restart with new port_id
2228 * @fip: The FCoE controller
2230 * Called with fcoe_ctlr lock held.
2232 static void fcoe_ctlr_vn_restart(struct fcoe_ctlr *fip)
2237 fcoe_ctlr_disc_stop_locked(fip->lp);
2240 * Get proposed port ID.
2241 * If this is the first try after link up, use any previous port_id.
2242 * If there was none, use the low bits of the port_name.
2243 * On subsequent tries, get the next random one.
2244 * Don't use reserved IDs, use another non-zero value, just as random.
2246 port_id = fip->port_id;
2247 if (fip->probe_tries)
2248 port_id = prandom_u32_state(&fip->rnd_state) & 0xffff;
2250 port_id = fip->lp->wwpn & 0xffff;
2251 if (!port_id || port_id == 0xffff)
2253 fip->port_id = port_id;
2255 if (fip->probe_tries < FIP_VN_RLIM_COUNT) {
2257 wait = prandom_u32() % FIP_VN_PROBE_WAIT;
2259 wait = FIP_VN_RLIM_INT;
2260 mod_timer(&fip->timer, jiffies + msecs_to_jiffies(wait));
2261 fcoe_ctlr_set_state(fip, FIP_ST_VNMP_START);
2265 * fcoe_ctlr_vn_start() - Start in VN2VN mode
2266 * @fip: The FCoE controller
2268 * Called with fcoe_ctlr lock held.
2270 static void fcoe_ctlr_vn_start(struct fcoe_ctlr *fip)
2272 fip->probe_tries = 0;
2273 prandom_seed_state(&fip->rnd_state, fip->lp->wwpn);
2274 fcoe_ctlr_vn_restart(fip);
2278 * fcoe_ctlr_vn_parse - parse probe request or response
2279 * @fip: The FCoE controller
2280 * @skb: incoming packet
2281 * @rdata: buffer for resulting parsed VN entry plus fcoe_rport
2283 * Returns non-zero error number on error.
2284 * Does not consume the packet.
2286 static int fcoe_ctlr_vn_parse(struct fcoe_ctlr *fip,
2287 struct sk_buff *skb,
2288 struct fcoe_rport *frport)
2290 struct fip_header *fiph;
2291 struct fip_desc *desc = NULL;
2292 struct fip_mac_desc *macd = NULL;
2293 struct fip_wwn_desc *wwn = NULL;
2294 struct fip_vn_desc *vn = NULL;
2295 struct fip_size_desc *size = NULL;
2302 fiph = (struct fip_header *)skb->data;
2303 frport->flags = ntohs(fiph->fip_flags);
2305 sub = fiph->fip_subcode;
2307 case FIP_SC_VN_PROBE_REQ:
2308 case FIP_SC_VN_PROBE_REP:
2309 case FIP_SC_VN_BEACON:
2310 desc_mask = BIT(FIP_DT_MAC) | BIT(FIP_DT_NAME) |
2313 case FIP_SC_VN_CLAIM_NOTIFY:
2314 case FIP_SC_VN_CLAIM_REP:
2315 desc_mask = BIT(FIP_DT_MAC) | BIT(FIP_DT_NAME) |
2316 BIT(FIP_DT_VN_ID) | BIT(FIP_DT_FC4F) |
2317 BIT(FIP_DT_FCOE_SIZE);
2320 LIBFCOE_FIP_DBG(fip, "vn_parse unknown subcode %u\n", sub);
2324 rlen = ntohs(fiph->fip_dl_len) * 4;
2325 if (rlen + sizeof(*fiph) > skb->len)
2328 desc = (struct fip_desc *)(fiph + 1);
2330 dlen = desc->fip_dlen * FIP_BPW;
2331 if (dlen < sizeof(*desc) || dlen > rlen)
2334 dtype = desc->fip_dtype;
2336 if (!(desc_mask & BIT(dtype))) {
2337 LIBFCOE_FIP_DBG(fip,
2338 "unexpected or duplicated desc "
2340 "FIP VN2VN subtype %u\n",
2344 desc_mask &= ~BIT(dtype);
2349 if (dlen != sizeof(struct fip_mac_desc))
2351 macd = (struct fip_mac_desc *)desc;
2352 if (!is_valid_ether_addr(macd->fd_mac)) {
2353 LIBFCOE_FIP_DBG(fip,
2354 "Invalid MAC addr %pM in FIP VN2VN\n",
2358 memcpy(frport->enode_mac, macd->fd_mac, ETH_ALEN);
2361 if (dlen != sizeof(struct fip_wwn_desc))
2363 wwn = (struct fip_wwn_desc *)desc;
2364 frport->rdata.ids.node_name =
2365 get_unaligned_be64(&wwn->fd_wwn);
2368 if (dlen != sizeof(struct fip_vn_desc))
2370 vn = (struct fip_vn_desc *)desc;
2371 memcpy(frport->vn_mac, vn->fd_mac, ETH_ALEN);
2372 frport->rdata.ids.port_id = ntoh24(vn->fd_fc_id);
2373 frport->rdata.ids.port_name =
2374 get_unaligned_be64(&vn->fd_wwpn);
2377 if (dlen != sizeof(struct fip_fc4_feat))
2380 case FIP_DT_FCOE_SIZE:
2381 if (dlen != sizeof(struct fip_size_desc))
2383 size = (struct fip_size_desc *)desc;
2384 frport->fcoe_len = ntohs(size->fd_size);
2387 LIBFCOE_FIP_DBG(fip, "unexpected descriptor type %x "
2388 "in FIP probe\n", dtype);
2389 /* standard says ignore unknown descriptors >= 128 */
2390 if (dtype < FIP_DT_NON_CRITICAL)
2394 desc = (struct fip_desc *)((char *)desc + dlen);
2400 LIBFCOE_FIP_DBG(fip, "FIP length error in descriptor type %x len %zu\n",
2406 * fcoe_ctlr_vn_send_claim() - send multicast FIP VN2VN Claim Notification.
2407 * @fip: The FCoE controller
2409 * Called with ctlr_mutex held.
2411 static void fcoe_ctlr_vn_send_claim(struct fcoe_ctlr *fip)
2413 fcoe_ctlr_vn_send(fip, FIP_SC_VN_CLAIM_NOTIFY, fcoe_all_vn2vn, 0);
2414 fip->sol_time = jiffies;
2418 * fcoe_ctlr_vn_probe_req() - handle incoming VN2VN probe request.
2419 * @fip: The FCoE controller
2420 * @rdata: parsed remote port with frport from the probe request
2422 * Called with ctlr_mutex held.
2424 static void fcoe_ctlr_vn_probe_req(struct fcoe_ctlr *fip,
2425 struct fc_rport_priv *rdata)
2427 struct fcoe_rport *frport = fcoe_ctlr_rport(rdata);
2429 if (rdata->ids.port_id != fip->port_id)
2432 switch (fip->state) {
2433 case FIP_ST_VNMP_CLAIM:
2434 case FIP_ST_VNMP_UP:
2435 LIBFCOE_FIP_DBG(fip, "vn_probe_req: send reply, state %x\n",
2437 fcoe_ctlr_vn_send(fip, FIP_SC_VN_PROBE_REP,
2438 frport->enode_mac, 0);
2440 case FIP_ST_VNMP_PROBE1:
2441 case FIP_ST_VNMP_PROBE2:
2443 * Decide whether to reply to the Probe.
2444 * Our selected address is never a "recorded" one, so
2445 * only reply if our WWPN is greater and the
2446 * Probe's REC bit is not set.
2447 * If we don't reply, we will change our address.
2449 if (fip->lp->wwpn > rdata->ids.port_name &&
2450 !(frport->flags & FIP_FL_REC_OR_P2P)) {
2451 LIBFCOE_FIP_DBG(fip, "vn_probe_req: "
2452 "port_id collision\n");
2453 fcoe_ctlr_vn_send(fip, FIP_SC_VN_PROBE_REP,
2454 frport->enode_mac, 0);
2458 case FIP_ST_VNMP_START:
2459 LIBFCOE_FIP_DBG(fip, "vn_probe_req: "
2460 "restart VN2VN negotiation\n");
2461 fcoe_ctlr_vn_restart(fip);
2464 LIBFCOE_FIP_DBG(fip, "vn_probe_req: ignore state %x\n",
2471 * fcoe_ctlr_vn_probe_reply() - handle incoming VN2VN probe reply.
2472 * @fip: The FCoE controller
2473 * @rdata: parsed remote port with frport from the probe request
2475 * Called with ctlr_mutex held.
2477 static void fcoe_ctlr_vn_probe_reply(struct fcoe_ctlr *fip,
2478 struct fc_rport_priv *rdata)
2480 if (rdata->ids.port_id != fip->port_id)
2482 switch (fip->state) {
2483 case FIP_ST_VNMP_START:
2484 case FIP_ST_VNMP_PROBE1:
2485 case FIP_ST_VNMP_PROBE2:
2486 case FIP_ST_VNMP_CLAIM:
2487 LIBFCOE_FIP_DBG(fip, "vn_probe_reply: restart state %x\n",
2489 fcoe_ctlr_vn_restart(fip);
2491 case FIP_ST_VNMP_UP:
2492 LIBFCOE_FIP_DBG(fip, "vn_probe_reply: send claim notify\n");
2493 fcoe_ctlr_vn_send_claim(fip);
2501 * fcoe_ctlr_vn_add() - Add a VN2VN entry to the list, based on a claim reply.
2502 * @fip: The FCoE controller
2503 * @new: newly-parsed remote port with frport as a template for new rdata
2505 * Called with ctlr_mutex held.
2507 static void fcoe_ctlr_vn_add(struct fcoe_ctlr *fip, struct fc_rport_priv *new)
2509 struct fc_lport *lport = fip->lp;
2510 struct fc_rport_priv *rdata;
2511 struct fc_rport_identifiers *ids;
2512 struct fcoe_rport *frport;
2515 port_id = new->ids.port_id;
2516 if (port_id == fip->port_id)
2519 mutex_lock(&lport->disc.disc_mutex);
2520 rdata = fc_rport_create(lport, port_id);
2522 mutex_unlock(&lport->disc.disc_mutex);
2525 mutex_lock(&rdata->rp_mutex);
2526 mutex_unlock(&lport->disc.disc_mutex);
2528 rdata->ops = &fcoe_ctlr_vn_rport_ops;
2529 rdata->disc_id = lport->disc.disc_id;
2532 if ((ids->port_name != -1 && ids->port_name != new->ids.port_name) ||
2533 (ids->node_name != -1 && ids->node_name != new->ids.node_name)) {
2534 mutex_unlock(&rdata->rp_mutex);
2535 LIBFCOE_FIP_DBG(fip, "vn_add rport logoff %6.6x\n", port_id);
2536 fc_rport_logoff(rdata);
2537 mutex_lock(&rdata->rp_mutex);
2539 ids->port_name = new->ids.port_name;
2540 ids->node_name = new->ids.node_name;
2541 mutex_unlock(&rdata->rp_mutex);
2543 frport = fcoe_ctlr_rport(rdata);
2544 LIBFCOE_FIP_DBG(fip, "vn_add rport %6.6x %s state %d\n",
2545 port_id, frport->fcoe_len ? "old" : "new",
2547 *frport = *fcoe_ctlr_rport(new);
2552 * fcoe_ctlr_vn_lookup() - Find VN remote port's MAC address
2553 * @fip: The FCoE controller
2554 * @port_id: The port_id of the remote VN_node
2555 * @mac: buffer which will hold the VN_NODE destination MAC address, if found.
2557 * Returns non-zero error if no remote port found.
2559 static int fcoe_ctlr_vn_lookup(struct fcoe_ctlr *fip, u32 port_id, u8 *mac)
2561 struct fc_lport *lport = fip->lp;
2562 struct fc_rport_priv *rdata;
2563 struct fcoe_rport *frport;
2566 rdata = fc_rport_lookup(lport, port_id);
2568 frport = fcoe_ctlr_rport(rdata);
2569 memcpy(mac, frport->enode_mac, ETH_ALEN);
2571 kref_put(&rdata->kref, fc_rport_destroy);
2577 * fcoe_ctlr_vn_claim_notify() - handle received FIP VN2VN Claim Notification
2578 * @fip: The FCoE controller
2579 * @new: newly-parsed remote port with frport as a template for new rdata
2581 * Called with ctlr_mutex held.
2583 static void fcoe_ctlr_vn_claim_notify(struct fcoe_ctlr *fip,
2584 struct fc_rport_priv *new)
2586 struct fcoe_rport *frport = fcoe_ctlr_rport(new);
2588 if (frport->flags & FIP_FL_REC_OR_P2P) {
2589 LIBFCOE_FIP_DBG(fip, "send probe req for P2P/REC\n");
2590 fcoe_ctlr_vn_send(fip, FIP_SC_VN_PROBE_REQ, fcoe_all_vn2vn, 0);
2593 switch (fip->state) {
2594 case FIP_ST_VNMP_START:
2595 case FIP_ST_VNMP_PROBE1:
2596 case FIP_ST_VNMP_PROBE2:
2597 if (new->ids.port_id == fip->port_id) {
2598 LIBFCOE_FIP_DBG(fip, "vn_claim_notify: "
2599 "restart, state %d\n",
2601 fcoe_ctlr_vn_restart(fip);
2604 case FIP_ST_VNMP_CLAIM:
2605 case FIP_ST_VNMP_UP:
2606 if (new->ids.port_id == fip->port_id) {
2607 if (new->ids.port_name > fip->lp->wwpn) {
2608 LIBFCOE_FIP_DBG(fip, "vn_claim_notify: "
2609 "restart, port_id collision\n");
2610 fcoe_ctlr_vn_restart(fip);
2613 LIBFCOE_FIP_DBG(fip, "vn_claim_notify: "
2614 "send claim notify\n");
2615 fcoe_ctlr_vn_send_claim(fip);
2618 LIBFCOE_FIP_DBG(fip, "vn_claim_notify: send reply to %x\n",
2620 fcoe_ctlr_vn_send(fip, FIP_SC_VN_CLAIM_REP, frport->enode_mac,
2621 min((u32)frport->fcoe_len,
2622 fcoe_ctlr_fcoe_size(fip)));
2623 fcoe_ctlr_vn_add(fip, new);
2626 LIBFCOE_FIP_DBG(fip, "vn_claim_notify: "
2627 "ignoring claim from %x\n", new->ids.port_id);
2633 * fcoe_ctlr_vn_claim_resp() - handle received Claim Response
2634 * @fip: The FCoE controller that received the frame
2635 * @new: newly-parsed remote port with frport from the Claim Response
2637 * Called with ctlr_mutex held.
2639 static void fcoe_ctlr_vn_claim_resp(struct fcoe_ctlr *fip,
2640 struct fc_rport_priv *new)
2642 LIBFCOE_FIP_DBG(fip, "claim resp from from rport %x - state %s\n",
2643 new->ids.port_id, fcoe_ctlr_state(fip->state));
2644 if (fip->state == FIP_ST_VNMP_UP || fip->state == FIP_ST_VNMP_CLAIM)
2645 fcoe_ctlr_vn_add(fip, new);
2649 * fcoe_ctlr_vn_beacon() - handle received beacon.
2650 * @fip: The FCoE controller that received the frame
2651 * @new: newly-parsed remote port with frport from the Beacon
2653 * Called with ctlr_mutex held.
2655 static void fcoe_ctlr_vn_beacon(struct fcoe_ctlr *fip,
2656 struct fc_rport_priv *new)
2658 struct fc_lport *lport = fip->lp;
2659 struct fc_rport_priv *rdata;
2660 struct fcoe_rport *frport;
2662 frport = fcoe_ctlr_rport(new);
2663 if (frport->flags & FIP_FL_REC_OR_P2P) {
2664 LIBFCOE_FIP_DBG(fip, "p2p beacon while in vn2vn mode\n");
2665 fcoe_ctlr_vn_send(fip, FIP_SC_VN_PROBE_REQ, fcoe_all_vn2vn, 0);
2668 rdata = fc_rport_lookup(lport, new->ids.port_id);
2670 if (rdata->ids.node_name == new->ids.node_name &&
2671 rdata->ids.port_name == new->ids.port_name) {
2672 frport = fcoe_ctlr_rport(rdata);
2673 LIBFCOE_FIP_DBG(fip, "beacon from rport %x\n",
2674 rdata->ids.port_id);
2675 if (!frport->time && fip->state == FIP_ST_VNMP_UP) {
2676 LIBFCOE_FIP_DBG(fip, "beacon expired "
2678 rdata->ids.port_id);
2679 fc_rport_login(rdata);
2681 frport->time = jiffies;
2683 kref_put(&rdata->kref, fc_rport_destroy);
2686 if (fip->state != FIP_ST_VNMP_UP)
2690 * Beacon from a new neighbor.
2691 * Send a claim notify if one hasn't been sent recently.
2692 * Don't add the neighbor yet.
2694 LIBFCOE_FIP_DBG(fip, "beacon from new rport %x. sending claim notify\n",
2696 if (time_after(jiffies,
2697 fip->sol_time + msecs_to_jiffies(FIP_VN_ANN_WAIT)))
2698 fcoe_ctlr_vn_send_claim(fip);
2702 * fcoe_ctlr_vn_age() - Check for VN_ports without recent beacons
2703 * @fip: The FCoE controller
2705 * Called with ctlr_mutex held.
2706 * Called only in state FIP_ST_VNMP_UP.
2707 * Returns the soonest time for next age-out or a time far in the future.
2709 static unsigned long fcoe_ctlr_vn_age(struct fcoe_ctlr *fip)
2711 struct fc_lport *lport = fip->lp;
2712 struct fc_rport_priv *rdata;
2713 struct fcoe_rport *frport;
2714 unsigned long next_time;
2715 unsigned long deadline;
2717 next_time = jiffies + msecs_to_jiffies(FIP_VN_BEACON_INT * 10);
2718 mutex_lock(&lport->disc.disc_mutex);
2719 list_for_each_entry_rcu(rdata, &lport->disc.rports, peers) {
2720 if (!kref_get_unless_zero(&rdata->kref))
2722 frport = fcoe_ctlr_rport(rdata);
2723 if (!frport->time) {
2724 kref_put(&rdata->kref, fc_rport_destroy);
2727 deadline = frport->time +
2728 msecs_to_jiffies(FIP_VN_BEACON_INT * 25 / 10);
2729 if (time_after_eq(jiffies, deadline)) {
2731 LIBFCOE_FIP_DBG(fip,
2732 "port %16.16llx fc_id %6.6x beacon expired\n",
2733 rdata->ids.port_name, rdata->ids.port_id);
2734 fc_rport_logoff(rdata);
2735 } else if (time_before(deadline, next_time))
2736 next_time = deadline;
2737 kref_put(&rdata->kref, fc_rport_destroy);
2739 mutex_unlock(&lport->disc.disc_mutex);
2744 * fcoe_ctlr_vn_recv() - Receive a FIP frame
2745 * @fip: The FCoE controller that received the frame
2746 * @skb: The received FIP frame
2748 * Returns non-zero if the frame is dropped.
2749 * Always consumes the frame.
2751 static int fcoe_ctlr_vn_recv(struct fcoe_ctlr *fip, struct sk_buff *skb)
2753 struct fip_header *fiph;
2754 enum fip_vn2vn_subcode sub;
2755 struct fcoe_rport frport = { };
2756 int rc, vlan_id = 0;
2758 fiph = (struct fip_header *)skb->data;
2759 sub = fiph->fip_subcode;
2762 vlan_id = skb_vlan_tag_get_id(skb);
2764 if (vlan_id && vlan_id != fip->lp->vlan) {
2765 LIBFCOE_FIP_DBG(fip, "vn_recv drop frame sub %x vlan %d\n",
2771 rc = fcoe_ctlr_vn_parse(fip, skb, &frport);
2773 LIBFCOE_FIP_DBG(fip, "vn_recv vn_parse error %d\n", rc);
2777 mutex_lock(&fip->ctlr_mutex);
2779 case FIP_SC_VN_PROBE_REQ:
2780 fcoe_ctlr_vn_probe_req(fip, &frport.rdata);
2782 case FIP_SC_VN_PROBE_REP:
2783 fcoe_ctlr_vn_probe_reply(fip, &frport.rdata);
2785 case FIP_SC_VN_CLAIM_NOTIFY:
2786 fcoe_ctlr_vn_claim_notify(fip, &frport.rdata);
2788 case FIP_SC_VN_CLAIM_REP:
2789 fcoe_ctlr_vn_claim_resp(fip, &frport.rdata);
2791 case FIP_SC_VN_BEACON:
2792 fcoe_ctlr_vn_beacon(fip, &frport.rdata);
2795 LIBFCOE_FIP_DBG(fip, "vn_recv unknown subcode %d\n", sub);
2799 mutex_unlock(&fip->ctlr_mutex);
2806 * fcoe_ctlr_vlan_parse - parse vlan discovery request or response
2807 * @fip: The FCoE controller
2808 * @skb: incoming packet
2809 * @rdata: buffer for resulting parsed VLAN entry plus fcoe_rport
2811 * Returns non-zero error number on error.
2812 * Does not consume the packet.
2814 static int fcoe_ctlr_vlan_parse(struct fcoe_ctlr *fip,
2815 struct sk_buff *skb,
2816 struct fcoe_rport *frport)
2818 struct fip_header *fiph;
2819 struct fip_desc *desc = NULL;
2820 struct fip_mac_desc *macd = NULL;
2821 struct fip_wwn_desc *wwn = NULL;
2828 fiph = (struct fip_header *)skb->data;
2829 frport->flags = ntohs(fiph->fip_flags);
2831 sub = fiph->fip_subcode;
2834 desc_mask = BIT(FIP_DT_MAC) | BIT(FIP_DT_NAME);
2837 LIBFCOE_FIP_DBG(fip, "vn_parse unknown subcode %u\n", sub);
2841 rlen = ntohs(fiph->fip_dl_len) * 4;
2842 if (rlen + sizeof(*fiph) > skb->len)
2845 desc = (struct fip_desc *)(fiph + 1);
2847 dlen = desc->fip_dlen * FIP_BPW;
2848 if (dlen < sizeof(*desc) || dlen > rlen)
2851 dtype = desc->fip_dtype;
2853 if (!(desc_mask & BIT(dtype))) {
2854 LIBFCOE_FIP_DBG(fip,
2855 "unexpected or duplicated desc "
2857 "FIP VN2VN subtype %u\n",
2861 desc_mask &= ~BIT(dtype);
2866 if (dlen != sizeof(struct fip_mac_desc))
2868 macd = (struct fip_mac_desc *)desc;
2869 if (!is_valid_ether_addr(macd->fd_mac)) {
2870 LIBFCOE_FIP_DBG(fip,
2871 "Invalid MAC addr %pM in FIP VN2VN\n",
2875 memcpy(frport->enode_mac, macd->fd_mac, ETH_ALEN);
2878 if (dlen != sizeof(struct fip_wwn_desc))
2880 wwn = (struct fip_wwn_desc *)desc;
2881 frport->rdata.ids.node_name =
2882 get_unaligned_be64(&wwn->fd_wwn);
2885 LIBFCOE_FIP_DBG(fip, "unexpected descriptor type %x "
2886 "in FIP probe\n", dtype);
2887 /* standard says ignore unknown descriptors >= 128 */
2888 if (dtype < FIP_DT_NON_CRITICAL)
2892 desc = (struct fip_desc *)((char *)desc + dlen);
2898 LIBFCOE_FIP_DBG(fip, "FIP length error in descriptor type %x len %zu\n",
2904 * fcoe_ctlr_vlan_send() - Send a FIP VLAN Notification
2905 * @fip: The FCoE controller
2906 * @sub: sub-opcode for vlan notification or vn2vn vlan notification
2907 * @dest: The destination Ethernet MAC address
2908 * @min_len: minimum size of the Ethernet payload to be sent
2910 static void fcoe_ctlr_vlan_send(struct fcoe_ctlr *fip,
2911 enum fip_vlan_subcode sub,
2914 struct sk_buff *skb;
2915 struct fip_vlan_notify_frame {
2917 struct fip_header fip;
2918 struct fip_mac_desc mac;
2919 struct fip_vlan_desc vlan;
2924 len = sizeof(*frame);
2925 dlen = sizeof(frame->mac) + sizeof(frame->vlan);
2926 len = max(len, sizeof(struct ethhdr));
2928 skb = dev_alloc_skb(len);
2932 LIBFCOE_FIP_DBG(fip, "fip %s vlan notification, vlan %d\n",
2933 fip->mode == FIP_MODE_VN2VN ? "vn2vn" : "fcf",
2936 frame = (struct fip_vlan_notify_frame *)skb->data;
2937 memset(frame, 0, len);
2938 memcpy(frame->eth.h_dest, dest, ETH_ALEN);
2940 memcpy(frame->eth.h_source, fip->ctl_src_addr, ETH_ALEN);
2941 frame->eth.h_proto = htons(ETH_P_FIP);
2943 frame->fip.fip_ver = FIP_VER_ENCAPS(FIP_VER);
2944 frame->fip.fip_op = htons(FIP_OP_VLAN);
2945 frame->fip.fip_subcode = sub;
2946 frame->fip.fip_dl_len = htons(dlen / FIP_BPW);
2948 frame->mac.fd_desc.fip_dtype = FIP_DT_MAC;
2949 frame->mac.fd_desc.fip_dlen = sizeof(frame->mac) / FIP_BPW;
2950 memcpy(frame->mac.fd_mac, fip->ctl_src_addr, ETH_ALEN);
2952 frame->vlan.fd_desc.fip_dtype = FIP_DT_VLAN;
2953 frame->vlan.fd_desc.fip_dlen = sizeof(frame->vlan) / FIP_BPW;
2954 put_unaligned_be16(fip->lp->vlan, &frame->vlan.fd_vlan);
2957 skb->protocol = htons(ETH_P_FIP);
2958 skb->priority = fip->priority;
2959 skb_reset_mac_header(skb);
2960 skb_reset_network_header(skb);
2962 fip->send(fip, skb);
2966 * fcoe_ctlr_vlan_disk_reply() - send FIP VLAN Discovery Notification.
2967 * @fip: The FCoE controller
2969 * Called with ctlr_mutex held.
2971 static void fcoe_ctlr_vlan_disc_reply(struct fcoe_ctlr *fip,
2972 struct fc_rport_priv *rdata)
2974 struct fcoe_rport *frport = fcoe_ctlr_rport(rdata);
2975 enum fip_vlan_subcode sub = FIP_SC_VL_NOTE;
2977 if (fip->mode == FIP_MODE_VN2VN)
2978 sub = FIP_SC_VL_VN2VN_NOTE;
2980 fcoe_ctlr_vlan_send(fip, sub, frport->enode_mac);
2984 * fcoe_ctlr_vlan_recv - vlan request receive handler for VN2VN mode.
2985 * @lport: The local port
2986 * @fp: The received frame
2989 static int fcoe_ctlr_vlan_recv(struct fcoe_ctlr *fip, struct sk_buff *skb)
2991 struct fip_header *fiph;
2992 enum fip_vlan_subcode sub;
2993 struct fcoe_rport frport = { };
2996 fiph = (struct fip_header *)skb->data;
2997 sub = fiph->fip_subcode;
2998 rc = fcoe_ctlr_vlan_parse(fip, skb, &frport);
3000 LIBFCOE_FIP_DBG(fip, "vlan_recv vlan_parse error %d\n", rc);
3003 mutex_lock(&fip->ctlr_mutex);
3004 if (sub == FIP_SC_VL_REQ)
3005 fcoe_ctlr_vlan_disc_reply(fip, &frport.rdata);
3006 mutex_unlock(&fip->ctlr_mutex);
3014 * fcoe_ctlr_disc_recv - discovery receive handler for VN2VN mode.
3015 * @lport: The local port
3016 * @fp: The received frame
3018 * This should never be called since we don't see RSCNs or other
3019 * fabric-generated ELSes.
3021 static void fcoe_ctlr_disc_recv(struct fc_lport *lport, struct fc_frame *fp)
3023 struct fc_seq_els_data rjt_data;
3025 rjt_data.reason = ELS_RJT_UNSUP;
3026 rjt_data.explan = ELS_EXPL_NONE;
3027 fc_seq_els_rsp_send(fp, ELS_LS_RJT, &rjt_data);
3032 * fcoe_ctlr_disc_recv - start discovery for VN2VN mode.
3033 * @fip: The FCoE controller
3035 * This sets a flag indicating that remote ports should be created
3036 * and started for the peers we discover. We use the disc_callback
3037 * pointer as that flag. Peers already discovered are created here.
3039 * The lport lock is held during this call. The callback must be done
3040 * later, without holding either the lport or discovery locks.
3041 * The fcoe_ctlr lock may also be held during this call.
3043 static void fcoe_ctlr_disc_start(void (*callback)(struct fc_lport *,
3044 enum fc_disc_event),
3045 struct fc_lport *lport)
3047 struct fc_disc *disc = &lport->disc;
3048 struct fcoe_ctlr *fip = disc->priv;
3050 mutex_lock(&disc->disc_mutex);
3051 disc->disc_callback = callback;
3052 disc->disc_id = (disc->disc_id + 2) | 1;
3054 schedule_work(&fip->timer_work);
3055 mutex_unlock(&disc->disc_mutex);
3059 * fcoe_ctlr_vn_disc() - report FIP VN_port discovery results after claim state.
3060 * @fip: The FCoE controller
3062 * Starts the FLOGI and PLOGI login process to each discovered rport for which
3063 * we've received at least one beacon.
3064 * Performs the discovery complete callback.
3066 static void fcoe_ctlr_vn_disc(struct fcoe_ctlr *fip)
3068 struct fc_lport *lport = fip->lp;
3069 struct fc_disc *disc = &lport->disc;
3070 struct fc_rport_priv *rdata;
3071 struct fcoe_rport *frport;
3072 void (*callback)(struct fc_lport *, enum fc_disc_event);
3074 mutex_lock(&disc->disc_mutex);
3075 callback = disc->pending ? disc->disc_callback : NULL;
3077 list_for_each_entry_rcu(rdata, &disc->rports, peers) {
3078 if (!kref_get_unless_zero(&rdata->kref))
3080 frport = fcoe_ctlr_rport(rdata);
3082 fc_rport_login(rdata);
3083 kref_put(&rdata->kref, fc_rport_destroy);
3085 mutex_unlock(&disc->disc_mutex);
3087 callback(lport, DISC_EV_SUCCESS);
3091 * fcoe_ctlr_vn_timeout - timer work function for VN2VN mode.
3092 * @fip: The FCoE controller
3094 static void fcoe_ctlr_vn_timeout(struct fcoe_ctlr *fip)
3096 unsigned long next_time;
3098 u32 new_port_id = 0;
3100 mutex_lock(&fip->ctlr_mutex);
3101 switch (fip->state) {
3102 case FIP_ST_VNMP_START:
3103 fcoe_ctlr_set_state(fip, FIP_ST_VNMP_PROBE1);
3104 LIBFCOE_FIP_DBG(fip, "vn_timeout: send 1st probe request\n");
3105 fcoe_ctlr_vn_send(fip, FIP_SC_VN_PROBE_REQ, fcoe_all_vn2vn, 0);
3106 next_time = jiffies + msecs_to_jiffies(FIP_VN_PROBE_WAIT);
3108 case FIP_ST_VNMP_PROBE1:
3109 fcoe_ctlr_set_state(fip, FIP_ST_VNMP_PROBE2);
3110 LIBFCOE_FIP_DBG(fip, "vn_timeout: send 2nd probe request\n");
3111 fcoe_ctlr_vn_send(fip, FIP_SC_VN_PROBE_REQ, fcoe_all_vn2vn, 0);
3112 next_time = jiffies + msecs_to_jiffies(FIP_VN_ANN_WAIT);
3114 case FIP_ST_VNMP_PROBE2:
3115 fcoe_ctlr_set_state(fip, FIP_ST_VNMP_CLAIM);
3116 new_port_id = fip->port_id;
3117 hton24(mac, FIP_VN_FC_MAP);
3118 hton24(mac + 3, new_port_id);
3119 fcoe_ctlr_map_dest(fip);
3120 fip->update_mac(fip->lp, mac);
3121 LIBFCOE_FIP_DBG(fip, "vn_timeout: send claim notify\n");
3122 fcoe_ctlr_vn_send_claim(fip);
3123 next_time = jiffies + msecs_to_jiffies(FIP_VN_ANN_WAIT);
3125 case FIP_ST_VNMP_CLAIM:
3127 * This may be invoked either by starting discovery so don't
3128 * go to the next state unless it's been long enough.
3130 next_time = fip->sol_time + msecs_to_jiffies(FIP_VN_ANN_WAIT);
3131 if (time_after_eq(jiffies, next_time)) {
3132 fcoe_ctlr_set_state(fip, FIP_ST_VNMP_UP);
3133 LIBFCOE_FIP_DBG(fip, "vn_timeout: send vn2vn beacon\n");
3134 fcoe_ctlr_vn_send(fip, FIP_SC_VN_BEACON,
3136 next_time = jiffies + msecs_to_jiffies(FIP_VN_ANN_WAIT);
3137 fip->port_ka_time = next_time;
3139 fcoe_ctlr_vn_disc(fip);
3141 case FIP_ST_VNMP_UP:
3142 next_time = fcoe_ctlr_vn_age(fip);
3143 if (time_after_eq(jiffies, fip->port_ka_time)) {
3144 LIBFCOE_FIP_DBG(fip, "vn_timeout: send vn2vn beacon\n");
3145 fcoe_ctlr_vn_send(fip, FIP_SC_VN_BEACON,
3147 fip->port_ka_time = jiffies +
3148 msecs_to_jiffies(FIP_VN_BEACON_INT +
3149 (prandom_u32() % FIP_VN_BEACON_FUZZ));
3151 if (time_before(fip->port_ka_time, next_time))
3152 next_time = fip->port_ka_time;
3154 case FIP_ST_LINK_WAIT:
3157 WARN(1, "unexpected state %d\n", fip->state);
3160 mod_timer(&fip->timer, next_time);
3162 mutex_unlock(&fip->ctlr_mutex);
3164 /* If port ID is new, notify local port after dropping ctlr_mutex */
3166 fc_lport_set_local_id(fip->lp, new_port_id);
3170 * fcoe_ctlr_mode_set() - Set or reset the ctlr's mode
3171 * @lport: The local port to be (re)configured
3172 * @fip: The FCoE controller whose mode is changing
3173 * @fip_mode: The new fip mode
3175 * Note that the we shouldn't be changing the libfc discovery settings
3176 * (fc_disc_config) while an lport is going through the libfc state
3177 * machine. The mode can only be changed when a fcoe_ctlr device is
3178 * disabled, so that should ensure that this routine is only called
3179 * when nothing is happening.
3181 static void fcoe_ctlr_mode_set(struct fc_lport *lport, struct fcoe_ctlr *fip,
3182 enum fip_mode fip_mode)
3186 WARN_ON(lport->state != LPORT_ST_RESET &&
3187 lport->state != LPORT_ST_DISABLED);
3189 if (fip_mode == FIP_MODE_VN2VN) {
3190 lport->rport_priv_size = sizeof(struct fcoe_rport);
3191 lport->point_to_multipoint = 1;
3192 lport->tt.disc_recv_req = fcoe_ctlr_disc_recv;
3193 lport->tt.disc_start = fcoe_ctlr_disc_start;
3194 lport->tt.disc_stop = fcoe_ctlr_disc_stop;
3195 lport->tt.disc_stop_final = fcoe_ctlr_disc_stop_final;
3198 lport->rport_priv_size = 0;
3199 lport->point_to_multipoint = 0;
3200 lport->tt.disc_recv_req = NULL;
3201 lport->tt.disc_start = NULL;
3202 lport->tt.disc_stop = NULL;
3203 lport->tt.disc_stop_final = NULL;
3207 fc_disc_config(lport, priv);
3211 * fcoe_libfc_config() - Sets up libfc related properties for local port
3212 * @lport: The local port to configure libfc for
3213 * @fip: The FCoE controller in use by the local port
3214 * @tt: The libfc function template
3215 * @init_fcp: If non-zero, the FCP portion of libfc should be initialized
3217 * Returns : 0 for success
3219 int fcoe_libfc_config(struct fc_lport *lport, struct fcoe_ctlr *fip,
3220 const struct libfc_function_template *tt, int init_fcp)
3222 /* Set the function pointers set by the LLDD */
3223 memcpy(&lport->tt, tt, sizeof(*tt));
3224 if (init_fcp && fc_fcp_init(lport))
3226 fc_exch_init(lport);
3227 fc_elsct_init(lport);
3228 fc_lport_init(lport);
3229 fc_disc_init(lport);
3230 fcoe_ctlr_mode_set(lport, fip, fip->mode);
3233 EXPORT_SYMBOL_GPL(fcoe_libfc_config);
3235 void fcoe_fcf_get_selected(struct fcoe_fcf_device *fcf_dev)
3237 struct fcoe_ctlr_device *ctlr_dev = fcoe_fcf_dev_to_ctlr_dev(fcf_dev);
3238 struct fcoe_ctlr *fip = fcoe_ctlr_device_priv(ctlr_dev);
3239 struct fcoe_fcf *fcf;
3241 mutex_lock(&fip->ctlr_mutex);
3242 mutex_lock(&ctlr_dev->lock);
3244 fcf = fcoe_fcf_device_priv(fcf_dev);
3246 fcf_dev->selected = (fcf == fip->sel_fcf) ? 1 : 0;
3248 fcf_dev->selected = 0;
3250 mutex_unlock(&ctlr_dev->lock);
3251 mutex_unlock(&fip->ctlr_mutex);
3253 EXPORT_SYMBOL(fcoe_fcf_get_selected);
3255 void fcoe_ctlr_set_fip_mode(struct fcoe_ctlr_device *ctlr_dev)
3257 struct fcoe_ctlr *ctlr = fcoe_ctlr_device_priv(ctlr_dev);
3258 struct fc_lport *lport = ctlr->lp;
3260 mutex_lock(&ctlr->ctlr_mutex);
3261 switch (ctlr_dev->mode) {
3262 case FIP_CONN_TYPE_VN2VN:
3263 ctlr->mode = FIP_MODE_VN2VN;
3265 case FIP_CONN_TYPE_FABRIC:
3267 ctlr->mode = FIP_MODE_FABRIC;
3271 mutex_unlock(&ctlr->ctlr_mutex);
3273 fcoe_ctlr_mode_set(lport, ctlr, ctlr->mode);
3275 EXPORT_SYMBOL(fcoe_ctlr_set_fip_mode);