blob: 17ce2efc3c19da448bce64490bae78f024ef01e6 [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 */
Robert Love1875f272009-11-03 11:47:50 -080069static int fcoe_reset(struct Scsi_Host *);
Vasu Devfdd78022009-03-17 11:42:24 -070070static 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 *);
Robert Love1875f272009-11-03 11:47:50 -080073static int fcoe_percpu_receive_thread(void *);
74static void fcoe_clean_pending_queue(struct fc_lport *);
75static void fcoe_percpu_clean(struct fc_lport *);
76static int fcoe_link_ok(struct fc_lport *);
Vasu Devfdd78022009-03-17 11:42:24 -070077
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);
Robert Love1875f272009-11-03 11:47:50 -080085static struct fcoe_interface
86*fcoe_hostlist_lookup_port(const struct net_device *);
Robert Love85b4aa42008-12-09 15:10:24 -080087
Robert Love1875f272009-11-03 11:47:50 -080088static int fcoe_fip_recv(struct sk_buff *, struct net_device *,
89 struct packet_type *, struct net_device *);
90
91static void fcoe_fip_send(struct fcoe_ctlr *, struct sk_buff *);
92static void fcoe_update_src_mac(struct fc_lport *, u8 *);
93static u8 *fcoe_get_src_mac(struct fc_lport *);
94static void fcoe_destroy_work(struct work_struct *);
95
96static int fcoe_ddp_setup(struct fc_lport *, u16, struct scatterlist *,
97 unsigned int);
98static int fcoe_ddp_done(struct fc_lport *, u16);
99
100static int fcoe_cpu_callback(struct notifier_block *, unsigned long, void *);
101
102static int fcoe_create(const char *, struct kernel_param *);
103static int fcoe_destroy(const char *, struct kernel_param *);
104
Robert Love1875f272009-11-03 11:47:50 -0800105static struct fc_seq *fcoe_elsct_send(struct fc_lport *,
106 u32 did, struct fc_frame *,
107 unsigned int op,
108 void (*resp)(struct fc_seq *,
109 struct fc_frame *,
110 void *),
111 void *, u32 timeout);
112
113module_param_call(create, fcoe_create, NULL, NULL, S_IWUSR);
114__MODULE_PARM_TYPE(create, "string");
115MODULE_PARM_DESC(create, "Create fcoe fcoe using net device passed in.");
116module_param_call(destroy, fcoe_destroy, NULL, NULL, S_IWUSR);
117__MODULE_PARM_TYPE(destroy, "string");
118MODULE_PARM_DESC(destroy, "Destroy fcoe fcoe");
119
120/* notification function for packets from net device */
Robert Love85b4aa42008-12-09 15:10:24 -0800121static struct notifier_block fcoe_notifier = {
122 .notifier_call = fcoe_device_notification,
123};
124
Robert Love1875f272009-11-03 11:47:50 -0800125/* notification function for CPU hotplug events */
126static struct notifier_block fcoe_cpu_notifier = {
127 .notifier_call = fcoe_cpu_callback,
128};
129
Chris Leeche9084bb2009-11-03 11:46:34 -0800130static struct scsi_transport_template *fcoe_transport_template;
131static struct scsi_transport_template *fcoe_vport_transport_template;
Vasu Dev7f349142009-03-27 09:06:31 -0700132
Robert Love1875f272009-11-03 11:47:50 -0800133static int fcoe_vport_destroy(struct fc_vport *);
134static int fcoe_vport_create(struct fc_vport *, bool disabled);
135static int fcoe_vport_disable(struct fc_vport *, bool disable);
136static void fcoe_set_vport_symbolic_name(struct fc_vport *);
137
138static struct libfc_function_template fcoe_libfc_fcn_templ = {
139 .frame_send = fcoe_xmit,
140 .ddp_setup = fcoe_ddp_setup,
141 .ddp_done = fcoe_ddp_done,
142 .elsct_send = fcoe_elsct_send,
143};
Chris Leech9a057532009-11-03 11:46:40 -0800144
Vasu Dev7f349142009-03-27 09:06:31 -0700145struct fc_function_template fcoe_transport_function = {
146 .show_host_node_name = 1,
147 .show_host_port_name = 1,
148 .show_host_supported_classes = 1,
149 .show_host_supported_fc4s = 1,
150 .show_host_active_fc4s = 1,
151 .show_host_maxframe_size = 1,
152
153 .show_host_port_id = 1,
154 .show_host_supported_speeds = 1,
155 .get_host_speed = fc_get_host_speed,
156 .show_host_speed = 1,
157 .show_host_port_type = 1,
158 .get_host_port_state = fc_get_host_port_state,
159 .show_host_port_state = 1,
160 .show_host_symbolic_name = 1,
161
162 .dd_fcrport_size = sizeof(struct fc_rport_libfc_priv),
163 .show_rport_maxframe_size = 1,
164 .show_rport_supported_classes = 1,
165
166 .show_host_fabric_name = 1,
167 .show_starget_node_name = 1,
168 .show_starget_port_name = 1,
169 .show_starget_port_id = 1,
170 .set_rport_dev_loss_tmo = fc_set_rport_loss_tmo,
171 .show_rport_dev_loss_tmo = 1,
172 .get_fc_host_stats = fc_get_host_stats,
173 .issue_fc_host_lip = fcoe_reset,
174
175 .terminate_rport_io = fc_rport_terminate_io,
Chris Leech9a057532009-11-03 11:46:40 -0800176
177 .vport_create = fcoe_vport_create,
178 .vport_delete = fcoe_vport_destroy,
179 .vport_disable = fcoe_vport_disable,
Chris Leechdc8596d2009-11-03 11:47:18 -0800180 .set_vport_symbolic_name = fcoe_set_vport_symbolic_name,
Steve Maa51ab392009-11-03 11:47:34 -0800181
182 .bsg_request = fc_lport_bsg_request,
Vasu Dev7f349142009-03-27 09:06:31 -0700183};
184
Chris Leeche9084bb2009-11-03 11:46:34 -0800185struct fc_function_template fcoe_vport_transport_function = {
186 .show_host_node_name = 1,
187 .show_host_port_name = 1,
188 .show_host_supported_classes = 1,
189 .show_host_supported_fc4s = 1,
190 .show_host_active_fc4s = 1,
191 .show_host_maxframe_size = 1,
192
193 .show_host_port_id = 1,
194 .show_host_supported_speeds = 1,
195 .get_host_speed = fc_get_host_speed,
196 .show_host_speed = 1,
197 .show_host_port_type = 1,
198 .get_host_port_state = fc_get_host_port_state,
199 .show_host_port_state = 1,
200 .show_host_symbolic_name = 1,
201
202 .dd_fcrport_size = sizeof(struct fc_rport_libfc_priv),
203 .show_rport_maxframe_size = 1,
204 .show_rport_supported_classes = 1,
205
206 .show_host_fabric_name = 1,
207 .show_starget_node_name = 1,
208 .show_starget_port_name = 1,
209 .show_starget_port_id = 1,
210 .set_rport_dev_loss_tmo = fc_set_rport_loss_tmo,
211 .show_rport_dev_loss_tmo = 1,
212 .get_fc_host_stats = fc_get_host_stats,
213 .issue_fc_host_lip = fcoe_reset,
214
215 .terminate_rport_io = fc_rport_terminate_io,
Steve Maa51ab392009-11-03 11:47:34 -0800216
217 .bsg_request = fc_lport_bsg_request,
Chris Leeche9084bb2009-11-03 11:46:34 -0800218};
219
Vasu Dev7f349142009-03-27 09:06:31 -0700220static struct scsi_host_template fcoe_shost_template = {
221 .module = THIS_MODULE,
222 .name = "FCoE Driver",
223 .proc_name = FCOE_NAME,
224 .queuecommand = fc_queuecommand,
225 .eh_abort_handler = fc_eh_abort,
226 .eh_device_reset_handler = fc_eh_device_reset,
227 .eh_host_reset_handler = fc_eh_host_reset,
228 .slave_alloc = fc_slave_alloc,
229 .change_queue_depth = fc_change_queue_depth,
230 .change_queue_type = fc_change_queue_type,
231 .this_id = -1,
Vasu Dev14caf442009-10-15 17:46:55 -0700232 .cmd_per_lun = 3,
Vasu Dev7f349142009-03-27 09:06:31 -0700233 .can_queue = FCOE_MAX_OUTSTANDING_COMMANDS,
234 .use_clustering = ENABLE_CLUSTERING,
235 .sg_tablesize = SG_ALL,
236 .max_sectors = 0xffff,
237};
238
Chris Leech54b649f2009-08-25 14:00:07 -0700239/**
Robert Love1875f272009-11-03 11:47:50 -0800240 * fcoe_interface_setup() - Setup a FCoE interface
241 * @fcoe: The new FCoE interface
242 * @netdev: The net device that the fcoe interface is on
Chris Leech54b649f2009-08-25 14:00:07 -0700243 *
244 * Returns : 0 for success
Chris Leech2e70e242009-08-25 14:00:23 -0700245 * Locking: must be called with the RTNL mutex held
Chris Leech54b649f2009-08-25 14:00:07 -0700246 */
247static int fcoe_interface_setup(struct fcoe_interface *fcoe,
248 struct net_device *netdev)
249{
250 struct fcoe_ctlr *fip = &fcoe->ctlr;
251 struct netdev_hw_addr *ha;
252 u8 flogi_maddr[ETH_ALEN];
Yi Zoub7a727f2009-10-21 16:28:03 -0700253 const struct net_device_ops *ops;
Chris Leech54b649f2009-08-25 14:00:07 -0700254
255 fcoe->netdev = netdev;
256
Yi Zoub7a727f2009-10-21 16:28:03 -0700257 /* Let LLD initialize for FCoE */
258 ops = netdev->netdev_ops;
259 if (ops->ndo_fcoe_enable) {
260 if (ops->ndo_fcoe_enable(netdev))
261 FCOE_NETDEV_DBG(netdev, "Failed to enable FCoE"
262 " specific feature for LLD.\n");
263 }
264
Chris Leech54b649f2009-08-25 14:00:07 -0700265 /* Do not support for bonding device */
266 if ((netdev->priv_flags & IFF_MASTER_ALB) ||
267 (netdev->priv_flags & IFF_SLAVE_INACTIVE) ||
268 (netdev->priv_flags & IFF_MASTER_8023AD)) {
269 return -EOPNOTSUPP;
270 }
271
272 /* look for SAN MAC address, if multiple SAN MACs exist, only
273 * use the first one for SPMA */
274 rcu_read_lock();
275 for_each_dev_addr(netdev, ha) {
276 if ((ha->type == NETDEV_HW_ADDR_T_SAN) &&
277 (is_valid_ether_addr(fip->ctl_src_addr))) {
278 memcpy(fip->ctl_src_addr, ha->addr, ETH_ALEN);
279 fip->spma = 1;
280 break;
281 }
282 }
283 rcu_read_unlock();
284
285 /* setup Source Mac Address */
286 if (!fip->spma)
287 memcpy(fip->ctl_src_addr, netdev->dev_addr, netdev->addr_len);
288
289 /*
290 * Add FCoE MAC address as second unicast MAC address
291 * or enter promiscuous mode if not capable of listening
292 * for multiple unicast MACs.
293 */
Chris Leech54b649f2009-08-25 14:00:07 -0700294 memcpy(flogi_maddr, (u8[6]) FC_FCOE_FLOGI_MAC, ETH_ALEN);
295 dev_unicast_add(netdev, flogi_maddr);
296 if (fip->spma)
297 dev_unicast_add(netdev, fip->ctl_src_addr);
298 dev_mc_add(netdev, FIP_ALL_ENODE_MACS, ETH_ALEN, 0);
Chris Leech54b649f2009-08-25 14:00:07 -0700299
300 /*
301 * setup the receive function from ethernet driver
302 * on the ethertype for the given device
303 */
304 fcoe->fcoe_packet_type.func = fcoe_rcv;
305 fcoe->fcoe_packet_type.type = __constant_htons(ETH_P_FCOE);
306 fcoe->fcoe_packet_type.dev = netdev;
307 dev_add_pack(&fcoe->fcoe_packet_type);
308
309 fcoe->fip_packet_type.func = fcoe_fip_recv;
310 fcoe->fip_packet_type.type = htons(ETH_P_FIP);
311 fcoe->fip_packet_type.dev = netdev;
312 dev_add_pack(&fcoe->fip_packet_type);
313
314 return 0;
315}
316
Vasu Dev7f349142009-03-27 09:06:31 -0700317/**
Robert Love1875f272009-11-03 11:47:50 -0800318 * fcoe_interface_create() - Create a FCoE interface on a net device
319 * @netdev: The net device to create the FCoE interface on
Chris Leech030f4e02009-08-25 14:00:02 -0700320 *
321 * Returns: pointer to a struct fcoe_interface or NULL on error
322 */
323static struct fcoe_interface *fcoe_interface_create(struct net_device *netdev)
324{
325 struct fcoe_interface *fcoe;
326
327 fcoe = kzalloc(sizeof(*fcoe), GFP_KERNEL);
328 if (!fcoe) {
329 FCOE_NETDEV_DBG(netdev, "Could not allocate fcoe structure\n");
330 return NULL;
331 }
332
Chris Leech2e70e242009-08-25 14:00:23 -0700333 dev_hold(netdev);
Chris Leech030f4e02009-08-25 14:00:02 -0700334 kref_init(&fcoe->kref);
Chris Leech54b649f2009-08-25 14:00:07 -0700335
336 /*
337 * Initialize FIP.
338 */
339 fcoe_ctlr_init(&fcoe->ctlr);
340 fcoe->ctlr.send = fcoe_fip_send;
341 fcoe->ctlr.update_mac = fcoe_update_src_mac;
Chris Leech11b56182009-11-03 11:46:29 -0800342 fcoe->ctlr.get_src_addr = fcoe_get_src_mac;
Chris Leech54b649f2009-08-25 14:00:07 -0700343
344 fcoe_interface_setup(fcoe, netdev);
Chris Leech030f4e02009-08-25 14:00:02 -0700345
346 return fcoe;
347}
348
349/**
Robert Love1875f272009-11-03 11:47:50 -0800350 * fcoe_interface_cleanup() - Clean up a FCoE interface
351 * @fcoe: The FCoE interface to be cleaned up
Chris Leech2e70e242009-08-25 14:00:23 -0700352 *
353 * Caller must be holding the RTNL mutex
Chris Leech54b649f2009-08-25 14:00:07 -0700354 */
355void fcoe_interface_cleanup(struct fcoe_interface *fcoe)
356{
357 struct net_device *netdev = fcoe->netdev;
358 struct fcoe_ctlr *fip = &fcoe->ctlr;
359 u8 flogi_maddr[ETH_ALEN];
Yi Zoub7a727f2009-10-21 16:28:03 -0700360 const struct net_device_ops *ops;
Chris Leech54b649f2009-08-25 14:00:07 -0700361
362 /*
363 * Don't listen for Ethernet packets anymore.
364 * synchronize_net() ensures that the packet handlers are not running
365 * on another CPU. dev_remove_pack() would do that, this calls the
366 * unsyncronized version __dev_remove_pack() to avoid multiple delays.
367 */
368 __dev_remove_pack(&fcoe->fcoe_packet_type);
369 __dev_remove_pack(&fcoe->fip_packet_type);
370 synchronize_net();
371
Chris Leech54b649f2009-08-25 14:00:07 -0700372 /* Delete secondary MAC addresses */
Chris Leech54b649f2009-08-25 14:00:07 -0700373 memcpy(flogi_maddr, (u8[6]) FC_FCOE_FLOGI_MAC, ETH_ALEN);
374 dev_unicast_delete(netdev, flogi_maddr);
Chris Leech54b649f2009-08-25 14:00:07 -0700375 if (fip->spma)
376 dev_unicast_delete(netdev, fip->ctl_src_addr);
377 dev_mc_delete(netdev, FIP_ALL_ENODE_MACS, ETH_ALEN, 0);
Yi Zoub7a727f2009-10-21 16:28:03 -0700378
379 /* Tell the LLD we are done w/ FCoE */
380 ops = netdev->netdev_ops;
381 if (ops->ndo_fcoe_disable) {
382 if (ops->ndo_fcoe_disable(netdev))
383 FCOE_NETDEV_DBG(netdev, "Failed to disable FCoE"
384 " specific feature for LLD.\n");
385 }
Chris Leech54b649f2009-08-25 14:00:07 -0700386}
387
388/**
Chris Leech030f4e02009-08-25 14:00:02 -0700389 * fcoe_interface_release() - fcoe_port kref release function
Robert Love1875f272009-11-03 11:47:50 -0800390 * @kref: Embedded reference count in an fcoe_interface struct
Chris Leech030f4e02009-08-25 14:00:02 -0700391 */
392static void fcoe_interface_release(struct kref *kref)
393{
394 struct fcoe_interface *fcoe;
Chris Leech2e70e242009-08-25 14:00:23 -0700395 struct net_device *netdev;
Chris Leech030f4e02009-08-25 14:00:02 -0700396
397 fcoe = container_of(kref, struct fcoe_interface, kref);
Chris Leech2e70e242009-08-25 14:00:23 -0700398 netdev = fcoe->netdev;
399 /* tear-down the FCoE controller */
400 fcoe_ctlr_destroy(&fcoe->ctlr);
Chris Leech030f4e02009-08-25 14:00:02 -0700401 kfree(fcoe);
Chris Leech2e70e242009-08-25 14:00:23 -0700402 dev_put(netdev);
Chris Leech030f4e02009-08-25 14:00:02 -0700403}
404
405/**
Robert Love1875f272009-11-03 11:47:50 -0800406 * fcoe_interface_get() - Get a reference to a FCoE interface
407 * @fcoe: The FCoE interface to be held
Chris Leech030f4e02009-08-25 14:00:02 -0700408 */
409static inline void fcoe_interface_get(struct fcoe_interface *fcoe)
410{
411 kref_get(&fcoe->kref);
412}
413
414/**
Robert Love1875f272009-11-03 11:47:50 -0800415 * fcoe_interface_put() - Put a reference to a FCoE interface
416 * @fcoe: The FCoE interface to be released
Chris Leech030f4e02009-08-25 14:00:02 -0700417 */
418static inline void fcoe_interface_put(struct fcoe_interface *fcoe)
419{
420 kref_put(&fcoe->kref, fcoe_interface_release);
421}
422
423/**
Robert Love1875f272009-11-03 11:47:50 -0800424 * fcoe_fip_recv() - Handler for received FIP frames
425 * @skb: The receive skb
426 * @netdev: The associated net device
427 * @ptype: The packet_type structure which was used to register this handler
428 * @orig_dev: The original net_device the the skb was received on.
429 * (in case dev is a bond)
Vasu Devab6b85c2009-05-17 12:33:08 +0000430 *
431 * Returns: 0 for success
432 */
Robert Love1875f272009-11-03 11:47:50 -0800433static int fcoe_fip_recv(struct sk_buff *skb, struct net_device *netdev,
Vasu Devab6b85c2009-05-17 12:33:08 +0000434 struct packet_type *ptype,
435 struct net_device *orig_dev)
436{
Chris Leech259ad852009-08-25 13:59:41 -0700437 struct fcoe_interface *fcoe;
Vasu Devab6b85c2009-05-17 12:33:08 +0000438
Chris Leech259ad852009-08-25 13:59:41 -0700439 fcoe = container_of(ptype, struct fcoe_interface, fip_packet_type);
Chris Leech3fe9a0b2009-08-25 13:59:46 -0700440 fcoe_ctlr_recv(&fcoe->ctlr, skb);
Vasu Devab6b85c2009-05-17 12:33:08 +0000441 return 0;
442}
443
444/**
Robert Love1875f272009-11-03 11:47:50 -0800445 * fcoe_fip_send() - Send an Ethernet-encapsulated FIP frame
446 * @fip: The FCoE controller
447 * @skb: The FIP packet to be sent
Vasu Devab6b85c2009-05-17 12:33:08 +0000448 */
449static void fcoe_fip_send(struct fcoe_ctlr *fip, struct sk_buff *skb)
450{
Chris Leech3fe9a0b2009-08-25 13:59:46 -0700451 skb->dev = fcoe_from_ctlr(fip)->netdev;
Vasu Devab6b85c2009-05-17 12:33:08 +0000452 dev_queue_xmit(skb);
453}
454
455/**
Robert Love1875f272009-11-03 11:47:50 -0800456 * fcoe_update_src_mac() - Update the Ethernet MAC filters
457 * @lport: The local port to update the source MAC on
458 * @addr: Unicast MAC address to add
Vasu Devab6b85c2009-05-17 12:33:08 +0000459 *
460 * Remove any previously-set unicast MAC filter.
461 * Add secondary FCoE MAC address filter for our OUI.
462 */
Chris Leech11b56182009-11-03 11:46:29 -0800463static void fcoe_update_src_mac(struct fc_lport *lport, u8 *addr)
Vasu Devab6b85c2009-05-17 12:33:08 +0000464{
Chris Leech11b56182009-11-03 11:46:29 -0800465 struct fcoe_port *port = lport_priv(lport);
466 struct fcoe_interface *fcoe = port->fcoe;
Vasu Devab6b85c2009-05-17 12:33:08 +0000467
Vasu Devab6b85c2009-05-17 12:33:08 +0000468 rtnl_lock();
Chris Leech11b56182009-11-03 11:46:29 -0800469 if (!is_zero_ether_addr(port->data_src_addr))
470 dev_unicast_delete(fcoe->netdev, port->data_src_addr);
471 if (!is_zero_ether_addr(addr))
472 dev_unicast_add(fcoe->netdev, addr);
473 memcpy(port->data_src_addr, addr, ETH_ALEN);
Vasu Devab6b85c2009-05-17 12:33:08 +0000474 rtnl_unlock();
475}
476
477/**
Chris Leech11b56182009-11-03 11:46:29 -0800478 * fcoe_get_src_mac() - return the Ethernet source address for an lport
479 * @lport: libfc lport
480 */
481static u8 *fcoe_get_src_mac(struct fc_lport *lport)
482{
483 struct fcoe_port *port = lport_priv(lport);
484
485 return port->data_src_addr;
486}
487
488/**
Robert Love1875f272009-11-03 11:47:50 -0800489 * fcoe_lport_config() - Set up a local port
490 * @lport: The local port to be setup
Vasu Dev7f349142009-03-27 09:06:31 -0700491 *
492 * Returns: 0 for success
493 */
Robert Love1875f272009-11-03 11:47:50 -0800494static int fcoe_lport_config(struct fc_lport *lport)
Vasu Dev7f349142009-03-27 09:06:31 -0700495{
Robert Love1875f272009-11-03 11:47:50 -0800496 lport->link_up = 0;
497 lport->qfull = 0;
498 lport->max_retry_count = 3;
499 lport->max_rport_retry_count = 3;
500 lport->e_d_tov = 2 * 1000; /* FC-FS default */
501 lport->r_a_tov = 2 * 2 * 1000;
502 lport->service_params = (FCP_SPPF_INIT_FCN | FCP_SPPF_RD_XRDY_DIS |
503 FCP_SPPF_RETRY | FCP_SPPF_CONF_COMPL);
504 lport->does_npiv = 1;
Vasu Dev7f349142009-03-27 09:06:31 -0700505
Robert Love1875f272009-11-03 11:47:50 -0800506 fc_lport_init_stats(lport);
Vasu Dev7f349142009-03-27 09:06:31 -0700507
508 /* lport fc_lport related configuration */
Robert Love1875f272009-11-03 11:47:50 -0800509 fc_lport_config(lport);
Vasu Dev7f349142009-03-27 09:06:31 -0700510
511 /* offload related configuration */
Robert Love1875f272009-11-03 11:47:50 -0800512 lport->crc_offload = 0;
513 lport->seq_offload = 0;
514 lport->lro_enabled = 0;
515 lport->lro_xid = 0;
516 lport->lso_max = 0;
Vasu Dev7f349142009-03-27 09:06:31 -0700517
518 return 0;
519}
520
521/**
Robert Love1875f272009-11-03 11:47:50 -0800522 * fcoe_queue_timer() - The fcoe queue timer
523 * @lport: The local port
Vasu Dev1047f222009-05-06 10:52:40 -0700524 *
525 * Calls fcoe_check_wait_queue on timeout
Vasu Dev1047f222009-05-06 10:52:40 -0700526 */
Robert Love1875f272009-11-03 11:47:50 -0800527static void fcoe_queue_timer(ulong lport)
Vasu Dev1047f222009-05-06 10:52:40 -0700528{
Robert Love1875f272009-11-03 11:47:50 -0800529 fcoe_check_wait_queue((struct fc_lport *)lport, NULL);
Vasu Dev1047f222009-05-06 10:52:40 -0700530}
531
532/**
Robert Love1875f272009-11-03 11:47:50 -0800533 * fcoe_netdev_config() - Set up net devive for SW FCoE
534 * @lport: The local port that is associated with the net device
535 * @netdev: The associated net device
Vasu Dev7f349142009-03-27 09:06:31 -0700536 *
Robert Love1875f272009-11-03 11:47:50 -0800537 * Must be called after fcoe_lport_config() as it will use local port mutex
Vasu Dev7f349142009-03-27 09:06:31 -0700538 *
Robert Love1875f272009-11-03 11:47:50 -0800539 * Returns: 0 for success
Vasu Dev7f349142009-03-27 09:06:31 -0700540 */
Robert Love1875f272009-11-03 11:47:50 -0800541static int fcoe_netdev_config(struct fc_lport *lport, struct net_device *netdev)
Vasu Dev7f349142009-03-27 09:06:31 -0700542{
543 u32 mfs;
544 u64 wwnn, wwpn;
Chris Leech25024982009-08-25 13:59:35 -0700545 struct fcoe_interface *fcoe;
Chris Leech014f5c32009-08-25 13:59:30 -0700546 struct fcoe_port *port;
Vasu Dev7f349142009-03-27 09:06:31 -0700547
548 /* Setup lport private data to point to fcoe softc */
Robert Love1875f272009-11-03 11:47:50 -0800549 port = lport_priv(lport);
Chris Leech25024982009-08-25 13:59:35 -0700550 fcoe = port->fcoe;
Vasu Dev7f349142009-03-27 09:06:31 -0700551
552 /*
553 * Determine max frame size based on underlying device and optional
554 * user-configured limit. If the MFS is too low, fcoe_link_ok()
555 * will return 0, so do this first.
556 */
Yi Zou7221d7e2009-10-21 16:27:52 -0700557 mfs = netdev->mtu;
558 if (netdev->features & NETIF_F_FCOE_MTU) {
559 mfs = FCOE_MTU;
560 FCOE_NETDEV_DBG(netdev, "Supports FCOE_MTU of %d bytes\n", mfs);
561 }
562 mfs -= (sizeof(struct fcoe_hdr) + sizeof(struct fcoe_crc_eof));
Robert Love1875f272009-11-03 11:47:50 -0800563 if (fc_set_mfs(lport, mfs))
Vasu Dev7f349142009-03-27 09:06:31 -0700564 return -EINVAL;
565
Vasu Dev7f349142009-03-27 09:06:31 -0700566 /* offload features support */
Vasu Dev1d1b88d2009-07-29 17:05:45 -0700567 if (netdev->features & NETIF_F_SG)
Robert Love1875f272009-11-03 11:47:50 -0800568 lport->sg_supp = 1;
Vasu Dev7f349142009-03-27 09:06:31 -0700569
Vasu Dev7f349142009-03-27 09:06:31 -0700570 if (netdev->features & NETIF_F_FCOE_CRC) {
Robert Love1875f272009-11-03 11:47:50 -0800571 lport->crc_offload = 1;
Robert Loved5488eb2009-06-10 15:30:59 -0700572 FCOE_NETDEV_DBG(netdev, "Supports FCCRC offload\n");
Vasu Dev7f349142009-03-27 09:06:31 -0700573 }
Vasu Dev7f349142009-03-27 09:06:31 -0700574 if (netdev->features & NETIF_F_FSO) {
Robert Love1875f272009-11-03 11:47:50 -0800575 lport->seq_offload = 1;
576 lport->lso_max = netdev->gso_max_size;
Robert Loved5488eb2009-06-10 15:30:59 -0700577 FCOE_NETDEV_DBG(netdev, "Supports LSO for max len 0x%x\n",
Robert Love1875f272009-11-03 11:47:50 -0800578 lport->lso_max);
Vasu Dev7f349142009-03-27 09:06:31 -0700579 }
Vasu Dev7f349142009-03-27 09:06:31 -0700580 if (netdev->fcoe_ddp_xid) {
Robert Love1875f272009-11-03 11:47:50 -0800581 lport->lro_enabled = 1;
582 lport->lro_xid = netdev->fcoe_ddp_xid;
Robert Loved5488eb2009-06-10 15:30:59 -0700583 FCOE_NETDEV_DBG(netdev, "Supports LRO for max xid 0x%x\n",
Robert Love1875f272009-11-03 11:47:50 -0800584 lport->lro_xid);
Vasu Dev7f349142009-03-27 09:06:31 -0700585 }
Chris Leech014f5c32009-08-25 13:59:30 -0700586 skb_queue_head_init(&port->fcoe_pending_queue);
587 port->fcoe_pending_queue_active = 0;
Robert Love1875f272009-11-03 11:47:50 -0800588 setup_timer(&port->timer, fcoe_queue_timer, (unsigned long)lport);
Vasu Dev7f349142009-03-27 09:06:31 -0700589
Robert Love1875f272009-11-03 11:47:50 -0800590 if (!lport->vport) {
Chris Leech9a057532009-11-03 11:46:40 -0800591 wwnn = fcoe_wwn_from_mac(netdev->dev_addr, 1, 0);
Robert Love1875f272009-11-03 11:47:50 -0800592 fc_set_wwnn(lport, wwnn);
Chris Leech9a057532009-11-03 11:46:40 -0800593 /* XXX - 3rd arg needs to be vlan id */
594 wwpn = fcoe_wwn_from_mac(netdev->dev_addr, 2, 0);
Robert Love1875f272009-11-03 11:47:50 -0800595 fc_set_wwpn(lport, wwpn);
Chris Leech9a057532009-11-03 11:46:40 -0800596 }
Vasu Dev7f349142009-03-27 09:06:31 -0700597
Vasu Dev7f349142009-03-27 09:06:31 -0700598 return 0;
599}
600
601/**
Robert Love1875f272009-11-03 11:47:50 -0800602 * fcoe_shost_config() - Set up the SCSI host associated with a local port
603 * @lport: The local port
604 * @shost: The SCSI host to associate with the local port
605 * @dev: The device associated with the SCSI host
Vasu Dev7f349142009-03-27 09:06:31 -0700606 *
607 * Must be called after fcoe_lport_config() and fcoe_netdev_config()
608 *
Robert Love1875f272009-11-03 11:47:50 -0800609 * Returns: 0 for success
Vasu Dev7f349142009-03-27 09:06:31 -0700610 */
Robert Love1875f272009-11-03 11:47:50 -0800611static int fcoe_shost_config(struct fc_lport *lport, struct Scsi_Host *shost,
612 struct device *dev)
Vasu Dev7f349142009-03-27 09:06:31 -0700613{
614 int rc = 0;
615
616 /* lport scsi host config */
Robert Love1875f272009-11-03 11:47:50 -0800617 lport->host->max_lun = FCOE_MAX_LUN;
618 lport->host->max_id = FCOE_MAX_FCP_TARGET;
619 lport->host->max_channel = 0;
620 if (lport->vport)
621 lport->host->transportt = fcoe_vport_transport_template;
Chris Leeche9084bb2009-11-03 11:46:34 -0800622 else
Robert Love1875f272009-11-03 11:47:50 -0800623 lport->host->transportt = fcoe_transport_template;
Vasu Dev7f349142009-03-27 09:06:31 -0700624
625 /* add the new host to the SCSI-ml */
Robert Love1875f272009-11-03 11:47:50 -0800626 rc = scsi_add_host(lport->host, dev);
Vasu Dev7f349142009-03-27 09:06:31 -0700627 if (rc) {
Robert Love1875f272009-11-03 11:47:50 -0800628 FCOE_NETDEV_DBG(fcoe_netdev(lport), "fcoe_shost_config: "
Robert Loved5488eb2009-06-10 15:30:59 -0700629 "error on scsi_add_host\n");
Vasu Dev7f349142009-03-27 09:06:31 -0700630 return rc;
631 }
Chris Leech9a057532009-11-03 11:46:40 -0800632
Robert Love1875f272009-11-03 11:47:50 -0800633 if (!lport->vport)
634 fc_host_max_npiv_vports(lport->host) = USHORT_MAX;
Chris Leech9a057532009-11-03 11:46:40 -0800635
Robert Love1875f272009-11-03 11:47:50 -0800636 snprintf(fc_host_symbolic_name(lport->host), FC_SYMBOLIC_NAME_SIZE,
Chris Leech5baa17c2009-11-03 11:46:56 -0800637 "%s v%s over %s", FCOE_NAME, FCOE_VERSION,
Robert Love1875f272009-11-03 11:47:50 -0800638 fcoe_netdev(lport)->name);
Vasu Dev7f349142009-03-27 09:06:31 -0700639
640 return 0;
641}
642
Robert Love1875f272009-11-03 11:47:50 -0800643/**
644 * fcoe_oem_match() - The match routine for the offloaded exchange manager
645 * @fp: The I/O frame
Vasu Devd7179682009-07-29 17:05:21 -0700646 *
Robert Love1875f272009-11-03 11:47:50 -0800647 * This routine will be associated with an exchange manager (EM). When
648 * the libfc exchange handling code is looking for an EM to use it will
649 * call this routine and pass it the frame that it wishes to send. This
650 * routine will return True if the associated EM is to be used and False
651 * if the echange code should continue looking for an EM.
652 *
653 * The offload EM that this routine is associated with will handle any
654 * packets that are for SCSI read requests.
655 *
656 * Returns: True for read types I/O, otherwise returns false.
Vasu Devd7179682009-07-29 17:05:21 -0700657 */
658bool fcoe_oem_match(struct fc_frame *fp)
659{
Yi Zou05cc7392009-08-25 13:59:03 -0700660 return fc_fcp_is_read(fr_fsp(fp)) &&
661 (fr_fsp(fp)->data_len > fcoe_ddp_min);
Vasu Devd7179682009-07-29 17:05:21 -0700662}
663
Vasu Dev7f349142009-03-27 09:06:31 -0700664/**
Robert Love1875f272009-11-03 11:47:50 -0800665 * fcoe_em_config() - Allocate and configure an exchange manager
666 * @lport: The local port that the new EM will be associated with
Vasu Dev7f349142009-03-27 09:06:31 -0700667 *
Robert Love1875f272009-11-03 11:47:50 -0800668 * Returns: 0 on success
Vasu Dev7f349142009-03-27 09:06:31 -0700669 */
Robert Love1875f272009-11-03 11:47:50 -0800670static inline int fcoe_em_config(struct fc_lport *lport)
Vasu Dev7f349142009-03-27 09:06:31 -0700671{
Robert Love1875f272009-11-03 11:47:50 -0800672 struct fcoe_port *port = lport_priv(lport);
Chris Leech25024982009-08-25 13:59:35 -0700673 struct fcoe_interface *fcoe = port->fcoe;
674 struct fcoe_interface *oldfcoe = NULL;
Vasu Dev1d1b88d2009-07-29 17:05:45 -0700675 struct net_device *old_real_dev, *cur_real_dev;
Vasu Devd7179682009-07-29 17:05:21 -0700676 u16 min_xid = FCOE_MIN_XID;
677 u16 max_xid = FCOE_MAX_XID;
678
679 /*
680 * Check if need to allocate an em instance for
681 * offload exchange ids to be shared across all VN_PORTs/lport.
682 */
Robert Love1875f272009-11-03 11:47:50 -0800683 if (!lport->lro_enabled || !lport->lro_xid ||
684 (lport->lro_xid >= max_xid)) {
685 lport->lro_xid = 0;
Vasu Devd7179682009-07-29 17:05:21 -0700686 goto skip_oem;
687 }
688
689 /*
690 * Reuse existing offload em instance in case
Vasu Dev1d1b88d2009-07-29 17:05:45 -0700691 * it is already allocated on real eth device
Vasu Devd7179682009-07-29 17:05:21 -0700692 */
Chris Leech25024982009-08-25 13:59:35 -0700693 if (fcoe->netdev->priv_flags & IFF_802_1Q_VLAN)
694 cur_real_dev = vlan_dev_real_dev(fcoe->netdev);
Vasu Dev1d1b88d2009-07-29 17:05:45 -0700695 else
Chris Leech25024982009-08-25 13:59:35 -0700696 cur_real_dev = fcoe->netdev;
Vasu Dev1d1b88d2009-07-29 17:05:45 -0700697
Chris Leech25024982009-08-25 13:59:35 -0700698 list_for_each_entry(oldfcoe, &fcoe_hostlist, list) {
Chris Leech25024982009-08-25 13:59:35 -0700699 if (oldfcoe->netdev->priv_flags & IFF_802_1Q_VLAN)
700 old_real_dev = vlan_dev_real_dev(oldfcoe->netdev);
Vasu Dev1d1b88d2009-07-29 17:05:45 -0700701 else
Chris Leech25024982009-08-25 13:59:35 -0700702 old_real_dev = oldfcoe->netdev;
Vasu Dev1d1b88d2009-07-29 17:05:45 -0700703
704 if (cur_real_dev == old_real_dev) {
Chris Leech991cbb62009-08-25 13:59:51 -0700705 fcoe->oem = oldfcoe->oem;
Vasu Devd7179682009-07-29 17:05:21 -0700706 break;
707 }
708 }
709
Chris Leech991cbb62009-08-25 13:59:51 -0700710 if (fcoe->oem) {
Robert Love1875f272009-11-03 11:47:50 -0800711 if (!fc_exch_mgr_add(lport, fcoe->oem, fcoe_oem_match)) {
Vasu Devd7179682009-07-29 17:05:21 -0700712 printk(KERN_ERR "fcoe_em_config: failed to add "
713 "offload em:%p on interface:%s\n",
Chris Leech991cbb62009-08-25 13:59:51 -0700714 fcoe->oem, fcoe->netdev->name);
Vasu Devd7179682009-07-29 17:05:21 -0700715 return -ENOMEM;
716 }
717 } else {
Robert Love1875f272009-11-03 11:47:50 -0800718 fcoe->oem = fc_exch_mgr_alloc(lport, FC_CLASS_3,
719 FCOE_MIN_XID, lport->lro_xid,
720 fcoe_oem_match);
Chris Leech991cbb62009-08-25 13:59:51 -0700721 if (!fcoe->oem) {
Vasu Devd7179682009-07-29 17:05:21 -0700722 printk(KERN_ERR "fcoe_em_config: failed to allocate "
723 "em for offload exches on interface:%s\n",
Chris Leech25024982009-08-25 13:59:35 -0700724 fcoe->netdev->name);
Vasu Devd7179682009-07-29 17:05:21 -0700725 return -ENOMEM;
726 }
727 }
728
729 /*
730 * Exclude offload EM xid range from next EM xid range.
731 */
Robert Love1875f272009-11-03 11:47:50 -0800732 min_xid += lport->lro_xid + 1;
Vasu Devd7179682009-07-29 17:05:21 -0700733
734skip_oem:
Robert Love1875f272009-11-03 11:47:50 -0800735 if (!fc_exch_mgr_alloc(lport, FC_CLASS_3, min_xid, max_xid, NULL)) {
Vasu Devd7179682009-07-29 17:05:21 -0700736 printk(KERN_ERR "fcoe_em_config: failed to "
Chris Leech25024982009-08-25 13:59:35 -0700737 "allocate em on interface %s\n", fcoe->netdev->name);
Vasu Dev7f349142009-03-27 09:06:31 -0700738 return -ENOMEM;
Vasu Devd7179682009-07-29 17:05:21 -0700739 }
Vasu Dev7f349142009-03-27 09:06:31 -0700740
741 return 0;
742}
743
744/**
Robert Love1875f272009-11-03 11:47:50 -0800745 * fcoe_if_destroy() - Tear down a SW FCoE instance
746 * @lport: The local port to be destroyed
Vasu Dev7f349142009-03-27 09:06:31 -0700747 */
Chris Leechaf7f85d2009-08-25 13:59:24 -0700748static void fcoe_if_destroy(struct fc_lport *lport)
Vasu Dev7f349142009-03-27 09:06:31 -0700749{
Chris Leech014f5c32009-08-25 13:59:30 -0700750 struct fcoe_port *port = lport_priv(lport);
751 struct fcoe_interface *fcoe = port->fcoe;
Chris Leech25024982009-08-25 13:59:35 -0700752 struct net_device *netdev = fcoe->netdev;
Vasu Dev7f349142009-03-27 09:06:31 -0700753
Robert Loved5488eb2009-06-10 15:30:59 -0700754 FCOE_NETDEV_DBG(netdev, "Destroying interface\n");
Vasu Dev7f349142009-03-27 09:06:31 -0700755
Vasu Dev7f349142009-03-27 09:06:31 -0700756 /* Logout of the fabric */
Chris Leechaf7f85d2009-08-25 13:59:24 -0700757 fc_fabric_logoff(lport);
Vasu Dev7f349142009-03-27 09:06:31 -0700758
Vasu Dev7f349142009-03-27 09:06:31 -0700759 /* Cleanup the fc_lport */
Chris Leechaf7f85d2009-08-25 13:59:24 -0700760 fc_lport_destroy(lport);
761 fc_fcp_destroy(lport);
Vasu Dev7f349142009-03-27 09:06:31 -0700762
Chris Leech54b649f2009-08-25 14:00:07 -0700763 /* Stop the transmit retry timer */
764 del_timer_sync(&port->timer);
765
766 /* Free existing transmit skbs */
767 fcoe_clean_pending_queue(lport);
768
Chris Leech11b56182009-11-03 11:46:29 -0800769 rtnl_lock();
770 if (!is_zero_ether_addr(port->data_src_addr))
771 dev_unicast_delete(netdev, port->data_src_addr);
772 rtnl_unlock();
773
Chris Leech54b649f2009-08-25 14:00:07 -0700774 /* receives may not be stopped until after this */
775 fcoe_interface_put(fcoe);
776
777 /* Free queued packets for the per-CPU receive threads */
778 fcoe_percpu_clean(lport);
779
Vasu Dev7f349142009-03-27 09:06:31 -0700780 /* Detach from the scsi-ml */
Chris Leechaf7f85d2009-08-25 13:59:24 -0700781 fc_remove_host(lport->host);
782 scsi_remove_host(lport->host);
Vasu Dev7f349142009-03-27 09:06:31 -0700783
784 /* There are no more rports or I/O, free the EM */
Chris Leechaf7f85d2009-08-25 13:59:24 -0700785 fc_exch_mgr_free(lport);
Vasu Dev7f349142009-03-27 09:06:31 -0700786
Vasu Dev7f349142009-03-27 09:06:31 -0700787 /* Free memory used by statistical counters */
Chris Leechaf7f85d2009-08-25 13:59:24 -0700788 fc_lport_free_stats(lport);
Vasu Dev7f349142009-03-27 09:06:31 -0700789
Chris Leech2e70e242009-08-25 14:00:23 -0700790 /* Release the Scsi_Host */
Chris Leechaf7f85d2009-08-25 13:59:24 -0700791 scsi_host_put(lport->host);
Vasu Dev7f349142009-03-27 09:06:31 -0700792}
793
Robert Love1875f272009-11-03 11:47:50 -0800794/**
795 * fcoe_ddp_setup() - Call a LLD's ddp_setup through the net device
796 * @lport: The local port to setup DDP for
797 * @xid: The exchange ID for this DDP transfer
798 * @sgl: The scatterlist describing this transfer
799 * @sgc: The number of sg items
Vasu Dev7f349142009-03-27 09:06:31 -0700800 *
Robert Love1875f272009-11-03 11:47:50 -0800801 * Returns: 0 if the DDP context was not configured
Vasu Dev7f349142009-03-27 09:06:31 -0700802 */
Robert Love1875f272009-11-03 11:47:50 -0800803static int fcoe_ddp_setup(struct fc_lport *lport, u16 xid,
804 struct scatterlist *sgl, unsigned int sgc)
Vasu Dev7f349142009-03-27 09:06:31 -0700805{
Robert Love1875f272009-11-03 11:47:50 -0800806 struct net_device *netdev = fcoe_netdev(lport);
Vasu Dev7f349142009-03-27 09:06:31 -0700807
Robert Love1875f272009-11-03 11:47:50 -0800808 if (netdev->netdev_ops->ndo_fcoe_ddp_setup)
809 return netdev->netdev_ops->ndo_fcoe_ddp_setup(netdev,
810 xid, sgl,
811 sgc);
Vasu Dev7f349142009-03-27 09:06:31 -0700812
813 return 0;
814}
815
Vasu Dev7f349142009-03-27 09:06:31 -0700816/**
Robert Love1875f272009-11-03 11:47:50 -0800817 * fcoe_ddp_done() - Call a LLD's ddp_done through the net device
818 * @lport: The local port to complete DDP on
819 * @xid: The exchange ID for this DDP transfer
Vasu Dev7f349142009-03-27 09:06:31 -0700820 *
Robert Love1875f272009-11-03 11:47:50 -0800821 * Returns: the length of data that have been completed by DDP
822 */
823static int fcoe_ddp_done(struct fc_lport *lport, u16 xid)
824{
825 struct net_device *netdev = fcoe_netdev(lport);
826
827 if (netdev->netdev_ops->ndo_fcoe_ddp_done)
828 return netdev->netdev_ops->ndo_fcoe_ddp_done(netdev, xid);
829 return 0;
830}
831
832/**
833 * fcoe_if_create() - Create a FCoE instance on an interface
834 * @fcoe: The FCoE interface to create a local port on
835 * @parent: The device pointer to be the parent in sysfs for the SCSI host
836 * @npiv: Indicates if the port is a vport or not
Vasu Dev7f349142009-03-27 09:06:31 -0700837 *
Robert Love1875f272009-11-03 11:47:50 -0800838 * Creates a fc_lport instance and a Scsi_Host instance and configure them.
839 *
840 * Returns: The allocated fc_lport or an error pointer
Vasu Dev7f349142009-03-27 09:06:31 -0700841 */
Chris Leech030f4e02009-08-25 14:00:02 -0700842static struct fc_lport *fcoe_if_create(struct fcoe_interface *fcoe,
Chris Leech9a057532009-11-03 11:46:40 -0800843 struct device *parent, int npiv)
Vasu Dev7f349142009-03-27 09:06:31 -0700844{
Robert Love1875f272009-11-03 11:47:50 -0800845 struct net_device *netdev = fcoe->netdev;
Chris Leechaf7f85d2009-08-25 13:59:24 -0700846 struct fc_lport *lport = NULL;
Chris Leech014f5c32009-08-25 13:59:30 -0700847 struct fcoe_port *port;
Vasu Dev7f349142009-03-27 09:06:31 -0700848 struct Scsi_Host *shost;
Robert Love1875f272009-11-03 11:47:50 -0800849 int rc;
Chris Leech9a057532009-11-03 11:46:40 -0800850 /*
851 * parent is only a vport if npiv is 1,
852 * but we'll only use vport in that case so go ahead and set it
853 */
854 struct fc_vport *vport = dev_to_vport(parent);
Vasu Dev7f349142009-03-27 09:06:31 -0700855
Robert Loved5488eb2009-06-10 15:30:59 -0700856 FCOE_NETDEV_DBG(netdev, "Create Interface\n");
Vasu Dev7f349142009-03-27 09:06:31 -0700857
Chris Leech9a057532009-11-03 11:46:40 -0800858 if (!npiv) {
859 lport = libfc_host_alloc(&fcoe_shost_template,
860 sizeof(struct fcoe_port));
861 } else {
862 lport = libfc_vport_create(vport,
863 sizeof(struct fcoe_port));
864 }
Chris Leech86221962009-11-03 11:46:08 -0800865 if (!lport) {
Chris Leech014f5c32009-08-25 13:59:30 -0700866 FCOE_NETDEV_DBG(netdev, "Could not allocate host structure\n");
867 rc = -ENOMEM;
Chris Leech030f4e02009-08-25 14:00:02 -0700868 goto out;
Chris Leech014f5c32009-08-25 13:59:30 -0700869 }
Chris Leech86221962009-11-03 11:46:08 -0800870 shost = lport->host;
Chris Leech014f5c32009-08-25 13:59:30 -0700871 port = lport_priv(lport);
Chris Leech2e70e242009-08-25 14:00:23 -0700872 port->lport = lport;
Chris Leech014f5c32009-08-25 13:59:30 -0700873 port->fcoe = fcoe;
Chris Leech2e70e242009-08-25 14:00:23 -0700874 INIT_WORK(&port->destroy_work, fcoe_destroy_work);
Vasu Dev7f349142009-03-27 09:06:31 -0700875
Robert Love1875f272009-11-03 11:47:50 -0800876 /* configure a fc_lport including the exchange manager */
Chris Leechaf7f85d2009-08-25 13:59:24 -0700877 rc = fcoe_lport_config(lport);
Vasu Dev7f349142009-03-27 09:06:31 -0700878 if (rc) {
Robert Loved5488eb2009-06-10 15:30:59 -0700879 FCOE_NETDEV_DBG(netdev, "Could not configure lport for the "
880 "interface\n");
Vasu Dev7f349142009-03-27 09:06:31 -0700881 goto out_host_put;
882 }
883
Chris Leech9a057532009-11-03 11:46:40 -0800884 if (npiv) {
885 FCOE_NETDEV_DBG(netdev, "Setting vport names, 0x%llX 0x%llX\n",
Robert Love1875f272009-11-03 11:47:50 -0800886 vport->node_name, vport->port_name);
Chris Leech9a057532009-11-03 11:46:40 -0800887 fc_set_wwnn(lport, vport->node_name);
888 fc_set_wwpn(lport, vport->port_name);
889 }
890
Vasu Devab6b85c2009-05-17 12:33:08 +0000891 /* configure lport network properties */
Chris Leechaf7f85d2009-08-25 13:59:24 -0700892 rc = fcoe_netdev_config(lport, netdev);
Vasu Devab6b85c2009-05-17 12:33:08 +0000893 if (rc) {
Robert Loved5488eb2009-06-10 15:30:59 -0700894 FCOE_NETDEV_DBG(netdev, "Could not configure netdev for the "
895 "interface\n");
Chris Leech54b649f2009-08-25 14:00:07 -0700896 goto out_lp_destroy;
Vasu Devab6b85c2009-05-17 12:33:08 +0000897 }
Joe Eykholt97c83892009-03-17 11:42:40 -0700898
Vasu Dev7f349142009-03-27 09:06:31 -0700899 /* configure lport scsi host properties */
Chris Leechaf7f85d2009-08-25 13:59:24 -0700900 rc = fcoe_shost_config(lport, shost, parent);
Vasu Dev7f349142009-03-27 09:06:31 -0700901 if (rc) {
Robert Loved5488eb2009-06-10 15:30:59 -0700902 FCOE_NETDEV_DBG(netdev, "Could not configure shost for the "
903 "interface\n");
Chris Leech54b649f2009-08-25 14:00:07 -0700904 goto out_lp_destroy;
Vasu Dev7f349142009-03-27 09:06:31 -0700905 }
906
Vasu Dev7f349142009-03-27 09:06:31 -0700907 /* Initialize the library */
Chris Leechaf7f85d2009-08-25 13:59:24 -0700908 rc = fcoe_libfc_config(lport, &fcoe_libfc_fcn_templ);
Vasu Dev7f349142009-03-27 09:06:31 -0700909 if (rc) {
Robert Loved5488eb2009-06-10 15:30:59 -0700910 FCOE_NETDEV_DBG(netdev, "Could not configure libfc for the "
911 "interface\n");
Vasu Dev7f349142009-03-27 09:06:31 -0700912 goto out_lp_destroy;
913 }
914
Chris Leech9a057532009-11-03 11:46:40 -0800915 if (!npiv) {
916 /*
917 * fcoe_em_alloc() and fcoe_hostlist_add() both
918 * need to be atomic with respect to other changes to the
919 * hostlist since fcoe_em_alloc() looks for an existing EM
920 * instance on host list updated by fcoe_hostlist_add().
921 *
922 * This is currently handled through the fcoe_config_mutex
923 * begin held.
924 */
Chris Leechc863df32009-08-25 14:00:18 -0700925
Chris Leech9a057532009-11-03 11:46:40 -0800926 /* lport exch manager allocation */
927 rc = fcoe_em_config(lport);
928 if (rc) {
929 FCOE_NETDEV_DBG(netdev, "Could not configure the EM "
Robert Love1875f272009-11-03 11:47:50 -0800930 "for the interface\n");
Chris Leech9a057532009-11-03 11:46:40 -0800931 goto out_lp_destroy;
932 }
Vasu Dev96316092009-07-29 17:05:00 -0700933 }
934
Chris Leech030f4e02009-08-25 14:00:02 -0700935 fcoe_interface_get(fcoe);
Chris Leechaf7f85d2009-08-25 13:59:24 -0700936 return lport;
Vasu Dev7f349142009-03-27 09:06:31 -0700937
938out_lp_destroy:
Chris Leechaf7f85d2009-08-25 13:59:24 -0700939 fc_exch_mgr_free(lport);
Vasu Dev7f349142009-03-27 09:06:31 -0700940out_host_put:
Chris Leechaf7f85d2009-08-25 13:59:24 -0700941 scsi_host_put(lport->host);
942out:
943 return ERR_PTR(rc);
Vasu Dev7f349142009-03-27 09:06:31 -0700944}
945
946/**
Robert Love1875f272009-11-03 11:47:50 -0800947 * fcoe_if_init() - Initialization routine for fcoe.ko
Vasu Dev7f349142009-03-27 09:06:31 -0700948 *
Robert Love1875f272009-11-03 11:47:50 -0800949 * Attaches the SW FCoE transport to the FC transport
950 *
951 * Returns: 0 on success
Vasu Dev7f349142009-03-27 09:06:31 -0700952 */
953static int __init fcoe_if_init(void)
954{
955 /* attach to scsi transport */
Chris Leeche9084bb2009-11-03 11:46:34 -0800956 fcoe_transport_template = fc_attach_transport(&fcoe_transport_function);
957 fcoe_vport_transport_template =
958 fc_attach_transport(&fcoe_vport_transport_function);
Vasu Dev7f349142009-03-27 09:06:31 -0700959
Chris Leeche9084bb2009-11-03 11:46:34 -0800960 if (!fcoe_transport_template) {
Robert Loved5488eb2009-06-10 15:30:59 -0700961 printk(KERN_ERR "fcoe: Failed to attach to the FC transport\n");
Vasu Dev7f349142009-03-27 09:06:31 -0700962 return -ENODEV;
963 }
964
965 return 0;
966}
967
968/**
Robert Love1875f272009-11-03 11:47:50 -0800969 * fcoe_if_exit() - Tear down fcoe.ko
Vasu Dev7f349142009-03-27 09:06:31 -0700970 *
Robert Love1875f272009-11-03 11:47:50 -0800971 * Detaches the SW FCoE transport from the FC transport
972 *
973 * Returns: 0 on success
Vasu Dev7f349142009-03-27 09:06:31 -0700974 */
975int __exit fcoe_if_exit(void)
976{
Chris Leeche9084bb2009-11-03 11:46:34 -0800977 fc_release_transport(fcoe_transport_template);
978 fc_release_transport(fcoe_vport_transport_template);
979 fcoe_transport_template = NULL;
980 fcoe_vport_transport_template = NULL;
Vasu Dev7f349142009-03-27 09:06:31 -0700981 return 0;
982}
983
Robert Love85b4aa42008-12-09 15:10:24 -0800984/**
Robert Love1875f272009-11-03 11:47:50 -0800985 * fcoe_percpu_thread_create() - Create a receive thread for an online CPU
986 * @cpu: The CPU index of the CPU to create a receive thread for
Robert Love8976f422009-03-17 11:41:46 -0700987 */
988static void fcoe_percpu_thread_create(unsigned int cpu)
989{
990 struct fcoe_percpu_s *p;
991 struct task_struct *thread;
992
993 p = &per_cpu(fcoe_percpu, cpu);
994
995 thread = kthread_create(fcoe_percpu_receive_thread,
996 (void *)p, "fcoethread/%d", cpu);
997
Joe Eykholte7a51992009-08-25 14:04:08 -0700998 if (likely(!IS_ERR(thread))) {
Robert Love8976f422009-03-17 11:41:46 -0700999 kthread_bind(thread, cpu);
1000 wake_up_process(thread);
1001
1002 spin_lock_bh(&p->fcoe_rx_list.lock);
1003 p->thread = thread;
1004 spin_unlock_bh(&p->fcoe_rx_list.lock);
1005 }
1006}
1007
1008/**
Robert Love1875f272009-11-03 11:47:50 -08001009 * fcoe_percpu_thread_destroy() - Remove the receive thread of a CPU
1010 * @cpu: The CPU index of the CPU whose receive thread is to be destroyed
Robert Love8976f422009-03-17 11:41:46 -07001011 *
1012 * Destroys a per-CPU Rx thread. Any pending skbs are moved to the
1013 * current CPU's Rx thread. If the thread being destroyed is bound to
1014 * the CPU processing this context the skbs will be freed.
1015 */
1016static void fcoe_percpu_thread_destroy(unsigned int cpu)
1017{
1018 struct fcoe_percpu_s *p;
1019 struct task_struct *thread;
1020 struct page *crc_eof;
1021 struct sk_buff *skb;
1022#ifdef CONFIG_SMP
1023 struct fcoe_percpu_s *p0;
1024 unsigned targ_cpu = smp_processor_id();
1025#endif /* CONFIG_SMP */
1026
Robert Loved5488eb2009-06-10 15:30:59 -07001027 FCOE_DBG("Destroying receive thread for CPU %d\n", cpu);
Robert Love8976f422009-03-17 11:41:46 -07001028
1029 /* Prevent any new skbs from being queued for this CPU. */
1030 p = &per_cpu(fcoe_percpu, cpu);
1031 spin_lock_bh(&p->fcoe_rx_list.lock);
1032 thread = p->thread;
1033 p->thread = NULL;
1034 crc_eof = p->crc_eof_page;
1035 p->crc_eof_page = NULL;
1036 p->crc_eof_offset = 0;
1037 spin_unlock_bh(&p->fcoe_rx_list.lock);
1038
1039#ifdef CONFIG_SMP
1040 /*
1041 * Don't bother moving the skb's if this context is running
1042 * on the same CPU that is having its thread destroyed. This
1043 * can easily happen when the module is removed.
1044 */
1045 if (cpu != targ_cpu) {
1046 p0 = &per_cpu(fcoe_percpu, targ_cpu);
1047 spin_lock_bh(&p0->fcoe_rx_list.lock);
1048 if (p0->thread) {
Robert Loved5488eb2009-06-10 15:30:59 -07001049 FCOE_DBG("Moving frames from CPU %d to CPU %d\n",
1050 cpu, targ_cpu);
Robert Love8976f422009-03-17 11:41:46 -07001051
1052 while ((skb = __skb_dequeue(&p->fcoe_rx_list)) != NULL)
1053 __skb_queue_tail(&p0->fcoe_rx_list, skb);
1054 spin_unlock_bh(&p0->fcoe_rx_list.lock);
1055 } else {
1056 /*
1057 * The targeted CPU is not initialized and cannot accept
Robert Love1875f272009-11-03 11:47:50 -08001058 * new skbs. Unlock the targeted CPU and drop the skbs
Robert Love8976f422009-03-17 11:41:46 -07001059 * on the CPU that is going offline.
1060 */
1061 while ((skb = __skb_dequeue(&p->fcoe_rx_list)) != NULL)
1062 kfree_skb(skb);
1063 spin_unlock_bh(&p0->fcoe_rx_list.lock);
1064 }
1065 } else {
1066 /*
1067 * This scenario occurs when the module is being removed
1068 * and all threads are being destroyed. skbs will continue
1069 * to be shifted from the CPU thread that is being removed
1070 * to the CPU thread associated with the CPU that is processing
1071 * the module removal. Once there is only one CPU Rx thread it
1072 * will reach this case and we will drop all skbs and later
1073 * stop the thread.
1074 */
1075 spin_lock_bh(&p->fcoe_rx_list.lock);
1076 while ((skb = __skb_dequeue(&p->fcoe_rx_list)) != NULL)
1077 kfree_skb(skb);
1078 spin_unlock_bh(&p->fcoe_rx_list.lock);
1079 }
1080#else
1081 /*
Chris Leechdd3fd722009-04-21 16:27:36 -07001082 * This a non-SMP scenario where the singular Rx thread is
Robert Love8976f422009-03-17 11:41:46 -07001083 * being removed. Free all skbs and stop the thread.
1084 */
1085 spin_lock_bh(&p->fcoe_rx_list.lock);
1086 while ((skb = __skb_dequeue(&p->fcoe_rx_list)) != NULL)
1087 kfree_skb(skb);
1088 spin_unlock_bh(&p->fcoe_rx_list.lock);
1089#endif
1090
1091 if (thread)
1092 kthread_stop(thread);
1093
1094 if (crc_eof)
1095 put_page(crc_eof);
1096}
1097
1098/**
Robert Love1875f272009-11-03 11:47:50 -08001099 * fcoe_cpu_callback() - Handler for CPU hotplug events
1100 * @nfb: The callback data block
1101 * @action: The event triggering the callback
1102 * @hcpu: The index of the CPU that the event is for
Robert Love8976f422009-03-17 11:41:46 -07001103 *
Robert Love1875f272009-11-03 11:47:50 -08001104 * This creates or destroys per-CPU data for fcoe
Robert Love8976f422009-03-17 11:41:46 -07001105 *
1106 * Returns NOTIFY_OK always.
1107 */
1108static int fcoe_cpu_callback(struct notifier_block *nfb,
1109 unsigned long action, void *hcpu)
1110{
1111 unsigned cpu = (unsigned long)hcpu;
1112
1113 switch (action) {
1114 case CPU_ONLINE:
1115 case CPU_ONLINE_FROZEN:
Robert Loved5488eb2009-06-10 15:30:59 -07001116 FCOE_DBG("CPU %x online: Create Rx thread\n", cpu);
Robert Love8976f422009-03-17 11:41:46 -07001117 fcoe_percpu_thread_create(cpu);
1118 break;
1119 case CPU_DEAD:
1120 case CPU_DEAD_FROZEN:
Robert Loved5488eb2009-06-10 15:30:59 -07001121 FCOE_DBG("CPU %x offline: Remove Rx thread\n", cpu);
Robert Love8976f422009-03-17 11:41:46 -07001122 fcoe_percpu_thread_destroy(cpu);
1123 break;
1124 default:
1125 break;
1126 }
1127 return NOTIFY_OK;
1128}
1129
Robert Love8976f422009-03-17 11:41:46 -07001130/**
Robert Love1875f272009-11-03 11:47:50 -08001131 * fcoe_rcv() - Receive packets from a net device
1132 * @skb: The received packet
1133 * @netdev: The net device that the packet was received on
1134 * @ptype: The packet type context
1135 * @olddev: The last device net device
Robert Love85b4aa42008-12-09 15:10:24 -08001136 *
Robert Love1875f272009-11-03 11:47:50 -08001137 * This routine is called by NET_RX_SOFTIRQ. It receives a packet, builds a
1138 * FC frame and passes the frame to libfc.
Robert Love85b4aa42008-12-09 15:10:24 -08001139 *
1140 * Returns: 0 for success
Robert Love34f42a02009-02-27 10:55:45 -08001141 */
Robert Love1875f272009-11-03 11:47:50 -08001142int fcoe_rcv(struct sk_buff *skb, struct net_device *netdev,
Robert Love85b4aa42008-12-09 15:10:24 -08001143 struct packet_type *ptype, struct net_device *olddev)
1144{
Robert Love1875f272009-11-03 11:47:50 -08001145 struct fc_lport *lport;
Robert Love85b4aa42008-12-09 15:10:24 -08001146 struct fcoe_rcv_info *fr;
Chris Leech259ad852009-08-25 13:59:41 -07001147 struct fcoe_interface *fcoe;
Robert Love85b4aa42008-12-09 15:10:24 -08001148 struct fc_frame_header *fh;
Robert Love85b4aa42008-12-09 15:10:24 -08001149 struct fcoe_percpu_s *fps;
Vasu Devb2f00912009-08-25 13:58:53 -07001150 unsigned int cpu;
Robert Love85b4aa42008-12-09 15:10:24 -08001151
Chris Leech259ad852009-08-25 13:59:41 -07001152 fcoe = container_of(ptype, struct fcoe_interface, fcoe_packet_type);
Robert Love1875f272009-11-03 11:47:50 -08001153 lport = fcoe->ctlr.lp;
1154 if (unlikely(!lport)) {
1155 FCOE_NETDEV_DBG(netdev, "Cannot find hba structure");
Robert Love85b4aa42008-12-09 15:10:24 -08001156 goto err2;
1157 }
Robert Love1875f272009-11-03 11:47:50 -08001158 if (!lport->link_up)
Joe Eykholt97c83892009-03-17 11:42:40 -07001159 goto err2;
Robert Love85b4aa42008-12-09 15:10:24 -08001160
Robert Love1875f272009-11-03 11:47:50 -08001161 FCOE_NETDEV_DBG(netdev, "skb_info: len:%d data_len:%d head:%p "
Robert Loved5488eb2009-06-10 15:30:59 -07001162 "data:%p tail:%p end:%p sum:%d dev:%s",
1163 skb->len, skb->data_len, skb->head, skb->data,
1164 skb_tail_pointer(skb), skb_end_pointer(skb),
1165 skb->csum, skb->dev ? skb->dev->name : "<NULL>");
Robert Love85b4aa42008-12-09 15:10:24 -08001166
1167 /* check for FCOE packet type */
1168 if (unlikely(eth_hdr(skb)->h_proto != htons(ETH_P_FCOE))) {
Robert Love1875f272009-11-03 11:47:50 -08001169 FCOE_NETDEV_DBG(netdev, "Wrong FC type frame");
Robert Love85b4aa42008-12-09 15:10:24 -08001170 goto err;
1171 }
1172
1173 /*
1174 * Check for minimum frame length, and make sure required FCoE
1175 * and FC headers are pulled into the linear data area.
1176 */
1177 if (unlikely((skb->len < FCOE_MIN_FRAME) ||
Robert Love1875f272009-11-03 11:47:50 -08001178 !pskb_may_pull(skb, FCOE_HEADER_LEN)))
Robert Love85b4aa42008-12-09 15:10:24 -08001179 goto err;
1180
1181 skb_set_transport_header(skb, sizeof(struct fcoe_hdr));
1182 fh = (struct fc_frame_header *) skb_transport_header(skb);
1183
Robert Love85b4aa42008-12-09 15:10:24 -08001184 fr = fcoe_dev_from_skb(skb);
Robert Love1875f272009-11-03 11:47:50 -08001185 fr->fr_dev = lport;
Robert Love85b4aa42008-12-09 15:10:24 -08001186 fr->ptype = ptype;
Robert Love5e5e92d2009-03-17 11:41:35 -07001187
Robert Love85b4aa42008-12-09 15:10:24 -08001188 /*
Vasu Devb2f00912009-08-25 13:58:53 -07001189 * In case the incoming frame's exchange is originated from
1190 * the initiator, then received frame's exchange id is ANDed
1191 * with fc_cpu_mask bits to get the same cpu on which exchange
1192 * was originated, otherwise just use the current cpu.
Robert Love85b4aa42008-12-09 15:10:24 -08001193 */
Vasu Devb2f00912009-08-25 13:58:53 -07001194 if (ntoh24(fh->fh_f_ctl) & FC_FC_EX_CTX)
1195 cpu = ntohs(fh->fh_ox_id) & fc_cpu_mask;
1196 else
1197 cpu = smp_processor_id();
Robert Love5e5e92d2009-03-17 11:41:35 -07001198
Robert Love8976f422009-03-17 11:41:46 -07001199 fps = &per_cpu(fcoe_percpu, cpu);
Robert Love85b4aa42008-12-09 15:10:24 -08001200 spin_lock_bh(&fps->fcoe_rx_list.lock);
Robert Love8976f422009-03-17 11:41:46 -07001201 if (unlikely(!fps->thread)) {
1202 /*
1203 * The targeted CPU is not ready, let's target
1204 * the first CPU now. For non-SMP systems this
1205 * will check the same CPU twice.
1206 */
Robert Love1875f272009-11-03 11:47:50 -08001207 FCOE_NETDEV_DBG(netdev, "CPU is online, but no receive thread "
Robert Loved5488eb2009-06-10 15:30:59 -07001208 "ready for incoming skb- using first online "
1209 "CPU.\n");
Robert Love8976f422009-03-17 11:41:46 -07001210
1211 spin_unlock_bh(&fps->fcoe_rx_list.lock);
1212 cpu = first_cpu(cpu_online_map);
1213 fps = &per_cpu(fcoe_percpu, cpu);
1214 spin_lock_bh(&fps->fcoe_rx_list.lock);
1215 if (!fps->thread) {
1216 spin_unlock_bh(&fps->fcoe_rx_list.lock);
1217 goto err;
1218 }
1219 }
1220
1221 /*
1222 * We now have a valid CPU that we're targeting for
1223 * this skb. We also have this receive thread locked,
1224 * so we're free to queue skbs into it's queue.
1225 */
Robert Love85b4aa42008-12-09 15:10:24 -08001226 __skb_queue_tail(&fps->fcoe_rx_list, skb);
1227 if (fps->fcoe_rx_list.qlen == 1)
1228 wake_up_process(fps->thread);
1229
1230 spin_unlock_bh(&fps->fcoe_rx_list.lock);
1231
1232 return 0;
1233err:
Robert Love1875f272009-11-03 11:47:50 -08001234 fc_lport_get_stats(lport)->ErrorFrames++;
Robert Love85b4aa42008-12-09 15:10:24 -08001235
1236err2:
1237 kfree_skb(skb);
1238 return -1;
1239}
Robert Love85b4aa42008-12-09 15:10:24 -08001240
1241/**
Robert Love1875f272009-11-03 11:47:50 -08001242 * fcoe_start_io() - Start FCoE I/O
1243 * @skb: The packet to be transmitted
1244 *
1245 * This routine is called from the net device to start transmitting
1246 * FCoE packets.
Robert Love85b4aa42008-12-09 15:10:24 -08001247 *
1248 * Returns: 0 for success
Robert Love34f42a02009-02-27 10:55:45 -08001249 */
Robert Love85b4aa42008-12-09 15:10:24 -08001250static inline int fcoe_start_io(struct sk_buff *skb)
1251{
1252 int rc;
1253
1254 skb_get(skb);
1255 rc = dev_queue_xmit(skb);
1256 if (rc != 0)
1257 return rc;
1258 kfree_skb(skb);
1259 return 0;
1260}
1261
1262/**
Robert Love1875f272009-11-03 11:47:50 -08001263 * fcoe_get_paged_crc_eof() - Allocate a page to be used for the trailer CRC
1264 * @skb: The packet to be transmitted
1265 * @tlen: The total length of the trailer
1266 *
1267 * This routine allocates a page for frame trailers. The page is re-used if
1268 * there is enough room left on it for the current trailer. If there isn't
1269 * enough buffer left a new page is allocated for the trailer. Reference to
1270 * the page from this function as well as the skbs using the page fragments
1271 * ensure that the page is freed at the appropriate time.
Robert Love85b4aa42008-12-09 15:10:24 -08001272 *
1273 * Returns: 0 for success
Robert Love34f42a02009-02-27 10:55:45 -08001274 */
Robert Love85b4aa42008-12-09 15:10:24 -08001275static int fcoe_get_paged_crc_eof(struct sk_buff *skb, int tlen)
1276{
1277 struct fcoe_percpu_s *fps;
1278 struct page *page;
Robert Love85b4aa42008-12-09 15:10:24 -08001279
Robert Love5e5e92d2009-03-17 11:41:35 -07001280 fps = &get_cpu_var(fcoe_percpu);
Robert Love85b4aa42008-12-09 15:10:24 -08001281 page = fps->crc_eof_page;
1282 if (!page) {
1283 page = alloc_page(GFP_ATOMIC);
1284 if (!page) {
Robert Love5e5e92d2009-03-17 11:41:35 -07001285 put_cpu_var(fcoe_percpu);
Robert Love85b4aa42008-12-09 15:10:24 -08001286 return -ENOMEM;
1287 }
1288 fps->crc_eof_page = page;
Robert Love8976f422009-03-17 11:41:46 -07001289 fps->crc_eof_offset = 0;
Robert Love85b4aa42008-12-09 15:10:24 -08001290 }
1291
1292 get_page(page);
1293 skb_fill_page_desc(skb, skb_shinfo(skb)->nr_frags, page,
1294 fps->crc_eof_offset, tlen);
1295 skb->len += tlen;
1296 skb->data_len += tlen;
1297 skb->truesize += tlen;
1298 fps->crc_eof_offset += sizeof(struct fcoe_crc_eof);
1299
1300 if (fps->crc_eof_offset >= PAGE_SIZE) {
1301 fps->crc_eof_page = NULL;
1302 fps->crc_eof_offset = 0;
1303 put_page(page);
1304 }
Robert Love5e5e92d2009-03-17 11:41:35 -07001305 put_cpu_var(fcoe_percpu);
Robert Love85b4aa42008-12-09 15:10:24 -08001306 return 0;
1307}
1308
1309/**
Robert Love1875f272009-11-03 11:47:50 -08001310 * fcoe_fc_crc() - Calculates the CRC for a given frame
1311 * @fp: The frame to be checksumed
Robert Love85b4aa42008-12-09 15:10:24 -08001312 *
Robert Love1875f272009-11-03 11:47:50 -08001313 * This uses crc32() routine to calculate the CRC for a frame
1314 *
1315 * Return: The 32 bit CRC value
Robert Love34f42a02009-02-27 10:55:45 -08001316 */
Robert Love85b4aa42008-12-09 15:10:24 -08001317u32 fcoe_fc_crc(struct fc_frame *fp)
1318{
1319 struct sk_buff *skb = fp_skb(fp);
1320 struct skb_frag_struct *frag;
1321 unsigned char *data;
1322 unsigned long off, len, clen;
1323 u32 crc;
1324 unsigned i;
1325
1326 crc = crc32(~0, skb->data, skb_headlen(skb));
1327
1328 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
1329 frag = &skb_shinfo(skb)->frags[i];
1330 off = frag->page_offset;
1331 len = frag->size;
1332 while (len > 0) {
1333 clen = min(len, PAGE_SIZE - (off & ~PAGE_MASK));
1334 data = kmap_atomic(frag->page + (off >> PAGE_SHIFT),
1335 KM_SKB_DATA_SOFTIRQ);
1336 crc = crc32(crc, data + (off & ~PAGE_MASK), clen);
1337 kunmap_atomic(data, KM_SKB_DATA_SOFTIRQ);
1338 off += clen;
1339 len -= clen;
1340 }
1341 }
1342 return crc;
1343}
Robert Love85b4aa42008-12-09 15:10:24 -08001344
1345/**
Robert Love1875f272009-11-03 11:47:50 -08001346 * fcoe_xmit() - Transmit a FCoE frame
1347 * @lport: The local port that the frame is to be transmitted for
1348 * @fp: The frame to be transmitted
Robert Love85b4aa42008-12-09 15:10:24 -08001349 *
Robert Love1875f272009-11-03 11:47:50 -08001350 * Return: 0 for success
Robert Love34f42a02009-02-27 10:55:45 -08001351 */
Robert Love1875f272009-11-03 11:47:50 -08001352int fcoe_xmit(struct fc_lport *lport, struct fc_frame *fp)
Robert Love85b4aa42008-12-09 15:10:24 -08001353{
Vasu Dev4bb6b512009-05-06 10:52:34 -07001354 int wlen;
Robert Love85b4aa42008-12-09 15:10:24 -08001355 u32 crc;
1356 struct ethhdr *eh;
1357 struct fcoe_crc_eof *cp;
1358 struct sk_buff *skb;
1359 struct fcoe_dev_stats *stats;
1360 struct fc_frame_header *fh;
1361 unsigned int hlen; /* header length implies the version */
1362 unsigned int tlen; /* trailer length */
1363 unsigned int elen; /* eth header, may include vlan */
Robert Love1875f272009-11-03 11:47:50 -08001364 struct fcoe_port *port = lport_priv(lport);
Chris Leech3fe9a0b2009-08-25 13:59:46 -07001365 struct fcoe_interface *fcoe = port->fcoe;
Robert Love85b4aa42008-12-09 15:10:24 -08001366 u8 sof, eof;
1367 struct fcoe_hdr *hp;
1368
1369 WARN_ON((fr_len(fp) % sizeof(u32)) != 0);
1370
Robert Love85b4aa42008-12-09 15:10:24 -08001371 fh = fc_frame_header_get(fp);
Joe Eykholt97c83892009-03-17 11:42:40 -07001372 skb = fp_skb(fp);
1373 wlen = skb->len / FCOE_WORD_TO_BYTE;
1374
Robert Love1875f272009-11-03 11:47:50 -08001375 if (!lport->link_up) {
Dan Carpenter3caf02e2009-04-21 16:27:25 -07001376 kfree_skb(skb);
Joe Eykholt97c83892009-03-17 11:42:40 -07001377 return 0;
Robert Love85b4aa42008-12-09 15:10:24 -08001378 }
1379
Joe Eykholt97c83892009-03-17 11:42:40 -07001380 if (unlikely(fh->fh_r_ctl == FC_RCTL_ELS_REQ) &&
Robert Love1875f272009-11-03 11:47:50 -08001381 fcoe_ctlr_els_send(&fcoe->ctlr, lport, skb))
Joe Eykholt97c83892009-03-17 11:42:40 -07001382 return 0;
1383
Robert Love85b4aa42008-12-09 15:10:24 -08001384 sof = fr_sof(fp);
1385 eof = fr_eof(fp);
1386
Vasu Dev4e57e1c2009-05-06 10:52:46 -07001387 elen = sizeof(struct ethhdr);
Robert Love85b4aa42008-12-09 15:10:24 -08001388 hlen = sizeof(struct fcoe_hdr);
1389 tlen = sizeof(struct fcoe_crc_eof);
1390 wlen = (skb->len - tlen + sizeof(crc)) / FCOE_WORD_TO_BYTE;
1391
1392 /* crc offload */
Robert Love1875f272009-11-03 11:47:50 -08001393 if (likely(lport->crc_offload)) {
Yi Zou39ca9a02009-02-27 14:07:15 -08001394 skb->ip_summed = CHECKSUM_PARTIAL;
Robert Love85b4aa42008-12-09 15:10:24 -08001395 skb->csum_start = skb_headroom(skb);
1396 skb->csum_offset = skb->len;
1397 crc = 0;
1398 } else {
1399 skb->ip_summed = CHECKSUM_NONE;
1400 crc = fcoe_fc_crc(fp);
1401 }
1402
Chris Leech014f5c32009-08-25 13:59:30 -07001403 /* copy port crc and eof to the skb buff */
Robert Love85b4aa42008-12-09 15:10:24 -08001404 if (skb_is_nonlinear(skb)) {
1405 skb_frag_t *frag;
1406 if (fcoe_get_paged_crc_eof(skb, tlen)) {
Roel Kluine9041582009-02-27 10:56:22 -08001407 kfree_skb(skb);
Robert Love85b4aa42008-12-09 15:10:24 -08001408 return -ENOMEM;
1409 }
1410 frag = &skb_shinfo(skb)->frags[skb_shinfo(skb)->nr_frags - 1];
1411 cp = kmap_atomic(frag->page, KM_SKB_DATA_SOFTIRQ)
1412 + frag->page_offset;
1413 } else {
1414 cp = (struct fcoe_crc_eof *)skb_put(skb, tlen);
1415 }
1416
1417 memset(cp, 0, sizeof(*cp));
1418 cp->fcoe_eof = eof;
1419 cp->fcoe_crc32 = cpu_to_le32(~crc);
1420
1421 if (skb_is_nonlinear(skb)) {
1422 kunmap_atomic(cp, KM_SKB_DATA_SOFTIRQ);
1423 cp = NULL;
1424 }
1425
Chris Leech014f5c32009-08-25 13:59:30 -07001426 /* adjust skb network/transport offsets to match mac/fcoe/port */
Robert Love85b4aa42008-12-09 15:10:24 -08001427 skb_push(skb, elen + hlen);
1428 skb_reset_mac_header(skb);
1429 skb_reset_network_header(skb);
1430 skb->mac_len = elen;
Yi Zou211c7382009-02-27 14:06:37 -08001431 skb->protocol = htons(ETH_P_FCOE);
Chris Leech3fe9a0b2009-08-25 13:59:46 -07001432 skb->dev = fcoe->netdev;
Robert Love85b4aa42008-12-09 15:10:24 -08001433
1434 /* fill up mac and fcoe headers */
1435 eh = eth_hdr(skb);
1436 eh->h_proto = htons(ETH_P_FCOE);
Chris Leech3fe9a0b2009-08-25 13:59:46 -07001437 if (fcoe->ctlr.map_dest)
Robert Love85b4aa42008-12-09 15:10:24 -08001438 fc_fcoe_set_mac(eh->h_dest, fh->fh_d_id);
1439 else
1440 /* insert GW address */
Chris Leech3fe9a0b2009-08-25 13:59:46 -07001441 memcpy(eh->h_dest, fcoe->ctlr.dest_addr, ETH_ALEN);
Robert Love85b4aa42008-12-09 15:10:24 -08001442
Chris Leech3fe9a0b2009-08-25 13:59:46 -07001443 if (unlikely(fcoe->ctlr.flogi_oxid != FC_XID_UNKNOWN))
1444 memcpy(eh->h_source, fcoe->ctlr.ctl_src_addr, ETH_ALEN);
Robert Love85b4aa42008-12-09 15:10:24 -08001445 else
Chris Leech11b56182009-11-03 11:46:29 -08001446 memcpy(eh->h_source, port->data_src_addr, ETH_ALEN);
Robert Love85b4aa42008-12-09 15:10:24 -08001447
1448 hp = (struct fcoe_hdr *)(eh + 1);
1449 memset(hp, 0, sizeof(*hp));
1450 if (FC_FCOE_VER)
1451 FC_FCOE_ENCAPS_VER(hp, FC_FCOE_VER);
1452 hp->fcoe_sof = sof;
1453
Yi Zou39ca9a02009-02-27 14:07:15 -08001454 /* fcoe lso, mss is in max_payload which is non-zero for FCP data */
Robert Love1875f272009-11-03 11:47:50 -08001455 if (lport->seq_offload && fr_max_payload(fp)) {
Yi Zou39ca9a02009-02-27 14:07:15 -08001456 skb_shinfo(skb)->gso_type = SKB_GSO_FCOE;
1457 skb_shinfo(skb)->gso_size = fr_max_payload(fp);
1458 } else {
1459 skb_shinfo(skb)->gso_type = 0;
1460 skb_shinfo(skb)->gso_size = 0;
1461 }
Robert Love85b4aa42008-12-09 15:10:24 -08001462 /* update tx stats: regardless if LLD fails */
Robert Love1875f272009-11-03 11:47:50 -08001463 stats = fc_lport_get_stats(lport);
Robert Love582b45b2009-03-31 15:51:50 -07001464 stats->TxFrames++;
1465 stats->TxWords += wlen;
Robert Love85b4aa42008-12-09 15:10:24 -08001466
1467 /* send down to lld */
Robert Love1875f272009-11-03 11:47:50 -08001468 fr_dev(fp) = lport;
Chris Leech014f5c32009-08-25 13:59:30 -07001469 if (port->fcoe_pending_queue.qlen)
Robert Love1875f272009-11-03 11:47:50 -08001470 fcoe_check_wait_queue(lport, skb);
Vasu Dev4bb6b512009-05-06 10:52:34 -07001471 else if (fcoe_start_io(skb))
Robert Love1875f272009-11-03 11:47:50 -08001472 fcoe_check_wait_queue(lport, skb);
Robert Love85b4aa42008-12-09 15:10:24 -08001473
1474 return 0;
1475}
Robert Love85b4aa42008-12-09 15:10:24 -08001476
Robert Love34f42a02009-02-27 10:55:45 -08001477/**
Robert Love1875f272009-11-03 11:47:50 -08001478 * fcoe_percpu_flush_done() - Indicate per-CPU queue flush completion
1479 * @skb: The completed skb (argument required by destructor)
Joe Eykholte7a51992009-08-25 14:04:08 -07001480 */
1481static void fcoe_percpu_flush_done(struct sk_buff *skb)
1482{
1483 complete(&fcoe_flush_completion);
1484}
1485
1486/**
Robert Love1875f272009-11-03 11:47:50 -08001487 * fcoe_percpu_receive_thread() - The per-CPU packet receive thread
1488 * @arg: The per-CPU context
Robert Love85b4aa42008-12-09 15:10:24 -08001489 *
1490 * Return: 0 for success
Robert Love85b4aa42008-12-09 15:10:24 -08001491 */
1492int fcoe_percpu_receive_thread(void *arg)
1493{
1494 struct fcoe_percpu_s *p = arg;
1495 u32 fr_len;
Robert Love1875f272009-11-03 11:47:50 -08001496 struct fc_lport *lport;
Robert Love85b4aa42008-12-09 15:10:24 -08001497 struct fcoe_rcv_info *fr;
1498 struct fcoe_dev_stats *stats;
1499 struct fc_frame_header *fh;
1500 struct sk_buff *skb;
1501 struct fcoe_crc_eof crc_eof;
1502 struct fc_frame *fp;
1503 u8 *mac = NULL;
Chris Leech014f5c32009-08-25 13:59:30 -07001504 struct fcoe_port *port;
Robert Love85b4aa42008-12-09 15:10:24 -08001505 struct fcoe_hdr *hp;
1506
Robert Love4469c192009-02-27 10:56:38 -08001507 set_user_nice(current, -20);
Robert Love85b4aa42008-12-09 15:10:24 -08001508
1509 while (!kthread_should_stop()) {
1510
1511 spin_lock_bh(&p->fcoe_rx_list.lock);
1512 while ((skb = __skb_dequeue(&p->fcoe_rx_list)) == NULL) {
1513 set_current_state(TASK_INTERRUPTIBLE);
1514 spin_unlock_bh(&p->fcoe_rx_list.lock);
1515 schedule();
1516 set_current_state(TASK_RUNNING);
1517 if (kthread_should_stop())
1518 return 0;
1519 spin_lock_bh(&p->fcoe_rx_list.lock);
1520 }
1521 spin_unlock_bh(&p->fcoe_rx_list.lock);
1522 fr = fcoe_dev_from_skb(skb);
Robert Love1875f272009-11-03 11:47:50 -08001523 lport = fr->fr_dev;
1524 if (unlikely(!lport)) {
Joe Eykholte7a51992009-08-25 14:04:08 -07001525 if (skb->destructor != fcoe_percpu_flush_done)
1526 FCOE_NETDEV_DBG(skb->dev, "NULL lport in skb");
Robert Love85b4aa42008-12-09 15:10:24 -08001527 kfree_skb(skb);
1528 continue;
1529 }
1530
Robert Loved5488eb2009-06-10 15:30:59 -07001531 FCOE_NETDEV_DBG(skb->dev, "skb_info: len:%d data_len:%d "
1532 "head:%p data:%p tail:%p end:%p sum:%d dev:%s",
1533 skb->len, skb->data_len,
1534 skb->head, skb->data, skb_tail_pointer(skb),
1535 skb_end_pointer(skb), skb->csum,
1536 skb->dev ? skb->dev->name : "<NULL>");
Robert Love85b4aa42008-12-09 15:10:24 -08001537
1538 /*
1539 * Save source MAC address before discarding header.
1540 */
Robert Love1875f272009-11-03 11:47:50 -08001541 port = lport_priv(lport);
Robert Love85b4aa42008-12-09 15:10:24 -08001542 if (skb_is_nonlinear(skb))
1543 skb_linearize(skb); /* not ideal */
Joe Eykholt97c83892009-03-17 11:42:40 -07001544 mac = eth_hdr(skb)->h_source;
Robert Love85b4aa42008-12-09 15:10:24 -08001545
1546 /*
1547 * Frame length checks and setting up the header pointers
1548 * was done in fcoe_rcv already.
1549 */
1550 hp = (struct fcoe_hdr *) skb_network_header(skb);
1551 fh = (struct fc_frame_header *) skb_transport_header(skb);
1552
Robert Love1875f272009-11-03 11:47:50 -08001553 stats = fc_lport_get_stats(lport);
Robert Love85b4aa42008-12-09 15:10:24 -08001554 if (unlikely(FC_FCOE_DECAPS_VER(hp) != FC_FCOE_VER)) {
Robert Love582b45b2009-03-31 15:51:50 -07001555 if (stats->ErrorFrames < 5)
Robert Loved5488eb2009-06-10 15:30:59 -07001556 printk(KERN_WARNING "fcoe: FCoE version "
Robert Love582b45b2009-03-31 15:51:50 -07001557 "mismatch: The frame has "
1558 "version %x, but the "
1559 "initiator supports version "
1560 "%x\n", FC_FCOE_DECAPS_VER(hp),
1561 FC_FCOE_VER);
1562 stats->ErrorFrames++;
Robert Love85b4aa42008-12-09 15:10:24 -08001563 kfree_skb(skb);
1564 continue;
1565 }
1566
1567 skb_pull(skb, sizeof(struct fcoe_hdr));
1568 fr_len = skb->len - sizeof(struct fcoe_crc_eof);
1569
Robert Love582b45b2009-03-31 15:51:50 -07001570 stats->RxFrames++;
1571 stats->RxWords += fr_len / FCOE_WORD_TO_BYTE;
Robert Love85b4aa42008-12-09 15:10:24 -08001572
1573 fp = (struct fc_frame *)skb;
1574 fc_frame_init(fp);
Robert Love1875f272009-11-03 11:47:50 -08001575 fr_dev(fp) = lport;
Robert Love85b4aa42008-12-09 15:10:24 -08001576 fr_sof(fp) = hp->fcoe_sof;
1577
1578 /* Copy out the CRC and EOF trailer for access */
1579 if (skb_copy_bits(skb, fr_len, &crc_eof, sizeof(crc_eof))) {
1580 kfree_skb(skb);
1581 continue;
1582 }
1583 fr_eof(fp) = crc_eof.fcoe_eof;
1584 fr_crc(fp) = crc_eof.fcoe_crc32;
1585 if (pskb_trim(skb, fr_len)) {
1586 kfree_skb(skb);
1587 continue;
1588 }
1589
1590 /*
1591 * We only check CRC if no offload is available and if it is
1592 * it's solicited data, in which case, the FCP layer would
1593 * check it during the copy.
1594 */
Robert Love1875f272009-11-03 11:47:50 -08001595 if (lport->crc_offload &&
1596 skb->ip_summed == CHECKSUM_UNNECESSARY)
Robert Love85b4aa42008-12-09 15:10:24 -08001597 fr_flags(fp) &= ~FCPHF_CRC_UNCHECKED;
1598 else
1599 fr_flags(fp) |= FCPHF_CRC_UNCHECKED;
1600
1601 fh = fc_frame_header_get(fp);
1602 if (fh->fh_r_ctl == FC_RCTL_DD_SOL_DATA &&
1603 fh->fh_type == FC_TYPE_FCP) {
Robert Love1875f272009-11-03 11:47:50 -08001604 fc_exch_recv(lport, fp);
Robert Love85b4aa42008-12-09 15:10:24 -08001605 continue;
1606 }
1607 if (fr_flags(fp) & FCPHF_CRC_UNCHECKED) {
1608 if (le32_to_cpu(fr_crc(fp)) !=
1609 ~crc32(~0, skb->data, fr_len)) {
Robert Loved5488eb2009-06-10 15:30:59 -07001610 if (stats->InvalidCRCCount < 5)
Robert Love85b4aa42008-12-09 15:10:24 -08001611 printk(KERN_WARNING "fcoe: dropping "
1612 "frame with CRC error\n");
1613 stats->InvalidCRCCount++;
1614 stats->ErrorFrames++;
1615 fc_frame_free(fp);
1616 continue;
1617 }
1618 fr_flags(fp) &= ~FCPHF_CRC_UNCHECKED;
1619 }
Robert Love1875f272009-11-03 11:47:50 -08001620 fc_exch_recv(lport, fp);
Robert Love85b4aa42008-12-09 15:10:24 -08001621 }
1622 return 0;
1623}
1624
1625/**
Robert Love1875f272009-11-03 11:47:50 -08001626 * fcoe_check_wait_queue() - Attempt to clear the transmit backlog
1627 * @lport: The local port whose backlog is to be cleared
Robert Love85b4aa42008-12-09 15:10:24 -08001628 *
Robert Love1875f272009-11-03 11:47:50 -08001629 * This empties the wait_queue, dequeues the head of the wait_queue queue
1630 * and calls fcoe_start_io() for each packet. If all skb have been
1631 * transmitted it returns the qlen. If an error occurs it restores
1632 * wait_queue (to try again later) and returns -1.
Robert Love85b4aa42008-12-09 15:10:24 -08001633 *
Robert Love1875f272009-11-03 11:47:50 -08001634 * The wait_queue is used when the skb transmit fails. The failed skb
1635 * will go in the wait_queue which will be emptied by the timer function or
Robert Love85b4aa42008-12-09 15:10:24 -08001636 * by the next skb transmit.
Robert Love34f42a02009-02-27 10:55:45 -08001637 */
Robert Love1875f272009-11-03 11:47:50 -08001638static void fcoe_check_wait_queue(struct fc_lport *lport, struct sk_buff *skb)
Robert Love85b4aa42008-12-09 15:10:24 -08001639{
Robert Love1875f272009-11-03 11:47:50 -08001640 struct fcoe_port *port = lport_priv(lport);
Vasu Dev4bb6b512009-05-06 10:52:34 -07001641 int rc;
Robert Love85b4aa42008-12-09 15:10:24 -08001642
Chris Leech014f5c32009-08-25 13:59:30 -07001643 spin_lock_bh(&port->fcoe_pending_queue.lock);
Vasu Dev4bb6b512009-05-06 10:52:34 -07001644
1645 if (skb)
Chris Leech014f5c32009-08-25 13:59:30 -07001646 __skb_queue_tail(&port->fcoe_pending_queue, skb);
Vasu Dev4bb6b512009-05-06 10:52:34 -07001647
Chris Leech014f5c32009-08-25 13:59:30 -07001648 if (port->fcoe_pending_queue_active)
Vasu Devc826a312009-02-27 10:56:27 -08001649 goto out;
Chris Leech014f5c32009-08-25 13:59:30 -07001650 port->fcoe_pending_queue_active = 1;
Chris Leech55c8baf2009-02-27 10:56:32 -08001651
Chris Leech014f5c32009-08-25 13:59:30 -07001652 while (port->fcoe_pending_queue.qlen) {
Chris Leech55c8baf2009-02-27 10:56:32 -08001653 /* keep qlen > 0 until fcoe_start_io succeeds */
Chris Leech014f5c32009-08-25 13:59:30 -07001654 port->fcoe_pending_queue.qlen++;
1655 skb = __skb_dequeue(&port->fcoe_pending_queue);
Chris Leech55c8baf2009-02-27 10:56:32 -08001656
Chris Leech014f5c32009-08-25 13:59:30 -07001657 spin_unlock_bh(&port->fcoe_pending_queue.lock);
Chris Leech55c8baf2009-02-27 10:56:32 -08001658 rc = fcoe_start_io(skb);
Chris Leech014f5c32009-08-25 13:59:30 -07001659 spin_lock_bh(&port->fcoe_pending_queue.lock);
Chris Leech55c8baf2009-02-27 10:56:32 -08001660
1661 if (rc) {
Chris Leech014f5c32009-08-25 13:59:30 -07001662 __skb_queue_head(&port->fcoe_pending_queue, skb);
Chris Leech55c8baf2009-02-27 10:56:32 -08001663 /* undo temporary increment above */
Chris Leech014f5c32009-08-25 13:59:30 -07001664 port->fcoe_pending_queue.qlen--;
Chris Leech55c8baf2009-02-27 10:56:32 -08001665 break;
Robert Love85b4aa42008-12-09 15:10:24 -08001666 }
Chris Leech55c8baf2009-02-27 10:56:32 -08001667 /* undo temporary increment above */
Chris Leech014f5c32009-08-25 13:59:30 -07001668 port->fcoe_pending_queue.qlen--;
Robert Love85b4aa42008-12-09 15:10:24 -08001669 }
Chris Leech55c8baf2009-02-27 10:56:32 -08001670
Chris Leech014f5c32009-08-25 13:59:30 -07001671 if (port->fcoe_pending_queue.qlen < FCOE_LOW_QUEUE_DEPTH)
Robert Love1875f272009-11-03 11:47:50 -08001672 lport->qfull = 0;
Chris Leech014f5c32009-08-25 13:59:30 -07001673 if (port->fcoe_pending_queue.qlen && !timer_pending(&port->timer))
1674 mod_timer(&port->timer, jiffies + 2);
1675 port->fcoe_pending_queue_active = 0;
Vasu Devc826a312009-02-27 10:56:27 -08001676out:
Chris Leech014f5c32009-08-25 13:59:30 -07001677 if (port->fcoe_pending_queue.qlen > FCOE_MAX_QUEUE_DEPTH)
Robert Love1875f272009-11-03 11:47:50 -08001678 lport->qfull = 1;
Chris Leech014f5c32009-08-25 13:59:30 -07001679 spin_unlock_bh(&port->fcoe_pending_queue.lock);
Vasu Dev4bb6b512009-05-06 10:52:34 -07001680 return;
Robert Love85b4aa42008-12-09 15:10:24 -08001681}
1682
1683/**
Robert Love1875f272009-11-03 11:47:50 -08001684 * fcoe_dev_setup() - Setup the link change notification interface
Robert Love34f42a02009-02-27 10:55:45 -08001685 */
Randy Dunlapb0d428a2009-04-27 21:49:31 -07001686static void fcoe_dev_setup(void)
Robert Love85b4aa42008-12-09 15:10:24 -08001687{
Robert Love85b4aa42008-12-09 15:10:24 -08001688 register_netdevice_notifier(&fcoe_notifier);
1689}
1690
1691/**
Robert Love1875f272009-11-03 11:47:50 -08001692 * fcoe_dev_cleanup() - Cleanup the link change notification interface
Robert Love34f42a02009-02-27 10:55:45 -08001693 */
Robert Love85b4aa42008-12-09 15:10:24 -08001694static void fcoe_dev_cleanup(void)
1695{
1696 unregister_netdevice_notifier(&fcoe_notifier);
1697}
1698
1699/**
Robert Love1875f272009-11-03 11:47:50 -08001700 * fcoe_device_notification() - Handler for net device events
1701 * @notifier: The context of the notification
1702 * @event: The type of event
1703 * @ptr: The net device that the event was on
Robert Love85b4aa42008-12-09 15:10:24 -08001704 *
Robert Love1875f272009-11-03 11:47:50 -08001705 * This function is called by the Ethernet driver in case of link change event.
Robert Love85b4aa42008-12-09 15:10:24 -08001706 *
1707 * Returns: 0 for success
Robert Love34f42a02009-02-27 10:55:45 -08001708 */
Robert Love85b4aa42008-12-09 15:10:24 -08001709static int fcoe_device_notification(struct notifier_block *notifier,
1710 ulong event, void *ptr)
1711{
Robert Love1875f272009-11-03 11:47:50 -08001712 struct fc_lport *lport = NULL;
Vasu Dev1d1b88d2009-07-29 17:05:45 -07001713 struct net_device *netdev = ptr;
Chris Leech014f5c32009-08-25 13:59:30 -07001714 struct fcoe_interface *fcoe;
Chris Leech2e70e242009-08-25 14:00:23 -07001715 struct fcoe_port *port;
Robert Love85b4aa42008-12-09 15:10:24 -08001716 struct fcoe_dev_stats *stats;
Joe Eykholt97c83892009-03-17 11:42:40 -07001717 u32 link_possible = 1;
Robert Love85b4aa42008-12-09 15:10:24 -08001718 u32 mfs;
1719 int rc = NOTIFY_OK;
1720
Chris Leech014f5c32009-08-25 13:59:30 -07001721 list_for_each_entry(fcoe, &fcoe_hostlist, list) {
Chris Leech25024982009-08-25 13:59:35 -07001722 if (fcoe->netdev == netdev) {
Robert Love1875f272009-11-03 11:47:50 -08001723 lport = fcoe->ctlr.lp;
Robert Love85b4aa42008-12-09 15:10:24 -08001724 break;
1725 }
1726 }
Robert Love1875f272009-11-03 11:47:50 -08001727 if (!lport) {
Robert Love85b4aa42008-12-09 15:10:24 -08001728 rc = NOTIFY_DONE;
1729 goto out;
1730 }
1731
Robert Love85b4aa42008-12-09 15:10:24 -08001732 switch (event) {
1733 case NETDEV_DOWN:
1734 case NETDEV_GOING_DOWN:
Joe Eykholt97c83892009-03-17 11:42:40 -07001735 link_possible = 0;
Robert Love85b4aa42008-12-09 15:10:24 -08001736 break;
1737 case NETDEV_UP:
1738 case NETDEV_CHANGE:
Robert Love85b4aa42008-12-09 15:10:24 -08001739 break;
1740 case NETDEV_CHANGEMTU:
Yi Zou7221d7e2009-10-21 16:27:52 -07001741 if (netdev->features & NETIF_F_FCOE_MTU)
1742 break;
Vasu Dev1d1b88d2009-07-29 17:05:45 -07001743 mfs = netdev->mtu - (sizeof(struct fcoe_hdr) +
1744 sizeof(struct fcoe_crc_eof));
Robert Love85b4aa42008-12-09 15:10:24 -08001745 if (mfs >= FC_MIN_MAX_FRAME)
Robert Love1875f272009-11-03 11:47:50 -08001746 fc_set_mfs(lport, mfs);
Robert Love85b4aa42008-12-09 15:10:24 -08001747 break;
1748 case NETDEV_REGISTER:
1749 break;
Chris Leech2e70e242009-08-25 14:00:23 -07001750 case NETDEV_UNREGISTER:
1751 list_del(&fcoe->list);
1752 port = lport_priv(fcoe->ctlr.lp);
1753 fcoe_interface_cleanup(fcoe);
1754 schedule_work(&port->destroy_work);
1755 goto out;
1756 break;
Robert Love85b4aa42008-12-09 15:10:24 -08001757 default:
Vasu Dev1d1b88d2009-07-29 17:05:45 -07001758 FCOE_NETDEV_DBG(netdev, "Unknown event %ld "
Robert Loved5488eb2009-06-10 15:30:59 -07001759 "from netdev netlink\n", event);
Robert Love85b4aa42008-12-09 15:10:24 -08001760 }
Robert Love1875f272009-11-03 11:47:50 -08001761 if (link_possible && !fcoe_link_ok(lport))
Chris Leech3fe9a0b2009-08-25 13:59:46 -07001762 fcoe_ctlr_link_up(&fcoe->ctlr);
1763 else if (fcoe_ctlr_link_down(&fcoe->ctlr)) {
Robert Love1875f272009-11-03 11:47:50 -08001764 stats = fc_lport_get_stats(lport);
Joe Eykholt97c83892009-03-17 11:42:40 -07001765 stats->LinkFailureCount++;
Robert Love1875f272009-11-03 11:47:50 -08001766 fcoe_clean_pending_queue(lport);
Robert Love85b4aa42008-12-09 15:10:24 -08001767 }
1768out:
1769 return rc;
1770}
1771
1772/**
Robert Love1875f272009-11-03 11:47:50 -08001773 * fcoe_if_to_netdev() - Parse a name buffer to get a net device
1774 * @buffer: The name of the net device
Robert Love85b4aa42008-12-09 15:10:24 -08001775 *
Robert Love1875f272009-11-03 11:47:50 -08001776 * Returns: NULL or a ptr to net_device
Robert Love34f42a02009-02-27 10:55:45 -08001777 */
Robert Love85b4aa42008-12-09 15:10:24 -08001778static struct net_device *fcoe_if_to_netdev(const char *buffer)
1779{
1780 char *cp;
1781 char ifname[IFNAMSIZ + 2];
1782
1783 if (buffer) {
1784 strlcpy(ifname, buffer, IFNAMSIZ);
1785 cp = ifname + strlen(ifname);
1786 while (--cp >= ifname && *cp == '\n')
1787 *cp = '\0';
1788 return dev_get_by_name(&init_net, ifname);
1789 }
1790 return NULL;
1791}
1792
1793/**
Robert Love1875f272009-11-03 11:47:50 -08001794 * fcoe_destroy() - Destroy a FCoE interface
1795 * @buffer: The name of the Ethernet interface to be destroyed
1796 * @kp: The associated kernel parameter
1797 *
1798 * Called from sysfs.
Robert Love85b4aa42008-12-09 15:10:24 -08001799 *
1800 * Returns: 0 for success
Robert Love34f42a02009-02-27 10:55:45 -08001801 */
Robert Love85b4aa42008-12-09 15:10:24 -08001802static int fcoe_destroy(const char *buffer, struct kernel_param *kp)
1803{
Chris Leech030f4e02009-08-25 14:00:02 -07001804 struct fcoe_interface *fcoe;
Chris Leech2e70e242009-08-25 14:00:23 -07001805 struct net_device *netdev;
Mike Christie8eca355f2009-10-21 16:27:44 -07001806 int rc = 0;
Robert Love85b4aa42008-12-09 15:10:24 -08001807
Chris Leechdfc1d0f2009-08-25 14:00:13 -07001808 mutex_lock(&fcoe_config_mutex);
1809#ifdef CONFIG_FCOE_MODULE
1810 /*
1811 * Make sure the module has been initialized, and is not about to be
1812 * removed. Module paramter sysfs files are writable before the
1813 * module_init function is called and after module_exit.
1814 */
1815 if (THIS_MODULE->state != MODULE_STATE_LIVE) {
1816 rc = -ENODEV;
1817 goto out_nodev;
1818 }
1819#endif
1820
Robert Love85b4aa42008-12-09 15:10:24 -08001821 netdev = fcoe_if_to_netdev(buffer);
1822 if (!netdev) {
1823 rc = -ENODEV;
1824 goto out_nodev;
1825 }
Chris Leech2e70e242009-08-25 14:00:23 -07001826
Chris Leech090eb6c2009-08-25 14:00:28 -07001827 rtnl_lock();
Chris Leech2e70e242009-08-25 14:00:23 -07001828 fcoe = fcoe_hostlist_lookup_port(netdev);
1829 if (!fcoe) {
Chris Leech090eb6c2009-08-25 14:00:28 -07001830 rtnl_unlock();
Robert Love85b4aa42008-12-09 15:10:24 -08001831 rc = -ENODEV;
1832 goto out_putdev;
1833 }
Chris Leech2e70e242009-08-25 14:00:23 -07001834 list_del(&fcoe->list);
Chris Leech2e70e242009-08-25 14:00:23 -07001835 fcoe_interface_cleanup(fcoe);
1836 rtnl_unlock();
1837 fcoe_if_destroy(fcoe->ctlr.lp);
Robert Love85b4aa42008-12-09 15:10:24 -08001838out_putdev:
1839 dev_put(netdev);
1840out_nodev:
Chris Leechdfc1d0f2009-08-25 14:00:13 -07001841 mutex_unlock(&fcoe_config_mutex);
Robert Love85b4aa42008-12-09 15:10:24 -08001842 return rc;
1843}
1844
Robert Love1875f272009-11-03 11:47:50 -08001845/**
1846 * fcoe_destroy_work() - Destroy a FCoE port in a deferred work context
1847 * @work: Handle to the FCoE port to be destroyed
1848 */
Chris Leech2e70e242009-08-25 14:00:23 -07001849static void fcoe_destroy_work(struct work_struct *work)
1850{
1851 struct fcoe_port *port;
1852
1853 port = container_of(work, struct fcoe_port, destroy_work);
1854 mutex_lock(&fcoe_config_mutex);
1855 fcoe_if_destroy(port->lport);
1856 mutex_unlock(&fcoe_config_mutex);
1857}
1858
Robert Love85b4aa42008-12-09 15:10:24 -08001859/**
Robert Love1875f272009-11-03 11:47:50 -08001860 * fcoe_create() - Create a fcoe interface
1861 * @buffer: The name of the Ethernet interface to create on
1862 * @kp: The associated kernel param
1863 *
1864 * Called from sysfs.
Robert Love85b4aa42008-12-09 15:10:24 -08001865 *
1866 * Returns: 0 for success
Robert Love34f42a02009-02-27 10:55:45 -08001867 */
Robert Love85b4aa42008-12-09 15:10:24 -08001868static int fcoe_create(const char *buffer, struct kernel_param *kp)
1869{
1870 int rc;
Chris Leech030f4e02009-08-25 14:00:02 -07001871 struct fcoe_interface *fcoe;
Chris Leechaf7f85d2009-08-25 13:59:24 -07001872 struct fc_lport *lport;
Robert Love85b4aa42008-12-09 15:10:24 -08001873 struct net_device *netdev;
1874
Chris Leechdfc1d0f2009-08-25 14:00:13 -07001875 mutex_lock(&fcoe_config_mutex);
1876#ifdef CONFIG_FCOE_MODULE
1877 /*
1878 * Make sure the module has been initialized, and is not about to be
1879 * removed. Module paramter sysfs files are writable before the
1880 * module_init function is called and after module_exit.
1881 */
1882 if (THIS_MODULE->state != MODULE_STATE_LIVE) {
1883 rc = -ENODEV;
1884 goto out_nodev;
1885 }
1886#endif
1887
Chris Leech2e70e242009-08-25 14:00:23 -07001888 rtnl_lock();
Robert Love85b4aa42008-12-09 15:10:24 -08001889 netdev = fcoe_if_to_netdev(buffer);
1890 if (!netdev) {
1891 rc = -ENODEV;
1892 goto out_nodev;
1893 }
Chris Leech2e70e242009-08-25 14:00:23 -07001894
Robert Love85b4aa42008-12-09 15:10:24 -08001895 /* look for existing lport */
1896 if (fcoe_hostlist_lookup(netdev)) {
1897 rc = -EEXIST;
1898 goto out_putdev;
1899 }
Robert Love85b4aa42008-12-09 15:10:24 -08001900
Chris Leech030f4e02009-08-25 14:00:02 -07001901 fcoe = fcoe_interface_create(netdev);
1902 if (!fcoe) {
1903 rc = -ENOMEM;
1904 goto out_putdev;
1905 }
1906
Chris Leech9a057532009-11-03 11:46:40 -08001907 lport = fcoe_if_create(fcoe, &netdev->dev, 0);
Chris Leechaf7f85d2009-08-25 13:59:24 -07001908 if (IS_ERR(lport)) {
Robert Loved5488eb2009-06-10 15:30:59 -07001909 printk(KERN_ERR "fcoe: Failed to create interface (%s)\n",
Robert Love85b4aa42008-12-09 15:10:24 -08001910 netdev->name);
Robert Love85b4aa42008-12-09 15:10:24 -08001911 rc = -EIO;
Chris Leech2e70e242009-08-25 14:00:23 -07001912 fcoe_interface_cleanup(fcoe);
Chris Leech030f4e02009-08-25 14:00:02 -07001913 goto out_free;
Robert Love85b4aa42008-12-09 15:10:24 -08001914 }
Chris Leech030f4e02009-08-25 14:00:02 -07001915
Chris Leech54b649f2009-08-25 14:00:07 -07001916 /* Make this the "master" N_Port */
1917 fcoe->ctlr.lp = lport;
Chris Leech030f4e02009-08-25 14:00:02 -07001918
Chris Leechc863df32009-08-25 14:00:18 -07001919 /* add to lports list */
1920 fcoe_hostlist_add(lport);
1921
Chris Leech54b649f2009-08-25 14:00:07 -07001922 /* start FIP Discovery and FLOGI */
1923 lport->boot_time = jiffies;
1924 fc_fabric_login(lport);
1925 if (!fcoe_link_ok(lport))
1926 fcoe_ctlr_link_up(&fcoe->ctlr);
1927
1928 rc = 0;
Chris Leech030f4e02009-08-25 14:00:02 -07001929out_free:
Chris Leech54b649f2009-08-25 14:00:07 -07001930 /*
1931 * Release from init in fcoe_interface_create(), on success lport
1932 * should be holding a reference taken in fcoe_if_create().
1933 */
Chris Leech030f4e02009-08-25 14:00:02 -07001934 fcoe_interface_put(fcoe);
Robert Love85b4aa42008-12-09 15:10:24 -08001935out_putdev:
1936 dev_put(netdev);
1937out_nodev:
Chris Leech2e70e242009-08-25 14:00:23 -07001938 rtnl_unlock();
Chris Leechdfc1d0f2009-08-25 14:00:13 -07001939 mutex_unlock(&fcoe_config_mutex);
Robert Love85b4aa42008-12-09 15:10:24 -08001940 return rc;
1941}
1942
Robert Love34f42a02009-02-27 10:55:45 -08001943/**
Robert Love1875f272009-11-03 11:47:50 -08001944 * fcoe_link_ok() - Check if the link is OK for a local port
1945 * @lport: The local port to check link on
Robert Love85b4aa42008-12-09 15:10:24 -08001946 *
1947 * Any permanently-disqualifying conditions have been previously checked.
1948 * This also updates the speed setting, which may change with link for 100/1000.
1949 *
1950 * This function should probably be checking for PAUSE support at some point
1951 * in the future. Currently Per-priority-pause is not determinable using
1952 * ethtool, so we shouldn't be restrictive until that problem is resolved.
1953 *
1954 * Returns: 0 if link is OK for use by FCoE.
1955 *
1956 */
Robert Love1875f272009-11-03 11:47:50 -08001957int fcoe_link_ok(struct fc_lport *lport)
Robert Love85b4aa42008-12-09 15:10:24 -08001958{
Robert Love1875f272009-11-03 11:47:50 -08001959 struct fcoe_port *port = lport_priv(lport);
1960 struct net_device *netdev = port->fcoe->netdev;
Robert Love85b4aa42008-12-09 15:10:24 -08001961 struct ethtool_cmd ecmd = { ETHTOOL_GSET };
Robert Love85b4aa42008-12-09 15:10:24 -08001962
Robert Love1875f272009-11-03 11:47:50 -08001963 if ((netdev->flags & IFF_UP) && netif_carrier_ok(netdev) &&
1964 (!dev_ethtool_get_settings(netdev, &ecmd))) {
1965 lport->link_supported_speeds &=
Yi Zou2f718d62009-07-29 17:04:01 -07001966 ~(FC_PORTSPEED_1GBIT | FC_PORTSPEED_10GBIT);
1967 if (ecmd.supported & (SUPPORTED_1000baseT_Half |
1968 SUPPORTED_1000baseT_Full))
Robert Love1875f272009-11-03 11:47:50 -08001969 lport->link_supported_speeds |= FC_PORTSPEED_1GBIT;
Yi Zou2f718d62009-07-29 17:04:01 -07001970 if (ecmd.supported & SUPPORTED_10000baseT_Full)
Robert Love1875f272009-11-03 11:47:50 -08001971 lport->link_supported_speeds |=
Yi Zou2f718d62009-07-29 17:04:01 -07001972 FC_PORTSPEED_10GBIT;
1973 if (ecmd.speed == SPEED_1000)
Robert Love1875f272009-11-03 11:47:50 -08001974 lport->link_speed = FC_PORTSPEED_1GBIT;
Yi Zou2f718d62009-07-29 17:04:01 -07001975 if (ecmd.speed == SPEED_10000)
Robert Love1875f272009-11-03 11:47:50 -08001976 lport->link_speed = FC_PORTSPEED_10GBIT;
Robert Love85b4aa42008-12-09 15:10:24 -08001977
Yi Zou2f718d62009-07-29 17:04:01 -07001978 return 0;
1979 }
1980 return -1;
Robert Love85b4aa42008-12-09 15:10:24 -08001981}
Robert Love85b4aa42008-12-09 15:10:24 -08001982
Robert Love34f42a02009-02-27 10:55:45 -08001983/**
Robert Love1875f272009-11-03 11:47:50 -08001984 * fcoe_percpu_clean() - Clear all pending skbs for an local port
1985 * @lport: The local port whose skbs are to be cleared
Joe Eykholte7a51992009-08-25 14:04:08 -07001986 *
1987 * Must be called with fcoe_create_mutex held to single-thread completion.
1988 *
1989 * This flushes the pending skbs by adding a new skb to each queue and
1990 * waiting until they are all freed. This assures us that not only are
1991 * there no packets that will be handled by the lport, but also that any
1992 * threads already handling packet have returned.
Robert Love85b4aa42008-12-09 15:10:24 -08001993 */
Robert Love1875f272009-11-03 11:47:50 -08001994void fcoe_percpu_clean(struct fc_lport *lport)
Robert Love85b4aa42008-12-09 15:10:24 -08001995{
Robert Love85b4aa42008-12-09 15:10:24 -08001996 struct fcoe_percpu_s *pp;
1997 struct fcoe_rcv_info *fr;
1998 struct sk_buff_head *list;
1999 struct sk_buff *skb, *next;
2000 struct sk_buff *head;
Robert Love5e5e92d2009-03-17 11:41:35 -07002001 unsigned int cpu;
Robert Love85b4aa42008-12-09 15:10:24 -08002002
Robert Love5e5e92d2009-03-17 11:41:35 -07002003 for_each_possible_cpu(cpu) {
2004 pp = &per_cpu(fcoe_percpu, cpu);
2005 spin_lock_bh(&pp->fcoe_rx_list.lock);
2006 list = &pp->fcoe_rx_list;
2007 head = list->next;
2008 for (skb = head; skb != (struct sk_buff *)list;
2009 skb = next) {
2010 next = skb->next;
2011 fr = fcoe_dev_from_skb(skb);
Robert Love1875f272009-11-03 11:47:50 -08002012 if (fr->fr_dev == lport) {
Robert Love5e5e92d2009-03-17 11:41:35 -07002013 __skb_unlink(skb, list);
2014 kfree_skb(skb);
Robert Love85b4aa42008-12-09 15:10:24 -08002015 }
Robert Love85b4aa42008-12-09 15:10:24 -08002016 }
Joe Eykholte7a51992009-08-25 14:04:08 -07002017
2018 if (!pp->thread || !cpu_online(cpu)) {
2019 spin_unlock_bh(&pp->fcoe_rx_list.lock);
2020 continue;
2021 }
2022
2023 skb = dev_alloc_skb(0);
2024 if (!skb) {
2025 spin_unlock_bh(&pp->fcoe_rx_list.lock);
2026 continue;
2027 }
2028 skb->destructor = fcoe_percpu_flush_done;
2029
2030 __skb_queue_tail(&pp->fcoe_rx_list, skb);
2031 if (pp->fcoe_rx_list.qlen == 1)
2032 wake_up_process(pp->thread);
Robert Love5e5e92d2009-03-17 11:41:35 -07002033 spin_unlock_bh(&pp->fcoe_rx_list.lock);
Joe Eykholte7a51992009-08-25 14:04:08 -07002034
2035 wait_for_completion(&fcoe_flush_completion);
Robert Love85b4aa42008-12-09 15:10:24 -08002036 }
2037}
Robert Love85b4aa42008-12-09 15:10:24 -08002038
2039/**
Robert Love34f42a02009-02-27 10:55:45 -08002040 * fcoe_clean_pending_queue() - Dequeue a skb and free it
Robert Love1875f272009-11-03 11:47:50 -08002041 * @lport: The local port to dequeue a skb on
Robert Love34f42a02009-02-27 10:55:45 -08002042 */
Robert Love1875f272009-11-03 11:47:50 -08002043void fcoe_clean_pending_queue(struct fc_lport *lport)
Robert Love85b4aa42008-12-09 15:10:24 -08002044{
Robert Love1875f272009-11-03 11:47:50 -08002045 struct fcoe_port *port = lport_priv(lport);
Robert Love85b4aa42008-12-09 15:10:24 -08002046 struct sk_buff *skb;
2047
Chris Leech014f5c32009-08-25 13:59:30 -07002048 spin_lock_bh(&port->fcoe_pending_queue.lock);
2049 while ((skb = __skb_dequeue(&port->fcoe_pending_queue)) != NULL) {
2050 spin_unlock_bh(&port->fcoe_pending_queue.lock);
Robert Love85b4aa42008-12-09 15:10:24 -08002051 kfree_skb(skb);
Chris Leech014f5c32009-08-25 13:59:30 -07002052 spin_lock_bh(&port->fcoe_pending_queue.lock);
Robert Love85b4aa42008-12-09 15:10:24 -08002053 }
Chris Leech014f5c32009-08-25 13:59:30 -07002054 spin_unlock_bh(&port->fcoe_pending_queue.lock);
Robert Love85b4aa42008-12-09 15:10:24 -08002055}
Robert Love85b4aa42008-12-09 15:10:24 -08002056
2057/**
Robert Love1875f272009-11-03 11:47:50 -08002058 * fcoe_reset() - Reset a local port
2059 * @shost: The SCSI host associated with the local port to be reset
Robert Love85b4aa42008-12-09 15:10:24 -08002060 *
Robert Love1875f272009-11-03 11:47:50 -08002061 * Returns: Always 0 (return value required by FC transport template)
Robert Love85b4aa42008-12-09 15:10:24 -08002062 */
2063int fcoe_reset(struct Scsi_Host *shost)
2064{
2065 struct fc_lport *lport = shost_priv(shost);
2066 fc_lport_reset(lport);
2067 return 0;
2068}
Robert Love85b4aa42008-12-09 15:10:24 -08002069
Robert Love34f42a02009-02-27 10:55:45 -08002070/**
Robert Love1875f272009-11-03 11:47:50 -08002071 * fcoe_hostlist_lookup_port() - Find the FCoE interface associated with a net device
2072 * @netdev: The net device used as a key
Robert Love85b4aa42008-12-09 15:10:24 -08002073 *
Robert Love1875f272009-11-03 11:47:50 -08002074 * Locking: Must be called with the RNL mutex held.
2075 *
2076 * Returns: NULL or the FCoE interface
Robert Love85b4aa42008-12-09 15:10:24 -08002077 */
Chris Leech014f5c32009-08-25 13:59:30 -07002078static struct fcoe_interface *
Robert Love1875f272009-11-03 11:47:50 -08002079fcoe_hostlist_lookup_port(const struct net_device *netdev)
Robert Love85b4aa42008-12-09 15:10:24 -08002080{
Chris Leech014f5c32009-08-25 13:59:30 -07002081 struct fcoe_interface *fcoe;
Robert Love85b4aa42008-12-09 15:10:24 -08002082
Chris Leech014f5c32009-08-25 13:59:30 -07002083 list_for_each_entry(fcoe, &fcoe_hostlist, list) {
Robert Love1875f272009-11-03 11:47:50 -08002084 if (fcoe->netdev == netdev)
Chris Leech014f5c32009-08-25 13:59:30 -07002085 return fcoe;
Robert Love85b4aa42008-12-09 15:10:24 -08002086 }
Robert Love85b4aa42008-12-09 15:10:24 -08002087 return NULL;
2088}
2089
Robert Love34f42a02009-02-27 10:55:45 -08002090/**
Robert Love1875f272009-11-03 11:47:50 -08002091 * fcoe_hostlist_lookup() - Find the local port associated with a
2092 * given net device
2093 * @netdev: The netdevice used as a key
Robert Love85b4aa42008-12-09 15:10:24 -08002094 *
Robert Love1875f272009-11-03 11:47:50 -08002095 * Locking: Must be called with the RTNL mutex held
2096 *
2097 * Returns: NULL or the local port
Robert Love85b4aa42008-12-09 15:10:24 -08002098 */
Chris Leech090eb6c2009-08-25 14:00:28 -07002099static struct fc_lport *fcoe_hostlist_lookup(const struct net_device *netdev)
Robert Love85b4aa42008-12-09 15:10:24 -08002100{
Chris Leech014f5c32009-08-25 13:59:30 -07002101 struct fcoe_interface *fcoe;
Robert Love85b4aa42008-12-09 15:10:24 -08002102
Chris Leech014f5c32009-08-25 13:59:30 -07002103 fcoe = fcoe_hostlist_lookup_port(netdev);
Chris Leech3fe9a0b2009-08-25 13:59:46 -07002104 return (fcoe) ? fcoe->ctlr.lp : NULL;
Robert Love85b4aa42008-12-09 15:10:24 -08002105}
Robert Love85b4aa42008-12-09 15:10:24 -08002106
Robert Love34f42a02009-02-27 10:55:45 -08002107/**
Robert Love1875f272009-11-03 11:47:50 -08002108 * fcoe_hostlist_add() - Add the FCoE interface identified by a local
2109 * port to the hostlist
2110 * @lport: The local port that identifies the FCoE interface to be added
2111 *
2112 * Locking: must be called with the RTNL mutex held
Robert Love85b4aa42008-12-09 15:10:24 -08002113 *
2114 * Returns: 0 for success
2115 */
Chris Leech090eb6c2009-08-25 14:00:28 -07002116static int fcoe_hostlist_add(const struct fc_lport *lport)
Robert Love85b4aa42008-12-09 15:10:24 -08002117{
Chris Leech014f5c32009-08-25 13:59:30 -07002118 struct fcoe_interface *fcoe;
2119 struct fcoe_port *port;
Robert Love85b4aa42008-12-09 15:10:24 -08002120
Chris Leech014f5c32009-08-25 13:59:30 -07002121 fcoe = fcoe_hostlist_lookup_port(fcoe_netdev(lport));
2122 if (!fcoe) {
2123 port = lport_priv(lport);
2124 fcoe = port->fcoe;
2125 list_add_tail(&fcoe->list, &fcoe_hostlist);
Robert Love85b4aa42008-12-09 15:10:24 -08002126 }
2127 return 0;
2128}
Robert Love85b4aa42008-12-09 15:10:24 -08002129
Robert Love34f42a02009-02-27 10:55:45 -08002130/**
Robert Love1875f272009-11-03 11:47:50 -08002131 * fcoe_init() - Initialize fcoe.ko
Robert Love85b4aa42008-12-09 15:10:24 -08002132 *
Robert Love1875f272009-11-03 11:47:50 -08002133 * Returns: 0 on success, or a negative value on failure
Robert Love34f42a02009-02-27 10:55:45 -08002134 */
Robert Love85b4aa42008-12-09 15:10:24 -08002135static int __init fcoe_init(void)
2136{
Robert Love1875f272009-11-03 11:47:50 -08002137 struct fcoe_percpu_s *p;
Robert Love38eccab2009-03-17 11:41:30 -07002138 unsigned int cpu;
Robert Love8976f422009-03-17 11:41:46 -07002139 int rc = 0;
Robert Love85b4aa42008-12-09 15:10:24 -08002140
Chris Leechdfc1d0f2009-08-25 14:00:13 -07002141 mutex_lock(&fcoe_config_mutex);
2142
Robert Love38eccab2009-03-17 11:41:30 -07002143 for_each_possible_cpu(cpu) {
Robert Love5e5e92d2009-03-17 11:41:35 -07002144 p = &per_cpu(fcoe_percpu, cpu);
Robert Love38eccab2009-03-17 11:41:30 -07002145 skb_queue_head_init(&p->fcoe_rx_list);
2146 }
2147
Robert Love8976f422009-03-17 11:41:46 -07002148 for_each_online_cpu(cpu)
2149 fcoe_percpu_thread_create(cpu);
Robert Love85b4aa42008-12-09 15:10:24 -08002150
Robert Love8976f422009-03-17 11:41:46 -07002151 /* Initialize per CPU interrupt thread */
2152 rc = register_hotcpu_notifier(&fcoe_cpu_notifier);
2153 if (rc)
2154 goto out_free;
Robert Love85b4aa42008-12-09 15:10:24 -08002155
Robert Love8976f422009-03-17 11:41:46 -07002156 /* Setup link change notification */
Robert Love85b4aa42008-12-09 15:10:24 -08002157 fcoe_dev_setup();
2158
Chris Leech5892c322009-08-25 13:59:14 -07002159 rc = fcoe_if_init();
2160 if (rc)
2161 goto out_free;
Robert Love85b4aa42008-12-09 15:10:24 -08002162
Chris Leechdfc1d0f2009-08-25 14:00:13 -07002163 mutex_unlock(&fcoe_config_mutex);
Robert Love85b4aa42008-12-09 15:10:24 -08002164 return 0;
Robert Love8976f422009-03-17 11:41:46 -07002165
2166out_free:
2167 for_each_online_cpu(cpu) {
2168 fcoe_percpu_thread_destroy(cpu);
2169 }
Chris Leechdfc1d0f2009-08-25 14:00:13 -07002170 mutex_unlock(&fcoe_config_mutex);
Robert Love8976f422009-03-17 11:41:46 -07002171 return rc;
Robert Love85b4aa42008-12-09 15:10:24 -08002172}
2173module_init(fcoe_init);
2174
2175/**
Robert Love1875f272009-11-03 11:47:50 -08002176 * fcoe_exit() - Clean up fcoe.ko
Robert Love85b4aa42008-12-09 15:10:24 -08002177 *
Robert Love1875f272009-11-03 11:47:50 -08002178 * Returns: 0 on success or a negative value on failure
Robert Love34f42a02009-02-27 10:55:45 -08002179 */
Robert Love85b4aa42008-12-09 15:10:24 -08002180static void __exit fcoe_exit(void)
2181{
Chris Leech014f5c32009-08-25 13:59:30 -07002182 struct fcoe_interface *fcoe, *tmp;
Chris Leech2e70e242009-08-25 14:00:23 -07002183 struct fcoe_port *port;
Robert Love1875f272009-11-03 11:47:50 -08002184 unsigned int cpu;
Robert Love85b4aa42008-12-09 15:10:24 -08002185
Chris Leechdfc1d0f2009-08-25 14:00:13 -07002186 mutex_lock(&fcoe_config_mutex);
2187
Robert Love85b4aa42008-12-09 15:10:24 -08002188 fcoe_dev_cleanup();
2189
Vasu Dev5919a592009-03-27 09:03:29 -07002190 /* releases the associated fcoe hosts */
Chris Leech090eb6c2009-08-25 14:00:28 -07002191 rtnl_lock();
2192 list_for_each_entry_safe(fcoe, tmp, &fcoe_hostlist, list) {
Chris Leechc863df32009-08-25 14:00:18 -07002193 list_del(&fcoe->list);
Chris Leech2e70e242009-08-25 14:00:23 -07002194 port = lport_priv(fcoe->ctlr.lp);
Chris Leech2e70e242009-08-25 14:00:23 -07002195 fcoe_interface_cleanup(fcoe);
Chris Leech2e70e242009-08-25 14:00:23 -07002196 schedule_work(&port->destroy_work);
Chris Leechc863df32009-08-25 14:00:18 -07002197 }
Chris Leech090eb6c2009-08-25 14:00:28 -07002198 rtnl_unlock();
Robert Love85b4aa42008-12-09 15:10:24 -08002199
Robert Love8976f422009-03-17 11:41:46 -07002200 unregister_hotcpu_notifier(&fcoe_cpu_notifier);
2201
Chris Leech014f5c32009-08-25 13:59:30 -07002202 for_each_online_cpu(cpu)
Robert Love8976f422009-03-17 11:41:46 -07002203 fcoe_percpu_thread_destroy(cpu);
Robert Love85b4aa42008-12-09 15:10:24 -08002204
Chris Leechdfc1d0f2009-08-25 14:00:13 -07002205 mutex_unlock(&fcoe_config_mutex);
Chris Leech2e70e242009-08-25 14:00:23 -07002206
2207 /* flush any asyncronous interface destroys,
2208 * this should happen after the netdev notifier is unregistered */
2209 flush_scheduled_work();
Chris Leech9a057532009-11-03 11:46:40 -08002210 /* That will flush out all the N_Ports on the hostlist, but now we
2211 * may have NPIV VN_Ports scheduled for destruction */
2212 flush_scheduled_work();
Chris Leech2e70e242009-08-25 14:00:23 -07002213
2214 /* detach from scsi transport
2215 * must happen after all destroys are done, therefor after the flush */
2216 fcoe_if_exit();
Robert Love85b4aa42008-12-09 15:10:24 -08002217}
2218module_exit(fcoe_exit);
Chris Leech11b56182009-11-03 11:46:29 -08002219
2220/**
2221 * fcoe_flogi_resp() - FCoE specific FLOGI and FDISC response handler
2222 * @seq: active sequence in the FLOGI or FDISC exchange
2223 * @fp: response frame, or error encoded in a pointer (timeout)
2224 * @arg: pointer the the fcoe_ctlr structure
2225 *
2226 * This handles MAC address managment for FCoE, then passes control on to
2227 * the libfc FLOGI response handler.
2228 */
2229static void fcoe_flogi_resp(struct fc_seq *seq, struct fc_frame *fp, void *arg)
2230{
2231 struct fcoe_ctlr *fip = arg;
2232 struct fc_exch *exch = fc_seq_exch(seq);
2233 struct fc_lport *lport = exch->lp;
2234 u8 *mac;
2235
2236 if (IS_ERR(fp))
2237 goto done;
2238
2239 mac = fr_cb(fp)->granted_mac;
2240 if (is_zero_ether_addr(mac)) {
2241 /* pre-FIP */
2242 mac = eth_hdr(&fp->skb)->h_source;
2243 if (fcoe_ctlr_recv_flogi(fip, lport, fp, mac)) {
2244 fc_frame_free(fp);
2245 return;
2246 }
2247 } else {
2248 /* FIP, libfcoe has already seen it */
2249 fip->update_mac(lport, fr_cb(fp)->granted_mac);
2250 }
2251done:
2252 fc_lport_flogi_resp(seq, fp, lport);
2253}
2254
2255/**
2256 * fcoe_logo_resp() - FCoE specific LOGO response handler
2257 * @seq: active sequence in the LOGO exchange
2258 * @fp: response frame, or error encoded in a pointer (timeout)
2259 * @arg: pointer the the fcoe_ctlr structure
2260 *
2261 * This handles MAC address managment for FCoE, then passes control on to
2262 * the libfc LOGO response handler.
2263 */
2264static void fcoe_logo_resp(struct fc_seq *seq, struct fc_frame *fp, void *arg)
2265{
2266 struct fcoe_ctlr *fip = arg;
2267 struct fc_exch *exch = fc_seq_exch(seq);
2268 struct fc_lport *lport = exch->lp;
2269 static u8 zero_mac[ETH_ALEN] = { 0 };
2270
2271 if (!IS_ERR(fp))
2272 fip->update_mac(lport, zero_mac);
2273 fc_lport_logo_resp(seq, fp, lport);
2274}
2275
2276/**
2277 * fcoe_elsct_send - FCoE specific ELS handler
2278 *
2279 * This does special case handling of FIP encapsualted ELS exchanges for FCoE,
2280 * using FCoE specific response handlers and passing the FIP controller as
2281 * the argument (the lport is still available from the exchange).
2282 *
2283 * Most of the work here is just handed off to the libfc routine.
2284 */
Robert Love1875f272009-11-03 11:47:50 -08002285static struct fc_seq *fcoe_elsct_send(struct fc_lport *lport, u32 did,
2286 struct fc_frame *fp, unsigned int op,
2287 void (*resp)(struct fc_seq *,
2288 struct fc_frame *,
2289 void *),
2290 void *arg, u32 timeout)
Chris Leech11b56182009-11-03 11:46:29 -08002291{
2292 struct fcoe_port *port = lport_priv(lport);
2293 struct fcoe_interface *fcoe = port->fcoe;
2294 struct fcoe_ctlr *fip = &fcoe->ctlr;
2295 struct fc_frame_header *fh = fc_frame_header_get(fp);
2296
2297 switch (op) {
2298 case ELS_FLOGI:
2299 case ELS_FDISC:
2300 return fc_elsct_send(lport, did, fp, op, fcoe_flogi_resp,
2301 fip, timeout);
2302 case ELS_LOGO:
2303 /* only hook onto fabric logouts, not port logouts */
2304 if (ntoh24(fh->fh_d_id) != FC_FID_FLOGI)
2305 break;
2306 return fc_elsct_send(lport, did, fp, op, fcoe_logo_resp,
2307 fip, timeout);
2308 }
2309 return fc_elsct_send(lport, did, fp, op, resp, arg, timeout);
2310}
2311
Chris Leech9a057532009-11-03 11:46:40 -08002312/**
2313 * fcoe_vport_create() - create an fc_host/scsi_host for a vport
2314 * @vport: fc_vport object to create a new fc_host for
2315 * @disabled: start the new fc_host in a disabled state by default?
2316 *
2317 * Returns: 0 for success
2318 */
2319static int fcoe_vport_create(struct fc_vport *vport, bool disabled)
2320{
2321 struct Scsi_Host *shost = vport_to_shost(vport);
2322 struct fc_lport *n_port = shost_priv(shost);
2323 struct fcoe_port *port = lport_priv(n_port);
2324 struct fcoe_interface *fcoe = port->fcoe;
2325 struct net_device *netdev = fcoe->netdev;
2326 struct fc_lport *vn_port;
2327
2328 mutex_lock(&fcoe_config_mutex);
2329 vn_port = fcoe_if_create(fcoe, &vport->dev, 1);
2330 mutex_unlock(&fcoe_config_mutex);
2331
2332 if (IS_ERR(vn_port)) {
2333 printk(KERN_ERR "fcoe: fcoe_vport_create(%s) failed\n",
2334 netdev->name);
2335 return -EIO;
2336 }
2337
2338 if (disabled) {
2339 fc_vport_set_state(vport, FC_VPORT_DISABLED);
2340 } else {
2341 vn_port->boot_time = jiffies;
2342 fc_fabric_login(vn_port);
2343 fc_vport_setlink(vn_port);
2344 }
2345 return 0;
2346}
2347
2348/**
2349 * fcoe_vport_destroy() - destroy the fc_host/scsi_host for a vport
2350 * @vport: fc_vport object that is being destroyed
2351 *
2352 * Returns: 0 for success
2353 */
2354static int fcoe_vport_destroy(struct fc_vport *vport)
2355{
2356 struct Scsi_Host *shost = vport_to_shost(vport);
2357 struct fc_lport *n_port = shost_priv(shost);
2358 struct fc_lport *vn_port = vport->dd_data;
2359 struct fcoe_port *port = lport_priv(vn_port);
2360
2361 mutex_lock(&n_port->lp_mutex);
2362 list_del(&vn_port->list);
2363 mutex_unlock(&n_port->lp_mutex);
2364 schedule_work(&port->destroy_work);
2365 return 0;
2366}
2367
2368/**
2369 * fcoe_vport_disable() - change vport state
2370 * @vport: vport to bring online/offline
2371 * @disable: should the vport be disabled?
2372 */
2373static int fcoe_vport_disable(struct fc_vport *vport, bool disable)
2374{
2375 struct fc_lport *lport = vport->dd_data;
2376
2377 if (disable) {
2378 fc_vport_set_state(vport, FC_VPORT_DISABLED);
2379 fc_fabric_logoff(lport);
2380 } else {
2381 lport->boot_time = jiffies;
2382 fc_fabric_login(lport);
2383 fc_vport_setlink(lport);
2384 }
2385
2386 return 0;
2387}
2388
Chris Leechdc8596d2009-11-03 11:47:18 -08002389/**
2390 * fcoe_vport_set_symbolic_name() - append vport string to symbolic name
2391 * @vport: fc_vport with a new symbolic name string
2392 *
2393 * After generating a new symbolic name string, a new RSPN_ID request is
2394 * sent to the name server. There is no response handler, so if it fails
2395 * for some reason it will not be retried.
2396 */
2397static void fcoe_set_vport_symbolic_name(struct fc_vport *vport)
2398{
2399 struct fc_lport *lport = vport->dd_data;
2400 struct fc_frame *fp;
2401 size_t len;
2402
2403 snprintf(fc_host_symbolic_name(lport->host), FC_SYMBOLIC_NAME_SIZE,
2404 "%s v%s over %s : %s", FCOE_NAME, FCOE_VERSION,
2405 fcoe_netdev(lport)->name, vport->symbolic_name);
2406
2407 if (lport->state != LPORT_ST_READY)
2408 return;
2409
2410 len = strnlen(fc_host_symbolic_name(lport->host), 255);
2411 fp = fc_frame_alloc(lport,
2412 sizeof(struct fc_ct_hdr) +
2413 sizeof(struct fc_ns_rspn) + len);
2414 if (!fp)
2415 return;
2416 lport->tt.elsct_send(lport, FC_FID_DIR_SERV, fp, FC_NS_RSPN_ID,
2417 NULL, NULL, lport->e_d_tov);
2418}