blob: 26381f00e4e5350084d3de1641c4af1874c618fa [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 Zouf3da80e72009-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 Zouf3da80e72009-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;
1364
1365 list_for_each_entry(fcf, &fip->fcfs, list) {
Chris Leech9f8f3aa2010-04-09 14:23:16 -07001366 LIBFCOE_FIP_DBG(fip, "consider FCF for fab %16.16llx "
1367 "VFID %d map %x val %d\n",
1368 fcf->fabric_name, fcf->vfid,
Robert Love650bd122009-06-10 15:31:05 -07001369 fcf->fc_map, fcoe_ctlr_mtu_valid(fcf));
Joe Eykholt97c83892009-03-17 11:42:40 -07001370 if (!fcoe_ctlr_fcf_usable(fcf)) {
Chris Leech9f8f3aa2010-04-09 14:23:16 -07001371 LIBFCOE_FIP_DBG(fip, "FCF for fab %16.16llx "
1372 "map %x %svalid %savailable\n",
1373 fcf->fabric_name, fcf->fc_map,
1374 (fcf->flags & FIP_FL_SOL) ? "" : "in",
1375 (fcf->flags & FIP_FL_AVAIL) ?
1376 "" : "un");
Joe Eykholt97c83892009-03-17 11:42:40 -07001377 continue;
1378 }
1379 if (!best) {
1380 best = fcf;
1381 continue;
1382 }
1383 if (fcf->fabric_name != best->fabric_name ||
1384 fcf->vfid != best->vfid ||
1385 fcf->fc_map != best->fc_map) {
Joe Eykholt0f51c2e2009-11-03 11:48:16 -08001386 LIBFCOE_FIP_DBG(fip, "Conflicting fabric, VFID, "
Robert Love650bd122009-06-10 15:31:05 -07001387 "or FC-MAP\n");
Joe Eykholt97c83892009-03-17 11:42:40 -07001388 return;
1389 }
1390 if (fcf->pri < best->pri)
1391 best = fcf;
1392 }
1393 fip->sel_fcf = best;
1394}
1395
1396/**
Robert Love70b51aa2009-11-03 11:47:45 -08001397 * fcoe_ctlr_timeout() - FIP timeout handler
1398 * @arg: The FCoE controller that timed out
Joe Eykholt97c83892009-03-17 11:42:40 -07001399 */
1400static void fcoe_ctlr_timeout(unsigned long arg)
1401{
1402 struct fcoe_ctlr *fip = (struct fcoe_ctlr *)arg;
Joe Eykholtfdb068c2010-07-20 15:19:47 -07001403
1404 schedule_work(&fip->timer_work);
1405}
1406
1407/**
1408 * fcoe_ctlr_timer_work() - Worker thread function for timer work
1409 * @work: Handle to a FCoE controller
1410 *
1411 * Ages FCFs. Triggers FCF selection if possible.
1412 * Sends keep-alives and resets.
1413 */
1414static void fcoe_ctlr_timer_work(struct work_struct *work)
1415{
1416 struct fcoe_ctlr *fip;
1417 struct fc_lport *vport;
1418 u8 *mac;
1419 u8 reset = 0;
1420 u8 send_ctlr_ka = 0;
1421 u8 send_port_ka = 0;
Joe Eykholt97c83892009-03-17 11:42:40 -07001422 struct fcoe_fcf *sel;
1423 struct fcoe_fcf *fcf;
Joe Eykholt8690cb82010-06-11 16:44:10 -07001424 unsigned long next_timer;
Joe Eykholt97c83892009-03-17 11:42:40 -07001425
Joe Eykholtfdb068c2010-07-20 15:19:47 -07001426 fip = container_of(work, struct fcoe_ctlr, timer_work);
Joe Eykholte10f8c62010-07-20 15:20:30 -07001427 if (fip->mode == FIP_MODE_VN2VN)
1428 return fcoe_ctlr_vn_timeout(fip);
Joe Eykholtfdb068c2010-07-20 15:19:47 -07001429 mutex_lock(&fip->ctlr_mutex);
Joe Eykholt97c83892009-03-17 11:42:40 -07001430 if (fip->state == FIP_ST_DISABLED) {
Joe Eykholtfdb068c2010-07-20 15:19:47 -07001431 mutex_unlock(&fip->ctlr_mutex);
Joe Eykholt97c83892009-03-17 11:42:40 -07001432 return;
1433 }
1434
1435 fcf = fip->sel_fcf;
Joe Eykholt8690cb82010-06-11 16:44:10 -07001436 next_timer = fcoe_ctlr_age_fcfs(fip);
Joe Eykholt97c83892009-03-17 11:42:40 -07001437
1438 sel = fip->sel_fcf;
Joe Eykholt8690cb82010-06-11 16:44:10 -07001439 if (!sel && fip->sel_time) {
1440 if (time_after_eq(jiffies, fip->sel_time)) {
1441 fcoe_ctlr_select(fip);
1442 sel = fip->sel_fcf;
1443 fip->sel_time = 0;
1444 } else if (time_after(next_timer, fip->sel_time))
1445 next_timer = fip->sel_time;
Joe Eykholt97c83892009-03-17 11:42:40 -07001446 }
1447
1448 if (sel != fcf) {
1449 fcf = sel; /* the old FCF may have been freed */
Joe Eykholt69316ee2010-11-30 16:19:40 -08001450 fcoe_ctlr_announce(fip);
Joe Eykholt97c83892009-03-17 11:42:40 -07001451 if (sel) {
Joe Eykholt97c83892009-03-17 11:42:40 -07001452 fip->port_ka_time = jiffies +
Robert Love70b51aa2009-11-03 11:47:45 -08001453 msecs_to_jiffies(FIP_VN_KA_PERIOD);
Joe Eykholt97c83892009-03-17 11:42:40 -07001454 fip->ctlr_ka_time = jiffies + sel->fka_period;
Joe Eykholtd99ee452010-06-11 16:44:15 -07001455 if (time_after(next_timer, fip->ctlr_ka_time))
1456 next_timer = fip->ctlr_ka_time;
Joe Eykholt69316ee2010-11-30 16:19:40 -08001457 } else
Joe Eykholtfdb068c2010-07-20 15:19:47 -07001458 reset = 1;
Joe Eykholt97c83892009-03-17 11:42:40 -07001459 }
1460
Yi Zou8cdffdc2009-11-20 14:54:57 -08001461 if (sel && !sel->fd_flags) {
Joe Eykholt97c83892009-03-17 11:42:40 -07001462 if (time_after_eq(jiffies, fip->ctlr_ka_time)) {
1463 fip->ctlr_ka_time = jiffies + sel->fka_period;
Joe Eykholtfdb068c2010-07-20 15:19:47 -07001464 send_ctlr_ka = 1;
Joe Eykholt97c83892009-03-17 11:42:40 -07001465 }
1466 if (time_after(next_timer, fip->ctlr_ka_time))
1467 next_timer = fip->ctlr_ka_time;
1468
1469 if (time_after_eq(jiffies, fip->port_ka_time)) {
Bhanu Prakash Gollapudif47dd852010-01-21 10:16:00 -08001470 fip->port_ka_time = jiffies +
Robert Love70b51aa2009-11-03 11:47:45 -08001471 msecs_to_jiffies(FIP_VN_KA_PERIOD);
Joe Eykholtfdb068c2010-07-20 15:19:47 -07001472 send_port_ka = 1;
Joe Eykholt97c83892009-03-17 11:42:40 -07001473 }
1474 if (time_after(next_timer, fip->port_ka_time))
1475 next_timer = fip->port_ka_time;
Joe Eykholt97c83892009-03-17 11:42:40 -07001476 }
Joe Eykholt8690cb82010-06-11 16:44:10 -07001477 if (!list_empty(&fip->fcfs))
1478 mod_timer(&fip->timer, next_timer);
Joe Eykholtfdb068c2010-07-20 15:19:47 -07001479 mutex_unlock(&fip->ctlr_mutex);
Joe Eykholt97c83892009-03-17 11:42:40 -07001480
Bhanu Prakash Gollapudi516a6482010-06-11 16:43:44 -07001481 if (reset) {
Joe Eykholtdd42dac2009-11-03 11:48:27 -08001482 fc_lport_reset(fip->lp);
Bhanu Prakash Gollapudi516a6482010-06-11 16:43:44 -07001483 /* restart things with a solicitation */
1484 fcoe_ctlr_solicit(fip, NULL);
1485 }
Chris Leech11b56182009-11-03 11:46:29 -08001486
Joe Eykholtfdb068c2010-07-20 15:19:47 -07001487 if (send_ctlr_ka)
Chris Leech11b56182009-11-03 11:46:29 -08001488 fcoe_ctlr_send_keep_alive(fip, NULL, 0, fip->ctl_src_addr);
Joe Eykholtfdb068c2010-07-20 15:19:47 -07001489
1490 if (send_port_ka) {
Chris Leech11b56182009-11-03 11:46:29 -08001491 mutex_lock(&fip->lp->lp_mutex);
1492 mac = fip->get_src_addr(fip->lp);
1493 fcoe_ctlr_send_keep_alive(fip, fip->lp, 1, mac);
1494 list_for_each_entry(vport, &fip->lp->vports, list) {
1495 mac = fip->get_src_addr(vport);
1496 fcoe_ctlr_send_keep_alive(fip, vport, 1, mac);
1497 }
1498 mutex_unlock(&fip->lp->lp_mutex);
1499 }
Joe Eykholt97c83892009-03-17 11:42:40 -07001500}
1501
1502/**
Robert Love70b51aa2009-11-03 11:47:45 -08001503 * fcoe_ctlr_recv_work() - Worker thread function for receiving FIP frames
1504 * @recv_work: Handle to a FCoE controller
Joe Eykholt97c83892009-03-17 11:42:40 -07001505 */
1506static void fcoe_ctlr_recv_work(struct work_struct *recv_work)
1507{
1508 struct fcoe_ctlr *fip;
1509 struct sk_buff *skb;
1510
1511 fip = container_of(recv_work, struct fcoe_ctlr, recv_work);
Joe Eykholt1f4aed82009-11-03 11:48:22 -08001512 while ((skb = skb_dequeue(&fip->fip_recv_list)))
Joe Eykholt97c83892009-03-17 11:42:40 -07001513 fcoe_ctlr_recv_handler(fip, skb);
Joe Eykholt97c83892009-03-17 11:42:40 -07001514}
1515
1516/**
Joe Eykholt386309ce2009-11-03 11:49:16 -08001517 * fcoe_ctlr_recv_flogi() - Snoop pre-FIP receipt of FLOGI response
Robert Love70b51aa2009-11-03 11:47:45 -08001518 * @fip: The FCoE controller
1519 * @fp: The FC frame to snoop
Joe Eykholt97c83892009-03-17 11:42:40 -07001520 *
1521 * Snoop potential response to FLOGI or even incoming FLOGI.
1522 *
1523 * The caller has checked that we are waiting for login as indicated
1524 * by fip->flogi_oxid != FC_XID_UNKNOWN.
1525 *
1526 * The caller is responsible for freeing the frame.
Joe Eykholt386309ce2009-11-03 11:49:16 -08001527 * Fill in the granted_mac address.
Joe Eykholt97c83892009-03-17 11:42:40 -07001528 *
1529 * Return non-zero if the frame should not be delivered to libfc.
1530 */
Chris Leech11b56182009-11-03 11:46:29 -08001531int fcoe_ctlr_recv_flogi(struct fcoe_ctlr *fip, struct fc_lport *lport,
Joe Eykholt386309ce2009-11-03 11:49:16 -08001532 struct fc_frame *fp)
Joe Eykholt97c83892009-03-17 11:42:40 -07001533{
1534 struct fc_frame_header *fh;
1535 u8 op;
Joe Eykholt386309ce2009-11-03 11:49:16 -08001536 u8 *sa;
Joe Eykholt97c83892009-03-17 11:42:40 -07001537
Joe Eykholt386309ce2009-11-03 11:49:16 -08001538 sa = eth_hdr(&fp->skb)->h_source;
Joe Eykholt97c83892009-03-17 11:42:40 -07001539 fh = fc_frame_header_get(fp);
1540 if (fh->fh_type != FC_TYPE_ELS)
1541 return 0;
1542
1543 op = fc_frame_payload_op(fp);
1544 if (op == ELS_LS_ACC && fh->fh_r_ctl == FC_RCTL_ELS_REP &&
1545 fip->flogi_oxid == ntohs(fh->fh_ox_id)) {
1546
Joe Eykholtfdb068c2010-07-20 15:19:47 -07001547 mutex_lock(&fip->ctlr_mutex);
Joe Eykholt97c83892009-03-17 11:42:40 -07001548 if (fip->state != FIP_ST_AUTO && fip->state != FIP_ST_NON_FIP) {
Joe Eykholtfdb068c2010-07-20 15:19:47 -07001549 mutex_unlock(&fip->ctlr_mutex);
Joe Eykholt97c83892009-03-17 11:42:40 -07001550 return -EINVAL;
1551 }
Joe Eykholt9b651da2010-07-20 15:20:24 -07001552 fcoe_ctlr_set_state(fip, FIP_ST_NON_FIP);
Joe Eykholt0f51c2e2009-11-03 11:48:16 -08001553 LIBFCOE_FIP_DBG(fip,
1554 "received FLOGI LS_ACC using non-FIP mode\n");
Joe Eykholt97c83892009-03-17 11:42:40 -07001555
1556 /*
1557 * FLOGI accepted.
1558 * If the src mac addr is FC_OUI-based, then we mark the
1559 * address_mode flag to use FC_OUI-based Ethernet DA.
1560 * Otherwise we use the FCoE gateway addr
1561 */
1562 if (!compare_ether_addr(sa, (u8[6])FC_FCOE_FLOGI_MAC)) {
Joe Eykholtcd229e42010-07-20 15:20:40 -07001563 fcoe_ctlr_map_dest(fip);
Joe Eykholt97c83892009-03-17 11:42:40 -07001564 } else {
1565 memcpy(fip->dest_addr, sa, ETH_ALEN);
1566 fip->map_dest = 0;
1567 }
1568 fip->flogi_oxid = FC_XID_UNKNOWN;
Joe Eykholtfdb068c2010-07-20 15:19:47 -07001569 mutex_unlock(&fip->ctlr_mutex);
Joe Eykholt386309ce2009-11-03 11:49:16 -08001570 fc_fcoe_set_mac(fr_cb(fp)->granted_mac, fh->fh_d_id);
Joe Eykholt97c83892009-03-17 11:42:40 -07001571 } else if (op == ELS_FLOGI && fh->fh_r_ctl == FC_RCTL_ELS_REQ && sa) {
1572 /*
1573 * Save source MAC for point-to-point responses.
1574 */
Joe Eykholtfdb068c2010-07-20 15:19:47 -07001575 mutex_lock(&fip->ctlr_mutex);
Joe Eykholt97c83892009-03-17 11:42:40 -07001576 if (fip->state == FIP_ST_AUTO || fip->state == FIP_ST_NON_FIP) {
1577 memcpy(fip->dest_addr, sa, ETH_ALEN);
1578 fip->map_dest = 0;
Joe Eykholte49bf612010-03-12 16:07:57 -08001579 if (fip->state == FIP_ST_AUTO)
1580 LIBFCOE_FIP_DBG(fip, "received non-FIP FLOGI. "
1581 "Setting non-FIP mode\n");
Joe Eykholt9b651da2010-07-20 15:20:24 -07001582 fcoe_ctlr_set_state(fip, FIP_ST_NON_FIP);
Joe Eykholt97c83892009-03-17 11:42:40 -07001583 }
Joe Eykholtfdb068c2010-07-20 15:19:47 -07001584 mutex_unlock(&fip->ctlr_mutex);
Joe Eykholt97c83892009-03-17 11:42:40 -07001585 }
1586 return 0;
1587}
1588EXPORT_SYMBOL(fcoe_ctlr_recv_flogi);
1589
Vasu Dev5e80f7f2009-03-17 11:42:18 -07001590/**
Robert Love70b51aa2009-11-03 11:47:45 -08001591 * fcoe_wwn_from_mac() - Converts a 48-bit IEEE MAC address to a 64-bit FC WWN
1592 * @mac: The MAC address to convert
1593 * @scheme: The scheme to use when converting
1594 * @port: The port indicator for converting
Vasu Dev5e80f7f2009-03-17 11:42:18 -07001595 *
1596 * Returns: u64 fc world wide name
1597 */
1598u64 fcoe_wwn_from_mac(unsigned char mac[MAX_ADDR_LEN],
1599 unsigned int scheme, unsigned int port)
1600{
1601 u64 wwn;
1602 u64 host_mac;
1603
1604 /* The MAC is in NO, so flip only the low 48 bits */
1605 host_mac = ((u64) mac[0] << 40) |
1606 ((u64) mac[1] << 32) |
1607 ((u64) mac[2] << 24) |
1608 ((u64) mac[3] << 16) |
1609 ((u64) mac[4] << 8) |
1610 (u64) mac[5];
1611
1612 WARN_ON(host_mac >= (1ULL << 48));
1613 wwn = host_mac | ((u64) scheme << 60);
1614 switch (scheme) {
1615 case 1:
1616 WARN_ON(port != 0);
1617 break;
1618 case 2:
1619 WARN_ON(port >= 0xfff);
1620 wwn |= (u64) port << 48;
1621 break;
1622 default:
1623 WARN_ON(1);
1624 break;
1625 }
1626
1627 return wwn;
1628}
1629EXPORT_SYMBOL_GPL(fcoe_wwn_from_mac);
1630
1631/**
Joe Eykholte10f8c62010-07-20 15:20:30 -07001632 * fcoe_ctlr_rport() - return the fcoe_rport for a given fc_rport_priv
1633 * @rdata: libfc remote port
1634 */
1635static inline struct fcoe_rport *fcoe_ctlr_rport(struct fc_rport_priv *rdata)
1636{
1637 return (struct fcoe_rport *)(rdata + 1);
1638}
1639
1640/**
1641 * fcoe_ctlr_vn_send() - Send a FIP VN2VN Probe Request or Reply.
1642 * @fip: The FCoE controller
1643 * @sub: sub-opcode for probe request, reply, or advertisement.
1644 * @dest: The destination Ethernet MAC address
1645 * @min_len: minimum size of the Ethernet payload to be sent
1646 */
1647static void fcoe_ctlr_vn_send(struct fcoe_ctlr *fip,
1648 enum fip_vn2vn_subcode sub,
1649 const u8 *dest, size_t min_len)
1650{
1651 struct sk_buff *skb;
1652 struct fip_frame {
1653 struct ethhdr eth;
1654 struct fip_header fip;
1655 struct fip_mac_desc mac;
1656 struct fip_wwn_desc wwnn;
1657 struct fip_vn_desc vn;
1658 } __attribute__((packed)) *frame;
1659 struct fip_fc4_feat *ff;
1660 struct fip_size_desc *size;
1661 u32 fcp_feat;
1662 size_t len;
1663 size_t dlen;
1664
1665 len = sizeof(*frame);
1666 dlen = 0;
1667 if (sub == FIP_SC_VN_CLAIM_NOTIFY || sub == FIP_SC_VN_CLAIM_REP) {
1668 dlen = sizeof(struct fip_fc4_feat) +
1669 sizeof(struct fip_size_desc);
1670 len += dlen;
1671 }
1672 dlen += sizeof(frame->mac) + sizeof(frame->wwnn) + sizeof(frame->vn);
1673 len = max(len, min_len + sizeof(struct ethhdr));
1674
1675 skb = dev_alloc_skb(len);
1676 if (!skb)
1677 return;
1678
1679 frame = (struct fip_frame *)skb->data;
1680 memset(frame, 0, len);
1681 memcpy(frame->eth.h_dest, dest, ETH_ALEN);
1682 memcpy(frame->eth.h_source, fip->ctl_src_addr, ETH_ALEN);
1683 frame->eth.h_proto = htons(ETH_P_FIP);
1684
1685 frame->fip.fip_ver = FIP_VER_ENCAPS(FIP_VER);
1686 frame->fip.fip_op = htons(FIP_OP_VN2VN);
1687 frame->fip.fip_subcode = sub;
1688 frame->fip.fip_dl_len = htons(dlen / FIP_BPW);
1689
1690 frame->mac.fd_desc.fip_dtype = FIP_DT_MAC;
1691 frame->mac.fd_desc.fip_dlen = sizeof(frame->mac) / FIP_BPW;
1692 memcpy(frame->mac.fd_mac, fip->ctl_src_addr, ETH_ALEN);
1693
1694 frame->wwnn.fd_desc.fip_dtype = FIP_DT_NAME;
1695 frame->wwnn.fd_desc.fip_dlen = sizeof(frame->wwnn) / FIP_BPW;
1696 put_unaligned_be64(fip->lp->wwnn, &frame->wwnn.fd_wwn);
1697
1698 frame->vn.fd_desc.fip_dtype = FIP_DT_VN_ID;
1699 frame->vn.fd_desc.fip_dlen = sizeof(frame->vn) / FIP_BPW;
1700 hton24(frame->vn.fd_mac, FIP_VN_FC_MAP);
1701 hton24(frame->vn.fd_mac + 3, fip->port_id);
1702 hton24(frame->vn.fd_fc_id, fip->port_id);
1703 put_unaligned_be64(fip->lp->wwpn, &frame->vn.fd_wwpn);
1704
1705 /*
1706 * For claims, add FC-4 features.
1707 * TBD: Add interface to get fc-4 types and features from libfc.
1708 */
1709 if (sub == FIP_SC_VN_CLAIM_NOTIFY || sub == FIP_SC_VN_CLAIM_REP) {
1710 ff = (struct fip_fc4_feat *)(frame + 1);
1711 ff->fd_desc.fip_dtype = FIP_DT_FC4F;
1712 ff->fd_desc.fip_dlen = sizeof(*ff) / FIP_BPW;
1713 ff->fd_fts = fip->lp->fcts;
1714
1715 fcp_feat = 0;
1716 if (fip->lp->service_params & FCP_SPPF_INIT_FCN)
1717 fcp_feat |= FCP_FEAT_INIT;
1718 if (fip->lp->service_params & FCP_SPPF_TARG_FCN)
1719 fcp_feat |= FCP_FEAT_TARG;
1720 fcp_feat <<= (FC_TYPE_FCP * 4) % 32;
1721 ff->fd_ff.fd_feat[FC_TYPE_FCP * 4 / 32] = htonl(fcp_feat);
1722
1723 size = (struct fip_size_desc *)(ff + 1);
1724 size->fd_desc.fip_dtype = FIP_DT_FCOE_SIZE;
1725 size->fd_desc.fip_dlen = sizeof(*size) / FIP_BPW;
1726 size->fd_size = htons(fcoe_ctlr_fcoe_size(fip));
1727 }
1728
1729 skb_put(skb, len);
1730 skb->protocol = htons(ETH_P_FIP);
1731 skb_reset_mac_header(skb);
1732 skb_reset_network_header(skb);
1733
1734 fip->send(fip, skb);
1735}
1736
1737/**
1738 * fcoe_ctlr_vn_rport_callback - Event handler for rport events.
1739 * @lport: The lport which is receiving the event
1740 * @rdata: remote port private data
1741 * @event: The event that occured
1742 *
1743 * Locking Note: The rport lock must not be held when calling this function.
1744 */
1745static void fcoe_ctlr_vn_rport_callback(struct fc_lport *lport,
1746 struct fc_rport_priv *rdata,
1747 enum fc_rport_event event)
1748{
1749 struct fcoe_ctlr *fip = lport->disc.priv;
1750 struct fcoe_rport *frport = fcoe_ctlr_rport(rdata);
1751
1752 LIBFCOE_FIP_DBG(fip, "vn_rport_callback %x event %d\n",
1753 rdata->ids.port_id, event);
1754
1755 mutex_lock(&fip->ctlr_mutex);
1756 switch (event) {
1757 case RPORT_EV_READY:
1758 frport->login_count = 0;
1759 break;
1760 case RPORT_EV_LOGO:
1761 case RPORT_EV_FAILED:
1762 case RPORT_EV_STOP:
1763 frport->login_count++;
1764 if (frport->login_count > FCOE_CTLR_VN2VN_LOGIN_LIMIT) {
1765 LIBFCOE_FIP_DBG(fip,
1766 "rport FLOGI limited port_id %6.6x\n",
1767 rdata->ids.port_id);
1768 lport->tt.rport_logoff(rdata);
1769 }
1770 break;
1771 default:
1772 break;
1773 }
1774 mutex_unlock(&fip->ctlr_mutex);
1775}
1776
1777static struct fc_rport_operations fcoe_ctlr_vn_rport_ops = {
1778 .event_callback = fcoe_ctlr_vn_rport_callback,
1779};
1780
1781/**
1782 * fcoe_ctlr_disc_stop_locked() - stop discovery in VN2VN mode
1783 * @fip: The FCoE controller
1784 *
1785 * Called with ctlr_mutex held.
1786 */
1787static void fcoe_ctlr_disc_stop_locked(struct fc_lport *lport)
1788{
1789 mutex_lock(&lport->disc.disc_mutex);
1790 lport->disc.disc_callback = NULL;
1791 mutex_unlock(&lport->disc.disc_mutex);
1792}
1793
1794/**
1795 * fcoe_ctlr_disc_stop() - stop discovery in VN2VN mode
1796 * @fip: The FCoE controller
1797 *
1798 * Called through the local port template for discovery.
1799 * Called without the ctlr_mutex held.
1800 */
1801static void fcoe_ctlr_disc_stop(struct fc_lport *lport)
1802{
1803 struct fcoe_ctlr *fip = lport->disc.priv;
1804
1805 mutex_lock(&fip->ctlr_mutex);
1806 fcoe_ctlr_disc_stop_locked(lport);
1807 mutex_unlock(&fip->ctlr_mutex);
1808}
1809
1810/**
1811 * fcoe_ctlr_disc_stop_final() - stop discovery for shutdown in VN2VN mode
1812 * @fip: The FCoE controller
1813 *
1814 * Called through the local port template for discovery.
1815 * Called without the ctlr_mutex held.
1816 */
1817static void fcoe_ctlr_disc_stop_final(struct fc_lport *lport)
1818{
1819 fcoe_ctlr_disc_stop(lport);
1820 lport->tt.rport_flush_queue();
1821 synchronize_rcu();
1822}
1823
1824/**
1825 * fcoe_ctlr_vn_restart() - VN2VN probe restart with new port_id
1826 * @fip: The FCoE controller
1827 *
1828 * Called with fcoe_ctlr lock held.
1829 */
1830static void fcoe_ctlr_vn_restart(struct fcoe_ctlr *fip)
1831{
1832 unsigned long wait;
1833 u32 port_id;
1834
1835 fcoe_ctlr_disc_stop_locked(fip->lp);
1836
1837 /*
1838 * Get proposed port ID.
1839 * If this is the first try after link up, use any previous port_id.
1840 * If there was none, use the low bits of the port_name.
1841 * On subsequent tries, get the next random one.
1842 * Don't use reserved IDs, use another non-zero value, just as random.
1843 */
1844 port_id = fip->port_id;
1845 if (fip->probe_tries)
1846 port_id = prandom32(&fip->rnd_state) & 0xffff;
1847 else if (!port_id)
1848 port_id = fip->lp->wwpn & 0xffff;
1849 if (!port_id || port_id == 0xffff)
1850 port_id = 1;
1851 fip->port_id = port_id;
1852
1853 if (fip->probe_tries < FIP_VN_RLIM_COUNT) {
1854 fip->probe_tries++;
1855 wait = random32() % FIP_VN_PROBE_WAIT;
1856 } else
1857 wait = FIP_VN_RLIM_INT;
1858 mod_timer(&fip->timer, jiffies + msecs_to_jiffies(wait));
1859 fcoe_ctlr_set_state(fip, FIP_ST_VNMP_START);
1860}
1861
1862/**
1863 * fcoe_ctlr_vn_start() - Start in VN2VN mode
1864 * @fip: The FCoE controller
1865 *
1866 * Called with fcoe_ctlr lock held.
1867 */
1868static void fcoe_ctlr_vn_start(struct fcoe_ctlr *fip)
1869{
1870 fip->probe_tries = 0;
1871 prandom32_seed(&fip->rnd_state, fip->lp->wwpn);
1872 fcoe_ctlr_vn_restart(fip);
1873}
1874
1875/**
1876 * fcoe_ctlr_vn_parse - parse probe request or response
1877 * @fip: The FCoE controller
1878 * @skb: incoming packet
1879 * @rdata: buffer for resulting parsed VN entry plus fcoe_rport
1880 *
1881 * Returns non-zero error number on error.
1882 * Does not consume the packet.
1883 */
1884static int fcoe_ctlr_vn_parse(struct fcoe_ctlr *fip,
1885 struct sk_buff *skb,
1886 struct fc_rport_priv *rdata)
1887{
1888 struct fip_header *fiph;
1889 struct fip_desc *desc = NULL;
1890 struct fip_mac_desc *macd = NULL;
1891 struct fip_wwn_desc *wwn = NULL;
1892 struct fip_vn_desc *vn = NULL;
1893 struct fip_size_desc *size = NULL;
1894 struct fcoe_rport *frport;
1895 size_t rlen;
1896 size_t dlen;
1897 u32 desc_mask = 0;
1898 u32 dtype;
1899 u8 sub;
1900
1901 memset(rdata, 0, sizeof(*rdata) + sizeof(*frport));
1902 frport = fcoe_ctlr_rport(rdata);
1903
1904 fiph = (struct fip_header *)skb->data;
1905 frport->flags = ntohs(fiph->fip_flags);
1906
1907 sub = fiph->fip_subcode;
1908 switch (sub) {
1909 case FIP_SC_VN_PROBE_REQ:
1910 case FIP_SC_VN_PROBE_REP:
1911 case FIP_SC_VN_BEACON:
1912 desc_mask = BIT(FIP_DT_MAC) | BIT(FIP_DT_NAME) |
1913 BIT(FIP_DT_VN_ID);
1914 break;
1915 case FIP_SC_VN_CLAIM_NOTIFY:
1916 case FIP_SC_VN_CLAIM_REP:
1917 desc_mask = BIT(FIP_DT_MAC) | BIT(FIP_DT_NAME) |
1918 BIT(FIP_DT_VN_ID) | BIT(FIP_DT_FC4F) |
1919 BIT(FIP_DT_FCOE_SIZE);
1920 break;
1921 default:
1922 LIBFCOE_FIP_DBG(fip, "vn_parse unknown subcode %u\n", sub);
1923 return -EINVAL;
1924 }
1925
1926 rlen = ntohs(fiph->fip_dl_len) * 4;
1927 if (rlen + sizeof(*fiph) > skb->len)
1928 return -EINVAL;
1929
1930 desc = (struct fip_desc *)(fiph + 1);
1931 while (rlen > 0) {
1932 dlen = desc->fip_dlen * FIP_BPW;
1933 if (dlen < sizeof(*desc) || dlen > rlen)
1934 return -EINVAL;
1935
1936 dtype = desc->fip_dtype;
1937 if (dtype < 32) {
1938 if (!(desc_mask & BIT(dtype))) {
1939 LIBFCOE_FIP_DBG(fip,
1940 "unexpected or duplicated desc "
1941 "desc type %u in "
1942 "FIP VN2VN subtype %u\n",
1943 dtype, sub);
1944 return -EINVAL;
1945 }
1946 desc_mask &= ~BIT(dtype);
1947 }
1948
1949 switch (dtype) {
1950 case FIP_DT_MAC:
1951 if (dlen != sizeof(struct fip_mac_desc))
1952 goto len_err;
1953 macd = (struct fip_mac_desc *)desc;
1954 if (!is_valid_ether_addr(macd->fd_mac)) {
1955 LIBFCOE_FIP_DBG(fip,
1956 "Invalid MAC addr %pM in FIP VN2VN\n",
1957 macd->fd_mac);
1958 return -EINVAL;
1959 }
1960 memcpy(frport->enode_mac, macd->fd_mac, ETH_ALEN);
1961 break;
1962 case FIP_DT_NAME:
1963 if (dlen != sizeof(struct fip_wwn_desc))
1964 goto len_err;
1965 wwn = (struct fip_wwn_desc *)desc;
1966 rdata->ids.node_name = get_unaligned_be64(&wwn->fd_wwn);
1967 break;
1968 case FIP_DT_VN_ID:
1969 if (dlen != sizeof(struct fip_vn_desc))
1970 goto len_err;
1971 vn = (struct fip_vn_desc *)desc;
1972 memcpy(frport->vn_mac, vn->fd_mac, ETH_ALEN);
1973 rdata->ids.port_id = ntoh24(vn->fd_fc_id);
1974 rdata->ids.port_name = get_unaligned_be64(&vn->fd_wwpn);
1975 break;
1976 case FIP_DT_FC4F:
1977 if (dlen != sizeof(struct fip_fc4_feat))
1978 goto len_err;
1979 break;
1980 case FIP_DT_FCOE_SIZE:
1981 if (dlen != sizeof(struct fip_size_desc))
1982 goto len_err;
1983 size = (struct fip_size_desc *)desc;
1984 frport->fcoe_len = ntohs(size->fd_size);
1985 break;
1986 default:
1987 LIBFCOE_FIP_DBG(fip, "unexpected descriptor type %x "
1988 "in FIP probe\n", dtype);
1989 /* standard says ignore unknown descriptors >= 128 */
1990 if (dtype < FIP_DT_VENDOR_BASE)
1991 return -EINVAL;
1992 break;
1993 }
1994 desc = (struct fip_desc *)((char *)desc + dlen);
1995 rlen -= dlen;
1996 }
1997 return 0;
1998
1999len_err:
2000 LIBFCOE_FIP_DBG(fip, "FIP length error in descriptor type %x len %zu\n",
2001 dtype, dlen);
2002 return -EINVAL;
2003}
2004
2005/**
2006 * fcoe_ctlr_vn_send_claim() - send multicast FIP VN2VN Claim Notification.
2007 * @fip: The FCoE controller
2008 *
2009 * Called with ctlr_mutex held.
2010 */
2011static void fcoe_ctlr_vn_send_claim(struct fcoe_ctlr *fip)
2012{
2013 fcoe_ctlr_vn_send(fip, FIP_SC_VN_CLAIM_NOTIFY, fcoe_all_vn2vn, 0);
2014 fip->sol_time = jiffies;
2015}
2016
2017/**
2018 * fcoe_ctlr_vn_probe_req() - handle incoming VN2VN probe request.
2019 * @fip: The FCoE controller
2020 * @rdata: parsed remote port with frport from the probe request
2021 *
2022 * Called with ctlr_mutex held.
2023 */
2024static void fcoe_ctlr_vn_probe_req(struct fcoe_ctlr *fip,
2025 struct fc_rport_priv *rdata)
2026{
2027 struct fcoe_rport *frport = fcoe_ctlr_rport(rdata);
2028
2029 if (rdata->ids.port_id != fip->port_id)
2030 return;
2031
2032 switch (fip->state) {
2033 case FIP_ST_VNMP_CLAIM:
2034 case FIP_ST_VNMP_UP:
2035 fcoe_ctlr_vn_send(fip, FIP_SC_VN_PROBE_REP,
2036 frport->enode_mac, 0);
2037 break;
2038 case FIP_ST_VNMP_PROBE1:
2039 case FIP_ST_VNMP_PROBE2:
2040 /*
2041 * Decide whether to reply to the Probe.
2042 * Our selected address is never a "recorded" one, so
2043 * only reply if our WWPN is greater and the
2044 * Probe's REC bit is not set.
2045 * If we don't reply, we will change our address.
2046 */
2047 if (fip->lp->wwpn > rdata->ids.port_name &&
2048 !(frport->flags & FIP_FL_REC_OR_P2P)) {
2049 fcoe_ctlr_vn_send(fip, FIP_SC_VN_PROBE_REP,
2050 frport->enode_mac, 0);
2051 break;
2052 }
2053 /* fall through */
2054 case FIP_ST_VNMP_START:
2055 fcoe_ctlr_vn_restart(fip);
2056 break;
2057 default:
2058 break;
2059 }
2060}
2061
2062/**
2063 * fcoe_ctlr_vn_probe_reply() - handle incoming VN2VN probe reply.
2064 * @fip: The FCoE controller
2065 * @rdata: parsed remote port with frport from the probe request
2066 *
2067 * Called with ctlr_mutex held.
2068 */
2069static void fcoe_ctlr_vn_probe_reply(struct fcoe_ctlr *fip,
2070 struct fc_rport_priv *rdata)
2071{
2072 if (rdata->ids.port_id != fip->port_id)
2073 return;
2074 switch (fip->state) {
2075 case FIP_ST_VNMP_START:
2076 case FIP_ST_VNMP_PROBE1:
2077 case FIP_ST_VNMP_PROBE2:
2078 case FIP_ST_VNMP_CLAIM:
2079 fcoe_ctlr_vn_restart(fip);
2080 break;
2081 case FIP_ST_VNMP_UP:
2082 fcoe_ctlr_vn_send_claim(fip);
2083 break;
2084 default:
2085 break;
2086 }
2087}
2088
2089/**
2090 * fcoe_ctlr_vn_add() - Add a VN2VN entry to the list, based on a claim reply.
2091 * @fip: The FCoE controller
2092 * @new: newly-parsed remote port with frport as a template for new rdata
2093 *
2094 * Called with ctlr_mutex held.
2095 */
2096static void fcoe_ctlr_vn_add(struct fcoe_ctlr *fip, struct fc_rport_priv *new)
2097{
2098 struct fc_lport *lport = fip->lp;
2099 struct fc_rport_priv *rdata;
2100 struct fc_rport_identifiers *ids;
2101 struct fcoe_rport *frport;
2102 u32 port_id;
2103
2104 port_id = new->ids.port_id;
2105 if (port_id == fip->port_id)
2106 return;
2107
2108 mutex_lock(&lport->disc.disc_mutex);
2109 rdata = lport->tt.rport_create(lport, port_id);
2110 if (!rdata) {
2111 mutex_unlock(&lport->disc.disc_mutex);
2112 return;
2113 }
2114
2115 rdata->ops = &fcoe_ctlr_vn_rport_ops;
2116 rdata->disc_id = lport->disc.disc_id;
2117
2118 ids = &rdata->ids;
2119 if ((ids->port_name != -1 && ids->port_name != new->ids.port_name) ||
2120 (ids->node_name != -1 && ids->node_name != new->ids.node_name))
2121 lport->tt.rport_logoff(rdata);
2122 ids->port_name = new->ids.port_name;
2123 ids->node_name = new->ids.node_name;
2124 mutex_unlock(&lport->disc.disc_mutex);
2125
2126 frport = fcoe_ctlr_rport(rdata);
2127 LIBFCOE_FIP_DBG(fip, "vn_add rport %6.6x %s\n",
2128 port_id, frport->fcoe_len ? "old" : "new");
2129 *frport = *fcoe_ctlr_rport(new);
2130 frport->time = 0;
2131}
2132
2133/**
2134 * fcoe_ctlr_vn_lookup() - Find VN remote port's MAC address
2135 * @fip: The FCoE controller
2136 * @port_id: The port_id of the remote VN_node
2137 * @mac: buffer which will hold the VN_NODE destination MAC address, if found.
2138 *
2139 * Returns non-zero error if no remote port found.
2140 */
2141static int fcoe_ctlr_vn_lookup(struct fcoe_ctlr *fip, u32 port_id, u8 *mac)
2142{
2143 struct fc_lport *lport = fip->lp;
2144 struct fc_rport_priv *rdata;
2145 struct fcoe_rport *frport;
2146 int ret = -1;
2147
2148 rcu_read_lock();
2149 rdata = lport->tt.rport_lookup(lport, port_id);
2150 if (rdata) {
2151 frport = fcoe_ctlr_rport(rdata);
2152 memcpy(mac, frport->enode_mac, ETH_ALEN);
2153 ret = 0;
2154 }
2155 rcu_read_unlock();
2156 return ret;
2157}
2158
2159/**
2160 * fcoe_ctlr_vn_claim_notify() - handle received FIP VN2VN Claim Notification
2161 * @fip: The FCoE controller
2162 * @new: newly-parsed remote port with frport as a template for new rdata
2163 *
2164 * Called with ctlr_mutex held.
2165 */
2166static void fcoe_ctlr_vn_claim_notify(struct fcoe_ctlr *fip,
2167 struct fc_rport_priv *new)
2168{
2169 struct fcoe_rport *frport = fcoe_ctlr_rport(new);
2170
2171 if (frport->flags & FIP_FL_REC_OR_P2P) {
2172 fcoe_ctlr_vn_send(fip, FIP_SC_VN_PROBE_REQ, fcoe_all_vn2vn, 0);
2173 return;
2174 }
2175 switch (fip->state) {
2176 case FIP_ST_VNMP_START:
2177 case FIP_ST_VNMP_PROBE1:
2178 case FIP_ST_VNMP_PROBE2:
2179 if (new->ids.port_id == fip->port_id)
2180 fcoe_ctlr_vn_restart(fip);
2181 break;
2182 case FIP_ST_VNMP_CLAIM:
2183 case FIP_ST_VNMP_UP:
2184 if (new->ids.port_id == fip->port_id) {
2185 if (new->ids.port_name > fip->lp->wwpn) {
2186 fcoe_ctlr_vn_restart(fip);
2187 break;
2188 }
2189 fcoe_ctlr_vn_send_claim(fip);
2190 break;
2191 }
2192 fcoe_ctlr_vn_send(fip, FIP_SC_VN_CLAIM_REP, frport->enode_mac,
2193 min((u32)frport->fcoe_len,
2194 fcoe_ctlr_fcoe_size(fip)));
2195 fcoe_ctlr_vn_add(fip, new);
2196 break;
2197 default:
2198 break;
2199 }
2200}
2201
2202/**
2203 * fcoe_ctlr_vn_claim_resp() - handle received Claim Response
2204 * @fip: The FCoE controller that received the frame
2205 * @new: newly-parsed remote port with frport from the Claim Response
2206 *
2207 * Called with ctlr_mutex held.
2208 */
2209static void fcoe_ctlr_vn_claim_resp(struct fcoe_ctlr *fip,
2210 struct fc_rport_priv *new)
2211{
2212 LIBFCOE_FIP_DBG(fip, "claim resp from from rport %x - state %s\n",
2213 new->ids.port_id, fcoe_ctlr_state(fip->state));
2214 if (fip->state == FIP_ST_VNMP_UP || fip->state == FIP_ST_VNMP_CLAIM)
2215 fcoe_ctlr_vn_add(fip, new);
2216}
2217
2218/**
2219 * fcoe_ctlr_vn_beacon() - handle received beacon.
2220 * @fip: The FCoE controller that received the frame
2221 * @new: newly-parsed remote port with frport from the Beacon
2222 *
2223 * Called with ctlr_mutex held.
2224 */
2225static void fcoe_ctlr_vn_beacon(struct fcoe_ctlr *fip,
2226 struct fc_rport_priv *new)
2227{
2228 struct fc_lport *lport = fip->lp;
2229 struct fc_rport_priv *rdata;
2230 struct fcoe_rport *frport;
2231
2232 frport = fcoe_ctlr_rport(new);
2233 if (frport->flags & FIP_FL_REC_OR_P2P) {
2234 fcoe_ctlr_vn_send(fip, FIP_SC_VN_PROBE_REQ, fcoe_all_vn2vn, 0);
2235 return;
2236 }
2237 mutex_lock(&lport->disc.disc_mutex);
2238 rdata = lport->tt.rport_lookup(lport, new->ids.port_id);
2239 if (rdata)
2240 kref_get(&rdata->kref);
2241 mutex_unlock(&lport->disc.disc_mutex);
2242 if (rdata) {
2243 if (rdata->ids.node_name == new->ids.node_name &&
2244 rdata->ids.port_name == new->ids.port_name) {
2245 frport = fcoe_ctlr_rport(rdata);
2246 if (!frport->time && fip->state == FIP_ST_VNMP_UP)
2247 lport->tt.rport_login(rdata);
2248 frport->time = jiffies;
2249 }
2250 kref_put(&rdata->kref, lport->tt.rport_destroy);
2251 return;
2252 }
2253 if (fip->state != FIP_ST_VNMP_UP)
2254 return;
2255
2256 /*
2257 * Beacon from a new neighbor.
2258 * Send a claim notify if one hasn't been sent recently.
2259 * Don't add the neighbor yet.
2260 */
2261 LIBFCOE_FIP_DBG(fip, "beacon from new rport %x. sending claim notify\n",
2262 new->ids.port_id);
2263 if (time_after(jiffies,
2264 fip->sol_time + msecs_to_jiffies(FIP_VN_ANN_WAIT)))
2265 fcoe_ctlr_vn_send_claim(fip);
2266}
2267
2268/**
2269 * fcoe_ctlr_vn_age() - Check for VN_ports without recent beacons
2270 * @fip: The FCoE controller
2271 *
2272 * Called with ctlr_mutex held.
2273 * Called only in state FIP_ST_VNMP_UP.
2274 * Returns the soonest time for next age-out or a time far in the future.
2275 */
2276static unsigned long fcoe_ctlr_vn_age(struct fcoe_ctlr *fip)
2277{
2278 struct fc_lport *lport = fip->lp;
2279 struct fc_rport_priv *rdata;
2280 struct fcoe_rport *frport;
2281 unsigned long next_time;
2282 unsigned long deadline;
2283
2284 next_time = jiffies + msecs_to_jiffies(FIP_VN_BEACON_INT * 10);
2285 mutex_lock(&lport->disc.disc_mutex);
2286 list_for_each_entry_rcu(rdata, &lport->disc.rports, peers) {
2287 frport = fcoe_ctlr_rport(rdata);
2288 if (!frport->time)
2289 continue;
2290 deadline = frport->time +
2291 msecs_to_jiffies(FIP_VN_BEACON_INT * 25 / 10);
2292 if (time_after_eq(jiffies, deadline)) {
2293 frport->time = 0;
2294 LIBFCOE_FIP_DBG(fip,
2295 "port %16.16llx fc_id %6.6x beacon expired\n",
2296 rdata->ids.port_name, rdata->ids.port_id);
2297 lport->tt.rport_logoff(rdata);
2298 } else if (time_before(deadline, next_time))
2299 next_time = deadline;
2300 }
2301 mutex_unlock(&lport->disc.disc_mutex);
2302 return next_time;
2303}
2304
2305/**
2306 * fcoe_ctlr_vn_recv() - Receive a FIP frame
2307 * @fip: The FCoE controller that received the frame
2308 * @skb: The received FIP frame
2309 *
2310 * Returns non-zero if the frame is dropped.
2311 * Always consumes the frame.
2312 */
2313static int fcoe_ctlr_vn_recv(struct fcoe_ctlr *fip, struct sk_buff *skb)
2314{
2315 struct fip_header *fiph;
2316 enum fip_vn2vn_subcode sub;
Kiran Patil2dc02ee2010-10-08 17:12:41 -07002317 struct {
Joe Eykholte10f8c62010-07-20 15:20:30 -07002318 struct fc_rport_priv rdata;
2319 struct fcoe_rport frport;
2320 } buf;
2321 int rc;
2322
2323 fiph = (struct fip_header *)skb->data;
2324 sub = fiph->fip_subcode;
2325
2326 rc = fcoe_ctlr_vn_parse(fip, skb, &buf.rdata);
2327 if (rc) {
2328 LIBFCOE_FIP_DBG(fip, "vn_recv vn_parse error %d\n", rc);
2329 goto drop;
2330 }
2331
2332 mutex_lock(&fip->ctlr_mutex);
2333 switch (sub) {
2334 case FIP_SC_VN_PROBE_REQ:
2335 fcoe_ctlr_vn_probe_req(fip, &buf.rdata);
2336 break;
2337 case FIP_SC_VN_PROBE_REP:
2338 fcoe_ctlr_vn_probe_reply(fip, &buf.rdata);
2339 break;
2340 case FIP_SC_VN_CLAIM_NOTIFY:
2341 fcoe_ctlr_vn_claim_notify(fip, &buf.rdata);
2342 break;
2343 case FIP_SC_VN_CLAIM_REP:
2344 fcoe_ctlr_vn_claim_resp(fip, &buf.rdata);
2345 break;
2346 case FIP_SC_VN_BEACON:
2347 fcoe_ctlr_vn_beacon(fip, &buf.rdata);
2348 break;
2349 default:
2350 LIBFCOE_FIP_DBG(fip, "vn_recv unknown subcode %d\n", sub);
2351 rc = -1;
2352 break;
2353 }
2354 mutex_unlock(&fip->ctlr_mutex);
2355drop:
2356 kfree_skb(skb);
2357 return rc;
2358}
2359
2360/**
2361 * fcoe_ctlr_disc_recv - discovery receive handler for VN2VN mode.
Joe Eykholt922611562010-07-20 15:21:12 -07002362 * @lport: The local port
2363 * @fp: The received frame
Joe Eykholte10f8c62010-07-20 15:20:30 -07002364 *
2365 * This should never be called since we don't see RSCNs or other
2366 * fabric-generated ELSes.
2367 */
Joe Eykholt922611562010-07-20 15:21:12 -07002368static void fcoe_ctlr_disc_recv(struct fc_lport *lport, struct fc_frame *fp)
Joe Eykholte10f8c62010-07-20 15:20:30 -07002369{
2370 struct fc_seq_els_data rjt_data;
2371
Joe Eykholte10f8c62010-07-20 15:20:30 -07002372 rjt_data.reason = ELS_RJT_UNSUP;
2373 rjt_data.explan = ELS_EXPL_NONE;
Joe Eykholt922611562010-07-20 15:21:12 -07002374 lport->tt.seq_els_rsp_send(fp, ELS_LS_RJT, &rjt_data);
Joe Eykholte10f8c62010-07-20 15:20:30 -07002375 fc_frame_free(fp);
2376}
2377
2378/**
2379 * fcoe_ctlr_disc_recv - start discovery for VN2VN mode.
2380 * @fip: The FCoE controller
2381 *
2382 * This sets a flag indicating that remote ports should be created
2383 * and started for the peers we discover. We use the disc_callback
2384 * pointer as that flag. Peers already discovered are created here.
2385 *
2386 * The lport lock is held during this call. The callback must be done
2387 * later, without holding either the lport or discovery locks.
2388 * The fcoe_ctlr lock may also be held during this call.
2389 */
2390static void fcoe_ctlr_disc_start(void (*callback)(struct fc_lport *,
2391 enum fc_disc_event),
2392 struct fc_lport *lport)
2393{
2394 struct fc_disc *disc = &lport->disc;
2395 struct fcoe_ctlr *fip = disc->priv;
2396
2397 mutex_lock(&disc->disc_mutex);
2398 disc->disc_callback = callback;
2399 disc->disc_id = (disc->disc_id + 2) | 1;
2400 disc->pending = 1;
2401 schedule_work(&fip->timer_work);
2402 mutex_unlock(&disc->disc_mutex);
2403}
2404
2405/**
2406 * fcoe_ctlr_vn_disc() - report FIP VN_port discovery results after claim state.
2407 * @fip: The FCoE controller
2408 *
2409 * Starts the FLOGI and PLOGI login process to each discovered rport for which
2410 * we've received at least one beacon.
2411 * Performs the discovery complete callback.
2412 */
2413static void fcoe_ctlr_vn_disc(struct fcoe_ctlr *fip)
2414{
2415 struct fc_lport *lport = fip->lp;
2416 struct fc_disc *disc = &lport->disc;
2417 struct fc_rport_priv *rdata;
2418 struct fcoe_rport *frport;
2419 void (*callback)(struct fc_lport *, enum fc_disc_event);
2420
2421 mutex_lock(&disc->disc_mutex);
2422 callback = disc->pending ? disc->disc_callback : NULL;
2423 disc->pending = 0;
2424 list_for_each_entry_rcu(rdata, &disc->rports, peers) {
2425 frport = fcoe_ctlr_rport(rdata);
2426 if (frport->time)
2427 lport->tt.rport_login(rdata);
2428 }
2429 mutex_unlock(&disc->disc_mutex);
2430 if (callback)
2431 callback(lport, DISC_EV_SUCCESS);
2432}
2433
2434/**
2435 * fcoe_ctlr_vn_timeout - timer work function for VN2VN mode.
2436 * @fip: The FCoE controller
2437 */
2438static void fcoe_ctlr_vn_timeout(struct fcoe_ctlr *fip)
2439{
2440 unsigned long next_time;
2441 u8 mac[ETH_ALEN];
2442 u32 new_port_id = 0;
2443
2444 mutex_lock(&fip->ctlr_mutex);
2445 switch (fip->state) {
2446 case FIP_ST_VNMP_START:
2447 fcoe_ctlr_set_state(fip, FIP_ST_VNMP_PROBE1);
2448 fcoe_ctlr_vn_send(fip, FIP_SC_VN_PROBE_REQ, fcoe_all_vn2vn, 0);
2449 next_time = jiffies + msecs_to_jiffies(FIP_VN_PROBE_WAIT);
2450 break;
2451 case FIP_ST_VNMP_PROBE1:
2452 fcoe_ctlr_set_state(fip, FIP_ST_VNMP_PROBE2);
2453 fcoe_ctlr_vn_send(fip, FIP_SC_VN_PROBE_REQ, fcoe_all_vn2vn, 0);
2454 next_time = jiffies + msecs_to_jiffies(FIP_VN_ANN_WAIT);
2455 break;
2456 case FIP_ST_VNMP_PROBE2:
2457 fcoe_ctlr_set_state(fip, FIP_ST_VNMP_CLAIM);
2458 new_port_id = fip->port_id;
2459 hton24(mac, FIP_VN_FC_MAP);
2460 hton24(mac + 3, new_port_id);
Joe Eykholtcd229e42010-07-20 15:20:40 -07002461 fcoe_ctlr_map_dest(fip);
Joe Eykholte10f8c62010-07-20 15:20:30 -07002462 fip->update_mac(fip->lp, mac);
2463 fcoe_ctlr_vn_send_claim(fip);
2464 next_time = jiffies + msecs_to_jiffies(FIP_VN_ANN_WAIT);
2465 break;
2466 case FIP_ST_VNMP_CLAIM:
2467 /*
2468 * This may be invoked either by starting discovery so don't
2469 * go to the next state unless it's been long enough.
2470 */
2471 next_time = fip->sol_time + msecs_to_jiffies(FIP_VN_ANN_WAIT);
2472 if (time_after_eq(jiffies, next_time)) {
2473 fcoe_ctlr_set_state(fip, FIP_ST_VNMP_UP);
2474 fcoe_ctlr_vn_send(fip, FIP_SC_VN_BEACON,
2475 fcoe_all_vn2vn, 0);
2476 next_time = jiffies + msecs_to_jiffies(FIP_VN_ANN_WAIT);
2477 fip->port_ka_time = next_time;
2478 }
2479 fcoe_ctlr_vn_disc(fip);
2480 break;
2481 case FIP_ST_VNMP_UP:
2482 next_time = fcoe_ctlr_vn_age(fip);
2483 if (time_after_eq(jiffies, fip->port_ka_time)) {
2484 fcoe_ctlr_vn_send(fip, FIP_SC_VN_BEACON,
2485 fcoe_all_vn2vn, 0);
2486 fip->port_ka_time = jiffies +
2487 msecs_to_jiffies(FIP_VN_BEACON_INT +
2488 (random32() % FIP_VN_BEACON_FUZZ));
2489 }
2490 if (time_before(fip->port_ka_time, next_time))
2491 next_time = fip->port_ka_time;
2492 break;
2493 case FIP_ST_LINK_WAIT:
2494 goto unlock;
2495 default:
Joe Perches11aa9902010-11-30 16:19:09 -08002496 WARN(1, "unexpected state %d\n", fip->state);
Joe Eykholte10f8c62010-07-20 15:20:30 -07002497 goto unlock;
2498 }
2499 mod_timer(&fip->timer, next_time);
2500unlock:
2501 mutex_unlock(&fip->ctlr_mutex);
2502
2503 /* If port ID is new, notify local port after dropping ctlr_mutex */
2504 if (new_port_id)
2505 fc_lport_set_local_id(fip->lp, new_port_id);
2506}
2507
2508/**
Robert Love70b51aa2009-11-03 11:47:45 -08002509 * fcoe_libfc_config() - Sets up libfc related properties for local port
2510 * @lp: The local port to configure libfc for
Joe Eykholte10f8c62010-07-20 15:20:30 -07002511 * @fip: The FCoE controller in use by the local port
Robert Love70b51aa2009-11-03 11:47:45 -08002512 * @tt: The libfc function template
Joe Eykholte10f8c62010-07-20 15:20:30 -07002513 * @init_fcp: If non-zero, the FCP portion of libfc should be initialized
Vasu Dev5e80f7f2009-03-17 11:42:18 -07002514 *
2515 * Returns : 0 for success
2516 */
Joe Eykholte10f8c62010-07-20 15:20:30 -07002517int fcoe_libfc_config(struct fc_lport *lport, struct fcoe_ctlr *fip,
2518 const struct libfc_function_template *tt, int init_fcp)
Vasu Dev5e80f7f2009-03-17 11:42:18 -07002519{
2520 /* Set the function pointers set by the LLDD */
Robert Love70b51aa2009-11-03 11:47:45 -08002521 memcpy(&lport->tt, tt, sizeof(*tt));
Joe Eykholte10f8c62010-07-20 15:20:30 -07002522 if (init_fcp && fc_fcp_init(lport))
Vasu Dev5e80f7f2009-03-17 11:42:18 -07002523 return -ENOMEM;
Robert Love70b51aa2009-11-03 11:47:45 -08002524 fc_exch_init(lport);
2525 fc_elsct_init(lport);
2526 fc_lport_init(lport);
Joe Eykholte10f8c62010-07-20 15:20:30 -07002527 if (fip->mode == FIP_MODE_VN2VN)
2528 lport->rport_priv_size = sizeof(struct fcoe_rport);
Robert Love70b51aa2009-11-03 11:47:45 -08002529 fc_rport_init(lport);
Joe Eykholte10f8c62010-07-20 15:20:30 -07002530 if (fip->mode == FIP_MODE_VN2VN) {
2531 lport->point_to_multipoint = 1;
2532 lport->tt.disc_recv_req = fcoe_ctlr_disc_recv;
2533 lport->tt.disc_start = fcoe_ctlr_disc_start;
2534 lport->tt.disc_stop = fcoe_ctlr_disc_stop;
2535 lport->tt.disc_stop_final = fcoe_ctlr_disc_stop_final;
2536 mutex_init(&lport->disc.disc_mutex);
2537 INIT_LIST_HEAD(&lport->disc.rports);
2538 lport->disc.priv = fip;
2539 } else {
2540 fc_disc_init(lport);
2541 }
Vasu Dev5e80f7f2009-03-17 11:42:18 -07002542 return 0;
2543}
2544EXPORT_SYMBOL_GPL(fcoe_libfc_config);