blob: 749279698137a61ee48c1e41c1220733ee7b7ab5 [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{
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
Chris Leechaf7f85d2009-08-25 13:59:24 -0700509 * @lport: fc_lport to destroy
Vasu Dev7f349142009-03-27 09:06:31 -0700510 */
Chris Leechaf7f85d2009-08-25 13:59:24 -0700511static void fcoe_if_destroy(struct fc_lport *lport)
Vasu Dev7f349142009-03-27 09:06:31 -0700512{
Chris Leechaf7f85d2009-08-25 13:59:24 -0700513 struct fcoe_softc *fc = lport_priv(lport);
514 struct net_device *netdev = fc->netdev;
Vasu Dev7f349142009-03-27 09:06:31 -0700515
Robert Loved5488eb2009-06-10 15:30:59 -0700516 FCOE_NETDEV_DBG(netdev, "Destroying interface\n");
Vasu Dev7f349142009-03-27 09:06:31 -0700517
Vasu Dev7f349142009-03-27 09:06:31 -0700518 /* Logout of the fabric */
Chris Leechaf7f85d2009-08-25 13:59:24 -0700519 fc_fabric_logoff(lport);
Vasu Dev7f349142009-03-27 09:06:31 -0700520
521 /* Remove the instance from fcoe's list */
Chris Leechaf7f85d2009-08-25 13:59:24 -0700522 fcoe_hostlist_remove(lport);
Vasu Dev7f349142009-03-27 09:06:31 -0700523
Vasu Devab6b85c2009-05-17 12:33:08 +0000524 /* clean up netdev configurations */
525 fcoe_netdev_cleanup(fc);
526
527 /* tear-down the FCoE controller */
Joe Eykholt97c83892009-03-17 11:42:40 -0700528 fcoe_ctlr_destroy(&fc->ctlr);
Vasu Dev7f349142009-03-27 09:06:31 -0700529
Joe Eykholtf161fb72009-07-29 17:04:17 -0700530 /* Free queued packets for the per-CPU receive threads */
Chris Leechaf7f85d2009-08-25 13:59:24 -0700531 fcoe_percpu_clean(lport);
Joe Eykholtf161fb72009-07-29 17:04:17 -0700532
Vasu Dev7f349142009-03-27 09:06:31 -0700533 /* Cleanup the fc_lport */
Chris Leechaf7f85d2009-08-25 13:59:24 -0700534 fc_lport_destroy(lport);
535 fc_fcp_destroy(lport);
Vasu Dev7f349142009-03-27 09:06:31 -0700536
537 /* Detach from the scsi-ml */
Chris Leechaf7f85d2009-08-25 13:59:24 -0700538 fc_remove_host(lport->host);
539 scsi_remove_host(lport->host);
Vasu Dev7f349142009-03-27 09:06:31 -0700540
541 /* There are no more rports or I/O, free the EM */
Chris Leechaf7f85d2009-08-25 13:59:24 -0700542 fc_exch_mgr_free(lport);
Vasu Dev7f349142009-03-27 09:06:31 -0700543
Vasu Dev7f349142009-03-27 09:06:31 -0700544 /* Free existing skbs */
Chris Leechaf7f85d2009-08-25 13:59:24 -0700545 fcoe_clean_pending_queue(lport);
Vasu Dev7f349142009-03-27 09:06:31 -0700546
Vasu Dev1047f222009-05-06 10:52:40 -0700547 /* Stop the timer */
548 del_timer_sync(&fc->timer);
549
Vasu Dev7f349142009-03-27 09:06:31 -0700550 /* Free memory used by statistical counters */
Chris Leechaf7f85d2009-08-25 13:59:24 -0700551 fc_lport_free_stats(lport);
Vasu Dev7f349142009-03-27 09:06:31 -0700552
553 /* Release the net_device and Scsi_Host */
Vasu Dev1d1b88d2009-07-29 17:05:45 -0700554 dev_put(netdev);
Chris Leechaf7f85d2009-08-25 13:59:24 -0700555 scsi_host_put(lport->host);
Vasu Dev7f349142009-03-27 09:06:31 -0700556}
557
558/*
559 * fcoe_ddp_setup - calls LLD's ddp_setup through net_device
560 * @lp: the corresponding fc_lport
561 * @xid: the exchange id for this ddp transfer
562 * @sgl: the scatterlist describing this transfer
563 * @sgc: number of sg items
564 *
565 * Returns : 0 no ddp
566 */
567static int fcoe_ddp_setup(struct fc_lport *lp, u16 xid,
568 struct scatterlist *sgl, unsigned int sgc)
569{
570 struct net_device *n = fcoe_netdev(lp);
571
572 if (n->netdev_ops && n->netdev_ops->ndo_fcoe_ddp_setup)
573 return n->netdev_ops->ndo_fcoe_ddp_setup(n, xid, sgl, sgc);
574
575 return 0;
576}
577
578/*
579 * fcoe_ddp_done - calls LLD's ddp_done through net_device
580 * @lp: the corresponding fc_lport
581 * @xid: the exchange id for this ddp transfer
582 *
583 * Returns : the length of data that have been completed by ddp
584 */
585static int fcoe_ddp_done(struct fc_lport *lp, u16 xid)
586{
587 struct net_device *n = fcoe_netdev(lp);
588
589 if (n->netdev_ops && n->netdev_ops->ndo_fcoe_ddp_done)
590 return n->netdev_ops->ndo_fcoe_ddp_done(n, xid);
591 return 0;
592}
593
594static struct libfc_function_template fcoe_libfc_fcn_templ = {
595 .frame_send = fcoe_xmit,
596 .ddp_setup = fcoe_ddp_setup,
597 .ddp_done = fcoe_ddp_done,
598};
599
600/**
601 * fcoe_if_create() - this function creates the fcoe interface
602 * @netdev: pointer the associated netdevice
Chris Leechaf7f85d2009-08-25 13:59:24 -0700603 * @parent: device pointer to be the parent in sysfs for the SCSI host
Vasu Dev7f349142009-03-27 09:06:31 -0700604 *
605 * Creates fc_lport struct and scsi_host for lport, configures lport
606 * and starts fabric login.
607 *
Chris Leechaf7f85d2009-08-25 13:59:24 -0700608 * Returns : The allocated fc_lport or an error pointer
Vasu Dev7f349142009-03-27 09:06:31 -0700609 */
Chris Leechaf7f85d2009-08-25 13:59:24 -0700610static struct fc_lport *fcoe_if_create(struct net_device *netdev,
611 struct device *parent)
Vasu Dev7f349142009-03-27 09:06:31 -0700612{
613 int rc;
Chris Leechaf7f85d2009-08-25 13:59:24 -0700614 struct fc_lport *lport = NULL;
Vasu Dev7f349142009-03-27 09:06:31 -0700615 struct fcoe_softc *fc;
616 struct Scsi_Host *shost;
617
Robert Loved5488eb2009-06-10 15:30:59 -0700618 FCOE_NETDEV_DBG(netdev, "Create Interface\n");
Vasu Dev7f349142009-03-27 09:06:31 -0700619
Vasu Deva0a25da2009-03-17 11:42:29 -0700620 shost = libfc_host_alloc(&fcoe_shost_template,
621 sizeof(struct fcoe_softc));
Vasu Dev7f349142009-03-27 09:06:31 -0700622 if (!shost) {
Robert Loved5488eb2009-06-10 15:30:59 -0700623 FCOE_NETDEV_DBG(netdev, "Could not allocate host structure\n");
Chris Leechaf7f85d2009-08-25 13:59:24 -0700624 rc = -ENOMEM;
625 goto out;
Vasu Dev7f349142009-03-27 09:06:31 -0700626 }
Chris Leechaf7f85d2009-08-25 13:59:24 -0700627 lport = shost_priv(shost);
628 fc = lport_priv(lport);
Vasu Dev7f349142009-03-27 09:06:31 -0700629
630 /* configure fc_lport, e.g., em */
Chris Leechaf7f85d2009-08-25 13:59:24 -0700631 rc = fcoe_lport_config(lport);
Vasu Dev7f349142009-03-27 09:06:31 -0700632 if (rc) {
Robert Loved5488eb2009-06-10 15:30:59 -0700633 FCOE_NETDEV_DBG(netdev, "Could not configure lport for the "
634 "interface\n");
Vasu Dev7f349142009-03-27 09:06:31 -0700635 goto out_host_put;
636 }
637
Joe Eykholt97c83892009-03-17 11:42:40 -0700638 /*
639 * Initialize FIP.
640 */
641 fcoe_ctlr_init(&fc->ctlr);
642 fc->ctlr.send = fcoe_fip_send;
643 fc->ctlr.update_mac = fcoe_update_src_mac;
644
Vasu Devab6b85c2009-05-17 12:33:08 +0000645 /* configure lport network properties */
Chris Leechaf7f85d2009-08-25 13:59:24 -0700646 rc = fcoe_netdev_config(lport, netdev);
Vasu Devab6b85c2009-05-17 12:33:08 +0000647 if (rc) {
Robert Loved5488eb2009-06-10 15:30:59 -0700648 FCOE_NETDEV_DBG(netdev, "Could not configure netdev for the "
649 "interface\n");
Vasu Devab6b85c2009-05-17 12:33:08 +0000650 goto out_netdev_cleanup;
651 }
Joe Eykholt97c83892009-03-17 11:42:40 -0700652
Vasu Dev7f349142009-03-27 09:06:31 -0700653 /* configure lport scsi host properties */
Chris Leechaf7f85d2009-08-25 13:59:24 -0700654 rc = fcoe_shost_config(lport, shost, parent);
Vasu Dev7f349142009-03-27 09:06:31 -0700655 if (rc) {
Robert Loved5488eb2009-06-10 15:30:59 -0700656 FCOE_NETDEV_DBG(netdev, "Could not configure shost for the "
657 "interface\n");
Vasu Devab6b85c2009-05-17 12:33:08 +0000658 goto out_netdev_cleanup;
Vasu Dev7f349142009-03-27 09:06:31 -0700659 }
660
Vasu Dev7f349142009-03-27 09:06:31 -0700661 /* Initialize the library */
Chris Leechaf7f85d2009-08-25 13:59:24 -0700662 rc = fcoe_libfc_config(lport, &fcoe_libfc_fcn_templ);
Vasu Dev7f349142009-03-27 09:06:31 -0700663 if (rc) {
Robert Loved5488eb2009-06-10 15:30:59 -0700664 FCOE_NETDEV_DBG(netdev, "Could not configure libfc for the "
665 "interface\n");
Vasu Dev7f349142009-03-27 09:06:31 -0700666 goto out_lp_destroy;
667 }
668
Vasu Deve8af4d42009-07-29 17:05:15 -0700669 /*
670 * fcoe_em_alloc() and fcoe_hostlist_add() both
671 * need to be atomic under fcoe_hostlist_lock
672 * since fcoe_em_alloc() looks for an existing EM
673 * instance on host list updated by fcoe_hostlist_add().
674 */
675 write_lock(&fcoe_hostlist_lock);
Vasu Dev96316092009-07-29 17:05:00 -0700676 /* lport exch manager allocation */
Chris Leechaf7f85d2009-08-25 13:59:24 -0700677 rc = fcoe_em_config(lport);
Vasu Dev96316092009-07-29 17:05:00 -0700678 if (rc) {
679 FCOE_NETDEV_DBG(netdev, "Could not configure the EM for the "
680 "interface\n");
681 goto out_lp_destroy;
682 }
683
Vasu Dev7f349142009-03-27 09:06:31 -0700684 /* add to lports list */
Chris Leechaf7f85d2009-08-25 13:59:24 -0700685 fcoe_hostlist_add(lport);
Vasu Deve8af4d42009-07-29 17:05:15 -0700686 write_unlock(&fcoe_hostlist_lock);
Vasu Dev7f349142009-03-27 09:06:31 -0700687
Chris Leechaf7f85d2009-08-25 13:59:24 -0700688 lport->boot_time = jiffies;
Vasu Dev7f349142009-03-27 09:06:31 -0700689
Chris Leechaf7f85d2009-08-25 13:59:24 -0700690 fc_fabric_login(lport);
Vasu Dev7f349142009-03-27 09:06:31 -0700691
Chris Leechaf7f85d2009-08-25 13:59:24 -0700692 if (!fcoe_link_ok(lport))
Joe Eykholt97c83892009-03-17 11:42:40 -0700693 fcoe_ctlr_link_up(&fc->ctlr);
694
Vasu Dev7f349142009-03-27 09:06:31 -0700695 dev_hold(netdev);
696
Chris Leechaf7f85d2009-08-25 13:59:24 -0700697 return lport;
Vasu Dev7f349142009-03-27 09:06:31 -0700698
699out_lp_destroy:
Chris Leechaf7f85d2009-08-25 13:59:24 -0700700 fc_exch_mgr_free(lport);
Vasu Devab6b85c2009-05-17 12:33:08 +0000701out_netdev_cleanup:
702 fcoe_netdev_cleanup(fc);
Vasu Dev7f349142009-03-27 09:06:31 -0700703out_host_put:
Chris Leechaf7f85d2009-08-25 13:59:24 -0700704 scsi_host_put(lport->host);
705out:
706 return ERR_PTR(rc);
Vasu Dev7f349142009-03-27 09:06:31 -0700707}
708
709/**
710 * fcoe_if_init() - attach to scsi transport
711 *
712 * Returns : 0 on success
713 */
714static int __init fcoe_if_init(void)
715{
716 /* attach to scsi transport */
717 scsi_transport_fcoe_sw =
718 fc_attach_transport(&fcoe_transport_function);
719
720 if (!scsi_transport_fcoe_sw) {
Robert Loved5488eb2009-06-10 15:30:59 -0700721 printk(KERN_ERR "fcoe: Failed to attach to the FC transport\n");
Vasu Dev7f349142009-03-27 09:06:31 -0700722 return -ENODEV;
723 }
724
725 return 0;
726}
727
728/**
729 * fcoe_if_exit() - detach from scsi transport
730 *
731 * Returns : 0 on success
732 */
733int __exit fcoe_if_exit(void)
734{
735 fc_release_transport(scsi_transport_fcoe_sw);
736 return 0;
737}
738
Robert Love85b4aa42008-12-09 15:10:24 -0800739/**
Robert Love8976f422009-03-17 11:41:46 -0700740 * fcoe_percpu_thread_create() - Create a receive thread for an online cpu
741 * @cpu: cpu index for the online cpu
742 */
743static void fcoe_percpu_thread_create(unsigned int cpu)
744{
745 struct fcoe_percpu_s *p;
746 struct task_struct *thread;
747
748 p = &per_cpu(fcoe_percpu, cpu);
749
750 thread = kthread_create(fcoe_percpu_receive_thread,
751 (void *)p, "fcoethread/%d", cpu);
752
753 if (likely(!IS_ERR(p->thread))) {
754 kthread_bind(thread, cpu);
755 wake_up_process(thread);
756
757 spin_lock_bh(&p->fcoe_rx_list.lock);
758 p->thread = thread;
759 spin_unlock_bh(&p->fcoe_rx_list.lock);
760 }
761}
762
763/**
764 * fcoe_percpu_thread_destroy() - removes the rx thread for the given cpu
765 * @cpu: cpu index the rx thread is to be removed
766 *
767 * Destroys a per-CPU Rx thread. Any pending skbs are moved to the
768 * current CPU's Rx thread. If the thread being destroyed is bound to
769 * the CPU processing this context the skbs will be freed.
770 */
771static void fcoe_percpu_thread_destroy(unsigned int cpu)
772{
773 struct fcoe_percpu_s *p;
774 struct task_struct *thread;
775 struct page *crc_eof;
776 struct sk_buff *skb;
777#ifdef CONFIG_SMP
778 struct fcoe_percpu_s *p0;
779 unsigned targ_cpu = smp_processor_id();
780#endif /* CONFIG_SMP */
781
Robert Loved5488eb2009-06-10 15:30:59 -0700782 FCOE_DBG("Destroying receive thread for CPU %d\n", cpu);
Robert Love8976f422009-03-17 11:41:46 -0700783
784 /* Prevent any new skbs from being queued for this CPU. */
785 p = &per_cpu(fcoe_percpu, cpu);
786 spin_lock_bh(&p->fcoe_rx_list.lock);
787 thread = p->thread;
788 p->thread = NULL;
789 crc_eof = p->crc_eof_page;
790 p->crc_eof_page = NULL;
791 p->crc_eof_offset = 0;
792 spin_unlock_bh(&p->fcoe_rx_list.lock);
793
794#ifdef CONFIG_SMP
795 /*
796 * Don't bother moving the skb's if this context is running
797 * on the same CPU that is having its thread destroyed. This
798 * can easily happen when the module is removed.
799 */
800 if (cpu != targ_cpu) {
801 p0 = &per_cpu(fcoe_percpu, targ_cpu);
802 spin_lock_bh(&p0->fcoe_rx_list.lock);
803 if (p0->thread) {
Robert Loved5488eb2009-06-10 15:30:59 -0700804 FCOE_DBG("Moving frames from CPU %d to CPU %d\n",
805 cpu, targ_cpu);
Robert Love8976f422009-03-17 11:41:46 -0700806
807 while ((skb = __skb_dequeue(&p->fcoe_rx_list)) != NULL)
808 __skb_queue_tail(&p0->fcoe_rx_list, skb);
809 spin_unlock_bh(&p0->fcoe_rx_list.lock);
810 } else {
811 /*
812 * The targeted CPU is not initialized and cannot accept
813 * new skbs. Unlock the targeted CPU and drop the skbs
814 * on the CPU that is going offline.
815 */
816 while ((skb = __skb_dequeue(&p->fcoe_rx_list)) != NULL)
817 kfree_skb(skb);
818 spin_unlock_bh(&p0->fcoe_rx_list.lock);
819 }
820 } else {
821 /*
822 * This scenario occurs when the module is being removed
823 * and all threads are being destroyed. skbs will continue
824 * to be shifted from the CPU thread that is being removed
825 * to the CPU thread associated with the CPU that is processing
826 * the module removal. Once there is only one CPU Rx thread it
827 * will reach this case and we will drop all skbs and later
828 * stop the thread.
829 */
830 spin_lock_bh(&p->fcoe_rx_list.lock);
831 while ((skb = __skb_dequeue(&p->fcoe_rx_list)) != NULL)
832 kfree_skb(skb);
833 spin_unlock_bh(&p->fcoe_rx_list.lock);
834 }
835#else
836 /*
Chris Leechdd3fd722009-04-21 16:27:36 -0700837 * This a non-SMP scenario where the singular Rx thread is
Robert Love8976f422009-03-17 11:41:46 -0700838 * being removed. Free all skbs and stop the thread.
839 */
840 spin_lock_bh(&p->fcoe_rx_list.lock);
841 while ((skb = __skb_dequeue(&p->fcoe_rx_list)) != NULL)
842 kfree_skb(skb);
843 spin_unlock_bh(&p->fcoe_rx_list.lock);
844#endif
845
846 if (thread)
847 kthread_stop(thread);
848
849 if (crc_eof)
850 put_page(crc_eof);
851}
852
853/**
854 * fcoe_cpu_callback() - fcoe cpu hotplug event callback
855 * @nfb: callback data block
856 * @action: event triggering the callback
857 * @hcpu: index for the cpu of this event
858 *
859 * This creates or destroys per cpu data for fcoe
860 *
861 * Returns NOTIFY_OK always.
862 */
863static int fcoe_cpu_callback(struct notifier_block *nfb,
864 unsigned long action, void *hcpu)
865{
866 unsigned cpu = (unsigned long)hcpu;
867
868 switch (action) {
869 case CPU_ONLINE:
870 case CPU_ONLINE_FROZEN:
Robert Loved5488eb2009-06-10 15:30:59 -0700871 FCOE_DBG("CPU %x online: Create Rx thread\n", cpu);
Robert Love8976f422009-03-17 11:41:46 -0700872 fcoe_percpu_thread_create(cpu);
873 break;
874 case CPU_DEAD:
875 case CPU_DEAD_FROZEN:
Robert Loved5488eb2009-06-10 15:30:59 -0700876 FCOE_DBG("CPU %x offline: Remove Rx thread\n", cpu);
Robert Love8976f422009-03-17 11:41:46 -0700877 fcoe_percpu_thread_destroy(cpu);
878 break;
879 default:
880 break;
881 }
882 return NOTIFY_OK;
883}
884
885static struct notifier_block fcoe_cpu_notifier = {
886 .notifier_call = fcoe_cpu_callback,
887};
888
889/**
Robert Love34f42a02009-02-27 10:55:45 -0800890 * fcoe_rcv() - this is the fcoe receive function called by NET_RX_SOFTIRQ
Robert Love85b4aa42008-12-09 15:10:24 -0800891 * @skb: the receive skb
892 * @dev: associated net device
893 * @ptype: context
Chris Leechdd3fd722009-04-21 16:27:36 -0700894 * @olddev: last device
Robert Love85b4aa42008-12-09 15:10:24 -0800895 *
896 * this function will receive the packet and build fc frame and pass it up
897 *
898 * Returns: 0 for success
Robert Love34f42a02009-02-27 10:55:45 -0800899 */
Robert Love85b4aa42008-12-09 15:10:24 -0800900int fcoe_rcv(struct sk_buff *skb, struct net_device *dev,
901 struct packet_type *ptype, struct net_device *olddev)
902{
903 struct fc_lport *lp;
904 struct fcoe_rcv_info *fr;
905 struct fcoe_softc *fc;
Robert Love85b4aa42008-12-09 15:10:24 -0800906 struct fc_frame_header *fh;
Robert Love85b4aa42008-12-09 15:10:24 -0800907 struct fcoe_percpu_s *fps;
Vasu Devb2f00912009-08-25 13:58:53 -0700908 unsigned int cpu;
Robert Love85b4aa42008-12-09 15:10:24 -0800909
910 fc = container_of(ptype, struct fcoe_softc, fcoe_packet_type);
Joe Eykholt97c83892009-03-17 11:42:40 -0700911 lp = fc->ctlr.lp;
Robert Love85b4aa42008-12-09 15:10:24 -0800912 if (unlikely(lp == NULL)) {
Robert Loved5488eb2009-06-10 15:30:59 -0700913 FCOE_NETDEV_DBG(dev, "Cannot find hba structure");
Robert Love85b4aa42008-12-09 15:10:24 -0800914 goto err2;
915 }
Joe Eykholt97c83892009-03-17 11:42:40 -0700916 if (!lp->link_up)
917 goto err2;
Robert Love85b4aa42008-12-09 15:10:24 -0800918
Robert Loved5488eb2009-06-10 15:30:59 -0700919 FCOE_NETDEV_DBG(dev, "skb_info: len:%d data_len:%d head:%p "
920 "data:%p tail:%p end:%p sum:%d dev:%s",
921 skb->len, skb->data_len, skb->head, skb->data,
922 skb_tail_pointer(skb), skb_end_pointer(skb),
923 skb->csum, skb->dev ? skb->dev->name : "<NULL>");
Robert Love85b4aa42008-12-09 15:10:24 -0800924
925 /* check for FCOE packet type */
926 if (unlikely(eth_hdr(skb)->h_proto != htons(ETH_P_FCOE))) {
Robert Loved5488eb2009-06-10 15:30:59 -0700927 FCOE_NETDEV_DBG(dev, "Wrong FC type frame");
Robert Love85b4aa42008-12-09 15:10:24 -0800928 goto err;
929 }
930
931 /*
932 * Check for minimum frame length, and make sure required FCoE
933 * and FC headers are pulled into the linear data area.
934 */
935 if (unlikely((skb->len < FCOE_MIN_FRAME) ||
936 !pskb_may_pull(skb, FCOE_HEADER_LEN)))
937 goto err;
938
939 skb_set_transport_header(skb, sizeof(struct fcoe_hdr));
940 fh = (struct fc_frame_header *) skb_transport_header(skb);
941
Robert Love85b4aa42008-12-09 15:10:24 -0800942 fr = fcoe_dev_from_skb(skb);
943 fr->fr_dev = lp;
944 fr->ptype = ptype;
Robert Love5e5e92d2009-03-17 11:41:35 -0700945
Robert Love85b4aa42008-12-09 15:10:24 -0800946 /*
Vasu Devb2f00912009-08-25 13:58:53 -0700947 * In case the incoming frame's exchange is originated from
948 * the initiator, then received frame's exchange id is ANDed
949 * with fc_cpu_mask bits to get the same cpu on which exchange
950 * was originated, otherwise just use the current cpu.
Robert Love85b4aa42008-12-09 15:10:24 -0800951 */
Vasu Devb2f00912009-08-25 13:58:53 -0700952 if (ntoh24(fh->fh_f_ctl) & FC_FC_EX_CTX)
953 cpu = ntohs(fh->fh_ox_id) & fc_cpu_mask;
954 else
955 cpu = smp_processor_id();
Robert Love5e5e92d2009-03-17 11:41:35 -0700956
Robert Love8976f422009-03-17 11:41:46 -0700957 fps = &per_cpu(fcoe_percpu, cpu);
Robert Love85b4aa42008-12-09 15:10:24 -0800958 spin_lock_bh(&fps->fcoe_rx_list.lock);
Robert Love8976f422009-03-17 11:41:46 -0700959 if (unlikely(!fps->thread)) {
960 /*
961 * The targeted CPU is not ready, let's target
962 * the first CPU now. For non-SMP systems this
963 * will check the same CPU twice.
964 */
Robert Loved5488eb2009-06-10 15:30:59 -0700965 FCOE_NETDEV_DBG(dev, "CPU is online, but no receive thread "
966 "ready for incoming skb- using first online "
967 "CPU.\n");
Robert Love8976f422009-03-17 11:41:46 -0700968
969 spin_unlock_bh(&fps->fcoe_rx_list.lock);
970 cpu = first_cpu(cpu_online_map);
971 fps = &per_cpu(fcoe_percpu, cpu);
972 spin_lock_bh(&fps->fcoe_rx_list.lock);
973 if (!fps->thread) {
974 spin_unlock_bh(&fps->fcoe_rx_list.lock);
975 goto err;
976 }
977 }
978
979 /*
980 * We now have a valid CPU that we're targeting for
981 * this skb. We also have this receive thread locked,
982 * so we're free to queue skbs into it's queue.
983 */
Robert Love85b4aa42008-12-09 15:10:24 -0800984 __skb_queue_tail(&fps->fcoe_rx_list, skb);
985 if (fps->fcoe_rx_list.qlen == 1)
986 wake_up_process(fps->thread);
987
988 spin_unlock_bh(&fps->fcoe_rx_list.lock);
989
990 return 0;
991err:
Robert Love582b45b2009-03-31 15:51:50 -0700992 fc_lport_get_stats(lp)->ErrorFrames++;
Robert Love85b4aa42008-12-09 15:10:24 -0800993
994err2:
995 kfree_skb(skb);
996 return -1;
997}
Robert Love85b4aa42008-12-09 15:10:24 -0800998
999/**
Robert Love34f42a02009-02-27 10:55:45 -08001000 * fcoe_start_io() - pass to netdev to start xmit for fcoe
Robert Love85b4aa42008-12-09 15:10:24 -08001001 * @skb: the skb to be xmitted
1002 *
1003 * Returns: 0 for success
Robert Love34f42a02009-02-27 10:55:45 -08001004 */
Robert Love85b4aa42008-12-09 15:10:24 -08001005static inline int fcoe_start_io(struct sk_buff *skb)
1006{
1007 int rc;
1008
1009 skb_get(skb);
1010 rc = dev_queue_xmit(skb);
1011 if (rc != 0)
1012 return rc;
1013 kfree_skb(skb);
1014 return 0;
1015}
1016
1017/**
Chris Leechdd3fd722009-04-21 16:27:36 -07001018 * fcoe_get_paged_crc_eof() - in case we need to alloc a page for crc_eof
Robert Love85b4aa42008-12-09 15:10:24 -08001019 * @skb: the skb to be xmitted
1020 * @tlen: total len
1021 *
1022 * Returns: 0 for success
Robert Love34f42a02009-02-27 10:55:45 -08001023 */
Robert Love85b4aa42008-12-09 15:10:24 -08001024static int fcoe_get_paged_crc_eof(struct sk_buff *skb, int tlen)
1025{
1026 struct fcoe_percpu_s *fps;
1027 struct page *page;
Robert Love85b4aa42008-12-09 15:10:24 -08001028
Robert Love5e5e92d2009-03-17 11:41:35 -07001029 fps = &get_cpu_var(fcoe_percpu);
Robert Love85b4aa42008-12-09 15:10:24 -08001030 page = fps->crc_eof_page;
1031 if (!page) {
1032 page = alloc_page(GFP_ATOMIC);
1033 if (!page) {
Robert Love5e5e92d2009-03-17 11:41:35 -07001034 put_cpu_var(fcoe_percpu);
Robert Love85b4aa42008-12-09 15:10:24 -08001035 return -ENOMEM;
1036 }
1037 fps->crc_eof_page = page;
Robert Love8976f422009-03-17 11:41:46 -07001038 fps->crc_eof_offset = 0;
Robert Love85b4aa42008-12-09 15:10:24 -08001039 }
1040
1041 get_page(page);
1042 skb_fill_page_desc(skb, skb_shinfo(skb)->nr_frags, page,
1043 fps->crc_eof_offset, tlen);
1044 skb->len += tlen;
1045 skb->data_len += tlen;
1046 skb->truesize += tlen;
1047 fps->crc_eof_offset += sizeof(struct fcoe_crc_eof);
1048
1049 if (fps->crc_eof_offset >= PAGE_SIZE) {
1050 fps->crc_eof_page = NULL;
1051 fps->crc_eof_offset = 0;
1052 put_page(page);
1053 }
Robert Love5e5e92d2009-03-17 11:41:35 -07001054 put_cpu_var(fcoe_percpu);
Robert Love85b4aa42008-12-09 15:10:24 -08001055 return 0;
1056}
1057
1058/**
Robert Love34f42a02009-02-27 10:55:45 -08001059 * fcoe_fc_crc() - calculates FC CRC in this fcoe skb
Chris Leechdd3fd722009-04-21 16:27:36 -07001060 * @fp: the fc_frame containing data to be checksummed
Robert Love85b4aa42008-12-09 15:10:24 -08001061 *
1062 * This uses crc32() to calculate the crc for fc frame
1063 * Return : 32 bit crc
Robert Love34f42a02009-02-27 10:55:45 -08001064 */
Robert Love85b4aa42008-12-09 15:10:24 -08001065u32 fcoe_fc_crc(struct fc_frame *fp)
1066{
1067 struct sk_buff *skb = fp_skb(fp);
1068 struct skb_frag_struct *frag;
1069 unsigned char *data;
1070 unsigned long off, len, clen;
1071 u32 crc;
1072 unsigned i;
1073
1074 crc = crc32(~0, skb->data, skb_headlen(skb));
1075
1076 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
1077 frag = &skb_shinfo(skb)->frags[i];
1078 off = frag->page_offset;
1079 len = frag->size;
1080 while (len > 0) {
1081 clen = min(len, PAGE_SIZE - (off & ~PAGE_MASK));
1082 data = kmap_atomic(frag->page + (off >> PAGE_SHIFT),
1083 KM_SKB_DATA_SOFTIRQ);
1084 crc = crc32(crc, data + (off & ~PAGE_MASK), clen);
1085 kunmap_atomic(data, KM_SKB_DATA_SOFTIRQ);
1086 off += clen;
1087 len -= clen;
1088 }
1089 }
1090 return crc;
1091}
Robert Love85b4aa42008-12-09 15:10:24 -08001092
1093/**
Robert Love34f42a02009-02-27 10:55:45 -08001094 * fcoe_xmit() - FCoE frame transmit function
Robert Love85b4aa42008-12-09 15:10:24 -08001095 * @lp: the associated local port
1096 * @fp: the fc_frame to be transmitted
1097 *
1098 * Return : 0 for success
Robert Love34f42a02009-02-27 10:55:45 -08001099 */
Robert Love85b4aa42008-12-09 15:10:24 -08001100int fcoe_xmit(struct fc_lport *lp, struct fc_frame *fp)
1101{
Vasu Dev4bb6b512009-05-06 10:52:34 -07001102 int wlen;
Robert Love85b4aa42008-12-09 15:10:24 -08001103 u32 crc;
1104 struct ethhdr *eh;
1105 struct fcoe_crc_eof *cp;
1106 struct sk_buff *skb;
1107 struct fcoe_dev_stats *stats;
1108 struct fc_frame_header *fh;
1109 unsigned int hlen; /* header length implies the version */
1110 unsigned int tlen; /* trailer length */
1111 unsigned int elen; /* eth header, may include vlan */
Robert Love85b4aa42008-12-09 15:10:24 -08001112 struct fcoe_softc *fc;
1113 u8 sof, eof;
1114 struct fcoe_hdr *hp;
1115
1116 WARN_ON((fr_len(fp) % sizeof(u32)) != 0);
1117
Robert Lovefc47ff62009-02-27 10:55:55 -08001118 fc = lport_priv(lp);
Robert Love85b4aa42008-12-09 15:10:24 -08001119 fh = fc_frame_header_get(fp);
Joe Eykholt97c83892009-03-17 11:42:40 -07001120 skb = fp_skb(fp);
1121 wlen = skb->len / FCOE_WORD_TO_BYTE;
1122
1123 if (!lp->link_up) {
Dan Carpenter3caf02e2009-04-21 16:27:25 -07001124 kfree_skb(skb);
Joe Eykholt97c83892009-03-17 11:42:40 -07001125 return 0;
Robert Love85b4aa42008-12-09 15:10:24 -08001126 }
1127
Joe Eykholt97c83892009-03-17 11:42:40 -07001128 if (unlikely(fh->fh_r_ctl == FC_RCTL_ELS_REQ) &&
1129 fcoe_ctlr_els_send(&fc->ctlr, skb))
1130 return 0;
1131
Robert Love85b4aa42008-12-09 15:10:24 -08001132 sof = fr_sof(fp);
1133 eof = fr_eof(fp);
1134
Vasu Dev4e57e1c2009-05-06 10:52:46 -07001135 elen = sizeof(struct ethhdr);
Robert Love85b4aa42008-12-09 15:10:24 -08001136 hlen = sizeof(struct fcoe_hdr);
1137 tlen = sizeof(struct fcoe_crc_eof);
1138 wlen = (skb->len - tlen + sizeof(crc)) / FCOE_WORD_TO_BYTE;
1139
1140 /* crc offload */
1141 if (likely(lp->crc_offload)) {
Yi Zou39ca9a02009-02-27 14:07:15 -08001142 skb->ip_summed = CHECKSUM_PARTIAL;
Robert Love85b4aa42008-12-09 15:10:24 -08001143 skb->csum_start = skb_headroom(skb);
1144 skb->csum_offset = skb->len;
1145 crc = 0;
1146 } else {
1147 skb->ip_summed = CHECKSUM_NONE;
1148 crc = fcoe_fc_crc(fp);
1149 }
1150
1151 /* copy fc crc and eof to the skb buff */
1152 if (skb_is_nonlinear(skb)) {
1153 skb_frag_t *frag;
1154 if (fcoe_get_paged_crc_eof(skb, tlen)) {
Roel Kluine9041582009-02-27 10:56:22 -08001155 kfree_skb(skb);
Robert Love85b4aa42008-12-09 15:10:24 -08001156 return -ENOMEM;
1157 }
1158 frag = &skb_shinfo(skb)->frags[skb_shinfo(skb)->nr_frags - 1];
1159 cp = kmap_atomic(frag->page, KM_SKB_DATA_SOFTIRQ)
1160 + frag->page_offset;
1161 } else {
1162 cp = (struct fcoe_crc_eof *)skb_put(skb, tlen);
1163 }
1164
1165 memset(cp, 0, sizeof(*cp));
1166 cp->fcoe_eof = eof;
1167 cp->fcoe_crc32 = cpu_to_le32(~crc);
1168
1169 if (skb_is_nonlinear(skb)) {
1170 kunmap_atomic(cp, KM_SKB_DATA_SOFTIRQ);
1171 cp = NULL;
1172 }
1173
Chris Leechdd3fd722009-04-21 16:27:36 -07001174 /* adjust skb network/transport offsets to match mac/fcoe/fc */
Robert Love85b4aa42008-12-09 15:10:24 -08001175 skb_push(skb, elen + hlen);
1176 skb_reset_mac_header(skb);
1177 skb_reset_network_header(skb);
1178 skb->mac_len = elen;
Yi Zou211c7382009-02-27 14:06:37 -08001179 skb->protocol = htons(ETH_P_FCOE);
Vasu Dev1d1b88d2009-07-29 17:05:45 -07001180 skb->dev = fc->netdev;
Robert Love85b4aa42008-12-09 15:10:24 -08001181
1182 /* fill up mac and fcoe headers */
1183 eh = eth_hdr(skb);
1184 eh->h_proto = htons(ETH_P_FCOE);
Joe Eykholt97c83892009-03-17 11:42:40 -07001185 if (fc->ctlr.map_dest)
Robert Love85b4aa42008-12-09 15:10:24 -08001186 fc_fcoe_set_mac(eh->h_dest, fh->fh_d_id);
1187 else
1188 /* insert GW address */
Joe Eykholt97c83892009-03-17 11:42:40 -07001189 memcpy(eh->h_dest, fc->ctlr.dest_addr, ETH_ALEN);
Robert Love85b4aa42008-12-09 15:10:24 -08001190
Joe Eykholt97c83892009-03-17 11:42:40 -07001191 if (unlikely(fc->ctlr.flogi_oxid != FC_XID_UNKNOWN))
1192 memcpy(eh->h_source, fc->ctlr.ctl_src_addr, ETH_ALEN);
Robert Love85b4aa42008-12-09 15:10:24 -08001193 else
Joe Eykholt97c83892009-03-17 11:42:40 -07001194 memcpy(eh->h_source, fc->ctlr.data_src_addr, ETH_ALEN);
Robert Love85b4aa42008-12-09 15:10:24 -08001195
1196 hp = (struct fcoe_hdr *)(eh + 1);
1197 memset(hp, 0, sizeof(*hp));
1198 if (FC_FCOE_VER)
1199 FC_FCOE_ENCAPS_VER(hp, FC_FCOE_VER);
1200 hp->fcoe_sof = sof;
1201
Yi Zou39ca9a02009-02-27 14:07:15 -08001202 /* fcoe lso, mss is in max_payload which is non-zero for FCP data */
1203 if (lp->seq_offload && fr_max_payload(fp)) {
1204 skb_shinfo(skb)->gso_type = SKB_GSO_FCOE;
1205 skb_shinfo(skb)->gso_size = fr_max_payload(fp);
1206 } else {
1207 skb_shinfo(skb)->gso_type = 0;
1208 skb_shinfo(skb)->gso_size = 0;
1209 }
Robert Love85b4aa42008-12-09 15:10:24 -08001210 /* update tx stats: regardless if LLD fails */
Robert Love582b45b2009-03-31 15:51:50 -07001211 stats = fc_lport_get_stats(lp);
1212 stats->TxFrames++;
1213 stats->TxWords += wlen;
Robert Love85b4aa42008-12-09 15:10:24 -08001214
1215 /* send down to lld */
1216 fr_dev(fp) = lp;
1217 if (fc->fcoe_pending_queue.qlen)
Vasu Dev4bb6b512009-05-06 10:52:34 -07001218 fcoe_check_wait_queue(lp, skb);
1219 else if (fcoe_start_io(skb))
1220 fcoe_check_wait_queue(lp, skb);
Robert Love85b4aa42008-12-09 15:10:24 -08001221
1222 return 0;
1223}
Robert Love85b4aa42008-12-09 15:10:24 -08001224
Robert Love34f42a02009-02-27 10:55:45 -08001225/**
1226 * fcoe_percpu_receive_thread() - recv thread per cpu
Robert Love85b4aa42008-12-09 15:10:24 -08001227 * @arg: ptr to the fcoe per cpu struct
1228 *
1229 * Return: 0 for success
Robert Love85b4aa42008-12-09 15:10:24 -08001230 */
1231int fcoe_percpu_receive_thread(void *arg)
1232{
1233 struct fcoe_percpu_s *p = arg;
1234 u32 fr_len;
1235 struct fc_lport *lp;
1236 struct fcoe_rcv_info *fr;
1237 struct fcoe_dev_stats *stats;
1238 struct fc_frame_header *fh;
1239 struct sk_buff *skb;
1240 struct fcoe_crc_eof crc_eof;
1241 struct fc_frame *fp;
1242 u8 *mac = NULL;
1243 struct fcoe_softc *fc;
1244 struct fcoe_hdr *hp;
1245
Robert Love4469c192009-02-27 10:56:38 -08001246 set_user_nice(current, -20);
Robert Love85b4aa42008-12-09 15:10:24 -08001247
1248 while (!kthread_should_stop()) {
1249
1250 spin_lock_bh(&p->fcoe_rx_list.lock);
1251 while ((skb = __skb_dequeue(&p->fcoe_rx_list)) == NULL) {
1252 set_current_state(TASK_INTERRUPTIBLE);
1253 spin_unlock_bh(&p->fcoe_rx_list.lock);
1254 schedule();
1255 set_current_state(TASK_RUNNING);
1256 if (kthread_should_stop())
1257 return 0;
1258 spin_lock_bh(&p->fcoe_rx_list.lock);
1259 }
1260 spin_unlock_bh(&p->fcoe_rx_list.lock);
1261 fr = fcoe_dev_from_skb(skb);
1262 lp = fr->fr_dev;
1263 if (unlikely(lp == NULL)) {
Robert Loved5488eb2009-06-10 15:30:59 -07001264 FCOE_NETDEV_DBG(skb->dev, "Invalid HBA Structure");
Robert Love85b4aa42008-12-09 15:10:24 -08001265 kfree_skb(skb);
1266 continue;
1267 }
1268
Robert Loved5488eb2009-06-10 15:30:59 -07001269 FCOE_NETDEV_DBG(skb->dev, "skb_info: len:%d data_len:%d "
1270 "head:%p data:%p tail:%p end:%p sum:%d dev:%s",
1271 skb->len, skb->data_len,
1272 skb->head, skb->data, skb_tail_pointer(skb),
1273 skb_end_pointer(skb), skb->csum,
1274 skb->dev ? skb->dev->name : "<NULL>");
Robert Love85b4aa42008-12-09 15:10:24 -08001275
1276 /*
1277 * Save source MAC address before discarding header.
1278 */
1279 fc = lport_priv(lp);
Robert Love85b4aa42008-12-09 15:10:24 -08001280 if (skb_is_nonlinear(skb))
1281 skb_linearize(skb); /* not ideal */
Joe Eykholt97c83892009-03-17 11:42:40 -07001282 mac = eth_hdr(skb)->h_source;
Robert Love85b4aa42008-12-09 15:10:24 -08001283
1284 /*
1285 * Frame length checks and setting up the header pointers
1286 * was done in fcoe_rcv already.
1287 */
1288 hp = (struct fcoe_hdr *) skb_network_header(skb);
1289 fh = (struct fc_frame_header *) skb_transport_header(skb);
1290
Robert Love582b45b2009-03-31 15:51:50 -07001291 stats = fc_lport_get_stats(lp);
Robert Love85b4aa42008-12-09 15:10:24 -08001292 if (unlikely(FC_FCOE_DECAPS_VER(hp) != FC_FCOE_VER)) {
Robert Love582b45b2009-03-31 15:51:50 -07001293 if (stats->ErrorFrames < 5)
Robert Loved5488eb2009-06-10 15:30:59 -07001294 printk(KERN_WARNING "fcoe: FCoE version "
Robert Love582b45b2009-03-31 15:51:50 -07001295 "mismatch: The frame has "
1296 "version %x, but the "
1297 "initiator supports version "
1298 "%x\n", FC_FCOE_DECAPS_VER(hp),
1299 FC_FCOE_VER);
1300 stats->ErrorFrames++;
Robert Love85b4aa42008-12-09 15:10:24 -08001301 kfree_skb(skb);
1302 continue;
1303 }
1304
1305 skb_pull(skb, sizeof(struct fcoe_hdr));
1306 fr_len = skb->len - sizeof(struct fcoe_crc_eof);
1307
Robert Love582b45b2009-03-31 15:51:50 -07001308 stats->RxFrames++;
1309 stats->RxWords += fr_len / FCOE_WORD_TO_BYTE;
Robert Love85b4aa42008-12-09 15:10:24 -08001310
1311 fp = (struct fc_frame *)skb;
1312 fc_frame_init(fp);
1313 fr_dev(fp) = lp;
1314 fr_sof(fp) = hp->fcoe_sof;
1315
1316 /* Copy out the CRC and EOF trailer for access */
1317 if (skb_copy_bits(skb, fr_len, &crc_eof, sizeof(crc_eof))) {
1318 kfree_skb(skb);
1319 continue;
1320 }
1321 fr_eof(fp) = crc_eof.fcoe_eof;
1322 fr_crc(fp) = crc_eof.fcoe_crc32;
1323 if (pskb_trim(skb, fr_len)) {
1324 kfree_skb(skb);
1325 continue;
1326 }
1327
1328 /*
1329 * We only check CRC if no offload is available and if it is
1330 * it's solicited data, in which case, the FCP layer would
1331 * check it during the copy.
1332 */
Yi Zou07c00ec2009-02-27 14:07:31 -08001333 if (lp->crc_offload && skb->ip_summed == CHECKSUM_UNNECESSARY)
Robert Love85b4aa42008-12-09 15:10:24 -08001334 fr_flags(fp) &= ~FCPHF_CRC_UNCHECKED;
1335 else
1336 fr_flags(fp) |= FCPHF_CRC_UNCHECKED;
1337
1338 fh = fc_frame_header_get(fp);
1339 if (fh->fh_r_ctl == FC_RCTL_DD_SOL_DATA &&
1340 fh->fh_type == FC_TYPE_FCP) {
Vasu Dev52ff8782009-07-29 17:05:10 -07001341 fc_exch_recv(lp, fp);
Robert Love85b4aa42008-12-09 15:10:24 -08001342 continue;
1343 }
1344 if (fr_flags(fp) & FCPHF_CRC_UNCHECKED) {
1345 if (le32_to_cpu(fr_crc(fp)) !=
1346 ~crc32(~0, skb->data, fr_len)) {
Robert Loved5488eb2009-06-10 15:30:59 -07001347 if (stats->InvalidCRCCount < 5)
Robert Love85b4aa42008-12-09 15:10:24 -08001348 printk(KERN_WARNING "fcoe: dropping "
1349 "frame with CRC error\n");
1350 stats->InvalidCRCCount++;
1351 stats->ErrorFrames++;
1352 fc_frame_free(fp);
1353 continue;
1354 }
1355 fr_flags(fp) &= ~FCPHF_CRC_UNCHECKED;
1356 }
Joe Eykholt97c83892009-03-17 11:42:40 -07001357 if (unlikely(fc->ctlr.flogi_oxid != FC_XID_UNKNOWN) &&
1358 fcoe_ctlr_recv_flogi(&fc->ctlr, fp, mac)) {
1359 fc_frame_free(fp);
1360 continue;
1361 }
Vasu Dev52ff8782009-07-29 17:05:10 -07001362 fc_exch_recv(lp, fp);
Robert Love85b4aa42008-12-09 15:10:24 -08001363 }
1364 return 0;
1365}
1366
1367/**
Chris Leechdd3fd722009-04-21 16:27:36 -07001368 * fcoe_check_wait_queue() - attempt to clear the transmit backlog
1369 * @lp: the fc_lport
Robert Love85b4aa42008-12-09 15:10:24 -08001370 *
1371 * This empties the wait_queue, dequeue the head of the wait_queue queue
1372 * and calls fcoe_start_io() for each packet, if all skb have been
Vasu Devc826a312009-02-27 10:56:27 -08001373 * transmitted, return qlen or -1 if a error occurs, then restore
Chris Leechdd3fd722009-04-21 16:27:36 -07001374 * wait_queue and try again later.
Robert Love85b4aa42008-12-09 15:10:24 -08001375 *
1376 * The wait_queue is used when the skb transmit fails. skb will go
Chris Leechdd3fd722009-04-21 16:27:36 -07001377 * in the wait_queue which will be emptied by the timer function or
Robert Love85b4aa42008-12-09 15:10:24 -08001378 * by the next skb transmit.
Robert Love34f42a02009-02-27 10:55:45 -08001379 */
Vasu Dev4bb6b512009-05-06 10:52:34 -07001380static void fcoe_check_wait_queue(struct fc_lport *lp, struct sk_buff *skb)
Robert Love85b4aa42008-12-09 15:10:24 -08001381{
Chris Leech55c8baf2009-02-27 10:56:32 -08001382 struct fcoe_softc *fc = lport_priv(lp);
Vasu Dev4bb6b512009-05-06 10:52:34 -07001383 int rc;
Robert Love85b4aa42008-12-09 15:10:24 -08001384
Robert Love85b4aa42008-12-09 15:10:24 -08001385 spin_lock_bh(&fc->fcoe_pending_queue.lock);
Vasu Dev4bb6b512009-05-06 10:52:34 -07001386
1387 if (skb)
1388 __skb_queue_tail(&fc->fcoe_pending_queue, skb);
1389
Vasu Devc826a312009-02-27 10:56:27 -08001390 if (fc->fcoe_pending_queue_active)
1391 goto out;
1392 fc->fcoe_pending_queue_active = 1;
Chris Leech55c8baf2009-02-27 10:56:32 -08001393
1394 while (fc->fcoe_pending_queue.qlen) {
1395 /* keep qlen > 0 until fcoe_start_io succeeds */
1396 fc->fcoe_pending_queue.qlen++;
1397 skb = __skb_dequeue(&fc->fcoe_pending_queue);
1398
1399 spin_unlock_bh(&fc->fcoe_pending_queue.lock);
1400 rc = fcoe_start_io(skb);
1401 spin_lock_bh(&fc->fcoe_pending_queue.lock);
1402
1403 if (rc) {
1404 __skb_queue_head(&fc->fcoe_pending_queue, skb);
1405 /* undo temporary increment above */
1406 fc->fcoe_pending_queue.qlen--;
1407 break;
Robert Love85b4aa42008-12-09 15:10:24 -08001408 }
Chris Leech55c8baf2009-02-27 10:56:32 -08001409 /* undo temporary increment above */
1410 fc->fcoe_pending_queue.qlen--;
Robert Love85b4aa42008-12-09 15:10:24 -08001411 }
Chris Leech55c8baf2009-02-27 10:56:32 -08001412
1413 if (fc->fcoe_pending_queue.qlen < FCOE_LOW_QUEUE_DEPTH)
1414 lp->qfull = 0;
Vasu Dev1047f222009-05-06 10:52:40 -07001415 if (fc->fcoe_pending_queue.qlen && !timer_pending(&fc->timer))
1416 mod_timer(&fc->timer, jiffies + 2);
Vasu Devc826a312009-02-27 10:56:27 -08001417 fc->fcoe_pending_queue_active = 0;
Vasu Devc826a312009-02-27 10:56:27 -08001418out:
Vasu Dev4bb6b512009-05-06 10:52:34 -07001419 if (fc->fcoe_pending_queue.qlen > FCOE_MAX_QUEUE_DEPTH)
1420 lp->qfull = 1;
Robert Love85b4aa42008-12-09 15:10:24 -08001421 spin_unlock_bh(&fc->fcoe_pending_queue.lock);
Vasu Dev4bb6b512009-05-06 10:52:34 -07001422 return;
Robert Love85b4aa42008-12-09 15:10:24 -08001423}
1424
1425/**
Robert Love34f42a02009-02-27 10:55:45 -08001426 * fcoe_dev_setup() - setup link change notification interface
1427 */
Randy Dunlapb0d428a2009-04-27 21:49:31 -07001428static void fcoe_dev_setup(void)
Robert Love85b4aa42008-12-09 15:10:24 -08001429{
Robert Love85b4aa42008-12-09 15:10:24 -08001430 register_netdevice_notifier(&fcoe_notifier);
1431}
1432
1433/**
Randy Dunlapb0d428a2009-04-27 21:49:31 -07001434 * fcoe_dev_cleanup() - cleanup link change notification interface
Robert Love34f42a02009-02-27 10:55:45 -08001435 */
Robert Love85b4aa42008-12-09 15:10:24 -08001436static void fcoe_dev_cleanup(void)
1437{
1438 unregister_netdevice_notifier(&fcoe_notifier);
1439}
1440
1441/**
Robert Love34f42a02009-02-27 10:55:45 -08001442 * fcoe_device_notification() - netdev event notification callback
Robert Love85b4aa42008-12-09 15:10:24 -08001443 * @notifier: context of the notification
1444 * @event: type of event
1445 * @ptr: fixed array for output parsed ifname
1446 *
1447 * This function is called by the ethernet driver in case of link change event
1448 *
1449 * Returns: 0 for success
Robert Love34f42a02009-02-27 10:55:45 -08001450 */
Robert Love85b4aa42008-12-09 15:10:24 -08001451static int fcoe_device_notification(struct notifier_block *notifier,
1452 ulong event, void *ptr)
1453{
1454 struct fc_lport *lp = NULL;
Vasu Dev1d1b88d2009-07-29 17:05:45 -07001455 struct net_device *netdev = ptr;
Robert Love85b4aa42008-12-09 15:10:24 -08001456 struct fcoe_softc *fc;
1457 struct fcoe_dev_stats *stats;
Joe Eykholt97c83892009-03-17 11:42:40 -07001458 u32 link_possible = 1;
Robert Love85b4aa42008-12-09 15:10:24 -08001459 u32 mfs;
1460 int rc = NOTIFY_OK;
1461
1462 read_lock(&fcoe_hostlist_lock);
1463 list_for_each_entry(fc, &fcoe_hostlist, list) {
Vasu Dev1d1b88d2009-07-29 17:05:45 -07001464 if (fc->netdev == netdev) {
Joe Eykholt97c83892009-03-17 11:42:40 -07001465 lp = fc->ctlr.lp;
Robert Love85b4aa42008-12-09 15:10:24 -08001466 break;
1467 }
1468 }
1469 read_unlock(&fcoe_hostlist_lock);
1470 if (lp == NULL) {
1471 rc = NOTIFY_DONE;
1472 goto out;
1473 }
1474
Robert Love85b4aa42008-12-09 15:10:24 -08001475 switch (event) {
1476 case NETDEV_DOWN:
1477 case NETDEV_GOING_DOWN:
Joe Eykholt97c83892009-03-17 11:42:40 -07001478 link_possible = 0;
Robert Love85b4aa42008-12-09 15:10:24 -08001479 break;
1480 case NETDEV_UP:
1481 case NETDEV_CHANGE:
Robert Love85b4aa42008-12-09 15:10:24 -08001482 break;
1483 case NETDEV_CHANGEMTU:
Vasu Dev1d1b88d2009-07-29 17:05:45 -07001484 mfs = netdev->mtu - (sizeof(struct fcoe_hdr) +
1485 sizeof(struct fcoe_crc_eof));
Robert Love85b4aa42008-12-09 15:10:24 -08001486 if (mfs >= FC_MIN_MAX_FRAME)
1487 fc_set_mfs(lp, mfs);
Robert Love85b4aa42008-12-09 15:10:24 -08001488 break;
1489 case NETDEV_REGISTER:
1490 break;
1491 default:
Vasu Dev1d1b88d2009-07-29 17:05:45 -07001492 FCOE_NETDEV_DBG(netdev, "Unknown event %ld "
Robert Loved5488eb2009-06-10 15:30:59 -07001493 "from netdev netlink\n", event);
Robert Love85b4aa42008-12-09 15:10:24 -08001494 }
Joe Eykholt97c83892009-03-17 11:42:40 -07001495 if (link_possible && !fcoe_link_ok(lp))
1496 fcoe_ctlr_link_up(&fc->ctlr);
1497 else if (fcoe_ctlr_link_down(&fc->ctlr)) {
1498 stats = fc_lport_get_stats(lp);
1499 stats->LinkFailureCount++;
1500 fcoe_clean_pending_queue(lp);
Robert Love85b4aa42008-12-09 15:10:24 -08001501 }
1502out:
1503 return rc;
1504}
1505
1506/**
Robert Love34f42a02009-02-27 10:55:45 -08001507 * fcoe_if_to_netdev() - parse a name buffer to get netdev
Robert Love85b4aa42008-12-09 15:10:24 -08001508 * @buffer: incoming buffer to be copied
1509 *
Chris Leechdd3fd722009-04-21 16:27:36 -07001510 * Returns: NULL or ptr to net_device
Robert Love34f42a02009-02-27 10:55:45 -08001511 */
Robert Love85b4aa42008-12-09 15:10:24 -08001512static struct net_device *fcoe_if_to_netdev(const char *buffer)
1513{
1514 char *cp;
1515 char ifname[IFNAMSIZ + 2];
1516
1517 if (buffer) {
1518 strlcpy(ifname, buffer, IFNAMSIZ);
1519 cp = ifname + strlen(ifname);
1520 while (--cp >= ifname && *cp == '\n')
1521 *cp = '\0';
1522 return dev_get_by_name(&init_net, ifname);
1523 }
1524 return NULL;
1525}
1526
1527/**
Chris Leechdd3fd722009-04-21 16:27:36 -07001528 * fcoe_netdev_to_module_owner() - finds out the driver module of the netdev
Robert Love85b4aa42008-12-09 15:10:24 -08001529 * @netdev: the target netdev
1530 *
1531 * Returns: ptr to the struct module, NULL for failure
Robert Love34f42a02009-02-27 10:55:45 -08001532 */
Robert Loveb2ab99c2009-02-27 10:55:50 -08001533static struct module *
1534fcoe_netdev_to_module_owner(const struct net_device *netdev)
Robert Love85b4aa42008-12-09 15:10:24 -08001535{
1536 struct device *dev;
1537
1538 if (!netdev)
1539 return NULL;
1540
1541 dev = netdev->dev.parent;
1542 if (!dev)
1543 return NULL;
1544
1545 if (!dev->driver)
1546 return NULL;
1547
1548 return dev->driver->owner;
1549}
1550
1551/**
Robert Love34f42a02009-02-27 10:55:45 -08001552 * fcoe_ethdrv_get() - Hold the Ethernet driver
Robert Love85b4aa42008-12-09 15:10:24 -08001553 * @netdev: the target netdev
1554 *
Robert Love34f42a02009-02-27 10:55:45 -08001555 * Holds the Ethernet driver module by try_module_get() for
1556 * the corresponding netdev.
1557 *
Chris Leechdd3fd722009-04-21 16:27:36 -07001558 * Returns: 0 for success
Robert Love34f42a02009-02-27 10:55:45 -08001559 */
Robert Love85b4aa42008-12-09 15:10:24 -08001560static int fcoe_ethdrv_get(const struct net_device *netdev)
1561{
1562 struct module *owner;
1563
1564 owner = fcoe_netdev_to_module_owner(netdev);
1565 if (owner) {
Robert Loved5488eb2009-06-10 15:30:59 -07001566 FCOE_NETDEV_DBG(netdev, "Hold driver module %s\n",
1567 module_name(owner));
Robert Love85b4aa42008-12-09 15:10:24 -08001568 return try_module_get(owner);
1569 }
1570 return -ENODEV;
1571}
1572
1573/**
Robert Love34f42a02009-02-27 10:55:45 -08001574 * fcoe_ethdrv_put() - Release the Ethernet driver
Robert Love85b4aa42008-12-09 15:10:24 -08001575 * @netdev: the target netdev
1576 *
Robert Love34f42a02009-02-27 10:55:45 -08001577 * Releases the Ethernet driver module by module_put for
1578 * the corresponding netdev.
1579 *
Chris Leechdd3fd722009-04-21 16:27:36 -07001580 * Returns: 0 for success
Robert Love34f42a02009-02-27 10:55:45 -08001581 */
Robert Love85b4aa42008-12-09 15:10:24 -08001582static int fcoe_ethdrv_put(const struct net_device *netdev)
1583{
1584 struct module *owner;
1585
1586 owner = fcoe_netdev_to_module_owner(netdev);
1587 if (owner) {
Robert Loved5488eb2009-06-10 15:30:59 -07001588 FCOE_NETDEV_DBG(netdev, "Release driver module %s\n",
1589 module_name(owner));
Robert Love85b4aa42008-12-09 15:10:24 -08001590 module_put(owner);
1591 return 0;
1592 }
1593 return -ENODEV;
1594}
1595
1596/**
Robert Love34f42a02009-02-27 10:55:45 -08001597 * fcoe_destroy() - handles the destroy from sysfs
Chris Leechdd3fd722009-04-21 16:27:36 -07001598 * @buffer: expected to be an eth if name
Robert Love85b4aa42008-12-09 15:10:24 -08001599 * @kp: associated kernel param
1600 *
1601 * Returns: 0 for success
Robert Love34f42a02009-02-27 10:55:45 -08001602 */
Robert Love85b4aa42008-12-09 15:10:24 -08001603static int fcoe_destroy(const char *buffer, struct kernel_param *kp)
1604{
Robert Love85b4aa42008-12-09 15:10:24 -08001605 struct net_device *netdev;
Chris Leechaf7f85d2009-08-25 13:59:24 -07001606 struct fc_lport *lport;
1607 int rc;
Robert Love85b4aa42008-12-09 15:10:24 -08001608
1609 netdev = fcoe_if_to_netdev(buffer);
1610 if (!netdev) {
1611 rc = -ENODEV;
1612 goto out_nodev;
1613 }
1614 /* look for existing lport */
Chris Leechaf7f85d2009-08-25 13:59:24 -07001615 lport = fcoe_hostlist_lookup(netdev);
1616 if (!lport) {
Robert Love85b4aa42008-12-09 15:10:24 -08001617 rc = -ENODEV;
1618 goto out_putdev;
1619 }
Chris Leechaf7f85d2009-08-25 13:59:24 -07001620 fcoe_if_destroy(lport);
Robert Love85b4aa42008-12-09 15:10:24 -08001621 fcoe_ethdrv_put(netdev);
1622 rc = 0;
1623out_putdev:
1624 dev_put(netdev);
1625out_nodev:
1626 return rc;
1627}
1628
1629/**
Robert Love34f42a02009-02-27 10:55:45 -08001630 * fcoe_create() - Handles the create call from sysfs
Chris Leechdd3fd722009-04-21 16:27:36 -07001631 * @buffer: expected to be an eth if name
Robert Love85b4aa42008-12-09 15:10:24 -08001632 * @kp: associated kernel param
1633 *
1634 * Returns: 0 for success
Robert Love34f42a02009-02-27 10:55:45 -08001635 */
Robert Love85b4aa42008-12-09 15:10:24 -08001636static int fcoe_create(const char *buffer, struct kernel_param *kp)
1637{
1638 int rc;
Chris Leechaf7f85d2009-08-25 13:59:24 -07001639 struct fc_lport *lport;
Robert Love85b4aa42008-12-09 15:10:24 -08001640 struct net_device *netdev;
1641
1642 netdev = fcoe_if_to_netdev(buffer);
1643 if (!netdev) {
1644 rc = -ENODEV;
1645 goto out_nodev;
1646 }
1647 /* look for existing lport */
1648 if (fcoe_hostlist_lookup(netdev)) {
1649 rc = -EEXIST;
1650 goto out_putdev;
1651 }
1652 fcoe_ethdrv_get(netdev);
1653
Chris Leechaf7f85d2009-08-25 13:59:24 -07001654 lport = fcoe_if_create(netdev, &netdev->dev);
1655 if (IS_ERR(lport)) {
Robert Loved5488eb2009-06-10 15:30:59 -07001656 printk(KERN_ERR "fcoe: Failed to create interface (%s)\n",
Robert Love85b4aa42008-12-09 15:10:24 -08001657 netdev->name);
1658 fcoe_ethdrv_put(netdev);
1659 rc = -EIO;
1660 goto out_putdev;
1661 }
1662 rc = 0;
1663out_putdev:
1664 dev_put(netdev);
1665out_nodev:
1666 return rc;
1667}
1668
1669module_param_call(create, fcoe_create, NULL, NULL, S_IWUSR);
1670__MODULE_PARM_TYPE(create, "string");
1671MODULE_PARM_DESC(create, "Create fcoe port using net device passed in.");
1672module_param_call(destroy, fcoe_destroy, NULL, NULL, S_IWUSR);
1673__MODULE_PARM_TYPE(destroy, "string");
1674MODULE_PARM_DESC(destroy, "Destroy fcoe port");
1675
Robert Love34f42a02009-02-27 10:55:45 -08001676/**
1677 * fcoe_link_ok() - Check if link is ok for the fc_lport
Robert Love85b4aa42008-12-09 15:10:24 -08001678 * @lp: ptr to the fc_lport
1679 *
1680 * Any permanently-disqualifying conditions have been previously checked.
1681 * This also updates the speed setting, which may change with link for 100/1000.
1682 *
1683 * This function should probably be checking for PAUSE support at some point
1684 * in the future. Currently Per-priority-pause is not determinable using
1685 * ethtool, so we shouldn't be restrictive until that problem is resolved.
1686 *
1687 * Returns: 0 if link is OK for use by FCoE.
1688 *
1689 */
1690int fcoe_link_ok(struct fc_lport *lp)
1691{
Robert Lovefc47ff62009-02-27 10:55:55 -08001692 struct fcoe_softc *fc = lport_priv(lp);
Vasu Dev1d1b88d2009-07-29 17:05:45 -07001693 struct net_device *dev = fc->netdev;
Robert Love85b4aa42008-12-09 15:10:24 -08001694 struct ethtool_cmd ecmd = { ETHTOOL_GSET };
Robert Love85b4aa42008-12-09 15:10:24 -08001695
Yi Zou2f718d62009-07-29 17:04:01 -07001696 if ((dev->flags & IFF_UP) && netif_carrier_ok(dev) &&
1697 (!dev_ethtool_get_settings(dev, &ecmd))) {
1698 lp->link_supported_speeds &=
1699 ~(FC_PORTSPEED_1GBIT | FC_PORTSPEED_10GBIT);
1700 if (ecmd.supported & (SUPPORTED_1000baseT_Half |
1701 SUPPORTED_1000baseT_Full))
1702 lp->link_supported_speeds |= FC_PORTSPEED_1GBIT;
1703 if (ecmd.supported & SUPPORTED_10000baseT_Full)
1704 lp->link_supported_speeds |=
1705 FC_PORTSPEED_10GBIT;
1706 if (ecmd.speed == SPEED_1000)
1707 lp->link_speed = FC_PORTSPEED_1GBIT;
1708 if (ecmd.speed == SPEED_10000)
1709 lp->link_speed = FC_PORTSPEED_10GBIT;
Robert Love85b4aa42008-12-09 15:10:24 -08001710
Yi Zou2f718d62009-07-29 17:04:01 -07001711 return 0;
1712 }
1713 return -1;
Robert Love85b4aa42008-12-09 15:10:24 -08001714}
Robert Love85b4aa42008-12-09 15:10:24 -08001715
Robert Love34f42a02009-02-27 10:55:45 -08001716/**
1717 * fcoe_percpu_clean() - Clear the pending skbs for an lport
Robert Love85b4aa42008-12-09 15:10:24 -08001718 * @lp: the fc_lport
1719 */
1720void fcoe_percpu_clean(struct fc_lport *lp)
1721{
Robert Love85b4aa42008-12-09 15:10:24 -08001722 struct fcoe_percpu_s *pp;
1723 struct fcoe_rcv_info *fr;
1724 struct sk_buff_head *list;
1725 struct sk_buff *skb, *next;
1726 struct sk_buff *head;
Robert Love5e5e92d2009-03-17 11:41:35 -07001727 unsigned int cpu;
Robert Love85b4aa42008-12-09 15:10:24 -08001728
Robert Love5e5e92d2009-03-17 11:41:35 -07001729 for_each_possible_cpu(cpu) {
1730 pp = &per_cpu(fcoe_percpu, cpu);
1731 spin_lock_bh(&pp->fcoe_rx_list.lock);
1732 list = &pp->fcoe_rx_list;
1733 head = list->next;
1734 for (skb = head; skb != (struct sk_buff *)list;
1735 skb = next) {
1736 next = skb->next;
1737 fr = fcoe_dev_from_skb(skb);
1738 if (fr->fr_dev == lp) {
1739 __skb_unlink(skb, list);
1740 kfree_skb(skb);
Robert Love85b4aa42008-12-09 15:10:24 -08001741 }
Robert Love85b4aa42008-12-09 15:10:24 -08001742 }
Robert Love5e5e92d2009-03-17 11:41:35 -07001743 spin_unlock_bh(&pp->fcoe_rx_list.lock);
Robert Love85b4aa42008-12-09 15:10:24 -08001744 }
1745}
Robert Love85b4aa42008-12-09 15:10:24 -08001746
1747/**
Robert Love34f42a02009-02-27 10:55:45 -08001748 * fcoe_clean_pending_queue() - Dequeue a skb and free it
Robert Love85b4aa42008-12-09 15:10:24 -08001749 * @lp: the corresponding fc_lport
1750 *
1751 * Returns: none
Robert Love34f42a02009-02-27 10:55:45 -08001752 */
Robert Love85b4aa42008-12-09 15:10:24 -08001753void fcoe_clean_pending_queue(struct fc_lport *lp)
1754{
1755 struct fcoe_softc *fc = lport_priv(lp);
1756 struct sk_buff *skb;
1757
1758 spin_lock_bh(&fc->fcoe_pending_queue.lock);
1759 while ((skb = __skb_dequeue(&fc->fcoe_pending_queue)) != NULL) {
1760 spin_unlock_bh(&fc->fcoe_pending_queue.lock);
1761 kfree_skb(skb);
1762 spin_lock_bh(&fc->fcoe_pending_queue.lock);
1763 }
1764 spin_unlock_bh(&fc->fcoe_pending_queue.lock);
1765}
Robert Love85b4aa42008-12-09 15:10:24 -08001766
1767/**
Robert Love34f42a02009-02-27 10:55:45 -08001768 * fcoe_reset() - Resets the fcoe
Robert Love85b4aa42008-12-09 15:10:24 -08001769 * @shost: shost the reset is from
1770 *
1771 * Returns: always 0
1772 */
1773int fcoe_reset(struct Scsi_Host *shost)
1774{
1775 struct fc_lport *lport = shost_priv(shost);
1776 fc_lport_reset(lport);
1777 return 0;
1778}
Robert Love85b4aa42008-12-09 15:10:24 -08001779
Robert Love34f42a02009-02-27 10:55:45 -08001780/**
Robert Love34f42a02009-02-27 10:55:45 -08001781 * fcoe_hostlist_lookup_softc() - find the corresponding lport by a given device
Chris Leechdd3fd722009-04-21 16:27:36 -07001782 * @dev: this is currently ptr to net_device
Robert Love85b4aa42008-12-09 15:10:24 -08001783 *
Vasu Deve8af4d42009-07-29 17:05:15 -07001784 * Called with fcoe_hostlist_lock held.
1785 *
Robert Love85b4aa42008-12-09 15:10:24 -08001786 * Returns: NULL or the located fcoe_softc
1787 */
Robert Loveb2ab99c2009-02-27 10:55:50 -08001788static struct fcoe_softc *
1789fcoe_hostlist_lookup_softc(const struct net_device *dev)
Robert Love85b4aa42008-12-09 15:10:24 -08001790{
1791 struct fcoe_softc *fc;
1792
Robert Love85b4aa42008-12-09 15:10:24 -08001793 list_for_each_entry(fc, &fcoe_hostlist, list) {
Vasu Dev1d1b88d2009-07-29 17:05:45 -07001794 if (fc->netdev == dev)
Robert Love85b4aa42008-12-09 15:10:24 -08001795 return fc;
Robert Love85b4aa42008-12-09 15:10:24 -08001796 }
Robert Love85b4aa42008-12-09 15:10:24 -08001797 return NULL;
1798}
1799
Robert Love34f42a02009-02-27 10:55:45 -08001800/**
1801 * fcoe_hostlist_lookup() - Find the corresponding lport by netdev
Robert Love85b4aa42008-12-09 15:10:24 -08001802 * @netdev: ptr to net_device
1803 *
1804 * Returns: 0 for success
1805 */
1806struct fc_lport *fcoe_hostlist_lookup(const struct net_device *netdev)
1807{
1808 struct fcoe_softc *fc;
1809
Vasu Deve8af4d42009-07-29 17:05:15 -07001810 read_lock(&fcoe_hostlist_lock);
Robert Love85b4aa42008-12-09 15:10:24 -08001811 fc = fcoe_hostlist_lookup_softc(netdev);
Vasu Deve8af4d42009-07-29 17:05:15 -07001812 read_unlock(&fcoe_hostlist_lock);
Robert Love85b4aa42008-12-09 15:10:24 -08001813
Joe Eykholt97c83892009-03-17 11:42:40 -07001814 return (fc) ? fc->ctlr.lp : NULL;
Robert Love85b4aa42008-12-09 15:10:24 -08001815}
Robert Love85b4aa42008-12-09 15:10:24 -08001816
Robert Love34f42a02009-02-27 10:55:45 -08001817/**
1818 * fcoe_hostlist_add() - Add a lport to lports list
Chris Leechdd3fd722009-04-21 16:27:36 -07001819 * @lp: ptr to the fc_lport to be added
Robert Love85b4aa42008-12-09 15:10:24 -08001820 *
Vasu Deve8af4d42009-07-29 17:05:15 -07001821 * Called with write fcoe_hostlist_lock held.
1822 *
Robert Love85b4aa42008-12-09 15:10:24 -08001823 * Returns: 0 for success
1824 */
1825int fcoe_hostlist_add(const struct fc_lport *lp)
1826{
1827 struct fcoe_softc *fc;
1828
1829 fc = fcoe_hostlist_lookup_softc(fcoe_netdev(lp));
1830 if (!fc) {
Robert Lovefc47ff62009-02-27 10:55:55 -08001831 fc = lport_priv(lp);
Robert Love85b4aa42008-12-09 15:10:24 -08001832 list_add_tail(&fc->list, &fcoe_hostlist);
Robert Love85b4aa42008-12-09 15:10:24 -08001833 }
1834 return 0;
1835}
Robert Love85b4aa42008-12-09 15:10:24 -08001836
Robert Love34f42a02009-02-27 10:55:45 -08001837/**
1838 * fcoe_hostlist_remove() - remove a lport from lports list
Chris Leechdd3fd722009-04-21 16:27:36 -07001839 * @lp: ptr to the fc_lport to be removed
Robert Love85b4aa42008-12-09 15:10:24 -08001840 *
1841 * Returns: 0 for success
1842 */
1843int fcoe_hostlist_remove(const struct fc_lport *lp)
1844{
1845 struct fcoe_softc *fc;
1846
Vasu Deve8af4d42009-07-29 17:05:15 -07001847 write_lock_bh(&fcoe_hostlist_lock);
Robert Love85b4aa42008-12-09 15:10:24 -08001848 fc = fcoe_hostlist_lookup_softc(fcoe_netdev(lp));
1849 BUG_ON(!fc);
Robert Love85b4aa42008-12-09 15:10:24 -08001850 list_del(&fc->list);
1851 write_unlock_bh(&fcoe_hostlist_lock);
1852
1853 return 0;
1854}
Robert Love85b4aa42008-12-09 15:10:24 -08001855
1856/**
Robert Love34f42a02009-02-27 10:55:45 -08001857 * fcoe_init() - fcoe module loading initialization
Robert Love85b4aa42008-12-09 15:10:24 -08001858 *
Robert Love85b4aa42008-12-09 15:10:24 -08001859 * Returns 0 on success, negative on failure
Robert Love34f42a02009-02-27 10:55:45 -08001860 */
Robert Love85b4aa42008-12-09 15:10:24 -08001861static int __init fcoe_init(void)
1862{
Robert Love38eccab2009-03-17 11:41:30 -07001863 unsigned int cpu;
Robert Love8976f422009-03-17 11:41:46 -07001864 int rc = 0;
Robert Love85b4aa42008-12-09 15:10:24 -08001865 struct fcoe_percpu_s *p;
1866
Robert Love38eccab2009-03-17 11:41:30 -07001867 for_each_possible_cpu(cpu) {
Robert Love5e5e92d2009-03-17 11:41:35 -07001868 p = &per_cpu(fcoe_percpu, cpu);
Robert Love38eccab2009-03-17 11:41:30 -07001869 skb_queue_head_init(&p->fcoe_rx_list);
1870 }
1871
Robert Love8976f422009-03-17 11:41:46 -07001872 for_each_online_cpu(cpu)
1873 fcoe_percpu_thread_create(cpu);
Robert Love85b4aa42008-12-09 15:10:24 -08001874
Robert Love8976f422009-03-17 11:41:46 -07001875 /* Initialize per CPU interrupt thread */
1876 rc = register_hotcpu_notifier(&fcoe_cpu_notifier);
1877 if (rc)
1878 goto out_free;
Robert Love85b4aa42008-12-09 15:10:24 -08001879
Robert Love8976f422009-03-17 11:41:46 -07001880 /* Setup link change notification */
Robert Love85b4aa42008-12-09 15:10:24 -08001881 fcoe_dev_setup();
1882
Chris Leech5892c322009-08-25 13:59:14 -07001883 rc = fcoe_if_init();
1884 if (rc)
1885 goto out_free;
Robert Love85b4aa42008-12-09 15:10:24 -08001886
1887 return 0;
Robert Love8976f422009-03-17 11:41:46 -07001888
1889out_free:
1890 for_each_online_cpu(cpu) {
1891 fcoe_percpu_thread_destroy(cpu);
1892 }
1893
1894 return rc;
Robert Love85b4aa42008-12-09 15:10:24 -08001895}
1896module_init(fcoe_init);
1897
1898/**
Robert Love34f42a02009-02-27 10:55:45 -08001899 * fcoe_exit() - fcoe module unloading cleanup
Robert Love85b4aa42008-12-09 15:10:24 -08001900 *
1901 * Returns 0 on success, negative on failure
Robert Love34f42a02009-02-27 10:55:45 -08001902 */
Robert Love85b4aa42008-12-09 15:10:24 -08001903static void __exit fcoe_exit(void)
1904{
Robert Love5e5e92d2009-03-17 11:41:35 -07001905 unsigned int cpu;
Robert Love85b4aa42008-12-09 15:10:24 -08001906 struct fcoe_softc *fc, *tmp;
Robert Love85b4aa42008-12-09 15:10:24 -08001907
Robert Love85b4aa42008-12-09 15:10:24 -08001908 fcoe_dev_cleanup();
1909
Vasu Dev5919a592009-03-27 09:03:29 -07001910 /* releases the associated fcoe hosts */
Robert Love85b4aa42008-12-09 15:10:24 -08001911 list_for_each_entry_safe(fc, tmp, &fcoe_hostlist, list)
Chris Leechaf7f85d2009-08-25 13:59:24 -07001912 fcoe_if_destroy(fc->ctlr.lp);
Robert Love85b4aa42008-12-09 15:10:24 -08001913
Robert Love8976f422009-03-17 11:41:46 -07001914 unregister_hotcpu_notifier(&fcoe_cpu_notifier);
1915
1916 for_each_online_cpu(cpu) {
1917 fcoe_percpu_thread_destroy(cpu);
Robert Love85b4aa42008-12-09 15:10:24 -08001918 }
1919
Vasu Dev7f349142009-03-27 09:06:31 -07001920 /* detach from scsi transport */
1921 fcoe_if_exit();
Robert Love85b4aa42008-12-09 15:10:24 -08001922}
1923module_exit(fcoe_exit);