include cleanup: Update gfp.h and slab.h includes to prepare for breaking implicit...
[GitHub/mt8127/android_kernel_alcatel_ttab.git] / drivers / scsi / fcoe / fcoe.c
CommitLineData
85b4aa49 1/*
af7f85d9 2 * Copyright(c) 2007 - 2009 Intel Corporation. All rights reserved.
85b4aa49
RL
3 *
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms and conditions of the GNU General Public License,
6 * version 2, as published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope it will be useful, but WITHOUT
9 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
10 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
11 * more details.
12 *
13 * You should have received a copy of the GNU General Public License along with
14 * this program; if not, write to the Free Software Foundation, Inc.,
15 * 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
16 *
17 * Maintained at www.Open-FCoE.org
18 */
19
20#include <linux/module.h>
21#include <linux/version.h>
85b4aa49 22#include <linux/spinlock.h>
85b4aa49
RL
23#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>
85b4aa49 28#include <linux/crc32.h>
5a0e3ad6 29#include <linux/slab.h>
85b4aa49
RL
30#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>
97c8389d 41#include <scsi/fc/fc_fip.h>
85b4aa49
RL
42
43#include <scsi/libfc.h>
44#include <scsi/fc_frame.h>
45#include <scsi/libfcoe.h>
85b4aa49 46
fdd78027 47#include "fcoe.h"
7f349142 48
85b4aa49
RL
49MODULE_AUTHOR("Open-FCoE.org");
50MODULE_DESCRIPTION("FCoE");
9b34ecff 51MODULE_LICENSE("GPL v2");
85b4aa49 52
05cc7390
YZ
53/* 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
dfc1d0fe
CL
59DEFINE_MUTEX(fcoe_config_mutex);
60
e7a51997
JE
61/* fcoe_percpu_clean completion. Waiter protected by fcoe_create_mutex */
62static DECLARE_COMPLETION(fcoe_flush_completion);
63
85b4aa49 64/* fcoe host list */
090eb6c4 65/* must only by accessed under the RTNL mutex */
85b4aa49 66LIST_HEAD(fcoe_hostlist);
5e5e92df 67DEFINE_PER_CPU(struct fcoe_percpu_s, fcoe_percpu);
85b4aa49 68
dd3fd72e 69/* Function Prototypes */
1875f27e 70static int fcoe_reset(struct Scsi_Host *);
fdd78027
VD
71static 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 *);
1875f27e
RL
74static 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 *);
fdd78027
VD
78
79static struct fc_lport *fcoe_hostlist_lookup(const struct net_device *);
80static int fcoe_hostlist_add(const struct fc_lport *);
fdd78027 81
4bb6b515 82static void fcoe_check_wait_queue(struct fc_lport *, struct sk_buff *);
85b4aa49
RL
83static int fcoe_device_notification(struct notifier_block *, ulong, void *);
84static void fcoe_dev_setup(void);
85static void fcoe_dev_cleanup(void);
1875f27e
RL
86static struct fcoe_interface
87*fcoe_hostlist_lookup_port(const struct net_device *);
88
89static 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 *);
55a66d3c
VD
105static int fcoe_enable(const char *, struct kernel_param *);
106static int fcoe_disable(const char *, struct kernel_param *);
1875f27e 107
1875f27e
RL
108static 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);
859b7b64 115static void fcoe_recv_frame(struct sk_buff *skb);
1875f27e 116
b84056bf
YZ
117static void fcoe_get_lesb(struct fc_lport *, struct fc_els_lesb *);
118
1875f27e
RL
119module_param_call(create, fcoe_create, NULL, NULL, S_IWUSR);
120__MODULE_PARM_TYPE(create, "string");
55a66d3c 121MODULE_PARM_DESC(create, " Creates fcoe instance on a ethernet interface");
1875f27e
RL
122module_param_call(destroy, fcoe_destroy, NULL, NULL, S_IWUSR);
123__MODULE_PARM_TYPE(destroy, "string");
55a66d3c
VD
124MODULE_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.");
1875f27e
RL
131
132/* notification function for packets from net device */
85b4aa49
RL
133static struct notifier_block fcoe_notifier = {
134 .notifier_call = fcoe_device_notification,
135};
136
1875f27e
RL
137/* notification function for CPU hotplug events */
138static struct notifier_block fcoe_cpu_notifier = {
139 .notifier_call = fcoe_cpu_callback,
140};
141
e9084bb8
CL
142static struct scsi_transport_template *fcoe_transport_template;
143static struct scsi_transport_template *fcoe_vport_transport_template;
7f349142 144
1875f27e
RL
145static 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,
b84056bf 155 .get_lesb = fcoe_get_lesb,
1875f27e 156};
9a05753b 157
7f349142
VD
158struct 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,
9a05753b
CL
189
190 .vport_create = fcoe_vport_create,
191 .vport_delete = fcoe_vport_destroy,
192 .vport_disable = fcoe_vport_disable,
dc8596d3 193 .set_vport_symbolic_name = fcoe_set_vport_symbolic_name,
a51ab396
SM
194
195 .bsg_request = fc_lport_bsg_request,
7f349142
VD
196};
197
e9084bb8
CL
198struct 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,
a51ab396
SM
229
230 .bsg_request = fc_lport_bsg_request,
e9084bb8
CL
231};
232
7f349142
VD
233static 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,
14caf44c 245 .cmd_per_lun = 3,
7f349142
VD
246 .can_queue = FCOE_MAX_OUTSTANDING_COMMANDS,
247 .use_clustering = ENABLE_CLUSTERING,
248 .sg_tablesize = SG_ALL,
249 .max_sectors = 0xffff,
250};
251
54b649f8 252/**
1875f27e
RL
253 * fcoe_interface_setup() - Setup a FCoE interface
254 * @fcoe: The new FCoE interface
255 * @netdev: The net device that the fcoe interface is on
54b649f8
CL
256 *
257 * Returns : 0 for success
2e70e241 258 * Locking: must be called with the RTNL mutex held
54b649f8
CL
259 */
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;
5bab87e6 265 struct net_device *real_dev;
54b649f8 266 u8 flogi_maddr[ETH_ALEN];
b7a727f1 267 const struct net_device_ops *ops;
54b649f8
CL
268
269 fcoe->netdev = netdev;
270
b7a727f1
YZ
271 /* 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
54b649f8
CL
279 /* 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)) {
59d92516 283 FCOE_NETDEV_DBG(netdev, "Bonded interfaces not supported\n");
54b649f8
CL
284 return -EOPNOTSUPP;
285 }
286
287 /* look for SAN MAC address, if multiple SAN MACs exist, only
288 * use the first one for SPMA */
5bab87e6
YZ
289 real_dev = (netdev->priv_flags & IFF_802_1Q_VLAN) ?
290 vlan_dev_real_dev(netdev) : netdev;
54b649f8 291 rcu_read_lock();
5bab87e6 292 for_each_dev_addr(real_dev, ha) {
54b649f8 293 if ((ha->type == NETDEV_HW_ADDR_T_SAN) &&
bf361707 294 (is_valid_ether_addr(ha->addr))) {
54b649f8
CL
295 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 */
54b649f8
CL
311 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);
54b649f8
CL
316
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
030f4e00 334/**
1875f27e
RL
335 * fcoe_interface_create() - Create a FCoE interface on a net device
336 * @netdev: The net device to create the FCoE interface on
030f4e00
CL
337 *
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;
59d92516 343 int err;
030f4e00
CL
344
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
2e70e241 351 dev_hold(netdev);
030f4e00 352 kref_init(&fcoe->kref);
54b649f8
CL
353
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;
11b56188 360 fcoe->ctlr.get_src_addr = fcoe_get_src_mac;
54b649f8 361
59d92516 362 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 }
030f4e00
CL
369
370 return fcoe;
371}
372
54b649f8 373/**
1875f27e
RL
374 * fcoe_interface_cleanup() - Clean up a FCoE interface
375 * @fcoe: The FCoE interface to be cleaned up
2e70e241
CL
376 *
377 * Caller must be holding the RTNL mutex
54b649f8
CL
378 */
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];
b7a727f1 384 const struct net_device_ops *ops;
54b649f8
CL
385
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
54b649f8 396 /* Delete secondary MAC addresses */
54b649f8
CL
397 memcpy(flogi_maddr, (u8[6]) FC_FCOE_FLOGI_MAC, ETH_ALEN);
398 dev_unicast_delete(netdev, flogi_maddr);
54b649f8
CL
399 if (fip->spma)
400 dev_unicast_delete(netdev, fip->ctl_src_addr);
401 dev_mc_delete(netdev, FIP_ALL_ENODE_MACS, ETH_ALEN, 0);
b7a727f1
YZ
402
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 }
54b649f8
CL
410}
411
030f4e00
CL
412/**
413 * fcoe_interface_release() - fcoe_port kref release function
1875f27e 414 * @kref: Embedded reference count in an fcoe_interface struct
030f4e00
CL
415 */
416static void fcoe_interface_release(struct kref *kref)
417{
418 struct fcoe_interface *fcoe;
2e70e241 419 struct net_device *netdev;
030f4e00
CL
420
421 fcoe = container_of(kref, struct fcoe_interface, kref);
2e70e241
CL
422 netdev = fcoe->netdev;
423 /* tear-down the FCoE controller */
424 fcoe_ctlr_destroy(&fcoe->ctlr);
030f4e00 425 kfree(fcoe);
2e70e241 426 dev_put(netdev);
030f4e00
CL
427}
428
429/**
1875f27e
RL
430 * fcoe_interface_get() - Get a reference to a FCoE interface
431 * @fcoe: The FCoE interface to be held
030f4e00
CL
432 */
433static inline void fcoe_interface_get(struct fcoe_interface *fcoe)
434{
435 kref_get(&fcoe->kref);
436}
437
438/**
1875f27e
RL
439 * fcoe_interface_put() - Put a reference to a FCoE interface
440 * @fcoe: The FCoE interface to be released
030f4e00
CL
441 */
442static inline void fcoe_interface_put(struct fcoe_interface *fcoe)
443{
444 kref_put(&fcoe->kref, fcoe_interface_release);
445}
446
ab6b85c1 447/**
1875f27e
RL
448 * 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)
ab6b85c1
VD
454 *
455 * Returns: 0 for success
456 */
1875f27e 457static int fcoe_fip_recv(struct sk_buff *skb, struct net_device *netdev,
ab6b85c1
VD
458 struct packet_type *ptype,
459 struct net_device *orig_dev)
460{
259ad85d 461 struct fcoe_interface *fcoe;
ab6b85c1 462
259ad85d 463 fcoe = container_of(ptype, struct fcoe_interface, fip_packet_type);
3fe9a0ba 464 fcoe_ctlr_recv(&fcoe->ctlr, skb);
ab6b85c1
VD
465 return 0;
466}
467
468/**
1875f27e
RL
469 * fcoe_fip_send() - Send an Ethernet-encapsulated FIP frame
470 * @fip: The FCoE controller
471 * @skb: The FIP packet to be sent
ab6b85c1
VD
472 */
473static void fcoe_fip_send(struct fcoe_ctlr *fip, struct sk_buff *skb)
474{
3fe9a0ba 475 skb->dev = fcoe_from_ctlr(fip)->netdev;
ab6b85c1
VD
476 dev_queue_xmit(skb);
477}
478
479/**
1875f27e
RL
480 * 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
ab6b85c1
VD
483 *
484 * Remove any previously-set unicast MAC filter.
485 * Add secondary FCoE MAC address filter for our OUI.
486 */
11b56188 487static void fcoe_update_src_mac(struct fc_lport *lport, u8 *addr)
ab6b85c1 488{
11b56188
CL
489 struct fcoe_port *port = lport_priv(lport);
490 struct fcoe_interface *fcoe = port->fcoe;
25024989 491
ab6b85c1 492 rtnl_lock();
11b56188
CL
493 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);
ab6b85c1
VD
498 rtnl_unlock();
499}
500
11b56188
CL
501/**
502 * 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
7f349142 512/**
1875f27e
RL
513 * fcoe_lport_config() - Set up a local port
514 * @lport: The local port to be setup
7f349142
VD
515 *
516 * Returns: 0 for success
517 */
1875f27e 518static int fcoe_lport_config(struct fc_lport *lport)
7f349142 519{
1875f27e
RL
520 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;
529
530 fc_lport_init_stats(lport);
7f349142
VD
531
532 /* lport fc_lport related configuration */
1875f27e 533 fc_lport_config(lport);
7f349142
VD
534
535 /* offload related configuration */
1875f27e
RL
536 lport->crc_offload = 0;
537 lport->seq_offload = 0;
538 lport->lro_enabled = 0;
539 lport->lro_xid = 0;
540 lport->lso_max = 0;
7f349142
VD
541
542 return 0;
543}
544
1047f221 545/**
1875f27e
RL
546 * fcoe_queue_timer() - The fcoe queue timer
547 * @lport: The local port
1047f221
VD
548 *
549 * Calls fcoe_check_wait_queue on timeout
1047f221 550 */
1875f27e 551static void fcoe_queue_timer(ulong lport)
1047f221 552{
1875f27e 553 fcoe_check_wait_queue((struct fc_lport *)lport, NULL);
1047f221
VD
554}
555
dcece412
YZ
556/**
557 * 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
7f349142 573/**
1875f27e
RL
574 * 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
7f349142 577 *
1875f27e 578 * Must be called after fcoe_lport_config() as it will use local port mutex
7f349142 579 *
1875f27e 580 * Returns: 0 for success
7f349142 581 */
1875f27e 582static int fcoe_netdev_config(struct fc_lport *lport, struct net_device *netdev)
7f349142
VD
583{
584 u32 mfs;
585 u64 wwnn, wwpn;
25024989 586 struct fcoe_interface *fcoe;
014f5c3f 587 struct fcoe_port *port;
cc0136c2 588 int vid = 0;
7f349142
VD
589
590 /* Setup lport private data to point to fcoe softc */
1875f27e 591 port = lport_priv(lport);
25024989 592 fcoe = port->fcoe;
7f349142
VD
593
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 */
7221d7e5
YZ
599 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));
1875f27e 605 if (fc_set_mfs(lport, mfs))
7f349142
VD
606 return -EINVAL;
607
7f349142 608 /* offload features support */
1d1b88dc 609 if (netdev->features & NETIF_F_SG)
1875f27e 610 lport->sg_supp = 1;
7f349142 611
7f349142 612 if (netdev->features & NETIF_F_FCOE_CRC) {
1875f27e 613 lport->crc_offload = 1;
d5488eb9 614 FCOE_NETDEV_DBG(netdev, "Supports FCCRC offload\n");
7f349142 615 }
7f349142 616 if (netdev->features & NETIF_F_FSO) {
1875f27e
RL
617 lport->seq_offload = 1;
618 lport->lso_max = netdev->gso_max_size;
d5488eb9 619 FCOE_NETDEV_DBG(netdev, "Supports LSO for max len 0x%x\n",
1875f27e 620 lport->lso_max);
7f349142 621 }
7f349142 622 if (netdev->fcoe_ddp_xid) {
1875f27e
RL
623 lport->lro_enabled = 1;
624 lport->lro_xid = netdev->fcoe_ddp_xid;
d5488eb9 625 FCOE_NETDEV_DBG(netdev, "Supports LRO for max xid 0x%x\n",
1875f27e 626 lport->lro_xid);
7f349142 627 }
014f5c3f
CL
628 skb_queue_head_init(&port->fcoe_pending_queue);
629 port->fcoe_pending_queue_active = 0;
1875f27e 630 setup_timer(&port->timer, fcoe_queue_timer, (unsigned long)lport);
7f349142 631
1875f27e 632 if (!lport->vport) {
cc0136c2
YZ
633 /*
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);
dcece412
YZ
640
641 if (fcoe_get_wwn(netdev, &wwnn, NETDEV_FCOE_WWNN))
642 wwnn = fcoe_wwn_from_mac(fcoe->ctlr.ctl_src_addr, 1, 0);
1875f27e 643 fc_set_wwnn(lport, wwnn);
dcece412
YZ
644 if (fcoe_get_wwn(netdev, &wwpn, NETDEV_FCOE_WWPN))
645 wwpn = fcoe_wwn_from_mac(fcoe->ctlr.ctl_src_addr,
646 2, vid);
1875f27e 647 fc_set_wwpn(lport, wwpn);
9a05753b 648 }
7f349142 649
7f349142
VD
650 return 0;
651}
652
653/**
1875f27e
RL
654 * 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
7f349142
VD
658 *
659 * Must be called after fcoe_lport_config() and fcoe_netdev_config()
660 *
1875f27e 661 * Returns: 0 for success
7f349142 662 */
1875f27e
RL
663static int fcoe_shost_config(struct fc_lport *lport, struct Scsi_Host *shost,
664 struct device *dev)
7f349142
VD
665{
666 int rc = 0;
667
668 /* lport scsi host config */
1875f27e
RL
669 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;
e9084bb8 674 else
1875f27e 675 lport->host->transportt = fcoe_transport_template;
7f349142
VD
676
677 /* add the new host to the SCSI-ml */
1875f27e 678 rc = scsi_add_host(lport->host, dev);
7f349142 679 if (rc) {
1875f27e 680 FCOE_NETDEV_DBG(fcoe_netdev(lport), "fcoe_shost_config: "
d5488eb9 681 "error on scsi_add_host\n");
7f349142
VD
682 return rc;
683 }
9a05753b 684
1875f27e
RL
685 if (!lport->vport)
686 fc_host_max_npiv_vports(lport->host) = USHORT_MAX;
9a05753b 687
1875f27e 688 snprintf(fc_host_symbolic_name(lport->host), FC_SYMBOLIC_NAME_SIZE,
5baa17c3 689 "%s v%s over %s", FCOE_NAME, FCOE_VERSION,
1875f27e 690 fcoe_netdev(lport)->name);
7f349142
VD
691
692 return 0;
693}
694
1875f27e
RL
695/**
696 * fcoe_oem_match() - The match routine for the offloaded exchange manager
697 * @fp: The I/O frame
d7179680 698 *
1875f27e
RL
699 * 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.
d7179680
VD
709 */
710bool fcoe_oem_match(struct fc_frame *fp)
711{
05cc7390
YZ
712 return fc_fcp_is_read(fr_fsp(fp)) &&
713 (fr_fsp(fp)->data_len > fcoe_ddp_min);
d7179680
VD
714}
715
7f349142 716/**
1875f27e
RL
717 * fcoe_em_config() - Allocate and configure an exchange manager
718 * @lport: The local port that the new EM will be associated with
7f349142 719 *
1875f27e 720 * Returns: 0 on success
7f349142 721 */
1875f27e 722static inline int fcoe_em_config(struct fc_lport *lport)
7f349142 723{
1875f27e 724 struct fcoe_port *port = lport_priv(lport);
25024989
CL
725 struct fcoe_interface *fcoe = port->fcoe;
726 struct fcoe_interface *oldfcoe = NULL;
1d1b88dc 727 struct net_device *old_real_dev, *cur_real_dev;
d7179680
VD
728 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 */
1875f27e
RL
735 if (!lport->lro_enabled || !lport->lro_xid ||
736 (lport->lro_xid >= max_xid)) {
737 lport->lro_xid = 0;
d7179680
VD
738 goto skip_oem;
739 }
740
741 /*
742 * Reuse existing offload em instance in case
1d1b88dc 743 * it is already allocated on real eth device
d7179680 744 */
25024989
CL
745 if (fcoe->netdev->priv_flags & IFF_802_1Q_VLAN)
746 cur_real_dev = vlan_dev_real_dev(fcoe->netdev);
1d1b88dc 747 else
25024989 748 cur_real_dev = fcoe->netdev;
1d1b88dc 749
25024989 750 list_for_each_entry(oldfcoe, &fcoe_hostlist, list) {
25024989
CL
751 if (oldfcoe->netdev->priv_flags & IFF_802_1Q_VLAN)
752 old_real_dev = vlan_dev_real_dev(oldfcoe->netdev);
1d1b88dc 753 else
25024989 754 old_real_dev = oldfcoe->netdev;
1d1b88dc
VD
755
756 if (cur_real_dev == old_real_dev) {
991cbb60 757 fcoe->oem = oldfcoe->oem;
d7179680
VD
758 break;
759 }
760 }
761
991cbb60 762 if (fcoe->oem) {
1875f27e 763 if (!fc_exch_mgr_add(lport, fcoe->oem, fcoe_oem_match)) {
d7179680
VD
764 printk(KERN_ERR "fcoe_em_config: failed to add "
765 "offload em:%p on interface:%s\n",
991cbb60 766 fcoe->oem, fcoe->netdev->name);
d7179680
VD
767 return -ENOMEM;
768 }
769 } else {
1875f27e
RL
770 fcoe->oem = fc_exch_mgr_alloc(lport, FC_CLASS_3,
771 FCOE_MIN_XID, lport->lro_xid,
772 fcoe_oem_match);
991cbb60 773 if (!fcoe->oem) {
d7179680
VD
774 printk(KERN_ERR "fcoe_em_config: failed to allocate "
775 "em for offload exches on interface:%s\n",
25024989 776 fcoe->netdev->name);
d7179680
VD
777 return -ENOMEM;
778 }
779 }
780
781 /*
782 * Exclude offload EM xid range from next EM xid range.
783 */
1875f27e 784 min_xid += lport->lro_xid + 1;
d7179680
VD
785
786skip_oem:
1875f27e 787 if (!fc_exch_mgr_alloc(lport, FC_CLASS_3, min_xid, max_xid, NULL)) {
d7179680 788 printk(KERN_ERR "fcoe_em_config: failed to "
25024989 789 "allocate em on interface %s\n", fcoe->netdev->name);
7f349142 790 return -ENOMEM;
d7179680 791 }
7f349142
VD
792
793 return 0;
794}
795
796/**
1875f27e
RL
797 * fcoe_if_destroy() - Tear down a SW FCoE instance
798 * @lport: The local port to be destroyed
7f349142 799 */
af7f85d9 800static void fcoe_if_destroy(struct fc_lport *lport)
7f349142 801{
014f5c3f
CL
802 struct fcoe_port *port = lport_priv(lport);
803 struct fcoe_interface *fcoe = port->fcoe;
25024989 804 struct net_device *netdev = fcoe->netdev;
7f349142 805
d5488eb9 806 FCOE_NETDEV_DBG(netdev, "Destroying interface\n");
7f349142 807
7f349142 808 /* Logout of the fabric */
af7f85d9 809 fc_fabric_logoff(lport);
7f349142 810
54b649f8
CL
811 /* Cleanup the fc_lport */
812 fc_lport_destroy(lport);
813 fc_fcp_destroy(lport);
814
815 /* Stop the transmit retry timer */
816 del_timer_sync(&port->timer);
ab6b85c1 817
54b649f8
CL
818 /* Free existing transmit skbs */
819 fcoe_clean_pending_queue(lport);
820
11b56188
CL
821 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
54b649f8
CL
826 /* receives may not be stopped until after this */
827 fcoe_interface_put(fcoe);
7f349142 828
f161fb72 829 /* Free queued packets for the per-CPU receive threads */
af7f85d9 830 fcoe_percpu_clean(lport);
f161fb72 831
7f349142 832 /* Detach from the scsi-ml */
af7f85d9
CL
833 fc_remove_host(lport->host);
834 scsi_remove_host(lport->host);
7f349142
VD
835
836 /* There are no more rports or I/O, free the EM */
af7f85d9 837 fc_exch_mgr_free(lport);
7f349142 838
7f349142 839 /* Free memory used by statistical counters */
af7f85d9 840 fc_lport_free_stats(lport);
7f349142 841
2e70e241 842 /* Release the Scsi_Host */
af7f85d9 843 scsi_host_put(lport->host);
7f349142
VD
844}
845
1875f27e
RL
846/**
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
7f349142 852 *
1875f27e 853 * Returns: 0 if the DDP context was not configured
7f349142 854 */
1875f27e
RL
855static int fcoe_ddp_setup(struct fc_lport *lport, u16 xid,
856 struct scatterlist *sgl, unsigned int sgc)
7f349142 857{
1875f27e 858 struct net_device *netdev = fcoe_netdev(lport);
7f349142 859
1875f27e
RL
860 if (netdev->netdev_ops->ndo_fcoe_ddp_setup)
861 return netdev->netdev_ops->ndo_fcoe_ddp_setup(netdev,
862 xid, sgl,
863 sgc);
7f349142
VD
864
865 return 0;
866}
867
1875f27e
RL
868/**
869 * 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
7f349142 872 *
1875f27e 873 * Returns: the length of data that have been completed by DDP
7f349142 874 */
1875f27e 875static int fcoe_ddp_done(struct fc_lport *lport, u16 xid)
7f349142 876{
1875f27e 877 struct net_device *netdev = fcoe_netdev(lport);
7f349142 878
1875f27e
RL
879 if (netdev->netdev_ops->ndo_fcoe_ddp_done)
880 return netdev->netdev_ops->ndo_fcoe_ddp_done(netdev, xid);
7f349142
VD
881 return 0;
882}
883
7f349142 884/**
1875f27e
RL
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
7f349142 889 *
1875f27e 890 * Creates a fc_lport instance and a Scsi_Host instance and configure them.
7f349142 891 *
1875f27e 892 * Returns: The allocated fc_lport or an error pointer
7f349142 893 */
030f4e00 894static struct fc_lport *fcoe_if_create(struct fcoe_interface *fcoe,
9a05753b 895 struct device *parent, int npiv)
7f349142 896{
1875f27e 897 struct net_device *netdev = fcoe->netdev;
af7f85d9 898 struct fc_lport *lport = NULL;
014f5c3f 899 struct fcoe_port *port;
7f349142 900 struct Scsi_Host *shost;
1875f27e 901 int rc;
9a05753b
CL
902 /*
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);
7f349142 907
d5488eb9 908 FCOE_NETDEV_DBG(netdev, "Create Interface\n");
7f349142 909
9a05753b
CL
910 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 }
86221969 917 if (!lport) {
d5488eb9 918 FCOE_NETDEV_DBG(netdev, "Could not allocate host structure\n");
af7f85d9 919 rc = -ENOMEM;
030f4e00 920 goto out;
7f349142 921 }
86221969 922 shost = lport->host;
014f5c3f 923 port = lport_priv(lport);
2e70e241 924 port->lport = lport;
014f5c3f 925 port->fcoe = fcoe;
2e70e241 926 INIT_WORK(&port->destroy_work, fcoe_destroy_work);
7f349142 927
1875f27e 928 /* configure a fc_lport including the exchange manager */
af7f85d9 929 rc = fcoe_lport_config(lport);
7f349142 930 if (rc) {
d5488eb9
RL
931 FCOE_NETDEV_DBG(netdev, "Could not configure lport for the "
932 "interface\n");
7f349142
VD
933 goto out_host_put;
934 }
935
9a05753b
CL
936 if (npiv) {
937 FCOE_NETDEV_DBG(netdev, "Setting vport names, 0x%llX 0x%llX\n",
1875f27e 938 vport->node_name, vport->port_name);
9a05753b
CL
939 fc_set_wwnn(lport, vport->node_name);
940 fc_set_wwpn(lport, vport->port_name);
941 }
942
ab6b85c1 943 /* configure lport network properties */
af7f85d9 944 rc = fcoe_netdev_config(lport, netdev);
ab6b85c1 945 if (rc) {
d5488eb9
RL
946 FCOE_NETDEV_DBG(netdev, "Could not configure netdev for the "
947 "interface\n");
54b649f8 948 goto out_lp_destroy;
ab6b85c1 949 }
97c8389d 950
7f349142 951 /* configure lport scsi host properties */
af7f85d9 952 rc = fcoe_shost_config(lport, shost, parent);
7f349142 953 if (rc) {
d5488eb9
RL
954 FCOE_NETDEV_DBG(netdev, "Could not configure shost for the "
955 "interface\n");
54b649f8 956 goto out_lp_destroy;
7f349142
VD
957 }
958
96316099 959 /* Initialize the library */
af7f85d9 960 rc = fcoe_libfc_config(lport, &fcoe_libfc_fcn_templ);
7f349142 961 if (rc) {
96316099 962 FCOE_NETDEV_DBG(netdev, "Could not configure libfc for the "
d5488eb9 963 "interface\n");
96316099 964 goto out_lp_destroy;
7f349142
VD
965 }
966
9a05753b
CL
967 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 */
c863df33 977
9a05753b
CL
978 /* lport exch manager allocation */
979 rc = fcoe_em_config(lport);
980 if (rc) {
981 FCOE_NETDEV_DBG(netdev, "Could not configure the EM "
1875f27e 982 "for the interface\n");
9a05753b
CL
983 goto out_lp_destroy;
984 }
7f349142
VD
985 }
986
030f4e00 987 fcoe_interface_get(fcoe);
af7f85d9 988 return lport;
7f349142
VD
989
990out_lp_destroy:
af7f85d9 991 fc_exch_mgr_free(lport);
7f349142 992out_host_put:
af7f85d9
CL
993 scsi_host_put(lport->host);
994out:
995 return ERR_PTR(rc);
7f349142
VD
996}
997
998/**
1875f27e 999 * fcoe_if_init() - Initialization routine for fcoe.ko
7f349142 1000 *
1875f27e
RL
1001 * Attaches the SW FCoE transport to the FC transport
1002 *
1003 * Returns: 0 on success
7f349142
VD
1004 */
1005static int __init fcoe_if_init(void)
1006{
1007 /* attach to scsi transport */
e9084bb8
CL
1008 fcoe_transport_template = fc_attach_transport(&fcoe_transport_function);
1009 fcoe_vport_transport_template =
1010 fc_attach_transport(&fcoe_vport_transport_function);
7f349142 1011
e9084bb8 1012 if (!fcoe_transport_template) {
d5488eb9 1013 printk(KERN_ERR "fcoe: Failed to attach to the FC transport\n");
7f349142
VD
1014 return -ENODEV;
1015 }
1016
1017 return 0;
1018}
1019
1020/**
1875f27e
RL
1021 * fcoe_if_exit() - Tear down fcoe.ko
1022 *
1023 * Detaches the SW FCoE transport from the FC transport
7f349142 1024 *
1875f27e 1025 * Returns: 0 on success
7f349142
VD
1026 */
1027int __exit fcoe_if_exit(void)
1028{
e9084bb8
CL
1029 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;
7f349142
VD
1033 return 0;
1034}
1035
8976f424 1036/**
1875f27e
RL
1037 * 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
8976f424
RL
1039 */
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
e7a51997 1050 if (likely(!IS_ERR(thread))) {
8976f424
RL
1051 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/**
1875f27e
RL
1061 * 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
8976f424
RL
1063 *
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
d5488eb9 1079 FCOE_DBG("Destroying receive thread for CPU %d\n", cpu);
8976f424
RL
1080
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) {
d5488eb9
RL
1101 FCOE_DBG("Moving frames from CPU %d to CPU %d\n",
1102 cpu, targ_cpu);
8976f424
RL
1103
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
1875f27e 1110 * new skbs. Unlock the targeted CPU and drop the skbs
8976f424
RL
1111 * 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 /*
dd3fd72e 1134 * This a non-SMP scenario where the singular Rx thread is
8976f424
RL
1135 * 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/**
1875f27e
RL
1151 * 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
8976f424 1155 *
1875f27e 1156 * This creates or destroys per-CPU data for fcoe
8976f424
RL
1157 *
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:
d5488eb9 1168 FCOE_DBG("CPU %x online: Create Rx thread\n", cpu);
8976f424
RL
1169 fcoe_percpu_thread_create(cpu);
1170 break;
1171 case CPU_DEAD:
1172 case CPU_DEAD_FROZEN:
d5488eb9 1173 FCOE_DBG("CPU %x offline: Remove Rx thread\n", cpu);
8976f424
RL
1174 fcoe_percpu_thread_destroy(cpu);
1175 break;
1176 default:
1177 break;
1178 }
1179 return NOTIFY_OK;
1180}
1181
85b4aa49 1182/**
1875f27e
RL
1183 * 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
85b4aa49 1188 *
1875f27e
RL
1189 * This routine is called by NET_RX_SOFTIRQ. It receives a packet, builds a
1190 * FC frame and passes the frame to libfc.
85b4aa49
RL
1191 *
1192 * Returns: 0 for success
34f42a07 1193 */
1875f27e 1194int fcoe_rcv(struct sk_buff *skb, struct net_device *netdev,
85b4aa49
RL
1195 struct packet_type *ptype, struct net_device *olddev)
1196{
1875f27e 1197 struct fc_lport *lport;
85b4aa49 1198 struct fcoe_rcv_info *fr;
259ad85d 1199 struct fcoe_interface *fcoe;
85b4aa49 1200 struct fc_frame_header *fh;
85b4aa49 1201 struct fcoe_percpu_s *fps;
b2f0091f 1202 unsigned int cpu;
85b4aa49 1203
259ad85d 1204 fcoe = container_of(ptype, struct fcoe_interface, fcoe_packet_type);
1875f27e
RL
1205 lport = fcoe->ctlr.lp;
1206 if (unlikely(!lport)) {
1207 FCOE_NETDEV_DBG(netdev, "Cannot find hba structure");
85b4aa49
RL
1208 goto err2;
1209 }
1875f27e 1210 if (!lport->link_up)
97c8389d 1211 goto err2;
85b4aa49 1212
1875f27e 1213 FCOE_NETDEV_DBG(netdev, "skb_info: len:%d data_len:%d head:%p "
d5488eb9
RL
1214 "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>");
85b4aa49
RL
1218
1219 /* check for FCOE packet type */
1220 if (unlikely(eth_hdr(skb)->h_proto != htons(ETH_P_FCOE))) {
1875f27e 1221 FCOE_NETDEV_DBG(netdev, "Wrong FC type frame");
85b4aa49
RL
1222 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) ||
1875f27e 1230 !pskb_may_pull(skb, FCOE_HEADER_LEN)))
85b4aa49
RL
1231 goto err;
1232
1233 skb_set_transport_header(skb, sizeof(struct fcoe_hdr));
1234 fh = (struct fc_frame_header *) skb_transport_header(skb);
1235
85b4aa49 1236 fr = fcoe_dev_from_skb(skb);
1875f27e 1237 fr->fr_dev = lport;
85b4aa49 1238 fr->ptype = ptype;
5e5e92df 1239
85b4aa49 1240 /*
b2f0091f
VD
1241 * 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.
85b4aa49 1245 */
b2f0091f
VD
1246 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();
5e5e92df 1250
8976f424 1251 fps = &per_cpu(fcoe_percpu, cpu);
85b4aa49 1252 spin_lock_bh(&fps->fcoe_rx_list.lock);
8976f424
RL
1253 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 */
1875f27e 1259 FCOE_NETDEV_DBG(netdev, "CPU is online, but no receive thread "
d5488eb9
RL
1260 "ready for incoming skb- using first online "
1261 "CPU.\n");
8976f424
RL
1262
1263 spin_unlock_bh(&fps->fcoe_rx_list.lock);
6957177f 1264 cpu = cpumask_first(cpu_online_mask);
8976f424
RL
1265 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 */
85b4aa49 1278
859b7b64
CL
1279 /* 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 }
85b4aa49
RL
1297
1298 return 0;
1299err:
1875f27e 1300 fc_lport_get_stats(lport)->ErrorFrames++;
85b4aa49
RL
1301
1302err2:
1303 kfree_skb(skb);
1304 return -1;
1305}
85b4aa49
RL
1306
1307/**
1875f27e
RL
1308 * 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.
85b4aa49
RL
1313 *
1314 * Returns: 0 for success
34f42a07 1315 */
85b4aa49
RL
1316static inline int fcoe_start_io(struct sk_buff *skb)
1317{
18fa11ef 1318 struct sk_buff *nskb;
85b4aa49
RL
1319 int rc;
1320
18fa11ef
CL
1321 nskb = skb_clone(skb, GFP_ATOMIC);
1322 rc = dev_queue_xmit(nskb);
85b4aa49
RL
1323 if (rc != 0)
1324 return rc;
1325 kfree_skb(skb);
1326 return 0;
1327}
1328
1329/**
1875f27e
RL
1330 * 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.
85b4aa49
RL
1339 *
1340 * Returns: 0 for success
34f42a07 1341 */
85b4aa49
RL
1342static int fcoe_get_paged_crc_eof(struct sk_buff *skb, int tlen)
1343{
1344 struct fcoe_percpu_s *fps;
1345 struct page *page;
85b4aa49 1346
5e5e92df 1347 fps = &get_cpu_var(fcoe_percpu);
85b4aa49
RL
1348 page = fps->crc_eof_page;
1349 if (!page) {
1350 page = alloc_page(GFP_ATOMIC);
1351 if (!page) {
5e5e92df 1352 put_cpu_var(fcoe_percpu);
85b4aa49
RL
1353 return -ENOMEM;
1354 }
1355 fps->crc_eof_page = page;
8976f424 1356 fps->crc_eof_offset = 0;
85b4aa49
RL
1357 }
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 }
5e5e92df 1372 put_cpu_var(fcoe_percpu);
85b4aa49
RL
1373 return 0;
1374}
1375
1376/**
1875f27e
RL
1377 * fcoe_fc_crc() - Calculates the CRC for a given frame
1378 * @fp: The frame to be checksumed
85b4aa49 1379 *
1875f27e
RL
1380 * This uses crc32() routine to calculate the CRC for a frame
1381 *
1382 * Return: The 32 bit CRC value
34f42a07 1383 */
85b4aa49
RL
1384u32 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}
85b4aa49
RL
1411
1412/**
1875f27e
RL
1413 * 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
85b4aa49 1416 *
1875f27e 1417 * Return: 0 for success
34f42a07 1418 */
1875f27e 1419int fcoe_xmit(struct fc_lport *lport, struct fc_frame *fp)
85b4aa49 1420{
4bb6b515 1421 int wlen;
85b4aa49
RL
1422 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 */
1875f27e 1431 struct fcoe_port *port = lport_priv(lport);
3fe9a0ba 1432 struct fcoe_interface *fcoe = port->fcoe;
85b4aa49
RL
1433 u8 sof, eof;
1434 struct fcoe_hdr *hp;
1435
1436 WARN_ON((fr_len(fp) % sizeof(u32)) != 0);
1437
85b4aa49 1438 fh = fc_frame_header_get(fp);
97c8389d
JE
1439 skb = fp_skb(fp);
1440 wlen = skb->len / FCOE_WORD_TO_BYTE;
1441
1875f27e 1442 if (!lport->link_up) {
3caf02ee 1443 kfree_skb(skb);
97c8389d 1444 return 0;
85b4aa49
RL
1445 }
1446
97c8389d 1447 if (unlikely(fh->fh_r_ctl == FC_RCTL_ELS_REQ) &&
1875f27e 1448 fcoe_ctlr_els_send(&fcoe->ctlr, lport, skb))
97c8389d
JE
1449 return 0;
1450
85b4aa49
RL
1451 sof = fr_sof(fp);
1452 eof = fr_eof(fp);
1453
4e57e1cb 1454 elen = sizeof(struct ethhdr);
85b4aa49
RL
1455 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 */
1875f27e 1460 if (likely(lport->crc_offload)) {
39ca9a06 1461 skb->ip_summed = CHECKSUM_PARTIAL;
85b4aa49
RL
1462 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
014f5c3f 1470 /* copy port crc and eof to the skb buff */
85b4aa49
RL
1471 if (skb_is_nonlinear(skb)) {
1472 skb_frag_t *frag;
1473 if (fcoe_get_paged_crc_eof(skb, tlen)) {
e9041581 1474 kfree_skb(skb);
85b4aa49
RL
1475 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
014f5c3f 1493 /* adjust skb network/transport offsets to match mac/fcoe/port */
85b4aa49
RL
1494 skb_push(skb, elen + hlen);
1495 skb_reset_mac_header(skb);
1496 skb_reset_network_header(skb);
1497 skb->mac_len = elen;
211c738d 1498 skb->protocol = htons(ETH_P_FCOE);
3fe9a0ba 1499 skb->dev = fcoe->netdev;
85b4aa49
RL
1500
1501 /* fill up mac and fcoe headers */
1502 eh = eth_hdr(skb);
1503 eh->h_proto = htons(ETH_P_FCOE);
3fe9a0ba 1504 if (fcoe->ctlr.map_dest)
85b4aa49
RL
1505 fc_fcoe_set_mac(eh->h_dest, fh->fh_d_id);
1506 else
1507 /* insert GW address */
3fe9a0ba 1508 memcpy(eh->h_dest, fcoe->ctlr.dest_addr, ETH_ALEN);
85b4aa49 1509
3fe9a0ba
CL
1510 if (unlikely(fcoe->ctlr.flogi_oxid != FC_XID_UNKNOWN))
1511 memcpy(eh->h_source, fcoe->ctlr.ctl_src_addr, ETH_ALEN);
85b4aa49 1512 else
11b56188 1513 memcpy(eh->h_source, port->data_src_addr, ETH_ALEN);
85b4aa49
RL
1514
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
39ca9a06 1521 /* fcoe lso, mss is in max_payload which is non-zero for FCP data */
1875f27e 1522 if (lport->seq_offload && fr_max_payload(fp)) {
39ca9a06
YZ
1523 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 }
85b4aa49 1529 /* update tx stats: regardless if LLD fails */
1875f27e 1530 stats = fc_lport_get_stats(lport);
582b45bc
RL
1531 stats->TxFrames++;
1532 stats->TxWords += wlen;
85b4aa49
RL
1533
1534 /* send down to lld */
1875f27e 1535 fr_dev(fp) = lport;
014f5c3f 1536 if (port->fcoe_pending_queue.qlen)
1875f27e 1537 fcoe_check_wait_queue(lport, skb);
4bb6b515 1538 else if (fcoe_start_io(skb))
1875f27e 1539 fcoe_check_wait_queue(lport, skb);
85b4aa49
RL
1540
1541 return 0;
1542}
85b4aa49 1543
e7a51997 1544/**
1875f27e
RL
1545 * fcoe_percpu_flush_done() - Indicate per-CPU queue flush completion
1546 * @skb: The completed skb (argument required by destructor)
e7a51997
JE
1547 */
1548static void fcoe_percpu_flush_done(struct sk_buff *skb)
1549{
1550 complete(&fcoe_flush_completion);
1551}
1552
34f42a07 1553/**
859b7b64
CL
1554 * fcoe_recv_frame() - process a single received frame
1555 * @skb: frame to process
85b4aa49 1556 */
859b7b64 1557static void fcoe_recv_frame(struct sk_buff *skb)
85b4aa49 1558{
85b4aa49 1559 u32 fr_len;
1875f27e 1560 struct fc_lport *lport;
85b4aa49
RL
1561 struct fcoe_rcv_info *fr;
1562 struct fcoe_dev_stats *stats;
1563 struct fc_frame_header *fh;
85b4aa49
RL
1564 struct fcoe_crc_eof crc_eof;
1565 struct fc_frame *fp;
1566 u8 *mac = NULL;
014f5c3f 1567 struct fcoe_port *port;
85b4aa49
RL
1568 struct fcoe_hdr *hp;
1569
859b7b64
CL
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/**
1672 * fcoe_percpu_receive_thread() - The per-CPU packet receive thread
1673 * @arg: The per-CPU context
1674 *
1675 * Return: 0 for success
1676 */
1677int fcoe_percpu_receive_thread(void *arg)
1678{
1679 struct fcoe_percpu_s *p = arg;
1680 struct sk_buff *skb;
1681
4469c195 1682 set_user_nice(current, -20);
85b4aa49
RL
1683
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);
859b7b64 1697 fcoe_recv_frame(skb);
85b4aa49
RL
1698 }
1699 return 0;
1700}
1701
85b4aa49 1702/**
1875f27e
RL
1703 * fcoe_check_wait_queue() - Attempt to clear the transmit backlog
1704 * @lport: The local port whose backlog is to be cleared
85b4aa49 1705 *
1875f27e
RL
1706 * 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.
85b4aa49 1710 *
1875f27e
RL
1711 * 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
85b4aa49 1713 * by the next skb transmit.
34f42a07 1714 */
1875f27e 1715static void fcoe_check_wait_queue(struct fc_lport *lport, struct sk_buff *skb)
85b4aa49 1716{
1875f27e 1717 struct fcoe_port *port = lport_priv(lport);
4bb6b515 1718 int rc;
85b4aa49 1719
014f5c3f 1720 spin_lock_bh(&port->fcoe_pending_queue.lock);
4bb6b515
VD
1721
1722 if (skb)
014f5c3f 1723 __skb_queue_tail(&port->fcoe_pending_queue, skb);
4bb6b515 1724
014f5c3f 1725 if (port->fcoe_pending_queue_active)
c826a314 1726 goto out;
014f5c3f 1727 port->fcoe_pending_queue_active = 1;
55c8bafb 1728
014f5c3f 1729 while (port->fcoe_pending_queue.qlen) {
55c8bafb 1730 /* keep qlen > 0 until fcoe_start_io succeeds */
014f5c3f
CL
1731 port->fcoe_pending_queue.qlen++;
1732 skb = __skb_dequeue(&port->fcoe_pending_queue);
55c8bafb 1733
014f5c3f 1734 spin_unlock_bh(&port->fcoe_pending_queue.lock);
55c8bafb 1735 rc = fcoe_start_io(skb);
014f5c3f 1736 spin_lock_bh(&port->fcoe_pending_queue.lock);
55c8bafb
CL
1737
1738 if (rc) {
014f5c3f 1739 __skb_queue_head(&port->fcoe_pending_queue, skb);
55c8bafb 1740 /* undo temporary increment above */
014f5c3f 1741 port->fcoe_pending_queue.qlen--;
55c8bafb 1742 break;
85b4aa49 1743 }
55c8bafb 1744 /* undo temporary increment above */
014f5c3f 1745 port->fcoe_pending_queue.qlen--;
85b4aa49 1746 }
55c8bafb 1747
014f5c3f 1748 if (port->fcoe_pending_queue.qlen < FCOE_LOW_QUEUE_DEPTH)
1875f27e 1749 lport->qfull = 0;
014f5c3f
CL
1750 if (port->fcoe_pending_queue.qlen && !timer_pending(&port->timer))
1751 mod_timer(&port->timer, jiffies + 2);
1752 port->fcoe_pending_queue_active = 0;
c826a314 1753out:
014f5c3f 1754 if (port->fcoe_pending_queue.qlen > FCOE_MAX_QUEUE_DEPTH)
1875f27e 1755 lport->qfull = 1;
014f5c3f 1756 spin_unlock_bh(&port->fcoe_pending_queue.lock);
4bb6b515 1757 return;
85b4aa49
RL
1758}
1759
85b4aa49 1760/**
1875f27e 1761 * fcoe_dev_setup() - Setup the link change notification interface
34f42a07 1762 */
b0d428ad 1763static void fcoe_dev_setup(void)
85b4aa49 1764{
85b4aa49
RL
1765 register_netdevice_notifier(&fcoe_notifier);
1766}
1767
1768/**
1875f27e 1769 * fcoe_dev_cleanup() - Cleanup the link change notification interface
34f42a07 1770 */
85b4aa49
RL
1771static void fcoe_dev_cleanup(void)
1772{
1773 unregister_netdevice_notifier(&fcoe_notifier);
1774}
1775
1776/**
1875f27e
RL
1777 * 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
85b4aa49 1781 *
1875f27e 1782 * This function is called by the Ethernet driver in case of link change event.
85b4aa49
RL
1783 *
1784 * Returns: 0 for success
34f42a07 1785 */
85b4aa49
RL
1786static int fcoe_device_notification(struct notifier_block *notifier,
1787 ulong event, void *ptr)
1788{
1875f27e 1789 struct fc_lport *lport = NULL;
1d1b88dc 1790 struct net_device *netdev = ptr;
014f5c3f 1791 struct fcoe_interface *fcoe;
2e70e241 1792 struct fcoe_port *port;
85b4aa49 1793 struct fcoe_dev_stats *stats;
97c8389d 1794 u32 link_possible = 1;
85b4aa49
RL
1795 u32 mfs;
1796 int rc = NOTIFY_OK;
1797
014f5c3f 1798 list_for_each_entry(fcoe, &fcoe_hostlist, list) {
25024989 1799 if (fcoe->netdev == netdev) {
1875f27e 1800 lport = fcoe->ctlr.lp;
85b4aa49
RL
1801 break;
1802 }
1803 }
1875f27e 1804 if (!lport) {
85b4aa49
RL
1805 rc = NOTIFY_DONE;
1806 goto out;
1807 }
1808
85b4aa49
RL
1809 switch (event) {
1810 case NETDEV_DOWN:
1811 case NETDEV_GOING_DOWN:
97c8389d 1812 link_possible = 0;
85b4aa49
RL
1813 break;
1814 case NETDEV_UP:
1815 case NETDEV_CHANGE:
85b4aa49
RL
1816 break;
1817 case NETDEV_CHANGEMTU:
7221d7e5
YZ
1818 if (netdev->features & NETIF_F_FCOE_MTU)
1819 break;
1d1b88dc
VD
1820 mfs = netdev->mtu - (sizeof(struct fcoe_hdr) +
1821 sizeof(struct fcoe_crc_eof));
85b4aa49 1822 if (mfs >= FC_MIN_MAX_FRAME)
1875f27e 1823 fc_set_mfs(lport, mfs);
85b4aa49
RL
1824 break;
1825 case NETDEV_REGISTER:
1826 break;
2e70e241
CL
1827 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;
85b4aa49 1834 default:
1d1b88dc 1835 FCOE_NETDEV_DBG(netdev, "Unknown event %ld "
d5488eb9 1836 "from netdev netlink\n", event);
85b4aa49 1837 }
1875f27e 1838 if (link_possible && !fcoe_link_ok(lport))
3fe9a0ba
CL
1839 fcoe_ctlr_link_up(&fcoe->ctlr);
1840 else if (fcoe_ctlr_link_down(&fcoe->ctlr)) {
1875f27e 1841 stats = fc_lport_get_stats(lport);
97c8389d 1842 stats->LinkFailureCount++;
1875f27e 1843 fcoe_clean_pending_queue(lport);
85b4aa49
RL
1844 }
1845out:
1846 return rc;
1847}
1848
1849/**
1875f27e
RL
1850 * fcoe_if_to_netdev() - Parse a name buffer to get a net device
1851 * @buffer: The name of the net device
85b4aa49 1852 *
1875f27e 1853 * Returns: NULL or a ptr to net_device
34f42a07 1854 */
85b4aa49
RL
1855static 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
55a66d3c
VD
1870/**
1871 * 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
85b4aa49 1968/**
1875f27e
RL
1969 * 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.
85b4aa49
RL
1974 *
1975 * Returns: 0 for success
34f42a07 1976 */
85b4aa49
RL
1977static int fcoe_destroy(const char *buffer, struct kernel_param *kp)
1978{
030f4e00 1979 struct fcoe_interface *fcoe;
2e70e241 1980 struct net_device *netdev;
8eca355f 1981 int rc = 0;
85b4aa49 1982
dfc1d0fe
CL
1983 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
85b4aa49
RL
1996 netdev = fcoe_if_to_netdev(buffer);
1997 if (!netdev) {
1998 rc = -ENODEV;
1999 goto out_nodev;
2000 }
2e70e241 2001
090eb6c4 2002 rtnl_lock();
2e70e241
CL
2003 fcoe = fcoe_hostlist_lookup_port(netdev);
2004 if (!fcoe) {
090eb6c4 2005 rtnl_unlock();
85b4aa49
RL
2006 rc = -ENODEV;
2007 goto out_putdev;
2008 }
2e70e241 2009 list_del(&fcoe->list);
2e70e241
CL
2010 fcoe_interface_cleanup(fcoe);
2011 rtnl_unlock();
2012 fcoe_if_destroy(fcoe->ctlr.lp);
6409ea65
RL
2013 module_put(THIS_MODULE);
2014
85b4aa49
RL
2015out_putdev:
2016 dev_put(netdev);
2017out_nodev:
dfc1d0fe 2018 mutex_unlock(&fcoe_config_mutex);
85b4aa49
RL
2019 return rc;
2020}
2021
1875f27e
RL
2022/**
2023 * fcoe_destroy_work() - Destroy a FCoE port in a deferred work context
2024 * @work: Handle to the FCoE port to be destroyed
2025 */
2e70e241
CL
2026static 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
85b4aa49 2036/**
1875f27e
RL
2037 * 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.
85b4aa49
RL
2042 *
2043 * Returns: 0 for success
34f42a07 2044 */
85b4aa49
RL
2045static int fcoe_create(const char *buffer, struct kernel_param *kp)
2046{
2047 int rc;
030f4e00 2048 struct fcoe_interface *fcoe;
af7f85d9 2049 struct fc_lport *lport;
85b4aa49
RL
2050 struct net_device *netdev;
2051
dfc1d0fe
CL
2052 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
6409ea65
RL
2065 if (!try_module_get(THIS_MODULE)) {
2066 rc = -EINVAL;
2067 goto out_nomod;
2068 }
2069
2e70e241 2070 rtnl_lock();
85b4aa49
RL
2071 netdev = fcoe_if_to_netdev(buffer);
2072 if (!netdev) {
2073 rc = -ENODEV;
2074 goto out_nodev;
2075 }
2e70e241 2076
85b4aa49
RL
2077 /* look for existing lport */
2078 if (fcoe_hostlist_lookup(netdev)) {
2079 rc = -EEXIST;
2080 goto out_putdev;
2081 }
85b4aa49 2082
030f4e00
CL
2083 fcoe = fcoe_interface_create(netdev);
2084 if (!fcoe) {
2085 rc = -ENOMEM;
2086 goto out_putdev;
2087 }
2088
9a05753b 2089 lport = fcoe_if_create(fcoe, &netdev->dev, 0);
af7f85d9 2090 if (IS_ERR(lport)) {
d5488eb9 2091 printk(KERN_ERR "fcoe: Failed to create interface (%s)\n",
85b4aa49 2092 netdev->name);
85b4aa49 2093 rc = -EIO;
2e70e241 2094 fcoe_interface_cleanup(fcoe);
030f4e00 2095 goto out_free;
85b4aa49 2096 }
030f4e00 2097
54b649f8
CL
2098 /* Make this the "master" N_Port */
2099 fcoe->ctlr.lp = lport;
2100
c863df33
CL
2101 /* add to lports list */
2102 fcoe_hostlist_add(lport);
2103
54b649f8
CL
2104 /* 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);
030f4e00 2109
54b649f8
CL
2110 /*
2111 * Release from init in fcoe_interface_create(), on success lport
2112 * should be holding a reference taken in fcoe_if_create().
2113 */
030f4e00 2114 fcoe_interface_put(fcoe);
6409ea65
RL
2115 dev_put(netdev);
2116 rtnl_unlock();
2117 mutex_unlock(&fcoe_config_mutex);
2118
2119 return 0;
2120out_free:
2121 fcoe_interface_put(fcoe);
85b4aa49
RL
2122out_putdev:
2123 dev_put(netdev);
2124out_nodev:
2e70e241 2125 rtnl_unlock();
6409ea65
RL
2126 module_put(THIS_MODULE);
2127out_nomod:
dfc1d0fe 2128 mutex_unlock(&fcoe_config_mutex);
85b4aa49
RL
2129 return rc;
2130}
2131
34f42a07 2132/**
1875f27e
RL
2133 * fcoe_link_ok() - Check if the link is OK for a local port
2134 * @lport: The local port to check link on
85b4aa49
RL
2135 *
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 */
1875f27e 2146int fcoe_link_ok(struct fc_lport *lport)
85b4aa49 2147{
1875f27e
RL
2148 struct fcoe_port *port = lport_priv(lport);
2149 struct net_device *netdev = port->fcoe->netdev;
85b4aa49 2150 struct ethtool_cmd ecmd = { ETHTOOL_GSET };
85b4aa49 2151
1875f27e
RL
2152 if ((netdev->flags & IFF_UP) && netif_carrier_ok(netdev) &&
2153 (!dev_ethtool_get_settings(netdev, &ecmd))) {
2154 lport->link_supported_speeds &=
2f718d64
YZ
2155 ~(FC_PORTSPEED_1GBIT | FC_PORTSPEED_10GBIT);
2156 if (ecmd.supported & (SUPPORTED_1000baseT_Half |
2157 SUPPORTED_1000baseT_Full))
1875f27e 2158 lport->link_supported_speeds |= FC_PORTSPEED_1GBIT;
2f718d64 2159 if (ecmd.supported & SUPPORTED_10000baseT_Full)
1875f27e 2160 lport->link_supported_speeds |=
2f718d64
YZ
2161 FC_PORTSPEED_10GBIT;
2162 if (ecmd.speed == SPEED_1000)
1875f27e 2163 lport->link_speed = FC_PORTSPEED_1GBIT;
2f718d64 2164 if (ecmd.speed == SPEED_10000)
1875f27e 2165 lport->link_speed = FC_PORTSPEED_10GBIT;
85b4aa49 2166
2f718d64
YZ
2167 return 0;
2168 }
2169 return -1;
85b4aa49 2170}
85b4aa49 2171
34f42a07 2172/**
1875f27e
RL
2173 * fcoe_percpu_clean() - Clear all pending skbs for an local port
2174 * @lport: The local port whose skbs are to be cleared
e7a51997
JE
2175 *
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.
85b4aa49 2182 */
1875f27e 2183void fcoe_percpu_clean(struct fc_lport *lport)
85b4aa49 2184{
85b4aa49
RL
2185 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;
5e5e92df 2190 unsigned int cpu;
85b4aa49 2191
5e5e92df
RL
2192 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);
1875f27e 2201 if (fr->fr_dev == lport) {
5e5e92df
RL
2202 __skb_unlink(skb, list);
2203 kfree_skb(skb);
85b4aa49 2204 }
85b4aa49 2205 }
e7a51997
JE
2206
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);
5e5e92df 2222 spin_unlock_bh(&pp->fcoe_rx_list.lock);
e7a51997
JE
2223
2224 wait_for_completion(&fcoe_flush_completion);
85b4aa49
RL
2225 }
2226}
85b4aa49
RL
2227
2228/**
34f42a07 2229 * fcoe_clean_pending_queue() - Dequeue a skb and free it
1875f27e 2230 * @lport: The local port to dequeue a skb on
34f42a07 2231 */
1875f27e 2232void fcoe_clean_pending_queue(struct fc_lport *lport)
85b4aa49 2233{
1875f27e 2234 struct fcoe_port *port = lport_priv(lport);
85b4aa49
RL
2235 struct sk_buff *skb;
2236
014f5c3f
CL
2237 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);
85b4aa49 2240 kfree_skb(skb);
014f5c3f 2241 spin_lock_bh(&port->fcoe_pending_queue.lock);
85b4aa49 2242 }
014f5c3f 2243 spin_unlock_bh(&port->fcoe_pending_queue.lock);
85b4aa49 2244}
85b4aa49 2245
34f42a07 2246/**
1875f27e
RL
2247 * fcoe_reset() - Reset a local port
2248 * @shost: The SCSI host associated with the local port to be reset
85b4aa49 2249 *
1875f27e 2250 * Returns: Always 0 (return value required by FC transport template)
85b4aa49
RL
2251 */
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}
85b4aa49 2258
34f42a07 2259/**
1875f27e
RL
2260 * fcoe_hostlist_lookup_port() - Find the FCoE interface associated with a net device
2261 * @netdev: The net device used as a key
85b4aa49 2262 *
1875f27e
RL
2263 * Locking: Must be called with the RNL mutex held.
2264 *
2265 * Returns: NULL or the FCoE interface
85b4aa49 2266 */
014f5c3f 2267static struct fcoe_interface *
1875f27e 2268fcoe_hostlist_lookup_port(const struct net_device *netdev)
85b4aa49 2269{
014f5c3f 2270 struct fcoe_interface *fcoe;
85b4aa49 2271
014f5c3f 2272 list_for_each_entry(fcoe, &fcoe_hostlist, list) {
1875f27e 2273 if (fcoe->netdev == netdev)
014f5c3f 2274 return fcoe;
85b4aa49 2275 }
85b4aa49
RL
2276 return NULL;
2277}
2278
34f42a07 2279/**
1875f27e
RL
2280 * fcoe_hostlist_lookup() - Find the local port associated with a
2281 * given net device
2282 * @netdev: The netdevice used as a key
85b4aa49 2283 *
1875f27e
RL
2284 * Locking: Must be called with the RTNL mutex held
2285 *
2286 * Returns: NULL or the local port
85b4aa49 2287 */
090eb6c4 2288static struct fc_lport *fcoe_hostlist_lookup(const struct net_device *netdev)
85b4aa49 2289{
014f5c3f 2290 struct fcoe_interface *fcoe;
85b4aa49 2291
014f5c3f 2292 fcoe = fcoe_hostlist_lookup_port(netdev);
3fe9a0ba 2293 return (fcoe) ? fcoe->ctlr.lp : NULL;
85b4aa49 2294}
85b4aa49 2295
34f42a07 2296/**
1875f27e
RL
2297 * 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
85b4aa49 2300 *
090eb6c4 2301 * Locking: must be called with the RTNL mutex held
1875f27e
RL
2302 *
2303 * Returns: 0 for success
85b4aa49 2304 */
090eb6c4 2305static int fcoe_hostlist_add(const struct fc_lport *lport)
85b4aa49 2306{
014f5c3f
CL
2307 struct fcoe_interface *fcoe;
2308 struct fcoe_port *port;
2309
2310 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);
85b4aa49
RL
2315 }
2316 return 0;
2317}
85b4aa49 2318
85b4aa49 2319/**
1875f27e 2320 * fcoe_init() - Initialize fcoe.ko
85b4aa49 2321 *
1875f27e 2322 * Returns: 0 on success, or a negative value on failure
34f42a07 2323 */
85b4aa49
RL
2324static int __init fcoe_init(void)
2325{
1875f27e 2326 struct fcoe_percpu_s *p;
38eccabd 2327 unsigned int cpu;
8976f424 2328 int rc = 0;
85b4aa49 2329
dfc1d0fe
CL
2330 mutex_lock(&fcoe_config_mutex);
2331
38eccabd 2332 for_each_possible_cpu(cpu) {
5e5e92df 2333 p = &per_cpu(fcoe_percpu, cpu);
38eccabd
RL
2334 skb_queue_head_init(&p->fcoe_rx_list);
2335 }
2336
8976f424
RL
2337 for_each_online_cpu(cpu)
2338 fcoe_percpu_thread_create(cpu);
85b4aa49 2339
8976f424
RL
2340 /* Initialize per CPU interrupt thread */
2341 rc = register_hotcpu_notifier(&fcoe_cpu_notifier);
2342 if (rc)
2343 goto out_free;
85b4aa49 2344
8976f424 2345 /* Setup link change notification */
85b4aa49
RL
2346 fcoe_dev_setup();
2347
5892c32f
CL
2348 rc = fcoe_if_init();
2349 if (rc)
2350 goto out_free;
85b4aa49 2351
dfc1d0fe 2352 mutex_unlock(&fcoe_config_mutex);
85b4aa49 2353 return 0;
8976f424
RL
2354
2355out_free:
2356 for_each_online_cpu(cpu) {
2357 fcoe_percpu_thread_destroy(cpu);
2358 }
dfc1d0fe 2359 mutex_unlock(&fcoe_config_mutex);
8976f424 2360 return rc;
85b4aa49
RL
2361}
2362module_init(fcoe_init);
2363
2364/**
1875f27e 2365 * fcoe_exit() - Clean up fcoe.ko
85b4aa49 2366 *
1875f27e 2367 * Returns: 0 on success or a negative value on failure
34f42a07 2368 */
85b4aa49
RL
2369static void __exit fcoe_exit(void)
2370{
014f5c3f 2371 struct fcoe_interface *fcoe, *tmp;
2e70e241 2372 struct fcoe_port *port;
1875f27e 2373 unsigned int cpu;
85b4aa49 2374
dfc1d0fe
CL
2375 mutex_lock(&fcoe_config_mutex);
2376
85b4aa49
RL
2377 fcoe_dev_cleanup();
2378
5919a595 2379 /* releases the associated fcoe hosts */
090eb6c4
CL
2380 rtnl_lock();
2381 list_for_each_entry_safe(fcoe, tmp, &fcoe_hostlist, list) {
c863df33 2382 list_del(&fcoe->list);
2e70e241 2383 port = lport_priv(fcoe->ctlr.lp);
2e70e241 2384 fcoe_interface_cleanup(fcoe);
2e70e241 2385 schedule_work(&port->destroy_work);
c863df33 2386 }
090eb6c4 2387 rtnl_unlock();
85b4aa49 2388
8976f424
RL
2389 unregister_hotcpu_notifier(&fcoe_cpu_notifier);
2390
014f5c3f 2391 for_each_online_cpu(cpu)
8976f424 2392 fcoe_percpu_thread_destroy(cpu);
85b4aa49 2393
dfc1d0fe 2394 mutex_unlock(&fcoe_config_mutex);
2e70e241
CL
2395
2396 /* flush any asyncronous interface destroys,
2397 * this should happen after the netdev notifier is unregistered */
2398 flush_scheduled_work();
9a05753b
CL
2399 /* 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();
2e70e241
CL
2402
2403 /* detach from scsi transport
2404 * must happen after all destroys are done, therefor after the flush */
2405 fcoe_if_exit();
85b4aa49
RL
2406}
2407module_exit(fcoe_exit);
11b56188
CL
2408
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 */
386309ce 2431 if (fcoe_ctlr_recv_flogi(fip, lport, fp)) {
11b56188
CL
2432 fc_frame_free(fp);
2433 return;
2434 }
11b56188 2435 }
386309ce 2436 fcoe_update_src_mac(lport, mac);
11b56188
CL
2437done:
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{
386309ce 2452 struct fc_lport *lport = arg;
11b56188
CL
2453 static u8 zero_mac[ETH_ALEN] = { 0 };
2454
2455 if (!IS_ERR(fp))
386309ce 2456 fcoe_update_src_mac(lport, zero_mac);
11b56188
CL
2457 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 */
1875f27e
RL
2469static 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)
11b56188
CL
2475{
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,
386309ce 2491 lport, timeout);
11b56188
CL
2492 }
2493 return fc_elsct_send(lport, did, fp, op, resp, arg, timeout);
2494}
2495
9a05753b
CL
2496/**
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
dc8596d3
CL
2573/**
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,
b94f8951 2601 NULL, NULL, 3 * lport->r_a_tov);
dc8596d3 2602}
b84056bf
YZ
2603
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}