blob: 826c260f7951119765e22cf08436f0f7c472401e [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/**
Joe Eykholt69316ee2010-11-30 16:19:40 -0800234 * fcoe_ctlr_announce() - announce new selection
235 * @fip: The FCoE controller
236 *
237 * Also sets the destination MAC for FCoE and control packets
238 */
239static void fcoe_ctlr_announce(struct fcoe_ctlr *fip)
240{
241 struct fcoe_fcf *sel = fip->sel_fcf;
242
243 if (sel && !compare_ether_addr(sel->fcf_mac, fip->dest_addr))
244 return;
245 if (!is_zero_ether_addr(fip->dest_addr)) {
246 printk(KERN_NOTICE "libfcoe: host%d: "
247 "FIP Fibre-Channel Forwarder MAC %pM deselected\n",
248 fip->lp->host->host_no, fip->dest_addr);
249 memset(fip->dest_addr, 0, ETH_ALEN);
250 }
251 if (sel) {
252 printk(KERN_INFO "libfcoe: host%d: FIP selected "
253 "Fibre-Channel Forwarder MAC %pM\n",
254 fip->lp->host->host_no, sel->fcf_mac);
255 memcpy(fip->dest_addr, sel->fcf_mac, ETH_ALEN);
256 fip->map_dest = 0;
257 }
258}
259
260/**
Robert Love70b51aa2009-11-03 11:47:45 -0800261 * fcoe_ctlr_fcoe_size() - Return the maximum FCoE size required for VN_Port
262 * @fip: The FCoE controller to get the maximum FCoE size from
Joe Eykholt97c83892009-03-17 11:42:40 -0700263 *
264 * Returns the maximum packet size including the FCoE header and trailer,
265 * but not including any Ethernet or VLAN headers.
266 */
267static inline u32 fcoe_ctlr_fcoe_size(struct fcoe_ctlr *fip)
268{
269 /*
270 * Determine the max FCoE frame size allowed, including
271 * FCoE header and trailer.
272 * Note: lp->mfs is currently the payload size, not the frame size.
273 */
274 return fip->lp->mfs + sizeof(struct fc_frame_header) +
275 sizeof(struct fcoe_hdr) + sizeof(struct fcoe_crc_eof);
276}
277
278/**
Robert Love70b51aa2009-11-03 11:47:45 -0800279 * fcoe_ctlr_solicit() - Send a FIP solicitation
280 * @fip: The FCoE controller to send the solicitation on
281 * @fcf: The destination FCF (if NULL, a multicast solicitation is sent)
Joe Eykholt97c83892009-03-17 11:42:40 -0700282 */
283static void fcoe_ctlr_solicit(struct fcoe_ctlr *fip, struct fcoe_fcf *fcf)
284{
285 struct sk_buff *skb;
286 struct fip_sol {
287 struct ethhdr eth;
288 struct fip_header fip;
289 struct {
290 struct fip_mac_desc mac;
291 struct fip_wwn_desc wwnn;
292 struct fip_size_desc size;
293 } __attribute__((packed)) desc;
294 } __attribute__((packed)) *sol;
295 u32 fcoe_size;
296
297 skb = dev_alloc_skb(sizeof(*sol));
298 if (!skb)
299 return;
300
301 sol = (struct fip_sol *)skb->data;
302
303 memset(sol, 0, sizeof(*sol));
304 memcpy(sol->eth.h_dest, fcf ? fcf->fcf_mac : fcoe_all_fcfs, ETH_ALEN);
305 memcpy(sol->eth.h_source, fip->ctl_src_addr, ETH_ALEN);
306 sol->eth.h_proto = htons(ETH_P_FIP);
307
308 sol->fip.fip_ver = FIP_VER_ENCAPS(FIP_VER);
309 sol->fip.fip_op = htons(FIP_OP_DISC);
310 sol->fip.fip_subcode = FIP_SC_SOL;
311 sol->fip.fip_dl_len = htons(sizeof(sol->desc) / FIP_BPW);
312 sol->fip.fip_flags = htons(FIP_FL_FPMA);
Vasu Dev184dd342009-05-17 12:33:28 +0000313 if (fip->spma)
314 sol->fip.fip_flags |= htons(FIP_FL_SPMA);
Joe Eykholt97c83892009-03-17 11:42:40 -0700315
316 sol->desc.mac.fd_desc.fip_dtype = FIP_DT_MAC;
317 sol->desc.mac.fd_desc.fip_dlen = sizeof(sol->desc.mac) / FIP_BPW;
318 memcpy(sol->desc.mac.fd_mac, fip->ctl_src_addr, ETH_ALEN);
319
320 sol->desc.wwnn.fd_desc.fip_dtype = FIP_DT_NAME;
321 sol->desc.wwnn.fd_desc.fip_dlen = sizeof(sol->desc.wwnn) / FIP_BPW;
322 put_unaligned_be64(fip->lp->wwnn, &sol->desc.wwnn.fd_wwn);
323
324 fcoe_size = fcoe_ctlr_fcoe_size(fip);
325 sol->desc.size.fd_desc.fip_dtype = FIP_DT_FCOE_SIZE;
326 sol->desc.size.fd_desc.fip_dlen = sizeof(sol->desc.size) / FIP_BPW;
327 sol->desc.size.fd_size = htons(fcoe_size);
328
329 skb_put(skb, sizeof(*sol));
Chris Leech0f491532009-05-06 10:52:18 -0700330 skb->protocol = htons(ETH_P_FIP);
Joe Eykholt97c83892009-03-17 11:42:40 -0700331 skb_reset_mac_header(skb);
332 skb_reset_network_header(skb);
333 fip->send(fip, skb);
334
335 if (!fcf)
336 fip->sol_time = jiffies;
337}
338
339/**
Robert Love70b51aa2009-11-03 11:47:45 -0800340 * fcoe_ctlr_link_up() - Start FCoE controller
341 * @fip: The FCoE controller to start
Joe Eykholt97c83892009-03-17 11:42:40 -0700342 *
343 * Called from the LLD when the network link is ready.
344 */
345void fcoe_ctlr_link_up(struct fcoe_ctlr *fip)
346{
Joe Eykholtfdb068c2010-07-20 15:19:47 -0700347 mutex_lock(&fip->ctlr_mutex);
Joe Eykholt97c83892009-03-17 11:42:40 -0700348 if (fip->state == FIP_ST_NON_FIP || fip->state == FIP_ST_AUTO) {
Joe Eykholtfdb068c2010-07-20 15:19:47 -0700349 mutex_unlock(&fip->ctlr_mutex);
Joe Eykholt97c83892009-03-17 11:42:40 -0700350 fc_linkup(fip->lp);
351 } else if (fip->state == FIP_ST_LINK_WAIT) {
Joe Eykholt9b651da2010-07-20 15:20:24 -0700352 fcoe_ctlr_set_state(fip, fip->mode);
Joe Eykholte10f8c62010-07-20 15:20:30 -0700353 switch (fip->mode) {
354 default:
355 LIBFCOE_FIP_DBG(fip, "invalid mode %d\n", fip->mode);
356 /* fall-through */
357 case FIP_MODE_AUTO:
Joe Eykholt0f51c2e2009-11-03 11:48:16 -0800358 LIBFCOE_FIP_DBG(fip, "%s", "setting AUTO mode.\n");
Joe Eykholte10f8c62010-07-20 15:20:30 -0700359 /* fall-through */
360 case FIP_MODE_FABRIC:
361 case FIP_MODE_NON_FIP:
362 mutex_unlock(&fip->ctlr_mutex);
363 fc_linkup(fip->lp);
364 fcoe_ctlr_solicit(fip, NULL);
365 break;
366 case FIP_MODE_VN2VN:
367 fcoe_ctlr_vn_start(fip);
368 mutex_unlock(&fip->ctlr_mutex);
369 fc_linkup(fip->lp);
370 break;
371 }
Joe Eykholt97c83892009-03-17 11:42:40 -0700372 } else
Joe Eykholtfdb068c2010-07-20 15:19:47 -0700373 mutex_unlock(&fip->ctlr_mutex);
Joe Eykholt97c83892009-03-17 11:42:40 -0700374}
375EXPORT_SYMBOL(fcoe_ctlr_link_up);
376
377/**
Robert Love70b51aa2009-11-03 11:47:45 -0800378 * fcoe_ctlr_reset() - Reset a FCoE controller
379 * @fip: The FCoE controller to reset
Joe Eykholt97c83892009-03-17 11:42:40 -0700380 */
Joe Eykholtdd42dac2009-11-03 11:48:27 -0800381static void fcoe_ctlr_reset(struct fcoe_ctlr *fip)
Joe Eykholt97c83892009-03-17 11:42:40 -0700382{
Joe Eykholt97c83892009-03-17 11:42:40 -0700383 fcoe_ctlr_reset_fcfs(fip);
384 del_timer(&fip->timer);
Joe Eykholt97c83892009-03-17 11:42:40 -0700385 fip->ctlr_ka_time = 0;
386 fip->port_ka_time = 0;
387 fip->sol_time = 0;
388 fip->flogi_oxid = FC_XID_UNKNOWN;
Joe Eykholtcd229e42010-07-20 15:20:40 -0700389 fcoe_ctlr_map_dest(fip);
Joe Eykholt97c83892009-03-17 11:42:40 -0700390}
391
392/**
Robert Love70b51aa2009-11-03 11:47:45 -0800393 * fcoe_ctlr_link_down() - Stop a FCoE controller
394 * @fip: The FCoE controller to be stopped
Joe Eykholt97c83892009-03-17 11:42:40 -0700395 *
396 * Returns non-zero if the link was up and now isn't.
397 *
398 * Called from the LLD when the network link is not ready.
399 * There may be multiple calls while the link is down.
400 */
401int fcoe_ctlr_link_down(struct fcoe_ctlr *fip)
402{
Joe Eykholtdd42dac2009-11-03 11:48:27 -0800403 int link_dropped;
404
405 LIBFCOE_FIP_DBG(fip, "link down.\n");
Joe Eykholtfdb068c2010-07-20 15:19:47 -0700406 mutex_lock(&fip->ctlr_mutex);
Joe Eykholtdd42dac2009-11-03 11:48:27 -0800407 fcoe_ctlr_reset(fip);
Joe Eykholt42913652010-03-12 16:08:23 -0800408 link_dropped = fip->state != FIP_ST_LINK_WAIT;
Joe Eykholt9b651da2010-07-20 15:20:24 -0700409 fcoe_ctlr_set_state(fip, FIP_ST_LINK_WAIT);
Joe Eykholtfdb068c2010-07-20 15:19:47 -0700410 mutex_unlock(&fip->ctlr_mutex);
Joe Eykholtdd42dac2009-11-03 11:48:27 -0800411
412 if (link_dropped)
413 fc_linkdown(fip->lp);
414 return link_dropped;
Joe Eykholt97c83892009-03-17 11:42:40 -0700415}
416EXPORT_SYMBOL(fcoe_ctlr_link_down);
417
418/**
Robert Love70b51aa2009-11-03 11:47:45 -0800419 * fcoe_ctlr_send_keep_alive() - Send a keep-alive to the selected FCF
420 * @fip: The FCoE controller to send the FKA on
421 * @lport: libfc fc_lport to send from
422 * @ports: 0 for controller keep-alive, 1 for port keep-alive
423 * @sa: The source MAC address
Joe Eykholt97c83892009-03-17 11:42:40 -0700424 *
425 * A controller keep-alive is sent every fka_period (typically 8 seconds).
426 * The source MAC is the native MAC address.
427 *
428 * A port keep-alive is sent every 90 seconds while logged in.
429 * The source MAC is the assigned mapped source address.
430 * The destination is the FCF's F-port.
431 */
Chris Leech11b56182009-11-03 11:46:29 -0800432static void fcoe_ctlr_send_keep_alive(struct fcoe_ctlr *fip,
433 struct fc_lport *lport,
434 int ports, u8 *sa)
Joe Eykholt97c83892009-03-17 11:42:40 -0700435{
436 struct sk_buff *skb;
437 struct fip_kal {
438 struct ethhdr eth;
439 struct fip_header fip;
440 struct fip_mac_desc mac;
441 } __attribute__((packed)) *kal;
442 struct fip_vn_desc *vn;
443 u32 len;
444 struct fc_lport *lp;
445 struct fcoe_fcf *fcf;
446
447 fcf = fip->sel_fcf;
448 lp = fip->lp;
Joe Eykholt281ae642010-06-11 16:43:33 -0700449 if (!fcf || (ports && !lp->port_id))
Joe Eykholt97c83892009-03-17 11:42:40 -0700450 return;
451
Yi Zoube276cb2009-11-03 11:50:16 -0800452 len = sizeof(*kal) + ports * sizeof(*vn);
Joe Eykholt97c83892009-03-17 11:42:40 -0700453 skb = dev_alloc_skb(len);
454 if (!skb)
455 return;
456
457 kal = (struct fip_kal *)skb->data;
458 memset(kal, 0, len);
459 memcpy(kal->eth.h_dest, fcf->fcf_mac, ETH_ALEN);
460 memcpy(kal->eth.h_source, sa, ETH_ALEN);
461 kal->eth.h_proto = htons(ETH_P_FIP);
462
463 kal->fip.fip_ver = FIP_VER_ENCAPS(FIP_VER);
464 kal->fip.fip_op = htons(FIP_OP_CTRL);
465 kal->fip.fip_subcode = FIP_SC_KEEP_ALIVE;
466 kal->fip.fip_dl_len = htons((sizeof(kal->mac) +
Robert Love70b51aa2009-11-03 11:47:45 -0800467 ports * sizeof(*vn)) / FIP_BPW);
Joe Eykholt97c83892009-03-17 11:42:40 -0700468 kal->fip.fip_flags = htons(FIP_FL_FPMA);
Vasu Dev184dd342009-05-17 12:33:28 +0000469 if (fip->spma)
470 kal->fip.fip_flags |= htons(FIP_FL_SPMA);
Joe Eykholt97c83892009-03-17 11:42:40 -0700471
472 kal->mac.fd_desc.fip_dtype = FIP_DT_MAC;
473 kal->mac.fd_desc.fip_dlen = sizeof(kal->mac) / FIP_BPW;
474 memcpy(kal->mac.fd_mac, fip->ctl_src_addr, ETH_ALEN);
Joe Eykholt97c83892009-03-17 11:42:40 -0700475 if (ports) {
476 vn = (struct fip_vn_desc *)(kal + 1);
477 vn->fd_desc.fip_dtype = FIP_DT_VN_ID;
478 vn->fd_desc.fip_dlen = sizeof(*vn) / FIP_BPW;
Chris Leech11b56182009-11-03 11:46:29 -0800479 memcpy(vn->fd_mac, fip->get_src_addr(lport), ETH_ALEN);
Kaladhar Musunurufb831532010-05-07 15:18:57 -0700480 hton24(vn->fd_fc_id, lport->port_id);
481 put_unaligned_be64(lport->wwpn, &vn->fd_wwpn);
Joe Eykholt97c83892009-03-17 11:42:40 -0700482 }
Joe Eykholt97c83892009-03-17 11:42:40 -0700483 skb_put(skb, len);
Chris Leech0f491532009-05-06 10:52:18 -0700484 skb->protocol = htons(ETH_P_FIP);
Joe Eykholt97c83892009-03-17 11:42:40 -0700485 skb_reset_mac_header(skb);
486 skb_reset_network_header(skb);
487 fip->send(fip, skb);
488}
489
490/**
Robert Love70b51aa2009-11-03 11:47:45 -0800491 * fcoe_ctlr_encaps() - Encapsulate an ELS frame for FIP, without sending it
492 * @fip: The FCoE controller for the ELS frame
493 * @dtype: The FIP descriptor type for the frame
494 * @skb: The FCoE ELS frame including FC header but no FCoE headers
Joe Eykholte10f8c62010-07-20 15:20:30 -0700495 * @d_id: The destination port ID.
Joe Eykholt97c83892009-03-17 11:42:40 -0700496 *
497 * Returns non-zero error code on failure.
498 *
499 * The caller must check that the length is a multiple of 4.
500 *
501 * The @skb must have enough headroom (28 bytes) and tailroom (8 bytes).
502 * Headroom includes the FIP encapsulation description, FIP header, and
503 * Ethernet header. The tailroom is for the FIP MAC descriptor.
504 */
Chris Leech11b56182009-11-03 11:46:29 -0800505static int fcoe_ctlr_encaps(struct fcoe_ctlr *fip, struct fc_lport *lport,
Joe Eykholte10f8c62010-07-20 15:20:30 -0700506 u8 dtype, struct sk_buff *skb, u32 d_id)
Joe Eykholt97c83892009-03-17 11:42:40 -0700507{
508 struct fip_encaps_head {
509 struct ethhdr eth;
510 struct fip_header fip;
511 struct fip_encaps encaps;
512 } __attribute__((packed)) *cap;
Joe Eykholt55543452010-07-20 15:20:35 -0700513 struct fc_frame_header *fh;
Joe Eykholt97c83892009-03-17 11:42:40 -0700514 struct fip_mac_desc *mac;
515 struct fcoe_fcf *fcf;
516 size_t dlen;
Yi Zou5a84bae2009-07-29 17:03:55 -0700517 u16 fip_flags;
Joe Eykholt55543452010-07-20 15:20:35 -0700518 u8 op;
Joe Eykholt97c83892009-03-17 11:42:40 -0700519
Joe Eykholt55543452010-07-20 15:20:35 -0700520 fh = (struct fc_frame_header *)skb->data;
521 op = *(u8 *)(fh + 1);
Joe Eykholt97c83892009-03-17 11:42:40 -0700522 dlen = sizeof(struct fip_encaps) + skb->len; /* len before push */
523 cap = (struct fip_encaps_head *)skb_push(skb, sizeof(*cap));
Joe Eykholt97c83892009-03-17 11:42:40 -0700524 memset(cap, 0, sizeof(*cap));
Joe Eykholte10f8c62010-07-20 15:20:30 -0700525
526 if (lport->point_to_multipoint) {
527 if (fcoe_ctlr_vn_lookup(fip, d_id, cap->eth.h_dest))
528 return -ENODEV;
Joe Eykholt55543452010-07-20 15:20:35 -0700529 fip_flags = 0;
Joe Eykholte10f8c62010-07-20 15:20:30 -0700530 } else {
531 fcf = fip->sel_fcf;
532 if (!fcf)
533 return -ENODEV;
534 fip_flags = fcf->flags;
535 fip_flags &= fip->spma ? FIP_FL_SPMA | FIP_FL_FPMA :
536 FIP_FL_FPMA;
537 if (!fip_flags)
538 return -ENODEV;
539 memcpy(cap->eth.h_dest, fcf->fcf_mac, ETH_ALEN);
540 }
Joe Eykholt97c83892009-03-17 11:42:40 -0700541 memcpy(cap->eth.h_source, fip->ctl_src_addr, ETH_ALEN);
542 cap->eth.h_proto = htons(ETH_P_FIP);
543
544 cap->fip.fip_ver = FIP_VER_ENCAPS(FIP_VER);
545 cap->fip.fip_op = htons(FIP_OP_LS);
Joe Eykholt55543452010-07-20 15:20:35 -0700546 if (op == ELS_LS_ACC || op == ELS_LS_RJT)
547 cap->fip.fip_subcode = FIP_SC_REP;
548 else
549 cap->fip.fip_subcode = FIP_SC_REQ;
Yi Zou5a84bae2009-07-29 17:03:55 -0700550 cap->fip.fip_flags = htons(fip_flags);
Joe Eykholt97c83892009-03-17 11:42:40 -0700551
552 cap->encaps.fd_desc.fip_dtype = dtype;
553 cap->encaps.fd_desc.fip_dlen = dlen / FIP_BPW;
554
Joe Eykholt55543452010-07-20 15:20:35 -0700555 if (op != ELS_LS_RJT) {
556 dlen += sizeof(*mac);
557 mac = (struct fip_mac_desc *)skb_put(skb, sizeof(*mac));
558 memset(mac, 0, sizeof(*mac));
559 mac->fd_desc.fip_dtype = FIP_DT_MAC;
560 mac->fd_desc.fip_dlen = sizeof(*mac) / FIP_BPW;
561 if (dtype != FIP_DT_FLOGI && dtype != FIP_DT_FDISC) {
562 memcpy(mac->fd_mac, fip->get_src_addr(lport), ETH_ALEN);
563 } else if (fip->mode == FIP_MODE_VN2VN) {
564 hton24(mac->fd_mac, FIP_VN_FC_MAP);
565 hton24(mac->fd_mac + 3, fip->port_id);
566 } else if (fip_flags & FIP_FL_SPMA) {
567 LIBFCOE_FIP_DBG(fip, "FLOGI/FDISC sent with SPMA\n");
568 memcpy(mac->fd_mac, fip->ctl_src_addr, ETH_ALEN);
569 } else {
570 LIBFCOE_FIP_DBG(fip, "FLOGI/FDISC sent with FPMA\n");
571 /* FPMA only FLOGI. Must leave the MAC desc zeroed. */
572 }
Robert Love593abc02010-04-09 14:22:17 -0700573 }
Joe Eykholt55543452010-07-20 15:20:35 -0700574 cap->fip.fip_dl_len = htons(dlen / FIP_BPW);
Joe Eykholt97c83892009-03-17 11:42:40 -0700575
Chris Leech0f491532009-05-06 10:52:18 -0700576 skb->protocol = htons(ETH_P_FIP);
Joe Eykholt97c83892009-03-17 11:42:40 -0700577 skb_reset_mac_header(skb);
578 skb_reset_network_header(skb);
579 return 0;
580}
581
582/**
583 * fcoe_ctlr_els_send() - Send an ELS frame encapsulated by FIP if appropriate.
584 * @fip: FCoE controller.
Chris Leech11b56182009-11-03 11:46:29 -0800585 * @lport: libfc fc_lport to send from
Joe Eykholt97c83892009-03-17 11:42:40 -0700586 * @skb: FCoE ELS frame including FC header but no FCoE headers.
587 *
588 * Returns a non-zero error code if the frame should not be sent.
589 * Returns zero if the caller should send the frame with FCoE encapsulation.
590 *
591 * The caller must check that the length is a multiple of 4.
592 * The SKB must have enough headroom (28 bytes) and tailroom (8 bytes).
Joe Eykholte10f8c62010-07-20 15:20:30 -0700593 * The the skb must also be an fc_frame.
Joe Eykholt97c83892009-03-17 11:42:40 -0700594 */
Chris Leech11b56182009-11-03 11:46:29 -0800595int fcoe_ctlr_els_send(struct fcoe_ctlr *fip, struct fc_lport *lport,
596 struct sk_buff *skb)
Joe Eykholt97c83892009-03-17 11:42:40 -0700597{
Joe Eykholte10f8c62010-07-20 15:20:30 -0700598 struct fc_frame *fp;
Joe Eykholt97c83892009-03-17 11:42:40 -0700599 struct fc_frame_header *fh;
600 u16 old_xid;
601 u8 op;
Chris Leech11b56182009-11-03 11:46:29 -0800602 u8 mac[ETH_ALEN];
Joe Eykholt97c83892009-03-17 11:42:40 -0700603
Joe Eykholte10f8c62010-07-20 15:20:30 -0700604 fp = container_of(skb, struct fc_frame, skb);
Joe Eykholt97c83892009-03-17 11:42:40 -0700605 fh = (struct fc_frame_header *)skb->data;
606 op = *(u8 *)(fh + 1);
607
Joe Eykholte10f8c62010-07-20 15:20:30 -0700608 if (op == ELS_FLOGI && fip->mode != FIP_MODE_VN2VN) {
Joe Eykholt97c83892009-03-17 11:42:40 -0700609 old_xid = fip->flogi_oxid;
610 fip->flogi_oxid = ntohs(fh->fh_ox_id);
611 if (fip->state == FIP_ST_AUTO) {
612 if (old_xid == FC_XID_UNKNOWN)
613 fip->flogi_count = 0;
614 fip->flogi_count++;
615 if (fip->flogi_count < 3)
616 goto drop;
Joe Eykholtcd229e42010-07-20 15:20:40 -0700617 fcoe_ctlr_map_dest(fip);
Joe Eykholt97c83892009-03-17 11:42:40 -0700618 return 0;
619 }
Joe Eykholt5f48f702009-05-06 10:52:12 -0700620 if (fip->state == FIP_ST_NON_FIP)
Joe Eykholtcd229e42010-07-20 15:20:40 -0700621 fcoe_ctlr_map_dest(fip);
Joe Eykholt5f48f702009-05-06 10:52:12 -0700622 }
623
624 if (fip->state == FIP_ST_NON_FIP)
625 return 0;
Joe Eykholte10f8c62010-07-20 15:20:30 -0700626 if (!fip->sel_fcf && fip->mode != FIP_MODE_VN2VN)
Joe Eykholtf31f2a12009-11-03 11:48:32 -0800627 goto drop;
Joe Eykholt5f48f702009-05-06 10:52:12 -0700628 switch (op) {
629 case ELS_FLOGI:
Joe Eykholt97c83892009-03-17 11:42:40 -0700630 op = FIP_DT_FLOGI;
631 break;
632 case ELS_FDISC:
633 if (ntoh24(fh->fh_s_id))
634 return 0;
635 op = FIP_DT_FDISC;
636 break;
637 case ELS_LOGO:
Joe Eykholte10f8c62010-07-20 15:20:30 -0700638 if (fip->mode == FIP_MODE_VN2VN) {
639 if (fip->state != FIP_ST_VNMP_UP)
640 return -EINVAL;
641 if (ntoh24(fh->fh_d_id) == FC_FID_FLOGI)
642 return -EINVAL;
643 } else {
644 if (fip->state != FIP_ST_ENABLED)
645 return 0;
646 if (ntoh24(fh->fh_d_id) != FC_FID_FLOGI)
647 return 0;
648 }
Joe Eykholt97c83892009-03-17 11:42:40 -0700649 op = FIP_DT_LOGO;
650 break;
651 case ELS_LS_ACC:
Joe Eykholt97c83892009-03-17 11:42:40 -0700652 /*
Joe Eykholte10f8c62010-07-20 15:20:30 -0700653 * If non-FIP, we may have gotten an SID by accepting an FLOGI
Joe Eykholt97c83892009-03-17 11:42:40 -0700654 * from a point-to-point connection. Switch to using
655 * the source mac based on the SID. The destination
656 * MAC in this case would have been set by receving the
657 * FLOGI.
658 */
Joe Eykholte10f8c62010-07-20 15:20:30 -0700659 if (fip->state == FIP_ST_NON_FIP) {
660 if (fip->flogi_oxid == FC_XID_UNKNOWN)
661 return 0;
662 fip->flogi_oxid = FC_XID_UNKNOWN;
663 fc_fcoe_set_mac(mac, fh->fh_d_id);
664 fip->update_mac(lport, mac);
665 }
666 /* fall through */
667 case ELS_LS_RJT:
668 op = fr_encaps(fp);
669 if (op)
670 break;
Joe Eykholt97c83892009-03-17 11:42:40 -0700671 return 0;
672 default:
Joe Eykholte10f8c62010-07-20 15:20:30 -0700673 if (fip->state != FIP_ST_ENABLED &&
674 fip->state != FIP_ST_VNMP_UP)
Joe Eykholt97c83892009-03-17 11:42:40 -0700675 goto drop;
676 return 0;
677 }
Joe Eykholte10f8c62010-07-20 15:20:30 -0700678 LIBFCOE_FIP_DBG(fip, "els_send op %u d_id %x\n",
679 op, ntoh24(fh->fh_d_id));
680 if (fcoe_ctlr_encaps(fip, lport, op, skb, ntoh24(fh->fh_d_id)))
Joe Eykholt97c83892009-03-17 11:42:40 -0700681 goto drop;
682 fip->send(fip, skb);
683 return -EINPROGRESS;
684drop:
685 kfree_skb(skb);
686 return -EINVAL;
687}
688EXPORT_SYMBOL(fcoe_ctlr_els_send);
689
Robert Love70b51aa2009-11-03 11:47:45 -0800690/**
691 * fcoe_ctlr_age_fcfs() - Reset and free all old FCFs for a controller
692 * @fip: The FCoE controller to free FCFs on
Joe Eykholt97c83892009-03-17 11:42:40 -0700693 *
Joe Eykholtf018b732010-03-12 16:08:55 -0800694 * Called with lock held and preemption disabled.
Joe Eykholt97c83892009-03-17 11:42:40 -0700695 *
Joe Eykholt8690cb82010-06-11 16:44:10 -0700696 * An FCF is considered old if we have missed two advertisements.
697 * That is, there have been no valid advertisement from it for 2.5
698 * times its keep-alive period.
Joe Eykholt97c83892009-03-17 11:42:40 -0700699 *
700 * In addition, determine the time when an FCF selection can occur.
Yi Zouf3da80e2009-11-20 14:55:08 -0800701 *
702 * Also, increment the MissDiscAdvCount when no advertisement is received
703 * for the corresponding FCF for 1.5 * FKA_ADV_PERIOD (FC-BB-5 LESB).
Joe Eykholt8690cb82010-06-11 16:44:10 -0700704 *
705 * Returns the time in jiffies for the next call.
Joe Eykholt97c83892009-03-17 11:42:40 -0700706 */
Joe Eykholt8690cb82010-06-11 16:44:10 -0700707static unsigned long fcoe_ctlr_age_fcfs(struct fcoe_ctlr *fip)
Joe Eykholt97c83892009-03-17 11:42:40 -0700708{
709 struct fcoe_fcf *fcf;
710 struct fcoe_fcf *next;
Joe Eykholt8690cb82010-06-11 16:44:10 -0700711 unsigned long next_timer = jiffies + msecs_to_jiffies(FIP_VN_KA_PERIOD);
712 unsigned long deadline;
Joe Eykholt97c83892009-03-17 11:42:40 -0700713 unsigned long sel_time = 0;
Joe Eykholtf018b732010-03-12 16:08:55 -0800714 struct fcoe_dev_stats *stats;
Joe Eykholt97c83892009-03-17 11:42:40 -0700715
Joe Eykholtfdb068c2010-07-20 15:19:47 -0700716 stats = per_cpu_ptr(fip->lp->dev_stats, get_cpu());
717
Joe Eykholt97c83892009-03-17 11:42:40 -0700718 list_for_each_entry_safe(fcf, next, &fip->fcfs, list) {
Joe Eykholt8690cb82010-06-11 16:44:10 -0700719 deadline = fcf->time + fcf->fka_period + fcf->fka_period / 2;
720 if (fip->sel_fcf == fcf) {
721 if (time_after(jiffies, deadline)) {
Joe Eykholt8690cb82010-06-11 16:44:10 -0700722 stats->MissDiscAdvCount++;
723 printk(KERN_INFO "libfcoe: host%d: "
724 "Missing Discovery Advertisement "
725 "for fab %16.16llx count %lld\n",
726 fip->lp->host->host_no, fcf->fabric_name,
727 stats->MissDiscAdvCount);
728 } else if (time_after(next_timer, deadline))
729 next_timer = deadline;
Yi Zouf3da80e2009-11-20 14:55:08 -0800730 }
Joe Eykholt8690cb82010-06-11 16:44:10 -0700731
732 deadline += fcf->fka_period;
Joe Eykholtd99ee452010-06-11 16:44:15 -0700733 if (time_after_eq(jiffies, deadline)) {
Joe Eykholt97c83892009-03-17 11:42:40 -0700734 if (fip->sel_fcf == fcf)
735 fip->sel_fcf = NULL;
736 list_del(&fcf->list);
737 WARN_ON(!fip->fcf_count);
738 fip->fcf_count--;
739 kfree(fcf);
Joe Eykholtf018b732010-03-12 16:08:55 -0800740 stats->VLinkFailureCount++;
Joe Eykholt8690cb82010-06-11 16:44:10 -0700741 } else {
742 if (time_after(next_timer, deadline))
743 next_timer = deadline;
744 if (fcoe_ctlr_mtu_valid(fcf) &&
745 (!sel_time || time_before(sel_time, fcf->time)))
746 sel_time = fcf->time;
Joe Eykholt97c83892009-03-17 11:42:40 -0700747 }
748 }
Joe Eykholtfdb068c2010-07-20 15:19:47 -0700749 put_cpu();
Joe Eykholtd99ee452010-06-11 16:44:15 -0700750 if (sel_time && !fip->sel_fcf && !fip->sel_time) {
Joe Eykholt97c83892009-03-17 11:42:40 -0700751 sel_time += msecs_to_jiffies(FCOE_CTLR_START_DELAY);
752 fip->sel_time = sel_time;
Joe Eykholt97c83892009-03-17 11:42:40 -0700753 }
Joe Eykholtd99ee452010-06-11 16:44:15 -0700754
Joe Eykholt8690cb82010-06-11 16:44:10 -0700755 return next_timer;
Joe Eykholt97c83892009-03-17 11:42:40 -0700756}
757
758/**
Robert Love70b51aa2009-11-03 11:47:45 -0800759 * fcoe_ctlr_parse_adv() - Decode a FIP advertisement into a new FCF entry
Joe Eykholt0f51c2e2009-11-03 11:48:16 -0800760 * @fip: The FCoE controller receiving the advertisement
Robert Love70b51aa2009-11-03 11:47:45 -0800761 * @skb: The received FIP advertisement frame
762 * @fcf: The resulting FCF entry
Joe Eykholt97c83892009-03-17 11:42:40 -0700763 *
764 * Returns zero on a valid parsed advertisement,
765 * otherwise returns non zero value.
766 */
Joe Eykholt0f51c2e2009-11-03 11:48:16 -0800767static int fcoe_ctlr_parse_adv(struct fcoe_ctlr *fip,
768 struct sk_buff *skb, struct fcoe_fcf *fcf)
Joe Eykholt97c83892009-03-17 11:42:40 -0700769{
770 struct fip_header *fiph;
771 struct fip_desc *desc = NULL;
772 struct fip_wwn_desc *wwn;
773 struct fip_fab_desc *fab;
774 struct fip_fka_desc *fka;
775 unsigned long t;
776 size_t rlen;
777 size_t dlen;
Bhanu Prakash Gollapudi0a9c5d32010-06-11 16:44:25 -0700778 u32 desc_mask;
Joe Eykholt97c83892009-03-17 11:42:40 -0700779
780 memset(fcf, 0, sizeof(*fcf));
781 fcf->fka_period = msecs_to_jiffies(FCOE_CTLR_DEF_FKA);
782
783 fiph = (struct fip_header *)skb->data;
784 fcf->flags = ntohs(fiph->fip_flags);
785
Bhanu Prakash Gollapudi0a9c5d32010-06-11 16:44:25 -0700786 /*
787 * mask of required descriptors. validating each one clears its bit.
788 */
789 desc_mask = BIT(FIP_DT_PRI) | BIT(FIP_DT_MAC) | BIT(FIP_DT_NAME) |
790 BIT(FIP_DT_FAB) | BIT(FIP_DT_FKA);
791
Joe Eykholt97c83892009-03-17 11:42:40 -0700792 rlen = ntohs(fiph->fip_dl_len) * 4;
793 if (rlen + sizeof(*fiph) > skb->len)
794 return -EINVAL;
795
796 desc = (struct fip_desc *)(fiph + 1);
797 while (rlen > 0) {
798 dlen = desc->fip_dlen * FIP_BPW;
799 if (dlen < sizeof(*desc) || dlen > rlen)
800 return -EINVAL;
Bhanu Prakash Gollapudi0a9c5d32010-06-11 16:44:25 -0700801 /* Drop Adv if there are duplicate critical descriptors */
802 if ((desc->fip_dtype < 32) &&
803 !(desc_mask & 1U << desc->fip_dtype)) {
804 LIBFCOE_FIP_DBG(fip, "Duplicate Critical "
805 "Descriptors in FIP adv\n");
806 return -EINVAL;
807 }
Joe Eykholt97c83892009-03-17 11:42:40 -0700808 switch (desc->fip_dtype) {
809 case FIP_DT_PRI:
810 if (dlen != sizeof(struct fip_pri_desc))
811 goto len_err;
812 fcf->pri = ((struct fip_pri_desc *)desc)->fd_pri;
Bhanu Prakash Gollapudi0a9c5d32010-06-11 16:44:25 -0700813 desc_mask &= ~BIT(FIP_DT_PRI);
Joe Eykholt97c83892009-03-17 11:42:40 -0700814 break;
815 case FIP_DT_MAC:
816 if (dlen != sizeof(struct fip_mac_desc))
817 goto len_err;
818 memcpy(fcf->fcf_mac,
819 ((struct fip_mac_desc *)desc)->fd_mac,
820 ETH_ALEN);
821 if (!is_valid_ether_addr(fcf->fcf_mac)) {
Joe Eykholte10f8c62010-07-20 15:20:30 -0700822 LIBFCOE_FIP_DBG(fip,
823 "Invalid MAC addr %pM in FIP adv\n",
824 fcf->fcf_mac);
Joe Eykholt97c83892009-03-17 11:42:40 -0700825 return -EINVAL;
826 }
Bhanu Prakash Gollapudi0a9c5d32010-06-11 16:44:25 -0700827 desc_mask &= ~BIT(FIP_DT_MAC);
Joe Eykholt97c83892009-03-17 11:42:40 -0700828 break;
829 case FIP_DT_NAME:
830 if (dlen != sizeof(struct fip_wwn_desc))
831 goto len_err;
832 wwn = (struct fip_wwn_desc *)desc;
833 fcf->switch_name = get_unaligned_be64(&wwn->fd_wwn);
Bhanu Prakash Gollapudi0a9c5d32010-06-11 16:44:25 -0700834 desc_mask &= ~BIT(FIP_DT_NAME);
Joe Eykholt97c83892009-03-17 11:42:40 -0700835 break;
836 case FIP_DT_FAB:
837 if (dlen != sizeof(struct fip_fab_desc))
838 goto len_err;
839 fab = (struct fip_fab_desc *)desc;
840 fcf->fabric_name = get_unaligned_be64(&fab->fd_wwn);
841 fcf->vfid = ntohs(fab->fd_vfid);
842 fcf->fc_map = ntoh24(fab->fd_map);
Bhanu Prakash Gollapudi0a9c5d32010-06-11 16:44:25 -0700843 desc_mask &= ~BIT(FIP_DT_FAB);
Joe Eykholt97c83892009-03-17 11:42:40 -0700844 break;
845 case FIP_DT_FKA:
846 if (dlen != sizeof(struct fip_fka_desc))
847 goto len_err;
848 fka = (struct fip_fka_desc *)desc;
Yi Zou8cdffdc2009-11-20 14:54:57 -0800849 if (fka->fd_flags & FIP_FKA_ADV_D)
850 fcf->fd_flags = 1;
Joe Eykholt97c83892009-03-17 11:42:40 -0700851 t = ntohl(fka->fd_fka_period);
852 if (t >= FCOE_CTLR_MIN_FKA)
853 fcf->fka_period = msecs_to_jiffies(t);
Bhanu Prakash Gollapudi0a9c5d32010-06-11 16:44:25 -0700854 desc_mask &= ~BIT(FIP_DT_FKA);
Joe Eykholt97c83892009-03-17 11:42:40 -0700855 break;
856 case FIP_DT_MAP_OUI:
857 case FIP_DT_FCOE_SIZE:
858 case FIP_DT_FLOGI:
859 case FIP_DT_FDISC:
860 case FIP_DT_LOGO:
861 case FIP_DT_ELP:
862 default:
Joe Eykholt0f51c2e2009-11-03 11:48:16 -0800863 LIBFCOE_FIP_DBG(fip, "unexpected descriptor type %x "
Robert Love650bd122009-06-10 15:31:05 -0700864 "in FIP adv\n", desc->fip_dtype);
Joe Eykholt97c83892009-03-17 11:42:40 -0700865 /* standard says ignore unknown descriptors >= 128 */
866 if (desc->fip_dtype < FIP_DT_VENDOR_BASE)
867 return -EINVAL;
Bhanu Prakash Gollapudi1508f3ec2010-06-11 16:43:38 -0700868 break;
Joe Eykholt97c83892009-03-17 11:42:40 -0700869 }
870 desc = (struct fip_desc *)((char *)desc + dlen);
871 rlen -= dlen;
872 }
873 if (!fcf->fc_map || (fcf->fc_map & 0x10000))
874 return -EINVAL;
Vasu Dev240778e2010-07-20 15:21:33 -0700875 if (!fcf->switch_name)
Joe Eykholt97c83892009-03-17 11:42:40 -0700876 return -EINVAL;
Bhanu Prakash Gollapudi0a9c5d32010-06-11 16:44:25 -0700877 if (desc_mask) {
878 LIBFCOE_FIP_DBG(fip, "adv missing descriptors mask %x\n",
879 desc_mask);
880 return -EINVAL;
881 }
Joe Eykholt97c83892009-03-17 11:42:40 -0700882 return 0;
883
884len_err:
Joe Eykholt0f51c2e2009-11-03 11:48:16 -0800885 LIBFCOE_FIP_DBG(fip, "FIP length error in descriptor type %x len %zu\n",
Robert Love650bd122009-06-10 15:31:05 -0700886 desc->fip_dtype, dlen);
Joe Eykholt97c83892009-03-17 11:42:40 -0700887 return -EINVAL;
888}
889
890/**
Robert Love70b51aa2009-11-03 11:47:45 -0800891 * fcoe_ctlr_recv_adv() - Handle an incoming advertisement
892 * @fip: The FCoE controller receiving the advertisement
893 * @skb: The received FIP packet
Joe Eykholt97c83892009-03-17 11:42:40 -0700894 */
895static void fcoe_ctlr_recv_adv(struct fcoe_ctlr *fip, struct sk_buff *skb)
896{
897 struct fcoe_fcf *fcf;
898 struct fcoe_fcf new;
899 struct fcoe_fcf *found;
900 unsigned long sol_tov = msecs_to_jiffies(FCOE_CTRL_SOL_TOV);
901 int first = 0;
902 int mtu_valid;
903
Joe Eykholt0f51c2e2009-11-03 11:48:16 -0800904 if (fcoe_ctlr_parse_adv(fip, skb, &new))
Joe Eykholt97c83892009-03-17 11:42:40 -0700905 return;
906
Joe Eykholtfdb068c2010-07-20 15:19:47 -0700907 mutex_lock(&fip->ctlr_mutex);
Joe Eykholt97c83892009-03-17 11:42:40 -0700908 first = list_empty(&fip->fcfs);
909 found = NULL;
910 list_for_each_entry(fcf, &fip->fcfs, list) {
911 if (fcf->switch_name == new.switch_name &&
912 fcf->fabric_name == new.fabric_name &&
913 fcf->fc_map == new.fc_map &&
914 compare_ether_addr(fcf->fcf_mac, new.fcf_mac) == 0) {
915 found = fcf;
916 break;
917 }
918 }
919 if (!found) {
920 if (fip->fcf_count >= FCOE_CTLR_FCF_LIMIT)
921 goto out;
922
923 fcf = kmalloc(sizeof(*fcf), GFP_ATOMIC);
924 if (!fcf)
925 goto out;
926
927 fip->fcf_count++;
928 memcpy(fcf, &new, sizeof(new));
929 list_add(&fcf->list, &fip->fcfs);
930 } else {
931 /*
Joe Eykholtc600fea2010-06-11 16:44:20 -0700932 * Update the FCF's keep-alive descriptor flags.
933 * Other flag changes from new advertisements are
934 * ignored after a solicited advertisement is
935 * received and the FCF is selectable (usable).
Joe Eykholt97c83892009-03-17 11:42:40 -0700936 */
Joe Eykholtc600fea2010-06-11 16:44:20 -0700937 fcf->fd_flags = new.fd_flags;
938 if (!fcoe_ctlr_fcf_usable(fcf))
939 fcf->flags = new.flags;
940
Joe Eykholtd99ee452010-06-11 16:44:15 -0700941 if (fcf == fip->sel_fcf && !fcf->fd_flags) {
Joe Eykholt97c83892009-03-17 11:42:40 -0700942 fip->ctlr_ka_time -= fcf->fka_period;
943 fip->ctlr_ka_time += new.fka_period;
944 if (time_before(fip->ctlr_ka_time, fip->timer.expires))
945 mod_timer(&fip->timer, fip->ctlr_ka_time);
Joe Eykholtc600fea2010-06-11 16:44:20 -0700946 }
Joe Eykholt97c83892009-03-17 11:42:40 -0700947 fcf->fka_period = new.fka_period;
948 memcpy(fcf->fcf_mac, new.fcf_mac, ETH_ALEN);
949 }
950 mtu_valid = fcoe_ctlr_mtu_valid(fcf);
951 fcf->time = jiffies;
Robert Love650bd122009-06-10 15:31:05 -0700952 if (!found) {
Chris Leech9f8f3aa2010-04-09 14:23:16 -0700953 LIBFCOE_FIP_DBG(fip, "New FCF for fab %16.16llx "
954 "map %x val %d\n",
Robert Love650bd122009-06-10 15:31:05 -0700955 fcf->fabric_name, fcf->fc_map, mtu_valid);
956 }
Joe Eykholt97c83892009-03-17 11:42:40 -0700957
958 /*
959 * If this advertisement is not solicited and our max receive size
960 * hasn't been verified, send a solicited advertisement.
961 */
962 if (!mtu_valid)
963 fcoe_ctlr_solicit(fip, fcf);
964
965 /*
966 * If its been a while since we did a solicit, and this is
967 * the first advertisement we've received, do a multicast
968 * solicitation to gather as many advertisements as we can
969 * before selection occurs.
970 */
971 if (first && time_after(jiffies, fip->sol_time + sol_tov))
972 fcoe_ctlr_solicit(fip, NULL);
973
974 /*
975 * If this is the first validated FCF, note the time and
976 * set a timer to trigger selection.
977 */
Joe Eykholtd99ee452010-06-11 16:44:15 -0700978 if (mtu_valid && !fip->sel_fcf && fcoe_ctlr_fcf_usable(fcf)) {
Joe Eykholt97c83892009-03-17 11:42:40 -0700979 fip->sel_time = jiffies +
Robert Love70b51aa2009-11-03 11:47:45 -0800980 msecs_to_jiffies(FCOE_CTLR_START_DELAY);
Joe Eykholt97c83892009-03-17 11:42:40 -0700981 if (!timer_pending(&fip->timer) ||
982 time_before(fip->sel_time, fip->timer.expires))
983 mod_timer(&fip->timer, fip->sel_time);
984 }
985out:
Joe Eykholtfdb068c2010-07-20 15:19:47 -0700986 mutex_unlock(&fip->ctlr_mutex);
Joe Eykholt97c83892009-03-17 11:42:40 -0700987}
988
989/**
Robert Love70b51aa2009-11-03 11:47:45 -0800990 * fcoe_ctlr_recv_els() - Handle an incoming FIP encapsulated ELS frame
991 * @fip: The FCoE controller which received the packet
992 * @skb: The received FIP packet
Joe Eykholt97c83892009-03-17 11:42:40 -0700993 */
994static void fcoe_ctlr_recv_els(struct fcoe_ctlr *fip, struct sk_buff *skb)
995{
Robert Love70b51aa2009-11-03 11:47:45 -0800996 struct fc_lport *lport = fip->lp;
Joe Eykholt97c83892009-03-17 11:42:40 -0700997 struct fip_header *fiph;
Chris Leech11b56182009-11-03 11:46:29 -0800998 struct fc_frame *fp = (struct fc_frame *)skb;
Joe Eykholt97c83892009-03-17 11:42:40 -0700999 struct fc_frame_header *fh = NULL;
1000 struct fip_desc *desc;
1001 struct fip_encaps *els;
1002 struct fcoe_dev_stats *stats;
1003 enum fip_desc_type els_dtype = 0;
1004 u8 els_op;
1005 u8 sub;
1006 u8 granted_mac[ETH_ALEN] = { 0 };
1007 size_t els_len = 0;
1008 size_t rlen;
1009 size_t dlen;
Bhanu Prakash Gollapudibe613312010-06-11 16:44:36 -07001010 u32 desc_mask = 0;
1011 u32 desc_cnt = 0;
Joe Eykholt97c83892009-03-17 11:42:40 -07001012
1013 fiph = (struct fip_header *)skb->data;
1014 sub = fiph->fip_subcode;
1015 if (sub != FIP_SC_REQ && sub != FIP_SC_REP)
1016 goto drop;
1017
1018 rlen = ntohs(fiph->fip_dl_len) * 4;
1019 if (rlen + sizeof(*fiph) > skb->len)
1020 goto drop;
1021
1022 desc = (struct fip_desc *)(fiph + 1);
1023 while (rlen > 0) {
Bhanu Prakash Gollapudibe613312010-06-11 16:44:36 -07001024 desc_cnt++;
Joe Eykholt97c83892009-03-17 11:42:40 -07001025 dlen = desc->fip_dlen * FIP_BPW;
1026 if (dlen < sizeof(*desc) || dlen > rlen)
1027 goto drop;
Bhanu Prakash Gollapudi0a9c5d32010-06-11 16:44:25 -07001028 /* Drop ELS if there are duplicate critical descriptors */
1029 if (desc->fip_dtype < 32) {
Bhanu Prakash Gollapudibe613312010-06-11 16:44:36 -07001030 if (desc_mask & 1U << desc->fip_dtype) {
Bhanu Prakash Gollapudi0a9c5d32010-06-11 16:44:25 -07001031 LIBFCOE_FIP_DBG(fip, "Duplicate Critical "
1032 "Descriptors in FIP ELS\n");
1033 goto drop;
1034 }
Bhanu Prakash Gollapudibe613312010-06-11 16:44:36 -07001035 desc_mask |= (1 << desc->fip_dtype);
Bhanu Prakash Gollapudi0a9c5d32010-06-11 16:44:25 -07001036 }
Joe Eykholt97c83892009-03-17 11:42:40 -07001037 switch (desc->fip_dtype) {
1038 case FIP_DT_MAC:
Bhanu Prakash Gollapudibe613312010-06-11 16:44:36 -07001039 if (desc_cnt == 1) {
1040 LIBFCOE_FIP_DBG(fip, "FIP descriptors "
1041 "received out of order\n");
1042 goto drop;
1043 }
1044
Joe Eykholt97c83892009-03-17 11:42:40 -07001045 if (dlen != sizeof(struct fip_mac_desc))
1046 goto len_err;
1047 memcpy(granted_mac,
1048 ((struct fip_mac_desc *)desc)->fd_mac,
1049 ETH_ALEN);
Joe Eykholt97c83892009-03-17 11:42:40 -07001050 break;
1051 case FIP_DT_FLOGI:
1052 case FIP_DT_FDISC:
1053 case FIP_DT_LOGO:
1054 case FIP_DT_ELP:
Bhanu Prakash Gollapudibe613312010-06-11 16:44:36 -07001055 if (desc_cnt != 1) {
1056 LIBFCOE_FIP_DBG(fip, "FIP descriptors "
1057 "received out of order\n");
1058 goto drop;
1059 }
Joe Eykholt97c83892009-03-17 11:42:40 -07001060 if (fh)
1061 goto drop;
1062 if (dlen < sizeof(*els) + sizeof(*fh) + 1)
1063 goto len_err;
1064 els_len = dlen - sizeof(*els);
1065 els = (struct fip_encaps *)desc;
1066 fh = (struct fc_frame_header *)(els + 1);
1067 els_dtype = desc->fip_dtype;
1068 break;
1069 default:
Joe Eykholt0f51c2e2009-11-03 11:48:16 -08001070 LIBFCOE_FIP_DBG(fip, "unexpected descriptor type %x "
Robert Love650bd122009-06-10 15:31:05 -07001071 "in FIP adv\n", desc->fip_dtype);
Joe Eykholt97c83892009-03-17 11:42:40 -07001072 /* standard says ignore unknown descriptors >= 128 */
1073 if (desc->fip_dtype < FIP_DT_VENDOR_BASE)
1074 goto drop;
Bhanu Prakash Gollapudibe613312010-06-11 16:44:36 -07001075 if (desc_cnt <= 2) {
1076 LIBFCOE_FIP_DBG(fip, "FIP descriptors "
1077 "received out of order\n");
1078 goto drop;
1079 }
Bhanu Prakash Gollapudi1508f3ec2010-06-11 16:43:38 -07001080 break;
Joe Eykholt97c83892009-03-17 11:42:40 -07001081 }
1082 desc = (struct fip_desc *)((char *)desc + dlen);
1083 rlen -= dlen;
1084 }
1085
1086 if (!fh)
1087 goto drop;
1088 els_op = *(u8 *)(fh + 1);
1089
Joe Eykholte10f8c62010-07-20 15:20:30 -07001090 if ((els_dtype == FIP_DT_FLOGI || els_dtype == FIP_DT_FDISC) &&
1091 sub == FIP_SC_REP && els_op == ELS_LS_ACC &&
1092 fip->mode != FIP_MODE_VN2VN) {
1093 if (!is_valid_ether_addr(granted_mac)) {
1094 LIBFCOE_FIP_DBG(fip,
1095 "Invalid MAC address %pM in FIP ELS\n",
1096 granted_mac);
1097 goto drop;
1098 }
1099 memcpy(fr_cb(fp)->granted_mac, granted_mac, ETH_ALEN);
1100
1101 if (fip->flogi_oxid == ntohs(fh->fh_ox_id))
1102 fip->flogi_oxid = FC_XID_UNKNOWN;
1103 }
Joe Eykholt97c83892009-03-17 11:42:40 -07001104
Bhanu Prakash Gollapudibe613312010-06-11 16:44:36 -07001105 if ((desc_cnt == 0) || ((els_op != ELS_LS_RJT) &&
1106 (!(1U << FIP_DT_MAC & desc_mask)))) {
1107 LIBFCOE_FIP_DBG(fip, "Missing critical descriptors "
1108 "in FIP ELS\n");
1109 goto drop;
1110 }
1111
Joe Eykholt97c83892009-03-17 11:42:40 -07001112 /*
1113 * Convert skb into an fc_frame containing only the ELS.
1114 */
1115 skb_pull(skb, (u8 *)fh - skb->data);
1116 skb_trim(skb, els_len);
1117 fp = (struct fc_frame *)skb;
1118 fc_frame_init(fp);
1119 fr_sof(fp) = FC_SOF_I3;
1120 fr_eof(fp) = FC_EOF_T;
Robert Love70b51aa2009-11-03 11:47:45 -08001121 fr_dev(fp) = lport;
Joe Eykholte10f8c62010-07-20 15:20:30 -07001122 fr_encaps(fp) = els_dtype;
Joe Eykholt97c83892009-03-17 11:42:40 -07001123
Joe Eykholtf018b732010-03-12 16:08:55 -08001124 stats = per_cpu_ptr(lport->dev_stats, get_cpu());
Joe Eykholt97c83892009-03-17 11:42:40 -07001125 stats->RxFrames++;
1126 stats->RxWords += skb->len / FIP_BPW;
Joe Eykholtf018b732010-03-12 16:08:55 -08001127 put_cpu();
Joe Eykholt97c83892009-03-17 11:42:40 -07001128
Robert Love70b51aa2009-11-03 11:47:45 -08001129 fc_exch_recv(lport, fp);
Joe Eykholt97c83892009-03-17 11:42:40 -07001130 return;
1131
1132len_err:
Joe Eykholt0f51c2e2009-11-03 11:48:16 -08001133 LIBFCOE_FIP_DBG(fip, "FIP length error in descriptor type %x len %zu\n",
Robert Love650bd122009-06-10 15:31:05 -07001134 desc->fip_dtype, dlen);
Joe Eykholt97c83892009-03-17 11:42:40 -07001135drop:
1136 kfree_skb(skb);
1137}
1138
1139/**
Robert Love70b51aa2009-11-03 11:47:45 -08001140 * fcoe_ctlr_recv_els() - Handle an incoming link reset frame
1141 * @fip: The FCoE controller that received the frame
1142 * @fh: The received FIP header
Joe Eykholt97c83892009-03-17 11:42:40 -07001143 *
1144 * There may be multiple VN_Port descriptors.
1145 * The overall length has already been checked.
1146 */
1147static void fcoe_ctlr_recv_clr_vlink(struct fcoe_ctlr *fip,
Robert Love70b51aa2009-11-03 11:47:45 -08001148 struct fip_header *fh)
Joe Eykholt97c83892009-03-17 11:42:40 -07001149{
1150 struct fip_desc *desc;
1151 struct fip_mac_desc *mp;
1152 struct fip_wwn_desc *wp;
1153 struct fip_vn_desc *vp;
1154 size_t rlen;
1155 size_t dlen;
1156 struct fcoe_fcf *fcf = fip->sel_fcf;
Robert Love70b51aa2009-11-03 11:47:45 -08001157 struct fc_lport *lport = fip->lp;
Bhanu Prakash Gollapudi5550fda2010-06-11 16:44:31 -07001158 struct fc_lport *vn_port = NULL;
1159 u32 desc_mask;
1160 int is_vn_port = 0;
Joe Eykholt97c83892009-03-17 11:42:40 -07001161
Joe Eykholt0f51c2e2009-11-03 11:48:16 -08001162 LIBFCOE_FIP_DBG(fip, "Clear Virtual Link received\n");
Robert Loved29510a2010-05-07 15:18:30 -07001163
Robert Love7b2787e2010-05-07 15:18:41 -07001164 if (!fcf || !lport->port_id)
Joe Eykholt97c83892009-03-17 11:42:40 -07001165 return;
1166
1167 /*
1168 * mask of required descriptors. Validating each one clears its bit.
1169 */
1170 desc_mask = BIT(FIP_DT_MAC) | BIT(FIP_DT_NAME) | BIT(FIP_DT_VN_ID);
1171
1172 rlen = ntohs(fh->fip_dl_len) * FIP_BPW;
1173 desc = (struct fip_desc *)(fh + 1);
1174 while (rlen >= sizeof(*desc)) {
1175 dlen = desc->fip_dlen * FIP_BPW;
1176 if (dlen > rlen)
1177 return;
Bhanu Prakash Gollapudi0a9c5d32010-06-11 16:44:25 -07001178 /* Drop CVL if there are duplicate critical descriptors */
1179 if ((desc->fip_dtype < 32) &&
1180 !(desc_mask & 1U << desc->fip_dtype)) {
1181 LIBFCOE_FIP_DBG(fip, "Duplicate Critical "
1182 "Descriptors in FIP CVL\n");
1183 return;
1184 }
Joe Eykholt97c83892009-03-17 11:42:40 -07001185 switch (desc->fip_dtype) {
1186 case FIP_DT_MAC:
1187 mp = (struct fip_mac_desc *)desc;
1188 if (dlen < sizeof(*mp))
1189 return;
1190 if (compare_ether_addr(mp->fd_mac, fcf->fcf_mac))
1191 return;
1192 desc_mask &= ~BIT(FIP_DT_MAC);
1193 break;
1194 case FIP_DT_NAME:
1195 wp = (struct fip_wwn_desc *)desc;
1196 if (dlen < sizeof(*wp))
1197 return;
1198 if (get_unaligned_be64(&wp->fd_wwn) != fcf->switch_name)
1199 return;
1200 desc_mask &= ~BIT(FIP_DT_NAME);
1201 break;
1202 case FIP_DT_VN_ID:
1203 vp = (struct fip_vn_desc *)desc;
1204 if (dlen < sizeof(*vp))
1205 return;
1206 if (compare_ether_addr(vp->fd_mac,
Robert Love70b51aa2009-11-03 11:47:45 -08001207 fip->get_src_addr(lport)) == 0 &&
1208 get_unaligned_be64(&vp->fd_wwpn) == lport->wwpn &&
Bhanu Prakash Gollapudi5550fda2010-06-11 16:44:31 -07001209 ntoh24(vp->fd_fc_id) == lport->port_id) {
Joe Eykholt97c83892009-03-17 11:42:40 -07001210 desc_mask &= ~BIT(FIP_DT_VN_ID);
Bhanu Prakash Gollapudi5550fda2010-06-11 16:44:31 -07001211 break;
1212 }
1213 /* check if clr_vlink is for NPIV port */
1214 mutex_lock(&lport->lp_mutex);
1215 list_for_each_entry(vn_port, &lport->vports, list) {
1216 if (compare_ether_addr(vp->fd_mac,
1217 fip->get_src_addr(vn_port)) == 0 &&
1218 (get_unaligned_be64(&vp->fd_wwpn)
1219 == vn_port->wwpn) &&
1220 (ntoh24(vp->fd_fc_id) ==
1221 fc_host_port_id(vn_port->host))) {
1222 desc_mask &= ~BIT(FIP_DT_VN_ID);
1223 is_vn_port = 1;
1224 break;
1225 }
1226 }
1227 mutex_unlock(&lport->lp_mutex);
1228
Joe Eykholt97c83892009-03-17 11:42:40 -07001229 break;
1230 default:
1231 /* standard says ignore unknown descriptors >= 128 */
1232 if (desc->fip_dtype < FIP_DT_VENDOR_BASE)
1233 return;
1234 break;
1235 }
1236 desc = (struct fip_desc *)((char *)desc + dlen);
1237 rlen -= dlen;
1238 }
1239
1240 /*
1241 * reset only if all required descriptors were present and valid.
1242 */
1243 if (desc_mask) {
Joe Eykholt0f51c2e2009-11-03 11:48:16 -08001244 LIBFCOE_FIP_DBG(fip, "missing descriptors mask %x\n",
1245 desc_mask);
Joe Eykholt97c83892009-03-17 11:42:40 -07001246 } else {
Joe Eykholt0f51c2e2009-11-03 11:48:16 -08001247 LIBFCOE_FIP_DBG(fip, "performing Clear Virtual Link\n");
Joe Eykholtdd42dac2009-11-03 11:48:27 -08001248
Bhanu Prakash Gollapudi5550fda2010-06-11 16:44:31 -07001249 if (is_vn_port)
1250 fc_lport_reset(vn_port);
1251 else {
Joe Eykholtfdb068c2010-07-20 15:19:47 -07001252 mutex_lock(&fip->ctlr_mutex);
Bhanu Prakash Gollapudi5550fda2010-06-11 16:44:31 -07001253 per_cpu_ptr(lport->dev_stats,
Joe Eykholtfdb068c2010-07-20 15:19:47 -07001254 get_cpu())->VLinkFailureCount++;
1255 put_cpu();
Bhanu Prakash Gollapudi5550fda2010-06-11 16:44:31 -07001256 fcoe_ctlr_reset(fip);
Joe Eykholtfdb068c2010-07-20 15:19:47 -07001257 mutex_unlock(&fip->ctlr_mutex);
Joe Eykholtdd42dac2009-11-03 11:48:27 -08001258
Bhanu Prakash Gollapudi5550fda2010-06-11 16:44:31 -07001259 fc_lport_reset(fip->lp);
1260 fcoe_ctlr_solicit(fip, NULL);
1261 }
Joe Eykholt97c83892009-03-17 11:42:40 -07001262 }
1263}
1264
1265/**
Robert Love70b51aa2009-11-03 11:47:45 -08001266 * fcoe_ctlr_recv() - Receive a FIP packet
1267 * @fip: The FCoE controller that received the packet
1268 * @skb: The received FIP packet
Joe Eykholt97c83892009-03-17 11:42:40 -07001269 *
Joe Eykholt1f4aed82009-11-03 11:48:22 -08001270 * This may be called from either NET_RX_SOFTIRQ or IRQ.
Joe Eykholt97c83892009-03-17 11:42:40 -07001271 */
1272void fcoe_ctlr_recv(struct fcoe_ctlr *fip, struct sk_buff *skb)
1273{
Joe Eykholt1f4aed82009-11-03 11:48:22 -08001274 skb_queue_tail(&fip->fip_recv_list, skb);
Joe Eykholt97c83892009-03-17 11:42:40 -07001275 schedule_work(&fip->recv_work);
1276}
1277EXPORT_SYMBOL(fcoe_ctlr_recv);
1278
1279/**
Robert Love70b51aa2009-11-03 11:47:45 -08001280 * fcoe_ctlr_recv_handler() - Receive a FIP frame
1281 * @fip: The FCoE controller that received the frame
1282 * @skb: The received FIP frame
Joe Eykholt97c83892009-03-17 11:42:40 -07001283 *
1284 * Returns non-zero if the frame is dropped.
1285 */
1286static int fcoe_ctlr_recv_handler(struct fcoe_ctlr *fip, struct sk_buff *skb)
1287{
1288 struct fip_header *fiph;
1289 struct ethhdr *eh;
1290 enum fip_state state;
1291 u16 op;
1292 u8 sub;
1293
1294 if (skb_linearize(skb))
1295 goto drop;
1296 if (skb->len < sizeof(*fiph))
1297 goto drop;
1298 eh = eth_hdr(skb);
Joe Eykholte10f8c62010-07-20 15:20:30 -07001299 if (fip->mode == FIP_MODE_VN2VN) {
1300 if (compare_ether_addr(eh->h_dest, fip->ctl_src_addr) &&
1301 compare_ether_addr(eh->h_dest, fcoe_all_vn2vn) &&
1302 compare_ether_addr(eh->h_dest, fcoe_all_p2p))
1303 goto drop;
1304 } else if (compare_ether_addr(eh->h_dest, fip->ctl_src_addr) &&
1305 compare_ether_addr(eh->h_dest, fcoe_all_enode))
Joe Eykholt97c83892009-03-17 11:42:40 -07001306 goto drop;
1307 fiph = (struct fip_header *)skb->data;
1308 op = ntohs(fiph->fip_op);
1309 sub = fiph->fip_subcode;
1310
Joe Eykholt97c83892009-03-17 11:42:40 -07001311 if (FIP_VER_DECAPS(fiph->fip_ver) != FIP_VER)
1312 goto drop;
1313 if (ntohs(fiph->fip_dl_len) * FIP_BPW + sizeof(*fiph) > skb->len)
1314 goto drop;
1315
Joe Eykholtfdb068c2010-07-20 15:19:47 -07001316 mutex_lock(&fip->ctlr_mutex);
Joe Eykholt97c83892009-03-17 11:42:40 -07001317 state = fip->state;
1318 if (state == FIP_ST_AUTO) {
1319 fip->map_dest = 0;
Joe Eykholt9b651da2010-07-20 15:20:24 -07001320 fcoe_ctlr_set_state(fip, FIP_ST_ENABLED);
Joe Eykholt97c83892009-03-17 11:42:40 -07001321 state = FIP_ST_ENABLED;
Joe Eykholt0f51c2e2009-11-03 11:48:16 -08001322 LIBFCOE_FIP_DBG(fip, "Using FIP mode\n");
Joe Eykholt97c83892009-03-17 11:42:40 -07001323 }
Joe Eykholtfdb068c2010-07-20 15:19:47 -07001324 mutex_unlock(&fip->ctlr_mutex);
Joe Eykholte10f8c62010-07-20 15:20:30 -07001325
1326 if (fip->mode == FIP_MODE_VN2VN && op == FIP_OP_VN2VN)
1327 return fcoe_ctlr_vn_recv(fip, skb);
1328
1329 if (state != FIP_ST_ENABLED && state != FIP_ST_VNMP_UP &&
1330 state != FIP_ST_VNMP_CLAIM)
Joe Eykholt97c83892009-03-17 11:42:40 -07001331 goto drop;
1332
1333 if (op == FIP_OP_LS) {
1334 fcoe_ctlr_recv_els(fip, skb); /* consumes skb */
1335 return 0;
1336 }
Joe Eykholte10f8c62010-07-20 15:20:30 -07001337
1338 if (state != FIP_ST_ENABLED)
1339 goto drop;
1340
Joe Eykholt97c83892009-03-17 11:42:40 -07001341 if (op == FIP_OP_DISC && sub == FIP_SC_ADV)
1342 fcoe_ctlr_recv_adv(fip, skb);
1343 else if (op == FIP_OP_CTRL && sub == FIP_SC_CLR_VLINK)
1344 fcoe_ctlr_recv_clr_vlink(fip, fiph);
1345 kfree_skb(skb);
1346 return 0;
1347drop:
1348 kfree_skb(skb);
1349 return -1;
1350}
1351
1352/**
Robert Love70b51aa2009-11-03 11:47:45 -08001353 * fcoe_ctlr_select() - Select the best FCF (if possible)
1354 * @fip: The FCoE controller
Joe Eykholt97c83892009-03-17 11:42:40 -07001355 *
1356 * If there are conflicting advertisements, no FCF can be chosen.
1357 *
1358 * Called with lock held.
1359 */
1360static void fcoe_ctlr_select(struct fcoe_ctlr *fip)
1361{
1362 struct fcoe_fcf *fcf;
1363 struct fcoe_fcf *best = NULL;
Joe Eykholtb69ae0a2010-11-30 16:19:51 -08001364 struct fcoe_fcf *first;
1365
1366 first = list_first_entry(&fip->fcfs, struct fcoe_fcf, list);
Joe Eykholt97c83892009-03-17 11:42:40 -07001367
1368 list_for_each_entry(fcf, &fip->fcfs, list) {
Chris Leech9f8f3aa2010-04-09 14:23:16 -07001369 LIBFCOE_FIP_DBG(fip, "consider FCF for fab %16.16llx "
1370 "VFID %d map %x val %d\n",
1371 fcf->fabric_name, fcf->vfid,
Robert Love650bd122009-06-10 15:31:05 -07001372 fcf->fc_map, fcoe_ctlr_mtu_valid(fcf));
Joe Eykholtb69ae0a2010-11-30 16:19:51 -08001373 if (fcf->fabric_name != first->fabric_name ||
1374 fcf->vfid != first->vfid ||
1375 fcf->fc_map != first->fc_map) {
1376 LIBFCOE_FIP_DBG(fip, "Conflicting fabric, VFID, "
1377 "or FC-MAP\n");
1378 return NULL;
1379 }
Joe Eykholt97c83892009-03-17 11:42:40 -07001380 if (!fcoe_ctlr_fcf_usable(fcf)) {
Chris Leech9f8f3aa2010-04-09 14:23:16 -07001381 LIBFCOE_FIP_DBG(fip, "FCF for fab %16.16llx "
1382 "map %x %svalid %savailable\n",
1383 fcf->fabric_name, fcf->fc_map,
1384 (fcf->flags & FIP_FL_SOL) ? "" : "in",
1385 (fcf->flags & FIP_FL_AVAIL) ?
1386 "" : "un");
Joe Eykholt97c83892009-03-17 11:42:40 -07001387 continue;
1388 }
1389 if (!best) {
1390 best = fcf;
1391 continue;
1392 }
Joe Eykholt97c83892009-03-17 11:42:40 -07001393 if (fcf->pri < best->pri)
1394 best = fcf;
1395 }
1396 fip->sel_fcf = best;
Joe Eykholtc47036a2010-11-30 16:19:46 -08001397 if (best) {
1398 fip->port_ka_time = jiffies +
1399 msecs_to_jiffies(FIP_VN_KA_PERIOD);
1400 fip->ctlr_ka_time = jiffies + best->fka_period;
1401 if (time_before(fip->ctlr_ka_time, fip->timer.expires))
1402 mod_timer(&fip->timer, fip->ctlr_ka_time);
1403 }
Joe Eykholt97c83892009-03-17 11:42:40 -07001404}
1405
1406/**
Robert Love70b51aa2009-11-03 11:47:45 -08001407 * fcoe_ctlr_timeout() - FIP timeout handler
1408 * @arg: The FCoE controller that timed out
Joe Eykholt97c83892009-03-17 11:42:40 -07001409 */
1410static void fcoe_ctlr_timeout(unsigned long arg)
1411{
1412 struct fcoe_ctlr *fip = (struct fcoe_ctlr *)arg;
Joe Eykholtfdb068c2010-07-20 15:19:47 -07001413
1414 schedule_work(&fip->timer_work);
1415}
1416
1417/**
1418 * fcoe_ctlr_timer_work() - Worker thread function for timer work
1419 * @work: Handle to a FCoE controller
1420 *
1421 * Ages FCFs. Triggers FCF selection if possible.
1422 * Sends keep-alives and resets.
1423 */
1424static void fcoe_ctlr_timer_work(struct work_struct *work)
1425{
1426 struct fcoe_ctlr *fip;
1427 struct fc_lport *vport;
1428 u8 *mac;
1429 u8 reset = 0;
1430 u8 send_ctlr_ka = 0;
1431 u8 send_port_ka = 0;
Joe Eykholt97c83892009-03-17 11:42:40 -07001432 struct fcoe_fcf *sel;
1433 struct fcoe_fcf *fcf;
Joe Eykholt8690cb82010-06-11 16:44:10 -07001434 unsigned long next_timer;
Joe Eykholt97c83892009-03-17 11:42:40 -07001435
Joe Eykholtfdb068c2010-07-20 15:19:47 -07001436 fip = container_of(work, struct fcoe_ctlr, timer_work);
Joe Eykholte10f8c62010-07-20 15:20:30 -07001437 if (fip->mode == FIP_MODE_VN2VN)
1438 return fcoe_ctlr_vn_timeout(fip);
Joe Eykholtfdb068c2010-07-20 15:19:47 -07001439 mutex_lock(&fip->ctlr_mutex);
Joe Eykholt97c83892009-03-17 11:42:40 -07001440 if (fip->state == FIP_ST_DISABLED) {
Joe Eykholtfdb068c2010-07-20 15:19:47 -07001441 mutex_unlock(&fip->ctlr_mutex);
Joe Eykholt97c83892009-03-17 11:42:40 -07001442 return;
1443 }
1444
1445 fcf = fip->sel_fcf;
Joe Eykholt8690cb82010-06-11 16:44:10 -07001446 next_timer = fcoe_ctlr_age_fcfs(fip);
Joe Eykholt97c83892009-03-17 11:42:40 -07001447
1448 sel = fip->sel_fcf;
Joe Eykholt8690cb82010-06-11 16:44:10 -07001449 if (!sel && fip->sel_time) {
1450 if (time_after_eq(jiffies, fip->sel_time)) {
1451 fcoe_ctlr_select(fip);
1452 sel = fip->sel_fcf;
1453 fip->sel_time = 0;
1454 } else if (time_after(next_timer, fip->sel_time))
1455 next_timer = fip->sel_time;
Joe Eykholt97c83892009-03-17 11:42:40 -07001456 }
1457
1458 if (sel != fcf) {
1459 fcf = sel; /* the old FCF may have been freed */
Joe Eykholt69316ee2010-11-30 16:19:40 -08001460 fcoe_ctlr_announce(fip);
Joe Eykholt97c83892009-03-17 11:42:40 -07001461 if (sel) {
Joe Eykholtd99ee452010-06-11 16:44:15 -07001462 if (time_after(next_timer, fip->ctlr_ka_time))
1463 next_timer = fip->ctlr_ka_time;
Joe Eykholt69316ee2010-11-30 16:19:40 -08001464 } else
Joe Eykholtfdb068c2010-07-20 15:19:47 -07001465 reset = 1;
Joe Eykholt97c83892009-03-17 11:42:40 -07001466 }
1467
Yi Zou8cdffdc2009-11-20 14:54:57 -08001468 if (sel && !sel->fd_flags) {
Joe Eykholt97c83892009-03-17 11:42:40 -07001469 if (time_after_eq(jiffies, fip->ctlr_ka_time)) {
1470 fip->ctlr_ka_time = jiffies + sel->fka_period;
Joe Eykholtfdb068c2010-07-20 15:19:47 -07001471 send_ctlr_ka = 1;
Joe Eykholt97c83892009-03-17 11:42:40 -07001472 }
1473 if (time_after(next_timer, fip->ctlr_ka_time))
1474 next_timer = fip->ctlr_ka_time;
1475
1476 if (time_after_eq(jiffies, fip->port_ka_time)) {
Bhanu Prakash Gollapudif47dd852010-01-21 10:16:00 -08001477 fip->port_ka_time = jiffies +
Robert Love70b51aa2009-11-03 11:47:45 -08001478 msecs_to_jiffies(FIP_VN_KA_PERIOD);
Joe Eykholtfdb068c2010-07-20 15:19:47 -07001479 send_port_ka = 1;
Joe Eykholt97c83892009-03-17 11:42:40 -07001480 }
1481 if (time_after(next_timer, fip->port_ka_time))
1482 next_timer = fip->port_ka_time;
Joe Eykholt97c83892009-03-17 11:42:40 -07001483 }
Joe Eykholt8690cb82010-06-11 16:44:10 -07001484 if (!list_empty(&fip->fcfs))
1485 mod_timer(&fip->timer, next_timer);
Joe Eykholtfdb068c2010-07-20 15:19:47 -07001486 mutex_unlock(&fip->ctlr_mutex);
Joe Eykholt97c83892009-03-17 11:42:40 -07001487
Bhanu Prakash Gollapudi516a6482010-06-11 16:43:44 -07001488 if (reset) {
Joe Eykholtdd42dac2009-11-03 11:48:27 -08001489 fc_lport_reset(fip->lp);
Bhanu Prakash Gollapudi516a6482010-06-11 16:43:44 -07001490 /* restart things with a solicitation */
1491 fcoe_ctlr_solicit(fip, NULL);
1492 }
Chris Leech11b56182009-11-03 11:46:29 -08001493
Joe Eykholtfdb068c2010-07-20 15:19:47 -07001494 if (send_ctlr_ka)
Chris Leech11b56182009-11-03 11:46:29 -08001495 fcoe_ctlr_send_keep_alive(fip, NULL, 0, fip->ctl_src_addr);
Joe Eykholtfdb068c2010-07-20 15:19:47 -07001496
1497 if (send_port_ka) {
Chris Leech11b56182009-11-03 11:46:29 -08001498 mutex_lock(&fip->lp->lp_mutex);
1499 mac = fip->get_src_addr(fip->lp);
1500 fcoe_ctlr_send_keep_alive(fip, fip->lp, 1, mac);
1501 list_for_each_entry(vport, &fip->lp->vports, list) {
1502 mac = fip->get_src_addr(vport);
1503 fcoe_ctlr_send_keep_alive(fip, vport, 1, mac);
1504 }
1505 mutex_unlock(&fip->lp->lp_mutex);
1506 }
Joe Eykholt97c83892009-03-17 11:42:40 -07001507}
1508
1509/**
Robert Love70b51aa2009-11-03 11:47:45 -08001510 * fcoe_ctlr_recv_work() - Worker thread function for receiving FIP frames
1511 * @recv_work: Handle to a FCoE controller
Joe Eykholt97c83892009-03-17 11:42:40 -07001512 */
1513static void fcoe_ctlr_recv_work(struct work_struct *recv_work)
1514{
1515 struct fcoe_ctlr *fip;
1516 struct sk_buff *skb;
1517
1518 fip = container_of(recv_work, struct fcoe_ctlr, recv_work);
Joe Eykholt1f4aed82009-11-03 11:48:22 -08001519 while ((skb = skb_dequeue(&fip->fip_recv_list)))
Joe Eykholt97c83892009-03-17 11:42:40 -07001520 fcoe_ctlr_recv_handler(fip, skb);
Joe Eykholt97c83892009-03-17 11:42:40 -07001521}
1522
1523/**
Joe Eykholt386309c2009-11-03 11:49:16 -08001524 * fcoe_ctlr_recv_flogi() - Snoop pre-FIP receipt of FLOGI response
Robert Love70b51aa2009-11-03 11:47:45 -08001525 * @fip: The FCoE controller
1526 * @fp: The FC frame to snoop
Joe Eykholt97c83892009-03-17 11:42:40 -07001527 *
1528 * Snoop potential response to FLOGI or even incoming FLOGI.
1529 *
1530 * The caller has checked that we are waiting for login as indicated
1531 * by fip->flogi_oxid != FC_XID_UNKNOWN.
1532 *
1533 * The caller is responsible for freeing the frame.
Joe Eykholt386309c2009-11-03 11:49:16 -08001534 * Fill in the granted_mac address.
Joe Eykholt97c83892009-03-17 11:42:40 -07001535 *
1536 * Return non-zero if the frame should not be delivered to libfc.
1537 */
Chris Leech11b56182009-11-03 11:46:29 -08001538int fcoe_ctlr_recv_flogi(struct fcoe_ctlr *fip, struct fc_lport *lport,
Joe Eykholt386309c2009-11-03 11:49:16 -08001539 struct fc_frame *fp)
Joe Eykholt97c83892009-03-17 11:42:40 -07001540{
1541 struct fc_frame_header *fh;
1542 u8 op;
Joe Eykholt386309c2009-11-03 11:49:16 -08001543 u8 *sa;
Joe Eykholt97c83892009-03-17 11:42:40 -07001544
Joe Eykholt386309c2009-11-03 11:49:16 -08001545 sa = eth_hdr(&fp->skb)->h_source;
Joe Eykholt97c83892009-03-17 11:42:40 -07001546 fh = fc_frame_header_get(fp);
1547 if (fh->fh_type != FC_TYPE_ELS)
1548 return 0;
1549
1550 op = fc_frame_payload_op(fp);
1551 if (op == ELS_LS_ACC && fh->fh_r_ctl == FC_RCTL_ELS_REP &&
1552 fip->flogi_oxid == ntohs(fh->fh_ox_id)) {
1553
Joe Eykholtfdb068c2010-07-20 15:19:47 -07001554 mutex_lock(&fip->ctlr_mutex);
Joe Eykholt97c83892009-03-17 11:42:40 -07001555 if (fip->state != FIP_ST_AUTO && fip->state != FIP_ST_NON_FIP) {
Joe Eykholtfdb068c2010-07-20 15:19:47 -07001556 mutex_unlock(&fip->ctlr_mutex);
Joe Eykholt97c83892009-03-17 11:42:40 -07001557 return -EINVAL;
1558 }
Joe Eykholt9b651da2010-07-20 15:20:24 -07001559 fcoe_ctlr_set_state(fip, FIP_ST_NON_FIP);
Joe Eykholt0f51c2e2009-11-03 11:48:16 -08001560 LIBFCOE_FIP_DBG(fip,
1561 "received FLOGI LS_ACC using non-FIP mode\n");
Joe Eykholt97c83892009-03-17 11:42:40 -07001562
1563 /*
1564 * FLOGI accepted.
1565 * If the src mac addr is FC_OUI-based, then we mark the
1566 * address_mode flag to use FC_OUI-based Ethernet DA.
1567 * Otherwise we use the FCoE gateway addr
1568 */
1569 if (!compare_ether_addr(sa, (u8[6])FC_FCOE_FLOGI_MAC)) {
Joe Eykholtcd229e42010-07-20 15:20:40 -07001570 fcoe_ctlr_map_dest(fip);
Joe Eykholt97c83892009-03-17 11:42:40 -07001571 } else {
1572 memcpy(fip->dest_addr, sa, ETH_ALEN);
1573 fip->map_dest = 0;
1574 }
1575 fip->flogi_oxid = FC_XID_UNKNOWN;
Joe Eykholtfdb068c2010-07-20 15:19:47 -07001576 mutex_unlock(&fip->ctlr_mutex);
Joe Eykholt386309c2009-11-03 11:49:16 -08001577 fc_fcoe_set_mac(fr_cb(fp)->granted_mac, fh->fh_d_id);
Joe Eykholt97c83892009-03-17 11:42:40 -07001578 } else if (op == ELS_FLOGI && fh->fh_r_ctl == FC_RCTL_ELS_REQ && sa) {
1579 /*
1580 * Save source MAC for point-to-point responses.
1581 */
Joe Eykholtfdb068c2010-07-20 15:19:47 -07001582 mutex_lock(&fip->ctlr_mutex);
Joe Eykholt97c83892009-03-17 11:42:40 -07001583 if (fip->state == FIP_ST_AUTO || fip->state == FIP_ST_NON_FIP) {
1584 memcpy(fip->dest_addr, sa, ETH_ALEN);
1585 fip->map_dest = 0;
Joe Eykholte49bf612010-03-12 16:07:57 -08001586 if (fip->state == FIP_ST_AUTO)
1587 LIBFCOE_FIP_DBG(fip, "received non-FIP FLOGI. "
1588 "Setting non-FIP mode\n");
Joe Eykholt9b651da2010-07-20 15:20:24 -07001589 fcoe_ctlr_set_state(fip, FIP_ST_NON_FIP);
Joe Eykholt97c83892009-03-17 11:42:40 -07001590 }
Joe Eykholtfdb068c2010-07-20 15:19:47 -07001591 mutex_unlock(&fip->ctlr_mutex);
Joe Eykholt97c83892009-03-17 11:42:40 -07001592 }
1593 return 0;
1594}
1595EXPORT_SYMBOL(fcoe_ctlr_recv_flogi);
1596
Vasu Dev5e80f7f2009-03-17 11:42:18 -07001597/**
Robert Love70b51aa2009-11-03 11:47:45 -08001598 * fcoe_wwn_from_mac() - Converts a 48-bit IEEE MAC address to a 64-bit FC WWN
1599 * @mac: The MAC address to convert
1600 * @scheme: The scheme to use when converting
1601 * @port: The port indicator for converting
Vasu Dev5e80f7f2009-03-17 11:42:18 -07001602 *
1603 * Returns: u64 fc world wide name
1604 */
1605u64 fcoe_wwn_from_mac(unsigned char mac[MAX_ADDR_LEN],
1606 unsigned int scheme, unsigned int port)
1607{
1608 u64 wwn;
1609 u64 host_mac;
1610
1611 /* The MAC is in NO, so flip only the low 48 bits */
1612 host_mac = ((u64) mac[0] << 40) |
1613 ((u64) mac[1] << 32) |
1614 ((u64) mac[2] << 24) |
1615 ((u64) mac[3] << 16) |
1616 ((u64) mac[4] << 8) |
1617 (u64) mac[5];
1618
1619 WARN_ON(host_mac >= (1ULL << 48));
1620 wwn = host_mac | ((u64) scheme << 60);
1621 switch (scheme) {
1622 case 1:
1623 WARN_ON(port != 0);
1624 break;
1625 case 2:
1626 WARN_ON(port >= 0xfff);
1627 wwn |= (u64) port << 48;
1628 break;
1629 default:
1630 WARN_ON(1);
1631 break;
1632 }
1633
1634 return wwn;
1635}
1636EXPORT_SYMBOL_GPL(fcoe_wwn_from_mac);
1637
1638/**
Joe Eykholte10f8c62010-07-20 15:20:30 -07001639 * fcoe_ctlr_rport() - return the fcoe_rport for a given fc_rport_priv
1640 * @rdata: libfc remote port
1641 */
1642static inline struct fcoe_rport *fcoe_ctlr_rport(struct fc_rport_priv *rdata)
1643{
1644 return (struct fcoe_rport *)(rdata + 1);
1645}
1646
1647/**
1648 * fcoe_ctlr_vn_send() - Send a FIP VN2VN Probe Request or Reply.
1649 * @fip: The FCoE controller
1650 * @sub: sub-opcode for probe request, reply, or advertisement.
1651 * @dest: The destination Ethernet MAC address
1652 * @min_len: minimum size of the Ethernet payload to be sent
1653 */
1654static void fcoe_ctlr_vn_send(struct fcoe_ctlr *fip,
1655 enum fip_vn2vn_subcode sub,
1656 const u8 *dest, size_t min_len)
1657{
1658 struct sk_buff *skb;
1659 struct fip_frame {
1660 struct ethhdr eth;
1661 struct fip_header fip;
1662 struct fip_mac_desc mac;
1663 struct fip_wwn_desc wwnn;
1664 struct fip_vn_desc vn;
1665 } __attribute__((packed)) *frame;
1666 struct fip_fc4_feat *ff;
1667 struct fip_size_desc *size;
1668 u32 fcp_feat;
1669 size_t len;
1670 size_t dlen;
1671
1672 len = sizeof(*frame);
1673 dlen = 0;
1674 if (sub == FIP_SC_VN_CLAIM_NOTIFY || sub == FIP_SC_VN_CLAIM_REP) {
1675 dlen = sizeof(struct fip_fc4_feat) +
1676 sizeof(struct fip_size_desc);
1677 len += dlen;
1678 }
1679 dlen += sizeof(frame->mac) + sizeof(frame->wwnn) + sizeof(frame->vn);
1680 len = max(len, min_len + sizeof(struct ethhdr));
1681
1682 skb = dev_alloc_skb(len);
1683 if (!skb)
1684 return;
1685
1686 frame = (struct fip_frame *)skb->data;
1687 memset(frame, 0, len);
1688 memcpy(frame->eth.h_dest, dest, ETH_ALEN);
1689 memcpy(frame->eth.h_source, fip->ctl_src_addr, ETH_ALEN);
1690 frame->eth.h_proto = htons(ETH_P_FIP);
1691
1692 frame->fip.fip_ver = FIP_VER_ENCAPS(FIP_VER);
1693 frame->fip.fip_op = htons(FIP_OP_VN2VN);
1694 frame->fip.fip_subcode = sub;
1695 frame->fip.fip_dl_len = htons(dlen / FIP_BPW);
1696
1697 frame->mac.fd_desc.fip_dtype = FIP_DT_MAC;
1698 frame->mac.fd_desc.fip_dlen = sizeof(frame->mac) / FIP_BPW;
1699 memcpy(frame->mac.fd_mac, fip->ctl_src_addr, ETH_ALEN);
1700
1701 frame->wwnn.fd_desc.fip_dtype = FIP_DT_NAME;
1702 frame->wwnn.fd_desc.fip_dlen = sizeof(frame->wwnn) / FIP_BPW;
1703 put_unaligned_be64(fip->lp->wwnn, &frame->wwnn.fd_wwn);
1704
1705 frame->vn.fd_desc.fip_dtype = FIP_DT_VN_ID;
1706 frame->vn.fd_desc.fip_dlen = sizeof(frame->vn) / FIP_BPW;
1707 hton24(frame->vn.fd_mac, FIP_VN_FC_MAP);
1708 hton24(frame->vn.fd_mac + 3, fip->port_id);
1709 hton24(frame->vn.fd_fc_id, fip->port_id);
1710 put_unaligned_be64(fip->lp->wwpn, &frame->vn.fd_wwpn);
1711
1712 /*
1713 * For claims, add FC-4 features.
1714 * TBD: Add interface to get fc-4 types and features from libfc.
1715 */
1716 if (sub == FIP_SC_VN_CLAIM_NOTIFY || sub == FIP_SC_VN_CLAIM_REP) {
1717 ff = (struct fip_fc4_feat *)(frame + 1);
1718 ff->fd_desc.fip_dtype = FIP_DT_FC4F;
1719 ff->fd_desc.fip_dlen = sizeof(*ff) / FIP_BPW;
1720 ff->fd_fts = fip->lp->fcts;
1721
1722 fcp_feat = 0;
1723 if (fip->lp->service_params & FCP_SPPF_INIT_FCN)
1724 fcp_feat |= FCP_FEAT_INIT;
1725 if (fip->lp->service_params & FCP_SPPF_TARG_FCN)
1726 fcp_feat |= FCP_FEAT_TARG;
1727 fcp_feat <<= (FC_TYPE_FCP * 4) % 32;
1728 ff->fd_ff.fd_feat[FC_TYPE_FCP * 4 / 32] = htonl(fcp_feat);
1729
1730 size = (struct fip_size_desc *)(ff + 1);
1731 size->fd_desc.fip_dtype = FIP_DT_FCOE_SIZE;
1732 size->fd_desc.fip_dlen = sizeof(*size) / FIP_BPW;
1733 size->fd_size = htons(fcoe_ctlr_fcoe_size(fip));
1734 }
1735
1736 skb_put(skb, len);
1737 skb->protocol = htons(ETH_P_FIP);
1738 skb_reset_mac_header(skb);
1739 skb_reset_network_header(skb);
1740
1741 fip->send(fip, skb);
1742}
1743
1744/**
1745 * fcoe_ctlr_vn_rport_callback - Event handler for rport events.
1746 * @lport: The lport which is receiving the event
1747 * @rdata: remote port private data
1748 * @event: The event that occured
1749 *
1750 * Locking Note: The rport lock must not be held when calling this function.
1751 */
1752static void fcoe_ctlr_vn_rport_callback(struct fc_lport *lport,
1753 struct fc_rport_priv *rdata,
1754 enum fc_rport_event event)
1755{
1756 struct fcoe_ctlr *fip = lport->disc.priv;
1757 struct fcoe_rport *frport = fcoe_ctlr_rport(rdata);
1758
1759 LIBFCOE_FIP_DBG(fip, "vn_rport_callback %x event %d\n",
1760 rdata->ids.port_id, event);
1761
1762 mutex_lock(&fip->ctlr_mutex);
1763 switch (event) {
1764 case RPORT_EV_READY:
1765 frport->login_count = 0;
1766 break;
1767 case RPORT_EV_LOGO:
1768 case RPORT_EV_FAILED:
1769 case RPORT_EV_STOP:
1770 frport->login_count++;
1771 if (frport->login_count > FCOE_CTLR_VN2VN_LOGIN_LIMIT) {
1772 LIBFCOE_FIP_DBG(fip,
1773 "rport FLOGI limited port_id %6.6x\n",
1774 rdata->ids.port_id);
1775 lport->tt.rport_logoff(rdata);
1776 }
1777 break;
1778 default:
1779 break;
1780 }
1781 mutex_unlock(&fip->ctlr_mutex);
1782}
1783
1784static struct fc_rport_operations fcoe_ctlr_vn_rport_ops = {
1785 .event_callback = fcoe_ctlr_vn_rport_callback,
1786};
1787
1788/**
1789 * fcoe_ctlr_disc_stop_locked() - stop discovery in VN2VN mode
1790 * @fip: The FCoE controller
1791 *
1792 * Called with ctlr_mutex held.
1793 */
1794static void fcoe_ctlr_disc_stop_locked(struct fc_lport *lport)
1795{
1796 mutex_lock(&lport->disc.disc_mutex);
1797 lport->disc.disc_callback = NULL;
1798 mutex_unlock(&lport->disc.disc_mutex);
1799}
1800
1801/**
1802 * fcoe_ctlr_disc_stop() - stop discovery in VN2VN mode
1803 * @fip: The FCoE controller
1804 *
1805 * Called through the local port template for discovery.
1806 * Called without the ctlr_mutex held.
1807 */
1808static void fcoe_ctlr_disc_stop(struct fc_lport *lport)
1809{
1810 struct fcoe_ctlr *fip = lport->disc.priv;
1811
1812 mutex_lock(&fip->ctlr_mutex);
1813 fcoe_ctlr_disc_stop_locked(lport);
1814 mutex_unlock(&fip->ctlr_mutex);
1815}
1816
1817/**
1818 * fcoe_ctlr_disc_stop_final() - stop discovery for shutdown in VN2VN mode
1819 * @fip: The FCoE controller
1820 *
1821 * Called through the local port template for discovery.
1822 * Called without the ctlr_mutex held.
1823 */
1824static void fcoe_ctlr_disc_stop_final(struct fc_lport *lport)
1825{
1826 fcoe_ctlr_disc_stop(lport);
1827 lport->tt.rport_flush_queue();
1828 synchronize_rcu();
1829}
1830
1831/**
1832 * fcoe_ctlr_vn_restart() - VN2VN probe restart with new port_id
1833 * @fip: The FCoE controller
1834 *
1835 * Called with fcoe_ctlr lock held.
1836 */
1837static void fcoe_ctlr_vn_restart(struct fcoe_ctlr *fip)
1838{
1839 unsigned long wait;
1840 u32 port_id;
1841
1842 fcoe_ctlr_disc_stop_locked(fip->lp);
1843
1844 /*
1845 * Get proposed port ID.
1846 * If this is the first try after link up, use any previous port_id.
1847 * If there was none, use the low bits of the port_name.
1848 * On subsequent tries, get the next random one.
1849 * Don't use reserved IDs, use another non-zero value, just as random.
1850 */
1851 port_id = fip->port_id;
1852 if (fip->probe_tries)
1853 port_id = prandom32(&fip->rnd_state) & 0xffff;
1854 else if (!port_id)
1855 port_id = fip->lp->wwpn & 0xffff;
1856 if (!port_id || port_id == 0xffff)
1857 port_id = 1;
1858 fip->port_id = port_id;
1859
1860 if (fip->probe_tries < FIP_VN_RLIM_COUNT) {
1861 fip->probe_tries++;
1862 wait = random32() % FIP_VN_PROBE_WAIT;
1863 } else
1864 wait = FIP_VN_RLIM_INT;
1865 mod_timer(&fip->timer, jiffies + msecs_to_jiffies(wait));
1866 fcoe_ctlr_set_state(fip, FIP_ST_VNMP_START);
1867}
1868
1869/**
1870 * fcoe_ctlr_vn_start() - Start in VN2VN mode
1871 * @fip: The FCoE controller
1872 *
1873 * Called with fcoe_ctlr lock held.
1874 */
1875static void fcoe_ctlr_vn_start(struct fcoe_ctlr *fip)
1876{
1877 fip->probe_tries = 0;
1878 prandom32_seed(&fip->rnd_state, fip->lp->wwpn);
1879 fcoe_ctlr_vn_restart(fip);
1880}
1881
1882/**
1883 * fcoe_ctlr_vn_parse - parse probe request or response
1884 * @fip: The FCoE controller
1885 * @skb: incoming packet
1886 * @rdata: buffer for resulting parsed VN entry plus fcoe_rport
1887 *
1888 * Returns non-zero error number on error.
1889 * Does not consume the packet.
1890 */
1891static int fcoe_ctlr_vn_parse(struct fcoe_ctlr *fip,
1892 struct sk_buff *skb,
1893 struct fc_rport_priv *rdata)
1894{
1895 struct fip_header *fiph;
1896 struct fip_desc *desc = NULL;
1897 struct fip_mac_desc *macd = NULL;
1898 struct fip_wwn_desc *wwn = NULL;
1899 struct fip_vn_desc *vn = NULL;
1900 struct fip_size_desc *size = NULL;
1901 struct fcoe_rport *frport;
1902 size_t rlen;
1903 size_t dlen;
1904 u32 desc_mask = 0;
1905 u32 dtype;
1906 u8 sub;
1907
1908 memset(rdata, 0, sizeof(*rdata) + sizeof(*frport));
1909 frport = fcoe_ctlr_rport(rdata);
1910
1911 fiph = (struct fip_header *)skb->data;
1912 frport->flags = ntohs(fiph->fip_flags);
1913
1914 sub = fiph->fip_subcode;
1915 switch (sub) {
1916 case FIP_SC_VN_PROBE_REQ:
1917 case FIP_SC_VN_PROBE_REP:
1918 case FIP_SC_VN_BEACON:
1919 desc_mask = BIT(FIP_DT_MAC) | BIT(FIP_DT_NAME) |
1920 BIT(FIP_DT_VN_ID);
1921 break;
1922 case FIP_SC_VN_CLAIM_NOTIFY:
1923 case FIP_SC_VN_CLAIM_REP:
1924 desc_mask = BIT(FIP_DT_MAC) | BIT(FIP_DT_NAME) |
1925 BIT(FIP_DT_VN_ID) | BIT(FIP_DT_FC4F) |
1926 BIT(FIP_DT_FCOE_SIZE);
1927 break;
1928 default:
1929 LIBFCOE_FIP_DBG(fip, "vn_parse unknown subcode %u\n", sub);
1930 return -EINVAL;
1931 }
1932
1933 rlen = ntohs(fiph->fip_dl_len) * 4;
1934 if (rlen + sizeof(*fiph) > skb->len)
1935 return -EINVAL;
1936
1937 desc = (struct fip_desc *)(fiph + 1);
1938 while (rlen > 0) {
1939 dlen = desc->fip_dlen * FIP_BPW;
1940 if (dlen < sizeof(*desc) || dlen > rlen)
1941 return -EINVAL;
1942
1943 dtype = desc->fip_dtype;
1944 if (dtype < 32) {
1945 if (!(desc_mask & BIT(dtype))) {
1946 LIBFCOE_FIP_DBG(fip,
1947 "unexpected or duplicated desc "
1948 "desc type %u in "
1949 "FIP VN2VN subtype %u\n",
1950 dtype, sub);
1951 return -EINVAL;
1952 }
1953 desc_mask &= ~BIT(dtype);
1954 }
1955
1956 switch (dtype) {
1957 case FIP_DT_MAC:
1958 if (dlen != sizeof(struct fip_mac_desc))
1959 goto len_err;
1960 macd = (struct fip_mac_desc *)desc;
1961 if (!is_valid_ether_addr(macd->fd_mac)) {
1962 LIBFCOE_FIP_DBG(fip,
1963 "Invalid MAC addr %pM in FIP VN2VN\n",
1964 macd->fd_mac);
1965 return -EINVAL;
1966 }
1967 memcpy(frport->enode_mac, macd->fd_mac, ETH_ALEN);
1968 break;
1969 case FIP_DT_NAME:
1970 if (dlen != sizeof(struct fip_wwn_desc))
1971 goto len_err;
1972 wwn = (struct fip_wwn_desc *)desc;
1973 rdata->ids.node_name = get_unaligned_be64(&wwn->fd_wwn);
1974 break;
1975 case FIP_DT_VN_ID:
1976 if (dlen != sizeof(struct fip_vn_desc))
1977 goto len_err;
1978 vn = (struct fip_vn_desc *)desc;
1979 memcpy(frport->vn_mac, vn->fd_mac, ETH_ALEN);
1980 rdata->ids.port_id = ntoh24(vn->fd_fc_id);
1981 rdata->ids.port_name = get_unaligned_be64(&vn->fd_wwpn);
1982 break;
1983 case FIP_DT_FC4F:
1984 if (dlen != sizeof(struct fip_fc4_feat))
1985 goto len_err;
1986 break;
1987 case FIP_DT_FCOE_SIZE:
1988 if (dlen != sizeof(struct fip_size_desc))
1989 goto len_err;
1990 size = (struct fip_size_desc *)desc;
1991 frport->fcoe_len = ntohs(size->fd_size);
1992 break;
1993 default:
1994 LIBFCOE_FIP_DBG(fip, "unexpected descriptor type %x "
1995 "in FIP probe\n", dtype);
1996 /* standard says ignore unknown descriptors >= 128 */
1997 if (dtype < FIP_DT_VENDOR_BASE)
1998 return -EINVAL;
1999 break;
2000 }
2001 desc = (struct fip_desc *)((char *)desc + dlen);
2002 rlen -= dlen;
2003 }
2004 return 0;
2005
2006len_err:
2007 LIBFCOE_FIP_DBG(fip, "FIP length error in descriptor type %x len %zu\n",
2008 dtype, dlen);
2009 return -EINVAL;
2010}
2011
2012/**
2013 * fcoe_ctlr_vn_send_claim() - send multicast FIP VN2VN Claim Notification.
2014 * @fip: The FCoE controller
2015 *
2016 * Called with ctlr_mutex held.
2017 */
2018static void fcoe_ctlr_vn_send_claim(struct fcoe_ctlr *fip)
2019{
2020 fcoe_ctlr_vn_send(fip, FIP_SC_VN_CLAIM_NOTIFY, fcoe_all_vn2vn, 0);
2021 fip->sol_time = jiffies;
2022}
2023
2024/**
2025 * fcoe_ctlr_vn_probe_req() - handle incoming VN2VN probe request.
2026 * @fip: The FCoE controller
2027 * @rdata: parsed remote port with frport from the probe request
2028 *
2029 * Called with ctlr_mutex held.
2030 */
2031static void fcoe_ctlr_vn_probe_req(struct fcoe_ctlr *fip,
2032 struct fc_rport_priv *rdata)
2033{
2034 struct fcoe_rport *frport = fcoe_ctlr_rport(rdata);
2035
2036 if (rdata->ids.port_id != fip->port_id)
2037 return;
2038
2039 switch (fip->state) {
2040 case FIP_ST_VNMP_CLAIM:
2041 case FIP_ST_VNMP_UP:
2042 fcoe_ctlr_vn_send(fip, FIP_SC_VN_PROBE_REP,
2043 frport->enode_mac, 0);
2044 break;
2045 case FIP_ST_VNMP_PROBE1:
2046 case FIP_ST_VNMP_PROBE2:
2047 /*
2048 * Decide whether to reply to the Probe.
2049 * Our selected address is never a "recorded" one, so
2050 * only reply if our WWPN is greater and the
2051 * Probe's REC bit is not set.
2052 * If we don't reply, we will change our address.
2053 */
2054 if (fip->lp->wwpn > rdata->ids.port_name &&
2055 !(frport->flags & FIP_FL_REC_OR_P2P)) {
2056 fcoe_ctlr_vn_send(fip, FIP_SC_VN_PROBE_REP,
2057 frport->enode_mac, 0);
2058 break;
2059 }
2060 /* fall through */
2061 case FIP_ST_VNMP_START:
2062 fcoe_ctlr_vn_restart(fip);
2063 break;
2064 default:
2065 break;
2066 }
2067}
2068
2069/**
2070 * fcoe_ctlr_vn_probe_reply() - handle incoming VN2VN probe reply.
2071 * @fip: The FCoE controller
2072 * @rdata: parsed remote port with frport from the probe request
2073 *
2074 * Called with ctlr_mutex held.
2075 */
2076static void fcoe_ctlr_vn_probe_reply(struct fcoe_ctlr *fip,
2077 struct fc_rport_priv *rdata)
2078{
2079 if (rdata->ids.port_id != fip->port_id)
2080 return;
2081 switch (fip->state) {
2082 case FIP_ST_VNMP_START:
2083 case FIP_ST_VNMP_PROBE1:
2084 case FIP_ST_VNMP_PROBE2:
2085 case FIP_ST_VNMP_CLAIM:
2086 fcoe_ctlr_vn_restart(fip);
2087 break;
2088 case FIP_ST_VNMP_UP:
2089 fcoe_ctlr_vn_send_claim(fip);
2090 break;
2091 default:
2092 break;
2093 }
2094}
2095
2096/**
2097 * fcoe_ctlr_vn_add() - Add a VN2VN entry to the list, based on a claim reply.
2098 * @fip: The FCoE controller
2099 * @new: newly-parsed remote port with frport as a template for new rdata
2100 *
2101 * Called with ctlr_mutex held.
2102 */
2103static void fcoe_ctlr_vn_add(struct fcoe_ctlr *fip, struct fc_rport_priv *new)
2104{
2105 struct fc_lport *lport = fip->lp;
2106 struct fc_rport_priv *rdata;
2107 struct fc_rport_identifiers *ids;
2108 struct fcoe_rport *frport;
2109 u32 port_id;
2110
2111 port_id = new->ids.port_id;
2112 if (port_id == fip->port_id)
2113 return;
2114
2115 mutex_lock(&lport->disc.disc_mutex);
2116 rdata = lport->tt.rport_create(lport, port_id);
2117 if (!rdata) {
2118 mutex_unlock(&lport->disc.disc_mutex);
2119 return;
2120 }
2121
2122 rdata->ops = &fcoe_ctlr_vn_rport_ops;
2123 rdata->disc_id = lport->disc.disc_id;
2124
2125 ids = &rdata->ids;
2126 if ((ids->port_name != -1 && ids->port_name != new->ids.port_name) ||
2127 (ids->node_name != -1 && ids->node_name != new->ids.node_name))
2128 lport->tt.rport_logoff(rdata);
2129 ids->port_name = new->ids.port_name;
2130 ids->node_name = new->ids.node_name;
2131 mutex_unlock(&lport->disc.disc_mutex);
2132
2133 frport = fcoe_ctlr_rport(rdata);
2134 LIBFCOE_FIP_DBG(fip, "vn_add rport %6.6x %s\n",
2135 port_id, frport->fcoe_len ? "old" : "new");
2136 *frport = *fcoe_ctlr_rport(new);
2137 frport->time = 0;
2138}
2139
2140/**
2141 * fcoe_ctlr_vn_lookup() - Find VN remote port's MAC address
2142 * @fip: The FCoE controller
2143 * @port_id: The port_id of the remote VN_node
2144 * @mac: buffer which will hold the VN_NODE destination MAC address, if found.
2145 *
2146 * Returns non-zero error if no remote port found.
2147 */
2148static int fcoe_ctlr_vn_lookup(struct fcoe_ctlr *fip, u32 port_id, u8 *mac)
2149{
2150 struct fc_lport *lport = fip->lp;
2151 struct fc_rport_priv *rdata;
2152 struct fcoe_rport *frport;
2153 int ret = -1;
2154
2155 rcu_read_lock();
2156 rdata = lport->tt.rport_lookup(lport, port_id);
2157 if (rdata) {
2158 frport = fcoe_ctlr_rport(rdata);
2159 memcpy(mac, frport->enode_mac, ETH_ALEN);
2160 ret = 0;
2161 }
2162 rcu_read_unlock();
2163 return ret;
2164}
2165
2166/**
2167 * fcoe_ctlr_vn_claim_notify() - handle received FIP VN2VN Claim Notification
2168 * @fip: The FCoE controller
2169 * @new: newly-parsed remote port with frport as a template for new rdata
2170 *
2171 * Called with ctlr_mutex held.
2172 */
2173static void fcoe_ctlr_vn_claim_notify(struct fcoe_ctlr *fip,
2174 struct fc_rport_priv *new)
2175{
2176 struct fcoe_rport *frport = fcoe_ctlr_rport(new);
2177
2178 if (frport->flags & FIP_FL_REC_OR_P2P) {
2179 fcoe_ctlr_vn_send(fip, FIP_SC_VN_PROBE_REQ, fcoe_all_vn2vn, 0);
2180 return;
2181 }
2182 switch (fip->state) {
2183 case FIP_ST_VNMP_START:
2184 case FIP_ST_VNMP_PROBE1:
2185 case FIP_ST_VNMP_PROBE2:
2186 if (new->ids.port_id == fip->port_id)
2187 fcoe_ctlr_vn_restart(fip);
2188 break;
2189 case FIP_ST_VNMP_CLAIM:
2190 case FIP_ST_VNMP_UP:
2191 if (new->ids.port_id == fip->port_id) {
2192 if (new->ids.port_name > fip->lp->wwpn) {
2193 fcoe_ctlr_vn_restart(fip);
2194 break;
2195 }
2196 fcoe_ctlr_vn_send_claim(fip);
2197 break;
2198 }
2199 fcoe_ctlr_vn_send(fip, FIP_SC_VN_CLAIM_REP, frport->enode_mac,
2200 min((u32)frport->fcoe_len,
2201 fcoe_ctlr_fcoe_size(fip)));
2202 fcoe_ctlr_vn_add(fip, new);
2203 break;
2204 default:
2205 break;
2206 }
2207}
2208
2209/**
2210 * fcoe_ctlr_vn_claim_resp() - handle received Claim Response
2211 * @fip: The FCoE controller that received the frame
2212 * @new: newly-parsed remote port with frport from the Claim Response
2213 *
2214 * Called with ctlr_mutex held.
2215 */
2216static void fcoe_ctlr_vn_claim_resp(struct fcoe_ctlr *fip,
2217 struct fc_rport_priv *new)
2218{
2219 LIBFCOE_FIP_DBG(fip, "claim resp from from rport %x - state %s\n",
2220 new->ids.port_id, fcoe_ctlr_state(fip->state));
2221 if (fip->state == FIP_ST_VNMP_UP || fip->state == FIP_ST_VNMP_CLAIM)
2222 fcoe_ctlr_vn_add(fip, new);
2223}
2224
2225/**
2226 * fcoe_ctlr_vn_beacon() - handle received beacon.
2227 * @fip: The FCoE controller that received the frame
2228 * @new: newly-parsed remote port with frport from the Beacon
2229 *
2230 * Called with ctlr_mutex held.
2231 */
2232static void fcoe_ctlr_vn_beacon(struct fcoe_ctlr *fip,
2233 struct fc_rport_priv *new)
2234{
2235 struct fc_lport *lport = fip->lp;
2236 struct fc_rport_priv *rdata;
2237 struct fcoe_rport *frport;
2238
2239 frport = fcoe_ctlr_rport(new);
2240 if (frport->flags & FIP_FL_REC_OR_P2P) {
2241 fcoe_ctlr_vn_send(fip, FIP_SC_VN_PROBE_REQ, fcoe_all_vn2vn, 0);
2242 return;
2243 }
2244 mutex_lock(&lport->disc.disc_mutex);
2245 rdata = lport->tt.rport_lookup(lport, new->ids.port_id);
2246 if (rdata)
2247 kref_get(&rdata->kref);
2248 mutex_unlock(&lport->disc.disc_mutex);
2249 if (rdata) {
2250 if (rdata->ids.node_name == new->ids.node_name &&
2251 rdata->ids.port_name == new->ids.port_name) {
2252 frport = fcoe_ctlr_rport(rdata);
2253 if (!frport->time && fip->state == FIP_ST_VNMP_UP)
2254 lport->tt.rport_login(rdata);
2255 frport->time = jiffies;
2256 }
2257 kref_put(&rdata->kref, lport->tt.rport_destroy);
2258 return;
2259 }
2260 if (fip->state != FIP_ST_VNMP_UP)
2261 return;
2262
2263 /*
2264 * Beacon from a new neighbor.
2265 * Send a claim notify if one hasn't been sent recently.
2266 * Don't add the neighbor yet.
2267 */
2268 LIBFCOE_FIP_DBG(fip, "beacon from new rport %x. sending claim notify\n",
2269 new->ids.port_id);
2270 if (time_after(jiffies,
2271 fip->sol_time + msecs_to_jiffies(FIP_VN_ANN_WAIT)))
2272 fcoe_ctlr_vn_send_claim(fip);
2273}
2274
2275/**
2276 * fcoe_ctlr_vn_age() - Check for VN_ports without recent beacons
2277 * @fip: The FCoE controller
2278 *
2279 * Called with ctlr_mutex held.
2280 * Called only in state FIP_ST_VNMP_UP.
2281 * Returns the soonest time for next age-out or a time far in the future.
2282 */
2283static unsigned long fcoe_ctlr_vn_age(struct fcoe_ctlr *fip)
2284{
2285 struct fc_lport *lport = fip->lp;
2286 struct fc_rport_priv *rdata;
2287 struct fcoe_rport *frport;
2288 unsigned long next_time;
2289 unsigned long deadline;
2290
2291 next_time = jiffies + msecs_to_jiffies(FIP_VN_BEACON_INT * 10);
2292 mutex_lock(&lport->disc.disc_mutex);
2293 list_for_each_entry_rcu(rdata, &lport->disc.rports, peers) {
2294 frport = fcoe_ctlr_rport(rdata);
2295 if (!frport->time)
2296 continue;
2297 deadline = frport->time +
2298 msecs_to_jiffies(FIP_VN_BEACON_INT * 25 / 10);
2299 if (time_after_eq(jiffies, deadline)) {
2300 frport->time = 0;
2301 LIBFCOE_FIP_DBG(fip,
2302 "port %16.16llx fc_id %6.6x beacon expired\n",
2303 rdata->ids.port_name, rdata->ids.port_id);
2304 lport->tt.rport_logoff(rdata);
2305 } else if (time_before(deadline, next_time))
2306 next_time = deadline;
2307 }
2308 mutex_unlock(&lport->disc.disc_mutex);
2309 return next_time;
2310}
2311
2312/**
2313 * fcoe_ctlr_vn_recv() - Receive a FIP frame
2314 * @fip: The FCoE controller that received the frame
2315 * @skb: The received FIP frame
2316 *
2317 * Returns non-zero if the frame is dropped.
2318 * Always consumes the frame.
2319 */
2320static int fcoe_ctlr_vn_recv(struct fcoe_ctlr *fip, struct sk_buff *skb)
2321{
2322 struct fip_header *fiph;
2323 enum fip_vn2vn_subcode sub;
Kiran Patil2dc02ee2010-10-08 17:12:41 -07002324 struct {
Joe Eykholte10f8c62010-07-20 15:20:30 -07002325 struct fc_rport_priv rdata;
2326 struct fcoe_rport frport;
2327 } buf;
2328 int rc;
2329
2330 fiph = (struct fip_header *)skb->data;
2331 sub = fiph->fip_subcode;
2332
2333 rc = fcoe_ctlr_vn_parse(fip, skb, &buf.rdata);
2334 if (rc) {
2335 LIBFCOE_FIP_DBG(fip, "vn_recv vn_parse error %d\n", rc);
2336 goto drop;
2337 }
2338
2339 mutex_lock(&fip->ctlr_mutex);
2340 switch (sub) {
2341 case FIP_SC_VN_PROBE_REQ:
2342 fcoe_ctlr_vn_probe_req(fip, &buf.rdata);
2343 break;
2344 case FIP_SC_VN_PROBE_REP:
2345 fcoe_ctlr_vn_probe_reply(fip, &buf.rdata);
2346 break;
2347 case FIP_SC_VN_CLAIM_NOTIFY:
2348 fcoe_ctlr_vn_claim_notify(fip, &buf.rdata);
2349 break;
2350 case FIP_SC_VN_CLAIM_REP:
2351 fcoe_ctlr_vn_claim_resp(fip, &buf.rdata);
2352 break;
2353 case FIP_SC_VN_BEACON:
2354 fcoe_ctlr_vn_beacon(fip, &buf.rdata);
2355 break;
2356 default:
2357 LIBFCOE_FIP_DBG(fip, "vn_recv unknown subcode %d\n", sub);
2358 rc = -1;
2359 break;
2360 }
2361 mutex_unlock(&fip->ctlr_mutex);
2362drop:
2363 kfree_skb(skb);
2364 return rc;
2365}
2366
2367/**
2368 * fcoe_ctlr_disc_recv - discovery receive handler for VN2VN mode.
Joe Eykholt922611562010-07-20 15:21:12 -07002369 * @lport: The local port
2370 * @fp: The received frame
Joe Eykholte10f8c62010-07-20 15:20:30 -07002371 *
2372 * This should never be called since we don't see RSCNs or other
2373 * fabric-generated ELSes.
2374 */
Joe Eykholt922611562010-07-20 15:21:12 -07002375static void fcoe_ctlr_disc_recv(struct fc_lport *lport, struct fc_frame *fp)
Joe Eykholte10f8c62010-07-20 15:20:30 -07002376{
2377 struct fc_seq_els_data rjt_data;
2378
Joe Eykholte10f8c62010-07-20 15:20:30 -07002379 rjt_data.reason = ELS_RJT_UNSUP;
2380 rjt_data.explan = ELS_EXPL_NONE;
Joe Eykholt922611562010-07-20 15:21:12 -07002381 lport->tt.seq_els_rsp_send(fp, ELS_LS_RJT, &rjt_data);
Joe Eykholte10f8c62010-07-20 15:20:30 -07002382 fc_frame_free(fp);
2383}
2384
2385/**
2386 * fcoe_ctlr_disc_recv - start discovery for VN2VN mode.
2387 * @fip: The FCoE controller
2388 *
2389 * This sets a flag indicating that remote ports should be created
2390 * and started for the peers we discover. We use the disc_callback
2391 * pointer as that flag. Peers already discovered are created here.
2392 *
2393 * The lport lock is held during this call. The callback must be done
2394 * later, without holding either the lport or discovery locks.
2395 * The fcoe_ctlr lock may also be held during this call.
2396 */
2397static void fcoe_ctlr_disc_start(void (*callback)(struct fc_lport *,
2398 enum fc_disc_event),
2399 struct fc_lport *lport)
2400{
2401 struct fc_disc *disc = &lport->disc;
2402 struct fcoe_ctlr *fip = disc->priv;
2403
2404 mutex_lock(&disc->disc_mutex);
2405 disc->disc_callback = callback;
2406 disc->disc_id = (disc->disc_id + 2) | 1;
2407 disc->pending = 1;
2408 schedule_work(&fip->timer_work);
2409 mutex_unlock(&disc->disc_mutex);
2410}
2411
2412/**
2413 * fcoe_ctlr_vn_disc() - report FIP VN_port discovery results after claim state.
2414 * @fip: The FCoE controller
2415 *
2416 * Starts the FLOGI and PLOGI login process to each discovered rport for which
2417 * we've received at least one beacon.
2418 * Performs the discovery complete callback.
2419 */
2420static void fcoe_ctlr_vn_disc(struct fcoe_ctlr *fip)
2421{
2422 struct fc_lport *lport = fip->lp;
2423 struct fc_disc *disc = &lport->disc;
2424 struct fc_rport_priv *rdata;
2425 struct fcoe_rport *frport;
2426 void (*callback)(struct fc_lport *, enum fc_disc_event);
2427
2428 mutex_lock(&disc->disc_mutex);
2429 callback = disc->pending ? disc->disc_callback : NULL;
2430 disc->pending = 0;
2431 list_for_each_entry_rcu(rdata, &disc->rports, peers) {
2432 frport = fcoe_ctlr_rport(rdata);
2433 if (frport->time)
2434 lport->tt.rport_login(rdata);
2435 }
2436 mutex_unlock(&disc->disc_mutex);
2437 if (callback)
2438 callback(lport, DISC_EV_SUCCESS);
2439}
2440
2441/**
2442 * fcoe_ctlr_vn_timeout - timer work function for VN2VN mode.
2443 * @fip: The FCoE controller
2444 */
2445static void fcoe_ctlr_vn_timeout(struct fcoe_ctlr *fip)
2446{
2447 unsigned long next_time;
2448 u8 mac[ETH_ALEN];
2449 u32 new_port_id = 0;
2450
2451 mutex_lock(&fip->ctlr_mutex);
2452 switch (fip->state) {
2453 case FIP_ST_VNMP_START:
2454 fcoe_ctlr_set_state(fip, FIP_ST_VNMP_PROBE1);
2455 fcoe_ctlr_vn_send(fip, FIP_SC_VN_PROBE_REQ, fcoe_all_vn2vn, 0);
2456 next_time = jiffies + msecs_to_jiffies(FIP_VN_PROBE_WAIT);
2457 break;
2458 case FIP_ST_VNMP_PROBE1:
2459 fcoe_ctlr_set_state(fip, FIP_ST_VNMP_PROBE2);
2460 fcoe_ctlr_vn_send(fip, FIP_SC_VN_PROBE_REQ, fcoe_all_vn2vn, 0);
2461 next_time = jiffies + msecs_to_jiffies(FIP_VN_ANN_WAIT);
2462 break;
2463 case FIP_ST_VNMP_PROBE2:
2464 fcoe_ctlr_set_state(fip, FIP_ST_VNMP_CLAIM);
2465 new_port_id = fip->port_id;
2466 hton24(mac, FIP_VN_FC_MAP);
2467 hton24(mac + 3, new_port_id);
Joe Eykholtcd229e42010-07-20 15:20:40 -07002468 fcoe_ctlr_map_dest(fip);
Joe Eykholte10f8c62010-07-20 15:20:30 -07002469 fip->update_mac(fip->lp, mac);
2470 fcoe_ctlr_vn_send_claim(fip);
2471 next_time = jiffies + msecs_to_jiffies(FIP_VN_ANN_WAIT);
2472 break;
2473 case FIP_ST_VNMP_CLAIM:
2474 /*
2475 * This may be invoked either by starting discovery so don't
2476 * go to the next state unless it's been long enough.
2477 */
2478 next_time = fip->sol_time + msecs_to_jiffies(FIP_VN_ANN_WAIT);
2479 if (time_after_eq(jiffies, next_time)) {
2480 fcoe_ctlr_set_state(fip, FIP_ST_VNMP_UP);
2481 fcoe_ctlr_vn_send(fip, FIP_SC_VN_BEACON,
2482 fcoe_all_vn2vn, 0);
2483 next_time = jiffies + msecs_to_jiffies(FIP_VN_ANN_WAIT);
2484 fip->port_ka_time = next_time;
2485 }
2486 fcoe_ctlr_vn_disc(fip);
2487 break;
2488 case FIP_ST_VNMP_UP:
2489 next_time = fcoe_ctlr_vn_age(fip);
2490 if (time_after_eq(jiffies, fip->port_ka_time)) {
2491 fcoe_ctlr_vn_send(fip, FIP_SC_VN_BEACON,
2492 fcoe_all_vn2vn, 0);
2493 fip->port_ka_time = jiffies +
2494 msecs_to_jiffies(FIP_VN_BEACON_INT +
2495 (random32() % FIP_VN_BEACON_FUZZ));
2496 }
2497 if (time_before(fip->port_ka_time, next_time))
2498 next_time = fip->port_ka_time;
2499 break;
2500 case FIP_ST_LINK_WAIT:
2501 goto unlock;
2502 default:
Joe Perches11aa9902010-11-30 16:19:09 -08002503 WARN(1, "unexpected state %d\n", fip->state);
Joe Eykholte10f8c62010-07-20 15:20:30 -07002504 goto unlock;
2505 }
2506 mod_timer(&fip->timer, next_time);
2507unlock:
2508 mutex_unlock(&fip->ctlr_mutex);
2509
2510 /* If port ID is new, notify local port after dropping ctlr_mutex */
2511 if (new_port_id)
2512 fc_lport_set_local_id(fip->lp, new_port_id);
2513}
2514
2515/**
Robert Love70b51aa2009-11-03 11:47:45 -08002516 * fcoe_libfc_config() - Sets up libfc related properties for local port
2517 * @lp: The local port to configure libfc for
Joe Eykholte10f8c62010-07-20 15:20:30 -07002518 * @fip: The FCoE controller in use by the local port
Robert Love70b51aa2009-11-03 11:47:45 -08002519 * @tt: The libfc function template
Joe Eykholte10f8c62010-07-20 15:20:30 -07002520 * @init_fcp: If non-zero, the FCP portion of libfc should be initialized
Vasu Dev5e80f7f2009-03-17 11:42:18 -07002521 *
2522 * Returns : 0 for success
2523 */
Joe Eykholte10f8c62010-07-20 15:20:30 -07002524int fcoe_libfc_config(struct fc_lport *lport, struct fcoe_ctlr *fip,
2525 const struct libfc_function_template *tt, int init_fcp)
Vasu Dev5e80f7f2009-03-17 11:42:18 -07002526{
2527 /* Set the function pointers set by the LLDD */
Robert Love70b51aa2009-11-03 11:47:45 -08002528 memcpy(&lport->tt, tt, sizeof(*tt));
Joe Eykholte10f8c62010-07-20 15:20:30 -07002529 if (init_fcp && fc_fcp_init(lport))
Vasu Dev5e80f7f2009-03-17 11:42:18 -07002530 return -ENOMEM;
Robert Love70b51aa2009-11-03 11:47:45 -08002531 fc_exch_init(lport);
2532 fc_elsct_init(lport);
2533 fc_lport_init(lport);
Joe Eykholte10f8c62010-07-20 15:20:30 -07002534 if (fip->mode == FIP_MODE_VN2VN)
2535 lport->rport_priv_size = sizeof(struct fcoe_rport);
Robert Love70b51aa2009-11-03 11:47:45 -08002536 fc_rport_init(lport);
Joe Eykholte10f8c62010-07-20 15:20:30 -07002537 if (fip->mode == FIP_MODE_VN2VN) {
2538 lport->point_to_multipoint = 1;
2539 lport->tt.disc_recv_req = fcoe_ctlr_disc_recv;
2540 lport->tt.disc_start = fcoe_ctlr_disc_start;
2541 lport->tt.disc_stop = fcoe_ctlr_disc_stop;
2542 lport->tt.disc_stop_final = fcoe_ctlr_disc_stop_final;
2543 mutex_init(&lport->disc.disc_mutex);
2544 INIT_LIST_HEAD(&lport->disc.rports);
2545 lport->disc.priv = fip;
2546 } else {
2547 fc_disc_init(lport);
2548 }
Vasu Dev5e80f7f2009-03-17 11:42:18 -07002549 return 0;
2550}
2551EXPORT_SYMBOL_GPL(fcoe_libfc_config);