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