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