blob: 2f5bc7fd3fa90c5c3834fef6a377e025feb70f9f [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/**
Chris Leechdd3fd722009-04-21 16:27:36 -0700125 * fcoe_ctlr_destroy() - Disable and tear-down the FCoE controller.
Joe Eykholt97c83892009-03-17 11:42:40 -0700126 * @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));
Chris Leech0f491532009-05-06 10:52:18 -0700218 skb->protocol = htons(ETH_P_FIP);
Joe Eykholt97c83892009-03-17 11:42:40 -0700219 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);
Chris Leech0f491532009-05-06 10:52:18 -0700372 skb->protocol = htons(ETH_P_FIP);
Joe Eykholt97c83892009-03-17 11:42:40 -0700373 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
Chris Leech0f491532009-05-06 10:52:18 -0700435 skb->protocol = htons(ETH_P_FIP);
Joe Eykholt97c83892009-03-17 11:42:40 -0700436 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
Joe Eykholt97c83892009-03-17 11:42:40 -0700458 fh = (struct fc_frame_header *)skb->data;
459 op = *(u8 *)(fh + 1);
460
Joe Eykholt5f48f702009-05-06 10:52:12 -0700461 if (op == ELS_FLOGI) {
Joe Eykholt97c83892009-03-17 11:42:40 -0700462 old_xid = fip->flogi_oxid;
463 fip->flogi_oxid = ntohs(fh->fh_ox_id);
464 if (fip->state == FIP_ST_AUTO) {
465 if (old_xid == FC_XID_UNKNOWN)
466 fip->flogi_count = 0;
467 fip->flogi_count++;
468 if (fip->flogi_count < 3)
469 goto drop;
470 fip->map_dest = 1;
471 return 0;
472 }
Joe Eykholt5f48f702009-05-06 10:52:12 -0700473 if (fip->state == FIP_ST_NON_FIP)
474 fip->map_dest = 1;
475 }
476
477 if (fip->state == FIP_ST_NON_FIP)
478 return 0;
479
480 switch (op) {
481 case ELS_FLOGI:
Joe Eykholt97c83892009-03-17 11:42:40 -0700482 op = FIP_DT_FLOGI;
483 break;
484 case ELS_FDISC:
485 if (ntoh24(fh->fh_s_id))
486 return 0;
487 op = FIP_DT_FDISC;
488 break;
489 case ELS_LOGO:
490 if (fip->state != FIP_ST_ENABLED)
491 return 0;
492 if (ntoh24(fh->fh_d_id) != FC_FID_FLOGI)
493 return 0;
494 op = FIP_DT_LOGO;
495 break;
496 case ELS_LS_ACC:
497 if (fip->flogi_oxid == FC_XID_UNKNOWN)
498 return 0;
499 if (!ntoh24(fh->fh_s_id))
500 return 0;
501 if (fip->state == FIP_ST_AUTO)
502 return 0;
503 /*
504 * Here we must've gotten an SID by accepting an FLOGI
505 * from a point-to-point connection. Switch to using
506 * the source mac based on the SID. The destination
507 * MAC in this case would have been set by receving the
508 * FLOGI.
509 */
510 fip->flogi_oxid = FC_XID_UNKNOWN;
511 fc_fcoe_set_mac(fip->data_src_addr, fh->fh_s_id);
512 return 0;
513 default:
514 if (fip->state != FIP_ST_ENABLED)
515 goto drop;
516 return 0;
517 }
518 if (fcoe_ctlr_encaps(fip, op, skb))
519 goto drop;
520 fip->send(fip, skb);
521 return -EINPROGRESS;
522drop:
523 kfree_skb(skb);
524 return -EINVAL;
525}
526EXPORT_SYMBOL(fcoe_ctlr_els_send);
527
528/*
529 * fcoe_ctlr_age_fcfs() - Reset and free all old FCFs for a controller.
530 * @fip: FCoE controller.
531 *
532 * Called with lock held.
533 *
534 * An FCF is considered old if we have missed three advertisements.
535 * That is, there have been no valid advertisement from it for three
536 * times its keep-alive period including fuzz.
537 *
538 * In addition, determine the time when an FCF selection can occur.
539 */
540static void fcoe_ctlr_age_fcfs(struct fcoe_ctlr *fip)
541{
542 struct fcoe_fcf *fcf;
543 struct fcoe_fcf *next;
544 unsigned long sel_time = 0;
545
546 list_for_each_entry_safe(fcf, next, &fip->fcfs, list) {
547 if (time_after(jiffies, fcf->time + fcf->fka_period * 3 +
548 msecs_to_jiffies(FIP_FCF_FUZZ * 3))) {
549 if (fip->sel_fcf == fcf)
550 fip->sel_fcf = NULL;
551 list_del(&fcf->list);
552 WARN_ON(!fip->fcf_count);
553 fip->fcf_count--;
554 kfree(fcf);
555 } else if (fcoe_ctlr_mtu_valid(fcf) &&
556 (!sel_time || time_before(sel_time, fcf->time))) {
557 sel_time = fcf->time;
558 }
559 }
560 if (sel_time) {
561 sel_time += msecs_to_jiffies(FCOE_CTLR_START_DELAY);
562 fip->sel_time = sel_time;
563 if (time_before(sel_time, fip->timer.expires))
564 mod_timer(&fip->timer, sel_time);
565 } else {
566 fip->sel_time = 0;
567 }
568}
569
570/**
571 * fcoe_ctlr_parse_adv() - Decode a FIP advertisement into a new FCF entry.
572 * @skb: received FIP advertisement frame
573 * @fcf: resulting FCF entry.
574 *
575 * Returns zero on a valid parsed advertisement,
576 * otherwise returns non zero value.
577 */
578static int fcoe_ctlr_parse_adv(struct sk_buff *skb, struct fcoe_fcf *fcf)
579{
580 struct fip_header *fiph;
581 struct fip_desc *desc = NULL;
582 struct fip_wwn_desc *wwn;
583 struct fip_fab_desc *fab;
584 struct fip_fka_desc *fka;
585 unsigned long t;
586 size_t rlen;
587 size_t dlen;
588
589 memset(fcf, 0, sizeof(*fcf));
590 fcf->fka_period = msecs_to_jiffies(FCOE_CTLR_DEF_FKA);
591
592 fiph = (struct fip_header *)skb->data;
593 fcf->flags = ntohs(fiph->fip_flags);
594
595 rlen = ntohs(fiph->fip_dl_len) * 4;
596 if (rlen + sizeof(*fiph) > skb->len)
597 return -EINVAL;
598
599 desc = (struct fip_desc *)(fiph + 1);
600 while (rlen > 0) {
601 dlen = desc->fip_dlen * FIP_BPW;
602 if (dlen < sizeof(*desc) || dlen > rlen)
603 return -EINVAL;
604 switch (desc->fip_dtype) {
605 case FIP_DT_PRI:
606 if (dlen != sizeof(struct fip_pri_desc))
607 goto len_err;
608 fcf->pri = ((struct fip_pri_desc *)desc)->fd_pri;
609 break;
610 case FIP_DT_MAC:
611 if (dlen != sizeof(struct fip_mac_desc))
612 goto len_err;
613 memcpy(fcf->fcf_mac,
614 ((struct fip_mac_desc *)desc)->fd_mac,
615 ETH_ALEN);
616 if (!is_valid_ether_addr(fcf->fcf_mac)) {
617 FIP_DBG("invalid MAC addr in FIP adv\n");
618 return -EINVAL;
619 }
620 break;
621 case FIP_DT_NAME:
622 if (dlen != sizeof(struct fip_wwn_desc))
623 goto len_err;
624 wwn = (struct fip_wwn_desc *)desc;
625 fcf->switch_name = get_unaligned_be64(&wwn->fd_wwn);
626 break;
627 case FIP_DT_FAB:
628 if (dlen != sizeof(struct fip_fab_desc))
629 goto len_err;
630 fab = (struct fip_fab_desc *)desc;
631 fcf->fabric_name = get_unaligned_be64(&fab->fd_wwn);
632 fcf->vfid = ntohs(fab->fd_vfid);
633 fcf->fc_map = ntoh24(fab->fd_map);
634 break;
635 case FIP_DT_FKA:
636 if (dlen != sizeof(struct fip_fka_desc))
637 goto len_err;
638 fka = (struct fip_fka_desc *)desc;
639 t = ntohl(fka->fd_fka_period);
640 if (t >= FCOE_CTLR_MIN_FKA)
641 fcf->fka_period = msecs_to_jiffies(t);
642 break;
643 case FIP_DT_MAP_OUI:
644 case FIP_DT_FCOE_SIZE:
645 case FIP_DT_FLOGI:
646 case FIP_DT_FDISC:
647 case FIP_DT_LOGO:
648 case FIP_DT_ELP:
649 default:
650 FIP_DBG("unexpected descriptor type %x in FIP adv\n",
651 desc->fip_dtype);
652 /* standard says ignore unknown descriptors >= 128 */
653 if (desc->fip_dtype < FIP_DT_VENDOR_BASE)
654 return -EINVAL;
655 continue;
656 }
657 desc = (struct fip_desc *)((char *)desc + dlen);
658 rlen -= dlen;
659 }
660 if (!fcf->fc_map || (fcf->fc_map & 0x10000))
661 return -EINVAL;
662 if (!fcf->switch_name || !fcf->fabric_name)
663 return -EINVAL;
664 return 0;
665
666len_err:
667 FIP_DBG("FIP length error in descriptor type %x len %zu\n",
668 desc->fip_dtype, dlen);
669 return -EINVAL;
670}
671
672/**
673 * fcoe_ctlr_recv_adv() - Handle an incoming advertisement.
674 * @fip: FCoE controller.
675 * @skb: Received FIP packet.
676 */
677static void fcoe_ctlr_recv_adv(struct fcoe_ctlr *fip, struct sk_buff *skb)
678{
679 struct fcoe_fcf *fcf;
680 struct fcoe_fcf new;
681 struct fcoe_fcf *found;
682 unsigned long sol_tov = msecs_to_jiffies(FCOE_CTRL_SOL_TOV);
683 int first = 0;
684 int mtu_valid;
685
686 if (fcoe_ctlr_parse_adv(skb, &new))
687 return;
688
689 spin_lock_bh(&fip->lock);
690 first = list_empty(&fip->fcfs);
691 found = NULL;
692 list_for_each_entry(fcf, &fip->fcfs, list) {
693 if (fcf->switch_name == new.switch_name &&
694 fcf->fabric_name == new.fabric_name &&
695 fcf->fc_map == new.fc_map &&
696 compare_ether_addr(fcf->fcf_mac, new.fcf_mac) == 0) {
697 found = fcf;
698 break;
699 }
700 }
701 if (!found) {
702 if (fip->fcf_count >= FCOE_CTLR_FCF_LIMIT)
703 goto out;
704
705 fcf = kmalloc(sizeof(*fcf), GFP_ATOMIC);
706 if (!fcf)
707 goto out;
708
709 fip->fcf_count++;
710 memcpy(fcf, &new, sizeof(new));
711 list_add(&fcf->list, &fip->fcfs);
712 } else {
713 /*
714 * Flags in advertisements are ignored once the FCF is
715 * selected. Flags in unsolicited advertisements are
716 * ignored after a usable solicited advertisement
717 * has been received.
718 */
719 if (fcf == fip->sel_fcf) {
720 fip->ctlr_ka_time -= fcf->fka_period;
721 fip->ctlr_ka_time += new.fka_period;
722 if (time_before(fip->ctlr_ka_time, fip->timer.expires))
723 mod_timer(&fip->timer, fip->ctlr_ka_time);
724 } else if (!fcoe_ctlr_fcf_usable(fcf))
725 fcf->flags = new.flags;
726 fcf->fka_period = new.fka_period;
727 memcpy(fcf->fcf_mac, new.fcf_mac, ETH_ALEN);
728 }
729 mtu_valid = fcoe_ctlr_mtu_valid(fcf);
730 fcf->time = jiffies;
731 FIP_DBG_LVL(found ? 2 : 1, "%s FCF for fab %llx map %x val %d\n",
732 found ? "old" : "new",
733 fcf->fabric_name, fcf->fc_map, mtu_valid);
734
735 /*
736 * If this advertisement is not solicited and our max receive size
737 * hasn't been verified, send a solicited advertisement.
738 */
739 if (!mtu_valid)
740 fcoe_ctlr_solicit(fip, fcf);
741
742 /*
743 * If its been a while since we did a solicit, and this is
744 * the first advertisement we've received, do a multicast
745 * solicitation to gather as many advertisements as we can
746 * before selection occurs.
747 */
748 if (first && time_after(jiffies, fip->sol_time + sol_tov))
749 fcoe_ctlr_solicit(fip, NULL);
750
751 /*
752 * If this is the first validated FCF, note the time and
753 * set a timer to trigger selection.
754 */
755 if (mtu_valid && !fip->sel_time && fcoe_ctlr_fcf_usable(fcf)) {
756 fip->sel_time = jiffies +
757 msecs_to_jiffies(FCOE_CTLR_START_DELAY);
758 if (!timer_pending(&fip->timer) ||
759 time_before(fip->sel_time, fip->timer.expires))
760 mod_timer(&fip->timer, fip->sel_time);
761 }
762out:
763 spin_unlock_bh(&fip->lock);
764}
765
766/**
767 * fcoe_ctlr_recv_els() - Handle an incoming FIP-encapsulated ELS frame.
768 * @fip: FCoE controller.
769 * @skb: Received FIP packet.
770 */
771static void fcoe_ctlr_recv_els(struct fcoe_ctlr *fip, struct sk_buff *skb)
772{
773 struct fc_lport *lp = fip->lp;
774 struct fip_header *fiph;
775 struct fc_frame *fp;
776 struct fc_frame_header *fh = NULL;
777 struct fip_desc *desc;
778 struct fip_encaps *els;
779 struct fcoe_dev_stats *stats;
780 enum fip_desc_type els_dtype = 0;
781 u8 els_op;
782 u8 sub;
783 u8 granted_mac[ETH_ALEN] = { 0 };
784 size_t els_len = 0;
785 size_t rlen;
786 size_t dlen;
787
788 fiph = (struct fip_header *)skb->data;
789 sub = fiph->fip_subcode;
790 if (sub != FIP_SC_REQ && sub != FIP_SC_REP)
791 goto drop;
792
793 rlen = ntohs(fiph->fip_dl_len) * 4;
794 if (rlen + sizeof(*fiph) > skb->len)
795 goto drop;
796
797 desc = (struct fip_desc *)(fiph + 1);
798 while (rlen > 0) {
799 dlen = desc->fip_dlen * FIP_BPW;
800 if (dlen < sizeof(*desc) || dlen > rlen)
801 goto drop;
802 switch (desc->fip_dtype) {
803 case FIP_DT_MAC:
804 if (dlen != sizeof(struct fip_mac_desc))
805 goto len_err;
806 memcpy(granted_mac,
807 ((struct fip_mac_desc *)desc)->fd_mac,
808 ETH_ALEN);
809 if (!is_valid_ether_addr(granted_mac)) {
810 FIP_DBG("invalid MAC addrs in FIP ELS\n");
811 goto drop;
812 }
813 break;
814 case FIP_DT_FLOGI:
815 case FIP_DT_FDISC:
816 case FIP_DT_LOGO:
817 case FIP_DT_ELP:
818 if (fh)
819 goto drop;
820 if (dlen < sizeof(*els) + sizeof(*fh) + 1)
821 goto len_err;
822 els_len = dlen - sizeof(*els);
823 els = (struct fip_encaps *)desc;
824 fh = (struct fc_frame_header *)(els + 1);
825 els_dtype = desc->fip_dtype;
826 break;
827 default:
828 FIP_DBG("unexpected descriptor type %x "
829 "in FIP adv\n", desc->fip_dtype);
830 /* standard says ignore unknown descriptors >= 128 */
831 if (desc->fip_dtype < FIP_DT_VENDOR_BASE)
832 goto drop;
833 continue;
834 }
835 desc = (struct fip_desc *)((char *)desc + dlen);
836 rlen -= dlen;
837 }
838
839 if (!fh)
840 goto drop;
841 els_op = *(u8 *)(fh + 1);
842
843 if (els_dtype == FIP_DT_FLOGI && sub == FIP_SC_REP &&
844 fip->flogi_oxid == ntohs(fh->fh_ox_id) &&
845 els_op == ELS_LS_ACC && is_valid_ether_addr(granted_mac)) {
846 fip->flogi_oxid = FC_XID_UNKNOWN;
847 fip->update_mac(fip, fip->data_src_addr, granted_mac);
848 memcpy(fip->data_src_addr, granted_mac, ETH_ALEN);
849 }
850
851 /*
852 * Convert skb into an fc_frame containing only the ELS.
853 */
854 skb_pull(skb, (u8 *)fh - skb->data);
855 skb_trim(skb, els_len);
856 fp = (struct fc_frame *)skb;
857 fc_frame_init(fp);
858 fr_sof(fp) = FC_SOF_I3;
859 fr_eof(fp) = FC_EOF_T;
860 fr_dev(fp) = lp;
861
862 stats = fc_lport_get_stats(lp);
863 stats->RxFrames++;
864 stats->RxWords += skb->len / FIP_BPW;
865
866 fc_exch_recv(lp, lp->emp, fp);
867 return;
868
869len_err:
870 FIP_DBG("FIP length error in descriptor type %x len %zu\n",
871 desc->fip_dtype, dlen);
872drop:
873 kfree_skb(skb);
874}
875
876/**
877 * fcoe_ctlr_recv_els() - Handle an incoming link reset frame.
878 * @fip: FCoE controller.
879 * @fh: Received FIP header.
880 *
881 * There may be multiple VN_Port descriptors.
882 * The overall length has already been checked.
883 */
884static void fcoe_ctlr_recv_clr_vlink(struct fcoe_ctlr *fip,
885 struct fip_header *fh)
886{
887 struct fip_desc *desc;
888 struct fip_mac_desc *mp;
889 struct fip_wwn_desc *wp;
890 struct fip_vn_desc *vp;
891 size_t rlen;
892 size_t dlen;
893 struct fcoe_fcf *fcf = fip->sel_fcf;
894 struct fc_lport *lp = fip->lp;
895 u32 desc_mask;
896
897 FIP_DBG("Clear Virtual Link received\n");
898 if (!fcf)
899 return;
900 if (!fcf || !fc_host_port_id(lp->host))
901 return;
902
903 /*
904 * mask of required descriptors. Validating each one clears its bit.
905 */
906 desc_mask = BIT(FIP_DT_MAC) | BIT(FIP_DT_NAME) | BIT(FIP_DT_VN_ID);
907
908 rlen = ntohs(fh->fip_dl_len) * FIP_BPW;
909 desc = (struct fip_desc *)(fh + 1);
910 while (rlen >= sizeof(*desc)) {
911 dlen = desc->fip_dlen * FIP_BPW;
912 if (dlen > rlen)
913 return;
914 switch (desc->fip_dtype) {
915 case FIP_DT_MAC:
916 mp = (struct fip_mac_desc *)desc;
917 if (dlen < sizeof(*mp))
918 return;
919 if (compare_ether_addr(mp->fd_mac, fcf->fcf_mac))
920 return;
921 desc_mask &= ~BIT(FIP_DT_MAC);
922 break;
923 case FIP_DT_NAME:
924 wp = (struct fip_wwn_desc *)desc;
925 if (dlen < sizeof(*wp))
926 return;
927 if (get_unaligned_be64(&wp->fd_wwn) != fcf->switch_name)
928 return;
929 desc_mask &= ~BIT(FIP_DT_NAME);
930 break;
931 case FIP_DT_VN_ID:
932 vp = (struct fip_vn_desc *)desc;
933 if (dlen < sizeof(*vp))
934 return;
935 if (compare_ether_addr(vp->fd_mac,
936 fip->data_src_addr) == 0 &&
937 get_unaligned_be64(&vp->fd_wwpn) == lp->wwpn &&
938 ntoh24(vp->fd_fc_id) == fc_host_port_id(lp->host))
939 desc_mask &= ~BIT(FIP_DT_VN_ID);
940 break;
941 default:
942 /* standard says ignore unknown descriptors >= 128 */
943 if (desc->fip_dtype < FIP_DT_VENDOR_BASE)
944 return;
945 break;
946 }
947 desc = (struct fip_desc *)((char *)desc + dlen);
948 rlen -= dlen;
949 }
950
951 /*
952 * reset only if all required descriptors were present and valid.
953 */
954 if (desc_mask) {
955 FIP_DBG("missing descriptors mask %x\n", desc_mask);
956 } else {
957 FIP_DBG("performing Clear Virtual Link\n");
958 fcoe_ctlr_reset(fip, FIP_ST_ENABLED);
959 }
960}
961
962/**
963 * fcoe_ctlr_recv() - Receive a FIP frame.
964 * @fip: FCoE controller.
965 * @skb: Received FIP packet.
966 *
967 * This is called from NET_RX_SOFTIRQ.
968 */
969void fcoe_ctlr_recv(struct fcoe_ctlr *fip, struct sk_buff *skb)
970{
971 spin_lock_bh(&fip->fip_recv_list.lock);
972 __skb_queue_tail(&fip->fip_recv_list, skb);
973 spin_unlock_bh(&fip->fip_recv_list.lock);
974 schedule_work(&fip->recv_work);
975}
976EXPORT_SYMBOL(fcoe_ctlr_recv);
977
978/**
979 * fcoe_ctlr_recv_handler() - Receive a FIP frame.
980 * @fip: FCoE controller.
981 * @skb: Received FIP packet.
982 *
983 * Returns non-zero if the frame is dropped.
984 */
985static int fcoe_ctlr_recv_handler(struct fcoe_ctlr *fip, struct sk_buff *skb)
986{
987 struct fip_header *fiph;
988 struct ethhdr *eh;
989 enum fip_state state;
990 u16 op;
991 u8 sub;
992
993 if (skb_linearize(skb))
994 goto drop;
995 if (skb->len < sizeof(*fiph))
996 goto drop;
997 eh = eth_hdr(skb);
998 if (compare_ether_addr(eh->h_dest, fip->ctl_src_addr) &&
999 compare_ether_addr(eh->h_dest, FIP_ALL_ENODE_MACS))
1000 goto drop;
1001 fiph = (struct fip_header *)skb->data;
1002 op = ntohs(fiph->fip_op);
1003 sub = fiph->fip_subcode;
1004
1005 FIP_DBG_LVL(2, "ver %x op %x/%x dl %x fl %x\n",
1006 FIP_VER_DECAPS(fiph->fip_ver), op, sub,
1007 ntohs(fiph->fip_dl_len), ntohs(fiph->fip_flags));
1008
1009 if (FIP_VER_DECAPS(fiph->fip_ver) != FIP_VER)
1010 goto drop;
1011 if (ntohs(fiph->fip_dl_len) * FIP_BPW + sizeof(*fiph) > skb->len)
1012 goto drop;
1013
1014 spin_lock_bh(&fip->lock);
1015 state = fip->state;
1016 if (state == FIP_ST_AUTO) {
1017 fip->map_dest = 0;
1018 fip->state = FIP_ST_ENABLED;
1019 state = FIP_ST_ENABLED;
1020 FIP_DBG("using FIP mode\n");
1021 }
1022 spin_unlock_bh(&fip->lock);
1023 if (state != FIP_ST_ENABLED)
1024 goto drop;
1025
1026 if (op == FIP_OP_LS) {
1027 fcoe_ctlr_recv_els(fip, skb); /* consumes skb */
1028 return 0;
1029 }
1030 if (op == FIP_OP_DISC && sub == FIP_SC_ADV)
1031 fcoe_ctlr_recv_adv(fip, skb);
1032 else if (op == FIP_OP_CTRL && sub == FIP_SC_CLR_VLINK)
1033 fcoe_ctlr_recv_clr_vlink(fip, fiph);
1034 kfree_skb(skb);
1035 return 0;
1036drop:
1037 kfree_skb(skb);
1038 return -1;
1039}
1040
1041/**
1042 * fcoe_ctlr_select() - Select the best FCF, if possible.
1043 * @fip: FCoE controller.
1044 *
1045 * If there are conflicting advertisements, no FCF can be chosen.
1046 *
1047 * Called with lock held.
1048 */
1049static void fcoe_ctlr_select(struct fcoe_ctlr *fip)
1050{
1051 struct fcoe_fcf *fcf;
1052 struct fcoe_fcf *best = NULL;
1053
1054 list_for_each_entry(fcf, &fip->fcfs, list) {
1055 FIP_DBG("consider FCF for fab %llx VFID %d map %x val %d\n",
1056 fcf->fabric_name, fcf->vfid,
1057 fcf->fc_map, fcoe_ctlr_mtu_valid(fcf));
1058 if (!fcoe_ctlr_fcf_usable(fcf)) {
1059 FIP_DBG("FCF for fab %llx map %x %svalid %savailable\n",
1060 fcf->fabric_name, fcf->fc_map,
1061 (fcf->flags & FIP_FL_SOL) ? "" : "in",
1062 (fcf->flags & FIP_FL_AVAIL) ? "" : "un");
1063 continue;
1064 }
1065 if (!best) {
1066 best = fcf;
1067 continue;
1068 }
1069 if (fcf->fabric_name != best->fabric_name ||
1070 fcf->vfid != best->vfid ||
1071 fcf->fc_map != best->fc_map) {
1072 FIP_DBG("conflicting fabric, VFID, or FC-MAP\n");
1073 return;
1074 }
1075 if (fcf->pri < best->pri)
1076 best = fcf;
1077 }
1078 fip->sel_fcf = best;
1079}
1080
1081/**
1082 * fcoe_ctlr_timeout() - FIP timer function.
1083 * @arg: &fcoe_ctlr pointer.
1084 *
1085 * Ages FCFs. Triggers FCF selection if possible. Sends keep-alives.
1086 */
1087static void fcoe_ctlr_timeout(unsigned long arg)
1088{
1089 struct fcoe_ctlr *fip = (struct fcoe_ctlr *)arg;
1090 struct fcoe_fcf *sel;
1091 struct fcoe_fcf *fcf;
1092 unsigned long next_timer = jiffies + msecs_to_jiffies(FIP_VN_KA_PERIOD);
1093 DECLARE_MAC_BUF(buf);
1094 u8 send_ctlr_ka;
1095 u8 send_port_ka;
1096
1097 spin_lock_bh(&fip->lock);
1098 if (fip->state == FIP_ST_DISABLED) {
1099 spin_unlock_bh(&fip->lock);
1100 return;
1101 }
1102
1103 fcf = fip->sel_fcf;
1104 fcoe_ctlr_age_fcfs(fip);
1105
1106 sel = fip->sel_fcf;
1107 if (!sel && fip->sel_time && time_after_eq(jiffies, fip->sel_time)) {
1108 fcoe_ctlr_select(fip);
1109 sel = fip->sel_fcf;
1110 fip->sel_time = 0;
1111 }
1112
1113 if (sel != fcf) {
1114 fcf = sel; /* the old FCF may have been freed */
1115 if (sel) {
1116 printk(KERN_INFO "host%d: FIP selected "
1117 "Fibre-Channel Forwarder MAC %s\n",
1118 fip->lp->host->host_no,
1119 print_mac(buf, sel->fcf_mac));
1120 memcpy(fip->dest_addr, sel->fcf_mac, ETH_ALEN);
1121 fip->port_ka_time = jiffies +
1122 msecs_to_jiffies(FIP_VN_KA_PERIOD);
1123 fip->ctlr_ka_time = jiffies + sel->fka_period;
1124 fip->link = 1;
1125 } else {
1126 printk(KERN_NOTICE "host%d: "
1127 "FIP Fibre-Channel Forwarder timed out. "
1128 "Starting FCF discovery.\n",
1129 fip->lp->host->host_no);
1130 fip->link = 0;
1131 }
1132 schedule_work(&fip->link_work);
1133 }
1134
1135 send_ctlr_ka = 0;
1136 send_port_ka = 0;
1137 if (sel) {
1138 if (time_after_eq(jiffies, fip->ctlr_ka_time)) {
1139 fip->ctlr_ka_time = jiffies + sel->fka_period;
1140 send_ctlr_ka = 1;
1141 }
1142 if (time_after(next_timer, fip->ctlr_ka_time))
1143 next_timer = fip->ctlr_ka_time;
1144
1145 if (time_after_eq(jiffies, fip->port_ka_time)) {
1146 fip->port_ka_time += jiffies +
1147 msecs_to_jiffies(FIP_VN_KA_PERIOD);
1148 send_port_ka = 1;
1149 }
1150 if (time_after(next_timer, fip->port_ka_time))
1151 next_timer = fip->port_ka_time;
1152 mod_timer(&fip->timer, next_timer);
1153 } else if (fip->sel_time) {
1154 next_timer = fip->sel_time +
1155 msecs_to_jiffies(FCOE_CTLR_START_DELAY);
1156 mod_timer(&fip->timer, next_timer);
1157 }
1158 spin_unlock_bh(&fip->lock);
1159
1160 if (send_ctlr_ka)
1161 fcoe_ctlr_send_keep_alive(fip, 0, fip->ctl_src_addr);
1162 if (send_port_ka)
1163 fcoe_ctlr_send_keep_alive(fip, 1, fip->data_src_addr);
1164}
1165
1166/**
1167 * fcoe_ctlr_link_work() - worker thread function for link changes.
1168 * @work: pointer to link_work member inside &fcoe_ctlr.
1169 *
1170 * See if the link status has changed and if so, report it.
1171 *
1172 * This is here because fc_linkup() and fc_linkdown() must not
1173 * be called from the timer directly, since they use a mutex.
1174 */
1175static void fcoe_ctlr_link_work(struct work_struct *work)
1176{
1177 struct fcoe_ctlr *fip;
1178 int link;
1179 int last_link;
1180
1181 fip = container_of(work, struct fcoe_ctlr, link_work);
1182 spin_lock_bh(&fip->lock);
1183 last_link = fip->last_link;
1184 link = fip->link;
1185 fip->last_link = link;
1186 spin_unlock_bh(&fip->lock);
1187
1188 if (last_link != link) {
1189 if (link)
1190 fc_linkup(fip->lp);
1191 else
1192 fcoe_ctlr_reset(fip, FIP_ST_LINK_WAIT);
1193 }
1194}
1195
1196/**
1197 * fcoe_ctlr_recv_work() - Worker thread function for receiving FIP frames.
1198 * @recv_work: pointer to recv_work member inside &fcoe_ctlr.
1199 */
1200static void fcoe_ctlr_recv_work(struct work_struct *recv_work)
1201{
1202 struct fcoe_ctlr *fip;
1203 struct sk_buff *skb;
1204
1205 fip = container_of(recv_work, struct fcoe_ctlr, recv_work);
1206 spin_lock_bh(&fip->fip_recv_list.lock);
1207 while ((skb = __skb_dequeue(&fip->fip_recv_list))) {
1208 spin_unlock_bh(&fip->fip_recv_list.lock);
1209 fcoe_ctlr_recv_handler(fip, skb);
1210 spin_lock_bh(&fip->fip_recv_list.lock);
1211 }
1212 spin_unlock_bh(&fip->fip_recv_list.lock);
1213}
1214
1215/**
1216 * fcoe_ctlr_recv_flogi() - snoop Pre-FIP receipt of FLOGI response or request.
1217 * @fip: FCoE controller.
1218 * @fp: FC frame.
1219 * @sa: Ethernet source MAC address from received FCoE frame.
1220 *
1221 * Snoop potential response to FLOGI or even incoming FLOGI.
1222 *
1223 * The caller has checked that we are waiting for login as indicated
1224 * by fip->flogi_oxid != FC_XID_UNKNOWN.
1225 *
1226 * The caller is responsible for freeing the frame.
1227 *
1228 * Return non-zero if the frame should not be delivered to libfc.
1229 */
1230int fcoe_ctlr_recv_flogi(struct fcoe_ctlr *fip, struct fc_frame *fp, u8 *sa)
1231{
1232 struct fc_frame_header *fh;
1233 u8 op;
1234 u8 mac[ETH_ALEN];
1235
1236 fh = fc_frame_header_get(fp);
1237 if (fh->fh_type != FC_TYPE_ELS)
1238 return 0;
1239
1240 op = fc_frame_payload_op(fp);
1241 if (op == ELS_LS_ACC && fh->fh_r_ctl == FC_RCTL_ELS_REP &&
1242 fip->flogi_oxid == ntohs(fh->fh_ox_id)) {
1243
1244 spin_lock_bh(&fip->lock);
1245 if (fip->state != FIP_ST_AUTO && fip->state != FIP_ST_NON_FIP) {
1246 spin_unlock_bh(&fip->lock);
1247 return -EINVAL;
1248 }
1249 fip->state = FIP_ST_NON_FIP;
1250 FIP_DBG("received FLOGI LS_ACC using non-FIP mode\n");
1251
1252 /*
1253 * FLOGI accepted.
1254 * If the src mac addr is FC_OUI-based, then we mark the
1255 * address_mode flag to use FC_OUI-based Ethernet DA.
1256 * Otherwise we use the FCoE gateway addr
1257 */
1258 if (!compare_ether_addr(sa, (u8[6])FC_FCOE_FLOGI_MAC)) {
1259 fip->map_dest = 1;
1260 } else {
1261 memcpy(fip->dest_addr, sa, ETH_ALEN);
1262 fip->map_dest = 0;
1263 }
1264 fip->flogi_oxid = FC_XID_UNKNOWN;
1265 memcpy(mac, fip->data_src_addr, ETH_ALEN);
1266 fc_fcoe_set_mac(fip->data_src_addr, fh->fh_d_id);
1267 spin_unlock_bh(&fip->lock);
1268
1269 fip->update_mac(fip, mac, fip->data_src_addr);
1270 } else if (op == ELS_FLOGI && fh->fh_r_ctl == FC_RCTL_ELS_REQ && sa) {
1271 /*
1272 * Save source MAC for point-to-point responses.
1273 */
1274 spin_lock_bh(&fip->lock);
1275 if (fip->state == FIP_ST_AUTO || fip->state == FIP_ST_NON_FIP) {
1276 memcpy(fip->dest_addr, sa, ETH_ALEN);
1277 fip->map_dest = 0;
1278 if (fip->state == FIP_ST_NON_FIP)
1279 FIP_DBG("received FLOGI REQ, "
1280 "using non-FIP mode\n");
1281 fip->state = FIP_ST_NON_FIP;
1282 }
1283 spin_unlock_bh(&fip->lock);
1284 }
1285 return 0;
1286}
1287EXPORT_SYMBOL(fcoe_ctlr_recv_flogi);
1288
Vasu Dev5e80f7f2009-03-17 11:42:18 -07001289/**
1290 * fcoe_wwn_from_mac() - Converts 48-bit IEEE MAC address to 64-bit FC WWN.
1291 * @mac: mac address
1292 * @scheme: check port
1293 * @port: port indicator for converting
1294 *
1295 * Returns: u64 fc world wide name
1296 */
1297u64 fcoe_wwn_from_mac(unsigned char mac[MAX_ADDR_LEN],
1298 unsigned int scheme, unsigned int port)
1299{
1300 u64 wwn;
1301 u64 host_mac;
1302
1303 /* The MAC is in NO, so flip only the low 48 bits */
1304 host_mac = ((u64) mac[0] << 40) |
1305 ((u64) mac[1] << 32) |
1306 ((u64) mac[2] << 24) |
1307 ((u64) mac[3] << 16) |
1308 ((u64) mac[4] << 8) |
1309 (u64) mac[5];
1310
1311 WARN_ON(host_mac >= (1ULL << 48));
1312 wwn = host_mac | ((u64) scheme << 60);
1313 switch (scheme) {
1314 case 1:
1315 WARN_ON(port != 0);
1316 break;
1317 case 2:
1318 WARN_ON(port >= 0xfff);
1319 wwn |= (u64) port << 48;
1320 break;
1321 default:
1322 WARN_ON(1);
1323 break;
1324 }
1325
1326 return wwn;
1327}
1328EXPORT_SYMBOL_GPL(fcoe_wwn_from_mac);
1329
1330/**
1331 * fcoe_libfc_config() - sets up libfc related properties for lport
1332 * @lp: ptr to the fc_lport
1333 * @tt: libfc function template
1334 *
1335 * Returns : 0 for success
1336 */
1337int fcoe_libfc_config(struct fc_lport *lp, struct libfc_function_template *tt)
1338{
1339 /* Set the function pointers set by the LLDD */
1340 memcpy(&lp->tt, tt, sizeof(*tt));
1341 if (fc_fcp_init(lp))
1342 return -ENOMEM;
1343 fc_exch_init(lp);
1344 fc_elsct_init(lp);
1345 fc_lport_init(lp);
1346 fc_rport_init(lp);
1347 fc_disc_init(lp);
1348
1349 return 0;
1350}
1351EXPORT_SYMBOL_GPL(fcoe_libfc_config);