blob: 437eacf2732d74856adf0c659974695f065617d2 [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
Chris Leechdfc1d0f2009-08-25 14:00:13 -070058DEFINE_MUTEX(fcoe_config_mutex);
59
Joe Eykholte7a51992009-08-25 14:04:08 -070060/* fcoe_percpu_clean completion. Waiter protected by fcoe_create_mutex */
61static DECLARE_COMPLETION(fcoe_flush_completion);
62
Robert Love85b4aa42008-12-09 15:10:24 -080063/* fcoe host list */
Chris Leech090eb6c2009-08-25 14:00:28 -070064/* must only by accessed under the RTNL mutex */
Robert Love85b4aa42008-12-09 15:10:24 -080065LIST_HEAD(fcoe_hostlist);
Robert Love5e5e92d2009-03-17 11:41:35 -070066DEFINE_PER_CPU(struct fcoe_percpu_s, fcoe_percpu);
Robert Love85b4aa42008-12-09 15:10:24 -080067
Chris Leechdd3fd722009-04-21 16:27:36 -070068/* Function Prototypes */
Vasu Devfdd78022009-03-17 11:42:24 -070069static int fcoe_reset(struct Scsi_Host *shost);
70static int fcoe_xmit(struct fc_lport *, struct fc_frame *);
71static int fcoe_rcv(struct sk_buff *, struct net_device *,
72 struct packet_type *, struct net_device *);
73static int fcoe_percpu_receive_thread(void *arg);
74static void fcoe_clean_pending_queue(struct fc_lport *lp);
75static void fcoe_percpu_clean(struct fc_lport *lp);
76static int fcoe_link_ok(struct fc_lport *lp);
77
78static struct fc_lport *fcoe_hostlist_lookup(const struct net_device *);
79static int fcoe_hostlist_add(const struct fc_lport *);
Vasu Devfdd78022009-03-17 11:42:24 -070080
Vasu Dev4bb6b512009-05-06 10:52:34 -070081static void fcoe_check_wait_queue(struct fc_lport *, struct sk_buff *);
Robert Love85b4aa42008-12-09 15:10:24 -080082static int fcoe_device_notification(struct notifier_block *, ulong, void *);
83static void fcoe_dev_setup(void);
84static void fcoe_dev_cleanup(void);
Chris Leech2e70e242009-08-25 14:00:23 -070085static struct fcoe_interface *
86 fcoe_hostlist_lookup_port(const struct net_device *dev);
Robert Love85b4aa42008-12-09 15:10:24 -080087
88/* notification function from net device */
89static struct notifier_block fcoe_notifier = {
90 .notifier_call = fcoe_device_notification,
91};
92
Chris Leeche9084bb2009-11-03 11:46:34 -080093static struct scsi_transport_template *fcoe_transport_template;
94static struct scsi_transport_template *fcoe_vport_transport_template;
Vasu Dev7f349142009-03-27 09:06:31 -070095
Chris Leech9a057532009-11-03 11:46:40 -080096static int fcoe_vport_destroy(struct fc_vport *vport);
97static int fcoe_vport_create(struct fc_vport *vport, bool disabled);
98static int fcoe_vport_disable(struct fc_vport *vport, bool disable);
99
Vasu Dev7f349142009-03-27 09:06:31 -0700100struct fc_function_template fcoe_transport_function = {
101 .show_host_node_name = 1,
102 .show_host_port_name = 1,
103 .show_host_supported_classes = 1,
104 .show_host_supported_fc4s = 1,
105 .show_host_active_fc4s = 1,
106 .show_host_maxframe_size = 1,
107
108 .show_host_port_id = 1,
109 .show_host_supported_speeds = 1,
110 .get_host_speed = fc_get_host_speed,
111 .show_host_speed = 1,
112 .show_host_port_type = 1,
113 .get_host_port_state = fc_get_host_port_state,
114 .show_host_port_state = 1,
115 .show_host_symbolic_name = 1,
116
117 .dd_fcrport_size = sizeof(struct fc_rport_libfc_priv),
118 .show_rport_maxframe_size = 1,
119 .show_rport_supported_classes = 1,
120
121 .show_host_fabric_name = 1,
122 .show_starget_node_name = 1,
123 .show_starget_port_name = 1,
124 .show_starget_port_id = 1,
125 .set_rport_dev_loss_tmo = fc_set_rport_loss_tmo,
126 .show_rport_dev_loss_tmo = 1,
127 .get_fc_host_stats = fc_get_host_stats,
128 .issue_fc_host_lip = fcoe_reset,
129
130 .terminate_rport_io = fc_rport_terminate_io,
Chris Leech9a057532009-11-03 11:46:40 -0800131
132 .vport_create = fcoe_vport_create,
133 .vport_delete = fcoe_vport_destroy,
134 .vport_disable = fcoe_vport_disable,
Vasu Dev7f349142009-03-27 09:06:31 -0700135};
136
Chris Leeche9084bb2009-11-03 11:46:34 -0800137struct fc_function_template fcoe_vport_transport_function = {
138 .show_host_node_name = 1,
139 .show_host_port_name = 1,
140 .show_host_supported_classes = 1,
141 .show_host_supported_fc4s = 1,
142 .show_host_active_fc4s = 1,
143 .show_host_maxframe_size = 1,
144
145 .show_host_port_id = 1,
146 .show_host_supported_speeds = 1,
147 .get_host_speed = fc_get_host_speed,
148 .show_host_speed = 1,
149 .show_host_port_type = 1,
150 .get_host_port_state = fc_get_host_port_state,
151 .show_host_port_state = 1,
152 .show_host_symbolic_name = 1,
153
154 .dd_fcrport_size = sizeof(struct fc_rport_libfc_priv),
155 .show_rport_maxframe_size = 1,
156 .show_rport_supported_classes = 1,
157
158 .show_host_fabric_name = 1,
159 .show_starget_node_name = 1,
160 .show_starget_port_name = 1,
161 .show_starget_port_id = 1,
162 .set_rport_dev_loss_tmo = fc_set_rport_loss_tmo,
163 .show_rport_dev_loss_tmo = 1,
164 .get_fc_host_stats = fc_get_host_stats,
165 .issue_fc_host_lip = fcoe_reset,
166
167 .terminate_rport_io = fc_rport_terminate_io,
168};
169
Vasu Dev7f349142009-03-27 09:06:31 -0700170static struct scsi_host_template fcoe_shost_template = {
171 .module = THIS_MODULE,
172 .name = "FCoE Driver",
173 .proc_name = FCOE_NAME,
174 .queuecommand = fc_queuecommand,
175 .eh_abort_handler = fc_eh_abort,
176 .eh_device_reset_handler = fc_eh_device_reset,
177 .eh_host_reset_handler = fc_eh_host_reset,
178 .slave_alloc = fc_slave_alloc,
179 .change_queue_depth = fc_change_queue_depth,
180 .change_queue_type = fc_change_queue_type,
181 .this_id = -1,
Vasu Dev14caf442009-10-15 17:46:55 -0700182 .cmd_per_lun = 3,
Vasu Dev7f349142009-03-27 09:06:31 -0700183 .can_queue = FCOE_MAX_OUTSTANDING_COMMANDS,
184 .use_clustering = ENABLE_CLUSTERING,
185 .sg_tablesize = SG_ALL,
186 .max_sectors = 0xffff,
187};
188
Chris Leech54b649f2009-08-25 14:00:07 -0700189static int fcoe_fip_recv(struct sk_buff *skb, struct net_device *dev,
190 struct packet_type *ptype,
191 struct net_device *orig_dev);
192/**
193 * fcoe_interface_setup()
194 * @fcoe: new fcoe_interface
195 * @netdev : ptr to the associated netdevice struct
196 *
197 * Returns : 0 for success
Chris Leech2e70e242009-08-25 14:00:23 -0700198 * Locking: must be called with the RTNL mutex held
Chris Leech54b649f2009-08-25 14:00:07 -0700199 */
200static int fcoe_interface_setup(struct fcoe_interface *fcoe,
201 struct net_device *netdev)
202{
203 struct fcoe_ctlr *fip = &fcoe->ctlr;
204 struct netdev_hw_addr *ha;
205 u8 flogi_maddr[ETH_ALEN];
Yi Zoub7a727f2009-10-21 16:28:03 -0700206 const struct net_device_ops *ops;
Chris Leech54b649f2009-08-25 14:00:07 -0700207
208 fcoe->netdev = netdev;
209
Yi Zoub7a727f2009-10-21 16:28:03 -0700210 /* Let LLD initialize for FCoE */
211 ops = netdev->netdev_ops;
212 if (ops->ndo_fcoe_enable) {
213 if (ops->ndo_fcoe_enable(netdev))
214 FCOE_NETDEV_DBG(netdev, "Failed to enable FCoE"
215 " specific feature for LLD.\n");
216 }
217
Chris Leech54b649f2009-08-25 14:00:07 -0700218 /* Do not support for bonding device */
219 if ((netdev->priv_flags & IFF_MASTER_ALB) ||
220 (netdev->priv_flags & IFF_SLAVE_INACTIVE) ||
221 (netdev->priv_flags & IFF_MASTER_8023AD)) {
222 return -EOPNOTSUPP;
223 }
224
225 /* look for SAN MAC address, if multiple SAN MACs exist, only
226 * use the first one for SPMA */
227 rcu_read_lock();
228 for_each_dev_addr(netdev, ha) {
229 if ((ha->type == NETDEV_HW_ADDR_T_SAN) &&
230 (is_valid_ether_addr(fip->ctl_src_addr))) {
231 memcpy(fip->ctl_src_addr, ha->addr, ETH_ALEN);
232 fip->spma = 1;
233 break;
234 }
235 }
236 rcu_read_unlock();
237
238 /* setup Source Mac Address */
239 if (!fip->spma)
240 memcpy(fip->ctl_src_addr, netdev->dev_addr, netdev->addr_len);
241
242 /*
243 * Add FCoE MAC address as second unicast MAC address
244 * or enter promiscuous mode if not capable of listening
245 * for multiple unicast MACs.
246 */
Chris Leech54b649f2009-08-25 14:00:07 -0700247 memcpy(flogi_maddr, (u8[6]) FC_FCOE_FLOGI_MAC, ETH_ALEN);
248 dev_unicast_add(netdev, flogi_maddr);
249 if (fip->spma)
250 dev_unicast_add(netdev, fip->ctl_src_addr);
251 dev_mc_add(netdev, FIP_ALL_ENODE_MACS, ETH_ALEN, 0);
Chris Leech54b649f2009-08-25 14:00:07 -0700252
253 /*
254 * setup the receive function from ethernet driver
255 * on the ethertype for the given device
256 */
257 fcoe->fcoe_packet_type.func = fcoe_rcv;
258 fcoe->fcoe_packet_type.type = __constant_htons(ETH_P_FCOE);
259 fcoe->fcoe_packet_type.dev = netdev;
260 dev_add_pack(&fcoe->fcoe_packet_type);
261
262 fcoe->fip_packet_type.func = fcoe_fip_recv;
263 fcoe->fip_packet_type.type = htons(ETH_P_FIP);
264 fcoe->fip_packet_type.dev = netdev;
265 dev_add_pack(&fcoe->fip_packet_type);
266
267 return 0;
268}
269
270static void fcoe_fip_send(struct fcoe_ctlr *fip, struct sk_buff *skb);
Chris Leech11b56182009-11-03 11:46:29 -0800271static void fcoe_update_src_mac(struct fc_lport *lport, u8 *addr);
272static u8 *fcoe_get_src_mac(struct fc_lport *lport);
Chris Leech2e70e242009-08-25 14:00:23 -0700273static void fcoe_destroy_work(struct work_struct *work);
Chris Leech54b649f2009-08-25 14:00:07 -0700274
Vasu Dev7f349142009-03-27 09:06:31 -0700275/**
Chris Leech030f4e02009-08-25 14:00:02 -0700276 * fcoe_interface_create()
277 * @netdev: network interface
278 *
279 * Returns: pointer to a struct fcoe_interface or NULL on error
280 */
281static struct fcoe_interface *fcoe_interface_create(struct net_device *netdev)
282{
283 struct fcoe_interface *fcoe;
284
285 fcoe = kzalloc(sizeof(*fcoe), GFP_KERNEL);
286 if (!fcoe) {
287 FCOE_NETDEV_DBG(netdev, "Could not allocate fcoe structure\n");
288 return NULL;
289 }
290
Chris Leech2e70e242009-08-25 14:00:23 -0700291 dev_hold(netdev);
Chris Leech030f4e02009-08-25 14:00:02 -0700292 kref_init(&fcoe->kref);
Chris Leech54b649f2009-08-25 14:00:07 -0700293
294 /*
295 * Initialize FIP.
296 */
297 fcoe_ctlr_init(&fcoe->ctlr);
298 fcoe->ctlr.send = fcoe_fip_send;
299 fcoe->ctlr.update_mac = fcoe_update_src_mac;
Chris Leech11b56182009-11-03 11:46:29 -0800300 fcoe->ctlr.get_src_addr = fcoe_get_src_mac;
Chris Leech54b649f2009-08-25 14:00:07 -0700301
302 fcoe_interface_setup(fcoe, netdev);
Chris Leech030f4e02009-08-25 14:00:02 -0700303
304 return fcoe;
305}
306
307/**
Chris Leech54b649f2009-08-25 14:00:07 -0700308 * fcoe_interface_cleanup() - clean up netdev configurations
309 * @fcoe:
Chris Leech2e70e242009-08-25 14:00:23 -0700310 *
311 * Caller must be holding the RTNL mutex
Chris Leech54b649f2009-08-25 14:00:07 -0700312 */
313void fcoe_interface_cleanup(struct fcoe_interface *fcoe)
314{
315 struct net_device *netdev = fcoe->netdev;
316 struct fcoe_ctlr *fip = &fcoe->ctlr;
317 u8 flogi_maddr[ETH_ALEN];
Yi Zoub7a727f2009-10-21 16:28:03 -0700318 const struct net_device_ops *ops;
Chris Leech54b649f2009-08-25 14:00:07 -0700319
320 /*
321 * Don't listen for Ethernet packets anymore.
322 * synchronize_net() ensures that the packet handlers are not running
323 * on another CPU. dev_remove_pack() would do that, this calls the
324 * unsyncronized version __dev_remove_pack() to avoid multiple delays.
325 */
326 __dev_remove_pack(&fcoe->fcoe_packet_type);
327 __dev_remove_pack(&fcoe->fip_packet_type);
328 synchronize_net();
329
Chris Leech54b649f2009-08-25 14:00:07 -0700330 /* Delete secondary MAC addresses */
Chris Leech54b649f2009-08-25 14:00:07 -0700331 memcpy(flogi_maddr, (u8[6]) FC_FCOE_FLOGI_MAC, ETH_ALEN);
332 dev_unicast_delete(netdev, flogi_maddr);
Chris Leech54b649f2009-08-25 14:00:07 -0700333 if (fip->spma)
334 dev_unicast_delete(netdev, fip->ctl_src_addr);
335 dev_mc_delete(netdev, FIP_ALL_ENODE_MACS, ETH_ALEN, 0);
Yi Zoub7a727f2009-10-21 16:28:03 -0700336
337 /* Tell the LLD we are done w/ FCoE */
338 ops = netdev->netdev_ops;
339 if (ops->ndo_fcoe_disable) {
340 if (ops->ndo_fcoe_disable(netdev))
341 FCOE_NETDEV_DBG(netdev, "Failed to disable FCoE"
342 " specific feature for LLD.\n");
343 }
Chris Leech54b649f2009-08-25 14:00:07 -0700344}
345
346/**
Chris Leech030f4e02009-08-25 14:00:02 -0700347 * fcoe_interface_release() - fcoe_port kref release function
348 * @kref: embedded reference count in an fcoe_interface struct
349 */
350static void fcoe_interface_release(struct kref *kref)
351{
352 struct fcoe_interface *fcoe;
Chris Leech2e70e242009-08-25 14:00:23 -0700353 struct net_device *netdev;
Chris Leech030f4e02009-08-25 14:00:02 -0700354
355 fcoe = container_of(kref, struct fcoe_interface, kref);
Chris Leech2e70e242009-08-25 14:00:23 -0700356 netdev = fcoe->netdev;
357 /* tear-down the FCoE controller */
358 fcoe_ctlr_destroy(&fcoe->ctlr);
Chris Leech030f4e02009-08-25 14:00:02 -0700359 kfree(fcoe);
Chris Leech2e70e242009-08-25 14:00:23 -0700360 dev_put(netdev);
Chris Leech030f4e02009-08-25 14:00:02 -0700361}
362
363/**
364 * fcoe_interface_get()
365 * @fcoe:
366 */
367static inline void fcoe_interface_get(struct fcoe_interface *fcoe)
368{
369 kref_get(&fcoe->kref);
370}
371
372/**
373 * fcoe_interface_put()
374 * @fcoe:
375 */
376static inline void fcoe_interface_put(struct fcoe_interface *fcoe)
377{
378 kref_put(&fcoe->kref, fcoe_interface_release);
379}
380
381/**
Vasu Devab6b85c2009-05-17 12:33:08 +0000382 * fcoe_fip_recv - handle a received FIP frame.
383 * @skb: the receive skb
384 * @dev: associated &net_device
385 * @ptype: the &packet_type structure which was used to register this handler.
386 * @orig_dev: original receive &net_device, in case @dev is a bond.
387 *
388 * Returns: 0 for success
389 */
390static int fcoe_fip_recv(struct sk_buff *skb, struct net_device *dev,
391 struct packet_type *ptype,
392 struct net_device *orig_dev)
393{
Chris Leech259ad852009-08-25 13:59:41 -0700394 struct fcoe_interface *fcoe;
Vasu Devab6b85c2009-05-17 12:33:08 +0000395
Chris Leech259ad852009-08-25 13:59:41 -0700396 fcoe = container_of(ptype, struct fcoe_interface, fip_packet_type);
Chris Leech3fe9a0b2009-08-25 13:59:46 -0700397 fcoe_ctlr_recv(&fcoe->ctlr, skb);
Vasu Devab6b85c2009-05-17 12:33:08 +0000398 return 0;
399}
400
401/**
402 * fcoe_fip_send() - send an Ethernet-encapsulated FIP frame.
403 * @fip: FCoE controller.
404 * @skb: FIP Packet.
405 */
406static void fcoe_fip_send(struct fcoe_ctlr *fip, struct sk_buff *skb)
407{
Chris Leech3fe9a0b2009-08-25 13:59:46 -0700408 skb->dev = fcoe_from_ctlr(fip)->netdev;
Vasu Devab6b85c2009-05-17 12:33:08 +0000409 dev_queue_xmit(skb);
410}
411
412/**
413 * fcoe_update_src_mac() - Update Ethernet MAC filters.
Chris Leech11b56182009-11-03 11:46:29 -0800414 * @lport: libfc lport
415 * @addr: Unicast MAC address to add.
Vasu Devab6b85c2009-05-17 12:33:08 +0000416 *
417 * Remove any previously-set unicast MAC filter.
418 * Add secondary FCoE MAC address filter for our OUI.
419 */
Chris Leech11b56182009-11-03 11:46:29 -0800420static void fcoe_update_src_mac(struct fc_lport *lport, u8 *addr)
Vasu Devab6b85c2009-05-17 12:33:08 +0000421{
Chris Leech11b56182009-11-03 11:46:29 -0800422 struct fcoe_port *port = lport_priv(lport);
423 struct fcoe_interface *fcoe = port->fcoe;
Vasu Devab6b85c2009-05-17 12:33:08 +0000424
Vasu Devab6b85c2009-05-17 12:33:08 +0000425 rtnl_lock();
Chris Leech11b56182009-11-03 11:46:29 -0800426 if (!is_zero_ether_addr(port->data_src_addr))
427 dev_unicast_delete(fcoe->netdev, port->data_src_addr);
428 if (!is_zero_ether_addr(addr))
429 dev_unicast_add(fcoe->netdev, addr);
430 memcpy(port->data_src_addr, addr, ETH_ALEN);
Vasu Devab6b85c2009-05-17 12:33:08 +0000431 rtnl_unlock();
432}
433
434/**
Chris Leech11b56182009-11-03 11:46:29 -0800435 * fcoe_get_src_mac() - return the Ethernet source address for an lport
436 * @lport: libfc lport
437 */
438static u8 *fcoe_get_src_mac(struct fc_lport *lport)
439{
440 struct fcoe_port *port = lport_priv(lport);
441
442 return port->data_src_addr;
443}
444
445/**
Vasu Dev7f349142009-03-27 09:06:31 -0700446 * fcoe_lport_config() - sets up the fc_lport
447 * @lp: ptr to the fc_lport
Vasu Dev7f349142009-03-27 09:06:31 -0700448 *
449 * Returns: 0 for success
450 */
451static int fcoe_lport_config(struct fc_lport *lp)
452{
453 lp->link_up = 0;
454 lp->qfull = 0;
455 lp->max_retry_count = 3;
Abhijeet Joglekara3666952009-05-01 10:01:26 -0700456 lp->max_rport_retry_count = 3;
Vasu Dev7f349142009-03-27 09:06:31 -0700457 lp->e_d_tov = 2 * 1000; /* FC-FS default */
458 lp->r_a_tov = 2 * 2 * 1000;
459 lp->service_params = (FCP_SPPF_INIT_FCN | FCP_SPPF_RD_XRDY_DIS |
460 FCP_SPPF_RETRY | FCP_SPPF_CONF_COMPL);
Chris Leech9a057532009-11-03 11:46:40 -0800461 lp->does_npiv = 1;
Vasu Dev7f349142009-03-27 09:06:31 -0700462
463 fc_lport_init_stats(lp);
464
465 /* lport fc_lport related configuration */
466 fc_lport_config(lp);
467
468 /* offload related configuration */
469 lp->crc_offload = 0;
470 lp->seq_offload = 0;
471 lp->lro_enabled = 0;
472 lp->lro_xid = 0;
473 lp->lso_max = 0;
474
475 return 0;
476}
477
478/**
Vasu Dev1047f222009-05-06 10:52:40 -0700479 * fcoe_queue_timer() - fcoe queue timer
480 * @lp: the fc_lport pointer
481 *
482 * Calls fcoe_check_wait_queue on timeout
483 *
484 */
485static void fcoe_queue_timer(ulong lp)
486{
487 fcoe_check_wait_queue((struct fc_lport *)lp, NULL);
488}
489
490/**
Vasu Dev7f349142009-03-27 09:06:31 -0700491 * fcoe_netdev_config() - Set up netdev for SW FCoE
492 * @lp : ptr to the fc_lport
493 * @netdev : ptr to the associated netdevice struct
494 *
495 * Must be called after fcoe_lport_config() as it will use lport mutex
496 *
497 * Returns : 0 for success
498 */
499static int fcoe_netdev_config(struct fc_lport *lp, struct net_device *netdev)
500{
501 u32 mfs;
502 u64 wwnn, wwpn;
Chris Leech25024982009-08-25 13:59:35 -0700503 struct fcoe_interface *fcoe;
Chris Leech014f5c32009-08-25 13:59:30 -0700504 struct fcoe_port *port;
Vasu Dev7f349142009-03-27 09:06:31 -0700505
506 /* Setup lport private data to point to fcoe softc */
Chris Leech014f5c32009-08-25 13:59:30 -0700507 port = lport_priv(lp);
Chris Leech25024982009-08-25 13:59:35 -0700508 fcoe = port->fcoe;
Vasu Dev7f349142009-03-27 09:06:31 -0700509
510 /*
511 * Determine max frame size based on underlying device and optional
512 * user-configured limit. If the MFS is too low, fcoe_link_ok()
513 * will return 0, so do this first.
514 */
Yi Zou7221d7e2009-10-21 16:27:52 -0700515 mfs = netdev->mtu;
516 if (netdev->features & NETIF_F_FCOE_MTU) {
517 mfs = FCOE_MTU;
518 FCOE_NETDEV_DBG(netdev, "Supports FCOE_MTU of %d bytes\n", mfs);
519 }
520 mfs -= (sizeof(struct fcoe_hdr) + sizeof(struct fcoe_crc_eof));
Vasu Dev7f349142009-03-27 09:06:31 -0700521 if (fc_set_mfs(lp, mfs))
522 return -EINVAL;
523
Vasu Dev7f349142009-03-27 09:06:31 -0700524 /* offload features support */
Vasu Dev1d1b88d2009-07-29 17:05:45 -0700525 if (netdev->features & NETIF_F_SG)
Vasu Dev7f349142009-03-27 09:06:31 -0700526 lp->sg_supp = 1;
527
Vasu Dev7f349142009-03-27 09:06:31 -0700528 if (netdev->features & NETIF_F_FCOE_CRC) {
529 lp->crc_offload = 1;
Robert Loved5488eb2009-06-10 15:30:59 -0700530 FCOE_NETDEV_DBG(netdev, "Supports FCCRC offload\n");
Vasu Dev7f349142009-03-27 09:06:31 -0700531 }
Vasu Dev7f349142009-03-27 09:06:31 -0700532 if (netdev->features & NETIF_F_FSO) {
533 lp->seq_offload = 1;
534 lp->lso_max = netdev->gso_max_size;
Robert Loved5488eb2009-06-10 15:30:59 -0700535 FCOE_NETDEV_DBG(netdev, "Supports LSO for max len 0x%x\n",
536 lp->lso_max);
Vasu Dev7f349142009-03-27 09:06:31 -0700537 }
Vasu Dev7f349142009-03-27 09:06:31 -0700538 if (netdev->fcoe_ddp_xid) {
539 lp->lro_enabled = 1;
540 lp->lro_xid = netdev->fcoe_ddp_xid;
Robert Loved5488eb2009-06-10 15:30:59 -0700541 FCOE_NETDEV_DBG(netdev, "Supports LRO for max xid 0x%x\n",
542 lp->lro_xid);
Vasu Dev7f349142009-03-27 09:06:31 -0700543 }
Chris Leech014f5c32009-08-25 13:59:30 -0700544 skb_queue_head_init(&port->fcoe_pending_queue);
545 port->fcoe_pending_queue_active = 0;
546 setup_timer(&port->timer, fcoe_queue_timer, (unsigned long)lp);
Vasu Dev7f349142009-03-27 09:06:31 -0700547
Chris Leech9a057532009-11-03 11:46:40 -0800548 if (!lp->vport) {
549 wwnn = fcoe_wwn_from_mac(netdev->dev_addr, 1, 0);
550 fc_set_wwnn(lp, wwnn);
551 /* XXX - 3rd arg needs to be vlan id */
552 wwpn = fcoe_wwn_from_mac(netdev->dev_addr, 2, 0);
553 fc_set_wwpn(lp, wwpn);
554 }
Vasu Dev7f349142009-03-27 09:06:31 -0700555
Vasu Dev7f349142009-03-27 09:06:31 -0700556 return 0;
557}
558
559/**
560 * fcoe_shost_config() - Sets up fc_lport->host
561 * @lp : ptr to the fc_lport
562 * @shost : ptr to the associated scsi host
563 * @dev : device associated to scsi host
564 *
565 * Must be called after fcoe_lport_config() and fcoe_netdev_config()
566 *
567 * Returns : 0 for success
568 */
569static int fcoe_shost_config(struct fc_lport *lp, struct Scsi_Host *shost,
570 struct device *dev)
571{
572 int rc = 0;
573
574 /* lport scsi host config */
Vasu Dev7f349142009-03-27 09:06:31 -0700575 lp->host->max_lun = FCOE_MAX_LUN;
576 lp->host->max_id = FCOE_MAX_FCP_TARGET;
577 lp->host->max_channel = 0;
Chris Leeche9084bb2009-11-03 11:46:34 -0800578 if (lp->vport)
579 lp->host->transportt = fcoe_vport_transport_template;
580 else
581 lp->host->transportt = fcoe_transport_template;
Vasu Dev7f349142009-03-27 09:06:31 -0700582
583 /* add the new host to the SCSI-ml */
584 rc = scsi_add_host(lp->host, dev);
585 if (rc) {
Robert Loved5488eb2009-06-10 15:30:59 -0700586 FCOE_NETDEV_DBG(fcoe_netdev(lp), "fcoe_shost_config: "
587 "error on scsi_add_host\n");
Vasu Dev7f349142009-03-27 09:06:31 -0700588 return rc;
589 }
Chris Leech9a057532009-11-03 11:46:40 -0800590
591 if (!lp->vport)
592 fc_host_max_npiv_vports(lp->host) = USHORT_MAX;
593
Chris Leech5baa17c2009-11-03 11:46:56 -0800594 snprintf(fc_host_symbolic_name(lp->host), FC_SYMBOLIC_NAME_SIZE,
595 "%s v%s over %s", FCOE_NAME, FCOE_VERSION,
596 fcoe_netdev(lp)->name);
Vasu Dev7f349142009-03-27 09:06:31 -0700597
598 return 0;
599}
600
Vasu Devd7179682009-07-29 17:05:21 -0700601/*
602 * fcoe_oem_match() - match for read types IO
603 * @fp: the fc_frame for new IO.
604 *
605 * Returns : true for read types IO, otherwise returns false.
606 */
607bool fcoe_oem_match(struct fc_frame *fp)
608{
Yi Zou05cc7392009-08-25 13:59:03 -0700609 return fc_fcp_is_read(fr_fsp(fp)) &&
610 (fr_fsp(fp)->data_len > fcoe_ddp_min);
Vasu Devd7179682009-07-29 17:05:21 -0700611}
612
Vasu Dev7f349142009-03-27 09:06:31 -0700613/**
614 * fcoe_em_config() - allocates em for this lport
Chris Leech014f5c32009-08-25 13:59:30 -0700615 * @lp: the fcoe that em is to allocated for
Vasu Dev7f349142009-03-27 09:06:31 -0700616 *
617 * Returns : 0 on success
618 */
619static inline int fcoe_em_config(struct fc_lport *lp)
620{
Chris Leech014f5c32009-08-25 13:59:30 -0700621 struct fcoe_port *port = lport_priv(lp);
Chris Leech25024982009-08-25 13:59:35 -0700622 struct fcoe_interface *fcoe = port->fcoe;
623 struct fcoe_interface *oldfcoe = NULL;
Vasu Dev1d1b88d2009-07-29 17:05:45 -0700624 struct net_device *old_real_dev, *cur_real_dev;
Vasu Devd7179682009-07-29 17:05:21 -0700625 u16 min_xid = FCOE_MIN_XID;
626 u16 max_xid = FCOE_MAX_XID;
627
628 /*
629 * Check if need to allocate an em instance for
630 * offload exchange ids to be shared across all VN_PORTs/lport.
631 */
632 if (!lp->lro_enabled || !lp->lro_xid || (lp->lro_xid >= max_xid)) {
633 lp->lro_xid = 0;
634 goto skip_oem;
635 }
636
637 /*
638 * Reuse existing offload em instance in case
Vasu Dev1d1b88d2009-07-29 17:05:45 -0700639 * it is already allocated on real eth device
Vasu Devd7179682009-07-29 17:05:21 -0700640 */
Chris Leech25024982009-08-25 13:59:35 -0700641 if (fcoe->netdev->priv_flags & IFF_802_1Q_VLAN)
642 cur_real_dev = vlan_dev_real_dev(fcoe->netdev);
Vasu Dev1d1b88d2009-07-29 17:05:45 -0700643 else
Chris Leech25024982009-08-25 13:59:35 -0700644 cur_real_dev = fcoe->netdev;
Vasu Dev1d1b88d2009-07-29 17:05:45 -0700645
Chris Leech25024982009-08-25 13:59:35 -0700646 list_for_each_entry(oldfcoe, &fcoe_hostlist, list) {
Chris Leech25024982009-08-25 13:59:35 -0700647 if (oldfcoe->netdev->priv_flags & IFF_802_1Q_VLAN)
648 old_real_dev = vlan_dev_real_dev(oldfcoe->netdev);
Vasu Dev1d1b88d2009-07-29 17:05:45 -0700649 else
Chris Leech25024982009-08-25 13:59:35 -0700650 old_real_dev = oldfcoe->netdev;
Vasu Dev1d1b88d2009-07-29 17:05:45 -0700651
652 if (cur_real_dev == old_real_dev) {
Chris Leech991cbb62009-08-25 13:59:51 -0700653 fcoe->oem = oldfcoe->oem;
Vasu Devd7179682009-07-29 17:05:21 -0700654 break;
655 }
656 }
657
Chris Leech991cbb62009-08-25 13:59:51 -0700658 if (fcoe->oem) {
659 if (!fc_exch_mgr_add(lp, fcoe->oem, fcoe_oem_match)) {
Vasu Devd7179682009-07-29 17:05:21 -0700660 printk(KERN_ERR "fcoe_em_config: failed to add "
661 "offload em:%p on interface:%s\n",
Chris Leech991cbb62009-08-25 13:59:51 -0700662 fcoe->oem, fcoe->netdev->name);
Vasu Devd7179682009-07-29 17:05:21 -0700663 return -ENOMEM;
664 }
665 } else {
Chris Leech991cbb62009-08-25 13:59:51 -0700666 fcoe->oem = fc_exch_mgr_alloc(lp, FC_CLASS_3,
Vasu Devd7179682009-07-29 17:05:21 -0700667 FCOE_MIN_XID, lp->lro_xid,
668 fcoe_oem_match);
Chris Leech991cbb62009-08-25 13:59:51 -0700669 if (!fcoe->oem) {
Vasu Devd7179682009-07-29 17:05:21 -0700670 printk(KERN_ERR "fcoe_em_config: failed to allocate "
671 "em for offload exches on interface:%s\n",
Chris Leech25024982009-08-25 13:59:35 -0700672 fcoe->netdev->name);
Vasu Devd7179682009-07-29 17:05:21 -0700673 return -ENOMEM;
674 }
675 }
676
677 /*
678 * Exclude offload EM xid range from next EM xid range.
679 */
680 min_xid += lp->lro_xid + 1;
681
682skip_oem:
683 if (!fc_exch_mgr_alloc(lp, FC_CLASS_3, min_xid, max_xid, NULL)) {
684 printk(KERN_ERR "fcoe_em_config: failed to "
Chris Leech25024982009-08-25 13:59:35 -0700685 "allocate em on interface %s\n", fcoe->netdev->name);
Vasu Dev7f349142009-03-27 09:06:31 -0700686 return -ENOMEM;
Vasu Devd7179682009-07-29 17:05:21 -0700687 }
Vasu Dev7f349142009-03-27 09:06:31 -0700688
689 return 0;
690}
691
692/**
693 * fcoe_if_destroy() - FCoE software HBA tear-down function
Chris Leechaf7f85d2009-08-25 13:59:24 -0700694 * @lport: fc_lport to destroy
Vasu Dev7f349142009-03-27 09:06:31 -0700695 */
Chris Leechaf7f85d2009-08-25 13:59:24 -0700696static void fcoe_if_destroy(struct fc_lport *lport)
Vasu Dev7f349142009-03-27 09:06:31 -0700697{
Chris Leech014f5c32009-08-25 13:59:30 -0700698 struct fcoe_port *port = lport_priv(lport);
699 struct fcoe_interface *fcoe = port->fcoe;
Chris Leech25024982009-08-25 13:59:35 -0700700 struct net_device *netdev = fcoe->netdev;
Vasu Dev7f349142009-03-27 09:06:31 -0700701
Robert Loved5488eb2009-06-10 15:30:59 -0700702 FCOE_NETDEV_DBG(netdev, "Destroying interface\n");
Vasu Dev7f349142009-03-27 09:06:31 -0700703
Vasu Dev7f349142009-03-27 09:06:31 -0700704 /* Logout of the fabric */
Chris Leechaf7f85d2009-08-25 13:59:24 -0700705 fc_fabric_logoff(lport);
Vasu Dev7f349142009-03-27 09:06:31 -0700706
Vasu Dev7f349142009-03-27 09:06:31 -0700707 /* Cleanup the fc_lport */
Chris Leechaf7f85d2009-08-25 13:59:24 -0700708 fc_lport_destroy(lport);
709 fc_fcp_destroy(lport);
Vasu Dev7f349142009-03-27 09:06:31 -0700710
Chris Leech54b649f2009-08-25 14:00:07 -0700711 /* Stop the transmit retry timer */
712 del_timer_sync(&port->timer);
713
714 /* Free existing transmit skbs */
715 fcoe_clean_pending_queue(lport);
716
Chris Leech11b56182009-11-03 11:46:29 -0800717 rtnl_lock();
718 if (!is_zero_ether_addr(port->data_src_addr))
719 dev_unicast_delete(netdev, port->data_src_addr);
720 rtnl_unlock();
721
Chris Leech54b649f2009-08-25 14:00:07 -0700722 /* receives may not be stopped until after this */
723 fcoe_interface_put(fcoe);
724
725 /* Free queued packets for the per-CPU receive threads */
726 fcoe_percpu_clean(lport);
727
Vasu Dev7f349142009-03-27 09:06:31 -0700728 /* Detach from the scsi-ml */
Chris Leechaf7f85d2009-08-25 13:59:24 -0700729 fc_remove_host(lport->host);
730 scsi_remove_host(lport->host);
Vasu Dev7f349142009-03-27 09:06:31 -0700731
732 /* There are no more rports or I/O, free the EM */
Chris Leechaf7f85d2009-08-25 13:59:24 -0700733 fc_exch_mgr_free(lport);
Vasu Dev7f349142009-03-27 09:06:31 -0700734
Vasu Dev7f349142009-03-27 09:06:31 -0700735 /* Free memory used by statistical counters */
Chris Leechaf7f85d2009-08-25 13:59:24 -0700736 fc_lport_free_stats(lport);
Vasu Dev7f349142009-03-27 09:06:31 -0700737
Chris Leech2e70e242009-08-25 14:00:23 -0700738 /* Release the Scsi_Host */
Chris Leechaf7f85d2009-08-25 13:59:24 -0700739 scsi_host_put(lport->host);
Vasu Dev7f349142009-03-27 09:06:31 -0700740}
741
742/*
743 * fcoe_ddp_setup - calls LLD's ddp_setup through net_device
744 * @lp: the corresponding fc_lport
745 * @xid: the exchange id for this ddp transfer
746 * @sgl: the scatterlist describing this transfer
747 * @sgc: number of sg items
748 *
749 * Returns : 0 no ddp
750 */
751static int fcoe_ddp_setup(struct fc_lport *lp, u16 xid,
752 struct scatterlist *sgl, unsigned int sgc)
753{
754 struct net_device *n = fcoe_netdev(lp);
755
Yi Zoub04d0232009-10-21 16:26:55 -0700756 if (n->netdev_ops->ndo_fcoe_ddp_setup)
Vasu Dev7f349142009-03-27 09:06:31 -0700757 return n->netdev_ops->ndo_fcoe_ddp_setup(n, xid, sgl, sgc);
758
759 return 0;
760}
761
762/*
763 * fcoe_ddp_done - calls LLD's ddp_done through net_device
764 * @lp: the corresponding fc_lport
765 * @xid: the exchange id for this ddp transfer
766 *
767 * Returns : the length of data that have been completed by ddp
768 */
769static int fcoe_ddp_done(struct fc_lport *lp, u16 xid)
770{
771 struct net_device *n = fcoe_netdev(lp);
772
Yi Zoub04d0232009-10-21 16:26:55 -0700773 if (n->netdev_ops->ndo_fcoe_ddp_done)
Vasu Dev7f349142009-03-27 09:06:31 -0700774 return n->netdev_ops->ndo_fcoe_ddp_done(n, xid);
775 return 0;
776}
777
Chris Leech11b56182009-11-03 11:46:29 -0800778static struct fc_seq *fcoe_elsct_send(struct fc_lport *lport,
779 u32 did, struct fc_frame *fp, unsigned int op,
780 void (*resp)(struct fc_seq *, struct fc_frame *, void *),
781 void *arg, u32 timeout);
782
Vasu Dev7f349142009-03-27 09:06:31 -0700783static struct libfc_function_template fcoe_libfc_fcn_templ = {
784 .frame_send = fcoe_xmit,
785 .ddp_setup = fcoe_ddp_setup,
786 .ddp_done = fcoe_ddp_done,
Chris Leech11b56182009-11-03 11:46:29 -0800787 .elsct_send = fcoe_elsct_send,
Vasu Dev7f349142009-03-27 09:06:31 -0700788};
789
790/**
Chris Leech030f4e02009-08-25 14:00:02 -0700791 * fcoe_if_create() - this function creates the fcoe port
792 * @fcoe: fcoe_interface structure to create an fc_lport instance on
Chris Leechaf7f85d2009-08-25 13:59:24 -0700793 * @parent: device pointer to be the parent in sysfs for the SCSI host
Chris Leech9a057532009-11-03 11:46:40 -0800794 * @npiv: is this a vport?
Vasu Dev7f349142009-03-27 09:06:31 -0700795 *
Chris Leech54b649f2009-08-25 14:00:07 -0700796 * Creates fc_lport struct and scsi_host for lport, configures lport.
Vasu Dev7f349142009-03-27 09:06:31 -0700797 *
Chris Leechaf7f85d2009-08-25 13:59:24 -0700798 * Returns : The allocated fc_lport or an error pointer
Vasu Dev7f349142009-03-27 09:06:31 -0700799 */
Chris Leech030f4e02009-08-25 14:00:02 -0700800static struct fc_lport *fcoe_if_create(struct fcoe_interface *fcoe,
Chris Leech9a057532009-11-03 11:46:40 -0800801 struct device *parent, int npiv)
Vasu Dev7f349142009-03-27 09:06:31 -0700802{
803 int rc;
Chris Leechaf7f85d2009-08-25 13:59:24 -0700804 struct fc_lport *lport = NULL;
Chris Leech014f5c32009-08-25 13:59:30 -0700805 struct fcoe_port *port;
Vasu Dev7f349142009-03-27 09:06:31 -0700806 struct Scsi_Host *shost;
Chris Leech030f4e02009-08-25 14:00:02 -0700807 struct net_device *netdev = fcoe->netdev;
Chris Leech9a057532009-11-03 11:46:40 -0800808 /*
809 * parent is only a vport if npiv is 1,
810 * but we'll only use vport in that case so go ahead and set it
811 */
812 struct fc_vport *vport = dev_to_vport(parent);
Vasu Dev7f349142009-03-27 09:06:31 -0700813
Robert Loved5488eb2009-06-10 15:30:59 -0700814 FCOE_NETDEV_DBG(netdev, "Create Interface\n");
Vasu Dev7f349142009-03-27 09:06:31 -0700815
Chris Leech9a057532009-11-03 11:46:40 -0800816 if (!npiv) {
817 lport = libfc_host_alloc(&fcoe_shost_template,
818 sizeof(struct fcoe_port));
819 } else {
820 lport = libfc_vport_create(vport,
821 sizeof(struct fcoe_port));
822 }
Chris Leech86221962009-11-03 11:46:08 -0800823 if (!lport) {
Chris Leech014f5c32009-08-25 13:59:30 -0700824 FCOE_NETDEV_DBG(netdev, "Could not allocate host structure\n");
825 rc = -ENOMEM;
Chris Leech030f4e02009-08-25 14:00:02 -0700826 goto out;
Chris Leech014f5c32009-08-25 13:59:30 -0700827 }
Chris Leech86221962009-11-03 11:46:08 -0800828 shost = lport->host;
Chris Leech014f5c32009-08-25 13:59:30 -0700829 port = lport_priv(lport);
Chris Leech2e70e242009-08-25 14:00:23 -0700830 port->lport = lport;
Chris Leech014f5c32009-08-25 13:59:30 -0700831 port->fcoe = fcoe;
Chris Leech2e70e242009-08-25 14:00:23 -0700832 INIT_WORK(&port->destroy_work, fcoe_destroy_work);
Vasu Dev7f349142009-03-27 09:06:31 -0700833
834 /* configure fc_lport, e.g., em */
Chris Leechaf7f85d2009-08-25 13:59:24 -0700835 rc = fcoe_lport_config(lport);
Vasu Dev7f349142009-03-27 09:06:31 -0700836 if (rc) {
Robert Loved5488eb2009-06-10 15:30:59 -0700837 FCOE_NETDEV_DBG(netdev, "Could not configure lport for the "
838 "interface\n");
Vasu Dev7f349142009-03-27 09:06:31 -0700839 goto out_host_put;
840 }
841
Chris Leech9a057532009-11-03 11:46:40 -0800842 if (npiv) {
843 FCOE_NETDEV_DBG(netdev, "Setting vport names, 0x%llX 0x%llX\n",
844 vport->node_name, vport->port_name);
845 fc_set_wwnn(lport, vport->node_name);
846 fc_set_wwpn(lport, vport->port_name);
847 }
848
Vasu Devab6b85c2009-05-17 12:33:08 +0000849 /* configure lport network properties */
Chris Leechaf7f85d2009-08-25 13:59:24 -0700850 rc = fcoe_netdev_config(lport, netdev);
Vasu Devab6b85c2009-05-17 12:33:08 +0000851 if (rc) {
Robert Loved5488eb2009-06-10 15:30:59 -0700852 FCOE_NETDEV_DBG(netdev, "Could not configure netdev for the "
853 "interface\n");
Chris Leech54b649f2009-08-25 14:00:07 -0700854 goto out_lp_destroy;
Vasu Devab6b85c2009-05-17 12:33:08 +0000855 }
Joe Eykholt97c83892009-03-17 11:42:40 -0700856
Vasu Dev7f349142009-03-27 09:06:31 -0700857 /* configure lport scsi host properties */
Chris Leechaf7f85d2009-08-25 13:59:24 -0700858 rc = fcoe_shost_config(lport, shost, parent);
Vasu Dev7f349142009-03-27 09:06:31 -0700859 if (rc) {
Robert Loved5488eb2009-06-10 15:30:59 -0700860 FCOE_NETDEV_DBG(netdev, "Could not configure shost for the "
861 "interface\n");
Chris Leech54b649f2009-08-25 14:00:07 -0700862 goto out_lp_destroy;
Vasu Dev7f349142009-03-27 09:06:31 -0700863 }
864
Vasu Dev7f349142009-03-27 09:06:31 -0700865 /* Initialize the library */
Chris Leechaf7f85d2009-08-25 13:59:24 -0700866 rc = fcoe_libfc_config(lport, &fcoe_libfc_fcn_templ);
Vasu Dev7f349142009-03-27 09:06:31 -0700867 if (rc) {
Robert Loved5488eb2009-06-10 15:30:59 -0700868 FCOE_NETDEV_DBG(netdev, "Could not configure libfc for the "
869 "interface\n");
Vasu Dev7f349142009-03-27 09:06:31 -0700870 goto out_lp_destroy;
871 }
872
Chris Leech9a057532009-11-03 11:46:40 -0800873 if (!npiv) {
874 /*
875 * fcoe_em_alloc() and fcoe_hostlist_add() both
876 * need to be atomic with respect to other changes to the
877 * hostlist since fcoe_em_alloc() looks for an existing EM
878 * instance on host list updated by fcoe_hostlist_add().
879 *
880 * This is currently handled through the fcoe_config_mutex
881 * begin held.
882 */
Chris Leechc863df32009-08-25 14:00:18 -0700883
Chris Leech9a057532009-11-03 11:46:40 -0800884 /* lport exch manager allocation */
885 rc = fcoe_em_config(lport);
886 if (rc) {
887 FCOE_NETDEV_DBG(netdev, "Could not configure the EM "
888 "for the interface\n");
889 goto out_lp_destroy;
890 }
Vasu Dev96316092009-07-29 17:05:00 -0700891 }
892
Chris Leech030f4e02009-08-25 14:00:02 -0700893 fcoe_interface_get(fcoe);
Chris Leechaf7f85d2009-08-25 13:59:24 -0700894 return lport;
Vasu Dev7f349142009-03-27 09:06:31 -0700895
896out_lp_destroy:
Chris Leechaf7f85d2009-08-25 13:59:24 -0700897 fc_exch_mgr_free(lport);
Vasu Dev7f349142009-03-27 09:06:31 -0700898out_host_put:
Chris Leechaf7f85d2009-08-25 13:59:24 -0700899 scsi_host_put(lport->host);
900out:
901 return ERR_PTR(rc);
Vasu Dev7f349142009-03-27 09:06:31 -0700902}
903
904/**
905 * fcoe_if_init() - attach to scsi transport
906 *
907 * Returns : 0 on success
908 */
909static int __init fcoe_if_init(void)
910{
911 /* attach to scsi transport */
Chris Leeche9084bb2009-11-03 11:46:34 -0800912 fcoe_transport_template = fc_attach_transport(&fcoe_transport_function);
913 fcoe_vport_transport_template =
914 fc_attach_transport(&fcoe_vport_transport_function);
Vasu Dev7f349142009-03-27 09:06:31 -0700915
Chris Leeche9084bb2009-11-03 11:46:34 -0800916 if (!fcoe_transport_template) {
Robert Loved5488eb2009-06-10 15:30:59 -0700917 printk(KERN_ERR "fcoe: Failed to attach to the FC transport\n");
Vasu Dev7f349142009-03-27 09:06:31 -0700918 return -ENODEV;
919 }
920
921 return 0;
922}
923
924/**
925 * fcoe_if_exit() - detach from scsi transport
926 *
927 * Returns : 0 on success
928 */
929int __exit fcoe_if_exit(void)
930{
Chris Leeche9084bb2009-11-03 11:46:34 -0800931 fc_release_transport(fcoe_transport_template);
932 fc_release_transport(fcoe_vport_transport_template);
933 fcoe_transport_template = NULL;
934 fcoe_vport_transport_template = NULL;
Vasu Dev7f349142009-03-27 09:06:31 -0700935 return 0;
936}
937
Robert Love85b4aa42008-12-09 15:10:24 -0800938/**
Robert Love8976f422009-03-17 11:41:46 -0700939 * fcoe_percpu_thread_create() - Create a receive thread for an online cpu
940 * @cpu: cpu index for the online cpu
941 */
942static void fcoe_percpu_thread_create(unsigned int cpu)
943{
944 struct fcoe_percpu_s *p;
945 struct task_struct *thread;
946
947 p = &per_cpu(fcoe_percpu, cpu);
948
949 thread = kthread_create(fcoe_percpu_receive_thread,
950 (void *)p, "fcoethread/%d", cpu);
951
Joe Eykholte7a51992009-08-25 14:04:08 -0700952 if (likely(!IS_ERR(thread))) {
Robert Love8976f422009-03-17 11:41:46 -0700953 kthread_bind(thread, cpu);
954 wake_up_process(thread);
955
956 spin_lock_bh(&p->fcoe_rx_list.lock);
957 p->thread = thread;
958 spin_unlock_bh(&p->fcoe_rx_list.lock);
959 }
960}
961
962/**
963 * fcoe_percpu_thread_destroy() - removes the rx thread for the given cpu
964 * @cpu: cpu index the rx thread is to be removed
965 *
966 * Destroys a per-CPU Rx thread. Any pending skbs are moved to the
967 * current CPU's Rx thread. If the thread being destroyed is bound to
968 * the CPU processing this context the skbs will be freed.
969 */
970static void fcoe_percpu_thread_destroy(unsigned int cpu)
971{
972 struct fcoe_percpu_s *p;
973 struct task_struct *thread;
974 struct page *crc_eof;
975 struct sk_buff *skb;
976#ifdef CONFIG_SMP
977 struct fcoe_percpu_s *p0;
978 unsigned targ_cpu = smp_processor_id();
979#endif /* CONFIG_SMP */
980
Robert Loved5488eb2009-06-10 15:30:59 -0700981 FCOE_DBG("Destroying receive thread for CPU %d\n", cpu);
Robert Love8976f422009-03-17 11:41:46 -0700982
983 /* Prevent any new skbs from being queued for this CPU. */
984 p = &per_cpu(fcoe_percpu, cpu);
985 spin_lock_bh(&p->fcoe_rx_list.lock);
986 thread = p->thread;
987 p->thread = NULL;
988 crc_eof = p->crc_eof_page;
989 p->crc_eof_page = NULL;
990 p->crc_eof_offset = 0;
991 spin_unlock_bh(&p->fcoe_rx_list.lock);
992
993#ifdef CONFIG_SMP
994 /*
995 * Don't bother moving the skb's if this context is running
996 * on the same CPU that is having its thread destroyed. This
997 * can easily happen when the module is removed.
998 */
999 if (cpu != targ_cpu) {
1000 p0 = &per_cpu(fcoe_percpu, targ_cpu);
1001 spin_lock_bh(&p0->fcoe_rx_list.lock);
1002 if (p0->thread) {
Robert Loved5488eb2009-06-10 15:30:59 -07001003 FCOE_DBG("Moving frames from CPU %d to CPU %d\n",
1004 cpu, targ_cpu);
Robert Love8976f422009-03-17 11:41:46 -07001005
1006 while ((skb = __skb_dequeue(&p->fcoe_rx_list)) != NULL)
1007 __skb_queue_tail(&p0->fcoe_rx_list, skb);
1008 spin_unlock_bh(&p0->fcoe_rx_list.lock);
1009 } else {
1010 /*
1011 * The targeted CPU is not initialized and cannot accept
1012 * new skbs. Unlock the targeted CPU and drop the skbs
1013 * on the CPU that is going offline.
1014 */
1015 while ((skb = __skb_dequeue(&p->fcoe_rx_list)) != NULL)
1016 kfree_skb(skb);
1017 spin_unlock_bh(&p0->fcoe_rx_list.lock);
1018 }
1019 } else {
1020 /*
1021 * This scenario occurs when the module is being removed
1022 * and all threads are being destroyed. skbs will continue
1023 * to be shifted from the CPU thread that is being removed
1024 * to the CPU thread associated with the CPU that is processing
1025 * the module removal. Once there is only one CPU Rx thread it
1026 * will reach this case and we will drop all skbs and later
1027 * stop the thread.
1028 */
1029 spin_lock_bh(&p->fcoe_rx_list.lock);
1030 while ((skb = __skb_dequeue(&p->fcoe_rx_list)) != NULL)
1031 kfree_skb(skb);
1032 spin_unlock_bh(&p->fcoe_rx_list.lock);
1033 }
1034#else
1035 /*
Chris Leechdd3fd722009-04-21 16:27:36 -07001036 * This a non-SMP scenario where the singular Rx thread is
Robert Love8976f422009-03-17 11:41:46 -07001037 * being removed. Free all skbs and stop the thread.
1038 */
1039 spin_lock_bh(&p->fcoe_rx_list.lock);
1040 while ((skb = __skb_dequeue(&p->fcoe_rx_list)) != NULL)
1041 kfree_skb(skb);
1042 spin_unlock_bh(&p->fcoe_rx_list.lock);
1043#endif
1044
1045 if (thread)
1046 kthread_stop(thread);
1047
1048 if (crc_eof)
1049 put_page(crc_eof);
1050}
1051
1052/**
1053 * fcoe_cpu_callback() - fcoe cpu hotplug event callback
1054 * @nfb: callback data block
1055 * @action: event triggering the callback
1056 * @hcpu: index for the cpu of this event
1057 *
1058 * This creates or destroys per cpu data for fcoe
1059 *
1060 * Returns NOTIFY_OK always.
1061 */
1062static int fcoe_cpu_callback(struct notifier_block *nfb,
1063 unsigned long action, void *hcpu)
1064{
1065 unsigned cpu = (unsigned long)hcpu;
1066
1067 switch (action) {
1068 case CPU_ONLINE:
1069 case CPU_ONLINE_FROZEN:
Robert Loved5488eb2009-06-10 15:30:59 -07001070 FCOE_DBG("CPU %x online: Create Rx thread\n", cpu);
Robert Love8976f422009-03-17 11:41:46 -07001071 fcoe_percpu_thread_create(cpu);
1072 break;
1073 case CPU_DEAD:
1074 case CPU_DEAD_FROZEN:
Robert Loved5488eb2009-06-10 15:30:59 -07001075 FCOE_DBG("CPU %x offline: Remove Rx thread\n", cpu);
Robert Love8976f422009-03-17 11:41:46 -07001076 fcoe_percpu_thread_destroy(cpu);
1077 break;
1078 default:
1079 break;
1080 }
1081 return NOTIFY_OK;
1082}
1083
1084static struct notifier_block fcoe_cpu_notifier = {
1085 .notifier_call = fcoe_cpu_callback,
1086};
1087
1088/**
Robert Love34f42a02009-02-27 10:55:45 -08001089 * fcoe_rcv() - this is the fcoe receive function called by NET_RX_SOFTIRQ
Robert Love85b4aa42008-12-09 15:10:24 -08001090 * @skb: the receive skb
1091 * @dev: associated net device
1092 * @ptype: context
Chris Leechdd3fd722009-04-21 16:27:36 -07001093 * @olddev: last device
Robert Love85b4aa42008-12-09 15:10:24 -08001094 *
1095 * this function will receive the packet and build fc frame and pass it up
1096 *
1097 * Returns: 0 for success
Robert Love34f42a02009-02-27 10:55:45 -08001098 */
Robert Love85b4aa42008-12-09 15:10:24 -08001099int fcoe_rcv(struct sk_buff *skb, struct net_device *dev,
1100 struct packet_type *ptype, struct net_device *olddev)
1101{
1102 struct fc_lport *lp;
1103 struct fcoe_rcv_info *fr;
Chris Leech259ad852009-08-25 13:59:41 -07001104 struct fcoe_interface *fcoe;
Robert Love85b4aa42008-12-09 15:10:24 -08001105 struct fc_frame_header *fh;
Robert Love85b4aa42008-12-09 15:10:24 -08001106 struct fcoe_percpu_s *fps;
Vasu Devb2f00912009-08-25 13:58:53 -07001107 unsigned int cpu;
Robert Love85b4aa42008-12-09 15:10:24 -08001108
Chris Leech259ad852009-08-25 13:59:41 -07001109 fcoe = container_of(ptype, struct fcoe_interface, fcoe_packet_type);
Chris Leech3fe9a0b2009-08-25 13:59:46 -07001110 lp = fcoe->ctlr.lp;
Robert Love85b4aa42008-12-09 15:10:24 -08001111 if (unlikely(lp == NULL)) {
Robert Loved5488eb2009-06-10 15:30:59 -07001112 FCOE_NETDEV_DBG(dev, "Cannot find hba structure");
Robert Love85b4aa42008-12-09 15:10:24 -08001113 goto err2;
1114 }
Joe Eykholt97c83892009-03-17 11:42:40 -07001115 if (!lp->link_up)
1116 goto err2;
Robert Love85b4aa42008-12-09 15:10:24 -08001117
Robert Loved5488eb2009-06-10 15:30:59 -07001118 FCOE_NETDEV_DBG(dev, "skb_info: len:%d data_len:%d head:%p "
1119 "data:%p tail:%p end:%p sum:%d dev:%s",
1120 skb->len, skb->data_len, skb->head, skb->data,
1121 skb_tail_pointer(skb), skb_end_pointer(skb),
1122 skb->csum, skb->dev ? skb->dev->name : "<NULL>");
Robert Love85b4aa42008-12-09 15:10:24 -08001123
1124 /* check for FCOE packet type */
1125 if (unlikely(eth_hdr(skb)->h_proto != htons(ETH_P_FCOE))) {
Robert Loved5488eb2009-06-10 15:30:59 -07001126 FCOE_NETDEV_DBG(dev, "Wrong FC type frame");
Robert Love85b4aa42008-12-09 15:10:24 -08001127 goto err;
1128 }
1129
1130 /*
1131 * Check for minimum frame length, and make sure required FCoE
1132 * and FC headers are pulled into the linear data area.
1133 */
1134 if (unlikely((skb->len < FCOE_MIN_FRAME) ||
1135 !pskb_may_pull(skb, FCOE_HEADER_LEN)))
1136 goto err;
1137
1138 skb_set_transport_header(skb, sizeof(struct fcoe_hdr));
1139 fh = (struct fc_frame_header *) skb_transport_header(skb);
1140
Robert Love85b4aa42008-12-09 15:10:24 -08001141 fr = fcoe_dev_from_skb(skb);
1142 fr->fr_dev = lp;
1143 fr->ptype = ptype;
Robert Love5e5e92d2009-03-17 11:41:35 -07001144
Robert Love85b4aa42008-12-09 15:10:24 -08001145 /*
Vasu Devb2f00912009-08-25 13:58:53 -07001146 * In case the incoming frame's exchange is originated from
1147 * the initiator, then received frame's exchange id is ANDed
1148 * with fc_cpu_mask bits to get the same cpu on which exchange
1149 * was originated, otherwise just use the current cpu.
Robert Love85b4aa42008-12-09 15:10:24 -08001150 */
Vasu Devb2f00912009-08-25 13:58:53 -07001151 if (ntoh24(fh->fh_f_ctl) & FC_FC_EX_CTX)
1152 cpu = ntohs(fh->fh_ox_id) & fc_cpu_mask;
1153 else
1154 cpu = smp_processor_id();
Robert Love5e5e92d2009-03-17 11:41:35 -07001155
Robert Love8976f422009-03-17 11:41:46 -07001156 fps = &per_cpu(fcoe_percpu, cpu);
Robert Love85b4aa42008-12-09 15:10:24 -08001157 spin_lock_bh(&fps->fcoe_rx_list.lock);
Robert Love8976f422009-03-17 11:41:46 -07001158 if (unlikely(!fps->thread)) {
1159 /*
1160 * The targeted CPU is not ready, let's target
1161 * the first CPU now. For non-SMP systems this
1162 * will check the same CPU twice.
1163 */
Robert Loved5488eb2009-06-10 15:30:59 -07001164 FCOE_NETDEV_DBG(dev, "CPU is online, but no receive thread "
1165 "ready for incoming skb- using first online "
1166 "CPU.\n");
Robert Love8976f422009-03-17 11:41:46 -07001167
1168 spin_unlock_bh(&fps->fcoe_rx_list.lock);
1169 cpu = first_cpu(cpu_online_map);
1170 fps = &per_cpu(fcoe_percpu, cpu);
1171 spin_lock_bh(&fps->fcoe_rx_list.lock);
1172 if (!fps->thread) {
1173 spin_unlock_bh(&fps->fcoe_rx_list.lock);
1174 goto err;
1175 }
1176 }
1177
1178 /*
1179 * We now have a valid CPU that we're targeting for
1180 * this skb. We also have this receive thread locked,
1181 * so we're free to queue skbs into it's queue.
1182 */
Robert Love85b4aa42008-12-09 15:10:24 -08001183 __skb_queue_tail(&fps->fcoe_rx_list, skb);
1184 if (fps->fcoe_rx_list.qlen == 1)
1185 wake_up_process(fps->thread);
1186
1187 spin_unlock_bh(&fps->fcoe_rx_list.lock);
1188
1189 return 0;
1190err:
Robert Love582b45b2009-03-31 15:51:50 -07001191 fc_lport_get_stats(lp)->ErrorFrames++;
Robert Love85b4aa42008-12-09 15:10:24 -08001192
1193err2:
1194 kfree_skb(skb);
1195 return -1;
1196}
Robert Love85b4aa42008-12-09 15:10:24 -08001197
1198/**
Robert Love34f42a02009-02-27 10:55:45 -08001199 * fcoe_start_io() - pass to netdev to start xmit for fcoe
Robert Love85b4aa42008-12-09 15:10:24 -08001200 * @skb: the skb to be xmitted
1201 *
1202 * Returns: 0 for success
Robert Love34f42a02009-02-27 10:55:45 -08001203 */
Robert Love85b4aa42008-12-09 15:10:24 -08001204static inline int fcoe_start_io(struct sk_buff *skb)
1205{
1206 int rc;
1207
1208 skb_get(skb);
1209 rc = dev_queue_xmit(skb);
1210 if (rc != 0)
1211 return rc;
1212 kfree_skb(skb);
1213 return 0;
1214}
1215
1216/**
Chris Leechdd3fd722009-04-21 16:27:36 -07001217 * fcoe_get_paged_crc_eof() - in case we need to alloc a page for crc_eof
Robert Love85b4aa42008-12-09 15:10:24 -08001218 * @skb: the skb to be xmitted
1219 * @tlen: total len
1220 *
1221 * Returns: 0 for success
Robert Love34f42a02009-02-27 10:55:45 -08001222 */
Robert Love85b4aa42008-12-09 15:10:24 -08001223static int fcoe_get_paged_crc_eof(struct sk_buff *skb, int tlen)
1224{
1225 struct fcoe_percpu_s *fps;
1226 struct page *page;
Robert Love85b4aa42008-12-09 15:10:24 -08001227
Robert Love5e5e92d2009-03-17 11:41:35 -07001228 fps = &get_cpu_var(fcoe_percpu);
Robert Love85b4aa42008-12-09 15:10:24 -08001229 page = fps->crc_eof_page;
1230 if (!page) {
1231 page = alloc_page(GFP_ATOMIC);
1232 if (!page) {
Robert Love5e5e92d2009-03-17 11:41:35 -07001233 put_cpu_var(fcoe_percpu);
Robert Love85b4aa42008-12-09 15:10:24 -08001234 return -ENOMEM;
1235 }
1236 fps->crc_eof_page = page;
Robert Love8976f422009-03-17 11:41:46 -07001237 fps->crc_eof_offset = 0;
Robert Love85b4aa42008-12-09 15:10:24 -08001238 }
1239
1240 get_page(page);
1241 skb_fill_page_desc(skb, skb_shinfo(skb)->nr_frags, page,
1242 fps->crc_eof_offset, tlen);
1243 skb->len += tlen;
1244 skb->data_len += tlen;
1245 skb->truesize += tlen;
1246 fps->crc_eof_offset += sizeof(struct fcoe_crc_eof);
1247
1248 if (fps->crc_eof_offset >= PAGE_SIZE) {
1249 fps->crc_eof_page = NULL;
1250 fps->crc_eof_offset = 0;
1251 put_page(page);
1252 }
Robert Love5e5e92d2009-03-17 11:41:35 -07001253 put_cpu_var(fcoe_percpu);
Robert Love85b4aa42008-12-09 15:10:24 -08001254 return 0;
1255}
1256
1257/**
Robert Love34f42a02009-02-27 10:55:45 -08001258 * fcoe_fc_crc() - calculates FC CRC in this fcoe skb
Chris Leechdd3fd722009-04-21 16:27:36 -07001259 * @fp: the fc_frame containing data to be checksummed
Robert Love85b4aa42008-12-09 15:10:24 -08001260 *
Chris Leech014f5c32009-08-25 13:59:30 -07001261 * This uses crc32() to calculate the crc for port frame
Robert Love85b4aa42008-12-09 15:10:24 -08001262 * Return : 32 bit crc
Robert Love34f42a02009-02-27 10:55:45 -08001263 */
Robert Love85b4aa42008-12-09 15:10:24 -08001264u32 fcoe_fc_crc(struct fc_frame *fp)
1265{
1266 struct sk_buff *skb = fp_skb(fp);
1267 struct skb_frag_struct *frag;
1268 unsigned char *data;
1269 unsigned long off, len, clen;
1270 u32 crc;
1271 unsigned i;
1272
1273 crc = crc32(~0, skb->data, skb_headlen(skb));
1274
1275 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
1276 frag = &skb_shinfo(skb)->frags[i];
1277 off = frag->page_offset;
1278 len = frag->size;
1279 while (len > 0) {
1280 clen = min(len, PAGE_SIZE - (off & ~PAGE_MASK));
1281 data = kmap_atomic(frag->page + (off >> PAGE_SHIFT),
1282 KM_SKB_DATA_SOFTIRQ);
1283 crc = crc32(crc, data + (off & ~PAGE_MASK), clen);
1284 kunmap_atomic(data, KM_SKB_DATA_SOFTIRQ);
1285 off += clen;
1286 len -= clen;
1287 }
1288 }
1289 return crc;
1290}
Robert Love85b4aa42008-12-09 15:10:24 -08001291
1292/**
Robert Love34f42a02009-02-27 10:55:45 -08001293 * fcoe_xmit() - FCoE frame transmit function
Chris Leech014f5c32009-08-25 13:59:30 -07001294 * @lp: the associated local fcoe
Robert Love85b4aa42008-12-09 15:10:24 -08001295 * @fp: the fc_frame to be transmitted
1296 *
1297 * Return : 0 for success
Robert Love34f42a02009-02-27 10:55:45 -08001298 */
Robert Love85b4aa42008-12-09 15:10:24 -08001299int fcoe_xmit(struct fc_lport *lp, struct fc_frame *fp)
1300{
Vasu Dev4bb6b512009-05-06 10:52:34 -07001301 int wlen;
Robert Love85b4aa42008-12-09 15:10:24 -08001302 u32 crc;
1303 struct ethhdr *eh;
1304 struct fcoe_crc_eof *cp;
1305 struct sk_buff *skb;
1306 struct fcoe_dev_stats *stats;
1307 struct fc_frame_header *fh;
1308 unsigned int hlen; /* header length implies the version */
1309 unsigned int tlen; /* trailer length */
1310 unsigned int elen; /* eth header, may include vlan */
Chris Leech3fe9a0b2009-08-25 13:59:46 -07001311 struct fcoe_port *port = lport_priv(lp);
1312 struct fcoe_interface *fcoe = port->fcoe;
Robert Love85b4aa42008-12-09 15:10:24 -08001313 u8 sof, eof;
1314 struct fcoe_hdr *hp;
1315
1316 WARN_ON((fr_len(fp) % sizeof(u32)) != 0);
1317
Robert Love85b4aa42008-12-09 15:10:24 -08001318 fh = fc_frame_header_get(fp);
Joe Eykholt97c83892009-03-17 11:42:40 -07001319 skb = fp_skb(fp);
1320 wlen = skb->len / FCOE_WORD_TO_BYTE;
1321
1322 if (!lp->link_up) {
Dan Carpenter3caf02e2009-04-21 16:27:25 -07001323 kfree_skb(skb);
Joe Eykholt97c83892009-03-17 11:42:40 -07001324 return 0;
Robert Love85b4aa42008-12-09 15:10:24 -08001325 }
1326
Joe Eykholt97c83892009-03-17 11:42:40 -07001327 if (unlikely(fh->fh_r_ctl == FC_RCTL_ELS_REQ) &&
Chris Leech11b56182009-11-03 11:46:29 -08001328 fcoe_ctlr_els_send(&fcoe->ctlr, lp, skb))
Joe Eykholt97c83892009-03-17 11:42:40 -07001329 return 0;
1330
Robert Love85b4aa42008-12-09 15:10:24 -08001331 sof = fr_sof(fp);
1332 eof = fr_eof(fp);
1333
Vasu Dev4e57e1c2009-05-06 10:52:46 -07001334 elen = sizeof(struct ethhdr);
Robert Love85b4aa42008-12-09 15:10:24 -08001335 hlen = sizeof(struct fcoe_hdr);
1336 tlen = sizeof(struct fcoe_crc_eof);
1337 wlen = (skb->len - tlen + sizeof(crc)) / FCOE_WORD_TO_BYTE;
1338
1339 /* crc offload */
1340 if (likely(lp->crc_offload)) {
Yi Zou39ca9a02009-02-27 14:07:15 -08001341 skb->ip_summed = CHECKSUM_PARTIAL;
Robert Love85b4aa42008-12-09 15:10:24 -08001342 skb->csum_start = skb_headroom(skb);
1343 skb->csum_offset = skb->len;
1344 crc = 0;
1345 } else {
1346 skb->ip_summed = CHECKSUM_NONE;
1347 crc = fcoe_fc_crc(fp);
1348 }
1349
Chris Leech014f5c32009-08-25 13:59:30 -07001350 /* copy port crc and eof to the skb buff */
Robert Love85b4aa42008-12-09 15:10:24 -08001351 if (skb_is_nonlinear(skb)) {
1352 skb_frag_t *frag;
1353 if (fcoe_get_paged_crc_eof(skb, tlen)) {
Roel Kluine9041582009-02-27 10:56:22 -08001354 kfree_skb(skb);
Robert Love85b4aa42008-12-09 15:10:24 -08001355 return -ENOMEM;
1356 }
1357 frag = &skb_shinfo(skb)->frags[skb_shinfo(skb)->nr_frags - 1];
1358 cp = kmap_atomic(frag->page, KM_SKB_DATA_SOFTIRQ)
1359 + frag->page_offset;
1360 } else {
1361 cp = (struct fcoe_crc_eof *)skb_put(skb, tlen);
1362 }
1363
1364 memset(cp, 0, sizeof(*cp));
1365 cp->fcoe_eof = eof;
1366 cp->fcoe_crc32 = cpu_to_le32(~crc);
1367
1368 if (skb_is_nonlinear(skb)) {
1369 kunmap_atomic(cp, KM_SKB_DATA_SOFTIRQ);
1370 cp = NULL;
1371 }
1372
Chris Leech014f5c32009-08-25 13:59:30 -07001373 /* adjust skb network/transport offsets to match mac/fcoe/port */
Robert Love85b4aa42008-12-09 15:10:24 -08001374 skb_push(skb, elen + hlen);
1375 skb_reset_mac_header(skb);
1376 skb_reset_network_header(skb);
1377 skb->mac_len = elen;
Yi Zou211c7382009-02-27 14:06:37 -08001378 skb->protocol = htons(ETH_P_FCOE);
Chris Leech3fe9a0b2009-08-25 13:59:46 -07001379 skb->dev = fcoe->netdev;
Robert Love85b4aa42008-12-09 15:10:24 -08001380
1381 /* fill up mac and fcoe headers */
1382 eh = eth_hdr(skb);
1383 eh->h_proto = htons(ETH_P_FCOE);
Chris Leech3fe9a0b2009-08-25 13:59:46 -07001384 if (fcoe->ctlr.map_dest)
Robert Love85b4aa42008-12-09 15:10:24 -08001385 fc_fcoe_set_mac(eh->h_dest, fh->fh_d_id);
1386 else
1387 /* insert GW address */
Chris Leech3fe9a0b2009-08-25 13:59:46 -07001388 memcpy(eh->h_dest, fcoe->ctlr.dest_addr, ETH_ALEN);
Robert Love85b4aa42008-12-09 15:10:24 -08001389
Chris Leech3fe9a0b2009-08-25 13:59:46 -07001390 if (unlikely(fcoe->ctlr.flogi_oxid != FC_XID_UNKNOWN))
1391 memcpy(eh->h_source, fcoe->ctlr.ctl_src_addr, ETH_ALEN);
Robert Love85b4aa42008-12-09 15:10:24 -08001392 else
Chris Leech11b56182009-11-03 11:46:29 -08001393 memcpy(eh->h_source, port->data_src_addr, ETH_ALEN);
Robert Love85b4aa42008-12-09 15:10:24 -08001394
1395 hp = (struct fcoe_hdr *)(eh + 1);
1396 memset(hp, 0, sizeof(*hp));
1397 if (FC_FCOE_VER)
1398 FC_FCOE_ENCAPS_VER(hp, FC_FCOE_VER);
1399 hp->fcoe_sof = sof;
1400
Yi Zou39ca9a02009-02-27 14:07:15 -08001401 /* fcoe lso, mss is in max_payload which is non-zero for FCP data */
1402 if (lp->seq_offload && fr_max_payload(fp)) {
1403 skb_shinfo(skb)->gso_type = SKB_GSO_FCOE;
1404 skb_shinfo(skb)->gso_size = fr_max_payload(fp);
1405 } else {
1406 skb_shinfo(skb)->gso_type = 0;
1407 skb_shinfo(skb)->gso_size = 0;
1408 }
Robert Love85b4aa42008-12-09 15:10:24 -08001409 /* update tx stats: regardless if LLD fails */
Robert Love582b45b2009-03-31 15:51:50 -07001410 stats = fc_lport_get_stats(lp);
1411 stats->TxFrames++;
1412 stats->TxWords += wlen;
Robert Love85b4aa42008-12-09 15:10:24 -08001413
1414 /* send down to lld */
1415 fr_dev(fp) = lp;
Chris Leech014f5c32009-08-25 13:59:30 -07001416 if (port->fcoe_pending_queue.qlen)
Vasu Dev4bb6b512009-05-06 10:52:34 -07001417 fcoe_check_wait_queue(lp, skb);
1418 else if (fcoe_start_io(skb))
1419 fcoe_check_wait_queue(lp, skb);
Robert Love85b4aa42008-12-09 15:10:24 -08001420
1421 return 0;
1422}
Robert Love85b4aa42008-12-09 15:10:24 -08001423
Robert Love34f42a02009-02-27 10:55:45 -08001424/**
Joe Eykholte7a51992009-08-25 14:04:08 -07001425 * fcoe_percpu_flush_done() - Indicate percpu queue flush completion.
1426 * @skb: the skb being completed.
1427 */
1428static void fcoe_percpu_flush_done(struct sk_buff *skb)
1429{
1430 complete(&fcoe_flush_completion);
1431}
1432
1433/**
Robert Love34f42a02009-02-27 10:55:45 -08001434 * fcoe_percpu_receive_thread() - recv thread per cpu
Robert Love85b4aa42008-12-09 15:10:24 -08001435 * @arg: ptr to the fcoe per cpu struct
1436 *
1437 * Return: 0 for success
Robert Love85b4aa42008-12-09 15:10:24 -08001438 */
1439int fcoe_percpu_receive_thread(void *arg)
1440{
1441 struct fcoe_percpu_s *p = arg;
1442 u32 fr_len;
1443 struct fc_lport *lp;
1444 struct fcoe_rcv_info *fr;
1445 struct fcoe_dev_stats *stats;
1446 struct fc_frame_header *fh;
1447 struct sk_buff *skb;
1448 struct fcoe_crc_eof crc_eof;
1449 struct fc_frame *fp;
1450 u8 *mac = NULL;
Chris Leech014f5c32009-08-25 13:59:30 -07001451 struct fcoe_port *port;
Robert Love85b4aa42008-12-09 15:10:24 -08001452 struct fcoe_hdr *hp;
1453
Robert Love4469c192009-02-27 10:56:38 -08001454 set_user_nice(current, -20);
Robert Love85b4aa42008-12-09 15:10:24 -08001455
1456 while (!kthread_should_stop()) {
1457
1458 spin_lock_bh(&p->fcoe_rx_list.lock);
1459 while ((skb = __skb_dequeue(&p->fcoe_rx_list)) == NULL) {
1460 set_current_state(TASK_INTERRUPTIBLE);
1461 spin_unlock_bh(&p->fcoe_rx_list.lock);
1462 schedule();
1463 set_current_state(TASK_RUNNING);
1464 if (kthread_should_stop())
1465 return 0;
1466 spin_lock_bh(&p->fcoe_rx_list.lock);
1467 }
1468 spin_unlock_bh(&p->fcoe_rx_list.lock);
1469 fr = fcoe_dev_from_skb(skb);
1470 lp = fr->fr_dev;
1471 if (unlikely(lp == NULL)) {
Joe Eykholte7a51992009-08-25 14:04:08 -07001472 if (skb->destructor != fcoe_percpu_flush_done)
1473 FCOE_NETDEV_DBG(skb->dev, "NULL lport in skb");
Robert Love85b4aa42008-12-09 15:10:24 -08001474 kfree_skb(skb);
1475 continue;
1476 }
1477
Robert Loved5488eb2009-06-10 15:30:59 -07001478 FCOE_NETDEV_DBG(skb->dev, "skb_info: len:%d data_len:%d "
1479 "head:%p data:%p tail:%p end:%p sum:%d dev:%s",
1480 skb->len, skb->data_len,
1481 skb->head, skb->data, skb_tail_pointer(skb),
1482 skb_end_pointer(skb), skb->csum,
1483 skb->dev ? skb->dev->name : "<NULL>");
Robert Love85b4aa42008-12-09 15:10:24 -08001484
1485 /*
1486 * Save source MAC address before discarding header.
1487 */
Chris Leech014f5c32009-08-25 13:59:30 -07001488 port = lport_priv(lp);
Robert Love85b4aa42008-12-09 15:10:24 -08001489 if (skb_is_nonlinear(skb))
1490 skb_linearize(skb); /* not ideal */
Joe Eykholt97c83892009-03-17 11:42:40 -07001491 mac = eth_hdr(skb)->h_source;
Robert Love85b4aa42008-12-09 15:10:24 -08001492
1493 /*
1494 * Frame length checks and setting up the header pointers
1495 * was done in fcoe_rcv already.
1496 */
1497 hp = (struct fcoe_hdr *) skb_network_header(skb);
1498 fh = (struct fc_frame_header *) skb_transport_header(skb);
1499
Robert Love582b45b2009-03-31 15:51:50 -07001500 stats = fc_lport_get_stats(lp);
Robert Love85b4aa42008-12-09 15:10:24 -08001501 if (unlikely(FC_FCOE_DECAPS_VER(hp) != FC_FCOE_VER)) {
Robert Love582b45b2009-03-31 15:51:50 -07001502 if (stats->ErrorFrames < 5)
Robert Loved5488eb2009-06-10 15:30:59 -07001503 printk(KERN_WARNING "fcoe: FCoE version "
Robert Love582b45b2009-03-31 15:51:50 -07001504 "mismatch: The frame has "
1505 "version %x, but the "
1506 "initiator supports version "
1507 "%x\n", FC_FCOE_DECAPS_VER(hp),
1508 FC_FCOE_VER);
1509 stats->ErrorFrames++;
Robert Love85b4aa42008-12-09 15:10:24 -08001510 kfree_skb(skb);
1511 continue;
1512 }
1513
1514 skb_pull(skb, sizeof(struct fcoe_hdr));
1515 fr_len = skb->len - sizeof(struct fcoe_crc_eof);
1516
Robert Love582b45b2009-03-31 15:51:50 -07001517 stats->RxFrames++;
1518 stats->RxWords += fr_len / FCOE_WORD_TO_BYTE;
Robert Love85b4aa42008-12-09 15:10:24 -08001519
1520 fp = (struct fc_frame *)skb;
1521 fc_frame_init(fp);
1522 fr_dev(fp) = lp;
1523 fr_sof(fp) = hp->fcoe_sof;
1524
1525 /* Copy out the CRC and EOF trailer for access */
1526 if (skb_copy_bits(skb, fr_len, &crc_eof, sizeof(crc_eof))) {
1527 kfree_skb(skb);
1528 continue;
1529 }
1530 fr_eof(fp) = crc_eof.fcoe_eof;
1531 fr_crc(fp) = crc_eof.fcoe_crc32;
1532 if (pskb_trim(skb, fr_len)) {
1533 kfree_skb(skb);
1534 continue;
1535 }
1536
1537 /*
1538 * We only check CRC if no offload is available and if it is
1539 * it's solicited data, in which case, the FCP layer would
1540 * check it during the copy.
1541 */
Yi Zou07c00ec2009-02-27 14:07:31 -08001542 if (lp->crc_offload && skb->ip_summed == CHECKSUM_UNNECESSARY)
Robert Love85b4aa42008-12-09 15:10:24 -08001543 fr_flags(fp) &= ~FCPHF_CRC_UNCHECKED;
1544 else
1545 fr_flags(fp) |= FCPHF_CRC_UNCHECKED;
1546
1547 fh = fc_frame_header_get(fp);
1548 if (fh->fh_r_ctl == FC_RCTL_DD_SOL_DATA &&
1549 fh->fh_type == FC_TYPE_FCP) {
Vasu Dev52ff8782009-07-29 17:05:10 -07001550 fc_exch_recv(lp, fp);
Robert Love85b4aa42008-12-09 15:10:24 -08001551 continue;
1552 }
1553 if (fr_flags(fp) & FCPHF_CRC_UNCHECKED) {
1554 if (le32_to_cpu(fr_crc(fp)) !=
1555 ~crc32(~0, skb->data, fr_len)) {
Robert Loved5488eb2009-06-10 15:30:59 -07001556 if (stats->InvalidCRCCount < 5)
Robert Love85b4aa42008-12-09 15:10:24 -08001557 printk(KERN_WARNING "fcoe: dropping "
1558 "frame with CRC error\n");
1559 stats->InvalidCRCCount++;
1560 stats->ErrorFrames++;
1561 fc_frame_free(fp);
1562 continue;
1563 }
1564 fr_flags(fp) &= ~FCPHF_CRC_UNCHECKED;
1565 }
Vasu Dev52ff8782009-07-29 17:05:10 -07001566 fc_exch_recv(lp, fp);
Robert Love85b4aa42008-12-09 15:10:24 -08001567 }
1568 return 0;
1569}
1570
1571/**
Chris Leechdd3fd722009-04-21 16:27:36 -07001572 * fcoe_check_wait_queue() - attempt to clear the transmit backlog
1573 * @lp: the fc_lport
Robert Love85b4aa42008-12-09 15:10:24 -08001574 *
1575 * This empties the wait_queue, dequeue the head of the wait_queue queue
1576 * and calls fcoe_start_io() for each packet, if all skb have been
Vasu Devc826a312009-02-27 10:56:27 -08001577 * transmitted, return qlen or -1 if a error occurs, then restore
Chris Leechdd3fd722009-04-21 16:27:36 -07001578 * wait_queue and try again later.
Robert Love85b4aa42008-12-09 15:10:24 -08001579 *
1580 * The wait_queue is used when the skb transmit fails. skb will go
Chris Leechdd3fd722009-04-21 16:27:36 -07001581 * in the wait_queue which will be emptied by the timer function or
Robert Love85b4aa42008-12-09 15:10:24 -08001582 * by the next skb transmit.
Robert Love34f42a02009-02-27 10:55:45 -08001583 */
Vasu Dev4bb6b512009-05-06 10:52:34 -07001584static void fcoe_check_wait_queue(struct fc_lport *lp, struct sk_buff *skb)
Robert Love85b4aa42008-12-09 15:10:24 -08001585{
Chris Leech014f5c32009-08-25 13:59:30 -07001586 struct fcoe_port *port = lport_priv(lp);
Vasu Dev4bb6b512009-05-06 10:52:34 -07001587 int rc;
Robert Love85b4aa42008-12-09 15:10:24 -08001588
Chris Leech014f5c32009-08-25 13:59:30 -07001589 spin_lock_bh(&port->fcoe_pending_queue.lock);
Vasu Dev4bb6b512009-05-06 10:52:34 -07001590
1591 if (skb)
Chris Leech014f5c32009-08-25 13:59:30 -07001592 __skb_queue_tail(&port->fcoe_pending_queue, skb);
Vasu Dev4bb6b512009-05-06 10:52:34 -07001593
Chris Leech014f5c32009-08-25 13:59:30 -07001594 if (port->fcoe_pending_queue_active)
Vasu Devc826a312009-02-27 10:56:27 -08001595 goto out;
Chris Leech014f5c32009-08-25 13:59:30 -07001596 port->fcoe_pending_queue_active = 1;
Chris Leech55c8baf2009-02-27 10:56:32 -08001597
Chris Leech014f5c32009-08-25 13:59:30 -07001598 while (port->fcoe_pending_queue.qlen) {
Chris Leech55c8baf2009-02-27 10:56:32 -08001599 /* keep qlen > 0 until fcoe_start_io succeeds */
Chris Leech014f5c32009-08-25 13:59:30 -07001600 port->fcoe_pending_queue.qlen++;
1601 skb = __skb_dequeue(&port->fcoe_pending_queue);
Chris Leech55c8baf2009-02-27 10:56:32 -08001602
Chris Leech014f5c32009-08-25 13:59:30 -07001603 spin_unlock_bh(&port->fcoe_pending_queue.lock);
Chris Leech55c8baf2009-02-27 10:56:32 -08001604 rc = fcoe_start_io(skb);
Chris Leech014f5c32009-08-25 13:59:30 -07001605 spin_lock_bh(&port->fcoe_pending_queue.lock);
Chris Leech55c8baf2009-02-27 10:56:32 -08001606
1607 if (rc) {
Chris Leech014f5c32009-08-25 13:59:30 -07001608 __skb_queue_head(&port->fcoe_pending_queue, skb);
Chris Leech55c8baf2009-02-27 10:56:32 -08001609 /* undo temporary increment above */
Chris Leech014f5c32009-08-25 13:59:30 -07001610 port->fcoe_pending_queue.qlen--;
Chris Leech55c8baf2009-02-27 10:56:32 -08001611 break;
Robert Love85b4aa42008-12-09 15:10:24 -08001612 }
Chris Leech55c8baf2009-02-27 10:56:32 -08001613 /* undo temporary increment above */
Chris Leech014f5c32009-08-25 13:59:30 -07001614 port->fcoe_pending_queue.qlen--;
Robert Love85b4aa42008-12-09 15:10:24 -08001615 }
Chris Leech55c8baf2009-02-27 10:56:32 -08001616
Chris Leech014f5c32009-08-25 13:59:30 -07001617 if (port->fcoe_pending_queue.qlen < FCOE_LOW_QUEUE_DEPTH)
Chris Leech55c8baf2009-02-27 10:56:32 -08001618 lp->qfull = 0;
Chris Leech014f5c32009-08-25 13:59:30 -07001619 if (port->fcoe_pending_queue.qlen && !timer_pending(&port->timer))
1620 mod_timer(&port->timer, jiffies + 2);
1621 port->fcoe_pending_queue_active = 0;
Vasu Devc826a312009-02-27 10:56:27 -08001622out:
Chris Leech014f5c32009-08-25 13:59:30 -07001623 if (port->fcoe_pending_queue.qlen > FCOE_MAX_QUEUE_DEPTH)
Vasu Dev4bb6b512009-05-06 10:52:34 -07001624 lp->qfull = 1;
Chris Leech014f5c32009-08-25 13:59:30 -07001625 spin_unlock_bh(&port->fcoe_pending_queue.lock);
Vasu Dev4bb6b512009-05-06 10:52:34 -07001626 return;
Robert Love85b4aa42008-12-09 15:10:24 -08001627}
1628
1629/**
Robert Love34f42a02009-02-27 10:55:45 -08001630 * fcoe_dev_setup() - setup link change notification interface
1631 */
Randy Dunlapb0d428a2009-04-27 21:49:31 -07001632static void fcoe_dev_setup(void)
Robert Love85b4aa42008-12-09 15:10:24 -08001633{
Robert Love85b4aa42008-12-09 15:10:24 -08001634 register_netdevice_notifier(&fcoe_notifier);
1635}
1636
1637/**
Randy Dunlapb0d428a2009-04-27 21:49:31 -07001638 * fcoe_dev_cleanup() - cleanup link change notification interface
Robert Love34f42a02009-02-27 10:55:45 -08001639 */
Robert Love85b4aa42008-12-09 15:10:24 -08001640static void fcoe_dev_cleanup(void)
1641{
1642 unregister_netdevice_notifier(&fcoe_notifier);
1643}
1644
1645/**
Robert Love34f42a02009-02-27 10:55:45 -08001646 * fcoe_device_notification() - netdev event notification callback
Robert Love85b4aa42008-12-09 15:10:24 -08001647 * @notifier: context of the notification
1648 * @event: type of event
1649 * @ptr: fixed array for output parsed ifname
1650 *
1651 * This function is called by the ethernet driver in case of link change event
1652 *
1653 * Returns: 0 for success
Robert Love34f42a02009-02-27 10:55:45 -08001654 */
Robert Love85b4aa42008-12-09 15:10:24 -08001655static int fcoe_device_notification(struct notifier_block *notifier,
1656 ulong event, void *ptr)
1657{
1658 struct fc_lport *lp = NULL;
Vasu Dev1d1b88d2009-07-29 17:05:45 -07001659 struct net_device *netdev = ptr;
Chris Leech014f5c32009-08-25 13:59:30 -07001660 struct fcoe_interface *fcoe;
Chris Leech2e70e242009-08-25 14:00:23 -07001661 struct fcoe_port *port;
Robert Love85b4aa42008-12-09 15:10:24 -08001662 struct fcoe_dev_stats *stats;
Joe Eykholt97c83892009-03-17 11:42:40 -07001663 u32 link_possible = 1;
Robert Love85b4aa42008-12-09 15:10:24 -08001664 u32 mfs;
1665 int rc = NOTIFY_OK;
1666
Chris Leech014f5c32009-08-25 13:59:30 -07001667 list_for_each_entry(fcoe, &fcoe_hostlist, list) {
Chris Leech25024982009-08-25 13:59:35 -07001668 if (fcoe->netdev == netdev) {
Chris Leech3fe9a0b2009-08-25 13:59:46 -07001669 lp = fcoe->ctlr.lp;
Robert Love85b4aa42008-12-09 15:10:24 -08001670 break;
1671 }
1672 }
Robert Love85b4aa42008-12-09 15:10:24 -08001673 if (lp == NULL) {
1674 rc = NOTIFY_DONE;
1675 goto out;
1676 }
1677
Robert Love85b4aa42008-12-09 15:10:24 -08001678 switch (event) {
1679 case NETDEV_DOWN:
1680 case NETDEV_GOING_DOWN:
Joe Eykholt97c83892009-03-17 11:42:40 -07001681 link_possible = 0;
Robert Love85b4aa42008-12-09 15:10:24 -08001682 break;
1683 case NETDEV_UP:
1684 case NETDEV_CHANGE:
Robert Love85b4aa42008-12-09 15:10:24 -08001685 break;
1686 case NETDEV_CHANGEMTU:
Yi Zou7221d7e2009-10-21 16:27:52 -07001687 if (netdev->features & NETIF_F_FCOE_MTU)
1688 break;
Vasu Dev1d1b88d2009-07-29 17:05:45 -07001689 mfs = netdev->mtu - (sizeof(struct fcoe_hdr) +
1690 sizeof(struct fcoe_crc_eof));
Robert Love85b4aa42008-12-09 15:10:24 -08001691 if (mfs >= FC_MIN_MAX_FRAME)
1692 fc_set_mfs(lp, mfs);
Robert Love85b4aa42008-12-09 15:10:24 -08001693 break;
1694 case NETDEV_REGISTER:
1695 break;
Chris Leech2e70e242009-08-25 14:00:23 -07001696 case NETDEV_UNREGISTER:
1697 list_del(&fcoe->list);
1698 port = lport_priv(fcoe->ctlr.lp);
1699 fcoe_interface_cleanup(fcoe);
1700 schedule_work(&port->destroy_work);
1701 goto out;
1702 break;
Robert Love85b4aa42008-12-09 15:10:24 -08001703 default:
Vasu Dev1d1b88d2009-07-29 17:05:45 -07001704 FCOE_NETDEV_DBG(netdev, "Unknown event %ld "
Robert Loved5488eb2009-06-10 15:30:59 -07001705 "from netdev netlink\n", event);
Robert Love85b4aa42008-12-09 15:10:24 -08001706 }
Joe Eykholt97c83892009-03-17 11:42:40 -07001707 if (link_possible && !fcoe_link_ok(lp))
Chris Leech3fe9a0b2009-08-25 13:59:46 -07001708 fcoe_ctlr_link_up(&fcoe->ctlr);
1709 else if (fcoe_ctlr_link_down(&fcoe->ctlr)) {
Joe Eykholt97c83892009-03-17 11:42:40 -07001710 stats = fc_lport_get_stats(lp);
1711 stats->LinkFailureCount++;
1712 fcoe_clean_pending_queue(lp);
Robert Love85b4aa42008-12-09 15:10:24 -08001713 }
1714out:
1715 return rc;
1716}
1717
1718/**
Robert Love34f42a02009-02-27 10:55:45 -08001719 * fcoe_if_to_netdev() - parse a name buffer to get netdev
Robert Love85b4aa42008-12-09 15:10:24 -08001720 * @buffer: incoming buffer to be copied
1721 *
Chris Leechdd3fd722009-04-21 16:27:36 -07001722 * Returns: NULL or ptr to net_device
Robert Love34f42a02009-02-27 10:55:45 -08001723 */
Robert Love85b4aa42008-12-09 15:10:24 -08001724static struct net_device *fcoe_if_to_netdev(const char *buffer)
1725{
1726 char *cp;
1727 char ifname[IFNAMSIZ + 2];
1728
1729 if (buffer) {
1730 strlcpy(ifname, buffer, IFNAMSIZ);
1731 cp = ifname + strlen(ifname);
1732 while (--cp >= ifname && *cp == '\n')
1733 *cp = '\0';
1734 return dev_get_by_name(&init_net, ifname);
1735 }
1736 return NULL;
1737}
1738
1739/**
Robert Love34f42a02009-02-27 10:55:45 -08001740 * fcoe_destroy() - handles the destroy from sysfs
Chris Leechdd3fd722009-04-21 16:27:36 -07001741 * @buffer: expected to be an eth if name
Robert Love85b4aa42008-12-09 15:10:24 -08001742 * @kp: associated kernel param
1743 *
1744 * Returns: 0 for success
Robert Love34f42a02009-02-27 10:55:45 -08001745 */
Robert Love85b4aa42008-12-09 15:10:24 -08001746static int fcoe_destroy(const char *buffer, struct kernel_param *kp)
1747{
Chris Leech030f4e02009-08-25 14:00:02 -07001748 struct fcoe_interface *fcoe;
Chris Leech2e70e242009-08-25 14:00:23 -07001749 struct net_device *netdev;
Mike Christie8eca355f2009-10-21 16:27:44 -07001750 int rc = 0;
Robert Love85b4aa42008-12-09 15:10:24 -08001751
Chris Leechdfc1d0f2009-08-25 14:00:13 -07001752 mutex_lock(&fcoe_config_mutex);
1753#ifdef CONFIG_FCOE_MODULE
1754 /*
1755 * Make sure the module has been initialized, and is not about to be
1756 * removed. Module paramter sysfs files are writable before the
1757 * module_init function is called and after module_exit.
1758 */
1759 if (THIS_MODULE->state != MODULE_STATE_LIVE) {
1760 rc = -ENODEV;
1761 goto out_nodev;
1762 }
1763#endif
1764
Robert Love85b4aa42008-12-09 15:10:24 -08001765 netdev = fcoe_if_to_netdev(buffer);
1766 if (!netdev) {
1767 rc = -ENODEV;
1768 goto out_nodev;
1769 }
Chris Leech2e70e242009-08-25 14:00:23 -07001770
Chris Leech090eb6c2009-08-25 14:00:28 -07001771 rtnl_lock();
Chris Leech2e70e242009-08-25 14:00:23 -07001772 fcoe = fcoe_hostlist_lookup_port(netdev);
1773 if (!fcoe) {
Chris Leech090eb6c2009-08-25 14:00:28 -07001774 rtnl_unlock();
Robert Love85b4aa42008-12-09 15:10:24 -08001775 rc = -ENODEV;
1776 goto out_putdev;
1777 }
Chris Leech2e70e242009-08-25 14:00:23 -07001778 list_del(&fcoe->list);
Chris Leech2e70e242009-08-25 14:00:23 -07001779 fcoe_interface_cleanup(fcoe);
1780 rtnl_unlock();
1781 fcoe_if_destroy(fcoe->ctlr.lp);
Robert Love85b4aa42008-12-09 15:10:24 -08001782out_putdev:
1783 dev_put(netdev);
1784out_nodev:
Chris Leechdfc1d0f2009-08-25 14:00:13 -07001785 mutex_unlock(&fcoe_config_mutex);
Robert Love85b4aa42008-12-09 15:10:24 -08001786 return rc;
1787}
1788
Chris Leech2e70e242009-08-25 14:00:23 -07001789static void fcoe_destroy_work(struct work_struct *work)
1790{
1791 struct fcoe_port *port;
1792
1793 port = container_of(work, struct fcoe_port, destroy_work);
1794 mutex_lock(&fcoe_config_mutex);
1795 fcoe_if_destroy(port->lport);
1796 mutex_unlock(&fcoe_config_mutex);
1797}
1798
Robert Love85b4aa42008-12-09 15:10:24 -08001799/**
Robert Love34f42a02009-02-27 10:55:45 -08001800 * fcoe_create() - Handles the create call from sysfs
Chris Leechdd3fd722009-04-21 16:27:36 -07001801 * @buffer: expected to be an eth if name
Robert Love85b4aa42008-12-09 15:10:24 -08001802 * @kp: associated kernel param
1803 *
1804 * Returns: 0 for success
Robert Love34f42a02009-02-27 10:55:45 -08001805 */
Robert Love85b4aa42008-12-09 15:10:24 -08001806static int fcoe_create(const char *buffer, struct kernel_param *kp)
1807{
1808 int rc;
Chris Leech030f4e02009-08-25 14:00:02 -07001809 struct fcoe_interface *fcoe;
Chris Leechaf7f85d2009-08-25 13:59:24 -07001810 struct fc_lport *lport;
Robert Love85b4aa42008-12-09 15:10:24 -08001811 struct net_device *netdev;
1812
Chris Leechdfc1d0f2009-08-25 14:00:13 -07001813 mutex_lock(&fcoe_config_mutex);
1814#ifdef CONFIG_FCOE_MODULE
1815 /*
1816 * Make sure the module has been initialized, and is not about to be
1817 * removed. Module paramter sysfs files are writable before the
1818 * module_init function is called and after module_exit.
1819 */
1820 if (THIS_MODULE->state != MODULE_STATE_LIVE) {
1821 rc = -ENODEV;
1822 goto out_nodev;
1823 }
1824#endif
1825
Chris Leech2e70e242009-08-25 14:00:23 -07001826 rtnl_lock();
Robert Love85b4aa42008-12-09 15:10:24 -08001827 netdev = fcoe_if_to_netdev(buffer);
1828 if (!netdev) {
1829 rc = -ENODEV;
1830 goto out_nodev;
1831 }
Chris Leech2e70e242009-08-25 14:00:23 -07001832
Robert Love85b4aa42008-12-09 15:10:24 -08001833 /* look for existing lport */
1834 if (fcoe_hostlist_lookup(netdev)) {
1835 rc = -EEXIST;
1836 goto out_putdev;
1837 }
Robert Love85b4aa42008-12-09 15:10:24 -08001838
Chris Leech030f4e02009-08-25 14:00:02 -07001839 fcoe = fcoe_interface_create(netdev);
1840 if (!fcoe) {
1841 rc = -ENOMEM;
1842 goto out_putdev;
1843 }
1844
Chris Leech9a057532009-11-03 11:46:40 -08001845 lport = fcoe_if_create(fcoe, &netdev->dev, 0);
Chris Leechaf7f85d2009-08-25 13:59:24 -07001846 if (IS_ERR(lport)) {
Robert Loved5488eb2009-06-10 15:30:59 -07001847 printk(KERN_ERR "fcoe: Failed to create interface (%s)\n",
Robert Love85b4aa42008-12-09 15:10:24 -08001848 netdev->name);
Robert Love85b4aa42008-12-09 15:10:24 -08001849 rc = -EIO;
Chris Leech2e70e242009-08-25 14:00:23 -07001850 fcoe_interface_cleanup(fcoe);
Chris Leech030f4e02009-08-25 14:00:02 -07001851 goto out_free;
Robert Love85b4aa42008-12-09 15:10:24 -08001852 }
Chris Leech030f4e02009-08-25 14:00:02 -07001853
Chris Leech54b649f2009-08-25 14:00:07 -07001854 /* Make this the "master" N_Port */
1855 fcoe->ctlr.lp = lport;
Chris Leech030f4e02009-08-25 14:00:02 -07001856
Chris Leechc863df32009-08-25 14:00:18 -07001857 /* add to lports list */
1858 fcoe_hostlist_add(lport);
1859
Chris Leech54b649f2009-08-25 14:00:07 -07001860 /* start FIP Discovery and FLOGI */
1861 lport->boot_time = jiffies;
1862 fc_fabric_login(lport);
1863 if (!fcoe_link_ok(lport))
1864 fcoe_ctlr_link_up(&fcoe->ctlr);
1865
1866 rc = 0;
Chris Leech030f4e02009-08-25 14:00:02 -07001867out_free:
Chris Leech54b649f2009-08-25 14:00:07 -07001868 /*
1869 * Release from init in fcoe_interface_create(), on success lport
1870 * should be holding a reference taken in fcoe_if_create().
1871 */
Chris Leech030f4e02009-08-25 14:00:02 -07001872 fcoe_interface_put(fcoe);
Robert Love85b4aa42008-12-09 15:10:24 -08001873out_putdev:
1874 dev_put(netdev);
1875out_nodev:
Chris Leech2e70e242009-08-25 14:00:23 -07001876 rtnl_unlock();
Chris Leechdfc1d0f2009-08-25 14:00:13 -07001877 mutex_unlock(&fcoe_config_mutex);
Robert Love85b4aa42008-12-09 15:10:24 -08001878 return rc;
1879}
1880
1881module_param_call(create, fcoe_create, NULL, NULL, S_IWUSR);
1882__MODULE_PARM_TYPE(create, "string");
Chris Leech014f5c32009-08-25 13:59:30 -07001883MODULE_PARM_DESC(create, "Create fcoe fcoe using net device passed in.");
Robert Love85b4aa42008-12-09 15:10:24 -08001884module_param_call(destroy, fcoe_destroy, NULL, NULL, S_IWUSR);
1885__MODULE_PARM_TYPE(destroy, "string");
Chris Leech014f5c32009-08-25 13:59:30 -07001886MODULE_PARM_DESC(destroy, "Destroy fcoe fcoe");
Robert Love85b4aa42008-12-09 15:10:24 -08001887
Robert Love34f42a02009-02-27 10:55:45 -08001888/**
1889 * fcoe_link_ok() - Check if link is ok for the fc_lport
Robert Love85b4aa42008-12-09 15:10:24 -08001890 * @lp: ptr to the fc_lport
1891 *
1892 * Any permanently-disqualifying conditions have been previously checked.
1893 * This also updates the speed setting, which may change with link for 100/1000.
1894 *
1895 * This function should probably be checking for PAUSE support at some point
1896 * in the future. Currently Per-priority-pause is not determinable using
1897 * ethtool, so we shouldn't be restrictive until that problem is resolved.
1898 *
1899 * Returns: 0 if link is OK for use by FCoE.
1900 *
1901 */
1902int fcoe_link_ok(struct fc_lport *lp)
1903{
Chris Leech014f5c32009-08-25 13:59:30 -07001904 struct fcoe_port *port = lport_priv(lp);
Chris Leech25024982009-08-25 13:59:35 -07001905 struct net_device *dev = port->fcoe->netdev;
Robert Love85b4aa42008-12-09 15:10:24 -08001906 struct ethtool_cmd ecmd = { ETHTOOL_GSET };
Robert Love85b4aa42008-12-09 15:10:24 -08001907
Yi Zou2f718d62009-07-29 17:04:01 -07001908 if ((dev->flags & IFF_UP) && netif_carrier_ok(dev) &&
1909 (!dev_ethtool_get_settings(dev, &ecmd))) {
1910 lp->link_supported_speeds &=
1911 ~(FC_PORTSPEED_1GBIT | FC_PORTSPEED_10GBIT);
1912 if (ecmd.supported & (SUPPORTED_1000baseT_Half |
1913 SUPPORTED_1000baseT_Full))
1914 lp->link_supported_speeds |= FC_PORTSPEED_1GBIT;
1915 if (ecmd.supported & SUPPORTED_10000baseT_Full)
1916 lp->link_supported_speeds |=
1917 FC_PORTSPEED_10GBIT;
1918 if (ecmd.speed == SPEED_1000)
1919 lp->link_speed = FC_PORTSPEED_1GBIT;
1920 if (ecmd.speed == SPEED_10000)
1921 lp->link_speed = FC_PORTSPEED_10GBIT;
Robert Love85b4aa42008-12-09 15:10:24 -08001922
Yi Zou2f718d62009-07-29 17:04:01 -07001923 return 0;
1924 }
1925 return -1;
Robert Love85b4aa42008-12-09 15:10:24 -08001926}
Robert Love85b4aa42008-12-09 15:10:24 -08001927
Robert Love34f42a02009-02-27 10:55:45 -08001928/**
1929 * fcoe_percpu_clean() - Clear the pending skbs for an lport
Robert Love85b4aa42008-12-09 15:10:24 -08001930 * @lp: the fc_lport
Joe Eykholte7a51992009-08-25 14:04:08 -07001931 *
1932 * Must be called with fcoe_create_mutex held to single-thread completion.
1933 *
1934 * This flushes the pending skbs by adding a new skb to each queue and
1935 * waiting until they are all freed. This assures us that not only are
1936 * there no packets that will be handled by the lport, but also that any
1937 * threads already handling packet have returned.
Robert Love85b4aa42008-12-09 15:10:24 -08001938 */
1939void fcoe_percpu_clean(struct fc_lport *lp)
1940{
Robert Love85b4aa42008-12-09 15:10:24 -08001941 struct fcoe_percpu_s *pp;
1942 struct fcoe_rcv_info *fr;
1943 struct sk_buff_head *list;
1944 struct sk_buff *skb, *next;
1945 struct sk_buff *head;
Robert Love5e5e92d2009-03-17 11:41:35 -07001946 unsigned int cpu;
Robert Love85b4aa42008-12-09 15:10:24 -08001947
Robert Love5e5e92d2009-03-17 11:41:35 -07001948 for_each_possible_cpu(cpu) {
1949 pp = &per_cpu(fcoe_percpu, cpu);
1950 spin_lock_bh(&pp->fcoe_rx_list.lock);
1951 list = &pp->fcoe_rx_list;
1952 head = list->next;
1953 for (skb = head; skb != (struct sk_buff *)list;
1954 skb = next) {
1955 next = skb->next;
1956 fr = fcoe_dev_from_skb(skb);
1957 if (fr->fr_dev == lp) {
1958 __skb_unlink(skb, list);
1959 kfree_skb(skb);
Robert Love85b4aa42008-12-09 15:10:24 -08001960 }
Robert Love85b4aa42008-12-09 15:10:24 -08001961 }
Joe Eykholte7a51992009-08-25 14:04:08 -07001962
1963 if (!pp->thread || !cpu_online(cpu)) {
1964 spin_unlock_bh(&pp->fcoe_rx_list.lock);
1965 continue;
1966 }
1967
1968 skb = dev_alloc_skb(0);
1969 if (!skb) {
1970 spin_unlock_bh(&pp->fcoe_rx_list.lock);
1971 continue;
1972 }
1973 skb->destructor = fcoe_percpu_flush_done;
1974
1975 __skb_queue_tail(&pp->fcoe_rx_list, skb);
1976 if (pp->fcoe_rx_list.qlen == 1)
1977 wake_up_process(pp->thread);
Robert Love5e5e92d2009-03-17 11:41:35 -07001978 spin_unlock_bh(&pp->fcoe_rx_list.lock);
Joe Eykholte7a51992009-08-25 14:04:08 -07001979
1980 wait_for_completion(&fcoe_flush_completion);
Robert Love85b4aa42008-12-09 15:10:24 -08001981 }
1982}
Robert Love85b4aa42008-12-09 15:10:24 -08001983
1984/**
Robert Love34f42a02009-02-27 10:55:45 -08001985 * fcoe_clean_pending_queue() - Dequeue a skb and free it
Robert Love85b4aa42008-12-09 15:10:24 -08001986 * @lp: the corresponding fc_lport
1987 *
1988 * Returns: none
Robert Love34f42a02009-02-27 10:55:45 -08001989 */
Robert Love85b4aa42008-12-09 15:10:24 -08001990void fcoe_clean_pending_queue(struct fc_lport *lp)
1991{
Chris Leech014f5c32009-08-25 13:59:30 -07001992 struct fcoe_port *port = lport_priv(lp);
Robert Love85b4aa42008-12-09 15:10:24 -08001993 struct sk_buff *skb;
1994
Chris Leech014f5c32009-08-25 13:59:30 -07001995 spin_lock_bh(&port->fcoe_pending_queue.lock);
1996 while ((skb = __skb_dequeue(&port->fcoe_pending_queue)) != NULL) {
1997 spin_unlock_bh(&port->fcoe_pending_queue.lock);
Robert Love85b4aa42008-12-09 15:10:24 -08001998 kfree_skb(skb);
Chris Leech014f5c32009-08-25 13:59:30 -07001999 spin_lock_bh(&port->fcoe_pending_queue.lock);
Robert Love85b4aa42008-12-09 15:10:24 -08002000 }
Chris Leech014f5c32009-08-25 13:59:30 -07002001 spin_unlock_bh(&port->fcoe_pending_queue.lock);
Robert Love85b4aa42008-12-09 15:10:24 -08002002}
Robert Love85b4aa42008-12-09 15:10:24 -08002003
2004/**
Robert Love34f42a02009-02-27 10:55:45 -08002005 * fcoe_reset() - Resets the fcoe
Robert Love85b4aa42008-12-09 15:10:24 -08002006 * @shost: shost the reset is from
2007 *
2008 * Returns: always 0
2009 */
2010int fcoe_reset(struct Scsi_Host *shost)
2011{
2012 struct fc_lport *lport = shost_priv(shost);
2013 fc_lport_reset(lport);
2014 return 0;
2015}
Robert Love85b4aa42008-12-09 15:10:24 -08002016
Robert Love34f42a02009-02-27 10:55:45 -08002017/**
Chris Leech014f5c32009-08-25 13:59:30 -07002018 * fcoe_hostlist_lookup_port() - find the corresponding lport by a given device
Chris Leechdd3fd722009-04-21 16:27:36 -07002019 * @dev: this is currently ptr to net_device
Robert Love85b4aa42008-12-09 15:10:24 -08002020 *
Chris Leech014f5c32009-08-25 13:59:30 -07002021 * Returns: NULL or the located fcoe_port
Chris Leech090eb6c2009-08-25 14:00:28 -07002022 * Locking: must be called with the RNL mutex held
Robert Love85b4aa42008-12-09 15:10:24 -08002023 */
Chris Leech014f5c32009-08-25 13:59:30 -07002024static struct fcoe_interface *
2025fcoe_hostlist_lookup_port(const struct net_device *dev)
Robert Love85b4aa42008-12-09 15:10:24 -08002026{
Chris Leech014f5c32009-08-25 13:59:30 -07002027 struct fcoe_interface *fcoe;
Robert Love85b4aa42008-12-09 15:10:24 -08002028
Chris Leech014f5c32009-08-25 13:59:30 -07002029 list_for_each_entry(fcoe, &fcoe_hostlist, list) {
Chris Leech25024982009-08-25 13:59:35 -07002030 if (fcoe->netdev == dev)
Chris Leech014f5c32009-08-25 13:59:30 -07002031 return fcoe;
Robert Love85b4aa42008-12-09 15:10:24 -08002032 }
Robert Love85b4aa42008-12-09 15:10:24 -08002033 return NULL;
2034}
2035
Robert Love34f42a02009-02-27 10:55:45 -08002036/**
2037 * fcoe_hostlist_lookup() - Find the corresponding lport by netdev
Robert Love85b4aa42008-12-09 15:10:24 -08002038 * @netdev: ptr to net_device
2039 *
2040 * Returns: 0 for success
Chris Leech090eb6c2009-08-25 14:00:28 -07002041 * Locking: must be called with the RTNL mutex held
Robert Love85b4aa42008-12-09 15:10:24 -08002042 */
Chris Leech090eb6c2009-08-25 14:00:28 -07002043static struct fc_lport *fcoe_hostlist_lookup(const struct net_device *netdev)
Robert Love85b4aa42008-12-09 15:10:24 -08002044{
Chris Leech014f5c32009-08-25 13:59:30 -07002045 struct fcoe_interface *fcoe;
Robert Love85b4aa42008-12-09 15:10:24 -08002046
Chris Leech014f5c32009-08-25 13:59:30 -07002047 fcoe = fcoe_hostlist_lookup_port(netdev);
Chris Leech3fe9a0b2009-08-25 13:59:46 -07002048 return (fcoe) ? fcoe->ctlr.lp : NULL;
Robert Love85b4aa42008-12-09 15:10:24 -08002049}
Robert Love85b4aa42008-12-09 15:10:24 -08002050
Robert Love34f42a02009-02-27 10:55:45 -08002051/**
2052 * fcoe_hostlist_add() - Add a lport to lports list
Chris Leechdd3fd722009-04-21 16:27:36 -07002053 * @lp: ptr to the fc_lport to be added
Robert Love85b4aa42008-12-09 15:10:24 -08002054 *
2055 * Returns: 0 for success
Chris Leech090eb6c2009-08-25 14:00:28 -07002056 * Locking: must be called with the RTNL mutex held
Robert Love85b4aa42008-12-09 15:10:24 -08002057 */
Chris Leech090eb6c2009-08-25 14:00:28 -07002058static int fcoe_hostlist_add(const struct fc_lport *lport)
Robert Love85b4aa42008-12-09 15:10:24 -08002059{
Chris Leech014f5c32009-08-25 13:59:30 -07002060 struct fcoe_interface *fcoe;
2061 struct fcoe_port *port;
Robert Love85b4aa42008-12-09 15:10:24 -08002062
Chris Leech014f5c32009-08-25 13:59:30 -07002063 fcoe = fcoe_hostlist_lookup_port(fcoe_netdev(lport));
2064 if (!fcoe) {
2065 port = lport_priv(lport);
2066 fcoe = port->fcoe;
2067 list_add_tail(&fcoe->list, &fcoe_hostlist);
Robert Love85b4aa42008-12-09 15:10:24 -08002068 }
2069 return 0;
2070}
Robert Love85b4aa42008-12-09 15:10:24 -08002071
Robert Love34f42a02009-02-27 10:55:45 -08002072/**
Robert Love34f42a02009-02-27 10:55:45 -08002073 * fcoe_init() - fcoe module loading initialization
Robert Love85b4aa42008-12-09 15:10:24 -08002074 *
Robert Love85b4aa42008-12-09 15:10:24 -08002075 * Returns 0 on success, negative on failure
Robert Love34f42a02009-02-27 10:55:45 -08002076 */
Robert Love85b4aa42008-12-09 15:10:24 -08002077static int __init fcoe_init(void)
2078{
Robert Love38eccab2009-03-17 11:41:30 -07002079 unsigned int cpu;
Robert Love8976f422009-03-17 11:41:46 -07002080 int rc = 0;
Robert Love85b4aa42008-12-09 15:10:24 -08002081 struct fcoe_percpu_s *p;
2082
Chris Leechdfc1d0f2009-08-25 14:00:13 -07002083 mutex_lock(&fcoe_config_mutex);
2084
Robert Love38eccab2009-03-17 11:41:30 -07002085 for_each_possible_cpu(cpu) {
Robert Love5e5e92d2009-03-17 11:41:35 -07002086 p = &per_cpu(fcoe_percpu, cpu);
Robert Love38eccab2009-03-17 11:41:30 -07002087 skb_queue_head_init(&p->fcoe_rx_list);
2088 }
2089
Robert Love8976f422009-03-17 11:41:46 -07002090 for_each_online_cpu(cpu)
2091 fcoe_percpu_thread_create(cpu);
Robert Love85b4aa42008-12-09 15:10:24 -08002092
Robert Love8976f422009-03-17 11:41:46 -07002093 /* Initialize per CPU interrupt thread */
2094 rc = register_hotcpu_notifier(&fcoe_cpu_notifier);
2095 if (rc)
2096 goto out_free;
Robert Love85b4aa42008-12-09 15:10:24 -08002097
Robert Love8976f422009-03-17 11:41:46 -07002098 /* Setup link change notification */
Robert Love85b4aa42008-12-09 15:10:24 -08002099 fcoe_dev_setup();
2100
Chris Leech5892c322009-08-25 13:59:14 -07002101 rc = fcoe_if_init();
2102 if (rc)
2103 goto out_free;
Robert Love85b4aa42008-12-09 15:10:24 -08002104
Chris Leechdfc1d0f2009-08-25 14:00:13 -07002105 mutex_unlock(&fcoe_config_mutex);
Robert Love85b4aa42008-12-09 15:10:24 -08002106 return 0;
Robert Love8976f422009-03-17 11:41:46 -07002107
2108out_free:
2109 for_each_online_cpu(cpu) {
2110 fcoe_percpu_thread_destroy(cpu);
2111 }
Chris Leechdfc1d0f2009-08-25 14:00:13 -07002112 mutex_unlock(&fcoe_config_mutex);
Robert Love8976f422009-03-17 11:41:46 -07002113 return rc;
Robert Love85b4aa42008-12-09 15:10:24 -08002114}
2115module_init(fcoe_init);
2116
2117/**
Robert Love34f42a02009-02-27 10:55:45 -08002118 * fcoe_exit() - fcoe module unloading cleanup
Robert Love85b4aa42008-12-09 15:10:24 -08002119 *
2120 * Returns 0 on success, negative on failure
Robert Love34f42a02009-02-27 10:55:45 -08002121 */
Robert Love85b4aa42008-12-09 15:10:24 -08002122static void __exit fcoe_exit(void)
2123{
Robert Love5e5e92d2009-03-17 11:41:35 -07002124 unsigned int cpu;
Chris Leech014f5c32009-08-25 13:59:30 -07002125 struct fcoe_interface *fcoe, *tmp;
Chris Leech2e70e242009-08-25 14:00:23 -07002126 struct fcoe_port *port;
Robert Love85b4aa42008-12-09 15:10:24 -08002127
Chris Leechdfc1d0f2009-08-25 14:00:13 -07002128 mutex_lock(&fcoe_config_mutex);
2129
Robert Love85b4aa42008-12-09 15:10:24 -08002130 fcoe_dev_cleanup();
2131
Vasu Dev5919a592009-03-27 09:03:29 -07002132 /* releases the associated fcoe hosts */
Chris Leech090eb6c2009-08-25 14:00:28 -07002133 rtnl_lock();
2134 list_for_each_entry_safe(fcoe, tmp, &fcoe_hostlist, list) {
Chris Leechc863df32009-08-25 14:00:18 -07002135 list_del(&fcoe->list);
Chris Leech2e70e242009-08-25 14:00:23 -07002136 port = lport_priv(fcoe->ctlr.lp);
Chris Leech2e70e242009-08-25 14:00:23 -07002137 fcoe_interface_cleanup(fcoe);
Chris Leech2e70e242009-08-25 14:00:23 -07002138 schedule_work(&port->destroy_work);
Chris Leechc863df32009-08-25 14:00:18 -07002139 }
Chris Leech090eb6c2009-08-25 14:00:28 -07002140 rtnl_unlock();
Robert Love85b4aa42008-12-09 15:10:24 -08002141
Robert Love8976f422009-03-17 11:41:46 -07002142 unregister_hotcpu_notifier(&fcoe_cpu_notifier);
2143
Chris Leech014f5c32009-08-25 13:59:30 -07002144 for_each_online_cpu(cpu)
Robert Love8976f422009-03-17 11:41:46 -07002145 fcoe_percpu_thread_destroy(cpu);
Robert Love85b4aa42008-12-09 15:10:24 -08002146
Chris Leechdfc1d0f2009-08-25 14:00:13 -07002147 mutex_unlock(&fcoe_config_mutex);
Chris Leech2e70e242009-08-25 14:00:23 -07002148
2149 /* flush any asyncronous interface destroys,
2150 * this should happen after the netdev notifier is unregistered */
2151 flush_scheduled_work();
Chris Leech9a057532009-11-03 11:46:40 -08002152 /* That will flush out all the N_Ports on the hostlist, but now we
2153 * may have NPIV VN_Ports scheduled for destruction */
2154 flush_scheduled_work();
Chris Leech2e70e242009-08-25 14:00:23 -07002155
2156 /* detach from scsi transport
2157 * must happen after all destroys are done, therefor after the flush */
2158 fcoe_if_exit();
Robert Love85b4aa42008-12-09 15:10:24 -08002159}
2160module_exit(fcoe_exit);
Chris Leech11b56182009-11-03 11:46:29 -08002161
2162/**
2163 * fcoe_flogi_resp() - FCoE specific FLOGI and FDISC response handler
2164 * @seq: active sequence in the FLOGI or FDISC exchange
2165 * @fp: response frame, or error encoded in a pointer (timeout)
2166 * @arg: pointer the the fcoe_ctlr structure
2167 *
2168 * This handles MAC address managment for FCoE, then passes control on to
2169 * the libfc FLOGI response handler.
2170 */
2171static void fcoe_flogi_resp(struct fc_seq *seq, struct fc_frame *fp, void *arg)
2172{
2173 struct fcoe_ctlr *fip = arg;
2174 struct fc_exch *exch = fc_seq_exch(seq);
2175 struct fc_lport *lport = exch->lp;
2176 u8 *mac;
2177
2178 if (IS_ERR(fp))
2179 goto done;
2180
2181 mac = fr_cb(fp)->granted_mac;
2182 if (is_zero_ether_addr(mac)) {
2183 /* pre-FIP */
2184 mac = eth_hdr(&fp->skb)->h_source;
2185 if (fcoe_ctlr_recv_flogi(fip, lport, fp, mac)) {
2186 fc_frame_free(fp);
2187 return;
2188 }
2189 } else {
2190 /* FIP, libfcoe has already seen it */
2191 fip->update_mac(lport, fr_cb(fp)->granted_mac);
2192 }
2193done:
2194 fc_lport_flogi_resp(seq, fp, lport);
2195}
2196
2197/**
2198 * fcoe_logo_resp() - FCoE specific LOGO response handler
2199 * @seq: active sequence in the LOGO exchange
2200 * @fp: response frame, or error encoded in a pointer (timeout)
2201 * @arg: pointer the the fcoe_ctlr structure
2202 *
2203 * This handles MAC address managment for FCoE, then passes control on to
2204 * the libfc LOGO response handler.
2205 */
2206static void fcoe_logo_resp(struct fc_seq *seq, struct fc_frame *fp, void *arg)
2207{
2208 struct fcoe_ctlr *fip = arg;
2209 struct fc_exch *exch = fc_seq_exch(seq);
2210 struct fc_lport *lport = exch->lp;
2211 static u8 zero_mac[ETH_ALEN] = { 0 };
2212
2213 if (!IS_ERR(fp))
2214 fip->update_mac(lport, zero_mac);
2215 fc_lport_logo_resp(seq, fp, lport);
2216}
2217
2218/**
2219 * fcoe_elsct_send - FCoE specific ELS handler
2220 *
2221 * This does special case handling of FIP encapsualted ELS exchanges for FCoE,
2222 * using FCoE specific response handlers and passing the FIP controller as
2223 * the argument (the lport is still available from the exchange).
2224 *
2225 * Most of the work here is just handed off to the libfc routine.
2226 */
2227static struct fc_seq *fcoe_elsct_send(struct fc_lport *lport,
2228 u32 did, struct fc_frame *fp, unsigned int op,
2229 void (*resp)(struct fc_seq *, struct fc_frame *, void *),
2230 void *arg, u32 timeout)
2231{
2232 struct fcoe_port *port = lport_priv(lport);
2233 struct fcoe_interface *fcoe = port->fcoe;
2234 struct fcoe_ctlr *fip = &fcoe->ctlr;
2235 struct fc_frame_header *fh = fc_frame_header_get(fp);
2236
2237 switch (op) {
2238 case ELS_FLOGI:
2239 case ELS_FDISC:
2240 return fc_elsct_send(lport, did, fp, op, fcoe_flogi_resp,
2241 fip, timeout);
2242 case ELS_LOGO:
2243 /* only hook onto fabric logouts, not port logouts */
2244 if (ntoh24(fh->fh_d_id) != FC_FID_FLOGI)
2245 break;
2246 return fc_elsct_send(lport, did, fp, op, fcoe_logo_resp,
2247 fip, timeout);
2248 }
2249 return fc_elsct_send(lport, did, fp, op, resp, arg, timeout);
2250}
2251
Chris Leech9a057532009-11-03 11:46:40 -08002252/**
2253 * fcoe_vport_create() - create an fc_host/scsi_host for a vport
2254 * @vport: fc_vport object to create a new fc_host for
2255 * @disabled: start the new fc_host in a disabled state by default?
2256 *
2257 * Returns: 0 for success
2258 */
2259static int fcoe_vport_create(struct fc_vport *vport, bool disabled)
2260{
2261 struct Scsi_Host *shost = vport_to_shost(vport);
2262 struct fc_lport *n_port = shost_priv(shost);
2263 struct fcoe_port *port = lport_priv(n_port);
2264 struct fcoe_interface *fcoe = port->fcoe;
2265 struct net_device *netdev = fcoe->netdev;
2266 struct fc_lport *vn_port;
2267
2268 mutex_lock(&fcoe_config_mutex);
2269 vn_port = fcoe_if_create(fcoe, &vport->dev, 1);
2270 mutex_unlock(&fcoe_config_mutex);
2271
2272 if (IS_ERR(vn_port)) {
2273 printk(KERN_ERR "fcoe: fcoe_vport_create(%s) failed\n",
2274 netdev->name);
2275 return -EIO;
2276 }
2277
2278 if (disabled) {
2279 fc_vport_set_state(vport, FC_VPORT_DISABLED);
2280 } else {
2281 vn_port->boot_time = jiffies;
2282 fc_fabric_login(vn_port);
2283 fc_vport_setlink(vn_port);
2284 }
2285 return 0;
2286}
2287
2288/**
2289 * fcoe_vport_destroy() - destroy the fc_host/scsi_host for a vport
2290 * @vport: fc_vport object that is being destroyed
2291 *
2292 * Returns: 0 for success
2293 */
2294static int fcoe_vport_destroy(struct fc_vport *vport)
2295{
2296 struct Scsi_Host *shost = vport_to_shost(vport);
2297 struct fc_lport *n_port = shost_priv(shost);
2298 struct fc_lport *vn_port = vport->dd_data;
2299 struct fcoe_port *port = lport_priv(vn_port);
2300
2301 mutex_lock(&n_port->lp_mutex);
2302 list_del(&vn_port->list);
2303 mutex_unlock(&n_port->lp_mutex);
2304 schedule_work(&port->destroy_work);
2305 return 0;
2306}
2307
2308/**
2309 * fcoe_vport_disable() - change vport state
2310 * @vport: vport to bring online/offline
2311 * @disable: should the vport be disabled?
2312 */
2313static int fcoe_vport_disable(struct fc_vport *vport, bool disable)
2314{
2315 struct fc_lport *lport = vport->dd_data;
2316
2317 if (disable) {
2318 fc_vport_set_state(vport, FC_VPORT_DISABLED);
2319 fc_fabric_logoff(lport);
2320 } else {
2321 lport->boot_time = jiffies;
2322 fc_fabric_login(lport);
2323 fc_vport_setlink(lport);
2324 }
2325
2326 return 0;
2327}
2328