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