blob: 249a106888d9309ab44ff0f4d2cc82d1b0ff2dc8 [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
Yi Zou21b7b2f2011-01-28 16:04:45 -080047#include "libfcoe.h"
48
Joe Eykholt97c83892009-03-17 11:42:40 -070049#define FCOE_CTLR_MIN_FKA 500 /* min keep alive (mS) */
50#define FCOE_CTLR_DEF_FKA FIP_DEF_FKA /* default keep alive (mS) */
51
52static void fcoe_ctlr_timeout(unsigned long);
Joe Eykholt42913652010-03-12 16:08:23 -080053static void fcoe_ctlr_timer_work(struct work_struct *);
Joe Eykholt97c83892009-03-17 11:42:40 -070054static void fcoe_ctlr_recv_work(struct work_struct *);
Joe Eykholt794d98e2010-11-30 16:19:56 -080055static int fcoe_ctlr_flogi_retry(struct fcoe_ctlr *);
Joe Eykholt97c83892009-03-17 11:42:40 -070056
Joe Eykholte10f8c62010-07-20 15:20:30 -070057static void fcoe_ctlr_vn_start(struct fcoe_ctlr *);
58static int fcoe_ctlr_vn_recv(struct fcoe_ctlr *, struct sk_buff *);
59static void fcoe_ctlr_vn_timeout(struct fcoe_ctlr *);
60static int fcoe_ctlr_vn_lookup(struct fcoe_ctlr *, u32, u8 *);
61
Joe Eykholt97c83892009-03-17 11:42:40 -070062static u8 fcoe_all_fcfs[ETH_ALEN] = FIP_ALL_FCF_MACS;
Joe Eykholte10f8c62010-07-20 15:20:30 -070063static u8 fcoe_all_enode[ETH_ALEN] = FIP_ALL_ENODE_MACS;
64static u8 fcoe_all_vn2vn[ETH_ALEN] = FIP_ALL_VN2VN_MACS;
65static u8 fcoe_all_p2p[ETH_ALEN] = FIP_ALL_P2P_MACS;
Joe Eykholt97c83892009-03-17 11:42:40 -070066
Yi Zou0095a922011-01-28 16:05:00 -080067static const char * const fcoe_ctlr_states[] = {
Joe Eykholt9b651da2010-07-20 15:20:24 -070068 [FIP_ST_DISABLED] = "DISABLED",
69 [FIP_ST_LINK_WAIT] = "LINK_WAIT",
70 [FIP_ST_AUTO] = "AUTO",
71 [FIP_ST_NON_FIP] = "NON_FIP",
72 [FIP_ST_ENABLED] = "ENABLED",
Joe Eykholte10f8c62010-07-20 15:20:30 -070073 [FIP_ST_VNMP_START] = "VNMP_START",
74 [FIP_ST_VNMP_PROBE1] = "VNMP_PROBE1",
75 [FIP_ST_VNMP_PROBE2] = "VNMP_PROBE2",
76 [FIP_ST_VNMP_CLAIM] = "VNMP_CLAIM",
77 [FIP_ST_VNMP_UP] = "VNMP_UP",
Joe Eykholt9b651da2010-07-20 15:20:24 -070078};
79
80static const char *fcoe_ctlr_state(enum fip_state state)
81{
82 const char *cp = "unknown";
83
84 if (state < ARRAY_SIZE(fcoe_ctlr_states))
85 cp = fcoe_ctlr_states[state];
86 if (!cp)
87 cp = "unknown";
88 return cp;
89}
90
91/**
92 * fcoe_ctlr_set_state() - Set and do debug printing for the new FIP state.
93 * @fip: The FCoE controller
94 * @state: The new state
95 */
96static void fcoe_ctlr_set_state(struct fcoe_ctlr *fip, enum fip_state state)
97{
98 if (state == fip->state)
99 return;
100 if (fip->lp)
101 LIBFCOE_FIP_DBG(fip, "state %s -> %s\n",
102 fcoe_ctlr_state(fip->state), fcoe_ctlr_state(state));
103 fip->state = state;
104}
105
Robert Love70b51aa2009-11-03 11:47:45 -0800106/**
107 * fcoe_ctlr_mtu_valid() - Check if a FCF's MTU is valid
108 * @fcf: The FCF to check
109 *
Joe Eykholt97c83892009-03-17 11:42:40 -0700110 * Return non-zero if FCF fcoe_size has been validated.
111 */
112static inline int fcoe_ctlr_mtu_valid(const struct fcoe_fcf *fcf)
113{
114 return (fcf->flags & FIP_FL_SOL) != 0;
115}
116
Robert Love70b51aa2009-11-03 11:47:45 -0800117/**
118 * fcoe_ctlr_fcf_usable() - Check if a FCF is usable
119 * @fcf: The FCF to check
120 *
Joe Eykholt97c83892009-03-17 11:42:40 -0700121 * Return non-zero if the FCF is usable.
122 */
123static inline int fcoe_ctlr_fcf_usable(struct fcoe_fcf *fcf)
124{
125 u16 flags = FIP_FL_SOL | FIP_FL_AVAIL;
126
127 return (fcf->flags & flags) == flags;
128}
129
130/**
Joe Eykholtcd229e42010-07-20 15:20:40 -0700131 * fcoe_ctlr_map_dest() - Set flag and OUI for mapping destination addresses
132 * @fip: The FCoE controller
133 */
134static void fcoe_ctlr_map_dest(struct fcoe_ctlr *fip)
135{
136 if (fip->mode == FIP_MODE_VN2VN)
137 hton24(fip->dest_addr, FIP_VN_FC_MAP);
138 else
139 hton24(fip->dest_addr, FIP_DEF_FC_MAP);
140 hton24(fip->dest_addr + 3, 0);
141 fip->map_dest = 1;
142}
143
144/**
Robert Love70b51aa2009-11-03 11:47:45 -0800145 * fcoe_ctlr_init() - Initialize the FCoE Controller instance
146 * @fip: The FCoE controller to initialize
Joe Eykholt97c83892009-03-17 11:42:40 -0700147 */
Joe Eykholt3d902ac2010-07-20 15:19:58 -0700148void fcoe_ctlr_init(struct fcoe_ctlr *fip, enum fip_state mode)
Joe Eykholt97c83892009-03-17 11:42:40 -0700149{
Joe Eykholt9b651da2010-07-20 15:20:24 -0700150 fcoe_ctlr_set_state(fip, FIP_ST_LINK_WAIT);
Joe Eykholt3d902ac2010-07-20 15:19:58 -0700151 fip->mode = mode;
Joe Eykholt97c83892009-03-17 11:42:40 -0700152 INIT_LIST_HEAD(&fip->fcfs);
Joe Eykholtfdb068c2010-07-20 15:19:47 -0700153 mutex_init(&fip->ctlr_mutex);
Joe Eykholt794d98e2010-11-30 16:19:56 -0800154 spin_lock_init(&fip->ctlr_lock);
Joe Eykholt97c83892009-03-17 11:42:40 -0700155 fip->flogi_oxid = FC_XID_UNKNOWN;
156 setup_timer(&fip->timer, fcoe_ctlr_timeout, (unsigned long)fip);
Joe Eykholt42913652010-03-12 16:08:23 -0800157 INIT_WORK(&fip->timer_work, fcoe_ctlr_timer_work);
Joe Eykholt97c83892009-03-17 11:42:40 -0700158 INIT_WORK(&fip->recv_work, fcoe_ctlr_recv_work);
159 skb_queue_head_init(&fip->fip_recv_list);
160}
161EXPORT_SYMBOL(fcoe_ctlr_init);
162
163/**
Robert Love70b51aa2009-11-03 11:47:45 -0800164 * fcoe_ctlr_reset_fcfs() - Reset and free all FCFs for a controller
165 * @fip: The FCoE controller whose FCFs are to be reset
Joe Eykholt97c83892009-03-17 11:42:40 -0700166 *
167 * Called with &fcoe_ctlr lock held.
168 */
169static void fcoe_ctlr_reset_fcfs(struct fcoe_ctlr *fip)
170{
171 struct fcoe_fcf *fcf;
172 struct fcoe_fcf *next;
173
174 fip->sel_fcf = NULL;
175 list_for_each_entry_safe(fcf, next, &fip->fcfs, list) {
176 list_del(&fcf->list);
177 kfree(fcf);
178 }
179 fip->fcf_count = 0;
180 fip->sel_time = 0;
181}
182
183/**
Robert Love70b51aa2009-11-03 11:47:45 -0800184 * fcoe_ctlr_destroy() - Disable and tear down a FCoE controller
185 * @fip: The FCoE controller to tear down
Joe Eykholt97c83892009-03-17 11:42:40 -0700186 *
187 * This is called by FCoE drivers before freeing the &fcoe_ctlr.
188 *
189 * The receive handler will have been deleted before this to guarantee
190 * that no more recv_work will be scheduled.
191 *
192 * The timer routine will simply return once we set FIP_ST_DISABLED.
193 * This guarantees that no further timeouts or work will be scheduled.
194 */
195void fcoe_ctlr_destroy(struct fcoe_ctlr *fip)
196{
Chris Leecha4b7cfa2009-08-25 13:59:08 -0700197 cancel_work_sync(&fip->recv_work);
Joe Eykholt1f4aed82009-11-03 11:48:22 -0800198 skb_queue_purge(&fip->fip_recv_list);
Chris Leecha4b7cfa2009-08-25 13:59:08 -0700199
Joe Eykholtfdb068c2010-07-20 15:19:47 -0700200 mutex_lock(&fip->ctlr_mutex);
Joe Eykholt9b651da2010-07-20 15:20:24 -0700201 fcoe_ctlr_set_state(fip, FIP_ST_DISABLED);
Joe Eykholt97c83892009-03-17 11:42:40 -0700202 fcoe_ctlr_reset_fcfs(fip);
Joe Eykholtfdb068c2010-07-20 15:19:47 -0700203 mutex_unlock(&fip->ctlr_mutex);
Joe Eykholt97c83892009-03-17 11:42:40 -0700204 del_timer_sync(&fip->timer);
Joe Eykholt42913652010-03-12 16:08:23 -0800205 cancel_work_sync(&fip->timer_work);
Joe Eykholt97c83892009-03-17 11:42:40 -0700206}
207EXPORT_SYMBOL(fcoe_ctlr_destroy);
208
209/**
Joe Eykholt794d98e2010-11-30 16:19:56 -0800210 * fcoe_ctlr_announce() - announce new FCF selection
Joe Eykholt69316ee2010-11-30 16:19:40 -0800211 * @fip: The FCoE controller
212 *
213 * Also sets the destination MAC for FCoE and control packets
Joe Eykholt794d98e2010-11-30 16:19:56 -0800214 *
215 * Called with neither ctlr_mutex nor ctlr_lock held.
Joe Eykholt69316ee2010-11-30 16:19:40 -0800216 */
217static void fcoe_ctlr_announce(struct fcoe_ctlr *fip)
218{
Joe Eykholt794d98e2010-11-30 16:19:56 -0800219 struct fcoe_fcf *sel;
220 struct fcoe_fcf *fcf;
221
222 mutex_lock(&fip->ctlr_mutex);
223 spin_lock_bh(&fip->ctlr_lock);
224
225 kfree_skb(fip->flogi_req);
226 fip->flogi_req = NULL;
227 list_for_each_entry(fcf, &fip->fcfs, list)
228 fcf->flogi_sent = 0;
229
230 spin_unlock_bh(&fip->ctlr_lock);
231 sel = fip->sel_fcf;
Joe Eykholt69316ee2010-11-30 16:19:40 -0800232
233 if (sel && !compare_ether_addr(sel->fcf_mac, fip->dest_addr))
Joe Eykholt794d98e2010-11-30 16:19:56 -0800234 goto unlock;
Joe Eykholt69316ee2010-11-30 16:19:40 -0800235 if (!is_zero_ether_addr(fip->dest_addr)) {
236 printk(KERN_NOTICE "libfcoe: host%d: "
237 "FIP Fibre-Channel Forwarder MAC %pM deselected\n",
238 fip->lp->host->host_no, fip->dest_addr);
239 memset(fip->dest_addr, 0, ETH_ALEN);
240 }
241 if (sel) {
242 printk(KERN_INFO "libfcoe: host%d: FIP selected "
243 "Fibre-Channel Forwarder MAC %pM\n",
244 fip->lp->host->host_no, sel->fcf_mac);
Bhanu Prakash Gollapudi81c11dd2012-03-09 14:50:03 -0800245 memcpy(fip->dest_addr, sel->fcoe_mac, ETH_ALEN);
Joe Eykholt69316ee2010-11-30 16:19:40 -0800246 fip->map_dest = 0;
247 }
Joe Eykholt794d98e2010-11-30 16:19:56 -0800248unlock:
249 mutex_unlock(&fip->ctlr_mutex);
Joe Eykholt69316ee2010-11-30 16:19:40 -0800250}
251
252/**
Robert Love70b51aa2009-11-03 11:47:45 -0800253 * fcoe_ctlr_fcoe_size() - Return the maximum FCoE size required for VN_Port
254 * @fip: The FCoE controller to get the maximum FCoE size from
Joe Eykholt97c83892009-03-17 11:42:40 -0700255 *
256 * Returns the maximum packet size including the FCoE header and trailer,
257 * but not including any Ethernet or VLAN headers.
258 */
259static inline u32 fcoe_ctlr_fcoe_size(struct fcoe_ctlr *fip)
260{
261 /*
262 * Determine the max FCoE frame size allowed, including
263 * FCoE header and trailer.
264 * Note: lp->mfs is currently the payload size, not the frame size.
265 */
266 return fip->lp->mfs + sizeof(struct fc_frame_header) +
267 sizeof(struct fcoe_hdr) + sizeof(struct fcoe_crc_eof);
268}
269
270/**
Robert Love70b51aa2009-11-03 11:47:45 -0800271 * fcoe_ctlr_solicit() - Send a FIP solicitation
272 * @fip: The FCoE controller to send the solicitation on
273 * @fcf: The destination FCF (if NULL, a multicast solicitation is sent)
Joe Eykholt97c83892009-03-17 11:42:40 -0700274 */
275static void fcoe_ctlr_solicit(struct fcoe_ctlr *fip, struct fcoe_fcf *fcf)
276{
277 struct sk_buff *skb;
278 struct fip_sol {
279 struct ethhdr eth;
280 struct fip_header fip;
281 struct {
282 struct fip_mac_desc mac;
283 struct fip_wwn_desc wwnn;
284 struct fip_size_desc size;
Yi Zou0095a922011-01-28 16:05:00 -0800285 } __packed desc;
286 } __packed * sol;
Joe Eykholt97c83892009-03-17 11:42:40 -0700287 u32 fcoe_size;
288
289 skb = dev_alloc_skb(sizeof(*sol));
290 if (!skb)
291 return;
292
293 sol = (struct fip_sol *)skb->data;
294
295 memset(sol, 0, sizeof(*sol));
296 memcpy(sol->eth.h_dest, fcf ? fcf->fcf_mac : fcoe_all_fcfs, ETH_ALEN);
297 memcpy(sol->eth.h_source, fip->ctl_src_addr, ETH_ALEN);
298 sol->eth.h_proto = htons(ETH_P_FIP);
299
300 sol->fip.fip_ver = FIP_VER_ENCAPS(FIP_VER);
301 sol->fip.fip_op = htons(FIP_OP_DISC);
302 sol->fip.fip_subcode = FIP_SC_SOL;
303 sol->fip.fip_dl_len = htons(sizeof(sol->desc) / FIP_BPW);
304 sol->fip.fip_flags = htons(FIP_FL_FPMA);
Vasu Dev184dd342009-05-17 12:33:28 +0000305 if (fip->spma)
306 sol->fip.fip_flags |= htons(FIP_FL_SPMA);
Joe Eykholt97c83892009-03-17 11:42:40 -0700307
308 sol->desc.mac.fd_desc.fip_dtype = FIP_DT_MAC;
309 sol->desc.mac.fd_desc.fip_dlen = sizeof(sol->desc.mac) / FIP_BPW;
310 memcpy(sol->desc.mac.fd_mac, fip->ctl_src_addr, ETH_ALEN);
311
312 sol->desc.wwnn.fd_desc.fip_dtype = FIP_DT_NAME;
313 sol->desc.wwnn.fd_desc.fip_dlen = sizeof(sol->desc.wwnn) / FIP_BPW;
314 put_unaligned_be64(fip->lp->wwnn, &sol->desc.wwnn.fd_wwn);
315
316 fcoe_size = fcoe_ctlr_fcoe_size(fip);
317 sol->desc.size.fd_desc.fip_dtype = FIP_DT_FCOE_SIZE;
318 sol->desc.size.fd_desc.fip_dlen = sizeof(sol->desc.size) / FIP_BPW;
319 sol->desc.size.fd_size = htons(fcoe_size);
320
321 skb_put(skb, sizeof(*sol));
Chris Leech0f491532009-05-06 10:52:18 -0700322 skb->protocol = htons(ETH_P_FIP);
john fastabend6f6c2aa2011-11-18 13:35:56 -0800323 skb->priority = fip->priority;
Joe Eykholt97c83892009-03-17 11:42:40 -0700324 skb_reset_mac_header(skb);
325 skb_reset_network_header(skb);
326 fip->send(fip, skb);
327
328 if (!fcf)
329 fip->sol_time = jiffies;
330}
331
332/**
Robert Love70b51aa2009-11-03 11:47:45 -0800333 * fcoe_ctlr_link_up() - Start FCoE controller
334 * @fip: The FCoE controller to start
Joe Eykholt97c83892009-03-17 11:42:40 -0700335 *
336 * Called from the LLD when the network link is ready.
337 */
338void fcoe_ctlr_link_up(struct fcoe_ctlr *fip)
339{
Joe Eykholtfdb068c2010-07-20 15:19:47 -0700340 mutex_lock(&fip->ctlr_mutex);
Joe Eykholt97c83892009-03-17 11:42:40 -0700341 if (fip->state == FIP_ST_NON_FIP || fip->state == FIP_ST_AUTO) {
Joe Eykholtfdb068c2010-07-20 15:19:47 -0700342 mutex_unlock(&fip->ctlr_mutex);
Joe Eykholt97c83892009-03-17 11:42:40 -0700343 fc_linkup(fip->lp);
344 } else if (fip->state == FIP_ST_LINK_WAIT) {
Joe Eykholt9b651da2010-07-20 15:20:24 -0700345 fcoe_ctlr_set_state(fip, fip->mode);
Joe Eykholte10f8c62010-07-20 15:20:30 -0700346 switch (fip->mode) {
347 default:
348 LIBFCOE_FIP_DBG(fip, "invalid mode %d\n", fip->mode);
349 /* fall-through */
350 case FIP_MODE_AUTO:
Joe Eykholt0f51c2e2009-11-03 11:48:16 -0800351 LIBFCOE_FIP_DBG(fip, "%s", "setting AUTO mode.\n");
Joe Eykholte10f8c62010-07-20 15:20:30 -0700352 /* fall-through */
353 case FIP_MODE_FABRIC:
354 case FIP_MODE_NON_FIP:
355 mutex_unlock(&fip->ctlr_mutex);
356 fc_linkup(fip->lp);
357 fcoe_ctlr_solicit(fip, NULL);
358 break;
359 case FIP_MODE_VN2VN:
360 fcoe_ctlr_vn_start(fip);
361 mutex_unlock(&fip->ctlr_mutex);
362 fc_linkup(fip->lp);
363 break;
364 }
Joe Eykholt97c83892009-03-17 11:42:40 -0700365 } else
Joe Eykholtfdb068c2010-07-20 15:19:47 -0700366 mutex_unlock(&fip->ctlr_mutex);
Joe Eykholt97c83892009-03-17 11:42:40 -0700367}
368EXPORT_SYMBOL(fcoe_ctlr_link_up);
369
370/**
Robert Love70b51aa2009-11-03 11:47:45 -0800371 * fcoe_ctlr_reset() - Reset a FCoE controller
372 * @fip: The FCoE controller to reset
Joe Eykholt97c83892009-03-17 11:42:40 -0700373 */
Joe Eykholtdd42dac2009-11-03 11:48:27 -0800374static void fcoe_ctlr_reset(struct fcoe_ctlr *fip)
Joe Eykholt97c83892009-03-17 11:42:40 -0700375{
Joe Eykholt97c83892009-03-17 11:42:40 -0700376 fcoe_ctlr_reset_fcfs(fip);
377 del_timer(&fip->timer);
Joe Eykholt97c83892009-03-17 11:42:40 -0700378 fip->ctlr_ka_time = 0;
379 fip->port_ka_time = 0;
380 fip->sol_time = 0;
381 fip->flogi_oxid = FC_XID_UNKNOWN;
Joe Eykholtcd229e42010-07-20 15:20:40 -0700382 fcoe_ctlr_map_dest(fip);
Joe Eykholt97c83892009-03-17 11:42:40 -0700383}
384
385/**
Robert Love70b51aa2009-11-03 11:47:45 -0800386 * fcoe_ctlr_link_down() - Stop a FCoE controller
387 * @fip: The FCoE controller to be stopped
Joe Eykholt97c83892009-03-17 11:42:40 -0700388 *
389 * Returns non-zero if the link was up and now isn't.
390 *
391 * Called from the LLD when the network link is not ready.
392 * There may be multiple calls while the link is down.
393 */
394int fcoe_ctlr_link_down(struct fcoe_ctlr *fip)
395{
Joe Eykholtdd42dac2009-11-03 11:48:27 -0800396 int link_dropped;
397
398 LIBFCOE_FIP_DBG(fip, "link down.\n");
Joe Eykholtfdb068c2010-07-20 15:19:47 -0700399 mutex_lock(&fip->ctlr_mutex);
Joe Eykholtdd42dac2009-11-03 11:48:27 -0800400 fcoe_ctlr_reset(fip);
Joe Eykholt42913652010-03-12 16:08:23 -0800401 link_dropped = fip->state != FIP_ST_LINK_WAIT;
Joe Eykholt9b651da2010-07-20 15:20:24 -0700402 fcoe_ctlr_set_state(fip, FIP_ST_LINK_WAIT);
Joe Eykholtfdb068c2010-07-20 15:19:47 -0700403 mutex_unlock(&fip->ctlr_mutex);
Joe Eykholtdd42dac2009-11-03 11:48:27 -0800404
405 if (link_dropped)
406 fc_linkdown(fip->lp);
407 return link_dropped;
Joe Eykholt97c83892009-03-17 11:42:40 -0700408}
409EXPORT_SYMBOL(fcoe_ctlr_link_down);
410
411/**
Robert Love70b51aa2009-11-03 11:47:45 -0800412 * fcoe_ctlr_send_keep_alive() - Send a keep-alive to the selected FCF
413 * @fip: The FCoE controller to send the FKA on
414 * @lport: libfc fc_lport to send from
415 * @ports: 0 for controller keep-alive, 1 for port keep-alive
416 * @sa: The source MAC address
Joe Eykholt97c83892009-03-17 11:42:40 -0700417 *
418 * A controller keep-alive is sent every fka_period (typically 8 seconds).
419 * The source MAC is the native MAC address.
420 *
421 * A port keep-alive is sent every 90 seconds while logged in.
422 * The source MAC is the assigned mapped source address.
423 * The destination is the FCF's F-port.
424 */
Chris Leech11b56182009-11-03 11:46:29 -0800425static void fcoe_ctlr_send_keep_alive(struct fcoe_ctlr *fip,
426 struct fc_lport *lport,
427 int ports, u8 *sa)
Joe Eykholt97c83892009-03-17 11:42:40 -0700428{
429 struct sk_buff *skb;
430 struct fip_kal {
431 struct ethhdr eth;
432 struct fip_header fip;
433 struct fip_mac_desc mac;
Yi Zou0095a922011-01-28 16:05:00 -0800434 } __packed * kal;
Joe Eykholt97c83892009-03-17 11:42:40 -0700435 struct fip_vn_desc *vn;
436 u32 len;
437 struct fc_lport *lp;
438 struct fcoe_fcf *fcf;
439
440 fcf = fip->sel_fcf;
441 lp = fip->lp;
Joe Eykholt281ae642010-06-11 16:43:33 -0700442 if (!fcf || (ports && !lp->port_id))
Joe Eykholt97c83892009-03-17 11:42:40 -0700443 return;
444
Yi Zoube276cb2009-11-03 11:50:16 -0800445 len = sizeof(*kal) + ports * sizeof(*vn);
Joe Eykholt97c83892009-03-17 11:42:40 -0700446 skb = dev_alloc_skb(len);
447 if (!skb)
448 return;
449
450 kal = (struct fip_kal *)skb->data;
451 memset(kal, 0, len);
452 memcpy(kal->eth.h_dest, fcf->fcf_mac, ETH_ALEN);
453 memcpy(kal->eth.h_source, sa, ETH_ALEN);
454 kal->eth.h_proto = htons(ETH_P_FIP);
455
456 kal->fip.fip_ver = FIP_VER_ENCAPS(FIP_VER);
457 kal->fip.fip_op = htons(FIP_OP_CTRL);
458 kal->fip.fip_subcode = FIP_SC_KEEP_ALIVE;
459 kal->fip.fip_dl_len = htons((sizeof(kal->mac) +
Robert Love70b51aa2009-11-03 11:47:45 -0800460 ports * sizeof(*vn)) / FIP_BPW);
Joe Eykholt97c83892009-03-17 11:42:40 -0700461 kal->fip.fip_flags = htons(FIP_FL_FPMA);
Vasu Dev184dd342009-05-17 12:33:28 +0000462 if (fip->spma)
463 kal->fip.fip_flags |= htons(FIP_FL_SPMA);
Joe Eykholt97c83892009-03-17 11:42:40 -0700464
465 kal->mac.fd_desc.fip_dtype = FIP_DT_MAC;
466 kal->mac.fd_desc.fip_dlen = sizeof(kal->mac) / FIP_BPW;
467 memcpy(kal->mac.fd_mac, fip->ctl_src_addr, ETH_ALEN);
Joe Eykholt97c83892009-03-17 11:42:40 -0700468 if (ports) {
469 vn = (struct fip_vn_desc *)(kal + 1);
470 vn->fd_desc.fip_dtype = FIP_DT_VN_ID;
471 vn->fd_desc.fip_dlen = sizeof(*vn) / FIP_BPW;
Chris Leech11b56182009-11-03 11:46:29 -0800472 memcpy(vn->fd_mac, fip->get_src_addr(lport), ETH_ALEN);
Kaladhar Musunurufb831532010-05-07 15:18:57 -0700473 hton24(vn->fd_fc_id, lport->port_id);
474 put_unaligned_be64(lport->wwpn, &vn->fd_wwpn);
Joe Eykholt97c83892009-03-17 11:42:40 -0700475 }
Joe Eykholt97c83892009-03-17 11:42:40 -0700476 skb_put(skb, len);
Chris Leech0f491532009-05-06 10:52:18 -0700477 skb->protocol = htons(ETH_P_FIP);
john fastabend6f6c2aa2011-11-18 13:35:56 -0800478 skb->priority = fip->priority;
Joe Eykholt97c83892009-03-17 11:42:40 -0700479 skb_reset_mac_header(skb);
480 skb_reset_network_header(skb);
481 fip->send(fip, skb);
482}
483
484/**
Robert Love70b51aa2009-11-03 11:47:45 -0800485 * fcoe_ctlr_encaps() - Encapsulate an ELS frame for FIP, without sending it
486 * @fip: The FCoE controller for the ELS frame
487 * @dtype: The FIP descriptor type for the frame
488 * @skb: The FCoE ELS frame including FC header but no FCoE headers
Joe Eykholte10f8c62010-07-20 15:20:30 -0700489 * @d_id: The destination port ID.
Joe Eykholt97c83892009-03-17 11:42:40 -0700490 *
491 * Returns non-zero error code on failure.
492 *
493 * The caller must check that the length is a multiple of 4.
494 *
495 * The @skb must have enough headroom (28 bytes) and tailroom (8 bytes).
496 * Headroom includes the FIP encapsulation description, FIP header, and
497 * Ethernet header. The tailroom is for the FIP MAC descriptor.
498 */
Chris Leech11b56182009-11-03 11:46:29 -0800499static int fcoe_ctlr_encaps(struct fcoe_ctlr *fip, struct fc_lport *lport,
Joe Eykholte10f8c62010-07-20 15:20:30 -0700500 u8 dtype, struct sk_buff *skb, u32 d_id)
Joe Eykholt97c83892009-03-17 11:42:40 -0700501{
502 struct fip_encaps_head {
503 struct ethhdr eth;
504 struct fip_header fip;
505 struct fip_encaps encaps;
Yi Zou0095a922011-01-28 16:05:00 -0800506 } __packed * cap;
Joe Eykholt55543452010-07-20 15:20:35 -0700507 struct fc_frame_header *fh;
Joe Eykholt97c83892009-03-17 11:42:40 -0700508 struct fip_mac_desc *mac;
509 struct fcoe_fcf *fcf;
510 size_t dlen;
Yi Zou5a84bae2009-07-29 17:03:55 -0700511 u16 fip_flags;
Joe Eykholt55543452010-07-20 15:20:35 -0700512 u8 op;
Joe Eykholt97c83892009-03-17 11:42:40 -0700513
Joe Eykholt55543452010-07-20 15:20:35 -0700514 fh = (struct fc_frame_header *)skb->data;
515 op = *(u8 *)(fh + 1);
Joe Eykholt97c83892009-03-17 11:42:40 -0700516 dlen = sizeof(struct fip_encaps) + skb->len; /* len before push */
517 cap = (struct fip_encaps_head *)skb_push(skb, sizeof(*cap));
Joe Eykholt97c83892009-03-17 11:42:40 -0700518 memset(cap, 0, sizeof(*cap));
Joe Eykholte10f8c62010-07-20 15:20:30 -0700519
520 if (lport->point_to_multipoint) {
521 if (fcoe_ctlr_vn_lookup(fip, d_id, cap->eth.h_dest))
522 return -ENODEV;
Joe Eykholt55543452010-07-20 15:20:35 -0700523 fip_flags = 0;
Joe Eykholte10f8c62010-07-20 15:20:30 -0700524 } else {
525 fcf = fip->sel_fcf;
526 if (!fcf)
527 return -ENODEV;
528 fip_flags = fcf->flags;
529 fip_flags &= fip->spma ? FIP_FL_SPMA | FIP_FL_FPMA :
530 FIP_FL_FPMA;
531 if (!fip_flags)
532 return -ENODEV;
533 memcpy(cap->eth.h_dest, fcf->fcf_mac, ETH_ALEN);
534 }
Joe Eykholt97c83892009-03-17 11:42:40 -0700535 memcpy(cap->eth.h_source, fip->ctl_src_addr, ETH_ALEN);
536 cap->eth.h_proto = htons(ETH_P_FIP);
537
538 cap->fip.fip_ver = FIP_VER_ENCAPS(FIP_VER);
539 cap->fip.fip_op = htons(FIP_OP_LS);
Joe Eykholt55543452010-07-20 15:20:35 -0700540 if (op == ELS_LS_ACC || op == ELS_LS_RJT)
541 cap->fip.fip_subcode = FIP_SC_REP;
542 else
543 cap->fip.fip_subcode = FIP_SC_REQ;
Yi Zou5a84bae2009-07-29 17:03:55 -0700544 cap->fip.fip_flags = htons(fip_flags);
Joe Eykholt97c83892009-03-17 11:42:40 -0700545
546 cap->encaps.fd_desc.fip_dtype = dtype;
547 cap->encaps.fd_desc.fip_dlen = dlen / FIP_BPW;
548
Joe Eykholt55543452010-07-20 15:20:35 -0700549 if (op != ELS_LS_RJT) {
550 dlen += sizeof(*mac);
551 mac = (struct fip_mac_desc *)skb_put(skb, sizeof(*mac));
552 memset(mac, 0, sizeof(*mac));
553 mac->fd_desc.fip_dtype = FIP_DT_MAC;
554 mac->fd_desc.fip_dlen = sizeof(*mac) / FIP_BPW;
555 if (dtype != FIP_DT_FLOGI && dtype != FIP_DT_FDISC) {
556 memcpy(mac->fd_mac, fip->get_src_addr(lport), ETH_ALEN);
557 } else if (fip->mode == FIP_MODE_VN2VN) {
558 hton24(mac->fd_mac, FIP_VN_FC_MAP);
559 hton24(mac->fd_mac + 3, fip->port_id);
560 } else if (fip_flags & FIP_FL_SPMA) {
561 LIBFCOE_FIP_DBG(fip, "FLOGI/FDISC sent with SPMA\n");
562 memcpy(mac->fd_mac, fip->ctl_src_addr, ETH_ALEN);
563 } else {
564 LIBFCOE_FIP_DBG(fip, "FLOGI/FDISC sent with FPMA\n");
565 /* FPMA only FLOGI. Must leave the MAC desc zeroed. */
566 }
Robert Love593abc02010-04-09 14:22:17 -0700567 }
Joe Eykholt55543452010-07-20 15:20:35 -0700568 cap->fip.fip_dl_len = htons(dlen / FIP_BPW);
Joe Eykholt97c83892009-03-17 11:42:40 -0700569
Chris Leech0f491532009-05-06 10:52:18 -0700570 skb->protocol = htons(ETH_P_FIP);
john fastabend6f6c2aa2011-11-18 13:35:56 -0800571 skb->priority = fip->priority;
Joe Eykholt97c83892009-03-17 11:42:40 -0700572 skb_reset_mac_header(skb);
573 skb_reset_network_header(skb);
574 return 0;
575}
576
577/**
578 * fcoe_ctlr_els_send() - Send an ELS frame encapsulated by FIP if appropriate.
579 * @fip: FCoE controller.
Chris Leech11b56182009-11-03 11:46:29 -0800580 * @lport: libfc fc_lport to send from
Joe Eykholt97c83892009-03-17 11:42:40 -0700581 * @skb: FCoE ELS frame including FC header but no FCoE headers.
582 *
583 * Returns a non-zero error code if the frame should not be sent.
584 * Returns zero if the caller should send the frame with FCoE encapsulation.
585 *
586 * The caller must check that the length is a multiple of 4.
587 * The SKB must have enough headroom (28 bytes) and tailroom (8 bytes).
Joe Eykholte10f8c62010-07-20 15:20:30 -0700588 * The the skb must also be an fc_frame.
Joe Eykholt794d98e2010-11-30 16:19:56 -0800589 *
590 * This is called from the lower-level driver with spinlocks held,
591 * so we must not take a mutex here.
Joe Eykholt97c83892009-03-17 11:42:40 -0700592 */
Chris Leech11b56182009-11-03 11:46:29 -0800593int fcoe_ctlr_els_send(struct fcoe_ctlr *fip, struct fc_lport *lport,
594 struct sk_buff *skb)
Joe Eykholt97c83892009-03-17 11:42:40 -0700595{
Joe Eykholte10f8c62010-07-20 15:20:30 -0700596 struct fc_frame *fp;
Joe Eykholt97c83892009-03-17 11:42:40 -0700597 struct fc_frame_header *fh;
598 u16 old_xid;
599 u8 op;
Chris Leech11b56182009-11-03 11:46:29 -0800600 u8 mac[ETH_ALEN];
Joe Eykholt97c83892009-03-17 11:42:40 -0700601
Joe Eykholte10f8c62010-07-20 15:20:30 -0700602 fp = container_of(skb, struct fc_frame, skb);
Joe Eykholt97c83892009-03-17 11:42:40 -0700603 fh = (struct fc_frame_header *)skb->data;
604 op = *(u8 *)(fh + 1);
605
Joe Eykholte10f8c62010-07-20 15:20:30 -0700606 if (op == ELS_FLOGI && fip->mode != FIP_MODE_VN2VN) {
Joe Eykholt97c83892009-03-17 11:42:40 -0700607 old_xid = fip->flogi_oxid;
608 fip->flogi_oxid = ntohs(fh->fh_ox_id);
609 if (fip->state == FIP_ST_AUTO) {
610 if (old_xid == FC_XID_UNKNOWN)
611 fip->flogi_count = 0;
612 fip->flogi_count++;
613 if (fip->flogi_count < 3)
614 goto drop;
Joe Eykholtcd229e42010-07-20 15:20:40 -0700615 fcoe_ctlr_map_dest(fip);
Joe Eykholt97c83892009-03-17 11:42:40 -0700616 return 0;
617 }
Joe Eykholt5f48f702009-05-06 10:52:12 -0700618 if (fip->state == FIP_ST_NON_FIP)
Joe Eykholtcd229e42010-07-20 15:20:40 -0700619 fcoe_ctlr_map_dest(fip);
Joe Eykholt5f48f702009-05-06 10:52:12 -0700620 }
621
622 if (fip->state == FIP_ST_NON_FIP)
623 return 0;
Joe Eykholte10f8c62010-07-20 15:20:30 -0700624 if (!fip->sel_fcf && fip->mode != FIP_MODE_VN2VN)
Joe Eykholtf31f2a12009-11-03 11:48:32 -0800625 goto drop;
Joe Eykholt5f48f702009-05-06 10:52:12 -0700626 switch (op) {
627 case ELS_FLOGI:
Joe Eykholt97c83892009-03-17 11:42:40 -0700628 op = FIP_DT_FLOGI;
Joe Eykholt794d98e2010-11-30 16:19:56 -0800629 if (fip->mode == FIP_MODE_VN2VN)
630 break;
631 spin_lock_bh(&fip->ctlr_lock);
632 kfree_skb(fip->flogi_req);
633 fip->flogi_req = skb;
634 fip->flogi_req_send = 1;
635 spin_unlock_bh(&fip->ctlr_lock);
636 schedule_work(&fip->timer_work);
637 return -EINPROGRESS;
Joe Eykholt97c83892009-03-17 11:42:40 -0700638 case ELS_FDISC:
639 if (ntoh24(fh->fh_s_id))
640 return 0;
641 op = FIP_DT_FDISC;
642 break;
643 case ELS_LOGO:
Joe Eykholte10f8c62010-07-20 15:20:30 -0700644 if (fip->mode == FIP_MODE_VN2VN) {
645 if (fip->state != FIP_ST_VNMP_UP)
646 return -EINVAL;
647 if (ntoh24(fh->fh_d_id) == FC_FID_FLOGI)
648 return -EINVAL;
649 } else {
650 if (fip->state != FIP_ST_ENABLED)
651 return 0;
652 if (ntoh24(fh->fh_d_id) != FC_FID_FLOGI)
653 return 0;
654 }
Joe Eykholt97c83892009-03-17 11:42:40 -0700655 op = FIP_DT_LOGO;
656 break;
657 case ELS_LS_ACC:
Joe Eykholt97c83892009-03-17 11:42:40 -0700658 /*
Joe Eykholte10f8c62010-07-20 15:20:30 -0700659 * If non-FIP, we may have gotten an SID by accepting an FLOGI
Joe Eykholt97c83892009-03-17 11:42:40 -0700660 * from a point-to-point connection. Switch to using
661 * the source mac based on the SID. The destination
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300662 * MAC in this case would have been set by receiving the
Joe Eykholt97c83892009-03-17 11:42:40 -0700663 * FLOGI.
664 */
Joe Eykholte10f8c62010-07-20 15:20:30 -0700665 if (fip->state == FIP_ST_NON_FIP) {
666 if (fip->flogi_oxid == FC_XID_UNKNOWN)
667 return 0;
668 fip->flogi_oxid = FC_XID_UNKNOWN;
669 fc_fcoe_set_mac(mac, fh->fh_d_id);
670 fip->update_mac(lport, mac);
671 }
672 /* fall through */
673 case ELS_LS_RJT:
674 op = fr_encaps(fp);
675 if (op)
676 break;
Joe Eykholt97c83892009-03-17 11:42:40 -0700677 return 0;
678 default:
Joe Eykholte10f8c62010-07-20 15:20:30 -0700679 if (fip->state != FIP_ST_ENABLED &&
680 fip->state != FIP_ST_VNMP_UP)
Joe Eykholt97c83892009-03-17 11:42:40 -0700681 goto drop;
682 return 0;
683 }
Joe Eykholte10f8c62010-07-20 15:20:30 -0700684 LIBFCOE_FIP_DBG(fip, "els_send op %u d_id %x\n",
685 op, ntoh24(fh->fh_d_id));
686 if (fcoe_ctlr_encaps(fip, lport, op, skb, ntoh24(fh->fh_d_id)))
Joe Eykholt97c83892009-03-17 11:42:40 -0700687 goto drop;
688 fip->send(fip, skb);
689 return -EINPROGRESS;
690drop:
691 kfree_skb(skb);
692 return -EINVAL;
693}
694EXPORT_SYMBOL(fcoe_ctlr_els_send);
695
Robert Love70b51aa2009-11-03 11:47:45 -0800696/**
697 * fcoe_ctlr_age_fcfs() - Reset and free all old FCFs for a controller
698 * @fip: The FCoE controller to free FCFs on
Joe Eykholt97c83892009-03-17 11:42:40 -0700699 *
Joe Eykholtf018b732010-03-12 16:08:55 -0800700 * Called with lock held and preemption disabled.
Joe Eykholt97c83892009-03-17 11:42:40 -0700701 *
Joe Eykholt8690cb82010-06-11 16:44:10 -0700702 * An FCF is considered old if we have missed two advertisements.
703 * That is, there have been no valid advertisement from it for 2.5
704 * times its keep-alive period.
Joe Eykholt97c83892009-03-17 11:42:40 -0700705 *
706 * In addition, determine the time when an FCF selection can occur.
Yi Zouf3da80e2009-11-20 14:55:08 -0800707 *
708 * Also, increment the MissDiscAdvCount when no advertisement is received
709 * for the corresponding FCF for 1.5 * FKA_ADV_PERIOD (FC-BB-5 LESB).
Joe Eykholt8690cb82010-06-11 16:44:10 -0700710 *
711 * Returns the time in jiffies for the next call.
Joe Eykholt97c83892009-03-17 11:42:40 -0700712 */
Joe Eykholt8690cb82010-06-11 16:44:10 -0700713static unsigned long fcoe_ctlr_age_fcfs(struct fcoe_ctlr *fip)
Joe Eykholt97c83892009-03-17 11:42:40 -0700714{
715 struct fcoe_fcf *fcf;
716 struct fcoe_fcf *next;
Joe Eykholt8690cb82010-06-11 16:44:10 -0700717 unsigned long next_timer = jiffies + msecs_to_jiffies(FIP_VN_KA_PERIOD);
718 unsigned long deadline;
Joe Eykholt97c83892009-03-17 11:42:40 -0700719 unsigned long sel_time = 0;
Joe Eykholtf018b732010-03-12 16:08:55 -0800720 struct fcoe_dev_stats *stats;
Joe Eykholt97c83892009-03-17 11:42:40 -0700721
Joe Eykholtfdb068c2010-07-20 15:19:47 -0700722 stats = per_cpu_ptr(fip->lp->dev_stats, get_cpu());
723
Joe Eykholt97c83892009-03-17 11:42:40 -0700724 list_for_each_entry_safe(fcf, next, &fip->fcfs, list) {
Joe Eykholt8690cb82010-06-11 16:44:10 -0700725 deadline = fcf->time + fcf->fka_period + fcf->fka_period / 2;
726 if (fip->sel_fcf == fcf) {
727 if (time_after(jiffies, deadline)) {
Joe Eykholt8690cb82010-06-11 16:44:10 -0700728 stats->MissDiscAdvCount++;
729 printk(KERN_INFO "libfcoe: host%d: "
730 "Missing Discovery Advertisement "
731 "for fab %16.16llx count %lld\n",
732 fip->lp->host->host_no, fcf->fabric_name,
733 stats->MissDiscAdvCount);
734 } else if (time_after(next_timer, deadline))
735 next_timer = deadline;
Yi Zouf3da80e2009-11-20 14:55:08 -0800736 }
Joe Eykholt8690cb82010-06-11 16:44:10 -0700737
738 deadline += fcf->fka_period;
Joe Eykholtd99ee452010-06-11 16:44:15 -0700739 if (time_after_eq(jiffies, deadline)) {
Joe Eykholt97c83892009-03-17 11:42:40 -0700740 if (fip->sel_fcf == fcf)
741 fip->sel_fcf = NULL;
742 list_del(&fcf->list);
743 WARN_ON(!fip->fcf_count);
744 fip->fcf_count--;
745 kfree(fcf);
Joe Eykholtf018b732010-03-12 16:08:55 -0800746 stats->VLinkFailureCount++;
Joe Eykholt8690cb82010-06-11 16:44:10 -0700747 } else {
748 if (time_after(next_timer, deadline))
749 next_timer = deadline;
750 if (fcoe_ctlr_mtu_valid(fcf) &&
751 (!sel_time || time_before(sel_time, fcf->time)))
752 sel_time = fcf->time;
Joe Eykholt97c83892009-03-17 11:42:40 -0700753 }
754 }
Joe Eykholtfdb068c2010-07-20 15:19:47 -0700755 put_cpu();
Joe Eykholtd99ee452010-06-11 16:44:15 -0700756 if (sel_time && !fip->sel_fcf && !fip->sel_time) {
Joe Eykholt97c83892009-03-17 11:42:40 -0700757 sel_time += msecs_to_jiffies(FCOE_CTLR_START_DELAY);
758 fip->sel_time = sel_time;
Joe Eykholt97c83892009-03-17 11:42:40 -0700759 }
Joe Eykholtd99ee452010-06-11 16:44:15 -0700760
Joe Eykholt8690cb82010-06-11 16:44:10 -0700761 return next_timer;
Joe Eykholt97c83892009-03-17 11:42:40 -0700762}
763
764/**
Robert Love70b51aa2009-11-03 11:47:45 -0800765 * fcoe_ctlr_parse_adv() - Decode a FIP advertisement into a new FCF entry
Joe Eykholt0f51c2e2009-11-03 11:48:16 -0800766 * @fip: The FCoE controller receiving the advertisement
Robert Love70b51aa2009-11-03 11:47:45 -0800767 * @skb: The received FIP advertisement frame
768 * @fcf: The resulting FCF entry
Joe Eykholt97c83892009-03-17 11:42:40 -0700769 *
770 * Returns zero on a valid parsed advertisement,
771 * otherwise returns non zero value.
772 */
Joe Eykholt0f51c2e2009-11-03 11:48:16 -0800773static int fcoe_ctlr_parse_adv(struct fcoe_ctlr *fip,
774 struct sk_buff *skb, struct fcoe_fcf *fcf)
Joe Eykholt97c83892009-03-17 11:42:40 -0700775{
776 struct fip_header *fiph;
777 struct fip_desc *desc = NULL;
778 struct fip_wwn_desc *wwn;
779 struct fip_fab_desc *fab;
780 struct fip_fka_desc *fka;
781 unsigned long t;
782 size_t rlen;
783 size_t dlen;
Bhanu Prakash Gollapudi0a9c5d32010-06-11 16:44:25 -0700784 u32 desc_mask;
Joe Eykholt97c83892009-03-17 11:42:40 -0700785
786 memset(fcf, 0, sizeof(*fcf));
787 fcf->fka_period = msecs_to_jiffies(FCOE_CTLR_DEF_FKA);
788
789 fiph = (struct fip_header *)skb->data;
790 fcf->flags = ntohs(fiph->fip_flags);
791
Bhanu Prakash Gollapudi0a9c5d32010-06-11 16:44:25 -0700792 /*
793 * mask of required descriptors. validating each one clears its bit.
794 */
795 desc_mask = BIT(FIP_DT_PRI) | BIT(FIP_DT_MAC) | BIT(FIP_DT_NAME) |
796 BIT(FIP_DT_FAB) | BIT(FIP_DT_FKA);
797
Joe Eykholt97c83892009-03-17 11:42:40 -0700798 rlen = ntohs(fiph->fip_dl_len) * 4;
799 if (rlen + sizeof(*fiph) > skb->len)
800 return -EINVAL;
801
802 desc = (struct fip_desc *)(fiph + 1);
803 while (rlen > 0) {
804 dlen = desc->fip_dlen * FIP_BPW;
805 if (dlen < sizeof(*desc) || dlen > rlen)
806 return -EINVAL;
Bhanu Prakash Gollapudi0a9c5d32010-06-11 16:44:25 -0700807 /* Drop Adv if there are duplicate critical descriptors */
808 if ((desc->fip_dtype < 32) &&
809 !(desc_mask & 1U << desc->fip_dtype)) {
810 LIBFCOE_FIP_DBG(fip, "Duplicate Critical "
811 "Descriptors in FIP adv\n");
812 return -EINVAL;
813 }
Joe Eykholt97c83892009-03-17 11:42:40 -0700814 switch (desc->fip_dtype) {
815 case FIP_DT_PRI:
816 if (dlen != sizeof(struct fip_pri_desc))
817 goto len_err;
818 fcf->pri = ((struct fip_pri_desc *)desc)->fd_pri;
Bhanu Prakash Gollapudi0a9c5d32010-06-11 16:44:25 -0700819 desc_mask &= ~BIT(FIP_DT_PRI);
Joe Eykholt97c83892009-03-17 11:42:40 -0700820 break;
821 case FIP_DT_MAC:
822 if (dlen != sizeof(struct fip_mac_desc))
823 goto len_err;
824 memcpy(fcf->fcf_mac,
825 ((struct fip_mac_desc *)desc)->fd_mac,
826 ETH_ALEN);
Bhanu Prakash Gollapudi81c11dd2012-03-09 14:50:03 -0800827 memcpy(fcf->fcoe_mac, fcf->fcf_mac, ETH_ALEN);
Joe Eykholt97c83892009-03-17 11:42:40 -0700828 if (!is_valid_ether_addr(fcf->fcf_mac)) {
Joe Eykholte10f8c62010-07-20 15:20:30 -0700829 LIBFCOE_FIP_DBG(fip,
830 "Invalid MAC addr %pM in FIP adv\n",
831 fcf->fcf_mac);
Joe Eykholt97c83892009-03-17 11:42:40 -0700832 return -EINVAL;
833 }
Bhanu Prakash Gollapudi0a9c5d32010-06-11 16:44:25 -0700834 desc_mask &= ~BIT(FIP_DT_MAC);
Joe Eykholt97c83892009-03-17 11:42:40 -0700835 break;
836 case FIP_DT_NAME:
837 if (dlen != sizeof(struct fip_wwn_desc))
838 goto len_err;
839 wwn = (struct fip_wwn_desc *)desc;
840 fcf->switch_name = get_unaligned_be64(&wwn->fd_wwn);
Bhanu Prakash Gollapudi0a9c5d32010-06-11 16:44:25 -0700841 desc_mask &= ~BIT(FIP_DT_NAME);
Joe Eykholt97c83892009-03-17 11:42:40 -0700842 break;
843 case FIP_DT_FAB:
844 if (dlen != sizeof(struct fip_fab_desc))
845 goto len_err;
846 fab = (struct fip_fab_desc *)desc;
847 fcf->fabric_name = get_unaligned_be64(&fab->fd_wwn);
848 fcf->vfid = ntohs(fab->fd_vfid);
849 fcf->fc_map = ntoh24(fab->fd_map);
Bhanu Prakash Gollapudi0a9c5d32010-06-11 16:44:25 -0700850 desc_mask &= ~BIT(FIP_DT_FAB);
Joe Eykholt97c83892009-03-17 11:42:40 -0700851 break;
852 case FIP_DT_FKA:
853 if (dlen != sizeof(struct fip_fka_desc))
854 goto len_err;
855 fka = (struct fip_fka_desc *)desc;
Yi Zou8cdffdc2009-11-20 14:54:57 -0800856 if (fka->fd_flags & FIP_FKA_ADV_D)
857 fcf->fd_flags = 1;
Joe Eykholt97c83892009-03-17 11:42:40 -0700858 t = ntohl(fka->fd_fka_period);
859 if (t >= FCOE_CTLR_MIN_FKA)
860 fcf->fka_period = msecs_to_jiffies(t);
Bhanu Prakash Gollapudi0a9c5d32010-06-11 16:44:25 -0700861 desc_mask &= ~BIT(FIP_DT_FKA);
Joe Eykholt97c83892009-03-17 11:42:40 -0700862 break;
863 case FIP_DT_MAP_OUI:
864 case FIP_DT_FCOE_SIZE:
865 case FIP_DT_FLOGI:
866 case FIP_DT_FDISC:
867 case FIP_DT_LOGO:
868 case FIP_DT_ELP:
869 default:
Joe Eykholt0f51c2e2009-11-03 11:48:16 -0800870 LIBFCOE_FIP_DBG(fip, "unexpected descriptor type %x "
Robert Love650bd122009-06-10 15:31:05 -0700871 "in FIP adv\n", desc->fip_dtype);
Joe Eykholt97c83892009-03-17 11:42:40 -0700872 /* standard says ignore unknown descriptors >= 128 */
873 if (desc->fip_dtype < FIP_DT_VENDOR_BASE)
874 return -EINVAL;
Bhanu Prakash Gollapudi1508f3ec2010-06-11 16:43:38 -0700875 break;
Joe Eykholt97c83892009-03-17 11:42:40 -0700876 }
877 desc = (struct fip_desc *)((char *)desc + dlen);
878 rlen -= dlen;
879 }
880 if (!fcf->fc_map || (fcf->fc_map & 0x10000))
881 return -EINVAL;
Vasu Dev240778e2010-07-20 15:21:33 -0700882 if (!fcf->switch_name)
Joe Eykholt97c83892009-03-17 11:42:40 -0700883 return -EINVAL;
Bhanu Prakash Gollapudi0a9c5d32010-06-11 16:44:25 -0700884 if (desc_mask) {
885 LIBFCOE_FIP_DBG(fip, "adv missing descriptors mask %x\n",
886 desc_mask);
887 return -EINVAL;
888 }
Joe Eykholt97c83892009-03-17 11:42:40 -0700889 return 0;
890
891len_err:
Joe Eykholt0f51c2e2009-11-03 11:48:16 -0800892 LIBFCOE_FIP_DBG(fip, "FIP length error in descriptor type %x len %zu\n",
Robert Love650bd122009-06-10 15:31:05 -0700893 desc->fip_dtype, dlen);
Joe Eykholt97c83892009-03-17 11:42:40 -0700894 return -EINVAL;
895}
896
897/**
Robert Love70b51aa2009-11-03 11:47:45 -0800898 * fcoe_ctlr_recv_adv() - Handle an incoming advertisement
899 * @fip: The FCoE controller receiving the advertisement
900 * @skb: The received FIP packet
Joe Eykholt97c83892009-03-17 11:42:40 -0700901 */
902static void fcoe_ctlr_recv_adv(struct fcoe_ctlr *fip, struct sk_buff *skb)
903{
904 struct fcoe_fcf *fcf;
905 struct fcoe_fcf new;
906 struct fcoe_fcf *found;
907 unsigned long sol_tov = msecs_to_jiffies(FCOE_CTRL_SOL_TOV);
908 int first = 0;
909 int mtu_valid;
910
Joe Eykholt0f51c2e2009-11-03 11:48:16 -0800911 if (fcoe_ctlr_parse_adv(fip, skb, &new))
Joe Eykholt97c83892009-03-17 11:42:40 -0700912 return;
913
Joe Eykholtfdb068c2010-07-20 15:19:47 -0700914 mutex_lock(&fip->ctlr_mutex);
Joe Eykholt97c83892009-03-17 11:42:40 -0700915 first = list_empty(&fip->fcfs);
916 found = NULL;
917 list_for_each_entry(fcf, &fip->fcfs, list) {
918 if (fcf->switch_name == new.switch_name &&
919 fcf->fabric_name == new.fabric_name &&
920 fcf->fc_map == new.fc_map &&
921 compare_ether_addr(fcf->fcf_mac, new.fcf_mac) == 0) {
922 found = fcf;
923 break;
924 }
925 }
926 if (!found) {
927 if (fip->fcf_count >= FCOE_CTLR_FCF_LIMIT)
928 goto out;
929
930 fcf = kmalloc(sizeof(*fcf), GFP_ATOMIC);
931 if (!fcf)
932 goto out;
933
934 fip->fcf_count++;
935 memcpy(fcf, &new, sizeof(new));
936 list_add(&fcf->list, &fip->fcfs);
937 } else {
938 /*
Joe Eykholtc600fea2010-06-11 16:44:20 -0700939 * Update the FCF's keep-alive descriptor flags.
940 * Other flag changes from new advertisements are
941 * ignored after a solicited advertisement is
942 * received and the FCF is selectable (usable).
Joe Eykholt97c83892009-03-17 11:42:40 -0700943 */
Joe Eykholtc600fea2010-06-11 16:44:20 -0700944 fcf->fd_flags = new.fd_flags;
945 if (!fcoe_ctlr_fcf_usable(fcf))
946 fcf->flags = new.flags;
947
Joe Eykholtd99ee452010-06-11 16:44:15 -0700948 if (fcf == fip->sel_fcf && !fcf->fd_flags) {
Joe Eykholt97c83892009-03-17 11:42:40 -0700949 fip->ctlr_ka_time -= fcf->fka_period;
950 fip->ctlr_ka_time += new.fka_period;
951 if (time_before(fip->ctlr_ka_time, fip->timer.expires))
952 mod_timer(&fip->timer, fip->ctlr_ka_time);
Joe Eykholtc600fea2010-06-11 16:44:20 -0700953 }
Joe Eykholt97c83892009-03-17 11:42:40 -0700954 fcf->fka_period = new.fka_period;
955 memcpy(fcf->fcf_mac, new.fcf_mac, ETH_ALEN);
956 }
957 mtu_valid = fcoe_ctlr_mtu_valid(fcf);
958 fcf->time = jiffies;
Joe Eykholt9069f5c2010-11-30 16:20:02 -0800959 if (!found)
960 LIBFCOE_FIP_DBG(fip, "New FCF fab %16.16llx mac %pM\n",
961 fcf->fabric_name, fcf->fcf_mac);
Joe Eykholt97c83892009-03-17 11:42:40 -0700962
963 /*
964 * If this advertisement is not solicited and our max receive size
965 * hasn't been verified, send a solicited advertisement.
966 */
967 if (!mtu_valid)
968 fcoe_ctlr_solicit(fip, fcf);
969
970 /*
971 * If its been a while since we did a solicit, and this is
972 * the first advertisement we've received, do a multicast
973 * solicitation to gather as many advertisements as we can
974 * before selection occurs.
975 */
976 if (first && time_after(jiffies, fip->sol_time + sol_tov))
977 fcoe_ctlr_solicit(fip, NULL);
978
979 /*
Joe Eykholt981c1152010-11-30 16:20:07 -0800980 * Put this FCF at the head of the list for priority among equals.
981 * This helps in the case of an NPV switch which insists we use
982 * the FCF that answers multicast solicitations, not the others that
983 * are sending periodic multicast advertisements.
984 */
Kirill A. Shutemov63ce2492011-04-01 16:06:09 -0700985 if (mtu_valid)
986 list_move(&fcf->list, &fip->fcfs);
Joe Eykholt981c1152010-11-30 16:20:07 -0800987
988 /*
Joe Eykholt97c83892009-03-17 11:42:40 -0700989 * If this is the first validated FCF, note the time and
990 * set a timer to trigger selection.
991 */
Joe Eykholtd99ee452010-06-11 16:44:15 -0700992 if (mtu_valid && !fip->sel_fcf && fcoe_ctlr_fcf_usable(fcf)) {
Joe Eykholt97c83892009-03-17 11:42:40 -0700993 fip->sel_time = jiffies +
Robert Love70b51aa2009-11-03 11:47:45 -0800994 msecs_to_jiffies(FCOE_CTLR_START_DELAY);
Joe Eykholt97c83892009-03-17 11:42:40 -0700995 if (!timer_pending(&fip->timer) ||
996 time_before(fip->sel_time, fip->timer.expires))
997 mod_timer(&fip->timer, fip->sel_time);
998 }
999out:
Joe Eykholtfdb068c2010-07-20 15:19:47 -07001000 mutex_unlock(&fip->ctlr_mutex);
Joe Eykholt97c83892009-03-17 11:42:40 -07001001}
1002
1003/**
Robert Love70b51aa2009-11-03 11:47:45 -08001004 * fcoe_ctlr_recv_els() - Handle an incoming FIP encapsulated ELS frame
1005 * @fip: The FCoE controller which received the packet
1006 * @skb: The received FIP packet
Joe Eykholt97c83892009-03-17 11:42:40 -07001007 */
1008static void fcoe_ctlr_recv_els(struct fcoe_ctlr *fip, struct sk_buff *skb)
1009{
Robert Love70b51aa2009-11-03 11:47:45 -08001010 struct fc_lport *lport = fip->lp;
Joe Eykholt97c83892009-03-17 11:42:40 -07001011 struct fip_header *fiph;
Chris Leech11b56182009-11-03 11:46:29 -08001012 struct fc_frame *fp = (struct fc_frame *)skb;
Joe Eykholt97c83892009-03-17 11:42:40 -07001013 struct fc_frame_header *fh = NULL;
1014 struct fip_desc *desc;
1015 struct fip_encaps *els;
1016 struct fcoe_dev_stats *stats;
Bhanu Prakash Gollapudi81c11dd2012-03-09 14:50:03 -08001017 struct fcoe_fcf *sel;
Joe Eykholt97c83892009-03-17 11:42:40 -07001018 enum fip_desc_type els_dtype = 0;
1019 u8 els_op;
1020 u8 sub;
1021 u8 granted_mac[ETH_ALEN] = { 0 };
1022 size_t els_len = 0;
1023 size_t rlen;
1024 size_t dlen;
Bhanu Prakash Gollapudibe613312010-06-11 16:44:36 -07001025 u32 desc_mask = 0;
1026 u32 desc_cnt = 0;
Joe Eykholt97c83892009-03-17 11:42:40 -07001027
1028 fiph = (struct fip_header *)skb->data;
1029 sub = fiph->fip_subcode;
1030 if (sub != FIP_SC_REQ && sub != FIP_SC_REP)
1031 goto drop;
1032
1033 rlen = ntohs(fiph->fip_dl_len) * 4;
1034 if (rlen + sizeof(*fiph) > skb->len)
1035 goto drop;
1036
1037 desc = (struct fip_desc *)(fiph + 1);
1038 while (rlen > 0) {
Bhanu Prakash Gollapudibe613312010-06-11 16:44:36 -07001039 desc_cnt++;
Joe Eykholt97c83892009-03-17 11:42:40 -07001040 dlen = desc->fip_dlen * FIP_BPW;
1041 if (dlen < sizeof(*desc) || dlen > rlen)
1042 goto drop;
Bhanu Prakash Gollapudi0a9c5d32010-06-11 16:44:25 -07001043 /* Drop ELS if there are duplicate critical descriptors */
1044 if (desc->fip_dtype < 32) {
Bhanu Prakash Gollapudi81c11dd2012-03-09 14:50:03 -08001045 if ((desc->fip_dtype != FIP_DT_MAC) &&
1046 (desc_mask & 1U << desc->fip_dtype)) {
Bhanu Prakash Gollapudi0a9c5d32010-06-11 16:44:25 -07001047 LIBFCOE_FIP_DBG(fip, "Duplicate Critical "
1048 "Descriptors in FIP ELS\n");
1049 goto drop;
1050 }
Bhanu Prakash Gollapudibe613312010-06-11 16:44:36 -07001051 desc_mask |= (1 << desc->fip_dtype);
Bhanu Prakash Gollapudi0a9c5d32010-06-11 16:44:25 -07001052 }
Joe Eykholt97c83892009-03-17 11:42:40 -07001053 switch (desc->fip_dtype) {
1054 case FIP_DT_MAC:
Bhanu Prakash Gollapudi81c11dd2012-03-09 14:50:03 -08001055 sel = fip->sel_fcf;
Bhanu Prakash Gollapudibe613312010-06-11 16:44:36 -07001056 if (desc_cnt == 1) {
1057 LIBFCOE_FIP_DBG(fip, "FIP descriptors "
1058 "received out of order\n");
1059 goto drop;
1060 }
Bhanu Prakash Gollapudi81c11dd2012-03-09 14:50:03 -08001061 /*
1062 * Some switch implementations send two MAC descriptors,
1063 * with first MAC(granted_mac) being the FPMA, and the
1064 * second one(fcoe_mac) is used as destination address
1065 * for sending/receiving FCoE packets. FIP traffic is
1066 * sent using fip_mac. For regular switches, both
1067 * fip_mac and fcoe_mac would be the same.
1068 */
1069 if (desc_cnt == 2)
1070 memcpy(granted_mac,
1071 ((struct fip_mac_desc *)desc)->fd_mac,
1072 ETH_ALEN);
Bhanu Prakash Gollapudibe613312010-06-11 16:44:36 -07001073
Joe Eykholt97c83892009-03-17 11:42:40 -07001074 if (dlen != sizeof(struct fip_mac_desc))
1075 goto len_err;
Bhanu Prakash Gollapudi81c11dd2012-03-09 14:50:03 -08001076
1077 if ((desc_cnt == 3) && (sel))
1078 memcpy(sel->fcoe_mac,
1079 ((struct fip_mac_desc *)desc)->fd_mac,
1080 ETH_ALEN);
Joe Eykholt97c83892009-03-17 11:42:40 -07001081 break;
1082 case FIP_DT_FLOGI:
1083 case FIP_DT_FDISC:
1084 case FIP_DT_LOGO:
1085 case FIP_DT_ELP:
Bhanu Prakash Gollapudibe613312010-06-11 16:44:36 -07001086 if (desc_cnt != 1) {
1087 LIBFCOE_FIP_DBG(fip, "FIP descriptors "
1088 "received out of order\n");
1089 goto drop;
1090 }
Joe Eykholt97c83892009-03-17 11:42:40 -07001091 if (fh)
1092 goto drop;
1093 if (dlen < sizeof(*els) + sizeof(*fh) + 1)
1094 goto len_err;
1095 els_len = dlen - sizeof(*els);
1096 els = (struct fip_encaps *)desc;
1097 fh = (struct fc_frame_header *)(els + 1);
1098 els_dtype = desc->fip_dtype;
1099 break;
1100 default:
Joe Eykholt0f51c2e2009-11-03 11:48:16 -08001101 LIBFCOE_FIP_DBG(fip, "unexpected descriptor type %x "
Robert Love650bd122009-06-10 15:31:05 -07001102 "in FIP adv\n", desc->fip_dtype);
Joe Eykholt97c83892009-03-17 11:42:40 -07001103 /* standard says ignore unknown descriptors >= 128 */
1104 if (desc->fip_dtype < FIP_DT_VENDOR_BASE)
1105 goto drop;
Bhanu Prakash Gollapudibe613312010-06-11 16:44:36 -07001106 if (desc_cnt <= 2) {
1107 LIBFCOE_FIP_DBG(fip, "FIP descriptors "
1108 "received out of order\n");
1109 goto drop;
1110 }
Bhanu Prakash Gollapudi1508f3ec2010-06-11 16:43:38 -07001111 break;
Joe Eykholt97c83892009-03-17 11:42:40 -07001112 }
1113 desc = (struct fip_desc *)((char *)desc + dlen);
1114 rlen -= dlen;
1115 }
1116
1117 if (!fh)
1118 goto drop;
1119 els_op = *(u8 *)(fh + 1);
1120
Joe Eykholte10f8c62010-07-20 15:20:30 -07001121 if ((els_dtype == FIP_DT_FLOGI || els_dtype == FIP_DT_FDISC) &&
Joe Eykholt794d98e2010-11-30 16:19:56 -08001122 sub == FIP_SC_REP && fip->mode != FIP_MODE_VN2VN) {
1123 if (els_op == ELS_LS_ACC) {
1124 if (!is_valid_ether_addr(granted_mac)) {
1125 LIBFCOE_FIP_DBG(fip,
1126 "Invalid MAC address %pM in FIP ELS\n",
1127 granted_mac);
1128 goto drop;
1129 }
1130 memcpy(fr_cb(fp)->granted_mac, granted_mac, ETH_ALEN);
Joe Eykholte10f8c62010-07-20 15:20:30 -07001131
Joe Eykholt794d98e2010-11-30 16:19:56 -08001132 if (fip->flogi_oxid == ntohs(fh->fh_ox_id)) {
1133 fip->flogi_oxid = FC_XID_UNKNOWN;
1134 if (els_dtype == FIP_DT_FLOGI)
1135 fcoe_ctlr_announce(fip);
1136 }
1137 } else if (els_dtype == FIP_DT_FLOGI &&
1138 !fcoe_ctlr_flogi_retry(fip))
1139 goto drop; /* retrying FLOGI so drop reject */
Joe Eykholte10f8c62010-07-20 15:20:30 -07001140 }
Joe Eykholt97c83892009-03-17 11:42:40 -07001141
Bhanu Prakash Gollapudibe613312010-06-11 16:44:36 -07001142 if ((desc_cnt == 0) || ((els_op != ELS_LS_RJT) &&
1143 (!(1U << FIP_DT_MAC & desc_mask)))) {
1144 LIBFCOE_FIP_DBG(fip, "Missing critical descriptors "
1145 "in FIP ELS\n");
1146 goto drop;
1147 }
1148
Joe Eykholt97c83892009-03-17 11:42:40 -07001149 /*
1150 * Convert skb into an fc_frame containing only the ELS.
1151 */
1152 skb_pull(skb, (u8 *)fh - skb->data);
1153 skb_trim(skb, els_len);
1154 fp = (struct fc_frame *)skb;
1155 fc_frame_init(fp);
1156 fr_sof(fp) = FC_SOF_I3;
1157 fr_eof(fp) = FC_EOF_T;
Robert Love70b51aa2009-11-03 11:47:45 -08001158 fr_dev(fp) = lport;
Joe Eykholte10f8c62010-07-20 15:20:30 -07001159 fr_encaps(fp) = els_dtype;
Joe Eykholt97c83892009-03-17 11:42:40 -07001160
Joe Eykholtf018b732010-03-12 16:08:55 -08001161 stats = per_cpu_ptr(lport->dev_stats, get_cpu());
Joe Eykholt97c83892009-03-17 11:42:40 -07001162 stats->RxFrames++;
1163 stats->RxWords += skb->len / FIP_BPW;
Joe Eykholtf018b732010-03-12 16:08:55 -08001164 put_cpu();
Joe Eykholt97c83892009-03-17 11:42:40 -07001165
Robert Love70b51aa2009-11-03 11:47:45 -08001166 fc_exch_recv(lport, fp);
Joe Eykholt97c83892009-03-17 11:42:40 -07001167 return;
1168
1169len_err:
Joe Eykholt0f51c2e2009-11-03 11:48:16 -08001170 LIBFCOE_FIP_DBG(fip, "FIP length error in descriptor type %x len %zu\n",
Robert Love650bd122009-06-10 15:31:05 -07001171 desc->fip_dtype, dlen);
Joe Eykholt97c83892009-03-17 11:42:40 -07001172drop:
1173 kfree_skb(skb);
1174}
1175
1176/**
Robert Love70b51aa2009-11-03 11:47:45 -08001177 * fcoe_ctlr_recv_els() - Handle an incoming link reset frame
1178 * @fip: The FCoE controller that received the frame
1179 * @fh: The received FIP header
Joe Eykholt97c83892009-03-17 11:42:40 -07001180 *
1181 * There may be multiple VN_Port descriptors.
1182 * The overall length has already been checked.
1183 */
1184static void fcoe_ctlr_recv_clr_vlink(struct fcoe_ctlr *fip,
Robert Love70b51aa2009-11-03 11:47:45 -08001185 struct fip_header *fh)
Joe Eykholt97c83892009-03-17 11:42:40 -07001186{
1187 struct fip_desc *desc;
1188 struct fip_mac_desc *mp;
1189 struct fip_wwn_desc *wp;
1190 struct fip_vn_desc *vp;
1191 size_t rlen;
1192 size_t dlen;
1193 struct fcoe_fcf *fcf = fip->sel_fcf;
Robert Love70b51aa2009-11-03 11:47:45 -08001194 struct fc_lport *lport = fip->lp;
Bhanu Prakash Gollapudi5550fda2010-06-11 16:44:31 -07001195 struct fc_lport *vn_port = NULL;
1196 u32 desc_mask;
Bhanu Prakash Gollapudic051ad22011-05-16 16:45:24 -07001197 int num_vlink_desc;
1198 int reset_phys_port = 0;
1199 struct fip_vn_desc **vlink_desc_arr = NULL;
Joe Eykholt97c83892009-03-17 11:42:40 -07001200
Joe Eykholt0f51c2e2009-11-03 11:48:16 -08001201 LIBFCOE_FIP_DBG(fip, "Clear Virtual Link received\n");
Robert Loved29510a2010-05-07 15:18:30 -07001202
Robert Love7b2787e2010-05-07 15:18:41 -07001203 if (!fcf || !lport->port_id)
Joe Eykholt97c83892009-03-17 11:42:40 -07001204 return;
1205
1206 /*
1207 * mask of required descriptors. Validating each one clears its bit.
1208 */
Bhanu Prakash Gollapudic051ad22011-05-16 16:45:24 -07001209 desc_mask = BIT(FIP_DT_MAC) | BIT(FIP_DT_NAME);
Joe Eykholt97c83892009-03-17 11:42:40 -07001210
1211 rlen = ntohs(fh->fip_dl_len) * FIP_BPW;
1212 desc = (struct fip_desc *)(fh + 1);
Bhanu Prakash Gollapudic051ad22011-05-16 16:45:24 -07001213
1214 /*
1215 * Actually need to subtract 'sizeof(*mp) - sizeof(*wp)' from 'rlen'
1216 * before determining max Vx_Port descriptor but a buggy FCF could have
1217 * omited either or both MAC Address and Name Identifier descriptors
1218 */
1219 num_vlink_desc = rlen / sizeof(*vp);
1220 if (num_vlink_desc)
1221 vlink_desc_arr = kmalloc(sizeof(vp) * num_vlink_desc,
1222 GFP_ATOMIC);
1223 if (!vlink_desc_arr)
1224 return;
1225 num_vlink_desc = 0;
1226
Joe Eykholt97c83892009-03-17 11:42:40 -07001227 while (rlen >= sizeof(*desc)) {
1228 dlen = desc->fip_dlen * FIP_BPW;
1229 if (dlen > rlen)
Bhanu Prakash Gollapudic051ad22011-05-16 16:45:24 -07001230 goto err;
Bhanu Prakash Gollapudi0a9c5d32010-06-11 16:44:25 -07001231 /* Drop CVL if there are duplicate critical descriptors */
1232 if ((desc->fip_dtype < 32) &&
Bhanu Prakash Gollapudic051ad22011-05-16 16:45:24 -07001233 (desc->fip_dtype != FIP_DT_VN_ID) &&
Bhanu Prakash Gollapudi0a9c5d32010-06-11 16:44:25 -07001234 !(desc_mask & 1U << desc->fip_dtype)) {
1235 LIBFCOE_FIP_DBG(fip, "Duplicate Critical "
1236 "Descriptors in FIP CVL\n");
Bhanu Prakash Gollapudic051ad22011-05-16 16:45:24 -07001237 goto err;
Bhanu Prakash Gollapudi0a9c5d32010-06-11 16:44:25 -07001238 }
Joe Eykholt97c83892009-03-17 11:42:40 -07001239 switch (desc->fip_dtype) {
1240 case FIP_DT_MAC:
1241 mp = (struct fip_mac_desc *)desc;
1242 if (dlen < sizeof(*mp))
Bhanu Prakash Gollapudic051ad22011-05-16 16:45:24 -07001243 goto err;
Joe Eykholt97c83892009-03-17 11:42:40 -07001244 if (compare_ether_addr(mp->fd_mac, fcf->fcf_mac))
Bhanu Prakash Gollapudic051ad22011-05-16 16:45:24 -07001245 goto err;
Joe Eykholt97c83892009-03-17 11:42:40 -07001246 desc_mask &= ~BIT(FIP_DT_MAC);
1247 break;
1248 case FIP_DT_NAME:
1249 wp = (struct fip_wwn_desc *)desc;
1250 if (dlen < sizeof(*wp))
Bhanu Prakash Gollapudic051ad22011-05-16 16:45:24 -07001251 goto err;
Joe Eykholt97c83892009-03-17 11:42:40 -07001252 if (get_unaligned_be64(&wp->fd_wwn) != fcf->switch_name)
Bhanu Prakash Gollapudic051ad22011-05-16 16:45:24 -07001253 goto err;
Joe Eykholt97c83892009-03-17 11:42:40 -07001254 desc_mask &= ~BIT(FIP_DT_NAME);
1255 break;
1256 case FIP_DT_VN_ID:
1257 vp = (struct fip_vn_desc *)desc;
1258 if (dlen < sizeof(*vp))
Bhanu Prakash Gollapudic051ad22011-05-16 16:45:24 -07001259 goto err;
1260 vlink_desc_arr[num_vlink_desc++] = vp;
1261 vn_port = fc_vport_id_lookup(lport,
1262 ntoh24(vp->fd_fc_id));
1263 if (vn_port && (vn_port == lport)) {
1264 mutex_lock(&fip->ctlr_mutex);
1265 per_cpu_ptr(lport->dev_stats,
1266 get_cpu())->VLinkFailureCount++;
1267 put_cpu();
1268 fcoe_ctlr_reset(fip);
1269 mutex_unlock(&fip->ctlr_mutex);
Bhanu Prakash Gollapudi5550fda2010-06-11 16:44:31 -07001270 }
Joe Eykholt97c83892009-03-17 11:42:40 -07001271 break;
1272 default:
1273 /* standard says ignore unknown descriptors >= 128 */
1274 if (desc->fip_dtype < FIP_DT_VENDOR_BASE)
Bhanu Prakash Gollapudic051ad22011-05-16 16:45:24 -07001275 goto err;
Joe Eykholt97c83892009-03-17 11:42:40 -07001276 break;
1277 }
1278 desc = (struct fip_desc *)((char *)desc + dlen);
1279 rlen -= dlen;
1280 }
1281
1282 /*
1283 * reset only if all required descriptors were present and valid.
1284 */
Bhanu Prakash Gollapudic051ad22011-05-16 16:45:24 -07001285 if (desc_mask)
Joe Eykholt0f51c2e2009-11-03 11:48:16 -08001286 LIBFCOE_FIP_DBG(fip, "missing descriptors mask %x\n",
1287 desc_mask);
Bhanu Prakash Gollapudic051ad22011-05-16 16:45:24 -07001288 else if (!num_vlink_desc) {
1289 LIBFCOE_FIP_DBG(fip, "CVL: no Vx_Port descriptor found\n");
1290 /*
1291 * No Vx_Port description. Clear all NPIV ports,
1292 * followed by physical port
1293 */
Bhanu Prakash Gollapudic051ad22011-05-16 16:45:24 -07001294 mutex_lock(&fip->ctlr_mutex);
1295 per_cpu_ptr(lport->dev_stats,
1296 get_cpu())->VLinkFailureCount++;
1297 put_cpu();
1298 fcoe_ctlr_reset(fip);
1299 mutex_unlock(&fip->ctlr_mutex);
1300
Bhanu Prakash Gollapudi14619ea2012-03-09 14:49:53 -08001301 mutex_lock(&lport->lp_mutex);
1302 list_for_each_entry(vn_port, &lport->vports, list)
1303 fc_lport_reset(vn_port);
1304 mutex_unlock(&lport->lp_mutex);
1305
Bhanu Prakash Gollapudic051ad22011-05-16 16:45:24 -07001306 fc_lport_reset(fip->lp);
1307 fcoe_ctlr_solicit(fip, NULL);
1308 } else {
1309 int i;
1310
1311 LIBFCOE_FIP_DBG(fip, "performing Clear Virtual Link\n");
1312 for (i = 0; i < num_vlink_desc; i++) {
1313 vp = vlink_desc_arr[i];
1314 vn_port = fc_vport_id_lookup(lport,
1315 ntoh24(vp->fd_fc_id));
1316 if (!vn_port)
1317 continue;
1318
1319 /*
1320 * 'port_id' is already validated, check MAC address and
1321 * wwpn
1322 */
1323 if (compare_ether_addr(fip->get_src_addr(vn_port),
1324 vp->fd_mac) != 0 ||
1325 get_unaligned_be64(&vp->fd_wwpn) !=
1326 vn_port->wwpn)
1327 continue;
1328
1329 if (vn_port == lport)
1330 /*
1331 * Physical port, defer processing till all
1332 * listed NPIV ports are cleared
1333 */
1334 reset_phys_port = 1;
1335 else /* NPIV port */
1336 fc_lport_reset(vn_port);
1337 }
1338
1339 if (reset_phys_port) {
Bhanu Prakash Gollapudi5550fda2010-06-11 16:44:31 -07001340 fc_lport_reset(fip->lp);
1341 fcoe_ctlr_solicit(fip, NULL);
1342 }
Joe Eykholt97c83892009-03-17 11:42:40 -07001343 }
Bhanu Prakash Gollapudic051ad22011-05-16 16:45:24 -07001344
1345err:
1346 kfree(vlink_desc_arr);
Joe Eykholt97c83892009-03-17 11:42:40 -07001347}
1348
1349/**
Robert Love70b51aa2009-11-03 11:47:45 -08001350 * fcoe_ctlr_recv() - Receive a FIP packet
1351 * @fip: The FCoE controller that received the packet
1352 * @skb: The received FIP packet
Joe Eykholt97c83892009-03-17 11:42:40 -07001353 *
Joe Eykholt1f4aed82009-11-03 11:48:22 -08001354 * This may be called from either NET_RX_SOFTIRQ or IRQ.
Joe Eykholt97c83892009-03-17 11:42:40 -07001355 */
1356void fcoe_ctlr_recv(struct fcoe_ctlr *fip, struct sk_buff *skb)
1357{
Joe Eykholt1f4aed82009-11-03 11:48:22 -08001358 skb_queue_tail(&fip->fip_recv_list, skb);
Joe Eykholt97c83892009-03-17 11:42:40 -07001359 schedule_work(&fip->recv_work);
1360}
1361EXPORT_SYMBOL(fcoe_ctlr_recv);
1362
1363/**
Robert Love70b51aa2009-11-03 11:47:45 -08001364 * fcoe_ctlr_recv_handler() - Receive a FIP frame
1365 * @fip: The FCoE controller that received the frame
1366 * @skb: The received FIP frame
Joe Eykholt97c83892009-03-17 11:42:40 -07001367 *
1368 * Returns non-zero if the frame is dropped.
1369 */
1370static int fcoe_ctlr_recv_handler(struct fcoe_ctlr *fip, struct sk_buff *skb)
1371{
1372 struct fip_header *fiph;
1373 struct ethhdr *eh;
1374 enum fip_state state;
1375 u16 op;
1376 u8 sub;
1377
1378 if (skb_linearize(skb))
1379 goto drop;
1380 if (skb->len < sizeof(*fiph))
1381 goto drop;
1382 eh = eth_hdr(skb);
Joe Eykholte10f8c62010-07-20 15:20:30 -07001383 if (fip->mode == FIP_MODE_VN2VN) {
1384 if (compare_ether_addr(eh->h_dest, fip->ctl_src_addr) &&
1385 compare_ether_addr(eh->h_dest, fcoe_all_vn2vn) &&
1386 compare_ether_addr(eh->h_dest, fcoe_all_p2p))
1387 goto drop;
1388 } else if (compare_ether_addr(eh->h_dest, fip->ctl_src_addr) &&
1389 compare_ether_addr(eh->h_dest, fcoe_all_enode))
Joe Eykholt97c83892009-03-17 11:42:40 -07001390 goto drop;
1391 fiph = (struct fip_header *)skb->data;
1392 op = ntohs(fiph->fip_op);
1393 sub = fiph->fip_subcode;
1394
Joe Eykholt97c83892009-03-17 11:42:40 -07001395 if (FIP_VER_DECAPS(fiph->fip_ver) != FIP_VER)
1396 goto drop;
1397 if (ntohs(fiph->fip_dl_len) * FIP_BPW + sizeof(*fiph) > skb->len)
1398 goto drop;
1399
Joe Eykholtfdb068c2010-07-20 15:19:47 -07001400 mutex_lock(&fip->ctlr_mutex);
Joe Eykholt97c83892009-03-17 11:42:40 -07001401 state = fip->state;
1402 if (state == FIP_ST_AUTO) {
1403 fip->map_dest = 0;
Joe Eykholt9b651da2010-07-20 15:20:24 -07001404 fcoe_ctlr_set_state(fip, FIP_ST_ENABLED);
Joe Eykholt97c83892009-03-17 11:42:40 -07001405 state = FIP_ST_ENABLED;
Joe Eykholt0f51c2e2009-11-03 11:48:16 -08001406 LIBFCOE_FIP_DBG(fip, "Using FIP mode\n");
Joe Eykholt97c83892009-03-17 11:42:40 -07001407 }
Joe Eykholtfdb068c2010-07-20 15:19:47 -07001408 mutex_unlock(&fip->ctlr_mutex);
Joe Eykholte10f8c62010-07-20 15:20:30 -07001409
1410 if (fip->mode == FIP_MODE_VN2VN && op == FIP_OP_VN2VN)
1411 return fcoe_ctlr_vn_recv(fip, skb);
1412
1413 if (state != FIP_ST_ENABLED && state != FIP_ST_VNMP_UP &&
1414 state != FIP_ST_VNMP_CLAIM)
Joe Eykholt97c83892009-03-17 11:42:40 -07001415 goto drop;
1416
1417 if (op == FIP_OP_LS) {
1418 fcoe_ctlr_recv_els(fip, skb); /* consumes skb */
1419 return 0;
1420 }
Joe Eykholte10f8c62010-07-20 15:20:30 -07001421
1422 if (state != FIP_ST_ENABLED)
1423 goto drop;
1424
Joe Eykholt97c83892009-03-17 11:42:40 -07001425 if (op == FIP_OP_DISC && sub == FIP_SC_ADV)
1426 fcoe_ctlr_recv_adv(fip, skb);
1427 else if (op == FIP_OP_CTRL && sub == FIP_SC_CLR_VLINK)
1428 fcoe_ctlr_recv_clr_vlink(fip, fiph);
1429 kfree_skb(skb);
1430 return 0;
1431drop:
1432 kfree_skb(skb);
1433 return -1;
1434}
1435
1436/**
Robert Love70b51aa2009-11-03 11:47:45 -08001437 * fcoe_ctlr_select() - Select the best FCF (if possible)
1438 * @fip: The FCoE controller
Joe Eykholt97c83892009-03-17 11:42:40 -07001439 *
Joe Eykholtba9cd5d2010-11-30 16:20:12 -08001440 * Returns the selected FCF, or NULL if none are usable.
1441 *
Joe Eykholt97c83892009-03-17 11:42:40 -07001442 * If there are conflicting advertisements, no FCF can be chosen.
1443 *
Joe Eykholt794d98e2010-11-30 16:19:56 -08001444 * If there is already a selected FCF, this will choose a better one or
1445 * an equivalent one that hasn't already been sent a FLOGI.
1446 *
Joe Eykholt97c83892009-03-17 11:42:40 -07001447 * Called with lock held.
1448 */
Joe Eykholtba9cd5d2010-11-30 16:20:12 -08001449static struct fcoe_fcf *fcoe_ctlr_select(struct fcoe_ctlr *fip)
Joe Eykholt97c83892009-03-17 11:42:40 -07001450{
1451 struct fcoe_fcf *fcf;
Joe Eykholt794d98e2010-11-30 16:19:56 -08001452 struct fcoe_fcf *best = fip->sel_fcf;
Joe Eykholtb69ae0a2010-11-30 16:19:51 -08001453 struct fcoe_fcf *first;
1454
1455 first = list_first_entry(&fip->fcfs, struct fcoe_fcf, list);
Joe Eykholt97c83892009-03-17 11:42:40 -07001456
1457 list_for_each_entry(fcf, &fip->fcfs, list) {
Joe Eykholt9069f5c2010-11-30 16:20:02 -08001458 LIBFCOE_FIP_DBG(fip, "consider FCF fab %16.16llx "
1459 "VFID %d mac %pM map %x val %d "
1460 "sent %u pri %u\n",
1461 fcf->fabric_name, fcf->vfid, fcf->fcf_mac,
1462 fcf->fc_map, fcoe_ctlr_mtu_valid(fcf),
1463 fcf->flogi_sent, fcf->pri);
Joe Eykholtb69ae0a2010-11-30 16:19:51 -08001464 if (fcf->fabric_name != first->fabric_name ||
1465 fcf->vfid != first->vfid ||
1466 fcf->fc_map != first->fc_map) {
1467 LIBFCOE_FIP_DBG(fip, "Conflicting fabric, VFID, "
1468 "or FC-MAP\n");
1469 return NULL;
1470 }
Joe Eykholt794d98e2010-11-30 16:19:56 -08001471 if (fcf->flogi_sent)
1472 continue;
Joe Eykholt97c83892009-03-17 11:42:40 -07001473 if (!fcoe_ctlr_fcf_usable(fcf)) {
Chris Leech9f8f3aa2010-04-09 14:23:16 -07001474 LIBFCOE_FIP_DBG(fip, "FCF for fab %16.16llx "
1475 "map %x %svalid %savailable\n",
1476 fcf->fabric_name, fcf->fc_map,
1477 (fcf->flags & FIP_FL_SOL) ? "" : "in",
1478 (fcf->flags & FIP_FL_AVAIL) ?
1479 "" : "un");
Joe Eykholt97c83892009-03-17 11:42:40 -07001480 continue;
1481 }
Joe Eykholt794d98e2010-11-30 16:19:56 -08001482 if (!best || fcf->pri < best->pri || best->flogi_sent)
Joe Eykholt97c83892009-03-17 11:42:40 -07001483 best = fcf;
1484 }
1485 fip->sel_fcf = best;
Joe Eykholtc47036a2010-11-30 16:19:46 -08001486 if (best) {
Joe Eykholt9069f5c2010-11-30 16:20:02 -08001487 LIBFCOE_FIP_DBG(fip, "using FCF mac %pM\n", best->fcf_mac);
Joe Eykholtc47036a2010-11-30 16:19:46 -08001488 fip->port_ka_time = jiffies +
1489 msecs_to_jiffies(FIP_VN_KA_PERIOD);
1490 fip->ctlr_ka_time = jiffies + best->fka_period;
1491 if (time_before(fip->ctlr_ka_time, fip->timer.expires))
1492 mod_timer(&fip->timer, fip->ctlr_ka_time);
1493 }
Joe Eykholtba9cd5d2010-11-30 16:20:12 -08001494 return best;
Joe Eykholt97c83892009-03-17 11:42:40 -07001495}
1496
1497/**
Joe Eykholt794d98e2010-11-30 16:19:56 -08001498 * fcoe_ctlr_flogi_send_locked() - send FIP-encapsulated FLOGI to current FCF
1499 * @fip: The FCoE controller
1500 *
1501 * Returns non-zero error if it could not be sent.
1502 *
1503 * Called with ctlr_mutex and ctlr_lock held.
1504 * Caller must verify that fip->sel_fcf is not NULL.
1505 */
1506static int fcoe_ctlr_flogi_send_locked(struct fcoe_ctlr *fip)
1507{
1508 struct sk_buff *skb;
1509 struct sk_buff *skb_orig;
1510 struct fc_frame_header *fh;
1511 int error;
1512
1513 skb_orig = fip->flogi_req;
1514 if (!skb_orig)
1515 return -EINVAL;
1516
1517 /*
1518 * Clone and send the FLOGI request. If clone fails, use original.
1519 */
1520 skb = skb_clone(skb_orig, GFP_ATOMIC);
1521 if (!skb) {
1522 skb = skb_orig;
1523 fip->flogi_req = NULL;
1524 }
1525 fh = (struct fc_frame_header *)skb->data;
1526 error = fcoe_ctlr_encaps(fip, fip->lp, FIP_DT_FLOGI, skb,
1527 ntoh24(fh->fh_d_id));
1528 if (error) {
1529 kfree_skb(skb);
1530 return error;
1531 }
1532 fip->send(fip, skb);
1533 fip->sel_fcf->flogi_sent = 1;
1534 return 0;
1535}
1536
1537/**
1538 * fcoe_ctlr_flogi_retry() - resend FLOGI request to a new FCF if possible
1539 * @fip: The FCoE controller
1540 *
1541 * Returns non-zero error code if there's no FLOGI request to retry or
1542 * no alternate FCF available.
1543 */
1544static int fcoe_ctlr_flogi_retry(struct fcoe_ctlr *fip)
1545{
1546 struct fcoe_fcf *fcf;
1547 int error;
1548
1549 mutex_lock(&fip->ctlr_mutex);
1550 spin_lock_bh(&fip->ctlr_lock);
1551 LIBFCOE_FIP_DBG(fip, "re-sending FLOGI - reselect\n");
Joe Eykholtba9cd5d2010-11-30 16:20:12 -08001552 fcf = fcoe_ctlr_select(fip);
Joe Eykholt794d98e2010-11-30 16:19:56 -08001553 if (!fcf || fcf->flogi_sent) {
1554 kfree_skb(fip->flogi_req);
1555 fip->flogi_req = NULL;
1556 error = -ENOENT;
1557 } else {
1558 fcoe_ctlr_solicit(fip, NULL);
1559 error = fcoe_ctlr_flogi_send_locked(fip);
1560 }
1561 spin_unlock_bh(&fip->ctlr_lock);
1562 mutex_unlock(&fip->ctlr_mutex);
1563 return error;
1564}
1565
1566
1567/**
1568 * fcoe_ctlr_flogi_send() - Handle sending of FIP FLOGI.
1569 * @fip: The FCoE controller that timed out
1570 *
1571 * Done here because fcoe_ctlr_els_send() can't get mutex.
1572 *
1573 * Called with ctlr_mutex held. The caller must not hold ctlr_lock.
1574 */
1575static void fcoe_ctlr_flogi_send(struct fcoe_ctlr *fip)
1576{
1577 struct fcoe_fcf *fcf;
1578
1579 spin_lock_bh(&fip->ctlr_lock);
1580 fcf = fip->sel_fcf;
1581 if (!fcf || !fip->flogi_req_send)
1582 goto unlock;
1583
1584 LIBFCOE_FIP_DBG(fip, "sending FLOGI\n");
1585
1586 /*
1587 * If this FLOGI is being sent due to a timeout retry
1588 * to the same FCF as before, select a different FCF if possible.
1589 */
1590 if (fcf->flogi_sent) {
1591 LIBFCOE_FIP_DBG(fip, "sending FLOGI - reselect\n");
Joe Eykholtba9cd5d2010-11-30 16:20:12 -08001592 fcf = fcoe_ctlr_select(fip);
Joe Eykholt794d98e2010-11-30 16:19:56 -08001593 if (!fcf || fcf->flogi_sent) {
1594 LIBFCOE_FIP_DBG(fip, "sending FLOGI - clearing\n");
1595 list_for_each_entry(fcf, &fip->fcfs, list)
1596 fcf->flogi_sent = 0;
Joe Eykholtba9cd5d2010-11-30 16:20:12 -08001597 fcf = fcoe_ctlr_select(fip);
Joe Eykholt794d98e2010-11-30 16:19:56 -08001598 }
1599 }
1600 if (fcf) {
1601 fcoe_ctlr_flogi_send_locked(fip);
1602 fip->flogi_req_send = 0;
1603 } else /* XXX */
1604 LIBFCOE_FIP_DBG(fip, "No FCF selected - defer send\n");
1605unlock:
1606 spin_unlock_bh(&fip->ctlr_lock);
1607}
1608
1609/**
Robert Love70b51aa2009-11-03 11:47:45 -08001610 * fcoe_ctlr_timeout() - FIP timeout handler
1611 * @arg: The FCoE controller that timed out
Joe Eykholt97c83892009-03-17 11:42:40 -07001612 */
1613static void fcoe_ctlr_timeout(unsigned long arg)
1614{
1615 struct fcoe_ctlr *fip = (struct fcoe_ctlr *)arg;
Joe Eykholtfdb068c2010-07-20 15:19:47 -07001616
1617 schedule_work(&fip->timer_work);
1618}
1619
1620/**
1621 * fcoe_ctlr_timer_work() - Worker thread function for timer work
1622 * @work: Handle to a FCoE controller
1623 *
1624 * Ages FCFs. Triggers FCF selection if possible.
1625 * Sends keep-alives and resets.
1626 */
1627static void fcoe_ctlr_timer_work(struct work_struct *work)
1628{
1629 struct fcoe_ctlr *fip;
1630 struct fc_lport *vport;
1631 u8 *mac;
1632 u8 reset = 0;
1633 u8 send_ctlr_ka = 0;
1634 u8 send_port_ka = 0;
Joe Eykholt97c83892009-03-17 11:42:40 -07001635 struct fcoe_fcf *sel;
1636 struct fcoe_fcf *fcf;
Joe Eykholt8690cb82010-06-11 16:44:10 -07001637 unsigned long next_timer;
Joe Eykholt97c83892009-03-17 11:42:40 -07001638
Joe Eykholtfdb068c2010-07-20 15:19:47 -07001639 fip = container_of(work, struct fcoe_ctlr, timer_work);
Joe Eykholte10f8c62010-07-20 15:20:30 -07001640 if (fip->mode == FIP_MODE_VN2VN)
1641 return fcoe_ctlr_vn_timeout(fip);
Joe Eykholtfdb068c2010-07-20 15:19:47 -07001642 mutex_lock(&fip->ctlr_mutex);
Joe Eykholt97c83892009-03-17 11:42:40 -07001643 if (fip->state == FIP_ST_DISABLED) {
Joe Eykholtfdb068c2010-07-20 15:19:47 -07001644 mutex_unlock(&fip->ctlr_mutex);
Joe Eykholt97c83892009-03-17 11:42:40 -07001645 return;
1646 }
1647
1648 fcf = fip->sel_fcf;
Joe Eykholt8690cb82010-06-11 16:44:10 -07001649 next_timer = fcoe_ctlr_age_fcfs(fip);
Joe Eykholt97c83892009-03-17 11:42:40 -07001650
1651 sel = fip->sel_fcf;
Joe Eykholt8690cb82010-06-11 16:44:10 -07001652 if (!sel && fip->sel_time) {
1653 if (time_after_eq(jiffies, fip->sel_time)) {
Joe Eykholtba9cd5d2010-11-30 16:20:12 -08001654 sel = fcoe_ctlr_select(fip);
Joe Eykholt8690cb82010-06-11 16:44:10 -07001655 fip->sel_time = 0;
1656 } else if (time_after(next_timer, fip->sel_time))
1657 next_timer = fip->sel_time;
Joe Eykholt97c83892009-03-17 11:42:40 -07001658 }
1659
Joe Eykholt794d98e2010-11-30 16:19:56 -08001660 if (sel && fip->flogi_req_send)
1661 fcoe_ctlr_flogi_send(fip);
1662 else if (!sel && fcf)
1663 reset = 1;
Joe Eykholt97c83892009-03-17 11:42:40 -07001664
Yi Zou8cdffdc2009-11-20 14:54:57 -08001665 if (sel && !sel->fd_flags) {
Joe Eykholt97c83892009-03-17 11:42:40 -07001666 if (time_after_eq(jiffies, fip->ctlr_ka_time)) {
1667 fip->ctlr_ka_time = jiffies + sel->fka_period;
Joe Eykholtfdb068c2010-07-20 15:19:47 -07001668 send_ctlr_ka = 1;
Joe Eykholt97c83892009-03-17 11:42:40 -07001669 }
1670 if (time_after(next_timer, fip->ctlr_ka_time))
1671 next_timer = fip->ctlr_ka_time;
1672
1673 if (time_after_eq(jiffies, fip->port_ka_time)) {
Bhanu Prakash Gollapudif47dd852010-01-21 10:16:00 -08001674 fip->port_ka_time = jiffies +
Robert Love70b51aa2009-11-03 11:47:45 -08001675 msecs_to_jiffies(FIP_VN_KA_PERIOD);
Joe Eykholtfdb068c2010-07-20 15:19:47 -07001676 send_port_ka = 1;
Joe Eykholt97c83892009-03-17 11:42:40 -07001677 }
1678 if (time_after(next_timer, fip->port_ka_time))
1679 next_timer = fip->port_ka_time;
Joe Eykholt97c83892009-03-17 11:42:40 -07001680 }
Joe Eykholt8690cb82010-06-11 16:44:10 -07001681 if (!list_empty(&fip->fcfs))
1682 mod_timer(&fip->timer, next_timer);
Joe Eykholtfdb068c2010-07-20 15:19:47 -07001683 mutex_unlock(&fip->ctlr_mutex);
Joe Eykholt97c83892009-03-17 11:42:40 -07001684
Bhanu Prakash Gollapudi516a6482010-06-11 16:43:44 -07001685 if (reset) {
Joe Eykholtdd42dac2009-11-03 11:48:27 -08001686 fc_lport_reset(fip->lp);
Bhanu Prakash Gollapudi516a6482010-06-11 16:43:44 -07001687 /* restart things with a solicitation */
1688 fcoe_ctlr_solicit(fip, NULL);
1689 }
Chris Leech11b56182009-11-03 11:46:29 -08001690
Joe Eykholtfdb068c2010-07-20 15:19:47 -07001691 if (send_ctlr_ka)
Chris Leech11b56182009-11-03 11:46:29 -08001692 fcoe_ctlr_send_keep_alive(fip, NULL, 0, fip->ctl_src_addr);
Joe Eykholtfdb068c2010-07-20 15:19:47 -07001693
1694 if (send_port_ka) {
Chris Leech11b56182009-11-03 11:46:29 -08001695 mutex_lock(&fip->lp->lp_mutex);
1696 mac = fip->get_src_addr(fip->lp);
1697 fcoe_ctlr_send_keep_alive(fip, fip->lp, 1, mac);
1698 list_for_each_entry(vport, &fip->lp->vports, list) {
1699 mac = fip->get_src_addr(vport);
1700 fcoe_ctlr_send_keep_alive(fip, vport, 1, mac);
1701 }
1702 mutex_unlock(&fip->lp->lp_mutex);
1703 }
Joe Eykholt97c83892009-03-17 11:42:40 -07001704}
1705
1706/**
Robert Love70b51aa2009-11-03 11:47:45 -08001707 * fcoe_ctlr_recv_work() - Worker thread function for receiving FIP frames
1708 * @recv_work: Handle to a FCoE controller
Joe Eykholt97c83892009-03-17 11:42:40 -07001709 */
1710static void fcoe_ctlr_recv_work(struct work_struct *recv_work)
1711{
1712 struct fcoe_ctlr *fip;
1713 struct sk_buff *skb;
1714
1715 fip = container_of(recv_work, struct fcoe_ctlr, recv_work);
Joe Eykholt1f4aed82009-11-03 11:48:22 -08001716 while ((skb = skb_dequeue(&fip->fip_recv_list)))
Joe Eykholt97c83892009-03-17 11:42:40 -07001717 fcoe_ctlr_recv_handler(fip, skb);
Joe Eykholt97c83892009-03-17 11:42:40 -07001718}
1719
1720/**
Joe Eykholt386309ce2009-11-03 11:49:16 -08001721 * fcoe_ctlr_recv_flogi() - Snoop pre-FIP receipt of FLOGI response
Robert Love70b51aa2009-11-03 11:47:45 -08001722 * @fip: The FCoE controller
1723 * @fp: The FC frame to snoop
Joe Eykholt97c83892009-03-17 11:42:40 -07001724 *
1725 * Snoop potential response to FLOGI or even incoming FLOGI.
1726 *
1727 * The caller has checked that we are waiting for login as indicated
1728 * by fip->flogi_oxid != FC_XID_UNKNOWN.
1729 *
1730 * The caller is responsible for freeing the frame.
Joe Eykholt386309ce2009-11-03 11:49:16 -08001731 * Fill in the granted_mac address.
Joe Eykholt97c83892009-03-17 11:42:40 -07001732 *
1733 * Return non-zero if the frame should not be delivered to libfc.
1734 */
Chris Leech11b56182009-11-03 11:46:29 -08001735int fcoe_ctlr_recv_flogi(struct fcoe_ctlr *fip, struct fc_lport *lport,
Joe Eykholt386309ce2009-11-03 11:49:16 -08001736 struct fc_frame *fp)
Joe Eykholt97c83892009-03-17 11:42:40 -07001737{
1738 struct fc_frame_header *fh;
1739 u8 op;
Joe Eykholt386309ce2009-11-03 11:49:16 -08001740 u8 *sa;
Joe Eykholt97c83892009-03-17 11:42:40 -07001741
Joe Eykholt386309ce2009-11-03 11:49:16 -08001742 sa = eth_hdr(&fp->skb)->h_source;
Joe Eykholt97c83892009-03-17 11:42:40 -07001743 fh = fc_frame_header_get(fp);
1744 if (fh->fh_type != FC_TYPE_ELS)
1745 return 0;
1746
1747 op = fc_frame_payload_op(fp);
1748 if (op == ELS_LS_ACC && fh->fh_r_ctl == FC_RCTL_ELS_REP &&
1749 fip->flogi_oxid == ntohs(fh->fh_ox_id)) {
1750
Joe Eykholtfdb068c2010-07-20 15:19:47 -07001751 mutex_lock(&fip->ctlr_mutex);
Joe Eykholt97c83892009-03-17 11:42:40 -07001752 if (fip->state != FIP_ST_AUTO && fip->state != FIP_ST_NON_FIP) {
Joe Eykholtfdb068c2010-07-20 15:19:47 -07001753 mutex_unlock(&fip->ctlr_mutex);
Joe Eykholt97c83892009-03-17 11:42:40 -07001754 return -EINVAL;
1755 }
Joe Eykholt9b651da2010-07-20 15:20:24 -07001756 fcoe_ctlr_set_state(fip, FIP_ST_NON_FIP);
Joe Eykholt0f51c2e2009-11-03 11:48:16 -08001757 LIBFCOE_FIP_DBG(fip,
1758 "received FLOGI LS_ACC using non-FIP mode\n");
Joe Eykholt97c83892009-03-17 11:42:40 -07001759
1760 /*
1761 * FLOGI accepted.
1762 * If the src mac addr is FC_OUI-based, then we mark the
1763 * address_mode flag to use FC_OUI-based Ethernet DA.
1764 * Otherwise we use the FCoE gateway addr
1765 */
1766 if (!compare_ether_addr(sa, (u8[6])FC_FCOE_FLOGI_MAC)) {
Joe Eykholtcd229e42010-07-20 15:20:40 -07001767 fcoe_ctlr_map_dest(fip);
Joe Eykholt97c83892009-03-17 11:42:40 -07001768 } else {
1769 memcpy(fip->dest_addr, sa, ETH_ALEN);
1770 fip->map_dest = 0;
1771 }
1772 fip->flogi_oxid = FC_XID_UNKNOWN;
Joe Eykholtfdb068c2010-07-20 15:19:47 -07001773 mutex_unlock(&fip->ctlr_mutex);
Joe Eykholt386309ce2009-11-03 11:49:16 -08001774 fc_fcoe_set_mac(fr_cb(fp)->granted_mac, fh->fh_d_id);
Joe Eykholt97c83892009-03-17 11:42:40 -07001775 } else if (op == ELS_FLOGI && fh->fh_r_ctl == FC_RCTL_ELS_REQ && sa) {
1776 /*
1777 * Save source MAC for point-to-point responses.
1778 */
Joe Eykholtfdb068c2010-07-20 15:19:47 -07001779 mutex_lock(&fip->ctlr_mutex);
Joe Eykholt97c83892009-03-17 11:42:40 -07001780 if (fip->state == FIP_ST_AUTO || fip->state == FIP_ST_NON_FIP) {
1781 memcpy(fip->dest_addr, sa, ETH_ALEN);
1782 fip->map_dest = 0;
Joe Eykholte49bf612010-03-12 16:07:57 -08001783 if (fip->state == FIP_ST_AUTO)
1784 LIBFCOE_FIP_DBG(fip, "received non-FIP FLOGI. "
1785 "Setting non-FIP mode\n");
Joe Eykholt9b651da2010-07-20 15:20:24 -07001786 fcoe_ctlr_set_state(fip, FIP_ST_NON_FIP);
Joe Eykholt97c83892009-03-17 11:42:40 -07001787 }
Joe Eykholtfdb068c2010-07-20 15:19:47 -07001788 mutex_unlock(&fip->ctlr_mutex);
Joe Eykholt97c83892009-03-17 11:42:40 -07001789 }
1790 return 0;
1791}
1792EXPORT_SYMBOL(fcoe_ctlr_recv_flogi);
1793
Vasu Dev5e80f7f2009-03-17 11:42:18 -07001794/**
Robert Love70b51aa2009-11-03 11:47:45 -08001795 * fcoe_wwn_from_mac() - Converts a 48-bit IEEE MAC address to a 64-bit FC WWN
1796 * @mac: The MAC address to convert
1797 * @scheme: The scheme to use when converting
1798 * @port: The port indicator for converting
Vasu Dev5e80f7f2009-03-17 11:42:18 -07001799 *
1800 * Returns: u64 fc world wide name
1801 */
1802u64 fcoe_wwn_from_mac(unsigned char mac[MAX_ADDR_LEN],
1803 unsigned int scheme, unsigned int port)
1804{
1805 u64 wwn;
1806 u64 host_mac;
1807
1808 /* The MAC is in NO, so flip only the low 48 bits */
1809 host_mac = ((u64) mac[0] << 40) |
1810 ((u64) mac[1] << 32) |
1811 ((u64) mac[2] << 24) |
1812 ((u64) mac[3] << 16) |
1813 ((u64) mac[4] << 8) |
1814 (u64) mac[5];
1815
1816 WARN_ON(host_mac >= (1ULL << 48));
1817 wwn = host_mac | ((u64) scheme << 60);
1818 switch (scheme) {
1819 case 1:
1820 WARN_ON(port != 0);
1821 break;
1822 case 2:
1823 WARN_ON(port >= 0xfff);
1824 wwn |= (u64) port << 48;
1825 break;
1826 default:
1827 WARN_ON(1);
1828 break;
1829 }
1830
1831 return wwn;
1832}
1833EXPORT_SYMBOL_GPL(fcoe_wwn_from_mac);
1834
1835/**
Joe Eykholte10f8c62010-07-20 15:20:30 -07001836 * fcoe_ctlr_rport() - return the fcoe_rport for a given fc_rport_priv
1837 * @rdata: libfc remote port
1838 */
1839static inline struct fcoe_rport *fcoe_ctlr_rport(struct fc_rport_priv *rdata)
1840{
1841 return (struct fcoe_rport *)(rdata + 1);
1842}
1843
1844/**
1845 * fcoe_ctlr_vn_send() - Send a FIP VN2VN Probe Request or Reply.
1846 * @fip: The FCoE controller
1847 * @sub: sub-opcode for probe request, reply, or advertisement.
1848 * @dest: The destination Ethernet MAC address
1849 * @min_len: minimum size of the Ethernet payload to be sent
1850 */
1851static void fcoe_ctlr_vn_send(struct fcoe_ctlr *fip,
1852 enum fip_vn2vn_subcode sub,
1853 const u8 *dest, size_t min_len)
1854{
1855 struct sk_buff *skb;
1856 struct fip_frame {
1857 struct ethhdr eth;
1858 struct fip_header fip;
1859 struct fip_mac_desc mac;
1860 struct fip_wwn_desc wwnn;
1861 struct fip_vn_desc vn;
Yi Zou0095a922011-01-28 16:05:00 -08001862 } __packed * frame;
Joe Eykholte10f8c62010-07-20 15:20:30 -07001863 struct fip_fc4_feat *ff;
1864 struct fip_size_desc *size;
1865 u32 fcp_feat;
1866 size_t len;
1867 size_t dlen;
1868
1869 len = sizeof(*frame);
1870 dlen = 0;
1871 if (sub == FIP_SC_VN_CLAIM_NOTIFY || sub == FIP_SC_VN_CLAIM_REP) {
1872 dlen = sizeof(struct fip_fc4_feat) +
1873 sizeof(struct fip_size_desc);
1874 len += dlen;
1875 }
1876 dlen += sizeof(frame->mac) + sizeof(frame->wwnn) + sizeof(frame->vn);
1877 len = max(len, min_len + sizeof(struct ethhdr));
1878
1879 skb = dev_alloc_skb(len);
1880 if (!skb)
1881 return;
1882
1883 frame = (struct fip_frame *)skb->data;
1884 memset(frame, 0, len);
1885 memcpy(frame->eth.h_dest, dest, ETH_ALEN);
1886 memcpy(frame->eth.h_source, fip->ctl_src_addr, ETH_ALEN);
1887 frame->eth.h_proto = htons(ETH_P_FIP);
1888
1889 frame->fip.fip_ver = FIP_VER_ENCAPS(FIP_VER);
1890 frame->fip.fip_op = htons(FIP_OP_VN2VN);
1891 frame->fip.fip_subcode = sub;
1892 frame->fip.fip_dl_len = htons(dlen / FIP_BPW);
1893
1894 frame->mac.fd_desc.fip_dtype = FIP_DT_MAC;
1895 frame->mac.fd_desc.fip_dlen = sizeof(frame->mac) / FIP_BPW;
1896 memcpy(frame->mac.fd_mac, fip->ctl_src_addr, ETH_ALEN);
1897
1898 frame->wwnn.fd_desc.fip_dtype = FIP_DT_NAME;
1899 frame->wwnn.fd_desc.fip_dlen = sizeof(frame->wwnn) / FIP_BPW;
1900 put_unaligned_be64(fip->lp->wwnn, &frame->wwnn.fd_wwn);
1901
1902 frame->vn.fd_desc.fip_dtype = FIP_DT_VN_ID;
1903 frame->vn.fd_desc.fip_dlen = sizeof(frame->vn) / FIP_BPW;
1904 hton24(frame->vn.fd_mac, FIP_VN_FC_MAP);
1905 hton24(frame->vn.fd_mac + 3, fip->port_id);
1906 hton24(frame->vn.fd_fc_id, fip->port_id);
1907 put_unaligned_be64(fip->lp->wwpn, &frame->vn.fd_wwpn);
1908
1909 /*
1910 * For claims, add FC-4 features.
1911 * TBD: Add interface to get fc-4 types and features from libfc.
1912 */
1913 if (sub == FIP_SC_VN_CLAIM_NOTIFY || sub == FIP_SC_VN_CLAIM_REP) {
1914 ff = (struct fip_fc4_feat *)(frame + 1);
1915 ff->fd_desc.fip_dtype = FIP_DT_FC4F;
1916 ff->fd_desc.fip_dlen = sizeof(*ff) / FIP_BPW;
1917 ff->fd_fts = fip->lp->fcts;
1918
1919 fcp_feat = 0;
1920 if (fip->lp->service_params & FCP_SPPF_INIT_FCN)
1921 fcp_feat |= FCP_FEAT_INIT;
1922 if (fip->lp->service_params & FCP_SPPF_TARG_FCN)
1923 fcp_feat |= FCP_FEAT_TARG;
1924 fcp_feat <<= (FC_TYPE_FCP * 4) % 32;
1925 ff->fd_ff.fd_feat[FC_TYPE_FCP * 4 / 32] = htonl(fcp_feat);
1926
1927 size = (struct fip_size_desc *)(ff + 1);
1928 size->fd_desc.fip_dtype = FIP_DT_FCOE_SIZE;
1929 size->fd_desc.fip_dlen = sizeof(*size) / FIP_BPW;
1930 size->fd_size = htons(fcoe_ctlr_fcoe_size(fip));
1931 }
1932
1933 skb_put(skb, len);
1934 skb->protocol = htons(ETH_P_FIP);
john fastabend6f6c2aa2011-11-18 13:35:56 -08001935 skb->priority = fip->priority;
Joe Eykholte10f8c62010-07-20 15:20:30 -07001936 skb_reset_mac_header(skb);
1937 skb_reset_network_header(skb);
1938
1939 fip->send(fip, skb);
1940}
1941
1942/**
1943 * fcoe_ctlr_vn_rport_callback - Event handler for rport events.
1944 * @lport: The lport which is receiving the event
1945 * @rdata: remote port private data
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001946 * @event: The event that occurred
Joe Eykholte10f8c62010-07-20 15:20:30 -07001947 *
1948 * Locking Note: The rport lock must not be held when calling this function.
1949 */
1950static void fcoe_ctlr_vn_rport_callback(struct fc_lport *lport,
1951 struct fc_rport_priv *rdata,
1952 enum fc_rport_event event)
1953{
1954 struct fcoe_ctlr *fip = lport->disc.priv;
1955 struct fcoe_rport *frport = fcoe_ctlr_rport(rdata);
1956
1957 LIBFCOE_FIP_DBG(fip, "vn_rport_callback %x event %d\n",
1958 rdata->ids.port_id, event);
1959
1960 mutex_lock(&fip->ctlr_mutex);
1961 switch (event) {
1962 case RPORT_EV_READY:
1963 frport->login_count = 0;
1964 break;
1965 case RPORT_EV_LOGO:
1966 case RPORT_EV_FAILED:
1967 case RPORT_EV_STOP:
1968 frport->login_count++;
1969 if (frport->login_count > FCOE_CTLR_VN2VN_LOGIN_LIMIT) {
1970 LIBFCOE_FIP_DBG(fip,
1971 "rport FLOGI limited port_id %6.6x\n",
1972 rdata->ids.port_id);
1973 lport->tt.rport_logoff(rdata);
1974 }
1975 break;
1976 default:
1977 break;
1978 }
1979 mutex_unlock(&fip->ctlr_mutex);
1980}
1981
1982static struct fc_rport_operations fcoe_ctlr_vn_rport_ops = {
1983 .event_callback = fcoe_ctlr_vn_rport_callback,
1984};
1985
1986/**
1987 * fcoe_ctlr_disc_stop_locked() - stop discovery in VN2VN mode
1988 * @fip: The FCoE controller
1989 *
1990 * Called with ctlr_mutex held.
1991 */
1992static void fcoe_ctlr_disc_stop_locked(struct fc_lport *lport)
1993{
1994 mutex_lock(&lport->disc.disc_mutex);
1995 lport->disc.disc_callback = NULL;
1996 mutex_unlock(&lport->disc.disc_mutex);
1997}
1998
1999/**
2000 * fcoe_ctlr_disc_stop() - stop discovery in VN2VN mode
2001 * @fip: The FCoE controller
2002 *
2003 * Called through the local port template for discovery.
2004 * Called without the ctlr_mutex held.
2005 */
2006static void fcoe_ctlr_disc_stop(struct fc_lport *lport)
2007{
2008 struct fcoe_ctlr *fip = lport->disc.priv;
2009
2010 mutex_lock(&fip->ctlr_mutex);
2011 fcoe_ctlr_disc_stop_locked(lport);
2012 mutex_unlock(&fip->ctlr_mutex);
2013}
2014
2015/**
2016 * fcoe_ctlr_disc_stop_final() - stop discovery for shutdown in VN2VN mode
2017 * @fip: The FCoE controller
2018 *
2019 * Called through the local port template for discovery.
2020 * Called without the ctlr_mutex held.
2021 */
2022static void fcoe_ctlr_disc_stop_final(struct fc_lport *lport)
2023{
2024 fcoe_ctlr_disc_stop(lport);
2025 lport->tt.rport_flush_queue();
2026 synchronize_rcu();
2027}
2028
2029/**
2030 * fcoe_ctlr_vn_restart() - VN2VN probe restart with new port_id
2031 * @fip: The FCoE controller
2032 *
2033 * Called with fcoe_ctlr lock held.
2034 */
2035static void fcoe_ctlr_vn_restart(struct fcoe_ctlr *fip)
2036{
2037 unsigned long wait;
2038 u32 port_id;
2039
2040 fcoe_ctlr_disc_stop_locked(fip->lp);
2041
2042 /*
2043 * Get proposed port ID.
2044 * If this is the first try after link up, use any previous port_id.
2045 * If there was none, use the low bits of the port_name.
2046 * On subsequent tries, get the next random one.
2047 * Don't use reserved IDs, use another non-zero value, just as random.
2048 */
2049 port_id = fip->port_id;
2050 if (fip->probe_tries)
2051 port_id = prandom32(&fip->rnd_state) & 0xffff;
2052 else if (!port_id)
2053 port_id = fip->lp->wwpn & 0xffff;
2054 if (!port_id || port_id == 0xffff)
2055 port_id = 1;
2056 fip->port_id = port_id;
2057
2058 if (fip->probe_tries < FIP_VN_RLIM_COUNT) {
2059 fip->probe_tries++;
2060 wait = random32() % FIP_VN_PROBE_WAIT;
2061 } else
2062 wait = FIP_VN_RLIM_INT;
2063 mod_timer(&fip->timer, jiffies + msecs_to_jiffies(wait));
2064 fcoe_ctlr_set_state(fip, FIP_ST_VNMP_START);
2065}
2066
2067/**
2068 * fcoe_ctlr_vn_start() - Start in VN2VN mode
2069 * @fip: The FCoE controller
2070 *
2071 * Called with fcoe_ctlr lock held.
2072 */
2073static void fcoe_ctlr_vn_start(struct fcoe_ctlr *fip)
2074{
2075 fip->probe_tries = 0;
2076 prandom32_seed(&fip->rnd_state, fip->lp->wwpn);
2077 fcoe_ctlr_vn_restart(fip);
2078}
2079
2080/**
2081 * fcoe_ctlr_vn_parse - parse probe request or response
2082 * @fip: The FCoE controller
2083 * @skb: incoming packet
2084 * @rdata: buffer for resulting parsed VN entry plus fcoe_rport
2085 *
2086 * Returns non-zero error number on error.
2087 * Does not consume the packet.
2088 */
2089static int fcoe_ctlr_vn_parse(struct fcoe_ctlr *fip,
2090 struct sk_buff *skb,
2091 struct fc_rport_priv *rdata)
2092{
2093 struct fip_header *fiph;
2094 struct fip_desc *desc = NULL;
2095 struct fip_mac_desc *macd = NULL;
2096 struct fip_wwn_desc *wwn = NULL;
2097 struct fip_vn_desc *vn = NULL;
2098 struct fip_size_desc *size = NULL;
2099 struct fcoe_rport *frport;
2100 size_t rlen;
2101 size_t dlen;
2102 u32 desc_mask = 0;
2103 u32 dtype;
2104 u8 sub;
2105
2106 memset(rdata, 0, sizeof(*rdata) + sizeof(*frport));
2107 frport = fcoe_ctlr_rport(rdata);
2108
2109 fiph = (struct fip_header *)skb->data;
2110 frport->flags = ntohs(fiph->fip_flags);
2111
2112 sub = fiph->fip_subcode;
2113 switch (sub) {
2114 case FIP_SC_VN_PROBE_REQ:
2115 case FIP_SC_VN_PROBE_REP:
2116 case FIP_SC_VN_BEACON:
2117 desc_mask = BIT(FIP_DT_MAC) | BIT(FIP_DT_NAME) |
2118 BIT(FIP_DT_VN_ID);
2119 break;
2120 case FIP_SC_VN_CLAIM_NOTIFY:
2121 case FIP_SC_VN_CLAIM_REP:
2122 desc_mask = BIT(FIP_DT_MAC) | BIT(FIP_DT_NAME) |
2123 BIT(FIP_DT_VN_ID) | BIT(FIP_DT_FC4F) |
2124 BIT(FIP_DT_FCOE_SIZE);
2125 break;
2126 default:
2127 LIBFCOE_FIP_DBG(fip, "vn_parse unknown subcode %u\n", sub);
2128 return -EINVAL;
2129 }
2130
2131 rlen = ntohs(fiph->fip_dl_len) * 4;
2132 if (rlen + sizeof(*fiph) > skb->len)
2133 return -EINVAL;
2134
2135 desc = (struct fip_desc *)(fiph + 1);
2136 while (rlen > 0) {
2137 dlen = desc->fip_dlen * FIP_BPW;
2138 if (dlen < sizeof(*desc) || dlen > rlen)
2139 return -EINVAL;
2140
2141 dtype = desc->fip_dtype;
2142 if (dtype < 32) {
2143 if (!(desc_mask & BIT(dtype))) {
2144 LIBFCOE_FIP_DBG(fip,
2145 "unexpected or duplicated desc "
2146 "desc type %u in "
2147 "FIP VN2VN subtype %u\n",
2148 dtype, sub);
2149 return -EINVAL;
2150 }
2151 desc_mask &= ~BIT(dtype);
2152 }
2153
2154 switch (dtype) {
2155 case FIP_DT_MAC:
2156 if (dlen != sizeof(struct fip_mac_desc))
2157 goto len_err;
2158 macd = (struct fip_mac_desc *)desc;
2159 if (!is_valid_ether_addr(macd->fd_mac)) {
2160 LIBFCOE_FIP_DBG(fip,
2161 "Invalid MAC addr %pM in FIP VN2VN\n",
2162 macd->fd_mac);
2163 return -EINVAL;
2164 }
2165 memcpy(frport->enode_mac, macd->fd_mac, ETH_ALEN);
2166 break;
2167 case FIP_DT_NAME:
2168 if (dlen != sizeof(struct fip_wwn_desc))
2169 goto len_err;
2170 wwn = (struct fip_wwn_desc *)desc;
2171 rdata->ids.node_name = get_unaligned_be64(&wwn->fd_wwn);
2172 break;
2173 case FIP_DT_VN_ID:
2174 if (dlen != sizeof(struct fip_vn_desc))
2175 goto len_err;
2176 vn = (struct fip_vn_desc *)desc;
2177 memcpy(frport->vn_mac, vn->fd_mac, ETH_ALEN);
2178 rdata->ids.port_id = ntoh24(vn->fd_fc_id);
2179 rdata->ids.port_name = get_unaligned_be64(&vn->fd_wwpn);
2180 break;
2181 case FIP_DT_FC4F:
2182 if (dlen != sizeof(struct fip_fc4_feat))
2183 goto len_err;
2184 break;
2185 case FIP_DT_FCOE_SIZE:
2186 if (dlen != sizeof(struct fip_size_desc))
2187 goto len_err;
2188 size = (struct fip_size_desc *)desc;
2189 frport->fcoe_len = ntohs(size->fd_size);
2190 break;
2191 default:
2192 LIBFCOE_FIP_DBG(fip, "unexpected descriptor type %x "
2193 "in FIP probe\n", dtype);
2194 /* standard says ignore unknown descriptors >= 128 */
2195 if (dtype < FIP_DT_VENDOR_BASE)
2196 return -EINVAL;
2197 break;
2198 }
2199 desc = (struct fip_desc *)((char *)desc + dlen);
2200 rlen -= dlen;
2201 }
2202 return 0;
2203
2204len_err:
2205 LIBFCOE_FIP_DBG(fip, "FIP length error in descriptor type %x len %zu\n",
2206 dtype, dlen);
2207 return -EINVAL;
2208}
2209
2210/**
2211 * fcoe_ctlr_vn_send_claim() - send multicast FIP VN2VN Claim Notification.
2212 * @fip: The FCoE controller
2213 *
2214 * Called with ctlr_mutex held.
2215 */
2216static void fcoe_ctlr_vn_send_claim(struct fcoe_ctlr *fip)
2217{
2218 fcoe_ctlr_vn_send(fip, FIP_SC_VN_CLAIM_NOTIFY, fcoe_all_vn2vn, 0);
2219 fip->sol_time = jiffies;
2220}
2221
2222/**
2223 * fcoe_ctlr_vn_probe_req() - handle incoming VN2VN probe request.
2224 * @fip: The FCoE controller
2225 * @rdata: parsed remote port with frport from the probe request
2226 *
2227 * Called with ctlr_mutex held.
2228 */
2229static void fcoe_ctlr_vn_probe_req(struct fcoe_ctlr *fip,
2230 struct fc_rport_priv *rdata)
2231{
2232 struct fcoe_rport *frport = fcoe_ctlr_rport(rdata);
2233
2234 if (rdata->ids.port_id != fip->port_id)
2235 return;
2236
2237 switch (fip->state) {
2238 case FIP_ST_VNMP_CLAIM:
2239 case FIP_ST_VNMP_UP:
2240 fcoe_ctlr_vn_send(fip, FIP_SC_VN_PROBE_REP,
2241 frport->enode_mac, 0);
2242 break;
2243 case FIP_ST_VNMP_PROBE1:
2244 case FIP_ST_VNMP_PROBE2:
2245 /*
2246 * Decide whether to reply to the Probe.
2247 * Our selected address is never a "recorded" one, so
2248 * only reply if our WWPN is greater and the
2249 * Probe's REC bit is not set.
2250 * If we don't reply, we will change our address.
2251 */
2252 if (fip->lp->wwpn > rdata->ids.port_name &&
2253 !(frport->flags & FIP_FL_REC_OR_P2P)) {
2254 fcoe_ctlr_vn_send(fip, FIP_SC_VN_PROBE_REP,
2255 frport->enode_mac, 0);
2256 break;
2257 }
2258 /* fall through */
2259 case FIP_ST_VNMP_START:
2260 fcoe_ctlr_vn_restart(fip);
2261 break;
2262 default:
2263 break;
2264 }
2265}
2266
2267/**
2268 * fcoe_ctlr_vn_probe_reply() - handle incoming VN2VN probe reply.
2269 * @fip: The FCoE controller
2270 * @rdata: parsed remote port with frport from the probe request
2271 *
2272 * Called with ctlr_mutex held.
2273 */
2274static void fcoe_ctlr_vn_probe_reply(struct fcoe_ctlr *fip,
2275 struct fc_rport_priv *rdata)
2276{
2277 if (rdata->ids.port_id != fip->port_id)
2278 return;
2279 switch (fip->state) {
2280 case FIP_ST_VNMP_START:
2281 case FIP_ST_VNMP_PROBE1:
2282 case FIP_ST_VNMP_PROBE2:
2283 case FIP_ST_VNMP_CLAIM:
2284 fcoe_ctlr_vn_restart(fip);
2285 break;
2286 case FIP_ST_VNMP_UP:
2287 fcoe_ctlr_vn_send_claim(fip);
2288 break;
2289 default:
2290 break;
2291 }
2292}
2293
2294/**
2295 * fcoe_ctlr_vn_add() - Add a VN2VN entry to the list, based on a claim reply.
2296 * @fip: The FCoE controller
2297 * @new: newly-parsed remote port with frport as a template for new rdata
2298 *
2299 * Called with ctlr_mutex held.
2300 */
2301static void fcoe_ctlr_vn_add(struct fcoe_ctlr *fip, struct fc_rport_priv *new)
2302{
2303 struct fc_lport *lport = fip->lp;
2304 struct fc_rport_priv *rdata;
2305 struct fc_rport_identifiers *ids;
2306 struct fcoe_rport *frport;
2307 u32 port_id;
2308
2309 port_id = new->ids.port_id;
2310 if (port_id == fip->port_id)
2311 return;
2312
2313 mutex_lock(&lport->disc.disc_mutex);
2314 rdata = lport->tt.rport_create(lport, port_id);
2315 if (!rdata) {
2316 mutex_unlock(&lport->disc.disc_mutex);
2317 return;
2318 }
2319
2320 rdata->ops = &fcoe_ctlr_vn_rport_ops;
2321 rdata->disc_id = lport->disc.disc_id;
2322
2323 ids = &rdata->ids;
2324 if ((ids->port_name != -1 && ids->port_name != new->ids.port_name) ||
2325 (ids->node_name != -1 && ids->node_name != new->ids.node_name))
2326 lport->tt.rport_logoff(rdata);
2327 ids->port_name = new->ids.port_name;
2328 ids->node_name = new->ids.node_name;
2329 mutex_unlock(&lport->disc.disc_mutex);
2330
2331 frport = fcoe_ctlr_rport(rdata);
2332 LIBFCOE_FIP_DBG(fip, "vn_add rport %6.6x %s\n",
2333 port_id, frport->fcoe_len ? "old" : "new");
2334 *frport = *fcoe_ctlr_rport(new);
2335 frport->time = 0;
2336}
2337
2338/**
2339 * fcoe_ctlr_vn_lookup() - Find VN remote port's MAC address
2340 * @fip: The FCoE controller
2341 * @port_id: The port_id of the remote VN_node
2342 * @mac: buffer which will hold the VN_NODE destination MAC address, if found.
2343 *
2344 * Returns non-zero error if no remote port found.
2345 */
2346static int fcoe_ctlr_vn_lookup(struct fcoe_ctlr *fip, u32 port_id, u8 *mac)
2347{
2348 struct fc_lport *lport = fip->lp;
2349 struct fc_rport_priv *rdata;
2350 struct fcoe_rport *frport;
2351 int ret = -1;
2352
2353 rcu_read_lock();
2354 rdata = lport->tt.rport_lookup(lport, port_id);
2355 if (rdata) {
2356 frport = fcoe_ctlr_rport(rdata);
2357 memcpy(mac, frport->enode_mac, ETH_ALEN);
2358 ret = 0;
2359 }
2360 rcu_read_unlock();
2361 return ret;
2362}
2363
2364/**
2365 * fcoe_ctlr_vn_claim_notify() - handle received FIP VN2VN Claim Notification
2366 * @fip: The FCoE controller
2367 * @new: newly-parsed remote port with frport as a template for new rdata
2368 *
2369 * Called with ctlr_mutex held.
2370 */
2371static void fcoe_ctlr_vn_claim_notify(struct fcoe_ctlr *fip,
2372 struct fc_rport_priv *new)
2373{
2374 struct fcoe_rport *frport = fcoe_ctlr_rport(new);
2375
2376 if (frport->flags & FIP_FL_REC_OR_P2P) {
2377 fcoe_ctlr_vn_send(fip, FIP_SC_VN_PROBE_REQ, fcoe_all_vn2vn, 0);
2378 return;
2379 }
2380 switch (fip->state) {
2381 case FIP_ST_VNMP_START:
2382 case FIP_ST_VNMP_PROBE1:
2383 case FIP_ST_VNMP_PROBE2:
2384 if (new->ids.port_id == fip->port_id)
2385 fcoe_ctlr_vn_restart(fip);
2386 break;
2387 case FIP_ST_VNMP_CLAIM:
2388 case FIP_ST_VNMP_UP:
2389 if (new->ids.port_id == fip->port_id) {
2390 if (new->ids.port_name > fip->lp->wwpn) {
2391 fcoe_ctlr_vn_restart(fip);
2392 break;
2393 }
2394 fcoe_ctlr_vn_send_claim(fip);
2395 break;
2396 }
2397 fcoe_ctlr_vn_send(fip, FIP_SC_VN_CLAIM_REP, frport->enode_mac,
2398 min((u32)frport->fcoe_len,
2399 fcoe_ctlr_fcoe_size(fip)));
2400 fcoe_ctlr_vn_add(fip, new);
2401 break;
2402 default:
2403 break;
2404 }
2405}
2406
2407/**
2408 * fcoe_ctlr_vn_claim_resp() - handle received Claim Response
2409 * @fip: The FCoE controller that received the frame
2410 * @new: newly-parsed remote port with frport from the Claim Response
2411 *
2412 * Called with ctlr_mutex held.
2413 */
2414static void fcoe_ctlr_vn_claim_resp(struct fcoe_ctlr *fip,
2415 struct fc_rport_priv *new)
2416{
2417 LIBFCOE_FIP_DBG(fip, "claim resp from from rport %x - state %s\n",
2418 new->ids.port_id, fcoe_ctlr_state(fip->state));
2419 if (fip->state == FIP_ST_VNMP_UP || fip->state == FIP_ST_VNMP_CLAIM)
2420 fcoe_ctlr_vn_add(fip, new);
2421}
2422
2423/**
2424 * fcoe_ctlr_vn_beacon() - handle received beacon.
2425 * @fip: The FCoE controller that received the frame
2426 * @new: newly-parsed remote port with frport from the Beacon
2427 *
2428 * Called with ctlr_mutex held.
2429 */
2430static void fcoe_ctlr_vn_beacon(struct fcoe_ctlr *fip,
2431 struct fc_rport_priv *new)
2432{
2433 struct fc_lport *lport = fip->lp;
2434 struct fc_rport_priv *rdata;
2435 struct fcoe_rport *frport;
2436
2437 frport = fcoe_ctlr_rport(new);
2438 if (frport->flags & FIP_FL_REC_OR_P2P) {
2439 fcoe_ctlr_vn_send(fip, FIP_SC_VN_PROBE_REQ, fcoe_all_vn2vn, 0);
2440 return;
2441 }
2442 mutex_lock(&lport->disc.disc_mutex);
2443 rdata = lport->tt.rport_lookup(lport, new->ids.port_id);
2444 if (rdata)
2445 kref_get(&rdata->kref);
2446 mutex_unlock(&lport->disc.disc_mutex);
2447 if (rdata) {
2448 if (rdata->ids.node_name == new->ids.node_name &&
2449 rdata->ids.port_name == new->ids.port_name) {
2450 frport = fcoe_ctlr_rport(rdata);
2451 if (!frport->time && fip->state == FIP_ST_VNMP_UP)
2452 lport->tt.rport_login(rdata);
2453 frport->time = jiffies;
2454 }
2455 kref_put(&rdata->kref, lport->tt.rport_destroy);
2456 return;
2457 }
2458 if (fip->state != FIP_ST_VNMP_UP)
2459 return;
2460
2461 /*
2462 * Beacon from a new neighbor.
2463 * Send a claim notify if one hasn't been sent recently.
2464 * Don't add the neighbor yet.
2465 */
2466 LIBFCOE_FIP_DBG(fip, "beacon from new rport %x. sending claim notify\n",
2467 new->ids.port_id);
2468 if (time_after(jiffies,
2469 fip->sol_time + msecs_to_jiffies(FIP_VN_ANN_WAIT)))
2470 fcoe_ctlr_vn_send_claim(fip);
2471}
2472
2473/**
2474 * fcoe_ctlr_vn_age() - Check for VN_ports without recent beacons
2475 * @fip: The FCoE controller
2476 *
2477 * Called with ctlr_mutex held.
2478 * Called only in state FIP_ST_VNMP_UP.
2479 * Returns the soonest time for next age-out or a time far in the future.
2480 */
2481static unsigned long fcoe_ctlr_vn_age(struct fcoe_ctlr *fip)
2482{
2483 struct fc_lport *lport = fip->lp;
2484 struct fc_rport_priv *rdata;
2485 struct fcoe_rport *frport;
2486 unsigned long next_time;
2487 unsigned long deadline;
2488
2489 next_time = jiffies + msecs_to_jiffies(FIP_VN_BEACON_INT * 10);
2490 mutex_lock(&lport->disc.disc_mutex);
2491 list_for_each_entry_rcu(rdata, &lport->disc.rports, peers) {
2492 frport = fcoe_ctlr_rport(rdata);
2493 if (!frport->time)
2494 continue;
2495 deadline = frport->time +
2496 msecs_to_jiffies(FIP_VN_BEACON_INT * 25 / 10);
2497 if (time_after_eq(jiffies, deadline)) {
2498 frport->time = 0;
2499 LIBFCOE_FIP_DBG(fip,
2500 "port %16.16llx fc_id %6.6x beacon expired\n",
2501 rdata->ids.port_name, rdata->ids.port_id);
2502 lport->tt.rport_logoff(rdata);
2503 } else if (time_before(deadline, next_time))
2504 next_time = deadline;
2505 }
2506 mutex_unlock(&lport->disc.disc_mutex);
2507 return next_time;
2508}
2509
2510/**
2511 * fcoe_ctlr_vn_recv() - Receive a FIP frame
2512 * @fip: The FCoE controller that received the frame
2513 * @skb: The received FIP frame
2514 *
2515 * Returns non-zero if the frame is dropped.
2516 * Always consumes the frame.
2517 */
2518static int fcoe_ctlr_vn_recv(struct fcoe_ctlr *fip, struct sk_buff *skb)
2519{
2520 struct fip_header *fiph;
2521 enum fip_vn2vn_subcode sub;
Kiran Patil2dc02ee2010-10-08 17:12:41 -07002522 struct {
Joe Eykholte10f8c62010-07-20 15:20:30 -07002523 struct fc_rport_priv rdata;
2524 struct fcoe_rport frport;
2525 } buf;
2526 int rc;
2527
2528 fiph = (struct fip_header *)skb->data;
2529 sub = fiph->fip_subcode;
2530
2531 rc = fcoe_ctlr_vn_parse(fip, skb, &buf.rdata);
2532 if (rc) {
2533 LIBFCOE_FIP_DBG(fip, "vn_recv vn_parse error %d\n", rc);
2534 goto drop;
2535 }
2536
2537 mutex_lock(&fip->ctlr_mutex);
2538 switch (sub) {
2539 case FIP_SC_VN_PROBE_REQ:
2540 fcoe_ctlr_vn_probe_req(fip, &buf.rdata);
2541 break;
2542 case FIP_SC_VN_PROBE_REP:
2543 fcoe_ctlr_vn_probe_reply(fip, &buf.rdata);
2544 break;
2545 case FIP_SC_VN_CLAIM_NOTIFY:
2546 fcoe_ctlr_vn_claim_notify(fip, &buf.rdata);
2547 break;
2548 case FIP_SC_VN_CLAIM_REP:
2549 fcoe_ctlr_vn_claim_resp(fip, &buf.rdata);
2550 break;
2551 case FIP_SC_VN_BEACON:
2552 fcoe_ctlr_vn_beacon(fip, &buf.rdata);
2553 break;
2554 default:
2555 LIBFCOE_FIP_DBG(fip, "vn_recv unknown subcode %d\n", sub);
2556 rc = -1;
2557 break;
2558 }
2559 mutex_unlock(&fip->ctlr_mutex);
2560drop:
2561 kfree_skb(skb);
2562 return rc;
2563}
2564
2565/**
2566 * fcoe_ctlr_disc_recv - discovery receive handler for VN2VN mode.
Joe Eykholt922611562010-07-20 15:21:12 -07002567 * @lport: The local port
2568 * @fp: The received frame
Joe Eykholte10f8c62010-07-20 15:20:30 -07002569 *
2570 * This should never be called since we don't see RSCNs or other
2571 * fabric-generated ELSes.
2572 */
Joe Eykholt922611562010-07-20 15:21:12 -07002573static void fcoe_ctlr_disc_recv(struct fc_lport *lport, struct fc_frame *fp)
Joe Eykholte10f8c62010-07-20 15:20:30 -07002574{
2575 struct fc_seq_els_data rjt_data;
2576
Joe Eykholte10f8c62010-07-20 15:20:30 -07002577 rjt_data.reason = ELS_RJT_UNSUP;
2578 rjt_data.explan = ELS_EXPL_NONE;
Joe Eykholt922611562010-07-20 15:21:12 -07002579 lport->tt.seq_els_rsp_send(fp, ELS_LS_RJT, &rjt_data);
Joe Eykholte10f8c62010-07-20 15:20:30 -07002580 fc_frame_free(fp);
2581}
2582
2583/**
2584 * fcoe_ctlr_disc_recv - start discovery for VN2VN mode.
2585 * @fip: The FCoE controller
2586 *
2587 * This sets a flag indicating that remote ports should be created
2588 * and started for the peers we discover. We use the disc_callback
2589 * pointer as that flag. Peers already discovered are created here.
2590 *
2591 * The lport lock is held during this call. The callback must be done
2592 * later, without holding either the lport or discovery locks.
2593 * The fcoe_ctlr lock may also be held during this call.
2594 */
2595static void fcoe_ctlr_disc_start(void (*callback)(struct fc_lport *,
2596 enum fc_disc_event),
2597 struct fc_lport *lport)
2598{
2599 struct fc_disc *disc = &lport->disc;
2600 struct fcoe_ctlr *fip = disc->priv;
2601
2602 mutex_lock(&disc->disc_mutex);
2603 disc->disc_callback = callback;
2604 disc->disc_id = (disc->disc_id + 2) | 1;
2605 disc->pending = 1;
2606 schedule_work(&fip->timer_work);
2607 mutex_unlock(&disc->disc_mutex);
2608}
2609
2610/**
2611 * fcoe_ctlr_vn_disc() - report FIP VN_port discovery results after claim state.
2612 * @fip: The FCoE controller
2613 *
2614 * Starts the FLOGI and PLOGI login process to each discovered rport for which
2615 * we've received at least one beacon.
2616 * Performs the discovery complete callback.
2617 */
2618static void fcoe_ctlr_vn_disc(struct fcoe_ctlr *fip)
2619{
2620 struct fc_lport *lport = fip->lp;
2621 struct fc_disc *disc = &lport->disc;
2622 struct fc_rport_priv *rdata;
2623 struct fcoe_rport *frport;
2624 void (*callback)(struct fc_lport *, enum fc_disc_event);
2625
2626 mutex_lock(&disc->disc_mutex);
2627 callback = disc->pending ? disc->disc_callback : NULL;
2628 disc->pending = 0;
2629 list_for_each_entry_rcu(rdata, &disc->rports, peers) {
2630 frport = fcoe_ctlr_rport(rdata);
2631 if (frport->time)
2632 lport->tt.rport_login(rdata);
2633 }
2634 mutex_unlock(&disc->disc_mutex);
2635 if (callback)
2636 callback(lport, DISC_EV_SUCCESS);
2637}
2638
2639/**
2640 * fcoe_ctlr_vn_timeout - timer work function for VN2VN mode.
2641 * @fip: The FCoE controller
2642 */
2643static void fcoe_ctlr_vn_timeout(struct fcoe_ctlr *fip)
2644{
2645 unsigned long next_time;
2646 u8 mac[ETH_ALEN];
2647 u32 new_port_id = 0;
2648
2649 mutex_lock(&fip->ctlr_mutex);
2650 switch (fip->state) {
2651 case FIP_ST_VNMP_START:
2652 fcoe_ctlr_set_state(fip, FIP_ST_VNMP_PROBE1);
2653 fcoe_ctlr_vn_send(fip, FIP_SC_VN_PROBE_REQ, fcoe_all_vn2vn, 0);
2654 next_time = jiffies + msecs_to_jiffies(FIP_VN_PROBE_WAIT);
2655 break;
2656 case FIP_ST_VNMP_PROBE1:
2657 fcoe_ctlr_set_state(fip, FIP_ST_VNMP_PROBE2);
2658 fcoe_ctlr_vn_send(fip, FIP_SC_VN_PROBE_REQ, fcoe_all_vn2vn, 0);
2659 next_time = jiffies + msecs_to_jiffies(FIP_VN_ANN_WAIT);
2660 break;
2661 case FIP_ST_VNMP_PROBE2:
2662 fcoe_ctlr_set_state(fip, FIP_ST_VNMP_CLAIM);
2663 new_port_id = fip->port_id;
2664 hton24(mac, FIP_VN_FC_MAP);
2665 hton24(mac + 3, new_port_id);
Joe Eykholtcd229e42010-07-20 15:20:40 -07002666 fcoe_ctlr_map_dest(fip);
Joe Eykholte10f8c62010-07-20 15:20:30 -07002667 fip->update_mac(fip->lp, mac);
2668 fcoe_ctlr_vn_send_claim(fip);
2669 next_time = jiffies + msecs_to_jiffies(FIP_VN_ANN_WAIT);
2670 break;
2671 case FIP_ST_VNMP_CLAIM:
2672 /*
2673 * This may be invoked either by starting discovery so don't
2674 * go to the next state unless it's been long enough.
2675 */
2676 next_time = fip->sol_time + msecs_to_jiffies(FIP_VN_ANN_WAIT);
2677 if (time_after_eq(jiffies, next_time)) {
2678 fcoe_ctlr_set_state(fip, FIP_ST_VNMP_UP);
2679 fcoe_ctlr_vn_send(fip, FIP_SC_VN_BEACON,
2680 fcoe_all_vn2vn, 0);
2681 next_time = jiffies + msecs_to_jiffies(FIP_VN_ANN_WAIT);
2682 fip->port_ka_time = next_time;
2683 }
2684 fcoe_ctlr_vn_disc(fip);
2685 break;
2686 case FIP_ST_VNMP_UP:
2687 next_time = fcoe_ctlr_vn_age(fip);
2688 if (time_after_eq(jiffies, fip->port_ka_time)) {
2689 fcoe_ctlr_vn_send(fip, FIP_SC_VN_BEACON,
2690 fcoe_all_vn2vn, 0);
2691 fip->port_ka_time = jiffies +
2692 msecs_to_jiffies(FIP_VN_BEACON_INT +
2693 (random32() % FIP_VN_BEACON_FUZZ));
2694 }
2695 if (time_before(fip->port_ka_time, next_time))
2696 next_time = fip->port_ka_time;
2697 break;
2698 case FIP_ST_LINK_WAIT:
2699 goto unlock;
2700 default:
Joe Perches11aa9902010-11-30 16:19:09 -08002701 WARN(1, "unexpected state %d\n", fip->state);
Joe Eykholte10f8c62010-07-20 15:20:30 -07002702 goto unlock;
2703 }
2704 mod_timer(&fip->timer, next_time);
2705unlock:
2706 mutex_unlock(&fip->ctlr_mutex);
2707
2708 /* If port ID is new, notify local port after dropping ctlr_mutex */
2709 if (new_port_id)
2710 fc_lport_set_local_id(fip->lp, new_port_id);
2711}
2712
2713/**
Robert Love70b51aa2009-11-03 11:47:45 -08002714 * fcoe_libfc_config() - Sets up libfc related properties for local port
2715 * @lp: The local port to configure libfc for
Joe Eykholte10f8c62010-07-20 15:20:30 -07002716 * @fip: The FCoE controller in use by the local port
Robert Love70b51aa2009-11-03 11:47:45 -08002717 * @tt: The libfc function template
Joe Eykholte10f8c62010-07-20 15:20:30 -07002718 * @init_fcp: If non-zero, the FCP portion of libfc should be initialized
Vasu Dev5e80f7f2009-03-17 11:42:18 -07002719 *
2720 * Returns : 0 for success
2721 */
Joe Eykholte10f8c62010-07-20 15:20:30 -07002722int fcoe_libfc_config(struct fc_lport *lport, struct fcoe_ctlr *fip,
2723 const struct libfc_function_template *tt, int init_fcp)
Vasu Dev5e80f7f2009-03-17 11:42:18 -07002724{
2725 /* Set the function pointers set by the LLDD */
Robert Love70b51aa2009-11-03 11:47:45 -08002726 memcpy(&lport->tt, tt, sizeof(*tt));
Joe Eykholte10f8c62010-07-20 15:20:30 -07002727 if (init_fcp && fc_fcp_init(lport))
Vasu Dev5e80f7f2009-03-17 11:42:18 -07002728 return -ENOMEM;
Robert Love70b51aa2009-11-03 11:47:45 -08002729 fc_exch_init(lport);
2730 fc_elsct_init(lport);
2731 fc_lport_init(lport);
Joe Eykholte10f8c62010-07-20 15:20:30 -07002732 if (fip->mode == FIP_MODE_VN2VN)
2733 lport->rport_priv_size = sizeof(struct fcoe_rport);
Robert Love70b51aa2009-11-03 11:47:45 -08002734 fc_rport_init(lport);
Joe Eykholte10f8c62010-07-20 15:20:30 -07002735 if (fip->mode == FIP_MODE_VN2VN) {
2736 lport->point_to_multipoint = 1;
2737 lport->tt.disc_recv_req = fcoe_ctlr_disc_recv;
2738 lport->tt.disc_start = fcoe_ctlr_disc_start;
2739 lport->tt.disc_stop = fcoe_ctlr_disc_stop;
2740 lport->tt.disc_stop_final = fcoe_ctlr_disc_stop_final;
2741 mutex_init(&lport->disc.disc_mutex);
2742 INIT_LIST_HEAD(&lport->disc.rports);
2743 lport->disc.priv = fip;
2744 } else {
2745 fc_disc_init(lport);
2746 }
Vasu Dev5e80f7f2009-03-17 11:42:18 -07002747 return 0;
2748}
2749EXPORT_SYMBOL_GPL(fcoe_libfc_config);