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