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