2 * Copyright(c) 2007 - 2008 Intel Corporation. All rights reserved.
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms and conditions of the GNU General Public License,
6 * version 2, as published by the Free Software Foundation.
8 * This program is distributed in the hope it will be useful, but WITHOUT
9 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
10 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
13 * You should have received a copy of the GNU General Public License along with
14 * this program; if not, write to the Free Software Foundation, Inc.,
15 * 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
17 * Maintained at www.Open-FCoE.org
20 #include <linux/module.h>
21 #include <linux/version.h>
22 #include <linux/kernel.h>
23 #include <linux/spinlock.h>
24 #include <linux/skbuff.h>
25 #include <linux/netdevice.h>
26 #include <linux/etherdevice.h>
27 #include <linux/ethtool.h>
28 #include <linux/if_ether.h>
29 #include <linux/if_vlan.h>
30 #include <linux/kthread.h>
31 #include <linux/crc32.h>
32 #include <linux/cpu.h>
34 #include <linux/sysfs.h>
35 #include <linux/ctype.h>
36 #include <scsi/scsi_tcq.h>
37 #include <scsi/scsicam.h>
38 #include <scsi/scsi_transport.h>
39 #include <scsi/scsi_transport_fc.h>
40 #include <net/rtnetlink.h>
42 #include <scsi/fc/fc_encaps.h>
44 #include <scsi/libfc.h>
45 #include <scsi/fc_frame.h>
46 #include <scsi/libfcoe.h>
47 #include <scsi/fc_transport_fcoe.h>
49 static int debug_fcoe
;
51 #define FCOE_MAX_QUEUE_DEPTH 256
52 #define FCOE_LOW_QUEUE_DEPTH 32
54 /* destination address mode */
55 #define FCOE_GW_ADDR_MODE 0x00
56 #define FCOE_FCOUI_ADDR_MODE 0x01
58 #define FCOE_WORD_TO_BYTE 4
60 MODULE_AUTHOR("Open-FCoE.org");
61 MODULE_DESCRIPTION("FCoE");
62 MODULE_LICENSE("GPL");
65 LIST_HEAD(fcoe_hostlist
);
66 DEFINE_RWLOCK(fcoe_hostlist_lock
);
67 DEFINE_TIMER(fcoe_timer
, NULL
, 0, 0);
68 DEFINE_PER_CPU(struct fcoe_percpu_s
, fcoe_percpu
);
71 /* Function Prototyes */
72 static int fcoe_check_wait_queue(struct fc_lport
*);
73 static void fcoe_recv_flogi(struct fcoe_softc
*, struct fc_frame
*, u8
*);
74 static int fcoe_device_notification(struct notifier_block
*, ulong
, void *);
75 static void fcoe_dev_setup(void);
76 static void fcoe_dev_cleanup(void);
78 /* notification function from net device */
79 static struct notifier_block fcoe_notifier
= {
80 .notifier_call
= fcoe_device_notification
,
84 * fcoe_percpu_thread_create() - Create a receive thread for an online cpu
85 * @cpu: cpu index for the online cpu
87 static void fcoe_percpu_thread_create(unsigned int cpu
)
89 struct fcoe_percpu_s
*p
;
90 struct task_struct
*thread
;
92 p
= &per_cpu(fcoe_percpu
, cpu
);
94 thread
= kthread_create(fcoe_percpu_receive_thread
,
95 (void *)p
, "fcoethread/%d", cpu
);
97 if (likely(!IS_ERR(p
->thread
))) {
98 kthread_bind(thread
, cpu
);
99 wake_up_process(thread
);
101 spin_lock_bh(&p
->fcoe_rx_list
.lock
);
103 spin_unlock_bh(&p
->fcoe_rx_list
.lock
);
108 * fcoe_percpu_thread_destroy() - removes the rx thread for the given cpu
109 * @cpu: cpu index the rx thread is to be removed
111 * Destroys a per-CPU Rx thread. Any pending skbs are moved to the
112 * current CPU's Rx thread. If the thread being destroyed is bound to
113 * the CPU processing this context the skbs will be freed.
115 static void fcoe_percpu_thread_destroy(unsigned int cpu
)
117 struct fcoe_percpu_s
*p
;
118 struct task_struct
*thread
;
119 struct page
*crc_eof
;
122 struct fcoe_percpu_s
*p0
;
123 unsigned targ_cpu
= smp_processor_id();
124 #endif /* CONFIG_SMP */
126 printk(KERN_DEBUG
"fcoe: Destroying receive thread for CPU %d\n", cpu
);
128 /* Prevent any new skbs from being queued for this CPU. */
129 p
= &per_cpu(fcoe_percpu
, cpu
);
130 spin_lock_bh(&p
->fcoe_rx_list
.lock
);
133 crc_eof
= p
->crc_eof_page
;
134 p
->crc_eof_page
= NULL
;
135 p
->crc_eof_offset
= 0;
136 spin_unlock_bh(&p
->fcoe_rx_list
.lock
);
140 * Don't bother moving the skb's if this context is running
141 * on the same CPU that is having its thread destroyed. This
142 * can easily happen when the module is removed.
144 if (cpu
!= targ_cpu
) {
145 p0
= &per_cpu(fcoe_percpu
, targ_cpu
);
146 spin_lock_bh(&p0
->fcoe_rx_list
.lock
);
148 FC_DBG("Moving frames from CPU %d to CPU %d\n",
151 while ((skb
= __skb_dequeue(&p
->fcoe_rx_list
)) != NULL
)
152 __skb_queue_tail(&p0
->fcoe_rx_list
, skb
);
153 spin_unlock_bh(&p0
->fcoe_rx_list
.lock
);
156 * The targeted CPU is not initialized and cannot accept
157 * new skbs. Unlock the targeted CPU and drop the skbs
158 * on the CPU that is going offline.
160 while ((skb
= __skb_dequeue(&p
->fcoe_rx_list
)) != NULL
)
162 spin_unlock_bh(&p0
->fcoe_rx_list
.lock
);
166 * This scenario occurs when the module is being removed
167 * and all threads are being destroyed. skbs will continue
168 * to be shifted from the CPU thread that is being removed
169 * to the CPU thread associated with the CPU that is processing
170 * the module removal. Once there is only one CPU Rx thread it
171 * will reach this case and we will drop all skbs and later
174 spin_lock_bh(&p
->fcoe_rx_list
.lock
);
175 while ((skb
= __skb_dequeue(&p
->fcoe_rx_list
)) != NULL
)
177 spin_unlock_bh(&p
->fcoe_rx_list
.lock
);
181 * This a non-SMP scenario where the singluar Rx thread is
182 * being removed. Free all skbs and stop the thread.
184 spin_lock_bh(&p
->fcoe_rx_list
.lock
);
185 while ((skb
= __skb_dequeue(&p
->fcoe_rx_list
)) != NULL
)
187 spin_unlock_bh(&p
->fcoe_rx_list
.lock
);
191 kthread_stop(thread
);
198 * fcoe_cpu_callback() - fcoe cpu hotplug event callback
199 * @nfb: callback data block
200 * @action: event triggering the callback
201 * @hcpu: index for the cpu of this event
203 * This creates or destroys per cpu data for fcoe
205 * Returns NOTIFY_OK always.
207 static int fcoe_cpu_callback(struct notifier_block
*nfb
,
208 unsigned long action
, void *hcpu
)
210 unsigned cpu
= (unsigned long)hcpu
;
214 case CPU_ONLINE_FROZEN
:
215 FC_DBG("CPU %x online: Create Rx thread\n", cpu
);
216 fcoe_percpu_thread_create(cpu
);
219 case CPU_DEAD_FROZEN
:
220 FC_DBG("CPU %x offline: Remove Rx thread\n", cpu
);
221 fcoe_percpu_thread_destroy(cpu
);
229 static struct notifier_block fcoe_cpu_notifier
= {
230 .notifier_call
= fcoe_cpu_callback
,
234 * fcoe_rcv() - this is the fcoe receive function called by NET_RX_SOFTIRQ
235 * @skb: the receive skb
236 * @dev: associated net device
238 * @odldev: last device
240 * this function will receive the packet and build fc frame and pass it up
242 * Returns: 0 for success
244 int fcoe_rcv(struct sk_buff
*skb
, struct net_device
*dev
,
245 struct packet_type
*ptype
, struct net_device
*olddev
)
248 struct fcoe_rcv_info
*fr
;
249 struct fcoe_softc
*fc
;
250 struct fc_frame_header
*fh
;
251 struct fcoe_percpu_s
*fps
;
253 unsigned int cpu
= 0;
255 fc
= container_of(ptype
, struct fcoe_softc
, fcoe_packet_type
);
257 if (unlikely(lp
== NULL
)) {
258 FC_DBG("cannot find hba structure");
262 if (unlikely(debug_fcoe
)) {
263 FC_DBG("skb_info: len:%d data_len:%d head:%p data:%p tail:%p "
264 "end:%p sum:%d dev:%s", skb
->len
, skb
->data_len
,
265 skb
->head
, skb
->data
, skb_tail_pointer(skb
),
266 skb_end_pointer(skb
), skb
->csum
,
267 skb
->dev
? skb
->dev
->name
: "<NULL>");
271 /* check for FCOE packet type */
272 if (unlikely(eth_hdr(skb
)->h_proto
!= htons(ETH_P_FCOE
))) {
273 FC_DBG("wrong FC type frame");
278 * Check for minimum frame length, and make sure required FCoE
279 * and FC headers are pulled into the linear data area.
281 if (unlikely((skb
->len
< FCOE_MIN_FRAME
) ||
282 !pskb_may_pull(skb
, FCOE_HEADER_LEN
)))
285 skb_set_transport_header(skb
, sizeof(struct fcoe_hdr
));
286 fh
= (struct fc_frame_header
*) skb_transport_header(skb
);
288 oxid
= ntohs(fh
->fh_ox_id
);
290 fr
= fcoe_dev_from_skb(skb
);
296 * The incoming frame exchange id(oxid) is ANDed with num of online
297 * cpu bits to get cpu and then this cpu is used for selecting
298 * a per cpu kernel thread from fcoe_percpu.
300 cpu
= oxid
& (num_online_cpus() - 1);
303 fps
= &per_cpu(fcoe_percpu
, cpu
);
304 spin_lock_bh(&fps
->fcoe_rx_list
.lock
);
305 if (unlikely(!fps
->thread
)) {
307 * The targeted CPU is not ready, let's target
308 * the first CPU now. For non-SMP systems this
309 * will check the same CPU twice.
311 FC_DBG("CPU is online, but no receive thread ready "
312 "for incoming skb- using first online CPU.\n");
314 spin_unlock_bh(&fps
->fcoe_rx_list
.lock
);
315 cpu
= first_cpu(cpu_online_map
);
316 fps
= &per_cpu(fcoe_percpu
, cpu
);
317 spin_lock_bh(&fps
->fcoe_rx_list
.lock
);
319 spin_unlock_bh(&fps
->fcoe_rx_list
.lock
);
325 * We now have a valid CPU that we're targeting for
326 * this skb. We also have this receive thread locked,
327 * so we're free to queue skbs into it's queue.
329 __skb_queue_tail(&fps
->fcoe_rx_list
, skb
);
330 if (fps
->fcoe_rx_list
.qlen
== 1)
331 wake_up_process(fps
->thread
);
333 spin_unlock_bh(&fps
->fcoe_rx_list
.lock
);
337 fc_lport_get_stats(lp
)->ErrorFrames
++;
343 EXPORT_SYMBOL_GPL(fcoe_rcv
);
346 * fcoe_start_io() - pass to netdev to start xmit for fcoe
347 * @skb: the skb to be xmitted
349 * Returns: 0 for success
351 static inline int fcoe_start_io(struct sk_buff
*skb
)
356 rc
= dev_queue_xmit(skb
);
364 * fcoe_get_paged_crc_eof() - in case we need alloc a page for crc_eof
365 * @skb: the skb to be xmitted
368 * Returns: 0 for success
370 static int fcoe_get_paged_crc_eof(struct sk_buff
*skb
, int tlen
)
372 struct fcoe_percpu_s
*fps
;
375 fps
= &get_cpu_var(fcoe_percpu
);
376 page
= fps
->crc_eof_page
;
378 page
= alloc_page(GFP_ATOMIC
);
380 put_cpu_var(fcoe_percpu
);
383 fps
->crc_eof_page
= page
;
384 fps
->crc_eof_offset
= 0;
388 skb_fill_page_desc(skb
, skb_shinfo(skb
)->nr_frags
, page
,
389 fps
->crc_eof_offset
, tlen
);
391 skb
->data_len
+= tlen
;
392 skb
->truesize
+= tlen
;
393 fps
->crc_eof_offset
+= sizeof(struct fcoe_crc_eof
);
395 if (fps
->crc_eof_offset
>= PAGE_SIZE
) {
396 fps
->crc_eof_page
= NULL
;
397 fps
->crc_eof_offset
= 0;
400 put_cpu_var(fcoe_percpu
);
405 * fcoe_fc_crc() - calculates FC CRC in this fcoe skb
406 * @fp: the fc_frame containg data to be checksummed
408 * This uses crc32() to calculate the crc for fc frame
409 * Return : 32 bit crc
411 u32
fcoe_fc_crc(struct fc_frame
*fp
)
413 struct sk_buff
*skb
= fp_skb(fp
);
414 struct skb_frag_struct
*frag
;
416 unsigned long off
, len
, clen
;
420 crc
= crc32(~0, skb
->data
, skb_headlen(skb
));
422 for (i
= 0; i
< skb_shinfo(skb
)->nr_frags
; i
++) {
423 frag
= &skb_shinfo(skb
)->frags
[i
];
424 off
= frag
->page_offset
;
427 clen
= min(len
, PAGE_SIZE
- (off
& ~PAGE_MASK
));
428 data
= kmap_atomic(frag
->page
+ (off
>> PAGE_SHIFT
),
429 KM_SKB_DATA_SOFTIRQ
);
430 crc
= crc32(crc
, data
+ (off
& ~PAGE_MASK
), clen
);
431 kunmap_atomic(data
, KM_SKB_DATA_SOFTIRQ
);
438 EXPORT_SYMBOL_GPL(fcoe_fc_crc
);
441 * fcoe_xmit() - FCoE frame transmit function
442 * @lp: the associated local port
443 * @fp: the fc_frame to be transmitted
445 * Return : 0 for success
447 int fcoe_xmit(struct fc_lport
*lp
, struct fc_frame
*fp
)
452 struct fcoe_crc_eof
*cp
;
454 struct fcoe_dev_stats
*stats
;
455 struct fc_frame_header
*fh
;
456 unsigned int hlen
; /* header length implies the version */
457 unsigned int tlen
; /* trailer length */
458 unsigned int elen
; /* eth header, may include vlan */
459 int flogi_in_progress
= 0;
460 struct fcoe_softc
*fc
;
464 WARN_ON((fr_len(fp
) % sizeof(u32
)) != 0);
468 * if it is a flogi then we need to learn gw-addr
471 fh
= fc_frame_header_get(fp
);
472 if (unlikely(fh
->fh_r_ctl
== FC_RCTL_ELS_REQ
)) {
473 if (fc_frame_payload_op(fp
) == ELS_FLOGI
) {
474 fc
->flogi_oxid
= ntohs(fh
->fh_ox_id
);
475 fc
->address_mode
= FCOE_FCOUI_ADDR_MODE
;
476 fc
->flogi_progress
= 1;
477 flogi_in_progress
= 1;
478 } else if (fc
->flogi_progress
&& ntoh24(fh
->fh_s_id
) != 0) {
480 * Here we must've gotten an SID by accepting an FLOGI
481 * from a point-to-point connection. Switch to using
482 * the source mac based on the SID. The destination
483 * MAC in this case would have been set by receving the
486 fc_fcoe_set_mac(fc
->data_src_addr
, fh
->fh_s_id
);
487 fc
->flogi_progress
= 0;
495 elen
= (fc
->real_dev
->priv_flags
& IFF_802_1Q_VLAN
) ?
496 sizeof(struct vlan_ethhdr
) : sizeof(struct ethhdr
);
497 hlen
= sizeof(struct fcoe_hdr
);
498 tlen
= sizeof(struct fcoe_crc_eof
);
499 wlen
= (skb
->len
- tlen
+ sizeof(crc
)) / FCOE_WORD_TO_BYTE
;
502 if (likely(lp
->crc_offload
)) {
503 skb
->ip_summed
= CHECKSUM_PARTIAL
;
504 skb
->csum_start
= skb_headroom(skb
);
505 skb
->csum_offset
= skb
->len
;
508 skb
->ip_summed
= CHECKSUM_NONE
;
509 crc
= fcoe_fc_crc(fp
);
512 /* copy fc crc and eof to the skb buff */
513 if (skb_is_nonlinear(skb
)) {
515 if (fcoe_get_paged_crc_eof(skb
, tlen
)) {
519 frag
= &skb_shinfo(skb
)->frags
[skb_shinfo(skb
)->nr_frags
- 1];
520 cp
= kmap_atomic(frag
->page
, KM_SKB_DATA_SOFTIRQ
)
523 cp
= (struct fcoe_crc_eof
*)skb_put(skb
, tlen
);
526 memset(cp
, 0, sizeof(*cp
));
528 cp
->fcoe_crc32
= cpu_to_le32(~crc
);
530 if (skb_is_nonlinear(skb
)) {
531 kunmap_atomic(cp
, KM_SKB_DATA_SOFTIRQ
);
535 /* adjust skb netowrk/transport offsets to match mac/fcoe/fc */
536 skb_push(skb
, elen
+ hlen
);
537 skb_reset_mac_header(skb
);
538 skb_reset_network_header(skb
);
540 skb
->protocol
= htons(ETH_P_FCOE
);
541 skb
->dev
= fc
->real_dev
;
543 /* fill up mac and fcoe headers */
545 eh
->h_proto
= htons(ETH_P_FCOE
);
546 if (fc
->address_mode
== FCOE_FCOUI_ADDR_MODE
)
547 fc_fcoe_set_mac(eh
->h_dest
, fh
->fh_d_id
);
549 /* insert GW address */
550 memcpy(eh
->h_dest
, fc
->dest_addr
, ETH_ALEN
);
552 if (unlikely(flogi_in_progress
))
553 memcpy(eh
->h_source
, fc
->ctl_src_addr
, ETH_ALEN
);
555 memcpy(eh
->h_source
, fc
->data_src_addr
, ETH_ALEN
);
557 hp
= (struct fcoe_hdr
*)(eh
+ 1);
558 memset(hp
, 0, sizeof(*hp
));
560 FC_FCOE_ENCAPS_VER(hp
, FC_FCOE_VER
);
564 /* fcoe lso, mss is in max_payload which is non-zero for FCP data */
565 if (lp
->seq_offload
&& fr_max_payload(fp
)) {
566 skb_shinfo(skb
)->gso_type
= SKB_GSO_FCOE
;
567 skb_shinfo(skb
)->gso_size
= fr_max_payload(fp
);
569 skb_shinfo(skb
)->gso_type
= 0;
570 skb_shinfo(skb
)->gso_size
= 0;
573 /* update tx stats: regardless if LLD fails */
574 stats
= fc_lport_get_stats(lp
);
576 stats
->TxWords
+= wlen
;
578 /* send down to lld */
580 if (fc
->fcoe_pending_queue
.qlen
)
581 rc
= fcoe_check_wait_queue(lp
);
584 rc
= fcoe_start_io(skb
);
587 spin_lock_bh(&fc
->fcoe_pending_queue
.lock
);
588 __skb_queue_tail(&fc
->fcoe_pending_queue
, skb
);
589 spin_unlock_bh(&fc
->fcoe_pending_queue
.lock
);
590 if (fc
->fcoe_pending_queue
.qlen
> FCOE_MAX_QUEUE_DEPTH
)
596 EXPORT_SYMBOL_GPL(fcoe_xmit
);
599 * fcoe_percpu_receive_thread() - recv thread per cpu
600 * @arg: ptr to the fcoe per cpu struct
602 * Return: 0 for success
604 int fcoe_percpu_receive_thread(void *arg
)
606 struct fcoe_percpu_s
*p
= arg
;
609 struct fcoe_rcv_info
*fr
;
610 struct fcoe_dev_stats
*stats
;
611 struct fc_frame_header
*fh
;
613 struct fcoe_crc_eof crc_eof
;
616 struct fcoe_softc
*fc
;
619 set_user_nice(current
, -20);
621 while (!kthread_should_stop()) {
623 spin_lock_bh(&p
->fcoe_rx_list
.lock
);
624 while ((skb
= __skb_dequeue(&p
->fcoe_rx_list
)) == NULL
) {
625 set_current_state(TASK_INTERRUPTIBLE
);
626 spin_unlock_bh(&p
->fcoe_rx_list
.lock
);
628 set_current_state(TASK_RUNNING
);
629 if (kthread_should_stop())
631 spin_lock_bh(&p
->fcoe_rx_list
.lock
);
633 spin_unlock_bh(&p
->fcoe_rx_list
.lock
);
634 fr
= fcoe_dev_from_skb(skb
);
636 if (unlikely(lp
== NULL
)) {
637 FC_DBG("invalid HBA Structure");
642 if (unlikely(debug_fcoe
)) {
643 FC_DBG("skb_info: len:%d data_len:%d head:%p data:%p "
644 "tail:%p end:%p sum:%d dev:%s",
645 skb
->len
, skb
->data_len
,
646 skb
->head
, skb
->data
, skb_tail_pointer(skb
),
647 skb_end_pointer(skb
), skb
->csum
,
648 skb
->dev
? skb
->dev
->name
: "<NULL>");
652 * Save source MAC address before discarding header.
655 if (unlikely(fc
->flogi_progress
))
656 mac
= eth_hdr(skb
)->h_source
;
658 if (skb_is_nonlinear(skb
))
659 skb_linearize(skb
); /* not ideal */
662 * Frame length checks and setting up the header pointers
663 * was done in fcoe_rcv already.
665 hp
= (struct fcoe_hdr
*) skb_network_header(skb
);
666 fh
= (struct fc_frame_header
*) skb_transport_header(skb
);
668 stats
= fc_lport_get_stats(lp
);
669 if (unlikely(FC_FCOE_DECAPS_VER(hp
) != FC_FCOE_VER
)) {
670 if (stats
->ErrorFrames
< 5)
671 printk(KERN_WARNING
"FCoE version "
672 "mismatch: The frame has "
673 "version %x, but the "
674 "initiator supports version "
675 "%x\n", FC_FCOE_DECAPS_VER(hp
),
677 stats
->ErrorFrames
++;
682 skb_pull(skb
, sizeof(struct fcoe_hdr
));
683 fr_len
= skb
->len
- sizeof(struct fcoe_crc_eof
);
686 stats
->RxWords
+= fr_len
/ FCOE_WORD_TO_BYTE
;
688 fp
= (struct fc_frame
*)skb
;
691 fr_sof(fp
) = hp
->fcoe_sof
;
693 /* Copy out the CRC and EOF trailer for access */
694 if (skb_copy_bits(skb
, fr_len
, &crc_eof
, sizeof(crc_eof
))) {
698 fr_eof(fp
) = crc_eof
.fcoe_eof
;
699 fr_crc(fp
) = crc_eof
.fcoe_crc32
;
700 if (pskb_trim(skb
, fr_len
)) {
706 * We only check CRC if no offload is available and if it is
707 * it's solicited data, in which case, the FCP layer would
708 * check it during the copy.
710 if (lp
->crc_offload
&& skb
->ip_summed
== CHECKSUM_UNNECESSARY
)
711 fr_flags(fp
) &= ~FCPHF_CRC_UNCHECKED
;
713 fr_flags(fp
) |= FCPHF_CRC_UNCHECKED
;
715 fh
= fc_frame_header_get(fp
);
716 if (fh
->fh_r_ctl
== FC_RCTL_DD_SOL_DATA
&&
717 fh
->fh_type
== FC_TYPE_FCP
) {
718 fc_exch_recv(lp
, lp
->emp
, fp
);
721 if (fr_flags(fp
) & FCPHF_CRC_UNCHECKED
) {
722 if (le32_to_cpu(fr_crc(fp
)) !=
723 ~crc32(~0, skb
->data
, fr_len
)) {
724 if (debug_fcoe
|| stats
->InvalidCRCCount
< 5)
725 printk(KERN_WARNING
"fcoe: dropping "
726 "frame with CRC error\n");
727 stats
->InvalidCRCCount
++;
728 stats
->ErrorFrames
++;
732 fr_flags(fp
) &= ~FCPHF_CRC_UNCHECKED
;
734 /* non flogi and non data exchanges are handled here */
735 if (unlikely(fc
->flogi_progress
))
736 fcoe_recv_flogi(fc
, fp
, mac
);
737 fc_exch_recv(lp
, lp
->emp
, fp
);
743 * fcoe_recv_flogi() - flogi receive function
744 * @fc: associated fcoe_softc
745 * @fp: the recieved frame
746 * @sa: the source address of this flogi
748 * This is responsible to parse the flogi response and sets the corresponding
749 * mac address for the initiator, eitehr OUI based or GW based.
753 static void fcoe_recv_flogi(struct fcoe_softc
*fc
, struct fc_frame
*fp
, u8
*sa
)
755 struct fc_frame_header
*fh
;
758 fh
= fc_frame_header_get(fp
);
759 if (fh
->fh_type
!= FC_TYPE_ELS
)
761 op
= fc_frame_payload_op(fp
);
762 if (op
== ELS_LS_ACC
&& fh
->fh_r_ctl
== FC_RCTL_ELS_REP
&&
763 fc
->flogi_oxid
== ntohs(fh
->fh_ox_id
)) {
766 * If the src mac addr is FC_OUI-based, then we mark the
767 * address_mode flag to use FC_OUI-based Ethernet DA.
768 * Otherwise we use the FCoE gateway addr
770 if (!compare_ether_addr(sa
, (u8
[6]) FC_FCOE_FLOGI_MAC
)) {
771 fc
->address_mode
= FCOE_FCOUI_ADDR_MODE
;
773 memcpy(fc
->dest_addr
, sa
, ETH_ALEN
);
774 fc
->address_mode
= FCOE_GW_ADDR_MODE
;
778 * Remove any previously-set unicast MAC filter.
779 * Add secondary FCoE MAC address filter for our OUI.
782 if (compare_ether_addr(fc
->data_src_addr
, (u8
[6]) { 0 }))
783 dev_unicast_delete(fc
->real_dev
, fc
->data_src_addr
,
785 fc_fcoe_set_mac(fc
->data_src_addr
, fh
->fh_d_id
);
786 dev_unicast_add(fc
->real_dev
, fc
->data_src_addr
, ETH_ALEN
);
789 fc
->flogi_progress
= 0;
790 } else if (op
== ELS_FLOGI
&& fh
->fh_r_ctl
== FC_RCTL_ELS_REQ
&& sa
) {
792 * Save source MAC for point-to-point responses.
794 memcpy(fc
->dest_addr
, sa
, ETH_ALEN
);
795 fc
->address_mode
= FCOE_GW_ADDR_MODE
;
800 * fcoe_watchdog() - fcoe timer callback
803 * This checks the pending queue length for fcoe and set lport qfull
804 * if the FCOE_MAX_QUEUE_DEPTH is reached. This is done for all fc_lport on the
807 * Returns: 0 for success
809 void fcoe_watchdog(ulong vp
)
811 struct fcoe_softc
*fc
;
813 read_lock(&fcoe_hostlist_lock
);
814 list_for_each_entry(fc
, &fcoe_hostlist
, list
) {
816 fcoe_check_wait_queue(fc
->lp
);
818 read_unlock(&fcoe_hostlist_lock
);
820 fcoe_timer
.expires
= jiffies
+ (1 * HZ
);
821 add_timer(&fcoe_timer
);
826 * fcoe_check_wait_queue() - put the skb into fcoe pending xmit queue
827 * @lp: the fc_port for this skb
828 * @skb: the associated skb to be xmitted
830 * This empties the wait_queue, dequeue the head of the wait_queue queue
831 * and calls fcoe_start_io() for each packet, if all skb have been
832 * transmitted, return qlen or -1 if a error occurs, then restore
833 * wait_queue and try again later.
835 * The wait_queue is used when the skb transmit fails. skb will go
836 * in the wait_queue which will be emptied by the time function OR
837 * by the next skb transmit.
839 * Returns: 0 for success
841 static int fcoe_check_wait_queue(struct fc_lport
*lp
)
843 struct fcoe_softc
*fc
= lport_priv(lp
);
847 spin_lock_bh(&fc
->fcoe_pending_queue
.lock
);
848 if (fc
->fcoe_pending_queue_active
)
850 fc
->fcoe_pending_queue_active
= 1;
852 while (fc
->fcoe_pending_queue
.qlen
) {
853 /* keep qlen > 0 until fcoe_start_io succeeds */
854 fc
->fcoe_pending_queue
.qlen
++;
855 skb
= __skb_dequeue(&fc
->fcoe_pending_queue
);
857 spin_unlock_bh(&fc
->fcoe_pending_queue
.lock
);
858 rc
= fcoe_start_io(skb
);
859 spin_lock_bh(&fc
->fcoe_pending_queue
.lock
);
862 __skb_queue_head(&fc
->fcoe_pending_queue
, skb
);
863 /* undo temporary increment above */
864 fc
->fcoe_pending_queue
.qlen
--;
867 /* undo temporary increment above */
868 fc
->fcoe_pending_queue
.qlen
--;
871 if (fc
->fcoe_pending_queue
.qlen
< FCOE_LOW_QUEUE_DEPTH
)
873 fc
->fcoe_pending_queue_active
= 0;
874 rc
= fc
->fcoe_pending_queue
.qlen
;
876 spin_unlock_bh(&fc
->fcoe_pending_queue
.lock
);
881 * fcoe_dev_setup() - setup link change notification interface
883 static void fcoe_dev_setup()
886 * here setup a interface specific wd time to
887 * monitor the link state
889 register_netdevice_notifier(&fcoe_notifier
);
893 * fcoe_dev_setup() - cleanup link change notification interface
895 static void fcoe_dev_cleanup(void)
897 unregister_netdevice_notifier(&fcoe_notifier
);
901 * fcoe_device_notification() - netdev event notification callback
902 * @notifier: context of the notification
903 * @event: type of event
904 * @ptr: fixed array for output parsed ifname
906 * This function is called by the ethernet driver in case of link change event
908 * Returns: 0 for success
910 static int fcoe_device_notification(struct notifier_block
*notifier
,
911 ulong event
, void *ptr
)
913 struct fc_lport
*lp
= NULL
;
914 struct net_device
*real_dev
= ptr
;
915 struct fcoe_softc
*fc
;
916 struct fcoe_dev_stats
*stats
;
921 read_lock(&fcoe_hostlist_lock
);
922 list_for_each_entry(fc
, &fcoe_hostlist
, list
) {
923 if (fc
->real_dev
== real_dev
) {
928 read_unlock(&fcoe_hostlist_lock
);
934 new_link_up
= lp
->link_up
;
937 case NETDEV_GOING_DOWN
:
942 new_link_up
= !fcoe_link_ok(lp
);
944 case NETDEV_CHANGEMTU
:
945 mfs
= fc
->real_dev
->mtu
-
946 (sizeof(struct fcoe_hdr
) +
947 sizeof(struct fcoe_crc_eof
));
948 if (mfs
>= FC_MIN_MAX_FRAME
)
950 new_link_up
= !fcoe_link_ok(lp
);
952 case NETDEV_REGISTER
:
955 FC_DBG("unknown event %ld call", event
);
957 if (lp
->link_up
!= new_link_up
) {
961 stats
= fc_lport_get_stats(lp
);
962 stats
->LinkFailureCount
++;
964 fcoe_clean_pending_queue(lp
);
972 * fcoe_if_to_netdev() - parse a name buffer to get netdev
973 * @ifname: fixed array for output parsed ifname
974 * @buffer: incoming buffer to be copied
976 * Returns: NULL or ptr to netdeive
978 static struct net_device
*fcoe_if_to_netdev(const char *buffer
)
981 char ifname
[IFNAMSIZ
+ 2];
984 strlcpy(ifname
, buffer
, IFNAMSIZ
);
985 cp
= ifname
+ strlen(ifname
);
986 while (--cp
>= ifname
&& *cp
== '\n')
988 return dev_get_by_name(&init_net
, ifname
);
994 * fcoe_netdev_to_module_owner() - finds out the nic drive moddule of the netdev
995 * @netdev: the target netdev
997 * Returns: ptr to the struct module, NULL for failure
999 static struct module
*
1000 fcoe_netdev_to_module_owner(const struct net_device
*netdev
)
1007 dev
= netdev
->dev
.parent
;
1014 return dev
->driver
->owner
;
1018 * fcoe_ethdrv_get() - Hold the Ethernet driver
1019 * @netdev: the target netdev
1021 * Holds the Ethernet driver module by try_module_get() for
1022 * the corresponding netdev.
1024 * Returns: 0 for succsss
1026 static int fcoe_ethdrv_get(const struct net_device
*netdev
)
1028 struct module
*owner
;
1030 owner
= fcoe_netdev_to_module_owner(netdev
);
1032 printk(KERN_DEBUG
"fcoe:hold driver module %s for %s\n",
1033 module_name(owner
), netdev
->name
);
1034 return try_module_get(owner
);
1040 * fcoe_ethdrv_put() - Release the Ethernet driver
1041 * @netdev: the target netdev
1043 * Releases the Ethernet driver module by module_put for
1044 * the corresponding netdev.
1046 * Returns: 0 for succsss
1048 static int fcoe_ethdrv_put(const struct net_device
*netdev
)
1050 struct module
*owner
;
1052 owner
= fcoe_netdev_to_module_owner(netdev
);
1054 printk(KERN_DEBUG
"fcoe:release driver module %s for %s\n",
1055 module_name(owner
), netdev
->name
);
1063 * fcoe_destroy() - handles the destroy from sysfs
1064 * @buffer: expcted to be a eth if name
1065 * @kp: associated kernel param
1067 * Returns: 0 for success
1069 static int fcoe_destroy(const char *buffer
, struct kernel_param
*kp
)
1072 struct net_device
*netdev
;
1074 netdev
= fcoe_if_to_netdev(buffer
);
1079 /* look for existing lport */
1080 if (!fcoe_hostlist_lookup(netdev
)) {
1084 /* pass to transport */
1085 rc
= fcoe_transport_release(netdev
);
1087 printk(KERN_ERR
"fcoe: fcoe_transport_release(%s) failed\n",
1092 fcoe_ethdrv_put(netdev
);
1101 * fcoe_create() - Handles the create call from sysfs
1102 * @buffer: expcted to be a eth if name
1103 * @kp: associated kernel param
1105 * Returns: 0 for success
1107 static int fcoe_create(const char *buffer
, struct kernel_param
*kp
)
1110 struct net_device
*netdev
;
1112 netdev
= fcoe_if_to_netdev(buffer
);
1117 /* look for existing lport */
1118 if (fcoe_hostlist_lookup(netdev
)) {
1122 fcoe_ethdrv_get(netdev
);
1124 /* pass to transport */
1125 rc
= fcoe_transport_attach(netdev
);
1127 printk(KERN_ERR
"fcoe: fcoe_transport_attach(%s) failed\n",
1129 fcoe_ethdrv_put(netdev
);
1140 module_param_call(create
, fcoe_create
, NULL
, NULL
, S_IWUSR
);
1141 __MODULE_PARM_TYPE(create
, "string");
1142 MODULE_PARM_DESC(create
, "Create fcoe port using net device passed in.");
1143 module_param_call(destroy
, fcoe_destroy
, NULL
, NULL
, S_IWUSR
);
1144 __MODULE_PARM_TYPE(destroy
, "string");
1145 MODULE_PARM_DESC(destroy
, "Destroy fcoe port");
1148 * fcoe_link_ok() - Check if link is ok for the fc_lport
1149 * @lp: ptr to the fc_lport
1151 * Any permanently-disqualifying conditions have been previously checked.
1152 * This also updates the speed setting, which may change with link for 100/1000.
1154 * This function should probably be checking for PAUSE support at some point
1155 * in the future. Currently Per-priority-pause is not determinable using
1156 * ethtool, so we shouldn't be restrictive until that problem is resolved.
1158 * Returns: 0 if link is OK for use by FCoE.
1161 int fcoe_link_ok(struct fc_lport
*lp
)
1163 struct fcoe_softc
*fc
= lport_priv(lp
);
1164 struct net_device
*dev
= fc
->real_dev
;
1165 struct ethtool_cmd ecmd
= { ETHTOOL_GSET
};
1168 if ((dev
->flags
& IFF_UP
) && netif_carrier_ok(dev
)) {
1170 if (dev
->ethtool_ops
->get_settings
) {
1171 dev
->ethtool_ops
->get_settings(dev
, &ecmd
);
1172 lp
->link_supported_speeds
&=
1173 ~(FC_PORTSPEED_1GBIT
| FC_PORTSPEED_10GBIT
);
1174 if (ecmd
.supported
& (SUPPORTED_1000baseT_Half
|
1175 SUPPORTED_1000baseT_Full
))
1176 lp
->link_supported_speeds
|= FC_PORTSPEED_1GBIT
;
1177 if (ecmd
.supported
& SUPPORTED_10000baseT_Full
)
1178 lp
->link_supported_speeds
|=
1179 FC_PORTSPEED_10GBIT
;
1180 if (ecmd
.speed
== SPEED_1000
)
1181 lp
->link_speed
= FC_PORTSPEED_1GBIT
;
1182 if (ecmd
.speed
== SPEED_10000
)
1183 lp
->link_speed
= FC_PORTSPEED_10GBIT
;
1190 EXPORT_SYMBOL_GPL(fcoe_link_ok
);
1193 * fcoe_percpu_clean() - Clear the pending skbs for an lport
1196 void fcoe_percpu_clean(struct fc_lport
*lp
)
1198 struct fcoe_percpu_s
*pp
;
1199 struct fcoe_rcv_info
*fr
;
1200 struct sk_buff_head
*list
;
1201 struct sk_buff
*skb
, *next
;
1202 struct sk_buff
*head
;
1205 for_each_possible_cpu(cpu
) {
1206 pp
= &per_cpu(fcoe_percpu
, cpu
);
1207 spin_lock_bh(&pp
->fcoe_rx_list
.lock
);
1208 list
= &pp
->fcoe_rx_list
;
1210 for (skb
= head
; skb
!= (struct sk_buff
*)list
;
1213 fr
= fcoe_dev_from_skb(skb
);
1214 if (fr
->fr_dev
== lp
) {
1215 __skb_unlink(skb
, list
);
1219 spin_unlock_bh(&pp
->fcoe_rx_list
.lock
);
1222 EXPORT_SYMBOL_GPL(fcoe_percpu_clean
);
1225 * fcoe_clean_pending_queue() - Dequeue a skb and free it
1226 * @lp: the corresponding fc_lport
1230 void fcoe_clean_pending_queue(struct fc_lport
*lp
)
1232 struct fcoe_softc
*fc
= lport_priv(lp
);
1233 struct sk_buff
*skb
;
1235 spin_lock_bh(&fc
->fcoe_pending_queue
.lock
);
1236 while ((skb
= __skb_dequeue(&fc
->fcoe_pending_queue
)) != NULL
) {
1237 spin_unlock_bh(&fc
->fcoe_pending_queue
.lock
);
1239 spin_lock_bh(&fc
->fcoe_pending_queue
.lock
);
1241 spin_unlock_bh(&fc
->fcoe_pending_queue
.lock
);
1243 EXPORT_SYMBOL_GPL(fcoe_clean_pending_queue
);
1246 * libfc_host_alloc() - Allocate a Scsi_Host with room for the fc_lport
1247 * @sht: ptr to the scsi host templ
1248 * @priv_size: size of private data after fc_lport
1250 * Returns: ptr to Scsi_Host
1253 static inline struct Scsi_Host
*
1254 libfc_host_alloc(struct scsi_host_template
*sht
, int priv_size
)
1256 return scsi_host_alloc(sht
, sizeof(struct fc_lport
) + priv_size
);
1260 * fcoe_host_alloc() - Allocate a Scsi_Host with room for the fcoe_softc
1261 * @sht: ptr to the scsi host templ
1262 * @priv_size: size of private data after fc_lport
1264 * Returns: ptr to Scsi_Host
1266 struct Scsi_Host
*fcoe_host_alloc(struct scsi_host_template
*sht
, int priv_size
)
1268 return libfc_host_alloc(sht
, sizeof(struct fcoe_softc
) + priv_size
);
1270 EXPORT_SYMBOL_GPL(fcoe_host_alloc
);
1273 * fcoe_reset() - Resets the fcoe
1274 * @shost: shost the reset is from
1278 int fcoe_reset(struct Scsi_Host
*shost
)
1280 struct fc_lport
*lport
= shost_priv(shost
);
1281 fc_lport_reset(lport
);
1284 EXPORT_SYMBOL_GPL(fcoe_reset
);
1287 * fcoe_wwn_from_mac() - Converts 48-bit IEEE MAC address to 64-bit FC WWN.
1289 * @scheme: check port
1290 * @port: port indicator for converting
1292 * Returns: u64 fc world wide name
1294 u64
fcoe_wwn_from_mac(unsigned char mac
[MAX_ADDR_LEN
],
1295 unsigned int scheme
, unsigned int port
)
1300 /* The MAC is in NO, so flip only the low 48 bits */
1301 host_mac
= ((u64
) mac
[0] << 40) |
1302 ((u64
) mac
[1] << 32) |
1303 ((u64
) mac
[2] << 24) |
1304 ((u64
) mac
[3] << 16) |
1305 ((u64
) mac
[4] << 8) |
1308 WARN_ON(host_mac
>= (1ULL << 48));
1309 wwn
= host_mac
| ((u64
) scheme
<< 60);
1315 WARN_ON(port
>= 0xfff);
1316 wwn
|= (u64
) port
<< 48;
1325 EXPORT_SYMBOL_GPL(fcoe_wwn_from_mac
);
1328 * fcoe_hostlist_lookup_softc() - find the corresponding lport by a given device
1329 * @device: this is currently ptr to net_device
1331 * Returns: NULL or the located fcoe_softc
1333 static struct fcoe_softc
*
1334 fcoe_hostlist_lookup_softc(const struct net_device
*dev
)
1336 struct fcoe_softc
*fc
;
1338 read_lock(&fcoe_hostlist_lock
);
1339 list_for_each_entry(fc
, &fcoe_hostlist
, list
) {
1340 if (fc
->real_dev
== dev
) {
1341 read_unlock(&fcoe_hostlist_lock
);
1345 read_unlock(&fcoe_hostlist_lock
);
1350 * fcoe_hostlist_lookup() - Find the corresponding lport by netdev
1351 * @netdev: ptr to net_device
1353 * Returns: 0 for success
1355 struct fc_lport
*fcoe_hostlist_lookup(const struct net_device
*netdev
)
1357 struct fcoe_softc
*fc
;
1359 fc
= fcoe_hostlist_lookup_softc(netdev
);
1361 return (fc
) ? fc
->lp
: NULL
;
1363 EXPORT_SYMBOL_GPL(fcoe_hostlist_lookup
);
1366 * fcoe_hostlist_add() - Add a lport to lports list
1367 * @lp: ptr to the fc_lport to badded
1369 * Returns: 0 for success
1371 int fcoe_hostlist_add(const struct fc_lport
*lp
)
1373 struct fcoe_softc
*fc
;
1375 fc
= fcoe_hostlist_lookup_softc(fcoe_netdev(lp
));
1377 fc
= lport_priv(lp
);
1378 write_lock_bh(&fcoe_hostlist_lock
);
1379 list_add_tail(&fc
->list
, &fcoe_hostlist
);
1380 write_unlock_bh(&fcoe_hostlist_lock
);
1384 EXPORT_SYMBOL_GPL(fcoe_hostlist_add
);
1387 * fcoe_hostlist_remove() - remove a lport from lports list
1388 * @lp: ptr to the fc_lport to badded
1390 * Returns: 0 for success
1392 int fcoe_hostlist_remove(const struct fc_lport
*lp
)
1394 struct fcoe_softc
*fc
;
1396 fc
= fcoe_hostlist_lookup_softc(fcoe_netdev(lp
));
1398 write_lock_bh(&fcoe_hostlist_lock
);
1399 list_del(&fc
->list
);
1400 write_unlock_bh(&fcoe_hostlist_lock
);
1404 EXPORT_SYMBOL_GPL(fcoe_hostlist_remove
);
1407 * fcoe_libfc_config() - sets up libfc related properties for lport
1408 * @lp: ptr to the fc_lport
1409 * @tt: libfc function template
1411 * Returns : 0 for success
1413 int fcoe_libfc_config(struct fc_lport
*lp
, struct libfc_function_template
*tt
)
1415 /* Set the function pointers set by the LLDD */
1416 memcpy(&lp
->tt
, tt
, sizeof(*tt
));
1417 if (fc_fcp_init(lp
))
1427 EXPORT_SYMBOL_GPL(fcoe_libfc_config
);
1430 * fcoe_init() - fcoe module loading initialization
1432 * Initialization routine
1433 * 1. Will create fc transport software structure
1434 * 2. initialize the link list of port information structure
1436 * Returns 0 on success, negative on failure
1438 static int __init
fcoe_init(void)
1442 struct fcoe_percpu_s
*p
;
1444 INIT_LIST_HEAD(&fcoe_hostlist
);
1445 rwlock_init(&fcoe_hostlist_lock
);
1447 for_each_possible_cpu(cpu
) {
1448 p
= &per_cpu(fcoe_percpu
, cpu
);
1449 skb_queue_head_init(&p
->fcoe_rx_list
);
1452 for_each_online_cpu(cpu
)
1453 fcoe_percpu_thread_create(cpu
);
1455 /* Initialize per CPU interrupt thread */
1456 rc
= register_hotcpu_notifier(&fcoe_cpu_notifier
);
1460 /* Setup link change notification */
1463 setup_timer(&fcoe_timer
, fcoe_watchdog
, 0);
1465 mod_timer(&fcoe_timer
, jiffies
+ (10 * HZ
));
1467 /* initiatlize the fcoe transport */
1468 fcoe_transport_init();
1475 for_each_online_cpu(cpu
) {
1476 fcoe_percpu_thread_destroy(cpu
);
1481 module_init(fcoe_init
);
1484 * fcoe_exit() - fcoe module unloading cleanup
1486 * Returns 0 on success, negative on failure
1488 static void __exit
fcoe_exit(void)
1491 struct fcoe_softc
*fc
, *tmp
;
1495 /* Stop the timer */
1496 del_timer_sync(&fcoe_timer
);
1498 /* releases the associated fcoe transport for each lport */
1499 list_for_each_entry_safe(fc
, tmp
, &fcoe_hostlist
, list
)
1500 fcoe_transport_release(fc
->real_dev
);
1502 unregister_hotcpu_notifier(&fcoe_cpu_notifier
);
1504 for_each_online_cpu(cpu
) {
1505 fcoe_percpu_thread_destroy(cpu
);
1508 /* remove sw trasnport */
1511 /* detach the transport */
1512 fcoe_transport_exit();
1514 module_exit(fcoe_exit
);