blob: ebf2e20370d768b2e08e8a305347d0ab51d1c9a2 [file] [log] [blame]
Robert Love85b4aa42008-12-09 15:10:24 -08001/*
2 * Copyright(c) 2007 - 2008 Intel Corporation. All rights reserved.
3 *
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms and conditions of the GNU General Public License,
6 * version 2, as published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope it will be useful, but WITHOUT
9 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
10 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
11 * more details.
12 *
13 * You should have received a copy of the GNU General Public License along with
14 * this program; if not, write to the Free Software Foundation, Inc.,
15 * 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
16 *
17 * Maintained at www.Open-FCoE.org
18 */
19
20#include <linux/module.h>
21#include <linux/version.h>
Robert Love85b4aa42008-12-09 15:10:24 -080022#include <linux/spinlock.h>
Robert Love85b4aa42008-12-09 15:10:24 -080023#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>
Robert Love85b4aa42008-12-09 15:10:24 -080028#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>
Joe Eykholt97c83892009-03-17 11:42:40 -070040#include <scsi/fc/fc_fip.h>
Robert Love85b4aa42008-12-09 15:10:24 -080041
42#include <scsi/libfc.h>
43#include <scsi/fc_frame.h>
44#include <scsi/libfcoe.h>
Robert Love85b4aa42008-12-09 15:10:24 -080045
Vasu Devfdd78022009-03-17 11:42:24 -070046#include "fcoe.h"
47
Robert Love85b4aa42008-12-09 15:10:24 -080048MODULE_AUTHOR("Open-FCoE.org");
49MODULE_DESCRIPTION("FCoE");
Vasu Dev9b34ecf2009-03-17 11:42:13 -070050MODULE_LICENSE("GPL v2");
Robert Love85b4aa42008-12-09 15:10:24 -080051
52/* fcoe host list */
53LIST_HEAD(fcoe_hostlist);
54DEFINE_RWLOCK(fcoe_hostlist_lock);
Robert Love5e5e92d2009-03-17 11:41:35 -070055DEFINE_PER_CPU(struct fcoe_percpu_s, fcoe_percpu);
Robert Love85b4aa42008-12-09 15:10:24 -080056
Chris Leechdd3fd722009-04-21 16:27:36 -070057/* Function Prototypes */
Vasu Devfdd78022009-03-17 11:42:24 -070058static int fcoe_reset(struct Scsi_Host *shost);
59static int fcoe_xmit(struct fc_lport *, struct fc_frame *);
60static int fcoe_rcv(struct sk_buff *, struct net_device *,
61 struct packet_type *, struct net_device *);
62static int fcoe_percpu_receive_thread(void *arg);
63static void fcoe_clean_pending_queue(struct fc_lport *lp);
64static void fcoe_percpu_clean(struct fc_lport *lp);
65static int fcoe_link_ok(struct fc_lport *lp);
66
67static struct fc_lport *fcoe_hostlist_lookup(const struct net_device *);
68static int fcoe_hostlist_add(const struct fc_lport *);
69static int fcoe_hostlist_remove(const struct fc_lport *);
70
Vasu Dev4bb6b512009-05-06 10:52:34 -070071static void fcoe_check_wait_queue(struct fc_lport *, struct sk_buff *);
Robert Love85b4aa42008-12-09 15:10:24 -080072static int fcoe_device_notification(struct notifier_block *, ulong, void *);
73static void fcoe_dev_setup(void);
74static void fcoe_dev_cleanup(void);
75
76/* notification function from net device */
77static struct notifier_block fcoe_notifier = {
78 .notifier_call = fcoe_device_notification,
79};
80
Vasu Dev7f349142009-03-27 09:06:31 -070081static struct scsi_transport_template *scsi_transport_fcoe_sw;
82
83struct 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
116static 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/**
Vasu Devab6b85c2009-05-17 12:33:08 +0000136 * 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 */
144static 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 */
160static 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 */
175static 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))
Jiri Pirkoccffad252009-05-22 23:22:17 +0000182 dev_unicast_delete(fc->real_dev, old);
183 dev_unicast_add(fc->real_dev, new);
Vasu Devab6b85c2009-05-17 12:33:08 +0000184 rtnl_unlock();
185}
186
187/**
Vasu Dev7f349142009-03-27 09:06:31 -0700188 * fcoe_lport_config() - sets up the fc_lport
189 * @lp: ptr to the fc_lport
Vasu Dev7f349142009-03-27 09:06:31 -0700190 *
191 * Returns: 0 for success
192 */
193static int fcoe_lport_config(struct fc_lport *lp)
194{
195 lp->link_up = 0;
196 lp->qfull = 0;
197 lp->max_retry_count = 3;
Abhijeet Joglekara3666952009-05-01 10:01:26 -0700198 lp->max_rport_retry_count = 3;
Vasu Dev7f349142009-03-27 09:06:31 -0700199 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/**
Vasu Devab6b85c2009-05-17 12:33:08 +0000220 * fcoe_netdev_cleanup() - clean up netdev configurations
221 * @fc: ptr to the fcoe_softc
222 */
223void 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);
Jiri Pirkoccffad252009-05-22 23:22:17 +0000234 dev_unicast_delete(fc->real_dev, flogi_maddr);
Vasu Devab6b85c2009-05-17 12:33:08 +0000235 if (!is_zero_ether_addr(fc->ctlr.data_src_addr))
Jiri Pirkoccffad252009-05-22 23:22:17 +0000236 dev_unicast_delete(fc->real_dev, fc->ctlr.data_src_addr);
Vasu Dev184dd342009-05-17 12:33:28 +0000237 if (fc->ctlr.spma)
Jiri Pirkoccffad252009-05-22 23:22:17 +0000238 dev_unicast_delete(fc->real_dev, fc->ctlr.ctl_src_addr);
Vasu Devab6b85c2009-05-17 12:33:08 +0000239 dev_mc_delete(fc->real_dev, FIP_ALL_ENODE_MACS, ETH_ALEN, 0);
240 rtnl_unlock();
241}
242
243/**
Vasu Dev1047f222009-05-06 10:52:40 -0700244 * fcoe_queue_timer() - fcoe queue timer
245 * @lp: the fc_lport pointer
246 *
247 * Calls fcoe_check_wait_queue on timeout
248 *
249 */
250static void fcoe_queue_timer(ulong lp)
251{
252 fcoe_check_wait_queue((struct fc_lport *)lp, NULL);
253}
254
255/**
Vasu Dev7f349142009-03-27 09:06:31 -0700256 * 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 */
264static 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];
Vasu Dev184dd342009-05-17 12:33:28 +0000270 struct netdev_hw_addr *ha;
Vasu Dev7f349142009-03-27 09:06:31 -0700271
272 /* Setup lport private data to point to fcoe softc */
273 fc = lport_priv(lp);
Joe Eykholt97c83892009-03-17 11:42:40 -0700274 fc->ctlr.lp = lp;
Vasu Dev7f349142009-03-27 09:06:31 -0700275 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
Vasu Dev7f349142009-03-27 09:06:31 -0700299 /* 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;
Robert Loved5488eb2009-06-10 15:30:59 -0700306 FCOE_NETDEV_DBG(netdev, "Supports FCCRC offload\n");
Vasu Dev7f349142009-03-27 09:06:31 -0700307 }
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;
Robert Loved5488eb2009-06-10 15:30:59 -0700313 FCOE_NETDEV_DBG(netdev, "Supports LSO for max len 0x%x\n",
314 lp->lso_max);
Vasu Dev7f349142009-03-27 09:06:31 -0700315 }
316#endif
317 if (netdev->fcoe_ddp_xid) {
318 lp->lro_enabled = 1;
319 lp->lro_xid = netdev->fcoe_ddp_xid;
Robert Loved5488eb2009-06-10 15:30:59 -0700320 FCOE_NETDEV_DBG(netdev, "Supports LRO for max xid 0x%x\n",
321 lp->lro_xid);
Vasu Dev7f349142009-03-27 09:06:31 -0700322 }
323 skb_queue_head_init(&fc->fcoe_pending_queue);
324 fc->fcoe_pending_queue_active = 0;
Vasu Dev1047f222009-05-06 10:52:40 -0700325 setup_timer(&fc->timer, fcoe_queue_timer, (unsigned long)lp);
Vasu Dev7f349142009-03-27 09:06:31 -0700326
Vasu Dev184dd342009-05-17 12:33:28 +0000327 /* 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) &&
Yi Zou7a7f0c72009-07-29 17:03:50 -0700332 (is_valid_ether_addr(ha->addr))) {
Vasu Dev184dd342009-05-17 12:33:28 +0000333 memcpy(fc->ctlr.ctl_src_addr, ha->addr, ETH_ALEN);
334 fc->ctlr.spma = 1;
335 break;
336 }
337 }
338 rcu_read_unlock();
339
Vasu Dev7f349142009-03-27 09:06:31 -0700340 /* setup Source Mac Address */
Vasu Dev184dd342009-05-17 12:33:28 +0000341 if (!fc->ctlr.spma)
342 memcpy(fc->ctlr.ctl_src_addr, fc->real_dev->dev_addr,
343 fc->real_dev->addr_len);
Vasu Dev7f349142009-03-27 09:06:31 -0700344
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);
Jiri Pirkoccffad252009-05-22 23:22:17 +0000358 dev_unicast_add(fc->real_dev, flogi_maddr);
Vasu Dev184dd342009-05-17 12:33:28 +0000359 if (fc->ctlr.spma)
Jiri Pirkoccffad252009-05-22 23:22:17 +0000360 dev_unicast_add(fc->real_dev, fc->ctlr.ctl_src_addr);
Joe Eykholt6401bdc2009-04-21 16:27:46 -0700361 dev_mc_add(fc->real_dev, FIP_ALL_ENODE_MACS, ETH_ALEN, 0);
Vasu Dev7f349142009-03-27 09:06:31 -0700362 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
Vasu Devab6b85c2009-05-17 12:33:08 +0000373 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
Vasu Dev7f349142009-03-27 09:06:31 -0700378 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 */
391static 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) {
Robert Loved5488eb2009-06-10 15:30:59 -0700407 FCOE_NETDEV_DBG(fcoe_netdev(lp), "fcoe_shost_config: "
408 "error on scsi_add_host\n");
Vasu Dev7f349142009-03-27 09:06:31 -0700409 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 */
424static inline int fcoe_em_config(struct fc_lport *lp)
425{
Vasu Dev52ff8782009-07-29 17:05:10 -0700426 if (!fc_exch_mgr_alloc(lp, FC_CLASS_3, FCOE_MIN_XID,
427 FCOE_MAX_XID, NULL))
Vasu Dev7f349142009-03-27 09:06:31 -0700428 return -ENOMEM;
429
430 return 0;
431}
432
433/**
434 * fcoe_if_destroy() - FCoE software HBA tear-down function
435 * @netdev: ptr to the associated net_device
436 *
437 * Returns: 0 if link is OK for use by FCoE.
438 */
439static int fcoe_if_destroy(struct net_device *netdev)
440{
441 struct fc_lport *lp = NULL;
442 struct fcoe_softc *fc;
Vasu Dev7f349142009-03-27 09:06:31 -0700443
444 BUG_ON(!netdev);
445
Robert Loved5488eb2009-06-10 15:30:59 -0700446 FCOE_NETDEV_DBG(netdev, "Destroying interface\n");
Vasu Dev7f349142009-03-27 09:06:31 -0700447
448 lp = fcoe_hostlist_lookup(netdev);
449 if (!lp)
450 return -ENODEV;
451
452 fc = lport_priv(lp);
453
454 /* Logout of the fabric */
455 fc_fabric_logoff(lp);
456
457 /* Remove the instance from fcoe's list */
458 fcoe_hostlist_remove(lp);
459
Vasu Devab6b85c2009-05-17 12:33:08 +0000460 /* clean up netdev configurations */
461 fcoe_netdev_cleanup(fc);
462
463 /* tear-down the FCoE controller */
Joe Eykholt97c83892009-03-17 11:42:40 -0700464 fcoe_ctlr_destroy(&fc->ctlr);
Vasu Dev7f349142009-03-27 09:06:31 -0700465
Joe Eykholtf161fb72009-07-29 17:04:17 -0700466 /* Free queued packets for the per-CPU receive threads */
467 fcoe_percpu_clean(lp);
468
Vasu Dev7f349142009-03-27 09:06:31 -0700469 /* Cleanup the fc_lport */
470 fc_lport_destroy(lp);
471 fc_fcp_destroy(lp);
472
473 /* Detach from the scsi-ml */
474 fc_remove_host(lp->host);
475 scsi_remove_host(lp->host);
476
477 /* There are no more rports or I/O, free the EM */
Vasu Dev52ff8782009-07-29 17:05:10 -0700478 fc_exch_mgr_free(lp);
Vasu Dev7f349142009-03-27 09:06:31 -0700479
Vasu Dev7f349142009-03-27 09:06:31 -0700480 /* Free existing skbs */
481 fcoe_clean_pending_queue(lp);
482
Vasu Dev1047f222009-05-06 10:52:40 -0700483 /* Stop the timer */
484 del_timer_sync(&fc->timer);
485
Vasu Dev7f349142009-03-27 09:06:31 -0700486 /* Free memory used by statistical counters */
487 fc_lport_free_stats(lp);
488
489 /* Release the net_device and Scsi_Host */
490 dev_put(fc->real_dev);
491 scsi_host_put(lp->host);
492
493 return 0;
494}
495
496/*
497 * fcoe_ddp_setup - calls LLD's ddp_setup through net_device
498 * @lp: the corresponding fc_lport
499 * @xid: the exchange id for this ddp transfer
500 * @sgl: the scatterlist describing this transfer
501 * @sgc: number of sg items
502 *
503 * Returns : 0 no ddp
504 */
505static int fcoe_ddp_setup(struct fc_lport *lp, u16 xid,
506 struct scatterlist *sgl, unsigned int sgc)
507{
508 struct net_device *n = fcoe_netdev(lp);
509
510 if (n->netdev_ops && n->netdev_ops->ndo_fcoe_ddp_setup)
511 return n->netdev_ops->ndo_fcoe_ddp_setup(n, xid, sgl, sgc);
512
513 return 0;
514}
515
516/*
517 * fcoe_ddp_done - calls LLD's ddp_done through net_device
518 * @lp: the corresponding fc_lport
519 * @xid: the exchange id for this ddp transfer
520 *
521 * Returns : the length of data that have been completed by ddp
522 */
523static int fcoe_ddp_done(struct fc_lport *lp, u16 xid)
524{
525 struct net_device *n = fcoe_netdev(lp);
526
527 if (n->netdev_ops && n->netdev_ops->ndo_fcoe_ddp_done)
528 return n->netdev_ops->ndo_fcoe_ddp_done(n, xid);
529 return 0;
530}
531
532static struct libfc_function_template fcoe_libfc_fcn_templ = {
533 .frame_send = fcoe_xmit,
534 .ddp_setup = fcoe_ddp_setup,
535 .ddp_done = fcoe_ddp_done,
536};
537
538/**
539 * fcoe_if_create() - this function creates the fcoe interface
540 * @netdev: pointer the associated netdevice
541 *
542 * Creates fc_lport struct and scsi_host for lport, configures lport
543 * and starts fabric login.
544 *
545 * Returns : 0 on success
546 */
547static int fcoe_if_create(struct net_device *netdev)
548{
549 int rc;
550 struct fc_lport *lp = NULL;
551 struct fcoe_softc *fc;
552 struct Scsi_Host *shost;
553
554 BUG_ON(!netdev);
555
Robert Loved5488eb2009-06-10 15:30:59 -0700556 FCOE_NETDEV_DBG(netdev, "Create Interface\n");
Vasu Dev7f349142009-03-27 09:06:31 -0700557
558 lp = fcoe_hostlist_lookup(netdev);
559 if (lp)
560 return -EEXIST;
561
Vasu Deva0a25da2009-03-17 11:42:29 -0700562 shost = libfc_host_alloc(&fcoe_shost_template,
563 sizeof(struct fcoe_softc));
Vasu Dev7f349142009-03-27 09:06:31 -0700564 if (!shost) {
Robert Loved5488eb2009-06-10 15:30:59 -0700565 FCOE_NETDEV_DBG(netdev, "Could not allocate host structure\n");
Vasu Dev7f349142009-03-27 09:06:31 -0700566 return -ENOMEM;
567 }
568 lp = shost_priv(shost);
569 fc = lport_priv(lp);
570
571 /* configure fc_lport, e.g., em */
572 rc = fcoe_lport_config(lp);
573 if (rc) {
Robert Loved5488eb2009-06-10 15:30:59 -0700574 FCOE_NETDEV_DBG(netdev, "Could not configure lport for the "
575 "interface\n");
Vasu Dev7f349142009-03-27 09:06:31 -0700576 goto out_host_put;
577 }
578
Joe Eykholt97c83892009-03-17 11:42:40 -0700579 /*
580 * Initialize FIP.
581 */
582 fcoe_ctlr_init(&fc->ctlr);
583 fc->ctlr.send = fcoe_fip_send;
584 fc->ctlr.update_mac = fcoe_update_src_mac;
585
Vasu Devab6b85c2009-05-17 12:33:08 +0000586 /* configure lport network properties */
587 rc = fcoe_netdev_config(lp, netdev);
588 if (rc) {
Robert Loved5488eb2009-06-10 15:30:59 -0700589 FCOE_NETDEV_DBG(netdev, "Could not configure netdev for the "
590 "interface\n");
Vasu Devab6b85c2009-05-17 12:33:08 +0000591 goto out_netdev_cleanup;
592 }
Joe Eykholt97c83892009-03-17 11:42:40 -0700593
Vasu Dev7f349142009-03-27 09:06:31 -0700594 /* configure lport scsi host properties */
595 rc = fcoe_shost_config(lp, shost, &netdev->dev);
596 if (rc) {
Robert Loved5488eb2009-06-10 15:30:59 -0700597 FCOE_NETDEV_DBG(netdev, "Could not configure shost for the "
598 "interface\n");
Vasu Devab6b85c2009-05-17 12:33:08 +0000599 goto out_netdev_cleanup;
Vasu Dev7f349142009-03-27 09:06:31 -0700600 }
601
Vasu Dev7f349142009-03-27 09:06:31 -0700602 /* Initialize the library */
603 rc = fcoe_libfc_config(lp, &fcoe_libfc_fcn_templ);
604 if (rc) {
Robert Loved5488eb2009-06-10 15:30:59 -0700605 FCOE_NETDEV_DBG(netdev, "Could not configure libfc for the "
606 "interface\n");
Vasu Dev7f349142009-03-27 09:06:31 -0700607 goto out_lp_destroy;
608 }
609
Vasu Dev96316092009-07-29 17:05:00 -0700610 /* lport exch manager allocation */
611 rc = fcoe_em_config(lp);
612 if (rc) {
613 FCOE_NETDEV_DBG(netdev, "Could not configure the EM for the "
614 "interface\n");
615 goto out_lp_destroy;
616 }
617
Vasu Dev7f349142009-03-27 09:06:31 -0700618 /* add to lports list */
619 fcoe_hostlist_add(lp);
620
621 lp->boot_time = jiffies;
622
623 fc_fabric_login(lp);
624
Joe Eykholt97c83892009-03-17 11:42:40 -0700625 if (!fcoe_link_ok(lp))
626 fcoe_ctlr_link_up(&fc->ctlr);
627
Vasu Dev7f349142009-03-27 09:06:31 -0700628 dev_hold(netdev);
629
630 return rc;
631
632out_lp_destroy:
Vasu Dev52ff8782009-07-29 17:05:10 -0700633 fc_exch_mgr_free(lp);
Vasu Devab6b85c2009-05-17 12:33:08 +0000634out_netdev_cleanup:
635 fcoe_netdev_cleanup(fc);
Vasu Dev7f349142009-03-27 09:06:31 -0700636out_host_put:
637 scsi_host_put(lp->host);
638 return rc;
639}
640
641/**
642 * fcoe_if_init() - attach to scsi transport
643 *
644 * Returns : 0 on success
645 */
646static int __init fcoe_if_init(void)
647{
648 /* attach to scsi transport */
649 scsi_transport_fcoe_sw =
650 fc_attach_transport(&fcoe_transport_function);
651
652 if (!scsi_transport_fcoe_sw) {
Robert Loved5488eb2009-06-10 15:30:59 -0700653 printk(KERN_ERR "fcoe: Failed to attach to the FC transport\n");
Vasu Dev7f349142009-03-27 09:06:31 -0700654 return -ENODEV;
655 }
656
657 return 0;
658}
659
660/**
661 * fcoe_if_exit() - detach from scsi transport
662 *
663 * Returns : 0 on success
664 */
665int __exit fcoe_if_exit(void)
666{
667 fc_release_transport(scsi_transport_fcoe_sw);
668 return 0;
669}
670
Robert Love85b4aa42008-12-09 15:10:24 -0800671/**
Robert Love8976f422009-03-17 11:41:46 -0700672 * fcoe_percpu_thread_create() - Create a receive thread for an online cpu
673 * @cpu: cpu index for the online cpu
674 */
675static void fcoe_percpu_thread_create(unsigned int cpu)
676{
677 struct fcoe_percpu_s *p;
678 struct task_struct *thread;
679
680 p = &per_cpu(fcoe_percpu, cpu);
681
682 thread = kthread_create(fcoe_percpu_receive_thread,
683 (void *)p, "fcoethread/%d", cpu);
684
685 if (likely(!IS_ERR(p->thread))) {
686 kthread_bind(thread, cpu);
687 wake_up_process(thread);
688
689 spin_lock_bh(&p->fcoe_rx_list.lock);
690 p->thread = thread;
691 spin_unlock_bh(&p->fcoe_rx_list.lock);
692 }
693}
694
695/**
696 * fcoe_percpu_thread_destroy() - removes the rx thread for the given cpu
697 * @cpu: cpu index the rx thread is to be removed
698 *
699 * Destroys a per-CPU Rx thread. Any pending skbs are moved to the
700 * current CPU's Rx thread. If the thread being destroyed is bound to
701 * the CPU processing this context the skbs will be freed.
702 */
703static void fcoe_percpu_thread_destroy(unsigned int cpu)
704{
705 struct fcoe_percpu_s *p;
706 struct task_struct *thread;
707 struct page *crc_eof;
708 struct sk_buff *skb;
709#ifdef CONFIG_SMP
710 struct fcoe_percpu_s *p0;
711 unsigned targ_cpu = smp_processor_id();
712#endif /* CONFIG_SMP */
713
Robert Loved5488eb2009-06-10 15:30:59 -0700714 FCOE_DBG("Destroying receive thread for CPU %d\n", cpu);
Robert Love8976f422009-03-17 11:41:46 -0700715
716 /* Prevent any new skbs from being queued for this CPU. */
717 p = &per_cpu(fcoe_percpu, cpu);
718 spin_lock_bh(&p->fcoe_rx_list.lock);
719 thread = p->thread;
720 p->thread = NULL;
721 crc_eof = p->crc_eof_page;
722 p->crc_eof_page = NULL;
723 p->crc_eof_offset = 0;
724 spin_unlock_bh(&p->fcoe_rx_list.lock);
725
726#ifdef CONFIG_SMP
727 /*
728 * Don't bother moving the skb's if this context is running
729 * on the same CPU that is having its thread destroyed. This
730 * can easily happen when the module is removed.
731 */
732 if (cpu != targ_cpu) {
733 p0 = &per_cpu(fcoe_percpu, targ_cpu);
734 spin_lock_bh(&p0->fcoe_rx_list.lock);
735 if (p0->thread) {
Robert Loved5488eb2009-06-10 15:30:59 -0700736 FCOE_DBG("Moving frames from CPU %d to CPU %d\n",
737 cpu, targ_cpu);
Robert Love8976f422009-03-17 11:41:46 -0700738
739 while ((skb = __skb_dequeue(&p->fcoe_rx_list)) != NULL)
740 __skb_queue_tail(&p0->fcoe_rx_list, skb);
741 spin_unlock_bh(&p0->fcoe_rx_list.lock);
742 } else {
743 /*
744 * The targeted CPU is not initialized and cannot accept
745 * new skbs. Unlock the targeted CPU and drop the skbs
746 * on the CPU that is going offline.
747 */
748 while ((skb = __skb_dequeue(&p->fcoe_rx_list)) != NULL)
749 kfree_skb(skb);
750 spin_unlock_bh(&p0->fcoe_rx_list.lock);
751 }
752 } else {
753 /*
754 * This scenario occurs when the module is being removed
755 * and all threads are being destroyed. skbs will continue
756 * to be shifted from the CPU thread that is being removed
757 * to the CPU thread associated with the CPU that is processing
758 * the module removal. Once there is only one CPU Rx thread it
759 * will reach this case and we will drop all skbs and later
760 * stop the thread.
761 */
762 spin_lock_bh(&p->fcoe_rx_list.lock);
763 while ((skb = __skb_dequeue(&p->fcoe_rx_list)) != NULL)
764 kfree_skb(skb);
765 spin_unlock_bh(&p->fcoe_rx_list.lock);
766 }
767#else
768 /*
Chris Leechdd3fd722009-04-21 16:27:36 -0700769 * This a non-SMP scenario where the singular Rx thread is
Robert Love8976f422009-03-17 11:41:46 -0700770 * being removed. Free all skbs and stop the thread.
771 */
772 spin_lock_bh(&p->fcoe_rx_list.lock);
773 while ((skb = __skb_dequeue(&p->fcoe_rx_list)) != NULL)
774 kfree_skb(skb);
775 spin_unlock_bh(&p->fcoe_rx_list.lock);
776#endif
777
778 if (thread)
779 kthread_stop(thread);
780
781 if (crc_eof)
782 put_page(crc_eof);
783}
784
785/**
786 * fcoe_cpu_callback() - fcoe cpu hotplug event callback
787 * @nfb: callback data block
788 * @action: event triggering the callback
789 * @hcpu: index for the cpu of this event
790 *
791 * This creates or destroys per cpu data for fcoe
792 *
793 * Returns NOTIFY_OK always.
794 */
795static int fcoe_cpu_callback(struct notifier_block *nfb,
796 unsigned long action, void *hcpu)
797{
798 unsigned cpu = (unsigned long)hcpu;
799
800 switch (action) {
801 case CPU_ONLINE:
802 case CPU_ONLINE_FROZEN:
Robert Loved5488eb2009-06-10 15:30:59 -0700803 FCOE_DBG("CPU %x online: Create Rx thread\n", cpu);
Robert Love8976f422009-03-17 11:41:46 -0700804 fcoe_percpu_thread_create(cpu);
805 break;
806 case CPU_DEAD:
807 case CPU_DEAD_FROZEN:
Robert Loved5488eb2009-06-10 15:30:59 -0700808 FCOE_DBG("CPU %x offline: Remove Rx thread\n", cpu);
Robert Love8976f422009-03-17 11:41:46 -0700809 fcoe_percpu_thread_destroy(cpu);
810 break;
811 default:
812 break;
813 }
814 return NOTIFY_OK;
815}
816
817static struct notifier_block fcoe_cpu_notifier = {
818 .notifier_call = fcoe_cpu_callback,
819};
820
821/**
Robert Love34f42a02009-02-27 10:55:45 -0800822 * fcoe_rcv() - this is the fcoe receive function called by NET_RX_SOFTIRQ
Robert Love85b4aa42008-12-09 15:10:24 -0800823 * @skb: the receive skb
824 * @dev: associated net device
825 * @ptype: context
Chris Leechdd3fd722009-04-21 16:27:36 -0700826 * @olddev: last device
Robert Love85b4aa42008-12-09 15:10:24 -0800827 *
828 * this function will receive the packet and build fc frame and pass it up
829 *
830 * Returns: 0 for success
Robert Love34f42a02009-02-27 10:55:45 -0800831 */
Robert Love85b4aa42008-12-09 15:10:24 -0800832int fcoe_rcv(struct sk_buff *skb, struct net_device *dev,
833 struct packet_type *ptype, struct net_device *olddev)
834{
835 struct fc_lport *lp;
836 struct fcoe_rcv_info *fr;
837 struct fcoe_softc *fc;
Robert Love85b4aa42008-12-09 15:10:24 -0800838 struct fc_frame_header *fh;
Robert Love85b4aa42008-12-09 15:10:24 -0800839 struct fcoe_percpu_s *fps;
Robert Love38eccab2009-03-17 11:41:30 -0700840 unsigned short oxid;
Robert Love8976f422009-03-17 11:41:46 -0700841 unsigned int cpu = 0;
Robert Love85b4aa42008-12-09 15:10:24 -0800842
843 fc = container_of(ptype, struct fcoe_softc, fcoe_packet_type);
Joe Eykholt97c83892009-03-17 11:42:40 -0700844 lp = fc->ctlr.lp;
Robert Love85b4aa42008-12-09 15:10:24 -0800845 if (unlikely(lp == NULL)) {
Robert Loved5488eb2009-06-10 15:30:59 -0700846 FCOE_NETDEV_DBG(dev, "Cannot find hba structure");
Robert Love85b4aa42008-12-09 15:10:24 -0800847 goto err2;
848 }
Joe Eykholt97c83892009-03-17 11:42:40 -0700849 if (!lp->link_up)
850 goto err2;
Robert Love85b4aa42008-12-09 15:10:24 -0800851
Robert Loved5488eb2009-06-10 15:30:59 -0700852 FCOE_NETDEV_DBG(dev, "skb_info: len:%d data_len:%d head:%p "
853 "data:%p tail:%p end:%p sum:%d dev:%s",
854 skb->len, skb->data_len, skb->head, skb->data,
855 skb_tail_pointer(skb), skb_end_pointer(skb),
856 skb->csum, skb->dev ? skb->dev->name : "<NULL>");
Robert Love85b4aa42008-12-09 15:10:24 -0800857
858 /* check for FCOE packet type */
859 if (unlikely(eth_hdr(skb)->h_proto != htons(ETH_P_FCOE))) {
Robert Loved5488eb2009-06-10 15:30:59 -0700860 FCOE_NETDEV_DBG(dev, "Wrong FC type frame");
Robert Love85b4aa42008-12-09 15:10:24 -0800861 goto err;
862 }
863
864 /*
865 * Check for minimum frame length, and make sure required FCoE
866 * and FC headers are pulled into the linear data area.
867 */
868 if (unlikely((skb->len < FCOE_MIN_FRAME) ||
869 !pskb_may_pull(skb, FCOE_HEADER_LEN)))
870 goto err;
871
872 skb_set_transport_header(skb, sizeof(struct fcoe_hdr));
873 fh = (struct fc_frame_header *) skb_transport_header(skb);
874
875 oxid = ntohs(fh->fh_ox_id);
876
877 fr = fcoe_dev_from_skb(skb);
878 fr->fr_dev = lp;
879 fr->ptype = ptype;
Robert Love5e5e92d2009-03-17 11:41:35 -0700880
Robert Love85b4aa42008-12-09 15:10:24 -0800881#ifdef CONFIG_SMP
882 /*
883 * The incoming frame exchange id(oxid) is ANDed with num of online
Robert Love8976f422009-03-17 11:41:46 -0700884 * cpu bits to get cpu and then this cpu is used for selecting
885 * a per cpu kernel thread from fcoe_percpu.
Robert Love85b4aa42008-12-09 15:10:24 -0800886 */
Robert Love8976f422009-03-17 11:41:46 -0700887 cpu = oxid & (num_online_cpus() - 1);
Robert Love85b4aa42008-12-09 15:10:24 -0800888#endif
Robert Love5e5e92d2009-03-17 11:41:35 -0700889
Robert Love8976f422009-03-17 11:41:46 -0700890 fps = &per_cpu(fcoe_percpu, cpu);
Robert Love85b4aa42008-12-09 15:10:24 -0800891 spin_lock_bh(&fps->fcoe_rx_list.lock);
Robert Love8976f422009-03-17 11:41:46 -0700892 if (unlikely(!fps->thread)) {
893 /*
894 * The targeted CPU is not ready, let's target
895 * the first CPU now. For non-SMP systems this
896 * will check the same CPU twice.
897 */
Robert Loved5488eb2009-06-10 15:30:59 -0700898 FCOE_NETDEV_DBG(dev, "CPU is online, but no receive thread "
899 "ready for incoming skb- using first online "
900 "CPU.\n");
Robert Love8976f422009-03-17 11:41:46 -0700901
902 spin_unlock_bh(&fps->fcoe_rx_list.lock);
903 cpu = first_cpu(cpu_online_map);
904 fps = &per_cpu(fcoe_percpu, cpu);
905 spin_lock_bh(&fps->fcoe_rx_list.lock);
906 if (!fps->thread) {
907 spin_unlock_bh(&fps->fcoe_rx_list.lock);
908 goto err;
909 }
910 }
911
912 /*
913 * We now have a valid CPU that we're targeting for
914 * this skb. We also have this receive thread locked,
915 * so we're free to queue skbs into it's queue.
916 */
Robert Love85b4aa42008-12-09 15:10:24 -0800917 __skb_queue_tail(&fps->fcoe_rx_list, skb);
918 if (fps->fcoe_rx_list.qlen == 1)
919 wake_up_process(fps->thread);
920
921 spin_unlock_bh(&fps->fcoe_rx_list.lock);
922
923 return 0;
924err:
Robert Love582b45b2009-03-31 15:51:50 -0700925 fc_lport_get_stats(lp)->ErrorFrames++;
Robert Love85b4aa42008-12-09 15:10:24 -0800926
927err2:
928 kfree_skb(skb);
929 return -1;
930}
Robert Love85b4aa42008-12-09 15:10:24 -0800931
932/**
Robert Love34f42a02009-02-27 10:55:45 -0800933 * fcoe_start_io() - pass to netdev to start xmit for fcoe
Robert Love85b4aa42008-12-09 15:10:24 -0800934 * @skb: the skb to be xmitted
935 *
936 * Returns: 0 for success
Robert Love34f42a02009-02-27 10:55:45 -0800937 */
Robert Love85b4aa42008-12-09 15:10:24 -0800938static inline int fcoe_start_io(struct sk_buff *skb)
939{
940 int rc;
941
942 skb_get(skb);
943 rc = dev_queue_xmit(skb);
944 if (rc != 0)
945 return rc;
946 kfree_skb(skb);
947 return 0;
948}
949
950/**
Chris Leechdd3fd722009-04-21 16:27:36 -0700951 * fcoe_get_paged_crc_eof() - in case we need to alloc a page for crc_eof
Robert Love85b4aa42008-12-09 15:10:24 -0800952 * @skb: the skb to be xmitted
953 * @tlen: total len
954 *
955 * Returns: 0 for success
Robert Love34f42a02009-02-27 10:55:45 -0800956 */
Robert Love85b4aa42008-12-09 15:10:24 -0800957static int fcoe_get_paged_crc_eof(struct sk_buff *skb, int tlen)
958{
959 struct fcoe_percpu_s *fps;
960 struct page *page;
Robert Love85b4aa42008-12-09 15:10:24 -0800961
Robert Love5e5e92d2009-03-17 11:41:35 -0700962 fps = &get_cpu_var(fcoe_percpu);
Robert Love85b4aa42008-12-09 15:10:24 -0800963 page = fps->crc_eof_page;
964 if (!page) {
965 page = alloc_page(GFP_ATOMIC);
966 if (!page) {
Robert Love5e5e92d2009-03-17 11:41:35 -0700967 put_cpu_var(fcoe_percpu);
Robert Love85b4aa42008-12-09 15:10:24 -0800968 return -ENOMEM;
969 }
970 fps->crc_eof_page = page;
Robert Love8976f422009-03-17 11:41:46 -0700971 fps->crc_eof_offset = 0;
Robert Love85b4aa42008-12-09 15:10:24 -0800972 }
973
974 get_page(page);
975 skb_fill_page_desc(skb, skb_shinfo(skb)->nr_frags, page,
976 fps->crc_eof_offset, tlen);
977 skb->len += tlen;
978 skb->data_len += tlen;
979 skb->truesize += tlen;
980 fps->crc_eof_offset += sizeof(struct fcoe_crc_eof);
981
982 if (fps->crc_eof_offset >= PAGE_SIZE) {
983 fps->crc_eof_page = NULL;
984 fps->crc_eof_offset = 0;
985 put_page(page);
986 }
Robert Love5e5e92d2009-03-17 11:41:35 -0700987 put_cpu_var(fcoe_percpu);
Robert Love85b4aa42008-12-09 15:10:24 -0800988 return 0;
989}
990
991/**
Robert Love34f42a02009-02-27 10:55:45 -0800992 * fcoe_fc_crc() - calculates FC CRC in this fcoe skb
Chris Leechdd3fd722009-04-21 16:27:36 -0700993 * @fp: the fc_frame containing data to be checksummed
Robert Love85b4aa42008-12-09 15:10:24 -0800994 *
995 * This uses crc32() to calculate the crc for fc frame
996 * Return : 32 bit crc
Robert Love34f42a02009-02-27 10:55:45 -0800997 */
Robert Love85b4aa42008-12-09 15:10:24 -0800998u32 fcoe_fc_crc(struct fc_frame *fp)
999{
1000 struct sk_buff *skb = fp_skb(fp);
1001 struct skb_frag_struct *frag;
1002 unsigned char *data;
1003 unsigned long off, len, clen;
1004 u32 crc;
1005 unsigned i;
1006
1007 crc = crc32(~0, skb->data, skb_headlen(skb));
1008
1009 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
1010 frag = &skb_shinfo(skb)->frags[i];
1011 off = frag->page_offset;
1012 len = frag->size;
1013 while (len > 0) {
1014 clen = min(len, PAGE_SIZE - (off & ~PAGE_MASK));
1015 data = kmap_atomic(frag->page + (off >> PAGE_SHIFT),
1016 KM_SKB_DATA_SOFTIRQ);
1017 crc = crc32(crc, data + (off & ~PAGE_MASK), clen);
1018 kunmap_atomic(data, KM_SKB_DATA_SOFTIRQ);
1019 off += clen;
1020 len -= clen;
1021 }
1022 }
1023 return crc;
1024}
Robert Love85b4aa42008-12-09 15:10:24 -08001025
1026/**
Robert Love34f42a02009-02-27 10:55:45 -08001027 * fcoe_xmit() - FCoE frame transmit function
Robert Love85b4aa42008-12-09 15:10:24 -08001028 * @lp: the associated local port
1029 * @fp: the fc_frame to be transmitted
1030 *
1031 * Return : 0 for success
Robert Love34f42a02009-02-27 10:55:45 -08001032 */
Robert Love85b4aa42008-12-09 15:10:24 -08001033int fcoe_xmit(struct fc_lport *lp, struct fc_frame *fp)
1034{
Vasu Dev4bb6b512009-05-06 10:52:34 -07001035 int wlen;
Robert Love85b4aa42008-12-09 15:10:24 -08001036 u32 crc;
1037 struct ethhdr *eh;
1038 struct fcoe_crc_eof *cp;
1039 struct sk_buff *skb;
1040 struct fcoe_dev_stats *stats;
1041 struct fc_frame_header *fh;
1042 unsigned int hlen; /* header length implies the version */
1043 unsigned int tlen; /* trailer length */
1044 unsigned int elen; /* eth header, may include vlan */
Robert Love85b4aa42008-12-09 15:10:24 -08001045 struct fcoe_softc *fc;
1046 u8 sof, eof;
1047 struct fcoe_hdr *hp;
1048
1049 WARN_ON((fr_len(fp) % sizeof(u32)) != 0);
1050
Robert Lovefc47ff62009-02-27 10:55:55 -08001051 fc = lport_priv(lp);
Robert Love85b4aa42008-12-09 15:10:24 -08001052 fh = fc_frame_header_get(fp);
Joe Eykholt97c83892009-03-17 11:42:40 -07001053 skb = fp_skb(fp);
1054 wlen = skb->len / FCOE_WORD_TO_BYTE;
1055
1056 if (!lp->link_up) {
Dan Carpenter3caf02e2009-04-21 16:27:25 -07001057 kfree_skb(skb);
Joe Eykholt97c83892009-03-17 11:42:40 -07001058 return 0;
Robert Love85b4aa42008-12-09 15:10:24 -08001059 }
1060
Joe Eykholt97c83892009-03-17 11:42:40 -07001061 if (unlikely(fh->fh_r_ctl == FC_RCTL_ELS_REQ) &&
1062 fcoe_ctlr_els_send(&fc->ctlr, skb))
1063 return 0;
1064
Robert Love85b4aa42008-12-09 15:10:24 -08001065 sof = fr_sof(fp);
1066 eof = fr_eof(fp);
1067
Vasu Dev4e57e1c2009-05-06 10:52:46 -07001068 elen = sizeof(struct ethhdr);
Robert Love85b4aa42008-12-09 15:10:24 -08001069 hlen = sizeof(struct fcoe_hdr);
1070 tlen = sizeof(struct fcoe_crc_eof);
1071 wlen = (skb->len - tlen + sizeof(crc)) / FCOE_WORD_TO_BYTE;
1072
1073 /* crc offload */
1074 if (likely(lp->crc_offload)) {
Yi Zou39ca9a02009-02-27 14:07:15 -08001075 skb->ip_summed = CHECKSUM_PARTIAL;
Robert Love85b4aa42008-12-09 15:10:24 -08001076 skb->csum_start = skb_headroom(skb);
1077 skb->csum_offset = skb->len;
1078 crc = 0;
1079 } else {
1080 skb->ip_summed = CHECKSUM_NONE;
1081 crc = fcoe_fc_crc(fp);
1082 }
1083
1084 /* copy fc crc and eof to the skb buff */
1085 if (skb_is_nonlinear(skb)) {
1086 skb_frag_t *frag;
1087 if (fcoe_get_paged_crc_eof(skb, tlen)) {
Roel Kluine9041582009-02-27 10:56:22 -08001088 kfree_skb(skb);
Robert Love85b4aa42008-12-09 15:10:24 -08001089 return -ENOMEM;
1090 }
1091 frag = &skb_shinfo(skb)->frags[skb_shinfo(skb)->nr_frags - 1];
1092 cp = kmap_atomic(frag->page, KM_SKB_DATA_SOFTIRQ)
1093 + frag->page_offset;
1094 } else {
1095 cp = (struct fcoe_crc_eof *)skb_put(skb, tlen);
1096 }
1097
1098 memset(cp, 0, sizeof(*cp));
1099 cp->fcoe_eof = eof;
1100 cp->fcoe_crc32 = cpu_to_le32(~crc);
1101
1102 if (skb_is_nonlinear(skb)) {
1103 kunmap_atomic(cp, KM_SKB_DATA_SOFTIRQ);
1104 cp = NULL;
1105 }
1106
Chris Leechdd3fd722009-04-21 16:27:36 -07001107 /* adjust skb network/transport offsets to match mac/fcoe/fc */
Robert Love85b4aa42008-12-09 15:10:24 -08001108 skb_push(skb, elen + hlen);
1109 skb_reset_mac_header(skb);
1110 skb_reset_network_header(skb);
1111 skb->mac_len = elen;
Yi Zou211c7382009-02-27 14:06:37 -08001112 skb->protocol = htons(ETH_P_FCOE);
Robert Love85b4aa42008-12-09 15:10:24 -08001113 skb->dev = fc->real_dev;
1114
1115 /* fill up mac and fcoe headers */
1116 eh = eth_hdr(skb);
1117 eh->h_proto = htons(ETH_P_FCOE);
Joe Eykholt97c83892009-03-17 11:42:40 -07001118 if (fc->ctlr.map_dest)
Robert Love85b4aa42008-12-09 15:10:24 -08001119 fc_fcoe_set_mac(eh->h_dest, fh->fh_d_id);
1120 else
1121 /* insert GW address */
Joe Eykholt97c83892009-03-17 11:42:40 -07001122 memcpy(eh->h_dest, fc->ctlr.dest_addr, ETH_ALEN);
Robert Love85b4aa42008-12-09 15:10:24 -08001123
Joe Eykholt97c83892009-03-17 11:42:40 -07001124 if (unlikely(fc->ctlr.flogi_oxid != FC_XID_UNKNOWN))
1125 memcpy(eh->h_source, fc->ctlr.ctl_src_addr, ETH_ALEN);
Robert Love85b4aa42008-12-09 15:10:24 -08001126 else
Joe Eykholt97c83892009-03-17 11:42:40 -07001127 memcpy(eh->h_source, fc->ctlr.data_src_addr, ETH_ALEN);
Robert Love85b4aa42008-12-09 15:10:24 -08001128
1129 hp = (struct fcoe_hdr *)(eh + 1);
1130 memset(hp, 0, sizeof(*hp));
1131 if (FC_FCOE_VER)
1132 FC_FCOE_ENCAPS_VER(hp, FC_FCOE_VER);
1133 hp->fcoe_sof = sof;
1134
Yi Zou39ca9a02009-02-27 14:07:15 -08001135#ifdef NETIF_F_FSO
1136 /* fcoe lso, mss is in max_payload which is non-zero for FCP data */
1137 if (lp->seq_offload && fr_max_payload(fp)) {
1138 skb_shinfo(skb)->gso_type = SKB_GSO_FCOE;
1139 skb_shinfo(skb)->gso_size = fr_max_payload(fp);
1140 } else {
1141 skb_shinfo(skb)->gso_type = 0;
1142 skb_shinfo(skb)->gso_size = 0;
1143 }
1144#endif
Robert Love85b4aa42008-12-09 15:10:24 -08001145 /* update tx stats: regardless if LLD fails */
Robert Love582b45b2009-03-31 15:51:50 -07001146 stats = fc_lport_get_stats(lp);
1147 stats->TxFrames++;
1148 stats->TxWords += wlen;
Robert Love85b4aa42008-12-09 15:10:24 -08001149
1150 /* send down to lld */
1151 fr_dev(fp) = lp;
1152 if (fc->fcoe_pending_queue.qlen)
Vasu Dev4bb6b512009-05-06 10:52:34 -07001153 fcoe_check_wait_queue(lp, skb);
1154 else if (fcoe_start_io(skb))
1155 fcoe_check_wait_queue(lp, skb);
Robert Love85b4aa42008-12-09 15:10:24 -08001156
1157 return 0;
1158}
Robert Love85b4aa42008-12-09 15:10:24 -08001159
Robert Love34f42a02009-02-27 10:55:45 -08001160/**
1161 * fcoe_percpu_receive_thread() - recv thread per cpu
Robert Love85b4aa42008-12-09 15:10:24 -08001162 * @arg: ptr to the fcoe per cpu struct
1163 *
1164 * Return: 0 for success
Robert Love85b4aa42008-12-09 15:10:24 -08001165 */
1166int fcoe_percpu_receive_thread(void *arg)
1167{
1168 struct fcoe_percpu_s *p = arg;
1169 u32 fr_len;
1170 struct fc_lport *lp;
1171 struct fcoe_rcv_info *fr;
1172 struct fcoe_dev_stats *stats;
1173 struct fc_frame_header *fh;
1174 struct sk_buff *skb;
1175 struct fcoe_crc_eof crc_eof;
1176 struct fc_frame *fp;
1177 u8 *mac = NULL;
1178 struct fcoe_softc *fc;
1179 struct fcoe_hdr *hp;
1180
Robert Love4469c192009-02-27 10:56:38 -08001181 set_user_nice(current, -20);
Robert Love85b4aa42008-12-09 15:10:24 -08001182
1183 while (!kthread_should_stop()) {
1184
1185 spin_lock_bh(&p->fcoe_rx_list.lock);
1186 while ((skb = __skb_dequeue(&p->fcoe_rx_list)) == NULL) {
1187 set_current_state(TASK_INTERRUPTIBLE);
1188 spin_unlock_bh(&p->fcoe_rx_list.lock);
1189 schedule();
1190 set_current_state(TASK_RUNNING);
1191 if (kthread_should_stop())
1192 return 0;
1193 spin_lock_bh(&p->fcoe_rx_list.lock);
1194 }
1195 spin_unlock_bh(&p->fcoe_rx_list.lock);
1196 fr = fcoe_dev_from_skb(skb);
1197 lp = fr->fr_dev;
1198 if (unlikely(lp == NULL)) {
Robert Loved5488eb2009-06-10 15:30:59 -07001199 FCOE_NETDEV_DBG(skb->dev, "Invalid HBA Structure");
Robert Love85b4aa42008-12-09 15:10:24 -08001200 kfree_skb(skb);
1201 continue;
1202 }
1203
Robert Loved5488eb2009-06-10 15:30:59 -07001204 FCOE_NETDEV_DBG(skb->dev, "skb_info: len:%d data_len:%d "
1205 "head:%p data:%p tail:%p end:%p sum:%d dev:%s",
1206 skb->len, skb->data_len,
1207 skb->head, skb->data, skb_tail_pointer(skb),
1208 skb_end_pointer(skb), skb->csum,
1209 skb->dev ? skb->dev->name : "<NULL>");
Robert Love85b4aa42008-12-09 15:10:24 -08001210
1211 /*
1212 * Save source MAC address before discarding header.
1213 */
1214 fc = lport_priv(lp);
Robert Love85b4aa42008-12-09 15:10:24 -08001215 if (skb_is_nonlinear(skb))
1216 skb_linearize(skb); /* not ideal */
Joe Eykholt97c83892009-03-17 11:42:40 -07001217 mac = eth_hdr(skb)->h_source;
Robert Love85b4aa42008-12-09 15:10:24 -08001218
1219 /*
1220 * Frame length checks and setting up the header pointers
1221 * was done in fcoe_rcv already.
1222 */
1223 hp = (struct fcoe_hdr *) skb_network_header(skb);
1224 fh = (struct fc_frame_header *) skb_transport_header(skb);
1225
Robert Love582b45b2009-03-31 15:51:50 -07001226 stats = fc_lport_get_stats(lp);
Robert Love85b4aa42008-12-09 15:10:24 -08001227 if (unlikely(FC_FCOE_DECAPS_VER(hp) != FC_FCOE_VER)) {
Robert Love582b45b2009-03-31 15:51:50 -07001228 if (stats->ErrorFrames < 5)
Robert Loved5488eb2009-06-10 15:30:59 -07001229 printk(KERN_WARNING "fcoe: FCoE version "
Robert Love582b45b2009-03-31 15:51:50 -07001230 "mismatch: The frame has "
1231 "version %x, but the "
1232 "initiator supports version "
1233 "%x\n", FC_FCOE_DECAPS_VER(hp),
1234 FC_FCOE_VER);
1235 stats->ErrorFrames++;
Robert Love85b4aa42008-12-09 15:10:24 -08001236 kfree_skb(skb);
1237 continue;
1238 }
1239
1240 skb_pull(skb, sizeof(struct fcoe_hdr));
1241 fr_len = skb->len - sizeof(struct fcoe_crc_eof);
1242
Robert Love582b45b2009-03-31 15:51:50 -07001243 stats->RxFrames++;
1244 stats->RxWords += fr_len / FCOE_WORD_TO_BYTE;
Robert Love85b4aa42008-12-09 15:10:24 -08001245
1246 fp = (struct fc_frame *)skb;
1247 fc_frame_init(fp);
1248 fr_dev(fp) = lp;
1249 fr_sof(fp) = hp->fcoe_sof;
1250
1251 /* Copy out the CRC and EOF trailer for access */
1252 if (skb_copy_bits(skb, fr_len, &crc_eof, sizeof(crc_eof))) {
1253 kfree_skb(skb);
1254 continue;
1255 }
1256 fr_eof(fp) = crc_eof.fcoe_eof;
1257 fr_crc(fp) = crc_eof.fcoe_crc32;
1258 if (pskb_trim(skb, fr_len)) {
1259 kfree_skb(skb);
1260 continue;
1261 }
1262
1263 /*
1264 * We only check CRC if no offload is available and if it is
1265 * it's solicited data, in which case, the FCP layer would
1266 * check it during the copy.
1267 */
Yi Zou07c00ec2009-02-27 14:07:31 -08001268 if (lp->crc_offload && skb->ip_summed == CHECKSUM_UNNECESSARY)
Robert Love85b4aa42008-12-09 15:10:24 -08001269 fr_flags(fp) &= ~FCPHF_CRC_UNCHECKED;
1270 else
1271 fr_flags(fp) |= FCPHF_CRC_UNCHECKED;
1272
1273 fh = fc_frame_header_get(fp);
1274 if (fh->fh_r_ctl == FC_RCTL_DD_SOL_DATA &&
1275 fh->fh_type == FC_TYPE_FCP) {
Vasu Dev52ff8782009-07-29 17:05:10 -07001276 fc_exch_recv(lp, fp);
Robert Love85b4aa42008-12-09 15:10:24 -08001277 continue;
1278 }
1279 if (fr_flags(fp) & FCPHF_CRC_UNCHECKED) {
1280 if (le32_to_cpu(fr_crc(fp)) !=
1281 ~crc32(~0, skb->data, fr_len)) {
Robert Loved5488eb2009-06-10 15:30:59 -07001282 if (stats->InvalidCRCCount < 5)
Robert Love85b4aa42008-12-09 15:10:24 -08001283 printk(KERN_WARNING "fcoe: dropping "
1284 "frame with CRC error\n");
1285 stats->InvalidCRCCount++;
1286 stats->ErrorFrames++;
1287 fc_frame_free(fp);
1288 continue;
1289 }
1290 fr_flags(fp) &= ~FCPHF_CRC_UNCHECKED;
1291 }
Joe Eykholt97c83892009-03-17 11:42:40 -07001292 if (unlikely(fc->ctlr.flogi_oxid != FC_XID_UNKNOWN) &&
1293 fcoe_ctlr_recv_flogi(&fc->ctlr, fp, mac)) {
1294 fc_frame_free(fp);
1295 continue;
1296 }
Vasu Dev52ff8782009-07-29 17:05:10 -07001297 fc_exch_recv(lp, fp);
Robert Love85b4aa42008-12-09 15:10:24 -08001298 }
1299 return 0;
1300}
1301
1302/**
Chris Leechdd3fd722009-04-21 16:27:36 -07001303 * fcoe_check_wait_queue() - attempt to clear the transmit backlog
1304 * @lp: the fc_lport
Robert Love85b4aa42008-12-09 15:10:24 -08001305 *
1306 * This empties the wait_queue, dequeue the head of the wait_queue queue
1307 * and calls fcoe_start_io() for each packet, if all skb have been
Vasu Devc826a312009-02-27 10:56:27 -08001308 * transmitted, return qlen or -1 if a error occurs, then restore
Chris Leechdd3fd722009-04-21 16:27:36 -07001309 * wait_queue and try again later.
Robert Love85b4aa42008-12-09 15:10:24 -08001310 *
1311 * The wait_queue is used when the skb transmit fails. skb will go
Chris Leechdd3fd722009-04-21 16:27:36 -07001312 * in the wait_queue which will be emptied by the timer function or
Robert Love85b4aa42008-12-09 15:10:24 -08001313 * by the next skb transmit.
Robert Love34f42a02009-02-27 10:55:45 -08001314 */
Vasu Dev4bb6b512009-05-06 10:52:34 -07001315static void fcoe_check_wait_queue(struct fc_lport *lp, struct sk_buff *skb)
Robert Love85b4aa42008-12-09 15:10:24 -08001316{
Chris Leech55c8baf2009-02-27 10:56:32 -08001317 struct fcoe_softc *fc = lport_priv(lp);
Vasu Dev4bb6b512009-05-06 10:52:34 -07001318 int rc;
Robert Love85b4aa42008-12-09 15:10:24 -08001319
Robert Love85b4aa42008-12-09 15:10:24 -08001320 spin_lock_bh(&fc->fcoe_pending_queue.lock);
Vasu Dev4bb6b512009-05-06 10:52:34 -07001321
1322 if (skb)
1323 __skb_queue_tail(&fc->fcoe_pending_queue, skb);
1324
Vasu Devc826a312009-02-27 10:56:27 -08001325 if (fc->fcoe_pending_queue_active)
1326 goto out;
1327 fc->fcoe_pending_queue_active = 1;
Chris Leech55c8baf2009-02-27 10:56:32 -08001328
1329 while (fc->fcoe_pending_queue.qlen) {
1330 /* keep qlen > 0 until fcoe_start_io succeeds */
1331 fc->fcoe_pending_queue.qlen++;
1332 skb = __skb_dequeue(&fc->fcoe_pending_queue);
1333
1334 spin_unlock_bh(&fc->fcoe_pending_queue.lock);
1335 rc = fcoe_start_io(skb);
1336 spin_lock_bh(&fc->fcoe_pending_queue.lock);
1337
1338 if (rc) {
1339 __skb_queue_head(&fc->fcoe_pending_queue, skb);
1340 /* undo temporary increment above */
1341 fc->fcoe_pending_queue.qlen--;
1342 break;
Robert Love85b4aa42008-12-09 15:10:24 -08001343 }
Chris Leech55c8baf2009-02-27 10:56:32 -08001344 /* undo temporary increment above */
1345 fc->fcoe_pending_queue.qlen--;
Robert Love85b4aa42008-12-09 15:10:24 -08001346 }
Chris Leech55c8baf2009-02-27 10:56:32 -08001347
1348 if (fc->fcoe_pending_queue.qlen < FCOE_LOW_QUEUE_DEPTH)
1349 lp->qfull = 0;
Vasu Dev1047f222009-05-06 10:52:40 -07001350 if (fc->fcoe_pending_queue.qlen && !timer_pending(&fc->timer))
1351 mod_timer(&fc->timer, jiffies + 2);
Vasu Devc826a312009-02-27 10:56:27 -08001352 fc->fcoe_pending_queue_active = 0;
Vasu Devc826a312009-02-27 10:56:27 -08001353out:
Vasu Dev4bb6b512009-05-06 10:52:34 -07001354 if (fc->fcoe_pending_queue.qlen > FCOE_MAX_QUEUE_DEPTH)
1355 lp->qfull = 1;
Robert Love85b4aa42008-12-09 15:10:24 -08001356 spin_unlock_bh(&fc->fcoe_pending_queue.lock);
Vasu Dev4bb6b512009-05-06 10:52:34 -07001357 return;
Robert Love85b4aa42008-12-09 15:10:24 -08001358}
1359
1360/**
Robert Love34f42a02009-02-27 10:55:45 -08001361 * fcoe_dev_setup() - setup link change notification interface
1362 */
Randy Dunlapb0d428a2009-04-27 21:49:31 -07001363static void fcoe_dev_setup(void)
Robert Love85b4aa42008-12-09 15:10:24 -08001364{
Robert Love85b4aa42008-12-09 15:10:24 -08001365 register_netdevice_notifier(&fcoe_notifier);
1366}
1367
1368/**
Randy Dunlapb0d428a2009-04-27 21:49:31 -07001369 * fcoe_dev_cleanup() - cleanup link change notification interface
Robert Love34f42a02009-02-27 10:55:45 -08001370 */
Robert Love85b4aa42008-12-09 15:10:24 -08001371static void fcoe_dev_cleanup(void)
1372{
1373 unregister_netdevice_notifier(&fcoe_notifier);
1374}
1375
1376/**
Robert Love34f42a02009-02-27 10:55:45 -08001377 * fcoe_device_notification() - netdev event notification callback
Robert Love85b4aa42008-12-09 15:10:24 -08001378 * @notifier: context of the notification
1379 * @event: type of event
1380 * @ptr: fixed array for output parsed ifname
1381 *
1382 * This function is called by the ethernet driver in case of link change event
1383 *
1384 * Returns: 0 for success
Robert Love34f42a02009-02-27 10:55:45 -08001385 */
Robert Love85b4aa42008-12-09 15:10:24 -08001386static int fcoe_device_notification(struct notifier_block *notifier,
1387 ulong event, void *ptr)
1388{
1389 struct fc_lport *lp = NULL;
1390 struct net_device *real_dev = ptr;
1391 struct fcoe_softc *fc;
1392 struct fcoe_dev_stats *stats;
Joe Eykholt97c83892009-03-17 11:42:40 -07001393 u32 link_possible = 1;
Robert Love85b4aa42008-12-09 15:10:24 -08001394 u32 mfs;
1395 int rc = NOTIFY_OK;
1396
1397 read_lock(&fcoe_hostlist_lock);
1398 list_for_each_entry(fc, &fcoe_hostlist, list) {
1399 if (fc->real_dev == real_dev) {
Joe Eykholt97c83892009-03-17 11:42:40 -07001400 lp = fc->ctlr.lp;
Robert Love85b4aa42008-12-09 15:10:24 -08001401 break;
1402 }
1403 }
1404 read_unlock(&fcoe_hostlist_lock);
1405 if (lp == NULL) {
1406 rc = NOTIFY_DONE;
1407 goto out;
1408 }
1409
Robert Love85b4aa42008-12-09 15:10:24 -08001410 switch (event) {
1411 case NETDEV_DOWN:
1412 case NETDEV_GOING_DOWN:
Joe Eykholt97c83892009-03-17 11:42:40 -07001413 link_possible = 0;
Robert Love85b4aa42008-12-09 15:10:24 -08001414 break;
1415 case NETDEV_UP:
1416 case NETDEV_CHANGE:
Robert Love85b4aa42008-12-09 15:10:24 -08001417 break;
1418 case NETDEV_CHANGEMTU:
1419 mfs = fc->real_dev->mtu -
1420 (sizeof(struct fcoe_hdr) +
1421 sizeof(struct fcoe_crc_eof));
1422 if (mfs >= FC_MIN_MAX_FRAME)
1423 fc_set_mfs(lp, mfs);
Robert Love85b4aa42008-12-09 15:10:24 -08001424 break;
1425 case NETDEV_REGISTER:
1426 break;
1427 default:
Robert Loved5488eb2009-06-10 15:30:59 -07001428 FCOE_NETDEV_DBG(real_dev, "Unknown event %ld "
1429 "from netdev netlink\n", event);
Robert Love85b4aa42008-12-09 15:10:24 -08001430 }
Joe Eykholt97c83892009-03-17 11:42:40 -07001431 if (link_possible && !fcoe_link_ok(lp))
1432 fcoe_ctlr_link_up(&fc->ctlr);
1433 else if (fcoe_ctlr_link_down(&fc->ctlr)) {
1434 stats = fc_lport_get_stats(lp);
1435 stats->LinkFailureCount++;
1436 fcoe_clean_pending_queue(lp);
Robert Love85b4aa42008-12-09 15:10:24 -08001437 }
1438out:
1439 return rc;
1440}
1441
1442/**
Robert Love34f42a02009-02-27 10:55:45 -08001443 * fcoe_if_to_netdev() - parse a name buffer to get netdev
Robert Love85b4aa42008-12-09 15:10:24 -08001444 * @buffer: incoming buffer to be copied
1445 *
Chris Leechdd3fd722009-04-21 16:27:36 -07001446 * Returns: NULL or ptr to net_device
Robert Love34f42a02009-02-27 10:55:45 -08001447 */
Robert Love85b4aa42008-12-09 15:10:24 -08001448static struct net_device *fcoe_if_to_netdev(const char *buffer)
1449{
1450 char *cp;
1451 char ifname[IFNAMSIZ + 2];
1452
1453 if (buffer) {
1454 strlcpy(ifname, buffer, IFNAMSIZ);
1455 cp = ifname + strlen(ifname);
1456 while (--cp >= ifname && *cp == '\n')
1457 *cp = '\0';
1458 return dev_get_by_name(&init_net, ifname);
1459 }
1460 return NULL;
1461}
1462
1463/**
Chris Leechdd3fd722009-04-21 16:27:36 -07001464 * fcoe_netdev_to_module_owner() - finds out the driver module of the netdev
Robert Love85b4aa42008-12-09 15:10:24 -08001465 * @netdev: the target netdev
1466 *
1467 * Returns: ptr to the struct module, NULL for failure
Robert Love34f42a02009-02-27 10:55:45 -08001468 */
Robert Loveb2ab99c2009-02-27 10:55:50 -08001469static struct module *
1470fcoe_netdev_to_module_owner(const struct net_device *netdev)
Robert Love85b4aa42008-12-09 15:10:24 -08001471{
1472 struct device *dev;
1473
1474 if (!netdev)
1475 return NULL;
1476
1477 dev = netdev->dev.parent;
1478 if (!dev)
1479 return NULL;
1480
1481 if (!dev->driver)
1482 return NULL;
1483
1484 return dev->driver->owner;
1485}
1486
1487/**
Robert Love34f42a02009-02-27 10:55:45 -08001488 * fcoe_ethdrv_get() - Hold the Ethernet driver
Robert Love85b4aa42008-12-09 15:10:24 -08001489 * @netdev: the target netdev
1490 *
Robert Love34f42a02009-02-27 10:55:45 -08001491 * Holds the Ethernet driver module by try_module_get() for
1492 * the corresponding netdev.
1493 *
Chris Leechdd3fd722009-04-21 16:27:36 -07001494 * Returns: 0 for success
Robert Love34f42a02009-02-27 10:55:45 -08001495 */
Robert Love85b4aa42008-12-09 15:10:24 -08001496static int fcoe_ethdrv_get(const struct net_device *netdev)
1497{
1498 struct module *owner;
1499
1500 owner = fcoe_netdev_to_module_owner(netdev);
1501 if (owner) {
Robert Loved5488eb2009-06-10 15:30:59 -07001502 FCOE_NETDEV_DBG(netdev, "Hold driver module %s\n",
1503 module_name(owner));
Robert Love85b4aa42008-12-09 15:10:24 -08001504 return try_module_get(owner);
1505 }
1506 return -ENODEV;
1507}
1508
1509/**
Robert Love34f42a02009-02-27 10:55:45 -08001510 * fcoe_ethdrv_put() - Release the Ethernet driver
Robert Love85b4aa42008-12-09 15:10:24 -08001511 * @netdev: the target netdev
1512 *
Robert Love34f42a02009-02-27 10:55:45 -08001513 * Releases the Ethernet driver module by module_put for
1514 * the corresponding netdev.
1515 *
Chris Leechdd3fd722009-04-21 16:27:36 -07001516 * Returns: 0 for success
Robert Love34f42a02009-02-27 10:55:45 -08001517 */
Robert Love85b4aa42008-12-09 15:10:24 -08001518static int fcoe_ethdrv_put(const struct net_device *netdev)
1519{
1520 struct module *owner;
1521
1522 owner = fcoe_netdev_to_module_owner(netdev);
1523 if (owner) {
Robert Loved5488eb2009-06-10 15:30:59 -07001524 FCOE_NETDEV_DBG(netdev, "Release driver module %s\n",
1525 module_name(owner));
Robert Love85b4aa42008-12-09 15:10:24 -08001526 module_put(owner);
1527 return 0;
1528 }
1529 return -ENODEV;
1530}
1531
1532/**
Robert Love34f42a02009-02-27 10:55:45 -08001533 * fcoe_destroy() - handles the destroy from sysfs
Chris Leechdd3fd722009-04-21 16:27:36 -07001534 * @buffer: expected to be an eth if name
Robert Love85b4aa42008-12-09 15:10:24 -08001535 * @kp: associated kernel param
1536 *
1537 * Returns: 0 for success
Robert Love34f42a02009-02-27 10:55:45 -08001538 */
Robert Love85b4aa42008-12-09 15:10:24 -08001539static int fcoe_destroy(const char *buffer, struct kernel_param *kp)
1540{
1541 int rc;
1542 struct net_device *netdev;
1543
1544 netdev = fcoe_if_to_netdev(buffer);
1545 if (!netdev) {
1546 rc = -ENODEV;
1547 goto out_nodev;
1548 }
1549 /* look for existing lport */
1550 if (!fcoe_hostlist_lookup(netdev)) {
1551 rc = -ENODEV;
1552 goto out_putdev;
1553 }
Vasu Dev7f349142009-03-27 09:06:31 -07001554 rc = fcoe_if_destroy(netdev);
Robert Love85b4aa42008-12-09 15:10:24 -08001555 if (rc) {
Robert Loved5488eb2009-06-10 15:30:59 -07001556 printk(KERN_ERR "fcoe: Failed to destroy interface (%s)\n",
Robert Love85b4aa42008-12-09 15:10:24 -08001557 netdev->name);
1558 rc = -EIO;
1559 goto out_putdev;
1560 }
1561 fcoe_ethdrv_put(netdev);
1562 rc = 0;
1563out_putdev:
1564 dev_put(netdev);
1565out_nodev:
1566 return rc;
1567}
1568
1569/**
Robert Love34f42a02009-02-27 10:55:45 -08001570 * fcoe_create() - Handles the create call from sysfs
Chris Leechdd3fd722009-04-21 16:27:36 -07001571 * @buffer: expected to be an eth if name
Robert Love85b4aa42008-12-09 15:10:24 -08001572 * @kp: associated kernel param
1573 *
1574 * Returns: 0 for success
Robert Love34f42a02009-02-27 10:55:45 -08001575 */
Robert Love85b4aa42008-12-09 15:10:24 -08001576static int fcoe_create(const char *buffer, struct kernel_param *kp)
1577{
1578 int rc;
1579 struct net_device *netdev;
1580
1581 netdev = fcoe_if_to_netdev(buffer);
1582 if (!netdev) {
1583 rc = -ENODEV;
1584 goto out_nodev;
1585 }
1586 /* look for existing lport */
1587 if (fcoe_hostlist_lookup(netdev)) {
1588 rc = -EEXIST;
1589 goto out_putdev;
1590 }
1591 fcoe_ethdrv_get(netdev);
1592
Vasu Dev7f349142009-03-27 09:06:31 -07001593 rc = fcoe_if_create(netdev);
Robert Love85b4aa42008-12-09 15:10:24 -08001594 if (rc) {
Robert Loved5488eb2009-06-10 15:30:59 -07001595 printk(KERN_ERR "fcoe: Failed to create interface (%s)\n",
Robert Love85b4aa42008-12-09 15:10:24 -08001596 netdev->name);
1597 fcoe_ethdrv_put(netdev);
1598 rc = -EIO;
1599 goto out_putdev;
1600 }
1601 rc = 0;
1602out_putdev:
1603 dev_put(netdev);
1604out_nodev:
1605 return rc;
1606}
1607
1608module_param_call(create, fcoe_create, NULL, NULL, S_IWUSR);
1609__MODULE_PARM_TYPE(create, "string");
1610MODULE_PARM_DESC(create, "Create fcoe port using net device passed in.");
1611module_param_call(destroy, fcoe_destroy, NULL, NULL, S_IWUSR);
1612__MODULE_PARM_TYPE(destroy, "string");
1613MODULE_PARM_DESC(destroy, "Destroy fcoe port");
1614
Robert Love34f42a02009-02-27 10:55:45 -08001615/**
1616 * fcoe_link_ok() - Check if link is ok for the fc_lport
Robert Love85b4aa42008-12-09 15:10:24 -08001617 * @lp: ptr to the fc_lport
1618 *
1619 * Any permanently-disqualifying conditions have been previously checked.
1620 * This also updates the speed setting, which may change with link for 100/1000.
1621 *
1622 * This function should probably be checking for PAUSE support at some point
1623 * in the future. Currently Per-priority-pause is not determinable using
1624 * ethtool, so we shouldn't be restrictive until that problem is resolved.
1625 *
1626 * Returns: 0 if link is OK for use by FCoE.
1627 *
1628 */
1629int fcoe_link_ok(struct fc_lport *lp)
1630{
Robert Lovefc47ff62009-02-27 10:55:55 -08001631 struct fcoe_softc *fc = lport_priv(lp);
Robert Love85b4aa42008-12-09 15:10:24 -08001632 struct net_device *dev = fc->real_dev;
1633 struct ethtool_cmd ecmd = { ETHTOOL_GSET };
Robert Love85b4aa42008-12-09 15:10:24 -08001634
Yi Zou2f718d62009-07-29 17:04:01 -07001635 if ((dev->flags & IFF_UP) && netif_carrier_ok(dev) &&
1636 (!dev_ethtool_get_settings(dev, &ecmd))) {
1637 lp->link_supported_speeds &=
1638 ~(FC_PORTSPEED_1GBIT | FC_PORTSPEED_10GBIT);
1639 if (ecmd.supported & (SUPPORTED_1000baseT_Half |
1640 SUPPORTED_1000baseT_Full))
1641 lp->link_supported_speeds |= FC_PORTSPEED_1GBIT;
1642 if (ecmd.supported & SUPPORTED_10000baseT_Full)
1643 lp->link_supported_speeds |=
1644 FC_PORTSPEED_10GBIT;
1645 if (ecmd.speed == SPEED_1000)
1646 lp->link_speed = FC_PORTSPEED_1GBIT;
1647 if (ecmd.speed == SPEED_10000)
1648 lp->link_speed = FC_PORTSPEED_10GBIT;
Robert Love85b4aa42008-12-09 15:10:24 -08001649
Yi Zou2f718d62009-07-29 17:04:01 -07001650 return 0;
1651 }
1652 return -1;
Robert Love85b4aa42008-12-09 15:10:24 -08001653}
Robert Love85b4aa42008-12-09 15:10:24 -08001654
Robert Love34f42a02009-02-27 10:55:45 -08001655/**
1656 * fcoe_percpu_clean() - Clear the pending skbs for an lport
Robert Love85b4aa42008-12-09 15:10:24 -08001657 * @lp: the fc_lport
1658 */
1659void fcoe_percpu_clean(struct fc_lport *lp)
1660{
Robert Love85b4aa42008-12-09 15:10:24 -08001661 struct fcoe_percpu_s *pp;
1662 struct fcoe_rcv_info *fr;
1663 struct sk_buff_head *list;
1664 struct sk_buff *skb, *next;
1665 struct sk_buff *head;
Robert Love5e5e92d2009-03-17 11:41:35 -07001666 unsigned int cpu;
Robert Love85b4aa42008-12-09 15:10:24 -08001667
Robert Love5e5e92d2009-03-17 11:41:35 -07001668 for_each_possible_cpu(cpu) {
1669 pp = &per_cpu(fcoe_percpu, cpu);
1670 spin_lock_bh(&pp->fcoe_rx_list.lock);
1671 list = &pp->fcoe_rx_list;
1672 head = list->next;
1673 for (skb = head; skb != (struct sk_buff *)list;
1674 skb = next) {
1675 next = skb->next;
1676 fr = fcoe_dev_from_skb(skb);
1677 if (fr->fr_dev == lp) {
1678 __skb_unlink(skb, list);
1679 kfree_skb(skb);
Robert Love85b4aa42008-12-09 15:10:24 -08001680 }
Robert Love85b4aa42008-12-09 15:10:24 -08001681 }
Robert Love5e5e92d2009-03-17 11:41:35 -07001682 spin_unlock_bh(&pp->fcoe_rx_list.lock);
Robert Love85b4aa42008-12-09 15:10:24 -08001683 }
1684}
Robert Love85b4aa42008-12-09 15:10:24 -08001685
1686/**
Robert Love34f42a02009-02-27 10:55:45 -08001687 * fcoe_clean_pending_queue() - Dequeue a skb and free it
Robert Love85b4aa42008-12-09 15:10:24 -08001688 * @lp: the corresponding fc_lport
1689 *
1690 * Returns: none
Robert Love34f42a02009-02-27 10:55:45 -08001691 */
Robert Love85b4aa42008-12-09 15:10:24 -08001692void fcoe_clean_pending_queue(struct fc_lport *lp)
1693{
1694 struct fcoe_softc *fc = lport_priv(lp);
1695 struct sk_buff *skb;
1696
1697 spin_lock_bh(&fc->fcoe_pending_queue.lock);
1698 while ((skb = __skb_dequeue(&fc->fcoe_pending_queue)) != NULL) {
1699 spin_unlock_bh(&fc->fcoe_pending_queue.lock);
1700 kfree_skb(skb);
1701 spin_lock_bh(&fc->fcoe_pending_queue.lock);
1702 }
1703 spin_unlock_bh(&fc->fcoe_pending_queue.lock);
1704}
Robert Love85b4aa42008-12-09 15:10:24 -08001705
1706/**
Robert Love34f42a02009-02-27 10:55:45 -08001707 * fcoe_reset() - Resets the fcoe
Robert Love85b4aa42008-12-09 15:10:24 -08001708 * @shost: shost the reset is from
1709 *
1710 * Returns: always 0
1711 */
1712int fcoe_reset(struct Scsi_Host *shost)
1713{
1714 struct fc_lport *lport = shost_priv(shost);
1715 fc_lport_reset(lport);
1716 return 0;
1717}
Robert Love85b4aa42008-12-09 15:10:24 -08001718
Robert Love34f42a02009-02-27 10:55:45 -08001719/**
Robert Love34f42a02009-02-27 10:55:45 -08001720 * fcoe_hostlist_lookup_softc() - find the corresponding lport by a given device
Chris Leechdd3fd722009-04-21 16:27:36 -07001721 * @dev: this is currently ptr to net_device
Robert Love85b4aa42008-12-09 15:10:24 -08001722 *
1723 * Returns: NULL or the located fcoe_softc
1724 */
Robert Loveb2ab99c2009-02-27 10:55:50 -08001725static struct fcoe_softc *
1726fcoe_hostlist_lookup_softc(const struct net_device *dev)
Robert Love85b4aa42008-12-09 15:10:24 -08001727{
1728 struct fcoe_softc *fc;
1729
1730 read_lock(&fcoe_hostlist_lock);
1731 list_for_each_entry(fc, &fcoe_hostlist, list) {
1732 if (fc->real_dev == dev) {
1733 read_unlock(&fcoe_hostlist_lock);
1734 return fc;
1735 }
1736 }
1737 read_unlock(&fcoe_hostlist_lock);
1738 return NULL;
1739}
1740
Robert Love34f42a02009-02-27 10:55:45 -08001741/**
1742 * fcoe_hostlist_lookup() - Find the corresponding lport by netdev
Robert Love85b4aa42008-12-09 15:10:24 -08001743 * @netdev: ptr to net_device
1744 *
1745 * Returns: 0 for success
1746 */
1747struct fc_lport *fcoe_hostlist_lookup(const struct net_device *netdev)
1748{
1749 struct fcoe_softc *fc;
1750
1751 fc = fcoe_hostlist_lookup_softc(netdev);
1752
Joe Eykholt97c83892009-03-17 11:42:40 -07001753 return (fc) ? fc->ctlr.lp : NULL;
Robert Love85b4aa42008-12-09 15:10:24 -08001754}
Robert Love85b4aa42008-12-09 15:10:24 -08001755
Robert Love34f42a02009-02-27 10:55:45 -08001756/**
1757 * fcoe_hostlist_add() - Add a lport to lports list
Chris Leechdd3fd722009-04-21 16:27:36 -07001758 * @lp: ptr to the fc_lport to be added
Robert Love85b4aa42008-12-09 15:10:24 -08001759 *
1760 * Returns: 0 for success
1761 */
1762int fcoe_hostlist_add(const struct fc_lport *lp)
1763{
1764 struct fcoe_softc *fc;
1765
1766 fc = fcoe_hostlist_lookup_softc(fcoe_netdev(lp));
1767 if (!fc) {
Robert Lovefc47ff62009-02-27 10:55:55 -08001768 fc = lport_priv(lp);
Robert Love85b4aa42008-12-09 15:10:24 -08001769 write_lock_bh(&fcoe_hostlist_lock);
1770 list_add_tail(&fc->list, &fcoe_hostlist);
1771 write_unlock_bh(&fcoe_hostlist_lock);
1772 }
1773 return 0;
1774}
Robert Love85b4aa42008-12-09 15:10:24 -08001775
Robert Love34f42a02009-02-27 10:55:45 -08001776/**
1777 * fcoe_hostlist_remove() - remove a lport from lports list
Chris Leechdd3fd722009-04-21 16:27:36 -07001778 * @lp: ptr to the fc_lport to be removed
Robert Love85b4aa42008-12-09 15:10:24 -08001779 *
1780 * Returns: 0 for success
1781 */
1782int fcoe_hostlist_remove(const struct fc_lport *lp)
1783{
1784 struct fcoe_softc *fc;
1785
1786 fc = fcoe_hostlist_lookup_softc(fcoe_netdev(lp));
1787 BUG_ON(!fc);
1788 write_lock_bh(&fcoe_hostlist_lock);
1789 list_del(&fc->list);
1790 write_unlock_bh(&fcoe_hostlist_lock);
1791
1792 return 0;
1793}
Robert Love85b4aa42008-12-09 15:10:24 -08001794
1795/**
Robert Love34f42a02009-02-27 10:55:45 -08001796 * fcoe_init() - fcoe module loading initialization
Robert Love85b4aa42008-12-09 15:10:24 -08001797 *
Robert Love85b4aa42008-12-09 15:10:24 -08001798 * Returns 0 on success, negative on failure
Robert Love34f42a02009-02-27 10:55:45 -08001799 */
Robert Love85b4aa42008-12-09 15:10:24 -08001800static int __init fcoe_init(void)
1801{
Robert Love38eccab2009-03-17 11:41:30 -07001802 unsigned int cpu;
Robert Love8976f422009-03-17 11:41:46 -07001803 int rc = 0;
Robert Love85b4aa42008-12-09 15:10:24 -08001804 struct fcoe_percpu_s *p;
1805
Robert Love85b4aa42008-12-09 15:10:24 -08001806 INIT_LIST_HEAD(&fcoe_hostlist);
1807 rwlock_init(&fcoe_hostlist_lock);
1808
Robert Love38eccab2009-03-17 11:41:30 -07001809 for_each_possible_cpu(cpu) {
Robert Love5e5e92d2009-03-17 11:41:35 -07001810 p = &per_cpu(fcoe_percpu, cpu);
Robert Love38eccab2009-03-17 11:41:30 -07001811 skb_queue_head_init(&p->fcoe_rx_list);
1812 }
1813
Robert Love8976f422009-03-17 11:41:46 -07001814 for_each_online_cpu(cpu)
1815 fcoe_percpu_thread_create(cpu);
Robert Love85b4aa42008-12-09 15:10:24 -08001816
Robert Love8976f422009-03-17 11:41:46 -07001817 /* Initialize per CPU interrupt thread */
1818 rc = register_hotcpu_notifier(&fcoe_cpu_notifier);
1819 if (rc)
1820 goto out_free;
Robert Love85b4aa42008-12-09 15:10:24 -08001821
Robert Love8976f422009-03-17 11:41:46 -07001822 /* Setup link change notification */
Robert Love85b4aa42008-12-09 15:10:24 -08001823 fcoe_dev_setup();
1824
Vasu Dev7f349142009-03-27 09:06:31 -07001825 fcoe_if_init();
Robert Love85b4aa42008-12-09 15:10:24 -08001826
1827 return 0;
Robert Love8976f422009-03-17 11:41:46 -07001828
1829out_free:
1830 for_each_online_cpu(cpu) {
1831 fcoe_percpu_thread_destroy(cpu);
1832 }
1833
1834 return rc;
Robert Love85b4aa42008-12-09 15:10:24 -08001835}
1836module_init(fcoe_init);
1837
1838/**
Robert Love34f42a02009-02-27 10:55:45 -08001839 * fcoe_exit() - fcoe module unloading cleanup
Robert Love85b4aa42008-12-09 15:10:24 -08001840 *
1841 * Returns 0 on success, negative on failure
Robert Love34f42a02009-02-27 10:55:45 -08001842 */
Robert Love85b4aa42008-12-09 15:10:24 -08001843static void __exit fcoe_exit(void)
1844{
Robert Love5e5e92d2009-03-17 11:41:35 -07001845 unsigned int cpu;
Robert Love85b4aa42008-12-09 15:10:24 -08001846 struct fcoe_softc *fc, *tmp;
Robert Love85b4aa42008-12-09 15:10:24 -08001847
Robert Love85b4aa42008-12-09 15:10:24 -08001848 fcoe_dev_cleanup();
1849
Vasu Dev5919a592009-03-27 09:03:29 -07001850 /* releases the associated fcoe hosts */
Robert Love85b4aa42008-12-09 15:10:24 -08001851 list_for_each_entry_safe(fc, tmp, &fcoe_hostlist, list)
Vasu Dev7f349142009-03-27 09:06:31 -07001852 fcoe_if_destroy(fc->real_dev);
Robert Love85b4aa42008-12-09 15:10:24 -08001853
Robert Love8976f422009-03-17 11:41:46 -07001854 unregister_hotcpu_notifier(&fcoe_cpu_notifier);
1855
1856 for_each_online_cpu(cpu) {
1857 fcoe_percpu_thread_destroy(cpu);
Robert Love85b4aa42008-12-09 15:10:24 -08001858 }
1859
Vasu Dev7f349142009-03-27 09:06:31 -07001860 /* detach from scsi transport */
1861 fcoe_if_exit();
Robert Love85b4aa42008-12-09 15:10:24 -08001862}
1863module_exit(fcoe_exit);