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