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