blob: d8ea04a29199830ed37563f54bbf0f6ed7bf52bc [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>
34#include <net/rtnetlink.h>
35
36#include <scsi/fc/fc_els.h>
37#include <scsi/fc/fc_fs.h>
38#include <scsi/fc/fc_fip.h>
39#include <scsi/fc/fc_encaps.h>
40#include <scsi/fc/fc_fcoe.h>
Vasu Dev5e80f7f2009-03-17 11:42:18 -070041
42#include <scsi/libfc.h>
Joe Eykholt97c83892009-03-17 11:42:40 -070043#include <scsi/libfcoe.h>
Vasu Dev9b34ecf2009-03-17 11:42:13 -070044
45MODULE_AUTHOR("Open-FCoE.org");
Joe Eykholt97c83892009-03-17 11:42:40 -070046MODULE_DESCRIPTION("FIP discovery protocol support for FCoE HBAs");
Vasu Dev9b34ecf2009-03-17 11:42:13 -070047MODULE_LICENSE("GPL v2");
Vasu Dev5e80f7f2009-03-17 11:42:18 -070048
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);
53static void fcoe_ctlr_link_work(struct work_struct *);
54static void fcoe_ctlr_recv_work(struct work_struct *);
55
56static u8 fcoe_all_fcfs[ETH_ALEN] = FIP_ALL_FCF_MACS;
57
Robert Love650bd122009-06-10 15:31:05 -070058unsigned int libfcoe_debug_logging;
59module_param_named(debug_logging, libfcoe_debug_logging, int, S_IRUGO|S_IWUSR);
60MODULE_PARM_DESC(debug_logging, "a bit mask of logging levels");
Joe Eykholt97c83892009-03-17 11:42:40 -070061
Robert Love650bd122009-06-10 15:31:05 -070062#define LIBFCOE_LOGGING 0x01 /* General logging, not categorized */
63#define LIBFCOE_FIP_LOGGING 0x02 /* FIP logging */
64
65#define LIBFCOE_CHECK_LOGGING(LEVEL, CMD) \
66do { \
67 if (unlikely(libfcoe_debug_logging & LEVEL)) \
Joe Eykholt97c83892009-03-17 11:42:40 -070068 do { \
Robert Love650bd122009-06-10 15:31:05 -070069 CMD; \
70 } while (0); \
Joe Eykholta69b06b2009-08-25 13:58:42 -070071} while (0)
Joe Eykholt97c83892009-03-17 11:42:40 -070072
Robert Love650bd122009-06-10 15:31:05 -070073#define LIBFCOE_DBG(fmt, args...) \
74 LIBFCOE_CHECK_LOGGING(LIBFCOE_LOGGING, \
75 printk(KERN_INFO "libfcoe: " fmt, ##args);)
76
77#define LIBFCOE_FIP_DBG(fmt, args...) \
78 LIBFCOE_CHECK_LOGGING(LIBFCOE_FIP_LOGGING, \
79 printk(KERN_INFO "fip: " fmt, ##args);)
Joe Eykholt97c83892009-03-17 11:42:40 -070080
81/*
82 * Return non-zero if FCF fcoe_size has been validated.
83 */
84static inline int fcoe_ctlr_mtu_valid(const struct fcoe_fcf *fcf)
85{
86 return (fcf->flags & FIP_FL_SOL) != 0;
87}
88
89/*
90 * Return non-zero if the FCF is usable.
91 */
92static inline int fcoe_ctlr_fcf_usable(struct fcoe_fcf *fcf)
93{
94 u16 flags = FIP_FL_SOL | FIP_FL_AVAIL;
95
96 return (fcf->flags & flags) == flags;
97}
98
99/**
100 * fcoe_ctlr_init() - Initialize the FCoE Controller instance.
101 * @fip: FCoE controller.
102 */
103void fcoe_ctlr_init(struct fcoe_ctlr *fip)
104{
105 fip->state = FIP_ST_LINK_WAIT;
106 INIT_LIST_HEAD(&fip->fcfs);
107 spin_lock_init(&fip->lock);
108 fip->flogi_oxid = FC_XID_UNKNOWN;
109 setup_timer(&fip->timer, fcoe_ctlr_timeout, (unsigned long)fip);
110 INIT_WORK(&fip->link_work, fcoe_ctlr_link_work);
111 INIT_WORK(&fip->recv_work, fcoe_ctlr_recv_work);
112 skb_queue_head_init(&fip->fip_recv_list);
113}
114EXPORT_SYMBOL(fcoe_ctlr_init);
115
116/**
117 * fcoe_ctlr_reset_fcfs() - Reset and free all FCFs for a controller.
118 * @fip: FCoE controller.
119 *
120 * Called with &fcoe_ctlr lock held.
121 */
122static void fcoe_ctlr_reset_fcfs(struct fcoe_ctlr *fip)
123{
124 struct fcoe_fcf *fcf;
125 struct fcoe_fcf *next;
126
127 fip->sel_fcf = NULL;
128 list_for_each_entry_safe(fcf, next, &fip->fcfs, list) {
129 list_del(&fcf->list);
130 kfree(fcf);
131 }
132 fip->fcf_count = 0;
133 fip->sel_time = 0;
134}
135
136/**
Chris Leechdd3fd722009-04-21 16:27:36 -0700137 * fcoe_ctlr_destroy() - Disable and tear-down the FCoE controller.
Joe Eykholt97c83892009-03-17 11:42:40 -0700138 * @fip: FCoE controller.
139 *
140 * This is called by FCoE drivers before freeing the &fcoe_ctlr.
141 *
142 * The receive handler will have been deleted before this to guarantee
143 * that no more recv_work will be scheduled.
144 *
145 * The timer routine will simply return once we set FIP_ST_DISABLED.
146 * This guarantees that no further timeouts or work will be scheduled.
147 */
148void fcoe_ctlr_destroy(struct fcoe_ctlr *fip)
149{
Chris Leecha4b7cfa2009-08-25 13:59:08 -0700150 cancel_work_sync(&fip->recv_work);
151 spin_lock_bh(&fip->fip_recv_list.lock);
152 __skb_queue_purge(&fip->fip_recv_list);
153 spin_unlock_bh(&fip->fip_recv_list.lock);
154
Joe Eykholt97c83892009-03-17 11:42:40 -0700155 spin_lock_bh(&fip->lock);
156 fip->state = FIP_ST_DISABLED;
157 fcoe_ctlr_reset_fcfs(fip);
158 spin_unlock_bh(&fip->lock);
159 del_timer_sync(&fip->timer);
Chris Leecha4b7cfa2009-08-25 13:59:08 -0700160 cancel_work_sync(&fip->link_work);
Joe Eykholt97c83892009-03-17 11:42:40 -0700161}
162EXPORT_SYMBOL(fcoe_ctlr_destroy);
163
164/**
165 * fcoe_ctlr_fcoe_size() - Return the maximum FCoE size required for VN_Port.
166 * @fip: FCoE controller.
167 *
168 * Returns the maximum packet size including the FCoE header and trailer,
169 * but not including any Ethernet or VLAN headers.
170 */
171static inline u32 fcoe_ctlr_fcoe_size(struct fcoe_ctlr *fip)
172{
173 /*
174 * Determine the max FCoE frame size allowed, including
175 * FCoE header and trailer.
176 * Note: lp->mfs is currently the payload size, not the frame size.
177 */
178 return fip->lp->mfs + sizeof(struct fc_frame_header) +
179 sizeof(struct fcoe_hdr) + sizeof(struct fcoe_crc_eof);
180}
181
182/**
183 * fcoe_ctlr_solicit() - Send a solicitation.
184 * @fip: FCoE controller.
185 * @fcf: Destination FCF. If NULL, a multicast solicitation is sent.
186 */
187static void fcoe_ctlr_solicit(struct fcoe_ctlr *fip, struct fcoe_fcf *fcf)
188{
189 struct sk_buff *skb;
190 struct fip_sol {
191 struct ethhdr eth;
192 struct fip_header fip;
193 struct {
194 struct fip_mac_desc mac;
195 struct fip_wwn_desc wwnn;
196 struct fip_size_desc size;
197 } __attribute__((packed)) desc;
198 } __attribute__((packed)) *sol;
199 u32 fcoe_size;
200
201 skb = dev_alloc_skb(sizeof(*sol));
202 if (!skb)
203 return;
204
205 sol = (struct fip_sol *)skb->data;
206
207 memset(sol, 0, sizeof(*sol));
208 memcpy(sol->eth.h_dest, fcf ? fcf->fcf_mac : fcoe_all_fcfs, ETH_ALEN);
209 memcpy(sol->eth.h_source, fip->ctl_src_addr, ETH_ALEN);
210 sol->eth.h_proto = htons(ETH_P_FIP);
211
212 sol->fip.fip_ver = FIP_VER_ENCAPS(FIP_VER);
213 sol->fip.fip_op = htons(FIP_OP_DISC);
214 sol->fip.fip_subcode = FIP_SC_SOL;
215 sol->fip.fip_dl_len = htons(sizeof(sol->desc) / FIP_BPW);
216 sol->fip.fip_flags = htons(FIP_FL_FPMA);
Vasu Dev184dd342009-05-17 12:33:28 +0000217 if (fip->spma)
218 sol->fip.fip_flags |= htons(FIP_FL_SPMA);
Joe Eykholt97c83892009-03-17 11:42:40 -0700219
220 sol->desc.mac.fd_desc.fip_dtype = FIP_DT_MAC;
221 sol->desc.mac.fd_desc.fip_dlen = sizeof(sol->desc.mac) / FIP_BPW;
222 memcpy(sol->desc.mac.fd_mac, fip->ctl_src_addr, ETH_ALEN);
223
224 sol->desc.wwnn.fd_desc.fip_dtype = FIP_DT_NAME;
225 sol->desc.wwnn.fd_desc.fip_dlen = sizeof(sol->desc.wwnn) / FIP_BPW;
226 put_unaligned_be64(fip->lp->wwnn, &sol->desc.wwnn.fd_wwn);
227
228 fcoe_size = fcoe_ctlr_fcoe_size(fip);
229 sol->desc.size.fd_desc.fip_dtype = FIP_DT_FCOE_SIZE;
230 sol->desc.size.fd_desc.fip_dlen = sizeof(sol->desc.size) / FIP_BPW;
231 sol->desc.size.fd_size = htons(fcoe_size);
232
233 skb_put(skb, sizeof(*sol));
Chris Leech0f491532009-05-06 10:52:18 -0700234 skb->protocol = htons(ETH_P_FIP);
Joe Eykholt97c83892009-03-17 11:42:40 -0700235 skb_reset_mac_header(skb);
236 skb_reset_network_header(skb);
237 fip->send(fip, skb);
238
239 if (!fcf)
240 fip->sol_time = jiffies;
241}
242
243/**
244 * fcoe_ctlr_link_up() - Start FCoE controller.
245 * @fip: FCoE controller.
246 *
247 * Called from the LLD when the network link is ready.
248 */
249void fcoe_ctlr_link_up(struct fcoe_ctlr *fip)
250{
251 spin_lock_bh(&fip->lock);
252 if (fip->state == FIP_ST_NON_FIP || fip->state == FIP_ST_AUTO) {
253 fip->last_link = 1;
254 fip->link = 1;
255 spin_unlock_bh(&fip->lock);
256 fc_linkup(fip->lp);
257 } else if (fip->state == FIP_ST_LINK_WAIT) {
258 fip->state = FIP_ST_AUTO;
259 fip->last_link = 1;
260 fip->link = 1;
261 spin_unlock_bh(&fip->lock);
Robert Love650bd122009-06-10 15:31:05 -0700262 LIBFCOE_FIP_DBG("%s", "setting AUTO mode.\n");
Joe Eykholt97c83892009-03-17 11:42:40 -0700263 fc_linkup(fip->lp);
264 fcoe_ctlr_solicit(fip, NULL);
265 } else
266 spin_unlock_bh(&fip->lock);
267}
268EXPORT_SYMBOL(fcoe_ctlr_link_up);
269
270/**
271 * fcoe_ctlr_reset() - Reset FIP.
272 * @fip: FCoE controller.
273 * @new_state: FIP state to be entered.
274 *
275 * Returns non-zero if the link was up and now isn't.
276 */
277static int fcoe_ctlr_reset(struct fcoe_ctlr *fip, enum fip_state new_state)
278{
279 struct fc_lport *lp = fip->lp;
280 int link_dropped;
281
282 spin_lock_bh(&fip->lock);
283 fcoe_ctlr_reset_fcfs(fip);
284 del_timer(&fip->timer);
285 fip->state = new_state;
286 fip->ctlr_ka_time = 0;
287 fip->port_ka_time = 0;
288 fip->sol_time = 0;
289 fip->flogi_oxid = FC_XID_UNKNOWN;
290 fip->map_dest = 0;
291 fip->last_link = 0;
292 link_dropped = fip->link;
293 fip->link = 0;
294 spin_unlock_bh(&fip->lock);
295
296 if (link_dropped)
297 fc_linkdown(lp);
298
299 if (new_state == FIP_ST_ENABLED) {
300 fcoe_ctlr_solicit(fip, NULL);
301 fc_linkup(lp);
302 link_dropped = 0;
303 }
304 return link_dropped;
305}
306
307/**
308 * fcoe_ctlr_link_down() - Stop FCoE controller.
309 * @fip: FCoE controller.
310 *
311 * Returns non-zero if the link was up and now isn't.
312 *
313 * Called from the LLD when the network link is not ready.
314 * There may be multiple calls while the link is down.
315 */
316int fcoe_ctlr_link_down(struct fcoe_ctlr *fip)
317{
318 return fcoe_ctlr_reset(fip, FIP_ST_LINK_WAIT);
319}
320EXPORT_SYMBOL(fcoe_ctlr_link_down);
321
322/**
323 * fcoe_ctlr_send_keep_alive() - Send a keep-alive to the selected FCF.
324 * @fip: FCoE controller.
325 * @ports: 0 for controller keep-alive, 1 for port keep-alive.
326 * @sa: source MAC address.
327 *
328 * A controller keep-alive is sent every fka_period (typically 8 seconds).
329 * The source MAC is the native MAC address.
330 *
331 * A port keep-alive is sent every 90 seconds while logged in.
332 * The source MAC is the assigned mapped source address.
333 * The destination is the FCF's F-port.
334 */
335static void fcoe_ctlr_send_keep_alive(struct fcoe_ctlr *fip, int ports, u8 *sa)
336{
337 struct sk_buff *skb;
338 struct fip_kal {
339 struct ethhdr eth;
340 struct fip_header fip;
341 struct fip_mac_desc mac;
342 } __attribute__((packed)) *kal;
343 struct fip_vn_desc *vn;
344 u32 len;
345 struct fc_lport *lp;
346 struct fcoe_fcf *fcf;
347
348 fcf = fip->sel_fcf;
349 lp = fip->lp;
350 if (!fcf || !fc_host_port_id(lp->host))
351 return;
352
353 len = fcoe_ctlr_fcoe_size(fip) + sizeof(struct ethhdr);
354 BUG_ON(len < sizeof(*kal) + sizeof(*vn));
355 skb = dev_alloc_skb(len);
356 if (!skb)
357 return;
358
359 kal = (struct fip_kal *)skb->data;
360 memset(kal, 0, len);
361 memcpy(kal->eth.h_dest, fcf->fcf_mac, ETH_ALEN);
362 memcpy(kal->eth.h_source, sa, ETH_ALEN);
363 kal->eth.h_proto = htons(ETH_P_FIP);
364
365 kal->fip.fip_ver = FIP_VER_ENCAPS(FIP_VER);
366 kal->fip.fip_op = htons(FIP_OP_CTRL);
367 kal->fip.fip_subcode = FIP_SC_KEEP_ALIVE;
368 kal->fip.fip_dl_len = htons((sizeof(kal->mac) +
369 ports * sizeof(*vn)) / FIP_BPW);
370 kal->fip.fip_flags = htons(FIP_FL_FPMA);
Vasu Dev184dd342009-05-17 12:33:28 +0000371 if (fip->spma)
372 kal->fip.fip_flags |= htons(FIP_FL_SPMA);
Joe Eykholt97c83892009-03-17 11:42:40 -0700373
374 kal->mac.fd_desc.fip_dtype = FIP_DT_MAC;
375 kal->mac.fd_desc.fip_dlen = sizeof(kal->mac) / FIP_BPW;
376 memcpy(kal->mac.fd_mac, fip->ctl_src_addr, ETH_ALEN);
377
378 if (ports) {
379 vn = (struct fip_vn_desc *)(kal + 1);
380 vn->fd_desc.fip_dtype = FIP_DT_VN_ID;
381 vn->fd_desc.fip_dlen = sizeof(*vn) / FIP_BPW;
382 memcpy(vn->fd_mac, fip->data_src_addr, ETH_ALEN);
383 hton24(vn->fd_fc_id, fc_host_port_id(lp->host));
384 put_unaligned_be64(lp->wwpn, &vn->fd_wwpn);
385 }
386
387 skb_put(skb, len);
Chris Leech0f491532009-05-06 10:52:18 -0700388 skb->protocol = htons(ETH_P_FIP);
Joe Eykholt97c83892009-03-17 11:42:40 -0700389 skb_reset_mac_header(skb);
390 skb_reset_network_header(skb);
391 fip->send(fip, skb);
392}
393
394/**
395 * fcoe_ctlr_encaps() - Encapsulate an ELS frame for FIP, without sending it.
396 * @fip: FCoE controller.
397 * @dtype: FIP descriptor type for the frame.
398 * @skb: FCoE ELS frame including FC header but no FCoE headers.
399 *
400 * Returns non-zero error code on failure.
401 *
402 * The caller must check that the length is a multiple of 4.
403 *
404 * The @skb must have enough headroom (28 bytes) and tailroom (8 bytes).
405 * Headroom includes the FIP encapsulation description, FIP header, and
406 * Ethernet header. The tailroom is for the FIP MAC descriptor.
407 */
408static int fcoe_ctlr_encaps(struct fcoe_ctlr *fip,
409 u8 dtype, struct sk_buff *skb)
410{
411 struct fip_encaps_head {
412 struct ethhdr eth;
413 struct fip_header fip;
414 struct fip_encaps encaps;
415 } __attribute__((packed)) *cap;
416 struct fip_mac_desc *mac;
417 struct fcoe_fcf *fcf;
418 size_t dlen;
Yi Zou5a84bae2009-07-29 17:03:55 -0700419 u16 fip_flags;
Joe Eykholt97c83892009-03-17 11:42:40 -0700420
421 fcf = fip->sel_fcf;
422 if (!fcf)
423 return -ENODEV;
Yi Zou5a84bae2009-07-29 17:03:55 -0700424
425 /* set flags according to both FCF and lport's capability on SPMA */
426 fip_flags = fcf->flags;
427 fip_flags &= fip->spma ? FIP_FL_SPMA | FIP_FL_FPMA : FIP_FL_FPMA;
428 if (!fip_flags)
429 return -ENODEV;
430
Joe Eykholt97c83892009-03-17 11:42:40 -0700431 dlen = sizeof(struct fip_encaps) + skb->len; /* len before push */
432 cap = (struct fip_encaps_head *)skb_push(skb, sizeof(*cap));
433
434 memset(cap, 0, sizeof(*cap));
435 memcpy(cap->eth.h_dest, fcf->fcf_mac, ETH_ALEN);
436 memcpy(cap->eth.h_source, fip->ctl_src_addr, ETH_ALEN);
437 cap->eth.h_proto = htons(ETH_P_FIP);
438
439 cap->fip.fip_ver = FIP_VER_ENCAPS(FIP_VER);
440 cap->fip.fip_op = htons(FIP_OP_LS);
441 cap->fip.fip_subcode = FIP_SC_REQ;
442 cap->fip.fip_dl_len = htons((dlen + sizeof(*mac)) / FIP_BPW);
Yi Zou5a84bae2009-07-29 17:03:55 -0700443 cap->fip.fip_flags = htons(fip_flags);
Joe Eykholt97c83892009-03-17 11:42:40 -0700444
445 cap->encaps.fd_desc.fip_dtype = dtype;
446 cap->encaps.fd_desc.fip_dlen = dlen / FIP_BPW;
447
448 mac = (struct fip_mac_desc *)skb_put(skb, sizeof(*mac));
449 memset(mac, 0, sizeof(mac));
450 mac->fd_desc.fip_dtype = FIP_DT_MAC;
451 mac->fd_desc.fip_dlen = sizeof(*mac) / FIP_BPW;
Chris Leechdb36c062009-11-03 11:46:24 -0800452 if (dtype != FIP_DT_FLOGI && dtype != FIP_DT_FDISC)
Joe Eykholt97c83892009-03-17 11:42:40 -0700453 memcpy(mac->fd_mac, fip->data_src_addr, ETH_ALEN);
Vasu Dev184dd342009-05-17 12:33:28 +0000454 else if (fip->spma)
455 memcpy(mac->fd_mac, fip->ctl_src_addr, ETH_ALEN);
Joe Eykholt97c83892009-03-17 11:42:40 -0700456
Chris Leech0f491532009-05-06 10:52:18 -0700457 skb->protocol = htons(ETH_P_FIP);
Joe Eykholt97c83892009-03-17 11:42:40 -0700458 skb_reset_mac_header(skb);
459 skb_reset_network_header(skb);
460 return 0;
461}
462
463/**
464 * fcoe_ctlr_els_send() - Send an ELS frame encapsulated by FIP if appropriate.
465 * @fip: FCoE controller.
466 * @skb: FCoE ELS frame including FC header but no FCoE headers.
467 *
468 * Returns a non-zero error code if the frame should not be sent.
469 * Returns zero if the caller should send the frame with FCoE encapsulation.
470 *
471 * The caller must check that the length is a multiple of 4.
472 * The SKB must have enough headroom (28 bytes) and tailroom (8 bytes).
473 */
474int fcoe_ctlr_els_send(struct fcoe_ctlr *fip, struct sk_buff *skb)
475{
476 struct fc_frame_header *fh;
477 u16 old_xid;
478 u8 op;
479
Joe Eykholt97c83892009-03-17 11:42:40 -0700480 fh = (struct fc_frame_header *)skb->data;
481 op = *(u8 *)(fh + 1);
482
Joe Eykholt5f48f702009-05-06 10:52:12 -0700483 if (op == ELS_FLOGI) {
Joe Eykholt97c83892009-03-17 11:42:40 -0700484 old_xid = fip->flogi_oxid;
485 fip->flogi_oxid = ntohs(fh->fh_ox_id);
486 if (fip->state == FIP_ST_AUTO) {
487 if (old_xid == FC_XID_UNKNOWN)
488 fip->flogi_count = 0;
489 fip->flogi_count++;
490 if (fip->flogi_count < 3)
491 goto drop;
492 fip->map_dest = 1;
493 return 0;
494 }
Joe Eykholt5f48f702009-05-06 10:52:12 -0700495 if (fip->state == FIP_ST_NON_FIP)
496 fip->map_dest = 1;
497 }
498
499 if (fip->state == FIP_ST_NON_FIP)
500 return 0;
501
502 switch (op) {
503 case ELS_FLOGI:
Joe Eykholt97c83892009-03-17 11:42:40 -0700504 op = FIP_DT_FLOGI;
505 break;
506 case ELS_FDISC:
507 if (ntoh24(fh->fh_s_id))
508 return 0;
509 op = FIP_DT_FDISC;
510 break;
511 case ELS_LOGO:
512 if (fip->state != FIP_ST_ENABLED)
513 return 0;
514 if (ntoh24(fh->fh_d_id) != FC_FID_FLOGI)
515 return 0;
516 op = FIP_DT_LOGO;
517 break;
518 case ELS_LS_ACC:
519 if (fip->flogi_oxid == FC_XID_UNKNOWN)
520 return 0;
521 if (!ntoh24(fh->fh_s_id))
522 return 0;
523 if (fip->state == FIP_ST_AUTO)
524 return 0;
525 /*
526 * Here we must've gotten an SID by accepting an FLOGI
527 * from a point-to-point connection. Switch to using
528 * the source mac based on the SID. The destination
529 * MAC in this case would have been set by receving the
530 * FLOGI.
531 */
532 fip->flogi_oxid = FC_XID_UNKNOWN;
533 fc_fcoe_set_mac(fip->data_src_addr, fh->fh_s_id);
534 return 0;
535 default:
536 if (fip->state != FIP_ST_ENABLED)
537 goto drop;
538 return 0;
539 }
540 if (fcoe_ctlr_encaps(fip, op, skb))
541 goto drop;
542 fip->send(fip, skb);
543 return -EINPROGRESS;
544drop:
545 kfree_skb(skb);
546 return -EINVAL;
547}
548EXPORT_SYMBOL(fcoe_ctlr_els_send);
549
550/*
551 * fcoe_ctlr_age_fcfs() - Reset and free all old FCFs for a controller.
552 * @fip: FCoE controller.
553 *
554 * Called with lock held.
555 *
556 * An FCF is considered old if we have missed three advertisements.
557 * That is, there have been no valid advertisement from it for three
558 * times its keep-alive period including fuzz.
559 *
560 * In addition, determine the time when an FCF selection can occur.
561 */
562static void fcoe_ctlr_age_fcfs(struct fcoe_ctlr *fip)
563{
564 struct fcoe_fcf *fcf;
565 struct fcoe_fcf *next;
566 unsigned long sel_time = 0;
567
568 list_for_each_entry_safe(fcf, next, &fip->fcfs, list) {
569 if (time_after(jiffies, fcf->time + fcf->fka_period * 3 +
570 msecs_to_jiffies(FIP_FCF_FUZZ * 3))) {
571 if (fip->sel_fcf == fcf)
572 fip->sel_fcf = NULL;
573 list_del(&fcf->list);
574 WARN_ON(!fip->fcf_count);
575 fip->fcf_count--;
576 kfree(fcf);
577 } else if (fcoe_ctlr_mtu_valid(fcf) &&
578 (!sel_time || time_before(sel_time, fcf->time))) {
579 sel_time = fcf->time;
580 }
581 }
582 if (sel_time) {
583 sel_time += msecs_to_jiffies(FCOE_CTLR_START_DELAY);
584 fip->sel_time = sel_time;
585 if (time_before(sel_time, fip->timer.expires))
586 mod_timer(&fip->timer, sel_time);
587 } else {
588 fip->sel_time = 0;
589 }
590}
591
592/**
593 * fcoe_ctlr_parse_adv() - Decode a FIP advertisement into a new FCF entry.
594 * @skb: received FIP advertisement frame
595 * @fcf: resulting FCF entry.
596 *
597 * Returns zero on a valid parsed advertisement,
598 * otherwise returns non zero value.
599 */
600static int fcoe_ctlr_parse_adv(struct sk_buff *skb, struct fcoe_fcf *fcf)
601{
602 struct fip_header *fiph;
603 struct fip_desc *desc = NULL;
604 struct fip_wwn_desc *wwn;
605 struct fip_fab_desc *fab;
606 struct fip_fka_desc *fka;
607 unsigned long t;
608 size_t rlen;
609 size_t dlen;
610
611 memset(fcf, 0, sizeof(*fcf));
612 fcf->fka_period = msecs_to_jiffies(FCOE_CTLR_DEF_FKA);
613
614 fiph = (struct fip_header *)skb->data;
615 fcf->flags = ntohs(fiph->fip_flags);
616
617 rlen = ntohs(fiph->fip_dl_len) * 4;
618 if (rlen + sizeof(*fiph) > skb->len)
619 return -EINVAL;
620
621 desc = (struct fip_desc *)(fiph + 1);
622 while (rlen > 0) {
623 dlen = desc->fip_dlen * FIP_BPW;
624 if (dlen < sizeof(*desc) || dlen > rlen)
625 return -EINVAL;
626 switch (desc->fip_dtype) {
627 case FIP_DT_PRI:
628 if (dlen != sizeof(struct fip_pri_desc))
629 goto len_err;
630 fcf->pri = ((struct fip_pri_desc *)desc)->fd_pri;
631 break;
632 case FIP_DT_MAC:
633 if (dlen != sizeof(struct fip_mac_desc))
634 goto len_err;
635 memcpy(fcf->fcf_mac,
636 ((struct fip_mac_desc *)desc)->fd_mac,
637 ETH_ALEN);
638 if (!is_valid_ether_addr(fcf->fcf_mac)) {
Robert Love650bd122009-06-10 15:31:05 -0700639 LIBFCOE_FIP_DBG("Invalid MAC address "
640 "in FIP adv\n");
Joe Eykholt97c83892009-03-17 11:42:40 -0700641 return -EINVAL;
642 }
643 break;
644 case FIP_DT_NAME:
645 if (dlen != sizeof(struct fip_wwn_desc))
646 goto len_err;
647 wwn = (struct fip_wwn_desc *)desc;
648 fcf->switch_name = get_unaligned_be64(&wwn->fd_wwn);
649 break;
650 case FIP_DT_FAB:
651 if (dlen != sizeof(struct fip_fab_desc))
652 goto len_err;
653 fab = (struct fip_fab_desc *)desc;
654 fcf->fabric_name = get_unaligned_be64(&fab->fd_wwn);
655 fcf->vfid = ntohs(fab->fd_vfid);
656 fcf->fc_map = ntoh24(fab->fd_map);
657 break;
658 case FIP_DT_FKA:
659 if (dlen != sizeof(struct fip_fka_desc))
660 goto len_err;
661 fka = (struct fip_fka_desc *)desc;
662 t = ntohl(fka->fd_fka_period);
663 if (t >= FCOE_CTLR_MIN_FKA)
664 fcf->fka_period = msecs_to_jiffies(t);
665 break;
666 case FIP_DT_MAP_OUI:
667 case FIP_DT_FCOE_SIZE:
668 case FIP_DT_FLOGI:
669 case FIP_DT_FDISC:
670 case FIP_DT_LOGO:
671 case FIP_DT_ELP:
672 default:
Robert Love650bd122009-06-10 15:31:05 -0700673 LIBFCOE_FIP_DBG("unexpected descriptor type %x "
674 "in FIP adv\n", desc->fip_dtype);
Joe Eykholt97c83892009-03-17 11:42:40 -0700675 /* standard says ignore unknown descriptors >= 128 */
676 if (desc->fip_dtype < FIP_DT_VENDOR_BASE)
677 return -EINVAL;
678 continue;
679 }
680 desc = (struct fip_desc *)((char *)desc + dlen);
681 rlen -= dlen;
682 }
683 if (!fcf->fc_map || (fcf->fc_map & 0x10000))
684 return -EINVAL;
685 if (!fcf->switch_name || !fcf->fabric_name)
686 return -EINVAL;
687 return 0;
688
689len_err:
Robert Love650bd122009-06-10 15:31:05 -0700690 LIBFCOE_FIP_DBG("FIP length error in descriptor type %x len %zu\n",
691 desc->fip_dtype, dlen);
Joe Eykholt97c83892009-03-17 11:42:40 -0700692 return -EINVAL;
693}
694
695/**
696 * fcoe_ctlr_recv_adv() - Handle an incoming advertisement.
697 * @fip: FCoE controller.
698 * @skb: Received FIP packet.
699 */
700static void fcoe_ctlr_recv_adv(struct fcoe_ctlr *fip, struct sk_buff *skb)
701{
702 struct fcoe_fcf *fcf;
703 struct fcoe_fcf new;
704 struct fcoe_fcf *found;
705 unsigned long sol_tov = msecs_to_jiffies(FCOE_CTRL_SOL_TOV);
706 int first = 0;
707 int mtu_valid;
708
709 if (fcoe_ctlr_parse_adv(skb, &new))
710 return;
711
712 spin_lock_bh(&fip->lock);
713 first = list_empty(&fip->fcfs);
714 found = NULL;
715 list_for_each_entry(fcf, &fip->fcfs, list) {
716 if (fcf->switch_name == new.switch_name &&
717 fcf->fabric_name == new.fabric_name &&
718 fcf->fc_map == new.fc_map &&
719 compare_ether_addr(fcf->fcf_mac, new.fcf_mac) == 0) {
720 found = fcf;
721 break;
722 }
723 }
724 if (!found) {
725 if (fip->fcf_count >= FCOE_CTLR_FCF_LIMIT)
726 goto out;
727
728 fcf = kmalloc(sizeof(*fcf), GFP_ATOMIC);
729 if (!fcf)
730 goto out;
731
732 fip->fcf_count++;
733 memcpy(fcf, &new, sizeof(new));
734 list_add(&fcf->list, &fip->fcfs);
735 } else {
736 /*
737 * Flags in advertisements are ignored once the FCF is
738 * selected. Flags in unsolicited advertisements are
739 * ignored after a usable solicited advertisement
740 * has been received.
741 */
742 if (fcf == fip->sel_fcf) {
743 fip->ctlr_ka_time -= fcf->fka_period;
744 fip->ctlr_ka_time += new.fka_period;
745 if (time_before(fip->ctlr_ka_time, fip->timer.expires))
746 mod_timer(&fip->timer, fip->ctlr_ka_time);
747 } else if (!fcoe_ctlr_fcf_usable(fcf))
748 fcf->flags = new.flags;
749 fcf->fka_period = new.fka_period;
750 memcpy(fcf->fcf_mac, new.fcf_mac, ETH_ALEN);
751 }
752 mtu_valid = fcoe_ctlr_mtu_valid(fcf);
753 fcf->time = jiffies;
Robert Love650bd122009-06-10 15:31:05 -0700754 if (!found) {
755 LIBFCOE_FIP_DBG("New FCF for fab %llx map %x val %d\n",
756 fcf->fabric_name, fcf->fc_map, mtu_valid);
757 }
Joe Eykholt97c83892009-03-17 11:42:40 -0700758
759 /*
760 * If this advertisement is not solicited and our max receive size
761 * hasn't been verified, send a solicited advertisement.
762 */
763 if (!mtu_valid)
764 fcoe_ctlr_solicit(fip, fcf);
765
766 /*
767 * If its been a while since we did a solicit, and this is
768 * the first advertisement we've received, do a multicast
769 * solicitation to gather as many advertisements as we can
770 * before selection occurs.
771 */
772 if (first && time_after(jiffies, fip->sol_time + sol_tov))
773 fcoe_ctlr_solicit(fip, NULL);
774
775 /*
776 * If this is the first validated FCF, note the time and
777 * set a timer to trigger selection.
778 */
779 if (mtu_valid && !fip->sel_time && fcoe_ctlr_fcf_usable(fcf)) {
780 fip->sel_time = jiffies +
781 msecs_to_jiffies(FCOE_CTLR_START_DELAY);
782 if (!timer_pending(&fip->timer) ||
783 time_before(fip->sel_time, fip->timer.expires))
784 mod_timer(&fip->timer, fip->sel_time);
785 }
786out:
787 spin_unlock_bh(&fip->lock);
788}
789
790/**
791 * fcoe_ctlr_recv_els() - Handle an incoming FIP-encapsulated ELS frame.
792 * @fip: FCoE controller.
793 * @skb: Received FIP packet.
794 */
795static void fcoe_ctlr_recv_els(struct fcoe_ctlr *fip, struct sk_buff *skb)
796{
797 struct fc_lport *lp = fip->lp;
798 struct fip_header *fiph;
799 struct fc_frame *fp;
800 struct fc_frame_header *fh = NULL;
801 struct fip_desc *desc;
802 struct fip_encaps *els;
803 struct fcoe_dev_stats *stats;
804 enum fip_desc_type els_dtype = 0;
805 u8 els_op;
806 u8 sub;
807 u8 granted_mac[ETH_ALEN] = { 0 };
808 size_t els_len = 0;
809 size_t rlen;
810 size_t dlen;
811
812 fiph = (struct fip_header *)skb->data;
813 sub = fiph->fip_subcode;
814 if (sub != FIP_SC_REQ && sub != FIP_SC_REP)
815 goto drop;
816
817 rlen = ntohs(fiph->fip_dl_len) * 4;
818 if (rlen + sizeof(*fiph) > skb->len)
819 goto drop;
820
821 desc = (struct fip_desc *)(fiph + 1);
822 while (rlen > 0) {
823 dlen = desc->fip_dlen * FIP_BPW;
824 if (dlen < sizeof(*desc) || dlen > rlen)
825 goto drop;
826 switch (desc->fip_dtype) {
827 case FIP_DT_MAC:
828 if (dlen != sizeof(struct fip_mac_desc))
829 goto len_err;
830 memcpy(granted_mac,
831 ((struct fip_mac_desc *)desc)->fd_mac,
832 ETH_ALEN);
833 if (!is_valid_ether_addr(granted_mac)) {
Robert Love650bd122009-06-10 15:31:05 -0700834 LIBFCOE_FIP_DBG("Invalid MAC address "
835 "in FIP ELS\n");
Joe Eykholt97c83892009-03-17 11:42:40 -0700836 goto drop;
837 }
838 break;
839 case FIP_DT_FLOGI:
840 case FIP_DT_FDISC:
841 case FIP_DT_LOGO:
842 case FIP_DT_ELP:
843 if (fh)
844 goto drop;
845 if (dlen < sizeof(*els) + sizeof(*fh) + 1)
846 goto len_err;
847 els_len = dlen - sizeof(*els);
848 els = (struct fip_encaps *)desc;
849 fh = (struct fc_frame_header *)(els + 1);
850 els_dtype = desc->fip_dtype;
851 break;
852 default:
Robert Love650bd122009-06-10 15:31:05 -0700853 LIBFCOE_FIP_DBG("unexpected descriptor type %x "
854 "in FIP adv\n", desc->fip_dtype);
Joe Eykholt97c83892009-03-17 11:42:40 -0700855 /* standard says ignore unknown descriptors >= 128 */
856 if (desc->fip_dtype < FIP_DT_VENDOR_BASE)
857 goto drop;
858 continue;
859 }
860 desc = (struct fip_desc *)((char *)desc + dlen);
861 rlen -= dlen;
862 }
863
864 if (!fh)
865 goto drop;
866 els_op = *(u8 *)(fh + 1);
867
Chris Leechdb36c062009-11-03 11:46:24 -0800868 if ((els_dtype == FIP_DT_FLOGI || els_dtype == FIP_DT_FDISC) &&
869 sub == FIP_SC_REP && fip->flogi_oxid == ntohs(fh->fh_ox_id) &&
Joe Eykholt97c83892009-03-17 11:42:40 -0700870 els_op == ELS_LS_ACC && is_valid_ether_addr(granted_mac)) {
871 fip->flogi_oxid = FC_XID_UNKNOWN;
872 fip->update_mac(fip, fip->data_src_addr, granted_mac);
873 memcpy(fip->data_src_addr, granted_mac, ETH_ALEN);
874 }
875
876 /*
877 * Convert skb into an fc_frame containing only the ELS.
878 */
879 skb_pull(skb, (u8 *)fh - skb->data);
880 skb_trim(skb, els_len);
881 fp = (struct fc_frame *)skb;
882 fc_frame_init(fp);
883 fr_sof(fp) = FC_SOF_I3;
884 fr_eof(fp) = FC_EOF_T;
885 fr_dev(fp) = lp;
886
887 stats = fc_lport_get_stats(lp);
888 stats->RxFrames++;
889 stats->RxWords += skb->len / FIP_BPW;
890
Vasu Dev52ff8782009-07-29 17:05:10 -0700891 fc_exch_recv(lp, fp);
Joe Eykholt97c83892009-03-17 11:42:40 -0700892 return;
893
894len_err:
Robert Love650bd122009-06-10 15:31:05 -0700895 LIBFCOE_FIP_DBG("FIP length error in descriptor type %x len %zu\n",
896 desc->fip_dtype, dlen);
Joe Eykholt97c83892009-03-17 11:42:40 -0700897drop:
898 kfree_skb(skb);
899}
900
901/**
902 * fcoe_ctlr_recv_els() - Handle an incoming link reset frame.
903 * @fip: FCoE controller.
904 * @fh: Received FIP header.
905 *
906 * There may be multiple VN_Port descriptors.
907 * The overall length has already been checked.
908 */
909static void fcoe_ctlr_recv_clr_vlink(struct fcoe_ctlr *fip,
910 struct fip_header *fh)
911{
912 struct fip_desc *desc;
913 struct fip_mac_desc *mp;
914 struct fip_wwn_desc *wp;
915 struct fip_vn_desc *vp;
916 size_t rlen;
917 size_t dlen;
918 struct fcoe_fcf *fcf = fip->sel_fcf;
919 struct fc_lport *lp = fip->lp;
920 u32 desc_mask;
921
Robert Love650bd122009-06-10 15:31:05 -0700922 LIBFCOE_FIP_DBG("Clear Virtual Link received\n");
Joe Eykholt97c83892009-03-17 11:42:40 -0700923 if (!fcf)
924 return;
925 if (!fcf || !fc_host_port_id(lp->host))
926 return;
927
928 /*
929 * mask of required descriptors. Validating each one clears its bit.
930 */
931 desc_mask = BIT(FIP_DT_MAC) | BIT(FIP_DT_NAME) | BIT(FIP_DT_VN_ID);
932
933 rlen = ntohs(fh->fip_dl_len) * FIP_BPW;
934 desc = (struct fip_desc *)(fh + 1);
935 while (rlen >= sizeof(*desc)) {
936 dlen = desc->fip_dlen * FIP_BPW;
937 if (dlen > rlen)
938 return;
939 switch (desc->fip_dtype) {
940 case FIP_DT_MAC:
941 mp = (struct fip_mac_desc *)desc;
942 if (dlen < sizeof(*mp))
943 return;
944 if (compare_ether_addr(mp->fd_mac, fcf->fcf_mac))
945 return;
946 desc_mask &= ~BIT(FIP_DT_MAC);
947 break;
948 case FIP_DT_NAME:
949 wp = (struct fip_wwn_desc *)desc;
950 if (dlen < sizeof(*wp))
951 return;
952 if (get_unaligned_be64(&wp->fd_wwn) != fcf->switch_name)
953 return;
954 desc_mask &= ~BIT(FIP_DT_NAME);
955 break;
956 case FIP_DT_VN_ID:
957 vp = (struct fip_vn_desc *)desc;
958 if (dlen < sizeof(*vp))
959 return;
960 if (compare_ether_addr(vp->fd_mac,
961 fip->data_src_addr) == 0 &&
962 get_unaligned_be64(&vp->fd_wwpn) == lp->wwpn &&
963 ntoh24(vp->fd_fc_id) == fc_host_port_id(lp->host))
964 desc_mask &= ~BIT(FIP_DT_VN_ID);
965 break;
966 default:
967 /* standard says ignore unknown descriptors >= 128 */
968 if (desc->fip_dtype < FIP_DT_VENDOR_BASE)
969 return;
970 break;
971 }
972 desc = (struct fip_desc *)((char *)desc + dlen);
973 rlen -= dlen;
974 }
975
976 /*
977 * reset only if all required descriptors were present and valid.
978 */
979 if (desc_mask) {
Robert Love650bd122009-06-10 15:31:05 -0700980 LIBFCOE_FIP_DBG("missing descriptors mask %x\n", desc_mask);
Joe Eykholt97c83892009-03-17 11:42:40 -0700981 } else {
Robert Love650bd122009-06-10 15:31:05 -0700982 LIBFCOE_FIP_DBG("performing Clear Virtual Link\n");
Joe Eykholt97c83892009-03-17 11:42:40 -0700983 fcoe_ctlr_reset(fip, FIP_ST_ENABLED);
984 }
985}
986
987/**
988 * fcoe_ctlr_recv() - Receive a FIP frame.
989 * @fip: FCoE controller.
990 * @skb: Received FIP packet.
991 *
992 * This is called from NET_RX_SOFTIRQ.
993 */
994void fcoe_ctlr_recv(struct fcoe_ctlr *fip, struct sk_buff *skb)
995{
996 spin_lock_bh(&fip->fip_recv_list.lock);
997 __skb_queue_tail(&fip->fip_recv_list, skb);
998 spin_unlock_bh(&fip->fip_recv_list.lock);
999 schedule_work(&fip->recv_work);
1000}
1001EXPORT_SYMBOL(fcoe_ctlr_recv);
1002
1003/**
1004 * fcoe_ctlr_recv_handler() - Receive a FIP frame.
1005 * @fip: FCoE controller.
1006 * @skb: Received FIP packet.
1007 *
1008 * Returns non-zero if the frame is dropped.
1009 */
1010static int fcoe_ctlr_recv_handler(struct fcoe_ctlr *fip, struct sk_buff *skb)
1011{
1012 struct fip_header *fiph;
1013 struct ethhdr *eh;
1014 enum fip_state state;
1015 u16 op;
1016 u8 sub;
1017
1018 if (skb_linearize(skb))
1019 goto drop;
1020 if (skb->len < sizeof(*fiph))
1021 goto drop;
1022 eh = eth_hdr(skb);
1023 if (compare_ether_addr(eh->h_dest, fip->ctl_src_addr) &&
1024 compare_ether_addr(eh->h_dest, FIP_ALL_ENODE_MACS))
1025 goto drop;
1026 fiph = (struct fip_header *)skb->data;
1027 op = ntohs(fiph->fip_op);
1028 sub = fiph->fip_subcode;
1029
Joe Eykholt97c83892009-03-17 11:42:40 -07001030 if (FIP_VER_DECAPS(fiph->fip_ver) != FIP_VER)
1031 goto drop;
1032 if (ntohs(fiph->fip_dl_len) * FIP_BPW + sizeof(*fiph) > skb->len)
1033 goto drop;
1034
1035 spin_lock_bh(&fip->lock);
1036 state = fip->state;
1037 if (state == FIP_ST_AUTO) {
1038 fip->map_dest = 0;
1039 fip->state = FIP_ST_ENABLED;
1040 state = FIP_ST_ENABLED;
Robert Love650bd122009-06-10 15:31:05 -07001041 LIBFCOE_FIP_DBG("Using FIP mode\n");
Joe Eykholt97c83892009-03-17 11:42:40 -07001042 }
1043 spin_unlock_bh(&fip->lock);
1044 if (state != FIP_ST_ENABLED)
1045 goto drop;
1046
1047 if (op == FIP_OP_LS) {
1048 fcoe_ctlr_recv_els(fip, skb); /* consumes skb */
1049 return 0;
1050 }
1051 if (op == FIP_OP_DISC && sub == FIP_SC_ADV)
1052 fcoe_ctlr_recv_adv(fip, skb);
1053 else if (op == FIP_OP_CTRL && sub == FIP_SC_CLR_VLINK)
1054 fcoe_ctlr_recv_clr_vlink(fip, fiph);
1055 kfree_skb(skb);
1056 return 0;
1057drop:
1058 kfree_skb(skb);
1059 return -1;
1060}
1061
1062/**
1063 * fcoe_ctlr_select() - Select the best FCF, if possible.
1064 * @fip: FCoE controller.
1065 *
1066 * If there are conflicting advertisements, no FCF can be chosen.
1067 *
1068 * Called with lock held.
1069 */
1070static void fcoe_ctlr_select(struct fcoe_ctlr *fip)
1071{
1072 struct fcoe_fcf *fcf;
1073 struct fcoe_fcf *best = NULL;
1074
1075 list_for_each_entry(fcf, &fip->fcfs, list) {
Robert Love650bd122009-06-10 15:31:05 -07001076 LIBFCOE_FIP_DBG("consider FCF for fab %llx VFID %d map %x "
1077 "val %d\n", fcf->fabric_name, fcf->vfid,
1078 fcf->fc_map, fcoe_ctlr_mtu_valid(fcf));
Joe Eykholt97c83892009-03-17 11:42:40 -07001079 if (!fcoe_ctlr_fcf_usable(fcf)) {
Robert Love650bd122009-06-10 15:31:05 -07001080 LIBFCOE_FIP_DBG("FCF for fab %llx map %x %svalid "
1081 "%savailable\n", fcf->fabric_name,
1082 fcf->fc_map, (fcf->flags & FIP_FL_SOL)
1083 ? "" : "in", (fcf->flags & FIP_FL_AVAIL)
1084 ? "" : "un");
Joe Eykholt97c83892009-03-17 11:42:40 -07001085 continue;
1086 }
1087 if (!best) {
1088 best = fcf;
1089 continue;
1090 }
1091 if (fcf->fabric_name != best->fabric_name ||
1092 fcf->vfid != best->vfid ||
1093 fcf->fc_map != best->fc_map) {
Robert Love650bd122009-06-10 15:31:05 -07001094 LIBFCOE_FIP_DBG("Conflicting fabric, VFID, "
1095 "or FC-MAP\n");
Joe Eykholt97c83892009-03-17 11:42:40 -07001096 return;
1097 }
1098 if (fcf->pri < best->pri)
1099 best = fcf;
1100 }
1101 fip->sel_fcf = best;
1102}
1103
1104/**
1105 * fcoe_ctlr_timeout() - FIP timer function.
1106 * @arg: &fcoe_ctlr pointer.
1107 *
1108 * Ages FCFs. Triggers FCF selection if possible. Sends keep-alives.
1109 */
1110static void fcoe_ctlr_timeout(unsigned long arg)
1111{
1112 struct fcoe_ctlr *fip = (struct fcoe_ctlr *)arg;
1113 struct fcoe_fcf *sel;
1114 struct fcoe_fcf *fcf;
1115 unsigned long next_timer = jiffies + msecs_to_jiffies(FIP_VN_KA_PERIOD);
Joe Eykholt97c83892009-03-17 11:42:40 -07001116 u8 send_ctlr_ka;
1117 u8 send_port_ka;
1118
1119 spin_lock_bh(&fip->lock);
1120 if (fip->state == FIP_ST_DISABLED) {
1121 spin_unlock_bh(&fip->lock);
1122 return;
1123 }
1124
1125 fcf = fip->sel_fcf;
1126 fcoe_ctlr_age_fcfs(fip);
1127
1128 sel = fip->sel_fcf;
1129 if (!sel && fip->sel_time && time_after_eq(jiffies, fip->sel_time)) {
1130 fcoe_ctlr_select(fip);
1131 sel = fip->sel_fcf;
1132 fip->sel_time = 0;
1133 }
1134
1135 if (sel != fcf) {
1136 fcf = sel; /* the old FCF may have been freed */
1137 if (sel) {
Robert Love650bd122009-06-10 15:31:05 -07001138 printk(KERN_INFO "libfcoe: host%d: FIP selected "
Johannes Berg66d6fae2009-07-15 17:21:14 +02001139 "Fibre-Channel Forwarder MAC %pM\n",
1140 fip->lp->host->host_no, sel->fcf_mac);
Joe Eykholt97c83892009-03-17 11:42:40 -07001141 memcpy(fip->dest_addr, sel->fcf_mac, ETH_ALEN);
1142 fip->port_ka_time = jiffies +
1143 msecs_to_jiffies(FIP_VN_KA_PERIOD);
1144 fip->ctlr_ka_time = jiffies + sel->fka_period;
1145 fip->link = 1;
1146 } else {
Robert Love650bd122009-06-10 15:31:05 -07001147 printk(KERN_NOTICE "libfcoe: host%d: "
Joe Eykholt97c83892009-03-17 11:42:40 -07001148 "FIP Fibre-Channel Forwarder timed out. "
1149 "Starting FCF discovery.\n",
1150 fip->lp->host->host_no);
1151 fip->link = 0;
1152 }
1153 schedule_work(&fip->link_work);
1154 }
1155
1156 send_ctlr_ka = 0;
1157 send_port_ka = 0;
1158 if (sel) {
1159 if (time_after_eq(jiffies, fip->ctlr_ka_time)) {
1160 fip->ctlr_ka_time = jiffies + sel->fka_period;
1161 send_ctlr_ka = 1;
1162 }
1163 if (time_after(next_timer, fip->ctlr_ka_time))
1164 next_timer = fip->ctlr_ka_time;
1165
1166 if (time_after_eq(jiffies, fip->port_ka_time)) {
1167 fip->port_ka_time += jiffies +
1168 msecs_to_jiffies(FIP_VN_KA_PERIOD);
1169 send_port_ka = 1;
1170 }
1171 if (time_after(next_timer, fip->port_ka_time))
1172 next_timer = fip->port_ka_time;
1173 mod_timer(&fip->timer, next_timer);
1174 } else if (fip->sel_time) {
1175 next_timer = fip->sel_time +
1176 msecs_to_jiffies(FCOE_CTLR_START_DELAY);
1177 mod_timer(&fip->timer, next_timer);
1178 }
1179 spin_unlock_bh(&fip->lock);
1180
1181 if (send_ctlr_ka)
1182 fcoe_ctlr_send_keep_alive(fip, 0, fip->ctl_src_addr);
1183 if (send_port_ka)
1184 fcoe_ctlr_send_keep_alive(fip, 1, fip->data_src_addr);
1185}
1186
1187/**
1188 * fcoe_ctlr_link_work() - worker thread function for link changes.
1189 * @work: pointer to link_work member inside &fcoe_ctlr.
1190 *
1191 * See if the link status has changed and if so, report it.
1192 *
1193 * This is here because fc_linkup() and fc_linkdown() must not
1194 * be called from the timer directly, since they use a mutex.
1195 */
1196static void fcoe_ctlr_link_work(struct work_struct *work)
1197{
1198 struct fcoe_ctlr *fip;
1199 int link;
1200 int last_link;
1201
1202 fip = container_of(work, struct fcoe_ctlr, link_work);
1203 spin_lock_bh(&fip->lock);
1204 last_link = fip->last_link;
1205 link = fip->link;
1206 fip->last_link = link;
1207 spin_unlock_bh(&fip->lock);
1208
1209 if (last_link != link) {
1210 if (link)
1211 fc_linkup(fip->lp);
1212 else
1213 fcoe_ctlr_reset(fip, FIP_ST_LINK_WAIT);
1214 }
1215}
1216
1217/**
1218 * fcoe_ctlr_recv_work() - Worker thread function for receiving FIP frames.
1219 * @recv_work: pointer to recv_work member inside &fcoe_ctlr.
1220 */
1221static void fcoe_ctlr_recv_work(struct work_struct *recv_work)
1222{
1223 struct fcoe_ctlr *fip;
1224 struct sk_buff *skb;
1225
1226 fip = container_of(recv_work, struct fcoe_ctlr, recv_work);
1227 spin_lock_bh(&fip->fip_recv_list.lock);
1228 while ((skb = __skb_dequeue(&fip->fip_recv_list))) {
1229 spin_unlock_bh(&fip->fip_recv_list.lock);
1230 fcoe_ctlr_recv_handler(fip, skb);
1231 spin_lock_bh(&fip->fip_recv_list.lock);
1232 }
1233 spin_unlock_bh(&fip->fip_recv_list.lock);
1234}
1235
1236/**
1237 * fcoe_ctlr_recv_flogi() - snoop Pre-FIP receipt of FLOGI response or request.
1238 * @fip: FCoE controller.
1239 * @fp: FC frame.
1240 * @sa: Ethernet source MAC address from received FCoE frame.
1241 *
1242 * Snoop potential response to FLOGI or even incoming FLOGI.
1243 *
1244 * The caller has checked that we are waiting for login as indicated
1245 * by fip->flogi_oxid != FC_XID_UNKNOWN.
1246 *
1247 * The caller is responsible for freeing the frame.
1248 *
1249 * Return non-zero if the frame should not be delivered to libfc.
1250 */
1251int fcoe_ctlr_recv_flogi(struct fcoe_ctlr *fip, struct fc_frame *fp, u8 *sa)
1252{
1253 struct fc_frame_header *fh;
1254 u8 op;
1255 u8 mac[ETH_ALEN];
1256
1257 fh = fc_frame_header_get(fp);
1258 if (fh->fh_type != FC_TYPE_ELS)
1259 return 0;
1260
1261 op = fc_frame_payload_op(fp);
1262 if (op == ELS_LS_ACC && fh->fh_r_ctl == FC_RCTL_ELS_REP &&
1263 fip->flogi_oxid == ntohs(fh->fh_ox_id)) {
1264
1265 spin_lock_bh(&fip->lock);
1266 if (fip->state != FIP_ST_AUTO && fip->state != FIP_ST_NON_FIP) {
1267 spin_unlock_bh(&fip->lock);
1268 return -EINVAL;
1269 }
1270 fip->state = FIP_ST_NON_FIP;
Robert Love650bd122009-06-10 15:31:05 -07001271 LIBFCOE_FIP_DBG("received FLOGI LS_ACC using non-FIP mode\n");
Joe Eykholt97c83892009-03-17 11:42:40 -07001272
1273 /*
1274 * FLOGI accepted.
1275 * If the src mac addr is FC_OUI-based, then we mark the
1276 * address_mode flag to use FC_OUI-based Ethernet DA.
1277 * Otherwise we use the FCoE gateway addr
1278 */
1279 if (!compare_ether_addr(sa, (u8[6])FC_FCOE_FLOGI_MAC)) {
1280 fip->map_dest = 1;
1281 } else {
1282 memcpy(fip->dest_addr, sa, ETH_ALEN);
1283 fip->map_dest = 0;
1284 }
1285 fip->flogi_oxid = FC_XID_UNKNOWN;
1286 memcpy(mac, fip->data_src_addr, ETH_ALEN);
1287 fc_fcoe_set_mac(fip->data_src_addr, fh->fh_d_id);
1288 spin_unlock_bh(&fip->lock);
1289
1290 fip->update_mac(fip, mac, fip->data_src_addr);
1291 } else if (op == ELS_FLOGI && fh->fh_r_ctl == FC_RCTL_ELS_REQ && sa) {
1292 /*
1293 * Save source MAC for point-to-point responses.
1294 */
1295 spin_lock_bh(&fip->lock);
1296 if (fip->state == FIP_ST_AUTO || fip->state == FIP_ST_NON_FIP) {
1297 memcpy(fip->dest_addr, sa, ETH_ALEN);
1298 fip->map_dest = 0;
1299 if (fip->state == FIP_ST_NON_FIP)
Robert Love650bd122009-06-10 15:31:05 -07001300 LIBFCOE_FIP_DBG("received FLOGI REQ, "
Joe Eykholt97c83892009-03-17 11:42:40 -07001301 "using non-FIP mode\n");
1302 fip->state = FIP_ST_NON_FIP;
1303 }
1304 spin_unlock_bh(&fip->lock);
1305 }
1306 return 0;
1307}
1308EXPORT_SYMBOL(fcoe_ctlr_recv_flogi);
1309
Vasu Dev5e80f7f2009-03-17 11:42:18 -07001310/**
1311 * fcoe_wwn_from_mac() - Converts 48-bit IEEE MAC address to 64-bit FC WWN.
1312 * @mac: mac address
1313 * @scheme: check port
1314 * @port: port indicator for converting
1315 *
1316 * Returns: u64 fc world wide name
1317 */
1318u64 fcoe_wwn_from_mac(unsigned char mac[MAX_ADDR_LEN],
1319 unsigned int scheme, unsigned int port)
1320{
1321 u64 wwn;
1322 u64 host_mac;
1323
1324 /* The MAC is in NO, so flip only the low 48 bits */
1325 host_mac = ((u64) mac[0] << 40) |
1326 ((u64) mac[1] << 32) |
1327 ((u64) mac[2] << 24) |
1328 ((u64) mac[3] << 16) |
1329 ((u64) mac[4] << 8) |
1330 (u64) mac[5];
1331
1332 WARN_ON(host_mac >= (1ULL << 48));
1333 wwn = host_mac | ((u64) scheme << 60);
1334 switch (scheme) {
1335 case 1:
1336 WARN_ON(port != 0);
1337 break;
1338 case 2:
1339 WARN_ON(port >= 0xfff);
1340 wwn |= (u64) port << 48;
1341 break;
1342 default:
1343 WARN_ON(1);
1344 break;
1345 }
1346
1347 return wwn;
1348}
1349EXPORT_SYMBOL_GPL(fcoe_wwn_from_mac);
1350
1351/**
1352 * fcoe_libfc_config() - sets up libfc related properties for lport
1353 * @lp: ptr to the fc_lport
1354 * @tt: libfc function template
1355 *
1356 * Returns : 0 for success
1357 */
1358int fcoe_libfc_config(struct fc_lport *lp, struct libfc_function_template *tt)
1359{
1360 /* Set the function pointers set by the LLDD */
1361 memcpy(&lp->tt, tt, sizeof(*tt));
1362 if (fc_fcp_init(lp))
1363 return -ENOMEM;
1364 fc_exch_init(lp);
1365 fc_elsct_init(lp);
1366 fc_lport_init(lp);
1367 fc_rport_init(lp);
1368 fc_disc_init(lp);
1369
1370 return 0;
1371}
1372EXPORT_SYMBOL_GPL(fcoe_libfc_config);