blob: 44c963c8bc7d010c7821554224e4c0ca3b6a1ff3 [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 Leech014f5c32009-08-25 13:59:30 -0700154 struct fcoe_port *port;
Vasu Devab6b85c2009-05-17 12:33:08 +0000155
Chris Leech014f5c32009-08-25 13:59:30 -0700156 port = container_of(ptype, struct fcoe_port, fip_packet_type);
157 fcoe_ctlr_recv(&port->ctlr, skb);
Vasu Devab6b85c2009-05-17 12:33:08 +0000158 return 0;
159}
160
161/**
162 * fcoe_fip_send() - send an Ethernet-encapsulated FIP frame.
163 * @fip: FCoE controller.
164 * @skb: FIP Packet.
165 */
166static void fcoe_fip_send(struct fcoe_ctlr *fip, struct sk_buff *skb)
167{
Chris Leech25024982009-08-25 13:59:35 -0700168 skb->dev = fcoe_from_ctlr(fip)->fcoe->netdev;
Vasu Devab6b85c2009-05-17 12:33:08 +0000169 dev_queue_xmit(skb);
170}
171
172/**
173 * fcoe_update_src_mac() - Update Ethernet MAC filters.
174 * @fip: FCoE controller.
175 * @old: Unicast MAC address to delete if the MAC is non-zero.
176 * @new: Unicast MAC address to add.
177 *
178 * Remove any previously-set unicast MAC filter.
179 * Add secondary FCoE MAC address filter for our OUI.
180 */
181static void fcoe_update_src_mac(struct fcoe_ctlr *fip, u8 *old, u8 *new)
182{
Chris Leech25024982009-08-25 13:59:35 -0700183 struct fcoe_interface *fcoe;
Chris Leech014f5c32009-08-25 13:59:30 -0700184 struct fcoe_port *port;
Vasu Devab6b85c2009-05-17 12:33:08 +0000185
Chris Leech014f5c32009-08-25 13:59:30 -0700186 port = fcoe_from_ctlr(fip);
Chris Leech25024982009-08-25 13:59:35 -0700187 fcoe = port->fcoe;
188
Vasu Devab6b85c2009-05-17 12:33:08 +0000189 rtnl_lock();
190 if (!is_zero_ether_addr(old))
Chris Leech25024982009-08-25 13:59:35 -0700191 dev_unicast_delete(fcoe->netdev, old);
192 dev_unicast_add(fcoe->netdev, new);
Vasu Devab6b85c2009-05-17 12:33:08 +0000193 rtnl_unlock();
194}
195
196/**
Vasu Dev7f349142009-03-27 09:06:31 -0700197 * fcoe_lport_config() - sets up the fc_lport
198 * @lp: ptr to the fc_lport
Vasu Dev7f349142009-03-27 09:06:31 -0700199 *
200 * Returns: 0 for success
201 */
202static int fcoe_lport_config(struct fc_lport *lp)
203{
204 lp->link_up = 0;
205 lp->qfull = 0;
206 lp->max_retry_count = 3;
Abhijeet Joglekara3666952009-05-01 10:01:26 -0700207 lp->max_rport_retry_count = 3;
Vasu Dev7f349142009-03-27 09:06:31 -0700208 lp->e_d_tov = 2 * 1000; /* FC-FS default */
209 lp->r_a_tov = 2 * 2 * 1000;
210 lp->service_params = (FCP_SPPF_INIT_FCN | FCP_SPPF_RD_XRDY_DIS |
211 FCP_SPPF_RETRY | FCP_SPPF_CONF_COMPL);
212
213 fc_lport_init_stats(lp);
214
215 /* lport fc_lport related configuration */
216 fc_lport_config(lp);
217
218 /* offload related configuration */
219 lp->crc_offload = 0;
220 lp->seq_offload = 0;
221 lp->lro_enabled = 0;
222 lp->lro_xid = 0;
223 lp->lso_max = 0;
224
225 return 0;
226}
227
228/**
Vasu Devab6b85c2009-05-17 12:33:08 +0000229 * fcoe_netdev_cleanup() - clean up netdev configurations
Chris Leech014f5c32009-08-25 13:59:30 -0700230 * @port: ptr to the fcoe_port
Vasu Devab6b85c2009-05-17 12:33:08 +0000231 */
Chris Leech014f5c32009-08-25 13:59:30 -0700232void fcoe_netdev_cleanup(struct fcoe_port *port)
Vasu Devab6b85c2009-05-17 12:33:08 +0000233{
234 u8 flogi_maddr[ETH_ALEN];
Chris Leech25024982009-08-25 13:59:35 -0700235 struct fcoe_interface *fcoe = port->fcoe;
Vasu Devab6b85c2009-05-17 12:33:08 +0000236
237 /* Don't listen for Ethernet packets anymore */
Chris Leech014f5c32009-08-25 13:59:30 -0700238 dev_remove_pack(&port->fcoe_packet_type);
239 dev_remove_pack(&port->fip_packet_type);
Vasu Devab6b85c2009-05-17 12:33:08 +0000240
241 /* Delete secondary MAC addresses */
242 rtnl_lock();
243 memcpy(flogi_maddr, (u8[6]) FC_FCOE_FLOGI_MAC, ETH_ALEN);
Chris Leech25024982009-08-25 13:59:35 -0700244 dev_unicast_delete(fcoe->netdev, flogi_maddr);
Chris Leech014f5c32009-08-25 13:59:30 -0700245 if (!is_zero_ether_addr(port->ctlr.data_src_addr))
Chris Leech25024982009-08-25 13:59:35 -0700246 dev_unicast_delete(fcoe->netdev, port->ctlr.data_src_addr);
Chris Leech014f5c32009-08-25 13:59:30 -0700247 if (port->ctlr.spma)
Chris Leech25024982009-08-25 13:59:35 -0700248 dev_unicast_delete(fcoe->netdev, port->ctlr.ctl_src_addr);
249 dev_mc_delete(fcoe->netdev, FIP_ALL_ENODE_MACS, ETH_ALEN, 0);
Vasu Devab6b85c2009-05-17 12:33:08 +0000250 rtnl_unlock();
251}
252
253/**
Vasu Dev1047f222009-05-06 10:52:40 -0700254 * fcoe_queue_timer() - fcoe queue timer
255 * @lp: the fc_lport pointer
256 *
257 * Calls fcoe_check_wait_queue on timeout
258 *
259 */
260static void fcoe_queue_timer(ulong lp)
261{
262 fcoe_check_wait_queue((struct fc_lport *)lp, NULL);
263}
264
265/**
Vasu Dev7f349142009-03-27 09:06:31 -0700266 * fcoe_netdev_config() - Set up netdev for SW FCoE
267 * @lp : ptr to the fc_lport
268 * @netdev : ptr to the associated netdevice struct
269 *
270 * Must be called after fcoe_lport_config() as it will use lport mutex
271 *
272 * Returns : 0 for success
273 */
274static int fcoe_netdev_config(struct fc_lport *lp, struct net_device *netdev)
275{
276 u32 mfs;
277 u64 wwnn, wwpn;
Chris Leech25024982009-08-25 13:59:35 -0700278 struct fcoe_interface *fcoe;
Chris Leech014f5c32009-08-25 13:59:30 -0700279 struct fcoe_port *port;
Vasu Dev7f349142009-03-27 09:06:31 -0700280 u8 flogi_maddr[ETH_ALEN];
Vasu Dev184dd342009-05-17 12:33:28 +0000281 struct netdev_hw_addr *ha;
Vasu Dev7f349142009-03-27 09:06:31 -0700282
283 /* Setup lport private data to point to fcoe softc */
Chris Leech014f5c32009-08-25 13:59:30 -0700284 port = lport_priv(lp);
Chris Leech25024982009-08-25 13:59:35 -0700285 fcoe = port->fcoe;
Chris Leech014f5c32009-08-25 13:59:30 -0700286 port->ctlr.lp = lp;
Chris Leech25024982009-08-25 13:59:35 -0700287 fcoe->netdev = netdev;
Vasu Dev7f349142009-03-27 09:06:31 -0700288
289 /* Do not support for bonding device */
Vasu Dev1d1b88d2009-07-29 17:05:45 -0700290 if ((netdev->priv_flags & IFF_MASTER_ALB) ||
291 (netdev->priv_flags & IFF_SLAVE_INACTIVE) ||
292 (netdev->priv_flags & IFF_MASTER_8023AD)) {
Vasu Dev7f349142009-03-27 09:06:31 -0700293 return -EOPNOTSUPP;
294 }
295
296 /*
297 * Determine max frame size based on underlying device and optional
298 * user-configured limit. If the MFS is too low, fcoe_link_ok()
299 * will return 0, so do this first.
300 */
Vasu Dev1d1b88d2009-07-29 17:05:45 -0700301 mfs = netdev->mtu - (sizeof(struct fcoe_hdr) +
302 sizeof(struct fcoe_crc_eof));
Vasu Dev7f349142009-03-27 09:06:31 -0700303 if (fc_set_mfs(lp, mfs))
304 return -EINVAL;
305
Vasu Dev7f349142009-03-27 09:06:31 -0700306 /* offload features support */
Vasu Dev1d1b88d2009-07-29 17:05:45 -0700307 if (netdev->features & NETIF_F_SG)
Vasu Dev7f349142009-03-27 09:06:31 -0700308 lp->sg_supp = 1;
309
Vasu Dev7f349142009-03-27 09:06:31 -0700310 if (netdev->features & NETIF_F_FCOE_CRC) {
311 lp->crc_offload = 1;
Robert Loved5488eb2009-06-10 15:30:59 -0700312 FCOE_NETDEV_DBG(netdev, "Supports FCCRC offload\n");
Vasu Dev7f349142009-03-27 09:06:31 -0700313 }
Vasu Dev7f349142009-03-27 09:06:31 -0700314 if (netdev->features & NETIF_F_FSO) {
315 lp->seq_offload = 1;
316 lp->lso_max = netdev->gso_max_size;
Robert Loved5488eb2009-06-10 15:30:59 -0700317 FCOE_NETDEV_DBG(netdev, "Supports LSO for max len 0x%x\n",
318 lp->lso_max);
Vasu Dev7f349142009-03-27 09:06:31 -0700319 }
Vasu Dev7f349142009-03-27 09:06:31 -0700320 if (netdev->fcoe_ddp_xid) {
321 lp->lro_enabled = 1;
322 lp->lro_xid = netdev->fcoe_ddp_xid;
Robert Loved5488eb2009-06-10 15:30:59 -0700323 FCOE_NETDEV_DBG(netdev, "Supports LRO for max xid 0x%x\n",
324 lp->lro_xid);
Vasu Dev7f349142009-03-27 09:06:31 -0700325 }
Chris Leech014f5c32009-08-25 13:59:30 -0700326 skb_queue_head_init(&port->fcoe_pending_queue);
327 port->fcoe_pending_queue_active = 0;
328 setup_timer(&port->timer, fcoe_queue_timer, (unsigned long)lp);
Vasu Dev7f349142009-03-27 09:06:31 -0700329
Vasu Dev184dd342009-05-17 12:33:28 +0000330 /* look for SAN MAC address, if multiple SAN MACs exist, only
331 * use the first one for SPMA */
332 rcu_read_lock();
333 for_each_dev_addr(netdev, ha) {
334 if ((ha->type == NETDEV_HW_ADDR_T_SAN) &&
Chris Leech014f5c32009-08-25 13:59:30 -0700335 (is_valid_ether_addr(port->ctlr.ctl_src_addr))) {
336 memcpy(port->ctlr.ctl_src_addr, ha->addr, ETH_ALEN);
337 port->ctlr.spma = 1;
Vasu Dev184dd342009-05-17 12:33:28 +0000338 break;
339 }
340 }
341 rcu_read_unlock();
342
Vasu Dev7f349142009-03-27 09:06:31 -0700343 /* setup Source Mac Address */
Chris Leech014f5c32009-08-25 13:59:30 -0700344 if (!port->ctlr.spma)
345 memcpy(port->ctlr.ctl_src_addr, netdev->dev_addr,
346 netdev->addr_len);
Vasu Dev7f349142009-03-27 09:06:31 -0700347
Vasu Dev1d1b88d2009-07-29 17:05:45 -0700348 wwnn = fcoe_wwn_from_mac(netdev->dev_addr, 1, 0);
Vasu Dev7f349142009-03-27 09:06:31 -0700349 fc_set_wwnn(lp, wwnn);
350 /* XXX - 3rd arg needs to be vlan id */
Vasu Dev1d1b88d2009-07-29 17:05:45 -0700351 wwpn = fcoe_wwn_from_mac(netdev->dev_addr, 2, 0);
Vasu Dev7f349142009-03-27 09:06:31 -0700352 fc_set_wwpn(lp, wwpn);
353
354 /*
355 * Add FCoE MAC address as second unicast MAC address
356 * or enter promiscuous mode if not capable of listening
357 * for multiple unicast MACs.
358 */
359 rtnl_lock();
360 memcpy(flogi_maddr, (u8[6]) FC_FCOE_FLOGI_MAC, ETH_ALEN);
Vasu Dev1d1b88d2009-07-29 17:05:45 -0700361 dev_unicast_add(netdev, flogi_maddr);
Chris Leech014f5c32009-08-25 13:59:30 -0700362 if (port->ctlr.spma)
363 dev_unicast_add(netdev, port->ctlr.ctl_src_addr);
Vasu Dev1d1b88d2009-07-29 17:05:45 -0700364 dev_mc_add(netdev, FIP_ALL_ENODE_MACS, ETH_ALEN, 0);
Vasu Dev7f349142009-03-27 09:06:31 -0700365 rtnl_unlock();
366
367 /*
368 * setup the receive function from ethernet driver
369 * on the ethertype for the given device
370 */
Chris Leech014f5c32009-08-25 13:59:30 -0700371 port->fcoe_packet_type.func = fcoe_rcv;
372 port->fcoe_packet_type.type = __constant_htons(ETH_P_FCOE);
373 port->fcoe_packet_type.dev = netdev;
374 dev_add_pack(&port->fcoe_packet_type);
Vasu Dev7f349142009-03-27 09:06:31 -0700375
Chris Leech014f5c32009-08-25 13:59:30 -0700376 port->fip_packet_type.func = fcoe_fip_recv;
377 port->fip_packet_type.type = htons(ETH_P_FIP);
378 port->fip_packet_type.dev = netdev;
379 dev_add_pack(&port->fip_packet_type);
Vasu Devab6b85c2009-05-17 12:33:08 +0000380
Vasu Dev7f349142009-03-27 09:06:31 -0700381 return 0;
382}
383
384/**
385 * fcoe_shost_config() - Sets up fc_lport->host
386 * @lp : ptr to the fc_lport
387 * @shost : ptr to the associated scsi host
388 * @dev : device associated to scsi host
389 *
390 * Must be called after fcoe_lport_config() and fcoe_netdev_config()
391 *
392 * Returns : 0 for success
393 */
394static int fcoe_shost_config(struct fc_lport *lp, struct Scsi_Host *shost,
395 struct device *dev)
396{
397 int rc = 0;
398
399 /* lport scsi host config */
400 lp->host = shost;
401
402 lp->host->max_lun = FCOE_MAX_LUN;
403 lp->host->max_id = FCOE_MAX_FCP_TARGET;
404 lp->host->max_channel = 0;
405 lp->host->transportt = scsi_transport_fcoe_sw;
406
407 /* add the new host to the SCSI-ml */
408 rc = scsi_add_host(lp->host, dev);
409 if (rc) {
Robert Loved5488eb2009-06-10 15:30:59 -0700410 FCOE_NETDEV_DBG(fcoe_netdev(lp), "fcoe_shost_config: "
411 "error on scsi_add_host\n");
Vasu Dev7f349142009-03-27 09:06:31 -0700412 return rc;
413 }
414 sprintf(fc_host_symbolic_name(lp->host), "%s v%s over %s",
415 FCOE_NAME, FCOE_VERSION,
416 fcoe_netdev(lp)->name);
417
418 return 0;
419}
420
Vasu Devd7179682009-07-29 17:05:21 -0700421/*
422 * fcoe_oem_match() - match for read types IO
423 * @fp: the fc_frame for new IO.
424 *
425 * Returns : true for read types IO, otherwise returns false.
426 */
427bool fcoe_oem_match(struct fc_frame *fp)
428{
Yi Zou05cc7392009-08-25 13:59:03 -0700429 return fc_fcp_is_read(fr_fsp(fp)) &&
430 (fr_fsp(fp)->data_len > fcoe_ddp_min);
Vasu Devd7179682009-07-29 17:05:21 -0700431}
432
Vasu Dev7f349142009-03-27 09:06:31 -0700433/**
434 * fcoe_em_config() - allocates em for this lport
Chris Leech014f5c32009-08-25 13:59:30 -0700435 * @lp: the fcoe that em is to allocated for
Vasu Dev7f349142009-03-27 09:06:31 -0700436 *
Vasu Deve8af4d42009-07-29 17:05:15 -0700437 * Called with write fcoe_hostlist_lock held.
438 *
Vasu Dev7f349142009-03-27 09:06:31 -0700439 * Returns : 0 on success
440 */
441static inline int fcoe_em_config(struct fc_lport *lp)
442{
Chris Leech014f5c32009-08-25 13:59:30 -0700443 struct fcoe_port *port = lport_priv(lp);
Chris Leech25024982009-08-25 13:59:35 -0700444 struct fcoe_port *oldport = NULL;
445 struct fcoe_interface *fcoe = port->fcoe;
446 struct fcoe_interface *oldfcoe = NULL;
Vasu Dev1d1b88d2009-07-29 17:05:45 -0700447 struct net_device *old_real_dev, *cur_real_dev;
Vasu Devd7179682009-07-29 17:05:21 -0700448 u16 min_xid = FCOE_MIN_XID;
449 u16 max_xid = FCOE_MAX_XID;
450
451 /*
452 * Check if need to allocate an em instance for
453 * offload exchange ids to be shared across all VN_PORTs/lport.
454 */
455 if (!lp->lro_enabled || !lp->lro_xid || (lp->lro_xid >= max_xid)) {
456 lp->lro_xid = 0;
457 goto skip_oem;
458 }
459
460 /*
461 * Reuse existing offload em instance in case
Vasu Dev1d1b88d2009-07-29 17:05:45 -0700462 * it is already allocated on real eth device
Vasu Devd7179682009-07-29 17:05:21 -0700463 */
Chris Leech25024982009-08-25 13:59:35 -0700464 if (fcoe->netdev->priv_flags & IFF_802_1Q_VLAN)
465 cur_real_dev = vlan_dev_real_dev(fcoe->netdev);
Vasu Dev1d1b88d2009-07-29 17:05:45 -0700466 else
Chris Leech25024982009-08-25 13:59:35 -0700467 cur_real_dev = fcoe->netdev;
Vasu Dev1d1b88d2009-07-29 17:05:45 -0700468
Chris Leech25024982009-08-25 13:59:35 -0700469 list_for_each_entry(oldfcoe, &fcoe_hostlist, list) {
470 oldport = oldfcoe->priv;
471 if (oldfcoe->netdev->priv_flags & IFF_802_1Q_VLAN)
472 old_real_dev = vlan_dev_real_dev(oldfcoe->netdev);
Vasu Dev1d1b88d2009-07-29 17:05:45 -0700473 else
Chris Leech25024982009-08-25 13:59:35 -0700474 old_real_dev = oldfcoe->netdev;
Vasu Dev1d1b88d2009-07-29 17:05:45 -0700475
476 if (cur_real_dev == old_real_dev) {
Chris Leech25024982009-08-25 13:59:35 -0700477 port->oem = oldport->oem;
Vasu Devd7179682009-07-29 17:05:21 -0700478 break;
479 }
480 }
481
Chris Leech014f5c32009-08-25 13:59:30 -0700482 if (port->oem) {
483 if (!fc_exch_mgr_add(lp, port->oem, fcoe_oem_match)) {
Vasu Devd7179682009-07-29 17:05:21 -0700484 printk(KERN_ERR "fcoe_em_config: failed to add "
485 "offload em:%p on interface:%s\n",
Chris Leech25024982009-08-25 13:59:35 -0700486 port->oem, fcoe->netdev->name);
Vasu Devd7179682009-07-29 17:05:21 -0700487 return -ENOMEM;
488 }
489 } else {
Chris Leech014f5c32009-08-25 13:59:30 -0700490 port->oem = fc_exch_mgr_alloc(lp, FC_CLASS_3,
Vasu Devd7179682009-07-29 17:05:21 -0700491 FCOE_MIN_XID, lp->lro_xid,
492 fcoe_oem_match);
Chris Leech014f5c32009-08-25 13:59:30 -0700493 if (!port->oem) {
Vasu Devd7179682009-07-29 17:05:21 -0700494 printk(KERN_ERR "fcoe_em_config: failed to allocate "
495 "em for offload exches on interface:%s\n",
Chris Leech25024982009-08-25 13:59:35 -0700496 fcoe->netdev->name);
Vasu Devd7179682009-07-29 17:05:21 -0700497 return -ENOMEM;
498 }
499 }
500
501 /*
502 * Exclude offload EM xid range from next EM xid range.
503 */
504 min_xid += lp->lro_xid + 1;
505
506skip_oem:
507 if (!fc_exch_mgr_alloc(lp, FC_CLASS_3, min_xid, max_xid, NULL)) {
508 printk(KERN_ERR "fcoe_em_config: failed to "
Chris Leech25024982009-08-25 13:59:35 -0700509 "allocate em on interface %s\n", fcoe->netdev->name);
Vasu Dev7f349142009-03-27 09:06:31 -0700510 return -ENOMEM;
Vasu Devd7179682009-07-29 17:05:21 -0700511 }
Vasu Dev7f349142009-03-27 09:06:31 -0700512
513 return 0;
514}
515
516/**
517 * fcoe_if_destroy() - FCoE software HBA tear-down function
Chris Leechaf7f85d2009-08-25 13:59:24 -0700518 * @lport: fc_lport to destroy
Vasu Dev7f349142009-03-27 09:06:31 -0700519 */
Chris Leechaf7f85d2009-08-25 13:59:24 -0700520static void fcoe_if_destroy(struct fc_lport *lport)
Vasu Dev7f349142009-03-27 09:06:31 -0700521{
Chris Leech014f5c32009-08-25 13:59:30 -0700522 struct fcoe_port *port = lport_priv(lport);
523 struct fcoe_interface *fcoe = port->fcoe;
Chris Leech25024982009-08-25 13:59:35 -0700524 struct net_device *netdev = fcoe->netdev;
Vasu Dev7f349142009-03-27 09:06:31 -0700525
Robert Loved5488eb2009-06-10 15:30:59 -0700526 FCOE_NETDEV_DBG(netdev, "Destroying interface\n");
Vasu Dev7f349142009-03-27 09:06:31 -0700527
Vasu Dev7f349142009-03-27 09:06:31 -0700528 /* Logout of the fabric */
Chris Leechaf7f85d2009-08-25 13:59:24 -0700529 fc_fabric_logoff(lport);
Vasu Dev7f349142009-03-27 09:06:31 -0700530
531 /* Remove the instance from fcoe's list */
Chris Leechaf7f85d2009-08-25 13:59:24 -0700532 fcoe_hostlist_remove(lport);
Vasu Dev7f349142009-03-27 09:06:31 -0700533
Vasu Devab6b85c2009-05-17 12:33:08 +0000534 /* clean up netdev configurations */
Chris Leech014f5c32009-08-25 13:59:30 -0700535 fcoe_netdev_cleanup(port);
Vasu Devab6b85c2009-05-17 12:33:08 +0000536
537 /* tear-down the FCoE controller */
Chris Leech014f5c32009-08-25 13:59:30 -0700538 fcoe_ctlr_destroy(&port->ctlr);
Vasu Dev7f349142009-03-27 09:06:31 -0700539
Joe Eykholtf161fb72009-07-29 17:04:17 -0700540 /* Free queued packets for the per-CPU receive threads */
Chris Leechaf7f85d2009-08-25 13:59:24 -0700541 fcoe_percpu_clean(lport);
Joe Eykholtf161fb72009-07-29 17:04:17 -0700542
Vasu Dev7f349142009-03-27 09:06:31 -0700543 /* Cleanup the fc_lport */
Chris Leechaf7f85d2009-08-25 13:59:24 -0700544 fc_lport_destroy(lport);
545 fc_fcp_destroy(lport);
Vasu Dev7f349142009-03-27 09:06:31 -0700546
547 /* Detach from the scsi-ml */
Chris Leechaf7f85d2009-08-25 13:59:24 -0700548 fc_remove_host(lport->host);
549 scsi_remove_host(lport->host);
Vasu Dev7f349142009-03-27 09:06:31 -0700550
551 /* There are no more rports or I/O, free the EM */
Chris Leechaf7f85d2009-08-25 13:59:24 -0700552 fc_exch_mgr_free(lport);
Vasu Dev7f349142009-03-27 09:06:31 -0700553
Vasu Dev7f349142009-03-27 09:06:31 -0700554 /* Free existing skbs */
Chris Leechaf7f85d2009-08-25 13:59:24 -0700555 fcoe_clean_pending_queue(lport);
Vasu Dev7f349142009-03-27 09:06:31 -0700556
Vasu Dev1047f222009-05-06 10:52:40 -0700557 /* Stop the timer */
Chris Leech014f5c32009-08-25 13:59:30 -0700558 del_timer_sync(&port->timer);
Vasu Dev1047f222009-05-06 10:52:40 -0700559
Vasu Dev7f349142009-03-27 09:06:31 -0700560 /* Free memory used by statistical counters */
Chris Leechaf7f85d2009-08-25 13:59:24 -0700561 fc_lport_free_stats(lport);
Vasu Dev7f349142009-03-27 09:06:31 -0700562
563 /* Release the net_device and Scsi_Host */
Vasu Dev1d1b88d2009-07-29 17:05:45 -0700564 dev_put(netdev);
Chris Leechaf7f85d2009-08-25 13:59:24 -0700565 scsi_host_put(lport->host);
Chris Leech014f5c32009-08-25 13:59:30 -0700566 kfree(fcoe); /* TODO, should be refcounted */
Vasu Dev7f349142009-03-27 09:06:31 -0700567}
568
569/*
570 * fcoe_ddp_setup - calls LLD's ddp_setup through net_device
571 * @lp: the corresponding fc_lport
572 * @xid: the exchange id for this ddp transfer
573 * @sgl: the scatterlist describing this transfer
574 * @sgc: number of sg items
575 *
576 * Returns : 0 no ddp
577 */
578static int fcoe_ddp_setup(struct fc_lport *lp, u16 xid,
579 struct scatterlist *sgl, unsigned int sgc)
580{
581 struct net_device *n = fcoe_netdev(lp);
582
583 if (n->netdev_ops && n->netdev_ops->ndo_fcoe_ddp_setup)
584 return n->netdev_ops->ndo_fcoe_ddp_setup(n, xid, sgl, sgc);
585
586 return 0;
587}
588
589/*
590 * fcoe_ddp_done - calls LLD's ddp_done through net_device
591 * @lp: the corresponding fc_lport
592 * @xid: the exchange id for this ddp transfer
593 *
594 * Returns : the length of data that have been completed by ddp
595 */
596static int fcoe_ddp_done(struct fc_lport *lp, u16 xid)
597{
598 struct net_device *n = fcoe_netdev(lp);
599
600 if (n->netdev_ops && n->netdev_ops->ndo_fcoe_ddp_done)
601 return n->netdev_ops->ndo_fcoe_ddp_done(n, xid);
602 return 0;
603}
604
605static struct libfc_function_template fcoe_libfc_fcn_templ = {
606 .frame_send = fcoe_xmit,
607 .ddp_setup = fcoe_ddp_setup,
608 .ddp_done = fcoe_ddp_done,
609};
610
611/**
612 * fcoe_if_create() - this function creates the fcoe interface
613 * @netdev: pointer the associated netdevice
Chris Leechaf7f85d2009-08-25 13:59:24 -0700614 * @parent: device pointer to be the parent in sysfs for the SCSI host
Vasu Dev7f349142009-03-27 09:06:31 -0700615 *
616 * Creates fc_lport struct and scsi_host for lport, configures lport
617 * and starts fabric login.
618 *
Chris Leechaf7f85d2009-08-25 13:59:24 -0700619 * Returns : The allocated fc_lport or an error pointer
Vasu Dev7f349142009-03-27 09:06:31 -0700620 */
Chris Leechaf7f85d2009-08-25 13:59:24 -0700621static struct fc_lport *fcoe_if_create(struct net_device *netdev,
622 struct device *parent)
Vasu Dev7f349142009-03-27 09:06:31 -0700623{
624 int rc;
Chris Leechaf7f85d2009-08-25 13:59:24 -0700625 struct fc_lport *lport = NULL;
Chris Leech014f5c32009-08-25 13:59:30 -0700626 struct fcoe_port *port;
627 struct fcoe_interface *fcoe;
Vasu Dev7f349142009-03-27 09:06:31 -0700628 struct Scsi_Host *shost;
629
Robert Loved5488eb2009-06-10 15:30:59 -0700630 FCOE_NETDEV_DBG(netdev, "Create Interface\n");
Vasu Dev7f349142009-03-27 09:06:31 -0700631
Chris Leech014f5c32009-08-25 13:59:30 -0700632 fcoe = kzalloc(sizeof(*fcoe), GFP_KERNEL);
633 if (!fcoe) {
634 FCOE_NETDEV_DBG(netdev, "Could not allocate fcoe structure\n");
Chris Leechaf7f85d2009-08-25 13:59:24 -0700635 rc = -ENOMEM;
636 goto out;
Vasu Dev7f349142009-03-27 09:06:31 -0700637 }
Chris Leech014f5c32009-08-25 13:59:30 -0700638
639 shost = libfc_host_alloc(&fcoe_shost_template,
640 sizeof(struct fcoe_port));
641 if (!shost) {
642 FCOE_NETDEV_DBG(netdev, "Could not allocate host structure\n");
643 rc = -ENOMEM;
644 goto out_kfree_port;
645 }
Chris Leechaf7f85d2009-08-25 13:59:24 -0700646 lport = shost_priv(shost);
Chris Leech014f5c32009-08-25 13:59:30 -0700647 port = lport_priv(lport);
648
649 port->fcoe = fcoe;
650 fcoe->priv = port;
Vasu Dev7f349142009-03-27 09:06:31 -0700651
652 /* configure fc_lport, e.g., em */
Chris Leechaf7f85d2009-08-25 13:59:24 -0700653 rc = fcoe_lport_config(lport);
Vasu Dev7f349142009-03-27 09:06:31 -0700654 if (rc) {
Robert Loved5488eb2009-06-10 15:30:59 -0700655 FCOE_NETDEV_DBG(netdev, "Could not configure lport for the "
656 "interface\n");
Vasu Dev7f349142009-03-27 09:06:31 -0700657 goto out_host_put;
658 }
659
Joe Eykholt97c83892009-03-17 11:42:40 -0700660 /*
661 * Initialize FIP.
662 */
Chris Leech014f5c32009-08-25 13:59:30 -0700663 fcoe_ctlr_init(&port->ctlr);
664 port->ctlr.send = fcoe_fip_send;
665 port->ctlr.update_mac = fcoe_update_src_mac;
Joe Eykholt97c83892009-03-17 11:42:40 -0700666
Vasu Devab6b85c2009-05-17 12:33:08 +0000667 /* configure lport network properties */
Chris Leechaf7f85d2009-08-25 13:59:24 -0700668 rc = fcoe_netdev_config(lport, netdev);
Vasu Devab6b85c2009-05-17 12:33:08 +0000669 if (rc) {
Robert Loved5488eb2009-06-10 15:30:59 -0700670 FCOE_NETDEV_DBG(netdev, "Could not configure netdev for the "
671 "interface\n");
Vasu Devab6b85c2009-05-17 12:33:08 +0000672 goto out_netdev_cleanup;
673 }
Joe Eykholt97c83892009-03-17 11:42:40 -0700674
Vasu Dev7f349142009-03-27 09:06:31 -0700675 /* configure lport scsi host properties */
Chris Leechaf7f85d2009-08-25 13:59:24 -0700676 rc = fcoe_shost_config(lport, shost, parent);
Vasu Dev7f349142009-03-27 09:06:31 -0700677 if (rc) {
Robert Loved5488eb2009-06-10 15:30:59 -0700678 FCOE_NETDEV_DBG(netdev, "Could not configure shost for the "
679 "interface\n");
Vasu Devab6b85c2009-05-17 12:33:08 +0000680 goto out_netdev_cleanup;
Vasu Dev7f349142009-03-27 09:06:31 -0700681 }
682
Vasu Dev7f349142009-03-27 09:06:31 -0700683 /* Initialize the library */
Chris Leechaf7f85d2009-08-25 13:59:24 -0700684 rc = fcoe_libfc_config(lport, &fcoe_libfc_fcn_templ);
Vasu Dev7f349142009-03-27 09:06:31 -0700685 if (rc) {
Robert Loved5488eb2009-06-10 15:30:59 -0700686 FCOE_NETDEV_DBG(netdev, "Could not configure libfc for the "
687 "interface\n");
Vasu Dev7f349142009-03-27 09:06:31 -0700688 goto out_lp_destroy;
689 }
690
Vasu Deve8af4d42009-07-29 17:05:15 -0700691 /*
692 * fcoe_em_alloc() and fcoe_hostlist_add() both
693 * need to be atomic under fcoe_hostlist_lock
694 * since fcoe_em_alloc() looks for an existing EM
695 * instance on host list updated by fcoe_hostlist_add().
696 */
697 write_lock(&fcoe_hostlist_lock);
Vasu Dev96316092009-07-29 17:05:00 -0700698 /* lport exch manager allocation */
Chris Leechaf7f85d2009-08-25 13:59:24 -0700699 rc = fcoe_em_config(lport);
Vasu Dev96316092009-07-29 17:05:00 -0700700 if (rc) {
701 FCOE_NETDEV_DBG(netdev, "Could not configure the EM for the "
702 "interface\n");
703 goto out_lp_destroy;
704 }
705
Vasu Dev7f349142009-03-27 09:06:31 -0700706 /* add to lports list */
Chris Leechaf7f85d2009-08-25 13:59:24 -0700707 fcoe_hostlist_add(lport);
Vasu Deve8af4d42009-07-29 17:05:15 -0700708 write_unlock(&fcoe_hostlist_lock);
Vasu Dev7f349142009-03-27 09:06:31 -0700709
Chris Leechaf7f85d2009-08-25 13:59:24 -0700710 lport->boot_time = jiffies;
Vasu Dev7f349142009-03-27 09:06:31 -0700711
Chris Leechaf7f85d2009-08-25 13:59:24 -0700712 fc_fabric_login(lport);
Vasu Dev7f349142009-03-27 09:06:31 -0700713
Chris Leechaf7f85d2009-08-25 13:59:24 -0700714 if (!fcoe_link_ok(lport))
Chris Leech014f5c32009-08-25 13:59:30 -0700715 fcoe_ctlr_link_up(&port->ctlr);
Joe Eykholt97c83892009-03-17 11:42:40 -0700716
Vasu Dev7f349142009-03-27 09:06:31 -0700717 dev_hold(netdev);
718
Chris Leechaf7f85d2009-08-25 13:59:24 -0700719 return lport;
Vasu Dev7f349142009-03-27 09:06:31 -0700720
721out_lp_destroy:
Chris Leechaf7f85d2009-08-25 13:59:24 -0700722 fc_exch_mgr_free(lport);
Vasu Devab6b85c2009-05-17 12:33:08 +0000723out_netdev_cleanup:
Chris Leech014f5c32009-08-25 13:59:30 -0700724 fcoe_netdev_cleanup(port);
Vasu Dev7f349142009-03-27 09:06:31 -0700725out_host_put:
Chris Leechaf7f85d2009-08-25 13:59:24 -0700726 scsi_host_put(lport->host);
Chris Leech014f5c32009-08-25 13:59:30 -0700727out_kfree_port:
728 kfree(fcoe);
Chris Leechaf7f85d2009-08-25 13:59:24 -0700729out:
730 return ERR_PTR(rc);
Vasu Dev7f349142009-03-27 09:06:31 -0700731}
732
733/**
734 * fcoe_if_init() - attach to scsi transport
735 *
736 * Returns : 0 on success
737 */
738static int __init fcoe_if_init(void)
739{
740 /* attach to scsi transport */
741 scsi_transport_fcoe_sw =
742 fc_attach_transport(&fcoe_transport_function);
743
744 if (!scsi_transport_fcoe_sw) {
Robert Loved5488eb2009-06-10 15:30:59 -0700745 printk(KERN_ERR "fcoe: Failed to attach to the FC transport\n");
Vasu Dev7f349142009-03-27 09:06:31 -0700746 return -ENODEV;
747 }
748
749 return 0;
750}
751
752/**
753 * fcoe_if_exit() - detach from scsi transport
754 *
755 * Returns : 0 on success
756 */
757int __exit fcoe_if_exit(void)
758{
759 fc_release_transport(scsi_transport_fcoe_sw);
760 return 0;
761}
762
Robert Love85b4aa42008-12-09 15:10:24 -0800763/**
Robert Love8976f422009-03-17 11:41:46 -0700764 * fcoe_percpu_thread_create() - Create a receive thread for an online cpu
765 * @cpu: cpu index for the online cpu
766 */
767static void fcoe_percpu_thread_create(unsigned int cpu)
768{
769 struct fcoe_percpu_s *p;
770 struct task_struct *thread;
771
772 p = &per_cpu(fcoe_percpu, cpu);
773
774 thread = kthread_create(fcoe_percpu_receive_thread,
775 (void *)p, "fcoethread/%d", cpu);
776
777 if (likely(!IS_ERR(p->thread))) {
778 kthread_bind(thread, cpu);
779 wake_up_process(thread);
780
781 spin_lock_bh(&p->fcoe_rx_list.lock);
782 p->thread = thread;
783 spin_unlock_bh(&p->fcoe_rx_list.lock);
784 }
785}
786
787/**
788 * fcoe_percpu_thread_destroy() - removes the rx thread for the given cpu
789 * @cpu: cpu index the rx thread is to be removed
790 *
791 * Destroys a per-CPU Rx thread. Any pending skbs are moved to the
792 * current CPU's Rx thread. If the thread being destroyed is bound to
793 * the CPU processing this context the skbs will be freed.
794 */
795static void fcoe_percpu_thread_destroy(unsigned int cpu)
796{
797 struct fcoe_percpu_s *p;
798 struct task_struct *thread;
799 struct page *crc_eof;
800 struct sk_buff *skb;
801#ifdef CONFIG_SMP
802 struct fcoe_percpu_s *p0;
803 unsigned targ_cpu = smp_processor_id();
804#endif /* CONFIG_SMP */
805
Robert Loved5488eb2009-06-10 15:30:59 -0700806 FCOE_DBG("Destroying receive thread for CPU %d\n", cpu);
Robert Love8976f422009-03-17 11:41:46 -0700807
808 /* Prevent any new skbs from being queued for this CPU. */
809 p = &per_cpu(fcoe_percpu, cpu);
810 spin_lock_bh(&p->fcoe_rx_list.lock);
811 thread = p->thread;
812 p->thread = NULL;
813 crc_eof = p->crc_eof_page;
814 p->crc_eof_page = NULL;
815 p->crc_eof_offset = 0;
816 spin_unlock_bh(&p->fcoe_rx_list.lock);
817
818#ifdef CONFIG_SMP
819 /*
820 * Don't bother moving the skb's if this context is running
821 * on the same CPU that is having its thread destroyed. This
822 * can easily happen when the module is removed.
823 */
824 if (cpu != targ_cpu) {
825 p0 = &per_cpu(fcoe_percpu, targ_cpu);
826 spin_lock_bh(&p0->fcoe_rx_list.lock);
827 if (p0->thread) {
Robert Loved5488eb2009-06-10 15:30:59 -0700828 FCOE_DBG("Moving frames from CPU %d to CPU %d\n",
829 cpu, targ_cpu);
Robert Love8976f422009-03-17 11:41:46 -0700830
831 while ((skb = __skb_dequeue(&p->fcoe_rx_list)) != NULL)
832 __skb_queue_tail(&p0->fcoe_rx_list, skb);
833 spin_unlock_bh(&p0->fcoe_rx_list.lock);
834 } else {
835 /*
836 * The targeted CPU is not initialized and cannot accept
837 * new skbs. Unlock the targeted CPU and drop the skbs
838 * on the CPU that is going offline.
839 */
840 while ((skb = __skb_dequeue(&p->fcoe_rx_list)) != NULL)
841 kfree_skb(skb);
842 spin_unlock_bh(&p0->fcoe_rx_list.lock);
843 }
844 } else {
845 /*
846 * This scenario occurs when the module is being removed
847 * and all threads are being destroyed. skbs will continue
848 * to be shifted from the CPU thread that is being removed
849 * to the CPU thread associated with the CPU that is processing
850 * the module removal. Once there is only one CPU Rx thread it
851 * will reach this case and we will drop all skbs and later
852 * stop the thread.
853 */
854 spin_lock_bh(&p->fcoe_rx_list.lock);
855 while ((skb = __skb_dequeue(&p->fcoe_rx_list)) != NULL)
856 kfree_skb(skb);
857 spin_unlock_bh(&p->fcoe_rx_list.lock);
858 }
859#else
860 /*
Chris Leechdd3fd722009-04-21 16:27:36 -0700861 * This a non-SMP scenario where the singular Rx thread is
Robert Love8976f422009-03-17 11:41:46 -0700862 * being removed. Free all skbs and stop the thread.
863 */
864 spin_lock_bh(&p->fcoe_rx_list.lock);
865 while ((skb = __skb_dequeue(&p->fcoe_rx_list)) != NULL)
866 kfree_skb(skb);
867 spin_unlock_bh(&p->fcoe_rx_list.lock);
868#endif
869
870 if (thread)
871 kthread_stop(thread);
872
873 if (crc_eof)
874 put_page(crc_eof);
875}
876
877/**
878 * fcoe_cpu_callback() - fcoe cpu hotplug event callback
879 * @nfb: callback data block
880 * @action: event triggering the callback
881 * @hcpu: index for the cpu of this event
882 *
883 * This creates or destroys per cpu data for fcoe
884 *
885 * Returns NOTIFY_OK always.
886 */
887static int fcoe_cpu_callback(struct notifier_block *nfb,
888 unsigned long action, void *hcpu)
889{
890 unsigned cpu = (unsigned long)hcpu;
891
892 switch (action) {
893 case CPU_ONLINE:
894 case CPU_ONLINE_FROZEN:
Robert Loved5488eb2009-06-10 15:30:59 -0700895 FCOE_DBG("CPU %x online: Create Rx thread\n", cpu);
Robert Love8976f422009-03-17 11:41:46 -0700896 fcoe_percpu_thread_create(cpu);
897 break;
898 case CPU_DEAD:
899 case CPU_DEAD_FROZEN:
Robert Loved5488eb2009-06-10 15:30:59 -0700900 FCOE_DBG("CPU %x offline: Remove Rx thread\n", cpu);
Robert Love8976f422009-03-17 11:41:46 -0700901 fcoe_percpu_thread_destroy(cpu);
902 break;
903 default:
904 break;
905 }
906 return NOTIFY_OK;
907}
908
909static struct notifier_block fcoe_cpu_notifier = {
910 .notifier_call = fcoe_cpu_callback,
911};
912
913/**
Robert Love34f42a02009-02-27 10:55:45 -0800914 * fcoe_rcv() - this is the fcoe receive function called by NET_RX_SOFTIRQ
Robert Love85b4aa42008-12-09 15:10:24 -0800915 * @skb: the receive skb
916 * @dev: associated net device
917 * @ptype: context
Chris Leechdd3fd722009-04-21 16:27:36 -0700918 * @olddev: last device
Robert Love85b4aa42008-12-09 15:10:24 -0800919 *
920 * this function will receive the packet and build fc frame and pass it up
921 *
922 * Returns: 0 for success
Robert Love34f42a02009-02-27 10:55:45 -0800923 */
Robert Love85b4aa42008-12-09 15:10:24 -0800924int fcoe_rcv(struct sk_buff *skb, struct net_device *dev,
925 struct packet_type *ptype, struct net_device *olddev)
926{
927 struct fc_lport *lp;
928 struct fcoe_rcv_info *fr;
Chris Leech014f5c32009-08-25 13:59:30 -0700929 struct fcoe_port *port;
Robert Love85b4aa42008-12-09 15:10:24 -0800930 struct fc_frame_header *fh;
Robert Love85b4aa42008-12-09 15:10:24 -0800931 struct fcoe_percpu_s *fps;
Vasu Devb2f00912009-08-25 13:58:53 -0700932 unsigned int cpu;
Robert Love85b4aa42008-12-09 15:10:24 -0800933
Chris Leech014f5c32009-08-25 13:59:30 -0700934 port = container_of(ptype, struct fcoe_port, fcoe_packet_type);
935 lp = port->ctlr.lp;
Robert Love85b4aa42008-12-09 15:10:24 -0800936 if (unlikely(lp == NULL)) {
Robert Loved5488eb2009-06-10 15:30:59 -0700937 FCOE_NETDEV_DBG(dev, "Cannot find hba structure");
Robert Love85b4aa42008-12-09 15:10:24 -0800938 goto err2;
939 }
Joe Eykholt97c83892009-03-17 11:42:40 -0700940 if (!lp->link_up)
941 goto err2;
Robert Love85b4aa42008-12-09 15:10:24 -0800942
Robert Loved5488eb2009-06-10 15:30:59 -0700943 FCOE_NETDEV_DBG(dev, "skb_info: len:%d data_len:%d head:%p "
944 "data:%p tail:%p end:%p sum:%d dev:%s",
945 skb->len, skb->data_len, skb->head, skb->data,
946 skb_tail_pointer(skb), skb_end_pointer(skb),
947 skb->csum, skb->dev ? skb->dev->name : "<NULL>");
Robert Love85b4aa42008-12-09 15:10:24 -0800948
949 /* check for FCOE packet type */
950 if (unlikely(eth_hdr(skb)->h_proto != htons(ETH_P_FCOE))) {
Robert Loved5488eb2009-06-10 15:30:59 -0700951 FCOE_NETDEV_DBG(dev, "Wrong FC type frame");
Robert Love85b4aa42008-12-09 15:10:24 -0800952 goto err;
953 }
954
955 /*
956 * Check for minimum frame length, and make sure required FCoE
957 * and FC headers are pulled into the linear data area.
958 */
959 if (unlikely((skb->len < FCOE_MIN_FRAME) ||
960 !pskb_may_pull(skb, FCOE_HEADER_LEN)))
961 goto err;
962
963 skb_set_transport_header(skb, sizeof(struct fcoe_hdr));
964 fh = (struct fc_frame_header *) skb_transport_header(skb);
965
Robert Love85b4aa42008-12-09 15:10:24 -0800966 fr = fcoe_dev_from_skb(skb);
967 fr->fr_dev = lp;
968 fr->ptype = ptype;
Robert Love5e5e92d2009-03-17 11:41:35 -0700969
Robert Love85b4aa42008-12-09 15:10:24 -0800970 /*
Vasu Devb2f00912009-08-25 13:58:53 -0700971 * In case the incoming frame's exchange is originated from
972 * the initiator, then received frame's exchange id is ANDed
973 * with fc_cpu_mask bits to get the same cpu on which exchange
974 * was originated, otherwise just use the current cpu.
Robert Love85b4aa42008-12-09 15:10:24 -0800975 */
Vasu Devb2f00912009-08-25 13:58:53 -0700976 if (ntoh24(fh->fh_f_ctl) & FC_FC_EX_CTX)
977 cpu = ntohs(fh->fh_ox_id) & fc_cpu_mask;
978 else
979 cpu = smp_processor_id();
Robert Love5e5e92d2009-03-17 11:41:35 -0700980
Robert Love8976f422009-03-17 11:41:46 -0700981 fps = &per_cpu(fcoe_percpu, cpu);
Robert Love85b4aa42008-12-09 15:10:24 -0800982 spin_lock_bh(&fps->fcoe_rx_list.lock);
Robert Love8976f422009-03-17 11:41:46 -0700983 if (unlikely(!fps->thread)) {
984 /*
985 * The targeted CPU is not ready, let's target
986 * the first CPU now. For non-SMP systems this
987 * will check the same CPU twice.
988 */
Robert Loved5488eb2009-06-10 15:30:59 -0700989 FCOE_NETDEV_DBG(dev, "CPU is online, but no receive thread "
990 "ready for incoming skb- using first online "
991 "CPU.\n");
Robert Love8976f422009-03-17 11:41:46 -0700992
993 spin_unlock_bh(&fps->fcoe_rx_list.lock);
994 cpu = first_cpu(cpu_online_map);
995 fps = &per_cpu(fcoe_percpu, cpu);
996 spin_lock_bh(&fps->fcoe_rx_list.lock);
997 if (!fps->thread) {
998 spin_unlock_bh(&fps->fcoe_rx_list.lock);
999 goto err;
1000 }
1001 }
1002
1003 /*
1004 * We now have a valid CPU that we're targeting for
1005 * this skb. We also have this receive thread locked,
1006 * so we're free to queue skbs into it's queue.
1007 */
Robert Love85b4aa42008-12-09 15:10:24 -08001008 __skb_queue_tail(&fps->fcoe_rx_list, skb);
1009 if (fps->fcoe_rx_list.qlen == 1)
1010 wake_up_process(fps->thread);
1011
1012 spin_unlock_bh(&fps->fcoe_rx_list.lock);
1013
1014 return 0;
1015err:
Robert Love582b45b2009-03-31 15:51:50 -07001016 fc_lport_get_stats(lp)->ErrorFrames++;
Robert Love85b4aa42008-12-09 15:10:24 -08001017
1018err2:
1019 kfree_skb(skb);
1020 return -1;
1021}
Robert Love85b4aa42008-12-09 15:10:24 -08001022
1023/**
Robert Love34f42a02009-02-27 10:55:45 -08001024 * fcoe_start_io() - pass to netdev to start xmit for fcoe
Robert Love85b4aa42008-12-09 15:10:24 -08001025 * @skb: the skb to be xmitted
1026 *
1027 * Returns: 0 for success
Robert Love34f42a02009-02-27 10:55:45 -08001028 */
Robert Love85b4aa42008-12-09 15:10:24 -08001029static inline int fcoe_start_io(struct sk_buff *skb)
1030{
1031 int rc;
1032
1033 skb_get(skb);
1034 rc = dev_queue_xmit(skb);
1035 if (rc != 0)
1036 return rc;
1037 kfree_skb(skb);
1038 return 0;
1039}
1040
1041/**
Chris Leechdd3fd722009-04-21 16:27:36 -07001042 * fcoe_get_paged_crc_eof() - in case we need to alloc a page for crc_eof
Robert Love85b4aa42008-12-09 15:10:24 -08001043 * @skb: the skb to be xmitted
1044 * @tlen: total len
1045 *
1046 * Returns: 0 for success
Robert Love34f42a02009-02-27 10:55:45 -08001047 */
Robert Love85b4aa42008-12-09 15:10:24 -08001048static int fcoe_get_paged_crc_eof(struct sk_buff *skb, int tlen)
1049{
1050 struct fcoe_percpu_s *fps;
1051 struct page *page;
Robert Love85b4aa42008-12-09 15:10:24 -08001052
Robert Love5e5e92d2009-03-17 11:41:35 -07001053 fps = &get_cpu_var(fcoe_percpu);
Robert Love85b4aa42008-12-09 15:10:24 -08001054 page = fps->crc_eof_page;
1055 if (!page) {
1056 page = alloc_page(GFP_ATOMIC);
1057 if (!page) {
Robert Love5e5e92d2009-03-17 11:41:35 -07001058 put_cpu_var(fcoe_percpu);
Robert Love85b4aa42008-12-09 15:10:24 -08001059 return -ENOMEM;
1060 }
1061 fps->crc_eof_page = page;
Robert Love8976f422009-03-17 11:41:46 -07001062 fps->crc_eof_offset = 0;
Robert Love85b4aa42008-12-09 15:10:24 -08001063 }
1064
1065 get_page(page);
1066 skb_fill_page_desc(skb, skb_shinfo(skb)->nr_frags, page,
1067 fps->crc_eof_offset, tlen);
1068 skb->len += tlen;
1069 skb->data_len += tlen;
1070 skb->truesize += tlen;
1071 fps->crc_eof_offset += sizeof(struct fcoe_crc_eof);
1072
1073 if (fps->crc_eof_offset >= PAGE_SIZE) {
1074 fps->crc_eof_page = NULL;
1075 fps->crc_eof_offset = 0;
1076 put_page(page);
1077 }
Robert Love5e5e92d2009-03-17 11:41:35 -07001078 put_cpu_var(fcoe_percpu);
Robert Love85b4aa42008-12-09 15:10:24 -08001079 return 0;
1080}
1081
1082/**
Robert Love34f42a02009-02-27 10:55:45 -08001083 * fcoe_fc_crc() - calculates FC CRC in this fcoe skb
Chris Leechdd3fd722009-04-21 16:27:36 -07001084 * @fp: the fc_frame containing data to be checksummed
Robert Love85b4aa42008-12-09 15:10:24 -08001085 *
Chris Leech014f5c32009-08-25 13:59:30 -07001086 * This uses crc32() to calculate the crc for port frame
Robert Love85b4aa42008-12-09 15:10:24 -08001087 * Return : 32 bit crc
Robert Love34f42a02009-02-27 10:55:45 -08001088 */
Robert Love85b4aa42008-12-09 15:10:24 -08001089u32 fcoe_fc_crc(struct fc_frame *fp)
1090{
1091 struct sk_buff *skb = fp_skb(fp);
1092 struct skb_frag_struct *frag;
1093 unsigned char *data;
1094 unsigned long off, len, clen;
1095 u32 crc;
1096 unsigned i;
1097
1098 crc = crc32(~0, skb->data, skb_headlen(skb));
1099
1100 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
1101 frag = &skb_shinfo(skb)->frags[i];
1102 off = frag->page_offset;
1103 len = frag->size;
1104 while (len > 0) {
1105 clen = min(len, PAGE_SIZE - (off & ~PAGE_MASK));
1106 data = kmap_atomic(frag->page + (off >> PAGE_SHIFT),
1107 KM_SKB_DATA_SOFTIRQ);
1108 crc = crc32(crc, data + (off & ~PAGE_MASK), clen);
1109 kunmap_atomic(data, KM_SKB_DATA_SOFTIRQ);
1110 off += clen;
1111 len -= clen;
1112 }
1113 }
1114 return crc;
1115}
Robert Love85b4aa42008-12-09 15:10:24 -08001116
1117/**
Robert Love34f42a02009-02-27 10:55:45 -08001118 * fcoe_xmit() - FCoE frame transmit function
Chris Leech014f5c32009-08-25 13:59:30 -07001119 * @lp: the associated local fcoe
Robert Love85b4aa42008-12-09 15:10:24 -08001120 * @fp: the fc_frame to be transmitted
1121 *
1122 * Return : 0 for success
Robert Love34f42a02009-02-27 10:55:45 -08001123 */
Robert Love85b4aa42008-12-09 15:10:24 -08001124int fcoe_xmit(struct fc_lport *lp, struct fc_frame *fp)
1125{
Vasu Dev4bb6b512009-05-06 10:52:34 -07001126 int wlen;
Robert Love85b4aa42008-12-09 15:10:24 -08001127 u32 crc;
1128 struct ethhdr *eh;
1129 struct fcoe_crc_eof *cp;
1130 struct sk_buff *skb;
1131 struct fcoe_dev_stats *stats;
1132 struct fc_frame_header *fh;
1133 unsigned int hlen; /* header length implies the version */
1134 unsigned int tlen; /* trailer length */
1135 unsigned int elen; /* eth header, may include vlan */
Chris Leech014f5c32009-08-25 13:59:30 -07001136 struct fcoe_port *port;
Robert Love85b4aa42008-12-09 15:10:24 -08001137 u8 sof, eof;
1138 struct fcoe_hdr *hp;
1139
1140 WARN_ON((fr_len(fp) % sizeof(u32)) != 0);
1141
Chris Leech014f5c32009-08-25 13:59:30 -07001142 port = lport_priv(lp);
Robert Love85b4aa42008-12-09 15:10:24 -08001143 fh = fc_frame_header_get(fp);
Joe Eykholt97c83892009-03-17 11:42:40 -07001144 skb = fp_skb(fp);
1145 wlen = skb->len / FCOE_WORD_TO_BYTE;
1146
1147 if (!lp->link_up) {
Dan Carpenter3caf02e2009-04-21 16:27:25 -07001148 kfree_skb(skb);
Joe Eykholt97c83892009-03-17 11:42:40 -07001149 return 0;
Robert Love85b4aa42008-12-09 15:10:24 -08001150 }
1151
Joe Eykholt97c83892009-03-17 11:42:40 -07001152 if (unlikely(fh->fh_r_ctl == FC_RCTL_ELS_REQ) &&
Chris Leech014f5c32009-08-25 13:59:30 -07001153 fcoe_ctlr_els_send(&port->ctlr, skb))
Joe Eykholt97c83892009-03-17 11:42:40 -07001154 return 0;
1155
Robert Love85b4aa42008-12-09 15:10:24 -08001156 sof = fr_sof(fp);
1157 eof = fr_eof(fp);
1158
Vasu Dev4e57e1c2009-05-06 10:52:46 -07001159 elen = sizeof(struct ethhdr);
Robert Love85b4aa42008-12-09 15:10:24 -08001160 hlen = sizeof(struct fcoe_hdr);
1161 tlen = sizeof(struct fcoe_crc_eof);
1162 wlen = (skb->len - tlen + sizeof(crc)) / FCOE_WORD_TO_BYTE;
1163
1164 /* crc offload */
1165 if (likely(lp->crc_offload)) {
Yi Zou39ca9a02009-02-27 14:07:15 -08001166 skb->ip_summed = CHECKSUM_PARTIAL;
Robert Love85b4aa42008-12-09 15:10:24 -08001167 skb->csum_start = skb_headroom(skb);
1168 skb->csum_offset = skb->len;
1169 crc = 0;
1170 } else {
1171 skb->ip_summed = CHECKSUM_NONE;
1172 crc = fcoe_fc_crc(fp);
1173 }
1174
Chris Leech014f5c32009-08-25 13:59:30 -07001175 /* copy port crc and eof to the skb buff */
Robert Love85b4aa42008-12-09 15:10:24 -08001176 if (skb_is_nonlinear(skb)) {
1177 skb_frag_t *frag;
1178 if (fcoe_get_paged_crc_eof(skb, tlen)) {
Roel Kluine9041582009-02-27 10:56:22 -08001179 kfree_skb(skb);
Robert Love85b4aa42008-12-09 15:10:24 -08001180 return -ENOMEM;
1181 }
1182 frag = &skb_shinfo(skb)->frags[skb_shinfo(skb)->nr_frags - 1];
1183 cp = kmap_atomic(frag->page, KM_SKB_DATA_SOFTIRQ)
1184 + frag->page_offset;
1185 } else {
1186 cp = (struct fcoe_crc_eof *)skb_put(skb, tlen);
1187 }
1188
1189 memset(cp, 0, sizeof(*cp));
1190 cp->fcoe_eof = eof;
1191 cp->fcoe_crc32 = cpu_to_le32(~crc);
1192
1193 if (skb_is_nonlinear(skb)) {
1194 kunmap_atomic(cp, KM_SKB_DATA_SOFTIRQ);
1195 cp = NULL;
1196 }
1197
Chris Leech014f5c32009-08-25 13:59:30 -07001198 /* adjust skb network/transport offsets to match mac/fcoe/port */
Robert Love85b4aa42008-12-09 15:10:24 -08001199 skb_push(skb, elen + hlen);
1200 skb_reset_mac_header(skb);
1201 skb_reset_network_header(skb);
1202 skb->mac_len = elen;
Yi Zou211c7382009-02-27 14:06:37 -08001203 skb->protocol = htons(ETH_P_FCOE);
Chris Leech25024982009-08-25 13:59:35 -07001204 skb->dev = port->fcoe->netdev;
Robert Love85b4aa42008-12-09 15:10:24 -08001205
1206 /* fill up mac and fcoe headers */
1207 eh = eth_hdr(skb);
1208 eh->h_proto = htons(ETH_P_FCOE);
Chris Leech014f5c32009-08-25 13:59:30 -07001209 if (port->ctlr.map_dest)
Robert Love85b4aa42008-12-09 15:10:24 -08001210 fc_fcoe_set_mac(eh->h_dest, fh->fh_d_id);
1211 else
1212 /* insert GW address */
Chris Leech014f5c32009-08-25 13:59:30 -07001213 memcpy(eh->h_dest, port->ctlr.dest_addr, ETH_ALEN);
Robert Love85b4aa42008-12-09 15:10:24 -08001214
Chris Leech014f5c32009-08-25 13:59:30 -07001215 if (unlikely(port->ctlr.flogi_oxid != FC_XID_UNKNOWN))
1216 memcpy(eh->h_source, port->ctlr.ctl_src_addr, ETH_ALEN);
Robert Love85b4aa42008-12-09 15:10:24 -08001217 else
Chris Leech014f5c32009-08-25 13:59:30 -07001218 memcpy(eh->h_source, port->ctlr.data_src_addr, ETH_ALEN);
Robert Love85b4aa42008-12-09 15:10:24 -08001219
1220 hp = (struct fcoe_hdr *)(eh + 1);
1221 memset(hp, 0, sizeof(*hp));
1222 if (FC_FCOE_VER)
1223 FC_FCOE_ENCAPS_VER(hp, FC_FCOE_VER);
1224 hp->fcoe_sof = sof;
1225
Yi Zou39ca9a02009-02-27 14:07:15 -08001226 /* fcoe lso, mss is in max_payload which is non-zero for FCP data */
1227 if (lp->seq_offload && fr_max_payload(fp)) {
1228 skb_shinfo(skb)->gso_type = SKB_GSO_FCOE;
1229 skb_shinfo(skb)->gso_size = fr_max_payload(fp);
1230 } else {
1231 skb_shinfo(skb)->gso_type = 0;
1232 skb_shinfo(skb)->gso_size = 0;
1233 }
Robert Love85b4aa42008-12-09 15:10:24 -08001234 /* update tx stats: regardless if LLD fails */
Robert Love582b45b2009-03-31 15:51:50 -07001235 stats = fc_lport_get_stats(lp);
1236 stats->TxFrames++;
1237 stats->TxWords += wlen;
Robert Love85b4aa42008-12-09 15:10:24 -08001238
1239 /* send down to lld */
1240 fr_dev(fp) = lp;
Chris Leech014f5c32009-08-25 13:59:30 -07001241 if (port->fcoe_pending_queue.qlen)
Vasu Dev4bb6b512009-05-06 10:52:34 -07001242 fcoe_check_wait_queue(lp, skb);
1243 else if (fcoe_start_io(skb))
1244 fcoe_check_wait_queue(lp, skb);
Robert Love85b4aa42008-12-09 15:10:24 -08001245
1246 return 0;
1247}
Robert Love85b4aa42008-12-09 15:10:24 -08001248
Robert Love34f42a02009-02-27 10:55:45 -08001249/**
1250 * fcoe_percpu_receive_thread() - recv thread per cpu
Robert Love85b4aa42008-12-09 15:10:24 -08001251 * @arg: ptr to the fcoe per cpu struct
1252 *
1253 * Return: 0 for success
Robert Love85b4aa42008-12-09 15:10:24 -08001254 */
1255int fcoe_percpu_receive_thread(void *arg)
1256{
1257 struct fcoe_percpu_s *p = arg;
1258 u32 fr_len;
1259 struct fc_lport *lp;
1260 struct fcoe_rcv_info *fr;
1261 struct fcoe_dev_stats *stats;
1262 struct fc_frame_header *fh;
1263 struct sk_buff *skb;
1264 struct fcoe_crc_eof crc_eof;
1265 struct fc_frame *fp;
1266 u8 *mac = NULL;
Chris Leech014f5c32009-08-25 13:59:30 -07001267 struct fcoe_port *port;
Robert Love85b4aa42008-12-09 15:10:24 -08001268 struct fcoe_hdr *hp;
1269
Robert Love4469c192009-02-27 10:56:38 -08001270 set_user_nice(current, -20);
Robert Love85b4aa42008-12-09 15:10:24 -08001271
1272 while (!kthread_should_stop()) {
1273
1274 spin_lock_bh(&p->fcoe_rx_list.lock);
1275 while ((skb = __skb_dequeue(&p->fcoe_rx_list)) == NULL) {
1276 set_current_state(TASK_INTERRUPTIBLE);
1277 spin_unlock_bh(&p->fcoe_rx_list.lock);
1278 schedule();
1279 set_current_state(TASK_RUNNING);
1280 if (kthread_should_stop())
1281 return 0;
1282 spin_lock_bh(&p->fcoe_rx_list.lock);
1283 }
1284 spin_unlock_bh(&p->fcoe_rx_list.lock);
1285 fr = fcoe_dev_from_skb(skb);
1286 lp = fr->fr_dev;
1287 if (unlikely(lp == NULL)) {
Robert Loved5488eb2009-06-10 15:30:59 -07001288 FCOE_NETDEV_DBG(skb->dev, "Invalid HBA Structure");
Robert Love85b4aa42008-12-09 15:10:24 -08001289 kfree_skb(skb);
1290 continue;
1291 }
1292
Robert Loved5488eb2009-06-10 15:30:59 -07001293 FCOE_NETDEV_DBG(skb->dev, "skb_info: len:%d data_len:%d "
1294 "head:%p data:%p tail:%p end:%p sum:%d dev:%s",
1295 skb->len, skb->data_len,
1296 skb->head, skb->data, skb_tail_pointer(skb),
1297 skb_end_pointer(skb), skb->csum,
1298 skb->dev ? skb->dev->name : "<NULL>");
Robert Love85b4aa42008-12-09 15:10:24 -08001299
1300 /*
1301 * Save source MAC address before discarding header.
1302 */
Chris Leech014f5c32009-08-25 13:59:30 -07001303 port = lport_priv(lp);
Robert Love85b4aa42008-12-09 15:10:24 -08001304 if (skb_is_nonlinear(skb))
1305 skb_linearize(skb); /* not ideal */
Joe Eykholt97c83892009-03-17 11:42:40 -07001306 mac = eth_hdr(skb)->h_source;
Robert Love85b4aa42008-12-09 15:10:24 -08001307
1308 /*
1309 * Frame length checks and setting up the header pointers
1310 * was done in fcoe_rcv already.
1311 */
1312 hp = (struct fcoe_hdr *) skb_network_header(skb);
1313 fh = (struct fc_frame_header *) skb_transport_header(skb);
1314
Robert Love582b45b2009-03-31 15:51:50 -07001315 stats = fc_lport_get_stats(lp);
Robert Love85b4aa42008-12-09 15:10:24 -08001316 if (unlikely(FC_FCOE_DECAPS_VER(hp) != FC_FCOE_VER)) {
Robert Love582b45b2009-03-31 15:51:50 -07001317 if (stats->ErrorFrames < 5)
Robert Loved5488eb2009-06-10 15:30:59 -07001318 printk(KERN_WARNING "fcoe: FCoE version "
Robert Love582b45b2009-03-31 15:51:50 -07001319 "mismatch: The frame has "
1320 "version %x, but the "
1321 "initiator supports version "
1322 "%x\n", FC_FCOE_DECAPS_VER(hp),
1323 FC_FCOE_VER);
1324 stats->ErrorFrames++;
Robert Love85b4aa42008-12-09 15:10:24 -08001325 kfree_skb(skb);
1326 continue;
1327 }
1328
1329 skb_pull(skb, sizeof(struct fcoe_hdr));
1330 fr_len = skb->len - sizeof(struct fcoe_crc_eof);
1331
Robert Love582b45b2009-03-31 15:51:50 -07001332 stats->RxFrames++;
1333 stats->RxWords += fr_len / FCOE_WORD_TO_BYTE;
Robert Love85b4aa42008-12-09 15:10:24 -08001334
1335 fp = (struct fc_frame *)skb;
1336 fc_frame_init(fp);
1337 fr_dev(fp) = lp;
1338 fr_sof(fp) = hp->fcoe_sof;
1339
1340 /* Copy out the CRC and EOF trailer for access */
1341 if (skb_copy_bits(skb, fr_len, &crc_eof, sizeof(crc_eof))) {
1342 kfree_skb(skb);
1343 continue;
1344 }
1345 fr_eof(fp) = crc_eof.fcoe_eof;
1346 fr_crc(fp) = crc_eof.fcoe_crc32;
1347 if (pskb_trim(skb, fr_len)) {
1348 kfree_skb(skb);
1349 continue;
1350 }
1351
1352 /*
1353 * We only check CRC if no offload is available and if it is
1354 * it's solicited data, in which case, the FCP layer would
1355 * check it during the copy.
1356 */
Yi Zou07c00ec2009-02-27 14:07:31 -08001357 if (lp->crc_offload && skb->ip_summed == CHECKSUM_UNNECESSARY)
Robert Love85b4aa42008-12-09 15:10:24 -08001358 fr_flags(fp) &= ~FCPHF_CRC_UNCHECKED;
1359 else
1360 fr_flags(fp) |= FCPHF_CRC_UNCHECKED;
1361
1362 fh = fc_frame_header_get(fp);
1363 if (fh->fh_r_ctl == FC_RCTL_DD_SOL_DATA &&
1364 fh->fh_type == FC_TYPE_FCP) {
Vasu Dev52ff8782009-07-29 17:05:10 -07001365 fc_exch_recv(lp, fp);
Robert Love85b4aa42008-12-09 15:10:24 -08001366 continue;
1367 }
1368 if (fr_flags(fp) & FCPHF_CRC_UNCHECKED) {
1369 if (le32_to_cpu(fr_crc(fp)) !=
1370 ~crc32(~0, skb->data, fr_len)) {
Robert Loved5488eb2009-06-10 15:30:59 -07001371 if (stats->InvalidCRCCount < 5)
Robert Love85b4aa42008-12-09 15:10:24 -08001372 printk(KERN_WARNING "fcoe: dropping "
1373 "frame with CRC error\n");
1374 stats->InvalidCRCCount++;
1375 stats->ErrorFrames++;
1376 fc_frame_free(fp);
1377 continue;
1378 }
1379 fr_flags(fp) &= ~FCPHF_CRC_UNCHECKED;
1380 }
Chris Leech014f5c32009-08-25 13:59:30 -07001381 if (unlikely(port->ctlr.flogi_oxid != FC_XID_UNKNOWN) &&
1382 fcoe_ctlr_recv_flogi(&port->ctlr, fp, mac)) {
Joe Eykholt97c83892009-03-17 11:42:40 -07001383 fc_frame_free(fp);
1384 continue;
1385 }
Vasu Dev52ff8782009-07-29 17:05:10 -07001386 fc_exch_recv(lp, fp);
Robert Love85b4aa42008-12-09 15:10:24 -08001387 }
1388 return 0;
1389}
1390
1391/**
Chris Leechdd3fd722009-04-21 16:27:36 -07001392 * fcoe_check_wait_queue() - attempt to clear the transmit backlog
1393 * @lp: the fc_lport
Robert Love85b4aa42008-12-09 15:10:24 -08001394 *
1395 * This empties the wait_queue, dequeue the head of the wait_queue queue
1396 * and calls fcoe_start_io() for each packet, if all skb have been
Vasu Devc826a312009-02-27 10:56:27 -08001397 * transmitted, return qlen or -1 if a error occurs, then restore
Chris Leechdd3fd722009-04-21 16:27:36 -07001398 * wait_queue and try again later.
Robert Love85b4aa42008-12-09 15:10:24 -08001399 *
1400 * The wait_queue is used when the skb transmit fails. skb will go
Chris Leechdd3fd722009-04-21 16:27:36 -07001401 * in the wait_queue which will be emptied by the timer function or
Robert Love85b4aa42008-12-09 15:10:24 -08001402 * by the next skb transmit.
Robert Love34f42a02009-02-27 10:55:45 -08001403 */
Vasu Dev4bb6b512009-05-06 10:52:34 -07001404static void fcoe_check_wait_queue(struct fc_lport *lp, struct sk_buff *skb)
Robert Love85b4aa42008-12-09 15:10:24 -08001405{
Chris Leech014f5c32009-08-25 13:59:30 -07001406 struct fcoe_port *port = lport_priv(lp);
Vasu Dev4bb6b512009-05-06 10:52:34 -07001407 int rc;
Robert Love85b4aa42008-12-09 15:10:24 -08001408
Chris Leech014f5c32009-08-25 13:59:30 -07001409 spin_lock_bh(&port->fcoe_pending_queue.lock);
Vasu Dev4bb6b512009-05-06 10:52:34 -07001410
1411 if (skb)
Chris Leech014f5c32009-08-25 13:59:30 -07001412 __skb_queue_tail(&port->fcoe_pending_queue, skb);
Vasu Dev4bb6b512009-05-06 10:52:34 -07001413
Chris Leech014f5c32009-08-25 13:59:30 -07001414 if (port->fcoe_pending_queue_active)
Vasu Devc826a312009-02-27 10:56:27 -08001415 goto out;
Chris Leech014f5c32009-08-25 13:59:30 -07001416 port->fcoe_pending_queue_active = 1;
Chris Leech55c8baf2009-02-27 10:56:32 -08001417
Chris Leech014f5c32009-08-25 13:59:30 -07001418 while (port->fcoe_pending_queue.qlen) {
Chris Leech55c8baf2009-02-27 10:56:32 -08001419 /* keep qlen > 0 until fcoe_start_io succeeds */
Chris Leech014f5c32009-08-25 13:59:30 -07001420 port->fcoe_pending_queue.qlen++;
1421 skb = __skb_dequeue(&port->fcoe_pending_queue);
Chris Leech55c8baf2009-02-27 10:56:32 -08001422
Chris Leech014f5c32009-08-25 13:59:30 -07001423 spin_unlock_bh(&port->fcoe_pending_queue.lock);
Chris Leech55c8baf2009-02-27 10:56:32 -08001424 rc = fcoe_start_io(skb);
Chris Leech014f5c32009-08-25 13:59:30 -07001425 spin_lock_bh(&port->fcoe_pending_queue.lock);
Chris Leech55c8baf2009-02-27 10:56:32 -08001426
1427 if (rc) {
Chris Leech014f5c32009-08-25 13:59:30 -07001428 __skb_queue_head(&port->fcoe_pending_queue, skb);
Chris Leech55c8baf2009-02-27 10:56:32 -08001429 /* undo temporary increment above */
Chris Leech014f5c32009-08-25 13:59:30 -07001430 port->fcoe_pending_queue.qlen--;
Chris Leech55c8baf2009-02-27 10:56:32 -08001431 break;
Robert Love85b4aa42008-12-09 15:10:24 -08001432 }
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--;
Robert Love85b4aa42008-12-09 15:10:24 -08001435 }
Chris Leech55c8baf2009-02-27 10:56:32 -08001436
Chris Leech014f5c32009-08-25 13:59:30 -07001437 if (port->fcoe_pending_queue.qlen < FCOE_LOW_QUEUE_DEPTH)
Chris Leech55c8baf2009-02-27 10:56:32 -08001438 lp->qfull = 0;
Chris Leech014f5c32009-08-25 13:59:30 -07001439 if (port->fcoe_pending_queue.qlen && !timer_pending(&port->timer))
1440 mod_timer(&port->timer, jiffies + 2);
1441 port->fcoe_pending_queue_active = 0;
Vasu Devc826a312009-02-27 10:56:27 -08001442out:
Chris Leech014f5c32009-08-25 13:59:30 -07001443 if (port->fcoe_pending_queue.qlen > FCOE_MAX_QUEUE_DEPTH)
Vasu Dev4bb6b512009-05-06 10:52:34 -07001444 lp->qfull = 1;
Chris Leech014f5c32009-08-25 13:59:30 -07001445 spin_unlock_bh(&port->fcoe_pending_queue.lock);
Vasu Dev4bb6b512009-05-06 10:52:34 -07001446 return;
Robert Love85b4aa42008-12-09 15:10:24 -08001447}
1448
1449/**
Robert Love34f42a02009-02-27 10:55:45 -08001450 * fcoe_dev_setup() - setup link change notification interface
1451 */
Randy Dunlapb0d428a2009-04-27 21:49:31 -07001452static void fcoe_dev_setup(void)
Robert Love85b4aa42008-12-09 15:10:24 -08001453{
Robert Love85b4aa42008-12-09 15:10:24 -08001454 register_netdevice_notifier(&fcoe_notifier);
1455}
1456
1457/**
Randy Dunlapb0d428a2009-04-27 21:49:31 -07001458 * fcoe_dev_cleanup() - cleanup link change notification interface
Robert Love34f42a02009-02-27 10:55:45 -08001459 */
Robert Love85b4aa42008-12-09 15:10:24 -08001460static void fcoe_dev_cleanup(void)
1461{
1462 unregister_netdevice_notifier(&fcoe_notifier);
1463}
1464
1465/**
Robert Love34f42a02009-02-27 10:55:45 -08001466 * fcoe_device_notification() - netdev event notification callback
Robert Love85b4aa42008-12-09 15:10:24 -08001467 * @notifier: context of the notification
1468 * @event: type of event
1469 * @ptr: fixed array for output parsed ifname
1470 *
1471 * This function is called by the ethernet driver in case of link change event
1472 *
1473 * Returns: 0 for success
Robert Love34f42a02009-02-27 10:55:45 -08001474 */
Robert Love85b4aa42008-12-09 15:10:24 -08001475static int fcoe_device_notification(struct notifier_block *notifier,
1476 ulong event, void *ptr)
1477{
1478 struct fc_lport *lp = NULL;
Vasu Dev1d1b88d2009-07-29 17:05:45 -07001479 struct net_device *netdev = ptr;
Chris Leech014f5c32009-08-25 13:59:30 -07001480 struct fcoe_interface *fcoe;
1481 struct fcoe_port *port = NULL;
Robert Love85b4aa42008-12-09 15:10:24 -08001482 struct fcoe_dev_stats *stats;
Joe Eykholt97c83892009-03-17 11:42:40 -07001483 u32 link_possible = 1;
Robert Love85b4aa42008-12-09 15:10:24 -08001484 u32 mfs;
1485 int rc = NOTIFY_OK;
1486
1487 read_lock(&fcoe_hostlist_lock);
Chris Leech014f5c32009-08-25 13:59:30 -07001488 list_for_each_entry(fcoe, &fcoe_hostlist, list) {
1489 port = fcoe->priv;
Chris Leech25024982009-08-25 13:59:35 -07001490 if (fcoe->netdev == netdev) {
Chris Leech014f5c32009-08-25 13:59:30 -07001491 lp = port->ctlr.lp;
Robert Love85b4aa42008-12-09 15:10:24 -08001492 break;
1493 }
1494 }
1495 read_unlock(&fcoe_hostlist_lock);
1496 if (lp == NULL) {
1497 rc = NOTIFY_DONE;
1498 goto out;
1499 }
1500
Robert Love85b4aa42008-12-09 15:10:24 -08001501 switch (event) {
1502 case NETDEV_DOWN:
1503 case NETDEV_GOING_DOWN:
Joe Eykholt97c83892009-03-17 11:42:40 -07001504 link_possible = 0;
Robert Love85b4aa42008-12-09 15:10:24 -08001505 break;
1506 case NETDEV_UP:
1507 case NETDEV_CHANGE:
Robert Love85b4aa42008-12-09 15:10:24 -08001508 break;
1509 case NETDEV_CHANGEMTU:
Vasu Dev1d1b88d2009-07-29 17:05:45 -07001510 mfs = netdev->mtu - (sizeof(struct fcoe_hdr) +
1511 sizeof(struct fcoe_crc_eof));
Robert Love85b4aa42008-12-09 15:10:24 -08001512 if (mfs >= FC_MIN_MAX_FRAME)
1513 fc_set_mfs(lp, mfs);
Robert Love85b4aa42008-12-09 15:10:24 -08001514 break;
1515 case NETDEV_REGISTER:
1516 break;
1517 default:
Vasu Dev1d1b88d2009-07-29 17:05:45 -07001518 FCOE_NETDEV_DBG(netdev, "Unknown event %ld "
Robert Loved5488eb2009-06-10 15:30:59 -07001519 "from netdev netlink\n", event);
Robert Love85b4aa42008-12-09 15:10:24 -08001520 }
Joe Eykholt97c83892009-03-17 11:42:40 -07001521 if (link_possible && !fcoe_link_ok(lp))
Chris Leech014f5c32009-08-25 13:59:30 -07001522 fcoe_ctlr_link_up(&port->ctlr);
1523 else if (fcoe_ctlr_link_down(&port->ctlr)) {
Joe Eykholt97c83892009-03-17 11:42:40 -07001524 stats = fc_lport_get_stats(lp);
1525 stats->LinkFailureCount++;
1526 fcoe_clean_pending_queue(lp);
Robert Love85b4aa42008-12-09 15:10:24 -08001527 }
1528out:
1529 return rc;
1530}
1531
1532/**
Robert Love34f42a02009-02-27 10:55:45 -08001533 * fcoe_if_to_netdev() - parse a name buffer to get netdev
Robert Love85b4aa42008-12-09 15:10:24 -08001534 * @buffer: incoming buffer to be copied
1535 *
Chris Leechdd3fd722009-04-21 16:27:36 -07001536 * Returns: NULL or ptr to net_device
Robert Love34f42a02009-02-27 10:55:45 -08001537 */
Robert Love85b4aa42008-12-09 15:10:24 -08001538static struct net_device *fcoe_if_to_netdev(const char *buffer)
1539{
1540 char *cp;
1541 char ifname[IFNAMSIZ + 2];
1542
1543 if (buffer) {
1544 strlcpy(ifname, buffer, IFNAMSIZ);
1545 cp = ifname + strlen(ifname);
1546 while (--cp >= ifname && *cp == '\n')
1547 *cp = '\0';
1548 return dev_get_by_name(&init_net, ifname);
1549 }
1550 return NULL;
1551}
1552
1553/**
Chris Leechdd3fd722009-04-21 16:27:36 -07001554 * fcoe_netdev_to_module_owner() - finds out the driver module of the netdev
Robert Love85b4aa42008-12-09 15:10:24 -08001555 * @netdev: the target netdev
1556 *
1557 * Returns: ptr to the struct module, NULL for failure
Robert Love34f42a02009-02-27 10:55:45 -08001558 */
Robert Loveb2ab99c2009-02-27 10:55:50 -08001559static struct module *
1560fcoe_netdev_to_module_owner(const struct net_device *netdev)
Robert Love85b4aa42008-12-09 15:10:24 -08001561{
1562 struct device *dev;
1563
1564 if (!netdev)
1565 return NULL;
1566
1567 dev = netdev->dev.parent;
1568 if (!dev)
1569 return NULL;
1570
1571 if (!dev->driver)
1572 return NULL;
1573
1574 return dev->driver->owner;
1575}
1576
1577/**
Robert Love34f42a02009-02-27 10:55:45 -08001578 * fcoe_ethdrv_get() - Hold the Ethernet driver
Robert Love85b4aa42008-12-09 15:10:24 -08001579 * @netdev: the target netdev
1580 *
Robert Love34f42a02009-02-27 10:55:45 -08001581 * Holds the Ethernet driver module by try_module_get() for
1582 * the corresponding netdev.
1583 *
Chris Leechdd3fd722009-04-21 16:27:36 -07001584 * Returns: 0 for success
Robert Love34f42a02009-02-27 10:55:45 -08001585 */
Robert Love85b4aa42008-12-09 15:10:24 -08001586static int fcoe_ethdrv_get(const struct net_device *netdev)
1587{
1588 struct module *owner;
1589
1590 owner = fcoe_netdev_to_module_owner(netdev);
1591 if (owner) {
Robert Loved5488eb2009-06-10 15:30:59 -07001592 FCOE_NETDEV_DBG(netdev, "Hold driver module %s\n",
1593 module_name(owner));
Robert Love85b4aa42008-12-09 15:10:24 -08001594 return try_module_get(owner);
1595 }
1596 return -ENODEV;
1597}
1598
1599/**
Robert Love34f42a02009-02-27 10:55:45 -08001600 * fcoe_ethdrv_put() - Release the Ethernet driver
Robert Love85b4aa42008-12-09 15:10:24 -08001601 * @netdev: the target netdev
1602 *
Robert Love34f42a02009-02-27 10:55:45 -08001603 * Releases the Ethernet driver module by module_put for
1604 * the corresponding netdev.
1605 *
Chris Leechdd3fd722009-04-21 16:27:36 -07001606 * Returns: 0 for success
Robert Love34f42a02009-02-27 10:55:45 -08001607 */
Robert Love85b4aa42008-12-09 15:10:24 -08001608static int fcoe_ethdrv_put(const struct net_device *netdev)
1609{
1610 struct module *owner;
1611
1612 owner = fcoe_netdev_to_module_owner(netdev);
1613 if (owner) {
Robert Loved5488eb2009-06-10 15:30:59 -07001614 FCOE_NETDEV_DBG(netdev, "Release driver module %s\n",
1615 module_name(owner));
Robert Love85b4aa42008-12-09 15:10:24 -08001616 module_put(owner);
1617 return 0;
1618 }
1619 return -ENODEV;
1620}
1621
1622/**
Robert Love34f42a02009-02-27 10:55:45 -08001623 * fcoe_destroy() - handles the destroy from sysfs
Chris Leechdd3fd722009-04-21 16:27:36 -07001624 * @buffer: expected to be an eth if name
Robert Love85b4aa42008-12-09 15:10:24 -08001625 * @kp: associated kernel param
1626 *
1627 * Returns: 0 for success
Robert Love34f42a02009-02-27 10:55:45 -08001628 */
Robert Love85b4aa42008-12-09 15:10:24 -08001629static int fcoe_destroy(const char *buffer, struct kernel_param *kp)
1630{
Robert Love85b4aa42008-12-09 15:10:24 -08001631 struct net_device *netdev;
Chris Leechaf7f85d2009-08-25 13:59:24 -07001632 struct fc_lport *lport;
1633 int rc;
Robert Love85b4aa42008-12-09 15:10:24 -08001634
1635 netdev = fcoe_if_to_netdev(buffer);
1636 if (!netdev) {
1637 rc = -ENODEV;
1638 goto out_nodev;
1639 }
1640 /* look for existing lport */
Chris Leechaf7f85d2009-08-25 13:59:24 -07001641 lport = fcoe_hostlist_lookup(netdev);
1642 if (!lport) {
Robert Love85b4aa42008-12-09 15:10:24 -08001643 rc = -ENODEV;
1644 goto out_putdev;
1645 }
Chris Leechaf7f85d2009-08-25 13:59:24 -07001646 fcoe_if_destroy(lport);
Robert Love85b4aa42008-12-09 15:10:24 -08001647 fcoe_ethdrv_put(netdev);
1648 rc = 0;
1649out_putdev:
1650 dev_put(netdev);
1651out_nodev:
1652 return rc;
1653}
1654
1655/**
Robert Love34f42a02009-02-27 10:55:45 -08001656 * fcoe_create() - Handles the create call from sysfs
Chris Leechdd3fd722009-04-21 16:27:36 -07001657 * @buffer: expected to be an eth if name
Robert Love85b4aa42008-12-09 15:10:24 -08001658 * @kp: associated kernel param
1659 *
1660 * Returns: 0 for success
Robert Love34f42a02009-02-27 10:55:45 -08001661 */
Robert Love85b4aa42008-12-09 15:10:24 -08001662static int fcoe_create(const char *buffer, struct kernel_param *kp)
1663{
1664 int rc;
Chris Leechaf7f85d2009-08-25 13:59:24 -07001665 struct fc_lport *lport;
Robert Love85b4aa42008-12-09 15:10:24 -08001666 struct net_device *netdev;
1667
1668 netdev = fcoe_if_to_netdev(buffer);
1669 if (!netdev) {
1670 rc = -ENODEV;
1671 goto out_nodev;
1672 }
1673 /* look for existing lport */
1674 if (fcoe_hostlist_lookup(netdev)) {
1675 rc = -EEXIST;
1676 goto out_putdev;
1677 }
1678 fcoe_ethdrv_get(netdev);
1679
Chris Leechaf7f85d2009-08-25 13:59:24 -07001680 lport = fcoe_if_create(netdev, &netdev->dev);
1681 if (IS_ERR(lport)) {
Robert Loved5488eb2009-06-10 15:30:59 -07001682 printk(KERN_ERR "fcoe: Failed to create interface (%s)\n",
Robert Love85b4aa42008-12-09 15:10:24 -08001683 netdev->name);
1684 fcoe_ethdrv_put(netdev);
1685 rc = -EIO;
1686 goto out_putdev;
1687 }
1688 rc = 0;
1689out_putdev:
1690 dev_put(netdev);
1691out_nodev:
1692 return rc;
1693}
1694
1695module_param_call(create, fcoe_create, NULL, NULL, S_IWUSR);
1696__MODULE_PARM_TYPE(create, "string");
Chris Leech014f5c32009-08-25 13:59:30 -07001697MODULE_PARM_DESC(create, "Create fcoe fcoe using net device passed in.");
Robert Love85b4aa42008-12-09 15:10:24 -08001698module_param_call(destroy, fcoe_destroy, NULL, NULL, S_IWUSR);
1699__MODULE_PARM_TYPE(destroy, "string");
Chris Leech014f5c32009-08-25 13:59:30 -07001700MODULE_PARM_DESC(destroy, "Destroy fcoe fcoe");
Robert Love85b4aa42008-12-09 15:10:24 -08001701
Robert Love34f42a02009-02-27 10:55:45 -08001702/**
1703 * fcoe_link_ok() - Check if link is ok for the fc_lport
Robert Love85b4aa42008-12-09 15:10:24 -08001704 * @lp: ptr to the fc_lport
1705 *
1706 * Any permanently-disqualifying conditions have been previously checked.
1707 * This also updates the speed setting, which may change with link for 100/1000.
1708 *
1709 * This function should probably be checking for PAUSE support at some point
1710 * in the future. Currently Per-priority-pause is not determinable using
1711 * ethtool, so we shouldn't be restrictive until that problem is resolved.
1712 *
1713 * Returns: 0 if link is OK for use by FCoE.
1714 *
1715 */
1716int fcoe_link_ok(struct fc_lport *lp)
1717{
Chris Leech014f5c32009-08-25 13:59:30 -07001718 struct fcoe_port *port = lport_priv(lp);
Chris Leech25024982009-08-25 13:59:35 -07001719 struct net_device *dev = port->fcoe->netdev;
Robert Love85b4aa42008-12-09 15:10:24 -08001720 struct ethtool_cmd ecmd = { ETHTOOL_GSET };
Robert Love85b4aa42008-12-09 15:10:24 -08001721
Yi Zou2f718d62009-07-29 17:04:01 -07001722 if ((dev->flags & IFF_UP) && netif_carrier_ok(dev) &&
1723 (!dev_ethtool_get_settings(dev, &ecmd))) {
1724 lp->link_supported_speeds &=
1725 ~(FC_PORTSPEED_1GBIT | FC_PORTSPEED_10GBIT);
1726 if (ecmd.supported & (SUPPORTED_1000baseT_Half |
1727 SUPPORTED_1000baseT_Full))
1728 lp->link_supported_speeds |= FC_PORTSPEED_1GBIT;
1729 if (ecmd.supported & SUPPORTED_10000baseT_Full)
1730 lp->link_supported_speeds |=
1731 FC_PORTSPEED_10GBIT;
1732 if (ecmd.speed == SPEED_1000)
1733 lp->link_speed = FC_PORTSPEED_1GBIT;
1734 if (ecmd.speed == SPEED_10000)
1735 lp->link_speed = FC_PORTSPEED_10GBIT;
Robert Love85b4aa42008-12-09 15:10:24 -08001736
Yi Zou2f718d62009-07-29 17:04:01 -07001737 return 0;
1738 }
1739 return -1;
Robert Love85b4aa42008-12-09 15:10:24 -08001740}
Robert Love85b4aa42008-12-09 15:10:24 -08001741
Robert Love34f42a02009-02-27 10:55:45 -08001742/**
1743 * fcoe_percpu_clean() - Clear the pending skbs for an lport
Robert Love85b4aa42008-12-09 15:10:24 -08001744 * @lp: the fc_lport
1745 */
1746void fcoe_percpu_clean(struct fc_lport *lp)
1747{
Robert Love85b4aa42008-12-09 15:10:24 -08001748 struct fcoe_percpu_s *pp;
1749 struct fcoe_rcv_info *fr;
1750 struct sk_buff_head *list;
1751 struct sk_buff *skb, *next;
1752 struct sk_buff *head;
Robert Love5e5e92d2009-03-17 11:41:35 -07001753 unsigned int cpu;
Robert Love85b4aa42008-12-09 15:10:24 -08001754
Robert Love5e5e92d2009-03-17 11:41:35 -07001755 for_each_possible_cpu(cpu) {
1756 pp = &per_cpu(fcoe_percpu, cpu);
1757 spin_lock_bh(&pp->fcoe_rx_list.lock);
1758 list = &pp->fcoe_rx_list;
1759 head = list->next;
1760 for (skb = head; skb != (struct sk_buff *)list;
1761 skb = next) {
1762 next = skb->next;
1763 fr = fcoe_dev_from_skb(skb);
1764 if (fr->fr_dev == lp) {
1765 __skb_unlink(skb, list);
1766 kfree_skb(skb);
Robert Love85b4aa42008-12-09 15:10:24 -08001767 }
Robert Love85b4aa42008-12-09 15:10:24 -08001768 }
Robert Love5e5e92d2009-03-17 11:41:35 -07001769 spin_unlock_bh(&pp->fcoe_rx_list.lock);
Robert Love85b4aa42008-12-09 15:10:24 -08001770 }
1771}
Robert Love85b4aa42008-12-09 15:10:24 -08001772
1773/**
Robert Love34f42a02009-02-27 10:55:45 -08001774 * fcoe_clean_pending_queue() - Dequeue a skb and free it
Robert Love85b4aa42008-12-09 15:10:24 -08001775 * @lp: the corresponding fc_lport
1776 *
1777 * Returns: none
Robert Love34f42a02009-02-27 10:55:45 -08001778 */
Robert Love85b4aa42008-12-09 15:10:24 -08001779void fcoe_clean_pending_queue(struct fc_lport *lp)
1780{
Chris Leech014f5c32009-08-25 13:59:30 -07001781 struct fcoe_port *port = lport_priv(lp);
Robert Love85b4aa42008-12-09 15:10:24 -08001782 struct sk_buff *skb;
1783
Chris Leech014f5c32009-08-25 13:59:30 -07001784 spin_lock_bh(&port->fcoe_pending_queue.lock);
1785 while ((skb = __skb_dequeue(&port->fcoe_pending_queue)) != NULL) {
1786 spin_unlock_bh(&port->fcoe_pending_queue.lock);
Robert Love85b4aa42008-12-09 15:10:24 -08001787 kfree_skb(skb);
Chris Leech014f5c32009-08-25 13:59:30 -07001788 spin_lock_bh(&port->fcoe_pending_queue.lock);
Robert Love85b4aa42008-12-09 15:10:24 -08001789 }
Chris Leech014f5c32009-08-25 13:59:30 -07001790 spin_unlock_bh(&port->fcoe_pending_queue.lock);
Robert Love85b4aa42008-12-09 15:10:24 -08001791}
Robert Love85b4aa42008-12-09 15:10:24 -08001792
1793/**
Robert Love34f42a02009-02-27 10:55:45 -08001794 * fcoe_reset() - Resets the fcoe
Robert Love85b4aa42008-12-09 15:10:24 -08001795 * @shost: shost the reset is from
1796 *
1797 * Returns: always 0
1798 */
1799int fcoe_reset(struct Scsi_Host *shost)
1800{
1801 struct fc_lport *lport = shost_priv(shost);
1802 fc_lport_reset(lport);
1803 return 0;
1804}
Robert Love85b4aa42008-12-09 15:10:24 -08001805
Robert Love34f42a02009-02-27 10:55:45 -08001806/**
Chris Leech014f5c32009-08-25 13:59:30 -07001807 * fcoe_hostlist_lookup_port() - find the corresponding lport by a given device
Chris Leechdd3fd722009-04-21 16:27:36 -07001808 * @dev: this is currently ptr to net_device
Robert Love85b4aa42008-12-09 15:10:24 -08001809 *
Vasu Deve8af4d42009-07-29 17:05:15 -07001810 * Called with fcoe_hostlist_lock held.
1811 *
Chris Leech014f5c32009-08-25 13:59:30 -07001812 * Returns: NULL or the located fcoe_port
Robert Love85b4aa42008-12-09 15:10:24 -08001813 */
Chris Leech014f5c32009-08-25 13:59:30 -07001814static struct fcoe_interface *
1815fcoe_hostlist_lookup_port(const struct net_device *dev)
Robert Love85b4aa42008-12-09 15:10:24 -08001816{
Chris Leech014f5c32009-08-25 13:59:30 -07001817 struct fcoe_interface *fcoe;
Robert Love85b4aa42008-12-09 15:10:24 -08001818
Chris Leech014f5c32009-08-25 13:59:30 -07001819 list_for_each_entry(fcoe, &fcoe_hostlist, list) {
Chris Leech25024982009-08-25 13:59:35 -07001820 if (fcoe->netdev == dev)
Chris Leech014f5c32009-08-25 13:59:30 -07001821 return fcoe;
Robert Love85b4aa42008-12-09 15:10:24 -08001822 }
Robert Love85b4aa42008-12-09 15:10:24 -08001823 return NULL;
1824}
1825
Robert Love34f42a02009-02-27 10:55:45 -08001826/**
1827 * fcoe_hostlist_lookup() - Find the corresponding lport by netdev
Robert Love85b4aa42008-12-09 15:10:24 -08001828 * @netdev: ptr to net_device
1829 *
1830 * Returns: 0 for success
1831 */
1832struct fc_lport *fcoe_hostlist_lookup(const struct net_device *netdev)
1833{
Chris Leech014f5c32009-08-25 13:59:30 -07001834 struct fcoe_interface *fcoe;
Robert Love85b4aa42008-12-09 15:10:24 -08001835
Vasu Deve8af4d42009-07-29 17:05:15 -07001836 read_lock(&fcoe_hostlist_lock);
Chris Leech014f5c32009-08-25 13:59:30 -07001837 fcoe = fcoe_hostlist_lookup_port(netdev);
Vasu Deve8af4d42009-07-29 17:05:15 -07001838 read_unlock(&fcoe_hostlist_lock);
Robert Love85b4aa42008-12-09 15:10:24 -08001839
Chris Leech014f5c32009-08-25 13:59:30 -07001840 return (fcoe) ? fcoe->priv->ctlr.lp : NULL;
Robert Love85b4aa42008-12-09 15:10:24 -08001841}
Robert Love85b4aa42008-12-09 15:10:24 -08001842
Robert Love34f42a02009-02-27 10:55:45 -08001843/**
1844 * fcoe_hostlist_add() - Add a lport to lports list
Chris Leechdd3fd722009-04-21 16:27:36 -07001845 * @lp: ptr to the fc_lport to be added
Robert Love85b4aa42008-12-09 15:10:24 -08001846 *
Vasu Deve8af4d42009-07-29 17:05:15 -07001847 * Called with write fcoe_hostlist_lock held.
1848 *
Robert Love85b4aa42008-12-09 15:10:24 -08001849 * Returns: 0 for success
1850 */
Chris Leech014f5c32009-08-25 13:59:30 -07001851int fcoe_hostlist_add(const struct fc_lport *lport)
Robert Love85b4aa42008-12-09 15:10:24 -08001852{
Chris Leech014f5c32009-08-25 13:59:30 -07001853 struct fcoe_interface *fcoe;
1854 struct fcoe_port *port;
Robert Love85b4aa42008-12-09 15:10:24 -08001855
Chris Leech014f5c32009-08-25 13:59:30 -07001856 fcoe = fcoe_hostlist_lookup_port(fcoe_netdev(lport));
1857 if (!fcoe) {
1858 port = lport_priv(lport);
1859 fcoe = port->fcoe;
1860 list_add_tail(&fcoe->list, &fcoe_hostlist);
Robert Love85b4aa42008-12-09 15:10:24 -08001861 }
1862 return 0;
1863}
Robert Love85b4aa42008-12-09 15:10:24 -08001864
Robert Love34f42a02009-02-27 10:55:45 -08001865/**
1866 * fcoe_hostlist_remove() - remove a lport from lports list
Chris Leechdd3fd722009-04-21 16:27:36 -07001867 * @lp: ptr to the fc_lport to be removed
Robert Love85b4aa42008-12-09 15:10:24 -08001868 *
1869 * Returns: 0 for success
1870 */
Chris Leech014f5c32009-08-25 13:59:30 -07001871int fcoe_hostlist_remove(const struct fc_lport *lport)
Robert Love85b4aa42008-12-09 15:10:24 -08001872{
Chris Leech014f5c32009-08-25 13:59:30 -07001873 struct fcoe_interface *fcoe;
Robert Love85b4aa42008-12-09 15:10:24 -08001874
Vasu Deve8af4d42009-07-29 17:05:15 -07001875 write_lock_bh(&fcoe_hostlist_lock);
Chris Leech014f5c32009-08-25 13:59:30 -07001876 fcoe = fcoe_hostlist_lookup_port(fcoe_netdev(lport));
1877 BUG_ON(!fcoe);
1878 list_del(&fcoe->list);
Robert Love85b4aa42008-12-09 15:10:24 -08001879 write_unlock_bh(&fcoe_hostlist_lock);
1880
1881 return 0;
1882}
Robert Love85b4aa42008-12-09 15:10:24 -08001883
1884/**
Robert Love34f42a02009-02-27 10:55:45 -08001885 * fcoe_init() - fcoe module loading initialization
Robert Love85b4aa42008-12-09 15:10:24 -08001886 *
Robert Love85b4aa42008-12-09 15:10:24 -08001887 * Returns 0 on success, negative on failure
Robert Love34f42a02009-02-27 10:55:45 -08001888 */
Robert Love85b4aa42008-12-09 15:10:24 -08001889static int __init fcoe_init(void)
1890{
Robert Love38eccab2009-03-17 11:41:30 -07001891 unsigned int cpu;
Robert Love8976f422009-03-17 11:41:46 -07001892 int rc = 0;
Robert Love85b4aa42008-12-09 15:10:24 -08001893 struct fcoe_percpu_s *p;
1894
Robert Love38eccab2009-03-17 11:41:30 -07001895 for_each_possible_cpu(cpu) {
Robert Love5e5e92d2009-03-17 11:41:35 -07001896 p = &per_cpu(fcoe_percpu, cpu);
Robert Love38eccab2009-03-17 11:41:30 -07001897 skb_queue_head_init(&p->fcoe_rx_list);
1898 }
1899
Robert Love8976f422009-03-17 11:41:46 -07001900 for_each_online_cpu(cpu)
1901 fcoe_percpu_thread_create(cpu);
Robert Love85b4aa42008-12-09 15:10:24 -08001902
Robert Love8976f422009-03-17 11:41:46 -07001903 /* Initialize per CPU interrupt thread */
1904 rc = register_hotcpu_notifier(&fcoe_cpu_notifier);
1905 if (rc)
1906 goto out_free;
Robert Love85b4aa42008-12-09 15:10:24 -08001907
Robert Love8976f422009-03-17 11:41:46 -07001908 /* Setup link change notification */
Robert Love85b4aa42008-12-09 15:10:24 -08001909 fcoe_dev_setup();
1910
Chris Leech5892c322009-08-25 13:59:14 -07001911 rc = fcoe_if_init();
1912 if (rc)
1913 goto out_free;
Robert Love85b4aa42008-12-09 15:10:24 -08001914
1915 return 0;
Robert Love8976f422009-03-17 11:41:46 -07001916
1917out_free:
1918 for_each_online_cpu(cpu) {
1919 fcoe_percpu_thread_destroy(cpu);
1920 }
1921
1922 return rc;
Robert Love85b4aa42008-12-09 15:10:24 -08001923}
1924module_init(fcoe_init);
1925
1926/**
Robert Love34f42a02009-02-27 10:55:45 -08001927 * fcoe_exit() - fcoe module unloading cleanup
Robert Love85b4aa42008-12-09 15:10:24 -08001928 *
1929 * Returns 0 on success, negative on failure
Robert Love34f42a02009-02-27 10:55:45 -08001930 */
Robert Love85b4aa42008-12-09 15:10:24 -08001931static void __exit fcoe_exit(void)
1932{
Robert Love5e5e92d2009-03-17 11:41:35 -07001933 unsigned int cpu;
Chris Leech014f5c32009-08-25 13:59:30 -07001934 struct fcoe_interface *fcoe, *tmp;
Robert Love85b4aa42008-12-09 15:10:24 -08001935
Robert Love85b4aa42008-12-09 15:10:24 -08001936 fcoe_dev_cleanup();
1937
Vasu Dev5919a592009-03-27 09:03:29 -07001938 /* releases the associated fcoe hosts */
Chris Leech014f5c32009-08-25 13:59:30 -07001939 list_for_each_entry_safe(fcoe, tmp, &fcoe_hostlist, list)
1940 fcoe_if_destroy(fcoe->priv->ctlr.lp);
Robert Love85b4aa42008-12-09 15:10:24 -08001941
Robert Love8976f422009-03-17 11:41:46 -07001942 unregister_hotcpu_notifier(&fcoe_cpu_notifier);
1943
Chris Leech014f5c32009-08-25 13:59:30 -07001944 for_each_online_cpu(cpu)
Robert Love8976f422009-03-17 11:41:46 -07001945 fcoe_percpu_thread_destroy(cpu);
Robert Love85b4aa42008-12-09 15:10:24 -08001946
Vasu Dev7f349142009-03-27 09:06:31 -07001947 /* detach from scsi transport */
1948 fcoe_if_exit();
Robert Love85b4aa42008-12-09 15:10:24 -08001949}
1950module_exit(fcoe_exit);