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