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