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