Commit | Line | Data |
---|---|---|
85b4aa49 RL |
1 | /* |
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> | |
85b4aa49 RL |
47 | |
48 | static int debug_fcoe; | |
49 | ||
50 | #define FCOE_MAX_QUEUE_DEPTH 256 | |
c826a314 | 51 | #define FCOE_LOW_QUEUE_DEPTH 32 |
85b4aa49 RL |
52 | |
53 | /* destination address mode */ | |
54 | #define FCOE_GW_ADDR_MODE 0x00 | |
55 | #define FCOE_FCOUI_ADDR_MODE 0x01 | |
56 | ||
57 | #define FCOE_WORD_TO_BYTE 4 | |
58 | ||
59 | MODULE_AUTHOR("Open-FCoE.org"); | |
60 | MODULE_DESCRIPTION("FCoE"); | |
61 | MODULE_LICENSE("GPL"); | |
62 | ||
63 | /* fcoe host list */ | |
64 | LIST_HEAD(fcoe_hostlist); | |
65 | DEFINE_RWLOCK(fcoe_hostlist_lock); | |
66 | DEFINE_TIMER(fcoe_timer, NULL, 0, 0); | |
5e5e92df | 67 | DEFINE_PER_CPU(struct fcoe_percpu_s, fcoe_percpu); |
85b4aa49 RL |
68 | |
69 | ||
70 | /* Function Prototyes */ | |
71 | static int fcoe_check_wait_queue(struct fc_lport *); | |
85b4aa49 | 72 | static void fcoe_recv_flogi(struct fcoe_softc *, struct fc_frame *, u8 *); |
85b4aa49 RL |
73 | static int fcoe_device_notification(struct notifier_block *, ulong, void *); |
74 | static void fcoe_dev_setup(void); | |
75 | static void fcoe_dev_cleanup(void); | |
76 | ||
77 | /* notification function from net device */ | |
78 | static struct notifier_block fcoe_notifier = { | |
79 | .notifier_call = fcoe_device_notification, | |
80 | }; | |
81 | ||
8976f424 RL |
82 | /** |
83 | * fcoe_percpu_thread_create() - Create a receive thread for an online cpu | |
84 | * @cpu: cpu index for the online cpu | |
85 | */ | |
86 | static void fcoe_percpu_thread_create(unsigned int cpu) | |
87 | { | |
88 | struct fcoe_percpu_s *p; | |
89 | struct task_struct *thread; | |
90 | ||
91 | p = &per_cpu(fcoe_percpu, cpu); | |
92 | ||
93 | thread = kthread_create(fcoe_percpu_receive_thread, | |
94 | (void *)p, "fcoethread/%d", cpu); | |
95 | ||
96 | if (likely(!IS_ERR(p->thread))) { | |
97 | kthread_bind(thread, cpu); | |
98 | wake_up_process(thread); | |
99 | ||
100 | spin_lock_bh(&p->fcoe_rx_list.lock); | |
101 | p->thread = thread; | |
102 | spin_unlock_bh(&p->fcoe_rx_list.lock); | |
103 | } | |
104 | } | |
105 | ||
106 | /** | |
107 | * fcoe_percpu_thread_destroy() - removes the rx thread for the given cpu | |
108 | * @cpu: cpu index the rx thread is to be removed | |
109 | * | |
110 | * Destroys a per-CPU Rx thread. Any pending skbs are moved to the | |
111 | * current CPU's Rx thread. If the thread being destroyed is bound to | |
112 | * the CPU processing this context the skbs will be freed. | |
113 | */ | |
114 | static void fcoe_percpu_thread_destroy(unsigned int cpu) | |
115 | { | |
116 | struct fcoe_percpu_s *p; | |
117 | struct task_struct *thread; | |
118 | struct page *crc_eof; | |
119 | struct sk_buff *skb; | |
120 | #ifdef CONFIG_SMP | |
121 | struct fcoe_percpu_s *p0; | |
122 | unsigned targ_cpu = smp_processor_id(); | |
123 | #endif /* CONFIG_SMP */ | |
124 | ||
125 | printk(KERN_DEBUG "fcoe: Destroying receive thread for CPU %d\n", cpu); | |
126 | ||
127 | /* Prevent any new skbs from being queued for this CPU. */ | |
128 | p = &per_cpu(fcoe_percpu, cpu); | |
129 | spin_lock_bh(&p->fcoe_rx_list.lock); | |
130 | thread = p->thread; | |
131 | p->thread = NULL; | |
132 | crc_eof = p->crc_eof_page; | |
133 | p->crc_eof_page = NULL; | |
134 | p->crc_eof_offset = 0; | |
135 | spin_unlock_bh(&p->fcoe_rx_list.lock); | |
136 | ||
137 | #ifdef CONFIG_SMP | |
138 | /* | |
139 | * Don't bother moving the skb's if this context is running | |
140 | * on the same CPU that is having its thread destroyed. This | |
141 | * can easily happen when the module is removed. | |
142 | */ | |
143 | if (cpu != targ_cpu) { | |
144 | p0 = &per_cpu(fcoe_percpu, targ_cpu); | |
145 | spin_lock_bh(&p0->fcoe_rx_list.lock); | |
146 | if (p0->thread) { | |
147 | FC_DBG("Moving frames from CPU %d to CPU %d\n", | |
148 | cpu, targ_cpu); | |
149 | ||
150 | while ((skb = __skb_dequeue(&p->fcoe_rx_list)) != NULL) | |
151 | __skb_queue_tail(&p0->fcoe_rx_list, skb); | |
152 | spin_unlock_bh(&p0->fcoe_rx_list.lock); | |
153 | } else { | |
154 | /* | |
155 | * The targeted CPU is not initialized and cannot accept | |
156 | * new skbs. Unlock the targeted CPU and drop the skbs | |
157 | * on the CPU that is going offline. | |
158 | */ | |
159 | while ((skb = __skb_dequeue(&p->fcoe_rx_list)) != NULL) | |
160 | kfree_skb(skb); | |
161 | spin_unlock_bh(&p0->fcoe_rx_list.lock); | |
162 | } | |
163 | } else { | |
164 | /* | |
165 | * This scenario occurs when the module is being removed | |
166 | * and all threads are being destroyed. skbs will continue | |
167 | * to be shifted from the CPU thread that is being removed | |
168 | * to the CPU thread associated with the CPU that is processing | |
169 | * the module removal. Once there is only one CPU Rx thread it | |
170 | * will reach this case and we will drop all skbs and later | |
171 | * stop the thread. | |
172 | */ | |
173 | spin_lock_bh(&p->fcoe_rx_list.lock); | |
174 | while ((skb = __skb_dequeue(&p->fcoe_rx_list)) != NULL) | |
175 | kfree_skb(skb); | |
176 | spin_unlock_bh(&p->fcoe_rx_list.lock); | |
177 | } | |
178 | #else | |
179 | /* | |
180 | * This a non-SMP scenario where the singluar Rx thread is | |
181 | * being removed. Free all skbs and stop the thread. | |
182 | */ | |
183 | spin_lock_bh(&p->fcoe_rx_list.lock); | |
184 | while ((skb = __skb_dequeue(&p->fcoe_rx_list)) != NULL) | |
185 | kfree_skb(skb); | |
186 | spin_unlock_bh(&p->fcoe_rx_list.lock); | |
187 | #endif | |
188 | ||
189 | if (thread) | |
190 | kthread_stop(thread); | |
191 | ||
192 | if (crc_eof) | |
193 | put_page(crc_eof); | |
194 | } | |
195 | ||
196 | /** | |
197 | * fcoe_cpu_callback() - fcoe cpu hotplug event callback | |
198 | * @nfb: callback data block | |
199 | * @action: event triggering the callback | |
200 | * @hcpu: index for the cpu of this event | |
201 | * | |
202 | * This creates or destroys per cpu data for fcoe | |
203 | * | |
204 | * Returns NOTIFY_OK always. | |
205 | */ | |
206 | static int fcoe_cpu_callback(struct notifier_block *nfb, | |
207 | unsigned long action, void *hcpu) | |
208 | { | |
209 | unsigned cpu = (unsigned long)hcpu; | |
210 | ||
211 | switch (action) { | |
212 | case CPU_ONLINE: | |
213 | case CPU_ONLINE_FROZEN: | |
214 | FC_DBG("CPU %x online: Create Rx thread\n", cpu); | |
215 | fcoe_percpu_thread_create(cpu); | |
216 | break; | |
217 | case CPU_DEAD: | |
218 | case CPU_DEAD_FROZEN: | |
219 | FC_DBG("CPU %x offline: Remove Rx thread\n", cpu); | |
220 | fcoe_percpu_thread_destroy(cpu); | |
221 | break; | |
222 | default: | |
223 | break; | |
224 | } | |
225 | return NOTIFY_OK; | |
226 | } | |
227 | ||
228 | static struct notifier_block fcoe_cpu_notifier = { | |
229 | .notifier_call = fcoe_cpu_callback, | |
230 | }; | |
231 | ||
85b4aa49 | 232 | /** |
34f42a07 | 233 | * fcoe_rcv() - this is the fcoe receive function called by NET_RX_SOFTIRQ |
85b4aa49 RL |
234 | * @skb: the receive skb |
235 | * @dev: associated net device | |
236 | * @ptype: context | |
237 | * @odldev: last device | |
238 | * | |
239 | * this function will receive the packet and build fc frame and pass it up | |
240 | * | |
241 | * Returns: 0 for success | |
34f42a07 | 242 | */ |
85b4aa49 RL |
243 | int fcoe_rcv(struct sk_buff *skb, struct net_device *dev, |
244 | struct packet_type *ptype, struct net_device *olddev) | |
245 | { | |
246 | struct fc_lport *lp; | |
247 | struct fcoe_rcv_info *fr; | |
248 | struct fcoe_softc *fc; | |
85b4aa49 | 249 | struct fc_frame_header *fh; |
85b4aa49 | 250 | struct fcoe_percpu_s *fps; |
38eccabd | 251 | unsigned short oxid; |
8976f424 | 252 | unsigned int cpu = 0; |
85b4aa49 RL |
253 | |
254 | fc = container_of(ptype, struct fcoe_softc, fcoe_packet_type); | |
255 | lp = fc->lp; | |
256 | if (unlikely(lp == NULL)) { | |
257 | FC_DBG("cannot find hba structure"); | |
258 | goto err2; | |
259 | } | |
260 | ||
261 | if (unlikely(debug_fcoe)) { | |
262 | FC_DBG("skb_info: len:%d data_len:%d head:%p data:%p tail:%p " | |
263 | "end:%p sum:%d dev:%s", skb->len, skb->data_len, | |
264 | skb->head, skb->data, skb_tail_pointer(skb), | |
265 | skb_end_pointer(skb), skb->csum, | |
266 | skb->dev ? skb->dev->name : "<NULL>"); | |
267 | ||
268 | } | |
269 | ||
270 | /* check for FCOE packet type */ | |
271 | if (unlikely(eth_hdr(skb)->h_proto != htons(ETH_P_FCOE))) { | |
272 | FC_DBG("wrong FC type frame"); | |
273 | goto err; | |
274 | } | |
275 | ||
276 | /* | |
277 | * Check for minimum frame length, and make sure required FCoE | |
278 | * and FC headers are pulled into the linear data area. | |
279 | */ | |
280 | if (unlikely((skb->len < FCOE_MIN_FRAME) || | |
281 | !pskb_may_pull(skb, FCOE_HEADER_LEN))) | |
282 | goto err; | |
283 | ||
284 | skb_set_transport_header(skb, sizeof(struct fcoe_hdr)); | |
285 | fh = (struct fc_frame_header *) skb_transport_header(skb); | |
286 | ||
287 | oxid = ntohs(fh->fh_ox_id); | |
288 | ||
289 | fr = fcoe_dev_from_skb(skb); | |
290 | fr->fr_dev = lp; | |
291 | fr->ptype = ptype; | |
5e5e92df | 292 | |
85b4aa49 RL |
293 | #ifdef CONFIG_SMP |
294 | /* | |
295 | * The incoming frame exchange id(oxid) is ANDed with num of online | |
8976f424 RL |
296 | * cpu bits to get cpu and then this cpu is used for selecting |
297 | * a per cpu kernel thread from fcoe_percpu. | |
85b4aa49 | 298 | */ |
8976f424 | 299 | cpu = oxid & (num_online_cpus() - 1); |
85b4aa49 | 300 | #endif |
5e5e92df | 301 | |
8976f424 | 302 | fps = &per_cpu(fcoe_percpu, cpu); |
85b4aa49 | 303 | spin_lock_bh(&fps->fcoe_rx_list.lock); |
8976f424 RL |
304 | if (unlikely(!fps->thread)) { |
305 | /* | |
306 | * The targeted CPU is not ready, let's target | |
307 | * the first CPU now. For non-SMP systems this | |
308 | * will check the same CPU twice. | |
309 | */ | |
310 | FC_DBG("CPU is online, but no receive thread ready " | |
311 | "for incoming skb- using first online CPU.\n"); | |
312 | ||
313 | spin_unlock_bh(&fps->fcoe_rx_list.lock); | |
314 | cpu = first_cpu(cpu_online_map); | |
315 | fps = &per_cpu(fcoe_percpu, cpu); | |
316 | spin_lock_bh(&fps->fcoe_rx_list.lock); | |
317 | if (!fps->thread) { | |
318 | spin_unlock_bh(&fps->fcoe_rx_list.lock); | |
319 | goto err; | |
320 | } | |
321 | } | |
322 | ||
323 | /* | |
324 | * We now have a valid CPU that we're targeting for | |
325 | * this skb. We also have this receive thread locked, | |
326 | * so we're free to queue skbs into it's queue. | |
327 | */ | |
85b4aa49 RL |
328 | __skb_queue_tail(&fps->fcoe_rx_list, skb); |
329 | if (fps->fcoe_rx_list.qlen == 1) | |
330 | wake_up_process(fps->thread); | |
331 | ||
332 | spin_unlock_bh(&fps->fcoe_rx_list.lock); | |
333 | ||
334 | return 0; | |
335 | err: | |
582b45bc | 336 | fc_lport_get_stats(lp)->ErrorFrames++; |
85b4aa49 RL |
337 | |
338 | err2: | |
339 | kfree_skb(skb); | |
340 | return -1; | |
341 | } | |
342 | EXPORT_SYMBOL_GPL(fcoe_rcv); | |
343 | ||
344 | /** | |
34f42a07 | 345 | * fcoe_start_io() - pass to netdev to start xmit for fcoe |
85b4aa49 RL |
346 | * @skb: the skb to be xmitted |
347 | * | |
348 | * Returns: 0 for success | |
34f42a07 | 349 | */ |
85b4aa49 RL |
350 | static inline int fcoe_start_io(struct sk_buff *skb) |
351 | { | |
352 | int rc; | |
353 | ||
354 | skb_get(skb); | |
355 | rc = dev_queue_xmit(skb); | |
356 | if (rc != 0) | |
357 | return rc; | |
358 | kfree_skb(skb); | |
359 | return 0; | |
360 | } | |
361 | ||
362 | /** | |
34f42a07 | 363 | * fcoe_get_paged_crc_eof() - in case we need alloc a page for crc_eof |
85b4aa49 RL |
364 | * @skb: the skb to be xmitted |
365 | * @tlen: total len | |
366 | * | |
367 | * Returns: 0 for success | |
34f42a07 | 368 | */ |
85b4aa49 RL |
369 | static int fcoe_get_paged_crc_eof(struct sk_buff *skb, int tlen) |
370 | { | |
371 | struct fcoe_percpu_s *fps; | |
372 | struct page *page; | |
85b4aa49 | 373 | |
5e5e92df | 374 | fps = &get_cpu_var(fcoe_percpu); |
85b4aa49 RL |
375 | page = fps->crc_eof_page; |
376 | if (!page) { | |
377 | page = alloc_page(GFP_ATOMIC); | |
378 | if (!page) { | |
5e5e92df | 379 | put_cpu_var(fcoe_percpu); |
85b4aa49 RL |
380 | return -ENOMEM; |
381 | } | |
382 | fps->crc_eof_page = page; | |
8976f424 | 383 | fps->crc_eof_offset = 0; |
85b4aa49 RL |
384 | } |
385 | ||
386 | get_page(page); | |
387 | skb_fill_page_desc(skb, skb_shinfo(skb)->nr_frags, page, | |
388 | fps->crc_eof_offset, tlen); | |
389 | skb->len += tlen; | |
390 | skb->data_len += tlen; | |
391 | skb->truesize += tlen; | |
392 | fps->crc_eof_offset += sizeof(struct fcoe_crc_eof); | |
393 | ||
394 | if (fps->crc_eof_offset >= PAGE_SIZE) { | |
395 | fps->crc_eof_page = NULL; | |
396 | fps->crc_eof_offset = 0; | |
397 | put_page(page); | |
398 | } | |
5e5e92df | 399 | put_cpu_var(fcoe_percpu); |
85b4aa49 RL |
400 | return 0; |
401 | } | |
402 | ||
403 | /** | |
34f42a07 | 404 | * fcoe_fc_crc() - calculates FC CRC in this fcoe skb |
85b4aa49 RL |
405 | * @fp: the fc_frame containg data to be checksummed |
406 | * | |
407 | * This uses crc32() to calculate the crc for fc frame | |
408 | * Return : 32 bit crc | |
34f42a07 | 409 | */ |
85b4aa49 RL |
410 | u32 fcoe_fc_crc(struct fc_frame *fp) |
411 | { | |
412 | struct sk_buff *skb = fp_skb(fp); | |
413 | struct skb_frag_struct *frag; | |
414 | unsigned char *data; | |
415 | unsigned long off, len, clen; | |
416 | u32 crc; | |
417 | unsigned i; | |
418 | ||
419 | crc = crc32(~0, skb->data, skb_headlen(skb)); | |
420 | ||
421 | for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) { | |
422 | frag = &skb_shinfo(skb)->frags[i]; | |
423 | off = frag->page_offset; | |
424 | len = frag->size; | |
425 | while (len > 0) { | |
426 | clen = min(len, PAGE_SIZE - (off & ~PAGE_MASK)); | |
427 | data = kmap_atomic(frag->page + (off >> PAGE_SHIFT), | |
428 | KM_SKB_DATA_SOFTIRQ); | |
429 | crc = crc32(crc, data + (off & ~PAGE_MASK), clen); | |
430 | kunmap_atomic(data, KM_SKB_DATA_SOFTIRQ); | |
431 | off += clen; | |
432 | len -= clen; | |
433 | } | |
434 | } | |
435 | return crc; | |
436 | } | |
437 | EXPORT_SYMBOL_GPL(fcoe_fc_crc); | |
438 | ||
439 | /** | |
34f42a07 | 440 | * fcoe_xmit() - FCoE frame transmit function |
85b4aa49 RL |
441 | * @lp: the associated local port |
442 | * @fp: the fc_frame to be transmitted | |
443 | * | |
444 | * Return : 0 for success | |
34f42a07 | 445 | */ |
85b4aa49 RL |
446 | int fcoe_xmit(struct fc_lport *lp, struct fc_frame *fp) |
447 | { | |
448 | int wlen, rc = 0; | |
449 | u32 crc; | |
450 | struct ethhdr *eh; | |
451 | struct fcoe_crc_eof *cp; | |
452 | struct sk_buff *skb; | |
453 | struct fcoe_dev_stats *stats; | |
454 | struct fc_frame_header *fh; | |
455 | unsigned int hlen; /* header length implies the version */ | |
456 | unsigned int tlen; /* trailer length */ | |
457 | unsigned int elen; /* eth header, may include vlan */ | |
458 | int flogi_in_progress = 0; | |
459 | struct fcoe_softc *fc; | |
460 | u8 sof, eof; | |
461 | struct fcoe_hdr *hp; | |
462 | ||
463 | WARN_ON((fr_len(fp) % sizeof(u32)) != 0); | |
464 | ||
fc47ff6b | 465 | fc = lport_priv(lp); |
85b4aa49 RL |
466 | /* |
467 | * if it is a flogi then we need to learn gw-addr | |
468 | * and my own fcid | |
469 | */ | |
470 | fh = fc_frame_header_get(fp); | |
471 | if (unlikely(fh->fh_r_ctl == FC_RCTL_ELS_REQ)) { | |
472 | if (fc_frame_payload_op(fp) == ELS_FLOGI) { | |
473 | fc->flogi_oxid = ntohs(fh->fh_ox_id); | |
474 | fc->address_mode = FCOE_FCOUI_ADDR_MODE; | |
475 | fc->flogi_progress = 1; | |
476 | flogi_in_progress = 1; | |
477 | } else if (fc->flogi_progress && ntoh24(fh->fh_s_id) != 0) { | |
478 | /* | |
479 | * Here we must've gotten an SID by accepting an FLOGI | |
480 | * from a point-to-point connection. Switch to using | |
481 | * the source mac based on the SID. The destination | |
482 | * MAC in this case would have been set by receving the | |
483 | * FLOGI. | |
484 | */ | |
485 | fc_fcoe_set_mac(fc->data_src_addr, fh->fh_s_id); | |
486 | fc->flogi_progress = 0; | |
487 | } | |
488 | } | |
489 | ||
490 | skb = fp_skb(fp); | |
491 | sof = fr_sof(fp); | |
492 | eof = fr_eof(fp); | |
493 | ||
494 | elen = (fc->real_dev->priv_flags & IFF_802_1Q_VLAN) ? | |
495 | sizeof(struct vlan_ethhdr) : sizeof(struct ethhdr); | |
496 | hlen = sizeof(struct fcoe_hdr); | |
497 | tlen = sizeof(struct fcoe_crc_eof); | |
498 | wlen = (skb->len - tlen + sizeof(crc)) / FCOE_WORD_TO_BYTE; | |
499 | ||
500 | /* crc offload */ | |
501 | if (likely(lp->crc_offload)) { | |
39ca9a06 | 502 | skb->ip_summed = CHECKSUM_PARTIAL; |
85b4aa49 RL |
503 | skb->csum_start = skb_headroom(skb); |
504 | skb->csum_offset = skb->len; | |
505 | crc = 0; | |
506 | } else { | |
507 | skb->ip_summed = CHECKSUM_NONE; | |
508 | crc = fcoe_fc_crc(fp); | |
509 | } | |
510 | ||
511 | /* copy fc crc and eof to the skb buff */ | |
512 | if (skb_is_nonlinear(skb)) { | |
513 | skb_frag_t *frag; | |
514 | if (fcoe_get_paged_crc_eof(skb, tlen)) { | |
e9041581 | 515 | kfree_skb(skb); |
85b4aa49 RL |
516 | return -ENOMEM; |
517 | } | |
518 | frag = &skb_shinfo(skb)->frags[skb_shinfo(skb)->nr_frags - 1]; | |
519 | cp = kmap_atomic(frag->page, KM_SKB_DATA_SOFTIRQ) | |
520 | + frag->page_offset; | |
521 | } else { | |
522 | cp = (struct fcoe_crc_eof *)skb_put(skb, tlen); | |
523 | } | |
524 | ||
525 | memset(cp, 0, sizeof(*cp)); | |
526 | cp->fcoe_eof = eof; | |
527 | cp->fcoe_crc32 = cpu_to_le32(~crc); | |
528 | ||
529 | if (skb_is_nonlinear(skb)) { | |
530 | kunmap_atomic(cp, KM_SKB_DATA_SOFTIRQ); | |
531 | cp = NULL; | |
532 | } | |
533 | ||
534 | /* adjust skb netowrk/transport offsets to match mac/fcoe/fc */ | |
535 | skb_push(skb, elen + hlen); | |
536 | skb_reset_mac_header(skb); | |
537 | skb_reset_network_header(skb); | |
538 | skb->mac_len = elen; | |
211c738d | 539 | skb->protocol = htons(ETH_P_FCOE); |
85b4aa49 RL |
540 | skb->dev = fc->real_dev; |
541 | ||
542 | /* fill up mac and fcoe headers */ | |
543 | eh = eth_hdr(skb); | |
544 | eh->h_proto = htons(ETH_P_FCOE); | |
545 | if (fc->address_mode == FCOE_FCOUI_ADDR_MODE) | |
546 | fc_fcoe_set_mac(eh->h_dest, fh->fh_d_id); | |
547 | else | |
548 | /* insert GW address */ | |
549 | memcpy(eh->h_dest, fc->dest_addr, ETH_ALEN); | |
550 | ||
551 | if (unlikely(flogi_in_progress)) | |
552 | memcpy(eh->h_source, fc->ctl_src_addr, ETH_ALEN); | |
553 | else | |
554 | memcpy(eh->h_source, fc->data_src_addr, ETH_ALEN); | |
555 | ||
556 | hp = (struct fcoe_hdr *)(eh + 1); | |
557 | memset(hp, 0, sizeof(*hp)); | |
558 | if (FC_FCOE_VER) | |
559 | FC_FCOE_ENCAPS_VER(hp, FC_FCOE_VER); | |
560 | hp->fcoe_sof = sof; | |
561 | ||
39ca9a06 YZ |
562 | #ifdef NETIF_F_FSO |
563 | /* fcoe lso, mss is in max_payload which is non-zero for FCP data */ | |
564 | if (lp->seq_offload && fr_max_payload(fp)) { | |
565 | skb_shinfo(skb)->gso_type = SKB_GSO_FCOE; | |
566 | skb_shinfo(skb)->gso_size = fr_max_payload(fp); | |
567 | } else { | |
568 | skb_shinfo(skb)->gso_type = 0; | |
569 | skb_shinfo(skb)->gso_size = 0; | |
570 | } | |
571 | #endif | |
85b4aa49 | 572 | /* update tx stats: regardless if LLD fails */ |
582b45bc RL |
573 | stats = fc_lport_get_stats(lp); |
574 | stats->TxFrames++; | |
575 | stats->TxWords += wlen; | |
85b4aa49 RL |
576 | |
577 | /* send down to lld */ | |
578 | fr_dev(fp) = lp; | |
579 | if (fc->fcoe_pending_queue.qlen) | |
580 | rc = fcoe_check_wait_queue(lp); | |
581 | ||
582 | if (rc == 0) | |
583 | rc = fcoe_start_io(skb); | |
584 | ||
585 | if (rc) { | |
55c8bafb CL |
586 | spin_lock_bh(&fc->fcoe_pending_queue.lock); |
587 | __skb_queue_tail(&fc->fcoe_pending_queue, skb); | |
588 | spin_unlock_bh(&fc->fcoe_pending_queue.lock); | |
85b4aa49 | 589 | if (fc->fcoe_pending_queue.qlen > FCOE_MAX_QUEUE_DEPTH) |
bc0e17f6 | 590 | lp->qfull = 1; |
85b4aa49 RL |
591 | } |
592 | ||
593 | return 0; | |
594 | } | |
595 | EXPORT_SYMBOL_GPL(fcoe_xmit); | |
596 | ||
34f42a07 RL |
597 | /** |
598 | * fcoe_percpu_receive_thread() - recv thread per cpu | |
85b4aa49 RL |
599 | * @arg: ptr to the fcoe per cpu struct |
600 | * | |
601 | * Return: 0 for success | |
85b4aa49 RL |
602 | */ |
603 | int fcoe_percpu_receive_thread(void *arg) | |
604 | { | |
605 | struct fcoe_percpu_s *p = arg; | |
606 | u32 fr_len; | |
607 | struct fc_lport *lp; | |
608 | struct fcoe_rcv_info *fr; | |
609 | struct fcoe_dev_stats *stats; | |
610 | struct fc_frame_header *fh; | |
611 | struct sk_buff *skb; | |
612 | struct fcoe_crc_eof crc_eof; | |
613 | struct fc_frame *fp; | |
614 | u8 *mac = NULL; | |
615 | struct fcoe_softc *fc; | |
616 | struct fcoe_hdr *hp; | |
617 | ||
4469c195 | 618 | set_user_nice(current, -20); |
85b4aa49 RL |
619 | |
620 | while (!kthread_should_stop()) { | |
621 | ||
622 | spin_lock_bh(&p->fcoe_rx_list.lock); | |
623 | while ((skb = __skb_dequeue(&p->fcoe_rx_list)) == NULL) { | |
624 | set_current_state(TASK_INTERRUPTIBLE); | |
625 | spin_unlock_bh(&p->fcoe_rx_list.lock); | |
626 | schedule(); | |
627 | set_current_state(TASK_RUNNING); | |
628 | if (kthread_should_stop()) | |
629 | return 0; | |
630 | spin_lock_bh(&p->fcoe_rx_list.lock); | |
631 | } | |
632 | spin_unlock_bh(&p->fcoe_rx_list.lock); | |
633 | fr = fcoe_dev_from_skb(skb); | |
634 | lp = fr->fr_dev; | |
635 | if (unlikely(lp == NULL)) { | |
636 | FC_DBG("invalid HBA Structure"); | |
637 | kfree_skb(skb); | |
638 | continue; | |
639 | } | |
640 | ||
85b4aa49 RL |
641 | if (unlikely(debug_fcoe)) { |
642 | FC_DBG("skb_info: len:%d data_len:%d head:%p data:%p " | |
643 | "tail:%p end:%p sum:%d dev:%s", | |
644 | skb->len, skb->data_len, | |
645 | skb->head, skb->data, skb_tail_pointer(skb), | |
646 | skb_end_pointer(skb), skb->csum, | |
647 | skb->dev ? skb->dev->name : "<NULL>"); | |
648 | } | |
649 | ||
650 | /* | |
651 | * Save source MAC address before discarding header. | |
652 | */ | |
653 | fc = lport_priv(lp); | |
654 | if (unlikely(fc->flogi_progress)) | |
655 | mac = eth_hdr(skb)->h_source; | |
656 | ||
657 | if (skb_is_nonlinear(skb)) | |
658 | skb_linearize(skb); /* not ideal */ | |
659 | ||
660 | /* | |
661 | * Frame length checks and setting up the header pointers | |
662 | * was done in fcoe_rcv already. | |
663 | */ | |
664 | hp = (struct fcoe_hdr *) skb_network_header(skb); | |
665 | fh = (struct fc_frame_header *) skb_transport_header(skb); | |
666 | ||
582b45bc | 667 | stats = fc_lport_get_stats(lp); |
85b4aa49 | 668 | if (unlikely(FC_FCOE_DECAPS_VER(hp) != FC_FCOE_VER)) { |
582b45bc RL |
669 | if (stats->ErrorFrames < 5) |
670 | printk(KERN_WARNING "FCoE version " | |
671 | "mismatch: The frame has " | |
672 | "version %x, but the " | |
673 | "initiator supports version " | |
674 | "%x\n", FC_FCOE_DECAPS_VER(hp), | |
675 | FC_FCOE_VER); | |
676 | stats->ErrorFrames++; | |
85b4aa49 RL |
677 | kfree_skb(skb); |
678 | continue; | |
679 | } | |
680 | ||
681 | skb_pull(skb, sizeof(struct fcoe_hdr)); | |
682 | fr_len = skb->len - sizeof(struct fcoe_crc_eof); | |
683 | ||
582b45bc RL |
684 | stats->RxFrames++; |
685 | stats->RxWords += fr_len / FCOE_WORD_TO_BYTE; | |
85b4aa49 RL |
686 | |
687 | fp = (struct fc_frame *)skb; | |
688 | fc_frame_init(fp); | |
689 | fr_dev(fp) = lp; | |
690 | fr_sof(fp) = hp->fcoe_sof; | |
691 | ||
692 | /* Copy out the CRC and EOF trailer for access */ | |
693 | if (skb_copy_bits(skb, fr_len, &crc_eof, sizeof(crc_eof))) { | |
694 | kfree_skb(skb); | |
695 | continue; | |
696 | } | |
697 | fr_eof(fp) = crc_eof.fcoe_eof; | |
698 | fr_crc(fp) = crc_eof.fcoe_crc32; | |
699 | if (pskb_trim(skb, fr_len)) { | |
700 | kfree_skb(skb); | |
701 | continue; | |
702 | } | |
703 | ||
704 | /* | |
705 | * We only check CRC if no offload is available and if it is | |
706 | * it's solicited data, in which case, the FCP layer would | |
707 | * check it during the copy. | |
708 | */ | |
07c00ec4 | 709 | if (lp->crc_offload && skb->ip_summed == CHECKSUM_UNNECESSARY) |
85b4aa49 RL |
710 | fr_flags(fp) &= ~FCPHF_CRC_UNCHECKED; |
711 | else | |
712 | fr_flags(fp) |= FCPHF_CRC_UNCHECKED; | |
713 | ||
714 | fh = fc_frame_header_get(fp); | |
715 | if (fh->fh_r_ctl == FC_RCTL_DD_SOL_DATA && | |
716 | fh->fh_type == FC_TYPE_FCP) { | |
717 | fc_exch_recv(lp, lp->emp, fp); | |
718 | continue; | |
719 | } | |
720 | if (fr_flags(fp) & FCPHF_CRC_UNCHECKED) { | |
721 | if (le32_to_cpu(fr_crc(fp)) != | |
722 | ~crc32(~0, skb->data, fr_len)) { | |
723 | if (debug_fcoe || stats->InvalidCRCCount < 5) | |
724 | printk(KERN_WARNING "fcoe: dropping " | |
725 | "frame with CRC error\n"); | |
726 | stats->InvalidCRCCount++; | |
727 | stats->ErrorFrames++; | |
728 | fc_frame_free(fp); | |
729 | continue; | |
730 | } | |
731 | fr_flags(fp) &= ~FCPHF_CRC_UNCHECKED; | |
732 | } | |
733 | /* non flogi and non data exchanges are handled here */ | |
734 | if (unlikely(fc->flogi_progress)) | |
735 | fcoe_recv_flogi(fc, fp, mac); | |
736 | fc_exch_recv(lp, lp->emp, fp); | |
737 | } | |
738 | return 0; | |
739 | } | |
740 | ||
741 | /** | |
34f42a07 | 742 | * fcoe_recv_flogi() - flogi receive function |
85b4aa49 RL |
743 | * @fc: associated fcoe_softc |
744 | * @fp: the recieved frame | |
745 | * @sa: the source address of this flogi | |
746 | * | |
747 | * This is responsible to parse the flogi response and sets the corresponding | |
748 | * mac address for the initiator, eitehr OUI based or GW based. | |
749 | * | |
750 | * Returns: none | |
34f42a07 | 751 | */ |
85b4aa49 RL |
752 | static void fcoe_recv_flogi(struct fcoe_softc *fc, struct fc_frame *fp, u8 *sa) |
753 | { | |
754 | struct fc_frame_header *fh; | |
755 | u8 op; | |
756 | ||
757 | fh = fc_frame_header_get(fp); | |
758 | if (fh->fh_type != FC_TYPE_ELS) | |
759 | return; | |
760 | op = fc_frame_payload_op(fp); | |
761 | if (op == ELS_LS_ACC && fh->fh_r_ctl == FC_RCTL_ELS_REP && | |
762 | fc->flogi_oxid == ntohs(fh->fh_ox_id)) { | |
763 | /* | |
764 | * FLOGI accepted. | |
765 | * If the src mac addr is FC_OUI-based, then we mark the | |
766 | * address_mode flag to use FC_OUI-based Ethernet DA. | |
767 | * Otherwise we use the FCoE gateway addr | |
768 | */ | |
769 | if (!compare_ether_addr(sa, (u8[6]) FC_FCOE_FLOGI_MAC)) { | |
770 | fc->address_mode = FCOE_FCOUI_ADDR_MODE; | |
771 | } else { | |
772 | memcpy(fc->dest_addr, sa, ETH_ALEN); | |
773 | fc->address_mode = FCOE_GW_ADDR_MODE; | |
774 | } | |
775 | ||
776 | /* | |
777 | * Remove any previously-set unicast MAC filter. | |
778 | * Add secondary FCoE MAC address filter for our OUI. | |
779 | */ | |
780 | rtnl_lock(); | |
781 | if (compare_ether_addr(fc->data_src_addr, (u8[6]) { 0 })) | |
782 | dev_unicast_delete(fc->real_dev, fc->data_src_addr, | |
783 | ETH_ALEN); | |
784 | fc_fcoe_set_mac(fc->data_src_addr, fh->fh_d_id); | |
785 | dev_unicast_add(fc->real_dev, fc->data_src_addr, ETH_ALEN); | |
786 | rtnl_unlock(); | |
787 | ||
788 | fc->flogi_progress = 0; | |
789 | } else if (op == ELS_FLOGI && fh->fh_r_ctl == FC_RCTL_ELS_REQ && sa) { | |
790 | /* | |
791 | * Save source MAC for point-to-point responses. | |
792 | */ | |
793 | memcpy(fc->dest_addr, sa, ETH_ALEN); | |
794 | fc->address_mode = FCOE_GW_ADDR_MODE; | |
795 | } | |
796 | } | |
797 | ||
798 | /** | |
34f42a07 | 799 | * fcoe_watchdog() - fcoe timer callback |
85b4aa49 RL |
800 | * @vp: |
801 | * | |
bc0e17f6 | 802 | * This checks the pending queue length for fcoe and set lport qfull |
85b4aa49 RL |
803 | * if the FCOE_MAX_QUEUE_DEPTH is reached. This is done for all fc_lport on the |
804 | * fcoe_hostlist. | |
805 | * | |
806 | * Returns: 0 for success | |
34f42a07 | 807 | */ |
85b4aa49 RL |
808 | void fcoe_watchdog(ulong vp) |
809 | { | |
85b4aa49 | 810 | struct fcoe_softc *fc; |
85b4aa49 RL |
811 | |
812 | read_lock(&fcoe_hostlist_lock); | |
813 | list_for_each_entry(fc, &fcoe_hostlist, list) { | |
c826a314 VD |
814 | if (fc->lp) |
815 | fcoe_check_wait_queue(fc->lp); | |
85b4aa49 RL |
816 | } |
817 | read_unlock(&fcoe_hostlist_lock); | |
818 | ||
819 | fcoe_timer.expires = jiffies + (1 * HZ); | |
820 | add_timer(&fcoe_timer); | |
821 | } | |
822 | ||
823 | ||
824 | /** | |
34f42a07 | 825 | * fcoe_check_wait_queue() - put the skb into fcoe pending xmit queue |
85b4aa49 RL |
826 | * @lp: the fc_port for this skb |
827 | * @skb: the associated skb to be xmitted | |
828 | * | |
829 | * This empties the wait_queue, dequeue the head of the wait_queue queue | |
830 | * and calls fcoe_start_io() for each packet, if all skb have been | |
c826a314 VD |
831 | * transmitted, return qlen or -1 if a error occurs, then restore |
832 | * wait_queue and try again later. | |
85b4aa49 RL |
833 | * |
834 | * The wait_queue is used when the skb transmit fails. skb will go | |
835 | * in the wait_queue which will be emptied by the time function OR | |
836 | * by the next skb transmit. | |
837 | * | |
838 | * Returns: 0 for success | |
34f42a07 | 839 | */ |
85b4aa49 RL |
840 | static int fcoe_check_wait_queue(struct fc_lport *lp) |
841 | { | |
55c8bafb | 842 | struct fcoe_softc *fc = lport_priv(lp); |
85b4aa49 | 843 | struct sk_buff *skb; |
c826a314 | 844 | int rc = -1; |
85b4aa49 | 845 | |
85b4aa49 | 846 | spin_lock_bh(&fc->fcoe_pending_queue.lock); |
c826a314 VD |
847 | if (fc->fcoe_pending_queue_active) |
848 | goto out; | |
849 | fc->fcoe_pending_queue_active = 1; | |
55c8bafb CL |
850 | |
851 | while (fc->fcoe_pending_queue.qlen) { | |
852 | /* keep qlen > 0 until fcoe_start_io succeeds */ | |
853 | fc->fcoe_pending_queue.qlen++; | |
854 | skb = __skb_dequeue(&fc->fcoe_pending_queue); | |
855 | ||
856 | spin_unlock_bh(&fc->fcoe_pending_queue.lock); | |
857 | rc = fcoe_start_io(skb); | |
858 | spin_lock_bh(&fc->fcoe_pending_queue.lock); | |
859 | ||
860 | if (rc) { | |
861 | __skb_queue_head(&fc->fcoe_pending_queue, skb); | |
862 | /* undo temporary increment above */ | |
863 | fc->fcoe_pending_queue.qlen--; | |
864 | break; | |
85b4aa49 | 865 | } |
55c8bafb CL |
866 | /* undo temporary increment above */ |
867 | fc->fcoe_pending_queue.qlen--; | |
85b4aa49 | 868 | } |
55c8bafb CL |
869 | |
870 | if (fc->fcoe_pending_queue.qlen < FCOE_LOW_QUEUE_DEPTH) | |
871 | lp->qfull = 0; | |
c826a314 VD |
872 | fc->fcoe_pending_queue_active = 0; |
873 | rc = fc->fcoe_pending_queue.qlen; | |
874 | out: | |
85b4aa49 | 875 | spin_unlock_bh(&fc->fcoe_pending_queue.lock); |
c826a314 | 876 | return rc; |
85b4aa49 RL |
877 | } |
878 | ||
85b4aa49 | 879 | /** |
34f42a07 RL |
880 | * fcoe_dev_setup() - setup link change notification interface |
881 | */ | |
882 | static void fcoe_dev_setup() | |
85b4aa49 RL |
883 | { |
884 | /* | |
885 | * here setup a interface specific wd time to | |
886 | * monitor the link state | |
887 | */ | |
888 | register_netdevice_notifier(&fcoe_notifier); | |
889 | } | |
890 | ||
891 | /** | |
34f42a07 RL |
892 | * fcoe_dev_setup() - cleanup link change notification interface |
893 | */ | |
85b4aa49 RL |
894 | static void fcoe_dev_cleanup(void) |
895 | { | |
896 | unregister_netdevice_notifier(&fcoe_notifier); | |
897 | } | |
898 | ||
899 | /** | |
34f42a07 | 900 | * fcoe_device_notification() - netdev event notification callback |
85b4aa49 RL |
901 | * @notifier: context of the notification |
902 | * @event: type of event | |
903 | * @ptr: fixed array for output parsed ifname | |
904 | * | |
905 | * This function is called by the ethernet driver in case of link change event | |
906 | * | |
907 | * Returns: 0 for success | |
34f42a07 | 908 | */ |
85b4aa49 RL |
909 | static int fcoe_device_notification(struct notifier_block *notifier, |
910 | ulong event, void *ptr) | |
911 | { | |
912 | struct fc_lport *lp = NULL; | |
913 | struct net_device *real_dev = ptr; | |
914 | struct fcoe_softc *fc; | |
915 | struct fcoe_dev_stats *stats; | |
bc0e17f6 | 916 | u32 new_link_up; |
85b4aa49 RL |
917 | u32 mfs; |
918 | int rc = NOTIFY_OK; | |
919 | ||
920 | read_lock(&fcoe_hostlist_lock); | |
921 | list_for_each_entry(fc, &fcoe_hostlist, list) { | |
922 | if (fc->real_dev == real_dev) { | |
923 | lp = fc->lp; | |
924 | break; | |
925 | } | |
926 | } | |
927 | read_unlock(&fcoe_hostlist_lock); | |
928 | if (lp == NULL) { | |
929 | rc = NOTIFY_DONE; | |
930 | goto out; | |
931 | } | |
932 | ||
bc0e17f6 | 933 | new_link_up = lp->link_up; |
85b4aa49 RL |
934 | switch (event) { |
935 | case NETDEV_DOWN: | |
936 | case NETDEV_GOING_DOWN: | |
bc0e17f6 | 937 | new_link_up = 0; |
85b4aa49 RL |
938 | break; |
939 | case NETDEV_UP: | |
940 | case NETDEV_CHANGE: | |
bc0e17f6 | 941 | new_link_up = !fcoe_link_ok(lp); |
85b4aa49 RL |
942 | break; |
943 | case NETDEV_CHANGEMTU: | |
944 | mfs = fc->real_dev->mtu - | |
945 | (sizeof(struct fcoe_hdr) + | |
946 | sizeof(struct fcoe_crc_eof)); | |
947 | if (mfs >= FC_MIN_MAX_FRAME) | |
948 | fc_set_mfs(lp, mfs); | |
bc0e17f6 | 949 | new_link_up = !fcoe_link_ok(lp); |
85b4aa49 RL |
950 | break; |
951 | case NETDEV_REGISTER: | |
952 | break; | |
953 | default: | |
954 | FC_DBG("unknown event %ld call", event); | |
955 | } | |
bc0e17f6 VD |
956 | if (lp->link_up != new_link_up) { |
957 | if (new_link_up) | |
85b4aa49 RL |
958 | fc_linkup(lp); |
959 | else { | |
582b45bc RL |
960 | stats = fc_lport_get_stats(lp); |
961 | stats->LinkFailureCount++; | |
85b4aa49 RL |
962 | fc_linkdown(lp); |
963 | fcoe_clean_pending_queue(lp); | |
964 | } | |
965 | } | |
966 | out: | |
967 | return rc; | |
968 | } | |
969 | ||
970 | /** | |
34f42a07 | 971 | * fcoe_if_to_netdev() - parse a name buffer to get netdev |
85b4aa49 RL |
972 | * @ifname: fixed array for output parsed ifname |
973 | * @buffer: incoming buffer to be copied | |
974 | * | |
975 | * Returns: NULL or ptr to netdeive | |
34f42a07 | 976 | */ |
85b4aa49 RL |
977 | static struct net_device *fcoe_if_to_netdev(const char *buffer) |
978 | { | |
979 | char *cp; | |
980 | char ifname[IFNAMSIZ + 2]; | |
981 | ||
982 | if (buffer) { | |
983 | strlcpy(ifname, buffer, IFNAMSIZ); | |
984 | cp = ifname + strlen(ifname); | |
985 | while (--cp >= ifname && *cp == '\n') | |
986 | *cp = '\0'; | |
987 | return dev_get_by_name(&init_net, ifname); | |
988 | } | |
989 | return NULL; | |
990 | } | |
991 | ||
992 | /** | |
34f42a07 | 993 | * fcoe_netdev_to_module_owner() - finds out the nic drive moddule of the netdev |
85b4aa49 RL |
994 | * @netdev: the target netdev |
995 | * | |
996 | * Returns: ptr to the struct module, NULL for failure | |
34f42a07 | 997 | */ |
b2ab99c9 RL |
998 | static struct module * |
999 | fcoe_netdev_to_module_owner(const struct net_device *netdev) | |
85b4aa49 RL |
1000 | { |
1001 | struct device *dev; | |
1002 | ||
1003 | if (!netdev) | |
1004 | return NULL; | |
1005 | ||
1006 | dev = netdev->dev.parent; | |
1007 | if (!dev) | |
1008 | return NULL; | |
1009 | ||
1010 | if (!dev->driver) | |
1011 | return NULL; | |
1012 | ||
1013 | return dev->driver->owner; | |
1014 | } | |
1015 | ||
1016 | /** | |
34f42a07 | 1017 | * fcoe_ethdrv_get() - Hold the Ethernet driver |
85b4aa49 RL |
1018 | * @netdev: the target netdev |
1019 | * | |
34f42a07 RL |
1020 | * Holds the Ethernet driver module by try_module_get() for |
1021 | * the corresponding netdev. | |
1022 | * | |
85b4aa49 | 1023 | * Returns: 0 for succsss |
34f42a07 | 1024 | */ |
85b4aa49 RL |
1025 | static int fcoe_ethdrv_get(const struct net_device *netdev) |
1026 | { | |
1027 | struct module *owner; | |
1028 | ||
1029 | owner = fcoe_netdev_to_module_owner(netdev); | |
1030 | if (owner) { | |
56b854bb JB |
1031 | printk(KERN_DEBUG "fcoe:hold driver module %s for %s\n", |
1032 | module_name(owner), netdev->name); | |
85b4aa49 RL |
1033 | return try_module_get(owner); |
1034 | } | |
1035 | return -ENODEV; | |
1036 | } | |
1037 | ||
1038 | /** | |
34f42a07 | 1039 | * fcoe_ethdrv_put() - Release the Ethernet driver |
85b4aa49 RL |
1040 | * @netdev: the target netdev |
1041 | * | |
34f42a07 RL |
1042 | * Releases the Ethernet driver module by module_put for |
1043 | * the corresponding netdev. | |
1044 | * | |
85b4aa49 | 1045 | * Returns: 0 for succsss |
34f42a07 | 1046 | */ |
85b4aa49 RL |
1047 | static int fcoe_ethdrv_put(const struct net_device *netdev) |
1048 | { | |
1049 | struct module *owner; | |
1050 | ||
1051 | owner = fcoe_netdev_to_module_owner(netdev); | |
1052 | if (owner) { | |
56b854bb JB |
1053 | printk(KERN_DEBUG "fcoe:release driver module %s for %s\n", |
1054 | module_name(owner), netdev->name); | |
85b4aa49 RL |
1055 | module_put(owner); |
1056 | return 0; | |
1057 | } | |
1058 | return -ENODEV; | |
1059 | } | |
1060 | ||
1061 | /** | |
34f42a07 | 1062 | * fcoe_destroy() - handles the destroy from sysfs |
85b4aa49 RL |
1063 | * @buffer: expcted to be a eth if name |
1064 | * @kp: associated kernel param | |
1065 | * | |
1066 | * Returns: 0 for success | |
34f42a07 | 1067 | */ |
85b4aa49 RL |
1068 | static int fcoe_destroy(const char *buffer, struct kernel_param *kp) |
1069 | { | |
1070 | int rc; | |
1071 | struct net_device *netdev; | |
1072 | ||
1073 | netdev = fcoe_if_to_netdev(buffer); | |
1074 | if (!netdev) { | |
1075 | rc = -ENODEV; | |
1076 | goto out_nodev; | |
1077 | } | |
1078 | /* look for existing lport */ | |
1079 | if (!fcoe_hostlist_lookup(netdev)) { | |
1080 | rc = -ENODEV; | |
1081 | goto out_putdev; | |
1082 | } | |
5919a595 | 1083 | rc = fcoe_sw_destroy(netdev); |
85b4aa49 | 1084 | if (rc) { |
5919a595 | 1085 | printk(KERN_ERR "fcoe: fcoe_sw_destroy(%s) failed\n", |
85b4aa49 RL |
1086 | netdev->name); |
1087 | rc = -EIO; | |
1088 | goto out_putdev; | |
1089 | } | |
1090 | fcoe_ethdrv_put(netdev); | |
1091 | rc = 0; | |
1092 | out_putdev: | |
1093 | dev_put(netdev); | |
1094 | out_nodev: | |
1095 | return rc; | |
1096 | } | |
1097 | ||
1098 | /** | |
34f42a07 | 1099 | * fcoe_create() - Handles the create call from sysfs |
85b4aa49 RL |
1100 | * @buffer: expcted to be a eth if name |
1101 | * @kp: associated kernel param | |
1102 | * | |
1103 | * Returns: 0 for success | |
34f42a07 | 1104 | */ |
85b4aa49 RL |
1105 | static int fcoe_create(const char *buffer, struct kernel_param *kp) |
1106 | { | |
1107 | int rc; | |
1108 | struct net_device *netdev; | |
1109 | ||
1110 | netdev = fcoe_if_to_netdev(buffer); | |
1111 | if (!netdev) { | |
1112 | rc = -ENODEV; | |
1113 | goto out_nodev; | |
1114 | } | |
1115 | /* look for existing lport */ | |
1116 | if (fcoe_hostlist_lookup(netdev)) { | |
1117 | rc = -EEXIST; | |
1118 | goto out_putdev; | |
1119 | } | |
1120 | fcoe_ethdrv_get(netdev); | |
1121 | ||
5919a595 | 1122 | rc = fcoe_sw_create(netdev); |
85b4aa49 | 1123 | if (rc) { |
5919a595 | 1124 | printk(KERN_ERR "fcoe: fcoe_sw_create(%s) failed\n", |
85b4aa49 RL |
1125 | netdev->name); |
1126 | fcoe_ethdrv_put(netdev); | |
1127 | rc = -EIO; | |
1128 | goto out_putdev; | |
1129 | } | |
1130 | rc = 0; | |
1131 | out_putdev: | |
1132 | dev_put(netdev); | |
1133 | out_nodev: | |
1134 | return rc; | |
1135 | } | |
1136 | ||
1137 | module_param_call(create, fcoe_create, NULL, NULL, S_IWUSR); | |
1138 | __MODULE_PARM_TYPE(create, "string"); | |
1139 | MODULE_PARM_DESC(create, "Create fcoe port using net device passed in."); | |
1140 | module_param_call(destroy, fcoe_destroy, NULL, NULL, S_IWUSR); | |
1141 | __MODULE_PARM_TYPE(destroy, "string"); | |
1142 | MODULE_PARM_DESC(destroy, "Destroy fcoe port"); | |
1143 | ||
34f42a07 RL |
1144 | /** |
1145 | * fcoe_link_ok() - Check if link is ok for the fc_lport | |
85b4aa49 RL |
1146 | * @lp: ptr to the fc_lport |
1147 | * | |
1148 | * Any permanently-disqualifying conditions have been previously checked. | |
1149 | * This also updates the speed setting, which may change with link for 100/1000. | |
1150 | * | |
1151 | * This function should probably be checking for PAUSE support at some point | |
1152 | * in the future. Currently Per-priority-pause is not determinable using | |
1153 | * ethtool, so we shouldn't be restrictive until that problem is resolved. | |
1154 | * | |
1155 | * Returns: 0 if link is OK for use by FCoE. | |
1156 | * | |
1157 | */ | |
1158 | int fcoe_link_ok(struct fc_lport *lp) | |
1159 | { | |
fc47ff6b | 1160 | struct fcoe_softc *fc = lport_priv(lp); |
85b4aa49 RL |
1161 | struct net_device *dev = fc->real_dev; |
1162 | struct ethtool_cmd ecmd = { ETHTOOL_GSET }; | |
1163 | int rc = 0; | |
1164 | ||
1165 | if ((dev->flags & IFF_UP) && netif_carrier_ok(dev)) { | |
1166 | dev = fc->phys_dev; | |
1167 | if (dev->ethtool_ops->get_settings) { | |
1168 | dev->ethtool_ops->get_settings(dev, &ecmd); | |
1169 | lp->link_supported_speeds &= | |
1170 | ~(FC_PORTSPEED_1GBIT | FC_PORTSPEED_10GBIT); | |
1171 | if (ecmd.supported & (SUPPORTED_1000baseT_Half | | |
1172 | SUPPORTED_1000baseT_Full)) | |
1173 | lp->link_supported_speeds |= FC_PORTSPEED_1GBIT; | |
1174 | if (ecmd.supported & SUPPORTED_10000baseT_Full) | |
1175 | lp->link_supported_speeds |= | |
1176 | FC_PORTSPEED_10GBIT; | |
1177 | if (ecmd.speed == SPEED_1000) | |
1178 | lp->link_speed = FC_PORTSPEED_1GBIT; | |
1179 | if (ecmd.speed == SPEED_10000) | |
1180 | lp->link_speed = FC_PORTSPEED_10GBIT; | |
1181 | } | |
1182 | } else | |
1183 | rc = -1; | |
1184 | ||
1185 | return rc; | |
1186 | } | |
1187 | EXPORT_SYMBOL_GPL(fcoe_link_ok); | |
1188 | ||
34f42a07 RL |
1189 | /** |
1190 | * fcoe_percpu_clean() - Clear the pending skbs for an lport | |
85b4aa49 RL |
1191 | * @lp: the fc_lport |
1192 | */ | |
1193 | void fcoe_percpu_clean(struct fc_lport *lp) | |
1194 | { | |
85b4aa49 RL |
1195 | struct fcoe_percpu_s *pp; |
1196 | struct fcoe_rcv_info *fr; | |
1197 | struct sk_buff_head *list; | |
1198 | struct sk_buff *skb, *next; | |
1199 | struct sk_buff *head; | |
5e5e92df | 1200 | unsigned int cpu; |
85b4aa49 | 1201 | |
5e5e92df RL |
1202 | for_each_possible_cpu(cpu) { |
1203 | pp = &per_cpu(fcoe_percpu, cpu); | |
1204 | spin_lock_bh(&pp->fcoe_rx_list.lock); | |
1205 | list = &pp->fcoe_rx_list; | |
1206 | head = list->next; | |
1207 | for (skb = head; skb != (struct sk_buff *)list; | |
1208 | skb = next) { | |
1209 | next = skb->next; | |
1210 | fr = fcoe_dev_from_skb(skb); | |
1211 | if (fr->fr_dev == lp) { | |
1212 | __skb_unlink(skb, list); | |
1213 | kfree_skb(skb); | |
85b4aa49 | 1214 | } |
85b4aa49 | 1215 | } |
5e5e92df | 1216 | spin_unlock_bh(&pp->fcoe_rx_list.lock); |
85b4aa49 RL |
1217 | } |
1218 | } | |
1219 | EXPORT_SYMBOL_GPL(fcoe_percpu_clean); | |
1220 | ||
1221 | /** | |
34f42a07 | 1222 | * fcoe_clean_pending_queue() - Dequeue a skb and free it |
85b4aa49 RL |
1223 | * @lp: the corresponding fc_lport |
1224 | * | |
1225 | * Returns: none | |
34f42a07 | 1226 | */ |
85b4aa49 RL |
1227 | void fcoe_clean_pending_queue(struct fc_lport *lp) |
1228 | { | |
1229 | struct fcoe_softc *fc = lport_priv(lp); | |
1230 | struct sk_buff *skb; | |
1231 | ||
1232 | spin_lock_bh(&fc->fcoe_pending_queue.lock); | |
1233 | while ((skb = __skb_dequeue(&fc->fcoe_pending_queue)) != NULL) { | |
1234 | spin_unlock_bh(&fc->fcoe_pending_queue.lock); | |
1235 | kfree_skb(skb); | |
1236 | spin_lock_bh(&fc->fcoe_pending_queue.lock); | |
1237 | } | |
1238 | spin_unlock_bh(&fc->fcoe_pending_queue.lock); | |
1239 | } | |
1240 | EXPORT_SYMBOL_GPL(fcoe_clean_pending_queue); | |
1241 | ||
1242 | /** | |
34f42a07 | 1243 | * libfc_host_alloc() - Allocate a Scsi_Host with room for the fc_lport |
85b4aa49 RL |
1244 | * @sht: ptr to the scsi host templ |
1245 | * @priv_size: size of private data after fc_lport | |
1246 | * | |
1247 | * Returns: ptr to Scsi_Host | |
34f42a07 | 1248 | * TODO: to libfc? |
85b4aa49 | 1249 | */ |
b2ab99c9 RL |
1250 | static inline struct Scsi_Host * |
1251 | libfc_host_alloc(struct scsi_host_template *sht, int priv_size) | |
85b4aa49 RL |
1252 | { |
1253 | return scsi_host_alloc(sht, sizeof(struct fc_lport) + priv_size); | |
1254 | } | |
1255 | ||
1256 | /** | |
34f42a07 | 1257 | * fcoe_host_alloc() - Allocate a Scsi_Host with room for the fcoe_softc |
85b4aa49 RL |
1258 | * @sht: ptr to the scsi host templ |
1259 | * @priv_size: size of private data after fc_lport | |
1260 | * | |
1261 | * Returns: ptr to Scsi_Host | |
1262 | */ | |
1263 | struct Scsi_Host *fcoe_host_alloc(struct scsi_host_template *sht, int priv_size) | |
1264 | { | |
1265 | return libfc_host_alloc(sht, sizeof(struct fcoe_softc) + priv_size); | |
1266 | } | |
1267 | EXPORT_SYMBOL_GPL(fcoe_host_alloc); | |
1268 | ||
34f42a07 RL |
1269 | /** |
1270 | * fcoe_reset() - Resets the fcoe | |
85b4aa49 RL |
1271 | * @shost: shost the reset is from |
1272 | * | |
1273 | * Returns: always 0 | |
1274 | */ | |
1275 | int fcoe_reset(struct Scsi_Host *shost) | |
1276 | { | |
1277 | struct fc_lport *lport = shost_priv(shost); | |
1278 | fc_lport_reset(lport); | |
1279 | return 0; | |
1280 | } | |
1281 | EXPORT_SYMBOL_GPL(fcoe_reset); | |
1282 | ||
34f42a07 RL |
1283 | /** |
1284 | * fcoe_wwn_from_mac() - Converts 48-bit IEEE MAC address to 64-bit FC WWN. | |
85b4aa49 RL |
1285 | * @mac: mac address |
1286 | * @scheme: check port | |
1287 | * @port: port indicator for converting | |
1288 | * | |
1289 | * Returns: u64 fc world wide name | |
1290 | */ | |
1291 | u64 fcoe_wwn_from_mac(unsigned char mac[MAX_ADDR_LEN], | |
1292 | unsigned int scheme, unsigned int port) | |
1293 | { | |
1294 | u64 wwn; | |
1295 | u64 host_mac; | |
1296 | ||
1297 | /* The MAC is in NO, so flip only the low 48 bits */ | |
1298 | host_mac = ((u64) mac[0] << 40) | | |
1299 | ((u64) mac[1] << 32) | | |
1300 | ((u64) mac[2] << 24) | | |
1301 | ((u64) mac[3] << 16) | | |
1302 | ((u64) mac[4] << 8) | | |
1303 | (u64) mac[5]; | |
1304 | ||
1305 | WARN_ON(host_mac >= (1ULL << 48)); | |
1306 | wwn = host_mac | ((u64) scheme << 60); | |
1307 | switch (scheme) { | |
1308 | case 1: | |
1309 | WARN_ON(port != 0); | |
1310 | break; | |
1311 | case 2: | |
1312 | WARN_ON(port >= 0xfff); | |
1313 | wwn |= (u64) port << 48; | |
1314 | break; | |
1315 | default: | |
1316 | WARN_ON(1); | |
1317 | break; | |
1318 | } | |
1319 | ||
1320 | return wwn; | |
1321 | } | |
1322 | EXPORT_SYMBOL_GPL(fcoe_wwn_from_mac); | |
34f42a07 RL |
1323 | |
1324 | /** | |
1325 | * fcoe_hostlist_lookup_softc() - find the corresponding lport by a given device | |
85b4aa49 RL |
1326 | * @device: this is currently ptr to net_device |
1327 | * | |
1328 | * Returns: NULL or the located fcoe_softc | |
1329 | */ | |
b2ab99c9 RL |
1330 | static struct fcoe_softc * |
1331 | fcoe_hostlist_lookup_softc(const struct net_device *dev) | |
85b4aa49 RL |
1332 | { |
1333 | struct fcoe_softc *fc; | |
1334 | ||
1335 | read_lock(&fcoe_hostlist_lock); | |
1336 | list_for_each_entry(fc, &fcoe_hostlist, list) { | |
1337 | if (fc->real_dev == dev) { | |
1338 | read_unlock(&fcoe_hostlist_lock); | |
1339 | return fc; | |
1340 | } | |
1341 | } | |
1342 | read_unlock(&fcoe_hostlist_lock); | |
1343 | return NULL; | |
1344 | } | |
1345 | ||
34f42a07 RL |
1346 | /** |
1347 | * fcoe_hostlist_lookup() - Find the corresponding lport by netdev | |
85b4aa49 RL |
1348 | * @netdev: ptr to net_device |
1349 | * | |
1350 | * Returns: 0 for success | |
1351 | */ | |
1352 | struct fc_lport *fcoe_hostlist_lookup(const struct net_device *netdev) | |
1353 | { | |
1354 | struct fcoe_softc *fc; | |
1355 | ||
1356 | fc = fcoe_hostlist_lookup_softc(netdev); | |
1357 | ||
1358 | return (fc) ? fc->lp : NULL; | |
1359 | } | |
1360 | EXPORT_SYMBOL_GPL(fcoe_hostlist_lookup); | |
1361 | ||
34f42a07 RL |
1362 | /** |
1363 | * fcoe_hostlist_add() - Add a lport to lports list | |
85b4aa49 RL |
1364 | * @lp: ptr to the fc_lport to badded |
1365 | * | |
1366 | * Returns: 0 for success | |
1367 | */ | |
1368 | int fcoe_hostlist_add(const struct fc_lport *lp) | |
1369 | { | |
1370 | struct fcoe_softc *fc; | |
1371 | ||
1372 | fc = fcoe_hostlist_lookup_softc(fcoe_netdev(lp)); | |
1373 | if (!fc) { | |
fc47ff6b | 1374 | fc = lport_priv(lp); |
85b4aa49 RL |
1375 | write_lock_bh(&fcoe_hostlist_lock); |
1376 | list_add_tail(&fc->list, &fcoe_hostlist); | |
1377 | write_unlock_bh(&fcoe_hostlist_lock); | |
1378 | } | |
1379 | return 0; | |
1380 | } | |
1381 | EXPORT_SYMBOL_GPL(fcoe_hostlist_add); | |
1382 | ||
34f42a07 RL |
1383 | /** |
1384 | * fcoe_hostlist_remove() - remove a lport from lports list | |
85b4aa49 RL |
1385 | * @lp: ptr to the fc_lport to badded |
1386 | * | |
1387 | * Returns: 0 for success | |
1388 | */ | |
1389 | int fcoe_hostlist_remove(const struct fc_lport *lp) | |
1390 | { | |
1391 | struct fcoe_softc *fc; | |
1392 | ||
1393 | fc = fcoe_hostlist_lookup_softc(fcoe_netdev(lp)); | |
1394 | BUG_ON(!fc); | |
1395 | write_lock_bh(&fcoe_hostlist_lock); | |
1396 | list_del(&fc->list); | |
1397 | write_unlock_bh(&fcoe_hostlist_lock); | |
1398 | ||
1399 | return 0; | |
1400 | } | |
1401 | EXPORT_SYMBOL_GPL(fcoe_hostlist_remove); | |
1402 | ||
1403 | /** | |
34f42a07 | 1404 | * fcoe_libfc_config() - sets up libfc related properties for lport |
85b4aa49 RL |
1405 | * @lp: ptr to the fc_lport |
1406 | * @tt: libfc function template | |
1407 | * | |
1408 | * Returns : 0 for success | |
34f42a07 | 1409 | */ |
85b4aa49 RL |
1410 | int fcoe_libfc_config(struct fc_lport *lp, struct libfc_function_template *tt) |
1411 | { | |
1412 | /* Set the function pointers set by the LLDD */ | |
1413 | memcpy(&lp->tt, tt, sizeof(*tt)); | |
1414 | if (fc_fcp_init(lp)) | |
1415 | return -ENOMEM; | |
1416 | fc_exch_init(lp); | |
1417 | fc_elsct_init(lp); | |
1418 | fc_lport_init(lp); | |
1419 | fc_rport_init(lp); | |
1420 | fc_disc_init(lp); | |
1421 | ||
1422 | return 0; | |
1423 | } | |
1424 | EXPORT_SYMBOL_GPL(fcoe_libfc_config); | |
1425 | ||
1426 | /** | |
34f42a07 | 1427 | * fcoe_init() - fcoe module loading initialization |
85b4aa49 | 1428 | * |
85b4aa49 | 1429 | * Returns 0 on success, negative on failure |
34f42a07 | 1430 | */ |
85b4aa49 RL |
1431 | static int __init fcoe_init(void) |
1432 | { | |
38eccabd | 1433 | unsigned int cpu; |
8976f424 | 1434 | int rc = 0; |
85b4aa49 RL |
1435 | struct fcoe_percpu_s *p; |
1436 | ||
85b4aa49 RL |
1437 | INIT_LIST_HEAD(&fcoe_hostlist); |
1438 | rwlock_init(&fcoe_hostlist_lock); | |
1439 | ||
38eccabd | 1440 | for_each_possible_cpu(cpu) { |
5e5e92df | 1441 | p = &per_cpu(fcoe_percpu, cpu); |
38eccabd RL |
1442 | skb_queue_head_init(&p->fcoe_rx_list); |
1443 | } | |
1444 | ||
8976f424 RL |
1445 | for_each_online_cpu(cpu) |
1446 | fcoe_percpu_thread_create(cpu); | |
85b4aa49 | 1447 | |
8976f424 RL |
1448 | /* Initialize per CPU interrupt thread */ |
1449 | rc = register_hotcpu_notifier(&fcoe_cpu_notifier); | |
1450 | if (rc) | |
1451 | goto out_free; | |
85b4aa49 | 1452 | |
8976f424 | 1453 | /* Setup link change notification */ |
85b4aa49 RL |
1454 | fcoe_dev_setup(); |
1455 | ||
a468f328 RL |
1456 | setup_timer(&fcoe_timer, fcoe_watchdog, 0); |
1457 | ||
1458 | mod_timer(&fcoe_timer, jiffies + (10 * HZ)); | |
85b4aa49 | 1459 | |
85b4aa49 RL |
1460 | fcoe_sw_init(); |
1461 | ||
1462 | return 0; | |
8976f424 RL |
1463 | |
1464 | out_free: | |
1465 | for_each_online_cpu(cpu) { | |
1466 | fcoe_percpu_thread_destroy(cpu); | |
1467 | } | |
1468 | ||
1469 | return rc; | |
85b4aa49 RL |
1470 | } |
1471 | module_init(fcoe_init); | |
1472 | ||
1473 | /** | |
34f42a07 | 1474 | * fcoe_exit() - fcoe module unloading cleanup |
85b4aa49 RL |
1475 | * |
1476 | * Returns 0 on success, negative on failure | |
34f42a07 | 1477 | */ |
85b4aa49 RL |
1478 | static void __exit fcoe_exit(void) |
1479 | { | |
5e5e92df | 1480 | unsigned int cpu; |
85b4aa49 | 1481 | struct fcoe_softc *fc, *tmp; |
85b4aa49 | 1482 | |
85b4aa49 RL |
1483 | fcoe_dev_cleanup(); |
1484 | ||
582b45bc | 1485 | /* Stop the timer */ |
85b4aa49 RL |
1486 | del_timer_sync(&fcoe_timer); |
1487 | ||
5919a595 | 1488 | /* releases the associated fcoe hosts */ |
85b4aa49 | 1489 | list_for_each_entry_safe(fc, tmp, &fcoe_hostlist, list) |
5919a595 | 1490 | fcoe_sw_destroy(fc->real_dev); |
85b4aa49 | 1491 | |
8976f424 RL |
1492 | unregister_hotcpu_notifier(&fcoe_cpu_notifier); |
1493 | ||
1494 | for_each_online_cpu(cpu) { | |
1495 | fcoe_percpu_thread_destroy(cpu); | |
85b4aa49 RL |
1496 | } |
1497 | ||
1498 | /* remove sw trasnport */ | |
1499 | fcoe_sw_exit(); | |
85b4aa49 RL |
1500 | } |
1501 | module_exit(fcoe_exit); |