blob: f01b9b44e8aa8a6ab387a969e7b7701400a4d1cf [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>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090029#include <linux/slab.h>
Robert Love85b4aa42008-12-09 15:10:24 -080030#include <linux/cpu.h>
31#include <linux/fs.h>
32#include <linux/sysfs.h>
33#include <linux/ctype.h>
34#include <scsi/scsi_tcq.h>
35#include <scsi/scsicam.h>
36#include <scsi/scsi_transport.h>
37#include <scsi/scsi_transport_fc.h>
38#include <net/rtnetlink.h>
39
40#include <scsi/fc/fc_encaps.h>
Joe Eykholt97c83892009-03-17 11:42:40 -070041#include <scsi/fc/fc_fip.h>
Robert Love85b4aa42008-12-09 15:10:24 -080042
43#include <scsi/libfc.h>
44#include <scsi/fc_frame.h>
45#include <scsi/libfcoe.h>
Robert Love85b4aa42008-12-09 15:10:24 -080046
Vasu Devfdd78022009-03-17 11:42:24 -070047#include "fcoe.h"
48
Robert Love85b4aa42008-12-09 15:10:24 -080049MODULE_AUTHOR("Open-FCoE.org");
50MODULE_DESCRIPTION("FCoE");
Vasu Dev9b34ecf2009-03-17 11:42:13 -070051MODULE_LICENSE("GPL v2");
Robert Love85b4aa42008-12-09 15:10:24 -080052
Yi Zou05cc7392009-08-25 13:59:03 -070053/* Performance tuning parameters for fcoe */
54static unsigned int fcoe_ddp_min;
55module_param_named(ddp_min, fcoe_ddp_min, uint, S_IRUGO | S_IWUSR);
56MODULE_PARM_DESC(ddp_min, "Minimum I/O size in bytes for " \
57 "Direct Data Placement (DDP).");
58
Chris Leechdfc1d0f2009-08-25 14:00:13 -070059DEFINE_MUTEX(fcoe_config_mutex);
60
Joe Eykholte7a51992009-08-25 14:04:08 -070061/* fcoe_percpu_clean completion. Waiter protected by fcoe_create_mutex */
62static DECLARE_COMPLETION(fcoe_flush_completion);
63
Robert Love85b4aa42008-12-09 15:10:24 -080064/* fcoe host list */
Chris Leech090eb6c2009-08-25 14:00:28 -070065/* must only by accessed under the RTNL mutex */
Robert Love85b4aa42008-12-09 15:10:24 -080066LIST_HEAD(fcoe_hostlist);
Robert Love5e5e92d2009-03-17 11:41:35 -070067DEFINE_PER_CPU(struct fcoe_percpu_s, fcoe_percpu);
Robert Love85b4aa42008-12-09 15:10:24 -080068
Chris Leechdd3fd722009-04-21 16:27:36 -070069/* Function Prototypes */
Robert Love1875f272009-11-03 11:47:50 -080070static int fcoe_reset(struct Scsi_Host *);
Vasu Devfdd78022009-03-17 11:42:24 -070071static int fcoe_xmit(struct fc_lport *, struct fc_frame *);
72static int fcoe_rcv(struct sk_buff *, struct net_device *,
73 struct packet_type *, struct net_device *);
Robert Love1875f272009-11-03 11:47:50 -080074static int fcoe_percpu_receive_thread(void *);
75static void fcoe_clean_pending_queue(struct fc_lport *);
76static void fcoe_percpu_clean(struct fc_lport *);
77static int fcoe_link_ok(struct fc_lport *);
Vasu Devfdd78022009-03-17 11:42:24 -070078
79static struct fc_lport *fcoe_hostlist_lookup(const struct net_device *);
80static int fcoe_hostlist_add(const struct fc_lport *);
Vasu Devfdd78022009-03-17 11:42:24 -070081
Vasu Dev4bb6b512009-05-06 10:52:34 -070082static void fcoe_check_wait_queue(struct fc_lport *, struct sk_buff *);
Robert Love85b4aa42008-12-09 15:10:24 -080083static int fcoe_device_notification(struct notifier_block *, ulong, void *);
84static void fcoe_dev_setup(void);
85static void fcoe_dev_cleanup(void);
Robert Love1875f272009-11-03 11:47:50 -080086static struct fcoe_interface
87*fcoe_hostlist_lookup_port(const struct net_device *);
Robert Love85b4aa42008-12-09 15:10:24 -080088
Robert Love1875f272009-11-03 11:47:50 -080089static int fcoe_fip_recv(struct sk_buff *, struct net_device *,
90 struct packet_type *, struct net_device *);
91
92static void fcoe_fip_send(struct fcoe_ctlr *, struct sk_buff *);
93static void fcoe_update_src_mac(struct fc_lport *, u8 *);
94static u8 *fcoe_get_src_mac(struct fc_lport *);
95static void fcoe_destroy_work(struct work_struct *);
96
97static int fcoe_ddp_setup(struct fc_lport *, u16, struct scatterlist *,
98 unsigned int);
99static int fcoe_ddp_done(struct fc_lport *, u16);
100
101static int fcoe_cpu_callback(struct notifier_block *, unsigned long, void *);
102
103static int fcoe_create(const char *, struct kernel_param *);
104static int fcoe_destroy(const char *, struct kernel_param *);
Vasu Dev55a66d32009-12-10 09:59:31 -0800105static int fcoe_enable(const char *, struct kernel_param *);
106static int fcoe_disable(const char *, struct kernel_param *);
Robert Love1875f272009-11-03 11:47:50 -0800107
Robert Love1875f272009-11-03 11:47:50 -0800108static struct fc_seq *fcoe_elsct_send(struct fc_lport *,
109 u32 did, struct fc_frame *,
110 unsigned int op,
111 void (*resp)(struct fc_seq *,
112 struct fc_frame *,
113 void *),
114 void *, u32 timeout);
Chris Leech859b7b62009-11-20 14:54:47 -0800115static void fcoe_recv_frame(struct sk_buff *skb);
Robert Love1875f272009-11-03 11:47:50 -0800116
Yi Zoub84056b2009-11-20 14:55:19 -0800117static void fcoe_get_lesb(struct fc_lport *, struct fc_els_lesb *);
118
Robert Love1875f272009-11-03 11:47:50 -0800119module_param_call(create, fcoe_create, NULL, NULL, S_IWUSR);
120__MODULE_PARM_TYPE(create, "string");
Vasu Dev55a66d32009-12-10 09:59:31 -0800121MODULE_PARM_DESC(create, " Creates fcoe instance on a ethernet interface");
Robert Love1875f272009-11-03 11:47:50 -0800122module_param_call(destroy, fcoe_destroy, NULL, NULL, S_IWUSR);
123__MODULE_PARM_TYPE(destroy, "string");
Vasu Dev55a66d32009-12-10 09:59:31 -0800124MODULE_PARM_DESC(destroy, " Destroys fcoe instance on a ethernet interface");
125module_param_call(enable, fcoe_enable, NULL, NULL, S_IWUSR);
126__MODULE_PARM_TYPE(enable, "string");
127MODULE_PARM_DESC(enable, " Enables fcoe on a ethernet interface.");
128module_param_call(disable, fcoe_disable, NULL, NULL, S_IWUSR);
129__MODULE_PARM_TYPE(disable, "string");
130MODULE_PARM_DESC(disable, " Disables fcoe on a ethernet interface.");
Robert Love1875f272009-11-03 11:47:50 -0800131
132/* notification function for packets from net device */
Robert Love85b4aa42008-12-09 15:10:24 -0800133static struct notifier_block fcoe_notifier = {
134 .notifier_call = fcoe_device_notification,
135};
136
Robert Love1875f272009-11-03 11:47:50 -0800137/* notification function for CPU hotplug events */
138static struct notifier_block fcoe_cpu_notifier = {
139 .notifier_call = fcoe_cpu_callback,
140};
141
Chris Leeche9084bb2009-11-03 11:46:34 -0800142static struct scsi_transport_template *fcoe_transport_template;
143static struct scsi_transport_template *fcoe_vport_transport_template;
Vasu Dev7f349142009-03-27 09:06:31 -0700144
Robert Love1875f272009-11-03 11:47:50 -0800145static int fcoe_vport_destroy(struct fc_vport *);
146static int fcoe_vport_create(struct fc_vport *, bool disabled);
147static int fcoe_vport_disable(struct fc_vport *, bool disable);
148static void fcoe_set_vport_symbolic_name(struct fc_vport *);
149
150static struct libfc_function_template fcoe_libfc_fcn_templ = {
151 .frame_send = fcoe_xmit,
152 .ddp_setup = fcoe_ddp_setup,
153 .ddp_done = fcoe_ddp_done,
154 .elsct_send = fcoe_elsct_send,
Yi Zoub84056b2009-11-20 14:55:19 -0800155 .get_lesb = fcoe_get_lesb,
Robert Love1875f272009-11-03 11:47:50 -0800156};
Chris Leech9a057532009-11-03 11:46:40 -0800157
Vasu Dev7f349142009-03-27 09:06:31 -0700158struct fc_function_template fcoe_transport_function = {
159 .show_host_node_name = 1,
160 .show_host_port_name = 1,
161 .show_host_supported_classes = 1,
162 .show_host_supported_fc4s = 1,
163 .show_host_active_fc4s = 1,
164 .show_host_maxframe_size = 1,
165
166 .show_host_port_id = 1,
167 .show_host_supported_speeds = 1,
168 .get_host_speed = fc_get_host_speed,
169 .show_host_speed = 1,
170 .show_host_port_type = 1,
171 .get_host_port_state = fc_get_host_port_state,
172 .show_host_port_state = 1,
173 .show_host_symbolic_name = 1,
174
175 .dd_fcrport_size = sizeof(struct fc_rport_libfc_priv),
176 .show_rport_maxframe_size = 1,
177 .show_rport_supported_classes = 1,
178
179 .show_host_fabric_name = 1,
180 .show_starget_node_name = 1,
181 .show_starget_port_name = 1,
182 .show_starget_port_id = 1,
183 .set_rport_dev_loss_tmo = fc_set_rport_loss_tmo,
184 .show_rport_dev_loss_tmo = 1,
185 .get_fc_host_stats = fc_get_host_stats,
186 .issue_fc_host_lip = fcoe_reset,
187
188 .terminate_rport_io = fc_rport_terminate_io,
Chris Leech9a057532009-11-03 11:46:40 -0800189
190 .vport_create = fcoe_vport_create,
191 .vport_delete = fcoe_vport_destroy,
192 .vport_disable = fcoe_vport_disable,
Chris Leechdc8596d2009-11-03 11:47:18 -0800193 .set_vport_symbolic_name = fcoe_set_vport_symbolic_name,
Steve Maa51ab392009-11-03 11:47:34 -0800194
195 .bsg_request = fc_lport_bsg_request,
Vasu Dev7f349142009-03-27 09:06:31 -0700196};
197
Chris Leeche9084bb2009-11-03 11:46:34 -0800198struct fc_function_template fcoe_vport_transport_function = {
199 .show_host_node_name = 1,
200 .show_host_port_name = 1,
201 .show_host_supported_classes = 1,
202 .show_host_supported_fc4s = 1,
203 .show_host_active_fc4s = 1,
204 .show_host_maxframe_size = 1,
205
206 .show_host_port_id = 1,
207 .show_host_supported_speeds = 1,
208 .get_host_speed = fc_get_host_speed,
209 .show_host_speed = 1,
210 .show_host_port_type = 1,
211 .get_host_port_state = fc_get_host_port_state,
212 .show_host_port_state = 1,
213 .show_host_symbolic_name = 1,
214
215 .dd_fcrport_size = sizeof(struct fc_rport_libfc_priv),
216 .show_rport_maxframe_size = 1,
217 .show_rport_supported_classes = 1,
218
219 .show_host_fabric_name = 1,
220 .show_starget_node_name = 1,
221 .show_starget_port_name = 1,
222 .show_starget_port_id = 1,
223 .set_rport_dev_loss_tmo = fc_set_rport_loss_tmo,
224 .show_rport_dev_loss_tmo = 1,
225 .get_fc_host_stats = fc_get_host_stats,
226 .issue_fc_host_lip = fcoe_reset,
227
228 .terminate_rport_io = fc_rport_terminate_io,
Steve Maa51ab392009-11-03 11:47:34 -0800229
230 .bsg_request = fc_lport_bsg_request,
Chris Leeche9084bb2009-11-03 11:46:34 -0800231};
232
Vasu Dev7f349142009-03-27 09:06:31 -0700233static struct scsi_host_template fcoe_shost_template = {
234 .module = THIS_MODULE,
235 .name = "FCoE Driver",
236 .proc_name = FCOE_NAME,
237 .queuecommand = fc_queuecommand,
238 .eh_abort_handler = fc_eh_abort,
239 .eh_device_reset_handler = fc_eh_device_reset,
240 .eh_host_reset_handler = fc_eh_host_reset,
241 .slave_alloc = fc_slave_alloc,
242 .change_queue_depth = fc_change_queue_depth,
243 .change_queue_type = fc_change_queue_type,
244 .this_id = -1,
Vasu Dev14caf442009-10-15 17:46:55 -0700245 .cmd_per_lun = 3,
Vasu Dev7f349142009-03-27 09:06:31 -0700246 .can_queue = FCOE_MAX_OUTSTANDING_COMMANDS,
247 .use_clustering = ENABLE_CLUSTERING,
248 .sg_tablesize = SG_ALL,
249 .max_sectors = 0xffff,
250};
251
Chris Leech54b649f2009-08-25 14:00:07 -0700252/**
Robert Love1875f272009-11-03 11:47:50 -0800253 * fcoe_interface_setup() - Setup a FCoE interface
254 * @fcoe: The new FCoE interface
255 * @netdev: The net device that the fcoe interface is on
Chris Leech54b649f2009-08-25 14:00:07 -0700256 *
257 * Returns : 0 for success
Chris Leech2e70e242009-08-25 14:00:23 -0700258 * Locking: must be called with the RTNL mutex held
Chris Leech54b649f2009-08-25 14:00:07 -0700259 */
260static int fcoe_interface_setup(struct fcoe_interface *fcoe,
261 struct net_device *netdev)
262{
263 struct fcoe_ctlr *fip = &fcoe->ctlr;
264 struct netdev_hw_addr *ha;
Yi Zou5bab87e2009-11-03 11:49:43 -0800265 struct net_device *real_dev;
Chris Leech54b649f2009-08-25 14:00:07 -0700266 u8 flogi_maddr[ETH_ALEN];
Yi Zoub7a727f2009-10-21 16:28:03 -0700267 const struct net_device_ops *ops;
Chris Leech54b649f2009-08-25 14:00:07 -0700268
269 fcoe->netdev = netdev;
270
Yi Zoub7a727f2009-10-21 16:28:03 -0700271 /* Let LLD initialize for FCoE */
272 ops = netdev->netdev_ops;
273 if (ops->ndo_fcoe_enable) {
274 if (ops->ndo_fcoe_enable(netdev))
275 FCOE_NETDEV_DBG(netdev, "Failed to enable FCoE"
276 " specific feature for LLD.\n");
277 }
278
Chris Leech54b649f2009-08-25 14:00:07 -0700279 /* Do not support for bonding device */
280 if ((netdev->priv_flags & IFF_MASTER_ALB) ||
281 (netdev->priv_flags & IFF_SLAVE_INACTIVE) ||
282 (netdev->priv_flags & IFF_MASTER_8023AD)) {
john fastabend59d92512009-11-03 11:48:44 -0800283 FCOE_NETDEV_DBG(netdev, "Bonded interfaces not supported\n");
Chris Leech54b649f2009-08-25 14:00:07 -0700284 return -EOPNOTSUPP;
285 }
286
287 /* look for SAN MAC address, if multiple SAN MACs exist, only
288 * use the first one for SPMA */
Yi Zou5bab87e2009-11-03 11:49:43 -0800289 real_dev = (netdev->priv_flags & IFF_802_1Q_VLAN) ?
290 vlan_dev_real_dev(netdev) : netdev;
Chris Leech54b649f2009-08-25 14:00:07 -0700291 rcu_read_lock();
Yi Zou5bab87e2009-11-03 11:49:43 -0800292 for_each_dev_addr(real_dev, ha) {
Chris Leech54b649f2009-08-25 14:00:07 -0700293 if ((ha->type == NETDEV_HW_ADDR_T_SAN) &&
Yi Zoubf361702009-11-03 11:49:38 -0800294 (is_valid_ether_addr(ha->addr))) {
Chris Leech54b649f2009-08-25 14:00:07 -0700295 memcpy(fip->ctl_src_addr, ha->addr, ETH_ALEN);
296 fip->spma = 1;
297 break;
298 }
299 }
300 rcu_read_unlock();
301
302 /* setup Source Mac Address */
303 if (!fip->spma)
304 memcpy(fip->ctl_src_addr, netdev->dev_addr, netdev->addr_len);
305
306 /*
307 * Add FCoE MAC address as second unicast MAC address
308 * or enter promiscuous mode if not capable of listening
309 * for multiple unicast MACs.
310 */
Chris Leech54b649f2009-08-25 14:00:07 -0700311 memcpy(flogi_maddr, (u8[6]) FC_FCOE_FLOGI_MAC, ETH_ALEN);
312 dev_unicast_add(netdev, flogi_maddr);
313 if (fip->spma)
314 dev_unicast_add(netdev, fip->ctl_src_addr);
315 dev_mc_add(netdev, FIP_ALL_ENODE_MACS, ETH_ALEN, 0);
Chris Leech54b649f2009-08-25 14:00:07 -0700316
317 /*
318 * setup the receive function from ethernet driver
319 * on the ethertype for the given device
320 */
321 fcoe->fcoe_packet_type.func = fcoe_rcv;
322 fcoe->fcoe_packet_type.type = __constant_htons(ETH_P_FCOE);
323 fcoe->fcoe_packet_type.dev = netdev;
324 dev_add_pack(&fcoe->fcoe_packet_type);
325
326 fcoe->fip_packet_type.func = fcoe_fip_recv;
327 fcoe->fip_packet_type.type = htons(ETH_P_FIP);
328 fcoe->fip_packet_type.dev = netdev;
329 dev_add_pack(&fcoe->fip_packet_type);
330
331 return 0;
332}
333
Vasu Dev7f349142009-03-27 09:06:31 -0700334/**
Robert Love1875f272009-11-03 11:47:50 -0800335 * fcoe_interface_create() - Create a FCoE interface on a net device
336 * @netdev: The net device to create the FCoE interface on
Chris Leech030f4e02009-08-25 14:00:02 -0700337 *
338 * Returns: pointer to a struct fcoe_interface or NULL on error
339 */
340static struct fcoe_interface *fcoe_interface_create(struct net_device *netdev)
341{
342 struct fcoe_interface *fcoe;
john fastabend59d92512009-11-03 11:48:44 -0800343 int err;
Chris Leech030f4e02009-08-25 14:00:02 -0700344
345 fcoe = kzalloc(sizeof(*fcoe), GFP_KERNEL);
346 if (!fcoe) {
347 FCOE_NETDEV_DBG(netdev, "Could not allocate fcoe structure\n");
348 return NULL;
349 }
350
Chris Leech2e70e242009-08-25 14:00:23 -0700351 dev_hold(netdev);
Chris Leech030f4e02009-08-25 14:00:02 -0700352 kref_init(&fcoe->kref);
Chris Leech54b649f2009-08-25 14:00:07 -0700353
354 /*
355 * Initialize FIP.
356 */
357 fcoe_ctlr_init(&fcoe->ctlr);
358 fcoe->ctlr.send = fcoe_fip_send;
359 fcoe->ctlr.update_mac = fcoe_update_src_mac;
Chris Leech11b56182009-11-03 11:46:29 -0800360 fcoe->ctlr.get_src_addr = fcoe_get_src_mac;
Chris Leech54b649f2009-08-25 14:00:07 -0700361
john fastabend59d92512009-11-03 11:48:44 -0800362 err = fcoe_interface_setup(fcoe, netdev);
363 if (err) {
364 fcoe_ctlr_destroy(&fcoe->ctlr);
365 kfree(fcoe);
366 dev_put(netdev);
367 return NULL;
368 }
Chris Leech030f4e02009-08-25 14:00:02 -0700369
370 return fcoe;
371}
372
373/**
Robert Love1875f272009-11-03 11:47:50 -0800374 * fcoe_interface_cleanup() - Clean up a FCoE interface
375 * @fcoe: The FCoE interface to be cleaned up
Chris Leech2e70e242009-08-25 14:00:23 -0700376 *
377 * Caller must be holding the RTNL mutex
Chris Leech54b649f2009-08-25 14:00:07 -0700378 */
379void fcoe_interface_cleanup(struct fcoe_interface *fcoe)
380{
381 struct net_device *netdev = fcoe->netdev;
382 struct fcoe_ctlr *fip = &fcoe->ctlr;
383 u8 flogi_maddr[ETH_ALEN];
Yi Zoub7a727f2009-10-21 16:28:03 -0700384 const struct net_device_ops *ops;
Chris Leech54b649f2009-08-25 14:00:07 -0700385
386 /*
387 * Don't listen for Ethernet packets anymore.
388 * synchronize_net() ensures that the packet handlers are not running
389 * on another CPU. dev_remove_pack() would do that, this calls the
390 * unsyncronized version __dev_remove_pack() to avoid multiple delays.
391 */
392 __dev_remove_pack(&fcoe->fcoe_packet_type);
393 __dev_remove_pack(&fcoe->fip_packet_type);
394 synchronize_net();
395
Chris Leech54b649f2009-08-25 14:00:07 -0700396 /* Delete secondary MAC addresses */
Chris Leech54b649f2009-08-25 14:00:07 -0700397 memcpy(flogi_maddr, (u8[6]) FC_FCOE_FLOGI_MAC, ETH_ALEN);
398 dev_unicast_delete(netdev, flogi_maddr);
Chris Leech54b649f2009-08-25 14:00:07 -0700399 if (fip->spma)
400 dev_unicast_delete(netdev, fip->ctl_src_addr);
401 dev_mc_delete(netdev, FIP_ALL_ENODE_MACS, ETH_ALEN, 0);
Yi Zoub7a727f2009-10-21 16:28:03 -0700402
403 /* Tell the LLD we are done w/ FCoE */
404 ops = netdev->netdev_ops;
405 if (ops->ndo_fcoe_disable) {
406 if (ops->ndo_fcoe_disable(netdev))
407 FCOE_NETDEV_DBG(netdev, "Failed to disable FCoE"
408 " specific feature for LLD.\n");
409 }
Chris Leech54b649f2009-08-25 14:00:07 -0700410}
411
412/**
Chris Leech030f4e02009-08-25 14:00:02 -0700413 * fcoe_interface_release() - fcoe_port kref release function
Robert Love1875f272009-11-03 11:47:50 -0800414 * @kref: Embedded reference count in an fcoe_interface struct
Chris Leech030f4e02009-08-25 14:00:02 -0700415 */
416static void fcoe_interface_release(struct kref *kref)
417{
418 struct fcoe_interface *fcoe;
Chris Leech2e70e242009-08-25 14:00:23 -0700419 struct net_device *netdev;
Chris Leech030f4e02009-08-25 14:00:02 -0700420
421 fcoe = container_of(kref, struct fcoe_interface, kref);
Chris Leech2e70e242009-08-25 14:00:23 -0700422 netdev = fcoe->netdev;
423 /* tear-down the FCoE controller */
424 fcoe_ctlr_destroy(&fcoe->ctlr);
Chris Leech030f4e02009-08-25 14:00:02 -0700425 kfree(fcoe);
Chris Leech2e70e242009-08-25 14:00:23 -0700426 dev_put(netdev);
Chris Leech030f4e02009-08-25 14:00:02 -0700427}
428
429/**
Robert Love1875f272009-11-03 11:47:50 -0800430 * fcoe_interface_get() - Get a reference to a FCoE interface
431 * @fcoe: The FCoE interface to be held
Chris Leech030f4e02009-08-25 14:00:02 -0700432 */
433static inline void fcoe_interface_get(struct fcoe_interface *fcoe)
434{
435 kref_get(&fcoe->kref);
436}
437
438/**
Robert Love1875f272009-11-03 11:47:50 -0800439 * fcoe_interface_put() - Put a reference to a FCoE interface
440 * @fcoe: The FCoE interface to be released
Chris Leech030f4e02009-08-25 14:00:02 -0700441 */
442static inline void fcoe_interface_put(struct fcoe_interface *fcoe)
443{
444 kref_put(&fcoe->kref, fcoe_interface_release);
445}
446
447/**
Robert Love1875f272009-11-03 11:47:50 -0800448 * fcoe_fip_recv() - Handler for received FIP frames
449 * @skb: The receive skb
450 * @netdev: The associated net device
451 * @ptype: The packet_type structure which was used to register this handler
452 * @orig_dev: The original net_device the the skb was received on.
453 * (in case dev is a bond)
Vasu Devab6b85c2009-05-17 12:33:08 +0000454 *
455 * Returns: 0 for success
456 */
Robert Love1875f272009-11-03 11:47:50 -0800457static int fcoe_fip_recv(struct sk_buff *skb, struct net_device *netdev,
Vasu Devab6b85c2009-05-17 12:33:08 +0000458 struct packet_type *ptype,
459 struct net_device *orig_dev)
460{
Chris Leech259ad852009-08-25 13:59:41 -0700461 struct fcoe_interface *fcoe;
Vasu Devab6b85c2009-05-17 12:33:08 +0000462
Chris Leech259ad852009-08-25 13:59:41 -0700463 fcoe = container_of(ptype, struct fcoe_interface, fip_packet_type);
Chris Leech3fe9a0b2009-08-25 13:59:46 -0700464 fcoe_ctlr_recv(&fcoe->ctlr, skb);
Vasu Devab6b85c2009-05-17 12:33:08 +0000465 return 0;
466}
467
468/**
Robert Love1875f272009-11-03 11:47:50 -0800469 * fcoe_fip_send() - Send an Ethernet-encapsulated FIP frame
470 * @fip: The FCoE controller
471 * @skb: The FIP packet to be sent
Vasu Devab6b85c2009-05-17 12:33:08 +0000472 */
473static void fcoe_fip_send(struct fcoe_ctlr *fip, struct sk_buff *skb)
474{
Chris Leech3fe9a0b2009-08-25 13:59:46 -0700475 skb->dev = fcoe_from_ctlr(fip)->netdev;
Vasu Devab6b85c2009-05-17 12:33:08 +0000476 dev_queue_xmit(skb);
477}
478
479/**
Robert Love1875f272009-11-03 11:47:50 -0800480 * fcoe_update_src_mac() - Update the Ethernet MAC filters
481 * @lport: The local port to update the source MAC on
482 * @addr: Unicast MAC address to add
Vasu Devab6b85c2009-05-17 12:33:08 +0000483 *
484 * Remove any previously-set unicast MAC filter.
485 * Add secondary FCoE MAC address filter for our OUI.
486 */
Chris Leech11b56182009-11-03 11:46:29 -0800487static void fcoe_update_src_mac(struct fc_lport *lport, u8 *addr)
Vasu Devab6b85c2009-05-17 12:33:08 +0000488{
Chris Leech11b56182009-11-03 11:46:29 -0800489 struct fcoe_port *port = lport_priv(lport);
490 struct fcoe_interface *fcoe = port->fcoe;
Vasu Devab6b85c2009-05-17 12:33:08 +0000491
Vasu Devab6b85c2009-05-17 12:33:08 +0000492 rtnl_lock();
Chris Leech11b56182009-11-03 11:46:29 -0800493 if (!is_zero_ether_addr(port->data_src_addr))
494 dev_unicast_delete(fcoe->netdev, port->data_src_addr);
495 if (!is_zero_ether_addr(addr))
496 dev_unicast_add(fcoe->netdev, addr);
497 memcpy(port->data_src_addr, addr, ETH_ALEN);
Vasu Devab6b85c2009-05-17 12:33:08 +0000498 rtnl_unlock();
499}
500
501/**
Chris Leech11b56182009-11-03 11:46:29 -0800502 * fcoe_get_src_mac() - return the Ethernet source address for an lport
503 * @lport: libfc lport
504 */
505static u8 *fcoe_get_src_mac(struct fc_lport *lport)
506{
507 struct fcoe_port *port = lport_priv(lport);
508
509 return port->data_src_addr;
510}
511
512/**
Robert Love1875f272009-11-03 11:47:50 -0800513 * fcoe_lport_config() - Set up a local port
514 * @lport: The local port to be setup
Vasu Dev7f349142009-03-27 09:06:31 -0700515 *
516 * Returns: 0 for success
517 */
Robert Love1875f272009-11-03 11:47:50 -0800518static int fcoe_lport_config(struct fc_lport *lport)
Vasu Dev7f349142009-03-27 09:06:31 -0700519{
Robert Love1875f272009-11-03 11:47:50 -0800520 lport->link_up = 0;
521 lport->qfull = 0;
522 lport->max_retry_count = 3;
523 lport->max_rport_retry_count = 3;
524 lport->e_d_tov = 2 * 1000; /* FC-FS default */
525 lport->r_a_tov = 2 * 2 * 1000;
526 lport->service_params = (FCP_SPPF_INIT_FCN | FCP_SPPF_RD_XRDY_DIS |
527 FCP_SPPF_RETRY | FCP_SPPF_CONF_COMPL);
528 lport->does_npiv = 1;
Vasu Dev7f349142009-03-27 09:06:31 -0700529
Robert Love1875f272009-11-03 11:47:50 -0800530 fc_lport_init_stats(lport);
Vasu Dev7f349142009-03-27 09:06:31 -0700531
532 /* lport fc_lport related configuration */
Robert Love1875f272009-11-03 11:47:50 -0800533 fc_lport_config(lport);
Vasu Dev7f349142009-03-27 09:06:31 -0700534
535 /* offload related configuration */
Robert Love1875f272009-11-03 11:47:50 -0800536 lport->crc_offload = 0;
537 lport->seq_offload = 0;
538 lport->lro_enabled = 0;
539 lport->lro_xid = 0;
540 lport->lso_max = 0;
Vasu Dev7f349142009-03-27 09:06:31 -0700541
542 return 0;
543}
544
545/**
Robert Love1875f272009-11-03 11:47:50 -0800546 * fcoe_queue_timer() - The fcoe queue timer
547 * @lport: The local port
Vasu Dev1047f222009-05-06 10:52:40 -0700548 *
549 * Calls fcoe_check_wait_queue on timeout
Vasu Dev1047f222009-05-06 10:52:40 -0700550 */
Robert Love1875f272009-11-03 11:47:50 -0800551static void fcoe_queue_timer(ulong lport)
Vasu Dev1047f222009-05-06 10:52:40 -0700552{
Robert Love1875f272009-11-03 11:47:50 -0800553 fcoe_check_wait_queue((struct fc_lport *)lport, NULL);
Vasu Dev1047f222009-05-06 10:52:40 -0700554}
555
556/**
Yi Zoudcece412009-11-20 15:22:21 -0800557 * fcoe_get_wwn() - Get the world wide name from LLD if it supports it
558 * @netdev: the associated net device
559 * @wwn: the output WWN
560 * @type: the type of WWN (WWPN or WWNN)
561 *
562 * Returns: 0 for success
563 */
564static int fcoe_get_wwn(struct net_device *netdev, u64 *wwn, int type)
565{
566 const struct net_device_ops *ops = netdev->netdev_ops;
567
568 if (ops->ndo_fcoe_get_wwn)
569 return ops->ndo_fcoe_get_wwn(netdev, wwn, type);
570 return -EINVAL;
571}
572
573/**
Robert Love1875f272009-11-03 11:47:50 -0800574 * fcoe_netdev_config() - Set up net devive for SW FCoE
575 * @lport: The local port that is associated with the net device
576 * @netdev: The associated net device
Vasu Dev7f349142009-03-27 09:06:31 -0700577 *
Robert Love1875f272009-11-03 11:47:50 -0800578 * Must be called after fcoe_lport_config() as it will use local port mutex
Vasu Dev7f349142009-03-27 09:06:31 -0700579 *
Robert Love1875f272009-11-03 11:47:50 -0800580 * Returns: 0 for success
Vasu Dev7f349142009-03-27 09:06:31 -0700581 */
Robert Love1875f272009-11-03 11:47:50 -0800582static int fcoe_netdev_config(struct fc_lport *lport, struct net_device *netdev)
Vasu Dev7f349142009-03-27 09:06:31 -0700583{
584 u32 mfs;
585 u64 wwnn, wwpn;
Chris Leech25024982009-08-25 13:59:35 -0700586 struct fcoe_interface *fcoe;
Chris Leech014f5c32009-08-25 13:59:30 -0700587 struct fcoe_port *port;
Yi Zoucc0136c2009-11-03 11:49:59 -0800588 int vid = 0;
Vasu Dev7f349142009-03-27 09:06:31 -0700589
590 /* Setup lport private data to point to fcoe softc */
Robert Love1875f272009-11-03 11:47:50 -0800591 port = lport_priv(lport);
Chris Leech25024982009-08-25 13:59:35 -0700592 fcoe = port->fcoe;
Vasu Dev7f349142009-03-27 09:06:31 -0700593
594 /*
595 * Determine max frame size based on underlying device and optional
596 * user-configured limit. If the MFS is too low, fcoe_link_ok()
597 * will return 0, so do this first.
598 */
Yi Zou7221d7e2009-10-21 16:27:52 -0700599 mfs = netdev->mtu;
600 if (netdev->features & NETIF_F_FCOE_MTU) {
601 mfs = FCOE_MTU;
602 FCOE_NETDEV_DBG(netdev, "Supports FCOE_MTU of %d bytes\n", mfs);
603 }
604 mfs -= (sizeof(struct fcoe_hdr) + sizeof(struct fcoe_crc_eof));
Robert Love1875f272009-11-03 11:47:50 -0800605 if (fc_set_mfs(lport, mfs))
Vasu Dev7f349142009-03-27 09:06:31 -0700606 return -EINVAL;
607
Vasu Dev7f349142009-03-27 09:06:31 -0700608 /* offload features support */
Vasu Dev1d1b88d2009-07-29 17:05:45 -0700609 if (netdev->features & NETIF_F_SG)
Robert Love1875f272009-11-03 11:47:50 -0800610 lport->sg_supp = 1;
Vasu Dev7f349142009-03-27 09:06:31 -0700611
Vasu Dev7f349142009-03-27 09:06:31 -0700612 if (netdev->features & NETIF_F_FCOE_CRC) {
Robert Love1875f272009-11-03 11:47:50 -0800613 lport->crc_offload = 1;
Robert Loved5488eb2009-06-10 15:30:59 -0700614 FCOE_NETDEV_DBG(netdev, "Supports FCCRC offload\n");
Vasu Dev7f349142009-03-27 09:06:31 -0700615 }
Vasu Dev7f349142009-03-27 09:06:31 -0700616 if (netdev->features & NETIF_F_FSO) {
Robert Love1875f272009-11-03 11:47:50 -0800617 lport->seq_offload = 1;
618 lport->lso_max = netdev->gso_max_size;
Robert Loved5488eb2009-06-10 15:30:59 -0700619 FCOE_NETDEV_DBG(netdev, "Supports LSO for max len 0x%x\n",
Robert Love1875f272009-11-03 11:47:50 -0800620 lport->lso_max);
Vasu Dev7f349142009-03-27 09:06:31 -0700621 }
Vasu Dev7f349142009-03-27 09:06:31 -0700622 if (netdev->fcoe_ddp_xid) {
Robert Love1875f272009-11-03 11:47:50 -0800623 lport->lro_enabled = 1;
624 lport->lro_xid = netdev->fcoe_ddp_xid;
Robert Loved5488eb2009-06-10 15:30:59 -0700625 FCOE_NETDEV_DBG(netdev, "Supports LRO for max xid 0x%x\n",
Robert Love1875f272009-11-03 11:47:50 -0800626 lport->lro_xid);
Vasu Dev7f349142009-03-27 09:06:31 -0700627 }
Chris Leech014f5c32009-08-25 13:59:30 -0700628 skb_queue_head_init(&port->fcoe_pending_queue);
629 port->fcoe_pending_queue_active = 0;
Robert Love1875f272009-11-03 11:47:50 -0800630 setup_timer(&port->timer, fcoe_queue_timer, (unsigned long)lport);
Vasu Dev7f349142009-03-27 09:06:31 -0700631
Robert Love1875f272009-11-03 11:47:50 -0800632 if (!lport->vport) {
Yi Zoucc0136c2009-11-03 11:49:59 -0800633 /*
634 * Use NAA 1&2 (FC-FS Rev. 2.0, Sec. 15) to generate WWNN/WWPN:
635 * For WWNN, we use NAA 1 w/ bit 27-16 of word 0 as 0.
636 * For WWPN, we use NAA 2 w/ bit 27-16 of word 0 from VLAN ID
637 */
638 if (netdev->priv_flags & IFF_802_1Q_VLAN)
639 vid = vlan_dev_vlan_id(netdev);
Yi Zoudcece412009-11-20 15:22:21 -0800640
641 if (fcoe_get_wwn(netdev, &wwnn, NETDEV_FCOE_WWNN))
642 wwnn = fcoe_wwn_from_mac(fcoe->ctlr.ctl_src_addr, 1, 0);
Robert Love1875f272009-11-03 11:47:50 -0800643 fc_set_wwnn(lport, wwnn);
Yi Zoudcece412009-11-20 15:22:21 -0800644 if (fcoe_get_wwn(netdev, &wwpn, NETDEV_FCOE_WWPN))
645 wwpn = fcoe_wwn_from_mac(fcoe->ctlr.ctl_src_addr,
646 2, vid);
Robert Love1875f272009-11-03 11:47:50 -0800647 fc_set_wwpn(lport, wwpn);
Chris Leech9a057532009-11-03 11:46:40 -0800648 }
Vasu Dev7f349142009-03-27 09:06:31 -0700649
Vasu Dev7f349142009-03-27 09:06:31 -0700650 return 0;
651}
652
653/**
Robert Love1875f272009-11-03 11:47:50 -0800654 * fcoe_shost_config() - Set up the SCSI host associated with a local port
655 * @lport: The local port
656 * @shost: The SCSI host to associate with the local port
657 * @dev: The device associated with the SCSI host
Vasu Dev7f349142009-03-27 09:06:31 -0700658 *
659 * Must be called after fcoe_lport_config() and fcoe_netdev_config()
660 *
Robert Love1875f272009-11-03 11:47:50 -0800661 * Returns: 0 for success
Vasu Dev7f349142009-03-27 09:06:31 -0700662 */
Robert Love1875f272009-11-03 11:47:50 -0800663static int fcoe_shost_config(struct fc_lport *lport, struct Scsi_Host *shost,
664 struct device *dev)
Vasu Dev7f349142009-03-27 09:06:31 -0700665{
666 int rc = 0;
667
668 /* lport scsi host config */
Robert Love1875f272009-11-03 11:47:50 -0800669 lport->host->max_lun = FCOE_MAX_LUN;
670 lport->host->max_id = FCOE_MAX_FCP_TARGET;
671 lport->host->max_channel = 0;
672 if (lport->vport)
673 lport->host->transportt = fcoe_vport_transport_template;
Chris Leeche9084bb2009-11-03 11:46:34 -0800674 else
Robert Love1875f272009-11-03 11:47:50 -0800675 lport->host->transportt = fcoe_transport_template;
Vasu Dev7f349142009-03-27 09:06:31 -0700676
677 /* add the new host to the SCSI-ml */
Robert Love1875f272009-11-03 11:47:50 -0800678 rc = scsi_add_host(lport->host, dev);
Vasu Dev7f349142009-03-27 09:06:31 -0700679 if (rc) {
Robert Love1875f272009-11-03 11:47:50 -0800680 FCOE_NETDEV_DBG(fcoe_netdev(lport), "fcoe_shost_config: "
Robert Loved5488eb2009-06-10 15:30:59 -0700681 "error on scsi_add_host\n");
Vasu Dev7f349142009-03-27 09:06:31 -0700682 return rc;
683 }
Chris Leech9a057532009-11-03 11:46:40 -0800684
Robert Love1875f272009-11-03 11:47:50 -0800685 if (!lport->vport)
686 fc_host_max_npiv_vports(lport->host) = USHORT_MAX;
Chris Leech9a057532009-11-03 11:46:40 -0800687
Robert Love1875f272009-11-03 11:47:50 -0800688 snprintf(fc_host_symbolic_name(lport->host), FC_SYMBOLIC_NAME_SIZE,
Chris Leech5baa17c2009-11-03 11:46:56 -0800689 "%s v%s over %s", FCOE_NAME, FCOE_VERSION,
Robert Love1875f272009-11-03 11:47:50 -0800690 fcoe_netdev(lport)->name);
Vasu Dev7f349142009-03-27 09:06:31 -0700691
692 return 0;
693}
694
Robert Love1875f272009-11-03 11:47:50 -0800695/**
696 * fcoe_oem_match() - The match routine for the offloaded exchange manager
697 * @fp: The I/O frame
Vasu Devd7179682009-07-29 17:05:21 -0700698 *
Robert Love1875f272009-11-03 11:47:50 -0800699 * This routine will be associated with an exchange manager (EM). When
700 * the libfc exchange handling code is looking for an EM to use it will
701 * call this routine and pass it the frame that it wishes to send. This
702 * routine will return True if the associated EM is to be used and False
703 * if the echange code should continue looking for an EM.
704 *
705 * The offload EM that this routine is associated with will handle any
706 * packets that are for SCSI read requests.
707 *
708 * Returns: True for read types I/O, otherwise returns false.
Vasu Devd7179682009-07-29 17:05:21 -0700709 */
710bool fcoe_oem_match(struct fc_frame *fp)
711{
Yi Zou05cc7392009-08-25 13:59:03 -0700712 return fc_fcp_is_read(fr_fsp(fp)) &&
713 (fr_fsp(fp)->data_len > fcoe_ddp_min);
Vasu Devd7179682009-07-29 17:05:21 -0700714}
715
Vasu Dev7f349142009-03-27 09:06:31 -0700716/**
Robert Love1875f272009-11-03 11:47:50 -0800717 * fcoe_em_config() - Allocate and configure an exchange manager
718 * @lport: The local port that the new EM will be associated with
Vasu Dev7f349142009-03-27 09:06:31 -0700719 *
Robert Love1875f272009-11-03 11:47:50 -0800720 * Returns: 0 on success
Vasu Dev7f349142009-03-27 09:06:31 -0700721 */
Robert Love1875f272009-11-03 11:47:50 -0800722static inline int fcoe_em_config(struct fc_lport *lport)
Vasu Dev7f349142009-03-27 09:06:31 -0700723{
Robert Love1875f272009-11-03 11:47:50 -0800724 struct fcoe_port *port = lport_priv(lport);
Chris Leech25024982009-08-25 13:59:35 -0700725 struct fcoe_interface *fcoe = port->fcoe;
726 struct fcoe_interface *oldfcoe = NULL;
Vasu Dev1d1b88d2009-07-29 17:05:45 -0700727 struct net_device *old_real_dev, *cur_real_dev;
Vasu Devd7179682009-07-29 17:05:21 -0700728 u16 min_xid = FCOE_MIN_XID;
729 u16 max_xid = FCOE_MAX_XID;
730
731 /*
732 * Check if need to allocate an em instance for
733 * offload exchange ids to be shared across all VN_PORTs/lport.
734 */
Robert Love1875f272009-11-03 11:47:50 -0800735 if (!lport->lro_enabled || !lport->lro_xid ||
736 (lport->lro_xid >= max_xid)) {
737 lport->lro_xid = 0;
Vasu Devd7179682009-07-29 17:05:21 -0700738 goto skip_oem;
739 }
740
741 /*
742 * Reuse existing offload em instance in case
Vasu Dev1d1b88d2009-07-29 17:05:45 -0700743 * it is already allocated on real eth device
Vasu Devd7179682009-07-29 17:05:21 -0700744 */
Chris Leech25024982009-08-25 13:59:35 -0700745 if (fcoe->netdev->priv_flags & IFF_802_1Q_VLAN)
746 cur_real_dev = vlan_dev_real_dev(fcoe->netdev);
Vasu Dev1d1b88d2009-07-29 17:05:45 -0700747 else
Chris Leech25024982009-08-25 13:59:35 -0700748 cur_real_dev = fcoe->netdev;
Vasu Dev1d1b88d2009-07-29 17:05:45 -0700749
Chris Leech25024982009-08-25 13:59:35 -0700750 list_for_each_entry(oldfcoe, &fcoe_hostlist, list) {
Chris Leech25024982009-08-25 13:59:35 -0700751 if (oldfcoe->netdev->priv_flags & IFF_802_1Q_VLAN)
752 old_real_dev = vlan_dev_real_dev(oldfcoe->netdev);
Vasu Dev1d1b88d2009-07-29 17:05:45 -0700753 else
Chris Leech25024982009-08-25 13:59:35 -0700754 old_real_dev = oldfcoe->netdev;
Vasu Dev1d1b88d2009-07-29 17:05:45 -0700755
756 if (cur_real_dev == old_real_dev) {
Chris Leech991cbb62009-08-25 13:59:51 -0700757 fcoe->oem = oldfcoe->oem;
Vasu Devd7179682009-07-29 17:05:21 -0700758 break;
759 }
760 }
761
Chris Leech991cbb62009-08-25 13:59:51 -0700762 if (fcoe->oem) {
Robert Love1875f272009-11-03 11:47:50 -0800763 if (!fc_exch_mgr_add(lport, fcoe->oem, fcoe_oem_match)) {
Vasu Devd7179682009-07-29 17:05:21 -0700764 printk(KERN_ERR "fcoe_em_config: failed to add "
765 "offload em:%p on interface:%s\n",
Chris Leech991cbb62009-08-25 13:59:51 -0700766 fcoe->oem, fcoe->netdev->name);
Vasu Devd7179682009-07-29 17:05:21 -0700767 return -ENOMEM;
768 }
769 } else {
Robert Love1875f272009-11-03 11:47:50 -0800770 fcoe->oem = fc_exch_mgr_alloc(lport, FC_CLASS_3,
771 FCOE_MIN_XID, lport->lro_xid,
772 fcoe_oem_match);
Chris Leech991cbb62009-08-25 13:59:51 -0700773 if (!fcoe->oem) {
Vasu Devd7179682009-07-29 17:05:21 -0700774 printk(KERN_ERR "fcoe_em_config: failed to allocate "
775 "em for offload exches on interface:%s\n",
Chris Leech25024982009-08-25 13:59:35 -0700776 fcoe->netdev->name);
Vasu Devd7179682009-07-29 17:05:21 -0700777 return -ENOMEM;
778 }
779 }
780
781 /*
782 * Exclude offload EM xid range from next EM xid range.
783 */
Robert Love1875f272009-11-03 11:47:50 -0800784 min_xid += lport->lro_xid + 1;
Vasu Devd7179682009-07-29 17:05:21 -0700785
786skip_oem:
Robert Love1875f272009-11-03 11:47:50 -0800787 if (!fc_exch_mgr_alloc(lport, FC_CLASS_3, min_xid, max_xid, NULL)) {
Vasu Devd7179682009-07-29 17:05:21 -0700788 printk(KERN_ERR "fcoe_em_config: failed to "
Chris Leech25024982009-08-25 13:59:35 -0700789 "allocate em on interface %s\n", fcoe->netdev->name);
Vasu Dev7f349142009-03-27 09:06:31 -0700790 return -ENOMEM;
Vasu Devd7179682009-07-29 17:05:21 -0700791 }
Vasu Dev7f349142009-03-27 09:06:31 -0700792
793 return 0;
794}
795
796/**
Robert Love1875f272009-11-03 11:47:50 -0800797 * fcoe_if_destroy() - Tear down a SW FCoE instance
798 * @lport: The local port to be destroyed
Vasu Dev7f349142009-03-27 09:06:31 -0700799 */
Chris Leechaf7f85d2009-08-25 13:59:24 -0700800static void fcoe_if_destroy(struct fc_lport *lport)
Vasu Dev7f349142009-03-27 09:06:31 -0700801{
Chris Leech014f5c32009-08-25 13:59:30 -0700802 struct fcoe_port *port = lport_priv(lport);
803 struct fcoe_interface *fcoe = port->fcoe;
Chris Leech25024982009-08-25 13:59:35 -0700804 struct net_device *netdev = fcoe->netdev;
Vasu Dev7f349142009-03-27 09:06:31 -0700805
Robert Loved5488eb2009-06-10 15:30:59 -0700806 FCOE_NETDEV_DBG(netdev, "Destroying interface\n");
Vasu Dev7f349142009-03-27 09:06:31 -0700807
Vasu Dev7f349142009-03-27 09:06:31 -0700808 /* Logout of the fabric */
Chris Leechaf7f85d2009-08-25 13:59:24 -0700809 fc_fabric_logoff(lport);
Vasu Dev7f349142009-03-27 09:06:31 -0700810
Vasu Dev7f349142009-03-27 09:06:31 -0700811 /* Cleanup the fc_lport */
Chris Leechaf7f85d2009-08-25 13:59:24 -0700812 fc_lport_destroy(lport);
813 fc_fcp_destroy(lport);
Vasu Dev7f349142009-03-27 09:06:31 -0700814
Chris Leech54b649f2009-08-25 14:00:07 -0700815 /* Stop the transmit retry timer */
816 del_timer_sync(&port->timer);
817
818 /* Free existing transmit skbs */
819 fcoe_clean_pending_queue(lport);
820
Chris Leech11b56182009-11-03 11:46:29 -0800821 rtnl_lock();
822 if (!is_zero_ether_addr(port->data_src_addr))
823 dev_unicast_delete(netdev, port->data_src_addr);
824 rtnl_unlock();
825
Chris Leech54b649f2009-08-25 14:00:07 -0700826 /* receives may not be stopped until after this */
827 fcoe_interface_put(fcoe);
828
829 /* Free queued packets for the per-CPU receive threads */
830 fcoe_percpu_clean(lport);
831
Vasu Dev7f349142009-03-27 09:06:31 -0700832 /* Detach from the scsi-ml */
Chris Leechaf7f85d2009-08-25 13:59:24 -0700833 fc_remove_host(lport->host);
834 scsi_remove_host(lport->host);
Vasu Dev7f349142009-03-27 09:06:31 -0700835
836 /* There are no more rports or I/O, free the EM */
Chris Leechaf7f85d2009-08-25 13:59:24 -0700837 fc_exch_mgr_free(lport);
Vasu Dev7f349142009-03-27 09:06:31 -0700838
Vasu Dev7f349142009-03-27 09:06:31 -0700839 /* Free memory used by statistical counters */
Chris Leechaf7f85d2009-08-25 13:59:24 -0700840 fc_lport_free_stats(lport);
Vasu Dev7f349142009-03-27 09:06:31 -0700841
Chris Leech2e70e242009-08-25 14:00:23 -0700842 /* Release the Scsi_Host */
Chris Leechaf7f85d2009-08-25 13:59:24 -0700843 scsi_host_put(lport->host);
Vasu Dev7f349142009-03-27 09:06:31 -0700844}
845
Robert Love1875f272009-11-03 11:47:50 -0800846/**
847 * fcoe_ddp_setup() - Call a LLD's ddp_setup through the net device
848 * @lport: The local port to setup DDP for
849 * @xid: The exchange ID for this DDP transfer
850 * @sgl: The scatterlist describing this transfer
851 * @sgc: The number of sg items
Vasu Dev7f349142009-03-27 09:06:31 -0700852 *
Robert Love1875f272009-11-03 11:47:50 -0800853 * Returns: 0 if the DDP context was not configured
Vasu Dev7f349142009-03-27 09:06:31 -0700854 */
Robert Love1875f272009-11-03 11:47:50 -0800855static int fcoe_ddp_setup(struct fc_lport *lport, u16 xid,
856 struct scatterlist *sgl, unsigned int sgc)
Vasu Dev7f349142009-03-27 09:06:31 -0700857{
Robert Love1875f272009-11-03 11:47:50 -0800858 struct net_device *netdev = fcoe_netdev(lport);
Vasu Dev7f349142009-03-27 09:06:31 -0700859
Robert Love1875f272009-11-03 11:47:50 -0800860 if (netdev->netdev_ops->ndo_fcoe_ddp_setup)
861 return netdev->netdev_ops->ndo_fcoe_ddp_setup(netdev,
862 xid, sgl,
863 sgc);
Vasu Dev7f349142009-03-27 09:06:31 -0700864
865 return 0;
866}
867
Vasu Dev7f349142009-03-27 09:06:31 -0700868/**
Robert Love1875f272009-11-03 11:47:50 -0800869 * fcoe_ddp_done() - Call a LLD's ddp_done through the net device
870 * @lport: The local port to complete DDP on
871 * @xid: The exchange ID for this DDP transfer
Vasu Dev7f349142009-03-27 09:06:31 -0700872 *
Robert Love1875f272009-11-03 11:47:50 -0800873 * Returns: the length of data that have been completed by DDP
874 */
875static int fcoe_ddp_done(struct fc_lport *lport, u16 xid)
876{
877 struct net_device *netdev = fcoe_netdev(lport);
878
879 if (netdev->netdev_ops->ndo_fcoe_ddp_done)
880 return netdev->netdev_ops->ndo_fcoe_ddp_done(netdev, xid);
881 return 0;
882}
883
884/**
885 * fcoe_if_create() - Create a FCoE instance on an interface
886 * @fcoe: The FCoE interface to create a local port on
887 * @parent: The device pointer to be the parent in sysfs for the SCSI host
888 * @npiv: Indicates if the port is a vport or not
Vasu Dev7f349142009-03-27 09:06:31 -0700889 *
Robert Love1875f272009-11-03 11:47:50 -0800890 * Creates a fc_lport instance and a Scsi_Host instance and configure them.
891 *
892 * Returns: The allocated fc_lport or an error pointer
Vasu Dev7f349142009-03-27 09:06:31 -0700893 */
Chris Leech030f4e02009-08-25 14:00:02 -0700894static struct fc_lport *fcoe_if_create(struct fcoe_interface *fcoe,
Chris Leech9a057532009-11-03 11:46:40 -0800895 struct device *parent, int npiv)
Vasu Dev7f349142009-03-27 09:06:31 -0700896{
Robert Love1875f272009-11-03 11:47:50 -0800897 struct net_device *netdev = fcoe->netdev;
Chris Leechaf7f85d2009-08-25 13:59:24 -0700898 struct fc_lport *lport = NULL;
Chris Leech014f5c32009-08-25 13:59:30 -0700899 struct fcoe_port *port;
Vasu Dev7f349142009-03-27 09:06:31 -0700900 struct Scsi_Host *shost;
Robert Love1875f272009-11-03 11:47:50 -0800901 int rc;
Chris Leech9a057532009-11-03 11:46:40 -0800902 /*
903 * parent is only a vport if npiv is 1,
904 * but we'll only use vport in that case so go ahead and set it
905 */
906 struct fc_vport *vport = dev_to_vport(parent);
Vasu Dev7f349142009-03-27 09:06:31 -0700907
Robert Loved5488eb2009-06-10 15:30:59 -0700908 FCOE_NETDEV_DBG(netdev, "Create Interface\n");
Vasu Dev7f349142009-03-27 09:06:31 -0700909
Chris Leech9a057532009-11-03 11:46:40 -0800910 if (!npiv) {
911 lport = libfc_host_alloc(&fcoe_shost_template,
912 sizeof(struct fcoe_port));
913 } else {
914 lport = libfc_vport_create(vport,
915 sizeof(struct fcoe_port));
916 }
Chris Leech86221962009-11-03 11:46:08 -0800917 if (!lport) {
Chris Leech014f5c32009-08-25 13:59:30 -0700918 FCOE_NETDEV_DBG(netdev, "Could not allocate host structure\n");
919 rc = -ENOMEM;
Chris Leech030f4e02009-08-25 14:00:02 -0700920 goto out;
Chris Leech014f5c32009-08-25 13:59:30 -0700921 }
Chris Leech86221962009-11-03 11:46:08 -0800922 shost = lport->host;
Chris Leech014f5c32009-08-25 13:59:30 -0700923 port = lport_priv(lport);
Chris Leech2e70e242009-08-25 14:00:23 -0700924 port->lport = lport;
Chris Leech014f5c32009-08-25 13:59:30 -0700925 port->fcoe = fcoe;
Chris Leech2e70e242009-08-25 14:00:23 -0700926 INIT_WORK(&port->destroy_work, fcoe_destroy_work);
Vasu Dev7f349142009-03-27 09:06:31 -0700927
Robert Love1875f272009-11-03 11:47:50 -0800928 /* configure a fc_lport including the exchange manager */
Chris Leechaf7f85d2009-08-25 13:59:24 -0700929 rc = fcoe_lport_config(lport);
Vasu Dev7f349142009-03-27 09:06:31 -0700930 if (rc) {
Robert Loved5488eb2009-06-10 15:30:59 -0700931 FCOE_NETDEV_DBG(netdev, "Could not configure lport for the "
932 "interface\n");
Vasu Dev7f349142009-03-27 09:06:31 -0700933 goto out_host_put;
934 }
935
Chris Leech9a057532009-11-03 11:46:40 -0800936 if (npiv) {
937 FCOE_NETDEV_DBG(netdev, "Setting vport names, 0x%llX 0x%llX\n",
Robert Love1875f272009-11-03 11:47:50 -0800938 vport->node_name, vport->port_name);
Chris Leech9a057532009-11-03 11:46:40 -0800939 fc_set_wwnn(lport, vport->node_name);
940 fc_set_wwpn(lport, vport->port_name);
941 }
942
Vasu Devab6b85c2009-05-17 12:33:08 +0000943 /* configure lport network properties */
Chris Leechaf7f85d2009-08-25 13:59:24 -0700944 rc = fcoe_netdev_config(lport, netdev);
Vasu Devab6b85c2009-05-17 12:33:08 +0000945 if (rc) {
Robert Loved5488eb2009-06-10 15:30:59 -0700946 FCOE_NETDEV_DBG(netdev, "Could not configure netdev for the "
947 "interface\n");
Chris Leech54b649f2009-08-25 14:00:07 -0700948 goto out_lp_destroy;
Vasu Devab6b85c2009-05-17 12:33:08 +0000949 }
Joe Eykholt97c83892009-03-17 11:42:40 -0700950
Vasu Dev7f349142009-03-27 09:06:31 -0700951 /* configure lport scsi host properties */
Chris Leechaf7f85d2009-08-25 13:59:24 -0700952 rc = fcoe_shost_config(lport, shost, parent);
Vasu Dev7f349142009-03-27 09:06:31 -0700953 if (rc) {
Robert Loved5488eb2009-06-10 15:30:59 -0700954 FCOE_NETDEV_DBG(netdev, "Could not configure shost for the "
955 "interface\n");
Chris Leech54b649f2009-08-25 14:00:07 -0700956 goto out_lp_destroy;
Vasu Dev7f349142009-03-27 09:06:31 -0700957 }
958
Vasu Dev7f349142009-03-27 09:06:31 -0700959 /* Initialize the library */
Chris Leechaf7f85d2009-08-25 13:59:24 -0700960 rc = fcoe_libfc_config(lport, &fcoe_libfc_fcn_templ);
Vasu Dev7f349142009-03-27 09:06:31 -0700961 if (rc) {
Robert Loved5488eb2009-06-10 15:30:59 -0700962 FCOE_NETDEV_DBG(netdev, "Could not configure libfc for the "
963 "interface\n");
Vasu Dev7f349142009-03-27 09:06:31 -0700964 goto out_lp_destroy;
965 }
966
Chris Leech9a057532009-11-03 11:46:40 -0800967 if (!npiv) {
968 /*
969 * fcoe_em_alloc() and fcoe_hostlist_add() both
970 * need to be atomic with respect to other changes to the
971 * hostlist since fcoe_em_alloc() looks for an existing EM
972 * instance on host list updated by fcoe_hostlist_add().
973 *
974 * This is currently handled through the fcoe_config_mutex
975 * begin held.
976 */
Chris Leechc863df32009-08-25 14:00:18 -0700977
Chris Leech9a057532009-11-03 11:46:40 -0800978 /* lport exch manager allocation */
979 rc = fcoe_em_config(lport);
980 if (rc) {
981 FCOE_NETDEV_DBG(netdev, "Could not configure the EM "
Robert Love1875f272009-11-03 11:47:50 -0800982 "for the interface\n");
Chris Leech9a057532009-11-03 11:46:40 -0800983 goto out_lp_destroy;
984 }
Vasu Dev96316092009-07-29 17:05:00 -0700985 }
986
Chris Leech030f4e02009-08-25 14:00:02 -0700987 fcoe_interface_get(fcoe);
Chris Leechaf7f85d2009-08-25 13:59:24 -0700988 return lport;
Vasu Dev7f349142009-03-27 09:06:31 -0700989
990out_lp_destroy:
Chris Leechaf7f85d2009-08-25 13:59:24 -0700991 fc_exch_mgr_free(lport);
Vasu Dev7f349142009-03-27 09:06:31 -0700992out_host_put:
Chris Leechaf7f85d2009-08-25 13:59:24 -0700993 scsi_host_put(lport->host);
994out:
995 return ERR_PTR(rc);
Vasu Dev7f349142009-03-27 09:06:31 -0700996}
997
998/**
Robert Love1875f272009-11-03 11:47:50 -0800999 * fcoe_if_init() - Initialization routine for fcoe.ko
Vasu Dev7f349142009-03-27 09:06:31 -07001000 *
Robert Love1875f272009-11-03 11:47:50 -08001001 * Attaches the SW FCoE transport to the FC transport
1002 *
1003 * Returns: 0 on success
Vasu Dev7f349142009-03-27 09:06:31 -07001004 */
1005static int __init fcoe_if_init(void)
1006{
1007 /* attach to scsi transport */
Chris Leeche9084bb2009-11-03 11:46:34 -08001008 fcoe_transport_template = fc_attach_transport(&fcoe_transport_function);
1009 fcoe_vport_transport_template =
1010 fc_attach_transport(&fcoe_vport_transport_function);
Vasu Dev7f349142009-03-27 09:06:31 -07001011
Chris Leeche9084bb2009-11-03 11:46:34 -08001012 if (!fcoe_transport_template) {
Robert Loved5488eb2009-06-10 15:30:59 -07001013 printk(KERN_ERR "fcoe: Failed to attach to the FC transport\n");
Vasu Dev7f349142009-03-27 09:06:31 -07001014 return -ENODEV;
1015 }
1016
1017 return 0;
1018}
1019
1020/**
Robert Love1875f272009-11-03 11:47:50 -08001021 * fcoe_if_exit() - Tear down fcoe.ko
Vasu Dev7f349142009-03-27 09:06:31 -07001022 *
Robert Love1875f272009-11-03 11:47:50 -08001023 * Detaches the SW FCoE transport from the FC transport
1024 *
1025 * Returns: 0 on success
Vasu Dev7f349142009-03-27 09:06:31 -07001026 */
1027int __exit fcoe_if_exit(void)
1028{
Chris Leeche9084bb2009-11-03 11:46:34 -08001029 fc_release_transport(fcoe_transport_template);
1030 fc_release_transport(fcoe_vport_transport_template);
1031 fcoe_transport_template = NULL;
1032 fcoe_vport_transport_template = NULL;
Vasu Dev7f349142009-03-27 09:06:31 -07001033 return 0;
1034}
1035
Robert Love85b4aa42008-12-09 15:10:24 -08001036/**
Robert Love1875f272009-11-03 11:47:50 -08001037 * fcoe_percpu_thread_create() - Create a receive thread for an online CPU
1038 * @cpu: The CPU index of the CPU to create a receive thread for
Robert Love8976f422009-03-17 11:41:46 -07001039 */
1040static void fcoe_percpu_thread_create(unsigned int cpu)
1041{
1042 struct fcoe_percpu_s *p;
1043 struct task_struct *thread;
1044
1045 p = &per_cpu(fcoe_percpu, cpu);
1046
1047 thread = kthread_create(fcoe_percpu_receive_thread,
1048 (void *)p, "fcoethread/%d", cpu);
1049
Joe Eykholte7a51992009-08-25 14:04:08 -07001050 if (likely(!IS_ERR(thread))) {
Robert Love8976f422009-03-17 11:41:46 -07001051 kthread_bind(thread, cpu);
1052 wake_up_process(thread);
1053
1054 spin_lock_bh(&p->fcoe_rx_list.lock);
1055 p->thread = thread;
1056 spin_unlock_bh(&p->fcoe_rx_list.lock);
1057 }
1058}
1059
1060/**
Robert Love1875f272009-11-03 11:47:50 -08001061 * fcoe_percpu_thread_destroy() - Remove the receive thread of a CPU
1062 * @cpu: The CPU index of the CPU whose receive thread is to be destroyed
Robert Love8976f422009-03-17 11:41:46 -07001063 *
1064 * Destroys a per-CPU Rx thread. Any pending skbs are moved to the
1065 * current CPU's Rx thread. If the thread being destroyed is bound to
1066 * the CPU processing this context the skbs will be freed.
1067 */
1068static void fcoe_percpu_thread_destroy(unsigned int cpu)
1069{
1070 struct fcoe_percpu_s *p;
1071 struct task_struct *thread;
1072 struct page *crc_eof;
1073 struct sk_buff *skb;
1074#ifdef CONFIG_SMP
1075 struct fcoe_percpu_s *p0;
1076 unsigned targ_cpu = smp_processor_id();
1077#endif /* CONFIG_SMP */
1078
Robert Loved5488eb2009-06-10 15:30:59 -07001079 FCOE_DBG("Destroying receive thread for CPU %d\n", cpu);
Robert Love8976f422009-03-17 11:41:46 -07001080
1081 /* Prevent any new skbs from being queued for this CPU. */
1082 p = &per_cpu(fcoe_percpu, cpu);
1083 spin_lock_bh(&p->fcoe_rx_list.lock);
1084 thread = p->thread;
1085 p->thread = NULL;
1086 crc_eof = p->crc_eof_page;
1087 p->crc_eof_page = NULL;
1088 p->crc_eof_offset = 0;
1089 spin_unlock_bh(&p->fcoe_rx_list.lock);
1090
1091#ifdef CONFIG_SMP
1092 /*
1093 * Don't bother moving the skb's if this context is running
1094 * on the same CPU that is having its thread destroyed. This
1095 * can easily happen when the module is removed.
1096 */
1097 if (cpu != targ_cpu) {
1098 p0 = &per_cpu(fcoe_percpu, targ_cpu);
1099 spin_lock_bh(&p0->fcoe_rx_list.lock);
1100 if (p0->thread) {
Robert Loved5488eb2009-06-10 15:30:59 -07001101 FCOE_DBG("Moving frames from CPU %d to CPU %d\n",
1102 cpu, targ_cpu);
Robert Love8976f422009-03-17 11:41:46 -07001103
1104 while ((skb = __skb_dequeue(&p->fcoe_rx_list)) != NULL)
1105 __skb_queue_tail(&p0->fcoe_rx_list, skb);
1106 spin_unlock_bh(&p0->fcoe_rx_list.lock);
1107 } else {
1108 /*
1109 * The targeted CPU is not initialized and cannot accept
Robert Love1875f272009-11-03 11:47:50 -08001110 * new skbs. Unlock the targeted CPU and drop the skbs
Robert Love8976f422009-03-17 11:41:46 -07001111 * on the CPU that is going offline.
1112 */
1113 while ((skb = __skb_dequeue(&p->fcoe_rx_list)) != NULL)
1114 kfree_skb(skb);
1115 spin_unlock_bh(&p0->fcoe_rx_list.lock);
1116 }
1117 } else {
1118 /*
1119 * This scenario occurs when the module is being removed
1120 * and all threads are being destroyed. skbs will continue
1121 * to be shifted from the CPU thread that is being removed
1122 * to the CPU thread associated with the CPU that is processing
1123 * the module removal. Once there is only one CPU Rx thread it
1124 * will reach this case and we will drop all skbs and later
1125 * stop the thread.
1126 */
1127 spin_lock_bh(&p->fcoe_rx_list.lock);
1128 while ((skb = __skb_dequeue(&p->fcoe_rx_list)) != NULL)
1129 kfree_skb(skb);
1130 spin_unlock_bh(&p->fcoe_rx_list.lock);
1131 }
1132#else
1133 /*
Chris Leechdd3fd722009-04-21 16:27:36 -07001134 * This a non-SMP scenario where the singular Rx thread is
Robert Love8976f422009-03-17 11:41:46 -07001135 * being removed. Free all skbs and stop the thread.
1136 */
1137 spin_lock_bh(&p->fcoe_rx_list.lock);
1138 while ((skb = __skb_dequeue(&p->fcoe_rx_list)) != NULL)
1139 kfree_skb(skb);
1140 spin_unlock_bh(&p->fcoe_rx_list.lock);
1141#endif
1142
1143 if (thread)
1144 kthread_stop(thread);
1145
1146 if (crc_eof)
1147 put_page(crc_eof);
1148}
1149
1150/**
Robert Love1875f272009-11-03 11:47:50 -08001151 * fcoe_cpu_callback() - Handler for CPU hotplug events
1152 * @nfb: The callback data block
1153 * @action: The event triggering the callback
1154 * @hcpu: The index of the CPU that the event is for
Robert Love8976f422009-03-17 11:41:46 -07001155 *
Robert Love1875f272009-11-03 11:47:50 -08001156 * This creates or destroys per-CPU data for fcoe
Robert Love8976f422009-03-17 11:41:46 -07001157 *
1158 * Returns NOTIFY_OK always.
1159 */
1160static int fcoe_cpu_callback(struct notifier_block *nfb,
1161 unsigned long action, void *hcpu)
1162{
1163 unsigned cpu = (unsigned long)hcpu;
1164
1165 switch (action) {
1166 case CPU_ONLINE:
1167 case CPU_ONLINE_FROZEN:
Robert Loved5488eb2009-06-10 15:30:59 -07001168 FCOE_DBG("CPU %x online: Create Rx thread\n", cpu);
Robert Love8976f422009-03-17 11:41:46 -07001169 fcoe_percpu_thread_create(cpu);
1170 break;
1171 case CPU_DEAD:
1172 case CPU_DEAD_FROZEN:
Robert Loved5488eb2009-06-10 15:30:59 -07001173 FCOE_DBG("CPU %x offline: Remove Rx thread\n", cpu);
Robert Love8976f422009-03-17 11:41:46 -07001174 fcoe_percpu_thread_destroy(cpu);
1175 break;
1176 default:
1177 break;
1178 }
1179 return NOTIFY_OK;
1180}
1181
Robert Love8976f422009-03-17 11:41:46 -07001182/**
Robert Love1875f272009-11-03 11:47:50 -08001183 * fcoe_rcv() - Receive packets from a net device
1184 * @skb: The received packet
1185 * @netdev: The net device that the packet was received on
1186 * @ptype: The packet type context
1187 * @olddev: The last device net device
Robert Love85b4aa42008-12-09 15:10:24 -08001188 *
Robert Love1875f272009-11-03 11:47:50 -08001189 * This routine is called by NET_RX_SOFTIRQ. It receives a packet, builds a
1190 * FC frame and passes the frame to libfc.
Robert Love85b4aa42008-12-09 15:10:24 -08001191 *
1192 * Returns: 0 for success
Robert Love34f42a02009-02-27 10:55:45 -08001193 */
Robert Love1875f272009-11-03 11:47:50 -08001194int fcoe_rcv(struct sk_buff *skb, struct net_device *netdev,
Robert Love85b4aa42008-12-09 15:10:24 -08001195 struct packet_type *ptype, struct net_device *olddev)
1196{
Robert Love1875f272009-11-03 11:47:50 -08001197 struct fc_lport *lport;
Robert Love85b4aa42008-12-09 15:10:24 -08001198 struct fcoe_rcv_info *fr;
Chris Leech259ad852009-08-25 13:59:41 -07001199 struct fcoe_interface *fcoe;
Robert Love85b4aa42008-12-09 15:10:24 -08001200 struct fc_frame_header *fh;
Robert Love85b4aa42008-12-09 15:10:24 -08001201 struct fcoe_percpu_s *fps;
Vasu Devb2f00912009-08-25 13:58:53 -07001202 unsigned int cpu;
Robert Love85b4aa42008-12-09 15:10:24 -08001203
Chris Leech259ad852009-08-25 13:59:41 -07001204 fcoe = container_of(ptype, struct fcoe_interface, fcoe_packet_type);
Robert Love1875f272009-11-03 11:47:50 -08001205 lport = fcoe->ctlr.lp;
1206 if (unlikely(!lport)) {
1207 FCOE_NETDEV_DBG(netdev, "Cannot find hba structure");
Robert Love85b4aa42008-12-09 15:10:24 -08001208 goto err2;
1209 }
Robert Love1875f272009-11-03 11:47:50 -08001210 if (!lport->link_up)
Joe Eykholt97c83892009-03-17 11:42:40 -07001211 goto err2;
Robert Love85b4aa42008-12-09 15:10:24 -08001212
Robert Love1875f272009-11-03 11:47:50 -08001213 FCOE_NETDEV_DBG(netdev, "skb_info: len:%d data_len:%d head:%p "
Robert Loved5488eb2009-06-10 15:30:59 -07001214 "data:%p tail:%p end:%p sum:%d dev:%s",
1215 skb->len, skb->data_len, skb->head, skb->data,
1216 skb_tail_pointer(skb), skb_end_pointer(skb),
1217 skb->csum, skb->dev ? skb->dev->name : "<NULL>");
Robert Love85b4aa42008-12-09 15:10:24 -08001218
1219 /* check for FCOE packet type */
1220 if (unlikely(eth_hdr(skb)->h_proto != htons(ETH_P_FCOE))) {
Robert Love1875f272009-11-03 11:47:50 -08001221 FCOE_NETDEV_DBG(netdev, "Wrong FC type frame");
Robert Love85b4aa42008-12-09 15:10:24 -08001222 goto err;
1223 }
1224
1225 /*
1226 * Check for minimum frame length, and make sure required FCoE
1227 * and FC headers are pulled into the linear data area.
1228 */
1229 if (unlikely((skb->len < FCOE_MIN_FRAME) ||
Robert Love1875f272009-11-03 11:47:50 -08001230 !pskb_may_pull(skb, FCOE_HEADER_LEN)))
Robert Love85b4aa42008-12-09 15:10:24 -08001231 goto err;
1232
1233 skb_set_transport_header(skb, sizeof(struct fcoe_hdr));
1234 fh = (struct fc_frame_header *) skb_transport_header(skb);
1235
Robert Love85b4aa42008-12-09 15:10:24 -08001236 fr = fcoe_dev_from_skb(skb);
Robert Love1875f272009-11-03 11:47:50 -08001237 fr->fr_dev = lport;
Robert Love85b4aa42008-12-09 15:10:24 -08001238 fr->ptype = ptype;
Robert Love5e5e92d2009-03-17 11:41:35 -07001239
Robert Love85b4aa42008-12-09 15:10:24 -08001240 /*
Vasu Devb2f00912009-08-25 13:58:53 -07001241 * In case the incoming frame's exchange is originated from
1242 * the initiator, then received frame's exchange id is ANDed
1243 * with fc_cpu_mask bits to get the same cpu on which exchange
1244 * was originated, otherwise just use the current cpu.
Robert Love85b4aa42008-12-09 15:10:24 -08001245 */
Vasu Devb2f00912009-08-25 13:58:53 -07001246 if (ntoh24(fh->fh_f_ctl) & FC_FC_EX_CTX)
1247 cpu = ntohs(fh->fh_ox_id) & fc_cpu_mask;
1248 else
1249 cpu = smp_processor_id();
Robert Love5e5e92d2009-03-17 11:41:35 -07001250
Robert Love8976f422009-03-17 11:41:46 -07001251 fps = &per_cpu(fcoe_percpu, cpu);
Robert Love85b4aa42008-12-09 15:10:24 -08001252 spin_lock_bh(&fps->fcoe_rx_list.lock);
Robert Love8976f422009-03-17 11:41:46 -07001253 if (unlikely(!fps->thread)) {
1254 /*
1255 * The targeted CPU is not ready, let's target
1256 * the first CPU now. For non-SMP systems this
1257 * will check the same CPU twice.
1258 */
Robert Love1875f272009-11-03 11:47:50 -08001259 FCOE_NETDEV_DBG(netdev, "CPU is online, but no receive thread "
Robert Loved5488eb2009-06-10 15:30:59 -07001260 "ready for incoming skb- using first online "
1261 "CPU.\n");
Robert Love8976f422009-03-17 11:41:46 -07001262
1263 spin_unlock_bh(&fps->fcoe_rx_list.lock);
Rusty Russell69571772009-12-17 11:43:14 -06001264 cpu = cpumask_first(cpu_online_mask);
Robert Love8976f422009-03-17 11:41:46 -07001265 fps = &per_cpu(fcoe_percpu, cpu);
1266 spin_lock_bh(&fps->fcoe_rx_list.lock);
1267 if (!fps->thread) {
1268 spin_unlock_bh(&fps->fcoe_rx_list.lock);
1269 goto err;
1270 }
1271 }
1272
1273 /*
1274 * We now have a valid CPU that we're targeting for
1275 * this skb. We also have this receive thread locked,
1276 * so we're free to queue skbs into it's queue.
1277 */
Robert Love85b4aa42008-12-09 15:10:24 -08001278
Chris Leech859b7b62009-11-20 14:54:47 -08001279 /* If this is a SCSI-FCP frame, and this is already executing on the
1280 * correct CPU, and the queue for this CPU is empty, then go ahead
1281 * and process the frame directly in the softirq context.
1282 * This lets us process completions without context switching from the
1283 * NET_RX softirq, to our receive processing thread, and then back to
1284 * BLOCK softirq context.
1285 */
1286 if (fh->fh_type == FC_TYPE_FCP &&
1287 cpu == smp_processor_id() &&
1288 skb_queue_empty(&fps->fcoe_rx_list)) {
1289 spin_unlock_bh(&fps->fcoe_rx_list.lock);
1290 fcoe_recv_frame(skb);
1291 } else {
1292 __skb_queue_tail(&fps->fcoe_rx_list, skb);
1293 if (fps->fcoe_rx_list.qlen == 1)
1294 wake_up_process(fps->thread);
1295 spin_unlock_bh(&fps->fcoe_rx_list.lock);
1296 }
Robert Love85b4aa42008-12-09 15:10:24 -08001297
1298 return 0;
1299err:
Robert Love1875f272009-11-03 11:47:50 -08001300 fc_lport_get_stats(lport)->ErrorFrames++;
Robert Love85b4aa42008-12-09 15:10:24 -08001301
1302err2:
1303 kfree_skb(skb);
1304 return -1;
1305}
Robert Love85b4aa42008-12-09 15:10:24 -08001306
1307/**
Robert Love1875f272009-11-03 11:47:50 -08001308 * fcoe_start_io() - Start FCoE I/O
1309 * @skb: The packet to be transmitted
1310 *
1311 * This routine is called from the net device to start transmitting
1312 * FCoE packets.
Robert Love85b4aa42008-12-09 15:10:24 -08001313 *
1314 * Returns: 0 for success
Robert Love34f42a02009-02-27 10:55:45 -08001315 */
Robert Love85b4aa42008-12-09 15:10:24 -08001316static inline int fcoe_start_io(struct sk_buff *skb)
1317{
Chris Leech18fa11e2009-11-03 11:50:05 -08001318 struct sk_buff *nskb;
Robert Love85b4aa42008-12-09 15:10:24 -08001319 int rc;
1320
Chris Leech18fa11e2009-11-03 11:50:05 -08001321 nskb = skb_clone(skb, GFP_ATOMIC);
1322 rc = dev_queue_xmit(nskb);
Robert Love85b4aa42008-12-09 15:10:24 -08001323 if (rc != 0)
1324 return rc;
1325 kfree_skb(skb);
1326 return 0;
1327}
1328
1329/**
Robert Love1875f272009-11-03 11:47:50 -08001330 * fcoe_get_paged_crc_eof() - Allocate a page to be used for the trailer CRC
1331 * @skb: The packet to be transmitted
1332 * @tlen: The total length of the trailer
1333 *
1334 * This routine allocates a page for frame trailers. The page is re-used if
1335 * there is enough room left on it for the current trailer. If there isn't
1336 * enough buffer left a new page is allocated for the trailer. Reference to
1337 * the page from this function as well as the skbs using the page fragments
1338 * ensure that the page is freed at the appropriate time.
Robert Love85b4aa42008-12-09 15:10:24 -08001339 *
1340 * Returns: 0 for success
Robert Love34f42a02009-02-27 10:55:45 -08001341 */
Robert Love85b4aa42008-12-09 15:10:24 -08001342static int fcoe_get_paged_crc_eof(struct sk_buff *skb, int tlen)
1343{
1344 struct fcoe_percpu_s *fps;
1345 struct page *page;
Robert Love85b4aa42008-12-09 15:10:24 -08001346
Robert Love5e5e92d2009-03-17 11:41:35 -07001347 fps = &get_cpu_var(fcoe_percpu);
Robert Love85b4aa42008-12-09 15:10:24 -08001348 page = fps->crc_eof_page;
1349 if (!page) {
1350 page = alloc_page(GFP_ATOMIC);
1351 if (!page) {
Robert Love5e5e92d2009-03-17 11:41:35 -07001352 put_cpu_var(fcoe_percpu);
Robert Love85b4aa42008-12-09 15:10:24 -08001353 return -ENOMEM;
1354 }
1355 fps->crc_eof_page = page;
Robert Love8976f422009-03-17 11:41:46 -07001356 fps->crc_eof_offset = 0;
Robert Love85b4aa42008-12-09 15:10:24 -08001357 }
1358
1359 get_page(page);
1360 skb_fill_page_desc(skb, skb_shinfo(skb)->nr_frags, page,
1361 fps->crc_eof_offset, tlen);
1362 skb->len += tlen;
1363 skb->data_len += tlen;
1364 skb->truesize += tlen;
1365 fps->crc_eof_offset += sizeof(struct fcoe_crc_eof);
1366
1367 if (fps->crc_eof_offset >= PAGE_SIZE) {
1368 fps->crc_eof_page = NULL;
1369 fps->crc_eof_offset = 0;
1370 put_page(page);
1371 }
Robert Love5e5e92d2009-03-17 11:41:35 -07001372 put_cpu_var(fcoe_percpu);
Robert Love85b4aa42008-12-09 15:10:24 -08001373 return 0;
1374}
1375
1376/**
Robert Love1875f272009-11-03 11:47:50 -08001377 * fcoe_fc_crc() - Calculates the CRC for a given frame
1378 * @fp: The frame to be checksumed
Robert Love85b4aa42008-12-09 15:10:24 -08001379 *
Robert Love1875f272009-11-03 11:47:50 -08001380 * This uses crc32() routine to calculate the CRC for a frame
1381 *
1382 * Return: The 32 bit CRC value
Robert Love34f42a02009-02-27 10:55:45 -08001383 */
Robert Love85b4aa42008-12-09 15:10:24 -08001384u32 fcoe_fc_crc(struct fc_frame *fp)
1385{
1386 struct sk_buff *skb = fp_skb(fp);
1387 struct skb_frag_struct *frag;
1388 unsigned char *data;
1389 unsigned long off, len, clen;
1390 u32 crc;
1391 unsigned i;
1392
1393 crc = crc32(~0, skb->data, skb_headlen(skb));
1394
1395 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
1396 frag = &skb_shinfo(skb)->frags[i];
1397 off = frag->page_offset;
1398 len = frag->size;
1399 while (len > 0) {
1400 clen = min(len, PAGE_SIZE - (off & ~PAGE_MASK));
1401 data = kmap_atomic(frag->page + (off >> PAGE_SHIFT),
1402 KM_SKB_DATA_SOFTIRQ);
1403 crc = crc32(crc, data + (off & ~PAGE_MASK), clen);
1404 kunmap_atomic(data, KM_SKB_DATA_SOFTIRQ);
1405 off += clen;
1406 len -= clen;
1407 }
1408 }
1409 return crc;
1410}
Robert Love85b4aa42008-12-09 15:10:24 -08001411
1412/**
Robert Love1875f272009-11-03 11:47:50 -08001413 * fcoe_xmit() - Transmit a FCoE frame
1414 * @lport: The local port that the frame is to be transmitted for
1415 * @fp: The frame to be transmitted
Robert Love85b4aa42008-12-09 15:10:24 -08001416 *
Robert Love1875f272009-11-03 11:47:50 -08001417 * Return: 0 for success
Robert Love34f42a02009-02-27 10:55:45 -08001418 */
Robert Love1875f272009-11-03 11:47:50 -08001419int fcoe_xmit(struct fc_lport *lport, struct fc_frame *fp)
Robert Love85b4aa42008-12-09 15:10:24 -08001420{
Vasu Dev4bb6b512009-05-06 10:52:34 -07001421 int wlen;
Robert Love85b4aa42008-12-09 15:10:24 -08001422 u32 crc;
1423 struct ethhdr *eh;
1424 struct fcoe_crc_eof *cp;
1425 struct sk_buff *skb;
1426 struct fcoe_dev_stats *stats;
1427 struct fc_frame_header *fh;
1428 unsigned int hlen; /* header length implies the version */
1429 unsigned int tlen; /* trailer length */
1430 unsigned int elen; /* eth header, may include vlan */
Robert Love1875f272009-11-03 11:47:50 -08001431 struct fcoe_port *port = lport_priv(lport);
Chris Leech3fe9a0b2009-08-25 13:59:46 -07001432 struct fcoe_interface *fcoe = port->fcoe;
Robert Love85b4aa42008-12-09 15:10:24 -08001433 u8 sof, eof;
1434 struct fcoe_hdr *hp;
1435
1436 WARN_ON((fr_len(fp) % sizeof(u32)) != 0);
1437
Robert Love85b4aa42008-12-09 15:10:24 -08001438 fh = fc_frame_header_get(fp);
Joe Eykholt97c83892009-03-17 11:42:40 -07001439 skb = fp_skb(fp);
1440 wlen = skb->len / FCOE_WORD_TO_BYTE;
1441
Robert Love1875f272009-11-03 11:47:50 -08001442 if (!lport->link_up) {
Dan Carpenter3caf02e2009-04-21 16:27:25 -07001443 kfree_skb(skb);
Joe Eykholt97c83892009-03-17 11:42:40 -07001444 return 0;
Robert Love85b4aa42008-12-09 15:10:24 -08001445 }
1446
Joe Eykholt97c83892009-03-17 11:42:40 -07001447 if (unlikely(fh->fh_r_ctl == FC_RCTL_ELS_REQ) &&
Robert Love1875f272009-11-03 11:47:50 -08001448 fcoe_ctlr_els_send(&fcoe->ctlr, lport, skb))
Joe Eykholt97c83892009-03-17 11:42:40 -07001449 return 0;
1450
Robert Love85b4aa42008-12-09 15:10:24 -08001451 sof = fr_sof(fp);
1452 eof = fr_eof(fp);
1453
Vasu Dev4e57e1c2009-05-06 10:52:46 -07001454 elen = sizeof(struct ethhdr);
Robert Love85b4aa42008-12-09 15:10:24 -08001455 hlen = sizeof(struct fcoe_hdr);
1456 tlen = sizeof(struct fcoe_crc_eof);
1457 wlen = (skb->len - tlen + sizeof(crc)) / FCOE_WORD_TO_BYTE;
1458
1459 /* crc offload */
Robert Love1875f272009-11-03 11:47:50 -08001460 if (likely(lport->crc_offload)) {
Yi Zou39ca9a02009-02-27 14:07:15 -08001461 skb->ip_summed = CHECKSUM_PARTIAL;
Robert Love85b4aa42008-12-09 15:10:24 -08001462 skb->csum_start = skb_headroom(skb);
1463 skb->csum_offset = skb->len;
1464 crc = 0;
1465 } else {
1466 skb->ip_summed = CHECKSUM_NONE;
1467 crc = fcoe_fc_crc(fp);
1468 }
1469
Chris Leech014f5c32009-08-25 13:59:30 -07001470 /* copy port crc and eof to the skb buff */
Robert Love85b4aa42008-12-09 15:10:24 -08001471 if (skb_is_nonlinear(skb)) {
1472 skb_frag_t *frag;
1473 if (fcoe_get_paged_crc_eof(skb, tlen)) {
Roel Kluine9041582009-02-27 10:56:22 -08001474 kfree_skb(skb);
Robert Love85b4aa42008-12-09 15:10:24 -08001475 return -ENOMEM;
1476 }
1477 frag = &skb_shinfo(skb)->frags[skb_shinfo(skb)->nr_frags - 1];
1478 cp = kmap_atomic(frag->page, KM_SKB_DATA_SOFTIRQ)
1479 + frag->page_offset;
1480 } else {
1481 cp = (struct fcoe_crc_eof *)skb_put(skb, tlen);
1482 }
1483
1484 memset(cp, 0, sizeof(*cp));
1485 cp->fcoe_eof = eof;
1486 cp->fcoe_crc32 = cpu_to_le32(~crc);
1487
1488 if (skb_is_nonlinear(skb)) {
1489 kunmap_atomic(cp, KM_SKB_DATA_SOFTIRQ);
1490 cp = NULL;
1491 }
1492
Chris Leech014f5c32009-08-25 13:59:30 -07001493 /* adjust skb network/transport offsets to match mac/fcoe/port */
Robert Love85b4aa42008-12-09 15:10:24 -08001494 skb_push(skb, elen + hlen);
1495 skb_reset_mac_header(skb);
1496 skb_reset_network_header(skb);
1497 skb->mac_len = elen;
Yi Zou211c7382009-02-27 14:06:37 -08001498 skb->protocol = htons(ETH_P_FCOE);
Chris Leech3fe9a0b2009-08-25 13:59:46 -07001499 skb->dev = fcoe->netdev;
Robert Love85b4aa42008-12-09 15:10:24 -08001500
1501 /* fill up mac and fcoe headers */
1502 eh = eth_hdr(skb);
1503 eh->h_proto = htons(ETH_P_FCOE);
Chris Leech3fe9a0b2009-08-25 13:59:46 -07001504 if (fcoe->ctlr.map_dest)
Robert Love85b4aa42008-12-09 15:10:24 -08001505 fc_fcoe_set_mac(eh->h_dest, fh->fh_d_id);
1506 else
1507 /* insert GW address */
Chris Leech3fe9a0b2009-08-25 13:59:46 -07001508 memcpy(eh->h_dest, fcoe->ctlr.dest_addr, ETH_ALEN);
Robert Love85b4aa42008-12-09 15:10:24 -08001509
Chris Leech3fe9a0b2009-08-25 13:59:46 -07001510 if (unlikely(fcoe->ctlr.flogi_oxid != FC_XID_UNKNOWN))
1511 memcpy(eh->h_source, fcoe->ctlr.ctl_src_addr, ETH_ALEN);
Robert Love85b4aa42008-12-09 15:10:24 -08001512 else
Chris Leech11b56182009-11-03 11:46:29 -08001513 memcpy(eh->h_source, port->data_src_addr, ETH_ALEN);
Robert Love85b4aa42008-12-09 15:10:24 -08001514
1515 hp = (struct fcoe_hdr *)(eh + 1);
1516 memset(hp, 0, sizeof(*hp));
1517 if (FC_FCOE_VER)
1518 FC_FCOE_ENCAPS_VER(hp, FC_FCOE_VER);
1519 hp->fcoe_sof = sof;
1520
Yi Zou39ca9a02009-02-27 14:07:15 -08001521 /* fcoe lso, mss is in max_payload which is non-zero for FCP data */
Robert Love1875f272009-11-03 11:47:50 -08001522 if (lport->seq_offload && fr_max_payload(fp)) {
Yi Zou39ca9a02009-02-27 14:07:15 -08001523 skb_shinfo(skb)->gso_type = SKB_GSO_FCOE;
1524 skb_shinfo(skb)->gso_size = fr_max_payload(fp);
1525 } else {
1526 skb_shinfo(skb)->gso_type = 0;
1527 skb_shinfo(skb)->gso_size = 0;
1528 }
Robert Love85b4aa42008-12-09 15:10:24 -08001529 /* update tx stats: regardless if LLD fails */
Robert Love1875f272009-11-03 11:47:50 -08001530 stats = fc_lport_get_stats(lport);
Robert Love582b45b2009-03-31 15:51:50 -07001531 stats->TxFrames++;
1532 stats->TxWords += wlen;
Robert Love85b4aa42008-12-09 15:10:24 -08001533
1534 /* send down to lld */
Robert Love1875f272009-11-03 11:47:50 -08001535 fr_dev(fp) = lport;
Chris Leech014f5c32009-08-25 13:59:30 -07001536 if (port->fcoe_pending_queue.qlen)
Robert Love1875f272009-11-03 11:47:50 -08001537 fcoe_check_wait_queue(lport, skb);
Vasu Dev4bb6b512009-05-06 10:52:34 -07001538 else if (fcoe_start_io(skb))
Robert Love1875f272009-11-03 11:47:50 -08001539 fcoe_check_wait_queue(lport, skb);
Robert Love85b4aa42008-12-09 15:10:24 -08001540
1541 return 0;
1542}
Robert Love85b4aa42008-12-09 15:10:24 -08001543
Robert Love34f42a02009-02-27 10:55:45 -08001544/**
Robert Love1875f272009-11-03 11:47:50 -08001545 * fcoe_percpu_flush_done() - Indicate per-CPU queue flush completion
1546 * @skb: The completed skb (argument required by destructor)
Joe Eykholte7a51992009-08-25 14:04:08 -07001547 */
1548static void fcoe_percpu_flush_done(struct sk_buff *skb)
1549{
1550 complete(&fcoe_flush_completion);
1551}
1552
1553/**
Chris Leech859b7b62009-11-20 14:54:47 -08001554 * fcoe_recv_frame() - process a single received frame
1555 * @skb: frame to process
1556 */
1557static void fcoe_recv_frame(struct sk_buff *skb)
1558{
1559 u32 fr_len;
1560 struct fc_lport *lport;
1561 struct fcoe_rcv_info *fr;
1562 struct fcoe_dev_stats *stats;
1563 struct fc_frame_header *fh;
1564 struct fcoe_crc_eof crc_eof;
1565 struct fc_frame *fp;
1566 u8 *mac = NULL;
1567 struct fcoe_port *port;
1568 struct fcoe_hdr *hp;
1569
1570 fr = fcoe_dev_from_skb(skb);
1571 lport = fr->fr_dev;
1572 if (unlikely(!lport)) {
1573 if (skb->destructor != fcoe_percpu_flush_done)
1574 FCOE_NETDEV_DBG(skb->dev, "NULL lport in skb");
1575 kfree_skb(skb);
1576 return;
1577 }
1578
1579 FCOE_NETDEV_DBG(skb->dev, "skb_info: len:%d data_len:%d "
1580 "head:%p data:%p tail:%p end:%p sum:%d dev:%s",
1581 skb->len, skb->data_len,
1582 skb->head, skb->data, skb_tail_pointer(skb),
1583 skb_end_pointer(skb), skb->csum,
1584 skb->dev ? skb->dev->name : "<NULL>");
1585
1586 /*
1587 * Save source MAC address before discarding header.
1588 */
1589 port = lport_priv(lport);
1590 if (skb_is_nonlinear(skb))
1591 skb_linearize(skb); /* not ideal */
1592 mac = eth_hdr(skb)->h_source;
1593
1594 /*
1595 * Frame length checks and setting up the header pointers
1596 * was done in fcoe_rcv already.
1597 */
1598 hp = (struct fcoe_hdr *) skb_network_header(skb);
1599 fh = (struct fc_frame_header *) skb_transport_header(skb);
1600
1601 stats = fc_lport_get_stats(lport);
1602 if (unlikely(FC_FCOE_DECAPS_VER(hp) != FC_FCOE_VER)) {
1603 if (stats->ErrorFrames < 5)
1604 printk(KERN_WARNING "fcoe: FCoE version "
1605 "mismatch: The frame has "
1606 "version %x, but the "
1607 "initiator supports version "
1608 "%x\n", FC_FCOE_DECAPS_VER(hp),
1609 FC_FCOE_VER);
1610 stats->ErrorFrames++;
1611 kfree_skb(skb);
1612 return;
1613 }
1614
1615 skb_pull(skb, sizeof(struct fcoe_hdr));
1616 fr_len = skb->len - sizeof(struct fcoe_crc_eof);
1617
1618 stats->RxFrames++;
1619 stats->RxWords += fr_len / FCOE_WORD_TO_BYTE;
1620
1621 fp = (struct fc_frame *)skb;
1622 fc_frame_init(fp);
1623 fr_dev(fp) = lport;
1624 fr_sof(fp) = hp->fcoe_sof;
1625
1626 /* Copy out the CRC and EOF trailer for access */
1627 if (skb_copy_bits(skb, fr_len, &crc_eof, sizeof(crc_eof))) {
1628 kfree_skb(skb);
1629 return;
1630 }
1631 fr_eof(fp) = crc_eof.fcoe_eof;
1632 fr_crc(fp) = crc_eof.fcoe_crc32;
1633 if (pskb_trim(skb, fr_len)) {
1634 kfree_skb(skb);
1635 return;
1636 }
1637
1638 /*
1639 * We only check CRC if no offload is available and if it is
1640 * it's solicited data, in which case, the FCP layer would
1641 * check it during the copy.
1642 */
1643 if (lport->crc_offload &&
1644 skb->ip_summed == CHECKSUM_UNNECESSARY)
1645 fr_flags(fp) &= ~FCPHF_CRC_UNCHECKED;
1646 else
1647 fr_flags(fp) |= FCPHF_CRC_UNCHECKED;
1648
1649 fh = fc_frame_header_get(fp);
1650 if (fh->fh_r_ctl == FC_RCTL_DD_SOL_DATA &&
1651 fh->fh_type == FC_TYPE_FCP) {
1652 fc_exch_recv(lport, fp);
1653 return;
1654 }
1655 if (fr_flags(fp) & FCPHF_CRC_UNCHECKED) {
1656 if (le32_to_cpu(fr_crc(fp)) !=
1657 ~crc32(~0, skb->data, fr_len)) {
1658 if (stats->InvalidCRCCount < 5)
1659 printk(KERN_WARNING "fcoe: dropping "
1660 "frame with CRC error\n");
1661 stats->InvalidCRCCount++;
1662 stats->ErrorFrames++;
1663 fc_frame_free(fp);
1664 return;
1665 }
1666 fr_flags(fp) &= ~FCPHF_CRC_UNCHECKED;
1667 }
1668 fc_exch_recv(lport, fp);
1669}
1670
1671/**
Robert Love1875f272009-11-03 11:47:50 -08001672 * fcoe_percpu_receive_thread() - The per-CPU packet receive thread
1673 * @arg: The per-CPU context
Robert Love85b4aa42008-12-09 15:10:24 -08001674 *
1675 * Return: 0 for success
Robert Love85b4aa42008-12-09 15:10:24 -08001676 */
1677int fcoe_percpu_receive_thread(void *arg)
1678{
1679 struct fcoe_percpu_s *p = arg;
Robert Love85b4aa42008-12-09 15:10:24 -08001680 struct sk_buff *skb;
Robert Love85b4aa42008-12-09 15:10:24 -08001681
Robert Love4469c192009-02-27 10:56:38 -08001682 set_user_nice(current, -20);
Robert Love85b4aa42008-12-09 15:10:24 -08001683
1684 while (!kthread_should_stop()) {
1685
1686 spin_lock_bh(&p->fcoe_rx_list.lock);
1687 while ((skb = __skb_dequeue(&p->fcoe_rx_list)) == NULL) {
1688 set_current_state(TASK_INTERRUPTIBLE);
1689 spin_unlock_bh(&p->fcoe_rx_list.lock);
1690 schedule();
1691 set_current_state(TASK_RUNNING);
1692 if (kthread_should_stop())
1693 return 0;
1694 spin_lock_bh(&p->fcoe_rx_list.lock);
1695 }
1696 spin_unlock_bh(&p->fcoe_rx_list.lock);
Chris Leech859b7b62009-11-20 14:54:47 -08001697 fcoe_recv_frame(skb);
Robert Love85b4aa42008-12-09 15:10:24 -08001698 }
1699 return 0;
1700}
1701
1702/**
Robert Love1875f272009-11-03 11:47:50 -08001703 * fcoe_check_wait_queue() - Attempt to clear the transmit backlog
1704 * @lport: The local port whose backlog is to be cleared
Robert Love85b4aa42008-12-09 15:10:24 -08001705 *
Robert Love1875f272009-11-03 11:47:50 -08001706 * This empties the wait_queue, dequeues the head of the wait_queue queue
1707 * and calls fcoe_start_io() for each packet. If all skb have been
1708 * transmitted it returns the qlen. If an error occurs it restores
1709 * wait_queue (to try again later) and returns -1.
Robert Love85b4aa42008-12-09 15:10:24 -08001710 *
Robert Love1875f272009-11-03 11:47:50 -08001711 * The wait_queue is used when the skb transmit fails. The failed skb
1712 * will go in the wait_queue which will be emptied by the timer function or
Robert Love85b4aa42008-12-09 15:10:24 -08001713 * by the next skb transmit.
Robert Love34f42a02009-02-27 10:55:45 -08001714 */
Robert Love1875f272009-11-03 11:47:50 -08001715static void fcoe_check_wait_queue(struct fc_lport *lport, struct sk_buff *skb)
Robert Love85b4aa42008-12-09 15:10:24 -08001716{
Robert Love1875f272009-11-03 11:47:50 -08001717 struct fcoe_port *port = lport_priv(lport);
Vasu Dev4bb6b512009-05-06 10:52:34 -07001718 int rc;
Robert Love85b4aa42008-12-09 15:10:24 -08001719
Chris Leech014f5c32009-08-25 13:59:30 -07001720 spin_lock_bh(&port->fcoe_pending_queue.lock);
Vasu Dev4bb6b512009-05-06 10:52:34 -07001721
1722 if (skb)
Chris Leech014f5c32009-08-25 13:59:30 -07001723 __skb_queue_tail(&port->fcoe_pending_queue, skb);
Vasu Dev4bb6b512009-05-06 10:52:34 -07001724
Chris Leech014f5c32009-08-25 13:59:30 -07001725 if (port->fcoe_pending_queue_active)
Vasu Devc826a312009-02-27 10:56:27 -08001726 goto out;
Chris Leech014f5c32009-08-25 13:59:30 -07001727 port->fcoe_pending_queue_active = 1;
Chris Leech55c8baf2009-02-27 10:56:32 -08001728
Chris Leech014f5c32009-08-25 13:59:30 -07001729 while (port->fcoe_pending_queue.qlen) {
Chris Leech55c8baf2009-02-27 10:56:32 -08001730 /* keep qlen > 0 until fcoe_start_io succeeds */
Chris Leech014f5c32009-08-25 13:59:30 -07001731 port->fcoe_pending_queue.qlen++;
1732 skb = __skb_dequeue(&port->fcoe_pending_queue);
Chris Leech55c8baf2009-02-27 10:56:32 -08001733
Chris Leech014f5c32009-08-25 13:59:30 -07001734 spin_unlock_bh(&port->fcoe_pending_queue.lock);
Chris Leech55c8baf2009-02-27 10:56:32 -08001735 rc = fcoe_start_io(skb);
Chris Leech014f5c32009-08-25 13:59:30 -07001736 spin_lock_bh(&port->fcoe_pending_queue.lock);
Chris Leech55c8baf2009-02-27 10:56:32 -08001737
1738 if (rc) {
Chris Leech014f5c32009-08-25 13:59:30 -07001739 __skb_queue_head(&port->fcoe_pending_queue, skb);
Chris Leech55c8baf2009-02-27 10:56:32 -08001740 /* undo temporary increment above */
Chris Leech014f5c32009-08-25 13:59:30 -07001741 port->fcoe_pending_queue.qlen--;
Chris Leech55c8baf2009-02-27 10:56:32 -08001742 break;
Robert Love85b4aa42008-12-09 15:10:24 -08001743 }
Chris Leech55c8baf2009-02-27 10:56:32 -08001744 /* undo temporary increment above */
Chris Leech014f5c32009-08-25 13:59:30 -07001745 port->fcoe_pending_queue.qlen--;
Robert Love85b4aa42008-12-09 15:10:24 -08001746 }
Chris Leech55c8baf2009-02-27 10:56:32 -08001747
Chris Leech014f5c32009-08-25 13:59:30 -07001748 if (port->fcoe_pending_queue.qlen < FCOE_LOW_QUEUE_DEPTH)
Robert Love1875f272009-11-03 11:47:50 -08001749 lport->qfull = 0;
Chris Leech014f5c32009-08-25 13:59:30 -07001750 if (port->fcoe_pending_queue.qlen && !timer_pending(&port->timer))
1751 mod_timer(&port->timer, jiffies + 2);
1752 port->fcoe_pending_queue_active = 0;
Vasu Devc826a312009-02-27 10:56:27 -08001753out:
Chris Leech014f5c32009-08-25 13:59:30 -07001754 if (port->fcoe_pending_queue.qlen > FCOE_MAX_QUEUE_DEPTH)
Robert Love1875f272009-11-03 11:47:50 -08001755 lport->qfull = 1;
Chris Leech014f5c32009-08-25 13:59:30 -07001756 spin_unlock_bh(&port->fcoe_pending_queue.lock);
Vasu Dev4bb6b512009-05-06 10:52:34 -07001757 return;
Robert Love85b4aa42008-12-09 15:10:24 -08001758}
1759
1760/**
Robert Love1875f272009-11-03 11:47:50 -08001761 * fcoe_dev_setup() - Setup the link change notification interface
Robert Love34f42a02009-02-27 10:55:45 -08001762 */
Randy Dunlapb0d428a2009-04-27 21:49:31 -07001763static void fcoe_dev_setup(void)
Robert Love85b4aa42008-12-09 15:10:24 -08001764{
Robert Love85b4aa42008-12-09 15:10:24 -08001765 register_netdevice_notifier(&fcoe_notifier);
1766}
1767
1768/**
Robert Love1875f272009-11-03 11:47:50 -08001769 * fcoe_dev_cleanup() - Cleanup the link change notification interface
Robert Love34f42a02009-02-27 10:55:45 -08001770 */
Robert Love85b4aa42008-12-09 15:10:24 -08001771static void fcoe_dev_cleanup(void)
1772{
1773 unregister_netdevice_notifier(&fcoe_notifier);
1774}
1775
1776/**
Robert Love1875f272009-11-03 11:47:50 -08001777 * fcoe_device_notification() - Handler for net device events
1778 * @notifier: The context of the notification
1779 * @event: The type of event
1780 * @ptr: The net device that the event was on
Robert Love85b4aa42008-12-09 15:10:24 -08001781 *
Robert Love1875f272009-11-03 11:47:50 -08001782 * This function is called by the Ethernet driver in case of link change event.
Robert Love85b4aa42008-12-09 15:10:24 -08001783 *
1784 * Returns: 0 for success
Robert Love34f42a02009-02-27 10:55:45 -08001785 */
Robert Love85b4aa42008-12-09 15:10:24 -08001786static int fcoe_device_notification(struct notifier_block *notifier,
1787 ulong event, void *ptr)
1788{
Robert Love1875f272009-11-03 11:47:50 -08001789 struct fc_lport *lport = NULL;
Vasu Dev1d1b88d2009-07-29 17:05:45 -07001790 struct net_device *netdev = ptr;
Chris Leech014f5c32009-08-25 13:59:30 -07001791 struct fcoe_interface *fcoe;
Chris Leech2e70e242009-08-25 14:00:23 -07001792 struct fcoe_port *port;
Robert Love85b4aa42008-12-09 15:10:24 -08001793 struct fcoe_dev_stats *stats;
Joe Eykholt97c83892009-03-17 11:42:40 -07001794 u32 link_possible = 1;
Robert Love85b4aa42008-12-09 15:10:24 -08001795 u32 mfs;
1796 int rc = NOTIFY_OK;
1797
Chris Leech014f5c32009-08-25 13:59:30 -07001798 list_for_each_entry(fcoe, &fcoe_hostlist, list) {
Chris Leech25024982009-08-25 13:59:35 -07001799 if (fcoe->netdev == netdev) {
Robert Love1875f272009-11-03 11:47:50 -08001800 lport = fcoe->ctlr.lp;
Robert Love85b4aa42008-12-09 15:10:24 -08001801 break;
1802 }
1803 }
Robert Love1875f272009-11-03 11:47:50 -08001804 if (!lport) {
Robert Love85b4aa42008-12-09 15:10:24 -08001805 rc = NOTIFY_DONE;
1806 goto out;
1807 }
1808
Robert Love85b4aa42008-12-09 15:10:24 -08001809 switch (event) {
1810 case NETDEV_DOWN:
1811 case NETDEV_GOING_DOWN:
Joe Eykholt97c83892009-03-17 11:42:40 -07001812 link_possible = 0;
Robert Love85b4aa42008-12-09 15:10:24 -08001813 break;
1814 case NETDEV_UP:
1815 case NETDEV_CHANGE:
Robert Love85b4aa42008-12-09 15:10:24 -08001816 break;
1817 case NETDEV_CHANGEMTU:
Yi Zou7221d7e2009-10-21 16:27:52 -07001818 if (netdev->features & NETIF_F_FCOE_MTU)
1819 break;
Vasu Dev1d1b88d2009-07-29 17:05:45 -07001820 mfs = netdev->mtu - (sizeof(struct fcoe_hdr) +
1821 sizeof(struct fcoe_crc_eof));
Robert Love85b4aa42008-12-09 15:10:24 -08001822 if (mfs >= FC_MIN_MAX_FRAME)
Robert Love1875f272009-11-03 11:47:50 -08001823 fc_set_mfs(lport, mfs);
Robert Love85b4aa42008-12-09 15:10:24 -08001824 break;
1825 case NETDEV_REGISTER:
1826 break;
Chris Leech2e70e242009-08-25 14:00:23 -07001827 case NETDEV_UNREGISTER:
1828 list_del(&fcoe->list);
1829 port = lport_priv(fcoe->ctlr.lp);
1830 fcoe_interface_cleanup(fcoe);
1831 schedule_work(&port->destroy_work);
1832 goto out;
1833 break;
Robert Love85b4aa42008-12-09 15:10:24 -08001834 default:
Vasu Dev1d1b88d2009-07-29 17:05:45 -07001835 FCOE_NETDEV_DBG(netdev, "Unknown event %ld "
Robert Loved5488eb2009-06-10 15:30:59 -07001836 "from netdev netlink\n", event);
Robert Love85b4aa42008-12-09 15:10:24 -08001837 }
Robert Love1875f272009-11-03 11:47:50 -08001838 if (link_possible && !fcoe_link_ok(lport))
Chris Leech3fe9a0b2009-08-25 13:59:46 -07001839 fcoe_ctlr_link_up(&fcoe->ctlr);
1840 else if (fcoe_ctlr_link_down(&fcoe->ctlr)) {
Robert Love1875f272009-11-03 11:47:50 -08001841 stats = fc_lport_get_stats(lport);
Joe Eykholt97c83892009-03-17 11:42:40 -07001842 stats->LinkFailureCount++;
Robert Love1875f272009-11-03 11:47:50 -08001843 fcoe_clean_pending_queue(lport);
Robert Love85b4aa42008-12-09 15:10:24 -08001844 }
1845out:
1846 return rc;
1847}
1848
1849/**
Robert Love1875f272009-11-03 11:47:50 -08001850 * fcoe_if_to_netdev() - Parse a name buffer to get a net device
1851 * @buffer: The name of the net device
Robert Love85b4aa42008-12-09 15:10:24 -08001852 *
Robert Love1875f272009-11-03 11:47:50 -08001853 * Returns: NULL or a ptr to net_device
Robert Love34f42a02009-02-27 10:55:45 -08001854 */
Robert Love85b4aa42008-12-09 15:10:24 -08001855static struct net_device *fcoe_if_to_netdev(const char *buffer)
1856{
1857 char *cp;
1858 char ifname[IFNAMSIZ + 2];
1859
1860 if (buffer) {
1861 strlcpy(ifname, buffer, IFNAMSIZ);
1862 cp = ifname + strlen(ifname);
1863 while (--cp >= ifname && *cp == '\n')
1864 *cp = '\0';
1865 return dev_get_by_name(&init_net, ifname);
1866 }
1867 return NULL;
1868}
1869
1870/**
Vasu Dev55a66d32009-12-10 09:59:31 -08001871 * fcoe_disable() - Disables a FCoE interface
1872 * @buffer: The name of the Ethernet interface to be disabled
1873 * @kp: The associated kernel parameter
1874 *
1875 * Called from sysfs.
1876 *
1877 * Returns: 0 for success
1878 */
1879static int fcoe_disable(const char *buffer, struct kernel_param *kp)
1880{
1881 struct fcoe_interface *fcoe;
1882 struct net_device *netdev;
1883 int rc = 0;
1884
1885 mutex_lock(&fcoe_config_mutex);
1886#ifdef CONFIG_FCOE_MODULE
1887 /*
1888 * Make sure the module has been initialized, and is not about to be
1889 * removed. Module paramter sysfs files are writable before the
1890 * module_init function is called and after module_exit.
1891 */
1892 if (THIS_MODULE->state != MODULE_STATE_LIVE) {
1893 rc = -ENODEV;
1894 goto out_nodev;
1895 }
1896#endif
1897
1898 netdev = fcoe_if_to_netdev(buffer);
1899 if (!netdev) {
1900 rc = -ENODEV;
1901 goto out_nodev;
1902 }
1903
1904 rtnl_lock();
1905 fcoe = fcoe_hostlist_lookup_port(netdev);
1906 rtnl_unlock();
1907
1908 if (fcoe)
1909 fc_fabric_logoff(fcoe->ctlr.lp);
1910 else
1911 rc = -ENODEV;
1912
1913 dev_put(netdev);
1914out_nodev:
1915 mutex_unlock(&fcoe_config_mutex);
1916 return rc;
1917}
1918
1919/**
1920 * fcoe_enable() - Enables a FCoE interface
1921 * @buffer: The name of the Ethernet interface to be enabled
1922 * @kp: The associated kernel parameter
1923 *
1924 * Called from sysfs.
1925 *
1926 * Returns: 0 for success
1927 */
1928static int fcoe_enable(const char *buffer, struct kernel_param *kp)
1929{
1930 struct fcoe_interface *fcoe;
1931 struct net_device *netdev;
1932 int rc = 0;
1933
1934 mutex_lock(&fcoe_config_mutex);
1935#ifdef CONFIG_FCOE_MODULE
1936 /*
1937 * Make sure the module has been initialized, and is not about to be
1938 * removed. Module paramter sysfs files are writable before the
1939 * module_init function is called and after module_exit.
1940 */
1941 if (THIS_MODULE->state != MODULE_STATE_LIVE) {
1942 rc = -ENODEV;
1943 goto out_nodev;
1944 }
1945#endif
1946
1947 netdev = fcoe_if_to_netdev(buffer);
1948 if (!netdev) {
1949 rc = -ENODEV;
1950 goto out_nodev;
1951 }
1952
1953 rtnl_lock();
1954 fcoe = fcoe_hostlist_lookup_port(netdev);
1955 rtnl_unlock();
1956
1957 if (fcoe)
1958 rc = fc_fabric_login(fcoe->ctlr.lp);
1959 else
1960 rc = -ENODEV;
1961
1962 dev_put(netdev);
1963out_nodev:
1964 mutex_unlock(&fcoe_config_mutex);
1965 return rc;
1966}
1967
1968/**
Robert Love1875f272009-11-03 11:47:50 -08001969 * fcoe_destroy() - Destroy a FCoE interface
1970 * @buffer: The name of the Ethernet interface to be destroyed
1971 * @kp: The associated kernel parameter
1972 *
1973 * Called from sysfs.
Robert Love85b4aa42008-12-09 15:10:24 -08001974 *
1975 * Returns: 0 for success
Robert Love34f42a02009-02-27 10:55:45 -08001976 */
Robert Love85b4aa42008-12-09 15:10:24 -08001977static int fcoe_destroy(const char *buffer, struct kernel_param *kp)
1978{
Chris Leech030f4e02009-08-25 14:00:02 -07001979 struct fcoe_interface *fcoe;
Chris Leech2e70e242009-08-25 14:00:23 -07001980 struct net_device *netdev;
Mike Christie8eca3552009-10-21 16:27:44 -07001981 int rc = 0;
Robert Love85b4aa42008-12-09 15:10:24 -08001982
Chris Leechdfc1d0f2009-08-25 14:00:13 -07001983 mutex_lock(&fcoe_config_mutex);
1984#ifdef CONFIG_FCOE_MODULE
1985 /*
1986 * Make sure the module has been initialized, and is not about to be
1987 * removed. Module paramter sysfs files are writable before the
1988 * module_init function is called and after module_exit.
1989 */
1990 if (THIS_MODULE->state != MODULE_STATE_LIVE) {
1991 rc = -ENODEV;
1992 goto out_nodev;
1993 }
1994#endif
1995
Robert Love85b4aa42008-12-09 15:10:24 -08001996 netdev = fcoe_if_to_netdev(buffer);
1997 if (!netdev) {
1998 rc = -ENODEV;
1999 goto out_nodev;
2000 }
Chris Leech2e70e242009-08-25 14:00:23 -07002001
Chris Leech090eb6c2009-08-25 14:00:28 -07002002 rtnl_lock();
Chris Leech2e70e242009-08-25 14:00:23 -07002003 fcoe = fcoe_hostlist_lookup_port(netdev);
2004 if (!fcoe) {
Chris Leech090eb6c2009-08-25 14:00:28 -07002005 rtnl_unlock();
Robert Love85b4aa42008-12-09 15:10:24 -08002006 rc = -ENODEV;
2007 goto out_putdev;
2008 }
Chris Leech2e70e242009-08-25 14:00:23 -07002009 list_del(&fcoe->list);
Chris Leech2e70e242009-08-25 14:00:23 -07002010 fcoe_interface_cleanup(fcoe);
2011 rtnl_unlock();
2012 fcoe_if_destroy(fcoe->ctlr.lp);
Rob Love6409ea62010-01-21 10:16:05 -08002013 module_put(THIS_MODULE);
2014
Robert Love85b4aa42008-12-09 15:10:24 -08002015out_putdev:
2016 dev_put(netdev);
2017out_nodev:
Chris Leechdfc1d0f2009-08-25 14:00:13 -07002018 mutex_unlock(&fcoe_config_mutex);
Robert Love85b4aa42008-12-09 15:10:24 -08002019 return rc;
2020}
2021
Robert Love1875f272009-11-03 11:47:50 -08002022/**
2023 * fcoe_destroy_work() - Destroy a FCoE port in a deferred work context
2024 * @work: Handle to the FCoE port to be destroyed
2025 */
Chris Leech2e70e242009-08-25 14:00:23 -07002026static void fcoe_destroy_work(struct work_struct *work)
2027{
2028 struct fcoe_port *port;
2029
2030 port = container_of(work, struct fcoe_port, destroy_work);
2031 mutex_lock(&fcoe_config_mutex);
2032 fcoe_if_destroy(port->lport);
2033 mutex_unlock(&fcoe_config_mutex);
2034}
2035
Robert Love85b4aa42008-12-09 15:10:24 -08002036/**
Robert Love1875f272009-11-03 11:47:50 -08002037 * fcoe_create() - Create a fcoe interface
2038 * @buffer: The name of the Ethernet interface to create on
2039 * @kp: The associated kernel param
2040 *
2041 * Called from sysfs.
Robert Love85b4aa42008-12-09 15:10:24 -08002042 *
2043 * Returns: 0 for success
Robert Love34f42a02009-02-27 10:55:45 -08002044 */
Robert Love85b4aa42008-12-09 15:10:24 -08002045static int fcoe_create(const char *buffer, struct kernel_param *kp)
2046{
2047 int rc;
Chris Leech030f4e02009-08-25 14:00:02 -07002048 struct fcoe_interface *fcoe;
Chris Leechaf7f85d2009-08-25 13:59:24 -07002049 struct fc_lport *lport;
Robert Love85b4aa42008-12-09 15:10:24 -08002050 struct net_device *netdev;
2051
Chris Leechdfc1d0f2009-08-25 14:00:13 -07002052 mutex_lock(&fcoe_config_mutex);
2053#ifdef CONFIG_FCOE_MODULE
2054 /*
2055 * Make sure the module has been initialized, and is not about to be
2056 * removed. Module paramter sysfs files are writable before the
2057 * module_init function is called and after module_exit.
2058 */
2059 if (THIS_MODULE->state != MODULE_STATE_LIVE) {
2060 rc = -ENODEV;
2061 goto out_nodev;
2062 }
2063#endif
2064
Rob Love6409ea62010-01-21 10:16:05 -08002065 if (!try_module_get(THIS_MODULE)) {
2066 rc = -EINVAL;
2067 goto out_nomod;
2068 }
2069
Chris Leech2e70e242009-08-25 14:00:23 -07002070 rtnl_lock();
Robert Love85b4aa42008-12-09 15:10:24 -08002071 netdev = fcoe_if_to_netdev(buffer);
2072 if (!netdev) {
2073 rc = -ENODEV;
2074 goto out_nodev;
2075 }
Chris Leech2e70e242009-08-25 14:00:23 -07002076
Robert Love85b4aa42008-12-09 15:10:24 -08002077 /* look for existing lport */
2078 if (fcoe_hostlist_lookup(netdev)) {
2079 rc = -EEXIST;
2080 goto out_putdev;
2081 }
Robert Love85b4aa42008-12-09 15:10:24 -08002082
Chris Leech030f4e02009-08-25 14:00:02 -07002083 fcoe = fcoe_interface_create(netdev);
2084 if (!fcoe) {
2085 rc = -ENOMEM;
2086 goto out_putdev;
2087 }
2088
Chris Leech9a057532009-11-03 11:46:40 -08002089 lport = fcoe_if_create(fcoe, &netdev->dev, 0);
Chris Leechaf7f85d2009-08-25 13:59:24 -07002090 if (IS_ERR(lport)) {
Robert Loved5488eb2009-06-10 15:30:59 -07002091 printk(KERN_ERR "fcoe: Failed to create interface (%s)\n",
Robert Love85b4aa42008-12-09 15:10:24 -08002092 netdev->name);
Robert Love85b4aa42008-12-09 15:10:24 -08002093 rc = -EIO;
Chris Leech2e70e242009-08-25 14:00:23 -07002094 fcoe_interface_cleanup(fcoe);
Chris Leech030f4e02009-08-25 14:00:02 -07002095 goto out_free;
Robert Love85b4aa42008-12-09 15:10:24 -08002096 }
Chris Leech030f4e02009-08-25 14:00:02 -07002097
Chris Leech54b649f2009-08-25 14:00:07 -07002098 /* Make this the "master" N_Port */
2099 fcoe->ctlr.lp = lport;
Chris Leech030f4e02009-08-25 14:00:02 -07002100
Chris Leechc863df32009-08-25 14:00:18 -07002101 /* add to lports list */
2102 fcoe_hostlist_add(lport);
2103
Chris Leech54b649f2009-08-25 14:00:07 -07002104 /* start FIP Discovery and FLOGI */
2105 lport->boot_time = jiffies;
2106 fc_fabric_login(lport);
2107 if (!fcoe_link_ok(lport))
2108 fcoe_ctlr_link_up(&fcoe->ctlr);
2109
Chris Leech54b649f2009-08-25 14:00:07 -07002110 /*
2111 * Release from init in fcoe_interface_create(), on success lport
2112 * should be holding a reference taken in fcoe_if_create().
2113 */
Chris Leech030f4e02009-08-25 14:00:02 -07002114 fcoe_interface_put(fcoe);
Rob Love6409ea62010-01-21 10:16:05 -08002115 dev_put(netdev);
2116 rtnl_unlock();
2117 mutex_unlock(&fcoe_config_mutex);
2118
2119 return 0;
2120out_free:
2121 fcoe_interface_put(fcoe);
Robert Love85b4aa42008-12-09 15:10:24 -08002122out_putdev:
2123 dev_put(netdev);
2124out_nodev:
Chris Leech2e70e242009-08-25 14:00:23 -07002125 rtnl_unlock();
Rob Love6409ea62010-01-21 10:16:05 -08002126 module_put(THIS_MODULE);
2127out_nomod:
Chris Leechdfc1d0f2009-08-25 14:00:13 -07002128 mutex_unlock(&fcoe_config_mutex);
Robert Love85b4aa42008-12-09 15:10:24 -08002129 return rc;
2130}
2131
Robert Love34f42a02009-02-27 10:55:45 -08002132/**
Robert Love1875f272009-11-03 11:47:50 -08002133 * fcoe_link_ok() - Check if the link is OK for a local port
2134 * @lport: The local port to check link on
Robert Love85b4aa42008-12-09 15:10:24 -08002135 *
2136 * Any permanently-disqualifying conditions have been previously checked.
2137 * This also updates the speed setting, which may change with link for 100/1000.
2138 *
2139 * This function should probably be checking for PAUSE support at some point
2140 * in the future. Currently Per-priority-pause is not determinable using
2141 * ethtool, so we shouldn't be restrictive until that problem is resolved.
2142 *
2143 * Returns: 0 if link is OK for use by FCoE.
2144 *
2145 */
Robert Love1875f272009-11-03 11:47:50 -08002146int fcoe_link_ok(struct fc_lport *lport)
Robert Love85b4aa42008-12-09 15:10:24 -08002147{
Robert Love1875f272009-11-03 11:47:50 -08002148 struct fcoe_port *port = lport_priv(lport);
2149 struct net_device *netdev = port->fcoe->netdev;
Robert Love85b4aa42008-12-09 15:10:24 -08002150 struct ethtool_cmd ecmd = { ETHTOOL_GSET };
Robert Love85b4aa42008-12-09 15:10:24 -08002151
Robert Love1875f272009-11-03 11:47:50 -08002152 if ((netdev->flags & IFF_UP) && netif_carrier_ok(netdev) &&
2153 (!dev_ethtool_get_settings(netdev, &ecmd))) {
2154 lport->link_supported_speeds &=
Yi Zou2f718d62009-07-29 17:04:01 -07002155 ~(FC_PORTSPEED_1GBIT | FC_PORTSPEED_10GBIT);
2156 if (ecmd.supported & (SUPPORTED_1000baseT_Half |
2157 SUPPORTED_1000baseT_Full))
Robert Love1875f272009-11-03 11:47:50 -08002158 lport->link_supported_speeds |= FC_PORTSPEED_1GBIT;
Yi Zou2f718d62009-07-29 17:04:01 -07002159 if (ecmd.supported & SUPPORTED_10000baseT_Full)
Robert Love1875f272009-11-03 11:47:50 -08002160 lport->link_supported_speeds |=
Yi Zou2f718d62009-07-29 17:04:01 -07002161 FC_PORTSPEED_10GBIT;
2162 if (ecmd.speed == SPEED_1000)
Robert Love1875f272009-11-03 11:47:50 -08002163 lport->link_speed = FC_PORTSPEED_1GBIT;
Yi Zou2f718d62009-07-29 17:04:01 -07002164 if (ecmd.speed == SPEED_10000)
Robert Love1875f272009-11-03 11:47:50 -08002165 lport->link_speed = FC_PORTSPEED_10GBIT;
Robert Love85b4aa42008-12-09 15:10:24 -08002166
Yi Zou2f718d62009-07-29 17:04:01 -07002167 return 0;
2168 }
2169 return -1;
Robert Love85b4aa42008-12-09 15:10:24 -08002170}
Robert Love85b4aa42008-12-09 15:10:24 -08002171
Robert Love34f42a02009-02-27 10:55:45 -08002172/**
Robert Love1875f272009-11-03 11:47:50 -08002173 * fcoe_percpu_clean() - Clear all pending skbs for an local port
2174 * @lport: The local port whose skbs are to be cleared
Joe Eykholte7a51992009-08-25 14:04:08 -07002175 *
2176 * Must be called with fcoe_create_mutex held to single-thread completion.
2177 *
2178 * This flushes the pending skbs by adding a new skb to each queue and
2179 * waiting until they are all freed. This assures us that not only are
2180 * there no packets that will be handled by the lport, but also that any
2181 * threads already handling packet have returned.
Robert Love85b4aa42008-12-09 15:10:24 -08002182 */
Robert Love1875f272009-11-03 11:47:50 -08002183void fcoe_percpu_clean(struct fc_lport *lport)
Robert Love85b4aa42008-12-09 15:10:24 -08002184{
Robert Love85b4aa42008-12-09 15:10:24 -08002185 struct fcoe_percpu_s *pp;
2186 struct fcoe_rcv_info *fr;
2187 struct sk_buff_head *list;
2188 struct sk_buff *skb, *next;
2189 struct sk_buff *head;
Robert Love5e5e92d2009-03-17 11:41:35 -07002190 unsigned int cpu;
Robert Love85b4aa42008-12-09 15:10:24 -08002191
Robert Love5e5e92d2009-03-17 11:41:35 -07002192 for_each_possible_cpu(cpu) {
2193 pp = &per_cpu(fcoe_percpu, cpu);
2194 spin_lock_bh(&pp->fcoe_rx_list.lock);
2195 list = &pp->fcoe_rx_list;
2196 head = list->next;
2197 for (skb = head; skb != (struct sk_buff *)list;
2198 skb = next) {
2199 next = skb->next;
2200 fr = fcoe_dev_from_skb(skb);
Robert Love1875f272009-11-03 11:47:50 -08002201 if (fr->fr_dev == lport) {
Robert Love5e5e92d2009-03-17 11:41:35 -07002202 __skb_unlink(skb, list);
2203 kfree_skb(skb);
Robert Love85b4aa42008-12-09 15:10:24 -08002204 }
Robert Love85b4aa42008-12-09 15:10:24 -08002205 }
Joe Eykholte7a51992009-08-25 14:04:08 -07002206
2207 if (!pp->thread || !cpu_online(cpu)) {
2208 spin_unlock_bh(&pp->fcoe_rx_list.lock);
2209 continue;
2210 }
2211
2212 skb = dev_alloc_skb(0);
2213 if (!skb) {
2214 spin_unlock_bh(&pp->fcoe_rx_list.lock);
2215 continue;
2216 }
2217 skb->destructor = fcoe_percpu_flush_done;
2218
2219 __skb_queue_tail(&pp->fcoe_rx_list, skb);
2220 if (pp->fcoe_rx_list.qlen == 1)
2221 wake_up_process(pp->thread);
Robert Love5e5e92d2009-03-17 11:41:35 -07002222 spin_unlock_bh(&pp->fcoe_rx_list.lock);
Joe Eykholte7a51992009-08-25 14:04:08 -07002223
2224 wait_for_completion(&fcoe_flush_completion);
Robert Love85b4aa42008-12-09 15:10:24 -08002225 }
2226}
Robert Love85b4aa42008-12-09 15:10:24 -08002227
2228/**
Robert Love34f42a02009-02-27 10:55:45 -08002229 * fcoe_clean_pending_queue() - Dequeue a skb and free it
Robert Love1875f272009-11-03 11:47:50 -08002230 * @lport: The local port to dequeue a skb on
Robert Love34f42a02009-02-27 10:55:45 -08002231 */
Robert Love1875f272009-11-03 11:47:50 -08002232void fcoe_clean_pending_queue(struct fc_lport *lport)
Robert Love85b4aa42008-12-09 15:10:24 -08002233{
Robert Love1875f272009-11-03 11:47:50 -08002234 struct fcoe_port *port = lport_priv(lport);
Robert Love85b4aa42008-12-09 15:10:24 -08002235 struct sk_buff *skb;
2236
Chris Leech014f5c32009-08-25 13:59:30 -07002237 spin_lock_bh(&port->fcoe_pending_queue.lock);
2238 while ((skb = __skb_dequeue(&port->fcoe_pending_queue)) != NULL) {
2239 spin_unlock_bh(&port->fcoe_pending_queue.lock);
Robert Love85b4aa42008-12-09 15:10:24 -08002240 kfree_skb(skb);
Chris Leech014f5c32009-08-25 13:59:30 -07002241 spin_lock_bh(&port->fcoe_pending_queue.lock);
Robert Love85b4aa42008-12-09 15:10:24 -08002242 }
Chris Leech014f5c32009-08-25 13:59:30 -07002243 spin_unlock_bh(&port->fcoe_pending_queue.lock);
Robert Love85b4aa42008-12-09 15:10:24 -08002244}
Robert Love85b4aa42008-12-09 15:10:24 -08002245
2246/**
Robert Love1875f272009-11-03 11:47:50 -08002247 * fcoe_reset() - Reset a local port
2248 * @shost: The SCSI host associated with the local port to be reset
Robert Love85b4aa42008-12-09 15:10:24 -08002249 *
Robert Love1875f272009-11-03 11:47:50 -08002250 * Returns: Always 0 (return value required by FC transport template)
Robert Love85b4aa42008-12-09 15:10:24 -08002251 */
2252int fcoe_reset(struct Scsi_Host *shost)
2253{
2254 struct fc_lport *lport = shost_priv(shost);
2255 fc_lport_reset(lport);
2256 return 0;
2257}
Robert Love85b4aa42008-12-09 15:10:24 -08002258
Robert Love34f42a02009-02-27 10:55:45 -08002259/**
Robert Love1875f272009-11-03 11:47:50 -08002260 * fcoe_hostlist_lookup_port() - Find the FCoE interface associated with a net device
2261 * @netdev: The net device used as a key
Robert Love85b4aa42008-12-09 15:10:24 -08002262 *
Robert Love1875f272009-11-03 11:47:50 -08002263 * Locking: Must be called with the RNL mutex held.
2264 *
2265 * Returns: NULL or the FCoE interface
Robert Love85b4aa42008-12-09 15:10:24 -08002266 */
Chris Leech014f5c32009-08-25 13:59:30 -07002267static struct fcoe_interface *
Robert Love1875f272009-11-03 11:47:50 -08002268fcoe_hostlist_lookup_port(const struct net_device *netdev)
Robert Love85b4aa42008-12-09 15:10:24 -08002269{
Chris Leech014f5c32009-08-25 13:59:30 -07002270 struct fcoe_interface *fcoe;
Robert Love85b4aa42008-12-09 15:10:24 -08002271
Chris Leech014f5c32009-08-25 13:59:30 -07002272 list_for_each_entry(fcoe, &fcoe_hostlist, list) {
Robert Love1875f272009-11-03 11:47:50 -08002273 if (fcoe->netdev == netdev)
Chris Leech014f5c32009-08-25 13:59:30 -07002274 return fcoe;
Robert Love85b4aa42008-12-09 15:10:24 -08002275 }
Robert Love85b4aa42008-12-09 15:10:24 -08002276 return NULL;
2277}
2278
Robert Love34f42a02009-02-27 10:55:45 -08002279/**
Robert Love1875f272009-11-03 11:47:50 -08002280 * fcoe_hostlist_lookup() - Find the local port associated with a
2281 * given net device
2282 * @netdev: The netdevice used as a key
Robert Love85b4aa42008-12-09 15:10:24 -08002283 *
Robert Love1875f272009-11-03 11:47:50 -08002284 * Locking: Must be called with the RTNL mutex held
2285 *
2286 * Returns: NULL or the local port
Robert Love85b4aa42008-12-09 15:10:24 -08002287 */
Chris Leech090eb6c2009-08-25 14:00:28 -07002288static struct fc_lport *fcoe_hostlist_lookup(const struct net_device *netdev)
Robert Love85b4aa42008-12-09 15:10:24 -08002289{
Chris Leech014f5c32009-08-25 13:59:30 -07002290 struct fcoe_interface *fcoe;
Robert Love85b4aa42008-12-09 15:10:24 -08002291
Chris Leech014f5c32009-08-25 13:59:30 -07002292 fcoe = fcoe_hostlist_lookup_port(netdev);
Chris Leech3fe9a0b2009-08-25 13:59:46 -07002293 return (fcoe) ? fcoe->ctlr.lp : NULL;
Robert Love85b4aa42008-12-09 15:10:24 -08002294}
Robert Love85b4aa42008-12-09 15:10:24 -08002295
Robert Love34f42a02009-02-27 10:55:45 -08002296/**
Robert Love1875f272009-11-03 11:47:50 -08002297 * fcoe_hostlist_add() - Add the FCoE interface identified by a local
2298 * port to the hostlist
2299 * @lport: The local port that identifies the FCoE interface to be added
2300 *
2301 * Locking: must be called with the RTNL mutex held
Robert Love85b4aa42008-12-09 15:10:24 -08002302 *
2303 * Returns: 0 for success
2304 */
Chris Leech090eb6c2009-08-25 14:00:28 -07002305static int fcoe_hostlist_add(const struct fc_lport *lport)
Robert Love85b4aa42008-12-09 15:10:24 -08002306{
Chris Leech014f5c32009-08-25 13:59:30 -07002307 struct fcoe_interface *fcoe;
2308 struct fcoe_port *port;
Robert Love85b4aa42008-12-09 15:10:24 -08002309
Chris Leech014f5c32009-08-25 13:59:30 -07002310 fcoe = fcoe_hostlist_lookup_port(fcoe_netdev(lport));
2311 if (!fcoe) {
2312 port = lport_priv(lport);
2313 fcoe = port->fcoe;
2314 list_add_tail(&fcoe->list, &fcoe_hostlist);
Robert Love85b4aa42008-12-09 15:10:24 -08002315 }
2316 return 0;
2317}
Robert Love85b4aa42008-12-09 15:10:24 -08002318
Robert Love34f42a02009-02-27 10:55:45 -08002319/**
Robert Love1875f272009-11-03 11:47:50 -08002320 * fcoe_init() - Initialize fcoe.ko
Robert Love85b4aa42008-12-09 15:10:24 -08002321 *
Robert Love1875f272009-11-03 11:47:50 -08002322 * Returns: 0 on success, or a negative value on failure
Robert Love34f42a02009-02-27 10:55:45 -08002323 */
Robert Love85b4aa42008-12-09 15:10:24 -08002324static int __init fcoe_init(void)
2325{
Robert Love1875f272009-11-03 11:47:50 -08002326 struct fcoe_percpu_s *p;
Robert Love38eccab2009-03-17 11:41:30 -07002327 unsigned int cpu;
Robert Love8976f422009-03-17 11:41:46 -07002328 int rc = 0;
Robert Love85b4aa42008-12-09 15:10:24 -08002329
Chris Leechdfc1d0f2009-08-25 14:00:13 -07002330 mutex_lock(&fcoe_config_mutex);
2331
Robert Love38eccab2009-03-17 11:41:30 -07002332 for_each_possible_cpu(cpu) {
Robert Love5e5e92d2009-03-17 11:41:35 -07002333 p = &per_cpu(fcoe_percpu, cpu);
Robert Love38eccab2009-03-17 11:41:30 -07002334 skb_queue_head_init(&p->fcoe_rx_list);
2335 }
2336
Robert Love8976f422009-03-17 11:41:46 -07002337 for_each_online_cpu(cpu)
2338 fcoe_percpu_thread_create(cpu);
Robert Love85b4aa42008-12-09 15:10:24 -08002339
Robert Love8976f422009-03-17 11:41:46 -07002340 /* Initialize per CPU interrupt thread */
2341 rc = register_hotcpu_notifier(&fcoe_cpu_notifier);
2342 if (rc)
2343 goto out_free;
Robert Love85b4aa42008-12-09 15:10:24 -08002344
Robert Love8976f422009-03-17 11:41:46 -07002345 /* Setup link change notification */
Robert Love85b4aa42008-12-09 15:10:24 -08002346 fcoe_dev_setup();
2347
Chris Leech5892c322009-08-25 13:59:14 -07002348 rc = fcoe_if_init();
2349 if (rc)
2350 goto out_free;
Robert Love85b4aa42008-12-09 15:10:24 -08002351
Chris Leechdfc1d0f2009-08-25 14:00:13 -07002352 mutex_unlock(&fcoe_config_mutex);
Robert Love85b4aa42008-12-09 15:10:24 -08002353 return 0;
Robert Love8976f422009-03-17 11:41:46 -07002354
2355out_free:
2356 for_each_online_cpu(cpu) {
2357 fcoe_percpu_thread_destroy(cpu);
2358 }
Chris Leechdfc1d0f2009-08-25 14:00:13 -07002359 mutex_unlock(&fcoe_config_mutex);
Robert Love8976f422009-03-17 11:41:46 -07002360 return rc;
Robert Love85b4aa42008-12-09 15:10:24 -08002361}
2362module_init(fcoe_init);
2363
2364/**
Robert Love1875f272009-11-03 11:47:50 -08002365 * fcoe_exit() - Clean up fcoe.ko
Robert Love85b4aa42008-12-09 15:10:24 -08002366 *
Robert Love1875f272009-11-03 11:47:50 -08002367 * Returns: 0 on success or a negative value on failure
Robert Love34f42a02009-02-27 10:55:45 -08002368 */
Robert Love85b4aa42008-12-09 15:10:24 -08002369static void __exit fcoe_exit(void)
2370{
Chris Leech014f5c32009-08-25 13:59:30 -07002371 struct fcoe_interface *fcoe, *tmp;
Chris Leech2e70e242009-08-25 14:00:23 -07002372 struct fcoe_port *port;
Robert Love1875f272009-11-03 11:47:50 -08002373 unsigned int cpu;
Robert Love85b4aa42008-12-09 15:10:24 -08002374
Chris Leechdfc1d0f2009-08-25 14:00:13 -07002375 mutex_lock(&fcoe_config_mutex);
2376
Robert Love85b4aa42008-12-09 15:10:24 -08002377 fcoe_dev_cleanup();
2378
Vasu Dev5919a592009-03-27 09:03:29 -07002379 /* releases the associated fcoe hosts */
Chris Leech090eb6c2009-08-25 14:00:28 -07002380 rtnl_lock();
2381 list_for_each_entry_safe(fcoe, tmp, &fcoe_hostlist, list) {
Chris Leechc863df32009-08-25 14:00:18 -07002382 list_del(&fcoe->list);
Chris Leech2e70e242009-08-25 14:00:23 -07002383 port = lport_priv(fcoe->ctlr.lp);
Chris Leech2e70e242009-08-25 14:00:23 -07002384 fcoe_interface_cleanup(fcoe);
Chris Leech2e70e242009-08-25 14:00:23 -07002385 schedule_work(&port->destroy_work);
Chris Leechc863df32009-08-25 14:00:18 -07002386 }
Chris Leech090eb6c2009-08-25 14:00:28 -07002387 rtnl_unlock();
Robert Love85b4aa42008-12-09 15:10:24 -08002388
Robert Love8976f422009-03-17 11:41:46 -07002389 unregister_hotcpu_notifier(&fcoe_cpu_notifier);
2390
Chris Leech014f5c32009-08-25 13:59:30 -07002391 for_each_online_cpu(cpu)
Robert Love8976f422009-03-17 11:41:46 -07002392 fcoe_percpu_thread_destroy(cpu);
Robert Love85b4aa42008-12-09 15:10:24 -08002393
Chris Leechdfc1d0f2009-08-25 14:00:13 -07002394 mutex_unlock(&fcoe_config_mutex);
Chris Leech2e70e242009-08-25 14:00:23 -07002395
2396 /* flush any asyncronous interface destroys,
2397 * this should happen after the netdev notifier is unregistered */
2398 flush_scheduled_work();
Chris Leech9a057532009-11-03 11:46:40 -08002399 /* That will flush out all the N_Ports on the hostlist, but now we
2400 * may have NPIV VN_Ports scheduled for destruction */
2401 flush_scheduled_work();
Chris Leech2e70e242009-08-25 14:00:23 -07002402
2403 /* detach from scsi transport
2404 * must happen after all destroys are done, therefor after the flush */
2405 fcoe_if_exit();
Robert Love85b4aa42008-12-09 15:10:24 -08002406}
2407module_exit(fcoe_exit);
Chris Leech11b56182009-11-03 11:46:29 -08002408
2409/**
2410 * fcoe_flogi_resp() - FCoE specific FLOGI and FDISC response handler
2411 * @seq: active sequence in the FLOGI or FDISC exchange
2412 * @fp: response frame, or error encoded in a pointer (timeout)
2413 * @arg: pointer the the fcoe_ctlr structure
2414 *
2415 * This handles MAC address managment for FCoE, then passes control on to
2416 * the libfc FLOGI response handler.
2417 */
2418static void fcoe_flogi_resp(struct fc_seq *seq, struct fc_frame *fp, void *arg)
2419{
2420 struct fcoe_ctlr *fip = arg;
2421 struct fc_exch *exch = fc_seq_exch(seq);
2422 struct fc_lport *lport = exch->lp;
2423 u8 *mac;
2424
2425 if (IS_ERR(fp))
2426 goto done;
2427
2428 mac = fr_cb(fp)->granted_mac;
2429 if (is_zero_ether_addr(mac)) {
2430 /* pre-FIP */
Joe Eykholt386309ce2009-11-03 11:49:16 -08002431 if (fcoe_ctlr_recv_flogi(fip, lport, fp)) {
Chris Leech11b56182009-11-03 11:46:29 -08002432 fc_frame_free(fp);
2433 return;
2434 }
Chris Leech11b56182009-11-03 11:46:29 -08002435 }
Joe Eykholt386309ce2009-11-03 11:49:16 -08002436 fcoe_update_src_mac(lport, mac);
Chris Leech11b56182009-11-03 11:46:29 -08002437done:
2438 fc_lport_flogi_resp(seq, fp, lport);
2439}
2440
2441/**
2442 * fcoe_logo_resp() - FCoE specific LOGO response handler
2443 * @seq: active sequence in the LOGO exchange
2444 * @fp: response frame, or error encoded in a pointer (timeout)
2445 * @arg: pointer the the fcoe_ctlr structure
2446 *
2447 * This handles MAC address managment for FCoE, then passes control on to
2448 * the libfc LOGO response handler.
2449 */
2450static void fcoe_logo_resp(struct fc_seq *seq, struct fc_frame *fp, void *arg)
2451{
Joe Eykholt386309ce2009-11-03 11:49:16 -08002452 struct fc_lport *lport = arg;
Chris Leech11b56182009-11-03 11:46:29 -08002453 static u8 zero_mac[ETH_ALEN] = { 0 };
2454
2455 if (!IS_ERR(fp))
Joe Eykholt386309ce2009-11-03 11:49:16 -08002456 fcoe_update_src_mac(lport, zero_mac);
Chris Leech11b56182009-11-03 11:46:29 -08002457 fc_lport_logo_resp(seq, fp, lport);
2458}
2459
2460/**
2461 * fcoe_elsct_send - FCoE specific ELS handler
2462 *
2463 * This does special case handling of FIP encapsualted ELS exchanges for FCoE,
2464 * using FCoE specific response handlers and passing the FIP controller as
2465 * the argument (the lport is still available from the exchange).
2466 *
2467 * Most of the work here is just handed off to the libfc routine.
2468 */
Robert Love1875f272009-11-03 11:47:50 -08002469static struct fc_seq *fcoe_elsct_send(struct fc_lport *lport, u32 did,
2470 struct fc_frame *fp, unsigned int op,
2471 void (*resp)(struct fc_seq *,
2472 struct fc_frame *,
2473 void *),
2474 void *arg, u32 timeout)
Chris Leech11b56182009-11-03 11:46:29 -08002475{
2476 struct fcoe_port *port = lport_priv(lport);
2477 struct fcoe_interface *fcoe = port->fcoe;
2478 struct fcoe_ctlr *fip = &fcoe->ctlr;
2479 struct fc_frame_header *fh = fc_frame_header_get(fp);
2480
2481 switch (op) {
2482 case ELS_FLOGI:
2483 case ELS_FDISC:
2484 return fc_elsct_send(lport, did, fp, op, fcoe_flogi_resp,
2485 fip, timeout);
2486 case ELS_LOGO:
2487 /* only hook onto fabric logouts, not port logouts */
2488 if (ntoh24(fh->fh_d_id) != FC_FID_FLOGI)
2489 break;
2490 return fc_elsct_send(lport, did, fp, op, fcoe_logo_resp,
Joe Eykholt386309ce2009-11-03 11:49:16 -08002491 lport, timeout);
Chris Leech11b56182009-11-03 11:46:29 -08002492 }
2493 return fc_elsct_send(lport, did, fp, op, resp, arg, timeout);
2494}
2495
Chris Leech9a057532009-11-03 11:46:40 -08002496/**
2497 * fcoe_vport_create() - create an fc_host/scsi_host for a vport
2498 * @vport: fc_vport object to create a new fc_host for
2499 * @disabled: start the new fc_host in a disabled state by default?
2500 *
2501 * Returns: 0 for success
2502 */
2503static int fcoe_vport_create(struct fc_vport *vport, bool disabled)
2504{
2505 struct Scsi_Host *shost = vport_to_shost(vport);
2506 struct fc_lport *n_port = shost_priv(shost);
2507 struct fcoe_port *port = lport_priv(n_port);
2508 struct fcoe_interface *fcoe = port->fcoe;
2509 struct net_device *netdev = fcoe->netdev;
2510 struct fc_lport *vn_port;
2511
2512 mutex_lock(&fcoe_config_mutex);
2513 vn_port = fcoe_if_create(fcoe, &vport->dev, 1);
2514 mutex_unlock(&fcoe_config_mutex);
2515
2516 if (IS_ERR(vn_port)) {
2517 printk(KERN_ERR "fcoe: fcoe_vport_create(%s) failed\n",
2518 netdev->name);
2519 return -EIO;
2520 }
2521
2522 if (disabled) {
2523 fc_vport_set_state(vport, FC_VPORT_DISABLED);
2524 } else {
2525 vn_port->boot_time = jiffies;
2526 fc_fabric_login(vn_port);
2527 fc_vport_setlink(vn_port);
2528 }
2529 return 0;
2530}
2531
2532/**
2533 * fcoe_vport_destroy() - destroy the fc_host/scsi_host for a vport
2534 * @vport: fc_vport object that is being destroyed
2535 *
2536 * Returns: 0 for success
2537 */
2538static int fcoe_vport_destroy(struct fc_vport *vport)
2539{
2540 struct Scsi_Host *shost = vport_to_shost(vport);
2541 struct fc_lport *n_port = shost_priv(shost);
2542 struct fc_lport *vn_port = vport->dd_data;
2543 struct fcoe_port *port = lport_priv(vn_port);
2544
2545 mutex_lock(&n_port->lp_mutex);
2546 list_del(&vn_port->list);
2547 mutex_unlock(&n_port->lp_mutex);
2548 schedule_work(&port->destroy_work);
2549 return 0;
2550}
2551
2552/**
2553 * fcoe_vport_disable() - change vport state
2554 * @vport: vport to bring online/offline
2555 * @disable: should the vport be disabled?
2556 */
2557static int fcoe_vport_disable(struct fc_vport *vport, bool disable)
2558{
2559 struct fc_lport *lport = vport->dd_data;
2560
2561 if (disable) {
2562 fc_vport_set_state(vport, FC_VPORT_DISABLED);
2563 fc_fabric_logoff(lport);
2564 } else {
2565 lport->boot_time = jiffies;
2566 fc_fabric_login(lport);
2567 fc_vport_setlink(lport);
2568 }
2569
2570 return 0;
2571}
2572
Chris Leechdc8596d2009-11-03 11:47:18 -08002573/**
2574 * fcoe_vport_set_symbolic_name() - append vport string to symbolic name
2575 * @vport: fc_vport with a new symbolic name string
2576 *
2577 * After generating a new symbolic name string, a new RSPN_ID request is
2578 * sent to the name server. There is no response handler, so if it fails
2579 * for some reason it will not be retried.
2580 */
2581static void fcoe_set_vport_symbolic_name(struct fc_vport *vport)
2582{
2583 struct fc_lport *lport = vport->dd_data;
2584 struct fc_frame *fp;
2585 size_t len;
2586
2587 snprintf(fc_host_symbolic_name(lport->host), FC_SYMBOLIC_NAME_SIZE,
2588 "%s v%s over %s : %s", FCOE_NAME, FCOE_VERSION,
2589 fcoe_netdev(lport)->name, vport->symbolic_name);
2590
2591 if (lport->state != LPORT_ST_READY)
2592 return;
2593
2594 len = strnlen(fc_host_symbolic_name(lport->host), 255);
2595 fp = fc_frame_alloc(lport,
2596 sizeof(struct fc_ct_hdr) +
2597 sizeof(struct fc_ns_rspn) + len);
2598 if (!fp)
2599 return;
2600 lport->tt.elsct_send(lport, FC_FID_DIR_SERV, fp, FC_NS_RSPN_ID,
Joe Eykholtb94f8952009-11-03 11:50:21 -08002601 NULL, NULL, 3 * lport->r_a_tov);
Chris Leechdc8596d2009-11-03 11:47:18 -08002602}
Yi Zoub84056b2009-11-20 14:55:19 -08002603
2604/**
2605 * fcoe_get_lesb() - Fill the FCoE Link Error Status Block
2606 * @lport: the local port
2607 * @fc_lesb: the link error status block
2608 */
2609static void fcoe_get_lesb(struct fc_lport *lport,
2610 struct fc_els_lesb *fc_lesb)
2611{
2612 unsigned int cpu;
2613 u32 lfc, vlfc, mdac;
2614 struct fcoe_dev_stats *devst;
2615 struct fcoe_fc_els_lesb *lesb;
2616 struct net_device *netdev = fcoe_netdev(lport);
2617
2618 lfc = 0;
2619 vlfc = 0;
2620 mdac = 0;
2621 lesb = (struct fcoe_fc_els_lesb *)fc_lesb;
2622 memset(lesb, 0, sizeof(*lesb));
2623 for_each_possible_cpu(cpu) {
2624 devst = per_cpu_ptr(lport->dev_stats, cpu);
2625 lfc += devst->LinkFailureCount;
2626 vlfc += devst->VLinkFailureCount;
2627 mdac += devst->MissDiscAdvCount;
2628 }
2629 lesb->lesb_link_fail = htonl(lfc);
2630 lesb->lesb_vlink_fail = htonl(vlfc);
2631 lesb->lesb_miss_fka = htonl(mdac);
2632 lesb->lesb_fcs_error = htonl(dev_get_stats(netdev)->rx_crc_errors);
2633}