Merge tag 'am654-fixes-for-v5.2' of git://git.kernel.org/pub/scm/linux/kernel/git...
[linux-2.6-block.git] / drivers / scsi / fcoe / fcoe_ctlr.c
CommitLineData
a61127c2 1// SPDX-License-Identifier: GPL-2.0-only
9b34ecff 2/*
97c8389d
JE
3 * Copyright (c) 2008-2009 Cisco Systems, Inc. All rights reserved.
4 * Copyright (c) 2009 Intel Corporation. All rights reserved.
9b34ecff 5 *
9b34ecff
VD
6 * Maintained at www.Open-FCoE.org
7 */
8
97c8389d 9#include <linux/types.h>
9b34ecff 10#include <linux/module.h>
97c8389d
JE
11#include <linux/kernel.h>
12#include <linux/list.h>
13#include <linux/spinlock.h>
14#include <linux/timer.h>
5e80f7f7 15#include <linux/netdevice.h>
97c8389d
JE
16#include <linux/etherdevice.h>
17#include <linux/ethtool.h>
18#include <linux/if_ether.h>
19#include <linux/if_vlan.h>
97c8389d
JE
20#include <linux/errno.h>
21#include <linux/bitops.h>
5a0e3ad6 22#include <linux/slab.h>
97c8389d
JE
23#include <net/rtnetlink.h>
24
25#include <scsi/fc/fc_els.h>
26#include <scsi/fc/fc_fs.h>
27#include <scsi/fc/fc_fip.h>
28#include <scsi/fc/fc_encaps.h>
29#include <scsi/fc/fc_fcoe.h>
e10f8c66 30#include <scsi/fc/fc_fcp.h>
5e80f7f7
VD
31
32#include <scsi/libfc.h>
97c8389d 33#include <scsi/libfcoe.h>
9b34ecff 34
21b7b2f5
YZ
35#include "libfcoe.h"
36
97c8389d
JE
37#define FCOE_CTLR_MIN_FKA 500 /* min keep alive (mS) */
38#define FCOE_CTLR_DEF_FKA FIP_DEF_FKA /* default keep alive (mS) */
39
e99e88a9 40static void fcoe_ctlr_timeout(struct timer_list *);
42913657 41static void fcoe_ctlr_timer_work(struct work_struct *);
97c8389d 42static void fcoe_ctlr_recv_work(struct work_struct *);
794d98e7 43static int fcoe_ctlr_flogi_retry(struct fcoe_ctlr *);
97c8389d 44
e10f8c66
JE
45static void fcoe_ctlr_vn_start(struct fcoe_ctlr *);
46static int fcoe_ctlr_vn_recv(struct fcoe_ctlr *, struct sk_buff *);
47static void fcoe_ctlr_vn_timeout(struct fcoe_ctlr *);
48static int fcoe_ctlr_vn_lookup(struct fcoe_ctlr *, u32, u8 *);
49
9a6cf881
HR
50static int fcoe_ctlr_vlan_recv(struct fcoe_ctlr *, struct sk_buff *);
51
97c8389d 52static u8 fcoe_all_fcfs[ETH_ALEN] = FIP_ALL_FCF_MACS;
e10f8c66
JE
53static u8 fcoe_all_enode[ETH_ALEN] = FIP_ALL_ENODE_MACS;
54static u8 fcoe_all_vn2vn[ETH_ALEN] = FIP_ALL_VN2VN_MACS;
55static u8 fcoe_all_p2p[ETH_ALEN] = FIP_ALL_P2P_MACS;
97c8389d 56
0095a921 57static const char * const fcoe_ctlr_states[] = {
9b651da9
JE
58 [FIP_ST_DISABLED] = "DISABLED",
59 [FIP_ST_LINK_WAIT] = "LINK_WAIT",
60 [FIP_ST_AUTO] = "AUTO",
61 [FIP_ST_NON_FIP] = "NON_FIP",
62 [FIP_ST_ENABLED] = "ENABLED",
e10f8c66
JE
63 [FIP_ST_VNMP_START] = "VNMP_START",
64 [FIP_ST_VNMP_PROBE1] = "VNMP_PROBE1",
65 [FIP_ST_VNMP_PROBE2] = "VNMP_PROBE2",
66 [FIP_ST_VNMP_CLAIM] = "VNMP_CLAIM",
67 [FIP_ST_VNMP_UP] = "VNMP_UP",
9b651da9
JE
68};
69
70static const char *fcoe_ctlr_state(enum fip_state state)
71{
72 const char *cp = "unknown";
73
74 if (state < ARRAY_SIZE(fcoe_ctlr_states))
75 cp = fcoe_ctlr_states[state];
76 if (!cp)
77 cp = "unknown";
78 return cp;
79}
80
81/**
82 * fcoe_ctlr_set_state() - Set and do debug printing for the new FIP state.
83 * @fip: The FCoE controller
84 * @state: The new state
85 */
86static void fcoe_ctlr_set_state(struct fcoe_ctlr *fip, enum fip_state state)
87{
88 if (state == fip->state)
89 return;
90 if (fip->lp)
91 LIBFCOE_FIP_DBG(fip, "state %s -> %s\n",
92 fcoe_ctlr_state(fip->state), fcoe_ctlr_state(state));
93 fip->state = state;
94}
95
70b51aab
RL
96/**
97 * fcoe_ctlr_mtu_valid() - Check if a FCF's MTU is valid
98 * @fcf: The FCF to check
99 *
97c8389d
JE
100 * Return non-zero if FCF fcoe_size has been validated.
101 */
102static inline int fcoe_ctlr_mtu_valid(const struct fcoe_fcf *fcf)
103{
104 return (fcf->flags & FIP_FL_SOL) != 0;
105}
106
70b51aab
RL
107/**
108 * fcoe_ctlr_fcf_usable() - Check if a FCF is usable
109 * @fcf: The FCF to check
110 *
97c8389d
JE
111 * Return non-zero if the FCF is usable.
112 */
113static inline int fcoe_ctlr_fcf_usable(struct fcoe_fcf *fcf)
114{
115 u16 flags = FIP_FL_SOL | FIP_FL_AVAIL;
116
117 return (fcf->flags & flags) == flags;
118}
119
cd229e42
JE
120/**
121 * fcoe_ctlr_map_dest() - Set flag and OUI for mapping destination addresses
122 * @fip: The FCoE controller
123 */
124static void fcoe_ctlr_map_dest(struct fcoe_ctlr *fip)
125{
126 if (fip->mode == FIP_MODE_VN2VN)
127 hton24(fip->dest_addr, FIP_VN_FC_MAP);
128 else
129 hton24(fip->dest_addr, FIP_DEF_FC_MAP);
130 hton24(fip->dest_addr + 3, 0);
131 fip->map_dest = 1;
132}
133
97c8389d 134/**
70b51aab
RL
135 * fcoe_ctlr_init() - Initialize the FCoE Controller instance
136 * @fip: The FCoE controller to initialize
97c8389d 137 */
8beb90aa 138void fcoe_ctlr_init(struct fcoe_ctlr *fip, enum fip_mode mode)
97c8389d 139{
9b651da9 140 fcoe_ctlr_set_state(fip, FIP_ST_LINK_WAIT);
3d902ac0 141 fip->mode = mode;
9a6cf881 142 fip->fip_resp = false;
97c8389d 143 INIT_LIST_HEAD(&fip->fcfs);
fdb068c6 144 mutex_init(&fip->ctlr_mutex);
794d98e7 145 spin_lock_init(&fip->ctlr_lock);
97c8389d 146 fip->flogi_oxid = FC_XID_UNKNOWN;
e99e88a9 147 timer_setup(&fip->timer, fcoe_ctlr_timeout, 0);
42913657 148 INIT_WORK(&fip->timer_work, fcoe_ctlr_timer_work);
97c8389d
JE
149 INIT_WORK(&fip->recv_work, fcoe_ctlr_recv_work);
150 skb_queue_head_init(&fip->fip_recv_list);
151}
152EXPORT_SYMBOL(fcoe_ctlr_init);
153
9d34876f
RL
154/**
155 * fcoe_sysfs_fcf_add() - Add a fcoe_fcf{,_device} to a fcoe_ctlr{,_device}
156 * @new: The newly discovered FCF
157 *
158 * Called with fip->ctlr_mutex held
159 */
8d55e507
RL
160static int fcoe_sysfs_fcf_add(struct fcoe_fcf *new)
161{
162 struct fcoe_ctlr *fip = new->fip;
9d34876f 163 struct fcoe_ctlr_device *ctlr_dev;
1c2c1b4f
BVA
164 struct fcoe_fcf_device *temp, *fcf_dev;
165 int rc = -ENOMEM;
8d55e507
RL
166
167 LIBFCOE_FIP_DBG(fip, "New FCF fab %16.16llx mac %pM\n",
168 new->fabric_name, new->fcf_mac);
169
1c2c1b4f
BVA
170 temp = kzalloc(sizeof(*temp), GFP_KERNEL);
171 if (!temp)
172 goto out;
173
1c2c1b4f
BVA
174 temp->fabric_name = new->fabric_name;
175 temp->switch_name = new->switch_name;
176 temp->fc_map = new->fc_map;
177 temp->vfid = new->vfid;
178 memcpy(temp->mac, new->fcf_mac, ETH_ALEN);
179 temp->priority = new->pri;
180 temp->fka_period = new->fka_period;
181 temp->selected = 0; /* default to unselected */
182
8d55e507 183 /*
9d34876f
RL
184 * If ctlr_dev doesn't exist then it means we're a libfcoe user
185 * who doesn't use fcoe_syfs and didn't allocate a fcoe_ctlr_device.
186 * fnic would be an example of a driver with this behavior. In this
187 * case we want to add the fcoe_fcf to the fcoe_ctlr list, but we
188 * don't want to make sysfs changes.
8d55e507 189 */
8d55e507 190
9d34876f
RL
191 ctlr_dev = fcoe_ctlr_to_ctlr_dev(fip);
192 if (ctlr_dev) {
193 mutex_lock(&ctlr_dev->lock);
194 fcf_dev = fcoe_fcf_device_add(ctlr_dev, temp);
195 if (unlikely(!fcf_dev)) {
196 rc = -ENOMEM;
55d0ac5d 197 mutex_unlock(&ctlr_dev->lock);
9d34876f
RL
198 goto out;
199 }
200
201 /*
202 * The fcoe_sysfs layer can return a CONNECTED fcf that
203 * has a priv (fcf was never deleted) or a CONNECTED fcf
204 * that doesn't have a priv (fcf was deleted). However,
205 * libfcoe will always delete FCFs before trying to add
206 * them. This is ensured because both recv_adv and
207 * age_fcfs are protected by the the fcoe_ctlr's mutex.
208 * This means that we should never get a FCF with a
209 * non-NULL priv pointer.
210 */
211 BUG_ON(fcf_dev->priv);
212
213 fcf_dev->priv = new;
214 new->fcf_dev = fcf_dev;
215 mutex_unlock(&ctlr_dev->lock);
216 }
8d55e507
RL
217
218 list_add(&new->list, &fip->fcfs);
219 fip->fcf_count++;
1c2c1b4f 220 rc = 0;
8d55e507 221
1c2c1b4f
BVA
222out:
223 kfree(temp);
8d55e507
RL
224 return rc;
225}
226
9d34876f
RL
227/**
228 * fcoe_sysfs_fcf_del() - Remove a fcoe_fcf{,_device} to a fcoe_ctlr{,_device}
229 * @new: The FCF to be removed
230 *
231 * Called with fip->ctlr_mutex held
232 */
8d55e507
RL
233static void fcoe_sysfs_fcf_del(struct fcoe_fcf *new)
234{
235 struct fcoe_ctlr *fip = new->fip;
9d34876f 236 struct fcoe_ctlr_device *cdev;
8d55e507
RL
237 struct fcoe_fcf_device *fcf_dev;
238
239 list_del(&new->list);
240 fip->fcf_count--;
241
9d34876f
RL
242 /*
243 * If ctlr_dev doesn't exist then it means we're a libfcoe user
244 * who doesn't use fcoe_syfs and didn't allocate a fcoe_ctlr_device
245 * or a fcoe_fcf_device.
246 *
247 * fnic would be an example of a driver with this behavior. In this
248 * case we want to remove the fcoe_fcf from the fcoe_ctlr list (above),
249 * but we don't want to make sysfs changes.
250 */
251 cdev = fcoe_ctlr_to_ctlr_dev(fip);
252 if (cdev) {
253 mutex_lock(&cdev->lock);
254 fcf_dev = fcoe_fcf_to_fcf_dev(new);
255 WARN_ON(!fcf_dev);
256 new->fcf_dev = NULL;
257 fcoe_fcf_device_delete(fcf_dev);
258 kfree(new);
259 mutex_unlock(&cdev->lock);
260 }
8d55e507
RL
261}
262
97c8389d 263/**
70b51aab
RL
264 * fcoe_ctlr_reset_fcfs() - Reset and free all FCFs for a controller
265 * @fip: The FCoE controller whose FCFs are to be reset
97c8389d
JE
266 *
267 * Called with &fcoe_ctlr lock held.
268 */
269static void fcoe_ctlr_reset_fcfs(struct fcoe_ctlr *fip)
270{
271 struct fcoe_fcf *fcf;
272 struct fcoe_fcf *next;
273
274 fip->sel_fcf = NULL;
275 list_for_each_entry_safe(fcf, next, &fip->fcfs, list) {
8d55e507 276 fcoe_sysfs_fcf_del(fcf);
97c8389d 277 }
8d55e507
RL
278 WARN_ON(fip->fcf_count);
279
97c8389d
JE
280 fip->sel_time = 0;
281}
282
283/**
70b51aab
RL
284 * fcoe_ctlr_destroy() - Disable and tear down a FCoE controller
285 * @fip: The FCoE controller to tear down
97c8389d
JE
286 *
287 * This is called by FCoE drivers before freeing the &fcoe_ctlr.
288 *
289 * The receive handler will have been deleted before this to guarantee
290 * that no more recv_work will be scheduled.
291 *
292 * The timer routine will simply return once we set FIP_ST_DISABLED.
293 * This guarantees that no further timeouts or work will be scheduled.
294 */
295void fcoe_ctlr_destroy(struct fcoe_ctlr *fip)
296{
a4b7cfae 297 cancel_work_sync(&fip->recv_work);
1f4aed81 298 skb_queue_purge(&fip->fip_recv_list);
a4b7cfae 299
fdb068c6 300 mutex_lock(&fip->ctlr_mutex);
9b651da9 301 fcoe_ctlr_set_state(fip, FIP_ST_DISABLED);
97c8389d 302 fcoe_ctlr_reset_fcfs(fip);
fdb068c6 303 mutex_unlock(&fip->ctlr_mutex);
97c8389d 304 del_timer_sync(&fip->timer);
42913657 305 cancel_work_sync(&fip->timer_work);
97c8389d
JE
306}
307EXPORT_SYMBOL(fcoe_ctlr_destroy);
308
69316ee2 309/**
794d98e7 310 * fcoe_ctlr_announce() - announce new FCF selection
69316ee2
JE
311 * @fip: The FCoE controller
312 *
313 * Also sets the destination MAC for FCoE and control packets
794d98e7
JE
314 *
315 * Called with neither ctlr_mutex nor ctlr_lock held.
69316ee2
JE
316 */
317static void fcoe_ctlr_announce(struct fcoe_ctlr *fip)
318{
794d98e7
JE
319 struct fcoe_fcf *sel;
320 struct fcoe_fcf *fcf;
321
322 mutex_lock(&fip->ctlr_mutex);
323 spin_lock_bh(&fip->ctlr_lock);
324
325 kfree_skb(fip->flogi_req);
326 fip->flogi_req = NULL;
327 list_for_each_entry(fcf, &fip->fcfs, list)
328 fcf->flogi_sent = 0;
329
330 spin_unlock_bh(&fip->ctlr_lock);
331 sel = fip->sel_fcf;
69316ee2 332
6942df7f 333 if (sel && ether_addr_equal(sel->fcf_mac, fip->dest_addr))
794d98e7 334 goto unlock;
69316ee2
JE
335 if (!is_zero_ether_addr(fip->dest_addr)) {
336 printk(KERN_NOTICE "libfcoe: host%d: "
337 "FIP Fibre-Channel Forwarder MAC %pM deselected\n",
338 fip->lp->host->host_no, fip->dest_addr);
339 memset(fip->dest_addr, 0, ETH_ALEN);
340 }
341 if (sel) {
342 printk(KERN_INFO "libfcoe: host%d: FIP selected "
343 "Fibre-Channel Forwarder MAC %pM\n",
344 fip->lp->host->host_no, sel->fcf_mac);
81c11dd2 345 memcpy(fip->dest_addr, sel->fcoe_mac, ETH_ALEN);
69316ee2
JE
346 fip->map_dest = 0;
347 }
794d98e7
JE
348unlock:
349 mutex_unlock(&fip->ctlr_mutex);
69316ee2
JE
350}
351
97c8389d 352/**
70b51aab
RL
353 * fcoe_ctlr_fcoe_size() - Return the maximum FCoE size required for VN_Port
354 * @fip: The FCoE controller to get the maximum FCoE size from
97c8389d
JE
355 *
356 * Returns the maximum packet size including the FCoE header and trailer,
357 * but not including any Ethernet or VLAN headers.
358 */
359static inline u32 fcoe_ctlr_fcoe_size(struct fcoe_ctlr *fip)
360{
361 /*
362 * Determine the max FCoE frame size allowed, including
363 * FCoE header and trailer.
364 * Note: lp->mfs is currently the payload size, not the frame size.
365 */
366 return fip->lp->mfs + sizeof(struct fc_frame_header) +
367 sizeof(struct fcoe_hdr) + sizeof(struct fcoe_crc_eof);
368}
369
370/**
70b51aab
RL
371 * fcoe_ctlr_solicit() - Send a FIP solicitation
372 * @fip: The FCoE controller to send the solicitation on
373 * @fcf: The destination FCF (if NULL, a multicast solicitation is sent)
97c8389d
JE
374 */
375static void fcoe_ctlr_solicit(struct fcoe_ctlr *fip, struct fcoe_fcf *fcf)
376{
377 struct sk_buff *skb;
378 struct fip_sol {
379 struct ethhdr eth;
380 struct fip_header fip;
381 struct {
382 struct fip_mac_desc mac;
383 struct fip_wwn_desc wwnn;
384 struct fip_size_desc size;
0095a921
YZ
385 } __packed desc;
386 } __packed * sol;
97c8389d
JE
387 u32 fcoe_size;
388
389 skb = dev_alloc_skb(sizeof(*sol));
390 if (!skb)
391 return;
392
393 sol = (struct fip_sol *)skb->data;
394
395 memset(sol, 0, sizeof(*sol));
396 memcpy(sol->eth.h_dest, fcf ? fcf->fcf_mac : fcoe_all_fcfs, ETH_ALEN);
397 memcpy(sol->eth.h_source, fip->ctl_src_addr, ETH_ALEN);
398 sol->eth.h_proto = htons(ETH_P_FIP);
399
400 sol->fip.fip_ver = FIP_VER_ENCAPS(FIP_VER);
401 sol->fip.fip_op = htons(FIP_OP_DISC);
402 sol->fip.fip_subcode = FIP_SC_SOL;
403 sol->fip.fip_dl_len = htons(sizeof(sol->desc) / FIP_BPW);
404 sol->fip.fip_flags = htons(FIP_FL_FPMA);
184dd345
VD
405 if (fip->spma)
406 sol->fip.fip_flags |= htons(FIP_FL_SPMA);
97c8389d
JE
407
408 sol->desc.mac.fd_desc.fip_dtype = FIP_DT_MAC;
409 sol->desc.mac.fd_desc.fip_dlen = sizeof(sol->desc.mac) / FIP_BPW;
410 memcpy(sol->desc.mac.fd_mac, fip->ctl_src_addr, ETH_ALEN);
411
412 sol->desc.wwnn.fd_desc.fip_dtype = FIP_DT_NAME;
413 sol->desc.wwnn.fd_desc.fip_dlen = sizeof(sol->desc.wwnn) / FIP_BPW;
414 put_unaligned_be64(fip->lp->wwnn, &sol->desc.wwnn.fd_wwn);
415
416 fcoe_size = fcoe_ctlr_fcoe_size(fip);
417 sol->desc.size.fd_desc.fip_dtype = FIP_DT_FCOE_SIZE;
418 sol->desc.size.fd_desc.fip_dlen = sizeof(sol->desc.size) / FIP_BPW;
419 sol->desc.size.fd_size = htons(fcoe_size);
420
421 skb_put(skb, sizeof(*sol));
0f491539 422 skb->protocol = htons(ETH_P_FIP);
6f6c2aa3 423 skb->priority = fip->priority;
97c8389d
JE
424 skb_reset_mac_header(skb);
425 skb_reset_network_header(skb);
426 fip->send(fip, skb);
427
428 if (!fcf)
429 fip->sol_time = jiffies;
430}
431
432/**
70b51aab
RL
433 * fcoe_ctlr_link_up() - Start FCoE controller
434 * @fip: The FCoE controller to start
97c8389d
JE
435 *
436 * Called from the LLD when the network link is ready.
437 */
438void fcoe_ctlr_link_up(struct fcoe_ctlr *fip)
439{
fdb068c6 440 mutex_lock(&fip->ctlr_mutex);
97c8389d 441 if (fip->state == FIP_ST_NON_FIP || fip->state == FIP_ST_AUTO) {
fdb068c6 442 mutex_unlock(&fip->ctlr_mutex);
97c8389d
JE
443 fc_linkup(fip->lp);
444 } else if (fip->state == FIP_ST_LINK_WAIT) {
8beb90aa
SD
445 if (fip->mode == FIP_MODE_NON_FIP)
446 fcoe_ctlr_set_state(fip, FIP_ST_NON_FIP);
447 else
448 fcoe_ctlr_set_state(fip, FIP_ST_AUTO);
e10f8c66
JE
449 switch (fip->mode) {
450 default:
451 LIBFCOE_FIP_DBG(fip, "invalid mode %d\n", fip->mode);
452 /* fall-through */
453 case FIP_MODE_AUTO:
0f51c2e5 454 LIBFCOE_FIP_DBG(fip, "%s", "setting AUTO mode.\n");
e10f8c66
JE
455 /* fall-through */
456 case FIP_MODE_FABRIC:
457 case FIP_MODE_NON_FIP:
458 mutex_unlock(&fip->ctlr_mutex);
459 fc_linkup(fip->lp);
460 fcoe_ctlr_solicit(fip, NULL);
461 break;
462 case FIP_MODE_VN2VN:
463 fcoe_ctlr_vn_start(fip);
464 mutex_unlock(&fip->ctlr_mutex);
465 fc_linkup(fip->lp);
466 break;
467 }
97c8389d 468 } else
fdb068c6 469 mutex_unlock(&fip->ctlr_mutex);
97c8389d
JE
470}
471EXPORT_SYMBOL(fcoe_ctlr_link_up);
472
473/**
70b51aab
RL
474 * fcoe_ctlr_reset() - Reset a FCoE controller
475 * @fip: The FCoE controller to reset
97c8389d 476 */
dd42dac4 477static void fcoe_ctlr_reset(struct fcoe_ctlr *fip)
97c8389d 478{
97c8389d
JE
479 fcoe_ctlr_reset_fcfs(fip);
480 del_timer(&fip->timer);
97c8389d
JE
481 fip->ctlr_ka_time = 0;
482 fip->port_ka_time = 0;
483 fip->sol_time = 0;
484 fip->flogi_oxid = FC_XID_UNKNOWN;
cd229e42 485 fcoe_ctlr_map_dest(fip);
97c8389d
JE
486}
487
488/**
70b51aab
RL
489 * fcoe_ctlr_link_down() - Stop a FCoE controller
490 * @fip: The FCoE controller to be stopped
97c8389d
JE
491 *
492 * Returns non-zero if the link was up and now isn't.
493 *
494 * Called from the LLD when the network link is not ready.
495 * There may be multiple calls while the link is down.
496 */
497int fcoe_ctlr_link_down(struct fcoe_ctlr *fip)
498{
dd42dac4
JE
499 int link_dropped;
500
501 LIBFCOE_FIP_DBG(fip, "link down.\n");
fdb068c6 502 mutex_lock(&fip->ctlr_mutex);
dd42dac4 503 fcoe_ctlr_reset(fip);
42913657 504 link_dropped = fip->state != FIP_ST_LINK_WAIT;
9b651da9 505 fcoe_ctlr_set_state(fip, FIP_ST_LINK_WAIT);
fdb068c6 506 mutex_unlock(&fip->ctlr_mutex);
dd42dac4
JE
507
508 if (link_dropped)
509 fc_linkdown(fip->lp);
510 return link_dropped;
97c8389d
JE
511}
512EXPORT_SYMBOL(fcoe_ctlr_link_down);
513
514/**
70b51aab
RL
515 * fcoe_ctlr_send_keep_alive() - Send a keep-alive to the selected FCF
516 * @fip: The FCoE controller to send the FKA on
517 * @lport: libfc fc_lport to send from
518 * @ports: 0 for controller keep-alive, 1 for port keep-alive
519 * @sa: The source MAC address
97c8389d
JE
520 *
521 * A controller keep-alive is sent every fka_period (typically 8 seconds).
522 * The source MAC is the native MAC address.
523 *
524 * A port keep-alive is sent every 90 seconds while logged in.
525 * The source MAC is the assigned mapped source address.
526 * The destination is the FCF's F-port.
527 */
11b56188
CL
528static void fcoe_ctlr_send_keep_alive(struct fcoe_ctlr *fip,
529 struct fc_lport *lport,
530 int ports, u8 *sa)
97c8389d
JE
531{
532 struct sk_buff *skb;
533 struct fip_kal {
534 struct ethhdr eth;
535 struct fip_header fip;
536 struct fip_mac_desc mac;
0095a921 537 } __packed * kal;
97c8389d
JE
538 struct fip_vn_desc *vn;
539 u32 len;
540 struct fc_lport *lp;
541 struct fcoe_fcf *fcf;
542
543 fcf = fip->sel_fcf;
544 lp = fip->lp;
281ae642 545 if (!fcf || (ports && !lp->port_id))
97c8389d
JE
546 return;
547
be276cbe 548 len = sizeof(*kal) + ports * sizeof(*vn);
97c8389d
JE
549 skb = dev_alloc_skb(len);
550 if (!skb)
551 return;
552
553 kal = (struct fip_kal *)skb->data;
554 memset(kal, 0, len);
555 memcpy(kal->eth.h_dest, fcf->fcf_mac, ETH_ALEN);
556 memcpy(kal->eth.h_source, sa, ETH_ALEN);
557 kal->eth.h_proto = htons(ETH_P_FIP);
558
559 kal->fip.fip_ver = FIP_VER_ENCAPS(FIP_VER);
560 kal->fip.fip_op = htons(FIP_OP_CTRL);
561 kal->fip.fip_subcode = FIP_SC_KEEP_ALIVE;
562 kal->fip.fip_dl_len = htons((sizeof(kal->mac) +
70b51aab 563 ports * sizeof(*vn)) / FIP_BPW);
97c8389d 564 kal->fip.fip_flags = htons(FIP_FL_FPMA);
184dd345
VD
565 if (fip->spma)
566 kal->fip.fip_flags |= htons(FIP_FL_SPMA);
97c8389d
JE
567
568 kal->mac.fd_desc.fip_dtype = FIP_DT_MAC;
569 kal->mac.fd_desc.fip_dlen = sizeof(kal->mac) / FIP_BPW;
570 memcpy(kal->mac.fd_mac, fip->ctl_src_addr, ETH_ALEN);
97c8389d
JE
571 if (ports) {
572 vn = (struct fip_vn_desc *)(kal + 1);
573 vn->fd_desc.fip_dtype = FIP_DT_VN_ID;
574 vn->fd_desc.fip_dlen = sizeof(*vn) / FIP_BPW;
11b56188 575 memcpy(vn->fd_mac, fip->get_src_addr(lport), ETH_ALEN);
fb83153d
KM
576 hton24(vn->fd_fc_id, lport->port_id);
577 put_unaligned_be64(lport->wwpn, &vn->fd_wwpn);
97c8389d 578 }
97c8389d 579 skb_put(skb, len);
0f491539 580 skb->protocol = htons(ETH_P_FIP);
6f6c2aa3 581 skb->priority = fip->priority;
97c8389d
JE
582 skb_reset_mac_header(skb);
583 skb_reset_network_header(skb);
584 fip->send(fip, skb);
585}
586
587/**
70b51aab
RL
588 * fcoe_ctlr_encaps() - Encapsulate an ELS frame for FIP, without sending it
589 * @fip: The FCoE controller for the ELS frame
590 * @dtype: The FIP descriptor type for the frame
591 * @skb: The FCoE ELS frame including FC header but no FCoE headers
e10f8c66 592 * @d_id: The destination port ID.
97c8389d
JE
593 *
594 * Returns non-zero error code on failure.
595 *
596 * The caller must check that the length is a multiple of 4.
597 *
598 * The @skb must have enough headroom (28 bytes) and tailroom (8 bytes).
599 * Headroom includes the FIP encapsulation description, FIP header, and
600 * Ethernet header. The tailroom is for the FIP MAC descriptor.
601 */
11b56188 602static int fcoe_ctlr_encaps(struct fcoe_ctlr *fip, struct fc_lport *lport,
e10f8c66 603 u8 dtype, struct sk_buff *skb, u32 d_id)
97c8389d
JE
604{
605 struct fip_encaps_head {
606 struct ethhdr eth;
607 struct fip_header fip;
608 struct fip_encaps encaps;
0095a921 609 } __packed * cap;
5554345b 610 struct fc_frame_header *fh;
97c8389d
JE
611 struct fip_mac_desc *mac;
612 struct fcoe_fcf *fcf;
613 size_t dlen;
5a84baea 614 u16 fip_flags;
5554345b 615 u8 op;
97c8389d 616
5554345b
JE
617 fh = (struct fc_frame_header *)skb->data;
618 op = *(u8 *)(fh + 1);
97c8389d 619 dlen = sizeof(struct fip_encaps) + skb->len; /* len before push */
d58ff351 620 cap = skb_push(skb, sizeof(*cap));
97c8389d 621 memset(cap, 0, sizeof(*cap));
e10f8c66
JE
622
623 if (lport->point_to_multipoint) {
624 if (fcoe_ctlr_vn_lookup(fip, d_id, cap->eth.h_dest))
625 return -ENODEV;
5554345b 626 fip_flags = 0;
e10f8c66
JE
627 } else {
628 fcf = fip->sel_fcf;
629 if (!fcf)
630 return -ENODEV;
631 fip_flags = fcf->flags;
632 fip_flags &= fip->spma ? FIP_FL_SPMA | FIP_FL_FPMA :
633 FIP_FL_FPMA;
634 if (!fip_flags)
635 return -ENODEV;
636 memcpy(cap->eth.h_dest, fcf->fcf_mac, ETH_ALEN);
637 }
97c8389d
JE
638 memcpy(cap->eth.h_source, fip->ctl_src_addr, ETH_ALEN);
639 cap->eth.h_proto = htons(ETH_P_FIP);
640
641 cap->fip.fip_ver = FIP_VER_ENCAPS(FIP_VER);
642 cap->fip.fip_op = htons(FIP_OP_LS);
5554345b
JE
643 if (op == ELS_LS_ACC || op == ELS_LS_RJT)
644 cap->fip.fip_subcode = FIP_SC_REP;
645 else
646 cap->fip.fip_subcode = FIP_SC_REQ;
5a84baea 647 cap->fip.fip_flags = htons(fip_flags);
97c8389d
JE
648
649 cap->encaps.fd_desc.fip_dtype = dtype;
650 cap->encaps.fd_desc.fip_dlen = dlen / FIP_BPW;
651
5554345b
JE
652 if (op != ELS_LS_RJT) {
653 dlen += sizeof(*mac);
b080db58 654 mac = skb_put_zero(skb, sizeof(*mac));
5554345b
JE
655 mac->fd_desc.fip_dtype = FIP_DT_MAC;
656 mac->fd_desc.fip_dlen = sizeof(*mac) / FIP_BPW;
657 if (dtype != FIP_DT_FLOGI && dtype != FIP_DT_FDISC) {
658 memcpy(mac->fd_mac, fip->get_src_addr(lport), ETH_ALEN);
659 } else if (fip->mode == FIP_MODE_VN2VN) {
660 hton24(mac->fd_mac, FIP_VN_FC_MAP);
661 hton24(mac->fd_mac + 3, fip->port_id);
662 } else if (fip_flags & FIP_FL_SPMA) {
663 LIBFCOE_FIP_DBG(fip, "FLOGI/FDISC sent with SPMA\n");
664 memcpy(mac->fd_mac, fip->ctl_src_addr, ETH_ALEN);
665 } else {
666 LIBFCOE_FIP_DBG(fip, "FLOGI/FDISC sent with FPMA\n");
667 /* FPMA only FLOGI. Must leave the MAC desc zeroed. */
668 }
593abc07 669 }
5554345b 670 cap->fip.fip_dl_len = htons(dlen / FIP_BPW);
97c8389d 671
0f491539 672 skb->protocol = htons(ETH_P_FIP);
6f6c2aa3 673 skb->priority = fip->priority;
97c8389d
JE
674 skb_reset_mac_header(skb);
675 skb_reset_network_header(skb);
676 return 0;
677}
678
679/**
680 * fcoe_ctlr_els_send() - Send an ELS frame encapsulated by FIP if appropriate.
681 * @fip: FCoE controller.
11b56188 682 * @lport: libfc fc_lport to send from
97c8389d
JE
683 * @skb: FCoE ELS frame including FC header but no FCoE headers.
684 *
685 * Returns a non-zero error code if the frame should not be sent.
686 * Returns zero if the caller should send the frame with FCoE encapsulation.
687 *
688 * The caller must check that the length is a multiple of 4.
689 * The SKB must have enough headroom (28 bytes) and tailroom (8 bytes).
e10f8c66 690 * The the skb must also be an fc_frame.
794d98e7
JE
691 *
692 * This is called from the lower-level driver with spinlocks held,
693 * so we must not take a mutex here.
97c8389d 694 */
11b56188
CL
695int fcoe_ctlr_els_send(struct fcoe_ctlr *fip, struct fc_lport *lport,
696 struct sk_buff *skb)
97c8389d 697{
e10f8c66 698 struct fc_frame *fp;
97c8389d
JE
699 struct fc_frame_header *fh;
700 u16 old_xid;
701 u8 op;
11b56188 702 u8 mac[ETH_ALEN];
97c8389d 703
e10f8c66 704 fp = container_of(skb, struct fc_frame, skb);
97c8389d
JE
705 fh = (struct fc_frame_header *)skb->data;
706 op = *(u8 *)(fh + 1);
707
e10f8c66 708 if (op == ELS_FLOGI && fip->mode != FIP_MODE_VN2VN) {
97c8389d
JE
709 old_xid = fip->flogi_oxid;
710 fip->flogi_oxid = ntohs(fh->fh_ox_id);
711 if (fip->state == FIP_ST_AUTO) {
712 if (old_xid == FC_XID_UNKNOWN)
713 fip->flogi_count = 0;
714 fip->flogi_count++;
715 if (fip->flogi_count < 3)
716 goto drop;
cd229e42 717 fcoe_ctlr_map_dest(fip);
97c8389d
JE
718 return 0;
719 }
5f48f70e 720 if (fip->state == FIP_ST_NON_FIP)
cd229e42 721 fcoe_ctlr_map_dest(fip);
5f48f70e
JE
722 }
723
724 if (fip->state == FIP_ST_NON_FIP)
725 return 0;
e10f8c66 726 if (!fip->sel_fcf && fip->mode != FIP_MODE_VN2VN)
f31f2a1c 727 goto drop;
5f48f70e
JE
728 switch (op) {
729 case ELS_FLOGI:
97c8389d 730 op = FIP_DT_FLOGI;
794d98e7
JE
731 if (fip->mode == FIP_MODE_VN2VN)
732 break;
733 spin_lock_bh(&fip->ctlr_lock);
734 kfree_skb(fip->flogi_req);
735 fip->flogi_req = skb;
736 fip->flogi_req_send = 1;
737 spin_unlock_bh(&fip->ctlr_lock);
738 schedule_work(&fip->timer_work);
739 return -EINPROGRESS;
97c8389d
JE
740 case ELS_FDISC:
741 if (ntoh24(fh->fh_s_id))
742 return 0;
743 op = FIP_DT_FDISC;
744 break;
745 case ELS_LOGO:
e10f8c66
JE
746 if (fip->mode == FIP_MODE_VN2VN) {
747 if (fip->state != FIP_ST_VNMP_UP)
63d0e3df 748 goto drop;
e10f8c66 749 if (ntoh24(fh->fh_d_id) == FC_FID_FLOGI)
63d0e3df 750 goto drop;
e10f8c66
JE
751 } else {
752 if (fip->state != FIP_ST_ENABLED)
753 return 0;
754 if (ntoh24(fh->fh_d_id) != FC_FID_FLOGI)
755 return 0;
756 }
97c8389d
JE
757 op = FIP_DT_LOGO;
758 break;
759 case ELS_LS_ACC:
97c8389d 760 /*
e10f8c66 761 * If non-FIP, we may have gotten an SID by accepting an FLOGI
97c8389d
JE
762 * from a point-to-point connection. Switch to using
763 * the source mac based on the SID. The destination
25985edc 764 * MAC in this case would have been set by receiving the
97c8389d
JE
765 * FLOGI.
766 */
e10f8c66
JE
767 if (fip->state == FIP_ST_NON_FIP) {
768 if (fip->flogi_oxid == FC_XID_UNKNOWN)
769 return 0;
770 fip->flogi_oxid = FC_XID_UNKNOWN;
771 fc_fcoe_set_mac(mac, fh->fh_d_id);
772 fip->update_mac(lport, mac);
773 }
774 /* fall through */
775 case ELS_LS_RJT:
776 op = fr_encaps(fp);
777 if (op)
778 break;
97c8389d
JE
779 return 0;
780 default:
e10f8c66
JE
781 if (fip->state != FIP_ST_ENABLED &&
782 fip->state != FIP_ST_VNMP_UP)
97c8389d
JE
783 goto drop;
784 return 0;
785 }
e10f8c66
JE
786 LIBFCOE_FIP_DBG(fip, "els_send op %u d_id %x\n",
787 op, ntoh24(fh->fh_d_id));
788 if (fcoe_ctlr_encaps(fip, lport, op, skb, ntoh24(fh->fh_d_id)))
97c8389d
JE
789 goto drop;
790 fip->send(fip, skb);
791 return -EINPROGRESS;
792drop:
c9596550
HR
793 LIBFCOE_FIP_DBG(fip, "drop els_send op %u d_id %x\n",
794 op, ntoh24(fh->fh_d_id));
2d7d4fd3 795 kfree_skb(skb);
97c8389d
JE
796 return -EINVAL;
797}
798EXPORT_SYMBOL(fcoe_ctlr_els_send);
799
70b51aab
RL
800/**
801 * fcoe_ctlr_age_fcfs() - Reset and free all old FCFs for a controller
802 * @fip: The FCoE controller to free FCFs on
97c8389d 803 *
f018b73a 804 * Called with lock held and preemption disabled.
97c8389d 805 *
8690cb83
JE
806 * An FCF is considered old if we have missed two advertisements.
807 * That is, there have been no valid advertisement from it for 2.5
808 * times its keep-alive period.
97c8389d
JE
809 *
810 * In addition, determine the time when an FCF selection can occur.
f3da80e7
YZ
811 *
812 * Also, increment the MissDiscAdvCount when no advertisement is received
813 * for the corresponding FCF for 1.5 * FKA_ADV_PERIOD (FC-BB-5 LESB).
8690cb83
JE
814 *
815 * Returns the time in jiffies for the next call.
97c8389d 816 */
8690cb83 817static unsigned long fcoe_ctlr_age_fcfs(struct fcoe_ctlr *fip)
97c8389d
JE
818{
819 struct fcoe_fcf *fcf;
820 struct fcoe_fcf *next;
8690cb83
JE
821 unsigned long next_timer = jiffies + msecs_to_jiffies(FIP_VN_KA_PERIOD);
822 unsigned long deadline;
97c8389d 823 unsigned long sel_time = 0;
8d55e507 824 struct list_head del_list;
1bd49b48 825 struct fc_stats *stats;
97c8389d 826
8d55e507
RL
827 INIT_LIST_HEAD(&del_list);
828
1bd49b48 829 stats = per_cpu_ptr(fip->lp->stats, get_cpu());
fdb068c6 830
97c8389d 831 list_for_each_entry_safe(fcf, next, &fip->fcfs, list) {
8690cb83
JE
832 deadline = fcf->time + fcf->fka_period + fcf->fka_period / 2;
833 if (fip->sel_fcf == fcf) {
834 if (time_after(jiffies, deadline)) {
8690cb83
JE
835 stats->MissDiscAdvCount++;
836 printk(KERN_INFO "libfcoe: host%d: "
837 "Missing Discovery Advertisement "
838 "for fab %16.16llx count %lld\n",
839 fip->lp->host->host_no, fcf->fabric_name,
840 stats->MissDiscAdvCount);
841 } else if (time_after(next_timer, deadline))
842 next_timer = deadline;
f3da80e7 843 }
8690cb83
JE
844
845 deadline += fcf->fka_period;
d99ee45b 846 if (time_after_eq(jiffies, deadline)) {
97c8389d
JE
847 if (fip->sel_fcf == fcf)
848 fip->sel_fcf = NULL;
8d55e507
RL
849 /*
850 * Move to delete list so we can call
851 * fcoe_sysfs_fcf_del (which can sleep)
852 * after the put_cpu().
853 */
97c8389d 854 list_del(&fcf->list);
8d55e507 855 list_add(&fcf->list, &del_list);
f018b73a 856 stats->VLinkFailureCount++;
8690cb83
JE
857 } else {
858 if (time_after(next_timer, deadline))
859 next_timer = deadline;
860 if (fcoe_ctlr_mtu_valid(fcf) &&
861 (!sel_time || time_before(sel_time, fcf->time)))
862 sel_time = fcf->time;
97c8389d
JE
863 }
864 }
fdb068c6 865 put_cpu();
8d55e507
RL
866
867 list_for_each_entry_safe(fcf, next, &del_list, list) {
868 /* Removes fcf from current list */
869 fcoe_sysfs_fcf_del(fcf);
870 }
871
d99ee45b 872 if (sel_time && !fip->sel_fcf && !fip->sel_time) {
97c8389d
JE
873 sel_time += msecs_to_jiffies(FCOE_CTLR_START_DELAY);
874 fip->sel_time = sel_time;
97c8389d 875 }
d99ee45b 876
8690cb83 877 return next_timer;
97c8389d
JE
878}
879
880/**
70b51aab 881 * fcoe_ctlr_parse_adv() - Decode a FIP advertisement into a new FCF entry
0f51c2e5 882 * @fip: The FCoE controller receiving the advertisement
70b51aab
RL
883 * @skb: The received FIP advertisement frame
884 * @fcf: The resulting FCF entry
97c8389d
JE
885 *
886 * Returns zero on a valid parsed advertisement,
887 * otherwise returns non zero value.
888 */
0f51c2e5
JE
889static int fcoe_ctlr_parse_adv(struct fcoe_ctlr *fip,
890 struct sk_buff *skb, struct fcoe_fcf *fcf)
97c8389d
JE
891{
892 struct fip_header *fiph;
893 struct fip_desc *desc = NULL;
894 struct fip_wwn_desc *wwn;
895 struct fip_fab_desc *fab;
896 struct fip_fka_desc *fka;
897 unsigned long t;
898 size_t rlen;
899 size_t dlen;
0a9c5d34 900 u32 desc_mask;
97c8389d
JE
901
902 memset(fcf, 0, sizeof(*fcf));
903 fcf->fka_period = msecs_to_jiffies(FCOE_CTLR_DEF_FKA);
904
905 fiph = (struct fip_header *)skb->data;
906 fcf->flags = ntohs(fiph->fip_flags);
907
0a9c5d34
BPG
908 /*
909 * mask of required descriptors. validating each one clears its bit.
910 */
911 desc_mask = BIT(FIP_DT_PRI) | BIT(FIP_DT_MAC) | BIT(FIP_DT_NAME) |
912 BIT(FIP_DT_FAB) | BIT(FIP_DT_FKA);
913
97c8389d
JE
914 rlen = ntohs(fiph->fip_dl_len) * 4;
915 if (rlen + sizeof(*fiph) > skb->len)
916 return -EINVAL;
917
918 desc = (struct fip_desc *)(fiph + 1);
919 while (rlen > 0) {
920 dlen = desc->fip_dlen * FIP_BPW;
921 if (dlen < sizeof(*desc) || dlen > rlen)
922 return -EINVAL;
0a9c5d34
BPG
923 /* Drop Adv if there are duplicate critical descriptors */
924 if ((desc->fip_dtype < 32) &&
925 !(desc_mask & 1U << desc->fip_dtype)) {
926 LIBFCOE_FIP_DBG(fip, "Duplicate Critical "
927 "Descriptors in FIP adv\n");
928 return -EINVAL;
929 }
97c8389d
JE
930 switch (desc->fip_dtype) {
931 case FIP_DT_PRI:
932 if (dlen != sizeof(struct fip_pri_desc))
933 goto len_err;
934 fcf->pri = ((struct fip_pri_desc *)desc)->fd_pri;
0a9c5d34 935 desc_mask &= ~BIT(FIP_DT_PRI);
97c8389d
JE
936 break;
937 case FIP_DT_MAC:
938 if (dlen != sizeof(struct fip_mac_desc))
939 goto len_err;
940 memcpy(fcf->fcf_mac,
941 ((struct fip_mac_desc *)desc)->fd_mac,
942 ETH_ALEN);
81c11dd2 943 memcpy(fcf->fcoe_mac, fcf->fcf_mac, ETH_ALEN);
97c8389d 944 if (!is_valid_ether_addr(fcf->fcf_mac)) {
e10f8c66
JE
945 LIBFCOE_FIP_DBG(fip,
946 "Invalid MAC addr %pM in FIP adv\n",
947 fcf->fcf_mac);
97c8389d
JE
948 return -EINVAL;
949 }
0a9c5d34 950 desc_mask &= ~BIT(FIP_DT_MAC);
97c8389d
JE
951 break;
952 case FIP_DT_NAME:
953 if (dlen != sizeof(struct fip_wwn_desc))
954 goto len_err;
955 wwn = (struct fip_wwn_desc *)desc;
956 fcf->switch_name = get_unaligned_be64(&wwn->fd_wwn);
0a9c5d34 957 desc_mask &= ~BIT(FIP_DT_NAME);
97c8389d
JE
958 break;
959 case FIP_DT_FAB:
960 if (dlen != sizeof(struct fip_fab_desc))
961 goto len_err;
962 fab = (struct fip_fab_desc *)desc;
963 fcf->fabric_name = get_unaligned_be64(&fab->fd_wwn);
964 fcf->vfid = ntohs(fab->fd_vfid);
965 fcf->fc_map = ntoh24(fab->fd_map);
0a9c5d34 966 desc_mask &= ~BIT(FIP_DT_FAB);
97c8389d
JE
967 break;
968 case FIP_DT_FKA:
969 if (dlen != sizeof(struct fip_fka_desc))
970 goto len_err;
971 fka = (struct fip_fka_desc *)desc;
8cdffdcc
YZ
972 if (fka->fd_flags & FIP_FKA_ADV_D)
973 fcf->fd_flags = 1;
97c8389d
JE
974 t = ntohl(fka->fd_fka_period);
975 if (t >= FCOE_CTLR_MIN_FKA)
976 fcf->fka_period = msecs_to_jiffies(t);
0a9c5d34 977 desc_mask &= ~BIT(FIP_DT_FKA);
97c8389d
JE
978 break;
979 case FIP_DT_MAP_OUI:
980 case FIP_DT_FCOE_SIZE:
981 case FIP_DT_FLOGI:
982 case FIP_DT_FDISC:
983 case FIP_DT_LOGO:
984 case FIP_DT_ELP:
985 default:
0f51c2e5 986 LIBFCOE_FIP_DBG(fip, "unexpected descriptor type %x "
650bd12b 987 "in FIP adv\n", desc->fip_dtype);
97c8389d 988 /* standard says ignore unknown descriptors >= 128 */
6a551c11 989 if (desc->fip_dtype < FIP_DT_NON_CRITICAL)
97c8389d 990 return -EINVAL;
1508f3ec 991 break;
97c8389d
JE
992 }
993 desc = (struct fip_desc *)((char *)desc + dlen);
994 rlen -= dlen;
995 }
996 if (!fcf->fc_map || (fcf->fc_map & 0x10000))
997 return -EINVAL;
240778e8 998 if (!fcf->switch_name)
97c8389d 999 return -EINVAL;
0a9c5d34
BPG
1000 if (desc_mask) {
1001 LIBFCOE_FIP_DBG(fip, "adv missing descriptors mask %x\n",
1002 desc_mask);
1003 return -EINVAL;
1004 }
97c8389d
JE
1005 return 0;
1006
1007len_err:
0f51c2e5 1008 LIBFCOE_FIP_DBG(fip, "FIP length error in descriptor type %x len %zu\n",
650bd12b 1009 desc->fip_dtype, dlen);
97c8389d
JE
1010 return -EINVAL;
1011}
1012
1013/**
70b51aab
RL
1014 * fcoe_ctlr_recv_adv() - Handle an incoming advertisement
1015 * @fip: The FCoE controller receiving the advertisement
1016 * @skb: The received FIP packet
97c8389d
JE
1017 */
1018static void fcoe_ctlr_recv_adv(struct fcoe_ctlr *fip, struct sk_buff *skb)
1019{
1020 struct fcoe_fcf *fcf;
1021 struct fcoe_fcf new;
97c8389d
JE
1022 unsigned long sol_tov = msecs_to_jiffies(FCOE_CTRL_SOL_TOV);
1023 int first = 0;
1024 int mtu_valid;
8d55e507
RL
1025 int found = 0;
1026 int rc = 0;
97c8389d 1027
0f51c2e5 1028 if (fcoe_ctlr_parse_adv(fip, skb, &new))
97c8389d
JE
1029 return;
1030
fdb068c6 1031 mutex_lock(&fip->ctlr_mutex);
97c8389d 1032 first = list_empty(&fip->fcfs);
97c8389d
JE
1033 list_for_each_entry(fcf, &fip->fcfs, list) {
1034 if (fcf->switch_name == new.switch_name &&
1035 fcf->fabric_name == new.fabric_name &&
1036 fcf->fc_map == new.fc_map &&
6942df7f 1037 ether_addr_equal(fcf->fcf_mac, new.fcf_mac)) {
8d55e507 1038 found = 1;
97c8389d
JE
1039 break;
1040 }
1041 }
1042 if (!found) {
1043 if (fip->fcf_count >= FCOE_CTLR_FCF_LIMIT)
1044 goto out;
1045
1046 fcf = kmalloc(sizeof(*fcf), GFP_ATOMIC);
1047 if (!fcf)
1048 goto out;
1049
97c8389d 1050 memcpy(fcf, &new, sizeof(new));
8d55e507
RL
1051 fcf->fip = fip;
1052 rc = fcoe_sysfs_fcf_add(fcf);
1053 if (rc) {
1054 printk(KERN_ERR "Failed to allocate sysfs instance "
1055 "for FCF, fab %16.16llx mac %pM\n",
1056 new.fabric_name, new.fcf_mac);
1057 kfree(fcf);
1058 goto out;
1059 }
97c8389d
JE
1060 } else {
1061 /*
c600fea2
JE
1062 * Update the FCF's keep-alive descriptor flags.
1063 * Other flag changes from new advertisements are
1064 * ignored after a solicited advertisement is
1065 * received and the FCF is selectable (usable).
97c8389d 1066 */
c600fea2
JE
1067 fcf->fd_flags = new.fd_flags;
1068 if (!fcoe_ctlr_fcf_usable(fcf))
1069 fcf->flags = new.flags;
1070
d99ee45b 1071 if (fcf == fip->sel_fcf && !fcf->fd_flags) {
97c8389d
JE
1072 fip->ctlr_ka_time -= fcf->fka_period;
1073 fip->ctlr_ka_time += new.fka_period;
1074 if (time_before(fip->ctlr_ka_time, fip->timer.expires))
1075 mod_timer(&fip->timer, fip->ctlr_ka_time);
c600fea2 1076 }
97c8389d
JE
1077 fcf->fka_period = new.fka_period;
1078 memcpy(fcf->fcf_mac, new.fcf_mac, ETH_ALEN);
1079 }
8d55e507 1080
97c8389d
JE
1081 mtu_valid = fcoe_ctlr_mtu_valid(fcf);
1082 fcf->time = jiffies;
9069f5c4
JE
1083 if (!found)
1084 LIBFCOE_FIP_DBG(fip, "New FCF fab %16.16llx mac %pM\n",
1085 fcf->fabric_name, fcf->fcf_mac);
97c8389d
JE
1086
1087 /*
1088 * If this advertisement is not solicited and our max receive size
1089 * hasn't been verified, send a solicited advertisement.
1090 */
1091 if (!mtu_valid)
1092 fcoe_ctlr_solicit(fip, fcf);
1093
1094 /*
1095 * If its been a while since we did a solicit, and this is
1096 * the first advertisement we've received, do a multicast
1097 * solicitation to gather as many advertisements as we can
1098 * before selection occurs.
1099 */
1100 if (first && time_after(jiffies, fip->sol_time + sol_tov))
1101 fcoe_ctlr_solicit(fip, NULL);
1102
981c1154
JE
1103 /*
1104 * Put this FCF at the head of the list for priority among equals.
1105 * This helps in the case of an NPV switch which insists we use
1106 * the FCF that answers multicast solicitations, not the others that
1107 * are sending periodic multicast advertisements.
1108 */
63ce2499
KS
1109 if (mtu_valid)
1110 list_move(&fcf->list, &fip->fcfs);
981c1154 1111
97c8389d
JE
1112 /*
1113 * If this is the first validated FCF, note the time and
1114 * set a timer to trigger selection.
1115 */
eac00c8a
UK
1116 if (mtu_valid && !fip->sel_fcf && !fip->sel_time &&
1117 fcoe_ctlr_fcf_usable(fcf)) {
97c8389d 1118 fip->sel_time = jiffies +
70b51aab 1119 msecs_to_jiffies(FCOE_CTLR_START_DELAY);
97c8389d
JE
1120 if (!timer_pending(&fip->timer) ||
1121 time_before(fip->sel_time, fip->timer.expires))
1122 mod_timer(&fip->timer, fip->sel_time);
1123 }
8d55e507 1124
97c8389d 1125out:
fdb068c6 1126 mutex_unlock(&fip->ctlr_mutex);
97c8389d
JE
1127}
1128
1129/**
70b51aab
RL
1130 * fcoe_ctlr_recv_els() - Handle an incoming FIP encapsulated ELS frame
1131 * @fip: The FCoE controller which received the packet
1132 * @skb: The received FIP packet
97c8389d
JE
1133 */
1134static void fcoe_ctlr_recv_els(struct fcoe_ctlr *fip, struct sk_buff *skb)
1135{
70b51aab 1136 struct fc_lport *lport = fip->lp;
97c8389d 1137 struct fip_header *fiph;
11b56188 1138 struct fc_frame *fp = (struct fc_frame *)skb;
97c8389d
JE
1139 struct fc_frame_header *fh = NULL;
1140 struct fip_desc *desc;
1141 struct fip_encaps *els;
81c11dd2 1142 struct fcoe_fcf *sel;
1bd49b48 1143 struct fc_stats *stats;
97c8389d
JE
1144 enum fip_desc_type els_dtype = 0;
1145 u8 els_op;
1146 u8 sub;
1147 u8 granted_mac[ETH_ALEN] = { 0 };
1148 size_t els_len = 0;
1149 size_t rlen;
1150 size_t dlen;
be61331d
BPG
1151 u32 desc_mask = 0;
1152 u32 desc_cnt = 0;
97c8389d
JE
1153
1154 fiph = (struct fip_header *)skb->data;
1155 sub = fiph->fip_subcode;
1156 if (sub != FIP_SC_REQ && sub != FIP_SC_REP)
1157 goto drop;
1158
1159 rlen = ntohs(fiph->fip_dl_len) * 4;
1160 if (rlen + sizeof(*fiph) > skb->len)
1161 goto drop;
1162
1163 desc = (struct fip_desc *)(fiph + 1);
1164 while (rlen > 0) {
be61331d 1165 desc_cnt++;
97c8389d
JE
1166 dlen = desc->fip_dlen * FIP_BPW;
1167 if (dlen < sizeof(*desc) || dlen > rlen)
1168 goto drop;
0a9c5d34
BPG
1169 /* Drop ELS if there are duplicate critical descriptors */
1170 if (desc->fip_dtype < 32) {
81c11dd2
BPG
1171 if ((desc->fip_dtype != FIP_DT_MAC) &&
1172 (desc_mask & 1U << desc->fip_dtype)) {
0a9c5d34
BPG
1173 LIBFCOE_FIP_DBG(fip, "Duplicate Critical "
1174 "Descriptors in FIP ELS\n");
1175 goto drop;
1176 }
be61331d 1177 desc_mask |= (1 << desc->fip_dtype);
0a9c5d34 1178 }
97c8389d
JE
1179 switch (desc->fip_dtype) {
1180 case FIP_DT_MAC:
81c11dd2 1181 sel = fip->sel_fcf;
be61331d
BPG
1182 if (desc_cnt == 1) {
1183 LIBFCOE_FIP_DBG(fip, "FIP descriptors "
1184 "received out of order\n");
1185 goto drop;
1186 }
81c11dd2
BPG
1187 /*
1188 * Some switch implementations send two MAC descriptors,
1189 * with first MAC(granted_mac) being the FPMA, and the
1190 * second one(fcoe_mac) is used as destination address
1191 * for sending/receiving FCoE packets. FIP traffic is
1192 * sent using fip_mac. For regular switches, both
1193 * fip_mac and fcoe_mac would be the same.
1194 */
1195 if (desc_cnt == 2)
1196 memcpy(granted_mac,
1197 ((struct fip_mac_desc *)desc)->fd_mac,
1198 ETH_ALEN);
be61331d 1199
97c8389d
JE
1200 if (dlen != sizeof(struct fip_mac_desc))
1201 goto len_err;
81c11dd2
BPG
1202
1203 if ((desc_cnt == 3) && (sel))
1204 memcpy(sel->fcoe_mac,
1205 ((struct fip_mac_desc *)desc)->fd_mac,
1206 ETH_ALEN);
97c8389d
JE
1207 break;
1208 case FIP_DT_FLOGI:
1209 case FIP_DT_FDISC:
1210 case FIP_DT_LOGO:
1211 case FIP_DT_ELP:
be61331d
BPG
1212 if (desc_cnt != 1) {
1213 LIBFCOE_FIP_DBG(fip, "FIP descriptors "
1214 "received out of order\n");
1215 goto drop;
1216 }
97c8389d
JE
1217 if (fh)
1218 goto drop;
1219 if (dlen < sizeof(*els) + sizeof(*fh) + 1)
1220 goto len_err;
1221 els_len = dlen - sizeof(*els);
1222 els = (struct fip_encaps *)desc;
1223 fh = (struct fc_frame_header *)(els + 1);
1224 els_dtype = desc->fip_dtype;
1225 break;
1226 default:
0f51c2e5 1227 LIBFCOE_FIP_DBG(fip, "unexpected descriptor type %x "
650bd12b 1228 "in FIP adv\n", desc->fip_dtype);
97c8389d 1229 /* standard says ignore unknown descriptors >= 128 */
6a551c11 1230 if (desc->fip_dtype < FIP_DT_NON_CRITICAL)
97c8389d 1231 goto drop;
be61331d
BPG
1232 if (desc_cnt <= 2) {
1233 LIBFCOE_FIP_DBG(fip, "FIP descriptors "
1234 "received out of order\n");
1235 goto drop;
1236 }
1508f3ec 1237 break;
97c8389d
JE
1238 }
1239 desc = (struct fip_desc *)((char *)desc + dlen);
1240 rlen -= dlen;
1241 }
1242
1243 if (!fh)
1244 goto drop;
1245 els_op = *(u8 *)(fh + 1);
1246
e10f8c66 1247 if ((els_dtype == FIP_DT_FLOGI || els_dtype == FIP_DT_FDISC) &&
794d98e7
JE
1248 sub == FIP_SC_REP && fip->mode != FIP_MODE_VN2VN) {
1249 if (els_op == ELS_LS_ACC) {
1250 if (!is_valid_ether_addr(granted_mac)) {
1251 LIBFCOE_FIP_DBG(fip,
1252 "Invalid MAC address %pM in FIP ELS\n",
1253 granted_mac);
1254 goto drop;
1255 }
1256 memcpy(fr_cb(fp)->granted_mac, granted_mac, ETH_ALEN);
e10f8c66 1257
794d98e7
JE
1258 if (fip->flogi_oxid == ntohs(fh->fh_ox_id)) {
1259 fip->flogi_oxid = FC_XID_UNKNOWN;
1260 if (els_dtype == FIP_DT_FLOGI)
1261 fcoe_ctlr_announce(fip);
1262 }
1263 } else if (els_dtype == FIP_DT_FLOGI &&
1264 !fcoe_ctlr_flogi_retry(fip))
1265 goto drop; /* retrying FLOGI so drop reject */
e10f8c66 1266 }
97c8389d 1267
be61331d
BPG
1268 if ((desc_cnt == 0) || ((els_op != ELS_LS_RJT) &&
1269 (!(1U << FIP_DT_MAC & desc_mask)))) {
1270 LIBFCOE_FIP_DBG(fip, "Missing critical descriptors "
1271 "in FIP ELS\n");
1272 goto drop;
1273 }
1274
97c8389d
JE
1275 /*
1276 * Convert skb into an fc_frame containing only the ELS.
1277 */
1278 skb_pull(skb, (u8 *)fh - skb->data);
1279 skb_trim(skb, els_len);
1280 fp = (struct fc_frame *)skb;
1281 fc_frame_init(fp);
1282 fr_sof(fp) = FC_SOF_I3;
1283 fr_eof(fp) = FC_EOF_T;
70b51aab 1284 fr_dev(fp) = lport;
e10f8c66 1285 fr_encaps(fp) = els_dtype;
97c8389d 1286
1bd49b48 1287 stats = per_cpu_ptr(lport->stats, get_cpu());
97c8389d
JE
1288 stats->RxFrames++;
1289 stats->RxWords += skb->len / FIP_BPW;
f018b73a 1290 put_cpu();
97c8389d 1291
70b51aab 1292 fc_exch_recv(lport, fp);
97c8389d
JE
1293 return;
1294
1295len_err:
0f51c2e5 1296 LIBFCOE_FIP_DBG(fip, "FIP length error in descriptor type %x len %zu\n",
650bd12b 1297 desc->fip_dtype, dlen);
97c8389d
JE
1298drop:
1299 kfree_skb(skb);
1300}
1301
1302/**
70b51aab
RL
1303 * fcoe_ctlr_recv_els() - Handle an incoming link reset frame
1304 * @fip: The FCoE controller that received the frame
1305 * @fh: The received FIP header
97c8389d
JE
1306 *
1307 * There may be multiple VN_Port descriptors.
1308 * The overall length has already been checked.
1309 */
1310static void fcoe_ctlr_recv_clr_vlink(struct fcoe_ctlr *fip,
fd37f66e 1311 struct sk_buff *skb)
97c8389d
JE
1312{
1313 struct fip_desc *desc;
1314 struct fip_mac_desc *mp;
1315 struct fip_wwn_desc *wp;
1316 struct fip_vn_desc *vp;
1317 size_t rlen;
1318 size_t dlen;
1319 struct fcoe_fcf *fcf = fip->sel_fcf;
70b51aab 1320 struct fc_lport *lport = fip->lp;
5550fda7
BPG
1321 struct fc_lport *vn_port = NULL;
1322 u32 desc_mask;
c051ad2e
BPG
1323 int num_vlink_desc;
1324 int reset_phys_port = 0;
1325 struct fip_vn_desc **vlink_desc_arr = NULL;
fd37f66e
CD
1326 struct fip_header *fh = (struct fip_header *)skb->data;
1327 struct ethhdr *eh = eth_hdr(skb);
97c8389d 1328
0f51c2e5 1329 LIBFCOE_FIP_DBG(fip, "Clear Virtual Link received\n");
d29510a2 1330
fd37f66e 1331 if (!fcf) {
b2593cbe
BPG
1332 /*
1333 * We are yet to select best FCF, but we got CVL in the
1334 * meantime. reset the ctlr and let it rediscover the FCF
1335 */
fd37f66e
CD
1336 LIBFCOE_FIP_DBG(fip, "Resetting fcoe_ctlr as FCF has not been "
1337 "selected yet\n");
b2593cbe
BPG
1338 mutex_lock(&fip->ctlr_mutex);
1339 fcoe_ctlr_reset(fip);
1340 mutex_unlock(&fip->ctlr_mutex);
97c8389d 1341 return;
b2593cbe 1342 }
97c8389d 1343
fd37f66e
CD
1344 /*
1345 * If we've selected an FCF check that the CVL is from there to avoid
1346 * processing CVLs from an unexpected source. If it is from an
1347 * unexpected source drop it on the floor.
1348 */
1349 if (!ether_addr_equal(eh->h_source, fcf->fcf_mac)) {
1350 LIBFCOE_FIP_DBG(fip, "Dropping CVL due to source address "
1351 "mismatch with FCF src=%pM\n", eh->h_source);
1352 return;
1353 }
1354
1355 /*
1356 * If we haven't logged into the fabric but receive a CVL we should
1357 * reset everything and go back to solicitation.
1358 */
1359 if (!lport->port_id) {
1360 LIBFCOE_FIP_DBG(fip, "lport not logged in, resoliciting\n");
1361 mutex_lock(&fip->ctlr_mutex);
1362 fcoe_ctlr_reset(fip);
1363 mutex_unlock(&fip->ctlr_mutex);
1364 fc_lport_reset(fip->lp);
1365 fcoe_ctlr_solicit(fip, NULL);
1366 return;
1367 }
1368
97c8389d
JE
1369 /*
1370 * mask of required descriptors. Validating each one clears its bit.
1371 */
c051ad2e 1372 desc_mask = BIT(FIP_DT_MAC) | BIT(FIP_DT_NAME);
97c8389d
JE
1373
1374 rlen = ntohs(fh->fip_dl_len) * FIP_BPW;
1375 desc = (struct fip_desc *)(fh + 1);
c051ad2e
BPG
1376
1377 /*
1378 * Actually need to subtract 'sizeof(*mp) - sizeof(*wp)' from 'rlen'
1379 * before determining max Vx_Port descriptor but a buggy FCF could have
0cf2a848 1380 * omitted either or both MAC Address and Name Identifier descriptors
c051ad2e
BPG
1381 */
1382 num_vlink_desc = rlen / sizeof(*vp);
1383 if (num_vlink_desc)
6da2ec56
KC
1384 vlink_desc_arr = kmalloc_array(num_vlink_desc, sizeof(vp),
1385 GFP_ATOMIC);
c051ad2e
BPG
1386 if (!vlink_desc_arr)
1387 return;
1388 num_vlink_desc = 0;
1389
97c8389d
JE
1390 while (rlen >= sizeof(*desc)) {
1391 dlen = desc->fip_dlen * FIP_BPW;
1392 if (dlen > rlen)
c051ad2e 1393 goto err;
0a9c5d34
BPG
1394 /* Drop CVL if there are duplicate critical descriptors */
1395 if ((desc->fip_dtype < 32) &&
c051ad2e 1396 (desc->fip_dtype != FIP_DT_VN_ID) &&
0a9c5d34
BPG
1397 !(desc_mask & 1U << desc->fip_dtype)) {
1398 LIBFCOE_FIP_DBG(fip, "Duplicate Critical "
1399 "Descriptors in FIP CVL\n");
c051ad2e 1400 goto err;
0a9c5d34 1401 }
97c8389d
JE
1402 switch (desc->fip_dtype) {
1403 case FIP_DT_MAC:
1404 mp = (struct fip_mac_desc *)desc;
1405 if (dlen < sizeof(*mp))
c051ad2e 1406 goto err;
6942df7f 1407 if (!ether_addr_equal(mp->fd_mac, fcf->fcf_mac))
c051ad2e 1408 goto err;
97c8389d
JE
1409 desc_mask &= ~BIT(FIP_DT_MAC);
1410 break;
1411 case FIP_DT_NAME:
1412 wp = (struct fip_wwn_desc *)desc;
1413 if (dlen < sizeof(*wp))
c051ad2e 1414 goto err;
97c8389d 1415 if (get_unaligned_be64(&wp->fd_wwn) != fcf->switch_name)
c051ad2e 1416 goto err;
97c8389d
JE
1417 desc_mask &= ~BIT(FIP_DT_NAME);
1418 break;
1419 case FIP_DT_VN_ID:
1420 vp = (struct fip_vn_desc *)desc;
1421 if (dlen < sizeof(*vp))
c051ad2e
BPG
1422 goto err;
1423 vlink_desc_arr[num_vlink_desc++] = vp;
1424 vn_port = fc_vport_id_lookup(lport,
1425 ntoh24(vp->fd_fc_id));
1426 if (vn_port && (vn_port == lport)) {
1427 mutex_lock(&fip->ctlr_mutex);
1bd49b48 1428 per_cpu_ptr(lport->stats,
c051ad2e
BPG
1429 get_cpu())->VLinkFailureCount++;
1430 put_cpu();
1431 fcoe_ctlr_reset(fip);
1432 mutex_unlock(&fip->ctlr_mutex);
5550fda7 1433 }
97c8389d
JE
1434 break;
1435 default:
1436 /* standard says ignore unknown descriptors >= 128 */
6a551c11 1437 if (desc->fip_dtype < FIP_DT_NON_CRITICAL)
c051ad2e 1438 goto err;
97c8389d
JE
1439 break;
1440 }
1441 desc = (struct fip_desc *)((char *)desc + dlen);
1442 rlen -= dlen;
1443 }
1444
1445 /*
1446 * reset only if all required descriptors were present and valid.
1447 */
c051ad2e 1448 if (desc_mask)
0f51c2e5
JE
1449 LIBFCOE_FIP_DBG(fip, "missing descriptors mask %x\n",
1450 desc_mask);
c051ad2e
BPG
1451 else if (!num_vlink_desc) {
1452 LIBFCOE_FIP_DBG(fip, "CVL: no Vx_Port descriptor found\n");
1453 /*
1454 * No Vx_Port description. Clear all NPIV ports,
1455 * followed by physical port
1456 */
c051ad2e 1457 mutex_lock(&fip->ctlr_mutex);
1bd49b48 1458 per_cpu_ptr(lport->stats, get_cpu())->VLinkFailureCount++;
c051ad2e
BPG
1459 put_cpu();
1460 fcoe_ctlr_reset(fip);
1461 mutex_unlock(&fip->ctlr_mutex);
1462
14619ea6
BPG
1463 mutex_lock(&lport->lp_mutex);
1464 list_for_each_entry(vn_port, &lport->vports, list)
1465 fc_lport_reset(vn_port);
1466 mutex_unlock(&lport->lp_mutex);
1467
c051ad2e
BPG
1468 fc_lport_reset(fip->lp);
1469 fcoe_ctlr_solicit(fip, NULL);
97c8389d 1470 } else {
c051ad2e 1471 int i;
dd42dac4 1472
c051ad2e
BPG
1473 LIBFCOE_FIP_DBG(fip, "performing Clear Virtual Link\n");
1474 for (i = 0; i < num_vlink_desc; i++) {
1475 vp = vlink_desc_arr[i];
1476 vn_port = fc_vport_id_lookup(lport,
1477 ntoh24(vp->fd_fc_id));
1478 if (!vn_port)
1479 continue;
1480
1481 /*
1482 * 'port_id' is already validated, check MAC address and
1483 * wwpn
1484 */
6942df7f
JP
1485 if (!ether_addr_equal(fip->get_src_addr(vn_port),
1486 vp->fd_mac) ||
c051ad2e
BPG
1487 get_unaligned_be64(&vp->fd_wwpn) !=
1488 vn_port->wwpn)
1489 continue;
1490
1491 if (vn_port == lport)
1492 /*
1493 * Physical port, defer processing till all
1494 * listed NPIV ports are cleared
1495 */
1496 reset_phys_port = 1;
1497 else /* NPIV port */
1498 fc_lport_reset(vn_port);
1499 }
5550fda7 1500
c051ad2e 1501 if (reset_phys_port) {
5550fda7
BPG
1502 fc_lport_reset(fip->lp);
1503 fcoe_ctlr_solicit(fip, NULL);
1504 }
97c8389d 1505 }
c051ad2e
BPG
1506
1507err:
1508 kfree(vlink_desc_arr);
97c8389d
JE
1509}
1510
1511/**
70b51aab
RL
1512 * fcoe_ctlr_recv() - Receive a FIP packet
1513 * @fip: The FCoE controller that received the packet
1514 * @skb: The received FIP packet
97c8389d 1515 *
1f4aed81 1516 * This may be called from either NET_RX_SOFTIRQ or IRQ.
97c8389d
JE
1517 */
1518void fcoe_ctlr_recv(struct fcoe_ctlr *fip, struct sk_buff *skb)
1519{
c0866286
NH
1520 skb = skb_share_check(skb, GFP_ATOMIC);
1521 if (!skb)
1522 return;
1f4aed81 1523 skb_queue_tail(&fip->fip_recv_list, skb);
97c8389d
JE
1524 schedule_work(&fip->recv_work);
1525}
1526EXPORT_SYMBOL(fcoe_ctlr_recv);
1527
1528/**
70b51aab
RL
1529 * fcoe_ctlr_recv_handler() - Receive a FIP frame
1530 * @fip: The FCoE controller that received the frame
1531 * @skb: The received FIP frame
97c8389d
JE
1532 *
1533 * Returns non-zero if the frame is dropped.
1534 */
1535static int fcoe_ctlr_recv_handler(struct fcoe_ctlr *fip, struct sk_buff *skb)
1536{
1537 struct fip_header *fiph;
1538 struct ethhdr *eh;
1539 enum fip_state state;
9a6cf881 1540 bool fip_vlan_resp = false;
97c8389d
JE
1541 u16 op;
1542 u8 sub;
1543
1544 if (skb_linearize(skb))
1545 goto drop;
1546 if (skb->len < sizeof(*fiph))
1547 goto drop;
1548 eh = eth_hdr(skb);
e10f8c66 1549 if (fip->mode == FIP_MODE_VN2VN) {
6942df7f
JP
1550 if (!ether_addr_equal(eh->h_dest, fip->ctl_src_addr) &&
1551 !ether_addr_equal(eh->h_dest, fcoe_all_vn2vn) &&
1552 !ether_addr_equal(eh->h_dest, fcoe_all_p2p))
e10f8c66 1553 goto drop;
6942df7f
JP
1554 } else if (!ether_addr_equal(eh->h_dest, fip->ctl_src_addr) &&
1555 !ether_addr_equal(eh->h_dest, fcoe_all_enode))
97c8389d
JE
1556 goto drop;
1557 fiph = (struct fip_header *)skb->data;
1558 op = ntohs(fiph->fip_op);
1559 sub = fiph->fip_subcode;
1560
97c8389d
JE
1561 if (FIP_VER_DECAPS(fiph->fip_ver) != FIP_VER)
1562 goto drop;
1563 if (ntohs(fiph->fip_dl_len) * FIP_BPW + sizeof(*fiph) > skb->len)
1564 goto drop;
1565
fdb068c6 1566 mutex_lock(&fip->ctlr_mutex);
97c8389d
JE
1567 state = fip->state;
1568 if (state == FIP_ST_AUTO) {
1569 fip->map_dest = 0;
9b651da9 1570 fcoe_ctlr_set_state(fip, FIP_ST_ENABLED);
97c8389d 1571 state = FIP_ST_ENABLED;
0f51c2e5 1572 LIBFCOE_FIP_DBG(fip, "Using FIP mode\n");
97c8389d 1573 }
9a6cf881 1574 fip_vlan_resp = fip->fip_resp;
fdb068c6 1575 mutex_unlock(&fip->ctlr_mutex);
e10f8c66
JE
1576
1577 if (fip->mode == FIP_MODE_VN2VN && op == FIP_OP_VN2VN)
1578 return fcoe_ctlr_vn_recv(fip, skb);
1579
9a6cf881
HR
1580 if (fip_vlan_resp && op == FIP_OP_VLAN) {
1581 LIBFCOE_FIP_DBG(fip, "fip vlan discovery\n");
1582 return fcoe_ctlr_vlan_recv(fip, skb);
1583 }
1584
e10f8c66
JE
1585 if (state != FIP_ST_ENABLED && state != FIP_ST_VNMP_UP &&
1586 state != FIP_ST_VNMP_CLAIM)
97c8389d
JE
1587 goto drop;
1588
1589 if (op == FIP_OP_LS) {
1590 fcoe_ctlr_recv_els(fip, skb); /* consumes skb */
1591 return 0;
1592 }
e10f8c66
JE
1593
1594 if (state != FIP_ST_ENABLED)
1595 goto drop;
1596
97c8389d
JE
1597 if (op == FIP_OP_DISC && sub == FIP_SC_ADV)
1598 fcoe_ctlr_recv_adv(fip, skb);
1599 else if (op == FIP_OP_CTRL && sub == FIP_SC_CLR_VLINK)
fd37f66e 1600 fcoe_ctlr_recv_clr_vlink(fip, skb);
97c8389d
JE
1601 kfree_skb(skb);
1602 return 0;
1603drop:
1604 kfree_skb(skb);
1605 return -1;
1606}
1607
1608/**
70b51aab
RL
1609 * fcoe_ctlr_select() - Select the best FCF (if possible)
1610 * @fip: The FCoE controller
97c8389d 1611 *
ba9cd5d0
JE
1612 * Returns the selected FCF, or NULL if none are usable.
1613 *
97c8389d
JE
1614 * If there are conflicting advertisements, no FCF can be chosen.
1615 *
794d98e7
JE
1616 * If there is already a selected FCF, this will choose a better one or
1617 * an equivalent one that hasn't already been sent a FLOGI.
1618 *
97c8389d
JE
1619 * Called with lock held.
1620 */
ba9cd5d0 1621static struct fcoe_fcf *fcoe_ctlr_select(struct fcoe_ctlr *fip)
97c8389d
JE
1622{
1623 struct fcoe_fcf *fcf;
794d98e7 1624 struct fcoe_fcf *best = fip->sel_fcf;
97c8389d
JE
1625
1626 list_for_each_entry(fcf, &fip->fcfs, list) {
9069f5c4
JE
1627 LIBFCOE_FIP_DBG(fip, "consider FCF fab %16.16llx "
1628 "VFID %d mac %pM map %x val %d "
1629 "sent %u pri %u\n",
1630 fcf->fabric_name, fcf->vfid, fcf->fcf_mac,
1631 fcf->fc_map, fcoe_ctlr_mtu_valid(fcf),
1632 fcf->flogi_sent, fcf->pri);
97c8389d 1633 if (!fcoe_ctlr_fcf_usable(fcf)) {
9f8f3aa6
CL
1634 LIBFCOE_FIP_DBG(fip, "FCF for fab %16.16llx "
1635 "map %x %svalid %savailable\n",
1636 fcf->fabric_name, fcf->fc_map,
1637 (fcf->flags & FIP_FL_SOL) ? "" : "in",
1638 (fcf->flags & FIP_FL_AVAIL) ?
1639 "" : "un");
97c8389d
JE
1640 continue;
1641 }
e6c10b7c
KM
1642 if (!best || fcf->pri < best->pri || best->flogi_sent)
1643 best = fcf;
1644 if (fcf->fabric_name != best->fabric_name ||
1645 fcf->vfid != best->vfid ||
1646 fcf->fc_map != best->fc_map) {
1f953b0d
BPG
1647 LIBFCOE_FIP_DBG(fip, "Conflicting fabric, VFID, "
1648 "or FC-MAP\n");
1649 return NULL;
1650 }
97c8389d
JE
1651 }
1652 fip->sel_fcf = best;
c47036a7 1653 if (best) {
9069f5c4 1654 LIBFCOE_FIP_DBG(fip, "using FCF mac %pM\n", best->fcf_mac);
c47036a7
JE
1655 fip->port_ka_time = jiffies +
1656 msecs_to_jiffies(FIP_VN_KA_PERIOD);
1657 fip->ctlr_ka_time = jiffies + best->fka_period;
1658 if (time_before(fip->ctlr_ka_time, fip->timer.expires))
1659 mod_timer(&fip->timer, fip->ctlr_ka_time);
1660 }
ba9cd5d0 1661 return best;
97c8389d
JE
1662}
1663
794d98e7
JE
1664/**
1665 * fcoe_ctlr_flogi_send_locked() - send FIP-encapsulated FLOGI to current FCF
1666 * @fip: The FCoE controller
1667 *
1668 * Returns non-zero error if it could not be sent.
1669 *
1670 * Called with ctlr_mutex and ctlr_lock held.
1671 * Caller must verify that fip->sel_fcf is not NULL.
1672 */
1673static int fcoe_ctlr_flogi_send_locked(struct fcoe_ctlr *fip)
1674{
1675 struct sk_buff *skb;
1676 struct sk_buff *skb_orig;
1677 struct fc_frame_header *fh;
1678 int error;
1679
1680 skb_orig = fip->flogi_req;
1681 if (!skb_orig)
1682 return -EINVAL;
1683
1684 /*
1685 * Clone and send the FLOGI request. If clone fails, use original.
1686 */
1687 skb = skb_clone(skb_orig, GFP_ATOMIC);
1688 if (!skb) {
1689 skb = skb_orig;
1690 fip->flogi_req = NULL;
1691 }
1692 fh = (struct fc_frame_header *)skb->data;
1693 error = fcoe_ctlr_encaps(fip, fip->lp, FIP_DT_FLOGI, skb,
1694 ntoh24(fh->fh_d_id));
1695 if (error) {
1696 kfree_skb(skb);
1697 return error;
1698 }
1699 fip->send(fip, skb);
1700 fip->sel_fcf->flogi_sent = 1;
1701 return 0;
1702}
1703
1704/**
1705 * fcoe_ctlr_flogi_retry() - resend FLOGI request to a new FCF if possible
1706 * @fip: The FCoE controller
1707 *
1708 * Returns non-zero error code if there's no FLOGI request to retry or
1709 * no alternate FCF available.
1710 */
1711static int fcoe_ctlr_flogi_retry(struct fcoe_ctlr *fip)
1712{
1713 struct fcoe_fcf *fcf;
1714 int error;
1715
1716 mutex_lock(&fip->ctlr_mutex);
1717 spin_lock_bh(&fip->ctlr_lock);
1718 LIBFCOE_FIP_DBG(fip, "re-sending FLOGI - reselect\n");
ba9cd5d0 1719 fcf = fcoe_ctlr_select(fip);
794d98e7
JE
1720 if (!fcf || fcf->flogi_sent) {
1721 kfree_skb(fip->flogi_req);
1722 fip->flogi_req = NULL;
1723 error = -ENOENT;
1724 } else {
1725 fcoe_ctlr_solicit(fip, NULL);
1726 error = fcoe_ctlr_flogi_send_locked(fip);
1727 }
1728 spin_unlock_bh(&fip->ctlr_lock);
1729 mutex_unlock(&fip->ctlr_mutex);
1730 return error;
1731}
1732
1733
1734/**
1735 * fcoe_ctlr_flogi_send() - Handle sending of FIP FLOGI.
1736 * @fip: The FCoE controller that timed out
1737 *
1738 * Done here because fcoe_ctlr_els_send() can't get mutex.
1739 *
1740 * Called with ctlr_mutex held. The caller must not hold ctlr_lock.
1741 */
1742static void fcoe_ctlr_flogi_send(struct fcoe_ctlr *fip)
1743{
1744 struct fcoe_fcf *fcf;
1745
1746 spin_lock_bh(&fip->ctlr_lock);
1747 fcf = fip->sel_fcf;
1748 if (!fcf || !fip->flogi_req_send)
1749 goto unlock;
1750
1751 LIBFCOE_FIP_DBG(fip, "sending FLOGI\n");
1752
1753 /*
1754 * If this FLOGI is being sent due to a timeout retry
1755 * to the same FCF as before, select a different FCF if possible.
1756 */
1757 if (fcf->flogi_sent) {
1758 LIBFCOE_FIP_DBG(fip, "sending FLOGI - reselect\n");
ba9cd5d0 1759 fcf = fcoe_ctlr_select(fip);
794d98e7
JE
1760 if (!fcf || fcf->flogi_sent) {
1761 LIBFCOE_FIP_DBG(fip, "sending FLOGI - clearing\n");
1762 list_for_each_entry(fcf, &fip->fcfs, list)
1763 fcf->flogi_sent = 0;
ba9cd5d0 1764 fcf = fcoe_ctlr_select(fip);
794d98e7
JE
1765 }
1766 }
1767 if (fcf) {
1768 fcoe_ctlr_flogi_send_locked(fip);
1769 fip->flogi_req_send = 0;
1770 } else /* XXX */
1771 LIBFCOE_FIP_DBG(fip, "No FCF selected - defer send\n");
1772unlock:
1773 spin_unlock_bh(&fip->ctlr_lock);
1774}
1775
97c8389d 1776/**
70b51aab
RL
1777 * fcoe_ctlr_timeout() - FIP timeout handler
1778 * @arg: The FCoE controller that timed out
97c8389d 1779 */
e99e88a9 1780static void fcoe_ctlr_timeout(struct timer_list *t)
97c8389d 1781{
e99e88a9 1782 struct fcoe_ctlr *fip = from_timer(fip, t, timer);
fdb068c6
JE
1783
1784 schedule_work(&fip->timer_work);
1785}
1786
1787/**
1788 * fcoe_ctlr_timer_work() - Worker thread function for timer work
1789 * @work: Handle to a FCoE controller
1790 *
1791 * Ages FCFs. Triggers FCF selection if possible.
1792 * Sends keep-alives and resets.
1793 */
1794static void fcoe_ctlr_timer_work(struct work_struct *work)
1795{
1796 struct fcoe_ctlr *fip;
1797 struct fc_lport *vport;
1798 u8 *mac;
1799 u8 reset = 0;
1800 u8 send_ctlr_ka = 0;
1801 u8 send_port_ka = 0;
97c8389d
JE
1802 struct fcoe_fcf *sel;
1803 struct fcoe_fcf *fcf;
8690cb83 1804 unsigned long next_timer;
97c8389d 1805
fdb068c6 1806 fip = container_of(work, struct fcoe_ctlr, timer_work);
e10f8c66
JE
1807 if (fip->mode == FIP_MODE_VN2VN)
1808 return fcoe_ctlr_vn_timeout(fip);
fdb068c6 1809 mutex_lock(&fip->ctlr_mutex);
97c8389d 1810 if (fip->state == FIP_ST_DISABLED) {
fdb068c6 1811 mutex_unlock(&fip->ctlr_mutex);
97c8389d
JE
1812 return;
1813 }
1814
1815 fcf = fip->sel_fcf;
8690cb83 1816 next_timer = fcoe_ctlr_age_fcfs(fip);
97c8389d
JE
1817
1818 sel = fip->sel_fcf;
8690cb83
JE
1819 if (!sel && fip->sel_time) {
1820 if (time_after_eq(jiffies, fip->sel_time)) {
ba9cd5d0 1821 sel = fcoe_ctlr_select(fip);
8690cb83
JE
1822 fip->sel_time = 0;
1823 } else if (time_after(next_timer, fip->sel_time))
1824 next_timer = fip->sel_time;
97c8389d
JE
1825 }
1826
794d98e7
JE
1827 if (sel && fip->flogi_req_send)
1828 fcoe_ctlr_flogi_send(fip);
1829 else if (!sel && fcf)
1830 reset = 1;
97c8389d 1831
8cdffdcc 1832 if (sel && !sel->fd_flags) {
97c8389d
JE
1833 if (time_after_eq(jiffies, fip->ctlr_ka_time)) {
1834 fip->ctlr_ka_time = jiffies + sel->fka_period;
fdb068c6 1835 send_ctlr_ka = 1;
97c8389d
JE
1836 }
1837 if (time_after(next_timer, fip->ctlr_ka_time))
1838 next_timer = fip->ctlr_ka_time;
1839
1840 if (time_after_eq(jiffies, fip->port_ka_time)) {
f47dd855 1841 fip->port_ka_time = jiffies +
70b51aab 1842 msecs_to_jiffies(FIP_VN_KA_PERIOD);
fdb068c6 1843 send_port_ka = 1;
97c8389d
JE
1844 }
1845 if (time_after(next_timer, fip->port_ka_time))
1846 next_timer = fip->port_ka_time;
97c8389d 1847 }
8690cb83
JE
1848 if (!list_empty(&fip->fcfs))
1849 mod_timer(&fip->timer, next_timer);
fdb068c6 1850 mutex_unlock(&fip->ctlr_mutex);
97c8389d 1851
516a6486 1852 if (reset) {
dd42dac4 1853 fc_lport_reset(fip->lp);
516a6486
BPG
1854 /* restart things with a solicitation */
1855 fcoe_ctlr_solicit(fip, NULL);
1856 }
11b56188 1857
fdb068c6 1858 if (send_ctlr_ka)
11b56188 1859 fcoe_ctlr_send_keep_alive(fip, NULL, 0, fip->ctl_src_addr);
fdb068c6
JE
1860
1861 if (send_port_ka) {
11b56188
CL
1862 mutex_lock(&fip->lp->lp_mutex);
1863 mac = fip->get_src_addr(fip->lp);
1864 fcoe_ctlr_send_keep_alive(fip, fip->lp, 1, mac);
1865 list_for_each_entry(vport, &fip->lp->vports, list) {
1866 mac = fip->get_src_addr(vport);
1867 fcoe_ctlr_send_keep_alive(fip, vport, 1, mac);
1868 }
1869 mutex_unlock(&fip->lp->lp_mutex);
1870 }
97c8389d
JE
1871}
1872
1873/**
70b51aab
RL
1874 * fcoe_ctlr_recv_work() - Worker thread function for receiving FIP frames
1875 * @recv_work: Handle to a FCoE controller
97c8389d
JE
1876 */
1877static void fcoe_ctlr_recv_work(struct work_struct *recv_work)
1878{
1879 struct fcoe_ctlr *fip;
1880 struct sk_buff *skb;
1881
1882 fip = container_of(recv_work, struct fcoe_ctlr, recv_work);
1f4aed81 1883 while ((skb = skb_dequeue(&fip->fip_recv_list)))
97c8389d 1884 fcoe_ctlr_recv_handler(fip, skb);
97c8389d
JE
1885}
1886
1887/**
386309ce 1888 * fcoe_ctlr_recv_flogi() - Snoop pre-FIP receipt of FLOGI response
70b51aab
RL
1889 * @fip: The FCoE controller
1890 * @fp: The FC frame to snoop
97c8389d
JE
1891 *
1892 * Snoop potential response to FLOGI or even incoming FLOGI.
1893 *
1894 * The caller has checked that we are waiting for login as indicated
1895 * by fip->flogi_oxid != FC_XID_UNKNOWN.
1896 *
1897 * The caller is responsible for freeing the frame.
386309ce 1898 * Fill in the granted_mac address.
97c8389d
JE
1899 *
1900 * Return non-zero if the frame should not be delivered to libfc.
1901 */
11b56188 1902int fcoe_ctlr_recv_flogi(struct fcoe_ctlr *fip, struct fc_lport *lport,
386309ce 1903 struct fc_frame *fp)
97c8389d
JE
1904{
1905 struct fc_frame_header *fh;
1906 u8 op;
386309ce 1907 u8 *sa;
97c8389d 1908
386309ce 1909 sa = eth_hdr(&fp->skb)->h_source;
97c8389d
JE
1910 fh = fc_frame_header_get(fp);
1911 if (fh->fh_type != FC_TYPE_ELS)
1912 return 0;
1913
1914 op = fc_frame_payload_op(fp);
1915 if (op == ELS_LS_ACC && fh->fh_r_ctl == FC_RCTL_ELS_REP &&
1916 fip->flogi_oxid == ntohs(fh->fh_ox_id)) {
1917
fdb068c6 1918 mutex_lock(&fip->ctlr_mutex);
97c8389d 1919 if (fip->state != FIP_ST_AUTO && fip->state != FIP_ST_NON_FIP) {
fdb068c6 1920 mutex_unlock(&fip->ctlr_mutex);
97c8389d
JE
1921 return -EINVAL;
1922 }
9b651da9 1923 fcoe_ctlr_set_state(fip, FIP_ST_NON_FIP);
0f51c2e5
JE
1924 LIBFCOE_FIP_DBG(fip,
1925 "received FLOGI LS_ACC using non-FIP mode\n");
97c8389d
JE
1926
1927 /*
1928 * FLOGI accepted.
1929 * If the src mac addr is FC_OUI-based, then we mark the
1930 * address_mode flag to use FC_OUI-based Ethernet DA.
1931 * Otherwise we use the FCoE gateway addr
1932 */
6942df7f 1933 if (ether_addr_equal(sa, (u8[6])FC_FCOE_FLOGI_MAC)) {
cd229e42 1934 fcoe_ctlr_map_dest(fip);
97c8389d
JE
1935 } else {
1936 memcpy(fip->dest_addr, sa, ETH_ALEN);
1937 fip->map_dest = 0;
1938 }
1939 fip->flogi_oxid = FC_XID_UNKNOWN;
fdb068c6 1940 mutex_unlock(&fip->ctlr_mutex);
386309ce 1941 fc_fcoe_set_mac(fr_cb(fp)->granted_mac, fh->fh_d_id);
97c8389d
JE
1942 } else if (op == ELS_FLOGI && fh->fh_r_ctl == FC_RCTL_ELS_REQ && sa) {
1943 /*
1944 * Save source MAC for point-to-point responses.
1945 */
fdb068c6 1946 mutex_lock(&fip->ctlr_mutex);
97c8389d
JE
1947 if (fip->state == FIP_ST_AUTO || fip->state == FIP_ST_NON_FIP) {
1948 memcpy(fip->dest_addr, sa, ETH_ALEN);
1949 fip->map_dest = 0;
e49bf614
JE
1950 if (fip->state == FIP_ST_AUTO)
1951 LIBFCOE_FIP_DBG(fip, "received non-FIP FLOGI. "
1952 "Setting non-FIP mode\n");
9b651da9 1953 fcoe_ctlr_set_state(fip, FIP_ST_NON_FIP);
97c8389d 1954 }
fdb068c6 1955 mutex_unlock(&fip->ctlr_mutex);
97c8389d
JE
1956 }
1957 return 0;
1958}
1959EXPORT_SYMBOL(fcoe_ctlr_recv_flogi);
1960
5e80f7f7 1961/**
70b51aab
RL
1962 * fcoe_wwn_from_mac() - Converts a 48-bit IEEE MAC address to a 64-bit FC WWN
1963 * @mac: The MAC address to convert
1964 * @scheme: The scheme to use when converting
1965 * @port: The port indicator for converting
5e80f7f7
VD
1966 *
1967 * Returns: u64 fc world wide name
1968 */
1969u64 fcoe_wwn_from_mac(unsigned char mac[MAX_ADDR_LEN],
1970 unsigned int scheme, unsigned int port)
1971{
1972 u64 wwn;
1973 u64 host_mac;
1974
1975 /* The MAC is in NO, so flip only the low 48 bits */
1976 host_mac = ((u64) mac[0] << 40) |
1977 ((u64) mac[1] << 32) |
1978 ((u64) mac[2] << 24) |
1979 ((u64) mac[3] << 16) |
1980 ((u64) mac[4] << 8) |
1981 (u64) mac[5];
1982
1983 WARN_ON(host_mac >= (1ULL << 48));
1984 wwn = host_mac | ((u64) scheme << 60);
1985 switch (scheme) {
1986 case 1:
1987 WARN_ON(port != 0);
1988 break;
1989 case 2:
1990 WARN_ON(port >= 0xfff);
1991 wwn |= (u64) port << 48;
1992 break;
1993 default:
1994 WARN_ON(1);
1995 break;
1996 }
1997
1998 return wwn;
1999}
2000EXPORT_SYMBOL_GPL(fcoe_wwn_from_mac);
2001
e10f8c66
JE
2002/**
2003 * fcoe_ctlr_rport() - return the fcoe_rport for a given fc_rport_priv
2004 * @rdata: libfc remote port
2005 */
2006static inline struct fcoe_rport *fcoe_ctlr_rport(struct fc_rport_priv *rdata)
2007{
2008 return (struct fcoe_rport *)(rdata + 1);
2009}
2010
2011/**
2012 * fcoe_ctlr_vn_send() - Send a FIP VN2VN Probe Request or Reply.
2013 * @fip: The FCoE controller
2014 * @sub: sub-opcode for probe request, reply, or advertisement.
2015 * @dest: The destination Ethernet MAC address
2016 * @min_len: minimum size of the Ethernet payload to be sent
2017 */
2018static void fcoe_ctlr_vn_send(struct fcoe_ctlr *fip,
2019 enum fip_vn2vn_subcode sub,
2020 const u8 *dest, size_t min_len)
2021{
2022 struct sk_buff *skb;
b3d30f4a 2023 struct fip_vn2vn_probe_frame {
e10f8c66
JE
2024 struct ethhdr eth;
2025 struct fip_header fip;
2026 struct fip_mac_desc mac;
2027 struct fip_wwn_desc wwnn;
2028 struct fip_vn_desc vn;
0095a921 2029 } __packed * frame;
e10f8c66
JE
2030 struct fip_fc4_feat *ff;
2031 struct fip_size_desc *size;
2032 u32 fcp_feat;
2033 size_t len;
2034 size_t dlen;
2035
2036 len = sizeof(*frame);
2037 dlen = 0;
2038 if (sub == FIP_SC_VN_CLAIM_NOTIFY || sub == FIP_SC_VN_CLAIM_REP) {
2039 dlen = sizeof(struct fip_fc4_feat) +
2040 sizeof(struct fip_size_desc);
2041 len += dlen;
2042 }
2043 dlen += sizeof(frame->mac) + sizeof(frame->wwnn) + sizeof(frame->vn);
2044 len = max(len, min_len + sizeof(struct ethhdr));
2045
2046 skb = dev_alloc_skb(len);
2047 if (!skb)
2048 return;
2049
b3d30f4a 2050 frame = (struct fip_vn2vn_probe_frame *)skb->data;
e10f8c66
JE
2051 memset(frame, 0, len);
2052 memcpy(frame->eth.h_dest, dest, ETH_ALEN);
d227f029
YZ
2053
2054 if (sub == FIP_SC_VN_BEACON) {
2055 hton24(frame->eth.h_source, FIP_VN_FC_MAP);
2056 hton24(frame->eth.h_source + 3, fip->port_id);
2057 } else {
2058 memcpy(frame->eth.h_source, fip->ctl_src_addr, ETH_ALEN);
2059 }
e10f8c66
JE
2060 frame->eth.h_proto = htons(ETH_P_FIP);
2061
2062 frame->fip.fip_ver = FIP_VER_ENCAPS(FIP_VER);
2063 frame->fip.fip_op = htons(FIP_OP_VN2VN);
2064 frame->fip.fip_subcode = sub;
2065 frame->fip.fip_dl_len = htons(dlen / FIP_BPW);
2066
2067 frame->mac.fd_desc.fip_dtype = FIP_DT_MAC;
2068 frame->mac.fd_desc.fip_dlen = sizeof(frame->mac) / FIP_BPW;
2069 memcpy(frame->mac.fd_mac, fip->ctl_src_addr, ETH_ALEN);
2070
2071 frame->wwnn.fd_desc.fip_dtype = FIP_DT_NAME;
2072 frame->wwnn.fd_desc.fip_dlen = sizeof(frame->wwnn) / FIP_BPW;
2073 put_unaligned_be64(fip->lp->wwnn, &frame->wwnn.fd_wwn);
2074
2075 frame->vn.fd_desc.fip_dtype = FIP_DT_VN_ID;
2076 frame->vn.fd_desc.fip_dlen = sizeof(frame->vn) / FIP_BPW;
2077 hton24(frame->vn.fd_mac, FIP_VN_FC_MAP);
2078 hton24(frame->vn.fd_mac + 3, fip->port_id);
2079 hton24(frame->vn.fd_fc_id, fip->port_id);
2080 put_unaligned_be64(fip->lp->wwpn, &frame->vn.fd_wwpn);
2081
2082 /*
2083 * For claims, add FC-4 features.
2084 * TBD: Add interface to get fc-4 types and features from libfc.
2085 */
2086 if (sub == FIP_SC_VN_CLAIM_NOTIFY || sub == FIP_SC_VN_CLAIM_REP) {
2087 ff = (struct fip_fc4_feat *)(frame + 1);
2088 ff->fd_desc.fip_dtype = FIP_DT_FC4F;
2089 ff->fd_desc.fip_dlen = sizeof(*ff) / FIP_BPW;
2090 ff->fd_fts = fip->lp->fcts;
2091
2092 fcp_feat = 0;
2093 if (fip->lp->service_params & FCP_SPPF_INIT_FCN)
2094 fcp_feat |= FCP_FEAT_INIT;
2095 if (fip->lp->service_params & FCP_SPPF_TARG_FCN)
2096 fcp_feat |= FCP_FEAT_TARG;
2097 fcp_feat <<= (FC_TYPE_FCP * 4) % 32;
2098 ff->fd_ff.fd_feat[FC_TYPE_FCP * 4 / 32] = htonl(fcp_feat);
2099
2100 size = (struct fip_size_desc *)(ff + 1);
2101 size->fd_desc.fip_dtype = FIP_DT_FCOE_SIZE;
2102 size->fd_desc.fip_dlen = sizeof(*size) / FIP_BPW;
2103 size->fd_size = htons(fcoe_ctlr_fcoe_size(fip));
2104 }
2105
2106 skb_put(skb, len);
2107 skb->protocol = htons(ETH_P_FIP);
6f6c2aa3 2108 skb->priority = fip->priority;
e10f8c66
JE
2109 skb_reset_mac_header(skb);
2110 skb_reset_network_header(skb);
2111
2112 fip->send(fip, skb);
2113}
2114
2115/**
2116 * fcoe_ctlr_vn_rport_callback - Event handler for rport events.
2117 * @lport: The lport which is receiving the event
2118 * @rdata: remote port private data
25985edc 2119 * @event: The event that occurred
e10f8c66
JE
2120 *
2121 * Locking Note: The rport lock must not be held when calling this function.
2122 */
2123static void fcoe_ctlr_vn_rport_callback(struct fc_lport *lport,
2124 struct fc_rport_priv *rdata,
2125 enum fc_rport_event event)
2126{
2127 struct fcoe_ctlr *fip = lport->disc.priv;
2128 struct fcoe_rport *frport = fcoe_ctlr_rport(rdata);
2129
2130 LIBFCOE_FIP_DBG(fip, "vn_rport_callback %x event %d\n",
2131 rdata->ids.port_id, event);
2132
2133 mutex_lock(&fip->ctlr_mutex);
2134 switch (event) {
2135 case RPORT_EV_READY:
2136 frport->login_count = 0;
2137 break;
2138 case RPORT_EV_LOGO:
2139 case RPORT_EV_FAILED:
2140 case RPORT_EV_STOP:
2141 frport->login_count++;
2142 if (frport->login_count > FCOE_CTLR_VN2VN_LOGIN_LIMIT) {
2143 LIBFCOE_FIP_DBG(fip,
2144 "rport FLOGI limited port_id %6.6x\n",
2145 rdata->ids.port_id);
c96c792a 2146 fc_rport_logoff(rdata);
e10f8c66
JE
2147 }
2148 break;
2149 default:
2150 break;
2151 }
2152 mutex_unlock(&fip->ctlr_mutex);
2153}
2154
2155static struct fc_rport_operations fcoe_ctlr_vn_rport_ops = {
2156 .event_callback = fcoe_ctlr_vn_rport_callback,
2157};
2158
2159/**
2160 * fcoe_ctlr_disc_stop_locked() - stop discovery in VN2VN mode
2161 * @fip: The FCoE controller
2162 *
2163 * Called with ctlr_mutex held.
2164 */
2165static void fcoe_ctlr_disc_stop_locked(struct fc_lport *lport)
2166{
d17efa00
MR
2167 struct fc_rport_priv *rdata;
2168
0993ed92 2169 mutex_lock(&lport->disc.disc_mutex);
a407c593
HR
2170 list_for_each_entry_rcu(rdata, &lport->disc.rports, peers) {
2171 if (kref_get_unless_zero(&rdata->kref)) {
c96c792a 2172 fc_rport_logoff(rdata);
944ef968 2173 kref_put(&rdata->kref, fc_rport_destroy);
a407c593
HR
2174 }
2175 }
e10f8c66
JE
2176 lport->disc.disc_callback = NULL;
2177 mutex_unlock(&lport->disc.disc_mutex);
2178}
2179
2180/**
2181 * fcoe_ctlr_disc_stop() - stop discovery in VN2VN mode
2182 * @fip: The FCoE controller
2183 *
2184 * Called through the local port template for discovery.
2185 * Called without the ctlr_mutex held.
2186 */
2187static void fcoe_ctlr_disc_stop(struct fc_lport *lport)
2188{
2189 struct fcoe_ctlr *fip = lport->disc.priv;
2190
2191 mutex_lock(&fip->ctlr_mutex);
2192 fcoe_ctlr_disc_stop_locked(lport);
2193 mutex_unlock(&fip->ctlr_mutex);
2194}
2195
2196/**
2197 * fcoe_ctlr_disc_stop_final() - stop discovery for shutdown in VN2VN mode
2198 * @fip: The FCoE controller
2199 *
2200 * Called through the local port template for discovery.
2201 * Called without the ctlr_mutex held.
2202 */
2203static void fcoe_ctlr_disc_stop_final(struct fc_lport *lport)
2204{
2205 fcoe_ctlr_disc_stop(lport);
5922a957 2206 fc_rport_flush_queue();
e10f8c66
JE
2207 synchronize_rcu();
2208}
2209
2210/**
2211 * fcoe_ctlr_vn_restart() - VN2VN probe restart with new port_id
2212 * @fip: The FCoE controller
2213 *
2214 * Called with fcoe_ctlr lock held.
2215 */
2216static void fcoe_ctlr_vn_restart(struct fcoe_ctlr *fip)
2217{
2218 unsigned long wait;
2219 u32 port_id;
2220
2221 fcoe_ctlr_disc_stop_locked(fip->lp);
2222
2223 /*
2224 * Get proposed port ID.
2225 * If this is the first try after link up, use any previous port_id.
2226 * If there was none, use the low bits of the port_name.
2227 * On subsequent tries, get the next random one.
2228 * Don't use reserved IDs, use another non-zero value, just as random.
2229 */
2230 port_id = fip->port_id;
2231 if (fip->probe_tries)
496f2f93 2232 port_id = prandom_u32_state(&fip->rnd_state) & 0xffff;
e10f8c66
JE
2233 else if (!port_id)
2234 port_id = fip->lp->wwpn & 0xffff;
2235 if (!port_id || port_id == 0xffff)
2236 port_id = 1;
2237 fip->port_id = port_id;
2238
2239 if (fip->probe_tries < FIP_VN_RLIM_COUNT) {
2240 fip->probe_tries++;
3b60a64f 2241 wait = prandom_u32() % FIP_VN_PROBE_WAIT;
e10f8c66
JE
2242 } else
2243 wait = FIP_VN_RLIM_INT;
2244 mod_timer(&fip->timer, jiffies + msecs_to_jiffies(wait));
2245 fcoe_ctlr_set_state(fip, FIP_ST_VNMP_START);
2246}
2247
2248/**
2249 * fcoe_ctlr_vn_start() - Start in VN2VN mode
2250 * @fip: The FCoE controller
2251 *
2252 * Called with fcoe_ctlr lock held.
2253 */
2254static void fcoe_ctlr_vn_start(struct fcoe_ctlr *fip)
2255{
2256 fip->probe_tries = 0;
496f2f93 2257 prandom_seed_state(&fip->rnd_state, fip->lp->wwpn);
e10f8c66
JE
2258 fcoe_ctlr_vn_restart(fip);
2259}
2260
2261/**
2262 * fcoe_ctlr_vn_parse - parse probe request or response
2263 * @fip: The FCoE controller
2264 * @skb: incoming packet
2265 * @rdata: buffer for resulting parsed VN entry plus fcoe_rport
2266 *
2267 * Returns non-zero error number on error.
2268 * Does not consume the packet.
2269 */
2270static int fcoe_ctlr_vn_parse(struct fcoe_ctlr *fip,
2271 struct sk_buff *skb,
2272 struct fc_rport_priv *rdata)
2273{
2274 struct fip_header *fiph;
2275 struct fip_desc *desc = NULL;
2276 struct fip_mac_desc *macd = NULL;
2277 struct fip_wwn_desc *wwn = NULL;
2278 struct fip_vn_desc *vn = NULL;
2279 struct fip_size_desc *size = NULL;
2280 struct fcoe_rport *frport;
2281 size_t rlen;
2282 size_t dlen;
2283 u32 desc_mask = 0;
2284 u32 dtype;
2285 u8 sub;
2286
2287 memset(rdata, 0, sizeof(*rdata) + sizeof(*frport));
2288 frport = fcoe_ctlr_rport(rdata);
2289
2290 fiph = (struct fip_header *)skb->data;
2291 frport->flags = ntohs(fiph->fip_flags);
2292
2293 sub = fiph->fip_subcode;
2294 switch (sub) {
2295 case FIP_SC_VN_PROBE_REQ:
2296 case FIP_SC_VN_PROBE_REP:
2297 case FIP_SC_VN_BEACON:
2298 desc_mask = BIT(FIP_DT_MAC) | BIT(FIP_DT_NAME) |
2299 BIT(FIP_DT_VN_ID);
2300 break;
2301 case FIP_SC_VN_CLAIM_NOTIFY:
2302 case FIP_SC_VN_CLAIM_REP:
2303 desc_mask = BIT(FIP_DT_MAC) | BIT(FIP_DT_NAME) |
2304 BIT(FIP_DT_VN_ID) | BIT(FIP_DT_FC4F) |
2305 BIT(FIP_DT_FCOE_SIZE);
2306 break;
2307 default:
2308 LIBFCOE_FIP_DBG(fip, "vn_parse unknown subcode %u\n", sub);
2309 return -EINVAL;
2310 }
2311
2312 rlen = ntohs(fiph->fip_dl_len) * 4;
2313 if (rlen + sizeof(*fiph) > skb->len)
2314 return -EINVAL;
2315
2316 desc = (struct fip_desc *)(fiph + 1);
2317 while (rlen > 0) {
2318 dlen = desc->fip_dlen * FIP_BPW;
2319 if (dlen < sizeof(*desc) || dlen > rlen)
2320 return -EINVAL;
2321
2322 dtype = desc->fip_dtype;
2323 if (dtype < 32) {
2324 if (!(desc_mask & BIT(dtype))) {
2325 LIBFCOE_FIP_DBG(fip,
2326 "unexpected or duplicated desc "
2327 "desc type %u in "
2328 "FIP VN2VN subtype %u\n",
2329 dtype, sub);
2330 return -EINVAL;
2331 }
2332 desc_mask &= ~BIT(dtype);
2333 }
2334
2335 switch (dtype) {
2336 case FIP_DT_MAC:
2337 if (dlen != sizeof(struct fip_mac_desc))
2338 goto len_err;
2339 macd = (struct fip_mac_desc *)desc;
2340 if (!is_valid_ether_addr(macd->fd_mac)) {
2341 LIBFCOE_FIP_DBG(fip,
2342 "Invalid MAC addr %pM in FIP VN2VN\n",
2343 macd->fd_mac);
2344 return -EINVAL;
2345 }
2346 memcpy(frport->enode_mac, macd->fd_mac, ETH_ALEN);
2347 break;
2348 case FIP_DT_NAME:
2349 if (dlen != sizeof(struct fip_wwn_desc))
2350 goto len_err;
2351 wwn = (struct fip_wwn_desc *)desc;
2352 rdata->ids.node_name = get_unaligned_be64(&wwn->fd_wwn);
2353 break;
2354 case FIP_DT_VN_ID:
2355 if (dlen != sizeof(struct fip_vn_desc))
2356 goto len_err;
2357 vn = (struct fip_vn_desc *)desc;
2358 memcpy(frport->vn_mac, vn->fd_mac, ETH_ALEN);
2359 rdata->ids.port_id = ntoh24(vn->fd_fc_id);
2360 rdata->ids.port_name = get_unaligned_be64(&vn->fd_wwpn);
2361 break;
2362 case FIP_DT_FC4F:
2363 if (dlen != sizeof(struct fip_fc4_feat))
2364 goto len_err;
2365 break;
2366 case FIP_DT_FCOE_SIZE:
2367 if (dlen != sizeof(struct fip_size_desc))
2368 goto len_err;
2369 size = (struct fip_size_desc *)desc;
2370 frport->fcoe_len = ntohs(size->fd_size);
2371 break;
2372 default:
2373 LIBFCOE_FIP_DBG(fip, "unexpected descriptor type %x "
2374 "in FIP probe\n", dtype);
2375 /* standard says ignore unknown descriptors >= 128 */
6a551c11 2376 if (dtype < FIP_DT_NON_CRITICAL)
e10f8c66
JE
2377 return -EINVAL;
2378 break;
2379 }
2380 desc = (struct fip_desc *)((char *)desc + dlen);
2381 rlen -= dlen;
2382 }
2383 return 0;
2384
2385len_err:
2386 LIBFCOE_FIP_DBG(fip, "FIP length error in descriptor type %x len %zu\n",
2387 dtype, dlen);
2388 return -EINVAL;
2389}
2390
2391/**
2392 * fcoe_ctlr_vn_send_claim() - send multicast FIP VN2VN Claim Notification.
2393 * @fip: The FCoE controller
2394 *
2395 * Called with ctlr_mutex held.
2396 */
2397static void fcoe_ctlr_vn_send_claim(struct fcoe_ctlr *fip)
2398{
2399 fcoe_ctlr_vn_send(fip, FIP_SC_VN_CLAIM_NOTIFY, fcoe_all_vn2vn, 0);
2400 fip->sol_time = jiffies;
2401}
2402
2403/**
2404 * fcoe_ctlr_vn_probe_req() - handle incoming VN2VN probe request.
2405 * @fip: The FCoE controller
2406 * @rdata: parsed remote port with frport from the probe request
2407 *
2408 * Called with ctlr_mutex held.
2409 */
2410static void fcoe_ctlr_vn_probe_req(struct fcoe_ctlr *fip,
2411 struct fc_rport_priv *rdata)
2412{
2413 struct fcoe_rport *frport = fcoe_ctlr_rport(rdata);
2414
2415 if (rdata->ids.port_id != fip->port_id)
2416 return;
2417
2418 switch (fip->state) {
2419 case FIP_ST_VNMP_CLAIM:
2420 case FIP_ST_VNMP_UP:
c9596550
HR
2421 LIBFCOE_FIP_DBG(fip, "vn_probe_req: send reply, state %x\n",
2422 fip->state);
e10f8c66
JE
2423 fcoe_ctlr_vn_send(fip, FIP_SC_VN_PROBE_REP,
2424 frport->enode_mac, 0);
2425 break;
2426 case FIP_ST_VNMP_PROBE1:
2427 case FIP_ST_VNMP_PROBE2:
2428 /*
2429 * Decide whether to reply to the Probe.
2430 * Our selected address is never a "recorded" one, so
2431 * only reply if our WWPN is greater and the
2432 * Probe's REC bit is not set.
2433 * If we don't reply, we will change our address.
2434 */
2435 if (fip->lp->wwpn > rdata->ids.port_name &&
2436 !(frport->flags & FIP_FL_REC_OR_P2P)) {
c9596550
HR
2437 LIBFCOE_FIP_DBG(fip, "vn_probe_req: "
2438 "port_id collision\n");
e10f8c66
JE
2439 fcoe_ctlr_vn_send(fip, FIP_SC_VN_PROBE_REP,
2440 frport->enode_mac, 0);
2441 break;
2442 }
2443 /* fall through */
2444 case FIP_ST_VNMP_START:
c9596550
HR
2445 LIBFCOE_FIP_DBG(fip, "vn_probe_req: "
2446 "restart VN2VN negotiation\n");
e10f8c66
JE
2447 fcoe_ctlr_vn_restart(fip);
2448 break;
2449 default:
c9596550
HR
2450 LIBFCOE_FIP_DBG(fip, "vn_probe_req: ignore state %x\n",
2451 fip->state);
e10f8c66
JE
2452 break;
2453 }
2454}
2455
2456/**
2457 * fcoe_ctlr_vn_probe_reply() - handle incoming VN2VN probe reply.
2458 * @fip: The FCoE controller
2459 * @rdata: parsed remote port with frport from the probe request
2460 *
2461 * Called with ctlr_mutex held.
2462 */
2463static void fcoe_ctlr_vn_probe_reply(struct fcoe_ctlr *fip,
2464 struct fc_rport_priv *rdata)
2465{
2466 if (rdata->ids.port_id != fip->port_id)
2467 return;
2468 switch (fip->state) {
2469 case FIP_ST_VNMP_START:
2470 case FIP_ST_VNMP_PROBE1:
2471 case FIP_ST_VNMP_PROBE2:
2472 case FIP_ST_VNMP_CLAIM:
c9596550
HR
2473 LIBFCOE_FIP_DBG(fip, "vn_probe_reply: restart state %x\n",
2474 fip->state);
e10f8c66
JE
2475 fcoe_ctlr_vn_restart(fip);
2476 break;
2477 case FIP_ST_VNMP_UP:
c9596550 2478 LIBFCOE_FIP_DBG(fip, "vn_probe_reply: send claim notify\n");
e10f8c66
JE
2479 fcoe_ctlr_vn_send_claim(fip);
2480 break;
2481 default:
2482 break;
2483 }
2484}
2485
2486/**
2487 * fcoe_ctlr_vn_add() - Add a VN2VN entry to the list, based on a claim reply.
2488 * @fip: The FCoE controller
2489 * @new: newly-parsed remote port with frport as a template for new rdata
2490 *
2491 * Called with ctlr_mutex held.
2492 */
2493static void fcoe_ctlr_vn_add(struct fcoe_ctlr *fip, struct fc_rport_priv *new)
2494{
2495 struct fc_lport *lport = fip->lp;
2496 struct fc_rport_priv *rdata;
2497 struct fc_rport_identifiers *ids;
2498 struct fcoe_rport *frport;
2499 u32 port_id;
2500
2501 port_id = new->ids.port_id;
2502 if (port_id == fip->port_id)
2503 return;
2504
2505 mutex_lock(&lport->disc.disc_mutex);
2580064b 2506 rdata = fc_rport_create(lport, port_id);
e10f8c66
JE
2507 if (!rdata) {
2508 mutex_unlock(&lport->disc.disc_mutex);
2509 return;
2510 }
a407c593
HR
2511 mutex_lock(&rdata->rp_mutex);
2512 mutex_unlock(&lport->disc.disc_mutex);
e10f8c66
JE
2513
2514 rdata->ops = &fcoe_ctlr_vn_rport_ops;
2515 rdata->disc_id = lport->disc.disc_id;
2516
2517 ids = &rdata->ids;
2518 if ((ids->port_name != -1 && ids->port_name != new->ids.port_name) ||
a407c593
HR
2519 (ids->node_name != -1 && ids->node_name != new->ids.node_name)) {
2520 mutex_unlock(&rdata->rp_mutex);
c9596550 2521 LIBFCOE_FIP_DBG(fip, "vn_add rport logoff %6.6x\n", port_id);
c96c792a 2522 fc_rport_logoff(rdata);
a407c593
HR
2523 mutex_lock(&rdata->rp_mutex);
2524 }
e10f8c66
JE
2525 ids->port_name = new->ids.port_name;
2526 ids->node_name = new->ids.node_name;
a407c593 2527 mutex_unlock(&rdata->rp_mutex);
e10f8c66
JE
2528
2529 frport = fcoe_ctlr_rport(rdata);
c9596550
HR
2530 LIBFCOE_FIP_DBG(fip, "vn_add rport %6.6x %s state %d\n",
2531 port_id, frport->fcoe_len ? "old" : "new",
2532 rdata->rp_state);
e10f8c66
JE
2533 *frport = *fcoe_ctlr_rport(new);
2534 frport->time = 0;
2535}
2536
2537/**
2538 * fcoe_ctlr_vn_lookup() - Find VN remote port's MAC address
2539 * @fip: The FCoE controller
2540 * @port_id: The port_id of the remote VN_node
2541 * @mac: buffer which will hold the VN_NODE destination MAC address, if found.
2542 *
2543 * Returns non-zero error if no remote port found.
2544 */
2545static int fcoe_ctlr_vn_lookup(struct fcoe_ctlr *fip, u32 port_id, u8 *mac)
2546{
2547 struct fc_lport *lport = fip->lp;
2548 struct fc_rport_priv *rdata;
2549 struct fcoe_rport *frport;
2550 int ret = -1;
2551
e87b7777 2552 rdata = fc_rport_lookup(lport, port_id);
e10f8c66
JE
2553 if (rdata) {
2554 frport = fcoe_ctlr_rport(rdata);
2555 memcpy(mac, frport->enode_mac, ETH_ALEN);
2556 ret = 0;
944ef968 2557 kref_put(&rdata->kref, fc_rport_destroy);
e10f8c66 2558 }
e10f8c66
JE
2559 return ret;
2560}
2561
2562/**
2563 * fcoe_ctlr_vn_claim_notify() - handle received FIP VN2VN Claim Notification
2564 * @fip: The FCoE controller
2565 * @new: newly-parsed remote port with frport as a template for new rdata
2566 *
2567 * Called with ctlr_mutex held.
2568 */
2569static void fcoe_ctlr_vn_claim_notify(struct fcoe_ctlr *fip,
2570 struct fc_rport_priv *new)
2571{
2572 struct fcoe_rport *frport = fcoe_ctlr_rport(new);
2573
2574 if (frport->flags & FIP_FL_REC_OR_P2P) {
c9596550 2575 LIBFCOE_FIP_DBG(fip, "send probe req for P2P/REC\n");
e10f8c66
JE
2576 fcoe_ctlr_vn_send(fip, FIP_SC_VN_PROBE_REQ, fcoe_all_vn2vn, 0);
2577 return;
2578 }
2579 switch (fip->state) {
2580 case FIP_ST_VNMP_START:
2581 case FIP_ST_VNMP_PROBE1:
2582 case FIP_ST_VNMP_PROBE2:
c9596550
HR
2583 if (new->ids.port_id == fip->port_id) {
2584 LIBFCOE_FIP_DBG(fip, "vn_claim_notify: "
2585 "restart, state %d\n",
2586 fip->state);
e10f8c66 2587 fcoe_ctlr_vn_restart(fip);
c9596550 2588 }
e10f8c66
JE
2589 break;
2590 case FIP_ST_VNMP_CLAIM:
2591 case FIP_ST_VNMP_UP:
2592 if (new->ids.port_id == fip->port_id) {
2593 if (new->ids.port_name > fip->lp->wwpn) {
c9596550
HR
2594 LIBFCOE_FIP_DBG(fip, "vn_claim_notify: "
2595 "restart, port_id collision\n");
e10f8c66
JE
2596 fcoe_ctlr_vn_restart(fip);
2597 break;
2598 }
c9596550
HR
2599 LIBFCOE_FIP_DBG(fip, "vn_claim_notify: "
2600 "send claim notify\n");
e10f8c66
JE
2601 fcoe_ctlr_vn_send_claim(fip);
2602 break;
2603 }
c9596550
HR
2604 LIBFCOE_FIP_DBG(fip, "vn_claim_notify: send reply to %x\n",
2605 new->ids.port_id);
e10f8c66
JE
2606 fcoe_ctlr_vn_send(fip, FIP_SC_VN_CLAIM_REP, frport->enode_mac,
2607 min((u32)frport->fcoe_len,
2608 fcoe_ctlr_fcoe_size(fip)));
2609 fcoe_ctlr_vn_add(fip, new);
2610 break;
2611 default:
c9596550
HR
2612 LIBFCOE_FIP_DBG(fip, "vn_claim_notify: "
2613 "ignoring claim from %x\n", new->ids.port_id);
e10f8c66
JE
2614 break;
2615 }
2616}
2617
2618/**
2619 * fcoe_ctlr_vn_claim_resp() - handle received Claim Response
2620 * @fip: The FCoE controller that received the frame
2621 * @new: newly-parsed remote port with frport from the Claim Response
2622 *
2623 * Called with ctlr_mutex held.
2624 */
2625static void fcoe_ctlr_vn_claim_resp(struct fcoe_ctlr *fip,
2626 struct fc_rport_priv *new)
2627{
2628 LIBFCOE_FIP_DBG(fip, "claim resp from from rport %x - state %s\n",
2629 new->ids.port_id, fcoe_ctlr_state(fip->state));
2630 if (fip->state == FIP_ST_VNMP_UP || fip->state == FIP_ST_VNMP_CLAIM)
2631 fcoe_ctlr_vn_add(fip, new);
2632}
2633
2634/**
2635 * fcoe_ctlr_vn_beacon() - handle received beacon.
2636 * @fip: The FCoE controller that received the frame
2637 * @new: newly-parsed remote port with frport from the Beacon
2638 *
2639 * Called with ctlr_mutex held.
2640 */
2641static void fcoe_ctlr_vn_beacon(struct fcoe_ctlr *fip,
2642 struct fc_rport_priv *new)
2643{
2644 struct fc_lport *lport = fip->lp;
2645 struct fc_rport_priv *rdata;
2646 struct fcoe_rport *frport;
2647
2648 frport = fcoe_ctlr_rport(new);
2649 if (frport->flags & FIP_FL_REC_OR_P2P) {
c9596550 2650 LIBFCOE_FIP_DBG(fip, "p2p beacon while in vn2vn mode\n");
e10f8c66
JE
2651 fcoe_ctlr_vn_send(fip, FIP_SC_VN_PROBE_REQ, fcoe_all_vn2vn, 0);
2652 return;
2653 }
e87b7777 2654 rdata = fc_rport_lookup(lport, new->ids.port_id);
e10f8c66
JE
2655 if (rdata) {
2656 if (rdata->ids.node_name == new->ids.node_name &&
2657 rdata->ids.port_name == new->ids.port_name) {
2658 frport = fcoe_ctlr_rport(rdata);
c9596550
HR
2659 LIBFCOE_FIP_DBG(fip, "beacon from rport %x\n",
2660 rdata->ids.port_id);
2661 if (!frport->time && fip->state == FIP_ST_VNMP_UP) {
2662 LIBFCOE_FIP_DBG(fip, "beacon expired "
2663 "for rport %x\n",
2664 rdata->ids.port_id);
05d7d3b0 2665 fc_rport_login(rdata);
c9596550 2666 }
e10f8c66
JE
2667 frport->time = jiffies;
2668 }
944ef968 2669 kref_put(&rdata->kref, fc_rport_destroy);
e10f8c66
JE
2670 return;
2671 }
2672 if (fip->state != FIP_ST_VNMP_UP)
2673 return;
2674
2675 /*
2676 * Beacon from a new neighbor.
2677 * Send a claim notify if one hasn't been sent recently.
2678 * Don't add the neighbor yet.
2679 */
2680 LIBFCOE_FIP_DBG(fip, "beacon from new rport %x. sending claim notify\n",
2681 new->ids.port_id);
2682 if (time_after(jiffies,
2683 fip->sol_time + msecs_to_jiffies(FIP_VN_ANN_WAIT)))
2684 fcoe_ctlr_vn_send_claim(fip);
2685}
2686
2687/**
2688 * fcoe_ctlr_vn_age() - Check for VN_ports without recent beacons
2689 * @fip: The FCoE controller
2690 *
2691 * Called with ctlr_mutex held.
2692 * Called only in state FIP_ST_VNMP_UP.
2693 * Returns the soonest time for next age-out or a time far in the future.
2694 */
2695static unsigned long fcoe_ctlr_vn_age(struct fcoe_ctlr *fip)
2696{
2697 struct fc_lport *lport = fip->lp;
2698 struct fc_rport_priv *rdata;
2699 struct fcoe_rport *frport;
2700 unsigned long next_time;
2701 unsigned long deadline;
2702
2703 next_time = jiffies + msecs_to_jiffies(FIP_VN_BEACON_INT * 10);
0993ed92 2704 mutex_lock(&lport->disc.disc_mutex);
e10f8c66 2705 list_for_each_entry_rcu(rdata, &lport->disc.rports, peers) {
a407c593
HR
2706 if (!kref_get_unless_zero(&rdata->kref))
2707 continue;
e10f8c66 2708 frport = fcoe_ctlr_rport(rdata);
a407c593 2709 if (!frport->time) {
944ef968 2710 kref_put(&rdata->kref, fc_rport_destroy);
e10f8c66 2711 continue;
a407c593 2712 }
e10f8c66
JE
2713 deadline = frport->time +
2714 msecs_to_jiffies(FIP_VN_BEACON_INT * 25 / 10);
2715 if (time_after_eq(jiffies, deadline)) {
2716 frport->time = 0;
2717 LIBFCOE_FIP_DBG(fip,
2718 "port %16.16llx fc_id %6.6x beacon expired\n",
2719 rdata->ids.port_name, rdata->ids.port_id);
c96c792a 2720 fc_rport_logoff(rdata);
e10f8c66
JE
2721 } else if (time_before(deadline, next_time))
2722 next_time = deadline;
944ef968 2723 kref_put(&rdata->kref, fc_rport_destroy);
e10f8c66 2724 }
0993ed92 2725 mutex_unlock(&lport->disc.disc_mutex);
e10f8c66
JE
2726 return next_time;
2727}
2728
2729/**
2730 * fcoe_ctlr_vn_recv() - Receive a FIP frame
2731 * @fip: The FCoE controller that received the frame
2732 * @skb: The received FIP frame
2733 *
2734 * Returns non-zero if the frame is dropped.
2735 * Always consumes the frame.
2736 */
2737static int fcoe_ctlr_vn_recv(struct fcoe_ctlr *fip, struct sk_buff *skb)
2738{
2739 struct fip_header *fiph;
2740 enum fip_vn2vn_subcode sub;
2dc02ee5 2741 struct {
e10f8c66
JE
2742 struct fc_rport_priv rdata;
2743 struct fcoe_rport frport;
2744 } buf;
5d5a51d2 2745 int rc, vlan_id = 0;
e10f8c66
JE
2746
2747 fiph = (struct fip_header *)skb->data;
2748 sub = fiph->fip_subcode;
2749
5d5a51d2
HR
2750 if (fip->lp->vlan)
2751 vlan_id = skb_vlan_tag_get_id(skb);
2752
2753 if (vlan_id && vlan_id != fip->lp->vlan) {
2754 LIBFCOE_FIP_DBG(fip, "vn_recv drop frame sub %x vlan %d\n",
2755 sub, vlan_id);
2756 rc = -EAGAIN;
2757 goto drop;
2758 }
2759
e10f8c66
JE
2760 rc = fcoe_ctlr_vn_parse(fip, skb, &buf.rdata);
2761 if (rc) {
2762 LIBFCOE_FIP_DBG(fip, "vn_recv vn_parse error %d\n", rc);
2763 goto drop;
2764 }
2765
2766 mutex_lock(&fip->ctlr_mutex);
2767 switch (sub) {
2768 case FIP_SC_VN_PROBE_REQ:
2769 fcoe_ctlr_vn_probe_req(fip, &buf.rdata);
2770 break;
2771 case FIP_SC_VN_PROBE_REP:
2772 fcoe_ctlr_vn_probe_reply(fip, &buf.rdata);
2773 break;
2774 case FIP_SC_VN_CLAIM_NOTIFY:
2775 fcoe_ctlr_vn_claim_notify(fip, &buf.rdata);
2776 break;
2777 case FIP_SC_VN_CLAIM_REP:
2778 fcoe_ctlr_vn_claim_resp(fip, &buf.rdata);
2779 break;
2780 case FIP_SC_VN_BEACON:
2781 fcoe_ctlr_vn_beacon(fip, &buf.rdata);
2782 break;
2783 default:
2784 LIBFCOE_FIP_DBG(fip, "vn_recv unknown subcode %d\n", sub);
2785 rc = -1;
2786 break;
2787 }
2788 mutex_unlock(&fip->ctlr_mutex);
2789drop:
2790 kfree_skb(skb);
2791 return rc;
2792}
2793
9a6cf881
HR
2794/**
2795 * fcoe_ctlr_vlan_parse - parse vlan discovery request or response
2796 * @fip: The FCoE controller
2797 * @skb: incoming packet
2798 * @rdata: buffer for resulting parsed VLAN entry plus fcoe_rport
2799 *
2800 * Returns non-zero error number on error.
2801 * Does not consume the packet.
2802 */
2803static int fcoe_ctlr_vlan_parse(struct fcoe_ctlr *fip,
2804 struct sk_buff *skb,
2805 struct fc_rport_priv *rdata)
2806{
2807 struct fip_header *fiph;
2808 struct fip_desc *desc = NULL;
2809 struct fip_mac_desc *macd = NULL;
2810 struct fip_wwn_desc *wwn = NULL;
2811 struct fcoe_rport *frport;
2812 size_t rlen;
2813 size_t dlen;
2814 u32 desc_mask = 0;
2815 u32 dtype;
2816 u8 sub;
2817
2818 memset(rdata, 0, sizeof(*rdata) + sizeof(*frport));
2819 frport = fcoe_ctlr_rport(rdata);
2820
2821 fiph = (struct fip_header *)skb->data;
2822 frport->flags = ntohs(fiph->fip_flags);
2823
2824 sub = fiph->fip_subcode;
2825 switch (sub) {
2826 case FIP_SC_VL_REQ:
2827 desc_mask = BIT(FIP_DT_MAC) | BIT(FIP_DT_NAME);
2828 break;
2829 default:
2830 LIBFCOE_FIP_DBG(fip, "vn_parse unknown subcode %u\n", sub);
2831 return -EINVAL;
2832 }
2833
2834 rlen = ntohs(fiph->fip_dl_len) * 4;
2835 if (rlen + sizeof(*fiph) > skb->len)
2836 return -EINVAL;
2837
2838 desc = (struct fip_desc *)(fiph + 1);
2839 while (rlen > 0) {
2840 dlen = desc->fip_dlen * FIP_BPW;
2841 if (dlen < sizeof(*desc) || dlen > rlen)
2842 return -EINVAL;
2843
2844 dtype = desc->fip_dtype;
2845 if (dtype < 32) {
2846 if (!(desc_mask & BIT(dtype))) {
2847 LIBFCOE_FIP_DBG(fip,
2848 "unexpected or duplicated desc "
2849 "desc type %u in "
2850 "FIP VN2VN subtype %u\n",
2851 dtype, sub);
2852 return -EINVAL;
2853 }
2854 desc_mask &= ~BIT(dtype);
2855 }
2856
2857 switch (dtype) {
2858 case FIP_DT_MAC:
2859 if (dlen != sizeof(struct fip_mac_desc))
2860 goto len_err;
2861 macd = (struct fip_mac_desc *)desc;
2862 if (!is_valid_ether_addr(macd->fd_mac)) {
2863 LIBFCOE_FIP_DBG(fip,
2864 "Invalid MAC addr %pM in FIP VN2VN\n",
2865 macd->fd_mac);
2866 return -EINVAL;
2867 }
2868 memcpy(frport->enode_mac, macd->fd_mac, ETH_ALEN);
2869 break;
2870 case FIP_DT_NAME:
2871 if (dlen != sizeof(struct fip_wwn_desc))
2872 goto len_err;
2873 wwn = (struct fip_wwn_desc *)desc;
2874 rdata->ids.node_name = get_unaligned_be64(&wwn->fd_wwn);
2875 break;
2876 default:
2877 LIBFCOE_FIP_DBG(fip, "unexpected descriptor type %x "
2878 "in FIP probe\n", dtype);
2879 /* standard says ignore unknown descriptors >= 128 */
2880 if (dtype < FIP_DT_NON_CRITICAL)
2881 return -EINVAL;
2882 break;
2883 }
2884 desc = (struct fip_desc *)((char *)desc + dlen);
2885 rlen -= dlen;
2886 }
2887 return 0;
2888
2889len_err:
2890 LIBFCOE_FIP_DBG(fip, "FIP length error in descriptor type %x len %zu\n",
2891 dtype, dlen);
2892 return -EINVAL;
2893}
2894
2895/**
2896 * fcoe_ctlr_vlan_send() - Send a FIP VLAN Notification
2897 * @fip: The FCoE controller
2898 * @sub: sub-opcode for vlan notification or vn2vn vlan notification
2899 * @dest: The destination Ethernet MAC address
2900 * @min_len: minimum size of the Ethernet payload to be sent
2901 */
2902static void fcoe_ctlr_vlan_send(struct fcoe_ctlr *fip,
2903 enum fip_vlan_subcode sub,
2904 const u8 *dest)
2905{
2906 struct sk_buff *skb;
2907 struct fip_vlan_notify_frame {
2908 struct ethhdr eth;
2909 struct fip_header fip;
2910 struct fip_mac_desc mac;
2911 struct fip_vlan_desc vlan;
2912 } __packed * frame;
2913 size_t len;
2914 size_t dlen;
2915
2916 len = sizeof(*frame);
2917 dlen = sizeof(frame->mac) + sizeof(frame->vlan);
2918 len = max(len, sizeof(struct ethhdr));
2919
2920 skb = dev_alloc_skb(len);
2921 if (!skb)
2922 return;
2923
2924 LIBFCOE_FIP_DBG(fip, "fip %s vlan notification, vlan %d\n",
2925 fip->mode == FIP_MODE_VN2VN ? "vn2vn" : "fcf",
2926 fip->lp->vlan);
2927
2928 frame = (struct fip_vlan_notify_frame *)skb->data;
2929 memset(frame, 0, len);
2930 memcpy(frame->eth.h_dest, dest, ETH_ALEN);
2931
2932 memcpy(frame->eth.h_source, fip->ctl_src_addr, ETH_ALEN);
2933 frame->eth.h_proto = htons(ETH_P_FIP);
2934
2935 frame->fip.fip_ver = FIP_VER_ENCAPS(FIP_VER);
2936 frame->fip.fip_op = htons(FIP_OP_VLAN);
2937 frame->fip.fip_subcode = sub;
2938 frame->fip.fip_dl_len = htons(dlen / FIP_BPW);
2939
2940 frame->mac.fd_desc.fip_dtype = FIP_DT_MAC;
2941 frame->mac.fd_desc.fip_dlen = sizeof(frame->mac) / FIP_BPW;
2942 memcpy(frame->mac.fd_mac, fip->ctl_src_addr, ETH_ALEN);
2943
2944 frame->vlan.fd_desc.fip_dtype = FIP_DT_VLAN;
2945 frame->vlan.fd_desc.fip_dlen = sizeof(frame->vlan) / FIP_BPW;
2946 put_unaligned_be16(fip->lp->vlan, &frame->vlan.fd_vlan);
2947
2948 skb_put(skb, len);
2949 skb->protocol = htons(ETH_P_FIP);
2950 skb->priority = fip->priority;
2951 skb_reset_mac_header(skb);
2952 skb_reset_network_header(skb);
2953
2954 fip->send(fip, skb);
2955}
2956
2957/**
2958 * fcoe_ctlr_vlan_disk_reply() - send FIP VLAN Discovery Notification.
2959 * @fip: The FCoE controller
2960 *
2961 * Called with ctlr_mutex held.
2962 */
2963static void fcoe_ctlr_vlan_disc_reply(struct fcoe_ctlr *fip,
2964 struct fc_rport_priv *rdata)
2965{
2966 struct fcoe_rport *frport = fcoe_ctlr_rport(rdata);
2967 enum fip_vlan_subcode sub = FIP_SC_VL_NOTE;
2968
2969 if (fip->mode == FIP_MODE_VN2VN)
2970 sub = FIP_SC_VL_VN2VN_NOTE;
2971
2972 fcoe_ctlr_vlan_send(fip, sub, frport->enode_mac);
2973}
2974
2975/**
2976 * fcoe_ctlr_vlan_recv - vlan request receive handler for VN2VN mode.
2977 * @lport: The local port
2978 * @fp: The received frame
2979 *
2980 */
2981static int fcoe_ctlr_vlan_recv(struct fcoe_ctlr *fip, struct sk_buff *skb)
2982{
2983 struct fip_header *fiph;
2984 enum fip_vlan_subcode sub;
2985 struct {
2986 struct fc_rport_priv rdata;
2987 struct fcoe_rport frport;
2988 } buf;
2989 int rc;
2990
2991 fiph = (struct fip_header *)skb->data;
2992 sub = fiph->fip_subcode;
2993 rc = fcoe_ctlr_vlan_parse(fip, skb, &buf.rdata);
2994 if (rc) {
2995 LIBFCOE_FIP_DBG(fip, "vlan_recv vlan_parse error %d\n", rc);
2996 goto drop;
2997 }
2998 mutex_lock(&fip->ctlr_mutex);
2999 if (sub == FIP_SC_VL_REQ)
3000 fcoe_ctlr_vlan_disc_reply(fip, &buf.rdata);
3001 mutex_unlock(&fip->ctlr_mutex);
3002
3003drop:
ea0a95d7 3004 kfree_skb(skb);
9a6cf881
HR
3005 return rc;
3006}
3007
e10f8c66
JE
3008/**
3009 * fcoe_ctlr_disc_recv - discovery receive handler for VN2VN mode.
92261156
JE
3010 * @lport: The local port
3011 * @fp: The received frame
e10f8c66
JE
3012 *
3013 * This should never be called since we don't see RSCNs or other
3014 * fabric-generated ELSes.
3015 */
92261156 3016static void fcoe_ctlr_disc_recv(struct fc_lport *lport, struct fc_frame *fp)
e10f8c66
JE
3017{
3018 struct fc_seq_els_data rjt_data;
3019
e10f8c66
JE
3020 rjt_data.reason = ELS_RJT_UNSUP;
3021 rjt_data.explan = ELS_EXPL_NONE;
7ab24dd1 3022 fc_seq_els_rsp_send(fp, ELS_LS_RJT, &rjt_data);
e10f8c66
JE
3023 fc_frame_free(fp);
3024}
3025
3026/**
3027 * fcoe_ctlr_disc_recv - start discovery for VN2VN mode.
3028 * @fip: The FCoE controller
3029 *
3030 * This sets a flag indicating that remote ports should be created
3031 * and started for the peers we discover. We use the disc_callback
3032 * pointer as that flag. Peers already discovered are created here.
3033 *
3034 * The lport lock is held during this call. The callback must be done
3035 * later, without holding either the lport or discovery locks.
3036 * The fcoe_ctlr lock may also be held during this call.
3037 */
3038static void fcoe_ctlr_disc_start(void (*callback)(struct fc_lport *,
3039 enum fc_disc_event),
3040 struct fc_lport *lport)
3041{
3042 struct fc_disc *disc = &lport->disc;
3043 struct fcoe_ctlr *fip = disc->priv;
3044
3045 mutex_lock(&disc->disc_mutex);
3046 disc->disc_callback = callback;
3047 disc->disc_id = (disc->disc_id + 2) | 1;
3048 disc->pending = 1;
3049 schedule_work(&fip->timer_work);
3050 mutex_unlock(&disc->disc_mutex);
3051}
3052
3053/**
3054 * fcoe_ctlr_vn_disc() - report FIP VN_port discovery results after claim state.
3055 * @fip: The FCoE controller
3056 *
3057 * Starts the FLOGI and PLOGI login process to each discovered rport for which
3058 * we've received at least one beacon.
3059 * Performs the discovery complete callback.
3060 */
3061static void fcoe_ctlr_vn_disc(struct fcoe_ctlr *fip)
3062{
3063 struct fc_lport *lport = fip->lp;
3064 struct fc_disc *disc = &lport->disc;
3065 struct fc_rport_priv *rdata;
3066 struct fcoe_rport *frport;
3067 void (*callback)(struct fc_lport *, enum fc_disc_event);
3068
3069 mutex_lock(&disc->disc_mutex);
3070 callback = disc->pending ? disc->disc_callback : NULL;
3071 disc->pending = 0;
3072 list_for_each_entry_rcu(rdata, &disc->rports, peers) {
a407c593
HR
3073 if (!kref_get_unless_zero(&rdata->kref))
3074 continue;
e10f8c66
JE
3075 frport = fcoe_ctlr_rport(rdata);
3076 if (frport->time)
05d7d3b0 3077 fc_rport_login(rdata);
944ef968 3078 kref_put(&rdata->kref, fc_rport_destroy);
e10f8c66 3079 }
0993ed92 3080 mutex_unlock(&disc->disc_mutex);
e10f8c66
JE
3081 if (callback)
3082 callback(lport, DISC_EV_SUCCESS);
3083}
3084
3085/**
3086 * fcoe_ctlr_vn_timeout - timer work function for VN2VN mode.
3087 * @fip: The FCoE controller
3088 */
3089static void fcoe_ctlr_vn_timeout(struct fcoe_ctlr *fip)
3090{
3091 unsigned long next_time;
3092 u8 mac[ETH_ALEN];
3093 u32 new_port_id = 0;
3094
3095 mutex_lock(&fip->ctlr_mutex);
3096 switch (fip->state) {
3097 case FIP_ST_VNMP_START:
3098 fcoe_ctlr_set_state(fip, FIP_ST_VNMP_PROBE1);
c9596550 3099 LIBFCOE_FIP_DBG(fip, "vn_timeout: send 1st probe request\n");
e10f8c66
JE
3100 fcoe_ctlr_vn_send(fip, FIP_SC_VN_PROBE_REQ, fcoe_all_vn2vn, 0);
3101 next_time = jiffies + msecs_to_jiffies(FIP_VN_PROBE_WAIT);
3102 break;
3103 case FIP_ST_VNMP_PROBE1:
3104 fcoe_ctlr_set_state(fip, FIP_ST_VNMP_PROBE2);
c9596550 3105 LIBFCOE_FIP_DBG(fip, "vn_timeout: send 2nd probe request\n");
e10f8c66
JE
3106 fcoe_ctlr_vn_send(fip, FIP_SC_VN_PROBE_REQ, fcoe_all_vn2vn, 0);
3107 next_time = jiffies + msecs_to_jiffies(FIP_VN_ANN_WAIT);
3108 break;
3109 case FIP_ST_VNMP_PROBE2:
3110 fcoe_ctlr_set_state(fip, FIP_ST_VNMP_CLAIM);
3111 new_port_id = fip->port_id;
3112 hton24(mac, FIP_VN_FC_MAP);
3113 hton24(mac + 3, new_port_id);
cd229e42 3114 fcoe_ctlr_map_dest(fip);
e10f8c66 3115 fip->update_mac(fip->lp, mac);
c9596550 3116 LIBFCOE_FIP_DBG(fip, "vn_timeout: send claim notify\n");
e10f8c66
JE
3117 fcoe_ctlr_vn_send_claim(fip);
3118 next_time = jiffies + msecs_to_jiffies(FIP_VN_ANN_WAIT);
3119 break;
3120 case FIP_ST_VNMP_CLAIM:
3121 /*
3122 * This may be invoked either by starting discovery so don't
3123 * go to the next state unless it's been long enough.
3124 */
3125 next_time = fip->sol_time + msecs_to_jiffies(FIP_VN_ANN_WAIT);
3126 if (time_after_eq(jiffies, next_time)) {
3127 fcoe_ctlr_set_state(fip, FIP_ST_VNMP_UP);
c9596550 3128 LIBFCOE_FIP_DBG(fip, "vn_timeout: send vn2vn beacon\n");
e10f8c66
JE
3129 fcoe_ctlr_vn_send(fip, FIP_SC_VN_BEACON,
3130 fcoe_all_vn2vn, 0);
3131 next_time = jiffies + msecs_to_jiffies(FIP_VN_ANN_WAIT);
3132 fip->port_ka_time = next_time;
3133 }
3134 fcoe_ctlr_vn_disc(fip);
3135 break;
3136 case FIP_ST_VNMP_UP:
3137 next_time = fcoe_ctlr_vn_age(fip);
3138 if (time_after_eq(jiffies, fip->port_ka_time)) {
c9596550 3139 LIBFCOE_FIP_DBG(fip, "vn_timeout: send vn2vn beacon\n");
e10f8c66
JE
3140 fcoe_ctlr_vn_send(fip, FIP_SC_VN_BEACON,
3141 fcoe_all_vn2vn, 0);
3142 fip->port_ka_time = jiffies +
3143 msecs_to_jiffies(FIP_VN_BEACON_INT +
3b60a64f 3144 (prandom_u32() % FIP_VN_BEACON_FUZZ));
e10f8c66
JE
3145 }
3146 if (time_before(fip->port_ka_time, next_time))
3147 next_time = fip->port_ka_time;
3148 break;
3149 case FIP_ST_LINK_WAIT:
3150 goto unlock;
3151 default:
11aa9900 3152 WARN(1, "unexpected state %d\n", fip->state);
e10f8c66
JE
3153 goto unlock;
3154 }
3155 mod_timer(&fip->timer, next_time);
3156unlock:
3157 mutex_unlock(&fip->ctlr_mutex);
3158
3159 /* If port ID is new, notify local port after dropping ctlr_mutex */
3160 if (new_port_id)
3161 fc_lport_set_local_id(fip->lp, new_port_id);
3162}
3163
0db0e377
RL
3164/**
3165 * fcoe_ctlr_mode_set() - Set or reset the ctlr's mode
3166 * @lport: The local port to be (re)configured
3167 * @fip: The FCoE controller whose mode is changing
3168 * @fip_mode: The new fip mode
3169 *
3170 * Note that the we shouldn't be changing the libfc discovery settings
3171 * (fc_disc_config) while an lport is going through the libfc state
3172 * machine. The mode can only be changed when a fcoe_ctlr device is
3173 * disabled, so that should ensure that this routine is only called
3174 * when nothing is happening.
3175 */
41463a88 3176static void fcoe_ctlr_mode_set(struct fc_lport *lport, struct fcoe_ctlr *fip,
1917d42d 3177 enum fip_mode fip_mode)
0db0e377
RL
3178{
3179 void *priv;
3180
3181 WARN_ON(lport->state != LPORT_ST_RESET &&
3182 lport->state != LPORT_ST_DISABLED);
3183
3184 if (fip_mode == FIP_MODE_VN2VN) {
3185 lport->rport_priv_size = sizeof(struct fcoe_rport);
3186 lport->point_to_multipoint = 1;
3187 lport->tt.disc_recv_req = fcoe_ctlr_disc_recv;
3188 lport->tt.disc_start = fcoe_ctlr_disc_start;
3189 lport->tt.disc_stop = fcoe_ctlr_disc_stop;
3190 lport->tt.disc_stop_final = fcoe_ctlr_disc_stop_final;
3191 priv = fip;
3192 } else {
3193 lport->rport_priv_size = 0;
3194 lport->point_to_multipoint = 0;
3195 lport->tt.disc_recv_req = NULL;
3196 lport->tt.disc_start = NULL;
3197 lport->tt.disc_stop = NULL;
3198 lport->tt.disc_stop_final = NULL;
3199 priv = lport;
3200 }
3201
3202 fc_disc_config(lport, priv);
3203}
3204
5e80f7f7 3205/**
70b51aab 3206 * fcoe_libfc_config() - Sets up libfc related properties for local port
8d55e507
RL
3207 * @lport: The local port to configure libfc for
3208 * @fip: The FCoE controller in use by the local port
3209 * @tt: The libfc function template
e10f8c66 3210 * @init_fcp: If non-zero, the FCP portion of libfc should be initialized
5e80f7f7
VD
3211 *
3212 * Returns : 0 for success
3213 */
e10f8c66
JE
3214int fcoe_libfc_config(struct fc_lport *lport, struct fcoe_ctlr *fip,
3215 const struct libfc_function_template *tt, int init_fcp)
5e80f7f7
VD
3216{
3217 /* Set the function pointers set by the LLDD */
70b51aab 3218 memcpy(&lport->tt, tt, sizeof(*tt));
e10f8c66 3219 if (init_fcp && fc_fcp_init(lport))
5e80f7f7 3220 return -ENOMEM;
70b51aab
RL
3221 fc_exch_init(lport);
3222 fc_elsct_init(lport);
3223 fc_lport_init(lport);
0807619d 3224 fc_disc_init(lport);
0db0e377 3225 fcoe_ctlr_mode_set(lport, fip, fip->mode);
5e80f7f7
VD
3226 return 0;
3227}
3228EXPORT_SYMBOL_GPL(fcoe_libfc_config);
8d55e507
RL
3229
3230void fcoe_fcf_get_selected(struct fcoe_fcf_device *fcf_dev)
3231{
3232 struct fcoe_ctlr_device *ctlr_dev = fcoe_fcf_dev_to_ctlr_dev(fcf_dev);
3233 struct fcoe_ctlr *fip = fcoe_ctlr_device_priv(ctlr_dev);
3234 struct fcoe_fcf *fcf;
3235
3236 mutex_lock(&fip->ctlr_mutex);
3237 mutex_lock(&ctlr_dev->lock);
3238
3239 fcf = fcoe_fcf_device_priv(fcf_dev);
3240 if (fcf)
3241 fcf_dev->selected = (fcf == fip->sel_fcf) ? 1 : 0;
3242 else
3243 fcf_dev->selected = 0;
3244
3245 mutex_unlock(&ctlr_dev->lock);
3246 mutex_unlock(&fip->ctlr_mutex);
3247}
3248EXPORT_SYMBOL(fcoe_fcf_get_selected);
3249
435c8667 3250void fcoe_ctlr_set_fip_mode(struct fcoe_ctlr_device *ctlr_dev)
8d55e507
RL
3251{
3252 struct fcoe_ctlr *ctlr = fcoe_ctlr_device_priv(ctlr_dev);
0db0e377 3253 struct fc_lport *lport = ctlr->lp;
8d55e507
RL
3254
3255 mutex_lock(&ctlr->ctlr_mutex);
435c8667
RL
3256 switch (ctlr_dev->mode) {
3257 case FIP_CONN_TYPE_VN2VN:
3258 ctlr->mode = FIP_MODE_VN2VN;
8d55e507 3259 break;
435c8667 3260 case FIP_CONN_TYPE_FABRIC:
8d55e507 3261 default:
435c8667 3262 ctlr->mode = FIP_MODE_FABRIC;
8d55e507
RL
3263 break;
3264 }
435c8667 3265
8d55e507 3266 mutex_unlock(&ctlr->ctlr_mutex);
0db0e377
RL
3267
3268 fcoe_ctlr_mode_set(lport, ctlr, ctlr->mode);
8d55e507 3269}
435c8667 3270EXPORT_SYMBOL(fcoe_ctlr_set_fip_mode);