Merge branch 'master' of git://oss.sgi.com/xfs/xfs into for-linus
[deliverable/linux.git] / drivers / scsi / fcoe / fcoe.c
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/spinlock.h>
23 #include <linux/netdevice.h>
24 #include <linux/etherdevice.h>
25 #include <linux/ethtool.h>
26 #include <linux/if_ether.h>
27 #include <linux/if_vlan.h>
28 #include <linux/crc32.h>
29 #include <linux/cpu.h>
30 #include <linux/fs.h>
31 #include <linux/sysfs.h>
32 #include <linux/ctype.h>
33 #include <scsi/scsi_tcq.h>
34 #include <scsi/scsicam.h>
35 #include <scsi/scsi_transport.h>
36 #include <scsi/scsi_transport_fc.h>
37 #include <net/rtnetlink.h>
38
39 #include <scsi/fc/fc_encaps.h>
40 #include <scsi/fc/fc_fip.h>
41
42 #include <scsi/libfc.h>
43 #include <scsi/fc_frame.h>
44 #include <scsi/libfcoe.h>
45
46 #include "fcoe.h"
47
48 static int debug_fcoe;
49
50 MODULE_AUTHOR("Open-FCoE.org");
51 MODULE_DESCRIPTION("FCoE");
52 MODULE_LICENSE("GPL v2");
53
54 /* fcoe host list */
55 LIST_HEAD(fcoe_hostlist);
56 DEFINE_RWLOCK(fcoe_hostlist_lock);
57 DEFINE_PER_CPU(struct fcoe_percpu_s, fcoe_percpu);
58
59 /* Function Prototypes */
60 static int fcoe_reset(struct Scsi_Host *shost);
61 static int fcoe_xmit(struct fc_lport *, struct fc_frame *);
62 static int fcoe_rcv(struct sk_buff *, struct net_device *,
63 struct packet_type *, struct net_device *);
64 static int fcoe_percpu_receive_thread(void *arg);
65 static void fcoe_clean_pending_queue(struct fc_lport *lp);
66 static void fcoe_percpu_clean(struct fc_lport *lp);
67 static int fcoe_link_ok(struct fc_lport *lp);
68
69 static struct fc_lport *fcoe_hostlist_lookup(const struct net_device *);
70 static int fcoe_hostlist_add(const struct fc_lport *);
71 static int fcoe_hostlist_remove(const struct fc_lport *);
72
73 static void fcoe_check_wait_queue(struct fc_lport *, struct sk_buff *);
74 static int fcoe_device_notification(struct notifier_block *, ulong, void *);
75 static void fcoe_dev_setup(void);
76 static void fcoe_dev_cleanup(void);
77
78 /* notification function from net device */
79 static struct notifier_block fcoe_notifier = {
80 .notifier_call = fcoe_device_notification,
81 };
82
83 static struct scsi_transport_template *scsi_transport_fcoe_sw;
84
85 struct fc_function_template fcoe_transport_function = {
86 .show_host_node_name = 1,
87 .show_host_port_name = 1,
88 .show_host_supported_classes = 1,
89 .show_host_supported_fc4s = 1,
90 .show_host_active_fc4s = 1,
91 .show_host_maxframe_size = 1,
92
93 .show_host_port_id = 1,
94 .show_host_supported_speeds = 1,
95 .get_host_speed = fc_get_host_speed,
96 .show_host_speed = 1,
97 .show_host_port_type = 1,
98 .get_host_port_state = fc_get_host_port_state,
99 .show_host_port_state = 1,
100 .show_host_symbolic_name = 1,
101
102 .dd_fcrport_size = sizeof(struct fc_rport_libfc_priv),
103 .show_rport_maxframe_size = 1,
104 .show_rport_supported_classes = 1,
105
106 .show_host_fabric_name = 1,
107 .show_starget_node_name = 1,
108 .show_starget_port_name = 1,
109 .show_starget_port_id = 1,
110 .set_rport_dev_loss_tmo = fc_set_rport_loss_tmo,
111 .show_rport_dev_loss_tmo = 1,
112 .get_fc_host_stats = fc_get_host_stats,
113 .issue_fc_host_lip = fcoe_reset,
114
115 .terminate_rport_io = fc_rport_terminate_io,
116 };
117
118 static struct scsi_host_template fcoe_shost_template = {
119 .module = THIS_MODULE,
120 .name = "FCoE Driver",
121 .proc_name = FCOE_NAME,
122 .queuecommand = fc_queuecommand,
123 .eh_abort_handler = fc_eh_abort,
124 .eh_device_reset_handler = fc_eh_device_reset,
125 .eh_host_reset_handler = fc_eh_host_reset,
126 .slave_alloc = fc_slave_alloc,
127 .change_queue_depth = fc_change_queue_depth,
128 .change_queue_type = fc_change_queue_type,
129 .this_id = -1,
130 .cmd_per_lun = 32,
131 .can_queue = FCOE_MAX_OUTSTANDING_COMMANDS,
132 .use_clustering = ENABLE_CLUSTERING,
133 .sg_tablesize = SG_ALL,
134 .max_sectors = 0xffff,
135 };
136
137 /**
138 * fcoe_lport_config() - sets up the fc_lport
139 * @lp: ptr to the fc_lport
140 *
141 * Returns: 0 for success
142 */
143 static int fcoe_lport_config(struct fc_lport *lp)
144 {
145 lp->link_up = 0;
146 lp->qfull = 0;
147 lp->max_retry_count = 3;
148 lp->max_rport_retry_count = 3;
149 lp->e_d_tov = 2 * 1000; /* FC-FS default */
150 lp->r_a_tov = 2 * 2 * 1000;
151 lp->service_params = (FCP_SPPF_INIT_FCN | FCP_SPPF_RD_XRDY_DIS |
152 FCP_SPPF_RETRY | FCP_SPPF_CONF_COMPL);
153
154 fc_lport_init_stats(lp);
155
156 /* lport fc_lport related configuration */
157 fc_lport_config(lp);
158
159 /* offload related configuration */
160 lp->crc_offload = 0;
161 lp->seq_offload = 0;
162 lp->lro_enabled = 0;
163 lp->lro_xid = 0;
164 lp->lso_max = 0;
165
166 return 0;
167 }
168
169 /**
170 * fcoe_queue_timer() - fcoe queue timer
171 * @lp: the fc_lport pointer
172 *
173 * Calls fcoe_check_wait_queue on timeout
174 *
175 */
176 static void fcoe_queue_timer(ulong lp)
177 {
178 fcoe_check_wait_queue((struct fc_lport *)lp, NULL);
179 }
180
181 /**
182 * fcoe_netdev_config() - Set up netdev for SW FCoE
183 * @lp : ptr to the fc_lport
184 * @netdev : ptr to the associated netdevice struct
185 *
186 * Must be called after fcoe_lport_config() as it will use lport mutex
187 *
188 * Returns : 0 for success
189 */
190 static int fcoe_netdev_config(struct fc_lport *lp, struct net_device *netdev)
191 {
192 u32 mfs;
193 u64 wwnn, wwpn;
194 struct fcoe_softc *fc;
195 u8 flogi_maddr[ETH_ALEN];
196
197 /* Setup lport private data to point to fcoe softc */
198 fc = lport_priv(lp);
199 fc->ctlr.lp = lp;
200 fc->real_dev = netdev;
201 fc->phys_dev = netdev;
202
203 /* Require support for get_pauseparam ethtool op. */
204 if (netdev->priv_flags & IFF_802_1Q_VLAN)
205 fc->phys_dev = vlan_dev_real_dev(netdev);
206
207 /* Do not support for bonding device */
208 if ((fc->real_dev->priv_flags & IFF_MASTER_ALB) ||
209 (fc->real_dev->priv_flags & IFF_SLAVE_INACTIVE) ||
210 (fc->real_dev->priv_flags & IFF_MASTER_8023AD)) {
211 return -EOPNOTSUPP;
212 }
213
214 /*
215 * Determine max frame size based on underlying device and optional
216 * user-configured limit. If the MFS is too low, fcoe_link_ok()
217 * will return 0, so do this first.
218 */
219 mfs = fc->real_dev->mtu - (sizeof(struct fcoe_hdr) +
220 sizeof(struct fcoe_crc_eof));
221 if (fc_set_mfs(lp, mfs))
222 return -EINVAL;
223
224 /* offload features support */
225 if (fc->real_dev->features & NETIF_F_SG)
226 lp->sg_supp = 1;
227
228 #ifdef NETIF_F_FCOE_CRC
229 if (netdev->features & NETIF_F_FCOE_CRC) {
230 lp->crc_offload = 1;
231 printk(KERN_DEBUG "fcoe:%s supports FCCRC offload\n",
232 netdev->name);
233 }
234 #endif
235 #ifdef NETIF_F_FSO
236 if (netdev->features & NETIF_F_FSO) {
237 lp->seq_offload = 1;
238 lp->lso_max = netdev->gso_max_size;
239 printk(KERN_DEBUG "fcoe:%s supports LSO for max len 0x%x\n",
240 netdev->name, lp->lso_max);
241 }
242 #endif
243 if (netdev->fcoe_ddp_xid) {
244 lp->lro_enabled = 1;
245 lp->lro_xid = netdev->fcoe_ddp_xid;
246 printk(KERN_DEBUG "fcoe:%s supports LRO for max xid 0x%x\n",
247 netdev->name, lp->lro_xid);
248 }
249 skb_queue_head_init(&fc->fcoe_pending_queue);
250 fc->fcoe_pending_queue_active = 0;
251 setup_timer(&fc->timer, fcoe_queue_timer, (unsigned long)lp);
252
253 /* setup Source Mac Address */
254 memcpy(fc->ctlr.ctl_src_addr, fc->real_dev->dev_addr,
255 fc->real_dev->addr_len);
256
257 wwnn = fcoe_wwn_from_mac(fc->real_dev->dev_addr, 1, 0);
258 fc_set_wwnn(lp, wwnn);
259 /* XXX - 3rd arg needs to be vlan id */
260 wwpn = fcoe_wwn_from_mac(fc->real_dev->dev_addr, 2, 0);
261 fc_set_wwpn(lp, wwpn);
262
263 /*
264 * Add FCoE MAC address as second unicast MAC address
265 * or enter promiscuous mode if not capable of listening
266 * for multiple unicast MACs.
267 */
268 rtnl_lock();
269 memcpy(flogi_maddr, (u8[6]) FC_FCOE_FLOGI_MAC, ETH_ALEN);
270 dev_unicast_add(fc->real_dev, flogi_maddr, ETH_ALEN);
271 dev_mc_add(fc->real_dev, FIP_ALL_ENODE_MACS, ETH_ALEN, 0);
272 rtnl_unlock();
273
274 /*
275 * setup the receive function from ethernet driver
276 * on the ethertype for the given device
277 */
278 fc->fcoe_packet_type.func = fcoe_rcv;
279 fc->fcoe_packet_type.type = __constant_htons(ETH_P_FCOE);
280 fc->fcoe_packet_type.dev = fc->real_dev;
281 dev_add_pack(&fc->fcoe_packet_type);
282
283 return 0;
284 }
285
286 /**
287 * fcoe_shost_config() - Sets up fc_lport->host
288 * @lp : ptr to the fc_lport
289 * @shost : ptr to the associated scsi host
290 * @dev : device associated to scsi host
291 *
292 * Must be called after fcoe_lport_config() and fcoe_netdev_config()
293 *
294 * Returns : 0 for success
295 */
296 static int fcoe_shost_config(struct fc_lport *lp, struct Scsi_Host *shost,
297 struct device *dev)
298 {
299 int rc = 0;
300
301 /* lport scsi host config */
302 lp->host = shost;
303
304 lp->host->max_lun = FCOE_MAX_LUN;
305 lp->host->max_id = FCOE_MAX_FCP_TARGET;
306 lp->host->max_channel = 0;
307 lp->host->transportt = scsi_transport_fcoe_sw;
308
309 /* add the new host to the SCSI-ml */
310 rc = scsi_add_host(lp->host, dev);
311 if (rc) {
312 FC_DBG("fcoe_shost_config:error on scsi_add_host\n");
313 return rc;
314 }
315 sprintf(fc_host_symbolic_name(lp->host), "%s v%s over %s",
316 FCOE_NAME, FCOE_VERSION,
317 fcoe_netdev(lp)->name);
318
319 return 0;
320 }
321
322 /**
323 * fcoe_em_config() - allocates em for this lport
324 * @lp: the port that em is to allocated for
325 *
326 * Returns : 0 on success
327 */
328 static inline int fcoe_em_config(struct fc_lport *lp)
329 {
330 BUG_ON(lp->emp);
331
332 lp->emp = fc_exch_mgr_alloc(lp, FC_CLASS_3,
333 FCOE_MIN_XID, FCOE_MAX_XID);
334 if (!lp->emp)
335 return -ENOMEM;
336
337 return 0;
338 }
339
340 /**
341 * fcoe_if_destroy() - FCoE software HBA tear-down function
342 * @netdev: ptr to the associated net_device
343 *
344 * Returns: 0 if link is OK for use by FCoE.
345 */
346 static int fcoe_if_destroy(struct net_device *netdev)
347 {
348 struct fc_lport *lp = NULL;
349 struct fcoe_softc *fc;
350 u8 flogi_maddr[ETH_ALEN];
351
352 BUG_ON(!netdev);
353
354 printk(KERN_DEBUG "fcoe_if_destroy:interface on %s\n",
355 netdev->name);
356
357 lp = fcoe_hostlist_lookup(netdev);
358 if (!lp)
359 return -ENODEV;
360
361 fc = lport_priv(lp);
362
363 /* Logout of the fabric */
364 fc_fabric_logoff(lp);
365
366 /* Remove the instance from fcoe's list */
367 fcoe_hostlist_remove(lp);
368
369 /* Don't listen for Ethernet packets anymore */
370 dev_remove_pack(&fc->fcoe_packet_type);
371 dev_remove_pack(&fc->fip_packet_type);
372 fcoe_ctlr_destroy(&fc->ctlr);
373
374 /* Cleanup the fc_lport */
375 fc_lport_destroy(lp);
376 fc_fcp_destroy(lp);
377
378 /* Detach from the scsi-ml */
379 fc_remove_host(lp->host);
380 scsi_remove_host(lp->host);
381
382 /* There are no more rports or I/O, free the EM */
383 if (lp->emp)
384 fc_exch_mgr_free(lp->emp);
385
386 /* Delete secondary MAC addresses */
387 rtnl_lock();
388 memcpy(flogi_maddr, (u8[6]) FC_FCOE_FLOGI_MAC, ETH_ALEN);
389 dev_unicast_delete(fc->real_dev, flogi_maddr, ETH_ALEN);
390 if (!is_zero_ether_addr(fc->ctlr.data_src_addr))
391 dev_unicast_delete(fc->real_dev,
392 fc->ctlr.data_src_addr, ETH_ALEN);
393 dev_mc_delete(fc->real_dev, FIP_ALL_ENODE_MACS, ETH_ALEN, 0);
394 rtnl_unlock();
395
396 /* Free the per-CPU receive threads */
397 fcoe_percpu_clean(lp);
398
399 /* Free existing skbs */
400 fcoe_clean_pending_queue(lp);
401
402 /* Stop the timer */
403 del_timer_sync(&fc->timer);
404
405 /* Free memory used by statistical counters */
406 fc_lport_free_stats(lp);
407
408 /* Release the net_device and Scsi_Host */
409 dev_put(fc->real_dev);
410 scsi_host_put(lp->host);
411
412 return 0;
413 }
414
415 /*
416 * fcoe_ddp_setup - calls LLD's ddp_setup through net_device
417 * @lp: the corresponding fc_lport
418 * @xid: the exchange id for this ddp transfer
419 * @sgl: the scatterlist describing this transfer
420 * @sgc: number of sg items
421 *
422 * Returns : 0 no ddp
423 */
424 static int fcoe_ddp_setup(struct fc_lport *lp, u16 xid,
425 struct scatterlist *sgl, unsigned int sgc)
426 {
427 struct net_device *n = fcoe_netdev(lp);
428
429 if (n->netdev_ops && n->netdev_ops->ndo_fcoe_ddp_setup)
430 return n->netdev_ops->ndo_fcoe_ddp_setup(n, xid, sgl, sgc);
431
432 return 0;
433 }
434
435 /*
436 * fcoe_ddp_done - calls LLD's ddp_done through net_device
437 * @lp: the corresponding fc_lport
438 * @xid: the exchange id for this ddp transfer
439 *
440 * Returns : the length of data that have been completed by ddp
441 */
442 static int fcoe_ddp_done(struct fc_lport *lp, u16 xid)
443 {
444 struct net_device *n = fcoe_netdev(lp);
445
446 if (n->netdev_ops && n->netdev_ops->ndo_fcoe_ddp_done)
447 return n->netdev_ops->ndo_fcoe_ddp_done(n, xid);
448 return 0;
449 }
450
451 static struct libfc_function_template fcoe_libfc_fcn_templ = {
452 .frame_send = fcoe_xmit,
453 .ddp_setup = fcoe_ddp_setup,
454 .ddp_done = fcoe_ddp_done,
455 };
456
457 /**
458 * fcoe_fip_recv - handle a received FIP frame.
459 * @skb: the receive skb
460 * @dev: associated &net_device
461 * @ptype: the &packet_type structure which was used to register this handler.
462 * @orig_dev: original receive &net_device, in case @dev is a bond.
463 *
464 * Returns: 0 for success
465 */
466 static int fcoe_fip_recv(struct sk_buff *skb, struct net_device *dev,
467 struct packet_type *ptype,
468 struct net_device *orig_dev)
469 {
470 struct fcoe_softc *fc;
471
472 fc = container_of(ptype, struct fcoe_softc, fip_packet_type);
473 fcoe_ctlr_recv(&fc->ctlr, skb);
474 return 0;
475 }
476
477 /**
478 * fcoe_fip_send() - send an Ethernet-encapsulated FIP frame.
479 * @fip: FCoE controller.
480 * @skb: FIP Packet.
481 */
482 static void fcoe_fip_send(struct fcoe_ctlr *fip, struct sk_buff *skb)
483 {
484 skb->dev = fcoe_from_ctlr(fip)->real_dev;
485 dev_queue_xmit(skb);
486 }
487
488 /**
489 * fcoe_update_src_mac() - Update Ethernet MAC filters.
490 * @fip: FCoE controller.
491 * @old: Unicast MAC address to delete if the MAC is non-zero.
492 * @new: Unicast MAC address to add.
493 *
494 * Remove any previously-set unicast MAC filter.
495 * Add secondary FCoE MAC address filter for our OUI.
496 */
497 static void fcoe_update_src_mac(struct fcoe_ctlr *fip, u8 *old, u8 *new)
498 {
499 struct fcoe_softc *fc;
500
501 fc = fcoe_from_ctlr(fip);
502 rtnl_lock();
503 if (!is_zero_ether_addr(old))
504 dev_unicast_delete(fc->real_dev, old, ETH_ALEN);
505 dev_unicast_add(fc->real_dev, new, ETH_ALEN);
506 rtnl_unlock();
507 }
508
509 /**
510 * fcoe_if_create() - this function creates the fcoe interface
511 * @netdev: pointer the associated netdevice
512 *
513 * Creates fc_lport struct and scsi_host for lport, configures lport
514 * and starts fabric login.
515 *
516 * Returns : 0 on success
517 */
518 static int fcoe_if_create(struct net_device *netdev)
519 {
520 int rc;
521 struct fc_lport *lp = NULL;
522 struct fcoe_softc *fc;
523 struct Scsi_Host *shost;
524
525 BUG_ON(!netdev);
526
527 printk(KERN_DEBUG "fcoe_if_create:interface on %s\n",
528 netdev->name);
529
530 lp = fcoe_hostlist_lookup(netdev);
531 if (lp)
532 return -EEXIST;
533
534 shost = libfc_host_alloc(&fcoe_shost_template,
535 sizeof(struct fcoe_softc));
536 if (!shost) {
537 FC_DBG("Could not allocate host structure\n");
538 return -ENOMEM;
539 }
540 lp = shost_priv(shost);
541 fc = lport_priv(lp);
542
543 /* configure fc_lport, e.g., em */
544 rc = fcoe_lport_config(lp);
545 if (rc) {
546 FC_DBG("Could not configure lport\n");
547 goto out_host_put;
548 }
549
550 /* configure lport network properties */
551 rc = fcoe_netdev_config(lp, netdev);
552 if (rc) {
553 FC_DBG("Could not configure netdev for lport\n");
554 goto out_host_put;
555 }
556
557 /*
558 * Initialize FIP.
559 */
560 fcoe_ctlr_init(&fc->ctlr);
561 fc->ctlr.send = fcoe_fip_send;
562 fc->ctlr.update_mac = fcoe_update_src_mac;
563
564 fc->fip_packet_type.func = fcoe_fip_recv;
565 fc->fip_packet_type.type = htons(ETH_P_FIP);
566 fc->fip_packet_type.dev = fc->real_dev;
567 dev_add_pack(&fc->fip_packet_type);
568
569 /* configure lport scsi host properties */
570 rc = fcoe_shost_config(lp, shost, &netdev->dev);
571 if (rc) {
572 FC_DBG("Could not configure shost for lport\n");
573 goto out_host_put;
574 }
575
576 /* lport exch manager allocation */
577 rc = fcoe_em_config(lp);
578 if (rc) {
579 FC_DBG("Could not configure em for lport\n");
580 goto out_host_put;
581 }
582
583 /* Initialize the library */
584 rc = fcoe_libfc_config(lp, &fcoe_libfc_fcn_templ);
585 if (rc) {
586 FC_DBG("Could not configure libfc for lport!\n");
587 goto out_lp_destroy;
588 }
589
590 /* add to lports list */
591 fcoe_hostlist_add(lp);
592
593 lp->boot_time = jiffies;
594
595 fc_fabric_login(lp);
596
597 if (!fcoe_link_ok(lp))
598 fcoe_ctlr_link_up(&fc->ctlr);
599
600 dev_hold(netdev);
601
602 return rc;
603
604 out_lp_destroy:
605 fc_exch_mgr_free(lp->emp); /* Free the EM */
606 out_host_put:
607 scsi_host_put(lp->host);
608 return rc;
609 }
610
611 /**
612 * fcoe_if_init() - attach to scsi transport
613 *
614 * Returns : 0 on success
615 */
616 static int __init fcoe_if_init(void)
617 {
618 /* attach to scsi transport */
619 scsi_transport_fcoe_sw =
620 fc_attach_transport(&fcoe_transport_function);
621
622 if (!scsi_transport_fcoe_sw) {
623 printk(KERN_ERR "fcoe_init:fc_attach_transport() failed\n");
624 return -ENODEV;
625 }
626
627 return 0;
628 }
629
630 /**
631 * fcoe_if_exit() - detach from scsi transport
632 *
633 * Returns : 0 on success
634 */
635 int __exit fcoe_if_exit(void)
636 {
637 fc_release_transport(scsi_transport_fcoe_sw);
638 return 0;
639 }
640
641 /**
642 * fcoe_percpu_thread_create() - Create a receive thread for an online cpu
643 * @cpu: cpu index for the online cpu
644 */
645 static void fcoe_percpu_thread_create(unsigned int cpu)
646 {
647 struct fcoe_percpu_s *p;
648 struct task_struct *thread;
649
650 p = &per_cpu(fcoe_percpu, cpu);
651
652 thread = kthread_create(fcoe_percpu_receive_thread,
653 (void *)p, "fcoethread/%d", cpu);
654
655 if (likely(!IS_ERR(p->thread))) {
656 kthread_bind(thread, cpu);
657 wake_up_process(thread);
658
659 spin_lock_bh(&p->fcoe_rx_list.lock);
660 p->thread = thread;
661 spin_unlock_bh(&p->fcoe_rx_list.lock);
662 }
663 }
664
665 /**
666 * fcoe_percpu_thread_destroy() - removes the rx thread for the given cpu
667 * @cpu: cpu index the rx thread is to be removed
668 *
669 * Destroys a per-CPU Rx thread. Any pending skbs are moved to the
670 * current CPU's Rx thread. If the thread being destroyed is bound to
671 * the CPU processing this context the skbs will be freed.
672 */
673 static void fcoe_percpu_thread_destroy(unsigned int cpu)
674 {
675 struct fcoe_percpu_s *p;
676 struct task_struct *thread;
677 struct page *crc_eof;
678 struct sk_buff *skb;
679 #ifdef CONFIG_SMP
680 struct fcoe_percpu_s *p0;
681 unsigned targ_cpu = smp_processor_id();
682 #endif /* CONFIG_SMP */
683
684 printk(KERN_DEBUG "fcoe: Destroying receive thread for CPU %d\n", cpu);
685
686 /* Prevent any new skbs from being queued for this CPU. */
687 p = &per_cpu(fcoe_percpu, cpu);
688 spin_lock_bh(&p->fcoe_rx_list.lock);
689 thread = p->thread;
690 p->thread = NULL;
691 crc_eof = p->crc_eof_page;
692 p->crc_eof_page = NULL;
693 p->crc_eof_offset = 0;
694 spin_unlock_bh(&p->fcoe_rx_list.lock);
695
696 #ifdef CONFIG_SMP
697 /*
698 * Don't bother moving the skb's if this context is running
699 * on the same CPU that is having its thread destroyed. This
700 * can easily happen when the module is removed.
701 */
702 if (cpu != targ_cpu) {
703 p0 = &per_cpu(fcoe_percpu, targ_cpu);
704 spin_lock_bh(&p0->fcoe_rx_list.lock);
705 if (p0->thread) {
706 FC_DBG("Moving frames from CPU %d to CPU %d\n",
707 cpu, targ_cpu);
708
709 while ((skb = __skb_dequeue(&p->fcoe_rx_list)) != NULL)
710 __skb_queue_tail(&p0->fcoe_rx_list, skb);
711 spin_unlock_bh(&p0->fcoe_rx_list.lock);
712 } else {
713 /*
714 * The targeted CPU is not initialized and cannot accept
715 * new skbs. Unlock the targeted CPU and drop the skbs
716 * on the CPU that is going offline.
717 */
718 while ((skb = __skb_dequeue(&p->fcoe_rx_list)) != NULL)
719 kfree_skb(skb);
720 spin_unlock_bh(&p0->fcoe_rx_list.lock);
721 }
722 } else {
723 /*
724 * This scenario occurs when the module is being removed
725 * and all threads are being destroyed. skbs will continue
726 * to be shifted from the CPU thread that is being removed
727 * to the CPU thread associated with the CPU that is processing
728 * the module removal. Once there is only one CPU Rx thread it
729 * will reach this case and we will drop all skbs and later
730 * stop the thread.
731 */
732 spin_lock_bh(&p->fcoe_rx_list.lock);
733 while ((skb = __skb_dequeue(&p->fcoe_rx_list)) != NULL)
734 kfree_skb(skb);
735 spin_unlock_bh(&p->fcoe_rx_list.lock);
736 }
737 #else
738 /*
739 * This a non-SMP scenario where the singular Rx thread is
740 * being removed. Free all skbs and stop the thread.
741 */
742 spin_lock_bh(&p->fcoe_rx_list.lock);
743 while ((skb = __skb_dequeue(&p->fcoe_rx_list)) != NULL)
744 kfree_skb(skb);
745 spin_unlock_bh(&p->fcoe_rx_list.lock);
746 #endif
747
748 if (thread)
749 kthread_stop(thread);
750
751 if (crc_eof)
752 put_page(crc_eof);
753 }
754
755 /**
756 * fcoe_cpu_callback() - fcoe cpu hotplug event callback
757 * @nfb: callback data block
758 * @action: event triggering the callback
759 * @hcpu: index for the cpu of this event
760 *
761 * This creates or destroys per cpu data for fcoe
762 *
763 * Returns NOTIFY_OK always.
764 */
765 static int fcoe_cpu_callback(struct notifier_block *nfb,
766 unsigned long action, void *hcpu)
767 {
768 unsigned cpu = (unsigned long)hcpu;
769
770 switch (action) {
771 case CPU_ONLINE:
772 case CPU_ONLINE_FROZEN:
773 FC_DBG("CPU %x online: Create Rx thread\n", cpu);
774 fcoe_percpu_thread_create(cpu);
775 break;
776 case CPU_DEAD:
777 case CPU_DEAD_FROZEN:
778 FC_DBG("CPU %x offline: Remove Rx thread\n", cpu);
779 fcoe_percpu_thread_destroy(cpu);
780 break;
781 default:
782 break;
783 }
784 return NOTIFY_OK;
785 }
786
787 static struct notifier_block fcoe_cpu_notifier = {
788 .notifier_call = fcoe_cpu_callback,
789 };
790
791 /**
792 * fcoe_rcv() - this is the fcoe receive function called by NET_RX_SOFTIRQ
793 * @skb: the receive skb
794 * @dev: associated net device
795 * @ptype: context
796 * @olddev: last device
797 *
798 * this function will receive the packet and build fc frame and pass it up
799 *
800 * Returns: 0 for success
801 */
802 int fcoe_rcv(struct sk_buff *skb, struct net_device *dev,
803 struct packet_type *ptype, struct net_device *olddev)
804 {
805 struct fc_lport *lp;
806 struct fcoe_rcv_info *fr;
807 struct fcoe_softc *fc;
808 struct fc_frame_header *fh;
809 struct fcoe_percpu_s *fps;
810 unsigned short oxid;
811 unsigned int cpu = 0;
812
813 fc = container_of(ptype, struct fcoe_softc, fcoe_packet_type);
814 lp = fc->ctlr.lp;
815 if (unlikely(lp == NULL)) {
816 FC_DBG("cannot find hba structure");
817 goto err2;
818 }
819 if (!lp->link_up)
820 goto err2;
821
822 if (unlikely(debug_fcoe)) {
823 FC_DBG("skb_info: len:%d data_len:%d head:%p data:%p tail:%p "
824 "end:%p sum:%d dev:%s", skb->len, skb->data_len,
825 skb->head, skb->data, skb_tail_pointer(skb),
826 skb_end_pointer(skb), skb->csum,
827 skb->dev ? skb->dev->name : "<NULL>");
828
829 }
830
831 /* check for FCOE packet type */
832 if (unlikely(eth_hdr(skb)->h_proto != htons(ETH_P_FCOE))) {
833 FC_DBG("wrong FC type frame");
834 goto err;
835 }
836
837 /*
838 * Check for minimum frame length, and make sure required FCoE
839 * and FC headers are pulled into the linear data area.
840 */
841 if (unlikely((skb->len < FCOE_MIN_FRAME) ||
842 !pskb_may_pull(skb, FCOE_HEADER_LEN)))
843 goto err;
844
845 skb_set_transport_header(skb, sizeof(struct fcoe_hdr));
846 fh = (struct fc_frame_header *) skb_transport_header(skb);
847
848 oxid = ntohs(fh->fh_ox_id);
849
850 fr = fcoe_dev_from_skb(skb);
851 fr->fr_dev = lp;
852 fr->ptype = ptype;
853
854 #ifdef CONFIG_SMP
855 /*
856 * The incoming frame exchange id(oxid) is ANDed with num of online
857 * cpu bits to get cpu and then this cpu is used for selecting
858 * a per cpu kernel thread from fcoe_percpu.
859 */
860 cpu = oxid & (num_online_cpus() - 1);
861 #endif
862
863 fps = &per_cpu(fcoe_percpu, cpu);
864 spin_lock_bh(&fps->fcoe_rx_list.lock);
865 if (unlikely(!fps->thread)) {
866 /*
867 * The targeted CPU is not ready, let's target
868 * the first CPU now. For non-SMP systems this
869 * will check the same CPU twice.
870 */
871 FC_DBG("CPU is online, but no receive thread ready "
872 "for incoming skb- using first online CPU.\n");
873
874 spin_unlock_bh(&fps->fcoe_rx_list.lock);
875 cpu = first_cpu(cpu_online_map);
876 fps = &per_cpu(fcoe_percpu, cpu);
877 spin_lock_bh(&fps->fcoe_rx_list.lock);
878 if (!fps->thread) {
879 spin_unlock_bh(&fps->fcoe_rx_list.lock);
880 goto err;
881 }
882 }
883
884 /*
885 * We now have a valid CPU that we're targeting for
886 * this skb. We also have this receive thread locked,
887 * so we're free to queue skbs into it's queue.
888 */
889 __skb_queue_tail(&fps->fcoe_rx_list, skb);
890 if (fps->fcoe_rx_list.qlen == 1)
891 wake_up_process(fps->thread);
892
893 spin_unlock_bh(&fps->fcoe_rx_list.lock);
894
895 return 0;
896 err:
897 fc_lport_get_stats(lp)->ErrorFrames++;
898
899 err2:
900 kfree_skb(skb);
901 return -1;
902 }
903
904 /**
905 * fcoe_start_io() - pass to netdev to start xmit for fcoe
906 * @skb: the skb to be xmitted
907 *
908 * Returns: 0 for success
909 */
910 static inline int fcoe_start_io(struct sk_buff *skb)
911 {
912 int rc;
913
914 skb_get(skb);
915 rc = dev_queue_xmit(skb);
916 if (rc != 0)
917 return rc;
918 kfree_skb(skb);
919 return 0;
920 }
921
922 /**
923 * fcoe_get_paged_crc_eof() - in case we need to alloc a page for crc_eof
924 * @skb: the skb to be xmitted
925 * @tlen: total len
926 *
927 * Returns: 0 for success
928 */
929 static int fcoe_get_paged_crc_eof(struct sk_buff *skb, int tlen)
930 {
931 struct fcoe_percpu_s *fps;
932 struct page *page;
933
934 fps = &get_cpu_var(fcoe_percpu);
935 page = fps->crc_eof_page;
936 if (!page) {
937 page = alloc_page(GFP_ATOMIC);
938 if (!page) {
939 put_cpu_var(fcoe_percpu);
940 return -ENOMEM;
941 }
942 fps->crc_eof_page = page;
943 fps->crc_eof_offset = 0;
944 }
945
946 get_page(page);
947 skb_fill_page_desc(skb, skb_shinfo(skb)->nr_frags, page,
948 fps->crc_eof_offset, tlen);
949 skb->len += tlen;
950 skb->data_len += tlen;
951 skb->truesize += tlen;
952 fps->crc_eof_offset += sizeof(struct fcoe_crc_eof);
953
954 if (fps->crc_eof_offset >= PAGE_SIZE) {
955 fps->crc_eof_page = NULL;
956 fps->crc_eof_offset = 0;
957 put_page(page);
958 }
959 put_cpu_var(fcoe_percpu);
960 return 0;
961 }
962
963 /**
964 * fcoe_fc_crc() - calculates FC CRC in this fcoe skb
965 * @fp: the fc_frame containing data to be checksummed
966 *
967 * This uses crc32() to calculate the crc for fc frame
968 * Return : 32 bit crc
969 */
970 u32 fcoe_fc_crc(struct fc_frame *fp)
971 {
972 struct sk_buff *skb = fp_skb(fp);
973 struct skb_frag_struct *frag;
974 unsigned char *data;
975 unsigned long off, len, clen;
976 u32 crc;
977 unsigned i;
978
979 crc = crc32(~0, skb->data, skb_headlen(skb));
980
981 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
982 frag = &skb_shinfo(skb)->frags[i];
983 off = frag->page_offset;
984 len = frag->size;
985 while (len > 0) {
986 clen = min(len, PAGE_SIZE - (off & ~PAGE_MASK));
987 data = kmap_atomic(frag->page + (off >> PAGE_SHIFT),
988 KM_SKB_DATA_SOFTIRQ);
989 crc = crc32(crc, data + (off & ~PAGE_MASK), clen);
990 kunmap_atomic(data, KM_SKB_DATA_SOFTIRQ);
991 off += clen;
992 len -= clen;
993 }
994 }
995 return crc;
996 }
997
998 /**
999 * fcoe_xmit() - FCoE frame transmit function
1000 * @lp: the associated local port
1001 * @fp: the fc_frame to be transmitted
1002 *
1003 * Return : 0 for success
1004 */
1005 int fcoe_xmit(struct fc_lport *lp, struct fc_frame *fp)
1006 {
1007 int wlen;
1008 u32 crc;
1009 struct ethhdr *eh;
1010 struct fcoe_crc_eof *cp;
1011 struct sk_buff *skb;
1012 struct fcoe_dev_stats *stats;
1013 struct fc_frame_header *fh;
1014 unsigned int hlen; /* header length implies the version */
1015 unsigned int tlen; /* trailer length */
1016 unsigned int elen; /* eth header, may include vlan */
1017 struct fcoe_softc *fc;
1018 u8 sof, eof;
1019 struct fcoe_hdr *hp;
1020
1021 WARN_ON((fr_len(fp) % sizeof(u32)) != 0);
1022
1023 fc = lport_priv(lp);
1024 fh = fc_frame_header_get(fp);
1025 skb = fp_skb(fp);
1026 wlen = skb->len / FCOE_WORD_TO_BYTE;
1027
1028 if (!lp->link_up) {
1029 kfree_skb(skb);
1030 return 0;
1031 }
1032
1033 if (unlikely(fh->fh_r_ctl == FC_RCTL_ELS_REQ) &&
1034 fcoe_ctlr_els_send(&fc->ctlr, skb))
1035 return 0;
1036
1037 sof = fr_sof(fp);
1038 eof = fr_eof(fp);
1039
1040 elen = sizeof(struct ethhdr);
1041 hlen = sizeof(struct fcoe_hdr);
1042 tlen = sizeof(struct fcoe_crc_eof);
1043 wlen = (skb->len - tlen + sizeof(crc)) / FCOE_WORD_TO_BYTE;
1044
1045 /* crc offload */
1046 if (likely(lp->crc_offload)) {
1047 skb->ip_summed = CHECKSUM_PARTIAL;
1048 skb->csum_start = skb_headroom(skb);
1049 skb->csum_offset = skb->len;
1050 crc = 0;
1051 } else {
1052 skb->ip_summed = CHECKSUM_NONE;
1053 crc = fcoe_fc_crc(fp);
1054 }
1055
1056 /* copy fc crc and eof to the skb buff */
1057 if (skb_is_nonlinear(skb)) {
1058 skb_frag_t *frag;
1059 if (fcoe_get_paged_crc_eof(skb, tlen)) {
1060 kfree_skb(skb);
1061 return -ENOMEM;
1062 }
1063 frag = &skb_shinfo(skb)->frags[skb_shinfo(skb)->nr_frags - 1];
1064 cp = kmap_atomic(frag->page, KM_SKB_DATA_SOFTIRQ)
1065 + frag->page_offset;
1066 } else {
1067 cp = (struct fcoe_crc_eof *)skb_put(skb, tlen);
1068 }
1069
1070 memset(cp, 0, sizeof(*cp));
1071 cp->fcoe_eof = eof;
1072 cp->fcoe_crc32 = cpu_to_le32(~crc);
1073
1074 if (skb_is_nonlinear(skb)) {
1075 kunmap_atomic(cp, KM_SKB_DATA_SOFTIRQ);
1076 cp = NULL;
1077 }
1078
1079 /* adjust skb network/transport offsets to match mac/fcoe/fc */
1080 skb_push(skb, elen + hlen);
1081 skb_reset_mac_header(skb);
1082 skb_reset_network_header(skb);
1083 skb->mac_len = elen;
1084 skb->protocol = htons(ETH_P_FCOE);
1085 skb->dev = fc->real_dev;
1086
1087 /* fill up mac and fcoe headers */
1088 eh = eth_hdr(skb);
1089 eh->h_proto = htons(ETH_P_FCOE);
1090 if (fc->ctlr.map_dest)
1091 fc_fcoe_set_mac(eh->h_dest, fh->fh_d_id);
1092 else
1093 /* insert GW address */
1094 memcpy(eh->h_dest, fc->ctlr.dest_addr, ETH_ALEN);
1095
1096 if (unlikely(fc->ctlr.flogi_oxid != FC_XID_UNKNOWN))
1097 memcpy(eh->h_source, fc->ctlr.ctl_src_addr, ETH_ALEN);
1098 else
1099 memcpy(eh->h_source, fc->ctlr.data_src_addr, ETH_ALEN);
1100
1101 hp = (struct fcoe_hdr *)(eh + 1);
1102 memset(hp, 0, sizeof(*hp));
1103 if (FC_FCOE_VER)
1104 FC_FCOE_ENCAPS_VER(hp, FC_FCOE_VER);
1105 hp->fcoe_sof = sof;
1106
1107 #ifdef NETIF_F_FSO
1108 /* fcoe lso, mss is in max_payload which is non-zero for FCP data */
1109 if (lp->seq_offload && fr_max_payload(fp)) {
1110 skb_shinfo(skb)->gso_type = SKB_GSO_FCOE;
1111 skb_shinfo(skb)->gso_size = fr_max_payload(fp);
1112 } else {
1113 skb_shinfo(skb)->gso_type = 0;
1114 skb_shinfo(skb)->gso_size = 0;
1115 }
1116 #endif
1117 /* update tx stats: regardless if LLD fails */
1118 stats = fc_lport_get_stats(lp);
1119 stats->TxFrames++;
1120 stats->TxWords += wlen;
1121
1122 /* send down to lld */
1123 fr_dev(fp) = lp;
1124 if (fc->fcoe_pending_queue.qlen)
1125 fcoe_check_wait_queue(lp, skb);
1126 else if (fcoe_start_io(skb))
1127 fcoe_check_wait_queue(lp, skb);
1128
1129 return 0;
1130 }
1131
1132 /**
1133 * fcoe_percpu_receive_thread() - recv thread per cpu
1134 * @arg: ptr to the fcoe per cpu struct
1135 *
1136 * Return: 0 for success
1137 */
1138 int fcoe_percpu_receive_thread(void *arg)
1139 {
1140 struct fcoe_percpu_s *p = arg;
1141 u32 fr_len;
1142 struct fc_lport *lp;
1143 struct fcoe_rcv_info *fr;
1144 struct fcoe_dev_stats *stats;
1145 struct fc_frame_header *fh;
1146 struct sk_buff *skb;
1147 struct fcoe_crc_eof crc_eof;
1148 struct fc_frame *fp;
1149 u8 *mac = NULL;
1150 struct fcoe_softc *fc;
1151 struct fcoe_hdr *hp;
1152
1153 set_user_nice(current, -20);
1154
1155 while (!kthread_should_stop()) {
1156
1157 spin_lock_bh(&p->fcoe_rx_list.lock);
1158 while ((skb = __skb_dequeue(&p->fcoe_rx_list)) == NULL) {
1159 set_current_state(TASK_INTERRUPTIBLE);
1160 spin_unlock_bh(&p->fcoe_rx_list.lock);
1161 schedule();
1162 set_current_state(TASK_RUNNING);
1163 if (kthread_should_stop())
1164 return 0;
1165 spin_lock_bh(&p->fcoe_rx_list.lock);
1166 }
1167 spin_unlock_bh(&p->fcoe_rx_list.lock);
1168 fr = fcoe_dev_from_skb(skb);
1169 lp = fr->fr_dev;
1170 if (unlikely(lp == NULL)) {
1171 FC_DBG("invalid HBA Structure");
1172 kfree_skb(skb);
1173 continue;
1174 }
1175
1176 if (unlikely(debug_fcoe)) {
1177 FC_DBG("skb_info: len:%d data_len:%d head:%p data:%p "
1178 "tail:%p end:%p sum:%d dev:%s",
1179 skb->len, skb->data_len,
1180 skb->head, skb->data, skb_tail_pointer(skb),
1181 skb_end_pointer(skb), skb->csum,
1182 skb->dev ? skb->dev->name : "<NULL>");
1183 }
1184
1185 /*
1186 * Save source MAC address before discarding header.
1187 */
1188 fc = lport_priv(lp);
1189 if (skb_is_nonlinear(skb))
1190 skb_linearize(skb); /* not ideal */
1191 mac = eth_hdr(skb)->h_source;
1192
1193 /*
1194 * Frame length checks and setting up the header pointers
1195 * was done in fcoe_rcv already.
1196 */
1197 hp = (struct fcoe_hdr *) skb_network_header(skb);
1198 fh = (struct fc_frame_header *) skb_transport_header(skb);
1199
1200 stats = fc_lport_get_stats(lp);
1201 if (unlikely(FC_FCOE_DECAPS_VER(hp) != FC_FCOE_VER)) {
1202 if (stats->ErrorFrames < 5)
1203 printk(KERN_WARNING "FCoE version "
1204 "mismatch: The frame has "
1205 "version %x, but the "
1206 "initiator supports version "
1207 "%x\n", FC_FCOE_DECAPS_VER(hp),
1208 FC_FCOE_VER);
1209 stats->ErrorFrames++;
1210 kfree_skb(skb);
1211 continue;
1212 }
1213
1214 skb_pull(skb, sizeof(struct fcoe_hdr));
1215 fr_len = skb->len - sizeof(struct fcoe_crc_eof);
1216
1217 stats->RxFrames++;
1218 stats->RxWords += fr_len / FCOE_WORD_TO_BYTE;
1219
1220 fp = (struct fc_frame *)skb;
1221 fc_frame_init(fp);
1222 fr_dev(fp) = lp;
1223 fr_sof(fp) = hp->fcoe_sof;
1224
1225 /* Copy out the CRC and EOF trailer for access */
1226 if (skb_copy_bits(skb, fr_len, &crc_eof, sizeof(crc_eof))) {
1227 kfree_skb(skb);
1228 continue;
1229 }
1230 fr_eof(fp) = crc_eof.fcoe_eof;
1231 fr_crc(fp) = crc_eof.fcoe_crc32;
1232 if (pskb_trim(skb, fr_len)) {
1233 kfree_skb(skb);
1234 continue;
1235 }
1236
1237 /*
1238 * We only check CRC if no offload is available and if it is
1239 * it's solicited data, in which case, the FCP layer would
1240 * check it during the copy.
1241 */
1242 if (lp->crc_offload && skb->ip_summed == CHECKSUM_UNNECESSARY)
1243 fr_flags(fp) &= ~FCPHF_CRC_UNCHECKED;
1244 else
1245 fr_flags(fp) |= FCPHF_CRC_UNCHECKED;
1246
1247 fh = fc_frame_header_get(fp);
1248 if (fh->fh_r_ctl == FC_RCTL_DD_SOL_DATA &&
1249 fh->fh_type == FC_TYPE_FCP) {
1250 fc_exch_recv(lp, lp->emp, fp);
1251 continue;
1252 }
1253 if (fr_flags(fp) & FCPHF_CRC_UNCHECKED) {
1254 if (le32_to_cpu(fr_crc(fp)) !=
1255 ~crc32(~0, skb->data, fr_len)) {
1256 if (debug_fcoe || stats->InvalidCRCCount < 5)
1257 printk(KERN_WARNING "fcoe: dropping "
1258 "frame with CRC error\n");
1259 stats->InvalidCRCCount++;
1260 stats->ErrorFrames++;
1261 fc_frame_free(fp);
1262 continue;
1263 }
1264 fr_flags(fp) &= ~FCPHF_CRC_UNCHECKED;
1265 }
1266 if (unlikely(fc->ctlr.flogi_oxid != FC_XID_UNKNOWN) &&
1267 fcoe_ctlr_recv_flogi(&fc->ctlr, fp, mac)) {
1268 fc_frame_free(fp);
1269 continue;
1270 }
1271 fc_exch_recv(lp, lp->emp, fp);
1272 }
1273 return 0;
1274 }
1275
1276 /**
1277 * fcoe_check_wait_queue() - attempt to clear the transmit backlog
1278 * @lp: the fc_lport
1279 *
1280 * This empties the wait_queue, dequeue the head of the wait_queue queue
1281 * and calls fcoe_start_io() for each packet, if all skb have been
1282 * transmitted, return qlen or -1 if a error occurs, then restore
1283 * wait_queue and try again later.
1284 *
1285 * The wait_queue is used when the skb transmit fails. skb will go
1286 * in the wait_queue which will be emptied by the timer function or
1287 * by the next skb transmit.
1288 */
1289 static void fcoe_check_wait_queue(struct fc_lport *lp, struct sk_buff *skb)
1290 {
1291 struct fcoe_softc *fc = lport_priv(lp);
1292 int rc;
1293
1294 spin_lock_bh(&fc->fcoe_pending_queue.lock);
1295
1296 if (skb)
1297 __skb_queue_tail(&fc->fcoe_pending_queue, skb);
1298
1299 if (fc->fcoe_pending_queue_active)
1300 goto out;
1301 fc->fcoe_pending_queue_active = 1;
1302
1303 while (fc->fcoe_pending_queue.qlen) {
1304 /* keep qlen > 0 until fcoe_start_io succeeds */
1305 fc->fcoe_pending_queue.qlen++;
1306 skb = __skb_dequeue(&fc->fcoe_pending_queue);
1307
1308 spin_unlock_bh(&fc->fcoe_pending_queue.lock);
1309 rc = fcoe_start_io(skb);
1310 spin_lock_bh(&fc->fcoe_pending_queue.lock);
1311
1312 if (rc) {
1313 __skb_queue_head(&fc->fcoe_pending_queue, skb);
1314 /* undo temporary increment above */
1315 fc->fcoe_pending_queue.qlen--;
1316 break;
1317 }
1318 /* undo temporary increment above */
1319 fc->fcoe_pending_queue.qlen--;
1320 }
1321
1322 if (fc->fcoe_pending_queue.qlen < FCOE_LOW_QUEUE_DEPTH)
1323 lp->qfull = 0;
1324 if (fc->fcoe_pending_queue.qlen && !timer_pending(&fc->timer))
1325 mod_timer(&fc->timer, jiffies + 2);
1326 fc->fcoe_pending_queue_active = 0;
1327 out:
1328 if (fc->fcoe_pending_queue.qlen > FCOE_MAX_QUEUE_DEPTH)
1329 lp->qfull = 1;
1330 spin_unlock_bh(&fc->fcoe_pending_queue.lock);
1331 return;
1332 }
1333
1334 /**
1335 * fcoe_dev_setup() - setup link change notification interface
1336 */
1337 static void fcoe_dev_setup(void)
1338 {
1339 register_netdevice_notifier(&fcoe_notifier);
1340 }
1341
1342 /**
1343 * fcoe_dev_cleanup() - cleanup link change notification interface
1344 */
1345 static void fcoe_dev_cleanup(void)
1346 {
1347 unregister_netdevice_notifier(&fcoe_notifier);
1348 }
1349
1350 /**
1351 * fcoe_device_notification() - netdev event notification callback
1352 * @notifier: context of the notification
1353 * @event: type of event
1354 * @ptr: fixed array for output parsed ifname
1355 *
1356 * This function is called by the ethernet driver in case of link change event
1357 *
1358 * Returns: 0 for success
1359 */
1360 static int fcoe_device_notification(struct notifier_block *notifier,
1361 ulong event, void *ptr)
1362 {
1363 struct fc_lport *lp = NULL;
1364 struct net_device *real_dev = ptr;
1365 struct fcoe_softc *fc;
1366 struct fcoe_dev_stats *stats;
1367 u32 link_possible = 1;
1368 u32 mfs;
1369 int rc = NOTIFY_OK;
1370
1371 read_lock(&fcoe_hostlist_lock);
1372 list_for_each_entry(fc, &fcoe_hostlist, list) {
1373 if (fc->real_dev == real_dev) {
1374 lp = fc->ctlr.lp;
1375 break;
1376 }
1377 }
1378 read_unlock(&fcoe_hostlist_lock);
1379 if (lp == NULL) {
1380 rc = NOTIFY_DONE;
1381 goto out;
1382 }
1383
1384 switch (event) {
1385 case NETDEV_DOWN:
1386 case NETDEV_GOING_DOWN:
1387 link_possible = 0;
1388 break;
1389 case NETDEV_UP:
1390 case NETDEV_CHANGE:
1391 break;
1392 case NETDEV_CHANGEMTU:
1393 mfs = fc->real_dev->mtu -
1394 (sizeof(struct fcoe_hdr) +
1395 sizeof(struct fcoe_crc_eof));
1396 if (mfs >= FC_MIN_MAX_FRAME)
1397 fc_set_mfs(lp, mfs);
1398 break;
1399 case NETDEV_REGISTER:
1400 break;
1401 default:
1402 FC_DBG("Unknown event %ld from netdev netlink\n", event);
1403 }
1404 if (link_possible && !fcoe_link_ok(lp))
1405 fcoe_ctlr_link_up(&fc->ctlr);
1406 else if (fcoe_ctlr_link_down(&fc->ctlr)) {
1407 stats = fc_lport_get_stats(lp);
1408 stats->LinkFailureCount++;
1409 fcoe_clean_pending_queue(lp);
1410 }
1411 out:
1412 return rc;
1413 }
1414
1415 /**
1416 * fcoe_if_to_netdev() - parse a name buffer to get netdev
1417 * @buffer: incoming buffer to be copied
1418 *
1419 * Returns: NULL or ptr to net_device
1420 */
1421 static struct net_device *fcoe_if_to_netdev(const char *buffer)
1422 {
1423 char *cp;
1424 char ifname[IFNAMSIZ + 2];
1425
1426 if (buffer) {
1427 strlcpy(ifname, buffer, IFNAMSIZ);
1428 cp = ifname + strlen(ifname);
1429 while (--cp >= ifname && *cp == '\n')
1430 *cp = '\0';
1431 return dev_get_by_name(&init_net, ifname);
1432 }
1433 return NULL;
1434 }
1435
1436 /**
1437 * fcoe_netdev_to_module_owner() - finds out the driver module of the netdev
1438 * @netdev: the target netdev
1439 *
1440 * Returns: ptr to the struct module, NULL for failure
1441 */
1442 static struct module *
1443 fcoe_netdev_to_module_owner(const struct net_device *netdev)
1444 {
1445 struct device *dev;
1446
1447 if (!netdev)
1448 return NULL;
1449
1450 dev = netdev->dev.parent;
1451 if (!dev)
1452 return NULL;
1453
1454 if (!dev->driver)
1455 return NULL;
1456
1457 return dev->driver->owner;
1458 }
1459
1460 /**
1461 * fcoe_ethdrv_get() - Hold the Ethernet driver
1462 * @netdev: the target netdev
1463 *
1464 * Holds the Ethernet driver module by try_module_get() for
1465 * the corresponding netdev.
1466 *
1467 * Returns: 0 for success
1468 */
1469 static int fcoe_ethdrv_get(const struct net_device *netdev)
1470 {
1471 struct module *owner;
1472
1473 owner = fcoe_netdev_to_module_owner(netdev);
1474 if (owner) {
1475 printk(KERN_DEBUG "fcoe:hold driver module %s for %s\n",
1476 module_name(owner), netdev->name);
1477 return try_module_get(owner);
1478 }
1479 return -ENODEV;
1480 }
1481
1482 /**
1483 * fcoe_ethdrv_put() - Release the Ethernet driver
1484 * @netdev: the target netdev
1485 *
1486 * Releases the Ethernet driver module by module_put for
1487 * the corresponding netdev.
1488 *
1489 * Returns: 0 for success
1490 */
1491 static int fcoe_ethdrv_put(const struct net_device *netdev)
1492 {
1493 struct module *owner;
1494
1495 owner = fcoe_netdev_to_module_owner(netdev);
1496 if (owner) {
1497 printk(KERN_DEBUG "fcoe:release driver module %s for %s\n",
1498 module_name(owner), netdev->name);
1499 module_put(owner);
1500 return 0;
1501 }
1502 return -ENODEV;
1503 }
1504
1505 /**
1506 * fcoe_destroy() - handles the destroy from sysfs
1507 * @buffer: expected to be an eth if name
1508 * @kp: associated kernel param
1509 *
1510 * Returns: 0 for success
1511 */
1512 static int fcoe_destroy(const char *buffer, struct kernel_param *kp)
1513 {
1514 int rc;
1515 struct net_device *netdev;
1516
1517 netdev = fcoe_if_to_netdev(buffer);
1518 if (!netdev) {
1519 rc = -ENODEV;
1520 goto out_nodev;
1521 }
1522 /* look for existing lport */
1523 if (!fcoe_hostlist_lookup(netdev)) {
1524 rc = -ENODEV;
1525 goto out_putdev;
1526 }
1527 rc = fcoe_if_destroy(netdev);
1528 if (rc) {
1529 printk(KERN_ERR "fcoe: fcoe_if_destroy(%s) failed\n",
1530 netdev->name);
1531 rc = -EIO;
1532 goto out_putdev;
1533 }
1534 fcoe_ethdrv_put(netdev);
1535 rc = 0;
1536 out_putdev:
1537 dev_put(netdev);
1538 out_nodev:
1539 return rc;
1540 }
1541
1542 /**
1543 * fcoe_create() - Handles the create call from sysfs
1544 * @buffer: expected to be an eth if name
1545 * @kp: associated kernel param
1546 *
1547 * Returns: 0 for success
1548 */
1549 static int fcoe_create(const char *buffer, struct kernel_param *kp)
1550 {
1551 int rc;
1552 struct net_device *netdev;
1553
1554 netdev = fcoe_if_to_netdev(buffer);
1555 if (!netdev) {
1556 rc = -ENODEV;
1557 goto out_nodev;
1558 }
1559 /* look for existing lport */
1560 if (fcoe_hostlist_lookup(netdev)) {
1561 rc = -EEXIST;
1562 goto out_putdev;
1563 }
1564 fcoe_ethdrv_get(netdev);
1565
1566 rc = fcoe_if_create(netdev);
1567 if (rc) {
1568 printk(KERN_ERR "fcoe: fcoe_if_create(%s) failed\n",
1569 netdev->name);
1570 fcoe_ethdrv_put(netdev);
1571 rc = -EIO;
1572 goto out_putdev;
1573 }
1574 rc = 0;
1575 out_putdev:
1576 dev_put(netdev);
1577 out_nodev:
1578 return rc;
1579 }
1580
1581 module_param_call(create, fcoe_create, NULL, NULL, S_IWUSR);
1582 __MODULE_PARM_TYPE(create, "string");
1583 MODULE_PARM_DESC(create, "Create fcoe port using net device passed in.");
1584 module_param_call(destroy, fcoe_destroy, NULL, NULL, S_IWUSR);
1585 __MODULE_PARM_TYPE(destroy, "string");
1586 MODULE_PARM_DESC(destroy, "Destroy fcoe port");
1587
1588 /**
1589 * fcoe_link_ok() - Check if link is ok for the fc_lport
1590 * @lp: ptr to the fc_lport
1591 *
1592 * Any permanently-disqualifying conditions have been previously checked.
1593 * This also updates the speed setting, which may change with link for 100/1000.
1594 *
1595 * This function should probably be checking for PAUSE support at some point
1596 * in the future. Currently Per-priority-pause is not determinable using
1597 * ethtool, so we shouldn't be restrictive until that problem is resolved.
1598 *
1599 * Returns: 0 if link is OK for use by FCoE.
1600 *
1601 */
1602 int fcoe_link_ok(struct fc_lport *lp)
1603 {
1604 struct fcoe_softc *fc = lport_priv(lp);
1605 struct net_device *dev = fc->real_dev;
1606 struct ethtool_cmd ecmd = { ETHTOOL_GSET };
1607 int rc = 0;
1608
1609 if ((dev->flags & IFF_UP) && netif_carrier_ok(dev)) {
1610 dev = fc->phys_dev;
1611 if (dev->ethtool_ops->get_settings) {
1612 dev->ethtool_ops->get_settings(dev, &ecmd);
1613 lp->link_supported_speeds &=
1614 ~(FC_PORTSPEED_1GBIT | FC_PORTSPEED_10GBIT);
1615 if (ecmd.supported & (SUPPORTED_1000baseT_Half |
1616 SUPPORTED_1000baseT_Full))
1617 lp->link_supported_speeds |= FC_PORTSPEED_1GBIT;
1618 if (ecmd.supported & SUPPORTED_10000baseT_Full)
1619 lp->link_supported_speeds |=
1620 FC_PORTSPEED_10GBIT;
1621 if (ecmd.speed == SPEED_1000)
1622 lp->link_speed = FC_PORTSPEED_1GBIT;
1623 if (ecmd.speed == SPEED_10000)
1624 lp->link_speed = FC_PORTSPEED_10GBIT;
1625 }
1626 } else
1627 rc = -1;
1628
1629 return rc;
1630 }
1631
1632 /**
1633 * fcoe_percpu_clean() - Clear the pending skbs for an lport
1634 * @lp: the fc_lport
1635 */
1636 void fcoe_percpu_clean(struct fc_lport *lp)
1637 {
1638 struct fcoe_percpu_s *pp;
1639 struct fcoe_rcv_info *fr;
1640 struct sk_buff_head *list;
1641 struct sk_buff *skb, *next;
1642 struct sk_buff *head;
1643 unsigned int cpu;
1644
1645 for_each_possible_cpu(cpu) {
1646 pp = &per_cpu(fcoe_percpu, cpu);
1647 spin_lock_bh(&pp->fcoe_rx_list.lock);
1648 list = &pp->fcoe_rx_list;
1649 head = list->next;
1650 for (skb = head; skb != (struct sk_buff *)list;
1651 skb = next) {
1652 next = skb->next;
1653 fr = fcoe_dev_from_skb(skb);
1654 if (fr->fr_dev == lp) {
1655 __skb_unlink(skb, list);
1656 kfree_skb(skb);
1657 }
1658 }
1659 spin_unlock_bh(&pp->fcoe_rx_list.lock);
1660 }
1661 }
1662
1663 /**
1664 * fcoe_clean_pending_queue() - Dequeue a skb and free it
1665 * @lp: the corresponding fc_lport
1666 *
1667 * Returns: none
1668 */
1669 void fcoe_clean_pending_queue(struct fc_lport *lp)
1670 {
1671 struct fcoe_softc *fc = lport_priv(lp);
1672 struct sk_buff *skb;
1673
1674 spin_lock_bh(&fc->fcoe_pending_queue.lock);
1675 while ((skb = __skb_dequeue(&fc->fcoe_pending_queue)) != NULL) {
1676 spin_unlock_bh(&fc->fcoe_pending_queue.lock);
1677 kfree_skb(skb);
1678 spin_lock_bh(&fc->fcoe_pending_queue.lock);
1679 }
1680 spin_unlock_bh(&fc->fcoe_pending_queue.lock);
1681 }
1682
1683 /**
1684 * fcoe_reset() - Resets the fcoe
1685 * @shost: shost the reset is from
1686 *
1687 * Returns: always 0
1688 */
1689 int fcoe_reset(struct Scsi_Host *shost)
1690 {
1691 struct fc_lport *lport = shost_priv(shost);
1692 fc_lport_reset(lport);
1693 return 0;
1694 }
1695
1696 /**
1697 * fcoe_hostlist_lookup_softc() - find the corresponding lport by a given device
1698 * @dev: this is currently ptr to net_device
1699 *
1700 * Returns: NULL or the located fcoe_softc
1701 */
1702 static struct fcoe_softc *
1703 fcoe_hostlist_lookup_softc(const struct net_device *dev)
1704 {
1705 struct fcoe_softc *fc;
1706
1707 read_lock(&fcoe_hostlist_lock);
1708 list_for_each_entry(fc, &fcoe_hostlist, list) {
1709 if (fc->real_dev == dev) {
1710 read_unlock(&fcoe_hostlist_lock);
1711 return fc;
1712 }
1713 }
1714 read_unlock(&fcoe_hostlist_lock);
1715 return NULL;
1716 }
1717
1718 /**
1719 * fcoe_hostlist_lookup() - Find the corresponding lport by netdev
1720 * @netdev: ptr to net_device
1721 *
1722 * Returns: 0 for success
1723 */
1724 struct fc_lport *fcoe_hostlist_lookup(const struct net_device *netdev)
1725 {
1726 struct fcoe_softc *fc;
1727
1728 fc = fcoe_hostlist_lookup_softc(netdev);
1729
1730 return (fc) ? fc->ctlr.lp : NULL;
1731 }
1732
1733 /**
1734 * fcoe_hostlist_add() - Add a lport to lports list
1735 * @lp: ptr to the fc_lport to be added
1736 *
1737 * Returns: 0 for success
1738 */
1739 int fcoe_hostlist_add(const struct fc_lport *lp)
1740 {
1741 struct fcoe_softc *fc;
1742
1743 fc = fcoe_hostlist_lookup_softc(fcoe_netdev(lp));
1744 if (!fc) {
1745 fc = lport_priv(lp);
1746 write_lock_bh(&fcoe_hostlist_lock);
1747 list_add_tail(&fc->list, &fcoe_hostlist);
1748 write_unlock_bh(&fcoe_hostlist_lock);
1749 }
1750 return 0;
1751 }
1752
1753 /**
1754 * fcoe_hostlist_remove() - remove a lport from lports list
1755 * @lp: ptr to the fc_lport to be removed
1756 *
1757 * Returns: 0 for success
1758 */
1759 int fcoe_hostlist_remove(const struct fc_lport *lp)
1760 {
1761 struct fcoe_softc *fc;
1762
1763 fc = fcoe_hostlist_lookup_softc(fcoe_netdev(lp));
1764 BUG_ON(!fc);
1765 write_lock_bh(&fcoe_hostlist_lock);
1766 list_del(&fc->list);
1767 write_unlock_bh(&fcoe_hostlist_lock);
1768
1769 return 0;
1770 }
1771
1772 /**
1773 * fcoe_init() - fcoe module loading initialization
1774 *
1775 * Returns 0 on success, negative on failure
1776 */
1777 static int __init fcoe_init(void)
1778 {
1779 unsigned int cpu;
1780 int rc = 0;
1781 struct fcoe_percpu_s *p;
1782
1783 INIT_LIST_HEAD(&fcoe_hostlist);
1784 rwlock_init(&fcoe_hostlist_lock);
1785
1786 for_each_possible_cpu(cpu) {
1787 p = &per_cpu(fcoe_percpu, cpu);
1788 skb_queue_head_init(&p->fcoe_rx_list);
1789 }
1790
1791 for_each_online_cpu(cpu)
1792 fcoe_percpu_thread_create(cpu);
1793
1794 /* Initialize per CPU interrupt thread */
1795 rc = register_hotcpu_notifier(&fcoe_cpu_notifier);
1796 if (rc)
1797 goto out_free;
1798
1799 /* Setup link change notification */
1800 fcoe_dev_setup();
1801
1802 fcoe_if_init();
1803
1804 return 0;
1805
1806 out_free:
1807 for_each_online_cpu(cpu) {
1808 fcoe_percpu_thread_destroy(cpu);
1809 }
1810
1811 return rc;
1812 }
1813 module_init(fcoe_init);
1814
1815 /**
1816 * fcoe_exit() - fcoe module unloading cleanup
1817 *
1818 * Returns 0 on success, negative on failure
1819 */
1820 static void __exit fcoe_exit(void)
1821 {
1822 unsigned int cpu;
1823 struct fcoe_softc *fc, *tmp;
1824
1825 fcoe_dev_cleanup();
1826
1827 /* releases the associated fcoe hosts */
1828 list_for_each_entry_safe(fc, tmp, &fcoe_hostlist, list)
1829 fcoe_if_destroy(fc->real_dev);
1830
1831 unregister_hotcpu_notifier(&fcoe_cpu_notifier);
1832
1833 for_each_online_cpu(cpu) {
1834 fcoe_percpu_thread_destroy(cpu);
1835 }
1836
1837 /* detach from scsi transport */
1838 fcoe_if_exit();
1839 }
1840 module_exit(fcoe_exit);
This page took 0.121547 seconds and 6 git commands to generate.