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