blob: a39d370bccf85034b082c999abdea19bb998ecb7 [file] [log] [blame]
Robert Love85b4aa42008-12-09 15:10:24 -08001/*
2 * Copyright(c) 2007 - 2008 Intel Corporation. All rights reserved.
3 *
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms and conditions of the GNU General Public License,
6 * version 2, as published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope it will be useful, but WITHOUT
9 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
10 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
11 * more details.
12 *
13 * You should have received a copy of the GNU General Public License along with
14 * this program; if not, write to the Free Software Foundation, Inc.,
15 * 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
16 *
17 * Maintained at www.Open-FCoE.org
18 */
19
20#include <linux/module.h>
21#include <linux/version.h>
Robert Love85b4aa42008-12-09 15:10:24 -080022#include <linux/spinlock.h>
Robert Love85b4aa42008-12-09 15:10:24 -080023#include <linux/netdevice.h>
24#include <linux/etherdevice.h>
25#include <linux/ethtool.h>
26#include <linux/if_ether.h>
27#include <linux/if_vlan.h>
Robert Love85b4aa42008-12-09 15:10:24 -080028#include <linux/crc32.h>
29#include <linux/cpu.h>
30#include <linux/fs.h>
31#include <linux/sysfs.h>
32#include <linux/ctype.h>
33#include <scsi/scsi_tcq.h>
34#include <scsi/scsicam.h>
35#include <scsi/scsi_transport.h>
36#include <scsi/scsi_transport_fc.h>
37#include <net/rtnetlink.h>
38
39#include <scsi/fc/fc_encaps.h>
Joe Eykholt97c83892009-03-17 11:42:40 -070040#include <scsi/fc/fc_fip.h>
Robert Love85b4aa42008-12-09 15:10:24 -080041
42#include <scsi/libfc.h>
43#include <scsi/fc_frame.h>
44#include <scsi/libfcoe.h>
Robert Love85b4aa42008-12-09 15:10:24 -080045
Vasu Devfdd78022009-03-17 11:42:24 -070046#include "fcoe.h"
47
Robert Love85b4aa42008-12-09 15:10:24 -080048MODULE_AUTHOR("Open-FCoE.org");
49MODULE_DESCRIPTION("FCoE");
Vasu Dev9b34ecf2009-03-17 11:42:13 -070050MODULE_LICENSE("GPL v2");
Robert Love85b4aa42008-12-09 15:10:24 -080051
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{
154 struct fcoe_softc *fc;
155
156 fc = container_of(ptype, struct fcoe_softc, fip_packet_type);
157 fcoe_ctlr_recv(&fc->ctlr, skb);
158 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{
Vasu Dev1d1b88d2009-07-29 17:05:45 -0700168 skb->dev = fcoe_from_ctlr(fip)->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{
183 struct fcoe_softc *fc;
184
185 fc = fcoe_from_ctlr(fip);
186 rtnl_lock();
187 if (!is_zero_ether_addr(old))
Vasu Dev1d1b88d2009-07-29 17:05:45 -0700188 dev_unicast_delete(fc->netdev, old);
189 dev_unicast_add(fc->netdev, new);
Vasu Devab6b85c2009-05-17 12:33:08 +0000190 rtnl_unlock();
191}
192
193/**
Vasu Dev7f349142009-03-27 09:06:31 -0700194 * fcoe_lport_config() - sets up the fc_lport
195 * @lp: ptr to the fc_lport
Vasu Dev7f349142009-03-27 09:06:31 -0700196 *
197 * Returns: 0 for success
198 */
199static int fcoe_lport_config(struct fc_lport *lp)
200{
201 lp->link_up = 0;
202 lp->qfull = 0;
203 lp->max_retry_count = 3;
Abhijeet Joglekara3666952009-05-01 10:01:26 -0700204 lp->max_rport_retry_count = 3;
Vasu Dev7f349142009-03-27 09:06:31 -0700205 lp->e_d_tov = 2 * 1000; /* FC-FS default */
206 lp->r_a_tov = 2 * 2 * 1000;
207 lp->service_params = (FCP_SPPF_INIT_FCN | FCP_SPPF_RD_XRDY_DIS |
208 FCP_SPPF_RETRY | FCP_SPPF_CONF_COMPL);
209
210 fc_lport_init_stats(lp);
211
212 /* lport fc_lport related configuration */
213 fc_lport_config(lp);
214
215 /* offload related configuration */
216 lp->crc_offload = 0;
217 lp->seq_offload = 0;
218 lp->lro_enabled = 0;
219 lp->lro_xid = 0;
220 lp->lso_max = 0;
221
222 return 0;
223}
224
225/**
Vasu Devab6b85c2009-05-17 12:33:08 +0000226 * fcoe_netdev_cleanup() - clean up netdev configurations
227 * @fc: ptr to the fcoe_softc
228 */
229void fcoe_netdev_cleanup(struct fcoe_softc *fc)
230{
231 u8 flogi_maddr[ETH_ALEN];
232
233 /* Don't listen for Ethernet packets anymore */
234 dev_remove_pack(&fc->fcoe_packet_type);
235 dev_remove_pack(&fc->fip_packet_type);
236
237 /* Delete secondary MAC addresses */
238 rtnl_lock();
239 memcpy(flogi_maddr, (u8[6]) FC_FCOE_FLOGI_MAC, ETH_ALEN);
Vasu Dev1d1b88d2009-07-29 17:05:45 -0700240 dev_unicast_delete(fc->netdev, flogi_maddr);
Vasu Devab6b85c2009-05-17 12:33:08 +0000241 if (!is_zero_ether_addr(fc->ctlr.data_src_addr))
Vasu Dev1d1b88d2009-07-29 17:05:45 -0700242 dev_unicast_delete(fc->netdev, fc->ctlr.data_src_addr);
Vasu Dev184dd342009-05-17 12:33:28 +0000243 if (fc->ctlr.spma)
Vasu Dev1d1b88d2009-07-29 17:05:45 -0700244 dev_unicast_delete(fc->netdev, fc->ctlr.ctl_src_addr);
245 dev_mc_delete(fc->netdev, FIP_ALL_ENODE_MACS, ETH_ALEN, 0);
Vasu Devab6b85c2009-05-17 12:33:08 +0000246 rtnl_unlock();
247}
248
249/**
Vasu Dev1047f222009-05-06 10:52:40 -0700250 * fcoe_queue_timer() - fcoe queue timer
251 * @lp: the fc_lport pointer
252 *
253 * Calls fcoe_check_wait_queue on timeout
254 *
255 */
256static void fcoe_queue_timer(ulong lp)
257{
258 fcoe_check_wait_queue((struct fc_lport *)lp, NULL);
259}
260
261/**
Vasu Dev7f349142009-03-27 09:06:31 -0700262 * fcoe_netdev_config() - Set up netdev for SW FCoE
263 * @lp : ptr to the fc_lport
264 * @netdev : ptr to the associated netdevice struct
265 *
266 * Must be called after fcoe_lport_config() as it will use lport mutex
267 *
268 * Returns : 0 for success
269 */
270static int fcoe_netdev_config(struct fc_lport *lp, struct net_device *netdev)
271{
272 u32 mfs;
273 u64 wwnn, wwpn;
274 struct fcoe_softc *fc;
275 u8 flogi_maddr[ETH_ALEN];
Vasu Dev184dd342009-05-17 12:33:28 +0000276 struct netdev_hw_addr *ha;
Vasu Dev7f349142009-03-27 09:06:31 -0700277
278 /* Setup lport private data to point to fcoe softc */
279 fc = lport_priv(lp);
Joe Eykholt97c83892009-03-17 11:42:40 -0700280 fc->ctlr.lp = lp;
Vasu Dev1d1b88d2009-07-29 17:05:45 -0700281 fc->netdev = netdev;
Vasu Dev7f349142009-03-27 09:06:31 -0700282
283 /* Do not support for bonding device */
Vasu Dev1d1b88d2009-07-29 17:05:45 -0700284 if ((netdev->priv_flags & IFF_MASTER_ALB) ||
285 (netdev->priv_flags & IFF_SLAVE_INACTIVE) ||
286 (netdev->priv_flags & IFF_MASTER_8023AD)) {
Vasu Dev7f349142009-03-27 09:06:31 -0700287 return -EOPNOTSUPP;
288 }
289
290 /*
291 * Determine max frame size based on underlying device and optional
292 * user-configured limit. If the MFS is too low, fcoe_link_ok()
293 * will return 0, so do this first.
294 */
Vasu Dev1d1b88d2009-07-29 17:05:45 -0700295 mfs = netdev->mtu - (sizeof(struct fcoe_hdr) +
296 sizeof(struct fcoe_crc_eof));
Vasu Dev7f349142009-03-27 09:06:31 -0700297 if (fc_set_mfs(lp, mfs))
298 return -EINVAL;
299
Vasu Dev7f349142009-03-27 09:06:31 -0700300 /* offload features support */
Vasu Dev1d1b88d2009-07-29 17:05:45 -0700301 if (netdev->features & NETIF_F_SG)
Vasu Dev7f349142009-03-27 09:06:31 -0700302 lp->sg_supp = 1;
303
Vasu Dev7f349142009-03-27 09:06:31 -0700304 if (netdev->features & NETIF_F_FCOE_CRC) {
305 lp->crc_offload = 1;
Robert Loved5488eb2009-06-10 15:30:59 -0700306 FCOE_NETDEV_DBG(netdev, "Supports FCCRC offload\n");
Vasu Dev7f349142009-03-27 09:06:31 -0700307 }
Vasu Dev7f349142009-03-27 09:06:31 -0700308 if (netdev->features & NETIF_F_FSO) {
309 lp->seq_offload = 1;
310 lp->lso_max = netdev->gso_max_size;
Robert Loved5488eb2009-06-10 15:30:59 -0700311 FCOE_NETDEV_DBG(netdev, "Supports LSO for max len 0x%x\n",
312 lp->lso_max);
Vasu Dev7f349142009-03-27 09:06:31 -0700313 }
Vasu Dev7f349142009-03-27 09:06:31 -0700314 if (netdev->fcoe_ddp_xid) {
315 lp->lro_enabled = 1;
316 lp->lro_xid = netdev->fcoe_ddp_xid;
Robert Loved5488eb2009-06-10 15:30:59 -0700317 FCOE_NETDEV_DBG(netdev, "Supports LRO for max xid 0x%x\n",
318 lp->lro_xid);
Vasu Dev7f349142009-03-27 09:06:31 -0700319 }
320 skb_queue_head_init(&fc->fcoe_pending_queue);
321 fc->fcoe_pending_queue_active = 0;
Vasu Dev1047f222009-05-06 10:52:40 -0700322 setup_timer(&fc->timer, fcoe_queue_timer, (unsigned long)lp);
Vasu Dev7f349142009-03-27 09:06:31 -0700323
Vasu Dev184dd342009-05-17 12:33:28 +0000324 /* look for SAN MAC address, if multiple SAN MACs exist, only
325 * use the first one for SPMA */
326 rcu_read_lock();
327 for_each_dev_addr(netdev, ha) {
328 if ((ha->type == NETDEV_HW_ADDR_T_SAN) &&
Yi Zou7a7f0c72009-07-29 17:03:50 -0700329 (is_valid_ether_addr(ha->addr))) {
Vasu Dev184dd342009-05-17 12:33:28 +0000330 memcpy(fc->ctlr.ctl_src_addr, ha->addr, ETH_ALEN);
331 fc->ctlr.spma = 1;
332 break;
333 }
334 }
335 rcu_read_unlock();
336
Vasu Dev7f349142009-03-27 09:06:31 -0700337 /* setup Source Mac Address */
Vasu Dev184dd342009-05-17 12:33:28 +0000338 if (!fc->ctlr.spma)
Vasu Dev1d1b88d2009-07-29 17:05:45 -0700339 memcpy(fc->ctlr.ctl_src_addr, netdev->dev_addr,
340 fc->netdev->addr_len);
Vasu Dev7f349142009-03-27 09:06:31 -0700341
Vasu Dev1d1b88d2009-07-29 17:05:45 -0700342 wwnn = fcoe_wwn_from_mac(netdev->dev_addr, 1, 0);
Vasu Dev7f349142009-03-27 09:06:31 -0700343 fc_set_wwnn(lp, wwnn);
344 /* XXX - 3rd arg needs to be vlan id */
Vasu Dev1d1b88d2009-07-29 17:05:45 -0700345 wwpn = fcoe_wwn_from_mac(netdev->dev_addr, 2, 0);
Vasu Dev7f349142009-03-27 09:06:31 -0700346 fc_set_wwpn(lp, wwpn);
347
348 /*
349 * Add FCoE MAC address as second unicast MAC address
350 * or enter promiscuous mode if not capable of listening
351 * for multiple unicast MACs.
352 */
353 rtnl_lock();
354 memcpy(flogi_maddr, (u8[6]) FC_FCOE_FLOGI_MAC, ETH_ALEN);
Vasu Dev1d1b88d2009-07-29 17:05:45 -0700355 dev_unicast_add(netdev, flogi_maddr);
Vasu Dev184dd342009-05-17 12:33:28 +0000356 if (fc->ctlr.spma)
Vasu Dev1d1b88d2009-07-29 17:05:45 -0700357 dev_unicast_add(netdev, fc->ctlr.ctl_src_addr);
358 dev_mc_add(netdev, FIP_ALL_ENODE_MACS, ETH_ALEN, 0);
Vasu Dev7f349142009-03-27 09:06:31 -0700359 rtnl_unlock();
360
361 /*
362 * setup the receive function from ethernet driver
363 * on the ethertype for the given device
364 */
365 fc->fcoe_packet_type.func = fcoe_rcv;
366 fc->fcoe_packet_type.type = __constant_htons(ETH_P_FCOE);
Vasu Dev1d1b88d2009-07-29 17:05:45 -0700367 fc->fcoe_packet_type.dev = netdev;
Vasu Dev7f349142009-03-27 09:06:31 -0700368 dev_add_pack(&fc->fcoe_packet_type);
369
Vasu Devab6b85c2009-05-17 12:33:08 +0000370 fc->fip_packet_type.func = fcoe_fip_recv;
371 fc->fip_packet_type.type = htons(ETH_P_FIP);
Vasu Dev1d1b88d2009-07-29 17:05:45 -0700372 fc->fip_packet_type.dev = netdev;
Vasu Devab6b85c2009-05-17 12:33:08 +0000373 dev_add_pack(&fc->fip_packet_type);
374
Vasu Dev7f349142009-03-27 09:06:31 -0700375 return 0;
376}
377
378/**
379 * fcoe_shost_config() - Sets up fc_lport->host
380 * @lp : ptr to the fc_lport
381 * @shost : ptr to the associated scsi host
382 * @dev : device associated to scsi host
383 *
384 * Must be called after fcoe_lport_config() and fcoe_netdev_config()
385 *
386 * Returns : 0 for success
387 */
388static int fcoe_shost_config(struct fc_lport *lp, struct Scsi_Host *shost,
389 struct device *dev)
390{
391 int rc = 0;
392
393 /* lport scsi host config */
394 lp->host = shost;
395
396 lp->host->max_lun = FCOE_MAX_LUN;
397 lp->host->max_id = FCOE_MAX_FCP_TARGET;
398 lp->host->max_channel = 0;
399 lp->host->transportt = scsi_transport_fcoe_sw;
400
401 /* add the new host to the SCSI-ml */
402 rc = scsi_add_host(lp->host, dev);
403 if (rc) {
Robert Loved5488eb2009-06-10 15:30:59 -0700404 FCOE_NETDEV_DBG(fcoe_netdev(lp), "fcoe_shost_config: "
405 "error on scsi_add_host\n");
Vasu Dev7f349142009-03-27 09:06:31 -0700406 return rc;
407 }
408 sprintf(fc_host_symbolic_name(lp->host), "%s v%s over %s",
409 FCOE_NAME, FCOE_VERSION,
410 fcoe_netdev(lp)->name);
411
412 return 0;
413}
414
Vasu Devd7179682009-07-29 17:05:21 -0700415/*
416 * fcoe_oem_match() - match for read types IO
417 * @fp: the fc_frame for new IO.
418 *
419 * Returns : true for read types IO, otherwise returns false.
420 */
421bool fcoe_oem_match(struct fc_frame *fp)
422{
Yi Zou05cc7392009-08-25 13:59:03 -0700423 return fc_fcp_is_read(fr_fsp(fp)) &&
424 (fr_fsp(fp)->data_len > fcoe_ddp_min);
Vasu Devd7179682009-07-29 17:05:21 -0700425}
426
Vasu Dev7f349142009-03-27 09:06:31 -0700427/**
428 * fcoe_em_config() - allocates em for this lport
429 * @lp: the port that em is to allocated for
430 *
Vasu Deve8af4d42009-07-29 17:05:15 -0700431 * Called with write fcoe_hostlist_lock held.
432 *
Vasu Dev7f349142009-03-27 09:06:31 -0700433 * Returns : 0 on success
434 */
435static inline int fcoe_em_config(struct fc_lport *lp)
436{
Vasu Devd7179682009-07-29 17:05:21 -0700437 struct fcoe_softc *fc = lport_priv(lp);
438 struct fcoe_softc *oldfc = NULL;
Vasu Dev1d1b88d2009-07-29 17:05:45 -0700439 struct net_device *old_real_dev, *cur_real_dev;
Vasu Devd7179682009-07-29 17:05:21 -0700440 u16 min_xid = FCOE_MIN_XID;
441 u16 max_xid = FCOE_MAX_XID;
442
443 /*
444 * Check if need to allocate an em instance for
445 * offload exchange ids to be shared across all VN_PORTs/lport.
446 */
447 if (!lp->lro_enabled || !lp->lro_xid || (lp->lro_xid >= max_xid)) {
448 lp->lro_xid = 0;
449 goto skip_oem;
450 }
451
452 /*
453 * Reuse existing offload em instance in case
Vasu Dev1d1b88d2009-07-29 17:05:45 -0700454 * it is already allocated on real eth device
Vasu Devd7179682009-07-29 17:05:21 -0700455 */
Vasu Dev1d1b88d2009-07-29 17:05:45 -0700456 if (fc->netdev->priv_flags & IFF_802_1Q_VLAN)
457 cur_real_dev = vlan_dev_real_dev(fc->netdev);
458 else
459 cur_real_dev = fc->netdev;
460
Vasu Devd7179682009-07-29 17:05:21 -0700461 list_for_each_entry(oldfc, &fcoe_hostlist, list) {
Vasu Dev1d1b88d2009-07-29 17:05:45 -0700462 if (oldfc->netdev->priv_flags & IFF_802_1Q_VLAN)
463 old_real_dev = vlan_dev_real_dev(oldfc->netdev);
464 else
465 old_real_dev = oldfc->netdev;
466
467 if (cur_real_dev == old_real_dev) {
Vasu Devd7179682009-07-29 17:05:21 -0700468 fc->oem = oldfc->oem;
469 break;
470 }
471 }
472
473 if (fc->oem) {
474 if (!fc_exch_mgr_add(lp, fc->oem, fcoe_oem_match)) {
475 printk(KERN_ERR "fcoe_em_config: failed to add "
476 "offload em:%p on interface:%s\n",
Vasu Dev1d1b88d2009-07-29 17:05:45 -0700477 fc->oem, fc->netdev->name);
Vasu Devd7179682009-07-29 17:05:21 -0700478 return -ENOMEM;
479 }
480 } else {
481 fc->oem = fc_exch_mgr_alloc(lp, FC_CLASS_3,
482 FCOE_MIN_XID, lp->lro_xid,
483 fcoe_oem_match);
484 if (!fc->oem) {
485 printk(KERN_ERR "fcoe_em_config: failed to allocate "
486 "em for offload exches on interface:%s\n",
Vasu Dev1d1b88d2009-07-29 17:05:45 -0700487 fc->netdev->name);
Vasu Devd7179682009-07-29 17:05:21 -0700488 return -ENOMEM;
489 }
490 }
491
492 /*
493 * Exclude offload EM xid range from next EM xid range.
494 */
495 min_xid += lp->lro_xid + 1;
496
497skip_oem:
498 if (!fc_exch_mgr_alloc(lp, FC_CLASS_3, min_xid, max_xid, NULL)) {
499 printk(KERN_ERR "fcoe_em_config: failed to "
Vasu Dev1d1b88d2009-07-29 17:05:45 -0700500 "allocate em on interface %s\n", fc->netdev->name);
Vasu Dev7f349142009-03-27 09:06:31 -0700501 return -ENOMEM;
Vasu Devd7179682009-07-29 17:05:21 -0700502 }
Vasu Dev7f349142009-03-27 09:06:31 -0700503
504 return 0;
505}
506
507/**
508 * fcoe_if_destroy() - FCoE software HBA tear-down function
509 * @netdev: ptr to the associated net_device
510 *
511 * Returns: 0 if link is OK for use by FCoE.
512 */
513static int fcoe_if_destroy(struct net_device *netdev)
514{
515 struct fc_lport *lp = NULL;
516 struct fcoe_softc *fc;
Vasu Dev7f349142009-03-27 09:06:31 -0700517
518 BUG_ON(!netdev);
519
Robert Loved5488eb2009-06-10 15:30:59 -0700520 FCOE_NETDEV_DBG(netdev, "Destroying interface\n");
Vasu Dev7f349142009-03-27 09:06:31 -0700521
522 lp = fcoe_hostlist_lookup(netdev);
523 if (!lp)
524 return -ENODEV;
525
526 fc = lport_priv(lp);
527
528 /* Logout of the fabric */
529 fc_fabric_logoff(lp);
530
531 /* Remove the instance from fcoe's list */
532 fcoe_hostlist_remove(lp);
533
Vasu Devab6b85c2009-05-17 12:33:08 +0000534 /* clean up netdev configurations */
535 fcoe_netdev_cleanup(fc);
536
537 /* tear-down the FCoE controller */
Joe Eykholt97c83892009-03-17 11:42:40 -0700538 fcoe_ctlr_destroy(&fc->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 */
541 fcoe_percpu_clean(lp);
542
Vasu Dev7f349142009-03-27 09:06:31 -0700543 /* Cleanup the fc_lport */
544 fc_lport_destroy(lp);
545 fc_fcp_destroy(lp);
546
547 /* Detach from the scsi-ml */
548 fc_remove_host(lp->host);
549 scsi_remove_host(lp->host);
550
551 /* There are no more rports or I/O, free the EM */
Vasu Dev52ff8782009-07-29 17:05:10 -0700552 fc_exch_mgr_free(lp);
Vasu Dev7f349142009-03-27 09:06:31 -0700553
Vasu Dev7f349142009-03-27 09:06:31 -0700554 /* Free existing skbs */
555 fcoe_clean_pending_queue(lp);
556
Vasu Dev1047f222009-05-06 10:52:40 -0700557 /* Stop the timer */
558 del_timer_sync(&fc->timer);
559
Vasu Dev7f349142009-03-27 09:06:31 -0700560 /* Free memory used by statistical counters */
561 fc_lport_free_stats(lp);
562
563 /* Release the net_device and Scsi_Host */
Vasu Dev1d1b88d2009-07-29 17:05:45 -0700564 dev_put(netdev);
Vasu Dev7f349142009-03-27 09:06:31 -0700565 scsi_host_put(lp->host);
566
567 return 0;
568}
569
570/*
571 * fcoe_ddp_setup - calls LLD's ddp_setup through net_device
572 * @lp: the corresponding fc_lport
573 * @xid: the exchange id for this ddp transfer
574 * @sgl: the scatterlist describing this transfer
575 * @sgc: number of sg items
576 *
577 * Returns : 0 no ddp
578 */
579static int fcoe_ddp_setup(struct fc_lport *lp, u16 xid,
580 struct scatterlist *sgl, unsigned int sgc)
581{
582 struct net_device *n = fcoe_netdev(lp);
583
584 if (n->netdev_ops && n->netdev_ops->ndo_fcoe_ddp_setup)
585 return n->netdev_ops->ndo_fcoe_ddp_setup(n, xid, sgl, sgc);
586
587 return 0;
588}
589
590/*
591 * fcoe_ddp_done - calls LLD's ddp_done through net_device
592 * @lp: the corresponding fc_lport
593 * @xid: the exchange id for this ddp transfer
594 *
595 * Returns : the length of data that have been completed by ddp
596 */
597static int fcoe_ddp_done(struct fc_lport *lp, u16 xid)
598{
599 struct net_device *n = fcoe_netdev(lp);
600
601 if (n->netdev_ops && n->netdev_ops->ndo_fcoe_ddp_done)
602 return n->netdev_ops->ndo_fcoe_ddp_done(n, xid);
603 return 0;
604}
605
606static struct libfc_function_template fcoe_libfc_fcn_templ = {
607 .frame_send = fcoe_xmit,
608 .ddp_setup = fcoe_ddp_setup,
609 .ddp_done = fcoe_ddp_done,
610};
611
612/**
613 * fcoe_if_create() - this function creates the fcoe interface
614 * @netdev: pointer the associated netdevice
615 *
616 * Creates fc_lport struct and scsi_host for lport, configures lport
617 * and starts fabric login.
618 *
619 * Returns : 0 on success
620 */
621static int fcoe_if_create(struct net_device *netdev)
622{
623 int rc;
624 struct fc_lport *lp = NULL;
625 struct fcoe_softc *fc;
626 struct Scsi_Host *shost;
627
628 BUG_ON(!netdev);
629
Robert Loved5488eb2009-06-10 15:30:59 -0700630 FCOE_NETDEV_DBG(netdev, "Create Interface\n");
Vasu Dev7f349142009-03-27 09:06:31 -0700631
632 lp = fcoe_hostlist_lookup(netdev);
633 if (lp)
634 return -EEXIST;
635
Vasu Deva0a25da2009-03-17 11:42:29 -0700636 shost = libfc_host_alloc(&fcoe_shost_template,
637 sizeof(struct fcoe_softc));
Vasu Dev7f349142009-03-27 09:06:31 -0700638 if (!shost) {
Robert Loved5488eb2009-06-10 15:30:59 -0700639 FCOE_NETDEV_DBG(netdev, "Could not allocate host structure\n");
Vasu Dev7f349142009-03-27 09:06:31 -0700640 return -ENOMEM;
641 }
642 lp = shost_priv(shost);
643 fc = lport_priv(lp);
644
645 /* configure fc_lport, e.g., em */
646 rc = fcoe_lport_config(lp);
647 if (rc) {
Robert Loved5488eb2009-06-10 15:30:59 -0700648 FCOE_NETDEV_DBG(netdev, "Could not configure lport for the "
649 "interface\n");
Vasu Dev7f349142009-03-27 09:06:31 -0700650 goto out_host_put;
651 }
652
Joe Eykholt97c83892009-03-17 11:42:40 -0700653 /*
654 * Initialize FIP.
655 */
656 fcoe_ctlr_init(&fc->ctlr);
657 fc->ctlr.send = fcoe_fip_send;
658 fc->ctlr.update_mac = fcoe_update_src_mac;
659
Vasu Devab6b85c2009-05-17 12:33:08 +0000660 /* configure lport network properties */
661 rc = fcoe_netdev_config(lp, netdev);
662 if (rc) {
Robert Loved5488eb2009-06-10 15:30:59 -0700663 FCOE_NETDEV_DBG(netdev, "Could not configure netdev for the "
664 "interface\n");
Vasu Devab6b85c2009-05-17 12:33:08 +0000665 goto out_netdev_cleanup;
666 }
Joe Eykholt97c83892009-03-17 11:42:40 -0700667
Vasu Dev7f349142009-03-27 09:06:31 -0700668 /* configure lport scsi host properties */
669 rc = fcoe_shost_config(lp, shost, &netdev->dev);
670 if (rc) {
Robert Loved5488eb2009-06-10 15:30:59 -0700671 FCOE_NETDEV_DBG(netdev, "Could not configure shost for the "
672 "interface\n");
Vasu Devab6b85c2009-05-17 12:33:08 +0000673 goto out_netdev_cleanup;
Vasu Dev7f349142009-03-27 09:06:31 -0700674 }
675
Vasu Dev7f349142009-03-27 09:06:31 -0700676 /* Initialize the library */
677 rc = fcoe_libfc_config(lp, &fcoe_libfc_fcn_templ);
678 if (rc) {
Robert Loved5488eb2009-06-10 15:30:59 -0700679 FCOE_NETDEV_DBG(netdev, "Could not configure libfc for the "
680 "interface\n");
Vasu Dev7f349142009-03-27 09:06:31 -0700681 goto out_lp_destroy;
682 }
683
Vasu Deve8af4d42009-07-29 17:05:15 -0700684 /*
685 * fcoe_em_alloc() and fcoe_hostlist_add() both
686 * need to be atomic under fcoe_hostlist_lock
687 * since fcoe_em_alloc() looks for an existing EM
688 * instance on host list updated by fcoe_hostlist_add().
689 */
690 write_lock(&fcoe_hostlist_lock);
Vasu Dev96316092009-07-29 17:05:00 -0700691 /* lport exch manager allocation */
692 rc = fcoe_em_config(lp);
693 if (rc) {
694 FCOE_NETDEV_DBG(netdev, "Could not configure the EM for the "
695 "interface\n");
696 goto out_lp_destroy;
697 }
698
Vasu Dev7f349142009-03-27 09:06:31 -0700699 /* add to lports list */
700 fcoe_hostlist_add(lp);
Vasu Deve8af4d42009-07-29 17:05:15 -0700701 write_unlock(&fcoe_hostlist_lock);
Vasu Dev7f349142009-03-27 09:06:31 -0700702
703 lp->boot_time = jiffies;
704
705 fc_fabric_login(lp);
706
Joe Eykholt97c83892009-03-17 11:42:40 -0700707 if (!fcoe_link_ok(lp))
708 fcoe_ctlr_link_up(&fc->ctlr);
709
Vasu Dev7f349142009-03-27 09:06:31 -0700710 dev_hold(netdev);
711
712 return rc;
713
714out_lp_destroy:
Vasu Dev52ff8782009-07-29 17:05:10 -0700715 fc_exch_mgr_free(lp);
Vasu Devab6b85c2009-05-17 12:33:08 +0000716out_netdev_cleanup:
717 fcoe_netdev_cleanup(fc);
Vasu Dev7f349142009-03-27 09:06:31 -0700718out_host_put:
719 scsi_host_put(lp->host);
720 return rc;
721}
722
723/**
724 * fcoe_if_init() - attach to scsi transport
725 *
726 * Returns : 0 on success
727 */
728static int __init fcoe_if_init(void)
729{
730 /* attach to scsi transport */
731 scsi_transport_fcoe_sw =
732 fc_attach_transport(&fcoe_transport_function);
733
734 if (!scsi_transport_fcoe_sw) {
Robert Loved5488eb2009-06-10 15:30:59 -0700735 printk(KERN_ERR "fcoe: Failed to attach to the FC transport\n");
Vasu Dev7f349142009-03-27 09:06:31 -0700736 return -ENODEV;
737 }
738
739 return 0;
740}
741
742/**
743 * fcoe_if_exit() - detach from scsi transport
744 *
745 * Returns : 0 on success
746 */
747int __exit fcoe_if_exit(void)
748{
749 fc_release_transport(scsi_transport_fcoe_sw);
750 return 0;
751}
752
Robert Love85b4aa42008-12-09 15:10:24 -0800753/**
Robert Love8976f422009-03-17 11:41:46 -0700754 * fcoe_percpu_thread_create() - Create a receive thread for an online cpu
755 * @cpu: cpu index for the online cpu
756 */
757static void fcoe_percpu_thread_create(unsigned int cpu)
758{
759 struct fcoe_percpu_s *p;
760 struct task_struct *thread;
761
762 p = &per_cpu(fcoe_percpu, cpu);
763
764 thread = kthread_create(fcoe_percpu_receive_thread,
765 (void *)p, "fcoethread/%d", cpu);
766
767 if (likely(!IS_ERR(p->thread))) {
768 kthread_bind(thread, cpu);
769 wake_up_process(thread);
770
771 spin_lock_bh(&p->fcoe_rx_list.lock);
772 p->thread = thread;
773 spin_unlock_bh(&p->fcoe_rx_list.lock);
774 }
775}
776
777/**
778 * fcoe_percpu_thread_destroy() - removes the rx thread for the given cpu
779 * @cpu: cpu index the rx thread is to be removed
780 *
781 * Destroys a per-CPU Rx thread. Any pending skbs are moved to the
782 * current CPU's Rx thread. If the thread being destroyed is bound to
783 * the CPU processing this context the skbs will be freed.
784 */
785static void fcoe_percpu_thread_destroy(unsigned int cpu)
786{
787 struct fcoe_percpu_s *p;
788 struct task_struct *thread;
789 struct page *crc_eof;
790 struct sk_buff *skb;
791#ifdef CONFIG_SMP
792 struct fcoe_percpu_s *p0;
793 unsigned targ_cpu = smp_processor_id();
794#endif /* CONFIG_SMP */
795
Robert Loved5488eb2009-06-10 15:30:59 -0700796 FCOE_DBG("Destroying receive thread for CPU %d\n", cpu);
Robert Love8976f422009-03-17 11:41:46 -0700797
798 /* Prevent any new skbs from being queued for this CPU. */
799 p = &per_cpu(fcoe_percpu, cpu);
800 spin_lock_bh(&p->fcoe_rx_list.lock);
801 thread = p->thread;
802 p->thread = NULL;
803 crc_eof = p->crc_eof_page;
804 p->crc_eof_page = NULL;
805 p->crc_eof_offset = 0;
806 spin_unlock_bh(&p->fcoe_rx_list.lock);
807
808#ifdef CONFIG_SMP
809 /*
810 * Don't bother moving the skb's if this context is running
811 * on the same CPU that is having its thread destroyed. This
812 * can easily happen when the module is removed.
813 */
814 if (cpu != targ_cpu) {
815 p0 = &per_cpu(fcoe_percpu, targ_cpu);
816 spin_lock_bh(&p0->fcoe_rx_list.lock);
817 if (p0->thread) {
Robert Loved5488eb2009-06-10 15:30:59 -0700818 FCOE_DBG("Moving frames from CPU %d to CPU %d\n",
819 cpu, targ_cpu);
Robert Love8976f422009-03-17 11:41:46 -0700820
821 while ((skb = __skb_dequeue(&p->fcoe_rx_list)) != NULL)
822 __skb_queue_tail(&p0->fcoe_rx_list, skb);
823 spin_unlock_bh(&p0->fcoe_rx_list.lock);
824 } else {
825 /*
826 * The targeted CPU is not initialized and cannot accept
827 * new skbs. Unlock the targeted CPU and drop the skbs
828 * on the CPU that is going offline.
829 */
830 while ((skb = __skb_dequeue(&p->fcoe_rx_list)) != NULL)
831 kfree_skb(skb);
832 spin_unlock_bh(&p0->fcoe_rx_list.lock);
833 }
834 } else {
835 /*
836 * This scenario occurs when the module is being removed
837 * and all threads are being destroyed. skbs will continue
838 * to be shifted from the CPU thread that is being removed
839 * to the CPU thread associated with the CPU that is processing
840 * the module removal. Once there is only one CPU Rx thread it
841 * will reach this case and we will drop all skbs and later
842 * stop the thread.
843 */
844 spin_lock_bh(&p->fcoe_rx_list.lock);
845 while ((skb = __skb_dequeue(&p->fcoe_rx_list)) != NULL)
846 kfree_skb(skb);
847 spin_unlock_bh(&p->fcoe_rx_list.lock);
848 }
849#else
850 /*
Chris Leechdd3fd722009-04-21 16:27:36 -0700851 * This a non-SMP scenario where the singular Rx thread is
Robert Love8976f422009-03-17 11:41:46 -0700852 * being removed. Free all skbs and 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#endif
859
860 if (thread)
861 kthread_stop(thread);
862
863 if (crc_eof)
864 put_page(crc_eof);
865}
866
867/**
868 * fcoe_cpu_callback() - fcoe cpu hotplug event callback
869 * @nfb: callback data block
870 * @action: event triggering the callback
871 * @hcpu: index for the cpu of this event
872 *
873 * This creates or destroys per cpu data for fcoe
874 *
875 * Returns NOTIFY_OK always.
876 */
877static int fcoe_cpu_callback(struct notifier_block *nfb,
878 unsigned long action, void *hcpu)
879{
880 unsigned cpu = (unsigned long)hcpu;
881
882 switch (action) {
883 case CPU_ONLINE:
884 case CPU_ONLINE_FROZEN:
Robert Loved5488eb2009-06-10 15:30:59 -0700885 FCOE_DBG("CPU %x online: Create Rx thread\n", cpu);
Robert Love8976f422009-03-17 11:41:46 -0700886 fcoe_percpu_thread_create(cpu);
887 break;
888 case CPU_DEAD:
889 case CPU_DEAD_FROZEN:
Robert Loved5488eb2009-06-10 15:30:59 -0700890 FCOE_DBG("CPU %x offline: Remove Rx thread\n", cpu);
Robert Love8976f422009-03-17 11:41:46 -0700891 fcoe_percpu_thread_destroy(cpu);
892 break;
893 default:
894 break;
895 }
896 return NOTIFY_OK;
897}
898
899static struct notifier_block fcoe_cpu_notifier = {
900 .notifier_call = fcoe_cpu_callback,
901};
902
903/**
Robert Love34f42a02009-02-27 10:55:45 -0800904 * fcoe_rcv() - this is the fcoe receive function called by NET_RX_SOFTIRQ
Robert Love85b4aa42008-12-09 15:10:24 -0800905 * @skb: the receive skb
906 * @dev: associated net device
907 * @ptype: context
Chris Leechdd3fd722009-04-21 16:27:36 -0700908 * @olddev: last device
Robert Love85b4aa42008-12-09 15:10:24 -0800909 *
910 * this function will receive the packet and build fc frame and pass it up
911 *
912 * Returns: 0 for success
Robert Love34f42a02009-02-27 10:55:45 -0800913 */
Robert Love85b4aa42008-12-09 15:10:24 -0800914int fcoe_rcv(struct sk_buff *skb, struct net_device *dev,
915 struct packet_type *ptype, struct net_device *olddev)
916{
917 struct fc_lport *lp;
918 struct fcoe_rcv_info *fr;
919 struct fcoe_softc *fc;
Robert Love85b4aa42008-12-09 15:10:24 -0800920 struct fc_frame_header *fh;
Robert Love85b4aa42008-12-09 15:10:24 -0800921 struct fcoe_percpu_s *fps;
Vasu Devb2f00912009-08-25 13:58:53 -0700922 unsigned int cpu;
Robert Love85b4aa42008-12-09 15:10:24 -0800923
924 fc = container_of(ptype, struct fcoe_softc, fcoe_packet_type);
Joe Eykholt97c83892009-03-17 11:42:40 -0700925 lp = fc->ctlr.lp;
Robert Love85b4aa42008-12-09 15:10:24 -0800926 if (unlikely(lp == NULL)) {
Robert Loved5488eb2009-06-10 15:30:59 -0700927 FCOE_NETDEV_DBG(dev, "Cannot find hba structure");
Robert Love85b4aa42008-12-09 15:10:24 -0800928 goto err2;
929 }
Joe Eykholt97c83892009-03-17 11:42:40 -0700930 if (!lp->link_up)
931 goto err2;
Robert Love85b4aa42008-12-09 15:10:24 -0800932
Robert Loved5488eb2009-06-10 15:30:59 -0700933 FCOE_NETDEV_DBG(dev, "skb_info: len:%d data_len:%d head:%p "
934 "data:%p tail:%p end:%p sum:%d dev:%s",
935 skb->len, skb->data_len, skb->head, skb->data,
936 skb_tail_pointer(skb), skb_end_pointer(skb),
937 skb->csum, skb->dev ? skb->dev->name : "<NULL>");
Robert Love85b4aa42008-12-09 15:10:24 -0800938
939 /* check for FCOE packet type */
940 if (unlikely(eth_hdr(skb)->h_proto != htons(ETH_P_FCOE))) {
Robert Loved5488eb2009-06-10 15:30:59 -0700941 FCOE_NETDEV_DBG(dev, "Wrong FC type frame");
Robert Love85b4aa42008-12-09 15:10:24 -0800942 goto err;
943 }
944
945 /*
946 * Check for minimum frame length, and make sure required FCoE
947 * and FC headers are pulled into the linear data area.
948 */
949 if (unlikely((skb->len < FCOE_MIN_FRAME) ||
950 !pskb_may_pull(skb, FCOE_HEADER_LEN)))
951 goto err;
952
953 skb_set_transport_header(skb, sizeof(struct fcoe_hdr));
954 fh = (struct fc_frame_header *) skb_transport_header(skb);
955
Robert Love85b4aa42008-12-09 15:10:24 -0800956 fr = fcoe_dev_from_skb(skb);
957 fr->fr_dev = lp;
958 fr->ptype = ptype;
Robert Love5e5e92d2009-03-17 11:41:35 -0700959
Robert Love85b4aa42008-12-09 15:10:24 -0800960 /*
Vasu Devb2f00912009-08-25 13:58:53 -0700961 * In case the incoming frame's exchange is originated from
962 * the initiator, then received frame's exchange id is ANDed
963 * with fc_cpu_mask bits to get the same cpu on which exchange
964 * was originated, otherwise just use the current cpu.
Robert Love85b4aa42008-12-09 15:10:24 -0800965 */
Vasu Devb2f00912009-08-25 13:58:53 -0700966 if (ntoh24(fh->fh_f_ctl) & FC_FC_EX_CTX)
967 cpu = ntohs(fh->fh_ox_id) & fc_cpu_mask;
968 else
969 cpu = smp_processor_id();
Robert Love5e5e92d2009-03-17 11:41:35 -0700970
Robert Love8976f422009-03-17 11:41:46 -0700971 fps = &per_cpu(fcoe_percpu, cpu);
Robert Love85b4aa42008-12-09 15:10:24 -0800972 spin_lock_bh(&fps->fcoe_rx_list.lock);
Robert Love8976f422009-03-17 11:41:46 -0700973 if (unlikely(!fps->thread)) {
974 /*
975 * The targeted CPU is not ready, let's target
976 * the first CPU now. For non-SMP systems this
977 * will check the same CPU twice.
978 */
Robert Loved5488eb2009-06-10 15:30:59 -0700979 FCOE_NETDEV_DBG(dev, "CPU is online, but no receive thread "
980 "ready for incoming skb- using first online "
981 "CPU.\n");
Robert Love8976f422009-03-17 11:41:46 -0700982
983 spin_unlock_bh(&fps->fcoe_rx_list.lock);
984 cpu = first_cpu(cpu_online_map);
985 fps = &per_cpu(fcoe_percpu, cpu);
986 spin_lock_bh(&fps->fcoe_rx_list.lock);
987 if (!fps->thread) {
988 spin_unlock_bh(&fps->fcoe_rx_list.lock);
989 goto err;
990 }
991 }
992
993 /*
994 * We now have a valid CPU that we're targeting for
995 * this skb. We also have this receive thread locked,
996 * so we're free to queue skbs into it's queue.
997 */
Robert Love85b4aa42008-12-09 15:10:24 -0800998 __skb_queue_tail(&fps->fcoe_rx_list, skb);
999 if (fps->fcoe_rx_list.qlen == 1)
1000 wake_up_process(fps->thread);
1001
1002 spin_unlock_bh(&fps->fcoe_rx_list.lock);
1003
1004 return 0;
1005err:
Robert Love582b45b2009-03-31 15:51:50 -07001006 fc_lport_get_stats(lp)->ErrorFrames++;
Robert Love85b4aa42008-12-09 15:10:24 -08001007
1008err2:
1009 kfree_skb(skb);
1010 return -1;
1011}
Robert Love85b4aa42008-12-09 15:10:24 -08001012
1013/**
Robert Love34f42a02009-02-27 10:55:45 -08001014 * fcoe_start_io() - pass to netdev to start xmit for fcoe
Robert Love85b4aa42008-12-09 15:10:24 -08001015 * @skb: the skb to be xmitted
1016 *
1017 * Returns: 0 for success
Robert Love34f42a02009-02-27 10:55:45 -08001018 */
Robert Love85b4aa42008-12-09 15:10:24 -08001019static inline int fcoe_start_io(struct sk_buff *skb)
1020{
1021 int rc;
1022
1023 skb_get(skb);
1024 rc = dev_queue_xmit(skb);
1025 if (rc != 0)
1026 return rc;
1027 kfree_skb(skb);
1028 return 0;
1029}
1030
1031/**
Chris Leechdd3fd722009-04-21 16:27:36 -07001032 * fcoe_get_paged_crc_eof() - in case we need to alloc a page for crc_eof
Robert Love85b4aa42008-12-09 15:10:24 -08001033 * @skb: the skb to be xmitted
1034 * @tlen: total len
1035 *
1036 * Returns: 0 for success
Robert Love34f42a02009-02-27 10:55:45 -08001037 */
Robert Love85b4aa42008-12-09 15:10:24 -08001038static int fcoe_get_paged_crc_eof(struct sk_buff *skb, int tlen)
1039{
1040 struct fcoe_percpu_s *fps;
1041 struct page *page;
Robert Love85b4aa42008-12-09 15:10:24 -08001042
Robert Love5e5e92d2009-03-17 11:41:35 -07001043 fps = &get_cpu_var(fcoe_percpu);
Robert Love85b4aa42008-12-09 15:10:24 -08001044 page = fps->crc_eof_page;
1045 if (!page) {
1046 page = alloc_page(GFP_ATOMIC);
1047 if (!page) {
Robert Love5e5e92d2009-03-17 11:41:35 -07001048 put_cpu_var(fcoe_percpu);
Robert Love85b4aa42008-12-09 15:10:24 -08001049 return -ENOMEM;
1050 }
1051 fps->crc_eof_page = page;
Robert Love8976f422009-03-17 11:41:46 -07001052 fps->crc_eof_offset = 0;
Robert Love85b4aa42008-12-09 15:10:24 -08001053 }
1054
1055 get_page(page);
1056 skb_fill_page_desc(skb, skb_shinfo(skb)->nr_frags, page,
1057 fps->crc_eof_offset, tlen);
1058 skb->len += tlen;
1059 skb->data_len += tlen;
1060 skb->truesize += tlen;
1061 fps->crc_eof_offset += sizeof(struct fcoe_crc_eof);
1062
1063 if (fps->crc_eof_offset >= PAGE_SIZE) {
1064 fps->crc_eof_page = NULL;
1065 fps->crc_eof_offset = 0;
1066 put_page(page);
1067 }
Robert Love5e5e92d2009-03-17 11:41:35 -07001068 put_cpu_var(fcoe_percpu);
Robert Love85b4aa42008-12-09 15:10:24 -08001069 return 0;
1070}
1071
1072/**
Robert Love34f42a02009-02-27 10:55:45 -08001073 * fcoe_fc_crc() - calculates FC CRC in this fcoe skb
Chris Leechdd3fd722009-04-21 16:27:36 -07001074 * @fp: the fc_frame containing data to be checksummed
Robert Love85b4aa42008-12-09 15:10:24 -08001075 *
1076 * This uses crc32() to calculate the crc for fc frame
1077 * Return : 32 bit crc
Robert Love34f42a02009-02-27 10:55:45 -08001078 */
Robert Love85b4aa42008-12-09 15:10:24 -08001079u32 fcoe_fc_crc(struct fc_frame *fp)
1080{
1081 struct sk_buff *skb = fp_skb(fp);
1082 struct skb_frag_struct *frag;
1083 unsigned char *data;
1084 unsigned long off, len, clen;
1085 u32 crc;
1086 unsigned i;
1087
1088 crc = crc32(~0, skb->data, skb_headlen(skb));
1089
1090 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
1091 frag = &skb_shinfo(skb)->frags[i];
1092 off = frag->page_offset;
1093 len = frag->size;
1094 while (len > 0) {
1095 clen = min(len, PAGE_SIZE - (off & ~PAGE_MASK));
1096 data = kmap_atomic(frag->page + (off >> PAGE_SHIFT),
1097 KM_SKB_DATA_SOFTIRQ);
1098 crc = crc32(crc, data + (off & ~PAGE_MASK), clen);
1099 kunmap_atomic(data, KM_SKB_DATA_SOFTIRQ);
1100 off += clen;
1101 len -= clen;
1102 }
1103 }
1104 return crc;
1105}
Robert Love85b4aa42008-12-09 15:10:24 -08001106
1107/**
Robert Love34f42a02009-02-27 10:55:45 -08001108 * fcoe_xmit() - FCoE frame transmit function
Robert Love85b4aa42008-12-09 15:10:24 -08001109 * @lp: the associated local port
1110 * @fp: the fc_frame to be transmitted
1111 *
1112 * Return : 0 for success
Robert Love34f42a02009-02-27 10:55:45 -08001113 */
Robert Love85b4aa42008-12-09 15:10:24 -08001114int fcoe_xmit(struct fc_lport *lp, struct fc_frame *fp)
1115{
Vasu Dev4bb6b512009-05-06 10:52:34 -07001116 int wlen;
Robert Love85b4aa42008-12-09 15:10:24 -08001117 u32 crc;
1118 struct ethhdr *eh;
1119 struct fcoe_crc_eof *cp;
1120 struct sk_buff *skb;
1121 struct fcoe_dev_stats *stats;
1122 struct fc_frame_header *fh;
1123 unsigned int hlen; /* header length implies the version */
1124 unsigned int tlen; /* trailer length */
1125 unsigned int elen; /* eth header, may include vlan */
Robert Love85b4aa42008-12-09 15:10:24 -08001126 struct fcoe_softc *fc;
1127 u8 sof, eof;
1128 struct fcoe_hdr *hp;
1129
1130 WARN_ON((fr_len(fp) % sizeof(u32)) != 0);
1131
Robert Lovefc47ff62009-02-27 10:55:55 -08001132 fc = lport_priv(lp);
Robert Love85b4aa42008-12-09 15:10:24 -08001133 fh = fc_frame_header_get(fp);
Joe Eykholt97c83892009-03-17 11:42:40 -07001134 skb = fp_skb(fp);
1135 wlen = skb->len / FCOE_WORD_TO_BYTE;
1136
1137 if (!lp->link_up) {
Dan Carpenter3caf02e2009-04-21 16:27:25 -07001138 kfree_skb(skb);
Joe Eykholt97c83892009-03-17 11:42:40 -07001139 return 0;
Robert Love85b4aa42008-12-09 15:10:24 -08001140 }
1141
Joe Eykholt97c83892009-03-17 11:42:40 -07001142 if (unlikely(fh->fh_r_ctl == FC_RCTL_ELS_REQ) &&
1143 fcoe_ctlr_els_send(&fc->ctlr, skb))
1144 return 0;
1145
Robert Love85b4aa42008-12-09 15:10:24 -08001146 sof = fr_sof(fp);
1147 eof = fr_eof(fp);
1148
Vasu Dev4e57e1c2009-05-06 10:52:46 -07001149 elen = sizeof(struct ethhdr);
Robert Love85b4aa42008-12-09 15:10:24 -08001150 hlen = sizeof(struct fcoe_hdr);
1151 tlen = sizeof(struct fcoe_crc_eof);
1152 wlen = (skb->len - tlen + sizeof(crc)) / FCOE_WORD_TO_BYTE;
1153
1154 /* crc offload */
1155 if (likely(lp->crc_offload)) {
Yi Zou39ca9a02009-02-27 14:07:15 -08001156 skb->ip_summed = CHECKSUM_PARTIAL;
Robert Love85b4aa42008-12-09 15:10:24 -08001157 skb->csum_start = skb_headroom(skb);
1158 skb->csum_offset = skb->len;
1159 crc = 0;
1160 } else {
1161 skb->ip_summed = CHECKSUM_NONE;
1162 crc = fcoe_fc_crc(fp);
1163 }
1164
1165 /* copy fc crc and eof to the skb buff */
1166 if (skb_is_nonlinear(skb)) {
1167 skb_frag_t *frag;
1168 if (fcoe_get_paged_crc_eof(skb, tlen)) {
Roel Kluine9041582009-02-27 10:56:22 -08001169 kfree_skb(skb);
Robert Love85b4aa42008-12-09 15:10:24 -08001170 return -ENOMEM;
1171 }
1172 frag = &skb_shinfo(skb)->frags[skb_shinfo(skb)->nr_frags - 1];
1173 cp = kmap_atomic(frag->page, KM_SKB_DATA_SOFTIRQ)
1174 + frag->page_offset;
1175 } else {
1176 cp = (struct fcoe_crc_eof *)skb_put(skb, tlen);
1177 }
1178
1179 memset(cp, 0, sizeof(*cp));
1180 cp->fcoe_eof = eof;
1181 cp->fcoe_crc32 = cpu_to_le32(~crc);
1182
1183 if (skb_is_nonlinear(skb)) {
1184 kunmap_atomic(cp, KM_SKB_DATA_SOFTIRQ);
1185 cp = NULL;
1186 }
1187
Chris Leechdd3fd722009-04-21 16:27:36 -07001188 /* adjust skb network/transport offsets to match mac/fcoe/fc */
Robert Love85b4aa42008-12-09 15:10:24 -08001189 skb_push(skb, elen + hlen);
1190 skb_reset_mac_header(skb);
1191 skb_reset_network_header(skb);
1192 skb->mac_len = elen;
Yi Zou211c7382009-02-27 14:06:37 -08001193 skb->protocol = htons(ETH_P_FCOE);
Vasu Dev1d1b88d2009-07-29 17:05:45 -07001194 skb->dev = fc->netdev;
Robert Love85b4aa42008-12-09 15:10:24 -08001195
1196 /* fill up mac and fcoe headers */
1197 eh = eth_hdr(skb);
1198 eh->h_proto = htons(ETH_P_FCOE);
Joe Eykholt97c83892009-03-17 11:42:40 -07001199 if (fc->ctlr.map_dest)
Robert Love85b4aa42008-12-09 15:10:24 -08001200 fc_fcoe_set_mac(eh->h_dest, fh->fh_d_id);
1201 else
1202 /* insert GW address */
Joe Eykholt97c83892009-03-17 11:42:40 -07001203 memcpy(eh->h_dest, fc->ctlr.dest_addr, ETH_ALEN);
Robert Love85b4aa42008-12-09 15:10:24 -08001204
Joe Eykholt97c83892009-03-17 11:42:40 -07001205 if (unlikely(fc->ctlr.flogi_oxid != FC_XID_UNKNOWN))
1206 memcpy(eh->h_source, fc->ctlr.ctl_src_addr, ETH_ALEN);
Robert Love85b4aa42008-12-09 15:10:24 -08001207 else
Joe Eykholt97c83892009-03-17 11:42:40 -07001208 memcpy(eh->h_source, fc->ctlr.data_src_addr, ETH_ALEN);
Robert Love85b4aa42008-12-09 15:10:24 -08001209
1210 hp = (struct fcoe_hdr *)(eh + 1);
1211 memset(hp, 0, sizeof(*hp));
1212 if (FC_FCOE_VER)
1213 FC_FCOE_ENCAPS_VER(hp, FC_FCOE_VER);
1214 hp->fcoe_sof = sof;
1215
Yi Zou39ca9a02009-02-27 14:07:15 -08001216 /* fcoe lso, mss is in max_payload which is non-zero for FCP data */
1217 if (lp->seq_offload && fr_max_payload(fp)) {
1218 skb_shinfo(skb)->gso_type = SKB_GSO_FCOE;
1219 skb_shinfo(skb)->gso_size = fr_max_payload(fp);
1220 } else {
1221 skb_shinfo(skb)->gso_type = 0;
1222 skb_shinfo(skb)->gso_size = 0;
1223 }
Robert Love85b4aa42008-12-09 15:10:24 -08001224 /* update tx stats: regardless if LLD fails */
Robert Love582b45b2009-03-31 15:51:50 -07001225 stats = fc_lport_get_stats(lp);
1226 stats->TxFrames++;
1227 stats->TxWords += wlen;
Robert Love85b4aa42008-12-09 15:10:24 -08001228
1229 /* send down to lld */
1230 fr_dev(fp) = lp;
1231 if (fc->fcoe_pending_queue.qlen)
Vasu Dev4bb6b512009-05-06 10:52:34 -07001232 fcoe_check_wait_queue(lp, skb);
1233 else if (fcoe_start_io(skb))
1234 fcoe_check_wait_queue(lp, skb);
Robert Love85b4aa42008-12-09 15:10:24 -08001235
1236 return 0;
1237}
Robert Love85b4aa42008-12-09 15:10:24 -08001238
Robert Love34f42a02009-02-27 10:55:45 -08001239/**
1240 * fcoe_percpu_receive_thread() - recv thread per cpu
Robert Love85b4aa42008-12-09 15:10:24 -08001241 * @arg: ptr to the fcoe per cpu struct
1242 *
1243 * Return: 0 for success
Robert Love85b4aa42008-12-09 15:10:24 -08001244 */
1245int fcoe_percpu_receive_thread(void *arg)
1246{
1247 struct fcoe_percpu_s *p = arg;
1248 u32 fr_len;
1249 struct fc_lport *lp;
1250 struct fcoe_rcv_info *fr;
1251 struct fcoe_dev_stats *stats;
1252 struct fc_frame_header *fh;
1253 struct sk_buff *skb;
1254 struct fcoe_crc_eof crc_eof;
1255 struct fc_frame *fp;
1256 u8 *mac = NULL;
1257 struct fcoe_softc *fc;
1258 struct fcoe_hdr *hp;
1259
Robert Love4469c192009-02-27 10:56:38 -08001260 set_user_nice(current, -20);
Robert Love85b4aa42008-12-09 15:10:24 -08001261
1262 while (!kthread_should_stop()) {
1263
1264 spin_lock_bh(&p->fcoe_rx_list.lock);
1265 while ((skb = __skb_dequeue(&p->fcoe_rx_list)) == NULL) {
1266 set_current_state(TASK_INTERRUPTIBLE);
1267 spin_unlock_bh(&p->fcoe_rx_list.lock);
1268 schedule();
1269 set_current_state(TASK_RUNNING);
1270 if (kthread_should_stop())
1271 return 0;
1272 spin_lock_bh(&p->fcoe_rx_list.lock);
1273 }
1274 spin_unlock_bh(&p->fcoe_rx_list.lock);
1275 fr = fcoe_dev_from_skb(skb);
1276 lp = fr->fr_dev;
1277 if (unlikely(lp == NULL)) {
Robert Loved5488eb2009-06-10 15:30:59 -07001278 FCOE_NETDEV_DBG(skb->dev, "Invalid HBA Structure");
Robert Love85b4aa42008-12-09 15:10:24 -08001279 kfree_skb(skb);
1280 continue;
1281 }
1282
Robert Loved5488eb2009-06-10 15:30:59 -07001283 FCOE_NETDEV_DBG(skb->dev, "skb_info: len:%d data_len:%d "
1284 "head:%p data:%p tail:%p end:%p sum:%d dev:%s",
1285 skb->len, skb->data_len,
1286 skb->head, skb->data, skb_tail_pointer(skb),
1287 skb_end_pointer(skb), skb->csum,
1288 skb->dev ? skb->dev->name : "<NULL>");
Robert Love85b4aa42008-12-09 15:10:24 -08001289
1290 /*
1291 * Save source MAC address before discarding header.
1292 */
1293 fc = lport_priv(lp);
Robert Love85b4aa42008-12-09 15:10:24 -08001294 if (skb_is_nonlinear(skb))
1295 skb_linearize(skb); /* not ideal */
Joe Eykholt97c83892009-03-17 11:42:40 -07001296 mac = eth_hdr(skb)->h_source;
Robert Love85b4aa42008-12-09 15:10:24 -08001297
1298 /*
1299 * Frame length checks and setting up the header pointers
1300 * was done in fcoe_rcv already.
1301 */
1302 hp = (struct fcoe_hdr *) skb_network_header(skb);
1303 fh = (struct fc_frame_header *) skb_transport_header(skb);
1304
Robert Love582b45b2009-03-31 15:51:50 -07001305 stats = fc_lport_get_stats(lp);
Robert Love85b4aa42008-12-09 15:10:24 -08001306 if (unlikely(FC_FCOE_DECAPS_VER(hp) != FC_FCOE_VER)) {
Robert Love582b45b2009-03-31 15:51:50 -07001307 if (stats->ErrorFrames < 5)
Robert Loved5488eb2009-06-10 15:30:59 -07001308 printk(KERN_WARNING "fcoe: FCoE version "
Robert Love582b45b2009-03-31 15:51:50 -07001309 "mismatch: The frame has "
1310 "version %x, but the "
1311 "initiator supports version "
1312 "%x\n", FC_FCOE_DECAPS_VER(hp),
1313 FC_FCOE_VER);
1314 stats->ErrorFrames++;
Robert Love85b4aa42008-12-09 15:10:24 -08001315 kfree_skb(skb);
1316 continue;
1317 }
1318
1319 skb_pull(skb, sizeof(struct fcoe_hdr));
1320 fr_len = skb->len - sizeof(struct fcoe_crc_eof);
1321
Robert Love582b45b2009-03-31 15:51:50 -07001322 stats->RxFrames++;
1323 stats->RxWords += fr_len / FCOE_WORD_TO_BYTE;
Robert Love85b4aa42008-12-09 15:10:24 -08001324
1325 fp = (struct fc_frame *)skb;
1326 fc_frame_init(fp);
1327 fr_dev(fp) = lp;
1328 fr_sof(fp) = hp->fcoe_sof;
1329
1330 /* Copy out the CRC and EOF trailer for access */
1331 if (skb_copy_bits(skb, fr_len, &crc_eof, sizeof(crc_eof))) {
1332 kfree_skb(skb);
1333 continue;
1334 }
1335 fr_eof(fp) = crc_eof.fcoe_eof;
1336 fr_crc(fp) = crc_eof.fcoe_crc32;
1337 if (pskb_trim(skb, fr_len)) {
1338 kfree_skb(skb);
1339 continue;
1340 }
1341
1342 /*
1343 * We only check CRC if no offload is available and if it is
1344 * it's solicited data, in which case, the FCP layer would
1345 * check it during the copy.
1346 */
Yi Zou07c00ec2009-02-27 14:07:31 -08001347 if (lp->crc_offload && skb->ip_summed == CHECKSUM_UNNECESSARY)
Robert Love85b4aa42008-12-09 15:10:24 -08001348 fr_flags(fp) &= ~FCPHF_CRC_UNCHECKED;
1349 else
1350 fr_flags(fp) |= FCPHF_CRC_UNCHECKED;
1351
1352 fh = fc_frame_header_get(fp);
1353 if (fh->fh_r_ctl == FC_RCTL_DD_SOL_DATA &&
1354 fh->fh_type == FC_TYPE_FCP) {
Vasu Dev52ff8782009-07-29 17:05:10 -07001355 fc_exch_recv(lp, fp);
Robert Love85b4aa42008-12-09 15:10:24 -08001356 continue;
1357 }
1358 if (fr_flags(fp) & FCPHF_CRC_UNCHECKED) {
1359 if (le32_to_cpu(fr_crc(fp)) !=
1360 ~crc32(~0, skb->data, fr_len)) {
Robert Loved5488eb2009-06-10 15:30:59 -07001361 if (stats->InvalidCRCCount < 5)
Robert Love85b4aa42008-12-09 15:10:24 -08001362 printk(KERN_WARNING "fcoe: dropping "
1363 "frame with CRC error\n");
1364 stats->InvalidCRCCount++;
1365 stats->ErrorFrames++;
1366 fc_frame_free(fp);
1367 continue;
1368 }
1369 fr_flags(fp) &= ~FCPHF_CRC_UNCHECKED;
1370 }
Joe Eykholt97c83892009-03-17 11:42:40 -07001371 if (unlikely(fc->ctlr.flogi_oxid != FC_XID_UNKNOWN) &&
1372 fcoe_ctlr_recv_flogi(&fc->ctlr, fp, mac)) {
1373 fc_frame_free(fp);
1374 continue;
1375 }
Vasu Dev52ff8782009-07-29 17:05:10 -07001376 fc_exch_recv(lp, fp);
Robert Love85b4aa42008-12-09 15:10:24 -08001377 }
1378 return 0;
1379}
1380
1381/**
Chris Leechdd3fd722009-04-21 16:27:36 -07001382 * fcoe_check_wait_queue() - attempt to clear the transmit backlog
1383 * @lp: the fc_lport
Robert Love85b4aa42008-12-09 15:10:24 -08001384 *
1385 * This empties the wait_queue, dequeue the head of the wait_queue queue
1386 * and calls fcoe_start_io() for each packet, if all skb have been
Vasu Devc826a312009-02-27 10:56:27 -08001387 * transmitted, return qlen or -1 if a error occurs, then restore
Chris Leechdd3fd722009-04-21 16:27:36 -07001388 * wait_queue and try again later.
Robert Love85b4aa42008-12-09 15:10:24 -08001389 *
1390 * The wait_queue is used when the skb transmit fails. skb will go
Chris Leechdd3fd722009-04-21 16:27:36 -07001391 * in the wait_queue which will be emptied by the timer function or
Robert Love85b4aa42008-12-09 15:10:24 -08001392 * by the next skb transmit.
Robert Love34f42a02009-02-27 10:55:45 -08001393 */
Vasu Dev4bb6b512009-05-06 10:52:34 -07001394static void fcoe_check_wait_queue(struct fc_lport *lp, struct sk_buff *skb)
Robert Love85b4aa42008-12-09 15:10:24 -08001395{
Chris Leech55c8baf2009-02-27 10:56:32 -08001396 struct fcoe_softc *fc = lport_priv(lp);
Vasu Dev4bb6b512009-05-06 10:52:34 -07001397 int rc;
Robert Love85b4aa42008-12-09 15:10:24 -08001398
Robert Love85b4aa42008-12-09 15:10:24 -08001399 spin_lock_bh(&fc->fcoe_pending_queue.lock);
Vasu Dev4bb6b512009-05-06 10:52:34 -07001400
1401 if (skb)
1402 __skb_queue_tail(&fc->fcoe_pending_queue, skb);
1403
Vasu Devc826a312009-02-27 10:56:27 -08001404 if (fc->fcoe_pending_queue_active)
1405 goto out;
1406 fc->fcoe_pending_queue_active = 1;
Chris Leech55c8baf2009-02-27 10:56:32 -08001407
1408 while (fc->fcoe_pending_queue.qlen) {
1409 /* keep qlen > 0 until fcoe_start_io succeeds */
1410 fc->fcoe_pending_queue.qlen++;
1411 skb = __skb_dequeue(&fc->fcoe_pending_queue);
1412
1413 spin_unlock_bh(&fc->fcoe_pending_queue.lock);
1414 rc = fcoe_start_io(skb);
1415 spin_lock_bh(&fc->fcoe_pending_queue.lock);
1416
1417 if (rc) {
1418 __skb_queue_head(&fc->fcoe_pending_queue, skb);
1419 /* undo temporary increment above */
1420 fc->fcoe_pending_queue.qlen--;
1421 break;
Robert Love85b4aa42008-12-09 15:10:24 -08001422 }
Chris Leech55c8baf2009-02-27 10:56:32 -08001423 /* undo temporary increment above */
1424 fc->fcoe_pending_queue.qlen--;
Robert Love85b4aa42008-12-09 15:10:24 -08001425 }
Chris Leech55c8baf2009-02-27 10:56:32 -08001426
1427 if (fc->fcoe_pending_queue.qlen < FCOE_LOW_QUEUE_DEPTH)
1428 lp->qfull = 0;
Vasu Dev1047f222009-05-06 10:52:40 -07001429 if (fc->fcoe_pending_queue.qlen && !timer_pending(&fc->timer))
1430 mod_timer(&fc->timer, jiffies + 2);
Vasu Devc826a312009-02-27 10:56:27 -08001431 fc->fcoe_pending_queue_active = 0;
Vasu Devc826a312009-02-27 10:56:27 -08001432out:
Vasu Dev4bb6b512009-05-06 10:52:34 -07001433 if (fc->fcoe_pending_queue.qlen > FCOE_MAX_QUEUE_DEPTH)
1434 lp->qfull = 1;
Robert Love85b4aa42008-12-09 15:10:24 -08001435 spin_unlock_bh(&fc->fcoe_pending_queue.lock);
Vasu Dev4bb6b512009-05-06 10:52:34 -07001436 return;
Robert Love85b4aa42008-12-09 15:10:24 -08001437}
1438
1439/**
Robert Love34f42a02009-02-27 10:55:45 -08001440 * fcoe_dev_setup() - setup link change notification interface
1441 */
Randy Dunlapb0d428a2009-04-27 21:49:31 -07001442static void fcoe_dev_setup(void)
Robert Love85b4aa42008-12-09 15:10:24 -08001443{
Robert Love85b4aa42008-12-09 15:10:24 -08001444 register_netdevice_notifier(&fcoe_notifier);
1445}
1446
1447/**
Randy Dunlapb0d428a2009-04-27 21:49:31 -07001448 * fcoe_dev_cleanup() - cleanup link change notification interface
Robert Love34f42a02009-02-27 10:55:45 -08001449 */
Robert Love85b4aa42008-12-09 15:10:24 -08001450static void fcoe_dev_cleanup(void)
1451{
1452 unregister_netdevice_notifier(&fcoe_notifier);
1453}
1454
1455/**
Robert Love34f42a02009-02-27 10:55:45 -08001456 * fcoe_device_notification() - netdev event notification callback
Robert Love85b4aa42008-12-09 15:10:24 -08001457 * @notifier: context of the notification
1458 * @event: type of event
1459 * @ptr: fixed array for output parsed ifname
1460 *
1461 * This function is called by the ethernet driver in case of link change event
1462 *
1463 * Returns: 0 for success
Robert Love34f42a02009-02-27 10:55:45 -08001464 */
Robert Love85b4aa42008-12-09 15:10:24 -08001465static int fcoe_device_notification(struct notifier_block *notifier,
1466 ulong event, void *ptr)
1467{
1468 struct fc_lport *lp = NULL;
Vasu Dev1d1b88d2009-07-29 17:05:45 -07001469 struct net_device *netdev = ptr;
Robert Love85b4aa42008-12-09 15:10:24 -08001470 struct fcoe_softc *fc;
1471 struct fcoe_dev_stats *stats;
Joe Eykholt97c83892009-03-17 11:42:40 -07001472 u32 link_possible = 1;
Robert Love85b4aa42008-12-09 15:10:24 -08001473 u32 mfs;
1474 int rc = NOTIFY_OK;
1475
1476 read_lock(&fcoe_hostlist_lock);
1477 list_for_each_entry(fc, &fcoe_hostlist, list) {
Vasu Dev1d1b88d2009-07-29 17:05:45 -07001478 if (fc->netdev == netdev) {
Joe Eykholt97c83892009-03-17 11:42:40 -07001479 lp = fc->ctlr.lp;
Robert Love85b4aa42008-12-09 15:10:24 -08001480 break;
1481 }
1482 }
1483 read_unlock(&fcoe_hostlist_lock);
1484 if (lp == NULL) {
1485 rc = NOTIFY_DONE;
1486 goto out;
1487 }
1488
Robert Love85b4aa42008-12-09 15:10:24 -08001489 switch (event) {
1490 case NETDEV_DOWN:
1491 case NETDEV_GOING_DOWN:
Joe Eykholt97c83892009-03-17 11:42:40 -07001492 link_possible = 0;
Robert Love85b4aa42008-12-09 15:10:24 -08001493 break;
1494 case NETDEV_UP:
1495 case NETDEV_CHANGE:
Robert Love85b4aa42008-12-09 15:10:24 -08001496 break;
1497 case NETDEV_CHANGEMTU:
Vasu Dev1d1b88d2009-07-29 17:05:45 -07001498 mfs = netdev->mtu - (sizeof(struct fcoe_hdr) +
1499 sizeof(struct fcoe_crc_eof));
Robert Love85b4aa42008-12-09 15:10:24 -08001500 if (mfs >= FC_MIN_MAX_FRAME)
1501 fc_set_mfs(lp, mfs);
Robert Love85b4aa42008-12-09 15:10:24 -08001502 break;
1503 case NETDEV_REGISTER:
1504 break;
1505 default:
Vasu Dev1d1b88d2009-07-29 17:05:45 -07001506 FCOE_NETDEV_DBG(netdev, "Unknown event %ld "
Robert Loved5488eb2009-06-10 15:30:59 -07001507 "from netdev netlink\n", event);
Robert Love85b4aa42008-12-09 15:10:24 -08001508 }
Joe Eykholt97c83892009-03-17 11:42:40 -07001509 if (link_possible && !fcoe_link_ok(lp))
1510 fcoe_ctlr_link_up(&fc->ctlr);
1511 else if (fcoe_ctlr_link_down(&fc->ctlr)) {
1512 stats = fc_lport_get_stats(lp);
1513 stats->LinkFailureCount++;
1514 fcoe_clean_pending_queue(lp);
Robert Love85b4aa42008-12-09 15:10:24 -08001515 }
1516out:
1517 return rc;
1518}
1519
1520/**
Robert Love34f42a02009-02-27 10:55:45 -08001521 * fcoe_if_to_netdev() - parse a name buffer to get netdev
Robert Love85b4aa42008-12-09 15:10:24 -08001522 * @buffer: incoming buffer to be copied
1523 *
Chris Leechdd3fd722009-04-21 16:27:36 -07001524 * Returns: NULL or ptr to net_device
Robert Love34f42a02009-02-27 10:55:45 -08001525 */
Robert Love85b4aa42008-12-09 15:10:24 -08001526static struct net_device *fcoe_if_to_netdev(const char *buffer)
1527{
1528 char *cp;
1529 char ifname[IFNAMSIZ + 2];
1530
1531 if (buffer) {
1532 strlcpy(ifname, buffer, IFNAMSIZ);
1533 cp = ifname + strlen(ifname);
1534 while (--cp >= ifname && *cp == '\n')
1535 *cp = '\0';
1536 return dev_get_by_name(&init_net, ifname);
1537 }
1538 return NULL;
1539}
1540
1541/**
Chris Leechdd3fd722009-04-21 16:27:36 -07001542 * fcoe_netdev_to_module_owner() - finds out the driver module of the netdev
Robert Love85b4aa42008-12-09 15:10:24 -08001543 * @netdev: the target netdev
1544 *
1545 * Returns: ptr to the struct module, NULL for failure
Robert Love34f42a02009-02-27 10:55:45 -08001546 */
Robert Loveb2ab99c2009-02-27 10:55:50 -08001547static struct module *
1548fcoe_netdev_to_module_owner(const struct net_device *netdev)
Robert Love85b4aa42008-12-09 15:10:24 -08001549{
1550 struct device *dev;
1551
1552 if (!netdev)
1553 return NULL;
1554
1555 dev = netdev->dev.parent;
1556 if (!dev)
1557 return NULL;
1558
1559 if (!dev->driver)
1560 return NULL;
1561
1562 return dev->driver->owner;
1563}
1564
1565/**
Robert Love34f42a02009-02-27 10:55:45 -08001566 * fcoe_ethdrv_get() - Hold the Ethernet driver
Robert Love85b4aa42008-12-09 15:10:24 -08001567 * @netdev: the target netdev
1568 *
Robert Love34f42a02009-02-27 10:55:45 -08001569 * Holds the Ethernet driver module by try_module_get() for
1570 * the corresponding netdev.
1571 *
Chris Leechdd3fd722009-04-21 16:27:36 -07001572 * Returns: 0 for success
Robert Love34f42a02009-02-27 10:55:45 -08001573 */
Robert Love85b4aa42008-12-09 15:10:24 -08001574static int fcoe_ethdrv_get(const struct net_device *netdev)
1575{
1576 struct module *owner;
1577
1578 owner = fcoe_netdev_to_module_owner(netdev);
1579 if (owner) {
Robert Loved5488eb2009-06-10 15:30:59 -07001580 FCOE_NETDEV_DBG(netdev, "Hold driver module %s\n",
1581 module_name(owner));
Robert Love85b4aa42008-12-09 15:10:24 -08001582 return try_module_get(owner);
1583 }
1584 return -ENODEV;
1585}
1586
1587/**
Robert Love34f42a02009-02-27 10:55:45 -08001588 * fcoe_ethdrv_put() - Release the Ethernet driver
Robert Love85b4aa42008-12-09 15:10:24 -08001589 * @netdev: the target netdev
1590 *
Robert Love34f42a02009-02-27 10:55:45 -08001591 * Releases the Ethernet driver module by module_put for
1592 * the corresponding netdev.
1593 *
Chris Leechdd3fd722009-04-21 16:27:36 -07001594 * Returns: 0 for success
Robert Love34f42a02009-02-27 10:55:45 -08001595 */
Robert Love85b4aa42008-12-09 15:10:24 -08001596static int fcoe_ethdrv_put(const struct net_device *netdev)
1597{
1598 struct module *owner;
1599
1600 owner = fcoe_netdev_to_module_owner(netdev);
1601 if (owner) {
Robert Loved5488eb2009-06-10 15:30:59 -07001602 FCOE_NETDEV_DBG(netdev, "Release driver module %s\n",
1603 module_name(owner));
Robert Love85b4aa42008-12-09 15:10:24 -08001604 module_put(owner);
1605 return 0;
1606 }
1607 return -ENODEV;
1608}
1609
1610/**
Robert Love34f42a02009-02-27 10:55:45 -08001611 * fcoe_destroy() - handles the destroy from sysfs
Chris Leechdd3fd722009-04-21 16:27:36 -07001612 * @buffer: expected to be an eth if name
Robert Love85b4aa42008-12-09 15:10:24 -08001613 * @kp: associated kernel param
1614 *
1615 * Returns: 0 for success
Robert Love34f42a02009-02-27 10:55:45 -08001616 */
Robert Love85b4aa42008-12-09 15:10:24 -08001617static int fcoe_destroy(const char *buffer, struct kernel_param *kp)
1618{
1619 int rc;
1620 struct net_device *netdev;
1621
1622 netdev = fcoe_if_to_netdev(buffer);
1623 if (!netdev) {
1624 rc = -ENODEV;
1625 goto out_nodev;
1626 }
1627 /* look for existing lport */
1628 if (!fcoe_hostlist_lookup(netdev)) {
1629 rc = -ENODEV;
1630 goto out_putdev;
1631 }
Vasu Dev7f349142009-03-27 09:06:31 -07001632 rc = fcoe_if_destroy(netdev);
Robert Love85b4aa42008-12-09 15:10:24 -08001633 if (rc) {
Robert Loved5488eb2009-06-10 15:30:59 -07001634 printk(KERN_ERR "fcoe: Failed to destroy interface (%s)\n",
Robert Love85b4aa42008-12-09 15:10:24 -08001635 netdev->name);
1636 rc = -EIO;
1637 goto out_putdev;
1638 }
1639 fcoe_ethdrv_put(netdev);
1640 rc = 0;
1641out_putdev:
1642 dev_put(netdev);
1643out_nodev:
1644 return rc;
1645}
1646
1647/**
Robert Love34f42a02009-02-27 10:55:45 -08001648 * fcoe_create() - Handles the create call from sysfs
Chris Leechdd3fd722009-04-21 16:27:36 -07001649 * @buffer: expected to be an eth if name
Robert Love85b4aa42008-12-09 15:10:24 -08001650 * @kp: associated kernel param
1651 *
1652 * Returns: 0 for success
Robert Love34f42a02009-02-27 10:55:45 -08001653 */
Robert Love85b4aa42008-12-09 15:10:24 -08001654static int fcoe_create(const char *buffer, struct kernel_param *kp)
1655{
1656 int rc;
1657 struct net_device *netdev;
1658
1659 netdev = fcoe_if_to_netdev(buffer);
1660 if (!netdev) {
1661 rc = -ENODEV;
1662 goto out_nodev;
1663 }
1664 /* look for existing lport */
1665 if (fcoe_hostlist_lookup(netdev)) {
1666 rc = -EEXIST;
1667 goto out_putdev;
1668 }
1669 fcoe_ethdrv_get(netdev);
1670
Vasu Dev7f349142009-03-27 09:06:31 -07001671 rc = fcoe_if_create(netdev);
Robert Love85b4aa42008-12-09 15:10:24 -08001672 if (rc) {
Robert Loved5488eb2009-06-10 15:30:59 -07001673 printk(KERN_ERR "fcoe: Failed to create interface (%s)\n",
Robert Love85b4aa42008-12-09 15:10:24 -08001674 netdev->name);
1675 fcoe_ethdrv_put(netdev);
1676 rc = -EIO;
1677 goto out_putdev;
1678 }
1679 rc = 0;
1680out_putdev:
1681 dev_put(netdev);
1682out_nodev:
1683 return rc;
1684}
1685
1686module_param_call(create, fcoe_create, NULL, NULL, S_IWUSR);
1687__MODULE_PARM_TYPE(create, "string");
1688MODULE_PARM_DESC(create, "Create fcoe port using net device passed in.");
1689module_param_call(destroy, fcoe_destroy, NULL, NULL, S_IWUSR);
1690__MODULE_PARM_TYPE(destroy, "string");
1691MODULE_PARM_DESC(destroy, "Destroy fcoe port");
1692
Robert Love34f42a02009-02-27 10:55:45 -08001693/**
1694 * fcoe_link_ok() - Check if link is ok for the fc_lport
Robert Love85b4aa42008-12-09 15:10:24 -08001695 * @lp: ptr to the fc_lport
1696 *
1697 * Any permanently-disqualifying conditions have been previously checked.
1698 * This also updates the speed setting, which may change with link for 100/1000.
1699 *
1700 * This function should probably be checking for PAUSE support at some point
1701 * in the future. Currently Per-priority-pause is not determinable using
1702 * ethtool, so we shouldn't be restrictive until that problem is resolved.
1703 *
1704 * Returns: 0 if link is OK for use by FCoE.
1705 *
1706 */
1707int fcoe_link_ok(struct fc_lport *lp)
1708{
Robert Lovefc47ff62009-02-27 10:55:55 -08001709 struct fcoe_softc *fc = lport_priv(lp);
Vasu Dev1d1b88d2009-07-29 17:05:45 -07001710 struct net_device *dev = fc->netdev;
Robert Love85b4aa42008-12-09 15:10:24 -08001711 struct ethtool_cmd ecmd = { ETHTOOL_GSET };
Robert Love85b4aa42008-12-09 15:10:24 -08001712
Yi Zou2f718d62009-07-29 17:04:01 -07001713 if ((dev->flags & IFF_UP) && netif_carrier_ok(dev) &&
1714 (!dev_ethtool_get_settings(dev, &ecmd))) {
1715 lp->link_supported_speeds &=
1716 ~(FC_PORTSPEED_1GBIT | FC_PORTSPEED_10GBIT);
1717 if (ecmd.supported & (SUPPORTED_1000baseT_Half |
1718 SUPPORTED_1000baseT_Full))
1719 lp->link_supported_speeds |= FC_PORTSPEED_1GBIT;
1720 if (ecmd.supported & SUPPORTED_10000baseT_Full)
1721 lp->link_supported_speeds |=
1722 FC_PORTSPEED_10GBIT;
1723 if (ecmd.speed == SPEED_1000)
1724 lp->link_speed = FC_PORTSPEED_1GBIT;
1725 if (ecmd.speed == SPEED_10000)
1726 lp->link_speed = FC_PORTSPEED_10GBIT;
Robert Love85b4aa42008-12-09 15:10:24 -08001727
Yi Zou2f718d62009-07-29 17:04:01 -07001728 return 0;
1729 }
1730 return -1;
Robert Love85b4aa42008-12-09 15:10:24 -08001731}
Robert Love85b4aa42008-12-09 15:10:24 -08001732
Robert Love34f42a02009-02-27 10:55:45 -08001733/**
1734 * fcoe_percpu_clean() - Clear the pending skbs for an lport
Robert Love85b4aa42008-12-09 15:10:24 -08001735 * @lp: the fc_lport
1736 */
1737void fcoe_percpu_clean(struct fc_lport *lp)
1738{
Robert Love85b4aa42008-12-09 15:10:24 -08001739 struct fcoe_percpu_s *pp;
1740 struct fcoe_rcv_info *fr;
1741 struct sk_buff_head *list;
1742 struct sk_buff *skb, *next;
1743 struct sk_buff *head;
Robert Love5e5e92d2009-03-17 11:41:35 -07001744 unsigned int cpu;
Robert Love85b4aa42008-12-09 15:10:24 -08001745
Robert Love5e5e92d2009-03-17 11:41:35 -07001746 for_each_possible_cpu(cpu) {
1747 pp = &per_cpu(fcoe_percpu, cpu);
1748 spin_lock_bh(&pp->fcoe_rx_list.lock);
1749 list = &pp->fcoe_rx_list;
1750 head = list->next;
1751 for (skb = head; skb != (struct sk_buff *)list;
1752 skb = next) {
1753 next = skb->next;
1754 fr = fcoe_dev_from_skb(skb);
1755 if (fr->fr_dev == lp) {
1756 __skb_unlink(skb, list);
1757 kfree_skb(skb);
Robert Love85b4aa42008-12-09 15:10:24 -08001758 }
Robert Love85b4aa42008-12-09 15:10:24 -08001759 }
Robert Love5e5e92d2009-03-17 11:41:35 -07001760 spin_unlock_bh(&pp->fcoe_rx_list.lock);
Robert Love85b4aa42008-12-09 15:10:24 -08001761 }
1762}
Robert Love85b4aa42008-12-09 15:10:24 -08001763
1764/**
Robert Love34f42a02009-02-27 10:55:45 -08001765 * fcoe_clean_pending_queue() - Dequeue a skb and free it
Robert Love85b4aa42008-12-09 15:10:24 -08001766 * @lp: the corresponding fc_lport
1767 *
1768 * Returns: none
Robert Love34f42a02009-02-27 10:55:45 -08001769 */
Robert Love85b4aa42008-12-09 15:10:24 -08001770void fcoe_clean_pending_queue(struct fc_lport *lp)
1771{
1772 struct fcoe_softc *fc = lport_priv(lp);
1773 struct sk_buff *skb;
1774
1775 spin_lock_bh(&fc->fcoe_pending_queue.lock);
1776 while ((skb = __skb_dequeue(&fc->fcoe_pending_queue)) != NULL) {
1777 spin_unlock_bh(&fc->fcoe_pending_queue.lock);
1778 kfree_skb(skb);
1779 spin_lock_bh(&fc->fcoe_pending_queue.lock);
1780 }
1781 spin_unlock_bh(&fc->fcoe_pending_queue.lock);
1782}
Robert Love85b4aa42008-12-09 15:10:24 -08001783
1784/**
Robert Love34f42a02009-02-27 10:55:45 -08001785 * fcoe_reset() - Resets the fcoe
Robert Love85b4aa42008-12-09 15:10:24 -08001786 * @shost: shost the reset is from
1787 *
1788 * Returns: always 0
1789 */
1790int fcoe_reset(struct Scsi_Host *shost)
1791{
1792 struct fc_lport *lport = shost_priv(shost);
1793 fc_lport_reset(lport);
1794 return 0;
1795}
Robert Love85b4aa42008-12-09 15:10:24 -08001796
Robert Love34f42a02009-02-27 10:55:45 -08001797/**
Robert Love34f42a02009-02-27 10:55:45 -08001798 * fcoe_hostlist_lookup_softc() - find the corresponding lport by a given device
Chris Leechdd3fd722009-04-21 16:27:36 -07001799 * @dev: this is currently ptr to net_device
Robert Love85b4aa42008-12-09 15:10:24 -08001800 *
Vasu Deve8af4d42009-07-29 17:05:15 -07001801 * Called with fcoe_hostlist_lock held.
1802 *
Robert Love85b4aa42008-12-09 15:10:24 -08001803 * Returns: NULL or the located fcoe_softc
1804 */
Robert Loveb2ab99c2009-02-27 10:55:50 -08001805static struct fcoe_softc *
1806fcoe_hostlist_lookup_softc(const struct net_device *dev)
Robert Love85b4aa42008-12-09 15:10:24 -08001807{
1808 struct fcoe_softc *fc;
1809
Robert Love85b4aa42008-12-09 15:10:24 -08001810 list_for_each_entry(fc, &fcoe_hostlist, list) {
Vasu Dev1d1b88d2009-07-29 17:05:45 -07001811 if (fc->netdev == dev)
Robert Love85b4aa42008-12-09 15:10:24 -08001812 return fc;
Robert Love85b4aa42008-12-09 15:10:24 -08001813 }
Robert Love85b4aa42008-12-09 15:10:24 -08001814 return NULL;
1815}
1816
Robert Love34f42a02009-02-27 10:55:45 -08001817/**
1818 * fcoe_hostlist_lookup() - Find the corresponding lport by netdev
Robert Love85b4aa42008-12-09 15:10:24 -08001819 * @netdev: ptr to net_device
1820 *
1821 * Returns: 0 for success
1822 */
1823struct fc_lport *fcoe_hostlist_lookup(const struct net_device *netdev)
1824{
1825 struct fcoe_softc *fc;
1826
Vasu Deve8af4d42009-07-29 17:05:15 -07001827 read_lock(&fcoe_hostlist_lock);
Robert Love85b4aa42008-12-09 15:10:24 -08001828 fc = fcoe_hostlist_lookup_softc(netdev);
Vasu Deve8af4d42009-07-29 17:05:15 -07001829 read_unlock(&fcoe_hostlist_lock);
Robert Love85b4aa42008-12-09 15:10:24 -08001830
Joe Eykholt97c83892009-03-17 11:42:40 -07001831 return (fc) ? fc->ctlr.lp : NULL;
Robert Love85b4aa42008-12-09 15:10:24 -08001832}
Robert Love85b4aa42008-12-09 15:10:24 -08001833
Robert Love34f42a02009-02-27 10:55:45 -08001834/**
1835 * fcoe_hostlist_add() - Add a lport to lports list
Chris Leechdd3fd722009-04-21 16:27:36 -07001836 * @lp: ptr to the fc_lport to be added
Robert Love85b4aa42008-12-09 15:10:24 -08001837 *
Vasu Deve8af4d42009-07-29 17:05:15 -07001838 * Called with write fcoe_hostlist_lock held.
1839 *
Robert Love85b4aa42008-12-09 15:10:24 -08001840 * Returns: 0 for success
1841 */
1842int fcoe_hostlist_add(const struct fc_lport *lp)
1843{
1844 struct fcoe_softc *fc;
1845
1846 fc = fcoe_hostlist_lookup_softc(fcoe_netdev(lp));
1847 if (!fc) {
Robert Lovefc47ff62009-02-27 10:55:55 -08001848 fc = lport_priv(lp);
Robert Love85b4aa42008-12-09 15:10:24 -08001849 list_add_tail(&fc->list, &fcoe_hostlist);
Robert Love85b4aa42008-12-09 15:10:24 -08001850 }
1851 return 0;
1852}
Robert Love85b4aa42008-12-09 15:10:24 -08001853
Robert Love34f42a02009-02-27 10:55:45 -08001854/**
1855 * fcoe_hostlist_remove() - remove a lport from lports list
Chris Leechdd3fd722009-04-21 16:27:36 -07001856 * @lp: ptr to the fc_lport to be removed
Robert Love85b4aa42008-12-09 15:10:24 -08001857 *
1858 * Returns: 0 for success
1859 */
1860int fcoe_hostlist_remove(const struct fc_lport *lp)
1861{
1862 struct fcoe_softc *fc;
1863
Vasu Deve8af4d42009-07-29 17:05:15 -07001864 write_lock_bh(&fcoe_hostlist_lock);
Robert Love85b4aa42008-12-09 15:10:24 -08001865 fc = fcoe_hostlist_lookup_softc(fcoe_netdev(lp));
1866 BUG_ON(!fc);
Robert Love85b4aa42008-12-09 15:10:24 -08001867 list_del(&fc->list);
1868 write_unlock_bh(&fcoe_hostlist_lock);
1869
1870 return 0;
1871}
Robert Love85b4aa42008-12-09 15:10:24 -08001872
1873/**
Robert Love34f42a02009-02-27 10:55:45 -08001874 * fcoe_init() - fcoe module loading initialization
Robert Love85b4aa42008-12-09 15:10:24 -08001875 *
Robert Love85b4aa42008-12-09 15:10:24 -08001876 * Returns 0 on success, negative on failure
Robert Love34f42a02009-02-27 10:55:45 -08001877 */
Robert Love85b4aa42008-12-09 15:10:24 -08001878static int __init fcoe_init(void)
1879{
Robert Love38eccab2009-03-17 11:41:30 -07001880 unsigned int cpu;
Robert Love8976f422009-03-17 11:41:46 -07001881 int rc = 0;
Robert Love85b4aa42008-12-09 15:10:24 -08001882 struct fcoe_percpu_s *p;
1883
Robert Love85b4aa42008-12-09 15:10:24 -08001884 INIT_LIST_HEAD(&fcoe_hostlist);
1885 rwlock_init(&fcoe_hostlist_lock);
1886
Robert Love38eccab2009-03-17 11:41:30 -07001887 for_each_possible_cpu(cpu) {
Robert Love5e5e92d2009-03-17 11:41:35 -07001888 p = &per_cpu(fcoe_percpu, cpu);
Robert Love38eccab2009-03-17 11:41:30 -07001889 skb_queue_head_init(&p->fcoe_rx_list);
1890 }
1891
Robert Love8976f422009-03-17 11:41:46 -07001892 for_each_online_cpu(cpu)
1893 fcoe_percpu_thread_create(cpu);
Robert Love85b4aa42008-12-09 15:10:24 -08001894
Robert Love8976f422009-03-17 11:41:46 -07001895 /* Initialize per CPU interrupt thread */
1896 rc = register_hotcpu_notifier(&fcoe_cpu_notifier);
1897 if (rc)
1898 goto out_free;
Robert Love85b4aa42008-12-09 15:10:24 -08001899
Robert Love8976f422009-03-17 11:41:46 -07001900 /* Setup link change notification */
Robert Love85b4aa42008-12-09 15:10:24 -08001901 fcoe_dev_setup();
1902
Vasu Dev7f349142009-03-27 09:06:31 -07001903 fcoe_if_init();
Robert Love85b4aa42008-12-09 15:10:24 -08001904
1905 return 0;
Robert Love8976f422009-03-17 11:41:46 -07001906
1907out_free:
1908 for_each_online_cpu(cpu) {
1909 fcoe_percpu_thread_destroy(cpu);
1910 }
1911
1912 return rc;
Robert Love85b4aa42008-12-09 15:10:24 -08001913}
1914module_init(fcoe_init);
1915
1916/**
Robert Love34f42a02009-02-27 10:55:45 -08001917 * fcoe_exit() - fcoe module unloading cleanup
Robert Love85b4aa42008-12-09 15:10:24 -08001918 *
1919 * Returns 0 on success, negative on failure
Robert Love34f42a02009-02-27 10:55:45 -08001920 */
Robert Love85b4aa42008-12-09 15:10:24 -08001921static void __exit fcoe_exit(void)
1922{
Robert Love5e5e92d2009-03-17 11:41:35 -07001923 unsigned int cpu;
Robert Love85b4aa42008-12-09 15:10:24 -08001924 struct fcoe_softc *fc, *tmp;
Robert Love85b4aa42008-12-09 15:10:24 -08001925
Robert Love85b4aa42008-12-09 15:10:24 -08001926 fcoe_dev_cleanup();
1927
Vasu Dev5919a592009-03-27 09:03:29 -07001928 /* releases the associated fcoe hosts */
Robert Love85b4aa42008-12-09 15:10:24 -08001929 list_for_each_entry_safe(fc, tmp, &fcoe_hostlist, list)
Vasu Dev1d1b88d2009-07-29 17:05:45 -07001930 fcoe_if_destroy(fc->netdev);
Robert Love85b4aa42008-12-09 15:10:24 -08001931
Robert Love8976f422009-03-17 11:41:46 -07001932 unregister_hotcpu_notifier(&fcoe_cpu_notifier);
1933
1934 for_each_online_cpu(cpu) {
1935 fcoe_percpu_thread_destroy(cpu);
Robert Love85b4aa42008-12-09 15:10:24 -08001936 }
1937
Vasu Dev7f349142009-03-27 09:06:31 -07001938 /* detach from scsi transport */
1939 fcoe_if_exit();
Robert Love85b4aa42008-12-09 15:10:24 -08001940}
1941module_exit(fcoe_exit);