blob: 648a2fc042710605fbe6d5611931fc859bf60e16 [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);
Robert Love5e5e92d2009-03-17 11:41:35 -070068DEFINE_PER_CPU(struct fcoe_percpu_s, fcoe_percpu);
Robert Love85b4aa42008-12-09 15:10:24 -080069
70
71/* Function Prototyes */
72static int fcoe_check_wait_queue(struct fc_lport *);
Robert Love85b4aa42008-12-09 15:10:24 -080073static void fcoe_recv_flogi(struct fcoe_softc *, struct fc_frame *, u8 *);
Robert Love85b4aa42008-12-09 15:10:24 -080074static int fcoe_device_notification(struct notifier_block *, ulong, void *);
75static void fcoe_dev_setup(void);
76static void fcoe_dev_cleanup(void);
77
78/* notification function from net device */
79static struct notifier_block fcoe_notifier = {
80 .notifier_call = fcoe_device_notification,
81};
82
Robert Love85b4aa42008-12-09 15:10:24 -080083/**
Robert Love34f42a02009-02-27 10:55:45 -080084 * fcoe_rcv() - this is the fcoe receive function called by NET_RX_SOFTIRQ
Robert Love85b4aa42008-12-09 15:10:24 -080085 * @skb: the receive skb
86 * @dev: associated net device
87 * @ptype: context
88 * @odldev: last device
89 *
90 * this function will receive the packet and build fc frame and pass it up
91 *
92 * Returns: 0 for success
Robert Love34f42a02009-02-27 10:55:45 -080093 */
Robert Love85b4aa42008-12-09 15:10:24 -080094int fcoe_rcv(struct sk_buff *skb, struct net_device *dev,
95 struct packet_type *ptype, struct net_device *olddev)
96{
97 struct fc_lport *lp;
98 struct fcoe_rcv_info *fr;
99 struct fcoe_softc *fc;
Robert Love85b4aa42008-12-09 15:10:24 -0800100 struct fc_frame_header *fh;
Robert Love85b4aa42008-12-09 15:10:24 -0800101 struct fcoe_percpu_s *fps;
Robert Love38eccab2009-03-17 11:41:30 -0700102 unsigned short oxid;
103 unsigned int cpu_idx;
Robert Love85b4aa42008-12-09 15:10:24 -0800104
105 fc = container_of(ptype, struct fcoe_softc, fcoe_packet_type);
106 lp = fc->lp;
107 if (unlikely(lp == NULL)) {
108 FC_DBG("cannot find hba structure");
109 goto err2;
110 }
111
112 if (unlikely(debug_fcoe)) {
113 FC_DBG("skb_info: len:%d data_len:%d head:%p data:%p tail:%p "
114 "end:%p sum:%d dev:%s", skb->len, skb->data_len,
115 skb->head, skb->data, skb_tail_pointer(skb),
116 skb_end_pointer(skb), skb->csum,
117 skb->dev ? skb->dev->name : "<NULL>");
118
119 }
120
121 /* check for FCOE packet type */
122 if (unlikely(eth_hdr(skb)->h_proto != htons(ETH_P_FCOE))) {
123 FC_DBG("wrong FC type frame");
124 goto err;
125 }
126
127 /*
128 * Check for minimum frame length, and make sure required FCoE
129 * and FC headers are pulled into the linear data area.
130 */
131 if (unlikely((skb->len < FCOE_MIN_FRAME) ||
132 !pskb_may_pull(skb, FCOE_HEADER_LEN)))
133 goto err;
134
135 skb_set_transport_header(skb, sizeof(struct fcoe_hdr));
136 fh = (struct fc_frame_header *) skb_transport_header(skb);
137
138 oxid = ntohs(fh->fh_ox_id);
139
140 fr = fcoe_dev_from_skb(skb);
141 fr->fr_dev = lp;
142 fr->ptype = ptype;
143 cpu_idx = 0;
Robert Love5e5e92d2009-03-17 11:41:35 -0700144
Robert Love85b4aa42008-12-09 15:10:24 -0800145#ifdef CONFIG_SMP
146 /*
147 * The incoming frame exchange id(oxid) is ANDed with num of online
148 * cpu bits to get cpu_idx and then this cpu_idx is used for selecting
149 * a per cpu kernel thread from fcoe_percpu. In case the cpu is
150 * offline or no kernel thread for derived cpu_idx then cpu_idx is
151 * initialize to first online cpu index.
152 */
153 cpu_idx = oxid & (num_online_cpus() - 1);
Robert Love5e5e92d2009-03-17 11:41:35 -0700154 if (!cpu_online(cpu_idx))
Robert Love85b4aa42008-12-09 15:10:24 -0800155 cpu_idx = first_cpu(cpu_online_map);
Robert Love5e5e92d2009-03-17 11:41:35 -0700156
Robert Love85b4aa42008-12-09 15:10:24 -0800157#endif
Robert Love5e5e92d2009-03-17 11:41:35 -0700158
159 fps = &per_cpu(fcoe_percpu, cpu_idx);
Robert Love85b4aa42008-12-09 15:10:24 -0800160
161 spin_lock_bh(&fps->fcoe_rx_list.lock);
162 __skb_queue_tail(&fps->fcoe_rx_list, skb);
163 if (fps->fcoe_rx_list.qlen == 1)
164 wake_up_process(fps->thread);
165
166 spin_unlock_bh(&fps->fcoe_rx_list.lock);
167
168 return 0;
169err:
Robert Love582b45b2009-03-31 15:51:50 -0700170 fc_lport_get_stats(lp)->ErrorFrames++;
Robert Love85b4aa42008-12-09 15:10:24 -0800171
172err2:
173 kfree_skb(skb);
174 return -1;
175}
176EXPORT_SYMBOL_GPL(fcoe_rcv);
177
178/**
Robert Love34f42a02009-02-27 10:55:45 -0800179 * fcoe_start_io() - pass to netdev to start xmit for fcoe
Robert Love85b4aa42008-12-09 15:10:24 -0800180 * @skb: the skb to be xmitted
181 *
182 * Returns: 0 for success
Robert Love34f42a02009-02-27 10:55:45 -0800183 */
Robert Love85b4aa42008-12-09 15:10:24 -0800184static inline int fcoe_start_io(struct sk_buff *skb)
185{
186 int rc;
187
188 skb_get(skb);
189 rc = dev_queue_xmit(skb);
190 if (rc != 0)
191 return rc;
192 kfree_skb(skb);
193 return 0;
194}
195
196/**
Robert Love34f42a02009-02-27 10:55:45 -0800197 * fcoe_get_paged_crc_eof() - in case we need alloc a page for crc_eof
Robert Love85b4aa42008-12-09 15:10:24 -0800198 * @skb: the skb to be xmitted
199 * @tlen: total len
200 *
201 * Returns: 0 for success
Robert Love34f42a02009-02-27 10:55:45 -0800202 */
Robert Love85b4aa42008-12-09 15:10:24 -0800203static int fcoe_get_paged_crc_eof(struct sk_buff *skb, int tlen)
204{
205 struct fcoe_percpu_s *fps;
206 struct page *page;
Robert Love85b4aa42008-12-09 15:10:24 -0800207
Robert Love5e5e92d2009-03-17 11:41:35 -0700208 fps = &get_cpu_var(fcoe_percpu);
Robert Love85b4aa42008-12-09 15:10:24 -0800209 page = fps->crc_eof_page;
210 if (!page) {
211 page = alloc_page(GFP_ATOMIC);
212 if (!page) {
Robert Love5e5e92d2009-03-17 11:41:35 -0700213 put_cpu_var(fcoe_percpu);
Robert Love85b4aa42008-12-09 15:10:24 -0800214 return -ENOMEM;
215 }
216 fps->crc_eof_page = page;
217 WARN_ON(fps->crc_eof_offset != 0);
218 }
219
220 get_page(page);
221 skb_fill_page_desc(skb, skb_shinfo(skb)->nr_frags, page,
222 fps->crc_eof_offset, tlen);
223 skb->len += tlen;
224 skb->data_len += tlen;
225 skb->truesize += tlen;
226 fps->crc_eof_offset += sizeof(struct fcoe_crc_eof);
227
228 if (fps->crc_eof_offset >= PAGE_SIZE) {
229 fps->crc_eof_page = NULL;
230 fps->crc_eof_offset = 0;
231 put_page(page);
232 }
Robert Love5e5e92d2009-03-17 11:41:35 -0700233 put_cpu_var(fcoe_percpu);
Robert Love85b4aa42008-12-09 15:10:24 -0800234 return 0;
235}
236
237/**
Robert Love34f42a02009-02-27 10:55:45 -0800238 * fcoe_fc_crc() - calculates FC CRC in this fcoe skb
Robert Love85b4aa42008-12-09 15:10:24 -0800239 * @fp: the fc_frame containg data to be checksummed
240 *
241 * This uses crc32() to calculate the crc for fc frame
242 * Return : 32 bit crc
Robert Love34f42a02009-02-27 10:55:45 -0800243 */
Robert Love85b4aa42008-12-09 15:10:24 -0800244u32 fcoe_fc_crc(struct fc_frame *fp)
245{
246 struct sk_buff *skb = fp_skb(fp);
247 struct skb_frag_struct *frag;
248 unsigned char *data;
249 unsigned long off, len, clen;
250 u32 crc;
251 unsigned i;
252
253 crc = crc32(~0, skb->data, skb_headlen(skb));
254
255 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
256 frag = &skb_shinfo(skb)->frags[i];
257 off = frag->page_offset;
258 len = frag->size;
259 while (len > 0) {
260 clen = min(len, PAGE_SIZE - (off & ~PAGE_MASK));
261 data = kmap_atomic(frag->page + (off >> PAGE_SHIFT),
262 KM_SKB_DATA_SOFTIRQ);
263 crc = crc32(crc, data + (off & ~PAGE_MASK), clen);
264 kunmap_atomic(data, KM_SKB_DATA_SOFTIRQ);
265 off += clen;
266 len -= clen;
267 }
268 }
269 return crc;
270}
271EXPORT_SYMBOL_GPL(fcoe_fc_crc);
272
273/**
Robert Love34f42a02009-02-27 10:55:45 -0800274 * fcoe_xmit() - FCoE frame transmit function
Robert Love85b4aa42008-12-09 15:10:24 -0800275 * @lp: the associated local port
276 * @fp: the fc_frame to be transmitted
277 *
278 * Return : 0 for success
Robert Love34f42a02009-02-27 10:55:45 -0800279 */
Robert Love85b4aa42008-12-09 15:10:24 -0800280int fcoe_xmit(struct fc_lport *lp, struct fc_frame *fp)
281{
282 int wlen, rc = 0;
283 u32 crc;
284 struct ethhdr *eh;
285 struct fcoe_crc_eof *cp;
286 struct sk_buff *skb;
287 struct fcoe_dev_stats *stats;
288 struct fc_frame_header *fh;
289 unsigned int hlen; /* header length implies the version */
290 unsigned int tlen; /* trailer length */
291 unsigned int elen; /* eth header, may include vlan */
292 int flogi_in_progress = 0;
293 struct fcoe_softc *fc;
294 u8 sof, eof;
295 struct fcoe_hdr *hp;
296
297 WARN_ON((fr_len(fp) % sizeof(u32)) != 0);
298
Robert Lovefc47ff62009-02-27 10:55:55 -0800299 fc = lport_priv(lp);
Robert Love85b4aa42008-12-09 15:10:24 -0800300 /*
301 * if it is a flogi then we need to learn gw-addr
302 * and my own fcid
303 */
304 fh = fc_frame_header_get(fp);
305 if (unlikely(fh->fh_r_ctl == FC_RCTL_ELS_REQ)) {
306 if (fc_frame_payload_op(fp) == ELS_FLOGI) {
307 fc->flogi_oxid = ntohs(fh->fh_ox_id);
308 fc->address_mode = FCOE_FCOUI_ADDR_MODE;
309 fc->flogi_progress = 1;
310 flogi_in_progress = 1;
311 } else if (fc->flogi_progress && ntoh24(fh->fh_s_id) != 0) {
312 /*
313 * Here we must've gotten an SID by accepting an FLOGI
314 * from a point-to-point connection. Switch to using
315 * the source mac based on the SID. The destination
316 * MAC in this case would have been set by receving the
317 * FLOGI.
318 */
319 fc_fcoe_set_mac(fc->data_src_addr, fh->fh_s_id);
320 fc->flogi_progress = 0;
321 }
322 }
323
324 skb = fp_skb(fp);
325 sof = fr_sof(fp);
326 eof = fr_eof(fp);
327
328 elen = (fc->real_dev->priv_flags & IFF_802_1Q_VLAN) ?
329 sizeof(struct vlan_ethhdr) : sizeof(struct ethhdr);
330 hlen = sizeof(struct fcoe_hdr);
331 tlen = sizeof(struct fcoe_crc_eof);
332 wlen = (skb->len - tlen + sizeof(crc)) / FCOE_WORD_TO_BYTE;
333
334 /* crc offload */
335 if (likely(lp->crc_offload)) {
Yi Zou39ca9a02009-02-27 14:07:15 -0800336 skb->ip_summed = CHECKSUM_PARTIAL;
Robert Love85b4aa42008-12-09 15:10:24 -0800337 skb->csum_start = skb_headroom(skb);
338 skb->csum_offset = skb->len;
339 crc = 0;
340 } else {
341 skb->ip_summed = CHECKSUM_NONE;
342 crc = fcoe_fc_crc(fp);
343 }
344
345 /* copy fc crc and eof to the skb buff */
346 if (skb_is_nonlinear(skb)) {
347 skb_frag_t *frag;
348 if (fcoe_get_paged_crc_eof(skb, tlen)) {
Roel Kluine9041582009-02-27 10:56:22 -0800349 kfree_skb(skb);
Robert Love85b4aa42008-12-09 15:10:24 -0800350 return -ENOMEM;
351 }
352 frag = &skb_shinfo(skb)->frags[skb_shinfo(skb)->nr_frags - 1];
353 cp = kmap_atomic(frag->page, KM_SKB_DATA_SOFTIRQ)
354 + frag->page_offset;
355 } else {
356 cp = (struct fcoe_crc_eof *)skb_put(skb, tlen);
357 }
358
359 memset(cp, 0, sizeof(*cp));
360 cp->fcoe_eof = eof;
361 cp->fcoe_crc32 = cpu_to_le32(~crc);
362
363 if (skb_is_nonlinear(skb)) {
364 kunmap_atomic(cp, KM_SKB_DATA_SOFTIRQ);
365 cp = NULL;
366 }
367
368 /* adjust skb netowrk/transport offsets to match mac/fcoe/fc */
369 skb_push(skb, elen + hlen);
370 skb_reset_mac_header(skb);
371 skb_reset_network_header(skb);
372 skb->mac_len = elen;
Yi Zou211c7382009-02-27 14:06:37 -0800373 skb->protocol = htons(ETH_P_FCOE);
Robert Love85b4aa42008-12-09 15:10:24 -0800374 skb->dev = fc->real_dev;
375
376 /* fill up mac and fcoe headers */
377 eh = eth_hdr(skb);
378 eh->h_proto = htons(ETH_P_FCOE);
379 if (fc->address_mode == FCOE_FCOUI_ADDR_MODE)
380 fc_fcoe_set_mac(eh->h_dest, fh->fh_d_id);
381 else
382 /* insert GW address */
383 memcpy(eh->h_dest, fc->dest_addr, ETH_ALEN);
384
385 if (unlikely(flogi_in_progress))
386 memcpy(eh->h_source, fc->ctl_src_addr, ETH_ALEN);
387 else
388 memcpy(eh->h_source, fc->data_src_addr, ETH_ALEN);
389
390 hp = (struct fcoe_hdr *)(eh + 1);
391 memset(hp, 0, sizeof(*hp));
392 if (FC_FCOE_VER)
393 FC_FCOE_ENCAPS_VER(hp, FC_FCOE_VER);
394 hp->fcoe_sof = sof;
395
Yi Zou39ca9a02009-02-27 14:07:15 -0800396#ifdef NETIF_F_FSO
397 /* fcoe lso, mss is in max_payload which is non-zero for FCP data */
398 if (lp->seq_offload && fr_max_payload(fp)) {
399 skb_shinfo(skb)->gso_type = SKB_GSO_FCOE;
400 skb_shinfo(skb)->gso_size = fr_max_payload(fp);
401 } else {
402 skb_shinfo(skb)->gso_type = 0;
403 skb_shinfo(skb)->gso_size = 0;
404 }
405#endif
Robert Love85b4aa42008-12-09 15:10:24 -0800406 /* update tx stats: regardless if LLD fails */
Robert Love582b45b2009-03-31 15:51:50 -0700407 stats = fc_lport_get_stats(lp);
408 stats->TxFrames++;
409 stats->TxWords += wlen;
Robert Love85b4aa42008-12-09 15:10:24 -0800410
411 /* send down to lld */
412 fr_dev(fp) = lp;
413 if (fc->fcoe_pending_queue.qlen)
414 rc = fcoe_check_wait_queue(lp);
415
416 if (rc == 0)
417 rc = fcoe_start_io(skb);
418
419 if (rc) {
Chris Leech55c8baf2009-02-27 10:56:32 -0800420 spin_lock_bh(&fc->fcoe_pending_queue.lock);
421 __skb_queue_tail(&fc->fcoe_pending_queue, skb);
422 spin_unlock_bh(&fc->fcoe_pending_queue.lock);
Robert Love85b4aa42008-12-09 15:10:24 -0800423 if (fc->fcoe_pending_queue.qlen > FCOE_MAX_QUEUE_DEPTH)
Vasu Devbc0e17f2009-02-27 10:54:57 -0800424 lp->qfull = 1;
Robert Love85b4aa42008-12-09 15:10:24 -0800425 }
426
427 return 0;
428}
429EXPORT_SYMBOL_GPL(fcoe_xmit);
430
Robert Love34f42a02009-02-27 10:55:45 -0800431/**
432 * fcoe_percpu_receive_thread() - recv thread per cpu
Robert Love85b4aa42008-12-09 15:10:24 -0800433 * @arg: ptr to the fcoe per cpu struct
434 *
435 * Return: 0 for success
Robert Love85b4aa42008-12-09 15:10:24 -0800436 */
437int fcoe_percpu_receive_thread(void *arg)
438{
439 struct fcoe_percpu_s *p = arg;
440 u32 fr_len;
441 struct fc_lport *lp;
442 struct fcoe_rcv_info *fr;
443 struct fcoe_dev_stats *stats;
444 struct fc_frame_header *fh;
445 struct sk_buff *skb;
446 struct fcoe_crc_eof crc_eof;
447 struct fc_frame *fp;
448 u8 *mac = NULL;
449 struct fcoe_softc *fc;
450 struct fcoe_hdr *hp;
451
Robert Love4469c192009-02-27 10:56:38 -0800452 set_user_nice(current, -20);
Robert Love85b4aa42008-12-09 15:10:24 -0800453
454 while (!kthread_should_stop()) {
455
456 spin_lock_bh(&p->fcoe_rx_list.lock);
457 while ((skb = __skb_dequeue(&p->fcoe_rx_list)) == NULL) {
458 set_current_state(TASK_INTERRUPTIBLE);
459 spin_unlock_bh(&p->fcoe_rx_list.lock);
460 schedule();
461 set_current_state(TASK_RUNNING);
462 if (kthread_should_stop())
463 return 0;
464 spin_lock_bh(&p->fcoe_rx_list.lock);
465 }
466 spin_unlock_bh(&p->fcoe_rx_list.lock);
467 fr = fcoe_dev_from_skb(skb);
468 lp = fr->fr_dev;
469 if (unlikely(lp == NULL)) {
470 FC_DBG("invalid HBA Structure");
471 kfree_skb(skb);
472 continue;
473 }
474
Robert Love85b4aa42008-12-09 15:10:24 -0800475 if (unlikely(debug_fcoe)) {
476 FC_DBG("skb_info: len:%d data_len:%d head:%p data:%p "
477 "tail:%p end:%p sum:%d dev:%s",
478 skb->len, skb->data_len,
479 skb->head, skb->data, skb_tail_pointer(skb),
480 skb_end_pointer(skb), skb->csum,
481 skb->dev ? skb->dev->name : "<NULL>");
482 }
483
484 /*
485 * Save source MAC address before discarding header.
486 */
487 fc = lport_priv(lp);
488 if (unlikely(fc->flogi_progress))
489 mac = eth_hdr(skb)->h_source;
490
491 if (skb_is_nonlinear(skb))
492 skb_linearize(skb); /* not ideal */
493
494 /*
495 * Frame length checks and setting up the header pointers
496 * was done in fcoe_rcv already.
497 */
498 hp = (struct fcoe_hdr *) skb_network_header(skb);
499 fh = (struct fc_frame_header *) skb_transport_header(skb);
500
Robert Love582b45b2009-03-31 15:51:50 -0700501 stats = fc_lport_get_stats(lp);
Robert Love85b4aa42008-12-09 15:10:24 -0800502 if (unlikely(FC_FCOE_DECAPS_VER(hp) != FC_FCOE_VER)) {
Robert Love582b45b2009-03-31 15:51:50 -0700503 if (stats->ErrorFrames < 5)
504 printk(KERN_WARNING "FCoE version "
505 "mismatch: The frame has "
506 "version %x, but the "
507 "initiator supports version "
508 "%x\n", FC_FCOE_DECAPS_VER(hp),
509 FC_FCOE_VER);
510 stats->ErrorFrames++;
Robert Love85b4aa42008-12-09 15:10:24 -0800511 kfree_skb(skb);
512 continue;
513 }
514
515 skb_pull(skb, sizeof(struct fcoe_hdr));
516 fr_len = skb->len - sizeof(struct fcoe_crc_eof);
517
Robert Love582b45b2009-03-31 15:51:50 -0700518 stats->RxFrames++;
519 stats->RxWords += fr_len / FCOE_WORD_TO_BYTE;
Robert Love85b4aa42008-12-09 15:10:24 -0800520
521 fp = (struct fc_frame *)skb;
522 fc_frame_init(fp);
523 fr_dev(fp) = lp;
524 fr_sof(fp) = hp->fcoe_sof;
525
526 /* Copy out the CRC and EOF trailer for access */
527 if (skb_copy_bits(skb, fr_len, &crc_eof, sizeof(crc_eof))) {
528 kfree_skb(skb);
529 continue;
530 }
531 fr_eof(fp) = crc_eof.fcoe_eof;
532 fr_crc(fp) = crc_eof.fcoe_crc32;
533 if (pskb_trim(skb, fr_len)) {
534 kfree_skb(skb);
535 continue;
536 }
537
538 /*
539 * We only check CRC if no offload is available and if it is
540 * it's solicited data, in which case, the FCP layer would
541 * check it during the copy.
542 */
Yi Zou07c00ec2009-02-27 14:07:31 -0800543 if (lp->crc_offload && skb->ip_summed == CHECKSUM_UNNECESSARY)
Robert Love85b4aa42008-12-09 15:10:24 -0800544 fr_flags(fp) &= ~FCPHF_CRC_UNCHECKED;
545 else
546 fr_flags(fp) |= FCPHF_CRC_UNCHECKED;
547
548 fh = fc_frame_header_get(fp);
549 if (fh->fh_r_ctl == FC_RCTL_DD_SOL_DATA &&
550 fh->fh_type == FC_TYPE_FCP) {
551 fc_exch_recv(lp, lp->emp, fp);
552 continue;
553 }
554 if (fr_flags(fp) & FCPHF_CRC_UNCHECKED) {
555 if (le32_to_cpu(fr_crc(fp)) !=
556 ~crc32(~0, skb->data, fr_len)) {
557 if (debug_fcoe || stats->InvalidCRCCount < 5)
558 printk(KERN_WARNING "fcoe: dropping "
559 "frame with CRC error\n");
560 stats->InvalidCRCCount++;
561 stats->ErrorFrames++;
562 fc_frame_free(fp);
563 continue;
564 }
565 fr_flags(fp) &= ~FCPHF_CRC_UNCHECKED;
566 }
567 /* non flogi and non data exchanges are handled here */
568 if (unlikely(fc->flogi_progress))
569 fcoe_recv_flogi(fc, fp, mac);
570 fc_exch_recv(lp, lp->emp, fp);
571 }
572 return 0;
573}
574
575/**
Robert Love34f42a02009-02-27 10:55:45 -0800576 * fcoe_recv_flogi() - flogi receive function
Robert Love85b4aa42008-12-09 15:10:24 -0800577 * @fc: associated fcoe_softc
578 * @fp: the recieved frame
579 * @sa: the source address of this flogi
580 *
581 * This is responsible to parse the flogi response and sets the corresponding
582 * mac address for the initiator, eitehr OUI based or GW based.
583 *
584 * Returns: none
Robert Love34f42a02009-02-27 10:55:45 -0800585 */
Robert Love85b4aa42008-12-09 15:10:24 -0800586static void fcoe_recv_flogi(struct fcoe_softc *fc, struct fc_frame *fp, u8 *sa)
587{
588 struct fc_frame_header *fh;
589 u8 op;
590
591 fh = fc_frame_header_get(fp);
592 if (fh->fh_type != FC_TYPE_ELS)
593 return;
594 op = fc_frame_payload_op(fp);
595 if (op == ELS_LS_ACC && fh->fh_r_ctl == FC_RCTL_ELS_REP &&
596 fc->flogi_oxid == ntohs(fh->fh_ox_id)) {
597 /*
598 * FLOGI accepted.
599 * If the src mac addr is FC_OUI-based, then we mark the
600 * address_mode flag to use FC_OUI-based Ethernet DA.
601 * Otherwise we use the FCoE gateway addr
602 */
603 if (!compare_ether_addr(sa, (u8[6]) FC_FCOE_FLOGI_MAC)) {
604 fc->address_mode = FCOE_FCOUI_ADDR_MODE;
605 } else {
606 memcpy(fc->dest_addr, sa, ETH_ALEN);
607 fc->address_mode = FCOE_GW_ADDR_MODE;
608 }
609
610 /*
611 * Remove any previously-set unicast MAC filter.
612 * Add secondary FCoE MAC address filter for our OUI.
613 */
614 rtnl_lock();
615 if (compare_ether_addr(fc->data_src_addr, (u8[6]) { 0 }))
616 dev_unicast_delete(fc->real_dev, fc->data_src_addr,
617 ETH_ALEN);
618 fc_fcoe_set_mac(fc->data_src_addr, fh->fh_d_id);
619 dev_unicast_add(fc->real_dev, fc->data_src_addr, ETH_ALEN);
620 rtnl_unlock();
621
622 fc->flogi_progress = 0;
623 } else if (op == ELS_FLOGI && fh->fh_r_ctl == FC_RCTL_ELS_REQ && sa) {
624 /*
625 * Save source MAC for point-to-point responses.
626 */
627 memcpy(fc->dest_addr, sa, ETH_ALEN);
628 fc->address_mode = FCOE_GW_ADDR_MODE;
629 }
630}
631
632/**
Robert Love34f42a02009-02-27 10:55:45 -0800633 * fcoe_watchdog() - fcoe timer callback
Robert Love85b4aa42008-12-09 15:10:24 -0800634 * @vp:
635 *
Vasu Devbc0e17f2009-02-27 10:54:57 -0800636 * This checks the pending queue length for fcoe and set lport qfull
Robert Love85b4aa42008-12-09 15:10:24 -0800637 * if the FCOE_MAX_QUEUE_DEPTH is reached. This is done for all fc_lport on the
638 * fcoe_hostlist.
639 *
640 * Returns: 0 for success
Robert Love34f42a02009-02-27 10:55:45 -0800641 */
Robert Love85b4aa42008-12-09 15:10:24 -0800642void fcoe_watchdog(ulong vp)
643{
Robert Love85b4aa42008-12-09 15:10:24 -0800644 struct fcoe_softc *fc;
Robert Love85b4aa42008-12-09 15:10:24 -0800645
646 read_lock(&fcoe_hostlist_lock);
647 list_for_each_entry(fc, &fcoe_hostlist, list) {
Vasu Devc826a312009-02-27 10:56:27 -0800648 if (fc->lp)
649 fcoe_check_wait_queue(fc->lp);
Robert Love85b4aa42008-12-09 15:10:24 -0800650 }
651 read_unlock(&fcoe_hostlist_lock);
652
653 fcoe_timer.expires = jiffies + (1 * HZ);
654 add_timer(&fcoe_timer);
655}
656
657
658/**
Robert Love34f42a02009-02-27 10:55:45 -0800659 * fcoe_check_wait_queue() - put the skb into fcoe pending xmit queue
Robert Love85b4aa42008-12-09 15:10:24 -0800660 * @lp: the fc_port for this skb
661 * @skb: the associated skb to be xmitted
662 *
663 * This empties the wait_queue, dequeue the head of the wait_queue queue
664 * and calls fcoe_start_io() for each packet, if all skb have been
Vasu Devc826a312009-02-27 10:56:27 -0800665 * transmitted, return qlen or -1 if a error occurs, then restore
666 * wait_queue and try again later.
Robert Love85b4aa42008-12-09 15:10:24 -0800667 *
668 * The wait_queue is used when the skb transmit fails. skb will go
669 * in the wait_queue which will be emptied by the time function OR
670 * by the next skb transmit.
671 *
672 * Returns: 0 for success
Robert Love34f42a02009-02-27 10:55:45 -0800673 */
Robert Love85b4aa42008-12-09 15:10:24 -0800674static int fcoe_check_wait_queue(struct fc_lport *lp)
675{
Chris Leech55c8baf2009-02-27 10:56:32 -0800676 struct fcoe_softc *fc = lport_priv(lp);
Robert Love85b4aa42008-12-09 15:10:24 -0800677 struct sk_buff *skb;
Vasu Devc826a312009-02-27 10:56:27 -0800678 int rc = -1;
Robert Love85b4aa42008-12-09 15:10:24 -0800679
Robert Love85b4aa42008-12-09 15:10:24 -0800680 spin_lock_bh(&fc->fcoe_pending_queue.lock);
Vasu Devc826a312009-02-27 10:56:27 -0800681 if (fc->fcoe_pending_queue_active)
682 goto out;
683 fc->fcoe_pending_queue_active = 1;
Chris Leech55c8baf2009-02-27 10:56:32 -0800684
685 while (fc->fcoe_pending_queue.qlen) {
686 /* keep qlen > 0 until fcoe_start_io succeeds */
687 fc->fcoe_pending_queue.qlen++;
688 skb = __skb_dequeue(&fc->fcoe_pending_queue);
689
690 spin_unlock_bh(&fc->fcoe_pending_queue.lock);
691 rc = fcoe_start_io(skb);
692 spin_lock_bh(&fc->fcoe_pending_queue.lock);
693
694 if (rc) {
695 __skb_queue_head(&fc->fcoe_pending_queue, skb);
696 /* undo temporary increment above */
697 fc->fcoe_pending_queue.qlen--;
698 break;
Robert Love85b4aa42008-12-09 15:10:24 -0800699 }
Chris Leech55c8baf2009-02-27 10:56:32 -0800700 /* undo temporary increment above */
701 fc->fcoe_pending_queue.qlen--;
Robert Love85b4aa42008-12-09 15:10:24 -0800702 }
Chris Leech55c8baf2009-02-27 10:56:32 -0800703
704 if (fc->fcoe_pending_queue.qlen < FCOE_LOW_QUEUE_DEPTH)
705 lp->qfull = 0;
Vasu Devc826a312009-02-27 10:56:27 -0800706 fc->fcoe_pending_queue_active = 0;
707 rc = fc->fcoe_pending_queue.qlen;
708out:
Robert Love85b4aa42008-12-09 15:10:24 -0800709 spin_unlock_bh(&fc->fcoe_pending_queue.lock);
Vasu Devc826a312009-02-27 10:56:27 -0800710 return rc;
Robert Love85b4aa42008-12-09 15:10:24 -0800711}
712
713/**
Robert Love34f42a02009-02-27 10:55:45 -0800714 * fcoe_dev_setup() - setup link change notification interface
715 */
716static void fcoe_dev_setup()
Robert Love85b4aa42008-12-09 15:10:24 -0800717{
718 /*
719 * here setup a interface specific wd time to
720 * monitor the link state
721 */
722 register_netdevice_notifier(&fcoe_notifier);
723}
724
725/**
Robert Love34f42a02009-02-27 10:55:45 -0800726 * fcoe_dev_setup() - cleanup link change notification interface
727 */
Robert Love85b4aa42008-12-09 15:10:24 -0800728static void fcoe_dev_cleanup(void)
729{
730 unregister_netdevice_notifier(&fcoe_notifier);
731}
732
733/**
Robert Love34f42a02009-02-27 10:55:45 -0800734 * fcoe_device_notification() - netdev event notification callback
Robert Love85b4aa42008-12-09 15:10:24 -0800735 * @notifier: context of the notification
736 * @event: type of event
737 * @ptr: fixed array for output parsed ifname
738 *
739 * This function is called by the ethernet driver in case of link change event
740 *
741 * Returns: 0 for success
Robert Love34f42a02009-02-27 10:55:45 -0800742 */
Robert Love85b4aa42008-12-09 15:10:24 -0800743static int fcoe_device_notification(struct notifier_block *notifier,
744 ulong event, void *ptr)
745{
746 struct fc_lport *lp = NULL;
747 struct net_device *real_dev = ptr;
748 struct fcoe_softc *fc;
749 struct fcoe_dev_stats *stats;
Vasu Devbc0e17f2009-02-27 10:54:57 -0800750 u32 new_link_up;
Robert Love85b4aa42008-12-09 15:10:24 -0800751 u32 mfs;
752 int rc = NOTIFY_OK;
753
754 read_lock(&fcoe_hostlist_lock);
755 list_for_each_entry(fc, &fcoe_hostlist, list) {
756 if (fc->real_dev == real_dev) {
757 lp = fc->lp;
758 break;
759 }
760 }
761 read_unlock(&fcoe_hostlist_lock);
762 if (lp == NULL) {
763 rc = NOTIFY_DONE;
764 goto out;
765 }
766
Vasu Devbc0e17f2009-02-27 10:54:57 -0800767 new_link_up = lp->link_up;
Robert Love85b4aa42008-12-09 15:10:24 -0800768 switch (event) {
769 case NETDEV_DOWN:
770 case NETDEV_GOING_DOWN:
Vasu Devbc0e17f2009-02-27 10:54:57 -0800771 new_link_up = 0;
Robert Love85b4aa42008-12-09 15:10:24 -0800772 break;
773 case NETDEV_UP:
774 case NETDEV_CHANGE:
Vasu Devbc0e17f2009-02-27 10:54:57 -0800775 new_link_up = !fcoe_link_ok(lp);
Robert Love85b4aa42008-12-09 15:10:24 -0800776 break;
777 case NETDEV_CHANGEMTU:
778 mfs = fc->real_dev->mtu -
779 (sizeof(struct fcoe_hdr) +
780 sizeof(struct fcoe_crc_eof));
781 if (mfs >= FC_MIN_MAX_FRAME)
782 fc_set_mfs(lp, mfs);
Vasu Devbc0e17f2009-02-27 10:54:57 -0800783 new_link_up = !fcoe_link_ok(lp);
Robert Love85b4aa42008-12-09 15:10:24 -0800784 break;
785 case NETDEV_REGISTER:
786 break;
787 default:
788 FC_DBG("unknown event %ld call", event);
789 }
Vasu Devbc0e17f2009-02-27 10:54:57 -0800790 if (lp->link_up != new_link_up) {
791 if (new_link_up)
Robert Love85b4aa42008-12-09 15:10:24 -0800792 fc_linkup(lp);
793 else {
Robert Love582b45b2009-03-31 15:51:50 -0700794 stats = fc_lport_get_stats(lp);
795 stats->LinkFailureCount++;
Robert Love85b4aa42008-12-09 15:10:24 -0800796 fc_linkdown(lp);
797 fcoe_clean_pending_queue(lp);
798 }
799 }
800out:
801 return rc;
802}
803
804/**
Robert Love34f42a02009-02-27 10:55:45 -0800805 * fcoe_if_to_netdev() - parse a name buffer to get netdev
Robert Love85b4aa42008-12-09 15:10:24 -0800806 * @ifname: fixed array for output parsed ifname
807 * @buffer: incoming buffer to be copied
808 *
809 * Returns: NULL or ptr to netdeive
Robert Love34f42a02009-02-27 10:55:45 -0800810 */
Robert Love85b4aa42008-12-09 15:10:24 -0800811static struct net_device *fcoe_if_to_netdev(const char *buffer)
812{
813 char *cp;
814 char ifname[IFNAMSIZ + 2];
815
816 if (buffer) {
817 strlcpy(ifname, buffer, IFNAMSIZ);
818 cp = ifname + strlen(ifname);
819 while (--cp >= ifname && *cp == '\n')
820 *cp = '\0';
821 return dev_get_by_name(&init_net, ifname);
822 }
823 return NULL;
824}
825
826/**
Robert Love34f42a02009-02-27 10:55:45 -0800827 * fcoe_netdev_to_module_owner() - finds out the nic drive moddule of the netdev
Robert Love85b4aa42008-12-09 15:10:24 -0800828 * @netdev: the target netdev
829 *
830 * Returns: ptr to the struct module, NULL for failure
Robert Love34f42a02009-02-27 10:55:45 -0800831 */
Robert Loveb2ab99c2009-02-27 10:55:50 -0800832static struct module *
833fcoe_netdev_to_module_owner(const struct net_device *netdev)
Robert Love85b4aa42008-12-09 15:10:24 -0800834{
835 struct device *dev;
836
837 if (!netdev)
838 return NULL;
839
840 dev = netdev->dev.parent;
841 if (!dev)
842 return NULL;
843
844 if (!dev->driver)
845 return NULL;
846
847 return dev->driver->owner;
848}
849
850/**
Robert Love34f42a02009-02-27 10:55:45 -0800851 * fcoe_ethdrv_get() - Hold the Ethernet driver
Robert Love85b4aa42008-12-09 15:10:24 -0800852 * @netdev: the target netdev
853 *
Robert Love34f42a02009-02-27 10:55:45 -0800854 * Holds the Ethernet driver module by try_module_get() for
855 * the corresponding netdev.
856 *
Robert Love85b4aa42008-12-09 15:10:24 -0800857 * Returns: 0 for succsss
Robert Love34f42a02009-02-27 10:55:45 -0800858 */
Robert Love85b4aa42008-12-09 15:10:24 -0800859static int fcoe_ethdrv_get(const struct net_device *netdev)
860{
861 struct module *owner;
862
863 owner = fcoe_netdev_to_module_owner(netdev);
864 if (owner) {
James Bottomley56b854b2008-12-29 15:45:41 -0600865 printk(KERN_DEBUG "fcoe:hold driver module %s for %s\n",
866 module_name(owner), netdev->name);
Robert Love85b4aa42008-12-09 15:10:24 -0800867 return try_module_get(owner);
868 }
869 return -ENODEV;
870}
871
872/**
Robert Love34f42a02009-02-27 10:55:45 -0800873 * fcoe_ethdrv_put() - Release the Ethernet driver
Robert Love85b4aa42008-12-09 15:10:24 -0800874 * @netdev: the target netdev
875 *
Robert Love34f42a02009-02-27 10:55:45 -0800876 * Releases the Ethernet driver module by module_put for
877 * the corresponding netdev.
878 *
Robert Love85b4aa42008-12-09 15:10:24 -0800879 * Returns: 0 for succsss
Robert Love34f42a02009-02-27 10:55:45 -0800880 */
Robert Love85b4aa42008-12-09 15:10:24 -0800881static int fcoe_ethdrv_put(const struct net_device *netdev)
882{
883 struct module *owner;
884
885 owner = fcoe_netdev_to_module_owner(netdev);
886 if (owner) {
James Bottomley56b854b2008-12-29 15:45:41 -0600887 printk(KERN_DEBUG "fcoe:release driver module %s for %s\n",
888 module_name(owner), netdev->name);
Robert Love85b4aa42008-12-09 15:10:24 -0800889 module_put(owner);
890 return 0;
891 }
892 return -ENODEV;
893}
894
895/**
Robert Love34f42a02009-02-27 10:55:45 -0800896 * fcoe_destroy() - handles the destroy from sysfs
Robert Love85b4aa42008-12-09 15:10:24 -0800897 * @buffer: expcted to be a eth if name
898 * @kp: associated kernel param
899 *
900 * Returns: 0 for success
Robert Love34f42a02009-02-27 10:55:45 -0800901 */
Robert Love85b4aa42008-12-09 15:10:24 -0800902static int fcoe_destroy(const char *buffer, struct kernel_param *kp)
903{
904 int rc;
905 struct net_device *netdev;
906
907 netdev = fcoe_if_to_netdev(buffer);
908 if (!netdev) {
909 rc = -ENODEV;
910 goto out_nodev;
911 }
912 /* look for existing lport */
913 if (!fcoe_hostlist_lookup(netdev)) {
914 rc = -ENODEV;
915 goto out_putdev;
916 }
917 /* pass to transport */
918 rc = fcoe_transport_release(netdev);
919 if (rc) {
920 printk(KERN_ERR "fcoe: fcoe_transport_release(%s) failed\n",
921 netdev->name);
922 rc = -EIO;
923 goto out_putdev;
924 }
925 fcoe_ethdrv_put(netdev);
926 rc = 0;
927out_putdev:
928 dev_put(netdev);
929out_nodev:
930 return rc;
931}
932
933/**
Robert Love34f42a02009-02-27 10:55:45 -0800934 * fcoe_create() - Handles the create call from sysfs
Robert Love85b4aa42008-12-09 15:10:24 -0800935 * @buffer: expcted to be a eth if name
936 * @kp: associated kernel param
937 *
938 * Returns: 0 for success
Robert Love34f42a02009-02-27 10:55:45 -0800939 */
Robert Love85b4aa42008-12-09 15:10:24 -0800940static int fcoe_create(const char *buffer, struct kernel_param *kp)
941{
942 int rc;
943 struct net_device *netdev;
944
945 netdev = fcoe_if_to_netdev(buffer);
946 if (!netdev) {
947 rc = -ENODEV;
948 goto out_nodev;
949 }
950 /* look for existing lport */
951 if (fcoe_hostlist_lookup(netdev)) {
952 rc = -EEXIST;
953 goto out_putdev;
954 }
955 fcoe_ethdrv_get(netdev);
956
957 /* pass to transport */
958 rc = fcoe_transport_attach(netdev);
959 if (rc) {
960 printk(KERN_ERR "fcoe: fcoe_transport_attach(%s) failed\n",
961 netdev->name);
962 fcoe_ethdrv_put(netdev);
963 rc = -EIO;
964 goto out_putdev;
965 }
966 rc = 0;
967out_putdev:
968 dev_put(netdev);
969out_nodev:
970 return rc;
971}
972
973module_param_call(create, fcoe_create, NULL, NULL, S_IWUSR);
974__MODULE_PARM_TYPE(create, "string");
975MODULE_PARM_DESC(create, "Create fcoe port using net device passed in.");
976module_param_call(destroy, fcoe_destroy, NULL, NULL, S_IWUSR);
977__MODULE_PARM_TYPE(destroy, "string");
978MODULE_PARM_DESC(destroy, "Destroy fcoe port");
979
Robert Love34f42a02009-02-27 10:55:45 -0800980/**
981 * fcoe_link_ok() - Check if link is ok for the fc_lport
Robert Love85b4aa42008-12-09 15:10:24 -0800982 * @lp: ptr to the fc_lport
983 *
984 * Any permanently-disqualifying conditions have been previously checked.
985 * This also updates the speed setting, which may change with link for 100/1000.
986 *
987 * This function should probably be checking for PAUSE support at some point
988 * in the future. Currently Per-priority-pause is not determinable using
989 * ethtool, so we shouldn't be restrictive until that problem is resolved.
990 *
991 * Returns: 0 if link is OK for use by FCoE.
992 *
993 */
994int fcoe_link_ok(struct fc_lport *lp)
995{
Robert Lovefc47ff62009-02-27 10:55:55 -0800996 struct fcoe_softc *fc = lport_priv(lp);
Robert Love85b4aa42008-12-09 15:10:24 -0800997 struct net_device *dev = fc->real_dev;
998 struct ethtool_cmd ecmd = { ETHTOOL_GSET };
999 int rc = 0;
1000
1001 if ((dev->flags & IFF_UP) && netif_carrier_ok(dev)) {
1002 dev = fc->phys_dev;
1003 if (dev->ethtool_ops->get_settings) {
1004 dev->ethtool_ops->get_settings(dev, &ecmd);
1005 lp->link_supported_speeds &=
1006 ~(FC_PORTSPEED_1GBIT | FC_PORTSPEED_10GBIT);
1007 if (ecmd.supported & (SUPPORTED_1000baseT_Half |
1008 SUPPORTED_1000baseT_Full))
1009 lp->link_supported_speeds |= FC_PORTSPEED_1GBIT;
1010 if (ecmd.supported & SUPPORTED_10000baseT_Full)
1011 lp->link_supported_speeds |=
1012 FC_PORTSPEED_10GBIT;
1013 if (ecmd.speed == SPEED_1000)
1014 lp->link_speed = FC_PORTSPEED_1GBIT;
1015 if (ecmd.speed == SPEED_10000)
1016 lp->link_speed = FC_PORTSPEED_10GBIT;
1017 }
1018 } else
1019 rc = -1;
1020
1021 return rc;
1022}
1023EXPORT_SYMBOL_GPL(fcoe_link_ok);
1024
Robert Love34f42a02009-02-27 10:55:45 -08001025/**
1026 * fcoe_percpu_clean() - Clear the pending skbs for an lport
Robert Love85b4aa42008-12-09 15:10:24 -08001027 * @lp: the fc_lport
1028 */
1029void fcoe_percpu_clean(struct fc_lport *lp)
1030{
Robert Love85b4aa42008-12-09 15:10:24 -08001031 struct fcoe_percpu_s *pp;
1032 struct fcoe_rcv_info *fr;
1033 struct sk_buff_head *list;
1034 struct sk_buff *skb, *next;
1035 struct sk_buff *head;
Robert Love5e5e92d2009-03-17 11:41:35 -07001036 unsigned int cpu;
Robert Love85b4aa42008-12-09 15:10:24 -08001037
Robert Love5e5e92d2009-03-17 11:41:35 -07001038 for_each_possible_cpu(cpu) {
1039 pp = &per_cpu(fcoe_percpu, cpu);
1040 spin_lock_bh(&pp->fcoe_rx_list.lock);
1041 list = &pp->fcoe_rx_list;
1042 head = list->next;
1043 for (skb = head; skb != (struct sk_buff *)list;
1044 skb = next) {
1045 next = skb->next;
1046 fr = fcoe_dev_from_skb(skb);
1047 if (fr->fr_dev == lp) {
1048 __skb_unlink(skb, list);
1049 kfree_skb(skb);
Robert Love85b4aa42008-12-09 15:10:24 -08001050 }
Robert Love85b4aa42008-12-09 15:10:24 -08001051 }
Robert Love5e5e92d2009-03-17 11:41:35 -07001052 spin_unlock_bh(&pp->fcoe_rx_list.lock);
Robert Love85b4aa42008-12-09 15:10:24 -08001053 }
1054}
1055EXPORT_SYMBOL_GPL(fcoe_percpu_clean);
1056
1057/**
Robert Love34f42a02009-02-27 10:55:45 -08001058 * fcoe_clean_pending_queue() - Dequeue a skb and free it
Robert Love85b4aa42008-12-09 15:10:24 -08001059 * @lp: the corresponding fc_lport
1060 *
1061 * Returns: none
Robert Love34f42a02009-02-27 10:55:45 -08001062 */
Robert Love85b4aa42008-12-09 15:10:24 -08001063void fcoe_clean_pending_queue(struct fc_lport *lp)
1064{
1065 struct fcoe_softc *fc = lport_priv(lp);
1066 struct sk_buff *skb;
1067
1068 spin_lock_bh(&fc->fcoe_pending_queue.lock);
1069 while ((skb = __skb_dequeue(&fc->fcoe_pending_queue)) != NULL) {
1070 spin_unlock_bh(&fc->fcoe_pending_queue.lock);
1071 kfree_skb(skb);
1072 spin_lock_bh(&fc->fcoe_pending_queue.lock);
1073 }
1074 spin_unlock_bh(&fc->fcoe_pending_queue.lock);
1075}
1076EXPORT_SYMBOL_GPL(fcoe_clean_pending_queue);
1077
1078/**
Robert Love34f42a02009-02-27 10:55:45 -08001079 * libfc_host_alloc() - Allocate a Scsi_Host with room for the fc_lport
Robert Love85b4aa42008-12-09 15:10:24 -08001080 * @sht: ptr to the scsi host templ
1081 * @priv_size: size of private data after fc_lport
1082 *
1083 * Returns: ptr to Scsi_Host
Robert Love34f42a02009-02-27 10:55:45 -08001084 * TODO: to libfc?
Robert Love85b4aa42008-12-09 15:10:24 -08001085 */
Robert Loveb2ab99c2009-02-27 10:55:50 -08001086static inline struct Scsi_Host *
1087libfc_host_alloc(struct scsi_host_template *sht, int priv_size)
Robert Love85b4aa42008-12-09 15:10:24 -08001088{
1089 return scsi_host_alloc(sht, sizeof(struct fc_lport) + priv_size);
1090}
1091
1092/**
Robert Love34f42a02009-02-27 10:55:45 -08001093 * fcoe_host_alloc() - Allocate a Scsi_Host with room for the fcoe_softc
Robert Love85b4aa42008-12-09 15:10:24 -08001094 * @sht: ptr to the scsi host templ
1095 * @priv_size: size of private data after fc_lport
1096 *
1097 * Returns: ptr to Scsi_Host
1098 */
1099struct Scsi_Host *fcoe_host_alloc(struct scsi_host_template *sht, int priv_size)
1100{
1101 return libfc_host_alloc(sht, sizeof(struct fcoe_softc) + priv_size);
1102}
1103EXPORT_SYMBOL_GPL(fcoe_host_alloc);
1104
Robert Love34f42a02009-02-27 10:55:45 -08001105/**
1106 * fcoe_reset() - Resets the fcoe
Robert Love85b4aa42008-12-09 15:10:24 -08001107 * @shost: shost the reset is from
1108 *
1109 * Returns: always 0
1110 */
1111int fcoe_reset(struct Scsi_Host *shost)
1112{
1113 struct fc_lport *lport = shost_priv(shost);
1114 fc_lport_reset(lport);
1115 return 0;
1116}
1117EXPORT_SYMBOL_GPL(fcoe_reset);
1118
Robert Love34f42a02009-02-27 10:55:45 -08001119/**
1120 * fcoe_wwn_from_mac() - Converts 48-bit IEEE MAC address to 64-bit FC WWN.
Robert Love85b4aa42008-12-09 15:10:24 -08001121 * @mac: mac address
1122 * @scheme: check port
1123 * @port: port indicator for converting
1124 *
1125 * Returns: u64 fc world wide name
1126 */
1127u64 fcoe_wwn_from_mac(unsigned char mac[MAX_ADDR_LEN],
1128 unsigned int scheme, unsigned int port)
1129{
1130 u64 wwn;
1131 u64 host_mac;
1132
1133 /* The MAC is in NO, so flip only the low 48 bits */
1134 host_mac = ((u64) mac[0] << 40) |
1135 ((u64) mac[1] << 32) |
1136 ((u64) mac[2] << 24) |
1137 ((u64) mac[3] << 16) |
1138 ((u64) mac[4] << 8) |
1139 (u64) mac[5];
1140
1141 WARN_ON(host_mac >= (1ULL << 48));
1142 wwn = host_mac | ((u64) scheme << 60);
1143 switch (scheme) {
1144 case 1:
1145 WARN_ON(port != 0);
1146 break;
1147 case 2:
1148 WARN_ON(port >= 0xfff);
1149 wwn |= (u64) port << 48;
1150 break;
1151 default:
1152 WARN_ON(1);
1153 break;
1154 }
1155
1156 return wwn;
1157}
1158EXPORT_SYMBOL_GPL(fcoe_wwn_from_mac);
Robert Love34f42a02009-02-27 10:55:45 -08001159
1160/**
1161 * fcoe_hostlist_lookup_softc() - find the corresponding lport by a given device
Robert Love85b4aa42008-12-09 15:10:24 -08001162 * @device: this is currently ptr to net_device
1163 *
1164 * Returns: NULL or the located fcoe_softc
1165 */
Robert Loveb2ab99c2009-02-27 10:55:50 -08001166static struct fcoe_softc *
1167fcoe_hostlist_lookup_softc(const struct net_device *dev)
Robert Love85b4aa42008-12-09 15:10:24 -08001168{
1169 struct fcoe_softc *fc;
1170
1171 read_lock(&fcoe_hostlist_lock);
1172 list_for_each_entry(fc, &fcoe_hostlist, list) {
1173 if (fc->real_dev == dev) {
1174 read_unlock(&fcoe_hostlist_lock);
1175 return fc;
1176 }
1177 }
1178 read_unlock(&fcoe_hostlist_lock);
1179 return NULL;
1180}
1181
Robert Love34f42a02009-02-27 10:55:45 -08001182/**
1183 * fcoe_hostlist_lookup() - Find the corresponding lport by netdev
Robert Love85b4aa42008-12-09 15:10:24 -08001184 * @netdev: ptr to net_device
1185 *
1186 * Returns: 0 for success
1187 */
1188struct fc_lport *fcoe_hostlist_lookup(const struct net_device *netdev)
1189{
1190 struct fcoe_softc *fc;
1191
1192 fc = fcoe_hostlist_lookup_softc(netdev);
1193
1194 return (fc) ? fc->lp : NULL;
1195}
1196EXPORT_SYMBOL_GPL(fcoe_hostlist_lookup);
1197
Robert Love34f42a02009-02-27 10:55:45 -08001198/**
1199 * fcoe_hostlist_add() - Add a lport to lports list
Robert Love85b4aa42008-12-09 15:10:24 -08001200 * @lp: ptr to the fc_lport to badded
1201 *
1202 * Returns: 0 for success
1203 */
1204int fcoe_hostlist_add(const struct fc_lport *lp)
1205{
1206 struct fcoe_softc *fc;
1207
1208 fc = fcoe_hostlist_lookup_softc(fcoe_netdev(lp));
1209 if (!fc) {
Robert Lovefc47ff62009-02-27 10:55:55 -08001210 fc = lport_priv(lp);
Robert Love85b4aa42008-12-09 15:10:24 -08001211 write_lock_bh(&fcoe_hostlist_lock);
1212 list_add_tail(&fc->list, &fcoe_hostlist);
1213 write_unlock_bh(&fcoe_hostlist_lock);
1214 }
1215 return 0;
1216}
1217EXPORT_SYMBOL_GPL(fcoe_hostlist_add);
1218
Robert Love34f42a02009-02-27 10:55:45 -08001219/**
1220 * fcoe_hostlist_remove() - remove a lport from lports list
Robert Love85b4aa42008-12-09 15:10:24 -08001221 * @lp: ptr to the fc_lport to badded
1222 *
1223 * Returns: 0 for success
1224 */
1225int fcoe_hostlist_remove(const struct fc_lport *lp)
1226{
1227 struct fcoe_softc *fc;
1228
1229 fc = fcoe_hostlist_lookup_softc(fcoe_netdev(lp));
1230 BUG_ON(!fc);
1231 write_lock_bh(&fcoe_hostlist_lock);
1232 list_del(&fc->list);
1233 write_unlock_bh(&fcoe_hostlist_lock);
1234
1235 return 0;
1236}
1237EXPORT_SYMBOL_GPL(fcoe_hostlist_remove);
1238
1239/**
Robert Love34f42a02009-02-27 10:55:45 -08001240 * fcoe_libfc_config() - sets up libfc related properties for lport
Robert Love85b4aa42008-12-09 15:10:24 -08001241 * @lp: ptr to the fc_lport
1242 * @tt: libfc function template
1243 *
1244 * Returns : 0 for success
Robert Love34f42a02009-02-27 10:55:45 -08001245 */
Robert Love85b4aa42008-12-09 15:10:24 -08001246int fcoe_libfc_config(struct fc_lport *lp, struct libfc_function_template *tt)
1247{
1248 /* Set the function pointers set by the LLDD */
1249 memcpy(&lp->tt, tt, sizeof(*tt));
1250 if (fc_fcp_init(lp))
1251 return -ENOMEM;
1252 fc_exch_init(lp);
1253 fc_elsct_init(lp);
1254 fc_lport_init(lp);
1255 fc_rport_init(lp);
1256 fc_disc_init(lp);
1257
1258 return 0;
1259}
1260EXPORT_SYMBOL_GPL(fcoe_libfc_config);
1261
1262/**
Robert Love34f42a02009-02-27 10:55:45 -08001263 * fcoe_init() - fcoe module loading initialization
Robert Love85b4aa42008-12-09 15:10:24 -08001264 *
1265 * Initialization routine
1266 * 1. Will create fc transport software structure
1267 * 2. initialize the link list of port information structure
1268 *
1269 * Returns 0 on success, negative on failure
Robert Love34f42a02009-02-27 10:55:45 -08001270 */
Robert Love85b4aa42008-12-09 15:10:24 -08001271static int __init fcoe_init(void)
1272{
Robert Love38eccab2009-03-17 11:41:30 -07001273 unsigned int cpu;
Robert Love85b4aa42008-12-09 15:10:24 -08001274 struct fcoe_percpu_s *p;
1275
Robert Love85b4aa42008-12-09 15:10:24 -08001276 INIT_LIST_HEAD(&fcoe_hostlist);
1277 rwlock_init(&fcoe_hostlist_lock);
1278
Robert Love38eccab2009-03-17 11:41:30 -07001279 for_each_possible_cpu(cpu) {
Robert Love5e5e92d2009-03-17 11:41:35 -07001280 p = &per_cpu(fcoe_percpu, cpu);
Robert Love38eccab2009-03-17 11:41:30 -07001281 skb_queue_head_init(&p->fcoe_rx_list);
1282 }
1283
Robert Love85b4aa42008-12-09 15:10:24 -08001284 /*
1285 * initialize per CPU interrupt thread
1286 */
1287 for_each_online_cpu(cpu) {
Robert Love5e5e92d2009-03-17 11:41:35 -07001288 p = &per_cpu(fcoe_percpu, cpu);
1289 p->thread = kthread_create(fcoe_percpu_receive_thread,
1290 (void *)p, "fcoethread/%d", cpu);
Robert Love85b4aa42008-12-09 15:10:24 -08001291
Robert Love5e5e92d2009-03-17 11:41:35 -07001292 /*
1293 * If there is no error then bind the thread to the CPU
1294 * and wake it up.
1295 */
1296 if (!IS_ERR(p->thread)) {
1297 kthread_bind(p->thread, cpu);
1298 wake_up_process(p->thread);
1299 } else {
1300 p->thread = NULL;
Robert Love85b4aa42008-12-09 15:10:24 -08001301 }
1302 }
1303
1304 /*
1305 * setup link change notification
1306 */
1307 fcoe_dev_setup();
1308
Robert Lovea468f322009-02-27 10:56:00 -08001309 setup_timer(&fcoe_timer, fcoe_watchdog, 0);
1310
1311 mod_timer(&fcoe_timer, jiffies + (10 * HZ));
Robert Love85b4aa42008-12-09 15:10:24 -08001312
1313 /* initiatlize the fcoe transport */
1314 fcoe_transport_init();
1315
1316 fcoe_sw_init();
1317
1318 return 0;
1319}
1320module_init(fcoe_init);
1321
1322/**
Robert Love34f42a02009-02-27 10:55:45 -08001323 * fcoe_exit() - fcoe module unloading cleanup
Robert Love85b4aa42008-12-09 15:10:24 -08001324 *
1325 * Returns 0 on success, negative on failure
Robert Love34f42a02009-02-27 10:55:45 -08001326 */
Robert Love85b4aa42008-12-09 15:10:24 -08001327static void __exit fcoe_exit(void)
1328{
Robert Love5e5e92d2009-03-17 11:41:35 -07001329 unsigned int cpu;
Robert Love85b4aa42008-12-09 15:10:24 -08001330 struct fcoe_softc *fc, *tmp;
1331 struct fcoe_percpu_s *p;
1332 struct sk_buff *skb;
1333
Robert Love85b4aa42008-12-09 15:10:24 -08001334 fcoe_dev_cleanup();
1335
Robert Love582b45b2009-03-31 15:51:50 -07001336 /* Stop the timer */
Robert Love85b4aa42008-12-09 15:10:24 -08001337 del_timer_sync(&fcoe_timer);
1338
Robert Loveb2ab99c2009-02-27 10:55:50 -08001339 /* releases the associated fcoe transport for each lport */
Robert Love85b4aa42008-12-09 15:10:24 -08001340 list_for_each_entry_safe(fc, tmp, &fcoe_hostlist, list)
1341 fcoe_transport_release(fc->real_dev);
1342
Robert Love5e5e92d2009-03-17 11:41:35 -07001343 for_each_possible_cpu(cpu) {
1344 p = &per_cpu(fcoe_percpu, cpu);
1345 if (p->thread) {
1346 kthread_stop(p->thread);
Robert Love85b4aa42008-12-09 15:10:24 -08001347 spin_lock_bh(&p->fcoe_rx_list.lock);
1348 while ((skb = __skb_dequeue(&p->fcoe_rx_list)) != NULL)
1349 kfree_skb(skb);
1350 spin_unlock_bh(&p->fcoe_rx_list.lock);
Robert Love5e5e92d2009-03-17 11:41:35 -07001351 if (p->crc_eof_page)
1352 put_page(p->crc_eof_page);
Robert Love85b4aa42008-12-09 15:10:24 -08001353 }
1354 }
1355
1356 /* remove sw trasnport */
1357 fcoe_sw_exit();
1358
1359 /* detach the transport */
1360 fcoe_transport_exit();
1361}
1362module_exit(fcoe_exit);