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