[SCSI] fcoe: vport symbolic name support
[linux-2.6-block.git] / drivers / scsi / fcoe / fcoe.c
CommitLineData
85b4aa49 1/*
af7f85d9 2 * Copyright(c) 2007 - 2009 Intel Corporation. All rights reserved.
85b4aa49
RL
3 *
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms and conditions of the GNU General Public License,
6 * version 2, as published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope it will be useful, but WITHOUT
9 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
10 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
11 * more details.
12 *
13 * You should have received a copy of the GNU General Public License along with
14 * this program; if not, write to the Free Software Foundation, Inc.,
15 * 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
16 *
17 * Maintained at www.Open-FCoE.org
18 */
19
20#include <linux/module.h>
21#include <linux/version.h>
85b4aa49 22#include <linux/spinlock.h>
85b4aa49
RL
23#include <linux/netdevice.h>
24#include <linux/etherdevice.h>
25#include <linux/ethtool.h>
26#include <linux/if_ether.h>
27#include <linux/if_vlan.h>
85b4aa49
RL
28#include <linux/crc32.h>
29#include <linux/cpu.h>
30#include <linux/fs.h>
31#include <linux/sysfs.h>
32#include <linux/ctype.h>
33#include <scsi/scsi_tcq.h>
34#include <scsi/scsicam.h>
35#include <scsi/scsi_transport.h>
36#include <scsi/scsi_transport_fc.h>
37#include <net/rtnetlink.h>
38
39#include <scsi/fc/fc_encaps.h>
97c8389d 40#include <scsi/fc/fc_fip.h>
85b4aa49
RL
41
42#include <scsi/libfc.h>
43#include <scsi/fc_frame.h>
44#include <scsi/libfcoe.h>
85b4aa49 45
fdd78027 46#include "fcoe.h"
7f349142 47
85b4aa49
RL
48MODULE_AUTHOR("Open-FCoE.org");
49MODULE_DESCRIPTION("FCoE");
9b34ecff 50MODULE_LICENSE("GPL v2");
85b4aa49 51
05cc7390
YZ
52/* Performance tuning parameters for fcoe */
53static unsigned int fcoe_ddp_min;
54module_param_named(ddp_min, fcoe_ddp_min, uint, S_IRUGO | S_IWUSR);
55MODULE_PARM_DESC(ddp_min, "Minimum I/O size in bytes for " \
56 "Direct Data Placement (DDP).");
57
dfc1d0fe
CL
58DEFINE_MUTEX(fcoe_config_mutex);
59
e7a51997
JE
60/* fcoe_percpu_clean completion. Waiter protected by fcoe_create_mutex */
61static DECLARE_COMPLETION(fcoe_flush_completion);
62
85b4aa49 63/* fcoe host list */
090eb6c4 64/* must only by accessed under the RTNL mutex */
85b4aa49 65LIST_HEAD(fcoe_hostlist);
5e5e92df 66DEFINE_PER_CPU(struct fcoe_percpu_s, fcoe_percpu);
85b4aa49 67
dd3fd72e 68/* Function Prototypes */
fdd78027
VD
69static int fcoe_reset(struct Scsi_Host *shost);
70static int fcoe_xmit(struct fc_lport *, struct fc_frame *);
71static int fcoe_rcv(struct sk_buff *, struct net_device *,
72 struct packet_type *, struct net_device *);
73static int fcoe_percpu_receive_thread(void *arg);
74static void fcoe_clean_pending_queue(struct fc_lport *lp);
75static void fcoe_percpu_clean(struct fc_lport *lp);
76static int fcoe_link_ok(struct fc_lport *lp);
77
78static struct fc_lport *fcoe_hostlist_lookup(const struct net_device *);
79static int fcoe_hostlist_add(const struct fc_lport *);
fdd78027 80
4bb6b515 81static void fcoe_check_wait_queue(struct fc_lport *, struct sk_buff *);
85b4aa49
RL
82static int fcoe_device_notification(struct notifier_block *, ulong, void *);
83static void fcoe_dev_setup(void);
84static void fcoe_dev_cleanup(void);
2e70e241
CL
85static struct fcoe_interface *
86 fcoe_hostlist_lookup_port(const struct net_device *dev);
85b4aa49
RL
87
88/* notification function from net device */
89static struct notifier_block fcoe_notifier = {
90 .notifier_call = fcoe_device_notification,
91};
92
e9084bb8
CL
93static struct scsi_transport_template *fcoe_transport_template;
94static struct scsi_transport_template *fcoe_vport_transport_template;
7f349142 95
9a05753b
CL
96static int fcoe_vport_destroy(struct fc_vport *vport);
97static int fcoe_vport_create(struct fc_vport *vport, bool disabled);
98static int fcoe_vport_disable(struct fc_vport *vport, bool disable);
dc8596d3 99static void fcoe_set_vport_symbolic_name(struct fc_vport *vport);
9a05753b 100
7f349142
VD
101struct fc_function_template fcoe_transport_function = {
102 .show_host_node_name = 1,
103 .show_host_port_name = 1,
104 .show_host_supported_classes = 1,
105 .show_host_supported_fc4s = 1,
106 .show_host_active_fc4s = 1,
107 .show_host_maxframe_size = 1,
108
109 .show_host_port_id = 1,
110 .show_host_supported_speeds = 1,
111 .get_host_speed = fc_get_host_speed,
112 .show_host_speed = 1,
113 .show_host_port_type = 1,
114 .get_host_port_state = fc_get_host_port_state,
115 .show_host_port_state = 1,
116 .show_host_symbolic_name = 1,
117
118 .dd_fcrport_size = sizeof(struct fc_rport_libfc_priv),
119 .show_rport_maxframe_size = 1,
120 .show_rport_supported_classes = 1,
121
122 .show_host_fabric_name = 1,
123 .show_starget_node_name = 1,
124 .show_starget_port_name = 1,
125 .show_starget_port_id = 1,
126 .set_rport_dev_loss_tmo = fc_set_rport_loss_tmo,
127 .show_rport_dev_loss_tmo = 1,
128 .get_fc_host_stats = fc_get_host_stats,
129 .issue_fc_host_lip = fcoe_reset,
130
131 .terminate_rport_io = fc_rport_terminate_io,
9a05753b
CL
132
133 .vport_create = fcoe_vport_create,
134 .vport_delete = fcoe_vport_destroy,
135 .vport_disable = fcoe_vport_disable,
dc8596d3 136 .set_vport_symbolic_name = fcoe_set_vport_symbolic_name,
7f349142
VD
137};
138
e9084bb8
CL
139struct fc_function_template fcoe_vport_transport_function = {
140 .show_host_node_name = 1,
141 .show_host_port_name = 1,
142 .show_host_supported_classes = 1,
143 .show_host_supported_fc4s = 1,
144 .show_host_active_fc4s = 1,
145 .show_host_maxframe_size = 1,
146
147 .show_host_port_id = 1,
148 .show_host_supported_speeds = 1,
149 .get_host_speed = fc_get_host_speed,
150 .show_host_speed = 1,
151 .show_host_port_type = 1,
152 .get_host_port_state = fc_get_host_port_state,
153 .show_host_port_state = 1,
154 .show_host_symbolic_name = 1,
155
156 .dd_fcrport_size = sizeof(struct fc_rport_libfc_priv),
157 .show_rport_maxframe_size = 1,
158 .show_rport_supported_classes = 1,
159
160 .show_host_fabric_name = 1,
161 .show_starget_node_name = 1,
162 .show_starget_port_name = 1,
163 .show_starget_port_id = 1,
164 .set_rport_dev_loss_tmo = fc_set_rport_loss_tmo,
165 .show_rport_dev_loss_tmo = 1,
166 .get_fc_host_stats = fc_get_host_stats,
167 .issue_fc_host_lip = fcoe_reset,
168
169 .terminate_rport_io = fc_rport_terminate_io,
170};
171
7f349142
VD
172static struct scsi_host_template fcoe_shost_template = {
173 .module = THIS_MODULE,
174 .name = "FCoE Driver",
175 .proc_name = FCOE_NAME,
176 .queuecommand = fc_queuecommand,
177 .eh_abort_handler = fc_eh_abort,
178 .eh_device_reset_handler = fc_eh_device_reset,
179 .eh_host_reset_handler = fc_eh_host_reset,
180 .slave_alloc = fc_slave_alloc,
181 .change_queue_depth = fc_change_queue_depth,
182 .change_queue_type = fc_change_queue_type,
183 .this_id = -1,
14caf44c 184 .cmd_per_lun = 3,
7f349142
VD
185 .can_queue = FCOE_MAX_OUTSTANDING_COMMANDS,
186 .use_clustering = ENABLE_CLUSTERING,
187 .sg_tablesize = SG_ALL,
188 .max_sectors = 0xffff,
189};
190
54b649f8
CL
191static int fcoe_fip_recv(struct sk_buff *skb, struct net_device *dev,
192 struct packet_type *ptype,
193 struct net_device *orig_dev);
194/**
195 * fcoe_interface_setup()
196 * @fcoe: new fcoe_interface
197 * @netdev : ptr to the associated netdevice struct
198 *
199 * Returns : 0 for success
2e70e241 200 * Locking: must be called with the RTNL mutex held
54b649f8
CL
201 */
202static int fcoe_interface_setup(struct fcoe_interface *fcoe,
203 struct net_device *netdev)
204{
205 struct fcoe_ctlr *fip = &fcoe->ctlr;
206 struct netdev_hw_addr *ha;
207 u8 flogi_maddr[ETH_ALEN];
b7a727f1 208 const struct net_device_ops *ops;
54b649f8
CL
209
210 fcoe->netdev = netdev;
211
b7a727f1
YZ
212 /* Let LLD initialize for FCoE */
213 ops = netdev->netdev_ops;
214 if (ops->ndo_fcoe_enable) {
215 if (ops->ndo_fcoe_enable(netdev))
216 FCOE_NETDEV_DBG(netdev, "Failed to enable FCoE"
217 " specific feature for LLD.\n");
218 }
219
54b649f8
CL
220 /* Do not support for bonding device */
221 if ((netdev->priv_flags & IFF_MASTER_ALB) ||
222 (netdev->priv_flags & IFF_SLAVE_INACTIVE) ||
223 (netdev->priv_flags & IFF_MASTER_8023AD)) {
224 return -EOPNOTSUPP;
225 }
226
227 /* look for SAN MAC address, if multiple SAN MACs exist, only
228 * use the first one for SPMA */
229 rcu_read_lock();
230 for_each_dev_addr(netdev, ha) {
231 if ((ha->type == NETDEV_HW_ADDR_T_SAN) &&
232 (is_valid_ether_addr(fip->ctl_src_addr))) {
233 memcpy(fip->ctl_src_addr, ha->addr, ETH_ALEN);
234 fip->spma = 1;
235 break;
236 }
237 }
238 rcu_read_unlock();
239
240 /* setup Source Mac Address */
241 if (!fip->spma)
242 memcpy(fip->ctl_src_addr, netdev->dev_addr, netdev->addr_len);
243
244 /*
245 * Add FCoE MAC address as second unicast MAC address
246 * or enter promiscuous mode if not capable of listening
247 * for multiple unicast MACs.
248 */
54b649f8
CL
249 memcpy(flogi_maddr, (u8[6]) FC_FCOE_FLOGI_MAC, ETH_ALEN);
250 dev_unicast_add(netdev, flogi_maddr);
251 if (fip->spma)
252 dev_unicast_add(netdev, fip->ctl_src_addr);
253 dev_mc_add(netdev, FIP_ALL_ENODE_MACS, ETH_ALEN, 0);
54b649f8
CL
254
255 /*
256 * setup the receive function from ethernet driver
257 * on the ethertype for the given device
258 */
259 fcoe->fcoe_packet_type.func = fcoe_rcv;
260 fcoe->fcoe_packet_type.type = __constant_htons(ETH_P_FCOE);
261 fcoe->fcoe_packet_type.dev = netdev;
262 dev_add_pack(&fcoe->fcoe_packet_type);
263
264 fcoe->fip_packet_type.func = fcoe_fip_recv;
265 fcoe->fip_packet_type.type = htons(ETH_P_FIP);
266 fcoe->fip_packet_type.dev = netdev;
267 dev_add_pack(&fcoe->fip_packet_type);
268
269 return 0;
270}
271
272static void fcoe_fip_send(struct fcoe_ctlr *fip, struct sk_buff *skb);
11b56188
CL
273static void fcoe_update_src_mac(struct fc_lport *lport, u8 *addr);
274static u8 *fcoe_get_src_mac(struct fc_lport *lport);
2e70e241 275static void fcoe_destroy_work(struct work_struct *work);
54b649f8 276
030f4e00
CL
277/**
278 * fcoe_interface_create()
279 * @netdev: network interface
280 *
281 * Returns: pointer to a struct fcoe_interface or NULL on error
282 */
283static struct fcoe_interface *fcoe_interface_create(struct net_device *netdev)
284{
285 struct fcoe_interface *fcoe;
286
287 fcoe = kzalloc(sizeof(*fcoe), GFP_KERNEL);
288 if (!fcoe) {
289 FCOE_NETDEV_DBG(netdev, "Could not allocate fcoe structure\n");
290 return NULL;
291 }
292
2e70e241 293 dev_hold(netdev);
030f4e00 294 kref_init(&fcoe->kref);
54b649f8
CL
295
296 /*
297 * Initialize FIP.
298 */
299 fcoe_ctlr_init(&fcoe->ctlr);
300 fcoe->ctlr.send = fcoe_fip_send;
301 fcoe->ctlr.update_mac = fcoe_update_src_mac;
11b56188 302 fcoe->ctlr.get_src_addr = fcoe_get_src_mac;
54b649f8
CL
303
304 fcoe_interface_setup(fcoe, netdev);
030f4e00
CL
305
306 return fcoe;
307}
308
54b649f8
CL
309/**
310 * fcoe_interface_cleanup() - clean up netdev configurations
311 * @fcoe:
2e70e241
CL
312 *
313 * Caller must be holding the RTNL mutex
54b649f8
CL
314 */
315void fcoe_interface_cleanup(struct fcoe_interface *fcoe)
316{
317 struct net_device *netdev = fcoe->netdev;
318 struct fcoe_ctlr *fip = &fcoe->ctlr;
319 u8 flogi_maddr[ETH_ALEN];
b7a727f1 320 const struct net_device_ops *ops;
54b649f8
CL
321
322 /*
323 * Don't listen for Ethernet packets anymore.
324 * synchronize_net() ensures that the packet handlers are not running
325 * on another CPU. dev_remove_pack() would do that, this calls the
326 * unsyncronized version __dev_remove_pack() to avoid multiple delays.
327 */
328 __dev_remove_pack(&fcoe->fcoe_packet_type);
329 __dev_remove_pack(&fcoe->fip_packet_type);
330 synchronize_net();
331
54b649f8 332 /* Delete secondary MAC addresses */
54b649f8
CL
333 memcpy(flogi_maddr, (u8[6]) FC_FCOE_FLOGI_MAC, ETH_ALEN);
334 dev_unicast_delete(netdev, flogi_maddr);
54b649f8
CL
335 if (fip->spma)
336 dev_unicast_delete(netdev, fip->ctl_src_addr);
337 dev_mc_delete(netdev, FIP_ALL_ENODE_MACS, ETH_ALEN, 0);
b7a727f1
YZ
338
339 /* Tell the LLD we are done w/ FCoE */
340 ops = netdev->netdev_ops;
341 if (ops->ndo_fcoe_disable) {
342 if (ops->ndo_fcoe_disable(netdev))
343 FCOE_NETDEV_DBG(netdev, "Failed to disable FCoE"
344 " specific feature for LLD.\n");
345 }
54b649f8
CL
346}
347
030f4e00
CL
348/**
349 * fcoe_interface_release() - fcoe_port kref release function
350 * @kref: embedded reference count in an fcoe_interface struct
351 */
352static void fcoe_interface_release(struct kref *kref)
353{
354 struct fcoe_interface *fcoe;
2e70e241 355 struct net_device *netdev;
030f4e00
CL
356
357 fcoe = container_of(kref, struct fcoe_interface, kref);
2e70e241
CL
358 netdev = fcoe->netdev;
359 /* tear-down the FCoE controller */
360 fcoe_ctlr_destroy(&fcoe->ctlr);
030f4e00 361 kfree(fcoe);
2e70e241 362 dev_put(netdev);
030f4e00
CL
363}
364
365/**
366 * fcoe_interface_get()
367 * @fcoe:
368 */
369static inline void fcoe_interface_get(struct fcoe_interface *fcoe)
370{
371 kref_get(&fcoe->kref);
372}
373
374/**
375 * fcoe_interface_put()
376 * @fcoe:
377 */
378static inline void fcoe_interface_put(struct fcoe_interface *fcoe)
379{
380 kref_put(&fcoe->kref, fcoe_interface_release);
381}
382
ab6b85c1
VD
383/**
384 * fcoe_fip_recv - handle a received FIP frame.
385 * @skb: the receive skb
386 * @dev: associated &net_device
387 * @ptype: the &packet_type structure which was used to register this handler.
388 * @orig_dev: original receive &net_device, in case @dev is a bond.
389 *
390 * Returns: 0 for success
391 */
392static int fcoe_fip_recv(struct sk_buff *skb, struct net_device *dev,
393 struct packet_type *ptype,
394 struct net_device *orig_dev)
395{
259ad85d 396 struct fcoe_interface *fcoe;
ab6b85c1 397
259ad85d 398 fcoe = container_of(ptype, struct fcoe_interface, fip_packet_type);
3fe9a0ba 399 fcoe_ctlr_recv(&fcoe->ctlr, skb);
ab6b85c1
VD
400 return 0;
401}
402
403/**
404 * fcoe_fip_send() - send an Ethernet-encapsulated FIP frame.
405 * @fip: FCoE controller.
406 * @skb: FIP Packet.
407 */
408static void fcoe_fip_send(struct fcoe_ctlr *fip, struct sk_buff *skb)
409{
3fe9a0ba 410 skb->dev = fcoe_from_ctlr(fip)->netdev;
ab6b85c1
VD
411 dev_queue_xmit(skb);
412}
413
414/**
415 * fcoe_update_src_mac() - Update Ethernet MAC filters.
11b56188
CL
416 * @lport: libfc lport
417 * @addr: Unicast MAC address to add.
ab6b85c1
VD
418 *
419 * Remove any previously-set unicast MAC filter.
420 * Add secondary FCoE MAC address filter for our OUI.
421 */
11b56188 422static void fcoe_update_src_mac(struct fc_lport *lport, u8 *addr)
ab6b85c1 423{
11b56188
CL
424 struct fcoe_port *port = lport_priv(lport);
425 struct fcoe_interface *fcoe = port->fcoe;
25024989 426
ab6b85c1 427 rtnl_lock();
11b56188
CL
428 if (!is_zero_ether_addr(port->data_src_addr))
429 dev_unicast_delete(fcoe->netdev, port->data_src_addr);
430 if (!is_zero_ether_addr(addr))
431 dev_unicast_add(fcoe->netdev, addr);
432 memcpy(port->data_src_addr, addr, ETH_ALEN);
ab6b85c1
VD
433 rtnl_unlock();
434}
435
11b56188
CL
436/**
437 * fcoe_get_src_mac() - return the Ethernet source address for an lport
438 * @lport: libfc lport
439 */
440static u8 *fcoe_get_src_mac(struct fc_lport *lport)
441{
442 struct fcoe_port *port = lport_priv(lport);
443
444 return port->data_src_addr;
445}
446
7f349142
VD
447/**
448 * fcoe_lport_config() - sets up the fc_lport
449 * @lp: ptr to the fc_lport
7f349142
VD
450 *
451 * Returns: 0 for success
452 */
453static int fcoe_lport_config(struct fc_lport *lp)
454{
455 lp->link_up = 0;
456 lp->qfull = 0;
457 lp->max_retry_count = 3;
a3666955 458 lp->max_rport_retry_count = 3;
7f349142
VD
459 lp->e_d_tov = 2 * 1000; /* FC-FS default */
460 lp->r_a_tov = 2 * 2 * 1000;
461 lp->service_params = (FCP_SPPF_INIT_FCN | FCP_SPPF_RD_XRDY_DIS |
462 FCP_SPPF_RETRY | FCP_SPPF_CONF_COMPL);
9a05753b 463 lp->does_npiv = 1;
7f349142
VD
464
465 fc_lport_init_stats(lp);
466
467 /* lport fc_lport related configuration */
468 fc_lport_config(lp);
469
470 /* offload related configuration */
471 lp->crc_offload = 0;
472 lp->seq_offload = 0;
473 lp->lro_enabled = 0;
474 lp->lro_xid = 0;
475 lp->lso_max = 0;
476
477 return 0;
478}
479
1047f221
VD
480/**
481 * fcoe_queue_timer() - fcoe queue timer
482 * @lp: the fc_lport pointer
483 *
484 * Calls fcoe_check_wait_queue on timeout
485 *
486 */
487static void fcoe_queue_timer(ulong lp)
488{
489 fcoe_check_wait_queue((struct fc_lport *)lp, NULL);
490}
491
7f349142
VD
492/**
493 * fcoe_netdev_config() - Set up netdev for SW FCoE
494 * @lp : ptr to the fc_lport
495 * @netdev : ptr to the associated netdevice struct
496 *
497 * Must be called after fcoe_lport_config() as it will use lport mutex
498 *
499 * Returns : 0 for success
500 */
501static int fcoe_netdev_config(struct fc_lport *lp, struct net_device *netdev)
502{
503 u32 mfs;
504 u64 wwnn, wwpn;
25024989 505 struct fcoe_interface *fcoe;
014f5c3f 506 struct fcoe_port *port;
7f349142
VD
507
508 /* Setup lport private data to point to fcoe softc */
014f5c3f 509 port = lport_priv(lp);
25024989 510 fcoe = port->fcoe;
7f349142
VD
511
512 /*
513 * Determine max frame size based on underlying device and optional
514 * user-configured limit. If the MFS is too low, fcoe_link_ok()
515 * will return 0, so do this first.
516 */
7221d7e5
YZ
517 mfs = netdev->mtu;
518 if (netdev->features & NETIF_F_FCOE_MTU) {
519 mfs = FCOE_MTU;
520 FCOE_NETDEV_DBG(netdev, "Supports FCOE_MTU of %d bytes\n", mfs);
521 }
522 mfs -= (sizeof(struct fcoe_hdr) + sizeof(struct fcoe_crc_eof));
7f349142
VD
523 if (fc_set_mfs(lp, mfs))
524 return -EINVAL;
525
7f349142 526 /* offload features support */
1d1b88dc 527 if (netdev->features & NETIF_F_SG)
7f349142
VD
528 lp->sg_supp = 1;
529
7f349142
VD
530 if (netdev->features & NETIF_F_FCOE_CRC) {
531 lp->crc_offload = 1;
d5488eb9 532 FCOE_NETDEV_DBG(netdev, "Supports FCCRC offload\n");
7f349142 533 }
7f349142
VD
534 if (netdev->features & NETIF_F_FSO) {
535 lp->seq_offload = 1;
536 lp->lso_max = netdev->gso_max_size;
d5488eb9
RL
537 FCOE_NETDEV_DBG(netdev, "Supports LSO for max len 0x%x\n",
538 lp->lso_max);
7f349142 539 }
7f349142
VD
540 if (netdev->fcoe_ddp_xid) {
541 lp->lro_enabled = 1;
542 lp->lro_xid = netdev->fcoe_ddp_xid;
d5488eb9
RL
543 FCOE_NETDEV_DBG(netdev, "Supports LRO for max xid 0x%x\n",
544 lp->lro_xid);
7f349142 545 }
014f5c3f
CL
546 skb_queue_head_init(&port->fcoe_pending_queue);
547 port->fcoe_pending_queue_active = 0;
548 setup_timer(&port->timer, fcoe_queue_timer, (unsigned long)lp);
7f349142 549
9a05753b
CL
550 if (!lp->vport) {
551 wwnn = fcoe_wwn_from_mac(netdev->dev_addr, 1, 0);
552 fc_set_wwnn(lp, wwnn);
553 /* XXX - 3rd arg needs to be vlan id */
554 wwpn = fcoe_wwn_from_mac(netdev->dev_addr, 2, 0);
555 fc_set_wwpn(lp, wwpn);
556 }
7f349142 557
7f349142
VD
558 return 0;
559}
560
561/**
562 * fcoe_shost_config() - Sets up fc_lport->host
563 * @lp : ptr to the fc_lport
564 * @shost : ptr to the associated scsi host
565 * @dev : device associated to scsi host
566 *
567 * Must be called after fcoe_lport_config() and fcoe_netdev_config()
568 *
569 * Returns : 0 for success
570 */
571static int fcoe_shost_config(struct fc_lport *lp, struct Scsi_Host *shost,
572 struct device *dev)
573{
574 int rc = 0;
575
576 /* lport scsi host config */
7f349142
VD
577 lp->host->max_lun = FCOE_MAX_LUN;
578 lp->host->max_id = FCOE_MAX_FCP_TARGET;
579 lp->host->max_channel = 0;
e9084bb8
CL
580 if (lp->vport)
581 lp->host->transportt = fcoe_vport_transport_template;
582 else
583 lp->host->transportt = fcoe_transport_template;
7f349142
VD
584
585 /* add the new host to the SCSI-ml */
586 rc = scsi_add_host(lp->host, dev);
587 if (rc) {
d5488eb9
RL
588 FCOE_NETDEV_DBG(fcoe_netdev(lp), "fcoe_shost_config: "
589 "error on scsi_add_host\n");
7f349142
VD
590 return rc;
591 }
9a05753b
CL
592
593 if (!lp->vport)
594 fc_host_max_npiv_vports(lp->host) = USHORT_MAX;
595
5baa17c3
CL
596 snprintf(fc_host_symbolic_name(lp->host), FC_SYMBOLIC_NAME_SIZE,
597 "%s v%s over %s", FCOE_NAME, FCOE_VERSION,
598 fcoe_netdev(lp)->name);
7f349142
VD
599
600 return 0;
601}
602
d7179680
VD
603/*
604 * fcoe_oem_match() - match for read types IO
605 * @fp: the fc_frame for new IO.
606 *
607 * Returns : true for read types IO, otherwise returns false.
608 */
609bool fcoe_oem_match(struct fc_frame *fp)
610{
05cc7390
YZ
611 return fc_fcp_is_read(fr_fsp(fp)) &&
612 (fr_fsp(fp)->data_len > fcoe_ddp_min);
d7179680
VD
613}
614
7f349142
VD
615/**
616 * fcoe_em_config() - allocates em for this lport
014f5c3f 617 * @lp: the fcoe that em is to allocated for
7f349142
VD
618 *
619 * Returns : 0 on success
620 */
621static inline int fcoe_em_config(struct fc_lport *lp)
622{
014f5c3f 623 struct fcoe_port *port = lport_priv(lp);
25024989
CL
624 struct fcoe_interface *fcoe = port->fcoe;
625 struct fcoe_interface *oldfcoe = NULL;
1d1b88dc 626 struct net_device *old_real_dev, *cur_real_dev;
d7179680
VD
627 u16 min_xid = FCOE_MIN_XID;
628 u16 max_xid = FCOE_MAX_XID;
629
630 /*
631 * Check if need to allocate an em instance for
632 * offload exchange ids to be shared across all VN_PORTs/lport.
633 */
634 if (!lp->lro_enabled || !lp->lro_xid || (lp->lro_xid >= max_xid)) {
635 lp->lro_xid = 0;
636 goto skip_oem;
637 }
638
639 /*
640 * Reuse existing offload em instance in case
1d1b88dc 641 * it is already allocated on real eth device
d7179680 642 */
25024989
CL
643 if (fcoe->netdev->priv_flags & IFF_802_1Q_VLAN)
644 cur_real_dev = vlan_dev_real_dev(fcoe->netdev);
1d1b88dc 645 else
25024989 646 cur_real_dev = fcoe->netdev;
1d1b88dc 647
25024989 648 list_for_each_entry(oldfcoe, &fcoe_hostlist, list) {
25024989
CL
649 if (oldfcoe->netdev->priv_flags & IFF_802_1Q_VLAN)
650 old_real_dev = vlan_dev_real_dev(oldfcoe->netdev);
1d1b88dc 651 else
25024989 652 old_real_dev = oldfcoe->netdev;
1d1b88dc
VD
653
654 if (cur_real_dev == old_real_dev) {
991cbb60 655 fcoe->oem = oldfcoe->oem;
d7179680
VD
656 break;
657 }
658 }
659
991cbb60
CL
660 if (fcoe->oem) {
661 if (!fc_exch_mgr_add(lp, fcoe->oem, fcoe_oem_match)) {
d7179680
VD
662 printk(KERN_ERR "fcoe_em_config: failed to add "
663 "offload em:%p on interface:%s\n",
991cbb60 664 fcoe->oem, fcoe->netdev->name);
d7179680
VD
665 return -ENOMEM;
666 }
667 } else {
991cbb60 668 fcoe->oem = fc_exch_mgr_alloc(lp, FC_CLASS_3,
d7179680
VD
669 FCOE_MIN_XID, lp->lro_xid,
670 fcoe_oem_match);
991cbb60 671 if (!fcoe->oem) {
d7179680
VD
672 printk(KERN_ERR "fcoe_em_config: failed to allocate "
673 "em for offload exches on interface:%s\n",
25024989 674 fcoe->netdev->name);
d7179680
VD
675 return -ENOMEM;
676 }
677 }
678
679 /*
680 * Exclude offload EM xid range from next EM xid range.
681 */
682 min_xid += lp->lro_xid + 1;
683
684skip_oem:
685 if (!fc_exch_mgr_alloc(lp, FC_CLASS_3, min_xid, max_xid, NULL)) {
686 printk(KERN_ERR "fcoe_em_config: failed to "
25024989 687 "allocate em on interface %s\n", fcoe->netdev->name);
7f349142 688 return -ENOMEM;
d7179680 689 }
7f349142
VD
690
691 return 0;
692}
693
694/**
695 * fcoe_if_destroy() - FCoE software HBA tear-down function
af7f85d9 696 * @lport: fc_lport to destroy
7f349142 697 */
af7f85d9 698static void fcoe_if_destroy(struct fc_lport *lport)
7f349142 699{
014f5c3f
CL
700 struct fcoe_port *port = lport_priv(lport);
701 struct fcoe_interface *fcoe = port->fcoe;
25024989 702 struct net_device *netdev = fcoe->netdev;
7f349142 703
d5488eb9 704 FCOE_NETDEV_DBG(netdev, "Destroying interface\n");
7f349142 705
7f349142 706 /* Logout of the fabric */
af7f85d9 707 fc_fabric_logoff(lport);
7f349142 708
54b649f8
CL
709 /* Cleanup the fc_lport */
710 fc_lport_destroy(lport);
711 fc_fcp_destroy(lport);
712
713 /* Stop the transmit retry timer */
714 del_timer_sync(&port->timer);
ab6b85c1 715
54b649f8
CL
716 /* Free existing transmit skbs */
717 fcoe_clean_pending_queue(lport);
718
11b56188
CL
719 rtnl_lock();
720 if (!is_zero_ether_addr(port->data_src_addr))
721 dev_unicast_delete(netdev, port->data_src_addr);
722 rtnl_unlock();
723
54b649f8
CL
724 /* receives may not be stopped until after this */
725 fcoe_interface_put(fcoe);
7f349142 726
f161fb72 727 /* Free queued packets for the per-CPU receive threads */
af7f85d9 728 fcoe_percpu_clean(lport);
f161fb72 729
7f349142 730 /* Detach from the scsi-ml */
af7f85d9
CL
731 fc_remove_host(lport->host);
732 scsi_remove_host(lport->host);
7f349142
VD
733
734 /* There are no more rports or I/O, free the EM */
af7f85d9 735 fc_exch_mgr_free(lport);
7f349142 736
7f349142 737 /* Free memory used by statistical counters */
af7f85d9 738 fc_lport_free_stats(lport);
7f349142 739
2e70e241 740 /* Release the Scsi_Host */
af7f85d9 741 scsi_host_put(lport->host);
7f349142
VD
742}
743
744/*
745 * fcoe_ddp_setup - calls LLD's ddp_setup through net_device
746 * @lp: the corresponding fc_lport
747 * @xid: the exchange id for this ddp transfer
748 * @sgl: the scatterlist describing this transfer
749 * @sgc: number of sg items
750 *
751 * Returns : 0 no ddp
752 */
753static int fcoe_ddp_setup(struct fc_lport *lp, u16 xid,
754 struct scatterlist *sgl, unsigned int sgc)
755{
756 struct net_device *n = fcoe_netdev(lp);
757
b04d023c 758 if (n->netdev_ops->ndo_fcoe_ddp_setup)
7f349142
VD
759 return n->netdev_ops->ndo_fcoe_ddp_setup(n, xid, sgl, sgc);
760
761 return 0;
762}
763
764/*
765 * fcoe_ddp_done - calls LLD's ddp_done through net_device
766 * @lp: the corresponding fc_lport
767 * @xid: the exchange id for this ddp transfer
768 *
769 * Returns : the length of data that have been completed by ddp
770 */
771static int fcoe_ddp_done(struct fc_lport *lp, u16 xid)
772{
773 struct net_device *n = fcoe_netdev(lp);
774
b04d023c 775 if (n->netdev_ops->ndo_fcoe_ddp_done)
7f349142
VD
776 return n->netdev_ops->ndo_fcoe_ddp_done(n, xid);
777 return 0;
778}
779
11b56188
CL
780static struct fc_seq *fcoe_elsct_send(struct fc_lport *lport,
781 u32 did, struct fc_frame *fp, unsigned int op,
782 void (*resp)(struct fc_seq *, struct fc_frame *, void *),
783 void *arg, u32 timeout);
784
7f349142
VD
785static struct libfc_function_template fcoe_libfc_fcn_templ = {
786 .frame_send = fcoe_xmit,
787 .ddp_setup = fcoe_ddp_setup,
788 .ddp_done = fcoe_ddp_done,
11b56188 789 .elsct_send = fcoe_elsct_send,
7f349142
VD
790};
791
792/**
030f4e00
CL
793 * fcoe_if_create() - this function creates the fcoe port
794 * @fcoe: fcoe_interface structure to create an fc_lport instance on
af7f85d9 795 * @parent: device pointer to be the parent in sysfs for the SCSI host
9a05753b 796 * @npiv: is this a vport?
7f349142 797 *
54b649f8 798 * Creates fc_lport struct and scsi_host for lport, configures lport.
7f349142 799 *
af7f85d9 800 * Returns : The allocated fc_lport or an error pointer
7f349142 801 */
030f4e00 802static struct fc_lport *fcoe_if_create(struct fcoe_interface *fcoe,
9a05753b 803 struct device *parent, int npiv)
7f349142
VD
804{
805 int rc;
af7f85d9 806 struct fc_lport *lport = NULL;
014f5c3f 807 struct fcoe_port *port;
7f349142 808 struct Scsi_Host *shost;
030f4e00 809 struct net_device *netdev = fcoe->netdev;
9a05753b
CL
810 /*
811 * parent is only a vport if npiv is 1,
812 * but we'll only use vport in that case so go ahead and set it
813 */
814 struct fc_vport *vport = dev_to_vport(parent);
7f349142 815
d5488eb9 816 FCOE_NETDEV_DBG(netdev, "Create Interface\n");
7f349142 817
9a05753b
CL
818 if (!npiv) {
819 lport = libfc_host_alloc(&fcoe_shost_template,
820 sizeof(struct fcoe_port));
821 } else {
822 lport = libfc_vport_create(vport,
823 sizeof(struct fcoe_port));
824 }
86221969 825 if (!lport) {
d5488eb9 826 FCOE_NETDEV_DBG(netdev, "Could not allocate host structure\n");
af7f85d9 827 rc = -ENOMEM;
030f4e00 828 goto out;
7f349142 829 }
86221969 830 shost = lport->host;
014f5c3f 831 port = lport_priv(lport);
2e70e241 832 port->lport = lport;
014f5c3f 833 port->fcoe = fcoe;
2e70e241 834 INIT_WORK(&port->destroy_work, fcoe_destroy_work);
7f349142
VD
835
836 /* configure fc_lport, e.g., em */
af7f85d9 837 rc = fcoe_lport_config(lport);
7f349142 838 if (rc) {
d5488eb9
RL
839 FCOE_NETDEV_DBG(netdev, "Could not configure lport for the "
840 "interface\n");
7f349142
VD
841 goto out_host_put;
842 }
843
9a05753b
CL
844 if (npiv) {
845 FCOE_NETDEV_DBG(netdev, "Setting vport names, 0x%llX 0x%llX\n",
846 vport->node_name, vport->port_name);
847 fc_set_wwnn(lport, vport->node_name);
848 fc_set_wwpn(lport, vport->port_name);
849 }
850
ab6b85c1 851 /* configure lport network properties */
af7f85d9 852 rc = fcoe_netdev_config(lport, netdev);
ab6b85c1 853 if (rc) {
d5488eb9
RL
854 FCOE_NETDEV_DBG(netdev, "Could not configure netdev for the "
855 "interface\n");
54b649f8 856 goto out_lp_destroy;
ab6b85c1 857 }
97c8389d 858
7f349142 859 /* configure lport scsi host properties */
af7f85d9 860 rc = fcoe_shost_config(lport, shost, parent);
7f349142 861 if (rc) {
d5488eb9
RL
862 FCOE_NETDEV_DBG(netdev, "Could not configure shost for the "
863 "interface\n");
54b649f8 864 goto out_lp_destroy;
7f349142
VD
865 }
866
96316099 867 /* Initialize the library */
af7f85d9 868 rc = fcoe_libfc_config(lport, &fcoe_libfc_fcn_templ);
7f349142 869 if (rc) {
96316099 870 FCOE_NETDEV_DBG(netdev, "Could not configure libfc for the "
d5488eb9 871 "interface\n");
96316099 872 goto out_lp_destroy;
7f349142
VD
873 }
874
9a05753b
CL
875 if (!npiv) {
876 /*
877 * fcoe_em_alloc() and fcoe_hostlist_add() both
878 * need to be atomic with respect to other changes to the
879 * hostlist since fcoe_em_alloc() looks for an existing EM
880 * instance on host list updated by fcoe_hostlist_add().
881 *
882 * This is currently handled through the fcoe_config_mutex
883 * begin held.
884 */
c863df33 885
9a05753b
CL
886 /* lport exch manager allocation */
887 rc = fcoe_em_config(lport);
888 if (rc) {
889 FCOE_NETDEV_DBG(netdev, "Could not configure the EM "
890 "for the interface\n");
891 goto out_lp_destroy;
892 }
7f349142
VD
893 }
894
030f4e00 895 fcoe_interface_get(fcoe);
af7f85d9 896 return lport;
7f349142
VD
897
898out_lp_destroy:
af7f85d9 899 fc_exch_mgr_free(lport);
7f349142 900out_host_put:
af7f85d9
CL
901 scsi_host_put(lport->host);
902out:
903 return ERR_PTR(rc);
7f349142
VD
904}
905
906/**
907 * fcoe_if_init() - attach to scsi transport
908 *
909 * Returns : 0 on success
910 */
911static int __init fcoe_if_init(void)
912{
913 /* attach to scsi transport */
e9084bb8
CL
914 fcoe_transport_template = fc_attach_transport(&fcoe_transport_function);
915 fcoe_vport_transport_template =
916 fc_attach_transport(&fcoe_vport_transport_function);
7f349142 917
e9084bb8 918 if (!fcoe_transport_template) {
d5488eb9 919 printk(KERN_ERR "fcoe: Failed to attach to the FC transport\n");
7f349142
VD
920 return -ENODEV;
921 }
922
923 return 0;
924}
925
926/**
927 * fcoe_if_exit() - detach from scsi transport
928 *
929 * Returns : 0 on success
930 */
931int __exit fcoe_if_exit(void)
932{
e9084bb8
CL
933 fc_release_transport(fcoe_transport_template);
934 fc_release_transport(fcoe_vport_transport_template);
935 fcoe_transport_template = NULL;
936 fcoe_vport_transport_template = NULL;
7f349142
VD
937 return 0;
938}
939
8976f424
RL
940/**
941 * fcoe_percpu_thread_create() - Create a receive thread for an online cpu
942 * @cpu: cpu index for the online cpu
943 */
944static void fcoe_percpu_thread_create(unsigned int cpu)
945{
946 struct fcoe_percpu_s *p;
947 struct task_struct *thread;
948
949 p = &per_cpu(fcoe_percpu, cpu);
950
951 thread = kthread_create(fcoe_percpu_receive_thread,
952 (void *)p, "fcoethread/%d", cpu);
953
e7a51997 954 if (likely(!IS_ERR(thread))) {
8976f424
RL
955 kthread_bind(thread, cpu);
956 wake_up_process(thread);
957
958 spin_lock_bh(&p->fcoe_rx_list.lock);
959 p->thread = thread;
960 spin_unlock_bh(&p->fcoe_rx_list.lock);
961 }
962}
963
964/**
965 * fcoe_percpu_thread_destroy() - removes the rx thread for the given cpu
966 * @cpu: cpu index the rx thread is to be removed
967 *
968 * Destroys a per-CPU Rx thread. Any pending skbs are moved to the
969 * current CPU's Rx thread. If the thread being destroyed is bound to
970 * the CPU processing this context the skbs will be freed.
971 */
972static void fcoe_percpu_thread_destroy(unsigned int cpu)
973{
974 struct fcoe_percpu_s *p;
975 struct task_struct *thread;
976 struct page *crc_eof;
977 struct sk_buff *skb;
978#ifdef CONFIG_SMP
979 struct fcoe_percpu_s *p0;
980 unsigned targ_cpu = smp_processor_id();
981#endif /* CONFIG_SMP */
982
d5488eb9 983 FCOE_DBG("Destroying receive thread for CPU %d\n", cpu);
8976f424
RL
984
985 /* Prevent any new skbs from being queued for this CPU. */
986 p = &per_cpu(fcoe_percpu, cpu);
987 spin_lock_bh(&p->fcoe_rx_list.lock);
988 thread = p->thread;
989 p->thread = NULL;
990 crc_eof = p->crc_eof_page;
991 p->crc_eof_page = NULL;
992 p->crc_eof_offset = 0;
993 spin_unlock_bh(&p->fcoe_rx_list.lock);
994
995#ifdef CONFIG_SMP
996 /*
997 * Don't bother moving the skb's if this context is running
998 * on the same CPU that is having its thread destroyed. This
999 * can easily happen when the module is removed.
1000 */
1001 if (cpu != targ_cpu) {
1002 p0 = &per_cpu(fcoe_percpu, targ_cpu);
1003 spin_lock_bh(&p0->fcoe_rx_list.lock);
1004 if (p0->thread) {
d5488eb9
RL
1005 FCOE_DBG("Moving frames from CPU %d to CPU %d\n",
1006 cpu, targ_cpu);
8976f424
RL
1007
1008 while ((skb = __skb_dequeue(&p->fcoe_rx_list)) != NULL)
1009 __skb_queue_tail(&p0->fcoe_rx_list, skb);
1010 spin_unlock_bh(&p0->fcoe_rx_list.lock);
1011 } else {
1012 /*
1013 * The targeted CPU is not initialized and cannot accept
1014 * new skbs. Unlock the targeted CPU and drop the skbs
1015 * on the CPU that is going offline.
1016 */
1017 while ((skb = __skb_dequeue(&p->fcoe_rx_list)) != NULL)
1018 kfree_skb(skb);
1019 spin_unlock_bh(&p0->fcoe_rx_list.lock);
1020 }
1021 } else {
1022 /*
1023 * This scenario occurs when the module is being removed
1024 * and all threads are being destroyed. skbs will continue
1025 * to be shifted from the CPU thread that is being removed
1026 * to the CPU thread associated with the CPU that is processing
1027 * the module removal. Once there is only one CPU Rx thread it
1028 * will reach this case and we will drop all skbs and later
1029 * stop the thread.
1030 */
1031 spin_lock_bh(&p->fcoe_rx_list.lock);
1032 while ((skb = __skb_dequeue(&p->fcoe_rx_list)) != NULL)
1033 kfree_skb(skb);
1034 spin_unlock_bh(&p->fcoe_rx_list.lock);
1035 }
1036#else
1037 /*
dd3fd72e 1038 * This a non-SMP scenario where the singular Rx thread is
8976f424
RL
1039 * being removed. Free all skbs and stop the thread.
1040 */
1041 spin_lock_bh(&p->fcoe_rx_list.lock);
1042 while ((skb = __skb_dequeue(&p->fcoe_rx_list)) != NULL)
1043 kfree_skb(skb);
1044 spin_unlock_bh(&p->fcoe_rx_list.lock);
1045#endif
1046
1047 if (thread)
1048 kthread_stop(thread);
1049
1050 if (crc_eof)
1051 put_page(crc_eof);
1052}
1053
1054/**
1055 * fcoe_cpu_callback() - fcoe cpu hotplug event callback
1056 * @nfb: callback data block
1057 * @action: event triggering the callback
1058 * @hcpu: index for the cpu of this event
1059 *
1060 * This creates or destroys per cpu data for fcoe
1061 *
1062 * Returns NOTIFY_OK always.
1063 */
1064static int fcoe_cpu_callback(struct notifier_block *nfb,
1065 unsigned long action, void *hcpu)
1066{
1067 unsigned cpu = (unsigned long)hcpu;
1068
1069 switch (action) {
1070 case CPU_ONLINE:
1071 case CPU_ONLINE_FROZEN:
d5488eb9 1072 FCOE_DBG("CPU %x online: Create Rx thread\n", cpu);
8976f424
RL
1073 fcoe_percpu_thread_create(cpu);
1074 break;
1075 case CPU_DEAD:
1076 case CPU_DEAD_FROZEN:
d5488eb9 1077 FCOE_DBG("CPU %x offline: Remove Rx thread\n", cpu);
8976f424
RL
1078 fcoe_percpu_thread_destroy(cpu);
1079 break;
1080 default:
1081 break;
1082 }
1083 return NOTIFY_OK;
1084}
1085
1086static struct notifier_block fcoe_cpu_notifier = {
1087 .notifier_call = fcoe_cpu_callback,
1088};
1089
85b4aa49 1090/**
34f42a07 1091 * fcoe_rcv() - this is the fcoe receive function called by NET_RX_SOFTIRQ
85b4aa49
RL
1092 * @skb: the receive skb
1093 * @dev: associated net device
1094 * @ptype: context
dd3fd72e 1095 * @olddev: last device
85b4aa49
RL
1096 *
1097 * this function will receive the packet and build fc frame and pass it up
1098 *
1099 * Returns: 0 for success
34f42a07 1100 */
85b4aa49
RL
1101int fcoe_rcv(struct sk_buff *skb, struct net_device *dev,
1102 struct packet_type *ptype, struct net_device *olddev)
1103{
1104 struct fc_lport *lp;
1105 struct fcoe_rcv_info *fr;
259ad85d 1106 struct fcoe_interface *fcoe;
85b4aa49 1107 struct fc_frame_header *fh;
85b4aa49 1108 struct fcoe_percpu_s *fps;
b2f0091f 1109 unsigned int cpu;
85b4aa49 1110
259ad85d 1111 fcoe = container_of(ptype, struct fcoe_interface, fcoe_packet_type);
3fe9a0ba 1112 lp = fcoe->ctlr.lp;
85b4aa49 1113 if (unlikely(lp == NULL)) {
d5488eb9 1114 FCOE_NETDEV_DBG(dev, "Cannot find hba structure");
85b4aa49
RL
1115 goto err2;
1116 }
97c8389d
JE
1117 if (!lp->link_up)
1118 goto err2;
85b4aa49 1119
d5488eb9
RL
1120 FCOE_NETDEV_DBG(dev, "skb_info: len:%d data_len:%d head:%p "
1121 "data:%p tail:%p end:%p sum:%d dev:%s",
1122 skb->len, skb->data_len, skb->head, skb->data,
1123 skb_tail_pointer(skb), skb_end_pointer(skb),
1124 skb->csum, skb->dev ? skb->dev->name : "<NULL>");
85b4aa49
RL
1125
1126 /* check for FCOE packet type */
1127 if (unlikely(eth_hdr(skb)->h_proto != htons(ETH_P_FCOE))) {
d5488eb9 1128 FCOE_NETDEV_DBG(dev, "Wrong FC type frame");
85b4aa49
RL
1129 goto err;
1130 }
1131
1132 /*
1133 * Check for minimum frame length, and make sure required FCoE
1134 * and FC headers are pulled into the linear data area.
1135 */
1136 if (unlikely((skb->len < FCOE_MIN_FRAME) ||
1137 !pskb_may_pull(skb, FCOE_HEADER_LEN)))
1138 goto err;
1139
1140 skb_set_transport_header(skb, sizeof(struct fcoe_hdr));
1141 fh = (struct fc_frame_header *) skb_transport_header(skb);
1142
85b4aa49
RL
1143 fr = fcoe_dev_from_skb(skb);
1144 fr->fr_dev = lp;
1145 fr->ptype = ptype;
5e5e92df 1146
85b4aa49 1147 /*
b2f0091f
VD
1148 * In case the incoming frame's exchange is originated from
1149 * the initiator, then received frame's exchange id is ANDed
1150 * with fc_cpu_mask bits to get the same cpu on which exchange
1151 * was originated, otherwise just use the current cpu.
85b4aa49 1152 */
b2f0091f
VD
1153 if (ntoh24(fh->fh_f_ctl) & FC_FC_EX_CTX)
1154 cpu = ntohs(fh->fh_ox_id) & fc_cpu_mask;
1155 else
1156 cpu = smp_processor_id();
5e5e92df 1157
8976f424 1158 fps = &per_cpu(fcoe_percpu, cpu);
85b4aa49 1159 spin_lock_bh(&fps->fcoe_rx_list.lock);
8976f424
RL
1160 if (unlikely(!fps->thread)) {
1161 /*
1162 * The targeted CPU is not ready, let's target
1163 * the first CPU now. For non-SMP systems this
1164 * will check the same CPU twice.
1165 */
d5488eb9
RL
1166 FCOE_NETDEV_DBG(dev, "CPU is online, but no receive thread "
1167 "ready for incoming skb- using first online "
1168 "CPU.\n");
8976f424
RL
1169
1170 spin_unlock_bh(&fps->fcoe_rx_list.lock);
1171 cpu = first_cpu(cpu_online_map);
1172 fps = &per_cpu(fcoe_percpu, cpu);
1173 spin_lock_bh(&fps->fcoe_rx_list.lock);
1174 if (!fps->thread) {
1175 spin_unlock_bh(&fps->fcoe_rx_list.lock);
1176 goto err;
1177 }
1178 }
1179
1180 /*
1181 * We now have a valid CPU that we're targeting for
1182 * this skb. We also have this receive thread locked,
1183 * so we're free to queue skbs into it's queue.
1184 */
85b4aa49
RL
1185 __skb_queue_tail(&fps->fcoe_rx_list, skb);
1186 if (fps->fcoe_rx_list.qlen == 1)
1187 wake_up_process(fps->thread);
1188
1189 spin_unlock_bh(&fps->fcoe_rx_list.lock);
1190
1191 return 0;
1192err:
582b45bc 1193 fc_lport_get_stats(lp)->ErrorFrames++;
85b4aa49
RL
1194
1195err2:
1196 kfree_skb(skb);
1197 return -1;
1198}
85b4aa49
RL
1199
1200/**
34f42a07 1201 * fcoe_start_io() - pass to netdev to start xmit for fcoe
85b4aa49
RL
1202 * @skb: the skb to be xmitted
1203 *
1204 * Returns: 0 for success
34f42a07 1205 */
85b4aa49
RL
1206static inline int fcoe_start_io(struct sk_buff *skb)
1207{
1208 int rc;
1209
1210 skb_get(skb);
1211 rc = dev_queue_xmit(skb);
1212 if (rc != 0)
1213 return rc;
1214 kfree_skb(skb);
1215 return 0;
1216}
1217
1218/**
dd3fd72e 1219 * fcoe_get_paged_crc_eof() - in case we need to alloc a page for crc_eof
85b4aa49
RL
1220 * @skb: the skb to be xmitted
1221 * @tlen: total len
1222 *
1223 * Returns: 0 for success
34f42a07 1224 */
85b4aa49
RL
1225static int fcoe_get_paged_crc_eof(struct sk_buff *skb, int tlen)
1226{
1227 struct fcoe_percpu_s *fps;
1228 struct page *page;
85b4aa49 1229
5e5e92df 1230 fps = &get_cpu_var(fcoe_percpu);
85b4aa49
RL
1231 page = fps->crc_eof_page;
1232 if (!page) {
1233 page = alloc_page(GFP_ATOMIC);
1234 if (!page) {
5e5e92df 1235 put_cpu_var(fcoe_percpu);
85b4aa49
RL
1236 return -ENOMEM;
1237 }
1238 fps->crc_eof_page = page;
8976f424 1239 fps->crc_eof_offset = 0;
85b4aa49
RL
1240 }
1241
1242 get_page(page);
1243 skb_fill_page_desc(skb, skb_shinfo(skb)->nr_frags, page,
1244 fps->crc_eof_offset, tlen);
1245 skb->len += tlen;
1246 skb->data_len += tlen;
1247 skb->truesize += tlen;
1248 fps->crc_eof_offset += sizeof(struct fcoe_crc_eof);
1249
1250 if (fps->crc_eof_offset >= PAGE_SIZE) {
1251 fps->crc_eof_page = NULL;
1252 fps->crc_eof_offset = 0;
1253 put_page(page);
1254 }
5e5e92df 1255 put_cpu_var(fcoe_percpu);
85b4aa49
RL
1256 return 0;
1257}
1258
1259/**
34f42a07 1260 * fcoe_fc_crc() - calculates FC CRC in this fcoe skb
dd3fd72e 1261 * @fp: the fc_frame containing data to be checksummed
85b4aa49 1262 *
014f5c3f 1263 * This uses crc32() to calculate the crc for port frame
85b4aa49 1264 * Return : 32 bit crc
34f42a07 1265 */
85b4aa49
RL
1266u32 fcoe_fc_crc(struct fc_frame *fp)
1267{
1268 struct sk_buff *skb = fp_skb(fp);
1269 struct skb_frag_struct *frag;
1270 unsigned char *data;
1271 unsigned long off, len, clen;
1272 u32 crc;
1273 unsigned i;
1274
1275 crc = crc32(~0, skb->data, skb_headlen(skb));
1276
1277 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
1278 frag = &skb_shinfo(skb)->frags[i];
1279 off = frag->page_offset;
1280 len = frag->size;
1281 while (len > 0) {
1282 clen = min(len, PAGE_SIZE - (off & ~PAGE_MASK));
1283 data = kmap_atomic(frag->page + (off >> PAGE_SHIFT),
1284 KM_SKB_DATA_SOFTIRQ);
1285 crc = crc32(crc, data + (off & ~PAGE_MASK), clen);
1286 kunmap_atomic(data, KM_SKB_DATA_SOFTIRQ);
1287 off += clen;
1288 len -= clen;
1289 }
1290 }
1291 return crc;
1292}
85b4aa49
RL
1293
1294/**
34f42a07 1295 * fcoe_xmit() - FCoE frame transmit function
014f5c3f 1296 * @lp: the associated local fcoe
85b4aa49
RL
1297 * @fp: the fc_frame to be transmitted
1298 *
1299 * Return : 0 for success
34f42a07 1300 */
85b4aa49
RL
1301int fcoe_xmit(struct fc_lport *lp, struct fc_frame *fp)
1302{
4bb6b515 1303 int wlen;
85b4aa49
RL
1304 u32 crc;
1305 struct ethhdr *eh;
1306 struct fcoe_crc_eof *cp;
1307 struct sk_buff *skb;
1308 struct fcoe_dev_stats *stats;
1309 struct fc_frame_header *fh;
1310 unsigned int hlen; /* header length implies the version */
1311 unsigned int tlen; /* trailer length */
1312 unsigned int elen; /* eth header, may include vlan */
3fe9a0ba
CL
1313 struct fcoe_port *port = lport_priv(lp);
1314 struct fcoe_interface *fcoe = port->fcoe;
85b4aa49
RL
1315 u8 sof, eof;
1316 struct fcoe_hdr *hp;
1317
1318 WARN_ON((fr_len(fp) % sizeof(u32)) != 0);
1319
85b4aa49 1320 fh = fc_frame_header_get(fp);
97c8389d
JE
1321 skb = fp_skb(fp);
1322 wlen = skb->len / FCOE_WORD_TO_BYTE;
1323
1324 if (!lp->link_up) {
3caf02ee 1325 kfree_skb(skb);
97c8389d 1326 return 0;
85b4aa49
RL
1327 }
1328
97c8389d 1329 if (unlikely(fh->fh_r_ctl == FC_RCTL_ELS_REQ) &&
11b56188 1330 fcoe_ctlr_els_send(&fcoe->ctlr, lp, skb))
97c8389d
JE
1331 return 0;
1332
85b4aa49
RL
1333 sof = fr_sof(fp);
1334 eof = fr_eof(fp);
1335
4e57e1cb 1336 elen = sizeof(struct ethhdr);
85b4aa49
RL
1337 hlen = sizeof(struct fcoe_hdr);
1338 tlen = sizeof(struct fcoe_crc_eof);
1339 wlen = (skb->len - tlen + sizeof(crc)) / FCOE_WORD_TO_BYTE;
1340
1341 /* crc offload */
1342 if (likely(lp->crc_offload)) {
39ca9a06 1343 skb->ip_summed = CHECKSUM_PARTIAL;
85b4aa49
RL
1344 skb->csum_start = skb_headroom(skb);
1345 skb->csum_offset = skb->len;
1346 crc = 0;
1347 } else {
1348 skb->ip_summed = CHECKSUM_NONE;
1349 crc = fcoe_fc_crc(fp);
1350 }
1351
014f5c3f 1352 /* copy port crc and eof to the skb buff */
85b4aa49
RL
1353 if (skb_is_nonlinear(skb)) {
1354 skb_frag_t *frag;
1355 if (fcoe_get_paged_crc_eof(skb, tlen)) {
e9041581 1356 kfree_skb(skb);
85b4aa49
RL
1357 return -ENOMEM;
1358 }
1359 frag = &skb_shinfo(skb)->frags[skb_shinfo(skb)->nr_frags - 1];
1360 cp = kmap_atomic(frag->page, KM_SKB_DATA_SOFTIRQ)
1361 + frag->page_offset;
1362 } else {
1363 cp = (struct fcoe_crc_eof *)skb_put(skb, tlen);
1364 }
1365
1366 memset(cp, 0, sizeof(*cp));
1367 cp->fcoe_eof = eof;
1368 cp->fcoe_crc32 = cpu_to_le32(~crc);
1369
1370 if (skb_is_nonlinear(skb)) {
1371 kunmap_atomic(cp, KM_SKB_DATA_SOFTIRQ);
1372 cp = NULL;
1373 }
1374
014f5c3f 1375 /* adjust skb network/transport offsets to match mac/fcoe/port */
85b4aa49
RL
1376 skb_push(skb, elen + hlen);
1377 skb_reset_mac_header(skb);
1378 skb_reset_network_header(skb);
1379 skb->mac_len = elen;
211c738d 1380 skb->protocol = htons(ETH_P_FCOE);
3fe9a0ba 1381 skb->dev = fcoe->netdev;
85b4aa49
RL
1382
1383 /* fill up mac and fcoe headers */
1384 eh = eth_hdr(skb);
1385 eh->h_proto = htons(ETH_P_FCOE);
3fe9a0ba 1386 if (fcoe->ctlr.map_dest)
85b4aa49
RL
1387 fc_fcoe_set_mac(eh->h_dest, fh->fh_d_id);
1388 else
1389 /* insert GW address */
3fe9a0ba 1390 memcpy(eh->h_dest, fcoe->ctlr.dest_addr, ETH_ALEN);
85b4aa49 1391
3fe9a0ba
CL
1392 if (unlikely(fcoe->ctlr.flogi_oxid != FC_XID_UNKNOWN))
1393 memcpy(eh->h_source, fcoe->ctlr.ctl_src_addr, ETH_ALEN);
85b4aa49 1394 else
11b56188 1395 memcpy(eh->h_source, port->data_src_addr, ETH_ALEN);
85b4aa49
RL
1396
1397 hp = (struct fcoe_hdr *)(eh + 1);
1398 memset(hp, 0, sizeof(*hp));
1399 if (FC_FCOE_VER)
1400 FC_FCOE_ENCAPS_VER(hp, FC_FCOE_VER);
1401 hp->fcoe_sof = sof;
1402
39ca9a06
YZ
1403 /* fcoe lso, mss is in max_payload which is non-zero for FCP data */
1404 if (lp->seq_offload && fr_max_payload(fp)) {
1405 skb_shinfo(skb)->gso_type = SKB_GSO_FCOE;
1406 skb_shinfo(skb)->gso_size = fr_max_payload(fp);
1407 } else {
1408 skb_shinfo(skb)->gso_type = 0;
1409 skb_shinfo(skb)->gso_size = 0;
1410 }
85b4aa49 1411 /* update tx stats: regardless if LLD fails */
582b45bc
RL
1412 stats = fc_lport_get_stats(lp);
1413 stats->TxFrames++;
1414 stats->TxWords += wlen;
85b4aa49
RL
1415
1416 /* send down to lld */
1417 fr_dev(fp) = lp;
014f5c3f 1418 if (port->fcoe_pending_queue.qlen)
4bb6b515
VD
1419 fcoe_check_wait_queue(lp, skb);
1420 else if (fcoe_start_io(skb))
1421 fcoe_check_wait_queue(lp, skb);
85b4aa49
RL
1422
1423 return 0;
1424}
85b4aa49 1425
e7a51997
JE
1426/**
1427 * fcoe_percpu_flush_done() - Indicate percpu queue flush completion.
1428 * @skb: the skb being completed.
1429 */
1430static void fcoe_percpu_flush_done(struct sk_buff *skb)
1431{
1432 complete(&fcoe_flush_completion);
1433}
1434
34f42a07
RL
1435/**
1436 * fcoe_percpu_receive_thread() - recv thread per cpu
85b4aa49
RL
1437 * @arg: ptr to the fcoe per cpu struct
1438 *
1439 * Return: 0 for success
85b4aa49
RL
1440 */
1441int fcoe_percpu_receive_thread(void *arg)
1442{
1443 struct fcoe_percpu_s *p = arg;
1444 u32 fr_len;
1445 struct fc_lport *lp;
1446 struct fcoe_rcv_info *fr;
1447 struct fcoe_dev_stats *stats;
1448 struct fc_frame_header *fh;
1449 struct sk_buff *skb;
1450 struct fcoe_crc_eof crc_eof;
1451 struct fc_frame *fp;
1452 u8 *mac = NULL;
014f5c3f 1453 struct fcoe_port *port;
85b4aa49
RL
1454 struct fcoe_hdr *hp;
1455
4469c195 1456 set_user_nice(current, -20);
85b4aa49
RL
1457
1458 while (!kthread_should_stop()) {
1459
1460 spin_lock_bh(&p->fcoe_rx_list.lock);
1461 while ((skb = __skb_dequeue(&p->fcoe_rx_list)) == NULL) {
1462 set_current_state(TASK_INTERRUPTIBLE);
1463 spin_unlock_bh(&p->fcoe_rx_list.lock);
1464 schedule();
1465 set_current_state(TASK_RUNNING);
1466 if (kthread_should_stop())
1467 return 0;
1468 spin_lock_bh(&p->fcoe_rx_list.lock);
1469 }
1470 spin_unlock_bh(&p->fcoe_rx_list.lock);
1471 fr = fcoe_dev_from_skb(skb);
1472 lp = fr->fr_dev;
1473 if (unlikely(lp == NULL)) {
e7a51997
JE
1474 if (skb->destructor != fcoe_percpu_flush_done)
1475 FCOE_NETDEV_DBG(skb->dev, "NULL lport in skb");
85b4aa49
RL
1476 kfree_skb(skb);
1477 continue;
1478 }
1479
d5488eb9
RL
1480 FCOE_NETDEV_DBG(skb->dev, "skb_info: len:%d data_len:%d "
1481 "head:%p data:%p tail:%p end:%p sum:%d dev:%s",
1482 skb->len, skb->data_len,
1483 skb->head, skb->data, skb_tail_pointer(skb),
1484 skb_end_pointer(skb), skb->csum,
1485 skb->dev ? skb->dev->name : "<NULL>");
85b4aa49
RL
1486
1487 /*
1488 * Save source MAC address before discarding header.
1489 */
014f5c3f 1490 port = lport_priv(lp);
85b4aa49
RL
1491 if (skb_is_nonlinear(skb))
1492 skb_linearize(skb); /* not ideal */
97c8389d 1493 mac = eth_hdr(skb)->h_source;
85b4aa49
RL
1494
1495 /*
1496 * Frame length checks and setting up the header pointers
1497 * was done in fcoe_rcv already.
1498 */
1499 hp = (struct fcoe_hdr *) skb_network_header(skb);
1500 fh = (struct fc_frame_header *) skb_transport_header(skb);
1501
582b45bc 1502 stats = fc_lport_get_stats(lp);
85b4aa49 1503 if (unlikely(FC_FCOE_DECAPS_VER(hp) != FC_FCOE_VER)) {
582b45bc 1504 if (stats->ErrorFrames < 5)
d5488eb9 1505 printk(KERN_WARNING "fcoe: FCoE version "
582b45bc
RL
1506 "mismatch: The frame has "
1507 "version %x, but the "
1508 "initiator supports version "
1509 "%x\n", FC_FCOE_DECAPS_VER(hp),
1510 FC_FCOE_VER);
1511 stats->ErrorFrames++;
85b4aa49
RL
1512 kfree_skb(skb);
1513 continue;
1514 }
1515
1516 skb_pull(skb, sizeof(struct fcoe_hdr));
1517 fr_len = skb->len - sizeof(struct fcoe_crc_eof);
1518
582b45bc
RL
1519 stats->RxFrames++;
1520 stats->RxWords += fr_len / FCOE_WORD_TO_BYTE;
85b4aa49
RL
1521
1522 fp = (struct fc_frame *)skb;
1523 fc_frame_init(fp);
1524 fr_dev(fp) = lp;
1525 fr_sof(fp) = hp->fcoe_sof;
1526
1527 /* Copy out the CRC and EOF trailer for access */
1528 if (skb_copy_bits(skb, fr_len, &crc_eof, sizeof(crc_eof))) {
1529 kfree_skb(skb);
1530 continue;
1531 }
1532 fr_eof(fp) = crc_eof.fcoe_eof;
1533 fr_crc(fp) = crc_eof.fcoe_crc32;
1534 if (pskb_trim(skb, fr_len)) {
1535 kfree_skb(skb);
1536 continue;
1537 }
1538
1539 /*
1540 * We only check CRC if no offload is available and if it is
1541 * it's solicited data, in which case, the FCP layer would
1542 * check it during the copy.
1543 */
07c00ec4 1544 if (lp->crc_offload && skb->ip_summed == CHECKSUM_UNNECESSARY)
85b4aa49
RL
1545 fr_flags(fp) &= ~FCPHF_CRC_UNCHECKED;
1546 else
1547 fr_flags(fp) |= FCPHF_CRC_UNCHECKED;
1548
1549 fh = fc_frame_header_get(fp);
1550 if (fh->fh_r_ctl == FC_RCTL_DD_SOL_DATA &&
1551 fh->fh_type == FC_TYPE_FCP) {
52ff878c 1552 fc_exch_recv(lp, fp);
85b4aa49
RL
1553 continue;
1554 }
1555 if (fr_flags(fp) & FCPHF_CRC_UNCHECKED) {
1556 if (le32_to_cpu(fr_crc(fp)) !=
1557 ~crc32(~0, skb->data, fr_len)) {
d5488eb9 1558 if (stats->InvalidCRCCount < 5)
85b4aa49
RL
1559 printk(KERN_WARNING "fcoe: dropping "
1560 "frame with CRC error\n");
1561 stats->InvalidCRCCount++;
1562 stats->ErrorFrames++;
1563 fc_frame_free(fp);
1564 continue;
1565 }
1566 fr_flags(fp) &= ~FCPHF_CRC_UNCHECKED;
1567 }
52ff878c 1568 fc_exch_recv(lp, fp);
85b4aa49
RL
1569 }
1570 return 0;
1571}
1572
85b4aa49 1573/**
dd3fd72e
CL
1574 * fcoe_check_wait_queue() - attempt to clear the transmit backlog
1575 * @lp: the fc_lport
85b4aa49
RL
1576 *
1577 * This empties the wait_queue, dequeue the head of the wait_queue queue
1578 * and calls fcoe_start_io() for each packet, if all skb have been
c826a314 1579 * transmitted, return qlen or -1 if a error occurs, then restore
dd3fd72e 1580 * wait_queue and try again later.
85b4aa49
RL
1581 *
1582 * The wait_queue is used when the skb transmit fails. skb will go
dd3fd72e 1583 * in the wait_queue which will be emptied by the timer function or
85b4aa49 1584 * by the next skb transmit.
34f42a07 1585 */
4bb6b515 1586static void fcoe_check_wait_queue(struct fc_lport *lp, struct sk_buff *skb)
85b4aa49 1587{
014f5c3f 1588 struct fcoe_port *port = lport_priv(lp);
4bb6b515 1589 int rc;
85b4aa49 1590
014f5c3f 1591 spin_lock_bh(&port->fcoe_pending_queue.lock);
4bb6b515
VD
1592
1593 if (skb)
014f5c3f 1594 __skb_queue_tail(&port->fcoe_pending_queue, skb);
4bb6b515 1595
014f5c3f 1596 if (port->fcoe_pending_queue_active)
c826a314 1597 goto out;
014f5c3f 1598 port->fcoe_pending_queue_active = 1;
55c8bafb 1599
014f5c3f 1600 while (port->fcoe_pending_queue.qlen) {
55c8bafb 1601 /* keep qlen > 0 until fcoe_start_io succeeds */
014f5c3f
CL
1602 port->fcoe_pending_queue.qlen++;
1603 skb = __skb_dequeue(&port->fcoe_pending_queue);
55c8bafb 1604
014f5c3f 1605 spin_unlock_bh(&port->fcoe_pending_queue.lock);
55c8bafb 1606 rc = fcoe_start_io(skb);
014f5c3f 1607 spin_lock_bh(&port->fcoe_pending_queue.lock);
55c8bafb
CL
1608
1609 if (rc) {
014f5c3f 1610 __skb_queue_head(&port->fcoe_pending_queue, skb);
55c8bafb 1611 /* undo temporary increment above */
014f5c3f 1612 port->fcoe_pending_queue.qlen--;
55c8bafb 1613 break;
85b4aa49 1614 }
55c8bafb 1615 /* undo temporary increment above */
014f5c3f 1616 port->fcoe_pending_queue.qlen--;
85b4aa49 1617 }
55c8bafb 1618
014f5c3f 1619 if (port->fcoe_pending_queue.qlen < FCOE_LOW_QUEUE_DEPTH)
55c8bafb 1620 lp->qfull = 0;
014f5c3f
CL
1621 if (port->fcoe_pending_queue.qlen && !timer_pending(&port->timer))
1622 mod_timer(&port->timer, jiffies + 2);
1623 port->fcoe_pending_queue_active = 0;
c826a314 1624out:
014f5c3f 1625 if (port->fcoe_pending_queue.qlen > FCOE_MAX_QUEUE_DEPTH)
4bb6b515 1626 lp->qfull = 1;
014f5c3f 1627 spin_unlock_bh(&port->fcoe_pending_queue.lock);
4bb6b515 1628 return;
85b4aa49
RL
1629}
1630
85b4aa49 1631/**
34f42a07
RL
1632 * fcoe_dev_setup() - setup link change notification interface
1633 */
b0d428ad 1634static void fcoe_dev_setup(void)
85b4aa49 1635{
85b4aa49
RL
1636 register_netdevice_notifier(&fcoe_notifier);
1637}
1638
1639/**
b0d428ad 1640 * fcoe_dev_cleanup() - cleanup link change notification interface
34f42a07 1641 */
85b4aa49
RL
1642static void fcoe_dev_cleanup(void)
1643{
1644 unregister_netdevice_notifier(&fcoe_notifier);
1645}
1646
1647/**
34f42a07 1648 * fcoe_device_notification() - netdev event notification callback
85b4aa49
RL
1649 * @notifier: context of the notification
1650 * @event: type of event
1651 * @ptr: fixed array for output parsed ifname
1652 *
1653 * This function is called by the ethernet driver in case of link change event
1654 *
1655 * Returns: 0 for success
34f42a07 1656 */
85b4aa49
RL
1657static int fcoe_device_notification(struct notifier_block *notifier,
1658 ulong event, void *ptr)
1659{
1660 struct fc_lport *lp = NULL;
1d1b88dc 1661 struct net_device *netdev = ptr;
014f5c3f 1662 struct fcoe_interface *fcoe;
2e70e241 1663 struct fcoe_port *port;
85b4aa49 1664 struct fcoe_dev_stats *stats;
97c8389d 1665 u32 link_possible = 1;
85b4aa49
RL
1666 u32 mfs;
1667 int rc = NOTIFY_OK;
1668
014f5c3f 1669 list_for_each_entry(fcoe, &fcoe_hostlist, list) {
25024989 1670 if (fcoe->netdev == netdev) {
3fe9a0ba 1671 lp = fcoe->ctlr.lp;
85b4aa49
RL
1672 break;
1673 }
1674 }
85b4aa49
RL
1675 if (lp == NULL) {
1676 rc = NOTIFY_DONE;
1677 goto out;
1678 }
1679
85b4aa49
RL
1680 switch (event) {
1681 case NETDEV_DOWN:
1682 case NETDEV_GOING_DOWN:
97c8389d 1683 link_possible = 0;
85b4aa49
RL
1684 break;
1685 case NETDEV_UP:
1686 case NETDEV_CHANGE:
85b4aa49
RL
1687 break;
1688 case NETDEV_CHANGEMTU:
7221d7e5
YZ
1689 if (netdev->features & NETIF_F_FCOE_MTU)
1690 break;
1d1b88dc
VD
1691 mfs = netdev->mtu - (sizeof(struct fcoe_hdr) +
1692 sizeof(struct fcoe_crc_eof));
85b4aa49
RL
1693 if (mfs >= FC_MIN_MAX_FRAME)
1694 fc_set_mfs(lp, mfs);
85b4aa49
RL
1695 break;
1696 case NETDEV_REGISTER:
1697 break;
2e70e241
CL
1698 case NETDEV_UNREGISTER:
1699 list_del(&fcoe->list);
1700 port = lport_priv(fcoe->ctlr.lp);
1701 fcoe_interface_cleanup(fcoe);
1702 schedule_work(&port->destroy_work);
1703 goto out;
1704 break;
85b4aa49 1705 default:
1d1b88dc 1706 FCOE_NETDEV_DBG(netdev, "Unknown event %ld "
d5488eb9 1707 "from netdev netlink\n", event);
85b4aa49 1708 }
97c8389d 1709 if (link_possible && !fcoe_link_ok(lp))
3fe9a0ba
CL
1710 fcoe_ctlr_link_up(&fcoe->ctlr);
1711 else if (fcoe_ctlr_link_down(&fcoe->ctlr)) {
97c8389d
JE
1712 stats = fc_lport_get_stats(lp);
1713 stats->LinkFailureCount++;
1714 fcoe_clean_pending_queue(lp);
85b4aa49
RL
1715 }
1716out:
1717 return rc;
1718}
1719
1720/**
34f42a07 1721 * fcoe_if_to_netdev() - parse a name buffer to get netdev
85b4aa49
RL
1722 * @buffer: incoming buffer to be copied
1723 *
dd3fd72e 1724 * Returns: NULL or ptr to net_device
34f42a07 1725 */
85b4aa49
RL
1726static struct net_device *fcoe_if_to_netdev(const char *buffer)
1727{
1728 char *cp;
1729 char ifname[IFNAMSIZ + 2];
1730
1731 if (buffer) {
1732 strlcpy(ifname, buffer, IFNAMSIZ);
1733 cp = ifname + strlen(ifname);
1734 while (--cp >= ifname && *cp == '\n')
1735 *cp = '\0';
1736 return dev_get_by_name(&init_net, ifname);
1737 }
1738 return NULL;
1739}
1740
85b4aa49 1741/**
34f42a07 1742 * fcoe_destroy() - handles the destroy from sysfs
dd3fd72e 1743 * @buffer: expected to be an eth if name
85b4aa49
RL
1744 * @kp: associated kernel param
1745 *
1746 * Returns: 0 for success
34f42a07 1747 */
85b4aa49
RL
1748static int fcoe_destroy(const char *buffer, struct kernel_param *kp)
1749{
030f4e00 1750 struct fcoe_interface *fcoe;
2e70e241 1751 struct net_device *netdev;
8eca355f 1752 int rc = 0;
85b4aa49 1753
dfc1d0fe
CL
1754 mutex_lock(&fcoe_config_mutex);
1755#ifdef CONFIG_FCOE_MODULE
1756 /*
1757 * Make sure the module has been initialized, and is not about to be
1758 * removed. Module paramter sysfs files are writable before the
1759 * module_init function is called and after module_exit.
1760 */
1761 if (THIS_MODULE->state != MODULE_STATE_LIVE) {
1762 rc = -ENODEV;
1763 goto out_nodev;
1764 }
1765#endif
1766
85b4aa49
RL
1767 netdev = fcoe_if_to_netdev(buffer);
1768 if (!netdev) {
1769 rc = -ENODEV;
1770 goto out_nodev;
1771 }
2e70e241 1772
090eb6c4 1773 rtnl_lock();
2e70e241
CL
1774 fcoe = fcoe_hostlist_lookup_port(netdev);
1775 if (!fcoe) {
090eb6c4 1776 rtnl_unlock();
85b4aa49
RL
1777 rc = -ENODEV;
1778 goto out_putdev;
1779 }
2e70e241 1780 list_del(&fcoe->list);
2e70e241
CL
1781 fcoe_interface_cleanup(fcoe);
1782 rtnl_unlock();
1783 fcoe_if_destroy(fcoe->ctlr.lp);
85b4aa49
RL
1784out_putdev:
1785 dev_put(netdev);
1786out_nodev:
dfc1d0fe 1787 mutex_unlock(&fcoe_config_mutex);
85b4aa49
RL
1788 return rc;
1789}
1790
2e70e241
CL
1791static void fcoe_destroy_work(struct work_struct *work)
1792{
1793 struct fcoe_port *port;
1794
1795 port = container_of(work, struct fcoe_port, destroy_work);
1796 mutex_lock(&fcoe_config_mutex);
1797 fcoe_if_destroy(port->lport);
1798 mutex_unlock(&fcoe_config_mutex);
1799}
1800
85b4aa49 1801/**
34f42a07 1802 * fcoe_create() - Handles the create call from sysfs
dd3fd72e 1803 * @buffer: expected to be an eth if name
85b4aa49
RL
1804 * @kp: associated kernel param
1805 *
1806 * Returns: 0 for success
34f42a07 1807 */
85b4aa49
RL
1808static int fcoe_create(const char *buffer, struct kernel_param *kp)
1809{
1810 int rc;
030f4e00 1811 struct fcoe_interface *fcoe;
af7f85d9 1812 struct fc_lport *lport;
85b4aa49
RL
1813 struct net_device *netdev;
1814
dfc1d0fe
CL
1815 mutex_lock(&fcoe_config_mutex);
1816#ifdef CONFIG_FCOE_MODULE
1817 /*
1818 * Make sure the module has been initialized, and is not about to be
1819 * removed. Module paramter sysfs files are writable before the
1820 * module_init function is called and after module_exit.
1821 */
1822 if (THIS_MODULE->state != MODULE_STATE_LIVE) {
1823 rc = -ENODEV;
1824 goto out_nodev;
1825 }
1826#endif
1827
2e70e241 1828 rtnl_lock();
85b4aa49
RL
1829 netdev = fcoe_if_to_netdev(buffer);
1830 if (!netdev) {
1831 rc = -ENODEV;
1832 goto out_nodev;
1833 }
2e70e241 1834
85b4aa49
RL
1835 /* look for existing lport */
1836 if (fcoe_hostlist_lookup(netdev)) {
1837 rc = -EEXIST;
1838 goto out_putdev;
1839 }
85b4aa49 1840
030f4e00
CL
1841 fcoe = fcoe_interface_create(netdev);
1842 if (!fcoe) {
1843 rc = -ENOMEM;
1844 goto out_putdev;
1845 }
1846
9a05753b 1847 lport = fcoe_if_create(fcoe, &netdev->dev, 0);
af7f85d9 1848 if (IS_ERR(lport)) {
d5488eb9 1849 printk(KERN_ERR "fcoe: Failed to create interface (%s)\n",
85b4aa49 1850 netdev->name);
85b4aa49 1851 rc = -EIO;
2e70e241 1852 fcoe_interface_cleanup(fcoe);
030f4e00 1853 goto out_free;
85b4aa49 1854 }
030f4e00 1855
54b649f8
CL
1856 /* Make this the "master" N_Port */
1857 fcoe->ctlr.lp = lport;
1858
c863df33
CL
1859 /* add to lports list */
1860 fcoe_hostlist_add(lport);
1861
54b649f8
CL
1862 /* start FIP Discovery and FLOGI */
1863 lport->boot_time = jiffies;
1864 fc_fabric_login(lport);
1865 if (!fcoe_link_ok(lport))
1866 fcoe_ctlr_link_up(&fcoe->ctlr);
030f4e00 1867
54b649f8 1868 rc = 0;
030f4e00 1869out_free:
54b649f8
CL
1870 /*
1871 * Release from init in fcoe_interface_create(), on success lport
1872 * should be holding a reference taken in fcoe_if_create().
1873 */
030f4e00 1874 fcoe_interface_put(fcoe);
85b4aa49
RL
1875out_putdev:
1876 dev_put(netdev);
1877out_nodev:
2e70e241 1878 rtnl_unlock();
dfc1d0fe 1879 mutex_unlock(&fcoe_config_mutex);
85b4aa49
RL
1880 return rc;
1881}
1882
1883module_param_call(create, fcoe_create, NULL, NULL, S_IWUSR);
1884__MODULE_PARM_TYPE(create, "string");
014f5c3f 1885MODULE_PARM_DESC(create, "Create fcoe fcoe using net device passed in.");
85b4aa49
RL
1886module_param_call(destroy, fcoe_destroy, NULL, NULL, S_IWUSR);
1887__MODULE_PARM_TYPE(destroy, "string");
014f5c3f 1888MODULE_PARM_DESC(destroy, "Destroy fcoe fcoe");
85b4aa49 1889
34f42a07
RL
1890/**
1891 * fcoe_link_ok() - Check if link is ok for the fc_lport
85b4aa49
RL
1892 * @lp: ptr to the fc_lport
1893 *
1894 * Any permanently-disqualifying conditions have been previously checked.
1895 * This also updates the speed setting, which may change with link for 100/1000.
1896 *
1897 * This function should probably be checking for PAUSE support at some point
1898 * in the future. Currently Per-priority-pause is not determinable using
1899 * ethtool, so we shouldn't be restrictive until that problem is resolved.
1900 *
1901 * Returns: 0 if link is OK for use by FCoE.
1902 *
1903 */
1904int fcoe_link_ok(struct fc_lport *lp)
1905{
014f5c3f 1906 struct fcoe_port *port = lport_priv(lp);
25024989 1907 struct net_device *dev = port->fcoe->netdev;
85b4aa49 1908 struct ethtool_cmd ecmd = { ETHTOOL_GSET };
85b4aa49 1909
2f718d64
YZ
1910 if ((dev->flags & IFF_UP) && netif_carrier_ok(dev) &&
1911 (!dev_ethtool_get_settings(dev, &ecmd))) {
1912 lp->link_supported_speeds &=
1913 ~(FC_PORTSPEED_1GBIT | FC_PORTSPEED_10GBIT);
1914 if (ecmd.supported & (SUPPORTED_1000baseT_Half |
1915 SUPPORTED_1000baseT_Full))
1916 lp->link_supported_speeds |= FC_PORTSPEED_1GBIT;
1917 if (ecmd.supported & SUPPORTED_10000baseT_Full)
1918 lp->link_supported_speeds |=
1919 FC_PORTSPEED_10GBIT;
1920 if (ecmd.speed == SPEED_1000)
1921 lp->link_speed = FC_PORTSPEED_1GBIT;
1922 if (ecmd.speed == SPEED_10000)
1923 lp->link_speed = FC_PORTSPEED_10GBIT;
85b4aa49 1924
2f718d64
YZ
1925 return 0;
1926 }
1927 return -1;
85b4aa49 1928}
85b4aa49 1929
34f42a07
RL
1930/**
1931 * fcoe_percpu_clean() - Clear the pending skbs for an lport
85b4aa49 1932 * @lp: the fc_lport
e7a51997
JE
1933 *
1934 * Must be called with fcoe_create_mutex held to single-thread completion.
1935 *
1936 * This flushes the pending skbs by adding a new skb to each queue and
1937 * waiting until they are all freed. This assures us that not only are
1938 * there no packets that will be handled by the lport, but also that any
1939 * threads already handling packet have returned.
85b4aa49
RL
1940 */
1941void fcoe_percpu_clean(struct fc_lport *lp)
1942{
85b4aa49
RL
1943 struct fcoe_percpu_s *pp;
1944 struct fcoe_rcv_info *fr;
1945 struct sk_buff_head *list;
1946 struct sk_buff *skb, *next;
1947 struct sk_buff *head;
5e5e92df 1948 unsigned int cpu;
85b4aa49 1949
5e5e92df
RL
1950 for_each_possible_cpu(cpu) {
1951 pp = &per_cpu(fcoe_percpu, cpu);
1952 spin_lock_bh(&pp->fcoe_rx_list.lock);
1953 list = &pp->fcoe_rx_list;
1954 head = list->next;
1955 for (skb = head; skb != (struct sk_buff *)list;
1956 skb = next) {
1957 next = skb->next;
1958 fr = fcoe_dev_from_skb(skb);
1959 if (fr->fr_dev == lp) {
1960 __skb_unlink(skb, list);
1961 kfree_skb(skb);
85b4aa49 1962 }
85b4aa49 1963 }
e7a51997
JE
1964
1965 if (!pp->thread || !cpu_online(cpu)) {
1966 spin_unlock_bh(&pp->fcoe_rx_list.lock);
1967 continue;
1968 }
1969
1970 skb = dev_alloc_skb(0);
1971 if (!skb) {
1972 spin_unlock_bh(&pp->fcoe_rx_list.lock);
1973 continue;
1974 }
1975 skb->destructor = fcoe_percpu_flush_done;
1976
1977 __skb_queue_tail(&pp->fcoe_rx_list, skb);
1978 if (pp->fcoe_rx_list.qlen == 1)
1979 wake_up_process(pp->thread);
5e5e92df 1980 spin_unlock_bh(&pp->fcoe_rx_list.lock);
e7a51997
JE
1981
1982 wait_for_completion(&fcoe_flush_completion);
85b4aa49
RL
1983 }
1984}
85b4aa49
RL
1985
1986/**
34f42a07 1987 * fcoe_clean_pending_queue() - Dequeue a skb and free it
85b4aa49
RL
1988 * @lp: the corresponding fc_lport
1989 *
1990 * Returns: none
34f42a07 1991 */
85b4aa49
RL
1992void fcoe_clean_pending_queue(struct fc_lport *lp)
1993{
014f5c3f 1994 struct fcoe_port *port = lport_priv(lp);
85b4aa49
RL
1995 struct sk_buff *skb;
1996
014f5c3f
CL
1997 spin_lock_bh(&port->fcoe_pending_queue.lock);
1998 while ((skb = __skb_dequeue(&port->fcoe_pending_queue)) != NULL) {
1999 spin_unlock_bh(&port->fcoe_pending_queue.lock);
85b4aa49 2000 kfree_skb(skb);
014f5c3f 2001 spin_lock_bh(&port->fcoe_pending_queue.lock);
85b4aa49 2002 }
014f5c3f 2003 spin_unlock_bh(&port->fcoe_pending_queue.lock);
85b4aa49 2004}
85b4aa49 2005
34f42a07
RL
2006/**
2007 * fcoe_reset() - Resets the fcoe
85b4aa49
RL
2008 * @shost: shost the reset is from
2009 *
2010 * Returns: always 0
2011 */
2012int fcoe_reset(struct Scsi_Host *shost)
2013{
2014 struct fc_lport *lport = shost_priv(shost);
2015 fc_lport_reset(lport);
2016 return 0;
2017}
85b4aa49 2018
34f42a07 2019/**
014f5c3f 2020 * fcoe_hostlist_lookup_port() - find the corresponding lport by a given device
dd3fd72e 2021 * @dev: this is currently ptr to net_device
85b4aa49 2022 *
014f5c3f 2023 * Returns: NULL or the located fcoe_port
090eb6c4 2024 * Locking: must be called with the RNL mutex held
85b4aa49 2025 */
014f5c3f
CL
2026static struct fcoe_interface *
2027fcoe_hostlist_lookup_port(const struct net_device *dev)
85b4aa49 2028{
014f5c3f 2029 struct fcoe_interface *fcoe;
85b4aa49 2030
014f5c3f 2031 list_for_each_entry(fcoe, &fcoe_hostlist, list) {
25024989 2032 if (fcoe->netdev == dev)
014f5c3f 2033 return fcoe;
85b4aa49 2034 }
85b4aa49
RL
2035 return NULL;
2036}
2037
34f42a07
RL
2038/**
2039 * fcoe_hostlist_lookup() - Find the corresponding lport by netdev
85b4aa49
RL
2040 * @netdev: ptr to net_device
2041 *
2042 * Returns: 0 for success
090eb6c4 2043 * Locking: must be called with the RTNL mutex held
85b4aa49 2044 */
090eb6c4 2045static struct fc_lport *fcoe_hostlist_lookup(const struct net_device *netdev)
85b4aa49 2046{
014f5c3f 2047 struct fcoe_interface *fcoe;
85b4aa49 2048
014f5c3f 2049 fcoe = fcoe_hostlist_lookup_port(netdev);
3fe9a0ba 2050 return (fcoe) ? fcoe->ctlr.lp : NULL;
85b4aa49 2051}
85b4aa49 2052
34f42a07
RL
2053/**
2054 * fcoe_hostlist_add() - Add a lport to lports list
dd3fd72e 2055 * @lp: ptr to the fc_lport to be added
85b4aa49
RL
2056 *
2057 * Returns: 0 for success
090eb6c4 2058 * Locking: must be called with the RTNL mutex held
85b4aa49 2059 */
090eb6c4 2060static int fcoe_hostlist_add(const struct fc_lport *lport)
85b4aa49 2061{
014f5c3f
CL
2062 struct fcoe_interface *fcoe;
2063 struct fcoe_port *port;
2064
2065 fcoe = fcoe_hostlist_lookup_port(fcoe_netdev(lport));
2066 if (!fcoe) {
2067 port = lport_priv(lport);
2068 fcoe = port->fcoe;
2069 list_add_tail(&fcoe->list, &fcoe_hostlist);
85b4aa49
RL
2070 }
2071 return 0;
2072}
85b4aa49 2073
85b4aa49 2074/**
34f42a07 2075 * fcoe_init() - fcoe module loading initialization
85b4aa49 2076 *
85b4aa49 2077 * Returns 0 on success, negative on failure
34f42a07 2078 */
85b4aa49
RL
2079static int __init fcoe_init(void)
2080{
38eccabd 2081 unsigned int cpu;
8976f424 2082 int rc = 0;
85b4aa49
RL
2083 struct fcoe_percpu_s *p;
2084
dfc1d0fe
CL
2085 mutex_lock(&fcoe_config_mutex);
2086
38eccabd 2087 for_each_possible_cpu(cpu) {
5e5e92df 2088 p = &per_cpu(fcoe_percpu, cpu);
38eccabd
RL
2089 skb_queue_head_init(&p->fcoe_rx_list);
2090 }
2091
8976f424
RL
2092 for_each_online_cpu(cpu)
2093 fcoe_percpu_thread_create(cpu);
85b4aa49 2094
8976f424
RL
2095 /* Initialize per CPU interrupt thread */
2096 rc = register_hotcpu_notifier(&fcoe_cpu_notifier);
2097 if (rc)
2098 goto out_free;
85b4aa49 2099
8976f424 2100 /* Setup link change notification */
85b4aa49
RL
2101 fcoe_dev_setup();
2102
5892c32f
CL
2103 rc = fcoe_if_init();
2104 if (rc)
2105 goto out_free;
85b4aa49 2106
dfc1d0fe 2107 mutex_unlock(&fcoe_config_mutex);
85b4aa49 2108 return 0;
8976f424
RL
2109
2110out_free:
2111 for_each_online_cpu(cpu) {
2112 fcoe_percpu_thread_destroy(cpu);
2113 }
dfc1d0fe 2114 mutex_unlock(&fcoe_config_mutex);
8976f424 2115 return rc;
85b4aa49
RL
2116}
2117module_init(fcoe_init);
2118
2119/**
34f42a07 2120 * fcoe_exit() - fcoe module unloading cleanup
85b4aa49
RL
2121 *
2122 * Returns 0 on success, negative on failure
34f42a07 2123 */
85b4aa49
RL
2124static void __exit fcoe_exit(void)
2125{
5e5e92df 2126 unsigned int cpu;
014f5c3f 2127 struct fcoe_interface *fcoe, *tmp;
2e70e241 2128 struct fcoe_port *port;
85b4aa49 2129
dfc1d0fe
CL
2130 mutex_lock(&fcoe_config_mutex);
2131
85b4aa49
RL
2132 fcoe_dev_cleanup();
2133
5919a595 2134 /* releases the associated fcoe hosts */
090eb6c4
CL
2135 rtnl_lock();
2136 list_for_each_entry_safe(fcoe, tmp, &fcoe_hostlist, list) {
c863df33 2137 list_del(&fcoe->list);
2e70e241 2138 port = lport_priv(fcoe->ctlr.lp);
2e70e241 2139 fcoe_interface_cleanup(fcoe);
2e70e241 2140 schedule_work(&port->destroy_work);
c863df33 2141 }
090eb6c4 2142 rtnl_unlock();
85b4aa49 2143
8976f424
RL
2144 unregister_hotcpu_notifier(&fcoe_cpu_notifier);
2145
014f5c3f 2146 for_each_online_cpu(cpu)
8976f424 2147 fcoe_percpu_thread_destroy(cpu);
85b4aa49 2148
dfc1d0fe 2149 mutex_unlock(&fcoe_config_mutex);
2e70e241
CL
2150
2151 /* flush any asyncronous interface destroys,
2152 * this should happen after the netdev notifier is unregistered */
2153 flush_scheduled_work();
9a05753b
CL
2154 /* That will flush out all the N_Ports on the hostlist, but now we
2155 * may have NPIV VN_Ports scheduled for destruction */
2156 flush_scheduled_work();
2e70e241
CL
2157
2158 /* detach from scsi transport
2159 * must happen after all destroys are done, therefor after the flush */
2160 fcoe_if_exit();
85b4aa49
RL
2161}
2162module_exit(fcoe_exit);
11b56188
CL
2163
2164/**
2165 * fcoe_flogi_resp() - FCoE specific FLOGI and FDISC response handler
2166 * @seq: active sequence in the FLOGI or FDISC exchange
2167 * @fp: response frame, or error encoded in a pointer (timeout)
2168 * @arg: pointer the the fcoe_ctlr structure
2169 *
2170 * This handles MAC address managment for FCoE, then passes control on to
2171 * the libfc FLOGI response handler.
2172 */
2173static void fcoe_flogi_resp(struct fc_seq *seq, struct fc_frame *fp, void *arg)
2174{
2175 struct fcoe_ctlr *fip = arg;
2176 struct fc_exch *exch = fc_seq_exch(seq);
2177 struct fc_lport *lport = exch->lp;
2178 u8 *mac;
2179
2180 if (IS_ERR(fp))
2181 goto done;
2182
2183 mac = fr_cb(fp)->granted_mac;
2184 if (is_zero_ether_addr(mac)) {
2185 /* pre-FIP */
2186 mac = eth_hdr(&fp->skb)->h_source;
2187 if (fcoe_ctlr_recv_flogi(fip, lport, fp, mac)) {
2188 fc_frame_free(fp);
2189 return;
2190 }
2191 } else {
2192 /* FIP, libfcoe has already seen it */
2193 fip->update_mac(lport, fr_cb(fp)->granted_mac);
2194 }
2195done:
2196 fc_lport_flogi_resp(seq, fp, lport);
2197}
2198
2199/**
2200 * fcoe_logo_resp() - FCoE specific LOGO response handler
2201 * @seq: active sequence in the LOGO exchange
2202 * @fp: response frame, or error encoded in a pointer (timeout)
2203 * @arg: pointer the the fcoe_ctlr structure
2204 *
2205 * This handles MAC address managment for FCoE, then passes control on to
2206 * the libfc LOGO response handler.
2207 */
2208static void fcoe_logo_resp(struct fc_seq *seq, struct fc_frame *fp, void *arg)
2209{
2210 struct fcoe_ctlr *fip = arg;
2211 struct fc_exch *exch = fc_seq_exch(seq);
2212 struct fc_lport *lport = exch->lp;
2213 static u8 zero_mac[ETH_ALEN] = { 0 };
2214
2215 if (!IS_ERR(fp))
2216 fip->update_mac(lport, zero_mac);
2217 fc_lport_logo_resp(seq, fp, lport);
2218}
2219
2220/**
2221 * fcoe_elsct_send - FCoE specific ELS handler
2222 *
2223 * This does special case handling of FIP encapsualted ELS exchanges for FCoE,
2224 * using FCoE specific response handlers and passing the FIP controller as
2225 * the argument (the lport is still available from the exchange).
2226 *
2227 * Most of the work here is just handed off to the libfc routine.
2228 */
2229static struct fc_seq *fcoe_elsct_send(struct fc_lport *lport,
2230 u32 did, struct fc_frame *fp, unsigned int op,
2231 void (*resp)(struct fc_seq *, struct fc_frame *, void *),
2232 void *arg, u32 timeout)
2233{
2234 struct fcoe_port *port = lport_priv(lport);
2235 struct fcoe_interface *fcoe = port->fcoe;
2236 struct fcoe_ctlr *fip = &fcoe->ctlr;
2237 struct fc_frame_header *fh = fc_frame_header_get(fp);
2238
2239 switch (op) {
2240 case ELS_FLOGI:
2241 case ELS_FDISC:
2242 return fc_elsct_send(lport, did, fp, op, fcoe_flogi_resp,
2243 fip, timeout);
2244 case ELS_LOGO:
2245 /* only hook onto fabric logouts, not port logouts */
2246 if (ntoh24(fh->fh_d_id) != FC_FID_FLOGI)
2247 break;
2248 return fc_elsct_send(lport, did, fp, op, fcoe_logo_resp,
2249 fip, timeout);
2250 }
2251 return fc_elsct_send(lport, did, fp, op, resp, arg, timeout);
2252}
2253
9a05753b
CL
2254/**
2255 * fcoe_vport_create() - create an fc_host/scsi_host for a vport
2256 * @vport: fc_vport object to create a new fc_host for
2257 * @disabled: start the new fc_host in a disabled state by default?
2258 *
2259 * Returns: 0 for success
2260 */
2261static int fcoe_vport_create(struct fc_vport *vport, bool disabled)
2262{
2263 struct Scsi_Host *shost = vport_to_shost(vport);
2264 struct fc_lport *n_port = shost_priv(shost);
2265 struct fcoe_port *port = lport_priv(n_port);
2266 struct fcoe_interface *fcoe = port->fcoe;
2267 struct net_device *netdev = fcoe->netdev;
2268 struct fc_lport *vn_port;
2269
2270 mutex_lock(&fcoe_config_mutex);
2271 vn_port = fcoe_if_create(fcoe, &vport->dev, 1);
2272 mutex_unlock(&fcoe_config_mutex);
2273
2274 if (IS_ERR(vn_port)) {
2275 printk(KERN_ERR "fcoe: fcoe_vport_create(%s) failed\n",
2276 netdev->name);
2277 return -EIO;
2278 }
2279
2280 if (disabled) {
2281 fc_vport_set_state(vport, FC_VPORT_DISABLED);
2282 } else {
2283 vn_port->boot_time = jiffies;
2284 fc_fabric_login(vn_port);
2285 fc_vport_setlink(vn_port);
2286 }
2287 return 0;
2288}
2289
2290/**
2291 * fcoe_vport_destroy() - destroy the fc_host/scsi_host for a vport
2292 * @vport: fc_vport object that is being destroyed
2293 *
2294 * Returns: 0 for success
2295 */
2296static int fcoe_vport_destroy(struct fc_vport *vport)
2297{
2298 struct Scsi_Host *shost = vport_to_shost(vport);
2299 struct fc_lport *n_port = shost_priv(shost);
2300 struct fc_lport *vn_port = vport->dd_data;
2301 struct fcoe_port *port = lport_priv(vn_port);
2302
2303 mutex_lock(&n_port->lp_mutex);
2304 list_del(&vn_port->list);
2305 mutex_unlock(&n_port->lp_mutex);
2306 schedule_work(&port->destroy_work);
2307 return 0;
2308}
2309
2310/**
2311 * fcoe_vport_disable() - change vport state
2312 * @vport: vport to bring online/offline
2313 * @disable: should the vport be disabled?
2314 */
2315static int fcoe_vport_disable(struct fc_vport *vport, bool disable)
2316{
2317 struct fc_lport *lport = vport->dd_data;
2318
2319 if (disable) {
2320 fc_vport_set_state(vport, FC_VPORT_DISABLED);
2321 fc_fabric_logoff(lport);
2322 } else {
2323 lport->boot_time = jiffies;
2324 fc_fabric_login(lport);
2325 fc_vport_setlink(lport);
2326 }
2327
2328 return 0;
2329}
2330
dc8596d3
CL
2331/**
2332 * fcoe_vport_set_symbolic_name() - append vport string to symbolic name
2333 * @vport: fc_vport with a new symbolic name string
2334 *
2335 * After generating a new symbolic name string, a new RSPN_ID request is
2336 * sent to the name server. There is no response handler, so if it fails
2337 * for some reason it will not be retried.
2338 */
2339static void fcoe_set_vport_symbolic_name(struct fc_vport *vport)
2340{
2341 struct fc_lport *lport = vport->dd_data;
2342 struct fc_frame *fp;
2343 size_t len;
2344
2345 snprintf(fc_host_symbolic_name(lport->host), FC_SYMBOLIC_NAME_SIZE,
2346 "%s v%s over %s : %s", FCOE_NAME, FCOE_VERSION,
2347 fcoe_netdev(lport)->name, vport->symbolic_name);
2348
2349 if (lport->state != LPORT_ST_READY)
2350 return;
2351
2352 len = strnlen(fc_host_symbolic_name(lport->host), 255);
2353 fp = fc_frame_alloc(lport,
2354 sizeof(struct fc_ct_hdr) +
2355 sizeof(struct fc_ns_rspn) + len);
2356 if (!fp)
2357 return;
2358 lport->tt.elsct_send(lport, FC_FID_DIR_SERV, fp, FC_NS_RSPN_ID,
2359 NULL, NULL, lport->e_d_tov);
2360}
2361