blob: 14dd8a0402b63a5a96ffbefdf7aceb097db7044f [file] [log] [blame]
Robert Love85b4aa42008-12-09 15:10:24 -08001/*
2 * Copyright(c) 2007 - 2008 Intel Corporation. All rights reserved.
3 *
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.
7 *
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
11 * more details.
12 *
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.
16 *
17 * Maintained at www.Open-FCoE.org
18 */
19
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>
33#include <linux/fs.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>
41
42#include <scsi/fc/fc_encaps.h>
43
44#include <scsi/libfc.h>
45#include <scsi/fc_frame.h>
46#include <scsi/libfcoe.h>
47#include <scsi/fc_transport_fcoe.h>
48
49static int debug_fcoe;
50
51#define FCOE_MAX_QUEUE_DEPTH 256
Vasu Devc826a312009-02-27 10:56:27 -080052#define FCOE_LOW_QUEUE_DEPTH 32
Robert Love85b4aa42008-12-09 15:10:24 -080053
54/* destination address mode */
55#define FCOE_GW_ADDR_MODE 0x00
56#define FCOE_FCOUI_ADDR_MODE 0x01
57
58#define FCOE_WORD_TO_BYTE 4
59
60MODULE_AUTHOR("Open-FCoE.org");
61MODULE_DESCRIPTION("FCoE");
62MODULE_LICENSE("GPL");
63
64/* fcoe host list */
65LIST_HEAD(fcoe_hostlist);
66DEFINE_RWLOCK(fcoe_hostlist_lock);
67DEFINE_TIMER(fcoe_timer, NULL, 0, 0);
68struct fcoe_percpu_s *fcoe_percpu[NR_CPUS];
69
70
71/* Function Prototyes */
72static int fcoe_check_wait_queue(struct fc_lport *);
73static void fcoe_insert_wait_queue_head(struct fc_lport *, struct sk_buff *);
74static void fcoe_insert_wait_queue(struct fc_lport *, struct sk_buff *);
75static void fcoe_recv_flogi(struct fcoe_softc *, struct fc_frame *, u8 *);
76#ifdef CONFIG_HOTPLUG_CPU
77static int fcoe_cpu_callback(struct notifier_block *, ulong, void *);
78#endif /* CONFIG_HOTPLUG_CPU */
79static int fcoe_device_notification(struct notifier_block *, ulong, void *);
80static void fcoe_dev_setup(void);
81static void fcoe_dev_cleanup(void);
82
83/* notification function from net device */
84static struct notifier_block fcoe_notifier = {
85 .notifier_call = fcoe_device_notification,
86};
87
88
89#ifdef CONFIG_HOTPLUG_CPU
90static struct notifier_block fcoe_cpu_notifier = {
91 .notifier_call = fcoe_cpu_callback,
92};
93
94/**
Robert Love34f42a02009-02-27 10:55:45 -080095 * fcoe_create_percpu_data() - creates the associated cpu data
Robert Love85b4aa42008-12-09 15:10:24 -080096 * @cpu: index for the cpu where fcoe cpu data will be created
97 *
98 * create percpu stats block, from cpu add notifier
99 *
100 * Returns: none
Robert Love34f42a02009-02-27 10:55:45 -0800101 */
Robert Love85b4aa42008-12-09 15:10:24 -0800102static void fcoe_create_percpu_data(int cpu)
103{
104 struct fc_lport *lp;
105 struct fcoe_softc *fc;
106
107 write_lock_bh(&fcoe_hostlist_lock);
108 list_for_each_entry(fc, &fcoe_hostlist, list) {
109 lp = fc->lp;
110 if (lp->dev_stats[cpu] == NULL)
111 lp->dev_stats[cpu] =
112 kzalloc(sizeof(struct fcoe_dev_stats),
113 GFP_KERNEL);
114 }
115 write_unlock_bh(&fcoe_hostlist_lock);
116}
117
118/**
Robert Love34f42a02009-02-27 10:55:45 -0800119 * fcoe_destroy_percpu_data() - destroys the associated cpu data
Robert Love85b4aa42008-12-09 15:10:24 -0800120 * @cpu: index for the cpu where fcoe cpu data will destroyed
121 *
122 * destroy percpu stats block called by cpu add/remove notifier
123 *
124 * Retuns: none
Robert Love34f42a02009-02-27 10:55:45 -0800125 */
Robert Love85b4aa42008-12-09 15:10:24 -0800126static void fcoe_destroy_percpu_data(int cpu)
127{
128 struct fc_lport *lp;
129 struct fcoe_softc *fc;
130
131 write_lock_bh(&fcoe_hostlist_lock);
132 list_for_each_entry(fc, &fcoe_hostlist, list) {
133 lp = fc->lp;
134 kfree(lp->dev_stats[cpu]);
135 lp->dev_stats[cpu] = NULL;
136 }
137 write_unlock_bh(&fcoe_hostlist_lock);
138}
139
140/**
Robert Love34f42a02009-02-27 10:55:45 -0800141 * fcoe_cpu_callback() - fcoe cpu hotplug event callback
Robert Love85b4aa42008-12-09 15:10:24 -0800142 * @nfb: callback data block
143 * @action: event triggering the callback
144 * @hcpu: index for the cpu of this event
145 *
146 * this creates or destroys per cpu data for fcoe
147 *
148 * Returns NOTIFY_OK always.
Robert Love34f42a02009-02-27 10:55:45 -0800149 */
Robert Love85b4aa42008-12-09 15:10:24 -0800150static int fcoe_cpu_callback(struct notifier_block *nfb, unsigned long action,
151 void *hcpu)
152{
153 unsigned int cpu = (unsigned long)hcpu;
154
155 switch (action) {
156 case CPU_ONLINE:
157 fcoe_create_percpu_data(cpu);
158 break;
159 case CPU_DEAD:
160 fcoe_destroy_percpu_data(cpu);
161 break;
162 default:
163 break;
164 }
165 return NOTIFY_OK;
166}
167#endif /* CONFIG_HOTPLUG_CPU */
168
169/**
Robert Love34f42a02009-02-27 10:55:45 -0800170 * fcoe_rcv() - this is the fcoe receive function called by NET_RX_SOFTIRQ
Robert Love85b4aa42008-12-09 15:10:24 -0800171 * @skb: the receive skb
172 * @dev: associated net device
173 * @ptype: context
174 * @odldev: last device
175 *
176 * this function will receive the packet and build fc frame and pass it up
177 *
178 * Returns: 0 for success
Robert Love34f42a02009-02-27 10:55:45 -0800179 */
Robert Love85b4aa42008-12-09 15:10:24 -0800180int fcoe_rcv(struct sk_buff *skb, struct net_device *dev,
181 struct packet_type *ptype, struct net_device *olddev)
182{
183 struct fc_lport *lp;
184 struct fcoe_rcv_info *fr;
185 struct fcoe_softc *fc;
186 struct fcoe_dev_stats *stats;
187 struct fc_frame_header *fh;
188 unsigned short oxid;
189 int cpu_idx;
190 struct fcoe_percpu_s *fps;
191
192 fc = container_of(ptype, struct fcoe_softc, fcoe_packet_type);
193 lp = fc->lp;
194 if (unlikely(lp == NULL)) {
195 FC_DBG("cannot find hba structure");
196 goto err2;
197 }
198
199 if (unlikely(debug_fcoe)) {
200 FC_DBG("skb_info: len:%d data_len:%d head:%p data:%p tail:%p "
201 "end:%p sum:%d dev:%s", skb->len, skb->data_len,
202 skb->head, skb->data, skb_tail_pointer(skb),
203 skb_end_pointer(skb), skb->csum,
204 skb->dev ? skb->dev->name : "<NULL>");
205
206 }
207
208 /* check for FCOE packet type */
209 if (unlikely(eth_hdr(skb)->h_proto != htons(ETH_P_FCOE))) {
210 FC_DBG("wrong FC type frame");
211 goto err;
212 }
213
214 /*
215 * Check for minimum frame length, and make sure required FCoE
216 * and FC headers are pulled into the linear data area.
217 */
218 if (unlikely((skb->len < FCOE_MIN_FRAME) ||
219 !pskb_may_pull(skb, FCOE_HEADER_LEN)))
220 goto err;
221
222 skb_set_transport_header(skb, sizeof(struct fcoe_hdr));
223 fh = (struct fc_frame_header *) skb_transport_header(skb);
224
225 oxid = ntohs(fh->fh_ox_id);
226
227 fr = fcoe_dev_from_skb(skb);
228 fr->fr_dev = lp;
229 fr->ptype = ptype;
230 cpu_idx = 0;
231#ifdef CONFIG_SMP
232 /*
233 * The incoming frame exchange id(oxid) is ANDed with num of online
234 * cpu bits to get cpu_idx and then this cpu_idx is used for selecting
235 * a per cpu kernel thread from fcoe_percpu. In case the cpu is
236 * offline or no kernel thread for derived cpu_idx then cpu_idx is
237 * initialize to first online cpu index.
238 */
239 cpu_idx = oxid & (num_online_cpus() - 1);
240 if (!fcoe_percpu[cpu_idx] || !cpu_online(cpu_idx))
241 cpu_idx = first_cpu(cpu_online_map);
242#endif
243 fps = fcoe_percpu[cpu_idx];
244
245 spin_lock_bh(&fps->fcoe_rx_list.lock);
246 __skb_queue_tail(&fps->fcoe_rx_list, skb);
247 if (fps->fcoe_rx_list.qlen == 1)
248 wake_up_process(fps->thread);
249
250 spin_unlock_bh(&fps->fcoe_rx_list.lock);
251
252 return 0;
253err:
254#ifdef CONFIG_SMP
255 stats = lp->dev_stats[smp_processor_id()];
256#else
257 stats = lp->dev_stats[0];
258#endif
259 if (stats)
260 stats->ErrorFrames++;
261
262err2:
263 kfree_skb(skb);
264 return -1;
265}
266EXPORT_SYMBOL_GPL(fcoe_rcv);
267
268/**
Robert Love34f42a02009-02-27 10:55:45 -0800269 * fcoe_start_io() - pass to netdev to start xmit for fcoe
Robert Love85b4aa42008-12-09 15:10:24 -0800270 * @skb: the skb to be xmitted
271 *
272 * Returns: 0 for success
Robert Love34f42a02009-02-27 10:55:45 -0800273 */
Robert Love85b4aa42008-12-09 15:10:24 -0800274static inline int fcoe_start_io(struct sk_buff *skb)
275{
276 int rc;
277
278 skb_get(skb);
279 rc = dev_queue_xmit(skb);
280 if (rc != 0)
281 return rc;
282 kfree_skb(skb);
283 return 0;
284}
285
286/**
Robert Love34f42a02009-02-27 10:55:45 -0800287 * fcoe_get_paged_crc_eof() - in case we need alloc a page for crc_eof
Robert Love85b4aa42008-12-09 15:10:24 -0800288 * @skb: the skb to be xmitted
289 * @tlen: total len
290 *
291 * Returns: 0 for success
Robert Love34f42a02009-02-27 10:55:45 -0800292 */
Robert Love85b4aa42008-12-09 15:10:24 -0800293static int fcoe_get_paged_crc_eof(struct sk_buff *skb, int tlen)
294{
295 struct fcoe_percpu_s *fps;
296 struct page *page;
297 int cpu_idx;
298
299 cpu_idx = get_cpu();
300 fps = fcoe_percpu[cpu_idx];
301 page = fps->crc_eof_page;
302 if (!page) {
303 page = alloc_page(GFP_ATOMIC);
304 if (!page) {
305 put_cpu();
306 return -ENOMEM;
307 }
308 fps->crc_eof_page = page;
309 WARN_ON(fps->crc_eof_offset != 0);
310 }
311
312 get_page(page);
313 skb_fill_page_desc(skb, skb_shinfo(skb)->nr_frags, page,
314 fps->crc_eof_offset, tlen);
315 skb->len += tlen;
316 skb->data_len += tlen;
317 skb->truesize += tlen;
318 fps->crc_eof_offset += sizeof(struct fcoe_crc_eof);
319
320 if (fps->crc_eof_offset >= PAGE_SIZE) {
321 fps->crc_eof_page = NULL;
322 fps->crc_eof_offset = 0;
323 put_page(page);
324 }
325 put_cpu();
326 return 0;
327}
328
329/**
Robert Love34f42a02009-02-27 10:55:45 -0800330 * fcoe_fc_crc() - calculates FC CRC in this fcoe skb
Robert Love85b4aa42008-12-09 15:10:24 -0800331 * @fp: the fc_frame containg data to be checksummed
332 *
333 * This uses crc32() to calculate the crc for fc frame
334 * Return : 32 bit crc
Robert Love34f42a02009-02-27 10:55:45 -0800335 */
Robert Love85b4aa42008-12-09 15:10:24 -0800336u32 fcoe_fc_crc(struct fc_frame *fp)
337{
338 struct sk_buff *skb = fp_skb(fp);
339 struct skb_frag_struct *frag;
340 unsigned char *data;
341 unsigned long off, len, clen;
342 u32 crc;
343 unsigned i;
344
345 crc = crc32(~0, skb->data, skb_headlen(skb));
346
347 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
348 frag = &skb_shinfo(skb)->frags[i];
349 off = frag->page_offset;
350 len = frag->size;
351 while (len > 0) {
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);
357 off += clen;
358 len -= clen;
359 }
360 }
361 return crc;
362}
363EXPORT_SYMBOL_GPL(fcoe_fc_crc);
364
365/**
Robert Love34f42a02009-02-27 10:55:45 -0800366 * fcoe_xmit() - FCoE frame transmit function
Robert Love85b4aa42008-12-09 15:10:24 -0800367 * @lp: the associated local port
368 * @fp: the fc_frame to be transmitted
369 *
370 * Return : 0 for success
Robert Love34f42a02009-02-27 10:55:45 -0800371 */
Robert Love85b4aa42008-12-09 15:10:24 -0800372int fcoe_xmit(struct fc_lport *lp, struct fc_frame *fp)
373{
374 int wlen, rc = 0;
375 u32 crc;
376 struct ethhdr *eh;
377 struct fcoe_crc_eof *cp;
378 struct sk_buff *skb;
379 struct fcoe_dev_stats *stats;
380 struct fc_frame_header *fh;
381 unsigned int hlen; /* header length implies the version */
382 unsigned int tlen; /* trailer length */
383 unsigned int elen; /* eth header, may include vlan */
384 int flogi_in_progress = 0;
385 struct fcoe_softc *fc;
386 u8 sof, eof;
387 struct fcoe_hdr *hp;
388
389 WARN_ON((fr_len(fp) % sizeof(u32)) != 0);
390
Robert Lovefc47ff62009-02-27 10:55:55 -0800391 fc = lport_priv(lp);
Robert Love85b4aa42008-12-09 15:10:24 -0800392 /*
393 * if it is a flogi then we need to learn gw-addr
394 * and my own fcid
395 */
396 fh = fc_frame_header_get(fp);
397 if (unlikely(fh->fh_r_ctl == FC_RCTL_ELS_REQ)) {
398 if (fc_frame_payload_op(fp) == ELS_FLOGI) {
399 fc->flogi_oxid = ntohs(fh->fh_ox_id);
400 fc->address_mode = FCOE_FCOUI_ADDR_MODE;
401 fc->flogi_progress = 1;
402 flogi_in_progress = 1;
403 } else if (fc->flogi_progress && ntoh24(fh->fh_s_id) != 0) {
404 /*
405 * Here we must've gotten an SID by accepting an FLOGI
406 * from a point-to-point connection. Switch to using
407 * the source mac based on the SID. The destination
408 * MAC in this case would have been set by receving the
409 * FLOGI.
410 */
411 fc_fcoe_set_mac(fc->data_src_addr, fh->fh_s_id);
412 fc->flogi_progress = 0;
413 }
414 }
415
416 skb = fp_skb(fp);
417 sof = fr_sof(fp);
418 eof = fr_eof(fp);
419
420 elen = (fc->real_dev->priv_flags & IFF_802_1Q_VLAN) ?
421 sizeof(struct vlan_ethhdr) : sizeof(struct ethhdr);
422 hlen = sizeof(struct fcoe_hdr);
423 tlen = sizeof(struct fcoe_crc_eof);
424 wlen = (skb->len - tlen + sizeof(crc)) / FCOE_WORD_TO_BYTE;
425
426 /* crc offload */
427 if (likely(lp->crc_offload)) {
428 skb->ip_summed = CHECKSUM_COMPLETE;
429 skb->csum_start = skb_headroom(skb);
430 skb->csum_offset = skb->len;
431 crc = 0;
432 } else {
433 skb->ip_summed = CHECKSUM_NONE;
434 crc = fcoe_fc_crc(fp);
435 }
436
437 /* copy fc crc and eof to the skb buff */
438 if (skb_is_nonlinear(skb)) {
439 skb_frag_t *frag;
440 if (fcoe_get_paged_crc_eof(skb, tlen)) {
Roel Kluine9041582009-02-27 10:56:22 -0800441 kfree_skb(skb);
Robert Love85b4aa42008-12-09 15:10:24 -0800442 return -ENOMEM;
443 }
444 frag = &skb_shinfo(skb)->frags[skb_shinfo(skb)->nr_frags - 1];
445 cp = kmap_atomic(frag->page, KM_SKB_DATA_SOFTIRQ)
446 + frag->page_offset;
447 } else {
448 cp = (struct fcoe_crc_eof *)skb_put(skb, tlen);
449 }
450
451 memset(cp, 0, sizeof(*cp));
452 cp->fcoe_eof = eof;
453 cp->fcoe_crc32 = cpu_to_le32(~crc);
454
455 if (skb_is_nonlinear(skb)) {
456 kunmap_atomic(cp, KM_SKB_DATA_SOFTIRQ);
457 cp = NULL;
458 }
459
460 /* adjust skb netowrk/transport offsets to match mac/fcoe/fc */
461 skb_push(skb, elen + hlen);
462 skb_reset_mac_header(skb);
463 skb_reset_network_header(skb);
464 skb->mac_len = elen;
465 skb->protocol = htons(ETH_P_802_3);
466 skb->dev = fc->real_dev;
467
468 /* fill up mac and fcoe headers */
469 eh = eth_hdr(skb);
470 eh->h_proto = htons(ETH_P_FCOE);
471 if (fc->address_mode == FCOE_FCOUI_ADDR_MODE)
472 fc_fcoe_set_mac(eh->h_dest, fh->fh_d_id);
473 else
474 /* insert GW address */
475 memcpy(eh->h_dest, fc->dest_addr, ETH_ALEN);
476
477 if (unlikely(flogi_in_progress))
478 memcpy(eh->h_source, fc->ctl_src_addr, ETH_ALEN);
479 else
480 memcpy(eh->h_source, fc->data_src_addr, ETH_ALEN);
481
482 hp = (struct fcoe_hdr *)(eh + 1);
483 memset(hp, 0, sizeof(*hp));
484 if (FC_FCOE_VER)
485 FC_FCOE_ENCAPS_VER(hp, FC_FCOE_VER);
486 hp->fcoe_sof = sof;
487
488 /* update tx stats: regardless if LLD fails */
489 stats = lp->dev_stats[smp_processor_id()];
490 if (stats) {
491 stats->TxFrames++;
492 stats->TxWords += wlen;
493 }
494
495 /* send down to lld */
496 fr_dev(fp) = lp;
497 if (fc->fcoe_pending_queue.qlen)
498 rc = fcoe_check_wait_queue(lp);
499
500 if (rc == 0)
501 rc = fcoe_start_io(skb);
502
503 if (rc) {
504 fcoe_insert_wait_queue(lp, skb);
505 if (fc->fcoe_pending_queue.qlen > FCOE_MAX_QUEUE_DEPTH)
Vasu Devbc0e17f2009-02-27 10:54:57 -0800506 lp->qfull = 1;
Robert Love85b4aa42008-12-09 15:10:24 -0800507 }
508
509 return 0;
510}
511EXPORT_SYMBOL_GPL(fcoe_xmit);
512
Robert Love34f42a02009-02-27 10:55:45 -0800513/**
514 * fcoe_percpu_receive_thread() - recv thread per cpu
Robert Love85b4aa42008-12-09 15:10:24 -0800515 * @arg: ptr to the fcoe per cpu struct
516 *
517 * Return: 0 for success
Robert Love85b4aa42008-12-09 15:10:24 -0800518 */
519int fcoe_percpu_receive_thread(void *arg)
520{
521 struct fcoe_percpu_s *p = arg;
522 u32 fr_len;
523 struct fc_lport *lp;
524 struct fcoe_rcv_info *fr;
525 struct fcoe_dev_stats *stats;
526 struct fc_frame_header *fh;
527 struct sk_buff *skb;
528 struct fcoe_crc_eof crc_eof;
529 struct fc_frame *fp;
530 u8 *mac = NULL;
531 struct fcoe_softc *fc;
532 struct fcoe_hdr *hp;
533
534 set_user_nice(current, 19);
535
536 while (!kthread_should_stop()) {
537
538 spin_lock_bh(&p->fcoe_rx_list.lock);
539 while ((skb = __skb_dequeue(&p->fcoe_rx_list)) == NULL) {
540 set_current_state(TASK_INTERRUPTIBLE);
541 spin_unlock_bh(&p->fcoe_rx_list.lock);
542 schedule();
543 set_current_state(TASK_RUNNING);
544 if (kthread_should_stop())
545 return 0;
546 spin_lock_bh(&p->fcoe_rx_list.lock);
547 }
548 spin_unlock_bh(&p->fcoe_rx_list.lock);
549 fr = fcoe_dev_from_skb(skb);
550 lp = fr->fr_dev;
551 if (unlikely(lp == NULL)) {
552 FC_DBG("invalid HBA Structure");
553 kfree_skb(skb);
554 continue;
555 }
556
557 stats = lp->dev_stats[smp_processor_id()];
558
559 if (unlikely(debug_fcoe)) {
560 FC_DBG("skb_info: len:%d data_len:%d head:%p data:%p "
561 "tail:%p end:%p sum:%d dev:%s",
562 skb->len, skb->data_len,
563 skb->head, skb->data, skb_tail_pointer(skb),
564 skb_end_pointer(skb), skb->csum,
565 skb->dev ? skb->dev->name : "<NULL>");
566 }
567
568 /*
569 * Save source MAC address before discarding header.
570 */
571 fc = lport_priv(lp);
572 if (unlikely(fc->flogi_progress))
573 mac = eth_hdr(skb)->h_source;
574
575 if (skb_is_nonlinear(skb))
576 skb_linearize(skb); /* not ideal */
577
578 /*
579 * Frame length checks and setting up the header pointers
580 * was done in fcoe_rcv already.
581 */
582 hp = (struct fcoe_hdr *) skb_network_header(skb);
583 fh = (struct fc_frame_header *) skb_transport_header(skb);
584
585 if (unlikely(FC_FCOE_DECAPS_VER(hp) != FC_FCOE_VER)) {
586 if (stats) {
587 if (stats->ErrorFrames < 5)
588 FC_DBG("unknown FCoE version %x",
589 FC_FCOE_DECAPS_VER(hp));
590 stats->ErrorFrames++;
591 }
592 kfree_skb(skb);
593 continue;
594 }
595
596 skb_pull(skb, sizeof(struct fcoe_hdr));
597 fr_len = skb->len - sizeof(struct fcoe_crc_eof);
598
599 if (stats) {
600 stats->RxFrames++;
601 stats->RxWords += fr_len / FCOE_WORD_TO_BYTE;
602 }
603
604 fp = (struct fc_frame *)skb;
605 fc_frame_init(fp);
606 fr_dev(fp) = lp;
607 fr_sof(fp) = hp->fcoe_sof;
608
609 /* Copy out the CRC and EOF trailer for access */
610 if (skb_copy_bits(skb, fr_len, &crc_eof, sizeof(crc_eof))) {
611 kfree_skb(skb);
612 continue;
613 }
614 fr_eof(fp) = crc_eof.fcoe_eof;
615 fr_crc(fp) = crc_eof.fcoe_crc32;
616 if (pskb_trim(skb, fr_len)) {
617 kfree_skb(skb);
618 continue;
619 }
620
621 /*
622 * We only check CRC if no offload is available and if it is
623 * it's solicited data, in which case, the FCP layer would
624 * check it during the copy.
625 */
626 if (lp->crc_offload)
627 fr_flags(fp) &= ~FCPHF_CRC_UNCHECKED;
628 else
629 fr_flags(fp) |= FCPHF_CRC_UNCHECKED;
630
631 fh = fc_frame_header_get(fp);
632 if (fh->fh_r_ctl == FC_RCTL_DD_SOL_DATA &&
633 fh->fh_type == FC_TYPE_FCP) {
634 fc_exch_recv(lp, lp->emp, fp);
635 continue;
636 }
637 if (fr_flags(fp) & FCPHF_CRC_UNCHECKED) {
638 if (le32_to_cpu(fr_crc(fp)) !=
639 ~crc32(~0, skb->data, fr_len)) {
640 if (debug_fcoe || stats->InvalidCRCCount < 5)
641 printk(KERN_WARNING "fcoe: dropping "
642 "frame with CRC error\n");
643 stats->InvalidCRCCount++;
644 stats->ErrorFrames++;
645 fc_frame_free(fp);
646 continue;
647 }
648 fr_flags(fp) &= ~FCPHF_CRC_UNCHECKED;
649 }
650 /* non flogi and non data exchanges are handled here */
651 if (unlikely(fc->flogi_progress))
652 fcoe_recv_flogi(fc, fp, mac);
653 fc_exch_recv(lp, lp->emp, fp);
654 }
655 return 0;
656}
657
658/**
Robert Love34f42a02009-02-27 10:55:45 -0800659 * fcoe_recv_flogi() - flogi receive function
Robert Love85b4aa42008-12-09 15:10:24 -0800660 * @fc: associated fcoe_softc
661 * @fp: the recieved frame
662 * @sa: the source address of this flogi
663 *
664 * This is responsible to parse the flogi response and sets the corresponding
665 * mac address for the initiator, eitehr OUI based or GW based.
666 *
667 * Returns: none
Robert Love34f42a02009-02-27 10:55:45 -0800668 */
Robert Love85b4aa42008-12-09 15:10:24 -0800669static void fcoe_recv_flogi(struct fcoe_softc *fc, struct fc_frame *fp, u8 *sa)
670{
671 struct fc_frame_header *fh;
672 u8 op;
673
674 fh = fc_frame_header_get(fp);
675 if (fh->fh_type != FC_TYPE_ELS)
676 return;
677 op = fc_frame_payload_op(fp);
678 if (op == ELS_LS_ACC && fh->fh_r_ctl == FC_RCTL_ELS_REP &&
679 fc->flogi_oxid == ntohs(fh->fh_ox_id)) {
680 /*
681 * FLOGI accepted.
682 * If the src mac addr is FC_OUI-based, then we mark the
683 * address_mode flag to use FC_OUI-based Ethernet DA.
684 * Otherwise we use the FCoE gateway addr
685 */
686 if (!compare_ether_addr(sa, (u8[6]) FC_FCOE_FLOGI_MAC)) {
687 fc->address_mode = FCOE_FCOUI_ADDR_MODE;
688 } else {
689 memcpy(fc->dest_addr, sa, ETH_ALEN);
690 fc->address_mode = FCOE_GW_ADDR_MODE;
691 }
692
693 /*
694 * Remove any previously-set unicast MAC filter.
695 * Add secondary FCoE MAC address filter for our OUI.
696 */
697 rtnl_lock();
698 if (compare_ether_addr(fc->data_src_addr, (u8[6]) { 0 }))
699 dev_unicast_delete(fc->real_dev, fc->data_src_addr,
700 ETH_ALEN);
701 fc_fcoe_set_mac(fc->data_src_addr, fh->fh_d_id);
702 dev_unicast_add(fc->real_dev, fc->data_src_addr, ETH_ALEN);
703 rtnl_unlock();
704
705 fc->flogi_progress = 0;
706 } else if (op == ELS_FLOGI && fh->fh_r_ctl == FC_RCTL_ELS_REQ && sa) {
707 /*
708 * Save source MAC for point-to-point responses.
709 */
710 memcpy(fc->dest_addr, sa, ETH_ALEN);
711 fc->address_mode = FCOE_GW_ADDR_MODE;
712 }
713}
714
715/**
Robert Love34f42a02009-02-27 10:55:45 -0800716 * fcoe_watchdog() - fcoe timer callback
Robert Love85b4aa42008-12-09 15:10:24 -0800717 * @vp:
718 *
Vasu Devbc0e17f2009-02-27 10:54:57 -0800719 * This checks the pending queue length for fcoe and set lport qfull
Robert Love85b4aa42008-12-09 15:10:24 -0800720 * if the FCOE_MAX_QUEUE_DEPTH is reached. This is done for all fc_lport on the
721 * fcoe_hostlist.
722 *
723 * Returns: 0 for success
Robert Love34f42a02009-02-27 10:55:45 -0800724 */
Robert Love85b4aa42008-12-09 15:10:24 -0800725void fcoe_watchdog(ulong vp)
726{
Robert Love85b4aa42008-12-09 15:10:24 -0800727 struct fcoe_softc *fc;
Robert Love85b4aa42008-12-09 15:10:24 -0800728
729 read_lock(&fcoe_hostlist_lock);
730 list_for_each_entry(fc, &fcoe_hostlist, list) {
Vasu Devc826a312009-02-27 10:56:27 -0800731 if (fc->lp)
732 fcoe_check_wait_queue(fc->lp);
Robert Love85b4aa42008-12-09 15:10:24 -0800733 }
734 read_unlock(&fcoe_hostlist_lock);
735
736 fcoe_timer.expires = jiffies + (1 * HZ);
737 add_timer(&fcoe_timer);
738}
739
740
741/**
Robert Love34f42a02009-02-27 10:55:45 -0800742 * fcoe_check_wait_queue() - put the skb into fcoe pending xmit queue
Robert Love85b4aa42008-12-09 15:10:24 -0800743 * @lp: the fc_port for this skb
744 * @skb: the associated skb to be xmitted
745 *
746 * This empties the wait_queue, dequeue the head of the wait_queue queue
747 * and calls fcoe_start_io() for each packet, if all skb have been
Vasu Devc826a312009-02-27 10:56:27 -0800748 * transmitted, return qlen or -1 if a error occurs, then restore
749 * wait_queue and try again later.
Robert Love85b4aa42008-12-09 15:10:24 -0800750 *
751 * The wait_queue is used when the skb transmit fails. skb will go
752 * in the wait_queue which will be emptied by the time function OR
753 * by the next skb transmit.
754 *
755 * Returns: 0 for success
Robert Love34f42a02009-02-27 10:55:45 -0800756 */
Robert Love85b4aa42008-12-09 15:10:24 -0800757static int fcoe_check_wait_queue(struct fc_lport *lp)
758{
Robert Love85b4aa42008-12-09 15:10:24 -0800759 struct sk_buff *skb;
760 struct fcoe_softc *fc;
Vasu Devc826a312009-02-27 10:56:27 -0800761 int rc = -1;
Robert Love85b4aa42008-12-09 15:10:24 -0800762
Robert Lovefc47ff62009-02-27 10:55:55 -0800763 fc = lport_priv(lp);
Robert Love85b4aa42008-12-09 15:10:24 -0800764 spin_lock_bh(&fc->fcoe_pending_queue.lock);
765
Vasu Devc826a312009-02-27 10:56:27 -0800766 if (fc->fcoe_pending_queue_active)
767 goto out;
768 fc->fcoe_pending_queue_active = 1;
Robert Love85b4aa42008-12-09 15:10:24 -0800769 if (fc->fcoe_pending_queue.qlen) {
770 while ((skb = __skb_dequeue(&fc->fcoe_pending_queue)) != NULL) {
771 spin_unlock_bh(&fc->fcoe_pending_queue.lock);
772 rc = fcoe_start_io(skb);
Vasu Devc826a312009-02-27 10:56:27 -0800773 if (rc)
Robert Love85b4aa42008-12-09 15:10:24 -0800774 fcoe_insert_wait_queue_head(lp, skb);
Robert Love85b4aa42008-12-09 15:10:24 -0800775 spin_lock_bh(&fc->fcoe_pending_queue.lock);
Vasu Devc826a312009-02-27 10:56:27 -0800776 if (rc)
777 break;
Robert Love85b4aa42008-12-09 15:10:24 -0800778 }
Vasu Devc826a312009-02-27 10:56:27 -0800779 /*
780 * if interface pending queue is below FCOE_LOW_QUEUE_DEPTH
781 * then clear qfull flag.
782 */
783 if (fc->fcoe_pending_queue.qlen < FCOE_LOW_QUEUE_DEPTH)
Vasu Devbc0e17f2009-02-27 10:54:57 -0800784 lp->qfull = 0;
Robert Love85b4aa42008-12-09 15:10:24 -0800785 }
Vasu Devc826a312009-02-27 10:56:27 -0800786 fc->fcoe_pending_queue_active = 0;
787 rc = fc->fcoe_pending_queue.qlen;
788out:
Robert Love85b4aa42008-12-09 15:10:24 -0800789 spin_unlock_bh(&fc->fcoe_pending_queue.lock);
Vasu Devc826a312009-02-27 10:56:27 -0800790 return rc;
Robert Love85b4aa42008-12-09 15:10:24 -0800791}
792
793/**
Robert Love34f42a02009-02-27 10:55:45 -0800794 * fcoe_insert_wait_queue_head() - puts skb to fcoe pending queue head
Robert Love85b4aa42008-12-09 15:10:24 -0800795 * @lp: the fc_port for this skb
796 * @skb: the associated skb to be xmitted
797 *
798 * Returns: none
Robert Love34f42a02009-02-27 10:55:45 -0800799 */
Robert Love85b4aa42008-12-09 15:10:24 -0800800static void fcoe_insert_wait_queue_head(struct fc_lport *lp,
801 struct sk_buff *skb)
802{
803 struct fcoe_softc *fc;
804
Robert Lovefc47ff62009-02-27 10:55:55 -0800805 fc = lport_priv(lp);
Robert Love85b4aa42008-12-09 15:10:24 -0800806 spin_lock_bh(&fc->fcoe_pending_queue.lock);
807 __skb_queue_head(&fc->fcoe_pending_queue, skb);
808 spin_unlock_bh(&fc->fcoe_pending_queue.lock);
809}
810
811/**
Robert Love34f42a02009-02-27 10:55:45 -0800812 * fcoe_insert_wait_queue() - put the skb into fcoe pending queue tail
Robert Love85b4aa42008-12-09 15:10:24 -0800813 * @lp: the fc_port for this skb
814 * @skb: the associated skb to be xmitted
815 *
816 * Returns: none
Robert Love34f42a02009-02-27 10:55:45 -0800817 */
Robert Love85b4aa42008-12-09 15:10:24 -0800818static void fcoe_insert_wait_queue(struct fc_lport *lp,
819 struct sk_buff *skb)
820{
821 struct fcoe_softc *fc;
822
Robert Lovefc47ff62009-02-27 10:55:55 -0800823 fc = lport_priv(lp);
Robert Love85b4aa42008-12-09 15:10:24 -0800824 spin_lock_bh(&fc->fcoe_pending_queue.lock);
825 __skb_queue_tail(&fc->fcoe_pending_queue, skb);
826 spin_unlock_bh(&fc->fcoe_pending_queue.lock);
827}
828
829/**
Robert Love34f42a02009-02-27 10:55:45 -0800830 * fcoe_dev_setup() - setup link change notification interface
831 */
832static void fcoe_dev_setup()
Robert Love85b4aa42008-12-09 15:10:24 -0800833{
834 /*
835 * here setup a interface specific wd time to
836 * monitor the link state
837 */
838 register_netdevice_notifier(&fcoe_notifier);
839}
840
841/**
Robert Love34f42a02009-02-27 10:55:45 -0800842 * fcoe_dev_setup() - cleanup link change notification interface
843 */
Robert Love85b4aa42008-12-09 15:10:24 -0800844static void fcoe_dev_cleanup(void)
845{
846 unregister_netdevice_notifier(&fcoe_notifier);
847}
848
849/**
Robert Love34f42a02009-02-27 10:55:45 -0800850 * fcoe_device_notification() - netdev event notification callback
Robert Love85b4aa42008-12-09 15:10:24 -0800851 * @notifier: context of the notification
852 * @event: type of event
853 * @ptr: fixed array for output parsed ifname
854 *
855 * This function is called by the ethernet driver in case of link change event
856 *
857 * Returns: 0 for success
Robert Love34f42a02009-02-27 10:55:45 -0800858 */
Robert Love85b4aa42008-12-09 15:10:24 -0800859static int fcoe_device_notification(struct notifier_block *notifier,
860 ulong event, void *ptr)
861{
862 struct fc_lport *lp = NULL;
863 struct net_device *real_dev = ptr;
864 struct fcoe_softc *fc;
865 struct fcoe_dev_stats *stats;
Vasu Devbc0e17f2009-02-27 10:54:57 -0800866 u32 new_link_up;
Robert Love85b4aa42008-12-09 15:10:24 -0800867 u32 mfs;
868 int rc = NOTIFY_OK;
869
870 read_lock(&fcoe_hostlist_lock);
871 list_for_each_entry(fc, &fcoe_hostlist, list) {
872 if (fc->real_dev == real_dev) {
873 lp = fc->lp;
874 break;
875 }
876 }
877 read_unlock(&fcoe_hostlist_lock);
878 if (lp == NULL) {
879 rc = NOTIFY_DONE;
880 goto out;
881 }
882
Vasu Devbc0e17f2009-02-27 10:54:57 -0800883 new_link_up = lp->link_up;
Robert Love85b4aa42008-12-09 15:10:24 -0800884 switch (event) {
885 case NETDEV_DOWN:
886 case NETDEV_GOING_DOWN:
Vasu Devbc0e17f2009-02-27 10:54:57 -0800887 new_link_up = 0;
Robert Love85b4aa42008-12-09 15:10:24 -0800888 break;
889 case NETDEV_UP:
890 case NETDEV_CHANGE:
Vasu Devbc0e17f2009-02-27 10:54:57 -0800891 new_link_up = !fcoe_link_ok(lp);
Robert Love85b4aa42008-12-09 15:10:24 -0800892 break;
893 case NETDEV_CHANGEMTU:
894 mfs = fc->real_dev->mtu -
895 (sizeof(struct fcoe_hdr) +
896 sizeof(struct fcoe_crc_eof));
897 if (mfs >= FC_MIN_MAX_FRAME)
898 fc_set_mfs(lp, mfs);
Vasu Devbc0e17f2009-02-27 10:54:57 -0800899 new_link_up = !fcoe_link_ok(lp);
Robert Love85b4aa42008-12-09 15:10:24 -0800900 break;
901 case NETDEV_REGISTER:
902 break;
903 default:
904 FC_DBG("unknown event %ld call", event);
905 }
Vasu Devbc0e17f2009-02-27 10:54:57 -0800906 if (lp->link_up != new_link_up) {
907 if (new_link_up)
Robert Love85b4aa42008-12-09 15:10:24 -0800908 fc_linkup(lp);
909 else {
910 stats = lp->dev_stats[smp_processor_id()];
911 if (stats)
912 stats->LinkFailureCount++;
913 fc_linkdown(lp);
914 fcoe_clean_pending_queue(lp);
915 }
916 }
917out:
918 return rc;
919}
920
921/**
Robert Love34f42a02009-02-27 10:55:45 -0800922 * fcoe_if_to_netdev() - parse a name buffer to get netdev
Robert Love85b4aa42008-12-09 15:10:24 -0800923 * @ifname: fixed array for output parsed ifname
924 * @buffer: incoming buffer to be copied
925 *
926 * Returns: NULL or ptr to netdeive
Robert Love34f42a02009-02-27 10:55:45 -0800927 */
Robert Love85b4aa42008-12-09 15:10:24 -0800928static struct net_device *fcoe_if_to_netdev(const char *buffer)
929{
930 char *cp;
931 char ifname[IFNAMSIZ + 2];
932
933 if (buffer) {
934 strlcpy(ifname, buffer, IFNAMSIZ);
935 cp = ifname + strlen(ifname);
936 while (--cp >= ifname && *cp == '\n')
937 *cp = '\0';
938 return dev_get_by_name(&init_net, ifname);
939 }
940 return NULL;
941}
942
943/**
Robert Love34f42a02009-02-27 10:55:45 -0800944 * fcoe_netdev_to_module_owner() - finds out the nic drive moddule of the netdev
Robert Love85b4aa42008-12-09 15:10:24 -0800945 * @netdev: the target netdev
946 *
947 * Returns: ptr to the struct module, NULL for failure
Robert Love34f42a02009-02-27 10:55:45 -0800948 */
Robert Loveb2ab99c2009-02-27 10:55:50 -0800949static struct module *
950fcoe_netdev_to_module_owner(const struct net_device *netdev)
Robert Love85b4aa42008-12-09 15:10:24 -0800951{
952 struct device *dev;
953
954 if (!netdev)
955 return NULL;
956
957 dev = netdev->dev.parent;
958 if (!dev)
959 return NULL;
960
961 if (!dev->driver)
962 return NULL;
963
964 return dev->driver->owner;
965}
966
967/**
Robert Love34f42a02009-02-27 10:55:45 -0800968 * fcoe_ethdrv_get() - Hold the Ethernet driver
Robert Love85b4aa42008-12-09 15:10:24 -0800969 * @netdev: the target netdev
970 *
Robert Love34f42a02009-02-27 10:55:45 -0800971 * Holds the Ethernet driver module by try_module_get() for
972 * the corresponding netdev.
973 *
Robert Love85b4aa42008-12-09 15:10:24 -0800974 * Returns: 0 for succsss
Robert Love34f42a02009-02-27 10:55:45 -0800975 */
Robert Love85b4aa42008-12-09 15:10:24 -0800976static int fcoe_ethdrv_get(const struct net_device *netdev)
977{
978 struct module *owner;
979
980 owner = fcoe_netdev_to_module_owner(netdev);
981 if (owner) {
James Bottomley56b854b2008-12-29 15:45:41 -0600982 printk(KERN_DEBUG "fcoe:hold driver module %s for %s\n",
983 module_name(owner), netdev->name);
Robert Love85b4aa42008-12-09 15:10:24 -0800984 return try_module_get(owner);
985 }
986 return -ENODEV;
987}
988
989/**
Robert Love34f42a02009-02-27 10:55:45 -0800990 * fcoe_ethdrv_put() - Release the Ethernet driver
Robert Love85b4aa42008-12-09 15:10:24 -0800991 * @netdev: the target netdev
992 *
Robert Love34f42a02009-02-27 10:55:45 -0800993 * Releases the Ethernet driver module by module_put for
994 * the corresponding netdev.
995 *
Robert Love85b4aa42008-12-09 15:10:24 -0800996 * Returns: 0 for succsss
Robert Love34f42a02009-02-27 10:55:45 -0800997 */
Robert Love85b4aa42008-12-09 15:10:24 -0800998static int fcoe_ethdrv_put(const struct net_device *netdev)
999{
1000 struct module *owner;
1001
1002 owner = fcoe_netdev_to_module_owner(netdev);
1003 if (owner) {
James Bottomley56b854b2008-12-29 15:45:41 -06001004 printk(KERN_DEBUG "fcoe:release driver module %s for %s\n",
1005 module_name(owner), netdev->name);
Robert Love85b4aa42008-12-09 15:10:24 -08001006 module_put(owner);
1007 return 0;
1008 }
1009 return -ENODEV;
1010}
1011
1012/**
Robert Love34f42a02009-02-27 10:55:45 -08001013 * fcoe_destroy() - handles the destroy from sysfs
Robert Love85b4aa42008-12-09 15:10:24 -08001014 * @buffer: expcted to be a eth if name
1015 * @kp: associated kernel param
1016 *
1017 * Returns: 0 for success
Robert Love34f42a02009-02-27 10:55:45 -08001018 */
Robert Love85b4aa42008-12-09 15:10:24 -08001019static int fcoe_destroy(const char *buffer, struct kernel_param *kp)
1020{
1021 int rc;
1022 struct net_device *netdev;
1023
1024 netdev = fcoe_if_to_netdev(buffer);
1025 if (!netdev) {
1026 rc = -ENODEV;
1027 goto out_nodev;
1028 }
1029 /* look for existing lport */
1030 if (!fcoe_hostlist_lookup(netdev)) {
1031 rc = -ENODEV;
1032 goto out_putdev;
1033 }
1034 /* pass to transport */
1035 rc = fcoe_transport_release(netdev);
1036 if (rc) {
1037 printk(KERN_ERR "fcoe: fcoe_transport_release(%s) failed\n",
1038 netdev->name);
1039 rc = -EIO;
1040 goto out_putdev;
1041 }
1042 fcoe_ethdrv_put(netdev);
1043 rc = 0;
1044out_putdev:
1045 dev_put(netdev);
1046out_nodev:
1047 return rc;
1048}
1049
1050/**
Robert Love34f42a02009-02-27 10:55:45 -08001051 * fcoe_create() - Handles the create call from sysfs
Robert Love85b4aa42008-12-09 15:10:24 -08001052 * @buffer: expcted to be a eth if name
1053 * @kp: associated kernel param
1054 *
1055 * Returns: 0 for success
Robert Love34f42a02009-02-27 10:55:45 -08001056 */
Robert Love85b4aa42008-12-09 15:10:24 -08001057static int fcoe_create(const char *buffer, struct kernel_param *kp)
1058{
1059 int rc;
1060 struct net_device *netdev;
1061
1062 netdev = fcoe_if_to_netdev(buffer);
1063 if (!netdev) {
1064 rc = -ENODEV;
1065 goto out_nodev;
1066 }
1067 /* look for existing lport */
1068 if (fcoe_hostlist_lookup(netdev)) {
1069 rc = -EEXIST;
1070 goto out_putdev;
1071 }
1072 fcoe_ethdrv_get(netdev);
1073
1074 /* pass to transport */
1075 rc = fcoe_transport_attach(netdev);
1076 if (rc) {
1077 printk(KERN_ERR "fcoe: fcoe_transport_attach(%s) failed\n",
1078 netdev->name);
1079 fcoe_ethdrv_put(netdev);
1080 rc = -EIO;
1081 goto out_putdev;
1082 }
1083 rc = 0;
1084out_putdev:
1085 dev_put(netdev);
1086out_nodev:
1087 return rc;
1088}
1089
1090module_param_call(create, fcoe_create, NULL, NULL, S_IWUSR);
1091__MODULE_PARM_TYPE(create, "string");
1092MODULE_PARM_DESC(create, "Create fcoe port using net device passed in.");
1093module_param_call(destroy, fcoe_destroy, NULL, NULL, S_IWUSR);
1094__MODULE_PARM_TYPE(destroy, "string");
1095MODULE_PARM_DESC(destroy, "Destroy fcoe port");
1096
Robert Love34f42a02009-02-27 10:55:45 -08001097/**
1098 * fcoe_link_ok() - Check if link is ok for the fc_lport
Robert Love85b4aa42008-12-09 15:10:24 -08001099 * @lp: ptr to the fc_lport
1100 *
1101 * Any permanently-disqualifying conditions have been previously checked.
1102 * This also updates the speed setting, which may change with link for 100/1000.
1103 *
1104 * This function should probably be checking for PAUSE support at some point
1105 * in the future. Currently Per-priority-pause is not determinable using
1106 * ethtool, so we shouldn't be restrictive until that problem is resolved.
1107 *
1108 * Returns: 0 if link is OK for use by FCoE.
1109 *
1110 */
1111int fcoe_link_ok(struct fc_lport *lp)
1112{
Robert Lovefc47ff62009-02-27 10:55:55 -08001113 struct fcoe_softc *fc = lport_priv(lp);
Robert Love85b4aa42008-12-09 15:10:24 -08001114 struct net_device *dev = fc->real_dev;
1115 struct ethtool_cmd ecmd = { ETHTOOL_GSET };
1116 int rc = 0;
1117
1118 if ((dev->flags & IFF_UP) && netif_carrier_ok(dev)) {
1119 dev = fc->phys_dev;
1120 if (dev->ethtool_ops->get_settings) {
1121 dev->ethtool_ops->get_settings(dev, &ecmd);
1122 lp->link_supported_speeds &=
1123 ~(FC_PORTSPEED_1GBIT | FC_PORTSPEED_10GBIT);
1124 if (ecmd.supported & (SUPPORTED_1000baseT_Half |
1125 SUPPORTED_1000baseT_Full))
1126 lp->link_supported_speeds |= FC_PORTSPEED_1GBIT;
1127 if (ecmd.supported & SUPPORTED_10000baseT_Full)
1128 lp->link_supported_speeds |=
1129 FC_PORTSPEED_10GBIT;
1130 if (ecmd.speed == SPEED_1000)
1131 lp->link_speed = FC_PORTSPEED_1GBIT;
1132 if (ecmd.speed == SPEED_10000)
1133 lp->link_speed = FC_PORTSPEED_10GBIT;
1134 }
1135 } else
1136 rc = -1;
1137
1138 return rc;
1139}
1140EXPORT_SYMBOL_GPL(fcoe_link_ok);
1141
Robert Love34f42a02009-02-27 10:55:45 -08001142/**
1143 * fcoe_percpu_clean() - Clear the pending skbs for an lport
Robert Love85b4aa42008-12-09 15:10:24 -08001144 * @lp: the fc_lport
1145 */
1146void fcoe_percpu_clean(struct fc_lport *lp)
1147{
1148 int idx;
1149 struct fcoe_percpu_s *pp;
1150 struct fcoe_rcv_info *fr;
1151 struct sk_buff_head *list;
1152 struct sk_buff *skb, *next;
1153 struct sk_buff *head;
1154
1155 for (idx = 0; idx < NR_CPUS; idx++) {
1156 if (fcoe_percpu[idx]) {
1157 pp = fcoe_percpu[idx];
1158 spin_lock_bh(&pp->fcoe_rx_list.lock);
1159 list = &pp->fcoe_rx_list;
1160 head = list->next;
1161 for (skb = head; skb != (struct sk_buff *)list;
1162 skb = next) {
1163 next = skb->next;
1164 fr = fcoe_dev_from_skb(skb);
1165 if (fr->fr_dev == lp) {
1166 __skb_unlink(skb, list);
1167 kfree_skb(skb);
1168 }
1169 }
1170 spin_unlock_bh(&pp->fcoe_rx_list.lock);
1171 }
1172 }
1173}
1174EXPORT_SYMBOL_GPL(fcoe_percpu_clean);
1175
1176/**
Robert Love34f42a02009-02-27 10:55:45 -08001177 * fcoe_clean_pending_queue() - Dequeue a skb and free it
Robert Love85b4aa42008-12-09 15:10:24 -08001178 * @lp: the corresponding fc_lport
1179 *
1180 * Returns: none
Robert Love34f42a02009-02-27 10:55:45 -08001181 */
Robert Love85b4aa42008-12-09 15:10:24 -08001182void fcoe_clean_pending_queue(struct fc_lport *lp)
1183{
1184 struct fcoe_softc *fc = lport_priv(lp);
1185 struct sk_buff *skb;
1186
1187 spin_lock_bh(&fc->fcoe_pending_queue.lock);
1188 while ((skb = __skb_dequeue(&fc->fcoe_pending_queue)) != NULL) {
1189 spin_unlock_bh(&fc->fcoe_pending_queue.lock);
1190 kfree_skb(skb);
1191 spin_lock_bh(&fc->fcoe_pending_queue.lock);
1192 }
1193 spin_unlock_bh(&fc->fcoe_pending_queue.lock);
1194}
1195EXPORT_SYMBOL_GPL(fcoe_clean_pending_queue);
1196
1197/**
Robert Love34f42a02009-02-27 10:55:45 -08001198 * libfc_host_alloc() - Allocate a Scsi_Host with room for the fc_lport
Robert Love85b4aa42008-12-09 15:10:24 -08001199 * @sht: ptr to the scsi host templ
1200 * @priv_size: size of private data after fc_lport
1201 *
1202 * Returns: ptr to Scsi_Host
Robert Love34f42a02009-02-27 10:55:45 -08001203 * TODO: to libfc?
Robert Love85b4aa42008-12-09 15:10:24 -08001204 */
Robert Loveb2ab99c2009-02-27 10:55:50 -08001205static inline struct Scsi_Host *
1206libfc_host_alloc(struct scsi_host_template *sht, int priv_size)
Robert Love85b4aa42008-12-09 15:10:24 -08001207{
1208 return scsi_host_alloc(sht, sizeof(struct fc_lport) + priv_size);
1209}
1210
1211/**
Robert Love34f42a02009-02-27 10:55:45 -08001212 * fcoe_host_alloc() - Allocate a Scsi_Host with room for the fcoe_softc
Robert Love85b4aa42008-12-09 15:10:24 -08001213 * @sht: ptr to the scsi host templ
1214 * @priv_size: size of private data after fc_lport
1215 *
1216 * Returns: ptr to Scsi_Host
1217 */
1218struct Scsi_Host *fcoe_host_alloc(struct scsi_host_template *sht, int priv_size)
1219{
1220 return libfc_host_alloc(sht, sizeof(struct fcoe_softc) + priv_size);
1221}
1222EXPORT_SYMBOL_GPL(fcoe_host_alloc);
1223
Robert Love34f42a02009-02-27 10:55:45 -08001224/**
1225 * fcoe_reset() - Resets the fcoe
Robert Love85b4aa42008-12-09 15:10:24 -08001226 * @shost: shost the reset is from
1227 *
1228 * Returns: always 0
1229 */
1230int fcoe_reset(struct Scsi_Host *shost)
1231{
1232 struct fc_lport *lport = shost_priv(shost);
1233 fc_lport_reset(lport);
1234 return 0;
1235}
1236EXPORT_SYMBOL_GPL(fcoe_reset);
1237
Robert Love34f42a02009-02-27 10:55:45 -08001238/**
1239 * fcoe_wwn_from_mac() - Converts 48-bit IEEE MAC address to 64-bit FC WWN.
Robert Love85b4aa42008-12-09 15:10:24 -08001240 * @mac: mac address
1241 * @scheme: check port
1242 * @port: port indicator for converting
1243 *
1244 * Returns: u64 fc world wide name
1245 */
1246u64 fcoe_wwn_from_mac(unsigned char mac[MAX_ADDR_LEN],
1247 unsigned int scheme, unsigned int port)
1248{
1249 u64 wwn;
1250 u64 host_mac;
1251
1252 /* The MAC is in NO, so flip only the low 48 bits */
1253 host_mac = ((u64) mac[0] << 40) |
1254 ((u64) mac[1] << 32) |
1255 ((u64) mac[2] << 24) |
1256 ((u64) mac[3] << 16) |
1257 ((u64) mac[4] << 8) |
1258 (u64) mac[5];
1259
1260 WARN_ON(host_mac >= (1ULL << 48));
1261 wwn = host_mac | ((u64) scheme << 60);
1262 switch (scheme) {
1263 case 1:
1264 WARN_ON(port != 0);
1265 break;
1266 case 2:
1267 WARN_ON(port >= 0xfff);
1268 wwn |= (u64) port << 48;
1269 break;
1270 default:
1271 WARN_ON(1);
1272 break;
1273 }
1274
1275 return wwn;
1276}
1277EXPORT_SYMBOL_GPL(fcoe_wwn_from_mac);
Robert Love34f42a02009-02-27 10:55:45 -08001278
1279/**
1280 * fcoe_hostlist_lookup_softc() - find the corresponding lport by a given device
Robert Love85b4aa42008-12-09 15:10:24 -08001281 * @device: this is currently ptr to net_device
1282 *
1283 * Returns: NULL or the located fcoe_softc
1284 */
Robert Loveb2ab99c2009-02-27 10:55:50 -08001285static struct fcoe_softc *
1286fcoe_hostlist_lookup_softc(const struct net_device *dev)
Robert Love85b4aa42008-12-09 15:10:24 -08001287{
1288 struct fcoe_softc *fc;
1289
1290 read_lock(&fcoe_hostlist_lock);
1291 list_for_each_entry(fc, &fcoe_hostlist, list) {
1292 if (fc->real_dev == dev) {
1293 read_unlock(&fcoe_hostlist_lock);
1294 return fc;
1295 }
1296 }
1297 read_unlock(&fcoe_hostlist_lock);
1298 return NULL;
1299}
1300
Robert Love34f42a02009-02-27 10:55:45 -08001301/**
1302 * fcoe_hostlist_lookup() - Find the corresponding lport by netdev
Robert Love85b4aa42008-12-09 15:10:24 -08001303 * @netdev: ptr to net_device
1304 *
1305 * Returns: 0 for success
1306 */
1307struct fc_lport *fcoe_hostlist_lookup(const struct net_device *netdev)
1308{
1309 struct fcoe_softc *fc;
1310
1311 fc = fcoe_hostlist_lookup_softc(netdev);
1312
1313 return (fc) ? fc->lp : NULL;
1314}
1315EXPORT_SYMBOL_GPL(fcoe_hostlist_lookup);
1316
Robert Love34f42a02009-02-27 10:55:45 -08001317/**
1318 * fcoe_hostlist_add() - Add a lport to lports list
Robert Love85b4aa42008-12-09 15:10:24 -08001319 * @lp: ptr to the fc_lport to badded
1320 *
1321 * Returns: 0 for success
1322 */
1323int fcoe_hostlist_add(const struct fc_lport *lp)
1324{
1325 struct fcoe_softc *fc;
1326
1327 fc = fcoe_hostlist_lookup_softc(fcoe_netdev(lp));
1328 if (!fc) {
Robert Lovefc47ff62009-02-27 10:55:55 -08001329 fc = lport_priv(lp);
Robert Love85b4aa42008-12-09 15:10:24 -08001330 write_lock_bh(&fcoe_hostlist_lock);
1331 list_add_tail(&fc->list, &fcoe_hostlist);
1332 write_unlock_bh(&fcoe_hostlist_lock);
1333 }
1334 return 0;
1335}
1336EXPORT_SYMBOL_GPL(fcoe_hostlist_add);
1337
Robert Love34f42a02009-02-27 10:55:45 -08001338/**
1339 * fcoe_hostlist_remove() - remove a lport from lports list
Robert Love85b4aa42008-12-09 15:10:24 -08001340 * @lp: ptr to the fc_lport to badded
1341 *
1342 * Returns: 0 for success
1343 */
1344int fcoe_hostlist_remove(const struct fc_lport *lp)
1345{
1346 struct fcoe_softc *fc;
1347
1348 fc = fcoe_hostlist_lookup_softc(fcoe_netdev(lp));
1349 BUG_ON(!fc);
1350 write_lock_bh(&fcoe_hostlist_lock);
1351 list_del(&fc->list);
1352 write_unlock_bh(&fcoe_hostlist_lock);
1353
1354 return 0;
1355}
1356EXPORT_SYMBOL_GPL(fcoe_hostlist_remove);
1357
1358/**
Robert Love34f42a02009-02-27 10:55:45 -08001359 * fcoe_libfc_config() - sets up libfc related properties for lport
Robert Love85b4aa42008-12-09 15:10:24 -08001360 * @lp: ptr to the fc_lport
1361 * @tt: libfc function template
1362 *
1363 * Returns : 0 for success
Robert Love34f42a02009-02-27 10:55:45 -08001364 */
Robert Love85b4aa42008-12-09 15:10:24 -08001365int fcoe_libfc_config(struct fc_lport *lp, struct libfc_function_template *tt)
1366{
1367 /* Set the function pointers set by the LLDD */
1368 memcpy(&lp->tt, tt, sizeof(*tt));
1369 if (fc_fcp_init(lp))
1370 return -ENOMEM;
1371 fc_exch_init(lp);
1372 fc_elsct_init(lp);
1373 fc_lport_init(lp);
1374 fc_rport_init(lp);
1375 fc_disc_init(lp);
1376
1377 return 0;
1378}
1379EXPORT_SYMBOL_GPL(fcoe_libfc_config);
1380
1381/**
Robert Love34f42a02009-02-27 10:55:45 -08001382 * fcoe_init() - fcoe module loading initialization
Robert Love85b4aa42008-12-09 15:10:24 -08001383 *
1384 * Initialization routine
1385 * 1. Will create fc transport software structure
1386 * 2. initialize the link list of port information structure
1387 *
1388 * Returns 0 on success, negative on failure
Robert Love34f42a02009-02-27 10:55:45 -08001389 */
Robert Love85b4aa42008-12-09 15:10:24 -08001390static int __init fcoe_init(void)
1391{
1392 int cpu;
1393 struct fcoe_percpu_s *p;
1394
1395
1396 INIT_LIST_HEAD(&fcoe_hostlist);
1397 rwlock_init(&fcoe_hostlist_lock);
1398
1399#ifdef CONFIG_HOTPLUG_CPU
1400 register_cpu_notifier(&fcoe_cpu_notifier);
1401#endif /* CONFIG_HOTPLUG_CPU */
1402
1403 /*
1404 * initialize per CPU interrupt thread
1405 */
1406 for_each_online_cpu(cpu) {
1407 p = kzalloc(sizeof(struct fcoe_percpu_s), GFP_KERNEL);
1408 if (p) {
1409 p->thread = kthread_create(fcoe_percpu_receive_thread,
1410 (void *)p,
1411 "fcoethread/%d", cpu);
1412
1413 /*
1414 * if there is no error then bind the thread to the cpu
1415 * initialize the semaphore and skb queue head
1416 */
1417 if (likely(!IS_ERR(p->thread))) {
1418 p->cpu = cpu;
1419 fcoe_percpu[cpu] = p;
1420 skb_queue_head_init(&p->fcoe_rx_list);
1421 kthread_bind(p->thread, cpu);
1422 wake_up_process(p->thread);
1423 } else {
1424 fcoe_percpu[cpu] = NULL;
1425 kfree(p);
Robert Love85b4aa42008-12-09 15:10:24 -08001426 }
1427 }
1428 }
1429
1430 /*
1431 * setup link change notification
1432 */
1433 fcoe_dev_setup();
1434
Robert Lovea468f322009-02-27 10:56:00 -08001435 setup_timer(&fcoe_timer, fcoe_watchdog, 0);
1436
1437 mod_timer(&fcoe_timer, jiffies + (10 * HZ));
Robert Love85b4aa42008-12-09 15:10:24 -08001438
1439 /* initiatlize the fcoe transport */
1440 fcoe_transport_init();
1441
1442 fcoe_sw_init();
1443
1444 return 0;
1445}
1446module_init(fcoe_init);
1447
1448/**
Robert Love34f42a02009-02-27 10:55:45 -08001449 * fcoe_exit() - fcoe module unloading cleanup
Robert Love85b4aa42008-12-09 15:10:24 -08001450 *
1451 * Returns 0 on success, negative on failure
Robert Love34f42a02009-02-27 10:55:45 -08001452 */
Robert Love85b4aa42008-12-09 15:10:24 -08001453static void __exit fcoe_exit(void)
1454{
1455 u32 idx;
1456 struct fcoe_softc *fc, *tmp;
1457 struct fcoe_percpu_s *p;
1458 struct sk_buff *skb;
1459
1460 /*
1461 * Stop all call back interfaces
1462 */
1463#ifdef CONFIG_HOTPLUG_CPU
1464 unregister_cpu_notifier(&fcoe_cpu_notifier);
1465#endif /* CONFIG_HOTPLUG_CPU */
1466 fcoe_dev_cleanup();
1467
1468 /*
1469 * stop timer
1470 */
1471 del_timer_sync(&fcoe_timer);
1472
Robert Loveb2ab99c2009-02-27 10:55:50 -08001473 /* releases the associated fcoe transport for each lport */
Robert Love85b4aa42008-12-09 15:10:24 -08001474 list_for_each_entry_safe(fc, tmp, &fcoe_hostlist, list)
1475 fcoe_transport_release(fc->real_dev);
1476
1477 for (idx = 0; idx < NR_CPUS; idx++) {
1478 if (fcoe_percpu[idx]) {
1479 kthread_stop(fcoe_percpu[idx]->thread);
1480 p = fcoe_percpu[idx];
1481 spin_lock_bh(&p->fcoe_rx_list.lock);
1482 while ((skb = __skb_dequeue(&p->fcoe_rx_list)) != NULL)
1483 kfree_skb(skb);
1484 spin_unlock_bh(&p->fcoe_rx_list.lock);
1485 if (fcoe_percpu[idx]->crc_eof_page)
1486 put_page(fcoe_percpu[idx]->crc_eof_page);
1487 kfree(fcoe_percpu[idx]);
1488 }
1489 }
1490
1491 /* remove sw trasnport */
1492 fcoe_sw_exit();
1493
1494 /* detach the transport */
1495 fcoe_transport_exit();
1496}
1497module_exit(fcoe_exit);