blob: dcccb0bf3170a33fbb165e9d1d5d30baf6ee59bd [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 *);
Joe Eykholt794d98e2010-11-30 16:19:56 -080057static int fcoe_ctlr_flogi_retry(struct fcoe_ctlr *);
58static void fcoe_ctlr_select(struct fcoe_ctlr *);
Joe Eykholt97c83892009-03-17 11:42:40 -070059
Joe Eykholte10f8c62010-07-20 15:20:30 -070060static void fcoe_ctlr_vn_start(struct fcoe_ctlr *);
61static int fcoe_ctlr_vn_recv(struct fcoe_ctlr *, struct sk_buff *);
62static void fcoe_ctlr_vn_timeout(struct fcoe_ctlr *);
63static int fcoe_ctlr_vn_lookup(struct fcoe_ctlr *, u32, u8 *);
64
Joe Eykholt97c83892009-03-17 11:42:40 -070065static u8 fcoe_all_fcfs[ETH_ALEN] = FIP_ALL_FCF_MACS;
Joe Eykholte10f8c62010-07-20 15:20:30 -070066static u8 fcoe_all_enode[ETH_ALEN] = FIP_ALL_ENODE_MACS;
67static u8 fcoe_all_vn2vn[ETH_ALEN] = FIP_ALL_VN2VN_MACS;
68static u8 fcoe_all_p2p[ETH_ALEN] = FIP_ALL_P2P_MACS;
Joe Eykholt97c83892009-03-17 11:42:40 -070069
Robert Love650bd122009-06-10 15:31:05 -070070unsigned int libfcoe_debug_logging;
71module_param_named(debug_logging, libfcoe_debug_logging, int, S_IRUGO|S_IWUSR);
72MODULE_PARM_DESC(debug_logging, "a bit mask of logging levels");
Joe Eykholt97c83892009-03-17 11:42:40 -070073
Robert Love70b51aa2009-11-03 11:47:45 -080074#define LIBFCOE_LOGGING 0x01 /* General logging, not categorized */
Robert Love650bd122009-06-10 15:31:05 -070075#define LIBFCOE_FIP_LOGGING 0x02 /* FIP logging */
76
Robert Love70b51aa2009-11-03 11:47:45 -080077#define LIBFCOE_CHECK_LOGGING(LEVEL, CMD) \
78do { \
79 if (unlikely(libfcoe_debug_logging & LEVEL)) \
80 do { \
81 CMD; \
82 } while (0); \
Joe Eykholta69b06b2009-08-25 13:58:42 -070083} while (0)
Joe Eykholt97c83892009-03-17 11:42:40 -070084
Robert Love650bd122009-06-10 15:31:05 -070085#define LIBFCOE_DBG(fmt, args...) \
86 LIBFCOE_CHECK_LOGGING(LIBFCOE_LOGGING, \
87 printk(KERN_INFO "libfcoe: " fmt, ##args);)
88
Joe Eykholt0f51c2e2009-11-03 11:48:16 -080089#define LIBFCOE_FIP_DBG(fip, fmt, args...) \
Robert Love650bd122009-06-10 15:31:05 -070090 LIBFCOE_CHECK_LOGGING(LIBFCOE_FIP_LOGGING, \
Joe Eykholt0f51c2e2009-11-03 11:48:16 -080091 printk(KERN_INFO "host%d: fip: " fmt, \
92 (fip)->lp->host->host_no, ##args);)
Joe Eykholt97c83892009-03-17 11:42:40 -070093
Joe Eykholt9b651da2010-07-20 15:20:24 -070094static const char *fcoe_ctlr_states[] = {
95 [FIP_ST_DISABLED] = "DISABLED",
96 [FIP_ST_LINK_WAIT] = "LINK_WAIT",
97 [FIP_ST_AUTO] = "AUTO",
98 [FIP_ST_NON_FIP] = "NON_FIP",
99 [FIP_ST_ENABLED] = "ENABLED",
Joe Eykholte10f8c62010-07-20 15:20:30 -0700100 [FIP_ST_VNMP_START] = "VNMP_START",
101 [FIP_ST_VNMP_PROBE1] = "VNMP_PROBE1",
102 [FIP_ST_VNMP_PROBE2] = "VNMP_PROBE2",
103 [FIP_ST_VNMP_CLAIM] = "VNMP_CLAIM",
104 [FIP_ST_VNMP_UP] = "VNMP_UP",
Joe Eykholt9b651da2010-07-20 15:20:24 -0700105};
106
107static const char *fcoe_ctlr_state(enum fip_state state)
108{
109 const char *cp = "unknown";
110
111 if (state < ARRAY_SIZE(fcoe_ctlr_states))
112 cp = fcoe_ctlr_states[state];
113 if (!cp)
114 cp = "unknown";
115 return cp;
116}
117
118/**
119 * fcoe_ctlr_set_state() - Set and do debug printing for the new FIP state.
120 * @fip: The FCoE controller
121 * @state: The new state
122 */
123static void fcoe_ctlr_set_state(struct fcoe_ctlr *fip, enum fip_state state)
124{
125 if (state == fip->state)
126 return;
127 if (fip->lp)
128 LIBFCOE_FIP_DBG(fip, "state %s -> %s\n",
129 fcoe_ctlr_state(fip->state), fcoe_ctlr_state(state));
130 fip->state = state;
131}
132
Robert Love70b51aa2009-11-03 11:47:45 -0800133/**
134 * fcoe_ctlr_mtu_valid() - Check if a FCF's MTU is valid
135 * @fcf: The FCF to check
136 *
Joe Eykholt97c83892009-03-17 11:42:40 -0700137 * Return non-zero if FCF fcoe_size has been validated.
138 */
139static inline int fcoe_ctlr_mtu_valid(const struct fcoe_fcf *fcf)
140{
141 return (fcf->flags & FIP_FL_SOL) != 0;
142}
143
Robert Love70b51aa2009-11-03 11:47:45 -0800144/**
145 * fcoe_ctlr_fcf_usable() - Check if a FCF is usable
146 * @fcf: The FCF to check
147 *
Joe Eykholt97c83892009-03-17 11:42:40 -0700148 * Return non-zero if the FCF is usable.
149 */
150static inline int fcoe_ctlr_fcf_usable(struct fcoe_fcf *fcf)
151{
152 u16 flags = FIP_FL_SOL | FIP_FL_AVAIL;
153
154 return (fcf->flags & flags) == flags;
155}
156
157/**
Joe Eykholtcd229e42010-07-20 15:20:40 -0700158 * fcoe_ctlr_map_dest() - Set flag and OUI for mapping destination addresses
159 * @fip: The FCoE controller
160 */
161static void fcoe_ctlr_map_dest(struct fcoe_ctlr *fip)
162{
163 if (fip->mode == FIP_MODE_VN2VN)
164 hton24(fip->dest_addr, FIP_VN_FC_MAP);
165 else
166 hton24(fip->dest_addr, FIP_DEF_FC_MAP);
167 hton24(fip->dest_addr + 3, 0);
168 fip->map_dest = 1;
169}
170
171/**
Robert Love70b51aa2009-11-03 11:47:45 -0800172 * fcoe_ctlr_init() - Initialize the FCoE Controller instance
173 * @fip: The FCoE controller to initialize
Joe Eykholt97c83892009-03-17 11:42:40 -0700174 */
Joe Eykholt3d902ac2010-07-20 15:19:58 -0700175void fcoe_ctlr_init(struct fcoe_ctlr *fip, enum fip_state mode)
Joe Eykholt97c83892009-03-17 11:42:40 -0700176{
Joe Eykholt9b651da2010-07-20 15:20:24 -0700177 fcoe_ctlr_set_state(fip, FIP_ST_LINK_WAIT);
Joe Eykholt3d902ac2010-07-20 15:19:58 -0700178 fip->mode = mode;
Joe Eykholt97c83892009-03-17 11:42:40 -0700179 INIT_LIST_HEAD(&fip->fcfs);
Joe Eykholtfdb068c2010-07-20 15:19:47 -0700180 mutex_init(&fip->ctlr_mutex);
Joe Eykholt794d98e2010-11-30 16:19:56 -0800181 spin_lock_init(&fip->ctlr_lock);
Joe Eykholt97c83892009-03-17 11:42:40 -0700182 fip->flogi_oxid = FC_XID_UNKNOWN;
183 setup_timer(&fip->timer, fcoe_ctlr_timeout, (unsigned long)fip);
Joe Eykholt42913652010-03-12 16:08:23 -0800184 INIT_WORK(&fip->timer_work, fcoe_ctlr_timer_work);
Joe Eykholt97c83892009-03-17 11:42:40 -0700185 INIT_WORK(&fip->recv_work, fcoe_ctlr_recv_work);
186 skb_queue_head_init(&fip->fip_recv_list);
187}
188EXPORT_SYMBOL(fcoe_ctlr_init);
189
190/**
Robert Love70b51aa2009-11-03 11:47:45 -0800191 * fcoe_ctlr_reset_fcfs() - Reset and free all FCFs for a controller
192 * @fip: The FCoE controller whose FCFs are to be reset
Joe Eykholt97c83892009-03-17 11:42:40 -0700193 *
194 * Called with &fcoe_ctlr lock held.
195 */
196static void fcoe_ctlr_reset_fcfs(struct fcoe_ctlr *fip)
197{
198 struct fcoe_fcf *fcf;
199 struct fcoe_fcf *next;
200
201 fip->sel_fcf = NULL;
202 list_for_each_entry_safe(fcf, next, &fip->fcfs, list) {
203 list_del(&fcf->list);
204 kfree(fcf);
205 }
206 fip->fcf_count = 0;
207 fip->sel_time = 0;
208}
209
210/**
Robert Love70b51aa2009-11-03 11:47:45 -0800211 * fcoe_ctlr_destroy() - Disable and tear down a FCoE controller
212 * @fip: The FCoE controller to tear down
Joe Eykholt97c83892009-03-17 11:42:40 -0700213 *
214 * This is called by FCoE drivers before freeing the &fcoe_ctlr.
215 *
216 * The receive handler will have been deleted before this to guarantee
217 * that no more recv_work will be scheduled.
218 *
219 * The timer routine will simply return once we set FIP_ST_DISABLED.
220 * This guarantees that no further timeouts or work will be scheduled.
221 */
222void fcoe_ctlr_destroy(struct fcoe_ctlr *fip)
223{
Chris Leecha4b7cfa2009-08-25 13:59:08 -0700224 cancel_work_sync(&fip->recv_work);
Joe Eykholt1f4aed82009-11-03 11:48:22 -0800225 skb_queue_purge(&fip->fip_recv_list);
Chris Leecha4b7cfa2009-08-25 13:59:08 -0700226
Joe Eykholtfdb068c2010-07-20 15:19:47 -0700227 mutex_lock(&fip->ctlr_mutex);
Joe Eykholt9b651da2010-07-20 15:20:24 -0700228 fcoe_ctlr_set_state(fip, FIP_ST_DISABLED);
Joe Eykholt97c83892009-03-17 11:42:40 -0700229 fcoe_ctlr_reset_fcfs(fip);
Joe Eykholtfdb068c2010-07-20 15:19:47 -0700230 mutex_unlock(&fip->ctlr_mutex);
Joe Eykholt97c83892009-03-17 11:42:40 -0700231 del_timer_sync(&fip->timer);
Joe Eykholt42913652010-03-12 16:08:23 -0800232 cancel_work_sync(&fip->timer_work);
Joe Eykholt97c83892009-03-17 11:42:40 -0700233}
234EXPORT_SYMBOL(fcoe_ctlr_destroy);
235
236/**
Joe Eykholt794d98e2010-11-30 16:19:56 -0800237 * fcoe_ctlr_announce() - announce new FCF selection
Joe Eykholt69316ee2010-11-30 16:19:40 -0800238 * @fip: The FCoE controller
239 *
240 * Also sets the destination MAC for FCoE and control packets
Joe Eykholt794d98e2010-11-30 16:19:56 -0800241 *
242 * Called with neither ctlr_mutex nor ctlr_lock held.
Joe Eykholt69316ee2010-11-30 16:19:40 -0800243 */
244static void fcoe_ctlr_announce(struct fcoe_ctlr *fip)
245{
Joe Eykholt794d98e2010-11-30 16:19:56 -0800246 struct fcoe_fcf *sel;
247 struct fcoe_fcf *fcf;
248
249 mutex_lock(&fip->ctlr_mutex);
250 spin_lock_bh(&fip->ctlr_lock);
251
252 kfree_skb(fip->flogi_req);
253 fip->flogi_req = NULL;
254 list_for_each_entry(fcf, &fip->fcfs, list)
255 fcf->flogi_sent = 0;
256
257 spin_unlock_bh(&fip->ctlr_lock);
258 sel = fip->sel_fcf;
Joe Eykholt69316ee2010-11-30 16:19:40 -0800259
260 if (sel && !compare_ether_addr(sel->fcf_mac, fip->dest_addr))
Joe Eykholt794d98e2010-11-30 16:19:56 -0800261 goto unlock;
Joe Eykholt69316ee2010-11-30 16:19:40 -0800262 if (!is_zero_ether_addr(fip->dest_addr)) {
263 printk(KERN_NOTICE "libfcoe: host%d: "
264 "FIP Fibre-Channel Forwarder MAC %pM deselected\n",
265 fip->lp->host->host_no, fip->dest_addr);
266 memset(fip->dest_addr, 0, ETH_ALEN);
267 }
268 if (sel) {
269 printk(KERN_INFO "libfcoe: host%d: FIP selected "
270 "Fibre-Channel Forwarder MAC %pM\n",
271 fip->lp->host->host_no, sel->fcf_mac);
272 memcpy(fip->dest_addr, sel->fcf_mac, ETH_ALEN);
273 fip->map_dest = 0;
274 }
Joe Eykholt794d98e2010-11-30 16:19:56 -0800275unlock:
276 mutex_unlock(&fip->ctlr_mutex);
Joe Eykholt69316ee2010-11-30 16:19:40 -0800277}
278
279/**
Robert Love70b51aa2009-11-03 11:47:45 -0800280 * fcoe_ctlr_fcoe_size() - Return the maximum FCoE size required for VN_Port
281 * @fip: The FCoE controller to get the maximum FCoE size from
Joe Eykholt97c83892009-03-17 11:42:40 -0700282 *
283 * Returns the maximum packet size including the FCoE header and trailer,
284 * but not including any Ethernet or VLAN headers.
285 */
286static inline u32 fcoe_ctlr_fcoe_size(struct fcoe_ctlr *fip)
287{
288 /*
289 * Determine the max FCoE frame size allowed, including
290 * FCoE header and trailer.
291 * Note: lp->mfs is currently the payload size, not the frame size.
292 */
293 return fip->lp->mfs + sizeof(struct fc_frame_header) +
294 sizeof(struct fcoe_hdr) + sizeof(struct fcoe_crc_eof);
295}
296
297/**
Robert Love70b51aa2009-11-03 11:47:45 -0800298 * fcoe_ctlr_solicit() - Send a FIP solicitation
299 * @fip: The FCoE controller to send the solicitation on
300 * @fcf: The destination FCF (if NULL, a multicast solicitation is sent)
Joe Eykholt97c83892009-03-17 11:42:40 -0700301 */
302static void fcoe_ctlr_solicit(struct fcoe_ctlr *fip, struct fcoe_fcf *fcf)
303{
304 struct sk_buff *skb;
305 struct fip_sol {
306 struct ethhdr eth;
307 struct fip_header fip;
308 struct {
309 struct fip_mac_desc mac;
310 struct fip_wwn_desc wwnn;
311 struct fip_size_desc size;
312 } __attribute__((packed)) desc;
313 } __attribute__((packed)) *sol;
314 u32 fcoe_size;
315
316 skb = dev_alloc_skb(sizeof(*sol));
317 if (!skb)
318 return;
319
320 sol = (struct fip_sol *)skb->data;
321
322 memset(sol, 0, sizeof(*sol));
323 memcpy(sol->eth.h_dest, fcf ? fcf->fcf_mac : fcoe_all_fcfs, ETH_ALEN);
324 memcpy(sol->eth.h_source, fip->ctl_src_addr, ETH_ALEN);
325 sol->eth.h_proto = htons(ETH_P_FIP);
326
327 sol->fip.fip_ver = FIP_VER_ENCAPS(FIP_VER);
328 sol->fip.fip_op = htons(FIP_OP_DISC);
329 sol->fip.fip_subcode = FIP_SC_SOL;
330 sol->fip.fip_dl_len = htons(sizeof(sol->desc) / FIP_BPW);
331 sol->fip.fip_flags = htons(FIP_FL_FPMA);
Vasu Dev184dd342009-05-17 12:33:28 +0000332 if (fip->spma)
333 sol->fip.fip_flags |= htons(FIP_FL_SPMA);
Joe Eykholt97c83892009-03-17 11:42:40 -0700334
335 sol->desc.mac.fd_desc.fip_dtype = FIP_DT_MAC;
336 sol->desc.mac.fd_desc.fip_dlen = sizeof(sol->desc.mac) / FIP_BPW;
337 memcpy(sol->desc.mac.fd_mac, fip->ctl_src_addr, ETH_ALEN);
338
339 sol->desc.wwnn.fd_desc.fip_dtype = FIP_DT_NAME;
340 sol->desc.wwnn.fd_desc.fip_dlen = sizeof(sol->desc.wwnn) / FIP_BPW;
341 put_unaligned_be64(fip->lp->wwnn, &sol->desc.wwnn.fd_wwn);
342
343 fcoe_size = fcoe_ctlr_fcoe_size(fip);
344 sol->desc.size.fd_desc.fip_dtype = FIP_DT_FCOE_SIZE;
345 sol->desc.size.fd_desc.fip_dlen = sizeof(sol->desc.size) / FIP_BPW;
346 sol->desc.size.fd_size = htons(fcoe_size);
347
348 skb_put(skb, sizeof(*sol));
Chris Leech0f491532009-05-06 10:52:18 -0700349 skb->protocol = htons(ETH_P_FIP);
Joe Eykholt97c83892009-03-17 11:42:40 -0700350 skb_reset_mac_header(skb);
351 skb_reset_network_header(skb);
352 fip->send(fip, skb);
353
354 if (!fcf)
355 fip->sol_time = jiffies;
356}
357
358/**
Robert Love70b51aa2009-11-03 11:47:45 -0800359 * fcoe_ctlr_link_up() - Start FCoE controller
360 * @fip: The FCoE controller to start
Joe Eykholt97c83892009-03-17 11:42:40 -0700361 *
362 * Called from the LLD when the network link is ready.
363 */
364void fcoe_ctlr_link_up(struct fcoe_ctlr *fip)
365{
Joe Eykholtfdb068c2010-07-20 15:19:47 -0700366 mutex_lock(&fip->ctlr_mutex);
Joe Eykholt97c83892009-03-17 11:42:40 -0700367 if (fip->state == FIP_ST_NON_FIP || fip->state == FIP_ST_AUTO) {
Joe Eykholtfdb068c2010-07-20 15:19:47 -0700368 mutex_unlock(&fip->ctlr_mutex);
Joe Eykholt97c83892009-03-17 11:42:40 -0700369 fc_linkup(fip->lp);
370 } else if (fip->state == FIP_ST_LINK_WAIT) {
Joe Eykholt9b651da2010-07-20 15:20:24 -0700371 fcoe_ctlr_set_state(fip, fip->mode);
Joe Eykholte10f8c62010-07-20 15:20:30 -0700372 switch (fip->mode) {
373 default:
374 LIBFCOE_FIP_DBG(fip, "invalid mode %d\n", fip->mode);
375 /* fall-through */
376 case FIP_MODE_AUTO:
Joe Eykholt0f51c2e2009-11-03 11:48:16 -0800377 LIBFCOE_FIP_DBG(fip, "%s", "setting AUTO mode.\n");
Joe Eykholte10f8c62010-07-20 15:20:30 -0700378 /* fall-through */
379 case FIP_MODE_FABRIC:
380 case FIP_MODE_NON_FIP:
381 mutex_unlock(&fip->ctlr_mutex);
382 fc_linkup(fip->lp);
383 fcoe_ctlr_solicit(fip, NULL);
384 break;
385 case FIP_MODE_VN2VN:
386 fcoe_ctlr_vn_start(fip);
387 mutex_unlock(&fip->ctlr_mutex);
388 fc_linkup(fip->lp);
389 break;
390 }
Joe Eykholt97c83892009-03-17 11:42:40 -0700391 } else
Joe Eykholtfdb068c2010-07-20 15:19:47 -0700392 mutex_unlock(&fip->ctlr_mutex);
Joe Eykholt97c83892009-03-17 11:42:40 -0700393}
394EXPORT_SYMBOL(fcoe_ctlr_link_up);
395
396/**
Robert Love70b51aa2009-11-03 11:47:45 -0800397 * fcoe_ctlr_reset() - Reset a FCoE controller
398 * @fip: The FCoE controller to reset
Joe Eykholt97c83892009-03-17 11:42:40 -0700399 */
Joe Eykholtdd42dac2009-11-03 11:48:27 -0800400static void fcoe_ctlr_reset(struct fcoe_ctlr *fip)
Joe Eykholt97c83892009-03-17 11:42:40 -0700401{
Joe Eykholt97c83892009-03-17 11:42:40 -0700402 fcoe_ctlr_reset_fcfs(fip);
403 del_timer(&fip->timer);
Joe Eykholt97c83892009-03-17 11:42:40 -0700404 fip->ctlr_ka_time = 0;
405 fip->port_ka_time = 0;
406 fip->sol_time = 0;
407 fip->flogi_oxid = FC_XID_UNKNOWN;
Joe Eykholtcd229e42010-07-20 15:20:40 -0700408 fcoe_ctlr_map_dest(fip);
Joe Eykholt97c83892009-03-17 11:42:40 -0700409}
410
411/**
Robert Love70b51aa2009-11-03 11:47:45 -0800412 * fcoe_ctlr_link_down() - Stop a FCoE controller
413 * @fip: The FCoE controller to be stopped
Joe Eykholt97c83892009-03-17 11:42:40 -0700414 *
415 * Returns non-zero if the link was up and now isn't.
416 *
417 * Called from the LLD when the network link is not ready.
418 * There may be multiple calls while the link is down.
419 */
420int fcoe_ctlr_link_down(struct fcoe_ctlr *fip)
421{
Joe Eykholtdd42dac2009-11-03 11:48:27 -0800422 int link_dropped;
423
424 LIBFCOE_FIP_DBG(fip, "link down.\n");
Joe Eykholtfdb068c2010-07-20 15:19:47 -0700425 mutex_lock(&fip->ctlr_mutex);
Joe Eykholtdd42dac2009-11-03 11:48:27 -0800426 fcoe_ctlr_reset(fip);
Joe Eykholt42913652010-03-12 16:08:23 -0800427 link_dropped = fip->state != FIP_ST_LINK_WAIT;
Joe Eykholt9b651da2010-07-20 15:20:24 -0700428 fcoe_ctlr_set_state(fip, FIP_ST_LINK_WAIT);
Joe Eykholtfdb068c2010-07-20 15:19:47 -0700429 mutex_unlock(&fip->ctlr_mutex);
Joe Eykholtdd42dac2009-11-03 11:48:27 -0800430
431 if (link_dropped)
432 fc_linkdown(fip->lp);
433 return link_dropped;
Joe Eykholt97c83892009-03-17 11:42:40 -0700434}
435EXPORT_SYMBOL(fcoe_ctlr_link_down);
436
437/**
Robert Love70b51aa2009-11-03 11:47:45 -0800438 * fcoe_ctlr_send_keep_alive() - Send a keep-alive to the selected FCF
439 * @fip: The FCoE controller to send the FKA on
440 * @lport: libfc fc_lport to send from
441 * @ports: 0 for controller keep-alive, 1 for port keep-alive
442 * @sa: The source MAC address
Joe Eykholt97c83892009-03-17 11:42:40 -0700443 *
444 * A controller keep-alive is sent every fka_period (typically 8 seconds).
445 * The source MAC is the native MAC address.
446 *
447 * A port keep-alive is sent every 90 seconds while logged in.
448 * The source MAC is the assigned mapped source address.
449 * The destination is the FCF's F-port.
450 */
Chris Leech11b56182009-11-03 11:46:29 -0800451static void fcoe_ctlr_send_keep_alive(struct fcoe_ctlr *fip,
452 struct fc_lport *lport,
453 int ports, u8 *sa)
Joe Eykholt97c83892009-03-17 11:42:40 -0700454{
455 struct sk_buff *skb;
456 struct fip_kal {
457 struct ethhdr eth;
458 struct fip_header fip;
459 struct fip_mac_desc mac;
460 } __attribute__((packed)) *kal;
461 struct fip_vn_desc *vn;
462 u32 len;
463 struct fc_lport *lp;
464 struct fcoe_fcf *fcf;
465
466 fcf = fip->sel_fcf;
467 lp = fip->lp;
Joe Eykholt281ae642010-06-11 16:43:33 -0700468 if (!fcf || (ports && !lp->port_id))
Joe Eykholt97c83892009-03-17 11:42:40 -0700469 return;
470
Yi Zoube276cb2009-11-03 11:50:16 -0800471 len = sizeof(*kal) + ports * sizeof(*vn);
Joe Eykholt97c83892009-03-17 11:42:40 -0700472 skb = dev_alloc_skb(len);
473 if (!skb)
474 return;
475
476 kal = (struct fip_kal *)skb->data;
477 memset(kal, 0, len);
478 memcpy(kal->eth.h_dest, fcf->fcf_mac, ETH_ALEN);
479 memcpy(kal->eth.h_source, sa, ETH_ALEN);
480 kal->eth.h_proto = htons(ETH_P_FIP);
481
482 kal->fip.fip_ver = FIP_VER_ENCAPS(FIP_VER);
483 kal->fip.fip_op = htons(FIP_OP_CTRL);
484 kal->fip.fip_subcode = FIP_SC_KEEP_ALIVE;
485 kal->fip.fip_dl_len = htons((sizeof(kal->mac) +
Robert Love70b51aa2009-11-03 11:47:45 -0800486 ports * sizeof(*vn)) / FIP_BPW);
Joe Eykholt97c83892009-03-17 11:42:40 -0700487 kal->fip.fip_flags = htons(FIP_FL_FPMA);
Vasu Dev184dd342009-05-17 12:33:28 +0000488 if (fip->spma)
489 kal->fip.fip_flags |= htons(FIP_FL_SPMA);
Joe Eykholt97c83892009-03-17 11:42:40 -0700490
491 kal->mac.fd_desc.fip_dtype = FIP_DT_MAC;
492 kal->mac.fd_desc.fip_dlen = sizeof(kal->mac) / FIP_BPW;
493 memcpy(kal->mac.fd_mac, fip->ctl_src_addr, ETH_ALEN);
Joe Eykholt97c83892009-03-17 11:42:40 -0700494 if (ports) {
495 vn = (struct fip_vn_desc *)(kal + 1);
496 vn->fd_desc.fip_dtype = FIP_DT_VN_ID;
497 vn->fd_desc.fip_dlen = sizeof(*vn) / FIP_BPW;
Chris Leech11b56182009-11-03 11:46:29 -0800498 memcpy(vn->fd_mac, fip->get_src_addr(lport), ETH_ALEN);
Kaladhar Musunurufb831532010-05-07 15:18:57 -0700499 hton24(vn->fd_fc_id, lport->port_id);
500 put_unaligned_be64(lport->wwpn, &vn->fd_wwpn);
Joe Eykholt97c83892009-03-17 11:42:40 -0700501 }
Joe Eykholt97c83892009-03-17 11:42:40 -0700502 skb_put(skb, len);
Chris Leech0f491532009-05-06 10:52:18 -0700503 skb->protocol = htons(ETH_P_FIP);
Joe Eykholt97c83892009-03-17 11:42:40 -0700504 skb_reset_mac_header(skb);
505 skb_reset_network_header(skb);
506 fip->send(fip, skb);
507}
508
509/**
Robert Love70b51aa2009-11-03 11:47:45 -0800510 * fcoe_ctlr_encaps() - Encapsulate an ELS frame for FIP, without sending it
511 * @fip: The FCoE controller for the ELS frame
512 * @dtype: The FIP descriptor type for the frame
513 * @skb: The FCoE ELS frame including FC header but no FCoE headers
Joe Eykholte10f8c62010-07-20 15:20:30 -0700514 * @d_id: The destination port ID.
Joe Eykholt97c83892009-03-17 11:42:40 -0700515 *
516 * Returns non-zero error code on failure.
517 *
518 * The caller must check that the length is a multiple of 4.
519 *
520 * The @skb must have enough headroom (28 bytes) and tailroom (8 bytes).
521 * Headroom includes the FIP encapsulation description, FIP header, and
522 * Ethernet header. The tailroom is for the FIP MAC descriptor.
523 */
Chris Leech11b56182009-11-03 11:46:29 -0800524static int fcoe_ctlr_encaps(struct fcoe_ctlr *fip, struct fc_lport *lport,
Joe Eykholte10f8c62010-07-20 15:20:30 -0700525 u8 dtype, struct sk_buff *skb, u32 d_id)
Joe Eykholt97c83892009-03-17 11:42:40 -0700526{
527 struct fip_encaps_head {
528 struct ethhdr eth;
529 struct fip_header fip;
530 struct fip_encaps encaps;
531 } __attribute__((packed)) *cap;
Joe Eykholt55543452010-07-20 15:20:35 -0700532 struct fc_frame_header *fh;
Joe Eykholt97c83892009-03-17 11:42:40 -0700533 struct fip_mac_desc *mac;
534 struct fcoe_fcf *fcf;
535 size_t dlen;
Yi Zou5a84bae2009-07-29 17:03:55 -0700536 u16 fip_flags;
Joe Eykholt55543452010-07-20 15:20:35 -0700537 u8 op;
Joe Eykholt97c83892009-03-17 11:42:40 -0700538
Joe Eykholt55543452010-07-20 15:20:35 -0700539 fh = (struct fc_frame_header *)skb->data;
540 op = *(u8 *)(fh + 1);
Joe Eykholt97c83892009-03-17 11:42:40 -0700541 dlen = sizeof(struct fip_encaps) + skb->len; /* len before push */
542 cap = (struct fip_encaps_head *)skb_push(skb, sizeof(*cap));
Joe Eykholt97c83892009-03-17 11:42:40 -0700543 memset(cap, 0, sizeof(*cap));
Joe Eykholte10f8c62010-07-20 15:20:30 -0700544
545 if (lport->point_to_multipoint) {
546 if (fcoe_ctlr_vn_lookup(fip, d_id, cap->eth.h_dest))
547 return -ENODEV;
Joe Eykholt55543452010-07-20 15:20:35 -0700548 fip_flags = 0;
Joe Eykholte10f8c62010-07-20 15:20:30 -0700549 } else {
550 fcf = fip->sel_fcf;
551 if (!fcf)
552 return -ENODEV;
553 fip_flags = fcf->flags;
554 fip_flags &= fip->spma ? FIP_FL_SPMA | FIP_FL_FPMA :
555 FIP_FL_FPMA;
556 if (!fip_flags)
557 return -ENODEV;
558 memcpy(cap->eth.h_dest, fcf->fcf_mac, ETH_ALEN);
559 }
Joe Eykholt97c83892009-03-17 11:42:40 -0700560 memcpy(cap->eth.h_source, fip->ctl_src_addr, ETH_ALEN);
561 cap->eth.h_proto = htons(ETH_P_FIP);
562
563 cap->fip.fip_ver = FIP_VER_ENCAPS(FIP_VER);
564 cap->fip.fip_op = htons(FIP_OP_LS);
Joe Eykholt55543452010-07-20 15:20:35 -0700565 if (op == ELS_LS_ACC || op == ELS_LS_RJT)
566 cap->fip.fip_subcode = FIP_SC_REP;
567 else
568 cap->fip.fip_subcode = FIP_SC_REQ;
Yi Zou5a84bae2009-07-29 17:03:55 -0700569 cap->fip.fip_flags = htons(fip_flags);
Joe Eykholt97c83892009-03-17 11:42:40 -0700570
571 cap->encaps.fd_desc.fip_dtype = dtype;
572 cap->encaps.fd_desc.fip_dlen = dlen / FIP_BPW;
573
Joe Eykholt55543452010-07-20 15:20:35 -0700574 if (op != ELS_LS_RJT) {
575 dlen += sizeof(*mac);
576 mac = (struct fip_mac_desc *)skb_put(skb, sizeof(*mac));
577 memset(mac, 0, sizeof(*mac));
578 mac->fd_desc.fip_dtype = FIP_DT_MAC;
579 mac->fd_desc.fip_dlen = sizeof(*mac) / FIP_BPW;
580 if (dtype != FIP_DT_FLOGI && dtype != FIP_DT_FDISC) {
581 memcpy(mac->fd_mac, fip->get_src_addr(lport), ETH_ALEN);
582 } else if (fip->mode == FIP_MODE_VN2VN) {
583 hton24(mac->fd_mac, FIP_VN_FC_MAP);
584 hton24(mac->fd_mac + 3, fip->port_id);
585 } else if (fip_flags & FIP_FL_SPMA) {
586 LIBFCOE_FIP_DBG(fip, "FLOGI/FDISC sent with SPMA\n");
587 memcpy(mac->fd_mac, fip->ctl_src_addr, ETH_ALEN);
588 } else {
589 LIBFCOE_FIP_DBG(fip, "FLOGI/FDISC sent with FPMA\n");
590 /* FPMA only FLOGI. Must leave the MAC desc zeroed. */
591 }
Robert Love593abc02010-04-09 14:22:17 -0700592 }
Joe Eykholt55543452010-07-20 15:20:35 -0700593 cap->fip.fip_dl_len = htons(dlen / FIP_BPW);
Joe Eykholt97c83892009-03-17 11:42:40 -0700594
Chris Leech0f491532009-05-06 10:52:18 -0700595 skb->protocol = htons(ETH_P_FIP);
Joe Eykholt97c83892009-03-17 11:42:40 -0700596 skb_reset_mac_header(skb);
597 skb_reset_network_header(skb);
598 return 0;
599}
600
601/**
602 * fcoe_ctlr_els_send() - Send an ELS frame encapsulated by FIP if appropriate.
603 * @fip: FCoE controller.
Chris Leech11b56182009-11-03 11:46:29 -0800604 * @lport: libfc fc_lport to send from
Joe Eykholt97c83892009-03-17 11:42:40 -0700605 * @skb: FCoE ELS frame including FC header but no FCoE headers.
606 *
607 * Returns a non-zero error code if the frame should not be sent.
608 * Returns zero if the caller should send the frame with FCoE encapsulation.
609 *
610 * The caller must check that the length is a multiple of 4.
611 * The SKB must have enough headroom (28 bytes) and tailroom (8 bytes).
Joe Eykholte10f8c62010-07-20 15:20:30 -0700612 * The the skb must also be an fc_frame.
Joe Eykholt794d98e2010-11-30 16:19:56 -0800613 *
614 * This is called from the lower-level driver with spinlocks held,
615 * so we must not take a mutex here.
Joe Eykholt97c83892009-03-17 11:42:40 -0700616 */
Chris Leech11b56182009-11-03 11:46:29 -0800617int fcoe_ctlr_els_send(struct fcoe_ctlr *fip, struct fc_lport *lport,
618 struct sk_buff *skb)
Joe Eykholt97c83892009-03-17 11:42:40 -0700619{
Joe Eykholte10f8c62010-07-20 15:20:30 -0700620 struct fc_frame *fp;
Joe Eykholt97c83892009-03-17 11:42:40 -0700621 struct fc_frame_header *fh;
622 u16 old_xid;
623 u8 op;
Chris Leech11b56182009-11-03 11:46:29 -0800624 u8 mac[ETH_ALEN];
Joe Eykholt97c83892009-03-17 11:42:40 -0700625
Joe Eykholte10f8c62010-07-20 15:20:30 -0700626 fp = container_of(skb, struct fc_frame, skb);
Joe Eykholt97c83892009-03-17 11:42:40 -0700627 fh = (struct fc_frame_header *)skb->data;
628 op = *(u8 *)(fh + 1);
629
Joe Eykholte10f8c62010-07-20 15:20:30 -0700630 if (op == ELS_FLOGI && fip->mode != FIP_MODE_VN2VN) {
Joe Eykholt97c83892009-03-17 11:42:40 -0700631 old_xid = fip->flogi_oxid;
632 fip->flogi_oxid = ntohs(fh->fh_ox_id);
633 if (fip->state == FIP_ST_AUTO) {
634 if (old_xid == FC_XID_UNKNOWN)
635 fip->flogi_count = 0;
636 fip->flogi_count++;
637 if (fip->flogi_count < 3)
638 goto drop;
Joe Eykholtcd229e42010-07-20 15:20:40 -0700639 fcoe_ctlr_map_dest(fip);
Joe Eykholt97c83892009-03-17 11:42:40 -0700640 return 0;
641 }
Joe Eykholt5f48f702009-05-06 10:52:12 -0700642 if (fip->state == FIP_ST_NON_FIP)
Joe Eykholtcd229e42010-07-20 15:20:40 -0700643 fcoe_ctlr_map_dest(fip);
Joe Eykholt5f48f702009-05-06 10:52:12 -0700644 }
645
646 if (fip->state == FIP_ST_NON_FIP)
647 return 0;
Joe Eykholte10f8c62010-07-20 15:20:30 -0700648 if (!fip->sel_fcf && fip->mode != FIP_MODE_VN2VN)
Joe Eykholtf31f2a12009-11-03 11:48:32 -0800649 goto drop;
Joe Eykholt5f48f702009-05-06 10:52:12 -0700650 switch (op) {
651 case ELS_FLOGI:
Joe Eykholt97c83892009-03-17 11:42:40 -0700652 op = FIP_DT_FLOGI;
Joe Eykholt794d98e2010-11-30 16:19:56 -0800653 if (fip->mode == FIP_MODE_VN2VN)
654 break;
655 spin_lock_bh(&fip->ctlr_lock);
656 kfree_skb(fip->flogi_req);
657 fip->flogi_req = skb;
658 fip->flogi_req_send = 1;
659 spin_unlock_bh(&fip->ctlr_lock);
660 schedule_work(&fip->timer_work);
661 return -EINPROGRESS;
Joe Eykholt97c83892009-03-17 11:42:40 -0700662 case ELS_FDISC:
663 if (ntoh24(fh->fh_s_id))
664 return 0;
665 op = FIP_DT_FDISC;
666 break;
667 case ELS_LOGO:
Joe Eykholte10f8c62010-07-20 15:20:30 -0700668 if (fip->mode == FIP_MODE_VN2VN) {
669 if (fip->state != FIP_ST_VNMP_UP)
670 return -EINVAL;
671 if (ntoh24(fh->fh_d_id) == FC_FID_FLOGI)
672 return -EINVAL;
673 } else {
674 if (fip->state != FIP_ST_ENABLED)
675 return 0;
676 if (ntoh24(fh->fh_d_id) != FC_FID_FLOGI)
677 return 0;
678 }
Joe Eykholt97c83892009-03-17 11:42:40 -0700679 op = FIP_DT_LOGO;
680 break;
681 case ELS_LS_ACC:
Joe Eykholt97c83892009-03-17 11:42:40 -0700682 /*
Joe Eykholte10f8c62010-07-20 15:20:30 -0700683 * If non-FIP, we may have gotten an SID by accepting an FLOGI
Joe Eykholt97c83892009-03-17 11:42:40 -0700684 * from a point-to-point connection. Switch to using
685 * the source mac based on the SID. The destination
686 * MAC in this case would have been set by receving the
687 * FLOGI.
688 */
Joe Eykholte10f8c62010-07-20 15:20:30 -0700689 if (fip->state == FIP_ST_NON_FIP) {
690 if (fip->flogi_oxid == FC_XID_UNKNOWN)
691 return 0;
692 fip->flogi_oxid = FC_XID_UNKNOWN;
693 fc_fcoe_set_mac(mac, fh->fh_d_id);
694 fip->update_mac(lport, mac);
695 }
696 /* fall through */
697 case ELS_LS_RJT:
698 op = fr_encaps(fp);
699 if (op)
700 break;
Joe Eykholt97c83892009-03-17 11:42:40 -0700701 return 0;
702 default:
Joe Eykholte10f8c62010-07-20 15:20:30 -0700703 if (fip->state != FIP_ST_ENABLED &&
704 fip->state != FIP_ST_VNMP_UP)
Joe Eykholt97c83892009-03-17 11:42:40 -0700705 goto drop;
706 return 0;
707 }
Joe Eykholte10f8c62010-07-20 15:20:30 -0700708 LIBFCOE_FIP_DBG(fip, "els_send op %u d_id %x\n",
709 op, ntoh24(fh->fh_d_id));
710 if (fcoe_ctlr_encaps(fip, lport, op, skb, ntoh24(fh->fh_d_id)))
Joe Eykholt97c83892009-03-17 11:42:40 -0700711 goto drop;
712 fip->send(fip, skb);
713 return -EINPROGRESS;
714drop:
715 kfree_skb(skb);
716 return -EINVAL;
717}
718EXPORT_SYMBOL(fcoe_ctlr_els_send);
719
Robert Love70b51aa2009-11-03 11:47:45 -0800720/**
721 * fcoe_ctlr_age_fcfs() - Reset and free all old FCFs for a controller
722 * @fip: The FCoE controller to free FCFs on
Joe Eykholt97c83892009-03-17 11:42:40 -0700723 *
Joe Eykholtf018b732010-03-12 16:08:55 -0800724 * Called with lock held and preemption disabled.
Joe Eykholt97c83892009-03-17 11:42:40 -0700725 *
Joe Eykholt8690cb82010-06-11 16:44:10 -0700726 * An FCF is considered old if we have missed two advertisements.
727 * That is, there have been no valid advertisement from it for 2.5
728 * times its keep-alive period.
Joe Eykholt97c83892009-03-17 11:42:40 -0700729 *
730 * In addition, determine the time when an FCF selection can occur.
Yi Zouf3da80e2009-11-20 14:55:08 -0800731 *
732 * Also, increment the MissDiscAdvCount when no advertisement is received
733 * for the corresponding FCF for 1.5 * FKA_ADV_PERIOD (FC-BB-5 LESB).
Joe Eykholt8690cb82010-06-11 16:44:10 -0700734 *
735 * Returns the time in jiffies for the next call.
Joe Eykholt97c83892009-03-17 11:42:40 -0700736 */
Joe Eykholt8690cb82010-06-11 16:44:10 -0700737static unsigned long fcoe_ctlr_age_fcfs(struct fcoe_ctlr *fip)
Joe Eykholt97c83892009-03-17 11:42:40 -0700738{
739 struct fcoe_fcf *fcf;
740 struct fcoe_fcf *next;
Joe Eykholt8690cb82010-06-11 16:44:10 -0700741 unsigned long next_timer = jiffies + msecs_to_jiffies(FIP_VN_KA_PERIOD);
742 unsigned long deadline;
Joe Eykholt97c83892009-03-17 11:42:40 -0700743 unsigned long sel_time = 0;
Joe Eykholtf018b732010-03-12 16:08:55 -0800744 struct fcoe_dev_stats *stats;
Joe Eykholt97c83892009-03-17 11:42:40 -0700745
Joe Eykholtfdb068c2010-07-20 15:19:47 -0700746 stats = per_cpu_ptr(fip->lp->dev_stats, get_cpu());
747
Joe Eykholt97c83892009-03-17 11:42:40 -0700748 list_for_each_entry_safe(fcf, next, &fip->fcfs, list) {
Joe Eykholt8690cb82010-06-11 16:44:10 -0700749 deadline = fcf->time + fcf->fka_period + fcf->fka_period / 2;
750 if (fip->sel_fcf == fcf) {
751 if (time_after(jiffies, deadline)) {
Joe Eykholt8690cb82010-06-11 16:44:10 -0700752 stats->MissDiscAdvCount++;
753 printk(KERN_INFO "libfcoe: host%d: "
754 "Missing Discovery Advertisement "
755 "for fab %16.16llx count %lld\n",
756 fip->lp->host->host_no, fcf->fabric_name,
757 stats->MissDiscAdvCount);
758 } else if (time_after(next_timer, deadline))
759 next_timer = deadline;
Yi Zouf3da80e2009-11-20 14:55:08 -0800760 }
Joe Eykholt8690cb82010-06-11 16:44:10 -0700761
762 deadline += fcf->fka_period;
Joe Eykholtd99ee452010-06-11 16:44:15 -0700763 if (time_after_eq(jiffies, deadline)) {
Joe Eykholt97c83892009-03-17 11:42:40 -0700764 if (fip->sel_fcf == fcf)
765 fip->sel_fcf = NULL;
766 list_del(&fcf->list);
767 WARN_ON(!fip->fcf_count);
768 fip->fcf_count--;
769 kfree(fcf);
Joe Eykholtf018b732010-03-12 16:08:55 -0800770 stats->VLinkFailureCount++;
Joe Eykholt8690cb82010-06-11 16:44:10 -0700771 } else {
772 if (time_after(next_timer, deadline))
773 next_timer = deadline;
774 if (fcoe_ctlr_mtu_valid(fcf) &&
775 (!sel_time || time_before(sel_time, fcf->time)))
776 sel_time = fcf->time;
Joe Eykholt97c83892009-03-17 11:42:40 -0700777 }
778 }
Joe Eykholtfdb068c2010-07-20 15:19:47 -0700779 put_cpu();
Joe Eykholtd99ee452010-06-11 16:44:15 -0700780 if (sel_time && !fip->sel_fcf && !fip->sel_time) {
Joe Eykholt97c83892009-03-17 11:42:40 -0700781 sel_time += msecs_to_jiffies(FCOE_CTLR_START_DELAY);
782 fip->sel_time = sel_time;
Joe Eykholt97c83892009-03-17 11:42:40 -0700783 }
Joe Eykholtd99ee452010-06-11 16:44:15 -0700784
Joe Eykholt8690cb82010-06-11 16:44:10 -0700785 return next_timer;
Joe Eykholt97c83892009-03-17 11:42:40 -0700786}
787
788/**
Robert Love70b51aa2009-11-03 11:47:45 -0800789 * fcoe_ctlr_parse_adv() - Decode a FIP advertisement into a new FCF entry
Joe Eykholt0f51c2e2009-11-03 11:48:16 -0800790 * @fip: The FCoE controller receiving the advertisement
Robert Love70b51aa2009-11-03 11:47:45 -0800791 * @skb: The received FIP advertisement frame
792 * @fcf: The resulting FCF entry
Joe Eykholt97c83892009-03-17 11:42:40 -0700793 *
794 * Returns zero on a valid parsed advertisement,
795 * otherwise returns non zero value.
796 */
Joe Eykholt0f51c2e2009-11-03 11:48:16 -0800797static int fcoe_ctlr_parse_adv(struct fcoe_ctlr *fip,
798 struct sk_buff *skb, struct fcoe_fcf *fcf)
Joe Eykholt97c83892009-03-17 11:42:40 -0700799{
800 struct fip_header *fiph;
801 struct fip_desc *desc = NULL;
802 struct fip_wwn_desc *wwn;
803 struct fip_fab_desc *fab;
804 struct fip_fka_desc *fka;
805 unsigned long t;
806 size_t rlen;
807 size_t dlen;
Bhanu Prakash Gollapudi0a9c5d32010-06-11 16:44:25 -0700808 u32 desc_mask;
Joe Eykholt97c83892009-03-17 11:42:40 -0700809
810 memset(fcf, 0, sizeof(*fcf));
811 fcf->fka_period = msecs_to_jiffies(FCOE_CTLR_DEF_FKA);
812
813 fiph = (struct fip_header *)skb->data;
814 fcf->flags = ntohs(fiph->fip_flags);
815
Bhanu Prakash Gollapudi0a9c5d32010-06-11 16:44:25 -0700816 /*
817 * mask of required descriptors. validating each one clears its bit.
818 */
819 desc_mask = BIT(FIP_DT_PRI) | BIT(FIP_DT_MAC) | BIT(FIP_DT_NAME) |
820 BIT(FIP_DT_FAB) | BIT(FIP_DT_FKA);
821
Joe Eykholt97c83892009-03-17 11:42:40 -0700822 rlen = ntohs(fiph->fip_dl_len) * 4;
823 if (rlen + sizeof(*fiph) > skb->len)
824 return -EINVAL;
825
826 desc = (struct fip_desc *)(fiph + 1);
827 while (rlen > 0) {
828 dlen = desc->fip_dlen * FIP_BPW;
829 if (dlen < sizeof(*desc) || dlen > rlen)
830 return -EINVAL;
Bhanu Prakash Gollapudi0a9c5d32010-06-11 16:44:25 -0700831 /* Drop Adv if there are duplicate critical descriptors */
832 if ((desc->fip_dtype < 32) &&
833 !(desc_mask & 1U << desc->fip_dtype)) {
834 LIBFCOE_FIP_DBG(fip, "Duplicate Critical "
835 "Descriptors in FIP adv\n");
836 return -EINVAL;
837 }
Joe Eykholt97c83892009-03-17 11:42:40 -0700838 switch (desc->fip_dtype) {
839 case FIP_DT_PRI:
840 if (dlen != sizeof(struct fip_pri_desc))
841 goto len_err;
842 fcf->pri = ((struct fip_pri_desc *)desc)->fd_pri;
Bhanu Prakash Gollapudi0a9c5d32010-06-11 16:44:25 -0700843 desc_mask &= ~BIT(FIP_DT_PRI);
Joe Eykholt97c83892009-03-17 11:42:40 -0700844 break;
845 case FIP_DT_MAC:
846 if (dlen != sizeof(struct fip_mac_desc))
847 goto len_err;
848 memcpy(fcf->fcf_mac,
849 ((struct fip_mac_desc *)desc)->fd_mac,
850 ETH_ALEN);
851 if (!is_valid_ether_addr(fcf->fcf_mac)) {
Joe Eykholte10f8c62010-07-20 15:20:30 -0700852 LIBFCOE_FIP_DBG(fip,
853 "Invalid MAC addr %pM in FIP adv\n",
854 fcf->fcf_mac);
Joe Eykholt97c83892009-03-17 11:42:40 -0700855 return -EINVAL;
856 }
Bhanu Prakash Gollapudi0a9c5d32010-06-11 16:44:25 -0700857 desc_mask &= ~BIT(FIP_DT_MAC);
Joe Eykholt97c83892009-03-17 11:42:40 -0700858 break;
859 case FIP_DT_NAME:
860 if (dlen != sizeof(struct fip_wwn_desc))
861 goto len_err;
862 wwn = (struct fip_wwn_desc *)desc;
863 fcf->switch_name = get_unaligned_be64(&wwn->fd_wwn);
Bhanu Prakash Gollapudi0a9c5d32010-06-11 16:44:25 -0700864 desc_mask &= ~BIT(FIP_DT_NAME);
Joe Eykholt97c83892009-03-17 11:42:40 -0700865 break;
866 case FIP_DT_FAB:
867 if (dlen != sizeof(struct fip_fab_desc))
868 goto len_err;
869 fab = (struct fip_fab_desc *)desc;
870 fcf->fabric_name = get_unaligned_be64(&fab->fd_wwn);
871 fcf->vfid = ntohs(fab->fd_vfid);
872 fcf->fc_map = ntoh24(fab->fd_map);
Bhanu Prakash Gollapudi0a9c5d32010-06-11 16:44:25 -0700873 desc_mask &= ~BIT(FIP_DT_FAB);
Joe Eykholt97c83892009-03-17 11:42:40 -0700874 break;
875 case FIP_DT_FKA:
876 if (dlen != sizeof(struct fip_fka_desc))
877 goto len_err;
878 fka = (struct fip_fka_desc *)desc;
Yi Zou8cdffdc2009-11-20 14:54:57 -0800879 if (fka->fd_flags & FIP_FKA_ADV_D)
880 fcf->fd_flags = 1;
Joe Eykholt97c83892009-03-17 11:42:40 -0700881 t = ntohl(fka->fd_fka_period);
882 if (t >= FCOE_CTLR_MIN_FKA)
883 fcf->fka_period = msecs_to_jiffies(t);
Bhanu Prakash Gollapudi0a9c5d32010-06-11 16:44:25 -0700884 desc_mask &= ~BIT(FIP_DT_FKA);
Joe Eykholt97c83892009-03-17 11:42:40 -0700885 break;
886 case FIP_DT_MAP_OUI:
887 case FIP_DT_FCOE_SIZE:
888 case FIP_DT_FLOGI:
889 case FIP_DT_FDISC:
890 case FIP_DT_LOGO:
891 case FIP_DT_ELP:
892 default:
Joe Eykholt0f51c2e2009-11-03 11:48:16 -0800893 LIBFCOE_FIP_DBG(fip, "unexpected descriptor type %x "
Robert Love650bd122009-06-10 15:31:05 -0700894 "in FIP adv\n", desc->fip_dtype);
Joe Eykholt97c83892009-03-17 11:42:40 -0700895 /* standard says ignore unknown descriptors >= 128 */
896 if (desc->fip_dtype < FIP_DT_VENDOR_BASE)
897 return -EINVAL;
Bhanu Prakash Gollapudi1508f3ec2010-06-11 16:43:38 -0700898 break;
Joe Eykholt97c83892009-03-17 11:42:40 -0700899 }
900 desc = (struct fip_desc *)((char *)desc + dlen);
901 rlen -= dlen;
902 }
903 if (!fcf->fc_map || (fcf->fc_map & 0x10000))
904 return -EINVAL;
Vasu Dev240778e2010-07-20 15:21:33 -0700905 if (!fcf->switch_name)
Joe Eykholt97c83892009-03-17 11:42:40 -0700906 return -EINVAL;
Bhanu Prakash Gollapudi0a9c5d32010-06-11 16:44:25 -0700907 if (desc_mask) {
908 LIBFCOE_FIP_DBG(fip, "adv missing descriptors mask %x\n",
909 desc_mask);
910 return -EINVAL;
911 }
Joe Eykholt97c83892009-03-17 11:42:40 -0700912 return 0;
913
914len_err:
Joe Eykholt0f51c2e2009-11-03 11:48:16 -0800915 LIBFCOE_FIP_DBG(fip, "FIP length error in descriptor type %x len %zu\n",
Robert Love650bd122009-06-10 15:31:05 -0700916 desc->fip_dtype, dlen);
Joe Eykholt97c83892009-03-17 11:42:40 -0700917 return -EINVAL;
918}
919
920/**
Robert Love70b51aa2009-11-03 11:47:45 -0800921 * fcoe_ctlr_recv_adv() - Handle an incoming advertisement
922 * @fip: The FCoE controller receiving the advertisement
923 * @skb: The received FIP packet
Joe Eykholt97c83892009-03-17 11:42:40 -0700924 */
925static void fcoe_ctlr_recv_adv(struct fcoe_ctlr *fip, struct sk_buff *skb)
926{
927 struct fcoe_fcf *fcf;
928 struct fcoe_fcf new;
929 struct fcoe_fcf *found;
930 unsigned long sol_tov = msecs_to_jiffies(FCOE_CTRL_SOL_TOV);
931 int first = 0;
932 int mtu_valid;
933
Joe Eykholt0f51c2e2009-11-03 11:48:16 -0800934 if (fcoe_ctlr_parse_adv(fip, skb, &new))
Joe Eykholt97c83892009-03-17 11:42:40 -0700935 return;
936
Joe Eykholtfdb068c2010-07-20 15:19:47 -0700937 mutex_lock(&fip->ctlr_mutex);
Joe Eykholt97c83892009-03-17 11:42:40 -0700938 first = list_empty(&fip->fcfs);
939 found = NULL;
940 list_for_each_entry(fcf, &fip->fcfs, list) {
941 if (fcf->switch_name == new.switch_name &&
942 fcf->fabric_name == new.fabric_name &&
943 fcf->fc_map == new.fc_map &&
944 compare_ether_addr(fcf->fcf_mac, new.fcf_mac) == 0) {
945 found = fcf;
946 break;
947 }
948 }
949 if (!found) {
950 if (fip->fcf_count >= FCOE_CTLR_FCF_LIMIT)
951 goto out;
952
953 fcf = kmalloc(sizeof(*fcf), GFP_ATOMIC);
954 if (!fcf)
955 goto out;
956
957 fip->fcf_count++;
958 memcpy(fcf, &new, sizeof(new));
959 list_add(&fcf->list, &fip->fcfs);
960 } else {
961 /*
Joe Eykholtc600fea2010-06-11 16:44:20 -0700962 * Update the FCF's keep-alive descriptor flags.
963 * Other flag changes from new advertisements are
964 * ignored after a solicited advertisement is
965 * received and the FCF is selectable (usable).
Joe Eykholt97c83892009-03-17 11:42:40 -0700966 */
Joe Eykholtc600fea2010-06-11 16:44:20 -0700967 fcf->fd_flags = new.fd_flags;
968 if (!fcoe_ctlr_fcf_usable(fcf))
969 fcf->flags = new.flags;
970
Joe Eykholtd99ee452010-06-11 16:44:15 -0700971 if (fcf == fip->sel_fcf && !fcf->fd_flags) {
Joe Eykholt97c83892009-03-17 11:42:40 -0700972 fip->ctlr_ka_time -= fcf->fka_period;
973 fip->ctlr_ka_time += new.fka_period;
974 if (time_before(fip->ctlr_ka_time, fip->timer.expires))
975 mod_timer(&fip->timer, fip->ctlr_ka_time);
Joe Eykholtc600fea2010-06-11 16:44:20 -0700976 }
Joe Eykholt97c83892009-03-17 11:42:40 -0700977 fcf->fka_period = new.fka_period;
978 memcpy(fcf->fcf_mac, new.fcf_mac, ETH_ALEN);
979 }
980 mtu_valid = fcoe_ctlr_mtu_valid(fcf);
981 fcf->time = jiffies;
Joe Eykholt9069f5c2010-11-30 16:20:02 -0800982 if (!found)
983 LIBFCOE_FIP_DBG(fip, "New FCF fab %16.16llx mac %pM\n",
984 fcf->fabric_name, fcf->fcf_mac);
Joe Eykholt97c83892009-03-17 11:42:40 -0700985
986 /*
987 * If this advertisement is not solicited and our max receive size
988 * hasn't been verified, send a solicited advertisement.
989 */
990 if (!mtu_valid)
991 fcoe_ctlr_solicit(fip, fcf);
992
993 /*
994 * If its been a while since we did a solicit, and this is
995 * the first advertisement we've received, do a multicast
996 * solicitation to gather as many advertisements as we can
997 * before selection occurs.
998 */
999 if (first && time_after(jiffies, fip->sol_time + sol_tov))
1000 fcoe_ctlr_solicit(fip, NULL);
1001
1002 /*
1003 * If this is the first validated FCF, note the time and
1004 * set a timer to trigger selection.
1005 */
Joe Eykholtd99ee452010-06-11 16:44:15 -07001006 if (mtu_valid && !fip->sel_fcf && fcoe_ctlr_fcf_usable(fcf)) {
Joe Eykholt97c83892009-03-17 11:42:40 -07001007 fip->sel_time = jiffies +
Robert Love70b51aa2009-11-03 11:47:45 -08001008 msecs_to_jiffies(FCOE_CTLR_START_DELAY);
Joe Eykholt97c83892009-03-17 11:42:40 -07001009 if (!timer_pending(&fip->timer) ||
1010 time_before(fip->sel_time, fip->timer.expires))
1011 mod_timer(&fip->timer, fip->sel_time);
1012 }
1013out:
Joe Eykholtfdb068c2010-07-20 15:19:47 -07001014 mutex_unlock(&fip->ctlr_mutex);
Joe Eykholt97c83892009-03-17 11:42:40 -07001015}
1016
1017/**
Robert Love70b51aa2009-11-03 11:47:45 -08001018 * fcoe_ctlr_recv_els() - Handle an incoming FIP encapsulated ELS frame
1019 * @fip: The FCoE controller which received the packet
1020 * @skb: The received FIP packet
Joe Eykholt97c83892009-03-17 11:42:40 -07001021 */
1022static void fcoe_ctlr_recv_els(struct fcoe_ctlr *fip, struct sk_buff *skb)
1023{
Robert Love70b51aa2009-11-03 11:47:45 -08001024 struct fc_lport *lport = fip->lp;
Joe Eykholt97c83892009-03-17 11:42:40 -07001025 struct fip_header *fiph;
Chris Leech11b56182009-11-03 11:46:29 -08001026 struct fc_frame *fp = (struct fc_frame *)skb;
Joe Eykholt97c83892009-03-17 11:42:40 -07001027 struct fc_frame_header *fh = NULL;
1028 struct fip_desc *desc;
1029 struct fip_encaps *els;
1030 struct fcoe_dev_stats *stats;
1031 enum fip_desc_type els_dtype = 0;
1032 u8 els_op;
1033 u8 sub;
1034 u8 granted_mac[ETH_ALEN] = { 0 };
1035 size_t els_len = 0;
1036 size_t rlen;
1037 size_t dlen;
Bhanu Prakash Gollapudibe613312010-06-11 16:44:36 -07001038 u32 desc_mask = 0;
1039 u32 desc_cnt = 0;
Joe Eykholt97c83892009-03-17 11:42:40 -07001040
1041 fiph = (struct fip_header *)skb->data;
1042 sub = fiph->fip_subcode;
1043 if (sub != FIP_SC_REQ && sub != FIP_SC_REP)
1044 goto drop;
1045
1046 rlen = ntohs(fiph->fip_dl_len) * 4;
1047 if (rlen + sizeof(*fiph) > skb->len)
1048 goto drop;
1049
1050 desc = (struct fip_desc *)(fiph + 1);
1051 while (rlen > 0) {
Bhanu Prakash Gollapudibe613312010-06-11 16:44:36 -07001052 desc_cnt++;
Joe Eykholt97c83892009-03-17 11:42:40 -07001053 dlen = desc->fip_dlen * FIP_BPW;
1054 if (dlen < sizeof(*desc) || dlen > rlen)
1055 goto drop;
Bhanu Prakash Gollapudi0a9c5d32010-06-11 16:44:25 -07001056 /* Drop ELS if there are duplicate critical descriptors */
1057 if (desc->fip_dtype < 32) {
Bhanu Prakash Gollapudibe613312010-06-11 16:44:36 -07001058 if (desc_mask & 1U << desc->fip_dtype) {
Bhanu Prakash Gollapudi0a9c5d32010-06-11 16:44:25 -07001059 LIBFCOE_FIP_DBG(fip, "Duplicate Critical "
1060 "Descriptors in FIP ELS\n");
1061 goto drop;
1062 }
Bhanu Prakash Gollapudibe613312010-06-11 16:44:36 -07001063 desc_mask |= (1 << desc->fip_dtype);
Bhanu Prakash Gollapudi0a9c5d32010-06-11 16:44:25 -07001064 }
Joe Eykholt97c83892009-03-17 11:42:40 -07001065 switch (desc->fip_dtype) {
1066 case FIP_DT_MAC:
Bhanu Prakash Gollapudibe613312010-06-11 16:44:36 -07001067 if (desc_cnt == 1) {
1068 LIBFCOE_FIP_DBG(fip, "FIP descriptors "
1069 "received out of order\n");
1070 goto drop;
1071 }
1072
Joe Eykholt97c83892009-03-17 11:42:40 -07001073 if (dlen != sizeof(struct fip_mac_desc))
1074 goto len_err;
1075 memcpy(granted_mac,
1076 ((struct fip_mac_desc *)desc)->fd_mac,
1077 ETH_ALEN);
Joe Eykholt97c83892009-03-17 11:42:40 -07001078 break;
1079 case FIP_DT_FLOGI:
1080 case FIP_DT_FDISC:
1081 case FIP_DT_LOGO:
1082 case FIP_DT_ELP:
Bhanu Prakash Gollapudibe613312010-06-11 16:44:36 -07001083 if (desc_cnt != 1) {
1084 LIBFCOE_FIP_DBG(fip, "FIP descriptors "
1085 "received out of order\n");
1086 goto drop;
1087 }
Joe Eykholt97c83892009-03-17 11:42:40 -07001088 if (fh)
1089 goto drop;
1090 if (dlen < sizeof(*els) + sizeof(*fh) + 1)
1091 goto len_err;
1092 els_len = dlen - sizeof(*els);
1093 els = (struct fip_encaps *)desc;
1094 fh = (struct fc_frame_header *)(els + 1);
1095 els_dtype = desc->fip_dtype;
1096 break;
1097 default:
Joe Eykholt0f51c2e2009-11-03 11:48:16 -08001098 LIBFCOE_FIP_DBG(fip, "unexpected descriptor type %x "
Robert Love650bd122009-06-10 15:31:05 -07001099 "in FIP adv\n", desc->fip_dtype);
Joe Eykholt97c83892009-03-17 11:42:40 -07001100 /* standard says ignore unknown descriptors >= 128 */
1101 if (desc->fip_dtype < FIP_DT_VENDOR_BASE)
1102 goto drop;
Bhanu Prakash Gollapudibe613312010-06-11 16:44:36 -07001103 if (desc_cnt <= 2) {
1104 LIBFCOE_FIP_DBG(fip, "FIP descriptors "
1105 "received out of order\n");
1106 goto drop;
1107 }
Bhanu Prakash Gollapudi1508f3ec2010-06-11 16:43:38 -07001108 break;
Joe Eykholt97c83892009-03-17 11:42:40 -07001109 }
1110 desc = (struct fip_desc *)((char *)desc + dlen);
1111 rlen -= dlen;
1112 }
1113
1114 if (!fh)
1115 goto drop;
1116 els_op = *(u8 *)(fh + 1);
1117
Joe Eykholte10f8c62010-07-20 15:20:30 -07001118 if ((els_dtype == FIP_DT_FLOGI || els_dtype == FIP_DT_FDISC) &&
Joe Eykholt794d98e2010-11-30 16:19:56 -08001119 sub == FIP_SC_REP && fip->mode != FIP_MODE_VN2VN) {
1120 if (els_op == ELS_LS_ACC) {
1121 if (!is_valid_ether_addr(granted_mac)) {
1122 LIBFCOE_FIP_DBG(fip,
1123 "Invalid MAC address %pM in FIP ELS\n",
1124 granted_mac);
1125 goto drop;
1126 }
1127 memcpy(fr_cb(fp)->granted_mac, granted_mac, ETH_ALEN);
Joe Eykholte10f8c62010-07-20 15:20:30 -07001128
Joe Eykholt794d98e2010-11-30 16:19:56 -08001129 if (fip->flogi_oxid == ntohs(fh->fh_ox_id)) {
1130 fip->flogi_oxid = FC_XID_UNKNOWN;
1131 if (els_dtype == FIP_DT_FLOGI)
1132 fcoe_ctlr_announce(fip);
1133 }
1134 } else if (els_dtype == FIP_DT_FLOGI &&
1135 !fcoe_ctlr_flogi_retry(fip))
1136 goto drop; /* retrying FLOGI so drop reject */
Joe Eykholte10f8c62010-07-20 15:20:30 -07001137 }
Joe Eykholt97c83892009-03-17 11:42:40 -07001138
Bhanu Prakash Gollapudibe613312010-06-11 16:44:36 -07001139 if ((desc_cnt == 0) || ((els_op != ELS_LS_RJT) &&
1140 (!(1U << FIP_DT_MAC & desc_mask)))) {
1141 LIBFCOE_FIP_DBG(fip, "Missing critical descriptors "
1142 "in FIP ELS\n");
1143 goto drop;
1144 }
1145
Joe Eykholt97c83892009-03-17 11:42:40 -07001146 /*
1147 * Convert skb into an fc_frame containing only the ELS.
1148 */
1149 skb_pull(skb, (u8 *)fh - skb->data);
1150 skb_trim(skb, els_len);
1151 fp = (struct fc_frame *)skb;
1152 fc_frame_init(fp);
1153 fr_sof(fp) = FC_SOF_I3;
1154 fr_eof(fp) = FC_EOF_T;
Robert Love70b51aa2009-11-03 11:47:45 -08001155 fr_dev(fp) = lport;
Joe Eykholte10f8c62010-07-20 15:20:30 -07001156 fr_encaps(fp) = els_dtype;
Joe Eykholt97c83892009-03-17 11:42:40 -07001157
Joe Eykholtf018b732010-03-12 16:08:55 -08001158 stats = per_cpu_ptr(lport->dev_stats, get_cpu());
Joe Eykholt97c83892009-03-17 11:42:40 -07001159 stats->RxFrames++;
1160 stats->RxWords += skb->len / FIP_BPW;
Joe Eykholtf018b732010-03-12 16:08:55 -08001161 put_cpu();
Joe Eykholt97c83892009-03-17 11:42:40 -07001162
Robert Love70b51aa2009-11-03 11:47:45 -08001163 fc_exch_recv(lport, fp);
Joe Eykholt97c83892009-03-17 11:42:40 -07001164 return;
1165
1166len_err:
Joe Eykholt0f51c2e2009-11-03 11:48:16 -08001167 LIBFCOE_FIP_DBG(fip, "FIP length error in descriptor type %x len %zu\n",
Robert Love650bd122009-06-10 15:31:05 -07001168 desc->fip_dtype, dlen);
Joe Eykholt97c83892009-03-17 11:42:40 -07001169drop:
1170 kfree_skb(skb);
1171}
1172
1173/**
Robert Love70b51aa2009-11-03 11:47:45 -08001174 * fcoe_ctlr_recv_els() - Handle an incoming link reset frame
1175 * @fip: The FCoE controller that received the frame
1176 * @fh: The received FIP header
Joe Eykholt97c83892009-03-17 11:42:40 -07001177 *
1178 * There may be multiple VN_Port descriptors.
1179 * The overall length has already been checked.
1180 */
1181static void fcoe_ctlr_recv_clr_vlink(struct fcoe_ctlr *fip,
Robert Love70b51aa2009-11-03 11:47:45 -08001182 struct fip_header *fh)
Joe Eykholt97c83892009-03-17 11:42:40 -07001183{
1184 struct fip_desc *desc;
1185 struct fip_mac_desc *mp;
1186 struct fip_wwn_desc *wp;
1187 struct fip_vn_desc *vp;
1188 size_t rlen;
1189 size_t dlen;
1190 struct fcoe_fcf *fcf = fip->sel_fcf;
Robert Love70b51aa2009-11-03 11:47:45 -08001191 struct fc_lport *lport = fip->lp;
Bhanu Prakash Gollapudi5550fda2010-06-11 16:44:31 -07001192 struct fc_lport *vn_port = NULL;
1193 u32 desc_mask;
1194 int is_vn_port = 0;
Joe Eykholt97c83892009-03-17 11:42:40 -07001195
Joe Eykholt0f51c2e2009-11-03 11:48:16 -08001196 LIBFCOE_FIP_DBG(fip, "Clear Virtual Link received\n");
Robert Loved29510a2010-05-07 15:18:30 -07001197
Robert Love7b2787e2010-05-07 15:18:41 -07001198 if (!fcf || !lport->port_id)
Joe Eykholt97c83892009-03-17 11:42:40 -07001199 return;
1200
1201 /*
1202 * mask of required descriptors. Validating each one clears its bit.
1203 */
1204 desc_mask = BIT(FIP_DT_MAC) | BIT(FIP_DT_NAME) | BIT(FIP_DT_VN_ID);
1205
1206 rlen = ntohs(fh->fip_dl_len) * FIP_BPW;
1207 desc = (struct fip_desc *)(fh + 1);
1208 while (rlen >= sizeof(*desc)) {
1209 dlen = desc->fip_dlen * FIP_BPW;
1210 if (dlen > rlen)
1211 return;
Bhanu Prakash Gollapudi0a9c5d32010-06-11 16:44:25 -07001212 /* Drop CVL if there are duplicate critical descriptors */
1213 if ((desc->fip_dtype < 32) &&
1214 !(desc_mask & 1U << desc->fip_dtype)) {
1215 LIBFCOE_FIP_DBG(fip, "Duplicate Critical "
1216 "Descriptors in FIP CVL\n");
1217 return;
1218 }
Joe Eykholt97c83892009-03-17 11:42:40 -07001219 switch (desc->fip_dtype) {
1220 case FIP_DT_MAC:
1221 mp = (struct fip_mac_desc *)desc;
1222 if (dlen < sizeof(*mp))
1223 return;
1224 if (compare_ether_addr(mp->fd_mac, fcf->fcf_mac))
1225 return;
1226 desc_mask &= ~BIT(FIP_DT_MAC);
1227 break;
1228 case FIP_DT_NAME:
1229 wp = (struct fip_wwn_desc *)desc;
1230 if (dlen < sizeof(*wp))
1231 return;
1232 if (get_unaligned_be64(&wp->fd_wwn) != fcf->switch_name)
1233 return;
1234 desc_mask &= ~BIT(FIP_DT_NAME);
1235 break;
1236 case FIP_DT_VN_ID:
1237 vp = (struct fip_vn_desc *)desc;
1238 if (dlen < sizeof(*vp))
1239 return;
1240 if (compare_ether_addr(vp->fd_mac,
Robert Love70b51aa2009-11-03 11:47:45 -08001241 fip->get_src_addr(lport)) == 0 &&
1242 get_unaligned_be64(&vp->fd_wwpn) == lport->wwpn &&
Bhanu Prakash Gollapudi5550fda2010-06-11 16:44:31 -07001243 ntoh24(vp->fd_fc_id) == lport->port_id) {
Joe Eykholt97c83892009-03-17 11:42:40 -07001244 desc_mask &= ~BIT(FIP_DT_VN_ID);
Bhanu Prakash Gollapudi5550fda2010-06-11 16:44:31 -07001245 break;
1246 }
1247 /* check if clr_vlink is for NPIV port */
1248 mutex_lock(&lport->lp_mutex);
1249 list_for_each_entry(vn_port, &lport->vports, list) {
1250 if (compare_ether_addr(vp->fd_mac,
1251 fip->get_src_addr(vn_port)) == 0 &&
1252 (get_unaligned_be64(&vp->fd_wwpn)
1253 == vn_port->wwpn) &&
1254 (ntoh24(vp->fd_fc_id) ==
1255 fc_host_port_id(vn_port->host))) {
1256 desc_mask &= ~BIT(FIP_DT_VN_ID);
1257 is_vn_port = 1;
1258 break;
1259 }
1260 }
1261 mutex_unlock(&lport->lp_mutex);
1262
Joe Eykholt97c83892009-03-17 11:42:40 -07001263 break;
1264 default:
1265 /* standard says ignore unknown descriptors >= 128 */
1266 if (desc->fip_dtype < FIP_DT_VENDOR_BASE)
1267 return;
1268 break;
1269 }
1270 desc = (struct fip_desc *)((char *)desc + dlen);
1271 rlen -= dlen;
1272 }
1273
1274 /*
1275 * reset only if all required descriptors were present and valid.
1276 */
1277 if (desc_mask) {
Joe Eykholt0f51c2e2009-11-03 11:48:16 -08001278 LIBFCOE_FIP_DBG(fip, "missing descriptors mask %x\n",
1279 desc_mask);
Joe Eykholt97c83892009-03-17 11:42:40 -07001280 } else {
Joe Eykholt0f51c2e2009-11-03 11:48:16 -08001281 LIBFCOE_FIP_DBG(fip, "performing Clear Virtual Link\n");
Joe Eykholtdd42dac2009-11-03 11:48:27 -08001282
Bhanu Prakash Gollapudi5550fda2010-06-11 16:44:31 -07001283 if (is_vn_port)
1284 fc_lport_reset(vn_port);
1285 else {
Joe Eykholtfdb068c2010-07-20 15:19:47 -07001286 mutex_lock(&fip->ctlr_mutex);
Bhanu Prakash Gollapudi5550fda2010-06-11 16:44:31 -07001287 per_cpu_ptr(lport->dev_stats,
Joe Eykholtfdb068c2010-07-20 15:19:47 -07001288 get_cpu())->VLinkFailureCount++;
1289 put_cpu();
Bhanu Prakash Gollapudi5550fda2010-06-11 16:44:31 -07001290 fcoe_ctlr_reset(fip);
Joe Eykholtfdb068c2010-07-20 15:19:47 -07001291 mutex_unlock(&fip->ctlr_mutex);
Joe Eykholtdd42dac2009-11-03 11:48:27 -08001292
Bhanu Prakash Gollapudi5550fda2010-06-11 16:44:31 -07001293 fc_lport_reset(fip->lp);
1294 fcoe_ctlr_solicit(fip, NULL);
1295 }
Joe Eykholt97c83892009-03-17 11:42:40 -07001296 }
1297}
1298
1299/**
Robert Love70b51aa2009-11-03 11:47:45 -08001300 * fcoe_ctlr_recv() - Receive a FIP packet
1301 * @fip: The FCoE controller that received the packet
1302 * @skb: The received FIP packet
Joe Eykholt97c83892009-03-17 11:42:40 -07001303 *
Joe Eykholt1f4aed82009-11-03 11:48:22 -08001304 * This may be called from either NET_RX_SOFTIRQ or IRQ.
Joe Eykholt97c83892009-03-17 11:42:40 -07001305 */
1306void fcoe_ctlr_recv(struct fcoe_ctlr *fip, struct sk_buff *skb)
1307{
Joe Eykholt1f4aed82009-11-03 11:48:22 -08001308 skb_queue_tail(&fip->fip_recv_list, skb);
Joe Eykholt97c83892009-03-17 11:42:40 -07001309 schedule_work(&fip->recv_work);
1310}
1311EXPORT_SYMBOL(fcoe_ctlr_recv);
1312
1313/**
Robert Love70b51aa2009-11-03 11:47:45 -08001314 * fcoe_ctlr_recv_handler() - Receive a FIP frame
1315 * @fip: The FCoE controller that received the frame
1316 * @skb: The received FIP frame
Joe Eykholt97c83892009-03-17 11:42:40 -07001317 *
1318 * Returns non-zero if the frame is dropped.
1319 */
1320static int fcoe_ctlr_recv_handler(struct fcoe_ctlr *fip, struct sk_buff *skb)
1321{
1322 struct fip_header *fiph;
1323 struct ethhdr *eh;
1324 enum fip_state state;
1325 u16 op;
1326 u8 sub;
1327
1328 if (skb_linearize(skb))
1329 goto drop;
1330 if (skb->len < sizeof(*fiph))
1331 goto drop;
1332 eh = eth_hdr(skb);
Joe Eykholte10f8c62010-07-20 15:20:30 -07001333 if (fip->mode == FIP_MODE_VN2VN) {
1334 if (compare_ether_addr(eh->h_dest, fip->ctl_src_addr) &&
1335 compare_ether_addr(eh->h_dest, fcoe_all_vn2vn) &&
1336 compare_ether_addr(eh->h_dest, fcoe_all_p2p))
1337 goto drop;
1338 } else if (compare_ether_addr(eh->h_dest, fip->ctl_src_addr) &&
1339 compare_ether_addr(eh->h_dest, fcoe_all_enode))
Joe Eykholt97c83892009-03-17 11:42:40 -07001340 goto drop;
1341 fiph = (struct fip_header *)skb->data;
1342 op = ntohs(fiph->fip_op);
1343 sub = fiph->fip_subcode;
1344
Joe Eykholt97c83892009-03-17 11:42:40 -07001345 if (FIP_VER_DECAPS(fiph->fip_ver) != FIP_VER)
1346 goto drop;
1347 if (ntohs(fiph->fip_dl_len) * FIP_BPW + sizeof(*fiph) > skb->len)
1348 goto drop;
1349
Joe Eykholtfdb068c2010-07-20 15:19:47 -07001350 mutex_lock(&fip->ctlr_mutex);
Joe Eykholt97c83892009-03-17 11:42:40 -07001351 state = fip->state;
1352 if (state == FIP_ST_AUTO) {
1353 fip->map_dest = 0;
Joe Eykholt9b651da2010-07-20 15:20:24 -07001354 fcoe_ctlr_set_state(fip, FIP_ST_ENABLED);
Joe Eykholt97c83892009-03-17 11:42:40 -07001355 state = FIP_ST_ENABLED;
Joe Eykholt0f51c2e2009-11-03 11:48:16 -08001356 LIBFCOE_FIP_DBG(fip, "Using FIP mode\n");
Joe Eykholt97c83892009-03-17 11:42:40 -07001357 }
Joe Eykholtfdb068c2010-07-20 15:19:47 -07001358 mutex_unlock(&fip->ctlr_mutex);
Joe Eykholte10f8c62010-07-20 15:20:30 -07001359
1360 if (fip->mode == FIP_MODE_VN2VN && op == FIP_OP_VN2VN)
1361 return fcoe_ctlr_vn_recv(fip, skb);
1362
1363 if (state != FIP_ST_ENABLED && state != FIP_ST_VNMP_UP &&
1364 state != FIP_ST_VNMP_CLAIM)
Joe Eykholt97c83892009-03-17 11:42:40 -07001365 goto drop;
1366
1367 if (op == FIP_OP_LS) {
1368 fcoe_ctlr_recv_els(fip, skb); /* consumes skb */
1369 return 0;
1370 }
Joe Eykholte10f8c62010-07-20 15:20:30 -07001371
1372 if (state != FIP_ST_ENABLED)
1373 goto drop;
1374
Joe Eykholt97c83892009-03-17 11:42:40 -07001375 if (op == FIP_OP_DISC && sub == FIP_SC_ADV)
1376 fcoe_ctlr_recv_adv(fip, skb);
1377 else if (op == FIP_OP_CTRL && sub == FIP_SC_CLR_VLINK)
1378 fcoe_ctlr_recv_clr_vlink(fip, fiph);
1379 kfree_skb(skb);
1380 return 0;
1381drop:
1382 kfree_skb(skb);
1383 return -1;
1384}
1385
1386/**
Robert Love70b51aa2009-11-03 11:47:45 -08001387 * fcoe_ctlr_select() - Select the best FCF (if possible)
1388 * @fip: The FCoE controller
Joe Eykholt97c83892009-03-17 11:42:40 -07001389 *
1390 * If there are conflicting advertisements, no FCF can be chosen.
1391 *
Joe Eykholt794d98e2010-11-30 16:19:56 -08001392 * If there is already a selected FCF, this will choose a better one or
1393 * an equivalent one that hasn't already been sent a FLOGI.
1394 *
Joe Eykholt97c83892009-03-17 11:42:40 -07001395 * Called with lock held.
1396 */
1397static void fcoe_ctlr_select(struct fcoe_ctlr *fip)
1398{
1399 struct fcoe_fcf *fcf;
Joe Eykholt794d98e2010-11-30 16:19:56 -08001400 struct fcoe_fcf *best = fip->sel_fcf;
Joe Eykholtb69ae0a2010-11-30 16:19:51 -08001401 struct fcoe_fcf *first;
1402
1403 first = list_first_entry(&fip->fcfs, struct fcoe_fcf, list);
Joe Eykholt97c83892009-03-17 11:42:40 -07001404
1405 list_for_each_entry(fcf, &fip->fcfs, list) {
Joe Eykholt9069f5c2010-11-30 16:20:02 -08001406 LIBFCOE_FIP_DBG(fip, "consider FCF fab %16.16llx "
1407 "VFID %d mac %pM map %x val %d "
1408 "sent %u pri %u\n",
1409 fcf->fabric_name, fcf->vfid, fcf->fcf_mac,
1410 fcf->fc_map, fcoe_ctlr_mtu_valid(fcf),
1411 fcf->flogi_sent, fcf->pri);
Joe Eykholtb69ae0a2010-11-30 16:19:51 -08001412 if (fcf->fabric_name != first->fabric_name ||
1413 fcf->vfid != first->vfid ||
1414 fcf->fc_map != first->fc_map) {
1415 LIBFCOE_FIP_DBG(fip, "Conflicting fabric, VFID, "
1416 "or FC-MAP\n");
1417 return NULL;
1418 }
Joe Eykholt794d98e2010-11-30 16:19:56 -08001419 if (fcf->flogi_sent)
1420 continue;
Joe Eykholt97c83892009-03-17 11:42:40 -07001421 if (!fcoe_ctlr_fcf_usable(fcf)) {
Chris Leech9f8f3aa2010-04-09 14:23:16 -07001422 LIBFCOE_FIP_DBG(fip, "FCF for fab %16.16llx "
1423 "map %x %svalid %savailable\n",
1424 fcf->fabric_name, fcf->fc_map,
1425 (fcf->flags & FIP_FL_SOL) ? "" : "in",
1426 (fcf->flags & FIP_FL_AVAIL) ?
1427 "" : "un");
Joe Eykholt97c83892009-03-17 11:42:40 -07001428 continue;
1429 }
Joe Eykholt794d98e2010-11-30 16:19:56 -08001430 if (!best || fcf->pri < best->pri || best->flogi_sent)
Joe Eykholt97c83892009-03-17 11:42:40 -07001431 best = fcf;
1432 }
1433 fip->sel_fcf = best;
Joe Eykholtc47036a2010-11-30 16:19:46 -08001434 if (best) {
Joe Eykholt9069f5c2010-11-30 16:20:02 -08001435 LIBFCOE_FIP_DBG(fip, "using FCF mac %pM\n", best->fcf_mac);
Joe Eykholtc47036a2010-11-30 16:19:46 -08001436 fip->port_ka_time = jiffies +
1437 msecs_to_jiffies(FIP_VN_KA_PERIOD);
1438 fip->ctlr_ka_time = jiffies + best->fka_period;
1439 if (time_before(fip->ctlr_ka_time, fip->timer.expires))
1440 mod_timer(&fip->timer, fip->ctlr_ka_time);
1441 }
Joe Eykholt97c83892009-03-17 11:42:40 -07001442}
1443
1444/**
Joe Eykholt794d98e2010-11-30 16:19:56 -08001445 * fcoe_ctlr_flogi_send_locked() - send FIP-encapsulated FLOGI to current FCF
1446 * @fip: The FCoE controller
1447 *
1448 * Returns non-zero error if it could not be sent.
1449 *
1450 * Called with ctlr_mutex and ctlr_lock held.
1451 * Caller must verify that fip->sel_fcf is not NULL.
1452 */
1453static int fcoe_ctlr_flogi_send_locked(struct fcoe_ctlr *fip)
1454{
1455 struct sk_buff *skb;
1456 struct sk_buff *skb_orig;
1457 struct fc_frame_header *fh;
1458 int error;
1459
1460 skb_orig = fip->flogi_req;
1461 if (!skb_orig)
1462 return -EINVAL;
1463
1464 /*
1465 * Clone and send the FLOGI request. If clone fails, use original.
1466 */
1467 skb = skb_clone(skb_orig, GFP_ATOMIC);
1468 if (!skb) {
1469 skb = skb_orig;
1470 fip->flogi_req = NULL;
1471 }
1472 fh = (struct fc_frame_header *)skb->data;
1473 error = fcoe_ctlr_encaps(fip, fip->lp, FIP_DT_FLOGI, skb,
1474 ntoh24(fh->fh_d_id));
1475 if (error) {
1476 kfree_skb(skb);
1477 return error;
1478 }
1479 fip->send(fip, skb);
1480 fip->sel_fcf->flogi_sent = 1;
1481 return 0;
1482}
1483
1484/**
1485 * fcoe_ctlr_flogi_retry() - resend FLOGI request to a new FCF if possible
1486 * @fip: The FCoE controller
1487 *
1488 * Returns non-zero error code if there's no FLOGI request to retry or
1489 * no alternate FCF available.
1490 */
1491static int fcoe_ctlr_flogi_retry(struct fcoe_ctlr *fip)
1492{
1493 struct fcoe_fcf *fcf;
1494 int error;
1495
1496 mutex_lock(&fip->ctlr_mutex);
1497 spin_lock_bh(&fip->ctlr_lock);
1498 LIBFCOE_FIP_DBG(fip, "re-sending FLOGI - reselect\n");
1499 fcoe_ctlr_select(fip);
1500 fcf = fip->sel_fcf;
1501 if (!fcf || fcf->flogi_sent) {
1502 kfree_skb(fip->flogi_req);
1503 fip->flogi_req = NULL;
1504 error = -ENOENT;
1505 } else {
1506 fcoe_ctlr_solicit(fip, NULL);
1507 error = fcoe_ctlr_flogi_send_locked(fip);
1508 }
1509 spin_unlock_bh(&fip->ctlr_lock);
1510 mutex_unlock(&fip->ctlr_mutex);
1511 return error;
1512}
1513
1514
1515/**
1516 * fcoe_ctlr_flogi_send() - Handle sending of FIP FLOGI.
1517 * @fip: The FCoE controller that timed out
1518 *
1519 * Done here because fcoe_ctlr_els_send() can't get mutex.
1520 *
1521 * Called with ctlr_mutex held. The caller must not hold ctlr_lock.
1522 */
1523static void fcoe_ctlr_flogi_send(struct fcoe_ctlr *fip)
1524{
1525 struct fcoe_fcf *fcf;
1526
1527 spin_lock_bh(&fip->ctlr_lock);
1528 fcf = fip->sel_fcf;
1529 if (!fcf || !fip->flogi_req_send)
1530 goto unlock;
1531
1532 LIBFCOE_FIP_DBG(fip, "sending FLOGI\n");
1533
1534 /*
1535 * If this FLOGI is being sent due to a timeout retry
1536 * to the same FCF as before, select a different FCF if possible.
1537 */
1538 if (fcf->flogi_sent) {
1539 LIBFCOE_FIP_DBG(fip, "sending FLOGI - reselect\n");
1540 fcoe_ctlr_select(fip);
1541 fcf = fip->sel_fcf;
1542 if (!fcf || fcf->flogi_sent) {
1543 LIBFCOE_FIP_DBG(fip, "sending FLOGI - clearing\n");
1544 list_for_each_entry(fcf, &fip->fcfs, list)
1545 fcf->flogi_sent = 0;
1546 fcoe_ctlr_select(fip);
1547 fcf = fip->sel_fcf;
1548 }
1549 }
1550 if (fcf) {
1551 fcoe_ctlr_flogi_send_locked(fip);
1552 fip->flogi_req_send = 0;
1553 } else /* XXX */
1554 LIBFCOE_FIP_DBG(fip, "No FCF selected - defer send\n");
1555unlock:
1556 spin_unlock_bh(&fip->ctlr_lock);
1557}
1558
1559/**
Robert Love70b51aa2009-11-03 11:47:45 -08001560 * fcoe_ctlr_timeout() - FIP timeout handler
1561 * @arg: The FCoE controller that timed out
Joe Eykholt97c83892009-03-17 11:42:40 -07001562 */
1563static void fcoe_ctlr_timeout(unsigned long arg)
1564{
1565 struct fcoe_ctlr *fip = (struct fcoe_ctlr *)arg;
Joe Eykholtfdb068c2010-07-20 15:19:47 -07001566
1567 schedule_work(&fip->timer_work);
1568}
1569
1570/**
1571 * fcoe_ctlr_timer_work() - Worker thread function for timer work
1572 * @work: Handle to a FCoE controller
1573 *
1574 * Ages FCFs. Triggers FCF selection if possible.
1575 * Sends keep-alives and resets.
1576 */
1577static void fcoe_ctlr_timer_work(struct work_struct *work)
1578{
1579 struct fcoe_ctlr *fip;
1580 struct fc_lport *vport;
1581 u8 *mac;
1582 u8 reset = 0;
1583 u8 send_ctlr_ka = 0;
1584 u8 send_port_ka = 0;
Joe Eykholt97c83892009-03-17 11:42:40 -07001585 struct fcoe_fcf *sel;
1586 struct fcoe_fcf *fcf;
Joe Eykholt8690cb82010-06-11 16:44:10 -07001587 unsigned long next_timer;
Joe Eykholt97c83892009-03-17 11:42:40 -07001588
Joe Eykholtfdb068c2010-07-20 15:19:47 -07001589 fip = container_of(work, struct fcoe_ctlr, timer_work);
Joe Eykholte10f8c62010-07-20 15:20:30 -07001590 if (fip->mode == FIP_MODE_VN2VN)
1591 return fcoe_ctlr_vn_timeout(fip);
Joe Eykholtfdb068c2010-07-20 15:19:47 -07001592 mutex_lock(&fip->ctlr_mutex);
Joe Eykholt97c83892009-03-17 11:42:40 -07001593 if (fip->state == FIP_ST_DISABLED) {
Joe Eykholtfdb068c2010-07-20 15:19:47 -07001594 mutex_unlock(&fip->ctlr_mutex);
Joe Eykholt97c83892009-03-17 11:42:40 -07001595 return;
1596 }
1597
1598 fcf = fip->sel_fcf;
Joe Eykholt8690cb82010-06-11 16:44:10 -07001599 next_timer = fcoe_ctlr_age_fcfs(fip);
Joe Eykholt97c83892009-03-17 11:42:40 -07001600
1601 sel = fip->sel_fcf;
Joe Eykholt8690cb82010-06-11 16:44:10 -07001602 if (!sel && fip->sel_time) {
1603 if (time_after_eq(jiffies, fip->sel_time)) {
1604 fcoe_ctlr_select(fip);
1605 sel = fip->sel_fcf;
1606 fip->sel_time = 0;
1607 } else if (time_after(next_timer, fip->sel_time))
1608 next_timer = fip->sel_time;
Joe Eykholt97c83892009-03-17 11:42:40 -07001609 }
1610
Joe Eykholt794d98e2010-11-30 16:19:56 -08001611 if (sel && fip->flogi_req_send)
1612 fcoe_ctlr_flogi_send(fip);
1613 else if (!sel && fcf)
1614 reset = 1;
Joe Eykholt97c83892009-03-17 11:42:40 -07001615
Yi Zou8cdffdc2009-11-20 14:54:57 -08001616 if (sel && !sel->fd_flags) {
Joe Eykholt97c83892009-03-17 11:42:40 -07001617 if (time_after_eq(jiffies, fip->ctlr_ka_time)) {
1618 fip->ctlr_ka_time = jiffies + sel->fka_period;
Joe Eykholtfdb068c2010-07-20 15:19:47 -07001619 send_ctlr_ka = 1;
Joe Eykholt97c83892009-03-17 11:42:40 -07001620 }
1621 if (time_after(next_timer, fip->ctlr_ka_time))
1622 next_timer = fip->ctlr_ka_time;
1623
1624 if (time_after_eq(jiffies, fip->port_ka_time)) {
Bhanu Prakash Gollapudif47dd852010-01-21 10:16:00 -08001625 fip->port_ka_time = jiffies +
Robert Love70b51aa2009-11-03 11:47:45 -08001626 msecs_to_jiffies(FIP_VN_KA_PERIOD);
Joe Eykholtfdb068c2010-07-20 15:19:47 -07001627 send_port_ka = 1;
Joe Eykholt97c83892009-03-17 11:42:40 -07001628 }
1629 if (time_after(next_timer, fip->port_ka_time))
1630 next_timer = fip->port_ka_time;
Joe Eykholt97c83892009-03-17 11:42:40 -07001631 }
Joe Eykholt8690cb82010-06-11 16:44:10 -07001632 if (!list_empty(&fip->fcfs))
1633 mod_timer(&fip->timer, next_timer);
Joe Eykholtfdb068c2010-07-20 15:19:47 -07001634 mutex_unlock(&fip->ctlr_mutex);
Joe Eykholt97c83892009-03-17 11:42:40 -07001635
Bhanu Prakash Gollapudi516a6482010-06-11 16:43:44 -07001636 if (reset) {
Joe Eykholtdd42dac2009-11-03 11:48:27 -08001637 fc_lport_reset(fip->lp);
Bhanu Prakash Gollapudi516a6482010-06-11 16:43:44 -07001638 /* restart things with a solicitation */
1639 fcoe_ctlr_solicit(fip, NULL);
1640 }
Chris Leech11b56182009-11-03 11:46:29 -08001641
Joe Eykholtfdb068c2010-07-20 15:19:47 -07001642 if (send_ctlr_ka)
Chris Leech11b56182009-11-03 11:46:29 -08001643 fcoe_ctlr_send_keep_alive(fip, NULL, 0, fip->ctl_src_addr);
Joe Eykholtfdb068c2010-07-20 15:19:47 -07001644
1645 if (send_port_ka) {
Chris Leech11b56182009-11-03 11:46:29 -08001646 mutex_lock(&fip->lp->lp_mutex);
1647 mac = fip->get_src_addr(fip->lp);
1648 fcoe_ctlr_send_keep_alive(fip, fip->lp, 1, mac);
1649 list_for_each_entry(vport, &fip->lp->vports, list) {
1650 mac = fip->get_src_addr(vport);
1651 fcoe_ctlr_send_keep_alive(fip, vport, 1, mac);
1652 }
1653 mutex_unlock(&fip->lp->lp_mutex);
1654 }
Joe Eykholt97c83892009-03-17 11:42:40 -07001655}
1656
1657/**
Robert Love70b51aa2009-11-03 11:47:45 -08001658 * fcoe_ctlr_recv_work() - Worker thread function for receiving FIP frames
1659 * @recv_work: Handle to a FCoE controller
Joe Eykholt97c83892009-03-17 11:42:40 -07001660 */
1661static void fcoe_ctlr_recv_work(struct work_struct *recv_work)
1662{
1663 struct fcoe_ctlr *fip;
1664 struct sk_buff *skb;
1665
1666 fip = container_of(recv_work, struct fcoe_ctlr, recv_work);
Joe Eykholt1f4aed82009-11-03 11:48:22 -08001667 while ((skb = skb_dequeue(&fip->fip_recv_list)))
Joe Eykholt97c83892009-03-17 11:42:40 -07001668 fcoe_ctlr_recv_handler(fip, skb);
Joe Eykholt97c83892009-03-17 11:42:40 -07001669}
1670
1671/**
Joe Eykholt386309c2009-11-03 11:49:16 -08001672 * fcoe_ctlr_recv_flogi() - Snoop pre-FIP receipt of FLOGI response
Robert Love70b51aa2009-11-03 11:47:45 -08001673 * @fip: The FCoE controller
1674 * @fp: The FC frame to snoop
Joe Eykholt97c83892009-03-17 11:42:40 -07001675 *
1676 * Snoop potential response to FLOGI or even incoming FLOGI.
1677 *
1678 * The caller has checked that we are waiting for login as indicated
1679 * by fip->flogi_oxid != FC_XID_UNKNOWN.
1680 *
1681 * The caller is responsible for freeing the frame.
Joe Eykholt386309c2009-11-03 11:49:16 -08001682 * Fill in the granted_mac address.
Joe Eykholt97c83892009-03-17 11:42:40 -07001683 *
1684 * Return non-zero if the frame should not be delivered to libfc.
1685 */
Chris Leech11b56182009-11-03 11:46:29 -08001686int fcoe_ctlr_recv_flogi(struct fcoe_ctlr *fip, struct fc_lport *lport,
Joe Eykholt386309c2009-11-03 11:49:16 -08001687 struct fc_frame *fp)
Joe Eykholt97c83892009-03-17 11:42:40 -07001688{
1689 struct fc_frame_header *fh;
1690 u8 op;
Joe Eykholt386309c2009-11-03 11:49:16 -08001691 u8 *sa;
Joe Eykholt97c83892009-03-17 11:42:40 -07001692
Joe Eykholt386309c2009-11-03 11:49:16 -08001693 sa = eth_hdr(&fp->skb)->h_source;
Joe Eykholt97c83892009-03-17 11:42:40 -07001694 fh = fc_frame_header_get(fp);
1695 if (fh->fh_type != FC_TYPE_ELS)
1696 return 0;
1697
1698 op = fc_frame_payload_op(fp);
1699 if (op == ELS_LS_ACC && fh->fh_r_ctl == FC_RCTL_ELS_REP &&
1700 fip->flogi_oxid == ntohs(fh->fh_ox_id)) {
1701
Joe Eykholtfdb068c2010-07-20 15:19:47 -07001702 mutex_lock(&fip->ctlr_mutex);
Joe Eykholt97c83892009-03-17 11:42:40 -07001703 if (fip->state != FIP_ST_AUTO && fip->state != FIP_ST_NON_FIP) {
Joe Eykholtfdb068c2010-07-20 15:19:47 -07001704 mutex_unlock(&fip->ctlr_mutex);
Joe Eykholt97c83892009-03-17 11:42:40 -07001705 return -EINVAL;
1706 }
Joe Eykholt9b651da2010-07-20 15:20:24 -07001707 fcoe_ctlr_set_state(fip, FIP_ST_NON_FIP);
Joe Eykholt0f51c2e2009-11-03 11:48:16 -08001708 LIBFCOE_FIP_DBG(fip,
1709 "received FLOGI LS_ACC using non-FIP mode\n");
Joe Eykholt97c83892009-03-17 11:42:40 -07001710
1711 /*
1712 * FLOGI accepted.
1713 * If the src mac addr is FC_OUI-based, then we mark the
1714 * address_mode flag to use FC_OUI-based Ethernet DA.
1715 * Otherwise we use the FCoE gateway addr
1716 */
1717 if (!compare_ether_addr(sa, (u8[6])FC_FCOE_FLOGI_MAC)) {
Joe Eykholtcd229e42010-07-20 15:20:40 -07001718 fcoe_ctlr_map_dest(fip);
Joe Eykholt97c83892009-03-17 11:42:40 -07001719 } else {
1720 memcpy(fip->dest_addr, sa, ETH_ALEN);
1721 fip->map_dest = 0;
1722 }
1723 fip->flogi_oxid = FC_XID_UNKNOWN;
Joe Eykholtfdb068c2010-07-20 15:19:47 -07001724 mutex_unlock(&fip->ctlr_mutex);
Joe Eykholt386309c2009-11-03 11:49:16 -08001725 fc_fcoe_set_mac(fr_cb(fp)->granted_mac, fh->fh_d_id);
Joe Eykholt97c83892009-03-17 11:42:40 -07001726 } else if (op == ELS_FLOGI && fh->fh_r_ctl == FC_RCTL_ELS_REQ && sa) {
1727 /*
1728 * Save source MAC for point-to-point responses.
1729 */
Joe Eykholtfdb068c2010-07-20 15:19:47 -07001730 mutex_lock(&fip->ctlr_mutex);
Joe Eykholt97c83892009-03-17 11:42:40 -07001731 if (fip->state == FIP_ST_AUTO || fip->state == FIP_ST_NON_FIP) {
1732 memcpy(fip->dest_addr, sa, ETH_ALEN);
1733 fip->map_dest = 0;
Joe Eykholte49bf612010-03-12 16:07:57 -08001734 if (fip->state == FIP_ST_AUTO)
1735 LIBFCOE_FIP_DBG(fip, "received non-FIP FLOGI. "
1736 "Setting non-FIP mode\n");
Joe Eykholt9b651da2010-07-20 15:20:24 -07001737 fcoe_ctlr_set_state(fip, FIP_ST_NON_FIP);
Joe Eykholt97c83892009-03-17 11:42:40 -07001738 }
Joe Eykholtfdb068c2010-07-20 15:19:47 -07001739 mutex_unlock(&fip->ctlr_mutex);
Joe Eykholt97c83892009-03-17 11:42:40 -07001740 }
1741 return 0;
1742}
1743EXPORT_SYMBOL(fcoe_ctlr_recv_flogi);
1744
Vasu Dev5e80f7f2009-03-17 11:42:18 -07001745/**
Robert Love70b51aa2009-11-03 11:47:45 -08001746 * fcoe_wwn_from_mac() - Converts a 48-bit IEEE MAC address to a 64-bit FC WWN
1747 * @mac: The MAC address to convert
1748 * @scheme: The scheme to use when converting
1749 * @port: The port indicator for converting
Vasu Dev5e80f7f2009-03-17 11:42:18 -07001750 *
1751 * Returns: u64 fc world wide name
1752 */
1753u64 fcoe_wwn_from_mac(unsigned char mac[MAX_ADDR_LEN],
1754 unsigned int scheme, unsigned int port)
1755{
1756 u64 wwn;
1757 u64 host_mac;
1758
1759 /* The MAC is in NO, so flip only the low 48 bits */
1760 host_mac = ((u64) mac[0] << 40) |
1761 ((u64) mac[1] << 32) |
1762 ((u64) mac[2] << 24) |
1763 ((u64) mac[3] << 16) |
1764 ((u64) mac[4] << 8) |
1765 (u64) mac[5];
1766
1767 WARN_ON(host_mac >= (1ULL << 48));
1768 wwn = host_mac | ((u64) scheme << 60);
1769 switch (scheme) {
1770 case 1:
1771 WARN_ON(port != 0);
1772 break;
1773 case 2:
1774 WARN_ON(port >= 0xfff);
1775 wwn |= (u64) port << 48;
1776 break;
1777 default:
1778 WARN_ON(1);
1779 break;
1780 }
1781
1782 return wwn;
1783}
1784EXPORT_SYMBOL_GPL(fcoe_wwn_from_mac);
1785
1786/**
Joe Eykholte10f8c62010-07-20 15:20:30 -07001787 * fcoe_ctlr_rport() - return the fcoe_rport for a given fc_rport_priv
1788 * @rdata: libfc remote port
1789 */
1790static inline struct fcoe_rport *fcoe_ctlr_rport(struct fc_rport_priv *rdata)
1791{
1792 return (struct fcoe_rport *)(rdata + 1);
1793}
1794
1795/**
1796 * fcoe_ctlr_vn_send() - Send a FIP VN2VN Probe Request or Reply.
1797 * @fip: The FCoE controller
1798 * @sub: sub-opcode for probe request, reply, or advertisement.
1799 * @dest: The destination Ethernet MAC address
1800 * @min_len: minimum size of the Ethernet payload to be sent
1801 */
1802static void fcoe_ctlr_vn_send(struct fcoe_ctlr *fip,
1803 enum fip_vn2vn_subcode sub,
1804 const u8 *dest, size_t min_len)
1805{
1806 struct sk_buff *skb;
1807 struct fip_frame {
1808 struct ethhdr eth;
1809 struct fip_header fip;
1810 struct fip_mac_desc mac;
1811 struct fip_wwn_desc wwnn;
1812 struct fip_vn_desc vn;
1813 } __attribute__((packed)) *frame;
1814 struct fip_fc4_feat *ff;
1815 struct fip_size_desc *size;
1816 u32 fcp_feat;
1817 size_t len;
1818 size_t dlen;
1819
1820 len = sizeof(*frame);
1821 dlen = 0;
1822 if (sub == FIP_SC_VN_CLAIM_NOTIFY || sub == FIP_SC_VN_CLAIM_REP) {
1823 dlen = sizeof(struct fip_fc4_feat) +
1824 sizeof(struct fip_size_desc);
1825 len += dlen;
1826 }
1827 dlen += sizeof(frame->mac) + sizeof(frame->wwnn) + sizeof(frame->vn);
1828 len = max(len, min_len + sizeof(struct ethhdr));
1829
1830 skb = dev_alloc_skb(len);
1831 if (!skb)
1832 return;
1833
1834 frame = (struct fip_frame *)skb->data;
1835 memset(frame, 0, len);
1836 memcpy(frame->eth.h_dest, dest, ETH_ALEN);
1837 memcpy(frame->eth.h_source, fip->ctl_src_addr, ETH_ALEN);
1838 frame->eth.h_proto = htons(ETH_P_FIP);
1839
1840 frame->fip.fip_ver = FIP_VER_ENCAPS(FIP_VER);
1841 frame->fip.fip_op = htons(FIP_OP_VN2VN);
1842 frame->fip.fip_subcode = sub;
1843 frame->fip.fip_dl_len = htons(dlen / FIP_BPW);
1844
1845 frame->mac.fd_desc.fip_dtype = FIP_DT_MAC;
1846 frame->mac.fd_desc.fip_dlen = sizeof(frame->mac) / FIP_BPW;
1847 memcpy(frame->mac.fd_mac, fip->ctl_src_addr, ETH_ALEN);
1848
1849 frame->wwnn.fd_desc.fip_dtype = FIP_DT_NAME;
1850 frame->wwnn.fd_desc.fip_dlen = sizeof(frame->wwnn) / FIP_BPW;
1851 put_unaligned_be64(fip->lp->wwnn, &frame->wwnn.fd_wwn);
1852
1853 frame->vn.fd_desc.fip_dtype = FIP_DT_VN_ID;
1854 frame->vn.fd_desc.fip_dlen = sizeof(frame->vn) / FIP_BPW;
1855 hton24(frame->vn.fd_mac, FIP_VN_FC_MAP);
1856 hton24(frame->vn.fd_mac + 3, fip->port_id);
1857 hton24(frame->vn.fd_fc_id, fip->port_id);
1858 put_unaligned_be64(fip->lp->wwpn, &frame->vn.fd_wwpn);
1859
1860 /*
1861 * For claims, add FC-4 features.
1862 * TBD: Add interface to get fc-4 types and features from libfc.
1863 */
1864 if (sub == FIP_SC_VN_CLAIM_NOTIFY || sub == FIP_SC_VN_CLAIM_REP) {
1865 ff = (struct fip_fc4_feat *)(frame + 1);
1866 ff->fd_desc.fip_dtype = FIP_DT_FC4F;
1867 ff->fd_desc.fip_dlen = sizeof(*ff) / FIP_BPW;
1868 ff->fd_fts = fip->lp->fcts;
1869
1870 fcp_feat = 0;
1871 if (fip->lp->service_params & FCP_SPPF_INIT_FCN)
1872 fcp_feat |= FCP_FEAT_INIT;
1873 if (fip->lp->service_params & FCP_SPPF_TARG_FCN)
1874 fcp_feat |= FCP_FEAT_TARG;
1875 fcp_feat <<= (FC_TYPE_FCP * 4) % 32;
1876 ff->fd_ff.fd_feat[FC_TYPE_FCP * 4 / 32] = htonl(fcp_feat);
1877
1878 size = (struct fip_size_desc *)(ff + 1);
1879 size->fd_desc.fip_dtype = FIP_DT_FCOE_SIZE;
1880 size->fd_desc.fip_dlen = sizeof(*size) / FIP_BPW;
1881 size->fd_size = htons(fcoe_ctlr_fcoe_size(fip));
1882 }
1883
1884 skb_put(skb, len);
1885 skb->protocol = htons(ETH_P_FIP);
1886 skb_reset_mac_header(skb);
1887 skb_reset_network_header(skb);
1888
1889 fip->send(fip, skb);
1890}
1891
1892/**
1893 * fcoe_ctlr_vn_rport_callback - Event handler for rport events.
1894 * @lport: The lport which is receiving the event
1895 * @rdata: remote port private data
1896 * @event: The event that occured
1897 *
1898 * Locking Note: The rport lock must not be held when calling this function.
1899 */
1900static void fcoe_ctlr_vn_rport_callback(struct fc_lport *lport,
1901 struct fc_rport_priv *rdata,
1902 enum fc_rport_event event)
1903{
1904 struct fcoe_ctlr *fip = lport->disc.priv;
1905 struct fcoe_rport *frport = fcoe_ctlr_rport(rdata);
1906
1907 LIBFCOE_FIP_DBG(fip, "vn_rport_callback %x event %d\n",
1908 rdata->ids.port_id, event);
1909
1910 mutex_lock(&fip->ctlr_mutex);
1911 switch (event) {
1912 case RPORT_EV_READY:
1913 frport->login_count = 0;
1914 break;
1915 case RPORT_EV_LOGO:
1916 case RPORT_EV_FAILED:
1917 case RPORT_EV_STOP:
1918 frport->login_count++;
1919 if (frport->login_count > FCOE_CTLR_VN2VN_LOGIN_LIMIT) {
1920 LIBFCOE_FIP_DBG(fip,
1921 "rport FLOGI limited port_id %6.6x\n",
1922 rdata->ids.port_id);
1923 lport->tt.rport_logoff(rdata);
1924 }
1925 break;
1926 default:
1927 break;
1928 }
1929 mutex_unlock(&fip->ctlr_mutex);
1930}
1931
1932static struct fc_rport_operations fcoe_ctlr_vn_rport_ops = {
1933 .event_callback = fcoe_ctlr_vn_rport_callback,
1934};
1935
1936/**
1937 * fcoe_ctlr_disc_stop_locked() - stop discovery in VN2VN mode
1938 * @fip: The FCoE controller
1939 *
1940 * Called with ctlr_mutex held.
1941 */
1942static void fcoe_ctlr_disc_stop_locked(struct fc_lport *lport)
1943{
1944 mutex_lock(&lport->disc.disc_mutex);
1945 lport->disc.disc_callback = NULL;
1946 mutex_unlock(&lport->disc.disc_mutex);
1947}
1948
1949/**
1950 * fcoe_ctlr_disc_stop() - stop discovery in VN2VN mode
1951 * @fip: The FCoE controller
1952 *
1953 * Called through the local port template for discovery.
1954 * Called without the ctlr_mutex held.
1955 */
1956static void fcoe_ctlr_disc_stop(struct fc_lport *lport)
1957{
1958 struct fcoe_ctlr *fip = lport->disc.priv;
1959
1960 mutex_lock(&fip->ctlr_mutex);
1961 fcoe_ctlr_disc_stop_locked(lport);
1962 mutex_unlock(&fip->ctlr_mutex);
1963}
1964
1965/**
1966 * fcoe_ctlr_disc_stop_final() - stop discovery for shutdown in VN2VN mode
1967 * @fip: The FCoE controller
1968 *
1969 * Called through the local port template for discovery.
1970 * Called without the ctlr_mutex held.
1971 */
1972static void fcoe_ctlr_disc_stop_final(struct fc_lport *lport)
1973{
1974 fcoe_ctlr_disc_stop(lport);
1975 lport->tt.rport_flush_queue();
1976 synchronize_rcu();
1977}
1978
1979/**
1980 * fcoe_ctlr_vn_restart() - VN2VN probe restart with new port_id
1981 * @fip: The FCoE controller
1982 *
1983 * Called with fcoe_ctlr lock held.
1984 */
1985static void fcoe_ctlr_vn_restart(struct fcoe_ctlr *fip)
1986{
1987 unsigned long wait;
1988 u32 port_id;
1989
1990 fcoe_ctlr_disc_stop_locked(fip->lp);
1991
1992 /*
1993 * Get proposed port ID.
1994 * If this is the first try after link up, use any previous port_id.
1995 * If there was none, use the low bits of the port_name.
1996 * On subsequent tries, get the next random one.
1997 * Don't use reserved IDs, use another non-zero value, just as random.
1998 */
1999 port_id = fip->port_id;
2000 if (fip->probe_tries)
2001 port_id = prandom32(&fip->rnd_state) & 0xffff;
2002 else if (!port_id)
2003 port_id = fip->lp->wwpn & 0xffff;
2004 if (!port_id || port_id == 0xffff)
2005 port_id = 1;
2006 fip->port_id = port_id;
2007
2008 if (fip->probe_tries < FIP_VN_RLIM_COUNT) {
2009 fip->probe_tries++;
2010 wait = random32() % FIP_VN_PROBE_WAIT;
2011 } else
2012 wait = FIP_VN_RLIM_INT;
2013 mod_timer(&fip->timer, jiffies + msecs_to_jiffies(wait));
2014 fcoe_ctlr_set_state(fip, FIP_ST_VNMP_START);
2015}
2016
2017/**
2018 * fcoe_ctlr_vn_start() - Start in VN2VN mode
2019 * @fip: The FCoE controller
2020 *
2021 * Called with fcoe_ctlr lock held.
2022 */
2023static void fcoe_ctlr_vn_start(struct fcoe_ctlr *fip)
2024{
2025 fip->probe_tries = 0;
2026 prandom32_seed(&fip->rnd_state, fip->lp->wwpn);
2027 fcoe_ctlr_vn_restart(fip);
2028}
2029
2030/**
2031 * fcoe_ctlr_vn_parse - parse probe request or response
2032 * @fip: The FCoE controller
2033 * @skb: incoming packet
2034 * @rdata: buffer for resulting parsed VN entry plus fcoe_rport
2035 *
2036 * Returns non-zero error number on error.
2037 * Does not consume the packet.
2038 */
2039static int fcoe_ctlr_vn_parse(struct fcoe_ctlr *fip,
2040 struct sk_buff *skb,
2041 struct fc_rport_priv *rdata)
2042{
2043 struct fip_header *fiph;
2044 struct fip_desc *desc = NULL;
2045 struct fip_mac_desc *macd = NULL;
2046 struct fip_wwn_desc *wwn = NULL;
2047 struct fip_vn_desc *vn = NULL;
2048 struct fip_size_desc *size = NULL;
2049 struct fcoe_rport *frport;
2050 size_t rlen;
2051 size_t dlen;
2052 u32 desc_mask = 0;
2053 u32 dtype;
2054 u8 sub;
2055
2056 memset(rdata, 0, sizeof(*rdata) + sizeof(*frport));
2057 frport = fcoe_ctlr_rport(rdata);
2058
2059 fiph = (struct fip_header *)skb->data;
2060 frport->flags = ntohs(fiph->fip_flags);
2061
2062 sub = fiph->fip_subcode;
2063 switch (sub) {
2064 case FIP_SC_VN_PROBE_REQ:
2065 case FIP_SC_VN_PROBE_REP:
2066 case FIP_SC_VN_BEACON:
2067 desc_mask = BIT(FIP_DT_MAC) | BIT(FIP_DT_NAME) |
2068 BIT(FIP_DT_VN_ID);
2069 break;
2070 case FIP_SC_VN_CLAIM_NOTIFY:
2071 case FIP_SC_VN_CLAIM_REP:
2072 desc_mask = BIT(FIP_DT_MAC) | BIT(FIP_DT_NAME) |
2073 BIT(FIP_DT_VN_ID) | BIT(FIP_DT_FC4F) |
2074 BIT(FIP_DT_FCOE_SIZE);
2075 break;
2076 default:
2077 LIBFCOE_FIP_DBG(fip, "vn_parse unknown subcode %u\n", sub);
2078 return -EINVAL;
2079 }
2080
2081 rlen = ntohs(fiph->fip_dl_len) * 4;
2082 if (rlen + sizeof(*fiph) > skb->len)
2083 return -EINVAL;
2084
2085 desc = (struct fip_desc *)(fiph + 1);
2086 while (rlen > 0) {
2087 dlen = desc->fip_dlen * FIP_BPW;
2088 if (dlen < sizeof(*desc) || dlen > rlen)
2089 return -EINVAL;
2090
2091 dtype = desc->fip_dtype;
2092 if (dtype < 32) {
2093 if (!(desc_mask & BIT(dtype))) {
2094 LIBFCOE_FIP_DBG(fip,
2095 "unexpected or duplicated desc "
2096 "desc type %u in "
2097 "FIP VN2VN subtype %u\n",
2098 dtype, sub);
2099 return -EINVAL;
2100 }
2101 desc_mask &= ~BIT(dtype);
2102 }
2103
2104 switch (dtype) {
2105 case FIP_DT_MAC:
2106 if (dlen != sizeof(struct fip_mac_desc))
2107 goto len_err;
2108 macd = (struct fip_mac_desc *)desc;
2109 if (!is_valid_ether_addr(macd->fd_mac)) {
2110 LIBFCOE_FIP_DBG(fip,
2111 "Invalid MAC addr %pM in FIP VN2VN\n",
2112 macd->fd_mac);
2113 return -EINVAL;
2114 }
2115 memcpy(frport->enode_mac, macd->fd_mac, ETH_ALEN);
2116 break;
2117 case FIP_DT_NAME:
2118 if (dlen != sizeof(struct fip_wwn_desc))
2119 goto len_err;
2120 wwn = (struct fip_wwn_desc *)desc;
2121 rdata->ids.node_name = get_unaligned_be64(&wwn->fd_wwn);
2122 break;
2123 case FIP_DT_VN_ID:
2124 if (dlen != sizeof(struct fip_vn_desc))
2125 goto len_err;
2126 vn = (struct fip_vn_desc *)desc;
2127 memcpy(frport->vn_mac, vn->fd_mac, ETH_ALEN);
2128 rdata->ids.port_id = ntoh24(vn->fd_fc_id);
2129 rdata->ids.port_name = get_unaligned_be64(&vn->fd_wwpn);
2130 break;
2131 case FIP_DT_FC4F:
2132 if (dlen != sizeof(struct fip_fc4_feat))
2133 goto len_err;
2134 break;
2135 case FIP_DT_FCOE_SIZE:
2136 if (dlen != sizeof(struct fip_size_desc))
2137 goto len_err;
2138 size = (struct fip_size_desc *)desc;
2139 frport->fcoe_len = ntohs(size->fd_size);
2140 break;
2141 default:
2142 LIBFCOE_FIP_DBG(fip, "unexpected descriptor type %x "
2143 "in FIP probe\n", dtype);
2144 /* standard says ignore unknown descriptors >= 128 */
2145 if (dtype < FIP_DT_VENDOR_BASE)
2146 return -EINVAL;
2147 break;
2148 }
2149 desc = (struct fip_desc *)((char *)desc + dlen);
2150 rlen -= dlen;
2151 }
2152 return 0;
2153
2154len_err:
2155 LIBFCOE_FIP_DBG(fip, "FIP length error in descriptor type %x len %zu\n",
2156 dtype, dlen);
2157 return -EINVAL;
2158}
2159
2160/**
2161 * fcoe_ctlr_vn_send_claim() - send multicast FIP VN2VN Claim Notification.
2162 * @fip: The FCoE controller
2163 *
2164 * Called with ctlr_mutex held.
2165 */
2166static void fcoe_ctlr_vn_send_claim(struct fcoe_ctlr *fip)
2167{
2168 fcoe_ctlr_vn_send(fip, FIP_SC_VN_CLAIM_NOTIFY, fcoe_all_vn2vn, 0);
2169 fip->sol_time = jiffies;
2170}
2171
2172/**
2173 * fcoe_ctlr_vn_probe_req() - handle incoming VN2VN probe request.
2174 * @fip: The FCoE controller
2175 * @rdata: parsed remote port with frport from the probe request
2176 *
2177 * Called with ctlr_mutex held.
2178 */
2179static void fcoe_ctlr_vn_probe_req(struct fcoe_ctlr *fip,
2180 struct fc_rport_priv *rdata)
2181{
2182 struct fcoe_rport *frport = fcoe_ctlr_rport(rdata);
2183
2184 if (rdata->ids.port_id != fip->port_id)
2185 return;
2186
2187 switch (fip->state) {
2188 case FIP_ST_VNMP_CLAIM:
2189 case FIP_ST_VNMP_UP:
2190 fcoe_ctlr_vn_send(fip, FIP_SC_VN_PROBE_REP,
2191 frport->enode_mac, 0);
2192 break;
2193 case FIP_ST_VNMP_PROBE1:
2194 case FIP_ST_VNMP_PROBE2:
2195 /*
2196 * Decide whether to reply to the Probe.
2197 * Our selected address is never a "recorded" one, so
2198 * only reply if our WWPN is greater and the
2199 * Probe's REC bit is not set.
2200 * If we don't reply, we will change our address.
2201 */
2202 if (fip->lp->wwpn > rdata->ids.port_name &&
2203 !(frport->flags & FIP_FL_REC_OR_P2P)) {
2204 fcoe_ctlr_vn_send(fip, FIP_SC_VN_PROBE_REP,
2205 frport->enode_mac, 0);
2206 break;
2207 }
2208 /* fall through */
2209 case FIP_ST_VNMP_START:
2210 fcoe_ctlr_vn_restart(fip);
2211 break;
2212 default:
2213 break;
2214 }
2215}
2216
2217/**
2218 * fcoe_ctlr_vn_probe_reply() - handle incoming VN2VN probe reply.
2219 * @fip: The FCoE controller
2220 * @rdata: parsed remote port with frport from the probe request
2221 *
2222 * Called with ctlr_mutex held.
2223 */
2224static void fcoe_ctlr_vn_probe_reply(struct fcoe_ctlr *fip,
2225 struct fc_rport_priv *rdata)
2226{
2227 if (rdata->ids.port_id != fip->port_id)
2228 return;
2229 switch (fip->state) {
2230 case FIP_ST_VNMP_START:
2231 case FIP_ST_VNMP_PROBE1:
2232 case FIP_ST_VNMP_PROBE2:
2233 case FIP_ST_VNMP_CLAIM:
2234 fcoe_ctlr_vn_restart(fip);
2235 break;
2236 case FIP_ST_VNMP_UP:
2237 fcoe_ctlr_vn_send_claim(fip);
2238 break;
2239 default:
2240 break;
2241 }
2242}
2243
2244/**
2245 * fcoe_ctlr_vn_add() - Add a VN2VN entry to the list, based on a claim reply.
2246 * @fip: The FCoE controller
2247 * @new: newly-parsed remote port with frport as a template for new rdata
2248 *
2249 * Called with ctlr_mutex held.
2250 */
2251static void fcoe_ctlr_vn_add(struct fcoe_ctlr *fip, struct fc_rport_priv *new)
2252{
2253 struct fc_lport *lport = fip->lp;
2254 struct fc_rport_priv *rdata;
2255 struct fc_rport_identifiers *ids;
2256 struct fcoe_rport *frport;
2257 u32 port_id;
2258
2259 port_id = new->ids.port_id;
2260 if (port_id == fip->port_id)
2261 return;
2262
2263 mutex_lock(&lport->disc.disc_mutex);
2264 rdata = lport->tt.rport_create(lport, port_id);
2265 if (!rdata) {
2266 mutex_unlock(&lport->disc.disc_mutex);
2267 return;
2268 }
2269
2270 rdata->ops = &fcoe_ctlr_vn_rport_ops;
2271 rdata->disc_id = lport->disc.disc_id;
2272
2273 ids = &rdata->ids;
2274 if ((ids->port_name != -1 && ids->port_name != new->ids.port_name) ||
2275 (ids->node_name != -1 && ids->node_name != new->ids.node_name))
2276 lport->tt.rport_logoff(rdata);
2277 ids->port_name = new->ids.port_name;
2278 ids->node_name = new->ids.node_name;
2279 mutex_unlock(&lport->disc.disc_mutex);
2280
2281 frport = fcoe_ctlr_rport(rdata);
2282 LIBFCOE_FIP_DBG(fip, "vn_add rport %6.6x %s\n",
2283 port_id, frport->fcoe_len ? "old" : "new");
2284 *frport = *fcoe_ctlr_rport(new);
2285 frport->time = 0;
2286}
2287
2288/**
2289 * fcoe_ctlr_vn_lookup() - Find VN remote port's MAC address
2290 * @fip: The FCoE controller
2291 * @port_id: The port_id of the remote VN_node
2292 * @mac: buffer which will hold the VN_NODE destination MAC address, if found.
2293 *
2294 * Returns non-zero error if no remote port found.
2295 */
2296static int fcoe_ctlr_vn_lookup(struct fcoe_ctlr *fip, u32 port_id, u8 *mac)
2297{
2298 struct fc_lport *lport = fip->lp;
2299 struct fc_rport_priv *rdata;
2300 struct fcoe_rport *frport;
2301 int ret = -1;
2302
2303 rcu_read_lock();
2304 rdata = lport->tt.rport_lookup(lport, port_id);
2305 if (rdata) {
2306 frport = fcoe_ctlr_rport(rdata);
2307 memcpy(mac, frport->enode_mac, ETH_ALEN);
2308 ret = 0;
2309 }
2310 rcu_read_unlock();
2311 return ret;
2312}
2313
2314/**
2315 * fcoe_ctlr_vn_claim_notify() - handle received FIP VN2VN Claim Notification
2316 * @fip: The FCoE controller
2317 * @new: newly-parsed remote port with frport as a template for new rdata
2318 *
2319 * Called with ctlr_mutex held.
2320 */
2321static void fcoe_ctlr_vn_claim_notify(struct fcoe_ctlr *fip,
2322 struct fc_rport_priv *new)
2323{
2324 struct fcoe_rport *frport = fcoe_ctlr_rport(new);
2325
2326 if (frport->flags & FIP_FL_REC_OR_P2P) {
2327 fcoe_ctlr_vn_send(fip, FIP_SC_VN_PROBE_REQ, fcoe_all_vn2vn, 0);
2328 return;
2329 }
2330 switch (fip->state) {
2331 case FIP_ST_VNMP_START:
2332 case FIP_ST_VNMP_PROBE1:
2333 case FIP_ST_VNMP_PROBE2:
2334 if (new->ids.port_id == fip->port_id)
2335 fcoe_ctlr_vn_restart(fip);
2336 break;
2337 case FIP_ST_VNMP_CLAIM:
2338 case FIP_ST_VNMP_UP:
2339 if (new->ids.port_id == fip->port_id) {
2340 if (new->ids.port_name > fip->lp->wwpn) {
2341 fcoe_ctlr_vn_restart(fip);
2342 break;
2343 }
2344 fcoe_ctlr_vn_send_claim(fip);
2345 break;
2346 }
2347 fcoe_ctlr_vn_send(fip, FIP_SC_VN_CLAIM_REP, frport->enode_mac,
2348 min((u32)frport->fcoe_len,
2349 fcoe_ctlr_fcoe_size(fip)));
2350 fcoe_ctlr_vn_add(fip, new);
2351 break;
2352 default:
2353 break;
2354 }
2355}
2356
2357/**
2358 * fcoe_ctlr_vn_claim_resp() - handle received Claim Response
2359 * @fip: The FCoE controller that received the frame
2360 * @new: newly-parsed remote port with frport from the Claim Response
2361 *
2362 * Called with ctlr_mutex held.
2363 */
2364static void fcoe_ctlr_vn_claim_resp(struct fcoe_ctlr *fip,
2365 struct fc_rport_priv *new)
2366{
2367 LIBFCOE_FIP_DBG(fip, "claim resp from from rport %x - state %s\n",
2368 new->ids.port_id, fcoe_ctlr_state(fip->state));
2369 if (fip->state == FIP_ST_VNMP_UP || fip->state == FIP_ST_VNMP_CLAIM)
2370 fcoe_ctlr_vn_add(fip, new);
2371}
2372
2373/**
2374 * fcoe_ctlr_vn_beacon() - handle received beacon.
2375 * @fip: The FCoE controller that received the frame
2376 * @new: newly-parsed remote port with frport from the Beacon
2377 *
2378 * Called with ctlr_mutex held.
2379 */
2380static void fcoe_ctlr_vn_beacon(struct fcoe_ctlr *fip,
2381 struct fc_rport_priv *new)
2382{
2383 struct fc_lport *lport = fip->lp;
2384 struct fc_rport_priv *rdata;
2385 struct fcoe_rport *frport;
2386
2387 frport = fcoe_ctlr_rport(new);
2388 if (frport->flags & FIP_FL_REC_OR_P2P) {
2389 fcoe_ctlr_vn_send(fip, FIP_SC_VN_PROBE_REQ, fcoe_all_vn2vn, 0);
2390 return;
2391 }
2392 mutex_lock(&lport->disc.disc_mutex);
2393 rdata = lport->tt.rport_lookup(lport, new->ids.port_id);
2394 if (rdata)
2395 kref_get(&rdata->kref);
2396 mutex_unlock(&lport->disc.disc_mutex);
2397 if (rdata) {
2398 if (rdata->ids.node_name == new->ids.node_name &&
2399 rdata->ids.port_name == new->ids.port_name) {
2400 frport = fcoe_ctlr_rport(rdata);
2401 if (!frport->time && fip->state == FIP_ST_VNMP_UP)
2402 lport->tt.rport_login(rdata);
2403 frport->time = jiffies;
2404 }
2405 kref_put(&rdata->kref, lport->tt.rport_destroy);
2406 return;
2407 }
2408 if (fip->state != FIP_ST_VNMP_UP)
2409 return;
2410
2411 /*
2412 * Beacon from a new neighbor.
2413 * Send a claim notify if one hasn't been sent recently.
2414 * Don't add the neighbor yet.
2415 */
2416 LIBFCOE_FIP_DBG(fip, "beacon from new rport %x. sending claim notify\n",
2417 new->ids.port_id);
2418 if (time_after(jiffies,
2419 fip->sol_time + msecs_to_jiffies(FIP_VN_ANN_WAIT)))
2420 fcoe_ctlr_vn_send_claim(fip);
2421}
2422
2423/**
2424 * fcoe_ctlr_vn_age() - Check for VN_ports without recent beacons
2425 * @fip: The FCoE controller
2426 *
2427 * Called with ctlr_mutex held.
2428 * Called only in state FIP_ST_VNMP_UP.
2429 * Returns the soonest time for next age-out or a time far in the future.
2430 */
2431static unsigned long fcoe_ctlr_vn_age(struct fcoe_ctlr *fip)
2432{
2433 struct fc_lport *lport = fip->lp;
2434 struct fc_rport_priv *rdata;
2435 struct fcoe_rport *frport;
2436 unsigned long next_time;
2437 unsigned long deadline;
2438
2439 next_time = jiffies + msecs_to_jiffies(FIP_VN_BEACON_INT * 10);
2440 mutex_lock(&lport->disc.disc_mutex);
2441 list_for_each_entry_rcu(rdata, &lport->disc.rports, peers) {
2442 frport = fcoe_ctlr_rport(rdata);
2443 if (!frport->time)
2444 continue;
2445 deadline = frport->time +
2446 msecs_to_jiffies(FIP_VN_BEACON_INT * 25 / 10);
2447 if (time_after_eq(jiffies, deadline)) {
2448 frport->time = 0;
2449 LIBFCOE_FIP_DBG(fip,
2450 "port %16.16llx fc_id %6.6x beacon expired\n",
2451 rdata->ids.port_name, rdata->ids.port_id);
2452 lport->tt.rport_logoff(rdata);
2453 } else if (time_before(deadline, next_time))
2454 next_time = deadline;
2455 }
2456 mutex_unlock(&lport->disc.disc_mutex);
2457 return next_time;
2458}
2459
2460/**
2461 * fcoe_ctlr_vn_recv() - Receive a FIP frame
2462 * @fip: The FCoE controller that received the frame
2463 * @skb: The received FIP frame
2464 *
2465 * Returns non-zero if the frame is dropped.
2466 * Always consumes the frame.
2467 */
2468static int fcoe_ctlr_vn_recv(struct fcoe_ctlr *fip, struct sk_buff *skb)
2469{
2470 struct fip_header *fiph;
2471 enum fip_vn2vn_subcode sub;
Kiran Patil2dc02ee2010-10-08 17:12:41 -07002472 struct {
Joe Eykholte10f8c62010-07-20 15:20:30 -07002473 struct fc_rport_priv rdata;
2474 struct fcoe_rport frport;
2475 } buf;
2476 int rc;
2477
2478 fiph = (struct fip_header *)skb->data;
2479 sub = fiph->fip_subcode;
2480
2481 rc = fcoe_ctlr_vn_parse(fip, skb, &buf.rdata);
2482 if (rc) {
2483 LIBFCOE_FIP_DBG(fip, "vn_recv vn_parse error %d\n", rc);
2484 goto drop;
2485 }
2486
2487 mutex_lock(&fip->ctlr_mutex);
2488 switch (sub) {
2489 case FIP_SC_VN_PROBE_REQ:
2490 fcoe_ctlr_vn_probe_req(fip, &buf.rdata);
2491 break;
2492 case FIP_SC_VN_PROBE_REP:
2493 fcoe_ctlr_vn_probe_reply(fip, &buf.rdata);
2494 break;
2495 case FIP_SC_VN_CLAIM_NOTIFY:
2496 fcoe_ctlr_vn_claim_notify(fip, &buf.rdata);
2497 break;
2498 case FIP_SC_VN_CLAIM_REP:
2499 fcoe_ctlr_vn_claim_resp(fip, &buf.rdata);
2500 break;
2501 case FIP_SC_VN_BEACON:
2502 fcoe_ctlr_vn_beacon(fip, &buf.rdata);
2503 break;
2504 default:
2505 LIBFCOE_FIP_DBG(fip, "vn_recv unknown subcode %d\n", sub);
2506 rc = -1;
2507 break;
2508 }
2509 mutex_unlock(&fip->ctlr_mutex);
2510drop:
2511 kfree_skb(skb);
2512 return rc;
2513}
2514
2515/**
2516 * fcoe_ctlr_disc_recv - discovery receive handler for VN2VN mode.
Joe Eykholt922611562010-07-20 15:21:12 -07002517 * @lport: The local port
2518 * @fp: The received frame
Joe Eykholte10f8c62010-07-20 15:20:30 -07002519 *
2520 * This should never be called since we don't see RSCNs or other
2521 * fabric-generated ELSes.
2522 */
Joe Eykholt922611562010-07-20 15:21:12 -07002523static void fcoe_ctlr_disc_recv(struct fc_lport *lport, struct fc_frame *fp)
Joe Eykholte10f8c62010-07-20 15:20:30 -07002524{
2525 struct fc_seq_els_data rjt_data;
2526
Joe Eykholte10f8c62010-07-20 15:20:30 -07002527 rjt_data.reason = ELS_RJT_UNSUP;
2528 rjt_data.explan = ELS_EXPL_NONE;
Joe Eykholt922611562010-07-20 15:21:12 -07002529 lport->tt.seq_els_rsp_send(fp, ELS_LS_RJT, &rjt_data);
Joe Eykholte10f8c62010-07-20 15:20:30 -07002530 fc_frame_free(fp);
2531}
2532
2533/**
2534 * fcoe_ctlr_disc_recv - start discovery for VN2VN mode.
2535 * @fip: The FCoE controller
2536 *
2537 * This sets a flag indicating that remote ports should be created
2538 * and started for the peers we discover. We use the disc_callback
2539 * pointer as that flag. Peers already discovered are created here.
2540 *
2541 * The lport lock is held during this call. The callback must be done
2542 * later, without holding either the lport or discovery locks.
2543 * The fcoe_ctlr lock may also be held during this call.
2544 */
2545static void fcoe_ctlr_disc_start(void (*callback)(struct fc_lport *,
2546 enum fc_disc_event),
2547 struct fc_lport *lport)
2548{
2549 struct fc_disc *disc = &lport->disc;
2550 struct fcoe_ctlr *fip = disc->priv;
2551
2552 mutex_lock(&disc->disc_mutex);
2553 disc->disc_callback = callback;
2554 disc->disc_id = (disc->disc_id + 2) | 1;
2555 disc->pending = 1;
2556 schedule_work(&fip->timer_work);
2557 mutex_unlock(&disc->disc_mutex);
2558}
2559
2560/**
2561 * fcoe_ctlr_vn_disc() - report FIP VN_port discovery results after claim state.
2562 * @fip: The FCoE controller
2563 *
2564 * Starts the FLOGI and PLOGI login process to each discovered rport for which
2565 * we've received at least one beacon.
2566 * Performs the discovery complete callback.
2567 */
2568static void fcoe_ctlr_vn_disc(struct fcoe_ctlr *fip)
2569{
2570 struct fc_lport *lport = fip->lp;
2571 struct fc_disc *disc = &lport->disc;
2572 struct fc_rport_priv *rdata;
2573 struct fcoe_rport *frport;
2574 void (*callback)(struct fc_lport *, enum fc_disc_event);
2575
2576 mutex_lock(&disc->disc_mutex);
2577 callback = disc->pending ? disc->disc_callback : NULL;
2578 disc->pending = 0;
2579 list_for_each_entry_rcu(rdata, &disc->rports, peers) {
2580 frport = fcoe_ctlr_rport(rdata);
2581 if (frport->time)
2582 lport->tt.rport_login(rdata);
2583 }
2584 mutex_unlock(&disc->disc_mutex);
2585 if (callback)
2586 callback(lport, DISC_EV_SUCCESS);
2587}
2588
2589/**
2590 * fcoe_ctlr_vn_timeout - timer work function for VN2VN mode.
2591 * @fip: The FCoE controller
2592 */
2593static void fcoe_ctlr_vn_timeout(struct fcoe_ctlr *fip)
2594{
2595 unsigned long next_time;
2596 u8 mac[ETH_ALEN];
2597 u32 new_port_id = 0;
2598
2599 mutex_lock(&fip->ctlr_mutex);
2600 switch (fip->state) {
2601 case FIP_ST_VNMP_START:
2602 fcoe_ctlr_set_state(fip, FIP_ST_VNMP_PROBE1);
2603 fcoe_ctlr_vn_send(fip, FIP_SC_VN_PROBE_REQ, fcoe_all_vn2vn, 0);
2604 next_time = jiffies + msecs_to_jiffies(FIP_VN_PROBE_WAIT);
2605 break;
2606 case FIP_ST_VNMP_PROBE1:
2607 fcoe_ctlr_set_state(fip, FIP_ST_VNMP_PROBE2);
2608 fcoe_ctlr_vn_send(fip, FIP_SC_VN_PROBE_REQ, fcoe_all_vn2vn, 0);
2609 next_time = jiffies + msecs_to_jiffies(FIP_VN_ANN_WAIT);
2610 break;
2611 case FIP_ST_VNMP_PROBE2:
2612 fcoe_ctlr_set_state(fip, FIP_ST_VNMP_CLAIM);
2613 new_port_id = fip->port_id;
2614 hton24(mac, FIP_VN_FC_MAP);
2615 hton24(mac + 3, new_port_id);
Joe Eykholtcd229e42010-07-20 15:20:40 -07002616 fcoe_ctlr_map_dest(fip);
Joe Eykholte10f8c62010-07-20 15:20:30 -07002617 fip->update_mac(fip->lp, mac);
2618 fcoe_ctlr_vn_send_claim(fip);
2619 next_time = jiffies + msecs_to_jiffies(FIP_VN_ANN_WAIT);
2620 break;
2621 case FIP_ST_VNMP_CLAIM:
2622 /*
2623 * This may be invoked either by starting discovery so don't
2624 * go to the next state unless it's been long enough.
2625 */
2626 next_time = fip->sol_time + msecs_to_jiffies(FIP_VN_ANN_WAIT);
2627 if (time_after_eq(jiffies, next_time)) {
2628 fcoe_ctlr_set_state(fip, FIP_ST_VNMP_UP);
2629 fcoe_ctlr_vn_send(fip, FIP_SC_VN_BEACON,
2630 fcoe_all_vn2vn, 0);
2631 next_time = jiffies + msecs_to_jiffies(FIP_VN_ANN_WAIT);
2632 fip->port_ka_time = next_time;
2633 }
2634 fcoe_ctlr_vn_disc(fip);
2635 break;
2636 case FIP_ST_VNMP_UP:
2637 next_time = fcoe_ctlr_vn_age(fip);
2638 if (time_after_eq(jiffies, fip->port_ka_time)) {
2639 fcoe_ctlr_vn_send(fip, FIP_SC_VN_BEACON,
2640 fcoe_all_vn2vn, 0);
2641 fip->port_ka_time = jiffies +
2642 msecs_to_jiffies(FIP_VN_BEACON_INT +
2643 (random32() % FIP_VN_BEACON_FUZZ));
2644 }
2645 if (time_before(fip->port_ka_time, next_time))
2646 next_time = fip->port_ka_time;
2647 break;
2648 case FIP_ST_LINK_WAIT:
2649 goto unlock;
2650 default:
Joe Perches11aa9902010-11-30 16:19:09 -08002651 WARN(1, "unexpected state %d\n", fip->state);
Joe Eykholte10f8c62010-07-20 15:20:30 -07002652 goto unlock;
2653 }
2654 mod_timer(&fip->timer, next_time);
2655unlock:
2656 mutex_unlock(&fip->ctlr_mutex);
2657
2658 /* If port ID is new, notify local port after dropping ctlr_mutex */
2659 if (new_port_id)
2660 fc_lport_set_local_id(fip->lp, new_port_id);
2661}
2662
2663/**
Robert Love70b51aa2009-11-03 11:47:45 -08002664 * fcoe_libfc_config() - Sets up libfc related properties for local port
2665 * @lp: The local port to configure libfc for
Joe Eykholte10f8c62010-07-20 15:20:30 -07002666 * @fip: The FCoE controller in use by the local port
Robert Love70b51aa2009-11-03 11:47:45 -08002667 * @tt: The libfc function template
Joe Eykholte10f8c62010-07-20 15:20:30 -07002668 * @init_fcp: If non-zero, the FCP portion of libfc should be initialized
Vasu Dev5e80f7f2009-03-17 11:42:18 -07002669 *
2670 * Returns : 0 for success
2671 */
Joe Eykholte10f8c62010-07-20 15:20:30 -07002672int fcoe_libfc_config(struct fc_lport *lport, struct fcoe_ctlr *fip,
2673 const struct libfc_function_template *tt, int init_fcp)
Vasu Dev5e80f7f2009-03-17 11:42:18 -07002674{
2675 /* Set the function pointers set by the LLDD */
Robert Love70b51aa2009-11-03 11:47:45 -08002676 memcpy(&lport->tt, tt, sizeof(*tt));
Joe Eykholte10f8c62010-07-20 15:20:30 -07002677 if (init_fcp && fc_fcp_init(lport))
Vasu Dev5e80f7f2009-03-17 11:42:18 -07002678 return -ENOMEM;
Robert Love70b51aa2009-11-03 11:47:45 -08002679 fc_exch_init(lport);
2680 fc_elsct_init(lport);
2681 fc_lport_init(lport);
Joe Eykholte10f8c62010-07-20 15:20:30 -07002682 if (fip->mode == FIP_MODE_VN2VN)
2683 lport->rport_priv_size = sizeof(struct fcoe_rport);
Robert Love70b51aa2009-11-03 11:47:45 -08002684 fc_rport_init(lport);
Joe Eykholte10f8c62010-07-20 15:20:30 -07002685 if (fip->mode == FIP_MODE_VN2VN) {
2686 lport->point_to_multipoint = 1;
2687 lport->tt.disc_recv_req = fcoe_ctlr_disc_recv;
2688 lport->tt.disc_start = fcoe_ctlr_disc_start;
2689 lport->tt.disc_stop = fcoe_ctlr_disc_stop;
2690 lport->tt.disc_stop_final = fcoe_ctlr_disc_stop_final;
2691 mutex_init(&lport->disc.disc_mutex);
2692 INIT_LIST_HEAD(&lport->disc.rports);
2693 lport->disc.priv = fip;
2694 } else {
2695 fc_disc_init(lport);
2696 }
Vasu Dev5e80f7f2009-03-17 11:42:18 -07002697 return 0;
2698}
2699EXPORT_SYMBOL_GPL(fcoe_libfc_config);