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 mutex_lock(&ctlr_dev->lock);
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 */
184 fcf_dev = fcoe_fcf_device_add(ctlr_dev, &temp);
185 if (unlikely(!fcf_dev)) {
191 * The fcoe_sysfs layer can return a CONNECTED fcf that
192 * has a priv (fcf was never deleted) or a CONNECTED fcf
193 * that doesn't have a priv (fcf was deleted). However,
194 * libfcoe will always delete FCFs before trying to add
195 * them. This is ensured because both recv_adv and
196 * age_fcfs are protected by the the fcoe_ctlr's mutex.
197 * This means that we should never get a FCF with a
198 * non-NULL priv pointer.
200 BUG_ON(fcf_dev->priv);
203 new->fcf_dev = fcf_dev;
205 list_add(&new->list, &fip->fcfs);
209 mutex_unlock(&ctlr_dev->lock);
213 static void fcoe_sysfs_fcf_del(struct fcoe_fcf *new)
215 struct fcoe_ctlr *fip = new->fip;
216 struct fcoe_ctlr_device *ctlr_dev = fcoe_ctlr_to_ctlr_dev(fip);
217 struct fcoe_fcf_device *fcf_dev;
219 list_del(&new->list);
222 mutex_lock(&ctlr_dev->lock);
224 fcf_dev = fcoe_fcf_to_fcf_dev(new);
227 fcoe_fcf_device_delete(fcf_dev);
230 mutex_unlock(&ctlr_dev->lock);
234 * fcoe_ctlr_reset_fcfs() - Reset and free all FCFs for a controller
235 * @fip: The FCoE controller whose FCFs are to be reset
237 * Called with &fcoe_ctlr lock held.
239 static void fcoe_ctlr_reset_fcfs(struct fcoe_ctlr *fip)
241 struct fcoe_fcf *fcf;
242 struct fcoe_fcf *next;
245 list_for_each_entry_safe(fcf, next, &fip->fcfs, list) {
246 fcoe_sysfs_fcf_del(fcf);
248 WARN_ON(fip->fcf_count);
254 * fcoe_ctlr_destroy() - Disable and tear down a FCoE controller
255 * @fip: The FCoE controller to tear down
257 * This is called by FCoE drivers before freeing the &fcoe_ctlr.
259 * The receive handler will have been deleted before this to guarantee
260 * that no more recv_work will be scheduled.
262 * The timer routine will simply return once we set FIP_ST_DISABLED.
263 * This guarantees that no further timeouts or work will be scheduled.
265 void fcoe_ctlr_destroy(struct fcoe_ctlr *fip)
267 cancel_work_sync(&fip->recv_work);
268 skb_queue_purge(&fip->fip_recv_list);
270 mutex_lock(&fip->ctlr_mutex);
271 fcoe_ctlr_set_state(fip, FIP_ST_DISABLED);
272 fcoe_ctlr_reset_fcfs(fip);
273 mutex_unlock(&fip->ctlr_mutex);
274 del_timer_sync(&fip->timer);
275 cancel_work_sync(&fip->timer_work);
277 EXPORT_SYMBOL(fcoe_ctlr_destroy);
280 * fcoe_ctlr_announce() - announce new FCF selection
281 * @fip: The FCoE controller
283 * Also sets the destination MAC for FCoE and control packets
285 * Called with neither ctlr_mutex nor ctlr_lock held.
287 static void fcoe_ctlr_announce(struct fcoe_ctlr *fip)
289 struct fcoe_fcf *sel;
290 struct fcoe_fcf *fcf;
292 mutex_lock(&fip->ctlr_mutex);
293 spin_lock_bh(&fip->ctlr_lock);
295 kfree_skb(fip->flogi_req);
296 fip->flogi_req = NULL;
297 list_for_each_entry(fcf, &fip->fcfs, list)
300 spin_unlock_bh(&fip->ctlr_lock);
303 if (sel && !compare_ether_addr(sel->fcf_mac, fip->dest_addr))
305 if (!is_zero_ether_addr(fip->dest_addr)) {
306 printk(KERN_NOTICE "libfcoe: host%d: "
307 "FIP Fibre-Channel Forwarder MAC %pM deselected\n",
308 fip->lp->host->host_no, fip->dest_addr);
309 memset(fip->dest_addr, 0, ETH_ALEN);
312 printk(KERN_INFO "libfcoe: host%d: FIP selected "
313 "Fibre-Channel Forwarder MAC %pM\n",
314 fip->lp->host->host_no, sel->fcf_mac);
315 memcpy(fip->dest_addr, sel->fcoe_mac, ETH_ALEN);
319 mutex_unlock(&fip->ctlr_mutex);
323 * fcoe_ctlr_fcoe_size() - Return the maximum FCoE size required for VN_Port
324 * @fip: The FCoE controller to get the maximum FCoE size from
326 * Returns the maximum packet size including the FCoE header and trailer,
327 * but not including any Ethernet or VLAN headers.
329 static inline u32 fcoe_ctlr_fcoe_size(struct fcoe_ctlr *fip)
332 * Determine the max FCoE frame size allowed, including
333 * FCoE header and trailer.
334 * Note: lp->mfs is currently the payload size, not the frame size.
336 return fip->lp->mfs + sizeof(struct fc_frame_header) +
337 sizeof(struct fcoe_hdr) + sizeof(struct fcoe_crc_eof);
341 * fcoe_ctlr_solicit() - Send a FIP solicitation
342 * @fip: The FCoE controller to send the solicitation on
343 * @fcf: The destination FCF (if NULL, a multicast solicitation is sent)
345 static void fcoe_ctlr_solicit(struct fcoe_ctlr *fip, struct fcoe_fcf *fcf)
350 struct fip_header fip;
352 struct fip_mac_desc mac;
353 struct fip_wwn_desc wwnn;
354 struct fip_size_desc size;
359 skb = dev_alloc_skb(sizeof(*sol));
363 sol = (struct fip_sol *)skb->data;
365 memset(sol, 0, sizeof(*sol));
366 memcpy(sol->eth.h_dest, fcf ? fcf->fcf_mac : fcoe_all_fcfs, ETH_ALEN);
367 memcpy(sol->eth.h_source, fip->ctl_src_addr, ETH_ALEN);
368 sol->eth.h_proto = htons(ETH_P_FIP);
370 sol->fip.fip_ver = FIP_VER_ENCAPS(FIP_VER);
371 sol->fip.fip_op = htons(FIP_OP_DISC);
372 sol->fip.fip_subcode = FIP_SC_SOL;
373 sol->fip.fip_dl_len = htons(sizeof(sol->desc) / FIP_BPW);
374 sol->fip.fip_flags = htons(FIP_FL_FPMA);
376 sol->fip.fip_flags |= htons(FIP_FL_SPMA);
378 sol->desc.mac.fd_desc.fip_dtype = FIP_DT_MAC;
379 sol->desc.mac.fd_desc.fip_dlen = sizeof(sol->desc.mac) / FIP_BPW;
380 memcpy(sol->desc.mac.fd_mac, fip->ctl_src_addr, ETH_ALEN);
382 sol->desc.wwnn.fd_desc.fip_dtype = FIP_DT_NAME;
383 sol->desc.wwnn.fd_desc.fip_dlen = sizeof(sol->desc.wwnn) / FIP_BPW;
384 put_unaligned_be64(fip->lp->wwnn, &sol->desc.wwnn.fd_wwn);
386 fcoe_size = fcoe_ctlr_fcoe_size(fip);
387 sol->desc.size.fd_desc.fip_dtype = FIP_DT_FCOE_SIZE;
388 sol->desc.size.fd_desc.fip_dlen = sizeof(sol->desc.size) / FIP_BPW;
389 sol->desc.size.fd_size = htons(fcoe_size);
391 skb_put(skb, sizeof(*sol));
392 skb->protocol = htons(ETH_P_FIP);
393 skb->priority = fip->priority;
394 skb_reset_mac_header(skb);
395 skb_reset_network_header(skb);
399 fip->sol_time = jiffies;
403 * fcoe_ctlr_link_up() - Start FCoE controller
404 * @fip: The FCoE controller to start
406 * Called from the LLD when the network link is ready.
408 void fcoe_ctlr_link_up(struct fcoe_ctlr *fip)
410 mutex_lock(&fip->ctlr_mutex);
411 if (fip->state == FIP_ST_NON_FIP || fip->state == FIP_ST_AUTO) {
412 mutex_unlock(&fip->ctlr_mutex);
414 } else if (fip->state == FIP_ST_LINK_WAIT) {
415 fcoe_ctlr_set_state(fip, fip->mode);
418 LIBFCOE_FIP_DBG(fip, "invalid mode %d\n", fip->mode);
421 LIBFCOE_FIP_DBG(fip, "%s", "setting AUTO mode.\n");
423 case FIP_MODE_FABRIC:
424 case FIP_MODE_NON_FIP:
425 mutex_unlock(&fip->ctlr_mutex);
427 fcoe_ctlr_solicit(fip, NULL);
430 fcoe_ctlr_vn_start(fip);
431 mutex_unlock(&fip->ctlr_mutex);
436 mutex_unlock(&fip->ctlr_mutex);
438 EXPORT_SYMBOL(fcoe_ctlr_link_up);
441 * fcoe_ctlr_reset() - Reset a FCoE controller
442 * @fip: The FCoE controller to reset
444 static void fcoe_ctlr_reset(struct fcoe_ctlr *fip)
446 fcoe_ctlr_reset_fcfs(fip);
447 del_timer(&fip->timer);
448 fip->ctlr_ka_time = 0;
449 fip->port_ka_time = 0;
451 fip->flogi_oxid = FC_XID_UNKNOWN;
452 fcoe_ctlr_map_dest(fip);
456 * fcoe_ctlr_link_down() - Stop a FCoE controller
457 * @fip: The FCoE controller to be stopped
459 * Returns non-zero if the link was up and now isn't.
461 * Called from the LLD when the network link is not ready.
462 * There may be multiple calls while the link is down.
464 int fcoe_ctlr_link_down(struct fcoe_ctlr *fip)
468 LIBFCOE_FIP_DBG(fip, "link down.\n");
469 mutex_lock(&fip->ctlr_mutex);
470 fcoe_ctlr_reset(fip);
471 link_dropped = fip->state != FIP_ST_LINK_WAIT;
472 fcoe_ctlr_set_state(fip, FIP_ST_LINK_WAIT);
473 mutex_unlock(&fip->ctlr_mutex);
476 fc_linkdown(fip->lp);
479 EXPORT_SYMBOL(fcoe_ctlr_link_down);
482 * fcoe_ctlr_send_keep_alive() - Send a keep-alive to the selected FCF
483 * @fip: The FCoE controller to send the FKA on
484 * @lport: libfc fc_lport to send from
485 * @ports: 0 for controller keep-alive, 1 for port keep-alive
486 * @sa: The source MAC address
488 * A controller keep-alive is sent every fka_period (typically 8 seconds).
489 * The source MAC is the native MAC address.
491 * A port keep-alive is sent every 90 seconds while logged in.
492 * The source MAC is the assigned mapped source address.
493 * The destination is the FCF's F-port.
495 static void fcoe_ctlr_send_keep_alive(struct fcoe_ctlr *fip,
496 struct fc_lport *lport,
502 struct fip_header fip;
503 struct fip_mac_desc mac;
505 struct fip_vn_desc *vn;
508 struct fcoe_fcf *fcf;
512 if (!fcf || (ports && !lp->port_id))
515 len = sizeof(*kal) + ports * sizeof(*vn);
516 skb = dev_alloc_skb(len);
520 kal = (struct fip_kal *)skb->data;
522 memcpy(kal->eth.h_dest, fcf->fcf_mac, ETH_ALEN);
523 memcpy(kal->eth.h_source, sa, ETH_ALEN);
524 kal->eth.h_proto = htons(ETH_P_FIP);
526 kal->fip.fip_ver = FIP_VER_ENCAPS(FIP_VER);
527 kal->fip.fip_op = htons(FIP_OP_CTRL);
528 kal->fip.fip_subcode = FIP_SC_KEEP_ALIVE;
529 kal->fip.fip_dl_len = htons((sizeof(kal->mac) +
530 ports * sizeof(*vn)) / FIP_BPW);
531 kal->fip.fip_flags = htons(FIP_FL_FPMA);
533 kal->fip.fip_flags |= htons(FIP_FL_SPMA);
535 kal->mac.fd_desc.fip_dtype = FIP_DT_MAC;
536 kal->mac.fd_desc.fip_dlen = sizeof(kal->mac) / FIP_BPW;
537 memcpy(kal->mac.fd_mac, fip->ctl_src_addr, ETH_ALEN);
539 vn = (struct fip_vn_desc *)(kal + 1);
540 vn->fd_desc.fip_dtype = FIP_DT_VN_ID;
541 vn->fd_desc.fip_dlen = sizeof(*vn) / FIP_BPW;
542 memcpy(vn->fd_mac, fip->get_src_addr(lport), ETH_ALEN);
543 hton24(vn->fd_fc_id, lport->port_id);
544 put_unaligned_be64(lport->wwpn, &vn->fd_wwpn);
547 skb->protocol = htons(ETH_P_FIP);
548 skb->priority = fip->priority;
549 skb_reset_mac_header(skb);
550 skb_reset_network_header(skb);
555 * fcoe_ctlr_encaps() - Encapsulate an ELS frame for FIP, without sending it
556 * @fip: The FCoE controller for the ELS frame
557 * @dtype: The FIP descriptor type for the frame
558 * @skb: The FCoE ELS frame including FC header but no FCoE headers
559 * @d_id: The destination port ID.
561 * Returns non-zero error code on failure.
563 * The caller must check that the length is a multiple of 4.
565 * The @skb must have enough headroom (28 bytes) and tailroom (8 bytes).
566 * Headroom includes the FIP encapsulation description, FIP header, and
567 * Ethernet header. The tailroom is for the FIP MAC descriptor.
569 static int fcoe_ctlr_encaps(struct fcoe_ctlr *fip, struct fc_lport *lport,
570 u8 dtype, struct sk_buff *skb, u32 d_id)
572 struct fip_encaps_head {
574 struct fip_header fip;
575 struct fip_encaps encaps;
577 struct fc_frame_header *fh;
578 struct fip_mac_desc *mac;
579 struct fcoe_fcf *fcf;
584 fh = (struct fc_frame_header *)skb->data;
585 op = *(u8 *)(fh + 1);
586 dlen = sizeof(struct fip_encaps) + skb->len; /* len before push */
587 cap = (struct fip_encaps_head *)skb_push(skb, sizeof(*cap));
588 memset(cap, 0, sizeof(*cap));
590 if (lport->point_to_multipoint) {
591 if (fcoe_ctlr_vn_lookup(fip, d_id, cap->eth.h_dest))
598 fip_flags = fcf->flags;
599 fip_flags &= fip->spma ? FIP_FL_SPMA | FIP_FL_FPMA :
603 memcpy(cap->eth.h_dest, fcf->fcf_mac, ETH_ALEN);
605 memcpy(cap->eth.h_source, fip->ctl_src_addr, ETH_ALEN);
606 cap->eth.h_proto = htons(ETH_P_FIP);
608 cap->fip.fip_ver = FIP_VER_ENCAPS(FIP_VER);
609 cap->fip.fip_op = htons(FIP_OP_LS);
610 if (op == ELS_LS_ACC || op == ELS_LS_RJT)
611 cap->fip.fip_subcode = FIP_SC_REP;
613 cap->fip.fip_subcode = FIP_SC_REQ;
614 cap->fip.fip_flags = htons(fip_flags);
616 cap->encaps.fd_desc.fip_dtype = dtype;
617 cap->encaps.fd_desc.fip_dlen = dlen / FIP_BPW;
619 if (op != ELS_LS_RJT) {
620 dlen += sizeof(*mac);
621 mac = (struct fip_mac_desc *)skb_put(skb, sizeof(*mac));
622 memset(mac, 0, sizeof(*mac));
623 mac->fd_desc.fip_dtype = FIP_DT_MAC;
624 mac->fd_desc.fip_dlen = sizeof(*mac) / FIP_BPW;
625 if (dtype != FIP_DT_FLOGI && dtype != FIP_DT_FDISC) {
626 memcpy(mac->fd_mac, fip->get_src_addr(lport), ETH_ALEN);
627 } else if (fip->mode == FIP_MODE_VN2VN) {
628 hton24(mac->fd_mac, FIP_VN_FC_MAP);
629 hton24(mac->fd_mac + 3, fip->port_id);
630 } else if (fip_flags & FIP_FL_SPMA) {
631 LIBFCOE_FIP_DBG(fip, "FLOGI/FDISC sent with SPMA\n");
632 memcpy(mac->fd_mac, fip->ctl_src_addr, ETH_ALEN);
634 LIBFCOE_FIP_DBG(fip, "FLOGI/FDISC sent with FPMA\n");
635 /* FPMA only FLOGI. Must leave the MAC desc zeroed. */
638 cap->fip.fip_dl_len = htons(dlen / FIP_BPW);
640 skb->protocol = htons(ETH_P_FIP);
641 skb->priority = fip->priority;
642 skb_reset_mac_header(skb);
643 skb_reset_network_header(skb);
648 * fcoe_ctlr_els_send() - Send an ELS frame encapsulated by FIP if appropriate.
649 * @fip: FCoE controller.
650 * @lport: libfc fc_lport to send from
651 * @skb: FCoE ELS frame including FC header but no FCoE headers.
653 * Returns a non-zero error code if the frame should not be sent.
654 * Returns zero if the caller should send the frame with FCoE encapsulation.
656 * The caller must check that the length is a multiple of 4.
657 * The SKB must have enough headroom (28 bytes) and tailroom (8 bytes).
658 * The the skb must also be an fc_frame.
660 * This is called from the lower-level driver with spinlocks held,
661 * so we must not take a mutex here.
663 int fcoe_ctlr_els_send(struct fcoe_ctlr *fip, struct fc_lport *lport,
667 struct fc_frame_header *fh;
672 fp = container_of(skb, struct fc_frame, skb);
673 fh = (struct fc_frame_header *)skb->data;
674 op = *(u8 *)(fh + 1);
676 if (op == ELS_FLOGI && fip->mode != FIP_MODE_VN2VN) {
677 old_xid = fip->flogi_oxid;
678 fip->flogi_oxid = ntohs(fh->fh_ox_id);
679 if (fip->state == FIP_ST_AUTO) {
680 if (old_xid == FC_XID_UNKNOWN)
681 fip->flogi_count = 0;
683 if (fip->flogi_count < 3)
685 fcoe_ctlr_map_dest(fip);
688 if (fip->state == FIP_ST_NON_FIP)
689 fcoe_ctlr_map_dest(fip);
692 if (fip->state == FIP_ST_NON_FIP)
694 if (!fip->sel_fcf && fip->mode != FIP_MODE_VN2VN)
699 if (fip->mode == FIP_MODE_VN2VN)
701 spin_lock_bh(&fip->ctlr_lock);
702 kfree_skb(fip->flogi_req);
703 fip->flogi_req = skb;
704 fip->flogi_req_send = 1;
705 spin_unlock_bh(&fip->ctlr_lock);
706 schedule_work(&fip->timer_work);
709 if (ntoh24(fh->fh_s_id))
714 if (fip->mode == FIP_MODE_VN2VN) {
715 if (fip->state != FIP_ST_VNMP_UP)
717 if (ntoh24(fh->fh_d_id) == FC_FID_FLOGI)
720 if (fip->state != FIP_ST_ENABLED)
722 if (ntoh24(fh->fh_d_id) != FC_FID_FLOGI)
729 * If non-FIP, we may have gotten an SID by accepting an FLOGI
730 * from a point-to-point connection. Switch to using
731 * the source mac based on the SID. The destination
732 * MAC in this case would have been set by receiving the
735 if (fip->state == FIP_ST_NON_FIP) {
736 if (fip->flogi_oxid == FC_XID_UNKNOWN)
738 fip->flogi_oxid = FC_XID_UNKNOWN;
739 fc_fcoe_set_mac(mac, fh->fh_d_id);
740 fip->update_mac(lport, mac);
749 if (fip->state != FIP_ST_ENABLED &&
750 fip->state != FIP_ST_VNMP_UP)
754 LIBFCOE_FIP_DBG(fip, "els_send op %u d_id %x\n",
755 op, ntoh24(fh->fh_d_id));
756 if (fcoe_ctlr_encaps(fip, lport, op, skb, ntoh24(fh->fh_d_id)))
764 EXPORT_SYMBOL(fcoe_ctlr_els_send);
767 * fcoe_ctlr_age_fcfs() - Reset and free all old FCFs for a controller
768 * @fip: The FCoE controller to free FCFs on
770 * Called with lock held and preemption disabled.
772 * An FCF is considered old if we have missed two advertisements.
773 * That is, there have been no valid advertisement from it for 2.5
774 * times its keep-alive period.
776 * In addition, determine the time when an FCF selection can occur.
778 * Also, increment the MissDiscAdvCount when no advertisement is received
779 * for the corresponding FCF for 1.5 * FKA_ADV_PERIOD (FC-BB-5 LESB).
781 * Returns the time in jiffies for the next call.
783 static unsigned long fcoe_ctlr_age_fcfs(struct fcoe_ctlr *fip)
785 struct fcoe_fcf *fcf;
786 struct fcoe_fcf *next;
787 unsigned long next_timer = jiffies + msecs_to_jiffies(FIP_VN_KA_PERIOD);
788 unsigned long deadline;
789 unsigned long sel_time = 0;
790 struct list_head del_list;
791 struct fcoe_dev_stats *stats;
793 INIT_LIST_HEAD(&del_list);
795 stats = per_cpu_ptr(fip->lp->dev_stats, get_cpu());
797 list_for_each_entry_safe(fcf, next, &fip->fcfs, list) {
798 deadline = fcf->time + fcf->fka_period + fcf->fka_period / 2;
799 if (fip->sel_fcf == fcf) {
800 if (time_after(jiffies, deadline)) {
801 stats->MissDiscAdvCount++;
802 printk(KERN_INFO "libfcoe: host%d: "
803 "Missing Discovery Advertisement "
804 "for fab %16.16llx count %lld\n",
805 fip->lp->host->host_no, fcf->fabric_name,
806 stats->MissDiscAdvCount);
807 } else if (time_after(next_timer, deadline))
808 next_timer = deadline;
811 deadline += fcf->fka_period;
812 if (time_after_eq(jiffies, deadline)) {
813 if (fip->sel_fcf == fcf)
816 * Move to delete list so we can call
817 * fcoe_sysfs_fcf_del (which can sleep)
818 * after the put_cpu().
820 list_del(&fcf->list);
821 list_add(&fcf->list, &del_list);
822 stats->VLinkFailureCount++;
824 if (time_after(next_timer, deadline))
825 next_timer = deadline;
826 if (fcoe_ctlr_mtu_valid(fcf) &&
827 (!sel_time || time_before(sel_time, fcf->time)))
828 sel_time = fcf->time;
833 list_for_each_entry_safe(fcf, next, &del_list, list) {
834 /* Removes fcf from current list */
835 fcoe_sysfs_fcf_del(fcf);
838 if (sel_time && !fip->sel_fcf && !fip->sel_time) {
839 sel_time += msecs_to_jiffies(FCOE_CTLR_START_DELAY);
840 fip->sel_time = sel_time;
847 * fcoe_ctlr_parse_adv() - Decode a FIP advertisement into a new FCF entry
848 * @fip: The FCoE controller receiving the advertisement
849 * @skb: The received FIP advertisement frame
850 * @fcf: The resulting FCF entry
852 * Returns zero on a valid parsed advertisement,
853 * otherwise returns non zero value.
855 static int fcoe_ctlr_parse_adv(struct fcoe_ctlr *fip,
856 struct sk_buff *skb, struct fcoe_fcf *fcf)
858 struct fip_header *fiph;
859 struct fip_desc *desc = NULL;
860 struct fip_wwn_desc *wwn;
861 struct fip_fab_desc *fab;
862 struct fip_fka_desc *fka;
868 memset(fcf, 0, sizeof(*fcf));
869 fcf->fka_period = msecs_to_jiffies(FCOE_CTLR_DEF_FKA);
871 fiph = (struct fip_header *)skb->data;
872 fcf->flags = ntohs(fiph->fip_flags);
875 * mask of required descriptors. validating each one clears its bit.
877 desc_mask = BIT(FIP_DT_PRI) | BIT(FIP_DT_MAC) | BIT(FIP_DT_NAME) |
878 BIT(FIP_DT_FAB) | BIT(FIP_DT_FKA);
880 rlen = ntohs(fiph->fip_dl_len) * 4;
881 if (rlen + sizeof(*fiph) > skb->len)
884 desc = (struct fip_desc *)(fiph + 1);
886 dlen = desc->fip_dlen * FIP_BPW;
887 if (dlen < sizeof(*desc) || dlen > rlen)
889 /* Drop Adv if there are duplicate critical descriptors */
890 if ((desc->fip_dtype < 32) &&
891 !(desc_mask & 1U << desc->fip_dtype)) {
892 LIBFCOE_FIP_DBG(fip, "Duplicate Critical "
893 "Descriptors in FIP adv\n");
896 switch (desc->fip_dtype) {
898 if (dlen != sizeof(struct fip_pri_desc))
900 fcf->pri = ((struct fip_pri_desc *)desc)->fd_pri;
901 desc_mask &= ~BIT(FIP_DT_PRI);
904 if (dlen != sizeof(struct fip_mac_desc))
907 ((struct fip_mac_desc *)desc)->fd_mac,
909 memcpy(fcf->fcoe_mac, fcf->fcf_mac, ETH_ALEN);
910 if (!is_valid_ether_addr(fcf->fcf_mac)) {
912 "Invalid MAC addr %pM in FIP adv\n",
916 desc_mask &= ~BIT(FIP_DT_MAC);
919 if (dlen != sizeof(struct fip_wwn_desc))
921 wwn = (struct fip_wwn_desc *)desc;
922 fcf->switch_name = get_unaligned_be64(&wwn->fd_wwn);
923 desc_mask &= ~BIT(FIP_DT_NAME);
926 if (dlen != sizeof(struct fip_fab_desc))
928 fab = (struct fip_fab_desc *)desc;
929 fcf->fabric_name = get_unaligned_be64(&fab->fd_wwn);
930 fcf->vfid = ntohs(fab->fd_vfid);
931 fcf->fc_map = ntoh24(fab->fd_map);
932 desc_mask &= ~BIT(FIP_DT_FAB);
935 if (dlen != sizeof(struct fip_fka_desc))
937 fka = (struct fip_fka_desc *)desc;
938 if (fka->fd_flags & FIP_FKA_ADV_D)
940 t = ntohl(fka->fd_fka_period);
941 if (t >= FCOE_CTLR_MIN_FKA)
942 fcf->fka_period = msecs_to_jiffies(t);
943 desc_mask &= ~BIT(FIP_DT_FKA);
946 case FIP_DT_FCOE_SIZE:
952 LIBFCOE_FIP_DBG(fip, "unexpected descriptor type %x "
953 "in FIP adv\n", desc->fip_dtype);
954 /* standard says ignore unknown descriptors >= 128 */
955 if (desc->fip_dtype < FIP_DT_VENDOR_BASE)
959 desc = (struct fip_desc *)((char *)desc + dlen);
962 if (!fcf->fc_map || (fcf->fc_map & 0x10000))
964 if (!fcf->switch_name)
967 LIBFCOE_FIP_DBG(fip, "adv missing descriptors mask %x\n",
974 LIBFCOE_FIP_DBG(fip, "FIP length error in descriptor type %x len %zu\n",
975 desc->fip_dtype, dlen);
980 * fcoe_ctlr_recv_adv() - Handle an incoming advertisement
981 * @fip: The FCoE controller receiving the advertisement
982 * @skb: The received FIP packet
984 static void fcoe_ctlr_recv_adv(struct fcoe_ctlr *fip, struct sk_buff *skb)
986 struct fcoe_fcf *fcf;
988 unsigned long sol_tov = msecs_to_jiffies(FCOE_CTRL_SOL_TOV);
994 if (fcoe_ctlr_parse_adv(fip, skb, &new))
997 mutex_lock(&fip->ctlr_mutex);
998 first = list_empty(&fip->fcfs);
999 list_for_each_entry(fcf, &fip->fcfs, list) {
1000 if (fcf->switch_name == new.switch_name &&
1001 fcf->fabric_name == new.fabric_name &&
1002 fcf->fc_map == new.fc_map &&
1003 compare_ether_addr(fcf->fcf_mac, new.fcf_mac) == 0) {
1009 if (fip->fcf_count >= FCOE_CTLR_FCF_LIMIT)
1012 fcf = kmalloc(sizeof(*fcf), GFP_ATOMIC);
1016 memcpy(fcf, &new, sizeof(new));
1018 rc = fcoe_sysfs_fcf_add(fcf);
1020 printk(KERN_ERR "Failed to allocate sysfs instance "
1021 "for FCF, fab %16.16llx mac %pM\n",
1022 new.fabric_name, new.fcf_mac);
1028 * Update the FCF's keep-alive descriptor flags.
1029 * Other flag changes from new advertisements are
1030 * ignored after a solicited advertisement is
1031 * received and the FCF is selectable (usable).
1033 fcf->fd_flags = new.fd_flags;
1034 if (!fcoe_ctlr_fcf_usable(fcf))
1035 fcf->flags = new.flags;
1037 if (fcf == fip->sel_fcf && !fcf->fd_flags) {
1038 fip->ctlr_ka_time -= fcf->fka_period;
1039 fip->ctlr_ka_time += new.fka_period;
1040 if (time_before(fip->ctlr_ka_time, fip->timer.expires))
1041 mod_timer(&fip->timer, fip->ctlr_ka_time);
1043 fcf->fka_period = new.fka_period;
1044 memcpy(fcf->fcf_mac, new.fcf_mac, ETH_ALEN);
1047 mtu_valid = fcoe_ctlr_mtu_valid(fcf);
1048 fcf->time = jiffies;
1050 LIBFCOE_FIP_DBG(fip, "New FCF fab %16.16llx mac %pM\n",
1051 fcf->fabric_name, fcf->fcf_mac);
1054 * If this advertisement is not solicited and our max receive size
1055 * hasn't been verified, send a solicited advertisement.
1058 fcoe_ctlr_solicit(fip, fcf);
1061 * If its been a while since we did a solicit, and this is
1062 * the first advertisement we've received, do a multicast
1063 * solicitation to gather as many advertisements as we can
1064 * before selection occurs.
1066 if (first && time_after(jiffies, fip->sol_time + sol_tov))
1067 fcoe_ctlr_solicit(fip, NULL);
1070 * Put this FCF at the head of the list for priority among equals.
1071 * This helps in the case of an NPV switch which insists we use
1072 * the FCF that answers multicast solicitations, not the others that
1073 * are sending periodic multicast advertisements.
1076 list_move(&fcf->list, &fip->fcfs);
1079 * If this is the first validated FCF, note the time and
1080 * set a timer to trigger selection.
1082 if (mtu_valid && !fip->sel_fcf && fcoe_ctlr_fcf_usable(fcf)) {
1083 fip->sel_time = jiffies +
1084 msecs_to_jiffies(FCOE_CTLR_START_DELAY);
1085 if (!timer_pending(&fip->timer) ||
1086 time_before(fip->sel_time, fip->timer.expires))
1087 mod_timer(&fip->timer, fip->sel_time);
1091 mutex_unlock(&fip->ctlr_mutex);
1095 * fcoe_ctlr_recv_els() - Handle an incoming FIP encapsulated ELS frame
1096 * @fip: The FCoE controller which received the packet
1097 * @skb: The received FIP packet
1099 static void fcoe_ctlr_recv_els(struct fcoe_ctlr *fip, struct sk_buff *skb)
1101 struct fc_lport *lport = fip->lp;
1102 struct fip_header *fiph;
1103 struct fc_frame *fp = (struct fc_frame *)skb;
1104 struct fc_frame_header *fh = NULL;
1105 struct fip_desc *desc;
1106 struct fip_encaps *els;
1107 struct fcoe_dev_stats *stats;
1108 struct fcoe_fcf *sel;
1109 enum fip_desc_type els_dtype = 0;
1112 u8 granted_mac[ETH_ALEN] = { 0 };
1119 fiph = (struct fip_header *)skb->data;
1120 sub = fiph->fip_subcode;
1121 if (sub != FIP_SC_REQ && sub != FIP_SC_REP)
1124 rlen = ntohs(fiph->fip_dl_len) * 4;
1125 if (rlen + sizeof(*fiph) > skb->len)
1128 desc = (struct fip_desc *)(fiph + 1);
1131 dlen = desc->fip_dlen * FIP_BPW;
1132 if (dlen < sizeof(*desc) || dlen > rlen)
1134 /* Drop ELS if there are duplicate critical descriptors */
1135 if (desc->fip_dtype < 32) {
1136 if ((desc->fip_dtype != FIP_DT_MAC) &&
1137 (desc_mask & 1U << desc->fip_dtype)) {
1138 LIBFCOE_FIP_DBG(fip, "Duplicate Critical "
1139 "Descriptors in FIP ELS\n");
1142 desc_mask |= (1 << desc->fip_dtype);
1144 switch (desc->fip_dtype) {
1147 if (desc_cnt == 1) {
1148 LIBFCOE_FIP_DBG(fip, "FIP descriptors "
1149 "received out of order\n");
1153 * Some switch implementations send two MAC descriptors,
1154 * with first MAC(granted_mac) being the FPMA, and the
1155 * second one(fcoe_mac) is used as destination address
1156 * for sending/receiving FCoE packets. FIP traffic is
1157 * sent using fip_mac. For regular switches, both
1158 * fip_mac and fcoe_mac would be the same.
1162 ((struct fip_mac_desc *)desc)->fd_mac,
1165 if (dlen != sizeof(struct fip_mac_desc))
1168 if ((desc_cnt == 3) && (sel))
1169 memcpy(sel->fcoe_mac,
1170 ((struct fip_mac_desc *)desc)->fd_mac,
1177 if (desc_cnt != 1) {
1178 LIBFCOE_FIP_DBG(fip, "FIP descriptors "
1179 "received out of order\n");
1184 if (dlen < sizeof(*els) + sizeof(*fh) + 1)
1186 els_len = dlen - sizeof(*els);
1187 els = (struct fip_encaps *)desc;
1188 fh = (struct fc_frame_header *)(els + 1);
1189 els_dtype = desc->fip_dtype;
1192 LIBFCOE_FIP_DBG(fip, "unexpected descriptor type %x "
1193 "in FIP adv\n", desc->fip_dtype);
1194 /* standard says ignore unknown descriptors >= 128 */
1195 if (desc->fip_dtype < FIP_DT_VENDOR_BASE)
1197 if (desc_cnt <= 2) {
1198 LIBFCOE_FIP_DBG(fip, "FIP descriptors "
1199 "received out of order\n");
1204 desc = (struct fip_desc *)((char *)desc + dlen);
1210 els_op = *(u8 *)(fh + 1);
1212 if ((els_dtype == FIP_DT_FLOGI || els_dtype == FIP_DT_FDISC) &&
1213 sub == FIP_SC_REP && fip->mode != FIP_MODE_VN2VN) {
1214 if (els_op == ELS_LS_ACC) {
1215 if (!is_valid_ether_addr(granted_mac)) {
1216 LIBFCOE_FIP_DBG(fip,
1217 "Invalid MAC address %pM in FIP ELS\n",
1221 memcpy(fr_cb(fp)->granted_mac, granted_mac, ETH_ALEN);
1223 if (fip->flogi_oxid == ntohs(fh->fh_ox_id)) {
1224 fip->flogi_oxid = FC_XID_UNKNOWN;
1225 if (els_dtype == FIP_DT_FLOGI)
1226 fcoe_ctlr_announce(fip);
1228 } else if (els_dtype == FIP_DT_FLOGI &&
1229 !fcoe_ctlr_flogi_retry(fip))
1230 goto drop; /* retrying FLOGI so drop reject */
1233 if ((desc_cnt == 0) || ((els_op != ELS_LS_RJT) &&
1234 (!(1U << FIP_DT_MAC & desc_mask)))) {
1235 LIBFCOE_FIP_DBG(fip, "Missing critical descriptors "
1241 * Convert skb into an fc_frame containing only the ELS.
1243 skb_pull(skb, (u8 *)fh - skb->data);
1244 skb_trim(skb, els_len);
1245 fp = (struct fc_frame *)skb;
1247 fr_sof(fp) = FC_SOF_I3;
1248 fr_eof(fp) = FC_EOF_T;
1250 fr_encaps(fp) = els_dtype;
1252 stats = per_cpu_ptr(lport->dev_stats, get_cpu());
1254 stats->RxWords += skb->len / FIP_BPW;
1257 fc_exch_recv(lport, fp);
1261 LIBFCOE_FIP_DBG(fip, "FIP length error in descriptor type %x len %zu\n",
1262 desc->fip_dtype, dlen);
1268 * fcoe_ctlr_recv_els() - Handle an incoming link reset frame
1269 * @fip: The FCoE controller that received the frame
1270 * @fh: The received FIP header
1272 * There may be multiple VN_Port descriptors.
1273 * The overall length has already been checked.
1275 static void fcoe_ctlr_recv_clr_vlink(struct fcoe_ctlr *fip,
1276 struct fip_header *fh)
1278 struct fip_desc *desc;
1279 struct fip_mac_desc *mp;
1280 struct fip_wwn_desc *wp;
1281 struct fip_vn_desc *vp;
1284 struct fcoe_fcf *fcf = fip->sel_fcf;
1285 struct fc_lport *lport = fip->lp;
1286 struct fc_lport *vn_port = NULL;
1289 int reset_phys_port = 0;
1290 struct fip_vn_desc **vlink_desc_arr = NULL;
1292 LIBFCOE_FIP_DBG(fip, "Clear Virtual Link received\n");
1294 if (!fcf || !lport->port_id)
1298 * mask of required descriptors. Validating each one clears its bit.
1300 desc_mask = BIT(FIP_DT_MAC) | BIT(FIP_DT_NAME);
1302 rlen = ntohs(fh->fip_dl_len) * FIP_BPW;
1303 desc = (struct fip_desc *)(fh + 1);
1306 * Actually need to subtract 'sizeof(*mp) - sizeof(*wp)' from 'rlen'
1307 * before determining max Vx_Port descriptor but a buggy FCF could have
1308 * omited either or both MAC Address and Name Identifier descriptors
1310 num_vlink_desc = rlen / sizeof(*vp);
1312 vlink_desc_arr = kmalloc(sizeof(vp) * num_vlink_desc,
1314 if (!vlink_desc_arr)
1318 while (rlen >= sizeof(*desc)) {
1319 dlen = desc->fip_dlen * FIP_BPW;
1322 /* Drop CVL if there are duplicate critical descriptors */
1323 if ((desc->fip_dtype < 32) &&
1324 (desc->fip_dtype != FIP_DT_VN_ID) &&
1325 !(desc_mask & 1U << desc->fip_dtype)) {
1326 LIBFCOE_FIP_DBG(fip, "Duplicate Critical "
1327 "Descriptors in FIP CVL\n");
1330 switch (desc->fip_dtype) {
1332 mp = (struct fip_mac_desc *)desc;
1333 if (dlen < sizeof(*mp))
1335 if (compare_ether_addr(mp->fd_mac, fcf->fcf_mac))
1337 desc_mask &= ~BIT(FIP_DT_MAC);
1340 wp = (struct fip_wwn_desc *)desc;
1341 if (dlen < sizeof(*wp))
1343 if (get_unaligned_be64(&wp->fd_wwn) != fcf->switch_name)
1345 desc_mask &= ~BIT(FIP_DT_NAME);
1348 vp = (struct fip_vn_desc *)desc;
1349 if (dlen < sizeof(*vp))
1351 vlink_desc_arr[num_vlink_desc++] = vp;
1352 vn_port = fc_vport_id_lookup(lport,
1353 ntoh24(vp->fd_fc_id));
1354 if (vn_port && (vn_port == lport)) {
1355 mutex_lock(&fip->ctlr_mutex);
1356 per_cpu_ptr(lport->dev_stats,
1357 get_cpu())->VLinkFailureCount++;
1359 fcoe_ctlr_reset(fip);
1360 mutex_unlock(&fip->ctlr_mutex);
1364 /* standard says ignore unknown descriptors >= 128 */
1365 if (desc->fip_dtype < FIP_DT_VENDOR_BASE)
1369 desc = (struct fip_desc *)((char *)desc + dlen);
1374 * reset only if all required descriptors were present and valid.
1377 LIBFCOE_FIP_DBG(fip, "missing descriptors mask %x\n",
1379 else if (!num_vlink_desc) {
1380 LIBFCOE_FIP_DBG(fip, "CVL: no Vx_Port descriptor found\n");
1382 * No Vx_Port description. Clear all NPIV ports,
1383 * followed by physical port
1385 mutex_lock(&fip->ctlr_mutex);
1386 per_cpu_ptr(lport->dev_stats,
1387 get_cpu())->VLinkFailureCount++;
1389 fcoe_ctlr_reset(fip);
1390 mutex_unlock(&fip->ctlr_mutex);
1392 mutex_lock(&lport->lp_mutex);
1393 list_for_each_entry(vn_port, &lport->vports, list)
1394 fc_lport_reset(vn_port);
1395 mutex_unlock(&lport->lp_mutex);
1397 fc_lport_reset(fip->lp);
1398 fcoe_ctlr_solicit(fip, NULL);
1402 LIBFCOE_FIP_DBG(fip, "performing Clear Virtual Link\n");
1403 for (i = 0; i < num_vlink_desc; i++) {
1404 vp = vlink_desc_arr[i];
1405 vn_port = fc_vport_id_lookup(lport,
1406 ntoh24(vp->fd_fc_id));
1411 * 'port_id' is already validated, check MAC address and
1414 if (compare_ether_addr(fip->get_src_addr(vn_port),
1416 get_unaligned_be64(&vp->fd_wwpn) !=
1420 if (vn_port == lport)
1422 * Physical port, defer processing till all
1423 * listed NPIV ports are cleared
1425 reset_phys_port = 1;
1426 else /* NPIV port */
1427 fc_lport_reset(vn_port);
1430 if (reset_phys_port) {
1431 fc_lport_reset(fip->lp);
1432 fcoe_ctlr_solicit(fip, NULL);
1437 kfree(vlink_desc_arr);
1441 * fcoe_ctlr_recv() - Receive a FIP packet
1442 * @fip: The FCoE controller that received the packet
1443 * @skb: The received FIP packet
1445 * This may be called from either NET_RX_SOFTIRQ or IRQ.
1447 void fcoe_ctlr_recv(struct fcoe_ctlr *fip, struct sk_buff *skb)
1449 skb_queue_tail(&fip->fip_recv_list, skb);
1450 schedule_work(&fip->recv_work);
1452 EXPORT_SYMBOL(fcoe_ctlr_recv);
1455 * fcoe_ctlr_recv_handler() - Receive a FIP frame
1456 * @fip: The FCoE controller that received the frame
1457 * @skb: The received FIP frame
1459 * Returns non-zero if the frame is dropped.
1461 static int fcoe_ctlr_recv_handler(struct fcoe_ctlr *fip, struct sk_buff *skb)
1463 struct fip_header *fiph;
1465 enum fip_state state;
1469 if (skb_linearize(skb))
1471 if (skb->len < sizeof(*fiph))
1474 if (fip->mode == FIP_MODE_VN2VN) {
1475 if (compare_ether_addr(eh->h_dest, fip->ctl_src_addr) &&
1476 compare_ether_addr(eh->h_dest, fcoe_all_vn2vn) &&
1477 compare_ether_addr(eh->h_dest, fcoe_all_p2p))
1479 } else if (compare_ether_addr(eh->h_dest, fip->ctl_src_addr) &&
1480 compare_ether_addr(eh->h_dest, fcoe_all_enode))
1482 fiph = (struct fip_header *)skb->data;
1483 op = ntohs(fiph->fip_op);
1484 sub = fiph->fip_subcode;
1486 if (FIP_VER_DECAPS(fiph->fip_ver) != FIP_VER)
1488 if (ntohs(fiph->fip_dl_len) * FIP_BPW + sizeof(*fiph) > skb->len)
1491 mutex_lock(&fip->ctlr_mutex);
1493 if (state == FIP_ST_AUTO) {
1495 fcoe_ctlr_set_state(fip, FIP_ST_ENABLED);
1496 state = FIP_ST_ENABLED;
1497 LIBFCOE_FIP_DBG(fip, "Using FIP mode\n");
1499 mutex_unlock(&fip->ctlr_mutex);
1501 if (fip->mode == FIP_MODE_VN2VN && op == FIP_OP_VN2VN)
1502 return fcoe_ctlr_vn_recv(fip, skb);
1504 if (state != FIP_ST_ENABLED && state != FIP_ST_VNMP_UP &&
1505 state != FIP_ST_VNMP_CLAIM)
1508 if (op == FIP_OP_LS) {
1509 fcoe_ctlr_recv_els(fip, skb); /* consumes skb */
1513 if (state != FIP_ST_ENABLED)
1516 if (op == FIP_OP_DISC && sub == FIP_SC_ADV)
1517 fcoe_ctlr_recv_adv(fip, skb);
1518 else if (op == FIP_OP_CTRL && sub == FIP_SC_CLR_VLINK)
1519 fcoe_ctlr_recv_clr_vlink(fip, fiph);
1528 * fcoe_ctlr_select() - Select the best FCF (if possible)
1529 * @fip: The FCoE controller
1531 * Returns the selected FCF, or NULL if none are usable.
1533 * If there are conflicting advertisements, no FCF can be chosen.
1535 * If there is already a selected FCF, this will choose a better one or
1536 * an equivalent one that hasn't already been sent a FLOGI.
1538 * Called with lock held.
1540 static struct fcoe_fcf *fcoe_ctlr_select(struct fcoe_ctlr *fip)
1542 struct fcoe_fcf *fcf;
1543 struct fcoe_fcf *best = fip->sel_fcf;
1544 struct fcoe_fcf *first;
1546 first = list_first_entry(&fip->fcfs, struct fcoe_fcf, list);
1548 list_for_each_entry(fcf, &fip->fcfs, list) {
1549 LIBFCOE_FIP_DBG(fip, "consider FCF fab %16.16llx "
1550 "VFID %d mac %pM map %x val %d "
1552 fcf->fabric_name, fcf->vfid, fcf->fcf_mac,
1553 fcf->fc_map, fcoe_ctlr_mtu_valid(fcf),
1554 fcf->flogi_sent, fcf->pri);
1555 if (fcf->fabric_name != first->fabric_name ||
1556 fcf->vfid != first->vfid ||
1557 fcf->fc_map != first->fc_map) {
1558 LIBFCOE_FIP_DBG(fip, "Conflicting fabric, VFID, "
1562 if (fcf->flogi_sent)
1564 if (!fcoe_ctlr_fcf_usable(fcf)) {
1565 LIBFCOE_FIP_DBG(fip, "FCF for fab %16.16llx "
1566 "map %x %svalid %savailable\n",
1567 fcf->fabric_name, fcf->fc_map,
1568 (fcf->flags & FIP_FL_SOL) ? "" : "in",
1569 (fcf->flags & FIP_FL_AVAIL) ?
1573 if (!best || fcf->pri < best->pri || best->flogi_sent)
1576 fip->sel_fcf = best;
1578 LIBFCOE_FIP_DBG(fip, "using FCF mac %pM\n", best->fcf_mac);
1579 fip->port_ka_time = jiffies +
1580 msecs_to_jiffies(FIP_VN_KA_PERIOD);
1581 fip->ctlr_ka_time = jiffies + best->fka_period;
1582 if (time_before(fip->ctlr_ka_time, fip->timer.expires))
1583 mod_timer(&fip->timer, fip->ctlr_ka_time);
1589 * fcoe_ctlr_flogi_send_locked() - send FIP-encapsulated FLOGI to current FCF
1590 * @fip: The FCoE controller
1592 * Returns non-zero error if it could not be sent.
1594 * Called with ctlr_mutex and ctlr_lock held.
1595 * Caller must verify that fip->sel_fcf is not NULL.
1597 static int fcoe_ctlr_flogi_send_locked(struct fcoe_ctlr *fip)
1599 struct sk_buff *skb;
1600 struct sk_buff *skb_orig;
1601 struct fc_frame_header *fh;
1604 skb_orig = fip->flogi_req;
1609 * Clone and send the FLOGI request. If clone fails, use original.
1611 skb = skb_clone(skb_orig, GFP_ATOMIC);
1614 fip->flogi_req = NULL;
1616 fh = (struct fc_frame_header *)skb->data;
1617 error = fcoe_ctlr_encaps(fip, fip->lp, FIP_DT_FLOGI, skb,
1618 ntoh24(fh->fh_d_id));
1623 fip->send(fip, skb);
1624 fip->sel_fcf->flogi_sent = 1;
1629 * fcoe_ctlr_flogi_retry() - resend FLOGI request to a new FCF if possible
1630 * @fip: The FCoE controller
1632 * Returns non-zero error code if there's no FLOGI request to retry or
1633 * no alternate FCF available.
1635 static int fcoe_ctlr_flogi_retry(struct fcoe_ctlr *fip)
1637 struct fcoe_fcf *fcf;
1640 mutex_lock(&fip->ctlr_mutex);
1641 spin_lock_bh(&fip->ctlr_lock);
1642 LIBFCOE_FIP_DBG(fip, "re-sending FLOGI - reselect\n");
1643 fcf = fcoe_ctlr_select(fip);
1644 if (!fcf || fcf->flogi_sent) {
1645 kfree_skb(fip->flogi_req);
1646 fip->flogi_req = NULL;
1649 fcoe_ctlr_solicit(fip, NULL);
1650 error = fcoe_ctlr_flogi_send_locked(fip);
1652 spin_unlock_bh(&fip->ctlr_lock);
1653 mutex_unlock(&fip->ctlr_mutex);
1659 * fcoe_ctlr_flogi_send() - Handle sending of FIP FLOGI.
1660 * @fip: The FCoE controller that timed out
1662 * Done here because fcoe_ctlr_els_send() can't get mutex.
1664 * Called with ctlr_mutex held. The caller must not hold ctlr_lock.
1666 static void fcoe_ctlr_flogi_send(struct fcoe_ctlr *fip)
1668 struct fcoe_fcf *fcf;
1670 spin_lock_bh(&fip->ctlr_lock);
1672 if (!fcf || !fip->flogi_req_send)
1675 LIBFCOE_FIP_DBG(fip, "sending FLOGI\n");
1678 * If this FLOGI is being sent due to a timeout retry
1679 * to the same FCF as before, select a different FCF if possible.
1681 if (fcf->flogi_sent) {
1682 LIBFCOE_FIP_DBG(fip, "sending FLOGI - reselect\n");
1683 fcf = fcoe_ctlr_select(fip);
1684 if (!fcf || fcf->flogi_sent) {
1685 LIBFCOE_FIP_DBG(fip, "sending FLOGI - clearing\n");
1686 list_for_each_entry(fcf, &fip->fcfs, list)
1687 fcf->flogi_sent = 0;
1688 fcf = fcoe_ctlr_select(fip);
1692 fcoe_ctlr_flogi_send_locked(fip);
1693 fip->flogi_req_send = 0;
1695 LIBFCOE_FIP_DBG(fip, "No FCF selected - defer send\n");
1697 spin_unlock_bh(&fip->ctlr_lock);
1701 * fcoe_ctlr_timeout() - FIP timeout handler
1702 * @arg: The FCoE controller that timed out
1704 static void fcoe_ctlr_timeout(unsigned long arg)
1706 struct fcoe_ctlr *fip = (struct fcoe_ctlr *)arg;
1708 schedule_work(&fip->timer_work);
1712 * fcoe_ctlr_timer_work() - Worker thread function for timer work
1713 * @work: Handle to a FCoE controller
1715 * Ages FCFs. Triggers FCF selection if possible.
1716 * Sends keep-alives and resets.
1718 static void fcoe_ctlr_timer_work(struct work_struct *work)
1720 struct fcoe_ctlr *fip;
1721 struct fc_lport *vport;
1724 u8 send_ctlr_ka = 0;
1725 u8 send_port_ka = 0;
1726 struct fcoe_fcf *sel;
1727 struct fcoe_fcf *fcf;
1728 unsigned long next_timer;
1730 fip = container_of(work, struct fcoe_ctlr, timer_work);
1731 if (fip->mode == FIP_MODE_VN2VN)
1732 return fcoe_ctlr_vn_timeout(fip);
1733 mutex_lock(&fip->ctlr_mutex);
1734 if (fip->state == FIP_ST_DISABLED) {
1735 mutex_unlock(&fip->ctlr_mutex);
1740 next_timer = fcoe_ctlr_age_fcfs(fip);
1743 if (!sel && fip->sel_time) {
1744 if (time_after_eq(jiffies, fip->sel_time)) {
1745 sel = fcoe_ctlr_select(fip);
1747 } else if (time_after(next_timer, fip->sel_time))
1748 next_timer = fip->sel_time;
1751 if (sel && fip->flogi_req_send)
1752 fcoe_ctlr_flogi_send(fip);
1753 else if (!sel && fcf)
1756 if (sel && !sel->fd_flags) {
1757 if (time_after_eq(jiffies, fip->ctlr_ka_time)) {
1758 fip->ctlr_ka_time = jiffies + sel->fka_period;
1761 if (time_after(next_timer, fip->ctlr_ka_time))
1762 next_timer = fip->ctlr_ka_time;
1764 if (time_after_eq(jiffies, fip->port_ka_time)) {
1765 fip->port_ka_time = jiffies +
1766 msecs_to_jiffies(FIP_VN_KA_PERIOD);
1769 if (time_after(next_timer, fip->port_ka_time))
1770 next_timer = fip->port_ka_time;
1772 if (!list_empty(&fip->fcfs))
1773 mod_timer(&fip->timer, next_timer);
1774 mutex_unlock(&fip->ctlr_mutex);
1777 fc_lport_reset(fip->lp);
1778 /* restart things with a solicitation */
1779 fcoe_ctlr_solicit(fip, NULL);
1783 fcoe_ctlr_send_keep_alive(fip, NULL, 0, fip->ctl_src_addr);
1786 mutex_lock(&fip->lp->lp_mutex);
1787 mac = fip->get_src_addr(fip->lp);
1788 fcoe_ctlr_send_keep_alive(fip, fip->lp, 1, mac);
1789 list_for_each_entry(vport, &fip->lp->vports, list) {
1790 mac = fip->get_src_addr(vport);
1791 fcoe_ctlr_send_keep_alive(fip, vport, 1, mac);
1793 mutex_unlock(&fip->lp->lp_mutex);
1798 * fcoe_ctlr_recv_work() - Worker thread function for receiving FIP frames
1799 * @recv_work: Handle to a FCoE controller
1801 static void fcoe_ctlr_recv_work(struct work_struct *recv_work)
1803 struct fcoe_ctlr *fip;
1804 struct sk_buff *skb;
1806 fip = container_of(recv_work, struct fcoe_ctlr, recv_work);
1807 while ((skb = skb_dequeue(&fip->fip_recv_list)))
1808 fcoe_ctlr_recv_handler(fip, skb);
1812 * fcoe_ctlr_recv_flogi() - Snoop pre-FIP receipt of FLOGI response
1813 * @fip: The FCoE controller
1814 * @fp: The FC frame to snoop
1816 * Snoop potential response to FLOGI or even incoming FLOGI.
1818 * The caller has checked that we are waiting for login as indicated
1819 * by fip->flogi_oxid != FC_XID_UNKNOWN.
1821 * The caller is responsible for freeing the frame.
1822 * Fill in the granted_mac address.
1824 * Return non-zero if the frame should not be delivered to libfc.
1826 int fcoe_ctlr_recv_flogi(struct fcoe_ctlr *fip, struct fc_lport *lport,
1827 struct fc_frame *fp)
1829 struct fc_frame_header *fh;
1833 sa = eth_hdr(&fp->skb)->h_source;
1834 fh = fc_frame_header_get(fp);
1835 if (fh->fh_type != FC_TYPE_ELS)
1838 op = fc_frame_payload_op(fp);
1839 if (op == ELS_LS_ACC && fh->fh_r_ctl == FC_RCTL_ELS_REP &&
1840 fip->flogi_oxid == ntohs(fh->fh_ox_id)) {
1842 mutex_lock(&fip->ctlr_mutex);
1843 if (fip->state != FIP_ST_AUTO && fip->state != FIP_ST_NON_FIP) {
1844 mutex_unlock(&fip->ctlr_mutex);
1847 fcoe_ctlr_set_state(fip, FIP_ST_NON_FIP);
1848 LIBFCOE_FIP_DBG(fip,
1849 "received FLOGI LS_ACC using non-FIP mode\n");
1853 * If the src mac addr is FC_OUI-based, then we mark the
1854 * address_mode flag to use FC_OUI-based Ethernet DA.
1855 * Otherwise we use the FCoE gateway addr
1857 if (!compare_ether_addr(sa, (u8[6])FC_FCOE_FLOGI_MAC)) {
1858 fcoe_ctlr_map_dest(fip);
1860 memcpy(fip->dest_addr, sa, ETH_ALEN);
1863 fip->flogi_oxid = FC_XID_UNKNOWN;
1864 mutex_unlock(&fip->ctlr_mutex);
1865 fc_fcoe_set_mac(fr_cb(fp)->granted_mac, fh->fh_d_id);
1866 } else if (op == ELS_FLOGI && fh->fh_r_ctl == FC_RCTL_ELS_REQ && sa) {
1868 * Save source MAC for point-to-point responses.
1870 mutex_lock(&fip->ctlr_mutex);
1871 if (fip->state == FIP_ST_AUTO || fip->state == FIP_ST_NON_FIP) {
1872 memcpy(fip->dest_addr, sa, ETH_ALEN);
1874 if (fip->state == FIP_ST_AUTO)
1875 LIBFCOE_FIP_DBG(fip, "received non-FIP FLOGI. "
1876 "Setting non-FIP mode\n");
1877 fcoe_ctlr_set_state(fip, FIP_ST_NON_FIP);
1879 mutex_unlock(&fip->ctlr_mutex);
1883 EXPORT_SYMBOL(fcoe_ctlr_recv_flogi);
1886 * fcoe_wwn_from_mac() - Converts a 48-bit IEEE MAC address to a 64-bit FC WWN
1887 * @mac: The MAC address to convert
1888 * @scheme: The scheme to use when converting
1889 * @port: The port indicator for converting
1891 * Returns: u64 fc world wide name
1893 u64 fcoe_wwn_from_mac(unsigned char mac[MAX_ADDR_LEN],
1894 unsigned int scheme, unsigned int port)
1899 /* The MAC is in NO, so flip only the low 48 bits */
1900 host_mac = ((u64) mac[0] << 40) |
1901 ((u64) mac[1] << 32) |
1902 ((u64) mac[2] << 24) |
1903 ((u64) mac[3] << 16) |
1904 ((u64) mac[4] << 8) |
1907 WARN_ON(host_mac >= (1ULL << 48));
1908 wwn = host_mac | ((u64) scheme << 60);
1914 WARN_ON(port >= 0xfff);
1915 wwn |= (u64) port << 48;
1924 EXPORT_SYMBOL_GPL(fcoe_wwn_from_mac);
1927 * fcoe_ctlr_rport() - return the fcoe_rport for a given fc_rport_priv
1928 * @rdata: libfc remote port
1930 static inline struct fcoe_rport *fcoe_ctlr_rport(struct fc_rport_priv *rdata)
1932 return (struct fcoe_rport *)(rdata + 1);
1936 * fcoe_ctlr_vn_send() - Send a FIP VN2VN Probe Request or Reply.
1937 * @fip: The FCoE controller
1938 * @sub: sub-opcode for probe request, reply, or advertisement.
1939 * @dest: The destination Ethernet MAC address
1940 * @min_len: minimum size of the Ethernet payload to be sent
1942 static void fcoe_ctlr_vn_send(struct fcoe_ctlr *fip,
1943 enum fip_vn2vn_subcode sub,
1944 const u8 *dest, size_t min_len)
1946 struct sk_buff *skb;
1949 struct fip_header fip;
1950 struct fip_mac_desc mac;
1951 struct fip_wwn_desc wwnn;
1952 struct fip_vn_desc vn;
1954 struct fip_fc4_feat *ff;
1955 struct fip_size_desc *size;
1960 len = sizeof(*frame);
1962 if (sub == FIP_SC_VN_CLAIM_NOTIFY || sub == FIP_SC_VN_CLAIM_REP) {
1963 dlen = sizeof(struct fip_fc4_feat) +
1964 sizeof(struct fip_size_desc);
1967 dlen += sizeof(frame->mac) + sizeof(frame->wwnn) + sizeof(frame->vn);
1968 len = max(len, min_len + sizeof(struct ethhdr));
1970 skb = dev_alloc_skb(len);
1974 frame = (struct fip_frame *)skb->data;
1975 memset(frame, 0, len);
1976 memcpy(frame->eth.h_dest, dest, ETH_ALEN);
1978 if (sub == FIP_SC_VN_BEACON) {
1979 hton24(frame->eth.h_source, FIP_VN_FC_MAP);
1980 hton24(frame->eth.h_source + 3, fip->port_id);
1982 memcpy(frame->eth.h_source, fip->ctl_src_addr, ETH_ALEN);
1984 frame->eth.h_proto = htons(ETH_P_FIP);
1986 frame->fip.fip_ver = FIP_VER_ENCAPS(FIP_VER);
1987 frame->fip.fip_op = htons(FIP_OP_VN2VN);
1988 frame->fip.fip_subcode = sub;
1989 frame->fip.fip_dl_len = htons(dlen / FIP_BPW);
1991 frame->mac.fd_desc.fip_dtype = FIP_DT_MAC;
1992 frame->mac.fd_desc.fip_dlen = sizeof(frame->mac) / FIP_BPW;
1993 memcpy(frame->mac.fd_mac, fip->ctl_src_addr, ETH_ALEN);
1995 frame->wwnn.fd_desc.fip_dtype = FIP_DT_NAME;
1996 frame->wwnn.fd_desc.fip_dlen = sizeof(frame->wwnn) / FIP_BPW;
1997 put_unaligned_be64(fip->lp->wwnn, &frame->wwnn.fd_wwn);
1999 frame->vn.fd_desc.fip_dtype = FIP_DT_VN_ID;
2000 frame->vn.fd_desc.fip_dlen = sizeof(frame->vn) / FIP_BPW;
2001 hton24(frame->vn.fd_mac, FIP_VN_FC_MAP);
2002 hton24(frame->vn.fd_mac + 3, fip->port_id);
2003 hton24(frame->vn.fd_fc_id, fip->port_id);
2004 put_unaligned_be64(fip->lp->wwpn, &frame->vn.fd_wwpn);
2007 * For claims, add FC-4 features.
2008 * TBD: Add interface to get fc-4 types and features from libfc.
2010 if (sub == FIP_SC_VN_CLAIM_NOTIFY || sub == FIP_SC_VN_CLAIM_REP) {
2011 ff = (struct fip_fc4_feat *)(frame + 1);
2012 ff->fd_desc.fip_dtype = FIP_DT_FC4F;
2013 ff->fd_desc.fip_dlen = sizeof(*ff) / FIP_BPW;
2014 ff->fd_fts = fip->lp->fcts;
2017 if (fip->lp->service_params & FCP_SPPF_INIT_FCN)
2018 fcp_feat |= FCP_FEAT_INIT;
2019 if (fip->lp->service_params & FCP_SPPF_TARG_FCN)
2020 fcp_feat |= FCP_FEAT_TARG;
2021 fcp_feat <<= (FC_TYPE_FCP * 4) % 32;
2022 ff->fd_ff.fd_feat[FC_TYPE_FCP * 4 / 32] = htonl(fcp_feat);
2024 size = (struct fip_size_desc *)(ff + 1);
2025 size->fd_desc.fip_dtype = FIP_DT_FCOE_SIZE;
2026 size->fd_desc.fip_dlen = sizeof(*size) / FIP_BPW;
2027 size->fd_size = htons(fcoe_ctlr_fcoe_size(fip));
2031 skb->protocol = htons(ETH_P_FIP);
2032 skb->priority = fip->priority;
2033 skb_reset_mac_header(skb);
2034 skb_reset_network_header(skb);
2036 fip->send(fip, skb);
2040 * fcoe_ctlr_vn_rport_callback - Event handler for rport events.
2041 * @lport: The lport which is receiving the event
2042 * @rdata: remote port private data
2043 * @event: The event that occurred
2045 * Locking Note: The rport lock must not be held when calling this function.
2047 static void fcoe_ctlr_vn_rport_callback(struct fc_lport *lport,
2048 struct fc_rport_priv *rdata,
2049 enum fc_rport_event event)
2051 struct fcoe_ctlr *fip = lport->disc.priv;
2052 struct fcoe_rport *frport = fcoe_ctlr_rport(rdata);
2054 LIBFCOE_FIP_DBG(fip, "vn_rport_callback %x event %d\n",
2055 rdata->ids.port_id, event);
2057 mutex_lock(&fip->ctlr_mutex);
2059 case RPORT_EV_READY:
2060 frport->login_count = 0;
2063 case RPORT_EV_FAILED:
2065 frport->login_count++;
2066 if (frport->login_count > FCOE_CTLR_VN2VN_LOGIN_LIMIT) {
2067 LIBFCOE_FIP_DBG(fip,
2068 "rport FLOGI limited port_id %6.6x\n",
2069 rdata->ids.port_id);
2070 lport->tt.rport_logoff(rdata);
2076 mutex_unlock(&fip->ctlr_mutex);
2079 static struct fc_rport_operations fcoe_ctlr_vn_rport_ops = {
2080 .event_callback = fcoe_ctlr_vn_rport_callback,
2084 * fcoe_ctlr_disc_stop_locked() - stop discovery in VN2VN mode
2085 * @fip: The FCoE controller
2087 * Called with ctlr_mutex held.
2089 static void fcoe_ctlr_disc_stop_locked(struct fc_lport *lport)
2091 mutex_lock(&lport->disc.disc_mutex);
2092 lport->disc.disc_callback = NULL;
2093 mutex_unlock(&lport->disc.disc_mutex);
2097 * fcoe_ctlr_disc_stop() - stop discovery in VN2VN mode
2098 * @fip: The FCoE controller
2100 * Called through the local port template for discovery.
2101 * Called without the ctlr_mutex held.
2103 static void fcoe_ctlr_disc_stop(struct fc_lport *lport)
2105 struct fcoe_ctlr *fip = lport->disc.priv;
2107 mutex_lock(&fip->ctlr_mutex);
2108 fcoe_ctlr_disc_stop_locked(lport);
2109 mutex_unlock(&fip->ctlr_mutex);
2113 * fcoe_ctlr_disc_stop_final() - stop discovery for shutdown in VN2VN mode
2114 * @fip: The FCoE controller
2116 * Called through the local port template for discovery.
2117 * Called without the ctlr_mutex held.
2119 static void fcoe_ctlr_disc_stop_final(struct fc_lport *lport)
2121 fcoe_ctlr_disc_stop(lport);
2122 lport->tt.rport_flush_queue();
2127 * fcoe_ctlr_vn_restart() - VN2VN probe restart with new port_id
2128 * @fip: The FCoE controller
2130 * Called with fcoe_ctlr lock held.
2132 static void fcoe_ctlr_vn_restart(struct fcoe_ctlr *fip)
2137 fcoe_ctlr_disc_stop_locked(fip->lp);
2140 * Get proposed port ID.
2141 * If this is the first try after link up, use any previous port_id.
2142 * If there was none, use the low bits of the port_name.
2143 * On subsequent tries, get the next random one.
2144 * Don't use reserved IDs, use another non-zero value, just as random.
2146 port_id = fip->port_id;
2147 if (fip->probe_tries)
2148 port_id = prandom32(&fip->rnd_state) & 0xffff;
2150 port_id = fip->lp->wwpn & 0xffff;
2151 if (!port_id || port_id == 0xffff)
2153 fip->port_id = port_id;
2155 if (fip->probe_tries < FIP_VN_RLIM_COUNT) {
2157 wait = random32() % FIP_VN_PROBE_WAIT;
2159 wait = FIP_VN_RLIM_INT;
2160 mod_timer(&fip->timer, jiffies + msecs_to_jiffies(wait));
2161 fcoe_ctlr_set_state(fip, FIP_ST_VNMP_START);
2165 * fcoe_ctlr_vn_start() - Start in VN2VN mode
2166 * @fip: The FCoE controller
2168 * Called with fcoe_ctlr lock held.
2170 static void fcoe_ctlr_vn_start(struct fcoe_ctlr *fip)
2172 fip->probe_tries = 0;
2173 prandom32_seed(&fip->rnd_state, fip->lp->wwpn);
2174 fcoe_ctlr_vn_restart(fip);
2178 * fcoe_ctlr_vn_parse - parse probe request or response
2179 * @fip: The FCoE controller
2180 * @skb: incoming packet
2181 * @rdata: buffer for resulting parsed VN entry plus fcoe_rport
2183 * Returns non-zero error number on error.
2184 * Does not consume the packet.
2186 static int fcoe_ctlr_vn_parse(struct fcoe_ctlr *fip,
2187 struct sk_buff *skb,
2188 struct fc_rport_priv *rdata)
2190 struct fip_header *fiph;
2191 struct fip_desc *desc = NULL;
2192 struct fip_mac_desc *macd = NULL;
2193 struct fip_wwn_desc *wwn = NULL;
2194 struct fip_vn_desc *vn = NULL;
2195 struct fip_size_desc *size = NULL;
2196 struct fcoe_rport *frport;
2203 memset(rdata, 0, sizeof(*rdata) + sizeof(*frport));
2204 frport = fcoe_ctlr_rport(rdata);
2206 fiph = (struct fip_header *)skb->data;
2207 frport->flags = ntohs(fiph->fip_flags);
2209 sub = fiph->fip_subcode;
2211 case FIP_SC_VN_PROBE_REQ:
2212 case FIP_SC_VN_PROBE_REP:
2213 case FIP_SC_VN_BEACON:
2214 desc_mask = BIT(FIP_DT_MAC) | BIT(FIP_DT_NAME) |
2217 case FIP_SC_VN_CLAIM_NOTIFY:
2218 case FIP_SC_VN_CLAIM_REP:
2219 desc_mask = BIT(FIP_DT_MAC) | BIT(FIP_DT_NAME) |
2220 BIT(FIP_DT_VN_ID) | BIT(FIP_DT_FC4F) |
2221 BIT(FIP_DT_FCOE_SIZE);
2224 LIBFCOE_FIP_DBG(fip, "vn_parse unknown subcode %u\n", sub);
2228 rlen = ntohs(fiph->fip_dl_len) * 4;
2229 if (rlen + sizeof(*fiph) > skb->len)
2232 desc = (struct fip_desc *)(fiph + 1);
2234 dlen = desc->fip_dlen * FIP_BPW;
2235 if (dlen < sizeof(*desc) || dlen > rlen)
2238 dtype = desc->fip_dtype;
2240 if (!(desc_mask & BIT(dtype))) {
2241 LIBFCOE_FIP_DBG(fip,
2242 "unexpected or duplicated desc "
2244 "FIP VN2VN subtype %u\n",
2248 desc_mask &= ~BIT(dtype);
2253 if (dlen != sizeof(struct fip_mac_desc))
2255 macd = (struct fip_mac_desc *)desc;
2256 if (!is_valid_ether_addr(macd->fd_mac)) {
2257 LIBFCOE_FIP_DBG(fip,
2258 "Invalid MAC addr %pM in FIP VN2VN\n",
2262 memcpy(frport->enode_mac, macd->fd_mac, ETH_ALEN);
2265 if (dlen != sizeof(struct fip_wwn_desc))
2267 wwn = (struct fip_wwn_desc *)desc;
2268 rdata->ids.node_name = get_unaligned_be64(&wwn->fd_wwn);
2271 if (dlen != sizeof(struct fip_vn_desc))
2273 vn = (struct fip_vn_desc *)desc;
2274 memcpy(frport->vn_mac, vn->fd_mac, ETH_ALEN);
2275 rdata->ids.port_id = ntoh24(vn->fd_fc_id);
2276 rdata->ids.port_name = get_unaligned_be64(&vn->fd_wwpn);
2279 if (dlen != sizeof(struct fip_fc4_feat))
2282 case FIP_DT_FCOE_SIZE:
2283 if (dlen != sizeof(struct fip_size_desc))
2285 size = (struct fip_size_desc *)desc;
2286 frport->fcoe_len = ntohs(size->fd_size);
2289 LIBFCOE_FIP_DBG(fip, "unexpected descriptor type %x "
2290 "in FIP probe\n", dtype);
2291 /* standard says ignore unknown descriptors >= 128 */
2292 if (dtype < FIP_DT_VENDOR_BASE)
2296 desc = (struct fip_desc *)((char *)desc + dlen);
2302 LIBFCOE_FIP_DBG(fip, "FIP length error in descriptor type %x len %zu\n",
2308 * fcoe_ctlr_vn_send_claim() - send multicast FIP VN2VN Claim Notification.
2309 * @fip: The FCoE controller
2311 * Called with ctlr_mutex held.
2313 static void fcoe_ctlr_vn_send_claim(struct fcoe_ctlr *fip)
2315 fcoe_ctlr_vn_send(fip, FIP_SC_VN_CLAIM_NOTIFY, fcoe_all_vn2vn, 0);
2316 fip->sol_time = jiffies;
2320 * fcoe_ctlr_vn_probe_req() - handle incoming VN2VN probe request.
2321 * @fip: The FCoE controller
2322 * @rdata: parsed remote port with frport from the probe request
2324 * Called with ctlr_mutex held.
2326 static void fcoe_ctlr_vn_probe_req(struct fcoe_ctlr *fip,
2327 struct fc_rport_priv *rdata)
2329 struct fcoe_rport *frport = fcoe_ctlr_rport(rdata);
2331 if (rdata->ids.port_id != fip->port_id)
2334 switch (fip->state) {
2335 case FIP_ST_VNMP_CLAIM:
2336 case FIP_ST_VNMP_UP:
2337 fcoe_ctlr_vn_send(fip, FIP_SC_VN_PROBE_REP,
2338 frport->enode_mac, 0);
2340 case FIP_ST_VNMP_PROBE1:
2341 case FIP_ST_VNMP_PROBE2:
2343 * Decide whether to reply to the Probe.
2344 * Our selected address is never a "recorded" one, so
2345 * only reply if our WWPN is greater and the
2346 * Probe's REC bit is not set.
2347 * If we don't reply, we will change our address.
2349 if (fip->lp->wwpn > rdata->ids.port_name &&
2350 !(frport->flags & FIP_FL_REC_OR_P2P)) {
2351 fcoe_ctlr_vn_send(fip, FIP_SC_VN_PROBE_REP,
2352 frport->enode_mac, 0);
2356 case FIP_ST_VNMP_START:
2357 fcoe_ctlr_vn_restart(fip);
2365 * fcoe_ctlr_vn_probe_reply() - handle incoming VN2VN probe reply.
2366 * @fip: The FCoE controller
2367 * @rdata: parsed remote port with frport from the probe request
2369 * Called with ctlr_mutex held.
2371 static void fcoe_ctlr_vn_probe_reply(struct fcoe_ctlr *fip,
2372 struct fc_rport_priv *rdata)
2374 if (rdata->ids.port_id != fip->port_id)
2376 switch (fip->state) {
2377 case FIP_ST_VNMP_START:
2378 case FIP_ST_VNMP_PROBE1:
2379 case FIP_ST_VNMP_PROBE2:
2380 case FIP_ST_VNMP_CLAIM:
2381 fcoe_ctlr_vn_restart(fip);
2383 case FIP_ST_VNMP_UP:
2384 fcoe_ctlr_vn_send_claim(fip);
2392 * fcoe_ctlr_vn_add() - Add a VN2VN entry to the list, based on a claim reply.
2393 * @fip: The FCoE controller
2394 * @new: newly-parsed remote port with frport as a template for new rdata
2396 * Called with ctlr_mutex held.
2398 static void fcoe_ctlr_vn_add(struct fcoe_ctlr *fip, struct fc_rport_priv *new)
2400 struct fc_lport *lport = fip->lp;
2401 struct fc_rport_priv *rdata;
2402 struct fc_rport_identifiers *ids;
2403 struct fcoe_rport *frport;
2406 port_id = new->ids.port_id;
2407 if (port_id == fip->port_id)
2410 mutex_lock(&lport->disc.disc_mutex);
2411 rdata = lport->tt.rport_create(lport, port_id);
2413 mutex_unlock(&lport->disc.disc_mutex);
2417 rdata->ops = &fcoe_ctlr_vn_rport_ops;
2418 rdata->disc_id = lport->disc.disc_id;
2421 if ((ids->port_name != -1 && ids->port_name != new->ids.port_name) ||
2422 (ids->node_name != -1 && ids->node_name != new->ids.node_name))
2423 lport->tt.rport_logoff(rdata);
2424 ids->port_name = new->ids.port_name;
2425 ids->node_name = new->ids.node_name;
2426 mutex_unlock(&lport->disc.disc_mutex);
2428 frport = fcoe_ctlr_rport(rdata);
2429 LIBFCOE_FIP_DBG(fip, "vn_add rport %6.6x %s\n",
2430 port_id, frport->fcoe_len ? "old" : "new");
2431 *frport = *fcoe_ctlr_rport(new);
2436 * fcoe_ctlr_vn_lookup() - Find VN remote port's MAC address
2437 * @fip: The FCoE controller
2438 * @port_id: The port_id of the remote VN_node
2439 * @mac: buffer which will hold the VN_NODE destination MAC address, if found.
2441 * Returns non-zero error if no remote port found.
2443 static int fcoe_ctlr_vn_lookup(struct fcoe_ctlr *fip, u32 port_id, u8 *mac)
2445 struct fc_lport *lport = fip->lp;
2446 struct fc_rport_priv *rdata;
2447 struct fcoe_rport *frport;
2451 rdata = lport->tt.rport_lookup(lport, port_id);
2453 frport = fcoe_ctlr_rport(rdata);
2454 memcpy(mac, frport->enode_mac, ETH_ALEN);
2462 * fcoe_ctlr_vn_claim_notify() - handle received FIP VN2VN Claim Notification
2463 * @fip: The FCoE controller
2464 * @new: newly-parsed remote port with frport as a template for new rdata
2466 * Called with ctlr_mutex held.
2468 static void fcoe_ctlr_vn_claim_notify(struct fcoe_ctlr *fip,
2469 struct fc_rport_priv *new)
2471 struct fcoe_rport *frport = fcoe_ctlr_rport(new);
2473 if (frport->flags & FIP_FL_REC_OR_P2P) {
2474 fcoe_ctlr_vn_send(fip, FIP_SC_VN_PROBE_REQ, fcoe_all_vn2vn, 0);
2477 switch (fip->state) {
2478 case FIP_ST_VNMP_START:
2479 case FIP_ST_VNMP_PROBE1:
2480 case FIP_ST_VNMP_PROBE2:
2481 if (new->ids.port_id == fip->port_id)
2482 fcoe_ctlr_vn_restart(fip);
2484 case FIP_ST_VNMP_CLAIM:
2485 case FIP_ST_VNMP_UP:
2486 if (new->ids.port_id == fip->port_id) {
2487 if (new->ids.port_name > fip->lp->wwpn) {
2488 fcoe_ctlr_vn_restart(fip);
2491 fcoe_ctlr_vn_send_claim(fip);
2494 fcoe_ctlr_vn_send(fip, FIP_SC_VN_CLAIM_REP, frport->enode_mac,
2495 min((u32)frport->fcoe_len,
2496 fcoe_ctlr_fcoe_size(fip)));
2497 fcoe_ctlr_vn_add(fip, new);
2505 * fcoe_ctlr_vn_claim_resp() - handle received Claim Response
2506 * @fip: The FCoE controller that received the frame
2507 * @new: newly-parsed remote port with frport from the Claim Response
2509 * Called with ctlr_mutex held.
2511 static void fcoe_ctlr_vn_claim_resp(struct fcoe_ctlr *fip,
2512 struct fc_rport_priv *new)
2514 LIBFCOE_FIP_DBG(fip, "claim resp from from rport %x - state %s\n",
2515 new->ids.port_id, fcoe_ctlr_state(fip->state));
2516 if (fip->state == FIP_ST_VNMP_UP || fip->state == FIP_ST_VNMP_CLAIM)
2517 fcoe_ctlr_vn_add(fip, new);
2521 * fcoe_ctlr_vn_beacon() - handle received beacon.
2522 * @fip: The FCoE controller that received the frame
2523 * @new: newly-parsed remote port with frport from the Beacon
2525 * Called with ctlr_mutex held.
2527 static void fcoe_ctlr_vn_beacon(struct fcoe_ctlr *fip,
2528 struct fc_rport_priv *new)
2530 struct fc_lport *lport = fip->lp;
2531 struct fc_rport_priv *rdata;
2532 struct fcoe_rport *frport;
2534 frport = fcoe_ctlr_rport(new);
2535 if (frport->flags & FIP_FL_REC_OR_P2P) {
2536 fcoe_ctlr_vn_send(fip, FIP_SC_VN_PROBE_REQ, fcoe_all_vn2vn, 0);
2539 mutex_lock(&lport->disc.disc_mutex);
2540 rdata = lport->tt.rport_lookup(lport, new->ids.port_id);
2542 kref_get(&rdata->kref);
2543 mutex_unlock(&lport->disc.disc_mutex);
2545 if (rdata->ids.node_name == new->ids.node_name &&
2546 rdata->ids.port_name == new->ids.port_name) {
2547 frport = fcoe_ctlr_rport(rdata);
2548 if (!frport->time && fip->state == FIP_ST_VNMP_UP)
2549 lport->tt.rport_login(rdata);
2550 frport->time = jiffies;
2552 kref_put(&rdata->kref, lport->tt.rport_destroy);
2555 if (fip->state != FIP_ST_VNMP_UP)
2559 * Beacon from a new neighbor.
2560 * Send a claim notify if one hasn't been sent recently.
2561 * Don't add the neighbor yet.
2563 LIBFCOE_FIP_DBG(fip, "beacon from new rport %x. sending claim notify\n",
2565 if (time_after(jiffies,
2566 fip->sol_time + msecs_to_jiffies(FIP_VN_ANN_WAIT)))
2567 fcoe_ctlr_vn_send_claim(fip);
2571 * fcoe_ctlr_vn_age() - Check for VN_ports without recent beacons
2572 * @fip: The FCoE controller
2574 * Called with ctlr_mutex held.
2575 * Called only in state FIP_ST_VNMP_UP.
2576 * Returns the soonest time for next age-out or a time far in the future.
2578 static unsigned long fcoe_ctlr_vn_age(struct fcoe_ctlr *fip)
2580 struct fc_lport *lport = fip->lp;
2581 struct fc_rport_priv *rdata;
2582 struct fcoe_rport *frport;
2583 unsigned long next_time;
2584 unsigned long deadline;
2586 next_time = jiffies + msecs_to_jiffies(FIP_VN_BEACON_INT * 10);
2587 mutex_lock(&lport->disc.disc_mutex);
2588 list_for_each_entry_rcu(rdata, &lport->disc.rports, peers) {
2589 frport = fcoe_ctlr_rport(rdata);
2592 deadline = frport->time +
2593 msecs_to_jiffies(FIP_VN_BEACON_INT * 25 / 10);
2594 if (time_after_eq(jiffies, deadline)) {
2596 LIBFCOE_FIP_DBG(fip,
2597 "port %16.16llx fc_id %6.6x beacon expired\n",
2598 rdata->ids.port_name, rdata->ids.port_id);
2599 lport->tt.rport_logoff(rdata);
2600 } else if (time_before(deadline, next_time))
2601 next_time = deadline;
2603 mutex_unlock(&lport->disc.disc_mutex);
2608 * fcoe_ctlr_vn_recv() - Receive a FIP frame
2609 * @fip: The FCoE controller that received the frame
2610 * @skb: The received FIP frame
2612 * Returns non-zero if the frame is dropped.
2613 * Always consumes the frame.
2615 static int fcoe_ctlr_vn_recv(struct fcoe_ctlr *fip, struct sk_buff *skb)
2617 struct fip_header *fiph;
2618 enum fip_vn2vn_subcode sub;
2620 struct fc_rport_priv rdata;
2621 struct fcoe_rport frport;
2625 fiph = (struct fip_header *)skb->data;
2626 sub = fiph->fip_subcode;
2628 rc = fcoe_ctlr_vn_parse(fip, skb, &buf.rdata);
2630 LIBFCOE_FIP_DBG(fip, "vn_recv vn_parse error %d\n", rc);
2634 mutex_lock(&fip->ctlr_mutex);
2636 case FIP_SC_VN_PROBE_REQ:
2637 fcoe_ctlr_vn_probe_req(fip, &buf.rdata);
2639 case FIP_SC_VN_PROBE_REP:
2640 fcoe_ctlr_vn_probe_reply(fip, &buf.rdata);
2642 case FIP_SC_VN_CLAIM_NOTIFY:
2643 fcoe_ctlr_vn_claim_notify(fip, &buf.rdata);
2645 case FIP_SC_VN_CLAIM_REP:
2646 fcoe_ctlr_vn_claim_resp(fip, &buf.rdata);
2648 case FIP_SC_VN_BEACON:
2649 fcoe_ctlr_vn_beacon(fip, &buf.rdata);
2652 LIBFCOE_FIP_DBG(fip, "vn_recv unknown subcode %d\n", sub);
2656 mutex_unlock(&fip->ctlr_mutex);
2663 * fcoe_ctlr_disc_recv - discovery receive handler for VN2VN mode.
2664 * @lport: The local port
2665 * @fp: The received frame
2667 * This should never be called since we don't see RSCNs or other
2668 * fabric-generated ELSes.
2670 static void fcoe_ctlr_disc_recv(struct fc_lport *lport, struct fc_frame *fp)
2672 struct fc_seq_els_data rjt_data;
2674 rjt_data.reason = ELS_RJT_UNSUP;
2675 rjt_data.explan = ELS_EXPL_NONE;
2676 lport->tt.seq_els_rsp_send(fp, ELS_LS_RJT, &rjt_data);
2681 * fcoe_ctlr_disc_recv - start discovery for VN2VN mode.
2682 * @fip: The FCoE controller
2684 * This sets a flag indicating that remote ports should be created
2685 * and started for the peers we discover. We use the disc_callback
2686 * pointer as that flag. Peers already discovered are created here.
2688 * The lport lock is held during this call. The callback must be done
2689 * later, without holding either the lport or discovery locks.
2690 * The fcoe_ctlr lock may also be held during this call.
2692 static void fcoe_ctlr_disc_start(void (*callback)(struct fc_lport *,
2693 enum fc_disc_event),
2694 struct fc_lport *lport)
2696 struct fc_disc *disc = &lport->disc;
2697 struct fcoe_ctlr *fip = disc->priv;
2699 mutex_lock(&disc->disc_mutex);
2700 disc->disc_callback = callback;
2701 disc->disc_id = (disc->disc_id + 2) | 1;
2703 schedule_work(&fip->timer_work);
2704 mutex_unlock(&disc->disc_mutex);
2708 * fcoe_ctlr_vn_disc() - report FIP VN_port discovery results after claim state.
2709 * @fip: The FCoE controller
2711 * Starts the FLOGI and PLOGI login process to each discovered rport for which
2712 * we've received at least one beacon.
2713 * Performs the discovery complete callback.
2715 static void fcoe_ctlr_vn_disc(struct fcoe_ctlr *fip)
2717 struct fc_lport *lport = fip->lp;
2718 struct fc_disc *disc = &lport->disc;
2719 struct fc_rport_priv *rdata;
2720 struct fcoe_rport *frport;
2721 void (*callback)(struct fc_lport *, enum fc_disc_event);
2723 mutex_lock(&disc->disc_mutex);
2724 callback = disc->pending ? disc->disc_callback : NULL;
2726 list_for_each_entry_rcu(rdata, &disc->rports, peers) {
2727 frport = fcoe_ctlr_rport(rdata);
2729 lport->tt.rport_login(rdata);
2731 mutex_unlock(&disc->disc_mutex);
2733 callback(lport, DISC_EV_SUCCESS);
2737 * fcoe_ctlr_vn_timeout - timer work function for VN2VN mode.
2738 * @fip: The FCoE controller
2740 static void fcoe_ctlr_vn_timeout(struct fcoe_ctlr *fip)
2742 unsigned long next_time;
2744 u32 new_port_id = 0;
2746 mutex_lock(&fip->ctlr_mutex);
2747 switch (fip->state) {
2748 case FIP_ST_VNMP_START:
2749 fcoe_ctlr_set_state(fip, FIP_ST_VNMP_PROBE1);
2750 fcoe_ctlr_vn_send(fip, FIP_SC_VN_PROBE_REQ, fcoe_all_vn2vn, 0);
2751 next_time = jiffies + msecs_to_jiffies(FIP_VN_PROBE_WAIT);
2753 case FIP_ST_VNMP_PROBE1:
2754 fcoe_ctlr_set_state(fip, FIP_ST_VNMP_PROBE2);
2755 fcoe_ctlr_vn_send(fip, FIP_SC_VN_PROBE_REQ, fcoe_all_vn2vn, 0);
2756 next_time = jiffies + msecs_to_jiffies(FIP_VN_ANN_WAIT);
2758 case FIP_ST_VNMP_PROBE2:
2759 fcoe_ctlr_set_state(fip, FIP_ST_VNMP_CLAIM);
2760 new_port_id = fip->port_id;
2761 hton24(mac, FIP_VN_FC_MAP);
2762 hton24(mac + 3, new_port_id);
2763 fcoe_ctlr_map_dest(fip);
2764 fip->update_mac(fip->lp, mac);
2765 fcoe_ctlr_vn_send_claim(fip);
2766 next_time = jiffies + msecs_to_jiffies(FIP_VN_ANN_WAIT);
2768 case FIP_ST_VNMP_CLAIM:
2770 * This may be invoked either by starting discovery so don't
2771 * go to the next state unless it's been long enough.
2773 next_time = fip->sol_time + msecs_to_jiffies(FIP_VN_ANN_WAIT);
2774 if (time_after_eq(jiffies, next_time)) {
2775 fcoe_ctlr_set_state(fip, FIP_ST_VNMP_UP);
2776 fcoe_ctlr_vn_send(fip, FIP_SC_VN_BEACON,
2778 next_time = jiffies + msecs_to_jiffies(FIP_VN_ANN_WAIT);
2779 fip->port_ka_time = next_time;
2781 fcoe_ctlr_vn_disc(fip);
2783 case FIP_ST_VNMP_UP:
2784 next_time = fcoe_ctlr_vn_age(fip);
2785 if (time_after_eq(jiffies, fip->port_ka_time)) {
2786 fcoe_ctlr_vn_send(fip, FIP_SC_VN_BEACON,
2788 fip->port_ka_time = jiffies +
2789 msecs_to_jiffies(FIP_VN_BEACON_INT +
2790 (random32() % FIP_VN_BEACON_FUZZ));
2792 if (time_before(fip->port_ka_time, next_time))
2793 next_time = fip->port_ka_time;
2795 case FIP_ST_LINK_WAIT:
2798 WARN(1, "unexpected state %d\n", fip->state);
2801 mod_timer(&fip->timer, next_time);
2803 mutex_unlock(&fip->ctlr_mutex);
2805 /* If port ID is new, notify local port after dropping ctlr_mutex */
2807 fc_lport_set_local_id(fip->lp, new_port_id);
2811 * fcoe_libfc_config() - Sets up libfc related properties for local port
2812 * @lport: The local port to configure libfc for
2813 * @fip: The FCoE controller in use by the local port
2814 * @tt: The libfc function template
2815 * @init_fcp: If non-zero, the FCP portion of libfc should be initialized
2817 * Returns : 0 for success
2819 int fcoe_libfc_config(struct fc_lport *lport, struct fcoe_ctlr *fip,
2820 const struct libfc_function_template *tt, int init_fcp)
2822 /* Set the function pointers set by the LLDD */
2823 memcpy(&lport->tt, tt, sizeof(*tt));
2824 if (init_fcp && fc_fcp_init(lport))
2826 fc_exch_init(lport);
2827 fc_elsct_init(lport);
2828 fc_lport_init(lport);
2829 if (fip->mode == FIP_MODE_VN2VN)
2830 lport->rport_priv_size = sizeof(struct fcoe_rport);
2831 fc_rport_init(lport);
2832 if (fip->mode == FIP_MODE_VN2VN) {
2833 lport->point_to_multipoint = 1;
2834 lport->tt.disc_recv_req = fcoe_ctlr_disc_recv;
2835 lport->tt.disc_start = fcoe_ctlr_disc_start;
2836 lport->tt.disc_stop = fcoe_ctlr_disc_stop;
2837 lport->tt.disc_stop_final = fcoe_ctlr_disc_stop_final;
2838 mutex_init(&lport->disc.disc_mutex);
2839 INIT_LIST_HEAD(&lport->disc.rports);
2840 lport->disc.priv = fip;
2842 fc_disc_init(lport);
2846 EXPORT_SYMBOL_GPL(fcoe_libfc_config);
2848 void fcoe_fcf_get_selected(struct fcoe_fcf_device *fcf_dev)
2850 struct fcoe_ctlr_device *ctlr_dev = fcoe_fcf_dev_to_ctlr_dev(fcf_dev);
2851 struct fcoe_ctlr *fip = fcoe_ctlr_device_priv(ctlr_dev);
2852 struct fcoe_fcf *fcf;
2854 mutex_lock(&fip->ctlr_mutex);
2855 mutex_lock(&ctlr_dev->lock);
2857 fcf = fcoe_fcf_device_priv(fcf_dev);
2859 fcf_dev->selected = (fcf == fip->sel_fcf) ? 1 : 0;
2861 fcf_dev->selected = 0;
2863 mutex_unlock(&ctlr_dev->lock);
2864 mutex_unlock(&fip->ctlr_mutex);
2866 EXPORT_SYMBOL(fcoe_fcf_get_selected);
2868 void fcoe_ctlr_get_fip_mode(struct fcoe_ctlr_device *ctlr_dev)
2870 struct fcoe_ctlr *ctlr = fcoe_ctlr_device_priv(ctlr_dev);
2872 mutex_lock(&ctlr->ctlr_mutex);
2873 switch (ctlr->mode) {
2874 case FIP_MODE_FABRIC:
2875 ctlr_dev->mode = FIP_CONN_TYPE_FABRIC;
2877 case FIP_MODE_VN2VN:
2878 ctlr_dev->mode = FIP_CONN_TYPE_VN2VN;
2881 ctlr_dev->mode = FIP_CONN_TYPE_UNKNOWN;
2884 mutex_unlock(&ctlr->ctlr_mutex);
2886 EXPORT_SYMBOL(fcoe_ctlr_get_fip_mode);