blob: c215235ee39feb10ec4d1af49ed962009da07307 [file] [log] [blame]
Robert Love85b4aa42008-12-09 15:10:24 -08001/*
Chris Leechaf7f85d2009-08-25 13:59:24 -07002 * Copyright(c) 2007 - 2009 Intel Corporation. All rights reserved.
Robert Love85b4aa42008-12-09 15:10:24 -08003 *
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
Yi Zou05cc7392009-08-25 13:59:03 -070052/* Performance tuning parameters for fcoe */
53static unsigned int fcoe_ddp_min;
54module_param_named(ddp_min, fcoe_ddp_min, uint, S_IRUGO | S_IWUSR);
55MODULE_PARM_DESC(ddp_min, "Minimum I/O size in bytes for " \
56 "Direct Data Placement (DDP).");
57
Robert Love85b4aa42008-12-09 15:10:24 -080058/* fcoe host list */
59LIST_HEAD(fcoe_hostlist);
60DEFINE_RWLOCK(fcoe_hostlist_lock);
Robert Love5e5e92d2009-03-17 11:41:35 -070061DEFINE_PER_CPU(struct fcoe_percpu_s, fcoe_percpu);
Robert Love85b4aa42008-12-09 15:10:24 -080062
Chris Leechdd3fd722009-04-21 16:27:36 -070063/* Function Prototypes */
Vasu Devfdd78022009-03-17 11:42:24 -070064static int fcoe_reset(struct Scsi_Host *shost);
65static int fcoe_xmit(struct fc_lport *, struct fc_frame *);
66static int fcoe_rcv(struct sk_buff *, struct net_device *,
67 struct packet_type *, struct net_device *);
68static int fcoe_percpu_receive_thread(void *arg);
69static void fcoe_clean_pending_queue(struct fc_lport *lp);
70static void fcoe_percpu_clean(struct fc_lport *lp);
71static int fcoe_link_ok(struct fc_lport *lp);
72
73static struct fc_lport *fcoe_hostlist_lookup(const struct net_device *);
74static int fcoe_hostlist_add(const struct fc_lport *);
75static int fcoe_hostlist_remove(const struct fc_lport *);
76
Vasu Dev4bb6b512009-05-06 10:52:34 -070077static void fcoe_check_wait_queue(struct fc_lport *, struct sk_buff *);
Robert Love85b4aa42008-12-09 15:10:24 -080078static int fcoe_device_notification(struct notifier_block *, ulong, void *);
79static void fcoe_dev_setup(void);
80static void fcoe_dev_cleanup(void);
81
82/* notification function from net device */
83static struct notifier_block fcoe_notifier = {
84 .notifier_call = fcoe_device_notification,
85};
86
Vasu Dev7f349142009-03-27 09:06:31 -070087static struct scsi_transport_template *scsi_transport_fcoe_sw;
88
89struct fc_function_template fcoe_transport_function = {
90 .show_host_node_name = 1,
91 .show_host_port_name = 1,
92 .show_host_supported_classes = 1,
93 .show_host_supported_fc4s = 1,
94 .show_host_active_fc4s = 1,
95 .show_host_maxframe_size = 1,
96
97 .show_host_port_id = 1,
98 .show_host_supported_speeds = 1,
99 .get_host_speed = fc_get_host_speed,
100 .show_host_speed = 1,
101 .show_host_port_type = 1,
102 .get_host_port_state = fc_get_host_port_state,
103 .show_host_port_state = 1,
104 .show_host_symbolic_name = 1,
105
106 .dd_fcrport_size = sizeof(struct fc_rport_libfc_priv),
107 .show_rport_maxframe_size = 1,
108 .show_rport_supported_classes = 1,
109
110 .show_host_fabric_name = 1,
111 .show_starget_node_name = 1,
112 .show_starget_port_name = 1,
113 .show_starget_port_id = 1,
114 .set_rport_dev_loss_tmo = fc_set_rport_loss_tmo,
115 .show_rport_dev_loss_tmo = 1,
116 .get_fc_host_stats = fc_get_host_stats,
117 .issue_fc_host_lip = fcoe_reset,
118
119 .terminate_rport_io = fc_rport_terminate_io,
120};
121
122static struct scsi_host_template fcoe_shost_template = {
123 .module = THIS_MODULE,
124 .name = "FCoE Driver",
125 .proc_name = FCOE_NAME,
126 .queuecommand = fc_queuecommand,
127 .eh_abort_handler = fc_eh_abort,
128 .eh_device_reset_handler = fc_eh_device_reset,
129 .eh_host_reset_handler = fc_eh_host_reset,
130 .slave_alloc = fc_slave_alloc,
131 .change_queue_depth = fc_change_queue_depth,
132 .change_queue_type = fc_change_queue_type,
133 .this_id = -1,
134 .cmd_per_lun = 32,
135 .can_queue = FCOE_MAX_OUTSTANDING_COMMANDS,
136 .use_clustering = ENABLE_CLUSTERING,
137 .sg_tablesize = SG_ALL,
138 .max_sectors = 0xffff,
139};
140
141/**
Vasu Devab6b85c2009-05-17 12:33:08 +0000142 * fcoe_fip_recv - handle a received FIP frame.
143 * @skb: the receive skb
144 * @dev: associated &net_device
145 * @ptype: the &packet_type structure which was used to register this handler.
146 * @orig_dev: original receive &net_device, in case @dev is a bond.
147 *
148 * Returns: 0 for success
149 */
150static int fcoe_fip_recv(struct sk_buff *skb, struct net_device *dev,
151 struct packet_type *ptype,
152 struct net_device *orig_dev)
153{
Chris Leech259ad852009-08-25 13:59:41 -0700154 struct fcoe_interface *fcoe;
Chris Leech014f5c32009-08-25 13:59:30 -0700155 struct fcoe_port *port;
Vasu Devab6b85c2009-05-17 12:33:08 +0000156
Chris Leech259ad852009-08-25 13:59:41 -0700157 fcoe = container_of(ptype, struct fcoe_interface, fip_packet_type);
158 port = fcoe->priv;
Chris Leech014f5c32009-08-25 13:59:30 -0700159 fcoe_ctlr_recv(&port->ctlr, skb);
Vasu Devab6b85c2009-05-17 12:33:08 +0000160 return 0;
161}
162
163/**
164 * fcoe_fip_send() - send an Ethernet-encapsulated FIP frame.
165 * @fip: FCoE controller.
166 * @skb: FIP Packet.
167 */
168static void fcoe_fip_send(struct fcoe_ctlr *fip, struct sk_buff *skb)
169{
Chris Leech25024982009-08-25 13:59:35 -0700170 skb->dev = fcoe_from_ctlr(fip)->fcoe->netdev;
Vasu Devab6b85c2009-05-17 12:33:08 +0000171 dev_queue_xmit(skb);
172}
173
174/**
175 * fcoe_update_src_mac() - Update Ethernet MAC filters.
176 * @fip: FCoE controller.
177 * @old: Unicast MAC address to delete if the MAC is non-zero.
178 * @new: Unicast MAC address to add.
179 *
180 * Remove any previously-set unicast MAC filter.
181 * Add secondary FCoE MAC address filter for our OUI.
182 */
183static void fcoe_update_src_mac(struct fcoe_ctlr *fip, u8 *old, u8 *new)
184{
Chris Leech25024982009-08-25 13:59:35 -0700185 struct fcoe_interface *fcoe;
Chris Leech014f5c32009-08-25 13:59:30 -0700186 struct fcoe_port *port;
Vasu Devab6b85c2009-05-17 12:33:08 +0000187
Chris Leech014f5c32009-08-25 13:59:30 -0700188 port = fcoe_from_ctlr(fip);
Chris Leech25024982009-08-25 13:59:35 -0700189 fcoe = port->fcoe;
190
Vasu Devab6b85c2009-05-17 12:33:08 +0000191 rtnl_lock();
192 if (!is_zero_ether_addr(old))
Chris Leech25024982009-08-25 13:59:35 -0700193 dev_unicast_delete(fcoe->netdev, old);
194 dev_unicast_add(fcoe->netdev, new);
Vasu Devab6b85c2009-05-17 12:33:08 +0000195 rtnl_unlock();
196}
197
198/**
Vasu Dev7f349142009-03-27 09:06:31 -0700199 * fcoe_lport_config() - sets up the fc_lport
200 * @lp: ptr to the fc_lport
Vasu Dev7f349142009-03-27 09:06:31 -0700201 *
202 * Returns: 0 for success
203 */
204static int fcoe_lport_config(struct fc_lport *lp)
205{
206 lp->link_up = 0;
207 lp->qfull = 0;
208 lp->max_retry_count = 3;
Abhijeet Joglekara3666952009-05-01 10:01:26 -0700209 lp->max_rport_retry_count = 3;
Vasu Dev7f349142009-03-27 09:06:31 -0700210 lp->e_d_tov = 2 * 1000; /* FC-FS default */
211 lp->r_a_tov = 2 * 2 * 1000;
212 lp->service_params = (FCP_SPPF_INIT_FCN | FCP_SPPF_RD_XRDY_DIS |
213 FCP_SPPF_RETRY | FCP_SPPF_CONF_COMPL);
214
215 fc_lport_init_stats(lp);
216
217 /* lport fc_lport related configuration */
218 fc_lport_config(lp);
219
220 /* offload related configuration */
221 lp->crc_offload = 0;
222 lp->seq_offload = 0;
223 lp->lro_enabled = 0;
224 lp->lro_xid = 0;
225 lp->lso_max = 0;
226
227 return 0;
228}
229
230/**
Vasu Devab6b85c2009-05-17 12:33:08 +0000231 * fcoe_netdev_cleanup() - clean up netdev configurations
Chris Leech014f5c32009-08-25 13:59:30 -0700232 * @port: ptr to the fcoe_port
Vasu Devab6b85c2009-05-17 12:33:08 +0000233 */
Chris Leech014f5c32009-08-25 13:59:30 -0700234void fcoe_netdev_cleanup(struct fcoe_port *port)
Vasu Devab6b85c2009-05-17 12:33:08 +0000235{
236 u8 flogi_maddr[ETH_ALEN];
Chris Leech25024982009-08-25 13:59:35 -0700237 struct fcoe_interface *fcoe = port->fcoe;
Vasu Devab6b85c2009-05-17 12:33:08 +0000238
239 /* Don't listen for Ethernet packets anymore */
Chris Leech259ad852009-08-25 13:59:41 -0700240 dev_remove_pack(&fcoe->fcoe_packet_type);
241 dev_remove_pack(&fcoe->fip_packet_type);
Vasu Devab6b85c2009-05-17 12:33:08 +0000242
243 /* Delete secondary MAC addresses */
244 rtnl_lock();
245 memcpy(flogi_maddr, (u8[6]) FC_FCOE_FLOGI_MAC, ETH_ALEN);
Chris Leech25024982009-08-25 13:59:35 -0700246 dev_unicast_delete(fcoe->netdev, flogi_maddr);
Chris Leech014f5c32009-08-25 13:59:30 -0700247 if (!is_zero_ether_addr(port->ctlr.data_src_addr))
Chris Leech25024982009-08-25 13:59:35 -0700248 dev_unicast_delete(fcoe->netdev, port->ctlr.data_src_addr);
Chris Leech014f5c32009-08-25 13:59:30 -0700249 if (port->ctlr.spma)
Chris Leech25024982009-08-25 13:59:35 -0700250 dev_unicast_delete(fcoe->netdev, port->ctlr.ctl_src_addr);
251 dev_mc_delete(fcoe->netdev, FIP_ALL_ENODE_MACS, ETH_ALEN, 0);
Vasu Devab6b85c2009-05-17 12:33:08 +0000252 rtnl_unlock();
253}
254
255/**
Vasu Dev1047f222009-05-06 10:52:40 -0700256 * fcoe_queue_timer() - fcoe queue timer
257 * @lp: the fc_lport pointer
258 *
259 * Calls fcoe_check_wait_queue on timeout
260 *
261 */
262static void fcoe_queue_timer(ulong lp)
263{
264 fcoe_check_wait_queue((struct fc_lport *)lp, NULL);
265}
266
267/**
Vasu Dev7f349142009-03-27 09:06:31 -0700268 * fcoe_netdev_config() - Set up netdev for SW FCoE
269 * @lp : ptr to the fc_lport
270 * @netdev : ptr to the associated netdevice struct
271 *
272 * Must be called after fcoe_lport_config() as it will use lport mutex
273 *
274 * Returns : 0 for success
275 */
276static int fcoe_netdev_config(struct fc_lport *lp, struct net_device *netdev)
277{
278 u32 mfs;
279 u64 wwnn, wwpn;
Chris Leech25024982009-08-25 13:59:35 -0700280 struct fcoe_interface *fcoe;
Chris Leech014f5c32009-08-25 13:59:30 -0700281 struct fcoe_port *port;
Vasu Dev7f349142009-03-27 09:06:31 -0700282 u8 flogi_maddr[ETH_ALEN];
Vasu Dev184dd342009-05-17 12:33:28 +0000283 struct netdev_hw_addr *ha;
Vasu Dev7f349142009-03-27 09:06:31 -0700284
285 /* Setup lport private data to point to fcoe softc */
Chris Leech014f5c32009-08-25 13:59:30 -0700286 port = lport_priv(lp);
Chris Leech25024982009-08-25 13:59:35 -0700287 fcoe = port->fcoe;
Chris Leech014f5c32009-08-25 13:59:30 -0700288 port->ctlr.lp = lp;
Chris Leech25024982009-08-25 13:59:35 -0700289 fcoe->netdev = netdev;
Vasu Dev7f349142009-03-27 09:06:31 -0700290
291 /* Do not support for bonding device */
Vasu Dev1d1b88d2009-07-29 17:05:45 -0700292 if ((netdev->priv_flags & IFF_MASTER_ALB) ||
293 (netdev->priv_flags & IFF_SLAVE_INACTIVE) ||
294 (netdev->priv_flags & IFF_MASTER_8023AD)) {
Vasu Dev7f349142009-03-27 09:06:31 -0700295 return -EOPNOTSUPP;
296 }
297
298 /*
299 * Determine max frame size based on underlying device and optional
300 * user-configured limit. If the MFS is too low, fcoe_link_ok()
301 * will return 0, so do this first.
302 */
Vasu Dev1d1b88d2009-07-29 17:05:45 -0700303 mfs = netdev->mtu - (sizeof(struct fcoe_hdr) +
304 sizeof(struct fcoe_crc_eof));
Vasu Dev7f349142009-03-27 09:06:31 -0700305 if (fc_set_mfs(lp, mfs))
306 return -EINVAL;
307
Vasu Dev7f349142009-03-27 09:06:31 -0700308 /* offload features support */
Vasu Dev1d1b88d2009-07-29 17:05:45 -0700309 if (netdev->features & NETIF_F_SG)
Vasu Dev7f349142009-03-27 09:06:31 -0700310 lp->sg_supp = 1;
311
Vasu Dev7f349142009-03-27 09:06:31 -0700312 if (netdev->features & NETIF_F_FCOE_CRC) {
313 lp->crc_offload = 1;
Robert Loved5488eb2009-06-10 15:30:59 -0700314 FCOE_NETDEV_DBG(netdev, "Supports FCCRC offload\n");
Vasu Dev7f349142009-03-27 09:06:31 -0700315 }
Vasu Dev7f349142009-03-27 09:06:31 -0700316 if (netdev->features & NETIF_F_FSO) {
317 lp->seq_offload = 1;
318 lp->lso_max = netdev->gso_max_size;
Robert Loved5488eb2009-06-10 15:30:59 -0700319 FCOE_NETDEV_DBG(netdev, "Supports LSO for max len 0x%x\n",
320 lp->lso_max);
Vasu Dev7f349142009-03-27 09:06:31 -0700321 }
Vasu Dev7f349142009-03-27 09:06:31 -0700322 if (netdev->fcoe_ddp_xid) {
323 lp->lro_enabled = 1;
324 lp->lro_xid = netdev->fcoe_ddp_xid;
Robert Loved5488eb2009-06-10 15:30:59 -0700325 FCOE_NETDEV_DBG(netdev, "Supports LRO for max xid 0x%x\n",
326 lp->lro_xid);
Vasu Dev7f349142009-03-27 09:06:31 -0700327 }
Chris Leech014f5c32009-08-25 13:59:30 -0700328 skb_queue_head_init(&port->fcoe_pending_queue);
329 port->fcoe_pending_queue_active = 0;
330 setup_timer(&port->timer, fcoe_queue_timer, (unsigned long)lp);
Vasu Dev7f349142009-03-27 09:06:31 -0700331
Vasu Dev184dd342009-05-17 12:33:28 +0000332 /* look for SAN MAC address, if multiple SAN MACs exist, only
333 * use the first one for SPMA */
334 rcu_read_lock();
335 for_each_dev_addr(netdev, ha) {
336 if ((ha->type == NETDEV_HW_ADDR_T_SAN) &&
Chris Leech014f5c32009-08-25 13:59:30 -0700337 (is_valid_ether_addr(port->ctlr.ctl_src_addr))) {
338 memcpy(port->ctlr.ctl_src_addr, ha->addr, ETH_ALEN);
339 port->ctlr.spma = 1;
Vasu Dev184dd342009-05-17 12:33:28 +0000340 break;
341 }
342 }
343 rcu_read_unlock();
344
Vasu Dev7f349142009-03-27 09:06:31 -0700345 /* setup Source Mac Address */
Chris Leech014f5c32009-08-25 13:59:30 -0700346 if (!port->ctlr.spma)
347 memcpy(port->ctlr.ctl_src_addr, netdev->dev_addr,
348 netdev->addr_len);
Vasu Dev7f349142009-03-27 09:06:31 -0700349
Vasu Dev1d1b88d2009-07-29 17:05:45 -0700350 wwnn = fcoe_wwn_from_mac(netdev->dev_addr, 1, 0);
Vasu Dev7f349142009-03-27 09:06:31 -0700351 fc_set_wwnn(lp, wwnn);
352 /* XXX - 3rd arg needs to be vlan id */
Vasu Dev1d1b88d2009-07-29 17:05:45 -0700353 wwpn = fcoe_wwn_from_mac(netdev->dev_addr, 2, 0);
Vasu Dev7f349142009-03-27 09:06:31 -0700354 fc_set_wwpn(lp, wwpn);
355
356 /*
357 * Add FCoE MAC address as second unicast MAC address
358 * or enter promiscuous mode if not capable of listening
359 * for multiple unicast MACs.
360 */
361 rtnl_lock();
362 memcpy(flogi_maddr, (u8[6]) FC_FCOE_FLOGI_MAC, ETH_ALEN);
Vasu Dev1d1b88d2009-07-29 17:05:45 -0700363 dev_unicast_add(netdev, flogi_maddr);
Chris Leech014f5c32009-08-25 13:59:30 -0700364 if (port->ctlr.spma)
365 dev_unicast_add(netdev, port->ctlr.ctl_src_addr);
Vasu Dev1d1b88d2009-07-29 17:05:45 -0700366 dev_mc_add(netdev, FIP_ALL_ENODE_MACS, ETH_ALEN, 0);
Vasu Dev7f349142009-03-27 09:06:31 -0700367 rtnl_unlock();
368
369 /*
370 * setup the receive function from ethernet driver
371 * on the ethertype for the given device
372 */
Chris Leech259ad852009-08-25 13:59:41 -0700373 fcoe->fcoe_packet_type.func = fcoe_rcv;
374 fcoe->fcoe_packet_type.type = __constant_htons(ETH_P_FCOE);
375 fcoe->fcoe_packet_type.dev = netdev;
376 dev_add_pack(&fcoe->fcoe_packet_type);
Vasu Dev7f349142009-03-27 09:06:31 -0700377
Chris Leech259ad852009-08-25 13:59:41 -0700378 fcoe->fip_packet_type.func = fcoe_fip_recv;
379 fcoe->fip_packet_type.type = htons(ETH_P_FIP);
380 fcoe->fip_packet_type.dev = netdev;
381 dev_add_pack(&fcoe->fip_packet_type);
Vasu Devab6b85c2009-05-17 12:33:08 +0000382
Vasu Dev7f349142009-03-27 09:06:31 -0700383 return 0;
384}
385
386/**
387 * fcoe_shost_config() - Sets up fc_lport->host
388 * @lp : ptr to the fc_lport
389 * @shost : ptr to the associated scsi host
390 * @dev : device associated to scsi host
391 *
392 * Must be called after fcoe_lport_config() and fcoe_netdev_config()
393 *
394 * Returns : 0 for success
395 */
396static int fcoe_shost_config(struct fc_lport *lp, struct Scsi_Host *shost,
397 struct device *dev)
398{
399 int rc = 0;
400
401 /* lport scsi host config */
402 lp->host = shost;
403
404 lp->host->max_lun = FCOE_MAX_LUN;
405 lp->host->max_id = FCOE_MAX_FCP_TARGET;
406 lp->host->max_channel = 0;
407 lp->host->transportt = scsi_transport_fcoe_sw;
408
409 /* add the new host to the SCSI-ml */
410 rc = scsi_add_host(lp->host, dev);
411 if (rc) {
Robert Loved5488eb2009-06-10 15:30:59 -0700412 FCOE_NETDEV_DBG(fcoe_netdev(lp), "fcoe_shost_config: "
413 "error on scsi_add_host\n");
Vasu Dev7f349142009-03-27 09:06:31 -0700414 return rc;
415 }
416 sprintf(fc_host_symbolic_name(lp->host), "%s v%s over %s",
417 FCOE_NAME, FCOE_VERSION,
418 fcoe_netdev(lp)->name);
419
420 return 0;
421}
422
Vasu Devd7179682009-07-29 17:05:21 -0700423/*
424 * fcoe_oem_match() - match for read types IO
425 * @fp: the fc_frame for new IO.
426 *
427 * Returns : true for read types IO, otherwise returns false.
428 */
429bool fcoe_oem_match(struct fc_frame *fp)
430{
Yi Zou05cc7392009-08-25 13:59:03 -0700431 return fc_fcp_is_read(fr_fsp(fp)) &&
432 (fr_fsp(fp)->data_len > fcoe_ddp_min);
Vasu Devd7179682009-07-29 17:05:21 -0700433}
434
Vasu Dev7f349142009-03-27 09:06:31 -0700435/**
436 * fcoe_em_config() - allocates em for this lport
Chris Leech014f5c32009-08-25 13:59:30 -0700437 * @lp: the fcoe that em is to allocated for
Vasu Dev7f349142009-03-27 09:06:31 -0700438 *
Vasu Deve8af4d42009-07-29 17:05:15 -0700439 * Called with write fcoe_hostlist_lock held.
440 *
Vasu Dev7f349142009-03-27 09:06:31 -0700441 * Returns : 0 on success
442 */
443static inline int fcoe_em_config(struct fc_lport *lp)
444{
Chris Leech014f5c32009-08-25 13:59:30 -0700445 struct fcoe_port *port = lport_priv(lp);
Chris Leech25024982009-08-25 13:59:35 -0700446 struct fcoe_port *oldport = NULL;
447 struct fcoe_interface *fcoe = port->fcoe;
448 struct fcoe_interface *oldfcoe = NULL;
Vasu Dev1d1b88d2009-07-29 17:05:45 -0700449 struct net_device *old_real_dev, *cur_real_dev;
Vasu Devd7179682009-07-29 17:05:21 -0700450 u16 min_xid = FCOE_MIN_XID;
451 u16 max_xid = FCOE_MAX_XID;
452
453 /*
454 * Check if need to allocate an em instance for
455 * offload exchange ids to be shared across all VN_PORTs/lport.
456 */
457 if (!lp->lro_enabled || !lp->lro_xid || (lp->lro_xid >= max_xid)) {
458 lp->lro_xid = 0;
459 goto skip_oem;
460 }
461
462 /*
463 * Reuse existing offload em instance in case
Vasu Dev1d1b88d2009-07-29 17:05:45 -0700464 * it is already allocated on real eth device
Vasu Devd7179682009-07-29 17:05:21 -0700465 */
Chris Leech25024982009-08-25 13:59:35 -0700466 if (fcoe->netdev->priv_flags & IFF_802_1Q_VLAN)
467 cur_real_dev = vlan_dev_real_dev(fcoe->netdev);
Vasu Dev1d1b88d2009-07-29 17:05:45 -0700468 else
Chris Leech25024982009-08-25 13:59:35 -0700469 cur_real_dev = fcoe->netdev;
Vasu Dev1d1b88d2009-07-29 17:05:45 -0700470
Chris Leech25024982009-08-25 13:59:35 -0700471 list_for_each_entry(oldfcoe, &fcoe_hostlist, list) {
472 oldport = oldfcoe->priv;
473 if (oldfcoe->netdev->priv_flags & IFF_802_1Q_VLAN)
474 old_real_dev = vlan_dev_real_dev(oldfcoe->netdev);
Vasu Dev1d1b88d2009-07-29 17:05:45 -0700475 else
Chris Leech25024982009-08-25 13:59:35 -0700476 old_real_dev = oldfcoe->netdev;
Vasu Dev1d1b88d2009-07-29 17:05:45 -0700477
478 if (cur_real_dev == old_real_dev) {
Chris Leech25024982009-08-25 13:59:35 -0700479 port->oem = oldport->oem;
Vasu Devd7179682009-07-29 17:05:21 -0700480 break;
481 }
482 }
483
Chris Leech014f5c32009-08-25 13:59:30 -0700484 if (port->oem) {
485 if (!fc_exch_mgr_add(lp, port->oem, fcoe_oem_match)) {
Vasu Devd7179682009-07-29 17:05:21 -0700486 printk(KERN_ERR "fcoe_em_config: failed to add "
487 "offload em:%p on interface:%s\n",
Chris Leech25024982009-08-25 13:59:35 -0700488 port->oem, fcoe->netdev->name);
Vasu Devd7179682009-07-29 17:05:21 -0700489 return -ENOMEM;
490 }
491 } else {
Chris Leech014f5c32009-08-25 13:59:30 -0700492 port->oem = fc_exch_mgr_alloc(lp, FC_CLASS_3,
Vasu Devd7179682009-07-29 17:05:21 -0700493 FCOE_MIN_XID, lp->lro_xid,
494 fcoe_oem_match);
Chris Leech014f5c32009-08-25 13:59:30 -0700495 if (!port->oem) {
Vasu Devd7179682009-07-29 17:05:21 -0700496 printk(KERN_ERR "fcoe_em_config: failed to allocate "
497 "em for offload exches on interface:%s\n",
Chris Leech25024982009-08-25 13:59:35 -0700498 fcoe->netdev->name);
Vasu Devd7179682009-07-29 17:05:21 -0700499 return -ENOMEM;
500 }
501 }
502
503 /*
504 * Exclude offload EM xid range from next EM xid range.
505 */
506 min_xid += lp->lro_xid + 1;
507
508skip_oem:
509 if (!fc_exch_mgr_alloc(lp, FC_CLASS_3, min_xid, max_xid, NULL)) {
510 printk(KERN_ERR "fcoe_em_config: failed to "
Chris Leech25024982009-08-25 13:59:35 -0700511 "allocate em on interface %s\n", fcoe->netdev->name);
Vasu Dev7f349142009-03-27 09:06:31 -0700512 return -ENOMEM;
Vasu Devd7179682009-07-29 17:05:21 -0700513 }
Vasu Dev7f349142009-03-27 09:06:31 -0700514
515 return 0;
516}
517
518/**
519 * fcoe_if_destroy() - FCoE software HBA tear-down function
Chris Leechaf7f85d2009-08-25 13:59:24 -0700520 * @lport: fc_lport to destroy
Vasu Dev7f349142009-03-27 09:06:31 -0700521 */
Chris Leechaf7f85d2009-08-25 13:59:24 -0700522static void fcoe_if_destroy(struct fc_lport *lport)
Vasu Dev7f349142009-03-27 09:06:31 -0700523{
Chris Leech014f5c32009-08-25 13:59:30 -0700524 struct fcoe_port *port = lport_priv(lport);
525 struct fcoe_interface *fcoe = port->fcoe;
Chris Leech25024982009-08-25 13:59:35 -0700526 struct net_device *netdev = fcoe->netdev;
Vasu Dev7f349142009-03-27 09:06:31 -0700527
Robert Loved5488eb2009-06-10 15:30:59 -0700528 FCOE_NETDEV_DBG(netdev, "Destroying interface\n");
Vasu Dev7f349142009-03-27 09:06:31 -0700529
Vasu Dev7f349142009-03-27 09:06:31 -0700530 /* Logout of the fabric */
Chris Leechaf7f85d2009-08-25 13:59:24 -0700531 fc_fabric_logoff(lport);
Vasu Dev7f349142009-03-27 09:06:31 -0700532
533 /* Remove the instance from fcoe's list */
Chris Leechaf7f85d2009-08-25 13:59:24 -0700534 fcoe_hostlist_remove(lport);
Vasu Dev7f349142009-03-27 09:06:31 -0700535
Vasu Devab6b85c2009-05-17 12:33:08 +0000536 /* clean up netdev configurations */
Chris Leech014f5c32009-08-25 13:59:30 -0700537 fcoe_netdev_cleanup(port);
Vasu Devab6b85c2009-05-17 12:33:08 +0000538
539 /* tear-down the FCoE controller */
Chris Leech014f5c32009-08-25 13:59:30 -0700540 fcoe_ctlr_destroy(&port->ctlr);
Vasu Dev7f349142009-03-27 09:06:31 -0700541
Joe Eykholtf161fb72009-07-29 17:04:17 -0700542 /* Free queued packets for the per-CPU receive threads */
Chris Leechaf7f85d2009-08-25 13:59:24 -0700543 fcoe_percpu_clean(lport);
Joe Eykholtf161fb72009-07-29 17:04:17 -0700544
Vasu Dev7f349142009-03-27 09:06:31 -0700545 /* Cleanup the fc_lport */
Chris Leechaf7f85d2009-08-25 13:59:24 -0700546 fc_lport_destroy(lport);
547 fc_fcp_destroy(lport);
Vasu Dev7f349142009-03-27 09:06:31 -0700548
549 /* Detach from the scsi-ml */
Chris Leechaf7f85d2009-08-25 13:59:24 -0700550 fc_remove_host(lport->host);
551 scsi_remove_host(lport->host);
Vasu Dev7f349142009-03-27 09:06:31 -0700552
553 /* There are no more rports or I/O, free the EM */
Chris Leechaf7f85d2009-08-25 13:59:24 -0700554 fc_exch_mgr_free(lport);
Vasu Dev7f349142009-03-27 09:06:31 -0700555
Vasu Dev7f349142009-03-27 09:06:31 -0700556 /* Free existing skbs */
Chris Leechaf7f85d2009-08-25 13:59:24 -0700557 fcoe_clean_pending_queue(lport);
Vasu Dev7f349142009-03-27 09:06:31 -0700558
Vasu Dev1047f222009-05-06 10:52:40 -0700559 /* Stop the timer */
Chris Leech014f5c32009-08-25 13:59:30 -0700560 del_timer_sync(&port->timer);
Vasu Dev1047f222009-05-06 10:52:40 -0700561
Vasu Dev7f349142009-03-27 09:06:31 -0700562 /* Free memory used by statistical counters */
Chris Leechaf7f85d2009-08-25 13:59:24 -0700563 fc_lport_free_stats(lport);
Vasu Dev7f349142009-03-27 09:06:31 -0700564
565 /* Release the net_device and Scsi_Host */
Vasu Dev1d1b88d2009-07-29 17:05:45 -0700566 dev_put(netdev);
Chris Leechaf7f85d2009-08-25 13:59:24 -0700567 scsi_host_put(lport->host);
Chris Leech014f5c32009-08-25 13:59:30 -0700568 kfree(fcoe); /* TODO, should be refcounted */
Vasu Dev7f349142009-03-27 09:06:31 -0700569}
570
571/*
572 * fcoe_ddp_setup - calls LLD's ddp_setup through net_device
573 * @lp: the corresponding fc_lport
574 * @xid: the exchange id for this ddp transfer
575 * @sgl: the scatterlist describing this transfer
576 * @sgc: number of sg items
577 *
578 * Returns : 0 no ddp
579 */
580static int fcoe_ddp_setup(struct fc_lport *lp, u16 xid,
581 struct scatterlist *sgl, unsigned int sgc)
582{
583 struct net_device *n = fcoe_netdev(lp);
584
585 if (n->netdev_ops && n->netdev_ops->ndo_fcoe_ddp_setup)
586 return n->netdev_ops->ndo_fcoe_ddp_setup(n, xid, sgl, sgc);
587
588 return 0;
589}
590
591/*
592 * fcoe_ddp_done - calls LLD's ddp_done through net_device
593 * @lp: the corresponding fc_lport
594 * @xid: the exchange id for this ddp transfer
595 *
596 * Returns : the length of data that have been completed by ddp
597 */
598static int fcoe_ddp_done(struct fc_lport *lp, u16 xid)
599{
600 struct net_device *n = fcoe_netdev(lp);
601
602 if (n->netdev_ops && n->netdev_ops->ndo_fcoe_ddp_done)
603 return n->netdev_ops->ndo_fcoe_ddp_done(n, xid);
604 return 0;
605}
606
607static struct libfc_function_template fcoe_libfc_fcn_templ = {
608 .frame_send = fcoe_xmit,
609 .ddp_setup = fcoe_ddp_setup,
610 .ddp_done = fcoe_ddp_done,
611};
612
613/**
614 * fcoe_if_create() - this function creates the fcoe interface
615 * @netdev: pointer the associated netdevice
Chris Leechaf7f85d2009-08-25 13:59:24 -0700616 * @parent: device pointer to be the parent in sysfs for the SCSI host
Vasu Dev7f349142009-03-27 09:06:31 -0700617 *
618 * Creates fc_lport struct and scsi_host for lport, configures lport
619 * and starts fabric login.
620 *
Chris Leechaf7f85d2009-08-25 13:59:24 -0700621 * Returns : The allocated fc_lport or an error pointer
Vasu Dev7f349142009-03-27 09:06:31 -0700622 */
Chris Leechaf7f85d2009-08-25 13:59:24 -0700623static struct fc_lport *fcoe_if_create(struct net_device *netdev,
624 struct device *parent)
Vasu Dev7f349142009-03-27 09:06:31 -0700625{
626 int rc;
Chris Leechaf7f85d2009-08-25 13:59:24 -0700627 struct fc_lport *lport = NULL;
Chris Leech014f5c32009-08-25 13:59:30 -0700628 struct fcoe_port *port;
629 struct fcoe_interface *fcoe;
Vasu Dev7f349142009-03-27 09:06:31 -0700630 struct Scsi_Host *shost;
631
Robert Loved5488eb2009-06-10 15:30:59 -0700632 FCOE_NETDEV_DBG(netdev, "Create Interface\n");
Vasu Dev7f349142009-03-27 09:06:31 -0700633
Chris Leech014f5c32009-08-25 13:59:30 -0700634 fcoe = kzalloc(sizeof(*fcoe), GFP_KERNEL);
635 if (!fcoe) {
636 FCOE_NETDEV_DBG(netdev, "Could not allocate fcoe structure\n");
Chris Leechaf7f85d2009-08-25 13:59:24 -0700637 rc = -ENOMEM;
638 goto out;
Vasu Dev7f349142009-03-27 09:06:31 -0700639 }
Chris Leech014f5c32009-08-25 13:59:30 -0700640
641 shost = libfc_host_alloc(&fcoe_shost_template,
642 sizeof(struct fcoe_port));
643 if (!shost) {
644 FCOE_NETDEV_DBG(netdev, "Could not allocate host structure\n");
645 rc = -ENOMEM;
646 goto out_kfree_port;
647 }
Chris Leechaf7f85d2009-08-25 13:59:24 -0700648 lport = shost_priv(shost);
Chris Leech014f5c32009-08-25 13:59:30 -0700649 port = lport_priv(lport);
650
651 port->fcoe = fcoe;
652 fcoe->priv = port;
Vasu Dev7f349142009-03-27 09:06:31 -0700653
654 /* configure fc_lport, e.g., em */
Chris Leechaf7f85d2009-08-25 13:59:24 -0700655 rc = fcoe_lport_config(lport);
Vasu Dev7f349142009-03-27 09:06:31 -0700656 if (rc) {
Robert Loved5488eb2009-06-10 15:30:59 -0700657 FCOE_NETDEV_DBG(netdev, "Could not configure lport for the "
658 "interface\n");
Vasu Dev7f349142009-03-27 09:06:31 -0700659 goto out_host_put;
660 }
661
Joe Eykholt97c83892009-03-17 11:42:40 -0700662 /*
663 * Initialize FIP.
664 */
Chris Leech014f5c32009-08-25 13:59:30 -0700665 fcoe_ctlr_init(&port->ctlr);
666 port->ctlr.send = fcoe_fip_send;
667 port->ctlr.update_mac = fcoe_update_src_mac;
Joe Eykholt97c83892009-03-17 11:42:40 -0700668
Vasu Devab6b85c2009-05-17 12:33:08 +0000669 /* configure lport network properties */
Chris Leechaf7f85d2009-08-25 13:59:24 -0700670 rc = fcoe_netdev_config(lport, netdev);
Vasu Devab6b85c2009-05-17 12:33:08 +0000671 if (rc) {
Robert Loved5488eb2009-06-10 15:30:59 -0700672 FCOE_NETDEV_DBG(netdev, "Could not configure netdev for the "
673 "interface\n");
Vasu Devab6b85c2009-05-17 12:33:08 +0000674 goto out_netdev_cleanup;
675 }
Joe Eykholt97c83892009-03-17 11:42:40 -0700676
Vasu Dev7f349142009-03-27 09:06:31 -0700677 /* configure lport scsi host properties */
Chris Leechaf7f85d2009-08-25 13:59:24 -0700678 rc = fcoe_shost_config(lport, shost, parent);
Vasu Dev7f349142009-03-27 09:06:31 -0700679 if (rc) {
Robert Loved5488eb2009-06-10 15:30:59 -0700680 FCOE_NETDEV_DBG(netdev, "Could not configure shost for the "
681 "interface\n");
Vasu Devab6b85c2009-05-17 12:33:08 +0000682 goto out_netdev_cleanup;
Vasu Dev7f349142009-03-27 09:06:31 -0700683 }
684
Vasu Dev7f349142009-03-27 09:06:31 -0700685 /* Initialize the library */
Chris Leechaf7f85d2009-08-25 13:59:24 -0700686 rc = fcoe_libfc_config(lport, &fcoe_libfc_fcn_templ);
Vasu Dev7f349142009-03-27 09:06:31 -0700687 if (rc) {
Robert Loved5488eb2009-06-10 15:30:59 -0700688 FCOE_NETDEV_DBG(netdev, "Could not configure libfc for the "
689 "interface\n");
Vasu Dev7f349142009-03-27 09:06:31 -0700690 goto out_lp_destroy;
691 }
692
Vasu Deve8af4d42009-07-29 17:05:15 -0700693 /*
694 * fcoe_em_alloc() and fcoe_hostlist_add() both
695 * need to be atomic under fcoe_hostlist_lock
696 * since fcoe_em_alloc() looks for an existing EM
697 * instance on host list updated by fcoe_hostlist_add().
698 */
699 write_lock(&fcoe_hostlist_lock);
Vasu Dev96316092009-07-29 17:05:00 -0700700 /* lport exch manager allocation */
Chris Leechaf7f85d2009-08-25 13:59:24 -0700701 rc = fcoe_em_config(lport);
Vasu Dev96316092009-07-29 17:05:00 -0700702 if (rc) {
703 FCOE_NETDEV_DBG(netdev, "Could not configure the EM for the "
704 "interface\n");
705 goto out_lp_destroy;
706 }
707
Vasu Dev7f349142009-03-27 09:06:31 -0700708 /* add to lports list */
Chris Leechaf7f85d2009-08-25 13:59:24 -0700709 fcoe_hostlist_add(lport);
Vasu Deve8af4d42009-07-29 17:05:15 -0700710 write_unlock(&fcoe_hostlist_lock);
Vasu Dev7f349142009-03-27 09:06:31 -0700711
Chris Leechaf7f85d2009-08-25 13:59:24 -0700712 lport->boot_time = jiffies;
Vasu Dev7f349142009-03-27 09:06:31 -0700713
Chris Leechaf7f85d2009-08-25 13:59:24 -0700714 fc_fabric_login(lport);
Vasu Dev7f349142009-03-27 09:06:31 -0700715
Chris Leechaf7f85d2009-08-25 13:59:24 -0700716 if (!fcoe_link_ok(lport))
Chris Leech014f5c32009-08-25 13:59:30 -0700717 fcoe_ctlr_link_up(&port->ctlr);
Joe Eykholt97c83892009-03-17 11:42:40 -0700718
Vasu Dev7f349142009-03-27 09:06:31 -0700719 dev_hold(netdev);
720
Chris Leechaf7f85d2009-08-25 13:59:24 -0700721 return lport;
Vasu Dev7f349142009-03-27 09:06:31 -0700722
723out_lp_destroy:
Chris Leechaf7f85d2009-08-25 13:59:24 -0700724 fc_exch_mgr_free(lport);
Vasu Devab6b85c2009-05-17 12:33:08 +0000725out_netdev_cleanup:
Chris Leech014f5c32009-08-25 13:59:30 -0700726 fcoe_netdev_cleanup(port);
Vasu Dev7f349142009-03-27 09:06:31 -0700727out_host_put:
Chris Leechaf7f85d2009-08-25 13:59:24 -0700728 scsi_host_put(lport->host);
Chris Leech014f5c32009-08-25 13:59:30 -0700729out_kfree_port:
730 kfree(fcoe);
Chris Leechaf7f85d2009-08-25 13:59:24 -0700731out:
732 return ERR_PTR(rc);
Vasu Dev7f349142009-03-27 09:06:31 -0700733}
734
735/**
736 * fcoe_if_init() - attach to scsi transport
737 *
738 * Returns : 0 on success
739 */
740static int __init fcoe_if_init(void)
741{
742 /* attach to scsi transport */
743 scsi_transport_fcoe_sw =
744 fc_attach_transport(&fcoe_transport_function);
745
746 if (!scsi_transport_fcoe_sw) {
Robert Loved5488eb2009-06-10 15:30:59 -0700747 printk(KERN_ERR "fcoe: Failed to attach to the FC transport\n");
Vasu Dev7f349142009-03-27 09:06:31 -0700748 return -ENODEV;
749 }
750
751 return 0;
752}
753
754/**
755 * fcoe_if_exit() - detach from scsi transport
756 *
757 * Returns : 0 on success
758 */
759int __exit fcoe_if_exit(void)
760{
761 fc_release_transport(scsi_transport_fcoe_sw);
762 return 0;
763}
764
Robert Love85b4aa42008-12-09 15:10:24 -0800765/**
Robert Love8976f422009-03-17 11:41:46 -0700766 * fcoe_percpu_thread_create() - Create a receive thread for an online cpu
767 * @cpu: cpu index for the online cpu
768 */
769static void fcoe_percpu_thread_create(unsigned int cpu)
770{
771 struct fcoe_percpu_s *p;
772 struct task_struct *thread;
773
774 p = &per_cpu(fcoe_percpu, cpu);
775
776 thread = kthread_create(fcoe_percpu_receive_thread,
777 (void *)p, "fcoethread/%d", cpu);
778
779 if (likely(!IS_ERR(p->thread))) {
780 kthread_bind(thread, cpu);
781 wake_up_process(thread);
782
783 spin_lock_bh(&p->fcoe_rx_list.lock);
784 p->thread = thread;
785 spin_unlock_bh(&p->fcoe_rx_list.lock);
786 }
787}
788
789/**
790 * fcoe_percpu_thread_destroy() - removes the rx thread for the given cpu
791 * @cpu: cpu index the rx thread is to be removed
792 *
793 * Destroys a per-CPU Rx thread. Any pending skbs are moved to the
794 * current CPU's Rx thread. If the thread being destroyed is bound to
795 * the CPU processing this context the skbs will be freed.
796 */
797static void fcoe_percpu_thread_destroy(unsigned int cpu)
798{
799 struct fcoe_percpu_s *p;
800 struct task_struct *thread;
801 struct page *crc_eof;
802 struct sk_buff *skb;
803#ifdef CONFIG_SMP
804 struct fcoe_percpu_s *p0;
805 unsigned targ_cpu = smp_processor_id();
806#endif /* CONFIG_SMP */
807
Robert Loved5488eb2009-06-10 15:30:59 -0700808 FCOE_DBG("Destroying receive thread for CPU %d\n", cpu);
Robert Love8976f422009-03-17 11:41:46 -0700809
810 /* Prevent any new skbs from being queued for this CPU. */
811 p = &per_cpu(fcoe_percpu, cpu);
812 spin_lock_bh(&p->fcoe_rx_list.lock);
813 thread = p->thread;
814 p->thread = NULL;
815 crc_eof = p->crc_eof_page;
816 p->crc_eof_page = NULL;
817 p->crc_eof_offset = 0;
818 spin_unlock_bh(&p->fcoe_rx_list.lock);
819
820#ifdef CONFIG_SMP
821 /*
822 * Don't bother moving the skb's if this context is running
823 * on the same CPU that is having its thread destroyed. This
824 * can easily happen when the module is removed.
825 */
826 if (cpu != targ_cpu) {
827 p0 = &per_cpu(fcoe_percpu, targ_cpu);
828 spin_lock_bh(&p0->fcoe_rx_list.lock);
829 if (p0->thread) {
Robert Loved5488eb2009-06-10 15:30:59 -0700830 FCOE_DBG("Moving frames from CPU %d to CPU %d\n",
831 cpu, targ_cpu);
Robert Love8976f422009-03-17 11:41:46 -0700832
833 while ((skb = __skb_dequeue(&p->fcoe_rx_list)) != NULL)
834 __skb_queue_tail(&p0->fcoe_rx_list, skb);
835 spin_unlock_bh(&p0->fcoe_rx_list.lock);
836 } else {
837 /*
838 * The targeted CPU is not initialized and cannot accept
839 * new skbs. Unlock the targeted CPU and drop the skbs
840 * on the CPU that is going offline.
841 */
842 while ((skb = __skb_dequeue(&p->fcoe_rx_list)) != NULL)
843 kfree_skb(skb);
844 spin_unlock_bh(&p0->fcoe_rx_list.lock);
845 }
846 } else {
847 /*
848 * This scenario occurs when the module is being removed
849 * and all threads are being destroyed. skbs will continue
850 * to be shifted from the CPU thread that is being removed
851 * to the CPU thread associated with the CPU that is processing
852 * the module removal. Once there is only one CPU Rx thread it
853 * will reach this case and we will drop all skbs and later
854 * stop the thread.
855 */
856 spin_lock_bh(&p->fcoe_rx_list.lock);
857 while ((skb = __skb_dequeue(&p->fcoe_rx_list)) != NULL)
858 kfree_skb(skb);
859 spin_unlock_bh(&p->fcoe_rx_list.lock);
860 }
861#else
862 /*
Chris Leechdd3fd722009-04-21 16:27:36 -0700863 * This a non-SMP scenario where the singular Rx thread is
Robert Love8976f422009-03-17 11:41:46 -0700864 * being removed. Free all skbs and stop the thread.
865 */
866 spin_lock_bh(&p->fcoe_rx_list.lock);
867 while ((skb = __skb_dequeue(&p->fcoe_rx_list)) != NULL)
868 kfree_skb(skb);
869 spin_unlock_bh(&p->fcoe_rx_list.lock);
870#endif
871
872 if (thread)
873 kthread_stop(thread);
874
875 if (crc_eof)
876 put_page(crc_eof);
877}
878
879/**
880 * fcoe_cpu_callback() - fcoe cpu hotplug event callback
881 * @nfb: callback data block
882 * @action: event triggering the callback
883 * @hcpu: index for the cpu of this event
884 *
885 * This creates or destroys per cpu data for fcoe
886 *
887 * Returns NOTIFY_OK always.
888 */
889static int fcoe_cpu_callback(struct notifier_block *nfb,
890 unsigned long action, void *hcpu)
891{
892 unsigned cpu = (unsigned long)hcpu;
893
894 switch (action) {
895 case CPU_ONLINE:
896 case CPU_ONLINE_FROZEN:
Robert Loved5488eb2009-06-10 15:30:59 -0700897 FCOE_DBG("CPU %x online: Create Rx thread\n", cpu);
Robert Love8976f422009-03-17 11:41:46 -0700898 fcoe_percpu_thread_create(cpu);
899 break;
900 case CPU_DEAD:
901 case CPU_DEAD_FROZEN:
Robert Loved5488eb2009-06-10 15:30:59 -0700902 FCOE_DBG("CPU %x offline: Remove Rx thread\n", cpu);
Robert Love8976f422009-03-17 11:41:46 -0700903 fcoe_percpu_thread_destroy(cpu);
904 break;
905 default:
906 break;
907 }
908 return NOTIFY_OK;
909}
910
911static struct notifier_block fcoe_cpu_notifier = {
912 .notifier_call = fcoe_cpu_callback,
913};
914
915/**
Robert Love34f42a02009-02-27 10:55:45 -0800916 * fcoe_rcv() - this is the fcoe receive function called by NET_RX_SOFTIRQ
Robert Love85b4aa42008-12-09 15:10:24 -0800917 * @skb: the receive skb
918 * @dev: associated net device
919 * @ptype: context
Chris Leechdd3fd722009-04-21 16:27:36 -0700920 * @olddev: last device
Robert Love85b4aa42008-12-09 15:10:24 -0800921 *
922 * this function will receive the packet and build fc frame and pass it up
923 *
924 * Returns: 0 for success
Robert Love34f42a02009-02-27 10:55:45 -0800925 */
Robert Love85b4aa42008-12-09 15:10:24 -0800926int fcoe_rcv(struct sk_buff *skb, struct net_device *dev,
927 struct packet_type *ptype, struct net_device *olddev)
928{
929 struct fc_lport *lp;
930 struct fcoe_rcv_info *fr;
Chris Leech259ad852009-08-25 13:59:41 -0700931 struct fcoe_interface *fcoe;
Chris Leech014f5c32009-08-25 13:59:30 -0700932 struct fcoe_port *port;
Robert Love85b4aa42008-12-09 15:10:24 -0800933 struct fc_frame_header *fh;
Robert Love85b4aa42008-12-09 15:10:24 -0800934 struct fcoe_percpu_s *fps;
Vasu Devb2f00912009-08-25 13:58:53 -0700935 unsigned int cpu;
Robert Love85b4aa42008-12-09 15:10:24 -0800936
Chris Leech259ad852009-08-25 13:59:41 -0700937 fcoe = container_of(ptype, struct fcoe_interface, fcoe_packet_type);
938 port = fcoe->priv;
Chris Leech014f5c32009-08-25 13:59:30 -0700939 lp = port->ctlr.lp;
Robert Love85b4aa42008-12-09 15:10:24 -0800940 if (unlikely(lp == NULL)) {
Robert Loved5488eb2009-06-10 15:30:59 -0700941 FCOE_NETDEV_DBG(dev, "Cannot find hba structure");
Robert Love85b4aa42008-12-09 15:10:24 -0800942 goto err2;
943 }
Joe Eykholt97c83892009-03-17 11:42:40 -0700944 if (!lp->link_up)
945 goto err2;
Robert Love85b4aa42008-12-09 15:10:24 -0800946
Robert Loved5488eb2009-06-10 15:30:59 -0700947 FCOE_NETDEV_DBG(dev, "skb_info: len:%d data_len:%d head:%p "
948 "data:%p tail:%p end:%p sum:%d dev:%s",
949 skb->len, skb->data_len, skb->head, skb->data,
950 skb_tail_pointer(skb), skb_end_pointer(skb),
951 skb->csum, skb->dev ? skb->dev->name : "<NULL>");
Robert Love85b4aa42008-12-09 15:10:24 -0800952
953 /* check for FCOE packet type */
954 if (unlikely(eth_hdr(skb)->h_proto != htons(ETH_P_FCOE))) {
Robert Loved5488eb2009-06-10 15:30:59 -0700955 FCOE_NETDEV_DBG(dev, "Wrong FC type frame");
Robert Love85b4aa42008-12-09 15:10:24 -0800956 goto err;
957 }
958
959 /*
960 * Check for minimum frame length, and make sure required FCoE
961 * and FC headers are pulled into the linear data area.
962 */
963 if (unlikely((skb->len < FCOE_MIN_FRAME) ||
964 !pskb_may_pull(skb, FCOE_HEADER_LEN)))
965 goto err;
966
967 skb_set_transport_header(skb, sizeof(struct fcoe_hdr));
968 fh = (struct fc_frame_header *) skb_transport_header(skb);
969
Robert Love85b4aa42008-12-09 15:10:24 -0800970 fr = fcoe_dev_from_skb(skb);
971 fr->fr_dev = lp;
972 fr->ptype = ptype;
Robert Love5e5e92d2009-03-17 11:41:35 -0700973
Robert Love85b4aa42008-12-09 15:10:24 -0800974 /*
Vasu Devb2f00912009-08-25 13:58:53 -0700975 * In case the incoming frame's exchange is originated from
976 * the initiator, then received frame's exchange id is ANDed
977 * with fc_cpu_mask bits to get the same cpu on which exchange
978 * was originated, otherwise just use the current cpu.
Robert Love85b4aa42008-12-09 15:10:24 -0800979 */
Vasu Devb2f00912009-08-25 13:58:53 -0700980 if (ntoh24(fh->fh_f_ctl) & FC_FC_EX_CTX)
981 cpu = ntohs(fh->fh_ox_id) & fc_cpu_mask;
982 else
983 cpu = smp_processor_id();
Robert Love5e5e92d2009-03-17 11:41:35 -0700984
Robert Love8976f422009-03-17 11:41:46 -0700985 fps = &per_cpu(fcoe_percpu, cpu);
Robert Love85b4aa42008-12-09 15:10:24 -0800986 spin_lock_bh(&fps->fcoe_rx_list.lock);
Robert Love8976f422009-03-17 11:41:46 -0700987 if (unlikely(!fps->thread)) {
988 /*
989 * The targeted CPU is not ready, let's target
990 * the first CPU now. For non-SMP systems this
991 * will check the same CPU twice.
992 */
Robert Loved5488eb2009-06-10 15:30:59 -0700993 FCOE_NETDEV_DBG(dev, "CPU is online, but no receive thread "
994 "ready for incoming skb- using first online "
995 "CPU.\n");
Robert Love8976f422009-03-17 11:41:46 -0700996
997 spin_unlock_bh(&fps->fcoe_rx_list.lock);
998 cpu = first_cpu(cpu_online_map);
999 fps = &per_cpu(fcoe_percpu, cpu);
1000 spin_lock_bh(&fps->fcoe_rx_list.lock);
1001 if (!fps->thread) {
1002 spin_unlock_bh(&fps->fcoe_rx_list.lock);
1003 goto err;
1004 }
1005 }
1006
1007 /*
1008 * We now have a valid CPU that we're targeting for
1009 * this skb. We also have this receive thread locked,
1010 * so we're free to queue skbs into it's queue.
1011 */
Robert Love85b4aa42008-12-09 15:10:24 -08001012 __skb_queue_tail(&fps->fcoe_rx_list, skb);
1013 if (fps->fcoe_rx_list.qlen == 1)
1014 wake_up_process(fps->thread);
1015
1016 spin_unlock_bh(&fps->fcoe_rx_list.lock);
1017
1018 return 0;
1019err:
Robert Love582b45b2009-03-31 15:51:50 -07001020 fc_lport_get_stats(lp)->ErrorFrames++;
Robert Love85b4aa42008-12-09 15:10:24 -08001021
1022err2:
1023 kfree_skb(skb);
1024 return -1;
1025}
Robert Love85b4aa42008-12-09 15:10:24 -08001026
1027/**
Robert Love34f42a02009-02-27 10:55:45 -08001028 * fcoe_start_io() - pass to netdev to start xmit for fcoe
Robert Love85b4aa42008-12-09 15:10:24 -08001029 * @skb: the skb to be xmitted
1030 *
1031 * Returns: 0 for success
Robert Love34f42a02009-02-27 10:55:45 -08001032 */
Robert Love85b4aa42008-12-09 15:10:24 -08001033static inline int fcoe_start_io(struct sk_buff *skb)
1034{
1035 int rc;
1036
1037 skb_get(skb);
1038 rc = dev_queue_xmit(skb);
1039 if (rc != 0)
1040 return rc;
1041 kfree_skb(skb);
1042 return 0;
1043}
1044
1045/**
Chris Leechdd3fd722009-04-21 16:27:36 -07001046 * fcoe_get_paged_crc_eof() - in case we need to alloc a page for crc_eof
Robert Love85b4aa42008-12-09 15:10:24 -08001047 * @skb: the skb to be xmitted
1048 * @tlen: total len
1049 *
1050 * Returns: 0 for success
Robert Love34f42a02009-02-27 10:55:45 -08001051 */
Robert Love85b4aa42008-12-09 15:10:24 -08001052static int fcoe_get_paged_crc_eof(struct sk_buff *skb, int tlen)
1053{
1054 struct fcoe_percpu_s *fps;
1055 struct page *page;
Robert Love85b4aa42008-12-09 15:10:24 -08001056
Robert Love5e5e92d2009-03-17 11:41:35 -07001057 fps = &get_cpu_var(fcoe_percpu);
Robert Love85b4aa42008-12-09 15:10:24 -08001058 page = fps->crc_eof_page;
1059 if (!page) {
1060 page = alloc_page(GFP_ATOMIC);
1061 if (!page) {
Robert Love5e5e92d2009-03-17 11:41:35 -07001062 put_cpu_var(fcoe_percpu);
Robert Love85b4aa42008-12-09 15:10:24 -08001063 return -ENOMEM;
1064 }
1065 fps->crc_eof_page = page;
Robert Love8976f422009-03-17 11:41:46 -07001066 fps->crc_eof_offset = 0;
Robert Love85b4aa42008-12-09 15:10:24 -08001067 }
1068
1069 get_page(page);
1070 skb_fill_page_desc(skb, skb_shinfo(skb)->nr_frags, page,
1071 fps->crc_eof_offset, tlen);
1072 skb->len += tlen;
1073 skb->data_len += tlen;
1074 skb->truesize += tlen;
1075 fps->crc_eof_offset += sizeof(struct fcoe_crc_eof);
1076
1077 if (fps->crc_eof_offset >= PAGE_SIZE) {
1078 fps->crc_eof_page = NULL;
1079 fps->crc_eof_offset = 0;
1080 put_page(page);
1081 }
Robert Love5e5e92d2009-03-17 11:41:35 -07001082 put_cpu_var(fcoe_percpu);
Robert Love85b4aa42008-12-09 15:10:24 -08001083 return 0;
1084}
1085
1086/**
Robert Love34f42a02009-02-27 10:55:45 -08001087 * fcoe_fc_crc() - calculates FC CRC in this fcoe skb
Chris Leechdd3fd722009-04-21 16:27:36 -07001088 * @fp: the fc_frame containing data to be checksummed
Robert Love85b4aa42008-12-09 15:10:24 -08001089 *
Chris Leech014f5c32009-08-25 13:59:30 -07001090 * This uses crc32() to calculate the crc for port frame
Robert Love85b4aa42008-12-09 15:10:24 -08001091 * Return : 32 bit crc
Robert Love34f42a02009-02-27 10:55:45 -08001092 */
Robert Love85b4aa42008-12-09 15:10:24 -08001093u32 fcoe_fc_crc(struct fc_frame *fp)
1094{
1095 struct sk_buff *skb = fp_skb(fp);
1096 struct skb_frag_struct *frag;
1097 unsigned char *data;
1098 unsigned long off, len, clen;
1099 u32 crc;
1100 unsigned i;
1101
1102 crc = crc32(~0, skb->data, skb_headlen(skb));
1103
1104 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
1105 frag = &skb_shinfo(skb)->frags[i];
1106 off = frag->page_offset;
1107 len = frag->size;
1108 while (len > 0) {
1109 clen = min(len, PAGE_SIZE - (off & ~PAGE_MASK));
1110 data = kmap_atomic(frag->page + (off >> PAGE_SHIFT),
1111 KM_SKB_DATA_SOFTIRQ);
1112 crc = crc32(crc, data + (off & ~PAGE_MASK), clen);
1113 kunmap_atomic(data, KM_SKB_DATA_SOFTIRQ);
1114 off += clen;
1115 len -= clen;
1116 }
1117 }
1118 return crc;
1119}
Robert Love85b4aa42008-12-09 15:10:24 -08001120
1121/**
Robert Love34f42a02009-02-27 10:55:45 -08001122 * fcoe_xmit() - FCoE frame transmit function
Chris Leech014f5c32009-08-25 13:59:30 -07001123 * @lp: the associated local fcoe
Robert Love85b4aa42008-12-09 15:10:24 -08001124 * @fp: the fc_frame to be transmitted
1125 *
1126 * Return : 0 for success
Robert Love34f42a02009-02-27 10:55:45 -08001127 */
Robert Love85b4aa42008-12-09 15:10:24 -08001128int fcoe_xmit(struct fc_lport *lp, struct fc_frame *fp)
1129{
Vasu Dev4bb6b512009-05-06 10:52:34 -07001130 int wlen;
Robert Love85b4aa42008-12-09 15:10:24 -08001131 u32 crc;
1132 struct ethhdr *eh;
1133 struct fcoe_crc_eof *cp;
1134 struct sk_buff *skb;
1135 struct fcoe_dev_stats *stats;
1136 struct fc_frame_header *fh;
1137 unsigned int hlen; /* header length implies the version */
1138 unsigned int tlen; /* trailer length */
1139 unsigned int elen; /* eth header, may include vlan */
Chris Leech014f5c32009-08-25 13:59:30 -07001140 struct fcoe_port *port;
Robert Love85b4aa42008-12-09 15:10:24 -08001141 u8 sof, eof;
1142 struct fcoe_hdr *hp;
1143
1144 WARN_ON((fr_len(fp) % sizeof(u32)) != 0);
1145
Chris Leech014f5c32009-08-25 13:59:30 -07001146 port = lport_priv(lp);
Robert Love85b4aa42008-12-09 15:10:24 -08001147 fh = fc_frame_header_get(fp);
Joe Eykholt97c83892009-03-17 11:42:40 -07001148 skb = fp_skb(fp);
1149 wlen = skb->len / FCOE_WORD_TO_BYTE;
1150
1151 if (!lp->link_up) {
Dan Carpenter3caf02e2009-04-21 16:27:25 -07001152 kfree_skb(skb);
Joe Eykholt97c83892009-03-17 11:42:40 -07001153 return 0;
Robert Love85b4aa42008-12-09 15:10:24 -08001154 }
1155
Joe Eykholt97c83892009-03-17 11:42:40 -07001156 if (unlikely(fh->fh_r_ctl == FC_RCTL_ELS_REQ) &&
Chris Leech014f5c32009-08-25 13:59:30 -07001157 fcoe_ctlr_els_send(&port->ctlr, skb))
Joe Eykholt97c83892009-03-17 11:42:40 -07001158 return 0;
1159
Robert Love85b4aa42008-12-09 15:10:24 -08001160 sof = fr_sof(fp);
1161 eof = fr_eof(fp);
1162
Vasu Dev4e57e1c2009-05-06 10:52:46 -07001163 elen = sizeof(struct ethhdr);
Robert Love85b4aa42008-12-09 15:10:24 -08001164 hlen = sizeof(struct fcoe_hdr);
1165 tlen = sizeof(struct fcoe_crc_eof);
1166 wlen = (skb->len - tlen + sizeof(crc)) / FCOE_WORD_TO_BYTE;
1167
1168 /* crc offload */
1169 if (likely(lp->crc_offload)) {
Yi Zou39ca9a02009-02-27 14:07:15 -08001170 skb->ip_summed = CHECKSUM_PARTIAL;
Robert Love85b4aa42008-12-09 15:10:24 -08001171 skb->csum_start = skb_headroom(skb);
1172 skb->csum_offset = skb->len;
1173 crc = 0;
1174 } else {
1175 skb->ip_summed = CHECKSUM_NONE;
1176 crc = fcoe_fc_crc(fp);
1177 }
1178
Chris Leech014f5c32009-08-25 13:59:30 -07001179 /* copy port crc and eof to the skb buff */
Robert Love85b4aa42008-12-09 15:10:24 -08001180 if (skb_is_nonlinear(skb)) {
1181 skb_frag_t *frag;
1182 if (fcoe_get_paged_crc_eof(skb, tlen)) {
Roel Kluine9041582009-02-27 10:56:22 -08001183 kfree_skb(skb);
Robert Love85b4aa42008-12-09 15:10:24 -08001184 return -ENOMEM;
1185 }
1186 frag = &skb_shinfo(skb)->frags[skb_shinfo(skb)->nr_frags - 1];
1187 cp = kmap_atomic(frag->page, KM_SKB_DATA_SOFTIRQ)
1188 + frag->page_offset;
1189 } else {
1190 cp = (struct fcoe_crc_eof *)skb_put(skb, tlen);
1191 }
1192
1193 memset(cp, 0, sizeof(*cp));
1194 cp->fcoe_eof = eof;
1195 cp->fcoe_crc32 = cpu_to_le32(~crc);
1196
1197 if (skb_is_nonlinear(skb)) {
1198 kunmap_atomic(cp, KM_SKB_DATA_SOFTIRQ);
1199 cp = NULL;
1200 }
1201
Chris Leech014f5c32009-08-25 13:59:30 -07001202 /* adjust skb network/transport offsets to match mac/fcoe/port */
Robert Love85b4aa42008-12-09 15:10:24 -08001203 skb_push(skb, elen + hlen);
1204 skb_reset_mac_header(skb);
1205 skb_reset_network_header(skb);
1206 skb->mac_len = elen;
Yi Zou211c7382009-02-27 14:06:37 -08001207 skb->protocol = htons(ETH_P_FCOE);
Chris Leech25024982009-08-25 13:59:35 -07001208 skb->dev = port->fcoe->netdev;
Robert Love85b4aa42008-12-09 15:10:24 -08001209
1210 /* fill up mac and fcoe headers */
1211 eh = eth_hdr(skb);
1212 eh->h_proto = htons(ETH_P_FCOE);
Chris Leech014f5c32009-08-25 13:59:30 -07001213 if (port->ctlr.map_dest)
Robert Love85b4aa42008-12-09 15:10:24 -08001214 fc_fcoe_set_mac(eh->h_dest, fh->fh_d_id);
1215 else
1216 /* insert GW address */
Chris Leech014f5c32009-08-25 13:59:30 -07001217 memcpy(eh->h_dest, port->ctlr.dest_addr, ETH_ALEN);
Robert Love85b4aa42008-12-09 15:10:24 -08001218
Chris Leech014f5c32009-08-25 13:59:30 -07001219 if (unlikely(port->ctlr.flogi_oxid != FC_XID_UNKNOWN))
1220 memcpy(eh->h_source, port->ctlr.ctl_src_addr, ETH_ALEN);
Robert Love85b4aa42008-12-09 15:10:24 -08001221 else
Chris Leech014f5c32009-08-25 13:59:30 -07001222 memcpy(eh->h_source, port->ctlr.data_src_addr, ETH_ALEN);
Robert Love85b4aa42008-12-09 15:10:24 -08001223
1224 hp = (struct fcoe_hdr *)(eh + 1);
1225 memset(hp, 0, sizeof(*hp));
1226 if (FC_FCOE_VER)
1227 FC_FCOE_ENCAPS_VER(hp, FC_FCOE_VER);
1228 hp->fcoe_sof = sof;
1229
Yi Zou39ca9a02009-02-27 14:07:15 -08001230 /* fcoe lso, mss is in max_payload which is non-zero for FCP data */
1231 if (lp->seq_offload && fr_max_payload(fp)) {
1232 skb_shinfo(skb)->gso_type = SKB_GSO_FCOE;
1233 skb_shinfo(skb)->gso_size = fr_max_payload(fp);
1234 } else {
1235 skb_shinfo(skb)->gso_type = 0;
1236 skb_shinfo(skb)->gso_size = 0;
1237 }
Robert Love85b4aa42008-12-09 15:10:24 -08001238 /* update tx stats: regardless if LLD fails */
Robert Love582b45b2009-03-31 15:51:50 -07001239 stats = fc_lport_get_stats(lp);
1240 stats->TxFrames++;
1241 stats->TxWords += wlen;
Robert Love85b4aa42008-12-09 15:10:24 -08001242
1243 /* send down to lld */
1244 fr_dev(fp) = lp;
Chris Leech014f5c32009-08-25 13:59:30 -07001245 if (port->fcoe_pending_queue.qlen)
Vasu Dev4bb6b512009-05-06 10:52:34 -07001246 fcoe_check_wait_queue(lp, skb);
1247 else if (fcoe_start_io(skb))
1248 fcoe_check_wait_queue(lp, skb);
Robert Love85b4aa42008-12-09 15:10:24 -08001249
1250 return 0;
1251}
Robert Love85b4aa42008-12-09 15:10:24 -08001252
Robert Love34f42a02009-02-27 10:55:45 -08001253/**
1254 * fcoe_percpu_receive_thread() - recv thread per cpu
Robert Love85b4aa42008-12-09 15:10:24 -08001255 * @arg: ptr to the fcoe per cpu struct
1256 *
1257 * Return: 0 for success
Robert Love85b4aa42008-12-09 15:10:24 -08001258 */
1259int fcoe_percpu_receive_thread(void *arg)
1260{
1261 struct fcoe_percpu_s *p = arg;
1262 u32 fr_len;
1263 struct fc_lport *lp;
1264 struct fcoe_rcv_info *fr;
1265 struct fcoe_dev_stats *stats;
1266 struct fc_frame_header *fh;
1267 struct sk_buff *skb;
1268 struct fcoe_crc_eof crc_eof;
1269 struct fc_frame *fp;
1270 u8 *mac = NULL;
Chris Leech014f5c32009-08-25 13:59:30 -07001271 struct fcoe_port *port;
Robert Love85b4aa42008-12-09 15:10:24 -08001272 struct fcoe_hdr *hp;
1273
Robert Love4469c192009-02-27 10:56:38 -08001274 set_user_nice(current, -20);
Robert Love85b4aa42008-12-09 15:10:24 -08001275
1276 while (!kthread_should_stop()) {
1277
1278 spin_lock_bh(&p->fcoe_rx_list.lock);
1279 while ((skb = __skb_dequeue(&p->fcoe_rx_list)) == NULL) {
1280 set_current_state(TASK_INTERRUPTIBLE);
1281 spin_unlock_bh(&p->fcoe_rx_list.lock);
1282 schedule();
1283 set_current_state(TASK_RUNNING);
1284 if (kthread_should_stop())
1285 return 0;
1286 spin_lock_bh(&p->fcoe_rx_list.lock);
1287 }
1288 spin_unlock_bh(&p->fcoe_rx_list.lock);
1289 fr = fcoe_dev_from_skb(skb);
1290 lp = fr->fr_dev;
1291 if (unlikely(lp == NULL)) {
Robert Loved5488eb2009-06-10 15:30:59 -07001292 FCOE_NETDEV_DBG(skb->dev, "Invalid HBA Structure");
Robert Love85b4aa42008-12-09 15:10:24 -08001293 kfree_skb(skb);
1294 continue;
1295 }
1296
Robert Loved5488eb2009-06-10 15:30:59 -07001297 FCOE_NETDEV_DBG(skb->dev, "skb_info: len:%d data_len:%d "
1298 "head:%p data:%p tail:%p end:%p sum:%d dev:%s",
1299 skb->len, skb->data_len,
1300 skb->head, skb->data, skb_tail_pointer(skb),
1301 skb_end_pointer(skb), skb->csum,
1302 skb->dev ? skb->dev->name : "<NULL>");
Robert Love85b4aa42008-12-09 15:10:24 -08001303
1304 /*
1305 * Save source MAC address before discarding header.
1306 */
Chris Leech014f5c32009-08-25 13:59:30 -07001307 port = lport_priv(lp);
Robert Love85b4aa42008-12-09 15:10:24 -08001308 if (skb_is_nonlinear(skb))
1309 skb_linearize(skb); /* not ideal */
Joe Eykholt97c83892009-03-17 11:42:40 -07001310 mac = eth_hdr(skb)->h_source;
Robert Love85b4aa42008-12-09 15:10:24 -08001311
1312 /*
1313 * Frame length checks and setting up the header pointers
1314 * was done in fcoe_rcv already.
1315 */
1316 hp = (struct fcoe_hdr *) skb_network_header(skb);
1317 fh = (struct fc_frame_header *) skb_transport_header(skb);
1318
Robert Love582b45b2009-03-31 15:51:50 -07001319 stats = fc_lport_get_stats(lp);
Robert Love85b4aa42008-12-09 15:10:24 -08001320 if (unlikely(FC_FCOE_DECAPS_VER(hp) != FC_FCOE_VER)) {
Robert Love582b45b2009-03-31 15:51:50 -07001321 if (stats->ErrorFrames < 5)
Robert Loved5488eb2009-06-10 15:30:59 -07001322 printk(KERN_WARNING "fcoe: FCoE version "
Robert Love582b45b2009-03-31 15:51:50 -07001323 "mismatch: The frame has "
1324 "version %x, but the "
1325 "initiator supports version "
1326 "%x\n", FC_FCOE_DECAPS_VER(hp),
1327 FC_FCOE_VER);
1328 stats->ErrorFrames++;
Robert Love85b4aa42008-12-09 15:10:24 -08001329 kfree_skb(skb);
1330 continue;
1331 }
1332
1333 skb_pull(skb, sizeof(struct fcoe_hdr));
1334 fr_len = skb->len - sizeof(struct fcoe_crc_eof);
1335
Robert Love582b45b2009-03-31 15:51:50 -07001336 stats->RxFrames++;
1337 stats->RxWords += fr_len / FCOE_WORD_TO_BYTE;
Robert Love85b4aa42008-12-09 15:10:24 -08001338
1339 fp = (struct fc_frame *)skb;
1340 fc_frame_init(fp);
1341 fr_dev(fp) = lp;
1342 fr_sof(fp) = hp->fcoe_sof;
1343
1344 /* Copy out the CRC and EOF trailer for access */
1345 if (skb_copy_bits(skb, fr_len, &crc_eof, sizeof(crc_eof))) {
1346 kfree_skb(skb);
1347 continue;
1348 }
1349 fr_eof(fp) = crc_eof.fcoe_eof;
1350 fr_crc(fp) = crc_eof.fcoe_crc32;
1351 if (pskb_trim(skb, fr_len)) {
1352 kfree_skb(skb);
1353 continue;
1354 }
1355
1356 /*
1357 * We only check CRC if no offload is available and if it is
1358 * it's solicited data, in which case, the FCP layer would
1359 * check it during the copy.
1360 */
Yi Zou07c00ec2009-02-27 14:07:31 -08001361 if (lp->crc_offload && skb->ip_summed == CHECKSUM_UNNECESSARY)
Robert Love85b4aa42008-12-09 15:10:24 -08001362 fr_flags(fp) &= ~FCPHF_CRC_UNCHECKED;
1363 else
1364 fr_flags(fp) |= FCPHF_CRC_UNCHECKED;
1365
1366 fh = fc_frame_header_get(fp);
1367 if (fh->fh_r_ctl == FC_RCTL_DD_SOL_DATA &&
1368 fh->fh_type == FC_TYPE_FCP) {
Vasu Dev52ff8782009-07-29 17:05:10 -07001369 fc_exch_recv(lp, fp);
Robert Love85b4aa42008-12-09 15:10:24 -08001370 continue;
1371 }
1372 if (fr_flags(fp) & FCPHF_CRC_UNCHECKED) {
1373 if (le32_to_cpu(fr_crc(fp)) !=
1374 ~crc32(~0, skb->data, fr_len)) {
Robert Loved5488eb2009-06-10 15:30:59 -07001375 if (stats->InvalidCRCCount < 5)
Robert Love85b4aa42008-12-09 15:10:24 -08001376 printk(KERN_WARNING "fcoe: dropping "
1377 "frame with CRC error\n");
1378 stats->InvalidCRCCount++;
1379 stats->ErrorFrames++;
1380 fc_frame_free(fp);
1381 continue;
1382 }
1383 fr_flags(fp) &= ~FCPHF_CRC_UNCHECKED;
1384 }
Chris Leech014f5c32009-08-25 13:59:30 -07001385 if (unlikely(port->ctlr.flogi_oxid != FC_XID_UNKNOWN) &&
1386 fcoe_ctlr_recv_flogi(&port->ctlr, fp, mac)) {
Joe Eykholt97c83892009-03-17 11:42:40 -07001387 fc_frame_free(fp);
1388 continue;
1389 }
Vasu Dev52ff8782009-07-29 17:05:10 -07001390 fc_exch_recv(lp, fp);
Robert Love85b4aa42008-12-09 15:10:24 -08001391 }
1392 return 0;
1393}
1394
1395/**
Chris Leechdd3fd722009-04-21 16:27:36 -07001396 * fcoe_check_wait_queue() - attempt to clear the transmit backlog
1397 * @lp: the fc_lport
Robert Love85b4aa42008-12-09 15:10:24 -08001398 *
1399 * This empties the wait_queue, dequeue the head of the wait_queue queue
1400 * and calls fcoe_start_io() for each packet, if all skb have been
Vasu Devc826a312009-02-27 10:56:27 -08001401 * transmitted, return qlen or -1 if a error occurs, then restore
Chris Leechdd3fd722009-04-21 16:27:36 -07001402 * wait_queue and try again later.
Robert Love85b4aa42008-12-09 15:10:24 -08001403 *
1404 * The wait_queue is used when the skb transmit fails. skb will go
Chris Leechdd3fd722009-04-21 16:27:36 -07001405 * in the wait_queue which will be emptied by the timer function or
Robert Love85b4aa42008-12-09 15:10:24 -08001406 * by the next skb transmit.
Robert Love34f42a02009-02-27 10:55:45 -08001407 */
Vasu Dev4bb6b512009-05-06 10:52:34 -07001408static void fcoe_check_wait_queue(struct fc_lport *lp, struct sk_buff *skb)
Robert Love85b4aa42008-12-09 15:10:24 -08001409{
Chris Leech014f5c32009-08-25 13:59:30 -07001410 struct fcoe_port *port = lport_priv(lp);
Vasu Dev4bb6b512009-05-06 10:52:34 -07001411 int rc;
Robert Love85b4aa42008-12-09 15:10:24 -08001412
Chris Leech014f5c32009-08-25 13:59:30 -07001413 spin_lock_bh(&port->fcoe_pending_queue.lock);
Vasu Dev4bb6b512009-05-06 10:52:34 -07001414
1415 if (skb)
Chris Leech014f5c32009-08-25 13:59:30 -07001416 __skb_queue_tail(&port->fcoe_pending_queue, skb);
Vasu Dev4bb6b512009-05-06 10:52:34 -07001417
Chris Leech014f5c32009-08-25 13:59:30 -07001418 if (port->fcoe_pending_queue_active)
Vasu Devc826a312009-02-27 10:56:27 -08001419 goto out;
Chris Leech014f5c32009-08-25 13:59:30 -07001420 port->fcoe_pending_queue_active = 1;
Chris Leech55c8baf2009-02-27 10:56:32 -08001421
Chris Leech014f5c32009-08-25 13:59:30 -07001422 while (port->fcoe_pending_queue.qlen) {
Chris Leech55c8baf2009-02-27 10:56:32 -08001423 /* keep qlen > 0 until fcoe_start_io succeeds */
Chris Leech014f5c32009-08-25 13:59:30 -07001424 port->fcoe_pending_queue.qlen++;
1425 skb = __skb_dequeue(&port->fcoe_pending_queue);
Chris Leech55c8baf2009-02-27 10:56:32 -08001426
Chris Leech014f5c32009-08-25 13:59:30 -07001427 spin_unlock_bh(&port->fcoe_pending_queue.lock);
Chris Leech55c8baf2009-02-27 10:56:32 -08001428 rc = fcoe_start_io(skb);
Chris Leech014f5c32009-08-25 13:59:30 -07001429 spin_lock_bh(&port->fcoe_pending_queue.lock);
Chris Leech55c8baf2009-02-27 10:56:32 -08001430
1431 if (rc) {
Chris Leech014f5c32009-08-25 13:59:30 -07001432 __skb_queue_head(&port->fcoe_pending_queue, skb);
Chris Leech55c8baf2009-02-27 10:56:32 -08001433 /* undo temporary increment above */
Chris Leech014f5c32009-08-25 13:59:30 -07001434 port->fcoe_pending_queue.qlen--;
Chris Leech55c8baf2009-02-27 10:56:32 -08001435 break;
Robert Love85b4aa42008-12-09 15:10:24 -08001436 }
Chris Leech55c8baf2009-02-27 10:56:32 -08001437 /* undo temporary increment above */
Chris Leech014f5c32009-08-25 13:59:30 -07001438 port->fcoe_pending_queue.qlen--;
Robert Love85b4aa42008-12-09 15:10:24 -08001439 }
Chris Leech55c8baf2009-02-27 10:56:32 -08001440
Chris Leech014f5c32009-08-25 13:59:30 -07001441 if (port->fcoe_pending_queue.qlen < FCOE_LOW_QUEUE_DEPTH)
Chris Leech55c8baf2009-02-27 10:56:32 -08001442 lp->qfull = 0;
Chris Leech014f5c32009-08-25 13:59:30 -07001443 if (port->fcoe_pending_queue.qlen && !timer_pending(&port->timer))
1444 mod_timer(&port->timer, jiffies + 2);
1445 port->fcoe_pending_queue_active = 0;
Vasu Devc826a312009-02-27 10:56:27 -08001446out:
Chris Leech014f5c32009-08-25 13:59:30 -07001447 if (port->fcoe_pending_queue.qlen > FCOE_MAX_QUEUE_DEPTH)
Vasu Dev4bb6b512009-05-06 10:52:34 -07001448 lp->qfull = 1;
Chris Leech014f5c32009-08-25 13:59:30 -07001449 spin_unlock_bh(&port->fcoe_pending_queue.lock);
Vasu Dev4bb6b512009-05-06 10:52:34 -07001450 return;
Robert Love85b4aa42008-12-09 15:10:24 -08001451}
1452
1453/**
Robert Love34f42a02009-02-27 10:55:45 -08001454 * fcoe_dev_setup() - setup link change notification interface
1455 */
Randy Dunlapb0d428a2009-04-27 21:49:31 -07001456static void fcoe_dev_setup(void)
Robert Love85b4aa42008-12-09 15:10:24 -08001457{
Robert Love85b4aa42008-12-09 15:10:24 -08001458 register_netdevice_notifier(&fcoe_notifier);
1459}
1460
1461/**
Randy Dunlapb0d428a2009-04-27 21:49:31 -07001462 * fcoe_dev_cleanup() - cleanup link change notification interface
Robert Love34f42a02009-02-27 10:55:45 -08001463 */
Robert Love85b4aa42008-12-09 15:10:24 -08001464static void fcoe_dev_cleanup(void)
1465{
1466 unregister_netdevice_notifier(&fcoe_notifier);
1467}
1468
1469/**
Robert Love34f42a02009-02-27 10:55:45 -08001470 * fcoe_device_notification() - netdev event notification callback
Robert Love85b4aa42008-12-09 15:10:24 -08001471 * @notifier: context of the notification
1472 * @event: type of event
1473 * @ptr: fixed array for output parsed ifname
1474 *
1475 * This function is called by the ethernet driver in case of link change event
1476 *
1477 * Returns: 0 for success
Robert Love34f42a02009-02-27 10:55:45 -08001478 */
Robert Love85b4aa42008-12-09 15:10:24 -08001479static int fcoe_device_notification(struct notifier_block *notifier,
1480 ulong event, void *ptr)
1481{
1482 struct fc_lport *lp = NULL;
Vasu Dev1d1b88d2009-07-29 17:05:45 -07001483 struct net_device *netdev = ptr;
Chris Leech014f5c32009-08-25 13:59:30 -07001484 struct fcoe_interface *fcoe;
1485 struct fcoe_port *port = NULL;
Robert Love85b4aa42008-12-09 15:10:24 -08001486 struct fcoe_dev_stats *stats;
Joe Eykholt97c83892009-03-17 11:42:40 -07001487 u32 link_possible = 1;
Robert Love85b4aa42008-12-09 15:10:24 -08001488 u32 mfs;
1489 int rc = NOTIFY_OK;
1490
1491 read_lock(&fcoe_hostlist_lock);
Chris Leech014f5c32009-08-25 13:59:30 -07001492 list_for_each_entry(fcoe, &fcoe_hostlist, list) {
1493 port = fcoe->priv;
Chris Leech25024982009-08-25 13:59:35 -07001494 if (fcoe->netdev == netdev) {
Chris Leech014f5c32009-08-25 13:59:30 -07001495 lp = port->ctlr.lp;
Robert Love85b4aa42008-12-09 15:10:24 -08001496 break;
1497 }
1498 }
1499 read_unlock(&fcoe_hostlist_lock);
1500 if (lp == NULL) {
1501 rc = NOTIFY_DONE;
1502 goto out;
1503 }
1504
Robert Love85b4aa42008-12-09 15:10:24 -08001505 switch (event) {
1506 case NETDEV_DOWN:
1507 case NETDEV_GOING_DOWN:
Joe Eykholt97c83892009-03-17 11:42:40 -07001508 link_possible = 0;
Robert Love85b4aa42008-12-09 15:10:24 -08001509 break;
1510 case NETDEV_UP:
1511 case NETDEV_CHANGE:
Robert Love85b4aa42008-12-09 15:10:24 -08001512 break;
1513 case NETDEV_CHANGEMTU:
Vasu Dev1d1b88d2009-07-29 17:05:45 -07001514 mfs = netdev->mtu - (sizeof(struct fcoe_hdr) +
1515 sizeof(struct fcoe_crc_eof));
Robert Love85b4aa42008-12-09 15:10:24 -08001516 if (mfs >= FC_MIN_MAX_FRAME)
1517 fc_set_mfs(lp, mfs);
Robert Love85b4aa42008-12-09 15:10:24 -08001518 break;
1519 case NETDEV_REGISTER:
1520 break;
1521 default:
Vasu Dev1d1b88d2009-07-29 17:05:45 -07001522 FCOE_NETDEV_DBG(netdev, "Unknown event %ld "
Robert Loved5488eb2009-06-10 15:30:59 -07001523 "from netdev netlink\n", event);
Robert Love85b4aa42008-12-09 15:10:24 -08001524 }
Joe Eykholt97c83892009-03-17 11:42:40 -07001525 if (link_possible && !fcoe_link_ok(lp))
Chris Leech014f5c32009-08-25 13:59:30 -07001526 fcoe_ctlr_link_up(&port->ctlr);
1527 else if (fcoe_ctlr_link_down(&port->ctlr)) {
Joe Eykholt97c83892009-03-17 11:42:40 -07001528 stats = fc_lport_get_stats(lp);
1529 stats->LinkFailureCount++;
1530 fcoe_clean_pending_queue(lp);
Robert Love85b4aa42008-12-09 15:10:24 -08001531 }
1532out:
1533 return rc;
1534}
1535
1536/**
Robert Love34f42a02009-02-27 10:55:45 -08001537 * fcoe_if_to_netdev() - parse a name buffer to get netdev
Robert Love85b4aa42008-12-09 15:10:24 -08001538 * @buffer: incoming buffer to be copied
1539 *
Chris Leechdd3fd722009-04-21 16:27:36 -07001540 * Returns: NULL or ptr to net_device
Robert Love34f42a02009-02-27 10:55:45 -08001541 */
Robert Love85b4aa42008-12-09 15:10:24 -08001542static struct net_device *fcoe_if_to_netdev(const char *buffer)
1543{
1544 char *cp;
1545 char ifname[IFNAMSIZ + 2];
1546
1547 if (buffer) {
1548 strlcpy(ifname, buffer, IFNAMSIZ);
1549 cp = ifname + strlen(ifname);
1550 while (--cp >= ifname && *cp == '\n')
1551 *cp = '\0';
1552 return dev_get_by_name(&init_net, ifname);
1553 }
1554 return NULL;
1555}
1556
1557/**
Chris Leechdd3fd722009-04-21 16:27:36 -07001558 * fcoe_netdev_to_module_owner() - finds out the driver module of the netdev
Robert Love85b4aa42008-12-09 15:10:24 -08001559 * @netdev: the target netdev
1560 *
1561 * Returns: ptr to the struct module, NULL for failure
Robert Love34f42a02009-02-27 10:55:45 -08001562 */
Robert Loveb2ab99c2009-02-27 10:55:50 -08001563static struct module *
1564fcoe_netdev_to_module_owner(const struct net_device *netdev)
Robert Love85b4aa42008-12-09 15:10:24 -08001565{
1566 struct device *dev;
1567
1568 if (!netdev)
1569 return NULL;
1570
1571 dev = netdev->dev.parent;
1572 if (!dev)
1573 return NULL;
1574
1575 if (!dev->driver)
1576 return NULL;
1577
1578 return dev->driver->owner;
1579}
1580
1581/**
Robert Love34f42a02009-02-27 10:55:45 -08001582 * fcoe_ethdrv_get() - Hold the Ethernet driver
Robert Love85b4aa42008-12-09 15:10:24 -08001583 * @netdev: the target netdev
1584 *
Robert Love34f42a02009-02-27 10:55:45 -08001585 * Holds the Ethernet driver module by try_module_get() for
1586 * the corresponding netdev.
1587 *
Chris Leechdd3fd722009-04-21 16:27:36 -07001588 * Returns: 0 for success
Robert Love34f42a02009-02-27 10:55:45 -08001589 */
Robert Love85b4aa42008-12-09 15:10:24 -08001590static int fcoe_ethdrv_get(const struct net_device *netdev)
1591{
1592 struct module *owner;
1593
1594 owner = fcoe_netdev_to_module_owner(netdev);
1595 if (owner) {
Robert Loved5488eb2009-06-10 15:30:59 -07001596 FCOE_NETDEV_DBG(netdev, "Hold driver module %s\n",
1597 module_name(owner));
Robert Love85b4aa42008-12-09 15:10:24 -08001598 return try_module_get(owner);
1599 }
1600 return -ENODEV;
1601}
1602
1603/**
Robert Love34f42a02009-02-27 10:55:45 -08001604 * fcoe_ethdrv_put() - Release the Ethernet driver
Robert Love85b4aa42008-12-09 15:10:24 -08001605 * @netdev: the target netdev
1606 *
Robert Love34f42a02009-02-27 10:55:45 -08001607 * Releases the Ethernet driver module by module_put for
1608 * the corresponding netdev.
1609 *
Chris Leechdd3fd722009-04-21 16:27:36 -07001610 * Returns: 0 for success
Robert Love34f42a02009-02-27 10:55:45 -08001611 */
Robert Love85b4aa42008-12-09 15:10:24 -08001612static int fcoe_ethdrv_put(const struct net_device *netdev)
1613{
1614 struct module *owner;
1615
1616 owner = fcoe_netdev_to_module_owner(netdev);
1617 if (owner) {
Robert Loved5488eb2009-06-10 15:30:59 -07001618 FCOE_NETDEV_DBG(netdev, "Release driver module %s\n",
1619 module_name(owner));
Robert Love85b4aa42008-12-09 15:10:24 -08001620 module_put(owner);
1621 return 0;
1622 }
1623 return -ENODEV;
1624}
1625
1626/**
Robert Love34f42a02009-02-27 10:55:45 -08001627 * fcoe_destroy() - handles the destroy from sysfs
Chris Leechdd3fd722009-04-21 16:27:36 -07001628 * @buffer: expected to be an eth if name
Robert Love85b4aa42008-12-09 15:10:24 -08001629 * @kp: associated kernel param
1630 *
1631 * Returns: 0 for success
Robert Love34f42a02009-02-27 10:55:45 -08001632 */
Robert Love85b4aa42008-12-09 15:10:24 -08001633static int fcoe_destroy(const char *buffer, struct kernel_param *kp)
1634{
Robert Love85b4aa42008-12-09 15:10:24 -08001635 struct net_device *netdev;
Chris Leechaf7f85d2009-08-25 13:59:24 -07001636 struct fc_lport *lport;
1637 int rc;
Robert Love85b4aa42008-12-09 15:10:24 -08001638
1639 netdev = fcoe_if_to_netdev(buffer);
1640 if (!netdev) {
1641 rc = -ENODEV;
1642 goto out_nodev;
1643 }
1644 /* look for existing lport */
Chris Leechaf7f85d2009-08-25 13:59:24 -07001645 lport = fcoe_hostlist_lookup(netdev);
1646 if (!lport) {
Robert Love85b4aa42008-12-09 15:10:24 -08001647 rc = -ENODEV;
1648 goto out_putdev;
1649 }
Chris Leechaf7f85d2009-08-25 13:59:24 -07001650 fcoe_if_destroy(lport);
Robert Love85b4aa42008-12-09 15:10:24 -08001651 fcoe_ethdrv_put(netdev);
1652 rc = 0;
1653out_putdev:
1654 dev_put(netdev);
1655out_nodev:
1656 return rc;
1657}
1658
1659/**
Robert Love34f42a02009-02-27 10:55:45 -08001660 * fcoe_create() - Handles the create call from sysfs
Chris Leechdd3fd722009-04-21 16:27:36 -07001661 * @buffer: expected to be an eth if name
Robert Love85b4aa42008-12-09 15:10:24 -08001662 * @kp: associated kernel param
1663 *
1664 * Returns: 0 for success
Robert Love34f42a02009-02-27 10:55:45 -08001665 */
Robert Love85b4aa42008-12-09 15:10:24 -08001666static int fcoe_create(const char *buffer, struct kernel_param *kp)
1667{
1668 int rc;
Chris Leechaf7f85d2009-08-25 13:59:24 -07001669 struct fc_lport *lport;
Robert Love85b4aa42008-12-09 15:10:24 -08001670 struct net_device *netdev;
1671
1672 netdev = fcoe_if_to_netdev(buffer);
1673 if (!netdev) {
1674 rc = -ENODEV;
1675 goto out_nodev;
1676 }
1677 /* look for existing lport */
1678 if (fcoe_hostlist_lookup(netdev)) {
1679 rc = -EEXIST;
1680 goto out_putdev;
1681 }
1682 fcoe_ethdrv_get(netdev);
1683
Chris Leechaf7f85d2009-08-25 13:59:24 -07001684 lport = fcoe_if_create(netdev, &netdev->dev);
1685 if (IS_ERR(lport)) {
Robert Loved5488eb2009-06-10 15:30:59 -07001686 printk(KERN_ERR "fcoe: Failed to create interface (%s)\n",
Robert Love85b4aa42008-12-09 15:10:24 -08001687 netdev->name);
1688 fcoe_ethdrv_put(netdev);
1689 rc = -EIO;
1690 goto out_putdev;
1691 }
1692 rc = 0;
1693out_putdev:
1694 dev_put(netdev);
1695out_nodev:
1696 return rc;
1697}
1698
1699module_param_call(create, fcoe_create, NULL, NULL, S_IWUSR);
1700__MODULE_PARM_TYPE(create, "string");
Chris Leech014f5c32009-08-25 13:59:30 -07001701MODULE_PARM_DESC(create, "Create fcoe fcoe using net device passed in.");
Robert Love85b4aa42008-12-09 15:10:24 -08001702module_param_call(destroy, fcoe_destroy, NULL, NULL, S_IWUSR);
1703__MODULE_PARM_TYPE(destroy, "string");
Chris Leech014f5c32009-08-25 13:59:30 -07001704MODULE_PARM_DESC(destroy, "Destroy fcoe fcoe");
Robert Love85b4aa42008-12-09 15:10:24 -08001705
Robert Love34f42a02009-02-27 10:55:45 -08001706/**
1707 * fcoe_link_ok() - Check if link is ok for the fc_lport
Robert Love85b4aa42008-12-09 15:10:24 -08001708 * @lp: ptr to the fc_lport
1709 *
1710 * Any permanently-disqualifying conditions have been previously checked.
1711 * This also updates the speed setting, which may change with link for 100/1000.
1712 *
1713 * This function should probably be checking for PAUSE support at some point
1714 * in the future. Currently Per-priority-pause is not determinable using
1715 * ethtool, so we shouldn't be restrictive until that problem is resolved.
1716 *
1717 * Returns: 0 if link is OK for use by FCoE.
1718 *
1719 */
1720int fcoe_link_ok(struct fc_lport *lp)
1721{
Chris Leech014f5c32009-08-25 13:59:30 -07001722 struct fcoe_port *port = lport_priv(lp);
Chris Leech25024982009-08-25 13:59:35 -07001723 struct net_device *dev = port->fcoe->netdev;
Robert Love85b4aa42008-12-09 15:10:24 -08001724 struct ethtool_cmd ecmd = { ETHTOOL_GSET };
Robert Love85b4aa42008-12-09 15:10:24 -08001725
Yi Zou2f718d62009-07-29 17:04:01 -07001726 if ((dev->flags & IFF_UP) && netif_carrier_ok(dev) &&
1727 (!dev_ethtool_get_settings(dev, &ecmd))) {
1728 lp->link_supported_speeds &=
1729 ~(FC_PORTSPEED_1GBIT | FC_PORTSPEED_10GBIT);
1730 if (ecmd.supported & (SUPPORTED_1000baseT_Half |
1731 SUPPORTED_1000baseT_Full))
1732 lp->link_supported_speeds |= FC_PORTSPEED_1GBIT;
1733 if (ecmd.supported & SUPPORTED_10000baseT_Full)
1734 lp->link_supported_speeds |=
1735 FC_PORTSPEED_10GBIT;
1736 if (ecmd.speed == SPEED_1000)
1737 lp->link_speed = FC_PORTSPEED_1GBIT;
1738 if (ecmd.speed == SPEED_10000)
1739 lp->link_speed = FC_PORTSPEED_10GBIT;
Robert Love85b4aa42008-12-09 15:10:24 -08001740
Yi Zou2f718d62009-07-29 17:04:01 -07001741 return 0;
1742 }
1743 return -1;
Robert Love85b4aa42008-12-09 15:10:24 -08001744}
Robert Love85b4aa42008-12-09 15:10:24 -08001745
Robert Love34f42a02009-02-27 10:55:45 -08001746/**
1747 * fcoe_percpu_clean() - Clear the pending skbs for an lport
Robert Love85b4aa42008-12-09 15:10:24 -08001748 * @lp: the fc_lport
1749 */
1750void fcoe_percpu_clean(struct fc_lport *lp)
1751{
Robert Love85b4aa42008-12-09 15:10:24 -08001752 struct fcoe_percpu_s *pp;
1753 struct fcoe_rcv_info *fr;
1754 struct sk_buff_head *list;
1755 struct sk_buff *skb, *next;
1756 struct sk_buff *head;
Robert Love5e5e92d2009-03-17 11:41:35 -07001757 unsigned int cpu;
Robert Love85b4aa42008-12-09 15:10:24 -08001758
Robert Love5e5e92d2009-03-17 11:41:35 -07001759 for_each_possible_cpu(cpu) {
1760 pp = &per_cpu(fcoe_percpu, cpu);
1761 spin_lock_bh(&pp->fcoe_rx_list.lock);
1762 list = &pp->fcoe_rx_list;
1763 head = list->next;
1764 for (skb = head; skb != (struct sk_buff *)list;
1765 skb = next) {
1766 next = skb->next;
1767 fr = fcoe_dev_from_skb(skb);
1768 if (fr->fr_dev == lp) {
1769 __skb_unlink(skb, list);
1770 kfree_skb(skb);
Robert Love85b4aa42008-12-09 15:10:24 -08001771 }
Robert Love85b4aa42008-12-09 15:10:24 -08001772 }
Robert Love5e5e92d2009-03-17 11:41:35 -07001773 spin_unlock_bh(&pp->fcoe_rx_list.lock);
Robert Love85b4aa42008-12-09 15:10:24 -08001774 }
1775}
Robert Love85b4aa42008-12-09 15:10:24 -08001776
1777/**
Robert Love34f42a02009-02-27 10:55:45 -08001778 * fcoe_clean_pending_queue() - Dequeue a skb and free it
Robert Love85b4aa42008-12-09 15:10:24 -08001779 * @lp: the corresponding fc_lport
1780 *
1781 * Returns: none
Robert Love34f42a02009-02-27 10:55:45 -08001782 */
Robert Love85b4aa42008-12-09 15:10:24 -08001783void fcoe_clean_pending_queue(struct fc_lport *lp)
1784{
Chris Leech014f5c32009-08-25 13:59:30 -07001785 struct fcoe_port *port = lport_priv(lp);
Robert Love85b4aa42008-12-09 15:10:24 -08001786 struct sk_buff *skb;
1787
Chris Leech014f5c32009-08-25 13:59:30 -07001788 spin_lock_bh(&port->fcoe_pending_queue.lock);
1789 while ((skb = __skb_dequeue(&port->fcoe_pending_queue)) != NULL) {
1790 spin_unlock_bh(&port->fcoe_pending_queue.lock);
Robert Love85b4aa42008-12-09 15:10:24 -08001791 kfree_skb(skb);
Chris Leech014f5c32009-08-25 13:59:30 -07001792 spin_lock_bh(&port->fcoe_pending_queue.lock);
Robert Love85b4aa42008-12-09 15:10:24 -08001793 }
Chris Leech014f5c32009-08-25 13:59:30 -07001794 spin_unlock_bh(&port->fcoe_pending_queue.lock);
Robert Love85b4aa42008-12-09 15:10:24 -08001795}
Robert Love85b4aa42008-12-09 15:10:24 -08001796
1797/**
Robert Love34f42a02009-02-27 10:55:45 -08001798 * fcoe_reset() - Resets the fcoe
Robert Love85b4aa42008-12-09 15:10:24 -08001799 * @shost: shost the reset is from
1800 *
1801 * Returns: always 0
1802 */
1803int fcoe_reset(struct Scsi_Host *shost)
1804{
1805 struct fc_lport *lport = shost_priv(shost);
1806 fc_lport_reset(lport);
1807 return 0;
1808}
Robert Love85b4aa42008-12-09 15:10:24 -08001809
Robert Love34f42a02009-02-27 10:55:45 -08001810/**
Chris Leech014f5c32009-08-25 13:59:30 -07001811 * fcoe_hostlist_lookup_port() - find the corresponding lport by a given device
Chris Leechdd3fd722009-04-21 16:27:36 -07001812 * @dev: this is currently ptr to net_device
Robert Love85b4aa42008-12-09 15:10:24 -08001813 *
Vasu Deve8af4d42009-07-29 17:05:15 -07001814 * Called with fcoe_hostlist_lock held.
1815 *
Chris Leech014f5c32009-08-25 13:59:30 -07001816 * Returns: NULL or the located fcoe_port
Robert Love85b4aa42008-12-09 15:10:24 -08001817 */
Chris Leech014f5c32009-08-25 13:59:30 -07001818static struct fcoe_interface *
1819fcoe_hostlist_lookup_port(const struct net_device *dev)
Robert Love85b4aa42008-12-09 15:10:24 -08001820{
Chris Leech014f5c32009-08-25 13:59:30 -07001821 struct fcoe_interface *fcoe;
Robert Love85b4aa42008-12-09 15:10:24 -08001822
Chris Leech014f5c32009-08-25 13:59:30 -07001823 list_for_each_entry(fcoe, &fcoe_hostlist, list) {
Chris Leech25024982009-08-25 13:59:35 -07001824 if (fcoe->netdev == dev)
Chris Leech014f5c32009-08-25 13:59:30 -07001825 return fcoe;
Robert Love85b4aa42008-12-09 15:10:24 -08001826 }
Robert Love85b4aa42008-12-09 15:10:24 -08001827 return NULL;
1828}
1829
Robert Love34f42a02009-02-27 10:55:45 -08001830/**
1831 * fcoe_hostlist_lookup() - Find the corresponding lport by netdev
Robert Love85b4aa42008-12-09 15:10:24 -08001832 * @netdev: ptr to net_device
1833 *
1834 * Returns: 0 for success
1835 */
1836struct fc_lport *fcoe_hostlist_lookup(const struct net_device *netdev)
1837{
Chris Leech014f5c32009-08-25 13:59:30 -07001838 struct fcoe_interface *fcoe;
Robert Love85b4aa42008-12-09 15:10:24 -08001839
Vasu Deve8af4d42009-07-29 17:05:15 -07001840 read_lock(&fcoe_hostlist_lock);
Chris Leech014f5c32009-08-25 13:59:30 -07001841 fcoe = fcoe_hostlist_lookup_port(netdev);
Vasu Deve8af4d42009-07-29 17:05:15 -07001842 read_unlock(&fcoe_hostlist_lock);
Robert Love85b4aa42008-12-09 15:10:24 -08001843
Chris Leech014f5c32009-08-25 13:59:30 -07001844 return (fcoe) ? fcoe->priv->ctlr.lp : NULL;
Robert Love85b4aa42008-12-09 15:10:24 -08001845}
Robert Love85b4aa42008-12-09 15:10:24 -08001846
Robert Love34f42a02009-02-27 10:55:45 -08001847/**
1848 * fcoe_hostlist_add() - Add a lport to lports list
Chris Leechdd3fd722009-04-21 16:27:36 -07001849 * @lp: ptr to the fc_lport to be added
Robert Love85b4aa42008-12-09 15:10:24 -08001850 *
Vasu Deve8af4d42009-07-29 17:05:15 -07001851 * Called with write fcoe_hostlist_lock held.
1852 *
Robert Love85b4aa42008-12-09 15:10:24 -08001853 * Returns: 0 for success
1854 */
Chris Leech014f5c32009-08-25 13:59:30 -07001855int fcoe_hostlist_add(const struct fc_lport *lport)
Robert Love85b4aa42008-12-09 15:10:24 -08001856{
Chris Leech014f5c32009-08-25 13:59:30 -07001857 struct fcoe_interface *fcoe;
1858 struct fcoe_port *port;
Robert Love85b4aa42008-12-09 15:10:24 -08001859
Chris Leech014f5c32009-08-25 13:59:30 -07001860 fcoe = fcoe_hostlist_lookup_port(fcoe_netdev(lport));
1861 if (!fcoe) {
1862 port = lport_priv(lport);
1863 fcoe = port->fcoe;
1864 list_add_tail(&fcoe->list, &fcoe_hostlist);
Robert Love85b4aa42008-12-09 15:10:24 -08001865 }
1866 return 0;
1867}
Robert Love85b4aa42008-12-09 15:10:24 -08001868
Robert Love34f42a02009-02-27 10:55:45 -08001869/**
1870 * fcoe_hostlist_remove() - remove a lport from lports list
Chris Leechdd3fd722009-04-21 16:27:36 -07001871 * @lp: ptr to the fc_lport to be removed
Robert Love85b4aa42008-12-09 15:10:24 -08001872 *
1873 * Returns: 0 for success
1874 */
Chris Leech014f5c32009-08-25 13:59:30 -07001875int fcoe_hostlist_remove(const struct fc_lport *lport)
Robert Love85b4aa42008-12-09 15:10:24 -08001876{
Chris Leech014f5c32009-08-25 13:59:30 -07001877 struct fcoe_interface *fcoe;
Robert Love85b4aa42008-12-09 15:10:24 -08001878
Vasu Deve8af4d42009-07-29 17:05:15 -07001879 write_lock_bh(&fcoe_hostlist_lock);
Chris Leech014f5c32009-08-25 13:59:30 -07001880 fcoe = fcoe_hostlist_lookup_port(fcoe_netdev(lport));
1881 BUG_ON(!fcoe);
1882 list_del(&fcoe->list);
Robert Love85b4aa42008-12-09 15:10:24 -08001883 write_unlock_bh(&fcoe_hostlist_lock);
1884
1885 return 0;
1886}
Robert Love85b4aa42008-12-09 15:10:24 -08001887
1888/**
Robert Love34f42a02009-02-27 10:55:45 -08001889 * fcoe_init() - fcoe module loading initialization
Robert Love85b4aa42008-12-09 15:10:24 -08001890 *
Robert Love85b4aa42008-12-09 15:10:24 -08001891 * Returns 0 on success, negative on failure
Robert Love34f42a02009-02-27 10:55:45 -08001892 */
Robert Love85b4aa42008-12-09 15:10:24 -08001893static int __init fcoe_init(void)
1894{
Robert Love38eccab2009-03-17 11:41:30 -07001895 unsigned int cpu;
Robert Love8976f422009-03-17 11:41:46 -07001896 int rc = 0;
Robert Love85b4aa42008-12-09 15:10:24 -08001897 struct fcoe_percpu_s *p;
1898
Robert Love38eccab2009-03-17 11:41:30 -07001899 for_each_possible_cpu(cpu) {
Robert Love5e5e92d2009-03-17 11:41:35 -07001900 p = &per_cpu(fcoe_percpu, cpu);
Robert Love38eccab2009-03-17 11:41:30 -07001901 skb_queue_head_init(&p->fcoe_rx_list);
1902 }
1903
Robert Love8976f422009-03-17 11:41:46 -07001904 for_each_online_cpu(cpu)
1905 fcoe_percpu_thread_create(cpu);
Robert Love85b4aa42008-12-09 15:10:24 -08001906
Robert Love8976f422009-03-17 11:41:46 -07001907 /* Initialize per CPU interrupt thread */
1908 rc = register_hotcpu_notifier(&fcoe_cpu_notifier);
1909 if (rc)
1910 goto out_free;
Robert Love85b4aa42008-12-09 15:10:24 -08001911
Robert Love8976f422009-03-17 11:41:46 -07001912 /* Setup link change notification */
Robert Love85b4aa42008-12-09 15:10:24 -08001913 fcoe_dev_setup();
1914
Chris Leech5892c322009-08-25 13:59:14 -07001915 rc = fcoe_if_init();
1916 if (rc)
1917 goto out_free;
Robert Love85b4aa42008-12-09 15:10:24 -08001918
1919 return 0;
Robert Love8976f422009-03-17 11:41:46 -07001920
1921out_free:
1922 for_each_online_cpu(cpu) {
1923 fcoe_percpu_thread_destroy(cpu);
1924 }
1925
1926 return rc;
Robert Love85b4aa42008-12-09 15:10:24 -08001927}
1928module_init(fcoe_init);
1929
1930/**
Robert Love34f42a02009-02-27 10:55:45 -08001931 * fcoe_exit() - fcoe module unloading cleanup
Robert Love85b4aa42008-12-09 15:10:24 -08001932 *
1933 * Returns 0 on success, negative on failure
Robert Love34f42a02009-02-27 10:55:45 -08001934 */
Robert Love85b4aa42008-12-09 15:10:24 -08001935static void __exit fcoe_exit(void)
1936{
Robert Love5e5e92d2009-03-17 11:41:35 -07001937 unsigned int cpu;
Chris Leech014f5c32009-08-25 13:59:30 -07001938 struct fcoe_interface *fcoe, *tmp;
Robert Love85b4aa42008-12-09 15:10:24 -08001939
Robert Love85b4aa42008-12-09 15:10:24 -08001940 fcoe_dev_cleanup();
1941
Vasu Dev5919a592009-03-27 09:03:29 -07001942 /* releases the associated fcoe hosts */
Chris Leech014f5c32009-08-25 13:59:30 -07001943 list_for_each_entry_safe(fcoe, tmp, &fcoe_hostlist, list)
1944 fcoe_if_destroy(fcoe->priv->ctlr.lp);
Robert Love85b4aa42008-12-09 15:10:24 -08001945
Robert Love8976f422009-03-17 11:41:46 -07001946 unregister_hotcpu_notifier(&fcoe_cpu_notifier);
1947
Chris Leech014f5c32009-08-25 13:59:30 -07001948 for_each_online_cpu(cpu)
Robert Love8976f422009-03-17 11:41:46 -07001949 fcoe_percpu_thread_destroy(cpu);
Robert Love85b4aa42008-12-09 15:10:24 -08001950
Vasu Dev7f349142009-03-27 09:06:31 -07001951 /* detach from scsi transport */
1952 fcoe_if_exit();
Robert Love85b4aa42008-12-09 15:10:24 -08001953}
1954module_exit(fcoe_exit);