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