blob: 27c21ca802b2d36c264b496bae5f9f8faa13331f [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>
Vasu Dev5e80f7f2009-03-17 11:42:18 -070042
43#include <scsi/libfc.h>
Joe Eykholt97c83892009-03-17 11:42:40 -070044#include <scsi/libfcoe.h>
Vasu Dev9b34ecf2009-03-17 11:42:13 -070045
46MODULE_AUTHOR("Open-FCoE.org");
Joe Eykholt97c83892009-03-17 11:42:40 -070047MODULE_DESCRIPTION("FIP discovery protocol support for FCoE HBAs");
Vasu Dev9b34ecf2009-03-17 11:42:13 -070048MODULE_LICENSE("GPL v2");
Vasu Dev5e80f7f2009-03-17 11:42:18 -070049
Joe Eykholt97c83892009-03-17 11:42:40 -070050#define FCOE_CTLR_MIN_FKA 500 /* min keep alive (mS) */
51#define FCOE_CTLR_DEF_FKA FIP_DEF_FKA /* default keep alive (mS) */
52
53static void fcoe_ctlr_timeout(unsigned long);
Joe Eykholt42913652010-03-12 16:08:23 -080054static void fcoe_ctlr_timer_work(struct work_struct *);
Joe Eykholt97c83892009-03-17 11:42:40 -070055static void fcoe_ctlr_recv_work(struct work_struct *);
56
57static u8 fcoe_all_fcfs[ETH_ALEN] = FIP_ALL_FCF_MACS;
58
Robert Love650bd122009-06-10 15:31:05 -070059unsigned int libfcoe_debug_logging;
60module_param_named(debug_logging, libfcoe_debug_logging, int, S_IRUGO|S_IWUSR);
61MODULE_PARM_DESC(debug_logging, "a bit mask of logging levels");
Joe Eykholt97c83892009-03-17 11:42:40 -070062
Robert Love70b51aa2009-11-03 11:47:45 -080063#define LIBFCOE_LOGGING 0x01 /* General logging, not categorized */
Robert Love650bd122009-06-10 15:31:05 -070064#define LIBFCOE_FIP_LOGGING 0x02 /* FIP logging */
65
Robert Love70b51aa2009-11-03 11:47:45 -080066#define LIBFCOE_CHECK_LOGGING(LEVEL, CMD) \
67do { \
68 if (unlikely(libfcoe_debug_logging & LEVEL)) \
69 do { \
70 CMD; \
71 } while (0); \
Joe Eykholta69b06b2009-08-25 13:58:42 -070072} while (0)
Joe Eykholt97c83892009-03-17 11:42:40 -070073
Robert Love650bd122009-06-10 15:31:05 -070074#define LIBFCOE_DBG(fmt, args...) \
75 LIBFCOE_CHECK_LOGGING(LIBFCOE_LOGGING, \
76 printk(KERN_INFO "libfcoe: " fmt, ##args);)
77
Joe Eykholt0f51c2e2009-11-03 11:48:16 -080078#define LIBFCOE_FIP_DBG(fip, fmt, args...) \
Robert Love650bd122009-06-10 15:31:05 -070079 LIBFCOE_CHECK_LOGGING(LIBFCOE_FIP_LOGGING, \
Joe Eykholt0f51c2e2009-11-03 11:48:16 -080080 printk(KERN_INFO "host%d: fip: " fmt, \
81 (fip)->lp->host->host_no, ##args);)
Joe Eykholt97c83892009-03-17 11:42:40 -070082
Robert Love70b51aa2009-11-03 11:47:45 -080083/**
84 * fcoe_ctlr_mtu_valid() - Check if a FCF's MTU is valid
85 * @fcf: The FCF to check
86 *
Joe Eykholt97c83892009-03-17 11:42:40 -070087 * Return non-zero if FCF fcoe_size has been validated.
88 */
89static inline int fcoe_ctlr_mtu_valid(const struct fcoe_fcf *fcf)
90{
91 return (fcf->flags & FIP_FL_SOL) != 0;
92}
93
Robert Love70b51aa2009-11-03 11:47:45 -080094/**
95 * fcoe_ctlr_fcf_usable() - Check if a FCF is usable
96 * @fcf: The FCF to check
97 *
Joe Eykholt97c83892009-03-17 11:42:40 -070098 * Return non-zero if the FCF is usable.
99 */
100static inline int fcoe_ctlr_fcf_usable(struct fcoe_fcf *fcf)
101{
102 u16 flags = FIP_FL_SOL | FIP_FL_AVAIL;
103
104 return (fcf->flags & flags) == flags;
105}
106
107/**
Robert Love70b51aa2009-11-03 11:47:45 -0800108 * fcoe_ctlr_init() - Initialize the FCoE Controller instance
109 * @fip: The FCoE controller to initialize
Joe Eykholt97c83892009-03-17 11:42:40 -0700110 */
111void fcoe_ctlr_init(struct fcoe_ctlr *fip)
112{
113 fip->state = FIP_ST_LINK_WAIT;
Joe Eykholt22bcd222009-11-03 11:48:11 -0800114 fip->mode = FIP_ST_AUTO;
Joe Eykholt97c83892009-03-17 11:42:40 -0700115 INIT_LIST_HEAD(&fip->fcfs);
116 spin_lock_init(&fip->lock);
117 fip->flogi_oxid = FC_XID_UNKNOWN;
118 setup_timer(&fip->timer, fcoe_ctlr_timeout, (unsigned long)fip);
Joe Eykholt42913652010-03-12 16:08:23 -0800119 INIT_WORK(&fip->timer_work, fcoe_ctlr_timer_work);
Joe Eykholt97c83892009-03-17 11:42:40 -0700120 INIT_WORK(&fip->recv_work, fcoe_ctlr_recv_work);
121 skb_queue_head_init(&fip->fip_recv_list);
122}
123EXPORT_SYMBOL(fcoe_ctlr_init);
124
125/**
Robert Love70b51aa2009-11-03 11:47:45 -0800126 * fcoe_ctlr_reset_fcfs() - Reset and free all FCFs for a controller
127 * @fip: The FCoE controller whose FCFs are to be reset
Joe Eykholt97c83892009-03-17 11:42:40 -0700128 *
129 * Called with &fcoe_ctlr lock held.
130 */
131static void fcoe_ctlr_reset_fcfs(struct fcoe_ctlr *fip)
132{
133 struct fcoe_fcf *fcf;
134 struct fcoe_fcf *next;
135
136 fip->sel_fcf = NULL;
137 list_for_each_entry_safe(fcf, next, &fip->fcfs, list) {
138 list_del(&fcf->list);
139 kfree(fcf);
140 }
141 fip->fcf_count = 0;
142 fip->sel_time = 0;
143}
144
145/**
Robert Love70b51aa2009-11-03 11:47:45 -0800146 * fcoe_ctlr_destroy() - Disable and tear down a FCoE controller
147 * @fip: The FCoE controller to tear down
Joe Eykholt97c83892009-03-17 11:42:40 -0700148 *
149 * This is called by FCoE drivers before freeing the &fcoe_ctlr.
150 *
151 * The receive handler will have been deleted before this to guarantee
152 * that no more recv_work will be scheduled.
153 *
154 * The timer routine will simply return once we set FIP_ST_DISABLED.
155 * This guarantees that no further timeouts or work will be scheduled.
156 */
157void fcoe_ctlr_destroy(struct fcoe_ctlr *fip)
158{
Chris Leecha4b7cfa2009-08-25 13:59:08 -0700159 cancel_work_sync(&fip->recv_work);
Joe Eykholt1f4aed82009-11-03 11:48:22 -0800160 skb_queue_purge(&fip->fip_recv_list);
Chris Leecha4b7cfa2009-08-25 13:59:08 -0700161
Joe Eykholt97c83892009-03-17 11:42:40 -0700162 spin_lock_bh(&fip->lock);
163 fip->state = FIP_ST_DISABLED;
164 fcoe_ctlr_reset_fcfs(fip);
165 spin_unlock_bh(&fip->lock);
166 del_timer_sync(&fip->timer);
Joe Eykholt42913652010-03-12 16:08:23 -0800167 cancel_work_sync(&fip->timer_work);
Joe Eykholt97c83892009-03-17 11:42:40 -0700168}
169EXPORT_SYMBOL(fcoe_ctlr_destroy);
170
171/**
Robert Love70b51aa2009-11-03 11:47:45 -0800172 * fcoe_ctlr_fcoe_size() - Return the maximum FCoE size required for VN_Port
173 * @fip: The FCoE controller to get the maximum FCoE size from
Joe Eykholt97c83892009-03-17 11:42:40 -0700174 *
175 * Returns the maximum packet size including the FCoE header and trailer,
176 * but not including any Ethernet or VLAN headers.
177 */
178static inline u32 fcoe_ctlr_fcoe_size(struct fcoe_ctlr *fip)
179{
180 /*
181 * Determine the max FCoE frame size allowed, including
182 * FCoE header and trailer.
183 * Note: lp->mfs is currently the payload size, not the frame size.
184 */
185 return fip->lp->mfs + sizeof(struct fc_frame_header) +
186 sizeof(struct fcoe_hdr) + sizeof(struct fcoe_crc_eof);
187}
188
189/**
Robert Love70b51aa2009-11-03 11:47:45 -0800190 * fcoe_ctlr_solicit() - Send a FIP solicitation
191 * @fip: The FCoE controller to send the solicitation on
192 * @fcf: The destination FCF (if NULL, a multicast solicitation is sent)
Joe Eykholt97c83892009-03-17 11:42:40 -0700193 */
194static void fcoe_ctlr_solicit(struct fcoe_ctlr *fip, struct fcoe_fcf *fcf)
195{
196 struct sk_buff *skb;
197 struct fip_sol {
198 struct ethhdr eth;
199 struct fip_header fip;
200 struct {
201 struct fip_mac_desc mac;
202 struct fip_wwn_desc wwnn;
203 struct fip_size_desc size;
204 } __attribute__((packed)) desc;
205 } __attribute__((packed)) *sol;
206 u32 fcoe_size;
207
208 skb = dev_alloc_skb(sizeof(*sol));
209 if (!skb)
210 return;
211
212 sol = (struct fip_sol *)skb->data;
213
214 memset(sol, 0, sizeof(*sol));
215 memcpy(sol->eth.h_dest, fcf ? fcf->fcf_mac : fcoe_all_fcfs, ETH_ALEN);
216 memcpy(sol->eth.h_source, fip->ctl_src_addr, ETH_ALEN);
217 sol->eth.h_proto = htons(ETH_P_FIP);
218
219 sol->fip.fip_ver = FIP_VER_ENCAPS(FIP_VER);
220 sol->fip.fip_op = htons(FIP_OP_DISC);
221 sol->fip.fip_subcode = FIP_SC_SOL;
222 sol->fip.fip_dl_len = htons(sizeof(sol->desc) / FIP_BPW);
223 sol->fip.fip_flags = htons(FIP_FL_FPMA);
Vasu Dev184dd342009-05-17 12:33:28 +0000224 if (fip->spma)
225 sol->fip.fip_flags |= htons(FIP_FL_SPMA);
Joe Eykholt97c83892009-03-17 11:42:40 -0700226
227 sol->desc.mac.fd_desc.fip_dtype = FIP_DT_MAC;
228 sol->desc.mac.fd_desc.fip_dlen = sizeof(sol->desc.mac) / FIP_BPW;
229 memcpy(sol->desc.mac.fd_mac, fip->ctl_src_addr, ETH_ALEN);
230
231 sol->desc.wwnn.fd_desc.fip_dtype = FIP_DT_NAME;
232 sol->desc.wwnn.fd_desc.fip_dlen = sizeof(sol->desc.wwnn) / FIP_BPW;
233 put_unaligned_be64(fip->lp->wwnn, &sol->desc.wwnn.fd_wwn);
234
235 fcoe_size = fcoe_ctlr_fcoe_size(fip);
236 sol->desc.size.fd_desc.fip_dtype = FIP_DT_FCOE_SIZE;
237 sol->desc.size.fd_desc.fip_dlen = sizeof(sol->desc.size) / FIP_BPW;
238 sol->desc.size.fd_size = htons(fcoe_size);
239
240 skb_put(skb, sizeof(*sol));
Chris Leech0f491532009-05-06 10:52:18 -0700241 skb->protocol = htons(ETH_P_FIP);
Joe Eykholt97c83892009-03-17 11:42:40 -0700242 skb_reset_mac_header(skb);
243 skb_reset_network_header(skb);
244 fip->send(fip, skb);
245
246 if (!fcf)
247 fip->sol_time = jiffies;
248}
249
250/**
Robert Love70b51aa2009-11-03 11:47:45 -0800251 * fcoe_ctlr_link_up() - Start FCoE controller
252 * @fip: The FCoE controller to start
Joe Eykholt97c83892009-03-17 11:42:40 -0700253 *
254 * Called from the LLD when the network link is ready.
255 */
256void fcoe_ctlr_link_up(struct fcoe_ctlr *fip)
257{
258 spin_lock_bh(&fip->lock);
259 if (fip->state == FIP_ST_NON_FIP || fip->state == FIP_ST_AUTO) {
Joe Eykholt97c83892009-03-17 11:42:40 -0700260 spin_unlock_bh(&fip->lock);
261 fc_linkup(fip->lp);
262 } else if (fip->state == FIP_ST_LINK_WAIT) {
Joe Eykholt22bcd222009-11-03 11:48:11 -0800263 fip->state = fip->mode;
Joe Eykholt97c83892009-03-17 11:42:40 -0700264 spin_unlock_bh(&fip->lock);
Joe Eykholt22bcd222009-11-03 11:48:11 -0800265 if (fip->state == FIP_ST_AUTO)
Joe Eykholt0f51c2e2009-11-03 11:48:16 -0800266 LIBFCOE_FIP_DBG(fip, "%s", "setting AUTO mode.\n");
Joe Eykholt97c83892009-03-17 11:42:40 -0700267 fc_linkup(fip->lp);
268 fcoe_ctlr_solicit(fip, NULL);
269 } else
270 spin_unlock_bh(&fip->lock);
271}
272EXPORT_SYMBOL(fcoe_ctlr_link_up);
273
274/**
Robert Love70b51aa2009-11-03 11:47:45 -0800275 * fcoe_ctlr_reset() - Reset a FCoE controller
276 * @fip: The FCoE controller to reset
Joe Eykholt97c83892009-03-17 11:42:40 -0700277 */
Joe Eykholtdd42dac2009-11-03 11:48:27 -0800278static void fcoe_ctlr_reset(struct fcoe_ctlr *fip)
Joe Eykholt97c83892009-03-17 11:42:40 -0700279{
Joe Eykholt97c83892009-03-17 11:42:40 -0700280 fcoe_ctlr_reset_fcfs(fip);
281 del_timer(&fip->timer);
Joe Eykholt97c83892009-03-17 11:42:40 -0700282 fip->ctlr_ka_time = 0;
283 fip->port_ka_time = 0;
284 fip->sol_time = 0;
285 fip->flogi_oxid = FC_XID_UNKNOWN;
286 fip->map_dest = 0;
Joe Eykholt97c83892009-03-17 11:42:40 -0700287}
288
289/**
Robert Love70b51aa2009-11-03 11:47:45 -0800290 * fcoe_ctlr_link_down() - Stop a FCoE controller
291 * @fip: The FCoE controller to be stopped
Joe Eykholt97c83892009-03-17 11:42:40 -0700292 *
293 * Returns non-zero if the link was up and now isn't.
294 *
295 * Called from the LLD when the network link is not ready.
296 * There may be multiple calls while the link is down.
297 */
298int fcoe_ctlr_link_down(struct fcoe_ctlr *fip)
299{
Joe Eykholtdd42dac2009-11-03 11:48:27 -0800300 int link_dropped;
301
302 LIBFCOE_FIP_DBG(fip, "link down.\n");
303 spin_lock_bh(&fip->lock);
304 fcoe_ctlr_reset(fip);
Joe Eykholt42913652010-03-12 16:08:23 -0800305 link_dropped = fip->state != FIP_ST_LINK_WAIT;
Joe Eykholtdd42dac2009-11-03 11:48:27 -0800306 fip->state = FIP_ST_LINK_WAIT;
307 spin_unlock_bh(&fip->lock);
308
309 if (link_dropped)
310 fc_linkdown(fip->lp);
311 return link_dropped;
Joe Eykholt97c83892009-03-17 11:42:40 -0700312}
313EXPORT_SYMBOL(fcoe_ctlr_link_down);
314
315/**
Robert Love70b51aa2009-11-03 11:47:45 -0800316 * fcoe_ctlr_send_keep_alive() - Send a keep-alive to the selected FCF
317 * @fip: The FCoE controller to send the FKA on
318 * @lport: libfc fc_lport to send from
319 * @ports: 0 for controller keep-alive, 1 for port keep-alive
320 * @sa: The source MAC address
Joe Eykholt97c83892009-03-17 11:42:40 -0700321 *
322 * A controller keep-alive is sent every fka_period (typically 8 seconds).
323 * The source MAC is the native MAC address.
324 *
325 * A port keep-alive is sent every 90 seconds while logged in.
326 * The source MAC is the assigned mapped source address.
327 * The destination is the FCF's F-port.
328 */
Chris Leech11b56182009-11-03 11:46:29 -0800329static void fcoe_ctlr_send_keep_alive(struct fcoe_ctlr *fip,
330 struct fc_lport *lport,
331 int ports, u8 *sa)
Joe Eykholt97c83892009-03-17 11:42:40 -0700332{
333 struct sk_buff *skb;
334 struct fip_kal {
335 struct ethhdr eth;
336 struct fip_header fip;
337 struct fip_mac_desc mac;
338 } __attribute__((packed)) *kal;
339 struct fip_vn_desc *vn;
340 u32 len;
341 struct fc_lport *lp;
342 struct fcoe_fcf *fcf;
343
344 fcf = fip->sel_fcf;
345 lp = fip->lp;
Joe Eykholt281ae642010-06-11 16:43:33 -0700346 if (!fcf || (ports && !lp->port_id))
Joe Eykholt97c83892009-03-17 11:42:40 -0700347 return;
348
Yi Zoube276cb2009-11-03 11:50:16 -0800349 len = sizeof(*kal) + ports * sizeof(*vn);
Joe Eykholt97c83892009-03-17 11:42:40 -0700350 skb = dev_alloc_skb(len);
351 if (!skb)
352 return;
353
354 kal = (struct fip_kal *)skb->data;
355 memset(kal, 0, len);
356 memcpy(kal->eth.h_dest, fcf->fcf_mac, ETH_ALEN);
357 memcpy(kal->eth.h_source, sa, ETH_ALEN);
358 kal->eth.h_proto = htons(ETH_P_FIP);
359
360 kal->fip.fip_ver = FIP_VER_ENCAPS(FIP_VER);
361 kal->fip.fip_op = htons(FIP_OP_CTRL);
362 kal->fip.fip_subcode = FIP_SC_KEEP_ALIVE;
363 kal->fip.fip_dl_len = htons((sizeof(kal->mac) +
Robert Love70b51aa2009-11-03 11:47:45 -0800364 ports * sizeof(*vn)) / FIP_BPW);
Joe Eykholt97c83892009-03-17 11:42:40 -0700365 kal->fip.fip_flags = htons(FIP_FL_FPMA);
Vasu Dev184dd342009-05-17 12:33:28 +0000366 if (fip->spma)
367 kal->fip.fip_flags |= htons(FIP_FL_SPMA);
Joe Eykholt97c83892009-03-17 11:42:40 -0700368
369 kal->mac.fd_desc.fip_dtype = FIP_DT_MAC;
370 kal->mac.fd_desc.fip_dlen = sizeof(kal->mac) / FIP_BPW;
371 memcpy(kal->mac.fd_mac, fip->ctl_src_addr, ETH_ALEN);
Joe Eykholt97c83892009-03-17 11:42:40 -0700372 if (ports) {
373 vn = (struct fip_vn_desc *)(kal + 1);
374 vn->fd_desc.fip_dtype = FIP_DT_VN_ID;
375 vn->fd_desc.fip_dlen = sizeof(*vn) / FIP_BPW;
Chris Leech11b56182009-11-03 11:46:29 -0800376 memcpy(vn->fd_mac, fip->get_src_addr(lport), ETH_ALEN);
Kaladhar Musunurufb831532010-05-07 15:18:57 -0700377 hton24(vn->fd_fc_id, lport->port_id);
378 put_unaligned_be64(lport->wwpn, &vn->fd_wwpn);
Joe Eykholt97c83892009-03-17 11:42:40 -0700379 }
Joe Eykholt97c83892009-03-17 11:42:40 -0700380 skb_put(skb, len);
Chris Leech0f491532009-05-06 10:52:18 -0700381 skb->protocol = htons(ETH_P_FIP);
Joe Eykholt97c83892009-03-17 11:42:40 -0700382 skb_reset_mac_header(skb);
383 skb_reset_network_header(skb);
384 fip->send(fip, skb);
385}
386
387/**
Robert Love70b51aa2009-11-03 11:47:45 -0800388 * fcoe_ctlr_encaps() - Encapsulate an ELS frame for FIP, without sending it
389 * @fip: The FCoE controller for the ELS frame
390 * @dtype: The FIP descriptor type for the frame
391 * @skb: The FCoE ELS frame including FC header but no FCoE headers
Joe Eykholt97c83892009-03-17 11:42:40 -0700392 *
393 * Returns non-zero error code on failure.
394 *
395 * The caller must check that the length is a multiple of 4.
396 *
397 * The @skb must have enough headroom (28 bytes) and tailroom (8 bytes).
398 * Headroom includes the FIP encapsulation description, FIP header, and
399 * Ethernet header. The tailroom is for the FIP MAC descriptor.
400 */
Chris Leech11b56182009-11-03 11:46:29 -0800401static int fcoe_ctlr_encaps(struct fcoe_ctlr *fip, struct fc_lport *lport,
Joe Eykholt97c83892009-03-17 11:42:40 -0700402 u8 dtype, struct sk_buff *skb)
403{
404 struct fip_encaps_head {
405 struct ethhdr eth;
406 struct fip_header fip;
407 struct fip_encaps encaps;
408 } __attribute__((packed)) *cap;
409 struct fip_mac_desc *mac;
410 struct fcoe_fcf *fcf;
411 size_t dlen;
Yi Zou5a84bae2009-07-29 17:03:55 -0700412 u16 fip_flags;
Joe Eykholt97c83892009-03-17 11:42:40 -0700413
414 fcf = fip->sel_fcf;
415 if (!fcf)
416 return -ENODEV;
Yi Zou5a84bae2009-07-29 17:03:55 -0700417
418 /* set flags according to both FCF and lport's capability on SPMA */
419 fip_flags = fcf->flags;
420 fip_flags &= fip->spma ? FIP_FL_SPMA | FIP_FL_FPMA : FIP_FL_FPMA;
421 if (!fip_flags)
422 return -ENODEV;
423
Joe Eykholt97c83892009-03-17 11:42:40 -0700424 dlen = sizeof(struct fip_encaps) + skb->len; /* len before push */
425 cap = (struct fip_encaps_head *)skb_push(skb, sizeof(*cap));
426
427 memset(cap, 0, sizeof(*cap));
428 memcpy(cap->eth.h_dest, fcf->fcf_mac, ETH_ALEN);
429 memcpy(cap->eth.h_source, fip->ctl_src_addr, ETH_ALEN);
430 cap->eth.h_proto = htons(ETH_P_FIP);
431
432 cap->fip.fip_ver = FIP_VER_ENCAPS(FIP_VER);
433 cap->fip.fip_op = htons(FIP_OP_LS);
434 cap->fip.fip_subcode = FIP_SC_REQ;
435 cap->fip.fip_dl_len = htons((dlen + sizeof(*mac)) / FIP_BPW);
Yi Zou5a84bae2009-07-29 17:03:55 -0700436 cap->fip.fip_flags = htons(fip_flags);
Joe Eykholt97c83892009-03-17 11:42:40 -0700437
438 cap->encaps.fd_desc.fip_dtype = dtype;
439 cap->encaps.fd_desc.fip_dlen = dlen / FIP_BPW;
440
441 mac = (struct fip_mac_desc *)skb_put(skb, sizeof(*mac));
Kaladhar Musunuru8b889e42010-05-07 15:18:52 -0700442 memset(mac, 0, sizeof(*mac));
Joe Eykholt97c83892009-03-17 11:42:40 -0700443 mac->fd_desc.fip_dtype = FIP_DT_MAC;
444 mac->fd_desc.fip_dlen = sizeof(*mac) / FIP_BPW;
Robert Love593abc02010-04-09 14:22:17 -0700445 if (dtype != FIP_DT_FLOGI && dtype != FIP_DT_FDISC) {
Chris Leech11b56182009-11-03 11:46:29 -0800446 memcpy(mac->fd_mac, fip->get_src_addr(lport), ETH_ALEN);
Robert Love593abc02010-04-09 14:22:17 -0700447 } else if (fip_flags & FIP_FL_SPMA) {
448 LIBFCOE_FIP_DBG(fip, "FLOGI/FDISC sent with SPMA\n");
Vasu Dev184dd342009-05-17 12:33:28 +0000449 memcpy(mac->fd_mac, fip->ctl_src_addr, ETH_ALEN);
Robert Love593abc02010-04-09 14:22:17 -0700450 } else {
451 LIBFCOE_FIP_DBG(fip, "FLOGI/FDISC sent with FPMA\n");
452 /* FPMA only FLOGI must leave the MAC desc set to all 0s */
453 }
Joe Eykholt97c83892009-03-17 11:42:40 -0700454
Chris Leech0f491532009-05-06 10:52:18 -0700455 skb->protocol = htons(ETH_P_FIP);
Joe Eykholt97c83892009-03-17 11:42:40 -0700456 skb_reset_mac_header(skb);
457 skb_reset_network_header(skb);
458 return 0;
459}
460
461/**
462 * fcoe_ctlr_els_send() - Send an ELS frame encapsulated by FIP if appropriate.
463 * @fip: FCoE controller.
Chris Leech11b56182009-11-03 11:46:29 -0800464 * @lport: libfc fc_lport to send from
Joe Eykholt97c83892009-03-17 11:42:40 -0700465 * @skb: FCoE ELS frame including FC header but no FCoE headers.
466 *
467 * Returns a non-zero error code if the frame should not be sent.
468 * Returns zero if the caller should send the frame with FCoE encapsulation.
469 *
470 * The caller must check that the length is a multiple of 4.
471 * The SKB must have enough headroom (28 bytes) and tailroom (8 bytes).
472 */
Chris Leech11b56182009-11-03 11:46:29 -0800473int fcoe_ctlr_els_send(struct fcoe_ctlr *fip, struct fc_lport *lport,
474 struct sk_buff *skb)
Joe Eykholt97c83892009-03-17 11:42:40 -0700475{
476 struct fc_frame_header *fh;
477 u16 old_xid;
478 u8 op;
Chris Leech11b56182009-11-03 11:46:29 -0800479 u8 mac[ETH_ALEN];
Joe Eykholt97c83892009-03-17 11:42:40 -0700480
Joe Eykholt97c83892009-03-17 11:42:40 -0700481 fh = (struct fc_frame_header *)skb->data;
482 op = *(u8 *)(fh + 1);
483
Joe Eykholt5f48f702009-05-06 10:52:12 -0700484 if (op == ELS_FLOGI) {
Joe Eykholt97c83892009-03-17 11:42:40 -0700485 old_xid = fip->flogi_oxid;
486 fip->flogi_oxid = ntohs(fh->fh_ox_id);
487 if (fip->state == FIP_ST_AUTO) {
488 if (old_xid == FC_XID_UNKNOWN)
489 fip->flogi_count = 0;
490 fip->flogi_count++;
491 if (fip->flogi_count < 3)
492 goto drop;
493 fip->map_dest = 1;
494 return 0;
495 }
Joe Eykholt5f48f702009-05-06 10:52:12 -0700496 if (fip->state == FIP_ST_NON_FIP)
497 fip->map_dest = 1;
498 }
499
500 if (fip->state == FIP_ST_NON_FIP)
501 return 0;
Joe Eykholtf31f2a12009-11-03 11:48:32 -0800502 if (!fip->sel_fcf)
503 goto drop;
Joe Eykholt5f48f702009-05-06 10:52:12 -0700504
505 switch (op) {
506 case ELS_FLOGI:
Joe Eykholt97c83892009-03-17 11:42:40 -0700507 op = FIP_DT_FLOGI;
508 break;
509 case ELS_FDISC:
510 if (ntoh24(fh->fh_s_id))
511 return 0;
512 op = FIP_DT_FDISC;
513 break;
514 case ELS_LOGO:
515 if (fip->state != FIP_ST_ENABLED)
516 return 0;
517 if (ntoh24(fh->fh_d_id) != FC_FID_FLOGI)
518 return 0;
519 op = FIP_DT_LOGO;
520 break;
521 case ELS_LS_ACC:
522 if (fip->flogi_oxid == FC_XID_UNKNOWN)
523 return 0;
524 if (!ntoh24(fh->fh_s_id))
525 return 0;
526 if (fip->state == FIP_ST_AUTO)
527 return 0;
528 /*
529 * Here we must've gotten an SID by accepting an FLOGI
530 * from a point-to-point connection. Switch to using
531 * the source mac based on the SID. The destination
532 * MAC in this case would have been set by receving the
533 * FLOGI.
534 */
535 fip->flogi_oxid = FC_XID_UNKNOWN;
Chris Leech11b56182009-11-03 11:46:29 -0800536 fc_fcoe_set_mac(mac, fh->fh_d_id);
537 fip->update_mac(lport, mac);
Joe Eykholt97c83892009-03-17 11:42:40 -0700538 return 0;
539 default:
540 if (fip->state != FIP_ST_ENABLED)
541 goto drop;
542 return 0;
543 }
Chris Leech11b56182009-11-03 11:46:29 -0800544 if (fcoe_ctlr_encaps(fip, lport, op, skb))
Joe Eykholt97c83892009-03-17 11:42:40 -0700545 goto drop;
546 fip->send(fip, skb);
547 return -EINPROGRESS;
548drop:
549 kfree_skb(skb);
550 return -EINVAL;
551}
552EXPORT_SYMBOL(fcoe_ctlr_els_send);
553
Robert Love70b51aa2009-11-03 11:47:45 -0800554/**
555 * fcoe_ctlr_age_fcfs() - Reset and free all old FCFs for a controller
556 * @fip: The FCoE controller to free FCFs on
Joe Eykholt97c83892009-03-17 11:42:40 -0700557 *
Joe Eykholtf018b732010-03-12 16:08:55 -0800558 * Called with lock held and preemption disabled.
Joe Eykholt97c83892009-03-17 11:42:40 -0700559 *
Joe Eykholt8690cb82010-06-11 16:44:10 -0700560 * An FCF is considered old if we have missed two advertisements.
561 * That is, there have been no valid advertisement from it for 2.5
562 * times its keep-alive period.
Joe Eykholt97c83892009-03-17 11:42:40 -0700563 *
564 * In addition, determine the time when an FCF selection can occur.
Yi Zouf3da80e2009-11-20 14:55:08 -0800565 *
566 * Also, increment the MissDiscAdvCount when no advertisement is received
567 * for the corresponding FCF for 1.5 * FKA_ADV_PERIOD (FC-BB-5 LESB).
Joe Eykholt8690cb82010-06-11 16:44:10 -0700568 *
569 * Returns the time in jiffies for the next call.
Joe Eykholt97c83892009-03-17 11:42:40 -0700570 */
Joe Eykholt8690cb82010-06-11 16:44:10 -0700571static unsigned long fcoe_ctlr_age_fcfs(struct fcoe_ctlr *fip)
Joe Eykholt97c83892009-03-17 11:42:40 -0700572{
573 struct fcoe_fcf *fcf;
574 struct fcoe_fcf *next;
Joe Eykholt8690cb82010-06-11 16:44:10 -0700575 unsigned long next_timer = jiffies + msecs_to_jiffies(FIP_VN_KA_PERIOD);
576 unsigned long deadline;
Joe Eykholt97c83892009-03-17 11:42:40 -0700577 unsigned long sel_time = 0;
Joe Eykholtf018b732010-03-12 16:08:55 -0800578 struct fcoe_dev_stats *stats;
Joe Eykholt97c83892009-03-17 11:42:40 -0700579
580 list_for_each_entry_safe(fcf, next, &fip->fcfs, list) {
Joe Eykholt8690cb82010-06-11 16:44:10 -0700581 deadline = fcf->time + fcf->fka_period + fcf->fka_period / 2;
582 if (fip->sel_fcf == fcf) {
583 if (time_after(jiffies, deadline)) {
584 stats = per_cpu_ptr(fip->lp->dev_stats,
585 smp_processor_id());
586 stats->MissDiscAdvCount++;
587 printk(KERN_INFO "libfcoe: host%d: "
588 "Missing Discovery Advertisement "
589 "for fab %16.16llx count %lld\n",
590 fip->lp->host->host_no, fcf->fabric_name,
591 stats->MissDiscAdvCount);
592 } else if (time_after(next_timer, deadline))
593 next_timer = deadline;
Yi Zouf3da80e2009-11-20 14:55:08 -0800594 }
Joe Eykholt8690cb82010-06-11 16:44:10 -0700595
596 deadline += fcf->fka_period;
Joe Eykholtd99ee452010-06-11 16:44:15 -0700597 if (time_after_eq(jiffies, deadline)) {
Joe Eykholt97c83892009-03-17 11:42:40 -0700598 if (fip->sel_fcf == fcf)
599 fip->sel_fcf = NULL;
600 list_del(&fcf->list);
601 WARN_ON(!fip->fcf_count);
602 fip->fcf_count--;
603 kfree(fcf);
Joe Eykholtf018b732010-03-12 16:08:55 -0800604 stats = per_cpu_ptr(fip->lp->dev_stats,
605 smp_processor_id());
606 stats->VLinkFailureCount++;
Joe Eykholt8690cb82010-06-11 16:44:10 -0700607 } else {
608 if (time_after(next_timer, deadline))
609 next_timer = deadline;
610 if (fcoe_ctlr_mtu_valid(fcf) &&
611 (!sel_time || time_before(sel_time, fcf->time)))
612 sel_time = fcf->time;
Joe Eykholt97c83892009-03-17 11:42:40 -0700613 }
614 }
Joe Eykholtd99ee452010-06-11 16:44:15 -0700615 if (sel_time && !fip->sel_fcf && !fip->sel_time) {
Joe Eykholt97c83892009-03-17 11:42:40 -0700616 sel_time += msecs_to_jiffies(FCOE_CTLR_START_DELAY);
617 fip->sel_time = sel_time;
Joe Eykholt97c83892009-03-17 11:42:40 -0700618 }
Joe Eykholtd99ee452010-06-11 16:44:15 -0700619
Joe Eykholt8690cb82010-06-11 16:44:10 -0700620 return next_timer;
Joe Eykholt97c83892009-03-17 11:42:40 -0700621}
622
623/**
Robert Love70b51aa2009-11-03 11:47:45 -0800624 * fcoe_ctlr_parse_adv() - Decode a FIP advertisement into a new FCF entry
Joe Eykholt0f51c2e2009-11-03 11:48:16 -0800625 * @fip: The FCoE controller receiving the advertisement
Robert Love70b51aa2009-11-03 11:47:45 -0800626 * @skb: The received FIP advertisement frame
627 * @fcf: The resulting FCF entry
Joe Eykholt97c83892009-03-17 11:42:40 -0700628 *
629 * Returns zero on a valid parsed advertisement,
630 * otherwise returns non zero value.
631 */
Joe Eykholt0f51c2e2009-11-03 11:48:16 -0800632static int fcoe_ctlr_parse_adv(struct fcoe_ctlr *fip,
633 struct sk_buff *skb, struct fcoe_fcf *fcf)
Joe Eykholt97c83892009-03-17 11:42:40 -0700634{
635 struct fip_header *fiph;
636 struct fip_desc *desc = NULL;
637 struct fip_wwn_desc *wwn;
638 struct fip_fab_desc *fab;
639 struct fip_fka_desc *fka;
640 unsigned long t;
641 size_t rlen;
642 size_t dlen;
643
644 memset(fcf, 0, sizeof(*fcf));
645 fcf->fka_period = msecs_to_jiffies(FCOE_CTLR_DEF_FKA);
646
647 fiph = (struct fip_header *)skb->data;
648 fcf->flags = ntohs(fiph->fip_flags);
649
650 rlen = ntohs(fiph->fip_dl_len) * 4;
651 if (rlen + sizeof(*fiph) > skb->len)
652 return -EINVAL;
653
654 desc = (struct fip_desc *)(fiph + 1);
655 while (rlen > 0) {
656 dlen = desc->fip_dlen * FIP_BPW;
657 if (dlen < sizeof(*desc) || dlen > rlen)
658 return -EINVAL;
659 switch (desc->fip_dtype) {
660 case FIP_DT_PRI:
661 if (dlen != sizeof(struct fip_pri_desc))
662 goto len_err;
663 fcf->pri = ((struct fip_pri_desc *)desc)->fd_pri;
664 break;
665 case FIP_DT_MAC:
666 if (dlen != sizeof(struct fip_mac_desc))
667 goto len_err;
668 memcpy(fcf->fcf_mac,
669 ((struct fip_mac_desc *)desc)->fd_mac,
670 ETH_ALEN);
671 if (!is_valid_ether_addr(fcf->fcf_mac)) {
Joe Eykholt0f51c2e2009-11-03 11:48:16 -0800672 LIBFCOE_FIP_DBG(fip, "Invalid MAC address "
Robert Love650bd122009-06-10 15:31:05 -0700673 "in FIP adv\n");
Joe Eykholt97c83892009-03-17 11:42:40 -0700674 return -EINVAL;
675 }
676 break;
677 case FIP_DT_NAME:
678 if (dlen != sizeof(struct fip_wwn_desc))
679 goto len_err;
680 wwn = (struct fip_wwn_desc *)desc;
681 fcf->switch_name = get_unaligned_be64(&wwn->fd_wwn);
682 break;
683 case FIP_DT_FAB:
684 if (dlen != sizeof(struct fip_fab_desc))
685 goto len_err;
686 fab = (struct fip_fab_desc *)desc;
687 fcf->fabric_name = get_unaligned_be64(&fab->fd_wwn);
688 fcf->vfid = ntohs(fab->fd_vfid);
689 fcf->fc_map = ntoh24(fab->fd_map);
690 break;
691 case FIP_DT_FKA:
692 if (dlen != sizeof(struct fip_fka_desc))
693 goto len_err;
694 fka = (struct fip_fka_desc *)desc;
Yi Zou8cdffdc2009-11-20 14:54:57 -0800695 if (fka->fd_flags & FIP_FKA_ADV_D)
696 fcf->fd_flags = 1;
Joe Eykholt97c83892009-03-17 11:42:40 -0700697 t = ntohl(fka->fd_fka_period);
698 if (t >= FCOE_CTLR_MIN_FKA)
699 fcf->fka_period = msecs_to_jiffies(t);
700 break;
701 case FIP_DT_MAP_OUI:
702 case FIP_DT_FCOE_SIZE:
703 case FIP_DT_FLOGI:
704 case FIP_DT_FDISC:
705 case FIP_DT_LOGO:
706 case FIP_DT_ELP:
707 default:
Joe Eykholt0f51c2e2009-11-03 11:48:16 -0800708 LIBFCOE_FIP_DBG(fip, "unexpected descriptor type %x "
Robert Love650bd122009-06-10 15:31:05 -0700709 "in FIP adv\n", desc->fip_dtype);
Joe Eykholt97c83892009-03-17 11:42:40 -0700710 /* standard says ignore unknown descriptors >= 128 */
711 if (desc->fip_dtype < FIP_DT_VENDOR_BASE)
712 return -EINVAL;
Bhanu Prakash Gollapudi1508f3ec2010-06-11 16:43:38 -0700713 break;
Joe Eykholt97c83892009-03-17 11:42:40 -0700714 }
715 desc = (struct fip_desc *)((char *)desc + dlen);
716 rlen -= dlen;
717 }
718 if (!fcf->fc_map || (fcf->fc_map & 0x10000))
719 return -EINVAL;
720 if (!fcf->switch_name || !fcf->fabric_name)
721 return -EINVAL;
722 return 0;
723
724len_err:
Joe Eykholt0f51c2e2009-11-03 11:48:16 -0800725 LIBFCOE_FIP_DBG(fip, "FIP length error in descriptor type %x len %zu\n",
Robert Love650bd122009-06-10 15:31:05 -0700726 desc->fip_dtype, dlen);
Joe Eykholt97c83892009-03-17 11:42:40 -0700727 return -EINVAL;
728}
729
730/**
Robert Love70b51aa2009-11-03 11:47:45 -0800731 * fcoe_ctlr_recv_adv() - Handle an incoming advertisement
732 * @fip: The FCoE controller receiving the advertisement
733 * @skb: The received FIP packet
Joe Eykholt97c83892009-03-17 11:42:40 -0700734 */
735static void fcoe_ctlr_recv_adv(struct fcoe_ctlr *fip, struct sk_buff *skb)
736{
737 struct fcoe_fcf *fcf;
738 struct fcoe_fcf new;
739 struct fcoe_fcf *found;
740 unsigned long sol_tov = msecs_to_jiffies(FCOE_CTRL_SOL_TOV);
741 int first = 0;
742 int mtu_valid;
743
Joe Eykholt0f51c2e2009-11-03 11:48:16 -0800744 if (fcoe_ctlr_parse_adv(fip, skb, &new))
Joe Eykholt97c83892009-03-17 11:42:40 -0700745 return;
746
747 spin_lock_bh(&fip->lock);
748 first = list_empty(&fip->fcfs);
749 found = NULL;
750 list_for_each_entry(fcf, &fip->fcfs, list) {
751 if (fcf->switch_name == new.switch_name &&
752 fcf->fabric_name == new.fabric_name &&
753 fcf->fc_map == new.fc_map &&
754 compare_ether_addr(fcf->fcf_mac, new.fcf_mac) == 0) {
755 found = fcf;
756 break;
757 }
758 }
759 if (!found) {
760 if (fip->fcf_count >= FCOE_CTLR_FCF_LIMIT)
761 goto out;
762
763 fcf = kmalloc(sizeof(*fcf), GFP_ATOMIC);
764 if (!fcf)
765 goto out;
766
767 fip->fcf_count++;
768 memcpy(fcf, &new, sizeof(new));
769 list_add(&fcf->list, &fip->fcfs);
770 } else {
771 /*
Joe Eykholtc600fea2010-06-11 16:44:20 -0700772 * Update the FCF's keep-alive descriptor flags.
773 * Other flag changes from new advertisements are
774 * ignored after a solicited advertisement is
775 * received and the FCF is selectable (usable).
Joe Eykholt97c83892009-03-17 11:42:40 -0700776 */
Joe Eykholtc600fea2010-06-11 16:44:20 -0700777 fcf->fd_flags = new.fd_flags;
778 if (!fcoe_ctlr_fcf_usable(fcf))
779 fcf->flags = new.flags;
780
Joe Eykholtd99ee452010-06-11 16:44:15 -0700781 if (fcf == fip->sel_fcf && !fcf->fd_flags) {
Joe Eykholt97c83892009-03-17 11:42:40 -0700782 fip->ctlr_ka_time -= fcf->fka_period;
783 fip->ctlr_ka_time += new.fka_period;
784 if (time_before(fip->ctlr_ka_time, fip->timer.expires))
785 mod_timer(&fip->timer, fip->ctlr_ka_time);
Joe Eykholtc600fea2010-06-11 16:44:20 -0700786 }
Joe Eykholt97c83892009-03-17 11:42:40 -0700787 fcf->fka_period = new.fka_period;
788 memcpy(fcf->fcf_mac, new.fcf_mac, ETH_ALEN);
789 }
790 mtu_valid = fcoe_ctlr_mtu_valid(fcf);
791 fcf->time = jiffies;
Robert Love650bd122009-06-10 15:31:05 -0700792 if (!found) {
Chris Leech9f8f3aa2010-04-09 14:23:16 -0700793 LIBFCOE_FIP_DBG(fip, "New FCF for fab %16.16llx "
794 "map %x val %d\n",
Robert Love650bd122009-06-10 15:31:05 -0700795 fcf->fabric_name, fcf->fc_map, mtu_valid);
796 }
Joe Eykholt97c83892009-03-17 11:42:40 -0700797
798 /*
799 * If this advertisement is not solicited and our max receive size
800 * hasn't been verified, send a solicited advertisement.
801 */
802 if (!mtu_valid)
803 fcoe_ctlr_solicit(fip, fcf);
804
805 /*
806 * If its been a while since we did a solicit, and this is
807 * the first advertisement we've received, do a multicast
808 * solicitation to gather as many advertisements as we can
809 * before selection occurs.
810 */
811 if (first && time_after(jiffies, fip->sol_time + sol_tov))
812 fcoe_ctlr_solicit(fip, NULL);
813
814 /*
815 * If this is the first validated FCF, note the time and
816 * set a timer to trigger selection.
817 */
Joe Eykholtd99ee452010-06-11 16:44:15 -0700818 if (mtu_valid && !fip->sel_fcf && fcoe_ctlr_fcf_usable(fcf)) {
Joe Eykholt97c83892009-03-17 11:42:40 -0700819 fip->sel_time = jiffies +
Robert Love70b51aa2009-11-03 11:47:45 -0800820 msecs_to_jiffies(FCOE_CTLR_START_DELAY);
Joe Eykholt97c83892009-03-17 11:42:40 -0700821 if (!timer_pending(&fip->timer) ||
822 time_before(fip->sel_time, fip->timer.expires))
823 mod_timer(&fip->timer, fip->sel_time);
824 }
825out:
826 spin_unlock_bh(&fip->lock);
827}
828
829/**
Robert Love70b51aa2009-11-03 11:47:45 -0800830 * fcoe_ctlr_recv_els() - Handle an incoming FIP encapsulated ELS frame
831 * @fip: The FCoE controller which received the packet
832 * @skb: The received FIP packet
Joe Eykholt97c83892009-03-17 11:42:40 -0700833 */
834static void fcoe_ctlr_recv_els(struct fcoe_ctlr *fip, struct sk_buff *skb)
835{
Robert Love70b51aa2009-11-03 11:47:45 -0800836 struct fc_lport *lport = fip->lp;
Joe Eykholt97c83892009-03-17 11:42:40 -0700837 struct fip_header *fiph;
Chris Leech11b56182009-11-03 11:46:29 -0800838 struct fc_frame *fp = (struct fc_frame *)skb;
Joe Eykholt97c83892009-03-17 11:42:40 -0700839 struct fc_frame_header *fh = NULL;
840 struct fip_desc *desc;
841 struct fip_encaps *els;
842 struct fcoe_dev_stats *stats;
843 enum fip_desc_type els_dtype = 0;
844 u8 els_op;
845 u8 sub;
846 u8 granted_mac[ETH_ALEN] = { 0 };
847 size_t els_len = 0;
848 size_t rlen;
849 size_t dlen;
850
851 fiph = (struct fip_header *)skb->data;
852 sub = fiph->fip_subcode;
853 if (sub != FIP_SC_REQ && sub != FIP_SC_REP)
854 goto drop;
855
856 rlen = ntohs(fiph->fip_dl_len) * 4;
857 if (rlen + sizeof(*fiph) > skb->len)
858 goto drop;
859
860 desc = (struct fip_desc *)(fiph + 1);
861 while (rlen > 0) {
862 dlen = desc->fip_dlen * FIP_BPW;
863 if (dlen < sizeof(*desc) || dlen > rlen)
864 goto drop;
865 switch (desc->fip_dtype) {
866 case FIP_DT_MAC:
867 if (dlen != sizeof(struct fip_mac_desc))
868 goto len_err;
869 memcpy(granted_mac,
870 ((struct fip_mac_desc *)desc)->fd_mac,
871 ETH_ALEN);
872 if (!is_valid_ether_addr(granted_mac)) {
Joe Eykholt0f51c2e2009-11-03 11:48:16 -0800873 LIBFCOE_FIP_DBG(fip, "Invalid MAC address "
Robert Love650bd122009-06-10 15:31:05 -0700874 "in FIP ELS\n");
Joe Eykholt97c83892009-03-17 11:42:40 -0700875 goto drop;
876 }
Chris Leech11b56182009-11-03 11:46:29 -0800877 memcpy(fr_cb(fp)->granted_mac, granted_mac, ETH_ALEN);
Joe Eykholt97c83892009-03-17 11:42:40 -0700878 break;
879 case FIP_DT_FLOGI:
880 case FIP_DT_FDISC:
881 case FIP_DT_LOGO:
882 case FIP_DT_ELP:
883 if (fh)
884 goto drop;
885 if (dlen < sizeof(*els) + sizeof(*fh) + 1)
886 goto len_err;
887 els_len = dlen - sizeof(*els);
888 els = (struct fip_encaps *)desc;
889 fh = (struct fc_frame_header *)(els + 1);
890 els_dtype = desc->fip_dtype;
891 break;
892 default:
Joe Eykholt0f51c2e2009-11-03 11:48:16 -0800893 LIBFCOE_FIP_DBG(fip, "unexpected descriptor type %x "
Robert Love650bd122009-06-10 15:31:05 -0700894 "in FIP adv\n", desc->fip_dtype);
Joe Eykholt97c83892009-03-17 11:42:40 -0700895 /* standard says ignore unknown descriptors >= 128 */
896 if (desc->fip_dtype < FIP_DT_VENDOR_BASE)
897 goto drop;
Bhanu Prakash Gollapudi1508f3ec2010-06-11 16:43:38 -0700898 break;
Joe Eykholt97c83892009-03-17 11:42:40 -0700899 }
900 desc = (struct fip_desc *)((char *)desc + dlen);
901 rlen -= dlen;
902 }
903
904 if (!fh)
905 goto drop;
906 els_op = *(u8 *)(fh + 1);
907
Chris Leech11b56182009-11-03 11:46:29 -0800908 if (els_dtype == FIP_DT_FLOGI && sub == FIP_SC_REP &&
909 fip->flogi_oxid == ntohs(fh->fh_ox_id) &&
910 els_op == ELS_LS_ACC && is_valid_ether_addr(granted_mac))
Joe Eykholt97c83892009-03-17 11:42:40 -0700911 fip->flogi_oxid = FC_XID_UNKNOWN;
Joe Eykholt97c83892009-03-17 11:42:40 -0700912
913 /*
914 * Convert skb into an fc_frame containing only the ELS.
915 */
916 skb_pull(skb, (u8 *)fh - skb->data);
917 skb_trim(skb, els_len);
918 fp = (struct fc_frame *)skb;
919 fc_frame_init(fp);
920 fr_sof(fp) = FC_SOF_I3;
921 fr_eof(fp) = FC_EOF_T;
Robert Love70b51aa2009-11-03 11:47:45 -0800922 fr_dev(fp) = lport;
Joe Eykholt97c83892009-03-17 11:42:40 -0700923
Joe Eykholtf018b732010-03-12 16:08:55 -0800924 stats = per_cpu_ptr(lport->dev_stats, get_cpu());
Joe Eykholt97c83892009-03-17 11:42:40 -0700925 stats->RxFrames++;
926 stats->RxWords += skb->len / FIP_BPW;
Joe Eykholtf018b732010-03-12 16:08:55 -0800927 put_cpu();
Joe Eykholt97c83892009-03-17 11:42:40 -0700928
Robert Love70b51aa2009-11-03 11:47:45 -0800929 fc_exch_recv(lport, fp);
Joe Eykholt97c83892009-03-17 11:42:40 -0700930 return;
931
932len_err:
Joe Eykholt0f51c2e2009-11-03 11:48:16 -0800933 LIBFCOE_FIP_DBG(fip, "FIP length error in descriptor type %x len %zu\n",
Robert Love650bd122009-06-10 15:31:05 -0700934 desc->fip_dtype, dlen);
Joe Eykholt97c83892009-03-17 11:42:40 -0700935drop:
936 kfree_skb(skb);
937}
938
939/**
Robert Love70b51aa2009-11-03 11:47:45 -0800940 * fcoe_ctlr_recv_els() - Handle an incoming link reset frame
941 * @fip: The FCoE controller that received the frame
942 * @fh: The received FIP header
Joe Eykholt97c83892009-03-17 11:42:40 -0700943 *
944 * There may be multiple VN_Port descriptors.
945 * The overall length has already been checked.
946 */
947static void fcoe_ctlr_recv_clr_vlink(struct fcoe_ctlr *fip,
Robert Love70b51aa2009-11-03 11:47:45 -0800948 struct fip_header *fh)
Joe Eykholt97c83892009-03-17 11:42:40 -0700949{
950 struct fip_desc *desc;
951 struct fip_mac_desc *mp;
952 struct fip_wwn_desc *wp;
953 struct fip_vn_desc *vp;
954 size_t rlen;
955 size_t dlen;
956 struct fcoe_fcf *fcf = fip->sel_fcf;
Robert Love70b51aa2009-11-03 11:47:45 -0800957 struct fc_lport *lport = fip->lp;
Joe Eykholt97c83892009-03-17 11:42:40 -0700958 u32 desc_mask;
959
Joe Eykholt0f51c2e2009-11-03 11:48:16 -0800960 LIBFCOE_FIP_DBG(fip, "Clear Virtual Link received\n");
Robert Loved29510a2010-05-07 15:18:30 -0700961
Robert Love7b2787e2010-05-07 15:18:41 -0700962 if (!fcf || !lport->port_id)
Joe Eykholt97c83892009-03-17 11:42:40 -0700963 return;
964
965 /*
966 * mask of required descriptors. Validating each one clears its bit.
967 */
968 desc_mask = BIT(FIP_DT_MAC) | BIT(FIP_DT_NAME) | BIT(FIP_DT_VN_ID);
969
970 rlen = ntohs(fh->fip_dl_len) * FIP_BPW;
971 desc = (struct fip_desc *)(fh + 1);
972 while (rlen >= sizeof(*desc)) {
973 dlen = desc->fip_dlen * FIP_BPW;
974 if (dlen > rlen)
975 return;
976 switch (desc->fip_dtype) {
977 case FIP_DT_MAC:
978 mp = (struct fip_mac_desc *)desc;
979 if (dlen < sizeof(*mp))
980 return;
981 if (compare_ether_addr(mp->fd_mac, fcf->fcf_mac))
982 return;
983 desc_mask &= ~BIT(FIP_DT_MAC);
984 break;
985 case FIP_DT_NAME:
986 wp = (struct fip_wwn_desc *)desc;
987 if (dlen < sizeof(*wp))
988 return;
989 if (get_unaligned_be64(&wp->fd_wwn) != fcf->switch_name)
990 return;
991 desc_mask &= ~BIT(FIP_DT_NAME);
992 break;
993 case FIP_DT_VN_ID:
994 vp = (struct fip_vn_desc *)desc;
995 if (dlen < sizeof(*vp))
996 return;
997 if (compare_ether_addr(vp->fd_mac,
Robert Love70b51aa2009-11-03 11:47:45 -0800998 fip->get_src_addr(lport)) == 0 &&
999 get_unaligned_be64(&vp->fd_wwpn) == lport->wwpn &&
Robert Love7b2787e2010-05-07 15:18:41 -07001000 ntoh24(vp->fd_fc_id) == lport->port_id)
Joe Eykholt97c83892009-03-17 11:42:40 -07001001 desc_mask &= ~BIT(FIP_DT_VN_ID);
1002 break;
1003 default:
1004 /* standard says ignore unknown descriptors >= 128 */
1005 if (desc->fip_dtype < FIP_DT_VENDOR_BASE)
1006 return;
1007 break;
1008 }
1009 desc = (struct fip_desc *)((char *)desc + dlen);
1010 rlen -= dlen;
1011 }
1012
1013 /*
1014 * reset only if all required descriptors were present and valid.
1015 */
1016 if (desc_mask) {
Joe Eykholt0f51c2e2009-11-03 11:48:16 -08001017 LIBFCOE_FIP_DBG(fip, "missing descriptors mask %x\n",
1018 desc_mask);
Joe Eykholt97c83892009-03-17 11:42:40 -07001019 } else {
Joe Eykholt0f51c2e2009-11-03 11:48:16 -08001020 LIBFCOE_FIP_DBG(fip, "performing Clear Virtual Link\n");
Joe Eykholtdd42dac2009-11-03 11:48:27 -08001021
1022 spin_lock_bh(&fip->lock);
Joe Eykholtf018b732010-03-12 16:08:55 -08001023 per_cpu_ptr(lport->dev_stats,
1024 smp_processor_id())->VLinkFailureCount++;
Joe Eykholtdd42dac2009-11-03 11:48:27 -08001025 fcoe_ctlr_reset(fip);
1026 spin_unlock_bh(&fip->lock);
1027
1028 fc_lport_reset(fip->lp);
1029 fcoe_ctlr_solicit(fip, NULL);
Joe Eykholt97c83892009-03-17 11:42:40 -07001030 }
1031}
1032
1033/**
Robert Love70b51aa2009-11-03 11:47:45 -08001034 * fcoe_ctlr_recv() - Receive a FIP packet
1035 * @fip: The FCoE controller that received the packet
1036 * @skb: The received FIP packet
Joe Eykholt97c83892009-03-17 11:42:40 -07001037 *
Joe Eykholt1f4aed82009-11-03 11:48:22 -08001038 * This may be called from either NET_RX_SOFTIRQ or IRQ.
Joe Eykholt97c83892009-03-17 11:42:40 -07001039 */
1040void fcoe_ctlr_recv(struct fcoe_ctlr *fip, struct sk_buff *skb)
1041{
Joe Eykholt1f4aed82009-11-03 11:48:22 -08001042 skb_queue_tail(&fip->fip_recv_list, skb);
Joe Eykholt97c83892009-03-17 11:42:40 -07001043 schedule_work(&fip->recv_work);
1044}
1045EXPORT_SYMBOL(fcoe_ctlr_recv);
1046
1047/**
Robert Love70b51aa2009-11-03 11:47:45 -08001048 * fcoe_ctlr_recv_handler() - Receive a FIP frame
1049 * @fip: The FCoE controller that received the frame
1050 * @skb: The received FIP frame
Joe Eykholt97c83892009-03-17 11:42:40 -07001051 *
1052 * Returns non-zero if the frame is dropped.
1053 */
1054static int fcoe_ctlr_recv_handler(struct fcoe_ctlr *fip, struct sk_buff *skb)
1055{
1056 struct fip_header *fiph;
1057 struct ethhdr *eh;
1058 enum fip_state state;
1059 u16 op;
1060 u8 sub;
1061
1062 if (skb_linearize(skb))
1063 goto drop;
1064 if (skb->len < sizeof(*fiph))
1065 goto drop;
1066 eh = eth_hdr(skb);
1067 if (compare_ether_addr(eh->h_dest, fip->ctl_src_addr) &&
1068 compare_ether_addr(eh->h_dest, FIP_ALL_ENODE_MACS))
1069 goto drop;
1070 fiph = (struct fip_header *)skb->data;
1071 op = ntohs(fiph->fip_op);
1072 sub = fiph->fip_subcode;
1073
Joe Eykholt97c83892009-03-17 11:42:40 -07001074 if (FIP_VER_DECAPS(fiph->fip_ver) != FIP_VER)
1075 goto drop;
1076 if (ntohs(fiph->fip_dl_len) * FIP_BPW + sizeof(*fiph) > skb->len)
1077 goto drop;
1078
1079 spin_lock_bh(&fip->lock);
1080 state = fip->state;
1081 if (state == FIP_ST_AUTO) {
1082 fip->map_dest = 0;
1083 fip->state = FIP_ST_ENABLED;
1084 state = FIP_ST_ENABLED;
Joe Eykholt0f51c2e2009-11-03 11:48:16 -08001085 LIBFCOE_FIP_DBG(fip, "Using FIP mode\n");
Joe Eykholt97c83892009-03-17 11:42:40 -07001086 }
1087 spin_unlock_bh(&fip->lock);
1088 if (state != FIP_ST_ENABLED)
1089 goto drop;
1090
1091 if (op == FIP_OP_LS) {
1092 fcoe_ctlr_recv_els(fip, skb); /* consumes skb */
1093 return 0;
1094 }
1095 if (op == FIP_OP_DISC && sub == FIP_SC_ADV)
1096 fcoe_ctlr_recv_adv(fip, skb);
1097 else if (op == FIP_OP_CTRL && sub == FIP_SC_CLR_VLINK)
1098 fcoe_ctlr_recv_clr_vlink(fip, fiph);
1099 kfree_skb(skb);
1100 return 0;
1101drop:
1102 kfree_skb(skb);
1103 return -1;
1104}
1105
1106/**
Robert Love70b51aa2009-11-03 11:47:45 -08001107 * fcoe_ctlr_select() - Select the best FCF (if possible)
1108 * @fip: The FCoE controller
Joe Eykholt97c83892009-03-17 11:42:40 -07001109 *
1110 * If there are conflicting advertisements, no FCF can be chosen.
1111 *
1112 * Called with lock held.
1113 */
1114static void fcoe_ctlr_select(struct fcoe_ctlr *fip)
1115{
1116 struct fcoe_fcf *fcf;
1117 struct fcoe_fcf *best = NULL;
1118
1119 list_for_each_entry(fcf, &fip->fcfs, list) {
Chris Leech9f8f3aa2010-04-09 14:23:16 -07001120 LIBFCOE_FIP_DBG(fip, "consider FCF for fab %16.16llx "
1121 "VFID %d map %x val %d\n",
1122 fcf->fabric_name, fcf->vfid,
Robert Love650bd122009-06-10 15:31:05 -07001123 fcf->fc_map, fcoe_ctlr_mtu_valid(fcf));
Joe Eykholt97c83892009-03-17 11:42:40 -07001124 if (!fcoe_ctlr_fcf_usable(fcf)) {
Chris Leech9f8f3aa2010-04-09 14:23:16 -07001125 LIBFCOE_FIP_DBG(fip, "FCF for fab %16.16llx "
1126 "map %x %svalid %savailable\n",
1127 fcf->fabric_name, fcf->fc_map,
1128 (fcf->flags & FIP_FL_SOL) ? "" : "in",
1129 (fcf->flags & FIP_FL_AVAIL) ?
1130 "" : "un");
Joe Eykholt97c83892009-03-17 11:42:40 -07001131 continue;
1132 }
1133 if (!best) {
1134 best = fcf;
1135 continue;
1136 }
1137 if (fcf->fabric_name != best->fabric_name ||
1138 fcf->vfid != best->vfid ||
1139 fcf->fc_map != best->fc_map) {
Joe Eykholt0f51c2e2009-11-03 11:48:16 -08001140 LIBFCOE_FIP_DBG(fip, "Conflicting fabric, VFID, "
Robert Love650bd122009-06-10 15:31:05 -07001141 "or FC-MAP\n");
Joe Eykholt97c83892009-03-17 11:42:40 -07001142 return;
1143 }
1144 if (fcf->pri < best->pri)
1145 best = fcf;
1146 }
1147 fip->sel_fcf = best;
1148}
1149
1150/**
Robert Love70b51aa2009-11-03 11:47:45 -08001151 * fcoe_ctlr_timeout() - FIP timeout handler
1152 * @arg: The FCoE controller that timed out
Joe Eykholt97c83892009-03-17 11:42:40 -07001153 *
1154 * Ages FCFs. Triggers FCF selection if possible. Sends keep-alives.
1155 */
1156static void fcoe_ctlr_timeout(unsigned long arg)
1157{
1158 struct fcoe_ctlr *fip = (struct fcoe_ctlr *)arg;
1159 struct fcoe_fcf *sel;
1160 struct fcoe_fcf *fcf;
Joe Eykholt8690cb82010-06-11 16:44:10 -07001161 unsigned long next_timer;
Joe Eykholt97c83892009-03-17 11:42:40 -07001162
1163 spin_lock_bh(&fip->lock);
1164 if (fip->state == FIP_ST_DISABLED) {
1165 spin_unlock_bh(&fip->lock);
1166 return;
1167 }
1168
1169 fcf = fip->sel_fcf;
Joe Eykholt8690cb82010-06-11 16:44:10 -07001170 next_timer = fcoe_ctlr_age_fcfs(fip);
Joe Eykholt97c83892009-03-17 11:42:40 -07001171
1172 sel = fip->sel_fcf;
Joe Eykholt8690cb82010-06-11 16:44:10 -07001173 if (!sel && fip->sel_time) {
1174 if (time_after_eq(jiffies, fip->sel_time)) {
1175 fcoe_ctlr_select(fip);
1176 sel = fip->sel_fcf;
1177 fip->sel_time = 0;
1178 } else if (time_after(next_timer, fip->sel_time))
1179 next_timer = fip->sel_time;
Joe Eykholt97c83892009-03-17 11:42:40 -07001180 }
1181
1182 if (sel != fcf) {
1183 fcf = sel; /* the old FCF may have been freed */
1184 if (sel) {
Robert Love650bd122009-06-10 15:31:05 -07001185 printk(KERN_INFO "libfcoe: host%d: FIP selected "
Johannes Berg66d6fae2009-07-15 17:21:14 +02001186 "Fibre-Channel Forwarder MAC %pM\n",
1187 fip->lp->host->host_no, sel->fcf_mac);
Joe Eykholt97c83892009-03-17 11:42:40 -07001188 memcpy(fip->dest_addr, sel->fcf_mac, ETH_ALEN);
1189 fip->port_ka_time = jiffies +
Robert Love70b51aa2009-11-03 11:47:45 -08001190 msecs_to_jiffies(FIP_VN_KA_PERIOD);
Joe Eykholt97c83892009-03-17 11:42:40 -07001191 fip->ctlr_ka_time = jiffies + sel->fka_period;
Joe Eykholtd99ee452010-06-11 16:44:15 -07001192 if (time_after(next_timer, fip->ctlr_ka_time))
1193 next_timer = fip->ctlr_ka_time;
Joe Eykholt97c83892009-03-17 11:42:40 -07001194 } else {
Robert Love650bd122009-06-10 15:31:05 -07001195 printk(KERN_NOTICE "libfcoe: host%d: "
Robert Love70b51aa2009-11-03 11:47:45 -08001196 "FIP Fibre-Channel Forwarder timed out. "
Joe Eykholt97c83892009-03-17 11:42:40 -07001197 "Starting FCF discovery.\n",
1198 fip->lp->host->host_no);
Joe Eykholtdd42dac2009-11-03 11:48:27 -08001199 fip->reset_req = 1;
Joe Eykholt42913652010-03-12 16:08:23 -08001200 schedule_work(&fip->timer_work);
Joe Eykholt97c83892009-03-17 11:42:40 -07001201 }
Joe Eykholt97c83892009-03-17 11:42:40 -07001202 }
1203
Yi Zou8cdffdc2009-11-20 14:54:57 -08001204 if (sel && !sel->fd_flags) {
Joe Eykholt97c83892009-03-17 11:42:40 -07001205 if (time_after_eq(jiffies, fip->ctlr_ka_time)) {
1206 fip->ctlr_ka_time = jiffies + sel->fka_period;
Chris Leech11b56182009-11-03 11:46:29 -08001207 fip->send_ctlr_ka = 1;
Joe Eykholt97c83892009-03-17 11:42:40 -07001208 }
1209 if (time_after(next_timer, fip->ctlr_ka_time))
1210 next_timer = fip->ctlr_ka_time;
1211
1212 if (time_after_eq(jiffies, fip->port_ka_time)) {
Bhanu Prakash Gollapudif47dd852010-01-21 10:16:00 -08001213 fip->port_ka_time = jiffies +
Robert Love70b51aa2009-11-03 11:47:45 -08001214 msecs_to_jiffies(FIP_VN_KA_PERIOD);
Chris Leech11b56182009-11-03 11:46:29 -08001215 fip->send_port_ka = 1;
Joe Eykholt97c83892009-03-17 11:42:40 -07001216 }
1217 if (time_after(next_timer, fip->port_ka_time))
1218 next_timer = fip->port_ka_time;
Joe Eykholt97c83892009-03-17 11:42:40 -07001219 }
Joe Eykholt8690cb82010-06-11 16:44:10 -07001220 if (!list_empty(&fip->fcfs))
1221 mod_timer(&fip->timer, next_timer);
Chris Leech11b56182009-11-03 11:46:29 -08001222 if (fip->send_ctlr_ka || fip->send_port_ka)
Joe Eykholt42913652010-03-12 16:08:23 -08001223 schedule_work(&fip->timer_work);
Joe Eykholt97c83892009-03-17 11:42:40 -07001224 spin_unlock_bh(&fip->lock);
Joe Eykholt97c83892009-03-17 11:42:40 -07001225}
1226
1227/**
Joe Eykholt42913652010-03-12 16:08:23 -08001228 * fcoe_ctlr_timer_work() - Worker thread function for timer work
Robert Love70b51aa2009-11-03 11:47:45 -08001229 * @work: Handle to a FCoE controller
Joe Eykholt97c83892009-03-17 11:42:40 -07001230 *
Joe Eykholt42913652010-03-12 16:08:23 -08001231 * Sends keep-alives and resets which must not
Joe Eykholt97c83892009-03-17 11:42:40 -07001232 * be called from the timer directly, since they use a mutex.
1233 */
Joe Eykholt42913652010-03-12 16:08:23 -08001234static void fcoe_ctlr_timer_work(struct work_struct *work)
Joe Eykholt97c83892009-03-17 11:42:40 -07001235{
1236 struct fcoe_ctlr *fip;
Chris Leech11b56182009-11-03 11:46:29 -08001237 struct fc_lport *vport;
1238 u8 *mac;
Joe Eykholtdd42dac2009-11-03 11:48:27 -08001239 int reset;
Joe Eykholt97c83892009-03-17 11:42:40 -07001240
Joe Eykholt42913652010-03-12 16:08:23 -08001241 fip = container_of(work, struct fcoe_ctlr, timer_work);
Joe Eykholt97c83892009-03-17 11:42:40 -07001242 spin_lock_bh(&fip->lock);
Joe Eykholtdd42dac2009-11-03 11:48:27 -08001243 reset = fip->reset_req;
1244 fip->reset_req = 0;
Joe Eykholt97c83892009-03-17 11:42:40 -07001245 spin_unlock_bh(&fip->lock);
1246
Bhanu Prakash Gollapudi516a6482010-06-11 16:43:44 -07001247 if (reset) {
Joe Eykholtdd42dac2009-11-03 11:48:27 -08001248 fc_lport_reset(fip->lp);
Bhanu Prakash Gollapudi516a6482010-06-11 16:43:44 -07001249 /* restart things with a solicitation */
1250 fcoe_ctlr_solicit(fip, NULL);
1251 }
Chris Leech11b56182009-11-03 11:46:29 -08001252
1253 if (fip->send_ctlr_ka) {
1254 fip->send_ctlr_ka = 0;
1255 fcoe_ctlr_send_keep_alive(fip, NULL, 0, fip->ctl_src_addr);
1256 }
1257 if (fip->send_port_ka) {
1258 fip->send_port_ka = 0;
1259 mutex_lock(&fip->lp->lp_mutex);
1260 mac = fip->get_src_addr(fip->lp);
1261 fcoe_ctlr_send_keep_alive(fip, fip->lp, 1, mac);
1262 list_for_each_entry(vport, &fip->lp->vports, list) {
1263 mac = fip->get_src_addr(vport);
1264 fcoe_ctlr_send_keep_alive(fip, vport, 1, mac);
1265 }
1266 mutex_unlock(&fip->lp->lp_mutex);
1267 }
Joe Eykholt97c83892009-03-17 11:42:40 -07001268}
1269
1270/**
Robert Love70b51aa2009-11-03 11:47:45 -08001271 * fcoe_ctlr_recv_work() - Worker thread function for receiving FIP frames
1272 * @recv_work: Handle to a FCoE controller
Joe Eykholt97c83892009-03-17 11:42:40 -07001273 */
1274static void fcoe_ctlr_recv_work(struct work_struct *recv_work)
1275{
1276 struct fcoe_ctlr *fip;
1277 struct sk_buff *skb;
1278
1279 fip = container_of(recv_work, struct fcoe_ctlr, recv_work);
Joe Eykholt1f4aed82009-11-03 11:48:22 -08001280 while ((skb = skb_dequeue(&fip->fip_recv_list)))
Joe Eykholt97c83892009-03-17 11:42:40 -07001281 fcoe_ctlr_recv_handler(fip, skb);
Joe Eykholt97c83892009-03-17 11:42:40 -07001282}
1283
1284/**
Joe Eykholt386309ce2009-11-03 11:49:16 -08001285 * fcoe_ctlr_recv_flogi() - Snoop pre-FIP receipt of FLOGI response
Robert Love70b51aa2009-11-03 11:47:45 -08001286 * @fip: The FCoE controller
1287 * @fp: The FC frame to snoop
Joe Eykholt97c83892009-03-17 11:42:40 -07001288 *
1289 * Snoop potential response to FLOGI or even incoming FLOGI.
1290 *
1291 * The caller has checked that we are waiting for login as indicated
1292 * by fip->flogi_oxid != FC_XID_UNKNOWN.
1293 *
1294 * The caller is responsible for freeing the frame.
Joe Eykholt386309ce2009-11-03 11:49:16 -08001295 * Fill in the granted_mac address.
Joe Eykholt97c83892009-03-17 11:42:40 -07001296 *
1297 * Return non-zero if the frame should not be delivered to libfc.
1298 */
Chris Leech11b56182009-11-03 11:46:29 -08001299int fcoe_ctlr_recv_flogi(struct fcoe_ctlr *fip, struct fc_lport *lport,
Joe Eykholt386309ce2009-11-03 11:49:16 -08001300 struct fc_frame *fp)
Joe Eykholt97c83892009-03-17 11:42:40 -07001301{
1302 struct fc_frame_header *fh;
1303 u8 op;
Joe Eykholt386309ce2009-11-03 11:49:16 -08001304 u8 *sa;
Joe Eykholt97c83892009-03-17 11:42:40 -07001305
Joe Eykholt386309ce2009-11-03 11:49:16 -08001306 sa = eth_hdr(&fp->skb)->h_source;
Joe Eykholt97c83892009-03-17 11:42:40 -07001307 fh = fc_frame_header_get(fp);
1308 if (fh->fh_type != FC_TYPE_ELS)
1309 return 0;
1310
1311 op = fc_frame_payload_op(fp);
1312 if (op == ELS_LS_ACC && fh->fh_r_ctl == FC_RCTL_ELS_REP &&
1313 fip->flogi_oxid == ntohs(fh->fh_ox_id)) {
1314
1315 spin_lock_bh(&fip->lock);
1316 if (fip->state != FIP_ST_AUTO && fip->state != FIP_ST_NON_FIP) {
1317 spin_unlock_bh(&fip->lock);
1318 return -EINVAL;
1319 }
1320 fip->state = FIP_ST_NON_FIP;
Joe Eykholt0f51c2e2009-11-03 11:48:16 -08001321 LIBFCOE_FIP_DBG(fip,
1322 "received FLOGI LS_ACC using non-FIP mode\n");
Joe Eykholt97c83892009-03-17 11:42:40 -07001323
1324 /*
1325 * FLOGI accepted.
1326 * If the src mac addr is FC_OUI-based, then we mark the
1327 * address_mode flag to use FC_OUI-based Ethernet DA.
1328 * Otherwise we use the FCoE gateway addr
1329 */
1330 if (!compare_ether_addr(sa, (u8[6])FC_FCOE_FLOGI_MAC)) {
1331 fip->map_dest = 1;
1332 } else {
1333 memcpy(fip->dest_addr, sa, ETH_ALEN);
1334 fip->map_dest = 0;
1335 }
1336 fip->flogi_oxid = FC_XID_UNKNOWN;
Joe Eykholt97c83892009-03-17 11:42:40 -07001337 spin_unlock_bh(&fip->lock);
Joe Eykholt386309ce2009-11-03 11:49:16 -08001338 fc_fcoe_set_mac(fr_cb(fp)->granted_mac, fh->fh_d_id);
Joe Eykholt97c83892009-03-17 11:42:40 -07001339 } else if (op == ELS_FLOGI && fh->fh_r_ctl == FC_RCTL_ELS_REQ && sa) {
1340 /*
1341 * Save source MAC for point-to-point responses.
1342 */
1343 spin_lock_bh(&fip->lock);
1344 if (fip->state == FIP_ST_AUTO || fip->state == FIP_ST_NON_FIP) {
1345 memcpy(fip->dest_addr, sa, ETH_ALEN);
1346 fip->map_dest = 0;
Joe Eykholte49bf612010-03-12 16:07:57 -08001347 if (fip->state == FIP_ST_AUTO)
1348 LIBFCOE_FIP_DBG(fip, "received non-FIP FLOGI. "
1349 "Setting non-FIP mode\n");
Joe Eykholt97c83892009-03-17 11:42:40 -07001350 fip->state = FIP_ST_NON_FIP;
1351 }
1352 spin_unlock_bh(&fip->lock);
1353 }
1354 return 0;
1355}
1356EXPORT_SYMBOL(fcoe_ctlr_recv_flogi);
1357
Vasu Dev5e80f7f2009-03-17 11:42:18 -07001358/**
Robert Love70b51aa2009-11-03 11:47:45 -08001359 * fcoe_wwn_from_mac() - Converts a 48-bit IEEE MAC address to a 64-bit FC WWN
1360 * @mac: The MAC address to convert
1361 * @scheme: The scheme to use when converting
1362 * @port: The port indicator for converting
Vasu Dev5e80f7f2009-03-17 11:42:18 -07001363 *
1364 * Returns: u64 fc world wide name
1365 */
1366u64 fcoe_wwn_from_mac(unsigned char mac[MAX_ADDR_LEN],
1367 unsigned int scheme, unsigned int port)
1368{
1369 u64 wwn;
1370 u64 host_mac;
1371
1372 /* The MAC is in NO, so flip only the low 48 bits */
1373 host_mac = ((u64) mac[0] << 40) |
1374 ((u64) mac[1] << 32) |
1375 ((u64) mac[2] << 24) |
1376 ((u64) mac[3] << 16) |
1377 ((u64) mac[4] << 8) |
1378 (u64) mac[5];
1379
1380 WARN_ON(host_mac >= (1ULL << 48));
1381 wwn = host_mac | ((u64) scheme << 60);
1382 switch (scheme) {
1383 case 1:
1384 WARN_ON(port != 0);
1385 break;
1386 case 2:
1387 WARN_ON(port >= 0xfff);
1388 wwn |= (u64) port << 48;
1389 break;
1390 default:
1391 WARN_ON(1);
1392 break;
1393 }
1394
1395 return wwn;
1396}
1397EXPORT_SYMBOL_GPL(fcoe_wwn_from_mac);
1398
1399/**
Robert Love70b51aa2009-11-03 11:47:45 -08001400 * fcoe_libfc_config() - Sets up libfc related properties for local port
1401 * @lp: The local port to configure libfc for
1402 * @tt: The libfc function template
Vasu Dev5e80f7f2009-03-17 11:42:18 -07001403 *
1404 * Returns : 0 for success
1405 */
Robert Love70b51aa2009-11-03 11:47:45 -08001406int fcoe_libfc_config(struct fc_lport *lport,
1407 struct libfc_function_template *tt)
Vasu Dev5e80f7f2009-03-17 11:42:18 -07001408{
1409 /* Set the function pointers set by the LLDD */
Robert Love70b51aa2009-11-03 11:47:45 -08001410 memcpy(&lport->tt, tt, sizeof(*tt));
1411 if (fc_fcp_init(lport))
Vasu Dev5e80f7f2009-03-17 11:42:18 -07001412 return -ENOMEM;
Robert Love70b51aa2009-11-03 11:47:45 -08001413 fc_exch_init(lport);
1414 fc_elsct_init(lport);
1415 fc_lport_init(lport);
1416 fc_rport_init(lport);
1417 fc_disc_init(lport);
Vasu Dev5e80f7f2009-03-17 11:42:18 -07001418
1419 return 0;
1420}
1421EXPORT_SYMBOL_GPL(fcoe_libfc_config);
Chris Leech11b56182009-11-03 11:46:29 -08001422