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