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