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