blob: ac481ad112adfb6fe82359e8e2934f96196f851d [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 */
Chris Leech090eb6c2009-08-25 14:00:28 -070061/* must only by accessed under the RTNL mutex */
Robert Love85b4aa42008-12-09 15:10:24 -080062LIST_HEAD(fcoe_hostlist);
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 *
530 * Returns : 0 on success
531 */
532static inline int fcoe_em_config(struct fc_lport *lp)
533{
Chris Leech014f5c32009-08-25 13:59:30 -0700534 struct fcoe_port *port = lport_priv(lp);
Chris Leech25024982009-08-25 13:59:35 -0700535 struct fcoe_interface *fcoe = port->fcoe;
536 struct fcoe_interface *oldfcoe = NULL;
Vasu Dev1d1b88d2009-07-29 17:05:45 -0700537 struct net_device *old_real_dev, *cur_real_dev;
Vasu Devd7179682009-07-29 17:05:21 -0700538 u16 min_xid = FCOE_MIN_XID;
539 u16 max_xid = FCOE_MAX_XID;
540
541 /*
542 * Check if need to allocate an em instance for
543 * offload exchange ids to be shared across all VN_PORTs/lport.
544 */
545 if (!lp->lro_enabled || !lp->lro_xid || (lp->lro_xid >= max_xid)) {
546 lp->lro_xid = 0;
547 goto skip_oem;
548 }
549
550 /*
551 * Reuse existing offload em instance in case
Vasu Dev1d1b88d2009-07-29 17:05:45 -0700552 * it is already allocated on real eth device
Vasu Devd7179682009-07-29 17:05:21 -0700553 */
Chris Leech25024982009-08-25 13:59:35 -0700554 if (fcoe->netdev->priv_flags & IFF_802_1Q_VLAN)
555 cur_real_dev = vlan_dev_real_dev(fcoe->netdev);
Vasu Dev1d1b88d2009-07-29 17:05:45 -0700556 else
Chris Leech25024982009-08-25 13:59:35 -0700557 cur_real_dev = fcoe->netdev;
Vasu Dev1d1b88d2009-07-29 17:05:45 -0700558
Chris Leech25024982009-08-25 13:59:35 -0700559 list_for_each_entry(oldfcoe, &fcoe_hostlist, list) {
Chris Leech25024982009-08-25 13:59:35 -0700560 if (oldfcoe->netdev->priv_flags & IFF_802_1Q_VLAN)
561 old_real_dev = vlan_dev_real_dev(oldfcoe->netdev);
Vasu Dev1d1b88d2009-07-29 17:05:45 -0700562 else
Chris Leech25024982009-08-25 13:59:35 -0700563 old_real_dev = oldfcoe->netdev;
Vasu Dev1d1b88d2009-07-29 17:05:45 -0700564
565 if (cur_real_dev == old_real_dev) {
Chris Leech991cbb62009-08-25 13:59:51 -0700566 fcoe->oem = oldfcoe->oem;
Vasu Devd7179682009-07-29 17:05:21 -0700567 break;
568 }
569 }
570
Chris Leech991cbb62009-08-25 13:59:51 -0700571 if (fcoe->oem) {
572 if (!fc_exch_mgr_add(lp, fcoe->oem, fcoe_oem_match)) {
Vasu Devd7179682009-07-29 17:05:21 -0700573 printk(KERN_ERR "fcoe_em_config: failed to add "
574 "offload em:%p on interface:%s\n",
Chris Leech991cbb62009-08-25 13:59:51 -0700575 fcoe->oem, fcoe->netdev->name);
Vasu Devd7179682009-07-29 17:05:21 -0700576 return -ENOMEM;
577 }
578 } else {
Chris Leech991cbb62009-08-25 13:59:51 -0700579 fcoe->oem = fc_exch_mgr_alloc(lp, FC_CLASS_3,
Vasu Devd7179682009-07-29 17:05:21 -0700580 FCOE_MIN_XID, lp->lro_xid,
581 fcoe_oem_match);
Chris Leech991cbb62009-08-25 13:59:51 -0700582 if (!fcoe->oem) {
Vasu Devd7179682009-07-29 17:05:21 -0700583 printk(KERN_ERR "fcoe_em_config: failed to allocate "
584 "em for offload exches on interface:%s\n",
Chris Leech25024982009-08-25 13:59:35 -0700585 fcoe->netdev->name);
Vasu Devd7179682009-07-29 17:05:21 -0700586 return -ENOMEM;
587 }
588 }
589
590 /*
591 * Exclude offload EM xid range from next EM xid range.
592 */
593 min_xid += lp->lro_xid + 1;
594
595skip_oem:
596 if (!fc_exch_mgr_alloc(lp, FC_CLASS_3, min_xid, max_xid, NULL)) {
597 printk(KERN_ERR "fcoe_em_config: failed to "
Chris Leech25024982009-08-25 13:59:35 -0700598 "allocate em on interface %s\n", fcoe->netdev->name);
Vasu Dev7f349142009-03-27 09:06:31 -0700599 return -ENOMEM;
Vasu Devd7179682009-07-29 17:05:21 -0700600 }
Vasu Dev7f349142009-03-27 09:06:31 -0700601
602 return 0;
603}
604
605/**
606 * fcoe_if_destroy() - FCoE software HBA tear-down function
Chris Leechaf7f85d2009-08-25 13:59:24 -0700607 * @lport: fc_lport to destroy
Vasu Dev7f349142009-03-27 09:06:31 -0700608 */
Chris Leechaf7f85d2009-08-25 13:59:24 -0700609static void fcoe_if_destroy(struct fc_lport *lport)
Vasu Dev7f349142009-03-27 09:06:31 -0700610{
Chris Leech014f5c32009-08-25 13:59:30 -0700611 struct fcoe_port *port = lport_priv(lport);
612 struct fcoe_interface *fcoe = port->fcoe;
Chris Leech25024982009-08-25 13:59:35 -0700613 struct net_device *netdev = fcoe->netdev;
Vasu Dev7f349142009-03-27 09:06:31 -0700614
Robert Loved5488eb2009-06-10 15:30:59 -0700615 FCOE_NETDEV_DBG(netdev, "Destroying interface\n");
Vasu Dev7f349142009-03-27 09:06:31 -0700616
Vasu Dev7f349142009-03-27 09:06:31 -0700617 /* Logout of the fabric */
Chris Leechaf7f85d2009-08-25 13:59:24 -0700618 fc_fabric_logoff(lport);
Vasu Dev7f349142009-03-27 09:06:31 -0700619
Vasu Dev7f349142009-03-27 09:06:31 -0700620 /* Cleanup the fc_lport */
Chris Leechaf7f85d2009-08-25 13:59:24 -0700621 fc_lport_destroy(lport);
622 fc_fcp_destroy(lport);
Vasu Dev7f349142009-03-27 09:06:31 -0700623
Chris Leech54b649f2009-08-25 14:00:07 -0700624 /* Stop the transmit retry timer */
625 del_timer_sync(&port->timer);
626
627 /* Free existing transmit skbs */
628 fcoe_clean_pending_queue(lport);
629
630 /* receives may not be stopped until after this */
631 fcoe_interface_put(fcoe);
632
633 /* Free queued packets for the per-CPU receive threads */
634 fcoe_percpu_clean(lport);
635
Vasu Dev7f349142009-03-27 09:06:31 -0700636 /* Detach from the scsi-ml */
Chris Leechaf7f85d2009-08-25 13:59:24 -0700637 fc_remove_host(lport->host);
638 scsi_remove_host(lport->host);
Vasu Dev7f349142009-03-27 09:06:31 -0700639
640 /* There are no more rports or I/O, free the EM */
Chris Leechaf7f85d2009-08-25 13:59:24 -0700641 fc_exch_mgr_free(lport);
Vasu Dev7f349142009-03-27 09:06:31 -0700642
Vasu Dev7f349142009-03-27 09:06:31 -0700643 /* Free memory used by statistical counters */
Chris Leechaf7f85d2009-08-25 13:59:24 -0700644 fc_lport_free_stats(lport);
Vasu Dev7f349142009-03-27 09:06:31 -0700645
Chris Leech2e70e242009-08-25 14:00:23 -0700646 /* Release the Scsi_Host */
Chris Leechaf7f85d2009-08-25 13:59:24 -0700647 scsi_host_put(lport->host);
Vasu Dev7f349142009-03-27 09:06:31 -0700648}
649
650/*
651 * fcoe_ddp_setup - calls LLD's ddp_setup through net_device
652 * @lp: the corresponding fc_lport
653 * @xid: the exchange id for this ddp transfer
654 * @sgl: the scatterlist describing this transfer
655 * @sgc: number of sg items
656 *
657 * Returns : 0 no ddp
658 */
659static int fcoe_ddp_setup(struct fc_lport *lp, u16 xid,
660 struct scatterlist *sgl, unsigned int sgc)
661{
662 struct net_device *n = fcoe_netdev(lp);
663
664 if (n->netdev_ops && n->netdev_ops->ndo_fcoe_ddp_setup)
665 return n->netdev_ops->ndo_fcoe_ddp_setup(n, xid, sgl, sgc);
666
667 return 0;
668}
669
670/*
671 * fcoe_ddp_done - calls LLD's ddp_done through net_device
672 * @lp: the corresponding fc_lport
673 * @xid: the exchange id for this ddp transfer
674 *
675 * Returns : the length of data that have been completed by ddp
676 */
677static int fcoe_ddp_done(struct fc_lport *lp, u16 xid)
678{
679 struct net_device *n = fcoe_netdev(lp);
680
681 if (n->netdev_ops && n->netdev_ops->ndo_fcoe_ddp_done)
682 return n->netdev_ops->ndo_fcoe_ddp_done(n, xid);
683 return 0;
684}
685
686static struct libfc_function_template fcoe_libfc_fcn_templ = {
687 .frame_send = fcoe_xmit,
688 .ddp_setup = fcoe_ddp_setup,
689 .ddp_done = fcoe_ddp_done,
690};
691
692/**
Chris Leech030f4e02009-08-25 14:00:02 -0700693 * fcoe_if_create() - this function creates the fcoe port
694 * @fcoe: fcoe_interface structure to create an fc_lport instance on
Chris Leechaf7f85d2009-08-25 13:59:24 -0700695 * @parent: device pointer to be the parent in sysfs for the SCSI host
Vasu Dev7f349142009-03-27 09:06:31 -0700696 *
Chris Leech54b649f2009-08-25 14:00:07 -0700697 * Creates fc_lport struct and scsi_host for lport, configures lport.
Vasu Dev7f349142009-03-27 09:06:31 -0700698 *
Chris Leechaf7f85d2009-08-25 13:59:24 -0700699 * Returns : The allocated fc_lport or an error pointer
Vasu Dev7f349142009-03-27 09:06:31 -0700700 */
Chris Leech030f4e02009-08-25 14:00:02 -0700701static struct fc_lport *fcoe_if_create(struct fcoe_interface *fcoe,
Chris Leechaf7f85d2009-08-25 13:59:24 -0700702 struct device *parent)
Vasu Dev7f349142009-03-27 09:06:31 -0700703{
704 int rc;
Chris Leechaf7f85d2009-08-25 13:59:24 -0700705 struct fc_lport *lport = NULL;
Chris Leech014f5c32009-08-25 13:59:30 -0700706 struct fcoe_port *port;
Vasu Dev7f349142009-03-27 09:06:31 -0700707 struct Scsi_Host *shost;
Chris Leech030f4e02009-08-25 14:00:02 -0700708 struct net_device *netdev = fcoe->netdev;
Vasu Dev7f349142009-03-27 09:06:31 -0700709
Robert Loved5488eb2009-06-10 15:30:59 -0700710 FCOE_NETDEV_DBG(netdev, "Create Interface\n");
Vasu Dev7f349142009-03-27 09:06:31 -0700711
Chris Leech014f5c32009-08-25 13:59:30 -0700712 shost = libfc_host_alloc(&fcoe_shost_template,
713 sizeof(struct fcoe_port));
714 if (!shost) {
715 FCOE_NETDEV_DBG(netdev, "Could not allocate host structure\n");
716 rc = -ENOMEM;
Chris Leech030f4e02009-08-25 14:00:02 -0700717 goto out;
Chris Leech014f5c32009-08-25 13:59:30 -0700718 }
Chris Leechaf7f85d2009-08-25 13:59:24 -0700719 lport = shost_priv(shost);
Chris Leech014f5c32009-08-25 13:59:30 -0700720 port = lport_priv(lport);
Chris Leech2e70e242009-08-25 14:00:23 -0700721 port->lport = lport;
Chris Leech014f5c32009-08-25 13:59:30 -0700722 port->fcoe = fcoe;
Chris Leech2e70e242009-08-25 14:00:23 -0700723 INIT_WORK(&port->destroy_work, fcoe_destroy_work);
Vasu Dev7f349142009-03-27 09:06:31 -0700724
725 /* configure fc_lport, e.g., em */
Chris Leechaf7f85d2009-08-25 13:59:24 -0700726 rc = fcoe_lport_config(lport);
Vasu Dev7f349142009-03-27 09:06:31 -0700727 if (rc) {
Robert Loved5488eb2009-06-10 15:30:59 -0700728 FCOE_NETDEV_DBG(netdev, "Could not configure lport for the "
729 "interface\n");
Vasu Dev7f349142009-03-27 09:06:31 -0700730 goto out_host_put;
731 }
732
Vasu Devab6b85c2009-05-17 12:33:08 +0000733 /* configure lport network properties */
Chris Leechaf7f85d2009-08-25 13:59:24 -0700734 rc = fcoe_netdev_config(lport, netdev);
Vasu Devab6b85c2009-05-17 12:33:08 +0000735 if (rc) {
Robert Loved5488eb2009-06-10 15:30:59 -0700736 FCOE_NETDEV_DBG(netdev, "Could not configure netdev for the "
737 "interface\n");
Chris Leech54b649f2009-08-25 14:00:07 -0700738 goto out_lp_destroy;
Vasu Devab6b85c2009-05-17 12:33:08 +0000739 }
Joe Eykholt97c83892009-03-17 11:42:40 -0700740
Vasu Dev7f349142009-03-27 09:06:31 -0700741 /* configure lport scsi host properties */
Chris Leechaf7f85d2009-08-25 13:59:24 -0700742 rc = fcoe_shost_config(lport, shost, parent);
Vasu Dev7f349142009-03-27 09:06:31 -0700743 if (rc) {
Robert Loved5488eb2009-06-10 15:30:59 -0700744 FCOE_NETDEV_DBG(netdev, "Could not configure shost for the "
745 "interface\n");
Chris Leech54b649f2009-08-25 14:00:07 -0700746 goto out_lp_destroy;
Vasu Dev7f349142009-03-27 09:06:31 -0700747 }
748
Vasu Dev7f349142009-03-27 09:06:31 -0700749 /* Initialize the library */
Chris Leechaf7f85d2009-08-25 13:59:24 -0700750 rc = fcoe_libfc_config(lport, &fcoe_libfc_fcn_templ);
Vasu Dev7f349142009-03-27 09:06:31 -0700751 if (rc) {
Robert Loved5488eb2009-06-10 15:30:59 -0700752 FCOE_NETDEV_DBG(netdev, "Could not configure libfc for the "
753 "interface\n");
Vasu Dev7f349142009-03-27 09:06:31 -0700754 goto out_lp_destroy;
755 }
756
Vasu Deve8af4d42009-07-29 17:05:15 -0700757 /*
758 * fcoe_em_alloc() and fcoe_hostlist_add() both
Chris Leechc863df32009-08-25 14:00:18 -0700759 * need to be atomic with respect to other changes to the hostlist
Vasu Deve8af4d42009-07-29 17:05:15 -0700760 * since fcoe_em_alloc() looks for an existing EM
761 * instance on host list updated by fcoe_hostlist_add().
Chris Leechc863df32009-08-25 14:00:18 -0700762 *
763 * This is currently handled through the fcoe_config_mutex begin held.
Vasu Deve8af4d42009-07-29 17:05:15 -0700764 */
Chris Leechc863df32009-08-25 14:00:18 -0700765
Vasu Dev96316092009-07-29 17:05:00 -0700766 /* lport exch manager allocation */
Chris Leechaf7f85d2009-08-25 13:59:24 -0700767 rc = fcoe_em_config(lport);
Vasu Dev96316092009-07-29 17:05:00 -0700768 if (rc) {
769 FCOE_NETDEV_DBG(netdev, "Could not configure the EM for the "
770 "interface\n");
771 goto out_lp_destroy;
772 }
773
Chris Leech030f4e02009-08-25 14:00:02 -0700774 fcoe_interface_get(fcoe);
Chris Leechaf7f85d2009-08-25 13:59:24 -0700775 return lport;
Vasu Dev7f349142009-03-27 09:06:31 -0700776
777out_lp_destroy:
Chris Leechaf7f85d2009-08-25 13:59:24 -0700778 fc_exch_mgr_free(lport);
Vasu Dev7f349142009-03-27 09:06:31 -0700779out_host_put:
Chris Leechaf7f85d2009-08-25 13:59:24 -0700780 scsi_host_put(lport->host);
781out:
782 return ERR_PTR(rc);
Vasu Dev7f349142009-03-27 09:06:31 -0700783}
784
785/**
786 * fcoe_if_init() - attach to scsi transport
787 *
788 * Returns : 0 on success
789 */
790static int __init fcoe_if_init(void)
791{
792 /* attach to scsi transport */
793 scsi_transport_fcoe_sw =
794 fc_attach_transport(&fcoe_transport_function);
795
796 if (!scsi_transport_fcoe_sw) {
Robert Loved5488eb2009-06-10 15:30:59 -0700797 printk(KERN_ERR "fcoe: Failed to attach to the FC transport\n");
Vasu Dev7f349142009-03-27 09:06:31 -0700798 return -ENODEV;
799 }
800
801 return 0;
802}
803
804/**
805 * fcoe_if_exit() - detach from scsi transport
806 *
807 * Returns : 0 on success
808 */
809int __exit fcoe_if_exit(void)
810{
811 fc_release_transport(scsi_transport_fcoe_sw);
Chris Leechdfc1d0f2009-08-25 14:00:13 -0700812 scsi_transport_fcoe_sw = NULL;
Vasu Dev7f349142009-03-27 09:06:31 -0700813 return 0;
814}
815
Robert Love85b4aa42008-12-09 15:10:24 -0800816/**
Robert Love8976f422009-03-17 11:41:46 -0700817 * fcoe_percpu_thread_create() - Create a receive thread for an online cpu
818 * @cpu: cpu index for the online cpu
819 */
820static void fcoe_percpu_thread_create(unsigned int cpu)
821{
822 struct fcoe_percpu_s *p;
823 struct task_struct *thread;
824
825 p = &per_cpu(fcoe_percpu, cpu);
826
827 thread = kthread_create(fcoe_percpu_receive_thread,
828 (void *)p, "fcoethread/%d", cpu);
829
830 if (likely(!IS_ERR(p->thread))) {
831 kthread_bind(thread, cpu);
832 wake_up_process(thread);
833
834 spin_lock_bh(&p->fcoe_rx_list.lock);
835 p->thread = thread;
836 spin_unlock_bh(&p->fcoe_rx_list.lock);
837 }
838}
839
840/**
841 * fcoe_percpu_thread_destroy() - removes the rx thread for the given cpu
842 * @cpu: cpu index the rx thread is to be removed
843 *
844 * Destroys a per-CPU Rx thread. Any pending skbs are moved to the
845 * current CPU's Rx thread. If the thread being destroyed is bound to
846 * the CPU processing this context the skbs will be freed.
847 */
848static void fcoe_percpu_thread_destroy(unsigned int cpu)
849{
850 struct fcoe_percpu_s *p;
851 struct task_struct *thread;
852 struct page *crc_eof;
853 struct sk_buff *skb;
854#ifdef CONFIG_SMP
855 struct fcoe_percpu_s *p0;
856 unsigned targ_cpu = smp_processor_id();
857#endif /* CONFIG_SMP */
858
Robert Loved5488eb2009-06-10 15:30:59 -0700859 FCOE_DBG("Destroying receive thread for CPU %d\n", cpu);
Robert Love8976f422009-03-17 11:41:46 -0700860
861 /* Prevent any new skbs from being queued for this CPU. */
862 p = &per_cpu(fcoe_percpu, cpu);
863 spin_lock_bh(&p->fcoe_rx_list.lock);
864 thread = p->thread;
865 p->thread = NULL;
866 crc_eof = p->crc_eof_page;
867 p->crc_eof_page = NULL;
868 p->crc_eof_offset = 0;
869 spin_unlock_bh(&p->fcoe_rx_list.lock);
870
871#ifdef CONFIG_SMP
872 /*
873 * Don't bother moving the skb's if this context is running
874 * on the same CPU that is having its thread destroyed. This
875 * can easily happen when the module is removed.
876 */
877 if (cpu != targ_cpu) {
878 p0 = &per_cpu(fcoe_percpu, targ_cpu);
879 spin_lock_bh(&p0->fcoe_rx_list.lock);
880 if (p0->thread) {
Robert Loved5488eb2009-06-10 15:30:59 -0700881 FCOE_DBG("Moving frames from CPU %d to CPU %d\n",
882 cpu, targ_cpu);
Robert Love8976f422009-03-17 11:41:46 -0700883
884 while ((skb = __skb_dequeue(&p->fcoe_rx_list)) != NULL)
885 __skb_queue_tail(&p0->fcoe_rx_list, skb);
886 spin_unlock_bh(&p0->fcoe_rx_list.lock);
887 } else {
888 /*
889 * The targeted CPU is not initialized and cannot accept
890 * new skbs. Unlock the targeted CPU and drop the skbs
891 * on the CPU that is going offline.
892 */
893 while ((skb = __skb_dequeue(&p->fcoe_rx_list)) != NULL)
894 kfree_skb(skb);
895 spin_unlock_bh(&p0->fcoe_rx_list.lock);
896 }
897 } else {
898 /*
899 * This scenario occurs when the module is being removed
900 * and all threads are being destroyed. skbs will continue
901 * to be shifted from the CPU thread that is being removed
902 * to the CPU thread associated with the CPU that is processing
903 * the module removal. Once there is only one CPU Rx thread it
904 * will reach this case and we will drop all skbs and later
905 * stop the thread.
906 */
907 spin_lock_bh(&p->fcoe_rx_list.lock);
908 while ((skb = __skb_dequeue(&p->fcoe_rx_list)) != NULL)
909 kfree_skb(skb);
910 spin_unlock_bh(&p->fcoe_rx_list.lock);
911 }
912#else
913 /*
Chris Leechdd3fd722009-04-21 16:27:36 -0700914 * This a non-SMP scenario where the singular Rx thread is
Robert Love8976f422009-03-17 11:41:46 -0700915 * being removed. Free all skbs and stop the thread.
916 */
917 spin_lock_bh(&p->fcoe_rx_list.lock);
918 while ((skb = __skb_dequeue(&p->fcoe_rx_list)) != NULL)
919 kfree_skb(skb);
920 spin_unlock_bh(&p->fcoe_rx_list.lock);
921#endif
922
923 if (thread)
924 kthread_stop(thread);
925
926 if (crc_eof)
927 put_page(crc_eof);
928}
929
930/**
931 * fcoe_cpu_callback() - fcoe cpu hotplug event callback
932 * @nfb: callback data block
933 * @action: event triggering the callback
934 * @hcpu: index for the cpu of this event
935 *
936 * This creates or destroys per cpu data for fcoe
937 *
938 * Returns NOTIFY_OK always.
939 */
940static int fcoe_cpu_callback(struct notifier_block *nfb,
941 unsigned long action, void *hcpu)
942{
943 unsigned cpu = (unsigned long)hcpu;
944
945 switch (action) {
946 case CPU_ONLINE:
947 case CPU_ONLINE_FROZEN:
Robert Loved5488eb2009-06-10 15:30:59 -0700948 FCOE_DBG("CPU %x online: Create Rx thread\n", cpu);
Robert Love8976f422009-03-17 11:41:46 -0700949 fcoe_percpu_thread_create(cpu);
950 break;
951 case CPU_DEAD:
952 case CPU_DEAD_FROZEN:
Robert Loved5488eb2009-06-10 15:30:59 -0700953 FCOE_DBG("CPU %x offline: Remove Rx thread\n", cpu);
Robert Love8976f422009-03-17 11:41:46 -0700954 fcoe_percpu_thread_destroy(cpu);
955 break;
956 default:
957 break;
958 }
959 return NOTIFY_OK;
960}
961
962static struct notifier_block fcoe_cpu_notifier = {
963 .notifier_call = fcoe_cpu_callback,
964};
965
966/**
Robert Love34f42a02009-02-27 10:55:45 -0800967 * fcoe_rcv() - this is the fcoe receive function called by NET_RX_SOFTIRQ
Robert Love85b4aa42008-12-09 15:10:24 -0800968 * @skb: the receive skb
969 * @dev: associated net device
970 * @ptype: context
Chris Leechdd3fd722009-04-21 16:27:36 -0700971 * @olddev: last device
Robert Love85b4aa42008-12-09 15:10:24 -0800972 *
973 * this function will receive the packet and build fc frame and pass it up
974 *
975 * Returns: 0 for success
Robert Love34f42a02009-02-27 10:55:45 -0800976 */
Robert Love85b4aa42008-12-09 15:10:24 -0800977int fcoe_rcv(struct sk_buff *skb, struct net_device *dev,
978 struct packet_type *ptype, struct net_device *olddev)
979{
980 struct fc_lport *lp;
981 struct fcoe_rcv_info *fr;
Chris Leech259ad852009-08-25 13:59:41 -0700982 struct fcoe_interface *fcoe;
Robert Love85b4aa42008-12-09 15:10:24 -0800983 struct fc_frame_header *fh;
Robert Love85b4aa42008-12-09 15:10:24 -0800984 struct fcoe_percpu_s *fps;
Vasu Devb2f00912009-08-25 13:58:53 -0700985 unsigned int cpu;
Robert Love85b4aa42008-12-09 15:10:24 -0800986
Chris Leech259ad852009-08-25 13:59:41 -0700987 fcoe = container_of(ptype, struct fcoe_interface, fcoe_packet_type);
Chris Leech3fe9a0b2009-08-25 13:59:46 -0700988 lp = fcoe->ctlr.lp;
Robert Love85b4aa42008-12-09 15:10:24 -0800989 if (unlikely(lp == NULL)) {
Robert Loved5488eb2009-06-10 15:30:59 -0700990 FCOE_NETDEV_DBG(dev, "Cannot find hba structure");
Robert Love85b4aa42008-12-09 15:10:24 -0800991 goto err2;
992 }
Joe Eykholt97c83892009-03-17 11:42:40 -0700993 if (!lp->link_up)
994 goto err2;
Robert Love85b4aa42008-12-09 15:10:24 -0800995
Robert Loved5488eb2009-06-10 15:30:59 -0700996 FCOE_NETDEV_DBG(dev, "skb_info: len:%d data_len:%d head:%p "
997 "data:%p tail:%p end:%p sum:%d dev:%s",
998 skb->len, skb->data_len, skb->head, skb->data,
999 skb_tail_pointer(skb), skb_end_pointer(skb),
1000 skb->csum, skb->dev ? skb->dev->name : "<NULL>");
Robert Love85b4aa42008-12-09 15:10:24 -08001001
1002 /* check for FCOE packet type */
1003 if (unlikely(eth_hdr(skb)->h_proto != htons(ETH_P_FCOE))) {
Robert Loved5488eb2009-06-10 15:30:59 -07001004 FCOE_NETDEV_DBG(dev, "Wrong FC type frame");
Robert Love85b4aa42008-12-09 15:10:24 -08001005 goto err;
1006 }
1007
1008 /*
1009 * Check for minimum frame length, and make sure required FCoE
1010 * and FC headers are pulled into the linear data area.
1011 */
1012 if (unlikely((skb->len < FCOE_MIN_FRAME) ||
1013 !pskb_may_pull(skb, FCOE_HEADER_LEN)))
1014 goto err;
1015
1016 skb_set_transport_header(skb, sizeof(struct fcoe_hdr));
1017 fh = (struct fc_frame_header *) skb_transport_header(skb);
1018
Robert Love85b4aa42008-12-09 15:10:24 -08001019 fr = fcoe_dev_from_skb(skb);
1020 fr->fr_dev = lp;
1021 fr->ptype = ptype;
Robert Love5e5e92d2009-03-17 11:41:35 -07001022
Robert Love85b4aa42008-12-09 15:10:24 -08001023 /*
Vasu Devb2f00912009-08-25 13:58:53 -07001024 * In case the incoming frame's exchange is originated from
1025 * the initiator, then received frame's exchange id is ANDed
1026 * with fc_cpu_mask bits to get the same cpu on which exchange
1027 * was originated, otherwise just use the current cpu.
Robert Love85b4aa42008-12-09 15:10:24 -08001028 */
Vasu Devb2f00912009-08-25 13:58:53 -07001029 if (ntoh24(fh->fh_f_ctl) & FC_FC_EX_CTX)
1030 cpu = ntohs(fh->fh_ox_id) & fc_cpu_mask;
1031 else
1032 cpu = smp_processor_id();
Robert Love5e5e92d2009-03-17 11:41:35 -07001033
Robert Love8976f422009-03-17 11:41:46 -07001034 fps = &per_cpu(fcoe_percpu, cpu);
Robert Love85b4aa42008-12-09 15:10:24 -08001035 spin_lock_bh(&fps->fcoe_rx_list.lock);
Robert Love8976f422009-03-17 11:41:46 -07001036 if (unlikely(!fps->thread)) {
1037 /*
1038 * The targeted CPU is not ready, let's target
1039 * the first CPU now. For non-SMP systems this
1040 * will check the same CPU twice.
1041 */
Robert Loved5488eb2009-06-10 15:30:59 -07001042 FCOE_NETDEV_DBG(dev, "CPU is online, but no receive thread "
1043 "ready for incoming skb- using first online "
1044 "CPU.\n");
Robert Love8976f422009-03-17 11:41:46 -07001045
1046 spin_unlock_bh(&fps->fcoe_rx_list.lock);
1047 cpu = first_cpu(cpu_online_map);
1048 fps = &per_cpu(fcoe_percpu, cpu);
1049 spin_lock_bh(&fps->fcoe_rx_list.lock);
1050 if (!fps->thread) {
1051 spin_unlock_bh(&fps->fcoe_rx_list.lock);
1052 goto err;
1053 }
1054 }
1055
1056 /*
1057 * We now have a valid CPU that we're targeting for
1058 * this skb. We also have this receive thread locked,
1059 * so we're free to queue skbs into it's queue.
1060 */
Robert Love85b4aa42008-12-09 15:10:24 -08001061 __skb_queue_tail(&fps->fcoe_rx_list, skb);
1062 if (fps->fcoe_rx_list.qlen == 1)
1063 wake_up_process(fps->thread);
1064
1065 spin_unlock_bh(&fps->fcoe_rx_list.lock);
1066
1067 return 0;
1068err:
Robert Love582b45b2009-03-31 15:51:50 -07001069 fc_lport_get_stats(lp)->ErrorFrames++;
Robert Love85b4aa42008-12-09 15:10:24 -08001070
1071err2:
1072 kfree_skb(skb);
1073 return -1;
1074}
Robert Love85b4aa42008-12-09 15:10:24 -08001075
1076/**
Robert Love34f42a02009-02-27 10:55:45 -08001077 * fcoe_start_io() - pass to netdev to start xmit for fcoe
Robert Love85b4aa42008-12-09 15:10:24 -08001078 * @skb: the skb to be xmitted
1079 *
1080 * Returns: 0 for success
Robert Love34f42a02009-02-27 10:55:45 -08001081 */
Robert Love85b4aa42008-12-09 15:10:24 -08001082static inline int fcoe_start_io(struct sk_buff *skb)
1083{
1084 int rc;
1085
1086 skb_get(skb);
1087 rc = dev_queue_xmit(skb);
1088 if (rc != 0)
1089 return rc;
1090 kfree_skb(skb);
1091 return 0;
1092}
1093
1094/**
Chris Leechdd3fd722009-04-21 16:27:36 -07001095 * fcoe_get_paged_crc_eof() - in case we need to alloc a page for crc_eof
Robert Love85b4aa42008-12-09 15:10:24 -08001096 * @skb: the skb to be xmitted
1097 * @tlen: total len
1098 *
1099 * Returns: 0 for success
Robert Love34f42a02009-02-27 10:55:45 -08001100 */
Robert Love85b4aa42008-12-09 15:10:24 -08001101static int fcoe_get_paged_crc_eof(struct sk_buff *skb, int tlen)
1102{
1103 struct fcoe_percpu_s *fps;
1104 struct page *page;
Robert Love85b4aa42008-12-09 15:10:24 -08001105
Robert Love5e5e92d2009-03-17 11:41:35 -07001106 fps = &get_cpu_var(fcoe_percpu);
Robert Love85b4aa42008-12-09 15:10:24 -08001107 page = fps->crc_eof_page;
1108 if (!page) {
1109 page = alloc_page(GFP_ATOMIC);
1110 if (!page) {
Robert Love5e5e92d2009-03-17 11:41:35 -07001111 put_cpu_var(fcoe_percpu);
Robert Love85b4aa42008-12-09 15:10:24 -08001112 return -ENOMEM;
1113 }
1114 fps->crc_eof_page = page;
Robert Love8976f422009-03-17 11:41:46 -07001115 fps->crc_eof_offset = 0;
Robert Love85b4aa42008-12-09 15:10:24 -08001116 }
1117
1118 get_page(page);
1119 skb_fill_page_desc(skb, skb_shinfo(skb)->nr_frags, page,
1120 fps->crc_eof_offset, tlen);
1121 skb->len += tlen;
1122 skb->data_len += tlen;
1123 skb->truesize += tlen;
1124 fps->crc_eof_offset += sizeof(struct fcoe_crc_eof);
1125
1126 if (fps->crc_eof_offset >= PAGE_SIZE) {
1127 fps->crc_eof_page = NULL;
1128 fps->crc_eof_offset = 0;
1129 put_page(page);
1130 }
Robert Love5e5e92d2009-03-17 11:41:35 -07001131 put_cpu_var(fcoe_percpu);
Robert Love85b4aa42008-12-09 15:10:24 -08001132 return 0;
1133}
1134
1135/**
Robert Love34f42a02009-02-27 10:55:45 -08001136 * fcoe_fc_crc() - calculates FC CRC in this fcoe skb
Chris Leechdd3fd722009-04-21 16:27:36 -07001137 * @fp: the fc_frame containing data to be checksummed
Robert Love85b4aa42008-12-09 15:10:24 -08001138 *
Chris Leech014f5c32009-08-25 13:59:30 -07001139 * This uses crc32() to calculate the crc for port frame
Robert Love85b4aa42008-12-09 15:10:24 -08001140 * Return : 32 bit crc
Robert Love34f42a02009-02-27 10:55:45 -08001141 */
Robert Love85b4aa42008-12-09 15:10:24 -08001142u32 fcoe_fc_crc(struct fc_frame *fp)
1143{
1144 struct sk_buff *skb = fp_skb(fp);
1145 struct skb_frag_struct *frag;
1146 unsigned char *data;
1147 unsigned long off, len, clen;
1148 u32 crc;
1149 unsigned i;
1150
1151 crc = crc32(~0, skb->data, skb_headlen(skb));
1152
1153 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
1154 frag = &skb_shinfo(skb)->frags[i];
1155 off = frag->page_offset;
1156 len = frag->size;
1157 while (len > 0) {
1158 clen = min(len, PAGE_SIZE - (off & ~PAGE_MASK));
1159 data = kmap_atomic(frag->page + (off >> PAGE_SHIFT),
1160 KM_SKB_DATA_SOFTIRQ);
1161 crc = crc32(crc, data + (off & ~PAGE_MASK), clen);
1162 kunmap_atomic(data, KM_SKB_DATA_SOFTIRQ);
1163 off += clen;
1164 len -= clen;
1165 }
1166 }
1167 return crc;
1168}
Robert Love85b4aa42008-12-09 15:10:24 -08001169
1170/**
Robert Love34f42a02009-02-27 10:55:45 -08001171 * fcoe_xmit() - FCoE frame transmit function
Chris Leech014f5c32009-08-25 13:59:30 -07001172 * @lp: the associated local fcoe
Robert Love85b4aa42008-12-09 15:10:24 -08001173 * @fp: the fc_frame to be transmitted
1174 *
1175 * Return : 0 for success
Robert Love34f42a02009-02-27 10:55:45 -08001176 */
Robert Love85b4aa42008-12-09 15:10:24 -08001177int fcoe_xmit(struct fc_lport *lp, struct fc_frame *fp)
1178{
Vasu Dev4bb6b512009-05-06 10:52:34 -07001179 int wlen;
Robert Love85b4aa42008-12-09 15:10:24 -08001180 u32 crc;
1181 struct ethhdr *eh;
1182 struct fcoe_crc_eof *cp;
1183 struct sk_buff *skb;
1184 struct fcoe_dev_stats *stats;
1185 struct fc_frame_header *fh;
1186 unsigned int hlen; /* header length implies the version */
1187 unsigned int tlen; /* trailer length */
1188 unsigned int elen; /* eth header, may include vlan */
Chris Leech3fe9a0b2009-08-25 13:59:46 -07001189 struct fcoe_port *port = lport_priv(lp);
1190 struct fcoe_interface *fcoe = port->fcoe;
Robert Love85b4aa42008-12-09 15:10:24 -08001191 u8 sof, eof;
1192 struct fcoe_hdr *hp;
1193
1194 WARN_ON((fr_len(fp) % sizeof(u32)) != 0);
1195
Robert Love85b4aa42008-12-09 15:10:24 -08001196 fh = fc_frame_header_get(fp);
Joe Eykholt97c83892009-03-17 11:42:40 -07001197 skb = fp_skb(fp);
1198 wlen = skb->len / FCOE_WORD_TO_BYTE;
1199
1200 if (!lp->link_up) {
Dan Carpenter3caf02e2009-04-21 16:27:25 -07001201 kfree_skb(skb);
Joe Eykholt97c83892009-03-17 11:42:40 -07001202 return 0;
Robert Love85b4aa42008-12-09 15:10:24 -08001203 }
1204
Joe Eykholt97c83892009-03-17 11:42:40 -07001205 if (unlikely(fh->fh_r_ctl == FC_RCTL_ELS_REQ) &&
Chris Leech3fe9a0b2009-08-25 13:59:46 -07001206 fcoe_ctlr_els_send(&fcoe->ctlr, skb))
Joe Eykholt97c83892009-03-17 11:42:40 -07001207 return 0;
1208
Robert Love85b4aa42008-12-09 15:10:24 -08001209 sof = fr_sof(fp);
1210 eof = fr_eof(fp);
1211
Vasu Dev4e57e1c2009-05-06 10:52:46 -07001212 elen = sizeof(struct ethhdr);
Robert Love85b4aa42008-12-09 15:10:24 -08001213 hlen = sizeof(struct fcoe_hdr);
1214 tlen = sizeof(struct fcoe_crc_eof);
1215 wlen = (skb->len - tlen + sizeof(crc)) / FCOE_WORD_TO_BYTE;
1216
1217 /* crc offload */
1218 if (likely(lp->crc_offload)) {
Yi Zou39ca9a02009-02-27 14:07:15 -08001219 skb->ip_summed = CHECKSUM_PARTIAL;
Robert Love85b4aa42008-12-09 15:10:24 -08001220 skb->csum_start = skb_headroom(skb);
1221 skb->csum_offset = skb->len;
1222 crc = 0;
1223 } else {
1224 skb->ip_summed = CHECKSUM_NONE;
1225 crc = fcoe_fc_crc(fp);
1226 }
1227
Chris Leech014f5c32009-08-25 13:59:30 -07001228 /* copy port crc and eof to the skb buff */
Robert Love85b4aa42008-12-09 15:10:24 -08001229 if (skb_is_nonlinear(skb)) {
1230 skb_frag_t *frag;
1231 if (fcoe_get_paged_crc_eof(skb, tlen)) {
Roel Kluine9041582009-02-27 10:56:22 -08001232 kfree_skb(skb);
Robert Love85b4aa42008-12-09 15:10:24 -08001233 return -ENOMEM;
1234 }
1235 frag = &skb_shinfo(skb)->frags[skb_shinfo(skb)->nr_frags - 1];
1236 cp = kmap_atomic(frag->page, KM_SKB_DATA_SOFTIRQ)
1237 + frag->page_offset;
1238 } else {
1239 cp = (struct fcoe_crc_eof *)skb_put(skb, tlen);
1240 }
1241
1242 memset(cp, 0, sizeof(*cp));
1243 cp->fcoe_eof = eof;
1244 cp->fcoe_crc32 = cpu_to_le32(~crc);
1245
1246 if (skb_is_nonlinear(skb)) {
1247 kunmap_atomic(cp, KM_SKB_DATA_SOFTIRQ);
1248 cp = NULL;
1249 }
1250
Chris Leech014f5c32009-08-25 13:59:30 -07001251 /* adjust skb network/transport offsets to match mac/fcoe/port */
Robert Love85b4aa42008-12-09 15:10:24 -08001252 skb_push(skb, elen + hlen);
1253 skb_reset_mac_header(skb);
1254 skb_reset_network_header(skb);
1255 skb->mac_len = elen;
Yi Zou211c7382009-02-27 14:06:37 -08001256 skb->protocol = htons(ETH_P_FCOE);
Chris Leech3fe9a0b2009-08-25 13:59:46 -07001257 skb->dev = fcoe->netdev;
Robert Love85b4aa42008-12-09 15:10:24 -08001258
1259 /* fill up mac and fcoe headers */
1260 eh = eth_hdr(skb);
1261 eh->h_proto = htons(ETH_P_FCOE);
Chris Leech3fe9a0b2009-08-25 13:59:46 -07001262 if (fcoe->ctlr.map_dest)
Robert Love85b4aa42008-12-09 15:10:24 -08001263 fc_fcoe_set_mac(eh->h_dest, fh->fh_d_id);
1264 else
1265 /* insert GW address */
Chris Leech3fe9a0b2009-08-25 13:59:46 -07001266 memcpy(eh->h_dest, fcoe->ctlr.dest_addr, ETH_ALEN);
Robert Love85b4aa42008-12-09 15:10:24 -08001267
Chris Leech3fe9a0b2009-08-25 13:59:46 -07001268 if (unlikely(fcoe->ctlr.flogi_oxid != FC_XID_UNKNOWN))
1269 memcpy(eh->h_source, fcoe->ctlr.ctl_src_addr, ETH_ALEN);
Robert Love85b4aa42008-12-09 15:10:24 -08001270 else
Chris Leech3fe9a0b2009-08-25 13:59:46 -07001271 memcpy(eh->h_source, fcoe->ctlr.data_src_addr, ETH_ALEN);
Robert Love85b4aa42008-12-09 15:10:24 -08001272
1273 hp = (struct fcoe_hdr *)(eh + 1);
1274 memset(hp, 0, sizeof(*hp));
1275 if (FC_FCOE_VER)
1276 FC_FCOE_ENCAPS_VER(hp, FC_FCOE_VER);
1277 hp->fcoe_sof = sof;
1278
Yi Zou39ca9a02009-02-27 14:07:15 -08001279 /* fcoe lso, mss is in max_payload which is non-zero for FCP data */
1280 if (lp->seq_offload && fr_max_payload(fp)) {
1281 skb_shinfo(skb)->gso_type = SKB_GSO_FCOE;
1282 skb_shinfo(skb)->gso_size = fr_max_payload(fp);
1283 } else {
1284 skb_shinfo(skb)->gso_type = 0;
1285 skb_shinfo(skb)->gso_size = 0;
1286 }
Robert Love85b4aa42008-12-09 15:10:24 -08001287 /* update tx stats: regardless if LLD fails */
Robert Love582b45b2009-03-31 15:51:50 -07001288 stats = fc_lport_get_stats(lp);
1289 stats->TxFrames++;
1290 stats->TxWords += wlen;
Robert Love85b4aa42008-12-09 15:10:24 -08001291
1292 /* send down to lld */
1293 fr_dev(fp) = lp;
Chris Leech014f5c32009-08-25 13:59:30 -07001294 if (port->fcoe_pending_queue.qlen)
Vasu Dev4bb6b512009-05-06 10:52:34 -07001295 fcoe_check_wait_queue(lp, skb);
1296 else if (fcoe_start_io(skb))
1297 fcoe_check_wait_queue(lp, skb);
Robert Love85b4aa42008-12-09 15:10:24 -08001298
1299 return 0;
1300}
Robert Love85b4aa42008-12-09 15:10:24 -08001301
Robert Love34f42a02009-02-27 10:55:45 -08001302/**
1303 * fcoe_percpu_receive_thread() - recv thread per cpu
Robert Love85b4aa42008-12-09 15:10:24 -08001304 * @arg: ptr to the fcoe per cpu struct
1305 *
1306 * Return: 0 for success
Robert Love85b4aa42008-12-09 15:10:24 -08001307 */
1308int fcoe_percpu_receive_thread(void *arg)
1309{
1310 struct fcoe_percpu_s *p = arg;
1311 u32 fr_len;
1312 struct fc_lport *lp;
1313 struct fcoe_rcv_info *fr;
1314 struct fcoe_dev_stats *stats;
1315 struct fc_frame_header *fh;
1316 struct sk_buff *skb;
1317 struct fcoe_crc_eof crc_eof;
1318 struct fc_frame *fp;
1319 u8 *mac = NULL;
Chris Leech014f5c32009-08-25 13:59:30 -07001320 struct fcoe_port *port;
Robert Love85b4aa42008-12-09 15:10:24 -08001321 struct fcoe_hdr *hp;
1322
Robert Love4469c192009-02-27 10:56:38 -08001323 set_user_nice(current, -20);
Robert Love85b4aa42008-12-09 15:10:24 -08001324
1325 while (!kthread_should_stop()) {
1326
1327 spin_lock_bh(&p->fcoe_rx_list.lock);
1328 while ((skb = __skb_dequeue(&p->fcoe_rx_list)) == NULL) {
1329 set_current_state(TASK_INTERRUPTIBLE);
1330 spin_unlock_bh(&p->fcoe_rx_list.lock);
1331 schedule();
1332 set_current_state(TASK_RUNNING);
1333 if (kthread_should_stop())
1334 return 0;
1335 spin_lock_bh(&p->fcoe_rx_list.lock);
1336 }
1337 spin_unlock_bh(&p->fcoe_rx_list.lock);
1338 fr = fcoe_dev_from_skb(skb);
1339 lp = fr->fr_dev;
1340 if (unlikely(lp == NULL)) {
Robert Loved5488eb2009-06-10 15:30:59 -07001341 FCOE_NETDEV_DBG(skb->dev, "Invalid HBA Structure");
Robert Love85b4aa42008-12-09 15:10:24 -08001342 kfree_skb(skb);
1343 continue;
1344 }
1345
Robert Loved5488eb2009-06-10 15:30:59 -07001346 FCOE_NETDEV_DBG(skb->dev, "skb_info: len:%d data_len:%d "
1347 "head:%p data:%p tail:%p end:%p sum:%d dev:%s",
1348 skb->len, skb->data_len,
1349 skb->head, skb->data, skb_tail_pointer(skb),
1350 skb_end_pointer(skb), skb->csum,
1351 skb->dev ? skb->dev->name : "<NULL>");
Robert Love85b4aa42008-12-09 15:10:24 -08001352
1353 /*
1354 * Save source MAC address before discarding header.
1355 */
Chris Leech014f5c32009-08-25 13:59:30 -07001356 port = lport_priv(lp);
Robert Love85b4aa42008-12-09 15:10:24 -08001357 if (skb_is_nonlinear(skb))
1358 skb_linearize(skb); /* not ideal */
Joe Eykholt97c83892009-03-17 11:42:40 -07001359 mac = eth_hdr(skb)->h_source;
Robert Love85b4aa42008-12-09 15:10:24 -08001360
1361 /*
1362 * Frame length checks and setting up the header pointers
1363 * was done in fcoe_rcv already.
1364 */
1365 hp = (struct fcoe_hdr *) skb_network_header(skb);
1366 fh = (struct fc_frame_header *) skb_transport_header(skb);
1367
Robert Love582b45b2009-03-31 15:51:50 -07001368 stats = fc_lport_get_stats(lp);
Robert Love85b4aa42008-12-09 15:10:24 -08001369 if (unlikely(FC_FCOE_DECAPS_VER(hp) != FC_FCOE_VER)) {
Robert Love582b45b2009-03-31 15:51:50 -07001370 if (stats->ErrorFrames < 5)
Robert Loved5488eb2009-06-10 15:30:59 -07001371 printk(KERN_WARNING "fcoe: FCoE version "
Robert Love582b45b2009-03-31 15:51:50 -07001372 "mismatch: The frame has "
1373 "version %x, but the "
1374 "initiator supports version "
1375 "%x\n", FC_FCOE_DECAPS_VER(hp),
1376 FC_FCOE_VER);
1377 stats->ErrorFrames++;
Robert Love85b4aa42008-12-09 15:10:24 -08001378 kfree_skb(skb);
1379 continue;
1380 }
1381
1382 skb_pull(skb, sizeof(struct fcoe_hdr));
1383 fr_len = skb->len - sizeof(struct fcoe_crc_eof);
1384
Robert Love582b45b2009-03-31 15:51:50 -07001385 stats->RxFrames++;
1386 stats->RxWords += fr_len / FCOE_WORD_TO_BYTE;
Robert Love85b4aa42008-12-09 15:10:24 -08001387
1388 fp = (struct fc_frame *)skb;
1389 fc_frame_init(fp);
1390 fr_dev(fp) = lp;
1391 fr_sof(fp) = hp->fcoe_sof;
1392
1393 /* Copy out the CRC and EOF trailer for access */
1394 if (skb_copy_bits(skb, fr_len, &crc_eof, sizeof(crc_eof))) {
1395 kfree_skb(skb);
1396 continue;
1397 }
1398 fr_eof(fp) = crc_eof.fcoe_eof;
1399 fr_crc(fp) = crc_eof.fcoe_crc32;
1400 if (pskb_trim(skb, fr_len)) {
1401 kfree_skb(skb);
1402 continue;
1403 }
1404
1405 /*
1406 * We only check CRC if no offload is available and if it is
1407 * it's solicited data, in which case, the FCP layer would
1408 * check it during the copy.
1409 */
Yi Zou07c00ec2009-02-27 14:07:31 -08001410 if (lp->crc_offload && skb->ip_summed == CHECKSUM_UNNECESSARY)
Robert Love85b4aa42008-12-09 15:10:24 -08001411 fr_flags(fp) &= ~FCPHF_CRC_UNCHECKED;
1412 else
1413 fr_flags(fp) |= FCPHF_CRC_UNCHECKED;
1414
1415 fh = fc_frame_header_get(fp);
1416 if (fh->fh_r_ctl == FC_RCTL_DD_SOL_DATA &&
1417 fh->fh_type == FC_TYPE_FCP) {
Vasu Dev52ff8782009-07-29 17:05:10 -07001418 fc_exch_recv(lp, fp);
Robert Love85b4aa42008-12-09 15:10:24 -08001419 continue;
1420 }
1421 if (fr_flags(fp) & FCPHF_CRC_UNCHECKED) {
1422 if (le32_to_cpu(fr_crc(fp)) !=
1423 ~crc32(~0, skb->data, fr_len)) {
Robert Loved5488eb2009-06-10 15:30:59 -07001424 if (stats->InvalidCRCCount < 5)
Robert Love85b4aa42008-12-09 15:10:24 -08001425 printk(KERN_WARNING "fcoe: dropping "
1426 "frame with CRC error\n");
1427 stats->InvalidCRCCount++;
1428 stats->ErrorFrames++;
1429 fc_frame_free(fp);
1430 continue;
1431 }
1432 fr_flags(fp) &= ~FCPHF_CRC_UNCHECKED;
1433 }
Chris Leech3fe9a0b2009-08-25 13:59:46 -07001434 if (unlikely(port->fcoe->ctlr.flogi_oxid != FC_XID_UNKNOWN) &&
1435 fcoe_ctlr_recv_flogi(&port->fcoe->ctlr, fp, mac)) {
Joe Eykholt97c83892009-03-17 11:42:40 -07001436 fc_frame_free(fp);
1437 continue;
1438 }
Vasu Dev52ff8782009-07-29 17:05:10 -07001439 fc_exch_recv(lp, fp);
Robert Love85b4aa42008-12-09 15:10:24 -08001440 }
1441 return 0;
1442}
1443
1444/**
Chris Leechdd3fd722009-04-21 16:27:36 -07001445 * fcoe_check_wait_queue() - attempt to clear the transmit backlog
1446 * @lp: the fc_lport
Robert Love85b4aa42008-12-09 15:10:24 -08001447 *
1448 * This empties the wait_queue, dequeue the head of the wait_queue queue
1449 * and calls fcoe_start_io() for each packet, if all skb have been
Vasu Devc826a312009-02-27 10:56:27 -08001450 * transmitted, return qlen or -1 if a error occurs, then restore
Chris Leechdd3fd722009-04-21 16:27:36 -07001451 * wait_queue and try again later.
Robert Love85b4aa42008-12-09 15:10:24 -08001452 *
1453 * The wait_queue is used when the skb transmit fails. skb will go
Chris Leechdd3fd722009-04-21 16:27:36 -07001454 * in the wait_queue which will be emptied by the timer function or
Robert Love85b4aa42008-12-09 15:10:24 -08001455 * by the next skb transmit.
Robert Love34f42a02009-02-27 10:55:45 -08001456 */
Vasu Dev4bb6b512009-05-06 10:52:34 -07001457static void fcoe_check_wait_queue(struct fc_lport *lp, struct sk_buff *skb)
Robert Love85b4aa42008-12-09 15:10:24 -08001458{
Chris Leech014f5c32009-08-25 13:59:30 -07001459 struct fcoe_port *port = lport_priv(lp);
Vasu Dev4bb6b512009-05-06 10:52:34 -07001460 int rc;
Robert Love85b4aa42008-12-09 15:10:24 -08001461
Chris Leech014f5c32009-08-25 13:59:30 -07001462 spin_lock_bh(&port->fcoe_pending_queue.lock);
Vasu Dev4bb6b512009-05-06 10:52:34 -07001463
1464 if (skb)
Chris Leech014f5c32009-08-25 13:59:30 -07001465 __skb_queue_tail(&port->fcoe_pending_queue, skb);
Vasu Dev4bb6b512009-05-06 10:52:34 -07001466
Chris Leech014f5c32009-08-25 13:59:30 -07001467 if (port->fcoe_pending_queue_active)
Vasu Devc826a312009-02-27 10:56:27 -08001468 goto out;
Chris Leech014f5c32009-08-25 13:59:30 -07001469 port->fcoe_pending_queue_active = 1;
Chris Leech55c8baf2009-02-27 10:56:32 -08001470
Chris Leech014f5c32009-08-25 13:59:30 -07001471 while (port->fcoe_pending_queue.qlen) {
Chris Leech55c8baf2009-02-27 10:56:32 -08001472 /* keep qlen > 0 until fcoe_start_io succeeds */
Chris Leech014f5c32009-08-25 13:59:30 -07001473 port->fcoe_pending_queue.qlen++;
1474 skb = __skb_dequeue(&port->fcoe_pending_queue);
Chris Leech55c8baf2009-02-27 10:56:32 -08001475
Chris Leech014f5c32009-08-25 13:59:30 -07001476 spin_unlock_bh(&port->fcoe_pending_queue.lock);
Chris Leech55c8baf2009-02-27 10:56:32 -08001477 rc = fcoe_start_io(skb);
Chris Leech014f5c32009-08-25 13:59:30 -07001478 spin_lock_bh(&port->fcoe_pending_queue.lock);
Chris Leech55c8baf2009-02-27 10:56:32 -08001479
1480 if (rc) {
Chris Leech014f5c32009-08-25 13:59:30 -07001481 __skb_queue_head(&port->fcoe_pending_queue, skb);
Chris Leech55c8baf2009-02-27 10:56:32 -08001482 /* undo temporary increment above */
Chris Leech014f5c32009-08-25 13:59:30 -07001483 port->fcoe_pending_queue.qlen--;
Chris Leech55c8baf2009-02-27 10:56:32 -08001484 break;
Robert Love85b4aa42008-12-09 15:10:24 -08001485 }
Chris Leech55c8baf2009-02-27 10:56:32 -08001486 /* undo temporary increment above */
Chris Leech014f5c32009-08-25 13:59:30 -07001487 port->fcoe_pending_queue.qlen--;
Robert Love85b4aa42008-12-09 15:10:24 -08001488 }
Chris Leech55c8baf2009-02-27 10:56:32 -08001489
Chris Leech014f5c32009-08-25 13:59:30 -07001490 if (port->fcoe_pending_queue.qlen < FCOE_LOW_QUEUE_DEPTH)
Chris Leech55c8baf2009-02-27 10:56:32 -08001491 lp->qfull = 0;
Chris Leech014f5c32009-08-25 13:59:30 -07001492 if (port->fcoe_pending_queue.qlen && !timer_pending(&port->timer))
1493 mod_timer(&port->timer, jiffies + 2);
1494 port->fcoe_pending_queue_active = 0;
Vasu Devc826a312009-02-27 10:56:27 -08001495out:
Chris Leech014f5c32009-08-25 13:59:30 -07001496 if (port->fcoe_pending_queue.qlen > FCOE_MAX_QUEUE_DEPTH)
Vasu Dev4bb6b512009-05-06 10:52:34 -07001497 lp->qfull = 1;
Chris Leech014f5c32009-08-25 13:59:30 -07001498 spin_unlock_bh(&port->fcoe_pending_queue.lock);
Vasu Dev4bb6b512009-05-06 10:52:34 -07001499 return;
Robert Love85b4aa42008-12-09 15:10:24 -08001500}
1501
1502/**
Robert Love34f42a02009-02-27 10:55:45 -08001503 * fcoe_dev_setup() - setup link change notification interface
1504 */
Randy Dunlapb0d428a2009-04-27 21:49:31 -07001505static void fcoe_dev_setup(void)
Robert Love85b4aa42008-12-09 15:10:24 -08001506{
Robert Love85b4aa42008-12-09 15:10:24 -08001507 register_netdevice_notifier(&fcoe_notifier);
1508}
1509
1510/**
Randy Dunlapb0d428a2009-04-27 21:49:31 -07001511 * fcoe_dev_cleanup() - cleanup link change notification interface
Robert Love34f42a02009-02-27 10:55:45 -08001512 */
Robert Love85b4aa42008-12-09 15:10:24 -08001513static void fcoe_dev_cleanup(void)
1514{
1515 unregister_netdevice_notifier(&fcoe_notifier);
1516}
1517
1518/**
Robert Love34f42a02009-02-27 10:55:45 -08001519 * fcoe_device_notification() - netdev event notification callback
Robert Love85b4aa42008-12-09 15:10:24 -08001520 * @notifier: context of the notification
1521 * @event: type of event
1522 * @ptr: fixed array for output parsed ifname
1523 *
1524 * This function is called by the ethernet driver in case of link change event
1525 *
1526 * Returns: 0 for success
Robert Love34f42a02009-02-27 10:55:45 -08001527 */
Robert Love85b4aa42008-12-09 15:10:24 -08001528static int fcoe_device_notification(struct notifier_block *notifier,
1529 ulong event, void *ptr)
1530{
1531 struct fc_lport *lp = NULL;
Vasu Dev1d1b88d2009-07-29 17:05:45 -07001532 struct net_device *netdev = ptr;
Chris Leech014f5c32009-08-25 13:59:30 -07001533 struct fcoe_interface *fcoe;
Chris Leech2e70e242009-08-25 14:00:23 -07001534 struct fcoe_port *port;
Robert Love85b4aa42008-12-09 15:10:24 -08001535 struct fcoe_dev_stats *stats;
Joe Eykholt97c83892009-03-17 11:42:40 -07001536 u32 link_possible = 1;
Robert Love85b4aa42008-12-09 15:10:24 -08001537 u32 mfs;
1538 int rc = NOTIFY_OK;
1539
Chris Leech014f5c32009-08-25 13:59:30 -07001540 list_for_each_entry(fcoe, &fcoe_hostlist, list) {
Chris Leech25024982009-08-25 13:59:35 -07001541 if (fcoe->netdev == netdev) {
Chris Leech3fe9a0b2009-08-25 13:59:46 -07001542 lp = fcoe->ctlr.lp;
Robert Love85b4aa42008-12-09 15:10:24 -08001543 break;
1544 }
1545 }
Robert Love85b4aa42008-12-09 15:10:24 -08001546 if (lp == NULL) {
1547 rc = NOTIFY_DONE;
1548 goto out;
1549 }
1550
Robert Love85b4aa42008-12-09 15:10:24 -08001551 switch (event) {
1552 case NETDEV_DOWN:
1553 case NETDEV_GOING_DOWN:
Joe Eykholt97c83892009-03-17 11:42:40 -07001554 link_possible = 0;
Robert Love85b4aa42008-12-09 15:10:24 -08001555 break;
1556 case NETDEV_UP:
1557 case NETDEV_CHANGE:
Robert Love85b4aa42008-12-09 15:10:24 -08001558 break;
1559 case NETDEV_CHANGEMTU:
Vasu Dev1d1b88d2009-07-29 17:05:45 -07001560 mfs = netdev->mtu - (sizeof(struct fcoe_hdr) +
1561 sizeof(struct fcoe_crc_eof));
Robert Love85b4aa42008-12-09 15:10:24 -08001562 if (mfs >= FC_MIN_MAX_FRAME)
1563 fc_set_mfs(lp, mfs);
Robert Love85b4aa42008-12-09 15:10:24 -08001564 break;
1565 case NETDEV_REGISTER:
1566 break;
Chris Leech2e70e242009-08-25 14:00:23 -07001567 case NETDEV_UNREGISTER:
1568 list_del(&fcoe->list);
1569 port = lport_priv(fcoe->ctlr.lp);
1570 fcoe_interface_cleanup(fcoe);
1571 schedule_work(&port->destroy_work);
1572 goto out;
1573 break;
Robert Love85b4aa42008-12-09 15:10:24 -08001574 default:
Vasu Dev1d1b88d2009-07-29 17:05:45 -07001575 FCOE_NETDEV_DBG(netdev, "Unknown event %ld "
Robert Loved5488eb2009-06-10 15:30:59 -07001576 "from netdev netlink\n", event);
Robert Love85b4aa42008-12-09 15:10:24 -08001577 }
Joe Eykholt97c83892009-03-17 11:42:40 -07001578 if (link_possible && !fcoe_link_ok(lp))
Chris Leech3fe9a0b2009-08-25 13:59:46 -07001579 fcoe_ctlr_link_up(&fcoe->ctlr);
1580 else if (fcoe_ctlr_link_down(&fcoe->ctlr)) {
Joe Eykholt97c83892009-03-17 11:42:40 -07001581 stats = fc_lport_get_stats(lp);
1582 stats->LinkFailureCount++;
1583 fcoe_clean_pending_queue(lp);
Robert Love85b4aa42008-12-09 15:10:24 -08001584 }
1585out:
1586 return rc;
1587}
1588
1589/**
Robert Love34f42a02009-02-27 10:55:45 -08001590 * fcoe_if_to_netdev() - parse a name buffer to get netdev
Robert Love85b4aa42008-12-09 15:10:24 -08001591 * @buffer: incoming buffer to be copied
1592 *
Chris Leechdd3fd722009-04-21 16:27:36 -07001593 * Returns: NULL or ptr to net_device
Robert Love34f42a02009-02-27 10:55:45 -08001594 */
Robert Love85b4aa42008-12-09 15:10:24 -08001595static struct net_device *fcoe_if_to_netdev(const char *buffer)
1596{
1597 char *cp;
1598 char ifname[IFNAMSIZ + 2];
1599
1600 if (buffer) {
1601 strlcpy(ifname, buffer, IFNAMSIZ);
1602 cp = ifname + strlen(ifname);
1603 while (--cp >= ifname && *cp == '\n')
1604 *cp = '\0';
1605 return dev_get_by_name(&init_net, ifname);
1606 }
1607 return NULL;
1608}
1609
1610/**
Robert Love34f42a02009-02-27 10:55:45 -08001611 * fcoe_destroy() - handles the destroy from sysfs
Chris Leechdd3fd722009-04-21 16:27:36 -07001612 * @buffer: expected to be an eth if name
Robert Love85b4aa42008-12-09 15:10:24 -08001613 * @kp: associated kernel param
1614 *
1615 * Returns: 0 for success
Robert Love34f42a02009-02-27 10:55:45 -08001616 */
Robert Love85b4aa42008-12-09 15:10:24 -08001617static int fcoe_destroy(const char *buffer, struct kernel_param *kp)
1618{
Chris Leech030f4e02009-08-25 14:00:02 -07001619 struct fcoe_interface *fcoe;
Chris Leech2e70e242009-08-25 14:00:23 -07001620 struct net_device *netdev;
Chris Leechaf7f85d2009-08-25 13:59:24 -07001621 int rc;
Robert Love85b4aa42008-12-09 15:10:24 -08001622
Chris Leechdfc1d0f2009-08-25 14:00:13 -07001623 mutex_lock(&fcoe_config_mutex);
1624#ifdef CONFIG_FCOE_MODULE
1625 /*
1626 * Make sure the module has been initialized, and is not about to be
1627 * removed. Module paramter sysfs files are writable before the
1628 * module_init function is called and after module_exit.
1629 */
1630 if (THIS_MODULE->state != MODULE_STATE_LIVE) {
1631 rc = -ENODEV;
1632 goto out_nodev;
1633 }
1634#endif
1635
Robert Love85b4aa42008-12-09 15:10:24 -08001636 netdev = fcoe_if_to_netdev(buffer);
1637 if (!netdev) {
1638 rc = -ENODEV;
1639 goto out_nodev;
1640 }
Chris Leech2e70e242009-08-25 14:00:23 -07001641
Chris Leech090eb6c2009-08-25 14:00:28 -07001642 rtnl_lock();
Chris Leech2e70e242009-08-25 14:00:23 -07001643 fcoe = fcoe_hostlist_lookup_port(netdev);
1644 if (!fcoe) {
Chris Leech090eb6c2009-08-25 14:00:28 -07001645 rtnl_unlock();
Robert Love85b4aa42008-12-09 15:10:24 -08001646 rc = -ENODEV;
1647 goto out_putdev;
1648 }
Chris Leech2e70e242009-08-25 14:00:23 -07001649 list_del(&fcoe->list);
Chris Leech2e70e242009-08-25 14:00:23 -07001650 fcoe_interface_cleanup(fcoe);
1651 rtnl_unlock();
1652 fcoe_if_destroy(fcoe->ctlr.lp);
Robert Love85b4aa42008-12-09 15:10:24 -08001653out_putdev:
1654 dev_put(netdev);
1655out_nodev:
Chris Leechdfc1d0f2009-08-25 14:00:13 -07001656 mutex_unlock(&fcoe_config_mutex);
Robert Love85b4aa42008-12-09 15:10:24 -08001657 return rc;
1658}
1659
Chris Leech2e70e242009-08-25 14:00:23 -07001660static void fcoe_destroy_work(struct work_struct *work)
1661{
1662 struct fcoe_port *port;
1663
1664 port = container_of(work, struct fcoe_port, destroy_work);
1665 mutex_lock(&fcoe_config_mutex);
1666 fcoe_if_destroy(port->lport);
1667 mutex_unlock(&fcoe_config_mutex);
1668}
1669
Robert Love85b4aa42008-12-09 15:10:24 -08001670/**
Robert Love34f42a02009-02-27 10:55:45 -08001671 * fcoe_create() - Handles the create call from sysfs
Chris Leechdd3fd722009-04-21 16:27:36 -07001672 * @buffer: expected to be an eth if name
Robert Love85b4aa42008-12-09 15:10:24 -08001673 * @kp: associated kernel param
1674 *
1675 * Returns: 0 for success
Robert Love34f42a02009-02-27 10:55:45 -08001676 */
Robert Love85b4aa42008-12-09 15:10:24 -08001677static int fcoe_create(const char *buffer, struct kernel_param *kp)
1678{
1679 int rc;
Chris Leech030f4e02009-08-25 14:00:02 -07001680 struct fcoe_interface *fcoe;
Chris Leechaf7f85d2009-08-25 13:59:24 -07001681 struct fc_lport *lport;
Robert Love85b4aa42008-12-09 15:10:24 -08001682 struct net_device *netdev;
1683
Chris Leechdfc1d0f2009-08-25 14:00:13 -07001684 mutex_lock(&fcoe_config_mutex);
1685#ifdef CONFIG_FCOE_MODULE
1686 /*
1687 * Make sure the module has been initialized, and is not about to be
1688 * removed. Module paramter sysfs files are writable before the
1689 * module_init function is called and after module_exit.
1690 */
1691 if (THIS_MODULE->state != MODULE_STATE_LIVE) {
1692 rc = -ENODEV;
1693 goto out_nodev;
1694 }
1695#endif
1696
Chris Leech2e70e242009-08-25 14:00:23 -07001697 rtnl_lock();
Robert Love85b4aa42008-12-09 15:10:24 -08001698 netdev = fcoe_if_to_netdev(buffer);
1699 if (!netdev) {
1700 rc = -ENODEV;
1701 goto out_nodev;
1702 }
Chris Leech2e70e242009-08-25 14:00:23 -07001703
Robert Love85b4aa42008-12-09 15:10:24 -08001704 /* look for existing lport */
1705 if (fcoe_hostlist_lookup(netdev)) {
1706 rc = -EEXIST;
1707 goto out_putdev;
1708 }
Robert Love85b4aa42008-12-09 15:10:24 -08001709
Chris Leech030f4e02009-08-25 14:00:02 -07001710 fcoe = fcoe_interface_create(netdev);
1711 if (!fcoe) {
1712 rc = -ENOMEM;
1713 goto out_putdev;
1714 }
1715
1716 lport = fcoe_if_create(fcoe, &netdev->dev);
Chris Leechaf7f85d2009-08-25 13:59:24 -07001717 if (IS_ERR(lport)) {
Robert Loved5488eb2009-06-10 15:30:59 -07001718 printk(KERN_ERR "fcoe: Failed to create interface (%s)\n",
Robert Love85b4aa42008-12-09 15:10:24 -08001719 netdev->name);
Robert Love85b4aa42008-12-09 15:10:24 -08001720 rc = -EIO;
Chris Leech2e70e242009-08-25 14:00:23 -07001721 fcoe_interface_cleanup(fcoe);
Chris Leech030f4e02009-08-25 14:00:02 -07001722 goto out_free;
Robert Love85b4aa42008-12-09 15:10:24 -08001723 }
Chris Leech030f4e02009-08-25 14:00:02 -07001724
Chris Leech54b649f2009-08-25 14:00:07 -07001725 /* Make this the "master" N_Port */
1726 fcoe->ctlr.lp = lport;
Chris Leech030f4e02009-08-25 14:00:02 -07001727
Chris Leechc863df32009-08-25 14:00:18 -07001728 /* add to lports list */
1729 fcoe_hostlist_add(lport);
1730
Chris Leech54b649f2009-08-25 14:00:07 -07001731 /* start FIP Discovery and FLOGI */
1732 lport->boot_time = jiffies;
1733 fc_fabric_login(lport);
1734 if (!fcoe_link_ok(lport))
1735 fcoe_ctlr_link_up(&fcoe->ctlr);
1736
1737 rc = 0;
Chris Leech030f4e02009-08-25 14:00:02 -07001738out_free:
Chris Leech54b649f2009-08-25 14:00:07 -07001739 /*
1740 * Release from init in fcoe_interface_create(), on success lport
1741 * should be holding a reference taken in fcoe_if_create().
1742 */
Chris Leech030f4e02009-08-25 14:00:02 -07001743 fcoe_interface_put(fcoe);
Robert Love85b4aa42008-12-09 15:10:24 -08001744out_putdev:
1745 dev_put(netdev);
1746out_nodev:
Chris Leech2e70e242009-08-25 14:00:23 -07001747 rtnl_unlock();
Chris Leechdfc1d0f2009-08-25 14:00:13 -07001748 mutex_unlock(&fcoe_config_mutex);
Robert Love85b4aa42008-12-09 15:10:24 -08001749 return rc;
1750}
1751
1752module_param_call(create, fcoe_create, NULL, NULL, S_IWUSR);
1753__MODULE_PARM_TYPE(create, "string");
Chris Leech014f5c32009-08-25 13:59:30 -07001754MODULE_PARM_DESC(create, "Create fcoe fcoe using net device passed in.");
Robert Love85b4aa42008-12-09 15:10:24 -08001755module_param_call(destroy, fcoe_destroy, NULL, NULL, S_IWUSR);
1756__MODULE_PARM_TYPE(destroy, "string");
Chris Leech014f5c32009-08-25 13:59:30 -07001757MODULE_PARM_DESC(destroy, "Destroy fcoe fcoe");
Robert Love85b4aa42008-12-09 15:10:24 -08001758
Robert Love34f42a02009-02-27 10:55:45 -08001759/**
1760 * fcoe_link_ok() - Check if link is ok for the fc_lport
Robert Love85b4aa42008-12-09 15:10:24 -08001761 * @lp: ptr to the fc_lport
1762 *
1763 * Any permanently-disqualifying conditions have been previously checked.
1764 * This also updates the speed setting, which may change with link for 100/1000.
1765 *
1766 * This function should probably be checking for PAUSE support at some point
1767 * in the future. Currently Per-priority-pause is not determinable using
1768 * ethtool, so we shouldn't be restrictive until that problem is resolved.
1769 *
1770 * Returns: 0 if link is OK for use by FCoE.
1771 *
1772 */
1773int fcoe_link_ok(struct fc_lport *lp)
1774{
Chris Leech014f5c32009-08-25 13:59:30 -07001775 struct fcoe_port *port = lport_priv(lp);
Chris Leech25024982009-08-25 13:59:35 -07001776 struct net_device *dev = port->fcoe->netdev;
Robert Love85b4aa42008-12-09 15:10:24 -08001777 struct ethtool_cmd ecmd = { ETHTOOL_GSET };
Robert Love85b4aa42008-12-09 15:10:24 -08001778
Yi Zou2f718d62009-07-29 17:04:01 -07001779 if ((dev->flags & IFF_UP) && netif_carrier_ok(dev) &&
1780 (!dev_ethtool_get_settings(dev, &ecmd))) {
1781 lp->link_supported_speeds &=
1782 ~(FC_PORTSPEED_1GBIT | FC_PORTSPEED_10GBIT);
1783 if (ecmd.supported & (SUPPORTED_1000baseT_Half |
1784 SUPPORTED_1000baseT_Full))
1785 lp->link_supported_speeds |= FC_PORTSPEED_1GBIT;
1786 if (ecmd.supported & SUPPORTED_10000baseT_Full)
1787 lp->link_supported_speeds |=
1788 FC_PORTSPEED_10GBIT;
1789 if (ecmd.speed == SPEED_1000)
1790 lp->link_speed = FC_PORTSPEED_1GBIT;
1791 if (ecmd.speed == SPEED_10000)
1792 lp->link_speed = FC_PORTSPEED_10GBIT;
Robert Love85b4aa42008-12-09 15:10:24 -08001793
Yi Zou2f718d62009-07-29 17:04:01 -07001794 return 0;
1795 }
1796 return -1;
Robert Love85b4aa42008-12-09 15:10:24 -08001797}
Robert Love85b4aa42008-12-09 15:10:24 -08001798
Robert Love34f42a02009-02-27 10:55:45 -08001799/**
1800 * fcoe_percpu_clean() - Clear the pending skbs for an lport
Robert Love85b4aa42008-12-09 15:10:24 -08001801 * @lp: the fc_lport
1802 */
1803void fcoe_percpu_clean(struct fc_lport *lp)
1804{
Robert Love85b4aa42008-12-09 15:10:24 -08001805 struct fcoe_percpu_s *pp;
1806 struct fcoe_rcv_info *fr;
1807 struct sk_buff_head *list;
1808 struct sk_buff *skb, *next;
1809 struct sk_buff *head;
Robert Love5e5e92d2009-03-17 11:41:35 -07001810 unsigned int cpu;
Robert Love85b4aa42008-12-09 15:10:24 -08001811
Robert Love5e5e92d2009-03-17 11:41:35 -07001812 for_each_possible_cpu(cpu) {
1813 pp = &per_cpu(fcoe_percpu, cpu);
1814 spin_lock_bh(&pp->fcoe_rx_list.lock);
1815 list = &pp->fcoe_rx_list;
1816 head = list->next;
1817 for (skb = head; skb != (struct sk_buff *)list;
1818 skb = next) {
1819 next = skb->next;
1820 fr = fcoe_dev_from_skb(skb);
1821 if (fr->fr_dev == lp) {
1822 __skb_unlink(skb, list);
1823 kfree_skb(skb);
Robert Love85b4aa42008-12-09 15:10:24 -08001824 }
Robert Love85b4aa42008-12-09 15:10:24 -08001825 }
Robert Love5e5e92d2009-03-17 11:41:35 -07001826 spin_unlock_bh(&pp->fcoe_rx_list.lock);
Robert Love85b4aa42008-12-09 15:10:24 -08001827 }
1828}
Robert Love85b4aa42008-12-09 15:10:24 -08001829
1830/**
Robert Love34f42a02009-02-27 10:55:45 -08001831 * fcoe_clean_pending_queue() - Dequeue a skb and free it
Robert Love85b4aa42008-12-09 15:10:24 -08001832 * @lp: the corresponding fc_lport
1833 *
1834 * Returns: none
Robert Love34f42a02009-02-27 10:55:45 -08001835 */
Robert Love85b4aa42008-12-09 15:10:24 -08001836void fcoe_clean_pending_queue(struct fc_lport *lp)
1837{
Chris Leech014f5c32009-08-25 13:59:30 -07001838 struct fcoe_port *port = lport_priv(lp);
Robert Love85b4aa42008-12-09 15:10:24 -08001839 struct sk_buff *skb;
1840
Chris Leech014f5c32009-08-25 13:59:30 -07001841 spin_lock_bh(&port->fcoe_pending_queue.lock);
1842 while ((skb = __skb_dequeue(&port->fcoe_pending_queue)) != NULL) {
1843 spin_unlock_bh(&port->fcoe_pending_queue.lock);
Robert Love85b4aa42008-12-09 15:10:24 -08001844 kfree_skb(skb);
Chris Leech014f5c32009-08-25 13:59:30 -07001845 spin_lock_bh(&port->fcoe_pending_queue.lock);
Robert Love85b4aa42008-12-09 15:10:24 -08001846 }
Chris Leech014f5c32009-08-25 13:59:30 -07001847 spin_unlock_bh(&port->fcoe_pending_queue.lock);
Robert Love85b4aa42008-12-09 15:10:24 -08001848}
Robert Love85b4aa42008-12-09 15:10:24 -08001849
1850/**
Robert Love34f42a02009-02-27 10:55:45 -08001851 * fcoe_reset() - Resets the fcoe
Robert Love85b4aa42008-12-09 15:10:24 -08001852 * @shost: shost the reset is from
1853 *
1854 * Returns: always 0
1855 */
1856int fcoe_reset(struct Scsi_Host *shost)
1857{
1858 struct fc_lport *lport = shost_priv(shost);
1859 fc_lport_reset(lport);
1860 return 0;
1861}
Robert Love85b4aa42008-12-09 15:10:24 -08001862
Robert Love34f42a02009-02-27 10:55:45 -08001863/**
Chris Leech014f5c32009-08-25 13:59:30 -07001864 * fcoe_hostlist_lookup_port() - find the corresponding lport by a given device
Chris Leechdd3fd722009-04-21 16:27:36 -07001865 * @dev: this is currently ptr to net_device
Robert Love85b4aa42008-12-09 15:10:24 -08001866 *
Chris Leech014f5c32009-08-25 13:59:30 -07001867 * Returns: NULL or the located fcoe_port
Chris Leech090eb6c2009-08-25 14:00:28 -07001868 * Locking: must be called with the RNL mutex held
Robert Love85b4aa42008-12-09 15:10:24 -08001869 */
Chris Leech014f5c32009-08-25 13:59:30 -07001870static struct fcoe_interface *
1871fcoe_hostlist_lookup_port(const struct net_device *dev)
Robert Love85b4aa42008-12-09 15:10:24 -08001872{
Chris Leech014f5c32009-08-25 13:59:30 -07001873 struct fcoe_interface *fcoe;
Robert Love85b4aa42008-12-09 15:10:24 -08001874
Chris Leech014f5c32009-08-25 13:59:30 -07001875 list_for_each_entry(fcoe, &fcoe_hostlist, list) {
Chris Leech25024982009-08-25 13:59:35 -07001876 if (fcoe->netdev == dev)
Chris Leech014f5c32009-08-25 13:59:30 -07001877 return fcoe;
Robert Love85b4aa42008-12-09 15:10:24 -08001878 }
Robert Love85b4aa42008-12-09 15:10:24 -08001879 return NULL;
1880}
1881
Robert Love34f42a02009-02-27 10:55:45 -08001882/**
1883 * fcoe_hostlist_lookup() - Find the corresponding lport by netdev
Robert Love85b4aa42008-12-09 15:10:24 -08001884 * @netdev: ptr to net_device
1885 *
1886 * Returns: 0 for success
Chris Leech090eb6c2009-08-25 14:00:28 -07001887 * Locking: must be called with the RTNL mutex held
Robert Love85b4aa42008-12-09 15:10:24 -08001888 */
Chris Leech090eb6c2009-08-25 14:00:28 -07001889static struct fc_lport *fcoe_hostlist_lookup(const struct net_device *netdev)
Robert Love85b4aa42008-12-09 15:10:24 -08001890{
Chris Leech014f5c32009-08-25 13:59:30 -07001891 struct fcoe_interface *fcoe;
Robert Love85b4aa42008-12-09 15:10:24 -08001892
Chris Leech014f5c32009-08-25 13:59:30 -07001893 fcoe = fcoe_hostlist_lookup_port(netdev);
Chris Leech3fe9a0b2009-08-25 13:59:46 -07001894 return (fcoe) ? fcoe->ctlr.lp : NULL;
Robert Love85b4aa42008-12-09 15:10:24 -08001895}
Robert Love85b4aa42008-12-09 15:10:24 -08001896
Robert Love34f42a02009-02-27 10:55:45 -08001897/**
1898 * fcoe_hostlist_add() - Add a lport to lports list
Chris Leechdd3fd722009-04-21 16:27:36 -07001899 * @lp: ptr to the fc_lport to be added
Robert Love85b4aa42008-12-09 15:10:24 -08001900 *
1901 * Returns: 0 for success
Chris Leech090eb6c2009-08-25 14:00:28 -07001902 * Locking: must be called with the RTNL mutex held
Robert Love85b4aa42008-12-09 15:10:24 -08001903 */
Chris Leech090eb6c2009-08-25 14:00:28 -07001904static int fcoe_hostlist_add(const struct fc_lport *lport)
Robert Love85b4aa42008-12-09 15:10:24 -08001905{
Chris Leech014f5c32009-08-25 13:59:30 -07001906 struct fcoe_interface *fcoe;
1907 struct fcoe_port *port;
Robert Love85b4aa42008-12-09 15:10:24 -08001908
Chris Leech014f5c32009-08-25 13:59:30 -07001909 fcoe = fcoe_hostlist_lookup_port(fcoe_netdev(lport));
1910 if (!fcoe) {
1911 port = lport_priv(lport);
1912 fcoe = port->fcoe;
1913 list_add_tail(&fcoe->list, &fcoe_hostlist);
Robert Love85b4aa42008-12-09 15:10:24 -08001914 }
1915 return 0;
1916}
Robert Love85b4aa42008-12-09 15:10:24 -08001917
Robert Love34f42a02009-02-27 10:55:45 -08001918/**
Robert Love34f42a02009-02-27 10:55:45 -08001919 * fcoe_init() - fcoe module loading initialization
Robert Love85b4aa42008-12-09 15:10:24 -08001920 *
Robert Love85b4aa42008-12-09 15:10:24 -08001921 * Returns 0 on success, negative on failure
Robert Love34f42a02009-02-27 10:55:45 -08001922 */
Robert Love85b4aa42008-12-09 15:10:24 -08001923static int __init fcoe_init(void)
1924{
Robert Love38eccab2009-03-17 11:41:30 -07001925 unsigned int cpu;
Robert Love8976f422009-03-17 11:41:46 -07001926 int rc = 0;
Robert Love85b4aa42008-12-09 15:10:24 -08001927 struct fcoe_percpu_s *p;
1928
Chris Leechdfc1d0f2009-08-25 14:00:13 -07001929 mutex_lock(&fcoe_config_mutex);
1930
Robert Love38eccab2009-03-17 11:41:30 -07001931 for_each_possible_cpu(cpu) {
Robert Love5e5e92d2009-03-17 11:41:35 -07001932 p = &per_cpu(fcoe_percpu, cpu);
Robert Love38eccab2009-03-17 11:41:30 -07001933 skb_queue_head_init(&p->fcoe_rx_list);
1934 }
1935
Robert Love8976f422009-03-17 11:41:46 -07001936 for_each_online_cpu(cpu)
1937 fcoe_percpu_thread_create(cpu);
Robert Love85b4aa42008-12-09 15:10:24 -08001938
Robert Love8976f422009-03-17 11:41:46 -07001939 /* Initialize per CPU interrupt thread */
1940 rc = register_hotcpu_notifier(&fcoe_cpu_notifier);
1941 if (rc)
1942 goto out_free;
Robert Love85b4aa42008-12-09 15:10:24 -08001943
Robert Love8976f422009-03-17 11:41:46 -07001944 /* Setup link change notification */
Robert Love85b4aa42008-12-09 15:10:24 -08001945 fcoe_dev_setup();
1946
Chris Leech5892c322009-08-25 13:59:14 -07001947 rc = fcoe_if_init();
1948 if (rc)
1949 goto out_free;
Robert Love85b4aa42008-12-09 15:10:24 -08001950
Chris Leechdfc1d0f2009-08-25 14:00:13 -07001951 mutex_unlock(&fcoe_config_mutex);
Robert Love85b4aa42008-12-09 15:10:24 -08001952 return 0;
Robert Love8976f422009-03-17 11:41:46 -07001953
1954out_free:
1955 for_each_online_cpu(cpu) {
1956 fcoe_percpu_thread_destroy(cpu);
1957 }
Chris Leechdfc1d0f2009-08-25 14:00:13 -07001958 mutex_unlock(&fcoe_config_mutex);
Robert Love8976f422009-03-17 11:41:46 -07001959 return rc;
Robert Love85b4aa42008-12-09 15:10:24 -08001960}
1961module_init(fcoe_init);
1962
1963/**
Robert Love34f42a02009-02-27 10:55:45 -08001964 * fcoe_exit() - fcoe module unloading cleanup
Robert Love85b4aa42008-12-09 15:10:24 -08001965 *
1966 * Returns 0 on success, negative on failure
Robert Love34f42a02009-02-27 10:55:45 -08001967 */
Robert Love85b4aa42008-12-09 15:10:24 -08001968static void __exit fcoe_exit(void)
1969{
Robert Love5e5e92d2009-03-17 11:41:35 -07001970 unsigned int cpu;
Chris Leech014f5c32009-08-25 13:59:30 -07001971 struct fcoe_interface *fcoe, *tmp;
Chris Leech2e70e242009-08-25 14:00:23 -07001972 struct fcoe_port *port;
Robert Love85b4aa42008-12-09 15:10:24 -08001973
Chris Leechdfc1d0f2009-08-25 14:00:13 -07001974 mutex_lock(&fcoe_config_mutex);
1975
Robert Love85b4aa42008-12-09 15:10:24 -08001976 fcoe_dev_cleanup();
1977
Vasu Dev5919a592009-03-27 09:03:29 -07001978 /* releases the associated fcoe hosts */
Chris Leech090eb6c2009-08-25 14:00:28 -07001979 rtnl_lock();
1980 list_for_each_entry_safe(fcoe, tmp, &fcoe_hostlist, list) {
Chris Leechc863df32009-08-25 14:00:18 -07001981 list_del(&fcoe->list);
Chris Leech2e70e242009-08-25 14:00:23 -07001982 port = lport_priv(fcoe->ctlr.lp);
Chris Leech2e70e242009-08-25 14:00:23 -07001983 fcoe_interface_cleanup(fcoe);
Chris Leech2e70e242009-08-25 14:00:23 -07001984 schedule_work(&port->destroy_work);
Chris Leechc863df32009-08-25 14:00:18 -07001985 }
Chris Leech090eb6c2009-08-25 14:00:28 -07001986 rtnl_unlock();
Robert Love85b4aa42008-12-09 15:10:24 -08001987
Robert Love8976f422009-03-17 11:41:46 -07001988 unregister_hotcpu_notifier(&fcoe_cpu_notifier);
1989
Chris Leech014f5c32009-08-25 13:59:30 -07001990 for_each_online_cpu(cpu)
Robert Love8976f422009-03-17 11:41:46 -07001991 fcoe_percpu_thread_destroy(cpu);
Robert Love85b4aa42008-12-09 15:10:24 -08001992
Chris Leechdfc1d0f2009-08-25 14:00:13 -07001993 mutex_unlock(&fcoe_config_mutex);
Chris Leech2e70e242009-08-25 14:00:23 -07001994
1995 /* flush any asyncronous interface destroys,
1996 * this should happen after the netdev notifier is unregistered */
1997 flush_scheduled_work();
1998
1999 /* detach from scsi transport
2000 * must happen after all destroys are done, therefor after the flush */
2001 fcoe_if_exit();
Robert Love85b4aa42008-12-09 15:10:24 -08002002}
2003module_exit(fcoe_exit);