blob: 86410b9a30c34a6a3e1b4c39e31d1331aaad862e [file] [log] [blame]
Robert Love85b4aa42008-12-09 15:10:24 -08001/*
2 * Copyright(c) 2007 - 2008 Intel Corporation. All rights reserved.
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>
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
52/* fcoe host list */
53LIST_HEAD(fcoe_hostlist);
54DEFINE_RWLOCK(fcoe_hostlist_lock);
Robert Love5e5e92d2009-03-17 11:41:35 -070055DEFINE_PER_CPU(struct fcoe_percpu_s, fcoe_percpu);
Robert Love85b4aa42008-12-09 15:10:24 -080056
Chris Leechdd3fd722009-04-21 16:27:36 -070057/* Function Prototypes */
Vasu Devfdd78022009-03-17 11:42:24 -070058static int fcoe_reset(struct Scsi_Host *shost);
59static int fcoe_xmit(struct fc_lport *, struct fc_frame *);
60static int fcoe_rcv(struct sk_buff *, struct net_device *,
61 struct packet_type *, struct net_device *);
62static int fcoe_percpu_receive_thread(void *arg);
63static void fcoe_clean_pending_queue(struct fc_lport *lp);
64static void fcoe_percpu_clean(struct fc_lport *lp);
65static int fcoe_link_ok(struct fc_lport *lp);
66
67static struct fc_lport *fcoe_hostlist_lookup(const struct net_device *);
68static int fcoe_hostlist_add(const struct fc_lport *);
69static int fcoe_hostlist_remove(const struct fc_lport *);
70
Vasu Dev4bb6b512009-05-06 10:52:34 -070071static void fcoe_check_wait_queue(struct fc_lport *, struct sk_buff *);
Robert Love85b4aa42008-12-09 15:10:24 -080072static int fcoe_device_notification(struct notifier_block *, ulong, void *);
73static void fcoe_dev_setup(void);
74static void fcoe_dev_cleanup(void);
75
76/* notification function from net device */
77static struct notifier_block fcoe_notifier = {
78 .notifier_call = fcoe_device_notification,
79};
80
Vasu Dev7f349142009-03-27 09:06:31 -070081static struct scsi_transport_template *scsi_transport_fcoe_sw;
82
83struct fc_function_template fcoe_transport_function = {
84 .show_host_node_name = 1,
85 .show_host_port_name = 1,
86 .show_host_supported_classes = 1,
87 .show_host_supported_fc4s = 1,
88 .show_host_active_fc4s = 1,
89 .show_host_maxframe_size = 1,
90
91 .show_host_port_id = 1,
92 .show_host_supported_speeds = 1,
93 .get_host_speed = fc_get_host_speed,
94 .show_host_speed = 1,
95 .show_host_port_type = 1,
96 .get_host_port_state = fc_get_host_port_state,
97 .show_host_port_state = 1,
98 .show_host_symbolic_name = 1,
99
100 .dd_fcrport_size = sizeof(struct fc_rport_libfc_priv),
101 .show_rport_maxframe_size = 1,
102 .show_rport_supported_classes = 1,
103
104 .show_host_fabric_name = 1,
105 .show_starget_node_name = 1,
106 .show_starget_port_name = 1,
107 .show_starget_port_id = 1,
108 .set_rport_dev_loss_tmo = fc_set_rport_loss_tmo,
109 .show_rport_dev_loss_tmo = 1,
110 .get_fc_host_stats = fc_get_host_stats,
111 .issue_fc_host_lip = fcoe_reset,
112
113 .terminate_rport_io = fc_rport_terminate_io,
114};
115
116static struct scsi_host_template fcoe_shost_template = {
117 .module = THIS_MODULE,
118 .name = "FCoE Driver",
119 .proc_name = FCOE_NAME,
120 .queuecommand = fc_queuecommand,
121 .eh_abort_handler = fc_eh_abort,
122 .eh_device_reset_handler = fc_eh_device_reset,
123 .eh_host_reset_handler = fc_eh_host_reset,
124 .slave_alloc = fc_slave_alloc,
125 .change_queue_depth = fc_change_queue_depth,
126 .change_queue_type = fc_change_queue_type,
127 .this_id = -1,
128 .cmd_per_lun = 32,
129 .can_queue = FCOE_MAX_OUTSTANDING_COMMANDS,
130 .use_clustering = ENABLE_CLUSTERING,
131 .sg_tablesize = SG_ALL,
132 .max_sectors = 0xffff,
133};
134
135/**
Vasu Devab6b85c2009-05-17 12:33:08 +0000136 * fcoe_fip_recv - handle a received FIP frame.
137 * @skb: the receive skb
138 * @dev: associated &net_device
139 * @ptype: the &packet_type structure which was used to register this handler.
140 * @orig_dev: original receive &net_device, in case @dev is a bond.
141 *
142 * Returns: 0 for success
143 */
144static int fcoe_fip_recv(struct sk_buff *skb, struct net_device *dev,
145 struct packet_type *ptype,
146 struct net_device *orig_dev)
147{
148 struct fcoe_softc *fc;
149
150 fc = container_of(ptype, struct fcoe_softc, fip_packet_type);
151 fcoe_ctlr_recv(&fc->ctlr, skb);
152 return 0;
153}
154
155/**
156 * fcoe_fip_send() - send an Ethernet-encapsulated FIP frame.
157 * @fip: FCoE controller.
158 * @skb: FIP Packet.
159 */
160static void fcoe_fip_send(struct fcoe_ctlr *fip, struct sk_buff *skb)
161{
162 skb->dev = fcoe_from_ctlr(fip)->real_dev;
163 dev_queue_xmit(skb);
164}
165
166/**
167 * fcoe_update_src_mac() - Update Ethernet MAC filters.
168 * @fip: FCoE controller.
169 * @old: Unicast MAC address to delete if the MAC is non-zero.
170 * @new: Unicast MAC address to add.
171 *
172 * Remove any previously-set unicast MAC filter.
173 * Add secondary FCoE MAC address filter for our OUI.
174 */
175static void fcoe_update_src_mac(struct fcoe_ctlr *fip, u8 *old, u8 *new)
176{
177 struct fcoe_softc *fc;
178
179 fc = fcoe_from_ctlr(fip);
180 rtnl_lock();
181 if (!is_zero_ether_addr(old))
Jiri Pirkoccffad22009-05-22 23:22:17 +0000182 dev_unicast_delete(fc->real_dev, old);
183 dev_unicast_add(fc->real_dev, new);
Vasu Devab6b85c2009-05-17 12:33:08 +0000184 rtnl_unlock();
185}
186
187/**
Vasu Dev7f349142009-03-27 09:06:31 -0700188 * fcoe_lport_config() - sets up the fc_lport
189 * @lp: ptr to the fc_lport
Vasu Dev7f349142009-03-27 09:06:31 -0700190 *
191 * Returns: 0 for success
192 */
193static int fcoe_lport_config(struct fc_lport *lp)
194{
195 lp->link_up = 0;
196 lp->qfull = 0;
197 lp->max_retry_count = 3;
Abhijeet Joglekara3666952009-05-01 10:01:26 -0700198 lp->max_rport_retry_count = 3;
Vasu Dev7f349142009-03-27 09:06:31 -0700199 lp->e_d_tov = 2 * 1000; /* FC-FS default */
200 lp->r_a_tov = 2 * 2 * 1000;
201 lp->service_params = (FCP_SPPF_INIT_FCN | FCP_SPPF_RD_XRDY_DIS |
202 FCP_SPPF_RETRY | FCP_SPPF_CONF_COMPL);
203
204 fc_lport_init_stats(lp);
205
206 /* lport fc_lport related configuration */
207 fc_lport_config(lp);
208
209 /* offload related configuration */
210 lp->crc_offload = 0;
211 lp->seq_offload = 0;
212 lp->lro_enabled = 0;
213 lp->lro_xid = 0;
214 lp->lso_max = 0;
215
216 return 0;
217}
218
219/**
Vasu Devab6b85c2009-05-17 12:33:08 +0000220 * fcoe_netdev_cleanup() - clean up netdev configurations
221 * @fc: ptr to the fcoe_softc
222 */
223void fcoe_netdev_cleanup(struct fcoe_softc *fc)
224{
225 u8 flogi_maddr[ETH_ALEN];
226
227 /* Don't listen for Ethernet packets anymore */
228 dev_remove_pack(&fc->fcoe_packet_type);
229 dev_remove_pack(&fc->fip_packet_type);
230
231 /* Delete secondary MAC addresses */
232 rtnl_lock();
233 memcpy(flogi_maddr, (u8[6]) FC_FCOE_FLOGI_MAC, ETH_ALEN);
Jiri Pirkoccffad22009-05-22 23:22:17 +0000234 dev_unicast_delete(fc->real_dev, flogi_maddr);
Vasu Devab6b85c2009-05-17 12:33:08 +0000235 if (!is_zero_ether_addr(fc->ctlr.data_src_addr))
Jiri Pirkoccffad22009-05-22 23:22:17 +0000236 dev_unicast_delete(fc->real_dev, fc->ctlr.data_src_addr);
Vasu Dev184dd342009-05-17 12:33:28 +0000237 if (fc->ctlr.spma)
Jiri Pirkoccffad22009-05-22 23:22:17 +0000238 dev_unicast_delete(fc->real_dev, fc->ctlr.ctl_src_addr);
Vasu Devab6b85c2009-05-17 12:33:08 +0000239 dev_mc_delete(fc->real_dev, FIP_ALL_ENODE_MACS, ETH_ALEN, 0);
240 rtnl_unlock();
241}
242
243/**
Vasu Dev1047f222009-05-06 10:52:40 -0700244 * fcoe_queue_timer() - fcoe queue timer
245 * @lp: the fc_lport pointer
246 *
247 * Calls fcoe_check_wait_queue on timeout
248 *
249 */
250static void fcoe_queue_timer(ulong lp)
251{
252 fcoe_check_wait_queue((struct fc_lport *)lp, NULL);
253}
254
255/**
Vasu Dev7f349142009-03-27 09:06:31 -0700256 * fcoe_netdev_config() - Set up netdev for SW FCoE
257 * @lp : ptr to the fc_lport
258 * @netdev : ptr to the associated netdevice struct
259 *
260 * Must be called after fcoe_lport_config() as it will use lport mutex
261 *
262 * Returns : 0 for success
263 */
264static int fcoe_netdev_config(struct fc_lport *lp, struct net_device *netdev)
265{
266 u32 mfs;
267 u64 wwnn, wwpn;
268 struct fcoe_softc *fc;
269 u8 flogi_maddr[ETH_ALEN];
Vasu Dev184dd342009-05-17 12:33:28 +0000270 struct netdev_hw_addr *ha;
Vasu Dev7f349142009-03-27 09:06:31 -0700271
272 /* Setup lport private data to point to fcoe softc */
273 fc = lport_priv(lp);
Joe Eykholt97c83892009-03-17 11:42:40 -0700274 fc->ctlr.lp = lp;
Vasu Dev7f349142009-03-27 09:06:31 -0700275 fc->real_dev = netdev;
276 fc->phys_dev = netdev;
277
278 /* Require support for get_pauseparam ethtool op. */
279 if (netdev->priv_flags & IFF_802_1Q_VLAN)
280 fc->phys_dev = vlan_dev_real_dev(netdev);
281
282 /* Do not support for bonding device */
283 if ((fc->real_dev->priv_flags & IFF_MASTER_ALB) ||
284 (fc->real_dev->priv_flags & IFF_SLAVE_INACTIVE) ||
285 (fc->real_dev->priv_flags & IFF_MASTER_8023AD)) {
286 return -EOPNOTSUPP;
287 }
288
289 /*
290 * Determine max frame size based on underlying device and optional
291 * user-configured limit. If the MFS is too low, fcoe_link_ok()
292 * will return 0, so do this first.
293 */
294 mfs = fc->real_dev->mtu - (sizeof(struct fcoe_hdr) +
295 sizeof(struct fcoe_crc_eof));
296 if (fc_set_mfs(lp, mfs))
297 return -EINVAL;
298
Vasu Dev7f349142009-03-27 09:06:31 -0700299 /* offload features support */
300 if (fc->real_dev->features & NETIF_F_SG)
301 lp->sg_supp = 1;
302
303#ifdef NETIF_F_FCOE_CRC
304 if (netdev->features & NETIF_F_FCOE_CRC) {
305 lp->crc_offload = 1;
Robert Loved5488eb2009-06-10 15:30:59 -0700306 FCOE_NETDEV_DBG(netdev, "Supports FCCRC offload\n");
Vasu Dev7f349142009-03-27 09:06:31 -0700307 }
308#endif
309#ifdef NETIF_F_FSO
310 if (netdev->features & NETIF_F_FSO) {
311 lp->seq_offload = 1;
312 lp->lso_max = netdev->gso_max_size;
Robert Loved5488eb2009-06-10 15:30:59 -0700313 FCOE_NETDEV_DBG(netdev, "Supports LSO for max len 0x%x\n",
314 lp->lso_max);
Vasu Dev7f349142009-03-27 09:06:31 -0700315 }
316#endif
317 if (netdev->fcoe_ddp_xid) {
318 lp->lro_enabled = 1;
319 lp->lro_xid = netdev->fcoe_ddp_xid;
Robert Loved5488eb2009-06-10 15:30:59 -0700320 FCOE_NETDEV_DBG(netdev, "Supports LRO for max xid 0x%x\n",
321 lp->lro_xid);
Vasu Dev7f349142009-03-27 09:06:31 -0700322 }
323 skb_queue_head_init(&fc->fcoe_pending_queue);
324 fc->fcoe_pending_queue_active = 0;
Vasu Dev1047f222009-05-06 10:52:40 -0700325 setup_timer(&fc->timer, fcoe_queue_timer, (unsigned long)lp);
Vasu Dev7f349142009-03-27 09:06:31 -0700326
Vasu Dev184dd342009-05-17 12:33:28 +0000327 /* look for SAN MAC address, if multiple SAN MACs exist, only
328 * use the first one for SPMA */
329 rcu_read_lock();
330 for_each_dev_addr(netdev, ha) {
331 if ((ha->type == NETDEV_HW_ADDR_T_SAN) &&
Yi Zou7a7f0c72009-07-29 17:03:50 -0700332 (is_valid_ether_addr(ha->addr))) {
Vasu Dev184dd342009-05-17 12:33:28 +0000333 memcpy(fc->ctlr.ctl_src_addr, ha->addr, ETH_ALEN);
334 fc->ctlr.spma = 1;
335 break;
336 }
337 }
338 rcu_read_unlock();
339
Vasu Dev7f349142009-03-27 09:06:31 -0700340 /* setup Source Mac Address */
Vasu Dev184dd342009-05-17 12:33:28 +0000341 if (!fc->ctlr.spma)
342 memcpy(fc->ctlr.ctl_src_addr, fc->real_dev->dev_addr,
343 fc->real_dev->addr_len);
Vasu Dev7f349142009-03-27 09:06:31 -0700344
345 wwnn = fcoe_wwn_from_mac(fc->real_dev->dev_addr, 1, 0);
346 fc_set_wwnn(lp, wwnn);
347 /* XXX - 3rd arg needs to be vlan id */
348 wwpn = fcoe_wwn_from_mac(fc->real_dev->dev_addr, 2, 0);
349 fc_set_wwpn(lp, wwpn);
350
351 /*
352 * Add FCoE MAC address as second unicast MAC address
353 * or enter promiscuous mode if not capable of listening
354 * for multiple unicast MACs.
355 */
356 rtnl_lock();
357 memcpy(flogi_maddr, (u8[6]) FC_FCOE_FLOGI_MAC, ETH_ALEN);
Jiri Pirkoccffad22009-05-22 23:22:17 +0000358 dev_unicast_add(fc->real_dev, flogi_maddr);
Vasu Dev184dd342009-05-17 12:33:28 +0000359 if (fc->ctlr.spma)
Jiri Pirkoccffad22009-05-22 23:22:17 +0000360 dev_unicast_add(fc->real_dev, fc->ctlr.ctl_src_addr);
Joe Eykholt6401bdc2009-04-21 16:27:46 -0700361 dev_mc_add(fc->real_dev, FIP_ALL_ENODE_MACS, ETH_ALEN, 0);
Vasu Dev7f349142009-03-27 09:06:31 -0700362 rtnl_unlock();
363
364 /*
365 * setup the receive function from ethernet driver
366 * on the ethertype for the given device
367 */
368 fc->fcoe_packet_type.func = fcoe_rcv;
369 fc->fcoe_packet_type.type = __constant_htons(ETH_P_FCOE);
370 fc->fcoe_packet_type.dev = fc->real_dev;
371 dev_add_pack(&fc->fcoe_packet_type);
372
Vasu Devab6b85c2009-05-17 12:33:08 +0000373 fc->fip_packet_type.func = fcoe_fip_recv;
374 fc->fip_packet_type.type = htons(ETH_P_FIP);
375 fc->fip_packet_type.dev = fc->real_dev;
376 dev_add_pack(&fc->fip_packet_type);
377
Vasu Dev7f349142009-03-27 09:06:31 -0700378 return 0;
379}
380
381/**
382 * fcoe_shost_config() - Sets up fc_lport->host
383 * @lp : ptr to the fc_lport
384 * @shost : ptr to the associated scsi host
385 * @dev : device associated to scsi host
386 *
387 * Must be called after fcoe_lport_config() and fcoe_netdev_config()
388 *
389 * Returns : 0 for success
390 */
391static int fcoe_shost_config(struct fc_lport *lp, struct Scsi_Host *shost,
392 struct device *dev)
393{
394 int rc = 0;
395
396 /* lport scsi host config */
397 lp->host = shost;
398
399 lp->host->max_lun = FCOE_MAX_LUN;
400 lp->host->max_id = FCOE_MAX_FCP_TARGET;
401 lp->host->max_channel = 0;
402 lp->host->transportt = scsi_transport_fcoe_sw;
403
404 /* add the new host to the SCSI-ml */
405 rc = scsi_add_host(lp->host, dev);
406 if (rc) {
Robert Loved5488eb2009-06-10 15:30:59 -0700407 FCOE_NETDEV_DBG(fcoe_netdev(lp), "fcoe_shost_config: "
408 "error on scsi_add_host\n");
Vasu Dev7f349142009-03-27 09:06:31 -0700409 return rc;
410 }
411 sprintf(fc_host_symbolic_name(lp->host), "%s v%s over %s",
412 FCOE_NAME, FCOE_VERSION,
413 fcoe_netdev(lp)->name);
414
415 return 0;
416}
417
418/**
419 * fcoe_em_config() - allocates em for this lport
420 * @lp: the port that em is to allocated for
421 *
Vasu Deve8af4d42009-07-29 17:05:15 -0700422 * Called with write fcoe_hostlist_lock held.
423 *
Vasu Dev7f349142009-03-27 09:06:31 -0700424 * Returns : 0 on success
425 */
426static inline int fcoe_em_config(struct fc_lport *lp)
427{
Vasu Dev52ff8782009-07-29 17:05:10 -0700428 if (!fc_exch_mgr_alloc(lp, FC_CLASS_3, FCOE_MIN_XID,
429 FCOE_MAX_XID, NULL))
Vasu Dev7f349142009-03-27 09:06:31 -0700430 return -ENOMEM;
431
432 return 0;
433}
434
435/**
436 * fcoe_if_destroy() - FCoE software HBA tear-down function
437 * @netdev: ptr to the associated net_device
438 *
439 * Returns: 0 if link is OK for use by FCoE.
440 */
441static int fcoe_if_destroy(struct net_device *netdev)
442{
443 struct fc_lport *lp = NULL;
444 struct fcoe_softc *fc;
Vasu Dev7f349142009-03-27 09:06:31 -0700445
446 BUG_ON(!netdev);
447
Robert Loved5488eb2009-06-10 15:30:59 -0700448 FCOE_NETDEV_DBG(netdev, "Destroying interface\n");
Vasu Dev7f349142009-03-27 09:06:31 -0700449
450 lp = fcoe_hostlist_lookup(netdev);
451 if (!lp)
452 return -ENODEV;
453
454 fc = lport_priv(lp);
455
456 /* Logout of the fabric */
457 fc_fabric_logoff(lp);
458
459 /* Remove the instance from fcoe's list */
460 fcoe_hostlist_remove(lp);
461
Vasu Devab6b85c2009-05-17 12:33:08 +0000462 /* clean up netdev configurations */
463 fcoe_netdev_cleanup(fc);
464
465 /* tear-down the FCoE controller */
Joe Eykholt97c83892009-03-17 11:42:40 -0700466 fcoe_ctlr_destroy(&fc->ctlr);
Vasu Dev7f349142009-03-27 09:06:31 -0700467
Joe Eykholtf161fb72009-07-29 17:04:17 -0700468 /* Free queued packets for the per-CPU receive threads */
469 fcoe_percpu_clean(lp);
470
Vasu Dev7f349142009-03-27 09:06:31 -0700471 /* Cleanup the fc_lport */
472 fc_lport_destroy(lp);
473 fc_fcp_destroy(lp);
474
475 /* Detach from the scsi-ml */
476 fc_remove_host(lp->host);
477 scsi_remove_host(lp->host);
478
479 /* There are no more rports or I/O, free the EM */
Vasu Dev52ff8782009-07-29 17:05:10 -0700480 fc_exch_mgr_free(lp);
Vasu Dev7f349142009-03-27 09:06:31 -0700481
Vasu Dev7f349142009-03-27 09:06:31 -0700482 /* Free existing skbs */
483 fcoe_clean_pending_queue(lp);
484
Vasu Dev1047f222009-05-06 10:52:40 -0700485 /* Stop the timer */
486 del_timer_sync(&fc->timer);
487
Vasu Dev7f349142009-03-27 09:06:31 -0700488 /* Free memory used by statistical counters */
489 fc_lport_free_stats(lp);
490
491 /* Release the net_device and Scsi_Host */
492 dev_put(fc->real_dev);
493 scsi_host_put(lp->host);
494
495 return 0;
496}
497
498/*
499 * fcoe_ddp_setup - calls LLD's ddp_setup through net_device
500 * @lp: the corresponding fc_lport
501 * @xid: the exchange id for this ddp transfer
502 * @sgl: the scatterlist describing this transfer
503 * @sgc: number of sg items
504 *
505 * Returns : 0 no ddp
506 */
507static int fcoe_ddp_setup(struct fc_lport *lp, u16 xid,
508 struct scatterlist *sgl, unsigned int sgc)
509{
510 struct net_device *n = fcoe_netdev(lp);
511
512 if (n->netdev_ops && n->netdev_ops->ndo_fcoe_ddp_setup)
513 return n->netdev_ops->ndo_fcoe_ddp_setup(n, xid, sgl, sgc);
514
515 return 0;
516}
517
518/*
519 * fcoe_ddp_done - calls LLD's ddp_done through net_device
520 * @lp: the corresponding fc_lport
521 * @xid: the exchange id for this ddp transfer
522 *
523 * Returns : the length of data that have been completed by ddp
524 */
525static int fcoe_ddp_done(struct fc_lport *lp, u16 xid)
526{
527 struct net_device *n = fcoe_netdev(lp);
528
529 if (n->netdev_ops && n->netdev_ops->ndo_fcoe_ddp_done)
530 return n->netdev_ops->ndo_fcoe_ddp_done(n, xid);
531 return 0;
532}
533
534static struct libfc_function_template fcoe_libfc_fcn_templ = {
535 .frame_send = fcoe_xmit,
536 .ddp_setup = fcoe_ddp_setup,
537 .ddp_done = fcoe_ddp_done,
538};
539
540/**
541 * fcoe_if_create() - this function creates the fcoe interface
542 * @netdev: pointer the associated netdevice
543 *
544 * Creates fc_lport struct and scsi_host for lport, configures lport
545 * and starts fabric login.
546 *
547 * Returns : 0 on success
548 */
549static int fcoe_if_create(struct net_device *netdev)
550{
551 int rc;
552 struct fc_lport *lp = NULL;
553 struct fcoe_softc *fc;
554 struct Scsi_Host *shost;
555
556 BUG_ON(!netdev);
557
Robert Loved5488eb2009-06-10 15:30:59 -0700558 FCOE_NETDEV_DBG(netdev, "Create Interface\n");
Vasu Dev7f349142009-03-27 09:06:31 -0700559
560 lp = fcoe_hostlist_lookup(netdev);
561 if (lp)
562 return -EEXIST;
563
Vasu Deva0a25da2009-03-17 11:42:29 -0700564 shost = libfc_host_alloc(&fcoe_shost_template,
565 sizeof(struct fcoe_softc));
Vasu Dev7f349142009-03-27 09:06:31 -0700566 if (!shost) {
Robert Loved5488eb2009-06-10 15:30:59 -0700567 FCOE_NETDEV_DBG(netdev, "Could not allocate host structure\n");
Vasu Dev7f349142009-03-27 09:06:31 -0700568 return -ENOMEM;
569 }
570 lp = shost_priv(shost);
571 fc = lport_priv(lp);
572
573 /* configure fc_lport, e.g., em */
574 rc = fcoe_lport_config(lp);
575 if (rc) {
Robert Loved5488eb2009-06-10 15:30:59 -0700576 FCOE_NETDEV_DBG(netdev, "Could not configure lport for the "
577 "interface\n");
Vasu Dev7f349142009-03-27 09:06:31 -0700578 goto out_host_put;
579 }
580
Joe Eykholt97c83892009-03-17 11:42:40 -0700581 /*
582 * Initialize FIP.
583 */
584 fcoe_ctlr_init(&fc->ctlr);
585 fc->ctlr.send = fcoe_fip_send;
586 fc->ctlr.update_mac = fcoe_update_src_mac;
587
Vasu Devab6b85c2009-05-17 12:33:08 +0000588 /* configure lport network properties */
589 rc = fcoe_netdev_config(lp, netdev);
590 if (rc) {
Robert Loved5488eb2009-06-10 15:30:59 -0700591 FCOE_NETDEV_DBG(netdev, "Could not configure netdev for the "
592 "interface\n");
Vasu Devab6b85c2009-05-17 12:33:08 +0000593 goto out_netdev_cleanup;
594 }
Joe Eykholt97c83892009-03-17 11:42:40 -0700595
Vasu Dev7f349142009-03-27 09:06:31 -0700596 /* configure lport scsi host properties */
597 rc = fcoe_shost_config(lp, shost, &netdev->dev);
598 if (rc) {
Robert Loved5488eb2009-06-10 15:30:59 -0700599 FCOE_NETDEV_DBG(netdev, "Could not configure shost for the "
600 "interface\n");
Vasu Devab6b85c2009-05-17 12:33:08 +0000601 goto out_netdev_cleanup;
Vasu Dev7f349142009-03-27 09:06:31 -0700602 }
603
Vasu Dev7f349142009-03-27 09:06:31 -0700604 /* Initialize the library */
605 rc = fcoe_libfc_config(lp, &fcoe_libfc_fcn_templ);
606 if (rc) {
Robert Loved5488eb2009-06-10 15:30:59 -0700607 FCOE_NETDEV_DBG(netdev, "Could not configure libfc for the "
608 "interface\n");
Vasu Dev7f349142009-03-27 09:06:31 -0700609 goto out_lp_destroy;
610 }
611
Vasu Deve8af4d42009-07-29 17:05:15 -0700612 /*
613 * fcoe_em_alloc() and fcoe_hostlist_add() both
614 * need to be atomic under fcoe_hostlist_lock
615 * since fcoe_em_alloc() looks for an existing EM
616 * instance on host list updated by fcoe_hostlist_add().
617 */
618 write_lock(&fcoe_hostlist_lock);
Vasu Dev96316092009-07-29 17:05:00 -0700619 /* lport exch manager allocation */
620 rc = fcoe_em_config(lp);
621 if (rc) {
622 FCOE_NETDEV_DBG(netdev, "Could not configure the EM for the "
623 "interface\n");
624 goto out_lp_destroy;
625 }
626
Vasu Dev7f349142009-03-27 09:06:31 -0700627 /* add to lports list */
628 fcoe_hostlist_add(lp);
Vasu Deve8af4d42009-07-29 17:05:15 -0700629 write_unlock(&fcoe_hostlist_lock);
Vasu Dev7f349142009-03-27 09:06:31 -0700630
631 lp->boot_time = jiffies;
632
633 fc_fabric_login(lp);
634
Joe Eykholt97c83892009-03-17 11:42:40 -0700635 if (!fcoe_link_ok(lp))
636 fcoe_ctlr_link_up(&fc->ctlr);
637
Vasu Dev7f349142009-03-27 09:06:31 -0700638 dev_hold(netdev);
639
640 return rc;
641
642out_lp_destroy:
Vasu Dev52ff8782009-07-29 17:05:10 -0700643 fc_exch_mgr_free(lp);
Vasu Devab6b85c2009-05-17 12:33:08 +0000644out_netdev_cleanup:
645 fcoe_netdev_cleanup(fc);
Vasu Dev7f349142009-03-27 09:06:31 -0700646out_host_put:
647 scsi_host_put(lp->host);
648 return rc;
649}
650
651/**
652 * fcoe_if_init() - attach to scsi transport
653 *
654 * Returns : 0 on success
655 */
656static int __init fcoe_if_init(void)
657{
658 /* attach to scsi transport */
659 scsi_transport_fcoe_sw =
660 fc_attach_transport(&fcoe_transport_function);
661
662 if (!scsi_transport_fcoe_sw) {
Robert Loved5488eb2009-06-10 15:30:59 -0700663 printk(KERN_ERR "fcoe: Failed to attach to the FC transport\n");
Vasu Dev7f349142009-03-27 09:06:31 -0700664 return -ENODEV;
665 }
666
667 return 0;
668}
669
670/**
671 * fcoe_if_exit() - detach from scsi transport
672 *
673 * Returns : 0 on success
674 */
675int __exit fcoe_if_exit(void)
676{
677 fc_release_transport(scsi_transport_fcoe_sw);
678 return 0;
679}
680
Robert Love85b4aa42008-12-09 15:10:24 -0800681/**
Robert Love8976f422009-03-17 11:41:46 -0700682 * fcoe_percpu_thread_create() - Create a receive thread for an online cpu
683 * @cpu: cpu index for the online cpu
684 */
685static void fcoe_percpu_thread_create(unsigned int cpu)
686{
687 struct fcoe_percpu_s *p;
688 struct task_struct *thread;
689
690 p = &per_cpu(fcoe_percpu, cpu);
691
692 thread = kthread_create(fcoe_percpu_receive_thread,
693 (void *)p, "fcoethread/%d", cpu);
694
695 if (likely(!IS_ERR(p->thread))) {
696 kthread_bind(thread, cpu);
697 wake_up_process(thread);
698
699 spin_lock_bh(&p->fcoe_rx_list.lock);
700 p->thread = thread;
701 spin_unlock_bh(&p->fcoe_rx_list.lock);
702 }
703}
704
705/**
706 * fcoe_percpu_thread_destroy() - removes the rx thread for the given cpu
707 * @cpu: cpu index the rx thread is to be removed
708 *
709 * Destroys a per-CPU Rx thread. Any pending skbs are moved to the
710 * current CPU's Rx thread. If the thread being destroyed is bound to
711 * the CPU processing this context the skbs will be freed.
712 */
713static void fcoe_percpu_thread_destroy(unsigned int cpu)
714{
715 struct fcoe_percpu_s *p;
716 struct task_struct *thread;
717 struct page *crc_eof;
718 struct sk_buff *skb;
719#ifdef CONFIG_SMP
720 struct fcoe_percpu_s *p0;
721 unsigned targ_cpu = smp_processor_id();
722#endif /* CONFIG_SMP */
723
Robert Loved5488eb2009-06-10 15:30:59 -0700724 FCOE_DBG("Destroying receive thread for CPU %d\n", cpu);
Robert Love8976f422009-03-17 11:41:46 -0700725
726 /* Prevent any new skbs from being queued for this CPU. */
727 p = &per_cpu(fcoe_percpu, cpu);
728 spin_lock_bh(&p->fcoe_rx_list.lock);
729 thread = p->thread;
730 p->thread = NULL;
731 crc_eof = p->crc_eof_page;
732 p->crc_eof_page = NULL;
733 p->crc_eof_offset = 0;
734 spin_unlock_bh(&p->fcoe_rx_list.lock);
735
736#ifdef CONFIG_SMP
737 /*
738 * Don't bother moving the skb's if this context is running
739 * on the same CPU that is having its thread destroyed. This
740 * can easily happen when the module is removed.
741 */
742 if (cpu != targ_cpu) {
743 p0 = &per_cpu(fcoe_percpu, targ_cpu);
744 spin_lock_bh(&p0->fcoe_rx_list.lock);
745 if (p0->thread) {
Robert Loved5488eb2009-06-10 15:30:59 -0700746 FCOE_DBG("Moving frames from CPU %d to CPU %d\n",
747 cpu, targ_cpu);
Robert Love8976f422009-03-17 11:41:46 -0700748
749 while ((skb = __skb_dequeue(&p->fcoe_rx_list)) != NULL)
750 __skb_queue_tail(&p0->fcoe_rx_list, skb);
751 spin_unlock_bh(&p0->fcoe_rx_list.lock);
752 } else {
753 /*
754 * The targeted CPU is not initialized and cannot accept
755 * new skbs. Unlock the targeted CPU and drop the skbs
756 * on the CPU that is going offline.
757 */
758 while ((skb = __skb_dequeue(&p->fcoe_rx_list)) != NULL)
759 kfree_skb(skb);
760 spin_unlock_bh(&p0->fcoe_rx_list.lock);
761 }
762 } else {
763 /*
764 * This scenario occurs when the module is being removed
765 * and all threads are being destroyed. skbs will continue
766 * to be shifted from the CPU thread that is being removed
767 * to the CPU thread associated with the CPU that is processing
768 * the module removal. Once there is only one CPU Rx thread it
769 * will reach this case and we will drop all skbs and later
770 * stop the thread.
771 */
772 spin_lock_bh(&p->fcoe_rx_list.lock);
773 while ((skb = __skb_dequeue(&p->fcoe_rx_list)) != NULL)
774 kfree_skb(skb);
775 spin_unlock_bh(&p->fcoe_rx_list.lock);
776 }
777#else
778 /*
Chris Leechdd3fd722009-04-21 16:27:36 -0700779 * This a non-SMP scenario where the singular Rx thread is
Robert Love8976f422009-03-17 11:41:46 -0700780 * being removed. Free all skbs and stop the thread.
781 */
782 spin_lock_bh(&p->fcoe_rx_list.lock);
783 while ((skb = __skb_dequeue(&p->fcoe_rx_list)) != NULL)
784 kfree_skb(skb);
785 spin_unlock_bh(&p->fcoe_rx_list.lock);
786#endif
787
788 if (thread)
789 kthread_stop(thread);
790
791 if (crc_eof)
792 put_page(crc_eof);
793}
794
795/**
796 * fcoe_cpu_callback() - fcoe cpu hotplug event callback
797 * @nfb: callback data block
798 * @action: event triggering the callback
799 * @hcpu: index for the cpu of this event
800 *
801 * This creates or destroys per cpu data for fcoe
802 *
803 * Returns NOTIFY_OK always.
804 */
805static int fcoe_cpu_callback(struct notifier_block *nfb,
806 unsigned long action, void *hcpu)
807{
808 unsigned cpu = (unsigned long)hcpu;
809
810 switch (action) {
811 case CPU_ONLINE:
812 case CPU_ONLINE_FROZEN:
Robert Loved5488eb2009-06-10 15:30:59 -0700813 FCOE_DBG("CPU %x online: Create Rx thread\n", cpu);
Robert Love8976f422009-03-17 11:41:46 -0700814 fcoe_percpu_thread_create(cpu);
815 break;
816 case CPU_DEAD:
817 case CPU_DEAD_FROZEN:
Robert Loved5488eb2009-06-10 15:30:59 -0700818 FCOE_DBG("CPU %x offline: Remove Rx thread\n", cpu);
Robert Love8976f422009-03-17 11:41:46 -0700819 fcoe_percpu_thread_destroy(cpu);
820 break;
821 default:
822 break;
823 }
824 return NOTIFY_OK;
825}
826
827static struct notifier_block fcoe_cpu_notifier = {
828 .notifier_call = fcoe_cpu_callback,
829};
830
831/**
Robert Love34f42a02009-02-27 10:55:45 -0800832 * fcoe_rcv() - this is the fcoe receive function called by NET_RX_SOFTIRQ
Robert Love85b4aa42008-12-09 15:10:24 -0800833 * @skb: the receive skb
834 * @dev: associated net device
835 * @ptype: context
Chris Leechdd3fd722009-04-21 16:27:36 -0700836 * @olddev: last device
Robert Love85b4aa42008-12-09 15:10:24 -0800837 *
838 * this function will receive the packet and build fc frame and pass it up
839 *
840 * Returns: 0 for success
Robert Love34f42a02009-02-27 10:55:45 -0800841 */
Robert Love85b4aa42008-12-09 15:10:24 -0800842int fcoe_rcv(struct sk_buff *skb, struct net_device *dev,
843 struct packet_type *ptype, struct net_device *olddev)
844{
845 struct fc_lport *lp;
846 struct fcoe_rcv_info *fr;
847 struct fcoe_softc *fc;
Robert Love85b4aa42008-12-09 15:10:24 -0800848 struct fc_frame_header *fh;
Robert Love85b4aa42008-12-09 15:10:24 -0800849 struct fcoe_percpu_s *fps;
Robert Love38eccab2009-03-17 11:41:30 -0700850 unsigned short oxid;
Robert Love8976f422009-03-17 11:41:46 -0700851 unsigned int cpu = 0;
Robert Love85b4aa42008-12-09 15:10:24 -0800852
853 fc = container_of(ptype, struct fcoe_softc, fcoe_packet_type);
Joe Eykholt97c83892009-03-17 11:42:40 -0700854 lp = fc->ctlr.lp;
Robert Love85b4aa42008-12-09 15:10:24 -0800855 if (unlikely(lp == NULL)) {
Robert Loved5488eb2009-06-10 15:30:59 -0700856 FCOE_NETDEV_DBG(dev, "Cannot find hba structure");
Robert Love85b4aa42008-12-09 15:10:24 -0800857 goto err2;
858 }
Joe Eykholt97c83892009-03-17 11:42:40 -0700859 if (!lp->link_up)
860 goto err2;
Robert Love85b4aa42008-12-09 15:10:24 -0800861
Robert Loved5488eb2009-06-10 15:30:59 -0700862 FCOE_NETDEV_DBG(dev, "skb_info: len:%d data_len:%d head:%p "
863 "data:%p tail:%p end:%p sum:%d dev:%s",
864 skb->len, skb->data_len, skb->head, skb->data,
865 skb_tail_pointer(skb), skb_end_pointer(skb),
866 skb->csum, skb->dev ? skb->dev->name : "<NULL>");
Robert Love85b4aa42008-12-09 15:10:24 -0800867
868 /* check for FCOE packet type */
869 if (unlikely(eth_hdr(skb)->h_proto != htons(ETH_P_FCOE))) {
Robert Loved5488eb2009-06-10 15:30:59 -0700870 FCOE_NETDEV_DBG(dev, "Wrong FC type frame");
Robert Love85b4aa42008-12-09 15:10:24 -0800871 goto err;
872 }
873
874 /*
875 * Check for minimum frame length, and make sure required FCoE
876 * and FC headers are pulled into the linear data area.
877 */
878 if (unlikely((skb->len < FCOE_MIN_FRAME) ||
879 !pskb_may_pull(skb, FCOE_HEADER_LEN)))
880 goto err;
881
882 skb_set_transport_header(skb, sizeof(struct fcoe_hdr));
883 fh = (struct fc_frame_header *) skb_transport_header(skb);
884
885 oxid = ntohs(fh->fh_ox_id);
886
887 fr = fcoe_dev_from_skb(skb);
888 fr->fr_dev = lp;
889 fr->ptype = ptype;
Robert Love5e5e92d2009-03-17 11:41:35 -0700890
Robert Love85b4aa42008-12-09 15:10:24 -0800891#ifdef CONFIG_SMP
892 /*
893 * The incoming frame exchange id(oxid) is ANDed with num of online
Robert Love8976f422009-03-17 11:41:46 -0700894 * cpu bits to get cpu and then this cpu is used for selecting
895 * a per cpu kernel thread from fcoe_percpu.
Robert Love85b4aa42008-12-09 15:10:24 -0800896 */
Robert Love8976f422009-03-17 11:41:46 -0700897 cpu = oxid & (num_online_cpus() - 1);
Robert Love85b4aa42008-12-09 15:10:24 -0800898#endif
Robert Love5e5e92d2009-03-17 11:41:35 -0700899
Robert Love8976f422009-03-17 11:41:46 -0700900 fps = &per_cpu(fcoe_percpu, cpu);
Robert Love85b4aa42008-12-09 15:10:24 -0800901 spin_lock_bh(&fps->fcoe_rx_list.lock);
Robert Love8976f422009-03-17 11:41:46 -0700902 if (unlikely(!fps->thread)) {
903 /*
904 * The targeted CPU is not ready, let's target
905 * the first CPU now. For non-SMP systems this
906 * will check the same CPU twice.
907 */
Robert Loved5488eb2009-06-10 15:30:59 -0700908 FCOE_NETDEV_DBG(dev, "CPU is online, but no receive thread "
909 "ready for incoming skb- using first online "
910 "CPU.\n");
Robert Love8976f422009-03-17 11:41:46 -0700911
912 spin_unlock_bh(&fps->fcoe_rx_list.lock);
913 cpu = first_cpu(cpu_online_map);
914 fps = &per_cpu(fcoe_percpu, cpu);
915 spin_lock_bh(&fps->fcoe_rx_list.lock);
916 if (!fps->thread) {
917 spin_unlock_bh(&fps->fcoe_rx_list.lock);
918 goto err;
919 }
920 }
921
922 /*
923 * We now have a valid CPU that we're targeting for
924 * this skb. We also have this receive thread locked,
925 * so we're free to queue skbs into it's queue.
926 */
Robert Love85b4aa42008-12-09 15:10:24 -0800927 __skb_queue_tail(&fps->fcoe_rx_list, skb);
928 if (fps->fcoe_rx_list.qlen == 1)
929 wake_up_process(fps->thread);
930
931 spin_unlock_bh(&fps->fcoe_rx_list.lock);
932
933 return 0;
934err:
Robert Love582b45b2009-03-31 15:51:50 -0700935 fc_lport_get_stats(lp)->ErrorFrames++;
Robert Love85b4aa42008-12-09 15:10:24 -0800936
937err2:
938 kfree_skb(skb);
939 return -1;
940}
Robert Love85b4aa42008-12-09 15:10:24 -0800941
942/**
Robert Love34f42a02009-02-27 10:55:45 -0800943 * fcoe_start_io() - pass to netdev to start xmit for fcoe
Robert Love85b4aa42008-12-09 15:10:24 -0800944 * @skb: the skb to be xmitted
945 *
946 * Returns: 0 for success
Robert Love34f42a02009-02-27 10:55:45 -0800947 */
Robert Love85b4aa42008-12-09 15:10:24 -0800948static inline int fcoe_start_io(struct sk_buff *skb)
949{
950 int rc;
951
952 skb_get(skb);
953 rc = dev_queue_xmit(skb);
954 if (rc != 0)
955 return rc;
956 kfree_skb(skb);
957 return 0;
958}
959
960/**
Chris Leechdd3fd722009-04-21 16:27:36 -0700961 * fcoe_get_paged_crc_eof() - in case we need to alloc a page for crc_eof
Robert Love85b4aa42008-12-09 15:10:24 -0800962 * @skb: the skb to be xmitted
963 * @tlen: total len
964 *
965 * Returns: 0 for success
Robert Love34f42a02009-02-27 10:55:45 -0800966 */
Robert Love85b4aa42008-12-09 15:10:24 -0800967static int fcoe_get_paged_crc_eof(struct sk_buff *skb, int tlen)
968{
969 struct fcoe_percpu_s *fps;
970 struct page *page;
Robert Love85b4aa42008-12-09 15:10:24 -0800971
Robert Love5e5e92d2009-03-17 11:41:35 -0700972 fps = &get_cpu_var(fcoe_percpu);
Robert Love85b4aa42008-12-09 15:10:24 -0800973 page = fps->crc_eof_page;
974 if (!page) {
975 page = alloc_page(GFP_ATOMIC);
976 if (!page) {
Robert Love5e5e92d2009-03-17 11:41:35 -0700977 put_cpu_var(fcoe_percpu);
Robert Love85b4aa42008-12-09 15:10:24 -0800978 return -ENOMEM;
979 }
980 fps->crc_eof_page = page;
Robert Love8976f422009-03-17 11:41:46 -0700981 fps->crc_eof_offset = 0;
Robert Love85b4aa42008-12-09 15:10:24 -0800982 }
983
984 get_page(page);
985 skb_fill_page_desc(skb, skb_shinfo(skb)->nr_frags, page,
986 fps->crc_eof_offset, tlen);
987 skb->len += tlen;
988 skb->data_len += tlen;
989 skb->truesize += tlen;
990 fps->crc_eof_offset += sizeof(struct fcoe_crc_eof);
991
992 if (fps->crc_eof_offset >= PAGE_SIZE) {
993 fps->crc_eof_page = NULL;
994 fps->crc_eof_offset = 0;
995 put_page(page);
996 }
Robert Love5e5e92d2009-03-17 11:41:35 -0700997 put_cpu_var(fcoe_percpu);
Robert Love85b4aa42008-12-09 15:10:24 -0800998 return 0;
999}
1000
1001/**
Robert Love34f42a02009-02-27 10:55:45 -08001002 * fcoe_fc_crc() - calculates FC CRC in this fcoe skb
Chris Leechdd3fd722009-04-21 16:27:36 -07001003 * @fp: the fc_frame containing data to be checksummed
Robert Love85b4aa42008-12-09 15:10:24 -08001004 *
1005 * This uses crc32() to calculate the crc for fc frame
1006 * Return : 32 bit crc
Robert Love34f42a02009-02-27 10:55:45 -08001007 */
Robert Love85b4aa42008-12-09 15:10:24 -08001008u32 fcoe_fc_crc(struct fc_frame *fp)
1009{
1010 struct sk_buff *skb = fp_skb(fp);
1011 struct skb_frag_struct *frag;
1012 unsigned char *data;
1013 unsigned long off, len, clen;
1014 u32 crc;
1015 unsigned i;
1016
1017 crc = crc32(~0, skb->data, skb_headlen(skb));
1018
1019 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
1020 frag = &skb_shinfo(skb)->frags[i];
1021 off = frag->page_offset;
1022 len = frag->size;
1023 while (len > 0) {
1024 clen = min(len, PAGE_SIZE - (off & ~PAGE_MASK));
1025 data = kmap_atomic(frag->page + (off >> PAGE_SHIFT),
1026 KM_SKB_DATA_SOFTIRQ);
1027 crc = crc32(crc, data + (off & ~PAGE_MASK), clen);
1028 kunmap_atomic(data, KM_SKB_DATA_SOFTIRQ);
1029 off += clen;
1030 len -= clen;
1031 }
1032 }
1033 return crc;
1034}
Robert Love85b4aa42008-12-09 15:10:24 -08001035
1036/**
Robert Love34f42a02009-02-27 10:55:45 -08001037 * fcoe_xmit() - FCoE frame transmit function
Robert Love85b4aa42008-12-09 15:10:24 -08001038 * @lp: the associated local port
1039 * @fp: the fc_frame to be transmitted
1040 *
1041 * Return : 0 for success
Robert Love34f42a02009-02-27 10:55:45 -08001042 */
Robert Love85b4aa42008-12-09 15:10:24 -08001043int fcoe_xmit(struct fc_lport *lp, struct fc_frame *fp)
1044{
Vasu Dev4bb6b512009-05-06 10:52:34 -07001045 int wlen;
Robert Love85b4aa42008-12-09 15:10:24 -08001046 u32 crc;
1047 struct ethhdr *eh;
1048 struct fcoe_crc_eof *cp;
1049 struct sk_buff *skb;
1050 struct fcoe_dev_stats *stats;
1051 struct fc_frame_header *fh;
1052 unsigned int hlen; /* header length implies the version */
1053 unsigned int tlen; /* trailer length */
1054 unsigned int elen; /* eth header, may include vlan */
Robert Love85b4aa42008-12-09 15:10:24 -08001055 struct fcoe_softc *fc;
1056 u8 sof, eof;
1057 struct fcoe_hdr *hp;
1058
1059 WARN_ON((fr_len(fp) % sizeof(u32)) != 0);
1060
Robert Lovefc47ff62009-02-27 10:55:55 -08001061 fc = lport_priv(lp);
Robert Love85b4aa42008-12-09 15:10:24 -08001062 fh = fc_frame_header_get(fp);
Joe Eykholt97c83892009-03-17 11:42:40 -07001063 skb = fp_skb(fp);
1064 wlen = skb->len / FCOE_WORD_TO_BYTE;
1065
1066 if (!lp->link_up) {
Dan Carpenter3caf02e2009-04-21 16:27:25 -07001067 kfree_skb(skb);
Joe Eykholt97c83892009-03-17 11:42:40 -07001068 return 0;
Robert Love85b4aa42008-12-09 15:10:24 -08001069 }
1070
Joe Eykholt97c83892009-03-17 11:42:40 -07001071 if (unlikely(fh->fh_r_ctl == FC_RCTL_ELS_REQ) &&
1072 fcoe_ctlr_els_send(&fc->ctlr, skb))
1073 return 0;
1074
Robert Love85b4aa42008-12-09 15:10:24 -08001075 sof = fr_sof(fp);
1076 eof = fr_eof(fp);
1077
Vasu Dev4e57e1c2009-05-06 10:52:46 -07001078 elen = sizeof(struct ethhdr);
Robert Love85b4aa42008-12-09 15:10:24 -08001079 hlen = sizeof(struct fcoe_hdr);
1080 tlen = sizeof(struct fcoe_crc_eof);
1081 wlen = (skb->len - tlen + sizeof(crc)) / FCOE_WORD_TO_BYTE;
1082
1083 /* crc offload */
1084 if (likely(lp->crc_offload)) {
Yi Zou39ca9a02009-02-27 14:07:15 -08001085 skb->ip_summed = CHECKSUM_PARTIAL;
Robert Love85b4aa42008-12-09 15:10:24 -08001086 skb->csum_start = skb_headroom(skb);
1087 skb->csum_offset = skb->len;
1088 crc = 0;
1089 } else {
1090 skb->ip_summed = CHECKSUM_NONE;
1091 crc = fcoe_fc_crc(fp);
1092 }
1093
1094 /* copy fc crc and eof to the skb buff */
1095 if (skb_is_nonlinear(skb)) {
1096 skb_frag_t *frag;
1097 if (fcoe_get_paged_crc_eof(skb, tlen)) {
Roel Kluine9041582009-02-27 10:56:22 -08001098 kfree_skb(skb);
Robert Love85b4aa42008-12-09 15:10:24 -08001099 return -ENOMEM;
1100 }
1101 frag = &skb_shinfo(skb)->frags[skb_shinfo(skb)->nr_frags - 1];
1102 cp = kmap_atomic(frag->page, KM_SKB_DATA_SOFTIRQ)
1103 + frag->page_offset;
1104 } else {
1105 cp = (struct fcoe_crc_eof *)skb_put(skb, tlen);
1106 }
1107
1108 memset(cp, 0, sizeof(*cp));
1109 cp->fcoe_eof = eof;
1110 cp->fcoe_crc32 = cpu_to_le32(~crc);
1111
1112 if (skb_is_nonlinear(skb)) {
1113 kunmap_atomic(cp, KM_SKB_DATA_SOFTIRQ);
1114 cp = NULL;
1115 }
1116
Chris Leechdd3fd722009-04-21 16:27:36 -07001117 /* adjust skb network/transport offsets to match mac/fcoe/fc */
Robert Love85b4aa42008-12-09 15:10:24 -08001118 skb_push(skb, elen + hlen);
1119 skb_reset_mac_header(skb);
1120 skb_reset_network_header(skb);
1121 skb->mac_len = elen;
Yi Zou211c7382009-02-27 14:06:37 -08001122 skb->protocol = htons(ETH_P_FCOE);
Robert Love85b4aa42008-12-09 15:10:24 -08001123 skb->dev = fc->real_dev;
1124
1125 /* fill up mac and fcoe headers */
1126 eh = eth_hdr(skb);
1127 eh->h_proto = htons(ETH_P_FCOE);
Joe Eykholt97c83892009-03-17 11:42:40 -07001128 if (fc->ctlr.map_dest)
Robert Love85b4aa42008-12-09 15:10:24 -08001129 fc_fcoe_set_mac(eh->h_dest, fh->fh_d_id);
1130 else
1131 /* insert GW address */
Joe Eykholt97c83892009-03-17 11:42:40 -07001132 memcpy(eh->h_dest, fc->ctlr.dest_addr, ETH_ALEN);
Robert Love85b4aa42008-12-09 15:10:24 -08001133
Joe Eykholt97c83892009-03-17 11:42:40 -07001134 if (unlikely(fc->ctlr.flogi_oxid != FC_XID_UNKNOWN))
1135 memcpy(eh->h_source, fc->ctlr.ctl_src_addr, ETH_ALEN);
Robert Love85b4aa42008-12-09 15:10:24 -08001136 else
Joe Eykholt97c83892009-03-17 11:42:40 -07001137 memcpy(eh->h_source, fc->ctlr.data_src_addr, ETH_ALEN);
Robert Love85b4aa42008-12-09 15:10:24 -08001138
1139 hp = (struct fcoe_hdr *)(eh + 1);
1140 memset(hp, 0, sizeof(*hp));
1141 if (FC_FCOE_VER)
1142 FC_FCOE_ENCAPS_VER(hp, FC_FCOE_VER);
1143 hp->fcoe_sof = sof;
1144
Yi Zou39ca9a02009-02-27 14:07:15 -08001145#ifdef NETIF_F_FSO
1146 /* fcoe lso, mss is in max_payload which is non-zero for FCP data */
1147 if (lp->seq_offload && fr_max_payload(fp)) {
1148 skb_shinfo(skb)->gso_type = SKB_GSO_FCOE;
1149 skb_shinfo(skb)->gso_size = fr_max_payload(fp);
1150 } else {
1151 skb_shinfo(skb)->gso_type = 0;
1152 skb_shinfo(skb)->gso_size = 0;
1153 }
1154#endif
Robert Love85b4aa42008-12-09 15:10:24 -08001155 /* update tx stats: regardless if LLD fails */
Robert Love582b45b2009-03-31 15:51:50 -07001156 stats = fc_lport_get_stats(lp);
1157 stats->TxFrames++;
1158 stats->TxWords += wlen;
Robert Love85b4aa42008-12-09 15:10:24 -08001159
1160 /* send down to lld */
1161 fr_dev(fp) = lp;
1162 if (fc->fcoe_pending_queue.qlen)
Vasu Dev4bb6b512009-05-06 10:52:34 -07001163 fcoe_check_wait_queue(lp, skb);
1164 else if (fcoe_start_io(skb))
1165 fcoe_check_wait_queue(lp, skb);
Robert Love85b4aa42008-12-09 15:10:24 -08001166
1167 return 0;
1168}
Robert Love85b4aa42008-12-09 15:10:24 -08001169
Robert Love34f42a02009-02-27 10:55:45 -08001170/**
1171 * fcoe_percpu_receive_thread() - recv thread per cpu
Robert Love85b4aa42008-12-09 15:10:24 -08001172 * @arg: ptr to the fcoe per cpu struct
1173 *
1174 * Return: 0 for success
Robert Love85b4aa42008-12-09 15:10:24 -08001175 */
1176int fcoe_percpu_receive_thread(void *arg)
1177{
1178 struct fcoe_percpu_s *p = arg;
1179 u32 fr_len;
1180 struct fc_lport *lp;
1181 struct fcoe_rcv_info *fr;
1182 struct fcoe_dev_stats *stats;
1183 struct fc_frame_header *fh;
1184 struct sk_buff *skb;
1185 struct fcoe_crc_eof crc_eof;
1186 struct fc_frame *fp;
1187 u8 *mac = NULL;
1188 struct fcoe_softc *fc;
1189 struct fcoe_hdr *hp;
1190
Robert Love4469c192009-02-27 10:56:38 -08001191 set_user_nice(current, -20);
Robert Love85b4aa42008-12-09 15:10:24 -08001192
1193 while (!kthread_should_stop()) {
1194
1195 spin_lock_bh(&p->fcoe_rx_list.lock);
1196 while ((skb = __skb_dequeue(&p->fcoe_rx_list)) == NULL) {
1197 set_current_state(TASK_INTERRUPTIBLE);
1198 spin_unlock_bh(&p->fcoe_rx_list.lock);
1199 schedule();
1200 set_current_state(TASK_RUNNING);
1201 if (kthread_should_stop())
1202 return 0;
1203 spin_lock_bh(&p->fcoe_rx_list.lock);
1204 }
1205 spin_unlock_bh(&p->fcoe_rx_list.lock);
1206 fr = fcoe_dev_from_skb(skb);
1207 lp = fr->fr_dev;
1208 if (unlikely(lp == NULL)) {
Robert Loved5488eb2009-06-10 15:30:59 -07001209 FCOE_NETDEV_DBG(skb->dev, "Invalid HBA Structure");
Robert Love85b4aa42008-12-09 15:10:24 -08001210 kfree_skb(skb);
1211 continue;
1212 }
1213
Robert Loved5488eb2009-06-10 15:30:59 -07001214 FCOE_NETDEV_DBG(skb->dev, "skb_info: len:%d data_len:%d "
1215 "head:%p data:%p tail:%p end:%p sum:%d dev:%s",
1216 skb->len, skb->data_len,
1217 skb->head, skb->data, skb_tail_pointer(skb),
1218 skb_end_pointer(skb), skb->csum,
1219 skb->dev ? skb->dev->name : "<NULL>");
Robert Love85b4aa42008-12-09 15:10:24 -08001220
1221 /*
1222 * Save source MAC address before discarding header.
1223 */
1224 fc = lport_priv(lp);
Robert Love85b4aa42008-12-09 15:10:24 -08001225 if (skb_is_nonlinear(skb))
1226 skb_linearize(skb); /* not ideal */
Joe Eykholt97c83892009-03-17 11:42:40 -07001227 mac = eth_hdr(skb)->h_source;
Robert Love85b4aa42008-12-09 15:10:24 -08001228
1229 /*
1230 * Frame length checks and setting up the header pointers
1231 * was done in fcoe_rcv already.
1232 */
1233 hp = (struct fcoe_hdr *) skb_network_header(skb);
1234 fh = (struct fc_frame_header *) skb_transport_header(skb);
1235
Robert Love582b45b2009-03-31 15:51:50 -07001236 stats = fc_lport_get_stats(lp);
Robert Love85b4aa42008-12-09 15:10:24 -08001237 if (unlikely(FC_FCOE_DECAPS_VER(hp) != FC_FCOE_VER)) {
Robert Love582b45b2009-03-31 15:51:50 -07001238 if (stats->ErrorFrames < 5)
Robert Loved5488eb2009-06-10 15:30:59 -07001239 printk(KERN_WARNING "fcoe: FCoE version "
Robert Love582b45b2009-03-31 15:51:50 -07001240 "mismatch: The frame has "
1241 "version %x, but the "
1242 "initiator supports version "
1243 "%x\n", FC_FCOE_DECAPS_VER(hp),
1244 FC_FCOE_VER);
1245 stats->ErrorFrames++;
Robert Love85b4aa42008-12-09 15:10:24 -08001246 kfree_skb(skb);
1247 continue;
1248 }
1249
1250 skb_pull(skb, sizeof(struct fcoe_hdr));
1251 fr_len = skb->len - sizeof(struct fcoe_crc_eof);
1252
Robert Love582b45b2009-03-31 15:51:50 -07001253 stats->RxFrames++;
1254 stats->RxWords += fr_len / FCOE_WORD_TO_BYTE;
Robert Love85b4aa42008-12-09 15:10:24 -08001255
1256 fp = (struct fc_frame *)skb;
1257 fc_frame_init(fp);
1258 fr_dev(fp) = lp;
1259 fr_sof(fp) = hp->fcoe_sof;
1260
1261 /* Copy out the CRC and EOF trailer for access */
1262 if (skb_copy_bits(skb, fr_len, &crc_eof, sizeof(crc_eof))) {
1263 kfree_skb(skb);
1264 continue;
1265 }
1266 fr_eof(fp) = crc_eof.fcoe_eof;
1267 fr_crc(fp) = crc_eof.fcoe_crc32;
1268 if (pskb_trim(skb, fr_len)) {
1269 kfree_skb(skb);
1270 continue;
1271 }
1272
1273 /*
1274 * We only check CRC if no offload is available and if it is
1275 * it's solicited data, in which case, the FCP layer would
1276 * check it during the copy.
1277 */
Yi Zou07c00ec2009-02-27 14:07:31 -08001278 if (lp->crc_offload && skb->ip_summed == CHECKSUM_UNNECESSARY)
Robert Love85b4aa42008-12-09 15:10:24 -08001279 fr_flags(fp) &= ~FCPHF_CRC_UNCHECKED;
1280 else
1281 fr_flags(fp) |= FCPHF_CRC_UNCHECKED;
1282
1283 fh = fc_frame_header_get(fp);
1284 if (fh->fh_r_ctl == FC_RCTL_DD_SOL_DATA &&
1285 fh->fh_type == FC_TYPE_FCP) {
Vasu Dev52ff8782009-07-29 17:05:10 -07001286 fc_exch_recv(lp, fp);
Robert Love85b4aa42008-12-09 15:10:24 -08001287 continue;
1288 }
1289 if (fr_flags(fp) & FCPHF_CRC_UNCHECKED) {
1290 if (le32_to_cpu(fr_crc(fp)) !=
1291 ~crc32(~0, skb->data, fr_len)) {
Robert Loved5488eb2009-06-10 15:30:59 -07001292 if (stats->InvalidCRCCount < 5)
Robert Love85b4aa42008-12-09 15:10:24 -08001293 printk(KERN_WARNING "fcoe: dropping "
1294 "frame with CRC error\n");
1295 stats->InvalidCRCCount++;
1296 stats->ErrorFrames++;
1297 fc_frame_free(fp);
1298 continue;
1299 }
1300 fr_flags(fp) &= ~FCPHF_CRC_UNCHECKED;
1301 }
Joe Eykholt97c83892009-03-17 11:42:40 -07001302 if (unlikely(fc->ctlr.flogi_oxid != FC_XID_UNKNOWN) &&
1303 fcoe_ctlr_recv_flogi(&fc->ctlr, fp, mac)) {
1304 fc_frame_free(fp);
1305 continue;
1306 }
Vasu Dev52ff8782009-07-29 17:05:10 -07001307 fc_exch_recv(lp, fp);
Robert Love85b4aa42008-12-09 15:10:24 -08001308 }
1309 return 0;
1310}
1311
1312/**
Chris Leechdd3fd722009-04-21 16:27:36 -07001313 * fcoe_check_wait_queue() - attempt to clear the transmit backlog
1314 * @lp: the fc_lport
Robert Love85b4aa42008-12-09 15:10:24 -08001315 *
1316 * This empties the wait_queue, dequeue the head of the wait_queue queue
1317 * and calls fcoe_start_io() for each packet, if all skb have been
Vasu Devc826a312009-02-27 10:56:27 -08001318 * transmitted, return qlen or -1 if a error occurs, then restore
Chris Leechdd3fd722009-04-21 16:27:36 -07001319 * wait_queue and try again later.
Robert Love85b4aa42008-12-09 15:10:24 -08001320 *
1321 * The wait_queue is used when the skb transmit fails. skb will go
Chris Leechdd3fd722009-04-21 16:27:36 -07001322 * in the wait_queue which will be emptied by the timer function or
Robert Love85b4aa42008-12-09 15:10:24 -08001323 * by the next skb transmit.
Robert Love34f42a02009-02-27 10:55:45 -08001324 */
Vasu Dev4bb6b512009-05-06 10:52:34 -07001325static void fcoe_check_wait_queue(struct fc_lport *lp, struct sk_buff *skb)
Robert Love85b4aa42008-12-09 15:10:24 -08001326{
Chris Leech55c8baf2009-02-27 10:56:32 -08001327 struct fcoe_softc *fc = lport_priv(lp);
Vasu Dev4bb6b512009-05-06 10:52:34 -07001328 int rc;
Robert Love85b4aa42008-12-09 15:10:24 -08001329
Robert Love85b4aa42008-12-09 15:10:24 -08001330 spin_lock_bh(&fc->fcoe_pending_queue.lock);
Vasu Dev4bb6b512009-05-06 10:52:34 -07001331
1332 if (skb)
1333 __skb_queue_tail(&fc->fcoe_pending_queue, skb);
1334
Vasu Devc826a312009-02-27 10:56:27 -08001335 if (fc->fcoe_pending_queue_active)
1336 goto out;
1337 fc->fcoe_pending_queue_active = 1;
Chris Leech55c8baf2009-02-27 10:56:32 -08001338
1339 while (fc->fcoe_pending_queue.qlen) {
1340 /* keep qlen > 0 until fcoe_start_io succeeds */
1341 fc->fcoe_pending_queue.qlen++;
1342 skb = __skb_dequeue(&fc->fcoe_pending_queue);
1343
1344 spin_unlock_bh(&fc->fcoe_pending_queue.lock);
1345 rc = fcoe_start_io(skb);
1346 spin_lock_bh(&fc->fcoe_pending_queue.lock);
1347
1348 if (rc) {
1349 __skb_queue_head(&fc->fcoe_pending_queue, skb);
1350 /* undo temporary increment above */
1351 fc->fcoe_pending_queue.qlen--;
1352 break;
Robert Love85b4aa42008-12-09 15:10:24 -08001353 }
Chris Leech55c8baf2009-02-27 10:56:32 -08001354 /* undo temporary increment above */
1355 fc->fcoe_pending_queue.qlen--;
Robert Love85b4aa42008-12-09 15:10:24 -08001356 }
Chris Leech55c8baf2009-02-27 10:56:32 -08001357
1358 if (fc->fcoe_pending_queue.qlen < FCOE_LOW_QUEUE_DEPTH)
1359 lp->qfull = 0;
Vasu Dev1047f222009-05-06 10:52:40 -07001360 if (fc->fcoe_pending_queue.qlen && !timer_pending(&fc->timer))
1361 mod_timer(&fc->timer, jiffies + 2);
Vasu Devc826a312009-02-27 10:56:27 -08001362 fc->fcoe_pending_queue_active = 0;
Vasu Devc826a312009-02-27 10:56:27 -08001363out:
Vasu Dev4bb6b512009-05-06 10:52:34 -07001364 if (fc->fcoe_pending_queue.qlen > FCOE_MAX_QUEUE_DEPTH)
1365 lp->qfull = 1;
Robert Love85b4aa42008-12-09 15:10:24 -08001366 spin_unlock_bh(&fc->fcoe_pending_queue.lock);
Vasu Dev4bb6b512009-05-06 10:52:34 -07001367 return;
Robert Love85b4aa42008-12-09 15:10:24 -08001368}
1369
1370/**
Robert Love34f42a02009-02-27 10:55:45 -08001371 * fcoe_dev_setup() - setup link change notification interface
1372 */
Randy Dunlapb0d428a2009-04-27 21:49:31 -07001373static void fcoe_dev_setup(void)
Robert Love85b4aa42008-12-09 15:10:24 -08001374{
Robert Love85b4aa42008-12-09 15:10:24 -08001375 register_netdevice_notifier(&fcoe_notifier);
1376}
1377
1378/**
Randy Dunlapb0d428a2009-04-27 21:49:31 -07001379 * fcoe_dev_cleanup() - cleanup link change notification interface
Robert Love34f42a02009-02-27 10:55:45 -08001380 */
Robert Love85b4aa42008-12-09 15:10:24 -08001381static void fcoe_dev_cleanup(void)
1382{
1383 unregister_netdevice_notifier(&fcoe_notifier);
1384}
1385
1386/**
Robert Love34f42a02009-02-27 10:55:45 -08001387 * fcoe_device_notification() - netdev event notification callback
Robert Love85b4aa42008-12-09 15:10:24 -08001388 * @notifier: context of the notification
1389 * @event: type of event
1390 * @ptr: fixed array for output parsed ifname
1391 *
1392 * This function is called by the ethernet driver in case of link change event
1393 *
1394 * Returns: 0 for success
Robert Love34f42a02009-02-27 10:55:45 -08001395 */
Robert Love85b4aa42008-12-09 15:10:24 -08001396static int fcoe_device_notification(struct notifier_block *notifier,
1397 ulong event, void *ptr)
1398{
1399 struct fc_lport *lp = NULL;
1400 struct net_device *real_dev = ptr;
1401 struct fcoe_softc *fc;
1402 struct fcoe_dev_stats *stats;
Joe Eykholt97c83892009-03-17 11:42:40 -07001403 u32 link_possible = 1;
Robert Love85b4aa42008-12-09 15:10:24 -08001404 u32 mfs;
1405 int rc = NOTIFY_OK;
1406
1407 read_lock(&fcoe_hostlist_lock);
1408 list_for_each_entry(fc, &fcoe_hostlist, list) {
1409 if (fc->real_dev == real_dev) {
Joe Eykholt97c83892009-03-17 11:42:40 -07001410 lp = fc->ctlr.lp;
Robert Love85b4aa42008-12-09 15:10:24 -08001411 break;
1412 }
1413 }
1414 read_unlock(&fcoe_hostlist_lock);
1415 if (lp == NULL) {
1416 rc = NOTIFY_DONE;
1417 goto out;
1418 }
1419
Robert Love85b4aa42008-12-09 15:10:24 -08001420 switch (event) {
1421 case NETDEV_DOWN:
1422 case NETDEV_GOING_DOWN:
Joe Eykholt97c83892009-03-17 11:42:40 -07001423 link_possible = 0;
Robert Love85b4aa42008-12-09 15:10:24 -08001424 break;
1425 case NETDEV_UP:
1426 case NETDEV_CHANGE:
Robert Love85b4aa42008-12-09 15:10:24 -08001427 break;
1428 case NETDEV_CHANGEMTU:
1429 mfs = fc->real_dev->mtu -
1430 (sizeof(struct fcoe_hdr) +
1431 sizeof(struct fcoe_crc_eof));
1432 if (mfs >= FC_MIN_MAX_FRAME)
1433 fc_set_mfs(lp, mfs);
Robert Love85b4aa42008-12-09 15:10:24 -08001434 break;
1435 case NETDEV_REGISTER:
1436 break;
1437 default:
Robert Loved5488eb2009-06-10 15:30:59 -07001438 FCOE_NETDEV_DBG(real_dev, "Unknown event %ld "
1439 "from netdev netlink\n", event);
Robert Love85b4aa42008-12-09 15:10:24 -08001440 }
Joe Eykholt97c83892009-03-17 11:42:40 -07001441 if (link_possible && !fcoe_link_ok(lp))
1442 fcoe_ctlr_link_up(&fc->ctlr);
1443 else if (fcoe_ctlr_link_down(&fc->ctlr)) {
1444 stats = fc_lport_get_stats(lp);
1445 stats->LinkFailureCount++;
1446 fcoe_clean_pending_queue(lp);
Robert Love85b4aa42008-12-09 15:10:24 -08001447 }
1448out:
1449 return rc;
1450}
1451
1452/**
Robert Love34f42a02009-02-27 10:55:45 -08001453 * fcoe_if_to_netdev() - parse a name buffer to get netdev
Robert Love85b4aa42008-12-09 15:10:24 -08001454 * @buffer: incoming buffer to be copied
1455 *
Chris Leechdd3fd722009-04-21 16:27:36 -07001456 * Returns: NULL or ptr to net_device
Robert Love34f42a02009-02-27 10:55:45 -08001457 */
Robert Love85b4aa42008-12-09 15:10:24 -08001458static struct net_device *fcoe_if_to_netdev(const char *buffer)
1459{
1460 char *cp;
1461 char ifname[IFNAMSIZ + 2];
1462
1463 if (buffer) {
1464 strlcpy(ifname, buffer, IFNAMSIZ);
1465 cp = ifname + strlen(ifname);
1466 while (--cp >= ifname && *cp == '\n')
1467 *cp = '\0';
1468 return dev_get_by_name(&init_net, ifname);
1469 }
1470 return NULL;
1471}
1472
1473/**
Chris Leechdd3fd722009-04-21 16:27:36 -07001474 * fcoe_netdev_to_module_owner() - finds out the driver module of the netdev
Robert Love85b4aa42008-12-09 15:10:24 -08001475 * @netdev: the target netdev
1476 *
1477 * Returns: ptr to the struct module, NULL for failure
Robert Love34f42a02009-02-27 10:55:45 -08001478 */
Robert Loveb2ab99c2009-02-27 10:55:50 -08001479static struct module *
1480fcoe_netdev_to_module_owner(const struct net_device *netdev)
Robert Love85b4aa42008-12-09 15:10:24 -08001481{
1482 struct device *dev;
1483
1484 if (!netdev)
1485 return NULL;
1486
1487 dev = netdev->dev.parent;
1488 if (!dev)
1489 return NULL;
1490
1491 if (!dev->driver)
1492 return NULL;
1493
1494 return dev->driver->owner;
1495}
1496
1497/**
Robert Love34f42a02009-02-27 10:55:45 -08001498 * fcoe_ethdrv_get() - Hold the Ethernet driver
Robert Love85b4aa42008-12-09 15:10:24 -08001499 * @netdev: the target netdev
1500 *
Robert Love34f42a02009-02-27 10:55:45 -08001501 * Holds the Ethernet driver module by try_module_get() for
1502 * the corresponding netdev.
1503 *
Chris Leechdd3fd722009-04-21 16:27:36 -07001504 * Returns: 0 for success
Robert Love34f42a02009-02-27 10:55:45 -08001505 */
Robert Love85b4aa42008-12-09 15:10:24 -08001506static int fcoe_ethdrv_get(const struct net_device *netdev)
1507{
1508 struct module *owner;
1509
1510 owner = fcoe_netdev_to_module_owner(netdev);
1511 if (owner) {
Robert Loved5488eb2009-06-10 15:30:59 -07001512 FCOE_NETDEV_DBG(netdev, "Hold driver module %s\n",
1513 module_name(owner));
Robert Love85b4aa42008-12-09 15:10:24 -08001514 return try_module_get(owner);
1515 }
1516 return -ENODEV;
1517}
1518
1519/**
Robert Love34f42a02009-02-27 10:55:45 -08001520 * fcoe_ethdrv_put() - Release the Ethernet driver
Robert Love85b4aa42008-12-09 15:10:24 -08001521 * @netdev: the target netdev
1522 *
Robert Love34f42a02009-02-27 10:55:45 -08001523 * Releases the Ethernet driver module by module_put for
1524 * the corresponding netdev.
1525 *
Chris Leechdd3fd722009-04-21 16:27:36 -07001526 * Returns: 0 for success
Robert Love34f42a02009-02-27 10:55:45 -08001527 */
Robert Love85b4aa42008-12-09 15:10:24 -08001528static int fcoe_ethdrv_put(const struct net_device *netdev)
1529{
1530 struct module *owner;
1531
1532 owner = fcoe_netdev_to_module_owner(netdev);
1533 if (owner) {
Robert Loved5488eb2009-06-10 15:30:59 -07001534 FCOE_NETDEV_DBG(netdev, "Release driver module %s\n",
1535 module_name(owner));
Robert Love85b4aa42008-12-09 15:10:24 -08001536 module_put(owner);
1537 return 0;
1538 }
1539 return -ENODEV;
1540}
1541
1542/**
Robert Love34f42a02009-02-27 10:55:45 -08001543 * fcoe_destroy() - handles the destroy from sysfs
Chris Leechdd3fd722009-04-21 16:27:36 -07001544 * @buffer: expected to be an eth if name
Robert Love85b4aa42008-12-09 15:10:24 -08001545 * @kp: associated kernel param
1546 *
1547 * Returns: 0 for success
Robert Love34f42a02009-02-27 10:55:45 -08001548 */
Robert Love85b4aa42008-12-09 15:10:24 -08001549static int fcoe_destroy(const char *buffer, struct kernel_param *kp)
1550{
1551 int rc;
1552 struct net_device *netdev;
1553
1554 netdev = fcoe_if_to_netdev(buffer);
1555 if (!netdev) {
1556 rc = -ENODEV;
1557 goto out_nodev;
1558 }
1559 /* look for existing lport */
1560 if (!fcoe_hostlist_lookup(netdev)) {
1561 rc = -ENODEV;
1562 goto out_putdev;
1563 }
Vasu Dev7f349142009-03-27 09:06:31 -07001564 rc = fcoe_if_destroy(netdev);
Robert Love85b4aa42008-12-09 15:10:24 -08001565 if (rc) {
Robert Loved5488eb2009-06-10 15:30:59 -07001566 printk(KERN_ERR "fcoe: Failed to destroy interface (%s)\n",
Robert Love85b4aa42008-12-09 15:10:24 -08001567 netdev->name);
1568 rc = -EIO;
1569 goto out_putdev;
1570 }
1571 fcoe_ethdrv_put(netdev);
1572 rc = 0;
1573out_putdev:
1574 dev_put(netdev);
1575out_nodev:
1576 return rc;
1577}
1578
1579/**
Robert Love34f42a02009-02-27 10:55:45 -08001580 * fcoe_create() - Handles the create call from sysfs
Chris Leechdd3fd722009-04-21 16:27:36 -07001581 * @buffer: expected to be an eth if name
Robert Love85b4aa42008-12-09 15:10:24 -08001582 * @kp: associated kernel param
1583 *
1584 * Returns: 0 for success
Robert Love34f42a02009-02-27 10:55:45 -08001585 */
Robert Love85b4aa42008-12-09 15:10:24 -08001586static int fcoe_create(const char *buffer, struct kernel_param *kp)
1587{
1588 int rc;
1589 struct net_device *netdev;
1590
1591 netdev = fcoe_if_to_netdev(buffer);
1592 if (!netdev) {
1593 rc = -ENODEV;
1594 goto out_nodev;
1595 }
1596 /* look for existing lport */
1597 if (fcoe_hostlist_lookup(netdev)) {
1598 rc = -EEXIST;
1599 goto out_putdev;
1600 }
1601 fcoe_ethdrv_get(netdev);
1602
Vasu Dev7f349142009-03-27 09:06:31 -07001603 rc = fcoe_if_create(netdev);
Robert Love85b4aa42008-12-09 15:10:24 -08001604 if (rc) {
Robert Loved5488eb2009-06-10 15:30:59 -07001605 printk(KERN_ERR "fcoe: Failed to create interface (%s)\n",
Robert Love85b4aa42008-12-09 15:10:24 -08001606 netdev->name);
1607 fcoe_ethdrv_put(netdev);
1608 rc = -EIO;
1609 goto out_putdev;
1610 }
1611 rc = 0;
1612out_putdev:
1613 dev_put(netdev);
1614out_nodev:
1615 return rc;
1616}
1617
1618module_param_call(create, fcoe_create, NULL, NULL, S_IWUSR);
1619__MODULE_PARM_TYPE(create, "string");
1620MODULE_PARM_DESC(create, "Create fcoe port using net device passed in.");
1621module_param_call(destroy, fcoe_destroy, NULL, NULL, S_IWUSR);
1622__MODULE_PARM_TYPE(destroy, "string");
1623MODULE_PARM_DESC(destroy, "Destroy fcoe port");
1624
Robert Love34f42a02009-02-27 10:55:45 -08001625/**
1626 * fcoe_link_ok() - Check if link is ok for the fc_lport
Robert Love85b4aa42008-12-09 15:10:24 -08001627 * @lp: ptr to the fc_lport
1628 *
1629 * Any permanently-disqualifying conditions have been previously checked.
1630 * This also updates the speed setting, which may change with link for 100/1000.
1631 *
1632 * This function should probably be checking for PAUSE support at some point
1633 * in the future. Currently Per-priority-pause is not determinable using
1634 * ethtool, so we shouldn't be restrictive until that problem is resolved.
1635 *
1636 * Returns: 0 if link is OK for use by FCoE.
1637 *
1638 */
1639int fcoe_link_ok(struct fc_lport *lp)
1640{
Robert Lovefc47ff62009-02-27 10:55:55 -08001641 struct fcoe_softc *fc = lport_priv(lp);
Robert Love85b4aa42008-12-09 15:10:24 -08001642 struct net_device *dev = fc->real_dev;
1643 struct ethtool_cmd ecmd = { ETHTOOL_GSET };
Robert Love85b4aa42008-12-09 15:10:24 -08001644
Yi Zou2f718d62009-07-29 17:04:01 -07001645 if ((dev->flags & IFF_UP) && netif_carrier_ok(dev) &&
1646 (!dev_ethtool_get_settings(dev, &ecmd))) {
1647 lp->link_supported_speeds &=
1648 ~(FC_PORTSPEED_1GBIT | FC_PORTSPEED_10GBIT);
1649 if (ecmd.supported & (SUPPORTED_1000baseT_Half |
1650 SUPPORTED_1000baseT_Full))
1651 lp->link_supported_speeds |= FC_PORTSPEED_1GBIT;
1652 if (ecmd.supported & SUPPORTED_10000baseT_Full)
1653 lp->link_supported_speeds |=
1654 FC_PORTSPEED_10GBIT;
1655 if (ecmd.speed == SPEED_1000)
1656 lp->link_speed = FC_PORTSPEED_1GBIT;
1657 if (ecmd.speed == SPEED_10000)
1658 lp->link_speed = FC_PORTSPEED_10GBIT;
Robert Love85b4aa42008-12-09 15:10:24 -08001659
Yi Zou2f718d62009-07-29 17:04:01 -07001660 return 0;
1661 }
1662 return -1;
Robert Love85b4aa42008-12-09 15:10:24 -08001663}
Robert Love85b4aa42008-12-09 15:10:24 -08001664
Robert Love34f42a02009-02-27 10:55:45 -08001665/**
1666 * fcoe_percpu_clean() - Clear the pending skbs for an lport
Robert Love85b4aa42008-12-09 15:10:24 -08001667 * @lp: the fc_lport
1668 */
1669void fcoe_percpu_clean(struct fc_lport *lp)
1670{
Robert Love85b4aa42008-12-09 15:10:24 -08001671 struct fcoe_percpu_s *pp;
1672 struct fcoe_rcv_info *fr;
1673 struct sk_buff_head *list;
1674 struct sk_buff *skb, *next;
1675 struct sk_buff *head;
Robert Love5e5e92d2009-03-17 11:41:35 -07001676 unsigned int cpu;
Robert Love85b4aa42008-12-09 15:10:24 -08001677
Robert Love5e5e92d2009-03-17 11:41:35 -07001678 for_each_possible_cpu(cpu) {
1679 pp = &per_cpu(fcoe_percpu, cpu);
1680 spin_lock_bh(&pp->fcoe_rx_list.lock);
1681 list = &pp->fcoe_rx_list;
1682 head = list->next;
1683 for (skb = head; skb != (struct sk_buff *)list;
1684 skb = next) {
1685 next = skb->next;
1686 fr = fcoe_dev_from_skb(skb);
1687 if (fr->fr_dev == lp) {
1688 __skb_unlink(skb, list);
1689 kfree_skb(skb);
Robert Love85b4aa42008-12-09 15:10:24 -08001690 }
Robert Love85b4aa42008-12-09 15:10:24 -08001691 }
Robert Love5e5e92d2009-03-17 11:41:35 -07001692 spin_unlock_bh(&pp->fcoe_rx_list.lock);
Robert Love85b4aa42008-12-09 15:10:24 -08001693 }
1694}
Robert Love85b4aa42008-12-09 15:10:24 -08001695
1696/**
Robert Love34f42a02009-02-27 10:55:45 -08001697 * fcoe_clean_pending_queue() - Dequeue a skb and free it
Robert Love85b4aa42008-12-09 15:10:24 -08001698 * @lp: the corresponding fc_lport
1699 *
1700 * Returns: none
Robert Love34f42a02009-02-27 10:55:45 -08001701 */
Robert Love85b4aa42008-12-09 15:10:24 -08001702void fcoe_clean_pending_queue(struct fc_lport *lp)
1703{
1704 struct fcoe_softc *fc = lport_priv(lp);
1705 struct sk_buff *skb;
1706
1707 spin_lock_bh(&fc->fcoe_pending_queue.lock);
1708 while ((skb = __skb_dequeue(&fc->fcoe_pending_queue)) != NULL) {
1709 spin_unlock_bh(&fc->fcoe_pending_queue.lock);
1710 kfree_skb(skb);
1711 spin_lock_bh(&fc->fcoe_pending_queue.lock);
1712 }
1713 spin_unlock_bh(&fc->fcoe_pending_queue.lock);
1714}
Robert Love85b4aa42008-12-09 15:10:24 -08001715
1716/**
Robert Love34f42a02009-02-27 10:55:45 -08001717 * fcoe_reset() - Resets the fcoe
Robert Love85b4aa42008-12-09 15:10:24 -08001718 * @shost: shost the reset is from
1719 *
1720 * Returns: always 0
1721 */
1722int fcoe_reset(struct Scsi_Host *shost)
1723{
1724 struct fc_lport *lport = shost_priv(shost);
1725 fc_lport_reset(lport);
1726 return 0;
1727}
Robert Love85b4aa42008-12-09 15:10:24 -08001728
Robert Love34f42a02009-02-27 10:55:45 -08001729/**
Robert Love34f42a02009-02-27 10:55:45 -08001730 * fcoe_hostlist_lookup_softc() - find the corresponding lport by a given device
Chris Leechdd3fd722009-04-21 16:27:36 -07001731 * @dev: this is currently ptr to net_device
Robert Love85b4aa42008-12-09 15:10:24 -08001732 *
Vasu Deve8af4d42009-07-29 17:05:15 -07001733 * Called with fcoe_hostlist_lock held.
1734 *
Robert Love85b4aa42008-12-09 15:10:24 -08001735 * Returns: NULL or the located fcoe_softc
1736 */
Robert Loveb2ab99c2009-02-27 10:55:50 -08001737static struct fcoe_softc *
1738fcoe_hostlist_lookup_softc(const struct net_device *dev)
Robert Love85b4aa42008-12-09 15:10:24 -08001739{
1740 struct fcoe_softc *fc;
1741
Robert Love85b4aa42008-12-09 15:10:24 -08001742 list_for_each_entry(fc, &fcoe_hostlist, list) {
Vasu Deve8af4d42009-07-29 17:05:15 -07001743 if (fc->real_dev == dev)
Robert Love85b4aa42008-12-09 15:10:24 -08001744 return fc;
Robert Love85b4aa42008-12-09 15:10:24 -08001745 }
Robert Love85b4aa42008-12-09 15:10:24 -08001746 return NULL;
1747}
1748
Robert Love34f42a02009-02-27 10:55:45 -08001749/**
1750 * fcoe_hostlist_lookup() - Find the corresponding lport by netdev
Robert Love85b4aa42008-12-09 15:10:24 -08001751 * @netdev: ptr to net_device
1752 *
1753 * Returns: 0 for success
1754 */
1755struct fc_lport *fcoe_hostlist_lookup(const struct net_device *netdev)
1756{
1757 struct fcoe_softc *fc;
1758
Vasu Deve8af4d42009-07-29 17:05:15 -07001759 read_lock(&fcoe_hostlist_lock);
Robert Love85b4aa42008-12-09 15:10:24 -08001760 fc = fcoe_hostlist_lookup_softc(netdev);
Vasu Deve8af4d42009-07-29 17:05:15 -07001761 read_unlock(&fcoe_hostlist_lock);
Robert Love85b4aa42008-12-09 15:10:24 -08001762
Joe Eykholt97c83892009-03-17 11:42:40 -07001763 return (fc) ? fc->ctlr.lp : NULL;
Robert Love85b4aa42008-12-09 15:10:24 -08001764}
Robert Love85b4aa42008-12-09 15:10:24 -08001765
Robert Love34f42a02009-02-27 10:55:45 -08001766/**
1767 * fcoe_hostlist_add() - Add a lport to lports list
Chris Leechdd3fd722009-04-21 16:27:36 -07001768 * @lp: ptr to the fc_lport to be added
Robert Love85b4aa42008-12-09 15:10:24 -08001769 *
Vasu Deve8af4d42009-07-29 17:05:15 -07001770 * Called with write fcoe_hostlist_lock held.
1771 *
Robert Love85b4aa42008-12-09 15:10:24 -08001772 * Returns: 0 for success
1773 */
1774int fcoe_hostlist_add(const struct fc_lport *lp)
1775{
1776 struct fcoe_softc *fc;
1777
1778 fc = fcoe_hostlist_lookup_softc(fcoe_netdev(lp));
1779 if (!fc) {
Robert Lovefc47ff62009-02-27 10:55:55 -08001780 fc = lport_priv(lp);
Robert Love85b4aa42008-12-09 15:10:24 -08001781 list_add_tail(&fc->list, &fcoe_hostlist);
Robert Love85b4aa42008-12-09 15:10:24 -08001782 }
1783 return 0;
1784}
Robert Love85b4aa42008-12-09 15:10:24 -08001785
Robert Love34f42a02009-02-27 10:55:45 -08001786/**
1787 * fcoe_hostlist_remove() - remove a lport from lports list
Chris Leechdd3fd722009-04-21 16:27:36 -07001788 * @lp: ptr to the fc_lport to be removed
Robert Love85b4aa42008-12-09 15:10:24 -08001789 *
1790 * Returns: 0 for success
1791 */
1792int fcoe_hostlist_remove(const struct fc_lport *lp)
1793{
1794 struct fcoe_softc *fc;
1795
Vasu Deve8af4d42009-07-29 17:05:15 -07001796 write_lock_bh(&fcoe_hostlist_lock);
Robert Love85b4aa42008-12-09 15:10:24 -08001797 fc = fcoe_hostlist_lookup_softc(fcoe_netdev(lp));
1798 BUG_ON(!fc);
Robert Love85b4aa42008-12-09 15:10:24 -08001799 list_del(&fc->list);
1800 write_unlock_bh(&fcoe_hostlist_lock);
1801
1802 return 0;
1803}
Robert Love85b4aa42008-12-09 15:10:24 -08001804
1805/**
Robert Love34f42a02009-02-27 10:55:45 -08001806 * fcoe_init() - fcoe module loading initialization
Robert Love85b4aa42008-12-09 15:10:24 -08001807 *
Robert Love85b4aa42008-12-09 15:10:24 -08001808 * Returns 0 on success, negative on failure
Robert Love34f42a02009-02-27 10:55:45 -08001809 */
Robert Love85b4aa42008-12-09 15:10:24 -08001810static int __init fcoe_init(void)
1811{
Robert Love38eccab2009-03-17 11:41:30 -07001812 unsigned int cpu;
Robert Love8976f422009-03-17 11:41:46 -07001813 int rc = 0;
Robert Love85b4aa42008-12-09 15:10:24 -08001814 struct fcoe_percpu_s *p;
1815
Robert Love85b4aa42008-12-09 15:10:24 -08001816 INIT_LIST_HEAD(&fcoe_hostlist);
1817 rwlock_init(&fcoe_hostlist_lock);
1818
Robert Love38eccab2009-03-17 11:41:30 -07001819 for_each_possible_cpu(cpu) {
Robert Love5e5e92d2009-03-17 11:41:35 -07001820 p = &per_cpu(fcoe_percpu, cpu);
Robert Love38eccab2009-03-17 11:41:30 -07001821 skb_queue_head_init(&p->fcoe_rx_list);
1822 }
1823
Robert Love8976f422009-03-17 11:41:46 -07001824 for_each_online_cpu(cpu)
1825 fcoe_percpu_thread_create(cpu);
Robert Love85b4aa42008-12-09 15:10:24 -08001826
Robert Love8976f422009-03-17 11:41:46 -07001827 /* Initialize per CPU interrupt thread */
1828 rc = register_hotcpu_notifier(&fcoe_cpu_notifier);
1829 if (rc)
1830 goto out_free;
Robert Love85b4aa42008-12-09 15:10:24 -08001831
Robert Love8976f422009-03-17 11:41:46 -07001832 /* Setup link change notification */
Robert Love85b4aa42008-12-09 15:10:24 -08001833 fcoe_dev_setup();
1834
Vasu Dev7f349142009-03-27 09:06:31 -07001835 fcoe_if_init();
Robert Love85b4aa42008-12-09 15:10:24 -08001836
1837 return 0;
Robert Love8976f422009-03-17 11:41:46 -07001838
1839out_free:
1840 for_each_online_cpu(cpu) {
1841 fcoe_percpu_thread_destroy(cpu);
1842 }
1843
1844 return rc;
Robert Love85b4aa42008-12-09 15:10:24 -08001845}
1846module_init(fcoe_init);
1847
1848/**
Robert Love34f42a02009-02-27 10:55:45 -08001849 * fcoe_exit() - fcoe module unloading cleanup
Robert Love85b4aa42008-12-09 15:10:24 -08001850 *
1851 * Returns 0 on success, negative on failure
Robert Love34f42a02009-02-27 10:55:45 -08001852 */
Robert Love85b4aa42008-12-09 15:10:24 -08001853static void __exit fcoe_exit(void)
1854{
Robert Love5e5e92d2009-03-17 11:41:35 -07001855 unsigned int cpu;
Robert Love85b4aa42008-12-09 15:10:24 -08001856 struct fcoe_softc *fc, *tmp;
Robert Love85b4aa42008-12-09 15:10:24 -08001857
Robert Love85b4aa42008-12-09 15:10:24 -08001858 fcoe_dev_cleanup();
1859
Vasu Dev5919a592009-03-27 09:03:29 -07001860 /* releases the associated fcoe hosts */
Robert Love85b4aa42008-12-09 15:10:24 -08001861 list_for_each_entry_safe(fc, tmp, &fcoe_hostlist, list)
Vasu Dev7f349142009-03-27 09:06:31 -07001862 fcoe_if_destroy(fc->real_dev);
Robert Love85b4aa42008-12-09 15:10:24 -08001863
Robert Love8976f422009-03-17 11:41:46 -07001864 unregister_hotcpu_notifier(&fcoe_cpu_notifier);
1865
1866 for_each_online_cpu(cpu) {
1867 fcoe_percpu_thread_destroy(cpu);
Robert Love85b4aa42008-12-09 15:10:24 -08001868 }
1869
Vasu Dev7f349142009-03-27 09:06:31 -07001870 /* detach from scsi transport */
1871 fcoe_if_exit();
Robert Love85b4aa42008-12-09 15:10:24 -08001872}
1873module_exit(fcoe_exit);