blob: bc17c71232023c7d3b830a541df976de7146a72c [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/**
Joe Eykholtcd229e42010-07-20 15:20:40 -0700156 * fcoe_ctlr_map_dest() - Set flag and OUI for mapping destination addresses
157 * @fip: The FCoE controller
158 */
159static void fcoe_ctlr_map_dest(struct fcoe_ctlr *fip)
160{
161 if (fip->mode == FIP_MODE_VN2VN)
162 hton24(fip->dest_addr, FIP_VN_FC_MAP);
163 else
164 hton24(fip->dest_addr, FIP_DEF_FC_MAP);
165 hton24(fip->dest_addr + 3, 0);
166 fip->map_dest = 1;
167}
168
169/**
Robert Love70b51aa2009-11-03 11:47:45 -0800170 * fcoe_ctlr_init() - Initialize the FCoE Controller instance
171 * @fip: The FCoE controller to initialize
Joe Eykholt97c83892009-03-17 11:42:40 -0700172 */
Joe Eykholt3d902ac2010-07-20 15:19:58 -0700173void fcoe_ctlr_init(struct fcoe_ctlr *fip, enum fip_state mode)
Joe Eykholt97c83892009-03-17 11:42:40 -0700174{
Joe Eykholt9b651da2010-07-20 15:20:24 -0700175 fcoe_ctlr_set_state(fip, FIP_ST_LINK_WAIT);
Joe Eykholt3d902ac2010-07-20 15:19:58 -0700176 fip->mode = mode;
Joe Eykholt97c83892009-03-17 11:42:40 -0700177 INIT_LIST_HEAD(&fip->fcfs);
Joe Eykholtfdb068c2010-07-20 15:19:47 -0700178 mutex_init(&fip->ctlr_mutex);
Joe Eykholt97c83892009-03-17 11:42:40 -0700179 fip->flogi_oxid = FC_XID_UNKNOWN;
180 setup_timer(&fip->timer, fcoe_ctlr_timeout, (unsigned long)fip);
Joe Eykholt42913652010-03-12 16:08:23 -0800181 INIT_WORK(&fip->timer_work, fcoe_ctlr_timer_work);
Joe Eykholt97c83892009-03-17 11:42:40 -0700182 INIT_WORK(&fip->recv_work, fcoe_ctlr_recv_work);
183 skb_queue_head_init(&fip->fip_recv_list);
184}
185EXPORT_SYMBOL(fcoe_ctlr_init);
186
187/**
Robert Love70b51aa2009-11-03 11:47:45 -0800188 * fcoe_ctlr_reset_fcfs() - Reset and free all FCFs for a controller
189 * @fip: The FCoE controller whose FCFs are to be reset
Joe Eykholt97c83892009-03-17 11:42:40 -0700190 *
191 * Called with &fcoe_ctlr lock held.
192 */
193static void fcoe_ctlr_reset_fcfs(struct fcoe_ctlr *fip)
194{
195 struct fcoe_fcf *fcf;
196 struct fcoe_fcf *next;
197
198 fip->sel_fcf = NULL;
199 list_for_each_entry_safe(fcf, next, &fip->fcfs, list) {
200 list_del(&fcf->list);
201 kfree(fcf);
202 }
203 fip->fcf_count = 0;
204 fip->sel_time = 0;
205}
206
207/**
Robert Love70b51aa2009-11-03 11:47:45 -0800208 * fcoe_ctlr_destroy() - Disable and tear down a FCoE controller
209 * @fip: The FCoE controller to tear down
Joe Eykholt97c83892009-03-17 11:42:40 -0700210 *
211 * This is called by FCoE drivers before freeing the &fcoe_ctlr.
212 *
213 * The receive handler will have been deleted before this to guarantee
214 * that no more recv_work will be scheduled.
215 *
216 * The timer routine will simply return once we set FIP_ST_DISABLED.
217 * This guarantees that no further timeouts or work will be scheduled.
218 */
219void fcoe_ctlr_destroy(struct fcoe_ctlr *fip)
220{
Chris Leecha4b7cfa2009-08-25 13:59:08 -0700221 cancel_work_sync(&fip->recv_work);
Joe Eykholt1f4aed82009-11-03 11:48:22 -0800222 skb_queue_purge(&fip->fip_recv_list);
Chris Leecha4b7cfa2009-08-25 13:59:08 -0700223
Joe Eykholtfdb068c2010-07-20 15:19:47 -0700224 mutex_lock(&fip->ctlr_mutex);
Joe Eykholt9b651da2010-07-20 15:20:24 -0700225 fcoe_ctlr_set_state(fip, FIP_ST_DISABLED);
Joe Eykholt97c83892009-03-17 11:42:40 -0700226 fcoe_ctlr_reset_fcfs(fip);
Joe Eykholtfdb068c2010-07-20 15:19:47 -0700227 mutex_unlock(&fip->ctlr_mutex);
Joe Eykholt97c83892009-03-17 11:42:40 -0700228 del_timer_sync(&fip->timer);
Joe Eykholt42913652010-03-12 16:08:23 -0800229 cancel_work_sync(&fip->timer_work);
Joe Eykholt97c83892009-03-17 11:42:40 -0700230}
231EXPORT_SYMBOL(fcoe_ctlr_destroy);
232
233/**
Robert Love70b51aa2009-11-03 11:47:45 -0800234 * fcoe_ctlr_fcoe_size() - Return the maximum FCoE size required for VN_Port
235 * @fip: The FCoE controller to get the maximum FCoE size from
Joe Eykholt97c83892009-03-17 11:42:40 -0700236 *
237 * Returns the maximum packet size including the FCoE header and trailer,
238 * but not including any Ethernet or VLAN headers.
239 */
240static inline u32 fcoe_ctlr_fcoe_size(struct fcoe_ctlr *fip)
241{
242 /*
243 * Determine the max FCoE frame size allowed, including
244 * FCoE header and trailer.
245 * Note: lp->mfs is currently the payload size, not the frame size.
246 */
247 return fip->lp->mfs + sizeof(struct fc_frame_header) +
248 sizeof(struct fcoe_hdr) + sizeof(struct fcoe_crc_eof);
249}
250
251/**
Robert Love70b51aa2009-11-03 11:47:45 -0800252 * fcoe_ctlr_solicit() - Send a FIP solicitation
253 * @fip: The FCoE controller to send the solicitation on
254 * @fcf: The destination FCF (if NULL, a multicast solicitation is sent)
Joe Eykholt97c83892009-03-17 11:42:40 -0700255 */
256static void fcoe_ctlr_solicit(struct fcoe_ctlr *fip, struct fcoe_fcf *fcf)
257{
258 struct sk_buff *skb;
259 struct fip_sol {
260 struct ethhdr eth;
261 struct fip_header fip;
262 struct {
263 struct fip_mac_desc mac;
264 struct fip_wwn_desc wwnn;
265 struct fip_size_desc size;
266 } __attribute__((packed)) desc;
267 } __attribute__((packed)) *sol;
268 u32 fcoe_size;
269
270 skb = dev_alloc_skb(sizeof(*sol));
271 if (!skb)
272 return;
273
274 sol = (struct fip_sol *)skb->data;
275
276 memset(sol, 0, sizeof(*sol));
277 memcpy(sol->eth.h_dest, fcf ? fcf->fcf_mac : fcoe_all_fcfs, ETH_ALEN);
278 memcpy(sol->eth.h_source, fip->ctl_src_addr, ETH_ALEN);
279 sol->eth.h_proto = htons(ETH_P_FIP);
280
281 sol->fip.fip_ver = FIP_VER_ENCAPS(FIP_VER);
282 sol->fip.fip_op = htons(FIP_OP_DISC);
283 sol->fip.fip_subcode = FIP_SC_SOL;
284 sol->fip.fip_dl_len = htons(sizeof(sol->desc) / FIP_BPW);
285 sol->fip.fip_flags = htons(FIP_FL_FPMA);
Vasu Dev184dd342009-05-17 12:33:28 +0000286 if (fip->spma)
287 sol->fip.fip_flags |= htons(FIP_FL_SPMA);
Joe Eykholt97c83892009-03-17 11:42:40 -0700288
289 sol->desc.mac.fd_desc.fip_dtype = FIP_DT_MAC;
290 sol->desc.mac.fd_desc.fip_dlen = sizeof(sol->desc.mac) / FIP_BPW;
291 memcpy(sol->desc.mac.fd_mac, fip->ctl_src_addr, ETH_ALEN);
292
293 sol->desc.wwnn.fd_desc.fip_dtype = FIP_DT_NAME;
294 sol->desc.wwnn.fd_desc.fip_dlen = sizeof(sol->desc.wwnn) / FIP_BPW;
295 put_unaligned_be64(fip->lp->wwnn, &sol->desc.wwnn.fd_wwn);
296
297 fcoe_size = fcoe_ctlr_fcoe_size(fip);
298 sol->desc.size.fd_desc.fip_dtype = FIP_DT_FCOE_SIZE;
299 sol->desc.size.fd_desc.fip_dlen = sizeof(sol->desc.size) / FIP_BPW;
300 sol->desc.size.fd_size = htons(fcoe_size);
301
302 skb_put(skb, sizeof(*sol));
Chris Leech0f491532009-05-06 10:52:18 -0700303 skb->protocol = htons(ETH_P_FIP);
Joe Eykholt97c83892009-03-17 11:42:40 -0700304 skb_reset_mac_header(skb);
305 skb_reset_network_header(skb);
306 fip->send(fip, skb);
307
308 if (!fcf)
309 fip->sol_time = jiffies;
310}
311
312/**
Robert Love70b51aa2009-11-03 11:47:45 -0800313 * fcoe_ctlr_link_up() - Start FCoE controller
314 * @fip: The FCoE controller to start
Joe Eykholt97c83892009-03-17 11:42:40 -0700315 *
316 * Called from the LLD when the network link is ready.
317 */
318void fcoe_ctlr_link_up(struct fcoe_ctlr *fip)
319{
Joe Eykholtfdb068c2010-07-20 15:19:47 -0700320 mutex_lock(&fip->ctlr_mutex);
Joe Eykholt97c83892009-03-17 11:42:40 -0700321 if (fip->state == FIP_ST_NON_FIP || fip->state == FIP_ST_AUTO) {
Joe Eykholtfdb068c2010-07-20 15:19:47 -0700322 mutex_unlock(&fip->ctlr_mutex);
Joe Eykholt97c83892009-03-17 11:42:40 -0700323 fc_linkup(fip->lp);
324 } else if (fip->state == FIP_ST_LINK_WAIT) {
Joe Eykholt9b651da2010-07-20 15:20:24 -0700325 fcoe_ctlr_set_state(fip, fip->mode);
Joe Eykholte10f8c62010-07-20 15:20:30 -0700326 switch (fip->mode) {
327 default:
328 LIBFCOE_FIP_DBG(fip, "invalid mode %d\n", fip->mode);
329 /* fall-through */
330 case FIP_MODE_AUTO:
Joe Eykholt0f51c2e2009-11-03 11:48:16 -0800331 LIBFCOE_FIP_DBG(fip, "%s", "setting AUTO mode.\n");
Joe Eykholte10f8c62010-07-20 15:20:30 -0700332 /* fall-through */
333 case FIP_MODE_FABRIC:
334 case FIP_MODE_NON_FIP:
335 mutex_unlock(&fip->ctlr_mutex);
336 fc_linkup(fip->lp);
337 fcoe_ctlr_solicit(fip, NULL);
338 break;
339 case FIP_MODE_VN2VN:
340 fcoe_ctlr_vn_start(fip);
341 mutex_unlock(&fip->ctlr_mutex);
342 fc_linkup(fip->lp);
343 break;
344 }
Joe Eykholt97c83892009-03-17 11:42:40 -0700345 } else
Joe Eykholtfdb068c2010-07-20 15:19:47 -0700346 mutex_unlock(&fip->ctlr_mutex);
Joe Eykholt97c83892009-03-17 11:42:40 -0700347}
348EXPORT_SYMBOL(fcoe_ctlr_link_up);
349
350/**
Robert Love70b51aa2009-11-03 11:47:45 -0800351 * fcoe_ctlr_reset() - Reset a FCoE controller
352 * @fip: The FCoE controller to reset
Joe Eykholt97c83892009-03-17 11:42:40 -0700353 */
Joe Eykholtdd42dac2009-11-03 11:48:27 -0800354static void fcoe_ctlr_reset(struct fcoe_ctlr *fip)
Joe Eykholt97c83892009-03-17 11:42:40 -0700355{
Joe Eykholt97c83892009-03-17 11:42:40 -0700356 fcoe_ctlr_reset_fcfs(fip);
357 del_timer(&fip->timer);
Joe Eykholt97c83892009-03-17 11:42:40 -0700358 fip->ctlr_ka_time = 0;
359 fip->port_ka_time = 0;
360 fip->sol_time = 0;
361 fip->flogi_oxid = FC_XID_UNKNOWN;
Joe Eykholtcd229e42010-07-20 15:20:40 -0700362 fcoe_ctlr_map_dest(fip);
Joe Eykholt97c83892009-03-17 11:42:40 -0700363}
364
365/**
Robert Love70b51aa2009-11-03 11:47:45 -0800366 * fcoe_ctlr_link_down() - Stop a FCoE controller
367 * @fip: The FCoE controller to be stopped
Joe Eykholt97c83892009-03-17 11:42:40 -0700368 *
369 * Returns non-zero if the link was up and now isn't.
370 *
371 * Called from the LLD when the network link is not ready.
372 * There may be multiple calls while the link is down.
373 */
374int fcoe_ctlr_link_down(struct fcoe_ctlr *fip)
375{
Joe Eykholtdd42dac2009-11-03 11:48:27 -0800376 int link_dropped;
377
378 LIBFCOE_FIP_DBG(fip, "link down.\n");
Joe Eykholtfdb068c2010-07-20 15:19:47 -0700379 mutex_lock(&fip->ctlr_mutex);
Joe Eykholtdd42dac2009-11-03 11:48:27 -0800380 fcoe_ctlr_reset(fip);
Joe Eykholt42913652010-03-12 16:08:23 -0800381 link_dropped = fip->state != FIP_ST_LINK_WAIT;
Joe Eykholt9b651da2010-07-20 15:20:24 -0700382 fcoe_ctlr_set_state(fip, FIP_ST_LINK_WAIT);
Joe Eykholtfdb068c2010-07-20 15:19:47 -0700383 mutex_unlock(&fip->ctlr_mutex);
Joe Eykholtdd42dac2009-11-03 11:48:27 -0800384
385 if (link_dropped)
386 fc_linkdown(fip->lp);
387 return link_dropped;
Joe Eykholt97c83892009-03-17 11:42:40 -0700388}
389EXPORT_SYMBOL(fcoe_ctlr_link_down);
390
391/**
Robert Love70b51aa2009-11-03 11:47:45 -0800392 * fcoe_ctlr_send_keep_alive() - Send a keep-alive to the selected FCF
393 * @fip: The FCoE controller to send the FKA on
394 * @lport: libfc fc_lport to send from
395 * @ports: 0 for controller keep-alive, 1 for port keep-alive
396 * @sa: The source MAC address
Joe Eykholt97c83892009-03-17 11:42:40 -0700397 *
398 * A controller keep-alive is sent every fka_period (typically 8 seconds).
399 * The source MAC is the native MAC address.
400 *
401 * A port keep-alive is sent every 90 seconds while logged in.
402 * The source MAC is the assigned mapped source address.
403 * The destination is the FCF's F-port.
404 */
Chris Leech11b56182009-11-03 11:46:29 -0800405static void fcoe_ctlr_send_keep_alive(struct fcoe_ctlr *fip,
406 struct fc_lport *lport,
407 int ports, u8 *sa)
Joe Eykholt97c83892009-03-17 11:42:40 -0700408{
409 struct sk_buff *skb;
410 struct fip_kal {
411 struct ethhdr eth;
412 struct fip_header fip;
413 struct fip_mac_desc mac;
414 } __attribute__((packed)) *kal;
415 struct fip_vn_desc *vn;
416 u32 len;
417 struct fc_lport *lp;
418 struct fcoe_fcf *fcf;
419
420 fcf = fip->sel_fcf;
421 lp = fip->lp;
Joe Eykholt281ae642010-06-11 16:43:33 -0700422 if (!fcf || (ports && !lp->port_id))
Joe Eykholt97c83892009-03-17 11:42:40 -0700423 return;
424
Yi Zoube276cb2009-11-03 11:50:16 -0800425 len = sizeof(*kal) + ports * sizeof(*vn);
Joe Eykholt97c83892009-03-17 11:42:40 -0700426 skb = dev_alloc_skb(len);
427 if (!skb)
428 return;
429
430 kal = (struct fip_kal *)skb->data;
431 memset(kal, 0, len);
432 memcpy(kal->eth.h_dest, fcf->fcf_mac, ETH_ALEN);
433 memcpy(kal->eth.h_source, sa, ETH_ALEN);
434 kal->eth.h_proto = htons(ETH_P_FIP);
435
436 kal->fip.fip_ver = FIP_VER_ENCAPS(FIP_VER);
437 kal->fip.fip_op = htons(FIP_OP_CTRL);
438 kal->fip.fip_subcode = FIP_SC_KEEP_ALIVE;
439 kal->fip.fip_dl_len = htons((sizeof(kal->mac) +
Robert Love70b51aa2009-11-03 11:47:45 -0800440 ports * sizeof(*vn)) / FIP_BPW);
Joe Eykholt97c83892009-03-17 11:42:40 -0700441 kal->fip.fip_flags = htons(FIP_FL_FPMA);
Vasu Dev184dd342009-05-17 12:33:28 +0000442 if (fip->spma)
443 kal->fip.fip_flags |= htons(FIP_FL_SPMA);
Joe Eykholt97c83892009-03-17 11:42:40 -0700444
445 kal->mac.fd_desc.fip_dtype = FIP_DT_MAC;
446 kal->mac.fd_desc.fip_dlen = sizeof(kal->mac) / FIP_BPW;
447 memcpy(kal->mac.fd_mac, fip->ctl_src_addr, ETH_ALEN);
Joe Eykholt97c83892009-03-17 11:42:40 -0700448 if (ports) {
449 vn = (struct fip_vn_desc *)(kal + 1);
450 vn->fd_desc.fip_dtype = FIP_DT_VN_ID;
451 vn->fd_desc.fip_dlen = sizeof(*vn) / FIP_BPW;
Chris Leech11b56182009-11-03 11:46:29 -0800452 memcpy(vn->fd_mac, fip->get_src_addr(lport), ETH_ALEN);
Kaladhar Musunurufb831532010-05-07 15:18:57 -0700453 hton24(vn->fd_fc_id, lport->port_id);
454 put_unaligned_be64(lport->wwpn, &vn->fd_wwpn);
Joe Eykholt97c83892009-03-17 11:42:40 -0700455 }
Joe Eykholt97c83892009-03-17 11:42:40 -0700456 skb_put(skb, len);
Chris Leech0f491532009-05-06 10:52:18 -0700457 skb->protocol = htons(ETH_P_FIP);
Joe Eykholt97c83892009-03-17 11:42:40 -0700458 skb_reset_mac_header(skb);
459 skb_reset_network_header(skb);
460 fip->send(fip, skb);
461}
462
463/**
Robert Love70b51aa2009-11-03 11:47:45 -0800464 * fcoe_ctlr_encaps() - Encapsulate an ELS frame for FIP, without sending it
465 * @fip: The FCoE controller for the ELS frame
466 * @dtype: The FIP descriptor type for the frame
467 * @skb: The FCoE ELS frame including FC header but no FCoE headers
Joe Eykholte10f8c62010-07-20 15:20:30 -0700468 * @d_id: The destination port ID.
Joe Eykholt97c83892009-03-17 11:42:40 -0700469 *
470 * Returns non-zero error code on failure.
471 *
472 * The caller must check that the length is a multiple of 4.
473 *
474 * The @skb must have enough headroom (28 bytes) and tailroom (8 bytes).
475 * Headroom includes the FIP encapsulation description, FIP header, and
476 * Ethernet header. The tailroom is for the FIP MAC descriptor.
477 */
Chris Leech11b56182009-11-03 11:46:29 -0800478static int fcoe_ctlr_encaps(struct fcoe_ctlr *fip, struct fc_lport *lport,
Joe Eykholte10f8c62010-07-20 15:20:30 -0700479 u8 dtype, struct sk_buff *skb, u32 d_id)
Joe Eykholt97c83892009-03-17 11:42:40 -0700480{
481 struct fip_encaps_head {
482 struct ethhdr eth;
483 struct fip_header fip;
484 struct fip_encaps encaps;
485 } __attribute__((packed)) *cap;
Joe Eykholt55543452010-07-20 15:20:35 -0700486 struct fc_frame_header *fh;
Joe Eykholt97c83892009-03-17 11:42:40 -0700487 struct fip_mac_desc *mac;
488 struct fcoe_fcf *fcf;
489 size_t dlen;
Yi Zou5a84bae2009-07-29 17:03:55 -0700490 u16 fip_flags;
Joe Eykholt55543452010-07-20 15:20:35 -0700491 u8 op;
Joe Eykholt97c83892009-03-17 11:42:40 -0700492
Joe Eykholt55543452010-07-20 15:20:35 -0700493 fh = (struct fc_frame_header *)skb->data;
494 op = *(u8 *)(fh + 1);
Joe Eykholt97c83892009-03-17 11:42:40 -0700495 dlen = sizeof(struct fip_encaps) + skb->len; /* len before push */
496 cap = (struct fip_encaps_head *)skb_push(skb, sizeof(*cap));
Joe Eykholt97c83892009-03-17 11:42:40 -0700497 memset(cap, 0, sizeof(*cap));
Joe Eykholte10f8c62010-07-20 15:20:30 -0700498
499 if (lport->point_to_multipoint) {
500 if (fcoe_ctlr_vn_lookup(fip, d_id, cap->eth.h_dest))
501 return -ENODEV;
Joe Eykholt55543452010-07-20 15:20:35 -0700502 fip_flags = 0;
Joe Eykholte10f8c62010-07-20 15:20:30 -0700503 } else {
504 fcf = fip->sel_fcf;
505 if (!fcf)
506 return -ENODEV;
507 fip_flags = fcf->flags;
508 fip_flags &= fip->spma ? FIP_FL_SPMA | FIP_FL_FPMA :
509 FIP_FL_FPMA;
510 if (!fip_flags)
511 return -ENODEV;
512 memcpy(cap->eth.h_dest, fcf->fcf_mac, ETH_ALEN);
513 }
Joe Eykholt97c83892009-03-17 11:42:40 -0700514 memcpy(cap->eth.h_source, fip->ctl_src_addr, ETH_ALEN);
515 cap->eth.h_proto = htons(ETH_P_FIP);
516
517 cap->fip.fip_ver = FIP_VER_ENCAPS(FIP_VER);
518 cap->fip.fip_op = htons(FIP_OP_LS);
Joe Eykholt55543452010-07-20 15:20:35 -0700519 if (op == ELS_LS_ACC || op == ELS_LS_RJT)
520 cap->fip.fip_subcode = FIP_SC_REP;
521 else
522 cap->fip.fip_subcode = FIP_SC_REQ;
Yi Zou5a84bae2009-07-29 17:03:55 -0700523 cap->fip.fip_flags = htons(fip_flags);
Joe Eykholt97c83892009-03-17 11:42:40 -0700524
525 cap->encaps.fd_desc.fip_dtype = dtype;
526 cap->encaps.fd_desc.fip_dlen = dlen / FIP_BPW;
527
Joe Eykholt55543452010-07-20 15:20:35 -0700528 if (op != ELS_LS_RJT) {
529 dlen += sizeof(*mac);
530 mac = (struct fip_mac_desc *)skb_put(skb, sizeof(*mac));
531 memset(mac, 0, sizeof(*mac));
532 mac->fd_desc.fip_dtype = FIP_DT_MAC;
533 mac->fd_desc.fip_dlen = sizeof(*mac) / FIP_BPW;
534 if (dtype != FIP_DT_FLOGI && dtype != FIP_DT_FDISC) {
535 memcpy(mac->fd_mac, fip->get_src_addr(lport), ETH_ALEN);
536 } else if (fip->mode == FIP_MODE_VN2VN) {
537 hton24(mac->fd_mac, FIP_VN_FC_MAP);
538 hton24(mac->fd_mac + 3, fip->port_id);
539 } else if (fip_flags & FIP_FL_SPMA) {
540 LIBFCOE_FIP_DBG(fip, "FLOGI/FDISC sent with SPMA\n");
541 memcpy(mac->fd_mac, fip->ctl_src_addr, ETH_ALEN);
542 } else {
543 LIBFCOE_FIP_DBG(fip, "FLOGI/FDISC sent with FPMA\n");
544 /* FPMA only FLOGI. Must leave the MAC desc zeroed. */
545 }
Robert Love593abc02010-04-09 14:22:17 -0700546 }
Joe Eykholt55543452010-07-20 15:20:35 -0700547 cap->fip.fip_dl_len = htons(dlen / FIP_BPW);
Joe Eykholt97c83892009-03-17 11:42:40 -0700548
Chris Leech0f491532009-05-06 10:52:18 -0700549 skb->protocol = htons(ETH_P_FIP);
Joe Eykholt97c83892009-03-17 11:42:40 -0700550 skb_reset_mac_header(skb);
551 skb_reset_network_header(skb);
552 return 0;
553}
554
555/**
556 * fcoe_ctlr_els_send() - Send an ELS frame encapsulated by FIP if appropriate.
557 * @fip: FCoE controller.
Chris Leech11b56182009-11-03 11:46:29 -0800558 * @lport: libfc fc_lport to send from
Joe Eykholt97c83892009-03-17 11:42:40 -0700559 * @skb: FCoE ELS frame including FC header but no FCoE headers.
560 *
561 * Returns a non-zero error code if the frame should not be sent.
562 * Returns zero if the caller should send the frame with FCoE encapsulation.
563 *
564 * The caller must check that the length is a multiple of 4.
565 * The SKB must have enough headroom (28 bytes) and tailroom (8 bytes).
Joe Eykholte10f8c62010-07-20 15:20:30 -0700566 * The the skb must also be an fc_frame.
Joe Eykholt97c83892009-03-17 11:42:40 -0700567 */
Chris Leech11b56182009-11-03 11:46:29 -0800568int fcoe_ctlr_els_send(struct fcoe_ctlr *fip, struct fc_lport *lport,
569 struct sk_buff *skb)
Joe Eykholt97c83892009-03-17 11:42:40 -0700570{
Joe Eykholte10f8c62010-07-20 15:20:30 -0700571 struct fc_frame *fp;
Joe Eykholt97c83892009-03-17 11:42:40 -0700572 struct fc_frame_header *fh;
573 u16 old_xid;
574 u8 op;
Chris Leech11b56182009-11-03 11:46:29 -0800575 u8 mac[ETH_ALEN];
Joe Eykholt97c83892009-03-17 11:42:40 -0700576
Joe Eykholte10f8c62010-07-20 15:20:30 -0700577 fp = container_of(skb, struct fc_frame, skb);
Joe Eykholt97c83892009-03-17 11:42:40 -0700578 fh = (struct fc_frame_header *)skb->data;
579 op = *(u8 *)(fh + 1);
580
Joe Eykholte10f8c62010-07-20 15:20:30 -0700581 if (op == ELS_FLOGI && fip->mode != FIP_MODE_VN2VN) {
Joe Eykholt97c83892009-03-17 11:42:40 -0700582 old_xid = fip->flogi_oxid;
583 fip->flogi_oxid = ntohs(fh->fh_ox_id);
584 if (fip->state == FIP_ST_AUTO) {
585 if (old_xid == FC_XID_UNKNOWN)
586 fip->flogi_count = 0;
587 fip->flogi_count++;
588 if (fip->flogi_count < 3)
589 goto drop;
Joe Eykholtcd229e42010-07-20 15:20:40 -0700590 fcoe_ctlr_map_dest(fip);
Joe Eykholt97c83892009-03-17 11:42:40 -0700591 return 0;
592 }
Joe Eykholt5f48f702009-05-06 10:52:12 -0700593 if (fip->state == FIP_ST_NON_FIP)
Joe Eykholtcd229e42010-07-20 15:20:40 -0700594 fcoe_ctlr_map_dest(fip);
Joe Eykholt5f48f702009-05-06 10:52:12 -0700595 }
596
597 if (fip->state == FIP_ST_NON_FIP)
598 return 0;
Joe Eykholte10f8c62010-07-20 15:20:30 -0700599 if (!fip->sel_fcf && fip->mode != FIP_MODE_VN2VN)
Joe Eykholtf31f2a12009-11-03 11:48:32 -0800600 goto drop;
Joe Eykholt5f48f702009-05-06 10:52:12 -0700601 switch (op) {
602 case ELS_FLOGI:
Joe Eykholt97c83892009-03-17 11:42:40 -0700603 op = FIP_DT_FLOGI;
604 break;
605 case ELS_FDISC:
606 if (ntoh24(fh->fh_s_id))
607 return 0;
608 op = FIP_DT_FDISC;
609 break;
610 case ELS_LOGO:
Joe Eykholte10f8c62010-07-20 15:20:30 -0700611 if (fip->mode == FIP_MODE_VN2VN) {
612 if (fip->state != FIP_ST_VNMP_UP)
613 return -EINVAL;
614 if (ntoh24(fh->fh_d_id) == FC_FID_FLOGI)
615 return -EINVAL;
616 } else {
617 if (fip->state != FIP_ST_ENABLED)
618 return 0;
619 if (ntoh24(fh->fh_d_id) != FC_FID_FLOGI)
620 return 0;
621 }
Joe Eykholt97c83892009-03-17 11:42:40 -0700622 op = FIP_DT_LOGO;
623 break;
624 case ELS_LS_ACC:
Joe Eykholt97c83892009-03-17 11:42:40 -0700625 /*
Joe Eykholte10f8c62010-07-20 15:20:30 -0700626 * If non-FIP, we may have gotten an SID by accepting an FLOGI
Joe Eykholt97c83892009-03-17 11:42:40 -0700627 * from a point-to-point connection. Switch to using
628 * the source mac based on the SID. The destination
629 * MAC in this case would have been set by receving the
630 * FLOGI.
631 */
Joe Eykholte10f8c62010-07-20 15:20:30 -0700632 if (fip->state == FIP_ST_NON_FIP) {
633 if (fip->flogi_oxid == FC_XID_UNKNOWN)
634 return 0;
635 fip->flogi_oxid = FC_XID_UNKNOWN;
636 fc_fcoe_set_mac(mac, fh->fh_d_id);
637 fip->update_mac(lport, mac);
638 }
639 /* fall through */
640 case ELS_LS_RJT:
641 op = fr_encaps(fp);
642 if (op)
643 break;
Joe Eykholt97c83892009-03-17 11:42:40 -0700644 return 0;
645 default:
Joe Eykholte10f8c62010-07-20 15:20:30 -0700646 if (fip->state != FIP_ST_ENABLED &&
647 fip->state != FIP_ST_VNMP_UP)
Joe Eykholt97c83892009-03-17 11:42:40 -0700648 goto drop;
649 return 0;
650 }
Joe Eykholte10f8c62010-07-20 15:20:30 -0700651 LIBFCOE_FIP_DBG(fip, "els_send op %u d_id %x\n",
652 op, ntoh24(fh->fh_d_id));
653 if (fcoe_ctlr_encaps(fip, lport, op, skb, ntoh24(fh->fh_d_id)))
Joe Eykholt97c83892009-03-17 11:42:40 -0700654 goto drop;
655 fip->send(fip, skb);
656 return -EINPROGRESS;
657drop:
658 kfree_skb(skb);
659 return -EINVAL;
660}
661EXPORT_SYMBOL(fcoe_ctlr_els_send);
662
Robert Love70b51aa2009-11-03 11:47:45 -0800663/**
664 * fcoe_ctlr_age_fcfs() - Reset and free all old FCFs for a controller
665 * @fip: The FCoE controller to free FCFs on
Joe Eykholt97c83892009-03-17 11:42:40 -0700666 *
Joe Eykholtf018b732010-03-12 16:08:55 -0800667 * Called with lock held and preemption disabled.
Joe Eykholt97c83892009-03-17 11:42:40 -0700668 *
Joe Eykholt8690cb82010-06-11 16:44:10 -0700669 * An FCF is considered old if we have missed two advertisements.
670 * That is, there have been no valid advertisement from it for 2.5
671 * times its keep-alive period.
Joe Eykholt97c83892009-03-17 11:42:40 -0700672 *
673 * In addition, determine the time when an FCF selection can occur.
Yi Zouf3da80e72009-11-20 14:55:08 -0800674 *
675 * Also, increment the MissDiscAdvCount when no advertisement is received
676 * for the corresponding FCF for 1.5 * FKA_ADV_PERIOD (FC-BB-5 LESB).
Joe Eykholt8690cb82010-06-11 16:44:10 -0700677 *
678 * Returns the time in jiffies for the next call.
Joe Eykholt97c83892009-03-17 11:42:40 -0700679 */
Joe Eykholt8690cb82010-06-11 16:44:10 -0700680static unsigned long fcoe_ctlr_age_fcfs(struct fcoe_ctlr *fip)
Joe Eykholt97c83892009-03-17 11:42:40 -0700681{
682 struct fcoe_fcf *fcf;
683 struct fcoe_fcf *next;
Joe Eykholt8690cb82010-06-11 16:44:10 -0700684 unsigned long next_timer = jiffies + msecs_to_jiffies(FIP_VN_KA_PERIOD);
685 unsigned long deadline;
Joe Eykholt97c83892009-03-17 11:42:40 -0700686 unsigned long sel_time = 0;
Joe Eykholtf018b732010-03-12 16:08:55 -0800687 struct fcoe_dev_stats *stats;
Joe Eykholt97c83892009-03-17 11:42:40 -0700688
Joe Eykholtfdb068c2010-07-20 15:19:47 -0700689 stats = per_cpu_ptr(fip->lp->dev_stats, get_cpu());
690
Joe Eykholt97c83892009-03-17 11:42:40 -0700691 list_for_each_entry_safe(fcf, next, &fip->fcfs, list) {
Joe Eykholt8690cb82010-06-11 16:44:10 -0700692 deadline = fcf->time + fcf->fka_period + fcf->fka_period / 2;
693 if (fip->sel_fcf == fcf) {
694 if (time_after(jiffies, deadline)) {
Joe Eykholt8690cb82010-06-11 16:44:10 -0700695 stats->MissDiscAdvCount++;
696 printk(KERN_INFO "libfcoe: host%d: "
697 "Missing Discovery Advertisement "
698 "for fab %16.16llx count %lld\n",
699 fip->lp->host->host_no, fcf->fabric_name,
700 stats->MissDiscAdvCount);
701 } else if (time_after(next_timer, deadline))
702 next_timer = deadline;
Yi Zouf3da80e72009-11-20 14:55:08 -0800703 }
Joe Eykholt8690cb82010-06-11 16:44:10 -0700704
705 deadline += fcf->fka_period;
Joe Eykholtd99ee452010-06-11 16:44:15 -0700706 if (time_after_eq(jiffies, deadline)) {
Joe Eykholt97c83892009-03-17 11:42:40 -0700707 if (fip->sel_fcf == fcf)
708 fip->sel_fcf = NULL;
709 list_del(&fcf->list);
710 WARN_ON(!fip->fcf_count);
711 fip->fcf_count--;
712 kfree(fcf);
Joe Eykholtf018b732010-03-12 16:08:55 -0800713 stats->VLinkFailureCount++;
Joe Eykholt8690cb82010-06-11 16:44:10 -0700714 } else {
715 if (time_after(next_timer, deadline))
716 next_timer = deadline;
717 if (fcoe_ctlr_mtu_valid(fcf) &&
718 (!sel_time || time_before(sel_time, fcf->time)))
719 sel_time = fcf->time;
Joe Eykholt97c83892009-03-17 11:42:40 -0700720 }
721 }
Joe Eykholtfdb068c2010-07-20 15:19:47 -0700722 put_cpu();
Joe Eykholtd99ee452010-06-11 16:44:15 -0700723 if (sel_time && !fip->sel_fcf && !fip->sel_time) {
Joe Eykholt97c83892009-03-17 11:42:40 -0700724 sel_time += msecs_to_jiffies(FCOE_CTLR_START_DELAY);
725 fip->sel_time = sel_time;
Joe Eykholt97c83892009-03-17 11:42:40 -0700726 }
Joe Eykholtd99ee452010-06-11 16:44:15 -0700727
Joe Eykholt8690cb82010-06-11 16:44:10 -0700728 return next_timer;
Joe Eykholt97c83892009-03-17 11:42:40 -0700729}
730
731/**
Robert Love70b51aa2009-11-03 11:47:45 -0800732 * fcoe_ctlr_parse_adv() - Decode a FIP advertisement into a new FCF entry
Joe Eykholt0f51c2e2009-11-03 11:48:16 -0800733 * @fip: The FCoE controller receiving the advertisement
Robert Love70b51aa2009-11-03 11:47:45 -0800734 * @skb: The received FIP advertisement frame
735 * @fcf: The resulting FCF entry
Joe Eykholt97c83892009-03-17 11:42:40 -0700736 *
737 * Returns zero on a valid parsed advertisement,
738 * otherwise returns non zero value.
739 */
Joe Eykholt0f51c2e2009-11-03 11:48:16 -0800740static int fcoe_ctlr_parse_adv(struct fcoe_ctlr *fip,
741 struct sk_buff *skb, struct fcoe_fcf *fcf)
Joe Eykholt97c83892009-03-17 11:42:40 -0700742{
743 struct fip_header *fiph;
744 struct fip_desc *desc = NULL;
745 struct fip_wwn_desc *wwn;
746 struct fip_fab_desc *fab;
747 struct fip_fka_desc *fka;
748 unsigned long t;
749 size_t rlen;
750 size_t dlen;
Bhanu Prakash Gollapudi0a9c5d32010-06-11 16:44:25 -0700751 u32 desc_mask;
Joe Eykholt97c83892009-03-17 11:42:40 -0700752
753 memset(fcf, 0, sizeof(*fcf));
754 fcf->fka_period = msecs_to_jiffies(FCOE_CTLR_DEF_FKA);
755
756 fiph = (struct fip_header *)skb->data;
757 fcf->flags = ntohs(fiph->fip_flags);
758
Bhanu Prakash Gollapudi0a9c5d32010-06-11 16:44:25 -0700759 /*
760 * mask of required descriptors. validating each one clears its bit.
761 */
762 desc_mask = BIT(FIP_DT_PRI) | BIT(FIP_DT_MAC) | BIT(FIP_DT_NAME) |
763 BIT(FIP_DT_FAB) | BIT(FIP_DT_FKA);
764
Joe Eykholt97c83892009-03-17 11:42:40 -0700765 rlen = ntohs(fiph->fip_dl_len) * 4;
766 if (rlen + sizeof(*fiph) > skb->len)
767 return -EINVAL;
768
769 desc = (struct fip_desc *)(fiph + 1);
770 while (rlen > 0) {
771 dlen = desc->fip_dlen * FIP_BPW;
772 if (dlen < sizeof(*desc) || dlen > rlen)
773 return -EINVAL;
Bhanu Prakash Gollapudi0a9c5d32010-06-11 16:44:25 -0700774 /* Drop Adv if there are duplicate critical descriptors */
775 if ((desc->fip_dtype < 32) &&
776 !(desc_mask & 1U << desc->fip_dtype)) {
777 LIBFCOE_FIP_DBG(fip, "Duplicate Critical "
778 "Descriptors in FIP adv\n");
779 return -EINVAL;
780 }
Joe Eykholt97c83892009-03-17 11:42:40 -0700781 switch (desc->fip_dtype) {
782 case FIP_DT_PRI:
783 if (dlen != sizeof(struct fip_pri_desc))
784 goto len_err;
785 fcf->pri = ((struct fip_pri_desc *)desc)->fd_pri;
Bhanu Prakash Gollapudi0a9c5d32010-06-11 16:44:25 -0700786 desc_mask &= ~BIT(FIP_DT_PRI);
Joe Eykholt97c83892009-03-17 11:42:40 -0700787 break;
788 case FIP_DT_MAC:
789 if (dlen != sizeof(struct fip_mac_desc))
790 goto len_err;
791 memcpy(fcf->fcf_mac,
792 ((struct fip_mac_desc *)desc)->fd_mac,
793 ETH_ALEN);
794 if (!is_valid_ether_addr(fcf->fcf_mac)) {
Joe Eykholte10f8c62010-07-20 15:20:30 -0700795 LIBFCOE_FIP_DBG(fip,
796 "Invalid MAC addr %pM in FIP adv\n",
797 fcf->fcf_mac);
Joe Eykholt97c83892009-03-17 11:42:40 -0700798 return -EINVAL;
799 }
Bhanu Prakash Gollapudi0a9c5d32010-06-11 16:44:25 -0700800 desc_mask &= ~BIT(FIP_DT_MAC);
Joe Eykholt97c83892009-03-17 11:42:40 -0700801 break;
802 case FIP_DT_NAME:
803 if (dlen != sizeof(struct fip_wwn_desc))
804 goto len_err;
805 wwn = (struct fip_wwn_desc *)desc;
806 fcf->switch_name = get_unaligned_be64(&wwn->fd_wwn);
Bhanu Prakash Gollapudi0a9c5d32010-06-11 16:44:25 -0700807 desc_mask &= ~BIT(FIP_DT_NAME);
Joe Eykholt97c83892009-03-17 11:42:40 -0700808 break;
809 case FIP_DT_FAB:
810 if (dlen != sizeof(struct fip_fab_desc))
811 goto len_err;
812 fab = (struct fip_fab_desc *)desc;
813 fcf->fabric_name = get_unaligned_be64(&fab->fd_wwn);
814 fcf->vfid = ntohs(fab->fd_vfid);
815 fcf->fc_map = ntoh24(fab->fd_map);
Bhanu Prakash Gollapudi0a9c5d32010-06-11 16:44:25 -0700816 desc_mask &= ~BIT(FIP_DT_FAB);
Joe Eykholt97c83892009-03-17 11:42:40 -0700817 break;
818 case FIP_DT_FKA:
819 if (dlen != sizeof(struct fip_fka_desc))
820 goto len_err;
821 fka = (struct fip_fka_desc *)desc;
Yi Zou8cdffdc2009-11-20 14:54:57 -0800822 if (fka->fd_flags & FIP_FKA_ADV_D)
823 fcf->fd_flags = 1;
Joe Eykholt97c83892009-03-17 11:42:40 -0700824 t = ntohl(fka->fd_fka_period);
825 if (t >= FCOE_CTLR_MIN_FKA)
826 fcf->fka_period = msecs_to_jiffies(t);
Bhanu Prakash Gollapudi0a9c5d32010-06-11 16:44:25 -0700827 desc_mask &= ~BIT(FIP_DT_FKA);
Joe Eykholt97c83892009-03-17 11:42:40 -0700828 break;
829 case FIP_DT_MAP_OUI:
830 case FIP_DT_FCOE_SIZE:
831 case FIP_DT_FLOGI:
832 case FIP_DT_FDISC:
833 case FIP_DT_LOGO:
834 case FIP_DT_ELP:
835 default:
Joe Eykholt0f51c2e2009-11-03 11:48:16 -0800836 LIBFCOE_FIP_DBG(fip, "unexpected descriptor type %x "
Robert Love650bd122009-06-10 15:31:05 -0700837 "in FIP adv\n", desc->fip_dtype);
Joe Eykholt97c83892009-03-17 11:42:40 -0700838 /* standard says ignore unknown descriptors >= 128 */
839 if (desc->fip_dtype < FIP_DT_VENDOR_BASE)
840 return -EINVAL;
Bhanu Prakash Gollapudi1508f3ec2010-06-11 16:43:38 -0700841 break;
Joe Eykholt97c83892009-03-17 11:42:40 -0700842 }
843 desc = (struct fip_desc *)((char *)desc + dlen);
844 rlen -= dlen;
845 }
846 if (!fcf->fc_map || (fcf->fc_map & 0x10000))
847 return -EINVAL;
Vasu Dev240778e2010-07-20 15:21:33 -0700848 if (!fcf->switch_name)
Joe Eykholt97c83892009-03-17 11:42:40 -0700849 return -EINVAL;
Bhanu Prakash Gollapudi0a9c5d32010-06-11 16:44:25 -0700850 if (desc_mask) {
851 LIBFCOE_FIP_DBG(fip, "adv missing descriptors mask %x\n",
852 desc_mask);
853 return -EINVAL;
854 }
Joe Eykholt97c83892009-03-17 11:42:40 -0700855 return 0;
856
857len_err:
Joe Eykholt0f51c2e2009-11-03 11:48:16 -0800858 LIBFCOE_FIP_DBG(fip, "FIP length error in descriptor type %x len %zu\n",
Robert Love650bd122009-06-10 15:31:05 -0700859 desc->fip_dtype, dlen);
Joe Eykholt97c83892009-03-17 11:42:40 -0700860 return -EINVAL;
861}
862
863/**
Robert Love70b51aa2009-11-03 11:47:45 -0800864 * fcoe_ctlr_recv_adv() - Handle an incoming advertisement
865 * @fip: The FCoE controller receiving the advertisement
866 * @skb: The received FIP packet
Joe Eykholt97c83892009-03-17 11:42:40 -0700867 */
868static void fcoe_ctlr_recv_adv(struct fcoe_ctlr *fip, struct sk_buff *skb)
869{
870 struct fcoe_fcf *fcf;
871 struct fcoe_fcf new;
872 struct fcoe_fcf *found;
873 unsigned long sol_tov = msecs_to_jiffies(FCOE_CTRL_SOL_TOV);
874 int first = 0;
875 int mtu_valid;
876
Joe Eykholt0f51c2e2009-11-03 11:48:16 -0800877 if (fcoe_ctlr_parse_adv(fip, skb, &new))
Joe Eykholt97c83892009-03-17 11:42:40 -0700878 return;
879
Joe Eykholtfdb068c2010-07-20 15:19:47 -0700880 mutex_lock(&fip->ctlr_mutex);
Joe Eykholt97c83892009-03-17 11:42:40 -0700881 first = list_empty(&fip->fcfs);
882 found = NULL;
883 list_for_each_entry(fcf, &fip->fcfs, list) {
884 if (fcf->switch_name == new.switch_name &&
885 fcf->fabric_name == new.fabric_name &&
886 fcf->fc_map == new.fc_map &&
887 compare_ether_addr(fcf->fcf_mac, new.fcf_mac) == 0) {
888 found = fcf;
889 break;
890 }
891 }
892 if (!found) {
893 if (fip->fcf_count >= FCOE_CTLR_FCF_LIMIT)
894 goto out;
895
896 fcf = kmalloc(sizeof(*fcf), GFP_ATOMIC);
897 if (!fcf)
898 goto out;
899
900 fip->fcf_count++;
901 memcpy(fcf, &new, sizeof(new));
902 list_add(&fcf->list, &fip->fcfs);
903 } else {
904 /*
Joe Eykholtc600fea2010-06-11 16:44:20 -0700905 * Update the FCF's keep-alive descriptor flags.
906 * Other flag changes from new advertisements are
907 * ignored after a solicited advertisement is
908 * received and the FCF is selectable (usable).
Joe Eykholt97c83892009-03-17 11:42:40 -0700909 */
Joe Eykholtc600fea2010-06-11 16:44:20 -0700910 fcf->fd_flags = new.fd_flags;
911 if (!fcoe_ctlr_fcf_usable(fcf))
912 fcf->flags = new.flags;
913
Joe Eykholtd99ee452010-06-11 16:44:15 -0700914 if (fcf == fip->sel_fcf && !fcf->fd_flags) {
Joe Eykholt97c83892009-03-17 11:42:40 -0700915 fip->ctlr_ka_time -= fcf->fka_period;
916 fip->ctlr_ka_time += new.fka_period;
917 if (time_before(fip->ctlr_ka_time, fip->timer.expires))
918 mod_timer(&fip->timer, fip->ctlr_ka_time);
Joe Eykholtc600fea2010-06-11 16:44:20 -0700919 }
Joe Eykholt97c83892009-03-17 11:42:40 -0700920 fcf->fka_period = new.fka_period;
921 memcpy(fcf->fcf_mac, new.fcf_mac, ETH_ALEN);
922 }
923 mtu_valid = fcoe_ctlr_mtu_valid(fcf);
924 fcf->time = jiffies;
Robert Love650bd122009-06-10 15:31:05 -0700925 if (!found) {
Chris Leech9f8f3aa2010-04-09 14:23:16 -0700926 LIBFCOE_FIP_DBG(fip, "New FCF for fab %16.16llx "
927 "map %x val %d\n",
Robert Love650bd122009-06-10 15:31:05 -0700928 fcf->fabric_name, fcf->fc_map, mtu_valid);
929 }
Joe Eykholt97c83892009-03-17 11:42:40 -0700930
931 /*
932 * If this advertisement is not solicited and our max receive size
933 * hasn't been verified, send a solicited advertisement.
934 */
935 if (!mtu_valid)
936 fcoe_ctlr_solicit(fip, fcf);
937
938 /*
939 * If its been a while since we did a solicit, and this is
940 * the first advertisement we've received, do a multicast
941 * solicitation to gather as many advertisements as we can
942 * before selection occurs.
943 */
944 if (first && time_after(jiffies, fip->sol_time + sol_tov))
945 fcoe_ctlr_solicit(fip, NULL);
946
947 /*
948 * If this is the first validated FCF, note the time and
949 * set a timer to trigger selection.
950 */
Joe Eykholtd99ee452010-06-11 16:44:15 -0700951 if (mtu_valid && !fip->sel_fcf && fcoe_ctlr_fcf_usable(fcf)) {
Joe Eykholt97c83892009-03-17 11:42:40 -0700952 fip->sel_time = jiffies +
Robert Love70b51aa2009-11-03 11:47:45 -0800953 msecs_to_jiffies(FCOE_CTLR_START_DELAY);
Joe Eykholt97c83892009-03-17 11:42:40 -0700954 if (!timer_pending(&fip->timer) ||
955 time_before(fip->sel_time, fip->timer.expires))
956 mod_timer(&fip->timer, fip->sel_time);
957 }
958out:
Joe Eykholtfdb068c2010-07-20 15:19:47 -0700959 mutex_unlock(&fip->ctlr_mutex);
Joe Eykholt97c83892009-03-17 11:42:40 -0700960}
961
962/**
Robert Love70b51aa2009-11-03 11:47:45 -0800963 * fcoe_ctlr_recv_els() - Handle an incoming FIP encapsulated ELS frame
964 * @fip: The FCoE controller which received the packet
965 * @skb: The received FIP packet
Joe Eykholt97c83892009-03-17 11:42:40 -0700966 */
967static void fcoe_ctlr_recv_els(struct fcoe_ctlr *fip, struct sk_buff *skb)
968{
Robert Love70b51aa2009-11-03 11:47:45 -0800969 struct fc_lport *lport = fip->lp;
Joe Eykholt97c83892009-03-17 11:42:40 -0700970 struct fip_header *fiph;
Chris Leech11b56182009-11-03 11:46:29 -0800971 struct fc_frame *fp = (struct fc_frame *)skb;
Joe Eykholt97c83892009-03-17 11:42:40 -0700972 struct fc_frame_header *fh = NULL;
973 struct fip_desc *desc;
974 struct fip_encaps *els;
975 struct fcoe_dev_stats *stats;
976 enum fip_desc_type els_dtype = 0;
977 u8 els_op;
978 u8 sub;
979 u8 granted_mac[ETH_ALEN] = { 0 };
980 size_t els_len = 0;
981 size_t rlen;
982 size_t dlen;
Bhanu Prakash Gollapudibe613312010-06-11 16:44:36 -0700983 u32 desc_mask = 0;
984 u32 desc_cnt = 0;
Joe Eykholt97c83892009-03-17 11:42:40 -0700985
986 fiph = (struct fip_header *)skb->data;
987 sub = fiph->fip_subcode;
988 if (sub != FIP_SC_REQ && sub != FIP_SC_REP)
989 goto drop;
990
991 rlen = ntohs(fiph->fip_dl_len) * 4;
992 if (rlen + sizeof(*fiph) > skb->len)
993 goto drop;
994
995 desc = (struct fip_desc *)(fiph + 1);
996 while (rlen > 0) {
Bhanu Prakash Gollapudibe613312010-06-11 16:44:36 -0700997 desc_cnt++;
Joe Eykholt97c83892009-03-17 11:42:40 -0700998 dlen = desc->fip_dlen * FIP_BPW;
999 if (dlen < sizeof(*desc) || dlen > rlen)
1000 goto drop;
Bhanu Prakash Gollapudi0a9c5d32010-06-11 16:44:25 -07001001 /* Drop ELS if there are duplicate critical descriptors */
1002 if (desc->fip_dtype < 32) {
Bhanu Prakash Gollapudibe613312010-06-11 16:44:36 -07001003 if (desc_mask & 1U << desc->fip_dtype) {
Bhanu Prakash Gollapudi0a9c5d32010-06-11 16:44:25 -07001004 LIBFCOE_FIP_DBG(fip, "Duplicate Critical "
1005 "Descriptors in FIP ELS\n");
1006 goto drop;
1007 }
Bhanu Prakash Gollapudibe613312010-06-11 16:44:36 -07001008 desc_mask |= (1 << desc->fip_dtype);
Bhanu Prakash Gollapudi0a9c5d32010-06-11 16:44:25 -07001009 }
Joe Eykholt97c83892009-03-17 11:42:40 -07001010 switch (desc->fip_dtype) {
1011 case FIP_DT_MAC:
Bhanu Prakash Gollapudibe613312010-06-11 16:44:36 -07001012 if (desc_cnt == 1) {
1013 LIBFCOE_FIP_DBG(fip, "FIP descriptors "
1014 "received out of order\n");
1015 goto drop;
1016 }
1017
Joe Eykholt97c83892009-03-17 11:42:40 -07001018 if (dlen != sizeof(struct fip_mac_desc))
1019 goto len_err;
1020 memcpy(granted_mac,
1021 ((struct fip_mac_desc *)desc)->fd_mac,
1022 ETH_ALEN);
Joe Eykholt97c83892009-03-17 11:42:40 -07001023 break;
1024 case FIP_DT_FLOGI:
1025 case FIP_DT_FDISC:
1026 case FIP_DT_LOGO:
1027 case FIP_DT_ELP:
Bhanu Prakash Gollapudibe613312010-06-11 16:44:36 -07001028 if (desc_cnt != 1) {
1029 LIBFCOE_FIP_DBG(fip, "FIP descriptors "
1030 "received out of order\n");
1031 goto drop;
1032 }
Joe Eykholt97c83892009-03-17 11:42:40 -07001033 if (fh)
1034 goto drop;
1035 if (dlen < sizeof(*els) + sizeof(*fh) + 1)
1036 goto len_err;
1037 els_len = dlen - sizeof(*els);
1038 els = (struct fip_encaps *)desc;
1039 fh = (struct fc_frame_header *)(els + 1);
1040 els_dtype = desc->fip_dtype;
1041 break;
1042 default:
Joe Eykholt0f51c2e2009-11-03 11:48:16 -08001043 LIBFCOE_FIP_DBG(fip, "unexpected descriptor type %x "
Robert Love650bd122009-06-10 15:31:05 -07001044 "in FIP adv\n", desc->fip_dtype);
Joe Eykholt97c83892009-03-17 11:42:40 -07001045 /* standard says ignore unknown descriptors >= 128 */
1046 if (desc->fip_dtype < FIP_DT_VENDOR_BASE)
1047 goto drop;
Bhanu Prakash Gollapudibe613312010-06-11 16:44:36 -07001048 if (desc_cnt <= 2) {
1049 LIBFCOE_FIP_DBG(fip, "FIP descriptors "
1050 "received out of order\n");
1051 goto drop;
1052 }
Bhanu Prakash Gollapudi1508f3ec2010-06-11 16:43:38 -07001053 break;
Joe Eykholt97c83892009-03-17 11:42:40 -07001054 }
1055 desc = (struct fip_desc *)((char *)desc + dlen);
1056 rlen -= dlen;
1057 }
1058
1059 if (!fh)
1060 goto drop;
1061 els_op = *(u8 *)(fh + 1);
1062
Joe Eykholte10f8c62010-07-20 15:20:30 -07001063 if ((els_dtype == FIP_DT_FLOGI || els_dtype == FIP_DT_FDISC) &&
1064 sub == FIP_SC_REP && els_op == ELS_LS_ACC &&
1065 fip->mode != FIP_MODE_VN2VN) {
1066 if (!is_valid_ether_addr(granted_mac)) {
1067 LIBFCOE_FIP_DBG(fip,
1068 "Invalid MAC address %pM in FIP ELS\n",
1069 granted_mac);
1070 goto drop;
1071 }
1072 memcpy(fr_cb(fp)->granted_mac, granted_mac, ETH_ALEN);
1073
1074 if (fip->flogi_oxid == ntohs(fh->fh_ox_id))
1075 fip->flogi_oxid = FC_XID_UNKNOWN;
1076 }
Joe Eykholt97c83892009-03-17 11:42:40 -07001077
Bhanu Prakash Gollapudibe613312010-06-11 16:44:36 -07001078 if ((desc_cnt == 0) || ((els_op != ELS_LS_RJT) &&
1079 (!(1U << FIP_DT_MAC & desc_mask)))) {
1080 LIBFCOE_FIP_DBG(fip, "Missing critical descriptors "
1081 "in FIP ELS\n");
1082 goto drop;
1083 }
1084
Joe Eykholt97c83892009-03-17 11:42:40 -07001085 /*
1086 * Convert skb into an fc_frame containing only the ELS.
1087 */
1088 skb_pull(skb, (u8 *)fh - skb->data);
1089 skb_trim(skb, els_len);
1090 fp = (struct fc_frame *)skb;
1091 fc_frame_init(fp);
1092 fr_sof(fp) = FC_SOF_I3;
1093 fr_eof(fp) = FC_EOF_T;
Robert Love70b51aa2009-11-03 11:47:45 -08001094 fr_dev(fp) = lport;
Joe Eykholte10f8c62010-07-20 15:20:30 -07001095 fr_encaps(fp) = els_dtype;
Joe Eykholt97c83892009-03-17 11:42:40 -07001096
Joe Eykholtf018b732010-03-12 16:08:55 -08001097 stats = per_cpu_ptr(lport->dev_stats, get_cpu());
Joe Eykholt97c83892009-03-17 11:42:40 -07001098 stats->RxFrames++;
1099 stats->RxWords += skb->len / FIP_BPW;
Joe Eykholtf018b732010-03-12 16:08:55 -08001100 put_cpu();
Joe Eykholt97c83892009-03-17 11:42:40 -07001101
Robert Love70b51aa2009-11-03 11:47:45 -08001102 fc_exch_recv(lport, fp);
Joe Eykholt97c83892009-03-17 11:42:40 -07001103 return;
1104
1105len_err:
Joe Eykholt0f51c2e2009-11-03 11:48:16 -08001106 LIBFCOE_FIP_DBG(fip, "FIP length error in descriptor type %x len %zu\n",
Robert Love650bd122009-06-10 15:31:05 -07001107 desc->fip_dtype, dlen);
Joe Eykholt97c83892009-03-17 11:42:40 -07001108drop:
1109 kfree_skb(skb);
1110}
1111
1112/**
Robert Love70b51aa2009-11-03 11:47:45 -08001113 * fcoe_ctlr_recv_els() - Handle an incoming link reset frame
1114 * @fip: The FCoE controller that received the frame
1115 * @fh: The received FIP header
Joe Eykholt97c83892009-03-17 11:42:40 -07001116 *
1117 * There may be multiple VN_Port descriptors.
1118 * The overall length has already been checked.
1119 */
1120static void fcoe_ctlr_recv_clr_vlink(struct fcoe_ctlr *fip,
Robert Love70b51aa2009-11-03 11:47:45 -08001121 struct fip_header *fh)
Joe Eykholt97c83892009-03-17 11:42:40 -07001122{
1123 struct fip_desc *desc;
1124 struct fip_mac_desc *mp;
1125 struct fip_wwn_desc *wp;
1126 struct fip_vn_desc *vp;
1127 size_t rlen;
1128 size_t dlen;
1129 struct fcoe_fcf *fcf = fip->sel_fcf;
Robert Love70b51aa2009-11-03 11:47:45 -08001130 struct fc_lport *lport = fip->lp;
Bhanu Prakash Gollapudi5550fda2010-06-11 16:44:31 -07001131 struct fc_lport *vn_port = NULL;
1132 u32 desc_mask;
1133 int is_vn_port = 0;
Joe Eykholt97c83892009-03-17 11:42:40 -07001134
Joe Eykholt0f51c2e2009-11-03 11:48:16 -08001135 LIBFCOE_FIP_DBG(fip, "Clear Virtual Link received\n");
Robert Loved29510a2010-05-07 15:18:30 -07001136
Robert Love7b2787e2010-05-07 15:18:41 -07001137 if (!fcf || !lport->port_id)
Joe Eykholt97c83892009-03-17 11:42:40 -07001138 return;
1139
1140 /*
1141 * mask of required descriptors. Validating each one clears its bit.
1142 */
1143 desc_mask = BIT(FIP_DT_MAC) | BIT(FIP_DT_NAME) | BIT(FIP_DT_VN_ID);
1144
1145 rlen = ntohs(fh->fip_dl_len) * FIP_BPW;
1146 desc = (struct fip_desc *)(fh + 1);
1147 while (rlen >= sizeof(*desc)) {
1148 dlen = desc->fip_dlen * FIP_BPW;
1149 if (dlen > rlen)
1150 return;
Bhanu Prakash Gollapudi0a9c5d32010-06-11 16:44:25 -07001151 /* Drop CVL if there are duplicate critical descriptors */
1152 if ((desc->fip_dtype < 32) &&
1153 !(desc_mask & 1U << desc->fip_dtype)) {
1154 LIBFCOE_FIP_DBG(fip, "Duplicate Critical "
1155 "Descriptors in FIP CVL\n");
1156 return;
1157 }
Joe Eykholt97c83892009-03-17 11:42:40 -07001158 switch (desc->fip_dtype) {
1159 case FIP_DT_MAC:
1160 mp = (struct fip_mac_desc *)desc;
1161 if (dlen < sizeof(*mp))
1162 return;
1163 if (compare_ether_addr(mp->fd_mac, fcf->fcf_mac))
1164 return;
1165 desc_mask &= ~BIT(FIP_DT_MAC);
1166 break;
1167 case FIP_DT_NAME:
1168 wp = (struct fip_wwn_desc *)desc;
1169 if (dlen < sizeof(*wp))
1170 return;
1171 if (get_unaligned_be64(&wp->fd_wwn) != fcf->switch_name)
1172 return;
1173 desc_mask &= ~BIT(FIP_DT_NAME);
1174 break;
1175 case FIP_DT_VN_ID:
1176 vp = (struct fip_vn_desc *)desc;
1177 if (dlen < sizeof(*vp))
1178 return;
1179 if (compare_ether_addr(vp->fd_mac,
Robert Love70b51aa2009-11-03 11:47:45 -08001180 fip->get_src_addr(lport)) == 0 &&
1181 get_unaligned_be64(&vp->fd_wwpn) == lport->wwpn &&
Bhanu Prakash Gollapudi5550fda2010-06-11 16:44:31 -07001182 ntoh24(vp->fd_fc_id) == lport->port_id) {
Joe Eykholt97c83892009-03-17 11:42:40 -07001183 desc_mask &= ~BIT(FIP_DT_VN_ID);
Bhanu Prakash Gollapudi5550fda2010-06-11 16:44:31 -07001184 break;
1185 }
1186 /* check if clr_vlink is for NPIV port */
1187 mutex_lock(&lport->lp_mutex);
1188 list_for_each_entry(vn_port, &lport->vports, list) {
1189 if (compare_ether_addr(vp->fd_mac,
1190 fip->get_src_addr(vn_port)) == 0 &&
1191 (get_unaligned_be64(&vp->fd_wwpn)
1192 == vn_port->wwpn) &&
1193 (ntoh24(vp->fd_fc_id) ==
1194 fc_host_port_id(vn_port->host))) {
1195 desc_mask &= ~BIT(FIP_DT_VN_ID);
1196 is_vn_port = 1;
1197 break;
1198 }
1199 }
1200 mutex_unlock(&lport->lp_mutex);
1201
Joe Eykholt97c83892009-03-17 11:42:40 -07001202 break;
1203 default:
1204 /* standard says ignore unknown descriptors >= 128 */
1205 if (desc->fip_dtype < FIP_DT_VENDOR_BASE)
1206 return;
1207 break;
1208 }
1209 desc = (struct fip_desc *)((char *)desc + dlen);
1210 rlen -= dlen;
1211 }
1212
1213 /*
1214 * reset only if all required descriptors were present and valid.
1215 */
1216 if (desc_mask) {
Joe Eykholt0f51c2e2009-11-03 11:48:16 -08001217 LIBFCOE_FIP_DBG(fip, "missing descriptors mask %x\n",
1218 desc_mask);
Joe Eykholt97c83892009-03-17 11:42:40 -07001219 } else {
Joe Eykholt0f51c2e2009-11-03 11:48:16 -08001220 LIBFCOE_FIP_DBG(fip, "performing Clear Virtual Link\n");
Joe Eykholtdd42dac2009-11-03 11:48:27 -08001221
Bhanu Prakash Gollapudi5550fda2010-06-11 16:44:31 -07001222 if (is_vn_port)
1223 fc_lport_reset(vn_port);
1224 else {
Joe Eykholtfdb068c2010-07-20 15:19:47 -07001225 mutex_lock(&fip->ctlr_mutex);
Bhanu Prakash Gollapudi5550fda2010-06-11 16:44:31 -07001226 per_cpu_ptr(lport->dev_stats,
Joe Eykholtfdb068c2010-07-20 15:19:47 -07001227 get_cpu())->VLinkFailureCount++;
1228 put_cpu();
Bhanu Prakash Gollapudi5550fda2010-06-11 16:44:31 -07001229 fcoe_ctlr_reset(fip);
Joe Eykholtfdb068c2010-07-20 15:19:47 -07001230 mutex_unlock(&fip->ctlr_mutex);
Joe Eykholtdd42dac2009-11-03 11:48:27 -08001231
Bhanu Prakash Gollapudi5550fda2010-06-11 16:44:31 -07001232 fc_lport_reset(fip->lp);
1233 fcoe_ctlr_solicit(fip, NULL);
1234 }
Joe Eykholt97c83892009-03-17 11:42:40 -07001235 }
1236}
1237
1238/**
Robert Love70b51aa2009-11-03 11:47:45 -08001239 * fcoe_ctlr_recv() - Receive a FIP packet
1240 * @fip: The FCoE controller that received the packet
1241 * @skb: The received FIP packet
Joe Eykholt97c83892009-03-17 11:42:40 -07001242 *
Joe Eykholt1f4aed82009-11-03 11:48:22 -08001243 * This may be called from either NET_RX_SOFTIRQ or IRQ.
Joe Eykholt97c83892009-03-17 11:42:40 -07001244 */
1245void fcoe_ctlr_recv(struct fcoe_ctlr *fip, struct sk_buff *skb)
1246{
Joe Eykholt1f4aed82009-11-03 11:48:22 -08001247 skb_queue_tail(&fip->fip_recv_list, skb);
Joe Eykholt97c83892009-03-17 11:42:40 -07001248 schedule_work(&fip->recv_work);
1249}
1250EXPORT_SYMBOL(fcoe_ctlr_recv);
1251
1252/**
Robert Love70b51aa2009-11-03 11:47:45 -08001253 * fcoe_ctlr_recv_handler() - Receive a FIP frame
1254 * @fip: The FCoE controller that received the frame
1255 * @skb: The received FIP frame
Joe Eykholt97c83892009-03-17 11:42:40 -07001256 *
1257 * Returns non-zero if the frame is dropped.
1258 */
1259static int fcoe_ctlr_recv_handler(struct fcoe_ctlr *fip, struct sk_buff *skb)
1260{
1261 struct fip_header *fiph;
1262 struct ethhdr *eh;
1263 enum fip_state state;
1264 u16 op;
1265 u8 sub;
1266
1267 if (skb_linearize(skb))
1268 goto drop;
1269 if (skb->len < sizeof(*fiph))
1270 goto drop;
1271 eh = eth_hdr(skb);
Joe Eykholte10f8c62010-07-20 15:20:30 -07001272 if (fip->mode == FIP_MODE_VN2VN) {
1273 if (compare_ether_addr(eh->h_dest, fip->ctl_src_addr) &&
1274 compare_ether_addr(eh->h_dest, fcoe_all_vn2vn) &&
1275 compare_ether_addr(eh->h_dest, fcoe_all_p2p))
1276 goto drop;
1277 } else if (compare_ether_addr(eh->h_dest, fip->ctl_src_addr) &&
1278 compare_ether_addr(eh->h_dest, fcoe_all_enode))
Joe Eykholt97c83892009-03-17 11:42:40 -07001279 goto drop;
1280 fiph = (struct fip_header *)skb->data;
1281 op = ntohs(fiph->fip_op);
1282 sub = fiph->fip_subcode;
1283
Joe Eykholt97c83892009-03-17 11:42:40 -07001284 if (FIP_VER_DECAPS(fiph->fip_ver) != FIP_VER)
1285 goto drop;
1286 if (ntohs(fiph->fip_dl_len) * FIP_BPW + sizeof(*fiph) > skb->len)
1287 goto drop;
1288
Joe Eykholtfdb068c2010-07-20 15:19:47 -07001289 mutex_lock(&fip->ctlr_mutex);
Joe Eykholt97c83892009-03-17 11:42:40 -07001290 state = fip->state;
1291 if (state == FIP_ST_AUTO) {
1292 fip->map_dest = 0;
Joe Eykholt9b651da2010-07-20 15:20:24 -07001293 fcoe_ctlr_set_state(fip, FIP_ST_ENABLED);
Joe Eykholt97c83892009-03-17 11:42:40 -07001294 state = FIP_ST_ENABLED;
Joe Eykholt0f51c2e2009-11-03 11:48:16 -08001295 LIBFCOE_FIP_DBG(fip, "Using FIP mode\n");
Joe Eykholt97c83892009-03-17 11:42:40 -07001296 }
Joe Eykholtfdb068c2010-07-20 15:19:47 -07001297 mutex_unlock(&fip->ctlr_mutex);
Joe Eykholte10f8c62010-07-20 15:20:30 -07001298
1299 if (fip->mode == FIP_MODE_VN2VN && op == FIP_OP_VN2VN)
1300 return fcoe_ctlr_vn_recv(fip, skb);
1301
1302 if (state != FIP_ST_ENABLED && state != FIP_ST_VNMP_UP &&
1303 state != FIP_ST_VNMP_CLAIM)
Joe Eykholt97c83892009-03-17 11:42:40 -07001304 goto drop;
1305
1306 if (op == FIP_OP_LS) {
1307 fcoe_ctlr_recv_els(fip, skb); /* consumes skb */
1308 return 0;
1309 }
Joe Eykholte10f8c62010-07-20 15:20:30 -07001310
1311 if (state != FIP_ST_ENABLED)
1312 goto drop;
1313
Joe Eykholt97c83892009-03-17 11:42:40 -07001314 if (op == FIP_OP_DISC && sub == FIP_SC_ADV)
1315 fcoe_ctlr_recv_adv(fip, skb);
1316 else if (op == FIP_OP_CTRL && sub == FIP_SC_CLR_VLINK)
1317 fcoe_ctlr_recv_clr_vlink(fip, fiph);
1318 kfree_skb(skb);
1319 return 0;
1320drop:
1321 kfree_skb(skb);
1322 return -1;
1323}
1324
1325/**
Robert Love70b51aa2009-11-03 11:47:45 -08001326 * fcoe_ctlr_select() - Select the best FCF (if possible)
1327 * @fip: The FCoE controller
Joe Eykholt97c83892009-03-17 11:42:40 -07001328 *
1329 * If there are conflicting advertisements, no FCF can be chosen.
1330 *
1331 * Called with lock held.
1332 */
1333static void fcoe_ctlr_select(struct fcoe_ctlr *fip)
1334{
1335 struct fcoe_fcf *fcf;
1336 struct fcoe_fcf *best = NULL;
1337
1338 list_for_each_entry(fcf, &fip->fcfs, list) {
Chris Leech9f8f3aa2010-04-09 14:23:16 -07001339 LIBFCOE_FIP_DBG(fip, "consider FCF for fab %16.16llx "
1340 "VFID %d map %x val %d\n",
1341 fcf->fabric_name, fcf->vfid,
Robert Love650bd122009-06-10 15:31:05 -07001342 fcf->fc_map, fcoe_ctlr_mtu_valid(fcf));
Joe Eykholt97c83892009-03-17 11:42:40 -07001343 if (!fcoe_ctlr_fcf_usable(fcf)) {
Chris Leech9f8f3aa2010-04-09 14:23:16 -07001344 LIBFCOE_FIP_DBG(fip, "FCF for fab %16.16llx "
1345 "map %x %svalid %savailable\n",
1346 fcf->fabric_name, fcf->fc_map,
1347 (fcf->flags & FIP_FL_SOL) ? "" : "in",
1348 (fcf->flags & FIP_FL_AVAIL) ?
1349 "" : "un");
Joe Eykholt97c83892009-03-17 11:42:40 -07001350 continue;
1351 }
1352 if (!best) {
1353 best = fcf;
1354 continue;
1355 }
1356 if (fcf->fabric_name != best->fabric_name ||
1357 fcf->vfid != best->vfid ||
1358 fcf->fc_map != best->fc_map) {
Joe Eykholt0f51c2e2009-11-03 11:48:16 -08001359 LIBFCOE_FIP_DBG(fip, "Conflicting fabric, VFID, "
Robert Love650bd122009-06-10 15:31:05 -07001360 "or FC-MAP\n");
Joe Eykholt97c83892009-03-17 11:42:40 -07001361 return;
1362 }
1363 if (fcf->pri < best->pri)
1364 best = fcf;
1365 }
1366 fip->sel_fcf = best;
1367}
1368
1369/**
Robert Love70b51aa2009-11-03 11:47:45 -08001370 * fcoe_ctlr_timeout() - FIP timeout handler
1371 * @arg: The FCoE controller that timed out
Joe Eykholt97c83892009-03-17 11:42:40 -07001372 */
1373static void fcoe_ctlr_timeout(unsigned long arg)
1374{
1375 struct fcoe_ctlr *fip = (struct fcoe_ctlr *)arg;
Joe Eykholtfdb068c2010-07-20 15:19:47 -07001376
1377 schedule_work(&fip->timer_work);
1378}
1379
1380/**
1381 * fcoe_ctlr_timer_work() - Worker thread function for timer work
1382 * @work: Handle to a FCoE controller
1383 *
1384 * Ages FCFs. Triggers FCF selection if possible.
1385 * Sends keep-alives and resets.
1386 */
1387static void fcoe_ctlr_timer_work(struct work_struct *work)
1388{
1389 struct fcoe_ctlr *fip;
1390 struct fc_lport *vport;
1391 u8 *mac;
1392 u8 reset = 0;
1393 u8 send_ctlr_ka = 0;
1394 u8 send_port_ka = 0;
Joe Eykholt97c83892009-03-17 11:42:40 -07001395 struct fcoe_fcf *sel;
1396 struct fcoe_fcf *fcf;
Joe Eykholt8690cb82010-06-11 16:44:10 -07001397 unsigned long next_timer;
Joe Eykholt97c83892009-03-17 11:42:40 -07001398
Joe Eykholtfdb068c2010-07-20 15:19:47 -07001399 fip = container_of(work, struct fcoe_ctlr, timer_work);
Joe Eykholte10f8c62010-07-20 15:20:30 -07001400 if (fip->mode == FIP_MODE_VN2VN)
1401 return fcoe_ctlr_vn_timeout(fip);
Joe Eykholtfdb068c2010-07-20 15:19:47 -07001402 mutex_lock(&fip->ctlr_mutex);
Joe Eykholt97c83892009-03-17 11:42:40 -07001403 if (fip->state == FIP_ST_DISABLED) {
Joe Eykholtfdb068c2010-07-20 15:19:47 -07001404 mutex_unlock(&fip->ctlr_mutex);
Joe Eykholt97c83892009-03-17 11:42:40 -07001405 return;
1406 }
1407
1408 fcf = fip->sel_fcf;
Joe Eykholt8690cb82010-06-11 16:44:10 -07001409 next_timer = fcoe_ctlr_age_fcfs(fip);
Joe Eykholt97c83892009-03-17 11:42:40 -07001410
1411 sel = fip->sel_fcf;
Joe Eykholt8690cb82010-06-11 16:44:10 -07001412 if (!sel && fip->sel_time) {
1413 if (time_after_eq(jiffies, fip->sel_time)) {
1414 fcoe_ctlr_select(fip);
1415 sel = fip->sel_fcf;
1416 fip->sel_time = 0;
1417 } else if (time_after(next_timer, fip->sel_time))
1418 next_timer = fip->sel_time;
Joe Eykholt97c83892009-03-17 11:42:40 -07001419 }
1420
1421 if (sel != fcf) {
1422 fcf = sel; /* the old FCF may have been freed */
1423 if (sel) {
Robert Love650bd122009-06-10 15:31:05 -07001424 printk(KERN_INFO "libfcoe: host%d: FIP selected "
Johannes Berg66d6fae2009-07-15 17:21:14 +02001425 "Fibre-Channel Forwarder MAC %pM\n",
1426 fip->lp->host->host_no, sel->fcf_mac);
Joe Eykholt97c83892009-03-17 11:42:40 -07001427 memcpy(fip->dest_addr, sel->fcf_mac, ETH_ALEN);
Joe Eykholtcd229e42010-07-20 15:20:40 -07001428 fip->map_dest = 0;
Joe Eykholt97c83892009-03-17 11:42:40 -07001429 fip->port_ka_time = jiffies +
Robert Love70b51aa2009-11-03 11:47:45 -08001430 msecs_to_jiffies(FIP_VN_KA_PERIOD);
Joe Eykholt97c83892009-03-17 11:42:40 -07001431 fip->ctlr_ka_time = jiffies + sel->fka_period;
Joe Eykholtd99ee452010-06-11 16:44:15 -07001432 if (time_after(next_timer, fip->ctlr_ka_time))
1433 next_timer = fip->ctlr_ka_time;
Joe Eykholt97c83892009-03-17 11:42:40 -07001434 } else {
Robert Love650bd122009-06-10 15:31:05 -07001435 printk(KERN_NOTICE "libfcoe: host%d: "
Robert Love70b51aa2009-11-03 11:47:45 -08001436 "FIP Fibre-Channel Forwarder timed out. "
Joe Eykholt97c83892009-03-17 11:42:40 -07001437 "Starting FCF discovery.\n",
1438 fip->lp->host->host_no);
Joe Eykholtfdb068c2010-07-20 15:19:47 -07001439 reset = 1;
Joe Eykholt97c83892009-03-17 11:42:40 -07001440 }
Joe Eykholt97c83892009-03-17 11:42:40 -07001441 }
1442
Yi Zou8cdffdc2009-11-20 14:54:57 -08001443 if (sel && !sel->fd_flags) {
Joe Eykholt97c83892009-03-17 11:42:40 -07001444 if (time_after_eq(jiffies, fip->ctlr_ka_time)) {
1445 fip->ctlr_ka_time = jiffies + sel->fka_period;
Joe Eykholtfdb068c2010-07-20 15:19:47 -07001446 send_ctlr_ka = 1;
Joe Eykholt97c83892009-03-17 11:42:40 -07001447 }
1448 if (time_after(next_timer, fip->ctlr_ka_time))
1449 next_timer = fip->ctlr_ka_time;
1450
1451 if (time_after_eq(jiffies, fip->port_ka_time)) {
Bhanu Prakash Gollapudif47dd852010-01-21 10:16:00 -08001452 fip->port_ka_time = jiffies +
Robert Love70b51aa2009-11-03 11:47:45 -08001453 msecs_to_jiffies(FIP_VN_KA_PERIOD);
Joe Eykholtfdb068c2010-07-20 15:19:47 -07001454 send_port_ka = 1;
Joe Eykholt97c83892009-03-17 11:42:40 -07001455 }
1456 if (time_after(next_timer, fip->port_ka_time))
1457 next_timer = fip->port_ka_time;
Joe Eykholt97c83892009-03-17 11:42:40 -07001458 }
Joe Eykholt8690cb82010-06-11 16:44:10 -07001459 if (!list_empty(&fip->fcfs))
1460 mod_timer(&fip->timer, next_timer);
Joe Eykholtfdb068c2010-07-20 15:19:47 -07001461 mutex_unlock(&fip->ctlr_mutex);
Joe Eykholt97c83892009-03-17 11:42:40 -07001462
Bhanu Prakash Gollapudi516a6482010-06-11 16:43:44 -07001463 if (reset) {
Joe Eykholtdd42dac2009-11-03 11:48:27 -08001464 fc_lport_reset(fip->lp);
Bhanu Prakash Gollapudi516a6482010-06-11 16:43:44 -07001465 /* restart things with a solicitation */
1466 fcoe_ctlr_solicit(fip, NULL);
1467 }
Chris Leech11b56182009-11-03 11:46:29 -08001468
Joe Eykholtfdb068c2010-07-20 15:19:47 -07001469 if (send_ctlr_ka)
Chris Leech11b56182009-11-03 11:46:29 -08001470 fcoe_ctlr_send_keep_alive(fip, NULL, 0, fip->ctl_src_addr);
Joe Eykholtfdb068c2010-07-20 15:19:47 -07001471
1472 if (send_port_ka) {
Chris Leech11b56182009-11-03 11:46:29 -08001473 mutex_lock(&fip->lp->lp_mutex);
1474 mac = fip->get_src_addr(fip->lp);
1475 fcoe_ctlr_send_keep_alive(fip, fip->lp, 1, mac);
1476 list_for_each_entry(vport, &fip->lp->vports, list) {
1477 mac = fip->get_src_addr(vport);
1478 fcoe_ctlr_send_keep_alive(fip, vport, 1, mac);
1479 }
1480 mutex_unlock(&fip->lp->lp_mutex);
1481 }
Joe Eykholt97c83892009-03-17 11:42:40 -07001482}
1483
1484/**
Robert Love70b51aa2009-11-03 11:47:45 -08001485 * fcoe_ctlr_recv_work() - Worker thread function for receiving FIP frames
1486 * @recv_work: Handle to a FCoE controller
Joe Eykholt97c83892009-03-17 11:42:40 -07001487 */
1488static void fcoe_ctlr_recv_work(struct work_struct *recv_work)
1489{
1490 struct fcoe_ctlr *fip;
1491 struct sk_buff *skb;
1492
1493 fip = container_of(recv_work, struct fcoe_ctlr, recv_work);
Joe Eykholt1f4aed82009-11-03 11:48:22 -08001494 while ((skb = skb_dequeue(&fip->fip_recv_list)))
Joe Eykholt97c83892009-03-17 11:42:40 -07001495 fcoe_ctlr_recv_handler(fip, skb);
Joe Eykholt97c83892009-03-17 11:42:40 -07001496}
1497
1498/**
Joe Eykholt386309ce2009-11-03 11:49:16 -08001499 * fcoe_ctlr_recv_flogi() - Snoop pre-FIP receipt of FLOGI response
Robert Love70b51aa2009-11-03 11:47:45 -08001500 * @fip: The FCoE controller
1501 * @fp: The FC frame to snoop
Joe Eykholt97c83892009-03-17 11:42:40 -07001502 *
1503 * Snoop potential response to FLOGI or even incoming FLOGI.
1504 *
1505 * The caller has checked that we are waiting for login as indicated
1506 * by fip->flogi_oxid != FC_XID_UNKNOWN.
1507 *
1508 * The caller is responsible for freeing the frame.
Joe Eykholt386309ce2009-11-03 11:49:16 -08001509 * Fill in the granted_mac address.
Joe Eykholt97c83892009-03-17 11:42:40 -07001510 *
1511 * Return non-zero if the frame should not be delivered to libfc.
1512 */
Chris Leech11b56182009-11-03 11:46:29 -08001513int fcoe_ctlr_recv_flogi(struct fcoe_ctlr *fip, struct fc_lport *lport,
Joe Eykholt386309ce2009-11-03 11:49:16 -08001514 struct fc_frame *fp)
Joe Eykholt97c83892009-03-17 11:42:40 -07001515{
1516 struct fc_frame_header *fh;
1517 u8 op;
Joe Eykholt386309ce2009-11-03 11:49:16 -08001518 u8 *sa;
Joe Eykholt97c83892009-03-17 11:42:40 -07001519
Joe Eykholt386309ce2009-11-03 11:49:16 -08001520 sa = eth_hdr(&fp->skb)->h_source;
Joe Eykholt97c83892009-03-17 11:42:40 -07001521 fh = fc_frame_header_get(fp);
1522 if (fh->fh_type != FC_TYPE_ELS)
1523 return 0;
1524
1525 op = fc_frame_payload_op(fp);
1526 if (op == ELS_LS_ACC && fh->fh_r_ctl == FC_RCTL_ELS_REP &&
1527 fip->flogi_oxid == ntohs(fh->fh_ox_id)) {
1528
Joe Eykholtfdb068c2010-07-20 15:19:47 -07001529 mutex_lock(&fip->ctlr_mutex);
Joe Eykholt97c83892009-03-17 11:42:40 -07001530 if (fip->state != FIP_ST_AUTO && fip->state != FIP_ST_NON_FIP) {
Joe Eykholtfdb068c2010-07-20 15:19:47 -07001531 mutex_unlock(&fip->ctlr_mutex);
Joe Eykholt97c83892009-03-17 11:42:40 -07001532 return -EINVAL;
1533 }
Joe Eykholt9b651da2010-07-20 15:20:24 -07001534 fcoe_ctlr_set_state(fip, FIP_ST_NON_FIP);
Joe Eykholt0f51c2e2009-11-03 11:48:16 -08001535 LIBFCOE_FIP_DBG(fip,
1536 "received FLOGI LS_ACC using non-FIP mode\n");
Joe Eykholt97c83892009-03-17 11:42:40 -07001537
1538 /*
1539 * FLOGI accepted.
1540 * If the src mac addr is FC_OUI-based, then we mark the
1541 * address_mode flag to use FC_OUI-based Ethernet DA.
1542 * Otherwise we use the FCoE gateway addr
1543 */
1544 if (!compare_ether_addr(sa, (u8[6])FC_FCOE_FLOGI_MAC)) {
Joe Eykholtcd229e42010-07-20 15:20:40 -07001545 fcoe_ctlr_map_dest(fip);
Joe Eykholt97c83892009-03-17 11:42:40 -07001546 } else {
1547 memcpy(fip->dest_addr, sa, ETH_ALEN);
1548 fip->map_dest = 0;
1549 }
1550 fip->flogi_oxid = FC_XID_UNKNOWN;
Joe Eykholtfdb068c2010-07-20 15:19:47 -07001551 mutex_unlock(&fip->ctlr_mutex);
Joe Eykholt386309ce2009-11-03 11:49:16 -08001552 fc_fcoe_set_mac(fr_cb(fp)->granted_mac, fh->fh_d_id);
Joe Eykholt97c83892009-03-17 11:42:40 -07001553 } else if (op == ELS_FLOGI && fh->fh_r_ctl == FC_RCTL_ELS_REQ && sa) {
1554 /*
1555 * Save source MAC for point-to-point responses.
1556 */
Joe Eykholtfdb068c2010-07-20 15:19:47 -07001557 mutex_lock(&fip->ctlr_mutex);
Joe Eykholt97c83892009-03-17 11:42:40 -07001558 if (fip->state == FIP_ST_AUTO || fip->state == FIP_ST_NON_FIP) {
1559 memcpy(fip->dest_addr, sa, ETH_ALEN);
1560 fip->map_dest = 0;
Joe Eykholte49bf612010-03-12 16:07:57 -08001561 if (fip->state == FIP_ST_AUTO)
1562 LIBFCOE_FIP_DBG(fip, "received non-FIP FLOGI. "
1563 "Setting non-FIP mode\n");
Joe Eykholt9b651da2010-07-20 15:20:24 -07001564 fcoe_ctlr_set_state(fip, FIP_ST_NON_FIP);
Joe Eykholt97c83892009-03-17 11:42:40 -07001565 }
Joe Eykholtfdb068c2010-07-20 15:19:47 -07001566 mutex_unlock(&fip->ctlr_mutex);
Joe Eykholt97c83892009-03-17 11:42:40 -07001567 }
1568 return 0;
1569}
1570EXPORT_SYMBOL(fcoe_ctlr_recv_flogi);
1571
Vasu Dev5e80f7f2009-03-17 11:42:18 -07001572/**
Robert Love70b51aa2009-11-03 11:47:45 -08001573 * fcoe_wwn_from_mac() - Converts a 48-bit IEEE MAC address to a 64-bit FC WWN
1574 * @mac: The MAC address to convert
1575 * @scheme: The scheme to use when converting
1576 * @port: The port indicator for converting
Vasu Dev5e80f7f2009-03-17 11:42:18 -07001577 *
1578 * Returns: u64 fc world wide name
1579 */
1580u64 fcoe_wwn_from_mac(unsigned char mac[MAX_ADDR_LEN],
1581 unsigned int scheme, unsigned int port)
1582{
1583 u64 wwn;
1584 u64 host_mac;
1585
1586 /* The MAC is in NO, so flip only the low 48 bits */
1587 host_mac = ((u64) mac[0] << 40) |
1588 ((u64) mac[1] << 32) |
1589 ((u64) mac[2] << 24) |
1590 ((u64) mac[3] << 16) |
1591 ((u64) mac[4] << 8) |
1592 (u64) mac[5];
1593
1594 WARN_ON(host_mac >= (1ULL << 48));
1595 wwn = host_mac | ((u64) scheme << 60);
1596 switch (scheme) {
1597 case 1:
1598 WARN_ON(port != 0);
1599 break;
1600 case 2:
1601 WARN_ON(port >= 0xfff);
1602 wwn |= (u64) port << 48;
1603 break;
1604 default:
1605 WARN_ON(1);
1606 break;
1607 }
1608
1609 return wwn;
1610}
1611EXPORT_SYMBOL_GPL(fcoe_wwn_from_mac);
1612
1613/**
Joe Eykholte10f8c62010-07-20 15:20:30 -07001614 * fcoe_ctlr_rport() - return the fcoe_rport for a given fc_rport_priv
1615 * @rdata: libfc remote port
1616 */
1617static inline struct fcoe_rport *fcoe_ctlr_rport(struct fc_rport_priv *rdata)
1618{
1619 return (struct fcoe_rport *)(rdata + 1);
1620}
1621
1622/**
1623 * fcoe_ctlr_vn_send() - Send a FIP VN2VN Probe Request or Reply.
1624 * @fip: The FCoE controller
1625 * @sub: sub-opcode for probe request, reply, or advertisement.
1626 * @dest: The destination Ethernet MAC address
1627 * @min_len: minimum size of the Ethernet payload to be sent
1628 */
1629static void fcoe_ctlr_vn_send(struct fcoe_ctlr *fip,
1630 enum fip_vn2vn_subcode sub,
1631 const u8 *dest, size_t min_len)
1632{
1633 struct sk_buff *skb;
1634 struct fip_frame {
1635 struct ethhdr eth;
1636 struct fip_header fip;
1637 struct fip_mac_desc mac;
1638 struct fip_wwn_desc wwnn;
1639 struct fip_vn_desc vn;
1640 } __attribute__((packed)) *frame;
1641 struct fip_fc4_feat *ff;
1642 struct fip_size_desc *size;
1643 u32 fcp_feat;
1644 size_t len;
1645 size_t dlen;
1646
1647 len = sizeof(*frame);
1648 dlen = 0;
1649 if (sub == FIP_SC_VN_CLAIM_NOTIFY || sub == FIP_SC_VN_CLAIM_REP) {
1650 dlen = sizeof(struct fip_fc4_feat) +
1651 sizeof(struct fip_size_desc);
1652 len += dlen;
1653 }
1654 dlen += sizeof(frame->mac) + sizeof(frame->wwnn) + sizeof(frame->vn);
1655 len = max(len, min_len + sizeof(struct ethhdr));
1656
1657 skb = dev_alloc_skb(len);
1658 if (!skb)
1659 return;
1660
1661 frame = (struct fip_frame *)skb->data;
1662 memset(frame, 0, len);
1663 memcpy(frame->eth.h_dest, dest, ETH_ALEN);
1664 memcpy(frame->eth.h_source, fip->ctl_src_addr, ETH_ALEN);
1665 frame->eth.h_proto = htons(ETH_P_FIP);
1666
1667 frame->fip.fip_ver = FIP_VER_ENCAPS(FIP_VER);
1668 frame->fip.fip_op = htons(FIP_OP_VN2VN);
1669 frame->fip.fip_subcode = sub;
1670 frame->fip.fip_dl_len = htons(dlen / FIP_BPW);
1671
1672 frame->mac.fd_desc.fip_dtype = FIP_DT_MAC;
1673 frame->mac.fd_desc.fip_dlen = sizeof(frame->mac) / FIP_BPW;
1674 memcpy(frame->mac.fd_mac, fip->ctl_src_addr, ETH_ALEN);
1675
1676 frame->wwnn.fd_desc.fip_dtype = FIP_DT_NAME;
1677 frame->wwnn.fd_desc.fip_dlen = sizeof(frame->wwnn) / FIP_BPW;
1678 put_unaligned_be64(fip->lp->wwnn, &frame->wwnn.fd_wwn);
1679
1680 frame->vn.fd_desc.fip_dtype = FIP_DT_VN_ID;
1681 frame->vn.fd_desc.fip_dlen = sizeof(frame->vn) / FIP_BPW;
1682 hton24(frame->vn.fd_mac, FIP_VN_FC_MAP);
1683 hton24(frame->vn.fd_mac + 3, fip->port_id);
1684 hton24(frame->vn.fd_fc_id, fip->port_id);
1685 put_unaligned_be64(fip->lp->wwpn, &frame->vn.fd_wwpn);
1686
1687 /*
1688 * For claims, add FC-4 features.
1689 * TBD: Add interface to get fc-4 types and features from libfc.
1690 */
1691 if (sub == FIP_SC_VN_CLAIM_NOTIFY || sub == FIP_SC_VN_CLAIM_REP) {
1692 ff = (struct fip_fc4_feat *)(frame + 1);
1693 ff->fd_desc.fip_dtype = FIP_DT_FC4F;
1694 ff->fd_desc.fip_dlen = sizeof(*ff) / FIP_BPW;
1695 ff->fd_fts = fip->lp->fcts;
1696
1697 fcp_feat = 0;
1698 if (fip->lp->service_params & FCP_SPPF_INIT_FCN)
1699 fcp_feat |= FCP_FEAT_INIT;
1700 if (fip->lp->service_params & FCP_SPPF_TARG_FCN)
1701 fcp_feat |= FCP_FEAT_TARG;
1702 fcp_feat <<= (FC_TYPE_FCP * 4) % 32;
1703 ff->fd_ff.fd_feat[FC_TYPE_FCP * 4 / 32] = htonl(fcp_feat);
1704
1705 size = (struct fip_size_desc *)(ff + 1);
1706 size->fd_desc.fip_dtype = FIP_DT_FCOE_SIZE;
1707 size->fd_desc.fip_dlen = sizeof(*size) / FIP_BPW;
1708 size->fd_size = htons(fcoe_ctlr_fcoe_size(fip));
1709 }
1710
1711 skb_put(skb, len);
1712 skb->protocol = htons(ETH_P_FIP);
1713 skb_reset_mac_header(skb);
1714 skb_reset_network_header(skb);
1715
1716 fip->send(fip, skb);
1717}
1718
1719/**
1720 * fcoe_ctlr_vn_rport_callback - Event handler for rport events.
1721 * @lport: The lport which is receiving the event
1722 * @rdata: remote port private data
1723 * @event: The event that occured
1724 *
1725 * Locking Note: The rport lock must not be held when calling this function.
1726 */
1727static void fcoe_ctlr_vn_rport_callback(struct fc_lport *lport,
1728 struct fc_rport_priv *rdata,
1729 enum fc_rport_event event)
1730{
1731 struct fcoe_ctlr *fip = lport->disc.priv;
1732 struct fcoe_rport *frport = fcoe_ctlr_rport(rdata);
1733
1734 LIBFCOE_FIP_DBG(fip, "vn_rport_callback %x event %d\n",
1735 rdata->ids.port_id, event);
1736
1737 mutex_lock(&fip->ctlr_mutex);
1738 switch (event) {
1739 case RPORT_EV_READY:
1740 frport->login_count = 0;
1741 break;
1742 case RPORT_EV_LOGO:
1743 case RPORT_EV_FAILED:
1744 case RPORT_EV_STOP:
1745 frport->login_count++;
1746 if (frport->login_count > FCOE_CTLR_VN2VN_LOGIN_LIMIT) {
1747 LIBFCOE_FIP_DBG(fip,
1748 "rport FLOGI limited port_id %6.6x\n",
1749 rdata->ids.port_id);
1750 lport->tt.rport_logoff(rdata);
1751 }
1752 break;
1753 default:
1754 break;
1755 }
1756 mutex_unlock(&fip->ctlr_mutex);
1757}
1758
1759static struct fc_rport_operations fcoe_ctlr_vn_rport_ops = {
1760 .event_callback = fcoe_ctlr_vn_rport_callback,
1761};
1762
1763/**
1764 * fcoe_ctlr_disc_stop_locked() - stop discovery in VN2VN mode
1765 * @fip: The FCoE controller
1766 *
1767 * Called with ctlr_mutex held.
1768 */
1769static void fcoe_ctlr_disc_stop_locked(struct fc_lport *lport)
1770{
1771 mutex_lock(&lport->disc.disc_mutex);
1772 lport->disc.disc_callback = NULL;
1773 mutex_unlock(&lport->disc.disc_mutex);
1774}
1775
1776/**
1777 * fcoe_ctlr_disc_stop() - stop discovery in VN2VN mode
1778 * @fip: The FCoE controller
1779 *
1780 * Called through the local port template for discovery.
1781 * Called without the ctlr_mutex held.
1782 */
1783static void fcoe_ctlr_disc_stop(struct fc_lport *lport)
1784{
1785 struct fcoe_ctlr *fip = lport->disc.priv;
1786
1787 mutex_lock(&fip->ctlr_mutex);
1788 fcoe_ctlr_disc_stop_locked(lport);
1789 mutex_unlock(&fip->ctlr_mutex);
1790}
1791
1792/**
1793 * fcoe_ctlr_disc_stop_final() - stop discovery for shutdown in VN2VN mode
1794 * @fip: The FCoE controller
1795 *
1796 * Called through the local port template for discovery.
1797 * Called without the ctlr_mutex held.
1798 */
1799static void fcoe_ctlr_disc_stop_final(struct fc_lport *lport)
1800{
1801 fcoe_ctlr_disc_stop(lport);
1802 lport->tt.rport_flush_queue();
1803 synchronize_rcu();
1804}
1805
1806/**
1807 * fcoe_ctlr_vn_restart() - VN2VN probe restart with new port_id
1808 * @fip: The FCoE controller
1809 *
1810 * Called with fcoe_ctlr lock held.
1811 */
1812static void fcoe_ctlr_vn_restart(struct fcoe_ctlr *fip)
1813{
1814 unsigned long wait;
1815 u32 port_id;
1816
1817 fcoe_ctlr_disc_stop_locked(fip->lp);
1818
1819 /*
1820 * Get proposed port ID.
1821 * If this is the first try after link up, use any previous port_id.
1822 * If there was none, use the low bits of the port_name.
1823 * On subsequent tries, get the next random one.
1824 * Don't use reserved IDs, use another non-zero value, just as random.
1825 */
1826 port_id = fip->port_id;
1827 if (fip->probe_tries)
1828 port_id = prandom32(&fip->rnd_state) & 0xffff;
1829 else if (!port_id)
1830 port_id = fip->lp->wwpn & 0xffff;
1831 if (!port_id || port_id == 0xffff)
1832 port_id = 1;
1833 fip->port_id = port_id;
1834
1835 if (fip->probe_tries < FIP_VN_RLIM_COUNT) {
1836 fip->probe_tries++;
1837 wait = random32() % FIP_VN_PROBE_WAIT;
1838 } else
1839 wait = FIP_VN_RLIM_INT;
1840 mod_timer(&fip->timer, jiffies + msecs_to_jiffies(wait));
1841 fcoe_ctlr_set_state(fip, FIP_ST_VNMP_START);
1842}
1843
1844/**
1845 * fcoe_ctlr_vn_start() - Start in VN2VN mode
1846 * @fip: The FCoE controller
1847 *
1848 * Called with fcoe_ctlr lock held.
1849 */
1850static void fcoe_ctlr_vn_start(struct fcoe_ctlr *fip)
1851{
1852 fip->probe_tries = 0;
1853 prandom32_seed(&fip->rnd_state, fip->lp->wwpn);
1854 fcoe_ctlr_vn_restart(fip);
1855}
1856
1857/**
1858 * fcoe_ctlr_vn_parse - parse probe request or response
1859 * @fip: The FCoE controller
1860 * @skb: incoming packet
1861 * @rdata: buffer for resulting parsed VN entry plus fcoe_rport
1862 *
1863 * Returns non-zero error number on error.
1864 * Does not consume the packet.
1865 */
1866static int fcoe_ctlr_vn_parse(struct fcoe_ctlr *fip,
1867 struct sk_buff *skb,
1868 struct fc_rport_priv *rdata)
1869{
1870 struct fip_header *fiph;
1871 struct fip_desc *desc = NULL;
1872 struct fip_mac_desc *macd = NULL;
1873 struct fip_wwn_desc *wwn = NULL;
1874 struct fip_vn_desc *vn = NULL;
1875 struct fip_size_desc *size = NULL;
1876 struct fcoe_rport *frport;
1877 size_t rlen;
1878 size_t dlen;
1879 u32 desc_mask = 0;
1880 u32 dtype;
1881 u8 sub;
1882
1883 memset(rdata, 0, sizeof(*rdata) + sizeof(*frport));
1884 frport = fcoe_ctlr_rport(rdata);
1885
1886 fiph = (struct fip_header *)skb->data;
1887 frport->flags = ntohs(fiph->fip_flags);
1888
1889 sub = fiph->fip_subcode;
1890 switch (sub) {
1891 case FIP_SC_VN_PROBE_REQ:
1892 case FIP_SC_VN_PROBE_REP:
1893 case FIP_SC_VN_BEACON:
1894 desc_mask = BIT(FIP_DT_MAC) | BIT(FIP_DT_NAME) |
1895 BIT(FIP_DT_VN_ID);
1896 break;
1897 case FIP_SC_VN_CLAIM_NOTIFY:
1898 case FIP_SC_VN_CLAIM_REP:
1899 desc_mask = BIT(FIP_DT_MAC) | BIT(FIP_DT_NAME) |
1900 BIT(FIP_DT_VN_ID) | BIT(FIP_DT_FC4F) |
1901 BIT(FIP_DT_FCOE_SIZE);
1902 break;
1903 default:
1904 LIBFCOE_FIP_DBG(fip, "vn_parse unknown subcode %u\n", sub);
1905 return -EINVAL;
1906 }
1907
1908 rlen = ntohs(fiph->fip_dl_len) * 4;
1909 if (rlen + sizeof(*fiph) > skb->len)
1910 return -EINVAL;
1911
1912 desc = (struct fip_desc *)(fiph + 1);
1913 while (rlen > 0) {
1914 dlen = desc->fip_dlen * FIP_BPW;
1915 if (dlen < sizeof(*desc) || dlen > rlen)
1916 return -EINVAL;
1917
1918 dtype = desc->fip_dtype;
1919 if (dtype < 32) {
1920 if (!(desc_mask & BIT(dtype))) {
1921 LIBFCOE_FIP_DBG(fip,
1922 "unexpected or duplicated desc "
1923 "desc type %u in "
1924 "FIP VN2VN subtype %u\n",
1925 dtype, sub);
1926 return -EINVAL;
1927 }
1928 desc_mask &= ~BIT(dtype);
1929 }
1930
1931 switch (dtype) {
1932 case FIP_DT_MAC:
1933 if (dlen != sizeof(struct fip_mac_desc))
1934 goto len_err;
1935 macd = (struct fip_mac_desc *)desc;
1936 if (!is_valid_ether_addr(macd->fd_mac)) {
1937 LIBFCOE_FIP_DBG(fip,
1938 "Invalid MAC addr %pM in FIP VN2VN\n",
1939 macd->fd_mac);
1940 return -EINVAL;
1941 }
1942 memcpy(frport->enode_mac, macd->fd_mac, ETH_ALEN);
1943 break;
1944 case FIP_DT_NAME:
1945 if (dlen != sizeof(struct fip_wwn_desc))
1946 goto len_err;
1947 wwn = (struct fip_wwn_desc *)desc;
1948 rdata->ids.node_name = get_unaligned_be64(&wwn->fd_wwn);
1949 break;
1950 case FIP_DT_VN_ID:
1951 if (dlen != sizeof(struct fip_vn_desc))
1952 goto len_err;
1953 vn = (struct fip_vn_desc *)desc;
1954 memcpy(frport->vn_mac, vn->fd_mac, ETH_ALEN);
1955 rdata->ids.port_id = ntoh24(vn->fd_fc_id);
1956 rdata->ids.port_name = get_unaligned_be64(&vn->fd_wwpn);
1957 break;
1958 case FIP_DT_FC4F:
1959 if (dlen != sizeof(struct fip_fc4_feat))
1960 goto len_err;
1961 break;
1962 case FIP_DT_FCOE_SIZE:
1963 if (dlen != sizeof(struct fip_size_desc))
1964 goto len_err;
1965 size = (struct fip_size_desc *)desc;
1966 frport->fcoe_len = ntohs(size->fd_size);
1967 break;
1968 default:
1969 LIBFCOE_FIP_DBG(fip, "unexpected descriptor type %x "
1970 "in FIP probe\n", dtype);
1971 /* standard says ignore unknown descriptors >= 128 */
1972 if (dtype < FIP_DT_VENDOR_BASE)
1973 return -EINVAL;
1974 break;
1975 }
1976 desc = (struct fip_desc *)((char *)desc + dlen);
1977 rlen -= dlen;
1978 }
1979 return 0;
1980
1981len_err:
1982 LIBFCOE_FIP_DBG(fip, "FIP length error in descriptor type %x len %zu\n",
1983 dtype, dlen);
1984 return -EINVAL;
1985}
1986
1987/**
1988 * fcoe_ctlr_vn_send_claim() - send multicast FIP VN2VN Claim Notification.
1989 * @fip: The FCoE controller
1990 *
1991 * Called with ctlr_mutex held.
1992 */
1993static void fcoe_ctlr_vn_send_claim(struct fcoe_ctlr *fip)
1994{
1995 fcoe_ctlr_vn_send(fip, FIP_SC_VN_CLAIM_NOTIFY, fcoe_all_vn2vn, 0);
1996 fip->sol_time = jiffies;
1997}
1998
1999/**
2000 * fcoe_ctlr_vn_probe_req() - handle incoming VN2VN probe request.
2001 * @fip: The FCoE controller
2002 * @rdata: parsed remote port with frport from the probe request
2003 *
2004 * Called with ctlr_mutex held.
2005 */
2006static void fcoe_ctlr_vn_probe_req(struct fcoe_ctlr *fip,
2007 struct fc_rport_priv *rdata)
2008{
2009 struct fcoe_rport *frport = fcoe_ctlr_rport(rdata);
2010
2011 if (rdata->ids.port_id != fip->port_id)
2012 return;
2013
2014 switch (fip->state) {
2015 case FIP_ST_VNMP_CLAIM:
2016 case FIP_ST_VNMP_UP:
2017 fcoe_ctlr_vn_send(fip, FIP_SC_VN_PROBE_REP,
2018 frport->enode_mac, 0);
2019 break;
2020 case FIP_ST_VNMP_PROBE1:
2021 case FIP_ST_VNMP_PROBE2:
2022 /*
2023 * Decide whether to reply to the Probe.
2024 * Our selected address is never a "recorded" one, so
2025 * only reply if our WWPN is greater and the
2026 * Probe's REC bit is not set.
2027 * If we don't reply, we will change our address.
2028 */
2029 if (fip->lp->wwpn > rdata->ids.port_name &&
2030 !(frport->flags & FIP_FL_REC_OR_P2P)) {
2031 fcoe_ctlr_vn_send(fip, FIP_SC_VN_PROBE_REP,
2032 frport->enode_mac, 0);
2033 break;
2034 }
2035 /* fall through */
2036 case FIP_ST_VNMP_START:
2037 fcoe_ctlr_vn_restart(fip);
2038 break;
2039 default:
2040 break;
2041 }
2042}
2043
2044/**
2045 * fcoe_ctlr_vn_probe_reply() - handle incoming VN2VN probe reply.
2046 * @fip: The FCoE controller
2047 * @rdata: parsed remote port with frport from the probe request
2048 *
2049 * Called with ctlr_mutex held.
2050 */
2051static void fcoe_ctlr_vn_probe_reply(struct fcoe_ctlr *fip,
2052 struct fc_rport_priv *rdata)
2053{
2054 if (rdata->ids.port_id != fip->port_id)
2055 return;
2056 switch (fip->state) {
2057 case FIP_ST_VNMP_START:
2058 case FIP_ST_VNMP_PROBE1:
2059 case FIP_ST_VNMP_PROBE2:
2060 case FIP_ST_VNMP_CLAIM:
2061 fcoe_ctlr_vn_restart(fip);
2062 break;
2063 case FIP_ST_VNMP_UP:
2064 fcoe_ctlr_vn_send_claim(fip);
2065 break;
2066 default:
2067 break;
2068 }
2069}
2070
2071/**
2072 * fcoe_ctlr_vn_add() - Add a VN2VN entry to the list, based on a claim reply.
2073 * @fip: The FCoE controller
2074 * @new: newly-parsed remote port with frport as a template for new rdata
2075 *
2076 * Called with ctlr_mutex held.
2077 */
2078static void fcoe_ctlr_vn_add(struct fcoe_ctlr *fip, struct fc_rport_priv *new)
2079{
2080 struct fc_lport *lport = fip->lp;
2081 struct fc_rport_priv *rdata;
2082 struct fc_rport_identifiers *ids;
2083 struct fcoe_rport *frport;
2084 u32 port_id;
2085
2086 port_id = new->ids.port_id;
2087 if (port_id == fip->port_id)
2088 return;
2089
2090 mutex_lock(&lport->disc.disc_mutex);
2091 rdata = lport->tt.rport_create(lport, port_id);
2092 if (!rdata) {
2093 mutex_unlock(&lport->disc.disc_mutex);
2094 return;
2095 }
2096
2097 rdata->ops = &fcoe_ctlr_vn_rport_ops;
2098 rdata->disc_id = lport->disc.disc_id;
2099
2100 ids = &rdata->ids;
2101 if ((ids->port_name != -1 && ids->port_name != new->ids.port_name) ||
2102 (ids->node_name != -1 && ids->node_name != new->ids.node_name))
2103 lport->tt.rport_logoff(rdata);
2104 ids->port_name = new->ids.port_name;
2105 ids->node_name = new->ids.node_name;
2106 mutex_unlock(&lport->disc.disc_mutex);
2107
2108 frport = fcoe_ctlr_rport(rdata);
2109 LIBFCOE_FIP_DBG(fip, "vn_add rport %6.6x %s\n",
2110 port_id, frport->fcoe_len ? "old" : "new");
2111 *frport = *fcoe_ctlr_rport(new);
2112 frport->time = 0;
2113}
2114
2115/**
2116 * fcoe_ctlr_vn_lookup() - Find VN remote port's MAC address
2117 * @fip: The FCoE controller
2118 * @port_id: The port_id of the remote VN_node
2119 * @mac: buffer which will hold the VN_NODE destination MAC address, if found.
2120 *
2121 * Returns non-zero error if no remote port found.
2122 */
2123static int fcoe_ctlr_vn_lookup(struct fcoe_ctlr *fip, u32 port_id, u8 *mac)
2124{
2125 struct fc_lport *lport = fip->lp;
2126 struct fc_rport_priv *rdata;
2127 struct fcoe_rport *frport;
2128 int ret = -1;
2129
2130 rcu_read_lock();
2131 rdata = lport->tt.rport_lookup(lport, port_id);
2132 if (rdata) {
2133 frport = fcoe_ctlr_rport(rdata);
2134 memcpy(mac, frport->enode_mac, ETH_ALEN);
2135 ret = 0;
2136 }
2137 rcu_read_unlock();
2138 return ret;
2139}
2140
2141/**
2142 * fcoe_ctlr_vn_claim_notify() - handle received FIP VN2VN Claim Notification
2143 * @fip: The FCoE controller
2144 * @new: newly-parsed remote port with frport as a template for new rdata
2145 *
2146 * Called with ctlr_mutex held.
2147 */
2148static void fcoe_ctlr_vn_claim_notify(struct fcoe_ctlr *fip,
2149 struct fc_rport_priv *new)
2150{
2151 struct fcoe_rport *frport = fcoe_ctlr_rport(new);
2152
2153 if (frport->flags & FIP_FL_REC_OR_P2P) {
2154 fcoe_ctlr_vn_send(fip, FIP_SC_VN_PROBE_REQ, fcoe_all_vn2vn, 0);
2155 return;
2156 }
2157 switch (fip->state) {
2158 case FIP_ST_VNMP_START:
2159 case FIP_ST_VNMP_PROBE1:
2160 case FIP_ST_VNMP_PROBE2:
2161 if (new->ids.port_id == fip->port_id)
2162 fcoe_ctlr_vn_restart(fip);
2163 break;
2164 case FIP_ST_VNMP_CLAIM:
2165 case FIP_ST_VNMP_UP:
2166 if (new->ids.port_id == fip->port_id) {
2167 if (new->ids.port_name > fip->lp->wwpn) {
2168 fcoe_ctlr_vn_restart(fip);
2169 break;
2170 }
2171 fcoe_ctlr_vn_send_claim(fip);
2172 break;
2173 }
2174 fcoe_ctlr_vn_send(fip, FIP_SC_VN_CLAIM_REP, frport->enode_mac,
2175 min((u32)frport->fcoe_len,
2176 fcoe_ctlr_fcoe_size(fip)));
2177 fcoe_ctlr_vn_add(fip, new);
2178 break;
2179 default:
2180 break;
2181 }
2182}
2183
2184/**
2185 * fcoe_ctlr_vn_claim_resp() - handle received Claim Response
2186 * @fip: The FCoE controller that received the frame
2187 * @new: newly-parsed remote port with frport from the Claim Response
2188 *
2189 * Called with ctlr_mutex held.
2190 */
2191static void fcoe_ctlr_vn_claim_resp(struct fcoe_ctlr *fip,
2192 struct fc_rport_priv *new)
2193{
2194 LIBFCOE_FIP_DBG(fip, "claim resp from from rport %x - state %s\n",
2195 new->ids.port_id, fcoe_ctlr_state(fip->state));
2196 if (fip->state == FIP_ST_VNMP_UP || fip->state == FIP_ST_VNMP_CLAIM)
2197 fcoe_ctlr_vn_add(fip, new);
2198}
2199
2200/**
2201 * fcoe_ctlr_vn_beacon() - handle received beacon.
2202 * @fip: The FCoE controller that received the frame
2203 * @new: newly-parsed remote port with frport from the Beacon
2204 *
2205 * Called with ctlr_mutex held.
2206 */
2207static void fcoe_ctlr_vn_beacon(struct fcoe_ctlr *fip,
2208 struct fc_rport_priv *new)
2209{
2210 struct fc_lport *lport = fip->lp;
2211 struct fc_rport_priv *rdata;
2212 struct fcoe_rport *frport;
2213
2214 frport = fcoe_ctlr_rport(new);
2215 if (frport->flags & FIP_FL_REC_OR_P2P) {
2216 fcoe_ctlr_vn_send(fip, FIP_SC_VN_PROBE_REQ, fcoe_all_vn2vn, 0);
2217 return;
2218 }
2219 mutex_lock(&lport->disc.disc_mutex);
2220 rdata = lport->tt.rport_lookup(lport, new->ids.port_id);
2221 if (rdata)
2222 kref_get(&rdata->kref);
2223 mutex_unlock(&lport->disc.disc_mutex);
2224 if (rdata) {
2225 if (rdata->ids.node_name == new->ids.node_name &&
2226 rdata->ids.port_name == new->ids.port_name) {
2227 frport = fcoe_ctlr_rport(rdata);
2228 if (!frport->time && fip->state == FIP_ST_VNMP_UP)
2229 lport->tt.rport_login(rdata);
2230 frport->time = jiffies;
2231 }
2232 kref_put(&rdata->kref, lport->tt.rport_destroy);
2233 return;
2234 }
2235 if (fip->state != FIP_ST_VNMP_UP)
2236 return;
2237
2238 /*
2239 * Beacon from a new neighbor.
2240 * Send a claim notify if one hasn't been sent recently.
2241 * Don't add the neighbor yet.
2242 */
2243 LIBFCOE_FIP_DBG(fip, "beacon from new rport %x. sending claim notify\n",
2244 new->ids.port_id);
2245 if (time_after(jiffies,
2246 fip->sol_time + msecs_to_jiffies(FIP_VN_ANN_WAIT)))
2247 fcoe_ctlr_vn_send_claim(fip);
2248}
2249
2250/**
2251 * fcoe_ctlr_vn_age() - Check for VN_ports without recent beacons
2252 * @fip: The FCoE controller
2253 *
2254 * Called with ctlr_mutex held.
2255 * Called only in state FIP_ST_VNMP_UP.
2256 * Returns the soonest time for next age-out or a time far in the future.
2257 */
2258static unsigned long fcoe_ctlr_vn_age(struct fcoe_ctlr *fip)
2259{
2260 struct fc_lport *lport = fip->lp;
2261 struct fc_rport_priv *rdata;
2262 struct fcoe_rport *frport;
2263 unsigned long next_time;
2264 unsigned long deadline;
2265
2266 next_time = jiffies + msecs_to_jiffies(FIP_VN_BEACON_INT * 10);
2267 mutex_lock(&lport->disc.disc_mutex);
2268 list_for_each_entry_rcu(rdata, &lport->disc.rports, peers) {
2269 frport = fcoe_ctlr_rport(rdata);
2270 if (!frport->time)
2271 continue;
2272 deadline = frport->time +
2273 msecs_to_jiffies(FIP_VN_BEACON_INT * 25 / 10);
2274 if (time_after_eq(jiffies, deadline)) {
2275 frport->time = 0;
2276 LIBFCOE_FIP_DBG(fip,
2277 "port %16.16llx fc_id %6.6x beacon expired\n",
2278 rdata->ids.port_name, rdata->ids.port_id);
2279 lport->tt.rport_logoff(rdata);
2280 } else if (time_before(deadline, next_time))
2281 next_time = deadline;
2282 }
2283 mutex_unlock(&lport->disc.disc_mutex);
2284 return next_time;
2285}
2286
2287/**
2288 * fcoe_ctlr_vn_recv() - Receive a FIP frame
2289 * @fip: The FCoE controller that received the frame
2290 * @skb: The received FIP frame
2291 *
2292 * Returns non-zero if the frame is dropped.
2293 * Always consumes the frame.
2294 */
2295static int fcoe_ctlr_vn_recv(struct fcoe_ctlr *fip, struct sk_buff *skb)
2296{
2297 struct fip_header *fiph;
2298 enum fip_vn2vn_subcode sub;
Kiran Patil2dc02ee2010-10-08 17:12:41 -07002299 struct {
Joe Eykholte10f8c62010-07-20 15:20:30 -07002300 struct fc_rport_priv rdata;
2301 struct fcoe_rport frport;
2302 } buf;
2303 int rc;
2304
2305 fiph = (struct fip_header *)skb->data;
2306 sub = fiph->fip_subcode;
2307
2308 rc = fcoe_ctlr_vn_parse(fip, skb, &buf.rdata);
2309 if (rc) {
2310 LIBFCOE_FIP_DBG(fip, "vn_recv vn_parse error %d\n", rc);
2311 goto drop;
2312 }
2313
2314 mutex_lock(&fip->ctlr_mutex);
2315 switch (sub) {
2316 case FIP_SC_VN_PROBE_REQ:
2317 fcoe_ctlr_vn_probe_req(fip, &buf.rdata);
2318 break;
2319 case FIP_SC_VN_PROBE_REP:
2320 fcoe_ctlr_vn_probe_reply(fip, &buf.rdata);
2321 break;
2322 case FIP_SC_VN_CLAIM_NOTIFY:
2323 fcoe_ctlr_vn_claim_notify(fip, &buf.rdata);
2324 break;
2325 case FIP_SC_VN_CLAIM_REP:
2326 fcoe_ctlr_vn_claim_resp(fip, &buf.rdata);
2327 break;
2328 case FIP_SC_VN_BEACON:
2329 fcoe_ctlr_vn_beacon(fip, &buf.rdata);
2330 break;
2331 default:
2332 LIBFCOE_FIP_DBG(fip, "vn_recv unknown subcode %d\n", sub);
2333 rc = -1;
2334 break;
2335 }
2336 mutex_unlock(&fip->ctlr_mutex);
2337drop:
2338 kfree_skb(skb);
2339 return rc;
2340}
2341
2342/**
2343 * fcoe_ctlr_disc_recv - discovery receive handler for VN2VN mode.
Joe Eykholt922611562010-07-20 15:21:12 -07002344 * @lport: The local port
2345 * @fp: The received frame
Joe Eykholte10f8c62010-07-20 15:20:30 -07002346 *
2347 * This should never be called since we don't see RSCNs or other
2348 * fabric-generated ELSes.
2349 */
Joe Eykholt922611562010-07-20 15:21:12 -07002350static void fcoe_ctlr_disc_recv(struct fc_lport *lport, struct fc_frame *fp)
Joe Eykholte10f8c62010-07-20 15:20:30 -07002351{
2352 struct fc_seq_els_data rjt_data;
2353
Joe Eykholte10f8c62010-07-20 15:20:30 -07002354 rjt_data.reason = ELS_RJT_UNSUP;
2355 rjt_data.explan = ELS_EXPL_NONE;
Joe Eykholt922611562010-07-20 15:21:12 -07002356 lport->tt.seq_els_rsp_send(fp, ELS_LS_RJT, &rjt_data);
Joe Eykholte10f8c62010-07-20 15:20:30 -07002357 fc_frame_free(fp);
2358}
2359
2360/**
2361 * fcoe_ctlr_disc_recv - start discovery for VN2VN mode.
2362 * @fip: The FCoE controller
2363 *
2364 * This sets a flag indicating that remote ports should be created
2365 * and started for the peers we discover. We use the disc_callback
2366 * pointer as that flag. Peers already discovered are created here.
2367 *
2368 * The lport lock is held during this call. The callback must be done
2369 * later, without holding either the lport or discovery locks.
2370 * The fcoe_ctlr lock may also be held during this call.
2371 */
2372static void fcoe_ctlr_disc_start(void (*callback)(struct fc_lport *,
2373 enum fc_disc_event),
2374 struct fc_lport *lport)
2375{
2376 struct fc_disc *disc = &lport->disc;
2377 struct fcoe_ctlr *fip = disc->priv;
2378
2379 mutex_lock(&disc->disc_mutex);
2380 disc->disc_callback = callback;
2381 disc->disc_id = (disc->disc_id + 2) | 1;
2382 disc->pending = 1;
2383 schedule_work(&fip->timer_work);
2384 mutex_unlock(&disc->disc_mutex);
2385}
2386
2387/**
2388 * fcoe_ctlr_vn_disc() - report FIP VN_port discovery results after claim state.
2389 * @fip: The FCoE controller
2390 *
2391 * Starts the FLOGI and PLOGI login process to each discovered rport for which
2392 * we've received at least one beacon.
2393 * Performs the discovery complete callback.
2394 */
2395static void fcoe_ctlr_vn_disc(struct fcoe_ctlr *fip)
2396{
2397 struct fc_lport *lport = fip->lp;
2398 struct fc_disc *disc = &lport->disc;
2399 struct fc_rport_priv *rdata;
2400 struct fcoe_rport *frport;
2401 void (*callback)(struct fc_lport *, enum fc_disc_event);
2402
2403 mutex_lock(&disc->disc_mutex);
2404 callback = disc->pending ? disc->disc_callback : NULL;
2405 disc->pending = 0;
2406 list_for_each_entry_rcu(rdata, &disc->rports, peers) {
2407 frport = fcoe_ctlr_rport(rdata);
2408 if (frport->time)
2409 lport->tt.rport_login(rdata);
2410 }
2411 mutex_unlock(&disc->disc_mutex);
2412 if (callback)
2413 callback(lport, DISC_EV_SUCCESS);
2414}
2415
2416/**
2417 * fcoe_ctlr_vn_timeout - timer work function for VN2VN mode.
2418 * @fip: The FCoE controller
2419 */
2420static void fcoe_ctlr_vn_timeout(struct fcoe_ctlr *fip)
2421{
2422 unsigned long next_time;
2423 u8 mac[ETH_ALEN];
2424 u32 new_port_id = 0;
2425
2426 mutex_lock(&fip->ctlr_mutex);
2427 switch (fip->state) {
2428 case FIP_ST_VNMP_START:
2429 fcoe_ctlr_set_state(fip, FIP_ST_VNMP_PROBE1);
2430 fcoe_ctlr_vn_send(fip, FIP_SC_VN_PROBE_REQ, fcoe_all_vn2vn, 0);
2431 next_time = jiffies + msecs_to_jiffies(FIP_VN_PROBE_WAIT);
2432 break;
2433 case FIP_ST_VNMP_PROBE1:
2434 fcoe_ctlr_set_state(fip, FIP_ST_VNMP_PROBE2);
2435 fcoe_ctlr_vn_send(fip, FIP_SC_VN_PROBE_REQ, fcoe_all_vn2vn, 0);
2436 next_time = jiffies + msecs_to_jiffies(FIP_VN_ANN_WAIT);
2437 break;
2438 case FIP_ST_VNMP_PROBE2:
2439 fcoe_ctlr_set_state(fip, FIP_ST_VNMP_CLAIM);
2440 new_port_id = fip->port_id;
2441 hton24(mac, FIP_VN_FC_MAP);
2442 hton24(mac + 3, new_port_id);
Joe Eykholtcd229e42010-07-20 15:20:40 -07002443 fcoe_ctlr_map_dest(fip);
Joe Eykholte10f8c62010-07-20 15:20:30 -07002444 fip->update_mac(fip->lp, mac);
2445 fcoe_ctlr_vn_send_claim(fip);
2446 next_time = jiffies + msecs_to_jiffies(FIP_VN_ANN_WAIT);
2447 break;
2448 case FIP_ST_VNMP_CLAIM:
2449 /*
2450 * This may be invoked either by starting discovery so don't
2451 * go to the next state unless it's been long enough.
2452 */
2453 next_time = fip->sol_time + msecs_to_jiffies(FIP_VN_ANN_WAIT);
2454 if (time_after_eq(jiffies, next_time)) {
2455 fcoe_ctlr_set_state(fip, FIP_ST_VNMP_UP);
2456 fcoe_ctlr_vn_send(fip, FIP_SC_VN_BEACON,
2457 fcoe_all_vn2vn, 0);
2458 next_time = jiffies + msecs_to_jiffies(FIP_VN_ANN_WAIT);
2459 fip->port_ka_time = next_time;
2460 }
2461 fcoe_ctlr_vn_disc(fip);
2462 break;
2463 case FIP_ST_VNMP_UP:
2464 next_time = fcoe_ctlr_vn_age(fip);
2465 if (time_after_eq(jiffies, fip->port_ka_time)) {
2466 fcoe_ctlr_vn_send(fip, FIP_SC_VN_BEACON,
2467 fcoe_all_vn2vn, 0);
2468 fip->port_ka_time = jiffies +
2469 msecs_to_jiffies(FIP_VN_BEACON_INT +
2470 (random32() % FIP_VN_BEACON_FUZZ));
2471 }
2472 if (time_before(fip->port_ka_time, next_time))
2473 next_time = fip->port_ka_time;
2474 break;
2475 case FIP_ST_LINK_WAIT:
2476 goto unlock;
2477 default:
2478 WARN(1, "unexpected state %d", fip->state);
2479 goto unlock;
2480 }
2481 mod_timer(&fip->timer, next_time);
2482unlock:
2483 mutex_unlock(&fip->ctlr_mutex);
2484
2485 /* If port ID is new, notify local port after dropping ctlr_mutex */
2486 if (new_port_id)
2487 fc_lport_set_local_id(fip->lp, new_port_id);
2488}
2489
2490/**
Robert Love70b51aa2009-11-03 11:47:45 -08002491 * fcoe_libfc_config() - Sets up libfc related properties for local port
2492 * @lp: The local port to configure libfc for
Joe Eykholte10f8c62010-07-20 15:20:30 -07002493 * @fip: The FCoE controller in use by the local port
Robert Love70b51aa2009-11-03 11:47:45 -08002494 * @tt: The libfc function template
Joe Eykholte10f8c62010-07-20 15:20:30 -07002495 * @init_fcp: If non-zero, the FCP portion of libfc should be initialized
Vasu Dev5e80f7f2009-03-17 11:42:18 -07002496 *
2497 * Returns : 0 for success
2498 */
Joe Eykholte10f8c62010-07-20 15:20:30 -07002499int fcoe_libfc_config(struct fc_lport *lport, struct fcoe_ctlr *fip,
2500 const struct libfc_function_template *tt, int init_fcp)
Vasu Dev5e80f7f2009-03-17 11:42:18 -07002501{
2502 /* Set the function pointers set by the LLDD */
Robert Love70b51aa2009-11-03 11:47:45 -08002503 memcpy(&lport->tt, tt, sizeof(*tt));
Joe Eykholte10f8c62010-07-20 15:20:30 -07002504 if (init_fcp && fc_fcp_init(lport))
Vasu Dev5e80f7f2009-03-17 11:42:18 -07002505 return -ENOMEM;
Robert Love70b51aa2009-11-03 11:47:45 -08002506 fc_exch_init(lport);
2507 fc_elsct_init(lport);
2508 fc_lport_init(lport);
Joe Eykholte10f8c62010-07-20 15:20:30 -07002509 if (fip->mode == FIP_MODE_VN2VN)
2510 lport->rport_priv_size = sizeof(struct fcoe_rport);
Robert Love70b51aa2009-11-03 11:47:45 -08002511 fc_rport_init(lport);
Joe Eykholte10f8c62010-07-20 15:20:30 -07002512 if (fip->mode == FIP_MODE_VN2VN) {
2513 lport->point_to_multipoint = 1;
2514 lport->tt.disc_recv_req = fcoe_ctlr_disc_recv;
2515 lport->tt.disc_start = fcoe_ctlr_disc_start;
2516 lport->tt.disc_stop = fcoe_ctlr_disc_stop;
2517 lport->tt.disc_stop_final = fcoe_ctlr_disc_stop_final;
2518 mutex_init(&lport->disc.disc_mutex);
2519 INIT_LIST_HEAD(&lport->disc.rports);
2520 lport->disc.priv = fip;
2521 } else {
2522 fc_disc_init(lport);
2523 }
Vasu Dev5e80f7f2009-03-17 11:42:18 -07002524 return 0;
2525}
2526EXPORT_SYMBOL_GPL(fcoe_libfc_config);