GNU Linux-libre 4.14.259-gnu1
[releases.git] / drivers / scsi / fcoe / fcoe_ctlr.c
1 /*
2  * Copyright (c) 2008-2009 Cisco Systems, Inc.  All rights reserved.
3  * Copyright (c) 2009 Intel Corporation.  All rights reserved.
4  *
5  * This program is free software; you can redistribute it and/or modify it
6  * under the terms and conditions of the GNU General Public License,
7  * version 2, as published by the Free Software Foundation.
8  *
9  * This program is distributed in the hope it will be useful, but WITHOUT
10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
12  * more details.
13  *
14  * You should have received a copy of the GNU General Public License along with
15  * this program; if not, write to the Free Software Foundation, Inc.,
16  * 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
17  *
18  * Maintained at www.Open-FCoE.org
19  */
20
21 #include <linux/types.h>
22 #include <linux/module.h>
23 #include <linux/kernel.h>
24 #include <linux/list.h>
25 #include <linux/spinlock.h>
26 #include <linux/timer.h>
27 #include <linux/netdevice.h>
28 #include <linux/etherdevice.h>
29 #include <linux/ethtool.h>
30 #include <linux/if_ether.h>
31 #include <linux/if_vlan.h>
32 #include <linux/errno.h>
33 #include <linux/bitops.h>
34 #include <linux/slab.h>
35 #include <net/rtnetlink.h>
36
37 #include <scsi/fc/fc_els.h>
38 #include <scsi/fc/fc_fs.h>
39 #include <scsi/fc/fc_fip.h>
40 #include <scsi/fc/fc_encaps.h>
41 #include <scsi/fc/fc_fcoe.h>
42 #include <scsi/fc/fc_fcp.h>
43
44 #include <scsi/libfc.h>
45 #include <scsi/libfcoe.h>
46
47 #include "libfcoe.h"
48
49 #define FCOE_CTLR_MIN_FKA       500             /* min keep alive (mS) */
50 #define FCOE_CTLR_DEF_FKA       FIP_DEF_FKA     /* default keep alive (mS) */
51
52 static void fcoe_ctlr_timeout(unsigned long);
53 static void fcoe_ctlr_timer_work(struct work_struct *);
54 static void fcoe_ctlr_recv_work(struct work_struct *);
55 static int fcoe_ctlr_flogi_retry(struct fcoe_ctlr *);
56
57 static void fcoe_ctlr_vn_start(struct fcoe_ctlr *);
58 static int fcoe_ctlr_vn_recv(struct fcoe_ctlr *, struct sk_buff *);
59 static void fcoe_ctlr_vn_timeout(struct fcoe_ctlr *);
60 static int fcoe_ctlr_vn_lookup(struct fcoe_ctlr *, u32, u8 *);
61
62 static int fcoe_ctlr_vlan_recv(struct fcoe_ctlr *, struct sk_buff *);
63
64 static u8 fcoe_all_fcfs[ETH_ALEN] = FIP_ALL_FCF_MACS;
65 static u8 fcoe_all_enode[ETH_ALEN] = FIP_ALL_ENODE_MACS;
66 static u8 fcoe_all_vn2vn[ETH_ALEN] = FIP_ALL_VN2VN_MACS;
67 static u8 fcoe_all_p2p[ETH_ALEN] = FIP_ALL_P2P_MACS;
68
69 static const char * const fcoe_ctlr_states[] = {
70         [FIP_ST_DISABLED] =     "DISABLED",
71         [FIP_ST_LINK_WAIT] =    "LINK_WAIT",
72         [FIP_ST_AUTO] =         "AUTO",
73         [FIP_ST_NON_FIP] =      "NON_FIP",
74         [FIP_ST_ENABLED] =      "ENABLED",
75         [FIP_ST_VNMP_START] =   "VNMP_START",
76         [FIP_ST_VNMP_PROBE1] =  "VNMP_PROBE1",
77         [FIP_ST_VNMP_PROBE2] =  "VNMP_PROBE2",
78         [FIP_ST_VNMP_CLAIM] =   "VNMP_CLAIM",
79         [FIP_ST_VNMP_UP] =      "VNMP_UP",
80 };
81
82 static const char *fcoe_ctlr_state(enum fip_state state)
83 {
84         const char *cp = "unknown";
85
86         if (state < ARRAY_SIZE(fcoe_ctlr_states))
87                 cp = fcoe_ctlr_states[state];
88         if (!cp)
89                 cp = "unknown";
90         return cp;
91 }
92
93 /**
94  * fcoe_ctlr_set_state() - Set and do debug printing for the new FIP state.
95  * @fip: The FCoE controller
96  * @state: The new state
97  */
98 static void fcoe_ctlr_set_state(struct fcoe_ctlr *fip, enum fip_state state)
99 {
100         if (state == fip->state)
101                 return;
102         if (fip->lp)
103                 LIBFCOE_FIP_DBG(fip, "state %s -> %s\n",
104                         fcoe_ctlr_state(fip->state), fcoe_ctlr_state(state));
105         fip->state = state;
106 }
107
108 /**
109  * fcoe_ctlr_mtu_valid() - Check if a FCF's MTU is valid
110  * @fcf: The FCF to check
111  *
112  * Return non-zero if FCF fcoe_size has been validated.
113  */
114 static inline int fcoe_ctlr_mtu_valid(const struct fcoe_fcf *fcf)
115 {
116         return (fcf->flags & FIP_FL_SOL) != 0;
117 }
118
119 /**
120  * fcoe_ctlr_fcf_usable() - Check if a FCF is usable
121  * @fcf: The FCF to check
122  *
123  * Return non-zero if the FCF is usable.
124  */
125 static inline int fcoe_ctlr_fcf_usable(struct fcoe_fcf *fcf)
126 {
127         u16 flags = FIP_FL_SOL | FIP_FL_AVAIL;
128
129         return (fcf->flags & flags) == flags;
130 }
131
132 /**
133  * fcoe_ctlr_map_dest() - Set flag and OUI for mapping destination addresses
134  * @fip: The FCoE controller
135  */
136 static void fcoe_ctlr_map_dest(struct fcoe_ctlr *fip)
137 {
138         if (fip->mode == FIP_MODE_VN2VN)
139                 hton24(fip->dest_addr, FIP_VN_FC_MAP);
140         else
141                 hton24(fip->dest_addr, FIP_DEF_FC_MAP);
142         hton24(fip->dest_addr + 3, 0);
143         fip->map_dest = 1;
144 }
145
146 /**
147  * fcoe_ctlr_init() - Initialize the FCoE Controller instance
148  * @fip: The FCoE controller to initialize
149  */
150 void fcoe_ctlr_init(struct fcoe_ctlr *fip, enum fip_mode mode)
151 {
152         fcoe_ctlr_set_state(fip, FIP_ST_LINK_WAIT);
153         fip->mode = mode;
154         fip->fip_resp = false;
155         INIT_LIST_HEAD(&fip->fcfs);
156         mutex_init(&fip->ctlr_mutex);
157         spin_lock_init(&fip->ctlr_lock);
158         fip->flogi_oxid = FC_XID_UNKNOWN;
159         setup_timer(&fip->timer, fcoe_ctlr_timeout, (unsigned long)fip);
160         INIT_WORK(&fip->timer_work, fcoe_ctlr_timer_work);
161         INIT_WORK(&fip->recv_work, fcoe_ctlr_recv_work);
162         skb_queue_head_init(&fip->fip_recv_list);
163 }
164 EXPORT_SYMBOL(fcoe_ctlr_init);
165
166 /**
167  * fcoe_sysfs_fcf_add() - Add a fcoe_fcf{,_device} to a fcoe_ctlr{,_device}
168  * @new: The newly discovered FCF
169  *
170  * Called with fip->ctlr_mutex held
171  */
172 static int fcoe_sysfs_fcf_add(struct fcoe_fcf *new)
173 {
174         struct fcoe_ctlr *fip = new->fip;
175         struct fcoe_ctlr_device *ctlr_dev;
176         struct fcoe_fcf_device *temp, *fcf_dev;
177         int rc = -ENOMEM;
178
179         LIBFCOE_FIP_DBG(fip, "New FCF fab %16.16llx mac %pM\n",
180                         new->fabric_name, new->fcf_mac);
181
182         temp = kzalloc(sizeof(*temp), GFP_KERNEL);
183         if (!temp)
184                 goto out;
185
186         temp->fabric_name = new->fabric_name;
187         temp->switch_name = new->switch_name;
188         temp->fc_map = new->fc_map;
189         temp->vfid = new->vfid;
190         memcpy(temp->mac, new->fcf_mac, ETH_ALEN);
191         temp->priority = new->pri;
192         temp->fka_period = new->fka_period;
193         temp->selected = 0; /* default to unselected */
194
195         /*
196          * If ctlr_dev doesn't exist then it means we're a libfcoe user
197          * who doesn't use fcoe_syfs and didn't allocate a fcoe_ctlr_device.
198          * fnic would be an example of a driver with this behavior. In this
199          * case we want to add the fcoe_fcf to the fcoe_ctlr list, but we
200          * don't want to make sysfs changes.
201          */
202
203         ctlr_dev = fcoe_ctlr_to_ctlr_dev(fip);
204         if (ctlr_dev) {
205                 mutex_lock(&ctlr_dev->lock);
206                 fcf_dev = fcoe_fcf_device_add(ctlr_dev, temp);
207                 if (unlikely(!fcf_dev)) {
208                         rc = -ENOMEM;
209                         mutex_unlock(&ctlr_dev->lock);
210                         goto out;
211                 }
212
213                 /*
214                  * The fcoe_sysfs layer can return a CONNECTED fcf that
215                  * has a priv (fcf was never deleted) or a CONNECTED fcf
216                  * that doesn't have a priv (fcf was deleted). However,
217                  * libfcoe will always delete FCFs before trying to add
218                  * them. This is ensured because both recv_adv and
219                  * age_fcfs are protected by the the fcoe_ctlr's mutex.
220                  * This means that we should never get a FCF with a
221                  * non-NULL priv pointer.
222                  */
223                 BUG_ON(fcf_dev->priv);
224
225                 fcf_dev->priv = new;
226                 new->fcf_dev = fcf_dev;
227                 mutex_unlock(&ctlr_dev->lock);
228         }
229
230         list_add(&new->list, &fip->fcfs);
231         fip->fcf_count++;
232         rc = 0;
233
234 out:
235         kfree(temp);
236         return rc;
237 }
238
239 /**
240  * fcoe_sysfs_fcf_del() - Remove a fcoe_fcf{,_device} to a fcoe_ctlr{,_device}
241  * @new: The FCF to be removed
242  *
243  * Called with fip->ctlr_mutex held
244  */
245 static void fcoe_sysfs_fcf_del(struct fcoe_fcf *new)
246 {
247         struct fcoe_ctlr *fip = new->fip;
248         struct fcoe_ctlr_device *cdev;
249         struct fcoe_fcf_device *fcf_dev;
250
251         list_del(&new->list);
252         fip->fcf_count--;
253
254         /*
255          * If ctlr_dev doesn't exist then it means we're a libfcoe user
256          * who doesn't use fcoe_syfs and didn't allocate a fcoe_ctlr_device
257          * or a fcoe_fcf_device.
258          *
259          * fnic would be an example of a driver with this behavior. In this
260          * case we want to remove the fcoe_fcf from the fcoe_ctlr list (above),
261          * but we don't want to make sysfs changes.
262          */
263         cdev = fcoe_ctlr_to_ctlr_dev(fip);
264         if (cdev) {
265                 mutex_lock(&cdev->lock);
266                 fcf_dev = fcoe_fcf_to_fcf_dev(new);
267                 WARN_ON(!fcf_dev);
268                 new->fcf_dev = NULL;
269                 fcoe_fcf_device_delete(fcf_dev);
270                 mutex_unlock(&cdev->lock);
271         }
272         kfree(new);
273 }
274
275 /**
276  * fcoe_ctlr_reset_fcfs() - Reset and free all FCFs for a controller
277  * @fip: The FCoE controller whose FCFs are to be reset
278  *
279  * Called with &fcoe_ctlr lock held.
280  */
281 static void fcoe_ctlr_reset_fcfs(struct fcoe_ctlr *fip)
282 {
283         struct fcoe_fcf *fcf;
284         struct fcoe_fcf *next;
285
286         fip->sel_fcf = NULL;
287         list_for_each_entry_safe(fcf, next, &fip->fcfs, list) {
288                 fcoe_sysfs_fcf_del(fcf);
289         }
290         WARN_ON(fip->fcf_count);
291
292         fip->sel_time = 0;
293 }
294
295 /**
296  * fcoe_ctlr_destroy() - Disable and tear down a FCoE controller
297  * @fip: The FCoE controller to tear down
298  *
299  * This is called by FCoE drivers before freeing the &fcoe_ctlr.
300  *
301  * The receive handler will have been deleted before this to guarantee
302  * that no more recv_work will be scheduled.
303  *
304  * The timer routine will simply return once we set FIP_ST_DISABLED.
305  * This guarantees that no further timeouts or work will be scheduled.
306  */
307 void fcoe_ctlr_destroy(struct fcoe_ctlr *fip)
308 {
309         cancel_work_sync(&fip->recv_work);
310         skb_queue_purge(&fip->fip_recv_list);
311
312         mutex_lock(&fip->ctlr_mutex);
313         fcoe_ctlr_set_state(fip, FIP_ST_DISABLED);
314         fcoe_ctlr_reset_fcfs(fip);
315         mutex_unlock(&fip->ctlr_mutex);
316         del_timer_sync(&fip->timer);
317         cancel_work_sync(&fip->timer_work);
318 }
319 EXPORT_SYMBOL(fcoe_ctlr_destroy);
320
321 /**
322  * fcoe_ctlr_announce() - announce new FCF selection
323  * @fip: The FCoE controller
324  *
325  * Also sets the destination MAC for FCoE and control packets
326  *
327  * Called with neither ctlr_mutex nor ctlr_lock held.
328  */
329 static void fcoe_ctlr_announce(struct fcoe_ctlr *fip)
330 {
331         struct fcoe_fcf *sel;
332         struct fcoe_fcf *fcf;
333
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(sizeof(vp) * num_vlink_desc,
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(unsigned long arg)
1793 {
1794         struct fcoe_ctlr *fip = (struct fcoe_ctlr *)arg;
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[MAX_ADDR_LEN],
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         rcu_read_lock();
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         rcu_read_unlock();
2189         mutex_lock(&lport->disc.disc_mutex);
2190         lport->disc.disc_callback = NULL;
2191         mutex_unlock(&lport->disc.disc_mutex);
2192 }
2193
2194 /**
2195  * fcoe_ctlr_disc_stop() - stop discovery in VN2VN mode
2196  * @fip: The FCoE controller
2197  *
2198  * Called through the local port template for discovery.
2199  * Called without the ctlr_mutex held.
2200  */
2201 static void fcoe_ctlr_disc_stop(struct fc_lport *lport)
2202 {
2203         struct fcoe_ctlr *fip = lport->disc.priv;
2204
2205         mutex_lock(&fip->ctlr_mutex);
2206         fcoe_ctlr_disc_stop_locked(lport);
2207         mutex_unlock(&fip->ctlr_mutex);
2208 }
2209
2210 /**
2211  * fcoe_ctlr_disc_stop_final() - stop discovery for shutdown in VN2VN mode
2212  * @fip: The FCoE controller
2213  *
2214  * Called through the local port template for discovery.
2215  * Called without the ctlr_mutex held.
2216  */
2217 static void fcoe_ctlr_disc_stop_final(struct fc_lport *lport)
2218 {
2219         fcoe_ctlr_disc_stop(lport);
2220         fc_rport_flush_queue();
2221         synchronize_rcu();
2222 }
2223
2224 /**
2225  * fcoe_ctlr_vn_restart() - VN2VN probe restart with new port_id
2226  * @fip: The FCoE controller
2227  *
2228  * Called with fcoe_ctlr lock held.
2229  */
2230 static void fcoe_ctlr_vn_restart(struct fcoe_ctlr *fip)
2231 {
2232         unsigned long wait;
2233         u32 port_id;
2234
2235         fcoe_ctlr_disc_stop_locked(fip->lp);
2236
2237         /*
2238          * Get proposed port ID.
2239          * If this is the first try after link up, use any previous port_id.
2240          * If there was none, use the low bits of the port_name.
2241          * On subsequent tries, get the next random one.
2242          * Don't use reserved IDs, use another non-zero value, just as random.
2243          */
2244         port_id = fip->port_id;
2245         if (fip->probe_tries)
2246                 port_id = prandom_u32_state(&fip->rnd_state) & 0xffff;
2247         else if (!port_id)
2248                 port_id = fip->lp->wwpn & 0xffff;
2249         if (!port_id || port_id == 0xffff)
2250                 port_id = 1;
2251         fip->port_id = port_id;
2252
2253         if (fip->probe_tries < FIP_VN_RLIM_COUNT) {
2254                 fip->probe_tries++;
2255                 wait = prandom_u32() % FIP_VN_PROBE_WAIT;
2256         } else
2257                 wait = FIP_VN_RLIM_INT;
2258         mod_timer(&fip->timer, jiffies + msecs_to_jiffies(wait));
2259         fcoe_ctlr_set_state(fip, FIP_ST_VNMP_START);
2260 }
2261
2262 /**
2263  * fcoe_ctlr_vn_start() - Start in VN2VN mode
2264  * @fip: The FCoE controller
2265  *
2266  * Called with fcoe_ctlr lock held.
2267  */
2268 static void fcoe_ctlr_vn_start(struct fcoe_ctlr *fip)
2269 {
2270         fip->probe_tries = 0;
2271         prandom_seed_state(&fip->rnd_state, fip->lp->wwpn);
2272         fcoe_ctlr_vn_restart(fip);
2273 }
2274
2275 /**
2276  * fcoe_ctlr_vn_parse - parse probe request or response
2277  * @fip: The FCoE controller
2278  * @skb: incoming packet
2279  * @rdata: buffer for resulting parsed VN entry plus fcoe_rport
2280  *
2281  * Returns non-zero error number on error.
2282  * Does not consume the packet.
2283  */
2284 static int fcoe_ctlr_vn_parse(struct fcoe_ctlr *fip,
2285                               struct sk_buff *skb,
2286                               struct fcoe_rport *frport)
2287 {
2288         struct fip_header *fiph;
2289         struct fip_desc *desc = NULL;
2290         struct fip_mac_desc *macd = NULL;
2291         struct fip_wwn_desc *wwn = NULL;
2292         struct fip_vn_desc *vn = NULL;
2293         struct fip_size_desc *size = NULL;
2294         size_t rlen;
2295         size_t dlen;
2296         u32 desc_mask = 0;
2297         u32 dtype;
2298         u8 sub;
2299
2300         fiph = (struct fip_header *)skb->data;
2301         frport->flags = ntohs(fiph->fip_flags);
2302
2303         sub = fiph->fip_subcode;
2304         switch (sub) {
2305         case FIP_SC_VN_PROBE_REQ:
2306         case FIP_SC_VN_PROBE_REP:
2307         case FIP_SC_VN_BEACON:
2308                 desc_mask = BIT(FIP_DT_MAC) | BIT(FIP_DT_NAME) |
2309                             BIT(FIP_DT_VN_ID);
2310                 break;
2311         case FIP_SC_VN_CLAIM_NOTIFY:
2312         case FIP_SC_VN_CLAIM_REP:
2313                 desc_mask = BIT(FIP_DT_MAC) | BIT(FIP_DT_NAME) |
2314                             BIT(FIP_DT_VN_ID) | BIT(FIP_DT_FC4F) |
2315                             BIT(FIP_DT_FCOE_SIZE);
2316                 break;
2317         default:
2318                 LIBFCOE_FIP_DBG(fip, "vn_parse unknown subcode %u\n", sub);
2319                 return -EINVAL;
2320         }
2321
2322         rlen = ntohs(fiph->fip_dl_len) * 4;
2323         if (rlen + sizeof(*fiph) > skb->len)
2324                 return -EINVAL;
2325
2326         desc = (struct fip_desc *)(fiph + 1);
2327         while (rlen > 0) {
2328                 dlen = desc->fip_dlen * FIP_BPW;
2329                 if (dlen < sizeof(*desc) || dlen > rlen)
2330                         return -EINVAL;
2331
2332                 dtype = desc->fip_dtype;
2333                 if (dtype < 32) {
2334                         if (!(desc_mask & BIT(dtype))) {
2335                                 LIBFCOE_FIP_DBG(fip,
2336                                                 "unexpected or duplicated desc "
2337                                                 "desc type %u in "
2338                                                 "FIP VN2VN subtype %u\n",
2339                                                 dtype, sub);
2340                                 return -EINVAL;
2341                         }
2342                         desc_mask &= ~BIT(dtype);
2343                 }
2344
2345                 switch (dtype) {
2346                 case FIP_DT_MAC:
2347                         if (dlen != sizeof(struct fip_mac_desc))
2348                                 goto len_err;
2349                         macd = (struct fip_mac_desc *)desc;
2350                         if (!is_valid_ether_addr(macd->fd_mac)) {
2351                                 LIBFCOE_FIP_DBG(fip,
2352                                         "Invalid MAC addr %pM in FIP VN2VN\n",
2353                                          macd->fd_mac);
2354                                 return -EINVAL;
2355                         }
2356                         memcpy(frport->enode_mac, macd->fd_mac, ETH_ALEN);
2357                         break;
2358                 case FIP_DT_NAME:
2359                         if (dlen != sizeof(struct fip_wwn_desc))
2360                                 goto len_err;
2361                         wwn = (struct fip_wwn_desc *)desc;
2362                         frport->rdata.ids.node_name =
2363                                 get_unaligned_be64(&wwn->fd_wwn);
2364                         break;
2365                 case FIP_DT_VN_ID:
2366                         if (dlen != sizeof(struct fip_vn_desc))
2367                                 goto len_err;
2368                         vn = (struct fip_vn_desc *)desc;
2369                         memcpy(frport->vn_mac, vn->fd_mac, ETH_ALEN);
2370                         frport->rdata.ids.port_id = ntoh24(vn->fd_fc_id);
2371                         frport->rdata.ids.port_name =
2372                                 get_unaligned_be64(&vn->fd_wwpn);
2373                         break;
2374                 case FIP_DT_FC4F:
2375                         if (dlen != sizeof(struct fip_fc4_feat))
2376                                 goto len_err;
2377                         break;
2378                 case FIP_DT_FCOE_SIZE:
2379                         if (dlen != sizeof(struct fip_size_desc))
2380                                 goto len_err;
2381                         size = (struct fip_size_desc *)desc;
2382                         frport->fcoe_len = ntohs(size->fd_size);
2383                         break;
2384                 default:
2385                         LIBFCOE_FIP_DBG(fip, "unexpected descriptor type %x "
2386                                         "in FIP probe\n", dtype);
2387                         /* standard says ignore unknown descriptors >= 128 */
2388                         if (dtype < FIP_DT_NON_CRITICAL)
2389                                 return -EINVAL;
2390                         break;
2391                 }
2392                 desc = (struct fip_desc *)((char *)desc + dlen);
2393                 rlen -= dlen;
2394         }
2395         return 0;
2396
2397 len_err:
2398         LIBFCOE_FIP_DBG(fip, "FIP length error in descriptor type %x len %zu\n",
2399                         dtype, dlen);
2400         return -EINVAL;
2401 }
2402
2403 /**
2404  * fcoe_ctlr_vn_send_claim() - send multicast FIP VN2VN Claim Notification.
2405  * @fip: The FCoE controller
2406  *
2407  * Called with ctlr_mutex held.
2408  */
2409 static void fcoe_ctlr_vn_send_claim(struct fcoe_ctlr *fip)
2410 {
2411         fcoe_ctlr_vn_send(fip, FIP_SC_VN_CLAIM_NOTIFY, fcoe_all_vn2vn, 0);
2412         fip->sol_time = jiffies;
2413 }
2414
2415 /**
2416  * fcoe_ctlr_vn_probe_req() - handle incoming VN2VN probe request.
2417  * @fip: The FCoE controller
2418  * @rdata: parsed remote port with frport from the probe request
2419  *
2420  * Called with ctlr_mutex held.
2421  */
2422 static void fcoe_ctlr_vn_probe_req(struct fcoe_ctlr *fip,
2423                                    struct fc_rport_priv *rdata)
2424 {
2425         struct fcoe_rport *frport = fcoe_ctlr_rport(rdata);
2426
2427         if (rdata->ids.port_id != fip->port_id)
2428                 return;
2429
2430         switch (fip->state) {
2431         case FIP_ST_VNMP_CLAIM:
2432         case FIP_ST_VNMP_UP:
2433                 LIBFCOE_FIP_DBG(fip, "vn_probe_req: send reply, state %x\n",
2434                                 fip->state);
2435                 fcoe_ctlr_vn_send(fip, FIP_SC_VN_PROBE_REP,
2436                                   frport->enode_mac, 0);
2437                 break;
2438         case FIP_ST_VNMP_PROBE1:
2439         case FIP_ST_VNMP_PROBE2:
2440                 /*
2441                  * Decide whether to reply to the Probe.
2442                  * Our selected address is never a "recorded" one, so
2443                  * only reply if our WWPN is greater and the
2444                  * Probe's REC bit is not set.
2445                  * If we don't reply, we will change our address.
2446                  */
2447                 if (fip->lp->wwpn > rdata->ids.port_name &&
2448                     !(frport->flags & FIP_FL_REC_OR_P2P)) {
2449                         LIBFCOE_FIP_DBG(fip, "vn_probe_req: "
2450                                         "port_id collision\n");
2451                         fcoe_ctlr_vn_send(fip, FIP_SC_VN_PROBE_REP,
2452                                           frport->enode_mac, 0);
2453                         break;
2454                 }
2455                 /* fall through */
2456         case FIP_ST_VNMP_START:
2457                 LIBFCOE_FIP_DBG(fip, "vn_probe_req: "
2458                                 "restart VN2VN negotiation\n");
2459                 fcoe_ctlr_vn_restart(fip);
2460                 break;
2461         default:
2462                 LIBFCOE_FIP_DBG(fip, "vn_probe_req: ignore state %x\n",
2463                                 fip->state);
2464                 break;
2465         }
2466 }
2467
2468 /**
2469  * fcoe_ctlr_vn_probe_reply() - handle incoming VN2VN probe reply.
2470  * @fip: The FCoE controller
2471  * @rdata: parsed remote port with frport from the probe request
2472  *
2473  * Called with ctlr_mutex held.
2474  */
2475 static void fcoe_ctlr_vn_probe_reply(struct fcoe_ctlr *fip,
2476                                    struct fc_rport_priv *rdata)
2477 {
2478         if (rdata->ids.port_id != fip->port_id)
2479                 return;
2480         switch (fip->state) {
2481         case FIP_ST_VNMP_START:
2482         case FIP_ST_VNMP_PROBE1:
2483         case FIP_ST_VNMP_PROBE2:
2484         case FIP_ST_VNMP_CLAIM:
2485                 LIBFCOE_FIP_DBG(fip, "vn_probe_reply: restart state %x\n",
2486                                 fip->state);
2487                 fcoe_ctlr_vn_restart(fip);
2488                 break;
2489         case FIP_ST_VNMP_UP:
2490                 LIBFCOE_FIP_DBG(fip, "vn_probe_reply: send claim notify\n");
2491                 fcoe_ctlr_vn_send_claim(fip);
2492                 break;
2493         default:
2494                 break;
2495         }
2496 }
2497
2498 /**
2499  * fcoe_ctlr_vn_add() - Add a VN2VN entry to the list, based on a claim reply.
2500  * @fip: The FCoE controller
2501  * @new: newly-parsed remote port with frport as a template for new rdata
2502  *
2503  * Called with ctlr_mutex held.
2504  */
2505 static void fcoe_ctlr_vn_add(struct fcoe_ctlr *fip, struct fc_rport_priv *new)
2506 {
2507         struct fc_lport *lport = fip->lp;
2508         struct fc_rport_priv *rdata;
2509         struct fc_rport_identifiers *ids;
2510         struct fcoe_rport *frport;
2511         u32 port_id;
2512
2513         port_id = new->ids.port_id;
2514         if (port_id == fip->port_id)
2515                 return;
2516
2517         mutex_lock(&lport->disc.disc_mutex);
2518         rdata = fc_rport_create(lport, port_id);
2519         if (!rdata) {
2520                 mutex_unlock(&lport->disc.disc_mutex);
2521                 return;
2522         }
2523         mutex_lock(&rdata->rp_mutex);
2524         mutex_unlock(&lport->disc.disc_mutex);
2525
2526         rdata->ops = &fcoe_ctlr_vn_rport_ops;
2527         rdata->disc_id = lport->disc.disc_id;
2528
2529         ids = &rdata->ids;
2530         if ((ids->port_name != -1 && ids->port_name != new->ids.port_name) ||
2531             (ids->node_name != -1 && ids->node_name != new->ids.node_name)) {
2532                 mutex_unlock(&rdata->rp_mutex);
2533                 LIBFCOE_FIP_DBG(fip, "vn_add rport logoff %6.6x\n", port_id);
2534                 fc_rport_logoff(rdata);
2535                 mutex_lock(&rdata->rp_mutex);
2536         }
2537         ids->port_name = new->ids.port_name;
2538         ids->node_name = new->ids.node_name;
2539         mutex_unlock(&rdata->rp_mutex);
2540
2541         frport = fcoe_ctlr_rport(rdata);
2542         LIBFCOE_FIP_DBG(fip, "vn_add rport %6.6x %s state %d\n",
2543                         port_id, frport->fcoe_len ? "old" : "new",
2544                         rdata->rp_state);
2545         *frport = *fcoe_ctlr_rport(new);
2546         frport->time = 0;
2547 }
2548
2549 /**
2550  * fcoe_ctlr_vn_lookup() - Find VN remote port's MAC address
2551  * @fip: The FCoE controller
2552  * @port_id:  The port_id of the remote VN_node
2553  * @mac: buffer which will hold the VN_NODE destination MAC address, if found.
2554  *
2555  * Returns non-zero error if no remote port found.
2556  */
2557 static int fcoe_ctlr_vn_lookup(struct fcoe_ctlr *fip, u32 port_id, u8 *mac)
2558 {
2559         struct fc_lport *lport = fip->lp;
2560         struct fc_rport_priv *rdata;
2561         struct fcoe_rport *frport;
2562         int ret = -1;
2563
2564         rdata = fc_rport_lookup(lport, port_id);
2565         if (rdata) {
2566                 frport = fcoe_ctlr_rport(rdata);
2567                 memcpy(mac, frport->enode_mac, ETH_ALEN);
2568                 ret = 0;
2569                 kref_put(&rdata->kref, fc_rport_destroy);
2570         }
2571         return ret;
2572 }
2573
2574 /**
2575  * fcoe_ctlr_vn_claim_notify() - handle received FIP VN2VN Claim Notification
2576  * @fip: The FCoE controller
2577  * @new: newly-parsed remote port with frport as a template for new rdata
2578  *
2579  * Called with ctlr_mutex held.
2580  */
2581 static void fcoe_ctlr_vn_claim_notify(struct fcoe_ctlr *fip,
2582                                       struct fc_rport_priv *new)
2583 {
2584         struct fcoe_rport *frport = fcoe_ctlr_rport(new);
2585
2586         if (frport->flags & FIP_FL_REC_OR_P2P) {
2587                 LIBFCOE_FIP_DBG(fip, "send probe req for P2P/REC\n");
2588                 fcoe_ctlr_vn_send(fip, FIP_SC_VN_PROBE_REQ, fcoe_all_vn2vn, 0);
2589                 return;
2590         }
2591         switch (fip->state) {
2592         case FIP_ST_VNMP_START:
2593         case FIP_ST_VNMP_PROBE1:
2594         case FIP_ST_VNMP_PROBE2:
2595                 if (new->ids.port_id == fip->port_id) {
2596                         LIBFCOE_FIP_DBG(fip, "vn_claim_notify: "
2597                                         "restart, state %d\n",
2598                                         fip->state);
2599                         fcoe_ctlr_vn_restart(fip);
2600                 }
2601                 break;
2602         case FIP_ST_VNMP_CLAIM:
2603         case FIP_ST_VNMP_UP:
2604                 if (new->ids.port_id == fip->port_id) {
2605                         if (new->ids.port_name > fip->lp->wwpn) {
2606                                 LIBFCOE_FIP_DBG(fip, "vn_claim_notify: "
2607                                                 "restart, port_id collision\n");
2608                                 fcoe_ctlr_vn_restart(fip);
2609                                 break;
2610                         }
2611                         LIBFCOE_FIP_DBG(fip, "vn_claim_notify: "
2612                                         "send claim notify\n");
2613                         fcoe_ctlr_vn_send_claim(fip);
2614                         break;
2615                 }
2616                 LIBFCOE_FIP_DBG(fip, "vn_claim_notify: send reply to %x\n",
2617                                 new->ids.port_id);
2618                 fcoe_ctlr_vn_send(fip, FIP_SC_VN_CLAIM_REP, frport->enode_mac,
2619                                   min((u32)frport->fcoe_len,
2620                                       fcoe_ctlr_fcoe_size(fip)));
2621                 fcoe_ctlr_vn_add(fip, new);
2622                 break;
2623         default:
2624                 LIBFCOE_FIP_DBG(fip, "vn_claim_notify: "
2625                                 "ignoring claim from %x\n", new->ids.port_id);
2626                 break;
2627         }
2628 }
2629
2630 /**
2631  * fcoe_ctlr_vn_claim_resp() - handle received Claim Response
2632  * @fip: The FCoE controller that received the frame
2633  * @new: newly-parsed remote port with frport from the Claim Response
2634  *
2635  * Called with ctlr_mutex held.
2636  */
2637 static void fcoe_ctlr_vn_claim_resp(struct fcoe_ctlr *fip,
2638                                     struct fc_rport_priv *new)
2639 {
2640         LIBFCOE_FIP_DBG(fip, "claim resp from from rport %x - state %s\n",
2641                         new->ids.port_id, fcoe_ctlr_state(fip->state));
2642         if (fip->state == FIP_ST_VNMP_UP || fip->state == FIP_ST_VNMP_CLAIM)
2643                 fcoe_ctlr_vn_add(fip, new);
2644 }
2645
2646 /**
2647  * fcoe_ctlr_vn_beacon() - handle received beacon.
2648  * @fip: The FCoE controller that received the frame
2649  * @new: newly-parsed remote port with frport from the Beacon
2650  *
2651  * Called with ctlr_mutex held.
2652  */
2653 static void fcoe_ctlr_vn_beacon(struct fcoe_ctlr *fip,
2654                                 struct fc_rport_priv *new)
2655 {
2656         struct fc_lport *lport = fip->lp;
2657         struct fc_rport_priv *rdata;
2658         struct fcoe_rport *frport;
2659
2660         frport = fcoe_ctlr_rport(new);
2661         if (frport->flags & FIP_FL_REC_OR_P2P) {
2662                 LIBFCOE_FIP_DBG(fip, "p2p beacon while in vn2vn mode\n");
2663                 fcoe_ctlr_vn_send(fip, FIP_SC_VN_PROBE_REQ, fcoe_all_vn2vn, 0);
2664                 return;
2665         }
2666         rdata = fc_rport_lookup(lport, new->ids.port_id);
2667         if (rdata) {
2668                 if (rdata->ids.node_name == new->ids.node_name &&
2669                     rdata->ids.port_name == new->ids.port_name) {
2670                         frport = fcoe_ctlr_rport(rdata);
2671                         LIBFCOE_FIP_DBG(fip, "beacon from rport %x\n",
2672                                         rdata->ids.port_id);
2673                         if (!frport->time && fip->state == FIP_ST_VNMP_UP) {
2674                                 LIBFCOE_FIP_DBG(fip, "beacon expired "
2675                                                 "for rport %x\n",
2676                                                 rdata->ids.port_id);
2677                                 fc_rport_login(rdata);
2678                         }
2679                         frport->time = jiffies;
2680                 }
2681                 kref_put(&rdata->kref, fc_rport_destroy);
2682                 return;
2683         }
2684         if (fip->state != FIP_ST_VNMP_UP)
2685                 return;
2686
2687         /*
2688          * Beacon from a new neighbor.
2689          * Send a claim notify if one hasn't been sent recently.
2690          * Don't add the neighbor yet.
2691          */
2692         LIBFCOE_FIP_DBG(fip, "beacon from new rport %x. sending claim notify\n",
2693                         new->ids.port_id);
2694         if (time_after(jiffies,
2695                        fip->sol_time + msecs_to_jiffies(FIP_VN_ANN_WAIT)))
2696                 fcoe_ctlr_vn_send_claim(fip);
2697 }
2698
2699 /**
2700  * fcoe_ctlr_vn_age() - Check for VN_ports without recent beacons
2701  * @fip: The FCoE controller
2702  *
2703  * Called with ctlr_mutex held.
2704  * Called only in state FIP_ST_VNMP_UP.
2705  * Returns the soonest time for next age-out or a time far in the future.
2706  */
2707 static unsigned long fcoe_ctlr_vn_age(struct fcoe_ctlr *fip)
2708 {
2709         struct fc_lport *lport = fip->lp;
2710         struct fc_rport_priv *rdata;
2711         struct fcoe_rport *frport;
2712         unsigned long next_time;
2713         unsigned long deadline;
2714
2715         next_time = jiffies + msecs_to_jiffies(FIP_VN_BEACON_INT * 10);
2716         rcu_read_lock();
2717         list_for_each_entry_rcu(rdata, &lport->disc.rports, peers) {
2718                 if (!kref_get_unless_zero(&rdata->kref))
2719                         continue;
2720                 frport = fcoe_ctlr_rport(rdata);
2721                 if (!frport->time) {
2722                         kref_put(&rdata->kref, fc_rport_destroy);
2723                         continue;
2724                 }
2725                 deadline = frport->time +
2726                            msecs_to_jiffies(FIP_VN_BEACON_INT * 25 / 10);
2727                 if (time_after_eq(jiffies, deadline)) {
2728                         frport->time = 0;
2729                         LIBFCOE_FIP_DBG(fip,
2730                                 "port %16.16llx fc_id %6.6x beacon expired\n",
2731                                 rdata->ids.port_name, rdata->ids.port_id);
2732                         fc_rport_logoff(rdata);
2733                 } else if (time_before(deadline, next_time))
2734                         next_time = deadline;
2735                 kref_put(&rdata->kref, fc_rport_destroy);
2736         }
2737         rcu_read_unlock();
2738         return next_time;
2739 }
2740
2741 /**
2742  * fcoe_ctlr_vn_recv() - Receive a FIP frame
2743  * @fip: The FCoE controller that received the frame
2744  * @skb: The received FIP frame
2745  *
2746  * Returns non-zero if the frame is dropped.
2747  * Always consumes the frame.
2748  */
2749 static int fcoe_ctlr_vn_recv(struct fcoe_ctlr *fip, struct sk_buff *skb)
2750 {
2751         struct fip_header *fiph;
2752         enum fip_vn2vn_subcode sub;
2753         struct fcoe_rport frport = { };
2754         int rc, vlan_id = 0;
2755
2756         fiph = (struct fip_header *)skb->data;
2757         sub = fiph->fip_subcode;
2758
2759         if (fip->lp->vlan)
2760                 vlan_id = skb_vlan_tag_get_id(skb);
2761
2762         if (vlan_id && vlan_id != fip->lp->vlan) {
2763                 LIBFCOE_FIP_DBG(fip, "vn_recv drop frame sub %x vlan %d\n",
2764                                 sub, vlan_id);
2765                 rc = -EAGAIN;
2766                 goto drop;
2767         }
2768
2769         rc = fcoe_ctlr_vn_parse(fip, skb, &frport);
2770         if (rc) {
2771                 LIBFCOE_FIP_DBG(fip, "vn_recv vn_parse error %d\n", rc);
2772                 goto drop;
2773         }
2774
2775         mutex_lock(&fip->ctlr_mutex);
2776         switch (sub) {
2777         case FIP_SC_VN_PROBE_REQ:
2778                 fcoe_ctlr_vn_probe_req(fip, &frport.rdata);
2779                 break;
2780         case FIP_SC_VN_PROBE_REP:
2781                 fcoe_ctlr_vn_probe_reply(fip, &frport.rdata);
2782                 break;
2783         case FIP_SC_VN_CLAIM_NOTIFY:
2784                 fcoe_ctlr_vn_claim_notify(fip, &frport.rdata);
2785                 break;
2786         case FIP_SC_VN_CLAIM_REP:
2787                 fcoe_ctlr_vn_claim_resp(fip, &frport.rdata);
2788                 break;
2789         case FIP_SC_VN_BEACON:
2790                 fcoe_ctlr_vn_beacon(fip, &frport.rdata);
2791                 break;
2792         default:
2793                 LIBFCOE_FIP_DBG(fip, "vn_recv unknown subcode %d\n", sub);
2794                 rc = -1;
2795                 break;
2796         }
2797         mutex_unlock(&fip->ctlr_mutex);
2798 drop:
2799         kfree_skb(skb);
2800         return rc;
2801 }
2802
2803 /**
2804  * fcoe_ctlr_vlan_parse - parse vlan discovery request or response
2805  * @fip: The FCoE controller
2806  * @skb: incoming packet
2807  * @rdata: buffer for resulting parsed VLAN entry plus fcoe_rport
2808  *
2809  * Returns non-zero error number on error.
2810  * Does not consume the packet.
2811  */
2812 static int fcoe_ctlr_vlan_parse(struct fcoe_ctlr *fip,
2813                               struct sk_buff *skb,
2814                               struct fcoe_rport *frport)
2815 {
2816         struct fip_header *fiph;
2817         struct fip_desc *desc = NULL;
2818         struct fip_mac_desc *macd = NULL;
2819         struct fip_wwn_desc *wwn = NULL;
2820         size_t rlen;
2821         size_t dlen;
2822         u32 desc_mask = 0;
2823         u32 dtype;
2824         u8 sub;
2825
2826         fiph = (struct fip_header *)skb->data;
2827         frport->flags = ntohs(fiph->fip_flags);
2828
2829         sub = fiph->fip_subcode;
2830         switch (sub) {
2831         case FIP_SC_VL_REQ:
2832                 desc_mask = BIT(FIP_DT_MAC) | BIT(FIP_DT_NAME);
2833                 break;
2834         default:
2835                 LIBFCOE_FIP_DBG(fip, "vn_parse unknown subcode %u\n", sub);
2836                 return -EINVAL;
2837         }
2838
2839         rlen = ntohs(fiph->fip_dl_len) * 4;
2840         if (rlen + sizeof(*fiph) > skb->len)
2841                 return -EINVAL;
2842
2843         desc = (struct fip_desc *)(fiph + 1);
2844         while (rlen > 0) {
2845                 dlen = desc->fip_dlen * FIP_BPW;
2846                 if (dlen < sizeof(*desc) || dlen > rlen)
2847                         return -EINVAL;
2848
2849                 dtype = desc->fip_dtype;
2850                 if (dtype < 32) {
2851                         if (!(desc_mask & BIT(dtype))) {
2852                                 LIBFCOE_FIP_DBG(fip,
2853                                                 "unexpected or duplicated desc "
2854                                                 "desc type %u in "
2855                                                 "FIP VN2VN subtype %u\n",
2856                                                 dtype, sub);
2857                                 return -EINVAL;
2858                         }
2859                         desc_mask &= ~BIT(dtype);
2860                 }
2861
2862                 switch (dtype) {
2863                 case FIP_DT_MAC:
2864                         if (dlen != sizeof(struct fip_mac_desc))
2865                                 goto len_err;
2866                         macd = (struct fip_mac_desc *)desc;
2867                         if (!is_valid_ether_addr(macd->fd_mac)) {
2868                                 LIBFCOE_FIP_DBG(fip,
2869                                         "Invalid MAC addr %pM in FIP VN2VN\n",
2870                                          macd->fd_mac);
2871                                 return -EINVAL;
2872                         }
2873                         memcpy(frport->enode_mac, macd->fd_mac, ETH_ALEN);
2874                         break;
2875                 case FIP_DT_NAME:
2876                         if (dlen != sizeof(struct fip_wwn_desc))
2877                                 goto len_err;
2878                         wwn = (struct fip_wwn_desc *)desc;
2879                         frport->rdata.ids.node_name =
2880                                 get_unaligned_be64(&wwn->fd_wwn);
2881                         break;
2882                 default:
2883                         LIBFCOE_FIP_DBG(fip, "unexpected descriptor type %x "
2884                                         "in FIP probe\n", dtype);
2885                         /* standard says ignore unknown descriptors >= 128 */
2886                         if (dtype < FIP_DT_NON_CRITICAL)
2887                                 return -EINVAL;
2888                         break;
2889                 }
2890                 desc = (struct fip_desc *)((char *)desc + dlen);
2891                 rlen -= dlen;
2892         }
2893         return 0;
2894
2895 len_err:
2896         LIBFCOE_FIP_DBG(fip, "FIP length error in descriptor type %x len %zu\n",
2897                         dtype, dlen);
2898         return -EINVAL;
2899 }
2900
2901 /**
2902  * fcoe_ctlr_vlan_send() - Send a FIP VLAN Notification
2903  * @fip: The FCoE controller
2904  * @sub: sub-opcode for vlan notification or vn2vn vlan notification
2905  * @dest: The destination Ethernet MAC address
2906  * @min_len: minimum size of the Ethernet payload to be sent
2907  */
2908 static void fcoe_ctlr_vlan_send(struct fcoe_ctlr *fip,
2909                               enum fip_vlan_subcode sub,
2910                               const u8 *dest)
2911 {
2912         struct sk_buff *skb;
2913         struct fip_vlan_notify_frame {
2914                 struct ethhdr eth;
2915                 struct fip_header fip;
2916                 struct fip_mac_desc mac;
2917                 struct fip_vlan_desc vlan;
2918         } __packed * frame;
2919         size_t len;
2920         size_t dlen;
2921
2922         len = sizeof(*frame);
2923         dlen = sizeof(frame->mac) + sizeof(frame->vlan);
2924         len = max(len, sizeof(struct ethhdr));
2925
2926         skb = dev_alloc_skb(len);
2927         if (!skb)
2928                 return;
2929
2930         LIBFCOE_FIP_DBG(fip, "fip %s vlan notification, vlan %d\n",
2931                         fip->mode == FIP_MODE_VN2VN ? "vn2vn" : "fcf",
2932                         fip->lp->vlan);
2933
2934         frame = (struct fip_vlan_notify_frame *)skb->data;
2935         memset(frame, 0, len);
2936         memcpy(frame->eth.h_dest, dest, ETH_ALEN);
2937
2938         memcpy(frame->eth.h_source, fip->ctl_src_addr, ETH_ALEN);
2939         frame->eth.h_proto = htons(ETH_P_FIP);
2940
2941         frame->fip.fip_ver = FIP_VER_ENCAPS(FIP_VER);
2942         frame->fip.fip_op = htons(FIP_OP_VLAN);
2943         frame->fip.fip_subcode = sub;
2944         frame->fip.fip_dl_len = htons(dlen / FIP_BPW);
2945
2946         frame->mac.fd_desc.fip_dtype = FIP_DT_MAC;
2947         frame->mac.fd_desc.fip_dlen = sizeof(frame->mac) / FIP_BPW;
2948         memcpy(frame->mac.fd_mac, fip->ctl_src_addr, ETH_ALEN);
2949
2950         frame->vlan.fd_desc.fip_dtype = FIP_DT_VLAN;
2951         frame->vlan.fd_desc.fip_dlen = sizeof(frame->vlan) / FIP_BPW;
2952         put_unaligned_be16(fip->lp->vlan, &frame->vlan.fd_vlan);
2953
2954         skb_put(skb, len);
2955         skb->protocol = htons(ETH_P_FIP);
2956         skb->priority = fip->priority;
2957         skb_reset_mac_header(skb);
2958         skb_reset_network_header(skb);
2959
2960         fip->send(fip, skb);
2961 }
2962
2963 /**
2964  * fcoe_ctlr_vlan_disk_reply() - send FIP VLAN Discovery Notification.
2965  * @fip: The FCoE controller
2966  *
2967  * Called with ctlr_mutex held.
2968  */
2969 static void fcoe_ctlr_vlan_disc_reply(struct fcoe_ctlr *fip,
2970                                       struct fc_rport_priv *rdata)
2971 {
2972         struct fcoe_rport *frport = fcoe_ctlr_rport(rdata);
2973         enum fip_vlan_subcode sub = FIP_SC_VL_NOTE;
2974
2975         if (fip->mode == FIP_MODE_VN2VN)
2976                 sub = FIP_SC_VL_VN2VN_NOTE;
2977
2978         fcoe_ctlr_vlan_send(fip, sub, frport->enode_mac);
2979 }
2980
2981 /**
2982  * fcoe_ctlr_vlan_recv - vlan request receive handler for VN2VN mode.
2983  * @lport: The local port
2984  * @fp: The received frame
2985  *
2986  */
2987 static int fcoe_ctlr_vlan_recv(struct fcoe_ctlr *fip, struct sk_buff *skb)
2988 {
2989         struct fip_header *fiph;
2990         enum fip_vlan_subcode sub;
2991         struct fcoe_rport frport = { };
2992         int rc;
2993
2994         fiph = (struct fip_header *)skb->data;
2995         sub = fiph->fip_subcode;
2996         rc = fcoe_ctlr_vlan_parse(fip, skb, &frport);
2997         if (rc) {
2998                 LIBFCOE_FIP_DBG(fip, "vlan_recv vlan_parse error %d\n", rc);
2999                 goto drop;
3000         }
3001         mutex_lock(&fip->ctlr_mutex);
3002         if (sub == FIP_SC_VL_REQ)
3003                 fcoe_ctlr_vlan_disc_reply(fip, &frport.rdata);
3004         mutex_unlock(&fip->ctlr_mutex);
3005
3006 drop:
3007         kfree_skb(skb);
3008         return rc;
3009 }
3010
3011 /**
3012  * fcoe_ctlr_disc_recv - discovery receive handler for VN2VN mode.
3013  * @lport: The local port
3014  * @fp: The received frame
3015  *
3016  * This should never be called since we don't see RSCNs or other
3017  * fabric-generated ELSes.
3018  */
3019 static void fcoe_ctlr_disc_recv(struct fc_lport *lport, struct fc_frame *fp)
3020 {
3021         struct fc_seq_els_data rjt_data;
3022
3023         rjt_data.reason = ELS_RJT_UNSUP;
3024         rjt_data.explan = ELS_EXPL_NONE;
3025         fc_seq_els_rsp_send(fp, ELS_LS_RJT, &rjt_data);
3026         fc_frame_free(fp);
3027 }
3028
3029 /**
3030  * fcoe_ctlr_disc_recv - start discovery for VN2VN mode.
3031  * @fip: The FCoE controller
3032  *
3033  * This sets a flag indicating that remote ports should be created
3034  * and started for the peers we discover.  We use the disc_callback
3035  * pointer as that flag.  Peers already discovered are created here.
3036  *
3037  * The lport lock is held during this call. The callback must be done
3038  * later, without holding either the lport or discovery locks.
3039  * The fcoe_ctlr lock may also be held during this call.
3040  */
3041 static void fcoe_ctlr_disc_start(void (*callback)(struct fc_lport *,
3042                                                   enum fc_disc_event),
3043                                  struct fc_lport *lport)
3044 {
3045         struct fc_disc *disc = &lport->disc;
3046         struct fcoe_ctlr *fip = disc->priv;
3047
3048         mutex_lock(&disc->disc_mutex);
3049         disc->disc_callback = callback;
3050         disc->disc_id = (disc->disc_id + 2) | 1;
3051         disc->pending = 1;
3052         schedule_work(&fip->timer_work);
3053         mutex_unlock(&disc->disc_mutex);
3054 }
3055
3056 /**
3057  * fcoe_ctlr_vn_disc() - report FIP VN_port discovery results after claim state.
3058  * @fip: The FCoE controller
3059  *
3060  * Starts the FLOGI and PLOGI login process to each discovered rport for which
3061  * we've received at least one beacon.
3062  * Performs the discovery complete callback.
3063  */
3064 static void fcoe_ctlr_vn_disc(struct fcoe_ctlr *fip)
3065 {
3066         struct fc_lport *lport = fip->lp;
3067         struct fc_disc *disc = &lport->disc;
3068         struct fc_rport_priv *rdata;
3069         struct fcoe_rport *frport;
3070         void (*callback)(struct fc_lport *, enum fc_disc_event);
3071
3072         mutex_lock(&disc->disc_mutex);
3073         callback = disc->pending ? disc->disc_callback : NULL;
3074         disc->pending = 0;
3075         mutex_unlock(&disc->disc_mutex);
3076         rcu_read_lock();
3077         list_for_each_entry_rcu(rdata, &disc->rports, peers) {
3078                 if (!kref_get_unless_zero(&rdata->kref))
3079                         continue;
3080                 frport = fcoe_ctlr_rport(rdata);
3081                 if (frport->time)
3082                         fc_rport_login(rdata);
3083                 kref_put(&rdata->kref, fc_rport_destroy);
3084         }
3085         rcu_read_unlock();
3086         if (callback)
3087                 callback(lport, DISC_EV_SUCCESS);
3088 }
3089
3090 /**
3091  * fcoe_ctlr_vn_timeout - timer work function for VN2VN mode.
3092  * @fip: The FCoE controller
3093  */
3094 static void fcoe_ctlr_vn_timeout(struct fcoe_ctlr *fip)
3095 {
3096         unsigned long next_time;
3097         u8 mac[ETH_ALEN];
3098         u32 new_port_id = 0;
3099
3100         mutex_lock(&fip->ctlr_mutex);
3101         switch (fip->state) {
3102         case FIP_ST_VNMP_START:
3103                 fcoe_ctlr_set_state(fip, FIP_ST_VNMP_PROBE1);
3104                 LIBFCOE_FIP_DBG(fip, "vn_timeout: send 1st probe request\n");
3105                 fcoe_ctlr_vn_send(fip, FIP_SC_VN_PROBE_REQ, fcoe_all_vn2vn, 0);
3106                 next_time = jiffies + msecs_to_jiffies(FIP_VN_PROBE_WAIT);
3107                 break;
3108         case FIP_ST_VNMP_PROBE1:
3109                 fcoe_ctlr_set_state(fip, FIP_ST_VNMP_PROBE2);
3110                 LIBFCOE_FIP_DBG(fip, "vn_timeout: send 2nd probe request\n");
3111                 fcoe_ctlr_vn_send(fip, FIP_SC_VN_PROBE_REQ, fcoe_all_vn2vn, 0);
3112                 next_time = jiffies + msecs_to_jiffies(FIP_VN_ANN_WAIT);
3113                 break;
3114         case FIP_ST_VNMP_PROBE2:
3115                 fcoe_ctlr_set_state(fip, FIP_ST_VNMP_CLAIM);
3116                 new_port_id = fip->port_id;
3117                 hton24(mac, FIP_VN_FC_MAP);
3118                 hton24(mac + 3, new_port_id);
3119                 fcoe_ctlr_map_dest(fip);
3120                 fip->update_mac(fip->lp, mac);
3121                 LIBFCOE_FIP_DBG(fip, "vn_timeout: send claim notify\n");
3122                 fcoe_ctlr_vn_send_claim(fip);
3123                 next_time = jiffies + msecs_to_jiffies(FIP_VN_ANN_WAIT);
3124                 break;
3125         case FIP_ST_VNMP_CLAIM:
3126                 /*
3127                  * This may be invoked either by starting discovery so don't
3128                  * go to the next state unless it's been long enough.
3129                  */
3130                 next_time = fip->sol_time + msecs_to_jiffies(FIP_VN_ANN_WAIT);
3131                 if (time_after_eq(jiffies, next_time)) {
3132                         fcoe_ctlr_set_state(fip, FIP_ST_VNMP_UP);
3133                         LIBFCOE_FIP_DBG(fip, "vn_timeout: send vn2vn beacon\n");
3134                         fcoe_ctlr_vn_send(fip, FIP_SC_VN_BEACON,
3135                                           fcoe_all_vn2vn, 0);
3136                         next_time = jiffies + msecs_to_jiffies(FIP_VN_ANN_WAIT);
3137                         fip->port_ka_time = next_time;
3138                 }
3139                 fcoe_ctlr_vn_disc(fip);
3140                 break;
3141         case FIP_ST_VNMP_UP:
3142                 next_time = fcoe_ctlr_vn_age(fip);
3143                 if (time_after_eq(jiffies, fip->port_ka_time)) {
3144                         LIBFCOE_FIP_DBG(fip, "vn_timeout: send vn2vn beacon\n");
3145                         fcoe_ctlr_vn_send(fip, FIP_SC_VN_BEACON,
3146                                           fcoe_all_vn2vn, 0);
3147                         fip->port_ka_time = jiffies +
3148                                  msecs_to_jiffies(FIP_VN_BEACON_INT +
3149                                         (prandom_u32() % FIP_VN_BEACON_FUZZ));
3150                 }
3151                 if (time_before(fip->port_ka_time, next_time))
3152                         next_time = fip->port_ka_time;
3153                 break;
3154         case FIP_ST_LINK_WAIT:
3155                 goto unlock;
3156         default:
3157                 WARN(1, "unexpected state %d\n", fip->state);
3158                 goto unlock;
3159         }
3160         mod_timer(&fip->timer, next_time);
3161 unlock:
3162         mutex_unlock(&fip->ctlr_mutex);
3163
3164         /* If port ID is new, notify local port after dropping ctlr_mutex */
3165         if (new_port_id)
3166                 fc_lport_set_local_id(fip->lp, new_port_id);
3167 }
3168
3169 /**
3170  * fcoe_ctlr_mode_set() - Set or reset the ctlr's mode
3171  * @lport: The local port to be (re)configured
3172  * @fip:   The FCoE controller whose mode is changing
3173  * @fip_mode: The new fip mode
3174  *
3175  * Note that the we shouldn't be changing the libfc discovery settings
3176  * (fc_disc_config) while an lport is going through the libfc state
3177  * machine. The mode can only be changed when a fcoe_ctlr device is
3178  * disabled, so that should ensure that this routine is only called
3179  * when nothing is happening.
3180  */
3181 static void fcoe_ctlr_mode_set(struct fc_lport *lport, struct fcoe_ctlr *fip,
3182                                enum fip_mode fip_mode)
3183 {
3184         void *priv;
3185
3186         WARN_ON(lport->state != LPORT_ST_RESET &&
3187                 lport->state != LPORT_ST_DISABLED);
3188
3189         if (fip_mode == FIP_MODE_VN2VN) {
3190                 lport->rport_priv_size = sizeof(struct fcoe_rport);
3191                 lport->point_to_multipoint = 1;
3192                 lport->tt.disc_recv_req = fcoe_ctlr_disc_recv;
3193                 lport->tt.disc_start = fcoe_ctlr_disc_start;
3194                 lport->tt.disc_stop = fcoe_ctlr_disc_stop;
3195                 lport->tt.disc_stop_final = fcoe_ctlr_disc_stop_final;
3196                 priv = fip;
3197         } else {
3198                 lport->rport_priv_size = 0;
3199                 lport->point_to_multipoint = 0;
3200                 lport->tt.disc_recv_req = NULL;
3201                 lport->tt.disc_start = NULL;
3202                 lport->tt.disc_stop = NULL;
3203                 lport->tt.disc_stop_final = NULL;
3204                 priv = lport;
3205         }
3206
3207         fc_disc_config(lport, priv);
3208 }
3209
3210 /**
3211  * fcoe_libfc_config() - Sets up libfc related properties for local port
3212  * @lport:    The local port to configure libfc for
3213  * @fip:      The FCoE controller in use by the local port
3214  * @tt:       The libfc function template
3215  * @init_fcp: If non-zero, the FCP portion of libfc should be initialized
3216  *
3217  * Returns : 0 for success
3218  */
3219 int fcoe_libfc_config(struct fc_lport *lport, struct fcoe_ctlr *fip,
3220                       const struct libfc_function_template *tt, int init_fcp)
3221 {
3222         /* Set the function pointers set by the LLDD */
3223         memcpy(&lport->tt, tt, sizeof(*tt));
3224         if (init_fcp && fc_fcp_init(lport))
3225                 return -ENOMEM;
3226         fc_exch_init(lport);
3227         fc_elsct_init(lport);
3228         fc_lport_init(lport);
3229         fc_disc_init(lport);
3230         fcoe_ctlr_mode_set(lport, fip, fip->mode);
3231         return 0;
3232 }
3233 EXPORT_SYMBOL_GPL(fcoe_libfc_config);
3234
3235 void fcoe_fcf_get_selected(struct fcoe_fcf_device *fcf_dev)
3236 {
3237         struct fcoe_ctlr_device *ctlr_dev = fcoe_fcf_dev_to_ctlr_dev(fcf_dev);
3238         struct fcoe_ctlr *fip = fcoe_ctlr_device_priv(ctlr_dev);
3239         struct fcoe_fcf *fcf;
3240
3241         mutex_lock(&fip->ctlr_mutex);
3242         mutex_lock(&ctlr_dev->lock);
3243
3244         fcf = fcoe_fcf_device_priv(fcf_dev);
3245         if (fcf)
3246                 fcf_dev->selected = (fcf == fip->sel_fcf) ? 1 : 0;
3247         else
3248                 fcf_dev->selected = 0;
3249
3250         mutex_unlock(&ctlr_dev->lock);
3251         mutex_unlock(&fip->ctlr_mutex);
3252 }
3253 EXPORT_SYMBOL(fcoe_fcf_get_selected);
3254
3255 void fcoe_ctlr_set_fip_mode(struct fcoe_ctlr_device *ctlr_dev)
3256 {
3257         struct fcoe_ctlr *ctlr = fcoe_ctlr_device_priv(ctlr_dev);
3258         struct fc_lport *lport = ctlr->lp;
3259
3260         mutex_lock(&ctlr->ctlr_mutex);
3261         switch (ctlr_dev->mode) {
3262         case FIP_CONN_TYPE_VN2VN:
3263                 ctlr->mode = FIP_MODE_VN2VN;
3264                 break;
3265         case FIP_CONN_TYPE_FABRIC:
3266         default:
3267                 ctlr->mode = FIP_MODE_FABRIC;
3268                 break;
3269         }
3270
3271         mutex_unlock(&ctlr->ctlr_mutex);
3272
3273         fcoe_ctlr_mode_set(lport, ctlr, ctlr->mode);
3274 }
3275 EXPORT_SYMBOL(fcoe_ctlr_set_fip_mode);