]> bbs.cooldavid.org Git - net-next-2.6.git/blame - drivers/scsi/fcoe/libfcoe.c
[SCSI] libfcoe: Set fip_flags according to fcf and lport's capability of SPMA support
[net-next-2.6.git] / drivers / scsi / fcoe / libfcoe.c
CommitLineData
9b34ecff 1/*
97c8389d
JE
2 * Copyright (c) 2008-2009 Cisco Systems, Inc. All rights reserved.
3 * Copyright (c) 2009 Intel Corporation. All rights reserved.
9b34ecff
VD
4 *
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.
8 *
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.
13 *
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.
17 *
18 * Maintained at www.Open-FCoE.org
19 */
20
97c8389d 21#include <linux/types.h>
9b34ecff 22#include <linux/module.h>
97c8389d
JE
23#include <linux/kernel.h>
24#include <linux/list.h>
25#include <linux/spinlock.h>
26#include <linux/timer.h>
5e80f7f7 27#include <linux/netdevice.h>
97c8389d
JE
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/netdevice.h>
33#include <linux/errno.h>
34#include <linux/bitops.h>
35#include <net/rtnetlink.h>
36
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>
5e80f7f7
VD
42
43#include <scsi/libfc.h>
97c8389d 44#include <scsi/libfcoe.h>
9b34ecff
VD
45
46MODULE_AUTHOR("Open-FCoE.org");
97c8389d 47MODULE_DESCRIPTION("FIP discovery protocol support for FCoE HBAs");
9b34ecff 48MODULE_LICENSE("GPL v2");
5e80f7f7 49
97c8389d
JE
50#define FCOE_CTLR_MIN_FKA 500 /* min keep alive (mS) */
51#define FCOE_CTLR_DEF_FKA FIP_DEF_FKA /* default keep alive (mS) */
52
53static void fcoe_ctlr_timeout(unsigned long);
54static void fcoe_ctlr_link_work(struct work_struct *);
55static void fcoe_ctlr_recv_work(struct work_struct *);
56
57static u8 fcoe_all_fcfs[ETH_ALEN] = FIP_ALL_FCF_MACS;
58
650bd12b
RL
59unsigned int libfcoe_debug_logging;
60module_param_named(debug_logging, libfcoe_debug_logging, int, S_IRUGO|S_IWUSR);
61MODULE_PARM_DESC(debug_logging, "a bit mask of logging levels");
97c8389d 62
650bd12b
RL
63#define LIBFCOE_LOGGING 0x01 /* General logging, not categorized */
64#define LIBFCOE_FIP_LOGGING 0x02 /* FIP logging */
65
66#define LIBFCOE_CHECK_LOGGING(LEVEL, CMD) \
67do { \
68 if (unlikely(libfcoe_debug_logging & LEVEL)) \
97c8389d 69 do { \
650bd12b
RL
70 CMD; \
71 } while (0); \
72} while (0);
73
74#define LIBFCOE_DBG(fmt, args...) \
75 LIBFCOE_CHECK_LOGGING(LIBFCOE_LOGGING, \
76 printk(KERN_INFO "libfcoe: " fmt, ##args);)
97c8389d 77
650bd12b
RL
78#define LIBFCOE_FIP_DBG(fmt, args...) \
79 LIBFCOE_CHECK_LOGGING(LIBFCOE_FIP_LOGGING, \
80 printk(KERN_INFO "fip: " fmt, ##args);)
97c8389d
JE
81
82/*
83 * Return non-zero if FCF fcoe_size has been validated.
84 */
85static inline int fcoe_ctlr_mtu_valid(const struct fcoe_fcf *fcf)
86{
87 return (fcf->flags & FIP_FL_SOL) != 0;
88}
89
90/*
91 * Return non-zero if the FCF is usable.
92 */
93static inline int fcoe_ctlr_fcf_usable(struct fcoe_fcf *fcf)
94{
95 u16 flags = FIP_FL_SOL | FIP_FL_AVAIL;
96
97 return (fcf->flags & flags) == flags;
98}
99
100/**
101 * fcoe_ctlr_init() - Initialize the FCoE Controller instance.
102 * @fip: FCoE controller.
103 */
104void fcoe_ctlr_init(struct fcoe_ctlr *fip)
105{
106 fip->state = FIP_ST_LINK_WAIT;
107 INIT_LIST_HEAD(&fip->fcfs);
108 spin_lock_init(&fip->lock);
109 fip->flogi_oxid = FC_XID_UNKNOWN;
110 setup_timer(&fip->timer, fcoe_ctlr_timeout, (unsigned long)fip);
111 INIT_WORK(&fip->link_work, fcoe_ctlr_link_work);
112 INIT_WORK(&fip->recv_work, fcoe_ctlr_recv_work);
113 skb_queue_head_init(&fip->fip_recv_list);
114}
115EXPORT_SYMBOL(fcoe_ctlr_init);
116
117/**
118 * fcoe_ctlr_reset_fcfs() - Reset and free all FCFs for a controller.
119 * @fip: FCoE controller.
120 *
121 * Called with &fcoe_ctlr lock held.
122 */
123static void fcoe_ctlr_reset_fcfs(struct fcoe_ctlr *fip)
124{
125 struct fcoe_fcf *fcf;
126 struct fcoe_fcf *next;
127
128 fip->sel_fcf = NULL;
129 list_for_each_entry_safe(fcf, next, &fip->fcfs, list) {
130 list_del(&fcf->list);
131 kfree(fcf);
132 }
133 fip->fcf_count = 0;
134 fip->sel_time = 0;
135}
136
137/**
dd3fd72e 138 * fcoe_ctlr_destroy() - Disable and tear-down the FCoE controller.
97c8389d
JE
139 * @fip: FCoE controller.
140 *
141 * This is called by FCoE drivers before freeing the &fcoe_ctlr.
142 *
143 * The receive handler will have been deleted before this to guarantee
144 * that no more recv_work will be scheduled.
145 *
146 * The timer routine will simply return once we set FIP_ST_DISABLED.
147 * This guarantees that no further timeouts or work will be scheduled.
148 */
149void fcoe_ctlr_destroy(struct fcoe_ctlr *fip)
150{
151 flush_work(&fip->recv_work);
152 spin_lock_bh(&fip->lock);
153 fip->state = FIP_ST_DISABLED;
154 fcoe_ctlr_reset_fcfs(fip);
155 spin_unlock_bh(&fip->lock);
156 del_timer_sync(&fip->timer);
157 flush_work(&fip->link_work);
158}
159EXPORT_SYMBOL(fcoe_ctlr_destroy);
160
161/**
162 * fcoe_ctlr_fcoe_size() - Return the maximum FCoE size required for VN_Port.
163 * @fip: FCoE controller.
164 *
165 * Returns the maximum packet size including the FCoE header and trailer,
166 * but not including any Ethernet or VLAN headers.
167 */
168static inline u32 fcoe_ctlr_fcoe_size(struct fcoe_ctlr *fip)
169{
170 /*
171 * Determine the max FCoE frame size allowed, including
172 * FCoE header and trailer.
173 * Note: lp->mfs is currently the payload size, not the frame size.
174 */
175 return fip->lp->mfs + sizeof(struct fc_frame_header) +
176 sizeof(struct fcoe_hdr) + sizeof(struct fcoe_crc_eof);
177}
178
179/**
180 * fcoe_ctlr_solicit() - Send a solicitation.
181 * @fip: FCoE controller.
182 * @fcf: Destination FCF. If NULL, a multicast solicitation is sent.
183 */
184static void fcoe_ctlr_solicit(struct fcoe_ctlr *fip, struct fcoe_fcf *fcf)
185{
186 struct sk_buff *skb;
187 struct fip_sol {
188 struct ethhdr eth;
189 struct fip_header fip;
190 struct {
191 struct fip_mac_desc mac;
192 struct fip_wwn_desc wwnn;
193 struct fip_size_desc size;
194 } __attribute__((packed)) desc;
195 } __attribute__((packed)) *sol;
196 u32 fcoe_size;
197
198 skb = dev_alloc_skb(sizeof(*sol));
199 if (!skb)
200 return;
201
202 sol = (struct fip_sol *)skb->data;
203
204 memset(sol, 0, sizeof(*sol));
205 memcpy(sol->eth.h_dest, fcf ? fcf->fcf_mac : fcoe_all_fcfs, ETH_ALEN);
206 memcpy(sol->eth.h_source, fip->ctl_src_addr, ETH_ALEN);
207 sol->eth.h_proto = htons(ETH_P_FIP);
208
209 sol->fip.fip_ver = FIP_VER_ENCAPS(FIP_VER);
210 sol->fip.fip_op = htons(FIP_OP_DISC);
211 sol->fip.fip_subcode = FIP_SC_SOL;
212 sol->fip.fip_dl_len = htons(sizeof(sol->desc) / FIP_BPW);
213 sol->fip.fip_flags = htons(FIP_FL_FPMA);
184dd345
VD
214 if (fip->spma)
215 sol->fip.fip_flags |= htons(FIP_FL_SPMA);
97c8389d
JE
216
217 sol->desc.mac.fd_desc.fip_dtype = FIP_DT_MAC;
218 sol->desc.mac.fd_desc.fip_dlen = sizeof(sol->desc.mac) / FIP_BPW;
219 memcpy(sol->desc.mac.fd_mac, fip->ctl_src_addr, ETH_ALEN);
220
221 sol->desc.wwnn.fd_desc.fip_dtype = FIP_DT_NAME;
222 sol->desc.wwnn.fd_desc.fip_dlen = sizeof(sol->desc.wwnn) / FIP_BPW;
223 put_unaligned_be64(fip->lp->wwnn, &sol->desc.wwnn.fd_wwn);
224
225 fcoe_size = fcoe_ctlr_fcoe_size(fip);
226 sol->desc.size.fd_desc.fip_dtype = FIP_DT_FCOE_SIZE;
227 sol->desc.size.fd_desc.fip_dlen = sizeof(sol->desc.size) / FIP_BPW;
228 sol->desc.size.fd_size = htons(fcoe_size);
229
230 skb_put(skb, sizeof(*sol));
0f491539 231 skb->protocol = htons(ETH_P_FIP);
97c8389d
JE
232 skb_reset_mac_header(skb);
233 skb_reset_network_header(skb);
234 fip->send(fip, skb);
235
236 if (!fcf)
237 fip->sol_time = jiffies;
238}
239
240/**
241 * fcoe_ctlr_link_up() - Start FCoE controller.
242 * @fip: FCoE controller.
243 *
244 * Called from the LLD when the network link is ready.
245 */
246void fcoe_ctlr_link_up(struct fcoe_ctlr *fip)
247{
248 spin_lock_bh(&fip->lock);
249 if (fip->state == FIP_ST_NON_FIP || fip->state == FIP_ST_AUTO) {
250 fip->last_link = 1;
251 fip->link = 1;
252 spin_unlock_bh(&fip->lock);
253 fc_linkup(fip->lp);
254 } else if (fip->state == FIP_ST_LINK_WAIT) {
255 fip->state = FIP_ST_AUTO;
256 fip->last_link = 1;
257 fip->link = 1;
258 spin_unlock_bh(&fip->lock);
650bd12b 259 LIBFCOE_FIP_DBG("%s", "setting AUTO mode.\n");
97c8389d
JE
260 fc_linkup(fip->lp);
261 fcoe_ctlr_solicit(fip, NULL);
262 } else
263 spin_unlock_bh(&fip->lock);
264}
265EXPORT_SYMBOL(fcoe_ctlr_link_up);
266
267/**
268 * fcoe_ctlr_reset() - Reset FIP.
269 * @fip: FCoE controller.
270 * @new_state: FIP state to be entered.
271 *
272 * Returns non-zero if the link was up and now isn't.
273 */
274static int fcoe_ctlr_reset(struct fcoe_ctlr *fip, enum fip_state new_state)
275{
276 struct fc_lport *lp = fip->lp;
277 int link_dropped;
278
279 spin_lock_bh(&fip->lock);
280 fcoe_ctlr_reset_fcfs(fip);
281 del_timer(&fip->timer);
282 fip->state = new_state;
283 fip->ctlr_ka_time = 0;
284 fip->port_ka_time = 0;
285 fip->sol_time = 0;
286 fip->flogi_oxid = FC_XID_UNKNOWN;
287 fip->map_dest = 0;
288 fip->last_link = 0;
289 link_dropped = fip->link;
290 fip->link = 0;
291 spin_unlock_bh(&fip->lock);
292
293 if (link_dropped)
294 fc_linkdown(lp);
295
296 if (new_state == FIP_ST_ENABLED) {
297 fcoe_ctlr_solicit(fip, NULL);
298 fc_linkup(lp);
299 link_dropped = 0;
300 }
301 return link_dropped;
302}
303
304/**
305 * fcoe_ctlr_link_down() - Stop FCoE controller.
306 * @fip: FCoE controller.
307 *
308 * Returns non-zero if the link was up and now isn't.
309 *
310 * Called from the LLD when the network link is not ready.
311 * There may be multiple calls while the link is down.
312 */
313int fcoe_ctlr_link_down(struct fcoe_ctlr *fip)
314{
315 return fcoe_ctlr_reset(fip, FIP_ST_LINK_WAIT);
316}
317EXPORT_SYMBOL(fcoe_ctlr_link_down);
318
319/**
320 * fcoe_ctlr_send_keep_alive() - Send a keep-alive to the selected FCF.
321 * @fip: FCoE controller.
322 * @ports: 0 for controller keep-alive, 1 for port keep-alive.
323 * @sa: source MAC address.
324 *
325 * A controller keep-alive is sent every fka_period (typically 8 seconds).
326 * The source MAC is the native MAC address.
327 *
328 * A port keep-alive is sent every 90 seconds while logged in.
329 * The source MAC is the assigned mapped source address.
330 * The destination is the FCF's F-port.
331 */
332static void fcoe_ctlr_send_keep_alive(struct fcoe_ctlr *fip, int ports, u8 *sa)
333{
334 struct sk_buff *skb;
335 struct fip_kal {
336 struct ethhdr eth;
337 struct fip_header fip;
338 struct fip_mac_desc mac;
339 } __attribute__((packed)) *kal;
340 struct fip_vn_desc *vn;
341 u32 len;
342 struct fc_lport *lp;
343 struct fcoe_fcf *fcf;
344
345 fcf = fip->sel_fcf;
346 lp = fip->lp;
347 if (!fcf || !fc_host_port_id(lp->host))
348 return;
349
350 len = fcoe_ctlr_fcoe_size(fip) + sizeof(struct ethhdr);
351 BUG_ON(len < sizeof(*kal) + sizeof(*vn));
352 skb = dev_alloc_skb(len);
353 if (!skb)
354 return;
355
356 kal = (struct fip_kal *)skb->data;
357 memset(kal, 0, len);
358 memcpy(kal->eth.h_dest, fcf->fcf_mac, ETH_ALEN);
359 memcpy(kal->eth.h_source, sa, ETH_ALEN);
360 kal->eth.h_proto = htons(ETH_P_FIP);
361
362 kal->fip.fip_ver = FIP_VER_ENCAPS(FIP_VER);
363 kal->fip.fip_op = htons(FIP_OP_CTRL);
364 kal->fip.fip_subcode = FIP_SC_KEEP_ALIVE;
365 kal->fip.fip_dl_len = htons((sizeof(kal->mac) +
366 ports * sizeof(*vn)) / FIP_BPW);
367 kal->fip.fip_flags = htons(FIP_FL_FPMA);
184dd345
VD
368 if (fip->spma)
369 kal->fip.fip_flags |= htons(FIP_FL_SPMA);
97c8389d
JE
370
371 kal->mac.fd_desc.fip_dtype = FIP_DT_MAC;
372 kal->mac.fd_desc.fip_dlen = sizeof(kal->mac) / FIP_BPW;
373 memcpy(kal->mac.fd_mac, fip->ctl_src_addr, ETH_ALEN);
374
375 if (ports) {
376 vn = (struct fip_vn_desc *)(kal + 1);
377 vn->fd_desc.fip_dtype = FIP_DT_VN_ID;
378 vn->fd_desc.fip_dlen = sizeof(*vn) / FIP_BPW;
379 memcpy(vn->fd_mac, fip->data_src_addr, ETH_ALEN);
380 hton24(vn->fd_fc_id, fc_host_port_id(lp->host));
381 put_unaligned_be64(lp->wwpn, &vn->fd_wwpn);
382 }
383
384 skb_put(skb, len);
0f491539 385 skb->protocol = htons(ETH_P_FIP);
97c8389d
JE
386 skb_reset_mac_header(skb);
387 skb_reset_network_header(skb);
388 fip->send(fip, skb);
389}
390
391/**
392 * fcoe_ctlr_encaps() - Encapsulate an ELS frame for FIP, without sending it.
393 * @fip: FCoE controller.
394 * @dtype: FIP descriptor type for the frame.
395 * @skb: FCoE ELS frame including FC header but no FCoE headers.
396 *
397 * Returns non-zero error code on failure.
398 *
399 * The caller must check that the length is a multiple of 4.
400 *
401 * The @skb must have enough headroom (28 bytes) and tailroom (8 bytes).
402 * Headroom includes the FIP encapsulation description, FIP header, and
403 * Ethernet header. The tailroom is for the FIP MAC descriptor.
404 */
405static int fcoe_ctlr_encaps(struct fcoe_ctlr *fip,
406 u8 dtype, struct sk_buff *skb)
407{
408 struct fip_encaps_head {
409 struct ethhdr eth;
410 struct fip_header fip;
411 struct fip_encaps encaps;
412 } __attribute__((packed)) *cap;
413 struct fip_mac_desc *mac;
414 struct fcoe_fcf *fcf;
415 size_t dlen;
5a84baea 416 u16 fip_flags;
97c8389d
JE
417
418 fcf = fip->sel_fcf;
419 if (!fcf)
420 return -ENODEV;
5a84baea
YZ
421
422 /* set flags according to both FCF and lport's capability on SPMA */
423 fip_flags = fcf->flags;
424 fip_flags &= fip->spma ? FIP_FL_SPMA | FIP_FL_FPMA : FIP_FL_FPMA;
425 if (!fip_flags)
426 return -ENODEV;
427
97c8389d
JE
428 dlen = sizeof(struct fip_encaps) + skb->len; /* len before push */
429 cap = (struct fip_encaps_head *)skb_push(skb, sizeof(*cap));
430
431 memset(cap, 0, sizeof(*cap));
432 memcpy(cap->eth.h_dest, fcf->fcf_mac, ETH_ALEN);
433 memcpy(cap->eth.h_source, fip->ctl_src_addr, ETH_ALEN);
434 cap->eth.h_proto = htons(ETH_P_FIP);
435
436 cap->fip.fip_ver = FIP_VER_ENCAPS(FIP_VER);
437 cap->fip.fip_op = htons(FIP_OP_LS);
438 cap->fip.fip_subcode = FIP_SC_REQ;
439 cap->fip.fip_dl_len = htons((dlen + sizeof(*mac)) / FIP_BPW);
5a84baea 440 cap->fip.fip_flags = htons(fip_flags);
97c8389d
JE
441
442 cap->encaps.fd_desc.fip_dtype = dtype;
443 cap->encaps.fd_desc.fip_dlen = dlen / FIP_BPW;
444
445 mac = (struct fip_mac_desc *)skb_put(skb, sizeof(*mac));
446 memset(mac, 0, sizeof(mac));
447 mac->fd_desc.fip_dtype = FIP_DT_MAC;
448 mac->fd_desc.fip_dlen = sizeof(*mac) / FIP_BPW;
184dd345 449 if (dtype != FIP_DT_FLOGI)
97c8389d 450 memcpy(mac->fd_mac, fip->data_src_addr, ETH_ALEN);
184dd345
VD
451 else if (fip->spma)
452 memcpy(mac->fd_mac, fip->ctl_src_addr, ETH_ALEN);
97c8389d 453
0f491539 454 skb->protocol = htons(ETH_P_FIP);
97c8389d
JE
455 skb_reset_mac_header(skb);
456 skb_reset_network_header(skb);
457 return 0;
458}
459
460/**
461 * fcoe_ctlr_els_send() - Send an ELS frame encapsulated by FIP if appropriate.
462 * @fip: FCoE controller.
463 * @skb: FCoE ELS frame including FC header but no FCoE headers.
464 *
465 * Returns a non-zero error code if the frame should not be sent.
466 * Returns zero if the caller should send the frame with FCoE encapsulation.
467 *
468 * The caller must check that the length is a multiple of 4.
469 * The SKB must have enough headroom (28 bytes) and tailroom (8 bytes).
470 */
471int fcoe_ctlr_els_send(struct fcoe_ctlr *fip, struct sk_buff *skb)
472{
473 struct fc_frame_header *fh;
474 u16 old_xid;
475 u8 op;
476
97c8389d
JE
477 fh = (struct fc_frame_header *)skb->data;
478 op = *(u8 *)(fh + 1);
479
5f48f70e 480 if (op == ELS_FLOGI) {
97c8389d
JE
481 old_xid = fip->flogi_oxid;
482 fip->flogi_oxid = ntohs(fh->fh_ox_id);
483 if (fip->state == FIP_ST_AUTO) {
484 if (old_xid == FC_XID_UNKNOWN)
485 fip->flogi_count = 0;
486 fip->flogi_count++;
487 if (fip->flogi_count < 3)
488 goto drop;
489 fip->map_dest = 1;
490 return 0;
491 }
5f48f70e
JE
492 if (fip->state == FIP_ST_NON_FIP)
493 fip->map_dest = 1;
494 }
495
496 if (fip->state == FIP_ST_NON_FIP)
497 return 0;
498
499 switch (op) {
500 case ELS_FLOGI:
97c8389d
JE
501 op = FIP_DT_FLOGI;
502 break;
503 case ELS_FDISC:
504 if (ntoh24(fh->fh_s_id))
505 return 0;
506 op = FIP_DT_FDISC;
507 break;
508 case ELS_LOGO:
509 if (fip->state != FIP_ST_ENABLED)
510 return 0;
511 if (ntoh24(fh->fh_d_id) != FC_FID_FLOGI)
512 return 0;
513 op = FIP_DT_LOGO;
514 break;
515 case ELS_LS_ACC:
516 if (fip->flogi_oxid == FC_XID_UNKNOWN)
517 return 0;
518 if (!ntoh24(fh->fh_s_id))
519 return 0;
520 if (fip->state == FIP_ST_AUTO)
521 return 0;
522 /*
523 * Here we must've gotten an SID by accepting an FLOGI
524 * from a point-to-point connection. Switch to using
525 * the source mac based on the SID. The destination
526 * MAC in this case would have been set by receving the
527 * FLOGI.
528 */
529 fip->flogi_oxid = FC_XID_UNKNOWN;
530 fc_fcoe_set_mac(fip->data_src_addr, fh->fh_s_id);
531 return 0;
532 default:
533 if (fip->state != FIP_ST_ENABLED)
534 goto drop;
535 return 0;
536 }
537 if (fcoe_ctlr_encaps(fip, op, skb))
538 goto drop;
539 fip->send(fip, skb);
540 return -EINPROGRESS;
541drop:
542 kfree_skb(skb);
543 return -EINVAL;
544}
545EXPORT_SYMBOL(fcoe_ctlr_els_send);
546
547/*
548 * fcoe_ctlr_age_fcfs() - Reset and free all old FCFs for a controller.
549 * @fip: FCoE controller.
550 *
551 * Called with lock held.
552 *
553 * An FCF is considered old if we have missed three advertisements.
554 * That is, there have been no valid advertisement from it for three
555 * times its keep-alive period including fuzz.
556 *
557 * In addition, determine the time when an FCF selection can occur.
558 */
559static void fcoe_ctlr_age_fcfs(struct fcoe_ctlr *fip)
560{
561 struct fcoe_fcf *fcf;
562 struct fcoe_fcf *next;
563 unsigned long sel_time = 0;
564
565 list_for_each_entry_safe(fcf, next, &fip->fcfs, list) {
566 if (time_after(jiffies, fcf->time + fcf->fka_period * 3 +
567 msecs_to_jiffies(FIP_FCF_FUZZ * 3))) {
568 if (fip->sel_fcf == fcf)
569 fip->sel_fcf = NULL;
570 list_del(&fcf->list);
571 WARN_ON(!fip->fcf_count);
572 fip->fcf_count--;
573 kfree(fcf);
574 } else if (fcoe_ctlr_mtu_valid(fcf) &&
575 (!sel_time || time_before(sel_time, fcf->time))) {
576 sel_time = fcf->time;
577 }
578 }
579 if (sel_time) {
580 sel_time += msecs_to_jiffies(FCOE_CTLR_START_DELAY);
581 fip->sel_time = sel_time;
582 if (time_before(sel_time, fip->timer.expires))
583 mod_timer(&fip->timer, sel_time);
584 } else {
585 fip->sel_time = 0;
586 }
587}
588
589/**
590 * fcoe_ctlr_parse_adv() - Decode a FIP advertisement into a new FCF entry.
591 * @skb: received FIP advertisement frame
592 * @fcf: resulting FCF entry.
593 *
594 * Returns zero on a valid parsed advertisement,
595 * otherwise returns non zero value.
596 */
597static int fcoe_ctlr_parse_adv(struct sk_buff *skb, struct fcoe_fcf *fcf)
598{
599 struct fip_header *fiph;
600 struct fip_desc *desc = NULL;
601 struct fip_wwn_desc *wwn;
602 struct fip_fab_desc *fab;
603 struct fip_fka_desc *fka;
604 unsigned long t;
605 size_t rlen;
606 size_t dlen;
607
608 memset(fcf, 0, sizeof(*fcf));
609 fcf->fka_period = msecs_to_jiffies(FCOE_CTLR_DEF_FKA);
610
611 fiph = (struct fip_header *)skb->data;
612 fcf->flags = ntohs(fiph->fip_flags);
613
614 rlen = ntohs(fiph->fip_dl_len) * 4;
615 if (rlen + sizeof(*fiph) > skb->len)
616 return -EINVAL;
617
618 desc = (struct fip_desc *)(fiph + 1);
619 while (rlen > 0) {
620 dlen = desc->fip_dlen * FIP_BPW;
621 if (dlen < sizeof(*desc) || dlen > rlen)
622 return -EINVAL;
623 switch (desc->fip_dtype) {
624 case FIP_DT_PRI:
625 if (dlen != sizeof(struct fip_pri_desc))
626 goto len_err;
627 fcf->pri = ((struct fip_pri_desc *)desc)->fd_pri;
628 break;
629 case FIP_DT_MAC:
630 if (dlen != sizeof(struct fip_mac_desc))
631 goto len_err;
632 memcpy(fcf->fcf_mac,
633 ((struct fip_mac_desc *)desc)->fd_mac,
634 ETH_ALEN);
635 if (!is_valid_ether_addr(fcf->fcf_mac)) {
650bd12b
RL
636 LIBFCOE_FIP_DBG("Invalid MAC address "
637 "in FIP adv\n");
97c8389d
JE
638 return -EINVAL;
639 }
640 break;
641 case FIP_DT_NAME:
642 if (dlen != sizeof(struct fip_wwn_desc))
643 goto len_err;
644 wwn = (struct fip_wwn_desc *)desc;
645 fcf->switch_name = get_unaligned_be64(&wwn->fd_wwn);
646 break;
647 case FIP_DT_FAB:
648 if (dlen != sizeof(struct fip_fab_desc))
649 goto len_err;
650 fab = (struct fip_fab_desc *)desc;
651 fcf->fabric_name = get_unaligned_be64(&fab->fd_wwn);
652 fcf->vfid = ntohs(fab->fd_vfid);
653 fcf->fc_map = ntoh24(fab->fd_map);
654 break;
655 case FIP_DT_FKA:
656 if (dlen != sizeof(struct fip_fka_desc))
657 goto len_err;
658 fka = (struct fip_fka_desc *)desc;
659 t = ntohl(fka->fd_fka_period);
660 if (t >= FCOE_CTLR_MIN_FKA)
661 fcf->fka_period = msecs_to_jiffies(t);
662 break;
663 case FIP_DT_MAP_OUI:
664 case FIP_DT_FCOE_SIZE:
665 case FIP_DT_FLOGI:
666 case FIP_DT_FDISC:
667 case FIP_DT_LOGO:
668 case FIP_DT_ELP:
669 default:
650bd12b
RL
670 LIBFCOE_FIP_DBG("unexpected descriptor type %x "
671 "in FIP adv\n", desc->fip_dtype);
97c8389d
JE
672 /* standard says ignore unknown descriptors >= 128 */
673 if (desc->fip_dtype < FIP_DT_VENDOR_BASE)
674 return -EINVAL;
675 continue;
676 }
677 desc = (struct fip_desc *)((char *)desc + dlen);
678 rlen -= dlen;
679 }
680 if (!fcf->fc_map || (fcf->fc_map & 0x10000))
681 return -EINVAL;
682 if (!fcf->switch_name || !fcf->fabric_name)
683 return -EINVAL;
684 return 0;
685
686len_err:
650bd12b
RL
687 LIBFCOE_FIP_DBG("FIP length error in descriptor type %x len %zu\n",
688 desc->fip_dtype, dlen);
97c8389d
JE
689 return -EINVAL;
690}
691
692/**
693 * fcoe_ctlr_recv_adv() - Handle an incoming advertisement.
694 * @fip: FCoE controller.
695 * @skb: Received FIP packet.
696 */
697static void fcoe_ctlr_recv_adv(struct fcoe_ctlr *fip, struct sk_buff *skb)
698{
699 struct fcoe_fcf *fcf;
700 struct fcoe_fcf new;
701 struct fcoe_fcf *found;
702 unsigned long sol_tov = msecs_to_jiffies(FCOE_CTRL_SOL_TOV);
703 int first = 0;
704 int mtu_valid;
705
706 if (fcoe_ctlr_parse_adv(skb, &new))
707 return;
708
709 spin_lock_bh(&fip->lock);
710 first = list_empty(&fip->fcfs);
711 found = NULL;
712 list_for_each_entry(fcf, &fip->fcfs, list) {
713 if (fcf->switch_name == new.switch_name &&
714 fcf->fabric_name == new.fabric_name &&
715 fcf->fc_map == new.fc_map &&
716 compare_ether_addr(fcf->fcf_mac, new.fcf_mac) == 0) {
717 found = fcf;
718 break;
719 }
720 }
721 if (!found) {
722 if (fip->fcf_count >= FCOE_CTLR_FCF_LIMIT)
723 goto out;
724
725 fcf = kmalloc(sizeof(*fcf), GFP_ATOMIC);
726 if (!fcf)
727 goto out;
728
729 fip->fcf_count++;
730 memcpy(fcf, &new, sizeof(new));
731 list_add(&fcf->list, &fip->fcfs);
732 } else {
733 /*
734 * Flags in advertisements are ignored once the FCF is
735 * selected. Flags in unsolicited advertisements are
736 * ignored after a usable solicited advertisement
737 * has been received.
738 */
739 if (fcf == fip->sel_fcf) {
740 fip->ctlr_ka_time -= fcf->fka_period;
741 fip->ctlr_ka_time += new.fka_period;
742 if (time_before(fip->ctlr_ka_time, fip->timer.expires))
743 mod_timer(&fip->timer, fip->ctlr_ka_time);
744 } else if (!fcoe_ctlr_fcf_usable(fcf))
745 fcf->flags = new.flags;
746 fcf->fka_period = new.fka_period;
747 memcpy(fcf->fcf_mac, new.fcf_mac, ETH_ALEN);
748 }
749 mtu_valid = fcoe_ctlr_mtu_valid(fcf);
750 fcf->time = jiffies;
650bd12b
RL
751 if (!found) {
752 LIBFCOE_FIP_DBG("New FCF for fab %llx map %x val %d\n",
753 fcf->fabric_name, fcf->fc_map, mtu_valid);
754 }
97c8389d
JE
755
756 /*
757 * If this advertisement is not solicited and our max receive size
758 * hasn't been verified, send a solicited advertisement.
759 */
760 if (!mtu_valid)
761 fcoe_ctlr_solicit(fip, fcf);
762
763 /*
764 * If its been a while since we did a solicit, and this is
765 * the first advertisement we've received, do a multicast
766 * solicitation to gather as many advertisements as we can
767 * before selection occurs.
768 */
769 if (first && time_after(jiffies, fip->sol_time + sol_tov))
770 fcoe_ctlr_solicit(fip, NULL);
771
772 /*
773 * If this is the first validated FCF, note the time and
774 * set a timer to trigger selection.
775 */
776 if (mtu_valid && !fip->sel_time && fcoe_ctlr_fcf_usable(fcf)) {
777 fip->sel_time = jiffies +
778 msecs_to_jiffies(FCOE_CTLR_START_DELAY);
779 if (!timer_pending(&fip->timer) ||
780 time_before(fip->sel_time, fip->timer.expires))
781 mod_timer(&fip->timer, fip->sel_time);
782 }
783out:
784 spin_unlock_bh(&fip->lock);
785}
786
787/**
788 * fcoe_ctlr_recv_els() - Handle an incoming FIP-encapsulated ELS frame.
789 * @fip: FCoE controller.
790 * @skb: Received FIP packet.
791 */
792static void fcoe_ctlr_recv_els(struct fcoe_ctlr *fip, struct sk_buff *skb)
793{
794 struct fc_lport *lp = fip->lp;
795 struct fip_header *fiph;
796 struct fc_frame *fp;
797 struct fc_frame_header *fh = NULL;
798 struct fip_desc *desc;
799 struct fip_encaps *els;
800 struct fcoe_dev_stats *stats;
801 enum fip_desc_type els_dtype = 0;
802 u8 els_op;
803 u8 sub;
804 u8 granted_mac[ETH_ALEN] = { 0 };
805 size_t els_len = 0;
806 size_t rlen;
807 size_t dlen;
808
809 fiph = (struct fip_header *)skb->data;
810 sub = fiph->fip_subcode;
811 if (sub != FIP_SC_REQ && sub != FIP_SC_REP)
812 goto drop;
813
814 rlen = ntohs(fiph->fip_dl_len) * 4;
815 if (rlen + sizeof(*fiph) > skb->len)
816 goto drop;
817
818 desc = (struct fip_desc *)(fiph + 1);
819 while (rlen > 0) {
820 dlen = desc->fip_dlen * FIP_BPW;
821 if (dlen < sizeof(*desc) || dlen > rlen)
822 goto drop;
823 switch (desc->fip_dtype) {
824 case FIP_DT_MAC:
825 if (dlen != sizeof(struct fip_mac_desc))
826 goto len_err;
827 memcpy(granted_mac,
828 ((struct fip_mac_desc *)desc)->fd_mac,
829 ETH_ALEN);
830 if (!is_valid_ether_addr(granted_mac)) {
650bd12b
RL
831 LIBFCOE_FIP_DBG("Invalid MAC address "
832 "in FIP ELS\n");
97c8389d
JE
833 goto drop;
834 }
835 break;
836 case FIP_DT_FLOGI:
837 case FIP_DT_FDISC:
838 case FIP_DT_LOGO:
839 case FIP_DT_ELP:
840 if (fh)
841 goto drop;
842 if (dlen < sizeof(*els) + sizeof(*fh) + 1)
843 goto len_err;
844 els_len = dlen - sizeof(*els);
845 els = (struct fip_encaps *)desc;
846 fh = (struct fc_frame_header *)(els + 1);
847 els_dtype = desc->fip_dtype;
848 break;
849 default:
650bd12b
RL
850 LIBFCOE_FIP_DBG("unexpected descriptor type %x "
851 "in FIP adv\n", desc->fip_dtype);
97c8389d
JE
852 /* standard says ignore unknown descriptors >= 128 */
853 if (desc->fip_dtype < FIP_DT_VENDOR_BASE)
854 goto drop;
855 continue;
856 }
857 desc = (struct fip_desc *)((char *)desc + dlen);
858 rlen -= dlen;
859 }
860
861 if (!fh)
862 goto drop;
863 els_op = *(u8 *)(fh + 1);
864
865 if (els_dtype == FIP_DT_FLOGI && sub == FIP_SC_REP &&
866 fip->flogi_oxid == ntohs(fh->fh_ox_id) &&
867 els_op == ELS_LS_ACC && is_valid_ether_addr(granted_mac)) {
868 fip->flogi_oxid = FC_XID_UNKNOWN;
869 fip->update_mac(fip, fip->data_src_addr, granted_mac);
870 memcpy(fip->data_src_addr, granted_mac, ETH_ALEN);
871 }
872
873 /*
874 * Convert skb into an fc_frame containing only the ELS.
875 */
876 skb_pull(skb, (u8 *)fh - skb->data);
877 skb_trim(skb, els_len);
878 fp = (struct fc_frame *)skb;
879 fc_frame_init(fp);
880 fr_sof(fp) = FC_SOF_I3;
881 fr_eof(fp) = FC_EOF_T;
882 fr_dev(fp) = lp;
883
884 stats = fc_lport_get_stats(lp);
885 stats->RxFrames++;
886 stats->RxWords += skb->len / FIP_BPW;
887
888 fc_exch_recv(lp, lp->emp, fp);
889 return;
890
891len_err:
650bd12b
RL
892 LIBFCOE_FIP_DBG("FIP length error in descriptor type %x len %zu\n",
893 desc->fip_dtype, dlen);
97c8389d
JE
894drop:
895 kfree_skb(skb);
896}
897
898/**
899 * fcoe_ctlr_recv_els() - Handle an incoming link reset frame.
900 * @fip: FCoE controller.
901 * @fh: Received FIP header.
902 *
903 * There may be multiple VN_Port descriptors.
904 * The overall length has already been checked.
905 */
906static void fcoe_ctlr_recv_clr_vlink(struct fcoe_ctlr *fip,
907 struct fip_header *fh)
908{
909 struct fip_desc *desc;
910 struct fip_mac_desc *mp;
911 struct fip_wwn_desc *wp;
912 struct fip_vn_desc *vp;
913 size_t rlen;
914 size_t dlen;
915 struct fcoe_fcf *fcf = fip->sel_fcf;
916 struct fc_lport *lp = fip->lp;
917 u32 desc_mask;
918
650bd12b 919 LIBFCOE_FIP_DBG("Clear Virtual Link received\n");
97c8389d
JE
920 if (!fcf)
921 return;
922 if (!fcf || !fc_host_port_id(lp->host))
923 return;
924
925 /*
926 * mask of required descriptors. Validating each one clears its bit.
927 */
928 desc_mask = BIT(FIP_DT_MAC) | BIT(FIP_DT_NAME) | BIT(FIP_DT_VN_ID);
929
930 rlen = ntohs(fh->fip_dl_len) * FIP_BPW;
931 desc = (struct fip_desc *)(fh + 1);
932 while (rlen >= sizeof(*desc)) {
933 dlen = desc->fip_dlen * FIP_BPW;
934 if (dlen > rlen)
935 return;
936 switch (desc->fip_dtype) {
937 case FIP_DT_MAC:
938 mp = (struct fip_mac_desc *)desc;
939 if (dlen < sizeof(*mp))
940 return;
941 if (compare_ether_addr(mp->fd_mac, fcf->fcf_mac))
942 return;
943 desc_mask &= ~BIT(FIP_DT_MAC);
944 break;
945 case FIP_DT_NAME:
946 wp = (struct fip_wwn_desc *)desc;
947 if (dlen < sizeof(*wp))
948 return;
949 if (get_unaligned_be64(&wp->fd_wwn) != fcf->switch_name)
950 return;
951 desc_mask &= ~BIT(FIP_DT_NAME);
952 break;
953 case FIP_DT_VN_ID:
954 vp = (struct fip_vn_desc *)desc;
955 if (dlen < sizeof(*vp))
956 return;
957 if (compare_ether_addr(vp->fd_mac,
958 fip->data_src_addr) == 0 &&
959 get_unaligned_be64(&vp->fd_wwpn) == lp->wwpn &&
960 ntoh24(vp->fd_fc_id) == fc_host_port_id(lp->host))
961 desc_mask &= ~BIT(FIP_DT_VN_ID);
962 break;
963 default:
964 /* standard says ignore unknown descriptors >= 128 */
965 if (desc->fip_dtype < FIP_DT_VENDOR_BASE)
966 return;
967 break;
968 }
969 desc = (struct fip_desc *)((char *)desc + dlen);
970 rlen -= dlen;
971 }
972
973 /*
974 * reset only if all required descriptors were present and valid.
975 */
976 if (desc_mask) {
650bd12b 977 LIBFCOE_FIP_DBG("missing descriptors mask %x\n", desc_mask);
97c8389d 978 } else {
650bd12b 979 LIBFCOE_FIP_DBG("performing Clear Virtual Link\n");
97c8389d
JE
980 fcoe_ctlr_reset(fip, FIP_ST_ENABLED);
981 }
982}
983
984/**
985 * fcoe_ctlr_recv() - Receive a FIP frame.
986 * @fip: FCoE controller.
987 * @skb: Received FIP packet.
988 *
989 * This is called from NET_RX_SOFTIRQ.
990 */
991void fcoe_ctlr_recv(struct fcoe_ctlr *fip, struct sk_buff *skb)
992{
993 spin_lock_bh(&fip->fip_recv_list.lock);
994 __skb_queue_tail(&fip->fip_recv_list, skb);
995 spin_unlock_bh(&fip->fip_recv_list.lock);
996 schedule_work(&fip->recv_work);
997}
998EXPORT_SYMBOL(fcoe_ctlr_recv);
999
1000/**
1001 * fcoe_ctlr_recv_handler() - Receive a FIP frame.
1002 * @fip: FCoE controller.
1003 * @skb: Received FIP packet.
1004 *
1005 * Returns non-zero if the frame is dropped.
1006 */
1007static int fcoe_ctlr_recv_handler(struct fcoe_ctlr *fip, struct sk_buff *skb)
1008{
1009 struct fip_header *fiph;
1010 struct ethhdr *eh;
1011 enum fip_state state;
1012 u16 op;
1013 u8 sub;
1014
1015 if (skb_linearize(skb))
1016 goto drop;
1017 if (skb->len < sizeof(*fiph))
1018 goto drop;
1019 eh = eth_hdr(skb);
1020 if (compare_ether_addr(eh->h_dest, fip->ctl_src_addr) &&
1021 compare_ether_addr(eh->h_dest, FIP_ALL_ENODE_MACS))
1022 goto drop;
1023 fiph = (struct fip_header *)skb->data;
1024 op = ntohs(fiph->fip_op);
1025 sub = fiph->fip_subcode;
1026
97c8389d
JE
1027 if (FIP_VER_DECAPS(fiph->fip_ver) != FIP_VER)
1028 goto drop;
1029 if (ntohs(fiph->fip_dl_len) * FIP_BPW + sizeof(*fiph) > skb->len)
1030 goto drop;
1031
1032 spin_lock_bh(&fip->lock);
1033 state = fip->state;
1034 if (state == FIP_ST_AUTO) {
1035 fip->map_dest = 0;
1036 fip->state = FIP_ST_ENABLED;
1037 state = FIP_ST_ENABLED;
650bd12b 1038 LIBFCOE_FIP_DBG("Using FIP mode\n");
97c8389d
JE
1039 }
1040 spin_unlock_bh(&fip->lock);
1041 if (state != FIP_ST_ENABLED)
1042 goto drop;
1043
1044 if (op == FIP_OP_LS) {
1045 fcoe_ctlr_recv_els(fip, skb); /* consumes skb */
1046 return 0;
1047 }
1048 if (op == FIP_OP_DISC && sub == FIP_SC_ADV)
1049 fcoe_ctlr_recv_adv(fip, skb);
1050 else if (op == FIP_OP_CTRL && sub == FIP_SC_CLR_VLINK)
1051 fcoe_ctlr_recv_clr_vlink(fip, fiph);
1052 kfree_skb(skb);
1053 return 0;
1054drop:
1055 kfree_skb(skb);
1056 return -1;
1057}
1058
1059/**
1060 * fcoe_ctlr_select() - Select the best FCF, if possible.
1061 * @fip: FCoE controller.
1062 *
1063 * If there are conflicting advertisements, no FCF can be chosen.
1064 *
1065 * Called with lock held.
1066 */
1067static void fcoe_ctlr_select(struct fcoe_ctlr *fip)
1068{
1069 struct fcoe_fcf *fcf;
1070 struct fcoe_fcf *best = NULL;
1071
1072 list_for_each_entry(fcf, &fip->fcfs, list) {
650bd12b
RL
1073 LIBFCOE_FIP_DBG("consider FCF for fab %llx VFID %d map %x "
1074 "val %d\n", fcf->fabric_name, fcf->vfid,
1075 fcf->fc_map, fcoe_ctlr_mtu_valid(fcf));
97c8389d 1076 if (!fcoe_ctlr_fcf_usable(fcf)) {
650bd12b
RL
1077 LIBFCOE_FIP_DBG("FCF for fab %llx map %x %svalid "
1078 "%savailable\n", fcf->fabric_name,
1079 fcf->fc_map, (fcf->flags & FIP_FL_SOL)
1080 ? "" : "in", (fcf->flags & FIP_FL_AVAIL)
1081 ? "" : "un");
97c8389d
JE
1082 continue;
1083 }
1084 if (!best) {
1085 best = fcf;
1086 continue;
1087 }
1088 if (fcf->fabric_name != best->fabric_name ||
1089 fcf->vfid != best->vfid ||
1090 fcf->fc_map != best->fc_map) {
650bd12b
RL
1091 LIBFCOE_FIP_DBG("Conflicting fabric, VFID, "
1092 "or FC-MAP\n");
97c8389d
JE
1093 return;
1094 }
1095 if (fcf->pri < best->pri)
1096 best = fcf;
1097 }
1098 fip->sel_fcf = best;
1099}
1100
1101/**
1102 * fcoe_ctlr_timeout() - FIP timer function.
1103 * @arg: &fcoe_ctlr pointer.
1104 *
1105 * Ages FCFs. Triggers FCF selection if possible. Sends keep-alives.
1106 */
1107static void fcoe_ctlr_timeout(unsigned long arg)
1108{
1109 struct fcoe_ctlr *fip = (struct fcoe_ctlr *)arg;
1110 struct fcoe_fcf *sel;
1111 struct fcoe_fcf *fcf;
1112 unsigned long next_timer = jiffies + msecs_to_jiffies(FIP_VN_KA_PERIOD);
97c8389d
JE
1113 u8 send_ctlr_ka;
1114 u8 send_port_ka;
1115
1116 spin_lock_bh(&fip->lock);
1117 if (fip->state == FIP_ST_DISABLED) {
1118 spin_unlock_bh(&fip->lock);
1119 return;
1120 }
1121
1122 fcf = fip->sel_fcf;
1123 fcoe_ctlr_age_fcfs(fip);
1124
1125 sel = fip->sel_fcf;
1126 if (!sel && fip->sel_time && time_after_eq(jiffies, fip->sel_time)) {
1127 fcoe_ctlr_select(fip);
1128 sel = fip->sel_fcf;
1129 fip->sel_time = 0;
1130 }
1131
1132 if (sel != fcf) {
1133 fcf = sel; /* the old FCF may have been freed */
1134 if (sel) {
650bd12b 1135 printk(KERN_INFO "libfcoe: host%d: FIP selected "
66d6faec
JB
1136 "Fibre-Channel Forwarder MAC %pM\n",
1137 fip->lp->host->host_no, sel->fcf_mac);
97c8389d
JE
1138 memcpy(fip->dest_addr, sel->fcf_mac, ETH_ALEN);
1139 fip->port_ka_time = jiffies +
1140 msecs_to_jiffies(FIP_VN_KA_PERIOD);
1141 fip->ctlr_ka_time = jiffies + sel->fka_period;
1142 fip->link = 1;
1143 } else {
650bd12b 1144 printk(KERN_NOTICE "libfcoe: host%d: "
97c8389d
JE
1145 "FIP Fibre-Channel Forwarder timed out. "
1146 "Starting FCF discovery.\n",
1147 fip->lp->host->host_no);
1148 fip->link = 0;
1149 }
1150 schedule_work(&fip->link_work);
1151 }
1152
1153 send_ctlr_ka = 0;
1154 send_port_ka = 0;
1155 if (sel) {
1156 if (time_after_eq(jiffies, fip->ctlr_ka_time)) {
1157 fip->ctlr_ka_time = jiffies + sel->fka_period;
1158 send_ctlr_ka = 1;
1159 }
1160 if (time_after(next_timer, fip->ctlr_ka_time))
1161 next_timer = fip->ctlr_ka_time;
1162
1163 if (time_after_eq(jiffies, fip->port_ka_time)) {
1164 fip->port_ka_time += jiffies +
1165 msecs_to_jiffies(FIP_VN_KA_PERIOD);
1166 send_port_ka = 1;
1167 }
1168 if (time_after(next_timer, fip->port_ka_time))
1169 next_timer = fip->port_ka_time;
1170 mod_timer(&fip->timer, next_timer);
1171 } else if (fip->sel_time) {
1172 next_timer = fip->sel_time +
1173 msecs_to_jiffies(FCOE_CTLR_START_DELAY);
1174 mod_timer(&fip->timer, next_timer);
1175 }
1176 spin_unlock_bh(&fip->lock);
1177
1178 if (send_ctlr_ka)
1179 fcoe_ctlr_send_keep_alive(fip, 0, fip->ctl_src_addr);
1180 if (send_port_ka)
1181 fcoe_ctlr_send_keep_alive(fip, 1, fip->data_src_addr);
1182}
1183
1184/**
1185 * fcoe_ctlr_link_work() - worker thread function for link changes.
1186 * @work: pointer to link_work member inside &fcoe_ctlr.
1187 *
1188 * See if the link status has changed and if so, report it.
1189 *
1190 * This is here because fc_linkup() and fc_linkdown() must not
1191 * be called from the timer directly, since they use a mutex.
1192 */
1193static void fcoe_ctlr_link_work(struct work_struct *work)
1194{
1195 struct fcoe_ctlr *fip;
1196 int link;
1197 int last_link;
1198
1199 fip = container_of(work, struct fcoe_ctlr, link_work);
1200 spin_lock_bh(&fip->lock);
1201 last_link = fip->last_link;
1202 link = fip->link;
1203 fip->last_link = link;
1204 spin_unlock_bh(&fip->lock);
1205
1206 if (last_link != link) {
1207 if (link)
1208 fc_linkup(fip->lp);
1209 else
1210 fcoe_ctlr_reset(fip, FIP_ST_LINK_WAIT);
1211 }
1212}
1213
1214/**
1215 * fcoe_ctlr_recv_work() - Worker thread function for receiving FIP frames.
1216 * @recv_work: pointer to recv_work member inside &fcoe_ctlr.
1217 */
1218static void fcoe_ctlr_recv_work(struct work_struct *recv_work)
1219{
1220 struct fcoe_ctlr *fip;
1221 struct sk_buff *skb;
1222
1223 fip = container_of(recv_work, struct fcoe_ctlr, recv_work);
1224 spin_lock_bh(&fip->fip_recv_list.lock);
1225 while ((skb = __skb_dequeue(&fip->fip_recv_list))) {
1226 spin_unlock_bh(&fip->fip_recv_list.lock);
1227 fcoe_ctlr_recv_handler(fip, skb);
1228 spin_lock_bh(&fip->fip_recv_list.lock);
1229 }
1230 spin_unlock_bh(&fip->fip_recv_list.lock);
1231}
1232
1233/**
1234 * fcoe_ctlr_recv_flogi() - snoop Pre-FIP receipt of FLOGI response or request.
1235 * @fip: FCoE controller.
1236 * @fp: FC frame.
1237 * @sa: Ethernet source MAC address from received FCoE frame.
1238 *
1239 * Snoop potential response to FLOGI or even incoming FLOGI.
1240 *
1241 * The caller has checked that we are waiting for login as indicated
1242 * by fip->flogi_oxid != FC_XID_UNKNOWN.
1243 *
1244 * The caller is responsible for freeing the frame.
1245 *
1246 * Return non-zero if the frame should not be delivered to libfc.
1247 */
1248int fcoe_ctlr_recv_flogi(struct fcoe_ctlr *fip, struct fc_frame *fp, u8 *sa)
1249{
1250 struct fc_frame_header *fh;
1251 u8 op;
1252 u8 mac[ETH_ALEN];
1253
1254 fh = fc_frame_header_get(fp);
1255 if (fh->fh_type != FC_TYPE_ELS)
1256 return 0;
1257
1258 op = fc_frame_payload_op(fp);
1259 if (op == ELS_LS_ACC && fh->fh_r_ctl == FC_RCTL_ELS_REP &&
1260 fip->flogi_oxid == ntohs(fh->fh_ox_id)) {
1261
1262 spin_lock_bh(&fip->lock);
1263 if (fip->state != FIP_ST_AUTO && fip->state != FIP_ST_NON_FIP) {
1264 spin_unlock_bh(&fip->lock);
1265 return -EINVAL;
1266 }
1267 fip->state = FIP_ST_NON_FIP;
650bd12b 1268 LIBFCOE_FIP_DBG("received FLOGI LS_ACC using non-FIP mode\n");
97c8389d
JE
1269
1270 /*
1271 * FLOGI accepted.
1272 * If the src mac addr is FC_OUI-based, then we mark the
1273 * address_mode flag to use FC_OUI-based Ethernet DA.
1274 * Otherwise we use the FCoE gateway addr
1275 */
1276 if (!compare_ether_addr(sa, (u8[6])FC_FCOE_FLOGI_MAC)) {
1277 fip->map_dest = 1;
1278 } else {
1279 memcpy(fip->dest_addr, sa, ETH_ALEN);
1280 fip->map_dest = 0;
1281 }
1282 fip->flogi_oxid = FC_XID_UNKNOWN;
1283 memcpy(mac, fip->data_src_addr, ETH_ALEN);
1284 fc_fcoe_set_mac(fip->data_src_addr, fh->fh_d_id);
1285 spin_unlock_bh(&fip->lock);
1286
1287 fip->update_mac(fip, mac, fip->data_src_addr);
1288 } else if (op == ELS_FLOGI && fh->fh_r_ctl == FC_RCTL_ELS_REQ && sa) {
1289 /*
1290 * Save source MAC for point-to-point responses.
1291 */
1292 spin_lock_bh(&fip->lock);
1293 if (fip->state == FIP_ST_AUTO || fip->state == FIP_ST_NON_FIP) {
1294 memcpy(fip->dest_addr, sa, ETH_ALEN);
1295 fip->map_dest = 0;
1296 if (fip->state == FIP_ST_NON_FIP)
650bd12b 1297 LIBFCOE_FIP_DBG("received FLOGI REQ, "
97c8389d
JE
1298 "using non-FIP mode\n");
1299 fip->state = FIP_ST_NON_FIP;
1300 }
1301 spin_unlock_bh(&fip->lock);
1302 }
1303 return 0;
1304}
1305EXPORT_SYMBOL(fcoe_ctlr_recv_flogi);
1306
5e80f7f7
VD
1307/**
1308 * fcoe_wwn_from_mac() - Converts 48-bit IEEE MAC address to 64-bit FC WWN.
1309 * @mac: mac address
1310 * @scheme: check port
1311 * @port: port indicator for converting
1312 *
1313 * Returns: u64 fc world wide name
1314 */
1315u64 fcoe_wwn_from_mac(unsigned char mac[MAX_ADDR_LEN],
1316 unsigned int scheme, unsigned int port)
1317{
1318 u64 wwn;
1319 u64 host_mac;
1320
1321 /* The MAC is in NO, so flip only the low 48 bits */
1322 host_mac = ((u64) mac[0] << 40) |
1323 ((u64) mac[1] << 32) |
1324 ((u64) mac[2] << 24) |
1325 ((u64) mac[3] << 16) |
1326 ((u64) mac[4] << 8) |
1327 (u64) mac[5];
1328
1329 WARN_ON(host_mac >= (1ULL << 48));
1330 wwn = host_mac | ((u64) scheme << 60);
1331 switch (scheme) {
1332 case 1:
1333 WARN_ON(port != 0);
1334 break;
1335 case 2:
1336 WARN_ON(port >= 0xfff);
1337 wwn |= (u64) port << 48;
1338 break;
1339 default:
1340 WARN_ON(1);
1341 break;
1342 }
1343
1344 return wwn;
1345}
1346EXPORT_SYMBOL_GPL(fcoe_wwn_from_mac);
1347
1348/**
1349 * fcoe_libfc_config() - sets up libfc related properties for lport
1350 * @lp: ptr to the fc_lport
1351 * @tt: libfc function template
1352 *
1353 * Returns : 0 for success
1354 */
1355int fcoe_libfc_config(struct fc_lport *lp, struct libfc_function_template *tt)
1356{
1357 /* Set the function pointers set by the LLDD */
1358 memcpy(&lp->tt, tt, sizeof(*tt));
1359 if (fc_fcp_init(lp))
1360 return -ENOMEM;
1361 fc_exch_init(lp);
1362 fc_elsct_init(lp);
1363 fc_lport_init(lp);
1364 fc_rport_init(lp);
1365 fc_disc_init(lp);
1366
1367 return 0;
1368}
1369EXPORT_SYMBOL_GPL(fcoe_libfc_config);