blob: 79df78f2b0858d9920c0539f307caeffc53b35fa [file] [log] [blame]
Vasu Dev9b34ecf2009-03-17 11:42:13 -07001/*
Joe Eykholt97c83892009-03-17 11:42:40 -07002 * Copyright (c) 2008-2009 Cisco Systems, Inc. All rights reserved.
3 * Copyright (c) 2009 Intel Corporation. All rights reserved.
Vasu Dev9b34ecf2009-03-17 11:42:13 -07004 *
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
Joe Eykholt97c83892009-03-17 11:42:40 -070021#include <linux/types.h>
Vasu Dev9b34ecf2009-03-17 11:42:13 -070022#include <linux/module.h>
Joe Eykholt97c83892009-03-17 11:42:40 -070023#include <linux/kernel.h>
24#include <linux/list.h>
25#include <linux/spinlock.h>
26#include <linux/timer.h>
Vasu Dev5e80f7f2009-03-17 11:42:18 -070027#include <linux/netdevice.h>
Joe Eykholt97c83892009-03-17 11:42:40 -070028#include <linux/etherdevice.h>
29#include <linux/ethtool.h>
30#include <linux/if_ether.h>
31#include <linux/if_vlan.h>
Joe Eykholt97c83892009-03-17 11:42:40 -070032#include <linux/errno.h>
33#include <linux/bitops.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090034#include <linux/slab.h>
Joe Eykholt97c83892009-03-17 11:42:40 -070035#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>
Joe Eykholte10f8c62010-07-20 15:20:30 -070042#include <scsi/fc/fc_fcp.h>
Vasu Dev5e80f7f2009-03-17 11:42:18 -070043
44#include <scsi/libfc.h>
Joe Eykholt97c83892009-03-17 11:42:40 -070045#include <scsi/libfcoe.h>
Vasu Dev9b34ecf2009-03-17 11:42:13 -070046
47MODULE_AUTHOR("Open-FCoE.org");
Joe Eykholt97c83892009-03-17 11:42:40 -070048MODULE_DESCRIPTION("FIP discovery protocol support for FCoE HBAs");
Vasu Dev9b34ecf2009-03-17 11:42:13 -070049MODULE_LICENSE("GPL v2");
Vasu Dev5e80f7f2009-03-17 11:42:18 -070050
Joe Eykholt97c83892009-03-17 11:42:40 -070051#define FCOE_CTLR_MIN_FKA 500 /* min keep alive (mS) */
52#define FCOE_CTLR_DEF_FKA FIP_DEF_FKA /* default keep alive (mS) */
53
54static void fcoe_ctlr_timeout(unsigned long);
Joe Eykholt42913652010-03-12 16:08:23 -080055static void fcoe_ctlr_timer_work(struct work_struct *);
Joe Eykholt97c83892009-03-17 11:42:40 -070056static void fcoe_ctlr_recv_work(struct work_struct *);
57
Joe Eykholte10f8c62010-07-20 15:20:30 -070058static void fcoe_ctlr_vn_start(struct fcoe_ctlr *);
59static int fcoe_ctlr_vn_recv(struct fcoe_ctlr *, struct sk_buff *);
60static void fcoe_ctlr_vn_timeout(struct fcoe_ctlr *);
61static int fcoe_ctlr_vn_lookup(struct fcoe_ctlr *, u32, u8 *);
62
Joe Eykholt97c83892009-03-17 11:42:40 -070063static u8 fcoe_all_fcfs[ETH_ALEN] = FIP_ALL_FCF_MACS;
Joe Eykholte10f8c62010-07-20 15:20:30 -070064static u8 fcoe_all_enode[ETH_ALEN] = FIP_ALL_ENODE_MACS;
65static u8 fcoe_all_vn2vn[ETH_ALEN] = FIP_ALL_VN2VN_MACS;
66static u8 fcoe_all_p2p[ETH_ALEN] = FIP_ALL_P2P_MACS;
Joe Eykholt97c83892009-03-17 11:42:40 -070067
Robert Love650bd122009-06-10 15:31:05 -070068unsigned int libfcoe_debug_logging;
69module_param_named(debug_logging, libfcoe_debug_logging, int, S_IRUGO|S_IWUSR);
70MODULE_PARM_DESC(debug_logging, "a bit mask of logging levels");
Joe Eykholt97c83892009-03-17 11:42:40 -070071
Robert Love70b51aa2009-11-03 11:47:45 -080072#define LIBFCOE_LOGGING 0x01 /* General logging, not categorized */
Robert Love650bd122009-06-10 15:31:05 -070073#define LIBFCOE_FIP_LOGGING 0x02 /* FIP logging */
74
Robert Love70b51aa2009-11-03 11:47:45 -080075#define LIBFCOE_CHECK_LOGGING(LEVEL, CMD) \
76do { \
77 if (unlikely(libfcoe_debug_logging & LEVEL)) \
78 do { \
79 CMD; \
80 } while (0); \
Joe Eykholta69b06b2009-08-25 13:58:42 -070081} while (0)
Joe Eykholt97c83892009-03-17 11:42:40 -070082
Robert Love650bd122009-06-10 15:31:05 -070083#define LIBFCOE_DBG(fmt, args...) \
84 LIBFCOE_CHECK_LOGGING(LIBFCOE_LOGGING, \
85 printk(KERN_INFO "libfcoe: " fmt, ##args);)
86
Joe Eykholt0f51c2e2009-11-03 11:48:16 -080087#define LIBFCOE_FIP_DBG(fip, fmt, args...) \
Robert Love650bd122009-06-10 15:31:05 -070088 LIBFCOE_CHECK_LOGGING(LIBFCOE_FIP_LOGGING, \
Joe Eykholt0f51c2e2009-11-03 11:48:16 -080089 printk(KERN_INFO "host%d: fip: " fmt, \
90 (fip)->lp->host->host_no, ##args);)
Joe Eykholt97c83892009-03-17 11:42:40 -070091
Joe Eykholt9b651da2010-07-20 15:20:24 -070092static const char *fcoe_ctlr_states[] = {
93 [FIP_ST_DISABLED] = "DISABLED",
94 [FIP_ST_LINK_WAIT] = "LINK_WAIT",
95 [FIP_ST_AUTO] = "AUTO",
96 [FIP_ST_NON_FIP] = "NON_FIP",
97 [FIP_ST_ENABLED] = "ENABLED",
Joe Eykholte10f8c62010-07-20 15:20:30 -070098 [FIP_ST_VNMP_START] = "VNMP_START",
99 [FIP_ST_VNMP_PROBE1] = "VNMP_PROBE1",
100 [FIP_ST_VNMP_PROBE2] = "VNMP_PROBE2",
101 [FIP_ST_VNMP_CLAIM] = "VNMP_CLAIM",
102 [FIP_ST_VNMP_UP] = "VNMP_UP",
Joe Eykholt9b651da2010-07-20 15:20:24 -0700103};
104
105static const char *fcoe_ctlr_state(enum fip_state state)
106{
107 const char *cp = "unknown";
108
109 if (state < ARRAY_SIZE(fcoe_ctlr_states))
110 cp = fcoe_ctlr_states[state];
111 if (!cp)
112 cp = "unknown";
113 return cp;
114}
115
116/**
117 * fcoe_ctlr_set_state() - Set and do debug printing for the new FIP state.
118 * @fip: The FCoE controller
119 * @state: The new state
120 */
121static void fcoe_ctlr_set_state(struct fcoe_ctlr *fip, enum fip_state state)
122{
123 if (state == fip->state)
124 return;
125 if (fip->lp)
126 LIBFCOE_FIP_DBG(fip, "state %s -> %s\n",
127 fcoe_ctlr_state(fip->state), fcoe_ctlr_state(state));
128 fip->state = state;
129}
130
Robert Love70b51aa2009-11-03 11:47:45 -0800131/**
132 * fcoe_ctlr_mtu_valid() - Check if a FCF's MTU is valid
133 * @fcf: The FCF to check
134 *
Joe Eykholt97c83892009-03-17 11:42:40 -0700135 * Return non-zero if FCF fcoe_size has been validated.
136 */
137static inline int fcoe_ctlr_mtu_valid(const struct fcoe_fcf *fcf)
138{
139 return (fcf->flags & FIP_FL_SOL) != 0;
140}
141
Robert Love70b51aa2009-11-03 11:47:45 -0800142/**
143 * fcoe_ctlr_fcf_usable() - Check if a FCF is usable
144 * @fcf: The FCF to check
145 *
Joe Eykholt97c83892009-03-17 11:42:40 -0700146 * Return non-zero if the FCF is usable.
147 */
148static inline int fcoe_ctlr_fcf_usable(struct fcoe_fcf *fcf)
149{
150 u16 flags = FIP_FL_SOL | FIP_FL_AVAIL;
151
152 return (fcf->flags & flags) == flags;
153}
154
155/**
Robert Love70b51aa2009-11-03 11:47:45 -0800156 * fcoe_ctlr_init() - Initialize the FCoE Controller instance
157 * @fip: The FCoE controller to initialize
Joe Eykholt97c83892009-03-17 11:42:40 -0700158 */
Joe Eykholt3d902ac2010-07-20 15:19:58 -0700159void fcoe_ctlr_init(struct fcoe_ctlr *fip, enum fip_state mode)
Joe Eykholt97c83892009-03-17 11:42:40 -0700160{
Joe Eykholt9b651da2010-07-20 15:20:24 -0700161 fcoe_ctlr_set_state(fip, FIP_ST_LINK_WAIT);
Joe Eykholt3d902ac2010-07-20 15:19:58 -0700162 fip->mode = mode;
Joe Eykholt97c83892009-03-17 11:42:40 -0700163 INIT_LIST_HEAD(&fip->fcfs);
Joe Eykholtfdb068c2010-07-20 15:19:47 -0700164 mutex_init(&fip->ctlr_mutex);
Joe Eykholt97c83892009-03-17 11:42:40 -0700165 fip->flogi_oxid = FC_XID_UNKNOWN;
166 setup_timer(&fip->timer, fcoe_ctlr_timeout, (unsigned long)fip);
Joe Eykholt42913652010-03-12 16:08:23 -0800167 INIT_WORK(&fip->timer_work, fcoe_ctlr_timer_work);
Joe Eykholt97c83892009-03-17 11:42:40 -0700168 INIT_WORK(&fip->recv_work, fcoe_ctlr_recv_work);
169 skb_queue_head_init(&fip->fip_recv_list);
170}
171EXPORT_SYMBOL(fcoe_ctlr_init);
172
173/**
Robert Love70b51aa2009-11-03 11:47:45 -0800174 * fcoe_ctlr_reset_fcfs() - Reset and free all FCFs for a controller
175 * @fip: The FCoE controller whose FCFs are to be reset
Joe Eykholt97c83892009-03-17 11:42:40 -0700176 *
177 * Called with &fcoe_ctlr lock held.
178 */
179static void fcoe_ctlr_reset_fcfs(struct fcoe_ctlr *fip)
180{
181 struct fcoe_fcf *fcf;
182 struct fcoe_fcf *next;
183
184 fip->sel_fcf = NULL;
185 list_for_each_entry_safe(fcf, next, &fip->fcfs, list) {
186 list_del(&fcf->list);
187 kfree(fcf);
188 }
189 fip->fcf_count = 0;
190 fip->sel_time = 0;
191}
192
193/**
Robert Love70b51aa2009-11-03 11:47:45 -0800194 * fcoe_ctlr_destroy() - Disable and tear down a FCoE controller
195 * @fip: The FCoE controller to tear down
Joe Eykholt97c83892009-03-17 11:42:40 -0700196 *
197 * This is called by FCoE drivers before freeing the &fcoe_ctlr.
198 *
199 * The receive handler will have been deleted before this to guarantee
200 * that no more recv_work will be scheduled.
201 *
202 * The timer routine will simply return once we set FIP_ST_DISABLED.
203 * This guarantees that no further timeouts or work will be scheduled.
204 */
205void fcoe_ctlr_destroy(struct fcoe_ctlr *fip)
206{
Chris Leecha4b7cfa2009-08-25 13:59:08 -0700207 cancel_work_sync(&fip->recv_work);
Joe Eykholt1f4aed82009-11-03 11:48:22 -0800208 skb_queue_purge(&fip->fip_recv_list);
Chris Leecha4b7cfa2009-08-25 13:59:08 -0700209
Joe Eykholtfdb068c2010-07-20 15:19:47 -0700210 mutex_lock(&fip->ctlr_mutex);
Joe Eykholt9b651da2010-07-20 15:20:24 -0700211 fcoe_ctlr_set_state(fip, FIP_ST_DISABLED);
Joe Eykholt97c83892009-03-17 11:42:40 -0700212 fcoe_ctlr_reset_fcfs(fip);
Joe Eykholtfdb068c2010-07-20 15:19:47 -0700213 mutex_unlock(&fip->ctlr_mutex);
Joe Eykholt97c83892009-03-17 11:42:40 -0700214 del_timer_sync(&fip->timer);
Joe Eykholt42913652010-03-12 16:08:23 -0800215 cancel_work_sync(&fip->timer_work);
Joe Eykholt97c83892009-03-17 11:42:40 -0700216}
217EXPORT_SYMBOL(fcoe_ctlr_destroy);
218
219/**
Robert Love70b51aa2009-11-03 11:47:45 -0800220 * fcoe_ctlr_fcoe_size() - Return the maximum FCoE size required for VN_Port
221 * @fip: The FCoE controller to get the maximum FCoE size from
Joe Eykholt97c83892009-03-17 11:42:40 -0700222 *
223 * Returns the maximum packet size including the FCoE header and trailer,
224 * but not including any Ethernet or VLAN headers.
225 */
226static inline u32 fcoe_ctlr_fcoe_size(struct fcoe_ctlr *fip)
227{
228 /*
229 * Determine the max FCoE frame size allowed, including
230 * FCoE header and trailer.
231 * Note: lp->mfs is currently the payload size, not the frame size.
232 */
233 return fip->lp->mfs + sizeof(struct fc_frame_header) +
234 sizeof(struct fcoe_hdr) + sizeof(struct fcoe_crc_eof);
235}
236
237/**
Robert Love70b51aa2009-11-03 11:47:45 -0800238 * fcoe_ctlr_solicit() - Send a FIP solicitation
239 * @fip: The FCoE controller to send the solicitation on
240 * @fcf: The destination FCF (if NULL, a multicast solicitation is sent)
Joe Eykholt97c83892009-03-17 11:42:40 -0700241 */
242static void fcoe_ctlr_solicit(struct fcoe_ctlr *fip, struct fcoe_fcf *fcf)
243{
244 struct sk_buff *skb;
245 struct fip_sol {
246 struct ethhdr eth;
247 struct fip_header fip;
248 struct {
249 struct fip_mac_desc mac;
250 struct fip_wwn_desc wwnn;
251 struct fip_size_desc size;
252 } __attribute__((packed)) desc;
253 } __attribute__((packed)) *sol;
254 u32 fcoe_size;
255
256 skb = dev_alloc_skb(sizeof(*sol));
257 if (!skb)
258 return;
259
260 sol = (struct fip_sol *)skb->data;
261
262 memset(sol, 0, sizeof(*sol));
263 memcpy(sol->eth.h_dest, fcf ? fcf->fcf_mac : fcoe_all_fcfs, ETH_ALEN);
264 memcpy(sol->eth.h_source, fip->ctl_src_addr, ETH_ALEN);
265 sol->eth.h_proto = htons(ETH_P_FIP);
266
267 sol->fip.fip_ver = FIP_VER_ENCAPS(FIP_VER);
268 sol->fip.fip_op = htons(FIP_OP_DISC);
269 sol->fip.fip_subcode = FIP_SC_SOL;
270 sol->fip.fip_dl_len = htons(sizeof(sol->desc) / FIP_BPW);
271 sol->fip.fip_flags = htons(FIP_FL_FPMA);
Vasu Dev184dd342009-05-17 12:33:28 +0000272 if (fip->spma)
273 sol->fip.fip_flags |= htons(FIP_FL_SPMA);
Joe Eykholt97c83892009-03-17 11:42:40 -0700274
275 sol->desc.mac.fd_desc.fip_dtype = FIP_DT_MAC;
276 sol->desc.mac.fd_desc.fip_dlen = sizeof(sol->desc.mac) / FIP_BPW;
277 memcpy(sol->desc.mac.fd_mac, fip->ctl_src_addr, ETH_ALEN);
278
279 sol->desc.wwnn.fd_desc.fip_dtype = FIP_DT_NAME;
280 sol->desc.wwnn.fd_desc.fip_dlen = sizeof(sol->desc.wwnn) / FIP_BPW;
281 put_unaligned_be64(fip->lp->wwnn, &sol->desc.wwnn.fd_wwn);
282
283 fcoe_size = fcoe_ctlr_fcoe_size(fip);
284 sol->desc.size.fd_desc.fip_dtype = FIP_DT_FCOE_SIZE;
285 sol->desc.size.fd_desc.fip_dlen = sizeof(sol->desc.size) / FIP_BPW;
286 sol->desc.size.fd_size = htons(fcoe_size);
287
288 skb_put(skb, sizeof(*sol));
Chris Leech0f491532009-05-06 10:52:18 -0700289 skb->protocol = htons(ETH_P_FIP);
Joe Eykholt97c83892009-03-17 11:42:40 -0700290 skb_reset_mac_header(skb);
291 skb_reset_network_header(skb);
292 fip->send(fip, skb);
293
294 if (!fcf)
295 fip->sol_time = jiffies;
296}
297
298/**
Robert Love70b51aa2009-11-03 11:47:45 -0800299 * fcoe_ctlr_link_up() - Start FCoE controller
300 * @fip: The FCoE controller to start
Joe Eykholt97c83892009-03-17 11:42:40 -0700301 *
302 * Called from the LLD when the network link is ready.
303 */
304void fcoe_ctlr_link_up(struct fcoe_ctlr *fip)
305{
Joe Eykholtfdb068c2010-07-20 15:19:47 -0700306 mutex_lock(&fip->ctlr_mutex);
Joe Eykholt97c83892009-03-17 11:42:40 -0700307 if (fip->state == FIP_ST_NON_FIP || fip->state == FIP_ST_AUTO) {
Joe Eykholtfdb068c2010-07-20 15:19:47 -0700308 mutex_unlock(&fip->ctlr_mutex);
Joe Eykholt97c83892009-03-17 11:42:40 -0700309 fc_linkup(fip->lp);
310 } else if (fip->state == FIP_ST_LINK_WAIT) {
Joe Eykholt9b651da2010-07-20 15:20:24 -0700311 fcoe_ctlr_set_state(fip, fip->mode);
Joe Eykholte10f8c62010-07-20 15:20:30 -0700312 switch (fip->mode) {
313 default:
314 LIBFCOE_FIP_DBG(fip, "invalid mode %d\n", fip->mode);
315 /* fall-through */
316 case FIP_MODE_AUTO:
Joe Eykholt0f51c2e2009-11-03 11:48:16 -0800317 LIBFCOE_FIP_DBG(fip, "%s", "setting AUTO mode.\n");
Joe Eykholte10f8c62010-07-20 15:20:30 -0700318 /* fall-through */
319 case FIP_MODE_FABRIC:
320 case FIP_MODE_NON_FIP:
321 mutex_unlock(&fip->ctlr_mutex);
322 fc_linkup(fip->lp);
323 fcoe_ctlr_solicit(fip, NULL);
324 break;
325 case FIP_MODE_VN2VN:
326 fcoe_ctlr_vn_start(fip);
327 mutex_unlock(&fip->ctlr_mutex);
328 fc_linkup(fip->lp);
329 break;
330 }
Joe Eykholt97c83892009-03-17 11:42:40 -0700331 } else
Joe Eykholtfdb068c2010-07-20 15:19:47 -0700332 mutex_unlock(&fip->ctlr_mutex);
Joe Eykholt97c83892009-03-17 11:42:40 -0700333}
334EXPORT_SYMBOL(fcoe_ctlr_link_up);
335
336/**
Robert Love70b51aa2009-11-03 11:47:45 -0800337 * fcoe_ctlr_reset() - Reset a FCoE controller
338 * @fip: The FCoE controller to reset
Joe Eykholt97c83892009-03-17 11:42:40 -0700339 */
Joe Eykholtdd42dac2009-11-03 11:48:27 -0800340static void fcoe_ctlr_reset(struct fcoe_ctlr *fip)
Joe Eykholt97c83892009-03-17 11:42:40 -0700341{
Joe Eykholt97c83892009-03-17 11:42:40 -0700342 fcoe_ctlr_reset_fcfs(fip);
343 del_timer(&fip->timer);
Joe Eykholt97c83892009-03-17 11:42:40 -0700344 fip->ctlr_ka_time = 0;
345 fip->port_ka_time = 0;
346 fip->sol_time = 0;
347 fip->flogi_oxid = FC_XID_UNKNOWN;
348 fip->map_dest = 0;
Joe Eykholt97c83892009-03-17 11:42:40 -0700349}
350
351/**
Robert Love70b51aa2009-11-03 11:47:45 -0800352 * fcoe_ctlr_link_down() - Stop a FCoE controller
353 * @fip: The FCoE controller to be stopped
Joe Eykholt97c83892009-03-17 11:42:40 -0700354 *
355 * Returns non-zero if the link was up and now isn't.
356 *
357 * Called from the LLD when the network link is not ready.
358 * There may be multiple calls while the link is down.
359 */
360int fcoe_ctlr_link_down(struct fcoe_ctlr *fip)
361{
Joe Eykholtdd42dac2009-11-03 11:48:27 -0800362 int link_dropped;
363
364 LIBFCOE_FIP_DBG(fip, "link down.\n");
Joe Eykholtfdb068c2010-07-20 15:19:47 -0700365 mutex_lock(&fip->ctlr_mutex);
Joe Eykholtdd42dac2009-11-03 11:48:27 -0800366 fcoe_ctlr_reset(fip);
Joe Eykholt42913652010-03-12 16:08:23 -0800367 link_dropped = fip->state != FIP_ST_LINK_WAIT;
Joe Eykholt9b651da2010-07-20 15:20:24 -0700368 fcoe_ctlr_set_state(fip, FIP_ST_LINK_WAIT);
Joe Eykholtfdb068c2010-07-20 15:19:47 -0700369 mutex_unlock(&fip->ctlr_mutex);
Joe Eykholtdd42dac2009-11-03 11:48:27 -0800370
371 if (link_dropped)
372 fc_linkdown(fip->lp);
373 return link_dropped;
Joe Eykholt97c83892009-03-17 11:42:40 -0700374}
375EXPORT_SYMBOL(fcoe_ctlr_link_down);
376
377/**
Robert Love70b51aa2009-11-03 11:47:45 -0800378 * fcoe_ctlr_send_keep_alive() - Send a keep-alive to the selected FCF
379 * @fip: The FCoE controller to send the FKA on
380 * @lport: libfc fc_lport to send from
381 * @ports: 0 for controller keep-alive, 1 for port keep-alive
382 * @sa: The source MAC address
Joe Eykholt97c83892009-03-17 11:42:40 -0700383 *
384 * A controller keep-alive is sent every fka_period (typically 8 seconds).
385 * The source MAC is the native MAC address.
386 *
387 * A port keep-alive is sent every 90 seconds while logged in.
388 * The source MAC is the assigned mapped source address.
389 * The destination is the FCF's F-port.
390 */
Chris Leech11b56182009-11-03 11:46:29 -0800391static void fcoe_ctlr_send_keep_alive(struct fcoe_ctlr *fip,
392 struct fc_lport *lport,
393 int ports, u8 *sa)
Joe Eykholt97c83892009-03-17 11:42:40 -0700394{
395 struct sk_buff *skb;
396 struct fip_kal {
397 struct ethhdr eth;
398 struct fip_header fip;
399 struct fip_mac_desc mac;
400 } __attribute__((packed)) *kal;
401 struct fip_vn_desc *vn;
402 u32 len;
403 struct fc_lport *lp;
404 struct fcoe_fcf *fcf;
405
406 fcf = fip->sel_fcf;
407 lp = fip->lp;
Joe Eykholt281ae642010-06-11 16:43:33 -0700408 if (!fcf || (ports && !lp->port_id))
Joe Eykholt97c83892009-03-17 11:42:40 -0700409 return;
410
Yi Zoube276cb2009-11-03 11:50:16 -0800411 len = sizeof(*kal) + ports * sizeof(*vn);
Joe Eykholt97c83892009-03-17 11:42:40 -0700412 skb = dev_alloc_skb(len);
413 if (!skb)
414 return;
415
416 kal = (struct fip_kal *)skb->data;
417 memset(kal, 0, len);
418 memcpy(kal->eth.h_dest, fcf->fcf_mac, ETH_ALEN);
419 memcpy(kal->eth.h_source, sa, ETH_ALEN);
420 kal->eth.h_proto = htons(ETH_P_FIP);
421
422 kal->fip.fip_ver = FIP_VER_ENCAPS(FIP_VER);
423 kal->fip.fip_op = htons(FIP_OP_CTRL);
424 kal->fip.fip_subcode = FIP_SC_KEEP_ALIVE;
425 kal->fip.fip_dl_len = htons((sizeof(kal->mac) +
Robert Love70b51aa2009-11-03 11:47:45 -0800426 ports * sizeof(*vn)) / FIP_BPW);
Joe Eykholt97c83892009-03-17 11:42:40 -0700427 kal->fip.fip_flags = htons(FIP_FL_FPMA);
Vasu Dev184dd342009-05-17 12:33:28 +0000428 if (fip->spma)
429 kal->fip.fip_flags |= htons(FIP_FL_SPMA);
Joe Eykholt97c83892009-03-17 11:42:40 -0700430
431 kal->mac.fd_desc.fip_dtype = FIP_DT_MAC;
432 kal->mac.fd_desc.fip_dlen = sizeof(kal->mac) / FIP_BPW;
433 memcpy(kal->mac.fd_mac, fip->ctl_src_addr, ETH_ALEN);
Joe Eykholt97c83892009-03-17 11:42:40 -0700434 if (ports) {
435 vn = (struct fip_vn_desc *)(kal + 1);
436 vn->fd_desc.fip_dtype = FIP_DT_VN_ID;
437 vn->fd_desc.fip_dlen = sizeof(*vn) / FIP_BPW;
Chris Leech11b56182009-11-03 11:46:29 -0800438 memcpy(vn->fd_mac, fip->get_src_addr(lport), ETH_ALEN);
Kaladhar Musunurufb831532010-05-07 15:18:57 -0700439 hton24(vn->fd_fc_id, lport->port_id);
440 put_unaligned_be64(lport->wwpn, &vn->fd_wwpn);
Joe Eykholt97c83892009-03-17 11:42:40 -0700441 }
Joe Eykholt97c83892009-03-17 11:42:40 -0700442 skb_put(skb, len);
Chris Leech0f491532009-05-06 10:52:18 -0700443 skb->protocol = htons(ETH_P_FIP);
Joe Eykholt97c83892009-03-17 11:42:40 -0700444 skb_reset_mac_header(skb);
445 skb_reset_network_header(skb);
446 fip->send(fip, skb);
447}
448
449/**
Robert Love70b51aa2009-11-03 11:47:45 -0800450 * fcoe_ctlr_encaps() - Encapsulate an ELS frame for FIP, without sending it
451 * @fip: The FCoE controller for the ELS frame
452 * @dtype: The FIP descriptor type for the frame
453 * @skb: The FCoE ELS frame including FC header but no FCoE headers
Joe Eykholte10f8c62010-07-20 15:20:30 -0700454 * @d_id: The destination port ID.
Joe Eykholt97c83892009-03-17 11:42:40 -0700455 *
456 * Returns non-zero error code on failure.
457 *
458 * The caller must check that the length is a multiple of 4.
459 *
460 * The @skb must have enough headroom (28 bytes) and tailroom (8 bytes).
461 * Headroom includes the FIP encapsulation description, FIP header, and
462 * Ethernet header. The tailroom is for the FIP MAC descriptor.
463 */
Chris Leech11b56182009-11-03 11:46:29 -0800464static int fcoe_ctlr_encaps(struct fcoe_ctlr *fip, struct fc_lport *lport,
Joe Eykholte10f8c62010-07-20 15:20:30 -0700465 u8 dtype, struct sk_buff *skb, u32 d_id)
Joe Eykholt97c83892009-03-17 11:42:40 -0700466{
467 struct fip_encaps_head {
468 struct ethhdr eth;
469 struct fip_header fip;
470 struct fip_encaps encaps;
471 } __attribute__((packed)) *cap;
472 struct fip_mac_desc *mac;
473 struct fcoe_fcf *fcf;
474 size_t dlen;
Yi Zou5a84bae2009-07-29 17:03:55 -0700475 u16 fip_flags;
Joe Eykholt97c83892009-03-17 11:42:40 -0700476
Joe Eykholt97c83892009-03-17 11:42:40 -0700477 dlen = sizeof(struct fip_encaps) + skb->len; /* len before push */
478 cap = (struct fip_encaps_head *)skb_push(skb, sizeof(*cap));
Joe Eykholt97c83892009-03-17 11:42:40 -0700479 memset(cap, 0, sizeof(*cap));
Joe Eykholte10f8c62010-07-20 15:20:30 -0700480
481 if (lport->point_to_multipoint) {
482 if (fcoe_ctlr_vn_lookup(fip, d_id, cap->eth.h_dest))
483 return -ENODEV;
484 } else {
485 fcf = fip->sel_fcf;
486 if (!fcf)
487 return -ENODEV;
488 fip_flags = fcf->flags;
489 fip_flags &= fip->spma ? FIP_FL_SPMA | FIP_FL_FPMA :
490 FIP_FL_FPMA;
491 if (!fip_flags)
492 return -ENODEV;
493 memcpy(cap->eth.h_dest, fcf->fcf_mac, ETH_ALEN);
494 }
Joe Eykholt97c83892009-03-17 11:42:40 -0700495 memcpy(cap->eth.h_source, fip->ctl_src_addr, ETH_ALEN);
496 cap->eth.h_proto = htons(ETH_P_FIP);
497
498 cap->fip.fip_ver = FIP_VER_ENCAPS(FIP_VER);
499 cap->fip.fip_op = htons(FIP_OP_LS);
500 cap->fip.fip_subcode = FIP_SC_REQ;
501 cap->fip.fip_dl_len = htons((dlen + sizeof(*mac)) / FIP_BPW);
Yi Zou5a84bae2009-07-29 17:03:55 -0700502 cap->fip.fip_flags = htons(fip_flags);
Joe Eykholt97c83892009-03-17 11:42:40 -0700503
504 cap->encaps.fd_desc.fip_dtype = dtype;
505 cap->encaps.fd_desc.fip_dlen = dlen / FIP_BPW;
506
507 mac = (struct fip_mac_desc *)skb_put(skb, sizeof(*mac));
Kaladhar Musunuru8b889e42010-05-07 15:18:52 -0700508 memset(mac, 0, sizeof(*mac));
Joe Eykholt97c83892009-03-17 11:42:40 -0700509 mac->fd_desc.fip_dtype = FIP_DT_MAC;
510 mac->fd_desc.fip_dlen = sizeof(*mac) / FIP_BPW;
Robert Love593abc02010-04-09 14:22:17 -0700511 if (dtype != FIP_DT_FLOGI && dtype != FIP_DT_FDISC) {
Chris Leech11b56182009-11-03 11:46:29 -0800512 memcpy(mac->fd_mac, fip->get_src_addr(lport), ETH_ALEN);
Robert Love593abc02010-04-09 14:22:17 -0700513 } else if (fip_flags & FIP_FL_SPMA) {
514 LIBFCOE_FIP_DBG(fip, "FLOGI/FDISC sent with SPMA\n");
Vasu Dev184dd342009-05-17 12:33:28 +0000515 memcpy(mac->fd_mac, fip->ctl_src_addr, ETH_ALEN);
Robert Love593abc02010-04-09 14:22:17 -0700516 } else {
517 LIBFCOE_FIP_DBG(fip, "FLOGI/FDISC sent with FPMA\n");
518 /* FPMA only FLOGI must leave the MAC desc set to all 0s */
519 }
Joe Eykholt97c83892009-03-17 11:42:40 -0700520
Chris Leech0f491532009-05-06 10:52:18 -0700521 skb->protocol = htons(ETH_P_FIP);
Joe Eykholt97c83892009-03-17 11:42:40 -0700522 skb_reset_mac_header(skb);
523 skb_reset_network_header(skb);
524 return 0;
525}
526
527/**
528 * fcoe_ctlr_els_send() - Send an ELS frame encapsulated by FIP if appropriate.
529 * @fip: FCoE controller.
Chris Leech11b56182009-11-03 11:46:29 -0800530 * @lport: libfc fc_lport to send from
Joe Eykholt97c83892009-03-17 11:42:40 -0700531 * @skb: FCoE ELS frame including FC header but no FCoE headers.
532 *
533 * Returns a non-zero error code if the frame should not be sent.
534 * Returns zero if the caller should send the frame with FCoE encapsulation.
535 *
536 * The caller must check that the length is a multiple of 4.
537 * The SKB must have enough headroom (28 bytes) and tailroom (8 bytes).
Joe Eykholte10f8c62010-07-20 15:20:30 -0700538 * The the skb must also be an fc_frame.
Joe Eykholt97c83892009-03-17 11:42:40 -0700539 */
Chris Leech11b56182009-11-03 11:46:29 -0800540int fcoe_ctlr_els_send(struct fcoe_ctlr *fip, struct fc_lport *lport,
541 struct sk_buff *skb)
Joe Eykholt97c83892009-03-17 11:42:40 -0700542{
Joe Eykholte10f8c62010-07-20 15:20:30 -0700543 struct fc_frame *fp;
Joe Eykholt97c83892009-03-17 11:42:40 -0700544 struct fc_frame_header *fh;
545 u16 old_xid;
546 u8 op;
Chris Leech11b56182009-11-03 11:46:29 -0800547 u8 mac[ETH_ALEN];
Joe Eykholt97c83892009-03-17 11:42:40 -0700548
Joe Eykholte10f8c62010-07-20 15:20:30 -0700549 fp = container_of(skb, struct fc_frame, skb);
Joe Eykholt97c83892009-03-17 11:42:40 -0700550 fh = (struct fc_frame_header *)skb->data;
551 op = *(u8 *)(fh + 1);
552
Joe Eykholte10f8c62010-07-20 15:20:30 -0700553 if (op == ELS_FLOGI && fip->mode != FIP_MODE_VN2VN) {
Joe Eykholt97c83892009-03-17 11:42:40 -0700554 old_xid = fip->flogi_oxid;
555 fip->flogi_oxid = ntohs(fh->fh_ox_id);
556 if (fip->state == FIP_ST_AUTO) {
557 if (old_xid == FC_XID_UNKNOWN)
558 fip->flogi_count = 0;
559 fip->flogi_count++;
560 if (fip->flogi_count < 3)
561 goto drop;
562 fip->map_dest = 1;
563 return 0;
564 }
Joe Eykholt5f48f702009-05-06 10:52:12 -0700565 if (fip->state == FIP_ST_NON_FIP)
566 fip->map_dest = 1;
567 }
568
569 if (fip->state == FIP_ST_NON_FIP)
570 return 0;
Joe Eykholte10f8c62010-07-20 15:20:30 -0700571 if (!fip->sel_fcf && fip->mode != FIP_MODE_VN2VN)
Joe Eykholtf31f2a12009-11-03 11:48:32 -0800572 goto drop;
Joe Eykholt5f48f702009-05-06 10:52:12 -0700573 switch (op) {
574 case ELS_FLOGI:
Joe Eykholt97c83892009-03-17 11:42:40 -0700575 op = FIP_DT_FLOGI;
576 break;
577 case ELS_FDISC:
578 if (ntoh24(fh->fh_s_id))
579 return 0;
580 op = FIP_DT_FDISC;
581 break;
582 case ELS_LOGO:
Joe Eykholte10f8c62010-07-20 15:20:30 -0700583 if (fip->mode == FIP_MODE_VN2VN) {
584 if (fip->state != FIP_ST_VNMP_UP)
585 return -EINVAL;
586 if (ntoh24(fh->fh_d_id) == FC_FID_FLOGI)
587 return -EINVAL;
588 } else {
589 if (fip->state != FIP_ST_ENABLED)
590 return 0;
591 if (ntoh24(fh->fh_d_id) != FC_FID_FLOGI)
592 return 0;
593 }
Joe Eykholt97c83892009-03-17 11:42:40 -0700594 op = FIP_DT_LOGO;
595 break;
596 case ELS_LS_ACC:
Joe Eykholt97c83892009-03-17 11:42:40 -0700597 /*
Joe Eykholte10f8c62010-07-20 15:20:30 -0700598 * If non-FIP, we may have gotten an SID by accepting an FLOGI
Joe Eykholt97c83892009-03-17 11:42:40 -0700599 * from a point-to-point connection. Switch to using
600 * the source mac based on the SID. The destination
601 * MAC in this case would have been set by receving the
602 * FLOGI.
603 */
Joe Eykholte10f8c62010-07-20 15:20:30 -0700604 if (fip->state == FIP_ST_NON_FIP) {
605 if (fip->flogi_oxid == FC_XID_UNKNOWN)
606 return 0;
607 fip->flogi_oxid = FC_XID_UNKNOWN;
608 fc_fcoe_set_mac(mac, fh->fh_d_id);
609 fip->update_mac(lport, mac);
610 }
611 /* fall through */
612 case ELS_LS_RJT:
613 op = fr_encaps(fp);
614 if (op)
615 break;
Joe Eykholt97c83892009-03-17 11:42:40 -0700616 return 0;
617 default:
Joe Eykholte10f8c62010-07-20 15:20:30 -0700618 if (fip->state != FIP_ST_ENABLED &&
619 fip->state != FIP_ST_VNMP_UP)
Joe Eykholt97c83892009-03-17 11:42:40 -0700620 goto drop;
621 return 0;
622 }
Joe Eykholte10f8c62010-07-20 15:20:30 -0700623 LIBFCOE_FIP_DBG(fip, "els_send op %u d_id %x\n",
624 op, ntoh24(fh->fh_d_id));
625 if (fcoe_ctlr_encaps(fip, lport, op, skb, ntoh24(fh->fh_d_id)))
Joe Eykholt97c83892009-03-17 11:42:40 -0700626 goto drop;
627 fip->send(fip, skb);
628 return -EINPROGRESS;
629drop:
630 kfree_skb(skb);
631 return -EINVAL;
632}
633EXPORT_SYMBOL(fcoe_ctlr_els_send);
634
Robert Love70b51aa2009-11-03 11:47:45 -0800635/**
636 * fcoe_ctlr_age_fcfs() - Reset and free all old FCFs for a controller
637 * @fip: The FCoE controller to free FCFs on
Joe Eykholt97c83892009-03-17 11:42:40 -0700638 *
Joe Eykholtf018b732010-03-12 16:08:55 -0800639 * Called with lock held and preemption disabled.
Joe Eykholt97c83892009-03-17 11:42:40 -0700640 *
Joe Eykholt8690cb82010-06-11 16:44:10 -0700641 * An FCF is considered old if we have missed two advertisements.
642 * That is, there have been no valid advertisement from it for 2.5
643 * times its keep-alive period.
Joe Eykholt97c83892009-03-17 11:42:40 -0700644 *
645 * In addition, determine the time when an FCF selection can occur.
Yi Zouf3da80e72009-11-20 14:55:08 -0800646 *
647 * Also, increment the MissDiscAdvCount when no advertisement is received
648 * for the corresponding FCF for 1.5 * FKA_ADV_PERIOD (FC-BB-5 LESB).
Joe Eykholt8690cb82010-06-11 16:44:10 -0700649 *
650 * Returns the time in jiffies for the next call.
Joe Eykholt97c83892009-03-17 11:42:40 -0700651 */
Joe Eykholt8690cb82010-06-11 16:44:10 -0700652static unsigned long fcoe_ctlr_age_fcfs(struct fcoe_ctlr *fip)
Joe Eykholt97c83892009-03-17 11:42:40 -0700653{
654 struct fcoe_fcf *fcf;
655 struct fcoe_fcf *next;
Joe Eykholt8690cb82010-06-11 16:44:10 -0700656 unsigned long next_timer = jiffies + msecs_to_jiffies(FIP_VN_KA_PERIOD);
657 unsigned long deadline;
Joe Eykholt97c83892009-03-17 11:42:40 -0700658 unsigned long sel_time = 0;
Joe Eykholtf018b732010-03-12 16:08:55 -0800659 struct fcoe_dev_stats *stats;
Joe Eykholt97c83892009-03-17 11:42:40 -0700660
Joe Eykholtfdb068c2010-07-20 15:19:47 -0700661 stats = per_cpu_ptr(fip->lp->dev_stats, get_cpu());
662
Joe Eykholt97c83892009-03-17 11:42:40 -0700663 list_for_each_entry_safe(fcf, next, &fip->fcfs, list) {
Joe Eykholt8690cb82010-06-11 16:44:10 -0700664 deadline = fcf->time + fcf->fka_period + fcf->fka_period / 2;
665 if (fip->sel_fcf == fcf) {
666 if (time_after(jiffies, deadline)) {
Joe Eykholt8690cb82010-06-11 16:44:10 -0700667 stats->MissDiscAdvCount++;
668 printk(KERN_INFO "libfcoe: host%d: "
669 "Missing Discovery Advertisement "
670 "for fab %16.16llx count %lld\n",
671 fip->lp->host->host_no, fcf->fabric_name,
672 stats->MissDiscAdvCount);
673 } else if (time_after(next_timer, deadline))
674 next_timer = deadline;
Yi Zouf3da80e72009-11-20 14:55:08 -0800675 }
Joe Eykholt8690cb82010-06-11 16:44:10 -0700676
677 deadline += fcf->fka_period;
Joe Eykholtd99ee452010-06-11 16:44:15 -0700678 if (time_after_eq(jiffies, deadline)) {
Joe Eykholt97c83892009-03-17 11:42:40 -0700679 if (fip->sel_fcf == fcf)
680 fip->sel_fcf = NULL;
681 list_del(&fcf->list);
682 WARN_ON(!fip->fcf_count);
683 fip->fcf_count--;
684 kfree(fcf);
Joe Eykholtf018b732010-03-12 16:08:55 -0800685 stats->VLinkFailureCount++;
Joe Eykholt8690cb82010-06-11 16:44:10 -0700686 } else {
687 if (time_after(next_timer, deadline))
688 next_timer = deadline;
689 if (fcoe_ctlr_mtu_valid(fcf) &&
690 (!sel_time || time_before(sel_time, fcf->time)))
691 sel_time = fcf->time;
Joe Eykholt97c83892009-03-17 11:42:40 -0700692 }
693 }
Joe Eykholtfdb068c2010-07-20 15:19:47 -0700694 put_cpu();
Joe Eykholtd99ee452010-06-11 16:44:15 -0700695 if (sel_time && !fip->sel_fcf && !fip->sel_time) {
Joe Eykholt97c83892009-03-17 11:42:40 -0700696 sel_time += msecs_to_jiffies(FCOE_CTLR_START_DELAY);
697 fip->sel_time = sel_time;
Joe Eykholt97c83892009-03-17 11:42:40 -0700698 }
Joe Eykholtd99ee452010-06-11 16:44:15 -0700699
Joe Eykholt8690cb82010-06-11 16:44:10 -0700700 return next_timer;
Joe Eykholt97c83892009-03-17 11:42:40 -0700701}
702
703/**
Robert Love70b51aa2009-11-03 11:47:45 -0800704 * fcoe_ctlr_parse_adv() - Decode a FIP advertisement into a new FCF entry
Joe Eykholt0f51c2e2009-11-03 11:48:16 -0800705 * @fip: The FCoE controller receiving the advertisement
Robert Love70b51aa2009-11-03 11:47:45 -0800706 * @skb: The received FIP advertisement frame
707 * @fcf: The resulting FCF entry
Joe Eykholt97c83892009-03-17 11:42:40 -0700708 *
709 * Returns zero on a valid parsed advertisement,
710 * otherwise returns non zero value.
711 */
Joe Eykholt0f51c2e2009-11-03 11:48:16 -0800712static int fcoe_ctlr_parse_adv(struct fcoe_ctlr *fip,
713 struct sk_buff *skb, struct fcoe_fcf *fcf)
Joe Eykholt97c83892009-03-17 11:42:40 -0700714{
715 struct fip_header *fiph;
716 struct fip_desc *desc = NULL;
717 struct fip_wwn_desc *wwn;
718 struct fip_fab_desc *fab;
719 struct fip_fka_desc *fka;
720 unsigned long t;
721 size_t rlen;
722 size_t dlen;
Bhanu Prakash Gollapudi0a9c5d32010-06-11 16:44:25 -0700723 u32 desc_mask;
Joe Eykholt97c83892009-03-17 11:42:40 -0700724
725 memset(fcf, 0, sizeof(*fcf));
726 fcf->fka_period = msecs_to_jiffies(FCOE_CTLR_DEF_FKA);
727
728 fiph = (struct fip_header *)skb->data;
729 fcf->flags = ntohs(fiph->fip_flags);
730
Bhanu Prakash Gollapudi0a9c5d32010-06-11 16:44:25 -0700731 /*
732 * mask of required descriptors. validating each one clears its bit.
733 */
734 desc_mask = BIT(FIP_DT_PRI) | BIT(FIP_DT_MAC) | BIT(FIP_DT_NAME) |
735 BIT(FIP_DT_FAB) | BIT(FIP_DT_FKA);
736
Joe Eykholt97c83892009-03-17 11:42:40 -0700737 rlen = ntohs(fiph->fip_dl_len) * 4;
738 if (rlen + sizeof(*fiph) > skb->len)
739 return -EINVAL;
740
741 desc = (struct fip_desc *)(fiph + 1);
742 while (rlen > 0) {
743 dlen = desc->fip_dlen * FIP_BPW;
744 if (dlen < sizeof(*desc) || dlen > rlen)
745 return -EINVAL;
Bhanu Prakash Gollapudi0a9c5d32010-06-11 16:44:25 -0700746 /* Drop Adv if there are duplicate critical descriptors */
747 if ((desc->fip_dtype < 32) &&
748 !(desc_mask & 1U << desc->fip_dtype)) {
749 LIBFCOE_FIP_DBG(fip, "Duplicate Critical "
750 "Descriptors in FIP adv\n");
751 return -EINVAL;
752 }
Joe Eykholt97c83892009-03-17 11:42:40 -0700753 switch (desc->fip_dtype) {
754 case FIP_DT_PRI:
755 if (dlen != sizeof(struct fip_pri_desc))
756 goto len_err;
757 fcf->pri = ((struct fip_pri_desc *)desc)->fd_pri;
Bhanu Prakash Gollapudi0a9c5d32010-06-11 16:44:25 -0700758 desc_mask &= ~BIT(FIP_DT_PRI);
Joe Eykholt97c83892009-03-17 11:42:40 -0700759 break;
760 case FIP_DT_MAC:
761 if (dlen != sizeof(struct fip_mac_desc))
762 goto len_err;
763 memcpy(fcf->fcf_mac,
764 ((struct fip_mac_desc *)desc)->fd_mac,
765 ETH_ALEN);
766 if (!is_valid_ether_addr(fcf->fcf_mac)) {
Joe Eykholte10f8c62010-07-20 15:20:30 -0700767 LIBFCOE_FIP_DBG(fip,
768 "Invalid MAC addr %pM in FIP adv\n",
769 fcf->fcf_mac);
Joe Eykholt97c83892009-03-17 11:42:40 -0700770 return -EINVAL;
771 }
Bhanu Prakash Gollapudi0a9c5d32010-06-11 16:44:25 -0700772 desc_mask &= ~BIT(FIP_DT_MAC);
Joe Eykholt97c83892009-03-17 11:42:40 -0700773 break;
774 case FIP_DT_NAME:
775 if (dlen != sizeof(struct fip_wwn_desc))
776 goto len_err;
777 wwn = (struct fip_wwn_desc *)desc;
778 fcf->switch_name = get_unaligned_be64(&wwn->fd_wwn);
Bhanu Prakash Gollapudi0a9c5d32010-06-11 16:44:25 -0700779 desc_mask &= ~BIT(FIP_DT_NAME);
Joe Eykholt97c83892009-03-17 11:42:40 -0700780 break;
781 case FIP_DT_FAB:
782 if (dlen != sizeof(struct fip_fab_desc))
783 goto len_err;
784 fab = (struct fip_fab_desc *)desc;
785 fcf->fabric_name = get_unaligned_be64(&fab->fd_wwn);
786 fcf->vfid = ntohs(fab->fd_vfid);
787 fcf->fc_map = ntoh24(fab->fd_map);
Bhanu Prakash Gollapudi0a9c5d32010-06-11 16:44:25 -0700788 desc_mask &= ~BIT(FIP_DT_FAB);
Joe Eykholt97c83892009-03-17 11:42:40 -0700789 break;
790 case FIP_DT_FKA:
791 if (dlen != sizeof(struct fip_fka_desc))
792 goto len_err;
793 fka = (struct fip_fka_desc *)desc;
Yi Zou8cdffdc2009-11-20 14:54:57 -0800794 if (fka->fd_flags & FIP_FKA_ADV_D)
795 fcf->fd_flags = 1;
Joe Eykholt97c83892009-03-17 11:42:40 -0700796 t = ntohl(fka->fd_fka_period);
797 if (t >= FCOE_CTLR_MIN_FKA)
798 fcf->fka_period = msecs_to_jiffies(t);
Bhanu Prakash Gollapudi0a9c5d32010-06-11 16:44:25 -0700799 desc_mask &= ~BIT(FIP_DT_FKA);
Joe Eykholt97c83892009-03-17 11:42:40 -0700800 break;
801 case FIP_DT_MAP_OUI:
802 case FIP_DT_FCOE_SIZE:
803 case FIP_DT_FLOGI:
804 case FIP_DT_FDISC:
805 case FIP_DT_LOGO:
806 case FIP_DT_ELP:
807 default:
Joe Eykholt0f51c2e2009-11-03 11:48:16 -0800808 LIBFCOE_FIP_DBG(fip, "unexpected descriptor type %x "
Robert Love650bd122009-06-10 15:31:05 -0700809 "in FIP adv\n", desc->fip_dtype);
Joe Eykholt97c83892009-03-17 11:42:40 -0700810 /* standard says ignore unknown descriptors >= 128 */
811 if (desc->fip_dtype < FIP_DT_VENDOR_BASE)
812 return -EINVAL;
Bhanu Prakash Gollapudi1508f3ec2010-06-11 16:43:38 -0700813 break;
Joe Eykholt97c83892009-03-17 11:42:40 -0700814 }
815 desc = (struct fip_desc *)((char *)desc + dlen);
816 rlen -= dlen;
817 }
818 if (!fcf->fc_map || (fcf->fc_map & 0x10000))
819 return -EINVAL;
820 if (!fcf->switch_name || !fcf->fabric_name)
821 return -EINVAL;
Bhanu Prakash Gollapudi0a9c5d32010-06-11 16:44:25 -0700822 if (desc_mask) {
823 LIBFCOE_FIP_DBG(fip, "adv missing descriptors mask %x\n",
824 desc_mask);
825 return -EINVAL;
826 }
Joe Eykholt97c83892009-03-17 11:42:40 -0700827 return 0;
828
829len_err:
Joe Eykholt0f51c2e2009-11-03 11:48:16 -0800830 LIBFCOE_FIP_DBG(fip, "FIP length error in descriptor type %x len %zu\n",
Robert Love650bd122009-06-10 15:31:05 -0700831 desc->fip_dtype, dlen);
Joe Eykholt97c83892009-03-17 11:42:40 -0700832 return -EINVAL;
833}
834
835/**
Robert Love70b51aa2009-11-03 11:47:45 -0800836 * fcoe_ctlr_recv_adv() - Handle an incoming advertisement
837 * @fip: The FCoE controller receiving the advertisement
838 * @skb: The received FIP packet
Joe Eykholt97c83892009-03-17 11:42:40 -0700839 */
840static void fcoe_ctlr_recv_adv(struct fcoe_ctlr *fip, struct sk_buff *skb)
841{
842 struct fcoe_fcf *fcf;
843 struct fcoe_fcf new;
844 struct fcoe_fcf *found;
845 unsigned long sol_tov = msecs_to_jiffies(FCOE_CTRL_SOL_TOV);
846 int first = 0;
847 int mtu_valid;
848
Joe Eykholt0f51c2e2009-11-03 11:48:16 -0800849 if (fcoe_ctlr_parse_adv(fip, skb, &new))
Joe Eykholt97c83892009-03-17 11:42:40 -0700850 return;
851
Joe Eykholtfdb068c2010-07-20 15:19:47 -0700852 mutex_lock(&fip->ctlr_mutex);
Joe Eykholt97c83892009-03-17 11:42:40 -0700853 first = list_empty(&fip->fcfs);
854 found = NULL;
855 list_for_each_entry(fcf, &fip->fcfs, list) {
856 if (fcf->switch_name == new.switch_name &&
857 fcf->fabric_name == new.fabric_name &&
858 fcf->fc_map == new.fc_map &&
859 compare_ether_addr(fcf->fcf_mac, new.fcf_mac) == 0) {
860 found = fcf;
861 break;
862 }
863 }
864 if (!found) {
865 if (fip->fcf_count >= FCOE_CTLR_FCF_LIMIT)
866 goto out;
867
868 fcf = kmalloc(sizeof(*fcf), GFP_ATOMIC);
869 if (!fcf)
870 goto out;
871
872 fip->fcf_count++;
873 memcpy(fcf, &new, sizeof(new));
874 list_add(&fcf->list, &fip->fcfs);
875 } else {
876 /*
Joe Eykholtc600fea2010-06-11 16:44:20 -0700877 * Update the FCF's keep-alive descriptor flags.
878 * Other flag changes from new advertisements are
879 * ignored after a solicited advertisement is
880 * received and the FCF is selectable (usable).
Joe Eykholt97c83892009-03-17 11:42:40 -0700881 */
Joe Eykholtc600fea2010-06-11 16:44:20 -0700882 fcf->fd_flags = new.fd_flags;
883 if (!fcoe_ctlr_fcf_usable(fcf))
884 fcf->flags = new.flags;
885
Joe Eykholtd99ee452010-06-11 16:44:15 -0700886 if (fcf == fip->sel_fcf && !fcf->fd_flags) {
Joe Eykholt97c83892009-03-17 11:42:40 -0700887 fip->ctlr_ka_time -= fcf->fka_period;
888 fip->ctlr_ka_time += new.fka_period;
889 if (time_before(fip->ctlr_ka_time, fip->timer.expires))
890 mod_timer(&fip->timer, fip->ctlr_ka_time);
Joe Eykholtc600fea2010-06-11 16:44:20 -0700891 }
Joe Eykholt97c83892009-03-17 11:42:40 -0700892 fcf->fka_period = new.fka_period;
893 memcpy(fcf->fcf_mac, new.fcf_mac, ETH_ALEN);
894 }
895 mtu_valid = fcoe_ctlr_mtu_valid(fcf);
896 fcf->time = jiffies;
Robert Love650bd122009-06-10 15:31:05 -0700897 if (!found) {
Chris Leech9f8f3aa2010-04-09 14:23:16 -0700898 LIBFCOE_FIP_DBG(fip, "New FCF for fab %16.16llx "
899 "map %x val %d\n",
Robert Love650bd122009-06-10 15:31:05 -0700900 fcf->fabric_name, fcf->fc_map, mtu_valid);
901 }
Joe Eykholt97c83892009-03-17 11:42:40 -0700902
903 /*
904 * If this advertisement is not solicited and our max receive size
905 * hasn't been verified, send a solicited advertisement.
906 */
907 if (!mtu_valid)
908 fcoe_ctlr_solicit(fip, fcf);
909
910 /*
911 * If its been a while since we did a solicit, and this is
912 * the first advertisement we've received, do a multicast
913 * solicitation to gather as many advertisements as we can
914 * before selection occurs.
915 */
916 if (first && time_after(jiffies, fip->sol_time + sol_tov))
917 fcoe_ctlr_solicit(fip, NULL);
918
919 /*
920 * If this is the first validated FCF, note the time and
921 * set a timer to trigger selection.
922 */
Joe Eykholtd99ee452010-06-11 16:44:15 -0700923 if (mtu_valid && !fip->sel_fcf && fcoe_ctlr_fcf_usable(fcf)) {
Joe Eykholt97c83892009-03-17 11:42:40 -0700924 fip->sel_time = jiffies +
Robert Love70b51aa2009-11-03 11:47:45 -0800925 msecs_to_jiffies(FCOE_CTLR_START_DELAY);
Joe Eykholt97c83892009-03-17 11:42:40 -0700926 if (!timer_pending(&fip->timer) ||
927 time_before(fip->sel_time, fip->timer.expires))
928 mod_timer(&fip->timer, fip->sel_time);
929 }
930out:
Joe Eykholtfdb068c2010-07-20 15:19:47 -0700931 mutex_unlock(&fip->ctlr_mutex);
Joe Eykholt97c83892009-03-17 11:42:40 -0700932}
933
934/**
Robert Love70b51aa2009-11-03 11:47:45 -0800935 * fcoe_ctlr_recv_els() - Handle an incoming FIP encapsulated ELS frame
936 * @fip: The FCoE controller which received the packet
937 * @skb: The received FIP packet
Joe Eykholt97c83892009-03-17 11:42:40 -0700938 */
939static void fcoe_ctlr_recv_els(struct fcoe_ctlr *fip, struct sk_buff *skb)
940{
Robert Love70b51aa2009-11-03 11:47:45 -0800941 struct fc_lport *lport = fip->lp;
Joe Eykholt97c83892009-03-17 11:42:40 -0700942 struct fip_header *fiph;
Chris Leech11b56182009-11-03 11:46:29 -0800943 struct fc_frame *fp = (struct fc_frame *)skb;
Joe Eykholt97c83892009-03-17 11:42:40 -0700944 struct fc_frame_header *fh = NULL;
945 struct fip_desc *desc;
946 struct fip_encaps *els;
947 struct fcoe_dev_stats *stats;
948 enum fip_desc_type els_dtype = 0;
949 u8 els_op;
950 u8 sub;
951 u8 granted_mac[ETH_ALEN] = { 0 };
952 size_t els_len = 0;
953 size_t rlen;
954 size_t dlen;
Bhanu Prakash Gollapudibe613312010-06-11 16:44:36 -0700955 u32 desc_mask = 0;
956 u32 desc_cnt = 0;
Joe Eykholt97c83892009-03-17 11:42:40 -0700957
958 fiph = (struct fip_header *)skb->data;
959 sub = fiph->fip_subcode;
960 if (sub != FIP_SC_REQ && sub != FIP_SC_REP)
961 goto drop;
962
963 rlen = ntohs(fiph->fip_dl_len) * 4;
964 if (rlen + sizeof(*fiph) > skb->len)
965 goto drop;
966
967 desc = (struct fip_desc *)(fiph + 1);
968 while (rlen > 0) {
Bhanu Prakash Gollapudibe613312010-06-11 16:44:36 -0700969 desc_cnt++;
Joe Eykholt97c83892009-03-17 11:42:40 -0700970 dlen = desc->fip_dlen * FIP_BPW;
971 if (dlen < sizeof(*desc) || dlen > rlen)
972 goto drop;
Bhanu Prakash Gollapudi0a9c5d32010-06-11 16:44:25 -0700973 /* Drop ELS if there are duplicate critical descriptors */
974 if (desc->fip_dtype < 32) {
Bhanu Prakash Gollapudibe613312010-06-11 16:44:36 -0700975 if (desc_mask & 1U << desc->fip_dtype) {
Bhanu Prakash Gollapudi0a9c5d32010-06-11 16:44:25 -0700976 LIBFCOE_FIP_DBG(fip, "Duplicate Critical "
977 "Descriptors in FIP ELS\n");
978 goto drop;
979 }
Bhanu Prakash Gollapudibe613312010-06-11 16:44:36 -0700980 desc_mask |= (1 << desc->fip_dtype);
Bhanu Prakash Gollapudi0a9c5d32010-06-11 16:44:25 -0700981 }
Joe Eykholt97c83892009-03-17 11:42:40 -0700982 switch (desc->fip_dtype) {
983 case FIP_DT_MAC:
Bhanu Prakash Gollapudibe613312010-06-11 16:44:36 -0700984 if (desc_cnt == 1) {
985 LIBFCOE_FIP_DBG(fip, "FIP descriptors "
986 "received out of order\n");
987 goto drop;
988 }
989
Joe Eykholt97c83892009-03-17 11:42:40 -0700990 if (dlen != sizeof(struct fip_mac_desc))
991 goto len_err;
992 memcpy(granted_mac,
993 ((struct fip_mac_desc *)desc)->fd_mac,
994 ETH_ALEN);
Joe Eykholt97c83892009-03-17 11:42:40 -0700995 break;
996 case FIP_DT_FLOGI:
997 case FIP_DT_FDISC:
998 case FIP_DT_LOGO:
999 case FIP_DT_ELP:
Bhanu Prakash Gollapudibe613312010-06-11 16:44:36 -07001000 if (desc_cnt != 1) {
1001 LIBFCOE_FIP_DBG(fip, "FIP descriptors "
1002 "received out of order\n");
1003 goto drop;
1004 }
Joe Eykholt97c83892009-03-17 11:42:40 -07001005 if (fh)
1006 goto drop;
1007 if (dlen < sizeof(*els) + sizeof(*fh) + 1)
1008 goto len_err;
1009 els_len = dlen - sizeof(*els);
1010 els = (struct fip_encaps *)desc;
1011 fh = (struct fc_frame_header *)(els + 1);
1012 els_dtype = desc->fip_dtype;
1013 break;
1014 default:
Joe Eykholt0f51c2e2009-11-03 11:48:16 -08001015 LIBFCOE_FIP_DBG(fip, "unexpected descriptor type %x "
Robert Love650bd122009-06-10 15:31:05 -07001016 "in FIP adv\n", desc->fip_dtype);
Joe Eykholt97c83892009-03-17 11:42:40 -07001017 /* standard says ignore unknown descriptors >= 128 */
1018 if (desc->fip_dtype < FIP_DT_VENDOR_BASE)
1019 goto drop;
Bhanu Prakash Gollapudibe613312010-06-11 16:44:36 -07001020 if (desc_cnt <= 2) {
1021 LIBFCOE_FIP_DBG(fip, "FIP descriptors "
1022 "received out of order\n");
1023 goto drop;
1024 }
Bhanu Prakash Gollapudi1508f3ec2010-06-11 16:43:38 -07001025 break;
Joe Eykholt97c83892009-03-17 11:42:40 -07001026 }
1027 desc = (struct fip_desc *)((char *)desc + dlen);
1028 rlen -= dlen;
1029 }
1030
1031 if (!fh)
1032 goto drop;
1033 els_op = *(u8 *)(fh + 1);
1034
Joe Eykholte10f8c62010-07-20 15:20:30 -07001035 if ((els_dtype == FIP_DT_FLOGI || els_dtype == FIP_DT_FDISC) &&
1036 sub == FIP_SC_REP && els_op == ELS_LS_ACC &&
1037 fip->mode != FIP_MODE_VN2VN) {
1038 if (!is_valid_ether_addr(granted_mac)) {
1039 LIBFCOE_FIP_DBG(fip,
1040 "Invalid MAC address %pM in FIP ELS\n",
1041 granted_mac);
1042 goto drop;
1043 }
1044 memcpy(fr_cb(fp)->granted_mac, granted_mac, ETH_ALEN);
1045
1046 if (fip->flogi_oxid == ntohs(fh->fh_ox_id))
1047 fip->flogi_oxid = FC_XID_UNKNOWN;
1048 }
Joe Eykholt97c83892009-03-17 11:42:40 -07001049
Bhanu Prakash Gollapudibe613312010-06-11 16:44:36 -07001050 if ((desc_cnt == 0) || ((els_op != ELS_LS_RJT) &&
1051 (!(1U << FIP_DT_MAC & desc_mask)))) {
1052 LIBFCOE_FIP_DBG(fip, "Missing critical descriptors "
1053 "in FIP ELS\n");
1054 goto drop;
1055 }
1056
Joe Eykholt97c83892009-03-17 11:42:40 -07001057 /*
1058 * Convert skb into an fc_frame containing only the ELS.
1059 */
1060 skb_pull(skb, (u8 *)fh - skb->data);
1061 skb_trim(skb, els_len);
1062 fp = (struct fc_frame *)skb;
1063 fc_frame_init(fp);
1064 fr_sof(fp) = FC_SOF_I3;
1065 fr_eof(fp) = FC_EOF_T;
Robert Love70b51aa2009-11-03 11:47:45 -08001066 fr_dev(fp) = lport;
Joe Eykholte10f8c62010-07-20 15:20:30 -07001067 fr_encaps(fp) = els_dtype;
Joe Eykholt97c83892009-03-17 11:42:40 -07001068
Joe Eykholtf018b732010-03-12 16:08:55 -08001069 stats = per_cpu_ptr(lport->dev_stats, get_cpu());
Joe Eykholt97c83892009-03-17 11:42:40 -07001070 stats->RxFrames++;
1071 stats->RxWords += skb->len / FIP_BPW;
Joe Eykholtf018b732010-03-12 16:08:55 -08001072 put_cpu();
Joe Eykholt97c83892009-03-17 11:42:40 -07001073
Robert Love70b51aa2009-11-03 11:47:45 -08001074 fc_exch_recv(lport, fp);
Joe Eykholt97c83892009-03-17 11:42:40 -07001075 return;
1076
1077len_err:
Joe Eykholt0f51c2e2009-11-03 11:48:16 -08001078 LIBFCOE_FIP_DBG(fip, "FIP length error in descriptor type %x len %zu\n",
Robert Love650bd122009-06-10 15:31:05 -07001079 desc->fip_dtype, dlen);
Joe Eykholt97c83892009-03-17 11:42:40 -07001080drop:
1081 kfree_skb(skb);
1082}
1083
1084/**
Robert Love70b51aa2009-11-03 11:47:45 -08001085 * fcoe_ctlr_recv_els() - Handle an incoming link reset frame
1086 * @fip: The FCoE controller that received the frame
1087 * @fh: The received FIP header
Joe Eykholt97c83892009-03-17 11:42:40 -07001088 *
1089 * There may be multiple VN_Port descriptors.
1090 * The overall length has already been checked.
1091 */
1092static void fcoe_ctlr_recv_clr_vlink(struct fcoe_ctlr *fip,
Robert Love70b51aa2009-11-03 11:47:45 -08001093 struct fip_header *fh)
Joe Eykholt97c83892009-03-17 11:42:40 -07001094{
1095 struct fip_desc *desc;
1096 struct fip_mac_desc *mp;
1097 struct fip_wwn_desc *wp;
1098 struct fip_vn_desc *vp;
1099 size_t rlen;
1100 size_t dlen;
1101 struct fcoe_fcf *fcf = fip->sel_fcf;
Robert Love70b51aa2009-11-03 11:47:45 -08001102 struct fc_lport *lport = fip->lp;
Bhanu Prakash Gollapudi5550fda2010-06-11 16:44:31 -07001103 struct fc_lport *vn_port = NULL;
1104 u32 desc_mask;
1105 int is_vn_port = 0;
Joe Eykholt97c83892009-03-17 11:42:40 -07001106
Joe Eykholt0f51c2e2009-11-03 11:48:16 -08001107 LIBFCOE_FIP_DBG(fip, "Clear Virtual Link received\n");
Robert Loved29510a2010-05-07 15:18:30 -07001108
Robert Love7b2787e2010-05-07 15:18:41 -07001109 if (!fcf || !lport->port_id)
Joe Eykholt97c83892009-03-17 11:42:40 -07001110 return;
1111
1112 /*
1113 * mask of required descriptors. Validating each one clears its bit.
1114 */
1115 desc_mask = BIT(FIP_DT_MAC) | BIT(FIP_DT_NAME) | BIT(FIP_DT_VN_ID);
1116
1117 rlen = ntohs(fh->fip_dl_len) * FIP_BPW;
1118 desc = (struct fip_desc *)(fh + 1);
1119 while (rlen >= sizeof(*desc)) {
1120 dlen = desc->fip_dlen * FIP_BPW;
1121 if (dlen > rlen)
1122 return;
Bhanu Prakash Gollapudi0a9c5d32010-06-11 16:44:25 -07001123 /* Drop CVL if there are duplicate critical descriptors */
1124 if ((desc->fip_dtype < 32) &&
1125 !(desc_mask & 1U << desc->fip_dtype)) {
1126 LIBFCOE_FIP_DBG(fip, "Duplicate Critical "
1127 "Descriptors in FIP CVL\n");
1128 return;
1129 }
Joe Eykholt97c83892009-03-17 11:42:40 -07001130 switch (desc->fip_dtype) {
1131 case FIP_DT_MAC:
1132 mp = (struct fip_mac_desc *)desc;
1133 if (dlen < sizeof(*mp))
1134 return;
1135 if (compare_ether_addr(mp->fd_mac, fcf->fcf_mac))
1136 return;
1137 desc_mask &= ~BIT(FIP_DT_MAC);
1138 break;
1139 case FIP_DT_NAME:
1140 wp = (struct fip_wwn_desc *)desc;
1141 if (dlen < sizeof(*wp))
1142 return;
1143 if (get_unaligned_be64(&wp->fd_wwn) != fcf->switch_name)
1144 return;
1145 desc_mask &= ~BIT(FIP_DT_NAME);
1146 break;
1147 case FIP_DT_VN_ID:
1148 vp = (struct fip_vn_desc *)desc;
1149 if (dlen < sizeof(*vp))
1150 return;
1151 if (compare_ether_addr(vp->fd_mac,
Robert Love70b51aa2009-11-03 11:47:45 -08001152 fip->get_src_addr(lport)) == 0 &&
1153 get_unaligned_be64(&vp->fd_wwpn) == lport->wwpn &&
Bhanu Prakash Gollapudi5550fda2010-06-11 16:44:31 -07001154 ntoh24(vp->fd_fc_id) == lport->port_id) {
Joe Eykholt97c83892009-03-17 11:42:40 -07001155 desc_mask &= ~BIT(FIP_DT_VN_ID);
Bhanu Prakash Gollapudi5550fda2010-06-11 16:44:31 -07001156 break;
1157 }
1158 /* check if clr_vlink is for NPIV port */
1159 mutex_lock(&lport->lp_mutex);
1160 list_for_each_entry(vn_port, &lport->vports, list) {
1161 if (compare_ether_addr(vp->fd_mac,
1162 fip->get_src_addr(vn_port)) == 0 &&
1163 (get_unaligned_be64(&vp->fd_wwpn)
1164 == vn_port->wwpn) &&
1165 (ntoh24(vp->fd_fc_id) ==
1166 fc_host_port_id(vn_port->host))) {
1167 desc_mask &= ~BIT(FIP_DT_VN_ID);
1168 is_vn_port = 1;
1169 break;
1170 }
1171 }
1172 mutex_unlock(&lport->lp_mutex);
1173
Joe Eykholt97c83892009-03-17 11:42:40 -07001174 break;
1175 default:
1176 /* standard says ignore unknown descriptors >= 128 */
1177 if (desc->fip_dtype < FIP_DT_VENDOR_BASE)
1178 return;
1179 break;
1180 }
1181 desc = (struct fip_desc *)((char *)desc + dlen);
1182 rlen -= dlen;
1183 }
1184
1185 /*
1186 * reset only if all required descriptors were present and valid.
1187 */
1188 if (desc_mask) {
Joe Eykholt0f51c2e2009-11-03 11:48:16 -08001189 LIBFCOE_FIP_DBG(fip, "missing descriptors mask %x\n",
1190 desc_mask);
Joe Eykholt97c83892009-03-17 11:42:40 -07001191 } else {
Joe Eykholt0f51c2e2009-11-03 11:48:16 -08001192 LIBFCOE_FIP_DBG(fip, "performing Clear Virtual Link\n");
Joe Eykholtdd42dac2009-11-03 11:48:27 -08001193
Bhanu Prakash Gollapudi5550fda2010-06-11 16:44:31 -07001194 if (is_vn_port)
1195 fc_lport_reset(vn_port);
1196 else {
Joe Eykholtfdb068c2010-07-20 15:19:47 -07001197 mutex_lock(&fip->ctlr_mutex);
Bhanu Prakash Gollapudi5550fda2010-06-11 16:44:31 -07001198 per_cpu_ptr(lport->dev_stats,
Joe Eykholtfdb068c2010-07-20 15:19:47 -07001199 get_cpu())->VLinkFailureCount++;
1200 put_cpu();
Bhanu Prakash Gollapudi5550fda2010-06-11 16:44:31 -07001201 fcoe_ctlr_reset(fip);
Joe Eykholtfdb068c2010-07-20 15:19:47 -07001202 mutex_unlock(&fip->ctlr_mutex);
Joe Eykholtdd42dac2009-11-03 11:48:27 -08001203
Bhanu Prakash Gollapudi5550fda2010-06-11 16:44:31 -07001204 fc_lport_reset(fip->lp);
1205 fcoe_ctlr_solicit(fip, NULL);
1206 }
Joe Eykholt97c83892009-03-17 11:42:40 -07001207 }
1208}
1209
1210/**
Robert Love70b51aa2009-11-03 11:47:45 -08001211 * fcoe_ctlr_recv() - Receive a FIP packet
1212 * @fip: The FCoE controller that received the packet
1213 * @skb: The received FIP packet
Joe Eykholt97c83892009-03-17 11:42:40 -07001214 *
Joe Eykholt1f4aed82009-11-03 11:48:22 -08001215 * This may be called from either NET_RX_SOFTIRQ or IRQ.
Joe Eykholt97c83892009-03-17 11:42:40 -07001216 */
1217void fcoe_ctlr_recv(struct fcoe_ctlr *fip, struct sk_buff *skb)
1218{
Joe Eykholt1f4aed82009-11-03 11:48:22 -08001219 skb_queue_tail(&fip->fip_recv_list, skb);
Joe Eykholt97c83892009-03-17 11:42:40 -07001220 schedule_work(&fip->recv_work);
1221}
1222EXPORT_SYMBOL(fcoe_ctlr_recv);
1223
1224/**
Robert Love70b51aa2009-11-03 11:47:45 -08001225 * fcoe_ctlr_recv_handler() - Receive a FIP frame
1226 * @fip: The FCoE controller that received the frame
1227 * @skb: The received FIP frame
Joe Eykholt97c83892009-03-17 11:42:40 -07001228 *
1229 * Returns non-zero if the frame is dropped.
1230 */
1231static int fcoe_ctlr_recv_handler(struct fcoe_ctlr *fip, struct sk_buff *skb)
1232{
1233 struct fip_header *fiph;
1234 struct ethhdr *eh;
1235 enum fip_state state;
1236 u16 op;
1237 u8 sub;
1238
1239 if (skb_linearize(skb))
1240 goto drop;
1241 if (skb->len < sizeof(*fiph))
1242 goto drop;
1243 eh = eth_hdr(skb);
Joe Eykholte10f8c62010-07-20 15:20:30 -07001244 if (fip->mode == FIP_MODE_VN2VN) {
1245 if (compare_ether_addr(eh->h_dest, fip->ctl_src_addr) &&
1246 compare_ether_addr(eh->h_dest, fcoe_all_vn2vn) &&
1247 compare_ether_addr(eh->h_dest, fcoe_all_p2p))
1248 goto drop;
1249 } else if (compare_ether_addr(eh->h_dest, fip->ctl_src_addr) &&
1250 compare_ether_addr(eh->h_dest, fcoe_all_enode))
Joe Eykholt97c83892009-03-17 11:42:40 -07001251 goto drop;
1252 fiph = (struct fip_header *)skb->data;
1253 op = ntohs(fiph->fip_op);
1254 sub = fiph->fip_subcode;
1255
Joe Eykholt97c83892009-03-17 11:42:40 -07001256 if (FIP_VER_DECAPS(fiph->fip_ver) != FIP_VER)
1257 goto drop;
1258 if (ntohs(fiph->fip_dl_len) * FIP_BPW + sizeof(*fiph) > skb->len)
1259 goto drop;
1260
Joe Eykholtfdb068c2010-07-20 15:19:47 -07001261 mutex_lock(&fip->ctlr_mutex);
Joe Eykholt97c83892009-03-17 11:42:40 -07001262 state = fip->state;
1263 if (state == FIP_ST_AUTO) {
1264 fip->map_dest = 0;
Joe Eykholt9b651da2010-07-20 15:20:24 -07001265 fcoe_ctlr_set_state(fip, FIP_ST_ENABLED);
Joe Eykholt97c83892009-03-17 11:42:40 -07001266 state = FIP_ST_ENABLED;
Joe Eykholt0f51c2e2009-11-03 11:48:16 -08001267 LIBFCOE_FIP_DBG(fip, "Using FIP mode\n");
Joe Eykholt97c83892009-03-17 11:42:40 -07001268 }
Joe Eykholtfdb068c2010-07-20 15:19:47 -07001269 mutex_unlock(&fip->ctlr_mutex);
Joe Eykholte10f8c62010-07-20 15:20:30 -07001270
1271 if (fip->mode == FIP_MODE_VN2VN && op == FIP_OP_VN2VN)
1272 return fcoe_ctlr_vn_recv(fip, skb);
1273
1274 if (state != FIP_ST_ENABLED && state != FIP_ST_VNMP_UP &&
1275 state != FIP_ST_VNMP_CLAIM)
Joe Eykholt97c83892009-03-17 11:42:40 -07001276 goto drop;
1277
1278 if (op == FIP_OP_LS) {
1279 fcoe_ctlr_recv_els(fip, skb); /* consumes skb */
1280 return 0;
1281 }
Joe Eykholte10f8c62010-07-20 15:20:30 -07001282
1283 if (state != FIP_ST_ENABLED)
1284 goto drop;
1285
Joe Eykholt97c83892009-03-17 11:42:40 -07001286 if (op == FIP_OP_DISC && sub == FIP_SC_ADV)
1287 fcoe_ctlr_recv_adv(fip, skb);
1288 else if (op == FIP_OP_CTRL && sub == FIP_SC_CLR_VLINK)
1289 fcoe_ctlr_recv_clr_vlink(fip, fiph);
1290 kfree_skb(skb);
1291 return 0;
1292drop:
1293 kfree_skb(skb);
1294 return -1;
1295}
1296
1297/**
Robert Love70b51aa2009-11-03 11:47:45 -08001298 * fcoe_ctlr_select() - Select the best FCF (if possible)
1299 * @fip: The FCoE controller
Joe Eykholt97c83892009-03-17 11:42:40 -07001300 *
1301 * If there are conflicting advertisements, no FCF can be chosen.
1302 *
1303 * Called with lock held.
1304 */
1305static void fcoe_ctlr_select(struct fcoe_ctlr *fip)
1306{
1307 struct fcoe_fcf *fcf;
1308 struct fcoe_fcf *best = NULL;
1309
1310 list_for_each_entry(fcf, &fip->fcfs, list) {
Chris Leech9f8f3aa2010-04-09 14:23:16 -07001311 LIBFCOE_FIP_DBG(fip, "consider FCF for fab %16.16llx "
1312 "VFID %d map %x val %d\n",
1313 fcf->fabric_name, fcf->vfid,
Robert Love650bd122009-06-10 15:31:05 -07001314 fcf->fc_map, fcoe_ctlr_mtu_valid(fcf));
Joe Eykholt97c83892009-03-17 11:42:40 -07001315 if (!fcoe_ctlr_fcf_usable(fcf)) {
Chris Leech9f8f3aa2010-04-09 14:23:16 -07001316 LIBFCOE_FIP_DBG(fip, "FCF for fab %16.16llx "
1317 "map %x %svalid %savailable\n",
1318 fcf->fabric_name, fcf->fc_map,
1319 (fcf->flags & FIP_FL_SOL) ? "" : "in",
1320 (fcf->flags & FIP_FL_AVAIL) ?
1321 "" : "un");
Joe Eykholt97c83892009-03-17 11:42:40 -07001322 continue;
1323 }
1324 if (!best) {
1325 best = fcf;
1326 continue;
1327 }
1328 if (fcf->fabric_name != best->fabric_name ||
1329 fcf->vfid != best->vfid ||
1330 fcf->fc_map != best->fc_map) {
Joe Eykholt0f51c2e2009-11-03 11:48:16 -08001331 LIBFCOE_FIP_DBG(fip, "Conflicting fabric, VFID, "
Robert Love650bd122009-06-10 15:31:05 -07001332 "or FC-MAP\n");
Joe Eykholt97c83892009-03-17 11:42:40 -07001333 return;
1334 }
1335 if (fcf->pri < best->pri)
1336 best = fcf;
1337 }
1338 fip->sel_fcf = best;
1339}
1340
1341/**
Robert Love70b51aa2009-11-03 11:47:45 -08001342 * fcoe_ctlr_timeout() - FIP timeout handler
1343 * @arg: The FCoE controller that timed out
Joe Eykholt97c83892009-03-17 11:42:40 -07001344 */
1345static void fcoe_ctlr_timeout(unsigned long arg)
1346{
1347 struct fcoe_ctlr *fip = (struct fcoe_ctlr *)arg;
Joe Eykholtfdb068c2010-07-20 15:19:47 -07001348
1349 schedule_work(&fip->timer_work);
1350}
1351
1352/**
1353 * fcoe_ctlr_timer_work() - Worker thread function for timer work
1354 * @work: Handle to a FCoE controller
1355 *
1356 * Ages FCFs. Triggers FCF selection if possible.
1357 * Sends keep-alives and resets.
1358 */
1359static void fcoe_ctlr_timer_work(struct work_struct *work)
1360{
1361 struct fcoe_ctlr *fip;
1362 struct fc_lport *vport;
1363 u8 *mac;
1364 u8 reset = 0;
1365 u8 send_ctlr_ka = 0;
1366 u8 send_port_ka = 0;
Joe Eykholt97c83892009-03-17 11:42:40 -07001367 struct fcoe_fcf *sel;
1368 struct fcoe_fcf *fcf;
Joe Eykholt8690cb82010-06-11 16:44:10 -07001369 unsigned long next_timer;
Joe Eykholt97c83892009-03-17 11:42:40 -07001370
Joe Eykholtfdb068c2010-07-20 15:19:47 -07001371 fip = container_of(work, struct fcoe_ctlr, timer_work);
Joe Eykholte10f8c62010-07-20 15:20:30 -07001372 if (fip->mode == FIP_MODE_VN2VN)
1373 return fcoe_ctlr_vn_timeout(fip);
Joe Eykholtfdb068c2010-07-20 15:19:47 -07001374 mutex_lock(&fip->ctlr_mutex);
Joe Eykholt97c83892009-03-17 11:42:40 -07001375 if (fip->state == FIP_ST_DISABLED) {
Joe Eykholtfdb068c2010-07-20 15:19:47 -07001376 mutex_unlock(&fip->ctlr_mutex);
Joe Eykholt97c83892009-03-17 11:42:40 -07001377 return;
1378 }
1379
1380 fcf = fip->sel_fcf;
Joe Eykholt8690cb82010-06-11 16:44:10 -07001381 next_timer = fcoe_ctlr_age_fcfs(fip);
Joe Eykholt97c83892009-03-17 11:42:40 -07001382
1383 sel = fip->sel_fcf;
Joe Eykholt8690cb82010-06-11 16:44:10 -07001384 if (!sel && fip->sel_time) {
1385 if (time_after_eq(jiffies, fip->sel_time)) {
1386 fcoe_ctlr_select(fip);
1387 sel = fip->sel_fcf;
1388 fip->sel_time = 0;
1389 } else if (time_after(next_timer, fip->sel_time))
1390 next_timer = fip->sel_time;
Joe Eykholt97c83892009-03-17 11:42:40 -07001391 }
1392
1393 if (sel != fcf) {
1394 fcf = sel; /* the old FCF may have been freed */
1395 if (sel) {
Robert Love650bd122009-06-10 15:31:05 -07001396 printk(KERN_INFO "libfcoe: host%d: FIP selected "
Johannes Berg66d6fae2009-07-15 17:21:14 +02001397 "Fibre-Channel Forwarder MAC %pM\n",
1398 fip->lp->host->host_no, sel->fcf_mac);
Joe Eykholt97c83892009-03-17 11:42:40 -07001399 memcpy(fip->dest_addr, sel->fcf_mac, ETH_ALEN);
1400 fip->port_ka_time = jiffies +
Robert Love70b51aa2009-11-03 11:47:45 -08001401 msecs_to_jiffies(FIP_VN_KA_PERIOD);
Joe Eykholt97c83892009-03-17 11:42:40 -07001402 fip->ctlr_ka_time = jiffies + sel->fka_period;
Joe Eykholtd99ee452010-06-11 16:44:15 -07001403 if (time_after(next_timer, fip->ctlr_ka_time))
1404 next_timer = fip->ctlr_ka_time;
Joe Eykholt97c83892009-03-17 11:42:40 -07001405 } else {
Robert Love650bd122009-06-10 15:31:05 -07001406 printk(KERN_NOTICE "libfcoe: host%d: "
Robert Love70b51aa2009-11-03 11:47:45 -08001407 "FIP Fibre-Channel Forwarder timed out. "
Joe Eykholt97c83892009-03-17 11:42:40 -07001408 "Starting FCF discovery.\n",
1409 fip->lp->host->host_no);
Joe Eykholtfdb068c2010-07-20 15:19:47 -07001410 reset = 1;
Joe Eykholt97c83892009-03-17 11:42:40 -07001411 }
Joe Eykholt97c83892009-03-17 11:42:40 -07001412 }
1413
Yi Zou8cdffdc2009-11-20 14:54:57 -08001414 if (sel && !sel->fd_flags) {
Joe Eykholt97c83892009-03-17 11:42:40 -07001415 if (time_after_eq(jiffies, fip->ctlr_ka_time)) {
1416 fip->ctlr_ka_time = jiffies + sel->fka_period;
Joe Eykholtfdb068c2010-07-20 15:19:47 -07001417 send_ctlr_ka = 1;
Joe Eykholt97c83892009-03-17 11:42:40 -07001418 }
1419 if (time_after(next_timer, fip->ctlr_ka_time))
1420 next_timer = fip->ctlr_ka_time;
1421
1422 if (time_after_eq(jiffies, fip->port_ka_time)) {
Bhanu Prakash Gollapudif47dd852010-01-21 10:16:00 -08001423 fip->port_ka_time = jiffies +
Robert Love70b51aa2009-11-03 11:47:45 -08001424 msecs_to_jiffies(FIP_VN_KA_PERIOD);
Joe Eykholtfdb068c2010-07-20 15:19:47 -07001425 send_port_ka = 1;
Joe Eykholt97c83892009-03-17 11:42:40 -07001426 }
1427 if (time_after(next_timer, fip->port_ka_time))
1428 next_timer = fip->port_ka_time;
Joe Eykholt97c83892009-03-17 11:42:40 -07001429 }
Joe Eykholt8690cb82010-06-11 16:44:10 -07001430 if (!list_empty(&fip->fcfs))
1431 mod_timer(&fip->timer, next_timer);
Joe Eykholtfdb068c2010-07-20 15:19:47 -07001432 mutex_unlock(&fip->ctlr_mutex);
Joe Eykholt97c83892009-03-17 11:42:40 -07001433
Bhanu Prakash Gollapudi516a6482010-06-11 16:43:44 -07001434 if (reset) {
Joe Eykholtdd42dac2009-11-03 11:48:27 -08001435 fc_lport_reset(fip->lp);
Bhanu Prakash Gollapudi516a6482010-06-11 16:43:44 -07001436 /* restart things with a solicitation */
1437 fcoe_ctlr_solicit(fip, NULL);
1438 }
Chris Leech11b56182009-11-03 11:46:29 -08001439
Joe Eykholtfdb068c2010-07-20 15:19:47 -07001440 if (send_ctlr_ka)
Chris Leech11b56182009-11-03 11:46:29 -08001441 fcoe_ctlr_send_keep_alive(fip, NULL, 0, fip->ctl_src_addr);
Joe Eykholtfdb068c2010-07-20 15:19:47 -07001442
1443 if (send_port_ka) {
Chris Leech11b56182009-11-03 11:46:29 -08001444 mutex_lock(&fip->lp->lp_mutex);
1445 mac = fip->get_src_addr(fip->lp);
1446 fcoe_ctlr_send_keep_alive(fip, fip->lp, 1, mac);
1447 list_for_each_entry(vport, &fip->lp->vports, list) {
1448 mac = fip->get_src_addr(vport);
1449 fcoe_ctlr_send_keep_alive(fip, vport, 1, mac);
1450 }
1451 mutex_unlock(&fip->lp->lp_mutex);
1452 }
Joe Eykholt97c83892009-03-17 11:42:40 -07001453}
1454
1455/**
Robert Love70b51aa2009-11-03 11:47:45 -08001456 * fcoe_ctlr_recv_work() - Worker thread function for receiving FIP frames
1457 * @recv_work: Handle to a FCoE controller
Joe Eykholt97c83892009-03-17 11:42:40 -07001458 */
1459static void fcoe_ctlr_recv_work(struct work_struct *recv_work)
1460{
1461 struct fcoe_ctlr *fip;
1462 struct sk_buff *skb;
1463
1464 fip = container_of(recv_work, struct fcoe_ctlr, recv_work);
Joe Eykholt1f4aed82009-11-03 11:48:22 -08001465 while ((skb = skb_dequeue(&fip->fip_recv_list)))
Joe Eykholt97c83892009-03-17 11:42:40 -07001466 fcoe_ctlr_recv_handler(fip, skb);
Joe Eykholt97c83892009-03-17 11:42:40 -07001467}
1468
1469/**
Joe Eykholt386309ce2009-11-03 11:49:16 -08001470 * fcoe_ctlr_recv_flogi() - Snoop pre-FIP receipt of FLOGI response
Robert Love70b51aa2009-11-03 11:47:45 -08001471 * @fip: The FCoE controller
1472 * @fp: The FC frame to snoop
Joe Eykholt97c83892009-03-17 11:42:40 -07001473 *
1474 * Snoop potential response to FLOGI or even incoming FLOGI.
1475 *
1476 * The caller has checked that we are waiting for login as indicated
1477 * by fip->flogi_oxid != FC_XID_UNKNOWN.
1478 *
1479 * The caller is responsible for freeing the frame.
Joe Eykholt386309ce2009-11-03 11:49:16 -08001480 * Fill in the granted_mac address.
Joe Eykholt97c83892009-03-17 11:42:40 -07001481 *
1482 * Return non-zero if the frame should not be delivered to libfc.
1483 */
Chris Leech11b56182009-11-03 11:46:29 -08001484int fcoe_ctlr_recv_flogi(struct fcoe_ctlr *fip, struct fc_lport *lport,
Joe Eykholt386309ce2009-11-03 11:49:16 -08001485 struct fc_frame *fp)
Joe Eykholt97c83892009-03-17 11:42:40 -07001486{
1487 struct fc_frame_header *fh;
1488 u8 op;
Joe Eykholt386309ce2009-11-03 11:49:16 -08001489 u8 *sa;
Joe Eykholt97c83892009-03-17 11:42:40 -07001490
Joe Eykholt386309ce2009-11-03 11:49:16 -08001491 sa = eth_hdr(&fp->skb)->h_source;
Joe Eykholt97c83892009-03-17 11:42:40 -07001492 fh = fc_frame_header_get(fp);
1493 if (fh->fh_type != FC_TYPE_ELS)
1494 return 0;
1495
1496 op = fc_frame_payload_op(fp);
1497 if (op == ELS_LS_ACC && fh->fh_r_ctl == FC_RCTL_ELS_REP &&
1498 fip->flogi_oxid == ntohs(fh->fh_ox_id)) {
1499
Joe Eykholtfdb068c2010-07-20 15:19:47 -07001500 mutex_lock(&fip->ctlr_mutex);
Joe Eykholt97c83892009-03-17 11:42:40 -07001501 if (fip->state != FIP_ST_AUTO && fip->state != FIP_ST_NON_FIP) {
Joe Eykholtfdb068c2010-07-20 15:19:47 -07001502 mutex_unlock(&fip->ctlr_mutex);
Joe Eykholt97c83892009-03-17 11:42:40 -07001503 return -EINVAL;
1504 }
Joe Eykholt9b651da2010-07-20 15:20:24 -07001505 fcoe_ctlr_set_state(fip, FIP_ST_NON_FIP);
Joe Eykholt0f51c2e2009-11-03 11:48:16 -08001506 LIBFCOE_FIP_DBG(fip,
1507 "received FLOGI LS_ACC using non-FIP mode\n");
Joe Eykholt97c83892009-03-17 11:42:40 -07001508
1509 /*
1510 * FLOGI accepted.
1511 * If the src mac addr is FC_OUI-based, then we mark the
1512 * address_mode flag to use FC_OUI-based Ethernet DA.
1513 * Otherwise we use the FCoE gateway addr
1514 */
1515 if (!compare_ether_addr(sa, (u8[6])FC_FCOE_FLOGI_MAC)) {
1516 fip->map_dest = 1;
1517 } else {
1518 memcpy(fip->dest_addr, sa, ETH_ALEN);
1519 fip->map_dest = 0;
1520 }
1521 fip->flogi_oxid = FC_XID_UNKNOWN;
Joe Eykholtfdb068c2010-07-20 15:19:47 -07001522 mutex_unlock(&fip->ctlr_mutex);
Joe Eykholt386309ce2009-11-03 11:49:16 -08001523 fc_fcoe_set_mac(fr_cb(fp)->granted_mac, fh->fh_d_id);
Joe Eykholt97c83892009-03-17 11:42:40 -07001524 } else if (op == ELS_FLOGI && fh->fh_r_ctl == FC_RCTL_ELS_REQ && sa) {
1525 /*
1526 * Save source MAC for point-to-point responses.
1527 */
Joe Eykholtfdb068c2010-07-20 15:19:47 -07001528 mutex_lock(&fip->ctlr_mutex);
Joe Eykholt97c83892009-03-17 11:42:40 -07001529 if (fip->state == FIP_ST_AUTO || fip->state == FIP_ST_NON_FIP) {
1530 memcpy(fip->dest_addr, sa, ETH_ALEN);
1531 fip->map_dest = 0;
Joe Eykholte49bf612010-03-12 16:07:57 -08001532 if (fip->state == FIP_ST_AUTO)
1533 LIBFCOE_FIP_DBG(fip, "received non-FIP FLOGI. "
1534 "Setting non-FIP mode\n");
Joe Eykholt9b651da2010-07-20 15:20:24 -07001535 fcoe_ctlr_set_state(fip, FIP_ST_NON_FIP);
Joe Eykholt97c83892009-03-17 11:42:40 -07001536 }
Joe Eykholtfdb068c2010-07-20 15:19:47 -07001537 mutex_unlock(&fip->ctlr_mutex);
Joe Eykholt97c83892009-03-17 11:42:40 -07001538 }
1539 return 0;
1540}
1541EXPORT_SYMBOL(fcoe_ctlr_recv_flogi);
1542
Vasu Dev5e80f7f2009-03-17 11:42:18 -07001543/**
Robert Love70b51aa2009-11-03 11:47:45 -08001544 * fcoe_wwn_from_mac() - Converts a 48-bit IEEE MAC address to a 64-bit FC WWN
1545 * @mac: The MAC address to convert
1546 * @scheme: The scheme to use when converting
1547 * @port: The port indicator for converting
Vasu Dev5e80f7f2009-03-17 11:42:18 -07001548 *
1549 * Returns: u64 fc world wide name
1550 */
1551u64 fcoe_wwn_from_mac(unsigned char mac[MAX_ADDR_LEN],
1552 unsigned int scheme, unsigned int port)
1553{
1554 u64 wwn;
1555 u64 host_mac;
1556
1557 /* The MAC is in NO, so flip only the low 48 bits */
1558 host_mac = ((u64) mac[0] << 40) |
1559 ((u64) mac[1] << 32) |
1560 ((u64) mac[2] << 24) |
1561 ((u64) mac[3] << 16) |
1562 ((u64) mac[4] << 8) |
1563 (u64) mac[5];
1564
1565 WARN_ON(host_mac >= (1ULL << 48));
1566 wwn = host_mac | ((u64) scheme << 60);
1567 switch (scheme) {
1568 case 1:
1569 WARN_ON(port != 0);
1570 break;
1571 case 2:
1572 WARN_ON(port >= 0xfff);
1573 wwn |= (u64) port << 48;
1574 break;
1575 default:
1576 WARN_ON(1);
1577 break;
1578 }
1579
1580 return wwn;
1581}
1582EXPORT_SYMBOL_GPL(fcoe_wwn_from_mac);
1583
1584/**
Joe Eykholte10f8c62010-07-20 15:20:30 -07001585 * fcoe_ctlr_rport() - return the fcoe_rport for a given fc_rport_priv
1586 * @rdata: libfc remote port
1587 */
1588static inline struct fcoe_rport *fcoe_ctlr_rport(struct fc_rport_priv *rdata)
1589{
1590 return (struct fcoe_rport *)(rdata + 1);
1591}
1592
1593/**
1594 * fcoe_ctlr_vn_send() - Send a FIP VN2VN Probe Request or Reply.
1595 * @fip: The FCoE controller
1596 * @sub: sub-opcode for probe request, reply, or advertisement.
1597 * @dest: The destination Ethernet MAC address
1598 * @min_len: minimum size of the Ethernet payload to be sent
1599 */
1600static void fcoe_ctlr_vn_send(struct fcoe_ctlr *fip,
1601 enum fip_vn2vn_subcode sub,
1602 const u8 *dest, size_t min_len)
1603{
1604 struct sk_buff *skb;
1605 struct fip_frame {
1606 struct ethhdr eth;
1607 struct fip_header fip;
1608 struct fip_mac_desc mac;
1609 struct fip_wwn_desc wwnn;
1610 struct fip_vn_desc vn;
1611 } __attribute__((packed)) *frame;
1612 struct fip_fc4_feat *ff;
1613 struct fip_size_desc *size;
1614 u32 fcp_feat;
1615 size_t len;
1616 size_t dlen;
1617
1618 len = sizeof(*frame);
1619 dlen = 0;
1620 if (sub == FIP_SC_VN_CLAIM_NOTIFY || sub == FIP_SC_VN_CLAIM_REP) {
1621 dlen = sizeof(struct fip_fc4_feat) +
1622 sizeof(struct fip_size_desc);
1623 len += dlen;
1624 }
1625 dlen += sizeof(frame->mac) + sizeof(frame->wwnn) + sizeof(frame->vn);
1626 len = max(len, min_len + sizeof(struct ethhdr));
1627
1628 skb = dev_alloc_skb(len);
1629 if (!skb)
1630 return;
1631
1632 frame = (struct fip_frame *)skb->data;
1633 memset(frame, 0, len);
1634 memcpy(frame->eth.h_dest, dest, ETH_ALEN);
1635 memcpy(frame->eth.h_source, fip->ctl_src_addr, ETH_ALEN);
1636 frame->eth.h_proto = htons(ETH_P_FIP);
1637
1638 frame->fip.fip_ver = FIP_VER_ENCAPS(FIP_VER);
1639 frame->fip.fip_op = htons(FIP_OP_VN2VN);
1640 frame->fip.fip_subcode = sub;
1641 frame->fip.fip_dl_len = htons(dlen / FIP_BPW);
1642
1643 frame->mac.fd_desc.fip_dtype = FIP_DT_MAC;
1644 frame->mac.fd_desc.fip_dlen = sizeof(frame->mac) / FIP_BPW;
1645 memcpy(frame->mac.fd_mac, fip->ctl_src_addr, ETH_ALEN);
1646
1647 frame->wwnn.fd_desc.fip_dtype = FIP_DT_NAME;
1648 frame->wwnn.fd_desc.fip_dlen = sizeof(frame->wwnn) / FIP_BPW;
1649 put_unaligned_be64(fip->lp->wwnn, &frame->wwnn.fd_wwn);
1650
1651 frame->vn.fd_desc.fip_dtype = FIP_DT_VN_ID;
1652 frame->vn.fd_desc.fip_dlen = sizeof(frame->vn) / FIP_BPW;
1653 hton24(frame->vn.fd_mac, FIP_VN_FC_MAP);
1654 hton24(frame->vn.fd_mac + 3, fip->port_id);
1655 hton24(frame->vn.fd_fc_id, fip->port_id);
1656 put_unaligned_be64(fip->lp->wwpn, &frame->vn.fd_wwpn);
1657
1658 /*
1659 * For claims, add FC-4 features.
1660 * TBD: Add interface to get fc-4 types and features from libfc.
1661 */
1662 if (sub == FIP_SC_VN_CLAIM_NOTIFY || sub == FIP_SC_VN_CLAIM_REP) {
1663 ff = (struct fip_fc4_feat *)(frame + 1);
1664 ff->fd_desc.fip_dtype = FIP_DT_FC4F;
1665 ff->fd_desc.fip_dlen = sizeof(*ff) / FIP_BPW;
1666 ff->fd_fts = fip->lp->fcts;
1667
1668 fcp_feat = 0;
1669 if (fip->lp->service_params & FCP_SPPF_INIT_FCN)
1670 fcp_feat |= FCP_FEAT_INIT;
1671 if (fip->lp->service_params & FCP_SPPF_TARG_FCN)
1672 fcp_feat |= FCP_FEAT_TARG;
1673 fcp_feat <<= (FC_TYPE_FCP * 4) % 32;
1674 ff->fd_ff.fd_feat[FC_TYPE_FCP * 4 / 32] = htonl(fcp_feat);
1675
1676 size = (struct fip_size_desc *)(ff + 1);
1677 size->fd_desc.fip_dtype = FIP_DT_FCOE_SIZE;
1678 size->fd_desc.fip_dlen = sizeof(*size) / FIP_BPW;
1679 size->fd_size = htons(fcoe_ctlr_fcoe_size(fip));
1680 }
1681
1682 skb_put(skb, len);
1683 skb->protocol = htons(ETH_P_FIP);
1684 skb_reset_mac_header(skb);
1685 skb_reset_network_header(skb);
1686
1687 fip->send(fip, skb);
1688}
1689
1690/**
1691 * fcoe_ctlr_vn_rport_callback - Event handler for rport events.
1692 * @lport: The lport which is receiving the event
1693 * @rdata: remote port private data
1694 * @event: The event that occured
1695 *
1696 * Locking Note: The rport lock must not be held when calling this function.
1697 */
1698static void fcoe_ctlr_vn_rport_callback(struct fc_lport *lport,
1699 struct fc_rport_priv *rdata,
1700 enum fc_rport_event event)
1701{
1702 struct fcoe_ctlr *fip = lport->disc.priv;
1703 struct fcoe_rport *frport = fcoe_ctlr_rport(rdata);
1704
1705 LIBFCOE_FIP_DBG(fip, "vn_rport_callback %x event %d\n",
1706 rdata->ids.port_id, event);
1707
1708 mutex_lock(&fip->ctlr_mutex);
1709 switch (event) {
1710 case RPORT_EV_READY:
1711 frport->login_count = 0;
1712 break;
1713 case RPORT_EV_LOGO:
1714 case RPORT_EV_FAILED:
1715 case RPORT_EV_STOP:
1716 frport->login_count++;
1717 if (frport->login_count > FCOE_CTLR_VN2VN_LOGIN_LIMIT) {
1718 LIBFCOE_FIP_DBG(fip,
1719 "rport FLOGI limited port_id %6.6x\n",
1720 rdata->ids.port_id);
1721 lport->tt.rport_logoff(rdata);
1722 }
1723 break;
1724 default:
1725 break;
1726 }
1727 mutex_unlock(&fip->ctlr_mutex);
1728}
1729
1730static struct fc_rport_operations fcoe_ctlr_vn_rport_ops = {
1731 .event_callback = fcoe_ctlr_vn_rport_callback,
1732};
1733
1734/**
1735 * fcoe_ctlr_disc_stop_locked() - stop discovery in VN2VN mode
1736 * @fip: The FCoE controller
1737 *
1738 * Called with ctlr_mutex held.
1739 */
1740static void fcoe_ctlr_disc_stop_locked(struct fc_lport *lport)
1741{
1742 mutex_lock(&lport->disc.disc_mutex);
1743 lport->disc.disc_callback = NULL;
1744 mutex_unlock(&lport->disc.disc_mutex);
1745}
1746
1747/**
1748 * fcoe_ctlr_disc_stop() - stop discovery in VN2VN mode
1749 * @fip: The FCoE controller
1750 *
1751 * Called through the local port template for discovery.
1752 * Called without the ctlr_mutex held.
1753 */
1754static void fcoe_ctlr_disc_stop(struct fc_lport *lport)
1755{
1756 struct fcoe_ctlr *fip = lport->disc.priv;
1757
1758 mutex_lock(&fip->ctlr_mutex);
1759 fcoe_ctlr_disc_stop_locked(lport);
1760 mutex_unlock(&fip->ctlr_mutex);
1761}
1762
1763/**
1764 * fcoe_ctlr_disc_stop_final() - stop discovery for shutdown in VN2VN mode
1765 * @fip: The FCoE controller
1766 *
1767 * Called through the local port template for discovery.
1768 * Called without the ctlr_mutex held.
1769 */
1770static void fcoe_ctlr_disc_stop_final(struct fc_lport *lport)
1771{
1772 fcoe_ctlr_disc_stop(lport);
1773 lport->tt.rport_flush_queue();
1774 synchronize_rcu();
1775}
1776
1777/**
1778 * fcoe_ctlr_vn_restart() - VN2VN probe restart with new port_id
1779 * @fip: The FCoE controller
1780 *
1781 * Called with fcoe_ctlr lock held.
1782 */
1783static void fcoe_ctlr_vn_restart(struct fcoe_ctlr *fip)
1784{
1785 unsigned long wait;
1786 u32 port_id;
1787
1788 fcoe_ctlr_disc_stop_locked(fip->lp);
1789
1790 /*
1791 * Get proposed port ID.
1792 * If this is the first try after link up, use any previous port_id.
1793 * If there was none, use the low bits of the port_name.
1794 * On subsequent tries, get the next random one.
1795 * Don't use reserved IDs, use another non-zero value, just as random.
1796 */
1797 port_id = fip->port_id;
1798 if (fip->probe_tries)
1799 port_id = prandom32(&fip->rnd_state) & 0xffff;
1800 else if (!port_id)
1801 port_id = fip->lp->wwpn & 0xffff;
1802 if (!port_id || port_id == 0xffff)
1803 port_id = 1;
1804 fip->port_id = port_id;
1805
1806 if (fip->probe_tries < FIP_VN_RLIM_COUNT) {
1807 fip->probe_tries++;
1808 wait = random32() % FIP_VN_PROBE_WAIT;
1809 } else
1810 wait = FIP_VN_RLIM_INT;
1811 mod_timer(&fip->timer, jiffies + msecs_to_jiffies(wait));
1812 fcoe_ctlr_set_state(fip, FIP_ST_VNMP_START);
1813}
1814
1815/**
1816 * fcoe_ctlr_vn_start() - Start in VN2VN mode
1817 * @fip: The FCoE controller
1818 *
1819 * Called with fcoe_ctlr lock held.
1820 */
1821static void fcoe_ctlr_vn_start(struct fcoe_ctlr *fip)
1822{
1823 fip->probe_tries = 0;
1824 prandom32_seed(&fip->rnd_state, fip->lp->wwpn);
1825 fcoe_ctlr_vn_restart(fip);
1826}
1827
1828/**
1829 * fcoe_ctlr_vn_parse - parse probe request or response
1830 * @fip: The FCoE controller
1831 * @skb: incoming packet
1832 * @rdata: buffer for resulting parsed VN entry plus fcoe_rport
1833 *
1834 * Returns non-zero error number on error.
1835 * Does not consume the packet.
1836 */
1837static int fcoe_ctlr_vn_parse(struct fcoe_ctlr *fip,
1838 struct sk_buff *skb,
1839 struct fc_rport_priv *rdata)
1840{
1841 struct fip_header *fiph;
1842 struct fip_desc *desc = NULL;
1843 struct fip_mac_desc *macd = NULL;
1844 struct fip_wwn_desc *wwn = NULL;
1845 struct fip_vn_desc *vn = NULL;
1846 struct fip_size_desc *size = NULL;
1847 struct fcoe_rport *frport;
1848 size_t rlen;
1849 size_t dlen;
1850 u32 desc_mask = 0;
1851 u32 dtype;
1852 u8 sub;
1853
1854 memset(rdata, 0, sizeof(*rdata) + sizeof(*frport));
1855 frport = fcoe_ctlr_rport(rdata);
1856
1857 fiph = (struct fip_header *)skb->data;
1858 frport->flags = ntohs(fiph->fip_flags);
1859
1860 sub = fiph->fip_subcode;
1861 switch (sub) {
1862 case FIP_SC_VN_PROBE_REQ:
1863 case FIP_SC_VN_PROBE_REP:
1864 case FIP_SC_VN_BEACON:
1865 desc_mask = BIT(FIP_DT_MAC) | BIT(FIP_DT_NAME) |
1866 BIT(FIP_DT_VN_ID);
1867 break;
1868 case FIP_SC_VN_CLAIM_NOTIFY:
1869 case FIP_SC_VN_CLAIM_REP:
1870 desc_mask = BIT(FIP_DT_MAC) | BIT(FIP_DT_NAME) |
1871 BIT(FIP_DT_VN_ID) | BIT(FIP_DT_FC4F) |
1872 BIT(FIP_DT_FCOE_SIZE);
1873 break;
1874 default:
1875 LIBFCOE_FIP_DBG(fip, "vn_parse unknown subcode %u\n", sub);
1876 return -EINVAL;
1877 }
1878
1879 rlen = ntohs(fiph->fip_dl_len) * 4;
1880 if (rlen + sizeof(*fiph) > skb->len)
1881 return -EINVAL;
1882
1883 desc = (struct fip_desc *)(fiph + 1);
1884 while (rlen > 0) {
1885 dlen = desc->fip_dlen * FIP_BPW;
1886 if (dlen < sizeof(*desc) || dlen > rlen)
1887 return -EINVAL;
1888
1889 dtype = desc->fip_dtype;
1890 if (dtype < 32) {
1891 if (!(desc_mask & BIT(dtype))) {
1892 LIBFCOE_FIP_DBG(fip,
1893 "unexpected or duplicated desc "
1894 "desc type %u in "
1895 "FIP VN2VN subtype %u\n",
1896 dtype, sub);
1897 return -EINVAL;
1898 }
1899 desc_mask &= ~BIT(dtype);
1900 }
1901
1902 switch (dtype) {
1903 case FIP_DT_MAC:
1904 if (dlen != sizeof(struct fip_mac_desc))
1905 goto len_err;
1906 macd = (struct fip_mac_desc *)desc;
1907 if (!is_valid_ether_addr(macd->fd_mac)) {
1908 LIBFCOE_FIP_DBG(fip,
1909 "Invalid MAC addr %pM in FIP VN2VN\n",
1910 macd->fd_mac);
1911 return -EINVAL;
1912 }
1913 memcpy(frport->enode_mac, macd->fd_mac, ETH_ALEN);
1914 break;
1915 case FIP_DT_NAME:
1916 if (dlen != sizeof(struct fip_wwn_desc))
1917 goto len_err;
1918 wwn = (struct fip_wwn_desc *)desc;
1919 rdata->ids.node_name = get_unaligned_be64(&wwn->fd_wwn);
1920 break;
1921 case FIP_DT_VN_ID:
1922 if (dlen != sizeof(struct fip_vn_desc))
1923 goto len_err;
1924 vn = (struct fip_vn_desc *)desc;
1925 memcpy(frport->vn_mac, vn->fd_mac, ETH_ALEN);
1926 rdata->ids.port_id = ntoh24(vn->fd_fc_id);
1927 rdata->ids.port_name = get_unaligned_be64(&vn->fd_wwpn);
1928 break;
1929 case FIP_DT_FC4F:
1930 if (dlen != sizeof(struct fip_fc4_feat))
1931 goto len_err;
1932 break;
1933 case FIP_DT_FCOE_SIZE:
1934 if (dlen != sizeof(struct fip_size_desc))
1935 goto len_err;
1936 size = (struct fip_size_desc *)desc;
1937 frport->fcoe_len = ntohs(size->fd_size);
1938 break;
1939 default:
1940 LIBFCOE_FIP_DBG(fip, "unexpected descriptor type %x "
1941 "in FIP probe\n", dtype);
1942 /* standard says ignore unknown descriptors >= 128 */
1943 if (dtype < FIP_DT_VENDOR_BASE)
1944 return -EINVAL;
1945 break;
1946 }
1947 desc = (struct fip_desc *)((char *)desc + dlen);
1948 rlen -= dlen;
1949 }
1950 return 0;
1951
1952len_err:
1953 LIBFCOE_FIP_DBG(fip, "FIP length error in descriptor type %x len %zu\n",
1954 dtype, dlen);
1955 return -EINVAL;
1956}
1957
1958/**
1959 * fcoe_ctlr_vn_send_claim() - send multicast FIP VN2VN Claim Notification.
1960 * @fip: The FCoE controller
1961 *
1962 * Called with ctlr_mutex held.
1963 */
1964static void fcoe_ctlr_vn_send_claim(struct fcoe_ctlr *fip)
1965{
1966 fcoe_ctlr_vn_send(fip, FIP_SC_VN_CLAIM_NOTIFY, fcoe_all_vn2vn, 0);
1967 fip->sol_time = jiffies;
1968}
1969
1970/**
1971 * fcoe_ctlr_vn_probe_req() - handle incoming VN2VN probe request.
1972 * @fip: The FCoE controller
1973 * @rdata: parsed remote port with frport from the probe request
1974 *
1975 * Called with ctlr_mutex held.
1976 */
1977static void fcoe_ctlr_vn_probe_req(struct fcoe_ctlr *fip,
1978 struct fc_rport_priv *rdata)
1979{
1980 struct fcoe_rport *frport = fcoe_ctlr_rport(rdata);
1981
1982 if (rdata->ids.port_id != fip->port_id)
1983 return;
1984
1985 switch (fip->state) {
1986 case FIP_ST_VNMP_CLAIM:
1987 case FIP_ST_VNMP_UP:
1988 fcoe_ctlr_vn_send(fip, FIP_SC_VN_PROBE_REP,
1989 frport->enode_mac, 0);
1990 break;
1991 case FIP_ST_VNMP_PROBE1:
1992 case FIP_ST_VNMP_PROBE2:
1993 /*
1994 * Decide whether to reply to the Probe.
1995 * Our selected address is never a "recorded" one, so
1996 * only reply if our WWPN is greater and the
1997 * Probe's REC bit is not set.
1998 * If we don't reply, we will change our address.
1999 */
2000 if (fip->lp->wwpn > rdata->ids.port_name &&
2001 !(frport->flags & FIP_FL_REC_OR_P2P)) {
2002 fcoe_ctlr_vn_send(fip, FIP_SC_VN_PROBE_REP,
2003 frport->enode_mac, 0);
2004 break;
2005 }
2006 /* fall through */
2007 case FIP_ST_VNMP_START:
2008 fcoe_ctlr_vn_restart(fip);
2009 break;
2010 default:
2011 break;
2012 }
2013}
2014
2015/**
2016 * fcoe_ctlr_vn_probe_reply() - handle incoming VN2VN probe reply.
2017 * @fip: The FCoE controller
2018 * @rdata: parsed remote port with frport from the probe request
2019 *
2020 * Called with ctlr_mutex held.
2021 */
2022static void fcoe_ctlr_vn_probe_reply(struct fcoe_ctlr *fip,
2023 struct fc_rport_priv *rdata)
2024{
2025 if (rdata->ids.port_id != fip->port_id)
2026 return;
2027 switch (fip->state) {
2028 case FIP_ST_VNMP_START:
2029 case FIP_ST_VNMP_PROBE1:
2030 case FIP_ST_VNMP_PROBE2:
2031 case FIP_ST_VNMP_CLAIM:
2032 fcoe_ctlr_vn_restart(fip);
2033 break;
2034 case FIP_ST_VNMP_UP:
2035 fcoe_ctlr_vn_send_claim(fip);
2036 break;
2037 default:
2038 break;
2039 }
2040}
2041
2042/**
2043 * fcoe_ctlr_vn_add() - Add a VN2VN entry to the list, based on a claim reply.
2044 * @fip: The FCoE controller
2045 * @new: newly-parsed remote port with frport as a template for new rdata
2046 *
2047 * Called with ctlr_mutex held.
2048 */
2049static void fcoe_ctlr_vn_add(struct fcoe_ctlr *fip, struct fc_rport_priv *new)
2050{
2051 struct fc_lport *lport = fip->lp;
2052 struct fc_rport_priv *rdata;
2053 struct fc_rport_identifiers *ids;
2054 struct fcoe_rport *frport;
2055 u32 port_id;
2056
2057 port_id = new->ids.port_id;
2058 if (port_id == fip->port_id)
2059 return;
2060
2061 mutex_lock(&lport->disc.disc_mutex);
2062 rdata = lport->tt.rport_create(lport, port_id);
2063 if (!rdata) {
2064 mutex_unlock(&lport->disc.disc_mutex);
2065 return;
2066 }
2067
2068 rdata->ops = &fcoe_ctlr_vn_rport_ops;
2069 rdata->disc_id = lport->disc.disc_id;
2070
2071 ids = &rdata->ids;
2072 if ((ids->port_name != -1 && ids->port_name != new->ids.port_name) ||
2073 (ids->node_name != -1 && ids->node_name != new->ids.node_name))
2074 lport->tt.rport_logoff(rdata);
2075 ids->port_name = new->ids.port_name;
2076 ids->node_name = new->ids.node_name;
2077 mutex_unlock(&lport->disc.disc_mutex);
2078
2079 frport = fcoe_ctlr_rport(rdata);
2080 LIBFCOE_FIP_DBG(fip, "vn_add rport %6.6x %s\n",
2081 port_id, frport->fcoe_len ? "old" : "new");
2082 *frport = *fcoe_ctlr_rport(new);
2083 frport->time = 0;
2084}
2085
2086/**
2087 * fcoe_ctlr_vn_lookup() - Find VN remote port's MAC address
2088 * @fip: The FCoE controller
2089 * @port_id: The port_id of the remote VN_node
2090 * @mac: buffer which will hold the VN_NODE destination MAC address, if found.
2091 *
2092 * Returns non-zero error if no remote port found.
2093 */
2094static int fcoe_ctlr_vn_lookup(struct fcoe_ctlr *fip, u32 port_id, u8 *mac)
2095{
2096 struct fc_lport *lport = fip->lp;
2097 struct fc_rport_priv *rdata;
2098 struct fcoe_rport *frport;
2099 int ret = -1;
2100
2101 rcu_read_lock();
2102 rdata = lport->tt.rport_lookup(lport, port_id);
2103 if (rdata) {
2104 frport = fcoe_ctlr_rport(rdata);
2105 memcpy(mac, frport->enode_mac, ETH_ALEN);
2106 ret = 0;
2107 }
2108 rcu_read_unlock();
2109 return ret;
2110}
2111
2112/**
2113 * fcoe_ctlr_vn_claim_notify() - handle received FIP VN2VN Claim Notification
2114 * @fip: The FCoE controller
2115 * @new: newly-parsed remote port with frport as a template for new rdata
2116 *
2117 * Called with ctlr_mutex held.
2118 */
2119static void fcoe_ctlr_vn_claim_notify(struct fcoe_ctlr *fip,
2120 struct fc_rport_priv *new)
2121{
2122 struct fcoe_rport *frport = fcoe_ctlr_rport(new);
2123
2124 if (frport->flags & FIP_FL_REC_OR_P2P) {
2125 fcoe_ctlr_vn_send(fip, FIP_SC_VN_PROBE_REQ, fcoe_all_vn2vn, 0);
2126 return;
2127 }
2128 switch (fip->state) {
2129 case FIP_ST_VNMP_START:
2130 case FIP_ST_VNMP_PROBE1:
2131 case FIP_ST_VNMP_PROBE2:
2132 if (new->ids.port_id == fip->port_id)
2133 fcoe_ctlr_vn_restart(fip);
2134 break;
2135 case FIP_ST_VNMP_CLAIM:
2136 case FIP_ST_VNMP_UP:
2137 if (new->ids.port_id == fip->port_id) {
2138 if (new->ids.port_name > fip->lp->wwpn) {
2139 fcoe_ctlr_vn_restart(fip);
2140 break;
2141 }
2142 fcoe_ctlr_vn_send_claim(fip);
2143 break;
2144 }
2145 fcoe_ctlr_vn_send(fip, FIP_SC_VN_CLAIM_REP, frport->enode_mac,
2146 min((u32)frport->fcoe_len,
2147 fcoe_ctlr_fcoe_size(fip)));
2148 fcoe_ctlr_vn_add(fip, new);
2149 break;
2150 default:
2151 break;
2152 }
2153}
2154
2155/**
2156 * fcoe_ctlr_vn_claim_resp() - handle received Claim Response
2157 * @fip: The FCoE controller that received the frame
2158 * @new: newly-parsed remote port with frport from the Claim Response
2159 *
2160 * Called with ctlr_mutex held.
2161 */
2162static void fcoe_ctlr_vn_claim_resp(struct fcoe_ctlr *fip,
2163 struct fc_rport_priv *new)
2164{
2165 LIBFCOE_FIP_DBG(fip, "claim resp from from rport %x - state %s\n",
2166 new->ids.port_id, fcoe_ctlr_state(fip->state));
2167 if (fip->state == FIP_ST_VNMP_UP || fip->state == FIP_ST_VNMP_CLAIM)
2168 fcoe_ctlr_vn_add(fip, new);
2169}
2170
2171/**
2172 * fcoe_ctlr_vn_beacon() - handle received beacon.
2173 * @fip: The FCoE controller that received the frame
2174 * @new: newly-parsed remote port with frport from the Beacon
2175 *
2176 * Called with ctlr_mutex held.
2177 */
2178static void fcoe_ctlr_vn_beacon(struct fcoe_ctlr *fip,
2179 struct fc_rport_priv *new)
2180{
2181 struct fc_lport *lport = fip->lp;
2182 struct fc_rport_priv *rdata;
2183 struct fcoe_rport *frport;
2184
2185 frport = fcoe_ctlr_rport(new);
2186 if (frport->flags & FIP_FL_REC_OR_P2P) {
2187 fcoe_ctlr_vn_send(fip, FIP_SC_VN_PROBE_REQ, fcoe_all_vn2vn, 0);
2188 return;
2189 }
2190 mutex_lock(&lport->disc.disc_mutex);
2191 rdata = lport->tt.rport_lookup(lport, new->ids.port_id);
2192 if (rdata)
2193 kref_get(&rdata->kref);
2194 mutex_unlock(&lport->disc.disc_mutex);
2195 if (rdata) {
2196 if (rdata->ids.node_name == new->ids.node_name &&
2197 rdata->ids.port_name == new->ids.port_name) {
2198 frport = fcoe_ctlr_rport(rdata);
2199 if (!frport->time && fip->state == FIP_ST_VNMP_UP)
2200 lport->tt.rport_login(rdata);
2201 frport->time = jiffies;
2202 }
2203 kref_put(&rdata->kref, lport->tt.rport_destroy);
2204 return;
2205 }
2206 if (fip->state != FIP_ST_VNMP_UP)
2207 return;
2208
2209 /*
2210 * Beacon from a new neighbor.
2211 * Send a claim notify if one hasn't been sent recently.
2212 * Don't add the neighbor yet.
2213 */
2214 LIBFCOE_FIP_DBG(fip, "beacon from new rport %x. sending claim notify\n",
2215 new->ids.port_id);
2216 if (time_after(jiffies,
2217 fip->sol_time + msecs_to_jiffies(FIP_VN_ANN_WAIT)))
2218 fcoe_ctlr_vn_send_claim(fip);
2219}
2220
2221/**
2222 * fcoe_ctlr_vn_age() - Check for VN_ports without recent beacons
2223 * @fip: The FCoE controller
2224 *
2225 * Called with ctlr_mutex held.
2226 * Called only in state FIP_ST_VNMP_UP.
2227 * Returns the soonest time for next age-out or a time far in the future.
2228 */
2229static unsigned long fcoe_ctlr_vn_age(struct fcoe_ctlr *fip)
2230{
2231 struct fc_lport *lport = fip->lp;
2232 struct fc_rport_priv *rdata;
2233 struct fcoe_rport *frport;
2234 unsigned long next_time;
2235 unsigned long deadline;
2236
2237 next_time = jiffies + msecs_to_jiffies(FIP_VN_BEACON_INT * 10);
2238 mutex_lock(&lport->disc.disc_mutex);
2239 list_for_each_entry_rcu(rdata, &lport->disc.rports, peers) {
2240 frport = fcoe_ctlr_rport(rdata);
2241 if (!frport->time)
2242 continue;
2243 deadline = frport->time +
2244 msecs_to_jiffies(FIP_VN_BEACON_INT * 25 / 10);
2245 if (time_after_eq(jiffies, deadline)) {
2246 frport->time = 0;
2247 LIBFCOE_FIP_DBG(fip,
2248 "port %16.16llx fc_id %6.6x beacon expired\n",
2249 rdata->ids.port_name, rdata->ids.port_id);
2250 lport->tt.rport_logoff(rdata);
2251 } else if (time_before(deadline, next_time))
2252 next_time = deadline;
2253 }
2254 mutex_unlock(&lport->disc.disc_mutex);
2255 return next_time;
2256}
2257
2258/**
2259 * fcoe_ctlr_vn_recv() - Receive a FIP frame
2260 * @fip: The FCoE controller that received the frame
2261 * @skb: The received FIP frame
2262 *
2263 * Returns non-zero if the frame is dropped.
2264 * Always consumes the frame.
2265 */
2266static int fcoe_ctlr_vn_recv(struct fcoe_ctlr *fip, struct sk_buff *skb)
2267{
2268 struct fip_header *fiph;
2269 enum fip_vn2vn_subcode sub;
2270 union {
2271 struct fc_rport_priv rdata;
2272 struct fcoe_rport frport;
2273 } buf;
2274 int rc;
2275
2276 fiph = (struct fip_header *)skb->data;
2277 sub = fiph->fip_subcode;
2278
2279 rc = fcoe_ctlr_vn_parse(fip, skb, &buf.rdata);
2280 if (rc) {
2281 LIBFCOE_FIP_DBG(fip, "vn_recv vn_parse error %d\n", rc);
2282 goto drop;
2283 }
2284
2285 mutex_lock(&fip->ctlr_mutex);
2286 switch (sub) {
2287 case FIP_SC_VN_PROBE_REQ:
2288 fcoe_ctlr_vn_probe_req(fip, &buf.rdata);
2289 break;
2290 case FIP_SC_VN_PROBE_REP:
2291 fcoe_ctlr_vn_probe_reply(fip, &buf.rdata);
2292 break;
2293 case FIP_SC_VN_CLAIM_NOTIFY:
2294 fcoe_ctlr_vn_claim_notify(fip, &buf.rdata);
2295 break;
2296 case FIP_SC_VN_CLAIM_REP:
2297 fcoe_ctlr_vn_claim_resp(fip, &buf.rdata);
2298 break;
2299 case FIP_SC_VN_BEACON:
2300 fcoe_ctlr_vn_beacon(fip, &buf.rdata);
2301 break;
2302 default:
2303 LIBFCOE_FIP_DBG(fip, "vn_recv unknown subcode %d\n", sub);
2304 rc = -1;
2305 break;
2306 }
2307 mutex_unlock(&fip->ctlr_mutex);
2308drop:
2309 kfree_skb(skb);
2310 return rc;
2311}
2312
2313/**
2314 * fcoe_ctlr_disc_recv - discovery receive handler for VN2VN mode.
2315 * @fip: The FCoE controller
2316 *
2317 * This should never be called since we don't see RSCNs or other
2318 * fabric-generated ELSes.
2319 */
2320static void fcoe_ctlr_disc_recv(struct fc_seq *seq, struct fc_frame *fp,
2321 struct fc_lport *lport)
2322{
2323 struct fc_seq_els_data rjt_data;
2324
2325 rjt_data.fp = NULL;
2326 rjt_data.reason = ELS_RJT_UNSUP;
2327 rjt_data.explan = ELS_EXPL_NONE;
2328 lport->tt.seq_els_rsp_send(seq, ELS_LS_RJT, &rjt_data);
2329 fc_frame_free(fp);
2330}
2331
2332/**
2333 * fcoe_ctlr_disc_recv - start discovery for VN2VN mode.
2334 * @fip: The FCoE controller
2335 *
2336 * This sets a flag indicating that remote ports should be created
2337 * and started for the peers we discover. We use the disc_callback
2338 * pointer as that flag. Peers already discovered are created here.
2339 *
2340 * The lport lock is held during this call. The callback must be done
2341 * later, without holding either the lport or discovery locks.
2342 * The fcoe_ctlr lock may also be held during this call.
2343 */
2344static void fcoe_ctlr_disc_start(void (*callback)(struct fc_lport *,
2345 enum fc_disc_event),
2346 struct fc_lport *lport)
2347{
2348 struct fc_disc *disc = &lport->disc;
2349 struct fcoe_ctlr *fip = disc->priv;
2350
2351 mutex_lock(&disc->disc_mutex);
2352 disc->disc_callback = callback;
2353 disc->disc_id = (disc->disc_id + 2) | 1;
2354 disc->pending = 1;
2355 schedule_work(&fip->timer_work);
2356 mutex_unlock(&disc->disc_mutex);
2357}
2358
2359/**
2360 * fcoe_ctlr_vn_disc() - report FIP VN_port discovery results after claim state.
2361 * @fip: The FCoE controller
2362 *
2363 * Starts the FLOGI and PLOGI login process to each discovered rport for which
2364 * we've received at least one beacon.
2365 * Performs the discovery complete callback.
2366 */
2367static void fcoe_ctlr_vn_disc(struct fcoe_ctlr *fip)
2368{
2369 struct fc_lport *lport = fip->lp;
2370 struct fc_disc *disc = &lport->disc;
2371 struct fc_rport_priv *rdata;
2372 struct fcoe_rport *frport;
2373 void (*callback)(struct fc_lport *, enum fc_disc_event);
2374
2375 mutex_lock(&disc->disc_mutex);
2376 callback = disc->pending ? disc->disc_callback : NULL;
2377 disc->pending = 0;
2378 list_for_each_entry_rcu(rdata, &disc->rports, peers) {
2379 frport = fcoe_ctlr_rport(rdata);
2380 if (frport->time)
2381 lport->tt.rport_login(rdata);
2382 }
2383 mutex_unlock(&disc->disc_mutex);
2384 if (callback)
2385 callback(lport, DISC_EV_SUCCESS);
2386}
2387
2388/**
2389 * fcoe_ctlr_vn_timeout - timer work function for VN2VN mode.
2390 * @fip: The FCoE controller
2391 */
2392static void fcoe_ctlr_vn_timeout(struct fcoe_ctlr *fip)
2393{
2394 unsigned long next_time;
2395 u8 mac[ETH_ALEN];
2396 u32 new_port_id = 0;
2397
2398 mutex_lock(&fip->ctlr_mutex);
2399 switch (fip->state) {
2400 case FIP_ST_VNMP_START:
2401 fcoe_ctlr_set_state(fip, FIP_ST_VNMP_PROBE1);
2402 fcoe_ctlr_vn_send(fip, FIP_SC_VN_PROBE_REQ, fcoe_all_vn2vn, 0);
2403 next_time = jiffies + msecs_to_jiffies(FIP_VN_PROBE_WAIT);
2404 break;
2405 case FIP_ST_VNMP_PROBE1:
2406 fcoe_ctlr_set_state(fip, FIP_ST_VNMP_PROBE2);
2407 fcoe_ctlr_vn_send(fip, FIP_SC_VN_PROBE_REQ, fcoe_all_vn2vn, 0);
2408 next_time = jiffies + msecs_to_jiffies(FIP_VN_ANN_WAIT);
2409 break;
2410 case FIP_ST_VNMP_PROBE2:
2411 fcoe_ctlr_set_state(fip, FIP_ST_VNMP_CLAIM);
2412 new_port_id = fip->port_id;
2413 hton24(mac, FIP_VN_FC_MAP);
2414 hton24(mac + 3, new_port_id);
2415 fip->update_mac(fip->lp, mac);
2416 fcoe_ctlr_vn_send_claim(fip);
2417 next_time = jiffies + msecs_to_jiffies(FIP_VN_ANN_WAIT);
2418 break;
2419 case FIP_ST_VNMP_CLAIM:
2420 /*
2421 * This may be invoked either by starting discovery so don't
2422 * go to the next state unless it's been long enough.
2423 */
2424 next_time = fip->sol_time + msecs_to_jiffies(FIP_VN_ANN_WAIT);
2425 if (time_after_eq(jiffies, next_time)) {
2426 fcoe_ctlr_set_state(fip, FIP_ST_VNMP_UP);
2427 fcoe_ctlr_vn_send(fip, FIP_SC_VN_BEACON,
2428 fcoe_all_vn2vn, 0);
2429 next_time = jiffies + msecs_to_jiffies(FIP_VN_ANN_WAIT);
2430 fip->port_ka_time = next_time;
2431 }
2432 fcoe_ctlr_vn_disc(fip);
2433 break;
2434 case FIP_ST_VNMP_UP:
2435 next_time = fcoe_ctlr_vn_age(fip);
2436 if (time_after_eq(jiffies, fip->port_ka_time)) {
2437 fcoe_ctlr_vn_send(fip, FIP_SC_VN_BEACON,
2438 fcoe_all_vn2vn, 0);
2439 fip->port_ka_time = jiffies +
2440 msecs_to_jiffies(FIP_VN_BEACON_INT +
2441 (random32() % FIP_VN_BEACON_FUZZ));
2442 }
2443 if (time_before(fip->port_ka_time, next_time))
2444 next_time = fip->port_ka_time;
2445 break;
2446 case FIP_ST_LINK_WAIT:
2447 goto unlock;
2448 default:
2449 WARN(1, "unexpected state %d", fip->state);
2450 goto unlock;
2451 }
2452 mod_timer(&fip->timer, next_time);
2453unlock:
2454 mutex_unlock(&fip->ctlr_mutex);
2455
2456 /* If port ID is new, notify local port after dropping ctlr_mutex */
2457 if (new_port_id)
2458 fc_lport_set_local_id(fip->lp, new_port_id);
2459}
2460
2461/**
Robert Love70b51aa2009-11-03 11:47:45 -08002462 * fcoe_libfc_config() - Sets up libfc related properties for local port
2463 * @lp: The local port to configure libfc for
Joe Eykholte10f8c62010-07-20 15:20:30 -07002464 * @fip: The FCoE controller in use by the local port
Robert Love70b51aa2009-11-03 11:47:45 -08002465 * @tt: The libfc function template
Joe Eykholte10f8c62010-07-20 15:20:30 -07002466 * @init_fcp: If non-zero, the FCP portion of libfc should be initialized
Vasu Dev5e80f7f2009-03-17 11:42:18 -07002467 *
2468 * Returns : 0 for success
2469 */
Joe Eykholte10f8c62010-07-20 15:20:30 -07002470int fcoe_libfc_config(struct fc_lport *lport, struct fcoe_ctlr *fip,
2471 const struct libfc_function_template *tt, int init_fcp)
Vasu Dev5e80f7f2009-03-17 11:42:18 -07002472{
2473 /* Set the function pointers set by the LLDD */
Robert Love70b51aa2009-11-03 11:47:45 -08002474 memcpy(&lport->tt, tt, sizeof(*tt));
Joe Eykholte10f8c62010-07-20 15:20:30 -07002475 if (init_fcp && fc_fcp_init(lport))
Vasu Dev5e80f7f2009-03-17 11:42:18 -07002476 return -ENOMEM;
Robert Love70b51aa2009-11-03 11:47:45 -08002477 fc_exch_init(lport);
2478 fc_elsct_init(lport);
2479 fc_lport_init(lport);
Joe Eykholte10f8c62010-07-20 15:20:30 -07002480 if (fip->mode == FIP_MODE_VN2VN)
2481 lport->rport_priv_size = sizeof(struct fcoe_rport);
Robert Love70b51aa2009-11-03 11:47:45 -08002482 fc_rport_init(lport);
Joe Eykholte10f8c62010-07-20 15:20:30 -07002483 if (fip->mode == FIP_MODE_VN2VN) {
2484 lport->point_to_multipoint = 1;
2485 lport->tt.disc_recv_req = fcoe_ctlr_disc_recv;
2486 lport->tt.disc_start = fcoe_ctlr_disc_start;
2487 lport->tt.disc_stop = fcoe_ctlr_disc_stop;
2488 lport->tt.disc_stop_final = fcoe_ctlr_disc_stop_final;
2489 mutex_init(&lport->disc.disc_mutex);
2490 INIT_LIST_HEAD(&lport->disc.rports);
2491 lport->disc.priv = fip;
2492 } else {
2493 fc_disc_init(lport);
2494 }
Vasu Dev5e80f7f2009-03-17 11:42:18 -07002495 return 0;
2496}
2497EXPORT_SYMBOL_GPL(fcoe_libfc_config);