blob: 481ba6f592efeaf0bca0c9bb9c65b41496638de7 [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>
Robert Love85b4aa42008-12-09 15:10:24 -080021#include <linux/spinlock.h>
Robert Love85b4aa42008-12-09 15:10:24 -080022#include <linux/netdevice.h>
23#include <linux/etherdevice.h>
24#include <linux/ethtool.h>
25#include <linux/if_ether.h>
26#include <linux/if_vlan.h>
Robert Love85b4aa42008-12-09 15:10:24 -080027#include <linux/crc32.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090028#include <linux/slab.h>
Robert Love85b4aa42008-12-09 15:10:24 -080029#include <linux/cpu.h>
30#include <linux/fs.h>
31#include <linux/sysfs.h>
32#include <linux/ctype.h>
Tejun Heo2ca32b42011-01-28 16:05:32 -080033#include <linux/workqueue.h>
john fastabend6f6c2aa2011-11-18 13:35:56 -080034#include <net/dcbnl.h>
35#include <net/dcbevent.h>
Robert Love85b4aa42008-12-09 15:10:24 -080036#include <scsi/scsi_tcq.h>
37#include <scsi/scsicam.h>
38#include <scsi/scsi_transport.h>
39#include <scsi/scsi_transport_fc.h>
40#include <net/rtnetlink.h>
41
42#include <scsi/fc/fc_encaps.h>
Joe Eykholt97c83892009-03-17 11:42:40 -070043#include <scsi/fc/fc_fip.h>
Robert Love85b4aa42008-12-09 15:10:24 -080044
45#include <scsi/libfc.h>
46#include <scsi/fc_frame.h>
47#include <scsi/libfcoe.h>
Robert Love85b4aa42008-12-09 15:10:24 -080048
Vasu Devfdd78022009-03-17 11:42:24 -070049#include "fcoe.h"
50
Robert Love85b4aa42008-12-09 15:10:24 -080051MODULE_AUTHOR("Open-FCoE.org");
52MODULE_DESCRIPTION("FCoE");
Vasu Dev9b34ecf2009-03-17 11:42:13 -070053MODULE_LICENSE("GPL v2");
Robert Love85b4aa42008-12-09 15:10:24 -080054
Yi Zou05cc7392009-08-25 13:59:03 -070055/* Performance tuning parameters for fcoe */
Vasu Dev860eca22011-09-27 21:38:18 -070056static unsigned int fcoe_ddp_min = 4096;
Yi Zou05cc7392009-08-25 13:59:03 -070057module_param_named(ddp_min, fcoe_ddp_min, uint, S_IRUGO | S_IWUSR);
58MODULE_PARM_DESC(ddp_min, "Minimum I/O size in bytes for " \
59 "Direct Data Placement (DDP).");
60
Bart Van Assche7c9c6842012-01-13 17:26:25 -080061unsigned int fcoe_debug_logging;
62module_param_named(debug_logging, fcoe_debug_logging, int, S_IRUGO|S_IWUSR);
63MODULE_PARM_DESC(debug_logging, "a bit mask of logging levels");
64
65static DEFINE_MUTEX(fcoe_config_mutex);
Chris Leechdfc1d0f2009-08-25 14:00:13 -070066
Tejun Heo2ca32b42011-01-28 16:05:32 -080067static struct workqueue_struct *fcoe_wq;
68
Joe Eykholte7a51992009-08-25 14:04:08 -070069/* fcoe_percpu_clean completion. Waiter protected by fcoe_create_mutex */
70static DECLARE_COMPLETION(fcoe_flush_completion);
71
Robert Love85b4aa42008-12-09 15:10:24 -080072/* fcoe host list */
Chris Leech090eb6c2009-08-25 14:00:28 -070073/* must only by accessed under the RTNL mutex */
Bart Van Assche7c9c6842012-01-13 17:26:25 -080074static LIST_HEAD(fcoe_hostlist);
75static DEFINE_PER_CPU(struct fcoe_percpu_s, fcoe_percpu);
Robert Love85b4aa42008-12-09 15:10:24 -080076
Chris Leechdd3fd722009-04-21 16:27:36 -070077/* Function Prototypes */
Robert Love1875f272009-11-03 11:47:50 -080078static int fcoe_reset(struct Scsi_Host *);
Vasu Devfdd78022009-03-17 11:42:24 -070079static int fcoe_xmit(struct fc_lport *, struct fc_frame *);
80static int fcoe_rcv(struct sk_buff *, struct net_device *,
81 struct packet_type *, struct net_device *);
Robert Love1875f272009-11-03 11:47:50 -080082static int fcoe_percpu_receive_thread(void *);
Robert Love1875f272009-11-03 11:47:50 -080083static void fcoe_percpu_clean(struct fc_lport *);
Robert Love5e4f8fe2010-05-07 15:18:35 -070084static int fcoe_link_speed_update(struct fc_lport *);
Robert Love1875f272009-11-03 11:47:50 -080085static int fcoe_link_ok(struct fc_lport *);
Vasu Devfdd78022009-03-17 11:42:24 -070086
87static struct fc_lport *fcoe_hostlist_lookup(const struct net_device *);
88static int fcoe_hostlist_add(const struct fc_lport *);
Vasu Devfdd78022009-03-17 11:42:24 -070089
Robert Love85b4aa42008-12-09 15:10:24 -080090static int fcoe_device_notification(struct notifier_block *, ulong, void *);
91static void fcoe_dev_setup(void);
92static void fcoe_dev_cleanup(void);
Robert Love1875f272009-11-03 11:47:50 -080093static struct fcoe_interface
94*fcoe_hostlist_lookup_port(const struct net_device *);
Robert Love85b4aa42008-12-09 15:10:24 -080095
Robert Love1875f272009-11-03 11:47:50 -080096static int fcoe_fip_recv(struct sk_buff *, struct net_device *,
97 struct packet_type *, struct net_device *);
98
99static void fcoe_fip_send(struct fcoe_ctlr *, struct sk_buff *);
100static void fcoe_update_src_mac(struct fc_lport *, u8 *);
101static u8 *fcoe_get_src_mac(struct fc_lport *);
102static void fcoe_destroy_work(struct work_struct *);
103
104static int fcoe_ddp_setup(struct fc_lport *, u16, struct scatterlist *,
105 unsigned int);
106static int fcoe_ddp_done(struct fc_lport *, u16);
Yi Zou71f89492011-06-20 16:59:10 -0700107static int fcoe_ddp_target(struct fc_lport *, u16, struct scatterlist *,
108 unsigned int);
Robert Love1875f272009-11-03 11:47:50 -0800109static int fcoe_cpu_callback(struct notifier_block *, unsigned long, void *);
john fastabend6f6c2aa2011-11-18 13:35:56 -0800110static int fcoe_dcb_app_notification(struct notifier_block *notifier,
111 ulong event, void *ptr);
Robert Love1875f272009-11-03 11:47:50 -0800112
Yi Zou78a58242011-01-28 16:05:16 -0800113static bool fcoe_match(struct net_device *netdev);
114static int fcoe_create(struct net_device *netdev, enum fip_state fip_mode);
115static int fcoe_destroy(struct net_device *netdev);
116static int fcoe_enable(struct net_device *netdev);
117static int fcoe_disable(struct net_device *netdev);
Robert Love1875f272009-11-03 11:47:50 -0800118
Robert Love1875f272009-11-03 11:47:50 -0800119static struct fc_seq *fcoe_elsct_send(struct fc_lport *,
120 u32 did, struct fc_frame *,
121 unsigned int op,
122 void (*resp)(struct fc_seq *,
123 struct fc_frame *,
124 void *),
125 void *, u32 timeout);
Chris Leech859b7b62009-11-20 14:54:47 -0800126static void fcoe_recv_frame(struct sk_buff *skb);
Robert Love1875f272009-11-03 11:47:50 -0800127
Yi Zoub84056b2009-11-20 14:55:19 -0800128static void fcoe_get_lesb(struct fc_lport *, struct fc_els_lesb *);
129
Robert Love1875f272009-11-03 11:47:50 -0800130/* notification function for packets from net device */
Robert Love85b4aa42008-12-09 15:10:24 -0800131static struct notifier_block fcoe_notifier = {
132 .notifier_call = fcoe_device_notification,
133};
134
Robert Love1875f272009-11-03 11:47:50 -0800135/* notification function for CPU hotplug events */
136static struct notifier_block fcoe_cpu_notifier = {
137 .notifier_call = fcoe_cpu_callback,
138};
139
john fastabend6f6c2aa2011-11-18 13:35:56 -0800140/* notification function for DCB events */
141static struct notifier_block dcb_notifier = {
142 .notifier_call = fcoe_dcb_app_notification,
143};
144
Yi Zou8ca86f82011-01-28 16:05:11 -0800145static struct scsi_transport_template *fcoe_nport_scsi_transport;
146static struct scsi_transport_template *fcoe_vport_scsi_transport;
Vasu Dev7f349142009-03-27 09:06:31 -0700147
Robert Love1875f272009-11-03 11:47:50 -0800148static int fcoe_vport_destroy(struct fc_vport *);
149static int fcoe_vport_create(struct fc_vport *, bool disabled);
150static int fcoe_vport_disable(struct fc_vport *, bool disable);
151static void fcoe_set_vport_symbolic_name(struct fc_vport *);
Joe Eykholt7d65b0d2010-03-12 16:08:02 -0800152static void fcoe_set_port_id(struct fc_lport *, u32, struct fc_frame *);
Robert Love1875f272009-11-03 11:47:50 -0800153
154static struct libfc_function_template fcoe_libfc_fcn_templ = {
155 .frame_send = fcoe_xmit,
156 .ddp_setup = fcoe_ddp_setup,
157 .ddp_done = fcoe_ddp_done,
Yi Zou71f89492011-06-20 16:59:10 -0700158 .ddp_target = fcoe_ddp_target,
Robert Love1875f272009-11-03 11:47:50 -0800159 .elsct_send = fcoe_elsct_send,
Yi Zoub84056b2009-11-20 14:55:19 -0800160 .get_lesb = fcoe_get_lesb,
Joe Eykholt7d65b0d2010-03-12 16:08:02 -0800161 .lport_set_port_id = fcoe_set_port_id,
Robert Love1875f272009-11-03 11:47:50 -0800162};
Chris Leech9a057532009-11-03 11:46:40 -0800163
Bart Van Assche7c9c6842012-01-13 17:26:25 -0800164static struct fc_function_template fcoe_nport_fc_functions = {
Vasu Dev7f349142009-03-27 09:06:31 -0700165 .show_host_node_name = 1,
166 .show_host_port_name = 1,
167 .show_host_supported_classes = 1,
168 .show_host_supported_fc4s = 1,
169 .show_host_active_fc4s = 1,
170 .show_host_maxframe_size = 1,
Neerav Parikh9f71af22012-01-22 17:30:16 -0800171 .show_host_serial_number = 1,
172 .show_host_manufacturer = 1,
173 .show_host_model = 1,
174 .show_host_model_description = 1,
175 .show_host_hardware_version = 1,
176 .show_host_driver_version = 1,
177 .show_host_firmware_version = 1,
178 .show_host_optionrom_version = 1,
Vasu Dev7f349142009-03-27 09:06:31 -0700179
180 .show_host_port_id = 1,
181 .show_host_supported_speeds = 1,
182 .get_host_speed = fc_get_host_speed,
183 .show_host_speed = 1,
184 .show_host_port_type = 1,
185 .get_host_port_state = fc_get_host_port_state,
186 .show_host_port_state = 1,
187 .show_host_symbolic_name = 1,
188
189 .dd_fcrport_size = sizeof(struct fc_rport_libfc_priv),
190 .show_rport_maxframe_size = 1,
191 .show_rport_supported_classes = 1,
192
193 .show_host_fabric_name = 1,
194 .show_starget_node_name = 1,
195 .show_starget_port_name = 1,
196 .show_starget_port_id = 1,
197 .set_rport_dev_loss_tmo = fc_set_rport_loss_tmo,
198 .show_rport_dev_loss_tmo = 1,
199 .get_fc_host_stats = fc_get_host_stats,
200 .issue_fc_host_lip = fcoe_reset,
201
202 .terminate_rport_io = fc_rport_terminate_io,
Chris Leech9a057532009-11-03 11:46:40 -0800203
204 .vport_create = fcoe_vport_create,
205 .vport_delete = fcoe_vport_destroy,
206 .vport_disable = fcoe_vport_disable,
Chris Leechdc8596d2009-11-03 11:47:18 -0800207 .set_vport_symbolic_name = fcoe_set_vport_symbolic_name,
Steve Maa51ab392009-11-03 11:47:34 -0800208
209 .bsg_request = fc_lport_bsg_request,
Vasu Dev7f349142009-03-27 09:06:31 -0700210};
211
Bart Van Assche7c9c6842012-01-13 17:26:25 -0800212static struct fc_function_template fcoe_vport_fc_functions = {
Chris Leeche9084bb2009-11-03 11:46:34 -0800213 .show_host_node_name = 1,
214 .show_host_port_name = 1,
215 .show_host_supported_classes = 1,
216 .show_host_supported_fc4s = 1,
217 .show_host_active_fc4s = 1,
218 .show_host_maxframe_size = 1,
Neerav Parikh7e5adcf2012-02-10 17:18:31 -0800219 .show_host_serial_number = 1,
220 .show_host_manufacturer = 1,
221 .show_host_model = 1,
222 .show_host_model_description = 1,
223 .show_host_hardware_version = 1,
224 .show_host_driver_version = 1,
225 .show_host_firmware_version = 1,
226 .show_host_optionrom_version = 1,
Chris Leeche9084bb2009-11-03 11:46:34 -0800227
228 .show_host_port_id = 1,
229 .show_host_supported_speeds = 1,
230 .get_host_speed = fc_get_host_speed,
231 .show_host_speed = 1,
232 .show_host_port_type = 1,
233 .get_host_port_state = fc_get_host_port_state,
234 .show_host_port_state = 1,
235 .show_host_symbolic_name = 1,
236
237 .dd_fcrport_size = sizeof(struct fc_rport_libfc_priv),
238 .show_rport_maxframe_size = 1,
239 .show_rport_supported_classes = 1,
240
241 .show_host_fabric_name = 1,
242 .show_starget_node_name = 1,
243 .show_starget_port_name = 1,
244 .show_starget_port_id = 1,
245 .set_rport_dev_loss_tmo = fc_set_rport_loss_tmo,
246 .show_rport_dev_loss_tmo = 1,
247 .get_fc_host_stats = fc_get_host_stats,
248 .issue_fc_host_lip = fcoe_reset,
249
250 .terminate_rport_io = fc_rport_terminate_io,
Steve Maa51ab392009-11-03 11:47:34 -0800251
252 .bsg_request = fc_lport_bsg_request,
Chris Leeche9084bb2009-11-03 11:46:34 -0800253};
254
Vasu Dev7f349142009-03-27 09:06:31 -0700255static struct scsi_host_template fcoe_shost_template = {
256 .module = THIS_MODULE,
257 .name = "FCoE Driver",
258 .proc_name = FCOE_NAME,
259 .queuecommand = fc_queuecommand,
260 .eh_abort_handler = fc_eh_abort,
261 .eh_device_reset_handler = fc_eh_device_reset,
262 .eh_host_reset_handler = fc_eh_host_reset,
263 .slave_alloc = fc_slave_alloc,
264 .change_queue_depth = fc_change_queue_depth,
265 .change_queue_type = fc_change_queue_type,
266 .this_id = -1,
Vasu Dev14caf442009-10-15 17:46:55 -0700267 .cmd_per_lun = 3,
Vasu Dev7f349142009-03-27 09:06:31 -0700268 .can_queue = FCOE_MAX_OUTSTANDING_COMMANDS,
269 .use_clustering = ENABLE_CLUSTERING,
270 .sg_tablesize = SG_ALL,
271 .max_sectors = 0xffff,
272};
273
Chris Leech54b649f2009-08-25 14:00:07 -0700274/**
Robert Love1875f272009-11-03 11:47:50 -0800275 * fcoe_interface_setup() - Setup a FCoE interface
276 * @fcoe: The new FCoE interface
277 * @netdev: The net device that the fcoe interface is on
Chris Leech54b649f2009-08-25 14:00:07 -0700278 *
279 * Returns : 0 for success
Chris Leech2e70e242009-08-25 14:00:23 -0700280 * Locking: must be called with the RTNL mutex held
Chris Leech54b649f2009-08-25 14:00:07 -0700281 */
282static int fcoe_interface_setup(struct fcoe_interface *fcoe,
283 struct net_device *netdev)
284{
285 struct fcoe_ctlr *fip = &fcoe->ctlr;
286 struct netdev_hw_addr *ha;
Yi Zou5bab87e2009-11-03 11:49:43 -0800287 struct net_device *real_dev;
Chris Leech54b649f2009-08-25 14:00:07 -0700288 u8 flogi_maddr[ETH_ALEN];
Yi Zoub7a727f2009-10-21 16:28:03 -0700289 const struct net_device_ops *ops;
Chris Leech54b649f2009-08-25 14:00:07 -0700290
291 fcoe->netdev = netdev;
292
Yi Zoub7a727f2009-10-21 16:28:03 -0700293 /* Let LLD initialize for FCoE */
294 ops = netdev->netdev_ops;
295 if (ops->ndo_fcoe_enable) {
296 if (ops->ndo_fcoe_enable(netdev))
297 FCOE_NETDEV_DBG(netdev, "Failed to enable FCoE"
298 " specific feature for LLD.\n");
299 }
300
Chris Leech54b649f2009-08-25 14:00:07 -0700301 /* Do not support for bonding device */
Jiri Pirkocc8bdf02011-03-01 20:05:35 +0000302 if (netdev->priv_flags & IFF_BONDING && netdev->flags & IFF_MASTER) {
john fastabend59d92512009-11-03 11:48:44 -0800303 FCOE_NETDEV_DBG(netdev, "Bonded interfaces not supported\n");
Chris Leech54b649f2009-08-25 14:00:07 -0700304 return -EOPNOTSUPP;
305 }
306
307 /* look for SAN MAC address, if multiple SAN MACs exist, only
308 * use the first one for SPMA */
Yi Zou5bab87e2009-11-03 11:49:43 -0800309 real_dev = (netdev->priv_flags & IFF_802_1Q_VLAN) ?
310 vlan_dev_real_dev(netdev) : netdev;
Vasu Devd1483bb2011-09-27 21:38:13 -0700311 fcoe->realdev = real_dev;
Chris Leech54b649f2009-08-25 14:00:07 -0700312 rcu_read_lock();
Yi Zou5bab87e2009-11-03 11:49:43 -0800313 for_each_dev_addr(real_dev, ha) {
Chris Leech54b649f2009-08-25 14:00:07 -0700314 if ((ha->type == NETDEV_HW_ADDR_T_SAN) &&
Yi Zoubf361702009-11-03 11:49:38 -0800315 (is_valid_ether_addr(ha->addr))) {
Chris Leech54b649f2009-08-25 14:00:07 -0700316 memcpy(fip->ctl_src_addr, ha->addr, ETH_ALEN);
317 fip->spma = 1;
318 break;
319 }
320 }
321 rcu_read_unlock();
322
323 /* setup Source Mac Address */
324 if (!fip->spma)
325 memcpy(fip->ctl_src_addr, netdev->dev_addr, netdev->addr_len);
326
327 /*
328 * Add FCoE MAC address as second unicast MAC address
329 * or enter promiscuous mode if not capable of listening
330 * for multiple unicast MACs.
331 */
Chris Leech54b649f2009-08-25 14:00:07 -0700332 memcpy(flogi_maddr, (u8[6]) FC_FCOE_FLOGI_MAC, ETH_ALEN);
Jiri Pirkoa748ee22010-04-01 21:22:09 +0000333 dev_uc_add(netdev, flogi_maddr);
Chris Leech54b649f2009-08-25 14:00:07 -0700334 if (fip->spma)
Jiri Pirkoa748ee22010-04-01 21:22:09 +0000335 dev_uc_add(netdev, fip->ctl_src_addr);
Joe Eykholte10f8c62010-07-20 15:20:30 -0700336 if (fip->mode == FIP_MODE_VN2VN) {
337 dev_mc_add(netdev, FIP_ALL_VN2VN_MACS);
338 dev_mc_add(netdev, FIP_ALL_P2P_MACS);
339 } else
340 dev_mc_add(netdev, FIP_ALL_ENODE_MACS);
Chris Leech54b649f2009-08-25 14:00:07 -0700341
342 /*
343 * setup the receive function from ethernet driver
344 * on the ethertype for the given device
345 */
346 fcoe->fcoe_packet_type.func = fcoe_rcv;
347 fcoe->fcoe_packet_type.type = __constant_htons(ETH_P_FCOE);
348 fcoe->fcoe_packet_type.dev = netdev;
349 dev_add_pack(&fcoe->fcoe_packet_type);
350
351 fcoe->fip_packet_type.func = fcoe_fip_recv;
352 fcoe->fip_packet_type.type = htons(ETH_P_FIP);
353 fcoe->fip_packet_type.dev = netdev;
354 dev_add_pack(&fcoe->fip_packet_type);
355
356 return 0;
357}
358
Vasu Dev7f349142009-03-27 09:06:31 -0700359/**
Robert Love1875f272009-11-03 11:47:50 -0800360 * fcoe_interface_create() - Create a FCoE interface on a net device
361 * @netdev: The net device to create the FCoE interface on
Joe Eykholt1dd454d2010-07-20 15:20:46 -0700362 * @fip_mode: The mode to use for FIP
Chris Leech030f4e02009-08-25 14:00:02 -0700363 *
364 * Returns: pointer to a struct fcoe_interface or NULL on error
365 */
Joe Eykholt1dd454d2010-07-20 15:20:46 -0700366static struct fcoe_interface *fcoe_interface_create(struct net_device *netdev,
367 enum fip_state fip_mode)
Chris Leech030f4e02009-08-25 14:00:02 -0700368{
369 struct fcoe_interface *fcoe;
john fastabend59d92512009-11-03 11:48:44 -0800370 int err;
Chris Leech030f4e02009-08-25 14:00:02 -0700371
Robert Love7287fb92011-01-28 16:03:47 -0800372 if (!try_module_get(THIS_MODULE)) {
373 FCOE_NETDEV_DBG(netdev,
374 "Could not get a reference to the module\n");
375 fcoe = ERR_PTR(-EBUSY);
376 goto out;
377 }
378
Chris Leech030f4e02009-08-25 14:00:02 -0700379 fcoe = kzalloc(sizeof(*fcoe), GFP_KERNEL);
380 if (!fcoe) {
381 FCOE_NETDEV_DBG(netdev, "Could not allocate fcoe structure\n");
Robert Love7287fb92011-01-28 16:03:47 -0800382 fcoe = ERR_PTR(-ENOMEM);
Robert Love6f687942012-02-10 17:18:36 -0800383 goto out_putmod;
Chris Leech030f4e02009-08-25 14:00:02 -0700384 }
385
Chris Leech2e70e242009-08-25 14:00:23 -0700386 dev_hold(netdev);
Chris Leech54b649f2009-08-25 14:00:07 -0700387
388 /*
389 * Initialize FIP.
390 */
Joe Eykholt1dd454d2010-07-20 15:20:46 -0700391 fcoe_ctlr_init(&fcoe->ctlr, fip_mode);
Chris Leech54b649f2009-08-25 14:00:07 -0700392 fcoe->ctlr.send = fcoe_fip_send;
393 fcoe->ctlr.update_mac = fcoe_update_src_mac;
Chris Leech11b56182009-11-03 11:46:29 -0800394 fcoe->ctlr.get_src_addr = fcoe_get_src_mac;
Chris Leech54b649f2009-08-25 14:00:07 -0700395
john fastabend59d92512009-11-03 11:48:44 -0800396 err = fcoe_interface_setup(fcoe, netdev);
397 if (err) {
398 fcoe_ctlr_destroy(&fcoe->ctlr);
399 kfree(fcoe);
400 dev_put(netdev);
Robert Love7287fb92011-01-28 16:03:47 -0800401 fcoe = ERR_PTR(err);
Robert Love6f687942012-02-10 17:18:36 -0800402 goto out_putmod;
john fastabend59d92512009-11-03 11:48:44 -0800403 }
Chris Leech030f4e02009-08-25 14:00:02 -0700404
Robert Love7287fb92011-01-28 16:03:47 -0800405 goto out;
406
Robert Love6f687942012-02-10 17:18:36 -0800407out_putmod:
Robert Love7287fb92011-01-28 16:03:47 -0800408 module_put(THIS_MODULE);
409out:
Chris Leech030f4e02009-08-25 14:00:02 -0700410 return fcoe;
411}
412
413/**
Vasu Dev433eba02012-04-20 12:16:27 -0700414 * fcoe_interface_remove() - remove FCoE interface from netdev
Vasu Devf04ca1b2011-04-01 16:06:45 -0700415 * @fcoe: The FCoE interface to be cleaned up
416 *
417 * Caller must be holding the RTNL mutex
418 */
Vasu Dev433eba02012-04-20 12:16:27 -0700419static void fcoe_interface_remove(struct fcoe_interface *fcoe)
Vasu Devf04ca1b2011-04-01 16:06:45 -0700420{
421 struct net_device *netdev = fcoe->netdev;
422 struct fcoe_ctlr *fip = &fcoe->ctlr;
423 u8 flogi_maddr[ETH_ALEN];
424 const struct net_device_ops *ops;
Vasu Devf04ca1b2011-04-01 16:06:45 -0700425
426 /*
427 * Don't listen for Ethernet packets anymore.
428 * synchronize_net() ensures that the packet handlers are not running
429 * on another CPU. dev_remove_pack() would do that, this calls the
430 * unsyncronized version __dev_remove_pack() to avoid multiple delays.
431 */
432 __dev_remove_pack(&fcoe->fcoe_packet_type);
433 __dev_remove_pack(&fcoe->fip_packet_type);
434 synchronize_net();
435
436 /* Delete secondary MAC addresses */
437 memcpy(flogi_maddr, (u8[6]) FC_FCOE_FLOGI_MAC, ETH_ALEN);
438 dev_uc_del(netdev, flogi_maddr);
439 if (fip->spma)
440 dev_uc_del(netdev, fip->ctl_src_addr);
441 if (fip->mode == FIP_MODE_VN2VN) {
442 dev_mc_del(netdev, FIP_ALL_VN2VN_MACS);
443 dev_mc_del(netdev, FIP_ALL_P2P_MACS);
444 } else
445 dev_mc_del(netdev, FIP_ALL_ENODE_MACS);
446
Vasu Devf04ca1b2011-04-01 16:06:45 -0700447 /* Tell the LLD we are done w/ FCoE */
448 ops = netdev->netdev_ops;
449 if (ops->ndo_fcoe_disable) {
450 if (ops->ndo_fcoe_disable(netdev))
451 FCOE_NETDEV_DBG(netdev, "Failed to disable FCoE"
452 " specific feature for LLD.\n");
453 }
Vasu Dev433eba02012-04-20 12:16:27 -0700454 fcoe->removed = 1;
455}
Neerav Parikhb2085a42011-06-20 16:59:51 -0700456
Vasu Dev433eba02012-04-20 12:16:27 -0700457
458/**
459 * fcoe_interface_cleanup() - Clean up a FCoE interface
460 * @fcoe: The FCoE interface to be cleaned up
461 */
462static void fcoe_interface_cleanup(struct fcoe_interface *fcoe)
463{
464 struct net_device *netdev = fcoe->netdev;
465 struct fcoe_ctlr *fip = &fcoe->ctlr;
466
467 rtnl_lock();
468 if (!fcoe->removed)
469 fcoe_interface_remove(fcoe);
Robert Love848e7d52011-08-25 12:40:47 -0700470 rtnl_unlock();
471
Neerav Parikhb2085a42011-06-20 16:59:51 -0700472 /* Release the self-reference taken during fcoe_interface_create() */
Robert Love1a8ef412012-02-10 17:18:46 -0800473 /* tear-down the FCoE controller */
474 fcoe_ctlr_destroy(fip);
Vasu Dev3cab4462012-04-20 12:16:38 -0700475 scsi_host_put(fcoe->ctlr.lp->host);
Robert Love1a8ef412012-02-10 17:18:46 -0800476 kfree(fcoe);
477 dev_put(netdev);
478 module_put(THIS_MODULE);
Vasu Devf04ca1b2011-04-01 16:06:45 -0700479}
480
481/**
Robert Love1875f272009-11-03 11:47:50 -0800482 * fcoe_fip_recv() - Handler for received FIP frames
483 * @skb: The receive skb
484 * @netdev: The associated net device
485 * @ptype: The packet_type structure which was used to register this handler
486 * @orig_dev: The original net_device the the skb was received on.
487 * (in case dev is a bond)
Vasu Devab6b85c2009-05-17 12:33:08 +0000488 *
489 * Returns: 0 for success
490 */
Robert Love1875f272009-11-03 11:47:50 -0800491static int fcoe_fip_recv(struct sk_buff *skb, struct net_device *netdev,
Vasu Devab6b85c2009-05-17 12:33:08 +0000492 struct packet_type *ptype,
493 struct net_device *orig_dev)
494{
Chris Leech259ad852009-08-25 13:59:41 -0700495 struct fcoe_interface *fcoe;
Vasu Devab6b85c2009-05-17 12:33:08 +0000496
Chris Leech259ad852009-08-25 13:59:41 -0700497 fcoe = container_of(ptype, struct fcoe_interface, fip_packet_type);
Chris Leech3fe9a0b2009-08-25 13:59:46 -0700498 fcoe_ctlr_recv(&fcoe->ctlr, skb);
Vasu Devab6b85c2009-05-17 12:33:08 +0000499 return 0;
500}
501
502/**
Vasu Dev980f5152011-07-27 15:11:05 -0700503 * fcoe_port_send() - Send an Ethernet-encapsulated FIP/FCoE frame
504 * @port: The FCoE port
505 * @skb: The FIP/FCoE packet to be sent
506 */
507static void fcoe_port_send(struct fcoe_port *port, struct sk_buff *skb)
508{
509 if (port->fcoe_pending_queue.qlen)
510 fcoe_check_wait_queue(port->lport, skb);
511 else if (fcoe_start_io(skb))
512 fcoe_check_wait_queue(port->lport, skb);
513}
514
515/**
Robert Love1875f272009-11-03 11:47:50 -0800516 * fcoe_fip_send() - Send an Ethernet-encapsulated FIP frame
517 * @fip: The FCoE controller
518 * @skb: The FIP packet to be sent
Vasu Devab6b85c2009-05-17 12:33:08 +0000519 */
520static void fcoe_fip_send(struct fcoe_ctlr *fip, struct sk_buff *skb)
521{
Chris Leech3fe9a0b2009-08-25 13:59:46 -0700522 skb->dev = fcoe_from_ctlr(fip)->netdev;
Vasu Dev980f5152011-07-27 15:11:05 -0700523 fcoe_port_send(lport_priv(fip->lp), skb);
Vasu Devab6b85c2009-05-17 12:33:08 +0000524}
525
526/**
Robert Love1875f272009-11-03 11:47:50 -0800527 * fcoe_update_src_mac() - Update the Ethernet MAC filters
528 * @lport: The local port to update the source MAC on
529 * @addr: Unicast MAC address to add
Vasu Devab6b85c2009-05-17 12:33:08 +0000530 *
531 * Remove any previously-set unicast MAC filter.
532 * Add secondary FCoE MAC address filter for our OUI.
533 */
Chris Leech11b56182009-11-03 11:46:29 -0800534static void fcoe_update_src_mac(struct fc_lport *lport, u8 *addr)
Vasu Devab6b85c2009-05-17 12:33:08 +0000535{
Chris Leech11b56182009-11-03 11:46:29 -0800536 struct fcoe_port *port = lport_priv(lport);
Bhanu Prakash Gollapudi8597ae82011-01-28 16:05:37 -0800537 struct fcoe_interface *fcoe = port->priv;
Vasu Devab6b85c2009-05-17 12:33:08 +0000538
Vasu Devab6b85c2009-05-17 12:33:08 +0000539 rtnl_lock();
Chris Leech11b56182009-11-03 11:46:29 -0800540 if (!is_zero_ether_addr(port->data_src_addr))
Jiri Pirkoa748ee22010-04-01 21:22:09 +0000541 dev_uc_del(fcoe->netdev, port->data_src_addr);
Chris Leech11b56182009-11-03 11:46:29 -0800542 if (!is_zero_ether_addr(addr))
Jiri Pirkoa748ee22010-04-01 21:22:09 +0000543 dev_uc_add(fcoe->netdev, addr);
Chris Leech11b56182009-11-03 11:46:29 -0800544 memcpy(port->data_src_addr, addr, ETH_ALEN);
Vasu Devab6b85c2009-05-17 12:33:08 +0000545 rtnl_unlock();
546}
547
548/**
Chris Leech11b56182009-11-03 11:46:29 -0800549 * fcoe_get_src_mac() - return the Ethernet source address for an lport
550 * @lport: libfc lport
551 */
552static u8 *fcoe_get_src_mac(struct fc_lport *lport)
553{
554 struct fcoe_port *port = lport_priv(lport);
555
556 return port->data_src_addr;
557}
558
559/**
Robert Love1875f272009-11-03 11:47:50 -0800560 * fcoe_lport_config() - Set up a local port
561 * @lport: The local port to be setup
Vasu Dev7f349142009-03-27 09:06:31 -0700562 *
563 * Returns: 0 for success
564 */
Robert Love1875f272009-11-03 11:47:50 -0800565static int fcoe_lport_config(struct fc_lport *lport)
Vasu Dev7f349142009-03-27 09:06:31 -0700566{
Robert Love1875f272009-11-03 11:47:50 -0800567 lport->link_up = 0;
568 lport->qfull = 0;
569 lport->max_retry_count = 3;
570 lport->max_rport_retry_count = 3;
571 lport->e_d_tov = 2 * 1000; /* FC-FS default */
572 lport->r_a_tov = 2 * 2 * 1000;
573 lport->service_params = (FCP_SPPF_INIT_FCN | FCP_SPPF_RD_XRDY_DIS |
574 FCP_SPPF_RETRY | FCP_SPPF_CONF_COMPL);
575 lport->does_npiv = 1;
Vasu Dev7f349142009-03-27 09:06:31 -0700576
Robert Love1875f272009-11-03 11:47:50 -0800577 fc_lport_init_stats(lport);
Vasu Dev7f349142009-03-27 09:06:31 -0700578
579 /* lport fc_lport related configuration */
Robert Love1875f272009-11-03 11:47:50 -0800580 fc_lport_config(lport);
Vasu Dev7f349142009-03-27 09:06:31 -0700581
582 /* offload related configuration */
Robert Love1875f272009-11-03 11:47:50 -0800583 lport->crc_offload = 0;
584 lport->seq_offload = 0;
585 lport->lro_enabled = 0;
586 lport->lro_xid = 0;
587 lport->lso_max = 0;
Vasu Dev7f349142009-03-27 09:06:31 -0700588
589 return 0;
590}
591
592/**
Yi Zou54a5b212010-07-20 15:21:17 -0700593 * fcoe_netdev_features_change - Updates the lport's offload flags based
594 * on the LLD netdev's FCoE feature flags
595 */
596static void fcoe_netdev_features_change(struct fc_lport *lport,
597 struct net_device *netdev)
598{
599 mutex_lock(&lport->lp_mutex);
600
601 if (netdev->features & NETIF_F_SG)
602 lport->sg_supp = 1;
603 else
604 lport->sg_supp = 0;
605
606 if (netdev->features & NETIF_F_FCOE_CRC) {
607 lport->crc_offload = 1;
608 FCOE_NETDEV_DBG(netdev, "Supports FCCRC offload\n");
609 } else {
610 lport->crc_offload = 0;
611 }
612
613 if (netdev->features & NETIF_F_FSO) {
614 lport->seq_offload = 1;
615 lport->lso_max = netdev->gso_max_size;
616 FCOE_NETDEV_DBG(netdev, "Supports LSO for max len 0x%x\n",
617 lport->lso_max);
618 } else {
619 lport->seq_offload = 0;
620 lport->lso_max = 0;
621 }
622
623 if (netdev->fcoe_ddp_xid) {
624 lport->lro_enabled = 1;
625 lport->lro_xid = netdev->fcoe_ddp_xid;
626 FCOE_NETDEV_DBG(netdev, "Supports LRO for max xid 0x%x\n",
627 lport->lro_xid);
628 } else {
629 lport->lro_enabled = 0;
630 lport->lro_xid = 0;
631 }
632
633 mutex_unlock(&lport->lp_mutex);
634}
635
636/**
Robert Love1875f272009-11-03 11:47:50 -0800637 * fcoe_netdev_config() - Set up net devive for SW FCoE
638 * @lport: The local port that is associated with the net device
639 * @netdev: The associated net device
Vasu Dev7f349142009-03-27 09:06:31 -0700640 *
Robert Love1875f272009-11-03 11:47:50 -0800641 * Must be called after fcoe_lport_config() as it will use local port mutex
Vasu Dev7f349142009-03-27 09:06:31 -0700642 *
Robert Love1875f272009-11-03 11:47:50 -0800643 * Returns: 0 for success
Vasu Dev7f349142009-03-27 09:06:31 -0700644 */
Robert Love1875f272009-11-03 11:47:50 -0800645static int fcoe_netdev_config(struct fc_lport *lport, struct net_device *netdev)
Vasu Dev7f349142009-03-27 09:06:31 -0700646{
647 u32 mfs;
648 u64 wwnn, wwpn;
Chris Leech25024982009-08-25 13:59:35 -0700649 struct fcoe_interface *fcoe;
Chris Leech014f5c32009-08-25 13:59:30 -0700650 struct fcoe_port *port;
Vasu Dev7f349142009-03-27 09:06:31 -0700651
652 /* Setup lport private data to point to fcoe softc */
Robert Love1875f272009-11-03 11:47:50 -0800653 port = lport_priv(lport);
Bhanu Prakash Gollapudi8597ae82011-01-28 16:05:37 -0800654 fcoe = port->priv;
Vasu Dev7f349142009-03-27 09:06:31 -0700655
656 /*
657 * Determine max frame size based on underlying device and optional
658 * user-configured limit. If the MFS is too low, fcoe_link_ok()
659 * will return 0, so do this first.
660 */
Yi Zou7221d7e2009-10-21 16:27:52 -0700661 mfs = netdev->mtu;
662 if (netdev->features & NETIF_F_FCOE_MTU) {
663 mfs = FCOE_MTU;
664 FCOE_NETDEV_DBG(netdev, "Supports FCOE_MTU of %d bytes\n", mfs);
665 }
666 mfs -= (sizeof(struct fcoe_hdr) + sizeof(struct fcoe_crc_eof));
Robert Love1875f272009-11-03 11:47:50 -0800667 if (fc_set_mfs(lport, mfs))
Vasu Dev7f349142009-03-27 09:06:31 -0700668 return -EINVAL;
669
Vasu Dev7f349142009-03-27 09:06:31 -0700670 /* offload features support */
Yi Zou54a5b212010-07-20 15:21:17 -0700671 fcoe_netdev_features_change(lport, netdev);
Vasu Dev7f349142009-03-27 09:06:31 -0700672
Chris Leech014f5c32009-08-25 13:59:30 -0700673 skb_queue_head_init(&port->fcoe_pending_queue);
674 port->fcoe_pending_queue_active = 0;
Robert Love1875f272009-11-03 11:47:50 -0800675 setup_timer(&port->timer, fcoe_queue_timer, (unsigned long)lport);
Vasu Dev7f349142009-03-27 09:06:31 -0700676
Robert Love5e4f8fe2010-05-07 15:18:35 -0700677 fcoe_link_speed_update(lport);
678
Robert Love1875f272009-11-03 11:47:50 -0800679 if (!lport->vport) {
Yi Zoudcece412009-11-20 15:22:21 -0800680 if (fcoe_get_wwn(netdev, &wwnn, NETDEV_FCOE_WWNN))
681 wwnn = fcoe_wwn_from_mac(fcoe->ctlr.ctl_src_addr, 1, 0);
Robert Love1875f272009-11-03 11:47:50 -0800682 fc_set_wwnn(lport, wwnn);
Yi Zoudcece412009-11-20 15:22:21 -0800683 if (fcoe_get_wwn(netdev, &wwpn, NETDEV_FCOE_WWPN))
684 wwpn = fcoe_wwn_from_mac(fcoe->ctlr.ctl_src_addr,
Vasu Devcf4aebca2010-07-20 15:21:22 -0700685 2, 0);
Robert Love1875f272009-11-03 11:47:50 -0800686 fc_set_wwpn(lport, wwpn);
Chris Leech9a057532009-11-03 11:46:40 -0800687 }
Vasu Dev7f349142009-03-27 09:06:31 -0700688
Vasu Dev7f349142009-03-27 09:06:31 -0700689 return 0;
690}
691
692/**
Robert Love1875f272009-11-03 11:47:50 -0800693 * fcoe_shost_config() - Set up the SCSI host associated with a local port
694 * @lport: The local port
Robert Love1875f272009-11-03 11:47:50 -0800695 * @dev: The device associated with the SCSI host
Vasu Dev7f349142009-03-27 09:06:31 -0700696 *
697 * Must be called after fcoe_lport_config() and fcoe_netdev_config()
698 *
Robert Love1875f272009-11-03 11:47:50 -0800699 * Returns: 0 for success
Vasu Dev7f349142009-03-27 09:06:31 -0700700 */
Vasu Dev8ba00a42010-04-09 14:22:54 -0700701static int fcoe_shost_config(struct fc_lport *lport, struct device *dev)
Vasu Dev7f349142009-03-27 09:06:31 -0700702{
703 int rc = 0;
704
705 /* lport scsi host config */
Robert Love1875f272009-11-03 11:47:50 -0800706 lport->host->max_lun = FCOE_MAX_LUN;
707 lport->host->max_id = FCOE_MAX_FCP_TARGET;
708 lport->host->max_channel = 0;
Vasu Devda87bfa2010-04-09 14:22:59 -0700709 lport->host->max_cmd_len = FCOE_MAX_CMD_LEN;
710
Robert Love1875f272009-11-03 11:47:50 -0800711 if (lport->vport)
Yi Zou8ca86f82011-01-28 16:05:11 -0800712 lport->host->transportt = fcoe_vport_scsi_transport;
Chris Leeche9084bb2009-11-03 11:46:34 -0800713 else
Yi Zou8ca86f82011-01-28 16:05:11 -0800714 lport->host->transportt = fcoe_nport_scsi_transport;
Vasu Dev7f349142009-03-27 09:06:31 -0700715
716 /* add the new host to the SCSI-ml */
Robert Love1875f272009-11-03 11:47:50 -0800717 rc = scsi_add_host(lport->host, dev);
Vasu Dev7f349142009-03-27 09:06:31 -0700718 if (rc) {
Robert Love1875f272009-11-03 11:47:50 -0800719 FCOE_NETDEV_DBG(fcoe_netdev(lport), "fcoe_shost_config: "
Robert Loved5488eb2009-06-10 15:30:59 -0700720 "error on scsi_add_host\n");
Vasu Dev7f349142009-03-27 09:06:31 -0700721 return rc;
722 }
Chris Leech9a057532009-11-03 11:46:40 -0800723
Robert Love1875f272009-11-03 11:47:50 -0800724 if (!lport->vport)
Alexey Dobriyan4be929b2010-05-24 14:33:03 -0700725 fc_host_max_npiv_vports(lport->host) = USHRT_MAX;
Chris Leech9a057532009-11-03 11:46:40 -0800726
Robert Love1875f272009-11-03 11:47:50 -0800727 snprintf(fc_host_symbolic_name(lport->host), FC_SYMBOLIC_NAME_SIZE,
Chris Leech5baa17c2009-11-03 11:46:56 -0800728 "%s v%s over %s", FCOE_NAME, FCOE_VERSION,
Robert Love1875f272009-11-03 11:47:50 -0800729 fcoe_netdev(lport)->name);
Vasu Dev7f349142009-03-27 09:06:31 -0700730
731 return 0;
732}
733
Neerav Parikh6fef3902012-01-22 17:30:10 -0800734
735/**
736 * fcoe_fdmi_info() - Get FDMI related info from net devive for SW FCoE
737 * @lport: The local port that is associated with the net device
738 * @netdev: The associated net device
739 *
740 * Must be called after fcoe_shost_config() as it will use local port mutex
741 *
742 */
743static void fcoe_fdmi_info(struct fc_lport *lport, struct net_device *netdev)
744{
745 struct fcoe_interface *fcoe;
746 struct fcoe_port *port;
747 struct net_device *realdev;
748 int rc;
749 struct netdev_fcoe_hbainfo fdmi;
750
751 port = lport_priv(lport);
752 fcoe = port->priv;
753 realdev = fcoe->realdev;
754
755 if (!realdev)
756 return;
757
758 /* No FDMI state m/c for NPIV ports */
759 if (lport->vport)
760 return;
761
762 if (realdev->netdev_ops->ndo_fcoe_get_hbainfo) {
763 memset(&fdmi, 0, sizeof(fdmi));
764 rc = realdev->netdev_ops->ndo_fcoe_get_hbainfo(realdev,
765 &fdmi);
766 if (rc) {
767 printk(KERN_INFO "fcoe: Failed to retrieve FDMI "
768 "information from netdev.\n");
769 return;
770 }
771
772 snprintf(fc_host_serial_number(lport->host),
773 FC_SERIAL_NUMBER_SIZE,
774 "%s",
775 fdmi.serial_number);
776 snprintf(fc_host_manufacturer(lport->host),
777 FC_SERIAL_NUMBER_SIZE,
778 "%s",
779 fdmi.manufacturer);
780 snprintf(fc_host_model(lport->host),
781 FC_SYMBOLIC_NAME_SIZE,
782 "%s",
783 fdmi.model);
784 snprintf(fc_host_model_description(lport->host),
785 FC_SYMBOLIC_NAME_SIZE,
786 "%s",
787 fdmi.model_description);
788 snprintf(fc_host_hardware_version(lport->host),
789 FC_VERSION_STRING_SIZE,
790 "%s",
791 fdmi.hardware_version);
792 snprintf(fc_host_driver_version(lport->host),
793 FC_VERSION_STRING_SIZE,
794 "%s",
795 fdmi.driver_version);
796 snprintf(fc_host_optionrom_version(lport->host),
797 FC_VERSION_STRING_SIZE,
798 "%s",
799 fdmi.optionrom_version);
800 snprintf(fc_host_firmware_version(lport->host),
801 FC_VERSION_STRING_SIZE,
802 "%s",
803 fdmi.firmware_version);
804
805 /* Enable FDMI lport states */
806 lport->fdmi_enabled = 1;
807 } else {
808 lport->fdmi_enabled = 0;
809 printk(KERN_INFO "fcoe: No FDMI support.\n");
810 }
811}
812
Robert Love1875f272009-11-03 11:47:50 -0800813/**
814 * fcoe_oem_match() - The match routine for the offloaded exchange manager
815 * @fp: The I/O frame
Vasu Devd7179682009-07-29 17:05:21 -0700816 *
Robert Love1875f272009-11-03 11:47:50 -0800817 * This routine will be associated with an exchange manager (EM). When
818 * the libfc exchange handling code is looking for an EM to use it will
819 * call this routine and pass it the frame that it wishes to send. This
820 * routine will return True if the associated EM is to be used and False
821 * if the echange code should continue looking for an EM.
822 *
823 * The offload EM that this routine is associated with will handle any
824 * packets that are for SCSI read requests.
825 *
Kiran Patil1ff99182011-06-20 16:59:15 -0700826 * This has been enhanced to work when FCoE stack is operating in target
827 * mode.
828 *
Robert Love1875f272009-11-03 11:47:50 -0800829 * Returns: True for read types I/O, otherwise returns false.
Vasu Devd7179682009-07-29 17:05:21 -0700830 */
Bart Van Assche7c9c6842012-01-13 17:26:25 -0800831static bool fcoe_oem_match(struct fc_frame *fp)
Vasu Devd7179682009-07-29 17:05:21 -0700832{
Kiran Patil1ff99182011-06-20 16:59:15 -0700833 struct fc_frame_header *fh = fc_frame_header_get(fp);
834 struct fcp_cmnd *fcp;
835
836 if (fc_fcp_is_read(fr_fsp(fp)) &&
837 (fr_fsp(fp)->data_len > fcoe_ddp_min))
838 return true;
Yi Zoua762dce2012-01-13 17:26:15 -0800839 else if ((fr_fsp(fp) == NULL) &&
840 (fh->fh_r_ctl == FC_RCTL_DD_UNSOL_CMD) &&
841 (ntohs(fh->fh_rx_id) == FC_XID_UNKNOWN)) {
Kiran Patil1ff99182011-06-20 16:59:15 -0700842 fcp = fc_frame_payload_get(fp, sizeof(*fcp));
Yi Zoua762dce2012-01-13 17:26:15 -0800843 if ((fcp->fc_flags & FCP_CFL_WRDATA) &&
844 (ntohl(fcp->fc_dl) > fcoe_ddp_min))
Kiran Patil1ff99182011-06-20 16:59:15 -0700845 return true;
846 }
847 return false;
Vasu Devd7179682009-07-29 17:05:21 -0700848}
849
Vasu Dev7f349142009-03-27 09:06:31 -0700850/**
Robert Love1875f272009-11-03 11:47:50 -0800851 * fcoe_em_config() - Allocate and configure an exchange manager
852 * @lport: The local port that the new EM will be associated with
Vasu Dev7f349142009-03-27 09:06:31 -0700853 *
Robert Love1875f272009-11-03 11:47:50 -0800854 * Returns: 0 on success
Vasu Dev7f349142009-03-27 09:06:31 -0700855 */
Robert Love1875f272009-11-03 11:47:50 -0800856static inline int fcoe_em_config(struct fc_lport *lport)
Vasu Dev7f349142009-03-27 09:06:31 -0700857{
Robert Love1875f272009-11-03 11:47:50 -0800858 struct fcoe_port *port = lport_priv(lport);
Bhanu Prakash Gollapudi8597ae82011-01-28 16:05:37 -0800859 struct fcoe_interface *fcoe = port->priv;
Chris Leech25024982009-08-25 13:59:35 -0700860 struct fcoe_interface *oldfcoe = NULL;
Vasu Dev1d1b88d2009-07-29 17:05:45 -0700861 struct net_device *old_real_dev, *cur_real_dev;
Vasu Devd7179682009-07-29 17:05:21 -0700862 u16 min_xid = FCOE_MIN_XID;
863 u16 max_xid = FCOE_MAX_XID;
864
865 /*
866 * Check if need to allocate an em instance for
867 * offload exchange ids to be shared across all VN_PORTs/lport.
868 */
Robert Love1875f272009-11-03 11:47:50 -0800869 if (!lport->lro_enabled || !lport->lro_xid ||
870 (lport->lro_xid >= max_xid)) {
871 lport->lro_xid = 0;
Vasu Devd7179682009-07-29 17:05:21 -0700872 goto skip_oem;
873 }
874
875 /*
876 * Reuse existing offload em instance in case
Vasu Dev1d1b88d2009-07-29 17:05:45 -0700877 * it is already allocated on real eth device
Vasu Devd7179682009-07-29 17:05:21 -0700878 */
Chris Leech25024982009-08-25 13:59:35 -0700879 if (fcoe->netdev->priv_flags & IFF_802_1Q_VLAN)
880 cur_real_dev = vlan_dev_real_dev(fcoe->netdev);
Vasu Dev1d1b88d2009-07-29 17:05:45 -0700881 else
Chris Leech25024982009-08-25 13:59:35 -0700882 cur_real_dev = fcoe->netdev;
Vasu Dev1d1b88d2009-07-29 17:05:45 -0700883
Chris Leech25024982009-08-25 13:59:35 -0700884 list_for_each_entry(oldfcoe, &fcoe_hostlist, list) {
Chris Leech25024982009-08-25 13:59:35 -0700885 if (oldfcoe->netdev->priv_flags & IFF_802_1Q_VLAN)
886 old_real_dev = vlan_dev_real_dev(oldfcoe->netdev);
Vasu Dev1d1b88d2009-07-29 17:05:45 -0700887 else
Chris Leech25024982009-08-25 13:59:35 -0700888 old_real_dev = oldfcoe->netdev;
Vasu Dev1d1b88d2009-07-29 17:05:45 -0700889
890 if (cur_real_dev == old_real_dev) {
Chris Leech991cbb62009-08-25 13:59:51 -0700891 fcoe->oem = oldfcoe->oem;
Vasu Devd7179682009-07-29 17:05:21 -0700892 break;
893 }
894 }
895
Chris Leech991cbb62009-08-25 13:59:51 -0700896 if (fcoe->oem) {
Robert Love1875f272009-11-03 11:47:50 -0800897 if (!fc_exch_mgr_add(lport, fcoe->oem, fcoe_oem_match)) {
Vasu Devd7179682009-07-29 17:05:21 -0700898 printk(KERN_ERR "fcoe_em_config: failed to add "
899 "offload em:%p on interface:%s\n",
Chris Leech991cbb62009-08-25 13:59:51 -0700900 fcoe->oem, fcoe->netdev->name);
Vasu Devd7179682009-07-29 17:05:21 -0700901 return -ENOMEM;
902 }
903 } else {
Robert Love1875f272009-11-03 11:47:50 -0800904 fcoe->oem = fc_exch_mgr_alloc(lport, FC_CLASS_3,
905 FCOE_MIN_XID, lport->lro_xid,
906 fcoe_oem_match);
Chris Leech991cbb62009-08-25 13:59:51 -0700907 if (!fcoe->oem) {
Vasu Devd7179682009-07-29 17:05:21 -0700908 printk(KERN_ERR "fcoe_em_config: failed to allocate "
909 "em for offload exches on interface:%s\n",
Chris Leech25024982009-08-25 13:59:35 -0700910 fcoe->netdev->name);
Vasu Devd7179682009-07-29 17:05:21 -0700911 return -ENOMEM;
912 }
913 }
914
915 /*
916 * Exclude offload EM xid range from next EM xid range.
917 */
Robert Love1875f272009-11-03 11:47:50 -0800918 min_xid += lport->lro_xid + 1;
Vasu Devd7179682009-07-29 17:05:21 -0700919
920skip_oem:
Robert Love1875f272009-11-03 11:47:50 -0800921 if (!fc_exch_mgr_alloc(lport, FC_CLASS_3, min_xid, max_xid, NULL)) {
Vasu Devd7179682009-07-29 17:05:21 -0700922 printk(KERN_ERR "fcoe_em_config: failed to "
Chris Leech25024982009-08-25 13:59:35 -0700923 "allocate em on interface %s\n", fcoe->netdev->name);
Vasu Dev7f349142009-03-27 09:06:31 -0700924 return -ENOMEM;
Vasu Devd7179682009-07-29 17:05:21 -0700925 }
Vasu Dev7f349142009-03-27 09:06:31 -0700926
927 return 0;
928}
929
930/**
Robert Love1875f272009-11-03 11:47:50 -0800931 * fcoe_if_destroy() - Tear down a SW FCoE instance
932 * @lport: The local port to be destroyed
Vasu Dev34ce27b2010-05-07 15:18:46 -0700933 *
Vasu Dev7f349142009-03-27 09:06:31 -0700934 */
Chris Leechaf7f85d2009-08-25 13:59:24 -0700935static void fcoe_if_destroy(struct fc_lport *lport)
Vasu Dev7f349142009-03-27 09:06:31 -0700936{
Neerav Parikhb2085a42011-06-20 16:59:51 -0700937 struct fcoe_port *port = lport_priv(lport);
938 struct fcoe_interface *fcoe = port->priv;
939 struct net_device *netdev = fcoe->netdev;
940
941 FCOE_NETDEV_DBG(netdev, "Destroying interface\n");
942
943 /* Logout of the fabric */
944 fc_fabric_logoff(lport);
945
946 /* Cleanup the fc_lport */
947 fc_lport_destroy(lport);
948
949 /* Stop the transmit retry timer */
950 del_timer_sync(&port->timer);
951
952 /* Free existing transmit skbs */
953 fcoe_clean_pending_queue(lport);
954
955 rtnl_lock();
956 if (!is_zero_ether_addr(port->data_src_addr))
957 dev_uc_del(netdev, port->data_src_addr);
Vasu Dev433eba02012-04-20 12:16:27 -0700958 if (lport->vport)
959 synchronize_net();
960 else
961 fcoe_interface_remove(fcoe);
Neerav Parikhb2085a42011-06-20 16:59:51 -0700962 rtnl_unlock();
963
Chris Leech54b649f2009-08-25 14:00:07 -0700964 /* Free queued packets for the per-CPU receive threads */
965 fcoe_percpu_clean(lport);
966
Vasu Dev7f349142009-03-27 09:06:31 -0700967 /* Detach from the scsi-ml */
Chris Leechaf7f85d2009-08-25 13:59:24 -0700968 fc_remove_host(lport->host);
969 scsi_remove_host(lport->host);
Vasu Dev7f349142009-03-27 09:06:31 -0700970
Yi Zou80e736f2010-11-30 16:18:07 -0800971 /* Destroy lport scsi_priv */
972 fc_fcp_destroy(lport);
973
Vasu Dev7f349142009-03-27 09:06:31 -0700974 /* There are no more rports or I/O, free the EM */
Chris Leechaf7f85d2009-08-25 13:59:24 -0700975 fc_exch_mgr_free(lport);
Vasu Dev7f349142009-03-27 09:06:31 -0700976
Vasu Dev7f349142009-03-27 09:06:31 -0700977 /* Free memory used by statistical counters */
Chris Leechaf7f85d2009-08-25 13:59:24 -0700978 fc_lport_free_stats(lport);
Vasu Dev7f349142009-03-27 09:06:31 -0700979
Vasu Dev3cab4462012-04-20 12:16:38 -0700980 /*
981 * Release the Scsi_Host for vport but hold on to
982 * master lport until it fcoe interface fully cleaned-up.
983 */
984 if (lport->vport)
985 scsi_host_put(lport->host);
Vasu Dev7f349142009-03-27 09:06:31 -0700986}
987
Robert Love1875f272009-11-03 11:47:50 -0800988/**
989 * fcoe_ddp_setup() - Call a LLD's ddp_setup through the net device
990 * @lport: The local port to setup DDP for
991 * @xid: The exchange ID for this DDP transfer
992 * @sgl: The scatterlist describing this transfer
993 * @sgc: The number of sg items
Vasu Dev7f349142009-03-27 09:06:31 -0700994 *
Robert Love1875f272009-11-03 11:47:50 -0800995 * Returns: 0 if the DDP context was not configured
Vasu Dev7f349142009-03-27 09:06:31 -0700996 */
Robert Love1875f272009-11-03 11:47:50 -0800997static int fcoe_ddp_setup(struct fc_lport *lport, u16 xid,
998 struct scatterlist *sgl, unsigned int sgc)
Vasu Dev7f349142009-03-27 09:06:31 -0700999{
Robert Love1875f272009-11-03 11:47:50 -08001000 struct net_device *netdev = fcoe_netdev(lport);
Vasu Dev7f349142009-03-27 09:06:31 -07001001
Robert Love1875f272009-11-03 11:47:50 -08001002 if (netdev->netdev_ops->ndo_fcoe_ddp_setup)
1003 return netdev->netdev_ops->ndo_fcoe_ddp_setup(netdev,
1004 xid, sgl,
1005 sgc);
Vasu Dev7f349142009-03-27 09:06:31 -07001006
1007 return 0;
1008}
1009
Vasu Dev7f349142009-03-27 09:06:31 -07001010/**
Yi Zou71f89492011-06-20 16:59:10 -07001011 * fcoe_ddp_target() - Call a LLD's ddp_target through the net device
1012 * @lport: The local port to setup DDP for
1013 * @xid: The exchange ID for this DDP transfer
1014 * @sgl: The scatterlist describing this transfer
1015 * @sgc: The number of sg items
1016 *
1017 * Returns: 0 if the DDP context was not configured
1018 */
1019static int fcoe_ddp_target(struct fc_lport *lport, u16 xid,
1020 struct scatterlist *sgl, unsigned int sgc)
1021{
1022 struct net_device *netdev = fcoe_netdev(lport);
1023
1024 if (netdev->netdev_ops->ndo_fcoe_ddp_target)
1025 return netdev->netdev_ops->ndo_fcoe_ddp_target(netdev, xid,
1026 sgl, sgc);
1027
1028 return 0;
1029}
1030
1031
1032/**
Robert Love1875f272009-11-03 11:47:50 -08001033 * fcoe_ddp_done() - Call a LLD's ddp_done through the net device
1034 * @lport: The local port to complete DDP on
1035 * @xid: The exchange ID for this DDP transfer
Vasu Dev7f349142009-03-27 09:06:31 -07001036 *
Robert Love1875f272009-11-03 11:47:50 -08001037 * Returns: the length of data that have been completed by DDP
1038 */
1039static int fcoe_ddp_done(struct fc_lport *lport, u16 xid)
1040{
1041 struct net_device *netdev = fcoe_netdev(lport);
1042
1043 if (netdev->netdev_ops->ndo_fcoe_ddp_done)
1044 return netdev->netdev_ops->ndo_fcoe_ddp_done(netdev, xid);
1045 return 0;
1046}
1047
1048/**
1049 * fcoe_if_create() - Create a FCoE instance on an interface
1050 * @fcoe: The FCoE interface to create a local port on
1051 * @parent: The device pointer to be the parent in sysfs for the SCSI host
1052 * @npiv: Indicates if the port is a vport or not
Vasu Dev7f349142009-03-27 09:06:31 -07001053 *
Robert Love1875f272009-11-03 11:47:50 -08001054 * Creates a fc_lport instance and a Scsi_Host instance and configure them.
1055 *
1056 * Returns: The allocated fc_lport or an error pointer
Vasu Dev7f349142009-03-27 09:06:31 -07001057 */
Chris Leech030f4e02009-08-25 14:00:02 -07001058static struct fc_lport *fcoe_if_create(struct fcoe_interface *fcoe,
Chris Leech9a057532009-11-03 11:46:40 -08001059 struct device *parent, int npiv)
Vasu Dev7f349142009-03-27 09:06:31 -07001060{
Robert Love1875f272009-11-03 11:47:50 -08001061 struct net_device *netdev = fcoe->netdev;
Vasu Dev72fa3962011-02-25 15:03:01 -08001062 struct fc_lport *lport, *n_port;
Chris Leech014f5c32009-08-25 13:59:30 -07001063 struct fcoe_port *port;
Vasu Dev72fa3962011-02-25 15:03:01 -08001064 struct Scsi_Host *shost;
Robert Love1875f272009-11-03 11:47:50 -08001065 int rc;
Chris Leech9a057532009-11-03 11:46:40 -08001066 /*
1067 * parent is only a vport if npiv is 1,
1068 * but we'll only use vport in that case so go ahead and set it
1069 */
1070 struct fc_vport *vport = dev_to_vport(parent);
Vasu Dev7f349142009-03-27 09:06:31 -07001071
Robert Loved5488eb2009-06-10 15:30:59 -07001072 FCOE_NETDEV_DBG(netdev, "Create Interface\n");
Vasu Dev7f349142009-03-27 09:06:31 -07001073
Vasu Dev72fa3962011-02-25 15:03:01 -08001074 if (!npiv)
1075 lport = libfc_host_alloc(&fcoe_shost_template, sizeof(*port));
1076 else
1077 lport = libfc_vport_create(vport, sizeof(*port));
1078
Chris Leech86221962009-11-03 11:46:08 -08001079 if (!lport) {
Chris Leech014f5c32009-08-25 13:59:30 -07001080 FCOE_NETDEV_DBG(netdev, "Could not allocate host structure\n");
1081 rc = -ENOMEM;
Chris Leech030f4e02009-08-25 14:00:02 -07001082 goto out;
Chris Leech014f5c32009-08-25 13:59:30 -07001083 }
Chris Leech014f5c32009-08-25 13:59:30 -07001084 port = lport_priv(lport);
Chris Leech2e70e242009-08-25 14:00:23 -07001085 port->lport = lport;
Bhanu Prakash Gollapudi8597ae82011-01-28 16:05:37 -08001086 port->priv = fcoe;
1087 port->max_queue_depth = FCOE_MAX_QUEUE_DEPTH;
1088 port->min_queue_depth = FCOE_MIN_QUEUE_DEPTH;
Chris Leech2e70e242009-08-25 14:00:23 -07001089 INIT_WORK(&port->destroy_work, fcoe_destroy_work);
Vasu Dev7f349142009-03-27 09:06:31 -07001090
Robert Love1875f272009-11-03 11:47:50 -08001091 /* configure a fc_lport including the exchange manager */
Chris Leechaf7f85d2009-08-25 13:59:24 -07001092 rc = fcoe_lport_config(lport);
Vasu Dev7f349142009-03-27 09:06:31 -07001093 if (rc) {
Robert Loved5488eb2009-06-10 15:30:59 -07001094 FCOE_NETDEV_DBG(netdev, "Could not configure lport for the "
1095 "interface\n");
Vasu Dev7f349142009-03-27 09:06:31 -07001096 goto out_host_put;
1097 }
1098
Chris Leech9a057532009-11-03 11:46:40 -08001099 if (npiv) {
Chris Leech9f8f3aa2010-04-09 14:23:16 -07001100 FCOE_NETDEV_DBG(netdev, "Setting vport names, "
1101 "%16.16llx %16.16llx\n",
Robert Love1875f272009-11-03 11:47:50 -08001102 vport->node_name, vport->port_name);
Chris Leech9a057532009-11-03 11:46:40 -08001103 fc_set_wwnn(lport, vport->node_name);
1104 fc_set_wwpn(lport, vport->port_name);
1105 }
1106
Vasu Devab6b85c2009-05-17 12:33:08 +00001107 /* configure lport network properties */
Chris Leechaf7f85d2009-08-25 13:59:24 -07001108 rc = fcoe_netdev_config(lport, netdev);
Vasu Devab6b85c2009-05-17 12:33:08 +00001109 if (rc) {
Robert Loved5488eb2009-06-10 15:30:59 -07001110 FCOE_NETDEV_DBG(netdev, "Could not configure netdev for the "
1111 "interface\n");
Chris Leech54b649f2009-08-25 14:00:07 -07001112 goto out_lp_destroy;
Vasu Devab6b85c2009-05-17 12:33:08 +00001113 }
Joe Eykholt97c83892009-03-17 11:42:40 -07001114
Vasu Dev7f349142009-03-27 09:06:31 -07001115 /* configure lport scsi host properties */
Vasu Dev8ba00a42010-04-09 14:22:54 -07001116 rc = fcoe_shost_config(lport, parent);
Vasu Dev7f349142009-03-27 09:06:31 -07001117 if (rc) {
Robert Loved5488eb2009-06-10 15:30:59 -07001118 FCOE_NETDEV_DBG(netdev, "Could not configure shost for the "
1119 "interface\n");
Chris Leech54b649f2009-08-25 14:00:07 -07001120 goto out_lp_destroy;
Vasu Dev7f349142009-03-27 09:06:31 -07001121 }
1122
Vasu Dev7f349142009-03-27 09:06:31 -07001123 /* Initialize the library */
Joe Eykholte10f8c62010-07-20 15:20:30 -07001124 rc = fcoe_libfc_config(lport, &fcoe->ctlr, &fcoe_libfc_fcn_templ, 1);
Vasu Dev7f349142009-03-27 09:06:31 -07001125 if (rc) {
Robert Loved5488eb2009-06-10 15:30:59 -07001126 FCOE_NETDEV_DBG(netdev, "Could not configure libfc for the "
1127 "interface\n");
Vasu Dev7f349142009-03-27 09:06:31 -07001128 goto out_lp_destroy;
1129 }
1130
Neerav Parikh6fef3902012-01-22 17:30:10 -08001131 /* Initialized FDMI information */
1132 fcoe_fdmi_info(lport, netdev);
1133
Vasu Dev72fa3962011-02-25 15:03:01 -08001134 /*
1135 * fcoe_em_alloc() and fcoe_hostlist_add() both
1136 * need to be atomic with respect to other changes to the
1137 * hostlist since fcoe_em_alloc() looks for an existing EM
1138 * instance on host list updated by fcoe_hostlist_add().
1139 *
1140 * This is currently handled through the fcoe_config_mutex
1141 * begin held.
1142 */
1143 if (!npiv)
Chris Leech9a057532009-11-03 11:46:40 -08001144 /* lport exch manager allocation */
1145 rc = fcoe_em_config(lport);
Vasu Dev72fa3962011-02-25 15:03:01 -08001146 else {
1147 shost = vport_to_shost(vport);
1148 n_port = shost_priv(shost);
1149 rc = fc_exch_mgr_list_clone(n_port, lport);
1150 }
1151
1152 if (rc) {
1153 FCOE_NETDEV_DBG(netdev, "Could not configure the EM\n");
1154 goto out_lp_destroy;
Vasu Dev96316092009-07-29 17:05:00 -07001155 }
1156
Chris Leechaf7f85d2009-08-25 13:59:24 -07001157 return lport;
Vasu Dev7f349142009-03-27 09:06:31 -07001158
1159out_lp_destroy:
Chris Leechaf7f85d2009-08-25 13:59:24 -07001160 fc_exch_mgr_free(lport);
Vasu Dev7f349142009-03-27 09:06:31 -07001161out_host_put:
Chris Leechaf7f85d2009-08-25 13:59:24 -07001162 scsi_host_put(lport->host);
1163out:
1164 return ERR_PTR(rc);
Vasu Dev7f349142009-03-27 09:06:31 -07001165}
1166
1167/**
Robert Love1875f272009-11-03 11:47:50 -08001168 * fcoe_if_init() - Initialization routine for fcoe.ko
Vasu Dev7f349142009-03-27 09:06:31 -07001169 *
Robert Love1875f272009-11-03 11:47:50 -08001170 * Attaches the SW FCoE transport to the FC transport
1171 *
1172 * Returns: 0 on success
Vasu Dev7f349142009-03-27 09:06:31 -07001173 */
1174static int __init fcoe_if_init(void)
1175{
1176 /* attach to scsi transport */
Yi Zou8ca86f82011-01-28 16:05:11 -08001177 fcoe_nport_scsi_transport =
1178 fc_attach_transport(&fcoe_nport_fc_functions);
1179 fcoe_vport_scsi_transport =
1180 fc_attach_transport(&fcoe_vport_fc_functions);
Vasu Dev7f349142009-03-27 09:06:31 -07001181
Yi Zou8ca86f82011-01-28 16:05:11 -08001182 if (!fcoe_nport_scsi_transport) {
Robert Loved5488eb2009-06-10 15:30:59 -07001183 printk(KERN_ERR "fcoe: Failed to attach to the FC transport\n");
Vasu Dev7f349142009-03-27 09:06:31 -07001184 return -ENODEV;
1185 }
1186
1187 return 0;
1188}
1189
1190/**
Robert Love1875f272009-11-03 11:47:50 -08001191 * fcoe_if_exit() - Tear down fcoe.ko
Vasu Dev7f349142009-03-27 09:06:31 -07001192 *
Robert Love1875f272009-11-03 11:47:50 -08001193 * Detaches the SW FCoE transport from the FC transport
1194 *
1195 * Returns: 0 on success
Vasu Dev7f349142009-03-27 09:06:31 -07001196 */
Bart Van Assche7c9c6842012-01-13 17:26:25 -08001197static int __exit fcoe_if_exit(void)
Vasu Dev7f349142009-03-27 09:06:31 -07001198{
Yi Zou8ca86f82011-01-28 16:05:11 -08001199 fc_release_transport(fcoe_nport_scsi_transport);
1200 fc_release_transport(fcoe_vport_scsi_transport);
1201 fcoe_nport_scsi_transport = NULL;
1202 fcoe_vport_scsi_transport = NULL;
Vasu Dev7f349142009-03-27 09:06:31 -07001203 return 0;
1204}
1205
Robert Love85b4aa42008-12-09 15:10:24 -08001206/**
Robert Love1875f272009-11-03 11:47:50 -08001207 * fcoe_percpu_thread_create() - Create a receive thread for an online CPU
1208 * @cpu: The CPU index of the CPU to create a receive thread for
Robert Love8976f422009-03-17 11:41:46 -07001209 */
1210static void fcoe_percpu_thread_create(unsigned int cpu)
1211{
1212 struct fcoe_percpu_s *p;
1213 struct task_struct *thread;
1214
1215 p = &per_cpu(fcoe_percpu, cpu);
1216
Eric Dumazet5c609ff2011-09-27 21:37:52 -07001217 thread = kthread_create_on_node(fcoe_percpu_receive_thread,
1218 (void *)p, cpu_to_node(cpu),
1219 "fcoethread/%d", cpu);
Robert Love8976f422009-03-17 11:41:46 -07001220
Joe Eykholte7a51992009-08-25 14:04:08 -07001221 if (likely(!IS_ERR(thread))) {
Robert Love8976f422009-03-17 11:41:46 -07001222 kthread_bind(thread, cpu);
1223 wake_up_process(thread);
1224
1225 spin_lock_bh(&p->fcoe_rx_list.lock);
1226 p->thread = thread;
1227 spin_unlock_bh(&p->fcoe_rx_list.lock);
1228 }
1229}
1230
1231/**
Robert Love1875f272009-11-03 11:47:50 -08001232 * fcoe_percpu_thread_destroy() - Remove the receive thread of a CPU
1233 * @cpu: The CPU index of the CPU whose receive thread is to be destroyed
Robert Love8976f422009-03-17 11:41:46 -07001234 *
1235 * Destroys a per-CPU Rx thread. Any pending skbs are moved to the
1236 * current CPU's Rx thread. If the thread being destroyed is bound to
1237 * the CPU processing this context the skbs will be freed.
1238 */
1239static void fcoe_percpu_thread_destroy(unsigned int cpu)
1240{
1241 struct fcoe_percpu_s *p;
1242 struct task_struct *thread;
1243 struct page *crc_eof;
1244 struct sk_buff *skb;
1245#ifdef CONFIG_SMP
1246 struct fcoe_percpu_s *p0;
Joe Eykholtf018b732010-03-12 16:08:55 -08001247 unsigned targ_cpu = get_cpu();
Robert Love8976f422009-03-17 11:41:46 -07001248#endif /* CONFIG_SMP */
1249
Robert Loved5488eb2009-06-10 15:30:59 -07001250 FCOE_DBG("Destroying receive thread for CPU %d\n", cpu);
Robert Love8976f422009-03-17 11:41:46 -07001251
1252 /* Prevent any new skbs from being queued for this CPU. */
1253 p = &per_cpu(fcoe_percpu, cpu);
1254 spin_lock_bh(&p->fcoe_rx_list.lock);
1255 thread = p->thread;
1256 p->thread = NULL;
1257 crc_eof = p->crc_eof_page;
1258 p->crc_eof_page = NULL;
1259 p->crc_eof_offset = 0;
1260 spin_unlock_bh(&p->fcoe_rx_list.lock);
1261
1262#ifdef CONFIG_SMP
1263 /*
1264 * Don't bother moving the skb's if this context is running
1265 * on the same CPU that is having its thread destroyed. This
1266 * can easily happen when the module is removed.
1267 */
1268 if (cpu != targ_cpu) {
1269 p0 = &per_cpu(fcoe_percpu, targ_cpu);
1270 spin_lock_bh(&p0->fcoe_rx_list.lock);
1271 if (p0->thread) {
Robert Loved5488eb2009-06-10 15:30:59 -07001272 FCOE_DBG("Moving frames from CPU %d to CPU %d\n",
1273 cpu, targ_cpu);
Robert Love8976f422009-03-17 11:41:46 -07001274
1275 while ((skb = __skb_dequeue(&p->fcoe_rx_list)) != NULL)
1276 __skb_queue_tail(&p0->fcoe_rx_list, skb);
1277 spin_unlock_bh(&p0->fcoe_rx_list.lock);
1278 } else {
1279 /*
1280 * The targeted CPU is not initialized and cannot accept
Robert Love1875f272009-11-03 11:47:50 -08001281 * new skbs. Unlock the targeted CPU and drop the skbs
Robert Love8976f422009-03-17 11:41:46 -07001282 * on the CPU that is going offline.
1283 */
1284 while ((skb = __skb_dequeue(&p->fcoe_rx_list)) != NULL)
1285 kfree_skb(skb);
1286 spin_unlock_bh(&p0->fcoe_rx_list.lock);
1287 }
1288 } else {
1289 /*
1290 * This scenario occurs when the module is being removed
1291 * and all threads are being destroyed. skbs will continue
1292 * to be shifted from the CPU thread that is being removed
1293 * to the CPU thread associated with the CPU that is processing
1294 * the module removal. Once there is only one CPU Rx thread it
1295 * will reach this case and we will drop all skbs and later
1296 * stop the thread.
1297 */
1298 spin_lock_bh(&p->fcoe_rx_list.lock);
1299 while ((skb = __skb_dequeue(&p->fcoe_rx_list)) != NULL)
1300 kfree_skb(skb);
1301 spin_unlock_bh(&p->fcoe_rx_list.lock);
1302 }
Joe Eykholtf018b732010-03-12 16:08:55 -08001303 put_cpu();
Robert Love8976f422009-03-17 11:41:46 -07001304#else
1305 /*
Chris Leechdd3fd722009-04-21 16:27:36 -07001306 * This a non-SMP scenario where the singular Rx thread is
Robert Love8976f422009-03-17 11:41:46 -07001307 * being removed. Free all skbs and stop the thread.
1308 */
1309 spin_lock_bh(&p->fcoe_rx_list.lock);
1310 while ((skb = __skb_dequeue(&p->fcoe_rx_list)) != NULL)
1311 kfree_skb(skb);
1312 spin_unlock_bh(&p->fcoe_rx_list.lock);
1313#endif
1314
1315 if (thread)
1316 kthread_stop(thread);
1317
1318 if (crc_eof)
1319 put_page(crc_eof);
1320}
1321
1322/**
Robert Love1875f272009-11-03 11:47:50 -08001323 * fcoe_cpu_callback() - Handler for CPU hotplug events
1324 * @nfb: The callback data block
1325 * @action: The event triggering the callback
1326 * @hcpu: The index of the CPU that the event is for
Robert Love8976f422009-03-17 11:41:46 -07001327 *
Robert Love1875f272009-11-03 11:47:50 -08001328 * This creates or destroys per-CPU data for fcoe
Robert Love8976f422009-03-17 11:41:46 -07001329 *
1330 * Returns NOTIFY_OK always.
1331 */
1332static int fcoe_cpu_callback(struct notifier_block *nfb,
1333 unsigned long action, void *hcpu)
1334{
1335 unsigned cpu = (unsigned long)hcpu;
1336
1337 switch (action) {
1338 case CPU_ONLINE:
1339 case CPU_ONLINE_FROZEN:
Robert Loved5488eb2009-06-10 15:30:59 -07001340 FCOE_DBG("CPU %x online: Create Rx thread\n", cpu);
Robert Love8976f422009-03-17 11:41:46 -07001341 fcoe_percpu_thread_create(cpu);
1342 break;
1343 case CPU_DEAD:
1344 case CPU_DEAD_FROZEN:
Robert Loved5488eb2009-06-10 15:30:59 -07001345 FCOE_DBG("CPU %x offline: Remove Rx thread\n", cpu);
Robert Love8976f422009-03-17 11:41:46 -07001346 fcoe_percpu_thread_destroy(cpu);
1347 break;
1348 default:
1349 break;
1350 }
1351 return NOTIFY_OK;
1352}
1353
Robert Love8976f422009-03-17 11:41:46 -07001354/**
Kiran Patil064287e2011-06-20 16:59:20 -07001355 * fcoe_select_cpu() - Selects CPU to handle post-processing of incoming
1356 * command.
Kiran Patil064287e2011-06-20 16:59:20 -07001357 *
Vasu Devd2722812011-07-27 15:11:10 -07001358 * This routine selects next CPU based on cpumask to distribute
1359 * incoming requests in round robin.
Kiran Patil064287e2011-06-20 16:59:20 -07001360 *
Vasu Devd2722812011-07-27 15:11:10 -07001361 * Returns: int CPU number
Kiran Patil064287e2011-06-20 16:59:20 -07001362 */
Vasu Devd2722812011-07-27 15:11:10 -07001363static inline unsigned int fcoe_select_cpu(void)
Kiran Patil064287e2011-06-20 16:59:20 -07001364{
1365 static unsigned int selected_cpu;
1366
Vasu Devd2722812011-07-27 15:11:10 -07001367 selected_cpu = cpumask_next(selected_cpu, cpu_online_mask);
1368 if (selected_cpu >= nr_cpu_ids)
1369 selected_cpu = cpumask_first(cpu_online_mask);
1370
Kiran Patil064287e2011-06-20 16:59:20 -07001371 return selected_cpu;
1372}
1373
1374/**
Robert Love1875f272009-11-03 11:47:50 -08001375 * fcoe_rcv() - Receive packets from a net device
1376 * @skb: The received packet
1377 * @netdev: The net device that the packet was received on
1378 * @ptype: The packet type context
1379 * @olddev: The last device net device
Robert Love85b4aa42008-12-09 15:10:24 -08001380 *
Robert Love1875f272009-11-03 11:47:50 -08001381 * This routine is called by NET_RX_SOFTIRQ. It receives a packet, builds a
1382 * FC frame and passes the frame to libfc.
Robert Love85b4aa42008-12-09 15:10:24 -08001383 *
1384 * Returns: 0 for success
Robert Love34f42a02009-02-27 10:55:45 -08001385 */
Bart Van Assche7c9c6842012-01-13 17:26:25 -08001386static int fcoe_rcv(struct sk_buff *skb, struct net_device *netdev,
Robert Love85b4aa42008-12-09 15:10:24 -08001387 struct packet_type *ptype, struct net_device *olddev)
1388{
Robert Love1875f272009-11-03 11:47:50 -08001389 struct fc_lport *lport;
Robert Love85b4aa42008-12-09 15:10:24 -08001390 struct fcoe_rcv_info *fr;
Chris Leech259ad852009-08-25 13:59:41 -07001391 struct fcoe_interface *fcoe;
Robert Love85b4aa42008-12-09 15:10:24 -08001392 struct fc_frame_header *fh;
Robert Love85b4aa42008-12-09 15:10:24 -08001393 struct fcoe_percpu_s *fps;
Vasu Dev519e5132010-07-20 15:19:32 -07001394 struct ethhdr *eh;
Vasu Devb2f00912009-08-25 13:58:53 -07001395 unsigned int cpu;
Robert Love85b4aa42008-12-09 15:10:24 -08001396
Chris Leech259ad852009-08-25 13:59:41 -07001397 fcoe = container_of(ptype, struct fcoe_interface, fcoe_packet_type);
Robert Love1875f272009-11-03 11:47:50 -08001398 lport = fcoe->ctlr.lp;
1399 if (unlikely(!lport)) {
1400 FCOE_NETDEV_DBG(netdev, "Cannot find hba structure");
Robert Love85b4aa42008-12-09 15:10:24 -08001401 goto err2;
1402 }
Robert Love1875f272009-11-03 11:47:50 -08001403 if (!lport->link_up)
Joe Eykholt97c83892009-03-17 11:42:40 -07001404 goto err2;
Robert Love85b4aa42008-12-09 15:10:24 -08001405
Robert Love1875f272009-11-03 11:47:50 -08001406 FCOE_NETDEV_DBG(netdev, "skb_info: len:%d data_len:%d head:%p "
Robert Loved5488eb2009-06-10 15:30:59 -07001407 "data:%p tail:%p end:%p sum:%d dev:%s",
1408 skb->len, skb->data_len, skb->head, skb->data,
1409 skb_tail_pointer(skb), skb_end_pointer(skb),
1410 skb->csum, skb->dev ? skb->dev->name : "<NULL>");
Robert Love85b4aa42008-12-09 15:10:24 -08001411
Vasu Dev519e5132010-07-20 15:19:32 -07001412 eh = eth_hdr(skb);
Vasu Dev519e5132010-07-20 15:19:32 -07001413
1414 if (is_fip_mode(&fcoe->ctlr) &&
1415 compare_ether_addr(eh->h_source, fcoe->ctlr.dest_addr)) {
1416 FCOE_NETDEV_DBG(netdev, "wrong source mac address:%pM\n",
1417 eh->h_source);
Robert Love85b4aa42008-12-09 15:10:24 -08001418 goto err;
1419 }
1420
1421 /*
1422 * Check for minimum frame length, and make sure required FCoE
1423 * and FC headers are pulled into the linear data area.
1424 */
1425 if (unlikely((skb->len < FCOE_MIN_FRAME) ||
Robert Love1875f272009-11-03 11:47:50 -08001426 !pskb_may_pull(skb, FCOE_HEADER_LEN)))
Robert Love85b4aa42008-12-09 15:10:24 -08001427 goto err;
1428
1429 skb_set_transport_header(skb, sizeof(struct fcoe_hdr));
1430 fh = (struct fc_frame_header *) skb_transport_header(skb);
1431
Robert Love0ee31cb2010-10-08 17:12:46 -07001432 if (ntoh24(&eh->h_dest[3]) != ntoh24(fh->fh_d_id)) {
1433 FCOE_NETDEV_DBG(netdev, "FC frame d_id mismatch with MAC:%pM\n",
1434 eh->h_dest);
1435 goto err;
1436 }
1437
Robert Love85b4aa42008-12-09 15:10:24 -08001438 fr = fcoe_dev_from_skb(skb);
Robert Love1875f272009-11-03 11:47:50 -08001439 fr->fr_dev = lport;
Robert Love5e5e92d2009-03-17 11:41:35 -07001440
Robert Love85b4aa42008-12-09 15:10:24 -08001441 /*
Vasu Devb2f00912009-08-25 13:58:53 -07001442 * In case the incoming frame's exchange is originated from
1443 * the initiator, then received frame's exchange id is ANDed
1444 * with fc_cpu_mask bits to get the same cpu on which exchange
Vasu Devd2722812011-07-27 15:11:10 -07001445 * was originated, otherwise select cpu using rx exchange id
1446 * or fcoe_select_cpu().
Robert Love85b4aa42008-12-09 15:10:24 -08001447 */
Vasu Devb2f00912009-08-25 13:58:53 -07001448 if (ntoh24(fh->fh_f_ctl) & FC_FC_EX_CTX)
1449 cpu = ntohs(fh->fh_ox_id) & fc_cpu_mask;
Kiran Patil064287e2011-06-20 16:59:20 -07001450 else {
Vasu Devd2722812011-07-27 15:11:10 -07001451 if (ntohs(fh->fh_rx_id) == FC_XID_UNKNOWN)
1452 cpu = fcoe_select_cpu();
1453 else
Kiran Patil29bdd2b2011-06-20 16:59:25 -07001454 cpu = ntohs(fh->fh_rx_id) & fc_cpu_mask;
Kiran Patil064287e2011-06-20 16:59:20 -07001455 }
Vasu Dev324f6672011-07-27 15:10:39 -07001456
1457 if (cpu >= nr_cpu_ids)
1458 goto err;
1459
Robert Love8976f422009-03-17 11:41:46 -07001460 fps = &per_cpu(fcoe_percpu, cpu);
Neil Horman94aa29f2012-03-09 14:50:08 -08001461 spin_lock(&fps->fcoe_rx_list.lock);
Robert Love8976f422009-03-17 11:41:46 -07001462 if (unlikely(!fps->thread)) {
1463 /*
1464 * The targeted CPU is not ready, let's target
1465 * the first CPU now. For non-SMP systems this
1466 * will check the same CPU twice.
1467 */
Robert Love1875f272009-11-03 11:47:50 -08001468 FCOE_NETDEV_DBG(netdev, "CPU is online, but no receive thread "
Robert Loved5488eb2009-06-10 15:30:59 -07001469 "ready for incoming skb- using first online "
1470 "CPU.\n");
Robert Love8976f422009-03-17 11:41:46 -07001471
Neil Horman94aa29f2012-03-09 14:50:08 -08001472 spin_unlock(&fps->fcoe_rx_list.lock);
Rusty Russell69571772009-12-17 11:43:14 -06001473 cpu = cpumask_first(cpu_online_mask);
Robert Love8976f422009-03-17 11:41:46 -07001474 fps = &per_cpu(fcoe_percpu, cpu);
Neil Horman94aa29f2012-03-09 14:50:08 -08001475 spin_lock(&fps->fcoe_rx_list.lock);
Robert Love8976f422009-03-17 11:41:46 -07001476 if (!fps->thread) {
Neil Horman94aa29f2012-03-09 14:50:08 -08001477 spin_unlock(&fps->fcoe_rx_list.lock);
Robert Love8976f422009-03-17 11:41:46 -07001478 goto err;
1479 }
1480 }
1481
1482 /*
1483 * We now have a valid CPU that we're targeting for
1484 * this skb. We also have this receive thread locked,
1485 * so we're free to queue skbs into it's queue.
1486 */
Robert Love85b4aa42008-12-09 15:10:24 -08001487
Neil Horman5e70c4c2012-03-09 14:49:48 -08001488 /*
1489 * Note: We used to have a set of conditions under which we would
1490 * call fcoe_recv_frame directly, rather than queuing to the rx list
1491 * as it could save a few cycles, but doing so is prohibited, as
1492 * fcoe_recv_frame has several paths that may sleep, which is forbidden
1493 * in softirq context.
Chris Leech859b7b62009-11-20 14:54:47 -08001494 */
Neil Horman5e70c4c2012-03-09 14:49:48 -08001495 __skb_queue_tail(&fps->fcoe_rx_list, skb);
Neil Horman20dc3812012-03-09 14:50:24 -08001496 if (fps->thread->state == TASK_INTERRUPTIBLE)
Neil Horman5e70c4c2012-03-09 14:49:48 -08001497 wake_up_process(fps->thread);
Neil Horman94aa29f2012-03-09 14:50:08 -08001498 spin_unlock(&fps->fcoe_rx_list.lock);
Robert Love85b4aa42008-12-09 15:10:24 -08001499
1500 return 0;
1501err:
Joe Eykholtf018b732010-03-12 16:08:55 -08001502 per_cpu_ptr(lport->dev_stats, get_cpu())->ErrorFrames++;
1503 put_cpu();
Robert Love85b4aa42008-12-09 15:10:24 -08001504err2:
1505 kfree_skb(skb);
1506 return -1;
1507}
Robert Love85b4aa42008-12-09 15:10:24 -08001508
1509/**
Bhanu Prakash Gollapudi8597ae82011-01-28 16:05:37 -08001510 * fcoe_alloc_paged_crc_eof() - Allocate a page to be used for the trailer CRC
Robert Love1875f272009-11-03 11:47:50 -08001511 * @skb: The packet to be transmitted
1512 * @tlen: The total length of the trailer
1513 *
Robert Love85b4aa42008-12-09 15:10:24 -08001514 * Returns: 0 for success
Robert Love34f42a02009-02-27 10:55:45 -08001515 */
Bhanu Prakash Gollapudi8597ae82011-01-28 16:05:37 -08001516static int fcoe_alloc_paged_crc_eof(struct sk_buff *skb, int tlen)
Robert Love85b4aa42008-12-09 15:10:24 -08001517{
1518 struct fcoe_percpu_s *fps;
Bhanu Prakash Gollapudi8597ae82011-01-28 16:05:37 -08001519 int rc;
Robert Love85b4aa42008-12-09 15:10:24 -08001520
Robert Love5e5e92d2009-03-17 11:41:35 -07001521 fps = &get_cpu_var(fcoe_percpu);
Bhanu Prakash Gollapudi8597ae82011-01-28 16:05:37 -08001522 rc = fcoe_get_paged_crc_eof(skb, tlen, fps);
Robert Love5e5e92d2009-03-17 11:41:35 -07001523 put_cpu_var(fcoe_percpu);
Robert Love85b4aa42008-12-09 15:10:24 -08001524
Bhanu Prakash Gollapudi8597ae82011-01-28 16:05:37 -08001525 return rc;
Robert Love85b4aa42008-12-09 15:10:24 -08001526}
Robert Love85b4aa42008-12-09 15:10:24 -08001527
1528/**
Robert Love1875f272009-11-03 11:47:50 -08001529 * fcoe_xmit() - Transmit a FCoE frame
1530 * @lport: The local port that the frame is to be transmitted for
1531 * @fp: The frame to be transmitted
Robert Love85b4aa42008-12-09 15:10:24 -08001532 *
Robert Love1875f272009-11-03 11:47:50 -08001533 * Return: 0 for success
Robert Love34f42a02009-02-27 10:55:45 -08001534 */
Bart Van Assche7c9c6842012-01-13 17:26:25 -08001535static int fcoe_xmit(struct fc_lport *lport, struct fc_frame *fp)
Robert Love85b4aa42008-12-09 15:10:24 -08001536{
Vasu Dev4bb6b512009-05-06 10:52:34 -07001537 int wlen;
Robert Love85b4aa42008-12-09 15:10:24 -08001538 u32 crc;
1539 struct ethhdr *eh;
1540 struct fcoe_crc_eof *cp;
1541 struct sk_buff *skb;
1542 struct fcoe_dev_stats *stats;
1543 struct fc_frame_header *fh;
1544 unsigned int hlen; /* header length implies the version */
1545 unsigned int tlen; /* trailer length */
1546 unsigned int elen; /* eth header, may include vlan */
Robert Love1875f272009-11-03 11:47:50 -08001547 struct fcoe_port *port = lport_priv(lport);
Bhanu Prakash Gollapudi8597ae82011-01-28 16:05:37 -08001548 struct fcoe_interface *fcoe = port->priv;
Robert Love85b4aa42008-12-09 15:10:24 -08001549 u8 sof, eof;
1550 struct fcoe_hdr *hp;
1551
1552 WARN_ON((fr_len(fp) % sizeof(u32)) != 0);
1553
Robert Love85b4aa42008-12-09 15:10:24 -08001554 fh = fc_frame_header_get(fp);
Joe Eykholt97c83892009-03-17 11:42:40 -07001555 skb = fp_skb(fp);
1556 wlen = skb->len / FCOE_WORD_TO_BYTE;
1557
Robert Love1875f272009-11-03 11:47:50 -08001558 if (!lport->link_up) {
Dan Carpenter3caf02e2009-04-21 16:27:25 -07001559 kfree_skb(skb);
Joe Eykholt97c83892009-03-17 11:42:40 -07001560 return 0;
Robert Love85b4aa42008-12-09 15:10:24 -08001561 }
1562
Joe Eykholt9860eeb2010-03-12 16:07:52 -08001563 if (unlikely(fh->fh_type == FC_TYPE_ELS) &&
Robert Love1875f272009-11-03 11:47:50 -08001564 fcoe_ctlr_els_send(&fcoe->ctlr, lport, skb))
Joe Eykholt97c83892009-03-17 11:42:40 -07001565 return 0;
1566
Robert Love85b4aa42008-12-09 15:10:24 -08001567 sof = fr_sof(fp);
1568 eof = fr_eof(fp);
1569
Vasu Dev4e57e1c2009-05-06 10:52:46 -07001570 elen = sizeof(struct ethhdr);
Robert Love85b4aa42008-12-09 15:10:24 -08001571 hlen = sizeof(struct fcoe_hdr);
1572 tlen = sizeof(struct fcoe_crc_eof);
1573 wlen = (skb->len - tlen + sizeof(crc)) / FCOE_WORD_TO_BYTE;
1574
1575 /* crc offload */
Robert Love1875f272009-11-03 11:47:50 -08001576 if (likely(lport->crc_offload)) {
Yi Zoucf64bc82012-03-16 23:08:12 +00001577 skb->ip_summed = CHECKSUM_UNNECESSARY;
Robert Love85b4aa42008-12-09 15:10:24 -08001578 skb->csum_start = skb_headroom(skb);
1579 skb->csum_offset = skb->len;
1580 crc = 0;
1581 } else {
1582 skb->ip_summed = CHECKSUM_NONE;
1583 crc = fcoe_fc_crc(fp);
1584 }
1585
Chris Leech014f5c32009-08-25 13:59:30 -07001586 /* copy port crc and eof to the skb buff */
Robert Love85b4aa42008-12-09 15:10:24 -08001587 if (skb_is_nonlinear(skb)) {
1588 skb_frag_t *frag;
Bhanu Prakash Gollapudi8597ae82011-01-28 16:05:37 -08001589 if (fcoe_alloc_paged_crc_eof(skb, tlen)) {
Roel Kluine9041582009-02-27 10:56:22 -08001590 kfree_skb(skb);
Robert Love85b4aa42008-12-09 15:10:24 -08001591 return -ENOMEM;
1592 }
1593 frag = &skb_shinfo(skb)->frags[skb_shinfo(skb)->nr_frags - 1];
Cong Wang77dfce02011-11-25 23:14:23 +08001594 cp = kmap_atomic(skb_frag_page(frag))
Robert Love85b4aa42008-12-09 15:10:24 -08001595 + frag->page_offset;
1596 } else {
1597 cp = (struct fcoe_crc_eof *)skb_put(skb, tlen);
1598 }
1599
1600 memset(cp, 0, sizeof(*cp));
1601 cp->fcoe_eof = eof;
1602 cp->fcoe_crc32 = cpu_to_le32(~crc);
1603
1604 if (skb_is_nonlinear(skb)) {
Cong Wang77dfce02011-11-25 23:14:23 +08001605 kunmap_atomic(cp);
Robert Love85b4aa42008-12-09 15:10:24 -08001606 cp = NULL;
1607 }
1608
Chris Leech014f5c32009-08-25 13:59:30 -07001609 /* adjust skb network/transport offsets to match mac/fcoe/port */
Robert Love85b4aa42008-12-09 15:10:24 -08001610 skb_push(skb, elen + hlen);
1611 skb_reset_mac_header(skb);
1612 skb_reset_network_header(skb);
1613 skb->mac_len = elen;
Yi Zou211c7382009-02-27 14:06:37 -08001614 skb->protocol = htons(ETH_P_FCOE);
john fastabend6f6c2aa2011-11-18 13:35:56 -08001615 skb->priority = port->priority;
1616
Vasu Devd1483bb2011-09-27 21:38:13 -07001617 if (fcoe->netdev->priv_flags & IFF_802_1Q_VLAN &&
1618 fcoe->realdev->features & NETIF_F_HW_VLAN_TX) {
1619 skb->vlan_tci = VLAN_TAG_PRESENT |
1620 vlan_dev_vlan_id(fcoe->netdev);
1621 skb->dev = fcoe->realdev;
1622 } else
1623 skb->dev = fcoe->netdev;
Robert Love85b4aa42008-12-09 15:10:24 -08001624
1625 /* fill up mac and fcoe headers */
1626 eh = eth_hdr(skb);
1627 eh->h_proto = htons(ETH_P_FCOE);
Joe Eykholtcd229e42010-07-20 15:20:40 -07001628 memcpy(eh->h_dest, fcoe->ctlr.dest_addr, ETH_ALEN);
Chris Leech3fe9a0b2009-08-25 13:59:46 -07001629 if (fcoe->ctlr.map_dest)
Joe Eykholtcd229e42010-07-20 15:20:40 -07001630 memcpy(eh->h_dest + 3, fh->fh_d_id, 3);
Robert Love85b4aa42008-12-09 15:10:24 -08001631
Chris Leech3fe9a0b2009-08-25 13:59:46 -07001632 if (unlikely(fcoe->ctlr.flogi_oxid != FC_XID_UNKNOWN))
1633 memcpy(eh->h_source, fcoe->ctlr.ctl_src_addr, ETH_ALEN);
Robert Love85b4aa42008-12-09 15:10:24 -08001634 else
Chris Leech11b56182009-11-03 11:46:29 -08001635 memcpy(eh->h_source, port->data_src_addr, ETH_ALEN);
Robert Love85b4aa42008-12-09 15:10:24 -08001636
1637 hp = (struct fcoe_hdr *)(eh + 1);
1638 memset(hp, 0, sizeof(*hp));
1639 if (FC_FCOE_VER)
1640 FC_FCOE_ENCAPS_VER(hp, FC_FCOE_VER);
1641 hp->fcoe_sof = sof;
1642
Yi Zou39ca9a02009-02-27 14:07:15 -08001643 /* fcoe lso, mss is in max_payload which is non-zero for FCP data */
Robert Love1875f272009-11-03 11:47:50 -08001644 if (lport->seq_offload && fr_max_payload(fp)) {
Yi Zou39ca9a02009-02-27 14:07:15 -08001645 skb_shinfo(skb)->gso_type = SKB_GSO_FCOE;
1646 skb_shinfo(skb)->gso_size = fr_max_payload(fp);
1647 } else {
1648 skb_shinfo(skb)->gso_type = 0;
1649 skb_shinfo(skb)->gso_size = 0;
1650 }
Robert Love85b4aa42008-12-09 15:10:24 -08001651 /* update tx stats: regardless if LLD fails */
Joe Eykholtf018b732010-03-12 16:08:55 -08001652 stats = per_cpu_ptr(lport->dev_stats, get_cpu());
Robert Love582b45b2009-03-31 15:51:50 -07001653 stats->TxFrames++;
1654 stats->TxWords += wlen;
Joe Eykholtf018b732010-03-12 16:08:55 -08001655 put_cpu();
Robert Love85b4aa42008-12-09 15:10:24 -08001656
1657 /* send down to lld */
Robert Love1875f272009-11-03 11:47:50 -08001658 fr_dev(fp) = lport;
Vasu Dev980f5152011-07-27 15:11:05 -07001659 fcoe_port_send(port, skb);
Robert Love85b4aa42008-12-09 15:10:24 -08001660 return 0;
1661}
Robert Love85b4aa42008-12-09 15:10:24 -08001662
Robert Love34f42a02009-02-27 10:55:45 -08001663/**
Robert Love1875f272009-11-03 11:47:50 -08001664 * fcoe_percpu_flush_done() - Indicate per-CPU queue flush completion
1665 * @skb: The completed skb (argument required by destructor)
Joe Eykholte7a51992009-08-25 14:04:08 -07001666 */
1667static void fcoe_percpu_flush_done(struct sk_buff *skb)
1668{
1669 complete(&fcoe_flush_completion);
1670}
1671
1672/**
Vasu Dev52ee83212011-01-28 16:03:52 -08001673 * fcoe_filter_frames() - filter out bad fcoe frames, i.e. bad CRC
1674 * @lport: The local port the frame was received on
1675 * @fp: The received frame
1676 *
1677 * Return: 0 on passing filtering checks
1678 */
1679static inline int fcoe_filter_frames(struct fc_lport *lport,
1680 struct fc_frame *fp)
1681{
1682 struct fcoe_interface *fcoe;
1683 struct fc_frame_header *fh;
1684 struct sk_buff *skb = (struct sk_buff *)fp;
1685 struct fcoe_dev_stats *stats;
1686
1687 /*
1688 * We only check CRC if no offload is available and if it is
1689 * it's solicited data, in which case, the FCP layer would
1690 * check it during the copy.
1691 */
1692 if (lport->crc_offload && skb->ip_summed == CHECKSUM_UNNECESSARY)
1693 fr_flags(fp) &= ~FCPHF_CRC_UNCHECKED;
1694 else
1695 fr_flags(fp) |= FCPHF_CRC_UNCHECKED;
1696
1697 fh = (struct fc_frame_header *) skb_transport_header(skb);
1698 fh = fc_frame_header_get(fp);
1699 if (fh->fh_r_ctl == FC_RCTL_DD_SOL_DATA && fh->fh_type == FC_TYPE_FCP)
1700 return 0;
1701
Bhanu Prakash Gollapudi8597ae82011-01-28 16:05:37 -08001702 fcoe = ((struct fcoe_port *)lport_priv(lport))->priv;
Vasu Dev52ee83212011-01-28 16:03:52 -08001703 if (is_fip_mode(&fcoe->ctlr) && fc_frame_payload_op(fp) == ELS_LOGO &&
1704 ntoh24(fh->fh_s_id) == FC_FID_FLOGI) {
1705 FCOE_DBG("fcoe: dropping FCoE lport LOGO in fip mode\n");
1706 return -EINVAL;
1707 }
1708
Dan Carpenterf2f96d22011-02-25 15:03:23 -08001709 if (!(fr_flags(fp) & FCPHF_CRC_UNCHECKED) ||
Vasu Dev52ee83212011-01-28 16:03:52 -08001710 le32_to_cpu(fr_crc(fp)) == ~crc32(~0, skb->data, skb->len)) {
1711 fr_flags(fp) &= ~FCPHF_CRC_UNCHECKED;
1712 return 0;
1713 }
1714
1715 stats = per_cpu_ptr(lport->dev_stats, get_cpu());
1716 stats->InvalidCRCCount++;
1717 if (stats->InvalidCRCCount < 5)
1718 printk(KERN_WARNING "fcoe: dropping frame with CRC error\n");
Thomas Gleixner7e1e7ea2011-11-11 20:52:01 +01001719 put_cpu();
Vasu Dev52ee83212011-01-28 16:03:52 -08001720 return -EINVAL;
1721}
1722
1723/**
Chris Leech859b7b62009-11-20 14:54:47 -08001724 * fcoe_recv_frame() - process a single received frame
1725 * @skb: frame to process
1726 */
1727static void fcoe_recv_frame(struct sk_buff *skb)
1728{
1729 u32 fr_len;
1730 struct fc_lport *lport;
1731 struct fcoe_rcv_info *fr;
1732 struct fcoe_dev_stats *stats;
Chris Leech859b7b62009-11-20 14:54:47 -08001733 struct fcoe_crc_eof crc_eof;
1734 struct fc_frame *fp;
Chris Leech859b7b62009-11-20 14:54:47 -08001735 struct fcoe_port *port;
1736 struct fcoe_hdr *hp;
1737
1738 fr = fcoe_dev_from_skb(skb);
1739 lport = fr->fr_dev;
1740 if (unlikely(!lport)) {
1741 if (skb->destructor != fcoe_percpu_flush_done)
1742 FCOE_NETDEV_DBG(skb->dev, "NULL lport in skb");
1743 kfree_skb(skb);
1744 return;
1745 }
1746
1747 FCOE_NETDEV_DBG(skb->dev, "skb_info: len:%d data_len:%d "
1748 "head:%p data:%p tail:%p end:%p sum:%d dev:%s",
1749 skb->len, skb->data_len,
1750 skb->head, skb->data, skb_tail_pointer(skb),
1751 skb_end_pointer(skb), skb->csum,
1752 skb->dev ? skb->dev->name : "<NULL>");
1753
Chris Leech859b7b62009-11-20 14:54:47 -08001754 port = lport_priv(lport);
Robert Lovef1633012011-12-16 14:24:49 -08001755 skb_linearize(skb); /* check for skb_is_nonlinear is within skb_linearize */
Chris Leech859b7b62009-11-20 14:54:47 -08001756
1757 /*
1758 * Frame length checks and setting up the header pointers
1759 * was done in fcoe_rcv already.
1760 */
1761 hp = (struct fcoe_hdr *) skb_network_header(skb);
Chris Leech859b7b62009-11-20 14:54:47 -08001762
Joe Eykholtf018b732010-03-12 16:08:55 -08001763 stats = per_cpu_ptr(lport->dev_stats, get_cpu());
Chris Leech859b7b62009-11-20 14:54:47 -08001764 if (unlikely(FC_FCOE_DECAPS_VER(hp) != FC_FCOE_VER)) {
1765 if (stats->ErrorFrames < 5)
1766 printk(KERN_WARNING "fcoe: FCoE version "
1767 "mismatch: The frame has "
1768 "version %x, but the "
1769 "initiator supports version "
1770 "%x\n", FC_FCOE_DECAPS_VER(hp),
1771 FC_FCOE_VER);
Joe Eykholtf018b732010-03-12 16:08:55 -08001772 goto drop;
Chris Leech859b7b62009-11-20 14:54:47 -08001773 }
1774
1775 skb_pull(skb, sizeof(struct fcoe_hdr));
1776 fr_len = skb->len - sizeof(struct fcoe_crc_eof);
1777
1778 stats->RxFrames++;
1779 stats->RxWords += fr_len / FCOE_WORD_TO_BYTE;
1780
1781 fp = (struct fc_frame *)skb;
1782 fc_frame_init(fp);
1783 fr_dev(fp) = lport;
1784 fr_sof(fp) = hp->fcoe_sof;
1785
1786 /* Copy out the CRC and EOF trailer for access */
Joe Eykholtf018b732010-03-12 16:08:55 -08001787 if (skb_copy_bits(skb, fr_len, &crc_eof, sizeof(crc_eof)))
1788 goto drop;
Chris Leech859b7b62009-11-20 14:54:47 -08001789 fr_eof(fp) = crc_eof.fcoe_eof;
1790 fr_crc(fp) = crc_eof.fcoe_crc32;
Joe Eykholtf018b732010-03-12 16:08:55 -08001791 if (pskb_trim(skb, fr_len))
1792 goto drop;
Chris Leech859b7b62009-11-20 14:54:47 -08001793
Vasu Dev52ee83212011-01-28 16:03:52 -08001794 if (!fcoe_filter_frames(lport, fp)) {
1795 put_cpu();
1796 fc_exch_recv(lport, fp);
1797 return;
Chris Leech859b7b62009-11-20 14:54:47 -08001798 }
Joe Eykholtf018b732010-03-12 16:08:55 -08001799drop:
1800 stats->ErrorFrames++;
1801 put_cpu();
1802 kfree_skb(skb);
Chris Leech859b7b62009-11-20 14:54:47 -08001803}
1804
1805/**
Robert Love1875f272009-11-03 11:47:50 -08001806 * fcoe_percpu_receive_thread() - The per-CPU packet receive thread
1807 * @arg: The per-CPU context
Robert Love85b4aa42008-12-09 15:10:24 -08001808 *
1809 * Return: 0 for success
Robert Love85b4aa42008-12-09 15:10:24 -08001810 */
Bart Van Assche7c9c6842012-01-13 17:26:25 -08001811static int fcoe_percpu_receive_thread(void *arg)
Robert Love85b4aa42008-12-09 15:10:24 -08001812{
1813 struct fcoe_percpu_s *p = arg;
Robert Love85b4aa42008-12-09 15:10:24 -08001814 struct sk_buff *skb;
Neil Horman20dc3812012-03-09 14:50:24 -08001815 struct sk_buff_head tmp;
1816
1817 skb_queue_head_init(&tmp);
Robert Love85b4aa42008-12-09 15:10:24 -08001818
Robert Love4469c192009-02-27 10:56:38 -08001819 set_user_nice(current, -20);
Robert Love85b4aa42008-12-09 15:10:24 -08001820
1821 while (!kthread_should_stop()) {
1822
1823 spin_lock_bh(&p->fcoe_rx_list.lock);
Neil Horman20dc3812012-03-09 14:50:24 -08001824 skb_queue_splice_init(&p->fcoe_rx_list, &tmp);
1825 spin_unlock_bh(&p->fcoe_rx_list.lock);
1826
1827 while ((skb = __skb_dequeue(&tmp)) != NULL)
1828 fcoe_recv_frame(skb);
1829
1830 spin_lock_bh(&p->fcoe_rx_list.lock);
1831 if (!skb_queue_len(&p->fcoe_rx_list)) {
Robert Love85b4aa42008-12-09 15:10:24 -08001832 set_current_state(TASK_INTERRUPTIBLE);
1833 spin_unlock_bh(&p->fcoe_rx_list.lock);
1834 schedule();
1835 set_current_state(TASK_RUNNING);
Neil Horman20dc3812012-03-09 14:50:24 -08001836 } else
1837 spin_unlock_bh(&p->fcoe_rx_list.lock);
Robert Love85b4aa42008-12-09 15:10:24 -08001838 }
1839 return 0;
1840}
1841
1842/**
Robert Love1875f272009-11-03 11:47:50 -08001843 * fcoe_dev_setup() - Setup the link change notification interface
Robert Love34f42a02009-02-27 10:55:45 -08001844 */
Randy Dunlapb0d428a2009-04-27 21:49:31 -07001845static void fcoe_dev_setup(void)
Robert Love85b4aa42008-12-09 15:10:24 -08001846{
john fastabend6f6c2aa2011-11-18 13:35:56 -08001847 register_dcbevent_notifier(&dcb_notifier);
Robert Love85b4aa42008-12-09 15:10:24 -08001848 register_netdevice_notifier(&fcoe_notifier);
1849}
1850
1851/**
Robert Love1875f272009-11-03 11:47:50 -08001852 * fcoe_dev_cleanup() - Cleanup the link change notification interface
Robert Love34f42a02009-02-27 10:55:45 -08001853 */
Robert Love85b4aa42008-12-09 15:10:24 -08001854static void fcoe_dev_cleanup(void)
1855{
john fastabend6f6c2aa2011-11-18 13:35:56 -08001856 unregister_dcbevent_notifier(&dcb_notifier);
Robert Love85b4aa42008-12-09 15:10:24 -08001857 unregister_netdevice_notifier(&fcoe_notifier);
1858}
1859
john fastabend6f6c2aa2011-11-18 13:35:56 -08001860static struct fcoe_interface *
1861fcoe_hostlist_lookup_realdev_port(struct net_device *netdev)
1862{
1863 struct fcoe_interface *fcoe;
1864 struct net_device *real_dev;
1865
1866 list_for_each_entry(fcoe, &fcoe_hostlist, list) {
1867 if (fcoe->netdev->priv_flags & IFF_802_1Q_VLAN)
1868 real_dev = vlan_dev_real_dev(fcoe->netdev);
1869 else
1870 real_dev = fcoe->netdev;
1871
1872 if (netdev == real_dev)
1873 return fcoe;
1874 }
1875 return NULL;
1876}
1877
1878static int fcoe_dcb_app_notification(struct notifier_block *notifier,
1879 ulong event, void *ptr)
1880{
1881 struct dcb_app_type *entry = ptr;
1882 struct fcoe_interface *fcoe;
1883 struct net_device *netdev;
1884 struct fcoe_port *port;
1885 int prio;
1886
1887 if (entry->app.selector != DCB_APP_IDTYPE_ETHTYPE)
1888 return NOTIFY_OK;
1889
1890 netdev = dev_get_by_index(&init_net, entry->ifindex);
1891 if (!netdev)
1892 return NOTIFY_OK;
1893
1894 fcoe = fcoe_hostlist_lookup_realdev_port(netdev);
1895 dev_put(netdev);
1896 if (!fcoe)
1897 return NOTIFY_OK;
1898
1899 if (entry->dcbx & DCB_CAP_DCBX_VER_CEE)
1900 prio = ffs(entry->app.priority) - 1;
1901 else
1902 prio = entry->app.priority;
1903
1904 if (prio < 0)
1905 return NOTIFY_OK;
1906
1907 if (entry->app.protocol == ETH_P_FIP ||
1908 entry->app.protocol == ETH_P_FCOE)
1909 fcoe->ctlr.priority = prio;
1910
1911 if (entry->app.protocol == ETH_P_FCOE) {
1912 port = lport_priv(fcoe->ctlr.lp);
1913 port->priority = prio;
1914 }
1915
1916 return NOTIFY_OK;
1917}
1918
Robert Love85b4aa42008-12-09 15:10:24 -08001919/**
Robert Love1875f272009-11-03 11:47:50 -08001920 * fcoe_device_notification() - Handler for net device events
1921 * @notifier: The context of the notification
1922 * @event: The type of event
1923 * @ptr: The net device that the event was on
Robert Love85b4aa42008-12-09 15:10:24 -08001924 *
Robert Love1875f272009-11-03 11:47:50 -08001925 * This function is called by the Ethernet driver in case of link change event.
Robert Love85b4aa42008-12-09 15:10:24 -08001926 *
1927 * Returns: 0 for success
Robert Love34f42a02009-02-27 10:55:45 -08001928 */
Robert Love85b4aa42008-12-09 15:10:24 -08001929static int fcoe_device_notification(struct notifier_block *notifier,
1930 ulong event, void *ptr)
1931{
Robert Love1875f272009-11-03 11:47:50 -08001932 struct fc_lport *lport = NULL;
Vasu Dev1d1b88d2009-07-29 17:05:45 -07001933 struct net_device *netdev = ptr;
Chris Leech014f5c32009-08-25 13:59:30 -07001934 struct fcoe_interface *fcoe;
Chris Leech2e70e242009-08-25 14:00:23 -07001935 struct fcoe_port *port;
Robert Love85b4aa42008-12-09 15:10:24 -08001936 struct fcoe_dev_stats *stats;
Joe Eykholt97c83892009-03-17 11:42:40 -07001937 u32 link_possible = 1;
Robert Love85b4aa42008-12-09 15:10:24 -08001938 u32 mfs;
1939 int rc = NOTIFY_OK;
1940
Chris Leech014f5c32009-08-25 13:59:30 -07001941 list_for_each_entry(fcoe, &fcoe_hostlist, list) {
Chris Leech25024982009-08-25 13:59:35 -07001942 if (fcoe->netdev == netdev) {
Robert Love1875f272009-11-03 11:47:50 -08001943 lport = fcoe->ctlr.lp;
Robert Love85b4aa42008-12-09 15:10:24 -08001944 break;
1945 }
1946 }
Robert Love1875f272009-11-03 11:47:50 -08001947 if (!lport) {
Robert Love85b4aa42008-12-09 15:10:24 -08001948 rc = NOTIFY_DONE;
1949 goto out;
1950 }
1951
Robert Love85b4aa42008-12-09 15:10:24 -08001952 switch (event) {
1953 case NETDEV_DOWN:
1954 case NETDEV_GOING_DOWN:
Joe Eykholt97c83892009-03-17 11:42:40 -07001955 link_possible = 0;
Robert Love85b4aa42008-12-09 15:10:24 -08001956 break;
1957 case NETDEV_UP:
1958 case NETDEV_CHANGE:
Robert Love85b4aa42008-12-09 15:10:24 -08001959 break;
1960 case NETDEV_CHANGEMTU:
Yi Zou7221d7e2009-10-21 16:27:52 -07001961 if (netdev->features & NETIF_F_FCOE_MTU)
1962 break;
Vasu Dev1d1b88d2009-07-29 17:05:45 -07001963 mfs = netdev->mtu - (sizeof(struct fcoe_hdr) +
1964 sizeof(struct fcoe_crc_eof));
Robert Love85b4aa42008-12-09 15:10:24 -08001965 if (mfs >= FC_MIN_MAX_FRAME)
Robert Love1875f272009-11-03 11:47:50 -08001966 fc_set_mfs(lport, mfs);
Robert Love85b4aa42008-12-09 15:10:24 -08001967 break;
1968 case NETDEV_REGISTER:
1969 break;
Chris Leech2e70e242009-08-25 14:00:23 -07001970 case NETDEV_UNREGISTER:
1971 list_del(&fcoe->list);
1972 port = lport_priv(fcoe->ctlr.lp);
Tejun Heo2ca32b42011-01-28 16:05:32 -08001973 queue_work(fcoe_wq, &port->destroy_work);
Chris Leech2e70e242009-08-25 14:00:23 -07001974 goto out;
1975 break;
Yi Zou54a5b212010-07-20 15:21:17 -07001976 case NETDEV_FEAT_CHANGE:
1977 fcoe_netdev_features_change(lport, netdev);
1978 break;
Robert Love85b4aa42008-12-09 15:10:24 -08001979 default:
Vasu Dev1d1b88d2009-07-29 17:05:45 -07001980 FCOE_NETDEV_DBG(netdev, "Unknown event %ld "
Robert Loved5488eb2009-06-10 15:30:59 -07001981 "from netdev netlink\n", event);
Robert Love85b4aa42008-12-09 15:10:24 -08001982 }
Robert Love5e4f8fe2010-05-07 15:18:35 -07001983
1984 fcoe_link_speed_update(lport);
1985
Robert Love1875f272009-11-03 11:47:50 -08001986 if (link_possible && !fcoe_link_ok(lport))
Chris Leech3fe9a0b2009-08-25 13:59:46 -07001987 fcoe_ctlr_link_up(&fcoe->ctlr);
1988 else if (fcoe_ctlr_link_down(&fcoe->ctlr)) {
Joe Eykholtf018b732010-03-12 16:08:55 -08001989 stats = per_cpu_ptr(lport->dev_stats, get_cpu());
Joe Eykholt97c83892009-03-17 11:42:40 -07001990 stats->LinkFailureCount++;
Joe Eykholtf018b732010-03-12 16:08:55 -08001991 put_cpu();
Robert Love1875f272009-11-03 11:47:50 -08001992 fcoe_clean_pending_queue(lport);
Robert Love85b4aa42008-12-09 15:10:24 -08001993 }
1994out:
1995 return rc;
1996}
1997
1998/**
Vasu Dev55a66d32009-12-10 09:59:31 -08001999 * fcoe_disable() - Disables a FCoE interface
Yi Zou78a58242011-01-28 16:05:16 -08002000 * @netdev : The net_device object the Ethernet interface to create on
Vasu Dev55a66d32009-12-10 09:59:31 -08002001 *
Yi Zou78a58242011-01-28 16:05:16 -08002002 * Called from fcoe transport.
Vasu Dev55a66d32009-12-10 09:59:31 -08002003 *
2004 * Returns: 0 for success
2005 */
Yi Zou78a58242011-01-28 16:05:16 -08002006static int fcoe_disable(struct net_device *netdev)
Vasu Dev55a66d32009-12-10 09:59:31 -08002007{
2008 struct fcoe_interface *fcoe;
Vasu Dev55a66d32009-12-10 09:59:31 -08002009 int rc = 0;
2010
2011 mutex_lock(&fcoe_config_mutex);
Vasu Dev55a66d32009-12-10 09:59:31 -08002012
Robert Loveee5df622011-04-01 16:05:59 -07002013 rtnl_lock();
Vasu Dev55a66d32009-12-10 09:59:31 -08002014 fcoe = fcoe_hostlist_lookup_port(netdev);
2015 rtnl_unlock();
2016
Chris Leech9ee50e42010-04-09 14:22:23 -07002017 if (fcoe) {
Chris Leech9ee50e42010-04-09 14:22:23 -07002018 fcoe_ctlr_link_down(&fcoe->ctlr);
Vasu Dev9d4cbc02010-07-20 15:19:26 -07002019 fcoe_clean_pending_queue(fcoe->ctlr.lp);
Chris Leech9ee50e42010-04-09 14:22:23 -07002020 } else
Vasu Dev55a66d32009-12-10 09:59:31 -08002021 rc = -ENODEV;
2022
Vasu Dev55a66d32009-12-10 09:59:31 -08002023 mutex_unlock(&fcoe_config_mutex);
2024 return rc;
2025}
2026
2027/**
2028 * fcoe_enable() - Enables a FCoE interface
Yi Zou78a58242011-01-28 16:05:16 -08002029 * @netdev : The net_device object the Ethernet interface to create on
Vasu Dev55a66d32009-12-10 09:59:31 -08002030 *
Yi Zou78a58242011-01-28 16:05:16 -08002031 * Called from fcoe transport.
Vasu Dev55a66d32009-12-10 09:59:31 -08002032 *
2033 * Returns: 0 for success
2034 */
Yi Zou78a58242011-01-28 16:05:16 -08002035static int fcoe_enable(struct net_device *netdev)
Vasu Dev55a66d32009-12-10 09:59:31 -08002036{
2037 struct fcoe_interface *fcoe;
Vasu Dev55a66d32009-12-10 09:59:31 -08002038 int rc = 0;
2039
2040 mutex_lock(&fcoe_config_mutex);
Robert Loveee5df622011-04-01 16:05:59 -07002041 rtnl_lock();
Vasu Dev55a66d32009-12-10 09:59:31 -08002042 fcoe = fcoe_hostlist_lookup_port(netdev);
2043 rtnl_unlock();
2044
Vasu Dev9d4cbc02010-07-20 15:19:26 -07002045 if (!fcoe)
Vasu Dev55a66d32009-12-10 09:59:31 -08002046 rc = -ENODEV;
Vasu Dev9d4cbc02010-07-20 15:19:26 -07002047 else if (!fcoe_link_ok(fcoe->ctlr.lp))
2048 fcoe_ctlr_link_up(&fcoe->ctlr);
Vasu Dev55a66d32009-12-10 09:59:31 -08002049
Vasu Dev55a66d32009-12-10 09:59:31 -08002050 mutex_unlock(&fcoe_config_mutex);
2051 return rc;
2052}
2053
2054/**
Robert Love1875f272009-11-03 11:47:50 -08002055 * fcoe_destroy() - Destroy a FCoE interface
Yi Zou78a58242011-01-28 16:05:16 -08002056 * @netdev : The net_device object the Ethernet interface to create on
Robert Love1875f272009-11-03 11:47:50 -08002057 *
Yi Zou78a58242011-01-28 16:05:16 -08002058 * Called from fcoe transport
Robert Love85b4aa42008-12-09 15:10:24 -08002059 *
2060 * Returns: 0 for success
Robert Love34f42a02009-02-27 10:55:45 -08002061 */
Yi Zou78a58242011-01-28 16:05:16 -08002062static int fcoe_destroy(struct net_device *netdev)
Robert Love85b4aa42008-12-09 15:10:24 -08002063{
Chris Leech030f4e02009-08-25 14:00:02 -07002064 struct fcoe_interface *fcoe;
Vasu Devf04ca1b2011-04-01 16:06:45 -07002065 struct fc_lport *lport;
Neerav Parikhb2085a42011-06-20 16:59:51 -07002066 struct fcoe_port *port;
Mike Christie8eca3552009-10-21 16:27:44 -07002067 int rc = 0;
Robert Love85b4aa42008-12-09 15:10:24 -08002068
Chris Leechdfc1d0f2009-08-25 14:00:13 -07002069 mutex_lock(&fcoe_config_mutex);
Robert Loveee5df622011-04-01 16:05:59 -07002070 rtnl_lock();
Chris Leech2e70e242009-08-25 14:00:23 -07002071 fcoe = fcoe_hostlist_lookup_port(netdev);
2072 if (!fcoe) {
Robert Love85b4aa42008-12-09 15:10:24 -08002073 rc = -ENODEV;
Yi Zou78a58242011-01-28 16:05:16 -08002074 goto out_nodev;
Robert Love85b4aa42008-12-09 15:10:24 -08002075 }
Vasu Devf04ca1b2011-04-01 16:06:45 -07002076 lport = fcoe->ctlr.lp;
Neerav Parikhb2085a42011-06-20 16:59:51 -07002077 port = lport_priv(lport);
Yi Zou54a5b212010-07-20 15:21:17 -07002078 list_del(&fcoe->list);
Neerav Parikhb2085a42011-06-20 16:59:51 -07002079 queue_work(fcoe_wq, &port->destroy_work);
Robert Love85b4aa42008-12-09 15:10:24 -08002080out_nodev:
Neerav Parikhb2085a42011-06-20 16:59:51 -07002081 rtnl_unlock();
Chris Leechdfc1d0f2009-08-25 14:00:13 -07002082 mutex_unlock(&fcoe_config_mutex);
Robert Love85b4aa42008-12-09 15:10:24 -08002083 return rc;
2084}
2085
Robert Love1875f272009-11-03 11:47:50 -08002086/**
2087 * fcoe_destroy_work() - Destroy a FCoE port in a deferred work context
2088 * @work: Handle to the FCoE port to be destroyed
2089 */
Chris Leech2e70e242009-08-25 14:00:23 -07002090static void fcoe_destroy_work(struct work_struct *work)
2091{
2092 struct fcoe_port *port;
Neerav Parikhb2085a42011-06-20 16:59:51 -07002093 struct fcoe_interface *fcoe;
Chris Leech2e70e242009-08-25 14:00:23 -07002094
2095 port = container_of(work, struct fcoe_port, destroy_work);
2096 mutex_lock(&fcoe_config_mutex);
Neerav Parikhb2085a42011-06-20 16:59:51 -07002097
Neerav Parikhb2085a42011-06-20 16:59:51 -07002098 fcoe = port->priv;
Chris Leech2e70e242009-08-25 14:00:23 -07002099 fcoe_if_destroy(port->lport);
Robert Loveccefd232012-02-10 17:18:41 -08002100 fcoe_interface_cleanup(fcoe);
Neerav Parikhb2085a42011-06-20 16:59:51 -07002101
Chris Leech2e70e242009-08-25 14:00:23 -07002102 mutex_unlock(&fcoe_config_mutex);
2103}
2104
Robert Love85b4aa42008-12-09 15:10:24 -08002105/**
Yi Zou78a58242011-01-28 16:05:16 -08002106 * fcoe_match() - Check if the FCoE is supported on the given netdevice
2107 * @netdev : The net_device object the Ethernet interface to create on
Robert Love1875f272009-11-03 11:47:50 -08002108 *
Yi Zou78a58242011-01-28 16:05:16 -08002109 * Called from fcoe transport.
2110 *
2111 * Returns: always returns true as this is the default FCoE transport,
2112 * i.e., support all netdevs.
2113 */
2114static bool fcoe_match(struct net_device *netdev)
2115{
2116 return true;
2117}
2118
2119/**
john fastabend6f6c2aa2011-11-18 13:35:56 -08002120 * fcoe_dcb_create() - Initialize DCB attributes and hooks
2121 * @netdev: The net_device object of the L2 link that should be queried
2122 * @port: The fcoe_port to bind FCoE APP priority with
2123 * @
2124 */
2125static void fcoe_dcb_create(struct fcoe_interface *fcoe)
2126{
2127#ifdef CONFIG_DCB
2128 int dcbx;
2129 u8 fup, up;
2130 struct net_device *netdev = fcoe->realdev;
2131 struct fcoe_port *port = lport_priv(fcoe->ctlr.lp);
2132 struct dcb_app app = {
2133 .priority = 0,
2134 .protocol = ETH_P_FCOE
2135 };
2136
2137 /* setup DCB priority attributes. */
2138 if (netdev && netdev->dcbnl_ops && netdev->dcbnl_ops->getdcbx) {
2139 dcbx = netdev->dcbnl_ops->getdcbx(netdev);
2140
2141 if (dcbx & DCB_CAP_DCBX_VER_IEEE) {
2142 app.selector = IEEE_8021QAZ_APP_SEL_ETHERTYPE;
2143 up = dcb_ieee_getapp_mask(netdev, &app);
2144 app.protocol = ETH_P_FIP;
2145 fup = dcb_ieee_getapp_mask(netdev, &app);
2146 } else {
2147 app.selector = DCB_APP_IDTYPE_ETHTYPE;
2148 up = dcb_getapp(netdev, &app);
2149 app.protocol = ETH_P_FIP;
2150 fup = dcb_getapp(netdev, &app);
2151 }
2152
2153 port->priority = ffs(up) ? ffs(up) - 1 : 0;
2154 fcoe->ctlr.priority = ffs(fup) ? ffs(fup) - 1 : port->priority;
2155 }
2156#endif
2157}
2158
2159/**
Yi Zou78a58242011-01-28 16:05:16 -08002160 * fcoe_create() - Create a fcoe interface
2161 * @netdev : The net_device object the Ethernet interface to create on
2162 * @fip_mode: The FIP mode for this creation
2163 *
2164 * Called from fcoe transport
Robert Love85b4aa42008-12-09 15:10:24 -08002165 *
2166 * Returns: 0 for success
Robert Love34f42a02009-02-27 10:55:45 -08002167 */
Yi Zou78a58242011-01-28 16:05:16 -08002168static int fcoe_create(struct net_device *netdev, enum fip_state fip_mode)
Robert Love85b4aa42008-12-09 15:10:24 -08002169{
Neerav Parikhb2085a42011-06-20 16:59:51 -07002170 int rc = 0;
Chris Leech030f4e02009-08-25 14:00:02 -07002171 struct fcoe_interface *fcoe;
Chris Leechaf7f85d2009-08-25 13:59:24 -07002172 struct fc_lport *lport;
Robert Love85b4aa42008-12-09 15:10:24 -08002173
Chris Leechdfc1d0f2009-08-25 14:00:13 -07002174 mutex_lock(&fcoe_config_mutex);
Robert Loveee5df622011-04-01 16:05:59 -07002175 rtnl_lock();
Vasu Dev34ce27b2010-05-07 15:18:46 -07002176
Robert Love85b4aa42008-12-09 15:10:24 -08002177 /* look for existing lport */
2178 if (fcoe_hostlist_lookup(netdev)) {
2179 rc = -EEXIST;
Yi Zou78a58242011-01-28 16:05:16 -08002180 goto out_nodev;
Robert Love85b4aa42008-12-09 15:10:24 -08002181 }
Robert Love85b4aa42008-12-09 15:10:24 -08002182
Joe Eykholt1dd454d2010-07-20 15:20:46 -07002183 fcoe = fcoe_interface_create(netdev, fip_mode);
Robert Love7287fb92011-01-28 16:03:47 -08002184 if (IS_ERR(fcoe)) {
2185 rc = PTR_ERR(fcoe);
Yi Zou78a58242011-01-28 16:05:16 -08002186 goto out_nodev;
Chris Leech030f4e02009-08-25 14:00:02 -07002187 }
2188
Chris Leech9a057532009-11-03 11:46:40 -08002189 lport = fcoe_if_create(fcoe, &netdev->dev, 0);
Chris Leechaf7f85d2009-08-25 13:59:24 -07002190 if (IS_ERR(lport)) {
Robert Loved5488eb2009-06-10 15:30:59 -07002191 printk(KERN_ERR "fcoe: Failed to create interface (%s)\n",
Robert Love85b4aa42008-12-09 15:10:24 -08002192 netdev->name);
Robert Love85b4aa42008-12-09 15:10:24 -08002193 rc = -EIO;
Robert Love848e7d52011-08-25 12:40:47 -07002194 rtnl_unlock();
Chris Leech2e70e242009-08-25 14:00:23 -07002195 fcoe_interface_cleanup(fcoe);
Robert Love848e7d52011-08-25 12:40:47 -07002196 goto out_nortnl;
Robert Love85b4aa42008-12-09 15:10:24 -08002197 }
Chris Leech030f4e02009-08-25 14:00:02 -07002198
Chris Leech54b649f2009-08-25 14:00:07 -07002199 /* Make this the "master" N_Port */
2200 fcoe->ctlr.lp = lport;
Chris Leech030f4e02009-08-25 14:00:02 -07002201
john fastabend6f6c2aa2011-11-18 13:35:56 -08002202 /* setup DCB priority attributes. */
2203 fcoe_dcb_create(fcoe);
2204
Chris Leechc863df32009-08-25 14:00:18 -07002205 /* add to lports list */
2206 fcoe_hostlist_add(lport);
2207
Chris Leech54b649f2009-08-25 14:00:07 -07002208 /* start FIP Discovery and FLOGI */
2209 lport->boot_time = jiffies;
2210 fc_fabric_login(lport);
Robert Love22805122012-03-13 18:22:12 -07002211 if (!fcoe_link_ok(lport)) {
2212 rtnl_unlock();
Chris Leech54b649f2009-08-25 14:00:07 -07002213 fcoe_ctlr_link_up(&fcoe->ctlr);
Robert Love22805122012-03-13 18:22:12 -07002214 mutex_unlock(&fcoe_config_mutex);
2215 return rc;
2216 }
Chris Leech54b649f2009-08-25 14:00:07 -07002217
Robert Love85b4aa42008-12-09 15:10:24 -08002218out_nodev:
Vasu Dev34ce27b2010-05-07 15:18:46 -07002219 rtnl_unlock();
Robert Love848e7d52011-08-25 12:40:47 -07002220out_nortnl:
Chris Leechdfc1d0f2009-08-25 14:00:13 -07002221 mutex_unlock(&fcoe_config_mutex);
Robert Love85b4aa42008-12-09 15:10:24 -08002222 return rc;
2223}
2224
Robert Love34f42a02009-02-27 10:55:45 -08002225/**
Robert Love5e4f8fe2010-05-07 15:18:35 -07002226 * fcoe_link_speed_update() - Update the supported and actual link speeds
2227 * @lport: The local port to update speeds for
Robert Love85b4aa42008-12-09 15:10:24 -08002228 *
Robert Love5e4f8fe2010-05-07 15:18:35 -07002229 * Returns: 0 if the ethtool query was successful
2230 * -1 if the ethtool query failed
Robert Love85b4aa42008-12-09 15:10:24 -08002231 */
Bart Van Assche7c9c6842012-01-13 17:26:25 -08002232static int fcoe_link_speed_update(struct fc_lport *lport)
Robert Love85b4aa42008-12-09 15:10:24 -08002233{
Bhanu Prakash Gollapudi8597ae82011-01-28 16:05:37 -08002234 struct net_device *netdev = fcoe_netdev(lport);
David Decotigny8ae6dac2011-04-27 18:32:38 +00002235 struct ethtool_cmd ecmd;
Robert Love85b4aa42008-12-09 15:10:24 -08002236
Jiri Pirko4bc71cb2011-09-03 03:34:30 +00002237 if (!__ethtool_get_settings(netdev, &ecmd)) {
Robert Love1875f272009-11-03 11:47:50 -08002238 lport->link_supported_speeds &=
Yi Zou2f718d62009-07-29 17:04:01 -07002239 ~(FC_PORTSPEED_1GBIT | FC_PORTSPEED_10GBIT);
2240 if (ecmd.supported & (SUPPORTED_1000baseT_Half |
2241 SUPPORTED_1000baseT_Full))
Robert Love1875f272009-11-03 11:47:50 -08002242 lport->link_supported_speeds |= FC_PORTSPEED_1GBIT;
Yi Zou2f718d62009-07-29 17:04:01 -07002243 if (ecmd.supported & SUPPORTED_10000baseT_Full)
Robert Love1875f272009-11-03 11:47:50 -08002244 lport->link_supported_speeds |=
Yi Zou2f718d62009-07-29 17:04:01 -07002245 FC_PORTSPEED_10GBIT;
David Decotigny8ae6dac2011-04-27 18:32:38 +00002246 switch (ethtool_cmd_speed(&ecmd)) {
2247 case SPEED_1000:
Robert Love1875f272009-11-03 11:47:50 -08002248 lport->link_speed = FC_PORTSPEED_1GBIT;
David Decotigny8ae6dac2011-04-27 18:32:38 +00002249 break;
2250 case SPEED_10000:
Robert Love1875f272009-11-03 11:47:50 -08002251 lport->link_speed = FC_PORTSPEED_10GBIT;
David Decotigny8ae6dac2011-04-27 18:32:38 +00002252 break;
2253 }
Yi Zou2f718d62009-07-29 17:04:01 -07002254 return 0;
2255 }
2256 return -1;
Robert Love85b4aa42008-12-09 15:10:24 -08002257}
Robert Love85b4aa42008-12-09 15:10:24 -08002258
Robert Love34f42a02009-02-27 10:55:45 -08002259/**
Robert Love5e4f8fe2010-05-07 15:18:35 -07002260 * fcoe_link_ok() - Check if the link is OK for a local port
2261 * @lport: The local port to check link on
2262 *
2263 * Returns: 0 if link is UP and OK, -1 if not
2264 *
2265 */
Bart Van Assche7c9c6842012-01-13 17:26:25 -08002266static int fcoe_link_ok(struct fc_lport *lport)
Robert Love5e4f8fe2010-05-07 15:18:35 -07002267{
Bhanu Prakash Gollapudi8597ae82011-01-28 16:05:37 -08002268 struct net_device *netdev = fcoe_netdev(lport);
Robert Love5e4f8fe2010-05-07 15:18:35 -07002269
2270 if (netif_oper_up(netdev))
2271 return 0;
2272 return -1;
2273}
2274
2275/**
Robert Love1875f272009-11-03 11:47:50 -08002276 * fcoe_percpu_clean() - Clear all pending skbs for an local port
2277 * @lport: The local port whose skbs are to be cleared
Joe Eykholte7a51992009-08-25 14:04:08 -07002278 *
2279 * Must be called with fcoe_create_mutex held to single-thread completion.
2280 *
2281 * This flushes the pending skbs by adding a new skb to each queue and
2282 * waiting until they are all freed. This assures us that not only are
2283 * there no packets that will be handled by the lport, but also that any
2284 * threads already handling packet have returned.
Robert Love85b4aa42008-12-09 15:10:24 -08002285 */
Bart Van Assche7c9c6842012-01-13 17:26:25 -08002286static void fcoe_percpu_clean(struct fc_lport *lport)
Robert Love85b4aa42008-12-09 15:10:24 -08002287{
Robert Love85b4aa42008-12-09 15:10:24 -08002288 struct fcoe_percpu_s *pp;
Neil Hormandd060e72012-03-09 14:50:19 -08002289 struct sk_buff *skb;
Robert Love5e5e92d2009-03-17 11:41:35 -07002290 unsigned int cpu;
Robert Love85b4aa42008-12-09 15:10:24 -08002291
Robert Love5e5e92d2009-03-17 11:41:35 -07002292 for_each_possible_cpu(cpu) {
2293 pp = &per_cpu(fcoe_percpu, cpu);
Joe Eykholte7a51992009-08-25 14:04:08 -07002294
Neil Hormandd060e72012-03-09 14:50:19 -08002295 if (!pp->thread || !cpu_online(cpu))
Joe Eykholte7a51992009-08-25 14:04:08 -07002296 continue;
Joe Eykholte7a51992009-08-25 14:04:08 -07002297
2298 skb = dev_alloc_skb(0);
2299 if (!skb) {
2300 spin_unlock_bh(&pp->fcoe_rx_list.lock);
2301 continue;
2302 }
2303 skb->destructor = fcoe_percpu_flush_done;
2304
Neil Hormandd060e72012-03-09 14:50:19 -08002305 spin_lock_bh(&pp->fcoe_rx_list.lock);
Joe Eykholte7a51992009-08-25 14:04:08 -07002306 __skb_queue_tail(&pp->fcoe_rx_list, skb);
2307 if (pp->fcoe_rx_list.qlen == 1)
2308 wake_up_process(pp->thread);
Robert Love5e5e92d2009-03-17 11:41:35 -07002309 spin_unlock_bh(&pp->fcoe_rx_list.lock);
Joe Eykholte7a51992009-08-25 14:04:08 -07002310
2311 wait_for_completion(&fcoe_flush_completion);
Robert Love85b4aa42008-12-09 15:10:24 -08002312 }
2313}
Robert Love85b4aa42008-12-09 15:10:24 -08002314
2315/**
Robert Love1875f272009-11-03 11:47:50 -08002316 * fcoe_reset() - Reset a local port
2317 * @shost: The SCSI host associated with the local port to be reset
Robert Love85b4aa42008-12-09 15:10:24 -08002318 *
Robert Love1875f272009-11-03 11:47:50 -08002319 * Returns: Always 0 (return value required by FC transport template)
Robert Love85b4aa42008-12-09 15:10:24 -08002320 */
Bart Van Assche7c9c6842012-01-13 17:26:25 -08002321static int fcoe_reset(struct Scsi_Host *shost)
Robert Love85b4aa42008-12-09 15:10:24 -08002322{
2323 struct fc_lport *lport = shost_priv(shost);
Vasu Devd2f80952011-02-25 15:03:28 -08002324 struct fcoe_port *port = lport_priv(lport);
2325 struct fcoe_interface *fcoe = port->priv;
2326
2327 fcoe_ctlr_link_down(&fcoe->ctlr);
2328 fcoe_clean_pending_queue(fcoe->ctlr.lp);
2329 if (!fcoe_link_ok(fcoe->ctlr.lp))
2330 fcoe_ctlr_link_up(&fcoe->ctlr);
Robert Love85b4aa42008-12-09 15:10:24 -08002331 return 0;
2332}
Robert Love85b4aa42008-12-09 15:10:24 -08002333
Robert Love34f42a02009-02-27 10:55:45 -08002334/**
Robert Love1875f272009-11-03 11:47:50 -08002335 * fcoe_hostlist_lookup_port() - Find the FCoE interface associated with a net device
2336 * @netdev: The net device used as a key
Robert Love85b4aa42008-12-09 15:10:24 -08002337 *
Robert Love1875f272009-11-03 11:47:50 -08002338 * Locking: Must be called with the RNL mutex held.
2339 *
2340 * Returns: NULL or the FCoE interface
Robert Love85b4aa42008-12-09 15:10:24 -08002341 */
Chris Leech014f5c32009-08-25 13:59:30 -07002342static struct fcoe_interface *
Robert Love1875f272009-11-03 11:47:50 -08002343fcoe_hostlist_lookup_port(const struct net_device *netdev)
Robert Love85b4aa42008-12-09 15:10:24 -08002344{
Chris Leech014f5c32009-08-25 13:59:30 -07002345 struct fcoe_interface *fcoe;
Robert Love85b4aa42008-12-09 15:10:24 -08002346
Chris Leech014f5c32009-08-25 13:59:30 -07002347 list_for_each_entry(fcoe, &fcoe_hostlist, list) {
Robert Love1875f272009-11-03 11:47:50 -08002348 if (fcoe->netdev == netdev)
Chris Leech014f5c32009-08-25 13:59:30 -07002349 return fcoe;
Robert Love85b4aa42008-12-09 15:10:24 -08002350 }
Robert Love85b4aa42008-12-09 15:10:24 -08002351 return NULL;
2352}
2353
Robert Love34f42a02009-02-27 10:55:45 -08002354/**
Robert Love1875f272009-11-03 11:47:50 -08002355 * fcoe_hostlist_lookup() - Find the local port associated with a
2356 * given net device
2357 * @netdev: The netdevice used as a key
Robert Love85b4aa42008-12-09 15:10:24 -08002358 *
Robert Love1875f272009-11-03 11:47:50 -08002359 * Locking: Must be called with the RTNL mutex held
2360 *
2361 * Returns: NULL or the local port
Robert Love85b4aa42008-12-09 15:10:24 -08002362 */
Chris Leech090eb6c2009-08-25 14:00:28 -07002363static struct fc_lport *fcoe_hostlist_lookup(const struct net_device *netdev)
Robert Love85b4aa42008-12-09 15:10:24 -08002364{
Chris Leech014f5c32009-08-25 13:59:30 -07002365 struct fcoe_interface *fcoe;
Robert Love85b4aa42008-12-09 15:10:24 -08002366
Chris Leech014f5c32009-08-25 13:59:30 -07002367 fcoe = fcoe_hostlist_lookup_port(netdev);
Chris Leech3fe9a0b2009-08-25 13:59:46 -07002368 return (fcoe) ? fcoe->ctlr.lp : NULL;
Robert Love85b4aa42008-12-09 15:10:24 -08002369}
Robert Love85b4aa42008-12-09 15:10:24 -08002370
Robert Love34f42a02009-02-27 10:55:45 -08002371/**
Robert Love1875f272009-11-03 11:47:50 -08002372 * fcoe_hostlist_add() - Add the FCoE interface identified by a local
2373 * port to the hostlist
2374 * @lport: The local port that identifies the FCoE interface to be added
2375 *
2376 * Locking: must be called with the RTNL mutex held
Robert Love85b4aa42008-12-09 15:10:24 -08002377 *
2378 * Returns: 0 for success
2379 */
Chris Leech090eb6c2009-08-25 14:00:28 -07002380static int fcoe_hostlist_add(const struct fc_lport *lport)
Robert Love85b4aa42008-12-09 15:10:24 -08002381{
Chris Leech014f5c32009-08-25 13:59:30 -07002382 struct fcoe_interface *fcoe;
2383 struct fcoe_port *port;
Robert Love85b4aa42008-12-09 15:10:24 -08002384
Chris Leech014f5c32009-08-25 13:59:30 -07002385 fcoe = fcoe_hostlist_lookup_port(fcoe_netdev(lport));
2386 if (!fcoe) {
2387 port = lport_priv(lport);
Bhanu Prakash Gollapudi8597ae82011-01-28 16:05:37 -08002388 fcoe = port->priv;
Chris Leech014f5c32009-08-25 13:59:30 -07002389 list_add_tail(&fcoe->list, &fcoe_hostlist);
Robert Love85b4aa42008-12-09 15:10:24 -08002390 }
2391 return 0;
2392}
Robert Love85b4aa42008-12-09 15:10:24 -08002393
Yi Zou78a58242011-01-28 16:05:16 -08002394
2395static struct fcoe_transport fcoe_sw_transport = {
2396 .name = {FCOE_TRANSPORT_DEFAULT},
2397 .attached = false,
2398 .list = LIST_HEAD_INIT(fcoe_sw_transport.list),
2399 .match = fcoe_match,
2400 .create = fcoe_create,
2401 .destroy = fcoe_destroy,
2402 .enable = fcoe_enable,
2403 .disable = fcoe_disable,
2404};
2405
Robert Love34f42a02009-02-27 10:55:45 -08002406/**
Robert Love1875f272009-11-03 11:47:50 -08002407 * fcoe_init() - Initialize fcoe.ko
Robert Love85b4aa42008-12-09 15:10:24 -08002408 *
Robert Love1875f272009-11-03 11:47:50 -08002409 * Returns: 0 on success, or a negative value on failure
Robert Love34f42a02009-02-27 10:55:45 -08002410 */
Robert Love85b4aa42008-12-09 15:10:24 -08002411static int __init fcoe_init(void)
2412{
Robert Love1875f272009-11-03 11:47:50 -08002413 struct fcoe_percpu_s *p;
Robert Love38eccab2009-03-17 11:41:30 -07002414 unsigned int cpu;
Robert Love8976f422009-03-17 11:41:46 -07002415 int rc = 0;
Robert Love85b4aa42008-12-09 15:10:24 -08002416
Tejun Heo2ca32b42011-01-28 16:05:32 -08002417 fcoe_wq = alloc_workqueue("fcoe", 0, 0);
2418 if (!fcoe_wq)
2419 return -ENOMEM;
2420
Yi Zou78a58242011-01-28 16:05:16 -08002421 /* register as a fcoe transport */
2422 rc = fcoe_transport_attach(&fcoe_sw_transport);
2423 if (rc) {
2424 printk(KERN_ERR "failed to register an fcoe transport, check "
2425 "if libfcoe is loaded\n");
2426 return rc;
2427 }
2428
Chris Leechdfc1d0f2009-08-25 14:00:13 -07002429 mutex_lock(&fcoe_config_mutex);
2430
Robert Love38eccab2009-03-17 11:41:30 -07002431 for_each_possible_cpu(cpu) {
Robert Love5e5e92d2009-03-17 11:41:35 -07002432 p = &per_cpu(fcoe_percpu, cpu);
Robert Love38eccab2009-03-17 11:41:30 -07002433 skb_queue_head_init(&p->fcoe_rx_list);
2434 }
2435
Robert Love8976f422009-03-17 11:41:46 -07002436 for_each_online_cpu(cpu)
2437 fcoe_percpu_thread_create(cpu);
Robert Love85b4aa42008-12-09 15:10:24 -08002438
Robert Love8976f422009-03-17 11:41:46 -07002439 /* Initialize per CPU interrupt thread */
2440 rc = register_hotcpu_notifier(&fcoe_cpu_notifier);
2441 if (rc)
2442 goto out_free;
Robert Love85b4aa42008-12-09 15:10:24 -08002443
Robert Love8976f422009-03-17 11:41:46 -07002444 /* Setup link change notification */
Robert Love85b4aa42008-12-09 15:10:24 -08002445 fcoe_dev_setup();
2446
Chris Leech5892c322009-08-25 13:59:14 -07002447 rc = fcoe_if_init();
2448 if (rc)
2449 goto out_free;
Robert Love85b4aa42008-12-09 15:10:24 -08002450
Chris Leechdfc1d0f2009-08-25 14:00:13 -07002451 mutex_unlock(&fcoe_config_mutex);
Robert Love85b4aa42008-12-09 15:10:24 -08002452 return 0;
Robert Love8976f422009-03-17 11:41:46 -07002453
2454out_free:
2455 for_each_online_cpu(cpu) {
2456 fcoe_percpu_thread_destroy(cpu);
2457 }
Chris Leechdfc1d0f2009-08-25 14:00:13 -07002458 mutex_unlock(&fcoe_config_mutex);
Tejun Heo2ca32b42011-01-28 16:05:32 -08002459 destroy_workqueue(fcoe_wq);
Robert Love8976f422009-03-17 11:41:46 -07002460 return rc;
Robert Love85b4aa42008-12-09 15:10:24 -08002461}
2462module_init(fcoe_init);
2463
2464/**
Robert Love1875f272009-11-03 11:47:50 -08002465 * fcoe_exit() - Clean up fcoe.ko
Robert Love85b4aa42008-12-09 15:10:24 -08002466 *
Robert Love1875f272009-11-03 11:47:50 -08002467 * Returns: 0 on success or a negative value on failure
Robert Love34f42a02009-02-27 10:55:45 -08002468 */
Robert Love85b4aa42008-12-09 15:10:24 -08002469static void __exit fcoe_exit(void)
2470{
Chris Leech014f5c32009-08-25 13:59:30 -07002471 struct fcoe_interface *fcoe, *tmp;
Chris Leech2e70e242009-08-25 14:00:23 -07002472 struct fcoe_port *port;
Robert Love1875f272009-11-03 11:47:50 -08002473 unsigned int cpu;
Robert Love85b4aa42008-12-09 15:10:24 -08002474
Chris Leechdfc1d0f2009-08-25 14:00:13 -07002475 mutex_lock(&fcoe_config_mutex);
2476
Robert Love85b4aa42008-12-09 15:10:24 -08002477 fcoe_dev_cleanup();
2478
Vasu Dev5919a592009-03-27 09:03:29 -07002479 /* releases the associated fcoe hosts */
Chris Leech090eb6c2009-08-25 14:00:28 -07002480 rtnl_lock();
2481 list_for_each_entry_safe(fcoe, tmp, &fcoe_hostlist, list) {
Chris Leechc863df32009-08-25 14:00:18 -07002482 list_del(&fcoe->list);
Chris Leech2e70e242009-08-25 14:00:23 -07002483 port = lport_priv(fcoe->ctlr.lp);
Tejun Heo2ca32b42011-01-28 16:05:32 -08002484 queue_work(fcoe_wq, &port->destroy_work);
Chris Leechc863df32009-08-25 14:00:18 -07002485 }
Chris Leech090eb6c2009-08-25 14:00:28 -07002486 rtnl_unlock();
Robert Love85b4aa42008-12-09 15:10:24 -08002487
Robert Love8976f422009-03-17 11:41:46 -07002488 unregister_hotcpu_notifier(&fcoe_cpu_notifier);
2489
Chris Leech014f5c32009-08-25 13:59:30 -07002490 for_each_online_cpu(cpu)
Robert Love8976f422009-03-17 11:41:46 -07002491 fcoe_percpu_thread_destroy(cpu);
Robert Love85b4aa42008-12-09 15:10:24 -08002492
Chris Leechdfc1d0f2009-08-25 14:00:13 -07002493 mutex_unlock(&fcoe_config_mutex);
Chris Leech2e70e242009-08-25 14:00:23 -07002494
Tejun Heo2ca32b42011-01-28 16:05:32 -08002495 /*
2496 * destroy_work's may be chained but destroy_workqueue()
2497 * can take care of them. Just kill the fcoe_wq.
2498 */
2499 destroy_workqueue(fcoe_wq);
Chris Leech2e70e242009-08-25 14:00:23 -07002500
Tejun Heo2ca32b42011-01-28 16:05:32 -08002501 /*
2502 * Detaching from the scsi transport must happen after all
2503 * destroys are done on the fcoe_wq. destroy_workqueue will
2504 * enusre the fcoe_wq is flushed.
2505 */
Chris Leech2e70e242009-08-25 14:00:23 -07002506 fcoe_if_exit();
Yi Zou78a58242011-01-28 16:05:16 -08002507
2508 /* detach from fcoe transport */
2509 fcoe_transport_detach(&fcoe_sw_transport);
Robert Love85b4aa42008-12-09 15:10:24 -08002510}
2511module_exit(fcoe_exit);
Chris Leech11b56182009-11-03 11:46:29 -08002512
2513/**
2514 * fcoe_flogi_resp() - FCoE specific FLOGI and FDISC response handler
2515 * @seq: active sequence in the FLOGI or FDISC exchange
2516 * @fp: response frame, or error encoded in a pointer (timeout)
2517 * @arg: pointer the the fcoe_ctlr structure
2518 *
Uwe Kleine-König65155b32010-06-11 12:17:01 +02002519 * This handles MAC address management for FCoE, then passes control on to
Chris Leech11b56182009-11-03 11:46:29 -08002520 * the libfc FLOGI response handler.
2521 */
2522static void fcoe_flogi_resp(struct fc_seq *seq, struct fc_frame *fp, void *arg)
2523{
2524 struct fcoe_ctlr *fip = arg;
2525 struct fc_exch *exch = fc_seq_exch(seq);
2526 struct fc_lport *lport = exch->lp;
2527 u8 *mac;
2528
2529 if (IS_ERR(fp))
2530 goto done;
2531
2532 mac = fr_cb(fp)->granted_mac;
Vasu Dev907c07d2011-10-28 11:34:23 -07002533 /* pre-FIP */
2534 if (is_zero_ether_addr(mac))
2535 fcoe_ctlr_recv_flogi(fip, lport, fp);
2536 if (!is_zero_ether_addr(mac))
2537 fcoe_update_src_mac(lport, mac);
Chris Leech11b56182009-11-03 11:46:29 -08002538done:
2539 fc_lport_flogi_resp(seq, fp, lport);
2540}
2541
2542/**
2543 * fcoe_logo_resp() - FCoE specific LOGO response handler
2544 * @seq: active sequence in the LOGO exchange
2545 * @fp: response frame, or error encoded in a pointer (timeout)
2546 * @arg: pointer the the fcoe_ctlr structure
2547 *
Uwe Kleine-König65155b32010-06-11 12:17:01 +02002548 * This handles MAC address management for FCoE, then passes control on to
Chris Leech11b56182009-11-03 11:46:29 -08002549 * the libfc LOGO response handler.
2550 */
2551static void fcoe_logo_resp(struct fc_seq *seq, struct fc_frame *fp, void *arg)
2552{
Joe Eykholt386309c2009-11-03 11:49:16 -08002553 struct fc_lport *lport = arg;
Chris Leech11b56182009-11-03 11:46:29 -08002554 static u8 zero_mac[ETH_ALEN] = { 0 };
2555
2556 if (!IS_ERR(fp))
Joe Eykholt386309c2009-11-03 11:49:16 -08002557 fcoe_update_src_mac(lport, zero_mac);
Chris Leech11b56182009-11-03 11:46:29 -08002558 fc_lport_logo_resp(seq, fp, lport);
2559}
2560
2561/**
2562 * fcoe_elsct_send - FCoE specific ELS handler
2563 *
2564 * This does special case handling of FIP encapsualted ELS exchanges for FCoE,
2565 * using FCoE specific response handlers and passing the FIP controller as
2566 * the argument (the lport is still available from the exchange).
2567 *
2568 * Most of the work here is just handed off to the libfc routine.
2569 */
Robert Love1875f272009-11-03 11:47:50 -08002570static struct fc_seq *fcoe_elsct_send(struct fc_lport *lport, u32 did,
2571 struct fc_frame *fp, unsigned int op,
2572 void (*resp)(struct fc_seq *,
2573 struct fc_frame *,
2574 void *),
2575 void *arg, u32 timeout)
Chris Leech11b56182009-11-03 11:46:29 -08002576{
2577 struct fcoe_port *port = lport_priv(lport);
Bhanu Prakash Gollapudi8597ae82011-01-28 16:05:37 -08002578 struct fcoe_interface *fcoe = port->priv;
Chris Leech11b56182009-11-03 11:46:29 -08002579 struct fcoe_ctlr *fip = &fcoe->ctlr;
2580 struct fc_frame_header *fh = fc_frame_header_get(fp);
2581
2582 switch (op) {
2583 case ELS_FLOGI:
2584 case ELS_FDISC:
Joe Eykholte10f8c62010-07-20 15:20:30 -07002585 if (lport->point_to_multipoint)
2586 break;
Chris Leech11b56182009-11-03 11:46:29 -08002587 return fc_elsct_send(lport, did, fp, op, fcoe_flogi_resp,
2588 fip, timeout);
2589 case ELS_LOGO:
2590 /* only hook onto fabric logouts, not port logouts */
2591 if (ntoh24(fh->fh_d_id) != FC_FID_FLOGI)
2592 break;
2593 return fc_elsct_send(lport, did, fp, op, fcoe_logo_resp,
Joe Eykholt386309c2009-11-03 11:49:16 -08002594 lport, timeout);
Chris Leech11b56182009-11-03 11:46:29 -08002595 }
2596 return fc_elsct_send(lport, did, fp, op, resp, arg, timeout);
2597}
2598
Chris Leech9a057532009-11-03 11:46:40 -08002599/**
2600 * fcoe_vport_create() - create an fc_host/scsi_host for a vport
2601 * @vport: fc_vport object to create a new fc_host for
2602 * @disabled: start the new fc_host in a disabled state by default?
2603 *
2604 * Returns: 0 for success
2605 */
2606static int fcoe_vport_create(struct fc_vport *vport, bool disabled)
2607{
2608 struct Scsi_Host *shost = vport_to_shost(vport);
2609 struct fc_lport *n_port = shost_priv(shost);
2610 struct fcoe_port *port = lport_priv(n_port);
Bhanu Prakash Gollapudi8597ae82011-01-28 16:05:37 -08002611 struct fcoe_interface *fcoe = port->priv;
Chris Leech9a057532009-11-03 11:46:40 -08002612 struct net_device *netdev = fcoe->netdev;
2613 struct fc_lport *vn_port;
Neerav Parikhbdf25212011-05-16 16:45:29 -07002614 int rc;
2615 char buf[32];
2616
2617 rc = fcoe_validate_vport_create(vport);
2618 if (rc) {
Bhanu Prakash Gollapudid8348952011-08-04 17:38:49 -07002619 fcoe_wwn_to_str(vport->port_name, buf, sizeof(buf));
Neerav Parikhbdf25212011-05-16 16:45:29 -07002620 printk(KERN_ERR "fcoe: Failed to create vport, "
2621 "WWPN (0x%s) already exists\n",
2622 buf);
2623 return rc;
2624 }
Chris Leech9a057532009-11-03 11:46:40 -08002625
2626 mutex_lock(&fcoe_config_mutex);
Jiri Pirko4bc71cb2011-09-03 03:34:30 +00002627 rtnl_lock();
Chris Leech9a057532009-11-03 11:46:40 -08002628 vn_port = fcoe_if_create(fcoe, &vport->dev, 1);
Jiri Pirko4bc71cb2011-09-03 03:34:30 +00002629 rtnl_unlock();
Chris Leech9a057532009-11-03 11:46:40 -08002630 mutex_unlock(&fcoe_config_mutex);
2631
2632 if (IS_ERR(vn_port)) {
2633 printk(KERN_ERR "fcoe: fcoe_vport_create(%s) failed\n",
2634 netdev->name);
2635 return -EIO;
2636 }
2637
2638 if (disabled) {
2639 fc_vport_set_state(vport, FC_VPORT_DISABLED);
2640 } else {
2641 vn_port->boot_time = jiffies;
2642 fc_fabric_login(vn_port);
2643 fc_vport_setlink(vn_port);
2644 }
2645 return 0;
2646}
2647
2648/**
2649 * fcoe_vport_destroy() - destroy the fc_host/scsi_host for a vport
2650 * @vport: fc_vport object that is being destroyed
2651 *
2652 * Returns: 0 for success
2653 */
2654static int fcoe_vport_destroy(struct fc_vport *vport)
2655{
2656 struct Scsi_Host *shost = vport_to_shost(vport);
2657 struct fc_lport *n_port = shost_priv(shost);
2658 struct fc_lport *vn_port = vport->dd_data;
Chris Leech9a057532009-11-03 11:46:40 -08002659
2660 mutex_lock(&n_port->lp_mutex);
2661 list_del(&vn_port->list);
2662 mutex_unlock(&n_port->lp_mutex);
Robert Loveccefd232012-02-10 17:18:41 -08002663
2664 mutex_lock(&fcoe_config_mutex);
2665 fcoe_if_destroy(vn_port);
2666 mutex_unlock(&fcoe_config_mutex);
2667
Chris Leech9a057532009-11-03 11:46:40 -08002668 return 0;
2669}
2670
2671/**
2672 * fcoe_vport_disable() - change vport state
2673 * @vport: vport to bring online/offline
2674 * @disable: should the vport be disabled?
2675 */
2676static int fcoe_vport_disable(struct fc_vport *vport, bool disable)
2677{
2678 struct fc_lport *lport = vport->dd_data;
2679
2680 if (disable) {
2681 fc_vport_set_state(vport, FC_VPORT_DISABLED);
2682 fc_fabric_logoff(lport);
2683 } else {
2684 lport->boot_time = jiffies;
2685 fc_fabric_login(lport);
2686 fc_vport_setlink(lport);
2687 }
2688
2689 return 0;
2690}
2691
Chris Leechdc8596d2009-11-03 11:47:18 -08002692/**
2693 * fcoe_vport_set_symbolic_name() - append vport string to symbolic name
2694 * @vport: fc_vport with a new symbolic name string
2695 *
2696 * After generating a new symbolic name string, a new RSPN_ID request is
2697 * sent to the name server. There is no response handler, so if it fails
2698 * for some reason it will not be retried.
2699 */
2700static void fcoe_set_vport_symbolic_name(struct fc_vport *vport)
2701{
2702 struct fc_lport *lport = vport->dd_data;
2703 struct fc_frame *fp;
2704 size_t len;
2705
2706 snprintf(fc_host_symbolic_name(lport->host), FC_SYMBOLIC_NAME_SIZE,
2707 "%s v%s over %s : %s", FCOE_NAME, FCOE_VERSION,
2708 fcoe_netdev(lport)->name, vport->symbolic_name);
2709
2710 if (lport->state != LPORT_ST_READY)
2711 return;
2712
2713 len = strnlen(fc_host_symbolic_name(lport->host), 255);
2714 fp = fc_frame_alloc(lport,
2715 sizeof(struct fc_ct_hdr) +
2716 sizeof(struct fc_ns_rspn) + len);
2717 if (!fp)
2718 return;
2719 lport->tt.elsct_send(lport, FC_FID_DIR_SERV, fp, FC_NS_RSPN_ID,
Joe Eykholtb94f8952009-11-03 11:50:21 -08002720 NULL, NULL, 3 * lport->r_a_tov);
Chris Leechdc8596d2009-11-03 11:47:18 -08002721}
Yi Zoub84056b2009-11-20 14:55:19 -08002722
2723/**
2724 * fcoe_get_lesb() - Fill the FCoE Link Error Status Block
2725 * @lport: the local port
2726 * @fc_lesb: the link error status block
2727 */
2728static void fcoe_get_lesb(struct fc_lport *lport,
2729 struct fc_els_lesb *fc_lesb)
2730{
Yi Zoub84056b2009-11-20 14:55:19 -08002731 struct net_device *netdev = fcoe_netdev(lport);
2732
Bhanu Prakash Gollapudi814740d2011-10-03 16:45:01 -07002733 __fcoe_get_lesb(lport, fc_lesb, netdev);
Yi Zoub84056b2009-11-20 14:55:19 -08002734}
Joe Eykholt7d65b0d2010-03-12 16:08:02 -08002735
2736/**
2737 * fcoe_set_port_id() - Callback from libfc when Port_ID is set.
2738 * @lport: the local port
2739 * @port_id: the port ID
2740 * @fp: the received frame, if any, that caused the port_id to be set.
2741 *
2742 * This routine handles the case where we received a FLOGI and are
2743 * entering point-to-point mode. We need to call fcoe_ctlr_recv_flogi()
2744 * so it can set the non-mapped mode and gateway address.
2745 *
2746 * The FLOGI LS_ACC is handled by fcoe_flogi_resp().
2747 */
2748static void fcoe_set_port_id(struct fc_lport *lport,
2749 u32 port_id, struct fc_frame *fp)
2750{
2751 struct fcoe_port *port = lport_priv(lport);
Bhanu Prakash Gollapudi8597ae82011-01-28 16:05:37 -08002752 struct fcoe_interface *fcoe = port->priv;
Joe Eykholt7d65b0d2010-03-12 16:08:02 -08002753
2754 if (fp && fc_frame_payload_op(fp) == ELS_FLOGI)
2755 fcoe_ctlr_recv_flogi(&fcoe->ctlr, lport, fp);
2756}