blob: b222e2dd5def5ecfb47af468d7bb094d28843573 [file] [log] [blame]
Robert Love85b4aa42008-12-09 15:10:24 -08001/*
Chris Leechaf7f85d2009-08-25 13:59:24 -07002 * Copyright(c) 2007 - 2009 Intel Corporation. All rights reserved.
Robert Love85b4aa42008-12-09 15:10:24 -08003 *
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms and conditions of the GNU General Public License,
6 * version 2, as published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope it will be useful, but WITHOUT
9 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
10 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
11 * more details.
12 *
13 * You should have received a copy of the GNU General Public License along with
14 * this program; if not, write to the Free Software Foundation, Inc.,
15 * 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
16 *
17 * Maintained at www.Open-FCoE.org
18 */
19
20#include <linux/module.h>
Robert Love85b4aa42008-12-09 15:10:24 -080021#include <linux/spinlock.h>
Robert Love85b4aa42008-12-09 15:10:24 -080022#include <linux/netdevice.h>
23#include <linux/etherdevice.h>
24#include <linux/ethtool.h>
25#include <linux/if_ether.h>
26#include <linux/if_vlan.h>
Robert Love85b4aa42008-12-09 15:10:24 -080027#include <linux/crc32.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090028#include <linux/slab.h>
Robert Love85b4aa42008-12-09 15:10:24 -080029#include <linux/cpu.h>
30#include <linux/fs.h>
31#include <linux/sysfs.h>
32#include <linux/ctype.h>
Tejun Heo2ca32b42011-01-28 16:05:32 -080033#include <linux/workqueue.h>
john fastabend6f6c2aa2011-11-18 13:35:56 -080034#include <net/dcbnl.h>
35#include <net/dcbevent.h>
Robert Love85b4aa42008-12-09 15:10:24 -080036#include <scsi/scsi_tcq.h>
37#include <scsi/scsicam.h>
38#include <scsi/scsi_transport.h>
39#include <scsi/scsi_transport_fc.h>
40#include <net/rtnetlink.h>
41
42#include <scsi/fc/fc_encaps.h>
Joe Eykholt97c83892009-03-17 11:42:40 -070043#include <scsi/fc/fc_fip.h>
Robert Love85b4aa42008-12-09 15:10:24 -080044
45#include <scsi/libfc.h>
46#include <scsi/fc_frame.h>
47#include <scsi/libfcoe.h>
Robert Love85b4aa42008-12-09 15:10:24 -080048
Vasu Devfdd78022009-03-17 11:42:24 -070049#include "fcoe.h"
50
Robert Love85b4aa42008-12-09 15:10:24 -080051MODULE_AUTHOR("Open-FCoE.org");
52MODULE_DESCRIPTION("FCoE");
Vasu Dev9b34ecf2009-03-17 11:42:13 -070053MODULE_LICENSE("GPL v2");
Robert Love85b4aa42008-12-09 15:10:24 -080054
Yi Zou05cc7392009-08-25 13:59:03 -070055/* Performance tuning parameters for fcoe */
Vasu Dev860eca22011-09-27 21:38:18 -070056static unsigned int fcoe_ddp_min = 4096;
Yi Zou05cc7392009-08-25 13:59:03 -070057module_param_named(ddp_min, fcoe_ddp_min, uint, S_IRUGO | S_IWUSR);
58MODULE_PARM_DESC(ddp_min, "Minimum I/O size in bytes for " \
59 "Direct Data Placement (DDP).");
60
Bart Van Assche7c9c6842012-01-13 17:26:25 -080061unsigned int fcoe_debug_logging;
62module_param_named(debug_logging, fcoe_debug_logging, int, S_IRUGO|S_IWUSR);
63MODULE_PARM_DESC(debug_logging, "a bit mask of logging levels");
64
65static DEFINE_MUTEX(fcoe_config_mutex);
Chris Leechdfc1d0f2009-08-25 14:00:13 -070066
Tejun Heo2ca32b42011-01-28 16:05:32 -080067static struct workqueue_struct *fcoe_wq;
68
Joe Eykholte7a51992009-08-25 14:04:08 -070069/* fcoe_percpu_clean completion. Waiter protected by fcoe_create_mutex */
70static DECLARE_COMPLETION(fcoe_flush_completion);
71
Robert Love85b4aa42008-12-09 15:10:24 -080072/* fcoe host list */
Chris Leech090eb6c2009-08-25 14:00:28 -070073/* must only by accessed under the RTNL mutex */
Bart Van Assche7c9c6842012-01-13 17:26:25 -080074static LIST_HEAD(fcoe_hostlist);
75static DEFINE_PER_CPU(struct fcoe_percpu_s, fcoe_percpu);
Robert Love85b4aa42008-12-09 15:10:24 -080076
Chris Leechdd3fd722009-04-21 16:27:36 -070077/* Function Prototypes */
Robert Love1875f272009-11-03 11:47:50 -080078static int fcoe_reset(struct Scsi_Host *);
Vasu Devfdd78022009-03-17 11:42:24 -070079static int fcoe_xmit(struct fc_lport *, struct fc_frame *);
80static int fcoe_rcv(struct sk_buff *, struct net_device *,
81 struct packet_type *, struct net_device *);
Robert Love1875f272009-11-03 11:47:50 -080082static int fcoe_percpu_receive_thread(void *);
Robert Love1875f272009-11-03 11:47:50 -080083static void fcoe_percpu_clean(struct fc_lport *);
Robert Love5e4f8fe2010-05-07 15:18:35 -070084static int fcoe_link_speed_update(struct fc_lport *);
Robert Love1875f272009-11-03 11:47:50 -080085static int fcoe_link_ok(struct fc_lport *);
Vasu Devfdd78022009-03-17 11:42:24 -070086
87static struct fc_lport *fcoe_hostlist_lookup(const struct net_device *);
88static int fcoe_hostlist_add(const struct fc_lport *);
Vasu Devfdd78022009-03-17 11:42:24 -070089
Robert Love85b4aa42008-12-09 15:10:24 -080090static int fcoe_device_notification(struct notifier_block *, ulong, void *);
91static void fcoe_dev_setup(void);
92static void fcoe_dev_cleanup(void);
Robert Love1875f272009-11-03 11:47:50 -080093static struct fcoe_interface
94*fcoe_hostlist_lookup_port(const struct net_device *);
Robert Love85b4aa42008-12-09 15:10:24 -080095
Robert Love1875f272009-11-03 11:47:50 -080096static int fcoe_fip_recv(struct sk_buff *, struct net_device *,
97 struct packet_type *, struct net_device *);
98
99static void fcoe_fip_send(struct fcoe_ctlr *, struct sk_buff *);
100static void fcoe_update_src_mac(struct fc_lport *, u8 *);
101static u8 *fcoe_get_src_mac(struct fc_lport *);
102static void fcoe_destroy_work(struct work_struct *);
103
104static int fcoe_ddp_setup(struct fc_lport *, u16, struct scatterlist *,
105 unsigned int);
106static int fcoe_ddp_done(struct fc_lport *, u16);
Yi Zou71f89492011-06-20 16:59:10 -0700107static int fcoe_ddp_target(struct fc_lport *, u16, struct scatterlist *,
108 unsigned int);
Robert Love1875f272009-11-03 11:47:50 -0800109static int fcoe_cpu_callback(struct notifier_block *, unsigned long, void *);
john fastabend6f6c2aa2011-11-18 13:35:56 -0800110static int fcoe_dcb_app_notification(struct notifier_block *notifier,
111 ulong event, void *ptr);
Robert Love1875f272009-11-03 11:47:50 -0800112
Yi Zou78a58242011-01-28 16:05:16 -0800113static bool fcoe_match(struct net_device *netdev);
114static int fcoe_create(struct net_device *netdev, enum fip_state fip_mode);
115static int fcoe_destroy(struct net_device *netdev);
116static int fcoe_enable(struct net_device *netdev);
117static int fcoe_disable(struct net_device *netdev);
Robert Love1875f272009-11-03 11:47:50 -0800118
Robert Love1875f272009-11-03 11:47:50 -0800119static struct fc_seq *fcoe_elsct_send(struct fc_lport *,
120 u32 did, struct fc_frame *,
121 unsigned int op,
122 void (*resp)(struct fc_seq *,
123 struct fc_frame *,
124 void *),
125 void *, u32 timeout);
Chris Leech859b7b62009-11-20 14:54:47 -0800126static void fcoe_recv_frame(struct sk_buff *skb);
Robert Love1875f272009-11-03 11:47:50 -0800127
Yi Zoub84056b2009-11-20 14:55:19 -0800128static void fcoe_get_lesb(struct fc_lport *, struct fc_els_lesb *);
129
Robert Love1875f272009-11-03 11:47:50 -0800130/* notification function for packets from net device */
Robert Love85b4aa42008-12-09 15:10:24 -0800131static struct notifier_block fcoe_notifier = {
132 .notifier_call = fcoe_device_notification,
133};
134
Robert Love1875f272009-11-03 11:47:50 -0800135/* notification function for CPU hotplug events */
136static struct notifier_block fcoe_cpu_notifier = {
137 .notifier_call = fcoe_cpu_callback,
138};
139
john fastabend6f6c2aa2011-11-18 13:35:56 -0800140/* notification function for DCB events */
141static struct notifier_block dcb_notifier = {
142 .notifier_call = fcoe_dcb_app_notification,
143};
144
Yi Zou8ca86f82011-01-28 16:05:11 -0800145static struct scsi_transport_template *fcoe_nport_scsi_transport;
146static struct scsi_transport_template *fcoe_vport_scsi_transport;
Vasu Dev7f349142009-03-27 09:06:31 -0700147
Robert Love1875f272009-11-03 11:47:50 -0800148static int fcoe_vport_destroy(struct fc_vport *);
149static int fcoe_vport_create(struct fc_vport *, bool disabled);
150static int fcoe_vport_disable(struct fc_vport *, bool disable);
151static void fcoe_set_vport_symbolic_name(struct fc_vport *);
Joe Eykholt7d65b0d2010-03-12 16:08:02 -0800152static void fcoe_set_port_id(struct fc_lport *, u32, struct fc_frame *);
Robert Love1875f272009-11-03 11:47:50 -0800153
154static struct libfc_function_template fcoe_libfc_fcn_templ = {
155 .frame_send = fcoe_xmit,
156 .ddp_setup = fcoe_ddp_setup,
157 .ddp_done = fcoe_ddp_done,
Yi Zou71f89492011-06-20 16:59:10 -0700158 .ddp_target = fcoe_ddp_target,
Robert Love1875f272009-11-03 11:47:50 -0800159 .elsct_send = fcoe_elsct_send,
Yi Zoub84056b2009-11-20 14:55:19 -0800160 .get_lesb = fcoe_get_lesb,
Joe Eykholt7d65b0d2010-03-12 16:08:02 -0800161 .lport_set_port_id = fcoe_set_port_id,
Robert Love1875f272009-11-03 11:47:50 -0800162};
Chris Leech9a057532009-11-03 11:46:40 -0800163
Bart Van Assche7c9c6842012-01-13 17:26:25 -0800164static struct fc_function_template fcoe_nport_fc_functions = {
Vasu Dev7f349142009-03-27 09:06:31 -0700165 .show_host_node_name = 1,
166 .show_host_port_name = 1,
167 .show_host_supported_classes = 1,
168 .show_host_supported_fc4s = 1,
169 .show_host_active_fc4s = 1,
170 .show_host_maxframe_size = 1,
171
172 .show_host_port_id = 1,
173 .show_host_supported_speeds = 1,
174 .get_host_speed = fc_get_host_speed,
175 .show_host_speed = 1,
176 .show_host_port_type = 1,
177 .get_host_port_state = fc_get_host_port_state,
178 .show_host_port_state = 1,
179 .show_host_symbolic_name = 1,
180
181 .dd_fcrport_size = sizeof(struct fc_rport_libfc_priv),
182 .show_rport_maxframe_size = 1,
183 .show_rport_supported_classes = 1,
184
185 .show_host_fabric_name = 1,
186 .show_starget_node_name = 1,
187 .show_starget_port_name = 1,
188 .show_starget_port_id = 1,
189 .set_rport_dev_loss_tmo = fc_set_rport_loss_tmo,
190 .show_rport_dev_loss_tmo = 1,
191 .get_fc_host_stats = fc_get_host_stats,
192 .issue_fc_host_lip = fcoe_reset,
193
194 .terminate_rport_io = fc_rport_terminate_io,
Chris Leech9a057532009-11-03 11:46:40 -0800195
196 .vport_create = fcoe_vport_create,
197 .vport_delete = fcoe_vport_destroy,
198 .vport_disable = fcoe_vport_disable,
Chris Leechdc8596d2009-11-03 11:47:18 -0800199 .set_vport_symbolic_name = fcoe_set_vport_symbolic_name,
Steve Maa51ab392009-11-03 11:47:34 -0800200
201 .bsg_request = fc_lport_bsg_request,
Vasu Dev7f349142009-03-27 09:06:31 -0700202};
203
Bart Van Assche7c9c6842012-01-13 17:26:25 -0800204static struct fc_function_template fcoe_vport_fc_functions = {
Chris Leeche9084bb2009-11-03 11:46:34 -0800205 .show_host_node_name = 1,
206 .show_host_port_name = 1,
207 .show_host_supported_classes = 1,
208 .show_host_supported_fc4s = 1,
209 .show_host_active_fc4s = 1,
210 .show_host_maxframe_size = 1,
211
212 .show_host_port_id = 1,
213 .show_host_supported_speeds = 1,
214 .get_host_speed = fc_get_host_speed,
215 .show_host_speed = 1,
216 .show_host_port_type = 1,
217 .get_host_port_state = fc_get_host_port_state,
218 .show_host_port_state = 1,
219 .show_host_symbolic_name = 1,
220
221 .dd_fcrport_size = sizeof(struct fc_rport_libfc_priv),
222 .show_rport_maxframe_size = 1,
223 .show_rport_supported_classes = 1,
224
225 .show_host_fabric_name = 1,
226 .show_starget_node_name = 1,
227 .show_starget_port_name = 1,
228 .show_starget_port_id = 1,
229 .set_rport_dev_loss_tmo = fc_set_rport_loss_tmo,
230 .show_rport_dev_loss_tmo = 1,
231 .get_fc_host_stats = fc_get_host_stats,
232 .issue_fc_host_lip = fcoe_reset,
233
234 .terminate_rport_io = fc_rport_terminate_io,
Steve Maa51ab392009-11-03 11:47:34 -0800235
236 .bsg_request = fc_lport_bsg_request,
Chris Leeche9084bb2009-11-03 11:46:34 -0800237};
238
Vasu Dev7f349142009-03-27 09:06:31 -0700239static struct scsi_host_template fcoe_shost_template = {
240 .module = THIS_MODULE,
241 .name = "FCoE Driver",
242 .proc_name = FCOE_NAME,
243 .queuecommand = fc_queuecommand,
244 .eh_abort_handler = fc_eh_abort,
245 .eh_device_reset_handler = fc_eh_device_reset,
246 .eh_host_reset_handler = fc_eh_host_reset,
247 .slave_alloc = fc_slave_alloc,
248 .change_queue_depth = fc_change_queue_depth,
249 .change_queue_type = fc_change_queue_type,
250 .this_id = -1,
Vasu Dev14caf442009-10-15 17:46:55 -0700251 .cmd_per_lun = 3,
Vasu Dev7f349142009-03-27 09:06:31 -0700252 .can_queue = FCOE_MAX_OUTSTANDING_COMMANDS,
253 .use_clustering = ENABLE_CLUSTERING,
254 .sg_tablesize = SG_ALL,
255 .max_sectors = 0xffff,
256};
257
Chris Leech54b649f2009-08-25 14:00:07 -0700258/**
Robert Love1875f272009-11-03 11:47:50 -0800259 * fcoe_interface_setup() - Setup a FCoE interface
260 * @fcoe: The new FCoE interface
261 * @netdev: The net device that the fcoe interface is on
Chris Leech54b649f2009-08-25 14:00:07 -0700262 *
263 * Returns : 0 for success
Chris Leech2e70e242009-08-25 14:00:23 -0700264 * Locking: must be called with the RTNL mutex held
Chris Leech54b649f2009-08-25 14:00:07 -0700265 */
266static int fcoe_interface_setup(struct fcoe_interface *fcoe,
267 struct net_device *netdev)
268{
269 struct fcoe_ctlr *fip = &fcoe->ctlr;
270 struct netdev_hw_addr *ha;
Yi Zou5bab87e2009-11-03 11:49:43 -0800271 struct net_device *real_dev;
Chris Leech54b649f2009-08-25 14:00:07 -0700272 u8 flogi_maddr[ETH_ALEN];
Yi Zoub7a727f2009-10-21 16:28:03 -0700273 const struct net_device_ops *ops;
Chris Leech54b649f2009-08-25 14:00:07 -0700274
275 fcoe->netdev = netdev;
276
Yi Zoub7a727f2009-10-21 16:28:03 -0700277 /* Let LLD initialize for FCoE */
278 ops = netdev->netdev_ops;
279 if (ops->ndo_fcoe_enable) {
280 if (ops->ndo_fcoe_enable(netdev))
281 FCOE_NETDEV_DBG(netdev, "Failed to enable FCoE"
282 " specific feature for LLD.\n");
283 }
284
Chris Leech54b649f2009-08-25 14:00:07 -0700285 /* Do not support for bonding device */
Jiri Pirkocc8bdf02011-03-01 20:05:35 +0000286 if (netdev->priv_flags & IFF_BONDING && netdev->flags & IFF_MASTER) {
john fastabend59d92512009-11-03 11:48:44 -0800287 FCOE_NETDEV_DBG(netdev, "Bonded interfaces not supported\n");
Chris Leech54b649f2009-08-25 14:00:07 -0700288 return -EOPNOTSUPP;
289 }
290
291 /* look for SAN MAC address, if multiple SAN MACs exist, only
292 * use the first one for SPMA */
Yi Zou5bab87e2009-11-03 11:49:43 -0800293 real_dev = (netdev->priv_flags & IFF_802_1Q_VLAN) ?
294 vlan_dev_real_dev(netdev) : netdev;
Vasu Devd1483bb2011-09-27 21:38:13 -0700295 fcoe->realdev = real_dev;
Chris Leech54b649f2009-08-25 14:00:07 -0700296 rcu_read_lock();
Yi Zou5bab87e2009-11-03 11:49:43 -0800297 for_each_dev_addr(real_dev, ha) {
Chris Leech54b649f2009-08-25 14:00:07 -0700298 if ((ha->type == NETDEV_HW_ADDR_T_SAN) &&
Yi Zoubf361702009-11-03 11:49:38 -0800299 (is_valid_ether_addr(ha->addr))) {
Chris Leech54b649f2009-08-25 14:00:07 -0700300 memcpy(fip->ctl_src_addr, ha->addr, ETH_ALEN);
301 fip->spma = 1;
302 break;
303 }
304 }
305 rcu_read_unlock();
306
307 /* setup Source Mac Address */
308 if (!fip->spma)
309 memcpy(fip->ctl_src_addr, netdev->dev_addr, netdev->addr_len);
310
311 /*
312 * Add FCoE MAC address as second unicast MAC address
313 * or enter promiscuous mode if not capable of listening
314 * for multiple unicast MACs.
315 */
Chris Leech54b649f2009-08-25 14:00:07 -0700316 memcpy(flogi_maddr, (u8[6]) FC_FCOE_FLOGI_MAC, ETH_ALEN);
Jiri Pirkoa748ee22010-04-01 21:22:09 +0000317 dev_uc_add(netdev, flogi_maddr);
Chris Leech54b649f2009-08-25 14:00:07 -0700318 if (fip->spma)
Jiri Pirkoa748ee22010-04-01 21:22:09 +0000319 dev_uc_add(netdev, fip->ctl_src_addr);
Joe Eykholte10f8c62010-07-20 15:20:30 -0700320 if (fip->mode == FIP_MODE_VN2VN) {
321 dev_mc_add(netdev, FIP_ALL_VN2VN_MACS);
322 dev_mc_add(netdev, FIP_ALL_P2P_MACS);
323 } else
324 dev_mc_add(netdev, FIP_ALL_ENODE_MACS);
Chris Leech54b649f2009-08-25 14:00:07 -0700325
326 /*
327 * setup the receive function from ethernet driver
328 * on the ethertype for the given device
329 */
330 fcoe->fcoe_packet_type.func = fcoe_rcv;
331 fcoe->fcoe_packet_type.type = __constant_htons(ETH_P_FCOE);
332 fcoe->fcoe_packet_type.dev = netdev;
333 dev_add_pack(&fcoe->fcoe_packet_type);
334
335 fcoe->fip_packet_type.func = fcoe_fip_recv;
336 fcoe->fip_packet_type.type = htons(ETH_P_FIP);
337 fcoe->fip_packet_type.dev = netdev;
338 dev_add_pack(&fcoe->fip_packet_type);
339
340 return 0;
341}
342
Vasu Dev7f349142009-03-27 09:06:31 -0700343/**
Robert Love1875f272009-11-03 11:47:50 -0800344 * fcoe_interface_create() - Create a FCoE interface on a net device
345 * @netdev: The net device to create the FCoE interface on
Joe Eykholt1dd454d2010-07-20 15:20:46 -0700346 * @fip_mode: The mode to use for FIP
Chris Leech030f4e02009-08-25 14:00:02 -0700347 *
348 * Returns: pointer to a struct fcoe_interface or NULL on error
349 */
Joe Eykholt1dd454d2010-07-20 15:20:46 -0700350static struct fcoe_interface *fcoe_interface_create(struct net_device *netdev,
351 enum fip_state fip_mode)
Chris Leech030f4e02009-08-25 14:00:02 -0700352{
353 struct fcoe_interface *fcoe;
john fastabend59d92512009-11-03 11:48:44 -0800354 int err;
Chris Leech030f4e02009-08-25 14:00:02 -0700355
Robert Love7287fb92011-01-28 16:03:47 -0800356 if (!try_module_get(THIS_MODULE)) {
357 FCOE_NETDEV_DBG(netdev,
358 "Could not get a reference to the module\n");
359 fcoe = ERR_PTR(-EBUSY);
360 goto out;
361 }
362
Chris Leech030f4e02009-08-25 14:00:02 -0700363 fcoe = kzalloc(sizeof(*fcoe), GFP_KERNEL);
364 if (!fcoe) {
365 FCOE_NETDEV_DBG(netdev, "Could not allocate fcoe structure\n");
Robert Love7287fb92011-01-28 16:03:47 -0800366 fcoe = ERR_PTR(-ENOMEM);
367 goto out_nomod;
Chris Leech030f4e02009-08-25 14:00:02 -0700368 }
369
Chris Leech2e70e242009-08-25 14:00:23 -0700370 dev_hold(netdev);
Chris Leech030f4e02009-08-25 14:00:02 -0700371 kref_init(&fcoe->kref);
Chris Leech54b649f2009-08-25 14:00:07 -0700372
373 /*
374 * Initialize FIP.
375 */
Joe Eykholt1dd454d2010-07-20 15:20:46 -0700376 fcoe_ctlr_init(&fcoe->ctlr, fip_mode);
Chris Leech54b649f2009-08-25 14:00:07 -0700377 fcoe->ctlr.send = fcoe_fip_send;
378 fcoe->ctlr.update_mac = fcoe_update_src_mac;
Chris Leech11b56182009-11-03 11:46:29 -0800379 fcoe->ctlr.get_src_addr = fcoe_get_src_mac;
Chris Leech54b649f2009-08-25 14:00:07 -0700380
john fastabend59d92512009-11-03 11:48:44 -0800381 err = fcoe_interface_setup(fcoe, netdev);
382 if (err) {
383 fcoe_ctlr_destroy(&fcoe->ctlr);
384 kfree(fcoe);
385 dev_put(netdev);
Robert Love7287fb92011-01-28 16:03:47 -0800386 fcoe = ERR_PTR(err);
387 goto out_nomod;
john fastabend59d92512009-11-03 11:48:44 -0800388 }
Chris Leech030f4e02009-08-25 14:00:02 -0700389
Robert Love7287fb92011-01-28 16:03:47 -0800390 goto out;
391
392out_nomod:
393 module_put(THIS_MODULE);
394out:
Chris Leech030f4e02009-08-25 14:00:02 -0700395 return fcoe;
396}
397
398/**
399 * fcoe_interface_release() - fcoe_port kref release function
Robert Love1875f272009-11-03 11:47:50 -0800400 * @kref: Embedded reference count in an fcoe_interface struct
Chris Leech030f4e02009-08-25 14:00:02 -0700401 */
402static void fcoe_interface_release(struct kref *kref)
403{
404 struct fcoe_interface *fcoe;
Chris Leech2e70e242009-08-25 14:00:23 -0700405 struct net_device *netdev;
Chris Leech030f4e02009-08-25 14:00:02 -0700406
407 fcoe = container_of(kref, struct fcoe_interface, kref);
Chris Leech2e70e242009-08-25 14:00:23 -0700408 netdev = fcoe->netdev;
409 /* tear-down the FCoE controller */
410 fcoe_ctlr_destroy(&fcoe->ctlr);
Chris Leech030f4e02009-08-25 14:00:02 -0700411 kfree(fcoe);
Chris Leech2e70e242009-08-25 14:00:23 -0700412 dev_put(netdev);
Robert Love7287fb92011-01-28 16:03:47 -0800413 module_put(THIS_MODULE);
Chris Leech030f4e02009-08-25 14:00:02 -0700414}
415
416/**
Robert Love1875f272009-11-03 11:47:50 -0800417 * fcoe_interface_get() - Get a reference to a FCoE interface
418 * @fcoe: The FCoE interface to be held
Chris Leech030f4e02009-08-25 14:00:02 -0700419 */
420static inline void fcoe_interface_get(struct fcoe_interface *fcoe)
421{
422 kref_get(&fcoe->kref);
423}
424
425/**
Robert Love1875f272009-11-03 11:47:50 -0800426 * fcoe_interface_put() - Put a reference to a FCoE interface
427 * @fcoe: The FCoE interface to be released
Chris Leech030f4e02009-08-25 14:00:02 -0700428 */
429static inline void fcoe_interface_put(struct fcoe_interface *fcoe)
430{
431 kref_put(&fcoe->kref, fcoe_interface_release);
432}
433
434/**
Vasu Devf04ca1b2011-04-01 16:06:45 -0700435 * fcoe_interface_cleanup() - Clean up a FCoE interface
436 * @fcoe: The FCoE interface to be cleaned up
437 *
438 * Caller must be holding the RTNL mutex
439 */
Bart Van Assche7c9c6842012-01-13 17:26:25 -0800440static void fcoe_interface_cleanup(struct fcoe_interface *fcoe)
Vasu Devf04ca1b2011-04-01 16:06:45 -0700441{
442 struct net_device *netdev = fcoe->netdev;
443 struct fcoe_ctlr *fip = &fcoe->ctlr;
444 u8 flogi_maddr[ETH_ALEN];
445 const struct net_device_ops *ops;
Vasu Devf04ca1b2011-04-01 16:06:45 -0700446
Robert Love848e7d52011-08-25 12:40:47 -0700447 rtnl_lock();
448
Vasu Devf04ca1b2011-04-01 16:06:45 -0700449 /*
450 * Don't listen for Ethernet packets anymore.
451 * synchronize_net() ensures that the packet handlers are not running
452 * on another CPU. dev_remove_pack() would do that, this calls the
453 * unsyncronized version __dev_remove_pack() to avoid multiple delays.
454 */
455 __dev_remove_pack(&fcoe->fcoe_packet_type);
456 __dev_remove_pack(&fcoe->fip_packet_type);
457 synchronize_net();
458
459 /* Delete secondary MAC addresses */
460 memcpy(flogi_maddr, (u8[6]) FC_FCOE_FLOGI_MAC, ETH_ALEN);
461 dev_uc_del(netdev, flogi_maddr);
462 if (fip->spma)
463 dev_uc_del(netdev, fip->ctl_src_addr);
464 if (fip->mode == FIP_MODE_VN2VN) {
465 dev_mc_del(netdev, FIP_ALL_VN2VN_MACS);
466 dev_mc_del(netdev, FIP_ALL_P2P_MACS);
467 } else
468 dev_mc_del(netdev, FIP_ALL_ENODE_MACS);
469
Vasu Devf04ca1b2011-04-01 16:06:45 -0700470 /* Tell the LLD we are done w/ FCoE */
471 ops = netdev->netdev_ops;
472 if (ops->ndo_fcoe_disable) {
473 if (ops->ndo_fcoe_disable(netdev))
474 FCOE_NETDEV_DBG(netdev, "Failed to disable FCoE"
475 " specific feature for LLD.\n");
476 }
Neerav Parikhb2085a42011-06-20 16:59:51 -0700477
Robert Love848e7d52011-08-25 12:40:47 -0700478 rtnl_unlock();
479
Neerav Parikhb2085a42011-06-20 16:59:51 -0700480 /* Release the self-reference taken during fcoe_interface_create() */
Vasu Devf04ca1b2011-04-01 16:06:45 -0700481 fcoe_interface_put(fcoe);
482}
483
484/**
Robert Love1875f272009-11-03 11:47:50 -0800485 * fcoe_fip_recv() - Handler for received FIP frames
486 * @skb: The receive skb
487 * @netdev: The associated net device
488 * @ptype: The packet_type structure which was used to register this handler
489 * @orig_dev: The original net_device the the skb was received on.
490 * (in case dev is a bond)
Vasu Devab6b85c2009-05-17 12:33:08 +0000491 *
492 * Returns: 0 for success
493 */
Robert Love1875f272009-11-03 11:47:50 -0800494static int fcoe_fip_recv(struct sk_buff *skb, struct net_device *netdev,
Vasu Devab6b85c2009-05-17 12:33:08 +0000495 struct packet_type *ptype,
496 struct net_device *orig_dev)
497{
Chris Leech259ad852009-08-25 13:59:41 -0700498 struct fcoe_interface *fcoe;
Vasu Devab6b85c2009-05-17 12:33:08 +0000499
Chris Leech259ad852009-08-25 13:59:41 -0700500 fcoe = container_of(ptype, struct fcoe_interface, fip_packet_type);
Chris Leech3fe9a0b2009-08-25 13:59:46 -0700501 fcoe_ctlr_recv(&fcoe->ctlr, skb);
Vasu Devab6b85c2009-05-17 12:33:08 +0000502 return 0;
503}
504
505/**
Vasu Dev980f5152011-07-27 15:11:05 -0700506 * fcoe_port_send() - Send an Ethernet-encapsulated FIP/FCoE frame
507 * @port: The FCoE port
508 * @skb: The FIP/FCoE packet to be sent
509 */
510static void fcoe_port_send(struct fcoe_port *port, struct sk_buff *skb)
511{
512 if (port->fcoe_pending_queue.qlen)
513 fcoe_check_wait_queue(port->lport, skb);
514 else if (fcoe_start_io(skb))
515 fcoe_check_wait_queue(port->lport, skb);
516}
517
518/**
Robert Love1875f272009-11-03 11:47:50 -0800519 * fcoe_fip_send() - Send an Ethernet-encapsulated FIP frame
520 * @fip: The FCoE controller
521 * @skb: The FIP packet to be sent
Vasu Devab6b85c2009-05-17 12:33:08 +0000522 */
523static void fcoe_fip_send(struct fcoe_ctlr *fip, struct sk_buff *skb)
524{
Chris Leech3fe9a0b2009-08-25 13:59:46 -0700525 skb->dev = fcoe_from_ctlr(fip)->netdev;
Vasu Dev980f5152011-07-27 15:11:05 -0700526 fcoe_port_send(lport_priv(fip->lp), skb);
Vasu Devab6b85c2009-05-17 12:33:08 +0000527}
528
529/**
Robert Love1875f272009-11-03 11:47:50 -0800530 * fcoe_update_src_mac() - Update the Ethernet MAC filters
531 * @lport: The local port to update the source MAC on
532 * @addr: Unicast MAC address to add
Vasu Devab6b85c2009-05-17 12:33:08 +0000533 *
534 * Remove any previously-set unicast MAC filter.
535 * Add secondary FCoE MAC address filter for our OUI.
536 */
Chris Leech11b56182009-11-03 11:46:29 -0800537static void fcoe_update_src_mac(struct fc_lport *lport, u8 *addr)
Vasu Devab6b85c2009-05-17 12:33:08 +0000538{
Chris Leech11b56182009-11-03 11:46:29 -0800539 struct fcoe_port *port = lport_priv(lport);
Bhanu Prakash Gollapudi8597ae82011-01-28 16:05:37 -0800540 struct fcoe_interface *fcoe = port->priv;
Vasu Devab6b85c2009-05-17 12:33:08 +0000541
Vasu Devab6b85c2009-05-17 12:33:08 +0000542 rtnl_lock();
Chris Leech11b56182009-11-03 11:46:29 -0800543 if (!is_zero_ether_addr(port->data_src_addr))
Jiri Pirkoa748ee22010-04-01 21:22:09 +0000544 dev_uc_del(fcoe->netdev, port->data_src_addr);
Chris Leech11b56182009-11-03 11:46:29 -0800545 if (!is_zero_ether_addr(addr))
Jiri Pirkoa748ee22010-04-01 21:22:09 +0000546 dev_uc_add(fcoe->netdev, addr);
Chris Leech11b56182009-11-03 11:46:29 -0800547 memcpy(port->data_src_addr, addr, ETH_ALEN);
Vasu Devab6b85c2009-05-17 12:33:08 +0000548 rtnl_unlock();
549}
550
551/**
Chris Leech11b56182009-11-03 11:46:29 -0800552 * fcoe_get_src_mac() - return the Ethernet source address for an lport
553 * @lport: libfc lport
554 */
555static u8 *fcoe_get_src_mac(struct fc_lport *lport)
556{
557 struct fcoe_port *port = lport_priv(lport);
558
559 return port->data_src_addr;
560}
561
562/**
Robert Love1875f272009-11-03 11:47:50 -0800563 * fcoe_lport_config() - Set up a local port
564 * @lport: The local port to be setup
Vasu Dev7f349142009-03-27 09:06:31 -0700565 *
566 * Returns: 0 for success
567 */
Robert Love1875f272009-11-03 11:47:50 -0800568static int fcoe_lport_config(struct fc_lport *lport)
Vasu Dev7f349142009-03-27 09:06:31 -0700569{
Robert Love1875f272009-11-03 11:47:50 -0800570 lport->link_up = 0;
571 lport->qfull = 0;
572 lport->max_retry_count = 3;
573 lport->max_rport_retry_count = 3;
574 lport->e_d_tov = 2 * 1000; /* FC-FS default */
575 lport->r_a_tov = 2 * 2 * 1000;
576 lport->service_params = (FCP_SPPF_INIT_FCN | FCP_SPPF_RD_XRDY_DIS |
577 FCP_SPPF_RETRY | FCP_SPPF_CONF_COMPL);
578 lport->does_npiv = 1;
Vasu Dev7f349142009-03-27 09:06:31 -0700579
Robert Love1875f272009-11-03 11:47:50 -0800580 fc_lport_init_stats(lport);
Vasu Dev7f349142009-03-27 09:06:31 -0700581
582 /* lport fc_lport related configuration */
Robert Love1875f272009-11-03 11:47:50 -0800583 fc_lport_config(lport);
Vasu Dev7f349142009-03-27 09:06:31 -0700584
585 /* offload related configuration */
Robert Love1875f272009-11-03 11:47:50 -0800586 lport->crc_offload = 0;
587 lport->seq_offload = 0;
588 lport->lro_enabled = 0;
589 lport->lro_xid = 0;
590 lport->lso_max = 0;
Vasu Dev7f349142009-03-27 09:06:31 -0700591
592 return 0;
593}
594
595/**
Yi Zou54a5b212010-07-20 15:21:17 -0700596 * fcoe_netdev_features_change - Updates the lport's offload flags based
597 * on the LLD netdev's FCoE feature flags
598 */
599static void fcoe_netdev_features_change(struct fc_lport *lport,
600 struct net_device *netdev)
601{
602 mutex_lock(&lport->lp_mutex);
603
604 if (netdev->features & NETIF_F_SG)
605 lport->sg_supp = 1;
606 else
607 lport->sg_supp = 0;
608
609 if (netdev->features & NETIF_F_FCOE_CRC) {
610 lport->crc_offload = 1;
611 FCOE_NETDEV_DBG(netdev, "Supports FCCRC offload\n");
612 } else {
613 lport->crc_offload = 0;
614 }
615
616 if (netdev->features & NETIF_F_FSO) {
617 lport->seq_offload = 1;
618 lport->lso_max = netdev->gso_max_size;
619 FCOE_NETDEV_DBG(netdev, "Supports LSO for max len 0x%x\n",
620 lport->lso_max);
621 } else {
622 lport->seq_offload = 0;
623 lport->lso_max = 0;
624 }
625
626 if (netdev->fcoe_ddp_xid) {
627 lport->lro_enabled = 1;
628 lport->lro_xid = netdev->fcoe_ddp_xid;
629 FCOE_NETDEV_DBG(netdev, "Supports LRO for max xid 0x%x\n",
630 lport->lro_xid);
631 } else {
632 lport->lro_enabled = 0;
633 lport->lro_xid = 0;
634 }
635
636 mutex_unlock(&lport->lp_mutex);
637}
638
639/**
Robert Love1875f272009-11-03 11:47:50 -0800640 * fcoe_netdev_config() - Set up net devive for SW FCoE
641 * @lport: The local port that is associated with the net device
642 * @netdev: The associated net device
Vasu Dev7f349142009-03-27 09:06:31 -0700643 *
Robert Love1875f272009-11-03 11:47:50 -0800644 * Must be called after fcoe_lport_config() as it will use local port mutex
Vasu Dev7f349142009-03-27 09:06:31 -0700645 *
Robert Love1875f272009-11-03 11:47:50 -0800646 * Returns: 0 for success
Vasu Dev7f349142009-03-27 09:06:31 -0700647 */
Robert Love1875f272009-11-03 11:47:50 -0800648static int fcoe_netdev_config(struct fc_lport *lport, struct net_device *netdev)
Vasu Dev7f349142009-03-27 09:06:31 -0700649{
650 u32 mfs;
651 u64 wwnn, wwpn;
Chris Leech25024982009-08-25 13:59:35 -0700652 struct fcoe_interface *fcoe;
Chris Leech014f5c32009-08-25 13:59:30 -0700653 struct fcoe_port *port;
Vasu Dev7f349142009-03-27 09:06:31 -0700654
655 /* Setup lport private data to point to fcoe softc */
Robert Love1875f272009-11-03 11:47:50 -0800656 port = lport_priv(lport);
Bhanu Prakash Gollapudi8597ae82011-01-28 16:05:37 -0800657 fcoe = port->priv;
Vasu Dev7f349142009-03-27 09:06:31 -0700658
659 /*
660 * Determine max frame size based on underlying device and optional
661 * user-configured limit. If the MFS is too low, fcoe_link_ok()
662 * will return 0, so do this first.
663 */
Yi Zou7221d7e2009-10-21 16:27:52 -0700664 mfs = netdev->mtu;
665 if (netdev->features & NETIF_F_FCOE_MTU) {
666 mfs = FCOE_MTU;
667 FCOE_NETDEV_DBG(netdev, "Supports FCOE_MTU of %d bytes\n", mfs);
668 }
669 mfs -= (sizeof(struct fcoe_hdr) + sizeof(struct fcoe_crc_eof));
Robert Love1875f272009-11-03 11:47:50 -0800670 if (fc_set_mfs(lport, mfs))
Vasu Dev7f349142009-03-27 09:06:31 -0700671 return -EINVAL;
672
Vasu Dev7f349142009-03-27 09:06:31 -0700673 /* offload features support */
Yi Zou54a5b212010-07-20 15:21:17 -0700674 fcoe_netdev_features_change(lport, netdev);
Vasu Dev7f349142009-03-27 09:06:31 -0700675
Chris Leech014f5c32009-08-25 13:59:30 -0700676 skb_queue_head_init(&port->fcoe_pending_queue);
677 port->fcoe_pending_queue_active = 0;
Robert Love1875f272009-11-03 11:47:50 -0800678 setup_timer(&port->timer, fcoe_queue_timer, (unsigned long)lport);
Vasu Dev7f349142009-03-27 09:06:31 -0700679
Robert Love5e4f8fe2010-05-07 15:18:35 -0700680 fcoe_link_speed_update(lport);
681
Robert Love1875f272009-11-03 11:47:50 -0800682 if (!lport->vport) {
Yi Zoudcece412009-11-20 15:22:21 -0800683 if (fcoe_get_wwn(netdev, &wwnn, NETDEV_FCOE_WWNN))
684 wwnn = fcoe_wwn_from_mac(fcoe->ctlr.ctl_src_addr, 1, 0);
Robert Love1875f272009-11-03 11:47:50 -0800685 fc_set_wwnn(lport, wwnn);
Yi Zoudcece412009-11-20 15:22:21 -0800686 if (fcoe_get_wwn(netdev, &wwpn, NETDEV_FCOE_WWPN))
687 wwpn = fcoe_wwn_from_mac(fcoe->ctlr.ctl_src_addr,
Vasu Devcf4aebca2010-07-20 15:21:22 -0700688 2, 0);
Robert Love1875f272009-11-03 11:47:50 -0800689 fc_set_wwpn(lport, wwpn);
Chris Leech9a057532009-11-03 11:46:40 -0800690 }
Vasu Dev7f349142009-03-27 09:06:31 -0700691
Vasu Dev7f349142009-03-27 09:06:31 -0700692 return 0;
693}
694
695/**
Robert Love1875f272009-11-03 11:47:50 -0800696 * fcoe_shost_config() - Set up the SCSI host associated with a local port
697 * @lport: The local port
Robert Love1875f272009-11-03 11:47:50 -0800698 * @dev: The device associated with the SCSI host
Vasu Dev7f349142009-03-27 09:06:31 -0700699 *
700 * Must be called after fcoe_lport_config() and fcoe_netdev_config()
701 *
Robert Love1875f272009-11-03 11:47:50 -0800702 * Returns: 0 for success
Vasu Dev7f349142009-03-27 09:06:31 -0700703 */
Vasu Dev8ba00a42010-04-09 14:22:54 -0700704static int fcoe_shost_config(struct fc_lport *lport, struct device *dev)
Vasu Dev7f349142009-03-27 09:06:31 -0700705{
706 int rc = 0;
707
708 /* lport scsi host config */
Robert Love1875f272009-11-03 11:47:50 -0800709 lport->host->max_lun = FCOE_MAX_LUN;
710 lport->host->max_id = FCOE_MAX_FCP_TARGET;
711 lport->host->max_channel = 0;
Vasu Devda87bfa2010-04-09 14:22:59 -0700712 lport->host->max_cmd_len = FCOE_MAX_CMD_LEN;
713
Robert Love1875f272009-11-03 11:47:50 -0800714 if (lport->vport)
Yi Zou8ca86f82011-01-28 16:05:11 -0800715 lport->host->transportt = fcoe_vport_scsi_transport;
Chris Leeche9084bb2009-11-03 11:46:34 -0800716 else
Yi Zou8ca86f82011-01-28 16:05:11 -0800717 lport->host->transportt = fcoe_nport_scsi_transport;
Vasu Dev7f349142009-03-27 09:06:31 -0700718
719 /* add the new host to the SCSI-ml */
Robert Love1875f272009-11-03 11:47:50 -0800720 rc = scsi_add_host(lport->host, dev);
Vasu Dev7f349142009-03-27 09:06:31 -0700721 if (rc) {
Robert Love1875f272009-11-03 11:47:50 -0800722 FCOE_NETDEV_DBG(fcoe_netdev(lport), "fcoe_shost_config: "
Robert Loved5488eb2009-06-10 15:30:59 -0700723 "error on scsi_add_host\n");
Vasu Dev7f349142009-03-27 09:06:31 -0700724 return rc;
725 }
Chris Leech9a057532009-11-03 11:46:40 -0800726
Robert Love1875f272009-11-03 11:47:50 -0800727 if (!lport->vport)
Alexey Dobriyan4be929b2010-05-24 14:33:03 -0700728 fc_host_max_npiv_vports(lport->host) = USHRT_MAX;
Chris Leech9a057532009-11-03 11:46:40 -0800729
Robert Love1875f272009-11-03 11:47:50 -0800730 snprintf(fc_host_symbolic_name(lport->host), FC_SYMBOLIC_NAME_SIZE,
Chris Leech5baa17c2009-11-03 11:46:56 -0800731 "%s v%s over %s", FCOE_NAME, FCOE_VERSION,
Robert Love1875f272009-11-03 11:47:50 -0800732 fcoe_netdev(lport)->name);
Vasu Dev7f349142009-03-27 09:06:31 -0700733
734 return 0;
735}
736
Neerav Parikh6fef3902012-01-22 17:30:10 -0800737
738/**
739 * fcoe_fdmi_info() - Get FDMI related info from net devive for SW FCoE
740 * @lport: The local port that is associated with the net device
741 * @netdev: The associated net device
742 *
743 * Must be called after fcoe_shost_config() as it will use local port mutex
744 *
745 */
746static void fcoe_fdmi_info(struct fc_lport *lport, struct net_device *netdev)
747{
748 struct fcoe_interface *fcoe;
749 struct fcoe_port *port;
750 struct net_device *realdev;
751 int rc;
752 struct netdev_fcoe_hbainfo fdmi;
753
754 port = lport_priv(lport);
755 fcoe = port->priv;
756 realdev = fcoe->realdev;
757
758 if (!realdev)
759 return;
760
761 /* No FDMI state m/c for NPIV ports */
762 if (lport->vport)
763 return;
764
765 if (realdev->netdev_ops->ndo_fcoe_get_hbainfo) {
766 memset(&fdmi, 0, sizeof(fdmi));
767 rc = realdev->netdev_ops->ndo_fcoe_get_hbainfo(realdev,
768 &fdmi);
769 if (rc) {
770 printk(KERN_INFO "fcoe: Failed to retrieve FDMI "
771 "information from netdev.\n");
772 return;
773 }
774
775 snprintf(fc_host_serial_number(lport->host),
776 FC_SERIAL_NUMBER_SIZE,
777 "%s",
778 fdmi.serial_number);
779 snprintf(fc_host_manufacturer(lport->host),
780 FC_SERIAL_NUMBER_SIZE,
781 "%s",
782 fdmi.manufacturer);
783 snprintf(fc_host_model(lport->host),
784 FC_SYMBOLIC_NAME_SIZE,
785 "%s",
786 fdmi.model);
787 snprintf(fc_host_model_description(lport->host),
788 FC_SYMBOLIC_NAME_SIZE,
789 "%s",
790 fdmi.model_description);
791 snprintf(fc_host_hardware_version(lport->host),
792 FC_VERSION_STRING_SIZE,
793 "%s",
794 fdmi.hardware_version);
795 snprintf(fc_host_driver_version(lport->host),
796 FC_VERSION_STRING_SIZE,
797 "%s",
798 fdmi.driver_version);
799 snprintf(fc_host_optionrom_version(lport->host),
800 FC_VERSION_STRING_SIZE,
801 "%s",
802 fdmi.optionrom_version);
803 snprintf(fc_host_firmware_version(lport->host),
804 FC_VERSION_STRING_SIZE,
805 "%s",
806 fdmi.firmware_version);
807
808 /* Enable FDMI lport states */
809 lport->fdmi_enabled = 1;
810 } else {
811 lport->fdmi_enabled = 0;
812 printk(KERN_INFO "fcoe: No FDMI support.\n");
813 }
814}
815
Robert Love1875f272009-11-03 11:47:50 -0800816/**
817 * fcoe_oem_match() - The match routine for the offloaded exchange manager
818 * @fp: The I/O frame
Vasu Devd7179682009-07-29 17:05:21 -0700819 *
Robert Love1875f272009-11-03 11:47:50 -0800820 * This routine will be associated with an exchange manager (EM). When
821 * the libfc exchange handling code is looking for an EM to use it will
822 * call this routine and pass it the frame that it wishes to send. This
823 * routine will return True if the associated EM is to be used and False
824 * if the echange code should continue looking for an EM.
825 *
826 * The offload EM that this routine is associated with will handle any
827 * packets that are for SCSI read requests.
828 *
Kiran Patil1ff99182011-06-20 16:59:15 -0700829 * This has been enhanced to work when FCoE stack is operating in target
830 * mode.
831 *
Robert Love1875f272009-11-03 11:47:50 -0800832 * Returns: True for read types I/O, otherwise returns false.
Vasu Devd7179682009-07-29 17:05:21 -0700833 */
Bart Van Assche7c9c6842012-01-13 17:26:25 -0800834static bool fcoe_oem_match(struct fc_frame *fp)
Vasu Devd7179682009-07-29 17:05:21 -0700835{
Kiran Patil1ff99182011-06-20 16:59:15 -0700836 struct fc_frame_header *fh = fc_frame_header_get(fp);
837 struct fcp_cmnd *fcp;
838
839 if (fc_fcp_is_read(fr_fsp(fp)) &&
840 (fr_fsp(fp)->data_len > fcoe_ddp_min))
841 return true;
Yi Zoua762dce2012-01-13 17:26:15 -0800842 else if ((fr_fsp(fp) == NULL) &&
843 (fh->fh_r_ctl == FC_RCTL_DD_UNSOL_CMD) &&
844 (ntohs(fh->fh_rx_id) == FC_XID_UNKNOWN)) {
Kiran Patil1ff99182011-06-20 16:59:15 -0700845 fcp = fc_frame_payload_get(fp, sizeof(*fcp));
Yi Zoua762dce2012-01-13 17:26:15 -0800846 if ((fcp->fc_flags & FCP_CFL_WRDATA) &&
847 (ntohl(fcp->fc_dl) > fcoe_ddp_min))
Kiran Patil1ff99182011-06-20 16:59:15 -0700848 return true;
849 }
850 return false;
Vasu Devd7179682009-07-29 17:05:21 -0700851}
852
Vasu Dev7f349142009-03-27 09:06:31 -0700853/**
Robert Love1875f272009-11-03 11:47:50 -0800854 * fcoe_em_config() - Allocate and configure an exchange manager
855 * @lport: The local port that the new EM will be associated with
Vasu Dev7f349142009-03-27 09:06:31 -0700856 *
Robert Love1875f272009-11-03 11:47:50 -0800857 * Returns: 0 on success
Vasu Dev7f349142009-03-27 09:06:31 -0700858 */
Robert Love1875f272009-11-03 11:47:50 -0800859static inline int fcoe_em_config(struct fc_lport *lport)
Vasu Dev7f349142009-03-27 09:06:31 -0700860{
Robert Love1875f272009-11-03 11:47:50 -0800861 struct fcoe_port *port = lport_priv(lport);
Bhanu Prakash Gollapudi8597ae82011-01-28 16:05:37 -0800862 struct fcoe_interface *fcoe = port->priv;
Chris Leech25024982009-08-25 13:59:35 -0700863 struct fcoe_interface *oldfcoe = NULL;
Vasu Dev1d1b88d2009-07-29 17:05:45 -0700864 struct net_device *old_real_dev, *cur_real_dev;
Vasu Devd7179682009-07-29 17:05:21 -0700865 u16 min_xid = FCOE_MIN_XID;
866 u16 max_xid = FCOE_MAX_XID;
867
868 /*
869 * Check if need to allocate an em instance for
870 * offload exchange ids to be shared across all VN_PORTs/lport.
871 */
Robert Love1875f272009-11-03 11:47:50 -0800872 if (!lport->lro_enabled || !lport->lro_xid ||
873 (lport->lro_xid >= max_xid)) {
874 lport->lro_xid = 0;
Vasu Devd7179682009-07-29 17:05:21 -0700875 goto skip_oem;
876 }
877
878 /*
879 * Reuse existing offload em instance in case
Vasu Dev1d1b88d2009-07-29 17:05:45 -0700880 * it is already allocated on real eth device
Vasu Devd7179682009-07-29 17:05:21 -0700881 */
Chris Leech25024982009-08-25 13:59:35 -0700882 if (fcoe->netdev->priv_flags & IFF_802_1Q_VLAN)
883 cur_real_dev = vlan_dev_real_dev(fcoe->netdev);
Vasu Dev1d1b88d2009-07-29 17:05:45 -0700884 else
Chris Leech25024982009-08-25 13:59:35 -0700885 cur_real_dev = fcoe->netdev;
Vasu Dev1d1b88d2009-07-29 17:05:45 -0700886
Chris Leech25024982009-08-25 13:59:35 -0700887 list_for_each_entry(oldfcoe, &fcoe_hostlist, list) {
Chris Leech25024982009-08-25 13:59:35 -0700888 if (oldfcoe->netdev->priv_flags & IFF_802_1Q_VLAN)
889 old_real_dev = vlan_dev_real_dev(oldfcoe->netdev);
Vasu Dev1d1b88d2009-07-29 17:05:45 -0700890 else
Chris Leech25024982009-08-25 13:59:35 -0700891 old_real_dev = oldfcoe->netdev;
Vasu Dev1d1b88d2009-07-29 17:05:45 -0700892
893 if (cur_real_dev == old_real_dev) {
Chris Leech991cbb62009-08-25 13:59:51 -0700894 fcoe->oem = oldfcoe->oem;
Vasu Devd7179682009-07-29 17:05:21 -0700895 break;
896 }
897 }
898
Chris Leech991cbb62009-08-25 13:59:51 -0700899 if (fcoe->oem) {
Robert Love1875f272009-11-03 11:47:50 -0800900 if (!fc_exch_mgr_add(lport, fcoe->oem, fcoe_oem_match)) {
Vasu Devd7179682009-07-29 17:05:21 -0700901 printk(KERN_ERR "fcoe_em_config: failed to add "
902 "offload em:%p on interface:%s\n",
Chris Leech991cbb62009-08-25 13:59:51 -0700903 fcoe->oem, fcoe->netdev->name);
Vasu Devd7179682009-07-29 17:05:21 -0700904 return -ENOMEM;
905 }
906 } else {
Robert Love1875f272009-11-03 11:47:50 -0800907 fcoe->oem = fc_exch_mgr_alloc(lport, FC_CLASS_3,
908 FCOE_MIN_XID, lport->lro_xid,
909 fcoe_oem_match);
Chris Leech991cbb62009-08-25 13:59:51 -0700910 if (!fcoe->oem) {
Vasu Devd7179682009-07-29 17:05:21 -0700911 printk(KERN_ERR "fcoe_em_config: failed to allocate "
912 "em for offload exches on interface:%s\n",
Chris Leech25024982009-08-25 13:59:35 -0700913 fcoe->netdev->name);
Vasu Devd7179682009-07-29 17:05:21 -0700914 return -ENOMEM;
915 }
916 }
917
918 /*
919 * Exclude offload EM xid range from next EM xid range.
920 */
Robert Love1875f272009-11-03 11:47:50 -0800921 min_xid += lport->lro_xid + 1;
Vasu Devd7179682009-07-29 17:05:21 -0700922
923skip_oem:
Robert Love1875f272009-11-03 11:47:50 -0800924 if (!fc_exch_mgr_alloc(lport, FC_CLASS_3, min_xid, max_xid, NULL)) {
Vasu Devd7179682009-07-29 17:05:21 -0700925 printk(KERN_ERR "fcoe_em_config: failed to "
Chris Leech25024982009-08-25 13:59:35 -0700926 "allocate em on interface %s\n", fcoe->netdev->name);
Vasu Dev7f349142009-03-27 09:06:31 -0700927 return -ENOMEM;
Vasu Devd7179682009-07-29 17:05:21 -0700928 }
Vasu Dev7f349142009-03-27 09:06:31 -0700929
930 return 0;
931}
932
933/**
Robert Love1875f272009-11-03 11:47:50 -0800934 * fcoe_if_destroy() - Tear down a SW FCoE instance
935 * @lport: The local port to be destroyed
Vasu Dev34ce27b2010-05-07 15:18:46 -0700936 *
Vasu Dev7f349142009-03-27 09:06:31 -0700937 */
Chris Leechaf7f85d2009-08-25 13:59:24 -0700938static void fcoe_if_destroy(struct fc_lport *lport)
Vasu Dev7f349142009-03-27 09:06:31 -0700939{
Neerav Parikhb2085a42011-06-20 16:59:51 -0700940 struct fcoe_port *port = lport_priv(lport);
941 struct fcoe_interface *fcoe = port->priv;
942 struct net_device *netdev = fcoe->netdev;
943
944 FCOE_NETDEV_DBG(netdev, "Destroying interface\n");
945
946 /* Logout of the fabric */
947 fc_fabric_logoff(lport);
948
949 /* Cleanup the fc_lport */
950 fc_lport_destroy(lport);
951
952 /* Stop the transmit retry timer */
953 del_timer_sync(&port->timer);
954
955 /* Free existing transmit skbs */
956 fcoe_clean_pending_queue(lport);
957
958 rtnl_lock();
959 if (!is_zero_ether_addr(port->data_src_addr))
960 dev_uc_del(netdev, port->data_src_addr);
961 rtnl_unlock();
962
963 /* Release reference held in fcoe_if_create() */
964 fcoe_interface_put(fcoe);
965
Chris Leech54b649f2009-08-25 14:00:07 -0700966 /* Free queued packets for the per-CPU receive threads */
967 fcoe_percpu_clean(lport);
968
Vasu Dev7f349142009-03-27 09:06:31 -0700969 /* Detach from the scsi-ml */
Chris Leechaf7f85d2009-08-25 13:59:24 -0700970 fc_remove_host(lport->host);
971 scsi_remove_host(lport->host);
Vasu Dev7f349142009-03-27 09:06:31 -0700972
Yi Zou80e736f2010-11-30 16:18:07 -0800973 /* Destroy lport scsi_priv */
974 fc_fcp_destroy(lport);
975
Vasu Dev7f349142009-03-27 09:06:31 -0700976 /* There are no more rports or I/O, free the EM */
Chris Leechaf7f85d2009-08-25 13:59:24 -0700977 fc_exch_mgr_free(lport);
Vasu Dev7f349142009-03-27 09:06:31 -0700978
Vasu Dev7f349142009-03-27 09:06:31 -0700979 /* Free memory used by statistical counters */
Chris Leechaf7f85d2009-08-25 13:59:24 -0700980 fc_lport_free_stats(lport);
Vasu Dev7f349142009-03-27 09:06:31 -0700981
Chris Leech2e70e242009-08-25 14:00:23 -0700982 /* Release the Scsi_Host */
Chris Leechaf7f85d2009-08-25 13:59:24 -0700983 scsi_host_put(lport->host);
Vasu Dev7f349142009-03-27 09:06:31 -0700984}
985
Robert Love1875f272009-11-03 11:47:50 -0800986/**
987 * fcoe_ddp_setup() - Call a LLD's ddp_setup through the net device
988 * @lport: The local port to setup DDP for
989 * @xid: The exchange ID for this DDP transfer
990 * @sgl: The scatterlist describing this transfer
991 * @sgc: The number of sg items
Vasu Dev7f349142009-03-27 09:06:31 -0700992 *
Robert Love1875f272009-11-03 11:47:50 -0800993 * Returns: 0 if the DDP context was not configured
Vasu Dev7f349142009-03-27 09:06:31 -0700994 */
Robert Love1875f272009-11-03 11:47:50 -0800995static int fcoe_ddp_setup(struct fc_lport *lport, u16 xid,
996 struct scatterlist *sgl, unsigned int sgc)
Vasu Dev7f349142009-03-27 09:06:31 -0700997{
Robert Love1875f272009-11-03 11:47:50 -0800998 struct net_device *netdev = fcoe_netdev(lport);
Vasu Dev7f349142009-03-27 09:06:31 -0700999
Robert Love1875f272009-11-03 11:47:50 -08001000 if (netdev->netdev_ops->ndo_fcoe_ddp_setup)
1001 return netdev->netdev_ops->ndo_fcoe_ddp_setup(netdev,
1002 xid, sgl,
1003 sgc);
Vasu Dev7f349142009-03-27 09:06:31 -07001004
1005 return 0;
1006}
1007
Vasu Dev7f349142009-03-27 09:06:31 -07001008/**
Yi Zou71f89492011-06-20 16:59:10 -07001009 * fcoe_ddp_target() - Call a LLD's ddp_target through the net device
1010 * @lport: The local port to setup DDP for
1011 * @xid: The exchange ID for this DDP transfer
1012 * @sgl: The scatterlist describing this transfer
1013 * @sgc: The number of sg items
1014 *
1015 * Returns: 0 if the DDP context was not configured
1016 */
1017static int fcoe_ddp_target(struct fc_lport *lport, u16 xid,
1018 struct scatterlist *sgl, unsigned int sgc)
1019{
1020 struct net_device *netdev = fcoe_netdev(lport);
1021
1022 if (netdev->netdev_ops->ndo_fcoe_ddp_target)
1023 return netdev->netdev_ops->ndo_fcoe_ddp_target(netdev, xid,
1024 sgl, sgc);
1025
1026 return 0;
1027}
1028
1029
1030/**
Robert Love1875f272009-11-03 11:47:50 -08001031 * fcoe_ddp_done() - Call a LLD's ddp_done through the net device
1032 * @lport: The local port to complete DDP on
1033 * @xid: The exchange ID for this DDP transfer
Vasu Dev7f349142009-03-27 09:06:31 -07001034 *
Robert Love1875f272009-11-03 11:47:50 -08001035 * Returns: the length of data that have been completed by DDP
1036 */
1037static int fcoe_ddp_done(struct fc_lport *lport, u16 xid)
1038{
1039 struct net_device *netdev = fcoe_netdev(lport);
1040
1041 if (netdev->netdev_ops->ndo_fcoe_ddp_done)
1042 return netdev->netdev_ops->ndo_fcoe_ddp_done(netdev, xid);
1043 return 0;
1044}
1045
1046/**
1047 * fcoe_if_create() - Create a FCoE instance on an interface
1048 * @fcoe: The FCoE interface to create a local port on
1049 * @parent: The device pointer to be the parent in sysfs for the SCSI host
1050 * @npiv: Indicates if the port is a vport or not
Vasu Dev7f349142009-03-27 09:06:31 -07001051 *
Robert Love1875f272009-11-03 11:47:50 -08001052 * Creates a fc_lport instance and a Scsi_Host instance and configure them.
1053 *
1054 * Returns: The allocated fc_lport or an error pointer
Vasu Dev7f349142009-03-27 09:06:31 -07001055 */
Chris Leech030f4e02009-08-25 14:00:02 -07001056static struct fc_lport *fcoe_if_create(struct fcoe_interface *fcoe,
Chris Leech9a057532009-11-03 11:46:40 -08001057 struct device *parent, int npiv)
Vasu Dev7f349142009-03-27 09:06:31 -07001058{
Robert Love1875f272009-11-03 11:47:50 -08001059 struct net_device *netdev = fcoe->netdev;
Vasu Dev72fa3962011-02-25 15:03:01 -08001060 struct fc_lport *lport, *n_port;
Chris Leech014f5c32009-08-25 13:59:30 -07001061 struct fcoe_port *port;
Vasu Dev72fa3962011-02-25 15:03:01 -08001062 struct Scsi_Host *shost;
Robert Love1875f272009-11-03 11:47:50 -08001063 int rc;
Chris Leech9a057532009-11-03 11:46:40 -08001064 /*
1065 * parent is only a vport if npiv is 1,
1066 * but we'll only use vport in that case so go ahead and set it
1067 */
1068 struct fc_vport *vport = dev_to_vport(parent);
Vasu Dev7f349142009-03-27 09:06:31 -07001069
Robert Loved5488eb2009-06-10 15:30:59 -07001070 FCOE_NETDEV_DBG(netdev, "Create Interface\n");
Vasu Dev7f349142009-03-27 09:06:31 -07001071
Vasu Dev72fa3962011-02-25 15:03:01 -08001072 if (!npiv)
1073 lport = libfc_host_alloc(&fcoe_shost_template, sizeof(*port));
1074 else
1075 lport = libfc_vport_create(vport, sizeof(*port));
1076
Chris Leech86221962009-11-03 11:46:08 -08001077 if (!lport) {
Chris Leech014f5c32009-08-25 13:59:30 -07001078 FCOE_NETDEV_DBG(netdev, "Could not allocate host structure\n");
1079 rc = -ENOMEM;
Chris Leech030f4e02009-08-25 14:00:02 -07001080 goto out;
Chris Leech014f5c32009-08-25 13:59:30 -07001081 }
Chris Leech014f5c32009-08-25 13:59:30 -07001082 port = lport_priv(lport);
Chris Leech2e70e242009-08-25 14:00:23 -07001083 port->lport = lport;
Bhanu Prakash Gollapudi8597ae82011-01-28 16:05:37 -08001084 port->priv = fcoe;
1085 port->max_queue_depth = FCOE_MAX_QUEUE_DEPTH;
1086 port->min_queue_depth = FCOE_MIN_QUEUE_DEPTH;
Chris Leech2e70e242009-08-25 14:00:23 -07001087 INIT_WORK(&port->destroy_work, fcoe_destroy_work);
Vasu Dev7f349142009-03-27 09:06:31 -07001088
Robert Love1875f272009-11-03 11:47:50 -08001089 /* configure a fc_lport including the exchange manager */
Chris Leechaf7f85d2009-08-25 13:59:24 -07001090 rc = fcoe_lport_config(lport);
Vasu Dev7f349142009-03-27 09:06:31 -07001091 if (rc) {
Robert Loved5488eb2009-06-10 15:30:59 -07001092 FCOE_NETDEV_DBG(netdev, "Could not configure lport for the "
1093 "interface\n");
Vasu Dev7f349142009-03-27 09:06:31 -07001094 goto out_host_put;
1095 }
1096
Chris Leech9a057532009-11-03 11:46:40 -08001097 if (npiv) {
Chris Leech9f8f3aa2010-04-09 14:23:16 -07001098 FCOE_NETDEV_DBG(netdev, "Setting vport names, "
1099 "%16.16llx %16.16llx\n",
Robert Love1875f272009-11-03 11:47:50 -08001100 vport->node_name, vport->port_name);
Chris Leech9a057532009-11-03 11:46:40 -08001101 fc_set_wwnn(lport, vport->node_name);
1102 fc_set_wwpn(lport, vport->port_name);
1103 }
1104
Vasu Devab6b85c2009-05-17 12:33:08 +00001105 /* configure lport network properties */
Chris Leechaf7f85d2009-08-25 13:59:24 -07001106 rc = fcoe_netdev_config(lport, netdev);
Vasu Devab6b85c2009-05-17 12:33:08 +00001107 if (rc) {
Robert Loved5488eb2009-06-10 15:30:59 -07001108 FCOE_NETDEV_DBG(netdev, "Could not configure netdev for the "
1109 "interface\n");
Chris Leech54b649f2009-08-25 14:00:07 -07001110 goto out_lp_destroy;
Vasu Devab6b85c2009-05-17 12:33:08 +00001111 }
Joe Eykholt97c83892009-03-17 11:42:40 -07001112
Vasu Dev7f349142009-03-27 09:06:31 -07001113 /* configure lport scsi host properties */
Vasu Dev8ba00a42010-04-09 14:22:54 -07001114 rc = fcoe_shost_config(lport, parent);
Vasu Dev7f349142009-03-27 09:06:31 -07001115 if (rc) {
Robert Loved5488eb2009-06-10 15:30:59 -07001116 FCOE_NETDEV_DBG(netdev, "Could not configure shost for the "
1117 "interface\n");
Chris Leech54b649f2009-08-25 14:00:07 -07001118 goto out_lp_destroy;
Vasu Dev7f349142009-03-27 09:06:31 -07001119 }
1120
Vasu Dev7f349142009-03-27 09:06:31 -07001121 /* Initialize the library */
Joe Eykholte10f8c62010-07-20 15:20:30 -07001122 rc = fcoe_libfc_config(lport, &fcoe->ctlr, &fcoe_libfc_fcn_templ, 1);
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 libfc for the "
1125 "interface\n");
Vasu Dev7f349142009-03-27 09:06:31 -07001126 goto out_lp_destroy;
1127 }
1128
Neerav Parikh6fef3902012-01-22 17:30:10 -08001129 /* Initialized FDMI information */
1130 fcoe_fdmi_info(lport, netdev);
1131
Vasu Dev72fa3962011-02-25 15:03:01 -08001132 /*
1133 * fcoe_em_alloc() and fcoe_hostlist_add() both
1134 * need to be atomic with respect to other changes to the
1135 * hostlist since fcoe_em_alloc() looks for an existing EM
1136 * instance on host list updated by fcoe_hostlist_add().
1137 *
1138 * This is currently handled through the fcoe_config_mutex
1139 * begin held.
1140 */
1141 if (!npiv)
Chris Leech9a057532009-11-03 11:46:40 -08001142 /* lport exch manager allocation */
1143 rc = fcoe_em_config(lport);
Vasu Dev72fa3962011-02-25 15:03:01 -08001144 else {
1145 shost = vport_to_shost(vport);
1146 n_port = shost_priv(shost);
1147 rc = fc_exch_mgr_list_clone(n_port, lport);
1148 }
1149
1150 if (rc) {
1151 FCOE_NETDEV_DBG(netdev, "Could not configure the EM\n");
1152 goto out_lp_destroy;
Vasu Dev96316092009-07-29 17:05:00 -07001153 }
1154
Chris Leech030f4e02009-08-25 14:00:02 -07001155 fcoe_interface_get(fcoe);
Chris Leechaf7f85d2009-08-25 13:59:24 -07001156 return lport;
Vasu Dev7f349142009-03-27 09:06:31 -07001157
1158out_lp_destroy:
Chris Leechaf7f85d2009-08-25 13:59:24 -07001159 fc_exch_mgr_free(lport);
Vasu Dev7f349142009-03-27 09:06:31 -07001160out_host_put:
Chris Leechaf7f85d2009-08-25 13:59:24 -07001161 scsi_host_put(lport->host);
1162out:
1163 return ERR_PTR(rc);
Vasu Dev7f349142009-03-27 09:06:31 -07001164}
1165
1166/**
Robert Love1875f272009-11-03 11:47:50 -08001167 * fcoe_if_init() - Initialization routine for fcoe.ko
Vasu Dev7f349142009-03-27 09:06:31 -07001168 *
Robert Love1875f272009-11-03 11:47:50 -08001169 * Attaches the SW FCoE transport to the FC transport
1170 *
1171 * Returns: 0 on success
Vasu Dev7f349142009-03-27 09:06:31 -07001172 */
1173static int __init fcoe_if_init(void)
1174{
1175 /* attach to scsi transport */
Yi Zou8ca86f82011-01-28 16:05:11 -08001176 fcoe_nport_scsi_transport =
1177 fc_attach_transport(&fcoe_nport_fc_functions);
1178 fcoe_vport_scsi_transport =
1179 fc_attach_transport(&fcoe_vport_fc_functions);
Vasu Dev7f349142009-03-27 09:06:31 -07001180
Yi Zou8ca86f82011-01-28 16:05:11 -08001181 if (!fcoe_nport_scsi_transport) {
Robert Loved5488eb2009-06-10 15:30:59 -07001182 printk(KERN_ERR "fcoe: Failed to attach to the FC transport\n");
Vasu Dev7f349142009-03-27 09:06:31 -07001183 return -ENODEV;
1184 }
1185
1186 return 0;
1187}
1188
1189/**
Robert Love1875f272009-11-03 11:47:50 -08001190 * fcoe_if_exit() - Tear down fcoe.ko
Vasu Dev7f349142009-03-27 09:06:31 -07001191 *
Robert Love1875f272009-11-03 11:47:50 -08001192 * Detaches the SW FCoE transport from the FC transport
1193 *
1194 * Returns: 0 on success
Vasu Dev7f349142009-03-27 09:06:31 -07001195 */
Bart Van Assche7c9c6842012-01-13 17:26:25 -08001196static int __exit fcoe_if_exit(void)
Vasu Dev7f349142009-03-27 09:06:31 -07001197{
Yi Zou8ca86f82011-01-28 16:05:11 -08001198 fc_release_transport(fcoe_nport_scsi_transport);
1199 fc_release_transport(fcoe_vport_scsi_transport);
1200 fcoe_nport_scsi_transport = NULL;
1201 fcoe_vport_scsi_transport = NULL;
Vasu Dev7f349142009-03-27 09:06:31 -07001202 return 0;
1203}
1204
Robert Love85b4aa42008-12-09 15:10:24 -08001205/**
Robert Love1875f272009-11-03 11:47:50 -08001206 * fcoe_percpu_thread_create() - Create a receive thread for an online CPU
1207 * @cpu: The CPU index of the CPU to create a receive thread for
Robert Love8976f422009-03-17 11:41:46 -07001208 */
1209static void fcoe_percpu_thread_create(unsigned int cpu)
1210{
1211 struct fcoe_percpu_s *p;
1212 struct task_struct *thread;
1213
1214 p = &per_cpu(fcoe_percpu, cpu);
1215
Eric Dumazet5c609ff2011-09-27 21:37:52 -07001216 thread = kthread_create_on_node(fcoe_percpu_receive_thread,
1217 (void *)p, cpu_to_node(cpu),
1218 "fcoethread/%d", cpu);
Robert Love8976f422009-03-17 11:41:46 -07001219
Joe Eykholte7a51992009-08-25 14:04:08 -07001220 if (likely(!IS_ERR(thread))) {
Robert Love8976f422009-03-17 11:41:46 -07001221 kthread_bind(thread, cpu);
1222 wake_up_process(thread);
1223
1224 spin_lock_bh(&p->fcoe_rx_list.lock);
1225 p->thread = thread;
1226 spin_unlock_bh(&p->fcoe_rx_list.lock);
1227 }
1228}
1229
1230/**
Robert Love1875f272009-11-03 11:47:50 -08001231 * fcoe_percpu_thread_destroy() - Remove the receive thread of a CPU
1232 * @cpu: The CPU index of the CPU whose receive thread is to be destroyed
Robert Love8976f422009-03-17 11:41:46 -07001233 *
1234 * Destroys a per-CPU Rx thread. Any pending skbs are moved to the
1235 * current CPU's Rx thread. If the thread being destroyed is bound to
1236 * the CPU processing this context the skbs will be freed.
1237 */
1238static void fcoe_percpu_thread_destroy(unsigned int cpu)
1239{
1240 struct fcoe_percpu_s *p;
1241 struct task_struct *thread;
1242 struct page *crc_eof;
1243 struct sk_buff *skb;
1244#ifdef CONFIG_SMP
1245 struct fcoe_percpu_s *p0;
Joe Eykholtf018b732010-03-12 16:08:55 -08001246 unsigned targ_cpu = get_cpu();
Robert Love8976f422009-03-17 11:41:46 -07001247#endif /* CONFIG_SMP */
1248
Robert Loved5488eb2009-06-10 15:30:59 -07001249 FCOE_DBG("Destroying receive thread for CPU %d\n", cpu);
Robert Love8976f422009-03-17 11:41:46 -07001250
1251 /* Prevent any new skbs from being queued for this CPU. */
1252 p = &per_cpu(fcoe_percpu, cpu);
1253 spin_lock_bh(&p->fcoe_rx_list.lock);
1254 thread = p->thread;
1255 p->thread = NULL;
1256 crc_eof = p->crc_eof_page;
1257 p->crc_eof_page = NULL;
1258 p->crc_eof_offset = 0;
1259 spin_unlock_bh(&p->fcoe_rx_list.lock);
1260
1261#ifdef CONFIG_SMP
1262 /*
1263 * Don't bother moving the skb's if this context is running
1264 * on the same CPU that is having its thread destroyed. This
1265 * can easily happen when the module is removed.
1266 */
1267 if (cpu != targ_cpu) {
1268 p0 = &per_cpu(fcoe_percpu, targ_cpu);
1269 spin_lock_bh(&p0->fcoe_rx_list.lock);
1270 if (p0->thread) {
Robert Loved5488eb2009-06-10 15:30:59 -07001271 FCOE_DBG("Moving frames from CPU %d to CPU %d\n",
1272 cpu, targ_cpu);
Robert Love8976f422009-03-17 11:41:46 -07001273
1274 while ((skb = __skb_dequeue(&p->fcoe_rx_list)) != NULL)
1275 __skb_queue_tail(&p0->fcoe_rx_list, skb);
1276 spin_unlock_bh(&p0->fcoe_rx_list.lock);
1277 } else {
1278 /*
1279 * The targeted CPU is not initialized and cannot accept
Robert Love1875f272009-11-03 11:47:50 -08001280 * new skbs. Unlock the targeted CPU and drop the skbs
Robert Love8976f422009-03-17 11:41:46 -07001281 * on the CPU that is going offline.
1282 */
1283 while ((skb = __skb_dequeue(&p->fcoe_rx_list)) != NULL)
1284 kfree_skb(skb);
1285 spin_unlock_bh(&p0->fcoe_rx_list.lock);
1286 }
1287 } else {
1288 /*
1289 * This scenario occurs when the module is being removed
1290 * and all threads are being destroyed. skbs will continue
1291 * to be shifted from the CPU thread that is being removed
1292 * to the CPU thread associated with the CPU that is processing
1293 * the module removal. Once there is only one CPU Rx thread it
1294 * will reach this case and we will drop all skbs and later
1295 * stop the thread.
1296 */
1297 spin_lock_bh(&p->fcoe_rx_list.lock);
1298 while ((skb = __skb_dequeue(&p->fcoe_rx_list)) != NULL)
1299 kfree_skb(skb);
1300 spin_unlock_bh(&p->fcoe_rx_list.lock);
1301 }
Joe Eykholtf018b732010-03-12 16:08:55 -08001302 put_cpu();
Robert Love8976f422009-03-17 11:41:46 -07001303#else
1304 /*
Chris Leechdd3fd722009-04-21 16:27:36 -07001305 * This a non-SMP scenario where the singular Rx thread is
Robert Love8976f422009-03-17 11:41:46 -07001306 * being removed. Free all skbs and stop the thread.
1307 */
1308 spin_lock_bh(&p->fcoe_rx_list.lock);
1309 while ((skb = __skb_dequeue(&p->fcoe_rx_list)) != NULL)
1310 kfree_skb(skb);
1311 spin_unlock_bh(&p->fcoe_rx_list.lock);
1312#endif
1313
1314 if (thread)
1315 kthread_stop(thread);
1316
1317 if (crc_eof)
1318 put_page(crc_eof);
1319}
1320
1321/**
Robert Love1875f272009-11-03 11:47:50 -08001322 * fcoe_cpu_callback() - Handler for CPU hotplug events
1323 * @nfb: The callback data block
1324 * @action: The event triggering the callback
1325 * @hcpu: The index of the CPU that the event is for
Robert Love8976f422009-03-17 11:41:46 -07001326 *
Robert Love1875f272009-11-03 11:47:50 -08001327 * This creates or destroys per-CPU data for fcoe
Robert Love8976f422009-03-17 11:41:46 -07001328 *
1329 * Returns NOTIFY_OK always.
1330 */
1331static int fcoe_cpu_callback(struct notifier_block *nfb,
1332 unsigned long action, void *hcpu)
1333{
1334 unsigned cpu = (unsigned long)hcpu;
1335
1336 switch (action) {
1337 case CPU_ONLINE:
1338 case CPU_ONLINE_FROZEN:
Robert Loved5488eb2009-06-10 15:30:59 -07001339 FCOE_DBG("CPU %x online: Create Rx thread\n", cpu);
Robert Love8976f422009-03-17 11:41:46 -07001340 fcoe_percpu_thread_create(cpu);
1341 break;
1342 case CPU_DEAD:
1343 case CPU_DEAD_FROZEN:
Robert Loved5488eb2009-06-10 15:30:59 -07001344 FCOE_DBG("CPU %x offline: Remove Rx thread\n", cpu);
Robert Love8976f422009-03-17 11:41:46 -07001345 fcoe_percpu_thread_destroy(cpu);
1346 break;
1347 default:
1348 break;
1349 }
1350 return NOTIFY_OK;
1351}
1352
Robert Love8976f422009-03-17 11:41:46 -07001353/**
Kiran Patil064287e2011-06-20 16:59:20 -07001354 * fcoe_select_cpu() - Selects CPU to handle post-processing of incoming
1355 * command.
Kiran Patil064287e2011-06-20 16:59:20 -07001356 *
Vasu Devd2722812011-07-27 15:11:10 -07001357 * This routine selects next CPU based on cpumask to distribute
1358 * incoming requests in round robin.
Kiran Patil064287e2011-06-20 16:59:20 -07001359 *
Vasu Devd2722812011-07-27 15:11:10 -07001360 * Returns: int CPU number
Kiran Patil064287e2011-06-20 16:59:20 -07001361 */
Vasu Devd2722812011-07-27 15:11:10 -07001362static inline unsigned int fcoe_select_cpu(void)
Kiran Patil064287e2011-06-20 16:59:20 -07001363{
1364 static unsigned int selected_cpu;
1365
Vasu Devd2722812011-07-27 15:11:10 -07001366 selected_cpu = cpumask_next(selected_cpu, cpu_online_mask);
1367 if (selected_cpu >= nr_cpu_ids)
1368 selected_cpu = cpumask_first(cpu_online_mask);
1369
Kiran Patil064287e2011-06-20 16:59:20 -07001370 return selected_cpu;
1371}
1372
1373/**
Robert Love1875f272009-11-03 11:47:50 -08001374 * fcoe_rcv() - Receive packets from a net device
1375 * @skb: The received packet
1376 * @netdev: The net device that the packet was received on
1377 * @ptype: The packet type context
1378 * @olddev: The last device net device
Robert Love85b4aa42008-12-09 15:10:24 -08001379 *
Robert Love1875f272009-11-03 11:47:50 -08001380 * This routine is called by NET_RX_SOFTIRQ. It receives a packet, builds a
1381 * FC frame and passes the frame to libfc.
Robert Love85b4aa42008-12-09 15:10:24 -08001382 *
1383 * Returns: 0 for success
Robert Love34f42a02009-02-27 10:55:45 -08001384 */
Bart Van Assche7c9c6842012-01-13 17:26:25 -08001385static int fcoe_rcv(struct sk_buff *skb, struct net_device *netdev,
Robert Love85b4aa42008-12-09 15:10:24 -08001386 struct packet_type *ptype, struct net_device *olddev)
1387{
Robert Love1875f272009-11-03 11:47:50 -08001388 struct fc_lport *lport;
Robert Love85b4aa42008-12-09 15:10:24 -08001389 struct fcoe_rcv_info *fr;
Chris Leech259ad852009-08-25 13:59:41 -07001390 struct fcoe_interface *fcoe;
Robert Love85b4aa42008-12-09 15:10:24 -08001391 struct fc_frame_header *fh;
Robert Love85b4aa42008-12-09 15:10:24 -08001392 struct fcoe_percpu_s *fps;
Vasu Dev519e5132010-07-20 15:19:32 -07001393 struct ethhdr *eh;
Vasu Devb2f00912009-08-25 13:58:53 -07001394 unsigned int cpu;
Robert Love85b4aa42008-12-09 15:10:24 -08001395
Chris Leech259ad852009-08-25 13:59:41 -07001396 fcoe = container_of(ptype, struct fcoe_interface, fcoe_packet_type);
Robert Love1875f272009-11-03 11:47:50 -08001397 lport = fcoe->ctlr.lp;
1398 if (unlikely(!lport)) {
1399 FCOE_NETDEV_DBG(netdev, "Cannot find hba structure");
Robert Love85b4aa42008-12-09 15:10:24 -08001400 goto err2;
1401 }
Robert Love1875f272009-11-03 11:47:50 -08001402 if (!lport->link_up)
Joe Eykholt97c83892009-03-17 11:42:40 -07001403 goto err2;
Robert Love85b4aa42008-12-09 15:10:24 -08001404
Robert Love1875f272009-11-03 11:47:50 -08001405 FCOE_NETDEV_DBG(netdev, "skb_info: len:%d data_len:%d head:%p "
Robert Loved5488eb2009-06-10 15:30:59 -07001406 "data:%p tail:%p end:%p sum:%d dev:%s",
1407 skb->len, skb->data_len, skb->head, skb->data,
1408 skb_tail_pointer(skb), skb_end_pointer(skb),
1409 skb->csum, skb->dev ? skb->dev->name : "<NULL>");
Robert Love85b4aa42008-12-09 15:10:24 -08001410
Vasu Dev519e5132010-07-20 15:19:32 -07001411 eh = eth_hdr(skb);
Vasu Dev519e5132010-07-20 15:19:32 -07001412
1413 if (is_fip_mode(&fcoe->ctlr) &&
1414 compare_ether_addr(eh->h_source, fcoe->ctlr.dest_addr)) {
1415 FCOE_NETDEV_DBG(netdev, "wrong source mac address:%pM\n",
1416 eh->h_source);
Robert Love85b4aa42008-12-09 15:10:24 -08001417 goto err;
1418 }
1419
1420 /*
1421 * Check for minimum frame length, and make sure required FCoE
1422 * and FC headers are pulled into the linear data area.
1423 */
1424 if (unlikely((skb->len < FCOE_MIN_FRAME) ||
Robert Love1875f272009-11-03 11:47:50 -08001425 !pskb_may_pull(skb, FCOE_HEADER_LEN)))
Robert Love85b4aa42008-12-09 15:10:24 -08001426 goto err;
1427
1428 skb_set_transport_header(skb, sizeof(struct fcoe_hdr));
1429 fh = (struct fc_frame_header *) skb_transport_header(skb);
1430
Robert Love0ee31cb2010-10-08 17:12:46 -07001431 if (ntoh24(&eh->h_dest[3]) != ntoh24(fh->fh_d_id)) {
1432 FCOE_NETDEV_DBG(netdev, "FC frame d_id mismatch with MAC:%pM\n",
1433 eh->h_dest);
1434 goto err;
1435 }
1436
Robert Love85b4aa42008-12-09 15:10:24 -08001437 fr = fcoe_dev_from_skb(skb);
Robert Love1875f272009-11-03 11:47:50 -08001438 fr->fr_dev = lport;
Robert Love5e5e92d2009-03-17 11:41:35 -07001439
Robert Love85b4aa42008-12-09 15:10:24 -08001440 /*
Vasu Devb2f00912009-08-25 13:58:53 -07001441 * In case the incoming frame's exchange is originated from
1442 * the initiator, then received frame's exchange id is ANDed
1443 * with fc_cpu_mask bits to get the same cpu on which exchange
Vasu Devd2722812011-07-27 15:11:10 -07001444 * was originated, otherwise select cpu using rx exchange id
1445 * or fcoe_select_cpu().
Robert Love85b4aa42008-12-09 15:10:24 -08001446 */
Vasu Devb2f00912009-08-25 13:58:53 -07001447 if (ntoh24(fh->fh_f_ctl) & FC_FC_EX_CTX)
1448 cpu = ntohs(fh->fh_ox_id) & fc_cpu_mask;
Kiran Patil064287e2011-06-20 16:59:20 -07001449 else {
Vasu Devd2722812011-07-27 15:11:10 -07001450 if (ntohs(fh->fh_rx_id) == FC_XID_UNKNOWN)
1451 cpu = fcoe_select_cpu();
1452 else
Kiran Patil29bdd2b2011-06-20 16:59:25 -07001453 cpu = ntohs(fh->fh_rx_id) & fc_cpu_mask;
Kiran Patil064287e2011-06-20 16:59:20 -07001454 }
Vasu Dev324f6672011-07-27 15:10:39 -07001455
1456 if (cpu >= nr_cpu_ids)
1457 goto err;
1458
Robert Love8976f422009-03-17 11:41:46 -07001459 fps = &per_cpu(fcoe_percpu, cpu);
Robert Love85b4aa42008-12-09 15:10:24 -08001460 spin_lock_bh(&fps->fcoe_rx_list.lock);
Robert Love8976f422009-03-17 11:41:46 -07001461 if (unlikely(!fps->thread)) {
1462 /*
1463 * The targeted CPU is not ready, let's target
1464 * the first CPU now. For non-SMP systems this
1465 * will check the same CPU twice.
1466 */
Robert Love1875f272009-11-03 11:47:50 -08001467 FCOE_NETDEV_DBG(netdev, "CPU is online, but no receive thread "
Robert Loved5488eb2009-06-10 15:30:59 -07001468 "ready for incoming skb- using first online "
1469 "CPU.\n");
Robert Love8976f422009-03-17 11:41:46 -07001470
1471 spin_unlock_bh(&fps->fcoe_rx_list.lock);
Rusty Russell69571772009-12-17 11:43:14 -06001472 cpu = cpumask_first(cpu_online_mask);
Robert Love8976f422009-03-17 11:41:46 -07001473 fps = &per_cpu(fcoe_percpu, cpu);
1474 spin_lock_bh(&fps->fcoe_rx_list.lock);
1475 if (!fps->thread) {
1476 spin_unlock_bh(&fps->fcoe_rx_list.lock);
1477 goto err;
1478 }
1479 }
1480
1481 /*
1482 * We now have a valid CPU that we're targeting for
1483 * this skb. We also have this receive thread locked,
1484 * so we're free to queue skbs into it's queue.
1485 */
Robert Love85b4aa42008-12-09 15:10:24 -08001486
Chris Leech859b7b62009-11-20 14:54:47 -08001487 /* If this is a SCSI-FCP frame, and this is already executing on the
1488 * correct CPU, and the queue for this CPU is empty, then go ahead
1489 * and process the frame directly in the softirq context.
1490 * This lets us process completions without context switching from the
1491 * NET_RX softirq, to our receive processing thread, and then back to
1492 * BLOCK softirq context.
1493 */
1494 if (fh->fh_type == FC_TYPE_FCP &&
1495 cpu == smp_processor_id() &&
1496 skb_queue_empty(&fps->fcoe_rx_list)) {
1497 spin_unlock_bh(&fps->fcoe_rx_list.lock);
1498 fcoe_recv_frame(skb);
1499 } else {
1500 __skb_queue_tail(&fps->fcoe_rx_list, skb);
1501 if (fps->fcoe_rx_list.qlen == 1)
1502 wake_up_process(fps->thread);
1503 spin_unlock_bh(&fps->fcoe_rx_list.lock);
1504 }
Robert Love85b4aa42008-12-09 15:10:24 -08001505
1506 return 0;
1507err:
Joe Eykholtf018b732010-03-12 16:08:55 -08001508 per_cpu_ptr(lport->dev_stats, get_cpu())->ErrorFrames++;
1509 put_cpu();
Robert Love85b4aa42008-12-09 15:10:24 -08001510err2:
1511 kfree_skb(skb);
1512 return -1;
1513}
Robert Love85b4aa42008-12-09 15:10:24 -08001514
1515/**
Bhanu Prakash Gollapudi8597ae82011-01-28 16:05:37 -08001516 * fcoe_alloc_paged_crc_eof() - Allocate a page to be used for the trailer CRC
Robert Love1875f272009-11-03 11:47:50 -08001517 * @skb: The packet to be transmitted
1518 * @tlen: The total length of the trailer
1519 *
Robert Love85b4aa42008-12-09 15:10:24 -08001520 * Returns: 0 for success
Robert Love34f42a02009-02-27 10:55:45 -08001521 */
Bhanu Prakash Gollapudi8597ae82011-01-28 16:05:37 -08001522static int fcoe_alloc_paged_crc_eof(struct sk_buff *skb, int tlen)
Robert Love85b4aa42008-12-09 15:10:24 -08001523{
1524 struct fcoe_percpu_s *fps;
Bhanu Prakash Gollapudi8597ae82011-01-28 16:05:37 -08001525 int rc;
Robert Love85b4aa42008-12-09 15:10:24 -08001526
Robert Love5e5e92d2009-03-17 11:41:35 -07001527 fps = &get_cpu_var(fcoe_percpu);
Bhanu Prakash Gollapudi8597ae82011-01-28 16:05:37 -08001528 rc = fcoe_get_paged_crc_eof(skb, tlen, fps);
Robert Love5e5e92d2009-03-17 11:41:35 -07001529 put_cpu_var(fcoe_percpu);
Robert Love85b4aa42008-12-09 15:10:24 -08001530
Bhanu Prakash Gollapudi8597ae82011-01-28 16:05:37 -08001531 return rc;
Robert Love85b4aa42008-12-09 15:10:24 -08001532}
Robert Love85b4aa42008-12-09 15:10:24 -08001533
1534/**
Robert Love1875f272009-11-03 11:47:50 -08001535 * fcoe_xmit() - Transmit a FCoE frame
1536 * @lport: The local port that the frame is to be transmitted for
1537 * @fp: The frame to be transmitted
Robert Love85b4aa42008-12-09 15:10:24 -08001538 *
Robert Love1875f272009-11-03 11:47:50 -08001539 * Return: 0 for success
Robert Love34f42a02009-02-27 10:55:45 -08001540 */
Bart Van Assche7c9c6842012-01-13 17:26:25 -08001541static int fcoe_xmit(struct fc_lport *lport, struct fc_frame *fp)
Robert Love85b4aa42008-12-09 15:10:24 -08001542{
Vasu Dev4bb6b512009-05-06 10:52:34 -07001543 int wlen;
Robert Love85b4aa42008-12-09 15:10:24 -08001544 u32 crc;
1545 struct ethhdr *eh;
1546 struct fcoe_crc_eof *cp;
1547 struct sk_buff *skb;
1548 struct fcoe_dev_stats *stats;
1549 struct fc_frame_header *fh;
1550 unsigned int hlen; /* header length implies the version */
1551 unsigned int tlen; /* trailer length */
1552 unsigned int elen; /* eth header, may include vlan */
Robert Love1875f272009-11-03 11:47:50 -08001553 struct fcoe_port *port = lport_priv(lport);
Bhanu Prakash Gollapudi8597ae82011-01-28 16:05:37 -08001554 struct fcoe_interface *fcoe = port->priv;
Robert Love85b4aa42008-12-09 15:10:24 -08001555 u8 sof, eof;
1556 struct fcoe_hdr *hp;
1557
1558 WARN_ON((fr_len(fp) % sizeof(u32)) != 0);
1559
Robert Love85b4aa42008-12-09 15:10:24 -08001560 fh = fc_frame_header_get(fp);
Joe Eykholt97c83892009-03-17 11:42:40 -07001561 skb = fp_skb(fp);
1562 wlen = skb->len / FCOE_WORD_TO_BYTE;
1563
Robert Love1875f272009-11-03 11:47:50 -08001564 if (!lport->link_up) {
Dan Carpenter3caf02e2009-04-21 16:27:25 -07001565 kfree_skb(skb);
Joe Eykholt97c83892009-03-17 11:42:40 -07001566 return 0;
Robert Love85b4aa42008-12-09 15:10:24 -08001567 }
1568
Joe Eykholt9860eeb2010-03-12 16:07:52 -08001569 if (unlikely(fh->fh_type == FC_TYPE_ELS) &&
Robert Love1875f272009-11-03 11:47:50 -08001570 fcoe_ctlr_els_send(&fcoe->ctlr, lport, skb))
Joe Eykholt97c83892009-03-17 11:42:40 -07001571 return 0;
1572
Robert Love85b4aa42008-12-09 15:10:24 -08001573 sof = fr_sof(fp);
1574 eof = fr_eof(fp);
1575
Vasu Dev4e57e1c2009-05-06 10:52:46 -07001576 elen = sizeof(struct ethhdr);
Robert Love85b4aa42008-12-09 15:10:24 -08001577 hlen = sizeof(struct fcoe_hdr);
1578 tlen = sizeof(struct fcoe_crc_eof);
1579 wlen = (skb->len - tlen + sizeof(crc)) / FCOE_WORD_TO_BYTE;
1580
1581 /* crc offload */
Robert Love1875f272009-11-03 11:47:50 -08001582 if (likely(lport->crc_offload)) {
Yi Zou39ca9a02009-02-27 14:07:15 -08001583 skb->ip_summed = CHECKSUM_PARTIAL;
Robert Love85b4aa42008-12-09 15:10:24 -08001584 skb->csum_start = skb_headroom(skb);
1585 skb->csum_offset = skb->len;
1586 crc = 0;
1587 } else {
1588 skb->ip_summed = CHECKSUM_NONE;
1589 crc = fcoe_fc_crc(fp);
1590 }
1591
Chris Leech014f5c32009-08-25 13:59:30 -07001592 /* copy port crc and eof to the skb buff */
Robert Love85b4aa42008-12-09 15:10:24 -08001593 if (skb_is_nonlinear(skb)) {
1594 skb_frag_t *frag;
Bhanu Prakash Gollapudi8597ae82011-01-28 16:05:37 -08001595 if (fcoe_alloc_paged_crc_eof(skb, tlen)) {
Roel Kluine9041582009-02-27 10:56:22 -08001596 kfree_skb(skb);
Robert Love85b4aa42008-12-09 15:10:24 -08001597 return -ENOMEM;
1598 }
1599 frag = &skb_shinfo(skb)->frags[skb_shinfo(skb)->nr_frags - 1];
Ian Campbell165c68d2011-08-24 22:28:15 +00001600 cp = kmap_atomic(skb_frag_page(frag), KM_SKB_DATA_SOFTIRQ)
Robert Love85b4aa42008-12-09 15:10:24 -08001601 + frag->page_offset;
1602 } else {
1603 cp = (struct fcoe_crc_eof *)skb_put(skb, tlen);
1604 }
1605
1606 memset(cp, 0, sizeof(*cp));
1607 cp->fcoe_eof = eof;
1608 cp->fcoe_crc32 = cpu_to_le32(~crc);
1609
1610 if (skb_is_nonlinear(skb)) {
1611 kunmap_atomic(cp, KM_SKB_DATA_SOFTIRQ);
1612 cp = NULL;
1613 }
1614
Chris Leech014f5c32009-08-25 13:59:30 -07001615 /* adjust skb network/transport offsets to match mac/fcoe/port */
Robert Love85b4aa42008-12-09 15:10:24 -08001616 skb_push(skb, elen + hlen);
1617 skb_reset_mac_header(skb);
1618 skb_reset_network_header(skb);
1619 skb->mac_len = elen;
Yi Zou211c7382009-02-27 14:06:37 -08001620 skb->protocol = htons(ETH_P_FCOE);
john fastabend6f6c2aa2011-11-18 13:35:56 -08001621 skb->priority = port->priority;
1622
Vasu Devd1483bb2011-09-27 21:38:13 -07001623 if (fcoe->netdev->priv_flags & IFF_802_1Q_VLAN &&
1624 fcoe->realdev->features & NETIF_F_HW_VLAN_TX) {
1625 skb->vlan_tci = VLAN_TAG_PRESENT |
1626 vlan_dev_vlan_id(fcoe->netdev);
1627 skb->dev = fcoe->realdev;
1628 } else
1629 skb->dev = fcoe->netdev;
Robert Love85b4aa42008-12-09 15:10:24 -08001630
1631 /* fill up mac and fcoe headers */
1632 eh = eth_hdr(skb);
1633 eh->h_proto = htons(ETH_P_FCOE);
Joe Eykholtcd229e42010-07-20 15:20:40 -07001634 memcpy(eh->h_dest, fcoe->ctlr.dest_addr, ETH_ALEN);
Chris Leech3fe9a0b2009-08-25 13:59:46 -07001635 if (fcoe->ctlr.map_dest)
Joe Eykholtcd229e42010-07-20 15:20:40 -07001636 memcpy(eh->h_dest + 3, fh->fh_d_id, 3);
Robert Love85b4aa42008-12-09 15:10:24 -08001637
Chris Leech3fe9a0b2009-08-25 13:59:46 -07001638 if (unlikely(fcoe->ctlr.flogi_oxid != FC_XID_UNKNOWN))
1639 memcpy(eh->h_source, fcoe->ctlr.ctl_src_addr, ETH_ALEN);
Robert Love85b4aa42008-12-09 15:10:24 -08001640 else
Chris Leech11b56182009-11-03 11:46:29 -08001641 memcpy(eh->h_source, port->data_src_addr, ETH_ALEN);
Robert Love85b4aa42008-12-09 15:10:24 -08001642
1643 hp = (struct fcoe_hdr *)(eh + 1);
1644 memset(hp, 0, sizeof(*hp));
1645 if (FC_FCOE_VER)
1646 FC_FCOE_ENCAPS_VER(hp, FC_FCOE_VER);
1647 hp->fcoe_sof = sof;
1648
Yi Zou39ca9a02009-02-27 14:07:15 -08001649 /* fcoe lso, mss is in max_payload which is non-zero for FCP data */
Robert Love1875f272009-11-03 11:47:50 -08001650 if (lport->seq_offload && fr_max_payload(fp)) {
Yi Zou39ca9a02009-02-27 14:07:15 -08001651 skb_shinfo(skb)->gso_type = SKB_GSO_FCOE;
1652 skb_shinfo(skb)->gso_size = fr_max_payload(fp);
1653 } else {
1654 skb_shinfo(skb)->gso_type = 0;
1655 skb_shinfo(skb)->gso_size = 0;
1656 }
Robert Love85b4aa42008-12-09 15:10:24 -08001657 /* update tx stats: regardless if LLD fails */
Joe Eykholtf018b732010-03-12 16:08:55 -08001658 stats = per_cpu_ptr(lport->dev_stats, get_cpu());
Robert Love582b45b2009-03-31 15:51:50 -07001659 stats->TxFrames++;
1660 stats->TxWords += wlen;
Joe Eykholtf018b732010-03-12 16:08:55 -08001661 put_cpu();
Robert Love85b4aa42008-12-09 15:10:24 -08001662
1663 /* send down to lld */
Robert Love1875f272009-11-03 11:47:50 -08001664 fr_dev(fp) = lport;
Vasu Dev980f5152011-07-27 15:11:05 -07001665 fcoe_port_send(port, skb);
Robert Love85b4aa42008-12-09 15:10:24 -08001666 return 0;
1667}
Robert Love85b4aa42008-12-09 15:10:24 -08001668
Robert Love34f42a02009-02-27 10:55:45 -08001669/**
Robert Love1875f272009-11-03 11:47:50 -08001670 * fcoe_percpu_flush_done() - Indicate per-CPU queue flush completion
1671 * @skb: The completed skb (argument required by destructor)
Joe Eykholte7a51992009-08-25 14:04:08 -07001672 */
1673static void fcoe_percpu_flush_done(struct sk_buff *skb)
1674{
1675 complete(&fcoe_flush_completion);
1676}
1677
1678/**
Vasu Dev52ee8322011-01-28 16:03:52 -08001679 * fcoe_filter_frames() - filter out bad fcoe frames, i.e. bad CRC
1680 * @lport: The local port the frame was received on
1681 * @fp: The received frame
1682 *
1683 * Return: 0 on passing filtering checks
1684 */
1685static inline int fcoe_filter_frames(struct fc_lport *lport,
1686 struct fc_frame *fp)
1687{
1688 struct fcoe_interface *fcoe;
1689 struct fc_frame_header *fh;
1690 struct sk_buff *skb = (struct sk_buff *)fp;
1691 struct fcoe_dev_stats *stats;
1692
1693 /*
1694 * We only check CRC if no offload is available and if it is
1695 * it's solicited data, in which case, the FCP layer would
1696 * check it during the copy.
1697 */
1698 if (lport->crc_offload && skb->ip_summed == CHECKSUM_UNNECESSARY)
1699 fr_flags(fp) &= ~FCPHF_CRC_UNCHECKED;
1700 else
1701 fr_flags(fp) |= FCPHF_CRC_UNCHECKED;
1702
1703 fh = (struct fc_frame_header *) skb_transport_header(skb);
1704 fh = fc_frame_header_get(fp);
1705 if (fh->fh_r_ctl == FC_RCTL_DD_SOL_DATA && fh->fh_type == FC_TYPE_FCP)
1706 return 0;
1707
Bhanu Prakash Gollapudi8597ae82011-01-28 16:05:37 -08001708 fcoe = ((struct fcoe_port *)lport_priv(lport))->priv;
Vasu Dev52ee8322011-01-28 16:03:52 -08001709 if (is_fip_mode(&fcoe->ctlr) && fc_frame_payload_op(fp) == ELS_LOGO &&
1710 ntoh24(fh->fh_s_id) == FC_FID_FLOGI) {
1711 FCOE_DBG("fcoe: dropping FCoE lport LOGO in fip mode\n");
1712 return -EINVAL;
1713 }
1714
Dan Carpenterf2f96d22011-02-25 15:03:23 -08001715 if (!(fr_flags(fp) & FCPHF_CRC_UNCHECKED) ||
Vasu Dev52ee8322011-01-28 16:03:52 -08001716 le32_to_cpu(fr_crc(fp)) == ~crc32(~0, skb->data, skb->len)) {
1717 fr_flags(fp) &= ~FCPHF_CRC_UNCHECKED;
1718 return 0;
1719 }
1720
1721 stats = per_cpu_ptr(lport->dev_stats, get_cpu());
1722 stats->InvalidCRCCount++;
1723 if (stats->InvalidCRCCount < 5)
1724 printk(KERN_WARNING "fcoe: dropping frame with CRC error\n");
Thomas Gleixner7e1e7ea2011-11-11 20:52:01 +01001725 put_cpu();
Vasu Dev52ee8322011-01-28 16:03:52 -08001726 return -EINVAL;
1727}
1728
1729/**
Chris Leech859b7b62009-11-20 14:54:47 -08001730 * fcoe_recv_frame() - process a single received frame
1731 * @skb: frame to process
1732 */
1733static void fcoe_recv_frame(struct sk_buff *skb)
1734{
1735 u32 fr_len;
1736 struct fc_lport *lport;
1737 struct fcoe_rcv_info *fr;
1738 struct fcoe_dev_stats *stats;
Chris Leech859b7b62009-11-20 14:54:47 -08001739 struct fcoe_crc_eof crc_eof;
1740 struct fc_frame *fp;
Chris Leech859b7b62009-11-20 14:54:47 -08001741 struct fcoe_port *port;
1742 struct fcoe_hdr *hp;
1743
1744 fr = fcoe_dev_from_skb(skb);
1745 lport = fr->fr_dev;
1746 if (unlikely(!lport)) {
1747 if (skb->destructor != fcoe_percpu_flush_done)
1748 FCOE_NETDEV_DBG(skb->dev, "NULL lport in skb");
1749 kfree_skb(skb);
1750 return;
1751 }
1752
1753 FCOE_NETDEV_DBG(skb->dev, "skb_info: len:%d data_len:%d "
1754 "head:%p data:%p tail:%p end:%p sum:%d dev:%s",
1755 skb->len, skb->data_len,
1756 skb->head, skb->data, skb_tail_pointer(skb),
1757 skb_end_pointer(skb), skb->csum,
1758 skb->dev ? skb->dev->name : "<NULL>");
1759
Chris Leech859b7b62009-11-20 14:54:47 -08001760 port = lport_priv(lport);
Robert Lovef1633012011-12-16 14:24:49 -08001761 skb_linearize(skb); /* check for skb_is_nonlinear is within skb_linearize */
Chris Leech859b7b62009-11-20 14:54:47 -08001762
1763 /*
1764 * Frame length checks and setting up the header pointers
1765 * was done in fcoe_rcv already.
1766 */
1767 hp = (struct fcoe_hdr *) skb_network_header(skb);
Chris Leech859b7b62009-11-20 14:54:47 -08001768
Joe Eykholtf018b732010-03-12 16:08:55 -08001769 stats = per_cpu_ptr(lport->dev_stats, get_cpu());
Chris Leech859b7b62009-11-20 14:54:47 -08001770 if (unlikely(FC_FCOE_DECAPS_VER(hp) != FC_FCOE_VER)) {
1771 if (stats->ErrorFrames < 5)
1772 printk(KERN_WARNING "fcoe: FCoE version "
1773 "mismatch: The frame has "
1774 "version %x, but the "
1775 "initiator supports version "
1776 "%x\n", FC_FCOE_DECAPS_VER(hp),
1777 FC_FCOE_VER);
Joe Eykholtf018b732010-03-12 16:08:55 -08001778 goto drop;
Chris Leech859b7b62009-11-20 14:54:47 -08001779 }
1780
1781 skb_pull(skb, sizeof(struct fcoe_hdr));
1782 fr_len = skb->len - sizeof(struct fcoe_crc_eof);
1783
1784 stats->RxFrames++;
1785 stats->RxWords += fr_len / FCOE_WORD_TO_BYTE;
1786
1787 fp = (struct fc_frame *)skb;
1788 fc_frame_init(fp);
1789 fr_dev(fp) = lport;
1790 fr_sof(fp) = hp->fcoe_sof;
1791
1792 /* Copy out the CRC and EOF trailer for access */
Joe Eykholtf018b732010-03-12 16:08:55 -08001793 if (skb_copy_bits(skb, fr_len, &crc_eof, sizeof(crc_eof)))
1794 goto drop;
Chris Leech859b7b62009-11-20 14:54:47 -08001795 fr_eof(fp) = crc_eof.fcoe_eof;
1796 fr_crc(fp) = crc_eof.fcoe_crc32;
Joe Eykholtf018b732010-03-12 16:08:55 -08001797 if (pskb_trim(skb, fr_len))
1798 goto drop;
Chris Leech859b7b62009-11-20 14:54:47 -08001799
Vasu Dev52ee8322011-01-28 16:03:52 -08001800 if (!fcoe_filter_frames(lport, fp)) {
1801 put_cpu();
1802 fc_exch_recv(lport, fp);
1803 return;
Chris Leech859b7b62009-11-20 14:54:47 -08001804 }
Joe Eykholtf018b732010-03-12 16:08:55 -08001805drop:
1806 stats->ErrorFrames++;
1807 put_cpu();
1808 kfree_skb(skb);
Chris Leech859b7b62009-11-20 14:54:47 -08001809}
1810
1811/**
Robert Love1875f272009-11-03 11:47:50 -08001812 * fcoe_percpu_receive_thread() - The per-CPU packet receive thread
1813 * @arg: The per-CPU context
Robert Love85b4aa42008-12-09 15:10:24 -08001814 *
1815 * Return: 0 for success
Robert Love85b4aa42008-12-09 15:10:24 -08001816 */
Bart Van Assche7c9c6842012-01-13 17:26:25 -08001817static int fcoe_percpu_receive_thread(void *arg)
Robert Love85b4aa42008-12-09 15:10:24 -08001818{
1819 struct fcoe_percpu_s *p = arg;
Robert Love85b4aa42008-12-09 15:10:24 -08001820 struct sk_buff *skb;
Robert Love85b4aa42008-12-09 15:10:24 -08001821
Robert Love4469c192009-02-27 10:56:38 -08001822 set_user_nice(current, -20);
Robert Love85b4aa42008-12-09 15:10:24 -08001823
1824 while (!kthread_should_stop()) {
1825
1826 spin_lock_bh(&p->fcoe_rx_list.lock);
1827 while ((skb = __skb_dequeue(&p->fcoe_rx_list)) == NULL) {
1828 set_current_state(TASK_INTERRUPTIBLE);
1829 spin_unlock_bh(&p->fcoe_rx_list.lock);
1830 schedule();
1831 set_current_state(TASK_RUNNING);
1832 if (kthread_should_stop())
1833 return 0;
1834 spin_lock_bh(&p->fcoe_rx_list.lock);
1835 }
1836 spin_unlock_bh(&p->fcoe_rx_list.lock);
Chris Leech859b7b62009-11-20 14:54:47 -08001837 fcoe_recv_frame(skb);
Robert Love85b4aa42008-12-09 15:10:24 -08001838 }
1839 return 0;
1840}
1841
1842/**
Robert Love1875f272009-11-03 11:47:50 -08001843 * fcoe_dev_setup() - Setup the link change notification interface
Robert Love34f42a02009-02-27 10:55:45 -08001844 */
Randy Dunlapb0d428a2009-04-27 21:49:31 -07001845static void fcoe_dev_setup(void)
Robert Love85b4aa42008-12-09 15:10:24 -08001846{
john fastabend6f6c2aa2011-11-18 13:35:56 -08001847 register_dcbevent_notifier(&dcb_notifier);
Robert Love85b4aa42008-12-09 15:10:24 -08001848 register_netdevice_notifier(&fcoe_notifier);
1849}
1850
1851/**
Robert Love1875f272009-11-03 11:47:50 -08001852 * fcoe_dev_cleanup() - Cleanup the link change notification interface
Robert Love34f42a02009-02-27 10:55:45 -08001853 */
Robert Love85b4aa42008-12-09 15:10:24 -08001854static void fcoe_dev_cleanup(void)
1855{
john fastabend6f6c2aa2011-11-18 13:35:56 -08001856 unregister_dcbevent_notifier(&dcb_notifier);
Robert Love85b4aa42008-12-09 15:10:24 -08001857 unregister_netdevice_notifier(&fcoe_notifier);
1858}
1859
john fastabend6f6c2aa2011-11-18 13:35:56 -08001860static struct fcoe_interface *
1861fcoe_hostlist_lookup_realdev_port(struct net_device *netdev)
1862{
1863 struct fcoe_interface *fcoe;
1864 struct net_device *real_dev;
1865
1866 list_for_each_entry(fcoe, &fcoe_hostlist, list) {
1867 if (fcoe->netdev->priv_flags & IFF_802_1Q_VLAN)
1868 real_dev = vlan_dev_real_dev(fcoe->netdev);
1869 else
1870 real_dev = fcoe->netdev;
1871
1872 if (netdev == real_dev)
1873 return fcoe;
1874 }
1875 return NULL;
1876}
1877
1878static int fcoe_dcb_app_notification(struct notifier_block *notifier,
1879 ulong event, void *ptr)
1880{
1881 struct dcb_app_type *entry = ptr;
1882 struct fcoe_interface *fcoe;
1883 struct net_device *netdev;
1884 struct fcoe_port *port;
1885 int prio;
1886
1887 if (entry->app.selector != DCB_APP_IDTYPE_ETHTYPE)
1888 return NOTIFY_OK;
1889
1890 netdev = dev_get_by_index(&init_net, entry->ifindex);
1891 if (!netdev)
1892 return NOTIFY_OK;
1893
1894 fcoe = fcoe_hostlist_lookup_realdev_port(netdev);
1895 dev_put(netdev);
1896 if (!fcoe)
1897 return NOTIFY_OK;
1898
1899 if (entry->dcbx & DCB_CAP_DCBX_VER_CEE)
1900 prio = ffs(entry->app.priority) - 1;
1901 else
1902 prio = entry->app.priority;
1903
1904 if (prio < 0)
1905 return NOTIFY_OK;
1906
1907 if (entry->app.protocol == ETH_P_FIP ||
1908 entry->app.protocol == ETH_P_FCOE)
1909 fcoe->ctlr.priority = prio;
1910
1911 if (entry->app.protocol == ETH_P_FCOE) {
1912 port = lport_priv(fcoe->ctlr.lp);
1913 port->priority = prio;
1914 }
1915
1916 return NOTIFY_OK;
1917}
1918
Robert Love85b4aa42008-12-09 15:10:24 -08001919/**
Robert Love1875f272009-11-03 11:47:50 -08001920 * fcoe_device_notification() - Handler for net device events
1921 * @notifier: The context of the notification
1922 * @event: The type of event
1923 * @ptr: The net device that the event was on
Robert Love85b4aa42008-12-09 15:10:24 -08001924 *
Robert Love1875f272009-11-03 11:47:50 -08001925 * This function is called by the Ethernet driver in case of link change event.
Robert Love85b4aa42008-12-09 15:10:24 -08001926 *
1927 * Returns: 0 for success
Robert Love34f42a02009-02-27 10:55:45 -08001928 */
Robert Love85b4aa42008-12-09 15:10:24 -08001929static int fcoe_device_notification(struct notifier_block *notifier,
1930 ulong event, void *ptr)
1931{
Robert Love1875f272009-11-03 11:47:50 -08001932 struct fc_lport *lport = NULL;
Vasu Dev1d1b88d2009-07-29 17:05:45 -07001933 struct net_device *netdev = ptr;
Chris Leech014f5c32009-08-25 13:59:30 -07001934 struct fcoe_interface *fcoe;
Chris Leech2e70e242009-08-25 14:00:23 -07001935 struct fcoe_port *port;
Robert Love85b4aa42008-12-09 15:10:24 -08001936 struct fcoe_dev_stats *stats;
Joe Eykholt97c83892009-03-17 11:42:40 -07001937 u32 link_possible = 1;
Robert Love85b4aa42008-12-09 15:10:24 -08001938 u32 mfs;
1939 int rc = NOTIFY_OK;
1940
Chris Leech014f5c32009-08-25 13:59:30 -07001941 list_for_each_entry(fcoe, &fcoe_hostlist, list) {
Chris Leech25024982009-08-25 13:59:35 -07001942 if (fcoe->netdev == netdev) {
Robert Love1875f272009-11-03 11:47:50 -08001943 lport = fcoe->ctlr.lp;
Robert Love85b4aa42008-12-09 15:10:24 -08001944 break;
1945 }
1946 }
Robert Love1875f272009-11-03 11:47:50 -08001947 if (!lport) {
Robert Love85b4aa42008-12-09 15:10:24 -08001948 rc = NOTIFY_DONE;
1949 goto out;
1950 }
1951
Robert Love85b4aa42008-12-09 15:10:24 -08001952 switch (event) {
1953 case NETDEV_DOWN:
1954 case NETDEV_GOING_DOWN:
Joe Eykholt97c83892009-03-17 11:42:40 -07001955 link_possible = 0;
Robert Love85b4aa42008-12-09 15:10:24 -08001956 break;
1957 case NETDEV_UP:
1958 case NETDEV_CHANGE:
Robert Love85b4aa42008-12-09 15:10:24 -08001959 break;
1960 case NETDEV_CHANGEMTU:
Yi Zou7221d7e2009-10-21 16:27:52 -07001961 if (netdev->features & NETIF_F_FCOE_MTU)
1962 break;
Vasu Dev1d1b88d2009-07-29 17:05:45 -07001963 mfs = netdev->mtu - (sizeof(struct fcoe_hdr) +
1964 sizeof(struct fcoe_crc_eof));
Robert Love85b4aa42008-12-09 15:10:24 -08001965 if (mfs >= FC_MIN_MAX_FRAME)
Robert Love1875f272009-11-03 11:47:50 -08001966 fc_set_mfs(lport, mfs);
Robert Love85b4aa42008-12-09 15:10:24 -08001967 break;
1968 case NETDEV_REGISTER:
1969 break;
Chris Leech2e70e242009-08-25 14:00:23 -07001970 case NETDEV_UNREGISTER:
1971 list_del(&fcoe->list);
1972 port = lport_priv(fcoe->ctlr.lp);
Tejun Heo2ca32b42011-01-28 16:05:32 -08001973 queue_work(fcoe_wq, &port->destroy_work);
Chris Leech2e70e242009-08-25 14:00:23 -07001974 goto out;
1975 break;
Yi Zou54a5b212010-07-20 15:21:17 -07001976 case NETDEV_FEAT_CHANGE:
1977 fcoe_netdev_features_change(lport, netdev);
1978 break;
Robert Love85b4aa42008-12-09 15:10:24 -08001979 default:
Vasu Dev1d1b88d2009-07-29 17:05:45 -07001980 FCOE_NETDEV_DBG(netdev, "Unknown event %ld "
Robert Loved5488eb2009-06-10 15:30:59 -07001981 "from netdev netlink\n", event);
Robert Love85b4aa42008-12-09 15:10:24 -08001982 }
Robert Love5e4f8fe2010-05-07 15:18:35 -07001983
1984 fcoe_link_speed_update(lport);
1985
Robert Love1875f272009-11-03 11:47:50 -08001986 if (link_possible && !fcoe_link_ok(lport))
Chris Leech3fe9a0b2009-08-25 13:59:46 -07001987 fcoe_ctlr_link_up(&fcoe->ctlr);
1988 else if (fcoe_ctlr_link_down(&fcoe->ctlr)) {
Joe Eykholtf018b732010-03-12 16:08:55 -08001989 stats = per_cpu_ptr(lport->dev_stats, get_cpu());
Joe Eykholt97c83892009-03-17 11:42:40 -07001990 stats->LinkFailureCount++;
Joe Eykholtf018b732010-03-12 16:08:55 -08001991 put_cpu();
Robert Love1875f272009-11-03 11:47:50 -08001992 fcoe_clean_pending_queue(lport);
Robert Love85b4aa42008-12-09 15:10:24 -08001993 }
1994out:
1995 return rc;
1996}
1997
1998/**
Vasu Dev55a66d32009-12-10 09:59:31 -08001999 * fcoe_disable() - Disables a FCoE interface
Yi Zou78a58242011-01-28 16:05:16 -08002000 * @netdev : The net_device object the Ethernet interface to create on
Vasu Dev55a66d32009-12-10 09:59:31 -08002001 *
Yi Zou78a58242011-01-28 16:05:16 -08002002 * Called from fcoe transport.
Vasu Dev55a66d32009-12-10 09:59:31 -08002003 *
2004 * Returns: 0 for success
2005 */
Yi Zou78a58242011-01-28 16:05:16 -08002006static int fcoe_disable(struct net_device *netdev)
Vasu Dev55a66d32009-12-10 09:59:31 -08002007{
2008 struct fcoe_interface *fcoe;
Vasu Dev55a66d32009-12-10 09:59:31 -08002009 int rc = 0;
2010
2011 mutex_lock(&fcoe_config_mutex);
Vasu Dev55a66d32009-12-10 09:59:31 -08002012
Robert Loveee5df622011-04-01 16:05:59 -07002013 rtnl_lock();
Vasu Dev55a66d32009-12-10 09:59:31 -08002014 fcoe = fcoe_hostlist_lookup_port(netdev);
2015 rtnl_unlock();
2016
Chris Leech9ee50e42010-04-09 14:22:23 -07002017 if (fcoe) {
Chris Leech9ee50e42010-04-09 14:22:23 -07002018 fcoe_ctlr_link_down(&fcoe->ctlr);
Vasu Dev9d4cbc02010-07-20 15:19:26 -07002019 fcoe_clean_pending_queue(fcoe->ctlr.lp);
Chris Leech9ee50e42010-04-09 14:22:23 -07002020 } else
Vasu Dev55a66d32009-12-10 09:59:31 -08002021 rc = -ENODEV;
2022
Vasu Dev55a66d32009-12-10 09:59:31 -08002023 mutex_unlock(&fcoe_config_mutex);
2024 return rc;
2025}
2026
2027/**
2028 * fcoe_enable() - Enables a FCoE interface
Yi Zou78a58242011-01-28 16:05:16 -08002029 * @netdev : The net_device object the Ethernet interface to create on
Vasu Dev55a66d32009-12-10 09:59:31 -08002030 *
Yi Zou78a58242011-01-28 16:05:16 -08002031 * Called from fcoe transport.
Vasu Dev55a66d32009-12-10 09:59:31 -08002032 *
2033 * Returns: 0 for success
2034 */
Yi Zou78a58242011-01-28 16:05:16 -08002035static int fcoe_enable(struct net_device *netdev)
Vasu Dev55a66d32009-12-10 09:59:31 -08002036{
2037 struct fcoe_interface *fcoe;
Vasu Dev55a66d32009-12-10 09:59:31 -08002038 int rc = 0;
2039
2040 mutex_lock(&fcoe_config_mutex);
Robert Loveee5df622011-04-01 16:05:59 -07002041 rtnl_lock();
Vasu Dev55a66d32009-12-10 09:59:31 -08002042 fcoe = fcoe_hostlist_lookup_port(netdev);
2043 rtnl_unlock();
2044
Vasu Dev9d4cbc02010-07-20 15:19:26 -07002045 if (!fcoe)
Vasu Dev55a66d32009-12-10 09:59:31 -08002046 rc = -ENODEV;
Vasu Dev9d4cbc02010-07-20 15:19:26 -07002047 else if (!fcoe_link_ok(fcoe->ctlr.lp))
2048 fcoe_ctlr_link_up(&fcoe->ctlr);
Vasu Dev55a66d32009-12-10 09:59:31 -08002049
Vasu Dev55a66d32009-12-10 09:59:31 -08002050 mutex_unlock(&fcoe_config_mutex);
2051 return rc;
2052}
2053
2054/**
Robert Love1875f272009-11-03 11:47:50 -08002055 * fcoe_destroy() - Destroy a FCoE interface
Yi Zou78a58242011-01-28 16:05:16 -08002056 * @netdev : The net_device object the Ethernet interface to create on
Robert Love1875f272009-11-03 11:47:50 -08002057 *
Yi Zou78a58242011-01-28 16:05:16 -08002058 * Called from fcoe transport
Robert Love85b4aa42008-12-09 15:10:24 -08002059 *
2060 * Returns: 0 for success
Robert Love34f42a02009-02-27 10:55:45 -08002061 */
Yi Zou78a58242011-01-28 16:05:16 -08002062static int fcoe_destroy(struct net_device *netdev)
Robert Love85b4aa42008-12-09 15:10:24 -08002063{
Chris Leech030f4e02009-08-25 14:00:02 -07002064 struct fcoe_interface *fcoe;
Vasu Devf04ca1b2011-04-01 16:06:45 -07002065 struct fc_lport *lport;
Neerav Parikhb2085a42011-06-20 16:59:51 -07002066 struct fcoe_port *port;
Mike Christie8eca3552009-10-21 16:27:44 -07002067 int rc = 0;
Robert Love85b4aa42008-12-09 15:10:24 -08002068
Chris Leechdfc1d0f2009-08-25 14:00:13 -07002069 mutex_lock(&fcoe_config_mutex);
Robert Loveee5df622011-04-01 16:05:59 -07002070 rtnl_lock();
Chris Leech2e70e242009-08-25 14:00:23 -07002071 fcoe = fcoe_hostlist_lookup_port(netdev);
2072 if (!fcoe) {
Robert Love85b4aa42008-12-09 15:10:24 -08002073 rc = -ENODEV;
Yi Zou78a58242011-01-28 16:05:16 -08002074 goto out_nodev;
Robert Love85b4aa42008-12-09 15:10:24 -08002075 }
Vasu Devf04ca1b2011-04-01 16:06:45 -07002076 lport = fcoe->ctlr.lp;
Neerav Parikhb2085a42011-06-20 16:59:51 -07002077 port = lport_priv(lport);
Yi Zou54a5b212010-07-20 15:21:17 -07002078 list_del(&fcoe->list);
Neerav Parikhb2085a42011-06-20 16:59:51 -07002079 queue_work(fcoe_wq, &port->destroy_work);
Robert Love85b4aa42008-12-09 15:10:24 -08002080out_nodev:
Neerav Parikhb2085a42011-06-20 16:59:51 -07002081 rtnl_unlock();
Chris Leechdfc1d0f2009-08-25 14:00:13 -07002082 mutex_unlock(&fcoe_config_mutex);
Robert Love85b4aa42008-12-09 15:10:24 -08002083 return rc;
2084}
2085
Robert Love1875f272009-11-03 11:47:50 -08002086/**
2087 * fcoe_destroy_work() - Destroy a FCoE port in a deferred work context
2088 * @work: Handle to the FCoE port to be destroyed
2089 */
Chris Leech2e70e242009-08-25 14:00:23 -07002090static void fcoe_destroy_work(struct work_struct *work)
2091{
2092 struct fcoe_port *port;
Neerav Parikhb2085a42011-06-20 16:59:51 -07002093 struct fcoe_interface *fcoe;
2094 int npiv = 0;
Chris Leech2e70e242009-08-25 14:00:23 -07002095
2096 port = container_of(work, struct fcoe_port, destroy_work);
2097 mutex_lock(&fcoe_config_mutex);
Neerav Parikhb2085a42011-06-20 16:59:51 -07002098
2099 /* set if this is an NPIV port */
2100 npiv = port->lport->vport ? 1 : 0;
2101
2102 fcoe = port->priv;
Chris Leech2e70e242009-08-25 14:00:23 -07002103 fcoe_if_destroy(port->lport);
Neerav Parikhb2085a42011-06-20 16:59:51 -07002104
2105 /* Do not tear down the fcoe interface for NPIV port */
Robert Love848e7d52011-08-25 12:40:47 -07002106 if (!npiv)
Neerav Parikhb2085a42011-06-20 16:59:51 -07002107 fcoe_interface_cleanup(fcoe);
Neerav Parikhb2085a42011-06-20 16:59:51 -07002108
Chris Leech2e70e242009-08-25 14:00:23 -07002109 mutex_unlock(&fcoe_config_mutex);
2110}
2111
Robert Love85b4aa42008-12-09 15:10:24 -08002112/**
Yi Zou78a58242011-01-28 16:05:16 -08002113 * fcoe_match() - Check if the FCoE is supported on the given netdevice
2114 * @netdev : The net_device object the Ethernet interface to create on
Robert Love1875f272009-11-03 11:47:50 -08002115 *
Yi Zou78a58242011-01-28 16:05:16 -08002116 * Called from fcoe transport.
2117 *
2118 * Returns: always returns true as this is the default FCoE transport,
2119 * i.e., support all netdevs.
2120 */
2121static bool fcoe_match(struct net_device *netdev)
2122{
2123 return true;
2124}
2125
2126/**
john fastabend6f6c2aa2011-11-18 13:35:56 -08002127 * fcoe_dcb_create() - Initialize DCB attributes and hooks
2128 * @netdev: The net_device object of the L2 link that should be queried
2129 * @port: The fcoe_port to bind FCoE APP priority with
2130 * @
2131 */
2132static void fcoe_dcb_create(struct fcoe_interface *fcoe)
2133{
2134#ifdef CONFIG_DCB
2135 int dcbx;
2136 u8 fup, up;
2137 struct net_device *netdev = fcoe->realdev;
2138 struct fcoe_port *port = lport_priv(fcoe->ctlr.lp);
2139 struct dcb_app app = {
2140 .priority = 0,
2141 .protocol = ETH_P_FCOE
2142 };
2143
2144 /* setup DCB priority attributes. */
2145 if (netdev && netdev->dcbnl_ops && netdev->dcbnl_ops->getdcbx) {
2146 dcbx = netdev->dcbnl_ops->getdcbx(netdev);
2147
2148 if (dcbx & DCB_CAP_DCBX_VER_IEEE) {
2149 app.selector = IEEE_8021QAZ_APP_SEL_ETHERTYPE;
2150 up = dcb_ieee_getapp_mask(netdev, &app);
2151 app.protocol = ETH_P_FIP;
2152 fup = dcb_ieee_getapp_mask(netdev, &app);
2153 } else {
2154 app.selector = DCB_APP_IDTYPE_ETHTYPE;
2155 up = dcb_getapp(netdev, &app);
2156 app.protocol = ETH_P_FIP;
2157 fup = dcb_getapp(netdev, &app);
2158 }
2159
2160 port->priority = ffs(up) ? ffs(up) - 1 : 0;
2161 fcoe->ctlr.priority = ffs(fup) ? ffs(fup) - 1 : port->priority;
2162 }
2163#endif
2164}
2165
2166/**
Yi Zou78a58242011-01-28 16:05:16 -08002167 * fcoe_create() - Create a fcoe interface
2168 * @netdev : The net_device object the Ethernet interface to create on
2169 * @fip_mode: The FIP mode for this creation
2170 *
2171 * Called from fcoe transport
Robert Love85b4aa42008-12-09 15:10:24 -08002172 *
2173 * Returns: 0 for success
Robert Love34f42a02009-02-27 10:55:45 -08002174 */
Yi Zou78a58242011-01-28 16:05:16 -08002175static int fcoe_create(struct net_device *netdev, enum fip_state fip_mode)
Robert Love85b4aa42008-12-09 15:10:24 -08002176{
Neerav Parikhb2085a42011-06-20 16:59:51 -07002177 int rc = 0;
Chris Leech030f4e02009-08-25 14:00:02 -07002178 struct fcoe_interface *fcoe;
Chris Leechaf7f85d2009-08-25 13:59:24 -07002179 struct fc_lport *lport;
Robert Love85b4aa42008-12-09 15:10:24 -08002180
Chris Leechdfc1d0f2009-08-25 14:00:13 -07002181 mutex_lock(&fcoe_config_mutex);
Robert Loveee5df622011-04-01 16:05:59 -07002182 rtnl_lock();
Vasu Dev34ce27b2010-05-07 15:18:46 -07002183
Robert Love85b4aa42008-12-09 15:10:24 -08002184 /* look for existing lport */
2185 if (fcoe_hostlist_lookup(netdev)) {
2186 rc = -EEXIST;
Yi Zou78a58242011-01-28 16:05:16 -08002187 goto out_nodev;
Robert Love85b4aa42008-12-09 15:10:24 -08002188 }
Robert Love85b4aa42008-12-09 15:10:24 -08002189
Joe Eykholt1dd454d2010-07-20 15:20:46 -07002190 fcoe = fcoe_interface_create(netdev, fip_mode);
Robert Love7287fb92011-01-28 16:03:47 -08002191 if (IS_ERR(fcoe)) {
2192 rc = PTR_ERR(fcoe);
Yi Zou78a58242011-01-28 16:05:16 -08002193 goto out_nodev;
Chris Leech030f4e02009-08-25 14:00:02 -07002194 }
2195
Chris Leech9a057532009-11-03 11:46:40 -08002196 lport = fcoe_if_create(fcoe, &netdev->dev, 0);
Chris Leechaf7f85d2009-08-25 13:59:24 -07002197 if (IS_ERR(lport)) {
Robert Loved5488eb2009-06-10 15:30:59 -07002198 printk(KERN_ERR "fcoe: Failed to create interface (%s)\n",
Robert Love85b4aa42008-12-09 15:10:24 -08002199 netdev->name);
Robert Love85b4aa42008-12-09 15:10:24 -08002200 rc = -EIO;
Robert Love848e7d52011-08-25 12:40:47 -07002201 rtnl_unlock();
Chris Leech2e70e242009-08-25 14:00:23 -07002202 fcoe_interface_cleanup(fcoe);
Robert Love848e7d52011-08-25 12:40:47 -07002203 goto out_nortnl;
Robert Love85b4aa42008-12-09 15:10:24 -08002204 }
Chris Leech030f4e02009-08-25 14:00:02 -07002205
Chris Leech54b649f2009-08-25 14:00:07 -07002206 /* Make this the "master" N_Port */
2207 fcoe->ctlr.lp = lport;
Chris Leech030f4e02009-08-25 14:00:02 -07002208
john fastabend6f6c2aa2011-11-18 13:35:56 -08002209 /* setup DCB priority attributes. */
2210 fcoe_dcb_create(fcoe);
2211
Chris Leechc863df32009-08-25 14:00:18 -07002212 /* add to lports list */
2213 fcoe_hostlist_add(lport);
2214
Chris Leech54b649f2009-08-25 14:00:07 -07002215 /* start FIP Discovery and FLOGI */
2216 lport->boot_time = jiffies;
2217 fc_fabric_login(lport);
2218 if (!fcoe_link_ok(lport))
2219 fcoe_ctlr_link_up(&fcoe->ctlr);
2220
Robert Love85b4aa42008-12-09 15:10:24 -08002221out_nodev:
Vasu Dev34ce27b2010-05-07 15:18:46 -07002222 rtnl_unlock();
Robert Love848e7d52011-08-25 12:40:47 -07002223out_nortnl:
Chris Leechdfc1d0f2009-08-25 14:00:13 -07002224 mutex_unlock(&fcoe_config_mutex);
Robert Love85b4aa42008-12-09 15:10:24 -08002225 return rc;
2226}
2227
Robert Love34f42a02009-02-27 10:55:45 -08002228/**
Robert Love5e4f8fe2010-05-07 15:18:35 -07002229 * fcoe_link_speed_update() - Update the supported and actual link speeds
2230 * @lport: The local port to update speeds for
Robert Love85b4aa42008-12-09 15:10:24 -08002231 *
Robert Love5e4f8fe2010-05-07 15:18:35 -07002232 * Returns: 0 if the ethtool query was successful
2233 * -1 if the ethtool query failed
Robert Love85b4aa42008-12-09 15:10:24 -08002234 */
Bart Van Assche7c9c6842012-01-13 17:26:25 -08002235static int fcoe_link_speed_update(struct fc_lport *lport)
Robert Love85b4aa42008-12-09 15:10:24 -08002236{
Bhanu Prakash Gollapudi8597ae82011-01-28 16:05:37 -08002237 struct net_device *netdev = fcoe_netdev(lport);
David Decotigny8ae6dac2011-04-27 18:32:38 +00002238 struct ethtool_cmd ecmd;
Robert Love85b4aa42008-12-09 15:10:24 -08002239
Jiri Pirko4bc71cb2011-09-03 03:34:30 +00002240 if (!__ethtool_get_settings(netdev, &ecmd)) {
Robert Love1875f272009-11-03 11:47:50 -08002241 lport->link_supported_speeds &=
Yi Zou2f718d62009-07-29 17:04:01 -07002242 ~(FC_PORTSPEED_1GBIT | FC_PORTSPEED_10GBIT);
2243 if (ecmd.supported & (SUPPORTED_1000baseT_Half |
2244 SUPPORTED_1000baseT_Full))
Robert Love1875f272009-11-03 11:47:50 -08002245 lport->link_supported_speeds |= FC_PORTSPEED_1GBIT;
Yi Zou2f718d62009-07-29 17:04:01 -07002246 if (ecmd.supported & SUPPORTED_10000baseT_Full)
Robert Love1875f272009-11-03 11:47:50 -08002247 lport->link_supported_speeds |=
Yi Zou2f718d62009-07-29 17:04:01 -07002248 FC_PORTSPEED_10GBIT;
David Decotigny8ae6dac2011-04-27 18:32:38 +00002249 switch (ethtool_cmd_speed(&ecmd)) {
2250 case SPEED_1000:
Robert Love1875f272009-11-03 11:47:50 -08002251 lport->link_speed = FC_PORTSPEED_1GBIT;
David Decotigny8ae6dac2011-04-27 18:32:38 +00002252 break;
2253 case SPEED_10000:
Robert Love1875f272009-11-03 11:47:50 -08002254 lport->link_speed = FC_PORTSPEED_10GBIT;
David Decotigny8ae6dac2011-04-27 18:32:38 +00002255 break;
2256 }
Yi Zou2f718d62009-07-29 17:04:01 -07002257 return 0;
2258 }
2259 return -1;
Robert Love85b4aa42008-12-09 15:10:24 -08002260}
Robert Love85b4aa42008-12-09 15:10:24 -08002261
Robert Love34f42a02009-02-27 10:55:45 -08002262/**
Robert Love5e4f8fe2010-05-07 15:18:35 -07002263 * fcoe_link_ok() - Check if the link is OK for a local port
2264 * @lport: The local port to check link on
2265 *
2266 * Returns: 0 if link is UP and OK, -1 if not
2267 *
2268 */
Bart Van Assche7c9c6842012-01-13 17:26:25 -08002269static int fcoe_link_ok(struct fc_lport *lport)
Robert Love5e4f8fe2010-05-07 15:18:35 -07002270{
Bhanu Prakash Gollapudi8597ae82011-01-28 16:05:37 -08002271 struct net_device *netdev = fcoe_netdev(lport);
Robert Love5e4f8fe2010-05-07 15:18:35 -07002272
2273 if (netif_oper_up(netdev))
2274 return 0;
2275 return -1;
2276}
2277
2278/**
Robert Love1875f272009-11-03 11:47:50 -08002279 * fcoe_percpu_clean() - Clear all pending skbs for an local port
2280 * @lport: The local port whose skbs are to be cleared
Joe Eykholte7a51992009-08-25 14:04:08 -07002281 *
2282 * Must be called with fcoe_create_mutex held to single-thread completion.
2283 *
2284 * This flushes the pending skbs by adding a new skb to each queue and
2285 * waiting until they are all freed. This assures us that not only are
2286 * there no packets that will be handled by the lport, but also that any
2287 * threads already handling packet have returned.
Robert Love85b4aa42008-12-09 15:10:24 -08002288 */
Bart Van Assche7c9c6842012-01-13 17:26:25 -08002289static void fcoe_percpu_clean(struct fc_lport *lport)
Robert Love85b4aa42008-12-09 15:10:24 -08002290{
Robert Love85b4aa42008-12-09 15:10:24 -08002291 struct fcoe_percpu_s *pp;
2292 struct fcoe_rcv_info *fr;
2293 struct sk_buff_head *list;
2294 struct sk_buff *skb, *next;
2295 struct sk_buff *head;
Robert Love5e5e92d2009-03-17 11:41:35 -07002296 unsigned int cpu;
Robert Love85b4aa42008-12-09 15:10:24 -08002297
Robert Love5e5e92d2009-03-17 11:41:35 -07002298 for_each_possible_cpu(cpu) {
2299 pp = &per_cpu(fcoe_percpu, cpu);
2300 spin_lock_bh(&pp->fcoe_rx_list.lock);
2301 list = &pp->fcoe_rx_list;
2302 head = list->next;
2303 for (skb = head; skb != (struct sk_buff *)list;
2304 skb = next) {
2305 next = skb->next;
2306 fr = fcoe_dev_from_skb(skb);
Robert Love1875f272009-11-03 11:47:50 -08002307 if (fr->fr_dev == lport) {
Robert Love5e5e92d2009-03-17 11:41:35 -07002308 __skb_unlink(skb, list);
2309 kfree_skb(skb);
Robert Love85b4aa42008-12-09 15:10:24 -08002310 }
Robert Love85b4aa42008-12-09 15:10:24 -08002311 }
Joe Eykholte7a51992009-08-25 14:04:08 -07002312
2313 if (!pp->thread || !cpu_online(cpu)) {
2314 spin_unlock_bh(&pp->fcoe_rx_list.lock);
2315 continue;
2316 }
2317
2318 skb = dev_alloc_skb(0);
2319 if (!skb) {
2320 spin_unlock_bh(&pp->fcoe_rx_list.lock);
2321 continue;
2322 }
2323 skb->destructor = fcoe_percpu_flush_done;
2324
2325 __skb_queue_tail(&pp->fcoe_rx_list, skb);
2326 if (pp->fcoe_rx_list.qlen == 1)
2327 wake_up_process(pp->thread);
Robert Love5e5e92d2009-03-17 11:41:35 -07002328 spin_unlock_bh(&pp->fcoe_rx_list.lock);
Joe Eykholte7a51992009-08-25 14:04:08 -07002329
2330 wait_for_completion(&fcoe_flush_completion);
Robert Love85b4aa42008-12-09 15:10:24 -08002331 }
2332}
Robert Love85b4aa42008-12-09 15:10:24 -08002333
2334/**
Robert Love1875f272009-11-03 11:47:50 -08002335 * fcoe_reset() - Reset a local port
2336 * @shost: The SCSI host associated with the local port to be reset
Robert Love85b4aa42008-12-09 15:10:24 -08002337 *
Robert Love1875f272009-11-03 11:47:50 -08002338 * Returns: Always 0 (return value required by FC transport template)
Robert Love85b4aa42008-12-09 15:10:24 -08002339 */
Bart Van Assche7c9c6842012-01-13 17:26:25 -08002340static int fcoe_reset(struct Scsi_Host *shost)
Robert Love85b4aa42008-12-09 15:10:24 -08002341{
2342 struct fc_lport *lport = shost_priv(shost);
Vasu Devd2f80952011-02-25 15:03:28 -08002343 struct fcoe_port *port = lport_priv(lport);
2344 struct fcoe_interface *fcoe = port->priv;
2345
2346 fcoe_ctlr_link_down(&fcoe->ctlr);
2347 fcoe_clean_pending_queue(fcoe->ctlr.lp);
2348 if (!fcoe_link_ok(fcoe->ctlr.lp))
2349 fcoe_ctlr_link_up(&fcoe->ctlr);
Robert Love85b4aa42008-12-09 15:10:24 -08002350 return 0;
2351}
Robert Love85b4aa42008-12-09 15:10:24 -08002352
Robert Love34f42a02009-02-27 10:55:45 -08002353/**
Robert Love1875f272009-11-03 11:47:50 -08002354 * fcoe_hostlist_lookup_port() - Find the FCoE interface associated with a net device
2355 * @netdev: The net device used as a key
Robert Love85b4aa42008-12-09 15:10:24 -08002356 *
Robert Love1875f272009-11-03 11:47:50 -08002357 * Locking: Must be called with the RNL mutex held.
2358 *
2359 * Returns: NULL or the FCoE interface
Robert Love85b4aa42008-12-09 15:10:24 -08002360 */
Chris Leech014f5c32009-08-25 13:59:30 -07002361static struct fcoe_interface *
Robert Love1875f272009-11-03 11:47:50 -08002362fcoe_hostlist_lookup_port(const struct net_device *netdev)
Robert Love85b4aa42008-12-09 15:10:24 -08002363{
Chris Leech014f5c32009-08-25 13:59:30 -07002364 struct fcoe_interface *fcoe;
Robert Love85b4aa42008-12-09 15:10:24 -08002365
Chris Leech014f5c32009-08-25 13:59:30 -07002366 list_for_each_entry(fcoe, &fcoe_hostlist, list) {
Robert Love1875f272009-11-03 11:47:50 -08002367 if (fcoe->netdev == netdev)
Chris Leech014f5c32009-08-25 13:59:30 -07002368 return fcoe;
Robert Love85b4aa42008-12-09 15:10:24 -08002369 }
Robert Love85b4aa42008-12-09 15:10:24 -08002370 return NULL;
2371}
2372
Robert Love34f42a02009-02-27 10:55:45 -08002373/**
Robert Love1875f272009-11-03 11:47:50 -08002374 * fcoe_hostlist_lookup() - Find the local port associated with a
2375 * given net device
2376 * @netdev: The netdevice used as a key
Robert Love85b4aa42008-12-09 15:10:24 -08002377 *
Robert Love1875f272009-11-03 11:47:50 -08002378 * Locking: Must be called with the RTNL mutex held
2379 *
2380 * Returns: NULL or the local port
Robert Love85b4aa42008-12-09 15:10:24 -08002381 */
Chris Leech090eb6c2009-08-25 14:00:28 -07002382static struct fc_lport *fcoe_hostlist_lookup(const struct net_device *netdev)
Robert Love85b4aa42008-12-09 15:10:24 -08002383{
Chris Leech014f5c32009-08-25 13:59:30 -07002384 struct fcoe_interface *fcoe;
Robert Love85b4aa42008-12-09 15:10:24 -08002385
Chris Leech014f5c32009-08-25 13:59:30 -07002386 fcoe = fcoe_hostlist_lookup_port(netdev);
Chris Leech3fe9a0b2009-08-25 13:59:46 -07002387 return (fcoe) ? fcoe->ctlr.lp : NULL;
Robert Love85b4aa42008-12-09 15:10:24 -08002388}
Robert Love85b4aa42008-12-09 15:10:24 -08002389
Robert Love34f42a02009-02-27 10:55:45 -08002390/**
Robert Love1875f272009-11-03 11:47:50 -08002391 * fcoe_hostlist_add() - Add the FCoE interface identified by a local
2392 * port to the hostlist
2393 * @lport: The local port that identifies the FCoE interface to be added
2394 *
2395 * Locking: must be called with the RTNL mutex held
Robert Love85b4aa42008-12-09 15:10:24 -08002396 *
2397 * Returns: 0 for success
2398 */
Chris Leech090eb6c2009-08-25 14:00:28 -07002399static int fcoe_hostlist_add(const struct fc_lport *lport)
Robert Love85b4aa42008-12-09 15:10:24 -08002400{
Chris Leech014f5c32009-08-25 13:59:30 -07002401 struct fcoe_interface *fcoe;
2402 struct fcoe_port *port;
Robert Love85b4aa42008-12-09 15:10:24 -08002403
Chris Leech014f5c32009-08-25 13:59:30 -07002404 fcoe = fcoe_hostlist_lookup_port(fcoe_netdev(lport));
2405 if (!fcoe) {
2406 port = lport_priv(lport);
Bhanu Prakash Gollapudi8597ae82011-01-28 16:05:37 -08002407 fcoe = port->priv;
Chris Leech014f5c32009-08-25 13:59:30 -07002408 list_add_tail(&fcoe->list, &fcoe_hostlist);
Robert Love85b4aa42008-12-09 15:10:24 -08002409 }
2410 return 0;
2411}
Robert Love85b4aa42008-12-09 15:10:24 -08002412
Yi Zou78a58242011-01-28 16:05:16 -08002413
2414static struct fcoe_transport fcoe_sw_transport = {
2415 .name = {FCOE_TRANSPORT_DEFAULT},
2416 .attached = false,
2417 .list = LIST_HEAD_INIT(fcoe_sw_transport.list),
2418 .match = fcoe_match,
2419 .create = fcoe_create,
2420 .destroy = fcoe_destroy,
2421 .enable = fcoe_enable,
2422 .disable = fcoe_disable,
2423};
2424
Robert Love34f42a02009-02-27 10:55:45 -08002425/**
Robert Love1875f272009-11-03 11:47:50 -08002426 * fcoe_init() - Initialize fcoe.ko
Robert Love85b4aa42008-12-09 15:10:24 -08002427 *
Robert Love1875f272009-11-03 11:47:50 -08002428 * Returns: 0 on success, or a negative value on failure
Robert Love34f42a02009-02-27 10:55:45 -08002429 */
Robert Love85b4aa42008-12-09 15:10:24 -08002430static int __init fcoe_init(void)
2431{
Robert Love1875f272009-11-03 11:47:50 -08002432 struct fcoe_percpu_s *p;
Robert Love38eccab2009-03-17 11:41:30 -07002433 unsigned int cpu;
Robert Love8976f422009-03-17 11:41:46 -07002434 int rc = 0;
Robert Love85b4aa42008-12-09 15:10:24 -08002435
Tejun Heo2ca32b42011-01-28 16:05:32 -08002436 fcoe_wq = alloc_workqueue("fcoe", 0, 0);
2437 if (!fcoe_wq)
2438 return -ENOMEM;
2439
Yi Zou78a58242011-01-28 16:05:16 -08002440 /* register as a fcoe transport */
2441 rc = fcoe_transport_attach(&fcoe_sw_transport);
2442 if (rc) {
2443 printk(KERN_ERR "failed to register an fcoe transport, check "
2444 "if libfcoe is loaded\n");
2445 return rc;
2446 }
2447
Chris Leechdfc1d0f2009-08-25 14:00:13 -07002448 mutex_lock(&fcoe_config_mutex);
2449
Robert Love38eccab2009-03-17 11:41:30 -07002450 for_each_possible_cpu(cpu) {
Robert Love5e5e92d2009-03-17 11:41:35 -07002451 p = &per_cpu(fcoe_percpu, cpu);
Robert Love38eccab2009-03-17 11:41:30 -07002452 skb_queue_head_init(&p->fcoe_rx_list);
2453 }
2454
Robert Love8976f422009-03-17 11:41:46 -07002455 for_each_online_cpu(cpu)
2456 fcoe_percpu_thread_create(cpu);
Robert Love85b4aa42008-12-09 15:10:24 -08002457
Robert Love8976f422009-03-17 11:41:46 -07002458 /* Initialize per CPU interrupt thread */
2459 rc = register_hotcpu_notifier(&fcoe_cpu_notifier);
2460 if (rc)
2461 goto out_free;
Robert Love85b4aa42008-12-09 15:10:24 -08002462
Robert Love8976f422009-03-17 11:41:46 -07002463 /* Setup link change notification */
Robert Love85b4aa42008-12-09 15:10:24 -08002464 fcoe_dev_setup();
2465
Chris Leech5892c322009-08-25 13:59:14 -07002466 rc = fcoe_if_init();
2467 if (rc)
2468 goto out_free;
Robert Love85b4aa42008-12-09 15:10:24 -08002469
Chris Leechdfc1d0f2009-08-25 14:00:13 -07002470 mutex_unlock(&fcoe_config_mutex);
Robert Love85b4aa42008-12-09 15:10:24 -08002471 return 0;
Robert Love8976f422009-03-17 11:41:46 -07002472
2473out_free:
2474 for_each_online_cpu(cpu) {
2475 fcoe_percpu_thread_destroy(cpu);
2476 }
Chris Leechdfc1d0f2009-08-25 14:00:13 -07002477 mutex_unlock(&fcoe_config_mutex);
Tejun Heo2ca32b42011-01-28 16:05:32 -08002478 destroy_workqueue(fcoe_wq);
Robert Love8976f422009-03-17 11:41:46 -07002479 return rc;
Robert Love85b4aa42008-12-09 15:10:24 -08002480}
2481module_init(fcoe_init);
2482
2483/**
Robert Love1875f272009-11-03 11:47:50 -08002484 * fcoe_exit() - Clean up fcoe.ko
Robert Love85b4aa42008-12-09 15:10:24 -08002485 *
Robert Love1875f272009-11-03 11:47:50 -08002486 * Returns: 0 on success or a negative value on failure
Robert Love34f42a02009-02-27 10:55:45 -08002487 */
Robert Love85b4aa42008-12-09 15:10:24 -08002488static void __exit fcoe_exit(void)
2489{
Chris Leech014f5c32009-08-25 13:59:30 -07002490 struct fcoe_interface *fcoe, *tmp;
Chris Leech2e70e242009-08-25 14:00:23 -07002491 struct fcoe_port *port;
Robert Love1875f272009-11-03 11:47:50 -08002492 unsigned int cpu;
Robert Love85b4aa42008-12-09 15:10:24 -08002493
Chris Leechdfc1d0f2009-08-25 14:00:13 -07002494 mutex_lock(&fcoe_config_mutex);
2495
Robert Love85b4aa42008-12-09 15:10:24 -08002496 fcoe_dev_cleanup();
2497
Vasu Dev5919a592009-03-27 09:03:29 -07002498 /* releases the associated fcoe hosts */
Chris Leech090eb6c2009-08-25 14:00:28 -07002499 rtnl_lock();
2500 list_for_each_entry_safe(fcoe, tmp, &fcoe_hostlist, list) {
Chris Leechc863df32009-08-25 14:00:18 -07002501 list_del(&fcoe->list);
Chris Leech2e70e242009-08-25 14:00:23 -07002502 port = lport_priv(fcoe->ctlr.lp);
Tejun Heo2ca32b42011-01-28 16:05:32 -08002503 queue_work(fcoe_wq, &port->destroy_work);
Chris Leechc863df32009-08-25 14:00:18 -07002504 }
Chris Leech090eb6c2009-08-25 14:00:28 -07002505 rtnl_unlock();
Robert Love85b4aa42008-12-09 15:10:24 -08002506
Robert Love8976f422009-03-17 11:41:46 -07002507 unregister_hotcpu_notifier(&fcoe_cpu_notifier);
2508
Chris Leech014f5c32009-08-25 13:59:30 -07002509 for_each_online_cpu(cpu)
Robert Love8976f422009-03-17 11:41:46 -07002510 fcoe_percpu_thread_destroy(cpu);
Robert Love85b4aa42008-12-09 15:10:24 -08002511
Chris Leechdfc1d0f2009-08-25 14:00:13 -07002512 mutex_unlock(&fcoe_config_mutex);
Chris Leech2e70e242009-08-25 14:00:23 -07002513
Tejun Heo2ca32b42011-01-28 16:05:32 -08002514 /*
2515 * destroy_work's may be chained but destroy_workqueue()
2516 * can take care of them. Just kill the fcoe_wq.
2517 */
2518 destroy_workqueue(fcoe_wq);
Chris Leech2e70e242009-08-25 14:00:23 -07002519
Tejun Heo2ca32b42011-01-28 16:05:32 -08002520 /*
2521 * Detaching from the scsi transport must happen after all
2522 * destroys are done on the fcoe_wq. destroy_workqueue will
2523 * enusre the fcoe_wq is flushed.
2524 */
Chris Leech2e70e242009-08-25 14:00:23 -07002525 fcoe_if_exit();
Yi Zou78a58242011-01-28 16:05:16 -08002526
2527 /* detach from fcoe transport */
2528 fcoe_transport_detach(&fcoe_sw_transport);
Robert Love85b4aa42008-12-09 15:10:24 -08002529}
2530module_exit(fcoe_exit);
Chris Leech11b56182009-11-03 11:46:29 -08002531
2532/**
2533 * fcoe_flogi_resp() - FCoE specific FLOGI and FDISC response handler
2534 * @seq: active sequence in the FLOGI or FDISC exchange
2535 * @fp: response frame, or error encoded in a pointer (timeout)
2536 * @arg: pointer the the fcoe_ctlr structure
2537 *
Uwe Kleine-König65155b32010-06-11 12:17:01 +02002538 * This handles MAC address management for FCoE, then passes control on to
Chris Leech11b56182009-11-03 11:46:29 -08002539 * the libfc FLOGI response handler.
2540 */
2541static void fcoe_flogi_resp(struct fc_seq *seq, struct fc_frame *fp, void *arg)
2542{
2543 struct fcoe_ctlr *fip = arg;
2544 struct fc_exch *exch = fc_seq_exch(seq);
2545 struct fc_lport *lport = exch->lp;
2546 u8 *mac;
2547
2548 if (IS_ERR(fp))
2549 goto done;
2550
2551 mac = fr_cb(fp)->granted_mac;
Vasu Dev907c07d2011-10-28 11:34:23 -07002552 /* pre-FIP */
2553 if (is_zero_ether_addr(mac))
2554 fcoe_ctlr_recv_flogi(fip, lport, fp);
2555 if (!is_zero_ether_addr(mac))
2556 fcoe_update_src_mac(lport, mac);
Chris Leech11b56182009-11-03 11:46:29 -08002557done:
2558 fc_lport_flogi_resp(seq, fp, lport);
2559}
2560
2561/**
2562 * fcoe_logo_resp() - FCoE specific LOGO response handler
2563 * @seq: active sequence in the LOGO exchange
2564 * @fp: response frame, or error encoded in a pointer (timeout)
2565 * @arg: pointer the the fcoe_ctlr structure
2566 *
Uwe Kleine-König65155b32010-06-11 12:17:01 +02002567 * This handles MAC address management for FCoE, then passes control on to
Chris Leech11b56182009-11-03 11:46:29 -08002568 * the libfc LOGO response handler.
2569 */
2570static void fcoe_logo_resp(struct fc_seq *seq, struct fc_frame *fp, void *arg)
2571{
Joe Eykholt386309ce2009-11-03 11:49:16 -08002572 struct fc_lport *lport = arg;
Chris Leech11b56182009-11-03 11:46:29 -08002573 static u8 zero_mac[ETH_ALEN] = { 0 };
2574
2575 if (!IS_ERR(fp))
Joe Eykholt386309ce2009-11-03 11:49:16 -08002576 fcoe_update_src_mac(lport, zero_mac);
Chris Leech11b56182009-11-03 11:46:29 -08002577 fc_lport_logo_resp(seq, fp, lport);
2578}
2579
2580/**
2581 * fcoe_elsct_send - FCoE specific ELS handler
2582 *
2583 * This does special case handling of FIP encapsualted ELS exchanges for FCoE,
2584 * using FCoE specific response handlers and passing the FIP controller as
2585 * the argument (the lport is still available from the exchange).
2586 *
2587 * Most of the work here is just handed off to the libfc routine.
2588 */
Robert Love1875f272009-11-03 11:47:50 -08002589static struct fc_seq *fcoe_elsct_send(struct fc_lport *lport, u32 did,
2590 struct fc_frame *fp, unsigned int op,
2591 void (*resp)(struct fc_seq *,
2592 struct fc_frame *,
2593 void *),
2594 void *arg, u32 timeout)
Chris Leech11b56182009-11-03 11:46:29 -08002595{
2596 struct fcoe_port *port = lport_priv(lport);
Bhanu Prakash Gollapudi8597ae82011-01-28 16:05:37 -08002597 struct fcoe_interface *fcoe = port->priv;
Chris Leech11b56182009-11-03 11:46:29 -08002598 struct fcoe_ctlr *fip = &fcoe->ctlr;
2599 struct fc_frame_header *fh = fc_frame_header_get(fp);
2600
2601 switch (op) {
2602 case ELS_FLOGI:
2603 case ELS_FDISC:
Joe Eykholte10f8c62010-07-20 15:20:30 -07002604 if (lport->point_to_multipoint)
2605 break;
Chris Leech11b56182009-11-03 11:46:29 -08002606 return fc_elsct_send(lport, did, fp, op, fcoe_flogi_resp,
2607 fip, timeout);
2608 case ELS_LOGO:
2609 /* only hook onto fabric logouts, not port logouts */
2610 if (ntoh24(fh->fh_d_id) != FC_FID_FLOGI)
2611 break;
2612 return fc_elsct_send(lport, did, fp, op, fcoe_logo_resp,
Joe Eykholt386309ce2009-11-03 11:49:16 -08002613 lport, timeout);
Chris Leech11b56182009-11-03 11:46:29 -08002614 }
2615 return fc_elsct_send(lport, did, fp, op, resp, arg, timeout);
2616}
2617
Chris Leech9a057532009-11-03 11:46:40 -08002618/**
2619 * fcoe_vport_create() - create an fc_host/scsi_host for a vport
2620 * @vport: fc_vport object to create a new fc_host for
2621 * @disabled: start the new fc_host in a disabled state by default?
2622 *
2623 * Returns: 0 for success
2624 */
2625static int fcoe_vport_create(struct fc_vport *vport, bool disabled)
2626{
2627 struct Scsi_Host *shost = vport_to_shost(vport);
2628 struct fc_lport *n_port = shost_priv(shost);
2629 struct fcoe_port *port = lport_priv(n_port);
Bhanu Prakash Gollapudi8597ae82011-01-28 16:05:37 -08002630 struct fcoe_interface *fcoe = port->priv;
Chris Leech9a057532009-11-03 11:46:40 -08002631 struct net_device *netdev = fcoe->netdev;
2632 struct fc_lport *vn_port;
Neerav Parikhbdf25212011-05-16 16:45:29 -07002633 int rc;
2634 char buf[32];
2635
2636 rc = fcoe_validate_vport_create(vport);
2637 if (rc) {
Bhanu Prakash Gollapudid8348952011-08-04 17:38:49 -07002638 fcoe_wwn_to_str(vport->port_name, buf, sizeof(buf));
Neerav Parikhbdf25212011-05-16 16:45:29 -07002639 printk(KERN_ERR "fcoe: Failed to create vport, "
2640 "WWPN (0x%s) already exists\n",
2641 buf);
2642 return rc;
2643 }
Chris Leech9a057532009-11-03 11:46:40 -08002644
2645 mutex_lock(&fcoe_config_mutex);
Jiri Pirko4bc71cb2011-09-03 03:34:30 +00002646 rtnl_lock();
Chris Leech9a057532009-11-03 11:46:40 -08002647 vn_port = fcoe_if_create(fcoe, &vport->dev, 1);
Jiri Pirko4bc71cb2011-09-03 03:34:30 +00002648 rtnl_unlock();
Chris Leech9a057532009-11-03 11:46:40 -08002649 mutex_unlock(&fcoe_config_mutex);
2650
2651 if (IS_ERR(vn_port)) {
2652 printk(KERN_ERR "fcoe: fcoe_vport_create(%s) failed\n",
2653 netdev->name);
2654 return -EIO;
2655 }
2656
2657 if (disabled) {
2658 fc_vport_set_state(vport, FC_VPORT_DISABLED);
2659 } else {
2660 vn_port->boot_time = jiffies;
2661 fc_fabric_login(vn_port);
2662 fc_vport_setlink(vn_port);
2663 }
2664 return 0;
2665}
2666
2667/**
2668 * fcoe_vport_destroy() - destroy the fc_host/scsi_host for a vport
2669 * @vport: fc_vport object that is being destroyed
2670 *
2671 * Returns: 0 for success
2672 */
2673static int fcoe_vport_destroy(struct fc_vport *vport)
2674{
2675 struct Scsi_Host *shost = vport_to_shost(vport);
2676 struct fc_lport *n_port = shost_priv(shost);
2677 struct fc_lport *vn_port = vport->dd_data;
2678 struct fcoe_port *port = lport_priv(vn_port);
2679
2680 mutex_lock(&n_port->lp_mutex);
2681 list_del(&vn_port->list);
2682 mutex_unlock(&n_port->lp_mutex);
Tejun Heo2ca32b42011-01-28 16:05:32 -08002683 queue_work(fcoe_wq, &port->destroy_work);
Chris Leech9a057532009-11-03 11:46:40 -08002684 return 0;
2685}
2686
2687/**
2688 * fcoe_vport_disable() - change vport state
2689 * @vport: vport to bring online/offline
2690 * @disable: should the vport be disabled?
2691 */
2692static int fcoe_vport_disable(struct fc_vport *vport, bool disable)
2693{
2694 struct fc_lport *lport = vport->dd_data;
2695
2696 if (disable) {
2697 fc_vport_set_state(vport, FC_VPORT_DISABLED);
2698 fc_fabric_logoff(lport);
2699 } else {
2700 lport->boot_time = jiffies;
2701 fc_fabric_login(lport);
2702 fc_vport_setlink(lport);
2703 }
2704
2705 return 0;
2706}
2707
Chris Leechdc8596d2009-11-03 11:47:18 -08002708/**
2709 * fcoe_vport_set_symbolic_name() - append vport string to symbolic name
2710 * @vport: fc_vport with a new symbolic name string
2711 *
2712 * After generating a new symbolic name string, a new RSPN_ID request is
2713 * sent to the name server. There is no response handler, so if it fails
2714 * for some reason it will not be retried.
2715 */
2716static void fcoe_set_vport_symbolic_name(struct fc_vport *vport)
2717{
2718 struct fc_lport *lport = vport->dd_data;
2719 struct fc_frame *fp;
2720 size_t len;
2721
2722 snprintf(fc_host_symbolic_name(lport->host), FC_SYMBOLIC_NAME_SIZE,
2723 "%s v%s over %s : %s", FCOE_NAME, FCOE_VERSION,
2724 fcoe_netdev(lport)->name, vport->symbolic_name);
2725
2726 if (lport->state != LPORT_ST_READY)
2727 return;
2728
2729 len = strnlen(fc_host_symbolic_name(lport->host), 255);
2730 fp = fc_frame_alloc(lport,
2731 sizeof(struct fc_ct_hdr) +
2732 sizeof(struct fc_ns_rspn) + len);
2733 if (!fp)
2734 return;
2735 lport->tt.elsct_send(lport, FC_FID_DIR_SERV, fp, FC_NS_RSPN_ID,
Joe Eykholtb94f8952009-11-03 11:50:21 -08002736 NULL, NULL, 3 * lport->r_a_tov);
Chris Leechdc8596d2009-11-03 11:47:18 -08002737}
Yi Zoub84056b2009-11-20 14:55:19 -08002738
2739/**
2740 * fcoe_get_lesb() - Fill the FCoE Link Error Status Block
2741 * @lport: the local port
2742 * @fc_lesb: the link error status block
2743 */
2744static void fcoe_get_lesb(struct fc_lport *lport,
2745 struct fc_els_lesb *fc_lesb)
2746{
Yi Zoub84056b2009-11-20 14:55:19 -08002747 struct net_device *netdev = fcoe_netdev(lport);
2748
Bhanu Prakash Gollapudi814740d2011-10-03 16:45:01 -07002749 __fcoe_get_lesb(lport, fc_lesb, netdev);
Yi Zoub84056b2009-11-20 14:55:19 -08002750}
Joe Eykholt7d65b0d2010-03-12 16:08:02 -08002751
2752/**
2753 * fcoe_set_port_id() - Callback from libfc when Port_ID is set.
2754 * @lport: the local port
2755 * @port_id: the port ID
2756 * @fp: the received frame, if any, that caused the port_id to be set.
2757 *
2758 * This routine handles the case where we received a FLOGI and are
2759 * entering point-to-point mode. We need to call fcoe_ctlr_recv_flogi()
2760 * so it can set the non-mapped mode and gateway address.
2761 *
2762 * The FLOGI LS_ACC is handled by fcoe_flogi_resp().
2763 */
2764static void fcoe_set_port_id(struct fc_lport *lport,
2765 u32 port_id, struct fc_frame *fp)
2766{
2767 struct fcoe_port *port = lport_priv(lport);
Bhanu Prakash Gollapudi8597ae82011-01-28 16:05:37 -08002768 struct fcoe_interface *fcoe = port->priv;
Joe Eykholt7d65b0d2010-03-12 16:08:02 -08002769
2770 if (fp && fc_frame_payload_op(fp) == ELS_FLOGI)
2771 fcoe_ctlr_recv_flogi(&fcoe->ctlr, lport, fp);
2772}