blob: c0264a98439498760cc9c41544376ef1b6d94529 [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
Chris Leechdfc1d0f2009-08-25 14:00:13 -070058DEFINE_MUTEX(fcoe_config_mutex);
59
Robert Love85b4aa42008-12-09 15:10:24 -080060/* fcoe host list */
61LIST_HEAD(fcoe_hostlist);
62DEFINE_RWLOCK(fcoe_hostlist_lock);
Robert Love5e5e92d2009-03-17 11:41:35 -070063DEFINE_PER_CPU(struct fcoe_percpu_s, fcoe_percpu);
Robert Love85b4aa42008-12-09 15:10:24 -080064
Chris Leechdd3fd722009-04-21 16:27:36 -070065/* Function Prototypes */
Vasu Devfdd78022009-03-17 11:42:24 -070066static int fcoe_reset(struct Scsi_Host *shost);
67static int fcoe_xmit(struct fc_lport *, struct fc_frame *);
68static int fcoe_rcv(struct sk_buff *, struct net_device *,
69 struct packet_type *, struct net_device *);
70static int fcoe_percpu_receive_thread(void *arg);
71static void fcoe_clean_pending_queue(struct fc_lport *lp);
72static void fcoe_percpu_clean(struct fc_lport *lp);
73static int fcoe_link_ok(struct fc_lport *lp);
74
75static struct fc_lport *fcoe_hostlist_lookup(const struct net_device *);
76static int fcoe_hostlist_add(const struct fc_lport *);
Vasu Devfdd78022009-03-17 11:42:24 -070077
Vasu Dev4bb6b512009-05-06 10:52:34 -070078static void fcoe_check_wait_queue(struct fc_lport *, struct sk_buff *);
Robert Love85b4aa42008-12-09 15:10:24 -080079static int fcoe_device_notification(struct notifier_block *, ulong, void *);
80static void fcoe_dev_setup(void);
81static void fcoe_dev_cleanup(void);
Chris Leech2e70e242009-08-25 14:00:23 -070082static struct fcoe_interface *
83 fcoe_hostlist_lookup_port(const struct net_device *dev);
Robert Love85b4aa42008-12-09 15:10:24 -080084
85/* notification function from net device */
86static struct notifier_block fcoe_notifier = {
87 .notifier_call = fcoe_device_notification,
88};
89
Vasu Dev7f349142009-03-27 09:06:31 -070090static struct scsi_transport_template *scsi_transport_fcoe_sw;
91
92struct fc_function_template fcoe_transport_function = {
93 .show_host_node_name = 1,
94 .show_host_port_name = 1,
95 .show_host_supported_classes = 1,
96 .show_host_supported_fc4s = 1,
97 .show_host_active_fc4s = 1,
98 .show_host_maxframe_size = 1,
99
100 .show_host_port_id = 1,
101 .show_host_supported_speeds = 1,
102 .get_host_speed = fc_get_host_speed,
103 .show_host_speed = 1,
104 .show_host_port_type = 1,
105 .get_host_port_state = fc_get_host_port_state,
106 .show_host_port_state = 1,
107 .show_host_symbolic_name = 1,
108
109 .dd_fcrport_size = sizeof(struct fc_rport_libfc_priv),
110 .show_rport_maxframe_size = 1,
111 .show_rport_supported_classes = 1,
112
113 .show_host_fabric_name = 1,
114 .show_starget_node_name = 1,
115 .show_starget_port_name = 1,
116 .show_starget_port_id = 1,
117 .set_rport_dev_loss_tmo = fc_set_rport_loss_tmo,
118 .show_rport_dev_loss_tmo = 1,
119 .get_fc_host_stats = fc_get_host_stats,
120 .issue_fc_host_lip = fcoe_reset,
121
122 .terminate_rport_io = fc_rport_terminate_io,
123};
124
125static struct scsi_host_template fcoe_shost_template = {
126 .module = THIS_MODULE,
127 .name = "FCoE Driver",
128 .proc_name = FCOE_NAME,
129 .queuecommand = fc_queuecommand,
130 .eh_abort_handler = fc_eh_abort,
131 .eh_device_reset_handler = fc_eh_device_reset,
132 .eh_host_reset_handler = fc_eh_host_reset,
133 .slave_alloc = fc_slave_alloc,
134 .change_queue_depth = fc_change_queue_depth,
135 .change_queue_type = fc_change_queue_type,
136 .this_id = -1,
137 .cmd_per_lun = 32,
138 .can_queue = FCOE_MAX_OUTSTANDING_COMMANDS,
139 .use_clustering = ENABLE_CLUSTERING,
140 .sg_tablesize = SG_ALL,
141 .max_sectors = 0xffff,
142};
143
Chris Leech54b649f2009-08-25 14:00:07 -0700144static int fcoe_fip_recv(struct sk_buff *skb, struct net_device *dev,
145 struct packet_type *ptype,
146 struct net_device *orig_dev);
147/**
148 * fcoe_interface_setup()
149 * @fcoe: new fcoe_interface
150 * @netdev : ptr to the associated netdevice struct
151 *
152 * Returns : 0 for success
Chris Leech2e70e242009-08-25 14:00:23 -0700153 * Locking: must be called with the RTNL mutex held
Chris Leech54b649f2009-08-25 14:00:07 -0700154 */
155static int fcoe_interface_setup(struct fcoe_interface *fcoe,
156 struct net_device *netdev)
157{
158 struct fcoe_ctlr *fip = &fcoe->ctlr;
159 struct netdev_hw_addr *ha;
160 u8 flogi_maddr[ETH_ALEN];
161
162 fcoe->netdev = netdev;
163
164 /* Do not support for bonding device */
165 if ((netdev->priv_flags & IFF_MASTER_ALB) ||
166 (netdev->priv_flags & IFF_SLAVE_INACTIVE) ||
167 (netdev->priv_flags & IFF_MASTER_8023AD)) {
168 return -EOPNOTSUPP;
169 }
170
171 /* look for SAN MAC address, if multiple SAN MACs exist, only
172 * use the first one for SPMA */
173 rcu_read_lock();
174 for_each_dev_addr(netdev, ha) {
175 if ((ha->type == NETDEV_HW_ADDR_T_SAN) &&
176 (is_valid_ether_addr(fip->ctl_src_addr))) {
177 memcpy(fip->ctl_src_addr, ha->addr, ETH_ALEN);
178 fip->spma = 1;
179 break;
180 }
181 }
182 rcu_read_unlock();
183
184 /* setup Source Mac Address */
185 if (!fip->spma)
186 memcpy(fip->ctl_src_addr, netdev->dev_addr, netdev->addr_len);
187
188 /*
189 * Add FCoE MAC address as second unicast MAC address
190 * or enter promiscuous mode if not capable of listening
191 * for multiple unicast MACs.
192 */
Chris Leech54b649f2009-08-25 14:00:07 -0700193 memcpy(flogi_maddr, (u8[6]) FC_FCOE_FLOGI_MAC, ETH_ALEN);
194 dev_unicast_add(netdev, flogi_maddr);
195 if (fip->spma)
196 dev_unicast_add(netdev, fip->ctl_src_addr);
197 dev_mc_add(netdev, FIP_ALL_ENODE_MACS, ETH_ALEN, 0);
Chris Leech54b649f2009-08-25 14:00:07 -0700198
199 /*
200 * setup the receive function from ethernet driver
201 * on the ethertype for the given device
202 */
203 fcoe->fcoe_packet_type.func = fcoe_rcv;
204 fcoe->fcoe_packet_type.type = __constant_htons(ETH_P_FCOE);
205 fcoe->fcoe_packet_type.dev = netdev;
206 dev_add_pack(&fcoe->fcoe_packet_type);
207
208 fcoe->fip_packet_type.func = fcoe_fip_recv;
209 fcoe->fip_packet_type.type = htons(ETH_P_FIP);
210 fcoe->fip_packet_type.dev = netdev;
211 dev_add_pack(&fcoe->fip_packet_type);
212
213 return 0;
214}
215
216static void fcoe_fip_send(struct fcoe_ctlr *fip, struct sk_buff *skb);
217static void fcoe_update_src_mac(struct fcoe_ctlr *fip, u8 *old, u8 *new);
Chris Leech2e70e242009-08-25 14:00:23 -0700218static void fcoe_destroy_work(struct work_struct *work);
Chris Leech54b649f2009-08-25 14:00:07 -0700219
Vasu Dev7f349142009-03-27 09:06:31 -0700220/**
Chris Leech030f4e02009-08-25 14:00:02 -0700221 * fcoe_interface_create()
222 * @netdev: network interface
223 *
224 * Returns: pointer to a struct fcoe_interface or NULL on error
225 */
226static struct fcoe_interface *fcoe_interface_create(struct net_device *netdev)
227{
228 struct fcoe_interface *fcoe;
229
230 fcoe = kzalloc(sizeof(*fcoe), GFP_KERNEL);
231 if (!fcoe) {
232 FCOE_NETDEV_DBG(netdev, "Could not allocate fcoe structure\n");
233 return NULL;
234 }
235
Chris Leech2e70e242009-08-25 14:00:23 -0700236 dev_hold(netdev);
Chris Leech030f4e02009-08-25 14:00:02 -0700237 kref_init(&fcoe->kref);
Chris Leech54b649f2009-08-25 14:00:07 -0700238
239 /*
240 * Initialize FIP.
241 */
242 fcoe_ctlr_init(&fcoe->ctlr);
243 fcoe->ctlr.send = fcoe_fip_send;
244 fcoe->ctlr.update_mac = fcoe_update_src_mac;
245
246 fcoe_interface_setup(fcoe, netdev);
Chris Leech030f4e02009-08-25 14:00:02 -0700247
248 return fcoe;
249}
250
251/**
Chris Leech54b649f2009-08-25 14:00:07 -0700252 * fcoe_interface_cleanup() - clean up netdev configurations
253 * @fcoe:
Chris Leech2e70e242009-08-25 14:00:23 -0700254 *
255 * Caller must be holding the RTNL mutex
Chris Leech54b649f2009-08-25 14:00:07 -0700256 */
257void fcoe_interface_cleanup(struct fcoe_interface *fcoe)
258{
259 struct net_device *netdev = fcoe->netdev;
260 struct fcoe_ctlr *fip = &fcoe->ctlr;
261 u8 flogi_maddr[ETH_ALEN];
262
263 /*
264 * Don't listen for Ethernet packets anymore.
265 * synchronize_net() ensures that the packet handlers are not running
266 * on another CPU. dev_remove_pack() would do that, this calls the
267 * unsyncronized version __dev_remove_pack() to avoid multiple delays.
268 */
269 __dev_remove_pack(&fcoe->fcoe_packet_type);
270 __dev_remove_pack(&fcoe->fip_packet_type);
271 synchronize_net();
272
Chris Leech54b649f2009-08-25 14:00:07 -0700273 /* Delete secondary MAC addresses */
Chris Leech54b649f2009-08-25 14:00:07 -0700274 memcpy(flogi_maddr, (u8[6]) FC_FCOE_FLOGI_MAC, ETH_ALEN);
275 dev_unicast_delete(netdev, flogi_maddr);
276 if (!is_zero_ether_addr(fip->data_src_addr))
277 dev_unicast_delete(netdev, fip->data_src_addr);
278 if (fip->spma)
279 dev_unicast_delete(netdev, fip->ctl_src_addr);
280 dev_mc_delete(netdev, FIP_ALL_ENODE_MACS, ETH_ALEN, 0);
Chris Leech54b649f2009-08-25 14:00:07 -0700281}
282
283/**
Chris Leech030f4e02009-08-25 14:00:02 -0700284 * fcoe_interface_release() - fcoe_port kref release function
285 * @kref: embedded reference count in an fcoe_interface struct
286 */
287static void fcoe_interface_release(struct kref *kref)
288{
289 struct fcoe_interface *fcoe;
Chris Leech2e70e242009-08-25 14:00:23 -0700290 struct net_device *netdev;
Chris Leech030f4e02009-08-25 14:00:02 -0700291
292 fcoe = container_of(kref, struct fcoe_interface, kref);
Chris Leech2e70e242009-08-25 14:00:23 -0700293 netdev = fcoe->netdev;
294 /* tear-down the FCoE controller */
295 fcoe_ctlr_destroy(&fcoe->ctlr);
Chris Leech030f4e02009-08-25 14:00:02 -0700296 kfree(fcoe);
Chris Leech2e70e242009-08-25 14:00:23 -0700297 dev_put(netdev);
Chris Leech030f4e02009-08-25 14:00:02 -0700298}
299
300/**
301 * fcoe_interface_get()
302 * @fcoe:
303 */
304static inline void fcoe_interface_get(struct fcoe_interface *fcoe)
305{
306 kref_get(&fcoe->kref);
307}
308
309/**
310 * fcoe_interface_put()
311 * @fcoe:
312 */
313static inline void fcoe_interface_put(struct fcoe_interface *fcoe)
314{
315 kref_put(&fcoe->kref, fcoe_interface_release);
316}
317
318/**
Vasu Devab6b85c2009-05-17 12:33:08 +0000319 * fcoe_fip_recv - handle a received FIP frame.
320 * @skb: the receive skb
321 * @dev: associated &net_device
322 * @ptype: the &packet_type structure which was used to register this handler.
323 * @orig_dev: original receive &net_device, in case @dev is a bond.
324 *
325 * Returns: 0 for success
326 */
327static int fcoe_fip_recv(struct sk_buff *skb, struct net_device *dev,
328 struct packet_type *ptype,
329 struct net_device *orig_dev)
330{
Chris Leech259ad852009-08-25 13:59:41 -0700331 struct fcoe_interface *fcoe;
Vasu Devab6b85c2009-05-17 12:33:08 +0000332
Chris Leech259ad852009-08-25 13:59:41 -0700333 fcoe = container_of(ptype, struct fcoe_interface, fip_packet_type);
Chris Leech3fe9a0b2009-08-25 13:59:46 -0700334 fcoe_ctlr_recv(&fcoe->ctlr, skb);
Vasu Devab6b85c2009-05-17 12:33:08 +0000335 return 0;
336}
337
338/**
339 * fcoe_fip_send() - send an Ethernet-encapsulated FIP frame.
340 * @fip: FCoE controller.
341 * @skb: FIP Packet.
342 */
343static void fcoe_fip_send(struct fcoe_ctlr *fip, struct sk_buff *skb)
344{
Chris Leech3fe9a0b2009-08-25 13:59:46 -0700345 skb->dev = fcoe_from_ctlr(fip)->netdev;
Vasu Devab6b85c2009-05-17 12:33:08 +0000346 dev_queue_xmit(skb);
347}
348
349/**
350 * fcoe_update_src_mac() - Update Ethernet MAC filters.
351 * @fip: FCoE controller.
352 * @old: Unicast MAC address to delete if the MAC is non-zero.
353 * @new: Unicast MAC address to add.
354 *
355 * Remove any previously-set unicast MAC filter.
356 * Add secondary FCoE MAC address filter for our OUI.
357 */
358static void fcoe_update_src_mac(struct fcoe_ctlr *fip, u8 *old, u8 *new)
359{
Chris Leech25024982009-08-25 13:59:35 -0700360 struct fcoe_interface *fcoe;
Vasu Devab6b85c2009-05-17 12:33:08 +0000361
Chris Leech3fe9a0b2009-08-25 13:59:46 -0700362 fcoe = fcoe_from_ctlr(fip);
Vasu Devab6b85c2009-05-17 12:33:08 +0000363 rtnl_lock();
364 if (!is_zero_ether_addr(old))
Chris Leech25024982009-08-25 13:59:35 -0700365 dev_unicast_delete(fcoe->netdev, old);
366 dev_unicast_add(fcoe->netdev, new);
Vasu Devab6b85c2009-05-17 12:33:08 +0000367 rtnl_unlock();
368}
369
370/**
Vasu Dev7f349142009-03-27 09:06:31 -0700371 * fcoe_lport_config() - sets up the fc_lport
372 * @lp: ptr to the fc_lport
Vasu Dev7f349142009-03-27 09:06:31 -0700373 *
374 * Returns: 0 for success
375 */
376static int fcoe_lport_config(struct fc_lport *lp)
377{
378 lp->link_up = 0;
379 lp->qfull = 0;
380 lp->max_retry_count = 3;
Abhijeet Joglekara3666952009-05-01 10:01:26 -0700381 lp->max_rport_retry_count = 3;
Vasu Dev7f349142009-03-27 09:06:31 -0700382 lp->e_d_tov = 2 * 1000; /* FC-FS default */
383 lp->r_a_tov = 2 * 2 * 1000;
384 lp->service_params = (FCP_SPPF_INIT_FCN | FCP_SPPF_RD_XRDY_DIS |
385 FCP_SPPF_RETRY | FCP_SPPF_CONF_COMPL);
386
387 fc_lport_init_stats(lp);
388
389 /* lport fc_lport related configuration */
390 fc_lport_config(lp);
391
392 /* offload related configuration */
393 lp->crc_offload = 0;
394 lp->seq_offload = 0;
395 lp->lro_enabled = 0;
396 lp->lro_xid = 0;
397 lp->lso_max = 0;
398
399 return 0;
400}
401
402/**
Vasu Dev1047f222009-05-06 10:52:40 -0700403 * fcoe_queue_timer() - fcoe queue timer
404 * @lp: the fc_lport pointer
405 *
406 * Calls fcoe_check_wait_queue on timeout
407 *
408 */
409static void fcoe_queue_timer(ulong lp)
410{
411 fcoe_check_wait_queue((struct fc_lport *)lp, NULL);
412}
413
414/**
Vasu Dev7f349142009-03-27 09:06:31 -0700415 * fcoe_netdev_config() - Set up netdev for SW FCoE
416 * @lp : ptr to the fc_lport
417 * @netdev : ptr to the associated netdevice struct
418 *
419 * Must be called after fcoe_lport_config() as it will use lport mutex
420 *
421 * Returns : 0 for success
422 */
423static int fcoe_netdev_config(struct fc_lport *lp, struct net_device *netdev)
424{
425 u32 mfs;
426 u64 wwnn, wwpn;
Chris Leech25024982009-08-25 13:59:35 -0700427 struct fcoe_interface *fcoe;
Chris Leech014f5c32009-08-25 13:59:30 -0700428 struct fcoe_port *port;
Vasu Dev7f349142009-03-27 09:06:31 -0700429
430 /* Setup lport private data to point to fcoe softc */
Chris Leech014f5c32009-08-25 13:59:30 -0700431 port = lport_priv(lp);
Chris Leech25024982009-08-25 13:59:35 -0700432 fcoe = port->fcoe;
Vasu Dev7f349142009-03-27 09:06:31 -0700433
434 /*
435 * Determine max frame size based on underlying device and optional
436 * user-configured limit. If the MFS is too low, fcoe_link_ok()
437 * will return 0, so do this first.
438 */
Vasu Dev1d1b88d2009-07-29 17:05:45 -0700439 mfs = netdev->mtu - (sizeof(struct fcoe_hdr) +
440 sizeof(struct fcoe_crc_eof));
Vasu Dev7f349142009-03-27 09:06:31 -0700441 if (fc_set_mfs(lp, mfs))
442 return -EINVAL;
443
Vasu Dev7f349142009-03-27 09:06:31 -0700444 /* offload features support */
Vasu Dev1d1b88d2009-07-29 17:05:45 -0700445 if (netdev->features & NETIF_F_SG)
Vasu Dev7f349142009-03-27 09:06:31 -0700446 lp->sg_supp = 1;
447
Vasu Dev7f349142009-03-27 09:06:31 -0700448 if (netdev->features & NETIF_F_FCOE_CRC) {
449 lp->crc_offload = 1;
Robert Loved5488eb2009-06-10 15:30:59 -0700450 FCOE_NETDEV_DBG(netdev, "Supports FCCRC offload\n");
Vasu Dev7f349142009-03-27 09:06:31 -0700451 }
Vasu Dev7f349142009-03-27 09:06:31 -0700452 if (netdev->features & NETIF_F_FSO) {
453 lp->seq_offload = 1;
454 lp->lso_max = netdev->gso_max_size;
Robert Loved5488eb2009-06-10 15:30:59 -0700455 FCOE_NETDEV_DBG(netdev, "Supports LSO for max len 0x%x\n",
456 lp->lso_max);
Vasu Dev7f349142009-03-27 09:06:31 -0700457 }
Vasu Dev7f349142009-03-27 09:06:31 -0700458 if (netdev->fcoe_ddp_xid) {
459 lp->lro_enabled = 1;
460 lp->lro_xid = netdev->fcoe_ddp_xid;
Robert Loved5488eb2009-06-10 15:30:59 -0700461 FCOE_NETDEV_DBG(netdev, "Supports LRO for max xid 0x%x\n",
462 lp->lro_xid);
Vasu Dev7f349142009-03-27 09:06:31 -0700463 }
Chris Leech014f5c32009-08-25 13:59:30 -0700464 skb_queue_head_init(&port->fcoe_pending_queue);
465 port->fcoe_pending_queue_active = 0;
466 setup_timer(&port->timer, fcoe_queue_timer, (unsigned long)lp);
Vasu Dev7f349142009-03-27 09:06:31 -0700467
Vasu Dev1d1b88d2009-07-29 17:05:45 -0700468 wwnn = fcoe_wwn_from_mac(netdev->dev_addr, 1, 0);
Vasu Dev7f349142009-03-27 09:06:31 -0700469 fc_set_wwnn(lp, wwnn);
470 /* XXX - 3rd arg needs to be vlan id */
Vasu Dev1d1b88d2009-07-29 17:05:45 -0700471 wwpn = fcoe_wwn_from_mac(netdev->dev_addr, 2, 0);
Vasu Dev7f349142009-03-27 09:06:31 -0700472 fc_set_wwpn(lp, wwpn);
473
Vasu Dev7f349142009-03-27 09:06:31 -0700474 return 0;
475}
476
477/**
478 * fcoe_shost_config() - Sets up fc_lport->host
479 * @lp : ptr to the fc_lport
480 * @shost : ptr to the associated scsi host
481 * @dev : device associated to scsi host
482 *
483 * Must be called after fcoe_lport_config() and fcoe_netdev_config()
484 *
485 * Returns : 0 for success
486 */
487static int fcoe_shost_config(struct fc_lport *lp, struct Scsi_Host *shost,
488 struct device *dev)
489{
490 int rc = 0;
491
492 /* lport scsi host config */
493 lp->host = shost;
494
495 lp->host->max_lun = FCOE_MAX_LUN;
496 lp->host->max_id = FCOE_MAX_FCP_TARGET;
497 lp->host->max_channel = 0;
498 lp->host->transportt = scsi_transport_fcoe_sw;
499
500 /* add the new host to the SCSI-ml */
501 rc = scsi_add_host(lp->host, dev);
502 if (rc) {
Robert Loved5488eb2009-06-10 15:30:59 -0700503 FCOE_NETDEV_DBG(fcoe_netdev(lp), "fcoe_shost_config: "
504 "error on scsi_add_host\n");
Vasu Dev7f349142009-03-27 09:06:31 -0700505 return rc;
506 }
507 sprintf(fc_host_symbolic_name(lp->host), "%s v%s over %s",
508 FCOE_NAME, FCOE_VERSION,
509 fcoe_netdev(lp)->name);
510
511 return 0;
512}
513
Vasu Devd7179682009-07-29 17:05:21 -0700514/*
515 * fcoe_oem_match() - match for read types IO
516 * @fp: the fc_frame for new IO.
517 *
518 * Returns : true for read types IO, otherwise returns false.
519 */
520bool fcoe_oem_match(struct fc_frame *fp)
521{
Yi Zou05cc7392009-08-25 13:59:03 -0700522 return fc_fcp_is_read(fr_fsp(fp)) &&
523 (fr_fsp(fp)->data_len > fcoe_ddp_min);
Vasu Devd7179682009-07-29 17:05:21 -0700524}
525
Vasu Dev7f349142009-03-27 09:06:31 -0700526/**
527 * fcoe_em_config() - allocates em for this lport
Chris Leech014f5c32009-08-25 13:59:30 -0700528 * @lp: the fcoe that em is to allocated for
Vasu Dev7f349142009-03-27 09:06:31 -0700529 *
Vasu Deve8af4d42009-07-29 17:05:15 -0700530 * Called with write fcoe_hostlist_lock held.
531 *
Vasu Dev7f349142009-03-27 09:06:31 -0700532 * Returns : 0 on success
533 */
534static inline int fcoe_em_config(struct fc_lport *lp)
535{
Chris Leech014f5c32009-08-25 13:59:30 -0700536 struct fcoe_port *port = lport_priv(lp);
Chris Leech25024982009-08-25 13:59:35 -0700537 struct fcoe_interface *fcoe = port->fcoe;
538 struct fcoe_interface *oldfcoe = NULL;
Vasu Dev1d1b88d2009-07-29 17:05:45 -0700539 struct net_device *old_real_dev, *cur_real_dev;
Vasu Devd7179682009-07-29 17:05:21 -0700540 u16 min_xid = FCOE_MIN_XID;
541 u16 max_xid = FCOE_MAX_XID;
542
543 /*
544 * Check if need to allocate an em instance for
545 * offload exchange ids to be shared across all VN_PORTs/lport.
546 */
547 if (!lp->lro_enabled || !lp->lro_xid || (lp->lro_xid >= max_xid)) {
548 lp->lro_xid = 0;
549 goto skip_oem;
550 }
551
552 /*
553 * Reuse existing offload em instance in case
Vasu Dev1d1b88d2009-07-29 17:05:45 -0700554 * it is already allocated on real eth device
Vasu Devd7179682009-07-29 17:05:21 -0700555 */
Chris Leech25024982009-08-25 13:59:35 -0700556 if (fcoe->netdev->priv_flags & IFF_802_1Q_VLAN)
557 cur_real_dev = vlan_dev_real_dev(fcoe->netdev);
Vasu Dev1d1b88d2009-07-29 17:05:45 -0700558 else
Chris Leech25024982009-08-25 13:59:35 -0700559 cur_real_dev = fcoe->netdev;
Vasu Dev1d1b88d2009-07-29 17:05:45 -0700560
Chris Leech25024982009-08-25 13:59:35 -0700561 list_for_each_entry(oldfcoe, &fcoe_hostlist, list) {
Chris Leech25024982009-08-25 13:59:35 -0700562 if (oldfcoe->netdev->priv_flags & IFF_802_1Q_VLAN)
563 old_real_dev = vlan_dev_real_dev(oldfcoe->netdev);
Vasu Dev1d1b88d2009-07-29 17:05:45 -0700564 else
Chris Leech25024982009-08-25 13:59:35 -0700565 old_real_dev = oldfcoe->netdev;
Vasu Dev1d1b88d2009-07-29 17:05:45 -0700566
567 if (cur_real_dev == old_real_dev) {
Chris Leech991cbb62009-08-25 13:59:51 -0700568 fcoe->oem = oldfcoe->oem;
Vasu Devd7179682009-07-29 17:05:21 -0700569 break;
570 }
571 }
572
Chris Leech991cbb62009-08-25 13:59:51 -0700573 if (fcoe->oem) {
574 if (!fc_exch_mgr_add(lp, fcoe->oem, fcoe_oem_match)) {
Vasu Devd7179682009-07-29 17:05:21 -0700575 printk(KERN_ERR "fcoe_em_config: failed to add "
576 "offload em:%p on interface:%s\n",
Chris Leech991cbb62009-08-25 13:59:51 -0700577 fcoe->oem, fcoe->netdev->name);
Vasu Devd7179682009-07-29 17:05:21 -0700578 return -ENOMEM;
579 }
580 } else {
Chris Leech991cbb62009-08-25 13:59:51 -0700581 fcoe->oem = fc_exch_mgr_alloc(lp, FC_CLASS_3,
Vasu Devd7179682009-07-29 17:05:21 -0700582 FCOE_MIN_XID, lp->lro_xid,
583 fcoe_oem_match);
Chris Leech991cbb62009-08-25 13:59:51 -0700584 if (!fcoe->oem) {
Vasu Devd7179682009-07-29 17:05:21 -0700585 printk(KERN_ERR "fcoe_em_config: failed to allocate "
586 "em for offload exches on interface:%s\n",
Chris Leech25024982009-08-25 13:59:35 -0700587 fcoe->netdev->name);
Vasu Devd7179682009-07-29 17:05:21 -0700588 return -ENOMEM;
589 }
590 }
591
592 /*
593 * Exclude offload EM xid range from next EM xid range.
594 */
595 min_xid += lp->lro_xid + 1;
596
597skip_oem:
598 if (!fc_exch_mgr_alloc(lp, FC_CLASS_3, min_xid, max_xid, NULL)) {
599 printk(KERN_ERR "fcoe_em_config: failed to "
Chris Leech25024982009-08-25 13:59:35 -0700600 "allocate em on interface %s\n", fcoe->netdev->name);
Vasu Dev7f349142009-03-27 09:06:31 -0700601 return -ENOMEM;
Vasu Devd7179682009-07-29 17:05:21 -0700602 }
Vasu Dev7f349142009-03-27 09:06:31 -0700603
604 return 0;
605}
606
607/**
608 * fcoe_if_destroy() - FCoE software HBA tear-down function
Chris Leechaf7f85d2009-08-25 13:59:24 -0700609 * @lport: fc_lport to destroy
Vasu Dev7f349142009-03-27 09:06:31 -0700610 */
Chris Leechaf7f85d2009-08-25 13:59:24 -0700611static void fcoe_if_destroy(struct fc_lport *lport)
Vasu Dev7f349142009-03-27 09:06:31 -0700612{
Chris Leech014f5c32009-08-25 13:59:30 -0700613 struct fcoe_port *port = lport_priv(lport);
614 struct fcoe_interface *fcoe = port->fcoe;
Chris Leech25024982009-08-25 13:59:35 -0700615 struct net_device *netdev = fcoe->netdev;
Vasu Dev7f349142009-03-27 09:06:31 -0700616
Robert Loved5488eb2009-06-10 15:30:59 -0700617 FCOE_NETDEV_DBG(netdev, "Destroying interface\n");
Vasu Dev7f349142009-03-27 09:06:31 -0700618
Vasu Dev7f349142009-03-27 09:06:31 -0700619 /* Logout of the fabric */
Chris Leechaf7f85d2009-08-25 13:59:24 -0700620 fc_fabric_logoff(lport);
Vasu Dev7f349142009-03-27 09:06:31 -0700621
Vasu Dev7f349142009-03-27 09:06:31 -0700622 /* Cleanup the fc_lport */
Chris Leechaf7f85d2009-08-25 13:59:24 -0700623 fc_lport_destroy(lport);
624 fc_fcp_destroy(lport);
Vasu Dev7f349142009-03-27 09:06:31 -0700625
Chris Leech54b649f2009-08-25 14:00:07 -0700626 /* Stop the transmit retry timer */
627 del_timer_sync(&port->timer);
628
629 /* Free existing transmit skbs */
630 fcoe_clean_pending_queue(lport);
631
632 /* receives may not be stopped until after this */
633 fcoe_interface_put(fcoe);
634
635 /* Free queued packets for the per-CPU receive threads */
636 fcoe_percpu_clean(lport);
637
Vasu Dev7f349142009-03-27 09:06:31 -0700638 /* Detach from the scsi-ml */
Chris Leechaf7f85d2009-08-25 13:59:24 -0700639 fc_remove_host(lport->host);
640 scsi_remove_host(lport->host);
Vasu Dev7f349142009-03-27 09:06:31 -0700641
642 /* There are no more rports or I/O, free the EM */
Chris Leechaf7f85d2009-08-25 13:59:24 -0700643 fc_exch_mgr_free(lport);
Vasu Dev7f349142009-03-27 09:06:31 -0700644
Vasu Dev7f349142009-03-27 09:06:31 -0700645 /* Free memory used by statistical counters */
Chris Leechaf7f85d2009-08-25 13:59:24 -0700646 fc_lport_free_stats(lport);
Vasu Dev7f349142009-03-27 09:06:31 -0700647
Chris Leech2e70e242009-08-25 14:00:23 -0700648 /* Release the Scsi_Host */
Chris Leechaf7f85d2009-08-25 13:59:24 -0700649 scsi_host_put(lport->host);
Vasu Dev7f349142009-03-27 09:06:31 -0700650}
651
652/*
653 * fcoe_ddp_setup - calls LLD's ddp_setup through net_device
654 * @lp: the corresponding fc_lport
655 * @xid: the exchange id for this ddp transfer
656 * @sgl: the scatterlist describing this transfer
657 * @sgc: number of sg items
658 *
659 * Returns : 0 no ddp
660 */
661static int fcoe_ddp_setup(struct fc_lport *lp, u16 xid,
662 struct scatterlist *sgl, unsigned int sgc)
663{
664 struct net_device *n = fcoe_netdev(lp);
665
666 if (n->netdev_ops && n->netdev_ops->ndo_fcoe_ddp_setup)
667 return n->netdev_ops->ndo_fcoe_ddp_setup(n, xid, sgl, sgc);
668
669 return 0;
670}
671
672/*
673 * fcoe_ddp_done - calls LLD's ddp_done through net_device
674 * @lp: the corresponding fc_lport
675 * @xid: the exchange id for this ddp transfer
676 *
677 * Returns : the length of data that have been completed by ddp
678 */
679static int fcoe_ddp_done(struct fc_lport *lp, u16 xid)
680{
681 struct net_device *n = fcoe_netdev(lp);
682
683 if (n->netdev_ops && n->netdev_ops->ndo_fcoe_ddp_done)
684 return n->netdev_ops->ndo_fcoe_ddp_done(n, xid);
685 return 0;
686}
687
688static struct libfc_function_template fcoe_libfc_fcn_templ = {
689 .frame_send = fcoe_xmit,
690 .ddp_setup = fcoe_ddp_setup,
691 .ddp_done = fcoe_ddp_done,
692};
693
694/**
Chris Leech030f4e02009-08-25 14:00:02 -0700695 * fcoe_if_create() - this function creates the fcoe port
696 * @fcoe: fcoe_interface structure to create an fc_lport instance on
Chris Leechaf7f85d2009-08-25 13:59:24 -0700697 * @parent: device pointer to be the parent in sysfs for the SCSI host
Vasu Dev7f349142009-03-27 09:06:31 -0700698 *
Chris Leech54b649f2009-08-25 14:00:07 -0700699 * Creates fc_lport struct and scsi_host for lport, configures lport.
Vasu Dev7f349142009-03-27 09:06:31 -0700700 *
Chris Leechaf7f85d2009-08-25 13:59:24 -0700701 * Returns : The allocated fc_lport or an error pointer
Vasu Dev7f349142009-03-27 09:06:31 -0700702 */
Chris Leech030f4e02009-08-25 14:00:02 -0700703static struct fc_lport *fcoe_if_create(struct fcoe_interface *fcoe,
Chris Leechaf7f85d2009-08-25 13:59:24 -0700704 struct device *parent)
Vasu Dev7f349142009-03-27 09:06:31 -0700705{
706 int rc;
Chris Leechaf7f85d2009-08-25 13:59:24 -0700707 struct fc_lport *lport = NULL;
Chris Leech014f5c32009-08-25 13:59:30 -0700708 struct fcoe_port *port;
Vasu Dev7f349142009-03-27 09:06:31 -0700709 struct Scsi_Host *shost;
Chris Leech030f4e02009-08-25 14:00:02 -0700710 struct net_device *netdev = fcoe->netdev;
Vasu Dev7f349142009-03-27 09:06:31 -0700711
Robert Loved5488eb2009-06-10 15:30:59 -0700712 FCOE_NETDEV_DBG(netdev, "Create Interface\n");
Vasu Dev7f349142009-03-27 09:06:31 -0700713
Chris Leech014f5c32009-08-25 13:59:30 -0700714 shost = libfc_host_alloc(&fcoe_shost_template,
715 sizeof(struct fcoe_port));
716 if (!shost) {
717 FCOE_NETDEV_DBG(netdev, "Could not allocate host structure\n");
718 rc = -ENOMEM;
Chris Leech030f4e02009-08-25 14:00:02 -0700719 goto out;
Chris Leech014f5c32009-08-25 13:59:30 -0700720 }
Chris Leechaf7f85d2009-08-25 13:59:24 -0700721 lport = shost_priv(shost);
Chris Leech014f5c32009-08-25 13:59:30 -0700722 port = lport_priv(lport);
Chris Leech2e70e242009-08-25 14:00:23 -0700723 port->lport = lport;
Chris Leech014f5c32009-08-25 13:59:30 -0700724 port->fcoe = fcoe;
Chris Leech2e70e242009-08-25 14:00:23 -0700725 INIT_WORK(&port->destroy_work, fcoe_destroy_work);
Vasu Dev7f349142009-03-27 09:06:31 -0700726
727 /* configure fc_lport, e.g., em */
Chris Leechaf7f85d2009-08-25 13:59:24 -0700728 rc = fcoe_lport_config(lport);
Vasu Dev7f349142009-03-27 09:06:31 -0700729 if (rc) {
Robert Loved5488eb2009-06-10 15:30:59 -0700730 FCOE_NETDEV_DBG(netdev, "Could not configure lport for the "
731 "interface\n");
Vasu Dev7f349142009-03-27 09:06:31 -0700732 goto out_host_put;
733 }
734
Vasu Devab6b85c2009-05-17 12:33:08 +0000735 /* configure lport network properties */
Chris Leechaf7f85d2009-08-25 13:59:24 -0700736 rc = fcoe_netdev_config(lport, netdev);
Vasu Devab6b85c2009-05-17 12:33:08 +0000737 if (rc) {
Robert Loved5488eb2009-06-10 15:30:59 -0700738 FCOE_NETDEV_DBG(netdev, "Could not configure netdev for the "
739 "interface\n");
Chris Leech54b649f2009-08-25 14:00:07 -0700740 goto out_lp_destroy;
Vasu Devab6b85c2009-05-17 12:33:08 +0000741 }
Joe Eykholt97c83892009-03-17 11:42:40 -0700742
Vasu Dev7f349142009-03-27 09:06:31 -0700743 /* configure lport scsi host properties */
Chris Leechaf7f85d2009-08-25 13:59:24 -0700744 rc = fcoe_shost_config(lport, shost, parent);
Vasu Dev7f349142009-03-27 09:06:31 -0700745 if (rc) {
Robert Loved5488eb2009-06-10 15:30:59 -0700746 FCOE_NETDEV_DBG(netdev, "Could not configure shost for the "
747 "interface\n");
Chris Leech54b649f2009-08-25 14:00:07 -0700748 goto out_lp_destroy;
Vasu Dev7f349142009-03-27 09:06:31 -0700749 }
750
Vasu Dev7f349142009-03-27 09:06:31 -0700751 /* Initialize the library */
Chris Leechaf7f85d2009-08-25 13:59:24 -0700752 rc = fcoe_libfc_config(lport, &fcoe_libfc_fcn_templ);
Vasu Dev7f349142009-03-27 09:06:31 -0700753 if (rc) {
Robert Loved5488eb2009-06-10 15:30:59 -0700754 FCOE_NETDEV_DBG(netdev, "Could not configure libfc for the "
755 "interface\n");
Vasu Dev7f349142009-03-27 09:06:31 -0700756 goto out_lp_destroy;
757 }
758
Vasu Deve8af4d42009-07-29 17:05:15 -0700759 /*
760 * fcoe_em_alloc() and fcoe_hostlist_add() both
Chris Leechc863df32009-08-25 14:00:18 -0700761 * need to be atomic with respect to other changes to the hostlist
Vasu Deve8af4d42009-07-29 17:05:15 -0700762 * since fcoe_em_alloc() looks for an existing EM
763 * instance on host list updated by fcoe_hostlist_add().
Chris Leechc863df32009-08-25 14:00:18 -0700764 *
765 * This is currently handled through the fcoe_config_mutex begin held.
Vasu Deve8af4d42009-07-29 17:05:15 -0700766 */
Chris Leechc863df32009-08-25 14:00:18 -0700767
Vasu Dev96316092009-07-29 17:05:00 -0700768 /* lport exch manager allocation */
Chris Leechaf7f85d2009-08-25 13:59:24 -0700769 rc = fcoe_em_config(lport);
Vasu Dev96316092009-07-29 17:05:00 -0700770 if (rc) {
771 FCOE_NETDEV_DBG(netdev, "Could not configure the EM for the "
772 "interface\n");
773 goto out_lp_destroy;
774 }
775
Chris Leech030f4e02009-08-25 14:00:02 -0700776 fcoe_interface_get(fcoe);
Chris Leechaf7f85d2009-08-25 13:59:24 -0700777 return lport;
Vasu Dev7f349142009-03-27 09:06:31 -0700778
779out_lp_destroy:
Chris Leechaf7f85d2009-08-25 13:59:24 -0700780 fc_exch_mgr_free(lport);
Vasu Dev7f349142009-03-27 09:06:31 -0700781out_host_put:
Chris Leechaf7f85d2009-08-25 13:59:24 -0700782 scsi_host_put(lport->host);
783out:
784 return ERR_PTR(rc);
Vasu Dev7f349142009-03-27 09:06:31 -0700785}
786
787/**
788 * fcoe_if_init() - attach to scsi transport
789 *
790 * Returns : 0 on success
791 */
792static int __init fcoe_if_init(void)
793{
794 /* attach to scsi transport */
795 scsi_transport_fcoe_sw =
796 fc_attach_transport(&fcoe_transport_function);
797
798 if (!scsi_transport_fcoe_sw) {
Robert Loved5488eb2009-06-10 15:30:59 -0700799 printk(KERN_ERR "fcoe: Failed to attach to the FC transport\n");
Vasu Dev7f349142009-03-27 09:06:31 -0700800 return -ENODEV;
801 }
802
803 return 0;
804}
805
806/**
807 * fcoe_if_exit() - detach from scsi transport
808 *
809 * Returns : 0 on success
810 */
811int __exit fcoe_if_exit(void)
812{
813 fc_release_transport(scsi_transport_fcoe_sw);
Chris Leechdfc1d0f2009-08-25 14:00:13 -0700814 scsi_transport_fcoe_sw = NULL;
Vasu Dev7f349142009-03-27 09:06:31 -0700815 return 0;
816}
817
Robert Love85b4aa42008-12-09 15:10:24 -0800818/**
Robert Love8976f422009-03-17 11:41:46 -0700819 * fcoe_percpu_thread_create() - Create a receive thread for an online cpu
820 * @cpu: cpu index for the online cpu
821 */
822static void fcoe_percpu_thread_create(unsigned int cpu)
823{
824 struct fcoe_percpu_s *p;
825 struct task_struct *thread;
826
827 p = &per_cpu(fcoe_percpu, cpu);
828
829 thread = kthread_create(fcoe_percpu_receive_thread,
830 (void *)p, "fcoethread/%d", cpu);
831
832 if (likely(!IS_ERR(p->thread))) {
833 kthread_bind(thread, cpu);
834 wake_up_process(thread);
835
836 spin_lock_bh(&p->fcoe_rx_list.lock);
837 p->thread = thread;
838 spin_unlock_bh(&p->fcoe_rx_list.lock);
839 }
840}
841
842/**
843 * fcoe_percpu_thread_destroy() - removes the rx thread for the given cpu
844 * @cpu: cpu index the rx thread is to be removed
845 *
846 * Destroys a per-CPU Rx thread. Any pending skbs are moved to the
847 * current CPU's Rx thread. If the thread being destroyed is bound to
848 * the CPU processing this context the skbs will be freed.
849 */
850static void fcoe_percpu_thread_destroy(unsigned int cpu)
851{
852 struct fcoe_percpu_s *p;
853 struct task_struct *thread;
854 struct page *crc_eof;
855 struct sk_buff *skb;
856#ifdef CONFIG_SMP
857 struct fcoe_percpu_s *p0;
858 unsigned targ_cpu = smp_processor_id();
859#endif /* CONFIG_SMP */
860
Robert Loved5488eb2009-06-10 15:30:59 -0700861 FCOE_DBG("Destroying receive thread for CPU %d\n", cpu);
Robert Love8976f422009-03-17 11:41:46 -0700862
863 /* Prevent any new skbs from being queued for this CPU. */
864 p = &per_cpu(fcoe_percpu, cpu);
865 spin_lock_bh(&p->fcoe_rx_list.lock);
866 thread = p->thread;
867 p->thread = NULL;
868 crc_eof = p->crc_eof_page;
869 p->crc_eof_page = NULL;
870 p->crc_eof_offset = 0;
871 spin_unlock_bh(&p->fcoe_rx_list.lock);
872
873#ifdef CONFIG_SMP
874 /*
875 * Don't bother moving the skb's if this context is running
876 * on the same CPU that is having its thread destroyed. This
877 * can easily happen when the module is removed.
878 */
879 if (cpu != targ_cpu) {
880 p0 = &per_cpu(fcoe_percpu, targ_cpu);
881 spin_lock_bh(&p0->fcoe_rx_list.lock);
882 if (p0->thread) {
Robert Loved5488eb2009-06-10 15:30:59 -0700883 FCOE_DBG("Moving frames from CPU %d to CPU %d\n",
884 cpu, targ_cpu);
Robert Love8976f422009-03-17 11:41:46 -0700885
886 while ((skb = __skb_dequeue(&p->fcoe_rx_list)) != NULL)
887 __skb_queue_tail(&p0->fcoe_rx_list, skb);
888 spin_unlock_bh(&p0->fcoe_rx_list.lock);
889 } else {
890 /*
891 * The targeted CPU is not initialized and cannot accept
892 * new skbs. Unlock the targeted CPU and drop the skbs
893 * on the CPU that is going offline.
894 */
895 while ((skb = __skb_dequeue(&p->fcoe_rx_list)) != NULL)
896 kfree_skb(skb);
897 spin_unlock_bh(&p0->fcoe_rx_list.lock);
898 }
899 } else {
900 /*
901 * This scenario occurs when the module is being removed
902 * and all threads are being destroyed. skbs will continue
903 * to be shifted from the CPU thread that is being removed
904 * to the CPU thread associated with the CPU that is processing
905 * the module removal. Once there is only one CPU Rx thread it
906 * will reach this case and we will drop all skbs and later
907 * stop the thread.
908 */
909 spin_lock_bh(&p->fcoe_rx_list.lock);
910 while ((skb = __skb_dequeue(&p->fcoe_rx_list)) != NULL)
911 kfree_skb(skb);
912 spin_unlock_bh(&p->fcoe_rx_list.lock);
913 }
914#else
915 /*
Chris Leechdd3fd722009-04-21 16:27:36 -0700916 * This a non-SMP scenario where the singular Rx thread is
Robert Love8976f422009-03-17 11:41:46 -0700917 * being removed. Free all skbs and stop the thread.
918 */
919 spin_lock_bh(&p->fcoe_rx_list.lock);
920 while ((skb = __skb_dequeue(&p->fcoe_rx_list)) != NULL)
921 kfree_skb(skb);
922 spin_unlock_bh(&p->fcoe_rx_list.lock);
923#endif
924
925 if (thread)
926 kthread_stop(thread);
927
928 if (crc_eof)
929 put_page(crc_eof);
930}
931
932/**
933 * fcoe_cpu_callback() - fcoe cpu hotplug event callback
934 * @nfb: callback data block
935 * @action: event triggering the callback
936 * @hcpu: index for the cpu of this event
937 *
938 * This creates or destroys per cpu data for fcoe
939 *
940 * Returns NOTIFY_OK always.
941 */
942static int fcoe_cpu_callback(struct notifier_block *nfb,
943 unsigned long action, void *hcpu)
944{
945 unsigned cpu = (unsigned long)hcpu;
946
947 switch (action) {
948 case CPU_ONLINE:
949 case CPU_ONLINE_FROZEN:
Robert Loved5488eb2009-06-10 15:30:59 -0700950 FCOE_DBG("CPU %x online: Create Rx thread\n", cpu);
Robert Love8976f422009-03-17 11:41:46 -0700951 fcoe_percpu_thread_create(cpu);
952 break;
953 case CPU_DEAD:
954 case CPU_DEAD_FROZEN:
Robert Loved5488eb2009-06-10 15:30:59 -0700955 FCOE_DBG("CPU %x offline: Remove Rx thread\n", cpu);
Robert Love8976f422009-03-17 11:41:46 -0700956 fcoe_percpu_thread_destroy(cpu);
957 break;
958 default:
959 break;
960 }
961 return NOTIFY_OK;
962}
963
964static struct notifier_block fcoe_cpu_notifier = {
965 .notifier_call = fcoe_cpu_callback,
966};
967
968/**
Robert Love34f42a02009-02-27 10:55:45 -0800969 * fcoe_rcv() - this is the fcoe receive function called by NET_RX_SOFTIRQ
Robert Love85b4aa42008-12-09 15:10:24 -0800970 * @skb: the receive skb
971 * @dev: associated net device
972 * @ptype: context
Chris Leechdd3fd722009-04-21 16:27:36 -0700973 * @olddev: last device
Robert Love85b4aa42008-12-09 15:10:24 -0800974 *
975 * this function will receive the packet and build fc frame and pass it up
976 *
977 * Returns: 0 for success
Robert Love34f42a02009-02-27 10:55:45 -0800978 */
Robert Love85b4aa42008-12-09 15:10:24 -0800979int fcoe_rcv(struct sk_buff *skb, struct net_device *dev,
980 struct packet_type *ptype, struct net_device *olddev)
981{
982 struct fc_lport *lp;
983 struct fcoe_rcv_info *fr;
Chris Leech259ad852009-08-25 13:59:41 -0700984 struct fcoe_interface *fcoe;
Robert Love85b4aa42008-12-09 15:10:24 -0800985 struct fc_frame_header *fh;
Robert Love85b4aa42008-12-09 15:10:24 -0800986 struct fcoe_percpu_s *fps;
Vasu Devb2f00912009-08-25 13:58:53 -0700987 unsigned int cpu;
Robert Love85b4aa42008-12-09 15:10:24 -0800988
Chris Leech259ad852009-08-25 13:59:41 -0700989 fcoe = container_of(ptype, struct fcoe_interface, fcoe_packet_type);
Chris Leech3fe9a0b2009-08-25 13:59:46 -0700990 lp = fcoe->ctlr.lp;
Robert Love85b4aa42008-12-09 15:10:24 -0800991 if (unlikely(lp == NULL)) {
Robert Loved5488eb2009-06-10 15:30:59 -0700992 FCOE_NETDEV_DBG(dev, "Cannot find hba structure");
Robert Love85b4aa42008-12-09 15:10:24 -0800993 goto err2;
994 }
Joe Eykholt97c83892009-03-17 11:42:40 -0700995 if (!lp->link_up)
996 goto err2;
Robert Love85b4aa42008-12-09 15:10:24 -0800997
Robert Loved5488eb2009-06-10 15:30:59 -0700998 FCOE_NETDEV_DBG(dev, "skb_info: len:%d data_len:%d head:%p "
999 "data:%p tail:%p end:%p sum:%d dev:%s",
1000 skb->len, skb->data_len, skb->head, skb->data,
1001 skb_tail_pointer(skb), skb_end_pointer(skb),
1002 skb->csum, skb->dev ? skb->dev->name : "<NULL>");
Robert Love85b4aa42008-12-09 15:10:24 -08001003
1004 /* check for FCOE packet type */
1005 if (unlikely(eth_hdr(skb)->h_proto != htons(ETH_P_FCOE))) {
Robert Loved5488eb2009-06-10 15:30:59 -07001006 FCOE_NETDEV_DBG(dev, "Wrong FC type frame");
Robert Love85b4aa42008-12-09 15:10:24 -08001007 goto err;
1008 }
1009
1010 /*
1011 * Check for minimum frame length, and make sure required FCoE
1012 * and FC headers are pulled into the linear data area.
1013 */
1014 if (unlikely((skb->len < FCOE_MIN_FRAME) ||
1015 !pskb_may_pull(skb, FCOE_HEADER_LEN)))
1016 goto err;
1017
1018 skb_set_transport_header(skb, sizeof(struct fcoe_hdr));
1019 fh = (struct fc_frame_header *) skb_transport_header(skb);
1020
Robert Love85b4aa42008-12-09 15:10:24 -08001021 fr = fcoe_dev_from_skb(skb);
1022 fr->fr_dev = lp;
1023 fr->ptype = ptype;
Robert Love5e5e92d2009-03-17 11:41:35 -07001024
Robert Love85b4aa42008-12-09 15:10:24 -08001025 /*
Vasu Devb2f00912009-08-25 13:58:53 -07001026 * In case the incoming frame's exchange is originated from
1027 * the initiator, then received frame's exchange id is ANDed
1028 * with fc_cpu_mask bits to get the same cpu on which exchange
1029 * was originated, otherwise just use the current cpu.
Robert Love85b4aa42008-12-09 15:10:24 -08001030 */
Vasu Devb2f00912009-08-25 13:58:53 -07001031 if (ntoh24(fh->fh_f_ctl) & FC_FC_EX_CTX)
1032 cpu = ntohs(fh->fh_ox_id) & fc_cpu_mask;
1033 else
1034 cpu = smp_processor_id();
Robert Love5e5e92d2009-03-17 11:41:35 -07001035
Robert Love8976f422009-03-17 11:41:46 -07001036 fps = &per_cpu(fcoe_percpu, cpu);
Robert Love85b4aa42008-12-09 15:10:24 -08001037 spin_lock_bh(&fps->fcoe_rx_list.lock);
Robert Love8976f422009-03-17 11:41:46 -07001038 if (unlikely(!fps->thread)) {
1039 /*
1040 * The targeted CPU is not ready, let's target
1041 * the first CPU now. For non-SMP systems this
1042 * will check the same CPU twice.
1043 */
Robert Loved5488eb2009-06-10 15:30:59 -07001044 FCOE_NETDEV_DBG(dev, "CPU is online, but no receive thread "
1045 "ready for incoming skb- using first online "
1046 "CPU.\n");
Robert Love8976f422009-03-17 11:41:46 -07001047
1048 spin_unlock_bh(&fps->fcoe_rx_list.lock);
1049 cpu = first_cpu(cpu_online_map);
1050 fps = &per_cpu(fcoe_percpu, cpu);
1051 spin_lock_bh(&fps->fcoe_rx_list.lock);
1052 if (!fps->thread) {
1053 spin_unlock_bh(&fps->fcoe_rx_list.lock);
1054 goto err;
1055 }
1056 }
1057
1058 /*
1059 * We now have a valid CPU that we're targeting for
1060 * this skb. We also have this receive thread locked,
1061 * so we're free to queue skbs into it's queue.
1062 */
Robert Love85b4aa42008-12-09 15:10:24 -08001063 __skb_queue_tail(&fps->fcoe_rx_list, skb);
1064 if (fps->fcoe_rx_list.qlen == 1)
1065 wake_up_process(fps->thread);
1066
1067 spin_unlock_bh(&fps->fcoe_rx_list.lock);
1068
1069 return 0;
1070err:
Robert Love582b45b2009-03-31 15:51:50 -07001071 fc_lport_get_stats(lp)->ErrorFrames++;
Robert Love85b4aa42008-12-09 15:10:24 -08001072
1073err2:
1074 kfree_skb(skb);
1075 return -1;
1076}
Robert Love85b4aa42008-12-09 15:10:24 -08001077
1078/**
Robert Love34f42a02009-02-27 10:55:45 -08001079 * fcoe_start_io() - pass to netdev to start xmit for fcoe
Robert Love85b4aa42008-12-09 15:10:24 -08001080 * @skb: the skb to be xmitted
1081 *
1082 * Returns: 0 for success
Robert Love34f42a02009-02-27 10:55:45 -08001083 */
Robert Love85b4aa42008-12-09 15:10:24 -08001084static inline int fcoe_start_io(struct sk_buff *skb)
1085{
1086 int rc;
1087
1088 skb_get(skb);
1089 rc = dev_queue_xmit(skb);
1090 if (rc != 0)
1091 return rc;
1092 kfree_skb(skb);
1093 return 0;
1094}
1095
1096/**
Chris Leechdd3fd722009-04-21 16:27:36 -07001097 * fcoe_get_paged_crc_eof() - in case we need to alloc a page for crc_eof
Robert Love85b4aa42008-12-09 15:10:24 -08001098 * @skb: the skb to be xmitted
1099 * @tlen: total len
1100 *
1101 * Returns: 0 for success
Robert Love34f42a02009-02-27 10:55:45 -08001102 */
Robert Love85b4aa42008-12-09 15:10:24 -08001103static int fcoe_get_paged_crc_eof(struct sk_buff *skb, int tlen)
1104{
1105 struct fcoe_percpu_s *fps;
1106 struct page *page;
Robert Love85b4aa42008-12-09 15:10:24 -08001107
Robert Love5e5e92d2009-03-17 11:41:35 -07001108 fps = &get_cpu_var(fcoe_percpu);
Robert Love85b4aa42008-12-09 15:10:24 -08001109 page = fps->crc_eof_page;
1110 if (!page) {
1111 page = alloc_page(GFP_ATOMIC);
1112 if (!page) {
Robert Love5e5e92d2009-03-17 11:41:35 -07001113 put_cpu_var(fcoe_percpu);
Robert Love85b4aa42008-12-09 15:10:24 -08001114 return -ENOMEM;
1115 }
1116 fps->crc_eof_page = page;
Robert Love8976f422009-03-17 11:41:46 -07001117 fps->crc_eof_offset = 0;
Robert Love85b4aa42008-12-09 15:10:24 -08001118 }
1119
1120 get_page(page);
1121 skb_fill_page_desc(skb, skb_shinfo(skb)->nr_frags, page,
1122 fps->crc_eof_offset, tlen);
1123 skb->len += tlen;
1124 skb->data_len += tlen;
1125 skb->truesize += tlen;
1126 fps->crc_eof_offset += sizeof(struct fcoe_crc_eof);
1127
1128 if (fps->crc_eof_offset >= PAGE_SIZE) {
1129 fps->crc_eof_page = NULL;
1130 fps->crc_eof_offset = 0;
1131 put_page(page);
1132 }
Robert Love5e5e92d2009-03-17 11:41:35 -07001133 put_cpu_var(fcoe_percpu);
Robert Love85b4aa42008-12-09 15:10:24 -08001134 return 0;
1135}
1136
1137/**
Robert Love34f42a02009-02-27 10:55:45 -08001138 * fcoe_fc_crc() - calculates FC CRC in this fcoe skb
Chris Leechdd3fd722009-04-21 16:27:36 -07001139 * @fp: the fc_frame containing data to be checksummed
Robert Love85b4aa42008-12-09 15:10:24 -08001140 *
Chris Leech014f5c32009-08-25 13:59:30 -07001141 * This uses crc32() to calculate the crc for port frame
Robert Love85b4aa42008-12-09 15:10:24 -08001142 * Return : 32 bit crc
Robert Love34f42a02009-02-27 10:55:45 -08001143 */
Robert Love85b4aa42008-12-09 15:10:24 -08001144u32 fcoe_fc_crc(struct fc_frame *fp)
1145{
1146 struct sk_buff *skb = fp_skb(fp);
1147 struct skb_frag_struct *frag;
1148 unsigned char *data;
1149 unsigned long off, len, clen;
1150 u32 crc;
1151 unsigned i;
1152
1153 crc = crc32(~0, skb->data, skb_headlen(skb));
1154
1155 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
1156 frag = &skb_shinfo(skb)->frags[i];
1157 off = frag->page_offset;
1158 len = frag->size;
1159 while (len > 0) {
1160 clen = min(len, PAGE_SIZE - (off & ~PAGE_MASK));
1161 data = kmap_atomic(frag->page + (off >> PAGE_SHIFT),
1162 KM_SKB_DATA_SOFTIRQ);
1163 crc = crc32(crc, data + (off & ~PAGE_MASK), clen);
1164 kunmap_atomic(data, KM_SKB_DATA_SOFTIRQ);
1165 off += clen;
1166 len -= clen;
1167 }
1168 }
1169 return crc;
1170}
Robert Love85b4aa42008-12-09 15:10:24 -08001171
1172/**
Robert Love34f42a02009-02-27 10:55:45 -08001173 * fcoe_xmit() - FCoE frame transmit function
Chris Leech014f5c32009-08-25 13:59:30 -07001174 * @lp: the associated local fcoe
Robert Love85b4aa42008-12-09 15:10:24 -08001175 * @fp: the fc_frame to be transmitted
1176 *
1177 * Return : 0 for success
Robert Love34f42a02009-02-27 10:55:45 -08001178 */
Robert Love85b4aa42008-12-09 15:10:24 -08001179int fcoe_xmit(struct fc_lport *lp, struct fc_frame *fp)
1180{
Vasu Dev4bb6b512009-05-06 10:52:34 -07001181 int wlen;
Robert Love85b4aa42008-12-09 15:10:24 -08001182 u32 crc;
1183 struct ethhdr *eh;
1184 struct fcoe_crc_eof *cp;
1185 struct sk_buff *skb;
1186 struct fcoe_dev_stats *stats;
1187 struct fc_frame_header *fh;
1188 unsigned int hlen; /* header length implies the version */
1189 unsigned int tlen; /* trailer length */
1190 unsigned int elen; /* eth header, may include vlan */
Chris Leech3fe9a0b2009-08-25 13:59:46 -07001191 struct fcoe_port *port = lport_priv(lp);
1192 struct fcoe_interface *fcoe = port->fcoe;
Robert Love85b4aa42008-12-09 15:10:24 -08001193 u8 sof, eof;
1194 struct fcoe_hdr *hp;
1195
1196 WARN_ON((fr_len(fp) % sizeof(u32)) != 0);
1197
Robert Love85b4aa42008-12-09 15:10:24 -08001198 fh = fc_frame_header_get(fp);
Joe Eykholt97c83892009-03-17 11:42:40 -07001199 skb = fp_skb(fp);
1200 wlen = skb->len / FCOE_WORD_TO_BYTE;
1201
1202 if (!lp->link_up) {
Dan Carpenter3caf02e2009-04-21 16:27:25 -07001203 kfree_skb(skb);
Joe Eykholt97c83892009-03-17 11:42:40 -07001204 return 0;
Robert Love85b4aa42008-12-09 15:10:24 -08001205 }
1206
Joe Eykholt97c83892009-03-17 11:42:40 -07001207 if (unlikely(fh->fh_r_ctl == FC_RCTL_ELS_REQ) &&
Chris Leech3fe9a0b2009-08-25 13:59:46 -07001208 fcoe_ctlr_els_send(&fcoe->ctlr, skb))
Joe Eykholt97c83892009-03-17 11:42:40 -07001209 return 0;
1210
Robert Love85b4aa42008-12-09 15:10:24 -08001211 sof = fr_sof(fp);
1212 eof = fr_eof(fp);
1213
Vasu Dev4e57e1c2009-05-06 10:52:46 -07001214 elen = sizeof(struct ethhdr);
Robert Love85b4aa42008-12-09 15:10:24 -08001215 hlen = sizeof(struct fcoe_hdr);
1216 tlen = sizeof(struct fcoe_crc_eof);
1217 wlen = (skb->len - tlen + sizeof(crc)) / FCOE_WORD_TO_BYTE;
1218
1219 /* crc offload */
1220 if (likely(lp->crc_offload)) {
Yi Zou39ca9a02009-02-27 14:07:15 -08001221 skb->ip_summed = CHECKSUM_PARTIAL;
Robert Love85b4aa42008-12-09 15:10:24 -08001222 skb->csum_start = skb_headroom(skb);
1223 skb->csum_offset = skb->len;
1224 crc = 0;
1225 } else {
1226 skb->ip_summed = CHECKSUM_NONE;
1227 crc = fcoe_fc_crc(fp);
1228 }
1229
Chris Leech014f5c32009-08-25 13:59:30 -07001230 /* copy port crc and eof to the skb buff */
Robert Love85b4aa42008-12-09 15:10:24 -08001231 if (skb_is_nonlinear(skb)) {
1232 skb_frag_t *frag;
1233 if (fcoe_get_paged_crc_eof(skb, tlen)) {
Roel Kluine9041582009-02-27 10:56:22 -08001234 kfree_skb(skb);
Robert Love85b4aa42008-12-09 15:10:24 -08001235 return -ENOMEM;
1236 }
1237 frag = &skb_shinfo(skb)->frags[skb_shinfo(skb)->nr_frags - 1];
1238 cp = kmap_atomic(frag->page, KM_SKB_DATA_SOFTIRQ)
1239 + frag->page_offset;
1240 } else {
1241 cp = (struct fcoe_crc_eof *)skb_put(skb, tlen);
1242 }
1243
1244 memset(cp, 0, sizeof(*cp));
1245 cp->fcoe_eof = eof;
1246 cp->fcoe_crc32 = cpu_to_le32(~crc);
1247
1248 if (skb_is_nonlinear(skb)) {
1249 kunmap_atomic(cp, KM_SKB_DATA_SOFTIRQ);
1250 cp = NULL;
1251 }
1252
Chris Leech014f5c32009-08-25 13:59:30 -07001253 /* adjust skb network/transport offsets to match mac/fcoe/port */
Robert Love85b4aa42008-12-09 15:10:24 -08001254 skb_push(skb, elen + hlen);
1255 skb_reset_mac_header(skb);
1256 skb_reset_network_header(skb);
1257 skb->mac_len = elen;
Yi Zou211c7382009-02-27 14:06:37 -08001258 skb->protocol = htons(ETH_P_FCOE);
Chris Leech3fe9a0b2009-08-25 13:59:46 -07001259 skb->dev = fcoe->netdev;
Robert Love85b4aa42008-12-09 15:10:24 -08001260
1261 /* fill up mac and fcoe headers */
1262 eh = eth_hdr(skb);
1263 eh->h_proto = htons(ETH_P_FCOE);
Chris Leech3fe9a0b2009-08-25 13:59:46 -07001264 if (fcoe->ctlr.map_dest)
Robert Love85b4aa42008-12-09 15:10:24 -08001265 fc_fcoe_set_mac(eh->h_dest, fh->fh_d_id);
1266 else
1267 /* insert GW address */
Chris Leech3fe9a0b2009-08-25 13:59:46 -07001268 memcpy(eh->h_dest, fcoe->ctlr.dest_addr, ETH_ALEN);
Robert Love85b4aa42008-12-09 15:10:24 -08001269
Chris Leech3fe9a0b2009-08-25 13:59:46 -07001270 if (unlikely(fcoe->ctlr.flogi_oxid != FC_XID_UNKNOWN))
1271 memcpy(eh->h_source, fcoe->ctlr.ctl_src_addr, ETH_ALEN);
Robert Love85b4aa42008-12-09 15:10:24 -08001272 else
Chris Leech3fe9a0b2009-08-25 13:59:46 -07001273 memcpy(eh->h_source, fcoe->ctlr.data_src_addr, ETH_ALEN);
Robert Love85b4aa42008-12-09 15:10:24 -08001274
1275 hp = (struct fcoe_hdr *)(eh + 1);
1276 memset(hp, 0, sizeof(*hp));
1277 if (FC_FCOE_VER)
1278 FC_FCOE_ENCAPS_VER(hp, FC_FCOE_VER);
1279 hp->fcoe_sof = sof;
1280
Yi Zou39ca9a02009-02-27 14:07:15 -08001281 /* fcoe lso, mss is in max_payload which is non-zero for FCP data */
1282 if (lp->seq_offload && fr_max_payload(fp)) {
1283 skb_shinfo(skb)->gso_type = SKB_GSO_FCOE;
1284 skb_shinfo(skb)->gso_size = fr_max_payload(fp);
1285 } else {
1286 skb_shinfo(skb)->gso_type = 0;
1287 skb_shinfo(skb)->gso_size = 0;
1288 }
Robert Love85b4aa42008-12-09 15:10:24 -08001289 /* update tx stats: regardless if LLD fails */
Robert Love582b45b2009-03-31 15:51:50 -07001290 stats = fc_lport_get_stats(lp);
1291 stats->TxFrames++;
1292 stats->TxWords += wlen;
Robert Love85b4aa42008-12-09 15:10:24 -08001293
1294 /* send down to lld */
1295 fr_dev(fp) = lp;
Chris Leech014f5c32009-08-25 13:59:30 -07001296 if (port->fcoe_pending_queue.qlen)
Vasu Dev4bb6b512009-05-06 10:52:34 -07001297 fcoe_check_wait_queue(lp, skb);
1298 else if (fcoe_start_io(skb))
1299 fcoe_check_wait_queue(lp, skb);
Robert Love85b4aa42008-12-09 15:10:24 -08001300
1301 return 0;
1302}
Robert Love85b4aa42008-12-09 15:10:24 -08001303
Robert Love34f42a02009-02-27 10:55:45 -08001304/**
1305 * fcoe_percpu_receive_thread() - recv thread per cpu
Robert Love85b4aa42008-12-09 15:10:24 -08001306 * @arg: ptr to the fcoe per cpu struct
1307 *
1308 * Return: 0 for success
Robert Love85b4aa42008-12-09 15:10:24 -08001309 */
1310int fcoe_percpu_receive_thread(void *arg)
1311{
1312 struct fcoe_percpu_s *p = arg;
1313 u32 fr_len;
1314 struct fc_lport *lp;
1315 struct fcoe_rcv_info *fr;
1316 struct fcoe_dev_stats *stats;
1317 struct fc_frame_header *fh;
1318 struct sk_buff *skb;
1319 struct fcoe_crc_eof crc_eof;
1320 struct fc_frame *fp;
1321 u8 *mac = NULL;
Chris Leech014f5c32009-08-25 13:59:30 -07001322 struct fcoe_port *port;
Robert Love85b4aa42008-12-09 15:10:24 -08001323 struct fcoe_hdr *hp;
1324
Robert Love4469c192009-02-27 10:56:38 -08001325 set_user_nice(current, -20);
Robert Love85b4aa42008-12-09 15:10:24 -08001326
1327 while (!kthread_should_stop()) {
1328
1329 spin_lock_bh(&p->fcoe_rx_list.lock);
1330 while ((skb = __skb_dequeue(&p->fcoe_rx_list)) == NULL) {
1331 set_current_state(TASK_INTERRUPTIBLE);
1332 spin_unlock_bh(&p->fcoe_rx_list.lock);
1333 schedule();
1334 set_current_state(TASK_RUNNING);
1335 if (kthread_should_stop())
1336 return 0;
1337 spin_lock_bh(&p->fcoe_rx_list.lock);
1338 }
1339 spin_unlock_bh(&p->fcoe_rx_list.lock);
1340 fr = fcoe_dev_from_skb(skb);
1341 lp = fr->fr_dev;
1342 if (unlikely(lp == NULL)) {
Robert Loved5488eb2009-06-10 15:30:59 -07001343 FCOE_NETDEV_DBG(skb->dev, "Invalid HBA Structure");
Robert Love85b4aa42008-12-09 15:10:24 -08001344 kfree_skb(skb);
1345 continue;
1346 }
1347
Robert Loved5488eb2009-06-10 15:30:59 -07001348 FCOE_NETDEV_DBG(skb->dev, "skb_info: len:%d data_len:%d "
1349 "head:%p data:%p tail:%p end:%p sum:%d dev:%s",
1350 skb->len, skb->data_len,
1351 skb->head, skb->data, skb_tail_pointer(skb),
1352 skb_end_pointer(skb), skb->csum,
1353 skb->dev ? skb->dev->name : "<NULL>");
Robert Love85b4aa42008-12-09 15:10:24 -08001354
1355 /*
1356 * Save source MAC address before discarding header.
1357 */
Chris Leech014f5c32009-08-25 13:59:30 -07001358 port = lport_priv(lp);
Robert Love85b4aa42008-12-09 15:10:24 -08001359 if (skb_is_nonlinear(skb))
1360 skb_linearize(skb); /* not ideal */
Joe Eykholt97c83892009-03-17 11:42:40 -07001361 mac = eth_hdr(skb)->h_source;
Robert Love85b4aa42008-12-09 15:10:24 -08001362
1363 /*
1364 * Frame length checks and setting up the header pointers
1365 * was done in fcoe_rcv already.
1366 */
1367 hp = (struct fcoe_hdr *) skb_network_header(skb);
1368 fh = (struct fc_frame_header *) skb_transport_header(skb);
1369
Robert Love582b45b2009-03-31 15:51:50 -07001370 stats = fc_lport_get_stats(lp);
Robert Love85b4aa42008-12-09 15:10:24 -08001371 if (unlikely(FC_FCOE_DECAPS_VER(hp) != FC_FCOE_VER)) {
Robert Love582b45b2009-03-31 15:51:50 -07001372 if (stats->ErrorFrames < 5)
Robert Loved5488eb2009-06-10 15:30:59 -07001373 printk(KERN_WARNING "fcoe: FCoE version "
Robert Love582b45b2009-03-31 15:51:50 -07001374 "mismatch: The frame has "
1375 "version %x, but the "
1376 "initiator supports version "
1377 "%x\n", FC_FCOE_DECAPS_VER(hp),
1378 FC_FCOE_VER);
1379 stats->ErrorFrames++;
Robert Love85b4aa42008-12-09 15:10:24 -08001380 kfree_skb(skb);
1381 continue;
1382 }
1383
1384 skb_pull(skb, sizeof(struct fcoe_hdr));
1385 fr_len = skb->len - sizeof(struct fcoe_crc_eof);
1386
Robert Love582b45b2009-03-31 15:51:50 -07001387 stats->RxFrames++;
1388 stats->RxWords += fr_len / FCOE_WORD_TO_BYTE;
Robert Love85b4aa42008-12-09 15:10:24 -08001389
1390 fp = (struct fc_frame *)skb;
1391 fc_frame_init(fp);
1392 fr_dev(fp) = lp;
1393 fr_sof(fp) = hp->fcoe_sof;
1394
1395 /* Copy out the CRC and EOF trailer for access */
1396 if (skb_copy_bits(skb, fr_len, &crc_eof, sizeof(crc_eof))) {
1397 kfree_skb(skb);
1398 continue;
1399 }
1400 fr_eof(fp) = crc_eof.fcoe_eof;
1401 fr_crc(fp) = crc_eof.fcoe_crc32;
1402 if (pskb_trim(skb, fr_len)) {
1403 kfree_skb(skb);
1404 continue;
1405 }
1406
1407 /*
1408 * We only check CRC if no offload is available and if it is
1409 * it's solicited data, in which case, the FCP layer would
1410 * check it during the copy.
1411 */
Yi Zou07c00ec2009-02-27 14:07:31 -08001412 if (lp->crc_offload && skb->ip_summed == CHECKSUM_UNNECESSARY)
Robert Love85b4aa42008-12-09 15:10:24 -08001413 fr_flags(fp) &= ~FCPHF_CRC_UNCHECKED;
1414 else
1415 fr_flags(fp) |= FCPHF_CRC_UNCHECKED;
1416
1417 fh = fc_frame_header_get(fp);
1418 if (fh->fh_r_ctl == FC_RCTL_DD_SOL_DATA &&
1419 fh->fh_type == FC_TYPE_FCP) {
Vasu Dev52ff8782009-07-29 17:05:10 -07001420 fc_exch_recv(lp, fp);
Robert Love85b4aa42008-12-09 15:10:24 -08001421 continue;
1422 }
1423 if (fr_flags(fp) & FCPHF_CRC_UNCHECKED) {
1424 if (le32_to_cpu(fr_crc(fp)) !=
1425 ~crc32(~0, skb->data, fr_len)) {
Robert Loved5488eb2009-06-10 15:30:59 -07001426 if (stats->InvalidCRCCount < 5)
Robert Love85b4aa42008-12-09 15:10:24 -08001427 printk(KERN_WARNING "fcoe: dropping "
1428 "frame with CRC error\n");
1429 stats->InvalidCRCCount++;
1430 stats->ErrorFrames++;
1431 fc_frame_free(fp);
1432 continue;
1433 }
1434 fr_flags(fp) &= ~FCPHF_CRC_UNCHECKED;
1435 }
Chris Leech3fe9a0b2009-08-25 13:59:46 -07001436 if (unlikely(port->fcoe->ctlr.flogi_oxid != FC_XID_UNKNOWN) &&
1437 fcoe_ctlr_recv_flogi(&port->fcoe->ctlr, fp, mac)) {
Joe Eykholt97c83892009-03-17 11:42:40 -07001438 fc_frame_free(fp);
1439 continue;
1440 }
Vasu Dev52ff8782009-07-29 17:05:10 -07001441 fc_exch_recv(lp, fp);
Robert Love85b4aa42008-12-09 15:10:24 -08001442 }
1443 return 0;
1444}
1445
1446/**
Chris Leechdd3fd722009-04-21 16:27:36 -07001447 * fcoe_check_wait_queue() - attempt to clear the transmit backlog
1448 * @lp: the fc_lport
Robert Love85b4aa42008-12-09 15:10:24 -08001449 *
1450 * This empties the wait_queue, dequeue the head of the wait_queue queue
1451 * and calls fcoe_start_io() for each packet, if all skb have been
Vasu Devc826a312009-02-27 10:56:27 -08001452 * transmitted, return qlen or -1 if a error occurs, then restore
Chris Leechdd3fd722009-04-21 16:27:36 -07001453 * wait_queue and try again later.
Robert Love85b4aa42008-12-09 15:10:24 -08001454 *
1455 * The wait_queue is used when the skb transmit fails. skb will go
Chris Leechdd3fd722009-04-21 16:27:36 -07001456 * in the wait_queue which will be emptied by the timer function or
Robert Love85b4aa42008-12-09 15:10:24 -08001457 * by the next skb transmit.
Robert Love34f42a02009-02-27 10:55:45 -08001458 */
Vasu Dev4bb6b512009-05-06 10:52:34 -07001459static void fcoe_check_wait_queue(struct fc_lport *lp, struct sk_buff *skb)
Robert Love85b4aa42008-12-09 15:10:24 -08001460{
Chris Leech014f5c32009-08-25 13:59:30 -07001461 struct fcoe_port *port = lport_priv(lp);
Vasu Dev4bb6b512009-05-06 10:52:34 -07001462 int rc;
Robert Love85b4aa42008-12-09 15:10:24 -08001463
Chris Leech014f5c32009-08-25 13:59:30 -07001464 spin_lock_bh(&port->fcoe_pending_queue.lock);
Vasu Dev4bb6b512009-05-06 10:52:34 -07001465
1466 if (skb)
Chris Leech014f5c32009-08-25 13:59:30 -07001467 __skb_queue_tail(&port->fcoe_pending_queue, skb);
Vasu Dev4bb6b512009-05-06 10:52:34 -07001468
Chris Leech014f5c32009-08-25 13:59:30 -07001469 if (port->fcoe_pending_queue_active)
Vasu Devc826a312009-02-27 10:56:27 -08001470 goto out;
Chris Leech014f5c32009-08-25 13:59:30 -07001471 port->fcoe_pending_queue_active = 1;
Chris Leech55c8baf2009-02-27 10:56:32 -08001472
Chris Leech014f5c32009-08-25 13:59:30 -07001473 while (port->fcoe_pending_queue.qlen) {
Chris Leech55c8baf2009-02-27 10:56:32 -08001474 /* keep qlen > 0 until fcoe_start_io succeeds */
Chris Leech014f5c32009-08-25 13:59:30 -07001475 port->fcoe_pending_queue.qlen++;
1476 skb = __skb_dequeue(&port->fcoe_pending_queue);
Chris Leech55c8baf2009-02-27 10:56:32 -08001477
Chris Leech014f5c32009-08-25 13:59:30 -07001478 spin_unlock_bh(&port->fcoe_pending_queue.lock);
Chris Leech55c8baf2009-02-27 10:56:32 -08001479 rc = fcoe_start_io(skb);
Chris Leech014f5c32009-08-25 13:59:30 -07001480 spin_lock_bh(&port->fcoe_pending_queue.lock);
Chris Leech55c8baf2009-02-27 10:56:32 -08001481
1482 if (rc) {
Chris Leech014f5c32009-08-25 13:59:30 -07001483 __skb_queue_head(&port->fcoe_pending_queue, skb);
Chris Leech55c8baf2009-02-27 10:56:32 -08001484 /* undo temporary increment above */
Chris Leech014f5c32009-08-25 13:59:30 -07001485 port->fcoe_pending_queue.qlen--;
Chris Leech55c8baf2009-02-27 10:56:32 -08001486 break;
Robert Love85b4aa42008-12-09 15:10:24 -08001487 }
Chris Leech55c8baf2009-02-27 10:56:32 -08001488 /* undo temporary increment above */
Chris Leech014f5c32009-08-25 13:59:30 -07001489 port->fcoe_pending_queue.qlen--;
Robert Love85b4aa42008-12-09 15:10:24 -08001490 }
Chris Leech55c8baf2009-02-27 10:56:32 -08001491
Chris Leech014f5c32009-08-25 13:59:30 -07001492 if (port->fcoe_pending_queue.qlen < FCOE_LOW_QUEUE_DEPTH)
Chris Leech55c8baf2009-02-27 10:56:32 -08001493 lp->qfull = 0;
Chris Leech014f5c32009-08-25 13:59:30 -07001494 if (port->fcoe_pending_queue.qlen && !timer_pending(&port->timer))
1495 mod_timer(&port->timer, jiffies + 2);
1496 port->fcoe_pending_queue_active = 0;
Vasu Devc826a312009-02-27 10:56:27 -08001497out:
Chris Leech014f5c32009-08-25 13:59:30 -07001498 if (port->fcoe_pending_queue.qlen > FCOE_MAX_QUEUE_DEPTH)
Vasu Dev4bb6b512009-05-06 10:52:34 -07001499 lp->qfull = 1;
Chris Leech014f5c32009-08-25 13:59:30 -07001500 spin_unlock_bh(&port->fcoe_pending_queue.lock);
Vasu Dev4bb6b512009-05-06 10:52:34 -07001501 return;
Robert Love85b4aa42008-12-09 15:10:24 -08001502}
1503
1504/**
Robert Love34f42a02009-02-27 10:55:45 -08001505 * fcoe_dev_setup() - setup link change notification interface
1506 */
Randy Dunlapb0d428a2009-04-27 21:49:31 -07001507static void fcoe_dev_setup(void)
Robert Love85b4aa42008-12-09 15:10:24 -08001508{
Robert Love85b4aa42008-12-09 15:10:24 -08001509 register_netdevice_notifier(&fcoe_notifier);
1510}
1511
1512/**
Randy Dunlapb0d428a2009-04-27 21:49:31 -07001513 * fcoe_dev_cleanup() - cleanup link change notification interface
Robert Love34f42a02009-02-27 10:55:45 -08001514 */
Robert Love85b4aa42008-12-09 15:10:24 -08001515static void fcoe_dev_cleanup(void)
1516{
1517 unregister_netdevice_notifier(&fcoe_notifier);
1518}
1519
1520/**
Robert Love34f42a02009-02-27 10:55:45 -08001521 * fcoe_device_notification() - netdev event notification callback
Robert Love85b4aa42008-12-09 15:10:24 -08001522 * @notifier: context of the notification
1523 * @event: type of event
1524 * @ptr: fixed array for output parsed ifname
1525 *
1526 * This function is called by the ethernet driver in case of link change event
1527 *
1528 * Returns: 0 for success
Robert Love34f42a02009-02-27 10:55:45 -08001529 */
Robert Love85b4aa42008-12-09 15:10:24 -08001530static int fcoe_device_notification(struct notifier_block *notifier,
1531 ulong event, void *ptr)
1532{
1533 struct fc_lport *lp = NULL;
Vasu Dev1d1b88d2009-07-29 17:05:45 -07001534 struct net_device *netdev = ptr;
Chris Leech014f5c32009-08-25 13:59:30 -07001535 struct fcoe_interface *fcoe;
Chris Leech2e70e242009-08-25 14:00:23 -07001536 struct fcoe_port *port;
Robert Love85b4aa42008-12-09 15:10:24 -08001537 struct fcoe_dev_stats *stats;
Joe Eykholt97c83892009-03-17 11:42:40 -07001538 u32 link_possible = 1;
Robert Love85b4aa42008-12-09 15:10:24 -08001539 u32 mfs;
1540 int rc = NOTIFY_OK;
1541
Chris Leech2e70e242009-08-25 14:00:23 -07001542 write_lock(&fcoe_hostlist_lock);
Chris Leech014f5c32009-08-25 13:59:30 -07001543 list_for_each_entry(fcoe, &fcoe_hostlist, list) {
Chris Leech25024982009-08-25 13:59:35 -07001544 if (fcoe->netdev == netdev) {
Chris Leech3fe9a0b2009-08-25 13:59:46 -07001545 lp = fcoe->ctlr.lp;
Robert Love85b4aa42008-12-09 15:10:24 -08001546 break;
1547 }
1548 }
Robert Love85b4aa42008-12-09 15:10:24 -08001549 if (lp == NULL) {
1550 rc = NOTIFY_DONE;
1551 goto out;
1552 }
1553
Robert Love85b4aa42008-12-09 15:10:24 -08001554 switch (event) {
1555 case NETDEV_DOWN:
1556 case NETDEV_GOING_DOWN:
Joe Eykholt97c83892009-03-17 11:42:40 -07001557 link_possible = 0;
Robert Love85b4aa42008-12-09 15:10:24 -08001558 break;
1559 case NETDEV_UP:
1560 case NETDEV_CHANGE:
Robert Love85b4aa42008-12-09 15:10:24 -08001561 break;
1562 case NETDEV_CHANGEMTU:
Vasu Dev1d1b88d2009-07-29 17:05:45 -07001563 mfs = netdev->mtu - (sizeof(struct fcoe_hdr) +
1564 sizeof(struct fcoe_crc_eof));
Robert Love85b4aa42008-12-09 15:10:24 -08001565 if (mfs >= FC_MIN_MAX_FRAME)
1566 fc_set_mfs(lp, mfs);
Robert Love85b4aa42008-12-09 15:10:24 -08001567 break;
1568 case NETDEV_REGISTER:
1569 break;
Chris Leech2e70e242009-08-25 14:00:23 -07001570 case NETDEV_UNREGISTER:
1571 list_del(&fcoe->list);
1572 port = lport_priv(fcoe->ctlr.lp);
1573 fcoe_interface_cleanup(fcoe);
1574 schedule_work(&port->destroy_work);
1575 goto out;
1576 break;
Robert Love85b4aa42008-12-09 15:10:24 -08001577 default:
Vasu Dev1d1b88d2009-07-29 17:05:45 -07001578 FCOE_NETDEV_DBG(netdev, "Unknown event %ld "
Robert Loved5488eb2009-06-10 15:30:59 -07001579 "from netdev netlink\n", event);
Robert Love85b4aa42008-12-09 15:10:24 -08001580 }
Joe Eykholt97c83892009-03-17 11:42:40 -07001581 if (link_possible && !fcoe_link_ok(lp))
Chris Leech3fe9a0b2009-08-25 13:59:46 -07001582 fcoe_ctlr_link_up(&fcoe->ctlr);
1583 else if (fcoe_ctlr_link_down(&fcoe->ctlr)) {
Joe Eykholt97c83892009-03-17 11:42:40 -07001584 stats = fc_lport_get_stats(lp);
1585 stats->LinkFailureCount++;
1586 fcoe_clean_pending_queue(lp);
Robert Love85b4aa42008-12-09 15:10:24 -08001587 }
1588out:
Chris Leech2e70e242009-08-25 14:00:23 -07001589 write_unlock(&fcoe_hostlist_lock);
Robert Love85b4aa42008-12-09 15:10:24 -08001590 return rc;
1591}
1592
1593/**
Robert Love34f42a02009-02-27 10:55:45 -08001594 * fcoe_if_to_netdev() - parse a name buffer to get netdev
Robert Love85b4aa42008-12-09 15:10:24 -08001595 * @buffer: incoming buffer to be copied
1596 *
Chris Leechdd3fd722009-04-21 16:27:36 -07001597 * Returns: NULL or ptr to net_device
Robert Love34f42a02009-02-27 10:55:45 -08001598 */
Robert Love85b4aa42008-12-09 15:10:24 -08001599static struct net_device *fcoe_if_to_netdev(const char *buffer)
1600{
1601 char *cp;
1602 char ifname[IFNAMSIZ + 2];
1603
1604 if (buffer) {
1605 strlcpy(ifname, buffer, IFNAMSIZ);
1606 cp = ifname + strlen(ifname);
1607 while (--cp >= ifname && *cp == '\n')
1608 *cp = '\0';
1609 return dev_get_by_name(&init_net, ifname);
1610 }
1611 return NULL;
1612}
1613
1614/**
Robert Love34f42a02009-02-27 10:55:45 -08001615 * fcoe_destroy() - handles the destroy from sysfs
Chris Leechdd3fd722009-04-21 16:27:36 -07001616 * @buffer: expected to be an eth if name
Robert Love85b4aa42008-12-09 15:10:24 -08001617 * @kp: associated kernel param
1618 *
1619 * Returns: 0 for success
Robert Love34f42a02009-02-27 10:55:45 -08001620 */
Robert Love85b4aa42008-12-09 15:10:24 -08001621static int fcoe_destroy(const char *buffer, struct kernel_param *kp)
1622{
Chris Leech030f4e02009-08-25 14:00:02 -07001623 struct fcoe_interface *fcoe;
Chris Leech2e70e242009-08-25 14:00:23 -07001624 struct net_device *netdev;
Chris Leechaf7f85d2009-08-25 13:59:24 -07001625 int rc;
Robert Love85b4aa42008-12-09 15:10:24 -08001626
Chris Leechdfc1d0f2009-08-25 14:00:13 -07001627 mutex_lock(&fcoe_config_mutex);
1628#ifdef CONFIG_FCOE_MODULE
1629 /*
1630 * Make sure the module has been initialized, and is not about to be
1631 * removed. Module paramter sysfs files are writable before the
1632 * module_init function is called and after module_exit.
1633 */
1634 if (THIS_MODULE->state != MODULE_STATE_LIVE) {
1635 rc = -ENODEV;
1636 goto out_nodev;
1637 }
1638#endif
1639
Robert Love85b4aa42008-12-09 15:10:24 -08001640 netdev = fcoe_if_to_netdev(buffer);
1641 if (!netdev) {
1642 rc = -ENODEV;
1643 goto out_nodev;
1644 }
Chris Leech2e70e242009-08-25 14:00:23 -07001645
1646 write_lock(&fcoe_hostlist_lock);
1647 fcoe = fcoe_hostlist_lookup_port(netdev);
1648 if (!fcoe) {
1649 write_unlock(&fcoe_hostlist_lock);
Robert Love85b4aa42008-12-09 15:10:24 -08001650 rc = -ENODEV;
1651 goto out_putdev;
1652 }
Chris Leech2e70e242009-08-25 14:00:23 -07001653 list_del(&fcoe->list);
1654 write_unlock(&fcoe_hostlist_lock);
1655 rtnl_lock();
1656 fcoe_interface_cleanup(fcoe);
1657 rtnl_unlock();
1658 fcoe_if_destroy(fcoe->ctlr.lp);
Robert Love85b4aa42008-12-09 15:10:24 -08001659out_putdev:
1660 dev_put(netdev);
1661out_nodev:
Chris Leechdfc1d0f2009-08-25 14:00:13 -07001662 mutex_unlock(&fcoe_config_mutex);
Robert Love85b4aa42008-12-09 15:10:24 -08001663 return rc;
1664}
1665
Chris Leech2e70e242009-08-25 14:00:23 -07001666static void fcoe_destroy_work(struct work_struct *work)
1667{
1668 struct fcoe_port *port;
1669
1670 port = container_of(work, struct fcoe_port, destroy_work);
1671 mutex_lock(&fcoe_config_mutex);
1672 fcoe_if_destroy(port->lport);
1673 mutex_unlock(&fcoe_config_mutex);
1674}
1675
Robert Love85b4aa42008-12-09 15:10:24 -08001676/**
Robert Love34f42a02009-02-27 10:55:45 -08001677 * fcoe_create() - Handles the create call from sysfs
Chris Leechdd3fd722009-04-21 16:27:36 -07001678 * @buffer: expected to be an eth if name
Robert Love85b4aa42008-12-09 15:10:24 -08001679 * @kp: associated kernel param
1680 *
1681 * Returns: 0 for success
Robert Love34f42a02009-02-27 10:55:45 -08001682 */
Robert Love85b4aa42008-12-09 15:10:24 -08001683static int fcoe_create(const char *buffer, struct kernel_param *kp)
1684{
1685 int rc;
Chris Leech030f4e02009-08-25 14:00:02 -07001686 struct fcoe_interface *fcoe;
Chris Leechaf7f85d2009-08-25 13:59:24 -07001687 struct fc_lport *lport;
Robert Love85b4aa42008-12-09 15:10:24 -08001688 struct net_device *netdev;
1689
Chris Leechdfc1d0f2009-08-25 14:00:13 -07001690 mutex_lock(&fcoe_config_mutex);
1691#ifdef CONFIG_FCOE_MODULE
1692 /*
1693 * Make sure the module has been initialized, and is not about to be
1694 * removed. Module paramter sysfs files are writable before the
1695 * module_init function is called and after module_exit.
1696 */
1697 if (THIS_MODULE->state != MODULE_STATE_LIVE) {
1698 rc = -ENODEV;
1699 goto out_nodev;
1700 }
1701#endif
1702
Chris Leech2e70e242009-08-25 14:00:23 -07001703 rtnl_lock();
Robert Love85b4aa42008-12-09 15:10:24 -08001704 netdev = fcoe_if_to_netdev(buffer);
1705 if (!netdev) {
1706 rc = -ENODEV;
1707 goto out_nodev;
1708 }
Chris Leech2e70e242009-08-25 14:00:23 -07001709
Robert Love85b4aa42008-12-09 15:10:24 -08001710 /* look for existing lport */
1711 if (fcoe_hostlist_lookup(netdev)) {
1712 rc = -EEXIST;
1713 goto out_putdev;
1714 }
Robert Love85b4aa42008-12-09 15:10:24 -08001715
Chris Leech030f4e02009-08-25 14:00:02 -07001716 fcoe = fcoe_interface_create(netdev);
1717 if (!fcoe) {
1718 rc = -ENOMEM;
1719 goto out_putdev;
1720 }
1721
1722 lport = fcoe_if_create(fcoe, &netdev->dev);
Chris Leechaf7f85d2009-08-25 13:59:24 -07001723 if (IS_ERR(lport)) {
Robert Loved5488eb2009-06-10 15:30:59 -07001724 printk(KERN_ERR "fcoe: Failed to create interface (%s)\n",
Robert Love85b4aa42008-12-09 15:10:24 -08001725 netdev->name);
Robert Love85b4aa42008-12-09 15:10:24 -08001726 rc = -EIO;
Chris Leech2e70e242009-08-25 14:00:23 -07001727 fcoe_interface_cleanup(fcoe);
Chris Leech030f4e02009-08-25 14:00:02 -07001728 goto out_free;
Robert Love85b4aa42008-12-09 15:10:24 -08001729 }
Chris Leech030f4e02009-08-25 14:00:02 -07001730
Chris Leech54b649f2009-08-25 14:00:07 -07001731 /* Make this the "master" N_Port */
1732 fcoe->ctlr.lp = lport;
Chris Leech030f4e02009-08-25 14:00:02 -07001733
Chris Leechc863df32009-08-25 14:00:18 -07001734 /* add to lports list */
1735 fcoe_hostlist_add(lport);
1736
Chris Leech54b649f2009-08-25 14:00:07 -07001737 /* start FIP Discovery and FLOGI */
1738 lport->boot_time = jiffies;
1739 fc_fabric_login(lport);
1740 if (!fcoe_link_ok(lport))
1741 fcoe_ctlr_link_up(&fcoe->ctlr);
1742
1743 rc = 0;
Chris Leech030f4e02009-08-25 14:00:02 -07001744out_free:
Chris Leech54b649f2009-08-25 14:00:07 -07001745 /*
1746 * Release from init in fcoe_interface_create(), on success lport
1747 * should be holding a reference taken in fcoe_if_create().
1748 */
Chris Leech030f4e02009-08-25 14:00:02 -07001749 fcoe_interface_put(fcoe);
Robert Love85b4aa42008-12-09 15:10:24 -08001750out_putdev:
1751 dev_put(netdev);
1752out_nodev:
Chris Leech2e70e242009-08-25 14:00:23 -07001753 rtnl_unlock();
Chris Leechdfc1d0f2009-08-25 14:00:13 -07001754 mutex_unlock(&fcoe_config_mutex);
Robert Love85b4aa42008-12-09 15:10:24 -08001755 return rc;
1756}
1757
1758module_param_call(create, fcoe_create, NULL, NULL, S_IWUSR);
1759__MODULE_PARM_TYPE(create, "string");
Chris Leech014f5c32009-08-25 13:59:30 -07001760MODULE_PARM_DESC(create, "Create fcoe fcoe using net device passed in.");
Robert Love85b4aa42008-12-09 15:10:24 -08001761module_param_call(destroy, fcoe_destroy, NULL, NULL, S_IWUSR);
1762__MODULE_PARM_TYPE(destroy, "string");
Chris Leech014f5c32009-08-25 13:59:30 -07001763MODULE_PARM_DESC(destroy, "Destroy fcoe fcoe");
Robert Love85b4aa42008-12-09 15:10:24 -08001764
Robert Love34f42a02009-02-27 10:55:45 -08001765/**
1766 * fcoe_link_ok() - Check if link is ok for the fc_lport
Robert Love85b4aa42008-12-09 15:10:24 -08001767 * @lp: ptr to the fc_lport
1768 *
1769 * Any permanently-disqualifying conditions have been previously checked.
1770 * This also updates the speed setting, which may change with link for 100/1000.
1771 *
1772 * This function should probably be checking for PAUSE support at some point
1773 * in the future. Currently Per-priority-pause is not determinable using
1774 * ethtool, so we shouldn't be restrictive until that problem is resolved.
1775 *
1776 * Returns: 0 if link is OK for use by FCoE.
1777 *
1778 */
1779int fcoe_link_ok(struct fc_lport *lp)
1780{
Chris Leech014f5c32009-08-25 13:59:30 -07001781 struct fcoe_port *port = lport_priv(lp);
Chris Leech25024982009-08-25 13:59:35 -07001782 struct net_device *dev = port->fcoe->netdev;
Robert Love85b4aa42008-12-09 15:10:24 -08001783 struct ethtool_cmd ecmd = { ETHTOOL_GSET };
Robert Love85b4aa42008-12-09 15:10:24 -08001784
Yi Zou2f718d62009-07-29 17:04:01 -07001785 if ((dev->flags & IFF_UP) && netif_carrier_ok(dev) &&
1786 (!dev_ethtool_get_settings(dev, &ecmd))) {
1787 lp->link_supported_speeds &=
1788 ~(FC_PORTSPEED_1GBIT | FC_PORTSPEED_10GBIT);
1789 if (ecmd.supported & (SUPPORTED_1000baseT_Half |
1790 SUPPORTED_1000baseT_Full))
1791 lp->link_supported_speeds |= FC_PORTSPEED_1GBIT;
1792 if (ecmd.supported & SUPPORTED_10000baseT_Full)
1793 lp->link_supported_speeds |=
1794 FC_PORTSPEED_10GBIT;
1795 if (ecmd.speed == SPEED_1000)
1796 lp->link_speed = FC_PORTSPEED_1GBIT;
1797 if (ecmd.speed == SPEED_10000)
1798 lp->link_speed = FC_PORTSPEED_10GBIT;
Robert Love85b4aa42008-12-09 15:10:24 -08001799
Yi Zou2f718d62009-07-29 17:04:01 -07001800 return 0;
1801 }
1802 return -1;
Robert Love85b4aa42008-12-09 15:10:24 -08001803}
Robert Love85b4aa42008-12-09 15:10:24 -08001804
Robert Love34f42a02009-02-27 10:55:45 -08001805/**
1806 * fcoe_percpu_clean() - Clear the pending skbs for an lport
Robert Love85b4aa42008-12-09 15:10:24 -08001807 * @lp: the fc_lport
1808 */
1809void fcoe_percpu_clean(struct fc_lport *lp)
1810{
Robert Love85b4aa42008-12-09 15:10:24 -08001811 struct fcoe_percpu_s *pp;
1812 struct fcoe_rcv_info *fr;
1813 struct sk_buff_head *list;
1814 struct sk_buff *skb, *next;
1815 struct sk_buff *head;
Robert Love5e5e92d2009-03-17 11:41:35 -07001816 unsigned int cpu;
Robert Love85b4aa42008-12-09 15:10:24 -08001817
Robert Love5e5e92d2009-03-17 11:41:35 -07001818 for_each_possible_cpu(cpu) {
1819 pp = &per_cpu(fcoe_percpu, cpu);
1820 spin_lock_bh(&pp->fcoe_rx_list.lock);
1821 list = &pp->fcoe_rx_list;
1822 head = list->next;
1823 for (skb = head; skb != (struct sk_buff *)list;
1824 skb = next) {
1825 next = skb->next;
1826 fr = fcoe_dev_from_skb(skb);
1827 if (fr->fr_dev == lp) {
1828 __skb_unlink(skb, list);
1829 kfree_skb(skb);
Robert Love85b4aa42008-12-09 15:10:24 -08001830 }
Robert Love85b4aa42008-12-09 15:10:24 -08001831 }
Robert Love5e5e92d2009-03-17 11:41:35 -07001832 spin_unlock_bh(&pp->fcoe_rx_list.lock);
Robert Love85b4aa42008-12-09 15:10:24 -08001833 }
1834}
Robert Love85b4aa42008-12-09 15:10:24 -08001835
1836/**
Robert Love34f42a02009-02-27 10:55:45 -08001837 * fcoe_clean_pending_queue() - Dequeue a skb and free it
Robert Love85b4aa42008-12-09 15:10:24 -08001838 * @lp: the corresponding fc_lport
1839 *
1840 * Returns: none
Robert Love34f42a02009-02-27 10:55:45 -08001841 */
Robert Love85b4aa42008-12-09 15:10:24 -08001842void fcoe_clean_pending_queue(struct fc_lport *lp)
1843{
Chris Leech014f5c32009-08-25 13:59:30 -07001844 struct fcoe_port *port = lport_priv(lp);
Robert Love85b4aa42008-12-09 15:10:24 -08001845 struct sk_buff *skb;
1846
Chris Leech014f5c32009-08-25 13:59:30 -07001847 spin_lock_bh(&port->fcoe_pending_queue.lock);
1848 while ((skb = __skb_dequeue(&port->fcoe_pending_queue)) != NULL) {
1849 spin_unlock_bh(&port->fcoe_pending_queue.lock);
Robert Love85b4aa42008-12-09 15:10:24 -08001850 kfree_skb(skb);
Chris Leech014f5c32009-08-25 13:59:30 -07001851 spin_lock_bh(&port->fcoe_pending_queue.lock);
Robert Love85b4aa42008-12-09 15:10:24 -08001852 }
Chris Leech014f5c32009-08-25 13:59:30 -07001853 spin_unlock_bh(&port->fcoe_pending_queue.lock);
Robert Love85b4aa42008-12-09 15:10:24 -08001854}
Robert Love85b4aa42008-12-09 15:10:24 -08001855
1856/**
Robert Love34f42a02009-02-27 10:55:45 -08001857 * fcoe_reset() - Resets the fcoe
Robert Love85b4aa42008-12-09 15:10:24 -08001858 * @shost: shost the reset is from
1859 *
1860 * Returns: always 0
1861 */
1862int fcoe_reset(struct Scsi_Host *shost)
1863{
1864 struct fc_lport *lport = shost_priv(shost);
1865 fc_lport_reset(lport);
1866 return 0;
1867}
Robert Love85b4aa42008-12-09 15:10:24 -08001868
Robert Love34f42a02009-02-27 10:55:45 -08001869/**
Chris Leech014f5c32009-08-25 13:59:30 -07001870 * fcoe_hostlist_lookup_port() - find the corresponding lport by a given device
Chris Leechdd3fd722009-04-21 16:27:36 -07001871 * @dev: this is currently ptr to net_device
Robert Love85b4aa42008-12-09 15:10:24 -08001872 *
Vasu Deve8af4d42009-07-29 17:05:15 -07001873 * Called with fcoe_hostlist_lock held.
1874 *
Chris Leech014f5c32009-08-25 13:59:30 -07001875 * Returns: NULL or the located fcoe_port
Robert Love85b4aa42008-12-09 15:10:24 -08001876 */
Chris Leech014f5c32009-08-25 13:59:30 -07001877static struct fcoe_interface *
1878fcoe_hostlist_lookup_port(const struct net_device *dev)
Robert Love85b4aa42008-12-09 15:10:24 -08001879{
Chris Leech014f5c32009-08-25 13:59:30 -07001880 struct fcoe_interface *fcoe;
Robert Love85b4aa42008-12-09 15:10:24 -08001881
Chris Leech014f5c32009-08-25 13:59:30 -07001882 list_for_each_entry(fcoe, &fcoe_hostlist, list) {
Chris Leech25024982009-08-25 13:59:35 -07001883 if (fcoe->netdev == dev)
Chris Leech014f5c32009-08-25 13:59:30 -07001884 return fcoe;
Robert Love85b4aa42008-12-09 15:10:24 -08001885 }
Robert Love85b4aa42008-12-09 15:10:24 -08001886 return NULL;
1887}
1888
Robert Love34f42a02009-02-27 10:55:45 -08001889/**
1890 * fcoe_hostlist_lookup() - Find the corresponding lport by netdev
Robert Love85b4aa42008-12-09 15:10:24 -08001891 * @netdev: ptr to net_device
1892 *
1893 * Returns: 0 for success
1894 */
1895struct fc_lport *fcoe_hostlist_lookup(const struct net_device *netdev)
1896{
Chris Leech014f5c32009-08-25 13:59:30 -07001897 struct fcoe_interface *fcoe;
Robert Love85b4aa42008-12-09 15:10:24 -08001898
Vasu Deve8af4d42009-07-29 17:05:15 -07001899 read_lock(&fcoe_hostlist_lock);
Chris Leech014f5c32009-08-25 13:59:30 -07001900 fcoe = fcoe_hostlist_lookup_port(netdev);
Vasu Deve8af4d42009-07-29 17:05:15 -07001901 read_unlock(&fcoe_hostlist_lock);
Robert Love85b4aa42008-12-09 15:10:24 -08001902
Chris Leech3fe9a0b2009-08-25 13:59:46 -07001903 return (fcoe) ? fcoe->ctlr.lp : NULL;
Robert Love85b4aa42008-12-09 15:10:24 -08001904}
Robert Love85b4aa42008-12-09 15:10:24 -08001905
Robert Love34f42a02009-02-27 10:55:45 -08001906/**
1907 * fcoe_hostlist_add() - Add a lport to lports list
Chris Leechdd3fd722009-04-21 16:27:36 -07001908 * @lp: ptr to the fc_lport to be added
Robert Love85b4aa42008-12-09 15:10:24 -08001909 *
1910 * Returns: 0 for success
1911 */
Chris Leech014f5c32009-08-25 13:59:30 -07001912int fcoe_hostlist_add(const struct fc_lport *lport)
Robert Love85b4aa42008-12-09 15:10:24 -08001913{
Chris Leech014f5c32009-08-25 13:59:30 -07001914 struct fcoe_interface *fcoe;
1915 struct fcoe_port *port;
Robert Love85b4aa42008-12-09 15:10:24 -08001916
Chris Leechc863df32009-08-25 14:00:18 -07001917 write_lock_bh(&fcoe_hostlist_lock);
Chris Leech014f5c32009-08-25 13:59:30 -07001918 fcoe = fcoe_hostlist_lookup_port(fcoe_netdev(lport));
1919 if (!fcoe) {
1920 port = lport_priv(lport);
1921 fcoe = port->fcoe;
1922 list_add_tail(&fcoe->list, &fcoe_hostlist);
Robert Love85b4aa42008-12-09 15:10:24 -08001923 }
Chris Leechc863df32009-08-25 14:00:18 -07001924 write_unlock_bh(&fcoe_hostlist_lock);
Robert Love85b4aa42008-12-09 15:10:24 -08001925 return 0;
1926}
Robert Love85b4aa42008-12-09 15:10:24 -08001927
Robert Love34f42a02009-02-27 10:55:45 -08001928/**
Robert Love34f42a02009-02-27 10:55:45 -08001929 * fcoe_init() - fcoe module loading initialization
Robert Love85b4aa42008-12-09 15:10:24 -08001930 *
Robert Love85b4aa42008-12-09 15:10:24 -08001931 * Returns 0 on success, negative on failure
Robert Love34f42a02009-02-27 10:55:45 -08001932 */
Robert Love85b4aa42008-12-09 15:10:24 -08001933static int __init fcoe_init(void)
1934{
Robert Love38eccab2009-03-17 11:41:30 -07001935 unsigned int cpu;
Robert Love8976f422009-03-17 11:41:46 -07001936 int rc = 0;
Robert Love85b4aa42008-12-09 15:10:24 -08001937 struct fcoe_percpu_s *p;
1938
Chris Leechdfc1d0f2009-08-25 14:00:13 -07001939 mutex_lock(&fcoe_config_mutex);
1940
Robert Love38eccab2009-03-17 11:41:30 -07001941 for_each_possible_cpu(cpu) {
Robert Love5e5e92d2009-03-17 11:41:35 -07001942 p = &per_cpu(fcoe_percpu, cpu);
Robert Love38eccab2009-03-17 11:41:30 -07001943 skb_queue_head_init(&p->fcoe_rx_list);
1944 }
1945
Robert Love8976f422009-03-17 11:41:46 -07001946 for_each_online_cpu(cpu)
1947 fcoe_percpu_thread_create(cpu);
Robert Love85b4aa42008-12-09 15:10:24 -08001948
Robert Love8976f422009-03-17 11:41:46 -07001949 /* Initialize per CPU interrupt thread */
1950 rc = register_hotcpu_notifier(&fcoe_cpu_notifier);
1951 if (rc)
1952 goto out_free;
Robert Love85b4aa42008-12-09 15:10:24 -08001953
Robert Love8976f422009-03-17 11:41:46 -07001954 /* Setup link change notification */
Robert Love85b4aa42008-12-09 15:10:24 -08001955 fcoe_dev_setup();
1956
Chris Leech5892c322009-08-25 13:59:14 -07001957 rc = fcoe_if_init();
1958 if (rc)
1959 goto out_free;
Robert Love85b4aa42008-12-09 15:10:24 -08001960
Chris Leechdfc1d0f2009-08-25 14:00:13 -07001961 mutex_unlock(&fcoe_config_mutex);
Robert Love85b4aa42008-12-09 15:10:24 -08001962 return 0;
Robert Love8976f422009-03-17 11:41:46 -07001963
1964out_free:
1965 for_each_online_cpu(cpu) {
1966 fcoe_percpu_thread_destroy(cpu);
1967 }
Chris Leechdfc1d0f2009-08-25 14:00:13 -07001968 mutex_unlock(&fcoe_config_mutex);
Robert Love8976f422009-03-17 11:41:46 -07001969 return rc;
Robert Love85b4aa42008-12-09 15:10:24 -08001970}
1971module_init(fcoe_init);
1972
1973/**
Robert Love34f42a02009-02-27 10:55:45 -08001974 * fcoe_exit() - fcoe module unloading cleanup
Robert Love85b4aa42008-12-09 15:10:24 -08001975 *
1976 * Returns 0 on success, negative on failure
Robert Love34f42a02009-02-27 10:55:45 -08001977 */
Robert Love85b4aa42008-12-09 15:10:24 -08001978static void __exit fcoe_exit(void)
1979{
Robert Love5e5e92d2009-03-17 11:41:35 -07001980 unsigned int cpu;
Chris Leech014f5c32009-08-25 13:59:30 -07001981 struct fcoe_interface *fcoe, *tmp;
Chris Leechc863df32009-08-25 14:00:18 -07001982 LIST_HEAD(local_list);
Chris Leech2e70e242009-08-25 14:00:23 -07001983 struct fcoe_port *port;
Robert Love85b4aa42008-12-09 15:10:24 -08001984
Chris Leechdfc1d0f2009-08-25 14:00:13 -07001985 mutex_lock(&fcoe_config_mutex);
1986
Robert Love85b4aa42008-12-09 15:10:24 -08001987 fcoe_dev_cleanup();
1988
Vasu Dev5919a592009-03-27 09:03:29 -07001989 /* releases the associated fcoe hosts */
Chris Leechc863df32009-08-25 14:00:18 -07001990 write_lock_bh(&fcoe_hostlist_lock);
1991 list_splice_init(&fcoe_hostlist, &local_list);
1992 write_unlock_bh(&fcoe_hostlist_lock);
1993
1994 list_for_each_entry_safe(fcoe, tmp, &local_list, list) {
1995 list_del(&fcoe->list);
Chris Leech2e70e242009-08-25 14:00:23 -07001996 port = lport_priv(fcoe->ctlr.lp);
1997 rtnl_lock();
1998 fcoe_interface_cleanup(fcoe);
1999 rtnl_unlock();
2000 schedule_work(&port->destroy_work);
Chris Leechc863df32009-08-25 14:00:18 -07002001 }
Robert Love85b4aa42008-12-09 15:10:24 -08002002
Robert Love8976f422009-03-17 11:41:46 -07002003 unregister_hotcpu_notifier(&fcoe_cpu_notifier);
2004
Chris Leech014f5c32009-08-25 13:59:30 -07002005 for_each_online_cpu(cpu)
Robert Love8976f422009-03-17 11:41:46 -07002006 fcoe_percpu_thread_destroy(cpu);
Robert Love85b4aa42008-12-09 15:10:24 -08002007
Chris Leechdfc1d0f2009-08-25 14:00:13 -07002008 mutex_unlock(&fcoe_config_mutex);
Chris Leech2e70e242009-08-25 14:00:23 -07002009
2010 /* flush any asyncronous interface destroys,
2011 * this should happen after the netdev notifier is unregistered */
2012 flush_scheduled_work();
2013
2014 /* detach from scsi transport
2015 * must happen after all destroys are done, therefor after the flush */
2016 fcoe_if_exit();
Robert Love85b4aa42008-12-09 15:10:24 -08002017}
2018module_exit(fcoe_exit);