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