blob: b5d92fc93c7023f6d0f395c5ce44c4589afeabea [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 Love8d55e502012-05-22 19:06:26 -070044#include <scsi/fc/fc_fcoe.h>
Robert Love85b4aa42008-12-09 15:10:24 -080045
46#include <scsi/libfc.h>
47#include <scsi/fc_frame.h>
48#include <scsi/libfcoe.h>
Robert Love85b4aa42008-12-09 15:10:24 -080049
Vasu Devfdd78022009-03-17 11:42:24 -070050#include "fcoe.h"
51
Robert Love85b4aa42008-12-09 15:10:24 -080052MODULE_AUTHOR("Open-FCoE.org");
53MODULE_DESCRIPTION("FCoE");
Vasu Dev9b34ecf2009-03-17 11:42:13 -070054MODULE_LICENSE("GPL v2");
Robert Love85b4aa42008-12-09 15:10:24 -080055
Yi Zou05cc7392009-08-25 13:59:03 -070056/* Performance tuning parameters for fcoe */
Vasu Dev860eca22011-09-27 21:38:18 -070057static unsigned int fcoe_ddp_min = 4096;
Yi Zou05cc7392009-08-25 13:59:03 -070058module_param_named(ddp_min, fcoe_ddp_min, uint, S_IRUGO | S_IWUSR);
59MODULE_PARM_DESC(ddp_min, "Minimum I/O size in bytes for " \
60 "Direct Data Placement (DDP).");
61
Bart Van Assche7c9c6842012-01-13 17:26:25 -080062unsigned int fcoe_debug_logging;
63module_param_named(debug_logging, fcoe_debug_logging, int, S_IRUGO|S_IWUSR);
64MODULE_PARM_DESC(debug_logging, "a bit mask of logging levels");
65
66static DEFINE_MUTEX(fcoe_config_mutex);
Chris Leechdfc1d0f2009-08-25 14:00:13 -070067
Tejun Heo2ca32b42011-01-28 16:05:32 -080068static struct workqueue_struct *fcoe_wq;
69
Joe Eykholte7a51992009-08-25 14:04:08 -070070/* fcoe_percpu_clean completion. Waiter protected by fcoe_create_mutex */
71static DECLARE_COMPLETION(fcoe_flush_completion);
72
Robert Love85b4aa42008-12-09 15:10:24 -080073/* fcoe host list */
Chris Leech090eb6c2009-08-25 14:00:28 -070074/* must only by accessed under the RTNL mutex */
Bart Van Assche7c9c6842012-01-13 17:26:25 -080075static LIST_HEAD(fcoe_hostlist);
76static DEFINE_PER_CPU(struct fcoe_percpu_s, fcoe_percpu);
Robert Love85b4aa42008-12-09 15:10:24 -080077
Chris Leechdd3fd722009-04-21 16:27:36 -070078/* Function Prototypes */
Robert Love1875f272009-11-03 11:47:50 -080079static int fcoe_reset(struct Scsi_Host *);
Vasu Devfdd78022009-03-17 11:42:24 -070080static int fcoe_xmit(struct fc_lport *, struct fc_frame *);
81static int fcoe_rcv(struct sk_buff *, struct net_device *,
82 struct packet_type *, struct net_device *);
Robert Love1875f272009-11-03 11:47:50 -080083static int fcoe_percpu_receive_thread(void *);
Robert Love1875f272009-11-03 11:47:50 -080084static void fcoe_percpu_clean(struct fc_lport *);
85static 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 *);
Neil Hormanf9184df2013-01-15 14:34:40 -050089static void fcoe_hostlist_del(const struct fc_lport *);
Vasu Devfdd78022009-03-17 11:42:24 -070090
Robert Love85b4aa42008-12-09 15:10:24 -080091static int fcoe_device_notification(struct notifier_block *, ulong, void *);
92static void fcoe_dev_setup(void);
93static void fcoe_dev_cleanup(void);
Robert Love1875f272009-11-03 11:47:50 -080094static struct fcoe_interface
95*fcoe_hostlist_lookup_port(const struct net_device *);
Robert Love85b4aa42008-12-09 15:10:24 -080096
Robert Love1875f272009-11-03 11:47:50 -080097static int fcoe_fip_recv(struct sk_buff *, struct net_device *,
98 struct packet_type *, struct net_device *);
99
100static void fcoe_fip_send(struct fcoe_ctlr *, struct sk_buff *);
101static void fcoe_update_src_mac(struct fc_lport *, u8 *);
102static u8 *fcoe_get_src_mac(struct fc_lport *);
103static void fcoe_destroy_work(struct work_struct *);
104
105static int fcoe_ddp_setup(struct fc_lport *, u16, struct scatterlist *,
106 unsigned int);
107static int fcoe_ddp_done(struct fc_lport *, u16);
Yi Zou71f89492011-06-20 16:59:10 -0700108static int fcoe_ddp_target(struct fc_lport *, u16, struct scatterlist *,
109 unsigned int);
Robert Love1875f272009-11-03 11:47:50 -0800110static int fcoe_cpu_callback(struct notifier_block *, unsigned long, void *);
john fastabend6f6c2aa2011-11-18 13:35:56 -0800111static int fcoe_dcb_app_notification(struct notifier_block *notifier,
112 ulong event, void *ptr);
Robert Love1875f272009-11-03 11:47:50 -0800113
Yi Zou78a58242011-01-28 16:05:16 -0800114static bool fcoe_match(struct net_device *netdev);
115static int fcoe_create(struct net_device *netdev, enum fip_state fip_mode);
116static int fcoe_destroy(struct net_device *netdev);
117static int fcoe_enable(struct net_device *netdev);
118static int fcoe_disable(struct net_device *netdev);
Robert Love1875f272009-11-03 11:47:50 -0800119
Robert Love435c8662012-11-27 06:53:35 +0000120/* fcoe_syfs control interface handlers */
121static int fcoe_ctlr_alloc(struct net_device *netdev);
122static int fcoe_ctlr_enabled(struct fcoe_ctlr_device *cdev);
123
124
Robert Love1875f272009-11-03 11:47:50 -0800125static struct fc_seq *fcoe_elsct_send(struct fc_lport *,
126 u32 did, struct fc_frame *,
127 unsigned int op,
128 void (*resp)(struct fc_seq *,
129 struct fc_frame *,
130 void *),
131 void *, u32 timeout);
Chris Leech859b7b62009-11-20 14:54:47 -0800132static void fcoe_recv_frame(struct sk_buff *skb);
Robert Love1875f272009-11-03 11:47:50 -0800133
Robert Love1875f272009-11-03 11:47:50 -0800134/* notification function for packets from net device */
Robert Love85b4aa42008-12-09 15:10:24 -0800135static struct notifier_block fcoe_notifier = {
136 .notifier_call = fcoe_device_notification,
137};
138
Robert Love1875f272009-11-03 11:47:50 -0800139/* notification function for CPU hotplug events */
140static struct notifier_block fcoe_cpu_notifier = {
141 .notifier_call = fcoe_cpu_callback,
142};
143
john fastabend6f6c2aa2011-11-18 13:35:56 -0800144/* notification function for DCB events */
145static struct notifier_block dcb_notifier = {
146 .notifier_call = fcoe_dcb_app_notification,
147};
148
Yi Zou8ca86f82011-01-28 16:05:11 -0800149static struct scsi_transport_template *fcoe_nport_scsi_transport;
150static struct scsi_transport_template *fcoe_vport_scsi_transport;
Vasu Dev7f349142009-03-27 09:06:31 -0700151
Robert Love1875f272009-11-03 11:47:50 -0800152static int fcoe_vport_destroy(struct fc_vport *);
153static int fcoe_vport_create(struct fc_vport *, bool disabled);
154static int fcoe_vport_disable(struct fc_vport *, bool disable);
155static void fcoe_set_vport_symbolic_name(struct fc_vport *);
Joe Eykholt7d65b0d2010-03-12 16:08:02 -0800156static void fcoe_set_port_id(struct fc_lport *, u32, struct fc_frame *);
Robert Love8d55e502012-05-22 19:06:26 -0700157static void fcoe_fcf_get_vlan_id(struct fcoe_fcf_device *);
158
159static struct fcoe_sysfs_function_template fcoe_sysfs_templ = {
Robert Love435c8662012-11-27 06:53:35 +0000160 .set_fcoe_ctlr_mode = fcoe_ctlr_set_fip_mode,
161 .set_fcoe_ctlr_enabled = fcoe_ctlr_enabled,
Robert Love8d55e502012-05-22 19:06:26 -0700162 .get_fcoe_ctlr_link_fail = fcoe_ctlr_get_lesb,
163 .get_fcoe_ctlr_vlink_fail = fcoe_ctlr_get_lesb,
164 .get_fcoe_ctlr_miss_fka = fcoe_ctlr_get_lesb,
165 .get_fcoe_ctlr_symb_err = fcoe_ctlr_get_lesb,
166 .get_fcoe_ctlr_err_block = fcoe_ctlr_get_lesb,
167 .get_fcoe_ctlr_fcs_error = fcoe_ctlr_get_lesb,
168
169 .get_fcoe_fcf_selected = fcoe_fcf_get_selected,
170 .get_fcoe_fcf_vlan_id = fcoe_fcf_get_vlan_id,
171};
Robert Love1875f272009-11-03 11:47:50 -0800172
173static struct libfc_function_template fcoe_libfc_fcn_templ = {
174 .frame_send = fcoe_xmit,
175 .ddp_setup = fcoe_ddp_setup,
176 .ddp_done = fcoe_ddp_done,
Yi Zou71f89492011-06-20 16:59:10 -0700177 .ddp_target = fcoe_ddp_target,
Robert Love1875f272009-11-03 11:47:50 -0800178 .elsct_send = fcoe_elsct_send,
Yi Zoub84056b2009-11-20 14:55:19 -0800179 .get_lesb = fcoe_get_lesb,
Joe Eykholt7d65b0d2010-03-12 16:08:02 -0800180 .lport_set_port_id = fcoe_set_port_id,
Robert Love1875f272009-11-03 11:47:50 -0800181};
Chris Leech9a057532009-11-03 11:46:40 -0800182
Bart Van Assche7c9c6842012-01-13 17:26:25 -0800183static struct fc_function_template fcoe_nport_fc_functions = {
Vasu Dev7f349142009-03-27 09:06:31 -0700184 .show_host_node_name = 1,
185 .show_host_port_name = 1,
186 .show_host_supported_classes = 1,
187 .show_host_supported_fc4s = 1,
188 .show_host_active_fc4s = 1,
189 .show_host_maxframe_size = 1,
Neerav Parikh9f71af22012-01-22 17:30:16 -0800190 .show_host_serial_number = 1,
191 .show_host_manufacturer = 1,
192 .show_host_model = 1,
193 .show_host_model_description = 1,
194 .show_host_hardware_version = 1,
195 .show_host_driver_version = 1,
196 .show_host_firmware_version = 1,
197 .show_host_optionrom_version = 1,
Vasu Dev7f349142009-03-27 09:06:31 -0700198
199 .show_host_port_id = 1,
200 .show_host_supported_speeds = 1,
201 .get_host_speed = fc_get_host_speed,
202 .show_host_speed = 1,
203 .show_host_port_type = 1,
204 .get_host_port_state = fc_get_host_port_state,
205 .show_host_port_state = 1,
206 .show_host_symbolic_name = 1,
207
208 .dd_fcrport_size = sizeof(struct fc_rport_libfc_priv),
209 .show_rport_maxframe_size = 1,
210 .show_rport_supported_classes = 1,
211
212 .show_host_fabric_name = 1,
213 .show_starget_node_name = 1,
214 .show_starget_port_name = 1,
215 .show_starget_port_id = 1,
216 .set_rport_dev_loss_tmo = fc_set_rport_loss_tmo,
217 .show_rport_dev_loss_tmo = 1,
218 .get_fc_host_stats = fc_get_host_stats,
219 .issue_fc_host_lip = fcoe_reset,
220
221 .terminate_rport_io = fc_rport_terminate_io,
Chris Leech9a057532009-11-03 11:46:40 -0800222
223 .vport_create = fcoe_vport_create,
224 .vport_delete = fcoe_vport_destroy,
225 .vport_disable = fcoe_vport_disable,
Chris Leechdc8596d2009-11-03 11:47:18 -0800226 .set_vport_symbolic_name = fcoe_set_vport_symbolic_name,
Steve Maa51ab392009-11-03 11:47:34 -0800227
228 .bsg_request = fc_lport_bsg_request,
Vasu Dev7f349142009-03-27 09:06:31 -0700229};
230
Bart Van Assche7c9c6842012-01-13 17:26:25 -0800231static struct fc_function_template fcoe_vport_fc_functions = {
Chris Leeche9084bb2009-11-03 11:46:34 -0800232 .show_host_node_name = 1,
233 .show_host_port_name = 1,
234 .show_host_supported_classes = 1,
235 .show_host_supported_fc4s = 1,
236 .show_host_active_fc4s = 1,
237 .show_host_maxframe_size = 1,
Neerav Parikh7e5adcf2012-02-10 17:18:31 -0800238 .show_host_serial_number = 1,
239 .show_host_manufacturer = 1,
240 .show_host_model = 1,
241 .show_host_model_description = 1,
242 .show_host_hardware_version = 1,
243 .show_host_driver_version = 1,
244 .show_host_firmware_version = 1,
245 .show_host_optionrom_version = 1,
Chris Leeche9084bb2009-11-03 11:46:34 -0800246
247 .show_host_port_id = 1,
248 .show_host_supported_speeds = 1,
249 .get_host_speed = fc_get_host_speed,
250 .show_host_speed = 1,
251 .show_host_port_type = 1,
252 .get_host_port_state = fc_get_host_port_state,
253 .show_host_port_state = 1,
254 .show_host_symbolic_name = 1,
255
256 .dd_fcrport_size = sizeof(struct fc_rport_libfc_priv),
257 .show_rport_maxframe_size = 1,
258 .show_rport_supported_classes = 1,
259
260 .show_host_fabric_name = 1,
261 .show_starget_node_name = 1,
262 .show_starget_port_name = 1,
263 .show_starget_port_id = 1,
264 .set_rport_dev_loss_tmo = fc_set_rport_loss_tmo,
265 .show_rport_dev_loss_tmo = 1,
266 .get_fc_host_stats = fc_get_host_stats,
267 .issue_fc_host_lip = fcoe_reset,
268
269 .terminate_rport_io = fc_rport_terminate_io,
Steve Maa51ab392009-11-03 11:47:34 -0800270
271 .bsg_request = fc_lport_bsg_request,
Chris Leeche9084bb2009-11-03 11:46:34 -0800272};
273
Vasu Dev7f349142009-03-27 09:06:31 -0700274static struct scsi_host_template fcoe_shost_template = {
275 .module = THIS_MODULE,
276 .name = "FCoE Driver",
277 .proc_name = FCOE_NAME,
278 .queuecommand = fc_queuecommand,
279 .eh_abort_handler = fc_eh_abort,
280 .eh_device_reset_handler = fc_eh_device_reset,
281 .eh_host_reset_handler = fc_eh_host_reset,
282 .slave_alloc = fc_slave_alloc,
283 .change_queue_depth = fc_change_queue_depth,
284 .change_queue_type = fc_change_queue_type,
285 .this_id = -1,
Vasu Dev14caf442009-10-15 17:46:55 -0700286 .cmd_per_lun = 3,
Vasu Dev7f349142009-03-27 09:06:31 -0700287 .can_queue = FCOE_MAX_OUTSTANDING_COMMANDS,
288 .use_clustering = ENABLE_CLUSTERING,
289 .sg_tablesize = SG_ALL,
290 .max_sectors = 0xffff,
291};
292
Chris Leech54b649f2009-08-25 14:00:07 -0700293/**
Robert Love1875f272009-11-03 11:47:50 -0800294 * fcoe_interface_setup() - Setup a FCoE interface
295 * @fcoe: The new FCoE interface
296 * @netdev: The net device that the fcoe interface is on
Chris Leech54b649f2009-08-25 14:00:07 -0700297 *
298 * Returns : 0 for success
Chris Leech2e70e242009-08-25 14:00:23 -0700299 * Locking: must be called with the RTNL mutex held
Chris Leech54b649f2009-08-25 14:00:07 -0700300 */
301static int fcoe_interface_setup(struct fcoe_interface *fcoe,
302 struct net_device *netdev)
303{
Robert Love619fe4b2012-05-22 19:06:10 -0700304 struct fcoe_ctlr *fip = fcoe_to_ctlr(fcoe);
Chris Leech54b649f2009-08-25 14:00:07 -0700305 struct netdev_hw_addr *ha;
Yi Zou5bab87e2009-11-03 11:49:43 -0800306 struct net_device *real_dev;
Chris Leech54b649f2009-08-25 14:00:07 -0700307 u8 flogi_maddr[ETH_ALEN];
Yi Zoub7a727f2009-10-21 16:28:03 -0700308 const struct net_device_ops *ops;
Chris Leech54b649f2009-08-25 14:00:07 -0700309
310 fcoe->netdev = netdev;
311
Yi Zoub7a727f2009-10-21 16:28:03 -0700312 /* Let LLD initialize for FCoE */
313 ops = netdev->netdev_ops;
314 if (ops->ndo_fcoe_enable) {
315 if (ops->ndo_fcoe_enable(netdev))
316 FCOE_NETDEV_DBG(netdev, "Failed to enable FCoE"
317 " specific feature for LLD.\n");
318 }
319
Chris Leech54b649f2009-08-25 14:00:07 -0700320 /* Do not support for bonding device */
Jiri Pirkocc8bdf02011-03-01 20:05:35 +0000321 if (netdev->priv_flags & IFF_BONDING && netdev->flags & IFF_MASTER) {
john fastabend59d92512009-11-03 11:48:44 -0800322 FCOE_NETDEV_DBG(netdev, "Bonded interfaces not supported\n");
Chris Leech54b649f2009-08-25 14:00:07 -0700323 return -EOPNOTSUPP;
324 }
325
326 /* look for SAN MAC address, if multiple SAN MACs exist, only
327 * use the first one for SPMA */
Yi Zou5bab87e2009-11-03 11:49:43 -0800328 real_dev = (netdev->priv_flags & IFF_802_1Q_VLAN) ?
329 vlan_dev_real_dev(netdev) : netdev;
Vasu Devd1483bb2011-09-27 21:38:13 -0700330 fcoe->realdev = real_dev;
Chris Leech54b649f2009-08-25 14:00:07 -0700331 rcu_read_lock();
Yi Zou5bab87e2009-11-03 11:49:43 -0800332 for_each_dev_addr(real_dev, ha) {
Chris Leech54b649f2009-08-25 14:00:07 -0700333 if ((ha->type == NETDEV_HW_ADDR_T_SAN) &&
Yi Zoubf361702009-11-03 11:49:38 -0800334 (is_valid_ether_addr(ha->addr))) {
Chris Leech54b649f2009-08-25 14:00:07 -0700335 memcpy(fip->ctl_src_addr, ha->addr, ETH_ALEN);
336 fip->spma = 1;
337 break;
338 }
339 }
340 rcu_read_unlock();
341
342 /* setup Source Mac Address */
343 if (!fip->spma)
344 memcpy(fip->ctl_src_addr, netdev->dev_addr, netdev->addr_len);
345
346 /*
347 * Add FCoE MAC address as second unicast MAC address
348 * or enter promiscuous mode if not capable of listening
349 * for multiple unicast MACs.
350 */
Chris Leech54b649f2009-08-25 14:00:07 -0700351 memcpy(flogi_maddr, (u8[6]) FC_FCOE_FLOGI_MAC, ETH_ALEN);
Jiri Pirkoa748ee22010-04-01 21:22:09 +0000352 dev_uc_add(netdev, flogi_maddr);
Chris Leech54b649f2009-08-25 14:00:07 -0700353 if (fip->spma)
Jiri Pirkoa748ee22010-04-01 21:22:09 +0000354 dev_uc_add(netdev, fip->ctl_src_addr);
Joe Eykholte10f8c62010-07-20 15:20:30 -0700355 if (fip->mode == FIP_MODE_VN2VN) {
356 dev_mc_add(netdev, FIP_ALL_VN2VN_MACS);
357 dev_mc_add(netdev, FIP_ALL_P2P_MACS);
358 } else
359 dev_mc_add(netdev, FIP_ALL_ENODE_MACS);
Chris Leech54b649f2009-08-25 14:00:07 -0700360
361 /*
362 * setup the receive function from ethernet driver
363 * on the ethertype for the given device
364 */
365 fcoe->fcoe_packet_type.func = fcoe_rcv;
366 fcoe->fcoe_packet_type.type = __constant_htons(ETH_P_FCOE);
367 fcoe->fcoe_packet_type.dev = netdev;
368 dev_add_pack(&fcoe->fcoe_packet_type);
369
370 fcoe->fip_packet_type.func = fcoe_fip_recv;
371 fcoe->fip_packet_type.type = htons(ETH_P_FIP);
372 fcoe->fip_packet_type.dev = netdev;
373 dev_add_pack(&fcoe->fip_packet_type);
374
375 return 0;
376}
377
Vasu Dev7f349142009-03-27 09:06:31 -0700378/**
Robert Love1875f272009-11-03 11:47:50 -0800379 * fcoe_interface_create() - Create a FCoE interface on a net device
380 * @netdev: The net device to create the FCoE interface on
Joe Eykholt1dd454d2010-07-20 15:20:46 -0700381 * @fip_mode: The mode to use for FIP
Chris Leech030f4e02009-08-25 14:00:02 -0700382 *
383 * Returns: pointer to a struct fcoe_interface or NULL on error
384 */
Joe Eykholt1dd454d2010-07-20 15:20:46 -0700385static struct fcoe_interface *fcoe_interface_create(struct net_device *netdev,
386 enum fip_state fip_mode)
Chris Leech030f4e02009-08-25 14:00:02 -0700387{
Robert Love8d55e502012-05-22 19:06:26 -0700388 struct fcoe_ctlr_device *ctlr_dev;
Robert Love619fe4b2012-05-22 19:06:10 -0700389 struct fcoe_ctlr *ctlr;
Chris Leech030f4e02009-08-25 14:00:02 -0700390 struct fcoe_interface *fcoe;
Robert Love619fe4b2012-05-22 19:06:10 -0700391 int size;
john fastabend59d92512009-11-03 11:48:44 -0800392 int err;
Chris Leech030f4e02009-08-25 14:00:02 -0700393
Robert Love7287fb92011-01-28 16:03:47 -0800394 if (!try_module_get(THIS_MODULE)) {
395 FCOE_NETDEV_DBG(netdev,
396 "Could not get a reference to the module\n");
397 fcoe = ERR_PTR(-EBUSY);
398 goto out;
399 }
400
Robert Love619fe4b2012-05-22 19:06:10 -0700401 size = sizeof(struct fcoe_ctlr) + sizeof(struct fcoe_interface);
Robert Love8d55e502012-05-22 19:06:26 -0700402 ctlr_dev = fcoe_ctlr_device_add(&netdev->dev, &fcoe_sysfs_templ,
403 size);
404 if (!ctlr_dev) {
405 FCOE_DBG("Failed to add fcoe_ctlr_device\n");
Robert Love7287fb92011-01-28 16:03:47 -0800406 fcoe = ERR_PTR(-ENOMEM);
Robert Love6f687942012-02-10 17:18:36 -0800407 goto out_putmod;
Chris Leech030f4e02009-08-25 14:00:02 -0700408 }
409
Robert Love8d55e502012-05-22 19:06:26 -0700410 ctlr = fcoe_ctlr_device_priv(ctlr_dev);
411 fcoe = fcoe_ctlr_priv(ctlr);
412
Chris Leech2e70e242009-08-25 14:00:23 -0700413 dev_hold(netdev);
Chris Leech54b649f2009-08-25 14:00:07 -0700414
415 /*
416 * Initialize FIP.
417 */
Robert Love619fe4b2012-05-22 19:06:10 -0700418 fcoe_ctlr_init(ctlr, fip_mode);
419 ctlr->send = fcoe_fip_send;
420 ctlr->update_mac = fcoe_update_src_mac;
421 ctlr->get_src_addr = fcoe_get_src_mac;
Chris Leech54b649f2009-08-25 14:00:07 -0700422
john fastabend59d92512009-11-03 11:48:44 -0800423 err = fcoe_interface_setup(fcoe, netdev);
424 if (err) {
Robert Love619fe4b2012-05-22 19:06:10 -0700425 fcoe_ctlr_destroy(ctlr);
Robert Love8d55e502012-05-22 19:06:26 -0700426 fcoe_ctlr_device_delete(ctlr_dev);
john fastabend59d92512009-11-03 11:48:44 -0800427 dev_put(netdev);
Robert Love7287fb92011-01-28 16:03:47 -0800428 fcoe = ERR_PTR(err);
Robert Love6f687942012-02-10 17:18:36 -0800429 goto out_putmod;
john fastabend59d92512009-11-03 11:48:44 -0800430 }
Chris Leech030f4e02009-08-25 14:00:02 -0700431
Robert Love7287fb92011-01-28 16:03:47 -0800432 goto out;
433
Robert Love6f687942012-02-10 17:18:36 -0800434out_putmod:
Robert Love7287fb92011-01-28 16:03:47 -0800435 module_put(THIS_MODULE);
436out:
Chris Leech030f4e02009-08-25 14:00:02 -0700437 return fcoe;
438}
439
440/**
Vasu Dev433eba02012-04-20 12:16:27 -0700441 * fcoe_interface_remove() - remove FCoE interface from netdev
Vasu Devf04ca1b2011-04-01 16:06:45 -0700442 * @fcoe: The FCoE interface to be cleaned up
443 *
444 * Caller must be holding the RTNL mutex
445 */
Vasu Dev433eba02012-04-20 12:16:27 -0700446static void fcoe_interface_remove(struct fcoe_interface *fcoe)
Vasu Devf04ca1b2011-04-01 16:06:45 -0700447{
448 struct net_device *netdev = fcoe->netdev;
Robert Love619fe4b2012-05-22 19:06:10 -0700449 struct fcoe_ctlr *fip = fcoe_to_ctlr(fcoe);
Vasu Devf04ca1b2011-04-01 16:06:45 -0700450 u8 flogi_maddr[ETH_ALEN];
451 const struct net_device_ops *ops;
Vasu Devf04ca1b2011-04-01 16:06:45 -0700452
453 /*
454 * Don't listen for Ethernet packets anymore.
455 * synchronize_net() ensures that the packet handlers are not running
456 * on another CPU. dev_remove_pack() would do that, this calls the
457 * unsyncronized version __dev_remove_pack() to avoid multiple delays.
458 */
459 __dev_remove_pack(&fcoe->fcoe_packet_type);
460 __dev_remove_pack(&fcoe->fip_packet_type);
461 synchronize_net();
462
463 /* Delete secondary MAC addresses */
464 memcpy(flogi_maddr, (u8[6]) FC_FCOE_FLOGI_MAC, ETH_ALEN);
465 dev_uc_del(netdev, flogi_maddr);
466 if (fip->spma)
467 dev_uc_del(netdev, fip->ctl_src_addr);
468 if (fip->mode == FIP_MODE_VN2VN) {
469 dev_mc_del(netdev, FIP_ALL_VN2VN_MACS);
470 dev_mc_del(netdev, FIP_ALL_P2P_MACS);
471 } else
472 dev_mc_del(netdev, FIP_ALL_ENODE_MACS);
473
Vasu Devf04ca1b2011-04-01 16:06:45 -0700474 /* Tell the LLD we are done w/ FCoE */
475 ops = netdev->netdev_ops;
476 if (ops->ndo_fcoe_disable) {
477 if (ops->ndo_fcoe_disable(netdev))
478 FCOE_NETDEV_DBG(netdev, "Failed to disable FCoE"
479 " specific feature for LLD.\n");
480 }
Vasu Dev433eba02012-04-20 12:16:27 -0700481 fcoe->removed = 1;
482}
Neerav Parikhb2085a42011-06-20 16:59:51 -0700483
Vasu Dev433eba02012-04-20 12:16:27 -0700484
485/**
486 * fcoe_interface_cleanup() - Clean up a FCoE interface
487 * @fcoe: The FCoE interface to be cleaned up
488 */
489static void fcoe_interface_cleanup(struct fcoe_interface *fcoe)
490{
491 struct net_device *netdev = fcoe->netdev;
Robert Love619fe4b2012-05-22 19:06:10 -0700492 struct fcoe_ctlr *fip = fcoe_to_ctlr(fcoe);
Robert Love8d55e502012-05-22 19:06:26 -0700493 struct fcoe_ctlr_device *ctlr_dev = fcoe_ctlr_to_ctlr_dev(fip);
Vasu Dev433eba02012-04-20 12:16:27 -0700494
495 rtnl_lock();
496 if (!fcoe->removed)
497 fcoe_interface_remove(fcoe);
Robert Love848e7d52011-08-25 12:40:47 -0700498 rtnl_unlock();
499
Neerav Parikhb2085a42011-06-20 16:59:51 -0700500 /* Release the self-reference taken during fcoe_interface_create() */
Robert Love1a8ef412012-02-10 17:18:46 -0800501 /* tear-down the FCoE controller */
502 fcoe_ctlr_destroy(fip);
Robert Love619fe4b2012-05-22 19:06:10 -0700503 scsi_host_put(fip->lp->host);
Robert Love8d55e502012-05-22 19:06:26 -0700504 fcoe_ctlr_device_delete(ctlr_dev);
Robert Love1a8ef412012-02-10 17:18:46 -0800505 dev_put(netdev);
506 module_put(THIS_MODULE);
Vasu Devf04ca1b2011-04-01 16:06:45 -0700507}
508
509/**
Robert Love1875f272009-11-03 11:47:50 -0800510 * fcoe_fip_recv() - Handler for received FIP frames
511 * @skb: The receive skb
512 * @netdev: The associated net device
513 * @ptype: The packet_type structure which was used to register this handler
514 * @orig_dev: The original net_device the the skb was received on.
515 * (in case dev is a bond)
Vasu Devab6b85c2009-05-17 12:33:08 +0000516 *
517 * Returns: 0 for success
518 */
Robert Love1875f272009-11-03 11:47:50 -0800519static int fcoe_fip_recv(struct sk_buff *skb, struct net_device *netdev,
Vasu Devab6b85c2009-05-17 12:33:08 +0000520 struct packet_type *ptype,
521 struct net_device *orig_dev)
522{
Chris Leech259ad852009-08-25 13:59:41 -0700523 struct fcoe_interface *fcoe;
Robert Love619fe4b2012-05-22 19:06:10 -0700524 struct fcoe_ctlr *ctlr;
Vasu Devab6b85c2009-05-17 12:33:08 +0000525
Chris Leech259ad852009-08-25 13:59:41 -0700526 fcoe = container_of(ptype, struct fcoe_interface, fip_packet_type);
Robert Love619fe4b2012-05-22 19:06:10 -0700527 ctlr = fcoe_to_ctlr(fcoe);
528 fcoe_ctlr_recv(ctlr, skb);
Vasu Devab6b85c2009-05-17 12:33:08 +0000529 return 0;
530}
531
532/**
Vasu Dev980f5152011-07-27 15:11:05 -0700533 * fcoe_port_send() - Send an Ethernet-encapsulated FIP/FCoE frame
534 * @port: The FCoE port
535 * @skb: The FIP/FCoE packet to be sent
536 */
537static void fcoe_port_send(struct fcoe_port *port, struct sk_buff *skb)
538{
539 if (port->fcoe_pending_queue.qlen)
540 fcoe_check_wait_queue(port->lport, skb);
541 else if (fcoe_start_io(skb))
542 fcoe_check_wait_queue(port->lport, skb);
543}
544
545/**
Robert Love1875f272009-11-03 11:47:50 -0800546 * fcoe_fip_send() - Send an Ethernet-encapsulated FIP frame
547 * @fip: The FCoE controller
548 * @skb: The FIP packet to be sent
Vasu Devab6b85c2009-05-17 12:33:08 +0000549 */
550static void fcoe_fip_send(struct fcoe_ctlr *fip, struct sk_buff *skb)
551{
Chris Leech3fe9a0b2009-08-25 13:59:46 -0700552 skb->dev = fcoe_from_ctlr(fip)->netdev;
Vasu Dev980f5152011-07-27 15:11:05 -0700553 fcoe_port_send(lport_priv(fip->lp), skb);
Vasu Devab6b85c2009-05-17 12:33:08 +0000554}
555
556/**
Robert Love1875f272009-11-03 11:47:50 -0800557 * fcoe_update_src_mac() - Update the Ethernet MAC filters
558 * @lport: The local port to update the source MAC on
559 * @addr: Unicast MAC address to add
Vasu Devab6b85c2009-05-17 12:33:08 +0000560 *
561 * Remove any previously-set unicast MAC filter.
562 * Add secondary FCoE MAC address filter for our OUI.
563 */
Chris Leech11b56182009-11-03 11:46:29 -0800564static void fcoe_update_src_mac(struct fc_lport *lport, u8 *addr)
Vasu Devab6b85c2009-05-17 12:33:08 +0000565{
Chris Leech11b56182009-11-03 11:46:29 -0800566 struct fcoe_port *port = lport_priv(lport);
Bhanu Prakash Gollapudi8597ae82011-01-28 16:05:37 -0800567 struct fcoe_interface *fcoe = port->priv;
Vasu Devab6b85c2009-05-17 12:33:08 +0000568
Chris Leech11b56182009-11-03 11:46:29 -0800569 if (!is_zero_ether_addr(port->data_src_addr))
Jiri Pirkoa748ee22010-04-01 21:22:09 +0000570 dev_uc_del(fcoe->netdev, port->data_src_addr);
Chris Leech11b56182009-11-03 11:46:29 -0800571 if (!is_zero_ether_addr(addr))
Jiri Pirkoa748ee22010-04-01 21:22:09 +0000572 dev_uc_add(fcoe->netdev, addr);
Chris Leech11b56182009-11-03 11:46:29 -0800573 memcpy(port->data_src_addr, addr, ETH_ALEN);
Vasu Devab6b85c2009-05-17 12:33:08 +0000574}
575
576/**
Chris Leech11b56182009-11-03 11:46:29 -0800577 * fcoe_get_src_mac() - return the Ethernet source address for an lport
578 * @lport: libfc lport
579 */
580static u8 *fcoe_get_src_mac(struct fc_lport *lport)
581{
582 struct fcoe_port *port = lport_priv(lport);
583
584 return port->data_src_addr;
585}
586
587/**
Robert Love1875f272009-11-03 11:47:50 -0800588 * fcoe_lport_config() - Set up a local port
589 * @lport: The local port to be setup
Vasu Dev7f349142009-03-27 09:06:31 -0700590 *
591 * Returns: 0 for success
592 */
Robert Love1875f272009-11-03 11:47:50 -0800593static int fcoe_lport_config(struct fc_lport *lport)
Vasu Dev7f349142009-03-27 09:06:31 -0700594{
Robert Love1875f272009-11-03 11:47:50 -0800595 lport->link_up = 0;
596 lport->qfull = 0;
597 lport->max_retry_count = 3;
598 lport->max_rport_retry_count = 3;
599 lport->e_d_tov = 2 * 1000; /* FC-FS default */
600 lport->r_a_tov = 2 * 2 * 1000;
601 lport->service_params = (FCP_SPPF_INIT_FCN | FCP_SPPF_RD_XRDY_DIS |
602 FCP_SPPF_RETRY | FCP_SPPF_CONF_COMPL);
603 lport->does_npiv = 1;
Vasu Dev7f349142009-03-27 09:06:31 -0700604
Robert Love1875f272009-11-03 11:47:50 -0800605 fc_lport_init_stats(lport);
Vasu Dev7f349142009-03-27 09:06:31 -0700606
607 /* lport fc_lport related configuration */
Robert Love1875f272009-11-03 11:47:50 -0800608 fc_lport_config(lport);
Vasu Dev7f349142009-03-27 09:06:31 -0700609
610 /* offload related configuration */
Robert Love1875f272009-11-03 11:47:50 -0800611 lport->crc_offload = 0;
612 lport->seq_offload = 0;
613 lport->lro_enabled = 0;
614 lport->lro_xid = 0;
615 lport->lso_max = 0;
Vasu Dev7f349142009-03-27 09:06:31 -0700616
617 return 0;
618}
619
620/**
Yi Zou54a5b212010-07-20 15:21:17 -0700621 * fcoe_netdev_features_change - Updates the lport's offload flags based
622 * on the LLD netdev's FCoE feature flags
623 */
624static void fcoe_netdev_features_change(struct fc_lport *lport,
625 struct net_device *netdev)
626{
627 mutex_lock(&lport->lp_mutex);
628
629 if (netdev->features & NETIF_F_SG)
630 lport->sg_supp = 1;
631 else
632 lport->sg_supp = 0;
633
634 if (netdev->features & NETIF_F_FCOE_CRC) {
635 lport->crc_offload = 1;
636 FCOE_NETDEV_DBG(netdev, "Supports FCCRC offload\n");
637 } else {
638 lport->crc_offload = 0;
639 }
640
641 if (netdev->features & NETIF_F_FSO) {
642 lport->seq_offload = 1;
643 lport->lso_max = netdev->gso_max_size;
644 FCOE_NETDEV_DBG(netdev, "Supports LSO for max len 0x%x\n",
645 lport->lso_max);
646 } else {
647 lport->seq_offload = 0;
648 lport->lso_max = 0;
649 }
650
651 if (netdev->fcoe_ddp_xid) {
652 lport->lro_enabled = 1;
653 lport->lro_xid = netdev->fcoe_ddp_xid;
654 FCOE_NETDEV_DBG(netdev, "Supports LRO for max xid 0x%x\n",
655 lport->lro_xid);
656 } else {
657 lport->lro_enabled = 0;
658 lport->lro_xid = 0;
659 }
660
661 mutex_unlock(&lport->lp_mutex);
662}
663
664/**
Robert Love1875f272009-11-03 11:47:50 -0800665 * fcoe_netdev_config() - Set up net devive for SW FCoE
666 * @lport: The local port that is associated with the net device
667 * @netdev: The associated net device
Vasu Dev7f349142009-03-27 09:06:31 -0700668 *
Robert Love1875f272009-11-03 11:47:50 -0800669 * Must be called after fcoe_lport_config() as it will use local port mutex
Vasu Dev7f349142009-03-27 09:06:31 -0700670 *
Robert Love1875f272009-11-03 11:47:50 -0800671 * Returns: 0 for success
Vasu Dev7f349142009-03-27 09:06:31 -0700672 */
Robert Love1875f272009-11-03 11:47:50 -0800673static int fcoe_netdev_config(struct fc_lport *lport, struct net_device *netdev)
Vasu Dev7f349142009-03-27 09:06:31 -0700674{
675 u32 mfs;
676 u64 wwnn, wwpn;
Chris Leech25024982009-08-25 13:59:35 -0700677 struct fcoe_interface *fcoe;
Robert Love619fe4b2012-05-22 19:06:10 -0700678 struct fcoe_ctlr *ctlr;
Chris Leech014f5c32009-08-25 13:59:30 -0700679 struct fcoe_port *port;
Vasu Dev7f349142009-03-27 09:06:31 -0700680
681 /* Setup lport private data to point to fcoe softc */
Robert Love1875f272009-11-03 11:47:50 -0800682 port = lport_priv(lport);
Bhanu Prakash Gollapudi8597ae82011-01-28 16:05:37 -0800683 fcoe = port->priv;
Robert Love619fe4b2012-05-22 19:06:10 -0700684 ctlr = fcoe_to_ctlr(fcoe);
Vasu Dev7f349142009-03-27 09:06:31 -0700685
686 /*
687 * Determine max frame size based on underlying device and optional
688 * user-configured limit. If the MFS is too low, fcoe_link_ok()
689 * will return 0, so do this first.
690 */
Yi Zou7221d7e2009-10-21 16:27:52 -0700691 mfs = netdev->mtu;
692 if (netdev->features & NETIF_F_FCOE_MTU) {
693 mfs = FCOE_MTU;
694 FCOE_NETDEV_DBG(netdev, "Supports FCOE_MTU of %d bytes\n", mfs);
695 }
696 mfs -= (sizeof(struct fcoe_hdr) + sizeof(struct fcoe_crc_eof));
Robert Love1875f272009-11-03 11:47:50 -0800697 if (fc_set_mfs(lport, mfs))
Vasu Dev7f349142009-03-27 09:06:31 -0700698 return -EINVAL;
699
Vasu Dev7f349142009-03-27 09:06:31 -0700700 /* offload features support */
Yi Zou54a5b212010-07-20 15:21:17 -0700701 fcoe_netdev_features_change(lport, netdev);
Vasu Dev7f349142009-03-27 09:06:31 -0700702
Chris Leech014f5c32009-08-25 13:59:30 -0700703 skb_queue_head_init(&port->fcoe_pending_queue);
704 port->fcoe_pending_queue_active = 0;
Robert Love1875f272009-11-03 11:47:50 -0800705 setup_timer(&port->timer, fcoe_queue_timer, (unsigned long)lport);
Vasu Dev7f349142009-03-27 09:06:31 -0700706
Robert Love5e4f8fe2010-05-07 15:18:35 -0700707 fcoe_link_speed_update(lport);
708
Robert Love1875f272009-11-03 11:47:50 -0800709 if (!lport->vport) {
Yi Zoudcece412009-11-20 15:22:21 -0800710 if (fcoe_get_wwn(netdev, &wwnn, NETDEV_FCOE_WWNN))
Robert Love619fe4b2012-05-22 19:06:10 -0700711 wwnn = fcoe_wwn_from_mac(ctlr->ctl_src_addr, 1, 0);
Robert Love1875f272009-11-03 11:47:50 -0800712 fc_set_wwnn(lport, wwnn);
Yi Zoudcece412009-11-20 15:22:21 -0800713 if (fcoe_get_wwn(netdev, &wwpn, NETDEV_FCOE_WWPN))
Robert Love619fe4b2012-05-22 19:06:10 -0700714 wwpn = fcoe_wwn_from_mac(ctlr->ctl_src_addr,
Vasu Devcf4aebca2010-07-20 15:21:22 -0700715 2, 0);
Robert Love1875f272009-11-03 11:47:50 -0800716 fc_set_wwpn(lport, wwpn);
Chris Leech9a057532009-11-03 11:46:40 -0800717 }
Vasu Dev7f349142009-03-27 09:06:31 -0700718
Vasu Dev7f349142009-03-27 09:06:31 -0700719 return 0;
720}
721
722/**
Robert Love1875f272009-11-03 11:47:50 -0800723 * fcoe_shost_config() - Set up the SCSI host associated with a local port
724 * @lport: The local port
Robert Love1875f272009-11-03 11:47:50 -0800725 * @dev: The device associated with the SCSI host
Vasu Dev7f349142009-03-27 09:06:31 -0700726 *
727 * Must be called after fcoe_lport_config() and fcoe_netdev_config()
728 *
Robert Love1875f272009-11-03 11:47:50 -0800729 * Returns: 0 for success
Vasu Dev7f349142009-03-27 09:06:31 -0700730 */
Vasu Dev8ba00a42010-04-09 14:22:54 -0700731static int fcoe_shost_config(struct fc_lport *lport, struct device *dev)
Vasu Dev7f349142009-03-27 09:06:31 -0700732{
733 int rc = 0;
734
735 /* lport scsi host config */
Robert Love1875f272009-11-03 11:47:50 -0800736 lport->host->max_lun = FCOE_MAX_LUN;
737 lport->host->max_id = FCOE_MAX_FCP_TARGET;
738 lport->host->max_channel = 0;
Vasu Devda87bfa2010-04-09 14:22:59 -0700739 lport->host->max_cmd_len = FCOE_MAX_CMD_LEN;
740
Robert Love1875f272009-11-03 11:47:50 -0800741 if (lport->vport)
Yi Zou8ca86f82011-01-28 16:05:11 -0800742 lport->host->transportt = fcoe_vport_scsi_transport;
Chris Leeche9084bb2009-11-03 11:46:34 -0800743 else
Yi Zou8ca86f82011-01-28 16:05:11 -0800744 lport->host->transportt = fcoe_nport_scsi_transport;
Vasu Dev7f349142009-03-27 09:06:31 -0700745
746 /* add the new host to the SCSI-ml */
Robert Love1875f272009-11-03 11:47:50 -0800747 rc = scsi_add_host(lport->host, dev);
Vasu Dev7f349142009-03-27 09:06:31 -0700748 if (rc) {
Robert Love1875f272009-11-03 11:47:50 -0800749 FCOE_NETDEV_DBG(fcoe_netdev(lport), "fcoe_shost_config: "
Robert Loved5488eb2009-06-10 15:30:59 -0700750 "error on scsi_add_host\n");
Vasu Dev7f349142009-03-27 09:06:31 -0700751 return rc;
752 }
Chris Leech9a057532009-11-03 11:46:40 -0800753
Robert Love1875f272009-11-03 11:47:50 -0800754 if (!lport->vport)
Alexey Dobriyan4be929b2010-05-24 14:33:03 -0700755 fc_host_max_npiv_vports(lport->host) = USHRT_MAX;
Chris Leech9a057532009-11-03 11:46:40 -0800756
Robert Love1875f272009-11-03 11:47:50 -0800757 snprintf(fc_host_symbolic_name(lport->host), FC_SYMBOLIC_NAME_SIZE,
Chris Leech5baa17c2009-11-03 11:46:56 -0800758 "%s v%s over %s", FCOE_NAME, FCOE_VERSION,
Robert Love1875f272009-11-03 11:47:50 -0800759 fcoe_netdev(lport)->name);
Vasu Dev7f349142009-03-27 09:06:31 -0700760
761 return 0;
762}
763
Neerav Parikh6fef3902012-01-22 17:30:10 -0800764
765/**
766 * fcoe_fdmi_info() - Get FDMI related info from net devive for SW FCoE
767 * @lport: The local port that is associated with the net device
768 * @netdev: The associated net device
769 *
770 * Must be called after fcoe_shost_config() as it will use local port mutex
771 *
772 */
773static void fcoe_fdmi_info(struct fc_lport *lport, struct net_device *netdev)
774{
775 struct fcoe_interface *fcoe;
776 struct fcoe_port *port;
777 struct net_device *realdev;
778 int rc;
779 struct netdev_fcoe_hbainfo fdmi;
780
781 port = lport_priv(lport);
782 fcoe = port->priv;
783 realdev = fcoe->realdev;
784
785 if (!realdev)
786 return;
787
788 /* No FDMI state m/c for NPIV ports */
789 if (lport->vport)
790 return;
791
792 if (realdev->netdev_ops->ndo_fcoe_get_hbainfo) {
793 memset(&fdmi, 0, sizeof(fdmi));
794 rc = realdev->netdev_ops->ndo_fcoe_get_hbainfo(realdev,
795 &fdmi);
796 if (rc) {
797 printk(KERN_INFO "fcoe: Failed to retrieve FDMI "
798 "information from netdev.\n");
799 return;
800 }
801
802 snprintf(fc_host_serial_number(lport->host),
803 FC_SERIAL_NUMBER_SIZE,
804 "%s",
805 fdmi.serial_number);
806 snprintf(fc_host_manufacturer(lport->host),
807 FC_SERIAL_NUMBER_SIZE,
808 "%s",
809 fdmi.manufacturer);
810 snprintf(fc_host_model(lport->host),
811 FC_SYMBOLIC_NAME_SIZE,
812 "%s",
813 fdmi.model);
814 snprintf(fc_host_model_description(lport->host),
815 FC_SYMBOLIC_NAME_SIZE,
816 "%s",
817 fdmi.model_description);
818 snprintf(fc_host_hardware_version(lport->host),
819 FC_VERSION_STRING_SIZE,
820 "%s",
821 fdmi.hardware_version);
822 snprintf(fc_host_driver_version(lport->host),
823 FC_VERSION_STRING_SIZE,
824 "%s",
825 fdmi.driver_version);
826 snprintf(fc_host_optionrom_version(lport->host),
827 FC_VERSION_STRING_SIZE,
828 "%s",
829 fdmi.optionrom_version);
830 snprintf(fc_host_firmware_version(lport->host),
831 FC_VERSION_STRING_SIZE,
832 "%s",
833 fdmi.firmware_version);
834
835 /* Enable FDMI lport states */
836 lport->fdmi_enabled = 1;
837 } else {
838 lport->fdmi_enabled = 0;
839 printk(KERN_INFO "fcoe: No FDMI support.\n");
840 }
841}
842
Robert Love1875f272009-11-03 11:47:50 -0800843/**
844 * fcoe_oem_match() - The match routine for the offloaded exchange manager
845 * @fp: The I/O frame
Vasu Devd7179682009-07-29 17:05:21 -0700846 *
Robert Love1875f272009-11-03 11:47:50 -0800847 * This routine will be associated with an exchange manager (EM). When
848 * the libfc exchange handling code is looking for an EM to use it will
849 * call this routine and pass it the frame that it wishes to send. This
850 * routine will return True if the associated EM is to be used and False
851 * if the echange code should continue looking for an EM.
852 *
853 * The offload EM that this routine is associated with will handle any
854 * packets that are for SCSI read requests.
855 *
Kiran Patil1ff99182011-06-20 16:59:15 -0700856 * This has been enhanced to work when FCoE stack is operating in target
857 * mode.
858 *
Robert Love1875f272009-11-03 11:47:50 -0800859 * Returns: True for read types I/O, otherwise returns false.
Vasu Devd7179682009-07-29 17:05:21 -0700860 */
Bart Van Assche7c9c6842012-01-13 17:26:25 -0800861static bool fcoe_oem_match(struct fc_frame *fp)
Vasu Devd7179682009-07-29 17:05:21 -0700862{
Kiran Patil1ff99182011-06-20 16:59:15 -0700863 struct fc_frame_header *fh = fc_frame_header_get(fp);
864 struct fcp_cmnd *fcp;
865
866 if (fc_fcp_is_read(fr_fsp(fp)) &&
867 (fr_fsp(fp)->data_len > fcoe_ddp_min))
868 return true;
Yi Zoua762dce2012-01-13 17:26:15 -0800869 else if ((fr_fsp(fp) == NULL) &&
870 (fh->fh_r_ctl == FC_RCTL_DD_UNSOL_CMD) &&
871 (ntohs(fh->fh_rx_id) == FC_XID_UNKNOWN)) {
Kiran Patil1ff99182011-06-20 16:59:15 -0700872 fcp = fc_frame_payload_get(fp, sizeof(*fcp));
Yi Zoua762dce2012-01-13 17:26:15 -0800873 if ((fcp->fc_flags & FCP_CFL_WRDATA) &&
874 (ntohl(fcp->fc_dl) > fcoe_ddp_min))
Kiran Patil1ff99182011-06-20 16:59:15 -0700875 return true;
876 }
877 return false;
Vasu Devd7179682009-07-29 17:05:21 -0700878}
879
Vasu Dev7f349142009-03-27 09:06:31 -0700880/**
Robert Love1875f272009-11-03 11:47:50 -0800881 * fcoe_em_config() - Allocate and configure an exchange manager
882 * @lport: The local port that the new EM will be associated with
Vasu Dev7f349142009-03-27 09:06:31 -0700883 *
Robert Love1875f272009-11-03 11:47:50 -0800884 * Returns: 0 on success
Vasu Dev7f349142009-03-27 09:06:31 -0700885 */
Robert Love1875f272009-11-03 11:47:50 -0800886static inline int fcoe_em_config(struct fc_lport *lport)
Vasu Dev7f349142009-03-27 09:06:31 -0700887{
Robert Love1875f272009-11-03 11:47:50 -0800888 struct fcoe_port *port = lport_priv(lport);
Bhanu Prakash Gollapudi8597ae82011-01-28 16:05:37 -0800889 struct fcoe_interface *fcoe = port->priv;
Chris Leech25024982009-08-25 13:59:35 -0700890 struct fcoe_interface *oldfcoe = NULL;
Vasu Dev1d1b88d2009-07-29 17:05:45 -0700891 struct net_device *old_real_dev, *cur_real_dev;
Vasu Devd7179682009-07-29 17:05:21 -0700892 u16 min_xid = FCOE_MIN_XID;
893 u16 max_xid = FCOE_MAX_XID;
894
895 /*
896 * Check if need to allocate an em instance for
897 * offload exchange ids to be shared across all VN_PORTs/lport.
898 */
Robert Love1875f272009-11-03 11:47:50 -0800899 if (!lport->lro_enabled || !lport->lro_xid ||
900 (lport->lro_xid >= max_xid)) {
901 lport->lro_xid = 0;
Vasu Devd7179682009-07-29 17:05:21 -0700902 goto skip_oem;
903 }
904
905 /*
906 * Reuse existing offload em instance in case
Vasu Dev1d1b88d2009-07-29 17:05:45 -0700907 * it is already allocated on real eth device
Vasu Devd7179682009-07-29 17:05:21 -0700908 */
Chris Leech25024982009-08-25 13:59:35 -0700909 if (fcoe->netdev->priv_flags & IFF_802_1Q_VLAN)
910 cur_real_dev = vlan_dev_real_dev(fcoe->netdev);
Vasu Dev1d1b88d2009-07-29 17:05:45 -0700911 else
Chris Leech25024982009-08-25 13:59:35 -0700912 cur_real_dev = fcoe->netdev;
Vasu Dev1d1b88d2009-07-29 17:05:45 -0700913
Chris Leech25024982009-08-25 13:59:35 -0700914 list_for_each_entry(oldfcoe, &fcoe_hostlist, list) {
Chris Leech25024982009-08-25 13:59:35 -0700915 if (oldfcoe->netdev->priv_flags & IFF_802_1Q_VLAN)
916 old_real_dev = vlan_dev_real_dev(oldfcoe->netdev);
Vasu Dev1d1b88d2009-07-29 17:05:45 -0700917 else
Chris Leech25024982009-08-25 13:59:35 -0700918 old_real_dev = oldfcoe->netdev;
Vasu Dev1d1b88d2009-07-29 17:05:45 -0700919
920 if (cur_real_dev == old_real_dev) {
Chris Leech991cbb62009-08-25 13:59:51 -0700921 fcoe->oem = oldfcoe->oem;
Vasu Devd7179682009-07-29 17:05:21 -0700922 break;
923 }
924 }
925
Chris Leech991cbb62009-08-25 13:59:51 -0700926 if (fcoe->oem) {
Robert Love1875f272009-11-03 11:47:50 -0800927 if (!fc_exch_mgr_add(lport, fcoe->oem, fcoe_oem_match)) {
Vasu Devd7179682009-07-29 17:05:21 -0700928 printk(KERN_ERR "fcoe_em_config: failed to add "
929 "offload em:%p on interface:%s\n",
Chris Leech991cbb62009-08-25 13:59:51 -0700930 fcoe->oem, fcoe->netdev->name);
Vasu Devd7179682009-07-29 17:05:21 -0700931 return -ENOMEM;
932 }
933 } else {
Robert Love1875f272009-11-03 11:47:50 -0800934 fcoe->oem = fc_exch_mgr_alloc(lport, FC_CLASS_3,
935 FCOE_MIN_XID, lport->lro_xid,
936 fcoe_oem_match);
Chris Leech991cbb62009-08-25 13:59:51 -0700937 if (!fcoe->oem) {
Vasu Devd7179682009-07-29 17:05:21 -0700938 printk(KERN_ERR "fcoe_em_config: failed to allocate "
939 "em for offload exches on interface:%s\n",
Chris Leech25024982009-08-25 13:59:35 -0700940 fcoe->netdev->name);
Vasu Devd7179682009-07-29 17:05:21 -0700941 return -ENOMEM;
942 }
943 }
944
945 /*
946 * Exclude offload EM xid range from next EM xid range.
947 */
Robert Love1875f272009-11-03 11:47:50 -0800948 min_xid += lport->lro_xid + 1;
Vasu Devd7179682009-07-29 17:05:21 -0700949
950skip_oem:
Robert Love1875f272009-11-03 11:47:50 -0800951 if (!fc_exch_mgr_alloc(lport, FC_CLASS_3, min_xid, max_xid, NULL)) {
Vasu Devd7179682009-07-29 17:05:21 -0700952 printk(KERN_ERR "fcoe_em_config: failed to "
Chris Leech25024982009-08-25 13:59:35 -0700953 "allocate em on interface %s\n", fcoe->netdev->name);
Vasu Dev7f349142009-03-27 09:06:31 -0700954 return -ENOMEM;
Vasu Devd7179682009-07-29 17:05:21 -0700955 }
Vasu Dev7f349142009-03-27 09:06:31 -0700956
957 return 0;
958}
959
960/**
Robert Love1875f272009-11-03 11:47:50 -0800961 * fcoe_if_destroy() - Tear down a SW FCoE instance
962 * @lport: The local port to be destroyed
Vasu Dev34ce27b2010-05-07 15:18:46 -0700963 *
Vasu Dev7f349142009-03-27 09:06:31 -0700964 */
Chris Leechaf7f85d2009-08-25 13:59:24 -0700965static void fcoe_if_destroy(struct fc_lport *lport)
Vasu Dev7f349142009-03-27 09:06:31 -0700966{
Neerav Parikhb2085a42011-06-20 16:59:51 -0700967 struct fcoe_port *port = lport_priv(lport);
968 struct fcoe_interface *fcoe = port->priv;
969 struct net_device *netdev = fcoe->netdev;
970
971 FCOE_NETDEV_DBG(netdev, "Destroying interface\n");
972
973 /* Logout of the fabric */
974 fc_fabric_logoff(lport);
975
976 /* Cleanup the fc_lport */
977 fc_lport_destroy(lport);
978
979 /* Stop the transmit retry timer */
980 del_timer_sync(&port->timer);
981
982 /* Free existing transmit skbs */
983 fcoe_clean_pending_queue(lport);
984
985 rtnl_lock();
986 if (!is_zero_ether_addr(port->data_src_addr))
987 dev_uc_del(netdev, port->data_src_addr);
Vasu Dev433eba02012-04-20 12:16:27 -0700988 if (lport->vport)
989 synchronize_net();
990 else
991 fcoe_interface_remove(fcoe);
Neerav Parikhb2085a42011-06-20 16:59:51 -0700992 rtnl_unlock();
993
Chris Leech54b649f2009-08-25 14:00:07 -0700994 /* Free queued packets for the per-CPU receive threads */
995 fcoe_percpu_clean(lport);
996
Vasu Dev7f349142009-03-27 09:06:31 -0700997 /* Detach from the scsi-ml */
Chris Leechaf7f85d2009-08-25 13:59:24 -0700998 fc_remove_host(lport->host);
999 scsi_remove_host(lport->host);
Vasu Dev7f349142009-03-27 09:06:31 -07001000
Yi Zou80e736f2010-11-30 16:18:07 -08001001 /* Destroy lport scsi_priv */
1002 fc_fcp_destroy(lport);
1003
Vasu Dev7f349142009-03-27 09:06:31 -07001004 /* There are no more rports or I/O, free the EM */
Chris Leechaf7f85d2009-08-25 13:59:24 -07001005 fc_exch_mgr_free(lport);
Vasu Dev7f349142009-03-27 09:06:31 -07001006
Vasu Dev7f349142009-03-27 09:06:31 -07001007 /* Free memory used by statistical counters */
Chris Leechaf7f85d2009-08-25 13:59:24 -07001008 fc_lport_free_stats(lport);
Vasu Dev7f349142009-03-27 09:06:31 -07001009
Vasu Dev3cab4462012-04-20 12:16:38 -07001010 /*
1011 * Release the Scsi_Host for vport but hold on to
1012 * master lport until it fcoe interface fully cleaned-up.
1013 */
1014 if (lport->vport)
1015 scsi_host_put(lport->host);
Vasu Dev7f349142009-03-27 09:06:31 -07001016}
1017
Robert Love1875f272009-11-03 11:47:50 -08001018/**
1019 * fcoe_ddp_setup() - Call a LLD's ddp_setup through the net device
1020 * @lport: The local port to setup DDP for
1021 * @xid: The exchange ID for this DDP transfer
1022 * @sgl: The scatterlist describing this transfer
1023 * @sgc: The number of sg items
Vasu Dev7f349142009-03-27 09:06:31 -07001024 *
Robert Love1875f272009-11-03 11:47:50 -08001025 * Returns: 0 if the DDP context was not configured
Vasu Dev7f349142009-03-27 09:06:31 -07001026 */
Robert Love1875f272009-11-03 11:47:50 -08001027static int fcoe_ddp_setup(struct fc_lport *lport, u16 xid,
1028 struct scatterlist *sgl, unsigned int sgc)
Vasu Dev7f349142009-03-27 09:06:31 -07001029{
Robert Love1875f272009-11-03 11:47:50 -08001030 struct net_device *netdev = fcoe_netdev(lport);
Vasu Dev7f349142009-03-27 09:06:31 -07001031
Robert Love1875f272009-11-03 11:47:50 -08001032 if (netdev->netdev_ops->ndo_fcoe_ddp_setup)
1033 return netdev->netdev_ops->ndo_fcoe_ddp_setup(netdev,
1034 xid, sgl,
1035 sgc);
Vasu Dev7f349142009-03-27 09:06:31 -07001036
1037 return 0;
1038}
1039
Vasu Dev7f349142009-03-27 09:06:31 -07001040/**
Yi Zou71f89492011-06-20 16:59:10 -07001041 * fcoe_ddp_target() - Call a LLD's ddp_target through the net device
1042 * @lport: The local port to setup DDP for
1043 * @xid: The exchange ID for this DDP transfer
1044 * @sgl: The scatterlist describing this transfer
1045 * @sgc: The number of sg items
1046 *
1047 * Returns: 0 if the DDP context was not configured
1048 */
1049static int fcoe_ddp_target(struct fc_lport *lport, u16 xid,
1050 struct scatterlist *sgl, unsigned int sgc)
1051{
1052 struct net_device *netdev = fcoe_netdev(lport);
1053
1054 if (netdev->netdev_ops->ndo_fcoe_ddp_target)
1055 return netdev->netdev_ops->ndo_fcoe_ddp_target(netdev, xid,
1056 sgl, sgc);
1057
1058 return 0;
1059}
1060
1061
1062/**
Robert Love1875f272009-11-03 11:47:50 -08001063 * fcoe_ddp_done() - Call a LLD's ddp_done through the net device
1064 * @lport: The local port to complete DDP on
1065 * @xid: The exchange ID for this DDP transfer
Vasu Dev7f349142009-03-27 09:06:31 -07001066 *
Robert Love1875f272009-11-03 11:47:50 -08001067 * Returns: the length of data that have been completed by DDP
1068 */
1069static int fcoe_ddp_done(struct fc_lport *lport, u16 xid)
1070{
1071 struct net_device *netdev = fcoe_netdev(lport);
1072
1073 if (netdev->netdev_ops->ndo_fcoe_ddp_done)
1074 return netdev->netdev_ops->ndo_fcoe_ddp_done(netdev, xid);
1075 return 0;
1076}
1077
1078/**
1079 * fcoe_if_create() - Create a FCoE instance on an interface
1080 * @fcoe: The FCoE interface to create a local port on
1081 * @parent: The device pointer to be the parent in sysfs for the SCSI host
1082 * @npiv: Indicates if the port is a vport or not
Vasu Dev7f349142009-03-27 09:06:31 -07001083 *
Robert Love1875f272009-11-03 11:47:50 -08001084 * Creates a fc_lport instance and a Scsi_Host instance and configure them.
1085 *
1086 * Returns: The allocated fc_lport or an error pointer
Vasu Dev7f349142009-03-27 09:06:31 -07001087 */
Chris Leech030f4e02009-08-25 14:00:02 -07001088static struct fc_lport *fcoe_if_create(struct fcoe_interface *fcoe,
Chris Leech9a057532009-11-03 11:46:40 -08001089 struct device *parent, int npiv)
Vasu Dev7f349142009-03-27 09:06:31 -07001090{
Robert Love619fe4b2012-05-22 19:06:10 -07001091 struct fcoe_ctlr *ctlr = fcoe_to_ctlr(fcoe);
Robert Love1875f272009-11-03 11:47:50 -08001092 struct net_device *netdev = fcoe->netdev;
Vasu Dev72fa3962011-02-25 15:03:01 -08001093 struct fc_lport *lport, *n_port;
Chris Leech014f5c32009-08-25 13:59:30 -07001094 struct fcoe_port *port;
Vasu Dev72fa3962011-02-25 15:03:01 -08001095 struct Scsi_Host *shost;
Robert Love1875f272009-11-03 11:47:50 -08001096 int rc;
Chris Leech9a057532009-11-03 11:46:40 -08001097 /*
1098 * parent is only a vport if npiv is 1,
1099 * but we'll only use vport in that case so go ahead and set it
1100 */
1101 struct fc_vport *vport = dev_to_vport(parent);
Vasu Dev7f349142009-03-27 09:06:31 -07001102
Robert Loved5488eb2009-06-10 15:30:59 -07001103 FCOE_NETDEV_DBG(netdev, "Create Interface\n");
Vasu Dev7f349142009-03-27 09:06:31 -07001104
Vasu Dev72fa3962011-02-25 15:03:01 -08001105 if (!npiv)
1106 lport = libfc_host_alloc(&fcoe_shost_template, sizeof(*port));
1107 else
1108 lport = libfc_vport_create(vport, sizeof(*port));
1109
Chris Leech86221962009-11-03 11:46:08 -08001110 if (!lport) {
Chris Leech014f5c32009-08-25 13:59:30 -07001111 FCOE_NETDEV_DBG(netdev, "Could not allocate host structure\n");
1112 rc = -ENOMEM;
Chris Leech030f4e02009-08-25 14:00:02 -07001113 goto out;
Chris Leech014f5c32009-08-25 13:59:30 -07001114 }
Chris Leech014f5c32009-08-25 13:59:30 -07001115 port = lport_priv(lport);
Chris Leech2e70e242009-08-25 14:00:23 -07001116 port->lport = lport;
Bhanu Prakash Gollapudi8597ae82011-01-28 16:05:37 -08001117 port->priv = fcoe;
Yi Zou66524ec2012-12-06 06:23:43 +00001118 port->get_netdev = fcoe_netdev;
Bhanu Prakash Gollapudi8597ae82011-01-28 16:05:37 -08001119 port->max_queue_depth = FCOE_MAX_QUEUE_DEPTH;
1120 port->min_queue_depth = FCOE_MIN_QUEUE_DEPTH;
Chris Leech2e70e242009-08-25 14:00:23 -07001121 INIT_WORK(&port->destroy_work, fcoe_destroy_work);
Vasu Dev7f349142009-03-27 09:06:31 -07001122
Neil Hormanf9184df2013-01-15 14:34:40 -05001123 /*
1124 * Need to add the lport to the hostlist
1125 * so we catch NETDEV_CHANGE events.
1126 */
1127 fcoe_hostlist_add(lport);
1128
Robert Love1875f272009-11-03 11:47:50 -08001129 /* configure a fc_lport including the exchange manager */
Chris Leechaf7f85d2009-08-25 13:59:24 -07001130 rc = fcoe_lport_config(lport);
Vasu Dev7f349142009-03-27 09:06:31 -07001131 if (rc) {
Robert Loved5488eb2009-06-10 15:30:59 -07001132 FCOE_NETDEV_DBG(netdev, "Could not configure lport for the "
1133 "interface\n");
Vasu Dev7f349142009-03-27 09:06:31 -07001134 goto out_host_put;
1135 }
1136
Chris Leech9a057532009-11-03 11:46:40 -08001137 if (npiv) {
Chris Leech9f8f3aa2010-04-09 14:23:16 -07001138 FCOE_NETDEV_DBG(netdev, "Setting vport names, "
1139 "%16.16llx %16.16llx\n",
Robert Love1875f272009-11-03 11:47:50 -08001140 vport->node_name, vport->port_name);
Chris Leech9a057532009-11-03 11:46:40 -08001141 fc_set_wwnn(lport, vport->node_name);
1142 fc_set_wwpn(lport, vport->port_name);
1143 }
1144
Vasu Devab6b85c2009-05-17 12:33:08 +00001145 /* configure lport network properties */
Chris Leechaf7f85d2009-08-25 13:59:24 -07001146 rc = fcoe_netdev_config(lport, netdev);
Vasu Devab6b85c2009-05-17 12:33:08 +00001147 if (rc) {
Robert Loved5488eb2009-06-10 15:30:59 -07001148 FCOE_NETDEV_DBG(netdev, "Could not configure netdev for the "
1149 "interface\n");
Chris Leech54b649f2009-08-25 14:00:07 -07001150 goto out_lp_destroy;
Vasu Devab6b85c2009-05-17 12:33:08 +00001151 }
Joe Eykholt97c83892009-03-17 11:42:40 -07001152
Vasu Dev7f349142009-03-27 09:06:31 -07001153 /* configure lport scsi host properties */
Vasu Dev8ba00a42010-04-09 14:22:54 -07001154 rc = fcoe_shost_config(lport, parent);
Vasu Dev7f349142009-03-27 09:06:31 -07001155 if (rc) {
Robert Loved5488eb2009-06-10 15:30:59 -07001156 FCOE_NETDEV_DBG(netdev, "Could not configure shost for the "
1157 "interface\n");
Chris Leech54b649f2009-08-25 14:00:07 -07001158 goto out_lp_destroy;
Vasu Dev7f349142009-03-27 09:06:31 -07001159 }
1160
Vasu Dev7f349142009-03-27 09:06:31 -07001161 /* Initialize the library */
Robert Love619fe4b2012-05-22 19:06:10 -07001162 rc = fcoe_libfc_config(lport, ctlr, &fcoe_libfc_fcn_templ, 1);
Vasu Dev7f349142009-03-27 09:06:31 -07001163 if (rc) {
Robert Loved5488eb2009-06-10 15:30:59 -07001164 FCOE_NETDEV_DBG(netdev, "Could not configure libfc for the "
1165 "interface\n");
Vasu Dev7f349142009-03-27 09:06:31 -07001166 goto out_lp_destroy;
1167 }
1168
Neerav Parikh6fef3902012-01-22 17:30:10 -08001169 /* Initialized FDMI information */
1170 fcoe_fdmi_info(lport, netdev);
1171
Vasu Dev72fa3962011-02-25 15:03:01 -08001172 /*
1173 * fcoe_em_alloc() and fcoe_hostlist_add() both
1174 * need to be atomic with respect to other changes to the
1175 * hostlist since fcoe_em_alloc() looks for an existing EM
1176 * instance on host list updated by fcoe_hostlist_add().
1177 *
1178 * This is currently handled through the fcoe_config_mutex
1179 * begin held.
1180 */
1181 if (!npiv)
Chris Leech9a057532009-11-03 11:46:40 -08001182 /* lport exch manager allocation */
1183 rc = fcoe_em_config(lport);
Vasu Dev72fa3962011-02-25 15:03:01 -08001184 else {
1185 shost = vport_to_shost(vport);
1186 n_port = shost_priv(shost);
1187 rc = fc_exch_mgr_list_clone(n_port, lport);
1188 }
1189
1190 if (rc) {
1191 FCOE_NETDEV_DBG(netdev, "Could not configure the EM\n");
1192 goto out_lp_destroy;
Vasu Dev96316092009-07-29 17:05:00 -07001193 }
1194
Chris Leechaf7f85d2009-08-25 13:59:24 -07001195 return lport;
Vasu Dev7f349142009-03-27 09:06:31 -07001196
1197out_lp_destroy:
Chris Leechaf7f85d2009-08-25 13:59:24 -07001198 fc_exch_mgr_free(lport);
Vasu Dev7f349142009-03-27 09:06:31 -07001199out_host_put:
Neil Hormanf9184df2013-01-15 14:34:40 -05001200 fcoe_hostlist_del(lport);
Chris Leechaf7f85d2009-08-25 13:59:24 -07001201 scsi_host_put(lport->host);
1202out:
1203 return ERR_PTR(rc);
Vasu Dev7f349142009-03-27 09:06:31 -07001204}
1205
1206/**
Robert Love1875f272009-11-03 11:47:50 -08001207 * fcoe_if_init() - Initialization routine for fcoe.ko
Vasu Dev7f349142009-03-27 09:06:31 -07001208 *
Robert Love1875f272009-11-03 11:47:50 -08001209 * Attaches the SW FCoE transport to the FC transport
1210 *
1211 * Returns: 0 on success
Vasu Dev7f349142009-03-27 09:06:31 -07001212 */
1213static int __init fcoe_if_init(void)
1214{
1215 /* attach to scsi transport */
Yi Zou8ca86f82011-01-28 16:05:11 -08001216 fcoe_nport_scsi_transport =
1217 fc_attach_transport(&fcoe_nport_fc_functions);
1218 fcoe_vport_scsi_transport =
1219 fc_attach_transport(&fcoe_vport_fc_functions);
Vasu Dev7f349142009-03-27 09:06:31 -07001220
Yi Zou8ca86f82011-01-28 16:05:11 -08001221 if (!fcoe_nport_scsi_transport) {
Robert Loved5488eb2009-06-10 15:30:59 -07001222 printk(KERN_ERR "fcoe: Failed to attach to the FC transport\n");
Vasu Dev7f349142009-03-27 09:06:31 -07001223 return -ENODEV;
1224 }
1225
1226 return 0;
1227}
1228
1229/**
Robert Love1875f272009-11-03 11:47:50 -08001230 * fcoe_if_exit() - Tear down fcoe.ko
Vasu Dev7f349142009-03-27 09:06:31 -07001231 *
Robert Love1875f272009-11-03 11:47:50 -08001232 * Detaches the SW FCoE transport from the FC transport
1233 *
1234 * Returns: 0 on success
Vasu Dev7f349142009-03-27 09:06:31 -07001235 */
Bart Van Assche7c9c6842012-01-13 17:26:25 -08001236static int __exit fcoe_if_exit(void)
Vasu Dev7f349142009-03-27 09:06:31 -07001237{
Yi Zou8ca86f82011-01-28 16:05:11 -08001238 fc_release_transport(fcoe_nport_scsi_transport);
1239 fc_release_transport(fcoe_vport_scsi_transport);
1240 fcoe_nport_scsi_transport = NULL;
1241 fcoe_vport_scsi_transport = NULL;
Vasu Dev7f349142009-03-27 09:06:31 -07001242 return 0;
1243}
1244
Robert Love85b4aa42008-12-09 15:10:24 -08001245/**
Robert Love1875f272009-11-03 11:47:50 -08001246 * fcoe_percpu_thread_create() - Create a receive thread for an online CPU
1247 * @cpu: The CPU index of the CPU to create a receive thread for
Robert Love8976f422009-03-17 11:41:46 -07001248 */
1249static void fcoe_percpu_thread_create(unsigned int cpu)
1250{
1251 struct fcoe_percpu_s *p;
1252 struct task_struct *thread;
1253
1254 p = &per_cpu(fcoe_percpu, cpu);
1255
Eric Dumazet5c609ff2011-09-27 21:37:52 -07001256 thread = kthread_create_on_node(fcoe_percpu_receive_thread,
1257 (void *)p, cpu_to_node(cpu),
1258 "fcoethread/%d", cpu);
Robert Love8976f422009-03-17 11:41:46 -07001259
Joe Eykholte7a51992009-08-25 14:04:08 -07001260 if (likely(!IS_ERR(thread))) {
Robert Love8976f422009-03-17 11:41:46 -07001261 kthread_bind(thread, cpu);
1262 wake_up_process(thread);
1263
1264 spin_lock_bh(&p->fcoe_rx_list.lock);
1265 p->thread = thread;
1266 spin_unlock_bh(&p->fcoe_rx_list.lock);
1267 }
1268}
1269
1270/**
Robert Love1875f272009-11-03 11:47:50 -08001271 * fcoe_percpu_thread_destroy() - Remove the receive thread of a CPU
1272 * @cpu: The CPU index of the CPU whose receive thread is to be destroyed
Robert Love8976f422009-03-17 11:41:46 -07001273 *
1274 * Destroys a per-CPU Rx thread. Any pending skbs are moved to the
1275 * current CPU's Rx thread. If the thread being destroyed is bound to
1276 * the CPU processing this context the skbs will be freed.
1277 */
1278static void fcoe_percpu_thread_destroy(unsigned int cpu)
1279{
1280 struct fcoe_percpu_s *p;
1281 struct task_struct *thread;
1282 struct page *crc_eof;
1283 struct sk_buff *skb;
1284#ifdef CONFIG_SMP
1285 struct fcoe_percpu_s *p0;
Joe Eykholtf018b732010-03-12 16:08:55 -08001286 unsigned targ_cpu = get_cpu();
Robert Love8976f422009-03-17 11:41:46 -07001287#endif /* CONFIG_SMP */
1288
Robert Loved5488eb2009-06-10 15:30:59 -07001289 FCOE_DBG("Destroying receive thread for CPU %d\n", cpu);
Robert Love8976f422009-03-17 11:41:46 -07001290
1291 /* Prevent any new skbs from being queued for this CPU. */
1292 p = &per_cpu(fcoe_percpu, cpu);
1293 spin_lock_bh(&p->fcoe_rx_list.lock);
1294 thread = p->thread;
1295 p->thread = NULL;
1296 crc_eof = p->crc_eof_page;
1297 p->crc_eof_page = NULL;
1298 p->crc_eof_offset = 0;
1299 spin_unlock_bh(&p->fcoe_rx_list.lock);
1300
1301#ifdef CONFIG_SMP
1302 /*
1303 * Don't bother moving the skb's if this context is running
1304 * on the same CPU that is having its thread destroyed. This
1305 * can easily happen when the module is removed.
1306 */
1307 if (cpu != targ_cpu) {
1308 p0 = &per_cpu(fcoe_percpu, targ_cpu);
1309 spin_lock_bh(&p0->fcoe_rx_list.lock);
1310 if (p0->thread) {
Robert Loved5488eb2009-06-10 15:30:59 -07001311 FCOE_DBG("Moving frames from CPU %d to CPU %d\n",
1312 cpu, targ_cpu);
Robert Love8976f422009-03-17 11:41:46 -07001313
1314 while ((skb = __skb_dequeue(&p->fcoe_rx_list)) != NULL)
1315 __skb_queue_tail(&p0->fcoe_rx_list, skb);
1316 spin_unlock_bh(&p0->fcoe_rx_list.lock);
1317 } else {
1318 /*
1319 * The targeted CPU is not initialized and cannot accept
Robert Love1875f272009-11-03 11:47:50 -08001320 * new skbs. Unlock the targeted CPU and drop the skbs
Robert Love8976f422009-03-17 11:41:46 -07001321 * on the CPU that is going offline.
1322 */
1323 while ((skb = __skb_dequeue(&p->fcoe_rx_list)) != NULL)
1324 kfree_skb(skb);
1325 spin_unlock_bh(&p0->fcoe_rx_list.lock);
1326 }
1327 } else {
1328 /*
1329 * This scenario occurs when the module is being removed
1330 * and all threads are being destroyed. skbs will continue
1331 * to be shifted from the CPU thread that is being removed
1332 * to the CPU thread associated with the CPU that is processing
1333 * the module removal. Once there is only one CPU Rx thread it
1334 * will reach this case and we will drop all skbs and later
1335 * stop the thread.
1336 */
1337 spin_lock_bh(&p->fcoe_rx_list.lock);
1338 while ((skb = __skb_dequeue(&p->fcoe_rx_list)) != NULL)
1339 kfree_skb(skb);
1340 spin_unlock_bh(&p->fcoe_rx_list.lock);
1341 }
Joe Eykholtf018b732010-03-12 16:08:55 -08001342 put_cpu();
Robert Love8976f422009-03-17 11:41:46 -07001343#else
1344 /*
Chris Leechdd3fd722009-04-21 16:27:36 -07001345 * This a non-SMP scenario where the singular Rx thread is
Robert Love8976f422009-03-17 11:41:46 -07001346 * being removed. Free all skbs and stop the thread.
1347 */
1348 spin_lock_bh(&p->fcoe_rx_list.lock);
1349 while ((skb = __skb_dequeue(&p->fcoe_rx_list)) != NULL)
1350 kfree_skb(skb);
1351 spin_unlock_bh(&p->fcoe_rx_list.lock);
1352#endif
1353
1354 if (thread)
1355 kthread_stop(thread);
1356
1357 if (crc_eof)
1358 put_page(crc_eof);
1359}
1360
1361/**
Robert Love1875f272009-11-03 11:47:50 -08001362 * fcoe_cpu_callback() - Handler for CPU hotplug events
1363 * @nfb: The callback data block
1364 * @action: The event triggering the callback
1365 * @hcpu: The index of the CPU that the event is for
Robert Love8976f422009-03-17 11:41:46 -07001366 *
Robert Love1875f272009-11-03 11:47:50 -08001367 * This creates or destroys per-CPU data for fcoe
Robert Love8976f422009-03-17 11:41:46 -07001368 *
1369 * Returns NOTIFY_OK always.
1370 */
1371static int fcoe_cpu_callback(struct notifier_block *nfb,
1372 unsigned long action, void *hcpu)
1373{
1374 unsigned cpu = (unsigned long)hcpu;
1375
1376 switch (action) {
1377 case CPU_ONLINE:
1378 case CPU_ONLINE_FROZEN:
Robert Loved5488eb2009-06-10 15:30:59 -07001379 FCOE_DBG("CPU %x online: Create Rx thread\n", cpu);
Robert Love8976f422009-03-17 11:41:46 -07001380 fcoe_percpu_thread_create(cpu);
1381 break;
1382 case CPU_DEAD:
1383 case CPU_DEAD_FROZEN:
Robert Loved5488eb2009-06-10 15:30:59 -07001384 FCOE_DBG("CPU %x offline: Remove Rx thread\n", cpu);
Robert Love8976f422009-03-17 11:41:46 -07001385 fcoe_percpu_thread_destroy(cpu);
1386 break;
1387 default:
1388 break;
1389 }
1390 return NOTIFY_OK;
1391}
1392
Robert Love8976f422009-03-17 11:41:46 -07001393/**
Kiran Patil064287e2011-06-20 16:59:20 -07001394 * fcoe_select_cpu() - Selects CPU to handle post-processing of incoming
1395 * command.
Kiran Patil064287e2011-06-20 16:59:20 -07001396 *
Vasu Devd2722812011-07-27 15:11:10 -07001397 * This routine selects next CPU based on cpumask to distribute
1398 * incoming requests in round robin.
Kiran Patil064287e2011-06-20 16:59:20 -07001399 *
Vasu Devd2722812011-07-27 15:11:10 -07001400 * Returns: int CPU number
Kiran Patil064287e2011-06-20 16:59:20 -07001401 */
Vasu Devd2722812011-07-27 15:11:10 -07001402static inline unsigned int fcoe_select_cpu(void)
Kiran Patil064287e2011-06-20 16:59:20 -07001403{
1404 static unsigned int selected_cpu;
1405
Vasu Devd2722812011-07-27 15:11:10 -07001406 selected_cpu = cpumask_next(selected_cpu, cpu_online_mask);
1407 if (selected_cpu >= nr_cpu_ids)
1408 selected_cpu = cpumask_first(cpu_online_mask);
1409
Kiran Patil064287e2011-06-20 16:59:20 -07001410 return selected_cpu;
1411}
1412
1413/**
Robert Love1875f272009-11-03 11:47:50 -08001414 * fcoe_rcv() - Receive packets from a net device
1415 * @skb: The received packet
1416 * @netdev: The net device that the packet was received on
1417 * @ptype: The packet type context
1418 * @olddev: The last device net device
Robert Love85b4aa42008-12-09 15:10:24 -08001419 *
Robert Love1875f272009-11-03 11:47:50 -08001420 * This routine is called by NET_RX_SOFTIRQ. It receives a packet, builds a
1421 * FC frame and passes the frame to libfc.
Robert Love85b4aa42008-12-09 15:10:24 -08001422 *
1423 * Returns: 0 for success
Robert Love34f42a02009-02-27 10:55:45 -08001424 */
Bart Van Assche7c9c6842012-01-13 17:26:25 -08001425static int fcoe_rcv(struct sk_buff *skb, struct net_device *netdev,
Robert Love85b4aa42008-12-09 15:10:24 -08001426 struct packet_type *ptype, struct net_device *olddev)
1427{
Robert Love1875f272009-11-03 11:47:50 -08001428 struct fc_lport *lport;
Robert Love85b4aa42008-12-09 15:10:24 -08001429 struct fcoe_rcv_info *fr;
Robert Love619fe4b2012-05-22 19:06:10 -07001430 struct fcoe_ctlr *ctlr;
Chris Leech259ad852009-08-25 13:59:41 -07001431 struct fcoe_interface *fcoe;
Robert Love85b4aa42008-12-09 15:10:24 -08001432 struct fc_frame_header *fh;
Robert Love85b4aa42008-12-09 15:10:24 -08001433 struct fcoe_percpu_s *fps;
Vasu Dev519e5132010-07-20 15:19:32 -07001434 struct ethhdr *eh;
Vasu Devb2f00912009-08-25 13:58:53 -07001435 unsigned int cpu;
Robert Love85b4aa42008-12-09 15:10:24 -08001436
Chris Leech259ad852009-08-25 13:59:41 -07001437 fcoe = container_of(ptype, struct fcoe_interface, fcoe_packet_type);
Robert Love619fe4b2012-05-22 19:06:10 -07001438 ctlr = fcoe_to_ctlr(fcoe);
1439 lport = ctlr->lp;
Robert Love1875f272009-11-03 11:47:50 -08001440 if (unlikely(!lport)) {
1441 FCOE_NETDEV_DBG(netdev, "Cannot find hba structure");
Robert Love85b4aa42008-12-09 15:10:24 -08001442 goto err2;
1443 }
Robert Love1875f272009-11-03 11:47:50 -08001444 if (!lport->link_up)
Joe Eykholt97c83892009-03-17 11:42:40 -07001445 goto err2;
Robert Love85b4aa42008-12-09 15:10:24 -08001446
Robert Love1875f272009-11-03 11:47:50 -08001447 FCOE_NETDEV_DBG(netdev, "skb_info: len:%d data_len:%d head:%p "
Robert Loved5488eb2009-06-10 15:30:59 -07001448 "data:%p tail:%p end:%p sum:%d dev:%s",
1449 skb->len, skb->data_len, skb->head, skb->data,
1450 skb_tail_pointer(skb), skb_end_pointer(skb),
1451 skb->csum, skb->dev ? skb->dev->name : "<NULL>");
Robert Love85b4aa42008-12-09 15:10:24 -08001452
Vasu Dev519e5132010-07-20 15:19:32 -07001453 eh = eth_hdr(skb);
Vasu Dev519e5132010-07-20 15:19:32 -07001454
Robert Love619fe4b2012-05-22 19:06:10 -07001455 if (is_fip_mode(ctlr) &&
1456 compare_ether_addr(eh->h_source, ctlr->dest_addr)) {
Vasu Dev519e5132010-07-20 15:19:32 -07001457 FCOE_NETDEV_DBG(netdev, "wrong source mac address:%pM\n",
1458 eh->h_source);
Robert Love85b4aa42008-12-09 15:10:24 -08001459 goto err;
1460 }
1461
1462 /*
1463 * Check for minimum frame length, and make sure required FCoE
1464 * and FC headers are pulled into the linear data area.
1465 */
1466 if (unlikely((skb->len < FCOE_MIN_FRAME) ||
Robert Love1875f272009-11-03 11:47:50 -08001467 !pskb_may_pull(skb, FCOE_HEADER_LEN)))
Robert Love85b4aa42008-12-09 15:10:24 -08001468 goto err;
1469
1470 skb_set_transport_header(skb, sizeof(struct fcoe_hdr));
1471 fh = (struct fc_frame_header *) skb_transport_header(skb);
1472
Robert Love0ee31cb2010-10-08 17:12:46 -07001473 if (ntoh24(&eh->h_dest[3]) != ntoh24(fh->fh_d_id)) {
1474 FCOE_NETDEV_DBG(netdev, "FC frame d_id mismatch with MAC:%pM\n",
1475 eh->h_dest);
1476 goto err;
1477 }
1478
Robert Love85b4aa42008-12-09 15:10:24 -08001479 fr = fcoe_dev_from_skb(skb);
Robert Love1875f272009-11-03 11:47:50 -08001480 fr->fr_dev = lport;
Robert Love5e5e92d2009-03-17 11:41:35 -07001481
Robert Love85b4aa42008-12-09 15:10:24 -08001482 /*
Vasu Devb2f00912009-08-25 13:58:53 -07001483 * In case the incoming frame's exchange is originated from
1484 * the initiator, then received frame's exchange id is ANDed
1485 * with fc_cpu_mask bits to get the same cpu on which exchange
Vasu Devd2722812011-07-27 15:11:10 -07001486 * was originated, otherwise select cpu using rx exchange id
1487 * or fcoe_select_cpu().
Robert Love85b4aa42008-12-09 15:10:24 -08001488 */
Vasu Devb2f00912009-08-25 13:58:53 -07001489 if (ntoh24(fh->fh_f_ctl) & FC_FC_EX_CTX)
1490 cpu = ntohs(fh->fh_ox_id) & fc_cpu_mask;
Kiran Patil064287e2011-06-20 16:59:20 -07001491 else {
Vasu Devd2722812011-07-27 15:11:10 -07001492 if (ntohs(fh->fh_rx_id) == FC_XID_UNKNOWN)
1493 cpu = fcoe_select_cpu();
1494 else
Kiran Patil29bdd2b2011-06-20 16:59:25 -07001495 cpu = ntohs(fh->fh_rx_id) & fc_cpu_mask;
Kiran Patil064287e2011-06-20 16:59:20 -07001496 }
Vasu Dev324f6672011-07-27 15:10:39 -07001497
1498 if (cpu >= nr_cpu_ids)
1499 goto err;
1500
Robert Love8976f422009-03-17 11:41:46 -07001501 fps = &per_cpu(fcoe_percpu, cpu);
Neil Horman94aa29f2012-03-09 14:50:08 -08001502 spin_lock(&fps->fcoe_rx_list.lock);
Robert Love8976f422009-03-17 11:41:46 -07001503 if (unlikely(!fps->thread)) {
1504 /*
1505 * The targeted CPU is not ready, let's target
1506 * the first CPU now. For non-SMP systems this
1507 * will check the same CPU twice.
1508 */
Robert Love1875f272009-11-03 11:47:50 -08001509 FCOE_NETDEV_DBG(netdev, "CPU is online, but no receive thread "
Robert Loved5488eb2009-06-10 15:30:59 -07001510 "ready for incoming skb- using first online "
1511 "CPU.\n");
Robert Love8976f422009-03-17 11:41:46 -07001512
Neil Horman94aa29f2012-03-09 14:50:08 -08001513 spin_unlock(&fps->fcoe_rx_list.lock);
Rusty Russell69571772009-12-17 11:43:14 -06001514 cpu = cpumask_first(cpu_online_mask);
Robert Love8976f422009-03-17 11:41:46 -07001515 fps = &per_cpu(fcoe_percpu, cpu);
Neil Horman94aa29f2012-03-09 14:50:08 -08001516 spin_lock(&fps->fcoe_rx_list.lock);
Robert Love8976f422009-03-17 11:41:46 -07001517 if (!fps->thread) {
Neil Horman94aa29f2012-03-09 14:50:08 -08001518 spin_unlock(&fps->fcoe_rx_list.lock);
Robert Love8976f422009-03-17 11:41:46 -07001519 goto err;
1520 }
1521 }
1522
1523 /*
1524 * We now have a valid CPU that we're targeting for
1525 * this skb. We also have this receive thread locked,
1526 * so we're free to queue skbs into it's queue.
1527 */
Robert Love85b4aa42008-12-09 15:10:24 -08001528
Neil Horman5e70c4c2012-03-09 14:49:48 -08001529 /*
1530 * Note: We used to have a set of conditions under which we would
1531 * call fcoe_recv_frame directly, rather than queuing to the rx list
1532 * as it could save a few cycles, but doing so is prohibited, as
1533 * fcoe_recv_frame has several paths that may sleep, which is forbidden
1534 * in softirq context.
Chris Leech859b7b62009-11-20 14:54:47 -08001535 */
Neil Horman5e70c4c2012-03-09 14:49:48 -08001536 __skb_queue_tail(&fps->fcoe_rx_list, skb);
Neil Horman20dc3812012-03-09 14:50:24 -08001537 if (fps->thread->state == TASK_INTERRUPTIBLE)
Neil Horman5e70c4c2012-03-09 14:49:48 -08001538 wake_up_process(fps->thread);
Neil Horman94aa29f2012-03-09 14:50:08 -08001539 spin_unlock(&fps->fcoe_rx_list.lock);
Robert Love85b4aa42008-12-09 15:10:24 -08001540
1541 return 0;
1542err:
Vasu Dev1bd49b42012-05-25 10:26:43 -07001543 per_cpu_ptr(lport->stats, get_cpu())->ErrorFrames++;
Joe Eykholtf018b732010-03-12 16:08:55 -08001544 put_cpu();
Robert Love85b4aa42008-12-09 15:10:24 -08001545err2:
1546 kfree_skb(skb);
1547 return -1;
1548}
Robert Love85b4aa42008-12-09 15:10:24 -08001549
1550/**
Bhanu Prakash Gollapudi8597ae82011-01-28 16:05:37 -08001551 * fcoe_alloc_paged_crc_eof() - Allocate a page to be used for the trailer CRC
Robert Love1875f272009-11-03 11:47:50 -08001552 * @skb: The packet to be transmitted
1553 * @tlen: The total length of the trailer
1554 *
Robert Love85b4aa42008-12-09 15:10:24 -08001555 * Returns: 0 for success
Robert Love34f42a02009-02-27 10:55:45 -08001556 */
Bhanu Prakash Gollapudi8597ae82011-01-28 16:05:37 -08001557static int fcoe_alloc_paged_crc_eof(struct sk_buff *skb, int tlen)
Robert Love85b4aa42008-12-09 15:10:24 -08001558{
1559 struct fcoe_percpu_s *fps;
Bhanu Prakash Gollapudi8597ae82011-01-28 16:05:37 -08001560 int rc;
Robert Love85b4aa42008-12-09 15:10:24 -08001561
Robert Love5e5e92d2009-03-17 11:41:35 -07001562 fps = &get_cpu_var(fcoe_percpu);
Bhanu Prakash Gollapudi8597ae82011-01-28 16:05:37 -08001563 rc = fcoe_get_paged_crc_eof(skb, tlen, fps);
Robert Love5e5e92d2009-03-17 11:41:35 -07001564 put_cpu_var(fcoe_percpu);
Robert Love85b4aa42008-12-09 15:10:24 -08001565
Bhanu Prakash Gollapudi8597ae82011-01-28 16:05:37 -08001566 return rc;
Robert Love85b4aa42008-12-09 15:10:24 -08001567}
Robert Love85b4aa42008-12-09 15:10:24 -08001568
1569/**
Robert Love1875f272009-11-03 11:47:50 -08001570 * fcoe_xmit() - Transmit a FCoE frame
1571 * @lport: The local port that the frame is to be transmitted for
1572 * @fp: The frame to be transmitted
Robert Love85b4aa42008-12-09 15:10:24 -08001573 *
Robert Love1875f272009-11-03 11:47:50 -08001574 * Return: 0 for success
Robert Love34f42a02009-02-27 10:55:45 -08001575 */
Bart Van Assche7c9c6842012-01-13 17:26:25 -08001576static int fcoe_xmit(struct fc_lport *lport, struct fc_frame *fp)
Robert Love85b4aa42008-12-09 15:10:24 -08001577{
Vasu Dev4bb6b512009-05-06 10:52:34 -07001578 int wlen;
Robert Love85b4aa42008-12-09 15:10:24 -08001579 u32 crc;
1580 struct ethhdr *eh;
1581 struct fcoe_crc_eof *cp;
1582 struct sk_buff *skb;
Vasu Dev1bd49b42012-05-25 10:26:43 -07001583 struct fc_stats *stats;
Robert Love85b4aa42008-12-09 15:10:24 -08001584 struct fc_frame_header *fh;
1585 unsigned int hlen; /* header length implies the version */
1586 unsigned int tlen; /* trailer length */
1587 unsigned int elen; /* eth header, may include vlan */
Robert Love1875f272009-11-03 11:47:50 -08001588 struct fcoe_port *port = lport_priv(lport);
Bhanu Prakash Gollapudi8597ae82011-01-28 16:05:37 -08001589 struct fcoe_interface *fcoe = port->priv;
Robert Love619fe4b2012-05-22 19:06:10 -07001590 struct fcoe_ctlr *ctlr = fcoe_to_ctlr(fcoe);
Robert Love85b4aa42008-12-09 15:10:24 -08001591 u8 sof, eof;
1592 struct fcoe_hdr *hp;
1593
1594 WARN_ON((fr_len(fp) % sizeof(u32)) != 0);
1595
Robert Love85b4aa42008-12-09 15:10:24 -08001596 fh = fc_frame_header_get(fp);
Joe Eykholt97c83892009-03-17 11:42:40 -07001597 skb = fp_skb(fp);
1598 wlen = skb->len / FCOE_WORD_TO_BYTE;
1599
Robert Love1875f272009-11-03 11:47:50 -08001600 if (!lport->link_up) {
Dan Carpenter3caf02e2009-04-21 16:27:25 -07001601 kfree_skb(skb);
Joe Eykholt97c83892009-03-17 11:42:40 -07001602 return 0;
Robert Love85b4aa42008-12-09 15:10:24 -08001603 }
1604
Joe Eykholt9860eeb2010-03-12 16:07:52 -08001605 if (unlikely(fh->fh_type == FC_TYPE_ELS) &&
Robert Love619fe4b2012-05-22 19:06:10 -07001606 fcoe_ctlr_els_send(ctlr, lport, skb))
Joe Eykholt97c83892009-03-17 11:42:40 -07001607 return 0;
1608
Robert Love85b4aa42008-12-09 15:10:24 -08001609 sof = fr_sof(fp);
1610 eof = fr_eof(fp);
1611
Vasu Dev4e57e1c2009-05-06 10:52:46 -07001612 elen = sizeof(struct ethhdr);
Robert Love85b4aa42008-12-09 15:10:24 -08001613 hlen = sizeof(struct fcoe_hdr);
1614 tlen = sizeof(struct fcoe_crc_eof);
1615 wlen = (skb->len - tlen + sizeof(crc)) / FCOE_WORD_TO_BYTE;
1616
1617 /* crc offload */
Robert Love1875f272009-11-03 11:47:50 -08001618 if (likely(lport->crc_offload)) {
Yi Zoucf64bc82012-03-16 23:08:12 +00001619 skb->ip_summed = CHECKSUM_UNNECESSARY;
Robert Love85b4aa42008-12-09 15:10:24 -08001620 skb->csum_start = skb_headroom(skb);
1621 skb->csum_offset = skb->len;
1622 crc = 0;
1623 } else {
1624 skb->ip_summed = CHECKSUM_NONE;
1625 crc = fcoe_fc_crc(fp);
1626 }
1627
Chris Leech014f5c32009-08-25 13:59:30 -07001628 /* copy port crc and eof to the skb buff */
Robert Love85b4aa42008-12-09 15:10:24 -08001629 if (skb_is_nonlinear(skb)) {
1630 skb_frag_t *frag;
Bhanu Prakash Gollapudi8597ae82011-01-28 16:05:37 -08001631 if (fcoe_alloc_paged_crc_eof(skb, tlen)) {
Roel Kluine9041582009-02-27 10:56:22 -08001632 kfree_skb(skb);
Robert Love85b4aa42008-12-09 15:10:24 -08001633 return -ENOMEM;
1634 }
1635 frag = &skb_shinfo(skb)->frags[skb_shinfo(skb)->nr_frags - 1];
Cong Wang77dfce02011-11-25 23:14:23 +08001636 cp = kmap_atomic(skb_frag_page(frag))
Robert Love85b4aa42008-12-09 15:10:24 -08001637 + frag->page_offset;
1638 } else {
1639 cp = (struct fcoe_crc_eof *)skb_put(skb, tlen);
1640 }
1641
1642 memset(cp, 0, sizeof(*cp));
1643 cp->fcoe_eof = eof;
1644 cp->fcoe_crc32 = cpu_to_le32(~crc);
1645
1646 if (skb_is_nonlinear(skb)) {
Cong Wang77dfce02011-11-25 23:14:23 +08001647 kunmap_atomic(cp);
Robert Love85b4aa42008-12-09 15:10:24 -08001648 cp = NULL;
1649 }
1650
Chris Leech014f5c32009-08-25 13:59:30 -07001651 /* adjust skb network/transport offsets to match mac/fcoe/port */
Robert Love85b4aa42008-12-09 15:10:24 -08001652 skb_push(skb, elen + hlen);
1653 skb_reset_mac_header(skb);
1654 skb_reset_network_header(skb);
1655 skb->mac_len = elen;
Yi Zou211c7382009-02-27 14:06:37 -08001656 skb->protocol = htons(ETH_P_FCOE);
Neerav Parikh31c37a62012-09-24 11:52:45 -07001657 skb->priority = fcoe->priority;
john fastabend6f6c2aa2011-11-18 13:35:56 -08001658
Vasu Devd1483bb2011-09-27 21:38:13 -07001659 if (fcoe->netdev->priv_flags & IFF_802_1Q_VLAN &&
1660 fcoe->realdev->features & NETIF_F_HW_VLAN_TX) {
1661 skb->vlan_tci = VLAN_TAG_PRESENT |
1662 vlan_dev_vlan_id(fcoe->netdev);
1663 skb->dev = fcoe->realdev;
1664 } else
1665 skb->dev = fcoe->netdev;
Robert Love85b4aa42008-12-09 15:10:24 -08001666
1667 /* fill up mac and fcoe headers */
1668 eh = eth_hdr(skb);
1669 eh->h_proto = htons(ETH_P_FCOE);
Robert Love619fe4b2012-05-22 19:06:10 -07001670 memcpy(eh->h_dest, ctlr->dest_addr, ETH_ALEN);
1671 if (ctlr->map_dest)
Joe Eykholtcd229e42010-07-20 15:20:40 -07001672 memcpy(eh->h_dest + 3, fh->fh_d_id, 3);
Robert Love85b4aa42008-12-09 15:10:24 -08001673
Robert Love619fe4b2012-05-22 19:06:10 -07001674 if (unlikely(ctlr->flogi_oxid != FC_XID_UNKNOWN))
1675 memcpy(eh->h_source, ctlr->ctl_src_addr, ETH_ALEN);
Robert Love85b4aa42008-12-09 15:10:24 -08001676 else
Chris Leech11b56182009-11-03 11:46:29 -08001677 memcpy(eh->h_source, port->data_src_addr, ETH_ALEN);
Robert Love85b4aa42008-12-09 15:10:24 -08001678
1679 hp = (struct fcoe_hdr *)(eh + 1);
1680 memset(hp, 0, sizeof(*hp));
1681 if (FC_FCOE_VER)
1682 FC_FCOE_ENCAPS_VER(hp, FC_FCOE_VER);
1683 hp->fcoe_sof = sof;
1684
Yi Zou39ca9a02009-02-27 14:07:15 -08001685 /* fcoe lso, mss is in max_payload which is non-zero for FCP data */
Robert Love1875f272009-11-03 11:47:50 -08001686 if (lport->seq_offload && fr_max_payload(fp)) {
Yi Zou39ca9a02009-02-27 14:07:15 -08001687 skb_shinfo(skb)->gso_type = SKB_GSO_FCOE;
1688 skb_shinfo(skb)->gso_size = fr_max_payload(fp);
1689 } else {
1690 skb_shinfo(skb)->gso_type = 0;
1691 skb_shinfo(skb)->gso_size = 0;
1692 }
Robert Love85b4aa42008-12-09 15:10:24 -08001693 /* update tx stats: regardless if LLD fails */
Vasu Dev1bd49b42012-05-25 10:26:43 -07001694 stats = per_cpu_ptr(lport->stats, get_cpu());
Robert Love582b45b2009-03-31 15:51:50 -07001695 stats->TxFrames++;
1696 stats->TxWords += wlen;
Joe Eykholtf018b732010-03-12 16:08:55 -08001697 put_cpu();
Robert Love85b4aa42008-12-09 15:10:24 -08001698
1699 /* send down to lld */
Robert Love1875f272009-11-03 11:47:50 -08001700 fr_dev(fp) = lport;
Vasu Dev980f5152011-07-27 15:11:05 -07001701 fcoe_port_send(port, skb);
Robert Love85b4aa42008-12-09 15:10:24 -08001702 return 0;
1703}
Robert Love85b4aa42008-12-09 15:10:24 -08001704
Robert Love34f42a02009-02-27 10:55:45 -08001705/**
Robert Love1875f272009-11-03 11:47:50 -08001706 * fcoe_percpu_flush_done() - Indicate per-CPU queue flush completion
1707 * @skb: The completed skb (argument required by destructor)
Joe Eykholte7a51992009-08-25 14:04:08 -07001708 */
1709static void fcoe_percpu_flush_done(struct sk_buff *skb)
1710{
1711 complete(&fcoe_flush_completion);
1712}
1713
1714/**
Vasu Dev52ee83212011-01-28 16:03:52 -08001715 * fcoe_filter_frames() - filter out bad fcoe frames, i.e. bad CRC
1716 * @lport: The local port the frame was received on
1717 * @fp: The received frame
1718 *
1719 * Return: 0 on passing filtering checks
1720 */
1721static inline int fcoe_filter_frames(struct fc_lport *lport,
1722 struct fc_frame *fp)
1723{
Robert Love619fe4b2012-05-22 19:06:10 -07001724 struct fcoe_ctlr *ctlr;
Vasu Dev52ee83212011-01-28 16:03:52 -08001725 struct fcoe_interface *fcoe;
1726 struct fc_frame_header *fh;
1727 struct sk_buff *skb = (struct sk_buff *)fp;
Vasu Dev1bd49b42012-05-25 10:26:43 -07001728 struct fc_stats *stats;
Vasu Dev52ee83212011-01-28 16:03:52 -08001729
1730 /*
1731 * We only check CRC if no offload is available and if it is
1732 * it's solicited data, in which case, the FCP layer would
1733 * check it during the copy.
1734 */
1735 if (lport->crc_offload && skb->ip_summed == CHECKSUM_UNNECESSARY)
1736 fr_flags(fp) &= ~FCPHF_CRC_UNCHECKED;
1737 else
1738 fr_flags(fp) |= FCPHF_CRC_UNCHECKED;
1739
1740 fh = (struct fc_frame_header *) skb_transport_header(skb);
1741 fh = fc_frame_header_get(fp);
1742 if (fh->fh_r_ctl == FC_RCTL_DD_SOL_DATA && fh->fh_type == FC_TYPE_FCP)
1743 return 0;
1744
Bhanu Prakash Gollapudi8597ae82011-01-28 16:05:37 -08001745 fcoe = ((struct fcoe_port *)lport_priv(lport))->priv;
Robert Love619fe4b2012-05-22 19:06:10 -07001746 ctlr = fcoe_to_ctlr(fcoe);
1747 if (is_fip_mode(ctlr) && fc_frame_payload_op(fp) == ELS_LOGO &&
Vasu Dev52ee83212011-01-28 16:03:52 -08001748 ntoh24(fh->fh_s_id) == FC_FID_FLOGI) {
1749 FCOE_DBG("fcoe: dropping FCoE lport LOGO in fip mode\n");
1750 return -EINVAL;
1751 }
1752
Dan Carpenterf2f96d22011-02-25 15:03:23 -08001753 if (!(fr_flags(fp) & FCPHF_CRC_UNCHECKED) ||
Vasu Dev52ee83212011-01-28 16:03:52 -08001754 le32_to_cpu(fr_crc(fp)) == ~crc32(~0, skb->data, skb->len)) {
1755 fr_flags(fp) &= ~FCPHF_CRC_UNCHECKED;
1756 return 0;
1757 }
1758
Vasu Dev1bd49b42012-05-25 10:26:43 -07001759 stats = per_cpu_ptr(lport->stats, get_cpu());
Vasu Dev52ee83212011-01-28 16:03:52 -08001760 stats->InvalidCRCCount++;
1761 if (stats->InvalidCRCCount < 5)
1762 printk(KERN_WARNING "fcoe: dropping frame with CRC error\n");
Thomas Gleixner7e1e7ea2011-11-11 20:52:01 +01001763 put_cpu();
Vasu Dev52ee83212011-01-28 16:03:52 -08001764 return -EINVAL;
1765}
1766
1767/**
Chris Leech859b7b62009-11-20 14:54:47 -08001768 * fcoe_recv_frame() - process a single received frame
1769 * @skb: frame to process
1770 */
1771static void fcoe_recv_frame(struct sk_buff *skb)
1772{
1773 u32 fr_len;
1774 struct fc_lport *lport;
1775 struct fcoe_rcv_info *fr;
Vasu Dev1bd49b42012-05-25 10:26:43 -07001776 struct fc_stats *stats;
Chris Leech859b7b62009-11-20 14:54:47 -08001777 struct fcoe_crc_eof crc_eof;
1778 struct fc_frame *fp;
Chris Leech859b7b62009-11-20 14:54:47 -08001779 struct fcoe_port *port;
1780 struct fcoe_hdr *hp;
1781
1782 fr = fcoe_dev_from_skb(skb);
1783 lport = fr->fr_dev;
1784 if (unlikely(!lport)) {
1785 if (skb->destructor != fcoe_percpu_flush_done)
1786 FCOE_NETDEV_DBG(skb->dev, "NULL lport in skb");
1787 kfree_skb(skb);
1788 return;
1789 }
1790
1791 FCOE_NETDEV_DBG(skb->dev, "skb_info: len:%d data_len:%d "
1792 "head:%p data:%p tail:%p end:%p sum:%d dev:%s",
1793 skb->len, skb->data_len,
1794 skb->head, skb->data, skb_tail_pointer(skb),
1795 skb_end_pointer(skb), skb->csum,
1796 skb->dev ? skb->dev->name : "<NULL>");
1797
Chris Leech859b7b62009-11-20 14:54:47 -08001798 port = lport_priv(lport);
Robert Lovef1633012011-12-16 14:24:49 -08001799 skb_linearize(skb); /* check for skb_is_nonlinear is within skb_linearize */
Chris Leech859b7b62009-11-20 14:54:47 -08001800
1801 /*
1802 * Frame length checks and setting up the header pointers
1803 * was done in fcoe_rcv already.
1804 */
1805 hp = (struct fcoe_hdr *) skb_network_header(skb);
Chris Leech859b7b62009-11-20 14:54:47 -08001806
Vasu Dev1bd49b42012-05-25 10:26:43 -07001807 stats = per_cpu_ptr(lport->stats, get_cpu());
Chris Leech859b7b62009-11-20 14:54:47 -08001808 if (unlikely(FC_FCOE_DECAPS_VER(hp) != FC_FCOE_VER)) {
1809 if (stats->ErrorFrames < 5)
1810 printk(KERN_WARNING "fcoe: FCoE version "
1811 "mismatch: The frame has "
1812 "version %x, but the "
1813 "initiator supports version "
1814 "%x\n", FC_FCOE_DECAPS_VER(hp),
1815 FC_FCOE_VER);
Joe Eykholtf018b732010-03-12 16:08:55 -08001816 goto drop;
Chris Leech859b7b62009-11-20 14:54:47 -08001817 }
1818
1819 skb_pull(skb, sizeof(struct fcoe_hdr));
1820 fr_len = skb->len - sizeof(struct fcoe_crc_eof);
1821
1822 stats->RxFrames++;
1823 stats->RxWords += fr_len / FCOE_WORD_TO_BYTE;
1824
1825 fp = (struct fc_frame *)skb;
1826 fc_frame_init(fp);
1827 fr_dev(fp) = lport;
1828 fr_sof(fp) = hp->fcoe_sof;
1829
1830 /* Copy out the CRC and EOF trailer for access */
Joe Eykholtf018b732010-03-12 16:08:55 -08001831 if (skb_copy_bits(skb, fr_len, &crc_eof, sizeof(crc_eof)))
1832 goto drop;
Chris Leech859b7b62009-11-20 14:54:47 -08001833 fr_eof(fp) = crc_eof.fcoe_eof;
1834 fr_crc(fp) = crc_eof.fcoe_crc32;
Joe Eykholtf018b732010-03-12 16:08:55 -08001835 if (pskb_trim(skb, fr_len))
1836 goto drop;
Chris Leech859b7b62009-11-20 14:54:47 -08001837
Vasu Dev52ee83212011-01-28 16:03:52 -08001838 if (!fcoe_filter_frames(lport, fp)) {
1839 put_cpu();
1840 fc_exch_recv(lport, fp);
1841 return;
Chris Leech859b7b62009-11-20 14:54:47 -08001842 }
Joe Eykholtf018b732010-03-12 16:08:55 -08001843drop:
1844 stats->ErrorFrames++;
1845 put_cpu();
1846 kfree_skb(skb);
Chris Leech859b7b62009-11-20 14:54:47 -08001847}
1848
1849/**
Robert Love1875f272009-11-03 11:47:50 -08001850 * fcoe_percpu_receive_thread() - The per-CPU packet receive thread
1851 * @arg: The per-CPU context
Robert Love85b4aa42008-12-09 15:10:24 -08001852 *
1853 * Return: 0 for success
Robert Love85b4aa42008-12-09 15:10:24 -08001854 */
Bart Van Assche7c9c6842012-01-13 17:26:25 -08001855static int fcoe_percpu_receive_thread(void *arg)
Robert Love85b4aa42008-12-09 15:10:24 -08001856{
1857 struct fcoe_percpu_s *p = arg;
Robert Love85b4aa42008-12-09 15:10:24 -08001858 struct sk_buff *skb;
Neil Horman20dc3812012-03-09 14:50:24 -08001859 struct sk_buff_head tmp;
1860
1861 skb_queue_head_init(&tmp);
Robert Love85b4aa42008-12-09 15:10:24 -08001862
Robert Love4469c192009-02-27 10:56:38 -08001863 set_user_nice(current, -20);
Robert Love85b4aa42008-12-09 15:10:24 -08001864
Neil Horman95fdd5e2012-07-06 10:40:05 -07001865retry:
Robert Love85b4aa42008-12-09 15:10:24 -08001866 while (!kthread_should_stop()) {
1867
1868 spin_lock_bh(&p->fcoe_rx_list.lock);
Neil Horman20dc3812012-03-09 14:50:24 -08001869 skb_queue_splice_init(&p->fcoe_rx_list, &tmp);
Neil Horman95fdd5e2012-07-06 10:40:05 -07001870
1871 if (!skb_queue_len(&tmp)) {
1872 set_current_state(TASK_INTERRUPTIBLE);
1873 spin_unlock_bh(&p->fcoe_rx_list.lock);
1874 schedule();
1875 set_current_state(TASK_RUNNING);
1876 goto retry;
1877 }
1878
Neil Horman20dc3812012-03-09 14:50:24 -08001879 spin_unlock_bh(&p->fcoe_rx_list.lock);
1880
1881 while ((skb = __skb_dequeue(&tmp)) != NULL)
1882 fcoe_recv_frame(skb);
1883
Robert Love85b4aa42008-12-09 15:10:24 -08001884 }
1885 return 0;
1886}
1887
1888/**
Robert Love1875f272009-11-03 11:47:50 -08001889 * fcoe_dev_setup() - Setup the link change notification interface
Robert Love34f42a02009-02-27 10:55:45 -08001890 */
Randy Dunlapb0d428a2009-04-27 21:49:31 -07001891static void fcoe_dev_setup(void)
Robert Love85b4aa42008-12-09 15:10:24 -08001892{
john fastabend6f6c2aa2011-11-18 13:35:56 -08001893 register_dcbevent_notifier(&dcb_notifier);
Robert Love85b4aa42008-12-09 15:10:24 -08001894 register_netdevice_notifier(&fcoe_notifier);
1895}
1896
1897/**
Robert Love1875f272009-11-03 11:47:50 -08001898 * fcoe_dev_cleanup() - Cleanup the link change notification interface
Robert Love34f42a02009-02-27 10:55:45 -08001899 */
Robert Love85b4aa42008-12-09 15:10:24 -08001900static void fcoe_dev_cleanup(void)
1901{
john fastabend6f6c2aa2011-11-18 13:35:56 -08001902 unregister_dcbevent_notifier(&dcb_notifier);
Robert Love85b4aa42008-12-09 15:10:24 -08001903 unregister_netdevice_notifier(&fcoe_notifier);
1904}
1905
john fastabend6f6c2aa2011-11-18 13:35:56 -08001906static struct fcoe_interface *
1907fcoe_hostlist_lookup_realdev_port(struct net_device *netdev)
1908{
1909 struct fcoe_interface *fcoe;
1910 struct net_device *real_dev;
1911
1912 list_for_each_entry(fcoe, &fcoe_hostlist, list) {
1913 if (fcoe->netdev->priv_flags & IFF_802_1Q_VLAN)
1914 real_dev = vlan_dev_real_dev(fcoe->netdev);
1915 else
1916 real_dev = fcoe->netdev;
1917
1918 if (netdev == real_dev)
1919 return fcoe;
1920 }
1921 return NULL;
1922}
1923
1924static int fcoe_dcb_app_notification(struct notifier_block *notifier,
1925 ulong event, void *ptr)
1926{
1927 struct dcb_app_type *entry = ptr;
Robert Love619fe4b2012-05-22 19:06:10 -07001928 struct fcoe_ctlr *ctlr;
john fastabend6f6c2aa2011-11-18 13:35:56 -08001929 struct fcoe_interface *fcoe;
1930 struct net_device *netdev;
john fastabend6f6c2aa2011-11-18 13:35:56 -08001931 int prio;
1932
1933 if (entry->app.selector != DCB_APP_IDTYPE_ETHTYPE)
1934 return NOTIFY_OK;
1935
1936 netdev = dev_get_by_index(&init_net, entry->ifindex);
1937 if (!netdev)
1938 return NOTIFY_OK;
1939
1940 fcoe = fcoe_hostlist_lookup_realdev_port(netdev);
1941 dev_put(netdev);
1942 if (!fcoe)
1943 return NOTIFY_OK;
1944
Robert Love619fe4b2012-05-22 19:06:10 -07001945 ctlr = fcoe_to_ctlr(fcoe);
1946
john fastabend6f6c2aa2011-11-18 13:35:56 -08001947 if (entry->dcbx & DCB_CAP_DCBX_VER_CEE)
1948 prio = ffs(entry->app.priority) - 1;
1949 else
1950 prio = entry->app.priority;
1951
1952 if (prio < 0)
1953 return NOTIFY_OK;
1954
1955 if (entry->app.protocol == ETH_P_FIP ||
1956 entry->app.protocol == ETH_P_FCOE)
Robert Love619fe4b2012-05-22 19:06:10 -07001957 ctlr->priority = prio;
john fastabend6f6c2aa2011-11-18 13:35:56 -08001958
Neerav Parikh31c37a62012-09-24 11:52:45 -07001959 if (entry->app.protocol == ETH_P_FCOE)
1960 fcoe->priority = prio;
john fastabend6f6c2aa2011-11-18 13:35:56 -08001961
1962 return NOTIFY_OK;
1963}
1964
Robert Love85b4aa42008-12-09 15:10:24 -08001965/**
Robert Love1875f272009-11-03 11:47:50 -08001966 * fcoe_device_notification() - Handler for net device events
1967 * @notifier: The context of the notification
1968 * @event: The type of event
1969 * @ptr: The net device that the event was on
Robert Love85b4aa42008-12-09 15:10:24 -08001970 *
Robert Love1875f272009-11-03 11:47:50 -08001971 * This function is called by the Ethernet driver in case of link change event.
Robert Love85b4aa42008-12-09 15:10:24 -08001972 *
1973 * Returns: 0 for success
Robert Love34f42a02009-02-27 10:55:45 -08001974 */
Robert Love85b4aa42008-12-09 15:10:24 -08001975static int fcoe_device_notification(struct notifier_block *notifier,
1976 ulong event, void *ptr)
1977{
Robert Love435c8662012-11-27 06:53:35 +00001978 struct fcoe_ctlr_device *cdev;
Robert Love1875f272009-11-03 11:47:50 -08001979 struct fc_lport *lport = NULL;
Vasu Dev1d1b88d2009-07-29 17:05:45 -07001980 struct net_device *netdev = ptr;
Robert Love619fe4b2012-05-22 19:06:10 -07001981 struct fcoe_ctlr *ctlr;
Chris Leech014f5c32009-08-25 13:59:30 -07001982 struct fcoe_interface *fcoe;
Chris Leech2e70e242009-08-25 14:00:23 -07001983 struct fcoe_port *port;
Vasu Dev1bd49b42012-05-25 10:26:43 -07001984 struct fc_stats *stats;
Joe Eykholt97c83892009-03-17 11:42:40 -07001985 u32 link_possible = 1;
Robert Love85b4aa42008-12-09 15:10:24 -08001986 u32 mfs;
1987 int rc = NOTIFY_OK;
1988
Chris Leech014f5c32009-08-25 13:59:30 -07001989 list_for_each_entry(fcoe, &fcoe_hostlist, list) {
Chris Leech25024982009-08-25 13:59:35 -07001990 if (fcoe->netdev == netdev) {
Robert Love619fe4b2012-05-22 19:06:10 -07001991 ctlr = fcoe_to_ctlr(fcoe);
1992 lport = ctlr->lp;
Robert Love85b4aa42008-12-09 15:10:24 -08001993 break;
1994 }
1995 }
Robert Love1875f272009-11-03 11:47:50 -08001996 if (!lport) {
Robert Love85b4aa42008-12-09 15:10:24 -08001997 rc = NOTIFY_DONE;
1998 goto out;
1999 }
2000
Robert Love85b4aa42008-12-09 15:10:24 -08002001 switch (event) {
2002 case NETDEV_DOWN:
2003 case NETDEV_GOING_DOWN:
Joe Eykholt97c83892009-03-17 11:42:40 -07002004 link_possible = 0;
Robert Love85b4aa42008-12-09 15:10:24 -08002005 break;
2006 case NETDEV_UP:
2007 case NETDEV_CHANGE:
Robert Love85b4aa42008-12-09 15:10:24 -08002008 break;
2009 case NETDEV_CHANGEMTU:
Yi Zou7221d7e2009-10-21 16:27:52 -07002010 if (netdev->features & NETIF_F_FCOE_MTU)
2011 break;
Vasu Dev1d1b88d2009-07-29 17:05:45 -07002012 mfs = netdev->mtu - (sizeof(struct fcoe_hdr) +
2013 sizeof(struct fcoe_crc_eof));
Robert Love85b4aa42008-12-09 15:10:24 -08002014 if (mfs >= FC_MIN_MAX_FRAME)
Robert Love1875f272009-11-03 11:47:50 -08002015 fc_set_mfs(lport, mfs);
Robert Love85b4aa42008-12-09 15:10:24 -08002016 break;
2017 case NETDEV_REGISTER:
2018 break;
Chris Leech2e70e242009-08-25 14:00:23 -07002019 case NETDEV_UNREGISTER:
2020 list_del(&fcoe->list);
Robert Love619fe4b2012-05-22 19:06:10 -07002021 port = lport_priv(ctlr->lp);
Tejun Heo2ca32b42011-01-28 16:05:32 -08002022 queue_work(fcoe_wq, &port->destroy_work);
Chris Leech2e70e242009-08-25 14:00:23 -07002023 goto out;
2024 break;
Yi Zou54a5b212010-07-20 15:21:17 -07002025 case NETDEV_FEAT_CHANGE:
2026 fcoe_netdev_features_change(lport, netdev);
2027 break;
Robert Love85b4aa42008-12-09 15:10:24 -08002028 default:
Vasu Dev1d1b88d2009-07-29 17:05:45 -07002029 FCOE_NETDEV_DBG(netdev, "Unknown event %ld "
Robert Loved5488eb2009-06-10 15:30:59 -07002030 "from netdev netlink\n", event);
Robert Love85b4aa42008-12-09 15:10:24 -08002031 }
Robert Love5e4f8fe2010-05-07 15:18:35 -07002032
2033 fcoe_link_speed_update(lport);
2034
Robert Love435c8662012-11-27 06:53:35 +00002035 cdev = fcoe_ctlr_to_ctlr_dev(ctlr);
2036
2037 if (link_possible && !fcoe_link_ok(lport)) {
2038 switch (cdev->enabled) {
2039 case FCOE_CTLR_DISABLED:
2040 pr_info("Link up while interface is disabled.\n");
2041 break;
2042 case FCOE_CTLR_ENABLED:
2043 case FCOE_CTLR_UNUSED:
2044 fcoe_ctlr_link_up(ctlr);
2045 };
2046 } else if (fcoe_ctlr_link_down(ctlr)) {
2047 switch (cdev->enabled) {
2048 case FCOE_CTLR_DISABLED:
2049 pr_info("Link down while interface is disabled.\n");
2050 break;
2051 case FCOE_CTLR_ENABLED:
2052 case FCOE_CTLR_UNUSED:
2053 stats = per_cpu_ptr(lport->stats, get_cpu());
2054 stats->LinkFailureCount++;
2055 put_cpu();
2056 fcoe_clean_pending_queue(lport);
2057 };
Robert Love85b4aa42008-12-09 15:10:24 -08002058 }
2059out:
2060 return rc;
2061}
2062
2063/**
Vasu Dev55a66d32009-12-10 09:59:31 -08002064 * fcoe_disable() - Disables a FCoE interface
Yi Zou78a58242011-01-28 16:05:16 -08002065 * @netdev : The net_device object the Ethernet interface to create on
Vasu Dev55a66d32009-12-10 09:59:31 -08002066 *
Yi Zou78a58242011-01-28 16:05:16 -08002067 * Called from fcoe transport.
Vasu Dev55a66d32009-12-10 09:59:31 -08002068 *
2069 * Returns: 0 for success
Robert Love435c8662012-11-27 06:53:35 +00002070 *
2071 * Deprecated: use fcoe_ctlr_enabled()
Vasu Dev55a66d32009-12-10 09:59:31 -08002072 */
Yi Zou78a58242011-01-28 16:05:16 -08002073static int fcoe_disable(struct net_device *netdev)
Vasu Dev55a66d32009-12-10 09:59:31 -08002074{
Robert Love619fe4b2012-05-22 19:06:10 -07002075 struct fcoe_ctlr *ctlr;
Vasu Dev55a66d32009-12-10 09:59:31 -08002076 struct fcoe_interface *fcoe;
Vasu Dev55a66d32009-12-10 09:59:31 -08002077 int rc = 0;
2078
2079 mutex_lock(&fcoe_config_mutex);
Vasu Dev55a66d32009-12-10 09:59:31 -08002080
Robert Loveee5df622011-04-01 16:05:59 -07002081 rtnl_lock();
Vasu Dev55a66d32009-12-10 09:59:31 -08002082 fcoe = fcoe_hostlist_lookup_port(netdev);
2083 rtnl_unlock();
2084
Chris Leech9ee50e42010-04-09 14:22:23 -07002085 if (fcoe) {
Robert Love619fe4b2012-05-22 19:06:10 -07002086 ctlr = fcoe_to_ctlr(fcoe);
2087 fcoe_ctlr_link_down(ctlr);
2088 fcoe_clean_pending_queue(ctlr->lp);
Chris Leech9ee50e42010-04-09 14:22:23 -07002089 } else
Vasu Dev55a66d32009-12-10 09:59:31 -08002090 rc = -ENODEV;
2091
Vasu Dev55a66d32009-12-10 09:59:31 -08002092 mutex_unlock(&fcoe_config_mutex);
2093 return rc;
2094}
2095
2096/**
2097 * fcoe_enable() - Enables a FCoE interface
Yi Zou78a58242011-01-28 16:05:16 -08002098 * @netdev : The net_device object the Ethernet interface to create on
Vasu Dev55a66d32009-12-10 09:59:31 -08002099 *
Yi Zou78a58242011-01-28 16:05:16 -08002100 * Called from fcoe transport.
Vasu Dev55a66d32009-12-10 09:59:31 -08002101 *
2102 * Returns: 0 for success
2103 */
Yi Zou78a58242011-01-28 16:05:16 -08002104static int fcoe_enable(struct net_device *netdev)
Vasu Dev55a66d32009-12-10 09:59:31 -08002105{
Robert Love619fe4b2012-05-22 19:06:10 -07002106 struct fcoe_ctlr *ctlr;
Vasu Dev55a66d32009-12-10 09:59:31 -08002107 struct fcoe_interface *fcoe;
Vasu Dev55a66d32009-12-10 09:59:31 -08002108 int rc = 0;
2109
2110 mutex_lock(&fcoe_config_mutex);
Robert Loveee5df622011-04-01 16:05:59 -07002111 rtnl_lock();
Vasu Dev55a66d32009-12-10 09:59:31 -08002112 fcoe = fcoe_hostlist_lookup_port(netdev);
2113 rtnl_unlock();
2114
Robert Love619fe4b2012-05-22 19:06:10 -07002115 if (!fcoe) {
Vasu Dev55a66d32009-12-10 09:59:31 -08002116 rc = -ENODEV;
Robert Love619fe4b2012-05-22 19:06:10 -07002117 goto out;
2118 }
Vasu Dev55a66d32009-12-10 09:59:31 -08002119
Robert Love619fe4b2012-05-22 19:06:10 -07002120 ctlr = fcoe_to_ctlr(fcoe);
2121
2122 if (!fcoe_link_ok(ctlr->lp))
2123 fcoe_ctlr_link_up(ctlr);
2124
2125out:
Vasu Dev55a66d32009-12-10 09:59:31 -08002126 mutex_unlock(&fcoe_config_mutex);
2127 return rc;
2128}
2129
2130/**
Robert Love435c8662012-11-27 06:53:35 +00002131 * fcoe_ctlr_enabled() - Enable or disable an FCoE Controller
2132 * @cdev: The FCoE Controller that is being enabled or disabled
2133 *
2134 * fcoe_sysfs will ensure that the state of 'enabled' has
2135 * changed, so no checking is necessary here. This routine simply
2136 * calls fcoe_enable or fcoe_disable, both of which are deprecated.
2137 * When those routines are removed the functionality can be merged
2138 * here.
2139 */
2140static int fcoe_ctlr_enabled(struct fcoe_ctlr_device *cdev)
2141{
2142 struct fcoe_ctlr *ctlr = fcoe_ctlr_device_priv(cdev);
2143 struct fc_lport *lport = ctlr->lp;
2144 struct net_device *netdev = fcoe_netdev(lport);
2145
2146 switch (cdev->enabled) {
2147 case FCOE_CTLR_ENABLED:
2148 return fcoe_enable(netdev);
2149 case FCOE_CTLR_DISABLED:
2150 return fcoe_disable(netdev);
2151 case FCOE_CTLR_UNUSED:
2152 default:
2153 return -ENOTSUPP;
2154 };
2155}
2156
2157/**
Robert Love1875f272009-11-03 11:47:50 -08002158 * fcoe_destroy() - Destroy a FCoE interface
Yi Zou78a58242011-01-28 16:05:16 -08002159 * @netdev : The net_device object the Ethernet interface to create on
Robert Love1875f272009-11-03 11:47:50 -08002160 *
Yi Zou78a58242011-01-28 16:05:16 -08002161 * Called from fcoe transport
Robert Love85b4aa42008-12-09 15:10:24 -08002162 *
2163 * Returns: 0 for success
Robert Love34f42a02009-02-27 10:55:45 -08002164 */
Yi Zou78a58242011-01-28 16:05:16 -08002165static int fcoe_destroy(struct net_device *netdev)
Robert Love85b4aa42008-12-09 15:10:24 -08002166{
Robert Love619fe4b2012-05-22 19:06:10 -07002167 struct fcoe_ctlr *ctlr;
Chris Leech030f4e02009-08-25 14:00:02 -07002168 struct fcoe_interface *fcoe;
Vasu Devf04ca1b2011-04-01 16:06:45 -07002169 struct fc_lport *lport;
Neerav Parikhb2085a42011-06-20 16:59:51 -07002170 struct fcoe_port *port;
Mike Christie8eca3552009-10-21 16:27:44 -07002171 int rc = 0;
Robert Love85b4aa42008-12-09 15:10:24 -08002172
Chris Leechdfc1d0f2009-08-25 14:00:13 -07002173 mutex_lock(&fcoe_config_mutex);
Robert Loveee5df622011-04-01 16:05:59 -07002174 rtnl_lock();
Chris Leech2e70e242009-08-25 14:00:23 -07002175 fcoe = fcoe_hostlist_lookup_port(netdev);
2176 if (!fcoe) {
Robert Love85b4aa42008-12-09 15:10:24 -08002177 rc = -ENODEV;
Yi Zou78a58242011-01-28 16:05:16 -08002178 goto out_nodev;
Robert Love85b4aa42008-12-09 15:10:24 -08002179 }
Robert Love619fe4b2012-05-22 19:06:10 -07002180 ctlr = fcoe_to_ctlr(fcoe);
2181 lport = ctlr->lp;
Neerav Parikhb2085a42011-06-20 16:59:51 -07002182 port = lport_priv(lport);
Yi Zou54a5b212010-07-20 15:21:17 -07002183 list_del(&fcoe->list);
Neerav Parikhb2085a42011-06-20 16:59:51 -07002184 queue_work(fcoe_wq, &port->destroy_work);
Robert Love85b4aa42008-12-09 15:10:24 -08002185out_nodev:
Neerav Parikhb2085a42011-06-20 16:59:51 -07002186 rtnl_unlock();
Chris Leechdfc1d0f2009-08-25 14:00:13 -07002187 mutex_unlock(&fcoe_config_mutex);
Robert Love85b4aa42008-12-09 15:10:24 -08002188 return rc;
2189}
2190
Robert Love1875f272009-11-03 11:47:50 -08002191/**
2192 * fcoe_destroy_work() - Destroy a FCoE port in a deferred work context
2193 * @work: Handle to the FCoE port to be destroyed
2194 */
Chris Leech2e70e242009-08-25 14:00:23 -07002195static void fcoe_destroy_work(struct work_struct *work)
2196{
2197 struct fcoe_port *port;
Neerav Parikhb2085a42011-06-20 16:59:51 -07002198 struct fcoe_interface *fcoe;
Neerav Parikh94aa7432013-01-15 15:42:38 -08002199 struct Scsi_Host *shost;
2200 struct fc_host_attrs *fc_host;
2201 unsigned long flags;
2202 struct fc_vport *vport;
2203 struct fc_vport *next_vport;
Chris Leech2e70e242009-08-25 14:00:23 -07002204
2205 port = container_of(work, struct fcoe_port, destroy_work);
Neerav Parikh94aa7432013-01-15 15:42:38 -08002206 shost = port->lport->host;
2207 fc_host = shost_to_fc_host(shost);
2208
2209 /* Loop through all the vports and mark them for deletion */
2210 spin_lock_irqsave(shost->host_lock, flags);
2211 list_for_each_entry_safe(vport, next_vport, &fc_host->vports, peers) {
2212 if (vport->flags & (FC_VPORT_DEL | FC_VPORT_CREATING)) {
2213 continue;
2214 } else {
2215 vport->flags |= FC_VPORT_DELETING;
2216 queue_work(fc_host_work_q(shost),
2217 &vport->vport_delete_work);
2218 }
2219 }
2220 spin_unlock_irqrestore(shost->host_lock, flags);
2221
2222 flush_workqueue(fc_host_work_q(shost));
2223
Chris Leech2e70e242009-08-25 14:00:23 -07002224 mutex_lock(&fcoe_config_mutex);
Neerav Parikhb2085a42011-06-20 16:59:51 -07002225
Neerav Parikhb2085a42011-06-20 16:59:51 -07002226 fcoe = port->priv;
Chris Leech2e70e242009-08-25 14:00:23 -07002227 fcoe_if_destroy(port->lport);
Robert Loveccefd232012-02-10 17:18:41 -08002228 fcoe_interface_cleanup(fcoe);
Neerav Parikhb2085a42011-06-20 16:59:51 -07002229
Chris Leech2e70e242009-08-25 14:00:23 -07002230 mutex_unlock(&fcoe_config_mutex);
2231}
2232
Robert Love85b4aa42008-12-09 15:10:24 -08002233/**
Yi Zou78a58242011-01-28 16:05:16 -08002234 * fcoe_match() - Check if the FCoE is supported on the given netdevice
2235 * @netdev : The net_device object the Ethernet interface to create on
Robert Love1875f272009-11-03 11:47:50 -08002236 *
Yi Zou78a58242011-01-28 16:05:16 -08002237 * Called from fcoe transport.
2238 *
2239 * Returns: always returns true as this is the default FCoE transport,
2240 * i.e., support all netdevs.
2241 */
2242static bool fcoe_match(struct net_device *netdev)
2243{
2244 return true;
2245}
2246
2247/**
john fastabend6f6c2aa2011-11-18 13:35:56 -08002248 * fcoe_dcb_create() - Initialize DCB attributes and hooks
2249 * @netdev: The net_device object of the L2 link that should be queried
2250 * @port: The fcoe_port to bind FCoE APP priority with
2251 * @
2252 */
2253static void fcoe_dcb_create(struct fcoe_interface *fcoe)
2254{
2255#ifdef CONFIG_DCB
2256 int dcbx;
2257 u8 fup, up;
2258 struct net_device *netdev = fcoe->realdev;
Robert Love619fe4b2012-05-22 19:06:10 -07002259 struct fcoe_ctlr *ctlr = fcoe_to_ctlr(fcoe);
john fastabend6f6c2aa2011-11-18 13:35:56 -08002260 struct dcb_app app = {
2261 .priority = 0,
2262 .protocol = ETH_P_FCOE
2263 };
2264
2265 /* setup DCB priority attributes. */
2266 if (netdev && netdev->dcbnl_ops && netdev->dcbnl_ops->getdcbx) {
2267 dcbx = netdev->dcbnl_ops->getdcbx(netdev);
2268
2269 if (dcbx & DCB_CAP_DCBX_VER_IEEE) {
2270 app.selector = IEEE_8021QAZ_APP_SEL_ETHERTYPE;
2271 up = dcb_ieee_getapp_mask(netdev, &app);
2272 app.protocol = ETH_P_FIP;
2273 fup = dcb_ieee_getapp_mask(netdev, &app);
2274 } else {
2275 app.selector = DCB_APP_IDTYPE_ETHTYPE;
2276 up = dcb_getapp(netdev, &app);
2277 app.protocol = ETH_P_FIP;
2278 fup = dcb_getapp(netdev, &app);
2279 }
2280
Neerav Parikh31c37a62012-09-24 11:52:45 -07002281 fcoe->priority = ffs(up) ? ffs(up) - 1 : 0;
2282 ctlr->priority = ffs(fup) ? ffs(fup) - 1 : fcoe->priority;
john fastabend6f6c2aa2011-11-18 13:35:56 -08002283 }
2284#endif
2285}
2286
Robert Love435c8662012-11-27 06:53:35 +00002287enum fcoe_create_link_state {
2288 FCOE_CREATE_LINK_DOWN,
2289 FCOE_CREATE_LINK_UP,
2290};
2291
john fastabend6f6c2aa2011-11-18 13:35:56 -08002292/**
Robert Love435c8662012-11-27 06:53:35 +00002293 * _fcoe_create() - (internal) Create a fcoe interface
2294 * @netdev : The net_device object the Ethernet interface to create on
2295 * @fip_mode: The FIP mode for this creation
2296 * @link_state: The ctlr link state on creation
Yi Zou78a58242011-01-28 16:05:16 -08002297 *
Robert Love435c8662012-11-27 06:53:35 +00002298 * Called from either the libfcoe 'create' module parameter
2299 * via fcoe_create or from fcoe_syfs's ctlr_create file.
Robert Love85b4aa42008-12-09 15:10:24 -08002300 *
Robert Love435c8662012-11-27 06:53:35 +00002301 * libfcoe's 'create' module parameter is deprecated so some
2302 * consolidation of code can be done when that interface is
2303 * removed.
Robert Love34f42a02009-02-27 10:55:45 -08002304 */
Robert Love435c8662012-11-27 06:53:35 +00002305static int _fcoe_create(struct net_device *netdev, enum fip_state fip_mode,
2306 enum fcoe_create_link_state link_state)
Robert Love85b4aa42008-12-09 15:10:24 -08002307{
Neerav Parikhb2085a42011-06-20 16:59:51 -07002308 int rc = 0;
Robert Love8d55e502012-05-22 19:06:26 -07002309 struct fcoe_ctlr_device *ctlr_dev;
Robert Love619fe4b2012-05-22 19:06:10 -07002310 struct fcoe_ctlr *ctlr;
Chris Leech030f4e02009-08-25 14:00:02 -07002311 struct fcoe_interface *fcoe;
Chris Leechaf7f85d2009-08-25 13:59:24 -07002312 struct fc_lport *lport;
Robert Love85b4aa42008-12-09 15:10:24 -08002313
Chris Leechdfc1d0f2009-08-25 14:00:13 -07002314 mutex_lock(&fcoe_config_mutex);
Robert Loveee5df622011-04-01 16:05:59 -07002315 rtnl_lock();
Vasu Dev34ce27b2010-05-07 15:18:46 -07002316
Robert Love85b4aa42008-12-09 15:10:24 -08002317 /* look for existing lport */
2318 if (fcoe_hostlist_lookup(netdev)) {
2319 rc = -EEXIST;
Yi Zou78a58242011-01-28 16:05:16 -08002320 goto out_nodev;
Robert Love85b4aa42008-12-09 15:10:24 -08002321 }
Robert Love85b4aa42008-12-09 15:10:24 -08002322
Joe Eykholt1dd454d2010-07-20 15:20:46 -07002323 fcoe = fcoe_interface_create(netdev, fip_mode);
Robert Love7287fb92011-01-28 16:03:47 -08002324 if (IS_ERR(fcoe)) {
2325 rc = PTR_ERR(fcoe);
Yi Zou78a58242011-01-28 16:05:16 -08002326 goto out_nodev;
Chris Leech030f4e02009-08-25 14:00:02 -07002327 }
2328
Robert Love619fe4b2012-05-22 19:06:10 -07002329 ctlr = fcoe_to_ctlr(fcoe);
Robert Love8d55e502012-05-22 19:06:26 -07002330 ctlr_dev = fcoe_ctlr_to_ctlr_dev(ctlr);
2331 lport = fcoe_if_create(fcoe, &ctlr_dev->dev, 0);
Chris Leechaf7f85d2009-08-25 13:59:24 -07002332 if (IS_ERR(lport)) {
Robert Loved5488eb2009-06-10 15:30:59 -07002333 printk(KERN_ERR "fcoe: Failed to create interface (%s)\n",
Robert Love85b4aa42008-12-09 15:10:24 -08002334 netdev->name);
Robert Love85b4aa42008-12-09 15:10:24 -08002335 rc = -EIO;
Robert Love848e7d52011-08-25 12:40:47 -07002336 rtnl_unlock();
Chris Leech2e70e242009-08-25 14:00:23 -07002337 fcoe_interface_cleanup(fcoe);
Robert Love848e7d52011-08-25 12:40:47 -07002338 goto out_nortnl;
Robert Love85b4aa42008-12-09 15:10:24 -08002339 }
Chris Leech030f4e02009-08-25 14:00:02 -07002340
Chris Leech54b649f2009-08-25 14:00:07 -07002341 /* Make this the "master" N_Port */
Robert Love619fe4b2012-05-22 19:06:10 -07002342 ctlr->lp = lport;
Chris Leech030f4e02009-08-25 14:00:02 -07002343
john fastabend6f6c2aa2011-11-18 13:35:56 -08002344 /* setup DCB priority attributes. */
2345 fcoe_dcb_create(fcoe);
2346
Chris Leech54b649f2009-08-25 14:00:07 -07002347 /* start FIP Discovery and FLOGI */
2348 lport->boot_time = jiffies;
2349 fc_fabric_login(lport);
Robert Love435c8662012-11-27 06:53:35 +00002350
2351 /*
2352 * If the fcoe_ctlr_device is to be set to DISABLED
2353 * it must be done after the lport is added to the
2354 * hostlist, but before the rtnl_lock is released.
2355 * This is because the rtnl_lock protects the
2356 * hostlist that fcoe_device_notification uses. If
2357 * the FCoE Controller is intended to be created
2358 * DISABLED then 'enabled' needs to be considered
2359 * handling link events. 'enabled' must be set
2360 * before the lport can be found in the hostlist
2361 * when a link up event is received.
2362 */
2363 if (link_state == FCOE_CREATE_LINK_UP)
2364 ctlr_dev->enabled = FCOE_CTLR_ENABLED;
2365 else
2366 ctlr_dev->enabled = FCOE_CTLR_DISABLED;
2367
2368 if (link_state == FCOE_CREATE_LINK_UP &&
2369 !fcoe_link_ok(lport)) {
Robert Love22805122012-03-13 18:22:12 -07002370 rtnl_unlock();
Robert Love619fe4b2012-05-22 19:06:10 -07002371 fcoe_ctlr_link_up(ctlr);
Robert Love22805122012-03-13 18:22:12 -07002372 mutex_unlock(&fcoe_config_mutex);
2373 return rc;
2374 }
Chris Leech54b649f2009-08-25 14:00:07 -07002375
Robert Love85b4aa42008-12-09 15:10:24 -08002376out_nodev:
Vasu Dev34ce27b2010-05-07 15:18:46 -07002377 rtnl_unlock();
Robert Love848e7d52011-08-25 12:40:47 -07002378out_nortnl:
Chris Leechdfc1d0f2009-08-25 14:00:13 -07002379 mutex_unlock(&fcoe_config_mutex);
Robert Love85b4aa42008-12-09 15:10:24 -08002380 return rc;
2381}
2382
Robert Love34f42a02009-02-27 10:55:45 -08002383/**
Robert Love435c8662012-11-27 06:53:35 +00002384 * fcoe_create() - Create a fcoe interface
2385 * @netdev : The net_device object the Ethernet interface to create on
2386 * @fip_mode: The FIP mode for this creation
2387 *
2388 * Called from fcoe transport
2389 *
2390 * Returns: 0 for success
2391 */
2392static int fcoe_create(struct net_device *netdev, enum fip_state fip_mode)
2393{
2394 return _fcoe_create(netdev, fip_mode, FCOE_CREATE_LINK_UP);
2395}
2396
2397/**
2398 * fcoe_ctlr_alloc() - Allocate a fcoe interface from fcoe_sysfs
2399 * @netdev: The net_device to be used by the allocated FCoE Controller
2400 *
2401 * This routine is called from fcoe_sysfs. It will start the fcoe_ctlr
2402 * in a link_down state. The allows the user an opportunity to configure
2403 * the FCoE Controller from sysfs before enabling the FCoE Controller.
2404 *
2405 * Creating in with this routine starts the FCoE Controller in Fabric
2406 * mode. The user can change to VN2VN or another mode before enabling.
2407 */
2408static int fcoe_ctlr_alloc(struct net_device *netdev)
2409{
2410 return _fcoe_create(netdev, FIP_MODE_FABRIC,
2411 FCOE_CREATE_LINK_DOWN);
2412}
2413
2414/**
Robert Love5e4f8fe2010-05-07 15:18:35 -07002415 * fcoe_link_ok() - Check if the link is OK for a local port
2416 * @lport: The local port to check link on
2417 *
2418 * Returns: 0 if link is UP and OK, -1 if not
2419 *
2420 */
Bart Van Assche7c9c6842012-01-13 17:26:25 -08002421static int fcoe_link_ok(struct fc_lport *lport)
Robert Love5e4f8fe2010-05-07 15:18:35 -07002422{
Bhanu Prakash Gollapudi8597ae82011-01-28 16:05:37 -08002423 struct net_device *netdev = fcoe_netdev(lport);
Robert Love5e4f8fe2010-05-07 15:18:35 -07002424
2425 if (netif_oper_up(netdev))
2426 return 0;
2427 return -1;
2428}
2429
2430/**
Robert Love1875f272009-11-03 11:47:50 -08002431 * fcoe_percpu_clean() - Clear all pending skbs for an local port
2432 * @lport: The local port whose skbs are to be cleared
Joe Eykholte7a51992009-08-25 14:04:08 -07002433 *
2434 * Must be called with fcoe_create_mutex held to single-thread completion.
2435 *
2436 * This flushes the pending skbs by adding a new skb to each queue and
2437 * waiting until they are all freed. This assures us that not only are
2438 * there no packets that will be handled by the lport, but also that any
2439 * threads already handling packet have returned.
Robert Love85b4aa42008-12-09 15:10:24 -08002440 */
Bart Van Assche7c9c6842012-01-13 17:26:25 -08002441static void fcoe_percpu_clean(struct fc_lport *lport)
Robert Love85b4aa42008-12-09 15:10:24 -08002442{
Robert Love85b4aa42008-12-09 15:10:24 -08002443 struct fcoe_percpu_s *pp;
Neil Hormandd060e72012-03-09 14:50:19 -08002444 struct sk_buff *skb;
Robert Love5e5e92d2009-03-17 11:41:35 -07002445 unsigned int cpu;
Robert Love85b4aa42008-12-09 15:10:24 -08002446
Robert Love5e5e92d2009-03-17 11:41:35 -07002447 for_each_possible_cpu(cpu) {
2448 pp = &per_cpu(fcoe_percpu, cpu);
Joe Eykholte7a51992009-08-25 14:04:08 -07002449
Neil Hormandd060e72012-03-09 14:50:19 -08002450 if (!pp->thread || !cpu_online(cpu))
Joe Eykholte7a51992009-08-25 14:04:08 -07002451 continue;
Joe Eykholte7a51992009-08-25 14:04:08 -07002452
2453 skb = dev_alloc_skb(0);
Dan Carpenterb3b8abd2012-04-20 12:16:54 -07002454 if (!skb)
Joe Eykholte7a51992009-08-25 14:04:08 -07002455 continue;
Dan Carpenterb3b8abd2012-04-20 12:16:54 -07002456
Joe Eykholte7a51992009-08-25 14:04:08 -07002457 skb->destructor = fcoe_percpu_flush_done;
2458
Neil Hormandd060e72012-03-09 14:50:19 -08002459 spin_lock_bh(&pp->fcoe_rx_list.lock);
Joe Eykholte7a51992009-08-25 14:04:08 -07002460 __skb_queue_tail(&pp->fcoe_rx_list, skb);
2461 if (pp->fcoe_rx_list.qlen == 1)
2462 wake_up_process(pp->thread);
Robert Love5e5e92d2009-03-17 11:41:35 -07002463 spin_unlock_bh(&pp->fcoe_rx_list.lock);
Joe Eykholte7a51992009-08-25 14:04:08 -07002464
2465 wait_for_completion(&fcoe_flush_completion);
Robert Love85b4aa42008-12-09 15:10:24 -08002466 }
2467}
Robert Love85b4aa42008-12-09 15:10:24 -08002468
2469/**
Robert Love1875f272009-11-03 11:47:50 -08002470 * fcoe_reset() - Reset a local port
2471 * @shost: The SCSI host associated with the local port to be reset
Robert Love85b4aa42008-12-09 15:10:24 -08002472 *
Robert Love1875f272009-11-03 11:47:50 -08002473 * Returns: Always 0 (return value required by FC transport template)
Robert Love85b4aa42008-12-09 15:10:24 -08002474 */
Bart Van Assche7c9c6842012-01-13 17:26:25 -08002475static int fcoe_reset(struct Scsi_Host *shost)
Robert Love85b4aa42008-12-09 15:10:24 -08002476{
2477 struct fc_lport *lport = shost_priv(shost);
Vasu Devd2f80952011-02-25 15:03:28 -08002478 struct fcoe_port *port = lport_priv(lport);
2479 struct fcoe_interface *fcoe = port->priv;
Robert Love619fe4b2012-05-22 19:06:10 -07002480 struct fcoe_ctlr *ctlr = fcoe_to_ctlr(fcoe);
Robert Love435c8662012-11-27 06:53:35 +00002481 struct fcoe_ctlr_device *cdev = fcoe_ctlr_to_ctlr_dev(ctlr);
Vasu Devd2f80952011-02-25 15:03:28 -08002482
Robert Love619fe4b2012-05-22 19:06:10 -07002483 fcoe_ctlr_link_down(ctlr);
2484 fcoe_clean_pending_queue(ctlr->lp);
Robert Love435c8662012-11-27 06:53:35 +00002485
2486 if (cdev->enabled != FCOE_CTLR_DISABLED &&
2487 !fcoe_link_ok(ctlr->lp))
Robert Love619fe4b2012-05-22 19:06:10 -07002488 fcoe_ctlr_link_up(ctlr);
Robert Love85b4aa42008-12-09 15:10:24 -08002489 return 0;
2490}
Robert Love85b4aa42008-12-09 15:10:24 -08002491
Robert Love34f42a02009-02-27 10:55:45 -08002492/**
Robert Love1875f272009-11-03 11:47:50 -08002493 * fcoe_hostlist_lookup_port() - Find the FCoE interface associated with a net device
2494 * @netdev: The net device used as a key
Robert Love85b4aa42008-12-09 15:10:24 -08002495 *
Robert Love1875f272009-11-03 11:47:50 -08002496 * Locking: Must be called with the RNL mutex held.
2497 *
2498 * Returns: NULL or the FCoE interface
Robert Love85b4aa42008-12-09 15:10:24 -08002499 */
Chris Leech014f5c32009-08-25 13:59:30 -07002500static struct fcoe_interface *
Robert Love1875f272009-11-03 11:47:50 -08002501fcoe_hostlist_lookup_port(const struct net_device *netdev)
Robert Love85b4aa42008-12-09 15:10:24 -08002502{
Chris Leech014f5c32009-08-25 13:59:30 -07002503 struct fcoe_interface *fcoe;
Robert Love85b4aa42008-12-09 15:10:24 -08002504
Chris Leech014f5c32009-08-25 13:59:30 -07002505 list_for_each_entry(fcoe, &fcoe_hostlist, list) {
Robert Love1875f272009-11-03 11:47:50 -08002506 if (fcoe->netdev == netdev)
Chris Leech014f5c32009-08-25 13:59:30 -07002507 return fcoe;
Robert Love85b4aa42008-12-09 15:10:24 -08002508 }
Robert Love85b4aa42008-12-09 15:10:24 -08002509 return NULL;
2510}
2511
Robert Love34f42a02009-02-27 10:55:45 -08002512/**
Robert Love1875f272009-11-03 11:47:50 -08002513 * fcoe_hostlist_lookup() - Find the local port associated with a
2514 * given net device
2515 * @netdev: The netdevice used as a key
Robert Love85b4aa42008-12-09 15:10:24 -08002516 *
Robert Love1875f272009-11-03 11:47:50 -08002517 * Locking: Must be called with the RTNL mutex held
2518 *
2519 * Returns: NULL or the local port
Robert Love85b4aa42008-12-09 15:10:24 -08002520 */
Chris Leech090eb6c2009-08-25 14:00:28 -07002521static struct fc_lport *fcoe_hostlist_lookup(const struct net_device *netdev)
Robert Love85b4aa42008-12-09 15:10:24 -08002522{
Robert Love619fe4b2012-05-22 19:06:10 -07002523 struct fcoe_ctlr *ctlr;
Chris Leech014f5c32009-08-25 13:59:30 -07002524 struct fcoe_interface *fcoe;
Robert Love85b4aa42008-12-09 15:10:24 -08002525
Chris Leech014f5c32009-08-25 13:59:30 -07002526 fcoe = fcoe_hostlist_lookup_port(netdev);
Robert Love619fe4b2012-05-22 19:06:10 -07002527 ctlr = fcoe_to_ctlr(fcoe);
2528 return (fcoe) ? ctlr->lp : NULL;
Robert Love85b4aa42008-12-09 15:10:24 -08002529}
Robert Love85b4aa42008-12-09 15:10:24 -08002530
Robert Love34f42a02009-02-27 10:55:45 -08002531/**
Robert Love1875f272009-11-03 11:47:50 -08002532 * fcoe_hostlist_add() - Add the FCoE interface identified by a local
2533 * port to the hostlist
2534 * @lport: The local port that identifies the FCoE interface to be added
2535 *
2536 * Locking: must be called with the RTNL mutex held
Robert Love85b4aa42008-12-09 15:10:24 -08002537 *
2538 * Returns: 0 for success
2539 */
Chris Leech090eb6c2009-08-25 14:00:28 -07002540static int fcoe_hostlist_add(const struct fc_lport *lport)
Robert Love85b4aa42008-12-09 15:10:24 -08002541{
Chris Leech014f5c32009-08-25 13:59:30 -07002542 struct fcoe_interface *fcoe;
2543 struct fcoe_port *port;
Robert Love85b4aa42008-12-09 15:10:24 -08002544
Chris Leech014f5c32009-08-25 13:59:30 -07002545 fcoe = fcoe_hostlist_lookup_port(fcoe_netdev(lport));
2546 if (!fcoe) {
2547 port = lport_priv(lport);
Bhanu Prakash Gollapudi8597ae82011-01-28 16:05:37 -08002548 fcoe = port->priv;
Chris Leech014f5c32009-08-25 13:59:30 -07002549 list_add_tail(&fcoe->list, &fcoe_hostlist);
Robert Love85b4aa42008-12-09 15:10:24 -08002550 }
2551 return 0;
2552}
Robert Love85b4aa42008-12-09 15:10:24 -08002553
Neil Hormanf9184df2013-01-15 14:34:40 -05002554/**
2555 * fcoe_hostlist_del() - Remove the FCoE interface identified by a local
2556 * port to the hostlist
2557 * @lport: The local port that identifies the FCoE interface to be added
2558 *
2559 * Locking: must be called with the RTNL mutex held
2560 *
2561 */
2562static void fcoe_hostlist_del(const struct fc_lport *lport)
2563{
2564 struct fcoe_interface *fcoe;
2565 struct fcoe_port *port;
2566
2567 port = lport_priv(lport);
2568 fcoe = port->priv;
2569 list_del(&fcoe->list);
2570 return;
2571}
Yi Zou78a58242011-01-28 16:05:16 -08002572
2573static struct fcoe_transport fcoe_sw_transport = {
2574 .name = {FCOE_TRANSPORT_DEFAULT},
2575 .attached = false,
2576 .list = LIST_HEAD_INIT(fcoe_sw_transport.list),
2577 .match = fcoe_match,
Robert Love435c8662012-11-27 06:53:35 +00002578 .alloc = fcoe_ctlr_alloc,
Yi Zou78a58242011-01-28 16:05:16 -08002579 .create = fcoe_create,
2580 .destroy = fcoe_destroy,
2581 .enable = fcoe_enable,
2582 .disable = fcoe_disable,
2583};
2584
Robert Love34f42a02009-02-27 10:55:45 -08002585/**
Robert Love1875f272009-11-03 11:47:50 -08002586 * fcoe_init() - Initialize fcoe.ko
Robert Love85b4aa42008-12-09 15:10:24 -08002587 *
Robert Love1875f272009-11-03 11:47:50 -08002588 * Returns: 0 on success, or a negative value on failure
Robert Love34f42a02009-02-27 10:55:45 -08002589 */
Robert Love85b4aa42008-12-09 15:10:24 -08002590static int __init fcoe_init(void)
2591{
Robert Love1875f272009-11-03 11:47:50 -08002592 struct fcoe_percpu_s *p;
Robert Love38eccab2009-03-17 11:41:30 -07002593 unsigned int cpu;
Robert Love8976f422009-03-17 11:41:46 -07002594 int rc = 0;
Robert Love85b4aa42008-12-09 15:10:24 -08002595
Tejun Heo2ca32b42011-01-28 16:05:32 -08002596 fcoe_wq = alloc_workqueue("fcoe", 0, 0);
2597 if (!fcoe_wq)
2598 return -ENOMEM;
2599
Yi Zou78a58242011-01-28 16:05:16 -08002600 /* register as a fcoe transport */
2601 rc = fcoe_transport_attach(&fcoe_sw_transport);
2602 if (rc) {
2603 printk(KERN_ERR "failed to register an fcoe transport, check "
2604 "if libfcoe is loaded\n");
2605 return rc;
2606 }
2607
Chris Leechdfc1d0f2009-08-25 14:00:13 -07002608 mutex_lock(&fcoe_config_mutex);
2609
Robert Love38eccab2009-03-17 11:41:30 -07002610 for_each_possible_cpu(cpu) {
Robert Love5e5e92d2009-03-17 11:41:35 -07002611 p = &per_cpu(fcoe_percpu, cpu);
Robert Love38eccab2009-03-17 11:41:30 -07002612 skb_queue_head_init(&p->fcoe_rx_list);
2613 }
2614
Robert Love8976f422009-03-17 11:41:46 -07002615 for_each_online_cpu(cpu)
2616 fcoe_percpu_thread_create(cpu);
Robert Love85b4aa42008-12-09 15:10:24 -08002617
Robert Love8976f422009-03-17 11:41:46 -07002618 /* Initialize per CPU interrupt thread */
2619 rc = register_hotcpu_notifier(&fcoe_cpu_notifier);
2620 if (rc)
2621 goto out_free;
Robert Love85b4aa42008-12-09 15:10:24 -08002622
Robert Love8976f422009-03-17 11:41:46 -07002623 /* Setup link change notification */
Robert Love85b4aa42008-12-09 15:10:24 -08002624 fcoe_dev_setup();
2625
Chris Leech5892c322009-08-25 13:59:14 -07002626 rc = fcoe_if_init();
2627 if (rc)
2628 goto out_free;
Robert Love85b4aa42008-12-09 15:10:24 -08002629
Chris Leechdfc1d0f2009-08-25 14:00:13 -07002630 mutex_unlock(&fcoe_config_mutex);
Robert Love85b4aa42008-12-09 15:10:24 -08002631 return 0;
Robert Love8976f422009-03-17 11:41:46 -07002632
2633out_free:
2634 for_each_online_cpu(cpu) {
2635 fcoe_percpu_thread_destroy(cpu);
2636 }
Chris Leechdfc1d0f2009-08-25 14:00:13 -07002637 mutex_unlock(&fcoe_config_mutex);
Tejun Heo2ca32b42011-01-28 16:05:32 -08002638 destroy_workqueue(fcoe_wq);
Robert Love8976f422009-03-17 11:41:46 -07002639 return rc;
Robert Love85b4aa42008-12-09 15:10:24 -08002640}
2641module_init(fcoe_init);
2642
2643/**
Robert Love1875f272009-11-03 11:47:50 -08002644 * fcoe_exit() - Clean up fcoe.ko
Robert Love85b4aa42008-12-09 15:10:24 -08002645 *
Robert Love1875f272009-11-03 11:47:50 -08002646 * Returns: 0 on success or a negative value on failure
Robert Love34f42a02009-02-27 10:55:45 -08002647 */
Robert Love85b4aa42008-12-09 15:10:24 -08002648static void __exit fcoe_exit(void)
2649{
Chris Leech014f5c32009-08-25 13:59:30 -07002650 struct fcoe_interface *fcoe, *tmp;
Robert Love619fe4b2012-05-22 19:06:10 -07002651 struct fcoe_ctlr *ctlr;
Chris Leech2e70e242009-08-25 14:00:23 -07002652 struct fcoe_port *port;
Robert Love1875f272009-11-03 11:47:50 -08002653 unsigned int cpu;
Robert Love85b4aa42008-12-09 15:10:24 -08002654
Chris Leechdfc1d0f2009-08-25 14:00:13 -07002655 mutex_lock(&fcoe_config_mutex);
2656
Robert Love85b4aa42008-12-09 15:10:24 -08002657 fcoe_dev_cleanup();
2658
Vasu Dev5919a592009-03-27 09:03:29 -07002659 /* releases the associated fcoe hosts */
Chris Leech090eb6c2009-08-25 14:00:28 -07002660 rtnl_lock();
2661 list_for_each_entry_safe(fcoe, tmp, &fcoe_hostlist, list) {
Robert Love619fe4b2012-05-22 19:06:10 -07002662 ctlr = fcoe_to_ctlr(fcoe);
2663 port = lport_priv(ctlr->lp);
Neil Hormanf9184df2013-01-15 14:34:40 -05002664 fcoe_hostlist_del(port->lport);
Tejun Heo2ca32b42011-01-28 16:05:32 -08002665 queue_work(fcoe_wq, &port->destroy_work);
Chris Leechc863df32009-08-25 14:00:18 -07002666 }
Chris Leech090eb6c2009-08-25 14:00:28 -07002667 rtnl_unlock();
Robert Love85b4aa42008-12-09 15:10:24 -08002668
Robert Love8976f422009-03-17 11:41:46 -07002669 unregister_hotcpu_notifier(&fcoe_cpu_notifier);
2670
Chris Leech014f5c32009-08-25 13:59:30 -07002671 for_each_online_cpu(cpu)
Robert Love8976f422009-03-17 11:41:46 -07002672 fcoe_percpu_thread_destroy(cpu);
Robert Love85b4aa42008-12-09 15:10:24 -08002673
Chris Leechdfc1d0f2009-08-25 14:00:13 -07002674 mutex_unlock(&fcoe_config_mutex);
Chris Leech2e70e242009-08-25 14:00:23 -07002675
Tejun Heo2ca32b42011-01-28 16:05:32 -08002676 /*
2677 * destroy_work's may be chained but destroy_workqueue()
2678 * can take care of them. Just kill the fcoe_wq.
2679 */
2680 destroy_workqueue(fcoe_wq);
Chris Leech2e70e242009-08-25 14:00:23 -07002681
Tejun Heo2ca32b42011-01-28 16:05:32 -08002682 /*
2683 * Detaching from the scsi transport must happen after all
2684 * destroys are done on the fcoe_wq. destroy_workqueue will
2685 * enusre the fcoe_wq is flushed.
2686 */
Chris Leech2e70e242009-08-25 14:00:23 -07002687 fcoe_if_exit();
Yi Zou78a58242011-01-28 16:05:16 -08002688
2689 /* detach from fcoe transport */
2690 fcoe_transport_detach(&fcoe_sw_transport);
Robert Love85b4aa42008-12-09 15:10:24 -08002691}
2692module_exit(fcoe_exit);
Chris Leech11b56182009-11-03 11:46:29 -08002693
2694/**
2695 * fcoe_flogi_resp() - FCoE specific FLOGI and FDISC response handler
2696 * @seq: active sequence in the FLOGI or FDISC exchange
2697 * @fp: response frame, or error encoded in a pointer (timeout)
2698 * @arg: pointer the the fcoe_ctlr structure
2699 *
Uwe Kleine-König65155b32010-06-11 12:17:01 +02002700 * This handles MAC address management for FCoE, then passes control on to
Chris Leech11b56182009-11-03 11:46:29 -08002701 * the libfc FLOGI response handler.
2702 */
2703static void fcoe_flogi_resp(struct fc_seq *seq, struct fc_frame *fp, void *arg)
2704{
2705 struct fcoe_ctlr *fip = arg;
2706 struct fc_exch *exch = fc_seq_exch(seq);
2707 struct fc_lport *lport = exch->lp;
2708 u8 *mac;
2709
2710 if (IS_ERR(fp))
2711 goto done;
2712
2713 mac = fr_cb(fp)->granted_mac;
Vasu Dev907c07d2011-10-28 11:34:23 -07002714 /* pre-FIP */
2715 if (is_zero_ether_addr(mac))
2716 fcoe_ctlr_recv_flogi(fip, lport, fp);
2717 if (!is_zero_ether_addr(mac))
2718 fcoe_update_src_mac(lport, mac);
Chris Leech11b56182009-11-03 11:46:29 -08002719done:
2720 fc_lport_flogi_resp(seq, fp, lport);
2721}
2722
2723/**
2724 * fcoe_logo_resp() - FCoE specific LOGO response handler
2725 * @seq: active sequence in the LOGO exchange
2726 * @fp: response frame, or error encoded in a pointer (timeout)
2727 * @arg: pointer the the fcoe_ctlr structure
2728 *
Uwe Kleine-König65155b32010-06-11 12:17:01 +02002729 * This handles MAC address management for FCoE, then passes control on to
Chris Leech11b56182009-11-03 11:46:29 -08002730 * the libfc LOGO response handler.
2731 */
2732static void fcoe_logo_resp(struct fc_seq *seq, struct fc_frame *fp, void *arg)
2733{
Joe Eykholt386309c2009-11-03 11:49:16 -08002734 struct fc_lport *lport = arg;
Chris Leech11b56182009-11-03 11:46:29 -08002735 static u8 zero_mac[ETH_ALEN] = { 0 };
2736
2737 if (!IS_ERR(fp))
Joe Eykholt386309c2009-11-03 11:49:16 -08002738 fcoe_update_src_mac(lport, zero_mac);
Chris Leech11b56182009-11-03 11:46:29 -08002739 fc_lport_logo_resp(seq, fp, lport);
2740}
2741
2742/**
2743 * fcoe_elsct_send - FCoE specific ELS handler
2744 *
2745 * This does special case handling of FIP encapsualted ELS exchanges for FCoE,
2746 * using FCoE specific response handlers and passing the FIP controller as
2747 * the argument (the lport is still available from the exchange).
2748 *
2749 * Most of the work here is just handed off to the libfc routine.
2750 */
Robert Love1875f272009-11-03 11:47:50 -08002751static struct fc_seq *fcoe_elsct_send(struct fc_lport *lport, u32 did,
2752 struct fc_frame *fp, unsigned int op,
2753 void (*resp)(struct fc_seq *,
2754 struct fc_frame *,
2755 void *),
2756 void *arg, u32 timeout)
Chris Leech11b56182009-11-03 11:46:29 -08002757{
2758 struct fcoe_port *port = lport_priv(lport);
Bhanu Prakash Gollapudi8597ae82011-01-28 16:05:37 -08002759 struct fcoe_interface *fcoe = port->priv;
Robert Love619fe4b2012-05-22 19:06:10 -07002760 struct fcoe_ctlr *fip = fcoe_to_ctlr(fcoe);
Chris Leech11b56182009-11-03 11:46:29 -08002761 struct fc_frame_header *fh = fc_frame_header_get(fp);
2762
2763 switch (op) {
2764 case ELS_FLOGI:
2765 case ELS_FDISC:
Joe Eykholte10f8c62010-07-20 15:20:30 -07002766 if (lport->point_to_multipoint)
2767 break;
Chris Leech11b56182009-11-03 11:46:29 -08002768 return fc_elsct_send(lport, did, fp, op, fcoe_flogi_resp,
2769 fip, timeout);
2770 case ELS_LOGO:
2771 /* only hook onto fabric logouts, not port logouts */
2772 if (ntoh24(fh->fh_d_id) != FC_FID_FLOGI)
2773 break;
2774 return fc_elsct_send(lport, did, fp, op, fcoe_logo_resp,
Joe Eykholt386309c2009-11-03 11:49:16 -08002775 lport, timeout);
Chris Leech11b56182009-11-03 11:46:29 -08002776 }
2777 return fc_elsct_send(lport, did, fp, op, resp, arg, timeout);
2778}
2779
Chris Leech9a057532009-11-03 11:46:40 -08002780/**
2781 * fcoe_vport_create() - create an fc_host/scsi_host for a vport
2782 * @vport: fc_vport object to create a new fc_host for
2783 * @disabled: start the new fc_host in a disabled state by default?
2784 *
2785 * Returns: 0 for success
2786 */
2787static int fcoe_vport_create(struct fc_vport *vport, bool disabled)
2788{
2789 struct Scsi_Host *shost = vport_to_shost(vport);
2790 struct fc_lport *n_port = shost_priv(shost);
2791 struct fcoe_port *port = lport_priv(n_port);
Bhanu Prakash Gollapudi8597ae82011-01-28 16:05:37 -08002792 struct fcoe_interface *fcoe = port->priv;
Chris Leech9a057532009-11-03 11:46:40 -08002793 struct net_device *netdev = fcoe->netdev;
2794 struct fc_lport *vn_port;
Neerav Parikhbdf25212011-05-16 16:45:29 -07002795 int rc;
2796 char buf[32];
2797
2798 rc = fcoe_validate_vport_create(vport);
2799 if (rc) {
Bhanu Prakash Gollapudid8348952011-08-04 17:38:49 -07002800 fcoe_wwn_to_str(vport->port_name, buf, sizeof(buf));
Neerav Parikhbdf25212011-05-16 16:45:29 -07002801 printk(KERN_ERR "fcoe: Failed to create vport, "
2802 "WWPN (0x%s) already exists\n",
2803 buf);
2804 return rc;
2805 }
Chris Leech9a057532009-11-03 11:46:40 -08002806
2807 mutex_lock(&fcoe_config_mutex);
Jiri Pirko4bc71cb2011-09-03 03:34:30 +00002808 rtnl_lock();
Chris Leech9a057532009-11-03 11:46:40 -08002809 vn_port = fcoe_if_create(fcoe, &vport->dev, 1);
Jiri Pirko4bc71cb2011-09-03 03:34:30 +00002810 rtnl_unlock();
Chris Leech9a057532009-11-03 11:46:40 -08002811 mutex_unlock(&fcoe_config_mutex);
2812
2813 if (IS_ERR(vn_port)) {
2814 printk(KERN_ERR "fcoe: fcoe_vport_create(%s) failed\n",
2815 netdev->name);
2816 return -EIO;
2817 }
2818
2819 if (disabled) {
2820 fc_vport_set_state(vport, FC_VPORT_DISABLED);
2821 } else {
2822 vn_port->boot_time = jiffies;
2823 fc_fabric_login(vn_port);
2824 fc_vport_setlink(vn_port);
2825 }
2826 return 0;
2827}
2828
2829/**
2830 * fcoe_vport_destroy() - destroy the fc_host/scsi_host for a vport
2831 * @vport: fc_vport object that is being destroyed
2832 *
2833 * Returns: 0 for success
2834 */
2835static int fcoe_vport_destroy(struct fc_vport *vport)
2836{
2837 struct Scsi_Host *shost = vport_to_shost(vport);
2838 struct fc_lport *n_port = shost_priv(shost);
2839 struct fc_lport *vn_port = vport->dd_data;
Chris Leech9a057532009-11-03 11:46:40 -08002840
2841 mutex_lock(&n_port->lp_mutex);
2842 list_del(&vn_port->list);
2843 mutex_unlock(&n_port->lp_mutex);
Robert Loveccefd232012-02-10 17:18:41 -08002844
2845 mutex_lock(&fcoe_config_mutex);
2846 fcoe_if_destroy(vn_port);
2847 mutex_unlock(&fcoe_config_mutex);
2848
Chris Leech9a057532009-11-03 11:46:40 -08002849 return 0;
2850}
2851
2852/**
2853 * fcoe_vport_disable() - change vport state
2854 * @vport: vport to bring online/offline
2855 * @disable: should the vport be disabled?
2856 */
2857static int fcoe_vport_disable(struct fc_vport *vport, bool disable)
2858{
2859 struct fc_lport *lport = vport->dd_data;
2860
2861 if (disable) {
2862 fc_vport_set_state(vport, FC_VPORT_DISABLED);
2863 fc_fabric_logoff(lport);
2864 } else {
2865 lport->boot_time = jiffies;
2866 fc_fabric_login(lport);
2867 fc_vport_setlink(lport);
2868 }
2869
2870 return 0;
2871}
2872
Chris Leechdc8596d2009-11-03 11:47:18 -08002873/**
2874 * fcoe_vport_set_symbolic_name() - append vport string to symbolic name
2875 * @vport: fc_vport with a new symbolic name string
2876 *
2877 * After generating a new symbolic name string, a new RSPN_ID request is
2878 * sent to the name server. There is no response handler, so if it fails
2879 * for some reason it will not be retried.
2880 */
2881static void fcoe_set_vport_symbolic_name(struct fc_vport *vport)
2882{
2883 struct fc_lport *lport = vport->dd_data;
2884 struct fc_frame *fp;
2885 size_t len;
2886
2887 snprintf(fc_host_symbolic_name(lport->host), FC_SYMBOLIC_NAME_SIZE,
2888 "%s v%s over %s : %s", FCOE_NAME, FCOE_VERSION,
2889 fcoe_netdev(lport)->name, vport->symbolic_name);
2890
2891 if (lport->state != LPORT_ST_READY)
2892 return;
2893
2894 len = strnlen(fc_host_symbolic_name(lport->host), 255);
2895 fp = fc_frame_alloc(lport,
2896 sizeof(struct fc_ct_hdr) +
2897 sizeof(struct fc_ns_rspn) + len);
2898 if (!fp)
2899 return;
2900 lport->tt.elsct_send(lport, FC_FID_DIR_SERV, fp, FC_NS_RSPN_ID,
Joe Eykholtb94f8952009-11-03 11:50:21 -08002901 NULL, NULL, 3 * lport->r_a_tov);
Chris Leechdc8596d2009-11-03 11:47:18 -08002902}
Yi Zoub84056b2009-11-20 14:55:19 -08002903
Robert Love8d55e502012-05-22 19:06:26 -07002904static void fcoe_fcf_get_vlan_id(struct fcoe_fcf_device *fcf_dev)
2905{
2906 struct fcoe_ctlr_device *ctlr_dev =
2907 fcoe_fcf_dev_to_ctlr_dev(fcf_dev);
2908 struct fcoe_ctlr *ctlr = fcoe_ctlr_device_priv(ctlr_dev);
2909 struct fcoe_interface *fcoe = fcoe_ctlr_priv(ctlr);
2910
2911 fcf_dev->vlan_id = vlan_dev_vlan_id(fcoe->netdev);
2912}
2913
Joe Eykholt7d65b0d2010-03-12 16:08:02 -08002914/**
2915 * fcoe_set_port_id() - Callback from libfc when Port_ID is set.
2916 * @lport: the local port
2917 * @port_id: the port ID
2918 * @fp: the received frame, if any, that caused the port_id to be set.
2919 *
2920 * This routine handles the case where we received a FLOGI and are
2921 * entering point-to-point mode. We need to call fcoe_ctlr_recv_flogi()
2922 * so it can set the non-mapped mode and gateway address.
2923 *
2924 * The FLOGI LS_ACC is handled by fcoe_flogi_resp().
2925 */
2926static void fcoe_set_port_id(struct fc_lport *lport,
2927 u32 port_id, struct fc_frame *fp)
2928{
2929 struct fcoe_port *port = lport_priv(lport);
Bhanu Prakash Gollapudi8597ae82011-01-28 16:05:37 -08002930 struct fcoe_interface *fcoe = port->priv;
Robert Love619fe4b2012-05-22 19:06:10 -07002931 struct fcoe_ctlr *ctlr = fcoe_to_ctlr(fcoe);
Joe Eykholt7d65b0d2010-03-12 16:08:02 -08002932
2933 if (fp && fc_frame_payload_op(fp) == ELS_FLOGI)
Robert Love619fe4b2012-05-22 19:06:10 -07002934 fcoe_ctlr_recv_flogi(ctlr, lport, fp);
Joe Eykholt7d65b0d2010-03-12 16:08:02 -08002935}