blob: 5e77f23423db896294e23a0150e94aca886a6d80 [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 Devfdd780272009-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 Devfdd780272009-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 Devfdd780272009-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 Devfdd780272009-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,
219
220 .show_host_port_id = 1,
221 .show_host_supported_speeds = 1,
222 .get_host_speed = fc_get_host_speed,
223 .show_host_speed = 1,
224 .show_host_port_type = 1,
225 .get_host_port_state = fc_get_host_port_state,
226 .show_host_port_state = 1,
227 .show_host_symbolic_name = 1,
228
229 .dd_fcrport_size = sizeof(struct fc_rport_libfc_priv),
230 .show_rport_maxframe_size = 1,
231 .show_rport_supported_classes = 1,
232
233 .show_host_fabric_name = 1,
234 .show_starget_node_name = 1,
235 .show_starget_port_name = 1,
236 .show_starget_port_id = 1,
237 .set_rport_dev_loss_tmo = fc_set_rport_loss_tmo,
238 .show_rport_dev_loss_tmo = 1,
239 .get_fc_host_stats = fc_get_host_stats,
240 .issue_fc_host_lip = fcoe_reset,
241
242 .terminate_rport_io = fc_rport_terminate_io,
Steve Maa51ab392009-11-03 11:47:34 -0800243
244 .bsg_request = fc_lport_bsg_request,
Chris Leeche9084bb2009-11-03 11:46:34 -0800245};
246
Vasu Dev7f349142009-03-27 09:06:31 -0700247static struct scsi_host_template fcoe_shost_template = {
248 .module = THIS_MODULE,
249 .name = "FCoE Driver",
250 .proc_name = FCOE_NAME,
251 .queuecommand = fc_queuecommand,
252 .eh_abort_handler = fc_eh_abort,
253 .eh_device_reset_handler = fc_eh_device_reset,
254 .eh_host_reset_handler = fc_eh_host_reset,
255 .slave_alloc = fc_slave_alloc,
256 .change_queue_depth = fc_change_queue_depth,
257 .change_queue_type = fc_change_queue_type,
258 .this_id = -1,
Vasu Dev14caf442009-10-15 17:46:55 -0700259 .cmd_per_lun = 3,
Vasu Dev7f349142009-03-27 09:06:31 -0700260 .can_queue = FCOE_MAX_OUTSTANDING_COMMANDS,
261 .use_clustering = ENABLE_CLUSTERING,
262 .sg_tablesize = SG_ALL,
263 .max_sectors = 0xffff,
264};
265
Chris Leech54b649f2009-08-25 14:00:07 -0700266/**
Robert Love1875f272009-11-03 11:47:50 -0800267 * fcoe_interface_setup() - Setup a FCoE interface
268 * @fcoe: The new FCoE interface
269 * @netdev: The net device that the fcoe interface is on
Chris Leech54b649f2009-08-25 14:00:07 -0700270 *
271 * Returns : 0 for success
Chris Leech2e70e242009-08-25 14:00:23 -0700272 * Locking: must be called with the RTNL mutex held
Chris Leech54b649f2009-08-25 14:00:07 -0700273 */
274static int fcoe_interface_setup(struct fcoe_interface *fcoe,
275 struct net_device *netdev)
276{
277 struct fcoe_ctlr *fip = &fcoe->ctlr;
278 struct netdev_hw_addr *ha;
Yi Zou5bab87e2009-11-03 11:49:43 -0800279 struct net_device *real_dev;
Chris Leech54b649f2009-08-25 14:00:07 -0700280 u8 flogi_maddr[ETH_ALEN];
Yi Zoub7a727f2009-10-21 16:28:03 -0700281 const struct net_device_ops *ops;
Chris Leech54b649f2009-08-25 14:00:07 -0700282
283 fcoe->netdev = netdev;
284
Yi Zoub7a727f2009-10-21 16:28:03 -0700285 /* Let LLD initialize for FCoE */
286 ops = netdev->netdev_ops;
287 if (ops->ndo_fcoe_enable) {
288 if (ops->ndo_fcoe_enable(netdev))
289 FCOE_NETDEV_DBG(netdev, "Failed to enable FCoE"
290 " specific feature for LLD.\n");
291 }
292
Chris Leech54b649f2009-08-25 14:00:07 -0700293 /* Do not support for bonding device */
Jiri Pirkocc8bdf02011-03-01 20:05:35 +0000294 if (netdev->priv_flags & IFF_BONDING && netdev->flags & IFF_MASTER) {
john fastabend59d92512009-11-03 11:48:44 -0800295 FCOE_NETDEV_DBG(netdev, "Bonded interfaces not supported\n");
Chris Leech54b649f2009-08-25 14:00:07 -0700296 return -EOPNOTSUPP;
297 }
298
299 /* look for SAN MAC address, if multiple SAN MACs exist, only
300 * use the first one for SPMA */
Yi Zou5bab87e2009-11-03 11:49:43 -0800301 real_dev = (netdev->priv_flags & IFF_802_1Q_VLAN) ?
302 vlan_dev_real_dev(netdev) : netdev;
Vasu Devd1483bb2011-09-27 21:38:13 -0700303 fcoe->realdev = real_dev;
Chris Leech54b649f2009-08-25 14:00:07 -0700304 rcu_read_lock();
Yi Zou5bab87e2009-11-03 11:49:43 -0800305 for_each_dev_addr(real_dev, ha) {
Chris Leech54b649f2009-08-25 14:00:07 -0700306 if ((ha->type == NETDEV_HW_ADDR_T_SAN) &&
Yi Zoubf361702009-11-03 11:49:38 -0800307 (is_valid_ether_addr(ha->addr))) {
Chris Leech54b649f2009-08-25 14:00:07 -0700308 memcpy(fip->ctl_src_addr, ha->addr, ETH_ALEN);
309 fip->spma = 1;
310 break;
311 }
312 }
313 rcu_read_unlock();
314
315 /* setup Source Mac Address */
316 if (!fip->spma)
317 memcpy(fip->ctl_src_addr, netdev->dev_addr, netdev->addr_len);
318
319 /*
320 * Add FCoE MAC address as second unicast MAC address
321 * or enter promiscuous mode if not capable of listening
322 * for multiple unicast MACs.
323 */
Chris Leech54b649f2009-08-25 14:00:07 -0700324 memcpy(flogi_maddr, (u8[6]) FC_FCOE_FLOGI_MAC, ETH_ALEN);
Jiri Pirkoa748ee22010-04-01 21:22:09 +0000325 dev_uc_add(netdev, flogi_maddr);
Chris Leech54b649f2009-08-25 14:00:07 -0700326 if (fip->spma)
Jiri Pirkoa748ee22010-04-01 21:22:09 +0000327 dev_uc_add(netdev, fip->ctl_src_addr);
Joe Eykholte10f8c62010-07-20 15:20:30 -0700328 if (fip->mode == FIP_MODE_VN2VN) {
329 dev_mc_add(netdev, FIP_ALL_VN2VN_MACS);
330 dev_mc_add(netdev, FIP_ALL_P2P_MACS);
331 } else
332 dev_mc_add(netdev, FIP_ALL_ENODE_MACS);
Chris Leech54b649f2009-08-25 14:00:07 -0700333
334 /*
335 * setup the receive function from ethernet driver
336 * on the ethertype for the given device
337 */
338 fcoe->fcoe_packet_type.func = fcoe_rcv;
339 fcoe->fcoe_packet_type.type = __constant_htons(ETH_P_FCOE);
340 fcoe->fcoe_packet_type.dev = netdev;
341 dev_add_pack(&fcoe->fcoe_packet_type);
342
343 fcoe->fip_packet_type.func = fcoe_fip_recv;
344 fcoe->fip_packet_type.type = htons(ETH_P_FIP);
345 fcoe->fip_packet_type.dev = netdev;
346 dev_add_pack(&fcoe->fip_packet_type);
347
348 return 0;
349}
350
Vasu Dev7f349142009-03-27 09:06:31 -0700351/**
Robert Love1875f272009-11-03 11:47:50 -0800352 * fcoe_interface_create() - Create a FCoE interface on a net device
353 * @netdev: The net device to create the FCoE interface on
Joe Eykholt1dd454d2010-07-20 15:20:46 -0700354 * @fip_mode: The mode to use for FIP
Chris Leech030f4e02009-08-25 14:00:02 -0700355 *
356 * Returns: pointer to a struct fcoe_interface or NULL on error
357 */
Joe Eykholt1dd454d2010-07-20 15:20:46 -0700358static struct fcoe_interface *fcoe_interface_create(struct net_device *netdev,
359 enum fip_state fip_mode)
Chris Leech030f4e02009-08-25 14:00:02 -0700360{
361 struct fcoe_interface *fcoe;
john fastabend59d92512009-11-03 11:48:44 -0800362 int err;
Chris Leech030f4e02009-08-25 14:00:02 -0700363
Robert Love7287fb92011-01-28 16:03:47 -0800364 if (!try_module_get(THIS_MODULE)) {
365 FCOE_NETDEV_DBG(netdev,
366 "Could not get a reference to the module\n");
367 fcoe = ERR_PTR(-EBUSY);
368 goto out;
369 }
370
Chris Leech030f4e02009-08-25 14:00:02 -0700371 fcoe = kzalloc(sizeof(*fcoe), GFP_KERNEL);
372 if (!fcoe) {
373 FCOE_NETDEV_DBG(netdev, "Could not allocate fcoe structure\n");
Robert Love7287fb92011-01-28 16:03:47 -0800374 fcoe = ERR_PTR(-ENOMEM);
375 goto out_nomod;
Chris Leech030f4e02009-08-25 14:00:02 -0700376 }
377
Chris Leech2e70e242009-08-25 14:00:23 -0700378 dev_hold(netdev);
Chris Leech030f4e02009-08-25 14:00:02 -0700379 kref_init(&fcoe->kref);
Chris Leech54b649f2009-08-25 14:00:07 -0700380
381 /*
382 * Initialize FIP.
383 */
Joe Eykholt1dd454d2010-07-20 15:20:46 -0700384 fcoe_ctlr_init(&fcoe->ctlr, fip_mode);
Chris Leech54b649f2009-08-25 14:00:07 -0700385 fcoe->ctlr.send = fcoe_fip_send;
386 fcoe->ctlr.update_mac = fcoe_update_src_mac;
Chris Leech11b56182009-11-03 11:46:29 -0800387 fcoe->ctlr.get_src_addr = fcoe_get_src_mac;
Chris Leech54b649f2009-08-25 14:00:07 -0700388
john fastabend59d92512009-11-03 11:48:44 -0800389 err = fcoe_interface_setup(fcoe, netdev);
390 if (err) {
391 fcoe_ctlr_destroy(&fcoe->ctlr);
392 kfree(fcoe);
393 dev_put(netdev);
Robert Love7287fb92011-01-28 16:03:47 -0800394 fcoe = ERR_PTR(err);
395 goto out_nomod;
john fastabend59d92512009-11-03 11:48:44 -0800396 }
Chris Leech030f4e02009-08-25 14:00:02 -0700397
Robert Love7287fb92011-01-28 16:03:47 -0800398 goto out;
399
400out_nomod:
401 module_put(THIS_MODULE);
402out:
Chris Leech030f4e02009-08-25 14:00:02 -0700403 return fcoe;
404}
405
406/**
407 * fcoe_interface_release() - fcoe_port kref release function
Robert Love1875f272009-11-03 11:47:50 -0800408 * @kref: Embedded reference count in an fcoe_interface struct
Chris Leech030f4e02009-08-25 14:00:02 -0700409 */
410static void fcoe_interface_release(struct kref *kref)
411{
412 struct fcoe_interface *fcoe;
Chris Leech2e70e242009-08-25 14:00:23 -0700413 struct net_device *netdev;
Chris Leech030f4e02009-08-25 14:00:02 -0700414
415 fcoe = container_of(kref, struct fcoe_interface, kref);
Chris Leech2e70e242009-08-25 14:00:23 -0700416 netdev = fcoe->netdev;
417 /* tear-down the FCoE controller */
418 fcoe_ctlr_destroy(&fcoe->ctlr);
Chris Leech030f4e02009-08-25 14:00:02 -0700419 kfree(fcoe);
Chris Leech2e70e242009-08-25 14:00:23 -0700420 dev_put(netdev);
Robert Love7287fb92011-01-28 16:03:47 -0800421 module_put(THIS_MODULE);
Chris Leech030f4e02009-08-25 14:00:02 -0700422}
423
424/**
Robert Love1875f272009-11-03 11:47:50 -0800425 * fcoe_interface_get() - Get a reference to a FCoE interface
426 * @fcoe: The FCoE interface to be held
Chris Leech030f4e02009-08-25 14:00:02 -0700427 */
428static inline void fcoe_interface_get(struct fcoe_interface *fcoe)
429{
430 kref_get(&fcoe->kref);
431}
432
433/**
Robert Love1875f272009-11-03 11:47:50 -0800434 * fcoe_interface_put() - Put a reference to a FCoE interface
435 * @fcoe: The FCoE interface to be released
Chris Leech030f4e02009-08-25 14:00:02 -0700436 */
437static inline void fcoe_interface_put(struct fcoe_interface *fcoe)
438{
439 kref_put(&fcoe->kref, fcoe_interface_release);
440}
441
442/**
Vasu Devf04ca1b2011-04-01 16:06:45 -0700443 * fcoe_interface_cleanup() - Clean up a FCoE interface
444 * @fcoe: The FCoE interface to be cleaned up
445 *
446 * Caller must be holding the RTNL mutex
447 */
Bart Van Assche7c9c6842012-01-13 17:26:25 -0800448static void fcoe_interface_cleanup(struct fcoe_interface *fcoe)
Vasu Devf04ca1b2011-04-01 16:06:45 -0700449{
450 struct net_device *netdev = fcoe->netdev;
451 struct fcoe_ctlr *fip = &fcoe->ctlr;
452 u8 flogi_maddr[ETH_ALEN];
453 const struct net_device_ops *ops;
Vasu Devf04ca1b2011-04-01 16:06:45 -0700454
Robert Love848e7d52011-08-25 12:40:47 -0700455 rtnl_lock();
456
Vasu Devf04ca1b2011-04-01 16:06:45 -0700457 /*
458 * Don't listen for Ethernet packets anymore.
459 * synchronize_net() ensures that the packet handlers are not running
460 * on another CPU. dev_remove_pack() would do that, this calls the
461 * unsyncronized version __dev_remove_pack() to avoid multiple delays.
462 */
463 __dev_remove_pack(&fcoe->fcoe_packet_type);
464 __dev_remove_pack(&fcoe->fip_packet_type);
465 synchronize_net();
466
467 /* Delete secondary MAC addresses */
468 memcpy(flogi_maddr, (u8[6]) FC_FCOE_FLOGI_MAC, ETH_ALEN);
469 dev_uc_del(netdev, flogi_maddr);
470 if (fip->spma)
471 dev_uc_del(netdev, fip->ctl_src_addr);
472 if (fip->mode == FIP_MODE_VN2VN) {
473 dev_mc_del(netdev, FIP_ALL_VN2VN_MACS);
474 dev_mc_del(netdev, FIP_ALL_P2P_MACS);
475 } else
476 dev_mc_del(netdev, FIP_ALL_ENODE_MACS);
477
Vasu Devf04ca1b2011-04-01 16:06:45 -0700478 /* Tell the LLD we are done w/ FCoE */
479 ops = netdev->netdev_ops;
480 if (ops->ndo_fcoe_disable) {
481 if (ops->ndo_fcoe_disable(netdev))
482 FCOE_NETDEV_DBG(netdev, "Failed to disable FCoE"
483 " specific feature for LLD.\n");
484 }
Neerav Parikhb2085a42011-06-20 16:59:51 -0700485
Robert Love848e7d52011-08-25 12:40:47 -0700486 rtnl_unlock();
487
Neerav Parikhb2085a42011-06-20 16:59:51 -0700488 /* Release the self-reference taken during fcoe_interface_create() */
Vasu Devf04ca1b2011-04-01 16:06:45 -0700489 fcoe_interface_put(fcoe);
490}
491
492/**
Robert Love1875f272009-11-03 11:47:50 -0800493 * fcoe_fip_recv() - Handler for received FIP frames
494 * @skb: The receive skb
495 * @netdev: The associated net device
496 * @ptype: The packet_type structure which was used to register this handler
497 * @orig_dev: The original net_device the the skb was received on.
498 * (in case dev is a bond)
Vasu Devab6b85c2009-05-17 12:33:08 +0000499 *
500 * Returns: 0 for success
501 */
Robert Love1875f272009-11-03 11:47:50 -0800502static int fcoe_fip_recv(struct sk_buff *skb, struct net_device *netdev,
Vasu Devab6b85c2009-05-17 12:33:08 +0000503 struct packet_type *ptype,
504 struct net_device *orig_dev)
505{
Chris Leech259ad852009-08-25 13:59:41 -0700506 struct fcoe_interface *fcoe;
Vasu Devab6b85c2009-05-17 12:33:08 +0000507
Chris Leech259ad852009-08-25 13:59:41 -0700508 fcoe = container_of(ptype, struct fcoe_interface, fip_packet_type);
Chris Leech3fe9a0b2009-08-25 13:59:46 -0700509 fcoe_ctlr_recv(&fcoe->ctlr, skb);
Vasu Devab6b85c2009-05-17 12:33:08 +0000510 return 0;
511}
512
513/**
Vasu Dev980f5152011-07-27 15:11:05 -0700514 * fcoe_port_send() - Send an Ethernet-encapsulated FIP/FCoE frame
515 * @port: The FCoE port
516 * @skb: The FIP/FCoE packet to be sent
517 */
518static void fcoe_port_send(struct fcoe_port *port, struct sk_buff *skb)
519{
520 if (port->fcoe_pending_queue.qlen)
521 fcoe_check_wait_queue(port->lport, skb);
522 else if (fcoe_start_io(skb))
523 fcoe_check_wait_queue(port->lport, skb);
524}
525
526/**
Robert Love1875f272009-11-03 11:47:50 -0800527 * fcoe_fip_send() - Send an Ethernet-encapsulated FIP frame
528 * @fip: The FCoE controller
529 * @skb: The FIP packet to be sent
Vasu Devab6b85c2009-05-17 12:33:08 +0000530 */
531static void fcoe_fip_send(struct fcoe_ctlr *fip, struct sk_buff *skb)
532{
Chris Leech3fe9a0b2009-08-25 13:59:46 -0700533 skb->dev = fcoe_from_ctlr(fip)->netdev;
Vasu Dev980f5152011-07-27 15:11:05 -0700534 fcoe_port_send(lport_priv(fip->lp), skb);
Vasu Devab6b85c2009-05-17 12:33:08 +0000535}
536
537/**
Robert Love1875f272009-11-03 11:47:50 -0800538 * fcoe_update_src_mac() - Update the Ethernet MAC filters
539 * @lport: The local port to update the source MAC on
540 * @addr: Unicast MAC address to add
Vasu Devab6b85c2009-05-17 12:33:08 +0000541 *
542 * Remove any previously-set unicast MAC filter.
543 * Add secondary FCoE MAC address filter for our OUI.
544 */
Chris Leech11b56182009-11-03 11:46:29 -0800545static void fcoe_update_src_mac(struct fc_lport *lport, u8 *addr)
Vasu Devab6b85c2009-05-17 12:33:08 +0000546{
Chris Leech11b56182009-11-03 11:46:29 -0800547 struct fcoe_port *port = lport_priv(lport);
Bhanu Prakash Gollapudi8597ae82011-01-28 16:05:37 -0800548 struct fcoe_interface *fcoe = port->priv;
Vasu Devab6b85c2009-05-17 12:33:08 +0000549
Vasu Devab6b85c2009-05-17 12:33:08 +0000550 rtnl_lock();
Chris Leech11b56182009-11-03 11:46:29 -0800551 if (!is_zero_ether_addr(port->data_src_addr))
Jiri Pirkoa748ee22010-04-01 21:22:09 +0000552 dev_uc_del(fcoe->netdev, port->data_src_addr);
Chris Leech11b56182009-11-03 11:46:29 -0800553 if (!is_zero_ether_addr(addr))
Jiri Pirkoa748ee22010-04-01 21:22:09 +0000554 dev_uc_add(fcoe->netdev, addr);
Chris Leech11b56182009-11-03 11:46:29 -0800555 memcpy(port->data_src_addr, addr, ETH_ALEN);
Vasu Devab6b85c2009-05-17 12:33:08 +0000556 rtnl_unlock();
557}
558
559/**
Chris Leech11b56182009-11-03 11:46:29 -0800560 * fcoe_get_src_mac() - return the Ethernet source address for an lport
561 * @lport: libfc lport
562 */
563static u8 *fcoe_get_src_mac(struct fc_lport *lport)
564{
565 struct fcoe_port *port = lport_priv(lport);
566
567 return port->data_src_addr;
568}
569
570/**
Robert Love1875f272009-11-03 11:47:50 -0800571 * fcoe_lport_config() - Set up a local port
572 * @lport: The local port to be setup
Vasu Dev7f349142009-03-27 09:06:31 -0700573 *
574 * Returns: 0 for success
575 */
Robert Love1875f272009-11-03 11:47:50 -0800576static int fcoe_lport_config(struct fc_lport *lport)
Vasu Dev7f349142009-03-27 09:06:31 -0700577{
Robert Love1875f272009-11-03 11:47:50 -0800578 lport->link_up = 0;
579 lport->qfull = 0;
580 lport->max_retry_count = 3;
581 lport->max_rport_retry_count = 3;
582 lport->e_d_tov = 2 * 1000; /* FC-FS default */
583 lport->r_a_tov = 2 * 2 * 1000;
584 lport->service_params = (FCP_SPPF_INIT_FCN | FCP_SPPF_RD_XRDY_DIS |
585 FCP_SPPF_RETRY | FCP_SPPF_CONF_COMPL);
586 lport->does_npiv = 1;
Vasu Dev7f349142009-03-27 09:06:31 -0700587
Robert Love1875f272009-11-03 11:47:50 -0800588 fc_lport_init_stats(lport);
Vasu Dev7f349142009-03-27 09:06:31 -0700589
590 /* lport fc_lport related configuration */
Robert Love1875f272009-11-03 11:47:50 -0800591 fc_lport_config(lport);
Vasu Dev7f349142009-03-27 09:06:31 -0700592
593 /* offload related configuration */
Robert Love1875f272009-11-03 11:47:50 -0800594 lport->crc_offload = 0;
595 lport->seq_offload = 0;
596 lport->lro_enabled = 0;
597 lport->lro_xid = 0;
598 lport->lso_max = 0;
Vasu Dev7f349142009-03-27 09:06:31 -0700599
600 return 0;
601}
602
603/**
Yi Zou54a5b212010-07-20 15:21:17 -0700604 * fcoe_netdev_features_change - Updates the lport's offload flags based
605 * on the LLD netdev's FCoE feature flags
606 */
607static void fcoe_netdev_features_change(struct fc_lport *lport,
608 struct net_device *netdev)
609{
610 mutex_lock(&lport->lp_mutex);
611
612 if (netdev->features & NETIF_F_SG)
613 lport->sg_supp = 1;
614 else
615 lport->sg_supp = 0;
616
617 if (netdev->features & NETIF_F_FCOE_CRC) {
618 lport->crc_offload = 1;
619 FCOE_NETDEV_DBG(netdev, "Supports FCCRC offload\n");
620 } else {
621 lport->crc_offload = 0;
622 }
623
624 if (netdev->features & NETIF_F_FSO) {
625 lport->seq_offload = 1;
626 lport->lso_max = netdev->gso_max_size;
627 FCOE_NETDEV_DBG(netdev, "Supports LSO for max len 0x%x\n",
628 lport->lso_max);
629 } else {
630 lport->seq_offload = 0;
631 lport->lso_max = 0;
632 }
633
634 if (netdev->fcoe_ddp_xid) {
635 lport->lro_enabled = 1;
636 lport->lro_xid = netdev->fcoe_ddp_xid;
637 FCOE_NETDEV_DBG(netdev, "Supports LRO for max xid 0x%x\n",
638 lport->lro_xid);
639 } else {
640 lport->lro_enabled = 0;
641 lport->lro_xid = 0;
642 }
643
644 mutex_unlock(&lport->lp_mutex);
645}
646
647/**
Robert Love1875f272009-11-03 11:47:50 -0800648 * fcoe_netdev_config() - Set up net devive for SW FCoE
649 * @lport: The local port that is associated with the net device
650 * @netdev: The associated net device
Vasu Dev7f349142009-03-27 09:06:31 -0700651 *
Robert Love1875f272009-11-03 11:47:50 -0800652 * Must be called after fcoe_lport_config() as it will use local port mutex
Vasu Dev7f349142009-03-27 09:06:31 -0700653 *
Robert Love1875f272009-11-03 11:47:50 -0800654 * Returns: 0 for success
Vasu Dev7f349142009-03-27 09:06:31 -0700655 */
Robert Love1875f272009-11-03 11:47:50 -0800656static int fcoe_netdev_config(struct fc_lport *lport, struct net_device *netdev)
Vasu Dev7f349142009-03-27 09:06:31 -0700657{
658 u32 mfs;
659 u64 wwnn, wwpn;
Chris Leech25024982009-08-25 13:59:35 -0700660 struct fcoe_interface *fcoe;
Chris Leech014f5c32009-08-25 13:59:30 -0700661 struct fcoe_port *port;
Vasu Dev7f349142009-03-27 09:06:31 -0700662
663 /* Setup lport private data to point to fcoe softc */
Robert Love1875f272009-11-03 11:47:50 -0800664 port = lport_priv(lport);
Bhanu Prakash Gollapudi8597ae82011-01-28 16:05:37 -0800665 fcoe = port->priv;
Vasu Dev7f349142009-03-27 09:06:31 -0700666
667 /*
668 * Determine max frame size based on underlying device and optional
669 * user-configured limit. If the MFS is too low, fcoe_link_ok()
670 * will return 0, so do this first.
671 */
Yi Zou7221d7e2009-10-21 16:27:52 -0700672 mfs = netdev->mtu;
673 if (netdev->features & NETIF_F_FCOE_MTU) {
674 mfs = FCOE_MTU;
675 FCOE_NETDEV_DBG(netdev, "Supports FCOE_MTU of %d bytes\n", mfs);
676 }
677 mfs -= (sizeof(struct fcoe_hdr) + sizeof(struct fcoe_crc_eof));
Robert Love1875f272009-11-03 11:47:50 -0800678 if (fc_set_mfs(lport, mfs))
Vasu Dev7f349142009-03-27 09:06:31 -0700679 return -EINVAL;
680
Vasu Dev7f349142009-03-27 09:06:31 -0700681 /* offload features support */
Yi Zou54a5b212010-07-20 15:21:17 -0700682 fcoe_netdev_features_change(lport, netdev);
Vasu Dev7f349142009-03-27 09:06:31 -0700683
Chris Leech014f5c32009-08-25 13:59:30 -0700684 skb_queue_head_init(&port->fcoe_pending_queue);
685 port->fcoe_pending_queue_active = 0;
Robert Love1875f272009-11-03 11:47:50 -0800686 setup_timer(&port->timer, fcoe_queue_timer, (unsigned long)lport);
Vasu Dev7f349142009-03-27 09:06:31 -0700687
Robert Love5e4f8fe2010-05-07 15:18:35 -0700688 fcoe_link_speed_update(lport);
689
Robert Love1875f272009-11-03 11:47:50 -0800690 if (!lport->vport) {
Yi Zoudcece412009-11-20 15:22:21 -0800691 if (fcoe_get_wwn(netdev, &wwnn, NETDEV_FCOE_WWNN))
692 wwnn = fcoe_wwn_from_mac(fcoe->ctlr.ctl_src_addr, 1, 0);
Robert Love1875f272009-11-03 11:47:50 -0800693 fc_set_wwnn(lport, wwnn);
Yi Zoudcece412009-11-20 15:22:21 -0800694 if (fcoe_get_wwn(netdev, &wwpn, NETDEV_FCOE_WWPN))
695 wwpn = fcoe_wwn_from_mac(fcoe->ctlr.ctl_src_addr,
Vasu Devcf4aebca2010-07-20 15:21:22 -0700696 2, 0);
Robert Love1875f272009-11-03 11:47:50 -0800697 fc_set_wwpn(lport, wwpn);
Chris Leech9a057532009-11-03 11:46:40 -0800698 }
Vasu Dev7f349142009-03-27 09:06:31 -0700699
Vasu Dev7f349142009-03-27 09:06:31 -0700700 return 0;
701}
702
703/**
Robert Love1875f272009-11-03 11:47:50 -0800704 * fcoe_shost_config() - Set up the SCSI host associated with a local port
705 * @lport: The local port
Robert Love1875f272009-11-03 11:47:50 -0800706 * @dev: The device associated with the SCSI host
Vasu Dev7f349142009-03-27 09:06:31 -0700707 *
708 * Must be called after fcoe_lport_config() and fcoe_netdev_config()
709 *
Robert Love1875f272009-11-03 11:47:50 -0800710 * Returns: 0 for success
Vasu Dev7f349142009-03-27 09:06:31 -0700711 */
Vasu Dev8ba00a42010-04-09 14:22:54 -0700712static int fcoe_shost_config(struct fc_lport *lport, struct device *dev)
Vasu Dev7f349142009-03-27 09:06:31 -0700713{
714 int rc = 0;
715
716 /* lport scsi host config */
Robert Love1875f272009-11-03 11:47:50 -0800717 lport->host->max_lun = FCOE_MAX_LUN;
718 lport->host->max_id = FCOE_MAX_FCP_TARGET;
719 lport->host->max_channel = 0;
Vasu Devda87bfa2010-04-09 14:22:59 -0700720 lport->host->max_cmd_len = FCOE_MAX_CMD_LEN;
721
Robert Love1875f272009-11-03 11:47:50 -0800722 if (lport->vport)
Yi Zou8ca86f82011-01-28 16:05:11 -0800723 lport->host->transportt = fcoe_vport_scsi_transport;
Chris Leeche9084bb2009-11-03 11:46:34 -0800724 else
Yi Zou8ca86f82011-01-28 16:05:11 -0800725 lport->host->transportt = fcoe_nport_scsi_transport;
Vasu Dev7f349142009-03-27 09:06:31 -0700726
727 /* add the new host to the SCSI-ml */
Robert Love1875f272009-11-03 11:47:50 -0800728 rc = scsi_add_host(lport->host, dev);
Vasu Dev7f349142009-03-27 09:06:31 -0700729 if (rc) {
Robert Love1875f272009-11-03 11:47:50 -0800730 FCOE_NETDEV_DBG(fcoe_netdev(lport), "fcoe_shost_config: "
Robert Loved5488eb2009-06-10 15:30:59 -0700731 "error on scsi_add_host\n");
Vasu Dev7f349142009-03-27 09:06:31 -0700732 return rc;
733 }
Chris Leech9a057532009-11-03 11:46:40 -0800734
Robert Love1875f272009-11-03 11:47:50 -0800735 if (!lport->vport)
Alexey Dobriyan4be929b2010-05-24 14:33:03 -0700736 fc_host_max_npiv_vports(lport->host) = USHRT_MAX;
Chris Leech9a057532009-11-03 11:46:40 -0800737
Robert Love1875f272009-11-03 11:47:50 -0800738 snprintf(fc_host_symbolic_name(lport->host), FC_SYMBOLIC_NAME_SIZE,
Chris Leech5baa17c2009-11-03 11:46:56 -0800739 "%s v%s over %s", FCOE_NAME, FCOE_VERSION,
Robert Love1875f272009-11-03 11:47:50 -0800740 fcoe_netdev(lport)->name);
Vasu Dev7f349142009-03-27 09:06:31 -0700741
742 return 0;
743}
744
Neerav Parikh6fef3902012-01-22 17:30:10 -0800745
746/**
747 * fcoe_fdmi_info() - Get FDMI related info from net devive for SW FCoE
748 * @lport: The local port that is associated with the net device
749 * @netdev: The associated net device
750 *
751 * Must be called after fcoe_shost_config() as it will use local port mutex
752 *
753 */
754static void fcoe_fdmi_info(struct fc_lport *lport, struct net_device *netdev)
755{
756 struct fcoe_interface *fcoe;
757 struct fcoe_port *port;
758 struct net_device *realdev;
759 int rc;
760 struct netdev_fcoe_hbainfo fdmi;
761
762 port = lport_priv(lport);
763 fcoe = port->priv;
764 realdev = fcoe->realdev;
765
766 if (!realdev)
767 return;
768
769 /* No FDMI state m/c for NPIV ports */
770 if (lport->vport)
771 return;
772
773 if (realdev->netdev_ops->ndo_fcoe_get_hbainfo) {
774 memset(&fdmi, 0, sizeof(fdmi));
775 rc = realdev->netdev_ops->ndo_fcoe_get_hbainfo(realdev,
776 &fdmi);
777 if (rc) {
778 printk(KERN_INFO "fcoe: Failed to retrieve FDMI "
779 "information from netdev.\n");
780 return;
781 }
782
783 snprintf(fc_host_serial_number(lport->host),
784 FC_SERIAL_NUMBER_SIZE,
785 "%s",
786 fdmi.serial_number);
787 snprintf(fc_host_manufacturer(lport->host),
788 FC_SERIAL_NUMBER_SIZE,
789 "%s",
790 fdmi.manufacturer);
791 snprintf(fc_host_model(lport->host),
792 FC_SYMBOLIC_NAME_SIZE,
793 "%s",
794 fdmi.model);
795 snprintf(fc_host_model_description(lport->host),
796 FC_SYMBOLIC_NAME_SIZE,
797 "%s",
798 fdmi.model_description);
799 snprintf(fc_host_hardware_version(lport->host),
800 FC_VERSION_STRING_SIZE,
801 "%s",
802 fdmi.hardware_version);
803 snprintf(fc_host_driver_version(lport->host),
804 FC_VERSION_STRING_SIZE,
805 "%s",
806 fdmi.driver_version);
807 snprintf(fc_host_optionrom_version(lport->host),
808 FC_VERSION_STRING_SIZE,
809 "%s",
810 fdmi.optionrom_version);
811 snprintf(fc_host_firmware_version(lport->host),
812 FC_VERSION_STRING_SIZE,
813 "%s",
814 fdmi.firmware_version);
815
816 /* Enable FDMI lport states */
817 lport->fdmi_enabled = 1;
818 } else {
819 lport->fdmi_enabled = 0;
820 printk(KERN_INFO "fcoe: No FDMI support.\n");
821 }
822}
823
Robert Love1875f272009-11-03 11:47:50 -0800824/**
825 * fcoe_oem_match() - The match routine for the offloaded exchange manager
826 * @fp: The I/O frame
Vasu Devd7179682009-07-29 17:05:21 -0700827 *
Robert Love1875f272009-11-03 11:47:50 -0800828 * This routine will be associated with an exchange manager (EM). When
829 * the libfc exchange handling code is looking for an EM to use it will
830 * call this routine and pass it the frame that it wishes to send. This
831 * routine will return True if the associated EM is to be used and False
832 * if the echange code should continue looking for an EM.
833 *
834 * The offload EM that this routine is associated with will handle any
835 * packets that are for SCSI read requests.
836 *
Kiran Patil1ff99182011-06-20 16:59:15 -0700837 * This has been enhanced to work when FCoE stack is operating in target
838 * mode.
839 *
Robert Love1875f272009-11-03 11:47:50 -0800840 * Returns: True for read types I/O, otherwise returns false.
Vasu Devd7179682009-07-29 17:05:21 -0700841 */
Bart Van Assche7c9c6842012-01-13 17:26:25 -0800842static bool fcoe_oem_match(struct fc_frame *fp)
Vasu Devd7179682009-07-29 17:05:21 -0700843{
Kiran Patil1ff99182011-06-20 16:59:15 -0700844 struct fc_frame_header *fh = fc_frame_header_get(fp);
845 struct fcp_cmnd *fcp;
846
847 if (fc_fcp_is_read(fr_fsp(fp)) &&
848 (fr_fsp(fp)->data_len > fcoe_ddp_min))
849 return true;
Yi Zoua762dce2012-01-13 17:26:15 -0800850 else if ((fr_fsp(fp) == NULL) &&
851 (fh->fh_r_ctl == FC_RCTL_DD_UNSOL_CMD) &&
852 (ntohs(fh->fh_rx_id) == FC_XID_UNKNOWN)) {
Kiran Patil1ff99182011-06-20 16:59:15 -0700853 fcp = fc_frame_payload_get(fp, sizeof(*fcp));
Yi Zoua762dce2012-01-13 17:26:15 -0800854 if ((fcp->fc_flags & FCP_CFL_WRDATA) &&
855 (ntohl(fcp->fc_dl) > fcoe_ddp_min))
Kiran Patil1ff99182011-06-20 16:59:15 -0700856 return true;
857 }
858 return false;
Vasu Devd7179682009-07-29 17:05:21 -0700859}
860
Vasu Dev7f349142009-03-27 09:06:31 -0700861/**
Robert Love1875f272009-11-03 11:47:50 -0800862 * fcoe_em_config() - Allocate and configure an exchange manager
863 * @lport: The local port that the new EM will be associated with
Vasu Dev7f349142009-03-27 09:06:31 -0700864 *
Robert Love1875f272009-11-03 11:47:50 -0800865 * Returns: 0 on success
Vasu Dev7f349142009-03-27 09:06:31 -0700866 */
Robert Love1875f272009-11-03 11:47:50 -0800867static inline int fcoe_em_config(struct fc_lport *lport)
Vasu Dev7f349142009-03-27 09:06:31 -0700868{
Robert Love1875f272009-11-03 11:47:50 -0800869 struct fcoe_port *port = lport_priv(lport);
Bhanu Prakash Gollapudi8597ae82011-01-28 16:05:37 -0800870 struct fcoe_interface *fcoe = port->priv;
Chris Leech25024982009-08-25 13:59:35 -0700871 struct fcoe_interface *oldfcoe = NULL;
Vasu Dev1d1b88d2009-07-29 17:05:45 -0700872 struct net_device *old_real_dev, *cur_real_dev;
Vasu Devd7179682009-07-29 17:05:21 -0700873 u16 min_xid = FCOE_MIN_XID;
874 u16 max_xid = FCOE_MAX_XID;
875
876 /*
877 * Check if need to allocate an em instance for
878 * offload exchange ids to be shared across all VN_PORTs/lport.
879 */
Robert Love1875f272009-11-03 11:47:50 -0800880 if (!lport->lro_enabled || !lport->lro_xid ||
881 (lport->lro_xid >= max_xid)) {
882 lport->lro_xid = 0;
Vasu Devd7179682009-07-29 17:05:21 -0700883 goto skip_oem;
884 }
885
886 /*
887 * Reuse existing offload em instance in case
Vasu Dev1d1b88d2009-07-29 17:05:45 -0700888 * it is already allocated on real eth device
Vasu Devd7179682009-07-29 17:05:21 -0700889 */
Chris Leech25024982009-08-25 13:59:35 -0700890 if (fcoe->netdev->priv_flags & IFF_802_1Q_VLAN)
891 cur_real_dev = vlan_dev_real_dev(fcoe->netdev);
Vasu Dev1d1b88d2009-07-29 17:05:45 -0700892 else
Chris Leech25024982009-08-25 13:59:35 -0700893 cur_real_dev = fcoe->netdev;
Vasu Dev1d1b88d2009-07-29 17:05:45 -0700894
Chris Leech25024982009-08-25 13:59:35 -0700895 list_for_each_entry(oldfcoe, &fcoe_hostlist, list) {
Chris Leech25024982009-08-25 13:59:35 -0700896 if (oldfcoe->netdev->priv_flags & IFF_802_1Q_VLAN)
897 old_real_dev = vlan_dev_real_dev(oldfcoe->netdev);
Vasu Dev1d1b88d2009-07-29 17:05:45 -0700898 else
Chris Leech25024982009-08-25 13:59:35 -0700899 old_real_dev = oldfcoe->netdev;
Vasu Dev1d1b88d2009-07-29 17:05:45 -0700900
901 if (cur_real_dev == old_real_dev) {
Chris Leech991cbb62009-08-25 13:59:51 -0700902 fcoe->oem = oldfcoe->oem;
Vasu Devd7179682009-07-29 17:05:21 -0700903 break;
904 }
905 }
906
Chris Leech991cbb62009-08-25 13:59:51 -0700907 if (fcoe->oem) {
Robert Love1875f272009-11-03 11:47:50 -0800908 if (!fc_exch_mgr_add(lport, fcoe->oem, fcoe_oem_match)) {
Vasu Devd7179682009-07-29 17:05:21 -0700909 printk(KERN_ERR "fcoe_em_config: failed to add "
910 "offload em:%p on interface:%s\n",
Chris Leech991cbb62009-08-25 13:59:51 -0700911 fcoe->oem, fcoe->netdev->name);
Vasu Devd7179682009-07-29 17:05:21 -0700912 return -ENOMEM;
913 }
914 } else {
Robert Love1875f272009-11-03 11:47:50 -0800915 fcoe->oem = fc_exch_mgr_alloc(lport, FC_CLASS_3,
916 FCOE_MIN_XID, lport->lro_xid,
917 fcoe_oem_match);
Chris Leech991cbb62009-08-25 13:59:51 -0700918 if (!fcoe->oem) {
Vasu Devd7179682009-07-29 17:05:21 -0700919 printk(KERN_ERR "fcoe_em_config: failed to allocate "
920 "em for offload exches on interface:%s\n",
Chris Leech25024982009-08-25 13:59:35 -0700921 fcoe->netdev->name);
Vasu Devd7179682009-07-29 17:05:21 -0700922 return -ENOMEM;
923 }
924 }
925
926 /*
927 * Exclude offload EM xid range from next EM xid range.
928 */
Robert Love1875f272009-11-03 11:47:50 -0800929 min_xid += lport->lro_xid + 1;
Vasu Devd7179682009-07-29 17:05:21 -0700930
931skip_oem:
Robert Love1875f272009-11-03 11:47:50 -0800932 if (!fc_exch_mgr_alloc(lport, FC_CLASS_3, min_xid, max_xid, NULL)) {
Vasu Devd7179682009-07-29 17:05:21 -0700933 printk(KERN_ERR "fcoe_em_config: failed to "
Chris Leech25024982009-08-25 13:59:35 -0700934 "allocate em on interface %s\n", fcoe->netdev->name);
Vasu Dev7f349142009-03-27 09:06:31 -0700935 return -ENOMEM;
Vasu Devd7179682009-07-29 17:05:21 -0700936 }
Vasu Dev7f349142009-03-27 09:06:31 -0700937
938 return 0;
939}
940
941/**
Robert Love1875f272009-11-03 11:47:50 -0800942 * fcoe_if_destroy() - Tear down a SW FCoE instance
943 * @lport: The local port to be destroyed
Vasu Dev34ce27b2010-05-07 15:18:46 -0700944 *
Vasu Dev7f349142009-03-27 09:06:31 -0700945 */
Chris Leechaf7f85d2009-08-25 13:59:24 -0700946static void fcoe_if_destroy(struct fc_lport *lport)
Vasu Dev7f349142009-03-27 09:06:31 -0700947{
Neerav Parikhb2085a42011-06-20 16:59:51 -0700948 struct fcoe_port *port = lport_priv(lport);
949 struct fcoe_interface *fcoe = port->priv;
950 struct net_device *netdev = fcoe->netdev;
951
952 FCOE_NETDEV_DBG(netdev, "Destroying interface\n");
953
954 /* Logout of the fabric */
955 fc_fabric_logoff(lport);
956
957 /* Cleanup the fc_lport */
958 fc_lport_destroy(lport);
959
960 /* Stop the transmit retry timer */
961 del_timer_sync(&port->timer);
962
963 /* Free existing transmit skbs */
964 fcoe_clean_pending_queue(lport);
965
966 rtnl_lock();
967 if (!is_zero_ether_addr(port->data_src_addr))
968 dev_uc_del(netdev, port->data_src_addr);
969 rtnl_unlock();
970
971 /* Release reference held in fcoe_if_create() */
972 fcoe_interface_put(fcoe);
973
Chris Leech54b649f2009-08-25 14:00:07 -0700974 /* Free queued packets for the per-CPU receive threads */
975 fcoe_percpu_clean(lport);
976
Vasu Dev7f349142009-03-27 09:06:31 -0700977 /* Detach from the scsi-ml */
Chris Leechaf7f85d2009-08-25 13:59:24 -0700978 fc_remove_host(lport->host);
979 scsi_remove_host(lport->host);
Vasu Dev7f349142009-03-27 09:06:31 -0700980
Yi Zou80e736f2010-11-30 16:18:07 -0800981 /* Destroy lport scsi_priv */
982 fc_fcp_destroy(lport);
983
Vasu Dev7f349142009-03-27 09:06:31 -0700984 /* There are no more rports or I/O, free the EM */
Chris Leechaf7f85d2009-08-25 13:59:24 -0700985 fc_exch_mgr_free(lport);
Vasu Dev7f349142009-03-27 09:06:31 -0700986
Vasu Dev7f349142009-03-27 09:06:31 -0700987 /* Free memory used by statistical counters */
Chris Leechaf7f85d2009-08-25 13:59:24 -0700988 fc_lport_free_stats(lport);
Vasu Dev7f349142009-03-27 09:06:31 -0700989
Chris Leech2e70e242009-08-25 14:00:23 -0700990 /* Release the Scsi_Host */
Chris Leechaf7f85d2009-08-25 13:59:24 -0700991 scsi_host_put(lport->host);
Vasu Dev7f349142009-03-27 09:06:31 -0700992}
993
Robert Love1875f272009-11-03 11:47:50 -0800994/**
995 * fcoe_ddp_setup() - Call a LLD's ddp_setup through the net device
996 * @lport: The local port to setup DDP for
997 * @xid: The exchange ID for this DDP transfer
998 * @sgl: The scatterlist describing this transfer
999 * @sgc: The number of sg items
Vasu Dev7f349142009-03-27 09:06:31 -07001000 *
Robert Love1875f272009-11-03 11:47:50 -08001001 * Returns: 0 if the DDP context was not configured
Vasu Dev7f349142009-03-27 09:06:31 -07001002 */
Robert Love1875f272009-11-03 11:47:50 -08001003static int fcoe_ddp_setup(struct fc_lport *lport, u16 xid,
1004 struct scatterlist *sgl, unsigned int sgc)
Vasu Dev7f349142009-03-27 09:06:31 -07001005{
Robert Love1875f272009-11-03 11:47:50 -08001006 struct net_device *netdev = fcoe_netdev(lport);
Vasu Dev7f349142009-03-27 09:06:31 -07001007
Robert Love1875f272009-11-03 11:47:50 -08001008 if (netdev->netdev_ops->ndo_fcoe_ddp_setup)
1009 return netdev->netdev_ops->ndo_fcoe_ddp_setup(netdev,
1010 xid, sgl,
1011 sgc);
Vasu Dev7f349142009-03-27 09:06:31 -07001012
1013 return 0;
1014}
1015
Vasu Dev7f349142009-03-27 09:06:31 -07001016/**
Yi Zou71f89492011-06-20 16:59:10 -07001017 * fcoe_ddp_target() - Call a LLD's ddp_target through the net device
1018 * @lport: The local port to setup DDP for
1019 * @xid: The exchange ID for this DDP transfer
1020 * @sgl: The scatterlist describing this transfer
1021 * @sgc: The number of sg items
1022 *
1023 * Returns: 0 if the DDP context was not configured
1024 */
1025static int fcoe_ddp_target(struct fc_lport *lport, u16 xid,
1026 struct scatterlist *sgl, unsigned int sgc)
1027{
1028 struct net_device *netdev = fcoe_netdev(lport);
1029
1030 if (netdev->netdev_ops->ndo_fcoe_ddp_target)
1031 return netdev->netdev_ops->ndo_fcoe_ddp_target(netdev, xid,
1032 sgl, sgc);
1033
1034 return 0;
1035}
1036
1037
1038/**
Robert Love1875f272009-11-03 11:47:50 -08001039 * fcoe_ddp_done() - Call a LLD's ddp_done through the net device
1040 * @lport: The local port to complete DDP on
1041 * @xid: The exchange ID for this DDP transfer
Vasu Dev7f349142009-03-27 09:06:31 -07001042 *
Robert Love1875f272009-11-03 11:47:50 -08001043 * Returns: the length of data that have been completed by DDP
1044 */
1045static int fcoe_ddp_done(struct fc_lport *lport, u16 xid)
1046{
1047 struct net_device *netdev = fcoe_netdev(lport);
1048
1049 if (netdev->netdev_ops->ndo_fcoe_ddp_done)
1050 return netdev->netdev_ops->ndo_fcoe_ddp_done(netdev, xid);
1051 return 0;
1052}
1053
1054/**
1055 * fcoe_if_create() - Create a FCoE instance on an interface
1056 * @fcoe: The FCoE interface to create a local port on
1057 * @parent: The device pointer to be the parent in sysfs for the SCSI host
1058 * @npiv: Indicates if the port is a vport or not
Vasu Dev7f349142009-03-27 09:06:31 -07001059 *
Robert Love1875f272009-11-03 11:47:50 -08001060 * Creates a fc_lport instance and a Scsi_Host instance and configure them.
1061 *
1062 * Returns: The allocated fc_lport or an error pointer
Vasu Dev7f349142009-03-27 09:06:31 -07001063 */
Chris Leech030f4e02009-08-25 14:00:02 -07001064static struct fc_lport *fcoe_if_create(struct fcoe_interface *fcoe,
Chris Leech9a057532009-11-03 11:46:40 -08001065 struct device *parent, int npiv)
Vasu Dev7f349142009-03-27 09:06:31 -07001066{
Robert Love1875f272009-11-03 11:47:50 -08001067 struct net_device *netdev = fcoe->netdev;
Vasu Dev72fa3962011-02-25 15:03:01 -08001068 struct fc_lport *lport, *n_port;
Chris Leech014f5c32009-08-25 13:59:30 -07001069 struct fcoe_port *port;
Vasu Dev72fa3962011-02-25 15:03:01 -08001070 struct Scsi_Host *shost;
Robert Love1875f272009-11-03 11:47:50 -08001071 int rc;
Chris Leech9a057532009-11-03 11:46:40 -08001072 /*
1073 * parent is only a vport if npiv is 1,
1074 * but we'll only use vport in that case so go ahead and set it
1075 */
1076 struct fc_vport *vport = dev_to_vport(parent);
Vasu Dev7f349142009-03-27 09:06:31 -07001077
Robert Loved5488eb2009-06-10 15:30:59 -07001078 FCOE_NETDEV_DBG(netdev, "Create Interface\n");
Vasu Dev7f349142009-03-27 09:06:31 -07001079
Vasu Dev72fa3962011-02-25 15:03:01 -08001080 if (!npiv)
1081 lport = libfc_host_alloc(&fcoe_shost_template, sizeof(*port));
1082 else
1083 lport = libfc_vport_create(vport, sizeof(*port));
1084
Chris Leech86221962009-11-03 11:46:08 -08001085 if (!lport) {
Chris Leech014f5c32009-08-25 13:59:30 -07001086 FCOE_NETDEV_DBG(netdev, "Could not allocate host structure\n");
1087 rc = -ENOMEM;
Chris Leech030f4e02009-08-25 14:00:02 -07001088 goto out;
Chris Leech014f5c32009-08-25 13:59:30 -07001089 }
Chris Leech014f5c32009-08-25 13:59:30 -07001090 port = lport_priv(lport);
Chris Leech2e70e242009-08-25 14:00:23 -07001091 port->lport = lport;
Bhanu Prakash Gollapudi8597ae82011-01-28 16:05:37 -08001092 port->priv = fcoe;
1093 port->max_queue_depth = FCOE_MAX_QUEUE_DEPTH;
1094 port->min_queue_depth = FCOE_MIN_QUEUE_DEPTH;
Chris Leech2e70e242009-08-25 14:00:23 -07001095 INIT_WORK(&port->destroy_work, fcoe_destroy_work);
Vasu Dev7f349142009-03-27 09:06:31 -07001096
Robert Love1875f272009-11-03 11:47:50 -08001097 /* configure a fc_lport including the exchange manager */
Chris Leechaf7f85d2009-08-25 13:59:24 -07001098 rc = fcoe_lport_config(lport);
Vasu Dev7f349142009-03-27 09:06:31 -07001099 if (rc) {
Robert Loved5488eb2009-06-10 15:30:59 -07001100 FCOE_NETDEV_DBG(netdev, "Could not configure lport for the "
1101 "interface\n");
Vasu Dev7f349142009-03-27 09:06:31 -07001102 goto out_host_put;
1103 }
1104
Chris Leech9a057532009-11-03 11:46:40 -08001105 if (npiv) {
Chris Leech9f8f3aa2010-04-09 14:23:16 -07001106 FCOE_NETDEV_DBG(netdev, "Setting vport names, "
1107 "%16.16llx %16.16llx\n",
Robert Love1875f272009-11-03 11:47:50 -08001108 vport->node_name, vport->port_name);
Chris Leech9a057532009-11-03 11:46:40 -08001109 fc_set_wwnn(lport, vport->node_name);
1110 fc_set_wwpn(lport, vport->port_name);
1111 }
1112
Vasu Devab6b85c2009-05-17 12:33:08 +00001113 /* configure lport network properties */
Chris Leechaf7f85d2009-08-25 13:59:24 -07001114 rc = fcoe_netdev_config(lport, netdev);
Vasu Devab6b85c2009-05-17 12:33:08 +00001115 if (rc) {
Robert Loved5488eb2009-06-10 15:30:59 -07001116 FCOE_NETDEV_DBG(netdev, "Could not configure netdev for the "
1117 "interface\n");
Chris Leech54b649f2009-08-25 14:00:07 -07001118 goto out_lp_destroy;
Vasu Devab6b85c2009-05-17 12:33:08 +00001119 }
Joe Eykholt97c83892009-03-17 11:42:40 -07001120
Vasu Dev7f349142009-03-27 09:06:31 -07001121 /* configure lport scsi host properties */
Vasu Dev8ba00a42010-04-09 14:22:54 -07001122 rc = fcoe_shost_config(lport, parent);
Vasu Dev7f349142009-03-27 09:06:31 -07001123 if (rc) {
Robert Loved5488eb2009-06-10 15:30:59 -07001124 FCOE_NETDEV_DBG(netdev, "Could not configure shost for the "
1125 "interface\n");
Chris Leech54b649f2009-08-25 14:00:07 -07001126 goto out_lp_destroy;
Vasu Dev7f349142009-03-27 09:06:31 -07001127 }
1128
Vasu Dev7f349142009-03-27 09:06:31 -07001129 /* Initialize the library */
Joe Eykholte10f8c62010-07-20 15:20:30 -07001130 rc = fcoe_libfc_config(lport, &fcoe->ctlr, &fcoe_libfc_fcn_templ, 1);
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 libfc for the "
1133 "interface\n");
Vasu Dev7f349142009-03-27 09:06:31 -07001134 goto out_lp_destroy;
1135 }
1136
Neerav Parikh6fef3902012-01-22 17:30:10 -08001137 /* Initialized FDMI information */
1138 fcoe_fdmi_info(lport, netdev);
1139
Vasu Dev72fa3962011-02-25 15:03:01 -08001140 /*
1141 * fcoe_em_alloc() and fcoe_hostlist_add() both
1142 * need to be atomic with respect to other changes to the
1143 * hostlist since fcoe_em_alloc() looks for an existing EM
1144 * instance on host list updated by fcoe_hostlist_add().
1145 *
1146 * This is currently handled through the fcoe_config_mutex
1147 * begin held.
1148 */
1149 if (!npiv)
Chris Leech9a057532009-11-03 11:46:40 -08001150 /* lport exch manager allocation */
1151 rc = fcoe_em_config(lport);
Vasu Dev72fa3962011-02-25 15:03:01 -08001152 else {
1153 shost = vport_to_shost(vport);
1154 n_port = shost_priv(shost);
1155 rc = fc_exch_mgr_list_clone(n_port, lport);
1156 }
1157
1158 if (rc) {
1159 FCOE_NETDEV_DBG(netdev, "Could not configure the EM\n");
1160 goto out_lp_destroy;
Vasu Dev96316092009-07-29 17:05:00 -07001161 }
1162
Chris Leech030f4e02009-08-25 14:00:02 -07001163 fcoe_interface_get(fcoe);
Chris Leechaf7f85d2009-08-25 13:59:24 -07001164 return lport;
Vasu Dev7f349142009-03-27 09:06:31 -07001165
1166out_lp_destroy:
Chris Leechaf7f85d2009-08-25 13:59:24 -07001167 fc_exch_mgr_free(lport);
Vasu Dev7f349142009-03-27 09:06:31 -07001168out_host_put:
Chris Leechaf7f85d2009-08-25 13:59:24 -07001169 scsi_host_put(lport->host);
1170out:
1171 return ERR_PTR(rc);
Vasu Dev7f349142009-03-27 09:06:31 -07001172}
1173
1174/**
Robert Love1875f272009-11-03 11:47:50 -08001175 * fcoe_if_init() - Initialization routine for fcoe.ko
Vasu Dev7f349142009-03-27 09:06:31 -07001176 *
Robert Love1875f272009-11-03 11:47:50 -08001177 * Attaches the SW FCoE transport to the FC transport
1178 *
1179 * Returns: 0 on success
Vasu Dev7f349142009-03-27 09:06:31 -07001180 */
1181static int __init fcoe_if_init(void)
1182{
1183 /* attach to scsi transport */
Yi Zou8ca86f82011-01-28 16:05:11 -08001184 fcoe_nport_scsi_transport =
1185 fc_attach_transport(&fcoe_nport_fc_functions);
1186 fcoe_vport_scsi_transport =
1187 fc_attach_transport(&fcoe_vport_fc_functions);
Vasu Dev7f349142009-03-27 09:06:31 -07001188
Yi Zou8ca86f82011-01-28 16:05:11 -08001189 if (!fcoe_nport_scsi_transport) {
Robert Loved5488eb2009-06-10 15:30:59 -07001190 printk(KERN_ERR "fcoe: Failed to attach to the FC transport\n");
Vasu Dev7f349142009-03-27 09:06:31 -07001191 return -ENODEV;
1192 }
1193
1194 return 0;
1195}
1196
1197/**
Robert Love1875f272009-11-03 11:47:50 -08001198 * fcoe_if_exit() - Tear down fcoe.ko
Vasu Dev7f349142009-03-27 09:06:31 -07001199 *
Robert Love1875f272009-11-03 11:47:50 -08001200 * Detaches the SW FCoE transport from the FC transport
1201 *
1202 * Returns: 0 on success
Vasu Dev7f349142009-03-27 09:06:31 -07001203 */
Bart Van Assche7c9c6842012-01-13 17:26:25 -08001204static int __exit fcoe_if_exit(void)
Vasu Dev7f349142009-03-27 09:06:31 -07001205{
Yi Zou8ca86f82011-01-28 16:05:11 -08001206 fc_release_transport(fcoe_nport_scsi_transport);
1207 fc_release_transport(fcoe_vport_scsi_transport);
1208 fcoe_nport_scsi_transport = NULL;
1209 fcoe_vport_scsi_transport = NULL;
Vasu Dev7f349142009-03-27 09:06:31 -07001210 return 0;
1211}
1212
Robert Love85b4aa42008-12-09 15:10:24 -08001213/**
Robert Love1875f272009-11-03 11:47:50 -08001214 * fcoe_percpu_thread_create() - Create a receive thread for an online CPU
1215 * @cpu: The CPU index of the CPU to create a receive thread for
Robert Love8976f422009-03-17 11:41:46 -07001216 */
1217static void fcoe_percpu_thread_create(unsigned int cpu)
1218{
1219 struct fcoe_percpu_s *p;
1220 struct task_struct *thread;
1221
1222 p = &per_cpu(fcoe_percpu, cpu);
1223
Eric Dumazet5c609ff2011-09-27 21:37:52 -07001224 thread = kthread_create_on_node(fcoe_percpu_receive_thread,
1225 (void *)p, cpu_to_node(cpu),
1226 "fcoethread/%d", cpu);
Robert Love8976f422009-03-17 11:41:46 -07001227
Joe Eykholte7a51992009-08-25 14:04:08 -07001228 if (likely(!IS_ERR(thread))) {
Robert Love8976f422009-03-17 11:41:46 -07001229 kthread_bind(thread, cpu);
1230 wake_up_process(thread);
1231
1232 spin_lock_bh(&p->fcoe_rx_list.lock);
1233 p->thread = thread;
1234 spin_unlock_bh(&p->fcoe_rx_list.lock);
1235 }
1236}
1237
1238/**
Robert Love1875f272009-11-03 11:47:50 -08001239 * fcoe_percpu_thread_destroy() - Remove the receive thread of a CPU
1240 * @cpu: The CPU index of the CPU whose receive thread is to be destroyed
Robert Love8976f422009-03-17 11:41:46 -07001241 *
1242 * Destroys a per-CPU Rx thread. Any pending skbs are moved to the
1243 * current CPU's Rx thread. If the thread being destroyed is bound to
1244 * the CPU processing this context the skbs will be freed.
1245 */
1246static void fcoe_percpu_thread_destroy(unsigned int cpu)
1247{
1248 struct fcoe_percpu_s *p;
1249 struct task_struct *thread;
1250 struct page *crc_eof;
1251 struct sk_buff *skb;
1252#ifdef CONFIG_SMP
1253 struct fcoe_percpu_s *p0;
Joe Eykholtf018b732010-03-12 16:08:55 -08001254 unsigned targ_cpu = get_cpu();
Robert Love8976f422009-03-17 11:41:46 -07001255#endif /* CONFIG_SMP */
1256
Robert Loved5488eb2009-06-10 15:30:59 -07001257 FCOE_DBG("Destroying receive thread for CPU %d\n", cpu);
Robert Love8976f422009-03-17 11:41:46 -07001258
1259 /* Prevent any new skbs from being queued for this CPU. */
1260 p = &per_cpu(fcoe_percpu, cpu);
1261 spin_lock_bh(&p->fcoe_rx_list.lock);
1262 thread = p->thread;
1263 p->thread = NULL;
1264 crc_eof = p->crc_eof_page;
1265 p->crc_eof_page = NULL;
1266 p->crc_eof_offset = 0;
1267 spin_unlock_bh(&p->fcoe_rx_list.lock);
1268
1269#ifdef CONFIG_SMP
1270 /*
1271 * Don't bother moving the skb's if this context is running
1272 * on the same CPU that is having its thread destroyed. This
1273 * can easily happen when the module is removed.
1274 */
1275 if (cpu != targ_cpu) {
1276 p0 = &per_cpu(fcoe_percpu, targ_cpu);
1277 spin_lock_bh(&p0->fcoe_rx_list.lock);
1278 if (p0->thread) {
Robert Loved5488eb2009-06-10 15:30:59 -07001279 FCOE_DBG("Moving frames from CPU %d to CPU %d\n",
1280 cpu, targ_cpu);
Robert Love8976f422009-03-17 11:41:46 -07001281
1282 while ((skb = __skb_dequeue(&p->fcoe_rx_list)) != NULL)
1283 __skb_queue_tail(&p0->fcoe_rx_list, skb);
1284 spin_unlock_bh(&p0->fcoe_rx_list.lock);
1285 } else {
1286 /*
1287 * The targeted CPU is not initialized and cannot accept
Robert Love1875f272009-11-03 11:47:50 -08001288 * new skbs. Unlock the targeted CPU and drop the skbs
Robert Love8976f422009-03-17 11:41:46 -07001289 * on the CPU that is going offline.
1290 */
1291 while ((skb = __skb_dequeue(&p->fcoe_rx_list)) != NULL)
1292 kfree_skb(skb);
1293 spin_unlock_bh(&p0->fcoe_rx_list.lock);
1294 }
1295 } else {
1296 /*
1297 * This scenario occurs when the module is being removed
1298 * and all threads are being destroyed. skbs will continue
1299 * to be shifted from the CPU thread that is being removed
1300 * to the CPU thread associated with the CPU that is processing
1301 * the module removal. Once there is only one CPU Rx thread it
1302 * will reach this case and we will drop all skbs and later
1303 * stop the thread.
1304 */
1305 spin_lock_bh(&p->fcoe_rx_list.lock);
1306 while ((skb = __skb_dequeue(&p->fcoe_rx_list)) != NULL)
1307 kfree_skb(skb);
1308 spin_unlock_bh(&p->fcoe_rx_list.lock);
1309 }
Joe Eykholtf018b732010-03-12 16:08:55 -08001310 put_cpu();
Robert Love8976f422009-03-17 11:41:46 -07001311#else
1312 /*
Chris Leechdd3fd722009-04-21 16:27:36 -07001313 * This a non-SMP scenario where the singular Rx thread is
Robert Love8976f422009-03-17 11:41:46 -07001314 * being removed. Free all skbs and stop the thread.
1315 */
1316 spin_lock_bh(&p->fcoe_rx_list.lock);
1317 while ((skb = __skb_dequeue(&p->fcoe_rx_list)) != NULL)
1318 kfree_skb(skb);
1319 spin_unlock_bh(&p->fcoe_rx_list.lock);
1320#endif
1321
1322 if (thread)
1323 kthread_stop(thread);
1324
1325 if (crc_eof)
1326 put_page(crc_eof);
1327}
1328
1329/**
Robert Love1875f272009-11-03 11:47:50 -08001330 * fcoe_cpu_callback() - Handler for CPU hotplug events
1331 * @nfb: The callback data block
1332 * @action: The event triggering the callback
1333 * @hcpu: The index of the CPU that the event is for
Robert Love8976f422009-03-17 11:41:46 -07001334 *
Robert Love1875f272009-11-03 11:47:50 -08001335 * This creates or destroys per-CPU data for fcoe
Robert Love8976f422009-03-17 11:41:46 -07001336 *
1337 * Returns NOTIFY_OK always.
1338 */
1339static int fcoe_cpu_callback(struct notifier_block *nfb,
1340 unsigned long action, void *hcpu)
1341{
1342 unsigned cpu = (unsigned long)hcpu;
1343
1344 switch (action) {
1345 case CPU_ONLINE:
1346 case CPU_ONLINE_FROZEN:
Robert Loved5488eb2009-06-10 15:30:59 -07001347 FCOE_DBG("CPU %x online: Create Rx thread\n", cpu);
Robert Love8976f422009-03-17 11:41:46 -07001348 fcoe_percpu_thread_create(cpu);
1349 break;
1350 case CPU_DEAD:
1351 case CPU_DEAD_FROZEN:
Robert Loved5488eb2009-06-10 15:30:59 -07001352 FCOE_DBG("CPU %x offline: Remove Rx thread\n", cpu);
Robert Love8976f422009-03-17 11:41:46 -07001353 fcoe_percpu_thread_destroy(cpu);
1354 break;
1355 default:
1356 break;
1357 }
1358 return NOTIFY_OK;
1359}
1360
Robert Love8976f422009-03-17 11:41:46 -07001361/**
Kiran Patil064287e2011-06-20 16:59:20 -07001362 * fcoe_select_cpu() - Selects CPU to handle post-processing of incoming
1363 * command.
Kiran Patil064287e2011-06-20 16:59:20 -07001364 *
Vasu Devd2722812011-07-27 15:11:10 -07001365 * This routine selects next CPU based on cpumask to distribute
1366 * incoming requests in round robin.
Kiran Patil064287e2011-06-20 16:59:20 -07001367 *
Vasu Devd2722812011-07-27 15:11:10 -07001368 * Returns: int CPU number
Kiran Patil064287e2011-06-20 16:59:20 -07001369 */
Vasu Devd2722812011-07-27 15:11:10 -07001370static inline unsigned int fcoe_select_cpu(void)
Kiran Patil064287e2011-06-20 16:59:20 -07001371{
1372 static unsigned int selected_cpu;
1373
Vasu Devd2722812011-07-27 15:11:10 -07001374 selected_cpu = cpumask_next(selected_cpu, cpu_online_mask);
1375 if (selected_cpu >= nr_cpu_ids)
1376 selected_cpu = cpumask_first(cpu_online_mask);
1377
Kiran Patil064287e2011-06-20 16:59:20 -07001378 return selected_cpu;
1379}
1380
1381/**
Robert Love1875f272009-11-03 11:47:50 -08001382 * fcoe_rcv() - Receive packets from a net device
1383 * @skb: The received packet
1384 * @netdev: The net device that the packet was received on
1385 * @ptype: The packet type context
1386 * @olddev: The last device net device
Robert Love85b4aa42008-12-09 15:10:24 -08001387 *
Robert Love1875f272009-11-03 11:47:50 -08001388 * This routine is called by NET_RX_SOFTIRQ. It receives a packet, builds a
1389 * FC frame and passes the frame to libfc.
Robert Love85b4aa42008-12-09 15:10:24 -08001390 *
1391 * Returns: 0 for success
Robert Love34f42a02009-02-27 10:55:45 -08001392 */
Bart Van Assche7c9c6842012-01-13 17:26:25 -08001393static int fcoe_rcv(struct sk_buff *skb, struct net_device *netdev,
Robert Love85b4aa42008-12-09 15:10:24 -08001394 struct packet_type *ptype, struct net_device *olddev)
1395{
Robert Love1875f272009-11-03 11:47:50 -08001396 struct fc_lport *lport;
Robert Love85b4aa42008-12-09 15:10:24 -08001397 struct fcoe_rcv_info *fr;
Chris Leech259ad852009-08-25 13:59:41 -07001398 struct fcoe_interface *fcoe;
Robert Love85b4aa42008-12-09 15:10:24 -08001399 struct fc_frame_header *fh;
Robert Love85b4aa42008-12-09 15:10:24 -08001400 struct fcoe_percpu_s *fps;
Vasu Dev519e5132010-07-20 15:19:32 -07001401 struct ethhdr *eh;
Vasu Devb2f00912009-08-25 13:58:53 -07001402 unsigned int cpu;
Robert Love85b4aa42008-12-09 15:10:24 -08001403
Chris Leech259ad852009-08-25 13:59:41 -07001404 fcoe = container_of(ptype, struct fcoe_interface, fcoe_packet_type);
Robert Love1875f272009-11-03 11:47:50 -08001405 lport = fcoe->ctlr.lp;
1406 if (unlikely(!lport)) {
1407 FCOE_NETDEV_DBG(netdev, "Cannot find hba structure");
Robert Love85b4aa42008-12-09 15:10:24 -08001408 goto err2;
1409 }
Robert Love1875f272009-11-03 11:47:50 -08001410 if (!lport->link_up)
Joe Eykholt97c83892009-03-17 11:42:40 -07001411 goto err2;
Robert Love85b4aa42008-12-09 15:10:24 -08001412
Robert Love1875f272009-11-03 11:47:50 -08001413 FCOE_NETDEV_DBG(netdev, "skb_info: len:%d data_len:%d head:%p "
Robert Loved5488eb2009-06-10 15:30:59 -07001414 "data:%p tail:%p end:%p sum:%d dev:%s",
1415 skb->len, skb->data_len, skb->head, skb->data,
1416 skb_tail_pointer(skb), skb_end_pointer(skb),
1417 skb->csum, skb->dev ? skb->dev->name : "<NULL>");
Robert Love85b4aa42008-12-09 15:10:24 -08001418
Vasu Dev519e5132010-07-20 15:19:32 -07001419 eh = eth_hdr(skb);
Vasu Dev519e5132010-07-20 15:19:32 -07001420
1421 if (is_fip_mode(&fcoe->ctlr) &&
1422 compare_ether_addr(eh->h_source, fcoe->ctlr.dest_addr)) {
1423 FCOE_NETDEV_DBG(netdev, "wrong source mac address:%pM\n",
1424 eh->h_source);
Robert Love85b4aa42008-12-09 15:10:24 -08001425 goto err;
1426 }
1427
1428 /*
1429 * Check for minimum frame length, and make sure required FCoE
1430 * and FC headers are pulled into the linear data area.
1431 */
1432 if (unlikely((skb->len < FCOE_MIN_FRAME) ||
Robert Love1875f272009-11-03 11:47:50 -08001433 !pskb_may_pull(skb, FCOE_HEADER_LEN)))
Robert Love85b4aa42008-12-09 15:10:24 -08001434 goto err;
1435
1436 skb_set_transport_header(skb, sizeof(struct fcoe_hdr));
1437 fh = (struct fc_frame_header *) skb_transport_header(skb);
1438
Robert Love0ee31cb2010-10-08 17:12:46 -07001439 if (ntoh24(&eh->h_dest[3]) != ntoh24(fh->fh_d_id)) {
1440 FCOE_NETDEV_DBG(netdev, "FC frame d_id mismatch with MAC:%pM\n",
1441 eh->h_dest);
1442 goto err;
1443 }
1444
Robert Love85b4aa42008-12-09 15:10:24 -08001445 fr = fcoe_dev_from_skb(skb);
Robert Love1875f272009-11-03 11:47:50 -08001446 fr->fr_dev = lport;
Robert Love5e5e92d2009-03-17 11:41:35 -07001447
Robert Love85b4aa42008-12-09 15:10:24 -08001448 /*
Vasu Devb2f00912009-08-25 13:58:53 -07001449 * In case the incoming frame's exchange is originated from
1450 * the initiator, then received frame's exchange id is ANDed
1451 * with fc_cpu_mask bits to get the same cpu on which exchange
Vasu Devd2722812011-07-27 15:11:10 -07001452 * was originated, otherwise select cpu using rx exchange id
1453 * or fcoe_select_cpu().
Robert Love85b4aa42008-12-09 15:10:24 -08001454 */
Vasu Devb2f00912009-08-25 13:58:53 -07001455 if (ntoh24(fh->fh_f_ctl) & FC_FC_EX_CTX)
1456 cpu = ntohs(fh->fh_ox_id) & fc_cpu_mask;
Kiran Patil064287e2011-06-20 16:59:20 -07001457 else {
Vasu Devd2722812011-07-27 15:11:10 -07001458 if (ntohs(fh->fh_rx_id) == FC_XID_UNKNOWN)
1459 cpu = fcoe_select_cpu();
1460 else
Kiran Patil29bdd2b2011-06-20 16:59:25 -07001461 cpu = ntohs(fh->fh_rx_id) & fc_cpu_mask;
Kiran Patil064287e2011-06-20 16:59:20 -07001462 }
Vasu Dev324f6672011-07-27 15:10:39 -07001463
1464 if (cpu >= nr_cpu_ids)
1465 goto err;
1466
Robert Love8976f422009-03-17 11:41:46 -07001467 fps = &per_cpu(fcoe_percpu, cpu);
Robert Love85b4aa42008-12-09 15:10:24 -08001468 spin_lock_bh(&fps->fcoe_rx_list.lock);
Robert Love8976f422009-03-17 11:41:46 -07001469 if (unlikely(!fps->thread)) {
1470 /*
1471 * The targeted CPU is not ready, let's target
1472 * the first CPU now. For non-SMP systems this
1473 * will check the same CPU twice.
1474 */
Robert Love1875f272009-11-03 11:47:50 -08001475 FCOE_NETDEV_DBG(netdev, "CPU is online, but no receive thread "
Robert Loved5488eb2009-06-10 15:30:59 -07001476 "ready for incoming skb- using first online "
1477 "CPU.\n");
Robert Love8976f422009-03-17 11:41:46 -07001478
1479 spin_unlock_bh(&fps->fcoe_rx_list.lock);
Rusty Russell69571772009-12-17 11:43:14 -06001480 cpu = cpumask_first(cpu_online_mask);
Robert Love8976f422009-03-17 11:41:46 -07001481 fps = &per_cpu(fcoe_percpu, cpu);
1482 spin_lock_bh(&fps->fcoe_rx_list.lock);
1483 if (!fps->thread) {
1484 spin_unlock_bh(&fps->fcoe_rx_list.lock);
1485 goto err;
1486 }
1487 }
1488
1489 /*
1490 * We now have a valid CPU that we're targeting for
1491 * this skb. We also have this receive thread locked,
1492 * so we're free to queue skbs into it's queue.
1493 */
Robert Love85b4aa42008-12-09 15:10:24 -08001494
Chris Leech859b7b62009-11-20 14:54:47 -08001495 /* If this is a SCSI-FCP frame, and this is already executing on the
1496 * correct CPU, and the queue for this CPU is empty, then go ahead
1497 * and process the frame directly in the softirq context.
1498 * This lets us process completions without context switching from the
1499 * NET_RX softirq, to our receive processing thread, and then back to
1500 * BLOCK softirq context.
1501 */
1502 if (fh->fh_type == FC_TYPE_FCP &&
1503 cpu == smp_processor_id() &&
1504 skb_queue_empty(&fps->fcoe_rx_list)) {
1505 spin_unlock_bh(&fps->fcoe_rx_list.lock);
1506 fcoe_recv_frame(skb);
1507 } else {
1508 __skb_queue_tail(&fps->fcoe_rx_list, skb);
1509 if (fps->fcoe_rx_list.qlen == 1)
1510 wake_up_process(fps->thread);
1511 spin_unlock_bh(&fps->fcoe_rx_list.lock);
1512 }
Robert Love85b4aa42008-12-09 15:10:24 -08001513
1514 return 0;
1515err:
Joe Eykholtf018b732010-03-12 16:08:55 -08001516 per_cpu_ptr(lport->dev_stats, get_cpu())->ErrorFrames++;
1517 put_cpu();
Robert Love85b4aa42008-12-09 15:10:24 -08001518err2:
1519 kfree_skb(skb);
1520 return -1;
1521}
Robert Love85b4aa42008-12-09 15:10:24 -08001522
1523/**
Bhanu Prakash Gollapudi8597ae82011-01-28 16:05:37 -08001524 * fcoe_alloc_paged_crc_eof() - Allocate a page to be used for the trailer CRC
Robert Love1875f272009-11-03 11:47:50 -08001525 * @skb: The packet to be transmitted
1526 * @tlen: The total length of the trailer
1527 *
Robert Love85b4aa42008-12-09 15:10:24 -08001528 * Returns: 0 for success
Robert Love34f42a02009-02-27 10:55:45 -08001529 */
Bhanu Prakash Gollapudi8597ae82011-01-28 16:05:37 -08001530static int fcoe_alloc_paged_crc_eof(struct sk_buff *skb, int tlen)
Robert Love85b4aa42008-12-09 15:10:24 -08001531{
1532 struct fcoe_percpu_s *fps;
Bhanu Prakash Gollapudi8597ae82011-01-28 16:05:37 -08001533 int rc;
Robert Love85b4aa42008-12-09 15:10:24 -08001534
Robert Love5e5e92d2009-03-17 11:41:35 -07001535 fps = &get_cpu_var(fcoe_percpu);
Bhanu Prakash Gollapudi8597ae82011-01-28 16:05:37 -08001536 rc = fcoe_get_paged_crc_eof(skb, tlen, fps);
Robert Love5e5e92d2009-03-17 11:41:35 -07001537 put_cpu_var(fcoe_percpu);
Robert Love85b4aa42008-12-09 15:10:24 -08001538
Bhanu Prakash Gollapudi8597ae82011-01-28 16:05:37 -08001539 return rc;
Robert Love85b4aa42008-12-09 15:10:24 -08001540}
Robert Love85b4aa42008-12-09 15:10:24 -08001541
1542/**
Robert Love1875f272009-11-03 11:47:50 -08001543 * fcoe_xmit() - Transmit a FCoE frame
1544 * @lport: The local port that the frame is to be transmitted for
1545 * @fp: The frame to be transmitted
Robert Love85b4aa42008-12-09 15:10:24 -08001546 *
Robert Love1875f272009-11-03 11:47:50 -08001547 * Return: 0 for success
Robert Love34f42a02009-02-27 10:55:45 -08001548 */
Bart Van Assche7c9c6842012-01-13 17:26:25 -08001549static int fcoe_xmit(struct fc_lport *lport, struct fc_frame *fp)
Robert Love85b4aa42008-12-09 15:10:24 -08001550{
Vasu Dev4bb6b512009-05-06 10:52:34 -07001551 int wlen;
Robert Love85b4aa42008-12-09 15:10:24 -08001552 u32 crc;
1553 struct ethhdr *eh;
1554 struct fcoe_crc_eof *cp;
1555 struct sk_buff *skb;
1556 struct fcoe_dev_stats *stats;
1557 struct fc_frame_header *fh;
1558 unsigned int hlen; /* header length implies the version */
1559 unsigned int tlen; /* trailer length */
1560 unsigned int elen; /* eth header, may include vlan */
Robert Love1875f272009-11-03 11:47:50 -08001561 struct fcoe_port *port = lport_priv(lport);
Bhanu Prakash Gollapudi8597ae82011-01-28 16:05:37 -08001562 struct fcoe_interface *fcoe = port->priv;
Robert Love85b4aa42008-12-09 15:10:24 -08001563 u8 sof, eof;
1564 struct fcoe_hdr *hp;
1565
1566 WARN_ON((fr_len(fp) % sizeof(u32)) != 0);
1567
Robert Love85b4aa42008-12-09 15:10:24 -08001568 fh = fc_frame_header_get(fp);
Joe Eykholt97c83892009-03-17 11:42:40 -07001569 skb = fp_skb(fp);
1570 wlen = skb->len / FCOE_WORD_TO_BYTE;
1571
Robert Love1875f272009-11-03 11:47:50 -08001572 if (!lport->link_up) {
Dan Carpenter3caf02e2009-04-21 16:27:25 -07001573 kfree_skb(skb);
Joe Eykholt97c83892009-03-17 11:42:40 -07001574 return 0;
Robert Love85b4aa42008-12-09 15:10:24 -08001575 }
1576
Joe Eykholt9860eeb2010-03-12 16:07:52 -08001577 if (unlikely(fh->fh_type == FC_TYPE_ELS) &&
Robert Love1875f272009-11-03 11:47:50 -08001578 fcoe_ctlr_els_send(&fcoe->ctlr, lport, skb))
Joe Eykholt97c83892009-03-17 11:42:40 -07001579 return 0;
1580
Robert Love85b4aa42008-12-09 15:10:24 -08001581 sof = fr_sof(fp);
1582 eof = fr_eof(fp);
1583
Vasu Dev4e57e1c2009-05-06 10:52:46 -07001584 elen = sizeof(struct ethhdr);
Robert Love85b4aa42008-12-09 15:10:24 -08001585 hlen = sizeof(struct fcoe_hdr);
1586 tlen = sizeof(struct fcoe_crc_eof);
1587 wlen = (skb->len - tlen + sizeof(crc)) / FCOE_WORD_TO_BYTE;
1588
1589 /* crc offload */
Robert Love1875f272009-11-03 11:47:50 -08001590 if (likely(lport->crc_offload)) {
Yi Zou39ca9a02009-02-27 14:07:15 -08001591 skb->ip_summed = CHECKSUM_PARTIAL;
Robert Love85b4aa42008-12-09 15:10:24 -08001592 skb->csum_start = skb_headroom(skb);
1593 skb->csum_offset = skb->len;
1594 crc = 0;
1595 } else {
1596 skb->ip_summed = CHECKSUM_NONE;
1597 crc = fcoe_fc_crc(fp);
1598 }
1599
Chris Leech014f5c32009-08-25 13:59:30 -07001600 /* copy port crc and eof to the skb buff */
Robert Love85b4aa42008-12-09 15:10:24 -08001601 if (skb_is_nonlinear(skb)) {
1602 skb_frag_t *frag;
Bhanu Prakash Gollapudi8597ae82011-01-28 16:05:37 -08001603 if (fcoe_alloc_paged_crc_eof(skb, tlen)) {
Roel Kluine9041582009-02-27 10:56:22 -08001604 kfree_skb(skb);
Robert Love85b4aa42008-12-09 15:10:24 -08001605 return -ENOMEM;
1606 }
1607 frag = &skb_shinfo(skb)->frags[skb_shinfo(skb)->nr_frags - 1];
Ian Campbell165c68d2011-08-24 22:28:15 +00001608 cp = kmap_atomic(skb_frag_page(frag), KM_SKB_DATA_SOFTIRQ)
Robert Love85b4aa42008-12-09 15:10:24 -08001609 + frag->page_offset;
1610 } else {
1611 cp = (struct fcoe_crc_eof *)skb_put(skb, tlen);
1612 }
1613
1614 memset(cp, 0, sizeof(*cp));
1615 cp->fcoe_eof = eof;
1616 cp->fcoe_crc32 = cpu_to_le32(~crc);
1617
1618 if (skb_is_nonlinear(skb)) {
1619 kunmap_atomic(cp, KM_SKB_DATA_SOFTIRQ);
1620 cp = NULL;
1621 }
1622
Chris Leech014f5c32009-08-25 13:59:30 -07001623 /* adjust skb network/transport offsets to match mac/fcoe/port */
Robert Love85b4aa42008-12-09 15:10:24 -08001624 skb_push(skb, elen + hlen);
1625 skb_reset_mac_header(skb);
1626 skb_reset_network_header(skb);
1627 skb->mac_len = elen;
Yi Zou211c7382009-02-27 14:06:37 -08001628 skb->protocol = htons(ETH_P_FCOE);
john fastabend6f6c2aa2011-11-18 13:35:56 -08001629 skb->priority = port->priority;
1630
Vasu Devd1483bb2011-09-27 21:38:13 -07001631 if (fcoe->netdev->priv_flags & IFF_802_1Q_VLAN &&
1632 fcoe->realdev->features & NETIF_F_HW_VLAN_TX) {
1633 skb->vlan_tci = VLAN_TAG_PRESENT |
1634 vlan_dev_vlan_id(fcoe->netdev);
1635 skb->dev = fcoe->realdev;
1636 } else
1637 skb->dev = fcoe->netdev;
Robert Love85b4aa42008-12-09 15:10:24 -08001638
1639 /* fill up mac and fcoe headers */
1640 eh = eth_hdr(skb);
1641 eh->h_proto = htons(ETH_P_FCOE);
Joe Eykholtcd229e42010-07-20 15:20:40 -07001642 memcpy(eh->h_dest, fcoe->ctlr.dest_addr, ETH_ALEN);
Chris Leech3fe9a0b2009-08-25 13:59:46 -07001643 if (fcoe->ctlr.map_dest)
Joe Eykholtcd229e42010-07-20 15:20:40 -07001644 memcpy(eh->h_dest + 3, fh->fh_d_id, 3);
Robert Love85b4aa42008-12-09 15:10:24 -08001645
Chris Leech3fe9a0b2009-08-25 13:59:46 -07001646 if (unlikely(fcoe->ctlr.flogi_oxid != FC_XID_UNKNOWN))
1647 memcpy(eh->h_source, fcoe->ctlr.ctl_src_addr, ETH_ALEN);
Robert Love85b4aa42008-12-09 15:10:24 -08001648 else
Chris Leech11b56182009-11-03 11:46:29 -08001649 memcpy(eh->h_source, port->data_src_addr, ETH_ALEN);
Robert Love85b4aa42008-12-09 15:10:24 -08001650
1651 hp = (struct fcoe_hdr *)(eh + 1);
1652 memset(hp, 0, sizeof(*hp));
1653 if (FC_FCOE_VER)
1654 FC_FCOE_ENCAPS_VER(hp, FC_FCOE_VER);
1655 hp->fcoe_sof = sof;
1656
Yi Zou39ca9a02009-02-27 14:07:15 -08001657 /* fcoe lso, mss is in max_payload which is non-zero for FCP data */
Robert Love1875f272009-11-03 11:47:50 -08001658 if (lport->seq_offload && fr_max_payload(fp)) {
Yi Zou39ca9a02009-02-27 14:07:15 -08001659 skb_shinfo(skb)->gso_type = SKB_GSO_FCOE;
1660 skb_shinfo(skb)->gso_size = fr_max_payload(fp);
1661 } else {
1662 skb_shinfo(skb)->gso_type = 0;
1663 skb_shinfo(skb)->gso_size = 0;
1664 }
Robert Love85b4aa42008-12-09 15:10:24 -08001665 /* update tx stats: regardless if LLD fails */
Joe Eykholtf018b732010-03-12 16:08:55 -08001666 stats = per_cpu_ptr(lport->dev_stats, get_cpu());
Robert Love582b45b2009-03-31 15:51:50 -07001667 stats->TxFrames++;
1668 stats->TxWords += wlen;
Joe Eykholtf018b732010-03-12 16:08:55 -08001669 put_cpu();
Robert Love85b4aa42008-12-09 15:10:24 -08001670
1671 /* send down to lld */
Robert Love1875f272009-11-03 11:47:50 -08001672 fr_dev(fp) = lport;
Vasu Dev980f5152011-07-27 15:11:05 -07001673 fcoe_port_send(port, skb);
Robert Love85b4aa42008-12-09 15:10:24 -08001674 return 0;
1675}
Robert Love85b4aa42008-12-09 15:10:24 -08001676
Robert Love34f42a02009-02-27 10:55:45 -08001677/**
Robert Love1875f272009-11-03 11:47:50 -08001678 * fcoe_percpu_flush_done() - Indicate per-CPU queue flush completion
1679 * @skb: The completed skb (argument required by destructor)
Joe Eykholte7a51992009-08-25 14:04:08 -07001680 */
1681static void fcoe_percpu_flush_done(struct sk_buff *skb)
1682{
1683 complete(&fcoe_flush_completion);
1684}
1685
1686/**
Vasu Dev52ee8322011-01-28 16:03:52 -08001687 * fcoe_filter_frames() - filter out bad fcoe frames, i.e. bad CRC
1688 * @lport: The local port the frame was received on
1689 * @fp: The received frame
1690 *
1691 * Return: 0 on passing filtering checks
1692 */
1693static inline int fcoe_filter_frames(struct fc_lport *lport,
1694 struct fc_frame *fp)
1695{
1696 struct fcoe_interface *fcoe;
1697 struct fc_frame_header *fh;
1698 struct sk_buff *skb = (struct sk_buff *)fp;
1699 struct fcoe_dev_stats *stats;
1700
1701 /*
1702 * We only check CRC if no offload is available and if it is
1703 * it's solicited data, in which case, the FCP layer would
1704 * check it during the copy.
1705 */
1706 if (lport->crc_offload && skb->ip_summed == CHECKSUM_UNNECESSARY)
1707 fr_flags(fp) &= ~FCPHF_CRC_UNCHECKED;
1708 else
1709 fr_flags(fp) |= FCPHF_CRC_UNCHECKED;
1710
1711 fh = (struct fc_frame_header *) skb_transport_header(skb);
1712 fh = fc_frame_header_get(fp);
1713 if (fh->fh_r_ctl == FC_RCTL_DD_SOL_DATA && fh->fh_type == FC_TYPE_FCP)
1714 return 0;
1715
Bhanu Prakash Gollapudi8597ae82011-01-28 16:05:37 -08001716 fcoe = ((struct fcoe_port *)lport_priv(lport))->priv;
Vasu Dev52ee8322011-01-28 16:03:52 -08001717 if (is_fip_mode(&fcoe->ctlr) && fc_frame_payload_op(fp) == ELS_LOGO &&
1718 ntoh24(fh->fh_s_id) == FC_FID_FLOGI) {
1719 FCOE_DBG("fcoe: dropping FCoE lport LOGO in fip mode\n");
1720 return -EINVAL;
1721 }
1722
Dan Carpenterf2f96d22011-02-25 15:03:23 -08001723 if (!(fr_flags(fp) & FCPHF_CRC_UNCHECKED) ||
Vasu Dev52ee8322011-01-28 16:03:52 -08001724 le32_to_cpu(fr_crc(fp)) == ~crc32(~0, skb->data, skb->len)) {
1725 fr_flags(fp) &= ~FCPHF_CRC_UNCHECKED;
1726 return 0;
1727 }
1728
1729 stats = per_cpu_ptr(lport->dev_stats, get_cpu());
1730 stats->InvalidCRCCount++;
1731 if (stats->InvalidCRCCount < 5)
1732 printk(KERN_WARNING "fcoe: dropping frame with CRC error\n");
Thomas Gleixner7e1e7ea2011-11-11 20:52:01 +01001733 put_cpu();
Vasu Dev52ee8322011-01-28 16:03:52 -08001734 return -EINVAL;
1735}
1736
1737/**
Chris Leech859b7b62009-11-20 14:54:47 -08001738 * fcoe_recv_frame() - process a single received frame
1739 * @skb: frame to process
1740 */
1741static void fcoe_recv_frame(struct sk_buff *skb)
1742{
1743 u32 fr_len;
1744 struct fc_lport *lport;
1745 struct fcoe_rcv_info *fr;
1746 struct fcoe_dev_stats *stats;
Chris Leech859b7b62009-11-20 14:54:47 -08001747 struct fcoe_crc_eof crc_eof;
1748 struct fc_frame *fp;
Chris Leech859b7b62009-11-20 14:54:47 -08001749 struct fcoe_port *port;
1750 struct fcoe_hdr *hp;
1751
1752 fr = fcoe_dev_from_skb(skb);
1753 lport = fr->fr_dev;
1754 if (unlikely(!lport)) {
1755 if (skb->destructor != fcoe_percpu_flush_done)
1756 FCOE_NETDEV_DBG(skb->dev, "NULL lport in skb");
1757 kfree_skb(skb);
1758 return;
1759 }
1760
1761 FCOE_NETDEV_DBG(skb->dev, "skb_info: len:%d data_len:%d "
1762 "head:%p data:%p tail:%p end:%p sum:%d dev:%s",
1763 skb->len, skb->data_len,
1764 skb->head, skb->data, skb_tail_pointer(skb),
1765 skb_end_pointer(skb), skb->csum,
1766 skb->dev ? skb->dev->name : "<NULL>");
1767
Chris Leech859b7b62009-11-20 14:54:47 -08001768 port = lport_priv(lport);
Robert Lovef1633012011-12-16 14:24:49 -08001769 skb_linearize(skb); /* check for skb_is_nonlinear is within skb_linearize */
Chris Leech859b7b62009-11-20 14:54:47 -08001770
1771 /*
1772 * Frame length checks and setting up the header pointers
1773 * was done in fcoe_rcv already.
1774 */
1775 hp = (struct fcoe_hdr *) skb_network_header(skb);
Chris Leech859b7b62009-11-20 14:54:47 -08001776
Joe Eykholtf018b732010-03-12 16:08:55 -08001777 stats = per_cpu_ptr(lport->dev_stats, get_cpu());
Chris Leech859b7b62009-11-20 14:54:47 -08001778 if (unlikely(FC_FCOE_DECAPS_VER(hp) != FC_FCOE_VER)) {
1779 if (stats->ErrorFrames < 5)
1780 printk(KERN_WARNING "fcoe: FCoE version "
1781 "mismatch: The frame has "
1782 "version %x, but the "
1783 "initiator supports version "
1784 "%x\n", FC_FCOE_DECAPS_VER(hp),
1785 FC_FCOE_VER);
Joe Eykholtf018b732010-03-12 16:08:55 -08001786 goto drop;
Chris Leech859b7b62009-11-20 14:54:47 -08001787 }
1788
1789 skb_pull(skb, sizeof(struct fcoe_hdr));
1790 fr_len = skb->len - sizeof(struct fcoe_crc_eof);
1791
1792 stats->RxFrames++;
1793 stats->RxWords += fr_len / FCOE_WORD_TO_BYTE;
1794
1795 fp = (struct fc_frame *)skb;
1796 fc_frame_init(fp);
1797 fr_dev(fp) = lport;
1798 fr_sof(fp) = hp->fcoe_sof;
1799
1800 /* Copy out the CRC and EOF trailer for access */
Joe Eykholtf018b732010-03-12 16:08:55 -08001801 if (skb_copy_bits(skb, fr_len, &crc_eof, sizeof(crc_eof)))
1802 goto drop;
Chris Leech859b7b62009-11-20 14:54:47 -08001803 fr_eof(fp) = crc_eof.fcoe_eof;
1804 fr_crc(fp) = crc_eof.fcoe_crc32;
Joe Eykholtf018b732010-03-12 16:08:55 -08001805 if (pskb_trim(skb, fr_len))
1806 goto drop;
Chris Leech859b7b62009-11-20 14:54:47 -08001807
Vasu Dev52ee8322011-01-28 16:03:52 -08001808 if (!fcoe_filter_frames(lport, fp)) {
1809 put_cpu();
1810 fc_exch_recv(lport, fp);
1811 return;
Chris Leech859b7b62009-11-20 14:54:47 -08001812 }
Joe Eykholtf018b732010-03-12 16:08:55 -08001813drop:
1814 stats->ErrorFrames++;
1815 put_cpu();
1816 kfree_skb(skb);
Chris Leech859b7b62009-11-20 14:54:47 -08001817}
1818
1819/**
Robert Love1875f272009-11-03 11:47:50 -08001820 * fcoe_percpu_receive_thread() - The per-CPU packet receive thread
1821 * @arg: The per-CPU context
Robert Love85b4aa42008-12-09 15:10:24 -08001822 *
1823 * Return: 0 for success
Robert Love85b4aa42008-12-09 15:10:24 -08001824 */
Bart Van Assche7c9c6842012-01-13 17:26:25 -08001825static int fcoe_percpu_receive_thread(void *arg)
Robert Love85b4aa42008-12-09 15:10:24 -08001826{
1827 struct fcoe_percpu_s *p = arg;
Robert Love85b4aa42008-12-09 15:10:24 -08001828 struct sk_buff *skb;
Robert Love85b4aa42008-12-09 15:10:24 -08001829
Robert Love4469c192009-02-27 10:56:38 -08001830 set_user_nice(current, -20);
Robert Love85b4aa42008-12-09 15:10:24 -08001831
1832 while (!kthread_should_stop()) {
1833
1834 spin_lock_bh(&p->fcoe_rx_list.lock);
1835 while ((skb = __skb_dequeue(&p->fcoe_rx_list)) == NULL) {
1836 set_current_state(TASK_INTERRUPTIBLE);
1837 spin_unlock_bh(&p->fcoe_rx_list.lock);
1838 schedule();
1839 set_current_state(TASK_RUNNING);
1840 if (kthread_should_stop())
1841 return 0;
1842 spin_lock_bh(&p->fcoe_rx_list.lock);
1843 }
1844 spin_unlock_bh(&p->fcoe_rx_list.lock);
Chris Leech859b7b62009-11-20 14:54:47 -08001845 fcoe_recv_frame(skb);
Robert Love85b4aa42008-12-09 15:10:24 -08001846 }
1847 return 0;
1848}
1849
1850/**
Robert Love1875f272009-11-03 11:47:50 -08001851 * fcoe_dev_setup() - Setup the link change notification interface
Robert Love34f42a02009-02-27 10:55:45 -08001852 */
Randy Dunlapb0d428a2009-04-27 21:49:31 -07001853static void fcoe_dev_setup(void)
Robert Love85b4aa42008-12-09 15:10:24 -08001854{
john fastabend6f6c2aa2011-11-18 13:35:56 -08001855 register_dcbevent_notifier(&dcb_notifier);
Robert Love85b4aa42008-12-09 15:10:24 -08001856 register_netdevice_notifier(&fcoe_notifier);
1857}
1858
1859/**
Robert Love1875f272009-11-03 11:47:50 -08001860 * fcoe_dev_cleanup() - Cleanup the link change notification interface
Robert Love34f42a02009-02-27 10:55:45 -08001861 */
Robert Love85b4aa42008-12-09 15:10:24 -08001862static void fcoe_dev_cleanup(void)
1863{
john fastabend6f6c2aa2011-11-18 13:35:56 -08001864 unregister_dcbevent_notifier(&dcb_notifier);
Robert Love85b4aa42008-12-09 15:10:24 -08001865 unregister_netdevice_notifier(&fcoe_notifier);
1866}
1867
john fastabend6f6c2aa2011-11-18 13:35:56 -08001868static struct fcoe_interface *
1869fcoe_hostlist_lookup_realdev_port(struct net_device *netdev)
1870{
1871 struct fcoe_interface *fcoe;
1872 struct net_device *real_dev;
1873
1874 list_for_each_entry(fcoe, &fcoe_hostlist, list) {
1875 if (fcoe->netdev->priv_flags & IFF_802_1Q_VLAN)
1876 real_dev = vlan_dev_real_dev(fcoe->netdev);
1877 else
1878 real_dev = fcoe->netdev;
1879
1880 if (netdev == real_dev)
1881 return fcoe;
1882 }
1883 return NULL;
1884}
1885
1886static int fcoe_dcb_app_notification(struct notifier_block *notifier,
1887 ulong event, void *ptr)
1888{
1889 struct dcb_app_type *entry = ptr;
1890 struct fcoe_interface *fcoe;
1891 struct net_device *netdev;
1892 struct fcoe_port *port;
1893 int prio;
1894
1895 if (entry->app.selector != DCB_APP_IDTYPE_ETHTYPE)
1896 return NOTIFY_OK;
1897
1898 netdev = dev_get_by_index(&init_net, entry->ifindex);
1899 if (!netdev)
1900 return NOTIFY_OK;
1901
1902 fcoe = fcoe_hostlist_lookup_realdev_port(netdev);
1903 dev_put(netdev);
1904 if (!fcoe)
1905 return NOTIFY_OK;
1906
1907 if (entry->dcbx & DCB_CAP_DCBX_VER_CEE)
1908 prio = ffs(entry->app.priority) - 1;
1909 else
1910 prio = entry->app.priority;
1911
1912 if (prio < 0)
1913 return NOTIFY_OK;
1914
1915 if (entry->app.protocol == ETH_P_FIP ||
1916 entry->app.protocol == ETH_P_FCOE)
1917 fcoe->ctlr.priority = prio;
1918
1919 if (entry->app.protocol == ETH_P_FCOE) {
1920 port = lport_priv(fcoe->ctlr.lp);
1921 port->priority = prio;
1922 }
1923
1924 return NOTIFY_OK;
1925}
1926
Robert Love85b4aa42008-12-09 15:10:24 -08001927/**
Robert Love1875f272009-11-03 11:47:50 -08001928 * fcoe_device_notification() - Handler for net device events
1929 * @notifier: The context of the notification
1930 * @event: The type of event
1931 * @ptr: The net device that the event was on
Robert Love85b4aa42008-12-09 15:10:24 -08001932 *
Robert Love1875f272009-11-03 11:47:50 -08001933 * This function is called by the Ethernet driver in case of link change event.
Robert Love85b4aa42008-12-09 15:10:24 -08001934 *
1935 * Returns: 0 for success
Robert Love34f42a02009-02-27 10:55:45 -08001936 */
Robert Love85b4aa42008-12-09 15:10:24 -08001937static int fcoe_device_notification(struct notifier_block *notifier,
1938 ulong event, void *ptr)
1939{
Robert Love1875f272009-11-03 11:47:50 -08001940 struct fc_lport *lport = NULL;
Vasu Dev1d1b88d2009-07-29 17:05:45 -07001941 struct net_device *netdev = ptr;
Chris Leech014f5c32009-08-25 13:59:30 -07001942 struct fcoe_interface *fcoe;
Chris Leech2e70e242009-08-25 14:00:23 -07001943 struct fcoe_port *port;
Robert Love85b4aa42008-12-09 15:10:24 -08001944 struct fcoe_dev_stats *stats;
Joe Eykholt97c83892009-03-17 11:42:40 -07001945 u32 link_possible = 1;
Robert Love85b4aa42008-12-09 15:10:24 -08001946 u32 mfs;
1947 int rc = NOTIFY_OK;
1948
Chris Leech014f5c32009-08-25 13:59:30 -07001949 list_for_each_entry(fcoe, &fcoe_hostlist, list) {
Chris Leech25024982009-08-25 13:59:35 -07001950 if (fcoe->netdev == netdev) {
Robert Love1875f272009-11-03 11:47:50 -08001951 lport = fcoe->ctlr.lp;
Robert Love85b4aa42008-12-09 15:10:24 -08001952 break;
1953 }
1954 }
Robert Love1875f272009-11-03 11:47:50 -08001955 if (!lport) {
Robert Love85b4aa42008-12-09 15:10:24 -08001956 rc = NOTIFY_DONE;
1957 goto out;
1958 }
1959
Robert Love85b4aa42008-12-09 15:10:24 -08001960 switch (event) {
1961 case NETDEV_DOWN:
1962 case NETDEV_GOING_DOWN:
Joe Eykholt97c83892009-03-17 11:42:40 -07001963 link_possible = 0;
Robert Love85b4aa42008-12-09 15:10:24 -08001964 break;
1965 case NETDEV_UP:
1966 case NETDEV_CHANGE:
Robert Love85b4aa42008-12-09 15:10:24 -08001967 break;
1968 case NETDEV_CHANGEMTU:
Yi Zou7221d7e2009-10-21 16:27:52 -07001969 if (netdev->features & NETIF_F_FCOE_MTU)
1970 break;
Vasu Dev1d1b88d2009-07-29 17:05:45 -07001971 mfs = netdev->mtu - (sizeof(struct fcoe_hdr) +
1972 sizeof(struct fcoe_crc_eof));
Robert Love85b4aa42008-12-09 15:10:24 -08001973 if (mfs >= FC_MIN_MAX_FRAME)
Robert Love1875f272009-11-03 11:47:50 -08001974 fc_set_mfs(lport, mfs);
Robert Love85b4aa42008-12-09 15:10:24 -08001975 break;
1976 case NETDEV_REGISTER:
1977 break;
Chris Leech2e70e242009-08-25 14:00:23 -07001978 case NETDEV_UNREGISTER:
1979 list_del(&fcoe->list);
1980 port = lport_priv(fcoe->ctlr.lp);
Tejun Heo2ca32b42011-01-28 16:05:32 -08001981 queue_work(fcoe_wq, &port->destroy_work);
Chris Leech2e70e242009-08-25 14:00:23 -07001982 goto out;
1983 break;
Yi Zou54a5b212010-07-20 15:21:17 -07001984 case NETDEV_FEAT_CHANGE:
1985 fcoe_netdev_features_change(lport, netdev);
1986 break;
Robert Love85b4aa42008-12-09 15:10:24 -08001987 default:
Vasu Dev1d1b88d2009-07-29 17:05:45 -07001988 FCOE_NETDEV_DBG(netdev, "Unknown event %ld "
Robert Loved5488eb2009-06-10 15:30:59 -07001989 "from netdev netlink\n", event);
Robert Love85b4aa42008-12-09 15:10:24 -08001990 }
Robert Love5e4f8fe2010-05-07 15:18:35 -07001991
1992 fcoe_link_speed_update(lport);
1993
Robert Love1875f272009-11-03 11:47:50 -08001994 if (link_possible && !fcoe_link_ok(lport))
Chris Leech3fe9a0b2009-08-25 13:59:46 -07001995 fcoe_ctlr_link_up(&fcoe->ctlr);
1996 else if (fcoe_ctlr_link_down(&fcoe->ctlr)) {
Joe Eykholtf018b732010-03-12 16:08:55 -08001997 stats = per_cpu_ptr(lport->dev_stats, get_cpu());
Joe Eykholt97c83892009-03-17 11:42:40 -07001998 stats->LinkFailureCount++;
Joe Eykholtf018b732010-03-12 16:08:55 -08001999 put_cpu();
Robert Love1875f272009-11-03 11:47:50 -08002000 fcoe_clean_pending_queue(lport);
Robert Love85b4aa42008-12-09 15:10:24 -08002001 }
2002out:
2003 return rc;
2004}
2005
2006/**
Vasu Dev55a66d32009-12-10 09:59:31 -08002007 * fcoe_disable() - Disables a FCoE interface
Yi Zou78a58242011-01-28 16:05:16 -08002008 * @netdev : The net_device object the Ethernet interface to create on
Vasu Dev55a66d32009-12-10 09:59:31 -08002009 *
Yi Zou78a58242011-01-28 16:05:16 -08002010 * Called from fcoe transport.
Vasu Dev55a66d32009-12-10 09:59:31 -08002011 *
2012 * Returns: 0 for success
2013 */
Yi Zou78a58242011-01-28 16:05:16 -08002014static int fcoe_disable(struct net_device *netdev)
Vasu Dev55a66d32009-12-10 09:59:31 -08002015{
2016 struct fcoe_interface *fcoe;
Vasu Dev55a66d32009-12-10 09:59:31 -08002017 int rc = 0;
2018
2019 mutex_lock(&fcoe_config_mutex);
Vasu Dev55a66d32009-12-10 09:59:31 -08002020
Robert Loveee5df622011-04-01 16:05:59 -07002021 rtnl_lock();
Vasu Dev55a66d32009-12-10 09:59:31 -08002022 fcoe = fcoe_hostlist_lookup_port(netdev);
2023 rtnl_unlock();
2024
Chris Leech9ee50e42010-04-09 14:22:23 -07002025 if (fcoe) {
Chris Leech9ee50e42010-04-09 14:22:23 -07002026 fcoe_ctlr_link_down(&fcoe->ctlr);
Vasu Dev9d4cbc02010-07-20 15:19:26 -07002027 fcoe_clean_pending_queue(fcoe->ctlr.lp);
Chris Leech9ee50e42010-04-09 14:22:23 -07002028 } else
Vasu Dev55a66d32009-12-10 09:59:31 -08002029 rc = -ENODEV;
2030
Vasu Dev55a66d32009-12-10 09:59:31 -08002031 mutex_unlock(&fcoe_config_mutex);
2032 return rc;
2033}
2034
2035/**
2036 * fcoe_enable() - Enables a FCoE interface
Yi Zou78a58242011-01-28 16:05:16 -08002037 * @netdev : The net_device object the Ethernet interface to create on
Vasu Dev55a66d32009-12-10 09:59:31 -08002038 *
Yi Zou78a58242011-01-28 16:05:16 -08002039 * Called from fcoe transport.
Vasu Dev55a66d32009-12-10 09:59:31 -08002040 *
2041 * Returns: 0 for success
2042 */
Yi Zou78a58242011-01-28 16:05:16 -08002043static int fcoe_enable(struct net_device *netdev)
Vasu Dev55a66d32009-12-10 09:59:31 -08002044{
2045 struct fcoe_interface *fcoe;
Vasu Dev55a66d32009-12-10 09:59:31 -08002046 int rc = 0;
2047
2048 mutex_lock(&fcoe_config_mutex);
Robert Loveee5df622011-04-01 16:05:59 -07002049 rtnl_lock();
Vasu Dev55a66d32009-12-10 09:59:31 -08002050 fcoe = fcoe_hostlist_lookup_port(netdev);
2051 rtnl_unlock();
2052
Vasu Dev9d4cbc02010-07-20 15:19:26 -07002053 if (!fcoe)
Vasu Dev55a66d32009-12-10 09:59:31 -08002054 rc = -ENODEV;
Vasu Dev9d4cbc02010-07-20 15:19:26 -07002055 else if (!fcoe_link_ok(fcoe->ctlr.lp))
2056 fcoe_ctlr_link_up(&fcoe->ctlr);
Vasu Dev55a66d32009-12-10 09:59:31 -08002057
Vasu Dev55a66d32009-12-10 09:59:31 -08002058 mutex_unlock(&fcoe_config_mutex);
2059 return rc;
2060}
2061
2062/**
Robert Love1875f272009-11-03 11:47:50 -08002063 * fcoe_destroy() - Destroy a FCoE interface
Yi Zou78a58242011-01-28 16:05:16 -08002064 * @netdev : The net_device object the Ethernet interface to create on
Robert Love1875f272009-11-03 11:47:50 -08002065 *
Yi Zou78a58242011-01-28 16:05:16 -08002066 * Called from fcoe transport
Robert Love85b4aa42008-12-09 15:10:24 -08002067 *
2068 * Returns: 0 for success
Robert Love34f42a02009-02-27 10:55:45 -08002069 */
Yi Zou78a58242011-01-28 16:05:16 -08002070static int fcoe_destroy(struct net_device *netdev)
Robert Love85b4aa42008-12-09 15:10:24 -08002071{
Chris Leech030f4e02009-08-25 14:00:02 -07002072 struct fcoe_interface *fcoe;
Vasu Devf04ca1b2011-04-01 16:06:45 -07002073 struct fc_lport *lport;
Neerav Parikhb2085a42011-06-20 16:59:51 -07002074 struct fcoe_port *port;
Mike Christie8eca355f2009-10-21 16:27:44 -07002075 int rc = 0;
Robert Love85b4aa42008-12-09 15:10:24 -08002076
Chris Leechdfc1d0f2009-08-25 14:00:13 -07002077 mutex_lock(&fcoe_config_mutex);
Robert Loveee5df622011-04-01 16:05:59 -07002078 rtnl_lock();
Chris Leech2e70e242009-08-25 14:00:23 -07002079 fcoe = fcoe_hostlist_lookup_port(netdev);
2080 if (!fcoe) {
Robert Love85b4aa42008-12-09 15:10:24 -08002081 rc = -ENODEV;
Yi Zou78a58242011-01-28 16:05:16 -08002082 goto out_nodev;
Robert Love85b4aa42008-12-09 15:10:24 -08002083 }
Vasu Devf04ca1b2011-04-01 16:06:45 -07002084 lport = fcoe->ctlr.lp;
Neerav Parikhb2085a42011-06-20 16:59:51 -07002085 port = lport_priv(lport);
Yi Zou54a5b212010-07-20 15:21:17 -07002086 list_del(&fcoe->list);
Neerav Parikhb2085a42011-06-20 16:59:51 -07002087 queue_work(fcoe_wq, &port->destroy_work);
Robert Love85b4aa42008-12-09 15:10:24 -08002088out_nodev:
Neerav Parikhb2085a42011-06-20 16:59:51 -07002089 rtnl_unlock();
Chris Leechdfc1d0f2009-08-25 14:00:13 -07002090 mutex_unlock(&fcoe_config_mutex);
Robert Love85b4aa42008-12-09 15:10:24 -08002091 return rc;
2092}
2093
Robert Love1875f272009-11-03 11:47:50 -08002094/**
2095 * fcoe_destroy_work() - Destroy a FCoE port in a deferred work context
2096 * @work: Handle to the FCoE port to be destroyed
2097 */
Chris Leech2e70e242009-08-25 14:00:23 -07002098static void fcoe_destroy_work(struct work_struct *work)
2099{
2100 struct fcoe_port *port;
Neerav Parikhb2085a42011-06-20 16:59:51 -07002101 struct fcoe_interface *fcoe;
2102 int npiv = 0;
Chris Leech2e70e242009-08-25 14:00:23 -07002103
2104 port = container_of(work, struct fcoe_port, destroy_work);
2105 mutex_lock(&fcoe_config_mutex);
Neerav Parikhb2085a42011-06-20 16:59:51 -07002106
2107 /* set if this is an NPIV port */
2108 npiv = port->lport->vport ? 1 : 0;
2109
2110 fcoe = port->priv;
Chris Leech2e70e242009-08-25 14:00:23 -07002111 fcoe_if_destroy(port->lport);
Neerav Parikhb2085a42011-06-20 16:59:51 -07002112
2113 /* Do not tear down the fcoe interface for NPIV port */
Robert Love848e7d52011-08-25 12:40:47 -07002114 if (!npiv)
Neerav Parikhb2085a42011-06-20 16:59:51 -07002115 fcoe_interface_cleanup(fcoe);
Neerav Parikhb2085a42011-06-20 16:59:51 -07002116
Chris Leech2e70e242009-08-25 14:00:23 -07002117 mutex_unlock(&fcoe_config_mutex);
2118}
2119
Robert Love85b4aa42008-12-09 15:10:24 -08002120/**
Yi Zou78a58242011-01-28 16:05:16 -08002121 * fcoe_match() - Check if the FCoE is supported on the given netdevice
2122 * @netdev : The net_device object the Ethernet interface to create on
Robert Love1875f272009-11-03 11:47:50 -08002123 *
Yi Zou78a58242011-01-28 16:05:16 -08002124 * Called from fcoe transport.
2125 *
2126 * Returns: always returns true as this is the default FCoE transport,
2127 * i.e., support all netdevs.
2128 */
2129static bool fcoe_match(struct net_device *netdev)
2130{
2131 return true;
2132}
2133
2134/**
john fastabend6f6c2aa2011-11-18 13:35:56 -08002135 * fcoe_dcb_create() - Initialize DCB attributes and hooks
2136 * @netdev: The net_device object of the L2 link that should be queried
2137 * @port: The fcoe_port to bind FCoE APP priority with
2138 * @
2139 */
2140static void fcoe_dcb_create(struct fcoe_interface *fcoe)
2141{
2142#ifdef CONFIG_DCB
2143 int dcbx;
2144 u8 fup, up;
2145 struct net_device *netdev = fcoe->realdev;
2146 struct fcoe_port *port = lport_priv(fcoe->ctlr.lp);
2147 struct dcb_app app = {
2148 .priority = 0,
2149 .protocol = ETH_P_FCOE
2150 };
2151
2152 /* setup DCB priority attributes. */
2153 if (netdev && netdev->dcbnl_ops && netdev->dcbnl_ops->getdcbx) {
2154 dcbx = netdev->dcbnl_ops->getdcbx(netdev);
2155
2156 if (dcbx & DCB_CAP_DCBX_VER_IEEE) {
2157 app.selector = IEEE_8021QAZ_APP_SEL_ETHERTYPE;
2158 up = dcb_ieee_getapp_mask(netdev, &app);
2159 app.protocol = ETH_P_FIP;
2160 fup = dcb_ieee_getapp_mask(netdev, &app);
2161 } else {
2162 app.selector = DCB_APP_IDTYPE_ETHTYPE;
2163 up = dcb_getapp(netdev, &app);
2164 app.protocol = ETH_P_FIP;
2165 fup = dcb_getapp(netdev, &app);
2166 }
2167
2168 port->priority = ffs(up) ? ffs(up) - 1 : 0;
2169 fcoe->ctlr.priority = ffs(fup) ? ffs(fup) - 1 : port->priority;
2170 }
2171#endif
2172}
2173
2174/**
Yi Zou78a58242011-01-28 16:05:16 -08002175 * fcoe_create() - Create a fcoe interface
2176 * @netdev : The net_device object the Ethernet interface to create on
2177 * @fip_mode: The FIP mode for this creation
2178 *
2179 * Called from fcoe transport
Robert Love85b4aa42008-12-09 15:10:24 -08002180 *
2181 * Returns: 0 for success
Robert Love34f42a02009-02-27 10:55:45 -08002182 */
Yi Zou78a58242011-01-28 16:05:16 -08002183static int fcoe_create(struct net_device *netdev, enum fip_state fip_mode)
Robert Love85b4aa42008-12-09 15:10:24 -08002184{
Neerav Parikhb2085a42011-06-20 16:59:51 -07002185 int rc = 0;
Chris Leech030f4e02009-08-25 14:00:02 -07002186 struct fcoe_interface *fcoe;
Chris Leechaf7f85d2009-08-25 13:59:24 -07002187 struct fc_lport *lport;
Robert Love85b4aa42008-12-09 15:10:24 -08002188
Chris Leechdfc1d0f2009-08-25 14:00:13 -07002189 mutex_lock(&fcoe_config_mutex);
Robert Loveee5df622011-04-01 16:05:59 -07002190 rtnl_lock();
Vasu Dev34ce27b2010-05-07 15:18:46 -07002191
Robert Love85b4aa42008-12-09 15:10:24 -08002192 /* look for existing lport */
2193 if (fcoe_hostlist_lookup(netdev)) {
2194 rc = -EEXIST;
Yi Zou78a58242011-01-28 16:05:16 -08002195 goto out_nodev;
Robert Love85b4aa42008-12-09 15:10:24 -08002196 }
Robert Love85b4aa42008-12-09 15:10:24 -08002197
Joe Eykholt1dd454d2010-07-20 15:20:46 -07002198 fcoe = fcoe_interface_create(netdev, fip_mode);
Robert Love7287fb92011-01-28 16:03:47 -08002199 if (IS_ERR(fcoe)) {
2200 rc = PTR_ERR(fcoe);
Yi Zou78a58242011-01-28 16:05:16 -08002201 goto out_nodev;
Chris Leech030f4e02009-08-25 14:00:02 -07002202 }
2203
Chris Leech9a057532009-11-03 11:46:40 -08002204 lport = fcoe_if_create(fcoe, &netdev->dev, 0);
Chris Leechaf7f85d2009-08-25 13:59:24 -07002205 if (IS_ERR(lport)) {
Robert Loved5488eb2009-06-10 15:30:59 -07002206 printk(KERN_ERR "fcoe: Failed to create interface (%s)\n",
Robert Love85b4aa42008-12-09 15:10:24 -08002207 netdev->name);
Robert Love85b4aa42008-12-09 15:10:24 -08002208 rc = -EIO;
Robert Love848e7d52011-08-25 12:40:47 -07002209 rtnl_unlock();
Chris Leech2e70e242009-08-25 14:00:23 -07002210 fcoe_interface_cleanup(fcoe);
Robert Love848e7d52011-08-25 12:40:47 -07002211 goto out_nortnl;
Robert Love85b4aa42008-12-09 15:10:24 -08002212 }
Chris Leech030f4e02009-08-25 14:00:02 -07002213
Chris Leech54b649f2009-08-25 14:00:07 -07002214 /* Make this the "master" N_Port */
2215 fcoe->ctlr.lp = lport;
Chris Leech030f4e02009-08-25 14:00:02 -07002216
john fastabend6f6c2aa2011-11-18 13:35:56 -08002217 /* setup DCB priority attributes. */
2218 fcoe_dcb_create(fcoe);
2219
Chris Leechc863df32009-08-25 14:00:18 -07002220 /* add to lports list */
2221 fcoe_hostlist_add(lport);
2222
Chris Leech54b649f2009-08-25 14:00:07 -07002223 /* start FIP Discovery and FLOGI */
2224 lport->boot_time = jiffies;
2225 fc_fabric_login(lport);
2226 if (!fcoe_link_ok(lport))
2227 fcoe_ctlr_link_up(&fcoe->ctlr);
2228
Robert Love85b4aa42008-12-09 15:10:24 -08002229out_nodev:
Vasu Dev34ce27b2010-05-07 15:18:46 -07002230 rtnl_unlock();
Robert Love848e7d52011-08-25 12:40:47 -07002231out_nortnl:
Chris Leechdfc1d0f2009-08-25 14:00:13 -07002232 mutex_unlock(&fcoe_config_mutex);
Robert Love85b4aa42008-12-09 15:10:24 -08002233 return rc;
2234}
2235
Robert Love34f42a02009-02-27 10:55:45 -08002236/**
Robert Love5e4f8fe2010-05-07 15:18:35 -07002237 * fcoe_link_speed_update() - Update the supported and actual link speeds
2238 * @lport: The local port to update speeds for
Robert Love85b4aa42008-12-09 15:10:24 -08002239 *
Robert Love5e4f8fe2010-05-07 15:18:35 -07002240 * Returns: 0 if the ethtool query was successful
2241 * -1 if the ethtool query failed
Robert Love85b4aa42008-12-09 15:10:24 -08002242 */
Bart Van Assche7c9c6842012-01-13 17:26:25 -08002243static int fcoe_link_speed_update(struct fc_lport *lport)
Robert Love85b4aa42008-12-09 15:10:24 -08002244{
Bhanu Prakash Gollapudi8597ae82011-01-28 16:05:37 -08002245 struct net_device *netdev = fcoe_netdev(lport);
David Decotigny8ae6daca2011-04-27 18:32:38 +00002246 struct ethtool_cmd ecmd;
Robert Love85b4aa42008-12-09 15:10:24 -08002247
Jiri Pirko4bc71cb2011-09-03 03:34:30 +00002248 if (!__ethtool_get_settings(netdev, &ecmd)) {
Robert Love1875f272009-11-03 11:47:50 -08002249 lport->link_supported_speeds &=
Yi Zou2f718d62009-07-29 17:04:01 -07002250 ~(FC_PORTSPEED_1GBIT | FC_PORTSPEED_10GBIT);
2251 if (ecmd.supported & (SUPPORTED_1000baseT_Half |
2252 SUPPORTED_1000baseT_Full))
Robert Love1875f272009-11-03 11:47:50 -08002253 lport->link_supported_speeds |= FC_PORTSPEED_1GBIT;
Yi Zou2f718d62009-07-29 17:04:01 -07002254 if (ecmd.supported & SUPPORTED_10000baseT_Full)
Robert Love1875f272009-11-03 11:47:50 -08002255 lport->link_supported_speeds |=
Yi Zou2f718d62009-07-29 17:04:01 -07002256 FC_PORTSPEED_10GBIT;
David Decotigny8ae6daca2011-04-27 18:32:38 +00002257 switch (ethtool_cmd_speed(&ecmd)) {
2258 case SPEED_1000:
Robert Love1875f272009-11-03 11:47:50 -08002259 lport->link_speed = FC_PORTSPEED_1GBIT;
David Decotigny8ae6daca2011-04-27 18:32:38 +00002260 break;
2261 case SPEED_10000:
Robert Love1875f272009-11-03 11:47:50 -08002262 lport->link_speed = FC_PORTSPEED_10GBIT;
David Decotigny8ae6daca2011-04-27 18:32:38 +00002263 break;
2264 }
Yi Zou2f718d62009-07-29 17:04:01 -07002265 return 0;
2266 }
2267 return -1;
Robert Love85b4aa42008-12-09 15:10:24 -08002268}
Robert Love85b4aa42008-12-09 15:10:24 -08002269
Robert Love34f42a02009-02-27 10:55:45 -08002270/**
Robert Love5e4f8fe2010-05-07 15:18:35 -07002271 * fcoe_link_ok() - Check if the link is OK for a local port
2272 * @lport: The local port to check link on
2273 *
2274 * Returns: 0 if link is UP and OK, -1 if not
2275 *
2276 */
Bart Van Assche7c9c6842012-01-13 17:26:25 -08002277static int fcoe_link_ok(struct fc_lport *lport)
Robert Love5e4f8fe2010-05-07 15:18:35 -07002278{
Bhanu Prakash Gollapudi8597ae82011-01-28 16:05:37 -08002279 struct net_device *netdev = fcoe_netdev(lport);
Robert Love5e4f8fe2010-05-07 15:18:35 -07002280
2281 if (netif_oper_up(netdev))
2282 return 0;
2283 return -1;
2284}
2285
2286/**
Robert Love1875f272009-11-03 11:47:50 -08002287 * fcoe_percpu_clean() - Clear all pending skbs for an local port
2288 * @lport: The local port whose skbs are to be cleared
Joe Eykholte7a51992009-08-25 14:04:08 -07002289 *
2290 * Must be called with fcoe_create_mutex held to single-thread completion.
2291 *
2292 * This flushes the pending skbs by adding a new skb to each queue and
2293 * waiting until they are all freed. This assures us that not only are
2294 * there no packets that will be handled by the lport, but also that any
2295 * threads already handling packet have returned.
Robert Love85b4aa42008-12-09 15:10:24 -08002296 */
Bart Van Assche7c9c6842012-01-13 17:26:25 -08002297static void fcoe_percpu_clean(struct fc_lport *lport)
Robert Love85b4aa42008-12-09 15:10:24 -08002298{
Robert Love85b4aa42008-12-09 15:10:24 -08002299 struct fcoe_percpu_s *pp;
2300 struct fcoe_rcv_info *fr;
2301 struct sk_buff_head *list;
2302 struct sk_buff *skb, *next;
2303 struct sk_buff *head;
Robert Love5e5e92d2009-03-17 11:41:35 -07002304 unsigned int cpu;
Robert Love85b4aa42008-12-09 15:10:24 -08002305
Robert Love5e5e92d2009-03-17 11:41:35 -07002306 for_each_possible_cpu(cpu) {
2307 pp = &per_cpu(fcoe_percpu, cpu);
2308 spin_lock_bh(&pp->fcoe_rx_list.lock);
2309 list = &pp->fcoe_rx_list;
2310 head = list->next;
2311 for (skb = head; skb != (struct sk_buff *)list;
2312 skb = next) {
2313 next = skb->next;
2314 fr = fcoe_dev_from_skb(skb);
Robert Love1875f272009-11-03 11:47:50 -08002315 if (fr->fr_dev == lport) {
Robert Love5e5e92d2009-03-17 11:41:35 -07002316 __skb_unlink(skb, list);
2317 kfree_skb(skb);
Robert Love85b4aa42008-12-09 15:10:24 -08002318 }
Robert Love85b4aa42008-12-09 15:10:24 -08002319 }
Joe Eykholte7a51992009-08-25 14:04:08 -07002320
2321 if (!pp->thread || !cpu_online(cpu)) {
2322 spin_unlock_bh(&pp->fcoe_rx_list.lock);
2323 continue;
2324 }
2325
2326 skb = dev_alloc_skb(0);
2327 if (!skb) {
2328 spin_unlock_bh(&pp->fcoe_rx_list.lock);
2329 continue;
2330 }
2331 skb->destructor = fcoe_percpu_flush_done;
2332
2333 __skb_queue_tail(&pp->fcoe_rx_list, skb);
2334 if (pp->fcoe_rx_list.qlen == 1)
2335 wake_up_process(pp->thread);
Robert Love5e5e92d2009-03-17 11:41:35 -07002336 spin_unlock_bh(&pp->fcoe_rx_list.lock);
Joe Eykholte7a51992009-08-25 14:04:08 -07002337
2338 wait_for_completion(&fcoe_flush_completion);
Robert Love85b4aa42008-12-09 15:10:24 -08002339 }
2340}
Robert Love85b4aa42008-12-09 15:10:24 -08002341
2342/**
Robert Love1875f272009-11-03 11:47:50 -08002343 * fcoe_reset() - Reset a local port
2344 * @shost: The SCSI host associated with the local port to be reset
Robert Love85b4aa42008-12-09 15:10:24 -08002345 *
Robert Love1875f272009-11-03 11:47:50 -08002346 * Returns: Always 0 (return value required by FC transport template)
Robert Love85b4aa42008-12-09 15:10:24 -08002347 */
Bart Van Assche7c9c6842012-01-13 17:26:25 -08002348static int fcoe_reset(struct Scsi_Host *shost)
Robert Love85b4aa42008-12-09 15:10:24 -08002349{
2350 struct fc_lport *lport = shost_priv(shost);
Vasu Devd2f80952011-02-25 15:03:28 -08002351 struct fcoe_port *port = lport_priv(lport);
2352 struct fcoe_interface *fcoe = port->priv;
2353
2354 fcoe_ctlr_link_down(&fcoe->ctlr);
2355 fcoe_clean_pending_queue(fcoe->ctlr.lp);
2356 if (!fcoe_link_ok(fcoe->ctlr.lp))
2357 fcoe_ctlr_link_up(&fcoe->ctlr);
Robert Love85b4aa42008-12-09 15:10:24 -08002358 return 0;
2359}
Robert Love85b4aa42008-12-09 15:10:24 -08002360
Robert Love34f42a02009-02-27 10:55:45 -08002361/**
Robert Love1875f272009-11-03 11:47:50 -08002362 * fcoe_hostlist_lookup_port() - Find the FCoE interface associated with a net device
2363 * @netdev: The net device used as a key
Robert Love85b4aa42008-12-09 15:10:24 -08002364 *
Robert Love1875f272009-11-03 11:47:50 -08002365 * Locking: Must be called with the RNL mutex held.
2366 *
2367 * Returns: NULL or the FCoE interface
Robert Love85b4aa42008-12-09 15:10:24 -08002368 */
Chris Leech014f5c32009-08-25 13:59:30 -07002369static struct fcoe_interface *
Robert Love1875f272009-11-03 11:47:50 -08002370fcoe_hostlist_lookup_port(const struct net_device *netdev)
Robert Love85b4aa42008-12-09 15:10:24 -08002371{
Chris Leech014f5c32009-08-25 13:59:30 -07002372 struct fcoe_interface *fcoe;
Robert Love85b4aa42008-12-09 15:10:24 -08002373
Chris Leech014f5c32009-08-25 13:59:30 -07002374 list_for_each_entry(fcoe, &fcoe_hostlist, list) {
Robert Love1875f272009-11-03 11:47:50 -08002375 if (fcoe->netdev == netdev)
Chris Leech014f5c32009-08-25 13:59:30 -07002376 return fcoe;
Robert Love85b4aa42008-12-09 15:10:24 -08002377 }
Robert Love85b4aa42008-12-09 15:10:24 -08002378 return NULL;
2379}
2380
Robert Love34f42a02009-02-27 10:55:45 -08002381/**
Robert Love1875f272009-11-03 11:47:50 -08002382 * fcoe_hostlist_lookup() - Find the local port associated with a
2383 * given net device
2384 * @netdev: The netdevice used as a key
Robert Love85b4aa42008-12-09 15:10:24 -08002385 *
Robert Love1875f272009-11-03 11:47:50 -08002386 * Locking: Must be called with the RTNL mutex held
2387 *
2388 * Returns: NULL or the local port
Robert Love85b4aa42008-12-09 15:10:24 -08002389 */
Chris Leech090eb6c2009-08-25 14:00:28 -07002390static struct fc_lport *fcoe_hostlist_lookup(const struct net_device *netdev)
Robert Love85b4aa42008-12-09 15:10:24 -08002391{
Chris Leech014f5c32009-08-25 13:59:30 -07002392 struct fcoe_interface *fcoe;
Robert Love85b4aa42008-12-09 15:10:24 -08002393
Chris Leech014f5c32009-08-25 13:59:30 -07002394 fcoe = fcoe_hostlist_lookup_port(netdev);
Chris Leech3fe9a0b2009-08-25 13:59:46 -07002395 return (fcoe) ? fcoe->ctlr.lp : NULL;
Robert Love85b4aa42008-12-09 15:10:24 -08002396}
Robert Love85b4aa42008-12-09 15:10:24 -08002397
Robert Love34f42a02009-02-27 10:55:45 -08002398/**
Robert Love1875f272009-11-03 11:47:50 -08002399 * fcoe_hostlist_add() - Add the FCoE interface identified by a local
2400 * port to the hostlist
2401 * @lport: The local port that identifies the FCoE interface to be added
2402 *
2403 * Locking: must be called with the RTNL mutex held
Robert Love85b4aa42008-12-09 15:10:24 -08002404 *
2405 * Returns: 0 for success
2406 */
Chris Leech090eb6c2009-08-25 14:00:28 -07002407static int fcoe_hostlist_add(const struct fc_lport *lport)
Robert Love85b4aa42008-12-09 15:10:24 -08002408{
Chris Leech014f5c32009-08-25 13:59:30 -07002409 struct fcoe_interface *fcoe;
2410 struct fcoe_port *port;
Robert Love85b4aa42008-12-09 15:10:24 -08002411
Chris Leech014f5c32009-08-25 13:59:30 -07002412 fcoe = fcoe_hostlist_lookup_port(fcoe_netdev(lport));
2413 if (!fcoe) {
2414 port = lport_priv(lport);
Bhanu Prakash Gollapudi8597ae82011-01-28 16:05:37 -08002415 fcoe = port->priv;
Chris Leech014f5c32009-08-25 13:59:30 -07002416 list_add_tail(&fcoe->list, &fcoe_hostlist);
Robert Love85b4aa42008-12-09 15:10:24 -08002417 }
2418 return 0;
2419}
Robert Love85b4aa42008-12-09 15:10:24 -08002420
Yi Zou78a58242011-01-28 16:05:16 -08002421
2422static struct fcoe_transport fcoe_sw_transport = {
2423 .name = {FCOE_TRANSPORT_DEFAULT},
2424 .attached = false,
2425 .list = LIST_HEAD_INIT(fcoe_sw_transport.list),
2426 .match = fcoe_match,
2427 .create = fcoe_create,
2428 .destroy = fcoe_destroy,
2429 .enable = fcoe_enable,
2430 .disable = fcoe_disable,
2431};
2432
Robert Love34f42a02009-02-27 10:55:45 -08002433/**
Robert Love1875f272009-11-03 11:47:50 -08002434 * fcoe_init() - Initialize fcoe.ko
Robert Love85b4aa42008-12-09 15:10:24 -08002435 *
Robert Love1875f272009-11-03 11:47:50 -08002436 * Returns: 0 on success, or a negative value on failure
Robert Love34f42a02009-02-27 10:55:45 -08002437 */
Robert Love85b4aa42008-12-09 15:10:24 -08002438static int __init fcoe_init(void)
2439{
Robert Love1875f272009-11-03 11:47:50 -08002440 struct fcoe_percpu_s *p;
Robert Love38eccab2009-03-17 11:41:30 -07002441 unsigned int cpu;
Robert Love8976f422009-03-17 11:41:46 -07002442 int rc = 0;
Robert Love85b4aa42008-12-09 15:10:24 -08002443
Tejun Heo2ca32b42011-01-28 16:05:32 -08002444 fcoe_wq = alloc_workqueue("fcoe", 0, 0);
2445 if (!fcoe_wq)
2446 return -ENOMEM;
2447
Yi Zou78a58242011-01-28 16:05:16 -08002448 /* register as a fcoe transport */
2449 rc = fcoe_transport_attach(&fcoe_sw_transport);
2450 if (rc) {
2451 printk(KERN_ERR "failed to register an fcoe transport, check "
2452 "if libfcoe is loaded\n");
2453 return rc;
2454 }
2455
Chris Leechdfc1d0f2009-08-25 14:00:13 -07002456 mutex_lock(&fcoe_config_mutex);
2457
Robert Love38eccab2009-03-17 11:41:30 -07002458 for_each_possible_cpu(cpu) {
Robert Love5e5e92d2009-03-17 11:41:35 -07002459 p = &per_cpu(fcoe_percpu, cpu);
Robert Love38eccab2009-03-17 11:41:30 -07002460 skb_queue_head_init(&p->fcoe_rx_list);
2461 }
2462
Robert Love8976f422009-03-17 11:41:46 -07002463 for_each_online_cpu(cpu)
2464 fcoe_percpu_thread_create(cpu);
Robert Love85b4aa42008-12-09 15:10:24 -08002465
Robert Love8976f422009-03-17 11:41:46 -07002466 /* Initialize per CPU interrupt thread */
2467 rc = register_hotcpu_notifier(&fcoe_cpu_notifier);
2468 if (rc)
2469 goto out_free;
Robert Love85b4aa42008-12-09 15:10:24 -08002470
Robert Love8976f422009-03-17 11:41:46 -07002471 /* Setup link change notification */
Robert Love85b4aa42008-12-09 15:10:24 -08002472 fcoe_dev_setup();
2473
Chris Leech5892c322009-08-25 13:59:14 -07002474 rc = fcoe_if_init();
2475 if (rc)
2476 goto out_free;
Robert Love85b4aa42008-12-09 15:10:24 -08002477
Chris Leechdfc1d0f2009-08-25 14:00:13 -07002478 mutex_unlock(&fcoe_config_mutex);
Robert Love85b4aa42008-12-09 15:10:24 -08002479 return 0;
Robert Love8976f422009-03-17 11:41:46 -07002480
2481out_free:
2482 for_each_online_cpu(cpu) {
2483 fcoe_percpu_thread_destroy(cpu);
2484 }
Chris Leechdfc1d0f2009-08-25 14:00:13 -07002485 mutex_unlock(&fcoe_config_mutex);
Tejun Heo2ca32b42011-01-28 16:05:32 -08002486 destroy_workqueue(fcoe_wq);
Robert Love8976f422009-03-17 11:41:46 -07002487 return rc;
Robert Love85b4aa42008-12-09 15:10:24 -08002488}
2489module_init(fcoe_init);
2490
2491/**
Robert Love1875f272009-11-03 11:47:50 -08002492 * fcoe_exit() - Clean up fcoe.ko
Robert Love85b4aa42008-12-09 15:10:24 -08002493 *
Robert Love1875f272009-11-03 11:47:50 -08002494 * Returns: 0 on success or a negative value on failure
Robert Love34f42a02009-02-27 10:55:45 -08002495 */
Robert Love85b4aa42008-12-09 15:10:24 -08002496static void __exit fcoe_exit(void)
2497{
Chris Leech014f5c32009-08-25 13:59:30 -07002498 struct fcoe_interface *fcoe, *tmp;
Chris Leech2e70e242009-08-25 14:00:23 -07002499 struct fcoe_port *port;
Robert Love1875f272009-11-03 11:47:50 -08002500 unsigned int cpu;
Robert Love85b4aa42008-12-09 15:10:24 -08002501
Chris Leechdfc1d0f2009-08-25 14:00:13 -07002502 mutex_lock(&fcoe_config_mutex);
2503
Robert Love85b4aa42008-12-09 15:10:24 -08002504 fcoe_dev_cleanup();
2505
Vasu Dev5919a592009-03-27 09:03:29 -07002506 /* releases the associated fcoe hosts */
Chris Leech090eb6c2009-08-25 14:00:28 -07002507 rtnl_lock();
2508 list_for_each_entry_safe(fcoe, tmp, &fcoe_hostlist, list) {
Chris Leechc863df32009-08-25 14:00:18 -07002509 list_del(&fcoe->list);
Chris Leech2e70e242009-08-25 14:00:23 -07002510 port = lport_priv(fcoe->ctlr.lp);
Tejun Heo2ca32b42011-01-28 16:05:32 -08002511 queue_work(fcoe_wq, &port->destroy_work);
Chris Leechc863df32009-08-25 14:00:18 -07002512 }
Chris Leech090eb6c2009-08-25 14:00:28 -07002513 rtnl_unlock();
Robert Love85b4aa42008-12-09 15:10:24 -08002514
Robert Love8976f422009-03-17 11:41:46 -07002515 unregister_hotcpu_notifier(&fcoe_cpu_notifier);
2516
Chris Leech014f5c32009-08-25 13:59:30 -07002517 for_each_online_cpu(cpu)
Robert Love8976f422009-03-17 11:41:46 -07002518 fcoe_percpu_thread_destroy(cpu);
Robert Love85b4aa42008-12-09 15:10:24 -08002519
Chris Leechdfc1d0f2009-08-25 14:00:13 -07002520 mutex_unlock(&fcoe_config_mutex);
Chris Leech2e70e242009-08-25 14:00:23 -07002521
Tejun Heo2ca32b42011-01-28 16:05:32 -08002522 /*
2523 * destroy_work's may be chained but destroy_workqueue()
2524 * can take care of them. Just kill the fcoe_wq.
2525 */
2526 destroy_workqueue(fcoe_wq);
Chris Leech2e70e242009-08-25 14:00:23 -07002527
Tejun Heo2ca32b42011-01-28 16:05:32 -08002528 /*
2529 * Detaching from the scsi transport must happen after all
2530 * destroys are done on the fcoe_wq. destroy_workqueue will
2531 * enusre the fcoe_wq is flushed.
2532 */
Chris Leech2e70e242009-08-25 14:00:23 -07002533 fcoe_if_exit();
Yi Zou78a58242011-01-28 16:05:16 -08002534
2535 /* detach from fcoe transport */
2536 fcoe_transport_detach(&fcoe_sw_transport);
Robert Love85b4aa42008-12-09 15:10:24 -08002537}
2538module_exit(fcoe_exit);
Chris Leech11b56182009-11-03 11:46:29 -08002539
2540/**
2541 * fcoe_flogi_resp() - FCoE specific FLOGI and FDISC response handler
2542 * @seq: active sequence in the FLOGI or FDISC exchange
2543 * @fp: response frame, or error encoded in a pointer (timeout)
2544 * @arg: pointer the the fcoe_ctlr structure
2545 *
Uwe Kleine-König65155b32010-06-11 12:17:01 +02002546 * This handles MAC address management for FCoE, then passes control on to
Chris Leech11b56182009-11-03 11:46:29 -08002547 * the libfc FLOGI response handler.
2548 */
2549static void fcoe_flogi_resp(struct fc_seq *seq, struct fc_frame *fp, void *arg)
2550{
2551 struct fcoe_ctlr *fip = arg;
2552 struct fc_exch *exch = fc_seq_exch(seq);
2553 struct fc_lport *lport = exch->lp;
2554 u8 *mac;
2555
2556 if (IS_ERR(fp))
2557 goto done;
2558
2559 mac = fr_cb(fp)->granted_mac;
Vasu Dev907c07d2011-10-28 11:34:23 -07002560 /* pre-FIP */
2561 if (is_zero_ether_addr(mac))
2562 fcoe_ctlr_recv_flogi(fip, lport, fp);
2563 if (!is_zero_ether_addr(mac))
2564 fcoe_update_src_mac(lport, mac);
Chris Leech11b56182009-11-03 11:46:29 -08002565done:
2566 fc_lport_flogi_resp(seq, fp, lport);
2567}
2568
2569/**
2570 * fcoe_logo_resp() - FCoE specific LOGO response handler
2571 * @seq: active sequence in the LOGO exchange
2572 * @fp: response frame, or error encoded in a pointer (timeout)
2573 * @arg: pointer the the fcoe_ctlr structure
2574 *
Uwe Kleine-König65155b32010-06-11 12:17:01 +02002575 * This handles MAC address management for FCoE, then passes control on to
Chris Leech11b56182009-11-03 11:46:29 -08002576 * the libfc LOGO response handler.
2577 */
2578static void fcoe_logo_resp(struct fc_seq *seq, struct fc_frame *fp, void *arg)
2579{
Joe Eykholt386309ce2009-11-03 11:49:16 -08002580 struct fc_lport *lport = arg;
Chris Leech11b56182009-11-03 11:46:29 -08002581 static u8 zero_mac[ETH_ALEN] = { 0 };
2582
2583 if (!IS_ERR(fp))
Joe Eykholt386309ce2009-11-03 11:49:16 -08002584 fcoe_update_src_mac(lport, zero_mac);
Chris Leech11b56182009-11-03 11:46:29 -08002585 fc_lport_logo_resp(seq, fp, lport);
2586}
2587
2588/**
2589 * fcoe_elsct_send - FCoE specific ELS handler
2590 *
2591 * This does special case handling of FIP encapsualted ELS exchanges for FCoE,
2592 * using FCoE specific response handlers and passing the FIP controller as
2593 * the argument (the lport is still available from the exchange).
2594 *
2595 * Most of the work here is just handed off to the libfc routine.
2596 */
Robert Love1875f272009-11-03 11:47:50 -08002597static struct fc_seq *fcoe_elsct_send(struct fc_lport *lport, u32 did,
2598 struct fc_frame *fp, unsigned int op,
2599 void (*resp)(struct fc_seq *,
2600 struct fc_frame *,
2601 void *),
2602 void *arg, u32 timeout)
Chris Leech11b56182009-11-03 11:46:29 -08002603{
2604 struct fcoe_port *port = lport_priv(lport);
Bhanu Prakash Gollapudi8597ae82011-01-28 16:05:37 -08002605 struct fcoe_interface *fcoe = port->priv;
Chris Leech11b56182009-11-03 11:46:29 -08002606 struct fcoe_ctlr *fip = &fcoe->ctlr;
2607 struct fc_frame_header *fh = fc_frame_header_get(fp);
2608
2609 switch (op) {
2610 case ELS_FLOGI:
2611 case ELS_FDISC:
Joe Eykholte10f8c62010-07-20 15:20:30 -07002612 if (lport->point_to_multipoint)
2613 break;
Chris Leech11b56182009-11-03 11:46:29 -08002614 return fc_elsct_send(lport, did, fp, op, fcoe_flogi_resp,
2615 fip, timeout);
2616 case ELS_LOGO:
2617 /* only hook onto fabric logouts, not port logouts */
2618 if (ntoh24(fh->fh_d_id) != FC_FID_FLOGI)
2619 break;
2620 return fc_elsct_send(lport, did, fp, op, fcoe_logo_resp,
Joe Eykholt386309ce2009-11-03 11:49:16 -08002621 lport, timeout);
Chris Leech11b56182009-11-03 11:46:29 -08002622 }
2623 return fc_elsct_send(lport, did, fp, op, resp, arg, timeout);
2624}
2625
Chris Leech9a057532009-11-03 11:46:40 -08002626/**
2627 * fcoe_vport_create() - create an fc_host/scsi_host for a vport
2628 * @vport: fc_vport object to create a new fc_host for
2629 * @disabled: start the new fc_host in a disabled state by default?
2630 *
2631 * Returns: 0 for success
2632 */
2633static int fcoe_vport_create(struct fc_vport *vport, bool disabled)
2634{
2635 struct Scsi_Host *shost = vport_to_shost(vport);
2636 struct fc_lport *n_port = shost_priv(shost);
2637 struct fcoe_port *port = lport_priv(n_port);
Bhanu Prakash Gollapudi8597ae82011-01-28 16:05:37 -08002638 struct fcoe_interface *fcoe = port->priv;
Chris Leech9a057532009-11-03 11:46:40 -08002639 struct net_device *netdev = fcoe->netdev;
2640 struct fc_lport *vn_port;
Neerav Parikhbdf25212011-05-16 16:45:29 -07002641 int rc;
2642 char buf[32];
2643
2644 rc = fcoe_validate_vport_create(vport);
2645 if (rc) {
Bhanu Prakash Gollapudid8348952011-08-04 17:38:49 -07002646 fcoe_wwn_to_str(vport->port_name, buf, sizeof(buf));
Neerav Parikhbdf25212011-05-16 16:45:29 -07002647 printk(KERN_ERR "fcoe: Failed to create vport, "
2648 "WWPN (0x%s) already exists\n",
2649 buf);
2650 return rc;
2651 }
Chris Leech9a057532009-11-03 11:46:40 -08002652
2653 mutex_lock(&fcoe_config_mutex);
Jiri Pirko4bc71cb2011-09-03 03:34:30 +00002654 rtnl_lock();
Chris Leech9a057532009-11-03 11:46:40 -08002655 vn_port = fcoe_if_create(fcoe, &vport->dev, 1);
Jiri Pirko4bc71cb2011-09-03 03:34:30 +00002656 rtnl_unlock();
Chris Leech9a057532009-11-03 11:46:40 -08002657 mutex_unlock(&fcoe_config_mutex);
2658
2659 if (IS_ERR(vn_port)) {
2660 printk(KERN_ERR "fcoe: fcoe_vport_create(%s) failed\n",
2661 netdev->name);
2662 return -EIO;
2663 }
2664
2665 if (disabled) {
2666 fc_vport_set_state(vport, FC_VPORT_DISABLED);
2667 } else {
2668 vn_port->boot_time = jiffies;
2669 fc_fabric_login(vn_port);
2670 fc_vport_setlink(vn_port);
2671 }
2672 return 0;
2673}
2674
2675/**
2676 * fcoe_vport_destroy() - destroy the fc_host/scsi_host for a vport
2677 * @vport: fc_vport object that is being destroyed
2678 *
2679 * Returns: 0 for success
2680 */
2681static int fcoe_vport_destroy(struct fc_vport *vport)
2682{
2683 struct Scsi_Host *shost = vport_to_shost(vport);
2684 struct fc_lport *n_port = shost_priv(shost);
2685 struct fc_lport *vn_port = vport->dd_data;
2686 struct fcoe_port *port = lport_priv(vn_port);
2687
2688 mutex_lock(&n_port->lp_mutex);
2689 list_del(&vn_port->list);
2690 mutex_unlock(&n_port->lp_mutex);
Tejun Heo2ca32b42011-01-28 16:05:32 -08002691 queue_work(fcoe_wq, &port->destroy_work);
Chris Leech9a057532009-11-03 11:46:40 -08002692 return 0;
2693}
2694
2695/**
2696 * fcoe_vport_disable() - change vport state
2697 * @vport: vport to bring online/offline
2698 * @disable: should the vport be disabled?
2699 */
2700static int fcoe_vport_disable(struct fc_vport *vport, bool disable)
2701{
2702 struct fc_lport *lport = vport->dd_data;
2703
2704 if (disable) {
2705 fc_vport_set_state(vport, FC_VPORT_DISABLED);
2706 fc_fabric_logoff(lport);
2707 } else {
2708 lport->boot_time = jiffies;
2709 fc_fabric_login(lport);
2710 fc_vport_setlink(lport);
2711 }
2712
2713 return 0;
2714}
2715
Chris Leechdc8596d2009-11-03 11:47:18 -08002716/**
2717 * fcoe_vport_set_symbolic_name() - append vport string to symbolic name
2718 * @vport: fc_vport with a new symbolic name string
2719 *
2720 * After generating a new symbolic name string, a new RSPN_ID request is
2721 * sent to the name server. There is no response handler, so if it fails
2722 * for some reason it will not be retried.
2723 */
2724static void fcoe_set_vport_symbolic_name(struct fc_vport *vport)
2725{
2726 struct fc_lport *lport = vport->dd_data;
2727 struct fc_frame *fp;
2728 size_t len;
2729
2730 snprintf(fc_host_symbolic_name(lport->host), FC_SYMBOLIC_NAME_SIZE,
2731 "%s v%s over %s : %s", FCOE_NAME, FCOE_VERSION,
2732 fcoe_netdev(lport)->name, vport->symbolic_name);
2733
2734 if (lport->state != LPORT_ST_READY)
2735 return;
2736
2737 len = strnlen(fc_host_symbolic_name(lport->host), 255);
2738 fp = fc_frame_alloc(lport,
2739 sizeof(struct fc_ct_hdr) +
2740 sizeof(struct fc_ns_rspn) + len);
2741 if (!fp)
2742 return;
2743 lport->tt.elsct_send(lport, FC_FID_DIR_SERV, fp, FC_NS_RSPN_ID,
Joe Eykholtb94f8952009-11-03 11:50:21 -08002744 NULL, NULL, 3 * lport->r_a_tov);
Chris Leechdc8596d2009-11-03 11:47:18 -08002745}
Yi Zoub84056b2009-11-20 14:55:19 -08002746
2747/**
2748 * fcoe_get_lesb() - Fill the FCoE Link Error Status Block
2749 * @lport: the local port
2750 * @fc_lesb: the link error status block
2751 */
2752static void fcoe_get_lesb(struct fc_lport *lport,
2753 struct fc_els_lesb *fc_lesb)
2754{
Yi Zoub84056b2009-11-20 14:55:19 -08002755 struct net_device *netdev = fcoe_netdev(lport);
2756
Bhanu Prakash Gollapudi814740d2011-10-03 16:45:01 -07002757 __fcoe_get_lesb(lport, fc_lesb, netdev);
Yi Zoub84056b2009-11-20 14:55:19 -08002758}
Joe Eykholt7d65b0d2010-03-12 16:08:02 -08002759
2760/**
2761 * fcoe_set_port_id() - Callback from libfc when Port_ID is set.
2762 * @lport: the local port
2763 * @port_id: the port ID
2764 * @fp: the received frame, if any, that caused the port_id to be set.
2765 *
2766 * This routine handles the case where we received a FLOGI and are
2767 * entering point-to-point mode. We need to call fcoe_ctlr_recv_flogi()
2768 * so it can set the non-mapped mode and gateway address.
2769 *
2770 * The FLOGI LS_ACC is handled by fcoe_flogi_resp().
2771 */
2772static void fcoe_set_port_id(struct fc_lport *lport,
2773 u32 port_id, struct fc_frame *fp)
2774{
2775 struct fcoe_port *port = lport_priv(lport);
Bhanu Prakash Gollapudi8597ae82011-01-28 16:05:37 -08002776 struct fcoe_interface *fcoe = port->priv;
Joe Eykholt7d65b0d2010-03-12 16:08:02 -08002777
2778 if (fp && fc_frame_payload_op(fp) == ELS_FLOGI)
2779 fcoe_ctlr_recv_flogi(&fcoe->ctlr, lport, fp);
2780}