]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - drivers/scsi/fcoe/fcoe.c
scsi: remove eh_timed_out methods in the transport template
[mirror_ubuntu-bionic-kernel.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>
85b4aa49 21#include <linux/spinlock.h>
85b4aa49
RL
22#include <linux/netdevice.h>
23#include <linux/etherdevice.h>
24#include <linux/ethtool.h>
25#include <linux/if_ether.h>
26#include <linux/if_vlan.h>
85b4aa49 27#include <linux/crc32.h>
5a0e3ad6 28#include <linux/slab.h>
85b4aa49
RL
29#include <linux/cpu.h>
30#include <linux/fs.h>
31#include <linux/sysfs.h>
32#include <linux/ctype.h>
2ca32b48 33#include <linux/workqueue.h>
6f6c2aa3 34#include <net/dcbnl.h>
35#include <net/dcbevent.h>
85b4aa49
RL
36#include <scsi/scsi_tcq.h>
37#include <scsi/scsicam.h>
38#include <scsi/scsi_transport.h>
39#include <scsi/scsi_transport_fc.h>
40#include <net/rtnetlink.h>
41
42#include <scsi/fc/fc_encaps.h>
97c8389d 43#include <scsi/fc/fc_fip.h>
8d55e507 44#include <scsi/fc/fc_fcoe.h>
85b4aa49
RL
45
46#include <scsi/libfc.h>
47#include <scsi/fc_frame.h>
48#include <scsi/libfcoe.h>
85b4aa49 49
fdd78027 50#include "fcoe.h"
7f349142 51
85b4aa49
RL
52MODULE_AUTHOR("Open-FCoE.org");
53MODULE_DESCRIPTION("FCoE");
9b34ecff 54MODULE_LICENSE("GPL v2");
85b4aa49 55
05cc7390 56/* Performance tuning parameters for fcoe */
860eca2b 57static unsigned int fcoe_ddp_min = 4096;
05cc7390
YZ
58module_param_named(ddp_min, fcoe_ddp_min, uint, S_IRUGO | S_IWUSR);
59MODULE_PARM_DESC(ddp_min, "Minimum I/O size in bytes for " \
60 "Direct Data Placement (DDP).");
61
7c9c6841
BVA
62unsigned int fcoe_debug_logging;
63module_param_named(debug_logging, fcoe_debug_logging, int, S_IRUGO|S_IWUSR);
64MODULE_PARM_DESC(debug_logging, "a bit mask of logging levels");
65
69aabcce
HR
66unsigned int fcoe_e_d_tov = 2 * 1000;
67module_param_named(e_d_tov, fcoe_e_d_tov, int, S_IRUGO|S_IWUSR);
68MODULE_PARM_DESC(e_d_tov, "E_D_TOV in ms, default 2000");
69
70unsigned int fcoe_r_a_tov = 2 * 2 * 1000;
71module_param_named(r_a_tov, fcoe_r_a_tov, int, S_IRUGO|S_IWUSR);
72MODULE_PARM_DESC(r_a_tov, "R_A_TOV in ms, default 4000");
73
7c9c6841 74static DEFINE_MUTEX(fcoe_config_mutex);
dfc1d0fe 75
2ca32b48
TH
76static struct workqueue_struct *fcoe_wq;
77
85b4aa49 78/* fcoe host list */
090eb6c4 79/* must only by accessed under the RTNL mutex */
7c9c6841
BVA
80static LIST_HEAD(fcoe_hostlist);
81static DEFINE_PER_CPU(struct fcoe_percpu_s, fcoe_percpu);
85b4aa49 82
dd3fd72e 83/* Function Prototypes */
1875f27e 84static int fcoe_reset(struct Scsi_Host *);
fdd78027
VD
85static int fcoe_xmit(struct fc_lport *, struct fc_frame *);
86static int fcoe_rcv(struct sk_buff *, struct net_device *,
87 struct packet_type *, struct net_device *);
1875f27e
RL
88static void fcoe_percpu_clean(struct fc_lport *);
89static int fcoe_link_ok(struct fc_lport *);
fdd78027
VD
90
91static struct fc_lport *fcoe_hostlist_lookup(const struct net_device *);
92static int fcoe_hostlist_add(const struct fc_lport *);
f9184df3 93static void fcoe_hostlist_del(const struct fc_lport *);
fdd78027 94
85b4aa49
RL
95static int fcoe_device_notification(struct notifier_block *, ulong, void *);
96static void fcoe_dev_setup(void);
97static void fcoe_dev_cleanup(void);
1875f27e
RL
98static struct fcoe_interface
99*fcoe_hostlist_lookup_port(const struct net_device *);
100
101static int fcoe_fip_recv(struct sk_buff *, struct net_device *,
102 struct packet_type *, struct net_device *);
d242e668
HR
103static int fcoe_fip_vlan_recv(struct sk_buff *, struct net_device *,
104 struct packet_type *, struct net_device *);
1875f27e
RL
105
106static void fcoe_fip_send(struct fcoe_ctlr *, struct sk_buff *);
107static void fcoe_update_src_mac(struct fc_lport *, u8 *);
108static u8 *fcoe_get_src_mac(struct fc_lport *);
109static void fcoe_destroy_work(struct work_struct *);
110
111static int fcoe_ddp_setup(struct fc_lport *, u16, struct scatterlist *,
112 unsigned int);
113static int fcoe_ddp_done(struct fc_lport *, u16);
71f89491
YZ
114static int fcoe_ddp_target(struct fc_lport *, u16, struct scatterlist *,
115 unsigned int);
6f6c2aa3 116static int fcoe_dcb_app_notification(struct notifier_block *notifier,
117 ulong event, void *ptr);
1875f27e 118
78a58246 119static bool fcoe_match(struct net_device *netdev);
1917d42d 120static int fcoe_create(struct net_device *netdev, enum fip_mode fip_mode);
78a58246
YZ
121static int fcoe_destroy(struct net_device *netdev);
122static int fcoe_enable(struct net_device *netdev);
123static int fcoe_disable(struct net_device *netdev);
1875f27e 124
435c8667
RL
125/* fcoe_syfs control interface handlers */
126static int fcoe_ctlr_alloc(struct net_device *netdev);
127static int fcoe_ctlr_enabled(struct fcoe_ctlr_device *cdev);
a87dccc7 128static void fcoe_ctlr_mode(struct fcoe_ctlr_device *ctlr_dev);
435c8667 129
1875f27e
RL
130static struct fc_seq *fcoe_elsct_send(struct fc_lport *,
131 u32 did, struct fc_frame *,
132 unsigned int op,
133 void (*resp)(struct fc_seq *,
134 struct fc_frame *,
135 void *),
136 void *, u32 timeout);
859b7b64 137static void fcoe_recv_frame(struct sk_buff *skb);
1875f27e 138
1875f27e 139/* notification function for packets from net device */
85b4aa49
RL
140static struct notifier_block fcoe_notifier = {
141 .notifier_call = fcoe_device_notification,
142};
143
6f6c2aa3 144/* notification function for DCB events */
145static struct notifier_block dcb_notifier = {
146 .notifier_call = fcoe_dcb_app_notification,
147};
148
8ca86f84
YZ
149static struct scsi_transport_template *fcoe_nport_scsi_transport;
150static struct scsi_transport_template *fcoe_vport_scsi_transport;
7f349142 151
1875f27e
RL
152static int fcoe_vport_destroy(struct fc_vport *);
153static int fcoe_vport_create(struct fc_vport *, bool disabled);
154static int fcoe_vport_disable(struct fc_vport *, bool disable);
155static void fcoe_set_vport_symbolic_name(struct fc_vport *);
7d65b0df 156static void fcoe_set_port_id(struct fc_lport *, u32, struct fc_frame *);
8d55e507
RL
157static void fcoe_fcf_get_vlan_id(struct fcoe_fcf_device *);
158
a87dccc7 159
8d55e507 160static struct fcoe_sysfs_function_template fcoe_sysfs_templ = {
a87dccc7 161 .set_fcoe_ctlr_mode = fcoe_ctlr_mode,
435c8667 162 .set_fcoe_ctlr_enabled = fcoe_ctlr_enabled,
8d55e507
RL
163 .get_fcoe_ctlr_link_fail = fcoe_ctlr_get_lesb,
164 .get_fcoe_ctlr_vlink_fail = fcoe_ctlr_get_lesb,
165 .get_fcoe_ctlr_miss_fka = fcoe_ctlr_get_lesb,
166 .get_fcoe_ctlr_symb_err = fcoe_ctlr_get_lesb,
167 .get_fcoe_ctlr_err_block = fcoe_ctlr_get_lesb,
168 .get_fcoe_ctlr_fcs_error = fcoe_ctlr_get_lesb,
169
170 .get_fcoe_fcf_selected = fcoe_fcf_get_selected,
171 .get_fcoe_fcf_vlan_id = fcoe_fcf_get_vlan_id,
172};
1875f27e
RL
173
174static struct libfc_function_template fcoe_libfc_fcn_templ = {
175 .frame_send = fcoe_xmit,
176 .ddp_setup = fcoe_ddp_setup,
177 .ddp_done = fcoe_ddp_done,
71f89491 178 .ddp_target = fcoe_ddp_target,
1875f27e 179 .elsct_send = fcoe_elsct_send,
b84056bf 180 .get_lesb = fcoe_get_lesb,
7d65b0df 181 .lport_set_port_id = fcoe_set_port_id,
1875f27e 182};
9a05753b 183
7c9c6841 184static struct fc_function_template fcoe_nport_fc_functions = {
7f349142
VD
185 .show_host_node_name = 1,
186 .show_host_port_name = 1,
187 .show_host_supported_classes = 1,
188 .show_host_supported_fc4s = 1,
189 .show_host_active_fc4s = 1,
190 .show_host_maxframe_size = 1,
9f71af2f
NP
191 .show_host_serial_number = 1,
192 .show_host_manufacturer = 1,
193 .show_host_model = 1,
194 .show_host_model_description = 1,
195 .show_host_hardware_version = 1,
196 .show_host_driver_version = 1,
197 .show_host_firmware_version = 1,
198 .show_host_optionrom_version = 1,
7f349142
VD
199
200 .show_host_port_id = 1,
201 .show_host_supported_speeds = 1,
202 .get_host_speed = fc_get_host_speed,
203 .show_host_speed = 1,
204 .show_host_port_type = 1,
205 .get_host_port_state = fc_get_host_port_state,
206 .show_host_port_state = 1,
207 .show_host_symbolic_name = 1,
208
209 .dd_fcrport_size = sizeof(struct fc_rport_libfc_priv),
210 .show_rport_maxframe_size = 1,
211 .show_rport_supported_classes = 1,
212
213 .show_host_fabric_name = 1,
214 .show_starget_node_name = 1,
215 .show_starget_port_name = 1,
216 .show_starget_port_id = 1,
217 .set_rport_dev_loss_tmo = fc_set_rport_loss_tmo,
218 .show_rport_dev_loss_tmo = 1,
219 .get_fc_host_stats = fc_get_host_stats,
220 .issue_fc_host_lip = fcoe_reset,
221
222 .terminate_rport_io = fc_rport_terminate_io,
9a05753b
CL
223
224 .vport_create = fcoe_vport_create,
225 .vport_delete = fcoe_vport_destroy,
226 .vport_disable = fcoe_vport_disable,
dc8596d3 227 .set_vport_symbolic_name = fcoe_set_vport_symbolic_name,
a51ab396
SM
228
229 .bsg_request = fc_lport_bsg_request,
7f349142
VD
230};
231
7c9c6841 232static struct fc_function_template fcoe_vport_fc_functions = {
e9084bb8
CL
233 .show_host_node_name = 1,
234 .show_host_port_name = 1,
235 .show_host_supported_classes = 1,
236 .show_host_supported_fc4s = 1,
237 .show_host_active_fc4s = 1,
238 .show_host_maxframe_size = 1,
7e5adcfb
NP
239 .show_host_serial_number = 1,
240 .show_host_manufacturer = 1,
241 .show_host_model = 1,
242 .show_host_model_description = 1,
243 .show_host_hardware_version = 1,
244 .show_host_driver_version = 1,
245 .show_host_firmware_version = 1,
246 .show_host_optionrom_version = 1,
e9084bb8
CL
247
248 .show_host_port_id = 1,
249 .show_host_supported_speeds = 1,
250 .get_host_speed = fc_get_host_speed,
251 .show_host_speed = 1,
252 .show_host_port_type = 1,
253 .get_host_port_state = fc_get_host_port_state,
254 .show_host_port_state = 1,
255 .show_host_symbolic_name = 1,
256
257 .dd_fcrport_size = sizeof(struct fc_rport_libfc_priv),
258 .show_rport_maxframe_size = 1,
259 .show_rport_supported_classes = 1,
260
261 .show_host_fabric_name = 1,
262 .show_starget_node_name = 1,
263 .show_starget_port_name = 1,
264 .show_starget_port_id = 1,
265 .set_rport_dev_loss_tmo = fc_set_rport_loss_tmo,
266 .show_rport_dev_loss_tmo = 1,
267 .get_fc_host_stats = fc_get_host_stats,
268 .issue_fc_host_lip = fcoe_reset,
269
270 .terminate_rport_io = fc_rport_terminate_io,
a51ab396
SM
271
272 .bsg_request = fc_lport_bsg_request,
e9084bb8
CL
273};
274
7f349142
VD
275static struct scsi_host_template fcoe_shost_template = {
276 .module = THIS_MODULE,
277 .name = "FCoE Driver",
278 .proc_name = FCOE_NAME,
279 .queuecommand = fc_queuecommand,
b6a05c82 280 .eh_timed_out = fc_eh_timed_out,
7f349142
VD
281 .eh_abort_handler = fc_eh_abort,
282 .eh_device_reset_handler = fc_eh_device_reset,
283 .eh_host_reset_handler = fc_eh_host_reset,
284 .slave_alloc = fc_slave_alloc,
db5ed4df 285 .change_queue_depth = scsi_change_queue_depth,
7f349142 286 .this_id = -1,
14caf44c 287 .cmd_per_lun = 3,
7f349142
VD
288 .can_queue = FCOE_MAX_OUTSTANDING_COMMANDS,
289 .use_clustering = ENABLE_CLUSTERING,
290 .sg_tablesize = SG_ALL,
291 .max_sectors = 0xffff,
c40ecc12 292 .track_queue_depth = 1,
7f349142
VD
293};
294
54b649f8 295/**
1875f27e
RL
296 * fcoe_interface_setup() - Setup a FCoE interface
297 * @fcoe: The new FCoE interface
298 * @netdev: The net device that the fcoe interface is on
54b649f8
CL
299 *
300 * Returns : 0 for success
2e70e241 301 * Locking: must be called with the RTNL mutex held
54b649f8
CL
302 */
303static int fcoe_interface_setup(struct fcoe_interface *fcoe,
304 struct net_device *netdev)
305{
619fe4be 306 struct fcoe_ctlr *fip = fcoe_to_ctlr(fcoe);
54b649f8 307 struct netdev_hw_addr *ha;
5bab87e6 308 struct net_device *real_dev;
54b649f8 309 u8 flogi_maddr[ETH_ALEN];
b7a727f1 310 const struct net_device_ops *ops;
54b649f8
CL
311
312 fcoe->netdev = netdev;
313
b7a727f1
YZ
314 /* Let LLD initialize for FCoE */
315 ops = netdev->netdev_ops;
316 if (ops->ndo_fcoe_enable) {
317 if (ops->ndo_fcoe_enable(netdev))
318 FCOE_NETDEV_DBG(netdev, "Failed to enable FCoE"
319 " specific feature for LLD.\n");
320 }
321
54b649f8 322 /* Do not support for bonding device */
cc8bdf06 323 if (netdev->priv_flags & IFF_BONDING && netdev->flags & IFF_MASTER) {
59d92516 324 FCOE_NETDEV_DBG(netdev, "Bonded interfaces not supported\n");
54b649f8
CL
325 return -EOPNOTSUPP;
326 }
327
328 /* look for SAN MAC address, if multiple SAN MACs exist, only
329 * use the first one for SPMA */
5bab87e6
YZ
330 real_dev = (netdev->priv_flags & IFF_802_1Q_VLAN) ?
331 vlan_dev_real_dev(netdev) : netdev;
d1483bb9 332 fcoe->realdev = real_dev;
54b649f8 333 rcu_read_lock();
5bab87e6 334 for_each_dev_addr(real_dev, ha) {
54b649f8 335 if ((ha->type == NETDEV_HW_ADDR_T_SAN) &&
bf361707 336 (is_valid_ether_addr(ha->addr))) {
54b649f8
CL
337 memcpy(fip->ctl_src_addr, ha->addr, ETH_ALEN);
338 fip->spma = 1;
339 break;
340 }
341 }
342 rcu_read_unlock();
343
344 /* setup Source Mac Address */
345 if (!fip->spma)
346 memcpy(fip->ctl_src_addr, netdev->dev_addr, netdev->addr_len);
347
348 /*
349 * Add FCoE MAC address as second unicast MAC address
350 * or enter promiscuous mode if not capable of listening
351 * for multiple unicast MACs.
352 */
54b649f8 353 memcpy(flogi_maddr, (u8[6]) FC_FCOE_FLOGI_MAC, ETH_ALEN);
a748ee24 354 dev_uc_add(netdev, flogi_maddr);
54b649f8 355 if (fip->spma)
a748ee24 356 dev_uc_add(netdev, fip->ctl_src_addr);
e10f8c66
JE
357 if (fip->mode == FIP_MODE_VN2VN) {
358 dev_mc_add(netdev, FIP_ALL_VN2VN_MACS);
359 dev_mc_add(netdev, FIP_ALL_P2P_MACS);
360 } else
361 dev_mc_add(netdev, FIP_ALL_ENODE_MACS);
54b649f8
CL
362
363 /*
364 * setup the receive function from ethernet driver
365 * on the ethertype for the given device
366 */
367 fcoe->fcoe_packet_type.func = fcoe_rcv;
420fa211 368 fcoe->fcoe_packet_type.type = htons(ETH_P_FCOE);
54b649f8
CL
369 fcoe->fcoe_packet_type.dev = netdev;
370 dev_add_pack(&fcoe->fcoe_packet_type);
371
372 fcoe->fip_packet_type.func = fcoe_fip_recv;
373 fcoe->fip_packet_type.type = htons(ETH_P_FIP);
374 fcoe->fip_packet_type.dev = netdev;
375 dev_add_pack(&fcoe->fip_packet_type);
376
d242e668
HR
377 if (netdev != real_dev) {
378 fcoe->fip_vlan_packet_type.func = fcoe_fip_vlan_recv;
379 fcoe->fip_vlan_packet_type.type = htons(ETH_P_FIP);
380 fcoe->fip_vlan_packet_type.dev = real_dev;
381 dev_add_pack(&fcoe->fip_vlan_packet_type);
382 }
54b649f8
CL
383 return 0;
384}
385
030f4e00 386/**
1875f27e
RL
387 * fcoe_interface_create() - Create a FCoE interface on a net device
388 * @netdev: The net device to create the FCoE interface on
1dd454d9 389 * @fip_mode: The mode to use for FIP
030f4e00
CL
390 *
391 * Returns: pointer to a struct fcoe_interface or NULL on error
392 */
1dd454d9
JE
393static struct fcoe_interface *fcoe_interface_create(struct net_device *netdev,
394 enum fip_state fip_mode)
030f4e00 395{
8d55e507 396 struct fcoe_ctlr_device *ctlr_dev;
619fe4be 397 struct fcoe_ctlr *ctlr;
030f4e00 398 struct fcoe_interface *fcoe;
619fe4be 399 int size;
59d92516 400 int err;
030f4e00 401
7287fb91
RL
402 if (!try_module_get(THIS_MODULE)) {
403 FCOE_NETDEV_DBG(netdev,
404 "Could not get a reference to the module\n");
405 fcoe = ERR_PTR(-EBUSY);
406 goto out;
407 }
408
619fe4be 409 size = sizeof(struct fcoe_ctlr) + sizeof(struct fcoe_interface);
8d55e507
RL
410 ctlr_dev = fcoe_ctlr_device_add(&netdev->dev, &fcoe_sysfs_templ,
411 size);
412 if (!ctlr_dev) {
413 FCOE_DBG("Failed to add fcoe_ctlr_device\n");
7287fb91 414 fcoe = ERR_PTR(-ENOMEM);
6f68794c 415 goto out_putmod;
030f4e00
CL
416 }
417
8d55e507 418 ctlr = fcoe_ctlr_device_priv(ctlr_dev);
9d34876f 419 ctlr->cdev = ctlr_dev;
8d55e507
RL
420 fcoe = fcoe_ctlr_priv(ctlr);
421
2e70e241 422 dev_hold(netdev);
54b649f8
CL
423
424 /*
425 * Initialize FIP.
426 */
619fe4be
RL
427 fcoe_ctlr_init(ctlr, fip_mode);
428 ctlr->send = fcoe_fip_send;
429 ctlr->update_mac = fcoe_update_src_mac;
430 ctlr->get_src_addr = fcoe_get_src_mac;
54b649f8 431
59d92516 432 err = fcoe_interface_setup(fcoe, netdev);
433 if (err) {
619fe4be 434 fcoe_ctlr_destroy(ctlr);
8d55e507 435 fcoe_ctlr_device_delete(ctlr_dev);
59d92516 436 dev_put(netdev);
7287fb91 437 fcoe = ERR_PTR(err);
6f68794c 438 goto out_putmod;
59d92516 439 }
030f4e00 440
7287fb91
RL
441 goto out;
442
6f68794c 443out_putmod:
7287fb91
RL
444 module_put(THIS_MODULE);
445out:
030f4e00
CL
446 return fcoe;
447}
448
54b649f8 449/**
433eba04 450 * fcoe_interface_remove() - remove FCoE interface from netdev
1875f27e 451 * @fcoe: The FCoE interface to be cleaned up
2e70e241
CL
452 *
453 * Caller must be holding the RTNL mutex
54b649f8 454 */
433eba04 455static void fcoe_interface_remove(struct fcoe_interface *fcoe)
54b649f8
CL
456{
457 struct net_device *netdev = fcoe->netdev;
619fe4be 458 struct fcoe_ctlr *fip = fcoe_to_ctlr(fcoe);
54b649f8 459 u8 flogi_maddr[ETH_ALEN];
b7a727f1 460 const struct net_device_ops *ops;
54b649f8
CL
461
462 /*
463 * Don't listen for Ethernet packets anymore.
464 * synchronize_net() ensures that the packet handlers are not running
465 * on another CPU. dev_remove_pack() would do that, this calls the
466 * unsyncronized version __dev_remove_pack() to avoid multiple delays.
467 */
468 __dev_remove_pack(&fcoe->fcoe_packet_type);
469 __dev_remove_pack(&fcoe->fip_packet_type);
d242e668
HR
470 if (netdev != fcoe->realdev)
471 __dev_remove_pack(&fcoe->fip_vlan_packet_type);
54b649f8
CL
472 synchronize_net();
473
54b649f8 474 /* Delete secondary MAC addresses */
54b649f8 475 memcpy(flogi_maddr, (u8[6]) FC_FCOE_FLOGI_MAC, ETH_ALEN);
a748ee24 476 dev_uc_del(netdev, flogi_maddr);
54b649f8 477 if (fip->spma)
a748ee24 478 dev_uc_del(netdev, fip->ctl_src_addr);
e10f8c66
JE
479 if (fip->mode == FIP_MODE_VN2VN) {
480 dev_mc_del(netdev, FIP_ALL_VN2VN_MACS);
481 dev_mc_del(netdev, FIP_ALL_P2P_MACS);
482 } else
483 dev_mc_del(netdev, FIP_ALL_ENODE_MACS);
b7a727f1
YZ
484
485 /* Tell the LLD we are done w/ FCoE */
486 ops = netdev->netdev_ops;
487 if (ops->ndo_fcoe_disable) {
488 if (ops->ndo_fcoe_disable(netdev))
489 FCOE_NETDEV_DBG(netdev, "Failed to disable FCoE"
490 " specific feature for LLD.\n");
491 }
433eba04
VD
492 fcoe->removed = 1;
493}
494
495
496/**
497 * fcoe_interface_cleanup() - Clean up a FCoE interface
498 * @fcoe: The FCoE interface to be cleaned up
499 */
500static void fcoe_interface_cleanup(struct fcoe_interface *fcoe)
501{
502 struct net_device *netdev = fcoe->netdev;
619fe4be 503 struct fcoe_ctlr *fip = fcoe_to_ctlr(fcoe);
b2085a4e 504
433eba04
VD
505 rtnl_lock();
506 if (!fcoe->removed)
507 fcoe_interface_remove(fcoe);
848e7d5b
RL
508 rtnl_unlock();
509
b2085a4e 510 /* Release the self-reference taken during fcoe_interface_create() */
1a8ef414
RL
511 /* tear-down the FCoE controller */
512 fcoe_ctlr_destroy(fip);
619fe4be 513 scsi_host_put(fip->lp->host);
1a8ef414
RL
514 dev_put(netdev);
515 module_put(THIS_MODULE);
030f4e00
CL
516}
517
ab6b85c1 518/**
1875f27e
RL
519 * fcoe_fip_recv() - Handler for received FIP frames
520 * @skb: The receive skb
521 * @netdev: The associated net device
522 * @ptype: The packet_type structure which was used to register this handler
523 * @orig_dev: The original net_device the the skb was received on.
524 * (in case dev is a bond)
ab6b85c1
VD
525 *
526 * Returns: 0 for success
527 */
1875f27e 528static int fcoe_fip_recv(struct sk_buff *skb, struct net_device *netdev,
ab6b85c1
VD
529 struct packet_type *ptype,
530 struct net_device *orig_dev)
531{
259ad85d 532 struct fcoe_interface *fcoe;
619fe4be 533 struct fcoe_ctlr *ctlr;
ab6b85c1 534
259ad85d 535 fcoe = container_of(ptype, struct fcoe_interface, fip_packet_type);
619fe4be
RL
536 ctlr = fcoe_to_ctlr(fcoe);
537 fcoe_ctlr_recv(ctlr, skb);
ab6b85c1
VD
538 return 0;
539}
540
d242e668
HR
541/**
542 * fcoe_fip_vlan_recv() - Handler for received FIP VLAN discovery frames
543 * @skb: The receive skb
544 * @netdev: The associated net device
545 * @ptype: The packet_type structure which was used to register this handler
546 * @orig_dev: The original net_device the the skb was received on.
547 * (in case dev is a bond)
548 *
549 * Returns: 0 for success
550 */
551static int fcoe_fip_vlan_recv(struct sk_buff *skb, struct net_device *netdev,
552 struct packet_type *ptype,
553 struct net_device *orig_dev)
554{
555 struct fcoe_interface *fcoe;
556 struct fcoe_ctlr *ctlr;
557
558 fcoe = container_of(ptype, struct fcoe_interface, fip_vlan_packet_type);
559 ctlr = fcoe_to_ctlr(fcoe);
560 fcoe_ctlr_recv(ctlr, skb);
561 return 0;
562}
563
980f5156
VD
564/**
565 * fcoe_port_send() - Send an Ethernet-encapsulated FIP/FCoE frame
566 * @port: The FCoE port
567 * @skb: The FIP/FCoE packet to be sent
568 */
569static void fcoe_port_send(struct fcoe_port *port, struct sk_buff *skb)
570{
571 if (port->fcoe_pending_queue.qlen)
572 fcoe_check_wait_queue(port->lport, skb);
573 else if (fcoe_start_io(skb))
574 fcoe_check_wait_queue(port->lport, skb);
575}
576
ab6b85c1 577/**
1875f27e
RL
578 * fcoe_fip_send() - Send an Ethernet-encapsulated FIP frame
579 * @fip: The FCoE controller
580 * @skb: The FIP packet to be sent
ab6b85c1
VD
581 */
582static void fcoe_fip_send(struct fcoe_ctlr *fip, struct sk_buff *skb)
583{
d242e668
HR
584 struct fcoe_interface *fcoe = fcoe_from_ctlr(fip);
585 struct fip_frame {
586 struct ethhdr eth;
587 struct fip_header fip;
588 } __packed *frame;
589
590 /*
591 * Use default VLAN for FIP VLAN discovery protocol
592 */
593 frame = (struct fip_frame *)skb->data;
f7e6ed06
HR
594 if (ntohs(frame->eth.h_proto) == ETH_P_FIP &&
595 ntohs(frame->fip.fip_op) == FIP_OP_VLAN &&
d242e668
HR
596 fcoe->realdev != fcoe->netdev)
597 skb->dev = fcoe->realdev;
598 else
599 skb->dev = fcoe->netdev;
980f5156 600 fcoe_port_send(lport_priv(fip->lp), skb);
ab6b85c1
VD
601}
602
603/**
1875f27e
RL
604 * fcoe_update_src_mac() - Update the Ethernet MAC filters
605 * @lport: The local port to update the source MAC on
606 * @addr: Unicast MAC address to add
ab6b85c1
VD
607 *
608 * Remove any previously-set unicast MAC filter.
609 * Add secondary FCoE MAC address filter for our OUI.
610 */
11b56188 611static void fcoe_update_src_mac(struct fc_lport *lport, u8 *addr)
ab6b85c1 612{
11b56188 613 struct fcoe_port *port = lport_priv(lport);
8597ae8b 614 struct fcoe_interface *fcoe = port->priv;
25024989 615
11b56188 616 if (!is_zero_ether_addr(port->data_src_addr))
a748ee24 617 dev_uc_del(fcoe->netdev, port->data_src_addr);
11b56188 618 if (!is_zero_ether_addr(addr))
a748ee24 619 dev_uc_add(fcoe->netdev, addr);
11b56188 620 memcpy(port->data_src_addr, addr, ETH_ALEN);
ab6b85c1
VD
621}
622
11b56188
CL
623/**
624 * fcoe_get_src_mac() - return the Ethernet source address for an lport
625 * @lport: libfc lport
626 */
627static u8 *fcoe_get_src_mac(struct fc_lport *lport)
628{
629 struct fcoe_port *port = lport_priv(lport);
630
631 return port->data_src_addr;
632}
633
7f349142 634/**
1875f27e
RL
635 * fcoe_lport_config() - Set up a local port
636 * @lport: The local port to be setup
7f349142
VD
637 *
638 * Returns: 0 for success
639 */
1875f27e 640static int fcoe_lport_config(struct fc_lport *lport)
7f349142 641{
1875f27e
RL
642 lport->link_up = 0;
643 lport->qfull = 0;
644 lport->max_retry_count = 3;
645 lport->max_rport_retry_count = 3;
69aabcce
HR
646 lport->e_d_tov = fcoe_e_d_tov;
647 lport->r_a_tov = fcoe_r_a_tov;
1875f27e
RL
648 lport->service_params = (FCP_SPPF_INIT_FCN | FCP_SPPF_RD_XRDY_DIS |
649 FCP_SPPF_RETRY | FCP_SPPF_CONF_COMPL);
650 lport->does_npiv = 1;
651
652 fc_lport_init_stats(lport);
7f349142
VD
653
654 /* lport fc_lport related configuration */
1875f27e 655 fc_lport_config(lport);
7f349142
VD
656
657 /* offload related configuration */
1875f27e
RL
658 lport->crc_offload = 0;
659 lport->seq_offload = 0;
660 lport->lro_enabled = 0;
661 lport->lro_xid = 0;
662 lport->lso_max = 0;
7f349142
VD
663
664 return 0;
665}
666
54a5b21d
YZ
667/**
668 * fcoe_netdev_features_change - Updates the lport's offload flags based
669 * on the LLD netdev's FCoE feature flags
670 */
671static void fcoe_netdev_features_change(struct fc_lport *lport,
672 struct net_device *netdev)
673{
674 mutex_lock(&lport->lp_mutex);
675
676 if (netdev->features & NETIF_F_SG)
677 lport->sg_supp = 1;
678 else
679 lport->sg_supp = 0;
680
681 if (netdev->features & NETIF_F_FCOE_CRC) {
682 lport->crc_offload = 1;
683 FCOE_NETDEV_DBG(netdev, "Supports FCCRC offload\n");
684 } else {
685 lport->crc_offload = 0;
686 }
687
688 if (netdev->features & NETIF_F_FSO) {
689 lport->seq_offload = 1;
690 lport->lso_max = netdev->gso_max_size;
691 FCOE_NETDEV_DBG(netdev, "Supports LSO for max len 0x%x\n",
692 lport->lso_max);
693 } else {
694 lport->seq_offload = 0;
695 lport->lso_max = 0;
696 }
697
698 if (netdev->fcoe_ddp_xid) {
699 lport->lro_enabled = 1;
700 lport->lro_xid = netdev->fcoe_ddp_xid;
701 FCOE_NETDEV_DBG(netdev, "Supports LRO for max xid 0x%x\n",
702 lport->lro_xid);
703 } else {
704 lport->lro_enabled = 0;
705 lport->lro_xid = 0;
706 }
707
708 mutex_unlock(&lport->lp_mutex);
709}
710
7f349142 711/**
1875f27e
RL
712 * fcoe_netdev_config() - Set up net devive for SW FCoE
713 * @lport: The local port that is associated with the net device
714 * @netdev: The associated net device
7f349142 715 *
1875f27e 716 * Must be called after fcoe_lport_config() as it will use local port mutex
7f349142 717 *
1875f27e 718 * Returns: 0 for success
7f349142 719 */
1875f27e 720static int fcoe_netdev_config(struct fc_lport *lport, struct net_device *netdev)
7f349142
VD
721{
722 u32 mfs;
723 u64 wwnn, wwpn;
25024989 724 struct fcoe_interface *fcoe;
619fe4be 725 struct fcoe_ctlr *ctlr;
014f5c3f 726 struct fcoe_port *port;
7f349142
VD
727
728 /* Setup lport private data to point to fcoe softc */
1875f27e 729 port = lport_priv(lport);
8597ae8b 730 fcoe = port->priv;
619fe4be 731 ctlr = fcoe_to_ctlr(fcoe);
7f349142 732
9a6cf881
HR
733 /* Figure out the VLAN ID, if any */
734 if (netdev->priv_flags & IFF_802_1Q_VLAN)
735 lport->vlan = vlan_dev_vlan_id(netdev);
736 else
737 lport->vlan = 0;
738
7f349142
VD
739 /*
740 * Determine max frame size based on underlying device and optional
741 * user-configured limit. If the MFS is too low, fcoe_link_ok()
742 * will return 0, so do this first.
743 */
7221d7e5
YZ
744 mfs = netdev->mtu;
745 if (netdev->features & NETIF_F_FCOE_MTU) {
746 mfs = FCOE_MTU;
747 FCOE_NETDEV_DBG(netdev, "Supports FCOE_MTU of %d bytes\n", mfs);
748 }
749 mfs -= (sizeof(struct fcoe_hdr) + sizeof(struct fcoe_crc_eof));
1875f27e 750 if (fc_set_mfs(lport, mfs))
7f349142
VD
751 return -EINVAL;
752
7f349142 753 /* offload features support */
54a5b21d 754 fcoe_netdev_features_change(lport, netdev);
7f349142 755
014f5c3f
CL
756 skb_queue_head_init(&port->fcoe_pending_queue);
757 port->fcoe_pending_queue_active = 0;
1875f27e 758 setup_timer(&port->timer, fcoe_queue_timer, (unsigned long)lport);
7f349142 759
5e4f8fe7
RL
760 fcoe_link_speed_update(lport);
761
1875f27e 762 if (!lport->vport) {
dcece412 763 if (fcoe_get_wwn(netdev, &wwnn, NETDEV_FCOE_WWNN))
619fe4be 764 wwnn = fcoe_wwn_from_mac(ctlr->ctl_src_addr, 1, 0);
1875f27e 765 fc_set_wwnn(lport, wwnn);
dcece412 766 if (fcoe_get_wwn(netdev, &wwpn, NETDEV_FCOE_WWPN))
619fe4be 767 wwpn = fcoe_wwn_from_mac(ctlr->ctl_src_addr,
cf4aebca 768 2, 0);
1875f27e 769 fc_set_wwpn(lport, wwpn);
9a05753b 770 }
7f349142 771
7f349142
VD
772 return 0;
773}
774
775/**
1875f27e
RL
776 * fcoe_shost_config() - Set up the SCSI host associated with a local port
777 * @lport: The local port
1875f27e 778 * @dev: The device associated with the SCSI host
7f349142
VD
779 *
780 * Must be called after fcoe_lport_config() and fcoe_netdev_config()
781 *
1875f27e 782 * Returns: 0 for success
7f349142 783 */
8ba00a4b 784static int fcoe_shost_config(struct fc_lport *lport, struct device *dev)
7f349142
VD
785{
786 int rc = 0;
787
788 /* lport scsi host config */
1875f27e
RL
789 lport->host->max_lun = FCOE_MAX_LUN;
790 lport->host->max_id = FCOE_MAX_FCP_TARGET;
791 lport->host->max_channel = 0;
da87bfab
VD
792 lport->host->max_cmd_len = FCOE_MAX_CMD_LEN;
793
1875f27e 794 if (lport->vport)
8ca86f84 795 lport->host->transportt = fcoe_vport_scsi_transport;
e9084bb8 796 else
8ca86f84 797 lport->host->transportt = fcoe_nport_scsi_transport;
7f349142
VD
798
799 /* add the new host to the SCSI-ml */
1875f27e 800 rc = scsi_add_host(lport->host, dev);
7f349142 801 if (rc) {
1875f27e 802 FCOE_NETDEV_DBG(fcoe_netdev(lport), "fcoe_shost_config: "
d5488eb9 803 "error on scsi_add_host\n");
7f349142
VD
804 return rc;
805 }
9a05753b 806
1875f27e 807 if (!lport->vport)
4be929be 808 fc_host_max_npiv_vports(lport->host) = USHRT_MAX;
9a05753b 809
1875f27e 810 snprintf(fc_host_symbolic_name(lport->host), FC_SYMBOLIC_NAME_SIZE,
5baa17c3 811 "%s v%s over %s", FCOE_NAME, FCOE_VERSION,
1875f27e 812 fcoe_netdev(lport)->name);
7f349142
VD
813
814 return 0;
815}
816
6fef3902
NP
817
818/**
819 * fcoe_fdmi_info() - Get FDMI related info from net devive for SW FCoE
820 * @lport: The local port that is associated with the net device
821 * @netdev: The associated net device
822 *
823 * Must be called after fcoe_shost_config() as it will use local port mutex
824 *
825 */
826static void fcoe_fdmi_info(struct fc_lport *lport, struct net_device *netdev)
827{
828 struct fcoe_interface *fcoe;
829 struct fcoe_port *port;
830 struct net_device *realdev;
831 int rc;
6fef3902
NP
832
833 port = lport_priv(lport);
834 fcoe = port->priv;
835 realdev = fcoe->realdev;
836
6fef3902
NP
837 /* No FDMI state m/c for NPIV ports */
838 if (lport->vport)
839 return;
840
841 if (realdev->netdev_ops->ndo_fcoe_get_hbainfo) {
f07d46bb
NP
842 struct netdev_fcoe_hbainfo *fdmi;
843 fdmi = kzalloc(sizeof(*fdmi), GFP_KERNEL);
844 if (!fdmi)
845 return;
846
6fef3902 847 rc = realdev->netdev_ops->ndo_fcoe_get_hbainfo(realdev,
f07d46bb 848 fdmi);
6fef3902
NP
849 if (rc) {
850 printk(KERN_INFO "fcoe: Failed to retrieve FDMI "
851 "information from netdev.\n");
852 return;
853 }
854
855 snprintf(fc_host_serial_number(lport->host),
856 FC_SERIAL_NUMBER_SIZE,
857 "%s",
f07d46bb 858 fdmi->serial_number);
6fef3902
NP
859 snprintf(fc_host_manufacturer(lport->host),
860 FC_SERIAL_NUMBER_SIZE,
861 "%s",
f07d46bb 862 fdmi->manufacturer);
6fef3902
NP
863 snprintf(fc_host_model(lport->host),
864 FC_SYMBOLIC_NAME_SIZE,
865 "%s",
f07d46bb 866 fdmi->model);
6fef3902
NP
867 snprintf(fc_host_model_description(lport->host),
868 FC_SYMBOLIC_NAME_SIZE,
869 "%s",
f07d46bb 870 fdmi->model_description);
6fef3902
NP
871 snprintf(fc_host_hardware_version(lport->host),
872 FC_VERSION_STRING_SIZE,
873 "%s",
f07d46bb 874 fdmi->hardware_version);
6fef3902
NP
875 snprintf(fc_host_driver_version(lport->host),
876 FC_VERSION_STRING_SIZE,
877 "%s",
f07d46bb 878 fdmi->driver_version);
6fef3902
NP
879 snprintf(fc_host_optionrom_version(lport->host),
880 FC_VERSION_STRING_SIZE,
881 "%s",
f07d46bb 882 fdmi->optionrom_version);
6fef3902
NP
883 snprintf(fc_host_firmware_version(lport->host),
884 FC_VERSION_STRING_SIZE,
885 "%s",
f07d46bb 886 fdmi->firmware_version);
6fef3902
NP
887
888 /* Enable FDMI lport states */
889 lport->fdmi_enabled = 1;
f07d46bb 890 kfree(fdmi);
6fef3902
NP
891 } else {
892 lport->fdmi_enabled = 0;
893 printk(KERN_INFO "fcoe: No FDMI support.\n");
894 }
895}
896
1875f27e
RL
897/**
898 * fcoe_oem_match() - The match routine for the offloaded exchange manager
899 * @fp: The I/O frame
d7179680 900 *
1875f27e
RL
901 * This routine will be associated with an exchange manager (EM). When
902 * the libfc exchange handling code is looking for an EM to use it will
903 * call this routine and pass it the frame that it wishes to send. This
904 * routine will return True if the associated EM is to be used and False
905 * if the echange code should continue looking for an EM.
906 *
907 * The offload EM that this routine is associated with will handle any
908 * packets that are for SCSI read requests.
909 *
1ff9918b
KP
910 * This has been enhanced to work when FCoE stack is operating in target
911 * mode.
912 *
1875f27e 913 * Returns: True for read types I/O, otherwise returns false.
d7179680 914 */
7c9c6841 915static bool fcoe_oem_match(struct fc_frame *fp)
d7179680 916{
1ff9918b
KP
917 struct fc_frame_header *fh = fc_frame_header_get(fp);
918 struct fcp_cmnd *fcp;
919
920 if (fc_fcp_is_read(fr_fsp(fp)) &&
921 (fr_fsp(fp)->data_len > fcoe_ddp_min))
922 return true;
a762dce4
YZ
923 else if ((fr_fsp(fp) == NULL) &&
924 (fh->fh_r_ctl == FC_RCTL_DD_UNSOL_CMD) &&
925 (ntohs(fh->fh_rx_id) == FC_XID_UNKNOWN)) {
1ff9918b 926 fcp = fc_frame_payload_get(fp, sizeof(*fcp));
a762dce4
YZ
927 if ((fcp->fc_flags & FCP_CFL_WRDATA) &&
928 (ntohl(fcp->fc_dl) > fcoe_ddp_min))
1ff9918b
KP
929 return true;
930 }
931 return false;
d7179680
VD
932}
933
7f349142 934/**
1875f27e
RL
935 * fcoe_em_config() - Allocate and configure an exchange manager
936 * @lport: The local port that the new EM will be associated with
7f349142 937 *
1875f27e 938 * Returns: 0 on success
7f349142 939 */
1875f27e 940static inline int fcoe_em_config(struct fc_lport *lport)
7f349142 941{
1875f27e 942 struct fcoe_port *port = lport_priv(lport);
8597ae8b 943 struct fcoe_interface *fcoe = port->priv;
25024989 944 struct fcoe_interface *oldfcoe = NULL;
1d1b88dc 945 struct net_device *old_real_dev, *cur_real_dev;
d7179680
VD
946 u16 min_xid = FCOE_MIN_XID;
947 u16 max_xid = FCOE_MAX_XID;
948
949 /*
950 * Check if need to allocate an em instance for
951 * offload exchange ids to be shared across all VN_PORTs/lport.
952 */
1875f27e
RL
953 if (!lport->lro_enabled || !lport->lro_xid ||
954 (lport->lro_xid >= max_xid)) {
955 lport->lro_xid = 0;
d7179680
VD
956 goto skip_oem;
957 }
958
959 /*
960 * Reuse existing offload em instance in case
1d1b88dc 961 * it is already allocated on real eth device
d7179680 962 */
25024989
CL
963 if (fcoe->netdev->priv_flags & IFF_802_1Q_VLAN)
964 cur_real_dev = vlan_dev_real_dev(fcoe->netdev);
1d1b88dc 965 else
25024989 966 cur_real_dev = fcoe->netdev;
1d1b88dc 967
25024989 968 list_for_each_entry(oldfcoe, &fcoe_hostlist, list) {
25024989
CL
969 if (oldfcoe->netdev->priv_flags & IFF_802_1Q_VLAN)
970 old_real_dev = vlan_dev_real_dev(oldfcoe->netdev);
1d1b88dc 971 else
25024989 972 old_real_dev = oldfcoe->netdev;
1d1b88dc
VD
973
974 if (cur_real_dev == old_real_dev) {
991cbb60 975 fcoe->oem = oldfcoe->oem;
d7179680
VD
976 break;
977 }
978 }
979
991cbb60 980 if (fcoe->oem) {
1875f27e 981 if (!fc_exch_mgr_add(lport, fcoe->oem, fcoe_oem_match)) {
d7179680
VD
982 printk(KERN_ERR "fcoe_em_config: failed to add "
983 "offload em:%p on interface:%s\n",
991cbb60 984 fcoe->oem, fcoe->netdev->name);
d7179680
VD
985 return -ENOMEM;
986 }
987 } else {
1875f27e
RL
988 fcoe->oem = fc_exch_mgr_alloc(lport, FC_CLASS_3,
989 FCOE_MIN_XID, lport->lro_xid,
990 fcoe_oem_match);
991cbb60 991 if (!fcoe->oem) {
d7179680
VD
992 printk(KERN_ERR "fcoe_em_config: failed to allocate "
993 "em for offload exches on interface:%s\n",
25024989 994 fcoe->netdev->name);
d7179680
VD
995 return -ENOMEM;
996 }
997 }
998
999 /*
1000 * Exclude offload EM xid range from next EM xid range.
1001 */
1875f27e 1002 min_xid += lport->lro_xid + 1;
d7179680
VD
1003
1004skip_oem:
1875f27e 1005 if (!fc_exch_mgr_alloc(lport, FC_CLASS_3, min_xid, max_xid, NULL)) {
d7179680 1006 printk(KERN_ERR "fcoe_em_config: failed to "
25024989 1007 "allocate em on interface %s\n", fcoe->netdev->name);
7f349142 1008 return -ENOMEM;
d7179680 1009 }
7f349142
VD
1010
1011 return 0;
1012}
1013
1014/**
1875f27e
RL
1015 * fcoe_if_destroy() - Tear down a SW FCoE instance
1016 * @lport: The local port to be destroyed
34ce27bc 1017 *
7f349142 1018 */
af7f85d9 1019static void fcoe_if_destroy(struct fc_lport *lport)
7f349142 1020{
b2085a4e
NP
1021 struct fcoe_port *port = lport_priv(lport);
1022 struct fcoe_interface *fcoe = port->priv;
1023 struct net_device *netdev = fcoe->netdev;
1024
1025 FCOE_NETDEV_DBG(netdev, "Destroying interface\n");
1026
1027 /* Logout of the fabric */
1028 fc_fabric_logoff(lport);
1029
1030 /* Cleanup the fc_lport */
1031 fc_lport_destroy(lport);
1032
1033 /* Stop the transmit retry timer */
1034 del_timer_sync(&port->timer);
1035
1036 /* Free existing transmit skbs */
1037 fcoe_clean_pending_queue(lport);
1038
1039 rtnl_lock();
1040 if (!is_zero_ether_addr(port->data_src_addr))
1041 dev_uc_del(netdev, port->data_src_addr);
433eba04
VD
1042 if (lport->vport)
1043 synchronize_net();
1044 else
1045 fcoe_interface_remove(fcoe);
b2085a4e
NP
1046 rtnl_unlock();
1047
f161fb72 1048 /* Free queued packets for the per-CPU receive threads */
af7f85d9 1049 fcoe_percpu_clean(lport);
f161fb72 1050
7f349142 1051 /* Detach from the scsi-ml */
af7f85d9
CL
1052 fc_remove_host(lport->host);
1053 scsi_remove_host(lport->host);
7f349142 1054
80e736f8
YZ
1055 /* Destroy lport scsi_priv */
1056 fc_fcp_destroy(lport);
1057
7f349142 1058 /* There are no more rports or I/O, free the EM */
af7f85d9 1059 fc_exch_mgr_free(lport);
7f349142 1060
7f349142 1061 /* Free memory used by statistical counters */
af7f85d9 1062 fc_lport_free_stats(lport);
7f349142 1063
3cab4468
VD
1064 /*
1065 * Release the Scsi_Host for vport but hold on to
1066 * master lport until it fcoe interface fully cleaned-up.
1067 */
1068 if (lport->vport)
1069 scsi_host_put(lport->host);
7f349142
VD
1070}
1071
1875f27e
RL
1072/**
1073 * fcoe_ddp_setup() - Call a LLD's ddp_setup through the net device
1074 * @lport: The local port to setup DDP for
1075 * @xid: The exchange ID for this DDP transfer
1076 * @sgl: The scatterlist describing this transfer
1077 * @sgc: The number of sg items
7f349142 1078 *
1875f27e 1079 * Returns: 0 if the DDP context was not configured
7f349142 1080 */
1875f27e
RL
1081static int fcoe_ddp_setup(struct fc_lport *lport, u16 xid,
1082 struct scatterlist *sgl, unsigned int sgc)
7f349142 1083{
1875f27e 1084 struct net_device *netdev = fcoe_netdev(lport);
7f349142 1085
1875f27e
RL
1086 if (netdev->netdev_ops->ndo_fcoe_ddp_setup)
1087 return netdev->netdev_ops->ndo_fcoe_ddp_setup(netdev,
1088 xid, sgl,
1089 sgc);
7f349142
VD
1090
1091 return 0;
1092}
1093
71f89491
YZ
1094/**
1095 * fcoe_ddp_target() - Call a LLD's ddp_target through the net device
1096 * @lport: The local port to setup DDP for
1097 * @xid: The exchange ID for this DDP transfer
1098 * @sgl: The scatterlist describing this transfer
1099 * @sgc: The number of sg items
1100 *
1101 * Returns: 0 if the DDP context was not configured
1102 */
1103static int fcoe_ddp_target(struct fc_lport *lport, u16 xid,
1104 struct scatterlist *sgl, unsigned int sgc)
1105{
1106 struct net_device *netdev = fcoe_netdev(lport);
1107
1108 if (netdev->netdev_ops->ndo_fcoe_ddp_target)
1109 return netdev->netdev_ops->ndo_fcoe_ddp_target(netdev, xid,
1110 sgl, sgc);
1111
1112 return 0;
1113}
1114
1115
1875f27e
RL
1116/**
1117 * fcoe_ddp_done() - Call a LLD's ddp_done through the net device
1118 * @lport: The local port to complete DDP on
1119 * @xid: The exchange ID for this DDP transfer
7f349142 1120 *
1875f27e 1121 * Returns: the length of data that have been completed by DDP
7f349142 1122 */
1875f27e 1123static int fcoe_ddp_done(struct fc_lport *lport, u16 xid)
7f349142 1124{
1875f27e 1125 struct net_device *netdev = fcoe_netdev(lport);
7f349142 1126
1875f27e
RL
1127 if (netdev->netdev_ops->ndo_fcoe_ddp_done)
1128 return netdev->netdev_ops->ndo_fcoe_ddp_done(netdev, xid);
7f349142
VD
1129 return 0;
1130}
1131
7f349142 1132/**
1875f27e
RL
1133 * fcoe_if_create() - Create a FCoE instance on an interface
1134 * @fcoe: The FCoE interface to create a local port on
1135 * @parent: The device pointer to be the parent in sysfs for the SCSI host
1136 * @npiv: Indicates if the port is a vport or not
7f349142 1137 *
1875f27e 1138 * Creates a fc_lport instance and a Scsi_Host instance and configure them.
7f349142 1139 *
1875f27e 1140 * Returns: The allocated fc_lport or an error pointer
7f349142 1141 */
030f4e00 1142static struct fc_lport *fcoe_if_create(struct fcoe_interface *fcoe,
9a05753b 1143 struct device *parent, int npiv)
7f349142 1144{
619fe4be 1145 struct fcoe_ctlr *ctlr = fcoe_to_ctlr(fcoe);
1875f27e 1146 struct net_device *netdev = fcoe->netdev;
72fa396b 1147 struct fc_lport *lport, *n_port;
014f5c3f 1148 struct fcoe_port *port;
72fa396b 1149 struct Scsi_Host *shost;
1875f27e 1150 int rc;
9a05753b
CL
1151 /*
1152 * parent is only a vport if npiv is 1,
1153 * but we'll only use vport in that case so go ahead and set it
1154 */
1155 struct fc_vport *vport = dev_to_vport(parent);
7f349142 1156
d5488eb9 1157 FCOE_NETDEV_DBG(netdev, "Create Interface\n");
7f349142 1158
72fa396b
VD
1159 if (!npiv)
1160 lport = libfc_host_alloc(&fcoe_shost_template, sizeof(*port));
1161 else
1162 lport = libfc_vport_create(vport, sizeof(*port));
1163
86221969 1164 if (!lport) {
d5488eb9 1165 FCOE_NETDEV_DBG(netdev, "Could not allocate host structure\n");
af7f85d9 1166 rc = -ENOMEM;
030f4e00 1167 goto out;
7f349142 1168 }
014f5c3f 1169 port = lport_priv(lport);
2e70e241 1170 port->lport = lport;
8597ae8b 1171 port->priv = fcoe;
66524ec9 1172 port->get_netdev = fcoe_netdev;
8597ae8b
BPG
1173 port->max_queue_depth = FCOE_MAX_QUEUE_DEPTH;
1174 port->min_queue_depth = FCOE_MIN_QUEUE_DEPTH;
2e70e241 1175 INIT_WORK(&port->destroy_work, fcoe_destroy_work);
7f349142 1176
f9184df3
NH
1177 /*
1178 * Need to add the lport to the hostlist
1179 * so we catch NETDEV_CHANGE events.
1180 */
1181 fcoe_hostlist_add(lport);
1182
1875f27e 1183 /* configure a fc_lport including the exchange manager */
af7f85d9 1184 rc = fcoe_lport_config(lport);
7f349142 1185 if (rc) {
d5488eb9
RL
1186 FCOE_NETDEV_DBG(netdev, "Could not configure lport for the "
1187 "interface\n");
7f349142
VD
1188 goto out_host_put;
1189 }
1190
9a05753b 1191 if (npiv) {
9f8f3aa6
CL
1192 FCOE_NETDEV_DBG(netdev, "Setting vport names, "
1193 "%16.16llx %16.16llx\n",
1875f27e 1194 vport->node_name, vport->port_name);
9a05753b
CL
1195 fc_set_wwnn(lport, vport->node_name);
1196 fc_set_wwpn(lport, vport->port_name);
1197 }
1198
ab6b85c1 1199 /* configure lport network properties */
af7f85d9 1200 rc = fcoe_netdev_config(lport, netdev);
ab6b85c1 1201 if (rc) {
d5488eb9
RL
1202 FCOE_NETDEV_DBG(netdev, "Could not configure netdev for the "
1203 "interface\n");
54b649f8 1204 goto out_lp_destroy;
ab6b85c1 1205 }
97c8389d 1206
7f349142 1207 /* configure lport scsi host properties */
8ba00a4b 1208 rc = fcoe_shost_config(lport, parent);
7f349142 1209 if (rc) {
d5488eb9
RL
1210 FCOE_NETDEV_DBG(netdev, "Could not configure shost for the "
1211 "interface\n");
54b649f8 1212 goto out_lp_destroy;
7f349142
VD
1213 }
1214
96316099 1215 /* Initialize the library */
619fe4be 1216 rc = fcoe_libfc_config(lport, ctlr, &fcoe_libfc_fcn_templ, 1);
7f349142 1217 if (rc) {
96316099 1218 FCOE_NETDEV_DBG(netdev, "Could not configure libfc for the "
d5488eb9 1219 "interface\n");
96316099 1220 goto out_lp_destroy;
7f349142
VD
1221 }
1222
6fef3902
NP
1223 /* Initialized FDMI information */
1224 fcoe_fdmi_info(lport, netdev);
1225
72fa396b
VD
1226 /*
1227 * fcoe_em_alloc() and fcoe_hostlist_add() both
1228 * need to be atomic with respect to other changes to the
1229 * hostlist since fcoe_em_alloc() looks for an existing EM
1230 * instance on host list updated by fcoe_hostlist_add().
1231 *
1232 * This is currently handled through the fcoe_config_mutex
1233 * begin held.
1234 */
1235 if (!npiv)
9a05753b
CL
1236 /* lport exch manager allocation */
1237 rc = fcoe_em_config(lport);
72fa396b
VD
1238 else {
1239 shost = vport_to_shost(vport);
1240 n_port = shost_priv(shost);
1241 rc = fc_exch_mgr_list_clone(n_port, lport);
1242 }
1243
1244 if (rc) {
1245 FCOE_NETDEV_DBG(netdev, "Could not configure the EM\n");
1246 goto out_lp_destroy;
7f349142
VD
1247 }
1248
af7f85d9 1249 return lport;
7f349142
VD
1250
1251out_lp_destroy:
af7f85d9 1252 fc_exch_mgr_free(lport);
7f349142 1253out_host_put:
f9184df3 1254 fcoe_hostlist_del(lport);
af7f85d9
CL
1255 scsi_host_put(lport->host);
1256out:
1257 return ERR_PTR(rc);
7f349142
VD
1258}
1259
1260/**
1875f27e 1261 * fcoe_if_init() - Initialization routine for fcoe.ko
7f349142 1262 *
1875f27e
RL
1263 * Attaches the SW FCoE transport to the FC transport
1264 *
1265 * Returns: 0 on success
7f349142
VD
1266 */
1267static int __init fcoe_if_init(void)
1268{
1269 /* attach to scsi transport */
8ca86f84
YZ
1270 fcoe_nport_scsi_transport =
1271 fc_attach_transport(&fcoe_nport_fc_functions);
1272 fcoe_vport_scsi_transport =
1273 fc_attach_transport(&fcoe_vport_fc_functions);
7f349142 1274
8ca86f84 1275 if (!fcoe_nport_scsi_transport) {
d5488eb9 1276 printk(KERN_ERR "fcoe: Failed to attach to the FC transport\n");
7f349142
VD
1277 return -ENODEV;
1278 }
1279
1280 return 0;
1281}
1282
1283/**
1875f27e
RL
1284 * fcoe_if_exit() - Tear down fcoe.ko
1285 *
1286 * Detaches the SW FCoE transport from the FC transport
7f349142 1287 *
1875f27e 1288 * Returns: 0 on success
7f349142 1289 */
7c9c6841 1290static int __exit fcoe_if_exit(void)
7f349142 1291{
8ca86f84
YZ
1292 fc_release_transport(fcoe_nport_scsi_transport);
1293 fc_release_transport(fcoe_vport_scsi_transport);
1294 fcoe_nport_scsi_transport = NULL;
1295 fcoe_vport_scsi_transport = NULL;
7f349142
VD
1296 return 0;
1297}
1298
4b9bc86d 1299static void fcoe_thread_cleanup_local(unsigned int cpu)
8976f424 1300{
8976f424 1301 struct page *crc_eof;
4b9bc86d 1302 struct fcoe_percpu_s *p;
8976f424 1303
4b9bc86d 1304 p = per_cpu_ptr(&fcoe_percpu, cpu);
8976f424 1305 spin_lock_bh(&p->fcoe_rx_list.lock);
8976f424
RL
1306 crc_eof = p->crc_eof_page;
1307 p->crc_eof_page = NULL;
1308 p->crc_eof_offset = 0;
1309 spin_unlock_bh(&p->fcoe_rx_list.lock);
1310
8976f424
RL
1311 if (crc_eof)
1312 put_page(crc_eof);
4b9bc86d 1313 flush_work(&p->work);
8976f424
RL
1314}
1315
064287ee
KP
1316/**
1317 * fcoe_select_cpu() - Selects CPU to handle post-processing of incoming
1318 * command.
064287ee 1319 *
d272281c
VD
1320 * This routine selects next CPU based on cpumask to distribute
1321 * incoming requests in round robin.
064287ee 1322 *
d272281c 1323 * Returns: int CPU number
064287ee 1324 */
d272281c 1325static inline unsigned int fcoe_select_cpu(void)
064287ee
KP
1326{
1327 static unsigned int selected_cpu;
1328
d272281c
VD
1329 selected_cpu = cpumask_next(selected_cpu, cpu_online_mask);
1330 if (selected_cpu >= nr_cpu_ids)
1331 selected_cpu = cpumask_first(cpu_online_mask);
1332
064287ee
KP
1333 return selected_cpu;
1334}
1335
85b4aa49 1336/**
1875f27e
RL
1337 * fcoe_rcv() - Receive packets from a net device
1338 * @skb: The received packet
1339 * @netdev: The net device that the packet was received on
1340 * @ptype: The packet type context
1341 * @olddev: The last device net device
85b4aa49 1342 *
1875f27e
RL
1343 * This routine is called by NET_RX_SOFTIRQ. It receives a packet, builds a
1344 * FC frame and passes the frame to libfc.
85b4aa49
RL
1345 *
1346 * Returns: 0 for success
34f42a07 1347 */
7c9c6841 1348static int fcoe_rcv(struct sk_buff *skb, struct net_device *netdev,
85b4aa49
RL
1349 struct packet_type *ptype, struct net_device *olddev)
1350{
1875f27e 1351 struct fc_lport *lport;
85b4aa49 1352 struct fcoe_rcv_info *fr;
619fe4be 1353 struct fcoe_ctlr *ctlr;
259ad85d 1354 struct fcoe_interface *fcoe;
85b4aa49 1355 struct fc_frame_header *fh;
85b4aa49 1356 struct fcoe_percpu_s *fps;
519e5135 1357 struct ethhdr *eh;
b2f0091f 1358 unsigned int cpu;
85b4aa49 1359
259ad85d 1360 fcoe = container_of(ptype, struct fcoe_interface, fcoe_packet_type);
619fe4be
RL
1361 ctlr = fcoe_to_ctlr(fcoe);
1362 lport = ctlr->lp;
1875f27e 1363 if (unlikely(!lport)) {
465b87bf 1364 FCOE_NETDEV_DBG(netdev, "Cannot find hba structure\n");
85b4aa49
RL
1365 goto err2;
1366 }
1875f27e 1367 if (!lport->link_up)
97c8389d 1368 goto err2;
85b4aa49 1369
465b87bf
BVA
1370 FCOE_NETDEV_DBG(netdev,
1371 "skb_info: len:%d data_len:%d head:%p data:%p tail:%p end:%p sum:%d dev:%s\n",
d5488eb9
RL
1372 skb->len, skb->data_len, skb->head, skb->data,
1373 skb_tail_pointer(skb), skb_end_pointer(skb),
1374 skb->csum, skb->dev ? skb->dev->name : "<NULL>");
85b4aa49 1375
8b612434
NH
1376
1377 skb = skb_share_check(skb, GFP_ATOMIC);
1378
1379 if (skb == NULL)
1380 return NET_RX_DROP;
1381
519e5135 1382 eh = eth_hdr(skb);
519e5135 1383
619fe4be 1384 if (is_fip_mode(ctlr) &&
6942df7f 1385 !ether_addr_equal(eh->h_source, ctlr->dest_addr)) {
519e5135
VD
1386 FCOE_NETDEV_DBG(netdev, "wrong source mac address:%pM\n",
1387 eh->h_source);
85b4aa49
RL
1388 goto err;
1389 }
1390
1391 /*
1392 * Check for minimum frame length, and make sure required FCoE
1393 * and FC headers are pulled into the linear data area.
1394 */
1395 if (unlikely((skb->len < FCOE_MIN_FRAME) ||
1875f27e 1396 !pskb_may_pull(skb, FCOE_HEADER_LEN)))
85b4aa49
RL
1397 goto err;
1398
1399 skb_set_transport_header(skb, sizeof(struct fcoe_hdr));
1400 fh = (struct fc_frame_header *) skb_transport_header(skb);
1401
0ee31cb5
RL
1402 if (ntoh24(&eh->h_dest[3]) != ntoh24(fh->fh_d_id)) {
1403 FCOE_NETDEV_DBG(netdev, "FC frame d_id mismatch with MAC:%pM\n",
1404 eh->h_dest);
1405 goto err;
1406 }
1407
85b4aa49 1408 fr = fcoe_dev_from_skb(skb);
1875f27e 1409 fr->fr_dev = lport;
5e5e92df 1410
85b4aa49 1411 /*
b2f0091f
VD
1412 * In case the incoming frame's exchange is originated from
1413 * the initiator, then received frame's exchange id is ANDed
1414 * with fc_cpu_mask bits to get the same cpu on which exchange
d272281c
VD
1415 * was originated, otherwise select cpu using rx exchange id
1416 * or fcoe_select_cpu().
85b4aa49 1417 */
b2f0091f
VD
1418 if (ntoh24(fh->fh_f_ctl) & FC_FC_EX_CTX)
1419 cpu = ntohs(fh->fh_ox_id) & fc_cpu_mask;
064287ee 1420 else {
d272281c
VD
1421 if (ntohs(fh->fh_rx_id) == FC_XID_UNKNOWN)
1422 cpu = fcoe_select_cpu();
1423 else
29bdd2bb 1424 cpu = ntohs(fh->fh_rx_id) & fc_cpu_mask;
064287ee 1425 }
324f6678
VD
1426
1427 if (cpu >= nr_cpu_ids)
1428 goto err;
1429
8976f424 1430 fps = &per_cpu(fcoe_percpu, cpu);
94aa29f2 1431 spin_lock(&fps->fcoe_rx_list.lock);
8976f424
RL
1432 /*
1433 * We now have a valid CPU that we're targeting for
1434 * this skb. We also have this receive thread locked,
1435 * so we're free to queue skbs into it's queue.
1436 */
85b4aa49 1437
5e70c4c4
NH
1438 /*
1439 * Note: We used to have a set of conditions under which we would
1440 * call fcoe_recv_frame directly, rather than queuing to the rx list
1441 * as it could save a few cycles, but doing so is prohibited, as
1442 * fcoe_recv_frame has several paths that may sleep, which is forbidden
1443 * in softirq context.
859b7b64 1444 */
5e70c4c4 1445 __skb_queue_tail(&fps->fcoe_rx_list, skb);
4b9bc86d 1446 schedule_work_on(cpu, &fps->work);
94aa29f2 1447 spin_unlock(&fps->fcoe_rx_list.lock);
85b4aa49 1448
34bac2ef 1449 return NET_RX_SUCCESS;
85b4aa49 1450err:
1bd49b48 1451 per_cpu_ptr(lport->stats, get_cpu())->ErrorFrames++;
f018b73a 1452 put_cpu();
85b4aa49
RL
1453err2:
1454 kfree_skb(skb);
34bac2ef 1455 return NET_RX_DROP;
85b4aa49 1456}
85b4aa49
RL
1457
1458/**
8597ae8b 1459 * fcoe_alloc_paged_crc_eof() - Allocate a page to be used for the trailer CRC
1875f27e
RL
1460 * @skb: The packet to be transmitted
1461 * @tlen: The total length of the trailer
1462 *
85b4aa49 1463 * Returns: 0 for success
34f42a07 1464 */
8597ae8b 1465static int fcoe_alloc_paged_crc_eof(struct sk_buff *skb, int tlen)
85b4aa49
RL
1466{
1467 struct fcoe_percpu_s *fps;
8597ae8b 1468 int rc;
85b4aa49 1469
5e5e92df 1470 fps = &get_cpu_var(fcoe_percpu);
8597ae8b 1471 rc = fcoe_get_paged_crc_eof(skb, tlen, fps);
5e5e92df 1472 put_cpu_var(fcoe_percpu);
85b4aa49 1473
8597ae8b 1474 return rc;
85b4aa49 1475}
85b4aa49
RL
1476
1477/**
1875f27e
RL
1478 * fcoe_xmit() - Transmit a FCoE frame
1479 * @lport: The local port that the frame is to be transmitted for
1480 * @fp: The frame to be transmitted
85b4aa49 1481 *
1875f27e 1482 * Return: 0 for success
34f42a07 1483 */
7c9c6841 1484static int fcoe_xmit(struct fc_lport *lport, struct fc_frame *fp)
85b4aa49 1485{
4bb6b515 1486 int wlen;
85b4aa49
RL
1487 u32 crc;
1488 struct ethhdr *eh;
1489 struct fcoe_crc_eof *cp;
1490 struct sk_buff *skb;
1bd49b48 1491 struct fc_stats *stats;
85b4aa49
RL
1492 struct fc_frame_header *fh;
1493 unsigned int hlen; /* header length implies the version */
1494 unsigned int tlen; /* trailer length */
1495 unsigned int elen; /* eth header, may include vlan */
1875f27e 1496 struct fcoe_port *port = lport_priv(lport);
8597ae8b 1497 struct fcoe_interface *fcoe = port->priv;
619fe4be 1498 struct fcoe_ctlr *ctlr = fcoe_to_ctlr(fcoe);
85b4aa49
RL
1499 u8 sof, eof;
1500 struct fcoe_hdr *hp;
1501
1502 WARN_ON((fr_len(fp) % sizeof(u32)) != 0);
1503
85b4aa49 1504 fh = fc_frame_header_get(fp);
97c8389d
JE
1505 skb = fp_skb(fp);
1506 wlen = skb->len / FCOE_WORD_TO_BYTE;
1507
1875f27e 1508 if (!lport->link_up) {
3caf02ee 1509 kfree_skb(skb);
97c8389d 1510 return 0;
85b4aa49
RL
1511 }
1512
9860eeb4 1513 if (unlikely(fh->fh_type == FC_TYPE_ELS) &&
619fe4be 1514 fcoe_ctlr_els_send(ctlr, lport, skb))
97c8389d
JE
1515 return 0;
1516
85b4aa49
RL
1517 sof = fr_sof(fp);
1518 eof = fr_eof(fp);
1519
4e57e1cb 1520 elen = sizeof(struct ethhdr);
85b4aa49
RL
1521 hlen = sizeof(struct fcoe_hdr);
1522 tlen = sizeof(struct fcoe_crc_eof);
1523 wlen = (skb->len - tlen + sizeof(crc)) / FCOE_WORD_TO_BYTE;
1524
1525 /* crc offload */
1875f27e 1526 if (likely(lport->crc_offload)) {
253aab05 1527 skb->ip_summed = CHECKSUM_PARTIAL;
85b4aa49
RL
1528 skb->csum_start = skb_headroom(skb);
1529 skb->csum_offset = skb->len;
1530 crc = 0;
1531 } else {
1532 skb->ip_summed = CHECKSUM_NONE;
1533 crc = fcoe_fc_crc(fp);
1534 }
1535
014f5c3f 1536 /* copy port crc and eof to the skb buff */
85b4aa49
RL
1537 if (skb_is_nonlinear(skb)) {
1538 skb_frag_t *frag;
8597ae8b 1539 if (fcoe_alloc_paged_crc_eof(skb, tlen)) {
e9041581 1540 kfree_skb(skb);
85b4aa49
RL
1541 return -ENOMEM;
1542 }
1543 frag = &skb_shinfo(skb)->frags[skb_shinfo(skb)->nr_frags - 1];
77dfce07 1544 cp = kmap_atomic(skb_frag_page(frag))
85b4aa49
RL
1545 + frag->page_offset;
1546 } else {
1547 cp = (struct fcoe_crc_eof *)skb_put(skb, tlen);
1548 }
1549
1550 memset(cp, 0, sizeof(*cp));
1551 cp->fcoe_eof = eof;
1552 cp->fcoe_crc32 = cpu_to_le32(~crc);
1553
1554 if (skb_is_nonlinear(skb)) {
77dfce07 1555 kunmap_atomic(cp);
85b4aa49
RL
1556 cp = NULL;
1557 }
1558
014f5c3f 1559 /* adjust skb network/transport offsets to match mac/fcoe/port */
85b4aa49
RL
1560 skb_push(skb, elen + hlen);
1561 skb_reset_mac_header(skb);
1562 skb_reset_network_header(skb);
1563 skb->mac_len = elen;
211c738d 1564 skb->protocol = htons(ETH_P_FCOE);
31c37a6f 1565 skb->priority = fcoe->priority;
6f6c2aa3 1566
d1483bb9 1567 if (fcoe->netdev->priv_flags & IFF_802_1Q_VLAN &&
f646968f 1568 fcoe->realdev->features & NETIF_F_HW_VLAN_CTAG_TX) {
2884d423 1569 /* must set skb->dev before calling vlan_put_tag */
d1483bb9 1570 skb->dev = fcoe->realdev;
b960a0ac
JP
1571 __vlan_hwaccel_put_tag(skb, htons(ETH_P_8021Q),
1572 vlan_dev_vlan_id(fcoe->netdev));
d1483bb9
VD
1573 } else
1574 skb->dev = fcoe->netdev;
85b4aa49
RL
1575
1576 /* fill up mac and fcoe headers */
1577 eh = eth_hdr(skb);
1578 eh->h_proto = htons(ETH_P_FCOE);
619fe4be
RL
1579 memcpy(eh->h_dest, ctlr->dest_addr, ETH_ALEN);
1580 if (ctlr->map_dest)
cd229e42 1581 memcpy(eh->h_dest + 3, fh->fh_d_id, 3);
85b4aa49 1582
619fe4be
RL
1583 if (unlikely(ctlr->flogi_oxid != FC_XID_UNKNOWN))
1584 memcpy(eh->h_source, ctlr->ctl_src_addr, ETH_ALEN);
85b4aa49 1585 else
11b56188 1586 memcpy(eh->h_source, port->data_src_addr, ETH_ALEN);
85b4aa49
RL
1587
1588 hp = (struct fcoe_hdr *)(eh + 1);
1589 memset(hp, 0, sizeof(*hp));
1590 if (FC_FCOE_VER)
1591 FC_FCOE_ENCAPS_VER(hp, FC_FCOE_VER);
1592 hp->fcoe_sof = sof;
1593
39ca9a06 1594 /* fcoe lso, mss is in max_payload which is non-zero for FCP data */
1875f27e 1595 if (lport->seq_offload && fr_max_payload(fp)) {
39ca9a06
YZ
1596 skb_shinfo(skb)->gso_type = SKB_GSO_FCOE;
1597 skb_shinfo(skb)->gso_size = fr_max_payload(fp);
1598 } else {
1599 skb_shinfo(skb)->gso_type = 0;
1600 skb_shinfo(skb)->gso_size = 0;
1601 }
85b4aa49 1602 /* update tx stats: regardless if LLD fails */
1bd49b48 1603 stats = per_cpu_ptr(lport->stats, get_cpu());
582b45bc
RL
1604 stats->TxFrames++;
1605 stats->TxWords += wlen;
f018b73a 1606 put_cpu();
85b4aa49
RL
1607
1608 /* send down to lld */
1875f27e 1609 fr_dev(fp) = lport;
980f5156 1610 fcoe_port_send(port, skb);
85b4aa49
RL
1611 return 0;
1612}
85b4aa49 1613
52ee8321
VD
1614/**
1615 * fcoe_filter_frames() - filter out bad fcoe frames, i.e. bad CRC
1616 * @lport: The local port the frame was received on
1617 * @fp: The received frame
1618 *
1619 * Return: 0 on passing filtering checks
1620 */
1621static inline int fcoe_filter_frames(struct fc_lport *lport,
1622 struct fc_frame *fp)
1623{
619fe4be 1624 struct fcoe_ctlr *ctlr;
52ee8321
VD
1625 struct fcoe_interface *fcoe;
1626 struct fc_frame_header *fh;
1627 struct sk_buff *skb = (struct sk_buff *)fp;
1bd49b48 1628 struct fc_stats *stats;
52ee8321
VD
1629
1630 /*
1631 * We only check CRC if no offload is available and if it is
1632 * it's solicited data, in which case, the FCP layer would
1633 * check it during the copy.
1634 */
1635 if (lport->crc_offload && skb->ip_summed == CHECKSUM_UNNECESSARY)
1636 fr_flags(fp) &= ~FCPHF_CRC_UNCHECKED;
1637 else
1638 fr_flags(fp) |= FCPHF_CRC_UNCHECKED;
1639
1640 fh = (struct fc_frame_header *) skb_transport_header(skb);
1641 fh = fc_frame_header_get(fp);
1642 if (fh->fh_r_ctl == FC_RCTL_DD_SOL_DATA && fh->fh_type == FC_TYPE_FCP)
1643 return 0;
1644
8597ae8b 1645 fcoe = ((struct fcoe_port *)lport_priv(lport))->priv;
619fe4be
RL
1646 ctlr = fcoe_to_ctlr(fcoe);
1647 if (is_fip_mode(ctlr) && fc_frame_payload_op(fp) == ELS_LOGO &&
52ee8321
VD
1648 ntoh24(fh->fh_s_id) == FC_FID_FLOGI) {
1649 FCOE_DBG("fcoe: dropping FCoE lport LOGO in fip mode\n");
1650 return -EINVAL;
1651 }
1652
f2f96d20 1653 if (!(fr_flags(fp) & FCPHF_CRC_UNCHECKED) ||
52ee8321
VD
1654 le32_to_cpu(fr_crc(fp)) == ~crc32(~0, skb->data, skb->len)) {
1655 fr_flags(fp) &= ~FCPHF_CRC_UNCHECKED;
1656 return 0;
1657 }
1658
1bd49b48 1659 stats = per_cpu_ptr(lport->stats, get_cpu());
52ee8321
VD
1660 stats->InvalidCRCCount++;
1661 if (stats->InvalidCRCCount < 5)
1662 printk(KERN_WARNING "fcoe: dropping frame with CRC error\n");
7e1e7ead 1663 put_cpu();
52ee8321
VD
1664 return -EINVAL;
1665}
1666
34f42a07 1667/**
859b7b64
CL
1668 * fcoe_recv_frame() - process a single received frame
1669 * @skb: frame to process
85b4aa49 1670 */
859b7b64 1671static void fcoe_recv_frame(struct sk_buff *skb)
85b4aa49 1672{
85b4aa49 1673 u32 fr_len;
1875f27e 1674 struct fc_lport *lport;
85b4aa49 1675 struct fcoe_rcv_info *fr;
1bd49b48 1676 struct fc_stats *stats;
85b4aa49
RL
1677 struct fcoe_crc_eof crc_eof;
1678 struct fc_frame *fp;
014f5c3f 1679 struct fcoe_port *port;
85b4aa49
RL
1680 struct fcoe_hdr *hp;
1681
859b7b64
CL
1682 fr = fcoe_dev_from_skb(skb);
1683 lport = fr->fr_dev;
1684 if (unlikely(!lport)) {
4b9bc86d 1685 FCOE_NETDEV_DBG(skb->dev, "NULL lport in skb\n");
859b7b64
CL
1686 kfree_skb(skb);
1687 return;
1688 }
1689
465b87bf
BVA
1690 FCOE_NETDEV_DBG(skb->dev,
1691 "skb_info: len:%d data_len:%d head:%p data:%p tail:%p end:%p sum:%d dev:%s\n",
859b7b64
CL
1692 skb->len, skb->data_len,
1693 skb->head, skb->data, skb_tail_pointer(skb),
1694 skb_end_pointer(skb), skb->csum,
1695 skb->dev ? skb->dev->name : "<NULL>");
1696
859b7b64 1697 port = lport_priv(lport);
f1633011 1698 skb_linearize(skb); /* check for skb_is_nonlinear is within skb_linearize */
859b7b64
CL
1699
1700 /*
1701 * Frame length checks and setting up the header pointers
1702 * was done in fcoe_rcv already.
1703 */
1704 hp = (struct fcoe_hdr *) skb_network_header(skb);
859b7b64 1705
1bd49b48 1706 stats = per_cpu_ptr(lport->stats, get_cpu());
859b7b64
CL
1707 if (unlikely(FC_FCOE_DECAPS_VER(hp) != FC_FCOE_VER)) {
1708 if (stats->ErrorFrames < 5)
1709 printk(KERN_WARNING "fcoe: FCoE version "
1710 "mismatch: The frame has "
1711 "version %x, but the "
1712 "initiator supports version "
1713 "%x\n", FC_FCOE_DECAPS_VER(hp),
1714 FC_FCOE_VER);
f018b73a 1715 goto drop;
859b7b64
CL
1716 }
1717
1718 skb_pull(skb, sizeof(struct fcoe_hdr));
1719 fr_len = skb->len - sizeof(struct fcoe_crc_eof);
1720
1721 stats->RxFrames++;
1722 stats->RxWords += fr_len / FCOE_WORD_TO_BYTE;
1723
1724 fp = (struct fc_frame *)skb;
1725 fc_frame_init(fp);
1726 fr_dev(fp) = lport;
1727 fr_sof(fp) = hp->fcoe_sof;
1728
1729 /* Copy out the CRC and EOF trailer for access */
f018b73a
JE
1730 if (skb_copy_bits(skb, fr_len, &crc_eof, sizeof(crc_eof)))
1731 goto drop;
859b7b64
CL
1732 fr_eof(fp) = crc_eof.fcoe_eof;
1733 fr_crc(fp) = crc_eof.fcoe_crc32;
f018b73a
JE
1734 if (pskb_trim(skb, fr_len))
1735 goto drop;
859b7b64 1736
52ee8321
VD
1737 if (!fcoe_filter_frames(lport, fp)) {
1738 put_cpu();
1739 fc_exch_recv(lport, fp);
1740 return;
859b7b64 1741 }
f018b73a
JE
1742drop:
1743 stats->ErrorFrames++;
1744 put_cpu();
1745 kfree_skb(skb);
859b7b64
CL
1746}
1747
1748/**
4b9bc86d
SAS
1749 * fcoe_receive_work() - The per-CPU worker
1750 * @work: The work struct
859b7b64 1751 *
859b7b64 1752 */
4b9bc86d 1753static void fcoe_receive_work(struct work_struct *work)
859b7b64 1754{
4b9bc86d 1755 struct fcoe_percpu_s *p;
859b7b64 1756 struct sk_buff *skb;
20dc3811
NH
1757 struct sk_buff_head tmp;
1758
4b9bc86d 1759 p = container_of(work, struct fcoe_percpu_s, work);
20dc3811 1760 skb_queue_head_init(&tmp);
859b7b64 1761
4b9bc86d
SAS
1762 spin_lock_bh(&p->fcoe_rx_list.lock);
1763 skb_queue_splice_init(&p->fcoe_rx_list, &tmp);
1764 spin_unlock_bh(&p->fcoe_rx_list.lock);
95fdd5e9 1765
4b9bc86d
SAS
1766 if (!skb_queue_len(&tmp))
1767 return;
95fdd5e9 1768
4b9bc86d
SAS
1769 while ((skb = __skb_dequeue(&tmp)))
1770 fcoe_recv_frame(skb);
85b4aa49
RL
1771}
1772
85b4aa49 1773/**
1875f27e 1774 * fcoe_dev_setup() - Setup the link change notification interface
34f42a07 1775 */
b0d428ad 1776static void fcoe_dev_setup(void)
85b4aa49 1777{
6f6c2aa3 1778 register_dcbevent_notifier(&dcb_notifier);
85b4aa49
RL
1779 register_netdevice_notifier(&fcoe_notifier);
1780}
1781
1782/**
1875f27e 1783 * fcoe_dev_cleanup() - Cleanup the link change notification interface
34f42a07 1784 */
85b4aa49
RL
1785static void fcoe_dev_cleanup(void)
1786{
6f6c2aa3 1787 unregister_dcbevent_notifier(&dcb_notifier);
85b4aa49
RL
1788 unregister_netdevice_notifier(&fcoe_notifier);
1789}
1790
6f6c2aa3 1791static struct fcoe_interface *
1792fcoe_hostlist_lookup_realdev_port(struct net_device *netdev)
1793{
1794 struct fcoe_interface *fcoe;
1795 struct net_device *real_dev;
1796
1797 list_for_each_entry(fcoe, &fcoe_hostlist, list) {
1798 if (fcoe->netdev->priv_flags & IFF_802_1Q_VLAN)
1799 real_dev = vlan_dev_real_dev(fcoe->netdev);
1800 else
1801 real_dev = fcoe->netdev;
1802
1803 if (netdev == real_dev)
1804 return fcoe;
1805 }
1806 return NULL;
1807}
1808
1809static int fcoe_dcb_app_notification(struct notifier_block *notifier,
1810 ulong event, void *ptr)
1811{
1812 struct dcb_app_type *entry = ptr;
619fe4be 1813 struct fcoe_ctlr *ctlr;
6f6c2aa3 1814 struct fcoe_interface *fcoe;
1815 struct net_device *netdev;
6f6c2aa3 1816 int prio;
1817
1818 if (entry->app.selector != DCB_APP_IDTYPE_ETHTYPE)
1819 return NOTIFY_OK;
1820
1821 netdev = dev_get_by_index(&init_net, entry->ifindex);
1822 if (!netdev)
1823 return NOTIFY_OK;
1824
1825 fcoe = fcoe_hostlist_lookup_realdev_port(netdev);
1826 dev_put(netdev);
1827 if (!fcoe)
1828 return NOTIFY_OK;
1829
619fe4be
RL
1830 ctlr = fcoe_to_ctlr(fcoe);
1831
6f6c2aa3 1832 if (entry->dcbx & DCB_CAP_DCBX_VER_CEE)
1833 prio = ffs(entry->app.priority) - 1;
1834 else
1835 prio = entry->app.priority;
1836
1837 if (prio < 0)
1838 return NOTIFY_OK;
1839
1840 if (entry->app.protocol == ETH_P_FIP ||
1841 entry->app.protocol == ETH_P_FCOE)
619fe4be 1842 ctlr->priority = prio;
6f6c2aa3 1843
31c37a6f
NP
1844 if (entry->app.protocol == ETH_P_FCOE)
1845 fcoe->priority = prio;
6f6c2aa3 1846
1847 return NOTIFY_OK;
1848}
1849
85b4aa49 1850/**
1875f27e
RL
1851 * fcoe_device_notification() - Handler for net device events
1852 * @notifier: The context of the notification
1853 * @event: The type of event
1854 * @ptr: The net device that the event was on
85b4aa49 1855 *
1875f27e 1856 * This function is called by the Ethernet driver in case of link change event.
85b4aa49
RL
1857 *
1858 * Returns: 0 for success
34f42a07 1859 */
85b4aa49
RL
1860static int fcoe_device_notification(struct notifier_block *notifier,
1861 ulong event, void *ptr)
1862{
435c8667 1863 struct fcoe_ctlr_device *cdev;
1875f27e 1864 struct fc_lport *lport = NULL;
351638e7 1865 struct net_device *netdev = netdev_notifier_info_to_dev(ptr);
619fe4be 1866 struct fcoe_ctlr *ctlr;
014f5c3f 1867 struct fcoe_interface *fcoe;
2e70e241 1868 struct fcoe_port *port;
1bd49b48 1869 struct fc_stats *stats;
97c8389d 1870 u32 link_possible = 1;
85b4aa49
RL
1871 u32 mfs;
1872 int rc = NOTIFY_OK;
1873
014f5c3f 1874 list_for_each_entry(fcoe, &fcoe_hostlist, list) {
25024989 1875 if (fcoe->netdev == netdev) {
619fe4be
RL
1876 ctlr = fcoe_to_ctlr(fcoe);
1877 lport = ctlr->lp;
85b4aa49
RL
1878 break;
1879 }
1880 }
1875f27e 1881 if (!lport) {
85b4aa49
RL
1882 rc = NOTIFY_DONE;
1883 goto out;
1884 }
1885
85b4aa49
RL
1886 switch (event) {
1887 case NETDEV_DOWN:
1888 case NETDEV_GOING_DOWN:
97c8389d 1889 link_possible = 0;
85b4aa49
RL
1890 break;
1891 case NETDEV_UP:
1892 case NETDEV_CHANGE:
85b4aa49
RL
1893 break;
1894 case NETDEV_CHANGEMTU:
7221d7e5
YZ
1895 if (netdev->features & NETIF_F_FCOE_MTU)
1896 break;
1d1b88dc
VD
1897 mfs = netdev->mtu - (sizeof(struct fcoe_hdr) +
1898 sizeof(struct fcoe_crc_eof));
85b4aa49 1899 if (mfs >= FC_MIN_MAX_FRAME)
1875f27e 1900 fc_set_mfs(lport, mfs);
85b4aa49
RL
1901 break;
1902 case NETDEV_REGISTER:
1903 break;
2e70e241
CL
1904 case NETDEV_UNREGISTER:
1905 list_del(&fcoe->list);
619fe4be 1906 port = lport_priv(ctlr->lp);
2ca32b48 1907 queue_work(fcoe_wq, &port->destroy_work);
2e70e241
CL
1908 goto out;
1909 break;
54a5b21d
YZ
1910 case NETDEV_FEAT_CHANGE:
1911 fcoe_netdev_features_change(lport, netdev);
1912 break;
85b4aa49 1913 default:
1d1b88dc 1914 FCOE_NETDEV_DBG(netdev, "Unknown event %ld "
d5488eb9 1915 "from netdev netlink\n", event);
85b4aa49 1916 }
5e4f8fe7
RL
1917
1918 fcoe_link_speed_update(lport);
1919
435c8667
RL
1920 cdev = fcoe_ctlr_to_ctlr_dev(ctlr);
1921
1922 if (link_possible && !fcoe_link_ok(lport)) {
1923 switch (cdev->enabled) {
1924 case FCOE_CTLR_DISABLED:
1925 pr_info("Link up while interface is disabled.\n");
1926 break;
1927 case FCOE_CTLR_ENABLED:
1928 case FCOE_CTLR_UNUSED:
1929 fcoe_ctlr_link_up(ctlr);
1930 };
1931 } else if (fcoe_ctlr_link_down(ctlr)) {
1932 switch (cdev->enabled) {
1933 case FCOE_CTLR_DISABLED:
1934 pr_info("Link down while interface is disabled.\n");
1935 break;
1936 case FCOE_CTLR_ENABLED:
1937 case FCOE_CTLR_UNUSED:
1938 stats = per_cpu_ptr(lport->stats, get_cpu());
1939 stats->LinkFailureCount++;
1940 put_cpu();
1941 fcoe_clean_pending_queue(lport);
1942 };
85b4aa49
RL
1943 }
1944out:
1945 return rc;
1946}
1947
55a66d3c
VD
1948/**
1949 * fcoe_disable() - Disables a FCoE interface
78a58246 1950 * @netdev : The net_device object the Ethernet interface to create on
55a66d3c 1951 *
78a58246 1952 * Called from fcoe transport.
55a66d3c
VD
1953 *
1954 * Returns: 0 for success
435c8667
RL
1955 *
1956 * Deprecated: use fcoe_ctlr_enabled()
55a66d3c 1957 */
78a58246 1958static int fcoe_disable(struct net_device *netdev)
55a66d3c 1959{
619fe4be 1960 struct fcoe_ctlr *ctlr;
55a66d3c 1961 struct fcoe_interface *fcoe;
55a66d3c
VD
1962 int rc = 0;
1963
1964 mutex_lock(&fcoe_config_mutex);
55a66d3c 1965
ee5df628 1966 rtnl_lock();
55a66d3c
VD
1967 fcoe = fcoe_hostlist_lookup_port(netdev);
1968 rtnl_unlock();
1969
9ee50e48 1970 if (fcoe) {
619fe4be
RL
1971 ctlr = fcoe_to_ctlr(fcoe);
1972 fcoe_ctlr_link_down(ctlr);
1973 fcoe_clean_pending_queue(ctlr->lp);
9ee50e48 1974 } else
55a66d3c
VD
1975 rc = -ENODEV;
1976
55a66d3c
VD
1977 mutex_unlock(&fcoe_config_mutex);
1978 return rc;
1979}
1980
1981/**
1982 * fcoe_enable() - Enables a FCoE interface
78a58246 1983 * @netdev : The net_device object the Ethernet interface to create on
55a66d3c 1984 *
78a58246 1985 * Called from fcoe transport.
55a66d3c
VD
1986 *
1987 * Returns: 0 for success
1988 */
78a58246 1989static int fcoe_enable(struct net_device *netdev)
55a66d3c 1990{
619fe4be 1991 struct fcoe_ctlr *ctlr;
55a66d3c 1992 struct fcoe_interface *fcoe;
55a66d3c
VD
1993 int rc = 0;
1994
1995 mutex_lock(&fcoe_config_mutex);
ee5df628 1996 rtnl_lock();
55a66d3c
VD
1997 fcoe = fcoe_hostlist_lookup_port(netdev);
1998 rtnl_unlock();
1999
619fe4be 2000 if (!fcoe) {
55a66d3c 2001 rc = -ENODEV;
619fe4be
RL
2002 goto out;
2003 }
55a66d3c 2004
619fe4be
RL
2005 ctlr = fcoe_to_ctlr(fcoe);
2006
2007 if (!fcoe_link_ok(ctlr->lp))
2008 fcoe_ctlr_link_up(ctlr);
2009
2010out:
55a66d3c
VD
2011 mutex_unlock(&fcoe_config_mutex);
2012 return rc;
2013}
2014
435c8667
RL
2015/**
2016 * fcoe_ctlr_enabled() - Enable or disable an FCoE Controller
2017 * @cdev: The FCoE Controller that is being enabled or disabled
2018 *
2019 * fcoe_sysfs will ensure that the state of 'enabled' has
2020 * changed, so no checking is necessary here. This routine simply
2021 * calls fcoe_enable or fcoe_disable, both of which are deprecated.
2022 * When those routines are removed the functionality can be merged
2023 * here.
2024 */
2025static int fcoe_ctlr_enabled(struct fcoe_ctlr_device *cdev)
2026{
2027 struct fcoe_ctlr *ctlr = fcoe_ctlr_device_priv(cdev);
2028 struct fc_lport *lport = ctlr->lp;
2029 struct net_device *netdev = fcoe_netdev(lport);
2030
2031 switch (cdev->enabled) {
2032 case FCOE_CTLR_ENABLED:
2033 return fcoe_enable(netdev);
2034 case FCOE_CTLR_DISABLED:
2035 return fcoe_disable(netdev);
2036 case FCOE_CTLR_UNUSED:
2037 default:
2038 return -ENOTSUPP;
2039 };
2040}
2041
a87dccc7
HR
2042/**
2043 * fcoe_ctlr_mode() - Switch FIP mode
2044 * @cdev: The FCoE Controller that is being modified
2045 *
2046 * When the FIP mode has been changed we need to update
2047 * the multicast addresses to ensure we get the correct
2048 * frames.
2049 */
2050static void fcoe_ctlr_mode(struct fcoe_ctlr_device *ctlr_dev)
2051{
2052 struct fcoe_ctlr *ctlr = fcoe_ctlr_device_priv(ctlr_dev);
2053 struct fcoe_interface *fcoe = fcoe_ctlr_priv(ctlr);
2054
2055 if (ctlr_dev->mode == FIP_CONN_TYPE_VN2VN &&
2056 ctlr->mode != FIP_MODE_VN2VN) {
2057 dev_mc_del(fcoe->netdev, FIP_ALL_ENODE_MACS);
2058 dev_mc_add(fcoe->netdev, FIP_ALL_VN2VN_MACS);
2059 dev_mc_add(fcoe->netdev, FIP_ALL_P2P_MACS);
2060 } else if (ctlr->mode != FIP_MODE_FABRIC) {
2061 dev_mc_del(fcoe->netdev, FIP_ALL_VN2VN_MACS);
2062 dev_mc_del(fcoe->netdev, FIP_ALL_P2P_MACS);
2063 dev_mc_add(fcoe->netdev, FIP_ALL_ENODE_MACS);
2064 }
2065 fcoe_ctlr_set_fip_mode(ctlr_dev);
2066}
2067
85b4aa49 2068/**
1875f27e 2069 * fcoe_destroy() - Destroy a FCoE interface
78a58246 2070 * @netdev : The net_device object the Ethernet interface to create on
1875f27e 2071 *
78a58246 2072 * Called from fcoe transport
85b4aa49
RL
2073 *
2074 * Returns: 0 for success
34f42a07 2075 */
78a58246 2076static int fcoe_destroy(struct net_device *netdev)
85b4aa49 2077{
619fe4be 2078 struct fcoe_ctlr *ctlr;
030f4e00 2079 struct fcoe_interface *fcoe;
f04ca1b6 2080 struct fc_lport *lport;
b2085a4e 2081 struct fcoe_port *port;
8eca355f 2082 int rc = 0;
85b4aa49 2083
dfc1d0fe 2084 mutex_lock(&fcoe_config_mutex);
ee5df628 2085 rtnl_lock();
2e70e241
CL
2086 fcoe = fcoe_hostlist_lookup_port(netdev);
2087 if (!fcoe) {
85b4aa49 2088 rc = -ENODEV;
78a58246 2089 goto out_nodev;
85b4aa49 2090 }
619fe4be
RL
2091 ctlr = fcoe_to_ctlr(fcoe);
2092 lport = ctlr->lp;
b2085a4e 2093 port = lport_priv(lport);
54a5b21d 2094 list_del(&fcoe->list);
b2085a4e 2095 queue_work(fcoe_wq, &port->destroy_work);
85b4aa49 2096out_nodev:
b2085a4e 2097 rtnl_unlock();
dfc1d0fe 2098 mutex_unlock(&fcoe_config_mutex);
85b4aa49
RL
2099 return rc;
2100}
2101
1875f27e
RL
2102/**
2103 * fcoe_destroy_work() - Destroy a FCoE port in a deferred work context
2104 * @work: Handle to the FCoE port to be destroyed
2105 */
2e70e241
CL
2106static void fcoe_destroy_work(struct work_struct *work)
2107{
f9c4358e
RL
2108 struct fcoe_ctlr_device *cdev;
2109 struct fcoe_ctlr *ctlr;
2e70e241 2110 struct fcoe_port *port;
b2085a4e 2111 struct fcoe_interface *fcoe;
94aa743a
NP
2112 struct Scsi_Host *shost;
2113 struct fc_host_attrs *fc_host;
2114 unsigned long flags;
2115 struct fc_vport *vport;
2116 struct fc_vport *next_vport;
2e70e241
CL
2117
2118 port = container_of(work, struct fcoe_port, destroy_work);
94aa743a
NP
2119 shost = port->lport->host;
2120 fc_host = shost_to_fc_host(shost);
2121
2122 /* Loop through all the vports and mark them for deletion */
2123 spin_lock_irqsave(shost->host_lock, flags);
2124 list_for_each_entry_safe(vport, next_vport, &fc_host->vports, peers) {
2125 if (vport->flags & (FC_VPORT_DEL | FC_VPORT_CREATING)) {
2126 continue;
2127 } else {
2128 vport->flags |= FC_VPORT_DELETING;
2129 queue_work(fc_host_work_q(shost),
2130 &vport->vport_delete_work);
2131 }
2132 }
2133 spin_unlock_irqrestore(shost->host_lock, flags);
2134
2135 flush_workqueue(fc_host_work_q(shost));
2136
2e70e241 2137 mutex_lock(&fcoe_config_mutex);
b2085a4e 2138
b2085a4e 2139 fcoe = port->priv;
f9c4358e
RL
2140 ctlr = fcoe_to_ctlr(fcoe);
2141 cdev = fcoe_ctlr_to_ctlr_dev(ctlr);
2142
2e70e241 2143 fcoe_if_destroy(port->lport);
ccefd23e 2144 fcoe_interface_cleanup(fcoe);
b2085a4e 2145
2e70e241 2146 mutex_unlock(&fcoe_config_mutex);
f9c4358e
RL
2147
2148 fcoe_ctlr_device_delete(cdev);
2e70e241
CL
2149}
2150
78a58246
YZ
2151/**
2152 * fcoe_match() - Check if the FCoE is supported on the given netdevice
2153 * @netdev : The net_device object the Ethernet interface to create on
2154 *
2155 * Called from fcoe transport.
2156 *
2157 * Returns: always returns true as this is the default FCoE transport,
2158 * i.e., support all netdevs.
2159 */
2160static bool fcoe_match(struct net_device *netdev)
2161{
2162 return true;
2163}
2164
6f6c2aa3 2165/**
2166 * fcoe_dcb_create() - Initialize DCB attributes and hooks
2167 * @netdev: The net_device object of the L2 link that should be queried
2168 * @port: The fcoe_port to bind FCoE APP priority with
2169 * @
2170 */
2171static void fcoe_dcb_create(struct fcoe_interface *fcoe)
2172{
c216e876
HR
2173 int ctlr_prio = TC_PRIO_BESTEFFORT;
2174 int fcoe_prio = TC_PRIO_INTERACTIVE;
c5969656 2175 struct fcoe_ctlr *ctlr = fcoe_to_ctlr(fcoe);
6f6c2aa3 2176#ifdef CONFIG_DCB
2177 int dcbx;
2178 u8 fup, up;
2179 struct net_device *netdev = fcoe->realdev;
6f6c2aa3 2180 struct dcb_app app = {
2181 .priority = 0,
2182 .protocol = ETH_P_FCOE
2183 };
2184
2185 /* setup DCB priority attributes. */
2186 if (netdev && netdev->dcbnl_ops && netdev->dcbnl_ops->getdcbx) {
2187 dcbx = netdev->dcbnl_ops->getdcbx(netdev);
2188
2189 if (dcbx & DCB_CAP_DCBX_VER_IEEE) {
2190 app.selector = IEEE_8021QAZ_APP_SEL_ETHERTYPE;
2191 up = dcb_ieee_getapp_mask(netdev, &app);
2192 app.protocol = ETH_P_FIP;
2193 fup = dcb_ieee_getapp_mask(netdev, &app);
2194 } else {
2195 app.selector = DCB_APP_IDTYPE_ETHTYPE;
2196 up = dcb_getapp(netdev, &app);
2197 app.protocol = ETH_P_FIP;
2198 fup = dcb_getapp(netdev, &app);
2199 }
2200
c216e876
HR
2201 fcoe_prio = ffs(up) ? ffs(up) - 1 : 0;
2202 ctlr_prio = ffs(fup) ? ffs(fup) - 1 : fcoe_prio;
6f6c2aa3 2203 }
2204#endif
c216e876
HR
2205 fcoe->priority = fcoe_prio;
2206 ctlr->priority = ctlr_prio;
6f6c2aa3 2207}
2208
435c8667
RL
2209enum fcoe_create_link_state {
2210 FCOE_CREATE_LINK_DOWN,
2211 FCOE_CREATE_LINK_UP,
2212};
2213
85b4aa49 2214/**
435c8667
RL
2215 * _fcoe_create() - (internal) Create a fcoe interface
2216 * @netdev : The net_device object the Ethernet interface to create on
2217 * @fip_mode: The FIP mode for this creation
2218 * @link_state: The ctlr link state on creation
1875f27e 2219 *
435c8667
RL
2220 * Called from either the libfcoe 'create' module parameter
2221 * via fcoe_create or from fcoe_syfs's ctlr_create file.
85b4aa49 2222 *
435c8667
RL
2223 * libfcoe's 'create' module parameter is deprecated so some
2224 * consolidation of code can be done when that interface is
2225 * removed.
34f42a07 2226 */
1917d42d 2227static int _fcoe_create(struct net_device *netdev, enum fip_mode fip_mode,
435c8667 2228 enum fcoe_create_link_state link_state)
85b4aa49 2229{
b2085a4e 2230 int rc = 0;
8d55e507 2231 struct fcoe_ctlr_device *ctlr_dev;
619fe4be 2232 struct fcoe_ctlr *ctlr;
030f4e00 2233 struct fcoe_interface *fcoe;
af7f85d9 2234 struct fc_lport *lport;
85b4aa49 2235
dfc1d0fe 2236 mutex_lock(&fcoe_config_mutex);
ee5df628 2237 rtnl_lock();
34ce27bc 2238
85b4aa49
RL
2239 /* look for existing lport */
2240 if (fcoe_hostlist_lookup(netdev)) {
2241 rc = -EEXIST;
78a58246 2242 goto out_nodev;
85b4aa49 2243 }
85b4aa49 2244
1dd454d9 2245 fcoe = fcoe_interface_create(netdev, fip_mode);
7287fb91
RL
2246 if (IS_ERR(fcoe)) {
2247 rc = PTR_ERR(fcoe);
78a58246 2248 goto out_nodev;
030f4e00
CL
2249 }
2250
619fe4be 2251 ctlr = fcoe_to_ctlr(fcoe);
8d55e507
RL
2252 ctlr_dev = fcoe_ctlr_to_ctlr_dev(ctlr);
2253 lport = fcoe_if_create(fcoe, &ctlr_dev->dev, 0);
af7f85d9 2254 if (IS_ERR(lport)) {
d5488eb9 2255 printk(KERN_ERR "fcoe: Failed to create interface (%s)\n",
85b4aa49 2256 netdev->name);
85b4aa49 2257 rc = -EIO;
848e7d5b 2258 rtnl_unlock();
2e70e241 2259 fcoe_interface_cleanup(fcoe);
f9c4358e
RL
2260 mutex_unlock(&fcoe_config_mutex);
2261 fcoe_ctlr_device_delete(ctlr_dev);
2262 goto out;
85b4aa49 2263 }
030f4e00 2264
54b649f8 2265 /* Make this the "master" N_Port */
619fe4be 2266 ctlr->lp = lport;
54b649f8 2267
6f6c2aa3 2268 /* setup DCB priority attributes. */
2269 fcoe_dcb_create(fcoe);
2270
54b649f8
CL
2271 /* start FIP Discovery and FLOGI */
2272 lport->boot_time = jiffies;
2273 fc_fabric_login(lport);
435c8667
RL
2274
2275 /*
2276 * If the fcoe_ctlr_device is to be set to DISABLED
2277 * it must be done after the lport is added to the
2278 * hostlist, but before the rtnl_lock is released.
2279 * This is because the rtnl_lock protects the
2280 * hostlist that fcoe_device_notification uses. If
2281 * the FCoE Controller is intended to be created
2282 * DISABLED then 'enabled' needs to be considered
2283 * handling link events. 'enabled' must be set
2284 * before the lport can be found in the hostlist
2285 * when a link up event is received.
2286 */
2287 if (link_state == FCOE_CREATE_LINK_UP)
2288 ctlr_dev->enabled = FCOE_CTLR_ENABLED;
2289 else
2290 ctlr_dev->enabled = FCOE_CTLR_DISABLED;
2291
2292 if (link_state == FCOE_CREATE_LINK_UP &&
2293 !fcoe_link_ok(lport)) {
22805123 2294 rtnl_unlock();
619fe4be 2295 fcoe_ctlr_link_up(ctlr);
22805123
RL
2296 mutex_unlock(&fcoe_config_mutex);
2297 return rc;
2298 }
030f4e00 2299
85b4aa49 2300out_nodev:
34ce27bc 2301 rtnl_unlock();
dfc1d0fe 2302 mutex_unlock(&fcoe_config_mutex);
f9c4358e 2303out:
85b4aa49
RL
2304 return rc;
2305}
2306
435c8667
RL
2307/**
2308 * fcoe_create() - Create a fcoe interface
2309 * @netdev : The net_device object the Ethernet interface to create on
2310 * @fip_mode: The FIP mode for this creation
2311 *
2312 * Called from fcoe transport
2313 *
2314 * Returns: 0 for success
2315 */
1917d42d 2316static int fcoe_create(struct net_device *netdev, enum fip_mode fip_mode)
435c8667
RL
2317{
2318 return _fcoe_create(netdev, fip_mode, FCOE_CREATE_LINK_UP);
2319}
2320
2321/**
2322 * fcoe_ctlr_alloc() - Allocate a fcoe interface from fcoe_sysfs
2323 * @netdev: The net_device to be used by the allocated FCoE Controller
2324 *
2325 * This routine is called from fcoe_sysfs. It will start the fcoe_ctlr
2326 * in a link_down state. The allows the user an opportunity to configure
2327 * the FCoE Controller from sysfs before enabling the FCoE Controller.
2328 *
2329 * Creating in with this routine starts the FCoE Controller in Fabric
2330 * mode. The user can change to VN2VN or another mode before enabling.
2331 */
2332static int fcoe_ctlr_alloc(struct net_device *netdev)
2333{
2334 return _fcoe_create(netdev, FIP_MODE_FABRIC,
2335 FCOE_CREATE_LINK_DOWN);
2336}
2337
5e4f8fe7
RL
2338/**
2339 * fcoe_link_ok() - Check if the link is OK for a local port
2340 * @lport: The local port to check link on
2341 *
2342 * Returns: 0 if link is UP and OK, -1 if not
2343 *
2344 */
7c9c6841 2345static int fcoe_link_ok(struct fc_lport *lport)
5e4f8fe7 2346{
8597ae8b 2347 struct net_device *netdev = fcoe_netdev(lport);
5e4f8fe7
RL
2348
2349 if (netif_oper_up(netdev))
2350 return 0;
2351 return -1;
2352}
2353
34f42a07 2354/**
1875f27e
RL
2355 * fcoe_percpu_clean() - Clear all pending skbs for an local port
2356 * @lport: The local port whose skbs are to be cleared
e7a51997
JE
2357 *
2358 * Must be called with fcoe_create_mutex held to single-thread completion.
2359 *
4b9bc86d
SAS
2360 * This flushes the pending skbs by flush the work item for each CPU. The work
2361 * item on each possible CPU is flushed because we may have used the per-CPU
2362 * struct of an offline CPU.
85b4aa49 2363 */
7c9c6841 2364static void fcoe_percpu_clean(struct fc_lport *lport)
85b4aa49 2365{
85b4aa49 2366 struct fcoe_percpu_s *pp;
5e5e92df 2367 unsigned int cpu;
85b4aa49 2368
5e5e92df
RL
2369 for_each_possible_cpu(cpu) {
2370 pp = &per_cpu(fcoe_percpu, cpu);
e7a51997 2371
4b9bc86d 2372 flush_work(&pp->work);
85b4aa49
RL
2373 }
2374}
85b4aa49 2375
34f42a07 2376/**
1875f27e
RL
2377 * fcoe_reset() - Reset a local port
2378 * @shost: The SCSI host associated with the local port to be reset
85b4aa49 2379 *
1875f27e 2380 * Returns: Always 0 (return value required by FC transport template)
85b4aa49 2381 */
7c9c6841 2382static int fcoe_reset(struct Scsi_Host *shost)
85b4aa49
RL
2383{
2384 struct fc_lport *lport = shost_priv(shost);
d2f80952
VD
2385 struct fcoe_port *port = lport_priv(lport);
2386 struct fcoe_interface *fcoe = port->priv;
619fe4be 2387 struct fcoe_ctlr *ctlr = fcoe_to_ctlr(fcoe);
435c8667 2388 struct fcoe_ctlr_device *cdev = fcoe_ctlr_to_ctlr_dev(ctlr);
d2f80952 2389
619fe4be
RL
2390 fcoe_ctlr_link_down(ctlr);
2391 fcoe_clean_pending_queue(ctlr->lp);
435c8667
RL
2392
2393 if (cdev->enabled != FCOE_CTLR_DISABLED &&
2394 !fcoe_link_ok(ctlr->lp))
619fe4be 2395 fcoe_ctlr_link_up(ctlr);
85b4aa49
RL
2396 return 0;
2397}
85b4aa49 2398
34f42a07 2399/**
1875f27e
RL
2400 * fcoe_hostlist_lookup_port() - Find the FCoE interface associated with a net device
2401 * @netdev: The net device used as a key
85b4aa49 2402 *
1875f27e
RL
2403 * Locking: Must be called with the RNL mutex held.
2404 *
2405 * Returns: NULL or the FCoE interface
85b4aa49 2406 */
014f5c3f 2407static struct fcoe_interface *
1875f27e 2408fcoe_hostlist_lookup_port(const struct net_device *netdev)
85b4aa49 2409{
014f5c3f 2410 struct fcoe_interface *fcoe;
85b4aa49 2411
014f5c3f 2412 list_for_each_entry(fcoe, &fcoe_hostlist, list) {
1875f27e 2413 if (fcoe->netdev == netdev)
014f5c3f 2414 return fcoe;
85b4aa49 2415 }
85b4aa49
RL
2416 return NULL;
2417}
2418
34f42a07 2419/**
1875f27e
RL
2420 * fcoe_hostlist_lookup() - Find the local port associated with a
2421 * given net device
2422 * @netdev: The netdevice used as a key
85b4aa49 2423 *
1875f27e
RL
2424 * Locking: Must be called with the RTNL mutex held
2425 *
2426 * Returns: NULL or the local port
85b4aa49 2427 */
090eb6c4 2428static struct fc_lport *fcoe_hostlist_lookup(const struct net_device *netdev)
85b4aa49 2429{
619fe4be 2430 struct fcoe_ctlr *ctlr;
014f5c3f 2431 struct fcoe_interface *fcoe;
85b4aa49 2432
014f5c3f 2433 fcoe = fcoe_hostlist_lookup_port(netdev);
619fe4be
RL
2434 ctlr = fcoe_to_ctlr(fcoe);
2435 return (fcoe) ? ctlr->lp : NULL;
85b4aa49 2436}
85b4aa49 2437
34f42a07 2438/**
1875f27e
RL
2439 * fcoe_hostlist_add() - Add the FCoE interface identified by a local
2440 * port to the hostlist
2441 * @lport: The local port that identifies the FCoE interface to be added
85b4aa49 2442 *
090eb6c4 2443 * Locking: must be called with the RTNL mutex held
1875f27e
RL
2444 *
2445 * Returns: 0 for success
85b4aa49 2446 */
090eb6c4 2447static int fcoe_hostlist_add(const struct fc_lport *lport)
85b4aa49 2448{
014f5c3f
CL
2449 struct fcoe_interface *fcoe;
2450 struct fcoe_port *port;
2451
2452 fcoe = fcoe_hostlist_lookup_port(fcoe_netdev(lport));
2453 if (!fcoe) {
2454 port = lport_priv(lport);
8597ae8b 2455 fcoe = port->priv;
014f5c3f 2456 list_add_tail(&fcoe->list, &fcoe_hostlist);
85b4aa49
RL
2457 }
2458 return 0;
2459}
85b4aa49 2460
f9184df3
NH
2461/**
2462 * fcoe_hostlist_del() - Remove the FCoE interface identified by a local
2463 * port to the hostlist
2464 * @lport: The local port that identifies the FCoE interface to be added
2465 *
2466 * Locking: must be called with the RTNL mutex held
2467 *
2468 */
2469static void fcoe_hostlist_del(const struct fc_lport *lport)
2470{
2471 struct fcoe_interface *fcoe;
2472 struct fcoe_port *port;
2473
2474 port = lport_priv(lport);
2475 fcoe = port->priv;
2476 list_del(&fcoe->list);
2477 return;
2478}
78a58246
YZ
2479
2480static struct fcoe_transport fcoe_sw_transport = {
2481 .name = {FCOE_TRANSPORT_DEFAULT},
2482 .attached = false,
2483 .list = LIST_HEAD_INIT(fcoe_sw_transport.list),
2484 .match = fcoe_match,
435c8667 2485 .alloc = fcoe_ctlr_alloc,
78a58246
YZ
2486 .create = fcoe_create,
2487 .destroy = fcoe_destroy,
2488 .enable = fcoe_enable,
2489 .disable = fcoe_disable,
2490};
2491
85b4aa49 2492/**
1875f27e 2493 * fcoe_init() - Initialize fcoe.ko
85b4aa49 2494 *
1875f27e 2495 * Returns: 0 on success, or a negative value on failure
34f42a07 2496 */
85b4aa49
RL
2497static int __init fcoe_init(void)
2498{
1875f27e 2499 struct fcoe_percpu_s *p;
38eccabd 2500 unsigned int cpu;
8976f424 2501 int rc = 0;
85b4aa49 2502
2ca32b48
TH
2503 fcoe_wq = alloc_workqueue("fcoe", 0, 0);
2504 if (!fcoe_wq)
2505 return -ENOMEM;
2506
78a58246
YZ
2507 /* register as a fcoe transport */
2508 rc = fcoe_transport_attach(&fcoe_sw_transport);
2509 if (rc) {
2510 printk(KERN_ERR "failed to register an fcoe transport, check "
2511 "if libfcoe is loaded\n");
a561a8ea 2512 goto out_destroy;
78a58246
YZ
2513 }
2514
dfc1d0fe
CL
2515 mutex_lock(&fcoe_config_mutex);
2516
38eccabd 2517 for_each_possible_cpu(cpu) {
4b9bc86d
SAS
2518 p = per_cpu_ptr(&fcoe_percpu, cpu);
2519 INIT_WORK(&p->work, fcoe_receive_work);
38eccabd
RL
2520 skb_queue_head_init(&p->fcoe_rx_list);
2521 }
2522
8976f424 2523 /* Setup link change notification */
85b4aa49
RL
2524 fcoe_dev_setup();
2525
5892c32f
CL
2526 rc = fcoe_if_init();
2527 if (rc)
2528 goto out_free;
85b4aa49 2529
dfc1d0fe 2530 mutex_unlock(&fcoe_config_mutex);
85b4aa49 2531 return 0;
8976f424
RL
2532
2533out_free:
dfc1d0fe 2534 mutex_unlock(&fcoe_config_mutex);
a561a8ea 2535out_destroy:
2ca32b48 2536 destroy_workqueue(fcoe_wq);
8976f424 2537 return rc;
85b4aa49
RL
2538}
2539module_init(fcoe_init);
2540
2541/**
1875f27e 2542 * fcoe_exit() - Clean up fcoe.ko
85b4aa49 2543 *
1875f27e 2544 * Returns: 0 on success or a negative value on failure
34f42a07 2545 */
85b4aa49
RL
2546static void __exit fcoe_exit(void)
2547{
014f5c3f 2548 struct fcoe_interface *fcoe, *tmp;
619fe4be 2549 struct fcoe_ctlr *ctlr;
2e70e241 2550 struct fcoe_port *port;
1875f27e 2551 unsigned int cpu;
85b4aa49 2552
dfc1d0fe
CL
2553 mutex_lock(&fcoe_config_mutex);
2554
85b4aa49
RL
2555 fcoe_dev_cleanup();
2556
5919a595 2557 /* releases the associated fcoe hosts */
090eb6c4
CL
2558 rtnl_lock();
2559 list_for_each_entry_safe(fcoe, tmp, &fcoe_hostlist, list) {
619fe4be
RL
2560 ctlr = fcoe_to_ctlr(fcoe);
2561 port = lport_priv(ctlr->lp);
f9184df3 2562 fcoe_hostlist_del(port->lport);
2ca32b48 2563 queue_work(fcoe_wq, &port->destroy_work);
c863df33 2564 }
090eb6c4 2565 rtnl_unlock();
85b4aa49 2566
4b9bc86d
SAS
2567 for_each_possible_cpu(cpu)
2568 fcoe_thread_cleanup_local(cpu);
cd45ae38 2569
dfc1d0fe 2570 mutex_unlock(&fcoe_config_mutex);
2e70e241 2571
2ca32b48
TH
2572 /*
2573 * destroy_work's may be chained but destroy_workqueue()
2574 * can take care of them. Just kill the fcoe_wq.
2575 */
2576 destroy_workqueue(fcoe_wq);
2e70e241 2577
2ca32b48
TH
2578 /*
2579 * Detaching from the scsi transport must happen after all
2580 * destroys are done on the fcoe_wq. destroy_workqueue will
2581 * enusre the fcoe_wq is flushed.
2582 */
2e70e241 2583 fcoe_if_exit();
78a58246
YZ
2584
2585 /* detach from fcoe transport */
2586 fcoe_transport_detach(&fcoe_sw_transport);
85b4aa49
RL
2587}
2588module_exit(fcoe_exit);
11b56188
CL
2589
2590/**
2591 * fcoe_flogi_resp() - FCoE specific FLOGI and FDISC response handler
2592 * @seq: active sequence in the FLOGI or FDISC exchange
2593 * @fp: response frame, or error encoded in a pointer (timeout)
2594 * @arg: pointer the the fcoe_ctlr structure
2595 *
65155b37 2596 * This handles MAC address management for FCoE, then passes control on to
11b56188
CL
2597 * the libfc FLOGI response handler.
2598 */
2599static void fcoe_flogi_resp(struct fc_seq *seq, struct fc_frame *fp, void *arg)
2600{
2601 struct fcoe_ctlr *fip = arg;
2602 struct fc_exch *exch = fc_seq_exch(seq);
2603 struct fc_lport *lport = exch->lp;
2604 u8 *mac;
2605
2606 if (IS_ERR(fp))
2607 goto done;
2608
2609 mac = fr_cb(fp)->granted_mac;
907c07d4
VD
2610 /* pre-FIP */
2611 if (is_zero_ether_addr(mac))
2612 fcoe_ctlr_recv_flogi(fip, lport, fp);
2613 if (!is_zero_ether_addr(mac))
2614 fcoe_update_src_mac(lport, mac);
11b56188
CL
2615done:
2616 fc_lport_flogi_resp(seq, fp, lport);
2617}
2618
2619/**
2620 * fcoe_logo_resp() - FCoE specific LOGO response handler
2621 * @seq: active sequence in the LOGO exchange
2622 * @fp: response frame, or error encoded in a pointer (timeout)
2623 * @arg: pointer the the fcoe_ctlr structure
2624 *
65155b37 2625 * This handles MAC address management for FCoE, then passes control on to
11b56188
CL
2626 * the libfc LOGO response handler.
2627 */
2628static void fcoe_logo_resp(struct fc_seq *seq, struct fc_frame *fp, void *arg)
2629{
386309ce 2630 struct fc_lport *lport = arg;
11b56188
CL
2631 static u8 zero_mac[ETH_ALEN] = { 0 };
2632
2633 if (!IS_ERR(fp))
386309ce 2634 fcoe_update_src_mac(lport, zero_mac);
11b56188
CL
2635 fc_lport_logo_resp(seq, fp, lport);
2636}
2637
2638/**
2639 * fcoe_elsct_send - FCoE specific ELS handler
2640 *
2641 * This does special case handling of FIP encapsualted ELS exchanges for FCoE,
2642 * using FCoE specific response handlers and passing the FIP controller as
2643 * the argument (the lport is still available from the exchange).
2644 *
2645 * Most of the work here is just handed off to the libfc routine.
2646 */
1875f27e
RL
2647static struct fc_seq *fcoe_elsct_send(struct fc_lport *lport, u32 did,
2648 struct fc_frame *fp, unsigned int op,
2649 void (*resp)(struct fc_seq *,
2650 struct fc_frame *,
2651 void *),
2652 void *arg, u32 timeout)
11b56188
CL
2653{
2654 struct fcoe_port *port = lport_priv(lport);
8597ae8b 2655 struct fcoe_interface *fcoe = port->priv;
619fe4be 2656 struct fcoe_ctlr *fip = fcoe_to_ctlr(fcoe);
11b56188
CL
2657 struct fc_frame_header *fh = fc_frame_header_get(fp);
2658
2659 switch (op) {
2660 case ELS_FLOGI:
2661 case ELS_FDISC:
e10f8c66
JE
2662 if (lport->point_to_multipoint)
2663 break;
11b56188
CL
2664 return fc_elsct_send(lport, did, fp, op, fcoe_flogi_resp,
2665 fip, timeout);
2666 case ELS_LOGO:
2667 /* only hook onto fabric logouts, not port logouts */
2668 if (ntoh24(fh->fh_d_id) != FC_FID_FLOGI)
2669 break;
2670 return fc_elsct_send(lport, did, fp, op, fcoe_logo_resp,
386309ce 2671 lport, timeout);
11b56188
CL
2672 }
2673 return fc_elsct_send(lport, did, fp, op, resp, arg, timeout);
2674}
2675
9a05753b
CL
2676/**
2677 * fcoe_vport_create() - create an fc_host/scsi_host for a vport
2678 * @vport: fc_vport object to create a new fc_host for
2679 * @disabled: start the new fc_host in a disabled state by default?
2680 *
2681 * Returns: 0 for success
2682 */
2683static int fcoe_vport_create(struct fc_vport *vport, bool disabled)
2684{
2685 struct Scsi_Host *shost = vport_to_shost(vport);
2686 struct fc_lport *n_port = shost_priv(shost);
2687 struct fcoe_port *port = lport_priv(n_port);
8597ae8b 2688 struct fcoe_interface *fcoe = port->priv;
9a05753b
CL
2689 struct net_device *netdev = fcoe->netdev;
2690 struct fc_lport *vn_port;
bdf25218
NP
2691 int rc;
2692 char buf[32];
2693
2694 rc = fcoe_validate_vport_create(vport);
2695 if (rc) {
d834895c 2696 fcoe_wwn_to_str(vport->port_name, buf, sizeof(buf));
bdf25218
NP
2697 printk(KERN_ERR "fcoe: Failed to create vport, "
2698 "WWPN (0x%s) already exists\n",
2699 buf);
2700 return rc;
2701 }
9a05753b
CL
2702
2703 mutex_lock(&fcoe_config_mutex);
4bc71cb9 2704 rtnl_lock();
9a05753b 2705 vn_port = fcoe_if_create(fcoe, &vport->dev, 1);
4bc71cb9 2706 rtnl_unlock();
9a05753b
CL
2707 mutex_unlock(&fcoe_config_mutex);
2708
2709 if (IS_ERR(vn_port)) {
2710 printk(KERN_ERR "fcoe: fcoe_vport_create(%s) failed\n",
2711 netdev->name);
2712 return -EIO;
2713 }
2714
2715 if (disabled) {
2716 fc_vport_set_state(vport, FC_VPORT_DISABLED);
2717 } else {
2718 vn_port->boot_time = jiffies;
2719 fc_fabric_login(vn_port);
2720 fc_vport_setlink(vn_port);
2721 }
2722 return 0;
2723}
2724
2725/**
2726 * fcoe_vport_destroy() - destroy the fc_host/scsi_host for a vport
2727 * @vport: fc_vport object that is being destroyed
2728 *
2729 * Returns: 0 for success
2730 */
2731static int fcoe_vport_destroy(struct fc_vport *vport)
2732{
2733 struct Scsi_Host *shost = vport_to_shost(vport);
2734 struct fc_lport *n_port = shost_priv(shost);
2735 struct fc_lport *vn_port = vport->dd_data;
9a05753b
CL
2736
2737 mutex_lock(&n_port->lp_mutex);
2738 list_del(&vn_port->list);
2739 mutex_unlock(&n_port->lp_mutex);
ccefd23e
RL
2740
2741 mutex_lock(&fcoe_config_mutex);
2742 fcoe_if_destroy(vn_port);
2743 mutex_unlock(&fcoe_config_mutex);
2744
9a05753b
CL
2745 return 0;
2746}
2747
2748/**
2749 * fcoe_vport_disable() - change vport state
2750 * @vport: vport to bring online/offline
2751 * @disable: should the vport be disabled?
2752 */
2753static int fcoe_vport_disable(struct fc_vport *vport, bool disable)
2754{
2755 struct fc_lport *lport = vport->dd_data;
2756
2757 if (disable) {
2758 fc_vport_set_state(vport, FC_VPORT_DISABLED);
2759 fc_fabric_logoff(lport);
2760 } else {
2761 lport->boot_time = jiffies;
2762 fc_fabric_login(lport);
2763 fc_vport_setlink(lport);
2764 }
2765
2766 return 0;
2767}
2768
dc8596d3
CL
2769/**
2770 * fcoe_vport_set_symbolic_name() - append vport string to symbolic name
2771 * @vport: fc_vport with a new symbolic name string
2772 *
2773 * After generating a new symbolic name string, a new RSPN_ID request is
2774 * sent to the name server. There is no response handler, so if it fails
2775 * for some reason it will not be retried.
2776 */
2777static void fcoe_set_vport_symbolic_name(struct fc_vport *vport)
2778{
2779 struct fc_lport *lport = vport->dd_data;
2780 struct fc_frame *fp;
2781 size_t len;
2782
2783 snprintf(fc_host_symbolic_name(lport->host), FC_SYMBOLIC_NAME_SIZE,
2784 "%s v%s over %s : %s", FCOE_NAME, FCOE_VERSION,
2785 fcoe_netdev(lport)->name, vport->symbolic_name);
2786
2787 if (lport->state != LPORT_ST_READY)
2788 return;
2789
2790 len = strnlen(fc_host_symbolic_name(lport->host), 255);
2791 fp = fc_frame_alloc(lport,
2792 sizeof(struct fc_ct_hdr) +
2793 sizeof(struct fc_ns_rspn) + len);
2794 if (!fp)
2795 return;
2796 lport->tt.elsct_send(lport, FC_FID_DIR_SERV, fp, FC_NS_RSPN_ID,
b94f8951 2797 NULL, NULL, 3 * lport->r_a_tov);
dc8596d3 2798}
b84056bf 2799
8d55e507
RL
2800static void fcoe_fcf_get_vlan_id(struct fcoe_fcf_device *fcf_dev)
2801{
2802 struct fcoe_ctlr_device *ctlr_dev =
2803 fcoe_fcf_dev_to_ctlr_dev(fcf_dev);
2804 struct fcoe_ctlr *ctlr = fcoe_ctlr_device_priv(ctlr_dev);
2805 struct fcoe_interface *fcoe = fcoe_ctlr_priv(ctlr);
2806
2807 fcf_dev->vlan_id = vlan_dev_vlan_id(fcoe->netdev);
2808}
2809
7d65b0df
JE
2810/**
2811 * fcoe_set_port_id() - Callback from libfc when Port_ID is set.
2812 * @lport: the local port
2813 * @port_id: the port ID
2814 * @fp: the received frame, if any, that caused the port_id to be set.
2815 *
2816 * This routine handles the case where we received a FLOGI and are
2817 * entering point-to-point mode. We need to call fcoe_ctlr_recv_flogi()
2818 * so it can set the non-mapped mode and gateway address.
2819 *
2820 * The FLOGI LS_ACC is handled by fcoe_flogi_resp().
2821 */
2822static void fcoe_set_port_id(struct fc_lport *lport,
2823 u32 port_id, struct fc_frame *fp)
2824{
2825 struct fcoe_port *port = lport_priv(lport);
8597ae8b 2826 struct fcoe_interface *fcoe = port->priv;
619fe4be 2827 struct fcoe_ctlr *ctlr = fcoe_to_ctlr(fcoe);
7d65b0df
JE
2828
2829 if (fp && fc_frame_payload_op(fp) == ELS_FLOGI)
619fe4be 2830 fcoe_ctlr_recv_flogi(ctlr, lport, fp);
7d65b0df 2831}