1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (c) 2008-2009 Cisco Systems, Inc. All rights reserved.
4 * Copyright (c) 2009 Intel Corporation. All rights reserved.
6 * Maintained at www.Open-FCoE.org
9 #include <linux/types.h>
10 #include <linux/module.h>
11 #include <linux/kernel.h>
12 #include <linux/list.h>
13 #include <linux/spinlock.h>
14 #include <linux/timer.h>
15 #include <linux/netdevice.h>
16 #include <linux/etherdevice.h>
17 #include <linux/ethtool.h>
18 #include <linux/if_ether.h>
19 #include <linux/if_vlan.h>
20 #include <linux/errno.h>
21 #include <linux/bitops.h>
22 #include <linux/slab.h>
23 #include <net/rtnetlink.h>
25 #include <scsi/fc/fc_els.h>
26 #include <scsi/fc/fc_fs.h>
27 #include <scsi/fc/fc_fip.h>
28 #include <scsi/fc/fc_encaps.h>
29 #include <scsi/fc/fc_fcoe.h>
30 #include <scsi/fc/fc_fcp.h>
32 #include <scsi/libfc.h>
33 #include <scsi/libfcoe.h>
37 #define FCOE_CTLR_MIN_FKA 500 /* min keep alive (mS) */
38 #define FCOE_CTLR_DEF_FKA FIP_DEF_FKA /* default keep alive (mS) */
40 static void fcoe_ctlr_timeout(struct timer_list *);
41 static void fcoe_ctlr_timer_work(struct work_struct *);
42 static void fcoe_ctlr_recv_work(struct work_struct *);
43 static int fcoe_ctlr_flogi_retry(struct fcoe_ctlr *);
45 static void fcoe_ctlr_vn_start(struct fcoe_ctlr *);
46 static int fcoe_ctlr_vn_recv(struct fcoe_ctlr *, struct sk_buff *);
47 static void fcoe_ctlr_vn_timeout(struct fcoe_ctlr *);
48 static int fcoe_ctlr_vn_lookup(struct fcoe_ctlr *, u32, u8 *);
50 static int fcoe_ctlr_vlan_recv(struct fcoe_ctlr *, struct sk_buff *);
52 static u8 fcoe_all_fcfs[ETH_ALEN] = FIP_ALL_FCF_MACS;
53 static u8 fcoe_all_enode[ETH_ALEN] = FIP_ALL_ENODE_MACS;
54 static u8 fcoe_all_vn2vn[ETH_ALEN] = FIP_ALL_VN2VN_MACS;
55 static u8 fcoe_all_p2p[ETH_ALEN] = FIP_ALL_P2P_MACS;
57 static const char * const fcoe_ctlr_states[] = {
58 [FIP_ST_DISABLED] = "DISABLED",
59 [FIP_ST_LINK_WAIT] = "LINK_WAIT",
60 [FIP_ST_AUTO] = "AUTO",
61 [FIP_ST_NON_FIP] = "NON_FIP",
62 [FIP_ST_ENABLED] = "ENABLED",
63 [FIP_ST_VNMP_START] = "VNMP_START",
64 [FIP_ST_VNMP_PROBE1] = "VNMP_PROBE1",
65 [FIP_ST_VNMP_PROBE2] = "VNMP_PROBE2",
66 [FIP_ST_VNMP_CLAIM] = "VNMP_CLAIM",
67 [FIP_ST_VNMP_UP] = "VNMP_UP",
70 static const char *fcoe_ctlr_state(enum fip_state state)
72 const char *cp = "unknown";
74 if (state < ARRAY_SIZE(fcoe_ctlr_states))
75 cp = fcoe_ctlr_states[state];
82 * fcoe_ctlr_set_state() - Set and do debug printing for the new FIP state.
83 * @fip: The FCoE controller
84 * @state: The new state
86 static void fcoe_ctlr_set_state(struct fcoe_ctlr *fip, enum fip_state state)
88 if (state == fip->state)
91 LIBFCOE_FIP_DBG(fip, "state %s -> %s\n",
92 fcoe_ctlr_state(fip->state), fcoe_ctlr_state(state));
97 * fcoe_ctlr_mtu_valid() - Check if a FCF's MTU is valid
98 * @fcf: The FCF to check
100 * Return non-zero if FCF fcoe_size has been validated.
102 static inline int fcoe_ctlr_mtu_valid(const struct fcoe_fcf *fcf)
104 return (fcf->flags & FIP_FL_SOL) != 0;
108 * fcoe_ctlr_fcf_usable() - Check if a FCF is usable
109 * @fcf: The FCF to check
111 * Return non-zero if the FCF is usable.
113 static inline int fcoe_ctlr_fcf_usable(struct fcoe_fcf *fcf)
115 u16 flags = FIP_FL_SOL | FIP_FL_AVAIL;
117 return (fcf->flags & flags) == flags;
121 * fcoe_ctlr_map_dest() - Set flag and OUI for mapping destination addresses
122 * @fip: The FCoE controller
124 static void fcoe_ctlr_map_dest(struct fcoe_ctlr *fip)
126 if (fip->mode == FIP_MODE_VN2VN)
127 hton24(fip->dest_addr, FIP_VN_FC_MAP);
129 hton24(fip->dest_addr, FIP_DEF_FC_MAP);
130 hton24(fip->dest_addr + 3, 0);
135 * fcoe_ctlr_init() - Initialize the FCoE Controller instance
136 * @fip: The FCoE controller to initialize
137 * @mode: FIP mode to set
139 void fcoe_ctlr_init(struct fcoe_ctlr *fip, enum fip_mode mode)
141 fcoe_ctlr_set_state(fip, FIP_ST_LINK_WAIT);
143 fip->fip_resp = false;
144 INIT_LIST_HEAD(&fip->fcfs);
145 mutex_init(&fip->ctlr_mutex);
146 spin_lock_init(&fip->ctlr_lock);
147 fip->flogi_oxid = FC_XID_UNKNOWN;
148 timer_setup(&fip->timer, fcoe_ctlr_timeout, 0);
149 INIT_WORK(&fip->timer_work, fcoe_ctlr_timer_work);
150 INIT_WORK(&fip->recv_work, fcoe_ctlr_recv_work);
151 skb_queue_head_init(&fip->fip_recv_list);
153 EXPORT_SYMBOL(fcoe_ctlr_init);
156 * fcoe_sysfs_fcf_add() - Add a fcoe_fcf{,_device} to a fcoe_ctlr{,_device}
157 * @new: The newly discovered FCF
159 * Called with fip->ctlr_mutex held
161 static int fcoe_sysfs_fcf_add(struct fcoe_fcf *new)
163 struct fcoe_ctlr *fip = new->fip;
164 struct fcoe_ctlr_device *ctlr_dev;
165 struct fcoe_fcf_device *temp, *fcf_dev;
168 LIBFCOE_FIP_DBG(fip, "New FCF fab %16.16llx mac %pM\n",
169 new->fabric_name, new->fcf_mac);
171 temp = kzalloc(sizeof(*temp), GFP_KERNEL);
175 temp->fabric_name = new->fabric_name;
176 temp->switch_name = new->switch_name;
177 temp->fc_map = new->fc_map;
178 temp->vfid = new->vfid;
179 memcpy(temp->mac, new->fcf_mac, ETH_ALEN);
180 temp->priority = new->pri;
181 temp->fka_period = new->fka_period;
182 temp->selected = 0; /* default to unselected */
185 * If ctlr_dev doesn't exist then it means we're a libfcoe user
186 * who doesn't use fcoe_syfs and didn't allocate a fcoe_ctlr_device.
187 * fnic would be an example of a driver with this behavior. In this
188 * case we want to add the fcoe_fcf to the fcoe_ctlr list, but we
189 * don't want to make sysfs changes.
192 ctlr_dev = fcoe_ctlr_to_ctlr_dev(fip);
194 mutex_lock(&ctlr_dev->lock);
195 fcf_dev = fcoe_fcf_device_add(ctlr_dev, temp);
196 if (unlikely(!fcf_dev)) {
198 mutex_unlock(&ctlr_dev->lock);
203 * The fcoe_sysfs layer can return a CONNECTED fcf that
204 * has a priv (fcf was never deleted) or a CONNECTED fcf
205 * that doesn't have a priv (fcf was deleted). However,
206 * libfcoe will always delete FCFs before trying to add
207 * them. This is ensured because both recv_adv and
208 * age_fcfs are protected by the the fcoe_ctlr's mutex.
209 * This means that we should never get a FCF with a
210 * non-NULL priv pointer.
212 BUG_ON(fcf_dev->priv);
215 new->fcf_dev = fcf_dev;
216 mutex_unlock(&ctlr_dev->lock);
219 list_add(&new->list, &fip->fcfs);
229 * fcoe_sysfs_fcf_del() - Remove a fcoe_fcf{,_device} to a fcoe_ctlr{,_device}
230 * @new: The FCF to be removed
232 * Called with fip->ctlr_mutex held
234 static void fcoe_sysfs_fcf_del(struct fcoe_fcf *new)
236 struct fcoe_ctlr *fip = new->fip;
237 struct fcoe_ctlr_device *cdev;
238 struct fcoe_fcf_device *fcf_dev;
240 list_del(&new->list);
244 * If ctlr_dev doesn't exist then it means we're a libfcoe user
245 * who doesn't use fcoe_syfs and didn't allocate a fcoe_ctlr_device
246 * or a fcoe_fcf_device.
248 * fnic would be an example of a driver with this behavior. In this
249 * case we want to remove the fcoe_fcf from the fcoe_ctlr list (above),
250 * but we don't want to make sysfs changes.
252 cdev = fcoe_ctlr_to_ctlr_dev(fip);
254 mutex_lock(&cdev->lock);
255 fcf_dev = fcoe_fcf_to_fcf_dev(new);
258 fcoe_fcf_device_delete(fcf_dev);
259 mutex_unlock(&cdev->lock);
265 * fcoe_ctlr_reset_fcfs() - Reset and free all FCFs for a controller
266 * @fip: The FCoE controller whose FCFs are to be reset
268 * Called with &fcoe_ctlr lock held.
270 static void fcoe_ctlr_reset_fcfs(struct fcoe_ctlr *fip)
272 struct fcoe_fcf *fcf;
273 struct fcoe_fcf *next;
276 list_for_each_entry_safe(fcf, next, &fip->fcfs, list) {
277 fcoe_sysfs_fcf_del(fcf);
279 WARN_ON(fip->fcf_count);
285 * fcoe_ctlr_destroy() - Disable and tear down a FCoE controller
286 * @fip: The FCoE controller to tear down
288 * This is called by FCoE drivers before freeing the &fcoe_ctlr.
290 * The receive handler will have been deleted before this to guarantee
291 * that no more recv_work will be scheduled.
293 * The timer routine will simply return once we set FIP_ST_DISABLED.
294 * This guarantees that no further timeouts or work will be scheduled.
296 void fcoe_ctlr_destroy(struct fcoe_ctlr *fip)
298 cancel_work_sync(&fip->recv_work);
299 skb_queue_purge(&fip->fip_recv_list);
301 mutex_lock(&fip->ctlr_mutex);
302 fcoe_ctlr_set_state(fip, FIP_ST_DISABLED);
303 fcoe_ctlr_reset_fcfs(fip);
304 mutex_unlock(&fip->ctlr_mutex);
305 del_timer_sync(&fip->timer);
306 cancel_work_sync(&fip->timer_work);
308 EXPORT_SYMBOL(fcoe_ctlr_destroy);
311 * fcoe_ctlr_announce() - announce new FCF selection
312 * @fip: The FCoE controller
314 * Also sets the destination MAC for FCoE and control packets
316 * Called with neither ctlr_mutex nor ctlr_lock held.
318 static void fcoe_ctlr_announce(struct fcoe_ctlr *fip)
320 struct fcoe_fcf *sel;
321 struct fcoe_fcf *fcf;
324 mutex_lock(&fip->ctlr_mutex);
325 spin_lock_irqsave(&fip->ctlr_lock, flags);
327 kfree_skb(fip->flogi_req);
328 fip->flogi_req = NULL;
329 list_for_each_entry(fcf, &fip->fcfs, list)
332 spin_unlock_irqrestore(&fip->ctlr_lock, flags);
335 if (sel && ether_addr_equal(sel->fcf_mac, fip->dest_addr))
337 if (!is_zero_ether_addr(fip->dest_addr)) {
338 printk(KERN_NOTICE "libfcoe: host%d: "
339 "FIP Fibre-Channel Forwarder MAC %pM deselected\n",
340 fip->lp->host->host_no, fip->dest_addr);
341 eth_zero_addr(fip->dest_addr);
344 printk(KERN_INFO "libfcoe: host%d: FIP selected "
345 "Fibre-Channel Forwarder MAC %pM\n",
346 fip->lp->host->host_no, sel->fcf_mac);
347 memcpy(fip->dest_addr, sel->fcoe_mac, ETH_ALEN);
351 mutex_unlock(&fip->ctlr_mutex);
355 * fcoe_ctlr_fcoe_size() - Return the maximum FCoE size required for VN_Port
356 * @fip: The FCoE controller to get the maximum FCoE size from
358 * Returns the maximum packet size including the FCoE header and trailer,
359 * but not including any Ethernet or VLAN headers.
361 static inline u32 fcoe_ctlr_fcoe_size(struct fcoe_ctlr *fip)
364 * Determine the max FCoE frame size allowed, including
365 * FCoE header and trailer.
366 * Note: lp->mfs is currently the payload size, not the frame size.
368 return fip->lp->mfs + sizeof(struct fc_frame_header) +
369 sizeof(struct fcoe_hdr) + sizeof(struct fcoe_crc_eof);
373 * fcoe_ctlr_solicit() - Send a FIP solicitation
374 * @fip: The FCoE controller to send the solicitation on
375 * @fcf: The destination FCF (if NULL, a multicast solicitation is sent)
377 static void fcoe_ctlr_solicit(struct fcoe_ctlr *fip, struct fcoe_fcf *fcf)
382 struct fip_header fip;
384 struct fip_mac_desc mac;
385 struct fip_wwn_desc wwnn;
386 struct fip_size_desc size;
391 skb = dev_alloc_skb(sizeof(*sol));
395 sol = (struct fip_sol *)skb->data;
397 memset(sol, 0, sizeof(*sol));
398 memcpy(sol->eth.h_dest, fcf ? fcf->fcf_mac : fcoe_all_fcfs, ETH_ALEN);
399 memcpy(sol->eth.h_source, fip->ctl_src_addr, ETH_ALEN);
400 sol->eth.h_proto = htons(ETH_P_FIP);
402 sol->fip.fip_ver = FIP_VER_ENCAPS(FIP_VER);
403 sol->fip.fip_op = htons(FIP_OP_DISC);
404 sol->fip.fip_subcode = FIP_SC_SOL;
405 sol->fip.fip_dl_len = htons(sizeof(sol->desc) / FIP_BPW);
406 sol->fip.fip_flags = htons(FIP_FL_FPMA);
408 sol->fip.fip_flags |= htons(FIP_FL_SPMA);
410 sol->desc.mac.fd_desc.fip_dtype = FIP_DT_MAC;
411 sol->desc.mac.fd_desc.fip_dlen = sizeof(sol->desc.mac) / FIP_BPW;
412 memcpy(sol->desc.mac.fd_mac, fip->ctl_src_addr, ETH_ALEN);
414 sol->desc.wwnn.fd_desc.fip_dtype = FIP_DT_NAME;
415 sol->desc.wwnn.fd_desc.fip_dlen = sizeof(sol->desc.wwnn) / FIP_BPW;
416 put_unaligned_be64(fip->lp->wwnn, &sol->desc.wwnn.fd_wwn);
418 fcoe_size = fcoe_ctlr_fcoe_size(fip);
419 sol->desc.size.fd_desc.fip_dtype = FIP_DT_FCOE_SIZE;
420 sol->desc.size.fd_desc.fip_dlen = sizeof(sol->desc.size) / FIP_BPW;
421 sol->desc.size.fd_size = htons(fcoe_size);
423 skb_put(skb, sizeof(*sol));
424 skb->protocol = htons(ETH_P_FIP);
425 skb->priority = fip->priority;
426 skb_reset_mac_header(skb);
427 skb_reset_network_header(skb);
431 fip->sol_time = jiffies;
435 * fcoe_ctlr_link_up() - Start FCoE controller
436 * @fip: The FCoE controller to start
438 * Called from the LLD when the network link is ready.
440 void fcoe_ctlr_link_up(struct fcoe_ctlr *fip)
442 mutex_lock(&fip->ctlr_mutex);
443 if (fip->state == FIP_ST_NON_FIP || fip->state == FIP_ST_AUTO) {
444 mutex_unlock(&fip->ctlr_mutex);
446 } else if (fip->state == FIP_ST_LINK_WAIT) {
447 if (fip->mode == FIP_MODE_NON_FIP)
448 fcoe_ctlr_set_state(fip, FIP_ST_NON_FIP);
450 fcoe_ctlr_set_state(fip, FIP_ST_AUTO);
453 LIBFCOE_FIP_DBG(fip, "invalid mode %d\n", fip->mode);
456 LIBFCOE_FIP_DBG(fip, "%s", "setting AUTO mode.\n");
458 case FIP_MODE_FABRIC:
459 case FIP_MODE_NON_FIP:
460 mutex_unlock(&fip->ctlr_mutex);
462 fcoe_ctlr_solicit(fip, NULL);
465 fcoe_ctlr_vn_start(fip);
466 mutex_unlock(&fip->ctlr_mutex);
471 mutex_unlock(&fip->ctlr_mutex);
473 EXPORT_SYMBOL(fcoe_ctlr_link_up);
476 * fcoe_ctlr_reset() - Reset a FCoE controller
477 * @fip: The FCoE controller to reset
479 static void fcoe_ctlr_reset(struct fcoe_ctlr *fip)
481 fcoe_ctlr_reset_fcfs(fip);
482 del_timer(&fip->timer);
483 fip->ctlr_ka_time = 0;
484 fip->port_ka_time = 0;
486 fip->flogi_oxid = FC_XID_UNKNOWN;
487 fcoe_ctlr_map_dest(fip);
491 * fcoe_ctlr_link_down() - Stop a FCoE controller
492 * @fip: The FCoE controller to be stopped
494 * Returns non-zero if the link was up and now isn't.
496 * Called from the LLD when the network link is not ready.
497 * There may be multiple calls while the link is down.
499 int fcoe_ctlr_link_down(struct fcoe_ctlr *fip)
503 LIBFCOE_FIP_DBG(fip, "link down.\n");
504 mutex_lock(&fip->ctlr_mutex);
505 fcoe_ctlr_reset(fip);
506 link_dropped = fip->state != FIP_ST_LINK_WAIT;
507 fcoe_ctlr_set_state(fip, FIP_ST_LINK_WAIT);
508 mutex_unlock(&fip->ctlr_mutex);
511 fc_linkdown(fip->lp);
514 EXPORT_SYMBOL(fcoe_ctlr_link_down);
517 * fcoe_ctlr_send_keep_alive() - Send a keep-alive to the selected FCF
518 * @fip: The FCoE controller to send the FKA on
519 * @lport: libfc fc_lport to send from
520 * @ports: 0 for controller keep-alive, 1 for port keep-alive
521 * @sa: The source MAC address
523 * A controller keep-alive is sent every fka_period (typically 8 seconds).
524 * The source MAC is the native MAC address.
526 * A port keep-alive is sent every 90 seconds while logged in.
527 * The source MAC is the assigned mapped source address.
528 * The destination is the FCF's F-port.
530 static void fcoe_ctlr_send_keep_alive(struct fcoe_ctlr *fip,
531 struct fc_lport *lport,
537 struct fip_header fip;
538 struct fip_mac_desc mac;
540 struct fip_vn_desc *vn;
543 struct fcoe_fcf *fcf;
547 if (!fcf || (ports && !lp->port_id))
550 len = sizeof(*kal) + ports * sizeof(*vn);
551 skb = dev_alloc_skb(len);
555 kal = (struct fip_kal *)skb->data;
557 memcpy(kal->eth.h_dest, fcf->fcf_mac, ETH_ALEN);
558 memcpy(kal->eth.h_source, sa, ETH_ALEN);
559 kal->eth.h_proto = htons(ETH_P_FIP);
561 kal->fip.fip_ver = FIP_VER_ENCAPS(FIP_VER);
562 kal->fip.fip_op = htons(FIP_OP_CTRL);
563 kal->fip.fip_subcode = FIP_SC_KEEP_ALIVE;
564 kal->fip.fip_dl_len = htons((sizeof(kal->mac) +
565 ports * sizeof(*vn)) / FIP_BPW);
566 kal->fip.fip_flags = htons(FIP_FL_FPMA);
568 kal->fip.fip_flags |= htons(FIP_FL_SPMA);
570 kal->mac.fd_desc.fip_dtype = FIP_DT_MAC;
571 kal->mac.fd_desc.fip_dlen = sizeof(kal->mac) / FIP_BPW;
572 memcpy(kal->mac.fd_mac, fip->ctl_src_addr, ETH_ALEN);
574 vn = (struct fip_vn_desc *)(kal + 1);
575 vn->fd_desc.fip_dtype = FIP_DT_VN_ID;
576 vn->fd_desc.fip_dlen = sizeof(*vn) / FIP_BPW;
577 memcpy(vn->fd_mac, fip->get_src_addr(lport), ETH_ALEN);
578 hton24(vn->fd_fc_id, lport->port_id);
579 put_unaligned_be64(lport->wwpn, &vn->fd_wwpn);
582 skb->protocol = htons(ETH_P_FIP);
583 skb->priority = fip->priority;
584 skb_reset_mac_header(skb);
585 skb_reset_network_header(skb);
590 * fcoe_ctlr_encaps() - Encapsulate an ELS frame for FIP, without sending it
591 * @fip: The FCoE controller for the ELS frame
592 * @lport: The local port
593 * @dtype: The FIP descriptor type for the frame
594 * @skb: The FCoE ELS frame including FC header but no FCoE headers
595 * @d_id: The destination port ID.
597 * Returns non-zero error code on failure.
599 * The caller must check that the length is a multiple of 4.
601 * The @skb must have enough headroom (28 bytes) and tailroom (8 bytes).
602 * Headroom includes the FIP encapsulation description, FIP header, and
603 * Ethernet header. The tailroom is for the FIP MAC descriptor.
605 static int fcoe_ctlr_encaps(struct fcoe_ctlr *fip, struct fc_lport *lport,
606 u8 dtype, struct sk_buff *skb, u32 d_id)
608 struct fip_encaps_head {
610 struct fip_header fip;
611 struct fip_encaps encaps;
613 struct fc_frame_header *fh;
614 struct fip_mac_desc *mac;
615 struct fcoe_fcf *fcf;
620 fh = (struct fc_frame_header *)skb->data;
621 op = *(u8 *)(fh + 1);
622 dlen = sizeof(struct fip_encaps) + skb->len; /* len before push */
623 cap = skb_push(skb, sizeof(*cap));
624 memset(cap, 0, sizeof(*cap));
626 if (lport->point_to_multipoint) {
627 if (fcoe_ctlr_vn_lookup(fip, d_id, cap->eth.h_dest))
634 fip_flags = fcf->flags;
635 fip_flags &= fip->spma ? FIP_FL_SPMA | FIP_FL_FPMA :
639 memcpy(cap->eth.h_dest, fcf->fcf_mac, ETH_ALEN);
641 memcpy(cap->eth.h_source, fip->ctl_src_addr, ETH_ALEN);
642 cap->eth.h_proto = htons(ETH_P_FIP);
644 cap->fip.fip_ver = FIP_VER_ENCAPS(FIP_VER);
645 cap->fip.fip_op = htons(FIP_OP_LS);
646 if (op == ELS_LS_ACC || op == ELS_LS_RJT)
647 cap->fip.fip_subcode = FIP_SC_REP;
649 cap->fip.fip_subcode = FIP_SC_REQ;
650 cap->fip.fip_flags = htons(fip_flags);
652 cap->encaps.fd_desc.fip_dtype = dtype;
653 cap->encaps.fd_desc.fip_dlen = dlen / FIP_BPW;
655 if (op != ELS_LS_RJT) {
656 dlen += sizeof(*mac);
657 mac = skb_put_zero(skb, sizeof(*mac));
658 mac->fd_desc.fip_dtype = FIP_DT_MAC;
659 mac->fd_desc.fip_dlen = sizeof(*mac) / FIP_BPW;
660 if (dtype != FIP_DT_FLOGI && dtype != FIP_DT_FDISC) {
661 memcpy(mac->fd_mac, fip->get_src_addr(lport), ETH_ALEN);
662 } else if (fip->mode == FIP_MODE_VN2VN) {
663 hton24(mac->fd_mac, FIP_VN_FC_MAP);
664 hton24(mac->fd_mac + 3, fip->port_id);
665 } else if (fip_flags & FIP_FL_SPMA) {
666 LIBFCOE_FIP_DBG(fip, "FLOGI/FDISC sent with SPMA\n");
667 memcpy(mac->fd_mac, fip->ctl_src_addr, ETH_ALEN);
669 LIBFCOE_FIP_DBG(fip, "FLOGI/FDISC sent with FPMA\n");
670 /* FPMA only FLOGI. Must leave the MAC desc zeroed. */
673 cap->fip.fip_dl_len = htons(dlen / FIP_BPW);
675 skb->protocol = htons(ETH_P_FIP);
676 skb->priority = fip->priority;
677 skb_reset_mac_header(skb);
678 skb_reset_network_header(skb);
683 * fcoe_ctlr_els_send() - Send an ELS frame encapsulated by FIP if appropriate.
684 * @fip: FCoE controller.
685 * @lport: libfc fc_lport to send from
686 * @skb: FCoE ELS frame including FC header but no FCoE headers.
688 * Returns a non-zero error code if the frame should not be sent.
689 * Returns zero if the caller should send the frame with FCoE encapsulation.
691 * The caller must check that the length is a multiple of 4.
692 * The SKB must have enough headroom (28 bytes) and tailroom (8 bytes).
693 * The the skb must also be an fc_frame.
695 * This is called from the lower-level driver with spinlocks held,
696 * so we must not take a mutex here.
698 int fcoe_ctlr_els_send(struct fcoe_ctlr *fip, struct fc_lport *lport,
702 struct fc_frame_header *fh;
708 fp = container_of(skb, struct fc_frame, skb);
709 fh = (struct fc_frame_header *)skb->data;
710 op = *(u8 *)(fh + 1);
712 if (op == ELS_FLOGI && fip->mode != FIP_MODE_VN2VN) {
713 old_xid = fip->flogi_oxid;
714 fip->flogi_oxid = ntohs(fh->fh_ox_id);
715 if (fip->state == FIP_ST_AUTO) {
716 if (old_xid == FC_XID_UNKNOWN)
717 fip->flogi_count = 0;
719 if (fip->flogi_count < 3)
721 fcoe_ctlr_map_dest(fip);
724 if (fip->state == FIP_ST_NON_FIP)
725 fcoe_ctlr_map_dest(fip);
728 if (fip->state == FIP_ST_NON_FIP)
730 if (!fip->sel_fcf && fip->mode != FIP_MODE_VN2VN)
735 if (fip->mode == FIP_MODE_VN2VN)
737 spin_lock_irqsave(&fip->ctlr_lock, flags);
738 kfree_skb(fip->flogi_req);
739 fip->flogi_req = skb;
740 fip->flogi_req_send = 1;
741 spin_unlock_irqrestore(&fip->ctlr_lock, flags);
742 schedule_work(&fip->timer_work);
745 if (ntoh24(fh->fh_s_id))
750 if (fip->mode == FIP_MODE_VN2VN) {
751 if (fip->state != FIP_ST_VNMP_UP)
753 if (ntoh24(fh->fh_d_id) == FC_FID_FLOGI)
756 if (fip->state != FIP_ST_ENABLED)
758 if (ntoh24(fh->fh_d_id) != FC_FID_FLOGI)
765 * If non-FIP, we may have gotten an SID by accepting an FLOGI
766 * from a point-to-point connection. Switch to using
767 * the source mac based on the SID. The destination
768 * MAC in this case would have been set by receiving the
771 if (fip->state == FIP_ST_NON_FIP) {
772 if (fip->flogi_oxid == FC_XID_UNKNOWN)
774 fip->flogi_oxid = FC_XID_UNKNOWN;
775 fc_fcoe_set_mac(mac, fh->fh_d_id);
776 fip->update_mac(lport, mac);
785 if (fip->state != FIP_ST_ENABLED &&
786 fip->state != FIP_ST_VNMP_UP)
790 LIBFCOE_FIP_DBG(fip, "els_send op %u d_id %x\n",
791 op, ntoh24(fh->fh_d_id));
792 if (fcoe_ctlr_encaps(fip, lport, op, skb, ntoh24(fh->fh_d_id)))
797 LIBFCOE_FIP_DBG(fip, "drop els_send op %u d_id %x\n",
798 op, ntoh24(fh->fh_d_id));
802 EXPORT_SYMBOL(fcoe_ctlr_els_send);
805 * fcoe_ctlr_age_fcfs() - Reset and free all old FCFs for a controller
806 * @fip: The FCoE controller to free FCFs on
808 * Called with lock held and preemption disabled.
810 * An FCF is considered old if we have missed two advertisements.
811 * That is, there have been no valid advertisement from it for 2.5
812 * times its keep-alive period.
814 * In addition, determine the time when an FCF selection can occur.
816 * Also, increment the MissDiscAdvCount when no advertisement is received
817 * for the corresponding FCF for 1.5 * FKA_ADV_PERIOD (FC-BB-5 LESB).
819 * Returns the time in jiffies for the next call.
821 static unsigned long fcoe_ctlr_age_fcfs(struct fcoe_ctlr *fip)
823 struct fcoe_fcf *fcf;
824 struct fcoe_fcf *next;
825 unsigned long next_timer = jiffies + msecs_to_jiffies(FIP_VN_KA_PERIOD);
826 unsigned long deadline;
827 unsigned long sel_time = 0;
828 struct list_head del_list;
830 INIT_LIST_HEAD(&del_list);
832 list_for_each_entry_safe(fcf, next, &fip->fcfs, list) {
833 deadline = fcf->time + fcf->fka_period + fcf->fka_period / 2;
834 if (fip->sel_fcf == fcf) {
835 if (time_after(jiffies, deadline)) {
838 miss_cnt = this_cpu_inc_return(fip->lp->stats->MissDiscAdvCount);
839 printk(KERN_INFO "libfcoe: host%d: "
840 "Missing Discovery Advertisement "
841 "for fab %16.16llx count %lld\n",
842 fip->lp->host->host_no, fcf->fabric_name,
844 } else if (time_after(next_timer, deadline))
845 next_timer = deadline;
848 deadline += fcf->fka_period;
849 if (time_after_eq(jiffies, deadline)) {
850 if (fip->sel_fcf == fcf)
853 * Move to delete list so we can call
854 * fcoe_sysfs_fcf_del (which can sleep)
855 * after the put_cpu().
857 list_del(&fcf->list);
858 list_add(&fcf->list, &del_list);
859 this_cpu_inc(fip->lp->stats->VLinkFailureCount);
861 if (time_after(next_timer, deadline))
862 next_timer = deadline;
863 if (fcoe_ctlr_mtu_valid(fcf) &&
864 (!sel_time || time_before(sel_time, fcf->time)))
865 sel_time = fcf->time;
869 list_for_each_entry_safe(fcf, next, &del_list, list) {
870 /* Removes fcf from current list */
871 fcoe_sysfs_fcf_del(fcf);
874 if (sel_time && !fip->sel_fcf && !fip->sel_time) {
875 sel_time += msecs_to_jiffies(FCOE_CTLR_START_DELAY);
876 fip->sel_time = sel_time;
883 * fcoe_ctlr_parse_adv() - Decode a FIP advertisement into a new FCF entry
884 * @fip: The FCoE controller receiving the advertisement
885 * @skb: The received FIP advertisement frame
886 * @fcf: The resulting FCF entry
888 * Returns zero on a valid parsed advertisement,
889 * otherwise returns non zero value.
891 static int fcoe_ctlr_parse_adv(struct fcoe_ctlr *fip,
892 struct sk_buff *skb, struct fcoe_fcf *fcf)
894 struct fip_header *fiph;
895 struct fip_desc *desc = NULL;
896 struct fip_wwn_desc *wwn;
897 struct fip_fab_desc *fab;
898 struct fip_fka_desc *fka;
904 memset(fcf, 0, sizeof(*fcf));
905 fcf->fka_period = msecs_to_jiffies(FCOE_CTLR_DEF_FKA);
907 fiph = (struct fip_header *)skb->data;
908 fcf->flags = ntohs(fiph->fip_flags);
911 * mask of required descriptors. validating each one clears its bit.
913 desc_mask = BIT(FIP_DT_PRI) | BIT(FIP_DT_MAC) | BIT(FIP_DT_NAME) |
914 BIT(FIP_DT_FAB) | BIT(FIP_DT_FKA);
916 rlen = ntohs(fiph->fip_dl_len) * 4;
917 if (rlen + sizeof(*fiph) > skb->len)
920 desc = (struct fip_desc *)(fiph + 1);
922 dlen = desc->fip_dlen * FIP_BPW;
923 if (dlen < sizeof(*desc) || dlen > rlen)
925 /* Drop Adv if there are duplicate critical descriptors */
926 if ((desc->fip_dtype < 32) &&
927 !(desc_mask & 1U << desc->fip_dtype)) {
928 LIBFCOE_FIP_DBG(fip, "Duplicate Critical "
929 "Descriptors in FIP adv\n");
932 switch (desc->fip_dtype) {
934 if (dlen != sizeof(struct fip_pri_desc))
936 fcf->pri = ((struct fip_pri_desc *)desc)->fd_pri;
937 desc_mask &= ~BIT(FIP_DT_PRI);
940 if (dlen != sizeof(struct fip_mac_desc))
943 ((struct fip_mac_desc *)desc)->fd_mac,
945 memcpy(fcf->fcoe_mac, fcf->fcf_mac, ETH_ALEN);
946 if (!is_valid_ether_addr(fcf->fcf_mac)) {
948 "Invalid MAC addr %pM in FIP adv\n",
952 desc_mask &= ~BIT(FIP_DT_MAC);
955 if (dlen != sizeof(struct fip_wwn_desc))
957 wwn = (struct fip_wwn_desc *)desc;
958 fcf->switch_name = get_unaligned_be64(&wwn->fd_wwn);
959 desc_mask &= ~BIT(FIP_DT_NAME);
962 if (dlen != sizeof(struct fip_fab_desc))
964 fab = (struct fip_fab_desc *)desc;
965 fcf->fabric_name = get_unaligned_be64(&fab->fd_wwn);
966 fcf->vfid = ntohs(fab->fd_vfid);
967 fcf->fc_map = ntoh24(fab->fd_map);
968 desc_mask &= ~BIT(FIP_DT_FAB);
971 if (dlen != sizeof(struct fip_fka_desc))
973 fka = (struct fip_fka_desc *)desc;
974 if (fka->fd_flags & FIP_FKA_ADV_D)
976 t = ntohl(fka->fd_fka_period);
977 if (t >= FCOE_CTLR_MIN_FKA)
978 fcf->fka_period = msecs_to_jiffies(t);
979 desc_mask &= ~BIT(FIP_DT_FKA);
982 case FIP_DT_FCOE_SIZE:
988 LIBFCOE_FIP_DBG(fip, "unexpected descriptor type %x "
989 "in FIP adv\n", desc->fip_dtype);
990 /* standard says ignore unknown descriptors >= 128 */
991 if (desc->fip_dtype < FIP_DT_NON_CRITICAL)
995 desc = (struct fip_desc *)((char *)desc + dlen);
998 if (!fcf->fc_map || (fcf->fc_map & 0x10000))
1000 if (!fcf->switch_name)
1003 LIBFCOE_FIP_DBG(fip, "adv missing descriptors mask %x\n",
1010 LIBFCOE_FIP_DBG(fip, "FIP length error in descriptor type %x len %zu\n",
1011 desc->fip_dtype, dlen);
1016 * fcoe_ctlr_recv_adv() - Handle an incoming advertisement
1017 * @fip: The FCoE controller receiving the advertisement
1018 * @skb: The received FIP packet
1020 static void fcoe_ctlr_recv_adv(struct fcoe_ctlr *fip, struct sk_buff *skb)
1022 struct fcoe_fcf *fcf;
1023 struct fcoe_fcf new;
1024 unsigned long sol_tov = msecs_to_jiffies(FCOE_CTLR_SOL_TOV);
1030 if (fcoe_ctlr_parse_adv(fip, skb, &new))
1033 mutex_lock(&fip->ctlr_mutex);
1034 first = list_empty(&fip->fcfs);
1035 list_for_each_entry(fcf, &fip->fcfs, list) {
1036 if (fcf->switch_name == new.switch_name &&
1037 fcf->fabric_name == new.fabric_name &&
1038 fcf->fc_map == new.fc_map &&
1039 ether_addr_equal(fcf->fcf_mac, new.fcf_mac)) {
1045 if (fip->fcf_count >= FCOE_CTLR_FCF_LIMIT)
1048 fcf = kmalloc(sizeof(*fcf), GFP_ATOMIC);
1052 memcpy(fcf, &new, sizeof(new));
1054 rc = fcoe_sysfs_fcf_add(fcf);
1056 printk(KERN_ERR "Failed to allocate sysfs instance "
1057 "for FCF, fab %16.16llx mac %pM\n",
1058 new.fabric_name, new.fcf_mac);
1064 * Update the FCF's keep-alive descriptor flags.
1065 * Other flag changes from new advertisements are
1066 * ignored after a solicited advertisement is
1067 * received and the FCF is selectable (usable).
1069 fcf->fd_flags = new.fd_flags;
1070 if (!fcoe_ctlr_fcf_usable(fcf))
1071 fcf->flags = new.flags;
1073 if (fcf == fip->sel_fcf && !fcf->fd_flags) {
1074 fip->ctlr_ka_time -= fcf->fka_period;
1075 fip->ctlr_ka_time += new.fka_period;
1076 if (time_before(fip->ctlr_ka_time, fip->timer.expires))
1077 mod_timer(&fip->timer, fip->ctlr_ka_time);
1079 fcf->fka_period = new.fka_period;
1080 memcpy(fcf->fcf_mac, new.fcf_mac, ETH_ALEN);
1083 mtu_valid = fcoe_ctlr_mtu_valid(fcf);
1084 fcf->time = jiffies;
1086 LIBFCOE_FIP_DBG(fip, "New FCF fab %16.16llx mac %pM\n",
1087 fcf->fabric_name, fcf->fcf_mac);
1090 * If this advertisement is not solicited and our max receive size
1091 * hasn't been verified, send a solicited advertisement.
1094 fcoe_ctlr_solicit(fip, fcf);
1097 * If its been a while since we did a solicit, and this is
1098 * the first advertisement we've received, do a multicast
1099 * solicitation to gather as many advertisements as we can
1100 * before selection occurs.
1102 if (first && time_after(jiffies, fip->sol_time + sol_tov))
1103 fcoe_ctlr_solicit(fip, NULL);
1106 * Put this FCF at the head of the list for priority among equals.
1107 * This helps in the case of an NPV switch which insists we use
1108 * the FCF that answers multicast solicitations, not the others that
1109 * are sending periodic multicast advertisements.
1112 list_move(&fcf->list, &fip->fcfs);
1115 * If this is the first validated FCF, note the time and
1116 * set a timer to trigger selection.
1118 if (mtu_valid && !fip->sel_fcf && !fip->sel_time &&
1119 fcoe_ctlr_fcf_usable(fcf)) {
1120 fip->sel_time = jiffies +
1121 msecs_to_jiffies(FCOE_CTLR_START_DELAY);
1122 if (!timer_pending(&fip->timer) ||
1123 time_before(fip->sel_time, fip->timer.expires))
1124 mod_timer(&fip->timer, fip->sel_time);
1128 mutex_unlock(&fip->ctlr_mutex);
1132 * fcoe_ctlr_recv_els() - Handle an incoming FIP encapsulated ELS frame
1133 * @fip: The FCoE controller which received the packet
1134 * @skb: The received FIP packet
1136 static void fcoe_ctlr_recv_els(struct fcoe_ctlr *fip, struct sk_buff *skb)
1138 struct fc_lport *lport = fip->lp;
1139 struct fip_header *fiph;
1140 struct fc_frame *fp = (struct fc_frame *)skb;
1141 struct fc_frame_header *fh = NULL;
1142 struct fip_desc *desc;
1143 struct fip_encaps *els;
1144 struct fcoe_fcf *sel;
1145 enum fip_desc_type els_dtype = 0;
1148 u8 granted_mac[ETH_ALEN] = { 0 };
1155 fiph = (struct fip_header *)skb->data;
1156 sub = fiph->fip_subcode;
1157 if (sub != FIP_SC_REQ && sub != FIP_SC_REP)
1160 rlen = ntohs(fiph->fip_dl_len) * 4;
1161 if (rlen + sizeof(*fiph) > skb->len)
1164 desc = (struct fip_desc *)(fiph + 1);
1167 dlen = desc->fip_dlen * FIP_BPW;
1168 if (dlen < sizeof(*desc) || dlen > rlen)
1170 /* Drop ELS if there are duplicate critical descriptors */
1171 if (desc->fip_dtype < 32) {
1172 if ((desc->fip_dtype != FIP_DT_MAC) &&
1173 (desc_mask & 1U << desc->fip_dtype)) {
1174 LIBFCOE_FIP_DBG(fip, "Duplicate Critical "
1175 "Descriptors in FIP ELS\n");
1178 desc_mask |= (1 << desc->fip_dtype);
1180 switch (desc->fip_dtype) {
1183 if (desc_cnt == 1) {
1184 LIBFCOE_FIP_DBG(fip, "FIP descriptors "
1185 "received out of order\n");
1189 * Some switch implementations send two MAC descriptors,
1190 * with first MAC(granted_mac) being the FPMA, and the
1191 * second one(fcoe_mac) is used as destination address
1192 * for sending/receiving FCoE packets. FIP traffic is
1193 * sent using fip_mac. For regular switches, both
1194 * fip_mac and fcoe_mac would be the same.
1198 ((struct fip_mac_desc *)desc)->fd_mac,
1201 if (dlen != sizeof(struct fip_mac_desc))
1204 if ((desc_cnt == 3) && (sel))
1205 memcpy(sel->fcoe_mac,
1206 ((struct fip_mac_desc *)desc)->fd_mac,
1213 if (desc_cnt != 1) {
1214 LIBFCOE_FIP_DBG(fip, "FIP descriptors "
1215 "received out of order\n");
1220 if (dlen < sizeof(*els) + sizeof(*fh) + 1)
1222 els_len = dlen - sizeof(*els);
1223 els = (struct fip_encaps *)desc;
1224 fh = (struct fc_frame_header *)(els + 1);
1225 els_dtype = desc->fip_dtype;
1228 LIBFCOE_FIP_DBG(fip, "unexpected descriptor type %x "
1229 "in FIP adv\n", desc->fip_dtype);
1230 /* standard says ignore unknown descriptors >= 128 */
1231 if (desc->fip_dtype < FIP_DT_NON_CRITICAL)
1233 if (desc_cnt <= 2) {
1234 LIBFCOE_FIP_DBG(fip, "FIP descriptors "
1235 "received out of order\n");
1240 desc = (struct fip_desc *)((char *)desc + dlen);
1246 els_op = *(u8 *)(fh + 1);
1248 if ((els_dtype == FIP_DT_FLOGI || els_dtype == FIP_DT_FDISC) &&
1249 sub == FIP_SC_REP && fip->mode != FIP_MODE_VN2VN) {
1250 if (els_op == ELS_LS_ACC) {
1251 if (!is_valid_ether_addr(granted_mac)) {
1252 LIBFCOE_FIP_DBG(fip,
1253 "Invalid MAC address %pM in FIP ELS\n",
1257 memcpy(fr_cb(fp)->granted_mac, granted_mac, ETH_ALEN);
1259 if (fip->flogi_oxid == ntohs(fh->fh_ox_id)) {
1260 fip->flogi_oxid = FC_XID_UNKNOWN;
1261 if (els_dtype == FIP_DT_FLOGI)
1262 fcoe_ctlr_announce(fip);
1264 } else if (els_dtype == FIP_DT_FLOGI &&
1265 !fcoe_ctlr_flogi_retry(fip))
1266 goto drop; /* retrying FLOGI so drop reject */
1269 if ((desc_cnt == 0) || ((els_op != ELS_LS_RJT) &&
1270 (!(1U << FIP_DT_MAC & desc_mask)))) {
1271 LIBFCOE_FIP_DBG(fip, "Missing critical descriptors "
1277 * Convert skb into an fc_frame containing only the ELS.
1279 skb_pull(skb, (u8 *)fh - skb->data);
1280 skb_trim(skb, els_len);
1281 fp = (struct fc_frame *)skb;
1283 fr_sof(fp) = FC_SOF_I3;
1284 fr_eof(fp) = FC_EOF_T;
1286 fr_encaps(fp) = els_dtype;
1288 this_cpu_inc(lport->stats->RxFrames);
1289 this_cpu_add(lport->stats->RxWords, skb->len / FIP_BPW);
1291 fc_exch_recv(lport, fp);
1295 LIBFCOE_FIP_DBG(fip, "FIP length error in descriptor type %x len %zu\n",
1296 desc->fip_dtype, dlen);
1302 * fcoe_ctlr_recv_clr_vlink() - Handle an incoming link reset frame
1303 * @fip: The FCoE controller that received the frame
1304 * @skb: The received FIP packet
1306 * There may be multiple VN_Port descriptors.
1307 * The overall length has already been checked.
1309 static void fcoe_ctlr_recv_clr_vlink(struct fcoe_ctlr *fip,
1310 struct sk_buff *skb)
1312 struct fip_desc *desc;
1313 struct fip_mac_desc *mp;
1314 struct fip_wwn_desc *wp;
1315 struct fip_vn_desc *vp;
1318 struct fcoe_fcf *fcf = fip->sel_fcf;
1319 struct fc_lport *lport = fip->lp;
1320 struct fc_lport *vn_port = NULL;
1323 int reset_phys_port = 0;
1324 struct fip_vn_desc **vlink_desc_arr = NULL;
1325 struct fip_header *fh = (struct fip_header *)skb->data;
1326 struct ethhdr *eh = eth_hdr(skb);
1328 LIBFCOE_FIP_DBG(fip, "Clear Virtual Link received\n");
1332 * We are yet to select best FCF, but we got CVL in the
1333 * meantime. reset the ctlr and let it rediscover the FCF
1335 LIBFCOE_FIP_DBG(fip, "Resetting fcoe_ctlr as FCF has not been "
1337 mutex_lock(&fip->ctlr_mutex);
1338 fcoe_ctlr_reset(fip);
1339 mutex_unlock(&fip->ctlr_mutex);
1344 * If we've selected an FCF check that the CVL is from there to avoid
1345 * processing CVLs from an unexpected source. If it is from an
1346 * unexpected source drop it on the floor.
1348 if (!ether_addr_equal(eh->h_source, fcf->fcf_mac)) {
1349 LIBFCOE_FIP_DBG(fip, "Dropping CVL due to source address "
1350 "mismatch with FCF src=%pM\n", eh->h_source);
1355 * If we haven't logged into the fabric but receive a CVL we should
1356 * reset everything and go back to solicitation.
1358 if (!lport->port_id) {
1359 LIBFCOE_FIP_DBG(fip, "lport not logged in, resoliciting\n");
1360 mutex_lock(&fip->ctlr_mutex);
1361 fcoe_ctlr_reset(fip);
1362 mutex_unlock(&fip->ctlr_mutex);
1363 fc_lport_reset(fip->lp);
1364 fcoe_ctlr_solicit(fip, NULL);
1369 * mask of required descriptors. Validating each one clears its bit.
1371 desc_mask = BIT(FIP_DT_MAC) | BIT(FIP_DT_NAME);
1373 rlen = ntohs(fh->fip_dl_len) * FIP_BPW;
1374 desc = (struct fip_desc *)(fh + 1);
1377 * Actually need to subtract 'sizeof(*mp) - sizeof(*wp)' from 'rlen'
1378 * before determining max Vx_Port descriptor but a buggy FCF could have
1379 * omitted either or both MAC Address and Name Identifier descriptors
1381 num_vlink_desc = rlen / sizeof(*vp);
1383 vlink_desc_arr = kmalloc_array(num_vlink_desc, sizeof(vp),
1385 if (!vlink_desc_arr)
1389 while (rlen >= sizeof(*desc)) {
1390 dlen = desc->fip_dlen * FIP_BPW;
1393 /* Drop CVL if there are duplicate critical descriptors */
1394 if ((desc->fip_dtype < 32) &&
1395 (desc->fip_dtype != FIP_DT_VN_ID) &&
1396 !(desc_mask & 1U << desc->fip_dtype)) {
1397 LIBFCOE_FIP_DBG(fip, "Duplicate Critical "
1398 "Descriptors in FIP CVL\n");
1401 switch (desc->fip_dtype) {
1403 mp = (struct fip_mac_desc *)desc;
1404 if (dlen < sizeof(*mp))
1406 if (!ether_addr_equal(mp->fd_mac, fcf->fcf_mac))
1408 desc_mask &= ~BIT(FIP_DT_MAC);
1411 wp = (struct fip_wwn_desc *)desc;
1412 if (dlen < sizeof(*wp))
1414 if (get_unaligned_be64(&wp->fd_wwn) != fcf->switch_name)
1416 desc_mask &= ~BIT(FIP_DT_NAME);
1419 vp = (struct fip_vn_desc *)desc;
1420 if (dlen < sizeof(*vp))
1422 vlink_desc_arr[num_vlink_desc++] = vp;
1423 vn_port = fc_vport_id_lookup(lport,
1424 ntoh24(vp->fd_fc_id));
1425 if (vn_port && (vn_port == lport)) {
1426 mutex_lock(&fip->ctlr_mutex);
1427 this_cpu_inc(lport->stats->VLinkFailureCount);
1428 fcoe_ctlr_reset(fip);
1429 mutex_unlock(&fip->ctlr_mutex);
1433 /* standard says ignore unknown descriptors >= 128 */
1434 if (desc->fip_dtype < FIP_DT_NON_CRITICAL)
1438 desc = (struct fip_desc *)((char *)desc + dlen);
1443 * reset only if all required descriptors were present and valid.
1446 LIBFCOE_FIP_DBG(fip, "missing descriptors mask %x\n",
1448 else if (!num_vlink_desc) {
1449 LIBFCOE_FIP_DBG(fip, "CVL: no Vx_Port descriptor found\n");
1451 * No Vx_Port description. Clear all NPIV ports,
1452 * followed by physical port
1454 mutex_lock(&fip->ctlr_mutex);
1455 this_cpu_inc(lport->stats->VLinkFailureCount);
1456 fcoe_ctlr_reset(fip);
1457 mutex_unlock(&fip->ctlr_mutex);
1459 mutex_lock(&lport->lp_mutex);
1460 list_for_each_entry(vn_port, &lport->vports, list)
1461 fc_lport_reset(vn_port);
1462 mutex_unlock(&lport->lp_mutex);
1464 fc_lport_reset(fip->lp);
1465 fcoe_ctlr_solicit(fip, NULL);
1469 LIBFCOE_FIP_DBG(fip, "performing Clear Virtual Link\n");
1470 for (i = 0; i < num_vlink_desc; i++) {
1471 vp = vlink_desc_arr[i];
1472 vn_port = fc_vport_id_lookup(lport,
1473 ntoh24(vp->fd_fc_id));
1478 * 'port_id' is already validated, check MAC address and
1481 if (!ether_addr_equal(fip->get_src_addr(vn_port),
1483 get_unaligned_be64(&vp->fd_wwpn) !=
1487 if (vn_port == lport)
1489 * Physical port, defer processing till all
1490 * listed NPIV ports are cleared
1492 reset_phys_port = 1;
1493 else /* NPIV port */
1494 fc_lport_reset(vn_port);
1497 if (reset_phys_port) {
1498 fc_lport_reset(fip->lp);
1499 fcoe_ctlr_solicit(fip, NULL);
1504 kfree(vlink_desc_arr);
1508 * fcoe_ctlr_recv() - Receive a FIP packet
1509 * @fip: The FCoE controller that received the packet
1510 * @skb: The received FIP packet
1512 * This may be called from either NET_RX_SOFTIRQ or IRQ.
1514 void fcoe_ctlr_recv(struct fcoe_ctlr *fip, struct sk_buff *skb)
1516 skb = skb_share_check(skb, GFP_ATOMIC);
1519 skb_queue_tail(&fip->fip_recv_list, skb);
1520 schedule_work(&fip->recv_work);
1522 EXPORT_SYMBOL(fcoe_ctlr_recv);
1525 * fcoe_ctlr_recv_handler() - Receive a FIP frame
1526 * @fip: The FCoE controller that received the frame
1527 * @skb: The received FIP frame
1529 * Returns non-zero if the frame is dropped.
1531 static int fcoe_ctlr_recv_handler(struct fcoe_ctlr *fip, struct sk_buff *skb)
1533 struct fip_header *fiph;
1535 enum fip_state state;
1536 bool fip_vlan_resp = false;
1540 if (skb_linearize(skb))
1542 if (skb->len < sizeof(*fiph))
1545 if (fip->mode == FIP_MODE_VN2VN) {
1546 if (!ether_addr_equal(eh->h_dest, fip->ctl_src_addr) &&
1547 !ether_addr_equal(eh->h_dest, fcoe_all_vn2vn) &&
1548 !ether_addr_equal(eh->h_dest, fcoe_all_p2p))
1550 } else if (!ether_addr_equal(eh->h_dest, fip->ctl_src_addr) &&
1551 !ether_addr_equal(eh->h_dest, fcoe_all_enode))
1553 fiph = (struct fip_header *)skb->data;
1554 op = ntohs(fiph->fip_op);
1555 sub = fiph->fip_subcode;
1557 if (FIP_VER_DECAPS(fiph->fip_ver) != FIP_VER)
1559 if (ntohs(fiph->fip_dl_len) * FIP_BPW + sizeof(*fiph) > skb->len)
1562 mutex_lock(&fip->ctlr_mutex);
1564 if (state == FIP_ST_AUTO) {
1566 fcoe_ctlr_set_state(fip, FIP_ST_ENABLED);
1567 state = FIP_ST_ENABLED;
1568 LIBFCOE_FIP_DBG(fip, "Using FIP mode\n");
1570 fip_vlan_resp = fip->fip_resp;
1571 mutex_unlock(&fip->ctlr_mutex);
1573 if (fip->mode == FIP_MODE_VN2VN && op == FIP_OP_VN2VN)
1574 return fcoe_ctlr_vn_recv(fip, skb);
1576 if (fip_vlan_resp && op == FIP_OP_VLAN) {
1577 LIBFCOE_FIP_DBG(fip, "fip vlan discovery\n");
1578 return fcoe_ctlr_vlan_recv(fip, skb);
1581 if (state != FIP_ST_ENABLED && state != FIP_ST_VNMP_UP &&
1582 state != FIP_ST_VNMP_CLAIM)
1585 if (op == FIP_OP_LS) {
1586 fcoe_ctlr_recv_els(fip, skb); /* consumes skb */
1590 if (state != FIP_ST_ENABLED)
1593 if (op == FIP_OP_DISC && sub == FIP_SC_ADV)
1594 fcoe_ctlr_recv_adv(fip, skb);
1595 else if (op == FIP_OP_CTRL && sub == FIP_SC_CLR_VLINK)
1596 fcoe_ctlr_recv_clr_vlink(fip, skb);
1605 * fcoe_ctlr_select() - Select the best FCF (if possible)
1606 * @fip: The FCoE controller
1608 * Returns the selected FCF, or NULL if none are usable.
1610 * If there are conflicting advertisements, no FCF can be chosen.
1612 * If there is already a selected FCF, this will choose a better one or
1613 * an equivalent one that hasn't already been sent a FLOGI.
1615 * Called with lock held.
1617 static struct fcoe_fcf *fcoe_ctlr_select(struct fcoe_ctlr *fip)
1619 struct fcoe_fcf *fcf;
1620 struct fcoe_fcf *best = fip->sel_fcf;
1622 list_for_each_entry(fcf, &fip->fcfs, list) {
1623 LIBFCOE_FIP_DBG(fip, "consider FCF fab %16.16llx "
1624 "VFID %d mac %pM map %x val %d "
1626 fcf->fabric_name, fcf->vfid, fcf->fcf_mac,
1627 fcf->fc_map, fcoe_ctlr_mtu_valid(fcf),
1628 fcf->flogi_sent, fcf->pri);
1629 if (!fcoe_ctlr_fcf_usable(fcf)) {
1630 LIBFCOE_FIP_DBG(fip, "FCF for fab %16.16llx "
1631 "map %x %svalid %savailable\n",
1632 fcf->fabric_name, fcf->fc_map,
1633 (fcf->flags & FIP_FL_SOL) ? "" : "in",
1634 (fcf->flags & FIP_FL_AVAIL) ?
1638 if (!best || fcf->pri < best->pri || best->flogi_sent)
1640 if (fcf->fabric_name != best->fabric_name ||
1641 fcf->vfid != best->vfid ||
1642 fcf->fc_map != best->fc_map) {
1643 LIBFCOE_FIP_DBG(fip, "Conflicting fabric, VFID, "
1648 fip->sel_fcf = best;
1650 LIBFCOE_FIP_DBG(fip, "using FCF mac %pM\n", best->fcf_mac);
1651 fip->port_ka_time = jiffies +
1652 msecs_to_jiffies(FIP_VN_KA_PERIOD);
1653 fip->ctlr_ka_time = jiffies + best->fka_period;
1654 if (time_before(fip->ctlr_ka_time, fip->timer.expires))
1655 mod_timer(&fip->timer, fip->ctlr_ka_time);
1661 * fcoe_ctlr_flogi_send_locked() - send FIP-encapsulated FLOGI to current FCF
1662 * @fip: The FCoE controller
1664 * Returns non-zero error if it could not be sent.
1666 * Called with ctlr_mutex and ctlr_lock held.
1667 * Caller must verify that fip->sel_fcf is not NULL.
1669 static int fcoe_ctlr_flogi_send_locked(struct fcoe_ctlr *fip)
1671 struct sk_buff *skb;
1672 struct sk_buff *skb_orig;
1673 struct fc_frame_header *fh;
1676 skb_orig = fip->flogi_req;
1681 * Clone and send the FLOGI request. If clone fails, use original.
1683 skb = skb_clone(skb_orig, GFP_ATOMIC);
1686 fip->flogi_req = NULL;
1688 fh = (struct fc_frame_header *)skb->data;
1689 error = fcoe_ctlr_encaps(fip, fip->lp, FIP_DT_FLOGI, skb,
1690 ntoh24(fh->fh_d_id));
1695 fip->send(fip, skb);
1696 fip->sel_fcf->flogi_sent = 1;
1701 * fcoe_ctlr_flogi_retry() - resend FLOGI request to a new FCF if possible
1702 * @fip: The FCoE controller
1704 * Returns non-zero error code if there's no FLOGI request to retry or
1705 * no alternate FCF available.
1707 static int fcoe_ctlr_flogi_retry(struct fcoe_ctlr *fip)
1709 struct fcoe_fcf *fcf;
1710 unsigned long flags;
1713 mutex_lock(&fip->ctlr_mutex);
1714 spin_lock_irqsave(&fip->ctlr_lock, flags);
1715 LIBFCOE_FIP_DBG(fip, "re-sending FLOGI - reselect\n");
1716 fcf = fcoe_ctlr_select(fip);
1717 if (!fcf || fcf->flogi_sent) {
1718 kfree_skb(fip->flogi_req);
1719 fip->flogi_req = NULL;
1722 fcoe_ctlr_solicit(fip, NULL);
1723 error = fcoe_ctlr_flogi_send_locked(fip);
1725 spin_unlock_irqrestore(&fip->ctlr_lock, flags);
1726 mutex_unlock(&fip->ctlr_mutex);
1732 * fcoe_ctlr_flogi_send() - Handle sending of FIP FLOGI.
1733 * @fip: The FCoE controller that timed out
1735 * Done here because fcoe_ctlr_els_send() can't get mutex.
1737 * Called with ctlr_mutex held. The caller must not hold ctlr_lock.
1739 static void fcoe_ctlr_flogi_send(struct fcoe_ctlr *fip)
1741 struct fcoe_fcf *fcf;
1742 unsigned long flags;
1744 spin_lock_irqsave(&fip->ctlr_lock, flags);
1746 if (!fcf || !fip->flogi_req_send)
1749 LIBFCOE_FIP_DBG(fip, "sending FLOGI\n");
1752 * If this FLOGI is being sent due to a timeout retry
1753 * to the same FCF as before, select a different FCF if possible.
1755 if (fcf->flogi_sent) {
1756 LIBFCOE_FIP_DBG(fip, "sending FLOGI - reselect\n");
1757 fcf = fcoe_ctlr_select(fip);
1758 if (!fcf || fcf->flogi_sent) {
1759 LIBFCOE_FIP_DBG(fip, "sending FLOGI - clearing\n");
1760 list_for_each_entry(fcf, &fip->fcfs, list)
1761 fcf->flogi_sent = 0;
1762 fcf = fcoe_ctlr_select(fip);
1766 fcoe_ctlr_flogi_send_locked(fip);
1767 fip->flogi_req_send = 0;
1769 LIBFCOE_FIP_DBG(fip, "No FCF selected - defer send\n");
1771 spin_unlock_irqrestore(&fip->ctlr_lock, flags);
1775 * fcoe_ctlr_timeout() - FIP timeout handler
1776 * @t: Timer context use to obtain the controller reference
1778 static void fcoe_ctlr_timeout(struct timer_list *t)
1780 struct fcoe_ctlr *fip = from_timer(fip, t, timer);
1782 schedule_work(&fip->timer_work);
1786 * fcoe_ctlr_timer_work() - Worker thread function for timer work
1787 * @work: Handle to a FCoE controller
1789 * Ages FCFs. Triggers FCF selection if possible.
1790 * Sends keep-alives and resets.
1792 static void fcoe_ctlr_timer_work(struct work_struct *work)
1794 struct fcoe_ctlr *fip;
1795 struct fc_lport *vport;
1798 u8 send_ctlr_ka = 0;
1799 u8 send_port_ka = 0;
1800 struct fcoe_fcf *sel;
1801 struct fcoe_fcf *fcf;
1802 unsigned long next_timer;
1804 fip = container_of(work, struct fcoe_ctlr, timer_work);
1805 if (fip->mode == FIP_MODE_VN2VN)
1806 return fcoe_ctlr_vn_timeout(fip);
1807 mutex_lock(&fip->ctlr_mutex);
1808 if (fip->state == FIP_ST_DISABLED) {
1809 mutex_unlock(&fip->ctlr_mutex);
1814 next_timer = fcoe_ctlr_age_fcfs(fip);
1817 if (!sel && fip->sel_time) {
1818 if (time_after_eq(jiffies, fip->sel_time)) {
1819 sel = fcoe_ctlr_select(fip);
1821 } else if (time_after(next_timer, fip->sel_time))
1822 next_timer = fip->sel_time;
1825 if (sel && fip->flogi_req_send)
1826 fcoe_ctlr_flogi_send(fip);
1827 else if (!sel && fcf)
1830 if (sel && !sel->fd_flags) {
1831 if (time_after_eq(jiffies, fip->ctlr_ka_time)) {
1832 fip->ctlr_ka_time = jiffies + sel->fka_period;
1835 if (time_after(next_timer, fip->ctlr_ka_time))
1836 next_timer = fip->ctlr_ka_time;
1838 if (time_after_eq(jiffies, fip->port_ka_time)) {
1839 fip->port_ka_time = jiffies +
1840 msecs_to_jiffies(FIP_VN_KA_PERIOD);
1843 if (time_after(next_timer, fip->port_ka_time))
1844 next_timer = fip->port_ka_time;
1846 if (!list_empty(&fip->fcfs))
1847 mod_timer(&fip->timer, next_timer);
1848 mutex_unlock(&fip->ctlr_mutex);
1851 fc_lport_reset(fip->lp);
1852 /* restart things with a solicitation */
1853 fcoe_ctlr_solicit(fip, NULL);
1857 fcoe_ctlr_send_keep_alive(fip, NULL, 0, fip->ctl_src_addr);
1860 mutex_lock(&fip->lp->lp_mutex);
1861 mac = fip->get_src_addr(fip->lp);
1862 fcoe_ctlr_send_keep_alive(fip, fip->lp, 1, mac);
1863 list_for_each_entry(vport, &fip->lp->vports, list) {
1864 mac = fip->get_src_addr(vport);
1865 fcoe_ctlr_send_keep_alive(fip, vport, 1, mac);
1867 mutex_unlock(&fip->lp->lp_mutex);
1872 * fcoe_ctlr_recv_work() - Worker thread function for receiving FIP frames
1873 * @recv_work: Handle to a FCoE controller
1875 static void fcoe_ctlr_recv_work(struct work_struct *recv_work)
1877 struct fcoe_ctlr *fip;
1878 struct sk_buff *skb;
1880 fip = container_of(recv_work, struct fcoe_ctlr, recv_work);
1881 while ((skb = skb_dequeue(&fip->fip_recv_list)))
1882 fcoe_ctlr_recv_handler(fip, skb);
1886 * fcoe_ctlr_recv_flogi() - Snoop pre-FIP receipt of FLOGI response
1887 * @fip: The FCoE controller
1888 * @lport: The local port
1889 * @fp: The FC frame to snoop
1891 * Snoop potential response to FLOGI or even incoming FLOGI.
1893 * The caller has checked that we are waiting for login as indicated
1894 * by fip->flogi_oxid != FC_XID_UNKNOWN.
1896 * The caller is responsible for freeing the frame.
1897 * Fill in the granted_mac address.
1899 * Return non-zero if the frame should not be delivered to libfc.
1901 int fcoe_ctlr_recv_flogi(struct fcoe_ctlr *fip, struct fc_lport *lport,
1902 struct fc_frame *fp)
1904 struct fc_frame_header *fh;
1908 sa = eth_hdr(&fp->skb)->h_source;
1909 fh = fc_frame_header_get(fp);
1910 if (fh->fh_type != FC_TYPE_ELS)
1913 op = fc_frame_payload_op(fp);
1914 if (op == ELS_LS_ACC && fh->fh_r_ctl == FC_RCTL_ELS_REP &&
1915 fip->flogi_oxid == ntohs(fh->fh_ox_id)) {
1917 mutex_lock(&fip->ctlr_mutex);
1918 if (fip->state != FIP_ST_AUTO && fip->state != FIP_ST_NON_FIP) {
1919 mutex_unlock(&fip->ctlr_mutex);
1922 fcoe_ctlr_set_state(fip, FIP_ST_NON_FIP);
1923 LIBFCOE_FIP_DBG(fip,
1924 "received FLOGI LS_ACC using non-FIP mode\n");
1928 * If the src mac addr is FC_OUI-based, then we mark the
1929 * address_mode flag to use FC_OUI-based Ethernet DA.
1930 * Otherwise we use the FCoE gateway addr
1932 if (ether_addr_equal(sa, (u8[6])FC_FCOE_FLOGI_MAC)) {
1933 fcoe_ctlr_map_dest(fip);
1935 memcpy(fip->dest_addr, sa, ETH_ALEN);
1938 fip->flogi_oxid = FC_XID_UNKNOWN;
1939 mutex_unlock(&fip->ctlr_mutex);
1940 fc_fcoe_set_mac(fr_cb(fp)->granted_mac, fh->fh_d_id);
1941 } else if (op == ELS_FLOGI && fh->fh_r_ctl == FC_RCTL_ELS_REQ && sa) {
1943 * Save source MAC for point-to-point responses.
1945 mutex_lock(&fip->ctlr_mutex);
1946 if (fip->state == FIP_ST_AUTO || fip->state == FIP_ST_NON_FIP) {
1947 memcpy(fip->dest_addr, sa, ETH_ALEN);
1949 if (fip->state == FIP_ST_AUTO)
1950 LIBFCOE_FIP_DBG(fip, "received non-FIP FLOGI. "
1951 "Setting non-FIP mode\n");
1952 fcoe_ctlr_set_state(fip, FIP_ST_NON_FIP);
1954 mutex_unlock(&fip->ctlr_mutex);
1958 EXPORT_SYMBOL(fcoe_ctlr_recv_flogi);
1961 * fcoe_wwn_from_mac() - Converts a 48-bit IEEE MAC address to a 64-bit FC WWN
1962 * @mac: The MAC address to convert
1963 * @scheme: The scheme to use when converting
1964 * @port: The port indicator for converting
1966 * Returns: u64 fc world wide name
1968 u64 fcoe_wwn_from_mac(unsigned char mac[ETH_ALEN],
1969 unsigned int scheme, unsigned int port)
1974 /* The MAC is in NO, so flip only the low 48 bits */
1975 host_mac = ((u64) mac[0] << 40) |
1976 ((u64) mac[1] << 32) |
1977 ((u64) mac[2] << 24) |
1978 ((u64) mac[3] << 16) |
1979 ((u64) mac[4] << 8) |
1982 WARN_ON(host_mac >= (1ULL << 48));
1983 wwn = host_mac | ((u64) scheme << 60);
1989 WARN_ON(port >= 0xfff);
1990 wwn |= (u64) port << 48;
1999 EXPORT_SYMBOL_GPL(fcoe_wwn_from_mac);
2002 * fcoe_ctlr_rport() - return the fcoe_rport for a given fc_rport_priv
2003 * @rdata: libfc remote port
2005 static inline struct fcoe_rport *fcoe_ctlr_rport(struct fc_rport_priv *rdata)
2007 return container_of(rdata, struct fcoe_rport, rdata);
2011 * fcoe_ctlr_vn_send() - Send a FIP VN2VN Probe Request or Reply.
2012 * @fip: The FCoE controller
2013 * @sub: sub-opcode for probe request, reply, or advertisement.
2014 * @dest: The destination Ethernet MAC address
2015 * @min_len: minimum size of the Ethernet payload to be sent
2017 static void fcoe_ctlr_vn_send(struct fcoe_ctlr *fip,
2018 enum fip_vn2vn_subcode sub,
2019 const u8 *dest, size_t min_len)
2021 struct sk_buff *skb;
2022 struct fip_vn2vn_probe_frame {
2024 struct fip_header fip;
2025 struct fip_mac_desc mac;
2026 struct fip_wwn_desc wwnn;
2027 struct fip_vn_desc vn;
2029 struct fip_fc4_feat *ff;
2030 struct fip_size_desc *size;
2035 len = sizeof(*frame);
2037 if (sub == FIP_SC_VN_CLAIM_NOTIFY || sub == FIP_SC_VN_CLAIM_REP) {
2038 dlen = sizeof(struct fip_fc4_feat) +
2039 sizeof(struct fip_size_desc);
2042 dlen += sizeof(frame->mac) + sizeof(frame->wwnn) + sizeof(frame->vn);
2043 len = max(len, min_len + sizeof(struct ethhdr));
2045 skb = dev_alloc_skb(len);
2049 frame = (struct fip_vn2vn_probe_frame *)skb->data;
2050 memset(frame, 0, len);
2051 memcpy(frame->eth.h_dest, dest, ETH_ALEN);
2053 if (sub == FIP_SC_VN_BEACON) {
2054 hton24(frame->eth.h_source, FIP_VN_FC_MAP);
2055 hton24(frame->eth.h_source + 3, fip->port_id);
2057 memcpy(frame->eth.h_source, fip->ctl_src_addr, ETH_ALEN);
2059 frame->eth.h_proto = htons(ETH_P_FIP);
2061 frame->fip.fip_ver = FIP_VER_ENCAPS(FIP_VER);
2062 frame->fip.fip_op = htons(FIP_OP_VN2VN);
2063 frame->fip.fip_subcode = sub;
2064 frame->fip.fip_dl_len = htons(dlen / FIP_BPW);
2066 frame->mac.fd_desc.fip_dtype = FIP_DT_MAC;
2067 frame->mac.fd_desc.fip_dlen = sizeof(frame->mac) / FIP_BPW;
2068 memcpy(frame->mac.fd_mac, fip->ctl_src_addr, ETH_ALEN);
2070 frame->wwnn.fd_desc.fip_dtype = FIP_DT_NAME;
2071 frame->wwnn.fd_desc.fip_dlen = sizeof(frame->wwnn) / FIP_BPW;
2072 put_unaligned_be64(fip->lp->wwnn, &frame->wwnn.fd_wwn);
2074 frame->vn.fd_desc.fip_dtype = FIP_DT_VN_ID;
2075 frame->vn.fd_desc.fip_dlen = sizeof(frame->vn) / FIP_BPW;
2076 hton24(frame->vn.fd_mac, FIP_VN_FC_MAP);
2077 hton24(frame->vn.fd_mac + 3, fip->port_id);
2078 hton24(frame->vn.fd_fc_id, fip->port_id);
2079 put_unaligned_be64(fip->lp->wwpn, &frame->vn.fd_wwpn);
2082 * For claims, add FC-4 features.
2083 * TBD: Add interface to get fc-4 types and features from libfc.
2085 if (sub == FIP_SC_VN_CLAIM_NOTIFY || sub == FIP_SC_VN_CLAIM_REP) {
2086 ff = (struct fip_fc4_feat *)(frame + 1);
2087 ff->fd_desc.fip_dtype = FIP_DT_FC4F;
2088 ff->fd_desc.fip_dlen = sizeof(*ff) / FIP_BPW;
2089 ff->fd_fts = fip->lp->fcts;
2092 if (fip->lp->service_params & FCP_SPPF_INIT_FCN)
2093 fcp_feat |= FCP_FEAT_INIT;
2094 if (fip->lp->service_params & FCP_SPPF_TARG_FCN)
2095 fcp_feat |= FCP_FEAT_TARG;
2096 fcp_feat <<= (FC_TYPE_FCP * 4) % 32;
2097 ff->fd_ff.fd_feat[FC_TYPE_FCP * 4 / 32] = htonl(fcp_feat);
2099 size = (struct fip_size_desc *)(ff + 1);
2100 size->fd_desc.fip_dtype = FIP_DT_FCOE_SIZE;
2101 size->fd_desc.fip_dlen = sizeof(*size) / FIP_BPW;
2102 size->fd_size = htons(fcoe_ctlr_fcoe_size(fip));
2106 skb->protocol = htons(ETH_P_FIP);
2107 skb->priority = fip->priority;
2108 skb_reset_mac_header(skb);
2109 skb_reset_network_header(skb);
2111 fip->send(fip, skb);
2115 * fcoe_ctlr_vn_rport_callback - Event handler for rport events.
2116 * @lport: The lport which is receiving the event
2117 * @rdata: remote port private data
2118 * @event: The event that occurred
2120 * Locking Note: The rport lock must not be held when calling this function.
2122 static void fcoe_ctlr_vn_rport_callback(struct fc_lport *lport,
2123 struct fc_rport_priv *rdata,
2124 enum fc_rport_event event)
2126 struct fcoe_ctlr *fip = lport->disc.priv;
2127 struct fcoe_rport *frport = fcoe_ctlr_rport(rdata);
2129 LIBFCOE_FIP_DBG(fip, "vn_rport_callback %x event %d\n",
2130 rdata->ids.port_id, event);
2132 mutex_lock(&fip->ctlr_mutex);
2134 case RPORT_EV_READY:
2135 frport->login_count = 0;
2138 case RPORT_EV_FAILED:
2140 frport->login_count++;
2141 if (frport->login_count > FCOE_CTLR_VN2VN_LOGIN_LIMIT) {
2142 LIBFCOE_FIP_DBG(fip,
2143 "rport FLOGI limited port_id %6.6x\n",
2144 rdata->ids.port_id);
2145 fc_rport_logoff(rdata);
2151 mutex_unlock(&fip->ctlr_mutex);
2154 static struct fc_rport_operations fcoe_ctlr_vn_rport_ops = {
2155 .event_callback = fcoe_ctlr_vn_rport_callback,
2159 * fcoe_ctlr_disc_stop_locked() - stop discovery in VN2VN mode
2160 * @lport: The local port
2162 * Called with ctlr_mutex held.
2164 static void fcoe_ctlr_disc_stop_locked(struct fc_lport *lport)
2166 struct fc_rport_priv *rdata;
2168 mutex_lock(&lport->disc.disc_mutex);
2169 list_for_each_entry_rcu(rdata, &lport->disc.rports, peers) {
2170 if (kref_get_unless_zero(&rdata->kref)) {
2171 fc_rport_logoff(rdata);
2172 kref_put(&rdata->kref, fc_rport_destroy);
2175 lport->disc.disc_callback = NULL;
2176 mutex_unlock(&lport->disc.disc_mutex);
2180 * fcoe_ctlr_disc_stop() - stop discovery in VN2VN mode
2181 * @lport: The local port
2183 * Called through the local port template for discovery.
2184 * Called without the ctlr_mutex held.
2186 static void fcoe_ctlr_disc_stop(struct fc_lport *lport)
2188 struct fcoe_ctlr *fip = lport->disc.priv;
2190 mutex_lock(&fip->ctlr_mutex);
2191 fcoe_ctlr_disc_stop_locked(lport);
2192 mutex_unlock(&fip->ctlr_mutex);
2196 * fcoe_ctlr_disc_stop_final() - stop discovery for shutdown in VN2VN mode
2197 * @lport: The local port
2199 * Called through the local port template for discovery.
2200 * Called without the ctlr_mutex held.
2202 static void fcoe_ctlr_disc_stop_final(struct fc_lport *lport)
2204 fcoe_ctlr_disc_stop(lport);
2205 fc_rport_flush_queue();
2210 * fcoe_ctlr_vn_restart() - VN2VN probe restart with new port_id
2211 * @fip: The FCoE controller
2213 * Called with fcoe_ctlr lock held.
2215 static void fcoe_ctlr_vn_restart(struct fcoe_ctlr *fip)
2220 fcoe_ctlr_disc_stop_locked(fip->lp);
2223 * Get proposed port ID.
2224 * If this is the first try after link up, use any previous port_id.
2225 * If there was none, use the low bits of the port_name.
2226 * On subsequent tries, get the next random one.
2227 * Don't use reserved IDs, use another non-zero value, just as random.
2229 port_id = fip->port_id;
2230 if (fip->probe_tries)
2231 port_id = prandom_u32_state(&fip->rnd_state) & 0xffff;
2233 port_id = fip->lp->wwpn & 0xffff;
2234 if (!port_id || port_id == 0xffff)
2236 fip->port_id = port_id;
2238 if (fip->probe_tries < FIP_VN_RLIM_COUNT) {
2240 wait = get_random_u32_below(FIP_VN_PROBE_WAIT);
2242 wait = FIP_VN_RLIM_INT;
2243 mod_timer(&fip->timer, jiffies + msecs_to_jiffies(wait));
2244 fcoe_ctlr_set_state(fip, FIP_ST_VNMP_START);
2248 * fcoe_ctlr_vn_start() - Start in VN2VN mode
2249 * @fip: The FCoE controller
2251 * Called with fcoe_ctlr lock held.
2253 static void fcoe_ctlr_vn_start(struct fcoe_ctlr *fip)
2255 fip->probe_tries = 0;
2256 prandom_seed_state(&fip->rnd_state, fip->lp->wwpn);
2257 fcoe_ctlr_vn_restart(fip);
2261 * fcoe_ctlr_vn_parse - parse probe request or response
2262 * @fip: The FCoE controller
2263 * @skb: incoming packet
2264 * @frport: parsed FCoE rport from the probe request
2266 * Returns non-zero error number on error.
2267 * Does not consume the packet.
2269 static int fcoe_ctlr_vn_parse(struct fcoe_ctlr *fip,
2270 struct sk_buff *skb,
2271 struct fcoe_rport *frport)
2273 struct fip_header *fiph;
2274 struct fip_desc *desc = NULL;
2275 struct fip_mac_desc *macd = NULL;
2276 struct fip_wwn_desc *wwn = NULL;
2277 struct fip_vn_desc *vn = NULL;
2278 struct fip_size_desc *size = NULL;
2285 fiph = (struct fip_header *)skb->data;
2286 frport->flags = ntohs(fiph->fip_flags);
2288 sub = fiph->fip_subcode;
2290 case FIP_SC_VN_PROBE_REQ:
2291 case FIP_SC_VN_PROBE_REP:
2292 case FIP_SC_VN_BEACON:
2293 desc_mask = BIT(FIP_DT_MAC) | BIT(FIP_DT_NAME) |
2296 case FIP_SC_VN_CLAIM_NOTIFY:
2297 case FIP_SC_VN_CLAIM_REP:
2298 desc_mask = BIT(FIP_DT_MAC) | BIT(FIP_DT_NAME) |
2299 BIT(FIP_DT_VN_ID) | BIT(FIP_DT_FC4F) |
2300 BIT(FIP_DT_FCOE_SIZE);
2303 LIBFCOE_FIP_DBG(fip, "vn_parse unknown subcode %u\n", sub);
2307 rlen = ntohs(fiph->fip_dl_len) * 4;
2308 if (rlen + sizeof(*fiph) > skb->len)
2311 desc = (struct fip_desc *)(fiph + 1);
2313 dlen = desc->fip_dlen * FIP_BPW;
2314 if (dlen < sizeof(*desc) || dlen > rlen)
2317 dtype = desc->fip_dtype;
2319 if (!(desc_mask & BIT(dtype))) {
2320 LIBFCOE_FIP_DBG(fip,
2321 "unexpected or duplicated desc "
2323 "FIP VN2VN subtype %u\n",
2327 desc_mask &= ~BIT(dtype);
2332 if (dlen != sizeof(struct fip_mac_desc))
2334 macd = (struct fip_mac_desc *)desc;
2335 if (!is_valid_ether_addr(macd->fd_mac)) {
2336 LIBFCOE_FIP_DBG(fip,
2337 "Invalid MAC addr %pM in FIP VN2VN\n",
2341 memcpy(frport->enode_mac, macd->fd_mac, ETH_ALEN);
2344 if (dlen != sizeof(struct fip_wwn_desc))
2346 wwn = (struct fip_wwn_desc *)desc;
2347 frport->rdata.ids.node_name =
2348 get_unaligned_be64(&wwn->fd_wwn);
2351 if (dlen != sizeof(struct fip_vn_desc))
2353 vn = (struct fip_vn_desc *)desc;
2354 memcpy(frport->vn_mac, vn->fd_mac, ETH_ALEN);
2355 frport->rdata.ids.port_id = ntoh24(vn->fd_fc_id);
2356 frport->rdata.ids.port_name =
2357 get_unaligned_be64(&vn->fd_wwpn);
2360 if (dlen != sizeof(struct fip_fc4_feat))
2363 case FIP_DT_FCOE_SIZE:
2364 if (dlen != sizeof(struct fip_size_desc))
2366 size = (struct fip_size_desc *)desc;
2367 frport->fcoe_len = ntohs(size->fd_size);
2370 LIBFCOE_FIP_DBG(fip, "unexpected descriptor type %x "
2371 "in FIP probe\n", dtype);
2372 /* standard says ignore unknown descriptors >= 128 */
2373 if (dtype < FIP_DT_NON_CRITICAL)
2377 desc = (struct fip_desc *)((char *)desc + dlen);
2383 LIBFCOE_FIP_DBG(fip, "FIP length error in descriptor type %x len %zu\n",
2389 * fcoe_ctlr_vn_send_claim() - send multicast FIP VN2VN Claim Notification.
2390 * @fip: The FCoE controller
2392 * Called with ctlr_mutex held.
2394 static void fcoe_ctlr_vn_send_claim(struct fcoe_ctlr *fip)
2396 fcoe_ctlr_vn_send(fip, FIP_SC_VN_CLAIM_NOTIFY, fcoe_all_vn2vn, 0);
2397 fip->sol_time = jiffies;
2401 * fcoe_ctlr_vn_probe_req() - handle incoming VN2VN probe request.
2402 * @fip: The FCoE controller
2403 * @frport: parsed FCoE rport from the probe request
2405 * Called with ctlr_mutex held.
2407 static void fcoe_ctlr_vn_probe_req(struct fcoe_ctlr *fip,
2408 struct fcoe_rport *frport)
2410 if (frport->rdata.ids.port_id != fip->port_id)
2413 switch (fip->state) {
2414 case FIP_ST_VNMP_CLAIM:
2415 case FIP_ST_VNMP_UP:
2416 LIBFCOE_FIP_DBG(fip, "vn_probe_req: send reply, state %x\n",
2418 fcoe_ctlr_vn_send(fip, FIP_SC_VN_PROBE_REP,
2419 frport->enode_mac, 0);
2421 case FIP_ST_VNMP_PROBE1:
2422 case FIP_ST_VNMP_PROBE2:
2424 * Decide whether to reply to the Probe.
2425 * Our selected address is never a "recorded" one, so
2426 * only reply if our WWPN is greater and the
2427 * Probe's REC bit is not set.
2428 * If we don't reply, we will change our address.
2430 if (fip->lp->wwpn > frport->rdata.ids.port_name &&
2431 !(frport->flags & FIP_FL_REC_OR_P2P)) {
2432 LIBFCOE_FIP_DBG(fip, "vn_probe_req: "
2433 "port_id collision\n");
2434 fcoe_ctlr_vn_send(fip, FIP_SC_VN_PROBE_REP,
2435 frport->enode_mac, 0);
2439 case FIP_ST_VNMP_START:
2440 LIBFCOE_FIP_DBG(fip, "vn_probe_req: "
2441 "restart VN2VN negotiation\n");
2442 fcoe_ctlr_vn_restart(fip);
2445 LIBFCOE_FIP_DBG(fip, "vn_probe_req: ignore state %x\n",
2452 * fcoe_ctlr_vn_probe_reply() - handle incoming VN2VN probe reply.
2453 * @fip: The FCoE controller
2454 * @frport: parsed FCoE rport from the probe request
2456 * Called with ctlr_mutex held.
2458 static void fcoe_ctlr_vn_probe_reply(struct fcoe_ctlr *fip,
2459 struct fcoe_rport *frport)
2461 if (frport->rdata.ids.port_id != fip->port_id)
2463 switch (fip->state) {
2464 case FIP_ST_VNMP_START:
2465 case FIP_ST_VNMP_PROBE1:
2466 case FIP_ST_VNMP_PROBE2:
2467 case FIP_ST_VNMP_CLAIM:
2468 LIBFCOE_FIP_DBG(fip, "vn_probe_reply: restart state %x\n",
2470 fcoe_ctlr_vn_restart(fip);
2472 case FIP_ST_VNMP_UP:
2473 LIBFCOE_FIP_DBG(fip, "vn_probe_reply: send claim notify\n");
2474 fcoe_ctlr_vn_send_claim(fip);
2482 * fcoe_ctlr_vn_add() - Add a VN2VN entry to the list, based on a claim reply.
2483 * @fip: The FCoE controller
2484 * @new: newly-parsed FCoE rport as a template for new rdata
2486 * Called with ctlr_mutex held.
2488 static void fcoe_ctlr_vn_add(struct fcoe_ctlr *fip, struct fcoe_rport *new)
2490 struct fc_lport *lport = fip->lp;
2491 struct fc_rport_priv *rdata;
2492 struct fc_rport_identifiers *ids;
2493 struct fcoe_rport *frport;
2496 port_id = new->rdata.ids.port_id;
2497 if (port_id == fip->port_id)
2500 mutex_lock(&lport->disc.disc_mutex);
2501 rdata = fc_rport_create(lport, port_id);
2503 mutex_unlock(&lport->disc.disc_mutex);
2506 mutex_lock(&rdata->rp_mutex);
2507 mutex_unlock(&lport->disc.disc_mutex);
2509 rdata->ops = &fcoe_ctlr_vn_rport_ops;
2510 rdata->disc_id = lport->disc.disc_id;
2513 if ((ids->port_name != -1 &&
2514 ids->port_name != new->rdata.ids.port_name) ||
2515 (ids->node_name != -1 &&
2516 ids->node_name != new->rdata.ids.node_name)) {
2517 mutex_unlock(&rdata->rp_mutex);
2518 LIBFCOE_FIP_DBG(fip, "vn_add rport logoff %6.6x\n", port_id);
2519 fc_rport_logoff(rdata);
2520 mutex_lock(&rdata->rp_mutex);
2522 ids->port_name = new->rdata.ids.port_name;
2523 ids->node_name = new->rdata.ids.node_name;
2524 mutex_unlock(&rdata->rp_mutex);
2526 frport = fcoe_ctlr_rport(rdata);
2527 LIBFCOE_FIP_DBG(fip, "vn_add rport %6.6x %s state %d\n",
2528 port_id, frport->fcoe_len ? "old" : "new",
2530 frport->fcoe_len = new->fcoe_len;
2531 frport->flags = new->flags;
2532 frport->login_count = new->login_count;
2533 memcpy(frport->enode_mac, new->enode_mac, ETH_ALEN);
2534 memcpy(frport->vn_mac, new->vn_mac, ETH_ALEN);
2539 * fcoe_ctlr_vn_lookup() - Find VN remote port's MAC address
2540 * @fip: The FCoE controller
2541 * @port_id: The port_id of the remote VN_node
2542 * @mac: buffer which will hold the VN_NODE destination MAC address, if found.
2544 * Returns non-zero error if no remote port found.
2546 static int fcoe_ctlr_vn_lookup(struct fcoe_ctlr *fip, u32 port_id, u8 *mac)
2548 struct fc_lport *lport = fip->lp;
2549 struct fc_rport_priv *rdata;
2550 struct fcoe_rport *frport;
2553 rdata = fc_rport_lookup(lport, port_id);
2555 frport = fcoe_ctlr_rport(rdata);
2556 memcpy(mac, frport->enode_mac, ETH_ALEN);
2558 kref_put(&rdata->kref, fc_rport_destroy);
2564 * fcoe_ctlr_vn_claim_notify() - handle received FIP VN2VN Claim Notification
2565 * @fip: The FCoE controller
2566 * @new: newly-parsed FCoE rport as a template for new rdata
2568 * Called with ctlr_mutex held.
2570 static void fcoe_ctlr_vn_claim_notify(struct fcoe_ctlr *fip,
2571 struct fcoe_rport *new)
2573 if (new->flags & FIP_FL_REC_OR_P2P) {
2574 LIBFCOE_FIP_DBG(fip, "send probe req for P2P/REC\n");
2575 fcoe_ctlr_vn_send(fip, FIP_SC_VN_PROBE_REQ, fcoe_all_vn2vn, 0);
2578 switch (fip->state) {
2579 case FIP_ST_VNMP_START:
2580 case FIP_ST_VNMP_PROBE1:
2581 case FIP_ST_VNMP_PROBE2:
2582 if (new->rdata.ids.port_id == fip->port_id) {
2583 LIBFCOE_FIP_DBG(fip, "vn_claim_notify: "
2584 "restart, state %d\n",
2586 fcoe_ctlr_vn_restart(fip);
2589 case FIP_ST_VNMP_CLAIM:
2590 case FIP_ST_VNMP_UP:
2591 if (new->rdata.ids.port_id == fip->port_id) {
2592 if (new->rdata.ids.port_name > fip->lp->wwpn) {
2593 LIBFCOE_FIP_DBG(fip, "vn_claim_notify: "
2594 "restart, port_id collision\n");
2595 fcoe_ctlr_vn_restart(fip);
2598 LIBFCOE_FIP_DBG(fip, "vn_claim_notify: "
2599 "send claim notify\n");
2600 fcoe_ctlr_vn_send_claim(fip);
2603 LIBFCOE_FIP_DBG(fip, "vn_claim_notify: send reply to %x\n",
2604 new->rdata.ids.port_id);
2605 fcoe_ctlr_vn_send(fip, FIP_SC_VN_CLAIM_REP, new->enode_mac,
2606 min((u32)new->fcoe_len,
2607 fcoe_ctlr_fcoe_size(fip)));
2608 fcoe_ctlr_vn_add(fip, new);
2611 LIBFCOE_FIP_DBG(fip, "vn_claim_notify: "
2612 "ignoring claim from %x\n",
2613 new->rdata.ids.port_id);
2619 * fcoe_ctlr_vn_claim_resp() - handle received Claim Response
2620 * @fip: The FCoE controller that received the frame
2621 * @new: newly-parsed FCoE rport from the Claim Response
2623 * Called with ctlr_mutex held.
2625 static void fcoe_ctlr_vn_claim_resp(struct fcoe_ctlr *fip,
2626 struct fcoe_rport *new)
2628 LIBFCOE_FIP_DBG(fip, "claim resp from from rport %x - state %s\n",
2629 new->rdata.ids.port_id, fcoe_ctlr_state(fip->state));
2630 if (fip->state == FIP_ST_VNMP_UP || fip->state == FIP_ST_VNMP_CLAIM)
2631 fcoe_ctlr_vn_add(fip, new);
2635 * fcoe_ctlr_vn_beacon() - handle received beacon.
2636 * @fip: The FCoE controller that received the frame
2637 * @new: newly-parsed FCoE rport from the Beacon
2639 * Called with ctlr_mutex held.
2641 static void fcoe_ctlr_vn_beacon(struct fcoe_ctlr *fip,
2642 struct fcoe_rport *new)
2644 struct fc_lport *lport = fip->lp;
2645 struct fc_rport_priv *rdata;
2646 struct fcoe_rport *frport;
2648 if (new->flags & FIP_FL_REC_OR_P2P) {
2649 LIBFCOE_FIP_DBG(fip, "p2p beacon while in vn2vn mode\n");
2650 fcoe_ctlr_vn_send(fip, FIP_SC_VN_PROBE_REQ, fcoe_all_vn2vn, 0);
2653 rdata = fc_rport_lookup(lport, new->rdata.ids.port_id);
2655 if (rdata->ids.node_name == new->rdata.ids.node_name &&
2656 rdata->ids.port_name == new->rdata.ids.port_name) {
2657 frport = fcoe_ctlr_rport(rdata);
2659 LIBFCOE_FIP_DBG(fip, "beacon from rport %x\n",
2660 rdata->ids.port_id);
2661 if (!frport->time && fip->state == FIP_ST_VNMP_UP) {
2662 LIBFCOE_FIP_DBG(fip, "beacon expired "
2664 rdata->ids.port_id);
2665 fc_rport_login(rdata);
2667 frport->time = jiffies;
2669 kref_put(&rdata->kref, fc_rport_destroy);
2672 if (fip->state != FIP_ST_VNMP_UP)
2676 * Beacon from a new neighbor.
2677 * Send a claim notify if one hasn't been sent recently.
2678 * Don't add the neighbor yet.
2680 LIBFCOE_FIP_DBG(fip, "beacon from new rport %x. sending claim notify\n",
2681 new->rdata.ids.port_id);
2682 if (time_after(jiffies,
2683 fip->sol_time + msecs_to_jiffies(FIP_VN_ANN_WAIT)))
2684 fcoe_ctlr_vn_send_claim(fip);
2688 * fcoe_ctlr_vn_age() - Check for VN_ports without recent beacons
2689 * @fip: The FCoE controller
2691 * Called with ctlr_mutex held.
2692 * Called only in state FIP_ST_VNMP_UP.
2693 * Returns the soonest time for next age-out or a time far in the future.
2695 static unsigned long fcoe_ctlr_vn_age(struct fcoe_ctlr *fip)
2697 struct fc_lport *lport = fip->lp;
2698 struct fc_rport_priv *rdata;
2699 struct fcoe_rport *frport;
2700 unsigned long next_time;
2701 unsigned long deadline;
2703 next_time = jiffies + msecs_to_jiffies(FIP_VN_BEACON_INT * 10);
2704 mutex_lock(&lport->disc.disc_mutex);
2705 list_for_each_entry_rcu(rdata, &lport->disc.rports, peers) {
2706 if (!kref_get_unless_zero(&rdata->kref))
2708 frport = fcoe_ctlr_rport(rdata);
2709 if (!frport->time) {
2710 kref_put(&rdata->kref, fc_rport_destroy);
2713 deadline = frport->time +
2714 msecs_to_jiffies(FIP_VN_BEACON_INT * 25 / 10);
2715 if (time_after_eq(jiffies, deadline)) {
2717 LIBFCOE_FIP_DBG(fip,
2718 "port %16.16llx fc_id %6.6x beacon expired\n",
2719 rdata->ids.port_name, rdata->ids.port_id);
2720 fc_rport_logoff(rdata);
2721 } else if (time_before(deadline, next_time))
2722 next_time = deadline;
2723 kref_put(&rdata->kref, fc_rport_destroy);
2725 mutex_unlock(&lport->disc.disc_mutex);
2730 * fcoe_ctlr_vn_recv() - Receive a FIP frame
2731 * @fip: The FCoE controller that received the frame
2732 * @skb: The received FIP frame
2734 * Returns non-zero if the frame is dropped.
2735 * Always consumes the frame.
2737 static int fcoe_ctlr_vn_recv(struct fcoe_ctlr *fip, struct sk_buff *skb)
2739 struct fip_header *fiph;
2740 enum fip_vn2vn_subcode sub;
2741 struct fcoe_rport frport = { };
2742 int rc, vlan_id = 0;
2744 fiph = (struct fip_header *)skb->data;
2745 sub = fiph->fip_subcode;
2748 vlan_id = skb_vlan_tag_get_id(skb);
2750 if (vlan_id && vlan_id != fip->lp->vlan) {
2751 LIBFCOE_FIP_DBG(fip, "vn_recv drop frame sub %x vlan %d\n",
2757 rc = fcoe_ctlr_vn_parse(fip, skb, &frport);
2759 LIBFCOE_FIP_DBG(fip, "vn_recv vn_parse error %d\n", rc);
2763 mutex_lock(&fip->ctlr_mutex);
2765 case FIP_SC_VN_PROBE_REQ:
2766 fcoe_ctlr_vn_probe_req(fip, &frport);
2768 case FIP_SC_VN_PROBE_REP:
2769 fcoe_ctlr_vn_probe_reply(fip, &frport);
2771 case FIP_SC_VN_CLAIM_NOTIFY:
2772 fcoe_ctlr_vn_claim_notify(fip, &frport);
2774 case FIP_SC_VN_CLAIM_REP:
2775 fcoe_ctlr_vn_claim_resp(fip, &frport);
2777 case FIP_SC_VN_BEACON:
2778 fcoe_ctlr_vn_beacon(fip, &frport);
2781 LIBFCOE_FIP_DBG(fip, "vn_recv unknown subcode %d\n", sub);
2785 mutex_unlock(&fip->ctlr_mutex);
2792 * fcoe_ctlr_vlan_parse - parse vlan discovery request or response
2793 * @fip: The FCoE controller
2794 * @skb: incoming packet
2795 * @frport: parsed FCoE rport from the probe request
2797 * Returns non-zero error number on error.
2798 * Does not consume the packet.
2800 static int fcoe_ctlr_vlan_parse(struct fcoe_ctlr *fip,
2801 struct sk_buff *skb,
2802 struct fcoe_rport *frport)
2804 struct fip_header *fiph;
2805 struct fip_desc *desc = NULL;
2806 struct fip_mac_desc *macd = NULL;
2807 struct fip_wwn_desc *wwn = NULL;
2814 fiph = (struct fip_header *)skb->data;
2815 frport->flags = ntohs(fiph->fip_flags);
2817 sub = fiph->fip_subcode;
2820 desc_mask = BIT(FIP_DT_MAC) | BIT(FIP_DT_NAME);
2823 LIBFCOE_FIP_DBG(fip, "vn_parse unknown subcode %u\n", sub);
2827 rlen = ntohs(fiph->fip_dl_len) * 4;
2828 if (rlen + sizeof(*fiph) > skb->len)
2831 desc = (struct fip_desc *)(fiph + 1);
2833 dlen = desc->fip_dlen * FIP_BPW;
2834 if (dlen < sizeof(*desc) || dlen > rlen)
2837 dtype = desc->fip_dtype;
2839 if (!(desc_mask & BIT(dtype))) {
2840 LIBFCOE_FIP_DBG(fip,
2841 "unexpected or duplicated desc "
2843 "FIP VN2VN subtype %u\n",
2847 desc_mask &= ~BIT(dtype);
2852 if (dlen != sizeof(struct fip_mac_desc))
2854 macd = (struct fip_mac_desc *)desc;
2855 if (!is_valid_ether_addr(macd->fd_mac)) {
2856 LIBFCOE_FIP_DBG(fip,
2857 "Invalid MAC addr %pM in FIP VN2VN\n",
2861 memcpy(frport->enode_mac, macd->fd_mac, ETH_ALEN);
2864 if (dlen != sizeof(struct fip_wwn_desc))
2866 wwn = (struct fip_wwn_desc *)desc;
2867 frport->rdata.ids.node_name =
2868 get_unaligned_be64(&wwn->fd_wwn);
2871 LIBFCOE_FIP_DBG(fip, "unexpected descriptor type %x "
2872 "in FIP probe\n", dtype);
2873 /* standard says ignore unknown descriptors >= 128 */
2874 if (dtype < FIP_DT_NON_CRITICAL)
2878 desc = (struct fip_desc *)((char *)desc + dlen);
2884 LIBFCOE_FIP_DBG(fip, "FIP length error in descriptor type %x len %zu\n",
2890 * fcoe_ctlr_vlan_send() - Send a FIP VLAN Notification
2891 * @fip: The FCoE controller
2892 * @sub: sub-opcode for vlan notification or vn2vn vlan notification
2893 * @dest: The destination Ethernet MAC address
2895 static void fcoe_ctlr_vlan_send(struct fcoe_ctlr *fip,
2896 enum fip_vlan_subcode sub,
2899 struct sk_buff *skb;
2900 struct fip_vlan_notify_frame {
2902 struct fip_header fip;
2903 struct fip_mac_desc mac;
2904 struct fip_vlan_desc vlan;
2909 len = sizeof(*frame);
2910 dlen = sizeof(frame->mac) + sizeof(frame->vlan);
2911 len = max(len, sizeof(struct ethhdr));
2913 skb = dev_alloc_skb(len);
2917 LIBFCOE_FIP_DBG(fip, "fip %s vlan notification, vlan %d\n",
2918 fip->mode == FIP_MODE_VN2VN ? "vn2vn" : "fcf",
2921 frame = (struct fip_vlan_notify_frame *)skb->data;
2922 memset(frame, 0, len);
2923 memcpy(frame->eth.h_dest, dest, ETH_ALEN);
2925 memcpy(frame->eth.h_source, fip->ctl_src_addr, ETH_ALEN);
2926 frame->eth.h_proto = htons(ETH_P_FIP);
2928 frame->fip.fip_ver = FIP_VER_ENCAPS(FIP_VER);
2929 frame->fip.fip_op = htons(FIP_OP_VLAN);
2930 frame->fip.fip_subcode = sub;
2931 frame->fip.fip_dl_len = htons(dlen / FIP_BPW);
2933 frame->mac.fd_desc.fip_dtype = FIP_DT_MAC;
2934 frame->mac.fd_desc.fip_dlen = sizeof(frame->mac) / FIP_BPW;
2935 memcpy(frame->mac.fd_mac, fip->ctl_src_addr, ETH_ALEN);
2937 frame->vlan.fd_desc.fip_dtype = FIP_DT_VLAN;
2938 frame->vlan.fd_desc.fip_dlen = sizeof(frame->vlan) / FIP_BPW;
2939 put_unaligned_be16(fip->lp->vlan, &frame->vlan.fd_vlan);
2942 skb->protocol = htons(ETH_P_FIP);
2943 skb->priority = fip->priority;
2944 skb_reset_mac_header(skb);
2945 skb_reset_network_header(skb);
2947 fip->send(fip, skb);
2951 * fcoe_ctlr_vlan_disc_reply() - send FIP VLAN Discovery Notification.
2952 * @fip: The FCoE controller
2953 * @frport: The newly-parsed FCoE rport from the Discovery Request
2955 * Called with ctlr_mutex held.
2957 static void fcoe_ctlr_vlan_disc_reply(struct fcoe_ctlr *fip,
2958 struct fcoe_rport *frport)
2960 enum fip_vlan_subcode sub = FIP_SC_VL_NOTE;
2962 if (fip->mode == FIP_MODE_VN2VN)
2963 sub = FIP_SC_VL_VN2VN_NOTE;
2965 fcoe_ctlr_vlan_send(fip, sub, frport->enode_mac);
2969 * fcoe_ctlr_vlan_recv - vlan request receive handler for VN2VN mode.
2970 * @fip: The FCoE controller
2971 * @skb: The received FIP packet
2973 static int fcoe_ctlr_vlan_recv(struct fcoe_ctlr *fip, struct sk_buff *skb)
2975 struct fip_header *fiph;
2976 enum fip_vlan_subcode sub;
2977 struct fcoe_rport frport = { };
2980 fiph = (struct fip_header *)skb->data;
2981 sub = fiph->fip_subcode;
2982 rc = fcoe_ctlr_vlan_parse(fip, skb, &frport);
2984 LIBFCOE_FIP_DBG(fip, "vlan_recv vlan_parse error %d\n", rc);
2987 mutex_lock(&fip->ctlr_mutex);
2988 if (sub == FIP_SC_VL_REQ)
2989 fcoe_ctlr_vlan_disc_reply(fip, &frport);
2990 mutex_unlock(&fip->ctlr_mutex);
2998 * fcoe_ctlr_disc_recv - discovery receive handler for VN2VN mode.
2999 * @lport: The local port
3000 * @fp: The received frame
3002 * This should never be called since we don't see RSCNs or other
3003 * fabric-generated ELSes.
3005 static void fcoe_ctlr_disc_recv(struct fc_lport *lport, struct fc_frame *fp)
3007 struct fc_seq_els_data rjt_data;
3009 rjt_data.reason = ELS_RJT_UNSUP;
3010 rjt_data.explan = ELS_EXPL_NONE;
3011 fc_seq_els_rsp_send(fp, ELS_LS_RJT, &rjt_data);
3016 * fcoe_ctlr_disc_start - start discovery for VN2VN mode.
3018 * This sets a flag indicating that remote ports should be created
3019 * and started for the peers we discover. We use the disc_callback
3020 * pointer as that flag. Peers already discovered are created here.
3022 * The lport lock is held during this call. The callback must be done
3023 * later, without holding either the lport or discovery locks.
3024 * The fcoe_ctlr lock may also be held during this call.
3026 static void fcoe_ctlr_disc_start(void (*callback)(struct fc_lport *,
3027 enum fc_disc_event),
3028 struct fc_lport *lport)
3030 struct fc_disc *disc = &lport->disc;
3031 struct fcoe_ctlr *fip = disc->priv;
3033 mutex_lock(&disc->disc_mutex);
3034 disc->disc_callback = callback;
3035 disc->disc_id = (disc->disc_id + 2) | 1;
3037 schedule_work(&fip->timer_work);
3038 mutex_unlock(&disc->disc_mutex);
3042 * fcoe_ctlr_vn_disc() - report FIP VN_port discovery results after claim state.
3043 * @fip: The FCoE controller
3045 * Starts the FLOGI and PLOGI login process to each discovered rport for which
3046 * we've received at least one beacon.
3047 * Performs the discovery complete callback.
3049 static void fcoe_ctlr_vn_disc(struct fcoe_ctlr *fip)
3051 struct fc_lport *lport = fip->lp;
3052 struct fc_disc *disc = &lport->disc;
3053 struct fc_rport_priv *rdata;
3054 struct fcoe_rport *frport;
3055 void (*callback)(struct fc_lport *, enum fc_disc_event);
3057 mutex_lock(&disc->disc_mutex);
3058 callback = disc->pending ? disc->disc_callback : NULL;
3060 list_for_each_entry_rcu(rdata, &disc->rports, peers) {
3061 if (!kref_get_unless_zero(&rdata->kref))
3063 frport = fcoe_ctlr_rport(rdata);
3065 fc_rport_login(rdata);
3066 kref_put(&rdata->kref, fc_rport_destroy);
3068 mutex_unlock(&disc->disc_mutex);
3070 callback(lport, DISC_EV_SUCCESS);
3074 * fcoe_ctlr_vn_timeout - timer work function for VN2VN mode.
3075 * @fip: The FCoE controller
3077 static void fcoe_ctlr_vn_timeout(struct fcoe_ctlr *fip)
3079 unsigned long next_time;
3081 u32 new_port_id = 0;
3083 mutex_lock(&fip->ctlr_mutex);
3084 switch (fip->state) {
3085 case FIP_ST_VNMP_START:
3086 fcoe_ctlr_set_state(fip, FIP_ST_VNMP_PROBE1);
3087 LIBFCOE_FIP_DBG(fip, "vn_timeout: send 1st probe request\n");
3088 fcoe_ctlr_vn_send(fip, FIP_SC_VN_PROBE_REQ, fcoe_all_vn2vn, 0);
3089 next_time = jiffies + msecs_to_jiffies(FIP_VN_PROBE_WAIT);
3091 case FIP_ST_VNMP_PROBE1:
3092 fcoe_ctlr_set_state(fip, FIP_ST_VNMP_PROBE2);
3093 LIBFCOE_FIP_DBG(fip, "vn_timeout: send 2nd probe request\n");
3094 fcoe_ctlr_vn_send(fip, FIP_SC_VN_PROBE_REQ, fcoe_all_vn2vn, 0);
3095 next_time = jiffies + msecs_to_jiffies(FIP_VN_ANN_WAIT);
3097 case FIP_ST_VNMP_PROBE2:
3098 fcoe_ctlr_set_state(fip, FIP_ST_VNMP_CLAIM);
3099 new_port_id = fip->port_id;
3100 hton24(mac, FIP_VN_FC_MAP);
3101 hton24(mac + 3, new_port_id);
3102 fcoe_ctlr_map_dest(fip);
3103 fip->update_mac(fip->lp, mac);
3104 LIBFCOE_FIP_DBG(fip, "vn_timeout: send claim notify\n");
3105 fcoe_ctlr_vn_send_claim(fip);
3106 next_time = jiffies + msecs_to_jiffies(FIP_VN_ANN_WAIT);
3108 case FIP_ST_VNMP_CLAIM:
3110 * This may be invoked either by starting discovery so don't
3111 * go to the next state unless it's been long enough.
3113 next_time = fip->sol_time + msecs_to_jiffies(FIP_VN_ANN_WAIT);
3114 if (time_after_eq(jiffies, next_time)) {
3115 fcoe_ctlr_set_state(fip, FIP_ST_VNMP_UP);
3116 LIBFCOE_FIP_DBG(fip, "vn_timeout: send vn2vn beacon\n");
3117 fcoe_ctlr_vn_send(fip, FIP_SC_VN_BEACON,
3119 next_time = jiffies + msecs_to_jiffies(FIP_VN_ANN_WAIT);
3120 fip->port_ka_time = next_time;
3122 fcoe_ctlr_vn_disc(fip);
3124 case FIP_ST_VNMP_UP:
3125 next_time = fcoe_ctlr_vn_age(fip);
3126 if (time_after_eq(jiffies, fip->port_ka_time)) {
3127 LIBFCOE_FIP_DBG(fip, "vn_timeout: send vn2vn beacon\n");
3128 fcoe_ctlr_vn_send(fip, FIP_SC_VN_BEACON,
3130 fip->port_ka_time = jiffies +
3131 msecs_to_jiffies(FIP_VN_BEACON_INT +
3132 get_random_u32_below(FIP_VN_BEACON_FUZZ));
3134 if (time_before(fip->port_ka_time, next_time))
3135 next_time = fip->port_ka_time;
3137 case FIP_ST_LINK_WAIT:
3140 WARN(1, "unexpected state %d\n", fip->state);
3143 mod_timer(&fip->timer, next_time);
3145 mutex_unlock(&fip->ctlr_mutex);
3147 /* If port ID is new, notify local port after dropping ctlr_mutex */
3149 fc_lport_set_local_id(fip->lp, new_port_id);
3153 * fcoe_ctlr_mode_set() - Set or reset the ctlr's mode
3154 * @lport: The local port to be (re)configured
3155 * @fip: The FCoE controller whose mode is changing
3156 * @fip_mode: The new fip mode
3158 * Note that the we shouldn't be changing the libfc discovery settings
3159 * (fc_disc_config) while an lport is going through the libfc state
3160 * machine. The mode can only be changed when a fcoe_ctlr device is
3161 * disabled, so that should ensure that this routine is only called
3162 * when nothing is happening.
3164 static void fcoe_ctlr_mode_set(struct fc_lport *lport, struct fcoe_ctlr *fip,
3165 enum fip_mode fip_mode)
3169 WARN_ON(lport->state != LPORT_ST_RESET &&
3170 lport->state != LPORT_ST_DISABLED);
3172 if (fip_mode == FIP_MODE_VN2VN) {
3173 lport->rport_priv_size = sizeof(struct fcoe_rport);
3174 lport->point_to_multipoint = 1;
3175 lport->tt.disc_recv_req = fcoe_ctlr_disc_recv;
3176 lport->tt.disc_start = fcoe_ctlr_disc_start;
3177 lport->tt.disc_stop = fcoe_ctlr_disc_stop;
3178 lport->tt.disc_stop_final = fcoe_ctlr_disc_stop_final;
3181 lport->rport_priv_size = 0;
3182 lport->point_to_multipoint = 0;
3183 lport->tt.disc_recv_req = NULL;
3184 lport->tt.disc_start = NULL;
3185 lport->tt.disc_stop = NULL;
3186 lport->tt.disc_stop_final = NULL;
3190 fc_disc_config(lport, priv);
3194 * fcoe_libfc_config() - Sets up libfc related properties for local port
3195 * @lport: The local port to configure libfc for
3196 * @fip: The FCoE controller in use by the local port
3197 * @tt: The libfc function template
3198 * @init_fcp: If non-zero, the FCP portion of libfc should be initialized
3200 * Returns : 0 for success
3202 int fcoe_libfc_config(struct fc_lport *lport, struct fcoe_ctlr *fip,
3203 const struct libfc_function_template *tt, int init_fcp)
3205 /* Set the function pointers set by the LLDD */
3206 memcpy(&lport->tt, tt, sizeof(*tt));
3207 if (init_fcp && fc_fcp_init(lport))
3209 fc_exch_init(lport);
3210 fc_elsct_init(lport);
3211 fc_lport_init(lport);
3212 fc_disc_init(lport);
3213 fcoe_ctlr_mode_set(lport, fip, fip->mode);
3216 EXPORT_SYMBOL_GPL(fcoe_libfc_config);
3218 void fcoe_fcf_get_selected(struct fcoe_fcf_device *fcf_dev)
3220 struct fcoe_ctlr_device *ctlr_dev = fcoe_fcf_dev_to_ctlr_dev(fcf_dev);
3221 struct fcoe_ctlr *fip = fcoe_ctlr_device_priv(ctlr_dev);
3222 struct fcoe_fcf *fcf;
3224 mutex_lock(&fip->ctlr_mutex);
3225 mutex_lock(&ctlr_dev->lock);
3227 fcf = fcoe_fcf_device_priv(fcf_dev);
3229 fcf_dev->selected = (fcf == fip->sel_fcf) ? 1 : 0;
3231 fcf_dev->selected = 0;
3233 mutex_unlock(&ctlr_dev->lock);
3234 mutex_unlock(&fip->ctlr_mutex);
3236 EXPORT_SYMBOL(fcoe_fcf_get_selected);
3238 void fcoe_ctlr_set_fip_mode(struct fcoe_ctlr_device *ctlr_dev)
3240 struct fcoe_ctlr *ctlr = fcoe_ctlr_device_priv(ctlr_dev);
3241 struct fc_lport *lport = ctlr->lp;
3243 mutex_lock(&ctlr->ctlr_mutex);
3244 switch (ctlr_dev->mode) {
3245 case FIP_CONN_TYPE_VN2VN:
3246 ctlr->mode = FIP_MODE_VN2VN;
3248 case FIP_CONN_TYPE_FABRIC:
3250 ctlr->mode = FIP_MODE_FABRIC;
3254 mutex_unlock(&ctlr->ctlr_mutex);
3256 fcoe_ctlr_mode_set(lport, ctlr, ctlr->mode);
3258 EXPORT_SYMBOL(fcoe_ctlr_set_fip_mode);