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