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);
164 * fcoe_ctlr_reset_fcfs() - Reset and free all FCFs for a controller
165 * @fip: The FCoE controller whose FCFs are to be reset
167 * Called with &fcoe_ctlr lock held.
169 static void fcoe_ctlr_reset_fcfs(struct fcoe_ctlr *fip)
171 struct fcoe_fcf *fcf;
172 struct fcoe_fcf *next;
175 list_for_each_entry_safe(fcf, next, &fip->fcfs, list) {
176 list_del(&fcf->list);
184 * fcoe_ctlr_destroy() - Disable and tear down a FCoE controller
185 * @fip: The FCoE controller to tear down
187 * This is called by FCoE drivers before freeing the &fcoe_ctlr.
189 * The receive handler will have been deleted before this to guarantee
190 * that no more recv_work will be scheduled.
192 * The timer routine will simply return once we set FIP_ST_DISABLED.
193 * This guarantees that no further timeouts or work will be scheduled.
195 void fcoe_ctlr_destroy(struct fcoe_ctlr *fip)
197 cancel_work_sync(&fip->recv_work);
198 skb_queue_purge(&fip->fip_recv_list);
200 mutex_lock(&fip->ctlr_mutex);
201 fcoe_ctlr_set_state(fip, FIP_ST_DISABLED);
202 fcoe_ctlr_reset_fcfs(fip);
203 mutex_unlock(&fip->ctlr_mutex);
204 del_timer_sync(&fip->timer);
205 cancel_work_sync(&fip->timer_work);
207 EXPORT_SYMBOL(fcoe_ctlr_destroy);
210 * fcoe_ctlr_announce() - announce new FCF selection
211 * @fip: The FCoE controller
213 * Also sets the destination MAC for FCoE and control packets
215 * Called with neither ctlr_mutex nor ctlr_lock held.
217 static void fcoe_ctlr_announce(struct fcoe_ctlr *fip)
219 struct fcoe_fcf *sel;
220 struct fcoe_fcf *fcf;
222 mutex_lock(&fip->ctlr_mutex);
223 spin_lock_bh(&fip->ctlr_lock);
225 kfree_skb(fip->flogi_req);
226 fip->flogi_req = NULL;
227 list_for_each_entry(fcf, &fip->fcfs, list)
230 spin_unlock_bh(&fip->ctlr_lock);
233 if (sel && !compare_ether_addr(sel->fcf_mac, fip->dest_addr))
235 if (!is_zero_ether_addr(fip->dest_addr)) {
236 printk(KERN_NOTICE "libfcoe: host%d: "
237 "FIP Fibre-Channel Forwarder MAC %pM deselected\n",
238 fip->lp->host->host_no, fip->dest_addr);
239 memset(fip->dest_addr, 0, ETH_ALEN);
242 printk(KERN_INFO "libfcoe: host%d: FIP selected "
243 "Fibre-Channel Forwarder MAC %pM\n",
244 fip->lp->host->host_no, sel->fcf_mac);
245 memcpy(fip->dest_addr, sel->fcoe_mac, ETH_ALEN);
249 mutex_unlock(&fip->ctlr_mutex);
253 * fcoe_ctlr_fcoe_size() - Return the maximum FCoE size required for VN_Port
254 * @fip: The FCoE controller to get the maximum FCoE size from
256 * Returns the maximum packet size including the FCoE header and trailer,
257 * but not including any Ethernet or VLAN headers.
259 static inline u32 fcoe_ctlr_fcoe_size(struct fcoe_ctlr *fip)
262 * Determine the max FCoE frame size allowed, including
263 * FCoE header and trailer.
264 * Note: lp->mfs is currently the payload size, not the frame size.
266 return fip->lp->mfs + sizeof(struct fc_frame_header) +
267 sizeof(struct fcoe_hdr) + sizeof(struct fcoe_crc_eof);
271 * fcoe_ctlr_solicit() - Send a FIP solicitation
272 * @fip: The FCoE controller to send the solicitation on
273 * @fcf: The destination FCF (if NULL, a multicast solicitation is sent)
275 static void fcoe_ctlr_solicit(struct fcoe_ctlr *fip, struct fcoe_fcf *fcf)
280 struct fip_header fip;
282 struct fip_mac_desc mac;
283 struct fip_wwn_desc wwnn;
284 struct fip_size_desc size;
289 skb = dev_alloc_skb(sizeof(*sol));
293 sol = (struct fip_sol *)skb->data;
295 memset(sol, 0, sizeof(*sol));
296 memcpy(sol->eth.h_dest, fcf ? fcf->fcf_mac : fcoe_all_fcfs, ETH_ALEN);
297 memcpy(sol->eth.h_source, fip->ctl_src_addr, ETH_ALEN);
298 sol->eth.h_proto = htons(ETH_P_FIP);
300 sol->fip.fip_ver = FIP_VER_ENCAPS(FIP_VER);
301 sol->fip.fip_op = htons(FIP_OP_DISC);
302 sol->fip.fip_subcode = FIP_SC_SOL;
303 sol->fip.fip_dl_len = htons(sizeof(sol->desc) / FIP_BPW);
304 sol->fip.fip_flags = htons(FIP_FL_FPMA);
306 sol->fip.fip_flags |= htons(FIP_FL_SPMA);
308 sol->desc.mac.fd_desc.fip_dtype = FIP_DT_MAC;
309 sol->desc.mac.fd_desc.fip_dlen = sizeof(sol->desc.mac) / FIP_BPW;
310 memcpy(sol->desc.mac.fd_mac, fip->ctl_src_addr, ETH_ALEN);
312 sol->desc.wwnn.fd_desc.fip_dtype = FIP_DT_NAME;
313 sol->desc.wwnn.fd_desc.fip_dlen = sizeof(sol->desc.wwnn) / FIP_BPW;
314 put_unaligned_be64(fip->lp->wwnn, &sol->desc.wwnn.fd_wwn);
316 fcoe_size = fcoe_ctlr_fcoe_size(fip);
317 sol->desc.size.fd_desc.fip_dtype = FIP_DT_FCOE_SIZE;
318 sol->desc.size.fd_desc.fip_dlen = sizeof(sol->desc.size) / FIP_BPW;
319 sol->desc.size.fd_size = htons(fcoe_size);
321 skb_put(skb, sizeof(*sol));
322 skb->protocol = htons(ETH_P_FIP);
323 skb->priority = fip->priority;
324 skb_reset_mac_header(skb);
325 skb_reset_network_header(skb);
329 fip->sol_time = jiffies;
333 * fcoe_ctlr_link_up() - Start FCoE controller
334 * @fip: The FCoE controller to start
336 * Called from the LLD when the network link is ready.
338 void fcoe_ctlr_link_up(struct fcoe_ctlr *fip)
340 mutex_lock(&fip->ctlr_mutex);
341 if (fip->state == FIP_ST_NON_FIP || fip->state == FIP_ST_AUTO) {
342 mutex_unlock(&fip->ctlr_mutex);
344 } else if (fip->state == FIP_ST_LINK_WAIT) {
345 fcoe_ctlr_set_state(fip, fip->mode);
348 LIBFCOE_FIP_DBG(fip, "invalid mode %d\n", fip->mode);
351 LIBFCOE_FIP_DBG(fip, "%s", "setting AUTO mode.\n");
353 case FIP_MODE_FABRIC:
354 case FIP_MODE_NON_FIP:
355 mutex_unlock(&fip->ctlr_mutex);
357 fcoe_ctlr_solicit(fip, NULL);
360 fcoe_ctlr_vn_start(fip);
361 mutex_unlock(&fip->ctlr_mutex);
366 mutex_unlock(&fip->ctlr_mutex);
368 EXPORT_SYMBOL(fcoe_ctlr_link_up);
371 * fcoe_ctlr_reset() - Reset a FCoE controller
372 * @fip: The FCoE controller to reset
374 static void fcoe_ctlr_reset(struct fcoe_ctlr *fip)
376 fcoe_ctlr_reset_fcfs(fip);
377 del_timer(&fip->timer);
378 fip->ctlr_ka_time = 0;
379 fip->port_ka_time = 0;
381 fip->flogi_oxid = FC_XID_UNKNOWN;
382 fcoe_ctlr_map_dest(fip);
386 * fcoe_ctlr_link_down() - Stop a FCoE controller
387 * @fip: The FCoE controller to be stopped
389 * Returns non-zero if the link was up and now isn't.
391 * Called from the LLD when the network link is not ready.
392 * There may be multiple calls while the link is down.
394 int fcoe_ctlr_link_down(struct fcoe_ctlr *fip)
398 LIBFCOE_FIP_DBG(fip, "link down.\n");
399 mutex_lock(&fip->ctlr_mutex);
400 fcoe_ctlr_reset(fip);
401 link_dropped = fip->state != FIP_ST_LINK_WAIT;
402 fcoe_ctlr_set_state(fip, FIP_ST_LINK_WAIT);
403 mutex_unlock(&fip->ctlr_mutex);
406 fc_linkdown(fip->lp);
409 EXPORT_SYMBOL(fcoe_ctlr_link_down);
412 * fcoe_ctlr_send_keep_alive() - Send a keep-alive to the selected FCF
413 * @fip: The FCoE controller to send the FKA on
414 * @lport: libfc fc_lport to send from
415 * @ports: 0 for controller keep-alive, 1 for port keep-alive
416 * @sa: The source MAC address
418 * A controller keep-alive is sent every fka_period (typically 8 seconds).
419 * The source MAC is the native MAC address.
421 * A port keep-alive is sent every 90 seconds while logged in.
422 * The source MAC is the assigned mapped source address.
423 * The destination is the FCF's F-port.
425 static void fcoe_ctlr_send_keep_alive(struct fcoe_ctlr *fip,
426 struct fc_lport *lport,
432 struct fip_header fip;
433 struct fip_mac_desc mac;
435 struct fip_vn_desc *vn;
438 struct fcoe_fcf *fcf;
442 if (!fcf || (ports && !lp->port_id))
445 len = sizeof(*kal) + ports * sizeof(*vn);
446 skb = dev_alloc_skb(len);
450 kal = (struct fip_kal *)skb->data;
452 memcpy(kal->eth.h_dest, fcf->fcf_mac, ETH_ALEN);
453 memcpy(kal->eth.h_source, sa, ETH_ALEN);
454 kal->eth.h_proto = htons(ETH_P_FIP);
456 kal->fip.fip_ver = FIP_VER_ENCAPS(FIP_VER);
457 kal->fip.fip_op = htons(FIP_OP_CTRL);
458 kal->fip.fip_subcode = FIP_SC_KEEP_ALIVE;
459 kal->fip.fip_dl_len = htons((sizeof(kal->mac) +
460 ports * sizeof(*vn)) / FIP_BPW);
461 kal->fip.fip_flags = htons(FIP_FL_FPMA);
463 kal->fip.fip_flags |= htons(FIP_FL_SPMA);
465 kal->mac.fd_desc.fip_dtype = FIP_DT_MAC;
466 kal->mac.fd_desc.fip_dlen = sizeof(kal->mac) / FIP_BPW;
467 memcpy(kal->mac.fd_mac, fip->ctl_src_addr, ETH_ALEN);
469 vn = (struct fip_vn_desc *)(kal + 1);
470 vn->fd_desc.fip_dtype = FIP_DT_VN_ID;
471 vn->fd_desc.fip_dlen = sizeof(*vn) / FIP_BPW;
472 memcpy(vn->fd_mac, fip->get_src_addr(lport), ETH_ALEN);
473 hton24(vn->fd_fc_id, lport->port_id);
474 put_unaligned_be64(lport->wwpn, &vn->fd_wwpn);
477 skb->protocol = htons(ETH_P_FIP);
478 skb->priority = fip->priority;
479 skb_reset_mac_header(skb);
480 skb_reset_network_header(skb);
485 * fcoe_ctlr_encaps() - Encapsulate an ELS frame for FIP, without sending it
486 * @fip: The FCoE controller for the ELS frame
487 * @dtype: The FIP descriptor type for the frame
488 * @skb: The FCoE ELS frame including FC header but no FCoE headers
489 * @d_id: The destination port ID.
491 * Returns non-zero error code on failure.
493 * The caller must check that the length is a multiple of 4.
495 * The @skb must have enough headroom (28 bytes) and tailroom (8 bytes).
496 * Headroom includes the FIP encapsulation description, FIP header, and
497 * Ethernet header. The tailroom is for the FIP MAC descriptor.
499 static int fcoe_ctlr_encaps(struct fcoe_ctlr *fip, struct fc_lport *lport,
500 u8 dtype, struct sk_buff *skb, u32 d_id)
502 struct fip_encaps_head {
504 struct fip_header fip;
505 struct fip_encaps encaps;
507 struct fc_frame_header *fh;
508 struct fip_mac_desc *mac;
509 struct fcoe_fcf *fcf;
514 fh = (struct fc_frame_header *)skb->data;
515 op = *(u8 *)(fh + 1);
516 dlen = sizeof(struct fip_encaps) + skb->len; /* len before push */
517 cap = (struct fip_encaps_head *)skb_push(skb, sizeof(*cap));
518 memset(cap, 0, sizeof(*cap));
520 if (lport->point_to_multipoint) {
521 if (fcoe_ctlr_vn_lookup(fip, d_id, cap->eth.h_dest))
528 fip_flags = fcf->flags;
529 fip_flags &= fip->spma ? FIP_FL_SPMA | FIP_FL_FPMA :
533 memcpy(cap->eth.h_dest, fcf->fcf_mac, ETH_ALEN);
535 memcpy(cap->eth.h_source, fip->ctl_src_addr, ETH_ALEN);
536 cap->eth.h_proto = htons(ETH_P_FIP);
538 cap->fip.fip_ver = FIP_VER_ENCAPS(FIP_VER);
539 cap->fip.fip_op = htons(FIP_OP_LS);
540 if (op == ELS_LS_ACC || op == ELS_LS_RJT)
541 cap->fip.fip_subcode = FIP_SC_REP;
543 cap->fip.fip_subcode = FIP_SC_REQ;
544 cap->fip.fip_flags = htons(fip_flags);
546 cap->encaps.fd_desc.fip_dtype = dtype;
547 cap->encaps.fd_desc.fip_dlen = dlen / FIP_BPW;
549 if (op != ELS_LS_RJT) {
550 dlen += sizeof(*mac);
551 mac = (struct fip_mac_desc *)skb_put(skb, sizeof(*mac));
552 memset(mac, 0, sizeof(*mac));
553 mac->fd_desc.fip_dtype = FIP_DT_MAC;
554 mac->fd_desc.fip_dlen = sizeof(*mac) / FIP_BPW;
555 if (dtype != FIP_DT_FLOGI && dtype != FIP_DT_FDISC) {
556 memcpy(mac->fd_mac, fip->get_src_addr(lport), ETH_ALEN);
557 } else if (fip->mode == FIP_MODE_VN2VN) {
558 hton24(mac->fd_mac, FIP_VN_FC_MAP);
559 hton24(mac->fd_mac + 3, fip->port_id);
560 } else if (fip_flags & FIP_FL_SPMA) {
561 LIBFCOE_FIP_DBG(fip, "FLOGI/FDISC sent with SPMA\n");
562 memcpy(mac->fd_mac, fip->ctl_src_addr, ETH_ALEN);
564 LIBFCOE_FIP_DBG(fip, "FLOGI/FDISC sent with FPMA\n");
565 /* FPMA only FLOGI. Must leave the MAC desc zeroed. */
568 cap->fip.fip_dl_len = htons(dlen / FIP_BPW);
570 skb->protocol = htons(ETH_P_FIP);
571 skb->priority = fip->priority;
572 skb_reset_mac_header(skb);
573 skb_reset_network_header(skb);
578 * fcoe_ctlr_els_send() - Send an ELS frame encapsulated by FIP if appropriate.
579 * @fip: FCoE controller.
580 * @lport: libfc fc_lport to send from
581 * @skb: FCoE ELS frame including FC header but no FCoE headers.
583 * Returns a non-zero error code if the frame should not be sent.
584 * Returns zero if the caller should send the frame with FCoE encapsulation.
586 * The caller must check that the length is a multiple of 4.
587 * The SKB must have enough headroom (28 bytes) and tailroom (8 bytes).
588 * The the skb must also be an fc_frame.
590 * This is called from the lower-level driver with spinlocks held,
591 * so we must not take a mutex here.
593 int fcoe_ctlr_els_send(struct fcoe_ctlr *fip, struct fc_lport *lport,
597 struct fc_frame_header *fh;
602 fp = container_of(skb, struct fc_frame, skb);
603 fh = (struct fc_frame_header *)skb->data;
604 op = *(u8 *)(fh + 1);
606 if (op == ELS_FLOGI && fip->mode != FIP_MODE_VN2VN) {
607 old_xid = fip->flogi_oxid;
608 fip->flogi_oxid = ntohs(fh->fh_ox_id);
609 if (fip->state == FIP_ST_AUTO) {
610 if (old_xid == FC_XID_UNKNOWN)
611 fip->flogi_count = 0;
613 if (fip->flogi_count < 3)
615 fcoe_ctlr_map_dest(fip);
618 if (fip->state == FIP_ST_NON_FIP)
619 fcoe_ctlr_map_dest(fip);
622 if (fip->state == FIP_ST_NON_FIP)
624 if (!fip->sel_fcf && fip->mode != FIP_MODE_VN2VN)
629 if (fip->mode == FIP_MODE_VN2VN)
631 spin_lock_bh(&fip->ctlr_lock);
632 kfree_skb(fip->flogi_req);
633 fip->flogi_req = skb;
634 fip->flogi_req_send = 1;
635 spin_unlock_bh(&fip->ctlr_lock);
636 schedule_work(&fip->timer_work);
639 if (ntoh24(fh->fh_s_id))
644 if (fip->mode == FIP_MODE_VN2VN) {
645 if (fip->state != FIP_ST_VNMP_UP)
647 if (ntoh24(fh->fh_d_id) == FC_FID_FLOGI)
650 if (fip->state != FIP_ST_ENABLED)
652 if (ntoh24(fh->fh_d_id) != FC_FID_FLOGI)
659 * If non-FIP, we may have gotten an SID by accepting an FLOGI
660 * from a point-to-point connection. Switch to using
661 * the source mac based on the SID. The destination
662 * MAC in this case would have been set by receiving the
665 if (fip->state == FIP_ST_NON_FIP) {
666 if (fip->flogi_oxid == FC_XID_UNKNOWN)
668 fip->flogi_oxid = FC_XID_UNKNOWN;
669 fc_fcoe_set_mac(mac, fh->fh_d_id);
670 fip->update_mac(lport, mac);
679 if (fip->state != FIP_ST_ENABLED &&
680 fip->state != FIP_ST_VNMP_UP)
684 LIBFCOE_FIP_DBG(fip, "els_send op %u d_id %x\n",
685 op, ntoh24(fh->fh_d_id));
686 if (fcoe_ctlr_encaps(fip, lport, op, skb, ntoh24(fh->fh_d_id)))
694 EXPORT_SYMBOL(fcoe_ctlr_els_send);
697 * fcoe_ctlr_age_fcfs() - Reset and free all old FCFs for a controller
698 * @fip: The FCoE controller to free FCFs on
700 * Called with lock held and preemption disabled.
702 * An FCF is considered old if we have missed two advertisements.
703 * That is, there have been no valid advertisement from it for 2.5
704 * times its keep-alive period.
706 * In addition, determine the time when an FCF selection can occur.
708 * Also, increment the MissDiscAdvCount when no advertisement is received
709 * for the corresponding FCF for 1.5 * FKA_ADV_PERIOD (FC-BB-5 LESB).
711 * Returns the time in jiffies for the next call.
713 static unsigned long fcoe_ctlr_age_fcfs(struct fcoe_ctlr *fip)
715 struct fcoe_fcf *fcf;
716 struct fcoe_fcf *next;
717 unsigned long next_timer = jiffies + msecs_to_jiffies(FIP_VN_KA_PERIOD);
718 unsigned long deadline;
719 unsigned long sel_time = 0;
720 struct fcoe_dev_stats *stats;
722 stats = per_cpu_ptr(fip->lp->dev_stats, get_cpu());
724 list_for_each_entry_safe(fcf, next, &fip->fcfs, list) {
725 deadline = fcf->time + fcf->fka_period + fcf->fka_period / 2;
726 if (fip->sel_fcf == fcf) {
727 if (time_after(jiffies, deadline)) {
728 stats->MissDiscAdvCount++;
729 printk(KERN_INFO "libfcoe: host%d: "
730 "Missing Discovery Advertisement "
731 "for fab %16.16llx count %lld\n",
732 fip->lp->host->host_no, fcf->fabric_name,
733 stats->MissDiscAdvCount);
734 } else if (time_after(next_timer, deadline))
735 next_timer = deadline;
738 deadline += fcf->fka_period;
739 if (time_after_eq(jiffies, deadline)) {
740 if (fip->sel_fcf == fcf)
742 list_del(&fcf->list);
743 WARN_ON(!fip->fcf_count);
746 stats->VLinkFailureCount++;
748 if (time_after(next_timer, deadline))
749 next_timer = deadline;
750 if (fcoe_ctlr_mtu_valid(fcf) &&
751 (!sel_time || time_before(sel_time, fcf->time)))
752 sel_time = fcf->time;
756 if (sel_time && !fip->sel_fcf && !fip->sel_time) {
757 sel_time += msecs_to_jiffies(FCOE_CTLR_START_DELAY);
758 fip->sel_time = sel_time;
765 * fcoe_ctlr_parse_adv() - Decode a FIP advertisement into a new FCF entry
766 * @fip: The FCoE controller receiving the advertisement
767 * @skb: The received FIP advertisement frame
768 * @fcf: The resulting FCF entry
770 * Returns zero on a valid parsed advertisement,
771 * otherwise returns non zero value.
773 static int fcoe_ctlr_parse_adv(struct fcoe_ctlr *fip,
774 struct sk_buff *skb, struct fcoe_fcf *fcf)
776 struct fip_header *fiph;
777 struct fip_desc *desc = NULL;
778 struct fip_wwn_desc *wwn;
779 struct fip_fab_desc *fab;
780 struct fip_fka_desc *fka;
786 memset(fcf, 0, sizeof(*fcf));
787 fcf->fka_period = msecs_to_jiffies(FCOE_CTLR_DEF_FKA);
789 fiph = (struct fip_header *)skb->data;
790 fcf->flags = ntohs(fiph->fip_flags);
793 * mask of required descriptors. validating each one clears its bit.
795 desc_mask = BIT(FIP_DT_PRI) | BIT(FIP_DT_MAC) | BIT(FIP_DT_NAME) |
796 BIT(FIP_DT_FAB) | BIT(FIP_DT_FKA);
798 rlen = ntohs(fiph->fip_dl_len) * 4;
799 if (rlen + sizeof(*fiph) > skb->len)
802 desc = (struct fip_desc *)(fiph + 1);
804 dlen = desc->fip_dlen * FIP_BPW;
805 if (dlen < sizeof(*desc) || dlen > rlen)
807 /* Drop Adv if there are duplicate critical descriptors */
808 if ((desc->fip_dtype < 32) &&
809 !(desc_mask & 1U << desc->fip_dtype)) {
810 LIBFCOE_FIP_DBG(fip, "Duplicate Critical "
811 "Descriptors in FIP adv\n");
814 switch (desc->fip_dtype) {
816 if (dlen != sizeof(struct fip_pri_desc))
818 fcf->pri = ((struct fip_pri_desc *)desc)->fd_pri;
819 desc_mask &= ~BIT(FIP_DT_PRI);
822 if (dlen != sizeof(struct fip_mac_desc))
825 ((struct fip_mac_desc *)desc)->fd_mac,
827 memcpy(fcf->fcoe_mac, fcf->fcf_mac, ETH_ALEN);
828 if (!is_valid_ether_addr(fcf->fcf_mac)) {
830 "Invalid MAC addr %pM in FIP adv\n",
834 desc_mask &= ~BIT(FIP_DT_MAC);
837 if (dlen != sizeof(struct fip_wwn_desc))
839 wwn = (struct fip_wwn_desc *)desc;
840 fcf->switch_name = get_unaligned_be64(&wwn->fd_wwn);
841 desc_mask &= ~BIT(FIP_DT_NAME);
844 if (dlen != sizeof(struct fip_fab_desc))
846 fab = (struct fip_fab_desc *)desc;
847 fcf->fabric_name = get_unaligned_be64(&fab->fd_wwn);
848 fcf->vfid = ntohs(fab->fd_vfid);
849 fcf->fc_map = ntoh24(fab->fd_map);
850 desc_mask &= ~BIT(FIP_DT_FAB);
853 if (dlen != sizeof(struct fip_fka_desc))
855 fka = (struct fip_fka_desc *)desc;
856 if (fka->fd_flags & FIP_FKA_ADV_D)
858 t = ntohl(fka->fd_fka_period);
859 if (t >= FCOE_CTLR_MIN_FKA)
860 fcf->fka_period = msecs_to_jiffies(t);
861 desc_mask &= ~BIT(FIP_DT_FKA);
864 case FIP_DT_FCOE_SIZE:
870 LIBFCOE_FIP_DBG(fip, "unexpected descriptor type %x "
871 "in FIP adv\n", desc->fip_dtype);
872 /* standard says ignore unknown descriptors >= 128 */
873 if (desc->fip_dtype < FIP_DT_VENDOR_BASE)
877 desc = (struct fip_desc *)((char *)desc + dlen);
880 if (!fcf->fc_map || (fcf->fc_map & 0x10000))
882 if (!fcf->switch_name)
885 LIBFCOE_FIP_DBG(fip, "adv missing descriptors mask %x\n",
892 LIBFCOE_FIP_DBG(fip, "FIP length error in descriptor type %x len %zu\n",
893 desc->fip_dtype, dlen);
898 * fcoe_ctlr_recv_adv() - Handle an incoming advertisement
899 * @fip: The FCoE controller receiving the advertisement
900 * @skb: The received FIP packet
902 static void fcoe_ctlr_recv_adv(struct fcoe_ctlr *fip, struct sk_buff *skb)
904 struct fcoe_fcf *fcf;
906 struct fcoe_fcf *found;
907 unsigned long sol_tov = msecs_to_jiffies(FCOE_CTRL_SOL_TOV);
911 if (fcoe_ctlr_parse_adv(fip, skb, &new))
914 mutex_lock(&fip->ctlr_mutex);
915 first = list_empty(&fip->fcfs);
917 list_for_each_entry(fcf, &fip->fcfs, list) {
918 if (fcf->switch_name == new.switch_name &&
919 fcf->fabric_name == new.fabric_name &&
920 fcf->fc_map == new.fc_map &&
921 compare_ether_addr(fcf->fcf_mac, new.fcf_mac) == 0) {
927 if (fip->fcf_count >= FCOE_CTLR_FCF_LIMIT)
930 fcf = kmalloc(sizeof(*fcf), GFP_ATOMIC);
935 memcpy(fcf, &new, sizeof(new));
936 list_add(&fcf->list, &fip->fcfs);
939 * Update the FCF's keep-alive descriptor flags.
940 * Other flag changes from new advertisements are
941 * ignored after a solicited advertisement is
942 * received and the FCF is selectable (usable).
944 fcf->fd_flags = new.fd_flags;
945 if (!fcoe_ctlr_fcf_usable(fcf))
946 fcf->flags = new.flags;
948 if (fcf == fip->sel_fcf && !fcf->fd_flags) {
949 fip->ctlr_ka_time -= fcf->fka_period;
950 fip->ctlr_ka_time += new.fka_period;
951 if (time_before(fip->ctlr_ka_time, fip->timer.expires))
952 mod_timer(&fip->timer, fip->ctlr_ka_time);
954 fcf->fka_period = new.fka_period;
955 memcpy(fcf->fcf_mac, new.fcf_mac, ETH_ALEN);
957 mtu_valid = fcoe_ctlr_mtu_valid(fcf);
960 LIBFCOE_FIP_DBG(fip, "New FCF fab %16.16llx mac %pM\n",
961 fcf->fabric_name, fcf->fcf_mac);
964 * If this advertisement is not solicited and our max receive size
965 * hasn't been verified, send a solicited advertisement.
968 fcoe_ctlr_solicit(fip, fcf);
971 * If its been a while since we did a solicit, and this is
972 * the first advertisement we've received, do a multicast
973 * solicitation to gather as many advertisements as we can
974 * before selection occurs.
976 if (first && time_after(jiffies, fip->sol_time + sol_tov))
977 fcoe_ctlr_solicit(fip, NULL);
980 * Put this FCF at the head of the list for priority among equals.
981 * This helps in the case of an NPV switch which insists we use
982 * the FCF that answers multicast solicitations, not the others that
983 * are sending periodic multicast advertisements.
986 list_move(&fcf->list, &fip->fcfs);
989 * If this is the first validated FCF, note the time and
990 * set a timer to trigger selection.
992 if (mtu_valid && !fip->sel_fcf && fcoe_ctlr_fcf_usable(fcf)) {
993 fip->sel_time = jiffies +
994 msecs_to_jiffies(FCOE_CTLR_START_DELAY);
995 if (!timer_pending(&fip->timer) ||
996 time_before(fip->sel_time, fip->timer.expires))
997 mod_timer(&fip->timer, fip->sel_time);
1000 mutex_unlock(&fip->ctlr_mutex);
1004 * fcoe_ctlr_recv_els() - Handle an incoming FIP encapsulated ELS frame
1005 * @fip: The FCoE controller which received the packet
1006 * @skb: The received FIP packet
1008 static void fcoe_ctlr_recv_els(struct fcoe_ctlr *fip, struct sk_buff *skb)
1010 struct fc_lport *lport = fip->lp;
1011 struct fip_header *fiph;
1012 struct fc_frame *fp = (struct fc_frame *)skb;
1013 struct fc_frame_header *fh = NULL;
1014 struct fip_desc *desc;
1015 struct fip_encaps *els;
1016 struct fcoe_dev_stats *stats;
1017 struct fcoe_fcf *sel;
1018 enum fip_desc_type els_dtype = 0;
1021 u8 granted_mac[ETH_ALEN] = { 0 };
1028 fiph = (struct fip_header *)skb->data;
1029 sub = fiph->fip_subcode;
1030 if (sub != FIP_SC_REQ && sub != FIP_SC_REP)
1033 rlen = ntohs(fiph->fip_dl_len) * 4;
1034 if (rlen + sizeof(*fiph) > skb->len)
1037 desc = (struct fip_desc *)(fiph + 1);
1040 dlen = desc->fip_dlen * FIP_BPW;
1041 if (dlen < sizeof(*desc) || dlen > rlen)
1043 /* Drop ELS if there are duplicate critical descriptors */
1044 if (desc->fip_dtype < 32) {
1045 if ((desc->fip_dtype != FIP_DT_MAC) &&
1046 (desc_mask & 1U << desc->fip_dtype)) {
1047 LIBFCOE_FIP_DBG(fip, "Duplicate Critical "
1048 "Descriptors in FIP ELS\n");
1051 desc_mask |= (1 << desc->fip_dtype);
1053 switch (desc->fip_dtype) {
1056 if (desc_cnt == 1) {
1057 LIBFCOE_FIP_DBG(fip, "FIP descriptors "
1058 "received out of order\n");
1062 * Some switch implementations send two MAC descriptors,
1063 * with first MAC(granted_mac) being the FPMA, and the
1064 * second one(fcoe_mac) is used as destination address
1065 * for sending/receiving FCoE packets. FIP traffic is
1066 * sent using fip_mac. For regular switches, both
1067 * fip_mac and fcoe_mac would be the same.
1071 ((struct fip_mac_desc *)desc)->fd_mac,
1074 if (dlen != sizeof(struct fip_mac_desc))
1077 if ((desc_cnt == 3) && (sel))
1078 memcpy(sel->fcoe_mac,
1079 ((struct fip_mac_desc *)desc)->fd_mac,
1086 if (desc_cnt != 1) {
1087 LIBFCOE_FIP_DBG(fip, "FIP descriptors "
1088 "received out of order\n");
1093 if (dlen < sizeof(*els) + sizeof(*fh) + 1)
1095 els_len = dlen - sizeof(*els);
1096 els = (struct fip_encaps *)desc;
1097 fh = (struct fc_frame_header *)(els + 1);
1098 els_dtype = desc->fip_dtype;
1101 LIBFCOE_FIP_DBG(fip, "unexpected descriptor type %x "
1102 "in FIP adv\n", desc->fip_dtype);
1103 /* standard says ignore unknown descriptors >= 128 */
1104 if (desc->fip_dtype < FIP_DT_VENDOR_BASE)
1106 if (desc_cnt <= 2) {
1107 LIBFCOE_FIP_DBG(fip, "FIP descriptors "
1108 "received out of order\n");
1113 desc = (struct fip_desc *)((char *)desc + dlen);
1119 els_op = *(u8 *)(fh + 1);
1121 if ((els_dtype == FIP_DT_FLOGI || els_dtype == FIP_DT_FDISC) &&
1122 sub == FIP_SC_REP && fip->mode != FIP_MODE_VN2VN) {
1123 if (els_op == ELS_LS_ACC) {
1124 if (!is_valid_ether_addr(granted_mac)) {
1125 LIBFCOE_FIP_DBG(fip,
1126 "Invalid MAC address %pM in FIP ELS\n",
1130 memcpy(fr_cb(fp)->granted_mac, granted_mac, ETH_ALEN);
1132 if (fip->flogi_oxid == ntohs(fh->fh_ox_id)) {
1133 fip->flogi_oxid = FC_XID_UNKNOWN;
1134 if (els_dtype == FIP_DT_FLOGI)
1135 fcoe_ctlr_announce(fip);
1137 } else if (els_dtype == FIP_DT_FLOGI &&
1138 !fcoe_ctlr_flogi_retry(fip))
1139 goto drop; /* retrying FLOGI so drop reject */
1142 if ((desc_cnt == 0) || ((els_op != ELS_LS_RJT) &&
1143 (!(1U << FIP_DT_MAC & desc_mask)))) {
1144 LIBFCOE_FIP_DBG(fip, "Missing critical descriptors "
1150 * Convert skb into an fc_frame containing only the ELS.
1152 skb_pull(skb, (u8 *)fh - skb->data);
1153 skb_trim(skb, els_len);
1154 fp = (struct fc_frame *)skb;
1156 fr_sof(fp) = FC_SOF_I3;
1157 fr_eof(fp) = FC_EOF_T;
1159 fr_encaps(fp) = els_dtype;
1161 stats = per_cpu_ptr(lport->dev_stats, get_cpu());
1163 stats->RxWords += skb->len / FIP_BPW;
1166 fc_exch_recv(lport, fp);
1170 LIBFCOE_FIP_DBG(fip, "FIP length error in descriptor type %x len %zu\n",
1171 desc->fip_dtype, dlen);
1177 * fcoe_ctlr_recv_els() - Handle an incoming link reset frame
1178 * @fip: The FCoE controller that received the frame
1179 * @fh: The received FIP header
1181 * There may be multiple VN_Port descriptors.
1182 * The overall length has already been checked.
1184 static void fcoe_ctlr_recv_clr_vlink(struct fcoe_ctlr *fip,
1185 struct fip_header *fh)
1187 struct fip_desc *desc;
1188 struct fip_mac_desc *mp;
1189 struct fip_wwn_desc *wp;
1190 struct fip_vn_desc *vp;
1193 struct fcoe_fcf *fcf = fip->sel_fcf;
1194 struct fc_lport *lport = fip->lp;
1195 struct fc_lport *vn_port = NULL;
1198 int reset_phys_port = 0;
1199 struct fip_vn_desc **vlink_desc_arr = NULL;
1201 LIBFCOE_FIP_DBG(fip, "Clear Virtual Link received\n");
1203 if (!fcf || !lport->port_id)
1207 * mask of required descriptors. Validating each one clears its bit.
1209 desc_mask = BIT(FIP_DT_MAC) | BIT(FIP_DT_NAME);
1211 rlen = ntohs(fh->fip_dl_len) * FIP_BPW;
1212 desc = (struct fip_desc *)(fh + 1);
1215 * Actually need to subtract 'sizeof(*mp) - sizeof(*wp)' from 'rlen'
1216 * before determining max Vx_Port descriptor but a buggy FCF could have
1217 * omited either or both MAC Address and Name Identifier descriptors
1219 num_vlink_desc = rlen / sizeof(*vp);
1221 vlink_desc_arr = kmalloc(sizeof(vp) * num_vlink_desc,
1223 if (!vlink_desc_arr)
1227 while (rlen >= sizeof(*desc)) {
1228 dlen = desc->fip_dlen * FIP_BPW;
1231 /* Drop CVL if there are duplicate critical descriptors */
1232 if ((desc->fip_dtype < 32) &&
1233 (desc->fip_dtype != FIP_DT_VN_ID) &&
1234 !(desc_mask & 1U << desc->fip_dtype)) {
1235 LIBFCOE_FIP_DBG(fip, "Duplicate Critical "
1236 "Descriptors in FIP CVL\n");
1239 switch (desc->fip_dtype) {
1241 mp = (struct fip_mac_desc *)desc;
1242 if (dlen < sizeof(*mp))
1244 if (compare_ether_addr(mp->fd_mac, fcf->fcf_mac))
1246 desc_mask &= ~BIT(FIP_DT_MAC);
1249 wp = (struct fip_wwn_desc *)desc;
1250 if (dlen < sizeof(*wp))
1252 if (get_unaligned_be64(&wp->fd_wwn) != fcf->switch_name)
1254 desc_mask &= ~BIT(FIP_DT_NAME);
1257 vp = (struct fip_vn_desc *)desc;
1258 if (dlen < sizeof(*vp))
1260 vlink_desc_arr[num_vlink_desc++] = vp;
1261 vn_port = fc_vport_id_lookup(lport,
1262 ntoh24(vp->fd_fc_id));
1263 if (vn_port && (vn_port == lport)) {
1264 mutex_lock(&fip->ctlr_mutex);
1265 per_cpu_ptr(lport->dev_stats,
1266 get_cpu())->VLinkFailureCount++;
1268 fcoe_ctlr_reset(fip);
1269 mutex_unlock(&fip->ctlr_mutex);
1273 /* standard says ignore unknown descriptors >= 128 */
1274 if (desc->fip_dtype < FIP_DT_VENDOR_BASE)
1278 desc = (struct fip_desc *)((char *)desc + dlen);
1283 * reset only if all required descriptors were present and valid.
1286 LIBFCOE_FIP_DBG(fip, "missing descriptors mask %x\n",
1288 else if (!num_vlink_desc) {
1289 LIBFCOE_FIP_DBG(fip, "CVL: no Vx_Port descriptor found\n");
1291 * No Vx_Port description. Clear all NPIV ports,
1292 * followed by physical port
1294 mutex_lock(&fip->ctlr_mutex);
1295 per_cpu_ptr(lport->dev_stats,
1296 get_cpu())->VLinkFailureCount++;
1298 fcoe_ctlr_reset(fip);
1299 mutex_unlock(&fip->ctlr_mutex);
1301 mutex_lock(&lport->lp_mutex);
1302 list_for_each_entry(vn_port, &lport->vports, list)
1303 fc_lport_reset(vn_port);
1304 mutex_unlock(&lport->lp_mutex);
1306 fc_lport_reset(fip->lp);
1307 fcoe_ctlr_solicit(fip, NULL);
1311 LIBFCOE_FIP_DBG(fip, "performing Clear Virtual Link\n");
1312 for (i = 0; i < num_vlink_desc; i++) {
1313 vp = vlink_desc_arr[i];
1314 vn_port = fc_vport_id_lookup(lport,
1315 ntoh24(vp->fd_fc_id));
1320 * 'port_id' is already validated, check MAC address and
1323 if (compare_ether_addr(fip->get_src_addr(vn_port),
1325 get_unaligned_be64(&vp->fd_wwpn) !=
1329 if (vn_port == lport)
1331 * Physical port, defer processing till all
1332 * listed NPIV ports are cleared
1334 reset_phys_port = 1;
1335 else /* NPIV port */
1336 fc_lport_reset(vn_port);
1339 if (reset_phys_port) {
1340 fc_lport_reset(fip->lp);
1341 fcoe_ctlr_solicit(fip, NULL);
1346 kfree(vlink_desc_arr);
1350 * fcoe_ctlr_recv() - Receive a FIP packet
1351 * @fip: The FCoE controller that received the packet
1352 * @skb: The received FIP packet
1354 * This may be called from either NET_RX_SOFTIRQ or IRQ.
1356 void fcoe_ctlr_recv(struct fcoe_ctlr *fip, struct sk_buff *skb)
1358 skb_queue_tail(&fip->fip_recv_list, skb);
1359 schedule_work(&fip->recv_work);
1361 EXPORT_SYMBOL(fcoe_ctlr_recv);
1364 * fcoe_ctlr_recv_handler() - Receive a FIP frame
1365 * @fip: The FCoE controller that received the frame
1366 * @skb: The received FIP frame
1368 * Returns non-zero if the frame is dropped.
1370 static int fcoe_ctlr_recv_handler(struct fcoe_ctlr *fip, struct sk_buff *skb)
1372 struct fip_header *fiph;
1374 enum fip_state state;
1378 if (skb_linearize(skb))
1380 if (skb->len < sizeof(*fiph))
1383 if (fip->mode == FIP_MODE_VN2VN) {
1384 if (compare_ether_addr(eh->h_dest, fip->ctl_src_addr) &&
1385 compare_ether_addr(eh->h_dest, fcoe_all_vn2vn) &&
1386 compare_ether_addr(eh->h_dest, fcoe_all_p2p))
1388 } else if (compare_ether_addr(eh->h_dest, fip->ctl_src_addr) &&
1389 compare_ether_addr(eh->h_dest, fcoe_all_enode))
1391 fiph = (struct fip_header *)skb->data;
1392 op = ntohs(fiph->fip_op);
1393 sub = fiph->fip_subcode;
1395 if (FIP_VER_DECAPS(fiph->fip_ver) != FIP_VER)
1397 if (ntohs(fiph->fip_dl_len) * FIP_BPW + sizeof(*fiph) > skb->len)
1400 mutex_lock(&fip->ctlr_mutex);
1402 if (state == FIP_ST_AUTO) {
1404 fcoe_ctlr_set_state(fip, FIP_ST_ENABLED);
1405 state = FIP_ST_ENABLED;
1406 LIBFCOE_FIP_DBG(fip, "Using FIP mode\n");
1408 mutex_unlock(&fip->ctlr_mutex);
1410 if (fip->mode == FIP_MODE_VN2VN && op == FIP_OP_VN2VN)
1411 return fcoe_ctlr_vn_recv(fip, skb);
1413 if (state != FIP_ST_ENABLED && state != FIP_ST_VNMP_UP &&
1414 state != FIP_ST_VNMP_CLAIM)
1417 if (op == FIP_OP_LS) {
1418 fcoe_ctlr_recv_els(fip, skb); /* consumes skb */
1422 if (state != FIP_ST_ENABLED)
1425 if (op == FIP_OP_DISC && sub == FIP_SC_ADV)
1426 fcoe_ctlr_recv_adv(fip, skb);
1427 else if (op == FIP_OP_CTRL && sub == FIP_SC_CLR_VLINK)
1428 fcoe_ctlr_recv_clr_vlink(fip, fiph);
1437 * fcoe_ctlr_select() - Select the best FCF (if possible)
1438 * @fip: The FCoE controller
1440 * Returns the selected FCF, or NULL if none are usable.
1442 * If there are conflicting advertisements, no FCF can be chosen.
1444 * If there is already a selected FCF, this will choose a better one or
1445 * an equivalent one that hasn't already been sent a FLOGI.
1447 * Called with lock held.
1449 static struct fcoe_fcf *fcoe_ctlr_select(struct fcoe_ctlr *fip)
1451 struct fcoe_fcf *fcf;
1452 struct fcoe_fcf *best = fip->sel_fcf;
1453 struct fcoe_fcf *first;
1455 first = list_first_entry(&fip->fcfs, struct fcoe_fcf, list);
1457 list_for_each_entry(fcf, &fip->fcfs, list) {
1458 LIBFCOE_FIP_DBG(fip, "consider FCF fab %16.16llx "
1459 "VFID %d mac %pM map %x val %d "
1461 fcf->fabric_name, fcf->vfid, fcf->fcf_mac,
1462 fcf->fc_map, fcoe_ctlr_mtu_valid(fcf),
1463 fcf->flogi_sent, fcf->pri);
1464 if (fcf->fabric_name != first->fabric_name ||
1465 fcf->vfid != first->vfid ||
1466 fcf->fc_map != first->fc_map) {
1467 LIBFCOE_FIP_DBG(fip, "Conflicting fabric, VFID, "
1471 if (fcf->flogi_sent)
1473 if (!fcoe_ctlr_fcf_usable(fcf)) {
1474 LIBFCOE_FIP_DBG(fip, "FCF for fab %16.16llx "
1475 "map %x %svalid %savailable\n",
1476 fcf->fabric_name, fcf->fc_map,
1477 (fcf->flags & FIP_FL_SOL) ? "" : "in",
1478 (fcf->flags & FIP_FL_AVAIL) ?
1482 if (!best || fcf->pri < best->pri || best->flogi_sent)
1485 fip->sel_fcf = best;
1487 LIBFCOE_FIP_DBG(fip, "using FCF mac %pM\n", best->fcf_mac);
1488 fip->port_ka_time = jiffies +
1489 msecs_to_jiffies(FIP_VN_KA_PERIOD);
1490 fip->ctlr_ka_time = jiffies + best->fka_period;
1491 if (time_before(fip->ctlr_ka_time, fip->timer.expires))
1492 mod_timer(&fip->timer, fip->ctlr_ka_time);
1498 * fcoe_ctlr_flogi_send_locked() - send FIP-encapsulated FLOGI to current FCF
1499 * @fip: The FCoE controller
1501 * Returns non-zero error if it could not be sent.
1503 * Called with ctlr_mutex and ctlr_lock held.
1504 * Caller must verify that fip->sel_fcf is not NULL.
1506 static int fcoe_ctlr_flogi_send_locked(struct fcoe_ctlr *fip)
1508 struct sk_buff *skb;
1509 struct sk_buff *skb_orig;
1510 struct fc_frame_header *fh;
1513 skb_orig = fip->flogi_req;
1518 * Clone and send the FLOGI request. If clone fails, use original.
1520 skb = skb_clone(skb_orig, GFP_ATOMIC);
1523 fip->flogi_req = NULL;
1525 fh = (struct fc_frame_header *)skb->data;
1526 error = fcoe_ctlr_encaps(fip, fip->lp, FIP_DT_FLOGI, skb,
1527 ntoh24(fh->fh_d_id));
1532 fip->send(fip, skb);
1533 fip->sel_fcf->flogi_sent = 1;
1538 * fcoe_ctlr_flogi_retry() - resend FLOGI request to a new FCF if possible
1539 * @fip: The FCoE controller
1541 * Returns non-zero error code if there's no FLOGI request to retry or
1542 * no alternate FCF available.
1544 static int fcoe_ctlr_flogi_retry(struct fcoe_ctlr *fip)
1546 struct fcoe_fcf *fcf;
1549 mutex_lock(&fip->ctlr_mutex);
1550 spin_lock_bh(&fip->ctlr_lock);
1551 LIBFCOE_FIP_DBG(fip, "re-sending FLOGI - reselect\n");
1552 fcf = fcoe_ctlr_select(fip);
1553 if (!fcf || fcf->flogi_sent) {
1554 kfree_skb(fip->flogi_req);
1555 fip->flogi_req = NULL;
1558 fcoe_ctlr_solicit(fip, NULL);
1559 error = fcoe_ctlr_flogi_send_locked(fip);
1561 spin_unlock_bh(&fip->ctlr_lock);
1562 mutex_unlock(&fip->ctlr_mutex);
1568 * fcoe_ctlr_flogi_send() - Handle sending of FIP FLOGI.
1569 * @fip: The FCoE controller that timed out
1571 * Done here because fcoe_ctlr_els_send() can't get mutex.
1573 * Called with ctlr_mutex held. The caller must not hold ctlr_lock.
1575 static void fcoe_ctlr_flogi_send(struct fcoe_ctlr *fip)
1577 struct fcoe_fcf *fcf;
1579 spin_lock_bh(&fip->ctlr_lock);
1581 if (!fcf || !fip->flogi_req_send)
1584 LIBFCOE_FIP_DBG(fip, "sending FLOGI\n");
1587 * If this FLOGI is being sent due to a timeout retry
1588 * to the same FCF as before, select a different FCF if possible.
1590 if (fcf->flogi_sent) {
1591 LIBFCOE_FIP_DBG(fip, "sending FLOGI - reselect\n");
1592 fcf = fcoe_ctlr_select(fip);
1593 if (!fcf || fcf->flogi_sent) {
1594 LIBFCOE_FIP_DBG(fip, "sending FLOGI - clearing\n");
1595 list_for_each_entry(fcf, &fip->fcfs, list)
1596 fcf->flogi_sent = 0;
1597 fcf = fcoe_ctlr_select(fip);
1601 fcoe_ctlr_flogi_send_locked(fip);
1602 fip->flogi_req_send = 0;
1604 LIBFCOE_FIP_DBG(fip, "No FCF selected - defer send\n");
1606 spin_unlock_bh(&fip->ctlr_lock);
1610 * fcoe_ctlr_timeout() - FIP timeout handler
1611 * @arg: The FCoE controller that timed out
1613 static void fcoe_ctlr_timeout(unsigned long arg)
1615 struct fcoe_ctlr *fip = (struct fcoe_ctlr *)arg;
1617 schedule_work(&fip->timer_work);
1621 * fcoe_ctlr_timer_work() - Worker thread function for timer work
1622 * @work: Handle to a FCoE controller
1624 * Ages FCFs. Triggers FCF selection if possible.
1625 * Sends keep-alives and resets.
1627 static void fcoe_ctlr_timer_work(struct work_struct *work)
1629 struct fcoe_ctlr *fip;
1630 struct fc_lport *vport;
1633 u8 send_ctlr_ka = 0;
1634 u8 send_port_ka = 0;
1635 struct fcoe_fcf *sel;
1636 struct fcoe_fcf *fcf;
1637 unsigned long next_timer;
1639 fip = container_of(work, struct fcoe_ctlr, timer_work);
1640 if (fip->mode == FIP_MODE_VN2VN)
1641 return fcoe_ctlr_vn_timeout(fip);
1642 mutex_lock(&fip->ctlr_mutex);
1643 if (fip->state == FIP_ST_DISABLED) {
1644 mutex_unlock(&fip->ctlr_mutex);
1649 next_timer = fcoe_ctlr_age_fcfs(fip);
1652 if (!sel && fip->sel_time) {
1653 if (time_after_eq(jiffies, fip->sel_time)) {
1654 sel = fcoe_ctlr_select(fip);
1656 } else if (time_after(next_timer, fip->sel_time))
1657 next_timer = fip->sel_time;
1660 if (sel && fip->flogi_req_send)
1661 fcoe_ctlr_flogi_send(fip);
1662 else if (!sel && fcf)
1665 if (sel && !sel->fd_flags) {
1666 if (time_after_eq(jiffies, fip->ctlr_ka_time)) {
1667 fip->ctlr_ka_time = jiffies + sel->fka_period;
1670 if (time_after(next_timer, fip->ctlr_ka_time))
1671 next_timer = fip->ctlr_ka_time;
1673 if (time_after_eq(jiffies, fip->port_ka_time)) {
1674 fip->port_ka_time = jiffies +
1675 msecs_to_jiffies(FIP_VN_KA_PERIOD);
1678 if (time_after(next_timer, fip->port_ka_time))
1679 next_timer = fip->port_ka_time;
1681 if (!list_empty(&fip->fcfs))
1682 mod_timer(&fip->timer, next_timer);
1683 mutex_unlock(&fip->ctlr_mutex);
1686 fc_lport_reset(fip->lp);
1687 /* restart things with a solicitation */
1688 fcoe_ctlr_solicit(fip, NULL);
1692 fcoe_ctlr_send_keep_alive(fip, NULL, 0, fip->ctl_src_addr);
1695 mutex_lock(&fip->lp->lp_mutex);
1696 mac = fip->get_src_addr(fip->lp);
1697 fcoe_ctlr_send_keep_alive(fip, fip->lp, 1, mac);
1698 list_for_each_entry(vport, &fip->lp->vports, list) {
1699 mac = fip->get_src_addr(vport);
1700 fcoe_ctlr_send_keep_alive(fip, vport, 1, mac);
1702 mutex_unlock(&fip->lp->lp_mutex);
1707 * fcoe_ctlr_recv_work() - Worker thread function for receiving FIP frames
1708 * @recv_work: Handle to a FCoE controller
1710 static void fcoe_ctlr_recv_work(struct work_struct *recv_work)
1712 struct fcoe_ctlr *fip;
1713 struct sk_buff *skb;
1715 fip = container_of(recv_work, struct fcoe_ctlr, recv_work);
1716 while ((skb = skb_dequeue(&fip->fip_recv_list)))
1717 fcoe_ctlr_recv_handler(fip, skb);
1721 * fcoe_ctlr_recv_flogi() - Snoop pre-FIP receipt of FLOGI response
1722 * @fip: The FCoE controller
1723 * @fp: The FC frame to snoop
1725 * Snoop potential response to FLOGI or even incoming FLOGI.
1727 * The caller has checked that we are waiting for login as indicated
1728 * by fip->flogi_oxid != FC_XID_UNKNOWN.
1730 * The caller is responsible for freeing the frame.
1731 * Fill in the granted_mac address.
1733 * Return non-zero if the frame should not be delivered to libfc.
1735 int fcoe_ctlr_recv_flogi(struct fcoe_ctlr *fip, struct fc_lport *lport,
1736 struct fc_frame *fp)
1738 struct fc_frame_header *fh;
1742 sa = eth_hdr(&fp->skb)->h_source;
1743 fh = fc_frame_header_get(fp);
1744 if (fh->fh_type != FC_TYPE_ELS)
1747 op = fc_frame_payload_op(fp);
1748 if (op == ELS_LS_ACC && fh->fh_r_ctl == FC_RCTL_ELS_REP &&
1749 fip->flogi_oxid == ntohs(fh->fh_ox_id)) {
1751 mutex_lock(&fip->ctlr_mutex);
1752 if (fip->state != FIP_ST_AUTO && fip->state != FIP_ST_NON_FIP) {
1753 mutex_unlock(&fip->ctlr_mutex);
1756 fcoe_ctlr_set_state(fip, FIP_ST_NON_FIP);
1757 LIBFCOE_FIP_DBG(fip,
1758 "received FLOGI LS_ACC using non-FIP mode\n");
1762 * If the src mac addr is FC_OUI-based, then we mark the
1763 * address_mode flag to use FC_OUI-based Ethernet DA.
1764 * Otherwise we use the FCoE gateway addr
1766 if (!compare_ether_addr(sa, (u8[6])FC_FCOE_FLOGI_MAC)) {
1767 fcoe_ctlr_map_dest(fip);
1769 memcpy(fip->dest_addr, sa, ETH_ALEN);
1772 fip->flogi_oxid = FC_XID_UNKNOWN;
1773 mutex_unlock(&fip->ctlr_mutex);
1774 fc_fcoe_set_mac(fr_cb(fp)->granted_mac, fh->fh_d_id);
1775 } else if (op == ELS_FLOGI && fh->fh_r_ctl == FC_RCTL_ELS_REQ && sa) {
1777 * Save source MAC for point-to-point responses.
1779 mutex_lock(&fip->ctlr_mutex);
1780 if (fip->state == FIP_ST_AUTO || fip->state == FIP_ST_NON_FIP) {
1781 memcpy(fip->dest_addr, sa, ETH_ALEN);
1783 if (fip->state == FIP_ST_AUTO)
1784 LIBFCOE_FIP_DBG(fip, "received non-FIP FLOGI. "
1785 "Setting non-FIP mode\n");
1786 fcoe_ctlr_set_state(fip, FIP_ST_NON_FIP);
1788 mutex_unlock(&fip->ctlr_mutex);
1792 EXPORT_SYMBOL(fcoe_ctlr_recv_flogi);
1795 * fcoe_wwn_from_mac() - Converts a 48-bit IEEE MAC address to a 64-bit FC WWN
1796 * @mac: The MAC address to convert
1797 * @scheme: The scheme to use when converting
1798 * @port: The port indicator for converting
1800 * Returns: u64 fc world wide name
1802 u64 fcoe_wwn_from_mac(unsigned char mac[MAX_ADDR_LEN],
1803 unsigned int scheme, unsigned int port)
1808 /* The MAC is in NO, so flip only the low 48 bits */
1809 host_mac = ((u64) mac[0] << 40) |
1810 ((u64) mac[1] << 32) |
1811 ((u64) mac[2] << 24) |
1812 ((u64) mac[3] << 16) |
1813 ((u64) mac[4] << 8) |
1816 WARN_ON(host_mac >= (1ULL << 48));
1817 wwn = host_mac | ((u64) scheme << 60);
1823 WARN_ON(port >= 0xfff);
1824 wwn |= (u64) port << 48;
1833 EXPORT_SYMBOL_GPL(fcoe_wwn_from_mac);
1836 * fcoe_ctlr_rport() - return the fcoe_rport for a given fc_rport_priv
1837 * @rdata: libfc remote port
1839 static inline struct fcoe_rport *fcoe_ctlr_rport(struct fc_rport_priv *rdata)
1841 return (struct fcoe_rport *)(rdata + 1);
1845 * fcoe_ctlr_vn_send() - Send a FIP VN2VN Probe Request or Reply.
1846 * @fip: The FCoE controller
1847 * @sub: sub-opcode for probe request, reply, or advertisement.
1848 * @dest: The destination Ethernet MAC address
1849 * @min_len: minimum size of the Ethernet payload to be sent
1851 static void fcoe_ctlr_vn_send(struct fcoe_ctlr *fip,
1852 enum fip_vn2vn_subcode sub,
1853 const u8 *dest, size_t min_len)
1855 struct sk_buff *skb;
1858 struct fip_header fip;
1859 struct fip_mac_desc mac;
1860 struct fip_wwn_desc wwnn;
1861 struct fip_vn_desc vn;
1863 struct fip_fc4_feat *ff;
1864 struct fip_size_desc *size;
1869 len = sizeof(*frame);
1871 if (sub == FIP_SC_VN_CLAIM_NOTIFY || sub == FIP_SC_VN_CLAIM_REP) {
1872 dlen = sizeof(struct fip_fc4_feat) +
1873 sizeof(struct fip_size_desc);
1876 dlen += sizeof(frame->mac) + sizeof(frame->wwnn) + sizeof(frame->vn);
1877 len = max(len, min_len + sizeof(struct ethhdr));
1879 skb = dev_alloc_skb(len);
1883 frame = (struct fip_frame *)skb->data;
1884 memset(frame, 0, len);
1885 memcpy(frame->eth.h_dest, dest, ETH_ALEN);
1887 if (sub == FIP_SC_VN_BEACON) {
1888 hton24(frame->eth.h_source, FIP_VN_FC_MAP);
1889 hton24(frame->eth.h_source + 3, fip->port_id);
1891 memcpy(frame->eth.h_source, fip->ctl_src_addr, ETH_ALEN);
1893 frame->eth.h_proto = htons(ETH_P_FIP);
1895 frame->fip.fip_ver = FIP_VER_ENCAPS(FIP_VER);
1896 frame->fip.fip_op = htons(FIP_OP_VN2VN);
1897 frame->fip.fip_subcode = sub;
1898 frame->fip.fip_dl_len = htons(dlen / FIP_BPW);
1900 frame->mac.fd_desc.fip_dtype = FIP_DT_MAC;
1901 frame->mac.fd_desc.fip_dlen = sizeof(frame->mac) / FIP_BPW;
1902 memcpy(frame->mac.fd_mac, fip->ctl_src_addr, ETH_ALEN);
1904 frame->wwnn.fd_desc.fip_dtype = FIP_DT_NAME;
1905 frame->wwnn.fd_desc.fip_dlen = sizeof(frame->wwnn) / FIP_BPW;
1906 put_unaligned_be64(fip->lp->wwnn, &frame->wwnn.fd_wwn);
1908 frame->vn.fd_desc.fip_dtype = FIP_DT_VN_ID;
1909 frame->vn.fd_desc.fip_dlen = sizeof(frame->vn) / FIP_BPW;
1910 hton24(frame->vn.fd_mac, FIP_VN_FC_MAP);
1911 hton24(frame->vn.fd_mac + 3, fip->port_id);
1912 hton24(frame->vn.fd_fc_id, fip->port_id);
1913 put_unaligned_be64(fip->lp->wwpn, &frame->vn.fd_wwpn);
1916 * For claims, add FC-4 features.
1917 * TBD: Add interface to get fc-4 types and features from libfc.
1919 if (sub == FIP_SC_VN_CLAIM_NOTIFY || sub == FIP_SC_VN_CLAIM_REP) {
1920 ff = (struct fip_fc4_feat *)(frame + 1);
1921 ff->fd_desc.fip_dtype = FIP_DT_FC4F;
1922 ff->fd_desc.fip_dlen = sizeof(*ff) / FIP_BPW;
1923 ff->fd_fts = fip->lp->fcts;
1926 if (fip->lp->service_params & FCP_SPPF_INIT_FCN)
1927 fcp_feat |= FCP_FEAT_INIT;
1928 if (fip->lp->service_params & FCP_SPPF_TARG_FCN)
1929 fcp_feat |= FCP_FEAT_TARG;
1930 fcp_feat <<= (FC_TYPE_FCP * 4) % 32;
1931 ff->fd_ff.fd_feat[FC_TYPE_FCP * 4 / 32] = htonl(fcp_feat);
1933 size = (struct fip_size_desc *)(ff + 1);
1934 size->fd_desc.fip_dtype = FIP_DT_FCOE_SIZE;
1935 size->fd_desc.fip_dlen = sizeof(*size) / FIP_BPW;
1936 size->fd_size = htons(fcoe_ctlr_fcoe_size(fip));
1940 skb->protocol = htons(ETH_P_FIP);
1941 skb->priority = fip->priority;
1942 skb_reset_mac_header(skb);
1943 skb_reset_network_header(skb);
1945 fip->send(fip, skb);
1949 * fcoe_ctlr_vn_rport_callback - Event handler for rport events.
1950 * @lport: The lport which is receiving the event
1951 * @rdata: remote port private data
1952 * @event: The event that occurred
1954 * Locking Note: The rport lock must not be held when calling this function.
1956 static void fcoe_ctlr_vn_rport_callback(struct fc_lport *lport,
1957 struct fc_rport_priv *rdata,
1958 enum fc_rport_event event)
1960 struct fcoe_ctlr *fip = lport->disc.priv;
1961 struct fcoe_rport *frport = fcoe_ctlr_rport(rdata);
1963 LIBFCOE_FIP_DBG(fip, "vn_rport_callback %x event %d\n",
1964 rdata->ids.port_id, event);
1966 mutex_lock(&fip->ctlr_mutex);
1968 case RPORT_EV_READY:
1969 frport->login_count = 0;
1972 case RPORT_EV_FAILED:
1974 frport->login_count++;
1975 if (frport->login_count > FCOE_CTLR_VN2VN_LOGIN_LIMIT) {
1976 LIBFCOE_FIP_DBG(fip,
1977 "rport FLOGI limited port_id %6.6x\n",
1978 rdata->ids.port_id);
1979 lport->tt.rport_logoff(rdata);
1985 mutex_unlock(&fip->ctlr_mutex);
1988 static struct fc_rport_operations fcoe_ctlr_vn_rport_ops = {
1989 .event_callback = fcoe_ctlr_vn_rport_callback,
1993 * fcoe_ctlr_disc_stop_locked() - stop discovery in VN2VN mode
1994 * @fip: The FCoE controller
1996 * Called with ctlr_mutex held.
1998 static void fcoe_ctlr_disc_stop_locked(struct fc_lport *lport)
2000 mutex_lock(&lport->disc.disc_mutex);
2001 lport->disc.disc_callback = NULL;
2002 mutex_unlock(&lport->disc.disc_mutex);
2006 * fcoe_ctlr_disc_stop() - stop discovery in VN2VN mode
2007 * @fip: The FCoE controller
2009 * Called through the local port template for discovery.
2010 * Called without the ctlr_mutex held.
2012 static void fcoe_ctlr_disc_stop(struct fc_lport *lport)
2014 struct fcoe_ctlr *fip = lport->disc.priv;
2016 mutex_lock(&fip->ctlr_mutex);
2017 fcoe_ctlr_disc_stop_locked(lport);
2018 mutex_unlock(&fip->ctlr_mutex);
2022 * fcoe_ctlr_disc_stop_final() - stop discovery for shutdown in VN2VN mode
2023 * @fip: The FCoE controller
2025 * Called through the local port template for discovery.
2026 * Called without the ctlr_mutex held.
2028 static void fcoe_ctlr_disc_stop_final(struct fc_lport *lport)
2030 fcoe_ctlr_disc_stop(lport);
2031 lport->tt.rport_flush_queue();
2036 * fcoe_ctlr_vn_restart() - VN2VN probe restart with new port_id
2037 * @fip: The FCoE controller
2039 * Called with fcoe_ctlr lock held.
2041 static void fcoe_ctlr_vn_restart(struct fcoe_ctlr *fip)
2046 fcoe_ctlr_disc_stop_locked(fip->lp);
2049 * Get proposed port ID.
2050 * If this is the first try after link up, use any previous port_id.
2051 * If there was none, use the low bits of the port_name.
2052 * On subsequent tries, get the next random one.
2053 * Don't use reserved IDs, use another non-zero value, just as random.
2055 port_id = fip->port_id;
2056 if (fip->probe_tries)
2057 port_id = prandom32(&fip->rnd_state) & 0xffff;
2059 port_id = fip->lp->wwpn & 0xffff;
2060 if (!port_id || port_id == 0xffff)
2062 fip->port_id = port_id;
2064 if (fip->probe_tries < FIP_VN_RLIM_COUNT) {
2066 wait = random32() % FIP_VN_PROBE_WAIT;
2068 wait = FIP_VN_RLIM_INT;
2069 mod_timer(&fip->timer, jiffies + msecs_to_jiffies(wait));
2070 fcoe_ctlr_set_state(fip, FIP_ST_VNMP_START);
2074 * fcoe_ctlr_vn_start() - Start in VN2VN mode
2075 * @fip: The FCoE controller
2077 * Called with fcoe_ctlr lock held.
2079 static void fcoe_ctlr_vn_start(struct fcoe_ctlr *fip)
2081 fip->probe_tries = 0;
2082 prandom32_seed(&fip->rnd_state, fip->lp->wwpn);
2083 fcoe_ctlr_vn_restart(fip);
2087 * fcoe_ctlr_vn_parse - parse probe request or response
2088 * @fip: The FCoE controller
2089 * @skb: incoming packet
2090 * @rdata: buffer for resulting parsed VN entry plus fcoe_rport
2092 * Returns non-zero error number on error.
2093 * Does not consume the packet.
2095 static int fcoe_ctlr_vn_parse(struct fcoe_ctlr *fip,
2096 struct sk_buff *skb,
2097 struct fc_rport_priv *rdata)
2099 struct fip_header *fiph;
2100 struct fip_desc *desc = NULL;
2101 struct fip_mac_desc *macd = NULL;
2102 struct fip_wwn_desc *wwn = NULL;
2103 struct fip_vn_desc *vn = NULL;
2104 struct fip_size_desc *size = NULL;
2105 struct fcoe_rport *frport;
2112 memset(rdata, 0, sizeof(*rdata) + sizeof(*frport));
2113 frport = fcoe_ctlr_rport(rdata);
2115 fiph = (struct fip_header *)skb->data;
2116 frport->flags = ntohs(fiph->fip_flags);
2118 sub = fiph->fip_subcode;
2120 case FIP_SC_VN_PROBE_REQ:
2121 case FIP_SC_VN_PROBE_REP:
2122 case FIP_SC_VN_BEACON:
2123 desc_mask = BIT(FIP_DT_MAC) | BIT(FIP_DT_NAME) |
2126 case FIP_SC_VN_CLAIM_NOTIFY:
2127 case FIP_SC_VN_CLAIM_REP:
2128 desc_mask = BIT(FIP_DT_MAC) | BIT(FIP_DT_NAME) |
2129 BIT(FIP_DT_VN_ID) | BIT(FIP_DT_FC4F) |
2130 BIT(FIP_DT_FCOE_SIZE);
2133 LIBFCOE_FIP_DBG(fip, "vn_parse unknown subcode %u\n", sub);
2137 rlen = ntohs(fiph->fip_dl_len) * 4;
2138 if (rlen + sizeof(*fiph) > skb->len)
2141 desc = (struct fip_desc *)(fiph + 1);
2143 dlen = desc->fip_dlen * FIP_BPW;
2144 if (dlen < sizeof(*desc) || dlen > rlen)
2147 dtype = desc->fip_dtype;
2149 if (!(desc_mask & BIT(dtype))) {
2150 LIBFCOE_FIP_DBG(fip,
2151 "unexpected or duplicated desc "
2153 "FIP VN2VN subtype %u\n",
2157 desc_mask &= ~BIT(dtype);
2162 if (dlen != sizeof(struct fip_mac_desc))
2164 macd = (struct fip_mac_desc *)desc;
2165 if (!is_valid_ether_addr(macd->fd_mac)) {
2166 LIBFCOE_FIP_DBG(fip,
2167 "Invalid MAC addr %pM in FIP VN2VN\n",
2171 memcpy(frport->enode_mac, macd->fd_mac, ETH_ALEN);
2174 if (dlen != sizeof(struct fip_wwn_desc))
2176 wwn = (struct fip_wwn_desc *)desc;
2177 rdata->ids.node_name = get_unaligned_be64(&wwn->fd_wwn);
2180 if (dlen != sizeof(struct fip_vn_desc))
2182 vn = (struct fip_vn_desc *)desc;
2183 memcpy(frport->vn_mac, vn->fd_mac, ETH_ALEN);
2184 rdata->ids.port_id = ntoh24(vn->fd_fc_id);
2185 rdata->ids.port_name = get_unaligned_be64(&vn->fd_wwpn);
2188 if (dlen != sizeof(struct fip_fc4_feat))
2191 case FIP_DT_FCOE_SIZE:
2192 if (dlen != sizeof(struct fip_size_desc))
2194 size = (struct fip_size_desc *)desc;
2195 frport->fcoe_len = ntohs(size->fd_size);
2198 LIBFCOE_FIP_DBG(fip, "unexpected descriptor type %x "
2199 "in FIP probe\n", dtype);
2200 /* standard says ignore unknown descriptors >= 128 */
2201 if (dtype < FIP_DT_VENDOR_BASE)
2205 desc = (struct fip_desc *)((char *)desc + dlen);
2211 LIBFCOE_FIP_DBG(fip, "FIP length error in descriptor type %x len %zu\n",
2217 * fcoe_ctlr_vn_send_claim() - send multicast FIP VN2VN Claim Notification.
2218 * @fip: The FCoE controller
2220 * Called with ctlr_mutex held.
2222 static void fcoe_ctlr_vn_send_claim(struct fcoe_ctlr *fip)
2224 fcoe_ctlr_vn_send(fip, FIP_SC_VN_CLAIM_NOTIFY, fcoe_all_vn2vn, 0);
2225 fip->sol_time = jiffies;
2229 * fcoe_ctlr_vn_probe_req() - handle incoming VN2VN probe request.
2230 * @fip: The FCoE controller
2231 * @rdata: parsed remote port with frport from the probe request
2233 * Called with ctlr_mutex held.
2235 static void fcoe_ctlr_vn_probe_req(struct fcoe_ctlr *fip,
2236 struct fc_rport_priv *rdata)
2238 struct fcoe_rport *frport = fcoe_ctlr_rport(rdata);
2240 if (rdata->ids.port_id != fip->port_id)
2243 switch (fip->state) {
2244 case FIP_ST_VNMP_CLAIM:
2245 case FIP_ST_VNMP_UP:
2246 fcoe_ctlr_vn_send(fip, FIP_SC_VN_PROBE_REP,
2247 frport->enode_mac, 0);
2249 case FIP_ST_VNMP_PROBE1:
2250 case FIP_ST_VNMP_PROBE2:
2252 * Decide whether to reply to the Probe.
2253 * Our selected address is never a "recorded" one, so
2254 * only reply if our WWPN is greater and the
2255 * Probe's REC bit is not set.
2256 * If we don't reply, we will change our address.
2258 if (fip->lp->wwpn > rdata->ids.port_name &&
2259 !(frport->flags & FIP_FL_REC_OR_P2P)) {
2260 fcoe_ctlr_vn_send(fip, FIP_SC_VN_PROBE_REP,
2261 frport->enode_mac, 0);
2265 case FIP_ST_VNMP_START:
2266 fcoe_ctlr_vn_restart(fip);
2274 * fcoe_ctlr_vn_probe_reply() - handle incoming VN2VN probe reply.
2275 * @fip: The FCoE controller
2276 * @rdata: parsed remote port with frport from the probe request
2278 * Called with ctlr_mutex held.
2280 static void fcoe_ctlr_vn_probe_reply(struct fcoe_ctlr *fip,
2281 struct fc_rport_priv *rdata)
2283 if (rdata->ids.port_id != fip->port_id)
2285 switch (fip->state) {
2286 case FIP_ST_VNMP_START:
2287 case FIP_ST_VNMP_PROBE1:
2288 case FIP_ST_VNMP_PROBE2:
2289 case FIP_ST_VNMP_CLAIM:
2290 fcoe_ctlr_vn_restart(fip);
2292 case FIP_ST_VNMP_UP:
2293 fcoe_ctlr_vn_send_claim(fip);
2301 * fcoe_ctlr_vn_add() - Add a VN2VN entry to the list, based on a claim reply.
2302 * @fip: The FCoE controller
2303 * @new: newly-parsed remote port with frport as a template for new rdata
2305 * Called with ctlr_mutex held.
2307 static void fcoe_ctlr_vn_add(struct fcoe_ctlr *fip, struct fc_rport_priv *new)
2309 struct fc_lport *lport = fip->lp;
2310 struct fc_rport_priv *rdata;
2311 struct fc_rport_identifiers *ids;
2312 struct fcoe_rport *frport;
2315 port_id = new->ids.port_id;
2316 if (port_id == fip->port_id)
2319 mutex_lock(&lport->disc.disc_mutex);
2320 rdata = lport->tt.rport_create(lport, port_id);
2322 mutex_unlock(&lport->disc.disc_mutex);
2326 rdata->ops = &fcoe_ctlr_vn_rport_ops;
2327 rdata->disc_id = lport->disc.disc_id;
2330 if ((ids->port_name != -1 && ids->port_name != new->ids.port_name) ||
2331 (ids->node_name != -1 && ids->node_name != new->ids.node_name))
2332 lport->tt.rport_logoff(rdata);
2333 ids->port_name = new->ids.port_name;
2334 ids->node_name = new->ids.node_name;
2335 mutex_unlock(&lport->disc.disc_mutex);
2337 frport = fcoe_ctlr_rport(rdata);
2338 LIBFCOE_FIP_DBG(fip, "vn_add rport %6.6x %s\n",
2339 port_id, frport->fcoe_len ? "old" : "new");
2340 *frport = *fcoe_ctlr_rport(new);
2345 * fcoe_ctlr_vn_lookup() - Find VN remote port's MAC address
2346 * @fip: The FCoE controller
2347 * @port_id: The port_id of the remote VN_node
2348 * @mac: buffer which will hold the VN_NODE destination MAC address, if found.
2350 * Returns non-zero error if no remote port found.
2352 static int fcoe_ctlr_vn_lookup(struct fcoe_ctlr *fip, u32 port_id, u8 *mac)
2354 struct fc_lport *lport = fip->lp;
2355 struct fc_rport_priv *rdata;
2356 struct fcoe_rport *frport;
2360 rdata = lport->tt.rport_lookup(lport, port_id);
2362 frport = fcoe_ctlr_rport(rdata);
2363 memcpy(mac, frport->enode_mac, ETH_ALEN);
2371 * fcoe_ctlr_vn_claim_notify() - handle received FIP VN2VN Claim Notification
2372 * @fip: The FCoE controller
2373 * @new: newly-parsed remote port with frport as a template for new rdata
2375 * Called with ctlr_mutex held.
2377 static void fcoe_ctlr_vn_claim_notify(struct fcoe_ctlr *fip,
2378 struct fc_rport_priv *new)
2380 struct fcoe_rport *frport = fcoe_ctlr_rport(new);
2382 if (frport->flags & FIP_FL_REC_OR_P2P) {
2383 fcoe_ctlr_vn_send(fip, FIP_SC_VN_PROBE_REQ, fcoe_all_vn2vn, 0);
2386 switch (fip->state) {
2387 case FIP_ST_VNMP_START:
2388 case FIP_ST_VNMP_PROBE1:
2389 case FIP_ST_VNMP_PROBE2:
2390 if (new->ids.port_id == fip->port_id)
2391 fcoe_ctlr_vn_restart(fip);
2393 case FIP_ST_VNMP_CLAIM:
2394 case FIP_ST_VNMP_UP:
2395 if (new->ids.port_id == fip->port_id) {
2396 if (new->ids.port_name > fip->lp->wwpn) {
2397 fcoe_ctlr_vn_restart(fip);
2400 fcoe_ctlr_vn_send_claim(fip);
2403 fcoe_ctlr_vn_send(fip, FIP_SC_VN_CLAIM_REP, frport->enode_mac,
2404 min((u32)frport->fcoe_len,
2405 fcoe_ctlr_fcoe_size(fip)));
2406 fcoe_ctlr_vn_add(fip, new);
2414 * fcoe_ctlr_vn_claim_resp() - handle received Claim Response
2415 * @fip: The FCoE controller that received the frame
2416 * @new: newly-parsed remote port with frport from the Claim Response
2418 * Called with ctlr_mutex held.
2420 static void fcoe_ctlr_vn_claim_resp(struct fcoe_ctlr *fip,
2421 struct fc_rport_priv *new)
2423 LIBFCOE_FIP_DBG(fip, "claim resp from from rport %x - state %s\n",
2424 new->ids.port_id, fcoe_ctlr_state(fip->state));
2425 if (fip->state == FIP_ST_VNMP_UP || fip->state == FIP_ST_VNMP_CLAIM)
2426 fcoe_ctlr_vn_add(fip, new);
2430 * fcoe_ctlr_vn_beacon() - handle received beacon.
2431 * @fip: The FCoE controller that received the frame
2432 * @new: newly-parsed remote port with frport from the Beacon
2434 * Called with ctlr_mutex held.
2436 static void fcoe_ctlr_vn_beacon(struct fcoe_ctlr *fip,
2437 struct fc_rport_priv *new)
2439 struct fc_lport *lport = fip->lp;
2440 struct fc_rport_priv *rdata;
2441 struct fcoe_rport *frport;
2443 frport = fcoe_ctlr_rport(new);
2444 if (frport->flags & FIP_FL_REC_OR_P2P) {
2445 fcoe_ctlr_vn_send(fip, FIP_SC_VN_PROBE_REQ, fcoe_all_vn2vn, 0);
2448 mutex_lock(&lport->disc.disc_mutex);
2449 rdata = lport->tt.rport_lookup(lport, new->ids.port_id);
2451 kref_get(&rdata->kref);
2452 mutex_unlock(&lport->disc.disc_mutex);
2454 if (rdata->ids.node_name == new->ids.node_name &&
2455 rdata->ids.port_name == new->ids.port_name) {
2456 frport = fcoe_ctlr_rport(rdata);
2457 if (!frport->time && fip->state == FIP_ST_VNMP_UP)
2458 lport->tt.rport_login(rdata);
2459 frport->time = jiffies;
2461 kref_put(&rdata->kref, lport->tt.rport_destroy);
2464 if (fip->state != FIP_ST_VNMP_UP)
2468 * Beacon from a new neighbor.
2469 * Send a claim notify if one hasn't been sent recently.
2470 * Don't add the neighbor yet.
2472 LIBFCOE_FIP_DBG(fip, "beacon from new rport %x. sending claim notify\n",
2474 if (time_after(jiffies,
2475 fip->sol_time + msecs_to_jiffies(FIP_VN_ANN_WAIT)))
2476 fcoe_ctlr_vn_send_claim(fip);
2480 * fcoe_ctlr_vn_age() - Check for VN_ports without recent beacons
2481 * @fip: The FCoE controller
2483 * Called with ctlr_mutex held.
2484 * Called only in state FIP_ST_VNMP_UP.
2485 * Returns the soonest time for next age-out or a time far in the future.
2487 static unsigned long fcoe_ctlr_vn_age(struct fcoe_ctlr *fip)
2489 struct fc_lport *lport = fip->lp;
2490 struct fc_rport_priv *rdata;
2491 struct fcoe_rport *frport;
2492 unsigned long next_time;
2493 unsigned long deadline;
2495 next_time = jiffies + msecs_to_jiffies(FIP_VN_BEACON_INT * 10);
2496 mutex_lock(&lport->disc.disc_mutex);
2497 list_for_each_entry_rcu(rdata, &lport->disc.rports, peers) {
2498 frport = fcoe_ctlr_rport(rdata);
2501 deadline = frport->time +
2502 msecs_to_jiffies(FIP_VN_BEACON_INT * 25 / 10);
2503 if (time_after_eq(jiffies, deadline)) {
2505 LIBFCOE_FIP_DBG(fip,
2506 "port %16.16llx fc_id %6.6x beacon expired\n",
2507 rdata->ids.port_name, rdata->ids.port_id);
2508 lport->tt.rport_logoff(rdata);
2509 } else if (time_before(deadline, next_time))
2510 next_time = deadline;
2512 mutex_unlock(&lport->disc.disc_mutex);
2517 * fcoe_ctlr_vn_recv() - Receive a FIP frame
2518 * @fip: The FCoE controller that received the frame
2519 * @skb: The received FIP frame
2521 * Returns non-zero if the frame is dropped.
2522 * Always consumes the frame.
2524 static int fcoe_ctlr_vn_recv(struct fcoe_ctlr *fip, struct sk_buff *skb)
2526 struct fip_header *fiph;
2527 enum fip_vn2vn_subcode sub;
2529 struct fc_rport_priv rdata;
2530 struct fcoe_rport frport;
2534 fiph = (struct fip_header *)skb->data;
2535 sub = fiph->fip_subcode;
2537 rc = fcoe_ctlr_vn_parse(fip, skb, &buf.rdata);
2539 LIBFCOE_FIP_DBG(fip, "vn_recv vn_parse error %d\n", rc);
2543 mutex_lock(&fip->ctlr_mutex);
2545 case FIP_SC_VN_PROBE_REQ:
2546 fcoe_ctlr_vn_probe_req(fip, &buf.rdata);
2548 case FIP_SC_VN_PROBE_REP:
2549 fcoe_ctlr_vn_probe_reply(fip, &buf.rdata);
2551 case FIP_SC_VN_CLAIM_NOTIFY:
2552 fcoe_ctlr_vn_claim_notify(fip, &buf.rdata);
2554 case FIP_SC_VN_CLAIM_REP:
2555 fcoe_ctlr_vn_claim_resp(fip, &buf.rdata);
2557 case FIP_SC_VN_BEACON:
2558 fcoe_ctlr_vn_beacon(fip, &buf.rdata);
2561 LIBFCOE_FIP_DBG(fip, "vn_recv unknown subcode %d\n", sub);
2565 mutex_unlock(&fip->ctlr_mutex);
2572 * fcoe_ctlr_disc_recv - discovery receive handler for VN2VN mode.
2573 * @lport: The local port
2574 * @fp: The received frame
2576 * This should never be called since we don't see RSCNs or other
2577 * fabric-generated ELSes.
2579 static void fcoe_ctlr_disc_recv(struct fc_lport *lport, struct fc_frame *fp)
2581 struct fc_seq_els_data rjt_data;
2583 rjt_data.reason = ELS_RJT_UNSUP;
2584 rjt_data.explan = ELS_EXPL_NONE;
2585 lport->tt.seq_els_rsp_send(fp, ELS_LS_RJT, &rjt_data);
2590 * fcoe_ctlr_disc_recv - start discovery for VN2VN mode.
2591 * @fip: The FCoE controller
2593 * This sets a flag indicating that remote ports should be created
2594 * and started for the peers we discover. We use the disc_callback
2595 * pointer as that flag. Peers already discovered are created here.
2597 * The lport lock is held during this call. The callback must be done
2598 * later, without holding either the lport or discovery locks.
2599 * The fcoe_ctlr lock may also be held during this call.
2601 static void fcoe_ctlr_disc_start(void (*callback)(struct fc_lport *,
2602 enum fc_disc_event),
2603 struct fc_lport *lport)
2605 struct fc_disc *disc = &lport->disc;
2606 struct fcoe_ctlr *fip = disc->priv;
2608 mutex_lock(&disc->disc_mutex);
2609 disc->disc_callback = callback;
2610 disc->disc_id = (disc->disc_id + 2) | 1;
2612 schedule_work(&fip->timer_work);
2613 mutex_unlock(&disc->disc_mutex);
2617 * fcoe_ctlr_vn_disc() - report FIP VN_port discovery results after claim state.
2618 * @fip: The FCoE controller
2620 * Starts the FLOGI and PLOGI login process to each discovered rport for which
2621 * we've received at least one beacon.
2622 * Performs the discovery complete callback.
2624 static void fcoe_ctlr_vn_disc(struct fcoe_ctlr *fip)
2626 struct fc_lport *lport = fip->lp;
2627 struct fc_disc *disc = &lport->disc;
2628 struct fc_rport_priv *rdata;
2629 struct fcoe_rport *frport;
2630 void (*callback)(struct fc_lport *, enum fc_disc_event);
2632 mutex_lock(&disc->disc_mutex);
2633 callback = disc->pending ? disc->disc_callback : NULL;
2635 list_for_each_entry_rcu(rdata, &disc->rports, peers) {
2636 frport = fcoe_ctlr_rport(rdata);
2638 lport->tt.rport_login(rdata);
2640 mutex_unlock(&disc->disc_mutex);
2642 callback(lport, DISC_EV_SUCCESS);
2646 * fcoe_ctlr_vn_timeout - timer work function for VN2VN mode.
2647 * @fip: The FCoE controller
2649 static void fcoe_ctlr_vn_timeout(struct fcoe_ctlr *fip)
2651 unsigned long next_time;
2653 u32 new_port_id = 0;
2655 mutex_lock(&fip->ctlr_mutex);
2656 switch (fip->state) {
2657 case FIP_ST_VNMP_START:
2658 fcoe_ctlr_set_state(fip, FIP_ST_VNMP_PROBE1);
2659 fcoe_ctlr_vn_send(fip, FIP_SC_VN_PROBE_REQ, fcoe_all_vn2vn, 0);
2660 next_time = jiffies + msecs_to_jiffies(FIP_VN_PROBE_WAIT);
2662 case FIP_ST_VNMP_PROBE1:
2663 fcoe_ctlr_set_state(fip, FIP_ST_VNMP_PROBE2);
2664 fcoe_ctlr_vn_send(fip, FIP_SC_VN_PROBE_REQ, fcoe_all_vn2vn, 0);
2665 next_time = jiffies + msecs_to_jiffies(FIP_VN_ANN_WAIT);
2667 case FIP_ST_VNMP_PROBE2:
2668 fcoe_ctlr_set_state(fip, FIP_ST_VNMP_CLAIM);
2669 new_port_id = fip->port_id;
2670 hton24(mac, FIP_VN_FC_MAP);
2671 hton24(mac + 3, new_port_id);
2672 fcoe_ctlr_map_dest(fip);
2673 fip->update_mac(fip->lp, mac);
2674 fcoe_ctlr_vn_send_claim(fip);
2675 next_time = jiffies + msecs_to_jiffies(FIP_VN_ANN_WAIT);
2677 case FIP_ST_VNMP_CLAIM:
2679 * This may be invoked either by starting discovery so don't
2680 * go to the next state unless it's been long enough.
2682 next_time = fip->sol_time + msecs_to_jiffies(FIP_VN_ANN_WAIT);
2683 if (time_after_eq(jiffies, next_time)) {
2684 fcoe_ctlr_set_state(fip, FIP_ST_VNMP_UP);
2685 fcoe_ctlr_vn_send(fip, FIP_SC_VN_BEACON,
2687 next_time = jiffies + msecs_to_jiffies(FIP_VN_ANN_WAIT);
2688 fip->port_ka_time = next_time;
2690 fcoe_ctlr_vn_disc(fip);
2692 case FIP_ST_VNMP_UP:
2693 next_time = fcoe_ctlr_vn_age(fip);
2694 if (time_after_eq(jiffies, fip->port_ka_time)) {
2695 fcoe_ctlr_vn_send(fip, FIP_SC_VN_BEACON,
2697 fip->port_ka_time = jiffies +
2698 msecs_to_jiffies(FIP_VN_BEACON_INT +
2699 (random32() % FIP_VN_BEACON_FUZZ));
2701 if (time_before(fip->port_ka_time, next_time))
2702 next_time = fip->port_ka_time;
2704 case FIP_ST_LINK_WAIT:
2707 WARN(1, "unexpected state %d\n", fip->state);
2710 mod_timer(&fip->timer, next_time);
2712 mutex_unlock(&fip->ctlr_mutex);
2714 /* If port ID is new, notify local port after dropping ctlr_mutex */
2716 fc_lport_set_local_id(fip->lp, new_port_id);
2720 * fcoe_libfc_config() - Sets up libfc related properties for local port
2721 * @lp: The local port to configure libfc for
2722 * @fip: The FCoE controller in use by the local port
2723 * @tt: The libfc function template
2724 * @init_fcp: If non-zero, the FCP portion of libfc should be initialized
2726 * Returns : 0 for success
2728 int fcoe_libfc_config(struct fc_lport *lport, struct fcoe_ctlr *fip,
2729 const struct libfc_function_template *tt, int init_fcp)
2731 /* Set the function pointers set by the LLDD */
2732 memcpy(&lport->tt, tt, sizeof(*tt));
2733 if (init_fcp && fc_fcp_init(lport))
2735 fc_exch_init(lport);
2736 fc_elsct_init(lport);
2737 fc_lport_init(lport);
2738 if (fip->mode == FIP_MODE_VN2VN)
2739 lport->rport_priv_size = sizeof(struct fcoe_rport);
2740 fc_rport_init(lport);
2741 if (fip->mode == FIP_MODE_VN2VN) {
2742 lport->point_to_multipoint = 1;
2743 lport->tt.disc_recv_req = fcoe_ctlr_disc_recv;
2744 lport->tt.disc_start = fcoe_ctlr_disc_start;
2745 lport->tt.disc_stop = fcoe_ctlr_disc_stop;
2746 lport->tt.disc_stop_final = fcoe_ctlr_disc_stop_final;
2747 mutex_init(&lport->disc.disc_mutex);
2748 INIT_LIST_HEAD(&lport->disc.rports);
2749 lport->disc.priv = fip;
2751 fc_disc_init(lport);
2755 EXPORT_SYMBOL_GPL(fcoe_libfc_config);