ALSA: hda - Add quirk for Dell Vostro 1220
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / scsi / fcoe / libfcoe.c
blob3440da48d1698d3b2fde2796d1df1ea333edd3a9
1 /*
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
12 * more details.
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>
43 #include <scsi/libfc.h>
44 #include <scsi/libfcoe.h>
46 MODULE_AUTHOR("Open-FCoE.org");
47 MODULE_DESCRIPTION("FIP discovery protocol support for FCoE HBAs");
48 MODULE_LICENSE("GPL v2");
50 #define FCOE_CTLR_MIN_FKA 500 /* min keep alive (mS) */
51 #define FCOE_CTLR_DEF_FKA FIP_DEF_FKA /* default keep alive (mS) */
53 static void fcoe_ctlr_timeout(unsigned long);
54 static void fcoe_ctlr_link_work(struct work_struct *);
55 static void fcoe_ctlr_recv_work(struct work_struct *);
57 static u8 fcoe_all_fcfs[ETH_ALEN] = FIP_ALL_FCF_MACS;
59 unsigned int libfcoe_debug_logging;
60 module_param_named(debug_logging, libfcoe_debug_logging, int, S_IRUGO|S_IWUSR);
61 MODULE_PARM_DESC(debug_logging, "a bit mask of logging levels");
63 #define LIBFCOE_LOGGING 0x01 /* General logging, not categorized */
64 #define LIBFCOE_FIP_LOGGING 0x02 /* FIP logging */
66 #define LIBFCOE_CHECK_LOGGING(LEVEL, CMD) \
67 do { \
68 if (unlikely(libfcoe_debug_logging & LEVEL)) \
69 do { \
70 CMD; \
71 } while (0); \
72 } while (0)
74 #define LIBFCOE_DBG(fmt, args...) \
75 LIBFCOE_CHECK_LOGGING(LIBFCOE_LOGGING, \
76 printk(KERN_INFO "libfcoe: " fmt, ##args);)
78 #define LIBFCOE_FIP_DBG(fip, fmt, args...) \
79 LIBFCOE_CHECK_LOGGING(LIBFCOE_FIP_LOGGING, \
80 printk(KERN_INFO "host%d: fip: " fmt, \
81 (fip)->lp->host->host_no, ##args);)
83 /**
84 * fcoe_ctlr_mtu_valid() - Check if a FCF's MTU is valid
85 * @fcf: The FCF to check
87 * Return non-zero if FCF fcoe_size has been validated.
89 static inline int fcoe_ctlr_mtu_valid(const struct fcoe_fcf *fcf)
91 return (fcf->flags & FIP_FL_SOL) != 0;
94 /**
95 * fcoe_ctlr_fcf_usable() - Check if a FCF is usable
96 * @fcf: The FCF to check
98 * Return non-zero if the FCF is usable.
100 static inline int fcoe_ctlr_fcf_usable(struct fcoe_fcf *fcf)
102 u16 flags = FIP_FL_SOL | FIP_FL_AVAIL;
104 return (fcf->flags & flags) == flags;
108 * fcoe_ctlr_init() - Initialize the FCoE Controller instance
109 * @fip: The FCoE controller to initialize
111 void fcoe_ctlr_init(struct fcoe_ctlr *fip)
113 fip->state = FIP_ST_LINK_WAIT;
114 fip->mode = FIP_ST_AUTO;
115 INIT_LIST_HEAD(&fip->fcfs);
116 spin_lock_init(&fip->lock);
117 fip->flogi_oxid = FC_XID_UNKNOWN;
118 setup_timer(&fip->timer, fcoe_ctlr_timeout, (unsigned long)fip);
119 INIT_WORK(&fip->link_work, fcoe_ctlr_link_work);
120 INIT_WORK(&fip->recv_work, fcoe_ctlr_recv_work);
121 skb_queue_head_init(&fip->fip_recv_list);
123 EXPORT_SYMBOL(fcoe_ctlr_init);
126 * fcoe_ctlr_reset_fcfs() - Reset and free all FCFs for a controller
127 * @fip: The FCoE controller whose FCFs are to be reset
129 * Called with &fcoe_ctlr lock held.
131 static void fcoe_ctlr_reset_fcfs(struct fcoe_ctlr *fip)
133 struct fcoe_fcf *fcf;
134 struct fcoe_fcf *next;
136 fip->sel_fcf = NULL;
137 list_for_each_entry_safe(fcf, next, &fip->fcfs, list) {
138 list_del(&fcf->list);
139 kfree(fcf);
141 fip->fcf_count = 0;
142 fip->sel_time = 0;
146 * fcoe_ctlr_destroy() - Disable and tear down a FCoE controller
147 * @fip: The FCoE controller to tear down
149 * This is called by FCoE drivers before freeing the &fcoe_ctlr.
151 * The receive handler will have been deleted before this to guarantee
152 * that no more recv_work will be scheduled.
154 * The timer routine will simply return once we set FIP_ST_DISABLED.
155 * This guarantees that no further timeouts or work will be scheduled.
157 void fcoe_ctlr_destroy(struct fcoe_ctlr *fip)
159 cancel_work_sync(&fip->recv_work);
160 skb_queue_purge(&fip->fip_recv_list);
162 spin_lock_bh(&fip->lock);
163 fip->state = FIP_ST_DISABLED;
164 fcoe_ctlr_reset_fcfs(fip);
165 spin_unlock_bh(&fip->lock);
166 del_timer_sync(&fip->timer);
167 cancel_work_sync(&fip->link_work);
169 EXPORT_SYMBOL(fcoe_ctlr_destroy);
172 * fcoe_ctlr_fcoe_size() - Return the maximum FCoE size required for VN_Port
173 * @fip: The FCoE controller to get the maximum FCoE size from
175 * Returns the maximum packet size including the FCoE header and trailer,
176 * but not including any Ethernet or VLAN headers.
178 static inline u32 fcoe_ctlr_fcoe_size(struct fcoe_ctlr *fip)
181 * Determine the max FCoE frame size allowed, including
182 * FCoE header and trailer.
183 * Note: lp->mfs is currently the payload size, not the frame size.
185 return fip->lp->mfs + sizeof(struct fc_frame_header) +
186 sizeof(struct fcoe_hdr) + sizeof(struct fcoe_crc_eof);
190 * fcoe_ctlr_solicit() - Send a FIP solicitation
191 * @fip: The FCoE controller to send the solicitation on
192 * @fcf: The destination FCF (if NULL, a multicast solicitation is sent)
194 static void fcoe_ctlr_solicit(struct fcoe_ctlr *fip, struct fcoe_fcf *fcf)
196 struct sk_buff *skb;
197 struct fip_sol {
198 struct ethhdr eth;
199 struct fip_header fip;
200 struct {
201 struct fip_mac_desc mac;
202 struct fip_wwn_desc wwnn;
203 struct fip_size_desc size;
204 } __attribute__((packed)) desc;
205 } __attribute__((packed)) *sol;
206 u32 fcoe_size;
208 skb = dev_alloc_skb(sizeof(*sol));
209 if (!skb)
210 return;
212 sol = (struct fip_sol *)skb->data;
214 memset(sol, 0, sizeof(*sol));
215 memcpy(sol->eth.h_dest, fcf ? fcf->fcf_mac : fcoe_all_fcfs, ETH_ALEN);
216 memcpy(sol->eth.h_source, fip->ctl_src_addr, ETH_ALEN);
217 sol->eth.h_proto = htons(ETH_P_FIP);
219 sol->fip.fip_ver = FIP_VER_ENCAPS(FIP_VER);
220 sol->fip.fip_op = htons(FIP_OP_DISC);
221 sol->fip.fip_subcode = FIP_SC_SOL;
222 sol->fip.fip_dl_len = htons(sizeof(sol->desc) / FIP_BPW);
223 sol->fip.fip_flags = htons(FIP_FL_FPMA);
224 if (fip->spma)
225 sol->fip.fip_flags |= htons(FIP_FL_SPMA);
227 sol->desc.mac.fd_desc.fip_dtype = FIP_DT_MAC;
228 sol->desc.mac.fd_desc.fip_dlen = sizeof(sol->desc.mac) / FIP_BPW;
229 memcpy(sol->desc.mac.fd_mac, fip->ctl_src_addr, ETH_ALEN);
231 sol->desc.wwnn.fd_desc.fip_dtype = FIP_DT_NAME;
232 sol->desc.wwnn.fd_desc.fip_dlen = sizeof(sol->desc.wwnn) / FIP_BPW;
233 put_unaligned_be64(fip->lp->wwnn, &sol->desc.wwnn.fd_wwn);
235 fcoe_size = fcoe_ctlr_fcoe_size(fip);
236 sol->desc.size.fd_desc.fip_dtype = FIP_DT_FCOE_SIZE;
237 sol->desc.size.fd_desc.fip_dlen = sizeof(sol->desc.size) / FIP_BPW;
238 sol->desc.size.fd_size = htons(fcoe_size);
240 skb_put(skb, sizeof(*sol));
241 skb->protocol = htons(ETH_P_FIP);
242 skb_reset_mac_header(skb);
243 skb_reset_network_header(skb);
244 fip->send(fip, skb);
246 if (!fcf)
247 fip->sol_time = jiffies;
251 * fcoe_ctlr_link_up() - Start FCoE controller
252 * @fip: The FCoE controller to start
254 * Called from the LLD when the network link is ready.
256 void fcoe_ctlr_link_up(struct fcoe_ctlr *fip)
258 spin_lock_bh(&fip->lock);
259 if (fip->state == FIP_ST_NON_FIP || fip->state == FIP_ST_AUTO) {
260 fip->last_link = 1;
261 fip->link = 1;
262 spin_unlock_bh(&fip->lock);
263 fc_linkup(fip->lp);
264 } else if (fip->state == FIP_ST_LINK_WAIT) {
265 fip->state = fip->mode;
266 fip->last_link = 1;
267 fip->link = 1;
268 spin_unlock_bh(&fip->lock);
269 if (fip->state == FIP_ST_AUTO)
270 LIBFCOE_FIP_DBG(fip, "%s", "setting AUTO mode.\n");
271 fc_linkup(fip->lp);
272 fcoe_ctlr_solicit(fip, NULL);
273 } else
274 spin_unlock_bh(&fip->lock);
276 EXPORT_SYMBOL(fcoe_ctlr_link_up);
279 * fcoe_ctlr_reset() - Reset a FCoE controller
280 * @fip: The FCoE controller to reset
282 static void fcoe_ctlr_reset(struct fcoe_ctlr *fip)
284 fcoe_ctlr_reset_fcfs(fip);
285 del_timer(&fip->timer);
286 fip->ctlr_ka_time = 0;
287 fip->port_ka_time = 0;
288 fip->sol_time = 0;
289 fip->flogi_oxid = FC_XID_UNKNOWN;
290 fip->map_dest = 0;
294 * fcoe_ctlr_link_down() - Stop a FCoE controller
295 * @fip: The FCoE controller to be stopped
297 * Returns non-zero if the link was up and now isn't.
299 * Called from the LLD when the network link is not ready.
300 * There may be multiple calls while the link is down.
302 int fcoe_ctlr_link_down(struct fcoe_ctlr *fip)
304 int link_dropped;
306 LIBFCOE_FIP_DBG(fip, "link down.\n");
307 spin_lock_bh(&fip->lock);
308 fcoe_ctlr_reset(fip);
309 link_dropped = fip->link;
310 fip->link = 0;
311 fip->last_link = 0;
312 fip->state = FIP_ST_LINK_WAIT;
313 spin_unlock_bh(&fip->lock);
315 if (link_dropped)
316 fc_linkdown(fip->lp);
317 return link_dropped;
319 EXPORT_SYMBOL(fcoe_ctlr_link_down);
322 * fcoe_ctlr_send_keep_alive() - Send a keep-alive to the selected FCF
323 * @fip: The FCoE controller to send the FKA on
324 * @lport: libfc fc_lport to send from
325 * @ports: 0 for controller keep-alive, 1 for port keep-alive
326 * @sa: The source MAC address
328 * A controller keep-alive is sent every fka_period (typically 8 seconds).
329 * The source MAC is the native MAC address.
331 * A port keep-alive is sent every 90 seconds while logged in.
332 * The source MAC is the assigned mapped source address.
333 * The destination is the FCF's F-port.
335 static void fcoe_ctlr_send_keep_alive(struct fcoe_ctlr *fip,
336 struct fc_lport *lport,
337 int ports, u8 *sa)
339 struct sk_buff *skb;
340 struct fip_kal {
341 struct ethhdr eth;
342 struct fip_header fip;
343 struct fip_mac_desc mac;
344 } __attribute__((packed)) *kal;
345 struct fip_vn_desc *vn;
346 u32 len;
347 struct fc_lport *lp;
348 struct fcoe_fcf *fcf;
350 fcf = fip->sel_fcf;
351 lp = fip->lp;
352 if (!fcf || !fc_host_port_id(lp->host))
353 return;
355 len = sizeof(*kal) + ports * sizeof(*vn);
356 skb = dev_alloc_skb(len);
357 if (!skb)
358 return;
360 kal = (struct fip_kal *)skb->data;
361 memset(kal, 0, len);
362 memcpy(kal->eth.h_dest, fcf->fcf_mac, ETH_ALEN);
363 memcpy(kal->eth.h_source, sa, ETH_ALEN);
364 kal->eth.h_proto = htons(ETH_P_FIP);
366 kal->fip.fip_ver = FIP_VER_ENCAPS(FIP_VER);
367 kal->fip.fip_op = htons(FIP_OP_CTRL);
368 kal->fip.fip_subcode = FIP_SC_KEEP_ALIVE;
369 kal->fip.fip_dl_len = htons((sizeof(kal->mac) +
370 ports * sizeof(*vn)) / FIP_BPW);
371 kal->fip.fip_flags = htons(FIP_FL_FPMA);
372 if (fip->spma)
373 kal->fip.fip_flags |= htons(FIP_FL_SPMA);
375 kal->mac.fd_desc.fip_dtype = FIP_DT_MAC;
376 kal->mac.fd_desc.fip_dlen = sizeof(kal->mac) / FIP_BPW;
377 memcpy(kal->mac.fd_mac, fip->ctl_src_addr, ETH_ALEN);
378 if (ports) {
379 vn = (struct fip_vn_desc *)(kal + 1);
380 vn->fd_desc.fip_dtype = FIP_DT_VN_ID;
381 vn->fd_desc.fip_dlen = sizeof(*vn) / FIP_BPW;
382 memcpy(vn->fd_mac, fip->get_src_addr(lport), ETH_ALEN);
383 hton24(vn->fd_fc_id, fc_host_port_id(lp->host));
384 put_unaligned_be64(lp->wwpn, &vn->fd_wwpn);
386 skb_put(skb, len);
387 skb->protocol = htons(ETH_P_FIP);
388 skb_reset_mac_header(skb);
389 skb_reset_network_header(skb);
390 fip->send(fip, skb);
394 * fcoe_ctlr_encaps() - Encapsulate an ELS frame for FIP, without sending it
395 * @fip: The FCoE controller for the ELS frame
396 * @dtype: The FIP descriptor type for the frame
397 * @skb: The FCoE ELS frame including FC header but no FCoE headers
399 * Returns non-zero error code on failure.
401 * The caller must check that the length is a multiple of 4.
403 * The @skb must have enough headroom (28 bytes) and tailroom (8 bytes).
404 * Headroom includes the FIP encapsulation description, FIP header, and
405 * Ethernet header. The tailroom is for the FIP MAC descriptor.
407 static int fcoe_ctlr_encaps(struct fcoe_ctlr *fip, struct fc_lport *lport,
408 u8 dtype, struct sk_buff *skb)
410 struct fip_encaps_head {
411 struct ethhdr eth;
412 struct fip_header fip;
413 struct fip_encaps encaps;
414 } __attribute__((packed)) *cap;
415 struct fip_mac_desc *mac;
416 struct fcoe_fcf *fcf;
417 size_t dlen;
418 u16 fip_flags;
420 fcf = fip->sel_fcf;
421 if (!fcf)
422 return -ENODEV;
424 /* set flags according to both FCF and lport's capability on SPMA */
425 fip_flags = fcf->flags;
426 fip_flags &= fip->spma ? FIP_FL_SPMA | FIP_FL_FPMA : FIP_FL_FPMA;
427 if (!fip_flags)
428 return -ENODEV;
430 dlen = sizeof(struct fip_encaps) + skb->len; /* len before push */
431 cap = (struct fip_encaps_head *)skb_push(skb, sizeof(*cap));
433 memset(cap, 0, sizeof(*cap));
434 memcpy(cap->eth.h_dest, fcf->fcf_mac, ETH_ALEN);
435 memcpy(cap->eth.h_source, fip->ctl_src_addr, ETH_ALEN);
436 cap->eth.h_proto = htons(ETH_P_FIP);
438 cap->fip.fip_ver = FIP_VER_ENCAPS(FIP_VER);
439 cap->fip.fip_op = htons(FIP_OP_LS);
440 cap->fip.fip_subcode = FIP_SC_REQ;
441 cap->fip.fip_dl_len = htons((dlen + sizeof(*mac)) / FIP_BPW);
442 cap->fip.fip_flags = htons(fip_flags);
444 cap->encaps.fd_desc.fip_dtype = dtype;
445 cap->encaps.fd_desc.fip_dlen = dlen / FIP_BPW;
447 mac = (struct fip_mac_desc *)skb_put(skb, sizeof(*mac));
448 memset(mac, 0, sizeof(mac));
449 mac->fd_desc.fip_dtype = FIP_DT_MAC;
450 mac->fd_desc.fip_dlen = sizeof(*mac) / FIP_BPW;
451 if (dtype != FIP_DT_FLOGI && dtype != FIP_DT_FDISC)
452 memcpy(mac->fd_mac, fip->get_src_addr(lport), ETH_ALEN);
453 else if (fip->spma)
454 memcpy(mac->fd_mac, fip->ctl_src_addr, ETH_ALEN);
456 skb->protocol = htons(ETH_P_FIP);
457 skb_reset_mac_header(skb);
458 skb_reset_network_header(skb);
459 return 0;
463 * fcoe_ctlr_els_send() - Send an ELS frame encapsulated by FIP if appropriate.
464 * @fip: FCoE controller.
465 * @lport: libfc fc_lport to send from
466 * @skb: FCoE ELS frame including FC header but no FCoE headers.
468 * Returns a non-zero error code if the frame should not be sent.
469 * Returns zero if the caller should send the frame with FCoE encapsulation.
471 * The caller must check that the length is a multiple of 4.
472 * The SKB must have enough headroom (28 bytes) and tailroom (8 bytes).
474 int fcoe_ctlr_els_send(struct fcoe_ctlr *fip, struct fc_lport *lport,
475 struct sk_buff *skb)
477 struct fc_frame_header *fh;
478 u16 old_xid;
479 u8 op;
480 u8 mac[ETH_ALEN];
482 fh = (struct fc_frame_header *)skb->data;
483 op = *(u8 *)(fh + 1);
485 if (op == ELS_FLOGI) {
486 old_xid = fip->flogi_oxid;
487 fip->flogi_oxid = ntohs(fh->fh_ox_id);
488 if (fip->state == FIP_ST_AUTO) {
489 if (old_xid == FC_XID_UNKNOWN)
490 fip->flogi_count = 0;
491 fip->flogi_count++;
492 if (fip->flogi_count < 3)
493 goto drop;
494 fip->map_dest = 1;
495 return 0;
497 if (fip->state == FIP_ST_NON_FIP)
498 fip->map_dest = 1;
501 if (fip->state == FIP_ST_NON_FIP)
502 return 0;
503 if (!fip->sel_fcf)
504 goto drop;
506 switch (op) {
507 case ELS_FLOGI:
508 op = FIP_DT_FLOGI;
509 break;
510 case ELS_FDISC:
511 if (ntoh24(fh->fh_s_id))
512 return 0;
513 op = FIP_DT_FDISC;
514 break;
515 case ELS_LOGO:
516 if (fip->state != FIP_ST_ENABLED)
517 return 0;
518 if (ntoh24(fh->fh_d_id) != FC_FID_FLOGI)
519 return 0;
520 op = FIP_DT_LOGO;
521 break;
522 case ELS_LS_ACC:
523 if (fip->flogi_oxid == FC_XID_UNKNOWN)
524 return 0;
525 if (!ntoh24(fh->fh_s_id))
526 return 0;
527 if (fip->state == FIP_ST_AUTO)
528 return 0;
530 * Here we must've gotten an SID by accepting an FLOGI
531 * from a point-to-point connection. Switch to using
532 * the source mac based on the SID. The destination
533 * MAC in this case would have been set by receving the
534 * FLOGI.
536 fip->flogi_oxid = FC_XID_UNKNOWN;
537 fc_fcoe_set_mac(mac, fh->fh_d_id);
538 fip->update_mac(lport, mac);
539 return 0;
540 default:
541 if (fip->state != FIP_ST_ENABLED)
542 goto drop;
543 return 0;
545 if (fcoe_ctlr_encaps(fip, lport, op, skb))
546 goto drop;
547 fip->send(fip, skb);
548 return -EINPROGRESS;
549 drop:
550 kfree_skb(skb);
551 return -EINVAL;
553 EXPORT_SYMBOL(fcoe_ctlr_els_send);
556 * fcoe_ctlr_age_fcfs() - Reset and free all old FCFs for a controller
557 * @fip: The FCoE controller to free FCFs on
559 * Called with lock held.
561 * An FCF is considered old if we have missed three advertisements.
562 * That is, there have been no valid advertisement from it for three
563 * times its keep-alive period including fuzz.
565 * In addition, determine the time when an FCF selection can occur.
567 * Also, increment the MissDiscAdvCount when no advertisement is received
568 * for the corresponding FCF for 1.5 * FKA_ADV_PERIOD (FC-BB-5 LESB).
570 static void fcoe_ctlr_age_fcfs(struct fcoe_ctlr *fip)
572 struct fcoe_fcf *fcf;
573 struct fcoe_fcf *next;
574 unsigned long sel_time = 0;
575 unsigned long mda_time = 0;
577 list_for_each_entry_safe(fcf, next, &fip->fcfs, list) {
578 mda_time = fcf->fka_period + (fcf->fka_period >> 1);
579 if ((fip->sel_fcf == fcf) &&
580 (time_after(jiffies, fcf->time + mda_time))) {
581 mod_timer(&fip->timer, jiffies + mda_time);
582 fc_lport_get_stats(fip->lp)->MissDiscAdvCount++;
583 printk(KERN_INFO "libfcoe: host%d: Missing Discovery "
584 "Advertisement for fab %llx count %lld\n",
585 fip->lp->host->host_no, fcf->fabric_name,
586 fc_lport_get_stats(fip->lp)->MissDiscAdvCount);
588 if (time_after(jiffies, fcf->time + fcf->fka_period * 3 +
589 msecs_to_jiffies(FIP_FCF_FUZZ * 3))) {
590 if (fip->sel_fcf == fcf)
591 fip->sel_fcf = NULL;
592 list_del(&fcf->list);
593 WARN_ON(!fip->fcf_count);
594 fip->fcf_count--;
595 kfree(fcf);
596 fc_lport_get_stats(fip->lp)->VLinkFailureCount++;
597 } else if (fcoe_ctlr_mtu_valid(fcf) &&
598 (!sel_time || time_before(sel_time, fcf->time))) {
599 sel_time = fcf->time;
602 if (sel_time) {
603 sel_time += msecs_to_jiffies(FCOE_CTLR_START_DELAY);
604 fip->sel_time = sel_time;
605 if (time_before(sel_time, fip->timer.expires))
606 mod_timer(&fip->timer, sel_time);
607 } else {
608 fip->sel_time = 0;
613 * fcoe_ctlr_parse_adv() - Decode a FIP advertisement into a new FCF entry
614 * @fip: The FCoE controller receiving the advertisement
615 * @skb: The received FIP advertisement frame
616 * @fcf: The resulting FCF entry
618 * Returns zero on a valid parsed advertisement,
619 * otherwise returns non zero value.
621 static int fcoe_ctlr_parse_adv(struct fcoe_ctlr *fip,
622 struct sk_buff *skb, struct fcoe_fcf *fcf)
624 struct fip_header *fiph;
625 struct fip_desc *desc = NULL;
626 struct fip_wwn_desc *wwn;
627 struct fip_fab_desc *fab;
628 struct fip_fka_desc *fka;
629 unsigned long t;
630 size_t rlen;
631 size_t dlen;
633 memset(fcf, 0, sizeof(*fcf));
634 fcf->fka_period = msecs_to_jiffies(FCOE_CTLR_DEF_FKA);
636 fiph = (struct fip_header *)skb->data;
637 fcf->flags = ntohs(fiph->fip_flags);
639 rlen = ntohs(fiph->fip_dl_len) * 4;
640 if (rlen + sizeof(*fiph) > skb->len)
641 return -EINVAL;
643 desc = (struct fip_desc *)(fiph + 1);
644 while (rlen > 0) {
645 dlen = desc->fip_dlen * FIP_BPW;
646 if (dlen < sizeof(*desc) || dlen > rlen)
647 return -EINVAL;
648 switch (desc->fip_dtype) {
649 case FIP_DT_PRI:
650 if (dlen != sizeof(struct fip_pri_desc))
651 goto len_err;
652 fcf->pri = ((struct fip_pri_desc *)desc)->fd_pri;
653 break;
654 case FIP_DT_MAC:
655 if (dlen != sizeof(struct fip_mac_desc))
656 goto len_err;
657 memcpy(fcf->fcf_mac,
658 ((struct fip_mac_desc *)desc)->fd_mac,
659 ETH_ALEN);
660 if (!is_valid_ether_addr(fcf->fcf_mac)) {
661 LIBFCOE_FIP_DBG(fip, "Invalid MAC address "
662 "in FIP adv\n");
663 return -EINVAL;
665 break;
666 case FIP_DT_NAME:
667 if (dlen != sizeof(struct fip_wwn_desc))
668 goto len_err;
669 wwn = (struct fip_wwn_desc *)desc;
670 fcf->switch_name = get_unaligned_be64(&wwn->fd_wwn);
671 break;
672 case FIP_DT_FAB:
673 if (dlen != sizeof(struct fip_fab_desc))
674 goto len_err;
675 fab = (struct fip_fab_desc *)desc;
676 fcf->fabric_name = get_unaligned_be64(&fab->fd_wwn);
677 fcf->vfid = ntohs(fab->fd_vfid);
678 fcf->fc_map = ntoh24(fab->fd_map);
679 break;
680 case FIP_DT_FKA:
681 if (dlen != sizeof(struct fip_fka_desc))
682 goto len_err;
683 fka = (struct fip_fka_desc *)desc;
684 if (fka->fd_flags & FIP_FKA_ADV_D)
685 fcf->fd_flags = 1;
686 t = ntohl(fka->fd_fka_period);
687 if (t >= FCOE_CTLR_MIN_FKA)
688 fcf->fka_period = msecs_to_jiffies(t);
689 break;
690 case FIP_DT_MAP_OUI:
691 case FIP_DT_FCOE_SIZE:
692 case FIP_DT_FLOGI:
693 case FIP_DT_FDISC:
694 case FIP_DT_LOGO:
695 case FIP_DT_ELP:
696 default:
697 LIBFCOE_FIP_DBG(fip, "unexpected descriptor type %x "
698 "in FIP adv\n", desc->fip_dtype);
699 /* standard says ignore unknown descriptors >= 128 */
700 if (desc->fip_dtype < FIP_DT_VENDOR_BASE)
701 return -EINVAL;
702 continue;
704 desc = (struct fip_desc *)((char *)desc + dlen);
705 rlen -= dlen;
707 if (!fcf->fc_map || (fcf->fc_map & 0x10000))
708 return -EINVAL;
709 if (!fcf->switch_name || !fcf->fabric_name)
710 return -EINVAL;
711 return 0;
713 len_err:
714 LIBFCOE_FIP_DBG(fip, "FIP length error in descriptor type %x len %zu\n",
715 desc->fip_dtype, dlen);
716 return -EINVAL;
720 * fcoe_ctlr_recv_adv() - Handle an incoming advertisement
721 * @fip: The FCoE controller receiving the advertisement
722 * @skb: The received FIP packet
724 static void fcoe_ctlr_recv_adv(struct fcoe_ctlr *fip, struct sk_buff *skb)
726 struct fcoe_fcf *fcf;
727 struct fcoe_fcf new;
728 struct fcoe_fcf *found;
729 unsigned long sol_tov = msecs_to_jiffies(FCOE_CTRL_SOL_TOV);
730 int first = 0;
731 int mtu_valid;
733 if (fcoe_ctlr_parse_adv(fip, skb, &new))
734 return;
736 spin_lock_bh(&fip->lock);
737 first = list_empty(&fip->fcfs);
738 found = NULL;
739 list_for_each_entry(fcf, &fip->fcfs, list) {
740 if (fcf->switch_name == new.switch_name &&
741 fcf->fabric_name == new.fabric_name &&
742 fcf->fc_map == new.fc_map &&
743 compare_ether_addr(fcf->fcf_mac, new.fcf_mac) == 0) {
744 found = fcf;
745 break;
748 if (!found) {
749 if (fip->fcf_count >= FCOE_CTLR_FCF_LIMIT)
750 goto out;
752 fcf = kmalloc(sizeof(*fcf), GFP_ATOMIC);
753 if (!fcf)
754 goto out;
756 fip->fcf_count++;
757 memcpy(fcf, &new, sizeof(new));
758 list_add(&fcf->list, &fip->fcfs);
759 } else {
761 * Flags in advertisements are ignored once the FCF is
762 * selected. Flags in unsolicited advertisements are
763 * ignored after a usable solicited advertisement
764 * has been received.
766 if (fcf == fip->sel_fcf) {
767 fip->ctlr_ka_time -= fcf->fka_period;
768 fip->ctlr_ka_time += new.fka_period;
769 if (time_before(fip->ctlr_ka_time, fip->timer.expires))
770 mod_timer(&fip->timer, fip->ctlr_ka_time);
771 } else if (!fcoe_ctlr_fcf_usable(fcf))
772 fcf->flags = new.flags;
773 fcf->fka_period = new.fka_period;
774 memcpy(fcf->fcf_mac, new.fcf_mac, ETH_ALEN);
776 mtu_valid = fcoe_ctlr_mtu_valid(fcf);
777 fcf->time = jiffies;
778 if (!found) {
779 LIBFCOE_FIP_DBG(fip, "New FCF for fab %llx map %x val %d\n",
780 fcf->fabric_name, fcf->fc_map, mtu_valid);
784 * If this advertisement is not solicited and our max receive size
785 * hasn't been verified, send a solicited advertisement.
787 if (!mtu_valid)
788 fcoe_ctlr_solicit(fip, fcf);
791 * If its been a while since we did a solicit, and this is
792 * the first advertisement we've received, do a multicast
793 * solicitation to gather as many advertisements as we can
794 * before selection occurs.
796 if (first && time_after(jiffies, fip->sol_time + sol_tov))
797 fcoe_ctlr_solicit(fip, NULL);
800 * If this is the first validated FCF, note the time and
801 * set a timer to trigger selection.
803 if (mtu_valid && !fip->sel_time && fcoe_ctlr_fcf_usable(fcf)) {
804 fip->sel_time = jiffies +
805 msecs_to_jiffies(FCOE_CTLR_START_DELAY);
806 if (!timer_pending(&fip->timer) ||
807 time_before(fip->sel_time, fip->timer.expires))
808 mod_timer(&fip->timer, fip->sel_time);
810 out:
811 spin_unlock_bh(&fip->lock);
815 * fcoe_ctlr_recv_els() - Handle an incoming FIP encapsulated ELS frame
816 * @fip: The FCoE controller which received the packet
817 * @skb: The received FIP packet
819 static void fcoe_ctlr_recv_els(struct fcoe_ctlr *fip, struct sk_buff *skb)
821 struct fc_lport *lport = fip->lp;
822 struct fip_header *fiph;
823 struct fc_frame *fp = (struct fc_frame *)skb;
824 struct fc_frame_header *fh = NULL;
825 struct fip_desc *desc;
826 struct fip_encaps *els;
827 struct fcoe_dev_stats *stats;
828 enum fip_desc_type els_dtype = 0;
829 u8 els_op;
830 u8 sub;
831 u8 granted_mac[ETH_ALEN] = { 0 };
832 size_t els_len = 0;
833 size_t rlen;
834 size_t dlen;
836 fiph = (struct fip_header *)skb->data;
837 sub = fiph->fip_subcode;
838 if (sub != FIP_SC_REQ && sub != FIP_SC_REP)
839 goto drop;
841 rlen = ntohs(fiph->fip_dl_len) * 4;
842 if (rlen + sizeof(*fiph) > skb->len)
843 goto drop;
845 desc = (struct fip_desc *)(fiph + 1);
846 while (rlen > 0) {
847 dlen = desc->fip_dlen * FIP_BPW;
848 if (dlen < sizeof(*desc) || dlen > rlen)
849 goto drop;
850 switch (desc->fip_dtype) {
851 case FIP_DT_MAC:
852 if (dlen != sizeof(struct fip_mac_desc))
853 goto len_err;
854 memcpy(granted_mac,
855 ((struct fip_mac_desc *)desc)->fd_mac,
856 ETH_ALEN);
857 if (!is_valid_ether_addr(granted_mac)) {
858 LIBFCOE_FIP_DBG(fip, "Invalid MAC address "
859 "in FIP ELS\n");
860 goto drop;
862 memcpy(fr_cb(fp)->granted_mac, granted_mac, ETH_ALEN);
863 break;
864 case FIP_DT_FLOGI:
865 case FIP_DT_FDISC:
866 case FIP_DT_LOGO:
867 case FIP_DT_ELP:
868 if (fh)
869 goto drop;
870 if (dlen < sizeof(*els) + sizeof(*fh) + 1)
871 goto len_err;
872 els_len = dlen - sizeof(*els);
873 els = (struct fip_encaps *)desc;
874 fh = (struct fc_frame_header *)(els + 1);
875 els_dtype = desc->fip_dtype;
876 break;
877 default:
878 LIBFCOE_FIP_DBG(fip, "unexpected descriptor type %x "
879 "in FIP adv\n", desc->fip_dtype);
880 /* standard says ignore unknown descriptors >= 128 */
881 if (desc->fip_dtype < FIP_DT_VENDOR_BASE)
882 goto drop;
883 continue;
885 desc = (struct fip_desc *)((char *)desc + dlen);
886 rlen -= dlen;
889 if (!fh)
890 goto drop;
891 els_op = *(u8 *)(fh + 1);
893 if (els_dtype == FIP_DT_FLOGI && sub == FIP_SC_REP &&
894 fip->flogi_oxid == ntohs(fh->fh_ox_id) &&
895 els_op == ELS_LS_ACC && is_valid_ether_addr(granted_mac))
896 fip->flogi_oxid = FC_XID_UNKNOWN;
899 * Convert skb into an fc_frame containing only the ELS.
901 skb_pull(skb, (u8 *)fh - skb->data);
902 skb_trim(skb, els_len);
903 fp = (struct fc_frame *)skb;
904 fc_frame_init(fp);
905 fr_sof(fp) = FC_SOF_I3;
906 fr_eof(fp) = FC_EOF_T;
907 fr_dev(fp) = lport;
909 stats = fc_lport_get_stats(lport);
910 stats->RxFrames++;
911 stats->RxWords += skb->len / FIP_BPW;
913 fc_exch_recv(lport, fp);
914 return;
916 len_err:
917 LIBFCOE_FIP_DBG(fip, "FIP length error in descriptor type %x len %zu\n",
918 desc->fip_dtype, dlen);
919 drop:
920 kfree_skb(skb);
924 * fcoe_ctlr_recv_els() - Handle an incoming link reset frame
925 * @fip: The FCoE controller that received the frame
926 * @fh: The received FIP header
928 * There may be multiple VN_Port descriptors.
929 * The overall length has already been checked.
931 static void fcoe_ctlr_recv_clr_vlink(struct fcoe_ctlr *fip,
932 struct fip_header *fh)
934 struct fip_desc *desc;
935 struct fip_mac_desc *mp;
936 struct fip_wwn_desc *wp;
937 struct fip_vn_desc *vp;
938 size_t rlen;
939 size_t dlen;
940 struct fcoe_fcf *fcf = fip->sel_fcf;
941 struct fc_lport *lport = fip->lp;
942 u32 desc_mask;
944 LIBFCOE_FIP_DBG(fip, "Clear Virtual Link received\n");
945 if (!fcf)
946 return;
947 if (!fcf || !fc_host_port_id(lport->host))
948 return;
951 * mask of required descriptors. Validating each one clears its bit.
953 desc_mask = BIT(FIP_DT_MAC) | BIT(FIP_DT_NAME) | BIT(FIP_DT_VN_ID);
955 rlen = ntohs(fh->fip_dl_len) * FIP_BPW;
956 desc = (struct fip_desc *)(fh + 1);
957 while (rlen >= sizeof(*desc)) {
958 dlen = desc->fip_dlen * FIP_BPW;
959 if (dlen > rlen)
960 return;
961 switch (desc->fip_dtype) {
962 case FIP_DT_MAC:
963 mp = (struct fip_mac_desc *)desc;
964 if (dlen < sizeof(*mp))
965 return;
966 if (compare_ether_addr(mp->fd_mac, fcf->fcf_mac))
967 return;
968 desc_mask &= ~BIT(FIP_DT_MAC);
969 break;
970 case FIP_DT_NAME:
971 wp = (struct fip_wwn_desc *)desc;
972 if (dlen < sizeof(*wp))
973 return;
974 if (get_unaligned_be64(&wp->fd_wwn) != fcf->switch_name)
975 return;
976 desc_mask &= ~BIT(FIP_DT_NAME);
977 break;
978 case FIP_DT_VN_ID:
979 vp = (struct fip_vn_desc *)desc;
980 if (dlen < sizeof(*vp))
981 return;
982 if (compare_ether_addr(vp->fd_mac,
983 fip->get_src_addr(lport)) == 0 &&
984 get_unaligned_be64(&vp->fd_wwpn) == lport->wwpn &&
985 ntoh24(vp->fd_fc_id) ==
986 fc_host_port_id(lport->host))
987 desc_mask &= ~BIT(FIP_DT_VN_ID);
988 break;
989 default:
990 /* standard says ignore unknown descriptors >= 128 */
991 if (desc->fip_dtype < FIP_DT_VENDOR_BASE)
992 return;
993 break;
995 desc = (struct fip_desc *)((char *)desc + dlen);
996 rlen -= dlen;
1000 * reset only if all required descriptors were present and valid.
1002 if (desc_mask) {
1003 LIBFCOE_FIP_DBG(fip, "missing descriptors mask %x\n",
1004 desc_mask);
1005 } else {
1006 LIBFCOE_FIP_DBG(fip, "performing Clear Virtual Link\n");
1008 spin_lock_bh(&fip->lock);
1009 fc_lport_get_stats(lport)->VLinkFailureCount++;
1010 fcoe_ctlr_reset(fip);
1011 spin_unlock_bh(&fip->lock);
1013 fc_lport_reset(fip->lp);
1014 fcoe_ctlr_solicit(fip, NULL);
1019 * fcoe_ctlr_recv() - Receive a FIP packet
1020 * @fip: The FCoE controller that received the packet
1021 * @skb: The received FIP packet
1023 * This may be called from either NET_RX_SOFTIRQ or IRQ.
1025 void fcoe_ctlr_recv(struct fcoe_ctlr *fip, struct sk_buff *skb)
1027 skb_queue_tail(&fip->fip_recv_list, skb);
1028 schedule_work(&fip->recv_work);
1030 EXPORT_SYMBOL(fcoe_ctlr_recv);
1033 * fcoe_ctlr_recv_handler() - Receive a FIP frame
1034 * @fip: The FCoE controller that received the frame
1035 * @skb: The received FIP frame
1037 * Returns non-zero if the frame is dropped.
1039 static int fcoe_ctlr_recv_handler(struct fcoe_ctlr *fip, struct sk_buff *skb)
1041 struct fip_header *fiph;
1042 struct ethhdr *eh;
1043 enum fip_state state;
1044 u16 op;
1045 u8 sub;
1047 if (skb_linearize(skb))
1048 goto drop;
1049 if (skb->len < sizeof(*fiph))
1050 goto drop;
1051 eh = eth_hdr(skb);
1052 if (compare_ether_addr(eh->h_dest, fip->ctl_src_addr) &&
1053 compare_ether_addr(eh->h_dest, FIP_ALL_ENODE_MACS))
1054 goto drop;
1055 fiph = (struct fip_header *)skb->data;
1056 op = ntohs(fiph->fip_op);
1057 sub = fiph->fip_subcode;
1059 if (FIP_VER_DECAPS(fiph->fip_ver) != FIP_VER)
1060 goto drop;
1061 if (ntohs(fiph->fip_dl_len) * FIP_BPW + sizeof(*fiph) > skb->len)
1062 goto drop;
1064 spin_lock_bh(&fip->lock);
1065 state = fip->state;
1066 if (state == FIP_ST_AUTO) {
1067 fip->map_dest = 0;
1068 fip->state = FIP_ST_ENABLED;
1069 state = FIP_ST_ENABLED;
1070 LIBFCOE_FIP_DBG(fip, "Using FIP mode\n");
1072 spin_unlock_bh(&fip->lock);
1073 if (state != FIP_ST_ENABLED)
1074 goto drop;
1076 if (op == FIP_OP_LS) {
1077 fcoe_ctlr_recv_els(fip, skb); /* consumes skb */
1078 return 0;
1080 if (op == FIP_OP_DISC && sub == FIP_SC_ADV)
1081 fcoe_ctlr_recv_adv(fip, skb);
1082 else if (op == FIP_OP_CTRL && sub == FIP_SC_CLR_VLINK)
1083 fcoe_ctlr_recv_clr_vlink(fip, fiph);
1084 kfree_skb(skb);
1085 return 0;
1086 drop:
1087 kfree_skb(skb);
1088 return -1;
1092 * fcoe_ctlr_select() - Select the best FCF (if possible)
1093 * @fip: The FCoE controller
1095 * If there are conflicting advertisements, no FCF can be chosen.
1097 * Called with lock held.
1099 static void fcoe_ctlr_select(struct fcoe_ctlr *fip)
1101 struct fcoe_fcf *fcf;
1102 struct fcoe_fcf *best = NULL;
1104 list_for_each_entry(fcf, &fip->fcfs, list) {
1105 LIBFCOE_FIP_DBG(fip, "consider FCF for fab %llx VFID %d map %x "
1106 "val %d\n", fcf->fabric_name, fcf->vfid,
1107 fcf->fc_map, fcoe_ctlr_mtu_valid(fcf));
1108 if (!fcoe_ctlr_fcf_usable(fcf)) {
1109 LIBFCOE_FIP_DBG(fip, "FCF for fab %llx map %x %svalid "
1110 "%savailable\n", fcf->fabric_name,
1111 fcf->fc_map, (fcf->flags & FIP_FL_SOL)
1112 ? "" : "in", (fcf->flags & FIP_FL_AVAIL)
1113 ? "" : "un");
1114 continue;
1116 if (!best) {
1117 best = fcf;
1118 continue;
1120 if (fcf->fabric_name != best->fabric_name ||
1121 fcf->vfid != best->vfid ||
1122 fcf->fc_map != best->fc_map) {
1123 LIBFCOE_FIP_DBG(fip, "Conflicting fabric, VFID, "
1124 "or FC-MAP\n");
1125 return;
1127 if (fcf->pri < best->pri)
1128 best = fcf;
1130 fip->sel_fcf = best;
1134 * fcoe_ctlr_timeout() - FIP timeout handler
1135 * @arg: The FCoE controller that timed out
1137 * Ages FCFs. Triggers FCF selection if possible. Sends keep-alives.
1139 static void fcoe_ctlr_timeout(unsigned long arg)
1141 struct fcoe_ctlr *fip = (struct fcoe_ctlr *)arg;
1142 struct fcoe_fcf *sel;
1143 struct fcoe_fcf *fcf;
1144 unsigned long next_timer = jiffies + msecs_to_jiffies(FIP_VN_KA_PERIOD);
1146 spin_lock_bh(&fip->lock);
1147 if (fip->state == FIP_ST_DISABLED) {
1148 spin_unlock_bh(&fip->lock);
1149 return;
1152 fcf = fip->sel_fcf;
1153 fcoe_ctlr_age_fcfs(fip);
1155 sel = fip->sel_fcf;
1156 if (!sel && fip->sel_time && time_after_eq(jiffies, fip->sel_time)) {
1157 fcoe_ctlr_select(fip);
1158 sel = fip->sel_fcf;
1159 fip->sel_time = 0;
1162 if (sel != fcf) {
1163 fcf = sel; /* the old FCF may have been freed */
1164 if (sel) {
1165 printk(KERN_INFO "libfcoe: host%d: FIP selected "
1166 "Fibre-Channel Forwarder MAC %pM\n",
1167 fip->lp->host->host_no, sel->fcf_mac);
1168 memcpy(fip->dest_addr, sel->fcf_mac, ETH_ALEN);
1169 fip->port_ka_time = jiffies +
1170 msecs_to_jiffies(FIP_VN_KA_PERIOD);
1171 fip->ctlr_ka_time = jiffies + sel->fka_period;
1172 } else {
1173 printk(KERN_NOTICE "libfcoe: host%d: "
1174 "FIP Fibre-Channel Forwarder timed out. "
1175 "Starting FCF discovery.\n",
1176 fip->lp->host->host_no);
1177 fip->reset_req = 1;
1178 schedule_work(&fip->link_work);
1182 if (sel && !sel->fd_flags) {
1183 if (time_after_eq(jiffies, fip->ctlr_ka_time)) {
1184 fip->ctlr_ka_time = jiffies + sel->fka_period;
1185 fip->send_ctlr_ka = 1;
1187 if (time_after(next_timer, fip->ctlr_ka_time))
1188 next_timer = fip->ctlr_ka_time;
1190 if (time_after_eq(jiffies, fip->port_ka_time)) {
1191 fip->port_ka_time = jiffies +
1192 msecs_to_jiffies(FIP_VN_KA_PERIOD);
1193 fip->send_port_ka = 1;
1195 if (time_after(next_timer, fip->port_ka_time))
1196 next_timer = fip->port_ka_time;
1197 mod_timer(&fip->timer, next_timer);
1198 } else if (fip->sel_time) {
1199 next_timer = fip->sel_time +
1200 msecs_to_jiffies(FCOE_CTLR_START_DELAY);
1201 mod_timer(&fip->timer, next_timer);
1203 if (fip->send_ctlr_ka || fip->send_port_ka)
1204 schedule_work(&fip->link_work);
1205 spin_unlock_bh(&fip->lock);
1209 * fcoe_ctlr_link_work() - Worker thread function for link changes
1210 * @work: Handle to a FCoE controller
1212 * See if the link status has changed and if so, report it.
1214 * This is here because fc_linkup() and fc_linkdown() must not
1215 * be called from the timer directly, since they use a mutex.
1217 static void fcoe_ctlr_link_work(struct work_struct *work)
1219 struct fcoe_ctlr *fip;
1220 struct fc_lport *vport;
1221 u8 *mac;
1222 int link;
1223 int last_link;
1224 int reset;
1226 fip = container_of(work, struct fcoe_ctlr, link_work);
1227 spin_lock_bh(&fip->lock);
1228 last_link = fip->last_link;
1229 link = fip->link;
1230 fip->last_link = link;
1231 reset = fip->reset_req;
1232 fip->reset_req = 0;
1233 spin_unlock_bh(&fip->lock);
1235 if (last_link != link) {
1236 if (link)
1237 fc_linkup(fip->lp);
1238 else
1239 fc_linkdown(fip->lp);
1240 } else if (reset && link)
1241 fc_lport_reset(fip->lp);
1243 if (fip->send_ctlr_ka) {
1244 fip->send_ctlr_ka = 0;
1245 fcoe_ctlr_send_keep_alive(fip, NULL, 0, fip->ctl_src_addr);
1247 if (fip->send_port_ka) {
1248 fip->send_port_ka = 0;
1249 mutex_lock(&fip->lp->lp_mutex);
1250 mac = fip->get_src_addr(fip->lp);
1251 fcoe_ctlr_send_keep_alive(fip, fip->lp, 1, mac);
1252 list_for_each_entry(vport, &fip->lp->vports, list) {
1253 mac = fip->get_src_addr(vport);
1254 fcoe_ctlr_send_keep_alive(fip, vport, 1, mac);
1256 mutex_unlock(&fip->lp->lp_mutex);
1261 * fcoe_ctlr_recv_work() - Worker thread function for receiving FIP frames
1262 * @recv_work: Handle to a FCoE controller
1264 static void fcoe_ctlr_recv_work(struct work_struct *recv_work)
1266 struct fcoe_ctlr *fip;
1267 struct sk_buff *skb;
1269 fip = container_of(recv_work, struct fcoe_ctlr, recv_work);
1270 while ((skb = skb_dequeue(&fip->fip_recv_list)))
1271 fcoe_ctlr_recv_handler(fip, skb);
1275 * fcoe_ctlr_recv_flogi() - Snoop pre-FIP receipt of FLOGI response
1276 * @fip: The FCoE controller
1277 * @fp: The FC frame to snoop
1279 * Snoop potential response to FLOGI or even incoming FLOGI.
1281 * The caller has checked that we are waiting for login as indicated
1282 * by fip->flogi_oxid != FC_XID_UNKNOWN.
1284 * The caller is responsible for freeing the frame.
1285 * Fill in the granted_mac address.
1287 * Return non-zero if the frame should not be delivered to libfc.
1289 int fcoe_ctlr_recv_flogi(struct fcoe_ctlr *fip, struct fc_lport *lport,
1290 struct fc_frame *fp)
1292 struct fc_frame_header *fh;
1293 u8 op;
1294 u8 *sa;
1296 sa = eth_hdr(&fp->skb)->h_source;
1297 fh = fc_frame_header_get(fp);
1298 if (fh->fh_type != FC_TYPE_ELS)
1299 return 0;
1301 op = fc_frame_payload_op(fp);
1302 if (op == ELS_LS_ACC && fh->fh_r_ctl == FC_RCTL_ELS_REP &&
1303 fip->flogi_oxid == ntohs(fh->fh_ox_id)) {
1305 spin_lock_bh(&fip->lock);
1306 if (fip->state != FIP_ST_AUTO && fip->state != FIP_ST_NON_FIP) {
1307 spin_unlock_bh(&fip->lock);
1308 return -EINVAL;
1310 fip->state = FIP_ST_NON_FIP;
1311 LIBFCOE_FIP_DBG(fip,
1312 "received FLOGI LS_ACC using non-FIP mode\n");
1315 * FLOGI accepted.
1316 * If the src mac addr is FC_OUI-based, then we mark the
1317 * address_mode flag to use FC_OUI-based Ethernet DA.
1318 * Otherwise we use the FCoE gateway addr
1320 if (!compare_ether_addr(sa, (u8[6])FC_FCOE_FLOGI_MAC)) {
1321 fip->map_dest = 1;
1322 } else {
1323 memcpy(fip->dest_addr, sa, ETH_ALEN);
1324 fip->map_dest = 0;
1326 fip->flogi_oxid = FC_XID_UNKNOWN;
1327 spin_unlock_bh(&fip->lock);
1328 fc_fcoe_set_mac(fr_cb(fp)->granted_mac, fh->fh_d_id);
1329 } else if (op == ELS_FLOGI && fh->fh_r_ctl == FC_RCTL_ELS_REQ && sa) {
1331 * Save source MAC for point-to-point responses.
1333 spin_lock_bh(&fip->lock);
1334 if (fip->state == FIP_ST_AUTO || fip->state == FIP_ST_NON_FIP) {
1335 memcpy(fip->dest_addr, sa, ETH_ALEN);
1336 fip->map_dest = 0;
1337 if (fip->state == FIP_ST_NON_FIP)
1338 LIBFCOE_FIP_DBG(fip, "received FLOGI REQ, "
1339 "using non-FIP mode\n");
1340 fip->state = FIP_ST_NON_FIP;
1342 spin_unlock_bh(&fip->lock);
1344 return 0;
1346 EXPORT_SYMBOL(fcoe_ctlr_recv_flogi);
1349 * fcoe_wwn_from_mac() - Converts a 48-bit IEEE MAC address to a 64-bit FC WWN
1350 * @mac: The MAC address to convert
1351 * @scheme: The scheme to use when converting
1352 * @port: The port indicator for converting
1354 * Returns: u64 fc world wide name
1356 u64 fcoe_wwn_from_mac(unsigned char mac[MAX_ADDR_LEN],
1357 unsigned int scheme, unsigned int port)
1359 u64 wwn;
1360 u64 host_mac;
1362 /* The MAC is in NO, so flip only the low 48 bits */
1363 host_mac = ((u64) mac[0] << 40) |
1364 ((u64) mac[1] << 32) |
1365 ((u64) mac[2] << 24) |
1366 ((u64) mac[3] << 16) |
1367 ((u64) mac[4] << 8) |
1368 (u64) mac[5];
1370 WARN_ON(host_mac >= (1ULL << 48));
1371 wwn = host_mac | ((u64) scheme << 60);
1372 switch (scheme) {
1373 case 1:
1374 WARN_ON(port != 0);
1375 break;
1376 case 2:
1377 WARN_ON(port >= 0xfff);
1378 wwn |= (u64) port << 48;
1379 break;
1380 default:
1381 WARN_ON(1);
1382 break;
1385 return wwn;
1387 EXPORT_SYMBOL_GPL(fcoe_wwn_from_mac);
1390 * fcoe_libfc_config() - Sets up libfc related properties for local port
1391 * @lp: The local port to configure libfc for
1392 * @tt: The libfc function template
1394 * Returns : 0 for success
1396 int fcoe_libfc_config(struct fc_lport *lport,
1397 struct libfc_function_template *tt)
1399 /* Set the function pointers set by the LLDD */
1400 memcpy(&lport->tt, tt, sizeof(*tt));
1401 if (fc_fcp_init(lport))
1402 return -ENOMEM;
1403 fc_exch_init(lport);
1404 fc_elsct_init(lport);
1405 fc_lport_init(lport);
1406 fc_rport_init(lport);
1407 fc_disc_init(lport);
1409 return 0;
1411 EXPORT_SYMBOL_GPL(fcoe_libfc_config);