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 #ifdef CONFIG_HOTPLUG_CPU
75 static int fcoe_cpu_callback(struct notifier_block *, ulong, void *);
76 #endif /* CONFIG_HOTPLUG_CPU */
77 static int fcoe_device_notification(struct notifier_block *, ulong, void *);
78 static void fcoe_dev_setup(void);
79 static void fcoe_dev_cleanup(void);
81 /* notification function from net device */
82 static struct notifier_block fcoe_notifier = {
83 .notifier_call = fcoe_device_notification,
87 #ifdef CONFIG_HOTPLUG_CPU
88 static struct notifier_block fcoe_cpu_notifier = {
89 .notifier_call = fcoe_cpu_callback,
93 * fcoe_create_percpu_data() - creates the associated cpu data
94 * @cpu: index for the cpu where fcoe cpu data will be created
96 * create percpu stats block, from cpu add notifier
100 static void fcoe_create_percpu_data(unsigned int cpu)
103 struct fcoe_softc *fc;
105 write_lock_bh(&fcoe_hostlist_lock);
106 list_for_each_entry(fc, &fcoe_hostlist, list) {
108 if (lp->dev_stats[cpu] == NULL)
110 kzalloc(sizeof(struct fcoe_dev_stats),
113 write_unlock_bh(&fcoe_hostlist_lock);
117 * fcoe_destroy_percpu_data() - destroys the associated cpu data
118 * @cpu: index for the cpu where fcoe cpu data will destroyed
120 * destroy percpu stats block called by cpu add/remove notifier
124 static void fcoe_destroy_percpu_data(unsigned int cpu)
127 struct fcoe_softc *fc;
129 write_lock_bh(&fcoe_hostlist_lock);
130 list_for_each_entry(fc, &fcoe_hostlist, list) {
132 kfree(lp->dev_stats[cpu]);
133 lp->dev_stats[cpu] = NULL;
135 write_unlock_bh(&fcoe_hostlist_lock);
139 * fcoe_cpu_callback() - fcoe cpu hotplug event callback
140 * @nfb: callback data block
141 * @action: event triggering the callback
142 * @hcpu: index for the cpu of this event
144 * this creates or destroys per cpu data for fcoe
146 * Returns NOTIFY_OK always.
148 static int fcoe_cpu_callback(struct notifier_block *nfb, unsigned long action,
151 unsigned int cpu = (unsigned long)hcpu;
155 fcoe_create_percpu_data(cpu);
158 fcoe_destroy_percpu_data(cpu);
165 #endif /* CONFIG_HOTPLUG_CPU */
168 * fcoe_rcv() - this is the fcoe receive function called by NET_RX_SOFTIRQ
169 * @skb: the receive skb
170 * @dev: associated net device
172 * @odldev: last device
174 * this function will receive the packet and build fc frame and pass it up
176 * Returns: 0 for success
178 int fcoe_rcv(struct sk_buff *skb, struct net_device *dev,
179 struct packet_type *ptype, struct net_device *olddev)
182 struct fcoe_rcv_info *fr;
183 struct fcoe_softc *fc;
184 struct fcoe_dev_stats *stats;
185 struct fc_frame_header *fh;
186 struct fcoe_percpu_s *fps;
188 unsigned int cpu_idx;
190 fc = container_of(ptype, struct fcoe_softc, fcoe_packet_type);
192 if (unlikely(lp == NULL)) {
193 FC_DBG("cannot find hba structure");
197 if (unlikely(debug_fcoe)) {
198 FC_DBG("skb_info: len:%d data_len:%d head:%p data:%p tail:%p "
199 "end:%p sum:%d dev:%s", skb->len, skb->data_len,
200 skb->head, skb->data, skb_tail_pointer(skb),
201 skb_end_pointer(skb), skb->csum,
202 skb->dev ? skb->dev->name : "<NULL>");
206 /* check for FCOE packet type */
207 if (unlikely(eth_hdr(skb)->h_proto != htons(ETH_P_FCOE))) {
208 FC_DBG("wrong FC type frame");
213 * Check for minimum frame length, and make sure required FCoE
214 * and FC headers are pulled into the linear data area.
216 if (unlikely((skb->len < FCOE_MIN_FRAME) ||
217 !pskb_may_pull(skb, FCOE_HEADER_LEN)))
220 skb_set_transport_header(skb, sizeof(struct fcoe_hdr));
221 fh = (struct fc_frame_header *) skb_transport_header(skb);
223 oxid = ntohs(fh->fh_ox_id);
225 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 (!cpu_online(cpu_idx))
240 cpu_idx = first_cpu(cpu_online_map);
244 fps = &per_cpu(fcoe_percpu, cpu_idx);
246 spin_lock_bh(&fps->fcoe_rx_list.lock);
247 __skb_queue_tail(&fps->fcoe_rx_list, skb);
248 if (fps->fcoe_rx_list.qlen == 1)
249 wake_up_process(fps->thread);
251 spin_unlock_bh(&fps->fcoe_rx_list.lock);
256 stats = lp->dev_stats[smp_processor_id()];
258 stats = lp->dev_stats[0];
261 stats->ErrorFrames++;
267 EXPORT_SYMBOL_GPL(fcoe_rcv);
270 * fcoe_start_io() - pass to netdev to start xmit for fcoe
271 * @skb: the skb to be xmitted
273 * Returns: 0 for success
275 static inline int fcoe_start_io(struct sk_buff *skb)
280 rc = dev_queue_xmit(skb);
288 * fcoe_get_paged_crc_eof() - in case we need alloc a page for crc_eof
289 * @skb: the skb to be xmitted
292 * Returns: 0 for success
294 static int fcoe_get_paged_crc_eof(struct sk_buff *skb, int tlen)
296 struct fcoe_percpu_s *fps;
299 fps = &get_cpu_var(fcoe_percpu);
300 page = fps->crc_eof_page;
302 page = alloc_page(GFP_ATOMIC);
304 put_cpu_var(fcoe_percpu);
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;
324 put_cpu_var(fcoe_percpu);
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
335 u32 fcoe_fc_crc(struct fc_frame *fp)
337 struct sk_buff *skb = fp_skb(fp);
338 struct skb_frag_struct *frag;
340 unsigned long off, len, clen;
344 crc = crc32(~0, skb->data, skb_headlen(skb));
346 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
347 frag = &skb_shinfo(skb)->frags[i];
348 off = frag->page_offset;
351 clen = min(len, PAGE_SIZE - (off & ~PAGE_MASK));
352 data = kmap_atomic(frag->page + (off >> PAGE_SHIFT),
353 KM_SKB_DATA_SOFTIRQ);
354 crc = crc32(crc, data + (off & ~PAGE_MASK), clen);
355 kunmap_atomic(data, KM_SKB_DATA_SOFTIRQ);
362 EXPORT_SYMBOL_GPL(fcoe_fc_crc);
365 * fcoe_xmit() - FCoE frame transmit function
366 * @lp: the associated local port
367 * @fp: the fc_frame to be transmitted
369 * Return : 0 for success
371 int fcoe_xmit(struct fc_lport *lp, struct fc_frame *fp)
376 struct fcoe_crc_eof *cp;
378 struct fcoe_dev_stats *stats;
379 struct fc_frame_header *fh;
380 unsigned int hlen; /* header length implies the version */
381 unsigned int tlen; /* trailer length */
382 unsigned int elen; /* eth header, may include vlan */
383 int flogi_in_progress = 0;
384 struct fcoe_softc *fc;
388 WARN_ON((fr_len(fp) % sizeof(u32)) != 0);
392 * if it is a flogi then we need to learn gw-addr
395 fh = fc_frame_header_get(fp);
396 if (unlikely(fh->fh_r_ctl == FC_RCTL_ELS_REQ)) {
397 if (fc_frame_payload_op(fp) == ELS_FLOGI) {
398 fc->flogi_oxid = ntohs(fh->fh_ox_id);
399 fc->address_mode = FCOE_FCOUI_ADDR_MODE;
400 fc->flogi_progress = 1;
401 flogi_in_progress = 1;
402 } else if (fc->flogi_progress && ntoh24(fh->fh_s_id) != 0) {
404 * Here we must've gotten an SID by accepting an FLOGI
405 * from a point-to-point connection. Switch to using
406 * the source mac based on the SID. The destination
407 * MAC in this case would have been set by receving the
410 fc_fcoe_set_mac(fc->data_src_addr, fh->fh_s_id);
411 fc->flogi_progress = 0;
419 elen = (fc->real_dev->priv_flags & IFF_802_1Q_VLAN) ?
420 sizeof(struct vlan_ethhdr) : sizeof(struct ethhdr);
421 hlen = sizeof(struct fcoe_hdr);
422 tlen = sizeof(struct fcoe_crc_eof);
423 wlen = (skb->len - tlen + sizeof(crc)) / FCOE_WORD_TO_BYTE;
426 if (likely(lp->crc_offload)) {
427 skb->ip_summed = CHECKSUM_PARTIAL;
428 skb->csum_start = skb_headroom(skb);
429 skb->csum_offset = skb->len;
432 skb->ip_summed = CHECKSUM_NONE;
433 crc = fcoe_fc_crc(fp);
436 /* copy fc crc and eof to the skb buff */
437 if (skb_is_nonlinear(skb)) {
439 if (fcoe_get_paged_crc_eof(skb, tlen)) {
443 frag = &skb_shinfo(skb)->frags[skb_shinfo(skb)->nr_frags - 1];
444 cp = kmap_atomic(frag->page, KM_SKB_DATA_SOFTIRQ)
447 cp = (struct fcoe_crc_eof *)skb_put(skb, tlen);
450 memset(cp, 0, sizeof(*cp));
452 cp->fcoe_crc32 = cpu_to_le32(~crc);
454 if (skb_is_nonlinear(skb)) {
455 kunmap_atomic(cp, KM_SKB_DATA_SOFTIRQ);
459 /* adjust skb netowrk/transport offsets to match mac/fcoe/fc */
460 skb_push(skb, elen + hlen);
461 skb_reset_mac_header(skb);
462 skb_reset_network_header(skb);
464 skb->protocol = htons(ETH_P_FCOE);
465 skb->dev = fc->real_dev;
467 /* fill up mac and fcoe headers */
469 eh->h_proto = htons(ETH_P_FCOE);
470 if (fc->address_mode == FCOE_FCOUI_ADDR_MODE)
471 fc_fcoe_set_mac(eh->h_dest, fh->fh_d_id);
473 /* insert GW address */
474 memcpy(eh->h_dest, fc->dest_addr, ETH_ALEN);
476 if (unlikely(flogi_in_progress))
477 memcpy(eh->h_source, fc->ctl_src_addr, ETH_ALEN);
479 memcpy(eh->h_source, fc->data_src_addr, ETH_ALEN);
481 hp = (struct fcoe_hdr *)(eh + 1);
482 memset(hp, 0, sizeof(*hp));
484 FC_FCOE_ENCAPS_VER(hp, FC_FCOE_VER);
488 /* fcoe lso, mss is in max_payload which is non-zero for FCP data */
489 if (lp->seq_offload && fr_max_payload(fp)) {
490 skb_shinfo(skb)->gso_type = SKB_GSO_FCOE;
491 skb_shinfo(skb)->gso_size = fr_max_payload(fp);
493 skb_shinfo(skb)->gso_type = 0;
494 skb_shinfo(skb)->gso_size = 0;
497 /* update tx stats: regardless if LLD fails */
498 stats = lp->dev_stats[smp_processor_id()];
501 stats->TxWords += wlen;
504 /* send down to lld */
506 if (fc->fcoe_pending_queue.qlen)
507 rc = fcoe_check_wait_queue(lp);
510 rc = fcoe_start_io(skb);
513 spin_lock_bh(&fc->fcoe_pending_queue.lock);
514 __skb_queue_tail(&fc->fcoe_pending_queue, skb);
515 spin_unlock_bh(&fc->fcoe_pending_queue.lock);
516 if (fc->fcoe_pending_queue.qlen > FCOE_MAX_QUEUE_DEPTH)
522 EXPORT_SYMBOL_GPL(fcoe_xmit);
525 * fcoe_percpu_receive_thread() - recv thread per cpu
526 * @arg: ptr to the fcoe per cpu struct
528 * Return: 0 for success
530 int fcoe_percpu_receive_thread(void *arg)
532 struct fcoe_percpu_s *p = arg;
535 struct fcoe_rcv_info *fr;
536 struct fcoe_dev_stats *stats;
537 struct fc_frame_header *fh;
539 struct fcoe_crc_eof crc_eof;
542 struct fcoe_softc *fc;
545 set_user_nice(current, -20);
547 while (!kthread_should_stop()) {
549 spin_lock_bh(&p->fcoe_rx_list.lock);
550 while ((skb = __skb_dequeue(&p->fcoe_rx_list)) == NULL) {
551 set_current_state(TASK_INTERRUPTIBLE);
552 spin_unlock_bh(&p->fcoe_rx_list.lock);
554 set_current_state(TASK_RUNNING);
555 if (kthread_should_stop())
557 spin_lock_bh(&p->fcoe_rx_list.lock);
559 spin_unlock_bh(&p->fcoe_rx_list.lock);
560 fr = fcoe_dev_from_skb(skb);
562 if (unlikely(lp == NULL)) {
563 FC_DBG("invalid HBA Structure");
568 stats = lp->dev_stats[smp_processor_id()];
570 if (unlikely(debug_fcoe)) {
571 FC_DBG("skb_info: len:%d data_len:%d head:%p data:%p "
572 "tail:%p end:%p sum:%d dev:%s",
573 skb->len, skb->data_len,
574 skb->head, skb->data, skb_tail_pointer(skb),
575 skb_end_pointer(skb), skb->csum,
576 skb->dev ? skb->dev->name : "<NULL>");
580 * Save source MAC address before discarding header.
583 if (unlikely(fc->flogi_progress))
584 mac = eth_hdr(skb)->h_source;
586 if (skb_is_nonlinear(skb))
587 skb_linearize(skb); /* not ideal */
590 * Frame length checks and setting up the header pointers
591 * was done in fcoe_rcv already.
593 hp = (struct fcoe_hdr *) skb_network_header(skb);
594 fh = (struct fc_frame_header *) skb_transport_header(skb);
596 if (unlikely(FC_FCOE_DECAPS_VER(hp) != FC_FCOE_VER)) {
598 if (stats->ErrorFrames < 5)
599 FC_DBG("unknown FCoE version %x",
600 FC_FCOE_DECAPS_VER(hp));
601 stats->ErrorFrames++;
607 skb_pull(skb, sizeof(struct fcoe_hdr));
608 fr_len = skb->len - sizeof(struct fcoe_crc_eof);
612 stats->RxWords += fr_len / FCOE_WORD_TO_BYTE;
615 fp = (struct fc_frame *)skb;
618 fr_sof(fp) = hp->fcoe_sof;
620 /* Copy out the CRC and EOF trailer for access */
621 if (skb_copy_bits(skb, fr_len, &crc_eof, sizeof(crc_eof))) {
625 fr_eof(fp) = crc_eof.fcoe_eof;
626 fr_crc(fp) = crc_eof.fcoe_crc32;
627 if (pskb_trim(skb, fr_len)) {
633 * We only check CRC if no offload is available and if it is
634 * it's solicited data, in which case, the FCP layer would
635 * check it during the copy.
637 if (lp->crc_offload && skb->ip_summed == CHECKSUM_UNNECESSARY)
638 fr_flags(fp) &= ~FCPHF_CRC_UNCHECKED;
640 fr_flags(fp) |= FCPHF_CRC_UNCHECKED;
642 fh = fc_frame_header_get(fp);
643 if (fh->fh_r_ctl == FC_RCTL_DD_SOL_DATA &&
644 fh->fh_type == FC_TYPE_FCP) {
645 fc_exch_recv(lp, lp->emp, fp);
648 if (fr_flags(fp) & FCPHF_CRC_UNCHECKED) {
649 if (le32_to_cpu(fr_crc(fp)) !=
650 ~crc32(~0, skb->data, fr_len)) {
651 if (debug_fcoe || stats->InvalidCRCCount < 5)
652 printk(KERN_WARNING "fcoe: dropping "
653 "frame with CRC error\n");
654 stats->InvalidCRCCount++;
655 stats->ErrorFrames++;
659 fr_flags(fp) &= ~FCPHF_CRC_UNCHECKED;
661 /* non flogi and non data exchanges are handled here */
662 if (unlikely(fc->flogi_progress))
663 fcoe_recv_flogi(fc, fp, mac);
664 fc_exch_recv(lp, lp->emp, fp);
670 * fcoe_recv_flogi() - flogi receive function
671 * @fc: associated fcoe_softc
672 * @fp: the recieved frame
673 * @sa: the source address of this flogi
675 * This is responsible to parse the flogi response and sets the corresponding
676 * mac address for the initiator, eitehr OUI based or GW based.
680 static void fcoe_recv_flogi(struct fcoe_softc *fc, struct fc_frame *fp, u8 *sa)
682 struct fc_frame_header *fh;
685 fh = fc_frame_header_get(fp);
686 if (fh->fh_type != FC_TYPE_ELS)
688 op = fc_frame_payload_op(fp);
689 if (op == ELS_LS_ACC && fh->fh_r_ctl == FC_RCTL_ELS_REP &&
690 fc->flogi_oxid == ntohs(fh->fh_ox_id)) {
693 * If the src mac addr is FC_OUI-based, then we mark the
694 * address_mode flag to use FC_OUI-based Ethernet DA.
695 * Otherwise we use the FCoE gateway addr
697 if (!compare_ether_addr(sa, (u8[6]) FC_FCOE_FLOGI_MAC)) {
698 fc->address_mode = FCOE_FCOUI_ADDR_MODE;
700 memcpy(fc->dest_addr, sa, ETH_ALEN);
701 fc->address_mode = FCOE_GW_ADDR_MODE;
705 * Remove any previously-set unicast MAC filter.
706 * Add secondary FCoE MAC address filter for our OUI.
709 if (compare_ether_addr(fc->data_src_addr, (u8[6]) { 0 }))
710 dev_unicast_delete(fc->real_dev, fc->data_src_addr,
712 fc_fcoe_set_mac(fc->data_src_addr, fh->fh_d_id);
713 dev_unicast_add(fc->real_dev, fc->data_src_addr, ETH_ALEN);
716 fc->flogi_progress = 0;
717 } else if (op == ELS_FLOGI && fh->fh_r_ctl == FC_RCTL_ELS_REQ && sa) {
719 * Save source MAC for point-to-point responses.
721 memcpy(fc->dest_addr, sa, ETH_ALEN);
722 fc->address_mode = FCOE_GW_ADDR_MODE;
727 * fcoe_watchdog() - fcoe timer callback
730 * This checks the pending queue length for fcoe and set lport qfull
731 * if the FCOE_MAX_QUEUE_DEPTH is reached. This is done for all fc_lport on the
734 * Returns: 0 for success
736 void fcoe_watchdog(ulong vp)
738 struct fcoe_softc *fc;
740 read_lock(&fcoe_hostlist_lock);
741 list_for_each_entry(fc, &fcoe_hostlist, list) {
743 fcoe_check_wait_queue(fc->lp);
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 qlen or -1 if a error occurs, then restore
760 * wait_queue and try again later.
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)
770 struct fcoe_softc *fc = lport_priv(lp);
774 spin_lock_bh(&fc->fcoe_pending_queue.lock);
775 if (fc->fcoe_pending_queue_active)
777 fc->fcoe_pending_queue_active = 1;
779 while (fc->fcoe_pending_queue.qlen) {
780 /* keep qlen > 0 until fcoe_start_io succeeds */
781 fc->fcoe_pending_queue.qlen++;
782 skb = __skb_dequeue(&fc->fcoe_pending_queue);
784 spin_unlock_bh(&fc->fcoe_pending_queue.lock);
785 rc = fcoe_start_io(skb);
786 spin_lock_bh(&fc->fcoe_pending_queue.lock);
789 __skb_queue_head(&fc->fcoe_pending_queue, skb);
790 /* undo temporary increment above */
791 fc->fcoe_pending_queue.qlen--;
794 /* undo temporary increment above */
795 fc->fcoe_pending_queue.qlen--;
798 if (fc->fcoe_pending_queue.qlen < FCOE_LOW_QUEUE_DEPTH)
800 fc->fcoe_pending_queue_active = 0;
801 rc = fc->fcoe_pending_queue.qlen;
803 spin_unlock_bh(&fc->fcoe_pending_queue.lock);
808 * fcoe_dev_setup() - setup link change notification interface
810 static void fcoe_dev_setup()
813 * here setup a interface specific wd time to
814 * monitor the link state
816 register_netdevice_notifier(&fcoe_notifier);
820 * fcoe_dev_setup() - cleanup link change notification interface
822 static void fcoe_dev_cleanup(void)
824 unregister_netdevice_notifier(&fcoe_notifier);
828 * fcoe_device_notification() - netdev event notification callback
829 * @notifier: context of the notification
830 * @event: type of event
831 * @ptr: fixed array for output parsed ifname
833 * This function is called by the ethernet driver in case of link change event
835 * Returns: 0 for success
837 static int fcoe_device_notification(struct notifier_block *notifier,
838 ulong event, void *ptr)
840 struct fc_lport *lp = NULL;
841 struct net_device *real_dev = ptr;
842 struct fcoe_softc *fc;
843 struct fcoe_dev_stats *stats;
848 read_lock(&fcoe_hostlist_lock);
849 list_for_each_entry(fc, &fcoe_hostlist, list) {
850 if (fc->real_dev == real_dev) {
855 read_unlock(&fcoe_hostlist_lock);
861 new_link_up = lp->link_up;
864 case NETDEV_GOING_DOWN:
869 new_link_up = !fcoe_link_ok(lp);
871 case NETDEV_CHANGEMTU:
872 mfs = fc->real_dev->mtu -
873 (sizeof(struct fcoe_hdr) +
874 sizeof(struct fcoe_crc_eof));
875 if (mfs >= FC_MIN_MAX_FRAME)
877 new_link_up = !fcoe_link_ok(lp);
879 case NETDEV_REGISTER:
882 FC_DBG("unknown event %ld call", event);
884 if (lp->link_up != new_link_up) {
888 stats = lp->dev_stats[smp_processor_id()];
890 stats->LinkFailureCount++;
892 fcoe_clean_pending_queue(lp);
900 * fcoe_if_to_netdev() - parse a name buffer to get netdev
901 * @ifname: fixed array for output parsed ifname
902 * @buffer: incoming buffer to be copied
904 * Returns: NULL or ptr to netdeive
906 static struct net_device *fcoe_if_to_netdev(const char *buffer)
909 char ifname[IFNAMSIZ + 2];
912 strlcpy(ifname, buffer, IFNAMSIZ);
913 cp = ifname + strlen(ifname);
914 while (--cp >= ifname && *cp == '\n')
916 return dev_get_by_name(&init_net, ifname);
922 * fcoe_netdev_to_module_owner() - finds out the nic drive moddule of the netdev
923 * @netdev: the target netdev
925 * Returns: ptr to the struct module, NULL for failure
927 static struct module *
928 fcoe_netdev_to_module_owner(const struct net_device *netdev)
935 dev = netdev->dev.parent;
942 return dev->driver->owner;
946 * fcoe_ethdrv_get() - Hold the Ethernet driver
947 * @netdev: the target netdev
949 * Holds the Ethernet driver module by try_module_get() for
950 * the corresponding netdev.
952 * Returns: 0 for succsss
954 static int fcoe_ethdrv_get(const struct net_device *netdev)
956 struct module *owner;
958 owner = fcoe_netdev_to_module_owner(netdev);
960 printk(KERN_DEBUG "fcoe:hold driver module %s for %s\n",
961 module_name(owner), netdev->name);
962 return try_module_get(owner);
968 * fcoe_ethdrv_put() - Release the Ethernet driver
969 * @netdev: the target netdev
971 * Releases the Ethernet driver module by module_put for
972 * the corresponding netdev.
974 * Returns: 0 for succsss
976 static int fcoe_ethdrv_put(const struct net_device *netdev)
978 struct module *owner;
980 owner = fcoe_netdev_to_module_owner(netdev);
982 printk(KERN_DEBUG "fcoe:release driver module %s for %s\n",
983 module_name(owner), netdev->name);
991 * fcoe_destroy() - handles the destroy from sysfs
992 * @buffer: expcted to be a eth if name
993 * @kp: associated kernel param
995 * Returns: 0 for success
997 static int fcoe_destroy(const char *buffer, struct kernel_param *kp)
1000 struct net_device *netdev;
1002 netdev = fcoe_if_to_netdev(buffer);
1007 /* look for existing lport */
1008 if (!fcoe_hostlist_lookup(netdev)) {
1012 /* pass to transport */
1013 rc = fcoe_transport_release(netdev);
1015 printk(KERN_ERR "fcoe: fcoe_transport_release(%s) failed\n",
1020 fcoe_ethdrv_put(netdev);
1029 * fcoe_create() - Handles the create call from sysfs
1030 * @buffer: expcted to be a eth if name
1031 * @kp: associated kernel param
1033 * Returns: 0 for success
1035 static int fcoe_create(const char *buffer, struct kernel_param *kp)
1038 struct net_device *netdev;
1040 netdev = fcoe_if_to_netdev(buffer);
1045 /* look for existing lport */
1046 if (fcoe_hostlist_lookup(netdev)) {
1050 fcoe_ethdrv_get(netdev);
1052 /* pass to transport */
1053 rc = fcoe_transport_attach(netdev);
1055 printk(KERN_ERR "fcoe: fcoe_transport_attach(%s) failed\n",
1057 fcoe_ethdrv_put(netdev);
1068 module_param_call(create, fcoe_create, NULL, NULL, S_IWUSR);
1069 __MODULE_PARM_TYPE(create, "string");
1070 MODULE_PARM_DESC(create, "Create fcoe port using net device passed in.");
1071 module_param_call(destroy, fcoe_destroy, NULL, NULL, S_IWUSR);
1072 __MODULE_PARM_TYPE(destroy, "string");
1073 MODULE_PARM_DESC(destroy, "Destroy fcoe port");
1076 * fcoe_link_ok() - Check if link is ok for the fc_lport
1077 * @lp: ptr to the fc_lport
1079 * Any permanently-disqualifying conditions have been previously checked.
1080 * This also updates the speed setting, which may change with link for 100/1000.
1082 * This function should probably be checking for PAUSE support at some point
1083 * in the future. Currently Per-priority-pause is not determinable using
1084 * ethtool, so we shouldn't be restrictive until that problem is resolved.
1086 * Returns: 0 if link is OK for use by FCoE.
1089 int fcoe_link_ok(struct fc_lport *lp)
1091 struct fcoe_softc *fc = lport_priv(lp);
1092 struct net_device *dev = fc->real_dev;
1093 struct ethtool_cmd ecmd = { ETHTOOL_GSET };
1096 if ((dev->flags & IFF_UP) && netif_carrier_ok(dev)) {
1098 if (dev->ethtool_ops->get_settings) {
1099 dev->ethtool_ops->get_settings(dev, &ecmd);
1100 lp->link_supported_speeds &=
1101 ~(FC_PORTSPEED_1GBIT | FC_PORTSPEED_10GBIT);
1102 if (ecmd.supported & (SUPPORTED_1000baseT_Half |
1103 SUPPORTED_1000baseT_Full))
1104 lp->link_supported_speeds |= FC_PORTSPEED_1GBIT;
1105 if (ecmd.supported & SUPPORTED_10000baseT_Full)
1106 lp->link_supported_speeds |=
1107 FC_PORTSPEED_10GBIT;
1108 if (ecmd.speed == SPEED_1000)
1109 lp->link_speed = FC_PORTSPEED_1GBIT;
1110 if (ecmd.speed == SPEED_10000)
1111 lp->link_speed = FC_PORTSPEED_10GBIT;
1118 EXPORT_SYMBOL_GPL(fcoe_link_ok);
1121 * fcoe_percpu_clean() - Clear the pending skbs for an lport
1124 void fcoe_percpu_clean(struct fc_lport *lp)
1126 struct fcoe_percpu_s *pp;
1127 struct fcoe_rcv_info *fr;
1128 struct sk_buff_head *list;
1129 struct sk_buff *skb, *next;
1130 struct sk_buff *head;
1133 for_each_possible_cpu(cpu) {
1134 pp = &per_cpu(fcoe_percpu, cpu);
1135 spin_lock_bh(&pp->fcoe_rx_list.lock);
1136 list = &pp->fcoe_rx_list;
1138 for (skb = head; skb != (struct sk_buff *)list;
1141 fr = fcoe_dev_from_skb(skb);
1142 if (fr->fr_dev == lp) {
1143 __skb_unlink(skb, list);
1147 spin_unlock_bh(&pp->fcoe_rx_list.lock);
1150 EXPORT_SYMBOL_GPL(fcoe_percpu_clean);
1153 * fcoe_clean_pending_queue() - Dequeue a skb and free it
1154 * @lp: the corresponding fc_lport
1158 void fcoe_clean_pending_queue(struct fc_lport *lp)
1160 struct fcoe_softc *fc = lport_priv(lp);
1161 struct sk_buff *skb;
1163 spin_lock_bh(&fc->fcoe_pending_queue.lock);
1164 while ((skb = __skb_dequeue(&fc->fcoe_pending_queue)) != NULL) {
1165 spin_unlock_bh(&fc->fcoe_pending_queue.lock);
1167 spin_lock_bh(&fc->fcoe_pending_queue.lock);
1169 spin_unlock_bh(&fc->fcoe_pending_queue.lock);
1171 EXPORT_SYMBOL_GPL(fcoe_clean_pending_queue);
1174 * libfc_host_alloc() - Allocate a Scsi_Host with room for the fc_lport
1175 * @sht: ptr to the scsi host templ
1176 * @priv_size: size of private data after fc_lport
1178 * Returns: ptr to Scsi_Host
1181 static inline struct Scsi_Host *
1182 libfc_host_alloc(struct scsi_host_template *sht, int priv_size)
1184 return scsi_host_alloc(sht, sizeof(struct fc_lport) + priv_size);
1188 * fcoe_host_alloc() - Allocate a Scsi_Host with room for the fcoe_softc
1189 * @sht: ptr to the scsi host templ
1190 * @priv_size: size of private data after fc_lport
1192 * Returns: ptr to Scsi_Host
1194 struct Scsi_Host *fcoe_host_alloc(struct scsi_host_template *sht, int priv_size)
1196 return libfc_host_alloc(sht, sizeof(struct fcoe_softc) + priv_size);
1198 EXPORT_SYMBOL_GPL(fcoe_host_alloc);
1201 * fcoe_reset() - Resets the fcoe
1202 * @shost: shost the reset is from
1206 int fcoe_reset(struct Scsi_Host *shost)
1208 struct fc_lport *lport = shost_priv(shost);
1209 fc_lport_reset(lport);
1212 EXPORT_SYMBOL_GPL(fcoe_reset);
1215 * fcoe_wwn_from_mac() - Converts 48-bit IEEE MAC address to 64-bit FC WWN.
1217 * @scheme: check port
1218 * @port: port indicator for converting
1220 * Returns: u64 fc world wide name
1222 u64 fcoe_wwn_from_mac(unsigned char mac[MAX_ADDR_LEN],
1223 unsigned int scheme, unsigned int port)
1228 /* The MAC is in NO, so flip only the low 48 bits */
1229 host_mac = ((u64) mac[0] << 40) |
1230 ((u64) mac[1] << 32) |
1231 ((u64) mac[2] << 24) |
1232 ((u64) mac[3] << 16) |
1233 ((u64) mac[4] << 8) |
1236 WARN_ON(host_mac >= (1ULL << 48));
1237 wwn = host_mac | ((u64) scheme << 60);
1243 WARN_ON(port >= 0xfff);
1244 wwn |= (u64) port << 48;
1253 EXPORT_SYMBOL_GPL(fcoe_wwn_from_mac);
1256 * fcoe_hostlist_lookup_softc() - find the corresponding lport by a given device
1257 * @device: this is currently ptr to net_device
1259 * Returns: NULL or the located fcoe_softc
1261 static struct fcoe_softc *
1262 fcoe_hostlist_lookup_softc(const struct net_device *dev)
1264 struct fcoe_softc *fc;
1266 read_lock(&fcoe_hostlist_lock);
1267 list_for_each_entry(fc, &fcoe_hostlist, list) {
1268 if (fc->real_dev == dev) {
1269 read_unlock(&fcoe_hostlist_lock);
1273 read_unlock(&fcoe_hostlist_lock);
1278 * fcoe_hostlist_lookup() - Find the corresponding lport by netdev
1279 * @netdev: ptr to net_device
1281 * Returns: 0 for success
1283 struct fc_lport *fcoe_hostlist_lookup(const struct net_device *netdev)
1285 struct fcoe_softc *fc;
1287 fc = fcoe_hostlist_lookup_softc(netdev);
1289 return (fc) ? fc->lp : NULL;
1291 EXPORT_SYMBOL_GPL(fcoe_hostlist_lookup);
1294 * fcoe_hostlist_add() - Add a lport to lports list
1295 * @lp: ptr to the fc_lport to badded
1297 * Returns: 0 for success
1299 int fcoe_hostlist_add(const struct fc_lport *lp)
1301 struct fcoe_softc *fc;
1303 fc = fcoe_hostlist_lookup_softc(fcoe_netdev(lp));
1305 fc = lport_priv(lp);
1306 write_lock_bh(&fcoe_hostlist_lock);
1307 list_add_tail(&fc->list, &fcoe_hostlist);
1308 write_unlock_bh(&fcoe_hostlist_lock);
1312 EXPORT_SYMBOL_GPL(fcoe_hostlist_add);
1315 * fcoe_hostlist_remove() - remove a lport from lports list
1316 * @lp: ptr to the fc_lport to badded
1318 * Returns: 0 for success
1320 int fcoe_hostlist_remove(const struct fc_lport *lp)
1322 struct fcoe_softc *fc;
1324 fc = fcoe_hostlist_lookup_softc(fcoe_netdev(lp));
1326 write_lock_bh(&fcoe_hostlist_lock);
1327 list_del(&fc->list);
1328 write_unlock_bh(&fcoe_hostlist_lock);
1332 EXPORT_SYMBOL_GPL(fcoe_hostlist_remove);
1335 * fcoe_libfc_config() - sets up libfc related properties for lport
1336 * @lp: ptr to the fc_lport
1337 * @tt: libfc function template
1339 * Returns : 0 for success
1341 int fcoe_libfc_config(struct fc_lport *lp, struct libfc_function_template *tt)
1343 /* Set the function pointers set by the LLDD */
1344 memcpy(&lp->tt, tt, sizeof(*tt));
1345 if (fc_fcp_init(lp))
1355 EXPORT_SYMBOL_GPL(fcoe_libfc_config);
1358 * fcoe_init() - fcoe module loading initialization
1360 * Initialization routine
1361 * 1. Will create fc transport software structure
1362 * 2. initialize the link list of port information structure
1364 * Returns 0 on success, negative on failure
1366 static int __init fcoe_init(void)
1369 struct fcoe_percpu_s *p;
1371 INIT_LIST_HEAD(&fcoe_hostlist);
1372 rwlock_init(&fcoe_hostlist_lock);
1374 #ifdef CONFIG_HOTPLUG_CPU
1375 register_cpu_notifier(&fcoe_cpu_notifier);
1376 #endif /* CONFIG_HOTPLUG_CPU */
1378 for_each_possible_cpu(cpu) {
1379 p = &per_cpu(fcoe_percpu, cpu);
1380 skb_queue_head_init(&p->fcoe_rx_list);
1384 * initialize per CPU interrupt thread
1386 for_each_online_cpu(cpu) {
1387 p = &per_cpu(fcoe_percpu, cpu);
1388 p->thread = kthread_create(fcoe_percpu_receive_thread,
1389 (void *)p, "fcoethread/%d", cpu);
1392 * If there is no error then bind the thread to the CPU
1395 if (!IS_ERR(p->thread)) {
1396 kthread_bind(p->thread, cpu);
1397 wake_up_process(p->thread);
1404 * setup link change notification
1408 setup_timer(&fcoe_timer, fcoe_watchdog, 0);
1410 mod_timer(&fcoe_timer, jiffies + (10 * HZ));
1412 /* initiatlize the fcoe transport */
1413 fcoe_transport_init();
1419 module_init(fcoe_init);
1422 * fcoe_exit() - fcoe module unloading cleanup
1424 * Returns 0 on success, negative on failure
1426 static void __exit fcoe_exit(void)
1429 struct fcoe_softc *fc, *tmp;
1430 struct fcoe_percpu_s *p;
1431 struct sk_buff *skb;
1434 * Stop all call back interfaces
1436 #ifdef CONFIG_HOTPLUG_CPU
1437 unregister_cpu_notifier(&fcoe_cpu_notifier);
1438 #endif /* CONFIG_HOTPLUG_CPU */
1444 del_timer_sync(&fcoe_timer);
1446 /* releases the associated fcoe transport for each lport */
1447 list_for_each_entry_safe(fc, tmp, &fcoe_hostlist, list)
1448 fcoe_transport_release(fc->real_dev);
1450 for_each_possible_cpu(cpu) {
1451 p = &per_cpu(fcoe_percpu, cpu);
1453 kthread_stop(p->thread);
1454 spin_lock_bh(&p->fcoe_rx_list.lock);
1455 while ((skb = __skb_dequeue(&p->fcoe_rx_list)) != NULL)
1457 spin_unlock_bh(&p->fcoe_rx_list.lock);
1458 if (p->crc_eof_page)
1459 put_page(p->crc_eof_page);
1463 /* remove sw trasnport */
1466 /* detach the transport */
1467 fcoe_transport_exit();
1469 module_exit(fcoe_exit);