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