blob: 308a016fdaeafd99c984788465a23983a1c71425 [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,
Christoph Hellwigdb5ed4d2014-11-13 15:08:42 +0100283 .change_queue_depth = scsi_change_queue_depth,
Christoph Hellwiga62182f2014-10-02 14:39:55 +0200284 .change_queue_type = scsi_change_queue_type,
Vasu Dev7f349142009-03-27 09:06:31 -0700285 .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,
Christoph Hellwig2ecb2042014-11-03 14:09:02 +0100291 .use_blk_tags = 1,
Christoph Hellwigc40ecc12014-11-13 14:25:11 +0100292 .track_queue_depth = 1,
Vasu Dev7f349142009-03-27 09:06:31 -0700293};
294
Chris Leech54b649f2009-08-25 14:00:07 -0700295/**
Robert Love1875f272009-11-03 11:47:50 -0800296 * fcoe_interface_setup() - Setup a FCoE interface
297 * @fcoe: The new FCoE interface
298 * @netdev: The net device that the fcoe interface is on
Chris Leech54b649f2009-08-25 14:00:07 -0700299 *
300 * Returns : 0 for success
Chris Leech2e70e242009-08-25 14:00:23 -0700301 * Locking: must be called with the RTNL mutex held
Chris Leech54b649f2009-08-25 14:00:07 -0700302 */
303static int fcoe_interface_setup(struct fcoe_interface *fcoe,
304 struct net_device *netdev)
305{
Robert Love619fe4b2012-05-22 19:06:10 -0700306 struct fcoe_ctlr *fip = fcoe_to_ctlr(fcoe);
Chris Leech54b649f2009-08-25 14:00:07 -0700307 struct netdev_hw_addr *ha;
Yi Zou5bab87e2009-11-03 11:49:43 -0800308 struct net_device *real_dev;
Chris Leech54b649f2009-08-25 14:00:07 -0700309 u8 flogi_maddr[ETH_ALEN];
Yi Zoub7a727f2009-10-21 16:28:03 -0700310 const struct net_device_ops *ops;
Chris Leech54b649f2009-08-25 14:00:07 -0700311
312 fcoe->netdev = netdev;
313
Yi Zoub7a727f2009-10-21 16:28:03 -0700314 /* Let LLD initialize for FCoE */
315 ops = netdev->netdev_ops;
316 if (ops->ndo_fcoe_enable) {
317 if (ops->ndo_fcoe_enable(netdev))
318 FCOE_NETDEV_DBG(netdev, "Failed to enable FCoE"
319 " specific feature for LLD.\n");
320 }
321
Chris Leech54b649f2009-08-25 14:00:07 -0700322 /* Do not support for bonding device */
Jiri Pirkocc8bdf02011-03-01 20:05:35 +0000323 if (netdev->priv_flags & IFF_BONDING && netdev->flags & IFF_MASTER) {
john fastabend59d92512009-11-03 11:48:44 -0800324 FCOE_NETDEV_DBG(netdev, "Bonded interfaces not supported\n");
Chris Leech54b649f2009-08-25 14:00:07 -0700325 return -EOPNOTSUPP;
326 }
327
328 /* look for SAN MAC address, if multiple SAN MACs exist, only
329 * use the first one for SPMA */
Yi Zou5bab87e2009-11-03 11:49:43 -0800330 real_dev = (netdev->priv_flags & IFF_802_1Q_VLAN) ?
331 vlan_dev_real_dev(netdev) : netdev;
Vasu Devd1483bb2011-09-27 21:38:13 -0700332 fcoe->realdev = real_dev;
Chris Leech54b649f2009-08-25 14:00:07 -0700333 rcu_read_lock();
Yi Zou5bab87e2009-11-03 11:49:43 -0800334 for_each_dev_addr(real_dev, ha) {
Chris Leech54b649f2009-08-25 14:00:07 -0700335 if ((ha->type == NETDEV_HW_ADDR_T_SAN) &&
Yi Zoubf361702009-11-03 11:49:38 -0800336 (is_valid_ether_addr(ha->addr))) {
Chris Leech54b649f2009-08-25 14:00:07 -0700337 memcpy(fip->ctl_src_addr, ha->addr, ETH_ALEN);
338 fip->spma = 1;
339 break;
340 }
341 }
342 rcu_read_unlock();
343
344 /* setup Source Mac Address */
345 if (!fip->spma)
346 memcpy(fip->ctl_src_addr, netdev->dev_addr, netdev->addr_len);
347
348 /*
349 * Add FCoE MAC address as second unicast MAC address
350 * or enter promiscuous mode if not capable of listening
351 * for multiple unicast MACs.
352 */
Chris Leech54b649f2009-08-25 14:00:07 -0700353 memcpy(flogi_maddr, (u8[6]) FC_FCOE_FLOGI_MAC, ETH_ALEN);
Jiri Pirkoa748ee22010-04-01 21:22:09 +0000354 dev_uc_add(netdev, flogi_maddr);
Chris Leech54b649f2009-08-25 14:00:07 -0700355 if (fip->spma)
Jiri Pirkoa748ee22010-04-01 21:22:09 +0000356 dev_uc_add(netdev, fip->ctl_src_addr);
Joe Eykholte10f8c62010-07-20 15:20:30 -0700357 if (fip->mode == FIP_MODE_VN2VN) {
358 dev_mc_add(netdev, FIP_ALL_VN2VN_MACS);
359 dev_mc_add(netdev, FIP_ALL_P2P_MACS);
360 } else
361 dev_mc_add(netdev, FIP_ALL_ENODE_MACS);
Chris Leech54b649f2009-08-25 14:00:07 -0700362
363 /*
364 * setup the receive function from ethernet driver
365 * on the ethertype for the given device
366 */
367 fcoe->fcoe_packet_type.func = fcoe_rcv;
368 fcoe->fcoe_packet_type.type = __constant_htons(ETH_P_FCOE);
369 fcoe->fcoe_packet_type.dev = netdev;
370 dev_add_pack(&fcoe->fcoe_packet_type);
371
372 fcoe->fip_packet_type.func = fcoe_fip_recv;
373 fcoe->fip_packet_type.type = htons(ETH_P_FIP);
374 fcoe->fip_packet_type.dev = netdev;
375 dev_add_pack(&fcoe->fip_packet_type);
376
377 return 0;
378}
379
Vasu Dev7f349142009-03-27 09:06:31 -0700380/**
Robert Love1875f272009-11-03 11:47:50 -0800381 * fcoe_interface_create() - Create a FCoE interface on a net device
382 * @netdev: The net device to create the FCoE interface on
Joe Eykholt1dd454d2010-07-20 15:20:46 -0700383 * @fip_mode: The mode to use for FIP
Chris Leech030f4e02009-08-25 14:00:02 -0700384 *
385 * Returns: pointer to a struct fcoe_interface or NULL on error
386 */
Joe Eykholt1dd454d2010-07-20 15:20:46 -0700387static struct fcoe_interface *fcoe_interface_create(struct net_device *netdev,
388 enum fip_state fip_mode)
Chris Leech030f4e02009-08-25 14:00:02 -0700389{
Robert Love8d55e502012-05-22 19:06:26 -0700390 struct fcoe_ctlr_device *ctlr_dev;
Robert Love619fe4b2012-05-22 19:06:10 -0700391 struct fcoe_ctlr *ctlr;
Chris Leech030f4e02009-08-25 14:00:02 -0700392 struct fcoe_interface *fcoe;
Robert Love619fe4b2012-05-22 19:06:10 -0700393 int size;
john fastabend59d92512009-11-03 11:48:44 -0800394 int err;
Chris Leech030f4e02009-08-25 14:00:02 -0700395
Robert Love7287fb92011-01-28 16:03:47 -0800396 if (!try_module_get(THIS_MODULE)) {
397 FCOE_NETDEV_DBG(netdev,
398 "Could not get a reference to the module\n");
399 fcoe = ERR_PTR(-EBUSY);
400 goto out;
401 }
402
Robert Love619fe4b2012-05-22 19:06:10 -0700403 size = sizeof(struct fcoe_ctlr) + sizeof(struct fcoe_interface);
Robert Love8d55e502012-05-22 19:06:26 -0700404 ctlr_dev = fcoe_ctlr_device_add(&netdev->dev, &fcoe_sysfs_templ,
405 size);
406 if (!ctlr_dev) {
407 FCOE_DBG("Failed to add fcoe_ctlr_device\n");
Robert Love7287fb92011-01-28 16:03:47 -0800408 fcoe = ERR_PTR(-ENOMEM);
Robert Love6f687942012-02-10 17:18:36 -0800409 goto out_putmod;
Chris Leech030f4e02009-08-25 14:00:02 -0700410 }
411
Robert Love8d55e502012-05-22 19:06:26 -0700412 ctlr = fcoe_ctlr_device_priv(ctlr_dev);
Robert Love9d348762013-09-05 07:47:27 +0000413 ctlr->cdev = ctlr_dev;
Robert Love8d55e502012-05-22 19:06:26 -0700414 fcoe = fcoe_ctlr_priv(ctlr);
415
Chris Leech2e70e242009-08-25 14:00:23 -0700416 dev_hold(netdev);
Chris Leech54b649f2009-08-25 14:00:07 -0700417
418 /*
419 * Initialize FIP.
420 */
Robert Love619fe4b2012-05-22 19:06:10 -0700421 fcoe_ctlr_init(ctlr, fip_mode);
422 ctlr->send = fcoe_fip_send;
423 ctlr->update_mac = fcoe_update_src_mac;
424 ctlr->get_src_addr = fcoe_get_src_mac;
Chris Leech54b649f2009-08-25 14:00:07 -0700425
john fastabend59d92512009-11-03 11:48:44 -0800426 err = fcoe_interface_setup(fcoe, netdev);
427 if (err) {
Robert Love619fe4b2012-05-22 19:06:10 -0700428 fcoe_ctlr_destroy(ctlr);
Robert Love8d55e502012-05-22 19:06:26 -0700429 fcoe_ctlr_device_delete(ctlr_dev);
john fastabend59d92512009-11-03 11:48:44 -0800430 dev_put(netdev);
Robert Love7287fb92011-01-28 16:03:47 -0800431 fcoe = ERR_PTR(err);
Robert Love6f687942012-02-10 17:18:36 -0800432 goto out_putmod;
john fastabend59d92512009-11-03 11:48:44 -0800433 }
Chris Leech030f4e02009-08-25 14:00:02 -0700434
Robert Love7287fb92011-01-28 16:03:47 -0800435 goto out;
436
Robert Love6f687942012-02-10 17:18:36 -0800437out_putmod:
Robert Love7287fb92011-01-28 16:03:47 -0800438 module_put(THIS_MODULE);
439out:
Chris Leech030f4e02009-08-25 14:00:02 -0700440 return fcoe;
441}
442
443/**
Vasu Dev433eba02012-04-20 12:16:27 -0700444 * fcoe_interface_remove() - remove FCoE interface from netdev
Vasu Devf04ca1b2011-04-01 16:06:45 -0700445 * @fcoe: The FCoE interface to be cleaned up
446 *
447 * Caller must be holding the RTNL mutex
448 */
Vasu Dev433eba02012-04-20 12:16:27 -0700449static void fcoe_interface_remove(struct fcoe_interface *fcoe)
Vasu Devf04ca1b2011-04-01 16:06:45 -0700450{
451 struct net_device *netdev = fcoe->netdev;
Robert Love619fe4b2012-05-22 19:06:10 -0700452 struct fcoe_ctlr *fip = fcoe_to_ctlr(fcoe);
Vasu Devf04ca1b2011-04-01 16:06:45 -0700453 u8 flogi_maddr[ETH_ALEN];
454 const struct net_device_ops *ops;
Vasu Devf04ca1b2011-04-01 16:06:45 -0700455
456 /*
457 * Don't listen for Ethernet packets anymore.
458 * synchronize_net() ensures that the packet handlers are not running
459 * on another CPU. dev_remove_pack() would do that, this calls the
460 * unsyncronized version __dev_remove_pack() to avoid multiple delays.
461 */
462 __dev_remove_pack(&fcoe->fcoe_packet_type);
463 __dev_remove_pack(&fcoe->fip_packet_type);
464 synchronize_net();
465
466 /* Delete secondary MAC addresses */
467 memcpy(flogi_maddr, (u8[6]) FC_FCOE_FLOGI_MAC, ETH_ALEN);
468 dev_uc_del(netdev, flogi_maddr);
469 if (fip->spma)
470 dev_uc_del(netdev, fip->ctl_src_addr);
471 if (fip->mode == FIP_MODE_VN2VN) {
472 dev_mc_del(netdev, FIP_ALL_VN2VN_MACS);
473 dev_mc_del(netdev, FIP_ALL_P2P_MACS);
474 } else
475 dev_mc_del(netdev, FIP_ALL_ENODE_MACS);
476
Vasu Devf04ca1b2011-04-01 16:06:45 -0700477 /* Tell the LLD we are done w/ FCoE */
478 ops = netdev->netdev_ops;
479 if (ops->ndo_fcoe_disable) {
480 if (ops->ndo_fcoe_disable(netdev))
481 FCOE_NETDEV_DBG(netdev, "Failed to disable FCoE"
482 " specific feature for LLD.\n");
483 }
Vasu Dev433eba02012-04-20 12:16:27 -0700484 fcoe->removed = 1;
485}
Neerav Parikhb2085a42011-06-20 16:59:51 -0700486
Vasu Dev433eba02012-04-20 12:16:27 -0700487
488/**
489 * fcoe_interface_cleanup() - Clean up a FCoE interface
490 * @fcoe: The FCoE interface to be cleaned up
491 */
492static void fcoe_interface_cleanup(struct fcoe_interface *fcoe)
493{
494 struct net_device *netdev = fcoe->netdev;
Robert Love619fe4b2012-05-22 19:06:10 -0700495 struct fcoe_ctlr *fip = fcoe_to_ctlr(fcoe);
Vasu Dev433eba02012-04-20 12:16:27 -0700496
497 rtnl_lock();
498 if (!fcoe->removed)
499 fcoe_interface_remove(fcoe);
Robert Love848e7d52011-08-25 12:40:47 -0700500 rtnl_unlock();
501
Neerav Parikhb2085a42011-06-20 16:59:51 -0700502 /* Release the self-reference taken during fcoe_interface_create() */
Robert Love1a8ef412012-02-10 17:18:46 -0800503 /* tear-down the FCoE controller */
504 fcoe_ctlr_destroy(fip);
Robert Love619fe4b2012-05-22 19:06:10 -0700505 scsi_host_put(fip->lp->host);
Robert Love1a8ef412012-02-10 17:18:46 -0800506 dev_put(netdev);
507 module_put(THIS_MODULE);
Vasu Devf04ca1b2011-04-01 16:06:45 -0700508}
509
510/**
Robert Love1875f272009-11-03 11:47:50 -0800511 * fcoe_fip_recv() - Handler for received FIP frames
512 * @skb: The receive skb
513 * @netdev: The associated net device
514 * @ptype: The packet_type structure which was used to register this handler
515 * @orig_dev: The original net_device the the skb was received on.
516 * (in case dev is a bond)
Vasu Devab6b85c2009-05-17 12:33:08 +0000517 *
518 * Returns: 0 for success
519 */
Robert Love1875f272009-11-03 11:47:50 -0800520static int fcoe_fip_recv(struct sk_buff *skb, struct net_device *netdev,
Vasu Devab6b85c2009-05-17 12:33:08 +0000521 struct packet_type *ptype,
522 struct net_device *orig_dev)
523{
Chris Leech259ad852009-08-25 13:59:41 -0700524 struct fcoe_interface *fcoe;
Robert Love619fe4b2012-05-22 19:06:10 -0700525 struct fcoe_ctlr *ctlr;
Vasu Devab6b85c2009-05-17 12:33:08 +0000526
Chris Leech259ad852009-08-25 13:59:41 -0700527 fcoe = container_of(ptype, struct fcoe_interface, fip_packet_type);
Robert Love619fe4b2012-05-22 19:06:10 -0700528 ctlr = fcoe_to_ctlr(fcoe);
529 fcoe_ctlr_recv(ctlr, skb);
Vasu Devab6b85c2009-05-17 12:33:08 +0000530 return 0;
531}
532
533/**
Vasu Dev980f5152011-07-27 15:11:05 -0700534 * fcoe_port_send() - Send an Ethernet-encapsulated FIP/FCoE frame
535 * @port: The FCoE port
536 * @skb: The FIP/FCoE packet to be sent
537 */
538static void fcoe_port_send(struct fcoe_port *port, struct sk_buff *skb)
539{
540 if (port->fcoe_pending_queue.qlen)
541 fcoe_check_wait_queue(port->lport, skb);
542 else if (fcoe_start_io(skb))
543 fcoe_check_wait_queue(port->lport, skb);
544}
545
546/**
Robert Love1875f272009-11-03 11:47:50 -0800547 * fcoe_fip_send() - Send an Ethernet-encapsulated FIP frame
548 * @fip: The FCoE controller
549 * @skb: The FIP packet to be sent
Vasu Devab6b85c2009-05-17 12:33:08 +0000550 */
551static void fcoe_fip_send(struct fcoe_ctlr *fip, struct sk_buff *skb)
552{
Chris Leech3fe9a0b2009-08-25 13:59:46 -0700553 skb->dev = fcoe_from_ctlr(fip)->netdev;
Vasu Dev980f5152011-07-27 15:11:05 -0700554 fcoe_port_send(lport_priv(fip->lp), skb);
Vasu Devab6b85c2009-05-17 12:33:08 +0000555}
556
557/**
Robert Love1875f272009-11-03 11:47:50 -0800558 * fcoe_update_src_mac() - Update the Ethernet MAC filters
559 * @lport: The local port to update the source MAC on
560 * @addr: Unicast MAC address to add
Vasu Devab6b85c2009-05-17 12:33:08 +0000561 *
562 * Remove any previously-set unicast MAC filter.
563 * Add secondary FCoE MAC address filter for our OUI.
564 */
Chris Leech11b56182009-11-03 11:46:29 -0800565static void fcoe_update_src_mac(struct fc_lport *lport, u8 *addr)
Vasu Devab6b85c2009-05-17 12:33:08 +0000566{
Chris Leech11b56182009-11-03 11:46:29 -0800567 struct fcoe_port *port = lport_priv(lport);
Bhanu Prakash Gollapudi8597ae82011-01-28 16:05:37 -0800568 struct fcoe_interface *fcoe = port->priv;
Vasu Devab6b85c2009-05-17 12:33:08 +0000569
Chris Leech11b56182009-11-03 11:46:29 -0800570 if (!is_zero_ether_addr(port->data_src_addr))
Jiri Pirkoa748ee22010-04-01 21:22:09 +0000571 dev_uc_del(fcoe->netdev, port->data_src_addr);
Chris Leech11b56182009-11-03 11:46:29 -0800572 if (!is_zero_ether_addr(addr))
Jiri Pirkoa748ee22010-04-01 21:22:09 +0000573 dev_uc_add(fcoe->netdev, addr);
Chris Leech11b56182009-11-03 11:46:29 -0800574 memcpy(port->data_src_addr, addr, ETH_ALEN);
Vasu Devab6b85c2009-05-17 12:33:08 +0000575}
576
577/**
Chris Leech11b56182009-11-03 11:46:29 -0800578 * fcoe_get_src_mac() - return the Ethernet source address for an lport
579 * @lport: libfc lport
580 */
581static u8 *fcoe_get_src_mac(struct fc_lport *lport)
582{
583 struct fcoe_port *port = lport_priv(lport);
584
585 return port->data_src_addr;
586}
587
588/**
Robert Love1875f272009-11-03 11:47:50 -0800589 * fcoe_lport_config() - Set up a local port
590 * @lport: The local port to be setup
Vasu Dev7f349142009-03-27 09:06:31 -0700591 *
592 * Returns: 0 for success
593 */
Robert Love1875f272009-11-03 11:47:50 -0800594static int fcoe_lport_config(struct fc_lport *lport)
Vasu Dev7f349142009-03-27 09:06:31 -0700595{
Robert Love1875f272009-11-03 11:47:50 -0800596 lport->link_up = 0;
597 lport->qfull = 0;
598 lport->max_retry_count = 3;
599 lport->max_rport_retry_count = 3;
600 lport->e_d_tov = 2 * 1000; /* FC-FS default */
601 lport->r_a_tov = 2 * 2 * 1000;
602 lport->service_params = (FCP_SPPF_INIT_FCN | FCP_SPPF_RD_XRDY_DIS |
603 FCP_SPPF_RETRY | FCP_SPPF_CONF_COMPL);
604 lport->does_npiv = 1;
Vasu Dev7f349142009-03-27 09:06:31 -0700605
Robert Love1875f272009-11-03 11:47:50 -0800606 fc_lport_init_stats(lport);
Vasu Dev7f349142009-03-27 09:06:31 -0700607
608 /* lport fc_lport related configuration */
Robert Love1875f272009-11-03 11:47:50 -0800609 fc_lport_config(lport);
Vasu Dev7f349142009-03-27 09:06:31 -0700610
611 /* offload related configuration */
Robert Love1875f272009-11-03 11:47:50 -0800612 lport->crc_offload = 0;
613 lport->seq_offload = 0;
614 lport->lro_enabled = 0;
615 lport->lro_xid = 0;
616 lport->lso_max = 0;
Vasu Dev7f349142009-03-27 09:06:31 -0700617
618 return 0;
619}
620
621/**
Yi Zou54a5b212010-07-20 15:21:17 -0700622 * fcoe_netdev_features_change - Updates the lport's offload flags based
623 * on the LLD netdev's FCoE feature flags
624 */
625static void fcoe_netdev_features_change(struct fc_lport *lport,
626 struct net_device *netdev)
627{
628 mutex_lock(&lport->lp_mutex);
629
630 if (netdev->features & NETIF_F_SG)
631 lport->sg_supp = 1;
632 else
633 lport->sg_supp = 0;
634
635 if (netdev->features & NETIF_F_FCOE_CRC) {
636 lport->crc_offload = 1;
637 FCOE_NETDEV_DBG(netdev, "Supports FCCRC offload\n");
638 } else {
639 lport->crc_offload = 0;
640 }
641
642 if (netdev->features & NETIF_F_FSO) {
643 lport->seq_offload = 1;
644 lport->lso_max = netdev->gso_max_size;
645 FCOE_NETDEV_DBG(netdev, "Supports LSO for max len 0x%x\n",
646 lport->lso_max);
647 } else {
648 lport->seq_offload = 0;
649 lport->lso_max = 0;
650 }
651
652 if (netdev->fcoe_ddp_xid) {
653 lport->lro_enabled = 1;
654 lport->lro_xid = netdev->fcoe_ddp_xid;
655 FCOE_NETDEV_DBG(netdev, "Supports LRO for max xid 0x%x\n",
656 lport->lro_xid);
657 } else {
658 lport->lro_enabled = 0;
659 lport->lro_xid = 0;
660 }
661
662 mutex_unlock(&lport->lp_mutex);
663}
664
665/**
Robert Love1875f272009-11-03 11:47:50 -0800666 * fcoe_netdev_config() - Set up net devive for SW FCoE
667 * @lport: The local port that is associated with the net device
668 * @netdev: The associated net device
Vasu Dev7f349142009-03-27 09:06:31 -0700669 *
Robert Love1875f272009-11-03 11:47:50 -0800670 * Must be called after fcoe_lport_config() as it will use local port mutex
Vasu Dev7f349142009-03-27 09:06:31 -0700671 *
Robert Love1875f272009-11-03 11:47:50 -0800672 * Returns: 0 for success
Vasu Dev7f349142009-03-27 09:06:31 -0700673 */
Robert Love1875f272009-11-03 11:47:50 -0800674static int fcoe_netdev_config(struct fc_lport *lport, struct net_device *netdev)
Vasu Dev7f349142009-03-27 09:06:31 -0700675{
676 u32 mfs;
677 u64 wwnn, wwpn;
Chris Leech25024982009-08-25 13:59:35 -0700678 struct fcoe_interface *fcoe;
Robert Love619fe4b2012-05-22 19:06:10 -0700679 struct fcoe_ctlr *ctlr;
Chris Leech014f5c32009-08-25 13:59:30 -0700680 struct fcoe_port *port;
Vasu Dev7f349142009-03-27 09:06:31 -0700681
682 /* Setup lport private data to point to fcoe softc */
Robert Love1875f272009-11-03 11:47:50 -0800683 port = lport_priv(lport);
Bhanu Prakash Gollapudi8597ae82011-01-28 16:05:37 -0800684 fcoe = port->priv;
Robert Love619fe4b2012-05-22 19:06:10 -0700685 ctlr = fcoe_to_ctlr(fcoe);
Vasu Dev7f349142009-03-27 09:06:31 -0700686
687 /*
688 * Determine max frame size based on underlying device and optional
689 * user-configured limit. If the MFS is too low, fcoe_link_ok()
690 * will return 0, so do this first.
691 */
Yi Zou7221d7e2009-10-21 16:27:52 -0700692 mfs = netdev->mtu;
693 if (netdev->features & NETIF_F_FCOE_MTU) {
694 mfs = FCOE_MTU;
695 FCOE_NETDEV_DBG(netdev, "Supports FCOE_MTU of %d bytes\n", mfs);
696 }
697 mfs -= (sizeof(struct fcoe_hdr) + sizeof(struct fcoe_crc_eof));
Robert Love1875f272009-11-03 11:47:50 -0800698 if (fc_set_mfs(lport, mfs))
Vasu Dev7f349142009-03-27 09:06:31 -0700699 return -EINVAL;
700
Vasu Dev7f349142009-03-27 09:06:31 -0700701 /* offload features support */
Yi Zou54a5b212010-07-20 15:21:17 -0700702 fcoe_netdev_features_change(lport, netdev);
Vasu Dev7f349142009-03-27 09:06:31 -0700703
Chris Leech014f5c32009-08-25 13:59:30 -0700704 skb_queue_head_init(&port->fcoe_pending_queue);
705 port->fcoe_pending_queue_active = 0;
Robert Love1875f272009-11-03 11:47:50 -0800706 setup_timer(&port->timer, fcoe_queue_timer, (unsigned long)lport);
Vasu Dev7f349142009-03-27 09:06:31 -0700707
Robert Love5e4f8fe2010-05-07 15:18:35 -0700708 fcoe_link_speed_update(lport);
709
Robert Love1875f272009-11-03 11:47:50 -0800710 if (!lport->vport) {
Yi Zoudcece412009-11-20 15:22:21 -0800711 if (fcoe_get_wwn(netdev, &wwnn, NETDEV_FCOE_WWNN))
Robert Love619fe4b2012-05-22 19:06:10 -0700712 wwnn = fcoe_wwn_from_mac(ctlr->ctl_src_addr, 1, 0);
Robert Love1875f272009-11-03 11:47:50 -0800713 fc_set_wwnn(lport, wwnn);
Yi Zoudcece412009-11-20 15:22:21 -0800714 if (fcoe_get_wwn(netdev, &wwpn, NETDEV_FCOE_WWPN))
Robert Love619fe4b2012-05-22 19:06:10 -0700715 wwpn = fcoe_wwn_from_mac(ctlr->ctl_src_addr,
Vasu Devcf4aebca2010-07-20 15:21:22 -0700716 2, 0);
Robert Love1875f272009-11-03 11:47:50 -0800717 fc_set_wwpn(lport, wwpn);
Chris Leech9a057532009-11-03 11:46:40 -0800718 }
Vasu Dev7f349142009-03-27 09:06:31 -0700719
Vasu Dev7f349142009-03-27 09:06:31 -0700720 return 0;
721}
722
723/**
Robert Love1875f272009-11-03 11:47:50 -0800724 * fcoe_shost_config() - Set up the SCSI host associated with a local port
725 * @lport: The local port
Robert Love1875f272009-11-03 11:47:50 -0800726 * @dev: The device associated with the SCSI host
Vasu Dev7f349142009-03-27 09:06:31 -0700727 *
728 * Must be called after fcoe_lport_config() and fcoe_netdev_config()
729 *
Robert Love1875f272009-11-03 11:47:50 -0800730 * Returns: 0 for success
Vasu Dev7f349142009-03-27 09:06:31 -0700731 */
Vasu Dev8ba00a42010-04-09 14:22:54 -0700732static int fcoe_shost_config(struct fc_lport *lport, struct device *dev)
Vasu Dev7f349142009-03-27 09:06:31 -0700733{
734 int rc = 0;
735
736 /* lport scsi host config */
Robert Love1875f272009-11-03 11:47:50 -0800737 lport->host->max_lun = FCOE_MAX_LUN;
738 lport->host->max_id = FCOE_MAX_FCP_TARGET;
739 lport->host->max_channel = 0;
Vasu Devda87bfa2010-04-09 14:22:59 -0700740 lport->host->max_cmd_len = FCOE_MAX_CMD_LEN;
741
Robert Love1875f272009-11-03 11:47:50 -0800742 if (lport->vport)
Yi Zou8ca86f82011-01-28 16:05:11 -0800743 lport->host->transportt = fcoe_vport_scsi_transport;
Chris Leeche9084bb2009-11-03 11:46:34 -0800744 else
Yi Zou8ca86f82011-01-28 16:05:11 -0800745 lport->host->transportt = fcoe_nport_scsi_transport;
Vasu Dev7f349142009-03-27 09:06:31 -0700746
747 /* add the new host to the SCSI-ml */
Robert Love1875f272009-11-03 11:47:50 -0800748 rc = scsi_add_host(lport->host, dev);
Vasu Dev7f349142009-03-27 09:06:31 -0700749 if (rc) {
Robert Love1875f272009-11-03 11:47:50 -0800750 FCOE_NETDEV_DBG(fcoe_netdev(lport), "fcoe_shost_config: "
Robert Loved5488eb2009-06-10 15:30:59 -0700751 "error on scsi_add_host\n");
Vasu Dev7f349142009-03-27 09:06:31 -0700752 return rc;
753 }
Chris Leech9a057532009-11-03 11:46:40 -0800754
Robert Love1875f272009-11-03 11:47:50 -0800755 if (!lport->vport)
Alexey Dobriyan4be929b2010-05-24 14:33:03 -0700756 fc_host_max_npiv_vports(lport->host) = USHRT_MAX;
Chris Leech9a057532009-11-03 11:46:40 -0800757
Robert Love1875f272009-11-03 11:47:50 -0800758 snprintf(fc_host_symbolic_name(lport->host), FC_SYMBOLIC_NAME_SIZE,
Chris Leech5baa17c2009-11-03 11:46:56 -0800759 "%s v%s over %s", FCOE_NAME, FCOE_VERSION,
Robert Love1875f272009-11-03 11:47:50 -0800760 fcoe_netdev(lport)->name);
Vasu Dev7f349142009-03-27 09:06:31 -0700761
762 return 0;
763}
764
Neerav Parikh6fef3902012-01-22 17:30:10 -0800765
766/**
767 * fcoe_fdmi_info() - Get FDMI related info from net devive for SW FCoE
768 * @lport: The local port that is associated with the net device
769 * @netdev: The associated net device
770 *
771 * Must be called after fcoe_shost_config() as it will use local port mutex
772 *
773 */
774static void fcoe_fdmi_info(struct fc_lport *lport, struct net_device *netdev)
775{
776 struct fcoe_interface *fcoe;
777 struct fcoe_port *port;
778 struct net_device *realdev;
779 int rc;
Neerav Parikh6fef3902012-01-22 17:30:10 -0800780
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) {
Neerav Parikhf07d46b2013-05-18 05:12:28 +0000793 struct netdev_fcoe_hbainfo *fdmi;
794 fdmi = kzalloc(sizeof(*fdmi), GFP_KERNEL);
795 if (!fdmi)
796 return;
797
Neerav Parikh6fef3902012-01-22 17:30:10 -0800798 rc = realdev->netdev_ops->ndo_fcoe_get_hbainfo(realdev,
Neerav Parikhf07d46b2013-05-18 05:12:28 +0000799 fdmi);
Neerav Parikh6fef3902012-01-22 17:30:10 -0800800 if (rc) {
801 printk(KERN_INFO "fcoe: Failed to retrieve FDMI "
802 "information from netdev.\n");
803 return;
804 }
805
806 snprintf(fc_host_serial_number(lport->host),
807 FC_SERIAL_NUMBER_SIZE,
808 "%s",
Neerav Parikhf07d46b2013-05-18 05:12:28 +0000809 fdmi->serial_number);
Neerav Parikh6fef3902012-01-22 17:30:10 -0800810 snprintf(fc_host_manufacturer(lport->host),
811 FC_SERIAL_NUMBER_SIZE,
812 "%s",
Neerav Parikhf07d46b2013-05-18 05:12:28 +0000813 fdmi->manufacturer);
Neerav Parikh6fef3902012-01-22 17:30:10 -0800814 snprintf(fc_host_model(lport->host),
815 FC_SYMBOLIC_NAME_SIZE,
816 "%s",
Neerav Parikhf07d46b2013-05-18 05:12:28 +0000817 fdmi->model);
Neerav Parikh6fef3902012-01-22 17:30:10 -0800818 snprintf(fc_host_model_description(lport->host),
819 FC_SYMBOLIC_NAME_SIZE,
820 "%s",
Neerav Parikhf07d46b2013-05-18 05:12:28 +0000821 fdmi->model_description);
Neerav Parikh6fef3902012-01-22 17:30:10 -0800822 snprintf(fc_host_hardware_version(lport->host),
823 FC_VERSION_STRING_SIZE,
824 "%s",
Neerav Parikhf07d46b2013-05-18 05:12:28 +0000825 fdmi->hardware_version);
Neerav Parikh6fef3902012-01-22 17:30:10 -0800826 snprintf(fc_host_driver_version(lport->host),
827 FC_VERSION_STRING_SIZE,
828 "%s",
Neerav Parikhf07d46b2013-05-18 05:12:28 +0000829 fdmi->driver_version);
Neerav Parikh6fef3902012-01-22 17:30:10 -0800830 snprintf(fc_host_optionrom_version(lport->host),
831 FC_VERSION_STRING_SIZE,
832 "%s",
Neerav Parikhf07d46b2013-05-18 05:12:28 +0000833 fdmi->optionrom_version);
Neerav Parikh6fef3902012-01-22 17:30:10 -0800834 snprintf(fc_host_firmware_version(lport->host),
835 FC_VERSION_STRING_SIZE,
836 "%s",
Neerav Parikhf07d46b2013-05-18 05:12:28 +0000837 fdmi->firmware_version);
Neerav Parikh6fef3902012-01-22 17:30:10 -0800838
839 /* Enable FDMI lport states */
840 lport->fdmi_enabled = 1;
Neerav Parikhf07d46b2013-05-18 05:12:28 +0000841 kfree(fdmi);
Neerav Parikh6fef3902012-01-22 17:30:10 -0800842 } else {
843 lport->fdmi_enabled = 0;
844 printk(KERN_INFO "fcoe: No FDMI support.\n");
845 }
846}
847
Robert Love1875f272009-11-03 11:47:50 -0800848/**
849 * fcoe_oem_match() - The match routine for the offloaded exchange manager
850 * @fp: The I/O frame
Vasu Devd7179682009-07-29 17:05:21 -0700851 *
Robert Love1875f272009-11-03 11:47:50 -0800852 * This routine will be associated with an exchange manager (EM). When
853 * the libfc exchange handling code is looking for an EM to use it will
854 * call this routine and pass it the frame that it wishes to send. This
855 * routine will return True if the associated EM is to be used and False
856 * if the echange code should continue looking for an EM.
857 *
858 * The offload EM that this routine is associated with will handle any
859 * packets that are for SCSI read requests.
860 *
Kiran Patil1ff99182011-06-20 16:59:15 -0700861 * This has been enhanced to work when FCoE stack is operating in target
862 * mode.
863 *
Robert Love1875f272009-11-03 11:47:50 -0800864 * Returns: True for read types I/O, otherwise returns false.
Vasu Devd7179682009-07-29 17:05:21 -0700865 */
Bart Van Assche7c9c6842012-01-13 17:26:25 -0800866static bool fcoe_oem_match(struct fc_frame *fp)
Vasu Devd7179682009-07-29 17:05:21 -0700867{
Kiran Patil1ff99182011-06-20 16:59:15 -0700868 struct fc_frame_header *fh = fc_frame_header_get(fp);
869 struct fcp_cmnd *fcp;
870
871 if (fc_fcp_is_read(fr_fsp(fp)) &&
872 (fr_fsp(fp)->data_len > fcoe_ddp_min))
873 return true;
Yi Zoua762dce2012-01-13 17:26:15 -0800874 else if ((fr_fsp(fp) == NULL) &&
875 (fh->fh_r_ctl == FC_RCTL_DD_UNSOL_CMD) &&
876 (ntohs(fh->fh_rx_id) == FC_XID_UNKNOWN)) {
Kiran Patil1ff99182011-06-20 16:59:15 -0700877 fcp = fc_frame_payload_get(fp, sizeof(*fcp));
Yi Zoua762dce2012-01-13 17:26:15 -0800878 if ((fcp->fc_flags & FCP_CFL_WRDATA) &&
879 (ntohl(fcp->fc_dl) > fcoe_ddp_min))
Kiran Patil1ff99182011-06-20 16:59:15 -0700880 return true;
881 }
882 return false;
Vasu Devd7179682009-07-29 17:05:21 -0700883}
884
Vasu Dev7f349142009-03-27 09:06:31 -0700885/**
Robert Love1875f272009-11-03 11:47:50 -0800886 * fcoe_em_config() - Allocate and configure an exchange manager
887 * @lport: The local port that the new EM will be associated with
Vasu Dev7f349142009-03-27 09:06:31 -0700888 *
Robert Love1875f272009-11-03 11:47:50 -0800889 * Returns: 0 on success
Vasu Dev7f349142009-03-27 09:06:31 -0700890 */
Robert Love1875f272009-11-03 11:47:50 -0800891static inline int fcoe_em_config(struct fc_lport *lport)
Vasu Dev7f349142009-03-27 09:06:31 -0700892{
Robert Love1875f272009-11-03 11:47:50 -0800893 struct fcoe_port *port = lport_priv(lport);
Bhanu Prakash Gollapudi8597ae82011-01-28 16:05:37 -0800894 struct fcoe_interface *fcoe = port->priv;
Chris Leech25024982009-08-25 13:59:35 -0700895 struct fcoe_interface *oldfcoe = NULL;
Vasu Dev1d1b88d2009-07-29 17:05:45 -0700896 struct net_device *old_real_dev, *cur_real_dev;
Vasu Devd7179682009-07-29 17:05:21 -0700897 u16 min_xid = FCOE_MIN_XID;
898 u16 max_xid = FCOE_MAX_XID;
899
900 /*
901 * Check if need to allocate an em instance for
902 * offload exchange ids to be shared across all VN_PORTs/lport.
903 */
Robert Love1875f272009-11-03 11:47:50 -0800904 if (!lport->lro_enabled || !lport->lro_xid ||
905 (lport->lro_xid >= max_xid)) {
906 lport->lro_xid = 0;
Vasu Devd7179682009-07-29 17:05:21 -0700907 goto skip_oem;
908 }
909
910 /*
911 * Reuse existing offload em instance in case
Vasu Dev1d1b88d2009-07-29 17:05:45 -0700912 * it is already allocated on real eth device
Vasu Devd7179682009-07-29 17:05:21 -0700913 */
Chris Leech25024982009-08-25 13:59:35 -0700914 if (fcoe->netdev->priv_flags & IFF_802_1Q_VLAN)
915 cur_real_dev = vlan_dev_real_dev(fcoe->netdev);
Vasu Dev1d1b88d2009-07-29 17:05:45 -0700916 else
Chris Leech25024982009-08-25 13:59:35 -0700917 cur_real_dev = fcoe->netdev;
Vasu Dev1d1b88d2009-07-29 17:05:45 -0700918
Chris Leech25024982009-08-25 13:59:35 -0700919 list_for_each_entry(oldfcoe, &fcoe_hostlist, list) {
Chris Leech25024982009-08-25 13:59:35 -0700920 if (oldfcoe->netdev->priv_flags & IFF_802_1Q_VLAN)
921 old_real_dev = vlan_dev_real_dev(oldfcoe->netdev);
Vasu Dev1d1b88d2009-07-29 17:05:45 -0700922 else
Chris Leech25024982009-08-25 13:59:35 -0700923 old_real_dev = oldfcoe->netdev;
Vasu Dev1d1b88d2009-07-29 17:05:45 -0700924
925 if (cur_real_dev == old_real_dev) {
Chris Leech991cbb62009-08-25 13:59:51 -0700926 fcoe->oem = oldfcoe->oem;
Vasu Devd7179682009-07-29 17:05:21 -0700927 break;
928 }
929 }
930
Chris Leech991cbb62009-08-25 13:59:51 -0700931 if (fcoe->oem) {
Robert Love1875f272009-11-03 11:47:50 -0800932 if (!fc_exch_mgr_add(lport, fcoe->oem, fcoe_oem_match)) {
Vasu Devd7179682009-07-29 17:05:21 -0700933 printk(KERN_ERR "fcoe_em_config: failed to add "
934 "offload em:%p on interface:%s\n",
Chris Leech991cbb62009-08-25 13:59:51 -0700935 fcoe->oem, fcoe->netdev->name);
Vasu Devd7179682009-07-29 17:05:21 -0700936 return -ENOMEM;
937 }
938 } else {
Robert Love1875f272009-11-03 11:47:50 -0800939 fcoe->oem = fc_exch_mgr_alloc(lport, FC_CLASS_3,
940 FCOE_MIN_XID, lport->lro_xid,
941 fcoe_oem_match);
Chris Leech991cbb62009-08-25 13:59:51 -0700942 if (!fcoe->oem) {
Vasu Devd7179682009-07-29 17:05:21 -0700943 printk(KERN_ERR "fcoe_em_config: failed to allocate "
944 "em for offload exches on interface:%s\n",
Chris Leech25024982009-08-25 13:59:35 -0700945 fcoe->netdev->name);
Vasu Devd7179682009-07-29 17:05:21 -0700946 return -ENOMEM;
947 }
948 }
949
950 /*
951 * Exclude offload EM xid range from next EM xid range.
952 */
Robert Love1875f272009-11-03 11:47:50 -0800953 min_xid += lport->lro_xid + 1;
Vasu Devd7179682009-07-29 17:05:21 -0700954
955skip_oem:
Robert Love1875f272009-11-03 11:47:50 -0800956 if (!fc_exch_mgr_alloc(lport, FC_CLASS_3, min_xid, max_xid, NULL)) {
Vasu Devd7179682009-07-29 17:05:21 -0700957 printk(KERN_ERR "fcoe_em_config: failed to "
Chris Leech25024982009-08-25 13:59:35 -0700958 "allocate em on interface %s\n", fcoe->netdev->name);
Vasu Dev7f349142009-03-27 09:06:31 -0700959 return -ENOMEM;
Vasu Devd7179682009-07-29 17:05:21 -0700960 }
Vasu Dev7f349142009-03-27 09:06:31 -0700961
962 return 0;
963}
964
965/**
Robert Love1875f272009-11-03 11:47:50 -0800966 * fcoe_if_destroy() - Tear down a SW FCoE instance
967 * @lport: The local port to be destroyed
Vasu Dev34ce27b2010-05-07 15:18:46 -0700968 *
Vasu Dev7f349142009-03-27 09:06:31 -0700969 */
Chris Leechaf7f85d2009-08-25 13:59:24 -0700970static void fcoe_if_destroy(struct fc_lport *lport)
Vasu Dev7f349142009-03-27 09:06:31 -0700971{
Neerav Parikhb2085a42011-06-20 16:59:51 -0700972 struct fcoe_port *port = lport_priv(lport);
973 struct fcoe_interface *fcoe = port->priv;
974 struct net_device *netdev = fcoe->netdev;
975
976 FCOE_NETDEV_DBG(netdev, "Destroying interface\n");
977
978 /* Logout of the fabric */
979 fc_fabric_logoff(lport);
980
981 /* Cleanup the fc_lport */
982 fc_lport_destroy(lport);
983
984 /* Stop the transmit retry timer */
985 del_timer_sync(&port->timer);
986
987 /* Free existing transmit skbs */
988 fcoe_clean_pending_queue(lport);
989
990 rtnl_lock();
991 if (!is_zero_ether_addr(port->data_src_addr))
992 dev_uc_del(netdev, port->data_src_addr);
Vasu Dev433eba02012-04-20 12:16:27 -0700993 if (lport->vport)
994 synchronize_net();
995 else
996 fcoe_interface_remove(fcoe);
Neerav Parikhb2085a42011-06-20 16:59:51 -0700997 rtnl_unlock();
998
Chris Leech54b649f2009-08-25 14:00:07 -0700999 /* Free queued packets for the per-CPU receive threads */
1000 fcoe_percpu_clean(lport);
1001
Vasu Dev7f349142009-03-27 09:06:31 -07001002 /* Detach from the scsi-ml */
Chris Leechaf7f85d2009-08-25 13:59:24 -07001003 fc_remove_host(lport->host);
1004 scsi_remove_host(lport->host);
Vasu Dev7f349142009-03-27 09:06:31 -07001005
Yi Zou80e736f2010-11-30 16:18:07 -08001006 /* Destroy lport scsi_priv */
1007 fc_fcp_destroy(lport);
1008
Vasu Dev7f349142009-03-27 09:06:31 -07001009 /* There are no more rports or I/O, free the EM */
Chris Leechaf7f85d2009-08-25 13:59:24 -07001010 fc_exch_mgr_free(lport);
Vasu Dev7f349142009-03-27 09:06:31 -07001011
Vasu Dev7f349142009-03-27 09:06:31 -07001012 /* Free memory used by statistical counters */
Chris Leechaf7f85d2009-08-25 13:59:24 -07001013 fc_lport_free_stats(lport);
Vasu Dev7f349142009-03-27 09:06:31 -07001014
Vasu Dev3cab4462012-04-20 12:16:38 -07001015 /*
1016 * Release the Scsi_Host for vport but hold on to
1017 * master lport until it fcoe interface fully cleaned-up.
1018 */
1019 if (lport->vport)
1020 scsi_host_put(lport->host);
Vasu Dev7f349142009-03-27 09:06:31 -07001021}
1022
Robert Love1875f272009-11-03 11:47:50 -08001023/**
1024 * fcoe_ddp_setup() - Call a LLD's ddp_setup through the net device
1025 * @lport: The local port to setup DDP for
1026 * @xid: The exchange ID for this DDP transfer
1027 * @sgl: The scatterlist describing this transfer
1028 * @sgc: The number of sg items
Vasu Dev7f349142009-03-27 09:06:31 -07001029 *
Robert Love1875f272009-11-03 11:47:50 -08001030 * Returns: 0 if the DDP context was not configured
Vasu Dev7f349142009-03-27 09:06:31 -07001031 */
Robert Love1875f272009-11-03 11:47:50 -08001032static int fcoe_ddp_setup(struct fc_lport *lport, u16 xid,
1033 struct scatterlist *sgl, unsigned int sgc)
Vasu Dev7f349142009-03-27 09:06:31 -07001034{
Robert Love1875f272009-11-03 11:47:50 -08001035 struct net_device *netdev = fcoe_netdev(lport);
Vasu Dev7f349142009-03-27 09:06:31 -07001036
Robert Love1875f272009-11-03 11:47:50 -08001037 if (netdev->netdev_ops->ndo_fcoe_ddp_setup)
1038 return netdev->netdev_ops->ndo_fcoe_ddp_setup(netdev,
1039 xid, sgl,
1040 sgc);
Vasu Dev7f349142009-03-27 09:06:31 -07001041
1042 return 0;
1043}
1044
Vasu Dev7f349142009-03-27 09:06:31 -07001045/**
Yi Zou71f89492011-06-20 16:59:10 -07001046 * fcoe_ddp_target() - Call a LLD's ddp_target through the net device
1047 * @lport: The local port to setup DDP for
1048 * @xid: The exchange ID for this DDP transfer
1049 * @sgl: The scatterlist describing this transfer
1050 * @sgc: The number of sg items
1051 *
1052 * Returns: 0 if the DDP context was not configured
1053 */
1054static int fcoe_ddp_target(struct fc_lport *lport, u16 xid,
1055 struct scatterlist *sgl, unsigned int sgc)
1056{
1057 struct net_device *netdev = fcoe_netdev(lport);
1058
1059 if (netdev->netdev_ops->ndo_fcoe_ddp_target)
1060 return netdev->netdev_ops->ndo_fcoe_ddp_target(netdev, xid,
1061 sgl, sgc);
1062
1063 return 0;
1064}
1065
1066
1067/**
Robert Love1875f272009-11-03 11:47:50 -08001068 * fcoe_ddp_done() - Call a LLD's ddp_done through the net device
1069 * @lport: The local port to complete DDP on
1070 * @xid: The exchange ID for this DDP transfer
Vasu Dev7f349142009-03-27 09:06:31 -07001071 *
Robert Love1875f272009-11-03 11:47:50 -08001072 * Returns: the length of data that have been completed by DDP
1073 */
1074static int fcoe_ddp_done(struct fc_lport *lport, u16 xid)
1075{
1076 struct net_device *netdev = fcoe_netdev(lport);
1077
1078 if (netdev->netdev_ops->ndo_fcoe_ddp_done)
1079 return netdev->netdev_ops->ndo_fcoe_ddp_done(netdev, xid);
1080 return 0;
1081}
1082
1083/**
1084 * fcoe_if_create() - Create a FCoE instance on an interface
1085 * @fcoe: The FCoE interface to create a local port on
1086 * @parent: The device pointer to be the parent in sysfs for the SCSI host
1087 * @npiv: Indicates if the port is a vport or not
Vasu Dev7f349142009-03-27 09:06:31 -07001088 *
Robert Love1875f272009-11-03 11:47:50 -08001089 * Creates a fc_lport instance and a Scsi_Host instance and configure them.
1090 *
1091 * Returns: The allocated fc_lport or an error pointer
Vasu Dev7f349142009-03-27 09:06:31 -07001092 */
Chris Leech030f4e02009-08-25 14:00:02 -07001093static struct fc_lport *fcoe_if_create(struct fcoe_interface *fcoe,
Chris Leech9a057532009-11-03 11:46:40 -08001094 struct device *parent, int npiv)
Vasu Dev7f349142009-03-27 09:06:31 -07001095{
Robert Love619fe4b2012-05-22 19:06:10 -07001096 struct fcoe_ctlr *ctlr = fcoe_to_ctlr(fcoe);
Robert Love1875f272009-11-03 11:47:50 -08001097 struct net_device *netdev = fcoe->netdev;
Vasu Dev72fa3962011-02-25 15:03:01 -08001098 struct fc_lport *lport, *n_port;
Chris Leech014f5c32009-08-25 13:59:30 -07001099 struct fcoe_port *port;
Vasu Dev72fa3962011-02-25 15:03:01 -08001100 struct Scsi_Host *shost;
Robert Love1875f272009-11-03 11:47:50 -08001101 int rc;
Chris Leech9a057532009-11-03 11:46:40 -08001102 /*
1103 * parent is only a vport if npiv is 1,
1104 * but we'll only use vport in that case so go ahead and set it
1105 */
1106 struct fc_vport *vport = dev_to_vport(parent);
Vasu Dev7f349142009-03-27 09:06:31 -07001107
Robert Loved5488eb2009-06-10 15:30:59 -07001108 FCOE_NETDEV_DBG(netdev, "Create Interface\n");
Vasu Dev7f349142009-03-27 09:06:31 -07001109
Vasu Dev72fa3962011-02-25 15:03:01 -08001110 if (!npiv)
1111 lport = libfc_host_alloc(&fcoe_shost_template, sizeof(*port));
1112 else
1113 lport = libfc_vport_create(vport, sizeof(*port));
1114
Chris Leech86221962009-11-03 11:46:08 -08001115 if (!lport) {
Chris Leech014f5c32009-08-25 13:59:30 -07001116 FCOE_NETDEV_DBG(netdev, "Could not allocate host structure\n");
1117 rc = -ENOMEM;
Chris Leech030f4e02009-08-25 14:00:02 -07001118 goto out;
Chris Leech014f5c32009-08-25 13:59:30 -07001119 }
Chris Leech014f5c32009-08-25 13:59:30 -07001120 port = lport_priv(lport);
Chris Leech2e70e242009-08-25 14:00:23 -07001121 port->lport = lport;
Bhanu Prakash Gollapudi8597ae82011-01-28 16:05:37 -08001122 port->priv = fcoe;
Yi Zou66524ec2012-12-06 06:23:43 +00001123 port->get_netdev = fcoe_netdev;
Bhanu Prakash Gollapudi8597ae82011-01-28 16:05:37 -08001124 port->max_queue_depth = FCOE_MAX_QUEUE_DEPTH;
1125 port->min_queue_depth = FCOE_MIN_QUEUE_DEPTH;
Chris Leech2e70e242009-08-25 14:00:23 -07001126 INIT_WORK(&port->destroy_work, fcoe_destroy_work);
Vasu Dev7f349142009-03-27 09:06:31 -07001127
Neil Hormanf9184df2013-01-15 14:34:40 -05001128 /*
1129 * Need to add the lport to the hostlist
1130 * so we catch NETDEV_CHANGE events.
1131 */
1132 fcoe_hostlist_add(lport);
1133
Robert Love1875f272009-11-03 11:47:50 -08001134 /* configure a fc_lport including the exchange manager */
Chris Leechaf7f85d2009-08-25 13:59:24 -07001135 rc = fcoe_lport_config(lport);
Vasu Dev7f349142009-03-27 09:06:31 -07001136 if (rc) {
Robert Loved5488eb2009-06-10 15:30:59 -07001137 FCOE_NETDEV_DBG(netdev, "Could not configure lport for the "
1138 "interface\n");
Vasu Dev7f349142009-03-27 09:06:31 -07001139 goto out_host_put;
1140 }
1141
Chris Leech9a057532009-11-03 11:46:40 -08001142 if (npiv) {
Chris Leech9f8f3aa2010-04-09 14:23:16 -07001143 FCOE_NETDEV_DBG(netdev, "Setting vport names, "
1144 "%16.16llx %16.16llx\n",
Robert Love1875f272009-11-03 11:47:50 -08001145 vport->node_name, vport->port_name);
Chris Leech9a057532009-11-03 11:46:40 -08001146 fc_set_wwnn(lport, vport->node_name);
1147 fc_set_wwpn(lport, vport->port_name);
1148 }
1149
Vasu Devab6b85c2009-05-17 12:33:08 +00001150 /* configure lport network properties */
Chris Leechaf7f85d2009-08-25 13:59:24 -07001151 rc = fcoe_netdev_config(lport, netdev);
Vasu Devab6b85c2009-05-17 12:33:08 +00001152 if (rc) {
Robert Loved5488eb2009-06-10 15:30:59 -07001153 FCOE_NETDEV_DBG(netdev, "Could not configure netdev for the "
1154 "interface\n");
Chris Leech54b649f2009-08-25 14:00:07 -07001155 goto out_lp_destroy;
Vasu Devab6b85c2009-05-17 12:33:08 +00001156 }
Joe Eykholt97c83892009-03-17 11:42:40 -07001157
Vasu Dev7f349142009-03-27 09:06:31 -07001158 /* configure lport scsi host properties */
Vasu Dev8ba00a42010-04-09 14:22:54 -07001159 rc = fcoe_shost_config(lport, parent);
Vasu Dev7f349142009-03-27 09:06:31 -07001160 if (rc) {
Robert Loved5488eb2009-06-10 15:30:59 -07001161 FCOE_NETDEV_DBG(netdev, "Could not configure shost for the "
1162 "interface\n");
Chris Leech54b649f2009-08-25 14:00:07 -07001163 goto out_lp_destroy;
Vasu Dev7f349142009-03-27 09:06:31 -07001164 }
1165
Vasu Dev7f349142009-03-27 09:06:31 -07001166 /* Initialize the library */
Robert Love619fe4b2012-05-22 19:06:10 -07001167 rc = fcoe_libfc_config(lport, ctlr, &fcoe_libfc_fcn_templ, 1);
Vasu Dev7f349142009-03-27 09:06:31 -07001168 if (rc) {
Robert Loved5488eb2009-06-10 15:30:59 -07001169 FCOE_NETDEV_DBG(netdev, "Could not configure libfc for the "
1170 "interface\n");
Vasu Dev7f349142009-03-27 09:06:31 -07001171 goto out_lp_destroy;
1172 }
1173
Neerav Parikh6fef3902012-01-22 17:30:10 -08001174 /* Initialized FDMI information */
1175 fcoe_fdmi_info(lport, netdev);
1176
Vasu Dev72fa3962011-02-25 15:03:01 -08001177 /*
1178 * fcoe_em_alloc() and fcoe_hostlist_add() both
1179 * need to be atomic with respect to other changes to the
1180 * hostlist since fcoe_em_alloc() looks for an existing EM
1181 * instance on host list updated by fcoe_hostlist_add().
1182 *
1183 * This is currently handled through the fcoe_config_mutex
1184 * begin held.
1185 */
1186 if (!npiv)
Chris Leech9a057532009-11-03 11:46:40 -08001187 /* lport exch manager allocation */
1188 rc = fcoe_em_config(lport);
Vasu Dev72fa3962011-02-25 15:03:01 -08001189 else {
1190 shost = vport_to_shost(vport);
1191 n_port = shost_priv(shost);
1192 rc = fc_exch_mgr_list_clone(n_port, lport);
1193 }
1194
1195 if (rc) {
1196 FCOE_NETDEV_DBG(netdev, "Could not configure the EM\n");
1197 goto out_lp_destroy;
Vasu Dev96316092009-07-29 17:05:00 -07001198 }
1199
Chris Leechaf7f85d2009-08-25 13:59:24 -07001200 return lport;
Vasu Dev7f349142009-03-27 09:06:31 -07001201
1202out_lp_destroy:
Chris Leechaf7f85d2009-08-25 13:59:24 -07001203 fc_exch_mgr_free(lport);
Vasu Dev7f349142009-03-27 09:06:31 -07001204out_host_put:
Neil Hormanf9184df2013-01-15 14:34:40 -05001205 fcoe_hostlist_del(lport);
Chris Leechaf7f85d2009-08-25 13:59:24 -07001206 scsi_host_put(lport->host);
1207out:
1208 return ERR_PTR(rc);
Vasu Dev7f349142009-03-27 09:06:31 -07001209}
1210
1211/**
Robert Love1875f272009-11-03 11:47:50 -08001212 * fcoe_if_init() - Initialization routine for fcoe.ko
Vasu Dev7f349142009-03-27 09:06:31 -07001213 *
Robert Love1875f272009-11-03 11:47:50 -08001214 * Attaches the SW FCoE transport to the FC transport
1215 *
1216 * Returns: 0 on success
Vasu Dev7f349142009-03-27 09:06:31 -07001217 */
1218static int __init fcoe_if_init(void)
1219{
1220 /* attach to scsi transport */
Yi Zou8ca86f82011-01-28 16:05:11 -08001221 fcoe_nport_scsi_transport =
1222 fc_attach_transport(&fcoe_nport_fc_functions);
1223 fcoe_vport_scsi_transport =
1224 fc_attach_transport(&fcoe_vport_fc_functions);
Vasu Dev7f349142009-03-27 09:06:31 -07001225
Yi Zou8ca86f82011-01-28 16:05:11 -08001226 if (!fcoe_nport_scsi_transport) {
Robert Loved5488eb2009-06-10 15:30:59 -07001227 printk(KERN_ERR "fcoe: Failed to attach to the FC transport\n");
Vasu Dev7f349142009-03-27 09:06:31 -07001228 return -ENODEV;
1229 }
1230
1231 return 0;
1232}
1233
1234/**
Robert Love1875f272009-11-03 11:47:50 -08001235 * fcoe_if_exit() - Tear down fcoe.ko
Vasu Dev7f349142009-03-27 09:06:31 -07001236 *
Robert Love1875f272009-11-03 11:47:50 -08001237 * Detaches the SW FCoE transport from the FC transport
1238 *
1239 * Returns: 0 on success
Vasu Dev7f349142009-03-27 09:06:31 -07001240 */
Bart Van Assche7c9c6842012-01-13 17:26:25 -08001241static int __exit fcoe_if_exit(void)
Vasu Dev7f349142009-03-27 09:06:31 -07001242{
Yi Zou8ca86f82011-01-28 16:05:11 -08001243 fc_release_transport(fcoe_nport_scsi_transport);
1244 fc_release_transport(fcoe_vport_scsi_transport);
1245 fcoe_nport_scsi_transport = NULL;
1246 fcoe_vport_scsi_transport = NULL;
Vasu Dev7f349142009-03-27 09:06:31 -07001247 return 0;
1248}
1249
Robert Love85b4aa42008-12-09 15:10:24 -08001250/**
Robert Love1875f272009-11-03 11:47:50 -08001251 * fcoe_percpu_thread_create() - Create a receive thread for an online CPU
1252 * @cpu: The CPU index of the CPU to create a receive thread for
Robert Love8976f422009-03-17 11:41:46 -07001253 */
1254static void fcoe_percpu_thread_create(unsigned int cpu)
1255{
1256 struct fcoe_percpu_s *p;
1257 struct task_struct *thread;
1258
1259 p = &per_cpu(fcoe_percpu, cpu);
1260
Eric Dumazet5c609ff2011-09-27 21:37:52 -07001261 thread = kthread_create_on_node(fcoe_percpu_receive_thread,
1262 (void *)p, cpu_to_node(cpu),
1263 "fcoethread/%d", cpu);
Robert Love8976f422009-03-17 11:41:46 -07001264
Joe Eykholte7a51992009-08-25 14:04:08 -07001265 if (likely(!IS_ERR(thread))) {
Robert Love8976f422009-03-17 11:41:46 -07001266 kthread_bind(thread, cpu);
1267 wake_up_process(thread);
1268
1269 spin_lock_bh(&p->fcoe_rx_list.lock);
1270 p->thread = thread;
1271 spin_unlock_bh(&p->fcoe_rx_list.lock);
1272 }
1273}
1274
1275/**
Robert Love1875f272009-11-03 11:47:50 -08001276 * fcoe_percpu_thread_destroy() - Remove the receive thread of a CPU
1277 * @cpu: The CPU index of the CPU whose receive thread is to be destroyed
Robert Love8976f422009-03-17 11:41:46 -07001278 *
1279 * Destroys a per-CPU Rx thread. Any pending skbs are moved to the
1280 * current CPU's Rx thread. If the thread being destroyed is bound to
1281 * the CPU processing this context the skbs will be freed.
1282 */
1283static void fcoe_percpu_thread_destroy(unsigned int cpu)
1284{
1285 struct fcoe_percpu_s *p;
1286 struct task_struct *thread;
1287 struct page *crc_eof;
1288 struct sk_buff *skb;
1289#ifdef CONFIG_SMP
1290 struct fcoe_percpu_s *p0;
Joe Eykholtf018b732010-03-12 16:08:55 -08001291 unsigned targ_cpu = get_cpu();
Robert Love8976f422009-03-17 11:41:46 -07001292#endif /* CONFIG_SMP */
1293
Robert Loved5488eb2009-06-10 15:30:59 -07001294 FCOE_DBG("Destroying receive thread for CPU %d\n", cpu);
Robert Love8976f422009-03-17 11:41:46 -07001295
1296 /* Prevent any new skbs from being queued for this CPU. */
1297 p = &per_cpu(fcoe_percpu, cpu);
1298 spin_lock_bh(&p->fcoe_rx_list.lock);
1299 thread = p->thread;
1300 p->thread = NULL;
1301 crc_eof = p->crc_eof_page;
1302 p->crc_eof_page = NULL;
1303 p->crc_eof_offset = 0;
1304 spin_unlock_bh(&p->fcoe_rx_list.lock);
1305
1306#ifdef CONFIG_SMP
1307 /*
1308 * Don't bother moving the skb's if this context is running
1309 * on the same CPU that is having its thread destroyed. This
1310 * can easily happen when the module is removed.
1311 */
1312 if (cpu != targ_cpu) {
1313 p0 = &per_cpu(fcoe_percpu, targ_cpu);
1314 spin_lock_bh(&p0->fcoe_rx_list.lock);
1315 if (p0->thread) {
Robert Loved5488eb2009-06-10 15:30:59 -07001316 FCOE_DBG("Moving frames from CPU %d to CPU %d\n",
1317 cpu, targ_cpu);
Robert Love8976f422009-03-17 11:41:46 -07001318
1319 while ((skb = __skb_dequeue(&p->fcoe_rx_list)) != NULL)
1320 __skb_queue_tail(&p0->fcoe_rx_list, skb);
1321 spin_unlock_bh(&p0->fcoe_rx_list.lock);
1322 } else {
1323 /*
1324 * The targeted CPU is not initialized and cannot accept
Robert Love1875f272009-11-03 11:47:50 -08001325 * new skbs. Unlock the targeted CPU and drop the skbs
Robert Love8976f422009-03-17 11:41:46 -07001326 * on the CPU that is going offline.
1327 */
1328 while ((skb = __skb_dequeue(&p->fcoe_rx_list)) != NULL)
1329 kfree_skb(skb);
1330 spin_unlock_bh(&p0->fcoe_rx_list.lock);
1331 }
1332 } else {
1333 /*
1334 * This scenario occurs when the module is being removed
1335 * and all threads are being destroyed. skbs will continue
1336 * to be shifted from the CPU thread that is being removed
1337 * to the CPU thread associated with the CPU that is processing
1338 * the module removal. Once there is only one CPU Rx thread it
1339 * will reach this case and we will drop all skbs and later
1340 * stop the thread.
1341 */
1342 spin_lock_bh(&p->fcoe_rx_list.lock);
1343 while ((skb = __skb_dequeue(&p->fcoe_rx_list)) != NULL)
1344 kfree_skb(skb);
1345 spin_unlock_bh(&p->fcoe_rx_list.lock);
1346 }
Joe Eykholtf018b732010-03-12 16:08:55 -08001347 put_cpu();
Robert Love8976f422009-03-17 11:41:46 -07001348#else
1349 /*
Chris Leechdd3fd722009-04-21 16:27:36 -07001350 * This a non-SMP scenario where the singular Rx thread is
Robert Love8976f422009-03-17 11:41:46 -07001351 * being removed. Free all skbs and stop the thread.
1352 */
1353 spin_lock_bh(&p->fcoe_rx_list.lock);
1354 while ((skb = __skb_dequeue(&p->fcoe_rx_list)) != NULL)
1355 kfree_skb(skb);
1356 spin_unlock_bh(&p->fcoe_rx_list.lock);
1357#endif
1358
1359 if (thread)
1360 kthread_stop(thread);
1361
1362 if (crc_eof)
1363 put_page(crc_eof);
1364}
1365
1366/**
Robert Love1875f272009-11-03 11:47:50 -08001367 * fcoe_cpu_callback() - Handler for CPU hotplug events
1368 * @nfb: The callback data block
1369 * @action: The event triggering the callback
1370 * @hcpu: The index of the CPU that the event is for
Robert Love8976f422009-03-17 11:41:46 -07001371 *
Robert Love1875f272009-11-03 11:47:50 -08001372 * This creates or destroys per-CPU data for fcoe
Robert Love8976f422009-03-17 11:41:46 -07001373 *
1374 * Returns NOTIFY_OK always.
1375 */
1376static int fcoe_cpu_callback(struct notifier_block *nfb,
1377 unsigned long action, void *hcpu)
1378{
1379 unsigned cpu = (unsigned long)hcpu;
1380
1381 switch (action) {
1382 case CPU_ONLINE:
1383 case CPU_ONLINE_FROZEN:
Robert Loved5488eb2009-06-10 15:30:59 -07001384 FCOE_DBG("CPU %x online: Create Rx thread\n", cpu);
Robert Love8976f422009-03-17 11:41:46 -07001385 fcoe_percpu_thread_create(cpu);
1386 break;
1387 case CPU_DEAD:
1388 case CPU_DEAD_FROZEN:
Robert Loved5488eb2009-06-10 15:30:59 -07001389 FCOE_DBG("CPU %x offline: Remove Rx thread\n", cpu);
Robert Love8976f422009-03-17 11:41:46 -07001390 fcoe_percpu_thread_destroy(cpu);
1391 break;
1392 default:
1393 break;
1394 }
1395 return NOTIFY_OK;
1396}
1397
Robert Love8976f422009-03-17 11:41:46 -07001398/**
Kiran Patil064287e2011-06-20 16:59:20 -07001399 * fcoe_select_cpu() - Selects CPU to handle post-processing of incoming
1400 * command.
Kiran Patil064287e2011-06-20 16:59:20 -07001401 *
Vasu Devd2722812011-07-27 15:11:10 -07001402 * This routine selects next CPU based on cpumask to distribute
1403 * incoming requests in round robin.
Kiran Patil064287e2011-06-20 16:59:20 -07001404 *
Vasu Devd2722812011-07-27 15:11:10 -07001405 * Returns: int CPU number
Kiran Patil064287e2011-06-20 16:59:20 -07001406 */
Vasu Devd2722812011-07-27 15:11:10 -07001407static inline unsigned int fcoe_select_cpu(void)
Kiran Patil064287e2011-06-20 16:59:20 -07001408{
1409 static unsigned int selected_cpu;
1410
Vasu Devd2722812011-07-27 15:11:10 -07001411 selected_cpu = cpumask_next(selected_cpu, cpu_online_mask);
1412 if (selected_cpu >= nr_cpu_ids)
1413 selected_cpu = cpumask_first(cpu_online_mask);
1414
Kiran Patil064287e2011-06-20 16:59:20 -07001415 return selected_cpu;
1416}
1417
1418/**
Robert Love1875f272009-11-03 11:47:50 -08001419 * fcoe_rcv() - Receive packets from a net device
1420 * @skb: The received packet
1421 * @netdev: The net device that the packet was received on
1422 * @ptype: The packet type context
1423 * @olddev: The last device net device
Robert Love85b4aa42008-12-09 15:10:24 -08001424 *
Robert Love1875f272009-11-03 11:47:50 -08001425 * This routine is called by NET_RX_SOFTIRQ. It receives a packet, builds a
1426 * FC frame and passes the frame to libfc.
Robert Love85b4aa42008-12-09 15:10:24 -08001427 *
1428 * Returns: 0 for success
Robert Love34f42a02009-02-27 10:55:45 -08001429 */
Bart Van Assche7c9c6842012-01-13 17:26:25 -08001430static int fcoe_rcv(struct sk_buff *skb, struct net_device *netdev,
Robert Love85b4aa42008-12-09 15:10:24 -08001431 struct packet_type *ptype, struct net_device *olddev)
1432{
Robert Love1875f272009-11-03 11:47:50 -08001433 struct fc_lport *lport;
Robert Love85b4aa42008-12-09 15:10:24 -08001434 struct fcoe_rcv_info *fr;
Robert Love619fe4b2012-05-22 19:06:10 -07001435 struct fcoe_ctlr *ctlr;
Chris Leech259ad852009-08-25 13:59:41 -07001436 struct fcoe_interface *fcoe;
Robert Love85b4aa42008-12-09 15:10:24 -08001437 struct fc_frame_header *fh;
Robert Love85b4aa42008-12-09 15:10:24 -08001438 struct fcoe_percpu_s *fps;
Vasu Dev519e5132010-07-20 15:19:32 -07001439 struct ethhdr *eh;
Vasu Devb2f00912009-08-25 13:58:53 -07001440 unsigned int cpu;
Robert Love85b4aa42008-12-09 15:10:24 -08001441
Chris Leech259ad852009-08-25 13:59:41 -07001442 fcoe = container_of(ptype, struct fcoe_interface, fcoe_packet_type);
Robert Love619fe4b2012-05-22 19:06:10 -07001443 ctlr = fcoe_to_ctlr(fcoe);
1444 lport = ctlr->lp;
Robert Love1875f272009-11-03 11:47:50 -08001445 if (unlikely(!lport)) {
Bart Van Assche465b87b2013-08-14 15:42:09 +00001446 FCOE_NETDEV_DBG(netdev, "Cannot find hba structure\n");
Robert Love85b4aa42008-12-09 15:10:24 -08001447 goto err2;
1448 }
Robert Love1875f272009-11-03 11:47:50 -08001449 if (!lport->link_up)
Joe Eykholt97c83892009-03-17 11:42:40 -07001450 goto err2;
Robert Love85b4aa42008-12-09 15:10:24 -08001451
Bart Van Assche465b87b2013-08-14 15:42:09 +00001452 FCOE_NETDEV_DBG(netdev,
1453 "skb_info: len:%d data_len:%d head:%p data:%p tail:%p end:%p sum:%d dev:%s\n",
Robert Loved5488eb2009-06-10 15:30:59 -07001454 skb->len, skb->data_len, skb->head, skb->data,
1455 skb_tail_pointer(skb), skb_end_pointer(skb),
1456 skb->csum, skb->dev ? skb->dev->name : "<NULL>");
Robert Love85b4aa42008-12-09 15:10:24 -08001457
Neil Horman8b6124342013-08-03 10:45:54 +00001458
1459 skb = skb_share_check(skb, GFP_ATOMIC);
1460
1461 if (skb == NULL)
1462 return NET_RX_DROP;
1463
Vasu Dev519e5132010-07-20 15:19:32 -07001464 eh = eth_hdr(skb);
Vasu Dev519e5132010-07-20 15:19:32 -07001465
Robert Love619fe4b2012-05-22 19:06:10 -07001466 if (is_fip_mode(ctlr) &&
Joe Perches6942df72013-09-02 03:32:33 +00001467 !ether_addr_equal(eh->h_source, ctlr->dest_addr)) {
Vasu Dev519e5132010-07-20 15:19:32 -07001468 FCOE_NETDEV_DBG(netdev, "wrong source mac address:%pM\n",
1469 eh->h_source);
Robert Love85b4aa42008-12-09 15:10:24 -08001470 goto err;
1471 }
1472
1473 /*
1474 * Check for minimum frame length, and make sure required FCoE
1475 * and FC headers are pulled into the linear data area.
1476 */
1477 if (unlikely((skb->len < FCOE_MIN_FRAME) ||
Robert Love1875f272009-11-03 11:47:50 -08001478 !pskb_may_pull(skb, FCOE_HEADER_LEN)))
Robert Love85b4aa42008-12-09 15:10:24 -08001479 goto err;
1480
1481 skb_set_transport_header(skb, sizeof(struct fcoe_hdr));
1482 fh = (struct fc_frame_header *) skb_transport_header(skb);
1483
Robert Love0ee31cb2010-10-08 17:12:46 -07001484 if (ntoh24(&eh->h_dest[3]) != ntoh24(fh->fh_d_id)) {
1485 FCOE_NETDEV_DBG(netdev, "FC frame d_id mismatch with MAC:%pM\n",
1486 eh->h_dest);
1487 goto err;
1488 }
1489
Robert Love85b4aa42008-12-09 15:10:24 -08001490 fr = fcoe_dev_from_skb(skb);
Robert Love1875f272009-11-03 11:47:50 -08001491 fr->fr_dev = lport;
Robert Love5e5e92d2009-03-17 11:41:35 -07001492
Robert Love85b4aa42008-12-09 15:10:24 -08001493 /*
Vasu Devb2f00912009-08-25 13:58:53 -07001494 * In case the incoming frame's exchange is originated from
1495 * the initiator, then received frame's exchange id is ANDed
1496 * with fc_cpu_mask bits to get the same cpu on which exchange
Vasu Devd2722812011-07-27 15:11:10 -07001497 * was originated, otherwise select cpu using rx exchange id
1498 * or fcoe_select_cpu().
Robert Love85b4aa42008-12-09 15:10:24 -08001499 */
Vasu Devb2f00912009-08-25 13:58:53 -07001500 if (ntoh24(fh->fh_f_ctl) & FC_FC_EX_CTX)
1501 cpu = ntohs(fh->fh_ox_id) & fc_cpu_mask;
Kiran Patil064287e2011-06-20 16:59:20 -07001502 else {
Vasu Devd2722812011-07-27 15:11:10 -07001503 if (ntohs(fh->fh_rx_id) == FC_XID_UNKNOWN)
1504 cpu = fcoe_select_cpu();
1505 else
Kiran Patil29bdd2b2011-06-20 16:59:25 -07001506 cpu = ntohs(fh->fh_rx_id) & fc_cpu_mask;
Kiran Patil064287e2011-06-20 16:59:20 -07001507 }
Vasu Dev324f6672011-07-27 15:10:39 -07001508
1509 if (cpu >= nr_cpu_ids)
1510 goto err;
1511
Robert Love8976f422009-03-17 11:41:46 -07001512 fps = &per_cpu(fcoe_percpu, cpu);
Neil Horman94aa29f2012-03-09 14:50:08 -08001513 spin_lock(&fps->fcoe_rx_list.lock);
Robert Love8976f422009-03-17 11:41:46 -07001514 if (unlikely(!fps->thread)) {
1515 /*
1516 * The targeted CPU is not ready, let's target
1517 * the first CPU now. For non-SMP systems this
1518 * will check the same CPU twice.
1519 */
Robert Love1875f272009-11-03 11:47:50 -08001520 FCOE_NETDEV_DBG(netdev, "CPU is online, but no receive thread "
Robert Loved5488eb2009-06-10 15:30:59 -07001521 "ready for incoming skb- using first online "
1522 "CPU.\n");
Robert Love8976f422009-03-17 11:41:46 -07001523
Neil Horman94aa29f2012-03-09 14:50:08 -08001524 spin_unlock(&fps->fcoe_rx_list.lock);
Rusty Russell69571772009-12-17 11:43:14 -06001525 cpu = cpumask_first(cpu_online_mask);
Robert Love8976f422009-03-17 11:41:46 -07001526 fps = &per_cpu(fcoe_percpu, cpu);
Neil Horman94aa29f2012-03-09 14:50:08 -08001527 spin_lock(&fps->fcoe_rx_list.lock);
Robert Love8976f422009-03-17 11:41:46 -07001528 if (!fps->thread) {
Neil Horman94aa29f2012-03-09 14:50:08 -08001529 spin_unlock(&fps->fcoe_rx_list.lock);
Robert Love8976f422009-03-17 11:41:46 -07001530 goto err;
1531 }
1532 }
1533
1534 /*
1535 * We now have a valid CPU that we're targeting for
1536 * this skb. We also have this receive thread locked,
1537 * so we're free to queue skbs into it's queue.
1538 */
Robert Love85b4aa42008-12-09 15:10:24 -08001539
Neil Horman5e70c4c2012-03-09 14:49:48 -08001540 /*
1541 * Note: We used to have a set of conditions under which we would
1542 * call fcoe_recv_frame directly, rather than queuing to the rx list
1543 * as it could save a few cycles, but doing so is prohibited, as
1544 * fcoe_recv_frame has several paths that may sleep, which is forbidden
1545 * in softirq context.
Chris Leech859b7b62009-11-20 14:54:47 -08001546 */
Neil Horman5e70c4c2012-03-09 14:49:48 -08001547 __skb_queue_tail(&fps->fcoe_rx_list, skb);
Neil Horman20dc3812012-03-09 14:50:24 -08001548 if (fps->thread->state == TASK_INTERRUPTIBLE)
Neil Horman5e70c4c2012-03-09 14:49:48 -08001549 wake_up_process(fps->thread);
Neil Horman94aa29f2012-03-09 14:50:08 -08001550 spin_unlock(&fps->fcoe_rx_list.lock);
Robert Love85b4aa42008-12-09 15:10:24 -08001551
Neil Horman34bac2e2013-08-03 10:45:55 +00001552 return NET_RX_SUCCESS;
Robert Love85b4aa42008-12-09 15:10:24 -08001553err:
Vasu Dev1bd49b42012-05-25 10:26:43 -07001554 per_cpu_ptr(lport->stats, get_cpu())->ErrorFrames++;
Joe Eykholtf018b732010-03-12 16:08:55 -08001555 put_cpu();
Robert Love85b4aa42008-12-09 15:10:24 -08001556err2:
1557 kfree_skb(skb);
Neil Horman34bac2e2013-08-03 10:45:55 +00001558 return NET_RX_DROP;
Robert Love85b4aa42008-12-09 15:10:24 -08001559}
Robert Love85b4aa42008-12-09 15:10:24 -08001560
1561/**
Bhanu Prakash Gollapudi8597ae82011-01-28 16:05:37 -08001562 * fcoe_alloc_paged_crc_eof() - Allocate a page to be used for the trailer CRC
Robert Love1875f272009-11-03 11:47:50 -08001563 * @skb: The packet to be transmitted
1564 * @tlen: The total length of the trailer
1565 *
Robert Love85b4aa42008-12-09 15:10:24 -08001566 * Returns: 0 for success
Robert Love34f42a02009-02-27 10:55:45 -08001567 */
Bhanu Prakash Gollapudi8597ae82011-01-28 16:05:37 -08001568static int fcoe_alloc_paged_crc_eof(struct sk_buff *skb, int tlen)
Robert Love85b4aa42008-12-09 15:10:24 -08001569{
1570 struct fcoe_percpu_s *fps;
Bhanu Prakash Gollapudi8597ae82011-01-28 16:05:37 -08001571 int rc;
Robert Love85b4aa42008-12-09 15:10:24 -08001572
Robert Love5e5e92d2009-03-17 11:41:35 -07001573 fps = &get_cpu_var(fcoe_percpu);
Bhanu Prakash Gollapudi8597ae82011-01-28 16:05:37 -08001574 rc = fcoe_get_paged_crc_eof(skb, tlen, fps);
Robert Love5e5e92d2009-03-17 11:41:35 -07001575 put_cpu_var(fcoe_percpu);
Robert Love85b4aa42008-12-09 15:10:24 -08001576
Bhanu Prakash Gollapudi8597ae82011-01-28 16:05:37 -08001577 return rc;
Robert Love85b4aa42008-12-09 15:10:24 -08001578}
Robert Love85b4aa42008-12-09 15:10:24 -08001579
1580/**
Robert Love1875f272009-11-03 11:47:50 -08001581 * fcoe_xmit() - Transmit a FCoE frame
1582 * @lport: The local port that the frame is to be transmitted for
1583 * @fp: The frame to be transmitted
Robert Love85b4aa42008-12-09 15:10:24 -08001584 *
Robert Love1875f272009-11-03 11:47:50 -08001585 * Return: 0 for success
Robert Love34f42a02009-02-27 10:55:45 -08001586 */
Bart Van Assche7c9c6842012-01-13 17:26:25 -08001587static int fcoe_xmit(struct fc_lport *lport, struct fc_frame *fp)
Robert Love85b4aa42008-12-09 15:10:24 -08001588{
Vasu Dev4bb6b512009-05-06 10:52:34 -07001589 int wlen;
Robert Love85b4aa42008-12-09 15:10:24 -08001590 u32 crc;
1591 struct ethhdr *eh;
1592 struct fcoe_crc_eof *cp;
1593 struct sk_buff *skb;
Vasu Dev1bd49b42012-05-25 10:26:43 -07001594 struct fc_stats *stats;
Robert Love85b4aa42008-12-09 15:10:24 -08001595 struct fc_frame_header *fh;
1596 unsigned int hlen; /* header length implies the version */
1597 unsigned int tlen; /* trailer length */
1598 unsigned int elen; /* eth header, may include vlan */
Robert Love1875f272009-11-03 11:47:50 -08001599 struct fcoe_port *port = lport_priv(lport);
Bhanu Prakash Gollapudi8597ae82011-01-28 16:05:37 -08001600 struct fcoe_interface *fcoe = port->priv;
Robert Love619fe4b2012-05-22 19:06:10 -07001601 struct fcoe_ctlr *ctlr = fcoe_to_ctlr(fcoe);
Robert Love85b4aa42008-12-09 15:10:24 -08001602 u8 sof, eof;
1603 struct fcoe_hdr *hp;
1604
1605 WARN_ON((fr_len(fp) % sizeof(u32)) != 0);
1606
Robert Love85b4aa42008-12-09 15:10:24 -08001607 fh = fc_frame_header_get(fp);
Joe Eykholt97c83892009-03-17 11:42:40 -07001608 skb = fp_skb(fp);
1609 wlen = skb->len / FCOE_WORD_TO_BYTE;
1610
Robert Love1875f272009-11-03 11:47:50 -08001611 if (!lport->link_up) {
Dan Carpenter3caf02e2009-04-21 16:27:25 -07001612 kfree_skb(skb);
Joe Eykholt97c83892009-03-17 11:42:40 -07001613 return 0;
Robert Love85b4aa42008-12-09 15:10:24 -08001614 }
1615
Joe Eykholt9860eeb2010-03-12 16:07:52 -08001616 if (unlikely(fh->fh_type == FC_TYPE_ELS) &&
Robert Love619fe4b2012-05-22 19:06:10 -07001617 fcoe_ctlr_els_send(ctlr, lport, skb))
Joe Eykholt97c83892009-03-17 11:42:40 -07001618 return 0;
1619
Robert Love85b4aa42008-12-09 15:10:24 -08001620 sof = fr_sof(fp);
1621 eof = fr_eof(fp);
1622
Vasu Dev4e57e1c2009-05-06 10:52:46 -07001623 elen = sizeof(struct ethhdr);
Robert Love85b4aa42008-12-09 15:10:24 -08001624 hlen = sizeof(struct fcoe_hdr);
1625 tlen = sizeof(struct fcoe_crc_eof);
1626 wlen = (skb->len - tlen + sizeof(crc)) / FCOE_WORD_TO_BYTE;
1627
1628 /* crc offload */
Robert Love1875f272009-11-03 11:47:50 -08001629 if (likely(lport->crc_offload)) {
Yi Zoucf64bc82012-03-16 23:08:12 +00001630 skb->ip_summed = CHECKSUM_UNNECESSARY;
Robert Love85b4aa42008-12-09 15:10:24 -08001631 skb->csum_start = skb_headroom(skb);
1632 skb->csum_offset = skb->len;
1633 crc = 0;
1634 } else {
1635 skb->ip_summed = CHECKSUM_NONE;
1636 crc = fcoe_fc_crc(fp);
1637 }
1638
Chris Leech014f5c32009-08-25 13:59:30 -07001639 /* copy port crc and eof to the skb buff */
Robert Love85b4aa42008-12-09 15:10:24 -08001640 if (skb_is_nonlinear(skb)) {
1641 skb_frag_t *frag;
Bhanu Prakash Gollapudi8597ae82011-01-28 16:05:37 -08001642 if (fcoe_alloc_paged_crc_eof(skb, tlen)) {
Roel Kluine9041582009-02-27 10:56:22 -08001643 kfree_skb(skb);
Robert Love85b4aa42008-12-09 15:10:24 -08001644 return -ENOMEM;
1645 }
1646 frag = &skb_shinfo(skb)->frags[skb_shinfo(skb)->nr_frags - 1];
Cong Wang77dfce02011-11-25 23:14:23 +08001647 cp = kmap_atomic(skb_frag_page(frag))
Robert Love85b4aa42008-12-09 15:10:24 -08001648 + frag->page_offset;
1649 } else {
1650 cp = (struct fcoe_crc_eof *)skb_put(skb, tlen);
1651 }
1652
1653 memset(cp, 0, sizeof(*cp));
1654 cp->fcoe_eof = eof;
1655 cp->fcoe_crc32 = cpu_to_le32(~crc);
1656
1657 if (skb_is_nonlinear(skb)) {
Cong Wang77dfce02011-11-25 23:14:23 +08001658 kunmap_atomic(cp);
Robert Love85b4aa42008-12-09 15:10:24 -08001659 cp = NULL;
1660 }
1661
Chris Leech014f5c32009-08-25 13:59:30 -07001662 /* adjust skb network/transport offsets to match mac/fcoe/port */
Robert Love85b4aa42008-12-09 15:10:24 -08001663 skb_push(skb, elen + hlen);
1664 skb_reset_mac_header(skb);
1665 skb_reset_network_header(skb);
1666 skb->mac_len = elen;
Yi Zou211c7382009-02-27 14:06:37 -08001667 skb->protocol = htons(ETH_P_FCOE);
Neerav Parikh31c37a62012-09-24 11:52:45 -07001668 skb->priority = fcoe->priority;
john fastabend6f6c2aa2011-11-18 13:35:56 -08001669
Vasu Devd1483bb2011-09-27 21:38:13 -07001670 if (fcoe->netdev->priv_flags & IFF_802_1Q_VLAN &&
Patrick McHardyf6469682013-04-19 02:04:27 +00001671 fcoe->realdev->features & NETIF_F_HW_VLAN_CTAG_TX) {
Robert Love2884d422013-06-19 01:56:54 +00001672 /* must set skb->dev before calling vlan_put_tag */
Vasu Devd1483bb2011-09-27 21:38:13 -07001673 skb->dev = fcoe->realdev;
Robert Love2884d422013-06-19 01:56:54 +00001674 skb = __vlan_hwaccel_put_tag(skb, htons(ETH_P_8021Q),
1675 vlan_dev_vlan_id(fcoe->netdev));
1676 if (!skb)
1677 return -ENOMEM;
Vasu Devd1483bb2011-09-27 21:38:13 -07001678 } else
1679 skb->dev = fcoe->netdev;
Robert Love85b4aa42008-12-09 15:10:24 -08001680
1681 /* fill up mac and fcoe headers */
1682 eh = eth_hdr(skb);
1683 eh->h_proto = htons(ETH_P_FCOE);
Robert Love619fe4b2012-05-22 19:06:10 -07001684 memcpy(eh->h_dest, ctlr->dest_addr, ETH_ALEN);
1685 if (ctlr->map_dest)
Joe Eykholtcd229e42010-07-20 15:20:40 -07001686 memcpy(eh->h_dest + 3, fh->fh_d_id, 3);
Robert Love85b4aa42008-12-09 15:10:24 -08001687
Robert Love619fe4b2012-05-22 19:06:10 -07001688 if (unlikely(ctlr->flogi_oxid != FC_XID_UNKNOWN))
1689 memcpy(eh->h_source, ctlr->ctl_src_addr, ETH_ALEN);
Robert Love85b4aa42008-12-09 15:10:24 -08001690 else
Chris Leech11b56182009-11-03 11:46:29 -08001691 memcpy(eh->h_source, port->data_src_addr, ETH_ALEN);
Robert Love85b4aa42008-12-09 15:10:24 -08001692
1693 hp = (struct fcoe_hdr *)(eh + 1);
1694 memset(hp, 0, sizeof(*hp));
1695 if (FC_FCOE_VER)
1696 FC_FCOE_ENCAPS_VER(hp, FC_FCOE_VER);
1697 hp->fcoe_sof = sof;
1698
Yi Zou39ca9a02009-02-27 14:07:15 -08001699 /* fcoe lso, mss is in max_payload which is non-zero for FCP data */
Robert Love1875f272009-11-03 11:47:50 -08001700 if (lport->seq_offload && fr_max_payload(fp)) {
Yi Zou39ca9a02009-02-27 14:07:15 -08001701 skb_shinfo(skb)->gso_type = SKB_GSO_FCOE;
1702 skb_shinfo(skb)->gso_size = fr_max_payload(fp);
1703 } else {
1704 skb_shinfo(skb)->gso_type = 0;
1705 skb_shinfo(skb)->gso_size = 0;
1706 }
Robert Love85b4aa42008-12-09 15:10:24 -08001707 /* update tx stats: regardless if LLD fails */
Vasu Dev1bd49b42012-05-25 10:26:43 -07001708 stats = per_cpu_ptr(lport->stats, get_cpu());
Robert Love582b45b2009-03-31 15:51:50 -07001709 stats->TxFrames++;
1710 stats->TxWords += wlen;
Joe Eykholtf018b732010-03-12 16:08:55 -08001711 put_cpu();
Robert Love85b4aa42008-12-09 15:10:24 -08001712
1713 /* send down to lld */
Robert Love1875f272009-11-03 11:47:50 -08001714 fr_dev(fp) = lport;
Vasu Dev980f5152011-07-27 15:11:05 -07001715 fcoe_port_send(port, skb);
Robert Love85b4aa42008-12-09 15:10:24 -08001716 return 0;
1717}
Robert Love85b4aa42008-12-09 15:10:24 -08001718
Robert Love34f42a02009-02-27 10:55:45 -08001719/**
Robert Love1875f272009-11-03 11:47:50 -08001720 * fcoe_percpu_flush_done() - Indicate per-CPU queue flush completion
1721 * @skb: The completed skb (argument required by destructor)
Joe Eykholte7a51992009-08-25 14:04:08 -07001722 */
1723static void fcoe_percpu_flush_done(struct sk_buff *skb)
1724{
1725 complete(&fcoe_flush_completion);
1726}
1727
1728/**
Vasu Dev52ee8322011-01-28 16:03:52 -08001729 * fcoe_filter_frames() - filter out bad fcoe frames, i.e. bad CRC
1730 * @lport: The local port the frame was received on
1731 * @fp: The received frame
1732 *
1733 * Return: 0 on passing filtering checks
1734 */
1735static inline int fcoe_filter_frames(struct fc_lport *lport,
1736 struct fc_frame *fp)
1737{
Robert Love619fe4b2012-05-22 19:06:10 -07001738 struct fcoe_ctlr *ctlr;
Vasu Dev52ee8322011-01-28 16:03:52 -08001739 struct fcoe_interface *fcoe;
1740 struct fc_frame_header *fh;
1741 struct sk_buff *skb = (struct sk_buff *)fp;
Vasu Dev1bd49b42012-05-25 10:26:43 -07001742 struct fc_stats *stats;
Vasu Dev52ee8322011-01-28 16:03:52 -08001743
1744 /*
1745 * We only check CRC if no offload is available and if it is
1746 * it's solicited data, in which case, the FCP layer would
1747 * check it during the copy.
1748 */
1749 if (lport->crc_offload && skb->ip_summed == CHECKSUM_UNNECESSARY)
1750 fr_flags(fp) &= ~FCPHF_CRC_UNCHECKED;
1751 else
1752 fr_flags(fp) |= FCPHF_CRC_UNCHECKED;
1753
1754 fh = (struct fc_frame_header *) skb_transport_header(skb);
1755 fh = fc_frame_header_get(fp);
1756 if (fh->fh_r_ctl == FC_RCTL_DD_SOL_DATA && fh->fh_type == FC_TYPE_FCP)
1757 return 0;
1758
Bhanu Prakash Gollapudi8597ae82011-01-28 16:05:37 -08001759 fcoe = ((struct fcoe_port *)lport_priv(lport))->priv;
Robert Love619fe4b2012-05-22 19:06:10 -07001760 ctlr = fcoe_to_ctlr(fcoe);
1761 if (is_fip_mode(ctlr) && fc_frame_payload_op(fp) == ELS_LOGO &&
Vasu Dev52ee8322011-01-28 16:03:52 -08001762 ntoh24(fh->fh_s_id) == FC_FID_FLOGI) {
1763 FCOE_DBG("fcoe: dropping FCoE lport LOGO in fip mode\n");
1764 return -EINVAL;
1765 }
1766
Dan Carpenterf2f96d22011-02-25 15:03:23 -08001767 if (!(fr_flags(fp) & FCPHF_CRC_UNCHECKED) ||
Vasu Dev52ee8322011-01-28 16:03:52 -08001768 le32_to_cpu(fr_crc(fp)) == ~crc32(~0, skb->data, skb->len)) {
1769 fr_flags(fp) &= ~FCPHF_CRC_UNCHECKED;
1770 return 0;
1771 }
1772
Vasu Dev1bd49b42012-05-25 10:26:43 -07001773 stats = per_cpu_ptr(lport->stats, get_cpu());
Vasu Dev52ee8322011-01-28 16:03:52 -08001774 stats->InvalidCRCCount++;
1775 if (stats->InvalidCRCCount < 5)
1776 printk(KERN_WARNING "fcoe: dropping frame with CRC error\n");
Thomas Gleixner7e1e7ea2011-11-11 20:52:01 +01001777 put_cpu();
Vasu Dev52ee8322011-01-28 16:03:52 -08001778 return -EINVAL;
1779}
1780
1781/**
Chris Leech859b7b62009-11-20 14:54:47 -08001782 * fcoe_recv_frame() - process a single received frame
1783 * @skb: frame to process
1784 */
1785static void fcoe_recv_frame(struct sk_buff *skb)
1786{
1787 u32 fr_len;
1788 struct fc_lport *lport;
1789 struct fcoe_rcv_info *fr;
Vasu Dev1bd49b42012-05-25 10:26:43 -07001790 struct fc_stats *stats;
Chris Leech859b7b62009-11-20 14:54:47 -08001791 struct fcoe_crc_eof crc_eof;
1792 struct fc_frame *fp;
Chris Leech859b7b62009-11-20 14:54:47 -08001793 struct fcoe_port *port;
1794 struct fcoe_hdr *hp;
1795
1796 fr = fcoe_dev_from_skb(skb);
1797 lport = fr->fr_dev;
1798 if (unlikely(!lport)) {
1799 if (skb->destructor != fcoe_percpu_flush_done)
Bart Van Assche465b87b2013-08-14 15:42:09 +00001800 FCOE_NETDEV_DBG(skb->dev, "NULL lport in skb\n");
Chris Leech859b7b62009-11-20 14:54:47 -08001801 kfree_skb(skb);
1802 return;
1803 }
1804
Bart Van Assche465b87b2013-08-14 15:42:09 +00001805 FCOE_NETDEV_DBG(skb->dev,
1806 "skb_info: len:%d data_len:%d head:%p data:%p tail:%p end:%p sum:%d dev:%s\n",
Chris Leech859b7b62009-11-20 14:54:47 -08001807 skb->len, skb->data_len,
1808 skb->head, skb->data, skb_tail_pointer(skb),
1809 skb_end_pointer(skb), skb->csum,
1810 skb->dev ? skb->dev->name : "<NULL>");
1811
Chris Leech859b7b62009-11-20 14:54:47 -08001812 port = lport_priv(lport);
Robert Lovef1633012011-12-16 14:24:49 -08001813 skb_linearize(skb); /* check for skb_is_nonlinear is within skb_linearize */
Chris Leech859b7b62009-11-20 14:54:47 -08001814
1815 /*
1816 * Frame length checks and setting up the header pointers
1817 * was done in fcoe_rcv already.
1818 */
1819 hp = (struct fcoe_hdr *) skb_network_header(skb);
Chris Leech859b7b62009-11-20 14:54:47 -08001820
Vasu Dev1bd49b42012-05-25 10:26:43 -07001821 stats = per_cpu_ptr(lport->stats, get_cpu());
Chris Leech859b7b62009-11-20 14:54:47 -08001822 if (unlikely(FC_FCOE_DECAPS_VER(hp) != FC_FCOE_VER)) {
1823 if (stats->ErrorFrames < 5)
1824 printk(KERN_WARNING "fcoe: FCoE version "
1825 "mismatch: The frame has "
1826 "version %x, but the "
1827 "initiator supports version "
1828 "%x\n", FC_FCOE_DECAPS_VER(hp),
1829 FC_FCOE_VER);
Joe Eykholtf018b732010-03-12 16:08:55 -08001830 goto drop;
Chris Leech859b7b62009-11-20 14:54:47 -08001831 }
1832
1833 skb_pull(skb, sizeof(struct fcoe_hdr));
1834 fr_len = skb->len - sizeof(struct fcoe_crc_eof);
1835
1836 stats->RxFrames++;
1837 stats->RxWords += fr_len / FCOE_WORD_TO_BYTE;
1838
1839 fp = (struct fc_frame *)skb;
1840 fc_frame_init(fp);
1841 fr_dev(fp) = lport;
1842 fr_sof(fp) = hp->fcoe_sof;
1843
1844 /* Copy out the CRC and EOF trailer for access */
Joe Eykholtf018b732010-03-12 16:08:55 -08001845 if (skb_copy_bits(skb, fr_len, &crc_eof, sizeof(crc_eof)))
1846 goto drop;
Chris Leech859b7b62009-11-20 14:54:47 -08001847 fr_eof(fp) = crc_eof.fcoe_eof;
1848 fr_crc(fp) = crc_eof.fcoe_crc32;
Joe Eykholtf018b732010-03-12 16:08:55 -08001849 if (pskb_trim(skb, fr_len))
1850 goto drop;
Chris Leech859b7b62009-11-20 14:54:47 -08001851
Vasu Dev52ee8322011-01-28 16:03:52 -08001852 if (!fcoe_filter_frames(lport, fp)) {
1853 put_cpu();
1854 fc_exch_recv(lport, fp);
1855 return;
Chris Leech859b7b62009-11-20 14:54:47 -08001856 }
Joe Eykholtf018b732010-03-12 16:08:55 -08001857drop:
1858 stats->ErrorFrames++;
1859 put_cpu();
1860 kfree_skb(skb);
Chris Leech859b7b62009-11-20 14:54:47 -08001861}
1862
1863/**
Robert Love1875f272009-11-03 11:47:50 -08001864 * fcoe_percpu_receive_thread() - The per-CPU packet receive thread
1865 * @arg: The per-CPU context
Robert Love85b4aa42008-12-09 15:10:24 -08001866 *
1867 * Return: 0 for success
Robert Love85b4aa42008-12-09 15:10:24 -08001868 */
Bart Van Assche7c9c6842012-01-13 17:26:25 -08001869static int fcoe_percpu_receive_thread(void *arg)
Robert Love85b4aa42008-12-09 15:10:24 -08001870{
1871 struct fcoe_percpu_s *p = arg;
Robert Love85b4aa42008-12-09 15:10:24 -08001872 struct sk_buff *skb;
Neil Horman20dc3812012-03-09 14:50:24 -08001873 struct sk_buff_head tmp;
1874
1875 skb_queue_head_init(&tmp);
Robert Love85b4aa42008-12-09 15:10:24 -08001876
Dongsheng Yang8698a742014-03-11 18:09:12 +08001877 set_user_nice(current, MIN_NICE);
Robert Love85b4aa42008-12-09 15:10:24 -08001878
Neil Horman95fdd5e2012-07-06 10:40:05 -07001879retry:
Robert Love85b4aa42008-12-09 15:10:24 -08001880 while (!kthread_should_stop()) {
1881
1882 spin_lock_bh(&p->fcoe_rx_list.lock);
Neil Horman20dc3812012-03-09 14:50:24 -08001883 skb_queue_splice_init(&p->fcoe_rx_list, &tmp);
Neil Horman95fdd5e2012-07-06 10:40:05 -07001884
1885 if (!skb_queue_len(&tmp)) {
1886 set_current_state(TASK_INTERRUPTIBLE);
1887 spin_unlock_bh(&p->fcoe_rx_list.lock);
1888 schedule();
Neil Horman95fdd5e2012-07-06 10:40:05 -07001889 goto retry;
1890 }
1891
Neil Horman20dc3812012-03-09 14:50:24 -08001892 spin_unlock_bh(&p->fcoe_rx_list.lock);
1893
1894 while ((skb = __skb_dequeue(&tmp)) != NULL)
1895 fcoe_recv_frame(skb);
1896
Robert Love85b4aa42008-12-09 15:10:24 -08001897 }
1898 return 0;
1899}
1900
1901/**
Robert Love1875f272009-11-03 11:47:50 -08001902 * fcoe_dev_setup() - Setup the link change notification interface
Robert Love34f42a02009-02-27 10:55:45 -08001903 */
Randy Dunlapb0d428a2009-04-27 21:49:31 -07001904static void fcoe_dev_setup(void)
Robert Love85b4aa42008-12-09 15:10:24 -08001905{
john fastabend6f6c2aa2011-11-18 13:35:56 -08001906 register_dcbevent_notifier(&dcb_notifier);
Robert Love85b4aa42008-12-09 15:10:24 -08001907 register_netdevice_notifier(&fcoe_notifier);
1908}
1909
1910/**
Robert Love1875f272009-11-03 11:47:50 -08001911 * fcoe_dev_cleanup() - Cleanup the link change notification interface
Robert Love34f42a02009-02-27 10:55:45 -08001912 */
Robert Love85b4aa42008-12-09 15:10:24 -08001913static void fcoe_dev_cleanup(void)
1914{
john fastabend6f6c2aa2011-11-18 13:35:56 -08001915 unregister_dcbevent_notifier(&dcb_notifier);
Robert Love85b4aa42008-12-09 15:10:24 -08001916 unregister_netdevice_notifier(&fcoe_notifier);
1917}
1918
john fastabend6f6c2aa2011-11-18 13:35:56 -08001919static struct fcoe_interface *
1920fcoe_hostlist_lookup_realdev_port(struct net_device *netdev)
1921{
1922 struct fcoe_interface *fcoe;
1923 struct net_device *real_dev;
1924
1925 list_for_each_entry(fcoe, &fcoe_hostlist, list) {
1926 if (fcoe->netdev->priv_flags & IFF_802_1Q_VLAN)
1927 real_dev = vlan_dev_real_dev(fcoe->netdev);
1928 else
1929 real_dev = fcoe->netdev;
1930
1931 if (netdev == real_dev)
1932 return fcoe;
1933 }
1934 return NULL;
1935}
1936
1937static int fcoe_dcb_app_notification(struct notifier_block *notifier,
1938 ulong event, void *ptr)
1939{
1940 struct dcb_app_type *entry = ptr;
Robert Love619fe4b2012-05-22 19:06:10 -07001941 struct fcoe_ctlr *ctlr;
john fastabend6f6c2aa2011-11-18 13:35:56 -08001942 struct fcoe_interface *fcoe;
1943 struct net_device *netdev;
john fastabend6f6c2aa2011-11-18 13:35:56 -08001944 int prio;
1945
1946 if (entry->app.selector != DCB_APP_IDTYPE_ETHTYPE)
1947 return NOTIFY_OK;
1948
1949 netdev = dev_get_by_index(&init_net, entry->ifindex);
1950 if (!netdev)
1951 return NOTIFY_OK;
1952
1953 fcoe = fcoe_hostlist_lookup_realdev_port(netdev);
1954 dev_put(netdev);
1955 if (!fcoe)
1956 return NOTIFY_OK;
1957
Robert Love619fe4b2012-05-22 19:06:10 -07001958 ctlr = fcoe_to_ctlr(fcoe);
1959
john fastabend6f6c2aa2011-11-18 13:35:56 -08001960 if (entry->dcbx & DCB_CAP_DCBX_VER_CEE)
1961 prio = ffs(entry->app.priority) - 1;
1962 else
1963 prio = entry->app.priority;
1964
1965 if (prio < 0)
1966 return NOTIFY_OK;
1967
1968 if (entry->app.protocol == ETH_P_FIP ||
1969 entry->app.protocol == ETH_P_FCOE)
Robert Love619fe4b2012-05-22 19:06:10 -07001970 ctlr->priority = prio;
john fastabend6f6c2aa2011-11-18 13:35:56 -08001971
Neerav Parikh31c37a62012-09-24 11:52:45 -07001972 if (entry->app.protocol == ETH_P_FCOE)
1973 fcoe->priority = prio;
john fastabend6f6c2aa2011-11-18 13:35:56 -08001974
1975 return NOTIFY_OK;
1976}
1977
Robert Love85b4aa42008-12-09 15:10:24 -08001978/**
Robert Love1875f272009-11-03 11:47:50 -08001979 * fcoe_device_notification() - Handler for net device events
1980 * @notifier: The context of the notification
1981 * @event: The type of event
1982 * @ptr: The net device that the event was on
Robert Love85b4aa42008-12-09 15:10:24 -08001983 *
Robert Love1875f272009-11-03 11:47:50 -08001984 * This function is called by the Ethernet driver in case of link change event.
Robert Love85b4aa42008-12-09 15:10:24 -08001985 *
1986 * Returns: 0 for success
Robert Love34f42a02009-02-27 10:55:45 -08001987 */
Robert Love85b4aa42008-12-09 15:10:24 -08001988static int fcoe_device_notification(struct notifier_block *notifier,
1989 ulong event, void *ptr)
1990{
Robert Love435c8662012-11-27 06:53:35 +00001991 struct fcoe_ctlr_device *cdev;
Robert Love1875f272009-11-03 11:47:50 -08001992 struct fc_lport *lport = NULL;
Jiri Pirko351638e2013-05-28 01:30:21 +00001993 struct net_device *netdev = netdev_notifier_info_to_dev(ptr);
Robert Love619fe4b2012-05-22 19:06:10 -07001994 struct fcoe_ctlr *ctlr;
Chris Leech014f5c32009-08-25 13:59:30 -07001995 struct fcoe_interface *fcoe;
Chris Leech2e70e242009-08-25 14:00:23 -07001996 struct fcoe_port *port;
Vasu Dev1bd49b42012-05-25 10:26:43 -07001997 struct fc_stats *stats;
Joe Eykholt97c83892009-03-17 11:42:40 -07001998 u32 link_possible = 1;
Robert Love85b4aa42008-12-09 15:10:24 -08001999 u32 mfs;
2000 int rc = NOTIFY_OK;
2001
Chris Leech014f5c32009-08-25 13:59:30 -07002002 list_for_each_entry(fcoe, &fcoe_hostlist, list) {
Chris Leech25024982009-08-25 13:59:35 -07002003 if (fcoe->netdev == netdev) {
Robert Love619fe4b2012-05-22 19:06:10 -07002004 ctlr = fcoe_to_ctlr(fcoe);
2005 lport = ctlr->lp;
Robert Love85b4aa42008-12-09 15:10:24 -08002006 break;
2007 }
2008 }
Robert Love1875f272009-11-03 11:47:50 -08002009 if (!lport) {
Robert Love85b4aa42008-12-09 15:10:24 -08002010 rc = NOTIFY_DONE;
2011 goto out;
2012 }
2013
Robert Love85b4aa42008-12-09 15:10:24 -08002014 switch (event) {
2015 case NETDEV_DOWN:
2016 case NETDEV_GOING_DOWN:
Joe Eykholt97c83892009-03-17 11:42:40 -07002017 link_possible = 0;
Robert Love85b4aa42008-12-09 15:10:24 -08002018 break;
2019 case NETDEV_UP:
2020 case NETDEV_CHANGE:
Robert Love85b4aa42008-12-09 15:10:24 -08002021 break;
2022 case NETDEV_CHANGEMTU:
Yi Zou7221d7e2009-10-21 16:27:52 -07002023 if (netdev->features & NETIF_F_FCOE_MTU)
2024 break;
Vasu Dev1d1b88d2009-07-29 17:05:45 -07002025 mfs = netdev->mtu - (sizeof(struct fcoe_hdr) +
2026 sizeof(struct fcoe_crc_eof));
Robert Love85b4aa42008-12-09 15:10:24 -08002027 if (mfs >= FC_MIN_MAX_FRAME)
Robert Love1875f272009-11-03 11:47:50 -08002028 fc_set_mfs(lport, mfs);
Robert Love85b4aa42008-12-09 15:10:24 -08002029 break;
2030 case NETDEV_REGISTER:
2031 break;
Chris Leech2e70e242009-08-25 14:00:23 -07002032 case NETDEV_UNREGISTER:
2033 list_del(&fcoe->list);
Robert Love619fe4b2012-05-22 19:06:10 -07002034 port = lport_priv(ctlr->lp);
Tejun Heo2ca32b42011-01-28 16:05:32 -08002035 queue_work(fcoe_wq, &port->destroy_work);
Chris Leech2e70e242009-08-25 14:00:23 -07002036 goto out;
2037 break;
Yi Zou54a5b212010-07-20 15:21:17 -07002038 case NETDEV_FEAT_CHANGE:
2039 fcoe_netdev_features_change(lport, netdev);
2040 break;
Robert Love85b4aa42008-12-09 15:10:24 -08002041 default:
Vasu Dev1d1b88d2009-07-29 17:05:45 -07002042 FCOE_NETDEV_DBG(netdev, "Unknown event %ld "
Robert Loved5488eb2009-06-10 15:30:59 -07002043 "from netdev netlink\n", event);
Robert Love85b4aa42008-12-09 15:10:24 -08002044 }
Robert Love5e4f8fe2010-05-07 15:18:35 -07002045
2046 fcoe_link_speed_update(lport);
2047
Robert Love435c8662012-11-27 06:53:35 +00002048 cdev = fcoe_ctlr_to_ctlr_dev(ctlr);
2049
2050 if (link_possible && !fcoe_link_ok(lport)) {
2051 switch (cdev->enabled) {
2052 case FCOE_CTLR_DISABLED:
2053 pr_info("Link up while interface is disabled.\n");
2054 break;
2055 case FCOE_CTLR_ENABLED:
2056 case FCOE_CTLR_UNUSED:
2057 fcoe_ctlr_link_up(ctlr);
2058 };
2059 } else if (fcoe_ctlr_link_down(ctlr)) {
2060 switch (cdev->enabled) {
2061 case FCOE_CTLR_DISABLED:
2062 pr_info("Link down while interface is disabled.\n");
2063 break;
2064 case FCOE_CTLR_ENABLED:
2065 case FCOE_CTLR_UNUSED:
2066 stats = per_cpu_ptr(lport->stats, get_cpu());
2067 stats->LinkFailureCount++;
2068 put_cpu();
2069 fcoe_clean_pending_queue(lport);
2070 };
Robert Love85b4aa42008-12-09 15:10:24 -08002071 }
2072out:
2073 return rc;
2074}
2075
2076/**
Vasu Dev55a66d32009-12-10 09:59:31 -08002077 * fcoe_disable() - Disables a FCoE interface
Yi Zou78a58242011-01-28 16:05:16 -08002078 * @netdev : The net_device object the Ethernet interface to create on
Vasu Dev55a66d32009-12-10 09:59:31 -08002079 *
Yi Zou78a58242011-01-28 16:05:16 -08002080 * Called from fcoe transport.
Vasu Dev55a66d32009-12-10 09:59:31 -08002081 *
2082 * Returns: 0 for success
Robert Love435c8662012-11-27 06:53:35 +00002083 *
2084 * Deprecated: use fcoe_ctlr_enabled()
Vasu Dev55a66d32009-12-10 09:59:31 -08002085 */
Yi Zou78a58242011-01-28 16:05:16 -08002086static int fcoe_disable(struct net_device *netdev)
Vasu Dev55a66d32009-12-10 09:59:31 -08002087{
Robert Love619fe4b2012-05-22 19:06:10 -07002088 struct fcoe_ctlr *ctlr;
Vasu Dev55a66d32009-12-10 09:59:31 -08002089 struct fcoe_interface *fcoe;
Vasu Dev55a66d32009-12-10 09:59:31 -08002090 int rc = 0;
2091
2092 mutex_lock(&fcoe_config_mutex);
Vasu Dev55a66d32009-12-10 09:59:31 -08002093
Robert Loveee5df622011-04-01 16:05:59 -07002094 rtnl_lock();
Vasu Dev55a66d32009-12-10 09:59:31 -08002095 fcoe = fcoe_hostlist_lookup_port(netdev);
2096 rtnl_unlock();
2097
Chris Leech9ee50e42010-04-09 14:22:23 -07002098 if (fcoe) {
Robert Love619fe4b2012-05-22 19:06:10 -07002099 ctlr = fcoe_to_ctlr(fcoe);
2100 fcoe_ctlr_link_down(ctlr);
2101 fcoe_clean_pending_queue(ctlr->lp);
Chris Leech9ee50e42010-04-09 14:22:23 -07002102 } else
Vasu Dev55a66d32009-12-10 09:59:31 -08002103 rc = -ENODEV;
2104
Vasu Dev55a66d32009-12-10 09:59:31 -08002105 mutex_unlock(&fcoe_config_mutex);
2106 return rc;
2107}
2108
2109/**
2110 * fcoe_enable() - Enables a FCoE interface
Yi Zou78a58242011-01-28 16:05:16 -08002111 * @netdev : The net_device object the Ethernet interface to create on
Vasu Dev55a66d32009-12-10 09:59:31 -08002112 *
Yi Zou78a58242011-01-28 16:05:16 -08002113 * Called from fcoe transport.
Vasu Dev55a66d32009-12-10 09:59:31 -08002114 *
2115 * Returns: 0 for success
2116 */
Yi Zou78a58242011-01-28 16:05:16 -08002117static int fcoe_enable(struct net_device *netdev)
Vasu Dev55a66d32009-12-10 09:59:31 -08002118{
Robert Love619fe4b2012-05-22 19:06:10 -07002119 struct fcoe_ctlr *ctlr;
Vasu Dev55a66d32009-12-10 09:59:31 -08002120 struct fcoe_interface *fcoe;
Vasu Dev55a66d32009-12-10 09:59:31 -08002121 int rc = 0;
2122
2123 mutex_lock(&fcoe_config_mutex);
Robert Loveee5df622011-04-01 16:05:59 -07002124 rtnl_lock();
Vasu Dev55a66d32009-12-10 09:59:31 -08002125 fcoe = fcoe_hostlist_lookup_port(netdev);
2126 rtnl_unlock();
2127
Robert Love619fe4b2012-05-22 19:06:10 -07002128 if (!fcoe) {
Vasu Dev55a66d32009-12-10 09:59:31 -08002129 rc = -ENODEV;
Robert Love619fe4b2012-05-22 19:06:10 -07002130 goto out;
2131 }
Vasu Dev55a66d32009-12-10 09:59:31 -08002132
Robert Love619fe4b2012-05-22 19:06:10 -07002133 ctlr = fcoe_to_ctlr(fcoe);
2134
2135 if (!fcoe_link_ok(ctlr->lp))
2136 fcoe_ctlr_link_up(ctlr);
2137
2138out:
Vasu Dev55a66d32009-12-10 09:59:31 -08002139 mutex_unlock(&fcoe_config_mutex);
2140 return rc;
2141}
2142
2143/**
Robert Love435c8662012-11-27 06:53:35 +00002144 * fcoe_ctlr_enabled() - Enable or disable an FCoE Controller
2145 * @cdev: The FCoE Controller that is being enabled or disabled
2146 *
2147 * fcoe_sysfs will ensure that the state of 'enabled' has
2148 * changed, so no checking is necessary here. This routine simply
2149 * calls fcoe_enable or fcoe_disable, both of which are deprecated.
2150 * When those routines are removed the functionality can be merged
2151 * here.
2152 */
2153static int fcoe_ctlr_enabled(struct fcoe_ctlr_device *cdev)
2154{
2155 struct fcoe_ctlr *ctlr = fcoe_ctlr_device_priv(cdev);
2156 struct fc_lport *lport = ctlr->lp;
2157 struct net_device *netdev = fcoe_netdev(lport);
2158
2159 switch (cdev->enabled) {
2160 case FCOE_CTLR_ENABLED:
2161 return fcoe_enable(netdev);
2162 case FCOE_CTLR_DISABLED:
2163 return fcoe_disable(netdev);
2164 case FCOE_CTLR_UNUSED:
2165 default:
2166 return -ENOTSUPP;
2167 };
2168}
2169
2170/**
Robert Love1875f272009-11-03 11:47:50 -08002171 * fcoe_destroy() - Destroy a FCoE interface
Yi Zou78a58242011-01-28 16:05:16 -08002172 * @netdev : The net_device object the Ethernet interface to create on
Robert Love1875f272009-11-03 11:47:50 -08002173 *
Yi Zou78a58242011-01-28 16:05:16 -08002174 * Called from fcoe transport
Robert Love85b4aa42008-12-09 15:10:24 -08002175 *
2176 * Returns: 0 for success
Robert Love34f42a02009-02-27 10:55:45 -08002177 */
Yi Zou78a58242011-01-28 16:05:16 -08002178static int fcoe_destroy(struct net_device *netdev)
Robert Love85b4aa42008-12-09 15:10:24 -08002179{
Robert Love619fe4b2012-05-22 19:06:10 -07002180 struct fcoe_ctlr *ctlr;
Chris Leech030f4e02009-08-25 14:00:02 -07002181 struct fcoe_interface *fcoe;
Vasu Devf04ca1b2011-04-01 16:06:45 -07002182 struct fc_lport *lport;
Neerav Parikhb2085a42011-06-20 16:59:51 -07002183 struct fcoe_port *port;
Mike Christie8eca3552009-10-21 16:27:44 -07002184 int rc = 0;
Robert Love85b4aa42008-12-09 15:10:24 -08002185
Chris Leechdfc1d0f2009-08-25 14:00:13 -07002186 mutex_lock(&fcoe_config_mutex);
Robert Loveee5df622011-04-01 16:05:59 -07002187 rtnl_lock();
Chris Leech2e70e242009-08-25 14:00:23 -07002188 fcoe = fcoe_hostlist_lookup_port(netdev);
2189 if (!fcoe) {
Robert Love85b4aa42008-12-09 15:10:24 -08002190 rc = -ENODEV;
Yi Zou78a58242011-01-28 16:05:16 -08002191 goto out_nodev;
Robert Love85b4aa42008-12-09 15:10:24 -08002192 }
Robert Love619fe4b2012-05-22 19:06:10 -07002193 ctlr = fcoe_to_ctlr(fcoe);
2194 lport = ctlr->lp;
Neerav Parikhb2085a42011-06-20 16:59:51 -07002195 port = lport_priv(lport);
Yi Zou54a5b212010-07-20 15:21:17 -07002196 list_del(&fcoe->list);
Neerav Parikhb2085a42011-06-20 16:59:51 -07002197 queue_work(fcoe_wq, &port->destroy_work);
Robert Love85b4aa42008-12-09 15:10:24 -08002198out_nodev:
Neerav Parikhb2085a42011-06-20 16:59:51 -07002199 rtnl_unlock();
Chris Leechdfc1d0f2009-08-25 14:00:13 -07002200 mutex_unlock(&fcoe_config_mutex);
Robert Love85b4aa42008-12-09 15:10:24 -08002201 return rc;
2202}
2203
Robert Love1875f272009-11-03 11:47:50 -08002204/**
2205 * fcoe_destroy_work() - Destroy a FCoE port in a deferred work context
2206 * @work: Handle to the FCoE port to be destroyed
2207 */
Chris Leech2e70e242009-08-25 14:00:23 -07002208static void fcoe_destroy_work(struct work_struct *work)
2209{
Robert Lovef9c4358e2013-03-25 11:00:27 -07002210 struct fcoe_ctlr_device *cdev;
2211 struct fcoe_ctlr *ctlr;
Chris Leech2e70e242009-08-25 14:00:23 -07002212 struct fcoe_port *port;
Neerav Parikhb2085a42011-06-20 16:59:51 -07002213 struct fcoe_interface *fcoe;
Neerav Parikh94aa7432013-01-15 15:42:38 -08002214 struct Scsi_Host *shost;
2215 struct fc_host_attrs *fc_host;
2216 unsigned long flags;
2217 struct fc_vport *vport;
2218 struct fc_vport *next_vport;
Chris Leech2e70e242009-08-25 14:00:23 -07002219
2220 port = container_of(work, struct fcoe_port, destroy_work);
Neerav Parikh94aa7432013-01-15 15:42:38 -08002221 shost = port->lport->host;
2222 fc_host = shost_to_fc_host(shost);
2223
2224 /* Loop through all the vports and mark them for deletion */
2225 spin_lock_irqsave(shost->host_lock, flags);
2226 list_for_each_entry_safe(vport, next_vport, &fc_host->vports, peers) {
2227 if (vport->flags & (FC_VPORT_DEL | FC_VPORT_CREATING)) {
2228 continue;
2229 } else {
2230 vport->flags |= FC_VPORT_DELETING;
2231 queue_work(fc_host_work_q(shost),
2232 &vport->vport_delete_work);
2233 }
2234 }
2235 spin_unlock_irqrestore(shost->host_lock, flags);
2236
2237 flush_workqueue(fc_host_work_q(shost));
2238
Chris Leech2e70e242009-08-25 14:00:23 -07002239 mutex_lock(&fcoe_config_mutex);
Neerav Parikhb2085a42011-06-20 16:59:51 -07002240
Neerav Parikhb2085a42011-06-20 16:59:51 -07002241 fcoe = port->priv;
Robert Lovef9c4358e2013-03-25 11:00:27 -07002242 ctlr = fcoe_to_ctlr(fcoe);
2243 cdev = fcoe_ctlr_to_ctlr_dev(ctlr);
2244
Chris Leech2e70e242009-08-25 14:00:23 -07002245 fcoe_if_destroy(port->lport);
Robert Loveccefd232012-02-10 17:18:41 -08002246 fcoe_interface_cleanup(fcoe);
Neerav Parikhb2085a42011-06-20 16:59:51 -07002247
Chris Leech2e70e242009-08-25 14:00:23 -07002248 mutex_unlock(&fcoe_config_mutex);
Robert Lovef9c4358e2013-03-25 11:00:27 -07002249
2250 fcoe_ctlr_device_delete(cdev);
Chris Leech2e70e242009-08-25 14:00:23 -07002251}
2252
Robert Love85b4aa42008-12-09 15:10:24 -08002253/**
Yi Zou78a58242011-01-28 16:05:16 -08002254 * fcoe_match() - Check if the FCoE is supported on the given netdevice
2255 * @netdev : The net_device object the Ethernet interface to create on
Robert Love1875f272009-11-03 11:47:50 -08002256 *
Yi Zou78a58242011-01-28 16:05:16 -08002257 * Called from fcoe transport.
2258 *
2259 * Returns: always returns true as this is the default FCoE transport,
2260 * i.e., support all netdevs.
2261 */
2262static bool fcoe_match(struct net_device *netdev)
2263{
2264 return true;
2265}
2266
2267/**
john fastabend6f6c2aa2011-11-18 13:35:56 -08002268 * fcoe_dcb_create() - Initialize DCB attributes and hooks
2269 * @netdev: The net_device object of the L2 link that should be queried
2270 * @port: The fcoe_port to bind FCoE APP priority with
2271 * @
2272 */
2273static void fcoe_dcb_create(struct fcoe_interface *fcoe)
2274{
2275#ifdef CONFIG_DCB
2276 int dcbx;
2277 u8 fup, up;
2278 struct net_device *netdev = fcoe->realdev;
Robert Love619fe4b2012-05-22 19:06:10 -07002279 struct fcoe_ctlr *ctlr = fcoe_to_ctlr(fcoe);
john fastabend6f6c2aa2011-11-18 13:35:56 -08002280 struct dcb_app app = {
2281 .priority = 0,
2282 .protocol = ETH_P_FCOE
2283 };
2284
2285 /* setup DCB priority attributes. */
2286 if (netdev && netdev->dcbnl_ops && netdev->dcbnl_ops->getdcbx) {
2287 dcbx = netdev->dcbnl_ops->getdcbx(netdev);
2288
2289 if (dcbx & DCB_CAP_DCBX_VER_IEEE) {
2290 app.selector = IEEE_8021QAZ_APP_SEL_ETHERTYPE;
2291 up = dcb_ieee_getapp_mask(netdev, &app);
2292 app.protocol = ETH_P_FIP;
2293 fup = dcb_ieee_getapp_mask(netdev, &app);
2294 } else {
2295 app.selector = DCB_APP_IDTYPE_ETHTYPE;
2296 up = dcb_getapp(netdev, &app);
2297 app.protocol = ETH_P_FIP;
2298 fup = dcb_getapp(netdev, &app);
2299 }
2300
Neerav Parikh31c37a62012-09-24 11:52:45 -07002301 fcoe->priority = ffs(up) ? ffs(up) - 1 : 0;
2302 ctlr->priority = ffs(fup) ? ffs(fup) - 1 : fcoe->priority;
john fastabend6f6c2aa2011-11-18 13:35:56 -08002303 }
2304#endif
2305}
2306
Robert Love435c8662012-11-27 06:53:35 +00002307enum fcoe_create_link_state {
2308 FCOE_CREATE_LINK_DOWN,
2309 FCOE_CREATE_LINK_UP,
2310};
2311
john fastabend6f6c2aa2011-11-18 13:35:56 -08002312/**
Robert Love435c8662012-11-27 06:53:35 +00002313 * _fcoe_create() - (internal) Create a fcoe interface
2314 * @netdev : The net_device object the Ethernet interface to create on
2315 * @fip_mode: The FIP mode for this creation
2316 * @link_state: The ctlr link state on creation
Yi Zou78a58242011-01-28 16:05:16 -08002317 *
Robert Love435c8662012-11-27 06:53:35 +00002318 * Called from either the libfcoe 'create' module parameter
2319 * via fcoe_create or from fcoe_syfs's ctlr_create file.
Robert Love85b4aa42008-12-09 15:10:24 -08002320 *
Robert Love435c8662012-11-27 06:53:35 +00002321 * libfcoe's 'create' module parameter is deprecated so some
2322 * consolidation of code can be done when that interface is
2323 * removed.
Robert Love34f42a02009-02-27 10:55:45 -08002324 */
Robert Love435c8662012-11-27 06:53:35 +00002325static int _fcoe_create(struct net_device *netdev, enum fip_state fip_mode,
2326 enum fcoe_create_link_state link_state)
Robert Love85b4aa42008-12-09 15:10:24 -08002327{
Neerav Parikhb2085a42011-06-20 16:59:51 -07002328 int rc = 0;
Robert Love8d55e502012-05-22 19:06:26 -07002329 struct fcoe_ctlr_device *ctlr_dev;
Robert Love619fe4b2012-05-22 19:06:10 -07002330 struct fcoe_ctlr *ctlr;
Chris Leech030f4e02009-08-25 14:00:02 -07002331 struct fcoe_interface *fcoe;
Chris Leechaf7f85d2009-08-25 13:59:24 -07002332 struct fc_lport *lport;
Robert Love85b4aa42008-12-09 15:10:24 -08002333
Chris Leechdfc1d0f2009-08-25 14:00:13 -07002334 mutex_lock(&fcoe_config_mutex);
Robert Loveee5df622011-04-01 16:05:59 -07002335 rtnl_lock();
Vasu Dev34ce27b2010-05-07 15:18:46 -07002336
Robert Love85b4aa42008-12-09 15:10:24 -08002337 /* look for existing lport */
2338 if (fcoe_hostlist_lookup(netdev)) {
2339 rc = -EEXIST;
Yi Zou78a58242011-01-28 16:05:16 -08002340 goto out_nodev;
Robert Love85b4aa42008-12-09 15:10:24 -08002341 }
Robert Love85b4aa42008-12-09 15:10:24 -08002342
Joe Eykholt1dd454d2010-07-20 15:20:46 -07002343 fcoe = fcoe_interface_create(netdev, fip_mode);
Robert Love7287fb92011-01-28 16:03:47 -08002344 if (IS_ERR(fcoe)) {
2345 rc = PTR_ERR(fcoe);
Yi Zou78a58242011-01-28 16:05:16 -08002346 goto out_nodev;
Chris Leech030f4e02009-08-25 14:00:02 -07002347 }
2348
Robert Love619fe4b2012-05-22 19:06:10 -07002349 ctlr = fcoe_to_ctlr(fcoe);
Robert Love8d55e502012-05-22 19:06:26 -07002350 ctlr_dev = fcoe_ctlr_to_ctlr_dev(ctlr);
2351 lport = fcoe_if_create(fcoe, &ctlr_dev->dev, 0);
Chris Leechaf7f85d2009-08-25 13:59:24 -07002352 if (IS_ERR(lport)) {
Robert Loved5488eb2009-06-10 15:30:59 -07002353 printk(KERN_ERR "fcoe: Failed to create interface (%s)\n",
Robert Love85b4aa42008-12-09 15:10:24 -08002354 netdev->name);
Robert Love85b4aa42008-12-09 15:10:24 -08002355 rc = -EIO;
Robert Love848e7d52011-08-25 12:40:47 -07002356 rtnl_unlock();
Chris Leech2e70e242009-08-25 14:00:23 -07002357 fcoe_interface_cleanup(fcoe);
Robert Lovef9c4358e2013-03-25 11:00:27 -07002358 mutex_unlock(&fcoe_config_mutex);
2359 fcoe_ctlr_device_delete(ctlr_dev);
2360 goto out;
Robert Love85b4aa42008-12-09 15:10:24 -08002361 }
Chris Leech030f4e02009-08-25 14:00:02 -07002362
Chris Leech54b649f2009-08-25 14:00:07 -07002363 /* Make this the "master" N_Port */
Robert Love619fe4b2012-05-22 19:06:10 -07002364 ctlr->lp = lport;
Chris Leech030f4e02009-08-25 14:00:02 -07002365
john fastabend6f6c2aa2011-11-18 13:35:56 -08002366 /* setup DCB priority attributes. */
2367 fcoe_dcb_create(fcoe);
2368
Chris Leech54b649f2009-08-25 14:00:07 -07002369 /* start FIP Discovery and FLOGI */
2370 lport->boot_time = jiffies;
2371 fc_fabric_login(lport);
Robert Love435c8662012-11-27 06:53:35 +00002372
2373 /*
2374 * If the fcoe_ctlr_device is to be set to DISABLED
2375 * it must be done after the lport is added to the
2376 * hostlist, but before the rtnl_lock is released.
2377 * This is because the rtnl_lock protects the
2378 * hostlist that fcoe_device_notification uses. If
2379 * the FCoE Controller is intended to be created
2380 * DISABLED then 'enabled' needs to be considered
2381 * handling link events. 'enabled' must be set
2382 * before the lport can be found in the hostlist
2383 * when a link up event is received.
2384 */
2385 if (link_state == FCOE_CREATE_LINK_UP)
2386 ctlr_dev->enabled = FCOE_CTLR_ENABLED;
2387 else
2388 ctlr_dev->enabled = FCOE_CTLR_DISABLED;
2389
2390 if (link_state == FCOE_CREATE_LINK_UP &&
2391 !fcoe_link_ok(lport)) {
Robert Love22805122012-03-13 18:22:12 -07002392 rtnl_unlock();
Robert Love619fe4b2012-05-22 19:06:10 -07002393 fcoe_ctlr_link_up(ctlr);
Robert Love22805122012-03-13 18:22:12 -07002394 mutex_unlock(&fcoe_config_mutex);
2395 return rc;
2396 }
Chris Leech54b649f2009-08-25 14:00:07 -07002397
Robert Love85b4aa42008-12-09 15:10:24 -08002398out_nodev:
Vasu Dev34ce27b2010-05-07 15:18:46 -07002399 rtnl_unlock();
Chris Leechdfc1d0f2009-08-25 14:00:13 -07002400 mutex_unlock(&fcoe_config_mutex);
Robert Lovef9c4358e2013-03-25 11:00:27 -07002401out:
Robert Love85b4aa42008-12-09 15:10:24 -08002402 return rc;
2403}
2404
Robert Love34f42a02009-02-27 10:55:45 -08002405/**
Robert Love435c8662012-11-27 06:53:35 +00002406 * fcoe_create() - Create a fcoe interface
2407 * @netdev : The net_device object the Ethernet interface to create on
2408 * @fip_mode: The FIP mode for this creation
2409 *
2410 * Called from fcoe transport
2411 *
2412 * Returns: 0 for success
2413 */
2414static int fcoe_create(struct net_device *netdev, enum fip_state fip_mode)
2415{
2416 return _fcoe_create(netdev, fip_mode, FCOE_CREATE_LINK_UP);
2417}
2418
2419/**
2420 * fcoe_ctlr_alloc() - Allocate a fcoe interface from fcoe_sysfs
2421 * @netdev: The net_device to be used by the allocated FCoE Controller
2422 *
2423 * This routine is called from fcoe_sysfs. It will start the fcoe_ctlr
2424 * in a link_down state. The allows the user an opportunity to configure
2425 * the FCoE Controller from sysfs before enabling the FCoE Controller.
2426 *
2427 * Creating in with this routine starts the FCoE Controller in Fabric
2428 * mode. The user can change to VN2VN or another mode before enabling.
2429 */
2430static int fcoe_ctlr_alloc(struct net_device *netdev)
2431{
2432 return _fcoe_create(netdev, FIP_MODE_FABRIC,
2433 FCOE_CREATE_LINK_DOWN);
2434}
2435
2436/**
Robert Love5e4f8fe2010-05-07 15:18:35 -07002437 * fcoe_link_ok() - Check if the link is OK for a local port
2438 * @lport: The local port to check link on
2439 *
2440 * Returns: 0 if link is UP and OK, -1 if not
2441 *
2442 */
Bart Van Assche7c9c6842012-01-13 17:26:25 -08002443static int fcoe_link_ok(struct fc_lport *lport)
Robert Love5e4f8fe2010-05-07 15:18:35 -07002444{
Bhanu Prakash Gollapudi8597ae82011-01-28 16:05:37 -08002445 struct net_device *netdev = fcoe_netdev(lport);
Robert Love5e4f8fe2010-05-07 15:18:35 -07002446
2447 if (netif_oper_up(netdev))
2448 return 0;
2449 return -1;
2450}
2451
2452/**
Robert Love1875f272009-11-03 11:47:50 -08002453 * fcoe_percpu_clean() - Clear all pending skbs for an local port
2454 * @lport: The local port whose skbs are to be cleared
Joe Eykholte7a51992009-08-25 14:04:08 -07002455 *
2456 * Must be called with fcoe_create_mutex held to single-thread completion.
2457 *
2458 * This flushes the pending skbs by adding a new skb to each queue and
2459 * waiting until they are all freed. This assures us that not only are
2460 * there no packets that will be handled by the lport, but also that any
2461 * threads already handling packet have returned.
Robert Love85b4aa42008-12-09 15:10:24 -08002462 */
Bart Van Assche7c9c6842012-01-13 17:26:25 -08002463static void fcoe_percpu_clean(struct fc_lport *lport)
Robert Love85b4aa42008-12-09 15:10:24 -08002464{
Robert Love85b4aa42008-12-09 15:10:24 -08002465 struct fcoe_percpu_s *pp;
Neil Hormandd060e72012-03-09 14:50:19 -08002466 struct sk_buff *skb;
Robert Love5e5e92d2009-03-17 11:41:35 -07002467 unsigned int cpu;
Robert Love85b4aa42008-12-09 15:10:24 -08002468
Robert Love5e5e92d2009-03-17 11:41:35 -07002469 for_each_possible_cpu(cpu) {
2470 pp = &per_cpu(fcoe_percpu, cpu);
Joe Eykholte7a51992009-08-25 14:04:08 -07002471
Neil Hormandd060e72012-03-09 14:50:19 -08002472 if (!pp->thread || !cpu_online(cpu))
Joe Eykholte7a51992009-08-25 14:04:08 -07002473 continue;
Joe Eykholte7a51992009-08-25 14:04:08 -07002474
2475 skb = dev_alloc_skb(0);
Dan Carpenterb3b8abd2012-04-20 12:16:54 -07002476 if (!skb)
Joe Eykholte7a51992009-08-25 14:04:08 -07002477 continue;
Dan Carpenterb3b8abd2012-04-20 12:16:54 -07002478
Joe Eykholte7a51992009-08-25 14:04:08 -07002479 skb->destructor = fcoe_percpu_flush_done;
2480
Neil Hormandd060e72012-03-09 14:50:19 -08002481 spin_lock_bh(&pp->fcoe_rx_list.lock);
Joe Eykholte7a51992009-08-25 14:04:08 -07002482 __skb_queue_tail(&pp->fcoe_rx_list, skb);
2483 if (pp->fcoe_rx_list.qlen == 1)
2484 wake_up_process(pp->thread);
Robert Love5e5e92d2009-03-17 11:41:35 -07002485 spin_unlock_bh(&pp->fcoe_rx_list.lock);
Joe Eykholte7a51992009-08-25 14:04:08 -07002486
2487 wait_for_completion(&fcoe_flush_completion);
Robert Love85b4aa42008-12-09 15:10:24 -08002488 }
2489}
Robert Love85b4aa42008-12-09 15:10:24 -08002490
2491/**
Robert Love1875f272009-11-03 11:47:50 -08002492 * fcoe_reset() - Reset a local port
2493 * @shost: The SCSI host associated with the local port to be reset
Robert Love85b4aa42008-12-09 15:10:24 -08002494 *
Robert Love1875f272009-11-03 11:47:50 -08002495 * Returns: Always 0 (return value required by FC transport template)
Robert Love85b4aa42008-12-09 15:10:24 -08002496 */
Bart Van Assche7c9c6842012-01-13 17:26:25 -08002497static int fcoe_reset(struct Scsi_Host *shost)
Robert Love85b4aa42008-12-09 15:10:24 -08002498{
2499 struct fc_lport *lport = shost_priv(shost);
Vasu Devd2f80952011-02-25 15:03:28 -08002500 struct fcoe_port *port = lport_priv(lport);
2501 struct fcoe_interface *fcoe = port->priv;
Robert Love619fe4b2012-05-22 19:06:10 -07002502 struct fcoe_ctlr *ctlr = fcoe_to_ctlr(fcoe);
Robert Love435c8662012-11-27 06:53:35 +00002503 struct fcoe_ctlr_device *cdev = fcoe_ctlr_to_ctlr_dev(ctlr);
Vasu Devd2f80952011-02-25 15:03:28 -08002504
Robert Love619fe4b2012-05-22 19:06:10 -07002505 fcoe_ctlr_link_down(ctlr);
2506 fcoe_clean_pending_queue(ctlr->lp);
Robert Love435c8662012-11-27 06:53:35 +00002507
2508 if (cdev->enabled != FCOE_CTLR_DISABLED &&
2509 !fcoe_link_ok(ctlr->lp))
Robert Love619fe4b2012-05-22 19:06:10 -07002510 fcoe_ctlr_link_up(ctlr);
Robert Love85b4aa42008-12-09 15:10:24 -08002511 return 0;
2512}
Robert Love85b4aa42008-12-09 15:10:24 -08002513
Robert Love34f42a02009-02-27 10:55:45 -08002514/**
Robert Love1875f272009-11-03 11:47:50 -08002515 * fcoe_hostlist_lookup_port() - Find the FCoE interface associated with a net device
2516 * @netdev: The net device used as a key
Robert Love85b4aa42008-12-09 15:10:24 -08002517 *
Robert Love1875f272009-11-03 11:47:50 -08002518 * Locking: Must be called with the RNL mutex held.
2519 *
2520 * Returns: NULL or the FCoE interface
Robert Love85b4aa42008-12-09 15:10:24 -08002521 */
Chris Leech014f5c32009-08-25 13:59:30 -07002522static struct fcoe_interface *
Robert Love1875f272009-11-03 11:47:50 -08002523fcoe_hostlist_lookup_port(const struct net_device *netdev)
Robert Love85b4aa42008-12-09 15:10:24 -08002524{
Chris Leech014f5c32009-08-25 13:59:30 -07002525 struct fcoe_interface *fcoe;
Robert Love85b4aa42008-12-09 15:10:24 -08002526
Chris Leech014f5c32009-08-25 13:59:30 -07002527 list_for_each_entry(fcoe, &fcoe_hostlist, list) {
Robert Love1875f272009-11-03 11:47:50 -08002528 if (fcoe->netdev == netdev)
Chris Leech014f5c32009-08-25 13:59:30 -07002529 return fcoe;
Robert Love85b4aa42008-12-09 15:10:24 -08002530 }
Robert Love85b4aa42008-12-09 15:10:24 -08002531 return NULL;
2532}
2533
Robert Love34f42a02009-02-27 10:55:45 -08002534/**
Robert Love1875f272009-11-03 11:47:50 -08002535 * fcoe_hostlist_lookup() - Find the local port associated with a
2536 * given net device
2537 * @netdev: The netdevice used as a key
Robert Love85b4aa42008-12-09 15:10:24 -08002538 *
Robert Love1875f272009-11-03 11:47:50 -08002539 * Locking: Must be called with the RTNL mutex held
2540 *
2541 * Returns: NULL or the local port
Robert Love85b4aa42008-12-09 15:10:24 -08002542 */
Chris Leech090eb6c2009-08-25 14:00:28 -07002543static struct fc_lport *fcoe_hostlist_lookup(const struct net_device *netdev)
Robert Love85b4aa42008-12-09 15:10:24 -08002544{
Robert Love619fe4b2012-05-22 19:06:10 -07002545 struct fcoe_ctlr *ctlr;
Chris Leech014f5c32009-08-25 13:59:30 -07002546 struct fcoe_interface *fcoe;
Robert Love85b4aa42008-12-09 15:10:24 -08002547
Chris Leech014f5c32009-08-25 13:59:30 -07002548 fcoe = fcoe_hostlist_lookup_port(netdev);
Robert Love619fe4b2012-05-22 19:06:10 -07002549 ctlr = fcoe_to_ctlr(fcoe);
2550 return (fcoe) ? ctlr->lp : NULL;
Robert Love85b4aa42008-12-09 15:10:24 -08002551}
Robert Love85b4aa42008-12-09 15:10:24 -08002552
Robert Love34f42a02009-02-27 10:55:45 -08002553/**
Robert Love1875f272009-11-03 11:47:50 -08002554 * fcoe_hostlist_add() - Add the FCoE interface identified by a local
2555 * port to the hostlist
2556 * @lport: The local port that identifies the FCoE interface to be added
2557 *
2558 * Locking: must be called with the RTNL mutex held
Robert Love85b4aa42008-12-09 15:10:24 -08002559 *
2560 * Returns: 0 for success
2561 */
Chris Leech090eb6c2009-08-25 14:00:28 -07002562static int fcoe_hostlist_add(const struct fc_lport *lport)
Robert Love85b4aa42008-12-09 15:10:24 -08002563{
Chris Leech014f5c32009-08-25 13:59:30 -07002564 struct fcoe_interface *fcoe;
2565 struct fcoe_port *port;
Robert Love85b4aa42008-12-09 15:10:24 -08002566
Chris Leech014f5c32009-08-25 13:59:30 -07002567 fcoe = fcoe_hostlist_lookup_port(fcoe_netdev(lport));
2568 if (!fcoe) {
2569 port = lport_priv(lport);
Bhanu Prakash Gollapudi8597ae82011-01-28 16:05:37 -08002570 fcoe = port->priv;
Chris Leech014f5c32009-08-25 13:59:30 -07002571 list_add_tail(&fcoe->list, &fcoe_hostlist);
Robert Love85b4aa42008-12-09 15:10:24 -08002572 }
2573 return 0;
2574}
Robert Love85b4aa42008-12-09 15:10:24 -08002575
Neil Hormanf9184df2013-01-15 14:34:40 -05002576/**
2577 * fcoe_hostlist_del() - Remove the FCoE interface identified by a local
2578 * port to the hostlist
2579 * @lport: The local port that identifies the FCoE interface to be added
2580 *
2581 * Locking: must be called with the RTNL mutex held
2582 *
2583 */
2584static void fcoe_hostlist_del(const struct fc_lport *lport)
2585{
2586 struct fcoe_interface *fcoe;
2587 struct fcoe_port *port;
2588
2589 port = lport_priv(lport);
2590 fcoe = port->priv;
2591 list_del(&fcoe->list);
2592 return;
2593}
Yi Zou78a58242011-01-28 16:05:16 -08002594
2595static struct fcoe_transport fcoe_sw_transport = {
2596 .name = {FCOE_TRANSPORT_DEFAULT},
2597 .attached = false,
2598 .list = LIST_HEAD_INIT(fcoe_sw_transport.list),
2599 .match = fcoe_match,
Robert Love435c8662012-11-27 06:53:35 +00002600 .alloc = fcoe_ctlr_alloc,
Yi Zou78a58242011-01-28 16:05:16 -08002601 .create = fcoe_create,
2602 .destroy = fcoe_destroy,
2603 .enable = fcoe_enable,
2604 .disable = fcoe_disable,
2605};
2606
Robert Love34f42a02009-02-27 10:55:45 -08002607/**
Robert Love1875f272009-11-03 11:47:50 -08002608 * fcoe_init() - Initialize fcoe.ko
Robert Love85b4aa42008-12-09 15:10:24 -08002609 *
Robert Love1875f272009-11-03 11:47:50 -08002610 * Returns: 0 on success, or a negative value on failure
Robert Love34f42a02009-02-27 10:55:45 -08002611 */
Robert Love85b4aa42008-12-09 15:10:24 -08002612static int __init fcoe_init(void)
2613{
Robert Love1875f272009-11-03 11:47:50 -08002614 struct fcoe_percpu_s *p;
Robert Love38eccab2009-03-17 11:41:30 -07002615 unsigned int cpu;
Robert Love8976f422009-03-17 11:41:46 -07002616 int rc = 0;
Robert Love85b4aa42008-12-09 15:10:24 -08002617
Tejun Heo2ca32b42011-01-28 16:05:32 -08002618 fcoe_wq = alloc_workqueue("fcoe", 0, 0);
2619 if (!fcoe_wq)
2620 return -ENOMEM;
2621
Yi Zou78a58242011-01-28 16:05:16 -08002622 /* register as a fcoe transport */
2623 rc = fcoe_transport_attach(&fcoe_sw_transport);
2624 if (rc) {
2625 printk(KERN_ERR "failed to register an fcoe transport, check "
2626 "if libfcoe is loaded\n");
2627 return rc;
2628 }
2629
Chris Leechdfc1d0f2009-08-25 14:00:13 -07002630 mutex_lock(&fcoe_config_mutex);
2631
Robert Love38eccab2009-03-17 11:41:30 -07002632 for_each_possible_cpu(cpu) {
Robert Love5e5e92d2009-03-17 11:41:35 -07002633 p = &per_cpu(fcoe_percpu, cpu);
Robert Love38eccab2009-03-17 11:41:30 -07002634 skb_queue_head_init(&p->fcoe_rx_list);
2635 }
2636
Srivatsa S. Bhatcd45ae32014-03-11 02:09:52 +05302637 cpu_notifier_register_begin();
2638
Robert Love8976f422009-03-17 11:41:46 -07002639 for_each_online_cpu(cpu)
2640 fcoe_percpu_thread_create(cpu);
Robert Love85b4aa42008-12-09 15:10:24 -08002641
Robert Love8976f422009-03-17 11:41:46 -07002642 /* Initialize per CPU interrupt thread */
Srivatsa S. Bhatcd45ae32014-03-11 02:09:52 +05302643 rc = __register_hotcpu_notifier(&fcoe_cpu_notifier);
Robert Love8976f422009-03-17 11:41:46 -07002644 if (rc)
2645 goto out_free;
Robert Love85b4aa42008-12-09 15:10:24 -08002646
Srivatsa S. Bhatcd45ae32014-03-11 02:09:52 +05302647 cpu_notifier_register_done();
2648
Robert Love8976f422009-03-17 11:41:46 -07002649 /* Setup link change notification */
Robert Love85b4aa42008-12-09 15:10:24 -08002650 fcoe_dev_setup();
2651
Chris Leech5892c322009-08-25 13:59:14 -07002652 rc = fcoe_if_init();
2653 if (rc)
2654 goto out_free;
Robert Love85b4aa42008-12-09 15:10:24 -08002655
Chris Leechdfc1d0f2009-08-25 14:00:13 -07002656 mutex_unlock(&fcoe_config_mutex);
Robert Love85b4aa42008-12-09 15:10:24 -08002657 return 0;
Robert Love8976f422009-03-17 11:41:46 -07002658
2659out_free:
2660 for_each_online_cpu(cpu) {
2661 fcoe_percpu_thread_destroy(cpu);
2662 }
Srivatsa S. Bhatcd45ae32014-03-11 02:09:52 +05302663
2664 cpu_notifier_register_done();
2665
Chris Leechdfc1d0f2009-08-25 14:00:13 -07002666 mutex_unlock(&fcoe_config_mutex);
Tejun Heo2ca32b42011-01-28 16:05:32 -08002667 destroy_workqueue(fcoe_wq);
Robert Love8976f422009-03-17 11:41:46 -07002668 return rc;
Robert Love85b4aa42008-12-09 15:10:24 -08002669}
2670module_init(fcoe_init);
2671
2672/**
Robert Love1875f272009-11-03 11:47:50 -08002673 * fcoe_exit() - Clean up fcoe.ko
Robert Love85b4aa42008-12-09 15:10:24 -08002674 *
Robert Love1875f272009-11-03 11:47:50 -08002675 * Returns: 0 on success or a negative value on failure
Robert Love34f42a02009-02-27 10:55:45 -08002676 */
Robert Love85b4aa42008-12-09 15:10:24 -08002677static void __exit fcoe_exit(void)
2678{
Chris Leech014f5c32009-08-25 13:59:30 -07002679 struct fcoe_interface *fcoe, *tmp;
Robert Love619fe4b2012-05-22 19:06:10 -07002680 struct fcoe_ctlr *ctlr;
Chris Leech2e70e242009-08-25 14:00:23 -07002681 struct fcoe_port *port;
Robert Love1875f272009-11-03 11:47:50 -08002682 unsigned int cpu;
Robert Love85b4aa42008-12-09 15:10:24 -08002683
Chris Leechdfc1d0f2009-08-25 14:00:13 -07002684 mutex_lock(&fcoe_config_mutex);
2685
Robert Love85b4aa42008-12-09 15:10:24 -08002686 fcoe_dev_cleanup();
2687
Vasu Dev5919a592009-03-27 09:03:29 -07002688 /* releases the associated fcoe hosts */
Chris Leech090eb6c2009-08-25 14:00:28 -07002689 rtnl_lock();
2690 list_for_each_entry_safe(fcoe, tmp, &fcoe_hostlist, list) {
Robert Love619fe4b2012-05-22 19:06:10 -07002691 ctlr = fcoe_to_ctlr(fcoe);
2692 port = lport_priv(ctlr->lp);
Neil Hormanf9184df2013-01-15 14:34:40 -05002693 fcoe_hostlist_del(port->lport);
Tejun Heo2ca32b42011-01-28 16:05:32 -08002694 queue_work(fcoe_wq, &port->destroy_work);
Chris Leechc863df32009-08-25 14:00:18 -07002695 }
Chris Leech090eb6c2009-08-25 14:00:28 -07002696 rtnl_unlock();
Robert Love85b4aa42008-12-09 15:10:24 -08002697
Srivatsa S. Bhatcd45ae32014-03-11 02:09:52 +05302698 cpu_notifier_register_begin();
Robert Love8976f422009-03-17 11:41:46 -07002699
Chris Leech014f5c32009-08-25 13:59:30 -07002700 for_each_online_cpu(cpu)
Robert Love8976f422009-03-17 11:41:46 -07002701 fcoe_percpu_thread_destroy(cpu);
Robert Love85b4aa42008-12-09 15:10:24 -08002702
Srivatsa S. Bhatcd45ae32014-03-11 02:09:52 +05302703 __unregister_hotcpu_notifier(&fcoe_cpu_notifier);
2704
2705 cpu_notifier_register_done();
2706
Chris Leechdfc1d0f2009-08-25 14:00:13 -07002707 mutex_unlock(&fcoe_config_mutex);
Chris Leech2e70e242009-08-25 14:00:23 -07002708
Tejun Heo2ca32b42011-01-28 16:05:32 -08002709 /*
2710 * destroy_work's may be chained but destroy_workqueue()
2711 * can take care of them. Just kill the fcoe_wq.
2712 */
2713 destroy_workqueue(fcoe_wq);
Chris Leech2e70e242009-08-25 14:00:23 -07002714
Tejun Heo2ca32b42011-01-28 16:05:32 -08002715 /*
2716 * Detaching from the scsi transport must happen after all
2717 * destroys are done on the fcoe_wq. destroy_workqueue will
2718 * enusre the fcoe_wq is flushed.
2719 */
Chris Leech2e70e242009-08-25 14:00:23 -07002720 fcoe_if_exit();
Yi Zou78a58242011-01-28 16:05:16 -08002721
2722 /* detach from fcoe transport */
2723 fcoe_transport_detach(&fcoe_sw_transport);
Robert Love85b4aa42008-12-09 15:10:24 -08002724}
2725module_exit(fcoe_exit);
Chris Leech11b56182009-11-03 11:46:29 -08002726
2727/**
2728 * fcoe_flogi_resp() - FCoE specific FLOGI and FDISC response handler
2729 * @seq: active sequence in the FLOGI or FDISC exchange
2730 * @fp: response frame, or error encoded in a pointer (timeout)
2731 * @arg: pointer the the fcoe_ctlr structure
2732 *
Uwe Kleine-König65155b32010-06-11 12:17:01 +02002733 * This handles MAC address management for FCoE, then passes control on to
Chris Leech11b56182009-11-03 11:46:29 -08002734 * the libfc FLOGI response handler.
2735 */
2736static void fcoe_flogi_resp(struct fc_seq *seq, struct fc_frame *fp, void *arg)
2737{
2738 struct fcoe_ctlr *fip = arg;
2739 struct fc_exch *exch = fc_seq_exch(seq);
2740 struct fc_lport *lport = exch->lp;
2741 u8 *mac;
2742
2743 if (IS_ERR(fp))
2744 goto done;
2745
2746 mac = fr_cb(fp)->granted_mac;
Vasu Dev907c07d2011-10-28 11:34:23 -07002747 /* pre-FIP */
2748 if (is_zero_ether_addr(mac))
2749 fcoe_ctlr_recv_flogi(fip, lport, fp);
2750 if (!is_zero_ether_addr(mac))
2751 fcoe_update_src_mac(lport, mac);
Chris Leech11b56182009-11-03 11:46:29 -08002752done:
2753 fc_lport_flogi_resp(seq, fp, lport);
2754}
2755
2756/**
2757 * fcoe_logo_resp() - FCoE specific LOGO response handler
2758 * @seq: active sequence in the LOGO exchange
2759 * @fp: response frame, or error encoded in a pointer (timeout)
2760 * @arg: pointer the the fcoe_ctlr structure
2761 *
Uwe Kleine-König65155b32010-06-11 12:17:01 +02002762 * This handles MAC address management for FCoE, then passes control on to
Chris Leech11b56182009-11-03 11:46:29 -08002763 * the libfc LOGO response handler.
2764 */
2765static void fcoe_logo_resp(struct fc_seq *seq, struct fc_frame *fp, void *arg)
2766{
Joe Eykholt386309ce2009-11-03 11:49:16 -08002767 struct fc_lport *lport = arg;
Chris Leech11b56182009-11-03 11:46:29 -08002768 static u8 zero_mac[ETH_ALEN] = { 0 };
2769
2770 if (!IS_ERR(fp))
Joe Eykholt386309ce2009-11-03 11:49:16 -08002771 fcoe_update_src_mac(lport, zero_mac);
Chris Leech11b56182009-11-03 11:46:29 -08002772 fc_lport_logo_resp(seq, fp, lport);
2773}
2774
2775/**
2776 * fcoe_elsct_send - FCoE specific ELS handler
2777 *
2778 * This does special case handling of FIP encapsualted ELS exchanges for FCoE,
2779 * using FCoE specific response handlers and passing the FIP controller as
2780 * the argument (the lport is still available from the exchange).
2781 *
2782 * Most of the work here is just handed off to the libfc routine.
2783 */
Robert Love1875f272009-11-03 11:47:50 -08002784static struct fc_seq *fcoe_elsct_send(struct fc_lport *lport, u32 did,
2785 struct fc_frame *fp, unsigned int op,
2786 void (*resp)(struct fc_seq *,
2787 struct fc_frame *,
2788 void *),
2789 void *arg, u32 timeout)
Chris Leech11b56182009-11-03 11:46:29 -08002790{
2791 struct fcoe_port *port = lport_priv(lport);
Bhanu Prakash Gollapudi8597ae82011-01-28 16:05:37 -08002792 struct fcoe_interface *fcoe = port->priv;
Robert Love619fe4b2012-05-22 19:06:10 -07002793 struct fcoe_ctlr *fip = fcoe_to_ctlr(fcoe);
Chris Leech11b56182009-11-03 11:46:29 -08002794 struct fc_frame_header *fh = fc_frame_header_get(fp);
2795
2796 switch (op) {
2797 case ELS_FLOGI:
2798 case ELS_FDISC:
Joe Eykholte10f8c62010-07-20 15:20:30 -07002799 if (lport->point_to_multipoint)
2800 break;
Chris Leech11b56182009-11-03 11:46:29 -08002801 return fc_elsct_send(lport, did, fp, op, fcoe_flogi_resp,
2802 fip, timeout);
2803 case ELS_LOGO:
2804 /* only hook onto fabric logouts, not port logouts */
2805 if (ntoh24(fh->fh_d_id) != FC_FID_FLOGI)
2806 break;
2807 return fc_elsct_send(lport, did, fp, op, fcoe_logo_resp,
Joe Eykholt386309ce2009-11-03 11:49:16 -08002808 lport, timeout);
Chris Leech11b56182009-11-03 11:46:29 -08002809 }
2810 return fc_elsct_send(lport, did, fp, op, resp, arg, timeout);
2811}
2812
Chris Leech9a057532009-11-03 11:46:40 -08002813/**
2814 * fcoe_vport_create() - create an fc_host/scsi_host for a vport
2815 * @vport: fc_vport object to create a new fc_host for
2816 * @disabled: start the new fc_host in a disabled state by default?
2817 *
2818 * Returns: 0 for success
2819 */
2820static int fcoe_vport_create(struct fc_vport *vport, bool disabled)
2821{
2822 struct Scsi_Host *shost = vport_to_shost(vport);
2823 struct fc_lport *n_port = shost_priv(shost);
2824 struct fcoe_port *port = lport_priv(n_port);
Bhanu Prakash Gollapudi8597ae82011-01-28 16:05:37 -08002825 struct fcoe_interface *fcoe = port->priv;
Chris Leech9a057532009-11-03 11:46:40 -08002826 struct net_device *netdev = fcoe->netdev;
2827 struct fc_lport *vn_port;
Neerav Parikhbdf25212011-05-16 16:45:29 -07002828 int rc;
2829 char buf[32];
2830
2831 rc = fcoe_validate_vport_create(vport);
2832 if (rc) {
Bhanu Prakash Gollapudid8348952011-08-04 17:38:49 -07002833 fcoe_wwn_to_str(vport->port_name, buf, sizeof(buf));
Neerav Parikhbdf25212011-05-16 16:45:29 -07002834 printk(KERN_ERR "fcoe: Failed to create vport, "
2835 "WWPN (0x%s) already exists\n",
2836 buf);
2837 return rc;
2838 }
Chris Leech9a057532009-11-03 11:46:40 -08002839
2840 mutex_lock(&fcoe_config_mutex);
Jiri Pirko4bc71cb2011-09-03 03:34:30 +00002841 rtnl_lock();
Chris Leech9a057532009-11-03 11:46:40 -08002842 vn_port = fcoe_if_create(fcoe, &vport->dev, 1);
Jiri Pirko4bc71cb2011-09-03 03:34:30 +00002843 rtnl_unlock();
Chris Leech9a057532009-11-03 11:46:40 -08002844 mutex_unlock(&fcoe_config_mutex);
2845
2846 if (IS_ERR(vn_port)) {
2847 printk(KERN_ERR "fcoe: fcoe_vport_create(%s) failed\n",
2848 netdev->name);
2849 return -EIO;
2850 }
2851
2852 if (disabled) {
2853 fc_vport_set_state(vport, FC_VPORT_DISABLED);
2854 } else {
2855 vn_port->boot_time = jiffies;
2856 fc_fabric_login(vn_port);
2857 fc_vport_setlink(vn_port);
2858 }
2859 return 0;
2860}
2861
2862/**
2863 * fcoe_vport_destroy() - destroy the fc_host/scsi_host for a vport
2864 * @vport: fc_vport object that is being destroyed
2865 *
2866 * Returns: 0 for success
2867 */
2868static int fcoe_vport_destroy(struct fc_vport *vport)
2869{
2870 struct Scsi_Host *shost = vport_to_shost(vport);
2871 struct fc_lport *n_port = shost_priv(shost);
2872 struct fc_lport *vn_port = vport->dd_data;
Chris Leech9a057532009-11-03 11:46:40 -08002873
2874 mutex_lock(&n_port->lp_mutex);
2875 list_del(&vn_port->list);
2876 mutex_unlock(&n_port->lp_mutex);
Robert Loveccefd232012-02-10 17:18:41 -08002877
2878 mutex_lock(&fcoe_config_mutex);
2879 fcoe_if_destroy(vn_port);
2880 mutex_unlock(&fcoe_config_mutex);
2881
Chris Leech9a057532009-11-03 11:46:40 -08002882 return 0;
2883}
2884
2885/**
2886 * fcoe_vport_disable() - change vport state
2887 * @vport: vport to bring online/offline
2888 * @disable: should the vport be disabled?
2889 */
2890static int fcoe_vport_disable(struct fc_vport *vport, bool disable)
2891{
2892 struct fc_lport *lport = vport->dd_data;
2893
2894 if (disable) {
2895 fc_vport_set_state(vport, FC_VPORT_DISABLED);
2896 fc_fabric_logoff(lport);
2897 } else {
2898 lport->boot_time = jiffies;
2899 fc_fabric_login(lport);
2900 fc_vport_setlink(lport);
2901 }
2902
2903 return 0;
2904}
2905
Chris Leechdc8596d2009-11-03 11:47:18 -08002906/**
2907 * fcoe_vport_set_symbolic_name() - append vport string to symbolic name
2908 * @vport: fc_vport with a new symbolic name string
2909 *
2910 * After generating a new symbolic name string, a new RSPN_ID request is
2911 * sent to the name server. There is no response handler, so if it fails
2912 * for some reason it will not be retried.
2913 */
2914static void fcoe_set_vport_symbolic_name(struct fc_vport *vport)
2915{
2916 struct fc_lport *lport = vport->dd_data;
2917 struct fc_frame *fp;
2918 size_t len;
2919
2920 snprintf(fc_host_symbolic_name(lport->host), FC_SYMBOLIC_NAME_SIZE,
2921 "%s v%s over %s : %s", FCOE_NAME, FCOE_VERSION,
2922 fcoe_netdev(lport)->name, vport->symbolic_name);
2923
2924 if (lport->state != LPORT_ST_READY)
2925 return;
2926
2927 len = strnlen(fc_host_symbolic_name(lport->host), 255);
2928 fp = fc_frame_alloc(lport,
2929 sizeof(struct fc_ct_hdr) +
2930 sizeof(struct fc_ns_rspn) + len);
2931 if (!fp)
2932 return;
2933 lport->tt.elsct_send(lport, FC_FID_DIR_SERV, fp, FC_NS_RSPN_ID,
Joe Eykholtb94f8952009-11-03 11:50:21 -08002934 NULL, NULL, 3 * lport->r_a_tov);
Chris Leechdc8596d2009-11-03 11:47:18 -08002935}
Yi Zoub84056b2009-11-20 14:55:19 -08002936
Robert Love8d55e502012-05-22 19:06:26 -07002937static void fcoe_fcf_get_vlan_id(struct fcoe_fcf_device *fcf_dev)
2938{
2939 struct fcoe_ctlr_device *ctlr_dev =
2940 fcoe_fcf_dev_to_ctlr_dev(fcf_dev);
2941 struct fcoe_ctlr *ctlr = fcoe_ctlr_device_priv(ctlr_dev);
2942 struct fcoe_interface *fcoe = fcoe_ctlr_priv(ctlr);
2943
2944 fcf_dev->vlan_id = vlan_dev_vlan_id(fcoe->netdev);
2945}
2946
Joe Eykholt7d65b0d2010-03-12 16:08:02 -08002947/**
2948 * fcoe_set_port_id() - Callback from libfc when Port_ID is set.
2949 * @lport: the local port
2950 * @port_id: the port ID
2951 * @fp: the received frame, if any, that caused the port_id to be set.
2952 *
2953 * This routine handles the case where we received a FLOGI and are
2954 * entering point-to-point mode. We need to call fcoe_ctlr_recv_flogi()
2955 * so it can set the non-mapped mode and gateway address.
2956 *
2957 * The FLOGI LS_ACC is handled by fcoe_flogi_resp().
2958 */
2959static void fcoe_set_port_id(struct fc_lport *lport,
2960 u32 port_id, struct fc_frame *fp)
2961{
2962 struct fcoe_port *port = lport_priv(lport);
Bhanu Prakash Gollapudi8597ae82011-01-28 16:05:37 -08002963 struct fcoe_interface *fcoe = port->priv;
Robert Love619fe4b2012-05-22 19:06:10 -07002964 struct fcoe_ctlr *ctlr = fcoe_to_ctlr(fcoe);
Joe Eykholt7d65b0d2010-03-12 16:08:02 -08002965
2966 if (fp && fc_frame_payload_op(fp) == ELS_FLOGI)
Robert Love619fe4b2012-05-22 19:06:10 -07002967 fcoe_ctlr_recv_flogi(ctlr, lport, fp);
Joe Eykholt7d65b0d2010-03-12 16:08:02 -08002968}