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