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