blob: 5126685ab982700d437e0fa2bd4981f56b7a3172 [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 Leech030f4e02009-08-25 14:00:02 -0700387 kref_init(&fcoe->kref);
Chris Leech54b649f2009-08-25 14:00:07 -0700388
389 /*
390 * Initialize FIP.
391 */
Joe Eykholt1dd454d2010-07-20 15:20:46 -0700392 fcoe_ctlr_init(&fcoe->ctlr, fip_mode);
Chris Leech54b649f2009-08-25 14:00:07 -0700393 fcoe->ctlr.send = fcoe_fip_send;
394 fcoe->ctlr.update_mac = fcoe_update_src_mac;
Chris Leech11b56182009-11-03 11:46:29 -0800395 fcoe->ctlr.get_src_addr = fcoe_get_src_mac;
Chris Leech54b649f2009-08-25 14:00:07 -0700396
john fastabend59d92512009-11-03 11:48:44 -0800397 err = fcoe_interface_setup(fcoe, netdev);
398 if (err) {
399 fcoe_ctlr_destroy(&fcoe->ctlr);
400 kfree(fcoe);
401 dev_put(netdev);
Robert Love7287fb92011-01-28 16:03:47 -0800402 fcoe = ERR_PTR(err);
Robert Love6f687942012-02-10 17:18:36 -0800403 goto out_putmod;
john fastabend59d92512009-11-03 11:48:44 -0800404 }
Chris Leech030f4e02009-08-25 14:00:02 -0700405
Robert Love7287fb92011-01-28 16:03:47 -0800406 goto out;
407
Robert Love6f687942012-02-10 17:18:36 -0800408out_putmod:
Robert Love7287fb92011-01-28 16:03:47 -0800409 module_put(THIS_MODULE);
410out:
Chris Leech030f4e02009-08-25 14:00:02 -0700411 return fcoe;
412}
413
414/**
415 * fcoe_interface_release() - fcoe_port kref release function
Robert Love1875f272009-11-03 11:47:50 -0800416 * @kref: Embedded reference count in an fcoe_interface struct
Chris Leech030f4e02009-08-25 14:00:02 -0700417 */
418static void fcoe_interface_release(struct kref *kref)
419{
420 struct fcoe_interface *fcoe;
Chris Leech2e70e242009-08-25 14:00:23 -0700421 struct net_device *netdev;
Chris Leech030f4e02009-08-25 14:00:02 -0700422
423 fcoe = container_of(kref, struct fcoe_interface, kref);
Chris Leech2e70e242009-08-25 14:00:23 -0700424 netdev = fcoe->netdev;
425 /* tear-down the FCoE controller */
426 fcoe_ctlr_destroy(&fcoe->ctlr);
Chris Leech030f4e02009-08-25 14:00:02 -0700427 kfree(fcoe);
Chris Leech2e70e242009-08-25 14:00:23 -0700428 dev_put(netdev);
Robert Love7287fb92011-01-28 16:03:47 -0800429 module_put(THIS_MODULE);
Chris Leech030f4e02009-08-25 14:00:02 -0700430}
431
432/**
Robert Love1875f272009-11-03 11:47:50 -0800433 * fcoe_interface_get() - Get a reference to a FCoE interface
434 * @fcoe: The FCoE interface to be held
Chris Leech030f4e02009-08-25 14:00:02 -0700435 */
436static inline void fcoe_interface_get(struct fcoe_interface *fcoe)
437{
438 kref_get(&fcoe->kref);
439}
440
441/**
Robert Love1875f272009-11-03 11:47:50 -0800442 * fcoe_interface_put() - Put a reference to a FCoE interface
443 * @fcoe: The FCoE interface to be released
Chris Leech030f4e02009-08-25 14:00:02 -0700444 */
445static inline void fcoe_interface_put(struct fcoe_interface *fcoe)
446{
447 kref_put(&fcoe->kref, fcoe_interface_release);
448}
449
450/**
Vasu Devf04ca1b2011-04-01 16:06:45 -0700451 * fcoe_interface_cleanup() - Clean up a FCoE interface
452 * @fcoe: The FCoE interface to be cleaned up
453 *
454 * Caller must be holding the RTNL mutex
455 */
Bart Van Assche7c9c6842012-01-13 17:26:25 -0800456static void fcoe_interface_cleanup(struct fcoe_interface *fcoe)
Vasu Devf04ca1b2011-04-01 16:06:45 -0700457{
458 struct net_device *netdev = fcoe->netdev;
459 struct fcoe_ctlr *fip = &fcoe->ctlr;
460 u8 flogi_maddr[ETH_ALEN];
461 const struct net_device_ops *ops;
Vasu Devf04ca1b2011-04-01 16:06:45 -0700462
Robert Love848e7d52011-08-25 12:40:47 -0700463 rtnl_lock();
464
Vasu Devf04ca1b2011-04-01 16:06:45 -0700465 /*
466 * Don't listen for Ethernet packets anymore.
467 * synchronize_net() ensures that the packet handlers are not running
468 * on another CPU. dev_remove_pack() would do that, this calls the
469 * unsyncronized version __dev_remove_pack() to avoid multiple delays.
470 */
471 __dev_remove_pack(&fcoe->fcoe_packet_type);
472 __dev_remove_pack(&fcoe->fip_packet_type);
473 synchronize_net();
474
475 /* Delete secondary MAC addresses */
476 memcpy(flogi_maddr, (u8[6]) FC_FCOE_FLOGI_MAC, ETH_ALEN);
477 dev_uc_del(netdev, flogi_maddr);
478 if (fip->spma)
479 dev_uc_del(netdev, fip->ctl_src_addr);
480 if (fip->mode == FIP_MODE_VN2VN) {
481 dev_mc_del(netdev, FIP_ALL_VN2VN_MACS);
482 dev_mc_del(netdev, FIP_ALL_P2P_MACS);
483 } else
484 dev_mc_del(netdev, FIP_ALL_ENODE_MACS);
485
Vasu Devf04ca1b2011-04-01 16:06:45 -0700486 /* Tell the LLD we are done w/ FCoE */
487 ops = netdev->netdev_ops;
488 if (ops->ndo_fcoe_disable) {
489 if (ops->ndo_fcoe_disable(netdev))
490 FCOE_NETDEV_DBG(netdev, "Failed to disable FCoE"
491 " specific feature for LLD.\n");
492 }
Neerav Parikhb2085a42011-06-20 16:59:51 -0700493
Robert Love848e7d52011-08-25 12:40:47 -0700494 rtnl_unlock();
495
Neerav Parikhb2085a42011-06-20 16:59:51 -0700496 /* Release the self-reference taken during fcoe_interface_create() */
Vasu Devf04ca1b2011-04-01 16:06:45 -0700497 fcoe_interface_put(fcoe);
498}
499
500/**
Robert Love1875f272009-11-03 11:47:50 -0800501 * fcoe_fip_recv() - Handler for received FIP frames
502 * @skb: The receive skb
503 * @netdev: The associated net device
504 * @ptype: The packet_type structure which was used to register this handler
505 * @orig_dev: The original net_device the the skb was received on.
506 * (in case dev is a bond)
Vasu Devab6b85c2009-05-17 12:33:08 +0000507 *
508 * Returns: 0 for success
509 */
Robert Love1875f272009-11-03 11:47:50 -0800510static int fcoe_fip_recv(struct sk_buff *skb, struct net_device *netdev,
Vasu Devab6b85c2009-05-17 12:33:08 +0000511 struct packet_type *ptype,
512 struct net_device *orig_dev)
513{
Chris Leech259ad852009-08-25 13:59:41 -0700514 struct fcoe_interface *fcoe;
Vasu Devab6b85c2009-05-17 12:33:08 +0000515
Chris Leech259ad852009-08-25 13:59:41 -0700516 fcoe = container_of(ptype, struct fcoe_interface, fip_packet_type);
Chris Leech3fe9a0b2009-08-25 13:59:46 -0700517 fcoe_ctlr_recv(&fcoe->ctlr, skb);
Vasu Devab6b85c2009-05-17 12:33:08 +0000518 return 0;
519}
520
521/**
Vasu Dev980f5152011-07-27 15:11:05 -0700522 * fcoe_port_send() - Send an Ethernet-encapsulated FIP/FCoE frame
523 * @port: The FCoE port
524 * @skb: The FIP/FCoE packet to be sent
525 */
526static void fcoe_port_send(struct fcoe_port *port, struct sk_buff *skb)
527{
528 if (port->fcoe_pending_queue.qlen)
529 fcoe_check_wait_queue(port->lport, skb);
530 else if (fcoe_start_io(skb))
531 fcoe_check_wait_queue(port->lport, skb);
532}
533
534/**
Robert Love1875f272009-11-03 11:47:50 -0800535 * fcoe_fip_send() - Send an Ethernet-encapsulated FIP frame
536 * @fip: The FCoE controller
537 * @skb: The FIP packet to be sent
Vasu Devab6b85c2009-05-17 12:33:08 +0000538 */
539static void fcoe_fip_send(struct fcoe_ctlr *fip, struct sk_buff *skb)
540{
Chris Leech3fe9a0b2009-08-25 13:59:46 -0700541 skb->dev = fcoe_from_ctlr(fip)->netdev;
Vasu Dev980f5152011-07-27 15:11:05 -0700542 fcoe_port_send(lport_priv(fip->lp), skb);
Vasu Devab6b85c2009-05-17 12:33:08 +0000543}
544
545/**
Robert Love1875f272009-11-03 11:47:50 -0800546 * fcoe_update_src_mac() - Update the Ethernet MAC filters
547 * @lport: The local port to update the source MAC on
548 * @addr: Unicast MAC address to add
Vasu Devab6b85c2009-05-17 12:33:08 +0000549 *
550 * Remove any previously-set unicast MAC filter.
551 * Add secondary FCoE MAC address filter for our OUI.
552 */
Chris Leech11b56182009-11-03 11:46:29 -0800553static void fcoe_update_src_mac(struct fc_lport *lport, u8 *addr)
Vasu Devab6b85c2009-05-17 12:33:08 +0000554{
Chris Leech11b56182009-11-03 11:46:29 -0800555 struct fcoe_port *port = lport_priv(lport);
Bhanu Prakash Gollapudi8597ae82011-01-28 16:05:37 -0800556 struct fcoe_interface *fcoe = port->priv;
Vasu Devab6b85c2009-05-17 12:33:08 +0000557
Vasu Devab6b85c2009-05-17 12:33:08 +0000558 rtnl_lock();
Chris Leech11b56182009-11-03 11:46:29 -0800559 if (!is_zero_ether_addr(port->data_src_addr))
Jiri Pirkoa748ee22010-04-01 21:22:09 +0000560 dev_uc_del(fcoe->netdev, port->data_src_addr);
Chris Leech11b56182009-11-03 11:46:29 -0800561 if (!is_zero_ether_addr(addr))
Jiri Pirkoa748ee22010-04-01 21:22:09 +0000562 dev_uc_add(fcoe->netdev, addr);
Chris Leech11b56182009-11-03 11:46:29 -0800563 memcpy(port->data_src_addr, addr, ETH_ALEN);
Vasu Devab6b85c2009-05-17 12:33:08 +0000564 rtnl_unlock();
565}
566
567/**
Chris Leech11b56182009-11-03 11:46:29 -0800568 * fcoe_get_src_mac() - return the Ethernet source address for an lport
569 * @lport: libfc lport
570 */
571static u8 *fcoe_get_src_mac(struct fc_lport *lport)
572{
573 struct fcoe_port *port = lport_priv(lport);
574
575 return port->data_src_addr;
576}
577
578/**
Robert Love1875f272009-11-03 11:47:50 -0800579 * fcoe_lport_config() - Set up a local port
580 * @lport: The local port to be setup
Vasu Dev7f349142009-03-27 09:06:31 -0700581 *
582 * Returns: 0 for success
583 */
Robert Love1875f272009-11-03 11:47:50 -0800584static int fcoe_lport_config(struct fc_lport *lport)
Vasu Dev7f349142009-03-27 09:06:31 -0700585{
Robert Love1875f272009-11-03 11:47:50 -0800586 lport->link_up = 0;
587 lport->qfull = 0;
588 lport->max_retry_count = 3;
589 lport->max_rport_retry_count = 3;
590 lport->e_d_tov = 2 * 1000; /* FC-FS default */
591 lport->r_a_tov = 2 * 2 * 1000;
592 lport->service_params = (FCP_SPPF_INIT_FCN | FCP_SPPF_RD_XRDY_DIS |
593 FCP_SPPF_RETRY | FCP_SPPF_CONF_COMPL);
594 lport->does_npiv = 1;
Vasu Dev7f349142009-03-27 09:06:31 -0700595
Robert Love1875f272009-11-03 11:47:50 -0800596 fc_lport_init_stats(lport);
Vasu Dev7f349142009-03-27 09:06:31 -0700597
598 /* lport fc_lport related configuration */
Robert Love1875f272009-11-03 11:47:50 -0800599 fc_lport_config(lport);
Vasu Dev7f349142009-03-27 09:06:31 -0700600
601 /* offload related configuration */
Robert Love1875f272009-11-03 11:47:50 -0800602 lport->crc_offload = 0;
603 lport->seq_offload = 0;
604 lport->lro_enabled = 0;
605 lport->lro_xid = 0;
606 lport->lso_max = 0;
Vasu Dev7f349142009-03-27 09:06:31 -0700607
608 return 0;
609}
610
611/**
Yi Zou54a5b212010-07-20 15:21:17 -0700612 * fcoe_netdev_features_change - Updates the lport's offload flags based
613 * on the LLD netdev's FCoE feature flags
614 */
615static void fcoe_netdev_features_change(struct fc_lport *lport,
616 struct net_device *netdev)
617{
618 mutex_lock(&lport->lp_mutex);
619
620 if (netdev->features & NETIF_F_SG)
621 lport->sg_supp = 1;
622 else
623 lport->sg_supp = 0;
624
625 if (netdev->features & NETIF_F_FCOE_CRC) {
626 lport->crc_offload = 1;
627 FCOE_NETDEV_DBG(netdev, "Supports FCCRC offload\n");
628 } else {
629 lport->crc_offload = 0;
630 }
631
632 if (netdev->features & NETIF_F_FSO) {
633 lport->seq_offload = 1;
634 lport->lso_max = netdev->gso_max_size;
635 FCOE_NETDEV_DBG(netdev, "Supports LSO for max len 0x%x\n",
636 lport->lso_max);
637 } else {
638 lport->seq_offload = 0;
639 lport->lso_max = 0;
640 }
641
642 if (netdev->fcoe_ddp_xid) {
643 lport->lro_enabled = 1;
644 lport->lro_xid = netdev->fcoe_ddp_xid;
645 FCOE_NETDEV_DBG(netdev, "Supports LRO for max xid 0x%x\n",
646 lport->lro_xid);
647 } else {
648 lport->lro_enabled = 0;
649 lport->lro_xid = 0;
650 }
651
652 mutex_unlock(&lport->lp_mutex);
653}
654
655/**
Robert Love1875f272009-11-03 11:47:50 -0800656 * fcoe_netdev_config() - Set up net devive for SW FCoE
657 * @lport: The local port that is associated with the net device
658 * @netdev: The associated net device
Vasu Dev7f349142009-03-27 09:06:31 -0700659 *
Robert Love1875f272009-11-03 11:47:50 -0800660 * Must be called after fcoe_lport_config() as it will use local port mutex
Vasu Dev7f349142009-03-27 09:06:31 -0700661 *
Robert Love1875f272009-11-03 11:47:50 -0800662 * Returns: 0 for success
Vasu Dev7f349142009-03-27 09:06:31 -0700663 */
Robert Love1875f272009-11-03 11:47:50 -0800664static int fcoe_netdev_config(struct fc_lport *lport, struct net_device *netdev)
Vasu Dev7f349142009-03-27 09:06:31 -0700665{
666 u32 mfs;
667 u64 wwnn, wwpn;
Chris Leech25024982009-08-25 13:59:35 -0700668 struct fcoe_interface *fcoe;
Chris Leech014f5c32009-08-25 13:59:30 -0700669 struct fcoe_port *port;
Vasu Dev7f349142009-03-27 09:06:31 -0700670
671 /* Setup lport private data to point to fcoe softc */
Robert Love1875f272009-11-03 11:47:50 -0800672 port = lport_priv(lport);
Bhanu Prakash Gollapudi8597ae82011-01-28 16:05:37 -0800673 fcoe = port->priv;
Vasu Dev7f349142009-03-27 09:06:31 -0700674
675 /*
676 * Determine max frame size based on underlying device and optional
677 * user-configured limit. If the MFS is too low, fcoe_link_ok()
678 * will return 0, so do this first.
679 */
Yi Zou7221d7e2009-10-21 16:27:52 -0700680 mfs = netdev->mtu;
681 if (netdev->features & NETIF_F_FCOE_MTU) {
682 mfs = FCOE_MTU;
683 FCOE_NETDEV_DBG(netdev, "Supports FCOE_MTU of %d bytes\n", mfs);
684 }
685 mfs -= (sizeof(struct fcoe_hdr) + sizeof(struct fcoe_crc_eof));
Robert Love1875f272009-11-03 11:47:50 -0800686 if (fc_set_mfs(lport, mfs))
Vasu Dev7f349142009-03-27 09:06:31 -0700687 return -EINVAL;
688
Vasu Dev7f349142009-03-27 09:06:31 -0700689 /* offload features support */
Yi Zou54a5b212010-07-20 15:21:17 -0700690 fcoe_netdev_features_change(lport, netdev);
Vasu Dev7f349142009-03-27 09:06:31 -0700691
Chris Leech014f5c32009-08-25 13:59:30 -0700692 skb_queue_head_init(&port->fcoe_pending_queue);
693 port->fcoe_pending_queue_active = 0;
Robert Love1875f272009-11-03 11:47:50 -0800694 setup_timer(&port->timer, fcoe_queue_timer, (unsigned long)lport);
Vasu Dev7f349142009-03-27 09:06:31 -0700695
Robert Love5e4f8fe2010-05-07 15:18:35 -0700696 fcoe_link_speed_update(lport);
697
Robert Love1875f272009-11-03 11:47:50 -0800698 if (!lport->vport) {
Yi Zoudcece412009-11-20 15:22:21 -0800699 if (fcoe_get_wwn(netdev, &wwnn, NETDEV_FCOE_WWNN))
700 wwnn = fcoe_wwn_from_mac(fcoe->ctlr.ctl_src_addr, 1, 0);
Robert Love1875f272009-11-03 11:47:50 -0800701 fc_set_wwnn(lport, wwnn);
Yi Zoudcece412009-11-20 15:22:21 -0800702 if (fcoe_get_wwn(netdev, &wwpn, NETDEV_FCOE_WWPN))
703 wwpn = fcoe_wwn_from_mac(fcoe->ctlr.ctl_src_addr,
Vasu Devcf4aebca2010-07-20 15:21:22 -0700704 2, 0);
Robert Love1875f272009-11-03 11:47:50 -0800705 fc_set_wwpn(lport, wwpn);
Chris Leech9a057532009-11-03 11:46:40 -0800706 }
Vasu Dev7f349142009-03-27 09:06:31 -0700707
Vasu Dev7f349142009-03-27 09:06:31 -0700708 return 0;
709}
710
711/**
Robert Love1875f272009-11-03 11:47:50 -0800712 * fcoe_shost_config() - Set up the SCSI host associated with a local port
713 * @lport: The local port
Robert Love1875f272009-11-03 11:47:50 -0800714 * @dev: The device associated with the SCSI host
Vasu Dev7f349142009-03-27 09:06:31 -0700715 *
716 * Must be called after fcoe_lport_config() and fcoe_netdev_config()
717 *
Robert Love1875f272009-11-03 11:47:50 -0800718 * Returns: 0 for success
Vasu Dev7f349142009-03-27 09:06:31 -0700719 */
Vasu Dev8ba00a42010-04-09 14:22:54 -0700720static int fcoe_shost_config(struct fc_lport *lport, struct device *dev)
Vasu Dev7f349142009-03-27 09:06:31 -0700721{
722 int rc = 0;
723
724 /* lport scsi host config */
Robert Love1875f272009-11-03 11:47:50 -0800725 lport->host->max_lun = FCOE_MAX_LUN;
726 lport->host->max_id = FCOE_MAX_FCP_TARGET;
727 lport->host->max_channel = 0;
Vasu Devda87bfa2010-04-09 14:22:59 -0700728 lport->host->max_cmd_len = FCOE_MAX_CMD_LEN;
729
Robert Love1875f272009-11-03 11:47:50 -0800730 if (lport->vport)
Yi Zou8ca86f82011-01-28 16:05:11 -0800731 lport->host->transportt = fcoe_vport_scsi_transport;
Chris Leeche9084bb2009-11-03 11:46:34 -0800732 else
Yi Zou8ca86f82011-01-28 16:05:11 -0800733 lport->host->transportt = fcoe_nport_scsi_transport;
Vasu Dev7f349142009-03-27 09:06:31 -0700734
735 /* add the new host to the SCSI-ml */
Robert Love1875f272009-11-03 11:47:50 -0800736 rc = scsi_add_host(lport->host, dev);
Vasu Dev7f349142009-03-27 09:06:31 -0700737 if (rc) {
Robert Love1875f272009-11-03 11:47:50 -0800738 FCOE_NETDEV_DBG(fcoe_netdev(lport), "fcoe_shost_config: "
Robert Loved5488eb2009-06-10 15:30:59 -0700739 "error on scsi_add_host\n");
Vasu Dev7f349142009-03-27 09:06:31 -0700740 return rc;
741 }
Chris Leech9a057532009-11-03 11:46:40 -0800742
Robert Love1875f272009-11-03 11:47:50 -0800743 if (!lport->vport)
Alexey Dobriyan4be929b2010-05-24 14:33:03 -0700744 fc_host_max_npiv_vports(lport->host) = USHRT_MAX;
Chris Leech9a057532009-11-03 11:46:40 -0800745
Robert Love1875f272009-11-03 11:47:50 -0800746 snprintf(fc_host_symbolic_name(lport->host), FC_SYMBOLIC_NAME_SIZE,
Chris Leech5baa17c2009-11-03 11:46:56 -0800747 "%s v%s over %s", FCOE_NAME, FCOE_VERSION,
Robert Love1875f272009-11-03 11:47:50 -0800748 fcoe_netdev(lport)->name);
Vasu Dev7f349142009-03-27 09:06:31 -0700749
750 return 0;
751}
752
Neerav Parikh6fef3902012-01-22 17:30:10 -0800753
754/**
755 * fcoe_fdmi_info() - Get FDMI related info from net devive for SW FCoE
756 * @lport: The local port that is associated with the net device
757 * @netdev: The associated net device
758 *
759 * Must be called after fcoe_shost_config() as it will use local port mutex
760 *
761 */
762static void fcoe_fdmi_info(struct fc_lport *lport, struct net_device *netdev)
763{
764 struct fcoe_interface *fcoe;
765 struct fcoe_port *port;
766 struct net_device *realdev;
767 int rc;
768 struct netdev_fcoe_hbainfo fdmi;
769
770 port = lport_priv(lport);
771 fcoe = port->priv;
772 realdev = fcoe->realdev;
773
774 if (!realdev)
775 return;
776
777 /* No FDMI state m/c for NPIV ports */
778 if (lport->vport)
779 return;
780
781 if (realdev->netdev_ops->ndo_fcoe_get_hbainfo) {
782 memset(&fdmi, 0, sizeof(fdmi));
783 rc = realdev->netdev_ops->ndo_fcoe_get_hbainfo(realdev,
784 &fdmi);
785 if (rc) {
786 printk(KERN_INFO "fcoe: Failed to retrieve FDMI "
787 "information from netdev.\n");
788 return;
789 }
790
791 snprintf(fc_host_serial_number(lport->host),
792 FC_SERIAL_NUMBER_SIZE,
793 "%s",
794 fdmi.serial_number);
795 snprintf(fc_host_manufacturer(lport->host),
796 FC_SERIAL_NUMBER_SIZE,
797 "%s",
798 fdmi.manufacturer);
799 snprintf(fc_host_model(lport->host),
800 FC_SYMBOLIC_NAME_SIZE,
801 "%s",
802 fdmi.model);
803 snprintf(fc_host_model_description(lport->host),
804 FC_SYMBOLIC_NAME_SIZE,
805 "%s",
806 fdmi.model_description);
807 snprintf(fc_host_hardware_version(lport->host),
808 FC_VERSION_STRING_SIZE,
809 "%s",
810 fdmi.hardware_version);
811 snprintf(fc_host_driver_version(lport->host),
812 FC_VERSION_STRING_SIZE,
813 "%s",
814 fdmi.driver_version);
815 snprintf(fc_host_optionrom_version(lport->host),
816 FC_VERSION_STRING_SIZE,
817 "%s",
818 fdmi.optionrom_version);
819 snprintf(fc_host_firmware_version(lport->host),
820 FC_VERSION_STRING_SIZE,
821 "%s",
822 fdmi.firmware_version);
823
824 /* Enable FDMI lport states */
825 lport->fdmi_enabled = 1;
826 } else {
827 lport->fdmi_enabled = 0;
828 printk(KERN_INFO "fcoe: No FDMI support.\n");
829 }
830}
831
Robert Love1875f272009-11-03 11:47:50 -0800832/**
833 * fcoe_oem_match() - The match routine for the offloaded exchange manager
834 * @fp: The I/O frame
Vasu Devd7179682009-07-29 17:05:21 -0700835 *
Robert Love1875f272009-11-03 11:47:50 -0800836 * This routine will be associated with an exchange manager (EM). When
837 * the libfc exchange handling code is looking for an EM to use it will
838 * call this routine and pass it the frame that it wishes to send. This
839 * routine will return True if the associated EM is to be used and False
840 * if the echange code should continue looking for an EM.
841 *
842 * The offload EM that this routine is associated with will handle any
843 * packets that are for SCSI read requests.
844 *
Kiran Patil1ff99182011-06-20 16:59:15 -0700845 * This has been enhanced to work when FCoE stack is operating in target
846 * mode.
847 *
Robert Love1875f272009-11-03 11:47:50 -0800848 * Returns: True for read types I/O, otherwise returns false.
Vasu Devd7179682009-07-29 17:05:21 -0700849 */
Bart Van Assche7c9c6842012-01-13 17:26:25 -0800850static bool fcoe_oem_match(struct fc_frame *fp)
Vasu Devd7179682009-07-29 17:05:21 -0700851{
Kiran Patil1ff99182011-06-20 16:59:15 -0700852 struct fc_frame_header *fh = fc_frame_header_get(fp);
853 struct fcp_cmnd *fcp;
854
855 if (fc_fcp_is_read(fr_fsp(fp)) &&
856 (fr_fsp(fp)->data_len > fcoe_ddp_min))
857 return true;
Yi Zoua762dce2012-01-13 17:26:15 -0800858 else if ((fr_fsp(fp) == NULL) &&
859 (fh->fh_r_ctl == FC_RCTL_DD_UNSOL_CMD) &&
860 (ntohs(fh->fh_rx_id) == FC_XID_UNKNOWN)) {
Kiran Patil1ff99182011-06-20 16:59:15 -0700861 fcp = fc_frame_payload_get(fp, sizeof(*fcp));
Yi Zoua762dce2012-01-13 17:26:15 -0800862 if ((fcp->fc_flags & FCP_CFL_WRDATA) &&
863 (ntohl(fcp->fc_dl) > fcoe_ddp_min))
Kiran Patil1ff99182011-06-20 16:59:15 -0700864 return true;
865 }
866 return false;
Vasu Devd7179682009-07-29 17:05:21 -0700867}
868
Vasu Dev7f349142009-03-27 09:06:31 -0700869/**
Robert Love1875f272009-11-03 11:47:50 -0800870 * fcoe_em_config() - Allocate and configure an exchange manager
871 * @lport: The local port that the new EM will be associated with
Vasu Dev7f349142009-03-27 09:06:31 -0700872 *
Robert Love1875f272009-11-03 11:47:50 -0800873 * Returns: 0 on success
Vasu Dev7f349142009-03-27 09:06:31 -0700874 */
Robert Love1875f272009-11-03 11:47:50 -0800875static inline int fcoe_em_config(struct fc_lport *lport)
Vasu Dev7f349142009-03-27 09:06:31 -0700876{
Robert Love1875f272009-11-03 11:47:50 -0800877 struct fcoe_port *port = lport_priv(lport);
Bhanu Prakash Gollapudi8597ae82011-01-28 16:05:37 -0800878 struct fcoe_interface *fcoe = port->priv;
Chris Leech25024982009-08-25 13:59:35 -0700879 struct fcoe_interface *oldfcoe = NULL;
Vasu Dev1d1b88d2009-07-29 17:05:45 -0700880 struct net_device *old_real_dev, *cur_real_dev;
Vasu Devd7179682009-07-29 17:05:21 -0700881 u16 min_xid = FCOE_MIN_XID;
882 u16 max_xid = FCOE_MAX_XID;
883
884 /*
885 * Check if need to allocate an em instance for
886 * offload exchange ids to be shared across all VN_PORTs/lport.
887 */
Robert Love1875f272009-11-03 11:47:50 -0800888 if (!lport->lro_enabled || !lport->lro_xid ||
889 (lport->lro_xid >= max_xid)) {
890 lport->lro_xid = 0;
Vasu Devd7179682009-07-29 17:05:21 -0700891 goto skip_oem;
892 }
893
894 /*
895 * Reuse existing offload em instance in case
Vasu Dev1d1b88d2009-07-29 17:05:45 -0700896 * it is already allocated on real eth device
Vasu Devd7179682009-07-29 17:05:21 -0700897 */
Chris Leech25024982009-08-25 13:59:35 -0700898 if (fcoe->netdev->priv_flags & IFF_802_1Q_VLAN)
899 cur_real_dev = vlan_dev_real_dev(fcoe->netdev);
Vasu Dev1d1b88d2009-07-29 17:05:45 -0700900 else
Chris Leech25024982009-08-25 13:59:35 -0700901 cur_real_dev = fcoe->netdev;
Vasu Dev1d1b88d2009-07-29 17:05:45 -0700902
Chris Leech25024982009-08-25 13:59:35 -0700903 list_for_each_entry(oldfcoe, &fcoe_hostlist, list) {
Chris Leech25024982009-08-25 13:59:35 -0700904 if (oldfcoe->netdev->priv_flags & IFF_802_1Q_VLAN)
905 old_real_dev = vlan_dev_real_dev(oldfcoe->netdev);
Vasu Dev1d1b88d2009-07-29 17:05:45 -0700906 else
Chris Leech25024982009-08-25 13:59:35 -0700907 old_real_dev = oldfcoe->netdev;
Vasu Dev1d1b88d2009-07-29 17:05:45 -0700908
909 if (cur_real_dev == old_real_dev) {
Chris Leech991cbb62009-08-25 13:59:51 -0700910 fcoe->oem = oldfcoe->oem;
Vasu Devd7179682009-07-29 17:05:21 -0700911 break;
912 }
913 }
914
Chris Leech991cbb62009-08-25 13:59:51 -0700915 if (fcoe->oem) {
Robert Love1875f272009-11-03 11:47:50 -0800916 if (!fc_exch_mgr_add(lport, fcoe->oem, fcoe_oem_match)) {
Vasu Devd7179682009-07-29 17:05:21 -0700917 printk(KERN_ERR "fcoe_em_config: failed to add "
918 "offload em:%p on interface:%s\n",
Chris Leech991cbb62009-08-25 13:59:51 -0700919 fcoe->oem, fcoe->netdev->name);
Vasu Devd7179682009-07-29 17:05:21 -0700920 return -ENOMEM;
921 }
922 } else {
Robert Love1875f272009-11-03 11:47:50 -0800923 fcoe->oem = fc_exch_mgr_alloc(lport, FC_CLASS_3,
924 FCOE_MIN_XID, lport->lro_xid,
925 fcoe_oem_match);
Chris Leech991cbb62009-08-25 13:59:51 -0700926 if (!fcoe->oem) {
Vasu Devd7179682009-07-29 17:05:21 -0700927 printk(KERN_ERR "fcoe_em_config: failed to allocate "
928 "em for offload exches on interface:%s\n",
Chris Leech25024982009-08-25 13:59:35 -0700929 fcoe->netdev->name);
Vasu Devd7179682009-07-29 17:05:21 -0700930 return -ENOMEM;
931 }
932 }
933
934 /*
935 * Exclude offload EM xid range from next EM xid range.
936 */
Robert Love1875f272009-11-03 11:47:50 -0800937 min_xid += lport->lro_xid + 1;
Vasu Devd7179682009-07-29 17:05:21 -0700938
939skip_oem:
Robert Love1875f272009-11-03 11:47:50 -0800940 if (!fc_exch_mgr_alloc(lport, FC_CLASS_3, min_xid, max_xid, NULL)) {
Vasu Devd7179682009-07-29 17:05:21 -0700941 printk(KERN_ERR "fcoe_em_config: failed to "
Chris Leech25024982009-08-25 13:59:35 -0700942 "allocate em on interface %s\n", fcoe->netdev->name);
Vasu Dev7f349142009-03-27 09:06:31 -0700943 return -ENOMEM;
Vasu Devd7179682009-07-29 17:05:21 -0700944 }
Vasu Dev7f349142009-03-27 09:06:31 -0700945
946 return 0;
947}
948
949/**
Robert Love1875f272009-11-03 11:47:50 -0800950 * fcoe_if_destroy() - Tear down a SW FCoE instance
951 * @lport: The local port to be destroyed
Vasu Dev34ce27b2010-05-07 15:18:46 -0700952 *
Vasu Dev7f349142009-03-27 09:06:31 -0700953 */
Chris Leechaf7f85d2009-08-25 13:59:24 -0700954static void fcoe_if_destroy(struct fc_lport *lport)
Vasu Dev7f349142009-03-27 09:06:31 -0700955{
Neerav Parikhb2085a42011-06-20 16:59:51 -0700956 struct fcoe_port *port = lport_priv(lport);
957 struct fcoe_interface *fcoe = port->priv;
958 struct net_device *netdev = fcoe->netdev;
959
960 FCOE_NETDEV_DBG(netdev, "Destroying interface\n");
961
962 /* Logout of the fabric */
963 fc_fabric_logoff(lport);
964
965 /* Cleanup the fc_lport */
966 fc_lport_destroy(lport);
967
968 /* Stop the transmit retry timer */
969 del_timer_sync(&port->timer);
970
971 /* Free existing transmit skbs */
972 fcoe_clean_pending_queue(lport);
973
974 rtnl_lock();
975 if (!is_zero_ether_addr(port->data_src_addr))
976 dev_uc_del(netdev, port->data_src_addr);
977 rtnl_unlock();
978
979 /* Release reference held in fcoe_if_create() */
980 fcoe_interface_put(fcoe);
981
Chris Leech54b649f2009-08-25 14:00:07 -0700982 /* Free queued packets for the per-CPU receive threads */
983 fcoe_percpu_clean(lport);
984
Vasu Dev7f349142009-03-27 09:06:31 -0700985 /* Detach from the scsi-ml */
Chris Leechaf7f85d2009-08-25 13:59:24 -0700986 fc_remove_host(lport->host);
987 scsi_remove_host(lport->host);
Vasu Dev7f349142009-03-27 09:06:31 -0700988
Yi Zou80e736f2010-11-30 16:18:07 -0800989 /* Destroy lport scsi_priv */
990 fc_fcp_destroy(lport);
991
Vasu Dev7f349142009-03-27 09:06:31 -0700992 /* There are no more rports or I/O, free the EM */
Chris Leechaf7f85d2009-08-25 13:59:24 -0700993 fc_exch_mgr_free(lport);
Vasu Dev7f349142009-03-27 09:06:31 -0700994
Vasu Dev7f349142009-03-27 09:06:31 -0700995 /* Free memory used by statistical counters */
Chris Leechaf7f85d2009-08-25 13:59:24 -0700996 fc_lport_free_stats(lport);
Vasu Dev7f349142009-03-27 09:06:31 -0700997
Chris Leech2e70e242009-08-25 14:00:23 -0700998 /* Release the Scsi_Host */
Chris Leechaf7f85d2009-08-25 13:59:24 -0700999 scsi_host_put(lport->host);
Vasu Dev7f349142009-03-27 09:06:31 -07001000}
1001
Robert Love1875f272009-11-03 11:47:50 -08001002/**
1003 * fcoe_ddp_setup() - Call a LLD's ddp_setup through the net device
1004 * @lport: The local port to setup DDP for
1005 * @xid: The exchange ID for this DDP transfer
1006 * @sgl: The scatterlist describing this transfer
1007 * @sgc: The number of sg items
Vasu Dev7f349142009-03-27 09:06:31 -07001008 *
Robert Love1875f272009-11-03 11:47:50 -08001009 * Returns: 0 if the DDP context was not configured
Vasu Dev7f349142009-03-27 09:06:31 -07001010 */
Robert Love1875f272009-11-03 11:47:50 -08001011static int fcoe_ddp_setup(struct fc_lport *lport, u16 xid,
1012 struct scatterlist *sgl, unsigned int sgc)
Vasu Dev7f349142009-03-27 09:06:31 -07001013{
Robert Love1875f272009-11-03 11:47:50 -08001014 struct net_device *netdev = fcoe_netdev(lport);
Vasu Dev7f349142009-03-27 09:06:31 -07001015
Robert Love1875f272009-11-03 11:47:50 -08001016 if (netdev->netdev_ops->ndo_fcoe_ddp_setup)
1017 return netdev->netdev_ops->ndo_fcoe_ddp_setup(netdev,
1018 xid, sgl,
1019 sgc);
Vasu Dev7f349142009-03-27 09:06:31 -07001020
1021 return 0;
1022}
1023
Vasu Dev7f349142009-03-27 09:06:31 -07001024/**
Yi Zou71f89492011-06-20 16:59:10 -07001025 * fcoe_ddp_target() - Call a LLD's ddp_target through the net device
1026 * @lport: The local port to setup DDP for
1027 * @xid: The exchange ID for this DDP transfer
1028 * @sgl: The scatterlist describing this transfer
1029 * @sgc: The number of sg items
1030 *
1031 * Returns: 0 if the DDP context was not configured
1032 */
1033static int fcoe_ddp_target(struct fc_lport *lport, u16 xid,
1034 struct scatterlist *sgl, unsigned int sgc)
1035{
1036 struct net_device *netdev = fcoe_netdev(lport);
1037
1038 if (netdev->netdev_ops->ndo_fcoe_ddp_target)
1039 return netdev->netdev_ops->ndo_fcoe_ddp_target(netdev, xid,
1040 sgl, sgc);
1041
1042 return 0;
1043}
1044
1045
1046/**
Robert Love1875f272009-11-03 11:47:50 -08001047 * fcoe_ddp_done() - Call a LLD's ddp_done through the net device
1048 * @lport: The local port to complete DDP on
1049 * @xid: The exchange ID for this DDP transfer
Vasu Dev7f349142009-03-27 09:06:31 -07001050 *
Robert Love1875f272009-11-03 11:47:50 -08001051 * Returns: the length of data that have been completed by DDP
1052 */
1053static int fcoe_ddp_done(struct fc_lport *lport, u16 xid)
1054{
1055 struct net_device *netdev = fcoe_netdev(lport);
1056
1057 if (netdev->netdev_ops->ndo_fcoe_ddp_done)
1058 return netdev->netdev_ops->ndo_fcoe_ddp_done(netdev, xid);
1059 return 0;
1060}
1061
1062/**
1063 * fcoe_if_create() - Create a FCoE instance on an interface
1064 * @fcoe: The FCoE interface to create a local port on
1065 * @parent: The device pointer to be the parent in sysfs for the SCSI host
1066 * @npiv: Indicates if the port is a vport or not
Vasu Dev7f349142009-03-27 09:06:31 -07001067 *
Robert Love1875f272009-11-03 11:47:50 -08001068 * Creates a fc_lport instance and a Scsi_Host instance and configure them.
1069 *
1070 * Returns: The allocated fc_lport or an error pointer
Vasu Dev7f349142009-03-27 09:06:31 -07001071 */
Chris Leech030f4e02009-08-25 14:00:02 -07001072static struct fc_lport *fcoe_if_create(struct fcoe_interface *fcoe,
Chris Leech9a057532009-11-03 11:46:40 -08001073 struct device *parent, int npiv)
Vasu Dev7f349142009-03-27 09:06:31 -07001074{
Robert Love1875f272009-11-03 11:47:50 -08001075 struct net_device *netdev = fcoe->netdev;
Vasu Dev72fa3962011-02-25 15:03:01 -08001076 struct fc_lport *lport, *n_port;
Chris Leech014f5c32009-08-25 13:59:30 -07001077 struct fcoe_port *port;
Vasu Dev72fa3962011-02-25 15:03:01 -08001078 struct Scsi_Host *shost;
Robert Love1875f272009-11-03 11:47:50 -08001079 int rc;
Chris Leech9a057532009-11-03 11:46:40 -08001080 /*
1081 * parent is only a vport if npiv is 1,
1082 * but we'll only use vport in that case so go ahead and set it
1083 */
1084 struct fc_vport *vport = dev_to_vport(parent);
Vasu Dev7f349142009-03-27 09:06:31 -07001085
Robert Loved5488eb2009-06-10 15:30:59 -07001086 FCOE_NETDEV_DBG(netdev, "Create Interface\n");
Vasu Dev7f349142009-03-27 09:06:31 -07001087
Vasu Dev72fa3962011-02-25 15:03:01 -08001088 if (!npiv)
1089 lport = libfc_host_alloc(&fcoe_shost_template, sizeof(*port));
1090 else
1091 lport = libfc_vport_create(vport, sizeof(*port));
1092
Chris Leech86221962009-11-03 11:46:08 -08001093 if (!lport) {
Chris Leech014f5c32009-08-25 13:59:30 -07001094 FCOE_NETDEV_DBG(netdev, "Could not allocate host structure\n");
1095 rc = -ENOMEM;
Chris Leech030f4e02009-08-25 14:00:02 -07001096 goto out;
Chris Leech014f5c32009-08-25 13:59:30 -07001097 }
Chris Leech014f5c32009-08-25 13:59:30 -07001098 port = lport_priv(lport);
Chris Leech2e70e242009-08-25 14:00:23 -07001099 port->lport = lport;
Bhanu Prakash Gollapudi8597ae82011-01-28 16:05:37 -08001100 port->priv = fcoe;
1101 port->max_queue_depth = FCOE_MAX_QUEUE_DEPTH;
1102 port->min_queue_depth = FCOE_MIN_QUEUE_DEPTH;
Chris Leech2e70e242009-08-25 14:00:23 -07001103 INIT_WORK(&port->destroy_work, fcoe_destroy_work);
Vasu Dev7f349142009-03-27 09:06:31 -07001104
Robert Love1875f272009-11-03 11:47:50 -08001105 /* configure a fc_lport including the exchange manager */
Chris Leechaf7f85d2009-08-25 13:59:24 -07001106 rc = fcoe_lport_config(lport);
Vasu Dev7f349142009-03-27 09:06:31 -07001107 if (rc) {
Robert Loved5488eb2009-06-10 15:30:59 -07001108 FCOE_NETDEV_DBG(netdev, "Could not configure lport for the "
1109 "interface\n");
Vasu Dev7f349142009-03-27 09:06:31 -07001110 goto out_host_put;
1111 }
1112
Chris Leech9a057532009-11-03 11:46:40 -08001113 if (npiv) {
Chris Leech9f8f3aa2010-04-09 14:23:16 -07001114 FCOE_NETDEV_DBG(netdev, "Setting vport names, "
1115 "%16.16llx %16.16llx\n",
Robert Love1875f272009-11-03 11:47:50 -08001116 vport->node_name, vport->port_name);
Chris Leech9a057532009-11-03 11:46:40 -08001117 fc_set_wwnn(lport, vport->node_name);
1118 fc_set_wwpn(lport, vport->port_name);
1119 }
1120
Vasu Devab6b85c2009-05-17 12:33:08 +00001121 /* configure lport network properties */
Chris Leechaf7f85d2009-08-25 13:59:24 -07001122 rc = fcoe_netdev_config(lport, netdev);
Vasu Devab6b85c2009-05-17 12:33:08 +00001123 if (rc) {
Robert Loved5488eb2009-06-10 15:30:59 -07001124 FCOE_NETDEV_DBG(netdev, "Could not configure netdev for the "
1125 "interface\n");
Chris Leech54b649f2009-08-25 14:00:07 -07001126 goto out_lp_destroy;
Vasu Devab6b85c2009-05-17 12:33:08 +00001127 }
Joe Eykholt97c83892009-03-17 11:42:40 -07001128
Vasu Dev7f349142009-03-27 09:06:31 -07001129 /* configure lport scsi host properties */
Vasu Dev8ba00a42010-04-09 14:22:54 -07001130 rc = fcoe_shost_config(lport, parent);
Vasu Dev7f349142009-03-27 09:06:31 -07001131 if (rc) {
Robert Loved5488eb2009-06-10 15:30:59 -07001132 FCOE_NETDEV_DBG(netdev, "Could not configure shost for the "
1133 "interface\n");
Chris Leech54b649f2009-08-25 14:00:07 -07001134 goto out_lp_destroy;
Vasu Dev7f349142009-03-27 09:06:31 -07001135 }
1136
Vasu Dev7f349142009-03-27 09:06:31 -07001137 /* Initialize the library */
Joe Eykholte10f8c62010-07-20 15:20:30 -07001138 rc = fcoe_libfc_config(lport, &fcoe->ctlr, &fcoe_libfc_fcn_templ, 1);
Vasu Dev7f349142009-03-27 09:06:31 -07001139 if (rc) {
Robert Loved5488eb2009-06-10 15:30:59 -07001140 FCOE_NETDEV_DBG(netdev, "Could not configure libfc for the "
1141 "interface\n");
Vasu Dev7f349142009-03-27 09:06:31 -07001142 goto out_lp_destroy;
1143 }
1144
Neerav Parikh6fef3902012-01-22 17:30:10 -08001145 /* Initialized FDMI information */
1146 fcoe_fdmi_info(lport, netdev);
1147
Vasu Dev72fa3962011-02-25 15:03:01 -08001148 /*
1149 * fcoe_em_alloc() and fcoe_hostlist_add() both
1150 * need to be atomic with respect to other changes to the
1151 * hostlist since fcoe_em_alloc() looks for an existing EM
1152 * instance on host list updated by fcoe_hostlist_add().
1153 *
1154 * This is currently handled through the fcoe_config_mutex
1155 * begin held.
1156 */
1157 if (!npiv)
Chris Leech9a057532009-11-03 11:46:40 -08001158 /* lport exch manager allocation */
1159 rc = fcoe_em_config(lport);
Vasu Dev72fa3962011-02-25 15:03:01 -08001160 else {
1161 shost = vport_to_shost(vport);
1162 n_port = shost_priv(shost);
1163 rc = fc_exch_mgr_list_clone(n_port, lport);
1164 }
1165
1166 if (rc) {
1167 FCOE_NETDEV_DBG(netdev, "Could not configure the EM\n");
1168 goto out_lp_destroy;
Vasu Dev96316092009-07-29 17:05:00 -07001169 }
1170
Chris Leech030f4e02009-08-25 14:00:02 -07001171 fcoe_interface_get(fcoe);
Chris Leechaf7f85d2009-08-25 13:59:24 -07001172 return lport;
Vasu Dev7f349142009-03-27 09:06:31 -07001173
1174out_lp_destroy:
Chris Leechaf7f85d2009-08-25 13:59:24 -07001175 fc_exch_mgr_free(lport);
Vasu Dev7f349142009-03-27 09:06:31 -07001176out_host_put:
Chris Leechaf7f85d2009-08-25 13:59:24 -07001177 scsi_host_put(lport->host);
1178out:
1179 return ERR_PTR(rc);
Vasu Dev7f349142009-03-27 09:06:31 -07001180}
1181
1182/**
Robert Love1875f272009-11-03 11:47:50 -08001183 * fcoe_if_init() - Initialization routine for fcoe.ko
Vasu Dev7f349142009-03-27 09:06:31 -07001184 *
Robert Love1875f272009-11-03 11:47:50 -08001185 * Attaches the SW FCoE transport to the FC transport
1186 *
1187 * Returns: 0 on success
Vasu Dev7f349142009-03-27 09:06:31 -07001188 */
1189static int __init fcoe_if_init(void)
1190{
1191 /* attach to scsi transport */
Yi Zou8ca86f82011-01-28 16:05:11 -08001192 fcoe_nport_scsi_transport =
1193 fc_attach_transport(&fcoe_nport_fc_functions);
1194 fcoe_vport_scsi_transport =
1195 fc_attach_transport(&fcoe_vport_fc_functions);
Vasu Dev7f349142009-03-27 09:06:31 -07001196
Yi Zou8ca86f82011-01-28 16:05:11 -08001197 if (!fcoe_nport_scsi_transport) {
Robert Loved5488eb2009-06-10 15:30:59 -07001198 printk(KERN_ERR "fcoe: Failed to attach to the FC transport\n");
Vasu Dev7f349142009-03-27 09:06:31 -07001199 return -ENODEV;
1200 }
1201
1202 return 0;
1203}
1204
1205/**
Robert Love1875f272009-11-03 11:47:50 -08001206 * fcoe_if_exit() - Tear down fcoe.ko
Vasu Dev7f349142009-03-27 09:06:31 -07001207 *
Robert Love1875f272009-11-03 11:47:50 -08001208 * Detaches the SW FCoE transport from the FC transport
1209 *
1210 * Returns: 0 on success
Vasu Dev7f349142009-03-27 09:06:31 -07001211 */
Bart Van Assche7c9c6842012-01-13 17:26:25 -08001212static int __exit fcoe_if_exit(void)
Vasu Dev7f349142009-03-27 09:06:31 -07001213{
Yi Zou8ca86f82011-01-28 16:05:11 -08001214 fc_release_transport(fcoe_nport_scsi_transport);
1215 fc_release_transport(fcoe_vport_scsi_transport);
1216 fcoe_nport_scsi_transport = NULL;
1217 fcoe_vport_scsi_transport = NULL;
Vasu Dev7f349142009-03-27 09:06:31 -07001218 return 0;
1219}
1220
Robert Love85b4aa42008-12-09 15:10:24 -08001221/**
Robert Love1875f272009-11-03 11:47:50 -08001222 * fcoe_percpu_thread_create() - Create a receive thread for an online CPU
1223 * @cpu: The CPU index of the CPU to create a receive thread for
Robert Love8976f422009-03-17 11:41:46 -07001224 */
1225static void fcoe_percpu_thread_create(unsigned int cpu)
1226{
1227 struct fcoe_percpu_s *p;
1228 struct task_struct *thread;
1229
1230 p = &per_cpu(fcoe_percpu, cpu);
1231
Eric Dumazet5c609ff2011-09-27 21:37:52 -07001232 thread = kthread_create_on_node(fcoe_percpu_receive_thread,
1233 (void *)p, cpu_to_node(cpu),
1234 "fcoethread/%d", cpu);
Robert Love8976f422009-03-17 11:41:46 -07001235
Joe Eykholte7a51992009-08-25 14:04:08 -07001236 if (likely(!IS_ERR(thread))) {
Robert Love8976f422009-03-17 11:41:46 -07001237 kthread_bind(thread, cpu);
1238 wake_up_process(thread);
1239
1240 spin_lock_bh(&p->fcoe_rx_list.lock);
1241 p->thread = thread;
1242 spin_unlock_bh(&p->fcoe_rx_list.lock);
1243 }
1244}
1245
1246/**
Robert Love1875f272009-11-03 11:47:50 -08001247 * fcoe_percpu_thread_destroy() - Remove the receive thread of a CPU
1248 * @cpu: The CPU index of the CPU whose receive thread is to be destroyed
Robert Love8976f422009-03-17 11:41:46 -07001249 *
1250 * Destroys a per-CPU Rx thread. Any pending skbs are moved to the
1251 * current CPU's Rx thread. If the thread being destroyed is bound to
1252 * the CPU processing this context the skbs will be freed.
1253 */
1254static void fcoe_percpu_thread_destroy(unsigned int cpu)
1255{
1256 struct fcoe_percpu_s *p;
1257 struct task_struct *thread;
1258 struct page *crc_eof;
1259 struct sk_buff *skb;
1260#ifdef CONFIG_SMP
1261 struct fcoe_percpu_s *p0;
Joe Eykholtf018b732010-03-12 16:08:55 -08001262 unsigned targ_cpu = get_cpu();
Robert Love8976f422009-03-17 11:41:46 -07001263#endif /* CONFIG_SMP */
1264
Robert Loved5488eb2009-06-10 15:30:59 -07001265 FCOE_DBG("Destroying receive thread for CPU %d\n", cpu);
Robert Love8976f422009-03-17 11:41:46 -07001266
1267 /* Prevent any new skbs from being queued for this CPU. */
1268 p = &per_cpu(fcoe_percpu, cpu);
1269 spin_lock_bh(&p->fcoe_rx_list.lock);
1270 thread = p->thread;
1271 p->thread = NULL;
1272 crc_eof = p->crc_eof_page;
1273 p->crc_eof_page = NULL;
1274 p->crc_eof_offset = 0;
1275 spin_unlock_bh(&p->fcoe_rx_list.lock);
1276
1277#ifdef CONFIG_SMP
1278 /*
1279 * Don't bother moving the skb's if this context is running
1280 * on the same CPU that is having its thread destroyed. This
1281 * can easily happen when the module is removed.
1282 */
1283 if (cpu != targ_cpu) {
1284 p0 = &per_cpu(fcoe_percpu, targ_cpu);
1285 spin_lock_bh(&p0->fcoe_rx_list.lock);
1286 if (p0->thread) {
Robert Loved5488eb2009-06-10 15:30:59 -07001287 FCOE_DBG("Moving frames from CPU %d to CPU %d\n",
1288 cpu, targ_cpu);
Robert Love8976f422009-03-17 11:41:46 -07001289
1290 while ((skb = __skb_dequeue(&p->fcoe_rx_list)) != NULL)
1291 __skb_queue_tail(&p0->fcoe_rx_list, skb);
1292 spin_unlock_bh(&p0->fcoe_rx_list.lock);
1293 } else {
1294 /*
1295 * The targeted CPU is not initialized and cannot accept
Robert Love1875f272009-11-03 11:47:50 -08001296 * new skbs. Unlock the targeted CPU and drop the skbs
Robert Love8976f422009-03-17 11:41:46 -07001297 * on the CPU that is going offline.
1298 */
1299 while ((skb = __skb_dequeue(&p->fcoe_rx_list)) != NULL)
1300 kfree_skb(skb);
1301 spin_unlock_bh(&p0->fcoe_rx_list.lock);
1302 }
1303 } else {
1304 /*
1305 * This scenario occurs when the module is being removed
1306 * and all threads are being destroyed. skbs will continue
1307 * to be shifted from the CPU thread that is being removed
1308 * to the CPU thread associated with the CPU that is processing
1309 * the module removal. Once there is only one CPU Rx thread it
1310 * will reach this case and we will drop all skbs and later
1311 * stop the thread.
1312 */
1313 spin_lock_bh(&p->fcoe_rx_list.lock);
1314 while ((skb = __skb_dequeue(&p->fcoe_rx_list)) != NULL)
1315 kfree_skb(skb);
1316 spin_unlock_bh(&p->fcoe_rx_list.lock);
1317 }
Joe Eykholtf018b732010-03-12 16:08:55 -08001318 put_cpu();
Robert Love8976f422009-03-17 11:41:46 -07001319#else
1320 /*
Chris Leechdd3fd722009-04-21 16:27:36 -07001321 * This a non-SMP scenario where the singular Rx thread is
Robert Love8976f422009-03-17 11:41:46 -07001322 * being removed. Free all skbs and stop the thread.
1323 */
1324 spin_lock_bh(&p->fcoe_rx_list.lock);
1325 while ((skb = __skb_dequeue(&p->fcoe_rx_list)) != NULL)
1326 kfree_skb(skb);
1327 spin_unlock_bh(&p->fcoe_rx_list.lock);
1328#endif
1329
1330 if (thread)
1331 kthread_stop(thread);
1332
1333 if (crc_eof)
1334 put_page(crc_eof);
1335}
1336
1337/**
Robert Love1875f272009-11-03 11:47:50 -08001338 * fcoe_cpu_callback() - Handler for CPU hotplug events
1339 * @nfb: The callback data block
1340 * @action: The event triggering the callback
1341 * @hcpu: The index of the CPU that the event is for
Robert Love8976f422009-03-17 11:41:46 -07001342 *
Robert Love1875f272009-11-03 11:47:50 -08001343 * This creates or destroys per-CPU data for fcoe
Robert Love8976f422009-03-17 11:41:46 -07001344 *
1345 * Returns NOTIFY_OK always.
1346 */
1347static int fcoe_cpu_callback(struct notifier_block *nfb,
1348 unsigned long action, void *hcpu)
1349{
1350 unsigned cpu = (unsigned long)hcpu;
1351
1352 switch (action) {
1353 case CPU_ONLINE:
1354 case CPU_ONLINE_FROZEN:
Robert Loved5488eb2009-06-10 15:30:59 -07001355 FCOE_DBG("CPU %x online: Create Rx thread\n", cpu);
Robert Love8976f422009-03-17 11:41:46 -07001356 fcoe_percpu_thread_create(cpu);
1357 break;
1358 case CPU_DEAD:
1359 case CPU_DEAD_FROZEN:
Robert Loved5488eb2009-06-10 15:30:59 -07001360 FCOE_DBG("CPU %x offline: Remove Rx thread\n", cpu);
Robert Love8976f422009-03-17 11:41:46 -07001361 fcoe_percpu_thread_destroy(cpu);
1362 break;
1363 default:
1364 break;
1365 }
1366 return NOTIFY_OK;
1367}
1368
Robert Love8976f422009-03-17 11:41:46 -07001369/**
Kiran Patil064287e2011-06-20 16:59:20 -07001370 * fcoe_select_cpu() - Selects CPU to handle post-processing of incoming
1371 * command.
Kiran Patil064287e2011-06-20 16:59:20 -07001372 *
Vasu Devd2722812011-07-27 15:11:10 -07001373 * This routine selects next CPU based on cpumask to distribute
1374 * incoming requests in round robin.
Kiran Patil064287e2011-06-20 16:59:20 -07001375 *
Vasu Devd2722812011-07-27 15:11:10 -07001376 * Returns: int CPU number
Kiran Patil064287e2011-06-20 16:59:20 -07001377 */
Vasu Devd2722812011-07-27 15:11:10 -07001378static inline unsigned int fcoe_select_cpu(void)
Kiran Patil064287e2011-06-20 16:59:20 -07001379{
1380 static unsigned int selected_cpu;
1381
Vasu Devd2722812011-07-27 15:11:10 -07001382 selected_cpu = cpumask_next(selected_cpu, cpu_online_mask);
1383 if (selected_cpu >= nr_cpu_ids)
1384 selected_cpu = cpumask_first(cpu_online_mask);
1385
Kiran Patil064287e2011-06-20 16:59:20 -07001386 return selected_cpu;
1387}
1388
1389/**
Robert Love1875f272009-11-03 11:47:50 -08001390 * fcoe_rcv() - Receive packets from a net device
1391 * @skb: The received packet
1392 * @netdev: The net device that the packet was received on
1393 * @ptype: The packet type context
1394 * @olddev: The last device net device
Robert Love85b4aa42008-12-09 15:10:24 -08001395 *
Robert Love1875f272009-11-03 11:47:50 -08001396 * This routine is called by NET_RX_SOFTIRQ. It receives a packet, builds a
1397 * FC frame and passes the frame to libfc.
Robert Love85b4aa42008-12-09 15:10:24 -08001398 *
1399 * Returns: 0 for success
Robert Love34f42a02009-02-27 10:55:45 -08001400 */
Bart Van Assche7c9c6842012-01-13 17:26:25 -08001401static int fcoe_rcv(struct sk_buff *skb, struct net_device *netdev,
Robert Love85b4aa42008-12-09 15:10:24 -08001402 struct packet_type *ptype, struct net_device *olddev)
1403{
Robert Love1875f272009-11-03 11:47:50 -08001404 struct fc_lport *lport;
Robert Love85b4aa42008-12-09 15:10:24 -08001405 struct fcoe_rcv_info *fr;
Chris Leech259ad852009-08-25 13:59:41 -07001406 struct fcoe_interface *fcoe;
Robert Love85b4aa42008-12-09 15:10:24 -08001407 struct fc_frame_header *fh;
Robert Love85b4aa42008-12-09 15:10:24 -08001408 struct fcoe_percpu_s *fps;
Vasu Dev519e5132010-07-20 15:19:32 -07001409 struct ethhdr *eh;
Vasu Devb2f00912009-08-25 13:58:53 -07001410 unsigned int cpu;
Robert Love85b4aa42008-12-09 15:10:24 -08001411
Chris Leech259ad852009-08-25 13:59:41 -07001412 fcoe = container_of(ptype, struct fcoe_interface, fcoe_packet_type);
Robert Love1875f272009-11-03 11:47:50 -08001413 lport = fcoe->ctlr.lp;
1414 if (unlikely(!lport)) {
1415 FCOE_NETDEV_DBG(netdev, "Cannot find hba structure");
Robert Love85b4aa42008-12-09 15:10:24 -08001416 goto err2;
1417 }
Robert Love1875f272009-11-03 11:47:50 -08001418 if (!lport->link_up)
Joe Eykholt97c83892009-03-17 11:42:40 -07001419 goto err2;
Robert Love85b4aa42008-12-09 15:10:24 -08001420
Robert Love1875f272009-11-03 11:47:50 -08001421 FCOE_NETDEV_DBG(netdev, "skb_info: len:%d data_len:%d head:%p "
Robert Loved5488eb2009-06-10 15:30:59 -07001422 "data:%p tail:%p end:%p sum:%d dev:%s",
1423 skb->len, skb->data_len, skb->head, skb->data,
1424 skb_tail_pointer(skb), skb_end_pointer(skb),
1425 skb->csum, skb->dev ? skb->dev->name : "<NULL>");
Robert Love85b4aa42008-12-09 15:10:24 -08001426
Vasu Dev519e5132010-07-20 15:19:32 -07001427 eh = eth_hdr(skb);
Vasu Dev519e5132010-07-20 15:19:32 -07001428
1429 if (is_fip_mode(&fcoe->ctlr) &&
1430 compare_ether_addr(eh->h_source, fcoe->ctlr.dest_addr)) {
1431 FCOE_NETDEV_DBG(netdev, "wrong source mac address:%pM\n",
1432 eh->h_source);
Robert Love85b4aa42008-12-09 15:10:24 -08001433 goto err;
1434 }
1435
1436 /*
1437 * Check for minimum frame length, and make sure required FCoE
1438 * and FC headers are pulled into the linear data area.
1439 */
1440 if (unlikely((skb->len < FCOE_MIN_FRAME) ||
Robert Love1875f272009-11-03 11:47:50 -08001441 !pskb_may_pull(skb, FCOE_HEADER_LEN)))
Robert Love85b4aa42008-12-09 15:10:24 -08001442 goto err;
1443
1444 skb_set_transport_header(skb, sizeof(struct fcoe_hdr));
1445 fh = (struct fc_frame_header *) skb_transport_header(skb);
1446
Robert Love0ee31cb2010-10-08 17:12:46 -07001447 if (ntoh24(&eh->h_dest[3]) != ntoh24(fh->fh_d_id)) {
1448 FCOE_NETDEV_DBG(netdev, "FC frame d_id mismatch with MAC:%pM\n",
1449 eh->h_dest);
1450 goto err;
1451 }
1452
Robert Love85b4aa42008-12-09 15:10:24 -08001453 fr = fcoe_dev_from_skb(skb);
Robert Love1875f272009-11-03 11:47:50 -08001454 fr->fr_dev = lport;
Robert Love5e5e92d2009-03-17 11:41:35 -07001455
Robert Love85b4aa42008-12-09 15:10:24 -08001456 /*
Vasu Devb2f00912009-08-25 13:58:53 -07001457 * In case the incoming frame's exchange is originated from
1458 * the initiator, then received frame's exchange id is ANDed
1459 * with fc_cpu_mask bits to get the same cpu on which exchange
Vasu Devd2722812011-07-27 15:11:10 -07001460 * was originated, otherwise select cpu using rx exchange id
1461 * or fcoe_select_cpu().
Robert Love85b4aa42008-12-09 15:10:24 -08001462 */
Vasu Devb2f00912009-08-25 13:58:53 -07001463 if (ntoh24(fh->fh_f_ctl) & FC_FC_EX_CTX)
1464 cpu = ntohs(fh->fh_ox_id) & fc_cpu_mask;
Kiran Patil064287e2011-06-20 16:59:20 -07001465 else {
Vasu Devd2722812011-07-27 15:11:10 -07001466 if (ntohs(fh->fh_rx_id) == FC_XID_UNKNOWN)
1467 cpu = fcoe_select_cpu();
1468 else
Kiran Patil29bdd2b2011-06-20 16:59:25 -07001469 cpu = ntohs(fh->fh_rx_id) & fc_cpu_mask;
Kiran Patil064287e2011-06-20 16:59:20 -07001470 }
Vasu Dev324f6672011-07-27 15:10:39 -07001471
1472 if (cpu >= nr_cpu_ids)
1473 goto err;
1474
Robert Love8976f422009-03-17 11:41:46 -07001475 fps = &per_cpu(fcoe_percpu, cpu);
Robert Love85b4aa42008-12-09 15:10:24 -08001476 spin_lock_bh(&fps->fcoe_rx_list.lock);
Robert Love8976f422009-03-17 11:41:46 -07001477 if (unlikely(!fps->thread)) {
1478 /*
1479 * The targeted CPU is not ready, let's target
1480 * the first CPU now. For non-SMP systems this
1481 * will check the same CPU twice.
1482 */
Robert Love1875f272009-11-03 11:47:50 -08001483 FCOE_NETDEV_DBG(netdev, "CPU is online, but no receive thread "
Robert Loved5488eb2009-06-10 15:30:59 -07001484 "ready for incoming skb- using first online "
1485 "CPU.\n");
Robert Love8976f422009-03-17 11:41:46 -07001486
1487 spin_unlock_bh(&fps->fcoe_rx_list.lock);
Rusty Russell69571772009-12-17 11:43:14 -06001488 cpu = cpumask_first(cpu_online_mask);
Robert Love8976f422009-03-17 11:41:46 -07001489 fps = &per_cpu(fcoe_percpu, cpu);
1490 spin_lock_bh(&fps->fcoe_rx_list.lock);
1491 if (!fps->thread) {
1492 spin_unlock_bh(&fps->fcoe_rx_list.lock);
1493 goto err;
1494 }
1495 }
1496
1497 /*
1498 * We now have a valid CPU that we're targeting for
1499 * this skb. We also have this receive thread locked,
1500 * so we're free to queue skbs into it's queue.
1501 */
Robert Love85b4aa42008-12-09 15:10:24 -08001502
Chris Leech859b7b62009-11-20 14:54:47 -08001503 /* If this is a SCSI-FCP frame, and this is already executing on the
1504 * correct CPU, and the queue for this CPU is empty, then go ahead
1505 * and process the frame directly in the softirq context.
1506 * This lets us process completions without context switching from the
1507 * NET_RX softirq, to our receive processing thread, and then back to
1508 * BLOCK softirq context.
1509 */
1510 if (fh->fh_type == FC_TYPE_FCP &&
1511 cpu == smp_processor_id() &&
1512 skb_queue_empty(&fps->fcoe_rx_list)) {
1513 spin_unlock_bh(&fps->fcoe_rx_list.lock);
1514 fcoe_recv_frame(skb);
1515 } else {
1516 __skb_queue_tail(&fps->fcoe_rx_list, skb);
1517 if (fps->fcoe_rx_list.qlen == 1)
1518 wake_up_process(fps->thread);
1519 spin_unlock_bh(&fps->fcoe_rx_list.lock);
1520 }
Robert Love85b4aa42008-12-09 15:10:24 -08001521
1522 return 0;
1523err:
Joe Eykholtf018b732010-03-12 16:08:55 -08001524 per_cpu_ptr(lport->dev_stats, get_cpu())->ErrorFrames++;
1525 put_cpu();
Robert Love85b4aa42008-12-09 15:10:24 -08001526err2:
1527 kfree_skb(skb);
1528 return -1;
1529}
Robert Love85b4aa42008-12-09 15:10:24 -08001530
1531/**
Bhanu Prakash Gollapudi8597ae82011-01-28 16:05:37 -08001532 * fcoe_alloc_paged_crc_eof() - Allocate a page to be used for the trailer CRC
Robert Love1875f272009-11-03 11:47:50 -08001533 * @skb: The packet to be transmitted
1534 * @tlen: The total length of the trailer
1535 *
Robert Love85b4aa42008-12-09 15:10:24 -08001536 * Returns: 0 for success
Robert Love34f42a02009-02-27 10:55:45 -08001537 */
Bhanu Prakash Gollapudi8597ae82011-01-28 16:05:37 -08001538static int fcoe_alloc_paged_crc_eof(struct sk_buff *skb, int tlen)
Robert Love85b4aa42008-12-09 15:10:24 -08001539{
1540 struct fcoe_percpu_s *fps;
Bhanu Prakash Gollapudi8597ae82011-01-28 16:05:37 -08001541 int rc;
Robert Love85b4aa42008-12-09 15:10:24 -08001542
Robert Love5e5e92d2009-03-17 11:41:35 -07001543 fps = &get_cpu_var(fcoe_percpu);
Bhanu Prakash Gollapudi8597ae82011-01-28 16:05:37 -08001544 rc = fcoe_get_paged_crc_eof(skb, tlen, fps);
Robert Love5e5e92d2009-03-17 11:41:35 -07001545 put_cpu_var(fcoe_percpu);
Robert Love85b4aa42008-12-09 15:10:24 -08001546
Bhanu Prakash Gollapudi8597ae82011-01-28 16:05:37 -08001547 return rc;
Robert Love85b4aa42008-12-09 15:10:24 -08001548}
Robert Love85b4aa42008-12-09 15:10:24 -08001549
1550/**
Robert Love1875f272009-11-03 11:47:50 -08001551 * fcoe_xmit() - Transmit a FCoE frame
1552 * @lport: The local port that the frame is to be transmitted for
1553 * @fp: The frame to be transmitted
Robert Love85b4aa42008-12-09 15:10:24 -08001554 *
Robert Love1875f272009-11-03 11:47:50 -08001555 * Return: 0 for success
Robert Love34f42a02009-02-27 10:55:45 -08001556 */
Bart Van Assche7c9c6842012-01-13 17:26:25 -08001557static int fcoe_xmit(struct fc_lport *lport, struct fc_frame *fp)
Robert Love85b4aa42008-12-09 15:10:24 -08001558{
Vasu Dev4bb6b512009-05-06 10:52:34 -07001559 int wlen;
Robert Love85b4aa42008-12-09 15:10:24 -08001560 u32 crc;
1561 struct ethhdr *eh;
1562 struct fcoe_crc_eof *cp;
1563 struct sk_buff *skb;
1564 struct fcoe_dev_stats *stats;
1565 struct fc_frame_header *fh;
1566 unsigned int hlen; /* header length implies the version */
1567 unsigned int tlen; /* trailer length */
1568 unsigned int elen; /* eth header, may include vlan */
Robert Love1875f272009-11-03 11:47:50 -08001569 struct fcoe_port *port = lport_priv(lport);
Bhanu Prakash Gollapudi8597ae82011-01-28 16:05:37 -08001570 struct fcoe_interface *fcoe = port->priv;
Robert Love85b4aa42008-12-09 15:10:24 -08001571 u8 sof, eof;
1572 struct fcoe_hdr *hp;
1573
1574 WARN_ON((fr_len(fp) % sizeof(u32)) != 0);
1575
Robert Love85b4aa42008-12-09 15:10:24 -08001576 fh = fc_frame_header_get(fp);
Joe Eykholt97c83892009-03-17 11:42:40 -07001577 skb = fp_skb(fp);
1578 wlen = skb->len / FCOE_WORD_TO_BYTE;
1579
Robert Love1875f272009-11-03 11:47:50 -08001580 if (!lport->link_up) {
Dan Carpenter3caf02e2009-04-21 16:27:25 -07001581 kfree_skb(skb);
Joe Eykholt97c83892009-03-17 11:42:40 -07001582 return 0;
Robert Love85b4aa42008-12-09 15:10:24 -08001583 }
1584
Joe Eykholt9860eeb2010-03-12 16:07:52 -08001585 if (unlikely(fh->fh_type == FC_TYPE_ELS) &&
Robert Love1875f272009-11-03 11:47:50 -08001586 fcoe_ctlr_els_send(&fcoe->ctlr, lport, skb))
Joe Eykholt97c83892009-03-17 11:42:40 -07001587 return 0;
1588
Robert Love85b4aa42008-12-09 15:10:24 -08001589 sof = fr_sof(fp);
1590 eof = fr_eof(fp);
1591
Vasu Dev4e57e1c2009-05-06 10:52:46 -07001592 elen = sizeof(struct ethhdr);
Robert Love85b4aa42008-12-09 15:10:24 -08001593 hlen = sizeof(struct fcoe_hdr);
1594 tlen = sizeof(struct fcoe_crc_eof);
1595 wlen = (skb->len - tlen + sizeof(crc)) / FCOE_WORD_TO_BYTE;
1596
1597 /* crc offload */
Robert Love1875f272009-11-03 11:47:50 -08001598 if (likely(lport->crc_offload)) {
Yi Zou39ca9a02009-02-27 14:07:15 -08001599 skb->ip_summed = CHECKSUM_PARTIAL;
Robert Love85b4aa42008-12-09 15:10:24 -08001600 skb->csum_start = skb_headroom(skb);
1601 skb->csum_offset = skb->len;
1602 crc = 0;
1603 } else {
1604 skb->ip_summed = CHECKSUM_NONE;
1605 crc = fcoe_fc_crc(fp);
1606 }
1607
Chris Leech014f5c32009-08-25 13:59:30 -07001608 /* copy port crc and eof to the skb buff */
Robert Love85b4aa42008-12-09 15:10:24 -08001609 if (skb_is_nonlinear(skb)) {
1610 skb_frag_t *frag;
Bhanu Prakash Gollapudi8597ae82011-01-28 16:05:37 -08001611 if (fcoe_alloc_paged_crc_eof(skb, tlen)) {
Roel Kluine9041582009-02-27 10:56:22 -08001612 kfree_skb(skb);
Robert Love85b4aa42008-12-09 15:10:24 -08001613 return -ENOMEM;
1614 }
1615 frag = &skb_shinfo(skb)->frags[skb_shinfo(skb)->nr_frags - 1];
Ian Campbell165c68d2011-08-24 22:28:15 +00001616 cp = kmap_atomic(skb_frag_page(frag), KM_SKB_DATA_SOFTIRQ)
Robert Love85b4aa42008-12-09 15:10:24 -08001617 + frag->page_offset;
1618 } else {
1619 cp = (struct fcoe_crc_eof *)skb_put(skb, tlen);
1620 }
1621
1622 memset(cp, 0, sizeof(*cp));
1623 cp->fcoe_eof = eof;
1624 cp->fcoe_crc32 = cpu_to_le32(~crc);
1625
1626 if (skb_is_nonlinear(skb)) {
1627 kunmap_atomic(cp, KM_SKB_DATA_SOFTIRQ);
1628 cp = NULL;
1629 }
1630
Chris Leech014f5c32009-08-25 13:59:30 -07001631 /* adjust skb network/transport offsets to match mac/fcoe/port */
Robert Love85b4aa42008-12-09 15:10:24 -08001632 skb_push(skb, elen + hlen);
1633 skb_reset_mac_header(skb);
1634 skb_reset_network_header(skb);
1635 skb->mac_len = elen;
Yi Zou211c7382009-02-27 14:06:37 -08001636 skb->protocol = htons(ETH_P_FCOE);
john fastabend6f6c2aa2011-11-18 13:35:56 -08001637 skb->priority = port->priority;
1638
Vasu Devd1483bb2011-09-27 21:38:13 -07001639 if (fcoe->netdev->priv_flags & IFF_802_1Q_VLAN &&
1640 fcoe->realdev->features & NETIF_F_HW_VLAN_TX) {
1641 skb->vlan_tci = VLAN_TAG_PRESENT |
1642 vlan_dev_vlan_id(fcoe->netdev);
1643 skb->dev = fcoe->realdev;
1644 } else
1645 skb->dev = fcoe->netdev;
Robert Love85b4aa42008-12-09 15:10:24 -08001646
1647 /* fill up mac and fcoe headers */
1648 eh = eth_hdr(skb);
1649 eh->h_proto = htons(ETH_P_FCOE);
Joe Eykholtcd229e42010-07-20 15:20:40 -07001650 memcpy(eh->h_dest, fcoe->ctlr.dest_addr, ETH_ALEN);
Chris Leech3fe9a0b2009-08-25 13:59:46 -07001651 if (fcoe->ctlr.map_dest)
Joe Eykholtcd229e42010-07-20 15:20:40 -07001652 memcpy(eh->h_dest + 3, fh->fh_d_id, 3);
Robert Love85b4aa42008-12-09 15:10:24 -08001653
Chris Leech3fe9a0b2009-08-25 13:59:46 -07001654 if (unlikely(fcoe->ctlr.flogi_oxid != FC_XID_UNKNOWN))
1655 memcpy(eh->h_source, fcoe->ctlr.ctl_src_addr, ETH_ALEN);
Robert Love85b4aa42008-12-09 15:10:24 -08001656 else
Chris Leech11b56182009-11-03 11:46:29 -08001657 memcpy(eh->h_source, port->data_src_addr, ETH_ALEN);
Robert Love85b4aa42008-12-09 15:10:24 -08001658
1659 hp = (struct fcoe_hdr *)(eh + 1);
1660 memset(hp, 0, sizeof(*hp));
1661 if (FC_FCOE_VER)
1662 FC_FCOE_ENCAPS_VER(hp, FC_FCOE_VER);
1663 hp->fcoe_sof = sof;
1664
Yi Zou39ca9a02009-02-27 14:07:15 -08001665 /* fcoe lso, mss is in max_payload which is non-zero for FCP data */
Robert Love1875f272009-11-03 11:47:50 -08001666 if (lport->seq_offload && fr_max_payload(fp)) {
Yi Zou39ca9a02009-02-27 14:07:15 -08001667 skb_shinfo(skb)->gso_type = SKB_GSO_FCOE;
1668 skb_shinfo(skb)->gso_size = fr_max_payload(fp);
1669 } else {
1670 skb_shinfo(skb)->gso_type = 0;
1671 skb_shinfo(skb)->gso_size = 0;
1672 }
Robert Love85b4aa42008-12-09 15:10:24 -08001673 /* update tx stats: regardless if LLD fails */
Joe Eykholtf018b732010-03-12 16:08:55 -08001674 stats = per_cpu_ptr(lport->dev_stats, get_cpu());
Robert Love582b45b2009-03-31 15:51:50 -07001675 stats->TxFrames++;
1676 stats->TxWords += wlen;
Joe Eykholtf018b732010-03-12 16:08:55 -08001677 put_cpu();
Robert Love85b4aa42008-12-09 15:10:24 -08001678
1679 /* send down to lld */
Robert Love1875f272009-11-03 11:47:50 -08001680 fr_dev(fp) = lport;
Vasu Dev980f5152011-07-27 15:11:05 -07001681 fcoe_port_send(port, skb);
Robert Love85b4aa42008-12-09 15:10:24 -08001682 return 0;
1683}
Robert Love85b4aa42008-12-09 15:10:24 -08001684
Robert Love34f42a02009-02-27 10:55:45 -08001685/**
Robert Love1875f272009-11-03 11:47:50 -08001686 * fcoe_percpu_flush_done() - Indicate per-CPU queue flush completion
1687 * @skb: The completed skb (argument required by destructor)
Joe Eykholte7a51992009-08-25 14:04:08 -07001688 */
1689static void fcoe_percpu_flush_done(struct sk_buff *skb)
1690{
1691 complete(&fcoe_flush_completion);
1692}
1693
1694/**
Vasu Dev52ee83212011-01-28 16:03:52 -08001695 * fcoe_filter_frames() - filter out bad fcoe frames, i.e. bad CRC
1696 * @lport: The local port the frame was received on
1697 * @fp: The received frame
1698 *
1699 * Return: 0 on passing filtering checks
1700 */
1701static inline int fcoe_filter_frames(struct fc_lport *lport,
1702 struct fc_frame *fp)
1703{
1704 struct fcoe_interface *fcoe;
1705 struct fc_frame_header *fh;
1706 struct sk_buff *skb = (struct sk_buff *)fp;
1707 struct fcoe_dev_stats *stats;
1708
1709 /*
1710 * We only check CRC if no offload is available and if it is
1711 * it's solicited data, in which case, the FCP layer would
1712 * check it during the copy.
1713 */
1714 if (lport->crc_offload && skb->ip_summed == CHECKSUM_UNNECESSARY)
1715 fr_flags(fp) &= ~FCPHF_CRC_UNCHECKED;
1716 else
1717 fr_flags(fp) |= FCPHF_CRC_UNCHECKED;
1718
1719 fh = (struct fc_frame_header *) skb_transport_header(skb);
1720 fh = fc_frame_header_get(fp);
1721 if (fh->fh_r_ctl == FC_RCTL_DD_SOL_DATA && fh->fh_type == FC_TYPE_FCP)
1722 return 0;
1723
Bhanu Prakash Gollapudi8597ae82011-01-28 16:05:37 -08001724 fcoe = ((struct fcoe_port *)lport_priv(lport))->priv;
Vasu Dev52ee83212011-01-28 16:03:52 -08001725 if (is_fip_mode(&fcoe->ctlr) && fc_frame_payload_op(fp) == ELS_LOGO &&
1726 ntoh24(fh->fh_s_id) == FC_FID_FLOGI) {
1727 FCOE_DBG("fcoe: dropping FCoE lport LOGO in fip mode\n");
1728 return -EINVAL;
1729 }
1730
Dan Carpenterf2f96d22011-02-25 15:03:23 -08001731 if (!(fr_flags(fp) & FCPHF_CRC_UNCHECKED) ||
Vasu Dev52ee83212011-01-28 16:03:52 -08001732 le32_to_cpu(fr_crc(fp)) == ~crc32(~0, skb->data, skb->len)) {
1733 fr_flags(fp) &= ~FCPHF_CRC_UNCHECKED;
1734 return 0;
1735 }
1736
1737 stats = per_cpu_ptr(lport->dev_stats, get_cpu());
1738 stats->InvalidCRCCount++;
1739 if (stats->InvalidCRCCount < 5)
1740 printk(KERN_WARNING "fcoe: dropping frame with CRC error\n");
Thomas Gleixner7e1e7ea2011-11-11 20:52:01 +01001741 put_cpu();
Vasu Dev52ee83212011-01-28 16:03:52 -08001742 return -EINVAL;
1743}
1744
1745/**
Chris Leech859b7b62009-11-20 14:54:47 -08001746 * fcoe_recv_frame() - process a single received frame
1747 * @skb: frame to process
1748 */
1749static void fcoe_recv_frame(struct sk_buff *skb)
1750{
1751 u32 fr_len;
1752 struct fc_lport *lport;
1753 struct fcoe_rcv_info *fr;
1754 struct fcoe_dev_stats *stats;
Chris Leech859b7b62009-11-20 14:54:47 -08001755 struct fcoe_crc_eof crc_eof;
1756 struct fc_frame *fp;
Chris Leech859b7b62009-11-20 14:54:47 -08001757 struct fcoe_port *port;
1758 struct fcoe_hdr *hp;
1759
1760 fr = fcoe_dev_from_skb(skb);
1761 lport = fr->fr_dev;
1762 if (unlikely(!lport)) {
1763 if (skb->destructor != fcoe_percpu_flush_done)
1764 FCOE_NETDEV_DBG(skb->dev, "NULL lport in skb");
1765 kfree_skb(skb);
1766 return;
1767 }
1768
1769 FCOE_NETDEV_DBG(skb->dev, "skb_info: len:%d data_len:%d "
1770 "head:%p data:%p tail:%p end:%p sum:%d dev:%s",
1771 skb->len, skb->data_len,
1772 skb->head, skb->data, skb_tail_pointer(skb),
1773 skb_end_pointer(skb), skb->csum,
1774 skb->dev ? skb->dev->name : "<NULL>");
1775
Chris Leech859b7b62009-11-20 14:54:47 -08001776 port = lport_priv(lport);
Robert Lovef1633012011-12-16 14:24:49 -08001777 skb_linearize(skb); /* check for skb_is_nonlinear is within skb_linearize */
Chris Leech859b7b62009-11-20 14:54:47 -08001778
1779 /*
1780 * Frame length checks and setting up the header pointers
1781 * was done in fcoe_rcv already.
1782 */
1783 hp = (struct fcoe_hdr *) skb_network_header(skb);
Chris Leech859b7b62009-11-20 14:54:47 -08001784
Joe Eykholtf018b732010-03-12 16:08:55 -08001785 stats = per_cpu_ptr(lport->dev_stats, get_cpu());
Chris Leech859b7b62009-11-20 14:54:47 -08001786 if (unlikely(FC_FCOE_DECAPS_VER(hp) != FC_FCOE_VER)) {
1787 if (stats->ErrorFrames < 5)
1788 printk(KERN_WARNING "fcoe: FCoE version "
1789 "mismatch: The frame has "
1790 "version %x, but the "
1791 "initiator supports version "
1792 "%x\n", FC_FCOE_DECAPS_VER(hp),
1793 FC_FCOE_VER);
Joe Eykholtf018b732010-03-12 16:08:55 -08001794 goto drop;
Chris Leech859b7b62009-11-20 14:54:47 -08001795 }
1796
1797 skb_pull(skb, sizeof(struct fcoe_hdr));
1798 fr_len = skb->len - sizeof(struct fcoe_crc_eof);
1799
1800 stats->RxFrames++;
1801 stats->RxWords += fr_len / FCOE_WORD_TO_BYTE;
1802
1803 fp = (struct fc_frame *)skb;
1804 fc_frame_init(fp);
1805 fr_dev(fp) = lport;
1806 fr_sof(fp) = hp->fcoe_sof;
1807
1808 /* Copy out the CRC and EOF trailer for access */
Joe Eykholtf018b732010-03-12 16:08:55 -08001809 if (skb_copy_bits(skb, fr_len, &crc_eof, sizeof(crc_eof)))
1810 goto drop;
Chris Leech859b7b62009-11-20 14:54:47 -08001811 fr_eof(fp) = crc_eof.fcoe_eof;
1812 fr_crc(fp) = crc_eof.fcoe_crc32;
Joe Eykholtf018b732010-03-12 16:08:55 -08001813 if (pskb_trim(skb, fr_len))
1814 goto drop;
Chris Leech859b7b62009-11-20 14:54:47 -08001815
Vasu Dev52ee83212011-01-28 16:03:52 -08001816 if (!fcoe_filter_frames(lport, fp)) {
1817 put_cpu();
1818 fc_exch_recv(lport, fp);
1819 return;
Chris Leech859b7b62009-11-20 14:54:47 -08001820 }
Joe Eykholtf018b732010-03-12 16:08:55 -08001821drop:
1822 stats->ErrorFrames++;
1823 put_cpu();
1824 kfree_skb(skb);
Chris Leech859b7b62009-11-20 14:54:47 -08001825}
1826
1827/**
Robert Love1875f272009-11-03 11:47:50 -08001828 * fcoe_percpu_receive_thread() - The per-CPU packet receive thread
1829 * @arg: The per-CPU context
Robert Love85b4aa42008-12-09 15:10:24 -08001830 *
1831 * Return: 0 for success
Robert Love85b4aa42008-12-09 15:10:24 -08001832 */
Bart Van Assche7c9c6842012-01-13 17:26:25 -08001833static int fcoe_percpu_receive_thread(void *arg)
Robert Love85b4aa42008-12-09 15:10:24 -08001834{
1835 struct fcoe_percpu_s *p = arg;
Robert Love85b4aa42008-12-09 15:10:24 -08001836 struct sk_buff *skb;
Robert Love85b4aa42008-12-09 15:10:24 -08001837
Robert Love4469c192009-02-27 10:56:38 -08001838 set_user_nice(current, -20);
Robert Love85b4aa42008-12-09 15:10:24 -08001839
1840 while (!kthread_should_stop()) {
1841
1842 spin_lock_bh(&p->fcoe_rx_list.lock);
1843 while ((skb = __skb_dequeue(&p->fcoe_rx_list)) == NULL) {
1844 set_current_state(TASK_INTERRUPTIBLE);
1845 spin_unlock_bh(&p->fcoe_rx_list.lock);
1846 schedule();
1847 set_current_state(TASK_RUNNING);
1848 if (kthread_should_stop())
1849 return 0;
1850 spin_lock_bh(&p->fcoe_rx_list.lock);
1851 }
1852 spin_unlock_bh(&p->fcoe_rx_list.lock);
Chris Leech859b7b62009-11-20 14:54:47 -08001853 fcoe_recv_frame(skb);
Robert Love85b4aa42008-12-09 15:10:24 -08001854 }
1855 return 0;
1856}
1857
1858/**
Robert Love1875f272009-11-03 11:47:50 -08001859 * fcoe_dev_setup() - Setup the link change notification interface
Robert Love34f42a02009-02-27 10:55:45 -08001860 */
Randy Dunlapb0d428a2009-04-27 21:49:31 -07001861static void fcoe_dev_setup(void)
Robert Love85b4aa42008-12-09 15:10:24 -08001862{
john fastabend6f6c2aa2011-11-18 13:35:56 -08001863 register_dcbevent_notifier(&dcb_notifier);
Robert Love85b4aa42008-12-09 15:10:24 -08001864 register_netdevice_notifier(&fcoe_notifier);
1865}
1866
1867/**
Robert Love1875f272009-11-03 11:47:50 -08001868 * fcoe_dev_cleanup() - Cleanup the link change notification interface
Robert Love34f42a02009-02-27 10:55:45 -08001869 */
Robert Love85b4aa42008-12-09 15:10:24 -08001870static void fcoe_dev_cleanup(void)
1871{
john fastabend6f6c2aa2011-11-18 13:35:56 -08001872 unregister_dcbevent_notifier(&dcb_notifier);
Robert Love85b4aa42008-12-09 15:10:24 -08001873 unregister_netdevice_notifier(&fcoe_notifier);
1874}
1875
john fastabend6f6c2aa2011-11-18 13:35:56 -08001876static struct fcoe_interface *
1877fcoe_hostlist_lookup_realdev_port(struct net_device *netdev)
1878{
1879 struct fcoe_interface *fcoe;
1880 struct net_device *real_dev;
1881
1882 list_for_each_entry(fcoe, &fcoe_hostlist, list) {
1883 if (fcoe->netdev->priv_flags & IFF_802_1Q_VLAN)
1884 real_dev = vlan_dev_real_dev(fcoe->netdev);
1885 else
1886 real_dev = fcoe->netdev;
1887
1888 if (netdev == real_dev)
1889 return fcoe;
1890 }
1891 return NULL;
1892}
1893
1894static int fcoe_dcb_app_notification(struct notifier_block *notifier,
1895 ulong event, void *ptr)
1896{
1897 struct dcb_app_type *entry = ptr;
1898 struct fcoe_interface *fcoe;
1899 struct net_device *netdev;
1900 struct fcoe_port *port;
1901 int prio;
1902
1903 if (entry->app.selector != DCB_APP_IDTYPE_ETHTYPE)
1904 return NOTIFY_OK;
1905
1906 netdev = dev_get_by_index(&init_net, entry->ifindex);
1907 if (!netdev)
1908 return NOTIFY_OK;
1909
1910 fcoe = fcoe_hostlist_lookup_realdev_port(netdev);
1911 dev_put(netdev);
1912 if (!fcoe)
1913 return NOTIFY_OK;
1914
1915 if (entry->dcbx & DCB_CAP_DCBX_VER_CEE)
1916 prio = ffs(entry->app.priority) - 1;
1917 else
1918 prio = entry->app.priority;
1919
1920 if (prio < 0)
1921 return NOTIFY_OK;
1922
1923 if (entry->app.protocol == ETH_P_FIP ||
1924 entry->app.protocol == ETH_P_FCOE)
1925 fcoe->ctlr.priority = prio;
1926
1927 if (entry->app.protocol == ETH_P_FCOE) {
1928 port = lport_priv(fcoe->ctlr.lp);
1929 port->priority = prio;
1930 }
1931
1932 return NOTIFY_OK;
1933}
1934
Robert Love85b4aa42008-12-09 15:10:24 -08001935/**
Robert Love1875f272009-11-03 11:47:50 -08001936 * fcoe_device_notification() - Handler for net device events
1937 * @notifier: The context of the notification
1938 * @event: The type of event
1939 * @ptr: The net device that the event was on
Robert Love85b4aa42008-12-09 15:10:24 -08001940 *
Robert Love1875f272009-11-03 11:47:50 -08001941 * This function is called by the Ethernet driver in case of link change event.
Robert Love85b4aa42008-12-09 15:10:24 -08001942 *
1943 * Returns: 0 for success
Robert Love34f42a02009-02-27 10:55:45 -08001944 */
Robert Love85b4aa42008-12-09 15:10:24 -08001945static int fcoe_device_notification(struct notifier_block *notifier,
1946 ulong event, void *ptr)
1947{
Robert Love1875f272009-11-03 11:47:50 -08001948 struct fc_lport *lport = NULL;
Vasu Dev1d1b88d2009-07-29 17:05:45 -07001949 struct net_device *netdev = ptr;
Chris Leech014f5c32009-08-25 13:59:30 -07001950 struct fcoe_interface *fcoe;
Chris Leech2e70e242009-08-25 14:00:23 -07001951 struct fcoe_port *port;
Robert Love85b4aa42008-12-09 15:10:24 -08001952 struct fcoe_dev_stats *stats;
Joe Eykholt97c83892009-03-17 11:42:40 -07001953 u32 link_possible = 1;
Robert Love85b4aa42008-12-09 15:10:24 -08001954 u32 mfs;
1955 int rc = NOTIFY_OK;
1956
Chris Leech014f5c32009-08-25 13:59:30 -07001957 list_for_each_entry(fcoe, &fcoe_hostlist, list) {
Chris Leech25024982009-08-25 13:59:35 -07001958 if (fcoe->netdev == netdev) {
Robert Love1875f272009-11-03 11:47:50 -08001959 lport = fcoe->ctlr.lp;
Robert Love85b4aa42008-12-09 15:10:24 -08001960 break;
1961 }
1962 }
Robert Love1875f272009-11-03 11:47:50 -08001963 if (!lport) {
Robert Love85b4aa42008-12-09 15:10:24 -08001964 rc = NOTIFY_DONE;
1965 goto out;
1966 }
1967
Robert Love85b4aa42008-12-09 15:10:24 -08001968 switch (event) {
1969 case NETDEV_DOWN:
1970 case NETDEV_GOING_DOWN:
Joe Eykholt97c83892009-03-17 11:42:40 -07001971 link_possible = 0;
Robert Love85b4aa42008-12-09 15:10:24 -08001972 break;
1973 case NETDEV_UP:
1974 case NETDEV_CHANGE:
Robert Love85b4aa42008-12-09 15:10:24 -08001975 break;
1976 case NETDEV_CHANGEMTU:
Yi Zou7221d7e2009-10-21 16:27:52 -07001977 if (netdev->features & NETIF_F_FCOE_MTU)
1978 break;
Vasu Dev1d1b88d2009-07-29 17:05:45 -07001979 mfs = netdev->mtu - (sizeof(struct fcoe_hdr) +
1980 sizeof(struct fcoe_crc_eof));
Robert Love85b4aa42008-12-09 15:10:24 -08001981 if (mfs >= FC_MIN_MAX_FRAME)
Robert Love1875f272009-11-03 11:47:50 -08001982 fc_set_mfs(lport, mfs);
Robert Love85b4aa42008-12-09 15:10:24 -08001983 break;
1984 case NETDEV_REGISTER:
1985 break;
Chris Leech2e70e242009-08-25 14:00:23 -07001986 case NETDEV_UNREGISTER:
1987 list_del(&fcoe->list);
1988 port = lport_priv(fcoe->ctlr.lp);
Tejun Heo2ca32b42011-01-28 16:05:32 -08001989 queue_work(fcoe_wq, &port->destroy_work);
Chris Leech2e70e242009-08-25 14:00:23 -07001990 goto out;
1991 break;
Yi Zou54a5b212010-07-20 15:21:17 -07001992 case NETDEV_FEAT_CHANGE:
1993 fcoe_netdev_features_change(lport, netdev);
1994 break;
Robert Love85b4aa42008-12-09 15:10:24 -08001995 default:
Vasu Dev1d1b88d2009-07-29 17:05:45 -07001996 FCOE_NETDEV_DBG(netdev, "Unknown event %ld "
Robert Loved5488eb2009-06-10 15:30:59 -07001997 "from netdev netlink\n", event);
Robert Love85b4aa42008-12-09 15:10:24 -08001998 }
Robert Love5e4f8fe2010-05-07 15:18:35 -07001999
2000 fcoe_link_speed_update(lport);
2001
Robert Love1875f272009-11-03 11:47:50 -08002002 if (link_possible && !fcoe_link_ok(lport))
Chris Leech3fe9a0b2009-08-25 13:59:46 -07002003 fcoe_ctlr_link_up(&fcoe->ctlr);
2004 else if (fcoe_ctlr_link_down(&fcoe->ctlr)) {
Joe Eykholtf018b732010-03-12 16:08:55 -08002005 stats = per_cpu_ptr(lport->dev_stats, get_cpu());
Joe Eykholt97c83892009-03-17 11:42:40 -07002006 stats->LinkFailureCount++;
Joe Eykholtf018b732010-03-12 16:08:55 -08002007 put_cpu();
Robert Love1875f272009-11-03 11:47:50 -08002008 fcoe_clean_pending_queue(lport);
Robert Love85b4aa42008-12-09 15:10:24 -08002009 }
2010out:
2011 return rc;
2012}
2013
2014/**
Vasu Dev55a66d32009-12-10 09:59:31 -08002015 * fcoe_disable() - Disables a FCoE interface
Yi Zou78a58242011-01-28 16:05:16 -08002016 * @netdev : The net_device object the Ethernet interface to create on
Vasu Dev55a66d32009-12-10 09:59:31 -08002017 *
Yi Zou78a58242011-01-28 16:05:16 -08002018 * Called from fcoe transport.
Vasu Dev55a66d32009-12-10 09:59:31 -08002019 *
2020 * Returns: 0 for success
2021 */
Yi Zou78a58242011-01-28 16:05:16 -08002022static int fcoe_disable(struct net_device *netdev)
Vasu Dev55a66d32009-12-10 09:59:31 -08002023{
2024 struct fcoe_interface *fcoe;
Vasu Dev55a66d32009-12-10 09:59:31 -08002025 int rc = 0;
2026
2027 mutex_lock(&fcoe_config_mutex);
Vasu Dev55a66d32009-12-10 09:59:31 -08002028
Robert Loveee5df622011-04-01 16:05:59 -07002029 rtnl_lock();
Vasu Dev55a66d32009-12-10 09:59:31 -08002030 fcoe = fcoe_hostlist_lookup_port(netdev);
2031 rtnl_unlock();
2032
Chris Leech9ee50e42010-04-09 14:22:23 -07002033 if (fcoe) {
Chris Leech9ee50e42010-04-09 14:22:23 -07002034 fcoe_ctlr_link_down(&fcoe->ctlr);
Vasu Dev9d4cbc02010-07-20 15:19:26 -07002035 fcoe_clean_pending_queue(fcoe->ctlr.lp);
Chris Leech9ee50e42010-04-09 14:22:23 -07002036 } else
Vasu Dev55a66d32009-12-10 09:59:31 -08002037 rc = -ENODEV;
2038
Vasu Dev55a66d32009-12-10 09:59:31 -08002039 mutex_unlock(&fcoe_config_mutex);
2040 return rc;
2041}
2042
2043/**
2044 * fcoe_enable() - Enables a FCoE interface
Yi Zou78a58242011-01-28 16:05:16 -08002045 * @netdev : The net_device object the Ethernet interface to create on
Vasu Dev55a66d32009-12-10 09:59:31 -08002046 *
Yi Zou78a58242011-01-28 16:05:16 -08002047 * Called from fcoe transport.
Vasu Dev55a66d32009-12-10 09:59:31 -08002048 *
2049 * Returns: 0 for success
2050 */
Yi Zou78a58242011-01-28 16:05:16 -08002051static int fcoe_enable(struct net_device *netdev)
Vasu Dev55a66d32009-12-10 09:59:31 -08002052{
2053 struct fcoe_interface *fcoe;
Vasu Dev55a66d32009-12-10 09:59:31 -08002054 int rc = 0;
2055
2056 mutex_lock(&fcoe_config_mutex);
Robert Loveee5df622011-04-01 16:05:59 -07002057 rtnl_lock();
Vasu Dev55a66d32009-12-10 09:59:31 -08002058 fcoe = fcoe_hostlist_lookup_port(netdev);
2059 rtnl_unlock();
2060
Vasu Dev9d4cbc02010-07-20 15:19:26 -07002061 if (!fcoe)
Vasu Dev55a66d32009-12-10 09:59:31 -08002062 rc = -ENODEV;
Vasu Dev9d4cbc02010-07-20 15:19:26 -07002063 else if (!fcoe_link_ok(fcoe->ctlr.lp))
2064 fcoe_ctlr_link_up(&fcoe->ctlr);
Vasu Dev55a66d32009-12-10 09:59:31 -08002065
Vasu Dev55a66d32009-12-10 09:59:31 -08002066 mutex_unlock(&fcoe_config_mutex);
2067 return rc;
2068}
2069
2070/**
Robert Love1875f272009-11-03 11:47:50 -08002071 * fcoe_destroy() - Destroy a FCoE interface
Yi Zou78a58242011-01-28 16:05:16 -08002072 * @netdev : The net_device object the Ethernet interface to create on
Robert Love1875f272009-11-03 11:47:50 -08002073 *
Yi Zou78a58242011-01-28 16:05:16 -08002074 * Called from fcoe transport
Robert Love85b4aa42008-12-09 15:10:24 -08002075 *
2076 * Returns: 0 for success
Robert Love34f42a02009-02-27 10:55:45 -08002077 */
Yi Zou78a58242011-01-28 16:05:16 -08002078static int fcoe_destroy(struct net_device *netdev)
Robert Love85b4aa42008-12-09 15:10:24 -08002079{
Chris Leech030f4e02009-08-25 14:00:02 -07002080 struct fcoe_interface *fcoe;
Vasu Devf04ca1b2011-04-01 16:06:45 -07002081 struct fc_lport *lport;
Neerav Parikhb2085a42011-06-20 16:59:51 -07002082 struct fcoe_port *port;
Mike Christie8eca3552009-10-21 16:27:44 -07002083 int rc = 0;
Robert Love85b4aa42008-12-09 15:10:24 -08002084
Chris Leechdfc1d0f2009-08-25 14:00:13 -07002085 mutex_lock(&fcoe_config_mutex);
Robert Loveee5df622011-04-01 16:05:59 -07002086 rtnl_lock();
Chris Leech2e70e242009-08-25 14:00:23 -07002087 fcoe = fcoe_hostlist_lookup_port(netdev);
2088 if (!fcoe) {
Robert Love85b4aa42008-12-09 15:10:24 -08002089 rc = -ENODEV;
Yi Zou78a58242011-01-28 16:05:16 -08002090 goto out_nodev;
Robert Love85b4aa42008-12-09 15:10:24 -08002091 }
Vasu Devf04ca1b2011-04-01 16:06:45 -07002092 lport = fcoe->ctlr.lp;
Neerav Parikhb2085a42011-06-20 16:59:51 -07002093 port = lport_priv(lport);
Yi Zou54a5b212010-07-20 15:21:17 -07002094 list_del(&fcoe->list);
Neerav Parikhb2085a42011-06-20 16:59:51 -07002095 queue_work(fcoe_wq, &port->destroy_work);
Robert Love85b4aa42008-12-09 15:10:24 -08002096out_nodev:
Neerav Parikhb2085a42011-06-20 16:59:51 -07002097 rtnl_unlock();
Chris Leechdfc1d0f2009-08-25 14:00:13 -07002098 mutex_unlock(&fcoe_config_mutex);
Robert Love85b4aa42008-12-09 15:10:24 -08002099 return rc;
2100}
2101
Robert Love1875f272009-11-03 11:47:50 -08002102/**
2103 * fcoe_destroy_work() - Destroy a FCoE port in a deferred work context
2104 * @work: Handle to the FCoE port to be destroyed
2105 */
Chris Leech2e70e242009-08-25 14:00:23 -07002106static void fcoe_destroy_work(struct work_struct *work)
2107{
2108 struct fcoe_port *port;
Neerav Parikhb2085a42011-06-20 16:59:51 -07002109 struct fcoe_interface *fcoe;
Chris Leech2e70e242009-08-25 14:00:23 -07002110
2111 port = container_of(work, struct fcoe_port, destroy_work);
2112 mutex_lock(&fcoe_config_mutex);
Neerav Parikhb2085a42011-06-20 16:59:51 -07002113
Neerav Parikhb2085a42011-06-20 16:59:51 -07002114 fcoe = port->priv;
Chris Leech2e70e242009-08-25 14:00:23 -07002115 fcoe_if_destroy(port->lport);
Neerav Parikhb2085a42011-06-20 16:59:51 -07002116
Robert Loveccefd232012-02-10 17:18:41 -08002117 fcoe_interface_cleanup(fcoe);
Neerav Parikhb2085a42011-06-20 16:59:51 -07002118
Chris Leech2e70e242009-08-25 14:00:23 -07002119 mutex_unlock(&fcoe_config_mutex);
2120}
2121
Robert Love85b4aa42008-12-09 15:10:24 -08002122/**
Yi Zou78a58242011-01-28 16:05:16 -08002123 * fcoe_match() - Check if the FCoE is supported on the given netdevice
2124 * @netdev : The net_device object the Ethernet interface to create on
Robert Love1875f272009-11-03 11:47:50 -08002125 *
Yi Zou78a58242011-01-28 16:05:16 -08002126 * Called from fcoe transport.
2127 *
2128 * Returns: always returns true as this is the default FCoE transport,
2129 * i.e., support all netdevs.
2130 */
2131static bool fcoe_match(struct net_device *netdev)
2132{
2133 return true;
2134}
2135
2136/**
john fastabend6f6c2aa2011-11-18 13:35:56 -08002137 * fcoe_dcb_create() - Initialize DCB attributes and hooks
2138 * @netdev: The net_device object of the L2 link that should be queried
2139 * @port: The fcoe_port to bind FCoE APP priority with
2140 * @
2141 */
2142static void fcoe_dcb_create(struct fcoe_interface *fcoe)
2143{
2144#ifdef CONFIG_DCB
2145 int dcbx;
2146 u8 fup, up;
2147 struct net_device *netdev = fcoe->realdev;
2148 struct fcoe_port *port = lport_priv(fcoe->ctlr.lp);
2149 struct dcb_app app = {
2150 .priority = 0,
2151 .protocol = ETH_P_FCOE
2152 };
2153
2154 /* setup DCB priority attributes. */
2155 if (netdev && netdev->dcbnl_ops && netdev->dcbnl_ops->getdcbx) {
2156 dcbx = netdev->dcbnl_ops->getdcbx(netdev);
2157
2158 if (dcbx & DCB_CAP_DCBX_VER_IEEE) {
2159 app.selector = IEEE_8021QAZ_APP_SEL_ETHERTYPE;
2160 up = dcb_ieee_getapp_mask(netdev, &app);
2161 app.protocol = ETH_P_FIP;
2162 fup = dcb_ieee_getapp_mask(netdev, &app);
2163 } else {
2164 app.selector = DCB_APP_IDTYPE_ETHTYPE;
2165 up = dcb_getapp(netdev, &app);
2166 app.protocol = ETH_P_FIP;
2167 fup = dcb_getapp(netdev, &app);
2168 }
2169
2170 port->priority = ffs(up) ? ffs(up) - 1 : 0;
2171 fcoe->ctlr.priority = ffs(fup) ? ffs(fup) - 1 : port->priority;
2172 }
2173#endif
2174}
2175
2176/**
Yi Zou78a58242011-01-28 16:05:16 -08002177 * fcoe_create() - Create a fcoe interface
2178 * @netdev : The net_device object the Ethernet interface to create on
2179 * @fip_mode: The FIP mode for this creation
2180 *
2181 * Called from fcoe transport
Robert Love85b4aa42008-12-09 15:10:24 -08002182 *
2183 * Returns: 0 for success
Robert Love34f42a02009-02-27 10:55:45 -08002184 */
Yi Zou78a58242011-01-28 16:05:16 -08002185static int fcoe_create(struct net_device *netdev, enum fip_state fip_mode)
Robert Love85b4aa42008-12-09 15:10:24 -08002186{
Neerav Parikhb2085a42011-06-20 16:59:51 -07002187 int rc = 0;
Chris Leech030f4e02009-08-25 14:00:02 -07002188 struct fcoe_interface *fcoe;
Chris Leechaf7f85d2009-08-25 13:59:24 -07002189 struct fc_lport *lport;
Robert Love85b4aa42008-12-09 15:10:24 -08002190
Chris Leechdfc1d0f2009-08-25 14:00:13 -07002191 mutex_lock(&fcoe_config_mutex);
Robert Loveee5df622011-04-01 16:05:59 -07002192 rtnl_lock();
Vasu Dev34ce27b2010-05-07 15:18:46 -07002193
Robert Love85b4aa42008-12-09 15:10:24 -08002194 /* look for existing lport */
2195 if (fcoe_hostlist_lookup(netdev)) {
2196 rc = -EEXIST;
Yi Zou78a58242011-01-28 16:05:16 -08002197 goto out_nodev;
Robert Love85b4aa42008-12-09 15:10:24 -08002198 }
Robert Love85b4aa42008-12-09 15:10:24 -08002199
Joe Eykholt1dd454d2010-07-20 15:20:46 -07002200 fcoe = fcoe_interface_create(netdev, fip_mode);
Robert Love7287fb92011-01-28 16:03:47 -08002201 if (IS_ERR(fcoe)) {
2202 rc = PTR_ERR(fcoe);
Yi Zou78a58242011-01-28 16:05:16 -08002203 goto out_nodev;
Chris Leech030f4e02009-08-25 14:00:02 -07002204 }
2205
Chris Leech9a057532009-11-03 11:46:40 -08002206 lport = fcoe_if_create(fcoe, &netdev->dev, 0);
Chris Leechaf7f85d2009-08-25 13:59:24 -07002207 if (IS_ERR(lport)) {
Robert Loved5488eb2009-06-10 15:30:59 -07002208 printk(KERN_ERR "fcoe: Failed to create interface (%s)\n",
Robert Love85b4aa42008-12-09 15:10:24 -08002209 netdev->name);
Robert Love85b4aa42008-12-09 15:10:24 -08002210 rc = -EIO;
Robert Love848e7d52011-08-25 12:40:47 -07002211 rtnl_unlock();
Chris Leech2e70e242009-08-25 14:00:23 -07002212 fcoe_interface_cleanup(fcoe);
Robert Love848e7d52011-08-25 12:40:47 -07002213 goto out_nortnl;
Robert Love85b4aa42008-12-09 15:10:24 -08002214 }
Chris Leech030f4e02009-08-25 14:00:02 -07002215
Chris Leech54b649f2009-08-25 14:00:07 -07002216 /* Make this the "master" N_Port */
2217 fcoe->ctlr.lp = lport;
Chris Leech030f4e02009-08-25 14:00:02 -07002218
john fastabend6f6c2aa2011-11-18 13:35:56 -08002219 /* setup DCB priority attributes. */
2220 fcoe_dcb_create(fcoe);
2221
Chris Leechc863df32009-08-25 14:00:18 -07002222 /* add to lports list */
2223 fcoe_hostlist_add(lport);
2224
Chris Leech54b649f2009-08-25 14:00:07 -07002225 /* start FIP Discovery and FLOGI */
2226 lport->boot_time = jiffies;
2227 fc_fabric_login(lport);
2228 if (!fcoe_link_ok(lport))
2229 fcoe_ctlr_link_up(&fcoe->ctlr);
2230
Robert Love85b4aa42008-12-09 15:10:24 -08002231out_nodev:
Vasu Dev34ce27b2010-05-07 15:18:46 -07002232 rtnl_unlock();
Robert Love848e7d52011-08-25 12:40:47 -07002233out_nortnl:
Chris Leechdfc1d0f2009-08-25 14:00:13 -07002234 mutex_unlock(&fcoe_config_mutex);
Robert Love85b4aa42008-12-09 15:10:24 -08002235 return rc;
2236}
2237
Robert Love34f42a02009-02-27 10:55:45 -08002238/**
Robert Love5e4f8fe2010-05-07 15:18:35 -07002239 * fcoe_link_speed_update() - Update the supported and actual link speeds
2240 * @lport: The local port to update speeds for
Robert Love85b4aa42008-12-09 15:10:24 -08002241 *
Robert Love5e4f8fe2010-05-07 15:18:35 -07002242 * Returns: 0 if the ethtool query was successful
2243 * -1 if the ethtool query failed
Robert Love85b4aa42008-12-09 15:10:24 -08002244 */
Bart Van Assche7c9c6842012-01-13 17:26:25 -08002245static int fcoe_link_speed_update(struct fc_lport *lport)
Robert Love85b4aa42008-12-09 15:10:24 -08002246{
Bhanu Prakash Gollapudi8597ae82011-01-28 16:05:37 -08002247 struct net_device *netdev = fcoe_netdev(lport);
David Decotigny8ae6dac2011-04-27 18:32:38 +00002248 struct ethtool_cmd ecmd;
Robert Love85b4aa42008-12-09 15:10:24 -08002249
Jiri Pirko4bc71cb2011-09-03 03:34:30 +00002250 if (!__ethtool_get_settings(netdev, &ecmd)) {
Robert Love1875f272009-11-03 11:47:50 -08002251 lport->link_supported_speeds &=
Yi Zou2f718d62009-07-29 17:04:01 -07002252 ~(FC_PORTSPEED_1GBIT | FC_PORTSPEED_10GBIT);
2253 if (ecmd.supported & (SUPPORTED_1000baseT_Half |
2254 SUPPORTED_1000baseT_Full))
Robert Love1875f272009-11-03 11:47:50 -08002255 lport->link_supported_speeds |= FC_PORTSPEED_1GBIT;
Yi Zou2f718d62009-07-29 17:04:01 -07002256 if (ecmd.supported & SUPPORTED_10000baseT_Full)
Robert Love1875f272009-11-03 11:47:50 -08002257 lport->link_supported_speeds |=
Yi Zou2f718d62009-07-29 17:04:01 -07002258 FC_PORTSPEED_10GBIT;
David Decotigny8ae6dac2011-04-27 18:32:38 +00002259 switch (ethtool_cmd_speed(&ecmd)) {
2260 case SPEED_1000:
Robert Love1875f272009-11-03 11:47:50 -08002261 lport->link_speed = FC_PORTSPEED_1GBIT;
David Decotigny8ae6dac2011-04-27 18:32:38 +00002262 break;
2263 case SPEED_10000:
Robert Love1875f272009-11-03 11:47:50 -08002264 lport->link_speed = FC_PORTSPEED_10GBIT;
David Decotigny8ae6dac2011-04-27 18:32:38 +00002265 break;
2266 }
Yi Zou2f718d62009-07-29 17:04:01 -07002267 return 0;
2268 }
2269 return -1;
Robert Love85b4aa42008-12-09 15:10:24 -08002270}
Robert Love85b4aa42008-12-09 15:10:24 -08002271
Robert Love34f42a02009-02-27 10:55:45 -08002272/**
Robert Love5e4f8fe2010-05-07 15:18:35 -07002273 * fcoe_link_ok() - Check if the link is OK for a local port
2274 * @lport: The local port to check link on
2275 *
2276 * Returns: 0 if link is UP and OK, -1 if not
2277 *
2278 */
Bart Van Assche7c9c6842012-01-13 17:26:25 -08002279static int fcoe_link_ok(struct fc_lport *lport)
Robert Love5e4f8fe2010-05-07 15:18:35 -07002280{
Bhanu Prakash Gollapudi8597ae82011-01-28 16:05:37 -08002281 struct net_device *netdev = fcoe_netdev(lport);
Robert Love5e4f8fe2010-05-07 15:18:35 -07002282
2283 if (netif_oper_up(netdev))
2284 return 0;
2285 return -1;
2286}
2287
2288/**
Robert Love1875f272009-11-03 11:47:50 -08002289 * fcoe_percpu_clean() - Clear all pending skbs for an local port
2290 * @lport: The local port whose skbs are to be cleared
Joe Eykholte7a51992009-08-25 14:04:08 -07002291 *
2292 * Must be called with fcoe_create_mutex held to single-thread completion.
2293 *
2294 * This flushes the pending skbs by adding a new skb to each queue and
2295 * waiting until they are all freed. This assures us that not only are
2296 * there no packets that will be handled by the lport, but also that any
2297 * threads already handling packet have returned.
Robert Love85b4aa42008-12-09 15:10:24 -08002298 */
Bart Van Assche7c9c6842012-01-13 17:26:25 -08002299static void fcoe_percpu_clean(struct fc_lport *lport)
Robert Love85b4aa42008-12-09 15:10:24 -08002300{
Robert Love85b4aa42008-12-09 15:10:24 -08002301 struct fcoe_percpu_s *pp;
2302 struct fcoe_rcv_info *fr;
2303 struct sk_buff_head *list;
2304 struct sk_buff *skb, *next;
2305 struct sk_buff *head;
Robert Love5e5e92d2009-03-17 11:41:35 -07002306 unsigned int cpu;
Robert Love85b4aa42008-12-09 15:10:24 -08002307
Robert Love5e5e92d2009-03-17 11:41:35 -07002308 for_each_possible_cpu(cpu) {
2309 pp = &per_cpu(fcoe_percpu, cpu);
2310 spin_lock_bh(&pp->fcoe_rx_list.lock);
2311 list = &pp->fcoe_rx_list;
2312 head = list->next;
2313 for (skb = head; skb != (struct sk_buff *)list;
2314 skb = next) {
2315 next = skb->next;
2316 fr = fcoe_dev_from_skb(skb);
Robert Love1875f272009-11-03 11:47:50 -08002317 if (fr->fr_dev == lport) {
Robert Love5e5e92d2009-03-17 11:41:35 -07002318 __skb_unlink(skb, list);
2319 kfree_skb(skb);
Robert Love85b4aa42008-12-09 15:10:24 -08002320 }
Robert Love85b4aa42008-12-09 15:10:24 -08002321 }
Joe Eykholte7a51992009-08-25 14:04:08 -07002322
2323 if (!pp->thread || !cpu_online(cpu)) {
2324 spin_unlock_bh(&pp->fcoe_rx_list.lock);
2325 continue;
2326 }
2327
2328 skb = dev_alloc_skb(0);
2329 if (!skb) {
2330 spin_unlock_bh(&pp->fcoe_rx_list.lock);
2331 continue;
2332 }
2333 skb->destructor = fcoe_percpu_flush_done;
2334
2335 __skb_queue_tail(&pp->fcoe_rx_list, skb);
2336 if (pp->fcoe_rx_list.qlen == 1)
2337 wake_up_process(pp->thread);
Robert Love5e5e92d2009-03-17 11:41:35 -07002338 spin_unlock_bh(&pp->fcoe_rx_list.lock);
Joe Eykholte7a51992009-08-25 14:04:08 -07002339
2340 wait_for_completion(&fcoe_flush_completion);
Robert Love85b4aa42008-12-09 15:10:24 -08002341 }
2342}
Robert Love85b4aa42008-12-09 15:10:24 -08002343
2344/**
Robert Love1875f272009-11-03 11:47:50 -08002345 * fcoe_reset() - Reset a local port
2346 * @shost: The SCSI host associated with the local port to be reset
Robert Love85b4aa42008-12-09 15:10:24 -08002347 *
Robert Love1875f272009-11-03 11:47:50 -08002348 * Returns: Always 0 (return value required by FC transport template)
Robert Love85b4aa42008-12-09 15:10:24 -08002349 */
Bart Van Assche7c9c6842012-01-13 17:26:25 -08002350static int fcoe_reset(struct Scsi_Host *shost)
Robert Love85b4aa42008-12-09 15:10:24 -08002351{
2352 struct fc_lport *lport = shost_priv(shost);
Vasu Devd2f80952011-02-25 15:03:28 -08002353 struct fcoe_port *port = lport_priv(lport);
2354 struct fcoe_interface *fcoe = port->priv;
2355
2356 fcoe_ctlr_link_down(&fcoe->ctlr);
2357 fcoe_clean_pending_queue(fcoe->ctlr.lp);
2358 if (!fcoe_link_ok(fcoe->ctlr.lp))
2359 fcoe_ctlr_link_up(&fcoe->ctlr);
Robert Love85b4aa42008-12-09 15:10:24 -08002360 return 0;
2361}
Robert Love85b4aa42008-12-09 15:10:24 -08002362
Robert Love34f42a02009-02-27 10:55:45 -08002363/**
Robert Love1875f272009-11-03 11:47:50 -08002364 * fcoe_hostlist_lookup_port() - Find the FCoE interface associated with a net device
2365 * @netdev: The net device used as a key
Robert Love85b4aa42008-12-09 15:10:24 -08002366 *
Robert Love1875f272009-11-03 11:47:50 -08002367 * Locking: Must be called with the RNL mutex held.
2368 *
2369 * Returns: NULL or the FCoE interface
Robert Love85b4aa42008-12-09 15:10:24 -08002370 */
Chris Leech014f5c32009-08-25 13:59:30 -07002371static struct fcoe_interface *
Robert Love1875f272009-11-03 11:47:50 -08002372fcoe_hostlist_lookup_port(const struct net_device *netdev)
Robert Love85b4aa42008-12-09 15:10:24 -08002373{
Chris Leech014f5c32009-08-25 13:59:30 -07002374 struct fcoe_interface *fcoe;
Robert Love85b4aa42008-12-09 15:10:24 -08002375
Chris Leech014f5c32009-08-25 13:59:30 -07002376 list_for_each_entry(fcoe, &fcoe_hostlist, list) {
Robert Love1875f272009-11-03 11:47:50 -08002377 if (fcoe->netdev == netdev)
Chris Leech014f5c32009-08-25 13:59:30 -07002378 return fcoe;
Robert Love85b4aa42008-12-09 15:10:24 -08002379 }
Robert Love85b4aa42008-12-09 15:10:24 -08002380 return NULL;
2381}
2382
Robert Love34f42a02009-02-27 10:55:45 -08002383/**
Robert Love1875f272009-11-03 11:47:50 -08002384 * fcoe_hostlist_lookup() - Find the local port associated with a
2385 * given net device
2386 * @netdev: The netdevice used as a key
Robert Love85b4aa42008-12-09 15:10:24 -08002387 *
Robert Love1875f272009-11-03 11:47:50 -08002388 * Locking: Must be called with the RTNL mutex held
2389 *
2390 * Returns: NULL or the local port
Robert Love85b4aa42008-12-09 15:10:24 -08002391 */
Chris Leech090eb6c2009-08-25 14:00:28 -07002392static struct fc_lport *fcoe_hostlist_lookup(const struct net_device *netdev)
Robert Love85b4aa42008-12-09 15:10:24 -08002393{
Chris Leech014f5c32009-08-25 13:59:30 -07002394 struct fcoe_interface *fcoe;
Robert Love85b4aa42008-12-09 15:10:24 -08002395
Chris Leech014f5c32009-08-25 13:59:30 -07002396 fcoe = fcoe_hostlist_lookup_port(netdev);
Chris Leech3fe9a0b2009-08-25 13:59:46 -07002397 return (fcoe) ? fcoe->ctlr.lp : NULL;
Robert Love85b4aa42008-12-09 15:10:24 -08002398}
Robert Love85b4aa42008-12-09 15:10:24 -08002399
Robert Love34f42a02009-02-27 10:55:45 -08002400/**
Robert Love1875f272009-11-03 11:47:50 -08002401 * fcoe_hostlist_add() - Add the FCoE interface identified by a local
2402 * port to the hostlist
2403 * @lport: The local port that identifies the FCoE interface to be added
2404 *
2405 * Locking: must be called with the RTNL mutex held
Robert Love85b4aa42008-12-09 15:10:24 -08002406 *
2407 * Returns: 0 for success
2408 */
Chris Leech090eb6c2009-08-25 14:00:28 -07002409static int fcoe_hostlist_add(const struct fc_lport *lport)
Robert Love85b4aa42008-12-09 15:10:24 -08002410{
Chris Leech014f5c32009-08-25 13:59:30 -07002411 struct fcoe_interface *fcoe;
2412 struct fcoe_port *port;
Robert Love85b4aa42008-12-09 15:10:24 -08002413
Chris Leech014f5c32009-08-25 13:59:30 -07002414 fcoe = fcoe_hostlist_lookup_port(fcoe_netdev(lport));
2415 if (!fcoe) {
2416 port = lport_priv(lport);
Bhanu Prakash Gollapudi8597ae82011-01-28 16:05:37 -08002417 fcoe = port->priv;
Chris Leech014f5c32009-08-25 13:59:30 -07002418 list_add_tail(&fcoe->list, &fcoe_hostlist);
Robert Love85b4aa42008-12-09 15:10:24 -08002419 }
2420 return 0;
2421}
Robert Love85b4aa42008-12-09 15:10:24 -08002422
Yi Zou78a58242011-01-28 16:05:16 -08002423
2424static struct fcoe_transport fcoe_sw_transport = {
2425 .name = {FCOE_TRANSPORT_DEFAULT},
2426 .attached = false,
2427 .list = LIST_HEAD_INIT(fcoe_sw_transport.list),
2428 .match = fcoe_match,
2429 .create = fcoe_create,
2430 .destroy = fcoe_destroy,
2431 .enable = fcoe_enable,
2432 .disable = fcoe_disable,
2433};
2434
Robert Love34f42a02009-02-27 10:55:45 -08002435/**
Robert Love1875f272009-11-03 11:47:50 -08002436 * fcoe_init() - Initialize fcoe.ko
Robert Love85b4aa42008-12-09 15:10:24 -08002437 *
Robert Love1875f272009-11-03 11:47:50 -08002438 * Returns: 0 on success, or a negative value on failure
Robert Love34f42a02009-02-27 10:55:45 -08002439 */
Robert Love85b4aa42008-12-09 15:10:24 -08002440static int __init fcoe_init(void)
2441{
Robert Love1875f272009-11-03 11:47:50 -08002442 struct fcoe_percpu_s *p;
Robert Love38eccab2009-03-17 11:41:30 -07002443 unsigned int cpu;
Robert Love8976f422009-03-17 11:41:46 -07002444 int rc = 0;
Robert Love85b4aa42008-12-09 15:10:24 -08002445
Tejun Heo2ca32b42011-01-28 16:05:32 -08002446 fcoe_wq = alloc_workqueue("fcoe", 0, 0);
2447 if (!fcoe_wq)
2448 return -ENOMEM;
2449
Yi Zou78a58242011-01-28 16:05:16 -08002450 /* register as a fcoe transport */
2451 rc = fcoe_transport_attach(&fcoe_sw_transport);
2452 if (rc) {
2453 printk(KERN_ERR "failed to register an fcoe transport, check "
2454 "if libfcoe is loaded\n");
2455 return rc;
2456 }
2457
Chris Leechdfc1d0f2009-08-25 14:00:13 -07002458 mutex_lock(&fcoe_config_mutex);
2459
Robert Love38eccab2009-03-17 11:41:30 -07002460 for_each_possible_cpu(cpu) {
Robert Love5e5e92d2009-03-17 11:41:35 -07002461 p = &per_cpu(fcoe_percpu, cpu);
Robert Love38eccab2009-03-17 11:41:30 -07002462 skb_queue_head_init(&p->fcoe_rx_list);
2463 }
2464
Robert Love8976f422009-03-17 11:41:46 -07002465 for_each_online_cpu(cpu)
2466 fcoe_percpu_thread_create(cpu);
Robert Love85b4aa42008-12-09 15:10:24 -08002467
Robert Love8976f422009-03-17 11:41:46 -07002468 /* Initialize per CPU interrupt thread */
2469 rc = register_hotcpu_notifier(&fcoe_cpu_notifier);
2470 if (rc)
2471 goto out_free;
Robert Love85b4aa42008-12-09 15:10:24 -08002472
Robert Love8976f422009-03-17 11:41:46 -07002473 /* Setup link change notification */
Robert Love85b4aa42008-12-09 15:10:24 -08002474 fcoe_dev_setup();
2475
Chris Leech5892c322009-08-25 13:59:14 -07002476 rc = fcoe_if_init();
2477 if (rc)
2478 goto out_free;
Robert Love85b4aa42008-12-09 15:10:24 -08002479
Chris Leechdfc1d0f2009-08-25 14:00:13 -07002480 mutex_unlock(&fcoe_config_mutex);
Robert Love85b4aa42008-12-09 15:10:24 -08002481 return 0;
Robert Love8976f422009-03-17 11:41:46 -07002482
2483out_free:
2484 for_each_online_cpu(cpu) {
2485 fcoe_percpu_thread_destroy(cpu);
2486 }
Chris Leechdfc1d0f2009-08-25 14:00:13 -07002487 mutex_unlock(&fcoe_config_mutex);
Tejun Heo2ca32b42011-01-28 16:05:32 -08002488 destroy_workqueue(fcoe_wq);
Robert Love8976f422009-03-17 11:41:46 -07002489 return rc;
Robert Love85b4aa42008-12-09 15:10:24 -08002490}
2491module_init(fcoe_init);
2492
2493/**
Robert Love1875f272009-11-03 11:47:50 -08002494 * fcoe_exit() - Clean up fcoe.ko
Robert Love85b4aa42008-12-09 15:10:24 -08002495 *
Robert Love1875f272009-11-03 11:47:50 -08002496 * Returns: 0 on success or a negative value on failure
Robert Love34f42a02009-02-27 10:55:45 -08002497 */
Robert Love85b4aa42008-12-09 15:10:24 -08002498static void __exit fcoe_exit(void)
2499{
Chris Leech014f5c32009-08-25 13:59:30 -07002500 struct fcoe_interface *fcoe, *tmp;
Chris Leech2e70e242009-08-25 14:00:23 -07002501 struct fcoe_port *port;
Robert Love1875f272009-11-03 11:47:50 -08002502 unsigned int cpu;
Robert Love85b4aa42008-12-09 15:10:24 -08002503
Chris Leechdfc1d0f2009-08-25 14:00:13 -07002504 mutex_lock(&fcoe_config_mutex);
2505
Robert Love85b4aa42008-12-09 15:10:24 -08002506 fcoe_dev_cleanup();
2507
Vasu Dev5919a592009-03-27 09:03:29 -07002508 /* releases the associated fcoe hosts */
Chris Leech090eb6c2009-08-25 14:00:28 -07002509 rtnl_lock();
2510 list_for_each_entry_safe(fcoe, tmp, &fcoe_hostlist, list) {
Chris Leechc863df32009-08-25 14:00:18 -07002511 list_del(&fcoe->list);
Chris Leech2e70e242009-08-25 14:00:23 -07002512 port = lport_priv(fcoe->ctlr.lp);
Tejun Heo2ca32b42011-01-28 16:05:32 -08002513 queue_work(fcoe_wq, &port->destroy_work);
Chris Leechc863df32009-08-25 14:00:18 -07002514 }
Chris Leech090eb6c2009-08-25 14:00:28 -07002515 rtnl_unlock();
Robert Love85b4aa42008-12-09 15:10:24 -08002516
Robert Love8976f422009-03-17 11:41:46 -07002517 unregister_hotcpu_notifier(&fcoe_cpu_notifier);
2518
Chris Leech014f5c32009-08-25 13:59:30 -07002519 for_each_online_cpu(cpu)
Robert Love8976f422009-03-17 11:41:46 -07002520 fcoe_percpu_thread_destroy(cpu);
Robert Love85b4aa42008-12-09 15:10:24 -08002521
Chris Leechdfc1d0f2009-08-25 14:00:13 -07002522 mutex_unlock(&fcoe_config_mutex);
Chris Leech2e70e242009-08-25 14:00:23 -07002523
Tejun Heo2ca32b42011-01-28 16:05:32 -08002524 /*
2525 * destroy_work's may be chained but destroy_workqueue()
2526 * can take care of them. Just kill the fcoe_wq.
2527 */
2528 destroy_workqueue(fcoe_wq);
Chris Leech2e70e242009-08-25 14:00:23 -07002529
Tejun Heo2ca32b42011-01-28 16:05:32 -08002530 /*
2531 * Detaching from the scsi transport must happen after all
2532 * destroys are done on the fcoe_wq. destroy_workqueue will
2533 * enusre the fcoe_wq is flushed.
2534 */
Chris Leech2e70e242009-08-25 14:00:23 -07002535 fcoe_if_exit();
Yi Zou78a58242011-01-28 16:05:16 -08002536
2537 /* detach from fcoe transport */
2538 fcoe_transport_detach(&fcoe_sw_transport);
Robert Love85b4aa42008-12-09 15:10:24 -08002539}
2540module_exit(fcoe_exit);
Chris Leech11b56182009-11-03 11:46:29 -08002541
2542/**
2543 * fcoe_flogi_resp() - FCoE specific FLOGI and FDISC response handler
2544 * @seq: active sequence in the FLOGI or FDISC exchange
2545 * @fp: response frame, or error encoded in a pointer (timeout)
2546 * @arg: pointer the the fcoe_ctlr structure
2547 *
Uwe Kleine-König65155b32010-06-11 12:17:01 +02002548 * This handles MAC address management for FCoE, then passes control on to
Chris Leech11b56182009-11-03 11:46:29 -08002549 * the libfc FLOGI response handler.
2550 */
2551static void fcoe_flogi_resp(struct fc_seq *seq, struct fc_frame *fp, void *arg)
2552{
2553 struct fcoe_ctlr *fip = arg;
2554 struct fc_exch *exch = fc_seq_exch(seq);
2555 struct fc_lport *lport = exch->lp;
2556 u8 *mac;
2557
2558 if (IS_ERR(fp))
2559 goto done;
2560
2561 mac = fr_cb(fp)->granted_mac;
Vasu Dev907c07d2011-10-28 11:34:23 -07002562 /* pre-FIP */
2563 if (is_zero_ether_addr(mac))
2564 fcoe_ctlr_recv_flogi(fip, lport, fp);
2565 if (!is_zero_ether_addr(mac))
2566 fcoe_update_src_mac(lport, mac);
Chris Leech11b56182009-11-03 11:46:29 -08002567done:
2568 fc_lport_flogi_resp(seq, fp, lport);
2569}
2570
2571/**
2572 * fcoe_logo_resp() - FCoE specific LOGO response handler
2573 * @seq: active sequence in the LOGO exchange
2574 * @fp: response frame, or error encoded in a pointer (timeout)
2575 * @arg: pointer the the fcoe_ctlr structure
2576 *
Uwe Kleine-König65155b32010-06-11 12:17:01 +02002577 * This handles MAC address management for FCoE, then passes control on to
Chris Leech11b56182009-11-03 11:46:29 -08002578 * the libfc LOGO response handler.
2579 */
2580static void fcoe_logo_resp(struct fc_seq *seq, struct fc_frame *fp, void *arg)
2581{
Joe Eykholt386309c2009-11-03 11:49:16 -08002582 struct fc_lport *lport = arg;
Chris Leech11b56182009-11-03 11:46:29 -08002583 static u8 zero_mac[ETH_ALEN] = { 0 };
2584
2585 if (!IS_ERR(fp))
Joe Eykholt386309c2009-11-03 11:49:16 -08002586 fcoe_update_src_mac(lport, zero_mac);
Chris Leech11b56182009-11-03 11:46:29 -08002587 fc_lport_logo_resp(seq, fp, lport);
2588}
2589
2590/**
2591 * fcoe_elsct_send - FCoE specific ELS handler
2592 *
2593 * This does special case handling of FIP encapsualted ELS exchanges for FCoE,
2594 * using FCoE specific response handlers and passing the FIP controller as
2595 * the argument (the lport is still available from the exchange).
2596 *
2597 * Most of the work here is just handed off to the libfc routine.
2598 */
Robert Love1875f272009-11-03 11:47:50 -08002599static struct fc_seq *fcoe_elsct_send(struct fc_lport *lport, u32 did,
2600 struct fc_frame *fp, unsigned int op,
2601 void (*resp)(struct fc_seq *,
2602 struct fc_frame *,
2603 void *),
2604 void *arg, u32 timeout)
Chris Leech11b56182009-11-03 11:46:29 -08002605{
2606 struct fcoe_port *port = lport_priv(lport);
Bhanu Prakash Gollapudi8597ae82011-01-28 16:05:37 -08002607 struct fcoe_interface *fcoe = port->priv;
Chris Leech11b56182009-11-03 11:46:29 -08002608 struct fcoe_ctlr *fip = &fcoe->ctlr;
2609 struct fc_frame_header *fh = fc_frame_header_get(fp);
2610
2611 switch (op) {
2612 case ELS_FLOGI:
2613 case ELS_FDISC:
Joe Eykholte10f8c62010-07-20 15:20:30 -07002614 if (lport->point_to_multipoint)
2615 break;
Chris Leech11b56182009-11-03 11:46:29 -08002616 return fc_elsct_send(lport, did, fp, op, fcoe_flogi_resp,
2617 fip, timeout);
2618 case ELS_LOGO:
2619 /* only hook onto fabric logouts, not port logouts */
2620 if (ntoh24(fh->fh_d_id) != FC_FID_FLOGI)
2621 break;
2622 return fc_elsct_send(lport, did, fp, op, fcoe_logo_resp,
Joe Eykholt386309c2009-11-03 11:49:16 -08002623 lport, timeout);
Chris Leech11b56182009-11-03 11:46:29 -08002624 }
2625 return fc_elsct_send(lport, did, fp, op, resp, arg, timeout);
2626}
2627
Chris Leech9a057532009-11-03 11:46:40 -08002628/**
2629 * fcoe_vport_create() - create an fc_host/scsi_host for a vport
2630 * @vport: fc_vport object to create a new fc_host for
2631 * @disabled: start the new fc_host in a disabled state by default?
2632 *
2633 * Returns: 0 for success
2634 */
2635static int fcoe_vport_create(struct fc_vport *vport, bool disabled)
2636{
2637 struct Scsi_Host *shost = vport_to_shost(vport);
2638 struct fc_lport *n_port = shost_priv(shost);
2639 struct fcoe_port *port = lport_priv(n_port);
Bhanu Prakash Gollapudi8597ae82011-01-28 16:05:37 -08002640 struct fcoe_interface *fcoe = port->priv;
Chris Leech9a057532009-11-03 11:46:40 -08002641 struct net_device *netdev = fcoe->netdev;
2642 struct fc_lport *vn_port;
Neerav Parikhbdf25212011-05-16 16:45:29 -07002643 int rc;
2644 char buf[32];
2645
2646 rc = fcoe_validate_vport_create(vport);
2647 if (rc) {
Bhanu Prakash Gollapudid8348952011-08-04 17:38:49 -07002648 fcoe_wwn_to_str(vport->port_name, buf, sizeof(buf));
Neerav Parikhbdf25212011-05-16 16:45:29 -07002649 printk(KERN_ERR "fcoe: Failed to create vport, "
2650 "WWPN (0x%s) already exists\n",
2651 buf);
2652 return rc;
2653 }
Chris Leech9a057532009-11-03 11:46:40 -08002654
2655 mutex_lock(&fcoe_config_mutex);
Jiri Pirko4bc71cb2011-09-03 03:34:30 +00002656 rtnl_lock();
Chris Leech9a057532009-11-03 11:46:40 -08002657 vn_port = fcoe_if_create(fcoe, &vport->dev, 1);
Jiri Pirko4bc71cb2011-09-03 03:34:30 +00002658 rtnl_unlock();
Chris Leech9a057532009-11-03 11:46:40 -08002659 mutex_unlock(&fcoe_config_mutex);
2660
2661 if (IS_ERR(vn_port)) {
2662 printk(KERN_ERR "fcoe: fcoe_vport_create(%s) failed\n",
2663 netdev->name);
2664 return -EIO;
2665 }
2666
2667 if (disabled) {
2668 fc_vport_set_state(vport, FC_VPORT_DISABLED);
2669 } else {
2670 vn_port->boot_time = jiffies;
2671 fc_fabric_login(vn_port);
2672 fc_vport_setlink(vn_port);
2673 }
2674 return 0;
2675}
2676
2677/**
2678 * fcoe_vport_destroy() - destroy the fc_host/scsi_host for a vport
2679 * @vport: fc_vport object that is being destroyed
2680 *
2681 * Returns: 0 for success
2682 */
2683static int fcoe_vport_destroy(struct fc_vport *vport)
2684{
2685 struct Scsi_Host *shost = vport_to_shost(vport);
2686 struct fc_lport *n_port = shost_priv(shost);
2687 struct fc_lport *vn_port = vport->dd_data;
Chris Leech9a057532009-11-03 11:46:40 -08002688
2689 mutex_lock(&n_port->lp_mutex);
2690 list_del(&vn_port->list);
2691 mutex_unlock(&n_port->lp_mutex);
Robert Loveccefd232012-02-10 17:18:41 -08002692
2693 mutex_lock(&fcoe_config_mutex);
2694 fcoe_if_destroy(vn_port);
2695 mutex_unlock(&fcoe_config_mutex);
2696
Chris Leech9a057532009-11-03 11:46:40 -08002697 return 0;
2698}
2699
2700/**
2701 * fcoe_vport_disable() - change vport state
2702 * @vport: vport to bring online/offline
2703 * @disable: should the vport be disabled?
2704 */
2705static int fcoe_vport_disable(struct fc_vport *vport, bool disable)
2706{
2707 struct fc_lport *lport = vport->dd_data;
2708
2709 if (disable) {
2710 fc_vport_set_state(vport, FC_VPORT_DISABLED);
2711 fc_fabric_logoff(lport);
2712 } else {
2713 lport->boot_time = jiffies;
2714 fc_fabric_login(lport);
2715 fc_vport_setlink(lport);
2716 }
2717
2718 return 0;
2719}
2720
Chris Leechdc8596d2009-11-03 11:47:18 -08002721/**
2722 * fcoe_vport_set_symbolic_name() - append vport string to symbolic name
2723 * @vport: fc_vport with a new symbolic name string
2724 *
2725 * After generating a new symbolic name string, a new RSPN_ID request is
2726 * sent to the name server. There is no response handler, so if it fails
2727 * for some reason it will not be retried.
2728 */
2729static void fcoe_set_vport_symbolic_name(struct fc_vport *vport)
2730{
2731 struct fc_lport *lport = vport->dd_data;
2732 struct fc_frame *fp;
2733 size_t len;
2734
2735 snprintf(fc_host_symbolic_name(lport->host), FC_SYMBOLIC_NAME_SIZE,
2736 "%s v%s over %s : %s", FCOE_NAME, FCOE_VERSION,
2737 fcoe_netdev(lport)->name, vport->symbolic_name);
2738
2739 if (lport->state != LPORT_ST_READY)
2740 return;
2741
2742 len = strnlen(fc_host_symbolic_name(lport->host), 255);
2743 fp = fc_frame_alloc(lport,
2744 sizeof(struct fc_ct_hdr) +
2745 sizeof(struct fc_ns_rspn) + len);
2746 if (!fp)
2747 return;
2748 lport->tt.elsct_send(lport, FC_FID_DIR_SERV, fp, FC_NS_RSPN_ID,
Joe Eykholtb94f8952009-11-03 11:50:21 -08002749 NULL, NULL, 3 * lport->r_a_tov);
Chris Leechdc8596d2009-11-03 11:47:18 -08002750}
Yi Zoub84056b2009-11-20 14:55:19 -08002751
2752/**
2753 * fcoe_get_lesb() - Fill the FCoE Link Error Status Block
2754 * @lport: the local port
2755 * @fc_lesb: the link error status block
2756 */
2757static void fcoe_get_lesb(struct fc_lport *lport,
2758 struct fc_els_lesb *fc_lesb)
2759{
Yi Zoub84056b2009-11-20 14:55:19 -08002760 struct net_device *netdev = fcoe_netdev(lport);
2761
Bhanu Prakash Gollapudi814740d2011-10-03 16:45:01 -07002762 __fcoe_get_lesb(lport, fc_lesb, netdev);
Yi Zoub84056b2009-11-20 14:55:19 -08002763}
Joe Eykholt7d65b0d2010-03-12 16:08:02 -08002764
2765/**
2766 * fcoe_set_port_id() - Callback from libfc when Port_ID is set.
2767 * @lport: the local port
2768 * @port_id: the port ID
2769 * @fp: the received frame, if any, that caused the port_id to be set.
2770 *
2771 * This routine handles the case where we received a FLOGI and are
2772 * entering point-to-point mode. We need to call fcoe_ctlr_recv_flogi()
2773 * so it can set the non-mapped mode and gateway address.
2774 *
2775 * The FLOGI LS_ACC is handled by fcoe_flogi_resp().
2776 */
2777static void fcoe_set_port_id(struct fc_lport *lport,
2778 u32 port_id, struct fc_frame *fp)
2779{
2780 struct fcoe_port *port = lport_priv(lport);
Bhanu Prakash Gollapudi8597ae82011-01-28 16:05:37 -08002781 struct fcoe_interface *fcoe = port->priv;
Joe Eykholt7d65b0d2010-03-12 16:08:02 -08002782
2783 if (fp && fc_frame_payload_op(fp) == ELS_FLOGI)
2784 fcoe_ctlr_recv_flogi(&fcoe->ctlr, lport, fp);
2785}