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