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