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
53 /* destination address mode */
54 #define FCOE_GW_ADDR_MODE 0x00
55 #define FCOE_FCOUI_ADDR_MODE 0x01
57 #define FCOE_WORD_TO_BYTE 4
59 MODULE_AUTHOR("Open-FCoE.org");
60 MODULE_DESCRIPTION("FCoE");
61 MODULE_LICENSE("GPL");
64 LIST_HEAD(fcoe_hostlist);
65 DEFINE_RWLOCK(fcoe_hostlist_lock);
66 DEFINE_TIMER(fcoe_timer, NULL, 0, 0);
67 struct fcoe_percpu_s *fcoe_percpu[NR_CPUS];
70 /* Function Prototyes */
71 static int fcoe_check_wait_queue(struct fc_lport *);
72 static void fcoe_insert_wait_queue_head(struct fc_lport *, struct sk_buff *);
73 static void fcoe_insert_wait_queue(struct fc_lport *, struct sk_buff *);
74 static void fcoe_recv_flogi(struct fcoe_softc *, struct fc_frame *, u8 *);
75 #ifdef CONFIG_HOTPLUG_CPU
76 static int fcoe_cpu_callback(struct notifier_block *, ulong, void *);
77 #endif /* CONFIG_HOTPLUG_CPU */
78 static int fcoe_device_notification(struct notifier_block *, ulong, void *);
79 static void fcoe_dev_setup(void);
80 static void fcoe_dev_cleanup(void);
82 /* notification function from net device */
83 static struct notifier_block fcoe_notifier = {
84 .notifier_call = fcoe_device_notification,
88 #ifdef CONFIG_HOTPLUG_CPU
89 static struct notifier_block fcoe_cpu_notifier = {
90 .notifier_call = fcoe_cpu_callback,
94 * fcoe_create_percpu_data - creates the associated cpu data
95 * @cpu: index for the cpu where fcoe cpu data will be created
97 * create percpu stats block, from cpu add notifier
101 static void fcoe_create_percpu_data(int cpu)
104 struct fcoe_softc *fc;
106 write_lock_bh(&fcoe_hostlist_lock);
107 list_for_each_entry(fc, &fcoe_hostlist, list) {
109 if (lp->dev_stats[cpu] == NULL)
111 kzalloc(sizeof(struct fcoe_dev_stats),
114 write_unlock_bh(&fcoe_hostlist_lock);
118 * fcoe_destroy_percpu_data - destroys the associated cpu data
119 * @cpu: index for the cpu where fcoe cpu data will destroyed
121 * destroy percpu stats block called by cpu add/remove notifier
125 static void fcoe_destroy_percpu_data(int cpu)
128 struct fcoe_softc *fc;
130 write_lock_bh(&fcoe_hostlist_lock);
131 list_for_each_entry(fc, &fcoe_hostlist, list) {
133 kfree(lp->dev_stats[cpu]);
134 lp->dev_stats[cpu] = NULL;
136 write_unlock_bh(&fcoe_hostlist_lock);
140 * fcoe_cpu_callback - fcoe cpu hotplug event callback
141 * @nfb: callback data block
142 * @action: event triggering the callback
143 * @hcpu: index for the cpu of this event
145 * this creates or destroys per cpu data for fcoe
147 * Returns NOTIFY_OK always.
149 static int fcoe_cpu_callback(struct notifier_block *nfb, unsigned long action,
152 unsigned int cpu = (unsigned long)hcpu;
156 fcoe_create_percpu_data(cpu);
159 fcoe_destroy_percpu_data(cpu);
166 #endif /* CONFIG_HOTPLUG_CPU */
169 * fcoe_rcv - this is the fcoe receive function called by NET_RX_SOFTIRQ
170 * @skb: the receive skb
171 * @dev: associated net device
173 * @odldev: last device
175 * this function will receive the packet and build fc frame and pass it up
177 * Returns: 0 for success
179 int fcoe_rcv(struct sk_buff *skb, struct net_device *dev,
180 struct packet_type *ptype, struct net_device *olddev)
183 struct fcoe_rcv_info *fr;
184 struct fcoe_softc *fc;
185 struct fcoe_dev_stats *stats;
186 struct fc_frame_header *fh;
189 struct fcoe_percpu_s *fps;
191 fc = container_of(ptype, struct fcoe_softc, fcoe_packet_type);
193 if (unlikely(lp == NULL)) {
194 FC_DBG("cannot find hba structure");
198 if (unlikely(debug_fcoe)) {
199 FC_DBG("skb_info: len:%d data_len:%d head:%p data:%p tail:%p "
200 "end:%p sum:%d dev:%s", skb->len, skb->data_len,
201 skb->head, skb->data, skb_tail_pointer(skb),
202 skb_end_pointer(skb), skb->csum,
203 skb->dev ? skb->dev->name : "<NULL>");
207 /* check for FCOE packet type */
208 if (unlikely(eth_hdr(skb)->h_proto != htons(ETH_P_FCOE))) {
209 FC_DBG("wrong FC type frame");
214 * Check for minimum frame length, and make sure required FCoE
215 * and FC headers are pulled into the linear data area.
217 if (unlikely((skb->len < FCOE_MIN_FRAME) ||
218 !pskb_may_pull(skb, FCOE_HEADER_LEN)))
221 skb_set_transport_header(skb, sizeof(struct fcoe_hdr));
222 fh = (struct fc_frame_header *) skb_transport_header(skb);
224 oxid = ntohs(fh->fh_ox_id);
226 fr = fcoe_dev_from_skb(skb);
232 * The incoming frame exchange id(oxid) is ANDed with num of online
233 * cpu bits to get cpu_idx and then this cpu_idx is used for selecting
234 * a per cpu kernel thread from fcoe_percpu. In case the cpu is
235 * offline or no kernel thread for derived cpu_idx then cpu_idx is
236 * initialize to first online cpu index.
238 cpu_idx = oxid & (num_online_cpus() - 1);
239 if (!fcoe_percpu[cpu_idx] || !cpu_online(cpu_idx))
240 cpu_idx = first_cpu(cpu_online_map);
242 fps = fcoe_percpu[cpu_idx];
244 spin_lock_bh(&fps->fcoe_rx_list.lock);
245 __skb_queue_tail(&fps->fcoe_rx_list, skb);
246 if (fps->fcoe_rx_list.qlen == 1)
247 wake_up_process(fps->thread);
249 spin_unlock_bh(&fps->fcoe_rx_list.lock);
254 stats = lp->dev_stats[smp_processor_id()];
256 stats = lp->dev_stats[0];
259 stats->ErrorFrames++;
265 EXPORT_SYMBOL_GPL(fcoe_rcv);
268 * fcoe_start_io - pass to netdev to start xmit for fcoe
269 * @skb: the skb to be xmitted
271 * Returns: 0 for success
273 static inline int fcoe_start_io(struct sk_buff *skb)
278 rc = dev_queue_xmit(skb);
286 * fcoe_get_paged_crc_eof - in case we need alloc a page for crc_eof
287 * @skb: the skb to be xmitted
290 * Returns: 0 for success
292 static int fcoe_get_paged_crc_eof(struct sk_buff *skb, int tlen)
294 struct fcoe_percpu_s *fps;
299 fps = fcoe_percpu[cpu_idx];
300 page = fps->crc_eof_page;
302 page = alloc_page(GFP_ATOMIC);
307 fps->crc_eof_page = page;
308 WARN_ON(fps->crc_eof_offset != 0);
312 skb_fill_page_desc(skb, skb_shinfo(skb)->nr_frags, page,
313 fps->crc_eof_offset, tlen);
315 skb->data_len += tlen;
316 skb->truesize += tlen;
317 fps->crc_eof_offset += sizeof(struct fcoe_crc_eof);
319 if (fps->crc_eof_offset >= PAGE_SIZE) {
320 fps->crc_eof_page = NULL;
321 fps->crc_eof_offset = 0;
329 * fcoe_fc_crc - calculates FC CRC in this fcoe skb
330 * @fp: the fc_frame containg data to be checksummed
332 * This uses crc32() to calculate the crc for fc frame
333 * Return : 32 bit crc
336 u32 fcoe_fc_crc(struct fc_frame *fp)
338 struct sk_buff *skb = fp_skb(fp);
339 struct skb_frag_struct *frag;
341 unsigned long off, len, clen;
345 crc = crc32(~0, skb->data, skb_headlen(skb));
347 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
348 frag = &skb_shinfo(skb)->frags[i];
349 off = frag->page_offset;
352 clen = min(len, PAGE_SIZE - (off & ~PAGE_MASK));
353 data = kmap_atomic(frag->page + (off >> PAGE_SHIFT),
354 KM_SKB_DATA_SOFTIRQ);
355 crc = crc32(crc, data + (off & ~PAGE_MASK), clen);
356 kunmap_atomic(data, KM_SKB_DATA_SOFTIRQ);
363 EXPORT_SYMBOL_GPL(fcoe_fc_crc);
366 * fcoe_xmit - FCoE frame transmit function
367 * @lp: the associated local port
368 * @fp: the fc_frame to be transmitted
370 * Return : 0 for success
373 int fcoe_xmit(struct fc_lport *lp, struct fc_frame *fp)
378 struct fcoe_crc_eof *cp;
380 struct fcoe_dev_stats *stats;
381 struct fc_frame_header *fh;
382 unsigned int hlen; /* header length implies the version */
383 unsigned int tlen; /* trailer length */
384 unsigned int elen; /* eth header, may include vlan */
385 int flogi_in_progress = 0;
386 struct fcoe_softc *fc;
390 WARN_ON((fr_len(fp) % sizeof(u32)) != 0);
394 * if it is a flogi then we need to learn gw-addr
397 fh = fc_frame_header_get(fp);
398 if (unlikely(fh->fh_r_ctl == FC_RCTL_ELS_REQ)) {
399 if (fc_frame_payload_op(fp) == ELS_FLOGI) {
400 fc->flogi_oxid = ntohs(fh->fh_ox_id);
401 fc->address_mode = FCOE_FCOUI_ADDR_MODE;
402 fc->flogi_progress = 1;
403 flogi_in_progress = 1;
404 } else if (fc->flogi_progress && ntoh24(fh->fh_s_id) != 0) {
406 * Here we must've gotten an SID by accepting an FLOGI
407 * from a point-to-point connection. Switch to using
408 * the source mac based on the SID. The destination
409 * MAC in this case would have been set by receving the
412 fc_fcoe_set_mac(fc->data_src_addr, fh->fh_s_id);
413 fc->flogi_progress = 0;
421 elen = (fc->real_dev->priv_flags & IFF_802_1Q_VLAN) ?
422 sizeof(struct vlan_ethhdr) : sizeof(struct ethhdr);
423 hlen = sizeof(struct fcoe_hdr);
424 tlen = sizeof(struct fcoe_crc_eof);
425 wlen = (skb->len - tlen + sizeof(crc)) / FCOE_WORD_TO_BYTE;
428 if (likely(lp->crc_offload)) {
429 skb->ip_summed = CHECKSUM_COMPLETE;
430 skb->csum_start = skb_headroom(skb);
431 skb->csum_offset = skb->len;
434 skb->ip_summed = CHECKSUM_NONE;
435 crc = fcoe_fc_crc(fp);
438 /* copy fc crc and eof to the skb buff */
439 if (skb_is_nonlinear(skb)) {
441 if (fcoe_get_paged_crc_eof(skb, tlen)) {
445 frag = &skb_shinfo(skb)->frags[skb_shinfo(skb)->nr_frags - 1];
446 cp = kmap_atomic(frag->page, KM_SKB_DATA_SOFTIRQ)
449 cp = (struct fcoe_crc_eof *)skb_put(skb, tlen);
452 memset(cp, 0, sizeof(*cp));
454 cp->fcoe_crc32 = cpu_to_le32(~crc);
456 if (skb_is_nonlinear(skb)) {
457 kunmap_atomic(cp, KM_SKB_DATA_SOFTIRQ);
461 /* adjust skb netowrk/transport offsets to match mac/fcoe/fc */
462 skb_push(skb, elen + hlen);
463 skb_reset_mac_header(skb);
464 skb_reset_network_header(skb);
466 skb->protocol = htons(ETH_P_802_3);
467 skb->dev = fc->real_dev;
469 /* fill up mac and fcoe headers */
471 eh->h_proto = htons(ETH_P_FCOE);
472 if (fc->address_mode == FCOE_FCOUI_ADDR_MODE)
473 fc_fcoe_set_mac(eh->h_dest, fh->fh_d_id);
475 /* insert GW address */
476 memcpy(eh->h_dest, fc->dest_addr, ETH_ALEN);
478 if (unlikely(flogi_in_progress))
479 memcpy(eh->h_source, fc->ctl_src_addr, ETH_ALEN);
481 memcpy(eh->h_source, fc->data_src_addr, ETH_ALEN);
483 hp = (struct fcoe_hdr *)(eh + 1);
484 memset(hp, 0, sizeof(*hp));
486 FC_FCOE_ENCAPS_VER(hp, FC_FCOE_VER);
489 /* update tx stats: regardless if LLD fails */
490 stats = lp->dev_stats[smp_processor_id()];
493 stats->TxWords += wlen;
496 /* send down to lld */
498 if (fc->fcoe_pending_queue.qlen)
499 rc = fcoe_check_wait_queue(lp);
502 rc = fcoe_start_io(skb);
505 fcoe_insert_wait_queue(lp, skb);
506 if (fc->fcoe_pending_queue.qlen > FCOE_MAX_QUEUE_DEPTH)
512 EXPORT_SYMBOL_GPL(fcoe_xmit);
515 * fcoe_percpu_receive_thread - recv thread per cpu
516 * @arg: ptr to the fcoe per cpu struct
518 * Return: 0 for success
521 int fcoe_percpu_receive_thread(void *arg)
523 struct fcoe_percpu_s *p = arg;
526 struct fcoe_rcv_info *fr;
527 struct fcoe_dev_stats *stats;
528 struct fc_frame_header *fh;
530 struct fcoe_crc_eof crc_eof;
533 struct fcoe_softc *fc;
536 set_user_nice(current, 19);
538 while (!kthread_should_stop()) {
540 spin_lock_bh(&p->fcoe_rx_list.lock);
541 while ((skb = __skb_dequeue(&p->fcoe_rx_list)) == NULL) {
542 set_current_state(TASK_INTERRUPTIBLE);
543 spin_unlock_bh(&p->fcoe_rx_list.lock);
545 set_current_state(TASK_RUNNING);
546 if (kthread_should_stop())
548 spin_lock_bh(&p->fcoe_rx_list.lock);
550 spin_unlock_bh(&p->fcoe_rx_list.lock);
551 fr = fcoe_dev_from_skb(skb);
553 if (unlikely(lp == NULL)) {
554 FC_DBG("invalid HBA Structure");
559 stats = lp->dev_stats[smp_processor_id()];
561 if (unlikely(debug_fcoe)) {
562 FC_DBG("skb_info: len:%d data_len:%d head:%p data:%p "
563 "tail:%p end:%p sum:%d dev:%s",
564 skb->len, skb->data_len,
565 skb->head, skb->data, skb_tail_pointer(skb),
566 skb_end_pointer(skb), skb->csum,
567 skb->dev ? skb->dev->name : "<NULL>");
571 * Save source MAC address before discarding header.
574 if (unlikely(fc->flogi_progress))
575 mac = eth_hdr(skb)->h_source;
577 if (skb_is_nonlinear(skb))
578 skb_linearize(skb); /* not ideal */
581 * Frame length checks and setting up the header pointers
582 * was done in fcoe_rcv already.
584 hp = (struct fcoe_hdr *) skb_network_header(skb);
585 fh = (struct fc_frame_header *) skb_transport_header(skb);
587 if (unlikely(FC_FCOE_DECAPS_VER(hp) != FC_FCOE_VER)) {
589 if (stats->ErrorFrames < 5)
590 FC_DBG("unknown FCoE version %x",
591 FC_FCOE_DECAPS_VER(hp));
592 stats->ErrorFrames++;
598 skb_pull(skb, sizeof(struct fcoe_hdr));
599 fr_len = skb->len - sizeof(struct fcoe_crc_eof);
603 stats->RxWords += fr_len / FCOE_WORD_TO_BYTE;
606 fp = (struct fc_frame *)skb;
609 fr_sof(fp) = hp->fcoe_sof;
611 /* Copy out the CRC and EOF trailer for access */
612 if (skb_copy_bits(skb, fr_len, &crc_eof, sizeof(crc_eof))) {
616 fr_eof(fp) = crc_eof.fcoe_eof;
617 fr_crc(fp) = crc_eof.fcoe_crc32;
618 if (pskb_trim(skb, fr_len)) {
624 * We only check CRC if no offload is available and if it is
625 * it's solicited data, in which case, the FCP layer would
626 * check it during the copy.
629 fr_flags(fp) &= ~FCPHF_CRC_UNCHECKED;
631 fr_flags(fp) |= FCPHF_CRC_UNCHECKED;
633 fh = fc_frame_header_get(fp);
634 if (fh->fh_r_ctl == FC_RCTL_DD_SOL_DATA &&
635 fh->fh_type == FC_TYPE_FCP) {
636 fc_exch_recv(lp, lp->emp, fp);
639 if (fr_flags(fp) & FCPHF_CRC_UNCHECKED) {
640 if (le32_to_cpu(fr_crc(fp)) !=
641 ~crc32(~0, skb->data, fr_len)) {
642 if (debug_fcoe || stats->InvalidCRCCount < 5)
643 printk(KERN_WARNING "fcoe: dropping "
644 "frame with CRC error\n");
645 stats->InvalidCRCCount++;
646 stats->ErrorFrames++;
650 fr_flags(fp) &= ~FCPHF_CRC_UNCHECKED;
652 /* non flogi and non data exchanges are handled here */
653 if (unlikely(fc->flogi_progress))
654 fcoe_recv_flogi(fc, fp, mac);
655 fc_exch_recv(lp, lp->emp, fp);
661 * fcoe_recv_flogi - flogi receive function
662 * @fc: associated fcoe_softc
663 * @fp: the recieved frame
664 * @sa: the source address of this flogi
666 * This is responsible to parse the flogi response and sets the corresponding
667 * mac address for the initiator, eitehr OUI based or GW based.
671 static void fcoe_recv_flogi(struct fcoe_softc *fc, struct fc_frame *fp, u8 *sa)
673 struct fc_frame_header *fh;
676 fh = fc_frame_header_get(fp);
677 if (fh->fh_type != FC_TYPE_ELS)
679 op = fc_frame_payload_op(fp);
680 if (op == ELS_LS_ACC && fh->fh_r_ctl == FC_RCTL_ELS_REP &&
681 fc->flogi_oxid == ntohs(fh->fh_ox_id)) {
684 * If the src mac addr is FC_OUI-based, then we mark the
685 * address_mode flag to use FC_OUI-based Ethernet DA.
686 * Otherwise we use the FCoE gateway addr
688 if (!compare_ether_addr(sa, (u8[6]) FC_FCOE_FLOGI_MAC)) {
689 fc->address_mode = FCOE_FCOUI_ADDR_MODE;
691 memcpy(fc->dest_addr, sa, ETH_ALEN);
692 fc->address_mode = FCOE_GW_ADDR_MODE;
696 * Remove any previously-set unicast MAC filter.
697 * Add secondary FCoE MAC address filter for our OUI.
700 if (compare_ether_addr(fc->data_src_addr, (u8[6]) { 0 }))
701 dev_unicast_delete(fc->real_dev, fc->data_src_addr,
703 fc_fcoe_set_mac(fc->data_src_addr, fh->fh_d_id);
704 dev_unicast_add(fc->real_dev, fc->data_src_addr, ETH_ALEN);
707 fc->flogi_progress = 0;
708 } else if (op == ELS_FLOGI && fh->fh_r_ctl == FC_RCTL_ELS_REQ && sa) {
710 * Save source MAC for point-to-point responses.
712 memcpy(fc->dest_addr, sa, ETH_ALEN);
713 fc->address_mode = FCOE_GW_ADDR_MODE;
718 * fcoe_watchdog - fcoe timer callback
721 * This checks the pending queue length for fcoe and put fcoe to be paused state
722 * if the FCOE_MAX_QUEUE_DEPTH is reached. This is done for all fc_lport on the
725 * Returns: 0 for success
727 void fcoe_watchdog(ulong vp)
730 struct fcoe_softc *fc;
733 read_lock(&fcoe_hostlist_lock);
734 list_for_each_entry(fc, &fcoe_hostlist, list) {
737 if (fc->fcoe_pending_queue.qlen > FCOE_MAX_QUEUE_DEPTH)
739 if (fcoe_check_wait_queue(lp) < FCOE_MAX_QUEUE_DEPTH) {
745 read_unlock(&fcoe_hostlist_lock);
747 fcoe_timer.expires = jiffies + (1 * HZ);
748 add_timer(&fcoe_timer);
753 * fcoe_check_wait_queue - put the skb into fcoe pending xmit queue
754 * @lp: the fc_port for this skb
755 * @skb: the associated skb to be xmitted
757 * This empties the wait_queue, dequeue the head of the wait_queue queue
758 * and calls fcoe_start_io() for each packet, if all skb have been
759 * transmitted, return 0 if a error occurs, then restore wait_queue and
762 * The wait_queue is used when the skb transmit fails. skb will go
763 * in the wait_queue which will be emptied by the time function OR
764 * by the next skb transmit.
766 * Returns: 0 for success
768 static int fcoe_check_wait_queue(struct fc_lport *lp)
773 struct fcoe_softc *fc;
776 spin_lock_bh(&fc->fcoe_pending_queue.lock);
779 * is this interface paused?
781 if (fc->fcoe_pending_queue.qlen > FCOE_MAX_QUEUE_DEPTH)
783 if (fc->fcoe_pending_queue.qlen) {
784 while ((skb = __skb_dequeue(&fc->fcoe_pending_queue)) != NULL) {
785 spin_unlock_bh(&fc->fcoe_pending_queue.lock);
786 rc = fcoe_start_io(skb);
788 fcoe_insert_wait_queue_head(lp, skb);
791 spin_lock_bh(&fc->fcoe_pending_queue.lock);
793 if (fc->fcoe_pending_queue.qlen < FCOE_MAX_QUEUE_DEPTH)
796 spin_unlock_bh(&fc->fcoe_pending_queue.lock);
797 if ((unpause) && (paused))
799 return fc->fcoe_pending_queue.qlen;
803 * fcoe_insert_wait_queue_head - puts skb to fcoe pending queue head
804 * @lp: the fc_port for this skb
805 * @skb: the associated skb to be xmitted
809 static void fcoe_insert_wait_queue_head(struct fc_lport *lp,
812 struct fcoe_softc *fc;
815 spin_lock_bh(&fc->fcoe_pending_queue.lock);
816 __skb_queue_head(&fc->fcoe_pending_queue, skb);
817 spin_unlock_bh(&fc->fcoe_pending_queue.lock);
821 * fcoe_insert_wait_queue - put the skb into fcoe pending queue tail
822 * @lp: the fc_port for this skb
823 * @skb: the associated skb to be xmitted
827 static void fcoe_insert_wait_queue(struct fc_lport *lp,
830 struct fcoe_softc *fc;
833 spin_lock_bh(&fc->fcoe_pending_queue.lock);
834 __skb_queue_tail(&fc->fcoe_pending_queue, skb);
835 spin_unlock_bh(&fc->fcoe_pending_queue.lock);
839 * fcoe_dev_setup - setup link change notification interface
842 static void fcoe_dev_setup(void)
845 * here setup a interface specific wd time to
846 * monitor the link state
848 register_netdevice_notifier(&fcoe_notifier);
852 * fcoe_dev_setup - cleanup link change notification interface
854 static void fcoe_dev_cleanup(void)
856 unregister_netdevice_notifier(&fcoe_notifier);
860 * fcoe_device_notification - netdev event notification callback
861 * @notifier: context of the notification
862 * @event: type of event
863 * @ptr: fixed array for output parsed ifname
865 * This function is called by the ethernet driver in case of link change event
867 * Returns: 0 for success
869 static int fcoe_device_notification(struct notifier_block *notifier,
870 ulong event, void *ptr)
872 struct fc_lport *lp = NULL;
873 struct net_device *real_dev = ptr;
874 struct fcoe_softc *fc;
875 struct fcoe_dev_stats *stats;
880 read_lock(&fcoe_hostlist_lock);
881 list_for_each_entry(fc, &fcoe_hostlist, list) {
882 if (fc->real_dev == real_dev) {
887 read_unlock(&fcoe_hostlist_lock);
893 new_status = lp->link_status;
896 case NETDEV_GOING_DOWN:
897 new_status &= ~FC_LINK_UP;
901 new_status &= ~FC_LINK_UP;
902 if (!fcoe_link_ok(lp))
903 new_status |= FC_LINK_UP;
905 case NETDEV_CHANGEMTU:
906 mfs = fc->real_dev->mtu -
907 (sizeof(struct fcoe_hdr) +
908 sizeof(struct fcoe_crc_eof));
909 if (mfs >= FC_MIN_MAX_FRAME)
911 new_status &= ~FC_LINK_UP;
912 if (!fcoe_link_ok(lp))
913 new_status |= FC_LINK_UP;
915 case NETDEV_REGISTER:
918 FC_DBG("unknown event %ld call", event);
920 if (lp->link_status != new_status) {
921 if ((new_status & FC_LINK_UP) == FC_LINK_UP)
924 stats = lp->dev_stats[smp_processor_id()];
926 stats->LinkFailureCount++;
928 fcoe_clean_pending_queue(lp);
936 * fcoe_if_to_netdev - parse a name buffer to get netdev
937 * @ifname: fixed array for output parsed ifname
938 * @buffer: incoming buffer to be copied
940 * Returns: NULL or ptr to netdeive
942 static struct net_device *fcoe_if_to_netdev(const char *buffer)
945 char ifname[IFNAMSIZ + 2];
948 strlcpy(ifname, buffer, IFNAMSIZ);
949 cp = ifname + strlen(ifname);
950 while (--cp >= ifname && *cp == '\n')
952 return dev_get_by_name(&init_net, ifname);
958 * fcoe_netdev_to_module_owner - finds out the nic drive moddule of the netdev
959 * @netdev: the target netdev
961 * Returns: ptr to the struct module, NULL for failure
963 static struct module *fcoe_netdev_to_module_owner(
964 const struct net_device *netdev)
971 dev = netdev->dev.parent;
978 return dev->driver->owner;
982 * fcoe_ethdrv_get - holds the nic driver module by try_module_get() for
983 * the corresponding netdev.
984 * @netdev: the target netdev
986 * Returns: 0 for succsss
988 static int fcoe_ethdrv_get(const struct net_device *netdev)
990 struct module *owner;
992 owner = fcoe_netdev_to_module_owner(netdev);
994 printk(KERN_DEBUG "fcoe:hold driver module %s for %s\n",
995 module_name(owner), netdev->name);
996 return try_module_get(owner);
1002 * fcoe_ethdrv_get - releases the nic driver module by module_put for
1003 * the corresponding netdev.
1004 * @netdev: the target netdev
1006 * Returns: 0 for succsss
1008 static int fcoe_ethdrv_put(const struct net_device *netdev)
1010 struct module *owner;
1012 owner = fcoe_netdev_to_module_owner(netdev);
1014 printk(KERN_DEBUG "fcoe:release driver module %s for %s\n",
1015 module_name(owner), netdev->name);
1023 * fcoe_destroy- handles the destroy from sysfs
1024 * @buffer: expcted to be a eth if name
1025 * @kp: associated kernel param
1027 * Returns: 0 for success
1029 static int fcoe_destroy(const char *buffer, struct kernel_param *kp)
1032 struct net_device *netdev;
1034 netdev = fcoe_if_to_netdev(buffer);
1039 /* look for existing lport */
1040 if (!fcoe_hostlist_lookup(netdev)) {
1044 /* pass to transport */
1045 rc = fcoe_transport_release(netdev);
1047 printk(KERN_ERR "fcoe: fcoe_transport_release(%s) failed\n",
1052 fcoe_ethdrv_put(netdev);
1061 * fcoe_create - handles the create call from sysfs
1062 * @buffer: expcted to be a eth if name
1063 * @kp: associated kernel param
1065 * Returns: 0 for success
1067 static int fcoe_create(const char *buffer, struct kernel_param *kp)
1070 struct net_device *netdev;
1072 netdev = fcoe_if_to_netdev(buffer);
1077 /* look for existing lport */
1078 if (fcoe_hostlist_lookup(netdev)) {
1082 fcoe_ethdrv_get(netdev);
1084 /* pass to transport */
1085 rc = fcoe_transport_attach(netdev);
1087 printk(KERN_ERR "fcoe: fcoe_transport_attach(%s) failed\n",
1089 fcoe_ethdrv_put(netdev);
1100 module_param_call(create, fcoe_create, NULL, NULL, S_IWUSR);
1101 __MODULE_PARM_TYPE(create, "string");
1102 MODULE_PARM_DESC(create, "Create fcoe port using net device passed in.");
1103 module_param_call(destroy, fcoe_destroy, NULL, NULL, S_IWUSR);
1104 __MODULE_PARM_TYPE(destroy, "string");
1105 MODULE_PARM_DESC(destroy, "Destroy fcoe port");
1108 * fcoe_link_ok - check if link is ok for the fc_lport
1109 * @lp: ptr to the fc_lport
1111 * Any permanently-disqualifying conditions have been previously checked.
1112 * This also updates the speed setting, which may change with link for 100/1000.
1114 * This function should probably be checking for PAUSE support at some point
1115 * in the future. Currently Per-priority-pause is not determinable using
1116 * ethtool, so we shouldn't be restrictive until that problem is resolved.
1118 * Returns: 0 if link is OK for use by FCoE.
1121 int fcoe_link_ok(struct fc_lport *lp)
1123 struct fcoe_softc *fc = fcoe_softc(lp);
1124 struct net_device *dev = fc->real_dev;
1125 struct ethtool_cmd ecmd = { ETHTOOL_GSET };
1128 if ((dev->flags & IFF_UP) && netif_carrier_ok(dev)) {
1130 if (dev->ethtool_ops->get_settings) {
1131 dev->ethtool_ops->get_settings(dev, &ecmd);
1132 lp->link_supported_speeds &=
1133 ~(FC_PORTSPEED_1GBIT | FC_PORTSPEED_10GBIT);
1134 if (ecmd.supported & (SUPPORTED_1000baseT_Half |
1135 SUPPORTED_1000baseT_Full))
1136 lp->link_supported_speeds |= FC_PORTSPEED_1GBIT;
1137 if (ecmd.supported & SUPPORTED_10000baseT_Full)
1138 lp->link_supported_speeds |=
1139 FC_PORTSPEED_10GBIT;
1140 if (ecmd.speed == SPEED_1000)
1141 lp->link_speed = FC_PORTSPEED_1GBIT;
1142 if (ecmd.speed == SPEED_10000)
1143 lp->link_speed = FC_PORTSPEED_10GBIT;
1150 EXPORT_SYMBOL_GPL(fcoe_link_ok);
1153 * fcoe_percpu_clean - frees skb of the corresponding lport from the per
1157 void fcoe_percpu_clean(struct fc_lport *lp)
1160 struct fcoe_percpu_s *pp;
1161 struct fcoe_rcv_info *fr;
1162 struct sk_buff_head *list;
1163 struct sk_buff *skb, *next;
1164 struct sk_buff *head;
1166 for (idx = 0; idx < NR_CPUS; idx++) {
1167 if (fcoe_percpu[idx]) {
1168 pp = fcoe_percpu[idx];
1169 spin_lock_bh(&pp->fcoe_rx_list.lock);
1170 list = &pp->fcoe_rx_list;
1172 for (skb = head; skb != (struct sk_buff *)list;
1175 fr = fcoe_dev_from_skb(skb);
1176 if (fr->fr_dev == lp) {
1177 __skb_unlink(skb, list);
1181 spin_unlock_bh(&pp->fcoe_rx_list.lock);
1185 EXPORT_SYMBOL_GPL(fcoe_percpu_clean);
1188 * fcoe_clean_pending_queue - dequeue skb and free it
1189 * @lp: the corresponding fc_lport
1193 void fcoe_clean_pending_queue(struct fc_lport *lp)
1195 struct fcoe_softc *fc = lport_priv(lp);
1196 struct sk_buff *skb;
1198 spin_lock_bh(&fc->fcoe_pending_queue.lock);
1199 while ((skb = __skb_dequeue(&fc->fcoe_pending_queue)) != NULL) {
1200 spin_unlock_bh(&fc->fcoe_pending_queue.lock);
1202 spin_lock_bh(&fc->fcoe_pending_queue.lock);
1204 spin_unlock_bh(&fc->fcoe_pending_queue.lock);
1206 EXPORT_SYMBOL_GPL(fcoe_clean_pending_queue);
1209 * libfc_host_alloc - allocate a Scsi_Host with room for the fc_lport
1210 * @sht: ptr to the scsi host templ
1211 * @priv_size: size of private data after fc_lport
1213 * Returns: ptr to Scsi_Host
1216 static inline struct Scsi_Host *libfc_host_alloc(
1217 struct scsi_host_template *sht, int priv_size)
1219 return scsi_host_alloc(sht, sizeof(struct fc_lport) + priv_size);
1223 * fcoe_host_alloc - allocate a Scsi_Host with room for the fcoe_softc
1224 * @sht: ptr to the scsi host templ
1225 * @priv_size: size of private data after fc_lport
1227 * Returns: ptr to Scsi_Host
1229 struct Scsi_Host *fcoe_host_alloc(struct scsi_host_template *sht, int priv_size)
1231 return libfc_host_alloc(sht, sizeof(struct fcoe_softc) + priv_size);
1233 EXPORT_SYMBOL_GPL(fcoe_host_alloc);
1236 * fcoe_reset - resets the fcoe
1237 * @shost: shost the reset is from
1241 int fcoe_reset(struct Scsi_Host *shost)
1243 struct fc_lport *lport = shost_priv(shost);
1244 fc_lport_reset(lport);
1247 EXPORT_SYMBOL_GPL(fcoe_reset);
1250 * fcoe_wwn_from_mac - converts 48-bit IEEE MAC address to 64-bit FC WWN.
1252 * @scheme: check port
1253 * @port: port indicator for converting
1255 * Returns: u64 fc world wide name
1257 u64 fcoe_wwn_from_mac(unsigned char mac[MAX_ADDR_LEN],
1258 unsigned int scheme, unsigned int port)
1263 /* The MAC is in NO, so flip only the low 48 bits */
1264 host_mac = ((u64) mac[0] << 40) |
1265 ((u64) mac[1] << 32) |
1266 ((u64) mac[2] << 24) |
1267 ((u64) mac[3] << 16) |
1268 ((u64) mac[4] << 8) |
1271 WARN_ON(host_mac >= (1ULL << 48));
1272 wwn = host_mac | ((u64) scheme << 60);
1278 WARN_ON(port >= 0xfff);
1279 wwn |= (u64) port << 48;
1288 EXPORT_SYMBOL_GPL(fcoe_wwn_from_mac);
1290 * fcoe_hostlist_lookup_softc - find the corresponding lport by a given device
1291 * @device: this is currently ptr to net_device
1293 * Returns: NULL or the located fcoe_softc
1295 static struct fcoe_softc *fcoe_hostlist_lookup_softc(
1296 const struct net_device *dev)
1298 struct fcoe_softc *fc;
1300 read_lock(&fcoe_hostlist_lock);
1301 list_for_each_entry(fc, &fcoe_hostlist, list) {
1302 if (fc->real_dev == dev) {
1303 read_unlock(&fcoe_hostlist_lock);
1307 read_unlock(&fcoe_hostlist_lock);
1312 * fcoe_hostlist_lookup - find the corresponding lport by netdev
1313 * @netdev: ptr to net_device
1315 * Returns: 0 for success
1317 struct fc_lport *fcoe_hostlist_lookup(const struct net_device *netdev)
1319 struct fcoe_softc *fc;
1321 fc = fcoe_hostlist_lookup_softc(netdev);
1323 return (fc) ? fc->lp : NULL;
1325 EXPORT_SYMBOL_GPL(fcoe_hostlist_lookup);
1328 * fcoe_hostlist_add - add a lport to lports list
1329 * @lp: ptr to the fc_lport to badded
1331 * Returns: 0 for success
1333 int fcoe_hostlist_add(const struct fc_lport *lp)
1335 struct fcoe_softc *fc;
1337 fc = fcoe_hostlist_lookup_softc(fcoe_netdev(lp));
1339 fc = fcoe_softc(lp);
1340 write_lock_bh(&fcoe_hostlist_lock);
1341 list_add_tail(&fc->list, &fcoe_hostlist);
1342 write_unlock_bh(&fcoe_hostlist_lock);
1346 EXPORT_SYMBOL_GPL(fcoe_hostlist_add);
1349 * fcoe_hostlist_remove - remove a lport from lports list
1350 * @lp: ptr to the fc_lport to badded
1352 * Returns: 0 for success
1354 int fcoe_hostlist_remove(const struct fc_lport *lp)
1356 struct fcoe_softc *fc;
1358 fc = fcoe_hostlist_lookup_softc(fcoe_netdev(lp));
1360 write_lock_bh(&fcoe_hostlist_lock);
1361 list_del(&fc->list);
1362 write_unlock_bh(&fcoe_hostlist_lock);
1366 EXPORT_SYMBOL_GPL(fcoe_hostlist_remove);
1369 * fcoe_libfc_config - sets up libfc related properties for lport
1370 * @lp: ptr to the fc_lport
1371 * @tt: libfc function template
1373 * Returns : 0 for success
1375 int fcoe_libfc_config(struct fc_lport *lp, struct libfc_function_template *tt)
1377 /* Set the function pointers set by the LLDD */
1378 memcpy(&lp->tt, tt, sizeof(*tt));
1379 if (fc_fcp_init(lp))
1389 EXPORT_SYMBOL_GPL(fcoe_libfc_config);
1392 * fcoe_init - fcoe module loading initialization
1394 * Initialization routine
1395 * 1. Will create fc transport software structure
1396 * 2. initialize the link list of port information structure
1398 * Returns 0 on success, negative on failure
1400 static int __init fcoe_init(void)
1403 struct fcoe_percpu_s *p;
1406 INIT_LIST_HEAD(&fcoe_hostlist);
1407 rwlock_init(&fcoe_hostlist_lock);
1409 #ifdef CONFIG_HOTPLUG_CPU
1410 register_cpu_notifier(&fcoe_cpu_notifier);
1411 #endif /* CONFIG_HOTPLUG_CPU */
1414 * initialize per CPU interrupt thread
1416 for_each_online_cpu(cpu) {
1417 p = kzalloc(sizeof(struct fcoe_percpu_s), GFP_KERNEL);
1419 p->thread = kthread_create(fcoe_percpu_receive_thread,
1421 "fcoethread/%d", cpu);
1424 * if there is no error then bind the thread to the cpu
1425 * initialize the semaphore and skb queue head
1427 if (likely(!IS_ERR(p->thread))) {
1429 fcoe_percpu[cpu] = p;
1430 skb_queue_head_init(&p->fcoe_rx_list);
1431 kthread_bind(p->thread, cpu);
1432 wake_up_process(p->thread);
1434 fcoe_percpu[cpu] = NULL;
1442 * setup link change notification
1446 init_timer(&fcoe_timer);
1447 fcoe_timer.data = 0;
1448 fcoe_timer.function = fcoe_watchdog;
1449 fcoe_timer.expires = (jiffies + (10 * HZ));
1450 add_timer(&fcoe_timer);
1452 /* initiatlize the fcoe transport */
1453 fcoe_transport_init();
1459 module_init(fcoe_init);
1462 * fcoe_exit - fcoe module unloading cleanup
1464 * Returns 0 on success, negative on failure
1466 static void __exit fcoe_exit(void)
1469 struct fcoe_softc *fc, *tmp;
1470 struct fcoe_percpu_s *p;
1471 struct sk_buff *skb;
1474 * Stop all call back interfaces
1476 #ifdef CONFIG_HOTPLUG_CPU
1477 unregister_cpu_notifier(&fcoe_cpu_notifier);
1478 #endif /* CONFIG_HOTPLUG_CPU */
1484 del_timer_sync(&fcoe_timer);
1486 /* releases the assocaited fcoe transport for each lport */
1487 list_for_each_entry_safe(fc, tmp, &fcoe_hostlist, list)
1488 fcoe_transport_release(fc->real_dev);
1490 for (idx = 0; idx < NR_CPUS; idx++) {
1491 if (fcoe_percpu[idx]) {
1492 kthread_stop(fcoe_percpu[idx]->thread);
1493 p = fcoe_percpu[idx];
1494 spin_lock_bh(&p->fcoe_rx_list.lock);
1495 while ((skb = __skb_dequeue(&p->fcoe_rx_list)) != NULL)
1497 spin_unlock_bh(&p->fcoe_rx_list.lock);
1498 if (fcoe_percpu[idx]->crc_eof_page)
1499 put_page(fcoe_percpu[idx]->crc_eof_page);
1500 kfree(fcoe_percpu[idx]);
1504 /* remove sw trasnport */
1507 /* detach the transport */
1508 fcoe_transport_exit();
1510 module_exit(fcoe_exit);