blob: 787e7225ddde9152d72279efebf7b72b5ad93e0c [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 Love70b51aa2009-11-03 11:47:45 -080062#define LIBFCOE_LOGGING 0x01 /* General logging, not categorized */
Robert Love650bd122009-06-10 15:31:05 -070063#define LIBFCOE_FIP_LOGGING 0x02 /* FIP logging */
64
Robert Love70b51aa2009-11-03 11:47:45 -080065#define LIBFCOE_CHECK_LOGGING(LEVEL, CMD) \
66do { \
67 if (unlikely(libfcoe_debug_logging & LEVEL)) \
68 do { \
69 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
Joe Eykholt0f51c2e2009-11-03 11:48:16 -080077#define LIBFCOE_FIP_DBG(fip, fmt, args...) \
Robert Love650bd122009-06-10 15:31:05 -070078 LIBFCOE_CHECK_LOGGING(LIBFCOE_FIP_LOGGING, \
Joe Eykholt0f51c2e2009-11-03 11:48:16 -080079 printk(KERN_INFO "host%d: fip: " fmt, \
80 (fip)->lp->host->host_no, ##args);)
Joe Eykholt97c83892009-03-17 11:42:40 -070081
Robert Love70b51aa2009-11-03 11:47:45 -080082/**
83 * fcoe_ctlr_mtu_valid() - Check if a FCF's MTU is valid
84 * @fcf: The FCF to check
85 *
Joe Eykholt97c83892009-03-17 11:42:40 -070086 * Return non-zero if FCF fcoe_size has been validated.
87 */
88static inline int fcoe_ctlr_mtu_valid(const struct fcoe_fcf *fcf)
89{
90 return (fcf->flags & FIP_FL_SOL) != 0;
91}
92
Robert Love70b51aa2009-11-03 11:47:45 -080093/**
94 * fcoe_ctlr_fcf_usable() - Check if a FCF is usable
95 * @fcf: The FCF to check
96 *
Joe Eykholt97c83892009-03-17 11:42:40 -070097 * Return non-zero if the FCF is usable.
98 */
99static inline int fcoe_ctlr_fcf_usable(struct fcoe_fcf *fcf)
100{
101 u16 flags = FIP_FL_SOL | FIP_FL_AVAIL;
102
103 return (fcf->flags & flags) == flags;
104}
105
106/**
Robert Love70b51aa2009-11-03 11:47:45 -0800107 * fcoe_ctlr_init() - Initialize the FCoE Controller instance
108 * @fip: The FCoE controller to initialize
Joe Eykholt97c83892009-03-17 11:42:40 -0700109 */
110void fcoe_ctlr_init(struct fcoe_ctlr *fip)
111{
112 fip->state = FIP_ST_LINK_WAIT;
Joe Eykholt22bcd222009-11-03 11:48:11 -0800113 fip->mode = FIP_ST_AUTO;
Joe Eykholt97c83892009-03-17 11:42:40 -0700114 INIT_LIST_HEAD(&fip->fcfs);
115 spin_lock_init(&fip->lock);
116 fip->flogi_oxid = FC_XID_UNKNOWN;
117 setup_timer(&fip->timer, fcoe_ctlr_timeout, (unsigned long)fip);
118 INIT_WORK(&fip->link_work, fcoe_ctlr_link_work);
119 INIT_WORK(&fip->recv_work, fcoe_ctlr_recv_work);
120 skb_queue_head_init(&fip->fip_recv_list);
121}
122EXPORT_SYMBOL(fcoe_ctlr_init);
123
124/**
Robert Love70b51aa2009-11-03 11:47:45 -0800125 * fcoe_ctlr_reset_fcfs() - Reset and free all FCFs for a controller
126 * @fip: The FCoE controller whose FCFs are to be reset
Joe Eykholt97c83892009-03-17 11:42:40 -0700127 *
128 * Called with &fcoe_ctlr lock held.
129 */
130static void fcoe_ctlr_reset_fcfs(struct fcoe_ctlr *fip)
131{
132 struct fcoe_fcf *fcf;
133 struct fcoe_fcf *next;
134
135 fip->sel_fcf = NULL;
136 list_for_each_entry_safe(fcf, next, &fip->fcfs, list) {
137 list_del(&fcf->list);
138 kfree(fcf);
139 }
140 fip->fcf_count = 0;
141 fip->sel_time = 0;
142}
143
144/**
Robert Love70b51aa2009-11-03 11:47:45 -0800145 * fcoe_ctlr_destroy() - Disable and tear down a FCoE controller
146 * @fip: The FCoE controller to tear down
Joe Eykholt97c83892009-03-17 11:42:40 -0700147 *
148 * This is called by FCoE drivers before freeing the &fcoe_ctlr.
149 *
150 * The receive handler will have been deleted before this to guarantee
151 * that no more recv_work will be scheduled.
152 *
153 * The timer routine will simply return once we set FIP_ST_DISABLED.
154 * This guarantees that no further timeouts or work will be scheduled.
155 */
156void fcoe_ctlr_destroy(struct fcoe_ctlr *fip)
157{
Chris Leecha4b7cfa2009-08-25 13:59:08 -0700158 cancel_work_sync(&fip->recv_work);
Joe Eykholt1f4aed82009-11-03 11:48:22 -0800159 skb_queue_purge(&fip->fip_recv_list);
Chris Leecha4b7cfa2009-08-25 13:59:08 -0700160
Joe Eykholt97c83892009-03-17 11:42:40 -0700161 spin_lock_bh(&fip->lock);
162 fip->state = FIP_ST_DISABLED;
163 fcoe_ctlr_reset_fcfs(fip);
164 spin_unlock_bh(&fip->lock);
165 del_timer_sync(&fip->timer);
Chris Leecha4b7cfa2009-08-25 13:59:08 -0700166 cancel_work_sync(&fip->link_work);
Joe Eykholt97c83892009-03-17 11:42:40 -0700167}
168EXPORT_SYMBOL(fcoe_ctlr_destroy);
169
170/**
Robert Love70b51aa2009-11-03 11:47:45 -0800171 * fcoe_ctlr_fcoe_size() - Return the maximum FCoE size required for VN_Port
172 * @fip: The FCoE controller to get the maximum FCoE size from
Joe Eykholt97c83892009-03-17 11:42:40 -0700173 *
174 * Returns the maximum packet size including the FCoE header and trailer,
175 * but not including any Ethernet or VLAN headers.
176 */
177static inline u32 fcoe_ctlr_fcoe_size(struct fcoe_ctlr *fip)
178{
179 /*
180 * Determine the max FCoE frame size allowed, including
181 * FCoE header and trailer.
182 * Note: lp->mfs is currently the payload size, not the frame size.
183 */
184 return fip->lp->mfs + sizeof(struct fc_frame_header) +
185 sizeof(struct fcoe_hdr) + sizeof(struct fcoe_crc_eof);
186}
187
188/**
Robert Love70b51aa2009-11-03 11:47:45 -0800189 * fcoe_ctlr_solicit() - Send a FIP solicitation
190 * @fip: The FCoE controller to send the solicitation on
191 * @fcf: The destination FCF (if NULL, a multicast solicitation is sent)
Joe Eykholt97c83892009-03-17 11:42:40 -0700192 */
193static void fcoe_ctlr_solicit(struct fcoe_ctlr *fip, struct fcoe_fcf *fcf)
194{
195 struct sk_buff *skb;
196 struct fip_sol {
197 struct ethhdr eth;
198 struct fip_header fip;
199 struct {
200 struct fip_mac_desc mac;
201 struct fip_wwn_desc wwnn;
202 struct fip_size_desc size;
203 } __attribute__((packed)) desc;
204 } __attribute__((packed)) *sol;
205 u32 fcoe_size;
206
207 skb = dev_alloc_skb(sizeof(*sol));
208 if (!skb)
209 return;
210
211 sol = (struct fip_sol *)skb->data;
212
213 memset(sol, 0, sizeof(*sol));
214 memcpy(sol->eth.h_dest, fcf ? fcf->fcf_mac : fcoe_all_fcfs, ETH_ALEN);
215 memcpy(sol->eth.h_source, fip->ctl_src_addr, ETH_ALEN);
216 sol->eth.h_proto = htons(ETH_P_FIP);
217
218 sol->fip.fip_ver = FIP_VER_ENCAPS(FIP_VER);
219 sol->fip.fip_op = htons(FIP_OP_DISC);
220 sol->fip.fip_subcode = FIP_SC_SOL;
221 sol->fip.fip_dl_len = htons(sizeof(sol->desc) / FIP_BPW);
222 sol->fip.fip_flags = htons(FIP_FL_FPMA);
Vasu Dev184dd342009-05-17 12:33:28 +0000223 if (fip->spma)
224 sol->fip.fip_flags |= htons(FIP_FL_SPMA);
Joe Eykholt97c83892009-03-17 11:42:40 -0700225
226 sol->desc.mac.fd_desc.fip_dtype = FIP_DT_MAC;
227 sol->desc.mac.fd_desc.fip_dlen = sizeof(sol->desc.mac) / FIP_BPW;
228 memcpy(sol->desc.mac.fd_mac, fip->ctl_src_addr, ETH_ALEN);
229
230 sol->desc.wwnn.fd_desc.fip_dtype = FIP_DT_NAME;
231 sol->desc.wwnn.fd_desc.fip_dlen = sizeof(sol->desc.wwnn) / FIP_BPW;
232 put_unaligned_be64(fip->lp->wwnn, &sol->desc.wwnn.fd_wwn);
233
234 fcoe_size = fcoe_ctlr_fcoe_size(fip);
235 sol->desc.size.fd_desc.fip_dtype = FIP_DT_FCOE_SIZE;
236 sol->desc.size.fd_desc.fip_dlen = sizeof(sol->desc.size) / FIP_BPW;
237 sol->desc.size.fd_size = htons(fcoe_size);
238
239 skb_put(skb, sizeof(*sol));
Chris Leech0f491532009-05-06 10:52:18 -0700240 skb->protocol = htons(ETH_P_FIP);
Joe Eykholt97c83892009-03-17 11:42:40 -0700241 skb_reset_mac_header(skb);
242 skb_reset_network_header(skb);
243 fip->send(fip, skb);
244
245 if (!fcf)
246 fip->sol_time = jiffies;
247}
248
249/**
Robert Love70b51aa2009-11-03 11:47:45 -0800250 * fcoe_ctlr_link_up() - Start FCoE controller
251 * @fip: The FCoE controller to start
Joe Eykholt97c83892009-03-17 11:42:40 -0700252 *
253 * Called from the LLD when the network link is ready.
254 */
255void fcoe_ctlr_link_up(struct fcoe_ctlr *fip)
256{
257 spin_lock_bh(&fip->lock);
258 if (fip->state == FIP_ST_NON_FIP || fip->state == FIP_ST_AUTO) {
259 fip->last_link = 1;
260 fip->link = 1;
261 spin_unlock_bh(&fip->lock);
262 fc_linkup(fip->lp);
263 } else if (fip->state == FIP_ST_LINK_WAIT) {
Joe Eykholt22bcd222009-11-03 11:48:11 -0800264 fip->state = fip->mode;
Joe Eykholt97c83892009-03-17 11:42:40 -0700265 fip->last_link = 1;
266 fip->link = 1;
267 spin_unlock_bh(&fip->lock);
Joe Eykholt22bcd222009-11-03 11:48:11 -0800268 if (fip->state == FIP_ST_AUTO)
Joe Eykholt0f51c2e2009-11-03 11:48:16 -0800269 LIBFCOE_FIP_DBG(fip, "%s", "setting AUTO mode.\n");
Joe Eykholt97c83892009-03-17 11:42:40 -0700270 fc_linkup(fip->lp);
271 fcoe_ctlr_solicit(fip, NULL);
272 } else
273 spin_unlock_bh(&fip->lock);
274}
275EXPORT_SYMBOL(fcoe_ctlr_link_up);
276
277/**
Robert Love70b51aa2009-11-03 11:47:45 -0800278 * fcoe_ctlr_reset() - Reset a FCoE controller
279 * @fip: The FCoE controller to reset
280 * @new_state: The FIP state to be entered
Joe Eykholt97c83892009-03-17 11:42:40 -0700281 *
282 * Returns non-zero if the link was up and now isn't.
283 */
284static int fcoe_ctlr_reset(struct fcoe_ctlr *fip, enum fip_state new_state)
285{
Robert Love70b51aa2009-11-03 11:47:45 -0800286 struct fc_lport *lport = fip->lp;
Joe Eykholt97c83892009-03-17 11:42:40 -0700287 int link_dropped;
288
289 spin_lock_bh(&fip->lock);
290 fcoe_ctlr_reset_fcfs(fip);
291 del_timer(&fip->timer);
292 fip->state = new_state;
293 fip->ctlr_ka_time = 0;
294 fip->port_ka_time = 0;
295 fip->sol_time = 0;
296 fip->flogi_oxid = FC_XID_UNKNOWN;
297 fip->map_dest = 0;
298 fip->last_link = 0;
299 link_dropped = fip->link;
300 fip->link = 0;
301 spin_unlock_bh(&fip->lock);
302
303 if (link_dropped)
Robert Love70b51aa2009-11-03 11:47:45 -0800304 fc_linkdown(lport);
Joe Eykholt97c83892009-03-17 11:42:40 -0700305
306 if (new_state == FIP_ST_ENABLED) {
307 fcoe_ctlr_solicit(fip, NULL);
Robert Love70b51aa2009-11-03 11:47:45 -0800308 fc_linkup(lport);
Joe Eykholt97c83892009-03-17 11:42:40 -0700309 link_dropped = 0;
310 }
311 return link_dropped;
312}
313
314/**
Robert Love70b51aa2009-11-03 11:47:45 -0800315 * fcoe_ctlr_link_down() - Stop a FCoE controller
316 * @fip: The FCoE controller to be stopped
Joe Eykholt97c83892009-03-17 11:42:40 -0700317 *
318 * Returns non-zero if the link was up and now isn't.
319 *
320 * Called from the LLD when the network link is not ready.
321 * There may be multiple calls while the link is down.
322 */
323int fcoe_ctlr_link_down(struct fcoe_ctlr *fip)
324{
325 return fcoe_ctlr_reset(fip, FIP_ST_LINK_WAIT);
326}
327EXPORT_SYMBOL(fcoe_ctlr_link_down);
328
329/**
Robert Love70b51aa2009-11-03 11:47:45 -0800330 * fcoe_ctlr_send_keep_alive() - Send a keep-alive to the selected FCF
331 * @fip: The FCoE controller to send the FKA on
332 * @lport: libfc fc_lport to send from
333 * @ports: 0 for controller keep-alive, 1 for port keep-alive
334 * @sa: The source MAC address
Joe Eykholt97c83892009-03-17 11:42:40 -0700335 *
336 * A controller keep-alive is sent every fka_period (typically 8 seconds).
337 * The source MAC is the native MAC address.
338 *
339 * A port keep-alive is sent every 90 seconds while logged in.
340 * The source MAC is the assigned mapped source address.
341 * The destination is the FCF's F-port.
342 */
Chris Leech11b56182009-11-03 11:46:29 -0800343static void fcoe_ctlr_send_keep_alive(struct fcoe_ctlr *fip,
344 struct fc_lport *lport,
345 int ports, u8 *sa)
Joe Eykholt97c83892009-03-17 11:42:40 -0700346{
347 struct sk_buff *skb;
348 struct fip_kal {
349 struct ethhdr eth;
350 struct fip_header fip;
351 struct fip_mac_desc mac;
352 } __attribute__((packed)) *kal;
353 struct fip_vn_desc *vn;
354 u32 len;
355 struct fc_lport *lp;
356 struct fcoe_fcf *fcf;
357
358 fcf = fip->sel_fcf;
359 lp = fip->lp;
360 if (!fcf || !fc_host_port_id(lp->host))
361 return;
362
363 len = fcoe_ctlr_fcoe_size(fip) + sizeof(struct ethhdr);
364 BUG_ON(len < sizeof(*kal) + sizeof(*vn));
365 skb = dev_alloc_skb(len);
366 if (!skb)
367 return;
368
369 kal = (struct fip_kal *)skb->data;
370 memset(kal, 0, len);
371 memcpy(kal->eth.h_dest, fcf->fcf_mac, ETH_ALEN);
372 memcpy(kal->eth.h_source, sa, ETH_ALEN);
373 kal->eth.h_proto = htons(ETH_P_FIP);
374
375 kal->fip.fip_ver = FIP_VER_ENCAPS(FIP_VER);
376 kal->fip.fip_op = htons(FIP_OP_CTRL);
377 kal->fip.fip_subcode = FIP_SC_KEEP_ALIVE;
378 kal->fip.fip_dl_len = htons((sizeof(kal->mac) +
Robert Love70b51aa2009-11-03 11:47:45 -0800379 ports * sizeof(*vn)) / FIP_BPW);
Joe Eykholt97c83892009-03-17 11:42:40 -0700380 kal->fip.fip_flags = htons(FIP_FL_FPMA);
Vasu Dev184dd342009-05-17 12:33:28 +0000381 if (fip->spma)
382 kal->fip.fip_flags |= htons(FIP_FL_SPMA);
Joe Eykholt97c83892009-03-17 11:42:40 -0700383
384 kal->mac.fd_desc.fip_dtype = FIP_DT_MAC;
385 kal->mac.fd_desc.fip_dlen = sizeof(kal->mac) / FIP_BPW;
386 memcpy(kal->mac.fd_mac, fip->ctl_src_addr, ETH_ALEN);
Joe Eykholt97c83892009-03-17 11:42:40 -0700387 if (ports) {
388 vn = (struct fip_vn_desc *)(kal + 1);
389 vn->fd_desc.fip_dtype = FIP_DT_VN_ID;
390 vn->fd_desc.fip_dlen = sizeof(*vn) / FIP_BPW;
Chris Leech11b56182009-11-03 11:46:29 -0800391 memcpy(vn->fd_mac, fip->get_src_addr(lport), ETH_ALEN);
Joe Eykholt97c83892009-03-17 11:42:40 -0700392 hton24(vn->fd_fc_id, fc_host_port_id(lp->host));
393 put_unaligned_be64(lp->wwpn, &vn->fd_wwpn);
394 }
Joe Eykholt97c83892009-03-17 11:42:40 -0700395 skb_put(skb, len);
Chris Leech0f491532009-05-06 10:52:18 -0700396 skb->protocol = htons(ETH_P_FIP);
Joe Eykholt97c83892009-03-17 11:42:40 -0700397 skb_reset_mac_header(skb);
398 skb_reset_network_header(skb);
399 fip->send(fip, skb);
400}
401
402/**
Robert Love70b51aa2009-11-03 11:47:45 -0800403 * fcoe_ctlr_encaps() - Encapsulate an ELS frame for FIP, without sending it
404 * @fip: The FCoE controller for the ELS frame
405 * @dtype: The FIP descriptor type for the frame
406 * @skb: The FCoE ELS frame including FC header but no FCoE headers
Joe Eykholt97c83892009-03-17 11:42:40 -0700407 *
408 * Returns non-zero error code on failure.
409 *
410 * The caller must check that the length is a multiple of 4.
411 *
412 * The @skb must have enough headroom (28 bytes) and tailroom (8 bytes).
413 * Headroom includes the FIP encapsulation description, FIP header, and
414 * Ethernet header. The tailroom is for the FIP MAC descriptor.
415 */
Chris Leech11b56182009-11-03 11:46:29 -0800416static int fcoe_ctlr_encaps(struct fcoe_ctlr *fip, struct fc_lport *lport,
Joe Eykholt97c83892009-03-17 11:42:40 -0700417 u8 dtype, struct sk_buff *skb)
418{
419 struct fip_encaps_head {
420 struct ethhdr eth;
421 struct fip_header fip;
422 struct fip_encaps encaps;
423 } __attribute__((packed)) *cap;
424 struct fip_mac_desc *mac;
425 struct fcoe_fcf *fcf;
426 size_t dlen;
Yi Zou5a84bae2009-07-29 17:03:55 -0700427 u16 fip_flags;
Joe Eykholt97c83892009-03-17 11:42:40 -0700428
429 fcf = fip->sel_fcf;
430 if (!fcf)
431 return -ENODEV;
Yi Zou5a84bae2009-07-29 17:03:55 -0700432
433 /* set flags according to both FCF and lport's capability on SPMA */
434 fip_flags = fcf->flags;
435 fip_flags &= fip->spma ? FIP_FL_SPMA | FIP_FL_FPMA : FIP_FL_FPMA;
436 if (!fip_flags)
437 return -ENODEV;
438
Joe Eykholt97c83892009-03-17 11:42:40 -0700439 dlen = sizeof(struct fip_encaps) + skb->len; /* len before push */
440 cap = (struct fip_encaps_head *)skb_push(skb, sizeof(*cap));
441
442 memset(cap, 0, sizeof(*cap));
443 memcpy(cap->eth.h_dest, fcf->fcf_mac, ETH_ALEN);
444 memcpy(cap->eth.h_source, fip->ctl_src_addr, ETH_ALEN);
445 cap->eth.h_proto = htons(ETH_P_FIP);
446
447 cap->fip.fip_ver = FIP_VER_ENCAPS(FIP_VER);
448 cap->fip.fip_op = htons(FIP_OP_LS);
449 cap->fip.fip_subcode = FIP_SC_REQ;
450 cap->fip.fip_dl_len = htons((dlen + sizeof(*mac)) / FIP_BPW);
Yi Zou5a84bae2009-07-29 17:03:55 -0700451 cap->fip.fip_flags = htons(fip_flags);
Joe Eykholt97c83892009-03-17 11:42:40 -0700452
453 cap->encaps.fd_desc.fip_dtype = dtype;
454 cap->encaps.fd_desc.fip_dlen = dlen / FIP_BPW;
455
456 mac = (struct fip_mac_desc *)skb_put(skb, sizeof(*mac));
457 memset(mac, 0, sizeof(mac));
458 mac->fd_desc.fip_dtype = FIP_DT_MAC;
459 mac->fd_desc.fip_dlen = sizeof(*mac) / FIP_BPW;
Chris Leechdb36c062009-11-03 11:46:24 -0800460 if (dtype != FIP_DT_FLOGI && dtype != FIP_DT_FDISC)
Chris Leech11b56182009-11-03 11:46:29 -0800461 memcpy(mac->fd_mac, fip->get_src_addr(lport), ETH_ALEN);
Vasu Dev184dd342009-05-17 12:33:28 +0000462 else if (fip->spma)
463 memcpy(mac->fd_mac, fip->ctl_src_addr, ETH_ALEN);
Joe Eykholt97c83892009-03-17 11:42:40 -0700464
Chris Leech0f491532009-05-06 10:52:18 -0700465 skb->protocol = htons(ETH_P_FIP);
Joe Eykholt97c83892009-03-17 11:42:40 -0700466 skb_reset_mac_header(skb);
467 skb_reset_network_header(skb);
468 return 0;
469}
470
471/**
472 * fcoe_ctlr_els_send() - Send an ELS frame encapsulated by FIP if appropriate.
473 * @fip: FCoE controller.
Chris Leech11b56182009-11-03 11:46:29 -0800474 * @lport: libfc fc_lport to send from
Joe Eykholt97c83892009-03-17 11:42:40 -0700475 * @skb: FCoE ELS frame including FC header but no FCoE headers.
476 *
477 * Returns a non-zero error code if the frame should not be sent.
478 * Returns zero if the caller should send the frame with FCoE encapsulation.
479 *
480 * The caller must check that the length is a multiple of 4.
481 * The SKB must have enough headroom (28 bytes) and tailroom (8 bytes).
482 */
Chris Leech11b56182009-11-03 11:46:29 -0800483int fcoe_ctlr_els_send(struct fcoe_ctlr *fip, struct fc_lport *lport,
484 struct sk_buff *skb)
Joe Eykholt97c83892009-03-17 11:42:40 -0700485{
486 struct fc_frame_header *fh;
487 u16 old_xid;
488 u8 op;
Chris Leech11b56182009-11-03 11:46:29 -0800489 u8 mac[ETH_ALEN];
Joe Eykholt97c83892009-03-17 11:42:40 -0700490
Joe Eykholt97c83892009-03-17 11:42:40 -0700491 fh = (struct fc_frame_header *)skb->data;
492 op = *(u8 *)(fh + 1);
493
Joe Eykholt5f48f702009-05-06 10:52:12 -0700494 if (op == ELS_FLOGI) {
Joe Eykholt97c83892009-03-17 11:42:40 -0700495 old_xid = fip->flogi_oxid;
496 fip->flogi_oxid = ntohs(fh->fh_ox_id);
497 if (fip->state == FIP_ST_AUTO) {
498 if (old_xid == FC_XID_UNKNOWN)
499 fip->flogi_count = 0;
500 fip->flogi_count++;
501 if (fip->flogi_count < 3)
502 goto drop;
503 fip->map_dest = 1;
504 return 0;
505 }
Joe Eykholt5f48f702009-05-06 10:52:12 -0700506 if (fip->state == FIP_ST_NON_FIP)
507 fip->map_dest = 1;
508 }
509
510 if (fip->state == FIP_ST_NON_FIP)
511 return 0;
512
513 switch (op) {
514 case ELS_FLOGI:
Joe Eykholt97c83892009-03-17 11:42:40 -0700515 op = FIP_DT_FLOGI;
516 break;
517 case ELS_FDISC:
518 if (ntoh24(fh->fh_s_id))
519 return 0;
520 op = FIP_DT_FDISC;
521 break;
522 case ELS_LOGO:
523 if (fip->state != FIP_ST_ENABLED)
524 return 0;
525 if (ntoh24(fh->fh_d_id) != FC_FID_FLOGI)
526 return 0;
527 op = FIP_DT_LOGO;
528 break;
529 case ELS_LS_ACC:
530 if (fip->flogi_oxid == FC_XID_UNKNOWN)
531 return 0;
532 if (!ntoh24(fh->fh_s_id))
533 return 0;
534 if (fip->state == FIP_ST_AUTO)
535 return 0;
536 /*
537 * Here we must've gotten an SID by accepting an FLOGI
538 * from a point-to-point connection. Switch to using
539 * the source mac based on the SID. The destination
540 * MAC in this case would have been set by receving the
541 * FLOGI.
542 */
543 fip->flogi_oxid = FC_XID_UNKNOWN;
Chris Leech11b56182009-11-03 11:46:29 -0800544 fc_fcoe_set_mac(mac, fh->fh_d_id);
545 fip->update_mac(lport, mac);
Joe Eykholt97c83892009-03-17 11:42:40 -0700546 return 0;
547 default:
548 if (fip->state != FIP_ST_ENABLED)
549 goto drop;
550 return 0;
551 }
Chris Leech11b56182009-11-03 11:46:29 -0800552 if (fcoe_ctlr_encaps(fip, lport, op, skb))
Joe Eykholt97c83892009-03-17 11:42:40 -0700553 goto drop;
554 fip->send(fip, skb);
555 return -EINPROGRESS;
556drop:
557 kfree_skb(skb);
558 return -EINVAL;
559}
560EXPORT_SYMBOL(fcoe_ctlr_els_send);
561
Robert Love70b51aa2009-11-03 11:47:45 -0800562/**
563 * fcoe_ctlr_age_fcfs() - Reset and free all old FCFs for a controller
564 * @fip: The FCoE controller to free FCFs on
Joe Eykholt97c83892009-03-17 11:42:40 -0700565 *
566 * Called with lock held.
567 *
568 * An FCF is considered old if we have missed three advertisements.
569 * That is, there have been no valid advertisement from it for three
570 * times its keep-alive period including fuzz.
571 *
572 * In addition, determine the time when an FCF selection can occur.
573 */
574static void fcoe_ctlr_age_fcfs(struct fcoe_ctlr *fip)
575{
576 struct fcoe_fcf *fcf;
577 struct fcoe_fcf *next;
578 unsigned long sel_time = 0;
579
580 list_for_each_entry_safe(fcf, next, &fip->fcfs, list) {
581 if (time_after(jiffies, fcf->time + fcf->fka_period * 3 +
582 msecs_to_jiffies(FIP_FCF_FUZZ * 3))) {
583 if (fip->sel_fcf == fcf)
584 fip->sel_fcf = NULL;
585 list_del(&fcf->list);
586 WARN_ON(!fip->fcf_count);
587 fip->fcf_count--;
588 kfree(fcf);
589 } else if (fcoe_ctlr_mtu_valid(fcf) &&
590 (!sel_time || time_before(sel_time, fcf->time))) {
591 sel_time = fcf->time;
592 }
593 }
594 if (sel_time) {
595 sel_time += msecs_to_jiffies(FCOE_CTLR_START_DELAY);
596 fip->sel_time = sel_time;
597 if (time_before(sel_time, fip->timer.expires))
598 mod_timer(&fip->timer, sel_time);
599 } else {
600 fip->sel_time = 0;
601 }
602}
603
604/**
Robert Love70b51aa2009-11-03 11:47:45 -0800605 * fcoe_ctlr_parse_adv() - Decode a FIP advertisement into a new FCF entry
Joe Eykholt0f51c2e2009-11-03 11:48:16 -0800606 * @fip: The FCoE controller receiving the advertisement
Robert Love70b51aa2009-11-03 11:47:45 -0800607 * @skb: The received FIP advertisement frame
608 * @fcf: The resulting FCF entry
Joe Eykholt97c83892009-03-17 11:42:40 -0700609 *
610 * Returns zero on a valid parsed advertisement,
611 * otherwise returns non zero value.
612 */
Joe Eykholt0f51c2e2009-11-03 11:48:16 -0800613static int fcoe_ctlr_parse_adv(struct fcoe_ctlr *fip,
614 struct sk_buff *skb, struct fcoe_fcf *fcf)
Joe Eykholt97c83892009-03-17 11:42:40 -0700615{
616 struct fip_header *fiph;
617 struct fip_desc *desc = NULL;
618 struct fip_wwn_desc *wwn;
619 struct fip_fab_desc *fab;
620 struct fip_fka_desc *fka;
621 unsigned long t;
622 size_t rlen;
623 size_t dlen;
624
625 memset(fcf, 0, sizeof(*fcf));
626 fcf->fka_period = msecs_to_jiffies(FCOE_CTLR_DEF_FKA);
627
628 fiph = (struct fip_header *)skb->data;
629 fcf->flags = ntohs(fiph->fip_flags);
630
631 rlen = ntohs(fiph->fip_dl_len) * 4;
632 if (rlen + sizeof(*fiph) > skb->len)
633 return -EINVAL;
634
635 desc = (struct fip_desc *)(fiph + 1);
636 while (rlen > 0) {
637 dlen = desc->fip_dlen * FIP_BPW;
638 if (dlen < sizeof(*desc) || dlen > rlen)
639 return -EINVAL;
640 switch (desc->fip_dtype) {
641 case FIP_DT_PRI:
642 if (dlen != sizeof(struct fip_pri_desc))
643 goto len_err;
644 fcf->pri = ((struct fip_pri_desc *)desc)->fd_pri;
645 break;
646 case FIP_DT_MAC:
647 if (dlen != sizeof(struct fip_mac_desc))
648 goto len_err;
649 memcpy(fcf->fcf_mac,
650 ((struct fip_mac_desc *)desc)->fd_mac,
651 ETH_ALEN);
652 if (!is_valid_ether_addr(fcf->fcf_mac)) {
Joe Eykholt0f51c2e2009-11-03 11:48:16 -0800653 LIBFCOE_FIP_DBG(fip, "Invalid MAC address "
Robert Love650bd122009-06-10 15:31:05 -0700654 "in FIP adv\n");
Joe Eykholt97c83892009-03-17 11:42:40 -0700655 return -EINVAL;
656 }
657 break;
658 case FIP_DT_NAME:
659 if (dlen != sizeof(struct fip_wwn_desc))
660 goto len_err;
661 wwn = (struct fip_wwn_desc *)desc;
662 fcf->switch_name = get_unaligned_be64(&wwn->fd_wwn);
663 break;
664 case FIP_DT_FAB:
665 if (dlen != sizeof(struct fip_fab_desc))
666 goto len_err;
667 fab = (struct fip_fab_desc *)desc;
668 fcf->fabric_name = get_unaligned_be64(&fab->fd_wwn);
669 fcf->vfid = ntohs(fab->fd_vfid);
670 fcf->fc_map = ntoh24(fab->fd_map);
671 break;
672 case FIP_DT_FKA:
673 if (dlen != sizeof(struct fip_fka_desc))
674 goto len_err;
675 fka = (struct fip_fka_desc *)desc;
676 t = ntohl(fka->fd_fka_period);
677 if (t >= FCOE_CTLR_MIN_FKA)
678 fcf->fka_period = msecs_to_jiffies(t);
679 break;
680 case FIP_DT_MAP_OUI:
681 case FIP_DT_FCOE_SIZE:
682 case FIP_DT_FLOGI:
683 case FIP_DT_FDISC:
684 case FIP_DT_LOGO:
685 case FIP_DT_ELP:
686 default:
Joe Eykholt0f51c2e2009-11-03 11:48:16 -0800687 LIBFCOE_FIP_DBG(fip, "unexpected descriptor type %x "
Robert Love650bd122009-06-10 15:31:05 -0700688 "in FIP adv\n", desc->fip_dtype);
Joe Eykholt97c83892009-03-17 11:42:40 -0700689 /* standard says ignore unknown descriptors >= 128 */
690 if (desc->fip_dtype < FIP_DT_VENDOR_BASE)
691 return -EINVAL;
692 continue;
693 }
694 desc = (struct fip_desc *)((char *)desc + dlen);
695 rlen -= dlen;
696 }
697 if (!fcf->fc_map || (fcf->fc_map & 0x10000))
698 return -EINVAL;
699 if (!fcf->switch_name || !fcf->fabric_name)
700 return -EINVAL;
701 return 0;
702
703len_err:
Joe Eykholt0f51c2e2009-11-03 11:48:16 -0800704 LIBFCOE_FIP_DBG(fip, "FIP length error in descriptor type %x len %zu\n",
Robert Love650bd122009-06-10 15:31:05 -0700705 desc->fip_dtype, dlen);
Joe Eykholt97c83892009-03-17 11:42:40 -0700706 return -EINVAL;
707}
708
709/**
Robert Love70b51aa2009-11-03 11:47:45 -0800710 * fcoe_ctlr_recv_adv() - Handle an incoming advertisement
711 * @fip: The FCoE controller receiving the advertisement
712 * @skb: The received FIP packet
Joe Eykholt97c83892009-03-17 11:42:40 -0700713 */
714static void fcoe_ctlr_recv_adv(struct fcoe_ctlr *fip, struct sk_buff *skb)
715{
716 struct fcoe_fcf *fcf;
717 struct fcoe_fcf new;
718 struct fcoe_fcf *found;
719 unsigned long sol_tov = msecs_to_jiffies(FCOE_CTRL_SOL_TOV);
720 int first = 0;
721 int mtu_valid;
722
Joe Eykholt0f51c2e2009-11-03 11:48:16 -0800723 if (fcoe_ctlr_parse_adv(fip, skb, &new))
Joe Eykholt97c83892009-03-17 11:42:40 -0700724 return;
725
726 spin_lock_bh(&fip->lock);
727 first = list_empty(&fip->fcfs);
728 found = NULL;
729 list_for_each_entry(fcf, &fip->fcfs, list) {
730 if (fcf->switch_name == new.switch_name &&
731 fcf->fabric_name == new.fabric_name &&
732 fcf->fc_map == new.fc_map &&
733 compare_ether_addr(fcf->fcf_mac, new.fcf_mac) == 0) {
734 found = fcf;
735 break;
736 }
737 }
738 if (!found) {
739 if (fip->fcf_count >= FCOE_CTLR_FCF_LIMIT)
740 goto out;
741
742 fcf = kmalloc(sizeof(*fcf), GFP_ATOMIC);
743 if (!fcf)
744 goto out;
745
746 fip->fcf_count++;
747 memcpy(fcf, &new, sizeof(new));
748 list_add(&fcf->list, &fip->fcfs);
749 } else {
750 /*
751 * Flags in advertisements are ignored once the FCF is
752 * selected. Flags in unsolicited advertisements are
753 * ignored after a usable solicited advertisement
754 * has been received.
755 */
756 if (fcf == fip->sel_fcf) {
757 fip->ctlr_ka_time -= fcf->fka_period;
758 fip->ctlr_ka_time += new.fka_period;
759 if (time_before(fip->ctlr_ka_time, fip->timer.expires))
760 mod_timer(&fip->timer, fip->ctlr_ka_time);
761 } else if (!fcoe_ctlr_fcf_usable(fcf))
762 fcf->flags = new.flags;
763 fcf->fka_period = new.fka_period;
764 memcpy(fcf->fcf_mac, new.fcf_mac, ETH_ALEN);
765 }
766 mtu_valid = fcoe_ctlr_mtu_valid(fcf);
767 fcf->time = jiffies;
Robert Love650bd122009-06-10 15:31:05 -0700768 if (!found) {
Joe Eykholt0f51c2e2009-11-03 11:48:16 -0800769 LIBFCOE_FIP_DBG(fip, "New FCF for fab %llx map %x val %d\n",
Robert Love650bd122009-06-10 15:31:05 -0700770 fcf->fabric_name, fcf->fc_map, mtu_valid);
771 }
Joe Eykholt97c83892009-03-17 11:42:40 -0700772
773 /*
774 * If this advertisement is not solicited and our max receive size
775 * hasn't been verified, send a solicited advertisement.
776 */
777 if (!mtu_valid)
778 fcoe_ctlr_solicit(fip, fcf);
779
780 /*
781 * If its been a while since we did a solicit, and this is
782 * the first advertisement we've received, do a multicast
783 * solicitation to gather as many advertisements as we can
784 * before selection occurs.
785 */
786 if (first && time_after(jiffies, fip->sol_time + sol_tov))
787 fcoe_ctlr_solicit(fip, NULL);
788
789 /*
790 * If this is the first validated FCF, note the time and
791 * set a timer to trigger selection.
792 */
793 if (mtu_valid && !fip->sel_time && fcoe_ctlr_fcf_usable(fcf)) {
794 fip->sel_time = jiffies +
Robert Love70b51aa2009-11-03 11:47:45 -0800795 msecs_to_jiffies(FCOE_CTLR_START_DELAY);
Joe Eykholt97c83892009-03-17 11:42:40 -0700796 if (!timer_pending(&fip->timer) ||
797 time_before(fip->sel_time, fip->timer.expires))
798 mod_timer(&fip->timer, fip->sel_time);
799 }
800out:
801 spin_unlock_bh(&fip->lock);
802}
803
804/**
Robert Love70b51aa2009-11-03 11:47:45 -0800805 * fcoe_ctlr_recv_els() - Handle an incoming FIP encapsulated ELS frame
806 * @fip: The FCoE controller which received the packet
807 * @skb: The received FIP packet
Joe Eykholt97c83892009-03-17 11:42:40 -0700808 */
809static void fcoe_ctlr_recv_els(struct fcoe_ctlr *fip, struct sk_buff *skb)
810{
Robert Love70b51aa2009-11-03 11:47:45 -0800811 struct fc_lport *lport = fip->lp;
Joe Eykholt97c83892009-03-17 11:42:40 -0700812 struct fip_header *fiph;
Chris Leech11b56182009-11-03 11:46:29 -0800813 struct fc_frame *fp = (struct fc_frame *)skb;
Joe Eykholt97c83892009-03-17 11:42:40 -0700814 struct fc_frame_header *fh = NULL;
815 struct fip_desc *desc;
816 struct fip_encaps *els;
817 struct fcoe_dev_stats *stats;
818 enum fip_desc_type els_dtype = 0;
819 u8 els_op;
820 u8 sub;
821 u8 granted_mac[ETH_ALEN] = { 0 };
822 size_t els_len = 0;
823 size_t rlen;
824 size_t dlen;
825
826 fiph = (struct fip_header *)skb->data;
827 sub = fiph->fip_subcode;
828 if (sub != FIP_SC_REQ && sub != FIP_SC_REP)
829 goto drop;
830
831 rlen = ntohs(fiph->fip_dl_len) * 4;
832 if (rlen + sizeof(*fiph) > skb->len)
833 goto drop;
834
835 desc = (struct fip_desc *)(fiph + 1);
836 while (rlen > 0) {
837 dlen = desc->fip_dlen * FIP_BPW;
838 if (dlen < sizeof(*desc) || dlen > rlen)
839 goto drop;
840 switch (desc->fip_dtype) {
841 case FIP_DT_MAC:
842 if (dlen != sizeof(struct fip_mac_desc))
843 goto len_err;
844 memcpy(granted_mac,
845 ((struct fip_mac_desc *)desc)->fd_mac,
846 ETH_ALEN);
847 if (!is_valid_ether_addr(granted_mac)) {
Joe Eykholt0f51c2e2009-11-03 11:48:16 -0800848 LIBFCOE_FIP_DBG(fip, "Invalid MAC address "
Robert Love650bd122009-06-10 15:31:05 -0700849 "in FIP ELS\n");
Joe Eykholt97c83892009-03-17 11:42:40 -0700850 goto drop;
851 }
Chris Leech11b56182009-11-03 11:46:29 -0800852 memcpy(fr_cb(fp)->granted_mac, granted_mac, ETH_ALEN);
Joe Eykholt97c83892009-03-17 11:42:40 -0700853 break;
854 case FIP_DT_FLOGI:
855 case FIP_DT_FDISC:
856 case FIP_DT_LOGO:
857 case FIP_DT_ELP:
858 if (fh)
859 goto drop;
860 if (dlen < sizeof(*els) + sizeof(*fh) + 1)
861 goto len_err;
862 els_len = dlen - sizeof(*els);
863 els = (struct fip_encaps *)desc;
864 fh = (struct fc_frame_header *)(els + 1);
865 els_dtype = desc->fip_dtype;
866 break;
867 default:
Joe Eykholt0f51c2e2009-11-03 11:48:16 -0800868 LIBFCOE_FIP_DBG(fip, "unexpected descriptor type %x "
Robert Love650bd122009-06-10 15:31:05 -0700869 "in FIP adv\n", desc->fip_dtype);
Joe Eykholt97c83892009-03-17 11:42:40 -0700870 /* standard says ignore unknown descriptors >= 128 */
871 if (desc->fip_dtype < FIP_DT_VENDOR_BASE)
872 goto drop;
873 continue;
874 }
875 desc = (struct fip_desc *)((char *)desc + dlen);
876 rlen -= dlen;
877 }
878
879 if (!fh)
880 goto drop;
881 els_op = *(u8 *)(fh + 1);
882
Chris Leech11b56182009-11-03 11:46:29 -0800883 if (els_dtype == FIP_DT_FLOGI && sub == FIP_SC_REP &&
884 fip->flogi_oxid == ntohs(fh->fh_ox_id) &&
885 els_op == ELS_LS_ACC && is_valid_ether_addr(granted_mac))
Joe Eykholt97c83892009-03-17 11:42:40 -0700886 fip->flogi_oxid = FC_XID_UNKNOWN;
Joe Eykholt97c83892009-03-17 11:42:40 -0700887
888 /*
889 * Convert skb into an fc_frame containing only the ELS.
890 */
891 skb_pull(skb, (u8 *)fh - skb->data);
892 skb_trim(skb, els_len);
893 fp = (struct fc_frame *)skb;
894 fc_frame_init(fp);
895 fr_sof(fp) = FC_SOF_I3;
896 fr_eof(fp) = FC_EOF_T;
Robert Love70b51aa2009-11-03 11:47:45 -0800897 fr_dev(fp) = lport;
Joe Eykholt97c83892009-03-17 11:42:40 -0700898
Robert Love70b51aa2009-11-03 11:47:45 -0800899 stats = fc_lport_get_stats(lport);
Joe Eykholt97c83892009-03-17 11:42:40 -0700900 stats->RxFrames++;
901 stats->RxWords += skb->len / FIP_BPW;
902
Robert Love70b51aa2009-11-03 11:47:45 -0800903 fc_exch_recv(lport, fp);
Joe Eykholt97c83892009-03-17 11:42:40 -0700904 return;
905
906len_err:
Joe Eykholt0f51c2e2009-11-03 11:48:16 -0800907 LIBFCOE_FIP_DBG(fip, "FIP length error in descriptor type %x len %zu\n",
Robert Love650bd122009-06-10 15:31:05 -0700908 desc->fip_dtype, dlen);
Joe Eykholt97c83892009-03-17 11:42:40 -0700909drop:
910 kfree_skb(skb);
911}
912
913/**
Robert Love70b51aa2009-11-03 11:47:45 -0800914 * fcoe_ctlr_recv_els() - Handle an incoming link reset frame
915 * @fip: The FCoE controller that received the frame
916 * @fh: The received FIP header
Joe Eykholt97c83892009-03-17 11:42:40 -0700917 *
918 * There may be multiple VN_Port descriptors.
919 * The overall length has already been checked.
920 */
921static void fcoe_ctlr_recv_clr_vlink(struct fcoe_ctlr *fip,
Robert Love70b51aa2009-11-03 11:47:45 -0800922 struct fip_header *fh)
Joe Eykholt97c83892009-03-17 11:42:40 -0700923{
924 struct fip_desc *desc;
925 struct fip_mac_desc *mp;
926 struct fip_wwn_desc *wp;
927 struct fip_vn_desc *vp;
928 size_t rlen;
929 size_t dlen;
930 struct fcoe_fcf *fcf = fip->sel_fcf;
Robert Love70b51aa2009-11-03 11:47:45 -0800931 struct fc_lport *lport = fip->lp;
Joe Eykholt97c83892009-03-17 11:42:40 -0700932 u32 desc_mask;
933
Joe Eykholt0f51c2e2009-11-03 11:48:16 -0800934 LIBFCOE_FIP_DBG(fip, "Clear Virtual Link received\n");
Joe Eykholt97c83892009-03-17 11:42:40 -0700935 if (!fcf)
936 return;
Robert Love70b51aa2009-11-03 11:47:45 -0800937 if (!fcf || !fc_host_port_id(lport->host))
Joe Eykholt97c83892009-03-17 11:42:40 -0700938 return;
939
940 /*
941 * mask of required descriptors. Validating each one clears its bit.
942 */
943 desc_mask = BIT(FIP_DT_MAC) | BIT(FIP_DT_NAME) | BIT(FIP_DT_VN_ID);
944
945 rlen = ntohs(fh->fip_dl_len) * FIP_BPW;
946 desc = (struct fip_desc *)(fh + 1);
947 while (rlen >= sizeof(*desc)) {
948 dlen = desc->fip_dlen * FIP_BPW;
949 if (dlen > rlen)
950 return;
951 switch (desc->fip_dtype) {
952 case FIP_DT_MAC:
953 mp = (struct fip_mac_desc *)desc;
954 if (dlen < sizeof(*mp))
955 return;
956 if (compare_ether_addr(mp->fd_mac, fcf->fcf_mac))
957 return;
958 desc_mask &= ~BIT(FIP_DT_MAC);
959 break;
960 case FIP_DT_NAME:
961 wp = (struct fip_wwn_desc *)desc;
962 if (dlen < sizeof(*wp))
963 return;
964 if (get_unaligned_be64(&wp->fd_wwn) != fcf->switch_name)
965 return;
966 desc_mask &= ~BIT(FIP_DT_NAME);
967 break;
968 case FIP_DT_VN_ID:
969 vp = (struct fip_vn_desc *)desc;
970 if (dlen < sizeof(*vp))
971 return;
972 if (compare_ether_addr(vp->fd_mac,
Robert Love70b51aa2009-11-03 11:47:45 -0800973 fip->get_src_addr(lport)) == 0 &&
974 get_unaligned_be64(&vp->fd_wwpn) == lport->wwpn &&
975 ntoh24(vp->fd_fc_id) ==
976 fc_host_port_id(lport->host))
Joe Eykholt97c83892009-03-17 11:42:40 -0700977 desc_mask &= ~BIT(FIP_DT_VN_ID);
978 break;
979 default:
980 /* standard says ignore unknown descriptors >= 128 */
981 if (desc->fip_dtype < FIP_DT_VENDOR_BASE)
982 return;
983 break;
984 }
985 desc = (struct fip_desc *)((char *)desc + dlen);
986 rlen -= dlen;
987 }
988
989 /*
990 * reset only if all required descriptors were present and valid.
991 */
992 if (desc_mask) {
Joe Eykholt0f51c2e2009-11-03 11:48:16 -0800993 LIBFCOE_FIP_DBG(fip, "missing descriptors mask %x\n",
994 desc_mask);
Joe Eykholt97c83892009-03-17 11:42:40 -0700995 } else {
Joe Eykholt0f51c2e2009-11-03 11:48:16 -0800996 LIBFCOE_FIP_DBG(fip, "performing Clear Virtual Link\n");
Joe Eykholt97c83892009-03-17 11:42:40 -0700997 fcoe_ctlr_reset(fip, FIP_ST_ENABLED);
998 }
999}
1000
1001/**
Robert Love70b51aa2009-11-03 11:47:45 -08001002 * fcoe_ctlr_recv() - Receive a FIP packet
1003 * @fip: The FCoE controller that received the packet
1004 * @skb: The received FIP packet
Joe Eykholt97c83892009-03-17 11:42:40 -07001005 *
Joe Eykholt1f4aed82009-11-03 11:48:22 -08001006 * This may be called from either NET_RX_SOFTIRQ or IRQ.
Joe Eykholt97c83892009-03-17 11:42:40 -07001007 */
1008void fcoe_ctlr_recv(struct fcoe_ctlr *fip, struct sk_buff *skb)
1009{
Joe Eykholt1f4aed82009-11-03 11:48:22 -08001010 skb_queue_tail(&fip->fip_recv_list, skb);
Joe Eykholt97c83892009-03-17 11:42:40 -07001011 schedule_work(&fip->recv_work);
1012}
1013EXPORT_SYMBOL(fcoe_ctlr_recv);
1014
1015/**
Robert Love70b51aa2009-11-03 11:47:45 -08001016 * fcoe_ctlr_recv_handler() - Receive a FIP frame
1017 * @fip: The FCoE controller that received the frame
1018 * @skb: The received FIP frame
Joe Eykholt97c83892009-03-17 11:42:40 -07001019 *
1020 * Returns non-zero if the frame is dropped.
1021 */
1022static int fcoe_ctlr_recv_handler(struct fcoe_ctlr *fip, struct sk_buff *skb)
1023{
1024 struct fip_header *fiph;
1025 struct ethhdr *eh;
1026 enum fip_state state;
1027 u16 op;
1028 u8 sub;
1029
1030 if (skb_linearize(skb))
1031 goto drop;
1032 if (skb->len < sizeof(*fiph))
1033 goto drop;
1034 eh = eth_hdr(skb);
1035 if (compare_ether_addr(eh->h_dest, fip->ctl_src_addr) &&
1036 compare_ether_addr(eh->h_dest, FIP_ALL_ENODE_MACS))
1037 goto drop;
1038 fiph = (struct fip_header *)skb->data;
1039 op = ntohs(fiph->fip_op);
1040 sub = fiph->fip_subcode;
1041
Joe Eykholt97c83892009-03-17 11:42:40 -07001042 if (FIP_VER_DECAPS(fiph->fip_ver) != FIP_VER)
1043 goto drop;
1044 if (ntohs(fiph->fip_dl_len) * FIP_BPW + sizeof(*fiph) > skb->len)
1045 goto drop;
1046
1047 spin_lock_bh(&fip->lock);
1048 state = fip->state;
1049 if (state == FIP_ST_AUTO) {
1050 fip->map_dest = 0;
1051 fip->state = FIP_ST_ENABLED;
1052 state = FIP_ST_ENABLED;
Joe Eykholt0f51c2e2009-11-03 11:48:16 -08001053 LIBFCOE_FIP_DBG(fip, "Using FIP mode\n");
Joe Eykholt97c83892009-03-17 11:42:40 -07001054 }
1055 spin_unlock_bh(&fip->lock);
1056 if (state != FIP_ST_ENABLED)
1057 goto drop;
1058
1059 if (op == FIP_OP_LS) {
1060 fcoe_ctlr_recv_els(fip, skb); /* consumes skb */
1061 return 0;
1062 }
1063 if (op == FIP_OP_DISC && sub == FIP_SC_ADV)
1064 fcoe_ctlr_recv_adv(fip, skb);
1065 else if (op == FIP_OP_CTRL && sub == FIP_SC_CLR_VLINK)
1066 fcoe_ctlr_recv_clr_vlink(fip, fiph);
1067 kfree_skb(skb);
1068 return 0;
1069drop:
1070 kfree_skb(skb);
1071 return -1;
1072}
1073
1074/**
Robert Love70b51aa2009-11-03 11:47:45 -08001075 * fcoe_ctlr_select() - Select the best FCF (if possible)
1076 * @fip: The FCoE controller
Joe Eykholt97c83892009-03-17 11:42:40 -07001077 *
1078 * If there are conflicting advertisements, no FCF can be chosen.
1079 *
1080 * Called with lock held.
1081 */
1082static void fcoe_ctlr_select(struct fcoe_ctlr *fip)
1083{
1084 struct fcoe_fcf *fcf;
1085 struct fcoe_fcf *best = NULL;
1086
1087 list_for_each_entry(fcf, &fip->fcfs, list) {
Joe Eykholt0f51c2e2009-11-03 11:48:16 -08001088 LIBFCOE_FIP_DBG(fip, "consider FCF for fab %llx VFID %d map %x "
Robert Love650bd122009-06-10 15:31:05 -07001089 "val %d\n", fcf->fabric_name, fcf->vfid,
1090 fcf->fc_map, fcoe_ctlr_mtu_valid(fcf));
Joe Eykholt97c83892009-03-17 11:42:40 -07001091 if (!fcoe_ctlr_fcf_usable(fcf)) {
Joe Eykholt0f51c2e2009-11-03 11:48:16 -08001092 LIBFCOE_FIP_DBG(fip, "FCF for fab %llx map %x %svalid "
Robert Love650bd122009-06-10 15:31:05 -07001093 "%savailable\n", fcf->fabric_name,
1094 fcf->fc_map, (fcf->flags & FIP_FL_SOL)
1095 ? "" : "in", (fcf->flags & FIP_FL_AVAIL)
1096 ? "" : "un");
Joe Eykholt97c83892009-03-17 11:42:40 -07001097 continue;
1098 }
1099 if (!best) {
1100 best = fcf;
1101 continue;
1102 }
1103 if (fcf->fabric_name != best->fabric_name ||
1104 fcf->vfid != best->vfid ||
1105 fcf->fc_map != best->fc_map) {
Joe Eykholt0f51c2e2009-11-03 11:48:16 -08001106 LIBFCOE_FIP_DBG(fip, "Conflicting fabric, VFID, "
Robert Love650bd122009-06-10 15:31:05 -07001107 "or FC-MAP\n");
Joe Eykholt97c83892009-03-17 11:42:40 -07001108 return;
1109 }
1110 if (fcf->pri < best->pri)
1111 best = fcf;
1112 }
1113 fip->sel_fcf = best;
1114}
1115
1116/**
Robert Love70b51aa2009-11-03 11:47:45 -08001117 * fcoe_ctlr_timeout() - FIP timeout handler
1118 * @arg: The FCoE controller that timed out
Joe Eykholt97c83892009-03-17 11:42:40 -07001119 *
1120 * Ages FCFs. Triggers FCF selection if possible. Sends keep-alives.
1121 */
1122static void fcoe_ctlr_timeout(unsigned long arg)
1123{
1124 struct fcoe_ctlr *fip = (struct fcoe_ctlr *)arg;
1125 struct fcoe_fcf *sel;
1126 struct fcoe_fcf *fcf;
1127 unsigned long next_timer = jiffies + msecs_to_jiffies(FIP_VN_KA_PERIOD);
Joe Eykholt97c83892009-03-17 11:42:40 -07001128
1129 spin_lock_bh(&fip->lock);
1130 if (fip->state == FIP_ST_DISABLED) {
1131 spin_unlock_bh(&fip->lock);
1132 return;
1133 }
1134
1135 fcf = fip->sel_fcf;
1136 fcoe_ctlr_age_fcfs(fip);
1137
1138 sel = fip->sel_fcf;
1139 if (!sel && fip->sel_time && time_after_eq(jiffies, fip->sel_time)) {
1140 fcoe_ctlr_select(fip);
1141 sel = fip->sel_fcf;
1142 fip->sel_time = 0;
1143 }
1144
1145 if (sel != fcf) {
1146 fcf = sel; /* the old FCF may have been freed */
1147 if (sel) {
Robert Love650bd122009-06-10 15:31:05 -07001148 printk(KERN_INFO "libfcoe: host%d: FIP selected "
Johannes Berg66d6fae2009-07-15 17:21:14 +02001149 "Fibre-Channel Forwarder MAC %pM\n",
1150 fip->lp->host->host_no, sel->fcf_mac);
Joe Eykholt97c83892009-03-17 11:42:40 -07001151 memcpy(fip->dest_addr, sel->fcf_mac, ETH_ALEN);
1152 fip->port_ka_time = jiffies +
Robert Love70b51aa2009-11-03 11:47:45 -08001153 msecs_to_jiffies(FIP_VN_KA_PERIOD);
Joe Eykholt97c83892009-03-17 11:42:40 -07001154 fip->ctlr_ka_time = jiffies + sel->fka_period;
1155 fip->link = 1;
1156 } else {
Robert Love650bd122009-06-10 15:31:05 -07001157 printk(KERN_NOTICE "libfcoe: host%d: "
Robert Love70b51aa2009-11-03 11:47:45 -08001158 "FIP Fibre-Channel Forwarder timed out. "
Joe Eykholt97c83892009-03-17 11:42:40 -07001159 "Starting FCF discovery.\n",
1160 fip->lp->host->host_no);
1161 fip->link = 0;
1162 }
1163 schedule_work(&fip->link_work);
1164 }
1165
Joe Eykholt97c83892009-03-17 11:42:40 -07001166 if (sel) {
1167 if (time_after_eq(jiffies, fip->ctlr_ka_time)) {
1168 fip->ctlr_ka_time = jiffies + sel->fka_period;
Chris Leech11b56182009-11-03 11:46:29 -08001169 fip->send_ctlr_ka = 1;
Joe Eykholt97c83892009-03-17 11:42:40 -07001170 }
1171 if (time_after(next_timer, fip->ctlr_ka_time))
1172 next_timer = fip->ctlr_ka_time;
1173
1174 if (time_after_eq(jiffies, fip->port_ka_time)) {
1175 fip->port_ka_time += jiffies +
Robert Love70b51aa2009-11-03 11:47:45 -08001176 msecs_to_jiffies(FIP_VN_KA_PERIOD);
Chris Leech11b56182009-11-03 11:46:29 -08001177 fip->send_port_ka = 1;
Joe Eykholt97c83892009-03-17 11:42:40 -07001178 }
1179 if (time_after(next_timer, fip->port_ka_time))
1180 next_timer = fip->port_ka_time;
1181 mod_timer(&fip->timer, next_timer);
1182 } else if (fip->sel_time) {
1183 next_timer = fip->sel_time +
Robert Love70b51aa2009-11-03 11:47:45 -08001184 msecs_to_jiffies(FCOE_CTLR_START_DELAY);
Joe Eykholt97c83892009-03-17 11:42:40 -07001185 mod_timer(&fip->timer, next_timer);
1186 }
Chris Leech11b56182009-11-03 11:46:29 -08001187 if (fip->send_ctlr_ka || fip->send_port_ka)
1188 schedule_work(&fip->link_work);
Joe Eykholt97c83892009-03-17 11:42:40 -07001189 spin_unlock_bh(&fip->lock);
Joe Eykholt97c83892009-03-17 11:42:40 -07001190}
1191
1192/**
Robert Love70b51aa2009-11-03 11:47:45 -08001193 * fcoe_ctlr_link_work() - Worker thread function for link changes
1194 * @work: Handle to a FCoE controller
Joe Eykholt97c83892009-03-17 11:42:40 -07001195 *
1196 * See if the link status has changed and if so, report it.
1197 *
1198 * This is here because fc_linkup() and fc_linkdown() must not
1199 * be called from the timer directly, since they use a mutex.
1200 */
1201static void fcoe_ctlr_link_work(struct work_struct *work)
1202{
1203 struct fcoe_ctlr *fip;
Chris Leech11b56182009-11-03 11:46:29 -08001204 struct fc_lport *vport;
1205 u8 *mac;
Joe Eykholt97c83892009-03-17 11:42:40 -07001206 int link;
1207 int last_link;
1208
1209 fip = container_of(work, struct fcoe_ctlr, link_work);
1210 spin_lock_bh(&fip->lock);
1211 last_link = fip->last_link;
1212 link = fip->link;
1213 fip->last_link = link;
1214 spin_unlock_bh(&fip->lock);
1215
1216 if (last_link != link) {
1217 if (link)
1218 fc_linkup(fip->lp);
1219 else
1220 fcoe_ctlr_reset(fip, FIP_ST_LINK_WAIT);
1221 }
Chris Leech11b56182009-11-03 11:46:29 -08001222
1223 if (fip->send_ctlr_ka) {
1224 fip->send_ctlr_ka = 0;
1225 fcoe_ctlr_send_keep_alive(fip, NULL, 0, fip->ctl_src_addr);
1226 }
1227 if (fip->send_port_ka) {
1228 fip->send_port_ka = 0;
1229 mutex_lock(&fip->lp->lp_mutex);
1230 mac = fip->get_src_addr(fip->lp);
1231 fcoe_ctlr_send_keep_alive(fip, fip->lp, 1, mac);
1232 list_for_each_entry(vport, &fip->lp->vports, list) {
1233 mac = fip->get_src_addr(vport);
1234 fcoe_ctlr_send_keep_alive(fip, vport, 1, mac);
1235 }
1236 mutex_unlock(&fip->lp->lp_mutex);
1237 }
Joe Eykholt97c83892009-03-17 11:42:40 -07001238}
1239
1240/**
Robert Love70b51aa2009-11-03 11:47:45 -08001241 * fcoe_ctlr_recv_work() - Worker thread function for receiving FIP frames
1242 * @recv_work: Handle to a FCoE controller
Joe Eykholt97c83892009-03-17 11:42:40 -07001243 */
1244static void fcoe_ctlr_recv_work(struct work_struct *recv_work)
1245{
1246 struct fcoe_ctlr *fip;
1247 struct sk_buff *skb;
1248
1249 fip = container_of(recv_work, struct fcoe_ctlr, recv_work);
Joe Eykholt1f4aed82009-11-03 11:48:22 -08001250 while ((skb = skb_dequeue(&fip->fip_recv_list)))
Joe Eykholt97c83892009-03-17 11:42:40 -07001251 fcoe_ctlr_recv_handler(fip, skb);
Joe Eykholt97c83892009-03-17 11:42:40 -07001252}
1253
1254/**
Robert Love70b51aa2009-11-03 11:47:45 -08001255 * fcoe_ctlr_recv_flogi() - Snoop pre-FIP receipt of FLOGI response or request
1256 * @fip: The FCoE controller
1257 * @fp: The FC frame to snoop
1258 * @sa: Ethernet source MAC address from received FCoE frame
Joe Eykholt97c83892009-03-17 11:42:40 -07001259 *
1260 * Snoop potential response to FLOGI or even incoming FLOGI.
1261 *
1262 * The caller has checked that we are waiting for login as indicated
1263 * by fip->flogi_oxid != FC_XID_UNKNOWN.
1264 *
1265 * The caller is responsible for freeing the frame.
1266 *
1267 * Return non-zero if the frame should not be delivered to libfc.
1268 */
Chris Leech11b56182009-11-03 11:46:29 -08001269int fcoe_ctlr_recv_flogi(struct fcoe_ctlr *fip, struct fc_lport *lport,
1270 struct fc_frame *fp, u8 *sa)
Joe Eykholt97c83892009-03-17 11:42:40 -07001271{
1272 struct fc_frame_header *fh;
1273 u8 op;
1274 u8 mac[ETH_ALEN];
1275
1276 fh = fc_frame_header_get(fp);
1277 if (fh->fh_type != FC_TYPE_ELS)
1278 return 0;
1279
1280 op = fc_frame_payload_op(fp);
1281 if (op == ELS_LS_ACC && fh->fh_r_ctl == FC_RCTL_ELS_REP &&
1282 fip->flogi_oxid == ntohs(fh->fh_ox_id)) {
1283
1284 spin_lock_bh(&fip->lock);
1285 if (fip->state != FIP_ST_AUTO && fip->state != FIP_ST_NON_FIP) {
1286 spin_unlock_bh(&fip->lock);
1287 return -EINVAL;
1288 }
1289 fip->state = FIP_ST_NON_FIP;
Joe Eykholt0f51c2e2009-11-03 11:48:16 -08001290 LIBFCOE_FIP_DBG(fip,
1291 "received FLOGI LS_ACC using non-FIP mode\n");
Joe Eykholt97c83892009-03-17 11:42:40 -07001292
1293 /*
1294 * FLOGI accepted.
1295 * If the src mac addr is FC_OUI-based, then we mark the
1296 * address_mode flag to use FC_OUI-based Ethernet DA.
1297 * Otherwise we use the FCoE gateway addr
1298 */
1299 if (!compare_ether_addr(sa, (u8[6])FC_FCOE_FLOGI_MAC)) {
1300 fip->map_dest = 1;
1301 } else {
1302 memcpy(fip->dest_addr, sa, ETH_ALEN);
1303 fip->map_dest = 0;
1304 }
1305 fip->flogi_oxid = FC_XID_UNKNOWN;
Chris Leech11b56182009-11-03 11:46:29 -08001306 fc_fcoe_set_mac(mac, fh->fh_d_id);
1307 fip->update_mac(lport, mac);
Joe Eykholt97c83892009-03-17 11:42:40 -07001308 spin_unlock_bh(&fip->lock);
Joe Eykholt97c83892009-03-17 11:42:40 -07001309 } else if (op == ELS_FLOGI && fh->fh_r_ctl == FC_RCTL_ELS_REQ && sa) {
1310 /*
1311 * Save source MAC for point-to-point responses.
1312 */
1313 spin_lock_bh(&fip->lock);
1314 if (fip->state == FIP_ST_AUTO || fip->state == FIP_ST_NON_FIP) {
1315 memcpy(fip->dest_addr, sa, ETH_ALEN);
1316 fip->map_dest = 0;
1317 if (fip->state == FIP_ST_NON_FIP)
Joe Eykholt0f51c2e2009-11-03 11:48:16 -08001318 LIBFCOE_FIP_DBG(fip, "received FLOGI REQ, "
Joe Eykholt97c83892009-03-17 11:42:40 -07001319 "using non-FIP mode\n");
1320 fip->state = FIP_ST_NON_FIP;
1321 }
1322 spin_unlock_bh(&fip->lock);
1323 }
1324 return 0;
1325}
1326EXPORT_SYMBOL(fcoe_ctlr_recv_flogi);
1327
Vasu Dev5e80f7f2009-03-17 11:42:18 -07001328/**
Robert Love70b51aa2009-11-03 11:47:45 -08001329 * fcoe_wwn_from_mac() - Converts a 48-bit IEEE MAC address to a 64-bit FC WWN
1330 * @mac: The MAC address to convert
1331 * @scheme: The scheme to use when converting
1332 * @port: The port indicator for converting
Vasu Dev5e80f7f2009-03-17 11:42:18 -07001333 *
1334 * Returns: u64 fc world wide name
1335 */
1336u64 fcoe_wwn_from_mac(unsigned char mac[MAX_ADDR_LEN],
1337 unsigned int scheme, unsigned int port)
1338{
1339 u64 wwn;
1340 u64 host_mac;
1341
1342 /* The MAC is in NO, so flip only the low 48 bits */
1343 host_mac = ((u64) mac[0] << 40) |
1344 ((u64) mac[1] << 32) |
1345 ((u64) mac[2] << 24) |
1346 ((u64) mac[3] << 16) |
1347 ((u64) mac[4] << 8) |
1348 (u64) mac[5];
1349
1350 WARN_ON(host_mac >= (1ULL << 48));
1351 wwn = host_mac | ((u64) scheme << 60);
1352 switch (scheme) {
1353 case 1:
1354 WARN_ON(port != 0);
1355 break;
1356 case 2:
1357 WARN_ON(port >= 0xfff);
1358 wwn |= (u64) port << 48;
1359 break;
1360 default:
1361 WARN_ON(1);
1362 break;
1363 }
1364
1365 return wwn;
1366}
1367EXPORT_SYMBOL_GPL(fcoe_wwn_from_mac);
1368
1369/**
Robert Love70b51aa2009-11-03 11:47:45 -08001370 * fcoe_libfc_config() - Sets up libfc related properties for local port
1371 * @lp: The local port to configure libfc for
1372 * @tt: The libfc function template
Vasu Dev5e80f7f2009-03-17 11:42:18 -07001373 *
1374 * Returns : 0 for success
1375 */
Robert Love70b51aa2009-11-03 11:47:45 -08001376int fcoe_libfc_config(struct fc_lport *lport,
1377 struct libfc_function_template *tt)
Vasu Dev5e80f7f2009-03-17 11:42:18 -07001378{
1379 /* Set the function pointers set by the LLDD */
Robert Love70b51aa2009-11-03 11:47:45 -08001380 memcpy(&lport->tt, tt, sizeof(*tt));
1381 if (fc_fcp_init(lport))
Vasu Dev5e80f7f2009-03-17 11:42:18 -07001382 return -ENOMEM;
Robert Love70b51aa2009-11-03 11:47:45 -08001383 fc_exch_init(lport);
1384 fc_elsct_init(lport);
1385 fc_lport_init(lport);
1386 fc_rport_init(lport);
1387 fc_disc_init(lport);
Vasu Dev5e80f7f2009-03-17 11:42:18 -07001388
1389 return 0;
1390}
1391EXPORT_SYMBOL_GPL(fcoe_libfc_config);
Chris Leech11b56182009-11-03 11:46:29 -08001392