blob: 961bf364429ae065f497cd84659f92ec4b76666b [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 Devf04ca1b2011-04-01 16:06:45 -0700414 * fcoe_interface_cleanup() - Clean up a FCoE interface
415 * @fcoe: The FCoE interface to be cleaned up
416 *
417 * Caller must be holding the RTNL mutex
418 */
Bart Van Assche7c9c6842012-01-13 17:26:25 -0800419static void fcoe_interface_cleanup(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
Robert Love848e7d52011-08-25 12:40:47 -0700426 rtnl_lock();
427
Vasu Devf04ca1b2011-04-01 16:06:45 -0700428 /*
429 * Don't listen for Ethernet packets anymore.
430 * synchronize_net() ensures that the packet handlers are not running
431 * on another CPU. dev_remove_pack() would do that, this calls the
432 * unsyncronized version __dev_remove_pack() to avoid multiple delays.
433 */
434 __dev_remove_pack(&fcoe->fcoe_packet_type);
435 __dev_remove_pack(&fcoe->fip_packet_type);
436 synchronize_net();
437
438 /* Delete secondary MAC addresses */
439 memcpy(flogi_maddr, (u8[6]) FC_FCOE_FLOGI_MAC, ETH_ALEN);
440 dev_uc_del(netdev, flogi_maddr);
441 if (fip->spma)
442 dev_uc_del(netdev, fip->ctl_src_addr);
443 if (fip->mode == FIP_MODE_VN2VN) {
444 dev_mc_del(netdev, FIP_ALL_VN2VN_MACS);
445 dev_mc_del(netdev, FIP_ALL_P2P_MACS);
446 } else
447 dev_mc_del(netdev, FIP_ALL_ENODE_MACS);
448
Vasu Devf04ca1b2011-04-01 16:06:45 -0700449 /* Tell the LLD we are done w/ FCoE */
450 ops = netdev->netdev_ops;
451 if (ops->ndo_fcoe_disable) {
452 if (ops->ndo_fcoe_disable(netdev))
453 FCOE_NETDEV_DBG(netdev, "Failed to disable FCoE"
454 " specific feature for LLD.\n");
455 }
Neerav Parikhb2085a42011-06-20 16:59:51 -0700456
Robert Love848e7d52011-08-25 12:40:47 -0700457 rtnl_unlock();
458
Neerav Parikhb2085a42011-06-20 16:59:51 -0700459 /* Release the self-reference taken during fcoe_interface_create() */
Robert Love1a8ef412012-02-10 17:18:46 -0800460 /* tear-down the FCoE controller */
461 fcoe_ctlr_destroy(fip);
462 kfree(fcoe);
463 dev_put(netdev);
464 module_put(THIS_MODULE);
Vasu Devf04ca1b2011-04-01 16:06:45 -0700465}
466
467/**
Robert Love1875f272009-11-03 11:47:50 -0800468 * fcoe_fip_recv() - Handler for received FIP frames
469 * @skb: The receive skb
470 * @netdev: The associated net device
471 * @ptype: The packet_type structure which was used to register this handler
472 * @orig_dev: The original net_device the the skb was received on.
473 * (in case dev is a bond)
Vasu Devab6b85c2009-05-17 12:33:08 +0000474 *
475 * Returns: 0 for success
476 */
Robert Love1875f272009-11-03 11:47:50 -0800477static int fcoe_fip_recv(struct sk_buff *skb, struct net_device *netdev,
Vasu Devab6b85c2009-05-17 12:33:08 +0000478 struct packet_type *ptype,
479 struct net_device *orig_dev)
480{
Chris Leech259ad852009-08-25 13:59:41 -0700481 struct fcoe_interface *fcoe;
Vasu Devab6b85c2009-05-17 12:33:08 +0000482
Chris Leech259ad852009-08-25 13:59:41 -0700483 fcoe = container_of(ptype, struct fcoe_interface, fip_packet_type);
Chris Leech3fe9a0b2009-08-25 13:59:46 -0700484 fcoe_ctlr_recv(&fcoe->ctlr, skb);
Vasu Devab6b85c2009-05-17 12:33:08 +0000485 return 0;
486}
487
488/**
Vasu Dev980f5152011-07-27 15:11:05 -0700489 * fcoe_port_send() - Send an Ethernet-encapsulated FIP/FCoE frame
490 * @port: The FCoE port
491 * @skb: The FIP/FCoE packet to be sent
492 */
493static void fcoe_port_send(struct fcoe_port *port, struct sk_buff *skb)
494{
495 if (port->fcoe_pending_queue.qlen)
496 fcoe_check_wait_queue(port->lport, skb);
497 else if (fcoe_start_io(skb))
498 fcoe_check_wait_queue(port->lport, skb);
499}
500
501/**
Robert Love1875f272009-11-03 11:47:50 -0800502 * fcoe_fip_send() - Send an Ethernet-encapsulated FIP frame
503 * @fip: The FCoE controller
504 * @skb: The FIP packet to be sent
Vasu Devab6b85c2009-05-17 12:33:08 +0000505 */
506static void fcoe_fip_send(struct fcoe_ctlr *fip, struct sk_buff *skb)
507{
Chris Leech3fe9a0b2009-08-25 13:59:46 -0700508 skb->dev = fcoe_from_ctlr(fip)->netdev;
Vasu Dev980f5152011-07-27 15:11:05 -0700509 fcoe_port_send(lport_priv(fip->lp), skb);
Vasu Devab6b85c2009-05-17 12:33:08 +0000510}
511
512/**
Robert Love1875f272009-11-03 11:47:50 -0800513 * fcoe_update_src_mac() - Update the Ethernet MAC filters
514 * @lport: The local port to update the source MAC on
515 * @addr: Unicast MAC address to add
Vasu Devab6b85c2009-05-17 12:33:08 +0000516 *
517 * Remove any previously-set unicast MAC filter.
518 * Add secondary FCoE MAC address filter for our OUI.
519 */
Chris Leech11b56182009-11-03 11:46:29 -0800520static void fcoe_update_src_mac(struct fc_lport *lport, u8 *addr)
Vasu Devab6b85c2009-05-17 12:33:08 +0000521{
Chris Leech11b56182009-11-03 11:46:29 -0800522 struct fcoe_port *port = lport_priv(lport);
Bhanu Prakash Gollapudi8597ae82011-01-28 16:05:37 -0800523 struct fcoe_interface *fcoe = port->priv;
Vasu Devab6b85c2009-05-17 12:33:08 +0000524
Vasu Devab6b85c2009-05-17 12:33:08 +0000525 rtnl_lock();
Chris Leech11b56182009-11-03 11:46:29 -0800526 if (!is_zero_ether_addr(port->data_src_addr))
Jiri Pirkoa748ee22010-04-01 21:22:09 +0000527 dev_uc_del(fcoe->netdev, port->data_src_addr);
Chris Leech11b56182009-11-03 11:46:29 -0800528 if (!is_zero_ether_addr(addr))
Jiri Pirkoa748ee22010-04-01 21:22:09 +0000529 dev_uc_add(fcoe->netdev, addr);
Chris Leech11b56182009-11-03 11:46:29 -0800530 memcpy(port->data_src_addr, addr, ETH_ALEN);
Vasu Devab6b85c2009-05-17 12:33:08 +0000531 rtnl_unlock();
532}
533
534/**
Chris Leech11b56182009-11-03 11:46:29 -0800535 * fcoe_get_src_mac() - return the Ethernet source address for an lport
536 * @lport: libfc lport
537 */
538static u8 *fcoe_get_src_mac(struct fc_lport *lport)
539{
540 struct fcoe_port *port = lport_priv(lport);
541
542 return port->data_src_addr;
543}
544
545/**
Robert Love1875f272009-11-03 11:47:50 -0800546 * fcoe_lport_config() - Set up a local port
547 * @lport: The local port to be setup
Vasu Dev7f349142009-03-27 09:06:31 -0700548 *
549 * Returns: 0 for success
550 */
Robert Love1875f272009-11-03 11:47:50 -0800551static int fcoe_lport_config(struct fc_lport *lport)
Vasu Dev7f349142009-03-27 09:06:31 -0700552{
Robert Love1875f272009-11-03 11:47:50 -0800553 lport->link_up = 0;
554 lport->qfull = 0;
555 lport->max_retry_count = 3;
556 lport->max_rport_retry_count = 3;
557 lport->e_d_tov = 2 * 1000; /* FC-FS default */
558 lport->r_a_tov = 2 * 2 * 1000;
559 lport->service_params = (FCP_SPPF_INIT_FCN | FCP_SPPF_RD_XRDY_DIS |
560 FCP_SPPF_RETRY | FCP_SPPF_CONF_COMPL);
561 lport->does_npiv = 1;
Vasu Dev7f349142009-03-27 09:06:31 -0700562
Robert Love1875f272009-11-03 11:47:50 -0800563 fc_lport_init_stats(lport);
Vasu Dev7f349142009-03-27 09:06:31 -0700564
565 /* lport fc_lport related configuration */
Robert Love1875f272009-11-03 11:47:50 -0800566 fc_lport_config(lport);
Vasu Dev7f349142009-03-27 09:06:31 -0700567
568 /* offload related configuration */
Robert Love1875f272009-11-03 11:47:50 -0800569 lport->crc_offload = 0;
570 lport->seq_offload = 0;
571 lport->lro_enabled = 0;
572 lport->lro_xid = 0;
573 lport->lso_max = 0;
Vasu Dev7f349142009-03-27 09:06:31 -0700574
575 return 0;
576}
577
578/**
Yi Zou54a5b212010-07-20 15:21:17 -0700579 * fcoe_netdev_features_change - Updates the lport's offload flags based
580 * on the LLD netdev's FCoE feature flags
581 */
582static void fcoe_netdev_features_change(struct fc_lport *lport,
583 struct net_device *netdev)
584{
585 mutex_lock(&lport->lp_mutex);
586
587 if (netdev->features & NETIF_F_SG)
588 lport->sg_supp = 1;
589 else
590 lport->sg_supp = 0;
591
592 if (netdev->features & NETIF_F_FCOE_CRC) {
593 lport->crc_offload = 1;
594 FCOE_NETDEV_DBG(netdev, "Supports FCCRC offload\n");
595 } else {
596 lport->crc_offload = 0;
597 }
598
599 if (netdev->features & NETIF_F_FSO) {
600 lport->seq_offload = 1;
601 lport->lso_max = netdev->gso_max_size;
602 FCOE_NETDEV_DBG(netdev, "Supports LSO for max len 0x%x\n",
603 lport->lso_max);
604 } else {
605 lport->seq_offload = 0;
606 lport->lso_max = 0;
607 }
608
609 if (netdev->fcoe_ddp_xid) {
610 lport->lro_enabled = 1;
611 lport->lro_xid = netdev->fcoe_ddp_xid;
612 FCOE_NETDEV_DBG(netdev, "Supports LRO for max xid 0x%x\n",
613 lport->lro_xid);
614 } else {
615 lport->lro_enabled = 0;
616 lport->lro_xid = 0;
617 }
618
619 mutex_unlock(&lport->lp_mutex);
620}
621
622/**
Robert Love1875f272009-11-03 11:47:50 -0800623 * fcoe_netdev_config() - Set up net devive for SW FCoE
624 * @lport: The local port that is associated with the net device
625 * @netdev: The associated net device
Vasu Dev7f349142009-03-27 09:06:31 -0700626 *
Robert Love1875f272009-11-03 11:47:50 -0800627 * Must be called after fcoe_lport_config() as it will use local port mutex
Vasu Dev7f349142009-03-27 09:06:31 -0700628 *
Robert Love1875f272009-11-03 11:47:50 -0800629 * Returns: 0 for success
Vasu Dev7f349142009-03-27 09:06:31 -0700630 */
Robert Love1875f272009-11-03 11:47:50 -0800631static int fcoe_netdev_config(struct fc_lport *lport, struct net_device *netdev)
Vasu Dev7f349142009-03-27 09:06:31 -0700632{
633 u32 mfs;
634 u64 wwnn, wwpn;
Chris Leech25024982009-08-25 13:59:35 -0700635 struct fcoe_interface *fcoe;
Chris Leech014f5c32009-08-25 13:59:30 -0700636 struct fcoe_port *port;
Vasu Dev7f349142009-03-27 09:06:31 -0700637
638 /* Setup lport private data to point to fcoe softc */
Robert Love1875f272009-11-03 11:47:50 -0800639 port = lport_priv(lport);
Bhanu Prakash Gollapudi8597ae82011-01-28 16:05:37 -0800640 fcoe = port->priv;
Vasu Dev7f349142009-03-27 09:06:31 -0700641
642 /*
643 * Determine max frame size based on underlying device and optional
644 * user-configured limit. If the MFS is too low, fcoe_link_ok()
645 * will return 0, so do this first.
646 */
Yi Zou7221d7e2009-10-21 16:27:52 -0700647 mfs = netdev->mtu;
648 if (netdev->features & NETIF_F_FCOE_MTU) {
649 mfs = FCOE_MTU;
650 FCOE_NETDEV_DBG(netdev, "Supports FCOE_MTU of %d bytes\n", mfs);
651 }
652 mfs -= (sizeof(struct fcoe_hdr) + sizeof(struct fcoe_crc_eof));
Robert Love1875f272009-11-03 11:47:50 -0800653 if (fc_set_mfs(lport, mfs))
Vasu Dev7f349142009-03-27 09:06:31 -0700654 return -EINVAL;
655
Vasu Dev7f349142009-03-27 09:06:31 -0700656 /* offload features support */
Yi Zou54a5b212010-07-20 15:21:17 -0700657 fcoe_netdev_features_change(lport, netdev);
Vasu Dev7f349142009-03-27 09:06:31 -0700658
Chris Leech014f5c32009-08-25 13:59:30 -0700659 skb_queue_head_init(&port->fcoe_pending_queue);
660 port->fcoe_pending_queue_active = 0;
Robert Love1875f272009-11-03 11:47:50 -0800661 setup_timer(&port->timer, fcoe_queue_timer, (unsigned long)lport);
Vasu Dev7f349142009-03-27 09:06:31 -0700662
Robert Love5e4f8fe2010-05-07 15:18:35 -0700663 fcoe_link_speed_update(lport);
664
Robert Love1875f272009-11-03 11:47:50 -0800665 if (!lport->vport) {
Yi Zoudcece412009-11-20 15:22:21 -0800666 if (fcoe_get_wwn(netdev, &wwnn, NETDEV_FCOE_WWNN))
667 wwnn = fcoe_wwn_from_mac(fcoe->ctlr.ctl_src_addr, 1, 0);
Robert Love1875f272009-11-03 11:47:50 -0800668 fc_set_wwnn(lport, wwnn);
Yi Zoudcece412009-11-20 15:22:21 -0800669 if (fcoe_get_wwn(netdev, &wwpn, NETDEV_FCOE_WWPN))
670 wwpn = fcoe_wwn_from_mac(fcoe->ctlr.ctl_src_addr,
Vasu Devcf4aebca2010-07-20 15:21:22 -0700671 2, 0);
Robert Love1875f272009-11-03 11:47:50 -0800672 fc_set_wwpn(lport, wwpn);
Chris Leech9a057532009-11-03 11:46:40 -0800673 }
Vasu Dev7f349142009-03-27 09:06:31 -0700674
Vasu Dev7f349142009-03-27 09:06:31 -0700675 return 0;
676}
677
678/**
Robert Love1875f272009-11-03 11:47:50 -0800679 * fcoe_shost_config() - Set up the SCSI host associated with a local port
680 * @lport: The local port
Robert Love1875f272009-11-03 11:47:50 -0800681 * @dev: The device associated with the SCSI host
Vasu Dev7f349142009-03-27 09:06:31 -0700682 *
683 * Must be called after fcoe_lport_config() and fcoe_netdev_config()
684 *
Robert Love1875f272009-11-03 11:47:50 -0800685 * Returns: 0 for success
Vasu Dev7f349142009-03-27 09:06:31 -0700686 */
Vasu Dev8ba00a42010-04-09 14:22:54 -0700687static int fcoe_shost_config(struct fc_lport *lport, struct device *dev)
Vasu Dev7f349142009-03-27 09:06:31 -0700688{
689 int rc = 0;
690
691 /* lport scsi host config */
Robert Love1875f272009-11-03 11:47:50 -0800692 lport->host->max_lun = FCOE_MAX_LUN;
693 lport->host->max_id = FCOE_MAX_FCP_TARGET;
694 lport->host->max_channel = 0;
Vasu Devda87bfa2010-04-09 14:22:59 -0700695 lport->host->max_cmd_len = FCOE_MAX_CMD_LEN;
696
Robert Love1875f272009-11-03 11:47:50 -0800697 if (lport->vport)
Yi Zou8ca86f82011-01-28 16:05:11 -0800698 lport->host->transportt = fcoe_vport_scsi_transport;
Chris Leeche9084bb2009-11-03 11:46:34 -0800699 else
Yi Zou8ca86f82011-01-28 16:05:11 -0800700 lport->host->transportt = fcoe_nport_scsi_transport;
Vasu Dev7f349142009-03-27 09:06:31 -0700701
702 /* add the new host to the SCSI-ml */
Robert Love1875f272009-11-03 11:47:50 -0800703 rc = scsi_add_host(lport->host, dev);
Vasu Dev7f349142009-03-27 09:06:31 -0700704 if (rc) {
Robert Love1875f272009-11-03 11:47:50 -0800705 FCOE_NETDEV_DBG(fcoe_netdev(lport), "fcoe_shost_config: "
Robert Loved5488eb2009-06-10 15:30:59 -0700706 "error on scsi_add_host\n");
Vasu Dev7f349142009-03-27 09:06:31 -0700707 return rc;
708 }
Chris Leech9a057532009-11-03 11:46:40 -0800709
Robert Love1875f272009-11-03 11:47:50 -0800710 if (!lport->vport)
Alexey Dobriyan4be929b2010-05-24 14:33:03 -0700711 fc_host_max_npiv_vports(lport->host) = USHRT_MAX;
Chris Leech9a057532009-11-03 11:46:40 -0800712
Robert Love1875f272009-11-03 11:47:50 -0800713 snprintf(fc_host_symbolic_name(lport->host), FC_SYMBOLIC_NAME_SIZE,
Chris Leech5baa17c2009-11-03 11:46:56 -0800714 "%s v%s over %s", FCOE_NAME, FCOE_VERSION,
Robert Love1875f272009-11-03 11:47:50 -0800715 fcoe_netdev(lport)->name);
Vasu Dev7f349142009-03-27 09:06:31 -0700716
717 return 0;
718}
719
Neerav Parikh6fef3902012-01-22 17:30:10 -0800720
721/**
722 * fcoe_fdmi_info() - Get FDMI related info from net devive for SW FCoE
723 * @lport: The local port that is associated with the net device
724 * @netdev: The associated net device
725 *
726 * Must be called after fcoe_shost_config() as it will use local port mutex
727 *
728 */
729static void fcoe_fdmi_info(struct fc_lport *lport, struct net_device *netdev)
730{
731 struct fcoe_interface *fcoe;
732 struct fcoe_port *port;
733 struct net_device *realdev;
734 int rc;
735 struct netdev_fcoe_hbainfo fdmi;
736
737 port = lport_priv(lport);
738 fcoe = port->priv;
739 realdev = fcoe->realdev;
740
741 if (!realdev)
742 return;
743
744 /* No FDMI state m/c for NPIV ports */
745 if (lport->vport)
746 return;
747
748 if (realdev->netdev_ops->ndo_fcoe_get_hbainfo) {
749 memset(&fdmi, 0, sizeof(fdmi));
750 rc = realdev->netdev_ops->ndo_fcoe_get_hbainfo(realdev,
751 &fdmi);
752 if (rc) {
753 printk(KERN_INFO "fcoe: Failed to retrieve FDMI "
754 "information from netdev.\n");
755 return;
756 }
757
758 snprintf(fc_host_serial_number(lport->host),
759 FC_SERIAL_NUMBER_SIZE,
760 "%s",
761 fdmi.serial_number);
762 snprintf(fc_host_manufacturer(lport->host),
763 FC_SERIAL_NUMBER_SIZE,
764 "%s",
765 fdmi.manufacturer);
766 snprintf(fc_host_model(lport->host),
767 FC_SYMBOLIC_NAME_SIZE,
768 "%s",
769 fdmi.model);
770 snprintf(fc_host_model_description(lport->host),
771 FC_SYMBOLIC_NAME_SIZE,
772 "%s",
773 fdmi.model_description);
774 snprintf(fc_host_hardware_version(lport->host),
775 FC_VERSION_STRING_SIZE,
776 "%s",
777 fdmi.hardware_version);
778 snprintf(fc_host_driver_version(lport->host),
779 FC_VERSION_STRING_SIZE,
780 "%s",
781 fdmi.driver_version);
782 snprintf(fc_host_optionrom_version(lport->host),
783 FC_VERSION_STRING_SIZE,
784 "%s",
785 fdmi.optionrom_version);
786 snprintf(fc_host_firmware_version(lport->host),
787 FC_VERSION_STRING_SIZE,
788 "%s",
789 fdmi.firmware_version);
790
791 /* Enable FDMI lport states */
792 lport->fdmi_enabled = 1;
793 } else {
794 lport->fdmi_enabled = 0;
795 printk(KERN_INFO "fcoe: No FDMI support.\n");
796 }
797}
798
Robert Love1875f272009-11-03 11:47:50 -0800799/**
800 * fcoe_oem_match() - The match routine for the offloaded exchange manager
801 * @fp: The I/O frame
Vasu Devd7179682009-07-29 17:05:21 -0700802 *
Robert Love1875f272009-11-03 11:47:50 -0800803 * This routine will be associated with an exchange manager (EM). When
804 * the libfc exchange handling code is looking for an EM to use it will
805 * call this routine and pass it the frame that it wishes to send. This
806 * routine will return True if the associated EM is to be used and False
807 * if the echange code should continue looking for an EM.
808 *
809 * The offload EM that this routine is associated with will handle any
810 * packets that are for SCSI read requests.
811 *
Kiran Patil1ff99182011-06-20 16:59:15 -0700812 * This has been enhanced to work when FCoE stack is operating in target
813 * mode.
814 *
Robert Love1875f272009-11-03 11:47:50 -0800815 * Returns: True for read types I/O, otherwise returns false.
Vasu Devd7179682009-07-29 17:05:21 -0700816 */
Bart Van Assche7c9c6842012-01-13 17:26:25 -0800817static bool fcoe_oem_match(struct fc_frame *fp)
Vasu Devd7179682009-07-29 17:05:21 -0700818{
Kiran Patil1ff99182011-06-20 16:59:15 -0700819 struct fc_frame_header *fh = fc_frame_header_get(fp);
820 struct fcp_cmnd *fcp;
821
822 if (fc_fcp_is_read(fr_fsp(fp)) &&
823 (fr_fsp(fp)->data_len > fcoe_ddp_min))
824 return true;
Yi Zoua762dce2012-01-13 17:26:15 -0800825 else if ((fr_fsp(fp) == NULL) &&
826 (fh->fh_r_ctl == FC_RCTL_DD_UNSOL_CMD) &&
827 (ntohs(fh->fh_rx_id) == FC_XID_UNKNOWN)) {
Kiran Patil1ff99182011-06-20 16:59:15 -0700828 fcp = fc_frame_payload_get(fp, sizeof(*fcp));
Yi Zoua762dce2012-01-13 17:26:15 -0800829 if ((fcp->fc_flags & FCP_CFL_WRDATA) &&
830 (ntohl(fcp->fc_dl) > fcoe_ddp_min))
Kiran Patil1ff99182011-06-20 16:59:15 -0700831 return true;
832 }
833 return false;
Vasu Devd7179682009-07-29 17:05:21 -0700834}
835
Vasu Dev7f349142009-03-27 09:06:31 -0700836/**
Robert Love1875f272009-11-03 11:47:50 -0800837 * fcoe_em_config() - Allocate and configure an exchange manager
838 * @lport: The local port that the new EM will be associated with
Vasu Dev7f349142009-03-27 09:06:31 -0700839 *
Robert Love1875f272009-11-03 11:47:50 -0800840 * Returns: 0 on success
Vasu Dev7f349142009-03-27 09:06:31 -0700841 */
Robert Love1875f272009-11-03 11:47:50 -0800842static inline int fcoe_em_config(struct fc_lport *lport)
Vasu Dev7f349142009-03-27 09:06:31 -0700843{
Robert Love1875f272009-11-03 11:47:50 -0800844 struct fcoe_port *port = lport_priv(lport);
Bhanu Prakash Gollapudi8597ae82011-01-28 16:05:37 -0800845 struct fcoe_interface *fcoe = port->priv;
Chris Leech25024982009-08-25 13:59:35 -0700846 struct fcoe_interface *oldfcoe = NULL;
Vasu Dev1d1b88d2009-07-29 17:05:45 -0700847 struct net_device *old_real_dev, *cur_real_dev;
Vasu Devd7179682009-07-29 17:05:21 -0700848 u16 min_xid = FCOE_MIN_XID;
849 u16 max_xid = FCOE_MAX_XID;
850
851 /*
852 * Check if need to allocate an em instance for
853 * offload exchange ids to be shared across all VN_PORTs/lport.
854 */
Robert Love1875f272009-11-03 11:47:50 -0800855 if (!lport->lro_enabled || !lport->lro_xid ||
856 (lport->lro_xid >= max_xid)) {
857 lport->lro_xid = 0;
Vasu Devd7179682009-07-29 17:05:21 -0700858 goto skip_oem;
859 }
860
861 /*
862 * Reuse existing offload em instance in case
Vasu Dev1d1b88d2009-07-29 17:05:45 -0700863 * it is already allocated on real eth device
Vasu Devd7179682009-07-29 17:05:21 -0700864 */
Chris Leech25024982009-08-25 13:59:35 -0700865 if (fcoe->netdev->priv_flags & IFF_802_1Q_VLAN)
866 cur_real_dev = vlan_dev_real_dev(fcoe->netdev);
Vasu Dev1d1b88d2009-07-29 17:05:45 -0700867 else
Chris Leech25024982009-08-25 13:59:35 -0700868 cur_real_dev = fcoe->netdev;
Vasu Dev1d1b88d2009-07-29 17:05:45 -0700869
Chris Leech25024982009-08-25 13:59:35 -0700870 list_for_each_entry(oldfcoe, &fcoe_hostlist, list) {
Chris Leech25024982009-08-25 13:59:35 -0700871 if (oldfcoe->netdev->priv_flags & IFF_802_1Q_VLAN)
872 old_real_dev = vlan_dev_real_dev(oldfcoe->netdev);
Vasu Dev1d1b88d2009-07-29 17:05:45 -0700873 else
Chris Leech25024982009-08-25 13:59:35 -0700874 old_real_dev = oldfcoe->netdev;
Vasu Dev1d1b88d2009-07-29 17:05:45 -0700875
876 if (cur_real_dev == old_real_dev) {
Chris Leech991cbb62009-08-25 13:59:51 -0700877 fcoe->oem = oldfcoe->oem;
Vasu Devd7179682009-07-29 17:05:21 -0700878 break;
879 }
880 }
881
Chris Leech991cbb62009-08-25 13:59:51 -0700882 if (fcoe->oem) {
Robert Love1875f272009-11-03 11:47:50 -0800883 if (!fc_exch_mgr_add(lport, fcoe->oem, fcoe_oem_match)) {
Vasu Devd7179682009-07-29 17:05:21 -0700884 printk(KERN_ERR "fcoe_em_config: failed to add "
885 "offload em:%p on interface:%s\n",
Chris Leech991cbb62009-08-25 13:59:51 -0700886 fcoe->oem, fcoe->netdev->name);
Vasu Devd7179682009-07-29 17:05:21 -0700887 return -ENOMEM;
888 }
889 } else {
Robert Love1875f272009-11-03 11:47:50 -0800890 fcoe->oem = fc_exch_mgr_alloc(lport, FC_CLASS_3,
891 FCOE_MIN_XID, lport->lro_xid,
892 fcoe_oem_match);
Chris Leech991cbb62009-08-25 13:59:51 -0700893 if (!fcoe->oem) {
Vasu Devd7179682009-07-29 17:05:21 -0700894 printk(KERN_ERR "fcoe_em_config: failed to allocate "
895 "em for offload exches on interface:%s\n",
Chris Leech25024982009-08-25 13:59:35 -0700896 fcoe->netdev->name);
Vasu Devd7179682009-07-29 17:05:21 -0700897 return -ENOMEM;
898 }
899 }
900
901 /*
902 * Exclude offload EM xid range from next EM xid range.
903 */
Robert Love1875f272009-11-03 11:47:50 -0800904 min_xid += lport->lro_xid + 1;
Vasu Devd7179682009-07-29 17:05:21 -0700905
906skip_oem:
Robert Love1875f272009-11-03 11:47:50 -0800907 if (!fc_exch_mgr_alloc(lport, FC_CLASS_3, min_xid, max_xid, NULL)) {
Vasu Devd7179682009-07-29 17:05:21 -0700908 printk(KERN_ERR "fcoe_em_config: failed to "
Chris Leech25024982009-08-25 13:59:35 -0700909 "allocate em on interface %s\n", fcoe->netdev->name);
Vasu Dev7f349142009-03-27 09:06:31 -0700910 return -ENOMEM;
Vasu Devd7179682009-07-29 17:05:21 -0700911 }
Vasu Dev7f349142009-03-27 09:06:31 -0700912
913 return 0;
914}
915
916/**
Robert Love1875f272009-11-03 11:47:50 -0800917 * fcoe_if_destroy() - Tear down a SW FCoE instance
918 * @lport: The local port to be destroyed
Vasu Dev34ce27b2010-05-07 15:18:46 -0700919 *
Vasu Dev7f349142009-03-27 09:06:31 -0700920 */
Chris Leechaf7f85d2009-08-25 13:59:24 -0700921static void fcoe_if_destroy(struct fc_lport *lport)
Vasu Dev7f349142009-03-27 09:06:31 -0700922{
Neerav Parikhb2085a42011-06-20 16:59:51 -0700923 struct fcoe_port *port = lport_priv(lport);
924 struct fcoe_interface *fcoe = port->priv;
925 struct net_device *netdev = fcoe->netdev;
926
927 FCOE_NETDEV_DBG(netdev, "Destroying interface\n");
928
929 /* Logout of the fabric */
930 fc_fabric_logoff(lport);
931
932 /* Cleanup the fc_lport */
933 fc_lport_destroy(lport);
934
935 /* Stop the transmit retry timer */
936 del_timer_sync(&port->timer);
937
938 /* Free existing transmit skbs */
939 fcoe_clean_pending_queue(lport);
940
941 rtnl_lock();
942 if (!is_zero_ether_addr(port->data_src_addr))
943 dev_uc_del(netdev, port->data_src_addr);
944 rtnl_unlock();
945
Chris Leech54b649f2009-08-25 14:00:07 -0700946 /* Free queued packets for the per-CPU receive threads */
947 fcoe_percpu_clean(lport);
948
Vasu Dev7f349142009-03-27 09:06:31 -0700949 /* Detach from the scsi-ml */
Chris Leechaf7f85d2009-08-25 13:59:24 -0700950 fc_remove_host(lport->host);
951 scsi_remove_host(lport->host);
Vasu Dev7f349142009-03-27 09:06:31 -0700952
Yi Zou80e736f2010-11-30 16:18:07 -0800953 /* Destroy lport scsi_priv */
954 fc_fcp_destroy(lport);
955
Vasu Dev7f349142009-03-27 09:06:31 -0700956 /* There are no more rports or I/O, free the EM */
Chris Leechaf7f85d2009-08-25 13:59:24 -0700957 fc_exch_mgr_free(lport);
Vasu Dev7f349142009-03-27 09:06:31 -0700958
Vasu Dev7f349142009-03-27 09:06:31 -0700959 /* Free memory used by statistical counters */
Chris Leechaf7f85d2009-08-25 13:59:24 -0700960 fc_lport_free_stats(lport);
Vasu Dev7f349142009-03-27 09:06:31 -0700961
Chris Leech2e70e242009-08-25 14:00:23 -0700962 /* Release the Scsi_Host */
Chris Leechaf7f85d2009-08-25 13:59:24 -0700963 scsi_host_put(lport->host);
Vasu Dev7f349142009-03-27 09:06:31 -0700964}
965
Robert Love1875f272009-11-03 11:47:50 -0800966/**
967 * fcoe_ddp_setup() - Call a LLD's ddp_setup through the net device
968 * @lport: The local port to setup DDP for
969 * @xid: The exchange ID for this DDP transfer
970 * @sgl: The scatterlist describing this transfer
971 * @sgc: The number of sg items
Vasu Dev7f349142009-03-27 09:06:31 -0700972 *
Robert Love1875f272009-11-03 11:47:50 -0800973 * Returns: 0 if the DDP context was not configured
Vasu Dev7f349142009-03-27 09:06:31 -0700974 */
Robert Love1875f272009-11-03 11:47:50 -0800975static int fcoe_ddp_setup(struct fc_lport *lport, u16 xid,
976 struct scatterlist *sgl, unsigned int sgc)
Vasu Dev7f349142009-03-27 09:06:31 -0700977{
Robert Love1875f272009-11-03 11:47:50 -0800978 struct net_device *netdev = fcoe_netdev(lport);
Vasu Dev7f349142009-03-27 09:06:31 -0700979
Robert Love1875f272009-11-03 11:47:50 -0800980 if (netdev->netdev_ops->ndo_fcoe_ddp_setup)
981 return netdev->netdev_ops->ndo_fcoe_ddp_setup(netdev,
982 xid, sgl,
983 sgc);
Vasu Dev7f349142009-03-27 09:06:31 -0700984
985 return 0;
986}
987
Vasu Dev7f349142009-03-27 09:06:31 -0700988/**
Yi Zou71f89492011-06-20 16:59:10 -0700989 * fcoe_ddp_target() - Call a LLD's ddp_target 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
994 *
995 * Returns: 0 if the DDP context was not configured
996 */
997static int fcoe_ddp_target(struct fc_lport *lport, u16 xid,
998 struct scatterlist *sgl, unsigned int sgc)
999{
1000 struct net_device *netdev = fcoe_netdev(lport);
1001
1002 if (netdev->netdev_ops->ndo_fcoe_ddp_target)
1003 return netdev->netdev_ops->ndo_fcoe_ddp_target(netdev, xid,
1004 sgl, sgc);
1005
1006 return 0;
1007}
1008
1009
1010/**
Robert Love1875f272009-11-03 11:47:50 -08001011 * fcoe_ddp_done() - Call a LLD's ddp_done through the net device
1012 * @lport: The local port to complete DDP on
1013 * @xid: The exchange ID for this DDP transfer
Vasu Dev7f349142009-03-27 09:06:31 -07001014 *
Robert Love1875f272009-11-03 11:47:50 -08001015 * Returns: the length of data that have been completed by DDP
1016 */
1017static int fcoe_ddp_done(struct fc_lport *lport, u16 xid)
1018{
1019 struct net_device *netdev = fcoe_netdev(lport);
1020
1021 if (netdev->netdev_ops->ndo_fcoe_ddp_done)
1022 return netdev->netdev_ops->ndo_fcoe_ddp_done(netdev, xid);
1023 return 0;
1024}
1025
1026/**
1027 * fcoe_if_create() - Create a FCoE instance on an interface
1028 * @fcoe: The FCoE interface to create a local port on
1029 * @parent: The device pointer to be the parent in sysfs for the SCSI host
1030 * @npiv: Indicates if the port is a vport or not
Vasu Dev7f349142009-03-27 09:06:31 -07001031 *
Robert Love1875f272009-11-03 11:47:50 -08001032 * Creates a fc_lport instance and a Scsi_Host instance and configure them.
1033 *
1034 * Returns: The allocated fc_lport or an error pointer
Vasu Dev7f349142009-03-27 09:06:31 -07001035 */
Chris Leech030f4e02009-08-25 14:00:02 -07001036static struct fc_lport *fcoe_if_create(struct fcoe_interface *fcoe,
Chris Leech9a057532009-11-03 11:46:40 -08001037 struct device *parent, int npiv)
Vasu Dev7f349142009-03-27 09:06:31 -07001038{
Robert Love1875f272009-11-03 11:47:50 -08001039 struct net_device *netdev = fcoe->netdev;
Vasu Dev72fa3962011-02-25 15:03:01 -08001040 struct fc_lport *lport, *n_port;
Chris Leech014f5c32009-08-25 13:59:30 -07001041 struct fcoe_port *port;
Vasu Dev72fa3962011-02-25 15:03:01 -08001042 struct Scsi_Host *shost;
Robert Love1875f272009-11-03 11:47:50 -08001043 int rc;
Chris Leech9a057532009-11-03 11:46:40 -08001044 /*
1045 * parent is only a vport if npiv is 1,
1046 * but we'll only use vport in that case so go ahead and set it
1047 */
1048 struct fc_vport *vport = dev_to_vport(parent);
Vasu Dev7f349142009-03-27 09:06:31 -07001049
Robert Loved5488eb2009-06-10 15:30:59 -07001050 FCOE_NETDEV_DBG(netdev, "Create Interface\n");
Vasu Dev7f349142009-03-27 09:06:31 -07001051
Vasu Dev72fa3962011-02-25 15:03:01 -08001052 if (!npiv)
1053 lport = libfc_host_alloc(&fcoe_shost_template, sizeof(*port));
1054 else
1055 lport = libfc_vport_create(vport, sizeof(*port));
1056
Chris Leech86221962009-11-03 11:46:08 -08001057 if (!lport) {
Chris Leech014f5c32009-08-25 13:59:30 -07001058 FCOE_NETDEV_DBG(netdev, "Could not allocate host structure\n");
1059 rc = -ENOMEM;
Chris Leech030f4e02009-08-25 14:00:02 -07001060 goto out;
Chris Leech014f5c32009-08-25 13:59:30 -07001061 }
Chris Leech014f5c32009-08-25 13:59:30 -07001062 port = lport_priv(lport);
Chris Leech2e70e242009-08-25 14:00:23 -07001063 port->lport = lport;
Bhanu Prakash Gollapudi8597ae82011-01-28 16:05:37 -08001064 port->priv = fcoe;
1065 port->max_queue_depth = FCOE_MAX_QUEUE_DEPTH;
1066 port->min_queue_depth = FCOE_MIN_QUEUE_DEPTH;
Chris Leech2e70e242009-08-25 14:00:23 -07001067 INIT_WORK(&port->destroy_work, fcoe_destroy_work);
Vasu Dev7f349142009-03-27 09:06:31 -07001068
Robert Love1875f272009-11-03 11:47:50 -08001069 /* configure a fc_lport including the exchange manager */
Chris Leechaf7f85d2009-08-25 13:59:24 -07001070 rc = fcoe_lport_config(lport);
Vasu Dev7f349142009-03-27 09:06:31 -07001071 if (rc) {
Robert Loved5488eb2009-06-10 15:30:59 -07001072 FCOE_NETDEV_DBG(netdev, "Could not configure lport for the "
1073 "interface\n");
Vasu Dev7f349142009-03-27 09:06:31 -07001074 goto out_host_put;
1075 }
1076
Chris Leech9a057532009-11-03 11:46:40 -08001077 if (npiv) {
Chris Leech9f8f3aa2010-04-09 14:23:16 -07001078 FCOE_NETDEV_DBG(netdev, "Setting vport names, "
1079 "%16.16llx %16.16llx\n",
Robert Love1875f272009-11-03 11:47:50 -08001080 vport->node_name, vport->port_name);
Chris Leech9a057532009-11-03 11:46:40 -08001081 fc_set_wwnn(lport, vport->node_name);
1082 fc_set_wwpn(lport, vport->port_name);
1083 }
1084
Vasu Devab6b85c2009-05-17 12:33:08 +00001085 /* configure lport network properties */
Chris Leechaf7f85d2009-08-25 13:59:24 -07001086 rc = fcoe_netdev_config(lport, netdev);
Vasu Devab6b85c2009-05-17 12:33:08 +00001087 if (rc) {
Robert Loved5488eb2009-06-10 15:30:59 -07001088 FCOE_NETDEV_DBG(netdev, "Could not configure netdev for the "
1089 "interface\n");
Chris Leech54b649f2009-08-25 14:00:07 -07001090 goto out_lp_destroy;
Vasu Devab6b85c2009-05-17 12:33:08 +00001091 }
Joe Eykholt97c83892009-03-17 11:42:40 -07001092
Vasu Dev7f349142009-03-27 09:06:31 -07001093 /* configure lport scsi host properties */
Vasu Dev8ba00a42010-04-09 14:22:54 -07001094 rc = fcoe_shost_config(lport, parent);
Vasu Dev7f349142009-03-27 09:06:31 -07001095 if (rc) {
Robert Loved5488eb2009-06-10 15:30:59 -07001096 FCOE_NETDEV_DBG(netdev, "Could not configure shost for the "
1097 "interface\n");
Chris Leech54b649f2009-08-25 14:00:07 -07001098 goto out_lp_destroy;
Vasu Dev7f349142009-03-27 09:06:31 -07001099 }
1100
Vasu Dev7f349142009-03-27 09:06:31 -07001101 /* Initialize the library */
Joe Eykholte10f8c62010-07-20 15:20:30 -07001102 rc = fcoe_libfc_config(lport, &fcoe->ctlr, &fcoe_libfc_fcn_templ, 1);
Vasu Dev7f349142009-03-27 09:06:31 -07001103 if (rc) {
Robert Loved5488eb2009-06-10 15:30:59 -07001104 FCOE_NETDEV_DBG(netdev, "Could not configure libfc for the "
1105 "interface\n");
Vasu Dev7f349142009-03-27 09:06:31 -07001106 goto out_lp_destroy;
1107 }
1108
Neerav Parikh6fef3902012-01-22 17:30:10 -08001109 /* Initialized FDMI information */
1110 fcoe_fdmi_info(lport, netdev);
1111
Vasu Dev72fa3962011-02-25 15:03:01 -08001112 /*
1113 * fcoe_em_alloc() and fcoe_hostlist_add() both
1114 * need to be atomic with respect to other changes to the
1115 * hostlist since fcoe_em_alloc() looks for an existing EM
1116 * instance on host list updated by fcoe_hostlist_add().
1117 *
1118 * This is currently handled through the fcoe_config_mutex
1119 * begin held.
1120 */
1121 if (!npiv)
Chris Leech9a057532009-11-03 11:46:40 -08001122 /* lport exch manager allocation */
1123 rc = fcoe_em_config(lport);
Vasu Dev72fa3962011-02-25 15:03:01 -08001124 else {
1125 shost = vport_to_shost(vport);
1126 n_port = shost_priv(shost);
1127 rc = fc_exch_mgr_list_clone(n_port, lport);
1128 }
1129
1130 if (rc) {
1131 FCOE_NETDEV_DBG(netdev, "Could not configure the EM\n");
1132 goto out_lp_destroy;
Vasu Dev96316092009-07-29 17:05:00 -07001133 }
1134
Chris Leechaf7f85d2009-08-25 13:59:24 -07001135 return lport;
Vasu Dev7f349142009-03-27 09:06:31 -07001136
1137out_lp_destroy:
Chris Leechaf7f85d2009-08-25 13:59:24 -07001138 fc_exch_mgr_free(lport);
Vasu Dev7f349142009-03-27 09:06:31 -07001139out_host_put:
Chris Leechaf7f85d2009-08-25 13:59:24 -07001140 scsi_host_put(lport->host);
1141out:
1142 return ERR_PTR(rc);
Vasu Dev7f349142009-03-27 09:06:31 -07001143}
1144
1145/**
Robert Love1875f272009-11-03 11:47:50 -08001146 * fcoe_if_init() - Initialization routine for fcoe.ko
Vasu Dev7f349142009-03-27 09:06:31 -07001147 *
Robert Love1875f272009-11-03 11:47:50 -08001148 * Attaches the SW FCoE transport to the FC transport
1149 *
1150 * Returns: 0 on success
Vasu Dev7f349142009-03-27 09:06:31 -07001151 */
1152static int __init fcoe_if_init(void)
1153{
1154 /* attach to scsi transport */
Yi Zou8ca86f82011-01-28 16:05:11 -08001155 fcoe_nport_scsi_transport =
1156 fc_attach_transport(&fcoe_nport_fc_functions);
1157 fcoe_vport_scsi_transport =
1158 fc_attach_transport(&fcoe_vport_fc_functions);
Vasu Dev7f349142009-03-27 09:06:31 -07001159
Yi Zou8ca86f82011-01-28 16:05:11 -08001160 if (!fcoe_nport_scsi_transport) {
Robert Loved5488eb2009-06-10 15:30:59 -07001161 printk(KERN_ERR "fcoe: Failed to attach to the FC transport\n");
Vasu Dev7f349142009-03-27 09:06:31 -07001162 return -ENODEV;
1163 }
1164
1165 return 0;
1166}
1167
1168/**
Robert Love1875f272009-11-03 11:47:50 -08001169 * fcoe_if_exit() - Tear down fcoe.ko
Vasu Dev7f349142009-03-27 09:06:31 -07001170 *
Robert Love1875f272009-11-03 11:47:50 -08001171 * Detaches the SW FCoE transport from the FC transport
1172 *
1173 * Returns: 0 on success
Vasu Dev7f349142009-03-27 09:06:31 -07001174 */
Bart Van Assche7c9c6842012-01-13 17:26:25 -08001175static int __exit fcoe_if_exit(void)
Vasu Dev7f349142009-03-27 09:06:31 -07001176{
Yi Zou8ca86f82011-01-28 16:05:11 -08001177 fc_release_transport(fcoe_nport_scsi_transport);
1178 fc_release_transport(fcoe_vport_scsi_transport);
1179 fcoe_nport_scsi_transport = NULL;
1180 fcoe_vport_scsi_transport = NULL;
Vasu Dev7f349142009-03-27 09:06:31 -07001181 return 0;
1182}
1183
Robert Love85b4aa42008-12-09 15:10:24 -08001184/**
Robert Love1875f272009-11-03 11:47:50 -08001185 * fcoe_percpu_thread_create() - Create a receive thread for an online CPU
1186 * @cpu: The CPU index of the CPU to create a receive thread for
Robert Love8976f422009-03-17 11:41:46 -07001187 */
1188static void fcoe_percpu_thread_create(unsigned int cpu)
1189{
1190 struct fcoe_percpu_s *p;
1191 struct task_struct *thread;
1192
1193 p = &per_cpu(fcoe_percpu, cpu);
1194
Eric Dumazet5c609ff2011-09-27 21:37:52 -07001195 thread = kthread_create_on_node(fcoe_percpu_receive_thread,
1196 (void *)p, cpu_to_node(cpu),
1197 "fcoethread/%d", cpu);
Robert Love8976f422009-03-17 11:41:46 -07001198
Joe Eykholte7a51992009-08-25 14:04:08 -07001199 if (likely(!IS_ERR(thread))) {
Robert Love8976f422009-03-17 11:41:46 -07001200 kthread_bind(thread, cpu);
1201 wake_up_process(thread);
1202
1203 spin_lock_bh(&p->fcoe_rx_list.lock);
1204 p->thread = thread;
1205 spin_unlock_bh(&p->fcoe_rx_list.lock);
1206 }
1207}
1208
1209/**
Robert Love1875f272009-11-03 11:47:50 -08001210 * fcoe_percpu_thread_destroy() - Remove the receive thread of a CPU
1211 * @cpu: The CPU index of the CPU whose receive thread is to be destroyed
Robert Love8976f422009-03-17 11:41:46 -07001212 *
1213 * Destroys a per-CPU Rx thread. Any pending skbs are moved to the
1214 * current CPU's Rx thread. If the thread being destroyed is bound to
1215 * the CPU processing this context the skbs will be freed.
1216 */
1217static void fcoe_percpu_thread_destroy(unsigned int cpu)
1218{
1219 struct fcoe_percpu_s *p;
1220 struct task_struct *thread;
1221 struct page *crc_eof;
1222 struct sk_buff *skb;
1223#ifdef CONFIG_SMP
1224 struct fcoe_percpu_s *p0;
Joe Eykholtf018b732010-03-12 16:08:55 -08001225 unsigned targ_cpu = get_cpu();
Robert Love8976f422009-03-17 11:41:46 -07001226#endif /* CONFIG_SMP */
1227
Robert Loved5488eb2009-06-10 15:30:59 -07001228 FCOE_DBG("Destroying receive thread for CPU %d\n", cpu);
Robert Love8976f422009-03-17 11:41:46 -07001229
1230 /* Prevent any new skbs from being queued for this CPU. */
1231 p = &per_cpu(fcoe_percpu, cpu);
1232 spin_lock_bh(&p->fcoe_rx_list.lock);
1233 thread = p->thread;
1234 p->thread = NULL;
1235 crc_eof = p->crc_eof_page;
1236 p->crc_eof_page = NULL;
1237 p->crc_eof_offset = 0;
1238 spin_unlock_bh(&p->fcoe_rx_list.lock);
1239
1240#ifdef CONFIG_SMP
1241 /*
1242 * Don't bother moving the skb's if this context is running
1243 * on the same CPU that is having its thread destroyed. This
1244 * can easily happen when the module is removed.
1245 */
1246 if (cpu != targ_cpu) {
1247 p0 = &per_cpu(fcoe_percpu, targ_cpu);
1248 spin_lock_bh(&p0->fcoe_rx_list.lock);
1249 if (p0->thread) {
Robert Loved5488eb2009-06-10 15:30:59 -07001250 FCOE_DBG("Moving frames from CPU %d to CPU %d\n",
1251 cpu, targ_cpu);
Robert Love8976f422009-03-17 11:41:46 -07001252
1253 while ((skb = __skb_dequeue(&p->fcoe_rx_list)) != NULL)
1254 __skb_queue_tail(&p0->fcoe_rx_list, skb);
1255 spin_unlock_bh(&p0->fcoe_rx_list.lock);
1256 } else {
1257 /*
1258 * The targeted CPU is not initialized and cannot accept
Robert Love1875f272009-11-03 11:47:50 -08001259 * new skbs. Unlock the targeted CPU and drop the skbs
Robert Love8976f422009-03-17 11:41:46 -07001260 * on the CPU that is going offline.
1261 */
1262 while ((skb = __skb_dequeue(&p->fcoe_rx_list)) != NULL)
1263 kfree_skb(skb);
1264 spin_unlock_bh(&p0->fcoe_rx_list.lock);
1265 }
1266 } else {
1267 /*
1268 * This scenario occurs when the module is being removed
1269 * and all threads are being destroyed. skbs will continue
1270 * to be shifted from the CPU thread that is being removed
1271 * to the CPU thread associated with the CPU that is processing
1272 * the module removal. Once there is only one CPU Rx thread it
1273 * will reach this case and we will drop all skbs and later
1274 * stop the thread.
1275 */
1276 spin_lock_bh(&p->fcoe_rx_list.lock);
1277 while ((skb = __skb_dequeue(&p->fcoe_rx_list)) != NULL)
1278 kfree_skb(skb);
1279 spin_unlock_bh(&p->fcoe_rx_list.lock);
1280 }
Joe Eykholtf018b732010-03-12 16:08:55 -08001281 put_cpu();
Robert Love8976f422009-03-17 11:41:46 -07001282#else
1283 /*
Chris Leechdd3fd722009-04-21 16:27:36 -07001284 * This a non-SMP scenario where the singular Rx thread is
Robert Love8976f422009-03-17 11:41:46 -07001285 * being removed. Free all skbs and stop the thread.
1286 */
1287 spin_lock_bh(&p->fcoe_rx_list.lock);
1288 while ((skb = __skb_dequeue(&p->fcoe_rx_list)) != NULL)
1289 kfree_skb(skb);
1290 spin_unlock_bh(&p->fcoe_rx_list.lock);
1291#endif
1292
1293 if (thread)
1294 kthread_stop(thread);
1295
1296 if (crc_eof)
1297 put_page(crc_eof);
1298}
1299
1300/**
Robert Love1875f272009-11-03 11:47:50 -08001301 * fcoe_cpu_callback() - Handler for CPU hotplug events
1302 * @nfb: The callback data block
1303 * @action: The event triggering the callback
1304 * @hcpu: The index of the CPU that the event is for
Robert Love8976f422009-03-17 11:41:46 -07001305 *
Robert Love1875f272009-11-03 11:47:50 -08001306 * This creates or destroys per-CPU data for fcoe
Robert Love8976f422009-03-17 11:41:46 -07001307 *
1308 * Returns NOTIFY_OK always.
1309 */
1310static int fcoe_cpu_callback(struct notifier_block *nfb,
1311 unsigned long action, void *hcpu)
1312{
1313 unsigned cpu = (unsigned long)hcpu;
1314
1315 switch (action) {
1316 case CPU_ONLINE:
1317 case CPU_ONLINE_FROZEN:
Robert Loved5488eb2009-06-10 15:30:59 -07001318 FCOE_DBG("CPU %x online: Create Rx thread\n", cpu);
Robert Love8976f422009-03-17 11:41:46 -07001319 fcoe_percpu_thread_create(cpu);
1320 break;
1321 case CPU_DEAD:
1322 case CPU_DEAD_FROZEN:
Robert Loved5488eb2009-06-10 15:30:59 -07001323 FCOE_DBG("CPU %x offline: Remove Rx thread\n", cpu);
Robert Love8976f422009-03-17 11:41:46 -07001324 fcoe_percpu_thread_destroy(cpu);
1325 break;
1326 default:
1327 break;
1328 }
1329 return NOTIFY_OK;
1330}
1331
Robert Love8976f422009-03-17 11:41:46 -07001332/**
Kiran Patil064287e2011-06-20 16:59:20 -07001333 * fcoe_select_cpu() - Selects CPU to handle post-processing of incoming
1334 * command.
Kiran Patil064287e2011-06-20 16:59:20 -07001335 *
Vasu Devd2722812011-07-27 15:11:10 -07001336 * This routine selects next CPU based on cpumask to distribute
1337 * incoming requests in round robin.
Kiran Patil064287e2011-06-20 16:59:20 -07001338 *
Vasu Devd2722812011-07-27 15:11:10 -07001339 * Returns: int CPU number
Kiran Patil064287e2011-06-20 16:59:20 -07001340 */
Vasu Devd2722812011-07-27 15:11:10 -07001341static inline unsigned int fcoe_select_cpu(void)
Kiran Patil064287e2011-06-20 16:59:20 -07001342{
1343 static unsigned int selected_cpu;
1344
Vasu Devd2722812011-07-27 15:11:10 -07001345 selected_cpu = cpumask_next(selected_cpu, cpu_online_mask);
1346 if (selected_cpu >= nr_cpu_ids)
1347 selected_cpu = cpumask_first(cpu_online_mask);
1348
Kiran Patil064287e2011-06-20 16:59:20 -07001349 return selected_cpu;
1350}
1351
1352/**
Robert Love1875f272009-11-03 11:47:50 -08001353 * fcoe_rcv() - Receive packets from a net device
1354 * @skb: The received packet
1355 * @netdev: The net device that the packet was received on
1356 * @ptype: The packet type context
1357 * @olddev: The last device net device
Robert Love85b4aa42008-12-09 15:10:24 -08001358 *
Robert Love1875f272009-11-03 11:47:50 -08001359 * This routine is called by NET_RX_SOFTIRQ. It receives a packet, builds a
1360 * FC frame and passes the frame to libfc.
Robert Love85b4aa42008-12-09 15:10:24 -08001361 *
1362 * Returns: 0 for success
Robert Love34f42a02009-02-27 10:55:45 -08001363 */
Bart Van Assche7c9c6842012-01-13 17:26:25 -08001364static int fcoe_rcv(struct sk_buff *skb, struct net_device *netdev,
Robert Love85b4aa42008-12-09 15:10:24 -08001365 struct packet_type *ptype, struct net_device *olddev)
1366{
Robert Love1875f272009-11-03 11:47:50 -08001367 struct fc_lport *lport;
Robert Love85b4aa42008-12-09 15:10:24 -08001368 struct fcoe_rcv_info *fr;
Chris Leech259ad852009-08-25 13:59:41 -07001369 struct fcoe_interface *fcoe;
Robert Love85b4aa42008-12-09 15:10:24 -08001370 struct fc_frame_header *fh;
Robert Love85b4aa42008-12-09 15:10:24 -08001371 struct fcoe_percpu_s *fps;
Vasu Dev519e5132010-07-20 15:19:32 -07001372 struct ethhdr *eh;
Vasu Devb2f00912009-08-25 13:58:53 -07001373 unsigned int cpu;
Robert Love85b4aa42008-12-09 15:10:24 -08001374
Chris Leech259ad852009-08-25 13:59:41 -07001375 fcoe = container_of(ptype, struct fcoe_interface, fcoe_packet_type);
Robert Love1875f272009-11-03 11:47:50 -08001376 lport = fcoe->ctlr.lp;
1377 if (unlikely(!lport)) {
1378 FCOE_NETDEV_DBG(netdev, "Cannot find hba structure");
Robert Love85b4aa42008-12-09 15:10:24 -08001379 goto err2;
1380 }
Robert Love1875f272009-11-03 11:47:50 -08001381 if (!lport->link_up)
Joe Eykholt97c83892009-03-17 11:42:40 -07001382 goto err2;
Robert Love85b4aa42008-12-09 15:10:24 -08001383
Robert Love1875f272009-11-03 11:47:50 -08001384 FCOE_NETDEV_DBG(netdev, "skb_info: len:%d data_len:%d head:%p "
Robert Loved5488eb2009-06-10 15:30:59 -07001385 "data:%p tail:%p end:%p sum:%d dev:%s",
1386 skb->len, skb->data_len, skb->head, skb->data,
1387 skb_tail_pointer(skb), skb_end_pointer(skb),
1388 skb->csum, skb->dev ? skb->dev->name : "<NULL>");
Robert Love85b4aa42008-12-09 15:10:24 -08001389
Vasu Dev519e5132010-07-20 15:19:32 -07001390 eh = eth_hdr(skb);
Vasu Dev519e5132010-07-20 15:19:32 -07001391
1392 if (is_fip_mode(&fcoe->ctlr) &&
1393 compare_ether_addr(eh->h_source, fcoe->ctlr.dest_addr)) {
1394 FCOE_NETDEV_DBG(netdev, "wrong source mac address:%pM\n",
1395 eh->h_source);
Robert Love85b4aa42008-12-09 15:10:24 -08001396 goto err;
1397 }
1398
1399 /*
1400 * Check for minimum frame length, and make sure required FCoE
1401 * and FC headers are pulled into the linear data area.
1402 */
1403 if (unlikely((skb->len < FCOE_MIN_FRAME) ||
Robert Love1875f272009-11-03 11:47:50 -08001404 !pskb_may_pull(skb, FCOE_HEADER_LEN)))
Robert Love85b4aa42008-12-09 15:10:24 -08001405 goto err;
1406
1407 skb_set_transport_header(skb, sizeof(struct fcoe_hdr));
1408 fh = (struct fc_frame_header *) skb_transport_header(skb);
1409
Robert Love0ee31cb2010-10-08 17:12:46 -07001410 if (ntoh24(&eh->h_dest[3]) != ntoh24(fh->fh_d_id)) {
1411 FCOE_NETDEV_DBG(netdev, "FC frame d_id mismatch with MAC:%pM\n",
1412 eh->h_dest);
1413 goto err;
1414 }
1415
Robert Love85b4aa42008-12-09 15:10:24 -08001416 fr = fcoe_dev_from_skb(skb);
Robert Love1875f272009-11-03 11:47:50 -08001417 fr->fr_dev = lport;
Robert Love5e5e92d2009-03-17 11:41:35 -07001418
Robert Love85b4aa42008-12-09 15:10:24 -08001419 /*
Vasu Devb2f00912009-08-25 13:58:53 -07001420 * In case the incoming frame's exchange is originated from
1421 * the initiator, then received frame's exchange id is ANDed
1422 * with fc_cpu_mask bits to get the same cpu on which exchange
Vasu Devd2722812011-07-27 15:11:10 -07001423 * was originated, otherwise select cpu using rx exchange id
1424 * or fcoe_select_cpu().
Robert Love85b4aa42008-12-09 15:10:24 -08001425 */
Vasu Devb2f00912009-08-25 13:58:53 -07001426 if (ntoh24(fh->fh_f_ctl) & FC_FC_EX_CTX)
1427 cpu = ntohs(fh->fh_ox_id) & fc_cpu_mask;
Kiran Patil064287e2011-06-20 16:59:20 -07001428 else {
Vasu Devd2722812011-07-27 15:11:10 -07001429 if (ntohs(fh->fh_rx_id) == FC_XID_UNKNOWN)
1430 cpu = fcoe_select_cpu();
1431 else
Kiran Patil29bdd2b2011-06-20 16:59:25 -07001432 cpu = ntohs(fh->fh_rx_id) & fc_cpu_mask;
Kiran Patil064287e2011-06-20 16:59:20 -07001433 }
Vasu Dev324f6672011-07-27 15:10:39 -07001434
1435 if (cpu >= nr_cpu_ids)
1436 goto err;
1437
Robert Love8976f422009-03-17 11:41:46 -07001438 fps = &per_cpu(fcoe_percpu, cpu);
Neil Horman94aa29f2012-03-09 14:50:08 -08001439 spin_lock(&fps->fcoe_rx_list.lock);
Robert Love8976f422009-03-17 11:41:46 -07001440 if (unlikely(!fps->thread)) {
1441 /*
1442 * The targeted CPU is not ready, let's target
1443 * the first CPU now. For non-SMP systems this
1444 * will check the same CPU twice.
1445 */
Robert Love1875f272009-11-03 11:47:50 -08001446 FCOE_NETDEV_DBG(netdev, "CPU is online, but no receive thread "
Robert Loved5488eb2009-06-10 15:30:59 -07001447 "ready for incoming skb- using first online "
1448 "CPU.\n");
Robert Love8976f422009-03-17 11:41:46 -07001449
Neil Horman94aa29f2012-03-09 14:50:08 -08001450 spin_unlock(&fps->fcoe_rx_list.lock);
Rusty Russell69571772009-12-17 11:43:14 -06001451 cpu = cpumask_first(cpu_online_mask);
Robert Love8976f422009-03-17 11:41:46 -07001452 fps = &per_cpu(fcoe_percpu, cpu);
Neil Horman94aa29f2012-03-09 14:50:08 -08001453 spin_lock(&fps->fcoe_rx_list.lock);
Robert Love8976f422009-03-17 11:41:46 -07001454 if (!fps->thread) {
Neil Horman94aa29f2012-03-09 14:50:08 -08001455 spin_unlock(&fps->fcoe_rx_list.lock);
Robert Love8976f422009-03-17 11:41:46 -07001456 goto err;
1457 }
1458 }
1459
1460 /*
1461 * We now have a valid CPU that we're targeting for
1462 * this skb. We also have this receive thread locked,
1463 * so we're free to queue skbs into it's queue.
1464 */
Robert Love85b4aa42008-12-09 15:10:24 -08001465
Neil Horman5e70c4c2012-03-09 14:49:48 -08001466 /*
1467 * Note: We used to have a set of conditions under which we would
1468 * call fcoe_recv_frame directly, rather than queuing to the rx list
1469 * as it could save a few cycles, but doing so is prohibited, as
1470 * fcoe_recv_frame has several paths that may sleep, which is forbidden
1471 * in softirq context.
Chris Leech859b7b62009-11-20 14:54:47 -08001472 */
Neil Horman5e70c4c2012-03-09 14:49:48 -08001473 __skb_queue_tail(&fps->fcoe_rx_list, skb);
1474 if (fps->fcoe_rx_list.qlen == 1)
1475 wake_up_process(fps->thread);
Neil Horman94aa29f2012-03-09 14:50:08 -08001476 spin_unlock(&fps->fcoe_rx_list.lock);
Robert Love85b4aa42008-12-09 15:10:24 -08001477
1478 return 0;
1479err:
Joe Eykholtf018b732010-03-12 16:08:55 -08001480 per_cpu_ptr(lport->dev_stats, get_cpu())->ErrorFrames++;
1481 put_cpu();
Robert Love85b4aa42008-12-09 15:10:24 -08001482err2:
1483 kfree_skb(skb);
1484 return -1;
1485}
Robert Love85b4aa42008-12-09 15:10:24 -08001486
1487/**
Bhanu Prakash Gollapudi8597ae82011-01-28 16:05:37 -08001488 * fcoe_alloc_paged_crc_eof() - Allocate a page to be used for the trailer CRC
Robert Love1875f272009-11-03 11:47:50 -08001489 * @skb: The packet to be transmitted
1490 * @tlen: The total length of the trailer
1491 *
Robert Love85b4aa42008-12-09 15:10:24 -08001492 * Returns: 0 for success
Robert Love34f42a02009-02-27 10:55:45 -08001493 */
Bhanu Prakash Gollapudi8597ae82011-01-28 16:05:37 -08001494static int fcoe_alloc_paged_crc_eof(struct sk_buff *skb, int tlen)
Robert Love85b4aa42008-12-09 15:10:24 -08001495{
1496 struct fcoe_percpu_s *fps;
Bhanu Prakash Gollapudi8597ae82011-01-28 16:05:37 -08001497 int rc;
Robert Love85b4aa42008-12-09 15:10:24 -08001498
Robert Love5e5e92d2009-03-17 11:41:35 -07001499 fps = &get_cpu_var(fcoe_percpu);
Bhanu Prakash Gollapudi8597ae82011-01-28 16:05:37 -08001500 rc = fcoe_get_paged_crc_eof(skb, tlen, fps);
Robert Love5e5e92d2009-03-17 11:41:35 -07001501 put_cpu_var(fcoe_percpu);
Robert Love85b4aa42008-12-09 15:10:24 -08001502
Bhanu Prakash Gollapudi8597ae82011-01-28 16:05:37 -08001503 return rc;
Robert Love85b4aa42008-12-09 15:10:24 -08001504}
Robert Love85b4aa42008-12-09 15:10:24 -08001505
1506/**
Robert Love1875f272009-11-03 11:47:50 -08001507 * fcoe_xmit() - Transmit a FCoE frame
1508 * @lport: The local port that the frame is to be transmitted for
1509 * @fp: The frame to be transmitted
Robert Love85b4aa42008-12-09 15:10:24 -08001510 *
Robert Love1875f272009-11-03 11:47:50 -08001511 * Return: 0 for success
Robert Love34f42a02009-02-27 10:55:45 -08001512 */
Bart Van Assche7c9c6842012-01-13 17:26:25 -08001513static int fcoe_xmit(struct fc_lport *lport, struct fc_frame *fp)
Robert Love85b4aa42008-12-09 15:10:24 -08001514{
Vasu Dev4bb6b512009-05-06 10:52:34 -07001515 int wlen;
Robert Love85b4aa42008-12-09 15:10:24 -08001516 u32 crc;
1517 struct ethhdr *eh;
1518 struct fcoe_crc_eof *cp;
1519 struct sk_buff *skb;
1520 struct fcoe_dev_stats *stats;
1521 struct fc_frame_header *fh;
1522 unsigned int hlen; /* header length implies the version */
1523 unsigned int tlen; /* trailer length */
1524 unsigned int elen; /* eth header, may include vlan */
Robert Love1875f272009-11-03 11:47:50 -08001525 struct fcoe_port *port = lport_priv(lport);
Bhanu Prakash Gollapudi8597ae82011-01-28 16:05:37 -08001526 struct fcoe_interface *fcoe = port->priv;
Robert Love85b4aa42008-12-09 15:10:24 -08001527 u8 sof, eof;
1528 struct fcoe_hdr *hp;
1529
1530 WARN_ON((fr_len(fp) % sizeof(u32)) != 0);
1531
Robert Love85b4aa42008-12-09 15:10:24 -08001532 fh = fc_frame_header_get(fp);
Joe Eykholt97c83892009-03-17 11:42:40 -07001533 skb = fp_skb(fp);
1534 wlen = skb->len / FCOE_WORD_TO_BYTE;
1535
Robert Love1875f272009-11-03 11:47:50 -08001536 if (!lport->link_up) {
Dan Carpenter3caf02e2009-04-21 16:27:25 -07001537 kfree_skb(skb);
Joe Eykholt97c83892009-03-17 11:42:40 -07001538 return 0;
Robert Love85b4aa42008-12-09 15:10:24 -08001539 }
1540
Joe Eykholt9860eeb2010-03-12 16:07:52 -08001541 if (unlikely(fh->fh_type == FC_TYPE_ELS) &&
Robert Love1875f272009-11-03 11:47:50 -08001542 fcoe_ctlr_els_send(&fcoe->ctlr, lport, skb))
Joe Eykholt97c83892009-03-17 11:42:40 -07001543 return 0;
1544
Robert Love85b4aa42008-12-09 15:10:24 -08001545 sof = fr_sof(fp);
1546 eof = fr_eof(fp);
1547
Vasu Dev4e57e1c2009-05-06 10:52:46 -07001548 elen = sizeof(struct ethhdr);
Robert Love85b4aa42008-12-09 15:10:24 -08001549 hlen = sizeof(struct fcoe_hdr);
1550 tlen = sizeof(struct fcoe_crc_eof);
1551 wlen = (skb->len - tlen + sizeof(crc)) / FCOE_WORD_TO_BYTE;
1552
1553 /* crc offload */
Robert Love1875f272009-11-03 11:47:50 -08001554 if (likely(lport->crc_offload)) {
Yi Zou39ca9a02009-02-27 14:07:15 -08001555 skb->ip_summed = CHECKSUM_PARTIAL;
Robert Love85b4aa42008-12-09 15:10:24 -08001556 skb->csum_start = skb_headroom(skb);
1557 skb->csum_offset = skb->len;
1558 crc = 0;
1559 } else {
1560 skb->ip_summed = CHECKSUM_NONE;
1561 crc = fcoe_fc_crc(fp);
1562 }
1563
Chris Leech014f5c32009-08-25 13:59:30 -07001564 /* copy port crc and eof to the skb buff */
Robert Love85b4aa42008-12-09 15:10:24 -08001565 if (skb_is_nonlinear(skb)) {
1566 skb_frag_t *frag;
Bhanu Prakash Gollapudi8597ae82011-01-28 16:05:37 -08001567 if (fcoe_alloc_paged_crc_eof(skb, tlen)) {
Roel Kluine9041582009-02-27 10:56:22 -08001568 kfree_skb(skb);
Robert Love85b4aa42008-12-09 15:10:24 -08001569 return -ENOMEM;
1570 }
1571 frag = &skb_shinfo(skb)->frags[skb_shinfo(skb)->nr_frags - 1];
Ian Campbell165c68d2011-08-24 22:28:15 +00001572 cp = kmap_atomic(skb_frag_page(frag), KM_SKB_DATA_SOFTIRQ)
Robert Love85b4aa42008-12-09 15:10:24 -08001573 + frag->page_offset;
1574 } else {
1575 cp = (struct fcoe_crc_eof *)skb_put(skb, tlen);
1576 }
1577
1578 memset(cp, 0, sizeof(*cp));
1579 cp->fcoe_eof = eof;
1580 cp->fcoe_crc32 = cpu_to_le32(~crc);
1581
1582 if (skb_is_nonlinear(skb)) {
1583 kunmap_atomic(cp, KM_SKB_DATA_SOFTIRQ);
1584 cp = NULL;
1585 }
1586
Chris Leech014f5c32009-08-25 13:59:30 -07001587 /* adjust skb network/transport offsets to match mac/fcoe/port */
Robert Love85b4aa42008-12-09 15:10:24 -08001588 skb_push(skb, elen + hlen);
1589 skb_reset_mac_header(skb);
1590 skb_reset_network_header(skb);
1591 skb->mac_len = elen;
Yi Zou211c7382009-02-27 14:06:37 -08001592 skb->protocol = htons(ETH_P_FCOE);
john fastabend6f6c2aa2011-11-18 13:35:56 -08001593 skb->priority = port->priority;
1594
Vasu Devd1483bb2011-09-27 21:38:13 -07001595 if (fcoe->netdev->priv_flags & IFF_802_1Q_VLAN &&
1596 fcoe->realdev->features & NETIF_F_HW_VLAN_TX) {
1597 skb->vlan_tci = VLAN_TAG_PRESENT |
1598 vlan_dev_vlan_id(fcoe->netdev);
1599 skb->dev = fcoe->realdev;
1600 } else
1601 skb->dev = fcoe->netdev;
Robert Love85b4aa42008-12-09 15:10:24 -08001602
1603 /* fill up mac and fcoe headers */
1604 eh = eth_hdr(skb);
1605 eh->h_proto = htons(ETH_P_FCOE);
Joe Eykholtcd229e42010-07-20 15:20:40 -07001606 memcpy(eh->h_dest, fcoe->ctlr.dest_addr, ETH_ALEN);
Chris Leech3fe9a0b2009-08-25 13:59:46 -07001607 if (fcoe->ctlr.map_dest)
Joe Eykholtcd229e42010-07-20 15:20:40 -07001608 memcpy(eh->h_dest + 3, fh->fh_d_id, 3);
Robert Love85b4aa42008-12-09 15:10:24 -08001609
Chris Leech3fe9a0b2009-08-25 13:59:46 -07001610 if (unlikely(fcoe->ctlr.flogi_oxid != FC_XID_UNKNOWN))
1611 memcpy(eh->h_source, fcoe->ctlr.ctl_src_addr, ETH_ALEN);
Robert Love85b4aa42008-12-09 15:10:24 -08001612 else
Chris Leech11b56182009-11-03 11:46:29 -08001613 memcpy(eh->h_source, port->data_src_addr, ETH_ALEN);
Robert Love85b4aa42008-12-09 15:10:24 -08001614
1615 hp = (struct fcoe_hdr *)(eh + 1);
1616 memset(hp, 0, sizeof(*hp));
1617 if (FC_FCOE_VER)
1618 FC_FCOE_ENCAPS_VER(hp, FC_FCOE_VER);
1619 hp->fcoe_sof = sof;
1620
Yi Zou39ca9a02009-02-27 14:07:15 -08001621 /* fcoe lso, mss is in max_payload which is non-zero for FCP data */
Robert Love1875f272009-11-03 11:47:50 -08001622 if (lport->seq_offload && fr_max_payload(fp)) {
Yi Zou39ca9a02009-02-27 14:07:15 -08001623 skb_shinfo(skb)->gso_type = SKB_GSO_FCOE;
1624 skb_shinfo(skb)->gso_size = fr_max_payload(fp);
1625 } else {
1626 skb_shinfo(skb)->gso_type = 0;
1627 skb_shinfo(skb)->gso_size = 0;
1628 }
Robert Love85b4aa42008-12-09 15:10:24 -08001629 /* update tx stats: regardless if LLD fails */
Joe Eykholtf018b732010-03-12 16:08:55 -08001630 stats = per_cpu_ptr(lport->dev_stats, get_cpu());
Robert Love582b45b2009-03-31 15:51:50 -07001631 stats->TxFrames++;
1632 stats->TxWords += wlen;
Joe Eykholtf018b732010-03-12 16:08:55 -08001633 put_cpu();
Robert Love85b4aa42008-12-09 15:10:24 -08001634
1635 /* send down to lld */
Robert Love1875f272009-11-03 11:47:50 -08001636 fr_dev(fp) = lport;
Vasu Dev980f5152011-07-27 15:11:05 -07001637 fcoe_port_send(port, skb);
Robert Love85b4aa42008-12-09 15:10:24 -08001638 return 0;
1639}
Robert Love85b4aa42008-12-09 15:10:24 -08001640
Robert Love34f42a02009-02-27 10:55:45 -08001641/**
Robert Love1875f272009-11-03 11:47:50 -08001642 * fcoe_percpu_flush_done() - Indicate per-CPU queue flush completion
1643 * @skb: The completed skb (argument required by destructor)
Joe Eykholte7a51992009-08-25 14:04:08 -07001644 */
1645static void fcoe_percpu_flush_done(struct sk_buff *skb)
1646{
1647 complete(&fcoe_flush_completion);
1648}
1649
1650/**
Vasu Dev52ee8322011-01-28 16:03:52 -08001651 * fcoe_filter_frames() - filter out bad fcoe frames, i.e. bad CRC
1652 * @lport: The local port the frame was received on
1653 * @fp: The received frame
1654 *
1655 * Return: 0 on passing filtering checks
1656 */
1657static inline int fcoe_filter_frames(struct fc_lport *lport,
1658 struct fc_frame *fp)
1659{
1660 struct fcoe_interface *fcoe;
1661 struct fc_frame_header *fh;
1662 struct sk_buff *skb = (struct sk_buff *)fp;
1663 struct fcoe_dev_stats *stats;
1664
1665 /*
1666 * We only check CRC if no offload is available and if it is
1667 * it's solicited data, in which case, the FCP layer would
1668 * check it during the copy.
1669 */
1670 if (lport->crc_offload && skb->ip_summed == CHECKSUM_UNNECESSARY)
1671 fr_flags(fp) &= ~FCPHF_CRC_UNCHECKED;
1672 else
1673 fr_flags(fp) |= FCPHF_CRC_UNCHECKED;
1674
1675 fh = (struct fc_frame_header *) skb_transport_header(skb);
1676 fh = fc_frame_header_get(fp);
1677 if (fh->fh_r_ctl == FC_RCTL_DD_SOL_DATA && fh->fh_type == FC_TYPE_FCP)
1678 return 0;
1679
Bhanu Prakash Gollapudi8597ae82011-01-28 16:05:37 -08001680 fcoe = ((struct fcoe_port *)lport_priv(lport))->priv;
Vasu Dev52ee8322011-01-28 16:03:52 -08001681 if (is_fip_mode(&fcoe->ctlr) && fc_frame_payload_op(fp) == ELS_LOGO &&
1682 ntoh24(fh->fh_s_id) == FC_FID_FLOGI) {
1683 FCOE_DBG("fcoe: dropping FCoE lport LOGO in fip mode\n");
1684 return -EINVAL;
1685 }
1686
Dan Carpenterf2f96d22011-02-25 15:03:23 -08001687 if (!(fr_flags(fp) & FCPHF_CRC_UNCHECKED) ||
Vasu Dev52ee8322011-01-28 16:03:52 -08001688 le32_to_cpu(fr_crc(fp)) == ~crc32(~0, skb->data, skb->len)) {
1689 fr_flags(fp) &= ~FCPHF_CRC_UNCHECKED;
1690 return 0;
1691 }
1692
1693 stats = per_cpu_ptr(lport->dev_stats, get_cpu());
1694 stats->InvalidCRCCount++;
1695 if (stats->InvalidCRCCount < 5)
1696 printk(KERN_WARNING "fcoe: dropping frame with CRC error\n");
Thomas Gleixner7e1e7ea2011-11-11 20:52:01 +01001697 put_cpu();
Vasu Dev52ee8322011-01-28 16:03:52 -08001698 return -EINVAL;
1699}
1700
1701/**
Chris Leech859b7b62009-11-20 14:54:47 -08001702 * fcoe_recv_frame() - process a single received frame
1703 * @skb: frame to process
1704 */
1705static void fcoe_recv_frame(struct sk_buff *skb)
1706{
1707 u32 fr_len;
1708 struct fc_lport *lport;
1709 struct fcoe_rcv_info *fr;
1710 struct fcoe_dev_stats *stats;
Chris Leech859b7b62009-11-20 14:54:47 -08001711 struct fcoe_crc_eof crc_eof;
1712 struct fc_frame *fp;
Chris Leech859b7b62009-11-20 14:54:47 -08001713 struct fcoe_port *port;
1714 struct fcoe_hdr *hp;
1715
1716 fr = fcoe_dev_from_skb(skb);
1717 lport = fr->fr_dev;
1718 if (unlikely(!lport)) {
1719 if (skb->destructor != fcoe_percpu_flush_done)
1720 FCOE_NETDEV_DBG(skb->dev, "NULL lport in skb");
1721 kfree_skb(skb);
1722 return;
1723 }
1724
1725 FCOE_NETDEV_DBG(skb->dev, "skb_info: len:%d data_len:%d "
1726 "head:%p data:%p tail:%p end:%p sum:%d dev:%s",
1727 skb->len, skb->data_len,
1728 skb->head, skb->data, skb_tail_pointer(skb),
1729 skb_end_pointer(skb), skb->csum,
1730 skb->dev ? skb->dev->name : "<NULL>");
1731
Chris Leech859b7b62009-11-20 14:54:47 -08001732 port = lport_priv(lport);
Robert Lovef1633012011-12-16 14:24:49 -08001733 skb_linearize(skb); /* check for skb_is_nonlinear is within skb_linearize */
Chris Leech859b7b62009-11-20 14:54:47 -08001734
1735 /*
1736 * Frame length checks and setting up the header pointers
1737 * was done in fcoe_rcv already.
1738 */
1739 hp = (struct fcoe_hdr *) skb_network_header(skb);
Chris Leech859b7b62009-11-20 14:54:47 -08001740
Joe Eykholtf018b732010-03-12 16:08:55 -08001741 stats = per_cpu_ptr(lport->dev_stats, get_cpu());
Chris Leech859b7b62009-11-20 14:54:47 -08001742 if (unlikely(FC_FCOE_DECAPS_VER(hp) != FC_FCOE_VER)) {
1743 if (stats->ErrorFrames < 5)
1744 printk(KERN_WARNING "fcoe: FCoE version "
1745 "mismatch: The frame has "
1746 "version %x, but the "
1747 "initiator supports version "
1748 "%x\n", FC_FCOE_DECAPS_VER(hp),
1749 FC_FCOE_VER);
Joe Eykholtf018b732010-03-12 16:08:55 -08001750 goto drop;
Chris Leech859b7b62009-11-20 14:54:47 -08001751 }
1752
1753 skb_pull(skb, sizeof(struct fcoe_hdr));
1754 fr_len = skb->len - sizeof(struct fcoe_crc_eof);
1755
1756 stats->RxFrames++;
1757 stats->RxWords += fr_len / FCOE_WORD_TO_BYTE;
1758
1759 fp = (struct fc_frame *)skb;
1760 fc_frame_init(fp);
1761 fr_dev(fp) = lport;
1762 fr_sof(fp) = hp->fcoe_sof;
1763
1764 /* Copy out the CRC and EOF trailer for access */
Joe Eykholtf018b732010-03-12 16:08:55 -08001765 if (skb_copy_bits(skb, fr_len, &crc_eof, sizeof(crc_eof)))
1766 goto drop;
Chris Leech859b7b62009-11-20 14:54:47 -08001767 fr_eof(fp) = crc_eof.fcoe_eof;
1768 fr_crc(fp) = crc_eof.fcoe_crc32;
Joe Eykholtf018b732010-03-12 16:08:55 -08001769 if (pskb_trim(skb, fr_len))
1770 goto drop;
Chris Leech859b7b62009-11-20 14:54:47 -08001771
Vasu Dev52ee8322011-01-28 16:03:52 -08001772 if (!fcoe_filter_frames(lport, fp)) {
1773 put_cpu();
1774 fc_exch_recv(lport, fp);
1775 return;
Chris Leech859b7b62009-11-20 14:54:47 -08001776 }
Joe Eykholtf018b732010-03-12 16:08:55 -08001777drop:
1778 stats->ErrorFrames++;
1779 put_cpu();
1780 kfree_skb(skb);
Chris Leech859b7b62009-11-20 14:54:47 -08001781}
1782
1783/**
Robert Love1875f272009-11-03 11:47:50 -08001784 * fcoe_percpu_receive_thread() - The per-CPU packet receive thread
1785 * @arg: The per-CPU context
Robert Love85b4aa42008-12-09 15:10:24 -08001786 *
1787 * Return: 0 for success
Robert Love85b4aa42008-12-09 15:10:24 -08001788 */
Bart Van Assche7c9c6842012-01-13 17:26:25 -08001789static int fcoe_percpu_receive_thread(void *arg)
Robert Love85b4aa42008-12-09 15:10:24 -08001790{
1791 struct fcoe_percpu_s *p = arg;
Robert Love85b4aa42008-12-09 15:10:24 -08001792 struct sk_buff *skb;
Robert Love85b4aa42008-12-09 15:10:24 -08001793
Robert Love4469c192009-02-27 10:56:38 -08001794 set_user_nice(current, -20);
Robert Love85b4aa42008-12-09 15:10:24 -08001795
1796 while (!kthread_should_stop()) {
1797
1798 spin_lock_bh(&p->fcoe_rx_list.lock);
1799 while ((skb = __skb_dequeue(&p->fcoe_rx_list)) == NULL) {
1800 set_current_state(TASK_INTERRUPTIBLE);
1801 spin_unlock_bh(&p->fcoe_rx_list.lock);
1802 schedule();
1803 set_current_state(TASK_RUNNING);
1804 if (kthread_should_stop())
1805 return 0;
1806 spin_lock_bh(&p->fcoe_rx_list.lock);
1807 }
1808 spin_unlock_bh(&p->fcoe_rx_list.lock);
Chris Leech859b7b62009-11-20 14:54:47 -08001809 fcoe_recv_frame(skb);
Robert Love85b4aa42008-12-09 15:10:24 -08001810 }
1811 return 0;
1812}
1813
1814/**
Robert Love1875f272009-11-03 11:47:50 -08001815 * fcoe_dev_setup() - Setup the link change notification interface
Robert Love34f42a02009-02-27 10:55:45 -08001816 */
Randy Dunlapb0d428a2009-04-27 21:49:31 -07001817static void fcoe_dev_setup(void)
Robert Love85b4aa42008-12-09 15:10:24 -08001818{
john fastabend6f6c2aa2011-11-18 13:35:56 -08001819 register_dcbevent_notifier(&dcb_notifier);
Robert Love85b4aa42008-12-09 15:10:24 -08001820 register_netdevice_notifier(&fcoe_notifier);
1821}
1822
1823/**
Robert Love1875f272009-11-03 11:47:50 -08001824 * fcoe_dev_cleanup() - Cleanup the link change notification interface
Robert Love34f42a02009-02-27 10:55:45 -08001825 */
Robert Love85b4aa42008-12-09 15:10:24 -08001826static void fcoe_dev_cleanup(void)
1827{
john fastabend6f6c2aa2011-11-18 13:35:56 -08001828 unregister_dcbevent_notifier(&dcb_notifier);
Robert Love85b4aa42008-12-09 15:10:24 -08001829 unregister_netdevice_notifier(&fcoe_notifier);
1830}
1831
john fastabend6f6c2aa2011-11-18 13:35:56 -08001832static struct fcoe_interface *
1833fcoe_hostlist_lookup_realdev_port(struct net_device *netdev)
1834{
1835 struct fcoe_interface *fcoe;
1836 struct net_device *real_dev;
1837
1838 list_for_each_entry(fcoe, &fcoe_hostlist, list) {
1839 if (fcoe->netdev->priv_flags & IFF_802_1Q_VLAN)
1840 real_dev = vlan_dev_real_dev(fcoe->netdev);
1841 else
1842 real_dev = fcoe->netdev;
1843
1844 if (netdev == real_dev)
1845 return fcoe;
1846 }
1847 return NULL;
1848}
1849
1850static int fcoe_dcb_app_notification(struct notifier_block *notifier,
1851 ulong event, void *ptr)
1852{
1853 struct dcb_app_type *entry = ptr;
1854 struct fcoe_interface *fcoe;
1855 struct net_device *netdev;
1856 struct fcoe_port *port;
1857 int prio;
1858
1859 if (entry->app.selector != DCB_APP_IDTYPE_ETHTYPE)
1860 return NOTIFY_OK;
1861
1862 netdev = dev_get_by_index(&init_net, entry->ifindex);
1863 if (!netdev)
1864 return NOTIFY_OK;
1865
1866 fcoe = fcoe_hostlist_lookup_realdev_port(netdev);
1867 dev_put(netdev);
1868 if (!fcoe)
1869 return NOTIFY_OK;
1870
1871 if (entry->dcbx & DCB_CAP_DCBX_VER_CEE)
1872 prio = ffs(entry->app.priority) - 1;
1873 else
1874 prio = entry->app.priority;
1875
1876 if (prio < 0)
1877 return NOTIFY_OK;
1878
1879 if (entry->app.protocol == ETH_P_FIP ||
1880 entry->app.protocol == ETH_P_FCOE)
1881 fcoe->ctlr.priority = prio;
1882
1883 if (entry->app.protocol == ETH_P_FCOE) {
1884 port = lport_priv(fcoe->ctlr.lp);
1885 port->priority = prio;
1886 }
1887
1888 return NOTIFY_OK;
1889}
1890
Robert Love85b4aa42008-12-09 15:10:24 -08001891/**
Robert Love1875f272009-11-03 11:47:50 -08001892 * fcoe_device_notification() - Handler for net device events
1893 * @notifier: The context of the notification
1894 * @event: The type of event
1895 * @ptr: The net device that the event was on
Robert Love85b4aa42008-12-09 15:10:24 -08001896 *
Robert Love1875f272009-11-03 11:47:50 -08001897 * This function is called by the Ethernet driver in case of link change event.
Robert Love85b4aa42008-12-09 15:10:24 -08001898 *
1899 * Returns: 0 for success
Robert Love34f42a02009-02-27 10:55:45 -08001900 */
Robert Love85b4aa42008-12-09 15:10:24 -08001901static int fcoe_device_notification(struct notifier_block *notifier,
1902 ulong event, void *ptr)
1903{
Robert Love1875f272009-11-03 11:47:50 -08001904 struct fc_lport *lport = NULL;
Vasu Dev1d1b88d2009-07-29 17:05:45 -07001905 struct net_device *netdev = ptr;
Chris Leech014f5c32009-08-25 13:59:30 -07001906 struct fcoe_interface *fcoe;
Chris Leech2e70e242009-08-25 14:00:23 -07001907 struct fcoe_port *port;
Robert Love85b4aa42008-12-09 15:10:24 -08001908 struct fcoe_dev_stats *stats;
Joe Eykholt97c83892009-03-17 11:42:40 -07001909 u32 link_possible = 1;
Robert Love85b4aa42008-12-09 15:10:24 -08001910 u32 mfs;
1911 int rc = NOTIFY_OK;
1912
Chris Leech014f5c32009-08-25 13:59:30 -07001913 list_for_each_entry(fcoe, &fcoe_hostlist, list) {
Chris Leech25024982009-08-25 13:59:35 -07001914 if (fcoe->netdev == netdev) {
Robert Love1875f272009-11-03 11:47:50 -08001915 lport = fcoe->ctlr.lp;
Robert Love85b4aa42008-12-09 15:10:24 -08001916 break;
1917 }
1918 }
Robert Love1875f272009-11-03 11:47:50 -08001919 if (!lport) {
Robert Love85b4aa42008-12-09 15:10:24 -08001920 rc = NOTIFY_DONE;
1921 goto out;
1922 }
1923
Robert Love85b4aa42008-12-09 15:10:24 -08001924 switch (event) {
1925 case NETDEV_DOWN:
1926 case NETDEV_GOING_DOWN:
Joe Eykholt97c83892009-03-17 11:42:40 -07001927 link_possible = 0;
Robert Love85b4aa42008-12-09 15:10:24 -08001928 break;
1929 case NETDEV_UP:
1930 case NETDEV_CHANGE:
Robert Love85b4aa42008-12-09 15:10:24 -08001931 break;
1932 case NETDEV_CHANGEMTU:
Yi Zou7221d7e2009-10-21 16:27:52 -07001933 if (netdev->features & NETIF_F_FCOE_MTU)
1934 break;
Vasu Dev1d1b88d2009-07-29 17:05:45 -07001935 mfs = netdev->mtu - (sizeof(struct fcoe_hdr) +
1936 sizeof(struct fcoe_crc_eof));
Robert Love85b4aa42008-12-09 15:10:24 -08001937 if (mfs >= FC_MIN_MAX_FRAME)
Robert Love1875f272009-11-03 11:47:50 -08001938 fc_set_mfs(lport, mfs);
Robert Love85b4aa42008-12-09 15:10:24 -08001939 break;
1940 case NETDEV_REGISTER:
1941 break;
Chris Leech2e70e242009-08-25 14:00:23 -07001942 case NETDEV_UNREGISTER:
1943 list_del(&fcoe->list);
1944 port = lport_priv(fcoe->ctlr.lp);
Tejun Heo2ca32b42011-01-28 16:05:32 -08001945 queue_work(fcoe_wq, &port->destroy_work);
Chris Leech2e70e242009-08-25 14:00:23 -07001946 goto out;
1947 break;
Yi Zou54a5b212010-07-20 15:21:17 -07001948 case NETDEV_FEAT_CHANGE:
1949 fcoe_netdev_features_change(lport, netdev);
1950 break;
Robert Love85b4aa42008-12-09 15:10:24 -08001951 default:
Vasu Dev1d1b88d2009-07-29 17:05:45 -07001952 FCOE_NETDEV_DBG(netdev, "Unknown event %ld "
Robert Loved5488eb2009-06-10 15:30:59 -07001953 "from netdev netlink\n", event);
Robert Love85b4aa42008-12-09 15:10:24 -08001954 }
Robert Love5e4f8fe2010-05-07 15:18:35 -07001955
1956 fcoe_link_speed_update(lport);
1957
Robert Love1875f272009-11-03 11:47:50 -08001958 if (link_possible && !fcoe_link_ok(lport))
Chris Leech3fe9a0b2009-08-25 13:59:46 -07001959 fcoe_ctlr_link_up(&fcoe->ctlr);
1960 else if (fcoe_ctlr_link_down(&fcoe->ctlr)) {
Joe Eykholtf018b732010-03-12 16:08:55 -08001961 stats = per_cpu_ptr(lport->dev_stats, get_cpu());
Joe Eykholt97c83892009-03-17 11:42:40 -07001962 stats->LinkFailureCount++;
Joe Eykholtf018b732010-03-12 16:08:55 -08001963 put_cpu();
Robert Love1875f272009-11-03 11:47:50 -08001964 fcoe_clean_pending_queue(lport);
Robert Love85b4aa42008-12-09 15:10:24 -08001965 }
1966out:
1967 return rc;
1968}
1969
1970/**
Vasu Dev55a66d32009-12-10 09:59:31 -08001971 * fcoe_disable() - Disables a FCoE interface
Yi Zou78a58242011-01-28 16:05:16 -08001972 * @netdev : The net_device object the Ethernet interface to create on
Vasu Dev55a66d32009-12-10 09:59:31 -08001973 *
Yi Zou78a58242011-01-28 16:05:16 -08001974 * Called from fcoe transport.
Vasu Dev55a66d32009-12-10 09:59:31 -08001975 *
1976 * Returns: 0 for success
1977 */
Yi Zou78a58242011-01-28 16:05:16 -08001978static int fcoe_disable(struct net_device *netdev)
Vasu Dev55a66d32009-12-10 09:59:31 -08001979{
1980 struct fcoe_interface *fcoe;
Vasu Dev55a66d32009-12-10 09:59:31 -08001981 int rc = 0;
1982
1983 mutex_lock(&fcoe_config_mutex);
Vasu Dev55a66d32009-12-10 09:59:31 -08001984
Robert Loveee5df622011-04-01 16:05:59 -07001985 rtnl_lock();
Vasu Dev55a66d32009-12-10 09:59:31 -08001986 fcoe = fcoe_hostlist_lookup_port(netdev);
1987 rtnl_unlock();
1988
Chris Leech9ee50e42010-04-09 14:22:23 -07001989 if (fcoe) {
Chris Leech9ee50e42010-04-09 14:22:23 -07001990 fcoe_ctlr_link_down(&fcoe->ctlr);
Vasu Dev9d4cbc02010-07-20 15:19:26 -07001991 fcoe_clean_pending_queue(fcoe->ctlr.lp);
Chris Leech9ee50e42010-04-09 14:22:23 -07001992 } else
Vasu Dev55a66d32009-12-10 09:59:31 -08001993 rc = -ENODEV;
1994
Vasu Dev55a66d32009-12-10 09:59:31 -08001995 mutex_unlock(&fcoe_config_mutex);
1996 return rc;
1997}
1998
1999/**
2000 * fcoe_enable() - Enables a FCoE interface
Yi Zou78a58242011-01-28 16:05:16 -08002001 * @netdev : The net_device object the Ethernet interface to create on
Vasu Dev55a66d32009-12-10 09:59:31 -08002002 *
Yi Zou78a58242011-01-28 16:05:16 -08002003 * Called from fcoe transport.
Vasu Dev55a66d32009-12-10 09:59:31 -08002004 *
2005 * Returns: 0 for success
2006 */
Yi Zou78a58242011-01-28 16:05:16 -08002007static int fcoe_enable(struct net_device *netdev)
Vasu Dev55a66d32009-12-10 09:59:31 -08002008{
2009 struct fcoe_interface *fcoe;
Vasu Dev55a66d32009-12-10 09:59:31 -08002010 int rc = 0;
2011
2012 mutex_lock(&fcoe_config_mutex);
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
Vasu Dev9d4cbc02010-07-20 15:19:26 -07002017 if (!fcoe)
Vasu Dev55a66d32009-12-10 09:59:31 -08002018 rc = -ENODEV;
Vasu Dev9d4cbc02010-07-20 15:19:26 -07002019 else if (!fcoe_link_ok(fcoe->ctlr.lp))
2020 fcoe_ctlr_link_up(&fcoe->ctlr);
Vasu Dev55a66d32009-12-10 09:59:31 -08002021
Vasu Dev55a66d32009-12-10 09:59:31 -08002022 mutex_unlock(&fcoe_config_mutex);
2023 return rc;
2024}
2025
2026/**
Robert Love1875f272009-11-03 11:47:50 -08002027 * fcoe_destroy() - Destroy a FCoE interface
Yi Zou78a58242011-01-28 16:05:16 -08002028 * @netdev : The net_device object the Ethernet interface to create on
Robert Love1875f272009-11-03 11:47:50 -08002029 *
Yi Zou78a58242011-01-28 16:05:16 -08002030 * Called from fcoe transport
Robert Love85b4aa42008-12-09 15:10:24 -08002031 *
2032 * Returns: 0 for success
Robert Love34f42a02009-02-27 10:55:45 -08002033 */
Yi Zou78a58242011-01-28 16:05:16 -08002034static int fcoe_destroy(struct net_device *netdev)
Robert Love85b4aa42008-12-09 15:10:24 -08002035{
Chris Leech030f4e02009-08-25 14:00:02 -07002036 struct fcoe_interface *fcoe;
Vasu Devf04ca1b2011-04-01 16:06:45 -07002037 struct fc_lport *lport;
Neerav Parikhb2085a42011-06-20 16:59:51 -07002038 struct fcoe_port *port;
Mike Christie8eca3552009-10-21 16:27:44 -07002039 int rc = 0;
Robert Love85b4aa42008-12-09 15:10:24 -08002040
Chris Leechdfc1d0f2009-08-25 14:00:13 -07002041 mutex_lock(&fcoe_config_mutex);
Robert Loveee5df622011-04-01 16:05:59 -07002042 rtnl_lock();
Chris Leech2e70e242009-08-25 14:00:23 -07002043 fcoe = fcoe_hostlist_lookup_port(netdev);
2044 if (!fcoe) {
Robert Love85b4aa42008-12-09 15:10:24 -08002045 rc = -ENODEV;
Yi Zou78a58242011-01-28 16:05:16 -08002046 goto out_nodev;
Robert Love85b4aa42008-12-09 15:10:24 -08002047 }
Vasu Devf04ca1b2011-04-01 16:06:45 -07002048 lport = fcoe->ctlr.lp;
Neerav Parikhb2085a42011-06-20 16:59:51 -07002049 port = lport_priv(lport);
Yi Zou54a5b212010-07-20 15:21:17 -07002050 list_del(&fcoe->list);
Neerav Parikhb2085a42011-06-20 16:59:51 -07002051 queue_work(fcoe_wq, &port->destroy_work);
Robert Love85b4aa42008-12-09 15:10:24 -08002052out_nodev:
Neerav Parikhb2085a42011-06-20 16:59:51 -07002053 rtnl_unlock();
Chris Leechdfc1d0f2009-08-25 14:00:13 -07002054 mutex_unlock(&fcoe_config_mutex);
Robert Love85b4aa42008-12-09 15:10:24 -08002055 return rc;
2056}
2057
Robert Love1875f272009-11-03 11:47:50 -08002058/**
2059 * fcoe_destroy_work() - Destroy a FCoE port in a deferred work context
2060 * @work: Handle to the FCoE port to be destroyed
2061 */
Chris Leech2e70e242009-08-25 14:00:23 -07002062static void fcoe_destroy_work(struct work_struct *work)
2063{
2064 struct fcoe_port *port;
Neerav Parikhb2085a42011-06-20 16:59:51 -07002065 struct fcoe_interface *fcoe;
Chris Leech2e70e242009-08-25 14:00:23 -07002066
2067 port = container_of(work, struct fcoe_port, destroy_work);
2068 mutex_lock(&fcoe_config_mutex);
Neerav Parikhb2085a42011-06-20 16:59:51 -07002069
Neerav Parikhb2085a42011-06-20 16:59:51 -07002070 fcoe = port->priv;
Chris Leech2e70e242009-08-25 14:00:23 -07002071 fcoe_if_destroy(port->lport);
Robert Loveccefd232012-02-10 17:18:41 -08002072 fcoe_interface_cleanup(fcoe);
Neerav Parikhb2085a42011-06-20 16:59:51 -07002073
Chris Leech2e70e242009-08-25 14:00:23 -07002074 mutex_unlock(&fcoe_config_mutex);
2075}
2076
Robert Love85b4aa42008-12-09 15:10:24 -08002077/**
Yi Zou78a58242011-01-28 16:05:16 -08002078 * fcoe_match() - Check if the FCoE is supported on the given netdevice
2079 * @netdev : The net_device object the Ethernet interface to create on
Robert Love1875f272009-11-03 11:47:50 -08002080 *
Yi Zou78a58242011-01-28 16:05:16 -08002081 * Called from fcoe transport.
2082 *
2083 * Returns: always returns true as this is the default FCoE transport,
2084 * i.e., support all netdevs.
2085 */
2086static bool fcoe_match(struct net_device *netdev)
2087{
2088 return true;
2089}
2090
2091/**
john fastabend6f6c2aa2011-11-18 13:35:56 -08002092 * fcoe_dcb_create() - Initialize DCB attributes and hooks
2093 * @netdev: The net_device object of the L2 link that should be queried
2094 * @port: The fcoe_port to bind FCoE APP priority with
2095 * @
2096 */
2097static void fcoe_dcb_create(struct fcoe_interface *fcoe)
2098{
2099#ifdef CONFIG_DCB
2100 int dcbx;
2101 u8 fup, up;
2102 struct net_device *netdev = fcoe->realdev;
2103 struct fcoe_port *port = lport_priv(fcoe->ctlr.lp);
2104 struct dcb_app app = {
2105 .priority = 0,
2106 .protocol = ETH_P_FCOE
2107 };
2108
2109 /* setup DCB priority attributes. */
2110 if (netdev && netdev->dcbnl_ops && netdev->dcbnl_ops->getdcbx) {
2111 dcbx = netdev->dcbnl_ops->getdcbx(netdev);
2112
2113 if (dcbx & DCB_CAP_DCBX_VER_IEEE) {
2114 app.selector = IEEE_8021QAZ_APP_SEL_ETHERTYPE;
2115 up = dcb_ieee_getapp_mask(netdev, &app);
2116 app.protocol = ETH_P_FIP;
2117 fup = dcb_ieee_getapp_mask(netdev, &app);
2118 } else {
2119 app.selector = DCB_APP_IDTYPE_ETHTYPE;
2120 up = dcb_getapp(netdev, &app);
2121 app.protocol = ETH_P_FIP;
2122 fup = dcb_getapp(netdev, &app);
2123 }
2124
2125 port->priority = ffs(up) ? ffs(up) - 1 : 0;
2126 fcoe->ctlr.priority = ffs(fup) ? ffs(fup) - 1 : port->priority;
2127 }
2128#endif
2129}
2130
2131/**
Yi Zou78a58242011-01-28 16:05:16 -08002132 * fcoe_create() - Create a fcoe interface
2133 * @netdev : The net_device object the Ethernet interface to create on
2134 * @fip_mode: The FIP mode for this creation
2135 *
2136 * Called from fcoe transport
Robert Love85b4aa42008-12-09 15:10:24 -08002137 *
2138 * Returns: 0 for success
Robert Love34f42a02009-02-27 10:55:45 -08002139 */
Yi Zou78a58242011-01-28 16:05:16 -08002140static int fcoe_create(struct net_device *netdev, enum fip_state fip_mode)
Robert Love85b4aa42008-12-09 15:10:24 -08002141{
Neerav Parikhb2085a42011-06-20 16:59:51 -07002142 int rc = 0;
Chris Leech030f4e02009-08-25 14:00:02 -07002143 struct fcoe_interface *fcoe;
Chris Leechaf7f85d2009-08-25 13:59:24 -07002144 struct fc_lport *lport;
Robert Love85b4aa42008-12-09 15:10:24 -08002145
Chris Leechdfc1d0f2009-08-25 14:00:13 -07002146 mutex_lock(&fcoe_config_mutex);
Robert Loveee5df622011-04-01 16:05:59 -07002147 rtnl_lock();
Vasu Dev34ce27b2010-05-07 15:18:46 -07002148
Robert Love85b4aa42008-12-09 15:10:24 -08002149 /* look for existing lport */
2150 if (fcoe_hostlist_lookup(netdev)) {
2151 rc = -EEXIST;
Yi Zou78a58242011-01-28 16:05:16 -08002152 goto out_nodev;
Robert Love85b4aa42008-12-09 15:10:24 -08002153 }
Robert Love85b4aa42008-12-09 15:10:24 -08002154
Joe Eykholt1dd454d2010-07-20 15:20:46 -07002155 fcoe = fcoe_interface_create(netdev, fip_mode);
Robert Love7287fb92011-01-28 16:03:47 -08002156 if (IS_ERR(fcoe)) {
2157 rc = PTR_ERR(fcoe);
Yi Zou78a58242011-01-28 16:05:16 -08002158 goto out_nodev;
Chris Leech030f4e02009-08-25 14:00:02 -07002159 }
2160
Chris Leech9a057532009-11-03 11:46:40 -08002161 lport = fcoe_if_create(fcoe, &netdev->dev, 0);
Chris Leechaf7f85d2009-08-25 13:59:24 -07002162 if (IS_ERR(lport)) {
Robert Loved5488eb2009-06-10 15:30:59 -07002163 printk(KERN_ERR "fcoe: Failed to create interface (%s)\n",
Robert Love85b4aa42008-12-09 15:10:24 -08002164 netdev->name);
Robert Love85b4aa42008-12-09 15:10:24 -08002165 rc = -EIO;
Robert Love848e7d52011-08-25 12:40:47 -07002166 rtnl_unlock();
Chris Leech2e70e242009-08-25 14:00:23 -07002167 fcoe_interface_cleanup(fcoe);
Robert Love848e7d52011-08-25 12:40:47 -07002168 goto out_nortnl;
Robert Love85b4aa42008-12-09 15:10:24 -08002169 }
Chris Leech030f4e02009-08-25 14:00:02 -07002170
Chris Leech54b649f2009-08-25 14:00:07 -07002171 /* Make this the "master" N_Port */
2172 fcoe->ctlr.lp = lport;
Chris Leech030f4e02009-08-25 14:00:02 -07002173
john fastabend6f6c2aa2011-11-18 13:35:56 -08002174 /* setup DCB priority attributes. */
2175 fcoe_dcb_create(fcoe);
2176
Chris Leechc863df32009-08-25 14:00:18 -07002177 /* add to lports list */
2178 fcoe_hostlist_add(lport);
2179
Chris Leech54b649f2009-08-25 14:00:07 -07002180 /* start FIP Discovery and FLOGI */
2181 lport->boot_time = jiffies;
2182 fc_fabric_login(lport);
2183 if (!fcoe_link_ok(lport))
2184 fcoe_ctlr_link_up(&fcoe->ctlr);
2185
Robert Love85b4aa42008-12-09 15:10:24 -08002186out_nodev:
Vasu Dev34ce27b2010-05-07 15:18:46 -07002187 rtnl_unlock();
Robert Love848e7d52011-08-25 12:40:47 -07002188out_nortnl:
Chris Leechdfc1d0f2009-08-25 14:00:13 -07002189 mutex_unlock(&fcoe_config_mutex);
Robert Love85b4aa42008-12-09 15:10:24 -08002190 return rc;
2191}
2192
Robert Love34f42a02009-02-27 10:55:45 -08002193/**
Robert Love5e4f8fe2010-05-07 15:18:35 -07002194 * fcoe_link_speed_update() - Update the supported and actual link speeds
2195 * @lport: The local port to update speeds for
Robert Love85b4aa42008-12-09 15:10:24 -08002196 *
Robert Love5e4f8fe2010-05-07 15:18:35 -07002197 * Returns: 0 if the ethtool query was successful
2198 * -1 if the ethtool query failed
Robert Love85b4aa42008-12-09 15:10:24 -08002199 */
Bart Van Assche7c9c6842012-01-13 17:26:25 -08002200static int fcoe_link_speed_update(struct fc_lport *lport)
Robert Love85b4aa42008-12-09 15:10:24 -08002201{
Bhanu Prakash Gollapudi8597ae82011-01-28 16:05:37 -08002202 struct net_device *netdev = fcoe_netdev(lport);
David Decotigny8ae6dac2011-04-27 18:32:38 +00002203 struct ethtool_cmd ecmd;
Robert Love85b4aa42008-12-09 15:10:24 -08002204
Jiri Pirko4bc71cb2011-09-03 03:34:30 +00002205 if (!__ethtool_get_settings(netdev, &ecmd)) {
Robert Love1875f272009-11-03 11:47:50 -08002206 lport->link_supported_speeds &=
Yi Zou2f718d62009-07-29 17:04:01 -07002207 ~(FC_PORTSPEED_1GBIT | FC_PORTSPEED_10GBIT);
2208 if (ecmd.supported & (SUPPORTED_1000baseT_Half |
2209 SUPPORTED_1000baseT_Full))
Robert Love1875f272009-11-03 11:47:50 -08002210 lport->link_supported_speeds |= FC_PORTSPEED_1GBIT;
Yi Zou2f718d62009-07-29 17:04:01 -07002211 if (ecmd.supported & SUPPORTED_10000baseT_Full)
Robert Love1875f272009-11-03 11:47:50 -08002212 lport->link_supported_speeds |=
Yi Zou2f718d62009-07-29 17:04:01 -07002213 FC_PORTSPEED_10GBIT;
David Decotigny8ae6dac2011-04-27 18:32:38 +00002214 switch (ethtool_cmd_speed(&ecmd)) {
2215 case SPEED_1000:
Robert Love1875f272009-11-03 11:47:50 -08002216 lport->link_speed = FC_PORTSPEED_1GBIT;
David Decotigny8ae6dac2011-04-27 18:32:38 +00002217 break;
2218 case SPEED_10000:
Robert Love1875f272009-11-03 11:47:50 -08002219 lport->link_speed = FC_PORTSPEED_10GBIT;
David Decotigny8ae6dac2011-04-27 18:32:38 +00002220 break;
2221 }
Yi Zou2f718d62009-07-29 17:04:01 -07002222 return 0;
2223 }
2224 return -1;
Robert Love85b4aa42008-12-09 15:10:24 -08002225}
Robert Love85b4aa42008-12-09 15:10:24 -08002226
Robert Love34f42a02009-02-27 10:55:45 -08002227/**
Robert Love5e4f8fe2010-05-07 15:18:35 -07002228 * fcoe_link_ok() - Check if the link is OK for a local port
2229 * @lport: The local port to check link on
2230 *
2231 * Returns: 0 if link is UP and OK, -1 if not
2232 *
2233 */
Bart Van Assche7c9c6842012-01-13 17:26:25 -08002234static int fcoe_link_ok(struct fc_lport *lport)
Robert Love5e4f8fe2010-05-07 15:18:35 -07002235{
Bhanu Prakash Gollapudi8597ae82011-01-28 16:05:37 -08002236 struct net_device *netdev = fcoe_netdev(lport);
Robert Love5e4f8fe2010-05-07 15:18:35 -07002237
2238 if (netif_oper_up(netdev))
2239 return 0;
2240 return -1;
2241}
2242
2243/**
Robert Love1875f272009-11-03 11:47:50 -08002244 * fcoe_percpu_clean() - Clear all pending skbs for an local port
2245 * @lport: The local port whose skbs are to be cleared
Joe Eykholte7a51992009-08-25 14:04:08 -07002246 *
2247 * Must be called with fcoe_create_mutex held to single-thread completion.
2248 *
2249 * This flushes the pending skbs by adding a new skb to each queue and
2250 * waiting until they are all freed. This assures us that not only are
2251 * there no packets that will be handled by the lport, but also that any
2252 * threads already handling packet have returned.
Robert Love85b4aa42008-12-09 15:10:24 -08002253 */
Bart Van Assche7c9c6842012-01-13 17:26:25 -08002254static void fcoe_percpu_clean(struct fc_lport *lport)
Robert Love85b4aa42008-12-09 15:10:24 -08002255{
Robert Love85b4aa42008-12-09 15:10:24 -08002256 struct fcoe_percpu_s *pp;
2257 struct fcoe_rcv_info *fr;
2258 struct sk_buff_head *list;
2259 struct sk_buff *skb, *next;
2260 struct sk_buff *head;
Robert Love5e5e92d2009-03-17 11:41:35 -07002261 unsigned int cpu;
Robert Love85b4aa42008-12-09 15:10:24 -08002262
Robert Love5e5e92d2009-03-17 11:41:35 -07002263 for_each_possible_cpu(cpu) {
2264 pp = &per_cpu(fcoe_percpu, cpu);
2265 spin_lock_bh(&pp->fcoe_rx_list.lock);
2266 list = &pp->fcoe_rx_list;
2267 head = list->next;
2268 for (skb = head; skb != (struct sk_buff *)list;
2269 skb = next) {
2270 next = skb->next;
2271 fr = fcoe_dev_from_skb(skb);
Robert Love1875f272009-11-03 11:47:50 -08002272 if (fr->fr_dev == lport) {
Robert Love5e5e92d2009-03-17 11:41:35 -07002273 __skb_unlink(skb, list);
2274 kfree_skb(skb);
Robert Love85b4aa42008-12-09 15:10:24 -08002275 }
Robert Love85b4aa42008-12-09 15:10:24 -08002276 }
Joe Eykholte7a51992009-08-25 14:04:08 -07002277
2278 if (!pp->thread || !cpu_online(cpu)) {
2279 spin_unlock_bh(&pp->fcoe_rx_list.lock);
2280 continue;
2281 }
2282
2283 skb = dev_alloc_skb(0);
2284 if (!skb) {
2285 spin_unlock_bh(&pp->fcoe_rx_list.lock);
2286 continue;
2287 }
2288 skb->destructor = fcoe_percpu_flush_done;
2289
2290 __skb_queue_tail(&pp->fcoe_rx_list, skb);
2291 if (pp->fcoe_rx_list.qlen == 1)
2292 wake_up_process(pp->thread);
Robert Love5e5e92d2009-03-17 11:41:35 -07002293 spin_unlock_bh(&pp->fcoe_rx_list.lock);
Joe Eykholte7a51992009-08-25 14:04:08 -07002294
2295 wait_for_completion(&fcoe_flush_completion);
Robert Love85b4aa42008-12-09 15:10:24 -08002296 }
2297}
Robert Love85b4aa42008-12-09 15:10:24 -08002298
2299/**
Robert Love1875f272009-11-03 11:47:50 -08002300 * fcoe_reset() - Reset a local port
2301 * @shost: The SCSI host associated with the local port to be reset
Robert Love85b4aa42008-12-09 15:10:24 -08002302 *
Robert Love1875f272009-11-03 11:47:50 -08002303 * Returns: Always 0 (return value required by FC transport template)
Robert Love85b4aa42008-12-09 15:10:24 -08002304 */
Bart Van Assche7c9c6842012-01-13 17:26:25 -08002305static int fcoe_reset(struct Scsi_Host *shost)
Robert Love85b4aa42008-12-09 15:10:24 -08002306{
2307 struct fc_lport *lport = shost_priv(shost);
Vasu Devd2f80952011-02-25 15:03:28 -08002308 struct fcoe_port *port = lport_priv(lport);
2309 struct fcoe_interface *fcoe = port->priv;
2310
2311 fcoe_ctlr_link_down(&fcoe->ctlr);
2312 fcoe_clean_pending_queue(fcoe->ctlr.lp);
2313 if (!fcoe_link_ok(fcoe->ctlr.lp))
2314 fcoe_ctlr_link_up(&fcoe->ctlr);
Robert Love85b4aa42008-12-09 15:10:24 -08002315 return 0;
2316}
Robert Love85b4aa42008-12-09 15:10:24 -08002317
Robert Love34f42a02009-02-27 10:55:45 -08002318/**
Robert Love1875f272009-11-03 11:47:50 -08002319 * fcoe_hostlist_lookup_port() - Find the FCoE interface associated with a net device
2320 * @netdev: The net device used as a key
Robert Love85b4aa42008-12-09 15:10:24 -08002321 *
Robert Love1875f272009-11-03 11:47:50 -08002322 * Locking: Must be called with the RNL mutex held.
2323 *
2324 * Returns: NULL or the FCoE interface
Robert Love85b4aa42008-12-09 15:10:24 -08002325 */
Chris Leech014f5c32009-08-25 13:59:30 -07002326static struct fcoe_interface *
Robert Love1875f272009-11-03 11:47:50 -08002327fcoe_hostlist_lookup_port(const struct net_device *netdev)
Robert Love85b4aa42008-12-09 15:10:24 -08002328{
Chris Leech014f5c32009-08-25 13:59:30 -07002329 struct fcoe_interface *fcoe;
Robert Love85b4aa42008-12-09 15:10:24 -08002330
Chris Leech014f5c32009-08-25 13:59:30 -07002331 list_for_each_entry(fcoe, &fcoe_hostlist, list) {
Robert Love1875f272009-11-03 11:47:50 -08002332 if (fcoe->netdev == netdev)
Chris Leech014f5c32009-08-25 13:59:30 -07002333 return fcoe;
Robert Love85b4aa42008-12-09 15:10:24 -08002334 }
Robert Love85b4aa42008-12-09 15:10:24 -08002335 return NULL;
2336}
2337
Robert Love34f42a02009-02-27 10:55:45 -08002338/**
Robert Love1875f272009-11-03 11:47:50 -08002339 * fcoe_hostlist_lookup() - Find the local port associated with a
2340 * given net device
2341 * @netdev: The netdevice used as a key
Robert Love85b4aa42008-12-09 15:10:24 -08002342 *
Robert Love1875f272009-11-03 11:47:50 -08002343 * Locking: Must be called with the RTNL mutex held
2344 *
2345 * Returns: NULL or the local port
Robert Love85b4aa42008-12-09 15:10:24 -08002346 */
Chris Leech090eb6c2009-08-25 14:00:28 -07002347static struct fc_lport *fcoe_hostlist_lookup(const struct net_device *netdev)
Robert Love85b4aa42008-12-09 15:10:24 -08002348{
Chris Leech014f5c32009-08-25 13:59:30 -07002349 struct fcoe_interface *fcoe;
Robert Love85b4aa42008-12-09 15:10:24 -08002350
Chris Leech014f5c32009-08-25 13:59:30 -07002351 fcoe = fcoe_hostlist_lookup_port(netdev);
Chris Leech3fe9a0b2009-08-25 13:59:46 -07002352 return (fcoe) ? fcoe->ctlr.lp : NULL;
Robert Love85b4aa42008-12-09 15:10:24 -08002353}
Robert Love85b4aa42008-12-09 15:10:24 -08002354
Robert Love34f42a02009-02-27 10:55:45 -08002355/**
Robert Love1875f272009-11-03 11:47:50 -08002356 * fcoe_hostlist_add() - Add the FCoE interface identified by a local
2357 * port to the hostlist
2358 * @lport: The local port that identifies the FCoE interface to be added
2359 *
2360 * Locking: must be called with the RTNL mutex held
Robert Love85b4aa42008-12-09 15:10:24 -08002361 *
2362 * Returns: 0 for success
2363 */
Chris Leech090eb6c2009-08-25 14:00:28 -07002364static int fcoe_hostlist_add(const struct fc_lport *lport)
Robert Love85b4aa42008-12-09 15:10:24 -08002365{
Chris Leech014f5c32009-08-25 13:59:30 -07002366 struct fcoe_interface *fcoe;
2367 struct fcoe_port *port;
Robert Love85b4aa42008-12-09 15:10:24 -08002368
Chris Leech014f5c32009-08-25 13:59:30 -07002369 fcoe = fcoe_hostlist_lookup_port(fcoe_netdev(lport));
2370 if (!fcoe) {
2371 port = lport_priv(lport);
Bhanu Prakash Gollapudi8597ae82011-01-28 16:05:37 -08002372 fcoe = port->priv;
Chris Leech014f5c32009-08-25 13:59:30 -07002373 list_add_tail(&fcoe->list, &fcoe_hostlist);
Robert Love85b4aa42008-12-09 15:10:24 -08002374 }
2375 return 0;
2376}
Robert Love85b4aa42008-12-09 15:10:24 -08002377
Yi Zou78a58242011-01-28 16:05:16 -08002378
2379static struct fcoe_transport fcoe_sw_transport = {
2380 .name = {FCOE_TRANSPORT_DEFAULT},
2381 .attached = false,
2382 .list = LIST_HEAD_INIT(fcoe_sw_transport.list),
2383 .match = fcoe_match,
2384 .create = fcoe_create,
2385 .destroy = fcoe_destroy,
2386 .enable = fcoe_enable,
2387 .disable = fcoe_disable,
2388};
2389
Robert Love34f42a02009-02-27 10:55:45 -08002390/**
Robert Love1875f272009-11-03 11:47:50 -08002391 * fcoe_init() - Initialize fcoe.ko
Robert Love85b4aa42008-12-09 15:10:24 -08002392 *
Robert Love1875f272009-11-03 11:47:50 -08002393 * Returns: 0 on success, or a negative value on failure
Robert Love34f42a02009-02-27 10:55:45 -08002394 */
Robert Love85b4aa42008-12-09 15:10:24 -08002395static int __init fcoe_init(void)
2396{
Robert Love1875f272009-11-03 11:47:50 -08002397 struct fcoe_percpu_s *p;
Robert Love38eccab2009-03-17 11:41:30 -07002398 unsigned int cpu;
Robert Love8976f422009-03-17 11:41:46 -07002399 int rc = 0;
Robert Love85b4aa42008-12-09 15:10:24 -08002400
Tejun Heo2ca32b42011-01-28 16:05:32 -08002401 fcoe_wq = alloc_workqueue("fcoe", 0, 0);
2402 if (!fcoe_wq)
2403 return -ENOMEM;
2404
Yi Zou78a58242011-01-28 16:05:16 -08002405 /* register as a fcoe transport */
2406 rc = fcoe_transport_attach(&fcoe_sw_transport);
2407 if (rc) {
2408 printk(KERN_ERR "failed to register an fcoe transport, check "
2409 "if libfcoe is loaded\n");
2410 return rc;
2411 }
2412
Chris Leechdfc1d0f2009-08-25 14:00:13 -07002413 mutex_lock(&fcoe_config_mutex);
2414
Robert Love38eccab2009-03-17 11:41:30 -07002415 for_each_possible_cpu(cpu) {
Robert Love5e5e92d2009-03-17 11:41:35 -07002416 p = &per_cpu(fcoe_percpu, cpu);
Robert Love38eccab2009-03-17 11:41:30 -07002417 skb_queue_head_init(&p->fcoe_rx_list);
2418 }
2419
Robert Love8976f422009-03-17 11:41:46 -07002420 for_each_online_cpu(cpu)
2421 fcoe_percpu_thread_create(cpu);
Robert Love85b4aa42008-12-09 15:10:24 -08002422
Robert Love8976f422009-03-17 11:41:46 -07002423 /* Initialize per CPU interrupt thread */
2424 rc = register_hotcpu_notifier(&fcoe_cpu_notifier);
2425 if (rc)
2426 goto out_free;
Robert Love85b4aa42008-12-09 15:10:24 -08002427
Robert Love8976f422009-03-17 11:41:46 -07002428 /* Setup link change notification */
Robert Love85b4aa42008-12-09 15:10:24 -08002429 fcoe_dev_setup();
2430
Chris Leech5892c322009-08-25 13:59:14 -07002431 rc = fcoe_if_init();
2432 if (rc)
2433 goto out_free;
Robert Love85b4aa42008-12-09 15:10:24 -08002434
Chris Leechdfc1d0f2009-08-25 14:00:13 -07002435 mutex_unlock(&fcoe_config_mutex);
Robert Love85b4aa42008-12-09 15:10:24 -08002436 return 0;
Robert Love8976f422009-03-17 11:41:46 -07002437
2438out_free:
2439 for_each_online_cpu(cpu) {
2440 fcoe_percpu_thread_destroy(cpu);
2441 }
Chris Leechdfc1d0f2009-08-25 14:00:13 -07002442 mutex_unlock(&fcoe_config_mutex);
Tejun Heo2ca32b42011-01-28 16:05:32 -08002443 destroy_workqueue(fcoe_wq);
Robert Love8976f422009-03-17 11:41:46 -07002444 return rc;
Robert Love85b4aa42008-12-09 15:10:24 -08002445}
2446module_init(fcoe_init);
2447
2448/**
Robert Love1875f272009-11-03 11:47:50 -08002449 * fcoe_exit() - Clean up fcoe.ko
Robert Love85b4aa42008-12-09 15:10:24 -08002450 *
Robert Love1875f272009-11-03 11:47:50 -08002451 * Returns: 0 on success or a negative value on failure
Robert Love34f42a02009-02-27 10:55:45 -08002452 */
Robert Love85b4aa42008-12-09 15:10:24 -08002453static void __exit fcoe_exit(void)
2454{
Chris Leech014f5c32009-08-25 13:59:30 -07002455 struct fcoe_interface *fcoe, *tmp;
Chris Leech2e70e242009-08-25 14:00:23 -07002456 struct fcoe_port *port;
Robert Love1875f272009-11-03 11:47:50 -08002457 unsigned int cpu;
Robert Love85b4aa42008-12-09 15:10:24 -08002458
Chris Leechdfc1d0f2009-08-25 14:00:13 -07002459 mutex_lock(&fcoe_config_mutex);
2460
Robert Love85b4aa42008-12-09 15:10:24 -08002461 fcoe_dev_cleanup();
2462
Vasu Dev5919a592009-03-27 09:03:29 -07002463 /* releases the associated fcoe hosts */
Chris Leech090eb6c2009-08-25 14:00:28 -07002464 rtnl_lock();
2465 list_for_each_entry_safe(fcoe, tmp, &fcoe_hostlist, list) {
Chris Leechc863df32009-08-25 14:00:18 -07002466 list_del(&fcoe->list);
Chris Leech2e70e242009-08-25 14:00:23 -07002467 port = lport_priv(fcoe->ctlr.lp);
Tejun Heo2ca32b42011-01-28 16:05:32 -08002468 queue_work(fcoe_wq, &port->destroy_work);
Chris Leechc863df32009-08-25 14:00:18 -07002469 }
Chris Leech090eb6c2009-08-25 14:00:28 -07002470 rtnl_unlock();
Robert Love85b4aa42008-12-09 15:10:24 -08002471
Robert Love8976f422009-03-17 11:41:46 -07002472 unregister_hotcpu_notifier(&fcoe_cpu_notifier);
2473
Chris Leech014f5c32009-08-25 13:59:30 -07002474 for_each_online_cpu(cpu)
Robert Love8976f422009-03-17 11:41:46 -07002475 fcoe_percpu_thread_destroy(cpu);
Robert Love85b4aa42008-12-09 15:10:24 -08002476
Chris Leechdfc1d0f2009-08-25 14:00:13 -07002477 mutex_unlock(&fcoe_config_mutex);
Chris Leech2e70e242009-08-25 14:00:23 -07002478
Tejun Heo2ca32b42011-01-28 16:05:32 -08002479 /*
2480 * destroy_work's may be chained but destroy_workqueue()
2481 * can take care of them. Just kill the fcoe_wq.
2482 */
2483 destroy_workqueue(fcoe_wq);
Chris Leech2e70e242009-08-25 14:00:23 -07002484
Tejun Heo2ca32b42011-01-28 16:05:32 -08002485 /*
2486 * Detaching from the scsi transport must happen after all
2487 * destroys are done on the fcoe_wq. destroy_workqueue will
2488 * enusre the fcoe_wq is flushed.
2489 */
Chris Leech2e70e242009-08-25 14:00:23 -07002490 fcoe_if_exit();
Yi Zou78a58242011-01-28 16:05:16 -08002491
2492 /* detach from fcoe transport */
2493 fcoe_transport_detach(&fcoe_sw_transport);
Robert Love85b4aa42008-12-09 15:10:24 -08002494}
2495module_exit(fcoe_exit);
Chris Leech11b56182009-11-03 11:46:29 -08002496
2497/**
2498 * fcoe_flogi_resp() - FCoE specific FLOGI and FDISC response handler
2499 * @seq: active sequence in the FLOGI or FDISC exchange
2500 * @fp: response frame, or error encoded in a pointer (timeout)
2501 * @arg: pointer the the fcoe_ctlr structure
2502 *
Uwe Kleine-König65155b32010-06-11 12:17:01 +02002503 * This handles MAC address management for FCoE, then passes control on to
Chris Leech11b56182009-11-03 11:46:29 -08002504 * the libfc FLOGI response handler.
2505 */
2506static void fcoe_flogi_resp(struct fc_seq *seq, struct fc_frame *fp, void *arg)
2507{
2508 struct fcoe_ctlr *fip = arg;
2509 struct fc_exch *exch = fc_seq_exch(seq);
2510 struct fc_lport *lport = exch->lp;
2511 u8 *mac;
2512
2513 if (IS_ERR(fp))
2514 goto done;
2515
2516 mac = fr_cb(fp)->granted_mac;
Vasu Dev907c07d2011-10-28 11:34:23 -07002517 /* pre-FIP */
2518 if (is_zero_ether_addr(mac))
2519 fcoe_ctlr_recv_flogi(fip, lport, fp);
2520 if (!is_zero_ether_addr(mac))
2521 fcoe_update_src_mac(lport, mac);
Chris Leech11b56182009-11-03 11:46:29 -08002522done:
2523 fc_lport_flogi_resp(seq, fp, lport);
2524}
2525
2526/**
2527 * fcoe_logo_resp() - FCoE specific LOGO response handler
2528 * @seq: active sequence in the LOGO exchange
2529 * @fp: response frame, or error encoded in a pointer (timeout)
2530 * @arg: pointer the the fcoe_ctlr structure
2531 *
Uwe Kleine-König65155b32010-06-11 12:17:01 +02002532 * This handles MAC address management for FCoE, then passes control on to
Chris Leech11b56182009-11-03 11:46:29 -08002533 * the libfc LOGO response handler.
2534 */
2535static void fcoe_logo_resp(struct fc_seq *seq, struct fc_frame *fp, void *arg)
2536{
Joe Eykholt386309ce2009-11-03 11:49:16 -08002537 struct fc_lport *lport = arg;
Chris Leech11b56182009-11-03 11:46:29 -08002538 static u8 zero_mac[ETH_ALEN] = { 0 };
2539
2540 if (!IS_ERR(fp))
Joe Eykholt386309ce2009-11-03 11:49:16 -08002541 fcoe_update_src_mac(lport, zero_mac);
Chris Leech11b56182009-11-03 11:46:29 -08002542 fc_lport_logo_resp(seq, fp, lport);
2543}
2544
2545/**
2546 * fcoe_elsct_send - FCoE specific ELS handler
2547 *
2548 * This does special case handling of FIP encapsualted ELS exchanges for FCoE,
2549 * using FCoE specific response handlers and passing the FIP controller as
2550 * the argument (the lport is still available from the exchange).
2551 *
2552 * Most of the work here is just handed off to the libfc routine.
2553 */
Robert Love1875f272009-11-03 11:47:50 -08002554static struct fc_seq *fcoe_elsct_send(struct fc_lport *lport, u32 did,
2555 struct fc_frame *fp, unsigned int op,
2556 void (*resp)(struct fc_seq *,
2557 struct fc_frame *,
2558 void *),
2559 void *arg, u32 timeout)
Chris Leech11b56182009-11-03 11:46:29 -08002560{
2561 struct fcoe_port *port = lport_priv(lport);
Bhanu Prakash Gollapudi8597ae82011-01-28 16:05:37 -08002562 struct fcoe_interface *fcoe = port->priv;
Chris Leech11b56182009-11-03 11:46:29 -08002563 struct fcoe_ctlr *fip = &fcoe->ctlr;
2564 struct fc_frame_header *fh = fc_frame_header_get(fp);
2565
2566 switch (op) {
2567 case ELS_FLOGI:
2568 case ELS_FDISC:
Joe Eykholte10f8c62010-07-20 15:20:30 -07002569 if (lport->point_to_multipoint)
2570 break;
Chris Leech11b56182009-11-03 11:46:29 -08002571 return fc_elsct_send(lport, did, fp, op, fcoe_flogi_resp,
2572 fip, timeout);
2573 case ELS_LOGO:
2574 /* only hook onto fabric logouts, not port logouts */
2575 if (ntoh24(fh->fh_d_id) != FC_FID_FLOGI)
2576 break;
2577 return fc_elsct_send(lport, did, fp, op, fcoe_logo_resp,
Joe Eykholt386309ce2009-11-03 11:49:16 -08002578 lport, timeout);
Chris Leech11b56182009-11-03 11:46:29 -08002579 }
2580 return fc_elsct_send(lport, did, fp, op, resp, arg, timeout);
2581}
2582
Chris Leech9a057532009-11-03 11:46:40 -08002583/**
2584 * fcoe_vport_create() - create an fc_host/scsi_host for a vport
2585 * @vport: fc_vport object to create a new fc_host for
2586 * @disabled: start the new fc_host in a disabled state by default?
2587 *
2588 * Returns: 0 for success
2589 */
2590static int fcoe_vport_create(struct fc_vport *vport, bool disabled)
2591{
2592 struct Scsi_Host *shost = vport_to_shost(vport);
2593 struct fc_lport *n_port = shost_priv(shost);
2594 struct fcoe_port *port = lport_priv(n_port);
Bhanu Prakash Gollapudi8597ae82011-01-28 16:05:37 -08002595 struct fcoe_interface *fcoe = port->priv;
Chris Leech9a057532009-11-03 11:46:40 -08002596 struct net_device *netdev = fcoe->netdev;
2597 struct fc_lport *vn_port;
Neerav Parikhbdf25212011-05-16 16:45:29 -07002598 int rc;
2599 char buf[32];
2600
2601 rc = fcoe_validate_vport_create(vport);
2602 if (rc) {
Bhanu Prakash Gollapudid8348952011-08-04 17:38:49 -07002603 fcoe_wwn_to_str(vport->port_name, buf, sizeof(buf));
Neerav Parikhbdf25212011-05-16 16:45:29 -07002604 printk(KERN_ERR "fcoe: Failed to create vport, "
2605 "WWPN (0x%s) already exists\n",
2606 buf);
2607 return rc;
2608 }
Chris Leech9a057532009-11-03 11:46:40 -08002609
2610 mutex_lock(&fcoe_config_mutex);
Jiri Pirko4bc71cb2011-09-03 03:34:30 +00002611 rtnl_lock();
Chris Leech9a057532009-11-03 11:46:40 -08002612 vn_port = fcoe_if_create(fcoe, &vport->dev, 1);
Jiri Pirko4bc71cb2011-09-03 03:34:30 +00002613 rtnl_unlock();
Chris Leech9a057532009-11-03 11:46:40 -08002614 mutex_unlock(&fcoe_config_mutex);
2615
2616 if (IS_ERR(vn_port)) {
2617 printk(KERN_ERR "fcoe: fcoe_vport_create(%s) failed\n",
2618 netdev->name);
2619 return -EIO;
2620 }
2621
2622 if (disabled) {
2623 fc_vport_set_state(vport, FC_VPORT_DISABLED);
2624 } else {
2625 vn_port->boot_time = jiffies;
2626 fc_fabric_login(vn_port);
2627 fc_vport_setlink(vn_port);
2628 }
2629 return 0;
2630}
2631
2632/**
2633 * fcoe_vport_destroy() - destroy the fc_host/scsi_host for a vport
2634 * @vport: fc_vport object that is being destroyed
2635 *
2636 * Returns: 0 for success
2637 */
2638static int fcoe_vport_destroy(struct fc_vport *vport)
2639{
2640 struct Scsi_Host *shost = vport_to_shost(vport);
2641 struct fc_lport *n_port = shost_priv(shost);
2642 struct fc_lport *vn_port = vport->dd_data;
Chris Leech9a057532009-11-03 11:46:40 -08002643
2644 mutex_lock(&n_port->lp_mutex);
2645 list_del(&vn_port->list);
2646 mutex_unlock(&n_port->lp_mutex);
Robert Loveccefd232012-02-10 17:18:41 -08002647
2648 mutex_lock(&fcoe_config_mutex);
2649 fcoe_if_destroy(vn_port);
2650 mutex_unlock(&fcoe_config_mutex);
2651
Chris Leech9a057532009-11-03 11:46:40 -08002652 return 0;
2653}
2654
2655/**
2656 * fcoe_vport_disable() - change vport state
2657 * @vport: vport to bring online/offline
2658 * @disable: should the vport be disabled?
2659 */
2660static int fcoe_vport_disable(struct fc_vport *vport, bool disable)
2661{
2662 struct fc_lport *lport = vport->dd_data;
2663
2664 if (disable) {
2665 fc_vport_set_state(vport, FC_VPORT_DISABLED);
2666 fc_fabric_logoff(lport);
2667 } else {
2668 lport->boot_time = jiffies;
2669 fc_fabric_login(lport);
2670 fc_vport_setlink(lport);
2671 }
2672
2673 return 0;
2674}
2675
Chris Leechdc8596d2009-11-03 11:47:18 -08002676/**
2677 * fcoe_vport_set_symbolic_name() - append vport string to symbolic name
2678 * @vport: fc_vport with a new symbolic name string
2679 *
2680 * After generating a new symbolic name string, a new RSPN_ID request is
2681 * sent to the name server. There is no response handler, so if it fails
2682 * for some reason it will not be retried.
2683 */
2684static void fcoe_set_vport_symbolic_name(struct fc_vport *vport)
2685{
2686 struct fc_lport *lport = vport->dd_data;
2687 struct fc_frame *fp;
2688 size_t len;
2689
2690 snprintf(fc_host_symbolic_name(lport->host), FC_SYMBOLIC_NAME_SIZE,
2691 "%s v%s over %s : %s", FCOE_NAME, FCOE_VERSION,
2692 fcoe_netdev(lport)->name, vport->symbolic_name);
2693
2694 if (lport->state != LPORT_ST_READY)
2695 return;
2696
2697 len = strnlen(fc_host_symbolic_name(lport->host), 255);
2698 fp = fc_frame_alloc(lport,
2699 sizeof(struct fc_ct_hdr) +
2700 sizeof(struct fc_ns_rspn) + len);
2701 if (!fp)
2702 return;
2703 lport->tt.elsct_send(lport, FC_FID_DIR_SERV, fp, FC_NS_RSPN_ID,
Joe Eykholtb94f8952009-11-03 11:50:21 -08002704 NULL, NULL, 3 * lport->r_a_tov);
Chris Leechdc8596d2009-11-03 11:47:18 -08002705}
Yi Zoub84056b2009-11-20 14:55:19 -08002706
2707/**
2708 * fcoe_get_lesb() - Fill the FCoE Link Error Status Block
2709 * @lport: the local port
2710 * @fc_lesb: the link error status block
2711 */
2712static void fcoe_get_lesb(struct fc_lport *lport,
2713 struct fc_els_lesb *fc_lesb)
2714{
Yi Zoub84056b2009-11-20 14:55:19 -08002715 struct net_device *netdev = fcoe_netdev(lport);
2716
Bhanu Prakash Gollapudi814740d2011-10-03 16:45:01 -07002717 __fcoe_get_lesb(lport, fc_lesb, netdev);
Yi Zoub84056b2009-11-20 14:55:19 -08002718}
Joe Eykholt7d65b0d2010-03-12 16:08:02 -08002719
2720/**
2721 * fcoe_set_port_id() - Callback from libfc when Port_ID is set.
2722 * @lport: the local port
2723 * @port_id: the port ID
2724 * @fp: the received frame, if any, that caused the port_id to be set.
2725 *
2726 * This routine handles the case where we received a FLOGI and are
2727 * entering point-to-point mode. We need to call fcoe_ctlr_recv_flogi()
2728 * so it can set the non-mapped mode and gateway address.
2729 *
2730 * The FLOGI LS_ACC is handled by fcoe_flogi_resp().
2731 */
2732static void fcoe_set_port_id(struct fc_lport *lport,
2733 u32 port_id, struct fc_frame *fp)
2734{
2735 struct fcoe_port *port = lport_priv(lport);
Bhanu Prakash Gollapudi8597ae82011-01-28 16:05:37 -08002736 struct fcoe_interface *fcoe = port->priv;
Joe Eykholt7d65b0d2010-03-12 16:08:02 -08002737
2738 if (fp && fc_frame_payload_op(fp) == ELS_FLOGI)
2739 fcoe_ctlr_recv_flogi(&fcoe->ctlr, lport, fp);
2740}