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(unsigned long);
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 u8 fcoe_all_fcfs[ETH_ALEN] = FIP_ALL_FCF_MACS;
63 static u8 fcoe_all_enode[ETH_ALEN] = FIP_ALL_ENODE_MACS;
64 static u8 fcoe_all_vn2vn[ETH_ALEN] = FIP_ALL_VN2VN_MACS;
65 static u8 fcoe_all_p2p[ETH_ALEN] = FIP_ALL_P2P_MACS;
67 static const char * const fcoe_ctlr_states[] = {
68 [FIP_ST_DISABLED] = "DISABLED",
69 [FIP_ST_LINK_WAIT] = "LINK_WAIT",
70 [FIP_ST_AUTO] = "AUTO",
71 [FIP_ST_NON_FIP] = "NON_FIP",
72 [FIP_ST_ENABLED] = "ENABLED",
73 [FIP_ST_VNMP_START] = "VNMP_START",
74 [FIP_ST_VNMP_PROBE1] = "VNMP_PROBE1",
75 [FIP_ST_VNMP_PROBE2] = "VNMP_PROBE2",
76 [FIP_ST_VNMP_CLAIM] = "VNMP_CLAIM",
77 [FIP_ST_VNMP_UP] = "VNMP_UP",
80 static const char *fcoe_ctlr_state(enum fip_state state)
82 const char *cp = "unknown";
84 if (state < ARRAY_SIZE(fcoe_ctlr_states))
85 cp = fcoe_ctlr_states[state];
92 * fcoe_ctlr_set_state() - Set and do debug printing for the new FIP state.
93 * @fip: The FCoE controller
94 * @state: The new state
96 static void fcoe_ctlr_set_state(struct fcoe_ctlr *fip, enum fip_state state)
98 if (state == fip->state)
101 LIBFCOE_FIP_DBG(fip, "state %s -> %s\n",
102 fcoe_ctlr_state(fip->state), fcoe_ctlr_state(state));
107 * fcoe_ctlr_mtu_valid() - Check if a FCF's MTU is valid
108 * @fcf: The FCF to check
110 * Return non-zero if FCF fcoe_size has been validated.
112 static inline int fcoe_ctlr_mtu_valid(const struct fcoe_fcf *fcf)
114 return (fcf->flags & FIP_FL_SOL) != 0;
118 * fcoe_ctlr_fcf_usable() - Check if a FCF is usable
119 * @fcf: The FCF to check
121 * Return non-zero if the FCF is usable.
123 static inline int fcoe_ctlr_fcf_usable(struct fcoe_fcf *fcf)
125 u16 flags = FIP_FL_SOL | FIP_FL_AVAIL;
127 return (fcf->flags & flags) == flags;
131 * fcoe_ctlr_map_dest() - Set flag and OUI for mapping destination addresses
132 * @fip: The FCoE controller
134 static void fcoe_ctlr_map_dest(struct fcoe_ctlr *fip)
136 if (fip->mode == FIP_MODE_VN2VN)
137 hton24(fip->dest_addr, FIP_VN_FC_MAP);
139 hton24(fip->dest_addr, FIP_DEF_FC_MAP);
140 hton24(fip->dest_addr + 3, 0);
145 * fcoe_ctlr_init() - Initialize the FCoE Controller instance
146 * @fip: The FCoE controller to initialize
148 void fcoe_ctlr_init(struct fcoe_ctlr *fip, enum fip_state mode)
150 fcoe_ctlr_set_state(fip, FIP_ST_LINK_WAIT);
152 INIT_LIST_HEAD(&fip->fcfs);
153 mutex_init(&fip->ctlr_mutex);
154 spin_lock_init(&fip->ctlr_lock);
155 fip->flogi_oxid = FC_XID_UNKNOWN;
156 setup_timer(&fip->timer, fcoe_ctlr_timeout, (unsigned long)fip);
157 INIT_WORK(&fip->timer_work, fcoe_ctlr_timer_work);
158 INIT_WORK(&fip->recv_work, fcoe_ctlr_recv_work);
159 skb_queue_head_init(&fip->fip_recv_list);
161 EXPORT_SYMBOL(fcoe_ctlr_init);
163 static int fcoe_sysfs_fcf_add(struct fcoe_fcf *new)
165 struct fcoe_ctlr *fip = new->fip;
166 struct fcoe_ctlr_device *ctlr_dev = fcoe_ctlr_to_ctlr_dev(fip);
167 struct fcoe_fcf_device *temp, *fcf_dev;
170 LIBFCOE_FIP_DBG(fip, "New FCF fab %16.16llx mac %pM\n",
171 new->fabric_name, new->fcf_mac);
173 temp = kzalloc(sizeof(*temp), GFP_KERNEL);
177 mutex_lock(&ctlr_dev->lock);
179 temp->fabric_name = new->fabric_name;
180 temp->switch_name = new->switch_name;
181 temp->fc_map = new->fc_map;
182 temp->vfid = new->vfid;
183 memcpy(temp->mac, new->fcf_mac, ETH_ALEN);
184 temp->priority = new->pri;
185 temp->fka_period = new->fka_period;
186 temp->selected = 0; /* default to unselected */
188 fcf_dev = fcoe_fcf_device_add(ctlr_dev, temp);
189 if (unlikely(!fcf_dev))
193 * The fcoe_sysfs layer can return a CONNECTED fcf that
194 * has a priv (fcf was never deleted) or a CONNECTED fcf
195 * that doesn't have a priv (fcf was deleted). However,
196 * libfcoe will always delete FCFs before trying to add
197 * them. This is ensured because both recv_adv and
198 * age_fcfs are protected by the the fcoe_ctlr's mutex.
199 * This means that we should never get a FCF with a
200 * non-NULL priv pointer.
202 BUG_ON(fcf_dev->priv);
205 new->fcf_dev = fcf_dev;
207 list_add(&new->list, &fip->fcfs);
212 mutex_unlock(&ctlr_dev->lock);
219 static void fcoe_sysfs_fcf_del(struct fcoe_fcf *new)
221 struct fcoe_ctlr *fip = new->fip;
222 struct fcoe_ctlr_device *ctlr_dev = fcoe_ctlr_to_ctlr_dev(fip);
223 struct fcoe_fcf_device *fcf_dev;
225 list_del(&new->list);
228 mutex_lock(&ctlr_dev->lock);
230 fcf_dev = fcoe_fcf_to_fcf_dev(new);
233 fcoe_fcf_device_delete(fcf_dev);
236 mutex_unlock(&ctlr_dev->lock);
240 * fcoe_ctlr_reset_fcfs() - Reset and free all FCFs for a controller
241 * @fip: The FCoE controller whose FCFs are to be reset
243 * Called with &fcoe_ctlr lock held.
245 static void fcoe_ctlr_reset_fcfs(struct fcoe_ctlr *fip)
247 struct fcoe_fcf *fcf;
248 struct fcoe_fcf *next;
251 list_for_each_entry_safe(fcf, next, &fip->fcfs, list) {
252 fcoe_sysfs_fcf_del(fcf);
254 WARN_ON(fip->fcf_count);
260 * fcoe_ctlr_destroy() - Disable and tear down a FCoE controller
261 * @fip: The FCoE controller to tear down
263 * This is called by FCoE drivers before freeing the &fcoe_ctlr.
265 * The receive handler will have been deleted before this to guarantee
266 * that no more recv_work will be scheduled.
268 * The timer routine will simply return once we set FIP_ST_DISABLED.
269 * This guarantees that no further timeouts or work will be scheduled.
271 void fcoe_ctlr_destroy(struct fcoe_ctlr *fip)
273 cancel_work_sync(&fip->recv_work);
274 skb_queue_purge(&fip->fip_recv_list);
276 mutex_lock(&fip->ctlr_mutex);
277 fcoe_ctlr_set_state(fip, FIP_ST_DISABLED);
278 fcoe_ctlr_reset_fcfs(fip);
279 mutex_unlock(&fip->ctlr_mutex);
280 del_timer_sync(&fip->timer);
281 cancel_work_sync(&fip->timer_work);
283 EXPORT_SYMBOL(fcoe_ctlr_destroy);
286 * fcoe_ctlr_announce() - announce new FCF selection
287 * @fip: The FCoE controller
289 * Also sets the destination MAC for FCoE and control packets
291 * Called with neither ctlr_mutex nor ctlr_lock held.
293 static void fcoe_ctlr_announce(struct fcoe_ctlr *fip)
295 struct fcoe_fcf *sel;
296 struct fcoe_fcf *fcf;
298 mutex_lock(&fip->ctlr_mutex);
299 spin_lock_bh(&fip->ctlr_lock);
301 kfree_skb(fip->flogi_req);
302 fip->flogi_req = NULL;
303 list_for_each_entry(fcf, &fip->fcfs, list)
306 spin_unlock_bh(&fip->ctlr_lock);
309 if (sel && !compare_ether_addr(sel->fcf_mac, fip->dest_addr))
311 if (!is_zero_ether_addr(fip->dest_addr)) {
312 printk(KERN_NOTICE "libfcoe: host%d: "
313 "FIP Fibre-Channel Forwarder MAC %pM deselected\n",
314 fip->lp->host->host_no, fip->dest_addr);
315 memset(fip->dest_addr, 0, ETH_ALEN);
318 printk(KERN_INFO "libfcoe: host%d: FIP selected "
319 "Fibre-Channel Forwarder MAC %pM\n",
320 fip->lp->host->host_no, sel->fcf_mac);
321 memcpy(fip->dest_addr, sel->fcoe_mac, ETH_ALEN);
325 mutex_unlock(&fip->ctlr_mutex);
329 * fcoe_ctlr_fcoe_size() - Return the maximum FCoE size required for VN_Port
330 * @fip: The FCoE controller to get the maximum FCoE size from
332 * Returns the maximum packet size including the FCoE header and trailer,
333 * but not including any Ethernet or VLAN headers.
335 static inline u32 fcoe_ctlr_fcoe_size(struct fcoe_ctlr *fip)
338 * Determine the max FCoE frame size allowed, including
339 * FCoE header and trailer.
340 * Note: lp->mfs is currently the payload size, not the frame size.
342 return fip->lp->mfs + sizeof(struct fc_frame_header) +
343 sizeof(struct fcoe_hdr) + sizeof(struct fcoe_crc_eof);
347 * fcoe_ctlr_solicit() - Send a FIP solicitation
348 * @fip: The FCoE controller to send the solicitation on
349 * @fcf: The destination FCF (if NULL, a multicast solicitation is sent)
351 static void fcoe_ctlr_solicit(struct fcoe_ctlr *fip, struct fcoe_fcf *fcf)
356 struct fip_header fip;
358 struct fip_mac_desc mac;
359 struct fip_wwn_desc wwnn;
360 struct fip_size_desc size;
365 skb = dev_alloc_skb(sizeof(*sol));
369 sol = (struct fip_sol *)skb->data;
371 memset(sol, 0, sizeof(*sol));
372 memcpy(sol->eth.h_dest, fcf ? fcf->fcf_mac : fcoe_all_fcfs, ETH_ALEN);
373 memcpy(sol->eth.h_source, fip->ctl_src_addr, ETH_ALEN);
374 sol->eth.h_proto = htons(ETH_P_FIP);
376 sol->fip.fip_ver = FIP_VER_ENCAPS(FIP_VER);
377 sol->fip.fip_op = htons(FIP_OP_DISC);
378 sol->fip.fip_subcode = FIP_SC_SOL;
379 sol->fip.fip_dl_len = htons(sizeof(sol->desc) / FIP_BPW);
380 sol->fip.fip_flags = htons(FIP_FL_FPMA);
382 sol->fip.fip_flags |= htons(FIP_FL_SPMA);
384 sol->desc.mac.fd_desc.fip_dtype = FIP_DT_MAC;
385 sol->desc.mac.fd_desc.fip_dlen = sizeof(sol->desc.mac) / FIP_BPW;
386 memcpy(sol->desc.mac.fd_mac, fip->ctl_src_addr, ETH_ALEN);
388 sol->desc.wwnn.fd_desc.fip_dtype = FIP_DT_NAME;
389 sol->desc.wwnn.fd_desc.fip_dlen = sizeof(sol->desc.wwnn) / FIP_BPW;
390 put_unaligned_be64(fip->lp->wwnn, &sol->desc.wwnn.fd_wwn);
392 fcoe_size = fcoe_ctlr_fcoe_size(fip);
393 sol->desc.size.fd_desc.fip_dtype = FIP_DT_FCOE_SIZE;
394 sol->desc.size.fd_desc.fip_dlen = sizeof(sol->desc.size) / FIP_BPW;
395 sol->desc.size.fd_size = htons(fcoe_size);
397 skb_put(skb, sizeof(*sol));
398 skb->protocol = htons(ETH_P_FIP);
399 skb->priority = fip->priority;
400 skb_reset_mac_header(skb);
401 skb_reset_network_header(skb);
405 fip->sol_time = jiffies;
409 * fcoe_ctlr_link_up() - Start FCoE controller
410 * @fip: The FCoE controller to start
412 * Called from the LLD when the network link is ready.
414 void fcoe_ctlr_link_up(struct fcoe_ctlr *fip)
416 mutex_lock(&fip->ctlr_mutex);
417 if (fip->state == FIP_ST_NON_FIP || fip->state == FIP_ST_AUTO) {
418 mutex_unlock(&fip->ctlr_mutex);
420 } else if (fip->state == FIP_ST_LINK_WAIT) {
421 fcoe_ctlr_set_state(fip, fip->mode);
424 LIBFCOE_FIP_DBG(fip, "invalid mode %d\n", fip->mode);
427 LIBFCOE_FIP_DBG(fip, "%s", "setting AUTO mode.\n");
429 case FIP_MODE_FABRIC:
430 case FIP_MODE_NON_FIP:
431 mutex_unlock(&fip->ctlr_mutex);
433 fcoe_ctlr_solicit(fip, NULL);
436 fcoe_ctlr_vn_start(fip);
437 mutex_unlock(&fip->ctlr_mutex);
442 mutex_unlock(&fip->ctlr_mutex);
444 EXPORT_SYMBOL(fcoe_ctlr_link_up);
447 * fcoe_ctlr_reset() - Reset a FCoE controller
448 * @fip: The FCoE controller to reset
450 static void fcoe_ctlr_reset(struct fcoe_ctlr *fip)
452 fcoe_ctlr_reset_fcfs(fip);
453 del_timer(&fip->timer);
454 fip->ctlr_ka_time = 0;
455 fip->port_ka_time = 0;
457 fip->flogi_oxid = FC_XID_UNKNOWN;
458 fcoe_ctlr_map_dest(fip);
462 * fcoe_ctlr_link_down() - Stop a FCoE controller
463 * @fip: The FCoE controller to be stopped
465 * Returns non-zero if the link was up and now isn't.
467 * Called from the LLD when the network link is not ready.
468 * There may be multiple calls while the link is down.
470 int fcoe_ctlr_link_down(struct fcoe_ctlr *fip)
474 LIBFCOE_FIP_DBG(fip, "link down.\n");
475 mutex_lock(&fip->ctlr_mutex);
476 fcoe_ctlr_reset(fip);
477 link_dropped = fip->state != FIP_ST_LINK_WAIT;
478 fcoe_ctlr_set_state(fip, FIP_ST_LINK_WAIT);
479 mutex_unlock(&fip->ctlr_mutex);
482 fc_linkdown(fip->lp);
485 EXPORT_SYMBOL(fcoe_ctlr_link_down);
488 * fcoe_ctlr_send_keep_alive() - Send a keep-alive to the selected FCF
489 * @fip: The FCoE controller to send the FKA on
490 * @lport: libfc fc_lport to send from
491 * @ports: 0 for controller keep-alive, 1 for port keep-alive
492 * @sa: The source MAC address
494 * A controller keep-alive is sent every fka_period (typically 8 seconds).
495 * The source MAC is the native MAC address.
497 * A port keep-alive is sent every 90 seconds while logged in.
498 * The source MAC is the assigned mapped source address.
499 * The destination is the FCF's F-port.
501 static void fcoe_ctlr_send_keep_alive(struct fcoe_ctlr *fip,
502 struct fc_lport *lport,
508 struct fip_header fip;
509 struct fip_mac_desc mac;
511 struct fip_vn_desc *vn;
514 struct fcoe_fcf *fcf;
518 if (!fcf || (ports && !lp->port_id))
521 len = sizeof(*kal) + ports * sizeof(*vn);
522 skb = dev_alloc_skb(len);
526 kal = (struct fip_kal *)skb->data;
528 memcpy(kal->eth.h_dest, fcf->fcf_mac, ETH_ALEN);
529 memcpy(kal->eth.h_source, sa, ETH_ALEN);
530 kal->eth.h_proto = htons(ETH_P_FIP);
532 kal->fip.fip_ver = FIP_VER_ENCAPS(FIP_VER);
533 kal->fip.fip_op = htons(FIP_OP_CTRL);
534 kal->fip.fip_subcode = FIP_SC_KEEP_ALIVE;
535 kal->fip.fip_dl_len = htons((sizeof(kal->mac) +
536 ports * sizeof(*vn)) / FIP_BPW);
537 kal->fip.fip_flags = htons(FIP_FL_FPMA);
539 kal->fip.fip_flags |= htons(FIP_FL_SPMA);
541 kal->mac.fd_desc.fip_dtype = FIP_DT_MAC;
542 kal->mac.fd_desc.fip_dlen = sizeof(kal->mac) / FIP_BPW;
543 memcpy(kal->mac.fd_mac, fip->ctl_src_addr, ETH_ALEN);
545 vn = (struct fip_vn_desc *)(kal + 1);
546 vn->fd_desc.fip_dtype = FIP_DT_VN_ID;
547 vn->fd_desc.fip_dlen = sizeof(*vn) / FIP_BPW;
548 memcpy(vn->fd_mac, fip->get_src_addr(lport), ETH_ALEN);
549 hton24(vn->fd_fc_id, lport->port_id);
550 put_unaligned_be64(lport->wwpn, &vn->fd_wwpn);
553 skb->protocol = htons(ETH_P_FIP);
554 skb->priority = fip->priority;
555 skb_reset_mac_header(skb);
556 skb_reset_network_header(skb);
561 * fcoe_ctlr_encaps() - Encapsulate an ELS frame for FIP, without sending it
562 * @fip: The FCoE controller for the ELS frame
563 * @dtype: The FIP descriptor type for the frame
564 * @skb: The FCoE ELS frame including FC header but no FCoE headers
565 * @d_id: The destination port ID.
567 * Returns non-zero error code on failure.
569 * The caller must check that the length is a multiple of 4.
571 * The @skb must have enough headroom (28 bytes) and tailroom (8 bytes).
572 * Headroom includes the FIP encapsulation description, FIP header, and
573 * Ethernet header. The tailroom is for the FIP MAC descriptor.
575 static int fcoe_ctlr_encaps(struct fcoe_ctlr *fip, struct fc_lport *lport,
576 u8 dtype, struct sk_buff *skb, u32 d_id)
578 struct fip_encaps_head {
580 struct fip_header fip;
581 struct fip_encaps encaps;
583 struct fc_frame_header *fh;
584 struct fip_mac_desc *mac;
585 struct fcoe_fcf *fcf;
590 fh = (struct fc_frame_header *)skb->data;
591 op = *(u8 *)(fh + 1);
592 dlen = sizeof(struct fip_encaps) + skb->len; /* len before push */
593 cap = (struct fip_encaps_head *)skb_push(skb, sizeof(*cap));
594 memset(cap, 0, sizeof(*cap));
596 if (lport->point_to_multipoint) {
597 if (fcoe_ctlr_vn_lookup(fip, d_id, cap->eth.h_dest))
604 fip_flags = fcf->flags;
605 fip_flags &= fip->spma ? FIP_FL_SPMA | FIP_FL_FPMA :
609 memcpy(cap->eth.h_dest, fcf->fcf_mac, ETH_ALEN);
611 memcpy(cap->eth.h_source, fip->ctl_src_addr, ETH_ALEN);
612 cap->eth.h_proto = htons(ETH_P_FIP);
614 cap->fip.fip_ver = FIP_VER_ENCAPS(FIP_VER);
615 cap->fip.fip_op = htons(FIP_OP_LS);
616 if (op == ELS_LS_ACC || op == ELS_LS_RJT)
617 cap->fip.fip_subcode = FIP_SC_REP;
619 cap->fip.fip_subcode = FIP_SC_REQ;
620 cap->fip.fip_flags = htons(fip_flags);
622 cap->encaps.fd_desc.fip_dtype = dtype;
623 cap->encaps.fd_desc.fip_dlen = dlen / FIP_BPW;
625 if (op != ELS_LS_RJT) {
626 dlen += sizeof(*mac);
627 mac = (struct fip_mac_desc *)skb_put(skb, sizeof(*mac));
628 memset(mac, 0, sizeof(*mac));
629 mac->fd_desc.fip_dtype = FIP_DT_MAC;
630 mac->fd_desc.fip_dlen = sizeof(*mac) / FIP_BPW;
631 if (dtype != FIP_DT_FLOGI && dtype != FIP_DT_FDISC) {
632 memcpy(mac->fd_mac, fip->get_src_addr(lport), ETH_ALEN);
633 } else if (fip->mode == FIP_MODE_VN2VN) {
634 hton24(mac->fd_mac, FIP_VN_FC_MAP);
635 hton24(mac->fd_mac + 3, fip->port_id);
636 } else if (fip_flags & FIP_FL_SPMA) {
637 LIBFCOE_FIP_DBG(fip, "FLOGI/FDISC sent with SPMA\n");
638 memcpy(mac->fd_mac, fip->ctl_src_addr, ETH_ALEN);
640 LIBFCOE_FIP_DBG(fip, "FLOGI/FDISC sent with FPMA\n");
641 /* FPMA only FLOGI. Must leave the MAC desc zeroed. */
644 cap->fip.fip_dl_len = htons(dlen / FIP_BPW);
646 skb->protocol = htons(ETH_P_FIP);
647 skb->priority = fip->priority;
648 skb_reset_mac_header(skb);
649 skb_reset_network_header(skb);
654 * fcoe_ctlr_els_send() - Send an ELS frame encapsulated by FIP if appropriate.
655 * @fip: FCoE controller.
656 * @lport: libfc fc_lport to send from
657 * @skb: FCoE ELS frame including FC header but no FCoE headers.
659 * Returns a non-zero error code if the frame should not be sent.
660 * Returns zero if the caller should send the frame with FCoE encapsulation.
662 * The caller must check that the length is a multiple of 4.
663 * The SKB must have enough headroom (28 bytes) and tailroom (8 bytes).
664 * The the skb must also be an fc_frame.
666 * This is called from the lower-level driver with spinlocks held,
667 * so we must not take a mutex here.
669 int fcoe_ctlr_els_send(struct fcoe_ctlr *fip, struct fc_lport *lport,
673 struct fc_frame_header *fh;
678 fp = container_of(skb, struct fc_frame, skb);
679 fh = (struct fc_frame_header *)skb->data;
680 op = *(u8 *)(fh + 1);
682 if (op == ELS_FLOGI && fip->mode != FIP_MODE_VN2VN) {
683 old_xid = fip->flogi_oxid;
684 fip->flogi_oxid = ntohs(fh->fh_ox_id);
685 if (fip->state == FIP_ST_AUTO) {
686 if (old_xid == FC_XID_UNKNOWN)
687 fip->flogi_count = 0;
689 if (fip->flogi_count < 3)
691 fcoe_ctlr_map_dest(fip);
694 if (fip->state == FIP_ST_NON_FIP)
695 fcoe_ctlr_map_dest(fip);
698 if (fip->state == FIP_ST_NON_FIP)
700 if (!fip->sel_fcf && fip->mode != FIP_MODE_VN2VN)
705 if (fip->mode == FIP_MODE_VN2VN)
707 spin_lock_bh(&fip->ctlr_lock);
708 kfree_skb(fip->flogi_req);
709 fip->flogi_req = skb;
710 fip->flogi_req_send = 1;
711 spin_unlock_bh(&fip->ctlr_lock);
712 schedule_work(&fip->timer_work);
715 if (ntoh24(fh->fh_s_id))
720 if (fip->mode == FIP_MODE_VN2VN) {
721 if (fip->state != FIP_ST_VNMP_UP)
723 if (ntoh24(fh->fh_d_id) == FC_FID_FLOGI)
726 if (fip->state != FIP_ST_ENABLED)
728 if (ntoh24(fh->fh_d_id) != FC_FID_FLOGI)
735 * If non-FIP, we may have gotten an SID by accepting an FLOGI
736 * from a point-to-point connection. Switch to using
737 * the source mac based on the SID. The destination
738 * MAC in this case would have been set by receiving the
741 if (fip->state == FIP_ST_NON_FIP) {
742 if (fip->flogi_oxid == FC_XID_UNKNOWN)
744 fip->flogi_oxid = FC_XID_UNKNOWN;
745 fc_fcoe_set_mac(mac, fh->fh_d_id);
746 fip->update_mac(lport, mac);
755 if (fip->state != FIP_ST_ENABLED &&
756 fip->state != FIP_ST_VNMP_UP)
760 LIBFCOE_FIP_DBG(fip, "els_send op %u d_id %x\n",
761 op, ntoh24(fh->fh_d_id));
762 if (fcoe_ctlr_encaps(fip, lport, op, skb, ntoh24(fh->fh_d_id)))
770 EXPORT_SYMBOL(fcoe_ctlr_els_send);
773 * fcoe_ctlr_age_fcfs() - Reset and free all old FCFs for a controller
774 * @fip: The FCoE controller to free FCFs on
776 * Called with lock held and preemption disabled.
778 * An FCF is considered old if we have missed two advertisements.
779 * That is, there have been no valid advertisement from it for 2.5
780 * times its keep-alive period.
782 * In addition, determine the time when an FCF selection can occur.
784 * Also, increment the MissDiscAdvCount when no advertisement is received
785 * for the corresponding FCF for 1.5 * FKA_ADV_PERIOD (FC-BB-5 LESB).
787 * Returns the time in jiffies for the next call.
789 static unsigned long fcoe_ctlr_age_fcfs(struct fcoe_ctlr *fip)
791 struct fcoe_fcf *fcf;
792 struct fcoe_fcf *next;
793 unsigned long next_timer = jiffies + msecs_to_jiffies(FIP_VN_KA_PERIOD);
794 unsigned long deadline;
795 unsigned long sel_time = 0;
796 struct list_head del_list;
797 struct fc_stats *stats;
799 INIT_LIST_HEAD(&del_list);
801 stats = per_cpu_ptr(fip->lp->stats, get_cpu());
803 list_for_each_entry_safe(fcf, next, &fip->fcfs, list) {
804 deadline = fcf->time + fcf->fka_period + fcf->fka_period / 2;
805 if (fip->sel_fcf == fcf) {
806 if (time_after(jiffies, deadline)) {
807 stats->MissDiscAdvCount++;
808 printk(KERN_INFO "libfcoe: host%d: "
809 "Missing Discovery Advertisement "
810 "for fab %16.16llx count %lld\n",
811 fip->lp->host->host_no, fcf->fabric_name,
812 stats->MissDiscAdvCount);
813 } else if (time_after(next_timer, deadline))
814 next_timer = deadline;
817 deadline += fcf->fka_period;
818 if (time_after_eq(jiffies, deadline)) {
819 if (fip->sel_fcf == fcf)
822 * Move to delete list so we can call
823 * fcoe_sysfs_fcf_del (which can sleep)
824 * after the put_cpu().
826 list_del(&fcf->list);
827 list_add(&fcf->list, &del_list);
828 stats->VLinkFailureCount++;
830 if (time_after(next_timer, deadline))
831 next_timer = deadline;
832 if (fcoe_ctlr_mtu_valid(fcf) &&
833 (!sel_time || time_before(sel_time, fcf->time)))
834 sel_time = fcf->time;
839 list_for_each_entry_safe(fcf, next, &del_list, list) {
840 /* Removes fcf from current list */
841 fcoe_sysfs_fcf_del(fcf);
844 if (sel_time && !fip->sel_fcf && !fip->sel_time) {
845 sel_time += msecs_to_jiffies(FCOE_CTLR_START_DELAY);
846 fip->sel_time = sel_time;
853 * fcoe_ctlr_parse_adv() - Decode a FIP advertisement into a new FCF entry
854 * @fip: The FCoE controller receiving the advertisement
855 * @skb: The received FIP advertisement frame
856 * @fcf: The resulting FCF entry
858 * Returns zero on a valid parsed advertisement,
859 * otherwise returns non zero value.
861 static int fcoe_ctlr_parse_adv(struct fcoe_ctlr *fip,
862 struct sk_buff *skb, struct fcoe_fcf *fcf)
864 struct fip_header *fiph;
865 struct fip_desc *desc = NULL;
866 struct fip_wwn_desc *wwn;
867 struct fip_fab_desc *fab;
868 struct fip_fka_desc *fka;
874 memset(fcf, 0, sizeof(*fcf));
875 fcf->fka_period = msecs_to_jiffies(FCOE_CTLR_DEF_FKA);
877 fiph = (struct fip_header *)skb->data;
878 fcf->flags = ntohs(fiph->fip_flags);
881 * mask of required descriptors. validating each one clears its bit.
883 desc_mask = BIT(FIP_DT_PRI) | BIT(FIP_DT_MAC) | BIT(FIP_DT_NAME) |
884 BIT(FIP_DT_FAB) | BIT(FIP_DT_FKA);
886 rlen = ntohs(fiph->fip_dl_len) * 4;
887 if (rlen + sizeof(*fiph) > skb->len)
890 desc = (struct fip_desc *)(fiph + 1);
892 dlen = desc->fip_dlen * FIP_BPW;
893 if (dlen < sizeof(*desc) || dlen > rlen)
895 /* Drop Adv if there are duplicate critical descriptors */
896 if ((desc->fip_dtype < 32) &&
897 !(desc_mask & 1U << desc->fip_dtype)) {
898 LIBFCOE_FIP_DBG(fip, "Duplicate Critical "
899 "Descriptors in FIP adv\n");
902 switch (desc->fip_dtype) {
904 if (dlen != sizeof(struct fip_pri_desc))
906 fcf->pri = ((struct fip_pri_desc *)desc)->fd_pri;
907 desc_mask &= ~BIT(FIP_DT_PRI);
910 if (dlen != sizeof(struct fip_mac_desc))
913 ((struct fip_mac_desc *)desc)->fd_mac,
915 memcpy(fcf->fcoe_mac, fcf->fcf_mac, ETH_ALEN);
916 if (!is_valid_ether_addr(fcf->fcf_mac)) {
918 "Invalid MAC addr %pM in FIP adv\n",
922 desc_mask &= ~BIT(FIP_DT_MAC);
925 if (dlen != sizeof(struct fip_wwn_desc))
927 wwn = (struct fip_wwn_desc *)desc;
928 fcf->switch_name = get_unaligned_be64(&wwn->fd_wwn);
929 desc_mask &= ~BIT(FIP_DT_NAME);
932 if (dlen != sizeof(struct fip_fab_desc))
934 fab = (struct fip_fab_desc *)desc;
935 fcf->fabric_name = get_unaligned_be64(&fab->fd_wwn);
936 fcf->vfid = ntohs(fab->fd_vfid);
937 fcf->fc_map = ntoh24(fab->fd_map);
938 desc_mask &= ~BIT(FIP_DT_FAB);
941 if (dlen != sizeof(struct fip_fka_desc))
943 fka = (struct fip_fka_desc *)desc;
944 if (fka->fd_flags & FIP_FKA_ADV_D)
946 t = ntohl(fka->fd_fka_period);
947 if (t >= FCOE_CTLR_MIN_FKA)
948 fcf->fka_period = msecs_to_jiffies(t);
949 desc_mask &= ~BIT(FIP_DT_FKA);
952 case FIP_DT_FCOE_SIZE:
958 LIBFCOE_FIP_DBG(fip, "unexpected descriptor type %x "
959 "in FIP adv\n", desc->fip_dtype);
960 /* standard says ignore unknown descriptors >= 128 */
961 if (desc->fip_dtype < FIP_DT_VENDOR_BASE)
965 desc = (struct fip_desc *)((char *)desc + dlen);
968 if (!fcf->fc_map || (fcf->fc_map & 0x10000))
970 if (!fcf->switch_name)
973 LIBFCOE_FIP_DBG(fip, "adv missing descriptors mask %x\n",
980 LIBFCOE_FIP_DBG(fip, "FIP length error in descriptor type %x len %zu\n",
981 desc->fip_dtype, dlen);
986 * fcoe_ctlr_recv_adv() - Handle an incoming advertisement
987 * @fip: The FCoE controller receiving the advertisement
988 * @skb: The received FIP packet
990 static void fcoe_ctlr_recv_adv(struct fcoe_ctlr *fip, struct sk_buff *skb)
992 struct fcoe_fcf *fcf;
994 unsigned long sol_tov = msecs_to_jiffies(FCOE_CTRL_SOL_TOV);
1000 if (fcoe_ctlr_parse_adv(fip, skb, &new))
1003 mutex_lock(&fip->ctlr_mutex);
1004 first = list_empty(&fip->fcfs);
1005 list_for_each_entry(fcf, &fip->fcfs, list) {
1006 if (fcf->switch_name == new.switch_name &&
1007 fcf->fabric_name == new.fabric_name &&
1008 fcf->fc_map == new.fc_map &&
1009 compare_ether_addr(fcf->fcf_mac, new.fcf_mac) == 0) {
1015 if (fip->fcf_count >= FCOE_CTLR_FCF_LIMIT)
1018 fcf = kmalloc(sizeof(*fcf), GFP_ATOMIC);
1022 memcpy(fcf, &new, sizeof(new));
1024 rc = fcoe_sysfs_fcf_add(fcf);
1026 printk(KERN_ERR "Failed to allocate sysfs instance "
1027 "for FCF, fab %16.16llx mac %pM\n",
1028 new.fabric_name, new.fcf_mac);
1034 * Update the FCF's keep-alive descriptor flags.
1035 * Other flag changes from new advertisements are
1036 * ignored after a solicited advertisement is
1037 * received and the FCF is selectable (usable).
1039 fcf->fd_flags = new.fd_flags;
1040 if (!fcoe_ctlr_fcf_usable(fcf))
1041 fcf->flags = new.flags;
1043 if (fcf == fip->sel_fcf && !fcf->fd_flags) {
1044 fip->ctlr_ka_time -= fcf->fka_period;
1045 fip->ctlr_ka_time += new.fka_period;
1046 if (time_before(fip->ctlr_ka_time, fip->timer.expires))
1047 mod_timer(&fip->timer, fip->ctlr_ka_time);
1049 fcf->fka_period = new.fka_period;
1050 memcpy(fcf->fcf_mac, new.fcf_mac, ETH_ALEN);
1053 mtu_valid = fcoe_ctlr_mtu_valid(fcf);
1054 fcf->time = jiffies;
1056 LIBFCOE_FIP_DBG(fip, "New FCF fab %16.16llx mac %pM\n",
1057 fcf->fabric_name, fcf->fcf_mac);
1060 * If this advertisement is not solicited and our max receive size
1061 * hasn't been verified, send a solicited advertisement.
1064 fcoe_ctlr_solicit(fip, fcf);
1067 * If its been a while since we did a solicit, and this is
1068 * the first advertisement we've received, do a multicast
1069 * solicitation to gather as many advertisements as we can
1070 * before selection occurs.
1072 if (first && time_after(jiffies, fip->sol_time + sol_tov))
1073 fcoe_ctlr_solicit(fip, NULL);
1076 * Put this FCF at the head of the list for priority among equals.
1077 * This helps in the case of an NPV switch which insists we use
1078 * the FCF that answers multicast solicitations, not the others that
1079 * are sending periodic multicast advertisements.
1082 list_move(&fcf->list, &fip->fcfs);
1085 * If this is the first validated FCF, note the time and
1086 * set a timer to trigger selection.
1088 if (mtu_valid && !fip->sel_fcf && fcoe_ctlr_fcf_usable(fcf)) {
1089 fip->sel_time = jiffies +
1090 msecs_to_jiffies(FCOE_CTLR_START_DELAY);
1091 if (!timer_pending(&fip->timer) ||
1092 time_before(fip->sel_time, fip->timer.expires))
1093 mod_timer(&fip->timer, fip->sel_time);
1097 mutex_unlock(&fip->ctlr_mutex);
1101 * fcoe_ctlr_recv_els() - Handle an incoming FIP encapsulated ELS frame
1102 * @fip: The FCoE controller which received the packet
1103 * @skb: The received FIP packet
1105 static void fcoe_ctlr_recv_els(struct fcoe_ctlr *fip, struct sk_buff *skb)
1107 struct fc_lport *lport = fip->lp;
1108 struct fip_header *fiph;
1109 struct fc_frame *fp = (struct fc_frame *)skb;
1110 struct fc_frame_header *fh = NULL;
1111 struct fip_desc *desc;
1112 struct fip_encaps *els;
1113 struct fcoe_fcf *sel;
1114 struct fc_stats *stats;
1115 enum fip_desc_type els_dtype = 0;
1118 u8 granted_mac[ETH_ALEN] = { 0 };
1125 fiph = (struct fip_header *)skb->data;
1126 sub = fiph->fip_subcode;
1127 if (sub != FIP_SC_REQ && sub != FIP_SC_REP)
1130 rlen = ntohs(fiph->fip_dl_len) * 4;
1131 if (rlen + sizeof(*fiph) > skb->len)
1134 desc = (struct fip_desc *)(fiph + 1);
1137 dlen = desc->fip_dlen * FIP_BPW;
1138 if (dlen < sizeof(*desc) || dlen > rlen)
1140 /* Drop ELS if there are duplicate critical descriptors */
1141 if (desc->fip_dtype < 32) {
1142 if ((desc->fip_dtype != FIP_DT_MAC) &&
1143 (desc_mask & 1U << desc->fip_dtype)) {
1144 LIBFCOE_FIP_DBG(fip, "Duplicate Critical "
1145 "Descriptors in FIP ELS\n");
1148 desc_mask |= (1 << desc->fip_dtype);
1150 switch (desc->fip_dtype) {
1153 if (desc_cnt == 1) {
1154 LIBFCOE_FIP_DBG(fip, "FIP descriptors "
1155 "received out of order\n");
1159 * Some switch implementations send two MAC descriptors,
1160 * with first MAC(granted_mac) being the FPMA, and the
1161 * second one(fcoe_mac) is used as destination address
1162 * for sending/receiving FCoE packets. FIP traffic is
1163 * sent using fip_mac. For regular switches, both
1164 * fip_mac and fcoe_mac would be the same.
1168 ((struct fip_mac_desc *)desc)->fd_mac,
1171 if (dlen != sizeof(struct fip_mac_desc))
1174 if ((desc_cnt == 3) && (sel))
1175 memcpy(sel->fcoe_mac,
1176 ((struct fip_mac_desc *)desc)->fd_mac,
1183 if (desc_cnt != 1) {
1184 LIBFCOE_FIP_DBG(fip, "FIP descriptors "
1185 "received out of order\n");
1190 if (dlen < sizeof(*els) + sizeof(*fh) + 1)
1192 els_len = dlen - sizeof(*els);
1193 els = (struct fip_encaps *)desc;
1194 fh = (struct fc_frame_header *)(els + 1);
1195 els_dtype = desc->fip_dtype;
1198 LIBFCOE_FIP_DBG(fip, "unexpected descriptor type %x "
1199 "in FIP adv\n", desc->fip_dtype);
1200 /* standard says ignore unknown descriptors >= 128 */
1201 if (desc->fip_dtype < FIP_DT_VENDOR_BASE)
1203 if (desc_cnt <= 2) {
1204 LIBFCOE_FIP_DBG(fip, "FIP descriptors "
1205 "received out of order\n");
1210 desc = (struct fip_desc *)((char *)desc + dlen);
1216 els_op = *(u8 *)(fh + 1);
1218 if ((els_dtype == FIP_DT_FLOGI || els_dtype == FIP_DT_FDISC) &&
1219 sub == FIP_SC_REP && fip->mode != FIP_MODE_VN2VN) {
1220 if (els_op == ELS_LS_ACC) {
1221 if (!is_valid_ether_addr(granted_mac)) {
1222 LIBFCOE_FIP_DBG(fip,
1223 "Invalid MAC address %pM in FIP ELS\n",
1227 memcpy(fr_cb(fp)->granted_mac, granted_mac, ETH_ALEN);
1229 if (fip->flogi_oxid == ntohs(fh->fh_ox_id)) {
1230 fip->flogi_oxid = FC_XID_UNKNOWN;
1231 if (els_dtype == FIP_DT_FLOGI)
1232 fcoe_ctlr_announce(fip);
1234 } else if (els_dtype == FIP_DT_FLOGI &&
1235 !fcoe_ctlr_flogi_retry(fip))
1236 goto drop; /* retrying FLOGI so drop reject */
1239 if ((desc_cnt == 0) || ((els_op != ELS_LS_RJT) &&
1240 (!(1U << FIP_DT_MAC & desc_mask)))) {
1241 LIBFCOE_FIP_DBG(fip, "Missing critical descriptors "
1247 * Convert skb into an fc_frame containing only the ELS.
1249 skb_pull(skb, (u8 *)fh - skb->data);
1250 skb_trim(skb, els_len);
1251 fp = (struct fc_frame *)skb;
1253 fr_sof(fp) = FC_SOF_I3;
1254 fr_eof(fp) = FC_EOF_T;
1256 fr_encaps(fp) = els_dtype;
1258 stats = per_cpu_ptr(lport->stats, get_cpu());
1260 stats->RxWords += skb->len / FIP_BPW;
1263 fc_exch_recv(lport, fp);
1267 LIBFCOE_FIP_DBG(fip, "FIP length error in descriptor type %x len %zu\n",
1268 desc->fip_dtype, dlen);
1274 * fcoe_ctlr_recv_els() - Handle an incoming link reset frame
1275 * @fip: The FCoE controller that received the frame
1276 * @fh: The received FIP header
1278 * There may be multiple VN_Port descriptors.
1279 * The overall length has already been checked.
1281 static void fcoe_ctlr_recv_clr_vlink(struct fcoe_ctlr *fip,
1282 struct fip_header *fh)
1284 struct fip_desc *desc;
1285 struct fip_mac_desc *mp;
1286 struct fip_wwn_desc *wp;
1287 struct fip_vn_desc *vp;
1290 struct fcoe_fcf *fcf = fip->sel_fcf;
1291 struct fc_lport *lport = fip->lp;
1292 struct fc_lport *vn_port = NULL;
1295 int reset_phys_port = 0;
1296 struct fip_vn_desc **vlink_desc_arr = NULL;
1298 LIBFCOE_FIP_DBG(fip, "Clear Virtual Link received\n");
1300 if (!fcf || !lport->port_id) {
1302 * We are yet to select best FCF, but we got CVL in the
1303 * meantime. reset the ctlr and let it rediscover the FCF
1305 mutex_lock(&fip->ctlr_mutex);
1306 fcoe_ctlr_reset(fip);
1307 mutex_unlock(&fip->ctlr_mutex);
1312 * mask of required descriptors. Validating each one clears its bit.
1314 desc_mask = BIT(FIP_DT_MAC) | BIT(FIP_DT_NAME);
1316 rlen = ntohs(fh->fip_dl_len) * FIP_BPW;
1317 desc = (struct fip_desc *)(fh + 1);
1320 * Actually need to subtract 'sizeof(*mp) - sizeof(*wp)' from 'rlen'
1321 * before determining max Vx_Port descriptor but a buggy FCF could have
1322 * omited either or both MAC Address and Name Identifier descriptors
1324 num_vlink_desc = rlen / sizeof(*vp);
1326 vlink_desc_arr = kmalloc(sizeof(vp) * num_vlink_desc,
1328 if (!vlink_desc_arr)
1332 while (rlen >= sizeof(*desc)) {
1333 dlen = desc->fip_dlen * FIP_BPW;
1336 /* Drop CVL if there are duplicate critical descriptors */
1337 if ((desc->fip_dtype < 32) &&
1338 (desc->fip_dtype != FIP_DT_VN_ID) &&
1339 !(desc_mask & 1U << desc->fip_dtype)) {
1340 LIBFCOE_FIP_DBG(fip, "Duplicate Critical "
1341 "Descriptors in FIP CVL\n");
1344 switch (desc->fip_dtype) {
1346 mp = (struct fip_mac_desc *)desc;
1347 if (dlen < sizeof(*mp))
1349 if (compare_ether_addr(mp->fd_mac, fcf->fcf_mac))
1351 desc_mask &= ~BIT(FIP_DT_MAC);
1354 wp = (struct fip_wwn_desc *)desc;
1355 if (dlen < sizeof(*wp))
1357 if (get_unaligned_be64(&wp->fd_wwn) != fcf->switch_name)
1359 desc_mask &= ~BIT(FIP_DT_NAME);
1362 vp = (struct fip_vn_desc *)desc;
1363 if (dlen < sizeof(*vp))
1365 vlink_desc_arr[num_vlink_desc++] = vp;
1366 vn_port = fc_vport_id_lookup(lport,
1367 ntoh24(vp->fd_fc_id));
1368 if (vn_port && (vn_port == lport)) {
1369 mutex_lock(&fip->ctlr_mutex);
1370 per_cpu_ptr(lport->stats,
1371 get_cpu())->VLinkFailureCount++;
1373 fcoe_ctlr_reset(fip);
1374 mutex_unlock(&fip->ctlr_mutex);
1378 /* standard says ignore unknown descriptors >= 128 */
1379 if (desc->fip_dtype < FIP_DT_VENDOR_BASE)
1383 desc = (struct fip_desc *)((char *)desc + dlen);
1388 * reset only if all required descriptors were present and valid.
1391 LIBFCOE_FIP_DBG(fip, "missing descriptors mask %x\n",
1393 else if (!num_vlink_desc) {
1394 LIBFCOE_FIP_DBG(fip, "CVL: no Vx_Port descriptor found\n");
1396 * No Vx_Port description. Clear all NPIV ports,
1397 * followed by physical port
1399 mutex_lock(&fip->ctlr_mutex);
1400 per_cpu_ptr(lport->stats, get_cpu())->VLinkFailureCount++;
1402 fcoe_ctlr_reset(fip);
1403 mutex_unlock(&fip->ctlr_mutex);
1405 mutex_lock(&lport->lp_mutex);
1406 list_for_each_entry(vn_port, &lport->vports, list)
1407 fc_lport_reset(vn_port);
1408 mutex_unlock(&lport->lp_mutex);
1410 fc_lport_reset(fip->lp);
1411 fcoe_ctlr_solicit(fip, NULL);
1415 LIBFCOE_FIP_DBG(fip, "performing Clear Virtual Link\n");
1416 for (i = 0; i < num_vlink_desc; i++) {
1417 vp = vlink_desc_arr[i];
1418 vn_port = fc_vport_id_lookup(lport,
1419 ntoh24(vp->fd_fc_id));
1424 * 'port_id' is already validated, check MAC address and
1427 if (compare_ether_addr(fip->get_src_addr(vn_port),
1429 get_unaligned_be64(&vp->fd_wwpn) !=
1433 if (vn_port == lport)
1435 * Physical port, defer processing till all
1436 * listed NPIV ports are cleared
1438 reset_phys_port = 1;
1439 else /* NPIV port */
1440 fc_lport_reset(vn_port);
1443 if (reset_phys_port) {
1444 fc_lport_reset(fip->lp);
1445 fcoe_ctlr_solicit(fip, NULL);
1450 kfree(vlink_desc_arr);
1454 * fcoe_ctlr_recv() - Receive a FIP packet
1455 * @fip: The FCoE controller that received the packet
1456 * @skb: The received FIP packet
1458 * This may be called from either NET_RX_SOFTIRQ or IRQ.
1460 void fcoe_ctlr_recv(struct fcoe_ctlr *fip, struct sk_buff *skb)
1462 skb = skb_share_check(skb, GFP_ATOMIC);
1465 skb_queue_tail(&fip->fip_recv_list, skb);
1466 schedule_work(&fip->recv_work);
1468 EXPORT_SYMBOL(fcoe_ctlr_recv);
1471 * fcoe_ctlr_recv_handler() - Receive a FIP frame
1472 * @fip: The FCoE controller that received the frame
1473 * @skb: The received FIP frame
1475 * Returns non-zero if the frame is dropped.
1477 static int fcoe_ctlr_recv_handler(struct fcoe_ctlr *fip, struct sk_buff *skb)
1479 struct fip_header *fiph;
1481 enum fip_state state;
1485 if (skb_linearize(skb))
1487 if (skb->len < sizeof(*fiph))
1490 if (fip->mode == FIP_MODE_VN2VN) {
1491 if (compare_ether_addr(eh->h_dest, fip->ctl_src_addr) &&
1492 compare_ether_addr(eh->h_dest, fcoe_all_vn2vn) &&
1493 compare_ether_addr(eh->h_dest, fcoe_all_p2p))
1495 } else if (compare_ether_addr(eh->h_dest, fip->ctl_src_addr) &&
1496 compare_ether_addr(eh->h_dest, fcoe_all_enode))
1498 fiph = (struct fip_header *)skb->data;
1499 op = ntohs(fiph->fip_op);
1500 sub = fiph->fip_subcode;
1502 if (FIP_VER_DECAPS(fiph->fip_ver) != FIP_VER)
1504 if (ntohs(fiph->fip_dl_len) * FIP_BPW + sizeof(*fiph) > skb->len)
1507 mutex_lock(&fip->ctlr_mutex);
1509 if (state == FIP_ST_AUTO) {
1511 fcoe_ctlr_set_state(fip, FIP_ST_ENABLED);
1512 state = FIP_ST_ENABLED;
1513 LIBFCOE_FIP_DBG(fip, "Using FIP mode\n");
1515 mutex_unlock(&fip->ctlr_mutex);
1517 if (fip->mode == FIP_MODE_VN2VN && op == FIP_OP_VN2VN)
1518 return fcoe_ctlr_vn_recv(fip, skb);
1520 if (state != FIP_ST_ENABLED && state != FIP_ST_VNMP_UP &&
1521 state != FIP_ST_VNMP_CLAIM)
1524 if (op == FIP_OP_LS) {
1525 fcoe_ctlr_recv_els(fip, skb); /* consumes skb */
1529 if (state != FIP_ST_ENABLED)
1532 if (op == FIP_OP_DISC && sub == FIP_SC_ADV)
1533 fcoe_ctlr_recv_adv(fip, skb);
1534 else if (op == FIP_OP_CTRL && sub == FIP_SC_CLR_VLINK)
1535 fcoe_ctlr_recv_clr_vlink(fip, fiph);
1544 * fcoe_ctlr_select() - Select the best FCF (if possible)
1545 * @fip: The FCoE controller
1547 * Returns the selected FCF, or NULL if none are usable.
1549 * If there are conflicting advertisements, no FCF can be chosen.
1551 * If there is already a selected FCF, this will choose a better one or
1552 * an equivalent one that hasn't already been sent a FLOGI.
1554 * Called with lock held.
1556 static struct fcoe_fcf *fcoe_ctlr_select(struct fcoe_ctlr *fip)
1558 struct fcoe_fcf *fcf;
1559 struct fcoe_fcf *best = fip->sel_fcf;
1561 list_for_each_entry(fcf, &fip->fcfs, list) {
1562 LIBFCOE_FIP_DBG(fip, "consider FCF fab %16.16llx "
1563 "VFID %d mac %pM map %x val %d "
1565 fcf->fabric_name, fcf->vfid, fcf->fcf_mac,
1566 fcf->fc_map, fcoe_ctlr_mtu_valid(fcf),
1567 fcf->flogi_sent, fcf->pri);
1568 if (!fcoe_ctlr_fcf_usable(fcf)) {
1569 LIBFCOE_FIP_DBG(fip, "FCF for fab %16.16llx "
1570 "map %x %svalid %savailable\n",
1571 fcf->fabric_name, fcf->fc_map,
1572 (fcf->flags & FIP_FL_SOL) ? "" : "in",
1573 (fcf->flags & FIP_FL_AVAIL) ?
1577 if (!best || fcf->pri < best->pri || best->flogi_sent)
1579 if (fcf->fabric_name != best->fabric_name ||
1580 fcf->vfid != best->vfid ||
1581 fcf->fc_map != best->fc_map) {
1582 LIBFCOE_FIP_DBG(fip, "Conflicting fabric, VFID, "
1587 fip->sel_fcf = best;
1589 LIBFCOE_FIP_DBG(fip, "using FCF mac %pM\n", best->fcf_mac);
1590 fip->port_ka_time = jiffies +
1591 msecs_to_jiffies(FIP_VN_KA_PERIOD);
1592 fip->ctlr_ka_time = jiffies + best->fka_period;
1593 if (time_before(fip->ctlr_ka_time, fip->timer.expires))
1594 mod_timer(&fip->timer, fip->ctlr_ka_time);
1600 * fcoe_ctlr_flogi_send_locked() - send FIP-encapsulated FLOGI to current FCF
1601 * @fip: The FCoE controller
1603 * Returns non-zero error if it could not be sent.
1605 * Called with ctlr_mutex and ctlr_lock held.
1606 * Caller must verify that fip->sel_fcf is not NULL.
1608 static int fcoe_ctlr_flogi_send_locked(struct fcoe_ctlr *fip)
1610 struct sk_buff *skb;
1611 struct sk_buff *skb_orig;
1612 struct fc_frame_header *fh;
1615 skb_orig = fip->flogi_req;
1620 * Clone and send the FLOGI request. If clone fails, use original.
1622 skb = skb_clone(skb_orig, GFP_ATOMIC);
1625 fip->flogi_req = NULL;
1627 fh = (struct fc_frame_header *)skb->data;
1628 error = fcoe_ctlr_encaps(fip, fip->lp, FIP_DT_FLOGI, skb,
1629 ntoh24(fh->fh_d_id));
1634 fip->send(fip, skb);
1635 fip->sel_fcf->flogi_sent = 1;
1640 * fcoe_ctlr_flogi_retry() - resend FLOGI request to a new FCF if possible
1641 * @fip: The FCoE controller
1643 * Returns non-zero error code if there's no FLOGI request to retry or
1644 * no alternate FCF available.
1646 static int fcoe_ctlr_flogi_retry(struct fcoe_ctlr *fip)
1648 struct fcoe_fcf *fcf;
1651 mutex_lock(&fip->ctlr_mutex);
1652 spin_lock_bh(&fip->ctlr_lock);
1653 LIBFCOE_FIP_DBG(fip, "re-sending FLOGI - reselect\n");
1654 fcf = fcoe_ctlr_select(fip);
1655 if (!fcf || fcf->flogi_sent) {
1656 kfree_skb(fip->flogi_req);
1657 fip->flogi_req = NULL;
1660 fcoe_ctlr_solicit(fip, NULL);
1661 error = fcoe_ctlr_flogi_send_locked(fip);
1663 spin_unlock_bh(&fip->ctlr_lock);
1664 mutex_unlock(&fip->ctlr_mutex);
1670 * fcoe_ctlr_flogi_send() - Handle sending of FIP FLOGI.
1671 * @fip: The FCoE controller that timed out
1673 * Done here because fcoe_ctlr_els_send() can't get mutex.
1675 * Called with ctlr_mutex held. The caller must not hold ctlr_lock.
1677 static void fcoe_ctlr_flogi_send(struct fcoe_ctlr *fip)
1679 struct fcoe_fcf *fcf;
1681 spin_lock_bh(&fip->ctlr_lock);
1683 if (!fcf || !fip->flogi_req_send)
1686 LIBFCOE_FIP_DBG(fip, "sending FLOGI\n");
1689 * If this FLOGI is being sent due to a timeout retry
1690 * to the same FCF as before, select a different FCF if possible.
1692 if (fcf->flogi_sent) {
1693 LIBFCOE_FIP_DBG(fip, "sending FLOGI - reselect\n");
1694 fcf = fcoe_ctlr_select(fip);
1695 if (!fcf || fcf->flogi_sent) {
1696 LIBFCOE_FIP_DBG(fip, "sending FLOGI - clearing\n");
1697 list_for_each_entry(fcf, &fip->fcfs, list)
1698 fcf->flogi_sent = 0;
1699 fcf = fcoe_ctlr_select(fip);
1703 fcoe_ctlr_flogi_send_locked(fip);
1704 fip->flogi_req_send = 0;
1706 LIBFCOE_FIP_DBG(fip, "No FCF selected - defer send\n");
1708 spin_unlock_bh(&fip->ctlr_lock);
1712 * fcoe_ctlr_timeout() - FIP timeout handler
1713 * @arg: The FCoE controller that timed out
1715 static void fcoe_ctlr_timeout(unsigned long arg)
1717 struct fcoe_ctlr *fip = (struct fcoe_ctlr *)arg;
1719 schedule_work(&fip->timer_work);
1723 * fcoe_ctlr_timer_work() - Worker thread function for timer work
1724 * @work: Handle to a FCoE controller
1726 * Ages FCFs. Triggers FCF selection if possible.
1727 * Sends keep-alives and resets.
1729 static void fcoe_ctlr_timer_work(struct work_struct *work)
1731 struct fcoe_ctlr *fip;
1732 struct fc_lport *vport;
1735 u8 send_ctlr_ka = 0;
1736 u8 send_port_ka = 0;
1737 struct fcoe_fcf *sel;
1738 struct fcoe_fcf *fcf;
1739 unsigned long next_timer;
1741 fip = container_of(work, struct fcoe_ctlr, timer_work);
1742 if (fip->mode == FIP_MODE_VN2VN)
1743 return fcoe_ctlr_vn_timeout(fip);
1744 mutex_lock(&fip->ctlr_mutex);
1745 if (fip->state == FIP_ST_DISABLED) {
1746 mutex_unlock(&fip->ctlr_mutex);
1751 next_timer = fcoe_ctlr_age_fcfs(fip);
1754 if (!sel && fip->sel_time) {
1755 if (time_after_eq(jiffies, fip->sel_time)) {
1756 sel = fcoe_ctlr_select(fip);
1758 } else if (time_after(next_timer, fip->sel_time))
1759 next_timer = fip->sel_time;
1762 if (sel && fip->flogi_req_send)
1763 fcoe_ctlr_flogi_send(fip);
1764 else if (!sel && fcf)
1767 if (sel && !sel->fd_flags) {
1768 if (time_after_eq(jiffies, fip->ctlr_ka_time)) {
1769 fip->ctlr_ka_time = jiffies + sel->fka_period;
1772 if (time_after(next_timer, fip->ctlr_ka_time))
1773 next_timer = fip->ctlr_ka_time;
1775 if (time_after_eq(jiffies, fip->port_ka_time)) {
1776 fip->port_ka_time = jiffies +
1777 msecs_to_jiffies(FIP_VN_KA_PERIOD);
1780 if (time_after(next_timer, fip->port_ka_time))
1781 next_timer = fip->port_ka_time;
1783 if (!list_empty(&fip->fcfs))
1784 mod_timer(&fip->timer, next_timer);
1785 mutex_unlock(&fip->ctlr_mutex);
1788 fc_lport_reset(fip->lp);
1789 /* restart things with a solicitation */
1790 fcoe_ctlr_solicit(fip, NULL);
1794 fcoe_ctlr_send_keep_alive(fip, NULL, 0, fip->ctl_src_addr);
1797 mutex_lock(&fip->lp->lp_mutex);
1798 mac = fip->get_src_addr(fip->lp);
1799 fcoe_ctlr_send_keep_alive(fip, fip->lp, 1, mac);
1800 list_for_each_entry(vport, &fip->lp->vports, list) {
1801 mac = fip->get_src_addr(vport);
1802 fcoe_ctlr_send_keep_alive(fip, vport, 1, mac);
1804 mutex_unlock(&fip->lp->lp_mutex);
1809 * fcoe_ctlr_recv_work() - Worker thread function for receiving FIP frames
1810 * @recv_work: Handle to a FCoE controller
1812 static void fcoe_ctlr_recv_work(struct work_struct *recv_work)
1814 struct fcoe_ctlr *fip;
1815 struct sk_buff *skb;
1817 fip = container_of(recv_work, struct fcoe_ctlr, recv_work);
1818 while ((skb = skb_dequeue(&fip->fip_recv_list)))
1819 fcoe_ctlr_recv_handler(fip, skb);
1823 * fcoe_ctlr_recv_flogi() - Snoop pre-FIP receipt of FLOGI response
1824 * @fip: The FCoE controller
1825 * @fp: The FC frame to snoop
1827 * Snoop potential response to FLOGI or even incoming FLOGI.
1829 * The caller has checked that we are waiting for login as indicated
1830 * by fip->flogi_oxid != FC_XID_UNKNOWN.
1832 * The caller is responsible for freeing the frame.
1833 * Fill in the granted_mac address.
1835 * Return non-zero if the frame should not be delivered to libfc.
1837 int fcoe_ctlr_recv_flogi(struct fcoe_ctlr *fip, struct fc_lport *lport,
1838 struct fc_frame *fp)
1840 struct fc_frame_header *fh;
1844 sa = eth_hdr(&fp->skb)->h_source;
1845 fh = fc_frame_header_get(fp);
1846 if (fh->fh_type != FC_TYPE_ELS)
1849 op = fc_frame_payload_op(fp);
1850 if (op == ELS_LS_ACC && fh->fh_r_ctl == FC_RCTL_ELS_REP &&
1851 fip->flogi_oxid == ntohs(fh->fh_ox_id)) {
1853 mutex_lock(&fip->ctlr_mutex);
1854 if (fip->state != FIP_ST_AUTO && fip->state != FIP_ST_NON_FIP) {
1855 mutex_unlock(&fip->ctlr_mutex);
1858 fcoe_ctlr_set_state(fip, FIP_ST_NON_FIP);
1859 LIBFCOE_FIP_DBG(fip,
1860 "received FLOGI LS_ACC using non-FIP mode\n");
1864 * If the src mac addr is FC_OUI-based, then we mark the
1865 * address_mode flag to use FC_OUI-based Ethernet DA.
1866 * Otherwise we use the FCoE gateway addr
1868 if (!compare_ether_addr(sa, (u8[6])FC_FCOE_FLOGI_MAC)) {
1869 fcoe_ctlr_map_dest(fip);
1871 memcpy(fip->dest_addr, sa, ETH_ALEN);
1874 fip->flogi_oxid = FC_XID_UNKNOWN;
1875 mutex_unlock(&fip->ctlr_mutex);
1876 fc_fcoe_set_mac(fr_cb(fp)->granted_mac, fh->fh_d_id);
1877 } else if (op == ELS_FLOGI && fh->fh_r_ctl == FC_RCTL_ELS_REQ && sa) {
1879 * Save source MAC for point-to-point responses.
1881 mutex_lock(&fip->ctlr_mutex);
1882 if (fip->state == FIP_ST_AUTO || fip->state == FIP_ST_NON_FIP) {
1883 memcpy(fip->dest_addr, sa, ETH_ALEN);
1885 if (fip->state == FIP_ST_AUTO)
1886 LIBFCOE_FIP_DBG(fip, "received non-FIP FLOGI. "
1887 "Setting non-FIP mode\n");
1888 fcoe_ctlr_set_state(fip, FIP_ST_NON_FIP);
1890 mutex_unlock(&fip->ctlr_mutex);
1894 EXPORT_SYMBOL(fcoe_ctlr_recv_flogi);
1897 * fcoe_wwn_from_mac() - Converts a 48-bit IEEE MAC address to a 64-bit FC WWN
1898 * @mac: The MAC address to convert
1899 * @scheme: The scheme to use when converting
1900 * @port: The port indicator for converting
1902 * Returns: u64 fc world wide name
1904 u64 fcoe_wwn_from_mac(unsigned char mac[MAX_ADDR_LEN],
1905 unsigned int scheme, unsigned int port)
1910 /* The MAC is in NO, so flip only the low 48 bits */
1911 host_mac = ((u64) mac[0] << 40) |
1912 ((u64) mac[1] << 32) |
1913 ((u64) mac[2] << 24) |
1914 ((u64) mac[3] << 16) |
1915 ((u64) mac[4] << 8) |
1918 WARN_ON(host_mac >= (1ULL << 48));
1919 wwn = host_mac | ((u64) scheme << 60);
1925 WARN_ON(port >= 0xfff);
1926 wwn |= (u64) port << 48;
1935 EXPORT_SYMBOL_GPL(fcoe_wwn_from_mac);
1938 * fcoe_ctlr_rport() - return the fcoe_rport for a given fc_rport_priv
1939 * @rdata: libfc remote port
1941 static inline struct fcoe_rport *fcoe_ctlr_rport(struct fc_rport_priv *rdata)
1943 return (struct fcoe_rport *)(rdata + 1);
1947 * fcoe_ctlr_vn_send() - Send a FIP VN2VN Probe Request or Reply.
1948 * @fip: The FCoE controller
1949 * @sub: sub-opcode for probe request, reply, or advertisement.
1950 * @dest: The destination Ethernet MAC address
1951 * @min_len: minimum size of the Ethernet payload to be sent
1953 static void fcoe_ctlr_vn_send(struct fcoe_ctlr *fip,
1954 enum fip_vn2vn_subcode sub,
1955 const u8 *dest, size_t min_len)
1957 struct sk_buff *skb;
1960 struct fip_header fip;
1961 struct fip_mac_desc mac;
1962 struct fip_wwn_desc wwnn;
1963 struct fip_vn_desc vn;
1965 struct fip_fc4_feat *ff;
1966 struct fip_size_desc *size;
1971 len = sizeof(*frame);
1973 if (sub == FIP_SC_VN_CLAIM_NOTIFY || sub == FIP_SC_VN_CLAIM_REP) {
1974 dlen = sizeof(struct fip_fc4_feat) +
1975 sizeof(struct fip_size_desc);
1978 dlen += sizeof(frame->mac) + sizeof(frame->wwnn) + sizeof(frame->vn);
1979 len = max(len, min_len + sizeof(struct ethhdr));
1981 skb = dev_alloc_skb(len);
1985 frame = (struct fip_frame *)skb->data;
1986 memset(frame, 0, len);
1987 memcpy(frame->eth.h_dest, dest, ETH_ALEN);
1989 if (sub == FIP_SC_VN_BEACON) {
1990 hton24(frame->eth.h_source, FIP_VN_FC_MAP);
1991 hton24(frame->eth.h_source + 3, fip->port_id);
1993 memcpy(frame->eth.h_source, fip->ctl_src_addr, ETH_ALEN);
1995 frame->eth.h_proto = htons(ETH_P_FIP);
1997 frame->fip.fip_ver = FIP_VER_ENCAPS(FIP_VER);
1998 frame->fip.fip_op = htons(FIP_OP_VN2VN);
1999 frame->fip.fip_subcode = sub;
2000 frame->fip.fip_dl_len = htons(dlen / FIP_BPW);
2002 frame->mac.fd_desc.fip_dtype = FIP_DT_MAC;
2003 frame->mac.fd_desc.fip_dlen = sizeof(frame->mac) / FIP_BPW;
2004 memcpy(frame->mac.fd_mac, fip->ctl_src_addr, ETH_ALEN);
2006 frame->wwnn.fd_desc.fip_dtype = FIP_DT_NAME;
2007 frame->wwnn.fd_desc.fip_dlen = sizeof(frame->wwnn) / FIP_BPW;
2008 put_unaligned_be64(fip->lp->wwnn, &frame->wwnn.fd_wwn);
2010 frame->vn.fd_desc.fip_dtype = FIP_DT_VN_ID;
2011 frame->vn.fd_desc.fip_dlen = sizeof(frame->vn) / FIP_BPW;
2012 hton24(frame->vn.fd_mac, FIP_VN_FC_MAP);
2013 hton24(frame->vn.fd_mac + 3, fip->port_id);
2014 hton24(frame->vn.fd_fc_id, fip->port_id);
2015 put_unaligned_be64(fip->lp->wwpn, &frame->vn.fd_wwpn);
2018 * For claims, add FC-4 features.
2019 * TBD: Add interface to get fc-4 types and features from libfc.
2021 if (sub == FIP_SC_VN_CLAIM_NOTIFY || sub == FIP_SC_VN_CLAIM_REP) {
2022 ff = (struct fip_fc4_feat *)(frame + 1);
2023 ff->fd_desc.fip_dtype = FIP_DT_FC4F;
2024 ff->fd_desc.fip_dlen = sizeof(*ff) / FIP_BPW;
2025 ff->fd_fts = fip->lp->fcts;
2028 if (fip->lp->service_params & FCP_SPPF_INIT_FCN)
2029 fcp_feat |= FCP_FEAT_INIT;
2030 if (fip->lp->service_params & FCP_SPPF_TARG_FCN)
2031 fcp_feat |= FCP_FEAT_TARG;
2032 fcp_feat <<= (FC_TYPE_FCP * 4) % 32;
2033 ff->fd_ff.fd_feat[FC_TYPE_FCP * 4 / 32] = htonl(fcp_feat);
2035 size = (struct fip_size_desc *)(ff + 1);
2036 size->fd_desc.fip_dtype = FIP_DT_FCOE_SIZE;
2037 size->fd_desc.fip_dlen = sizeof(*size) / FIP_BPW;
2038 size->fd_size = htons(fcoe_ctlr_fcoe_size(fip));
2042 skb->protocol = htons(ETH_P_FIP);
2043 skb->priority = fip->priority;
2044 skb_reset_mac_header(skb);
2045 skb_reset_network_header(skb);
2047 fip->send(fip, skb);
2051 * fcoe_ctlr_vn_rport_callback - Event handler for rport events.
2052 * @lport: The lport which is receiving the event
2053 * @rdata: remote port private data
2054 * @event: The event that occurred
2056 * Locking Note: The rport lock must not be held when calling this function.
2058 static void fcoe_ctlr_vn_rport_callback(struct fc_lport *lport,
2059 struct fc_rport_priv *rdata,
2060 enum fc_rport_event event)
2062 struct fcoe_ctlr *fip = lport->disc.priv;
2063 struct fcoe_rport *frport = fcoe_ctlr_rport(rdata);
2065 LIBFCOE_FIP_DBG(fip, "vn_rport_callback %x event %d\n",
2066 rdata->ids.port_id, event);
2068 mutex_lock(&fip->ctlr_mutex);
2070 case RPORT_EV_READY:
2071 frport->login_count = 0;
2074 case RPORT_EV_FAILED:
2076 frport->login_count++;
2077 if (frport->login_count > FCOE_CTLR_VN2VN_LOGIN_LIMIT) {
2078 LIBFCOE_FIP_DBG(fip,
2079 "rport FLOGI limited port_id %6.6x\n",
2080 rdata->ids.port_id);
2081 lport->tt.rport_logoff(rdata);
2087 mutex_unlock(&fip->ctlr_mutex);
2090 static struct fc_rport_operations fcoe_ctlr_vn_rport_ops = {
2091 .event_callback = fcoe_ctlr_vn_rport_callback,
2095 * fcoe_ctlr_disc_stop_locked() - stop discovery in VN2VN mode
2096 * @fip: The FCoE controller
2098 * Called with ctlr_mutex held.
2100 static void fcoe_ctlr_disc_stop_locked(struct fc_lport *lport)
2102 struct fc_rport_priv *rdata;
2104 mutex_lock(&lport->disc.disc_mutex);
2105 list_for_each_entry_rcu(rdata, &lport->disc.rports, peers)
2106 lport->tt.rport_logoff(rdata);
2107 lport->disc.disc_callback = NULL;
2108 mutex_unlock(&lport->disc.disc_mutex);
2112 * fcoe_ctlr_disc_stop() - stop discovery in VN2VN mode
2113 * @fip: The FCoE controller
2115 * Called through the local port template for discovery.
2116 * Called without the ctlr_mutex held.
2118 static void fcoe_ctlr_disc_stop(struct fc_lport *lport)
2120 struct fcoe_ctlr *fip = lport->disc.priv;
2122 mutex_lock(&fip->ctlr_mutex);
2123 fcoe_ctlr_disc_stop_locked(lport);
2124 mutex_unlock(&fip->ctlr_mutex);
2128 * fcoe_ctlr_disc_stop_final() - stop discovery for shutdown in VN2VN mode
2129 * @fip: The FCoE controller
2131 * Called through the local port template for discovery.
2132 * Called without the ctlr_mutex held.
2134 static void fcoe_ctlr_disc_stop_final(struct fc_lport *lport)
2136 fcoe_ctlr_disc_stop(lport);
2137 lport->tt.rport_flush_queue();
2142 * fcoe_ctlr_vn_restart() - VN2VN probe restart with new port_id
2143 * @fip: The FCoE controller
2145 * Called with fcoe_ctlr lock held.
2147 static void fcoe_ctlr_vn_restart(struct fcoe_ctlr *fip)
2152 fcoe_ctlr_disc_stop_locked(fip->lp);
2155 * Get proposed port ID.
2156 * If this is the first try after link up, use any previous port_id.
2157 * If there was none, use the low bits of the port_name.
2158 * On subsequent tries, get the next random one.
2159 * Don't use reserved IDs, use another non-zero value, just as random.
2161 port_id = fip->port_id;
2162 if (fip->probe_tries)
2163 port_id = prandom_u32_state(&fip->rnd_state) & 0xffff;
2165 port_id = fip->lp->wwpn & 0xffff;
2166 if (!port_id || port_id == 0xffff)
2168 fip->port_id = port_id;
2170 if (fip->probe_tries < FIP_VN_RLIM_COUNT) {
2172 wait = prandom_u32() % FIP_VN_PROBE_WAIT;
2174 wait = FIP_VN_RLIM_INT;
2175 mod_timer(&fip->timer, jiffies + msecs_to_jiffies(wait));
2176 fcoe_ctlr_set_state(fip, FIP_ST_VNMP_START);
2180 * fcoe_ctlr_vn_start() - Start in VN2VN mode
2181 * @fip: The FCoE controller
2183 * Called with fcoe_ctlr lock held.
2185 static void fcoe_ctlr_vn_start(struct fcoe_ctlr *fip)
2187 fip->probe_tries = 0;
2188 prandom_seed_state(&fip->rnd_state, fip->lp->wwpn);
2189 fcoe_ctlr_vn_restart(fip);
2193 * fcoe_ctlr_vn_parse - parse probe request or response
2194 * @fip: The FCoE controller
2195 * @skb: incoming packet
2196 * @rdata: buffer for resulting parsed VN entry plus fcoe_rport
2198 * Returns non-zero error number on error.
2199 * Does not consume the packet.
2201 static int fcoe_ctlr_vn_parse(struct fcoe_ctlr *fip,
2202 struct sk_buff *skb,
2203 struct fc_rport_priv *rdata)
2205 struct fip_header *fiph;
2206 struct fip_desc *desc = NULL;
2207 struct fip_mac_desc *macd = NULL;
2208 struct fip_wwn_desc *wwn = NULL;
2209 struct fip_vn_desc *vn = NULL;
2210 struct fip_size_desc *size = NULL;
2211 struct fcoe_rport *frport;
2218 memset(rdata, 0, sizeof(*rdata) + sizeof(*frport));
2219 frport = fcoe_ctlr_rport(rdata);
2221 fiph = (struct fip_header *)skb->data;
2222 frport->flags = ntohs(fiph->fip_flags);
2224 sub = fiph->fip_subcode;
2226 case FIP_SC_VN_PROBE_REQ:
2227 case FIP_SC_VN_PROBE_REP:
2228 case FIP_SC_VN_BEACON:
2229 desc_mask = BIT(FIP_DT_MAC) | BIT(FIP_DT_NAME) |
2232 case FIP_SC_VN_CLAIM_NOTIFY:
2233 case FIP_SC_VN_CLAIM_REP:
2234 desc_mask = BIT(FIP_DT_MAC) | BIT(FIP_DT_NAME) |
2235 BIT(FIP_DT_VN_ID) | BIT(FIP_DT_FC4F) |
2236 BIT(FIP_DT_FCOE_SIZE);
2239 LIBFCOE_FIP_DBG(fip, "vn_parse unknown subcode %u\n", sub);
2243 rlen = ntohs(fiph->fip_dl_len) * 4;
2244 if (rlen + sizeof(*fiph) > skb->len)
2247 desc = (struct fip_desc *)(fiph + 1);
2249 dlen = desc->fip_dlen * FIP_BPW;
2250 if (dlen < sizeof(*desc) || dlen > rlen)
2253 dtype = desc->fip_dtype;
2255 if (!(desc_mask & BIT(dtype))) {
2256 LIBFCOE_FIP_DBG(fip,
2257 "unexpected or duplicated desc "
2259 "FIP VN2VN subtype %u\n",
2263 desc_mask &= ~BIT(dtype);
2268 if (dlen != sizeof(struct fip_mac_desc))
2270 macd = (struct fip_mac_desc *)desc;
2271 if (!is_valid_ether_addr(macd->fd_mac)) {
2272 LIBFCOE_FIP_DBG(fip,
2273 "Invalid MAC addr %pM in FIP VN2VN\n",
2277 memcpy(frport->enode_mac, macd->fd_mac, ETH_ALEN);
2280 if (dlen != sizeof(struct fip_wwn_desc))
2282 wwn = (struct fip_wwn_desc *)desc;
2283 rdata->ids.node_name = get_unaligned_be64(&wwn->fd_wwn);
2286 if (dlen != sizeof(struct fip_vn_desc))
2288 vn = (struct fip_vn_desc *)desc;
2289 memcpy(frport->vn_mac, vn->fd_mac, ETH_ALEN);
2290 rdata->ids.port_id = ntoh24(vn->fd_fc_id);
2291 rdata->ids.port_name = get_unaligned_be64(&vn->fd_wwpn);
2294 if (dlen != sizeof(struct fip_fc4_feat))
2297 case FIP_DT_FCOE_SIZE:
2298 if (dlen != sizeof(struct fip_size_desc))
2300 size = (struct fip_size_desc *)desc;
2301 frport->fcoe_len = ntohs(size->fd_size);
2304 LIBFCOE_FIP_DBG(fip, "unexpected descriptor type %x "
2305 "in FIP probe\n", dtype);
2306 /* standard says ignore unknown descriptors >= 128 */
2307 if (dtype < FIP_DT_VENDOR_BASE)
2311 desc = (struct fip_desc *)((char *)desc + dlen);
2317 LIBFCOE_FIP_DBG(fip, "FIP length error in descriptor type %x len %zu\n",
2323 * fcoe_ctlr_vn_send_claim() - send multicast FIP VN2VN Claim Notification.
2324 * @fip: The FCoE controller
2326 * Called with ctlr_mutex held.
2328 static void fcoe_ctlr_vn_send_claim(struct fcoe_ctlr *fip)
2330 fcoe_ctlr_vn_send(fip, FIP_SC_VN_CLAIM_NOTIFY, fcoe_all_vn2vn, 0);
2331 fip->sol_time = jiffies;
2335 * fcoe_ctlr_vn_probe_req() - handle incoming VN2VN probe request.
2336 * @fip: The FCoE controller
2337 * @rdata: parsed remote port with frport from the probe request
2339 * Called with ctlr_mutex held.
2341 static void fcoe_ctlr_vn_probe_req(struct fcoe_ctlr *fip,
2342 struct fc_rport_priv *rdata)
2344 struct fcoe_rport *frport = fcoe_ctlr_rport(rdata);
2346 if (rdata->ids.port_id != fip->port_id)
2349 switch (fip->state) {
2350 case FIP_ST_VNMP_CLAIM:
2351 case FIP_ST_VNMP_UP:
2352 fcoe_ctlr_vn_send(fip, FIP_SC_VN_PROBE_REP,
2353 frport->enode_mac, 0);
2355 case FIP_ST_VNMP_PROBE1:
2356 case FIP_ST_VNMP_PROBE2:
2358 * Decide whether to reply to the Probe.
2359 * Our selected address is never a "recorded" one, so
2360 * only reply if our WWPN is greater and the
2361 * Probe's REC bit is not set.
2362 * If we don't reply, we will change our address.
2364 if (fip->lp->wwpn > rdata->ids.port_name &&
2365 !(frport->flags & FIP_FL_REC_OR_P2P)) {
2366 fcoe_ctlr_vn_send(fip, FIP_SC_VN_PROBE_REP,
2367 frport->enode_mac, 0);
2371 case FIP_ST_VNMP_START:
2372 fcoe_ctlr_vn_restart(fip);
2380 * fcoe_ctlr_vn_probe_reply() - handle incoming VN2VN probe reply.
2381 * @fip: The FCoE controller
2382 * @rdata: parsed remote port with frport from the probe request
2384 * Called with ctlr_mutex held.
2386 static void fcoe_ctlr_vn_probe_reply(struct fcoe_ctlr *fip,
2387 struct fc_rport_priv *rdata)
2389 if (rdata->ids.port_id != fip->port_id)
2391 switch (fip->state) {
2392 case FIP_ST_VNMP_START:
2393 case FIP_ST_VNMP_PROBE1:
2394 case FIP_ST_VNMP_PROBE2:
2395 case FIP_ST_VNMP_CLAIM:
2396 fcoe_ctlr_vn_restart(fip);
2398 case FIP_ST_VNMP_UP:
2399 fcoe_ctlr_vn_send_claim(fip);
2407 * fcoe_ctlr_vn_add() - Add a VN2VN entry to the list, based on a claim reply.
2408 * @fip: The FCoE controller
2409 * @new: newly-parsed remote port with frport as a template for new rdata
2411 * Called with ctlr_mutex held.
2413 static void fcoe_ctlr_vn_add(struct fcoe_ctlr *fip, struct fc_rport_priv *new)
2415 struct fc_lport *lport = fip->lp;
2416 struct fc_rport_priv *rdata;
2417 struct fc_rport_identifiers *ids;
2418 struct fcoe_rport *frport;
2421 port_id = new->ids.port_id;
2422 if (port_id == fip->port_id)
2425 mutex_lock(&lport->disc.disc_mutex);
2426 rdata = lport->tt.rport_create(lport, port_id);
2428 mutex_unlock(&lport->disc.disc_mutex);
2432 rdata->ops = &fcoe_ctlr_vn_rport_ops;
2433 rdata->disc_id = lport->disc.disc_id;
2436 if ((ids->port_name != -1 && ids->port_name != new->ids.port_name) ||
2437 (ids->node_name != -1 && ids->node_name != new->ids.node_name))
2438 lport->tt.rport_logoff(rdata);
2439 ids->port_name = new->ids.port_name;
2440 ids->node_name = new->ids.node_name;
2441 mutex_unlock(&lport->disc.disc_mutex);
2443 frport = fcoe_ctlr_rport(rdata);
2444 LIBFCOE_FIP_DBG(fip, "vn_add rport %6.6x %s\n",
2445 port_id, frport->fcoe_len ? "old" : "new");
2446 *frport = *fcoe_ctlr_rport(new);
2451 * fcoe_ctlr_vn_lookup() - Find VN remote port's MAC address
2452 * @fip: The FCoE controller
2453 * @port_id: The port_id of the remote VN_node
2454 * @mac: buffer which will hold the VN_NODE destination MAC address, if found.
2456 * Returns non-zero error if no remote port found.
2458 static int fcoe_ctlr_vn_lookup(struct fcoe_ctlr *fip, u32 port_id, u8 *mac)
2460 struct fc_lport *lport = fip->lp;
2461 struct fc_rport_priv *rdata;
2462 struct fcoe_rport *frport;
2466 rdata = lport->tt.rport_lookup(lport, port_id);
2468 frport = fcoe_ctlr_rport(rdata);
2469 memcpy(mac, frport->enode_mac, ETH_ALEN);
2477 * fcoe_ctlr_vn_claim_notify() - handle received FIP VN2VN Claim Notification
2478 * @fip: The FCoE controller
2479 * @new: newly-parsed remote port with frport as a template for new rdata
2481 * Called with ctlr_mutex held.
2483 static void fcoe_ctlr_vn_claim_notify(struct fcoe_ctlr *fip,
2484 struct fc_rport_priv *new)
2486 struct fcoe_rport *frport = fcoe_ctlr_rport(new);
2488 if (frport->flags & FIP_FL_REC_OR_P2P) {
2489 fcoe_ctlr_vn_send(fip, FIP_SC_VN_PROBE_REQ, fcoe_all_vn2vn, 0);
2492 switch (fip->state) {
2493 case FIP_ST_VNMP_START:
2494 case FIP_ST_VNMP_PROBE1:
2495 case FIP_ST_VNMP_PROBE2:
2496 if (new->ids.port_id == fip->port_id)
2497 fcoe_ctlr_vn_restart(fip);
2499 case FIP_ST_VNMP_CLAIM:
2500 case FIP_ST_VNMP_UP:
2501 if (new->ids.port_id == fip->port_id) {
2502 if (new->ids.port_name > fip->lp->wwpn) {
2503 fcoe_ctlr_vn_restart(fip);
2506 fcoe_ctlr_vn_send_claim(fip);
2509 fcoe_ctlr_vn_send(fip, FIP_SC_VN_CLAIM_REP, frport->enode_mac,
2510 min((u32)frport->fcoe_len,
2511 fcoe_ctlr_fcoe_size(fip)));
2512 fcoe_ctlr_vn_add(fip, new);
2520 * fcoe_ctlr_vn_claim_resp() - handle received Claim Response
2521 * @fip: The FCoE controller that received the frame
2522 * @new: newly-parsed remote port with frport from the Claim Response
2524 * Called with ctlr_mutex held.
2526 static void fcoe_ctlr_vn_claim_resp(struct fcoe_ctlr *fip,
2527 struct fc_rport_priv *new)
2529 LIBFCOE_FIP_DBG(fip, "claim resp from from rport %x - state %s\n",
2530 new->ids.port_id, fcoe_ctlr_state(fip->state));
2531 if (fip->state == FIP_ST_VNMP_UP || fip->state == FIP_ST_VNMP_CLAIM)
2532 fcoe_ctlr_vn_add(fip, new);
2536 * fcoe_ctlr_vn_beacon() - handle received beacon.
2537 * @fip: The FCoE controller that received the frame
2538 * @new: newly-parsed remote port with frport from the Beacon
2540 * Called with ctlr_mutex held.
2542 static void fcoe_ctlr_vn_beacon(struct fcoe_ctlr *fip,
2543 struct fc_rport_priv *new)
2545 struct fc_lport *lport = fip->lp;
2546 struct fc_rport_priv *rdata;
2547 struct fcoe_rport *frport;
2549 frport = fcoe_ctlr_rport(new);
2550 if (frport->flags & FIP_FL_REC_OR_P2P) {
2551 fcoe_ctlr_vn_send(fip, FIP_SC_VN_PROBE_REQ, fcoe_all_vn2vn, 0);
2554 mutex_lock(&lport->disc.disc_mutex);
2555 rdata = lport->tt.rport_lookup(lport, new->ids.port_id);
2557 kref_get(&rdata->kref);
2558 mutex_unlock(&lport->disc.disc_mutex);
2560 if (rdata->ids.node_name == new->ids.node_name &&
2561 rdata->ids.port_name == new->ids.port_name) {
2562 frport = fcoe_ctlr_rport(rdata);
2563 if (!frport->time && fip->state == FIP_ST_VNMP_UP)
2564 lport->tt.rport_login(rdata);
2565 frport->time = jiffies;
2567 kref_put(&rdata->kref, lport->tt.rport_destroy);
2570 if (fip->state != FIP_ST_VNMP_UP)
2574 * Beacon from a new neighbor.
2575 * Send a claim notify if one hasn't been sent recently.
2576 * Don't add the neighbor yet.
2578 LIBFCOE_FIP_DBG(fip, "beacon from new rport %x. sending claim notify\n",
2580 if (time_after(jiffies,
2581 fip->sol_time + msecs_to_jiffies(FIP_VN_ANN_WAIT)))
2582 fcoe_ctlr_vn_send_claim(fip);
2586 * fcoe_ctlr_vn_age() - Check for VN_ports without recent beacons
2587 * @fip: The FCoE controller
2589 * Called with ctlr_mutex held.
2590 * Called only in state FIP_ST_VNMP_UP.
2591 * Returns the soonest time for next age-out or a time far in the future.
2593 static unsigned long fcoe_ctlr_vn_age(struct fcoe_ctlr *fip)
2595 struct fc_lport *lport = fip->lp;
2596 struct fc_rport_priv *rdata;
2597 struct fcoe_rport *frport;
2598 unsigned long next_time;
2599 unsigned long deadline;
2601 next_time = jiffies + msecs_to_jiffies(FIP_VN_BEACON_INT * 10);
2602 mutex_lock(&lport->disc.disc_mutex);
2603 list_for_each_entry_rcu(rdata, &lport->disc.rports, peers) {
2604 frport = fcoe_ctlr_rport(rdata);
2607 deadline = frport->time +
2608 msecs_to_jiffies(FIP_VN_BEACON_INT * 25 / 10);
2609 if (time_after_eq(jiffies, deadline)) {
2611 LIBFCOE_FIP_DBG(fip,
2612 "port %16.16llx fc_id %6.6x beacon expired\n",
2613 rdata->ids.port_name, rdata->ids.port_id);
2614 lport->tt.rport_logoff(rdata);
2615 } else if (time_before(deadline, next_time))
2616 next_time = deadline;
2618 mutex_unlock(&lport->disc.disc_mutex);
2623 * fcoe_ctlr_vn_recv() - Receive a FIP frame
2624 * @fip: The FCoE controller that received the frame
2625 * @skb: The received FIP frame
2627 * Returns non-zero if the frame is dropped.
2628 * Always consumes the frame.
2630 static int fcoe_ctlr_vn_recv(struct fcoe_ctlr *fip, struct sk_buff *skb)
2632 struct fip_header *fiph;
2633 enum fip_vn2vn_subcode sub;
2635 struct fc_rport_priv rdata;
2636 struct fcoe_rport frport;
2640 fiph = (struct fip_header *)skb->data;
2641 sub = fiph->fip_subcode;
2643 rc = fcoe_ctlr_vn_parse(fip, skb, &buf.rdata);
2645 LIBFCOE_FIP_DBG(fip, "vn_recv vn_parse error %d\n", rc);
2649 mutex_lock(&fip->ctlr_mutex);
2651 case FIP_SC_VN_PROBE_REQ:
2652 fcoe_ctlr_vn_probe_req(fip, &buf.rdata);
2654 case FIP_SC_VN_PROBE_REP:
2655 fcoe_ctlr_vn_probe_reply(fip, &buf.rdata);
2657 case FIP_SC_VN_CLAIM_NOTIFY:
2658 fcoe_ctlr_vn_claim_notify(fip, &buf.rdata);
2660 case FIP_SC_VN_CLAIM_REP:
2661 fcoe_ctlr_vn_claim_resp(fip, &buf.rdata);
2663 case FIP_SC_VN_BEACON:
2664 fcoe_ctlr_vn_beacon(fip, &buf.rdata);
2667 LIBFCOE_FIP_DBG(fip, "vn_recv unknown subcode %d\n", sub);
2671 mutex_unlock(&fip->ctlr_mutex);
2678 * fcoe_ctlr_disc_recv - discovery receive handler for VN2VN mode.
2679 * @lport: The local port
2680 * @fp: The received frame
2682 * This should never be called since we don't see RSCNs or other
2683 * fabric-generated ELSes.
2685 static void fcoe_ctlr_disc_recv(struct fc_lport *lport, struct fc_frame *fp)
2687 struct fc_seq_els_data rjt_data;
2689 rjt_data.reason = ELS_RJT_UNSUP;
2690 rjt_data.explan = ELS_EXPL_NONE;
2691 lport->tt.seq_els_rsp_send(fp, ELS_LS_RJT, &rjt_data);
2696 * fcoe_ctlr_disc_recv - start discovery for VN2VN mode.
2697 * @fip: The FCoE controller
2699 * This sets a flag indicating that remote ports should be created
2700 * and started for the peers we discover. We use the disc_callback
2701 * pointer as that flag. Peers already discovered are created here.
2703 * The lport lock is held during this call. The callback must be done
2704 * later, without holding either the lport or discovery locks.
2705 * The fcoe_ctlr lock may also be held during this call.
2707 static void fcoe_ctlr_disc_start(void (*callback)(struct fc_lport *,
2708 enum fc_disc_event),
2709 struct fc_lport *lport)
2711 struct fc_disc *disc = &lport->disc;
2712 struct fcoe_ctlr *fip = disc->priv;
2714 mutex_lock(&disc->disc_mutex);
2715 disc->disc_callback = callback;
2716 disc->disc_id = (disc->disc_id + 2) | 1;
2718 schedule_work(&fip->timer_work);
2719 mutex_unlock(&disc->disc_mutex);
2723 * fcoe_ctlr_vn_disc() - report FIP VN_port discovery results after claim state.
2724 * @fip: The FCoE controller
2726 * Starts the FLOGI and PLOGI login process to each discovered rport for which
2727 * we've received at least one beacon.
2728 * Performs the discovery complete callback.
2730 static void fcoe_ctlr_vn_disc(struct fcoe_ctlr *fip)
2732 struct fc_lport *lport = fip->lp;
2733 struct fc_disc *disc = &lport->disc;
2734 struct fc_rport_priv *rdata;
2735 struct fcoe_rport *frport;
2736 void (*callback)(struct fc_lport *, enum fc_disc_event);
2738 mutex_lock(&disc->disc_mutex);
2739 callback = disc->pending ? disc->disc_callback : NULL;
2741 list_for_each_entry_rcu(rdata, &disc->rports, peers) {
2742 frport = fcoe_ctlr_rport(rdata);
2744 lport->tt.rport_login(rdata);
2746 mutex_unlock(&disc->disc_mutex);
2748 callback(lport, DISC_EV_SUCCESS);
2752 * fcoe_ctlr_vn_timeout - timer work function for VN2VN mode.
2753 * @fip: The FCoE controller
2755 static void fcoe_ctlr_vn_timeout(struct fcoe_ctlr *fip)
2757 unsigned long next_time;
2759 u32 new_port_id = 0;
2761 mutex_lock(&fip->ctlr_mutex);
2762 switch (fip->state) {
2763 case FIP_ST_VNMP_START:
2764 fcoe_ctlr_set_state(fip, FIP_ST_VNMP_PROBE1);
2765 fcoe_ctlr_vn_send(fip, FIP_SC_VN_PROBE_REQ, fcoe_all_vn2vn, 0);
2766 next_time = jiffies + msecs_to_jiffies(FIP_VN_PROBE_WAIT);
2768 case FIP_ST_VNMP_PROBE1:
2769 fcoe_ctlr_set_state(fip, FIP_ST_VNMP_PROBE2);
2770 fcoe_ctlr_vn_send(fip, FIP_SC_VN_PROBE_REQ, fcoe_all_vn2vn, 0);
2771 next_time = jiffies + msecs_to_jiffies(FIP_VN_ANN_WAIT);
2773 case FIP_ST_VNMP_PROBE2:
2774 fcoe_ctlr_set_state(fip, FIP_ST_VNMP_CLAIM);
2775 new_port_id = fip->port_id;
2776 hton24(mac, FIP_VN_FC_MAP);
2777 hton24(mac + 3, new_port_id);
2778 fcoe_ctlr_map_dest(fip);
2779 fip->update_mac(fip->lp, mac);
2780 fcoe_ctlr_vn_send_claim(fip);
2781 next_time = jiffies + msecs_to_jiffies(FIP_VN_ANN_WAIT);
2783 case FIP_ST_VNMP_CLAIM:
2785 * This may be invoked either by starting discovery so don't
2786 * go to the next state unless it's been long enough.
2788 next_time = fip->sol_time + msecs_to_jiffies(FIP_VN_ANN_WAIT);
2789 if (time_after_eq(jiffies, next_time)) {
2790 fcoe_ctlr_set_state(fip, FIP_ST_VNMP_UP);
2791 fcoe_ctlr_vn_send(fip, FIP_SC_VN_BEACON,
2793 next_time = jiffies + msecs_to_jiffies(FIP_VN_ANN_WAIT);
2794 fip->port_ka_time = next_time;
2796 fcoe_ctlr_vn_disc(fip);
2798 case FIP_ST_VNMP_UP:
2799 next_time = fcoe_ctlr_vn_age(fip);
2800 if (time_after_eq(jiffies, fip->port_ka_time)) {
2801 fcoe_ctlr_vn_send(fip, FIP_SC_VN_BEACON,
2803 fip->port_ka_time = jiffies +
2804 msecs_to_jiffies(FIP_VN_BEACON_INT +
2805 (prandom_u32() % FIP_VN_BEACON_FUZZ));
2807 if (time_before(fip->port_ka_time, next_time))
2808 next_time = fip->port_ka_time;
2810 case FIP_ST_LINK_WAIT:
2813 WARN(1, "unexpected state %d\n", fip->state);
2816 mod_timer(&fip->timer, next_time);
2818 mutex_unlock(&fip->ctlr_mutex);
2820 /* If port ID is new, notify local port after dropping ctlr_mutex */
2822 fc_lport_set_local_id(fip->lp, new_port_id);
2826 * fcoe_ctlr_mode_set() - Set or reset the ctlr's mode
2827 * @lport: The local port to be (re)configured
2828 * @fip: The FCoE controller whose mode is changing
2829 * @fip_mode: The new fip mode
2831 * Note that the we shouldn't be changing the libfc discovery settings
2832 * (fc_disc_config) while an lport is going through the libfc state
2833 * machine. The mode can only be changed when a fcoe_ctlr device is
2834 * disabled, so that should ensure that this routine is only called
2835 * when nothing is happening.
2837 static void fcoe_ctlr_mode_set(struct fc_lport *lport, struct fcoe_ctlr *fip,
2838 enum fip_state fip_mode)
2842 WARN_ON(lport->state != LPORT_ST_RESET &&
2843 lport->state != LPORT_ST_DISABLED);
2845 if (fip_mode == FIP_MODE_VN2VN) {
2846 lport->rport_priv_size = sizeof(struct fcoe_rport);
2847 lport->point_to_multipoint = 1;
2848 lport->tt.disc_recv_req = fcoe_ctlr_disc_recv;
2849 lport->tt.disc_start = fcoe_ctlr_disc_start;
2850 lport->tt.disc_stop = fcoe_ctlr_disc_stop;
2851 lport->tt.disc_stop_final = fcoe_ctlr_disc_stop_final;
2854 lport->rport_priv_size = 0;
2855 lport->point_to_multipoint = 0;
2856 lport->tt.disc_recv_req = NULL;
2857 lport->tt.disc_start = NULL;
2858 lport->tt.disc_stop = NULL;
2859 lport->tt.disc_stop_final = NULL;
2863 fc_disc_config(lport, priv);
2867 * fcoe_libfc_config() - Sets up libfc related properties for local port
2868 * @lport: The local port to configure libfc for
2869 * @fip: The FCoE controller in use by the local port
2870 * @tt: The libfc function template
2871 * @init_fcp: If non-zero, the FCP portion of libfc should be initialized
2873 * Returns : 0 for success
2875 int fcoe_libfc_config(struct fc_lport *lport, struct fcoe_ctlr *fip,
2876 const struct libfc_function_template *tt, int init_fcp)
2878 /* Set the function pointers set by the LLDD */
2879 memcpy(&lport->tt, tt, sizeof(*tt));
2880 if (init_fcp && fc_fcp_init(lport))
2882 fc_exch_init(lport);
2883 fc_elsct_init(lport);
2884 fc_lport_init(lport);
2885 fc_rport_init(lport);
2886 fc_disc_init(lport);
2887 fcoe_ctlr_mode_set(lport, fip, fip->mode);
2890 EXPORT_SYMBOL_GPL(fcoe_libfc_config);
2892 void fcoe_fcf_get_selected(struct fcoe_fcf_device *fcf_dev)
2894 struct fcoe_ctlr_device *ctlr_dev = fcoe_fcf_dev_to_ctlr_dev(fcf_dev);
2895 struct fcoe_ctlr *fip = fcoe_ctlr_device_priv(ctlr_dev);
2896 struct fcoe_fcf *fcf;
2898 mutex_lock(&fip->ctlr_mutex);
2899 mutex_lock(&ctlr_dev->lock);
2901 fcf = fcoe_fcf_device_priv(fcf_dev);
2903 fcf_dev->selected = (fcf == fip->sel_fcf) ? 1 : 0;
2905 fcf_dev->selected = 0;
2907 mutex_unlock(&ctlr_dev->lock);
2908 mutex_unlock(&fip->ctlr_mutex);
2910 EXPORT_SYMBOL(fcoe_fcf_get_selected);
2912 void fcoe_ctlr_set_fip_mode(struct fcoe_ctlr_device *ctlr_dev)
2914 struct fcoe_ctlr *ctlr = fcoe_ctlr_device_priv(ctlr_dev);
2915 struct fc_lport *lport = ctlr->lp;
2917 mutex_lock(&ctlr->ctlr_mutex);
2918 switch (ctlr_dev->mode) {
2919 case FIP_CONN_TYPE_VN2VN:
2920 ctlr->mode = FIP_MODE_VN2VN;
2922 case FIP_CONN_TYPE_FABRIC:
2924 ctlr->mode = FIP_MODE_FABRIC;
2928 mutex_unlock(&ctlr->ctlr_mutex);
2930 fcoe_ctlr_mode_set(lport, ctlr, ctlr->mode);
2932 EXPORT_SYMBOL(fcoe_ctlr_set_fip_mode);