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