blob: 63aeeca384bac542f23efa3dde854fac73f80239 [file] [log] [blame]
Robert Love85b4aa42008-12-09 15:10:24 -08001/*
Chris Leechaf7f85d2009-08-25 13:59:24 -07002 * Copyright(c) 2007 - 2009 Intel Corporation. All rights reserved.
Robert Love85b4aa42008-12-09 15:10:24 -08003 *
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>
Robert Love85b4aa42008-12-09 15:10:24 -080022#include <linux/spinlock.h>
Robert Love85b4aa42008-12-09 15:10:24 -080023#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>
Robert Love85b4aa42008-12-09 15:10:24 -080028#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>
Joe Eykholt97c83892009-03-17 11:42:40 -070040#include <scsi/fc/fc_fip.h>
Robert Love85b4aa42008-12-09 15:10:24 -080041
42#include <scsi/libfc.h>
43#include <scsi/fc_frame.h>
44#include <scsi/libfcoe.h>
Robert Love85b4aa42008-12-09 15:10:24 -080045
Vasu Devfdd78022009-03-17 11:42:24 -070046#include "fcoe.h"
47
Robert Love85b4aa42008-12-09 15:10:24 -080048MODULE_AUTHOR("Open-FCoE.org");
49MODULE_DESCRIPTION("FCoE");
Vasu Dev9b34ecf2009-03-17 11:42:13 -070050MODULE_LICENSE("GPL v2");
Robert Love85b4aa42008-12-09 15:10:24 -080051
Yi Zou05cc7392009-08-25 13:59:03 -070052/* 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
Robert Love85b4aa42008-12-09 15:10:24 -080058/* fcoe host list */
59LIST_HEAD(fcoe_hostlist);
60DEFINE_RWLOCK(fcoe_hostlist_lock);
Robert Love5e5e92d2009-03-17 11:41:35 -070061DEFINE_PER_CPU(struct fcoe_percpu_s, fcoe_percpu);
Robert Love85b4aa42008-12-09 15:10:24 -080062
Chris Leechdd3fd722009-04-21 16:27:36 -070063/* Function Prototypes */
Vasu Devfdd78022009-03-17 11:42:24 -070064static 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
Vasu Dev4bb6b512009-05-06 10:52:34 -070077static void fcoe_check_wait_queue(struct fc_lport *, struct sk_buff *);
Robert Love85b4aa42008-12-09 15:10:24 -080078static 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
Vasu Dev7f349142009-03-27 09:06:31 -070087static 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
Chris Leech54b649f2009-08-25 14:00:07 -0700141static int fcoe_fip_recv(struct sk_buff *skb, struct net_device *dev,
142 struct packet_type *ptype,
143 struct net_device *orig_dev);
144/**
145 * fcoe_interface_setup()
146 * @fcoe: new fcoe_interface
147 * @netdev : ptr to the associated netdevice struct
148 *
149 * Returns : 0 for success
150 */
151static int fcoe_interface_setup(struct fcoe_interface *fcoe,
152 struct net_device *netdev)
153{
154 struct fcoe_ctlr *fip = &fcoe->ctlr;
155 struct netdev_hw_addr *ha;
156 u8 flogi_maddr[ETH_ALEN];
157
158 fcoe->netdev = netdev;
159
160 /* Do not support for bonding device */
161 if ((netdev->priv_flags & IFF_MASTER_ALB) ||
162 (netdev->priv_flags & IFF_SLAVE_INACTIVE) ||
163 (netdev->priv_flags & IFF_MASTER_8023AD)) {
164 return -EOPNOTSUPP;
165 }
166
167 /* look for SAN MAC address, if multiple SAN MACs exist, only
168 * use the first one for SPMA */
169 rcu_read_lock();
170 for_each_dev_addr(netdev, ha) {
171 if ((ha->type == NETDEV_HW_ADDR_T_SAN) &&
172 (is_valid_ether_addr(fip->ctl_src_addr))) {
173 memcpy(fip->ctl_src_addr, ha->addr, ETH_ALEN);
174 fip->spma = 1;
175 break;
176 }
177 }
178 rcu_read_unlock();
179
180 /* setup Source Mac Address */
181 if (!fip->spma)
182 memcpy(fip->ctl_src_addr, netdev->dev_addr, netdev->addr_len);
183
184 /*
185 * Add FCoE MAC address as second unicast MAC address
186 * or enter promiscuous mode if not capable of listening
187 * for multiple unicast MACs.
188 */
189 rtnl_lock();
190 memcpy(flogi_maddr, (u8[6]) FC_FCOE_FLOGI_MAC, ETH_ALEN);
191 dev_unicast_add(netdev, flogi_maddr);
192 if (fip->spma)
193 dev_unicast_add(netdev, fip->ctl_src_addr);
194 dev_mc_add(netdev, FIP_ALL_ENODE_MACS, ETH_ALEN, 0);
195 rtnl_unlock();
196
197 /*
198 * setup the receive function from ethernet driver
199 * on the ethertype for the given device
200 */
201 fcoe->fcoe_packet_type.func = fcoe_rcv;
202 fcoe->fcoe_packet_type.type = __constant_htons(ETH_P_FCOE);
203 fcoe->fcoe_packet_type.dev = netdev;
204 dev_add_pack(&fcoe->fcoe_packet_type);
205
206 fcoe->fip_packet_type.func = fcoe_fip_recv;
207 fcoe->fip_packet_type.type = htons(ETH_P_FIP);
208 fcoe->fip_packet_type.dev = netdev;
209 dev_add_pack(&fcoe->fip_packet_type);
210
211 return 0;
212}
213
214static void fcoe_fip_send(struct fcoe_ctlr *fip, struct sk_buff *skb);
215static void fcoe_update_src_mac(struct fcoe_ctlr *fip, u8 *old, u8 *new);
216
Vasu Dev7f349142009-03-27 09:06:31 -0700217/**
Chris Leech030f4e02009-08-25 14:00:02 -0700218 * fcoe_interface_create()
219 * @netdev: network interface
220 *
221 * Returns: pointer to a struct fcoe_interface or NULL on error
222 */
223static struct fcoe_interface *fcoe_interface_create(struct net_device *netdev)
224{
225 struct fcoe_interface *fcoe;
226
227 fcoe = kzalloc(sizeof(*fcoe), GFP_KERNEL);
228 if (!fcoe) {
229 FCOE_NETDEV_DBG(netdev, "Could not allocate fcoe structure\n");
230 return NULL;
231 }
232
233 kref_init(&fcoe->kref);
Chris Leech54b649f2009-08-25 14:00:07 -0700234
235 /*
236 * Initialize FIP.
237 */
238 fcoe_ctlr_init(&fcoe->ctlr);
239 fcoe->ctlr.send = fcoe_fip_send;
240 fcoe->ctlr.update_mac = fcoe_update_src_mac;
241
242 fcoe_interface_setup(fcoe, netdev);
Chris Leech030f4e02009-08-25 14:00:02 -0700243
244 return fcoe;
245}
246
247/**
Chris Leech54b649f2009-08-25 14:00:07 -0700248 * fcoe_interface_cleanup() - clean up netdev configurations
249 * @fcoe:
250 */
251void fcoe_interface_cleanup(struct fcoe_interface *fcoe)
252{
253 struct net_device *netdev = fcoe->netdev;
254 struct fcoe_ctlr *fip = &fcoe->ctlr;
255 u8 flogi_maddr[ETH_ALEN];
256
257 /*
258 * Don't listen for Ethernet packets anymore.
259 * synchronize_net() ensures that the packet handlers are not running
260 * on another CPU. dev_remove_pack() would do that, this calls the
261 * unsyncronized version __dev_remove_pack() to avoid multiple delays.
262 */
263 __dev_remove_pack(&fcoe->fcoe_packet_type);
264 __dev_remove_pack(&fcoe->fip_packet_type);
265 synchronize_net();
266
267 /* tear-down the FCoE controller */
268 fcoe_ctlr_destroy(&fcoe->ctlr);
269
270 /* Delete secondary MAC addresses */
271 rtnl_lock();
272 memcpy(flogi_maddr, (u8[6]) FC_FCOE_FLOGI_MAC, ETH_ALEN);
273 dev_unicast_delete(netdev, flogi_maddr);
274 if (!is_zero_ether_addr(fip->data_src_addr))
275 dev_unicast_delete(netdev, fip->data_src_addr);
276 if (fip->spma)
277 dev_unicast_delete(netdev, fip->ctl_src_addr);
278 dev_mc_delete(netdev, FIP_ALL_ENODE_MACS, ETH_ALEN, 0);
279 rtnl_unlock();
280}
281
282/**
Chris Leech030f4e02009-08-25 14:00:02 -0700283 * fcoe_interface_release() - fcoe_port kref release function
284 * @kref: embedded reference count in an fcoe_interface struct
285 */
286static void fcoe_interface_release(struct kref *kref)
287{
288 struct fcoe_interface *fcoe;
289
290 fcoe = container_of(kref, struct fcoe_interface, kref);
Chris Leech54b649f2009-08-25 14:00:07 -0700291 fcoe_interface_cleanup(fcoe);
Chris Leech030f4e02009-08-25 14:00:02 -0700292 kfree(fcoe);
293}
294
295/**
296 * fcoe_interface_get()
297 * @fcoe:
298 */
299static inline void fcoe_interface_get(struct fcoe_interface *fcoe)
300{
301 kref_get(&fcoe->kref);
302}
303
304/**
305 * fcoe_interface_put()
306 * @fcoe:
307 */
308static inline void fcoe_interface_put(struct fcoe_interface *fcoe)
309{
310 kref_put(&fcoe->kref, fcoe_interface_release);
311}
312
313/**
Vasu Devab6b85c2009-05-17 12:33:08 +0000314 * fcoe_fip_recv - handle a received FIP frame.
315 * @skb: the receive skb
316 * @dev: associated &net_device
317 * @ptype: the &packet_type structure which was used to register this handler.
318 * @orig_dev: original receive &net_device, in case @dev is a bond.
319 *
320 * Returns: 0 for success
321 */
322static int fcoe_fip_recv(struct sk_buff *skb, struct net_device *dev,
323 struct packet_type *ptype,
324 struct net_device *orig_dev)
325{
Chris Leech259ad852009-08-25 13:59:41 -0700326 struct fcoe_interface *fcoe;
Vasu Devab6b85c2009-05-17 12:33:08 +0000327
Chris Leech259ad852009-08-25 13:59:41 -0700328 fcoe = container_of(ptype, struct fcoe_interface, fip_packet_type);
Chris Leech3fe9a0b2009-08-25 13:59:46 -0700329 fcoe_ctlr_recv(&fcoe->ctlr, skb);
Vasu Devab6b85c2009-05-17 12:33:08 +0000330 return 0;
331}
332
333/**
334 * fcoe_fip_send() - send an Ethernet-encapsulated FIP frame.
335 * @fip: FCoE controller.
336 * @skb: FIP Packet.
337 */
338static void fcoe_fip_send(struct fcoe_ctlr *fip, struct sk_buff *skb)
339{
Chris Leech3fe9a0b2009-08-25 13:59:46 -0700340 skb->dev = fcoe_from_ctlr(fip)->netdev;
Vasu Devab6b85c2009-05-17 12:33:08 +0000341 dev_queue_xmit(skb);
342}
343
344/**
345 * fcoe_update_src_mac() - Update Ethernet MAC filters.
346 * @fip: FCoE controller.
347 * @old: Unicast MAC address to delete if the MAC is non-zero.
348 * @new: Unicast MAC address to add.
349 *
350 * Remove any previously-set unicast MAC filter.
351 * Add secondary FCoE MAC address filter for our OUI.
352 */
353static void fcoe_update_src_mac(struct fcoe_ctlr *fip, u8 *old, u8 *new)
354{
Chris Leech25024982009-08-25 13:59:35 -0700355 struct fcoe_interface *fcoe;
Vasu Devab6b85c2009-05-17 12:33:08 +0000356
Chris Leech3fe9a0b2009-08-25 13:59:46 -0700357 fcoe = fcoe_from_ctlr(fip);
Vasu Devab6b85c2009-05-17 12:33:08 +0000358 rtnl_lock();
359 if (!is_zero_ether_addr(old))
Chris Leech25024982009-08-25 13:59:35 -0700360 dev_unicast_delete(fcoe->netdev, old);
361 dev_unicast_add(fcoe->netdev, new);
Vasu Devab6b85c2009-05-17 12:33:08 +0000362 rtnl_unlock();
363}
364
365/**
Vasu Dev7f349142009-03-27 09:06:31 -0700366 * fcoe_lport_config() - sets up the fc_lport
367 * @lp: ptr to the fc_lport
Vasu Dev7f349142009-03-27 09:06:31 -0700368 *
369 * Returns: 0 for success
370 */
371static int fcoe_lport_config(struct fc_lport *lp)
372{
373 lp->link_up = 0;
374 lp->qfull = 0;
375 lp->max_retry_count = 3;
Abhijeet Joglekara3666952009-05-01 10:01:26 -0700376 lp->max_rport_retry_count = 3;
Vasu Dev7f349142009-03-27 09:06:31 -0700377 lp->e_d_tov = 2 * 1000; /* FC-FS default */
378 lp->r_a_tov = 2 * 2 * 1000;
379 lp->service_params = (FCP_SPPF_INIT_FCN | FCP_SPPF_RD_XRDY_DIS |
380 FCP_SPPF_RETRY | FCP_SPPF_CONF_COMPL);
381
382 fc_lport_init_stats(lp);
383
384 /* lport fc_lport related configuration */
385 fc_lport_config(lp);
386
387 /* offload related configuration */
388 lp->crc_offload = 0;
389 lp->seq_offload = 0;
390 lp->lro_enabled = 0;
391 lp->lro_xid = 0;
392 lp->lso_max = 0;
393
394 return 0;
395}
396
397/**
Vasu Dev1047f222009-05-06 10:52:40 -0700398 * fcoe_queue_timer() - fcoe queue timer
399 * @lp: the fc_lport pointer
400 *
401 * Calls fcoe_check_wait_queue on timeout
402 *
403 */
404static void fcoe_queue_timer(ulong lp)
405{
406 fcoe_check_wait_queue((struct fc_lport *)lp, NULL);
407}
408
409/**
Vasu Dev7f349142009-03-27 09:06:31 -0700410 * fcoe_netdev_config() - Set up netdev for SW FCoE
411 * @lp : ptr to the fc_lport
412 * @netdev : ptr to the associated netdevice struct
413 *
414 * Must be called after fcoe_lport_config() as it will use lport mutex
415 *
416 * Returns : 0 for success
417 */
418static int fcoe_netdev_config(struct fc_lport *lp, struct net_device *netdev)
419{
420 u32 mfs;
421 u64 wwnn, wwpn;
Chris Leech25024982009-08-25 13:59:35 -0700422 struct fcoe_interface *fcoe;
Chris Leech014f5c32009-08-25 13:59:30 -0700423 struct fcoe_port *port;
Vasu Dev7f349142009-03-27 09:06:31 -0700424
425 /* Setup lport private data to point to fcoe softc */
Chris Leech014f5c32009-08-25 13:59:30 -0700426 port = lport_priv(lp);
Chris Leech25024982009-08-25 13:59:35 -0700427 fcoe = port->fcoe;
Vasu Dev7f349142009-03-27 09:06:31 -0700428
429 /*
430 * Determine max frame size based on underlying device and optional
431 * user-configured limit. If the MFS is too low, fcoe_link_ok()
432 * will return 0, so do this first.
433 */
Vasu Dev1d1b88d2009-07-29 17:05:45 -0700434 mfs = netdev->mtu - (sizeof(struct fcoe_hdr) +
435 sizeof(struct fcoe_crc_eof));
Vasu Dev7f349142009-03-27 09:06:31 -0700436 if (fc_set_mfs(lp, mfs))
437 return -EINVAL;
438
Vasu Dev7f349142009-03-27 09:06:31 -0700439 /* offload features support */
Vasu Dev1d1b88d2009-07-29 17:05:45 -0700440 if (netdev->features & NETIF_F_SG)
Vasu Dev7f349142009-03-27 09:06:31 -0700441 lp->sg_supp = 1;
442
Vasu Dev7f349142009-03-27 09:06:31 -0700443 if (netdev->features & NETIF_F_FCOE_CRC) {
444 lp->crc_offload = 1;
Robert Loved5488eb2009-06-10 15:30:59 -0700445 FCOE_NETDEV_DBG(netdev, "Supports FCCRC offload\n");
Vasu Dev7f349142009-03-27 09:06:31 -0700446 }
Vasu Dev7f349142009-03-27 09:06:31 -0700447 if (netdev->features & NETIF_F_FSO) {
448 lp->seq_offload = 1;
449 lp->lso_max = netdev->gso_max_size;
Robert Loved5488eb2009-06-10 15:30:59 -0700450 FCOE_NETDEV_DBG(netdev, "Supports LSO for max len 0x%x\n",
451 lp->lso_max);
Vasu Dev7f349142009-03-27 09:06:31 -0700452 }
Vasu Dev7f349142009-03-27 09:06:31 -0700453 if (netdev->fcoe_ddp_xid) {
454 lp->lro_enabled = 1;
455 lp->lro_xid = netdev->fcoe_ddp_xid;
Robert Loved5488eb2009-06-10 15:30:59 -0700456 FCOE_NETDEV_DBG(netdev, "Supports LRO for max xid 0x%x\n",
457 lp->lro_xid);
Vasu Dev7f349142009-03-27 09:06:31 -0700458 }
Chris Leech014f5c32009-08-25 13:59:30 -0700459 skb_queue_head_init(&port->fcoe_pending_queue);
460 port->fcoe_pending_queue_active = 0;
461 setup_timer(&port->timer, fcoe_queue_timer, (unsigned long)lp);
Vasu Dev7f349142009-03-27 09:06:31 -0700462
Vasu Dev1d1b88d2009-07-29 17:05:45 -0700463 wwnn = fcoe_wwn_from_mac(netdev->dev_addr, 1, 0);
Vasu Dev7f349142009-03-27 09:06:31 -0700464 fc_set_wwnn(lp, wwnn);
465 /* XXX - 3rd arg needs to be vlan id */
Vasu Dev1d1b88d2009-07-29 17:05:45 -0700466 wwpn = fcoe_wwn_from_mac(netdev->dev_addr, 2, 0);
Vasu Dev7f349142009-03-27 09:06:31 -0700467 fc_set_wwpn(lp, wwpn);
468
Vasu Dev7f349142009-03-27 09:06:31 -0700469 return 0;
470}
471
472/**
473 * fcoe_shost_config() - Sets up fc_lport->host
474 * @lp : ptr to the fc_lport
475 * @shost : ptr to the associated scsi host
476 * @dev : device associated to scsi host
477 *
478 * Must be called after fcoe_lport_config() and fcoe_netdev_config()
479 *
480 * Returns : 0 for success
481 */
482static int fcoe_shost_config(struct fc_lport *lp, struct Scsi_Host *shost,
483 struct device *dev)
484{
485 int rc = 0;
486
487 /* lport scsi host config */
488 lp->host = shost;
489
490 lp->host->max_lun = FCOE_MAX_LUN;
491 lp->host->max_id = FCOE_MAX_FCP_TARGET;
492 lp->host->max_channel = 0;
493 lp->host->transportt = scsi_transport_fcoe_sw;
494
495 /* add the new host to the SCSI-ml */
496 rc = scsi_add_host(lp->host, dev);
497 if (rc) {
Robert Loved5488eb2009-06-10 15:30:59 -0700498 FCOE_NETDEV_DBG(fcoe_netdev(lp), "fcoe_shost_config: "
499 "error on scsi_add_host\n");
Vasu Dev7f349142009-03-27 09:06:31 -0700500 return rc;
501 }
502 sprintf(fc_host_symbolic_name(lp->host), "%s v%s over %s",
503 FCOE_NAME, FCOE_VERSION,
504 fcoe_netdev(lp)->name);
505
506 return 0;
507}
508
Vasu Devd7179682009-07-29 17:05:21 -0700509/*
510 * fcoe_oem_match() - match for read types IO
511 * @fp: the fc_frame for new IO.
512 *
513 * Returns : true for read types IO, otherwise returns false.
514 */
515bool fcoe_oem_match(struct fc_frame *fp)
516{
Yi Zou05cc7392009-08-25 13:59:03 -0700517 return fc_fcp_is_read(fr_fsp(fp)) &&
518 (fr_fsp(fp)->data_len > fcoe_ddp_min);
Vasu Devd7179682009-07-29 17:05:21 -0700519}
520
Vasu Dev7f349142009-03-27 09:06:31 -0700521/**
522 * fcoe_em_config() - allocates em for this lport
Chris Leech014f5c32009-08-25 13:59:30 -0700523 * @lp: the fcoe that em is to allocated for
Vasu Dev7f349142009-03-27 09:06:31 -0700524 *
Vasu Deve8af4d42009-07-29 17:05:15 -0700525 * Called with write fcoe_hostlist_lock held.
526 *
Vasu Dev7f349142009-03-27 09:06:31 -0700527 * Returns : 0 on success
528 */
529static inline int fcoe_em_config(struct fc_lport *lp)
530{
Chris Leech014f5c32009-08-25 13:59:30 -0700531 struct fcoe_port *port = lport_priv(lp);
Chris Leech25024982009-08-25 13:59:35 -0700532 struct fcoe_interface *fcoe = port->fcoe;
533 struct fcoe_interface *oldfcoe = NULL;
Vasu Dev1d1b88d2009-07-29 17:05:45 -0700534 struct net_device *old_real_dev, *cur_real_dev;
Vasu Devd7179682009-07-29 17:05:21 -0700535 u16 min_xid = FCOE_MIN_XID;
536 u16 max_xid = FCOE_MAX_XID;
537
538 /*
539 * Check if need to allocate an em instance for
540 * offload exchange ids to be shared across all VN_PORTs/lport.
541 */
542 if (!lp->lro_enabled || !lp->lro_xid || (lp->lro_xid >= max_xid)) {
543 lp->lro_xid = 0;
544 goto skip_oem;
545 }
546
547 /*
548 * Reuse existing offload em instance in case
Vasu Dev1d1b88d2009-07-29 17:05:45 -0700549 * it is already allocated on real eth device
Vasu Devd7179682009-07-29 17:05:21 -0700550 */
Chris Leech25024982009-08-25 13:59:35 -0700551 if (fcoe->netdev->priv_flags & IFF_802_1Q_VLAN)
552 cur_real_dev = vlan_dev_real_dev(fcoe->netdev);
Vasu Dev1d1b88d2009-07-29 17:05:45 -0700553 else
Chris Leech25024982009-08-25 13:59:35 -0700554 cur_real_dev = fcoe->netdev;
Vasu Dev1d1b88d2009-07-29 17:05:45 -0700555
Chris Leech25024982009-08-25 13:59:35 -0700556 list_for_each_entry(oldfcoe, &fcoe_hostlist, list) {
Chris Leech25024982009-08-25 13:59:35 -0700557 if (oldfcoe->netdev->priv_flags & IFF_802_1Q_VLAN)
558 old_real_dev = vlan_dev_real_dev(oldfcoe->netdev);
Vasu Dev1d1b88d2009-07-29 17:05:45 -0700559 else
Chris Leech25024982009-08-25 13:59:35 -0700560 old_real_dev = oldfcoe->netdev;
Vasu Dev1d1b88d2009-07-29 17:05:45 -0700561
562 if (cur_real_dev == old_real_dev) {
Chris Leech991cbb62009-08-25 13:59:51 -0700563 fcoe->oem = oldfcoe->oem;
Vasu Devd7179682009-07-29 17:05:21 -0700564 break;
565 }
566 }
567
Chris Leech991cbb62009-08-25 13:59:51 -0700568 if (fcoe->oem) {
569 if (!fc_exch_mgr_add(lp, fcoe->oem, fcoe_oem_match)) {
Vasu Devd7179682009-07-29 17:05:21 -0700570 printk(KERN_ERR "fcoe_em_config: failed to add "
571 "offload em:%p on interface:%s\n",
Chris Leech991cbb62009-08-25 13:59:51 -0700572 fcoe->oem, fcoe->netdev->name);
Vasu Devd7179682009-07-29 17:05:21 -0700573 return -ENOMEM;
574 }
575 } else {
Chris Leech991cbb62009-08-25 13:59:51 -0700576 fcoe->oem = fc_exch_mgr_alloc(lp, FC_CLASS_3,
Vasu Devd7179682009-07-29 17:05:21 -0700577 FCOE_MIN_XID, lp->lro_xid,
578 fcoe_oem_match);
Chris Leech991cbb62009-08-25 13:59:51 -0700579 if (!fcoe->oem) {
Vasu Devd7179682009-07-29 17:05:21 -0700580 printk(KERN_ERR "fcoe_em_config: failed to allocate "
581 "em for offload exches on interface:%s\n",
Chris Leech25024982009-08-25 13:59:35 -0700582 fcoe->netdev->name);
Vasu Devd7179682009-07-29 17:05:21 -0700583 return -ENOMEM;
584 }
585 }
586
587 /*
588 * Exclude offload EM xid range from next EM xid range.
589 */
590 min_xid += lp->lro_xid + 1;
591
592skip_oem:
593 if (!fc_exch_mgr_alloc(lp, FC_CLASS_3, min_xid, max_xid, NULL)) {
594 printk(KERN_ERR "fcoe_em_config: failed to "
Chris Leech25024982009-08-25 13:59:35 -0700595 "allocate em on interface %s\n", fcoe->netdev->name);
Vasu Dev7f349142009-03-27 09:06:31 -0700596 return -ENOMEM;
Vasu Devd7179682009-07-29 17:05:21 -0700597 }
Vasu Dev7f349142009-03-27 09:06:31 -0700598
599 return 0;
600}
601
602/**
603 * fcoe_if_destroy() - FCoE software HBA tear-down function
Chris Leechaf7f85d2009-08-25 13:59:24 -0700604 * @lport: fc_lport to destroy
Vasu Dev7f349142009-03-27 09:06:31 -0700605 */
Chris Leechaf7f85d2009-08-25 13:59:24 -0700606static void fcoe_if_destroy(struct fc_lport *lport)
Vasu Dev7f349142009-03-27 09:06:31 -0700607{
Chris Leech014f5c32009-08-25 13:59:30 -0700608 struct fcoe_port *port = lport_priv(lport);
609 struct fcoe_interface *fcoe = port->fcoe;
Chris Leech25024982009-08-25 13:59:35 -0700610 struct net_device *netdev = fcoe->netdev;
Vasu Dev7f349142009-03-27 09:06:31 -0700611
Robert Loved5488eb2009-06-10 15:30:59 -0700612 FCOE_NETDEV_DBG(netdev, "Destroying interface\n");
Vasu Dev7f349142009-03-27 09:06:31 -0700613
Vasu Dev7f349142009-03-27 09:06:31 -0700614 /* Logout of the fabric */
Chris Leechaf7f85d2009-08-25 13:59:24 -0700615 fc_fabric_logoff(lport);
Vasu Dev7f349142009-03-27 09:06:31 -0700616
617 /* Remove the instance from fcoe's list */
Chris Leechaf7f85d2009-08-25 13:59:24 -0700618 fcoe_hostlist_remove(lport);
Vasu Dev7f349142009-03-27 09:06:31 -0700619
Vasu Dev7f349142009-03-27 09:06:31 -0700620 /* Cleanup the fc_lport */
Chris Leechaf7f85d2009-08-25 13:59:24 -0700621 fc_lport_destroy(lport);
622 fc_fcp_destroy(lport);
Vasu Dev7f349142009-03-27 09:06:31 -0700623
Chris Leech54b649f2009-08-25 14:00:07 -0700624 /* Stop the transmit retry timer */
625 del_timer_sync(&port->timer);
626
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);
632
633 /* Free queued packets for the per-CPU receive threads */
634 fcoe_percpu_clean(lport);
635
Vasu Dev7f349142009-03-27 09:06:31 -0700636 /* Detach from the scsi-ml */
Chris Leechaf7f85d2009-08-25 13:59:24 -0700637 fc_remove_host(lport->host);
638 scsi_remove_host(lport->host);
Vasu Dev7f349142009-03-27 09:06:31 -0700639
640 /* There are no more rports or I/O, free the EM */
Chris Leechaf7f85d2009-08-25 13:59:24 -0700641 fc_exch_mgr_free(lport);
Vasu Dev7f349142009-03-27 09:06:31 -0700642
Vasu Dev7f349142009-03-27 09:06:31 -0700643 /* Free memory used by statistical counters */
Chris Leechaf7f85d2009-08-25 13:59:24 -0700644 fc_lport_free_stats(lport);
Vasu Dev7f349142009-03-27 09:06:31 -0700645
646 /* Release the net_device and Scsi_Host */
Vasu Dev1d1b88d2009-07-29 17:05:45 -0700647 dev_put(netdev);
Chris Leechaf7f85d2009-08-25 13:59:24 -0700648 scsi_host_put(lport->host);
Vasu Dev7f349142009-03-27 09:06:31 -0700649}
650
651/*
652 * fcoe_ddp_setup - calls LLD's ddp_setup through net_device
653 * @lp: the corresponding fc_lport
654 * @xid: the exchange id for this ddp transfer
655 * @sgl: the scatterlist describing this transfer
656 * @sgc: number of sg items
657 *
658 * Returns : 0 no ddp
659 */
660static int fcoe_ddp_setup(struct fc_lport *lp, u16 xid,
661 struct scatterlist *sgl, unsigned int sgc)
662{
663 struct net_device *n = fcoe_netdev(lp);
664
665 if (n->netdev_ops && n->netdev_ops->ndo_fcoe_ddp_setup)
666 return n->netdev_ops->ndo_fcoe_ddp_setup(n, xid, sgl, sgc);
667
668 return 0;
669}
670
671/*
672 * fcoe_ddp_done - calls LLD's ddp_done through net_device
673 * @lp: the corresponding fc_lport
674 * @xid: the exchange id for this ddp transfer
675 *
676 * Returns : the length of data that have been completed by ddp
677 */
678static int fcoe_ddp_done(struct fc_lport *lp, u16 xid)
679{
680 struct net_device *n = fcoe_netdev(lp);
681
682 if (n->netdev_ops && n->netdev_ops->ndo_fcoe_ddp_done)
683 return n->netdev_ops->ndo_fcoe_ddp_done(n, xid);
684 return 0;
685}
686
687static struct libfc_function_template fcoe_libfc_fcn_templ = {
688 .frame_send = fcoe_xmit,
689 .ddp_setup = fcoe_ddp_setup,
690 .ddp_done = fcoe_ddp_done,
691};
692
693/**
Chris Leech030f4e02009-08-25 14:00:02 -0700694 * fcoe_if_create() - this function creates the fcoe port
695 * @fcoe: fcoe_interface structure to create an fc_lport instance on
Chris Leechaf7f85d2009-08-25 13:59:24 -0700696 * @parent: device pointer to be the parent in sysfs for the SCSI host
Vasu Dev7f349142009-03-27 09:06:31 -0700697 *
Chris Leech54b649f2009-08-25 14:00:07 -0700698 * Creates fc_lport struct and scsi_host for lport, configures lport.
Vasu Dev7f349142009-03-27 09:06:31 -0700699 *
Chris Leechaf7f85d2009-08-25 13:59:24 -0700700 * Returns : The allocated fc_lport or an error pointer
Vasu Dev7f349142009-03-27 09:06:31 -0700701 */
Chris Leech030f4e02009-08-25 14:00:02 -0700702static struct fc_lport *fcoe_if_create(struct fcoe_interface *fcoe,
Chris Leechaf7f85d2009-08-25 13:59:24 -0700703 struct device *parent)
Vasu Dev7f349142009-03-27 09:06:31 -0700704{
705 int rc;
Chris Leechaf7f85d2009-08-25 13:59:24 -0700706 struct fc_lport *lport = NULL;
Chris Leech014f5c32009-08-25 13:59:30 -0700707 struct fcoe_port *port;
Vasu Dev7f349142009-03-27 09:06:31 -0700708 struct Scsi_Host *shost;
Chris Leech030f4e02009-08-25 14:00:02 -0700709 struct net_device *netdev = fcoe->netdev;
Vasu Dev7f349142009-03-27 09:06:31 -0700710
Robert Loved5488eb2009-06-10 15:30:59 -0700711 FCOE_NETDEV_DBG(netdev, "Create Interface\n");
Vasu Dev7f349142009-03-27 09:06:31 -0700712
Chris Leech014f5c32009-08-25 13:59:30 -0700713 shost = libfc_host_alloc(&fcoe_shost_template,
714 sizeof(struct fcoe_port));
715 if (!shost) {
716 FCOE_NETDEV_DBG(netdev, "Could not allocate host structure\n");
717 rc = -ENOMEM;
Chris Leech030f4e02009-08-25 14:00:02 -0700718 goto out;
Chris Leech014f5c32009-08-25 13:59:30 -0700719 }
Chris Leechaf7f85d2009-08-25 13:59:24 -0700720 lport = shost_priv(shost);
Chris Leech014f5c32009-08-25 13:59:30 -0700721 port = lport_priv(lport);
Chris Leech014f5c32009-08-25 13:59:30 -0700722 port->fcoe = fcoe;
Vasu Dev7f349142009-03-27 09:06:31 -0700723
724 /* configure fc_lport, e.g., em */
Chris Leechaf7f85d2009-08-25 13:59:24 -0700725 rc = fcoe_lport_config(lport);
Vasu Dev7f349142009-03-27 09:06:31 -0700726 if (rc) {
Robert Loved5488eb2009-06-10 15:30:59 -0700727 FCOE_NETDEV_DBG(netdev, "Could not configure lport for the "
728 "interface\n");
Vasu Dev7f349142009-03-27 09:06:31 -0700729 goto out_host_put;
730 }
731
Vasu Devab6b85c2009-05-17 12:33:08 +0000732 /* configure lport network properties */
Chris Leechaf7f85d2009-08-25 13:59:24 -0700733 rc = fcoe_netdev_config(lport, netdev);
Vasu Devab6b85c2009-05-17 12:33:08 +0000734 if (rc) {
Robert Loved5488eb2009-06-10 15:30:59 -0700735 FCOE_NETDEV_DBG(netdev, "Could not configure netdev for the "
736 "interface\n");
Chris Leech54b649f2009-08-25 14:00:07 -0700737 goto out_lp_destroy;
Vasu Devab6b85c2009-05-17 12:33:08 +0000738 }
Joe Eykholt97c83892009-03-17 11:42:40 -0700739
Vasu Dev7f349142009-03-27 09:06:31 -0700740 /* configure lport scsi host properties */
Chris Leechaf7f85d2009-08-25 13:59:24 -0700741 rc = fcoe_shost_config(lport, shost, parent);
Vasu Dev7f349142009-03-27 09:06:31 -0700742 if (rc) {
Robert Loved5488eb2009-06-10 15:30:59 -0700743 FCOE_NETDEV_DBG(netdev, "Could not configure shost for the "
744 "interface\n");
Chris Leech54b649f2009-08-25 14:00:07 -0700745 goto out_lp_destroy;
Vasu Dev7f349142009-03-27 09:06:31 -0700746 }
747
Vasu Dev7f349142009-03-27 09:06:31 -0700748 /* Initialize the library */
Chris Leechaf7f85d2009-08-25 13:59:24 -0700749 rc = fcoe_libfc_config(lport, &fcoe_libfc_fcn_templ);
Vasu Dev7f349142009-03-27 09:06:31 -0700750 if (rc) {
Robert Loved5488eb2009-06-10 15:30:59 -0700751 FCOE_NETDEV_DBG(netdev, "Could not configure libfc for the "
752 "interface\n");
Vasu Dev7f349142009-03-27 09:06:31 -0700753 goto out_lp_destroy;
754 }
755
Vasu Deve8af4d42009-07-29 17:05:15 -0700756 /*
757 * fcoe_em_alloc() and fcoe_hostlist_add() both
758 * need to be atomic under fcoe_hostlist_lock
759 * since fcoe_em_alloc() looks for an existing EM
760 * instance on host list updated by fcoe_hostlist_add().
761 */
762 write_lock(&fcoe_hostlist_lock);
Vasu Dev96316092009-07-29 17:05:00 -0700763 /* lport exch manager allocation */
Chris Leechaf7f85d2009-08-25 13:59:24 -0700764 rc = fcoe_em_config(lport);
Vasu Dev96316092009-07-29 17:05:00 -0700765 if (rc) {
766 FCOE_NETDEV_DBG(netdev, "Could not configure the EM for the "
767 "interface\n");
768 goto out_lp_destroy;
769 }
770
Vasu Dev7f349142009-03-27 09:06:31 -0700771 /* add to lports list */
Chris Leechaf7f85d2009-08-25 13:59:24 -0700772 fcoe_hostlist_add(lport);
Vasu Deve8af4d42009-07-29 17:05:15 -0700773 write_unlock(&fcoe_hostlist_lock);
Vasu Dev7f349142009-03-27 09:06:31 -0700774
Vasu Dev7f349142009-03-27 09:06:31 -0700775 dev_hold(netdev);
Chris Leech030f4e02009-08-25 14:00:02 -0700776 fcoe_interface_get(fcoe);
Chris Leechaf7f85d2009-08-25 13:59:24 -0700777 return lport;
Vasu Dev7f349142009-03-27 09:06:31 -0700778
779out_lp_destroy:
Chris Leechaf7f85d2009-08-25 13:59:24 -0700780 fc_exch_mgr_free(lport);
Vasu Dev7f349142009-03-27 09:06:31 -0700781out_host_put:
Chris Leechaf7f85d2009-08-25 13:59:24 -0700782 scsi_host_put(lport->host);
783out:
784 return ERR_PTR(rc);
Vasu Dev7f349142009-03-27 09:06:31 -0700785}
786
787/**
788 * fcoe_if_init() - attach to scsi transport
789 *
790 * Returns : 0 on success
791 */
792static int __init fcoe_if_init(void)
793{
794 /* attach to scsi transport */
795 scsi_transport_fcoe_sw =
796 fc_attach_transport(&fcoe_transport_function);
797
798 if (!scsi_transport_fcoe_sw) {
Robert Loved5488eb2009-06-10 15:30:59 -0700799 printk(KERN_ERR "fcoe: Failed to attach to the FC transport\n");
Vasu Dev7f349142009-03-27 09:06:31 -0700800 return -ENODEV;
801 }
802
803 return 0;
804}
805
806/**
807 * fcoe_if_exit() - detach from scsi transport
808 *
809 * Returns : 0 on success
810 */
811int __exit fcoe_if_exit(void)
812{
813 fc_release_transport(scsi_transport_fcoe_sw);
814 return 0;
815}
816
Robert Love85b4aa42008-12-09 15:10:24 -0800817/**
Robert Love8976f422009-03-17 11:41:46 -0700818 * fcoe_percpu_thread_create() - Create a receive thread for an online cpu
819 * @cpu: cpu index for the online cpu
820 */
821static void fcoe_percpu_thread_create(unsigned int cpu)
822{
823 struct fcoe_percpu_s *p;
824 struct task_struct *thread;
825
826 p = &per_cpu(fcoe_percpu, cpu);
827
828 thread = kthread_create(fcoe_percpu_receive_thread,
829 (void *)p, "fcoethread/%d", cpu);
830
831 if (likely(!IS_ERR(p->thread))) {
832 kthread_bind(thread, cpu);
833 wake_up_process(thread);
834
835 spin_lock_bh(&p->fcoe_rx_list.lock);
836 p->thread = thread;
837 spin_unlock_bh(&p->fcoe_rx_list.lock);
838 }
839}
840
841/**
842 * fcoe_percpu_thread_destroy() - removes the rx thread for the given cpu
843 * @cpu: cpu index the rx thread is to be removed
844 *
845 * Destroys a per-CPU Rx thread. Any pending skbs are moved to the
846 * current CPU's Rx thread. If the thread being destroyed is bound to
847 * the CPU processing this context the skbs will be freed.
848 */
849static void fcoe_percpu_thread_destroy(unsigned int cpu)
850{
851 struct fcoe_percpu_s *p;
852 struct task_struct *thread;
853 struct page *crc_eof;
854 struct sk_buff *skb;
855#ifdef CONFIG_SMP
856 struct fcoe_percpu_s *p0;
857 unsigned targ_cpu = smp_processor_id();
858#endif /* CONFIG_SMP */
859
Robert Loved5488eb2009-06-10 15:30:59 -0700860 FCOE_DBG("Destroying receive thread for CPU %d\n", cpu);
Robert Love8976f422009-03-17 11:41:46 -0700861
862 /* Prevent any new skbs from being queued for this CPU. */
863 p = &per_cpu(fcoe_percpu, cpu);
864 spin_lock_bh(&p->fcoe_rx_list.lock);
865 thread = p->thread;
866 p->thread = NULL;
867 crc_eof = p->crc_eof_page;
868 p->crc_eof_page = NULL;
869 p->crc_eof_offset = 0;
870 spin_unlock_bh(&p->fcoe_rx_list.lock);
871
872#ifdef CONFIG_SMP
873 /*
874 * Don't bother moving the skb's if this context is running
875 * on the same CPU that is having its thread destroyed. This
876 * can easily happen when the module is removed.
877 */
878 if (cpu != targ_cpu) {
879 p0 = &per_cpu(fcoe_percpu, targ_cpu);
880 spin_lock_bh(&p0->fcoe_rx_list.lock);
881 if (p0->thread) {
Robert Loved5488eb2009-06-10 15:30:59 -0700882 FCOE_DBG("Moving frames from CPU %d to CPU %d\n",
883 cpu, targ_cpu);
Robert Love8976f422009-03-17 11:41:46 -0700884
885 while ((skb = __skb_dequeue(&p->fcoe_rx_list)) != NULL)
886 __skb_queue_tail(&p0->fcoe_rx_list, skb);
887 spin_unlock_bh(&p0->fcoe_rx_list.lock);
888 } else {
889 /*
890 * The targeted CPU is not initialized and cannot accept
891 * new skbs. Unlock the targeted CPU and drop the skbs
892 * on the CPU that is going offline.
893 */
894 while ((skb = __skb_dequeue(&p->fcoe_rx_list)) != NULL)
895 kfree_skb(skb);
896 spin_unlock_bh(&p0->fcoe_rx_list.lock);
897 }
898 } else {
899 /*
900 * This scenario occurs when the module is being removed
901 * and all threads are being destroyed. skbs will continue
902 * to be shifted from the CPU thread that is being removed
903 * to the CPU thread associated with the CPU that is processing
904 * the module removal. Once there is only one CPU Rx thread it
905 * will reach this case and we will drop all skbs and later
906 * stop the thread.
907 */
908 spin_lock_bh(&p->fcoe_rx_list.lock);
909 while ((skb = __skb_dequeue(&p->fcoe_rx_list)) != NULL)
910 kfree_skb(skb);
911 spin_unlock_bh(&p->fcoe_rx_list.lock);
912 }
913#else
914 /*
Chris Leechdd3fd722009-04-21 16:27:36 -0700915 * This a non-SMP scenario where the singular Rx thread is
Robert Love8976f422009-03-17 11:41:46 -0700916 * being removed. Free all skbs and stop the thread.
917 */
918 spin_lock_bh(&p->fcoe_rx_list.lock);
919 while ((skb = __skb_dequeue(&p->fcoe_rx_list)) != NULL)
920 kfree_skb(skb);
921 spin_unlock_bh(&p->fcoe_rx_list.lock);
922#endif
923
924 if (thread)
925 kthread_stop(thread);
926
927 if (crc_eof)
928 put_page(crc_eof);
929}
930
931/**
932 * fcoe_cpu_callback() - fcoe cpu hotplug event callback
933 * @nfb: callback data block
934 * @action: event triggering the callback
935 * @hcpu: index for the cpu of this event
936 *
937 * This creates or destroys per cpu data for fcoe
938 *
939 * Returns NOTIFY_OK always.
940 */
941static int fcoe_cpu_callback(struct notifier_block *nfb,
942 unsigned long action, void *hcpu)
943{
944 unsigned cpu = (unsigned long)hcpu;
945
946 switch (action) {
947 case CPU_ONLINE:
948 case CPU_ONLINE_FROZEN:
Robert Loved5488eb2009-06-10 15:30:59 -0700949 FCOE_DBG("CPU %x online: Create Rx thread\n", cpu);
Robert Love8976f422009-03-17 11:41:46 -0700950 fcoe_percpu_thread_create(cpu);
951 break;
952 case CPU_DEAD:
953 case CPU_DEAD_FROZEN:
Robert Loved5488eb2009-06-10 15:30:59 -0700954 FCOE_DBG("CPU %x offline: Remove Rx thread\n", cpu);
Robert Love8976f422009-03-17 11:41:46 -0700955 fcoe_percpu_thread_destroy(cpu);
956 break;
957 default:
958 break;
959 }
960 return NOTIFY_OK;
961}
962
963static struct notifier_block fcoe_cpu_notifier = {
964 .notifier_call = fcoe_cpu_callback,
965};
966
967/**
Robert Love34f42a02009-02-27 10:55:45 -0800968 * fcoe_rcv() - this is the fcoe receive function called by NET_RX_SOFTIRQ
Robert Love85b4aa42008-12-09 15:10:24 -0800969 * @skb: the receive skb
970 * @dev: associated net device
971 * @ptype: context
Chris Leechdd3fd722009-04-21 16:27:36 -0700972 * @olddev: last device
Robert Love85b4aa42008-12-09 15:10:24 -0800973 *
974 * this function will receive the packet and build fc frame and pass it up
975 *
976 * Returns: 0 for success
Robert Love34f42a02009-02-27 10:55:45 -0800977 */
Robert Love85b4aa42008-12-09 15:10:24 -0800978int fcoe_rcv(struct sk_buff *skb, struct net_device *dev,
979 struct packet_type *ptype, struct net_device *olddev)
980{
981 struct fc_lport *lp;
982 struct fcoe_rcv_info *fr;
Chris Leech259ad852009-08-25 13:59:41 -0700983 struct fcoe_interface *fcoe;
Robert Love85b4aa42008-12-09 15:10:24 -0800984 struct fc_frame_header *fh;
Robert Love85b4aa42008-12-09 15:10:24 -0800985 struct fcoe_percpu_s *fps;
Vasu Devb2f00912009-08-25 13:58:53 -0700986 unsigned int cpu;
Robert Love85b4aa42008-12-09 15:10:24 -0800987
Chris Leech259ad852009-08-25 13:59:41 -0700988 fcoe = container_of(ptype, struct fcoe_interface, fcoe_packet_type);
Chris Leech3fe9a0b2009-08-25 13:59:46 -0700989 lp = fcoe->ctlr.lp;
Robert Love85b4aa42008-12-09 15:10:24 -0800990 if (unlikely(lp == NULL)) {
Robert Loved5488eb2009-06-10 15:30:59 -0700991 FCOE_NETDEV_DBG(dev, "Cannot find hba structure");
Robert Love85b4aa42008-12-09 15:10:24 -0800992 goto err2;
993 }
Joe Eykholt97c83892009-03-17 11:42:40 -0700994 if (!lp->link_up)
995 goto err2;
Robert Love85b4aa42008-12-09 15:10:24 -0800996
Robert Loved5488eb2009-06-10 15:30:59 -0700997 FCOE_NETDEV_DBG(dev, "skb_info: len:%d data_len:%d head:%p "
998 "data:%p tail:%p end:%p sum:%d dev:%s",
999 skb->len, skb->data_len, skb->head, skb->data,
1000 skb_tail_pointer(skb), skb_end_pointer(skb),
1001 skb->csum, skb->dev ? skb->dev->name : "<NULL>");
Robert Love85b4aa42008-12-09 15:10:24 -08001002
1003 /* check for FCOE packet type */
1004 if (unlikely(eth_hdr(skb)->h_proto != htons(ETH_P_FCOE))) {
Robert Loved5488eb2009-06-10 15:30:59 -07001005 FCOE_NETDEV_DBG(dev, "Wrong FC type frame");
Robert Love85b4aa42008-12-09 15:10:24 -08001006 goto err;
1007 }
1008
1009 /*
1010 * Check for minimum frame length, and make sure required FCoE
1011 * and FC headers are pulled into the linear data area.
1012 */
1013 if (unlikely((skb->len < FCOE_MIN_FRAME) ||
1014 !pskb_may_pull(skb, FCOE_HEADER_LEN)))
1015 goto err;
1016
1017 skb_set_transport_header(skb, sizeof(struct fcoe_hdr));
1018 fh = (struct fc_frame_header *) skb_transport_header(skb);
1019
Robert Love85b4aa42008-12-09 15:10:24 -08001020 fr = fcoe_dev_from_skb(skb);
1021 fr->fr_dev = lp;
1022 fr->ptype = ptype;
Robert Love5e5e92d2009-03-17 11:41:35 -07001023
Robert Love85b4aa42008-12-09 15:10:24 -08001024 /*
Vasu Devb2f00912009-08-25 13:58:53 -07001025 * In case the incoming frame's exchange is originated from
1026 * the initiator, then received frame's exchange id is ANDed
1027 * with fc_cpu_mask bits to get the same cpu on which exchange
1028 * was originated, otherwise just use the current cpu.
Robert Love85b4aa42008-12-09 15:10:24 -08001029 */
Vasu Devb2f00912009-08-25 13:58:53 -07001030 if (ntoh24(fh->fh_f_ctl) & FC_FC_EX_CTX)
1031 cpu = ntohs(fh->fh_ox_id) & fc_cpu_mask;
1032 else
1033 cpu = smp_processor_id();
Robert Love5e5e92d2009-03-17 11:41:35 -07001034
Robert Love8976f422009-03-17 11:41:46 -07001035 fps = &per_cpu(fcoe_percpu, cpu);
Robert Love85b4aa42008-12-09 15:10:24 -08001036 spin_lock_bh(&fps->fcoe_rx_list.lock);
Robert Love8976f422009-03-17 11:41:46 -07001037 if (unlikely(!fps->thread)) {
1038 /*
1039 * The targeted CPU is not ready, let's target
1040 * the first CPU now. For non-SMP systems this
1041 * will check the same CPU twice.
1042 */
Robert Loved5488eb2009-06-10 15:30:59 -07001043 FCOE_NETDEV_DBG(dev, "CPU is online, but no receive thread "
1044 "ready for incoming skb- using first online "
1045 "CPU.\n");
Robert Love8976f422009-03-17 11:41:46 -07001046
1047 spin_unlock_bh(&fps->fcoe_rx_list.lock);
1048 cpu = first_cpu(cpu_online_map);
1049 fps = &per_cpu(fcoe_percpu, cpu);
1050 spin_lock_bh(&fps->fcoe_rx_list.lock);
1051 if (!fps->thread) {
1052 spin_unlock_bh(&fps->fcoe_rx_list.lock);
1053 goto err;
1054 }
1055 }
1056
1057 /*
1058 * We now have a valid CPU that we're targeting for
1059 * this skb. We also have this receive thread locked,
1060 * so we're free to queue skbs into it's queue.
1061 */
Robert Love85b4aa42008-12-09 15:10:24 -08001062 __skb_queue_tail(&fps->fcoe_rx_list, skb);
1063 if (fps->fcoe_rx_list.qlen == 1)
1064 wake_up_process(fps->thread);
1065
1066 spin_unlock_bh(&fps->fcoe_rx_list.lock);
1067
1068 return 0;
1069err:
Robert Love582b45b2009-03-31 15:51:50 -07001070 fc_lport_get_stats(lp)->ErrorFrames++;
Robert Love85b4aa42008-12-09 15:10:24 -08001071
1072err2:
1073 kfree_skb(skb);
1074 return -1;
1075}
Robert Love85b4aa42008-12-09 15:10:24 -08001076
1077/**
Robert Love34f42a02009-02-27 10:55:45 -08001078 * fcoe_start_io() - pass to netdev to start xmit for fcoe
Robert Love85b4aa42008-12-09 15:10:24 -08001079 * @skb: the skb to be xmitted
1080 *
1081 * Returns: 0 for success
Robert Love34f42a02009-02-27 10:55:45 -08001082 */
Robert Love85b4aa42008-12-09 15:10:24 -08001083static inline int fcoe_start_io(struct sk_buff *skb)
1084{
1085 int rc;
1086
1087 skb_get(skb);
1088 rc = dev_queue_xmit(skb);
1089 if (rc != 0)
1090 return rc;
1091 kfree_skb(skb);
1092 return 0;
1093}
1094
1095/**
Chris Leechdd3fd722009-04-21 16:27:36 -07001096 * fcoe_get_paged_crc_eof() - in case we need to alloc a page for crc_eof
Robert Love85b4aa42008-12-09 15:10:24 -08001097 * @skb: the skb to be xmitted
1098 * @tlen: total len
1099 *
1100 * Returns: 0 for success
Robert Love34f42a02009-02-27 10:55:45 -08001101 */
Robert Love85b4aa42008-12-09 15:10:24 -08001102static int fcoe_get_paged_crc_eof(struct sk_buff *skb, int tlen)
1103{
1104 struct fcoe_percpu_s *fps;
1105 struct page *page;
Robert Love85b4aa42008-12-09 15:10:24 -08001106
Robert Love5e5e92d2009-03-17 11:41:35 -07001107 fps = &get_cpu_var(fcoe_percpu);
Robert Love85b4aa42008-12-09 15:10:24 -08001108 page = fps->crc_eof_page;
1109 if (!page) {
1110 page = alloc_page(GFP_ATOMIC);
1111 if (!page) {
Robert Love5e5e92d2009-03-17 11:41:35 -07001112 put_cpu_var(fcoe_percpu);
Robert Love85b4aa42008-12-09 15:10:24 -08001113 return -ENOMEM;
1114 }
1115 fps->crc_eof_page = page;
Robert Love8976f422009-03-17 11:41:46 -07001116 fps->crc_eof_offset = 0;
Robert Love85b4aa42008-12-09 15:10:24 -08001117 }
1118
1119 get_page(page);
1120 skb_fill_page_desc(skb, skb_shinfo(skb)->nr_frags, page,
1121 fps->crc_eof_offset, tlen);
1122 skb->len += tlen;
1123 skb->data_len += tlen;
1124 skb->truesize += tlen;
1125 fps->crc_eof_offset += sizeof(struct fcoe_crc_eof);
1126
1127 if (fps->crc_eof_offset >= PAGE_SIZE) {
1128 fps->crc_eof_page = NULL;
1129 fps->crc_eof_offset = 0;
1130 put_page(page);
1131 }
Robert Love5e5e92d2009-03-17 11:41:35 -07001132 put_cpu_var(fcoe_percpu);
Robert Love85b4aa42008-12-09 15:10:24 -08001133 return 0;
1134}
1135
1136/**
Robert Love34f42a02009-02-27 10:55:45 -08001137 * fcoe_fc_crc() - calculates FC CRC in this fcoe skb
Chris Leechdd3fd722009-04-21 16:27:36 -07001138 * @fp: the fc_frame containing data to be checksummed
Robert Love85b4aa42008-12-09 15:10:24 -08001139 *
Chris Leech014f5c32009-08-25 13:59:30 -07001140 * This uses crc32() to calculate the crc for port frame
Robert Love85b4aa42008-12-09 15:10:24 -08001141 * Return : 32 bit crc
Robert Love34f42a02009-02-27 10:55:45 -08001142 */
Robert Love85b4aa42008-12-09 15:10:24 -08001143u32 fcoe_fc_crc(struct fc_frame *fp)
1144{
1145 struct sk_buff *skb = fp_skb(fp);
1146 struct skb_frag_struct *frag;
1147 unsigned char *data;
1148 unsigned long off, len, clen;
1149 u32 crc;
1150 unsigned i;
1151
1152 crc = crc32(~0, skb->data, skb_headlen(skb));
1153
1154 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
1155 frag = &skb_shinfo(skb)->frags[i];
1156 off = frag->page_offset;
1157 len = frag->size;
1158 while (len > 0) {
1159 clen = min(len, PAGE_SIZE - (off & ~PAGE_MASK));
1160 data = kmap_atomic(frag->page + (off >> PAGE_SHIFT),
1161 KM_SKB_DATA_SOFTIRQ);
1162 crc = crc32(crc, data + (off & ~PAGE_MASK), clen);
1163 kunmap_atomic(data, KM_SKB_DATA_SOFTIRQ);
1164 off += clen;
1165 len -= clen;
1166 }
1167 }
1168 return crc;
1169}
Robert Love85b4aa42008-12-09 15:10:24 -08001170
1171/**
Robert Love34f42a02009-02-27 10:55:45 -08001172 * fcoe_xmit() - FCoE frame transmit function
Chris Leech014f5c32009-08-25 13:59:30 -07001173 * @lp: the associated local fcoe
Robert Love85b4aa42008-12-09 15:10:24 -08001174 * @fp: the fc_frame to be transmitted
1175 *
1176 * Return : 0 for success
Robert Love34f42a02009-02-27 10:55:45 -08001177 */
Robert Love85b4aa42008-12-09 15:10:24 -08001178int fcoe_xmit(struct fc_lport *lp, struct fc_frame *fp)
1179{
Vasu Dev4bb6b512009-05-06 10:52:34 -07001180 int wlen;
Robert Love85b4aa42008-12-09 15:10:24 -08001181 u32 crc;
1182 struct ethhdr *eh;
1183 struct fcoe_crc_eof *cp;
1184 struct sk_buff *skb;
1185 struct fcoe_dev_stats *stats;
1186 struct fc_frame_header *fh;
1187 unsigned int hlen; /* header length implies the version */
1188 unsigned int tlen; /* trailer length */
1189 unsigned int elen; /* eth header, may include vlan */
Chris Leech3fe9a0b2009-08-25 13:59:46 -07001190 struct fcoe_port *port = lport_priv(lp);
1191 struct fcoe_interface *fcoe = port->fcoe;
Robert Love85b4aa42008-12-09 15:10:24 -08001192 u8 sof, eof;
1193 struct fcoe_hdr *hp;
1194
1195 WARN_ON((fr_len(fp) % sizeof(u32)) != 0);
1196
Robert Love85b4aa42008-12-09 15:10:24 -08001197 fh = fc_frame_header_get(fp);
Joe Eykholt97c83892009-03-17 11:42:40 -07001198 skb = fp_skb(fp);
1199 wlen = skb->len / FCOE_WORD_TO_BYTE;
1200
1201 if (!lp->link_up) {
Dan Carpenter3caf02e2009-04-21 16:27:25 -07001202 kfree_skb(skb);
Joe Eykholt97c83892009-03-17 11:42:40 -07001203 return 0;
Robert Love85b4aa42008-12-09 15:10:24 -08001204 }
1205
Joe Eykholt97c83892009-03-17 11:42:40 -07001206 if (unlikely(fh->fh_r_ctl == FC_RCTL_ELS_REQ) &&
Chris Leech3fe9a0b2009-08-25 13:59:46 -07001207 fcoe_ctlr_els_send(&fcoe->ctlr, skb))
Joe Eykholt97c83892009-03-17 11:42:40 -07001208 return 0;
1209
Robert Love85b4aa42008-12-09 15:10:24 -08001210 sof = fr_sof(fp);
1211 eof = fr_eof(fp);
1212
Vasu Dev4e57e1c2009-05-06 10:52:46 -07001213 elen = sizeof(struct ethhdr);
Robert Love85b4aa42008-12-09 15:10:24 -08001214 hlen = sizeof(struct fcoe_hdr);
1215 tlen = sizeof(struct fcoe_crc_eof);
1216 wlen = (skb->len - tlen + sizeof(crc)) / FCOE_WORD_TO_BYTE;
1217
1218 /* crc offload */
1219 if (likely(lp->crc_offload)) {
Yi Zou39ca9a02009-02-27 14:07:15 -08001220 skb->ip_summed = CHECKSUM_PARTIAL;
Robert Love85b4aa42008-12-09 15:10:24 -08001221 skb->csum_start = skb_headroom(skb);
1222 skb->csum_offset = skb->len;
1223 crc = 0;
1224 } else {
1225 skb->ip_summed = CHECKSUM_NONE;
1226 crc = fcoe_fc_crc(fp);
1227 }
1228
Chris Leech014f5c32009-08-25 13:59:30 -07001229 /* copy port crc and eof to the skb buff */
Robert Love85b4aa42008-12-09 15:10:24 -08001230 if (skb_is_nonlinear(skb)) {
1231 skb_frag_t *frag;
1232 if (fcoe_get_paged_crc_eof(skb, tlen)) {
Roel Kluine9041582009-02-27 10:56:22 -08001233 kfree_skb(skb);
Robert Love85b4aa42008-12-09 15:10:24 -08001234 return -ENOMEM;
1235 }
1236 frag = &skb_shinfo(skb)->frags[skb_shinfo(skb)->nr_frags - 1];
1237 cp = kmap_atomic(frag->page, KM_SKB_DATA_SOFTIRQ)
1238 + frag->page_offset;
1239 } else {
1240 cp = (struct fcoe_crc_eof *)skb_put(skb, tlen);
1241 }
1242
1243 memset(cp, 0, sizeof(*cp));
1244 cp->fcoe_eof = eof;
1245 cp->fcoe_crc32 = cpu_to_le32(~crc);
1246
1247 if (skb_is_nonlinear(skb)) {
1248 kunmap_atomic(cp, KM_SKB_DATA_SOFTIRQ);
1249 cp = NULL;
1250 }
1251
Chris Leech014f5c32009-08-25 13:59:30 -07001252 /* adjust skb network/transport offsets to match mac/fcoe/port */
Robert Love85b4aa42008-12-09 15:10:24 -08001253 skb_push(skb, elen + hlen);
1254 skb_reset_mac_header(skb);
1255 skb_reset_network_header(skb);
1256 skb->mac_len = elen;
Yi Zou211c7382009-02-27 14:06:37 -08001257 skb->protocol = htons(ETH_P_FCOE);
Chris Leech3fe9a0b2009-08-25 13:59:46 -07001258 skb->dev = fcoe->netdev;
Robert Love85b4aa42008-12-09 15:10:24 -08001259
1260 /* fill up mac and fcoe headers */
1261 eh = eth_hdr(skb);
1262 eh->h_proto = htons(ETH_P_FCOE);
Chris Leech3fe9a0b2009-08-25 13:59:46 -07001263 if (fcoe->ctlr.map_dest)
Robert Love85b4aa42008-12-09 15:10:24 -08001264 fc_fcoe_set_mac(eh->h_dest, fh->fh_d_id);
1265 else
1266 /* insert GW address */
Chris Leech3fe9a0b2009-08-25 13:59:46 -07001267 memcpy(eh->h_dest, fcoe->ctlr.dest_addr, ETH_ALEN);
Robert Love85b4aa42008-12-09 15:10:24 -08001268
Chris Leech3fe9a0b2009-08-25 13:59:46 -07001269 if (unlikely(fcoe->ctlr.flogi_oxid != FC_XID_UNKNOWN))
1270 memcpy(eh->h_source, fcoe->ctlr.ctl_src_addr, ETH_ALEN);
Robert Love85b4aa42008-12-09 15:10:24 -08001271 else
Chris Leech3fe9a0b2009-08-25 13:59:46 -07001272 memcpy(eh->h_source, fcoe->ctlr.data_src_addr, ETH_ALEN);
Robert Love85b4aa42008-12-09 15:10:24 -08001273
1274 hp = (struct fcoe_hdr *)(eh + 1);
1275 memset(hp, 0, sizeof(*hp));
1276 if (FC_FCOE_VER)
1277 FC_FCOE_ENCAPS_VER(hp, FC_FCOE_VER);
1278 hp->fcoe_sof = sof;
1279
Yi Zou39ca9a02009-02-27 14:07:15 -08001280 /* fcoe lso, mss is in max_payload which is non-zero for FCP data */
1281 if (lp->seq_offload && fr_max_payload(fp)) {
1282 skb_shinfo(skb)->gso_type = SKB_GSO_FCOE;
1283 skb_shinfo(skb)->gso_size = fr_max_payload(fp);
1284 } else {
1285 skb_shinfo(skb)->gso_type = 0;
1286 skb_shinfo(skb)->gso_size = 0;
1287 }
Robert Love85b4aa42008-12-09 15:10:24 -08001288 /* update tx stats: regardless if LLD fails */
Robert Love582b45b2009-03-31 15:51:50 -07001289 stats = fc_lport_get_stats(lp);
1290 stats->TxFrames++;
1291 stats->TxWords += wlen;
Robert Love85b4aa42008-12-09 15:10:24 -08001292
1293 /* send down to lld */
1294 fr_dev(fp) = lp;
Chris Leech014f5c32009-08-25 13:59:30 -07001295 if (port->fcoe_pending_queue.qlen)
Vasu Dev4bb6b512009-05-06 10:52:34 -07001296 fcoe_check_wait_queue(lp, skb);
1297 else if (fcoe_start_io(skb))
1298 fcoe_check_wait_queue(lp, skb);
Robert Love85b4aa42008-12-09 15:10:24 -08001299
1300 return 0;
1301}
Robert Love85b4aa42008-12-09 15:10:24 -08001302
Robert Love34f42a02009-02-27 10:55:45 -08001303/**
1304 * fcoe_percpu_receive_thread() - recv thread per cpu
Robert Love85b4aa42008-12-09 15:10:24 -08001305 * @arg: ptr to the fcoe per cpu struct
1306 *
1307 * Return: 0 for success
Robert Love85b4aa42008-12-09 15:10:24 -08001308 */
1309int fcoe_percpu_receive_thread(void *arg)
1310{
1311 struct fcoe_percpu_s *p = arg;
1312 u32 fr_len;
1313 struct fc_lport *lp;
1314 struct fcoe_rcv_info *fr;
1315 struct fcoe_dev_stats *stats;
1316 struct fc_frame_header *fh;
1317 struct sk_buff *skb;
1318 struct fcoe_crc_eof crc_eof;
1319 struct fc_frame *fp;
1320 u8 *mac = NULL;
Chris Leech014f5c32009-08-25 13:59:30 -07001321 struct fcoe_port *port;
Robert Love85b4aa42008-12-09 15:10:24 -08001322 struct fcoe_hdr *hp;
1323
Robert Love4469c192009-02-27 10:56:38 -08001324 set_user_nice(current, -20);
Robert Love85b4aa42008-12-09 15:10:24 -08001325
1326 while (!kthread_should_stop()) {
1327
1328 spin_lock_bh(&p->fcoe_rx_list.lock);
1329 while ((skb = __skb_dequeue(&p->fcoe_rx_list)) == NULL) {
1330 set_current_state(TASK_INTERRUPTIBLE);
1331 spin_unlock_bh(&p->fcoe_rx_list.lock);
1332 schedule();
1333 set_current_state(TASK_RUNNING);
1334 if (kthread_should_stop())
1335 return 0;
1336 spin_lock_bh(&p->fcoe_rx_list.lock);
1337 }
1338 spin_unlock_bh(&p->fcoe_rx_list.lock);
1339 fr = fcoe_dev_from_skb(skb);
1340 lp = fr->fr_dev;
1341 if (unlikely(lp == NULL)) {
Robert Loved5488eb2009-06-10 15:30:59 -07001342 FCOE_NETDEV_DBG(skb->dev, "Invalid HBA Structure");
Robert Love85b4aa42008-12-09 15:10:24 -08001343 kfree_skb(skb);
1344 continue;
1345 }
1346
Robert Loved5488eb2009-06-10 15:30:59 -07001347 FCOE_NETDEV_DBG(skb->dev, "skb_info: len:%d data_len:%d "
1348 "head:%p data:%p tail:%p end:%p sum:%d dev:%s",
1349 skb->len, skb->data_len,
1350 skb->head, skb->data, skb_tail_pointer(skb),
1351 skb_end_pointer(skb), skb->csum,
1352 skb->dev ? skb->dev->name : "<NULL>");
Robert Love85b4aa42008-12-09 15:10:24 -08001353
1354 /*
1355 * Save source MAC address before discarding header.
1356 */
Chris Leech014f5c32009-08-25 13:59:30 -07001357 port = lport_priv(lp);
Robert Love85b4aa42008-12-09 15:10:24 -08001358 if (skb_is_nonlinear(skb))
1359 skb_linearize(skb); /* not ideal */
Joe Eykholt97c83892009-03-17 11:42:40 -07001360 mac = eth_hdr(skb)->h_source;
Robert Love85b4aa42008-12-09 15:10:24 -08001361
1362 /*
1363 * Frame length checks and setting up the header pointers
1364 * was done in fcoe_rcv already.
1365 */
1366 hp = (struct fcoe_hdr *) skb_network_header(skb);
1367 fh = (struct fc_frame_header *) skb_transport_header(skb);
1368
Robert Love582b45b2009-03-31 15:51:50 -07001369 stats = fc_lport_get_stats(lp);
Robert Love85b4aa42008-12-09 15:10:24 -08001370 if (unlikely(FC_FCOE_DECAPS_VER(hp) != FC_FCOE_VER)) {
Robert Love582b45b2009-03-31 15:51:50 -07001371 if (stats->ErrorFrames < 5)
Robert Loved5488eb2009-06-10 15:30:59 -07001372 printk(KERN_WARNING "fcoe: FCoE version "
Robert Love582b45b2009-03-31 15:51:50 -07001373 "mismatch: The frame has "
1374 "version %x, but the "
1375 "initiator supports version "
1376 "%x\n", FC_FCOE_DECAPS_VER(hp),
1377 FC_FCOE_VER);
1378 stats->ErrorFrames++;
Robert Love85b4aa42008-12-09 15:10:24 -08001379 kfree_skb(skb);
1380 continue;
1381 }
1382
1383 skb_pull(skb, sizeof(struct fcoe_hdr));
1384 fr_len = skb->len - sizeof(struct fcoe_crc_eof);
1385
Robert Love582b45b2009-03-31 15:51:50 -07001386 stats->RxFrames++;
1387 stats->RxWords += fr_len / FCOE_WORD_TO_BYTE;
Robert Love85b4aa42008-12-09 15:10:24 -08001388
1389 fp = (struct fc_frame *)skb;
1390 fc_frame_init(fp);
1391 fr_dev(fp) = lp;
1392 fr_sof(fp) = hp->fcoe_sof;
1393
1394 /* Copy out the CRC and EOF trailer for access */
1395 if (skb_copy_bits(skb, fr_len, &crc_eof, sizeof(crc_eof))) {
1396 kfree_skb(skb);
1397 continue;
1398 }
1399 fr_eof(fp) = crc_eof.fcoe_eof;
1400 fr_crc(fp) = crc_eof.fcoe_crc32;
1401 if (pskb_trim(skb, fr_len)) {
1402 kfree_skb(skb);
1403 continue;
1404 }
1405
1406 /*
1407 * We only check CRC if no offload is available and if it is
1408 * it's solicited data, in which case, the FCP layer would
1409 * check it during the copy.
1410 */
Yi Zou07c00ec2009-02-27 14:07:31 -08001411 if (lp->crc_offload && skb->ip_summed == CHECKSUM_UNNECESSARY)
Robert Love85b4aa42008-12-09 15:10:24 -08001412 fr_flags(fp) &= ~FCPHF_CRC_UNCHECKED;
1413 else
1414 fr_flags(fp) |= FCPHF_CRC_UNCHECKED;
1415
1416 fh = fc_frame_header_get(fp);
1417 if (fh->fh_r_ctl == FC_RCTL_DD_SOL_DATA &&
1418 fh->fh_type == FC_TYPE_FCP) {
Vasu Dev52ff8782009-07-29 17:05:10 -07001419 fc_exch_recv(lp, fp);
Robert Love85b4aa42008-12-09 15:10:24 -08001420 continue;
1421 }
1422 if (fr_flags(fp) & FCPHF_CRC_UNCHECKED) {
1423 if (le32_to_cpu(fr_crc(fp)) !=
1424 ~crc32(~0, skb->data, fr_len)) {
Robert Loved5488eb2009-06-10 15:30:59 -07001425 if (stats->InvalidCRCCount < 5)
Robert Love85b4aa42008-12-09 15:10:24 -08001426 printk(KERN_WARNING "fcoe: dropping "
1427 "frame with CRC error\n");
1428 stats->InvalidCRCCount++;
1429 stats->ErrorFrames++;
1430 fc_frame_free(fp);
1431 continue;
1432 }
1433 fr_flags(fp) &= ~FCPHF_CRC_UNCHECKED;
1434 }
Chris Leech3fe9a0b2009-08-25 13:59:46 -07001435 if (unlikely(port->fcoe->ctlr.flogi_oxid != FC_XID_UNKNOWN) &&
1436 fcoe_ctlr_recv_flogi(&port->fcoe->ctlr, fp, mac)) {
Joe Eykholt97c83892009-03-17 11:42:40 -07001437 fc_frame_free(fp);
1438 continue;
1439 }
Vasu Dev52ff8782009-07-29 17:05:10 -07001440 fc_exch_recv(lp, fp);
Robert Love85b4aa42008-12-09 15:10:24 -08001441 }
1442 return 0;
1443}
1444
1445/**
Chris Leechdd3fd722009-04-21 16:27:36 -07001446 * fcoe_check_wait_queue() - attempt to clear the transmit backlog
1447 * @lp: the fc_lport
Robert Love85b4aa42008-12-09 15:10:24 -08001448 *
1449 * This empties the wait_queue, dequeue the head of the wait_queue queue
1450 * and calls fcoe_start_io() for each packet, if all skb have been
Vasu Devc826a312009-02-27 10:56:27 -08001451 * transmitted, return qlen or -1 if a error occurs, then restore
Chris Leechdd3fd722009-04-21 16:27:36 -07001452 * wait_queue and try again later.
Robert Love85b4aa42008-12-09 15:10:24 -08001453 *
1454 * The wait_queue is used when the skb transmit fails. skb will go
Chris Leechdd3fd722009-04-21 16:27:36 -07001455 * in the wait_queue which will be emptied by the timer function or
Robert Love85b4aa42008-12-09 15:10:24 -08001456 * by the next skb transmit.
Robert Love34f42a02009-02-27 10:55:45 -08001457 */
Vasu Dev4bb6b512009-05-06 10:52:34 -07001458static void fcoe_check_wait_queue(struct fc_lport *lp, struct sk_buff *skb)
Robert Love85b4aa42008-12-09 15:10:24 -08001459{
Chris Leech014f5c32009-08-25 13:59:30 -07001460 struct fcoe_port *port = lport_priv(lp);
Vasu Dev4bb6b512009-05-06 10:52:34 -07001461 int rc;
Robert Love85b4aa42008-12-09 15:10:24 -08001462
Chris Leech014f5c32009-08-25 13:59:30 -07001463 spin_lock_bh(&port->fcoe_pending_queue.lock);
Vasu Dev4bb6b512009-05-06 10:52:34 -07001464
1465 if (skb)
Chris Leech014f5c32009-08-25 13:59:30 -07001466 __skb_queue_tail(&port->fcoe_pending_queue, skb);
Vasu Dev4bb6b512009-05-06 10:52:34 -07001467
Chris Leech014f5c32009-08-25 13:59:30 -07001468 if (port->fcoe_pending_queue_active)
Vasu Devc826a312009-02-27 10:56:27 -08001469 goto out;
Chris Leech014f5c32009-08-25 13:59:30 -07001470 port->fcoe_pending_queue_active = 1;
Chris Leech55c8baf2009-02-27 10:56:32 -08001471
Chris Leech014f5c32009-08-25 13:59:30 -07001472 while (port->fcoe_pending_queue.qlen) {
Chris Leech55c8baf2009-02-27 10:56:32 -08001473 /* keep qlen > 0 until fcoe_start_io succeeds */
Chris Leech014f5c32009-08-25 13:59:30 -07001474 port->fcoe_pending_queue.qlen++;
1475 skb = __skb_dequeue(&port->fcoe_pending_queue);
Chris Leech55c8baf2009-02-27 10:56:32 -08001476
Chris Leech014f5c32009-08-25 13:59:30 -07001477 spin_unlock_bh(&port->fcoe_pending_queue.lock);
Chris Leech55c8baf2009-02-27 10:56:32 -08001478 rc = fcoe_start_io(skb);
Chris Leech014f5c32009-08-25 13:59:30 -07001479 spin_lock_bh(&port->fcoe_pending_queue.lock);
Chris Leech55c8baf2009-02-27 10:56:32 -08001480
1481 if (rc) {
Chris Leech014f5c32009-08-25 13:59:30 -07001482 __skb_queue_head(&port->fcoe_pending_queue, skb);
Chris Leech55c8baf2009-02-27 10:56:32 -08001483 /* undo temporary increment above */
Chris Leech014f5c32009-08-25 13:59:30 -07001484 port->fcoe_pending_queue.qlen--;
Chris Leech55c8baf2009-02-27 10:56:32 -08001485 break;
Robert Love85b4aa42008-12-09 15:10:24 -08001486 }
Chris Leech55c8baf2009-02-27 10:56:32 -08001487 /* undo temporary increment above */
Chris Leech014f5c32009-08-25 13:59:30 -07001488 port->fcoe_pending_queue.qlen--;
Robert Love85b4aa42008-12-09 15:10:24 -08001489 }
Chris Leech55c8baf2009-02-27 10:56:32 -08001490
Chris Leech014f5c32009-08-25 13:59:30 -07001491 if (port->fcoe_pending_queue.qlen < FCOE_LOW_QUEUE_DEPTH)
Chris Leech55c8baf2009-02-27 10:56:32 -08001492 lp->qfull = 0;
Chris Leech014f5c32009-08-25 13:59:30 -07001493 if (port->fcoe_pending_queue.qlen && !timer_pending(&port->timer))
1494 mod_timer(&port->timer, jiffies + 2);
1495 port->fcoe_pending_queue_active = 0;
Vasu Devc826a312009-02-27 10:56:27 -08001496out:
Chris Leech014f5c32009-08-25 13:59:30 -07001497 if (port->fcoe_pending_queue.qlen > FCOE_MAX_QUEUE_DEPTH)
Vasu Dev4bb6b512009-05-06 10:52:34 -07001498 lp->qfull = 1;
Chris Leech014f5c32009-08-25 13:59:30 -07001499 spin_unlock_bh(&port->fcoe_pending_queue.lock);
Vasu Dev4bb6b512009-05-06 10:52:34 -07001500 return;
Robert Love85b4aa42008-12-09 15:10:24 -08001501}
1502
1503/**
Robert Love34f42a02009-02-27 10:55:45 -08001504 * fcoe_dev_setup() - setup link change notification interface
1505 */
Randy Dunlapb0d428a2009-04-27 21:49:31 -07001506static void fcoe_dev_setup(void)
Robert Love85b4aa42008-12-09 15:10:24 -08001507{
Robert Love85b4aa42008-12-09 15:10:24 -08001508 register_netdevice_notifier(&fcoe_notifier);
1509}
1510
1511/**
Randy Dunlapb0d428a2009-04-27 21:49:31 -07001512 * fcoe_dev_cleanup() - cleanup link change notification interface
Robert Love34f42a02009-02-27 10:55:45 -08001513 */
Robert Love85b4aa42008-12-09 15:10:24 -08001514static void fcoe_dev_cleanup(void)
1515{
1516 unregister_netdevice_notifier(&fcoe_notifier);
1517}
1518
1519/**
Robert Love34f42a02009-02-27 10:55:45 -08001520 * fcoe_device_notification() - netdev event notification callback
Robert Love85b4aa42008-12-09 15:10:24 -08001521 * @notifier: context of the notification
1522 * @event: type of event
1523 * @ptr: fixed array for output parsed ifname
1524 *
1525 * This function is called by the ethernet driver in case of link change event
1526 *
1527 * Returns: 0 for success
Robert Love34f42a02009-02-27 10:55:45 -08001528 */
Robert Love85b4aa42008-12-09 15:10:24 -08001529static int fcoe_device_notification(struct notifier_block *notifier,
1530 ulong event, void *ptr)
1531{
1532 struct fc_lport *lp = NULL;
Vasu Dev1d1b88d2009-07-29 17:05:45 -07001533 struct net_device *netdev = ptr;
Chris Leech014f5c32009-08-25 13:59:30 -07001534 struct fcoe_interface *fcoe;
Robert Love85b4aa42008-12-09 15:10:24 -08001535 struct fcoe_dev_stats *stats;
Joe Eykholt97c83892009-03-17 11:42:40 -07001536 u32 link_possible = 1;
Robert Love85b4aa42008-12-09 15:10:24 -08001537 u32 mfs;
1538 int rc = NOTIFY_OK;
1539
1540 read_lock(&fcoe_hostlist_lock);
Chris Leech014f5c32009-08-25 13:59:30 -07001541 list_for_each_entry(fcoe, &fcoe_hostlist, list) {
Chris Leech25024982009-08-25 13:59:35 -07001542 if (fcoe->netdev == netdev) {
Chris Leech3fe9a0b2009-08-25 13:59:46 -07001543 lp = fcoe->ctlr.lp;
Robert Love85b4aa42008-12-09 15:10:24 -08001544 break;
1545 }
1546 }
1547 read_unlock(&fcoe_hostlist_lock);
1548 if (lp == NULL) {
1549 rc = NOTIFY_DONE;
1550 goto out;
1551 }
1552
Robert Love85b4aa42008-12-09 15:10:24 -08001553 switch (event) {
1554 case NETDEV_DOWN:
1555 case NETDEV_GOING_DOWN:
Joe Eykholt97c83892009-03-17 11:42:40 -07001556 link_possible = 0;
Robert Love85b4aa42008-12-09 15:10:24 -08001557 break;
1558 case NETDEV_UP:
1559 case NETDEV_CHANGE:
Robert Love85b4aa42008-12-09 15:10:24 -08001560 break;
1561 case NETDEV_CHANGEMTU:
Vasu Dev1d1b88d2009-07-29 17:05:45 -07001562 mfs = netdev->mtu - (sizeof(struct fcoe_hdr) +
1563 sizeof(struct fcoe_crc_eof));
Robert Love85b4aa42008-12-09 15:10:24 -08001564 if (mfs >= FC_MIN_MAX_FRAME)
1565 fc_set_mfs(lp, mfs);
Robert Love85b4aa42008-12-09 15:10:24 -08001566 break;
1567 case NETDEV_REGISTER:
1568 break;
1569 default:
Vasu Dev1d1b88d2009-07-29 17:05:45 -07001570 FCOE_NETDEV_DBG(netdev, "Unknown event %ld "
Robert Loved5488eb2009-06-10 15:30:59 -07001571 "from netdev netlink\n", event);
Robert Love85b4aa42008-12-09 15:10:24 -08001572 }
Joe Eykholt97c83892009-03-17 11:42:40 -07001573 if (link_possible && !fcoe_link_ok(lp))
Chris Leech3fe9a0b2009-08-25 13:59:46 -07001574 fcoe_ctlr_link_up(&fcoe->ctlr);
1575 else if (fcoe_ctlr_link_down(&fcoe->ctlr)) {
Joe Eykholt97c83892009-03-17 11:42:40 -07001576 stats = fc_lport_get_stats(lp);
1577 stats->LinkFailureCount++;
1578 fcoe_clean_pending_queue(lp);
Robert Love85b4aa42008-12-09 15:10:24 -08001579 }
1580out:
1581 return rc;
1582}
1583
1584/**
Robert Love34f42a02009-02-27 10:55:45 -08001585 * fcoe_if_to_netdev() - parse a name buffer to get netdev
Robert Love85b4aa42008-12-09 15:10:24 -08001586 * @buffer: incoming buffer to be copied
1587 *
Chris Leechdd3fd722009-04-21 16:27:36 -07001588 * Returns: NULL or ptr to net_device
Robert Love34f42a02009-02-27 10:55:45 -08001589 */
Robert Love85b4aa42008-12-09 15:10:24 -08001590static struct net_device *fcoe_if_to_netdev(const char *buffer)
1591{
1592 char *cp;
1593 char ifname[IFNAMSIZ + 2];
1594
1595 if (buffer) {
1596 strlcpy(ifname, buffer, IFNAMSIZ);
1597 cp = ifname + strlen(ifname);
1598 while (--cp >= ifname && *cp == '\n')
1599 *cp = '\0';
1600 return dev_get_by_name(&init_net, ifname);
1601 }
1602 return NULL;
1603}
1604
1605/**
Chris Leechdd3fd722009-04-21 16:27:36 -07001606 * fcoe_netdev_to_module_owner() - finds out the driver module of the netdev
Robert Love85b4aa42008-12-09 15:10:24 -08001607 * @netdev: the target netdev
1608 *
1609 * Returns: ptr to the struct module, NULL for failure
Robert Love34f42a02009-02-27 10:55:45 -08001610 */
Robert Loveb2ab99c2009-02-27 10:55:50 -08001611static struct module *
1612fcoe_netdev_to_module_owner(const struct net_device *netdev)
Robert Love85b4aa42008-12-09 15:10:24 -08001613{
1614 struct device *dev;
1615
1616 if (!netdev)
1617 return NULL;
1618
1619 dev = netdev->dev.parent;
1620 if (!dev)
1621 return NULL;
1622
1623 if (!dev->driver)
1624 return NULL;
1625
1626 return dev->driver->owner;
1627}
1628
1629/**
Robert Love34f42a02009-02-27 10:55:45 -08001630 * fcoe_ethdrv_get() - Hold the Ethernet driver
Robert Love85b4aa42008-12-09 15:10:24 -08001631 * @netdev: the target netdev
1632 *
Robert Love34f42a02009-02-27 10:55:45 -08001633 * Holds the Ethernet driver module by try_module_get() for
1634 * the corresponding netdev.
1635 *
Chris Leechdd3fd722009-04-21 16:27:36 -07001636 * Returns: 0 for success
Robert Love34f42a02009-02-27 10:55:45 -08001637 */
Robert Love85b4aa42008-12-09 15:10:24 -08001638static int fcoe_ethdrv_get(const struct net_device *netdev)
1639{
1640 struct module *owner;
1641
1642 owner = fcoe_netdev_to_module_owner(netdev);
1643 if (owner) {
Robert Loved5488eb2009-06-10 15:30:59 -07001644 FCOE_NETDEV_DBG(netdev, "Hold driver module %s\n",
1645 module_name(owner));
Robert Love85b4aa42008-12-09 15:10:24 -08001646 return try_module_get(owner);
1647 }
1648 return -ENODEV;
1649}
1650
1651/**
Robert Love34f42a02009-02-27 10:55:45 -08001652 * fcoe_ethdrv_put() - Release the Ethernet driver
Robert Love85b4aa42008-12-09 15:10:24 -08001653 * @netdev: the target netdev
1654 *
Robert Love34f42a02009-02-27 10:55:45 -08001655 * Releases the Ethernet driver module by module_put for
1656 * the corresponding netdev.
1657 *
Chris Leechdd3fd722009-04-21 16:27:36 -07001658 * Returns: 0 for success
Robert Love34f42a02009-02-27 10:55:45 -08001659 */
Robert Love85b4aa42008-12-09 15:10:24 -08001660static int fcoe_ethdrv_put(const struct net_device *netdev)
1661{
1662 struct module *owner;
1663
1664 owner = fcoe_netdev_to_module_owner(netdev);
1665 if (owner) {
Robert Loved5488eb2009-06-10 15:30:59 -07001666 FCOE_NETDEV_DBG(netdev, "Release driver module %s\n",
1667 module_name(owner));
Robert Love85b4aa42008-12-09 15:10:24 -08001668 module_put(owner);
1669 return 0;
1670 }
1671 return -ENODEV;
1672}
1673
1674/**
Robert Love34f42a02009-02-27 10:55:45 -08001675 * fcoe_destroy() - handles the destroy from sysfs
Chris Leechdd3fd722009-04-21 16:27:36 -07001676 * @buffer: expected to be an eth if name
Robert Love85b4aa42008-12-09 15:10:24 -08001677 * @kp: associated kernel param
1678 *
1679 * Returns: 0 for success
Robert Love34f42a02009-02-27 10:55:45 -08001680 */
Robert Love85b4aa42008-12-09 15:10:24 -08001681static int fcoe_destroy(const char *buffer, struct kernel_param *kp)
1682{
Robert Love85b4aa42008-12-09 15:10:24 -08001683 struct net_device *netdev;
Chris Leech030f4e02009-08-25 14:00:02 -07001684 struct fcoe_interface *fcoe;
1685 struct fcoe_port *port;
Chris Leechaf7f85d2009-08-25 13:59:24 -07001686 struct fc_lport *lport;
1687 int rc;
Robert Love85b4aa42008-12-09 15:10:24 -08001688
1689 netdev = fcoe_if_to_netdev(buffer);
1690 if (!netdev) {
1691 rc = -ENODEV;
1692 goto out_nodev;
1693 }
1694 /* look for existing lport */
Chris Leechaf7f85d2009-08-25 13:59:24 -07001695 lport = fcoe_hostlist_lookup(netdev);
1696 if (!lport) {
Robert Love85b4aa42008-12-09 15:10:24 -08001697 rc = -ENODEV;
1698 goto out_putdev;
1699 }
Chris Leech030f4e02009-08-25 14:00:02 -07001700 port = lport_priv(lport);
1701 fcoe = port->fcoe;
Chris Leechaf7f85d2009-08-25 13:59:24 -07001702 fcoe_if_destroy(lport);
Robert Love85b4aa42008-12-09 15:10:24 -08001703 fcoe_ethdrv_put(netdev);
1704 rc = 0;
1705out_putdev:
1706 dev_put(netdev);
1707out_nodev:
1708 return rc;
1709}
1710
1711/**
Robert Love34f42a02009-02-27 10:55:45 -08001712 * fcoe_create() - Handles the create call from sysfs
Chris Leechdd3fd722009-04-21 16:27:36 -07001713 * @buffer: expected to be an eth if name
Robert Love85b4aa42008-12-09 15:10:24 -08001714 * @kp: associated kernel param
1715 *
1716 * Returns: 0 for success
Robert Love34f42a02009-02-27 10:55:45 -08001717 */
Robert Love85b4aa42008-12-09 15:10:24 -08001718static int fcoe_create(const char *buffer, struct kernel_param *kp)
1719{
1720 int rc;
Chris Leech030f4e02009-08-25 14:00:02 -07001721 struct fcoe_interface *fcoe;
Chris Leechaf7f85d2009-08-25 13:59:24 -07001722 struct fc_lport *lport;
Robert Love85b4aa42008-12-09 15:10:24 -08001723 struct net_device *netdev;
1724
1725 netdev = fcoe_if_to_netdev(buffer);
1726 if (!netdev) {
1727 rc = -ENODEV;
1728 goto out_nodev;
1729 }
1730 /* look for existing lport */
1731 if (fcoe_hostlist_lookup(netdev)) {
1732 rc = -EEXIST;
1733 goto out_putdev;
1734 }
1735 fcoe_ethdrv_get(netdev);
1736
Chris Leech030f4e02009-08-25 14:00:02 -07001737 fcoe = fcoe_interface_create(netdev);
1738 if (!fcoe) {
1739 rc = -ENOMEM;
1740 goto out_putdev;
1741 }
1742
1743 lport = fcoe_if_create(fcoe, &netdev->dev);
Chris Leechaf7f85d2009-08-25 13:59:24 -07001744 if (IS_ERR(lport)) {
Robert Loved5488eb2009-06-10 15:30:59 -07001745 printk(KERN_ERR "fcoe: Failed to create interface (%s)\n",
Robert Love85b4aa42008-12-09 15:10:24 -08001746 netdev->name);
1747 fcoe_ethdrv_put(netdev);
1748 rc = -EIO;
Chris Leech030f4e02009-08-25 14:00:02 -07001749 goto out_free;
Robert Love85b4aa42008-12-09 15:10:24 -08001750 }
Chris Leech030f4e02009-08-25 14:00:02 -07001751
Chris Leech54b649f2009-08-25 14:00:07 -07001752 /* Make this the "master" N_Port */
1753 fcoe->ctlr.lp = lport;
Chris Leech030f4e02009-08-25 14:00:02 -07001754
Chris Leech54b649f2009-08-25 14:00:07 -07001755 /* start FIP Discovery and FLOGI */
1756 lport->boot_time = jiffies;
1757 fc_fabric_login(lport);
1758 if (!fcoe_link_ok(lport))
1759 fcoe_ctlr_link_up(&fcoe->ctlr);
1760
1761 rc = 0;
Chris Leech030f4e02009-08-25 14:00:02 -07001762out_free:
Chris Leech54b649f2009-08-25 14:00:07 -07001763 /*
1764 * Release from init in fcoe_interface_create(), on success lport
1765 * should be holding a reference taken in fcoe_if_create().
1766 */
Chris Leech030f4e02009-08-25 14:00:02 -07001767 fcoe_interface_put(fcoe);
Robert Love85b4aa42008-12-09 15:10:24 -08001768out_putdev:
1769 dev_put(netdev);
1770out_nodev:
1771 return rc;
1772}
1773
1774module_param_call(create, fcoe_create, NULL, NULL, S_IWUSR);
1775__MODULE_PARM_TYPE(create, "string");
Chris Leech014f5c32009-08-25 13:59:30 -07001776MODULE_PARM_DESC(create, "Create fcoe fcoe using net device passed in.");
Robert Love85b4aa42008-12-09 15:10:24 -08001777module_param_call(destroy, fcoe_destroy, NULL, NULL, S_IWUSR);
1778__MODULE_PARM_TYPE(destroy, "string");
Chris Leech014f5c32009-08-25 13:59:30 -07001779MODULE_PARM_DESC(destroy, "Destroy fcoe fcoe");
Robert Love85b4aa42008-12-09 15:10:24 -08001780
Robert Love34f42a02009-02-27 10:55:45 -08001781/**
1782 * fcoe_link_ok() - Check if link is ok for the fc_lport
Robert Love85b4aa42008-12-09 15:10:24 -08001783 * @lp: ptr to the fc_lport
1784 *
1785 * Any permanently-disqualifying conditions have been previously checked.
1786 * This also updates the speed setting, which may change with link for 100/1000.
1787 *
1788 * This function should probably be checking for PAUSE support at some point
1789 * in the future. Currently Per-priority-pause is not determinable using
1790 * ethtool, so we shouldn't be restrictive until that problem is resolved.
1791 *
1792 * Returns: 0 if link is OK for use by FCoE.
1793 *
1794 */
1795int fcoe_link_ok(struct fc_lport *lp)
1796{
Chris Leech014f5c32009-08-25 13:59:30 -07001797 struct fcoe_port *port = lport_priv(lp);
Chris Leech25024982009-08-25 13:59:35 -07001798 struct net_device *dev = port->fcoe->netdev;
Robert Love85b4aa42008-12-09 15:10:24 -08001799 struct ethtool_cmd ecmd = { ETHTOOL_GSET };
Robert Love85b4aa42008-12-09 15:10:24 -08001800
Yi Zou2f718d62009-07-29 17:04:01 -07001801 if ((dev->flags & IFF_UP) && netif_carrier_ok(dev) &&
1802 (!dev_ethtool_get_settings(dev, &ecmd))) {
1803 lp->link_supported_speeds &=
1804 ~(FC_PORTSPEED_1GBIT | FC_PORTSPEED_10GBIT);
1805 if (ecmd.supported & (SUPPORTED_1000baseT_Half |
1806 SUPPORTED_1000baseT_Full))
1807 lp->link_supported_speeds |= FC_PORTSPEED_1GBIT;
1808 if (ecmd.supported & SUPPORTED_10000baseT_Full)
1809 lp->link_supported_speeds |=
1810 FC_PORTSPEED_10GBIT;
1811 if (ecmd.speed == SPEED_1000)
1812 lp->link_speed = FC_PORTSPEED_1GBIT;
1813 if (ecmd.speed == SPEED_10000)
1814 lp->link_speed = FC_PORTSPEED_10GBIT;
Robert Love85b4aa42008-12-09 15:10:24 -08001815
Yi Zou2f718d62009-07-29 17:04:01 -07001816 return 0;
1817 }
1818 return -1;
Robert Love85b4aa42008-12-09 15:10:24 -08001819}
Robert Love85b4aa42008-12-09 15:10:24 -08001820
Robert Love34f42a02009-02-27 10:55:45 -08001821/**
1822 * fcoe_percpu_clean() - Clear the pending skbs for an lport
Robert Love85b4aa42008-12-09 15:10:24 -08001823 * @lp: the fc_lport
1824 */
1825void fcoe_percpu_clean(struct fc_lport *lp)
1826{
Robert Love85b4aa42008-12-09 15:10:24 -08001827 struct fcoe_percpu_s *pp;
1828 struct fcoe_rcv_info *fr;
1829 struct sk_buff_head *list;
1830 struct sk_buff *skb, *next;
1831 struct sk_buff *head;
Robert Love5e5e92d2009-03-17 11:41:35 -07001832 unsigned int cpu;
Robert Love85b4aa42008-12-09 15:10:24 -08001833
Robert Love5e5e92d2009-03-17 11:41:35 -07001834 for_each_possible_cpu(cpu) {
1835 pp = &per_cpu(fcoe_percpu, cpu);
1836 spin_lock_bh(&pp->fcoe_rx_list.lock);
1837 list = &pp->fcoe_rx_list;
1838 head = list->next;
1839 for (skb = head; skb != (struct sk_buff *)list;
1840 skb = next) {
1841 next = skb->next;
1842 fr = fcoe_dev_from_skb(skb);
1843 if (fr->fr_dev == lp) {
1844 __skb_unlink(skb, list);
1845 kfree_skb(skb);
Robert Love85b4aa42008-12-09 15:10:24 -08001846 }
Robert Love85b4aa42008-12-09 15:10:24 -08001847 }
Robert Love5e5e92d2009-03-17 11:41:35 -07001848 spin_unlock_bh(&pp->fcoe_rx_list.lock);
Robert Love85b4aa42008-12-09 15:10:24 -08001849 }
1850}
Robert Love85b4aa42008-12-09 15:10:24 -08001851
1852/**
Robert Love34f42a02009-02-27 10:55:45 -08001853 * fcoe_clean_pending_queue() - Dequeue a skb and free it
Robert Love85b4aa42008-12-09 15:10:24 -08001854 * @lp: the corresponding fc_lport
1855 *
1856 * Returns: none
Robert Love34f42a02009-02-27 10:55:45 -08001857 */
Robert Love85b4aa42008-12-09 15:10:24 -08001858void fcoe_clean_pending_queue(struct fc_lport *lp)
1859{
Chris Leech014f5c32009-08-25 13:59:30 -07001860 struct fcoe_port *port = lport_priv(lp);
Robert Love85b4aa42008-12-09 15:10:24 -08001861 struct sk_buff *skb;
1862
Chris Leech014f5c32009-08-25 13:59:30 -07001863 spin_lock_bh(&port->fcoe_pending_queue.lock);
1864 while ((skb = __skb_dequeue(&port->fcoe_pending_queue)) != NULL) {
1865 spin_unlock_bh(&port->fcoe_pending_queue.lock);
Robert Love85b4aa42008-12-09 15:10:24 -08001866 kfree_skb(skb);
Chris Leech014f5c32009-08-25 13:59:30 -07001867 spin_lock_bh(&port->fcoe_pending_queue.lock);
Robert Love85b4aa42008-12-09 15:10:24 -08001868 }
Chris Leech014f5c32009-08-25 13:59:30 -07001869 spin_unlock_bh(&port->fcoe_pending_queue.lock);
Robert Love85b4aa42008-12-09 15:10:24 -08001870}
Robert Love85b4aa42008-12-09 15:10:24 -08001871
1872/**
Robert Love34f42a02009-02-27 10:55:45 -08001873 * fcoe_reset() - Resets the fcoe
Robert Love85b4aa42008-12-09 15:10:24 -08001874 * @shost: shost the reset is from
1875 *
1876 * Returns: always 0
1877 */
1878int fcoe_reset(struct Scsi_Host *shost)
1879{
1880 struct fc_lport *lport = shost_priv(shost);
1881 fc_lport_reset(lport);
1882 return 0;
1883}
Robert Love85b4aa42008-12-09 15:10:24 -08001884
Robert Love34f42a02009-02-27 10:55:45 -08001885/**
Chris Leech014f5c32009-08-25 13:59:30 -07001886 * fcoe_hostlist_lookup_port() - find the corresponding lport by a given device
Chris Leechdd3fd722009-04-21 16:27:36 -07001887 * @dev: this is currently ptr to net_device
Robert Love85b4aa42008-12-09 15:10:24 -08001888 *
Vasu Deve8af4d42009-07-29 17:05:15 -07001889 * Called with fcoe_hostlist_lock held.
1890 *
Chris Leech014f5c32009-08-25 13:59:30 -07001891 * Returns: NULL or the located fcoe_port
Robert Love85b4aa42008-12-09 15:10:24 -08001892 */
Chris Leech014f5c32009-08-25 13:59:30 -07001893static struct fcoe_interface *
1894fcoe_hostlist_lookup_port(const struct net_device *dev)
Robert Love85b4aa42008-12-09 15:10:24 -08001895{
Chris Leech014f5c32009-08-25 13:59:30 -07001896 struct fcoe_interface *fcoe;
Robert Love85b4aa42008-12-09 15:10:24 -08001897
Chris Leech014f5c32009-08-25 13:59:30 -07001898 list_for_each_entry(fcoe, &fcoe_hostlist, list) {
Chris Leech25024982009-08-25 13:59:35 -07001899 if (fcoe->netdev == dev)
Chris Leech014f5c32009-08-25 13:59:30 -07001900 return fcoe;
Robert Love85b4aa42008-12-09 15:10:24 -08001901 }
Robert Love85b4aa42008-12-09 15:10:24 -08001902 return NULL;
1903}
1904
Robert Love34f42a02009-02-27 10:55:45 -08001905/**
1906 * fcoe_hostlist_lookup() - Find the corresponding lport by netdev
Robert Love85b4aa42008-12-09 15:10:24 -08001907 * @netdev: ptr to net_device
1908 *
1909 * Returns: 0 for success
1910 */
1911struct fc_lport *fcoe_hostlist_lookup(const struct net_device *netdev)
1912{
Chris Leech014f5c32009-08-25 13:59:30 -07001913 struct fcoe_interface *fcoe;
Robert Love85b4aa42008-12-09 15:10:24 -08001914
Vasu Deve8af4d42009-07-29 17:05:15 -07001915 read_lock(&fcoe_hostlist_lock);
Chris Leech014f5c32009-08-25 13:59:30 -07001916 fcoe = fcoe_hostlist_lookup_port(netdev);
Vasu Deve8af4d42009-07-29 17:05:15 -07001917 read_unlock(&fcoe_hostlist_lock);
Robert Love85b4aa42008-12-09 15:10:24 -08001918
Chris Leech3fe9a0b2009-08-25 13:59:46 -07001919 return (fcoe) ? fcoe->ctlr.lp : NULL;
Robert Love85b4aa42008-12-09 15:10:24 -08001920}
Robert Love85b4aa42008-12-09 15:10:24 -08001921
Robert Love34f42a02009-02-27 10:55:45 -08001922/**
1923 * fcoe_hostlist_add() - Add a lport to lports list
Chris Leechdd3fd722009-04-21 16:27:36 -07001924 * @lp: ptr to the fc_lport to be added
Robert Love85b4aa42008-12-09 15:10:24 -08001925 *
Vasu Deve8af4d42009-07-29 17:05:15 -07001926 * Called with write fcoe_hostlist_lock held.
1927 *
Robert Love85b4aa42008-12-09 15:10:24 -08001928 * Returns: 0 for success
1929 */
Chris Leech014f5c32009-08-25 13:59:30 -07001930int fcoe_hostlist_add(const struct fc_lport *lport)
Robert Love85b4aa42008-12-09 15:10:24 -08001931{
Chris Leech014f5c32009-08-25 13:59:30 -07001932 struct fcoe_interface *fcoe;
1933 struct fcoe_port *port;
Robert Love85b4aa42008-12-09 15:10:24 -08001934
Chris Leech014f5c32009-08-25 13:59:30 -07001935 fcoe = fcoe_hostlist_lookup_port(fcoe_netdev(lport));
1936 if (!fcoe) {
1937 port = lport_priv(lport);
1938 fcoe = port->fcoe;
1939 list_add_tail(&fcoe->list, &fcoe_hostlist);
Robert Love85b4aa42008-12-09 15:10:24 -08001940 }
1941 return 0;
1942}
Robert Love85b4aa42008-12-09 15:10:24 -08001943
Robert Love34f42a02009-02-27 10:55:45 -08001944/**
1945 * fcoe_hostlist_remove() - remove a lport from lports list
Chris Leechdd3fd722009-04-21 16:27:36 -07001946 * @lp: ptr to the fc_lport to be removed
Robert Love85b4aa42008-12-09 15:10:24 -08001947 *
1948 * Returns: 0 for success
1949 */
Chris Leech014f5c32009-08-25 13:59:30 -07001950int fcoe_hostlist_remove(const struct fc_lport *lport)
Robert Love85b4aa42008-12-09 15:10:24 -08001951{
Chris Leech014f5c32009-08-25 13:59:30 -07001952 struct fcoe_interface *fcoe;
Robert Love85b4aa42008-12-09 15:10:24 -08001953
Vasu Deve8af4d42009-07-29 17:05:15 -07001954 write_lock_bh(&fcoe_hostlist_lock);
Chris Leech014f5c32009-08-25 13:59:30 -07001955 fcoe = fcoe_hostlist_lookup_port(fcoe_netdev(lport));
1956 BUG_ON(!fcoe);
1957 list_del(&fcoe->list);
Robert Love85b4aa42008-12-09 15:10:24 -08001958 write_unlock_bh(&fcoe_hostlist_lock);
1959
1960 return 0;
1961}
Robert Love85b4aa42008-12-09 15:10:24 -08001962
1963/**
Robert Love34f42a02009-02-27 10:55:45 -08001964 * fcoe_init() - fcoe module loading initialization
Robert Love85b4aa42008-12-09 15:10:24 -08001965 *
Robert Love85b4aa42008-12-09 15:10:24 -08001966 * Returns 0 on success, negative on failure
Robert Love34f42a02009-02-27 10:55:45 -08001967 */
Robert Love85b4aa42008-12-09 15:10:24 -08001968static int __init fcoe_init(void)
1969{
Robert Love38eccab2009-03-17 11:41:30 -07001970 unsigned int cpu;
Robert Love8976f422009-03-17 11:41:46 -07001971 int rc = 0;
Robert Love85b4aa42008-12-09 15:10:24 -08001972 struct fcoe_percpu_s *p;
1973
Robert Love38eccab2009-03-17 11:41:30 -07001974 for_each_possible_cpu(cpu) {
Robert Love5e5e92d2009-03-17 11:41:35 -07001975 p = &per_cpu(fcoe_percpu, cpu);
Robert Love38eccab2009-03-17 11:41:30 -07001976 skb_queue_head_init(&p->fcoe_rx_list);
1977 }
1978
Robert Love8976f422009-03-17 11:41:46 -07001979 for_each_online_cpu(cpu)
1980 fcoe_percpu_thread_create(cpu);
Robert Love85b4aa42008-12-09 15:10:24 -08001981
Robert Love8976f422009-03-17 11:41:46 -07001982 /* Initialize per CPU interrupt thread */
1983 rc = register_hotcpu_notifier(&fcoe_cpu_notifier);
1984 if (rc)
1985 goto out_free;
Robert Love85b4aa42008-12-09 15:10:24 -08001986
Robert Love8976f422009-03-17 11:41:46 -07001987 /* Setup link change notification */
Robert Love85b4aa42008-12-09 15:10:24 -08001988 fcoe_dev_setup();
1989
Chris Leech5892c322009-08-25 13:59:14 -07001990 rc = fcoe_if_init();
1991 if (rc)
1992 goto out_free;
Robert Love85b4aa42008-12-09 15:10:24 -08001993
1994 return 0;
Robert Love8976f422009-03-17 11:41:46 -07001995
1996out_free:
1997 for_each_online_cpu(cpu) {
1998 fcoe_percpu_thread_destroy(cpu);
1999 }
2000
2001 return rc;
Robert Love85b4aa42008-12-09 15:10:24 -08002002}
2003module_init(fcoe_init);
2004
2005/**
Robert Love34f42a02009-02-27 10:55:45 -08002006 * fcoe_exit() - fcoe module unloading cleanup
Robert Love85b4aa42008-12-09 15:10:24 -08002007 *
2008 * Returns 0 on success, negative on failure
Robert Love34f42a02009-02-27 10:55:45 -08002009 */
Robert Love85b4aa42008-12-09 15:10:24 -08002010static void __exit fcoe_exit(void)
2011{
Robert Love5e5e92d2009-03-17 11:41:35 -07002012 unsigned int cpu;
Chris Leech014f5c32009-08-25 13:59:30 -07002013 struct fcoe_interface *fcoe, *tmp;
Robert Love85b4aa42008-12-09 15:10:24 -08002014
Robert Love85b4aa42008-12-09 15:10:24 -08002015 fcoe_dev_cleanup();
2016
Vasu Dev5919a592009-03-27 09:03:29 -07002017 /* releases the associated fcoe hosts */
Chris Leech014f5c32009-08-25 13:59:30 -07002018 list_for_each_entry_safe(fcoe, tmp, &fcoe_hostlist, list)
Chris Leech3fe9a0b2009-08-25 13:59:46 -07002019 fcoe_if_destroy(fcoe->ctlr.lp);
Robert Love85b4aa42008-12-09 15:10:24 -08002020
Robert Love8976f422009-03-17 11:41:46 -07002021 unregister_hotcpu_notifier(&fcoe_cpu_notifier);
2022
Chris Leech014f5c32009-08-25 13:59:30 -07002023 for_each_online_cpu(cpu)
Robert Love8976f422009-03-17 11:41:46 -07002024 fcoe_percpu_thread_destroy(cpu);
Robert Love85b4aa42008-12-09 15:10:24 -08002025
Vasu Dev7f349142009-03-27 09:06:31 -07002026 /* detach from scsi transport */
2027 fcoe_if_exit();
Robert Love85b4aa42008-12-09 15:10:24 -08002028}
2029module_exit(fcoe_exit);