blob: 21927f7952d8c17c7e7313d46f10c87ab7625b00 [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 *);
Robert Love5e4f8fe2010-05-07 15:18:35 -070085static int fcoe_link_speed_update(struct fc_lport *);
Robert Love1875f272009-11-03 11:47:50 -080086static int fcoe_link_ok(struct fc_lport *);
Vasu Devfdd78022009-03-17 11:42:24 -070087
88static struct fc_lport *fcoe_hostlist_lookup(const struct net_device *);
89static int fcoe_hostlist_add(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
Yi Zoub84056b2009-11-20 14:55:19 -0800134static void fcoe_get_lesb(struct fc_lport *, struct fc_els_lesb *);
135
Robert Love1875f272009-11-03 11:47:50 -0800136/* notification function for packets from net device */
Robert Love85b4aa42008-12-09 15:10:24 -0800137static struct notifier_block fcoe_notifier = {
138 .notifier_call = fcoe_device_notification,
139};
140
Robert Love1875f272009-11-03 11:47:50 -0800141/* notification function for CPU hotplug events */
142static struct notifier_block fcoe_cpu_notifier = {
143 .notifier_call = fcoe_cpu_callback,
144};
145
john fastabend6f6c2aa2011-11-18 13:35:56 -0800146/* notification function for DCB events */
147static struct notifier_block dcb_notifier = {
148 .notifier_call = fcoe_dcb_app_notification,
149};
150
Yi Zou8ca86f82011-01-28 16:05:11 -0800151static struct scsi_transport_template *fcoe_nport_scsi_transport;
152static struct scsi_transport_template *fcoe_vport_scsi_transport;
Vasu Dev7f349142009-03-27 09:06:31 -0700153
Robert Love1875f272009-11-03 11:47:50 -0800154static int fcoe_vport_destroy(struct fc_vport *);
155static int fcoe_vport_create(struct fc_vport *, bool disabled);
156static int fcoe_vport_disable(struct fc_vport *, bool disable);
157static void fcoe_set_vport_symbolic_name(struct fc_vport *);
Joe Eykholt7d65b0d2010-03-12 16:08:02 -0800158static void fcoe_set_port_id(struct fc_lport *, u32, struct fc_frame *);
Robert Love8d55e502012-05-22 19:06:26 -0700159static void fcoe_ctlr_get_lesb(struct fcoe_ctlr_device *);
160static void fcoe_fcf_get_vlan_id(struct fcoe_fcf_device *);
161
162static struct fcoe_sysfs_function_template fcoe_sysfs_templ = {
Robert Love435c8662012-11-27 06:53:35 +0000163 .set_fcoe_ctlr_mode = fcoe_ctlr_set_fip_mode,
164 .set_fcoe_ctlr_enabled = fcoe_ctlr_enabled,
Robert Love8d55e502012-05-22 19:06:26 -0700165 .get_fcoe_ctlr_link_fail = fcoe_ctlr_get_lesb,
166 .get_fcoe_ctlr_vlink_fail = fcoe_ctlr_get_lesb,
167 .get_fcoe_ctlr_miss_fka = fcoe_ctlr_get_lesb,
168 .get_fcoe_ctlr_symb_err = fcoe_ctlr_get_lesb,
169 .get_fcoe_ctlr_err_block = fcoe_ctlr_get_lesb,
170 .get_fcoe_ctlr_fcs_error = fcoe_ctlr_get_lesb,
171
172 .get_fcoe_fcf_selected = fcoe_fcf_get_selected,
173 .get_fcoe_fcf_vlan_id = fcoe_fcf_get_vlan_id,
174};
Robert Love1875f272009-11-03 11:47:50 -0800175
176static struct libfc_function_template fcoe_libfc_fcn_templ = {
177 .frame_send = fcoe_xmit,
178 .ddp_setup = fcoe_ddp_setup,
179 .ddp_done = fcoe_ddp_done,
Yi Zou71f89492011-06-20 16:59:10 -0700180 .ddp_target = fcoe_ddp_target,
Robert Love1875f272009-11-03 11:47:50 -0800181 .elsct_send = fcoe_elsct_send,
Yi Zoub84056b2009-11-20 14:55:19 -0800182 .get_lesb = fcoe_get_lesb,
Joe Eykholt7d65b0d2010-03-12 16:08:02 -0800183 .lport_set_port_id = fcoe_set_port_id,
Robert Love1875f272009-11-03 11:47:50 -0800184};
Chris Leech9a057532009-11-03 11:46:40 -0800185
Bart Van Assche7c9c6842012-01-13 17:26:25 -0800186static struct fc_function_template fcoe_nport_fc_functions = {
Vasu Dev7f349142009-03-27 09:06:31 -0700187 .show_host_node_name = 1,
188 .show_host_port_name = 1,
189 .show_host_supported_classes = 1,
190 .show_host_supported_fc4s = 1,
191 .show_host_active_fc4s = 1,
192 .show_host_maxframe_size = 1,
Neerav Parikh9f71af22012-01-22 17:30:16 -0800193 .show_host_serial_number = 1,
194 .show_host_manufacturer = 1,
195 .show_host_model = 1,
196 .show_host_model_description = 1,
197 .show_host_hardware_version = 1,
198 .show_host_driver_version = 1,
199 .show_host_firmware_version = 1,
200 .show_host_optionrom_version = 1,
Vasu Dev7f349142009-03-27 09:06:31 -0700201
202 .show_host_port_id = 1,
203 .show_host_supported_speeds = 1,
204 .get_host_speed = fc_get_host_speed,
205 .show_host_speed = 1,
206 .show_host_port_type = 1,
207 .get_host_port_state = fc_get_host_port_state,
208 .show_host_port_state = 1,
209 .show_host_symbolic_name = 1,
210
211 .dd_fcrport_size = sizeof(struct fc_rport_libfc_priv),
212 .show_rport_maxframe_size = 1,
213 .show_rport_supported_classes = 1,
214
215 .show_host_fabric_name = 1,
216 .show_starget_node_name = 1,
217 .show_starget_port_name = 1,
218 .show_starget_port_id = 1,
219 .set_rport_dev_loss_tmo = fc_set_rport_loss_tmo,
220 .show_rport_dev_loss_tmo = 1,
221 .get_fc_host_stats = fc_get_host_stats,
222 .issue_fc_host_lip = fcoe_reset,
223
224 .terminate_rport_io = fc_rport_terminate_io,
Chris Leech9a057532009-11-03 11:46:40 -0800225
226 .vport_create = fcoe_vport_create,
227 .vport_delete = fcoe_vport_destroy,
228 .vport_disable = fcoe_vport_disable,
Chris Leechdc8596d2009-11-03 11:47:18 -0800229 .set_vport_symbolic_name = fcoe_set_vport_symbolic_name,
Steve Maa51ab392009-11-03 11:47:34 -0800230
231 .bsg_request = fc_lport_bsg_request,
Vasu Dev7f349142009-03-27 09:06:31 -0700232};
233
Bart Van Assche7c9c6842012-01-13 17:26:25 -0800234static struct fc_function_template fcoe_vport_fc_functions = {
Chris Leeche9084bb2009-11-03 11:46:34 -0800235 .show_host_node_name = 1,
236 .show_host_port_name = 1,
237 .show_host_supported_classes = 1,
238 .show_host_supported_fc4s = 1,
239 .show_host_active_fc4s = 1,
240 .show_host_maxframe_size = 1,
Neerav Parikh7e5adcf2012-02-10 17:18:31 -0800241 .show_host_serial_number = 1,
242 .show_host_manufacturer = 1,
243 .show_host_model = 1,
244 .show_host_model_description = 1,
245 .show_host_hardware_version = 1,
246 .show_host_driver_version = 1,
247 .show_host_firmware_version = 1,
248 .show_host_optionrom_version = 1,
Chris Leeche9084bb2009-11-03 11:46:34 -0800249
250 .show_host_port_id = 1,
251 .show_host_supported_speeds = 1,
252 .get_host_speed = fc_get_host_speed,
253 .show_host_speed = 1,
254 .show_host_port_type = 1,
255 .get_host_port_state = fc_get_host_port_state,
256 .show_host_port_state = 1,
257 .show_host_symbolic_name = 1,
258
259 .dd_fcrport_size = sizeof(struct fc_rport_libfc_priv),
260 .show_rport_maxframe_size = 1,
261 .show_rport_supported_classes = 1,
262
263 .show_host_fabric_name = 1,
264 .show_starget_node_name = 1,
265 .show_starget_port_name = 1,
266 .show_starget_port_id = 1,
267 .set_rport_dev_loss_tmo = fc_set_rport_loss_tmo,
268 .show_rport_dev_loss_tmo = 1,
269 .get_fc_host_stats = fc_get_host_stats,
270 .issue_fc_host_lip = fcoe_reset,
271
272 .terminate_rport_io = fc_rport_terminate_io,
Steve Maa51ab392009-11-03 11:47:34 -0800273
274 .bsg_request = fc_lport_bsg_request,
Chris Leeche9084bb2009-11-03 11:46:34 -0800275};
276
Vasu Dev7f349142009-03-27 09:06:31 -0700277static struct scsi_host_template fcoe_shost_template = {
278 .module = THIS_MODULE,
279 .name = "FCoE Driver",
280 .proc_name = FCOE_NAME,
281 .queuecommand = fc_queuecommand,
282 .eh_abort_handler = fc_eh_abort,
283 .eh_device_reset_handler = fc_eh_device_reset,
284 .eh_host_reset_handler = fc_eh_host_reset,
285 .slave_alloc = fc_slave_alloc,
286 .change_queue_depth = fc_change_queue_depth,
287 .change_queue_type = fc_change_queue_type,
288 .this_id = -1,
Vasu Dev14caf442009-10-15 17:46:55 -0700289 .cmd_per_lun = 3,
Vasu Dev7f349142009-03-27 09:06:31 -0700290 .can_queue = FCOE_MAX_OUTSTANDING_COMMANDS,
291 .use_clustering = ENABLE_CLUSTERING,
292 .sg_tablesize = SG_ALL,
293 .max_sectors = 0xffff,
294};
295
Chris Leech54b649f2009-08-25 14:00:07 -0700296/**
Robert Love1875f272009-11-03 11:47:50 -0800297 * fcoe_interface_setup() - Setup a FCoE interface
298 * @fcoe: The new FCoE interface
299 * @netdev: The net device that the fcoe interface is on
Chris Leech54b649f2009-08-25 14:00:07 -0700300 *
301 * Returns : 0 for success
Chris Leech2e70e242009-08-25 14:00:23 -0700302 * Locking: must be called with the RTNL mutex held
Chris Leech54b649f2009-08-25 14:00:07 -0700303 */
304static int fcoe_interface_setup(struct fcoe_interface *fcoe,
305 struct net_device *netdev)
306{
Robert Love619fe4b2012-05-22 19:06:10 -0700307 struct fcoe_ctlr *fip = fcoe_to_ctlr(fcoe);
Chris Leech54b649f2009-08-25 14:00:07 -0700308 struct netdev_hw_addr *ha;
Yi Zou5bab87e2009-11-03 11:49:43 -0800309 struct net_device *real_dev;
Chris Leech54b649f2009-08-25 14:00:07 -0700310 u8 flogi_maddr[ETH_ALEN];
Yi Zoub7a727f2009-10-21 16:28:03 -0700311 const struct net_device_ops *ops;
Chris Leech54b649f2009-08-25 14:00:07 -0700312
313 fcoe->netdev = netdev;
314
Yi Zoub7a727f2009-10-21 16:28:03 -0700315 /* Let LLD initialize for FCoE */
316 ops = netdev->netdev_ops;
317 if (ops->ndo_fcoe_enable) {
318 if (ops->ndo_fcoe_enable(netdev))
319 FCOE_NETDEV_DBG(netdev, "Failed to enable FCoE"
320 " specific feature for LLD.\n");
321 }
322
Chris Leech54b649f2009-08-25 14:00:07 -0700323 /* Do not support for bonding device */
Jiri Pirkocc8bdf02011-03-01 20:05:35 +0000324 if (netdev->priv_flags & IFF_BONDING && netdev->flags & IFF_MASTER) {
john fastabend59d92512009-11-03 11:48:44 -0800325 FCOE_NETDEV_DBG(netdev, "Bonded interfaces not supported\n");
Chris Leech54b649f2009-08-25 14:00:07 -0700326 return -EOPNOTSUPP;
327 }
328
329 /* look for SAN MAC address, if multiple SAN MACs exist, only
330 * use the first one for SPMA */
Yi Zou5bab87e2009-11-03 11:49:43 -0800331 real_dev = (netdev->priv_flags & IFF_802_1Q_VLAN) ?
332 vlan_dev_real_dev(netdev) : netdev;
Vasu Devd1483bb2011-09-27 21:38:13 -0700333 fcoe->realdev = real_dev;
Chris Leech54b649f2009-08-25 14:00:07 -0700334 rcu_read_lock();
Yi Zou5bab87e2009-11-03 11:49:43 -0800335 for_each_dev_addr(real_dev, ha) {
Chris Leech54b649f2009-08-25 14:00:07 -0700336 if ((ha->type == NETDEV_HW_ADDR_T_SAN) &&
Yi Zoubf361702009-11-03 11:49:38 -0800337 (is_valid_ether_addr(ha->addr))) {
Chris Leech54b649f2009-08-25 14:00:07 -0700338 memcpy(fip->ctl_src_addr, ha->addr, ETH_ALEN);
339 fip->spma = 1;
340 break;
341 }
342 }
343 rcu_read_unlock();
344
345 /* setup Source Mac Address */
346 if (!fip->spma)
347 memcpy(fip->ctl_src_addr, netdev->dev_addr, netdev->addr_len);
348
349 /*
350 * Add FCoE MAC address as second unicast MAC address
351 * or enter promiscuous mode if not capable of listening
352 * for multiple unicast MACs.
353 */
Chris Leech54b649f2009-08-25 14:00:07 -0700354 memcpy(flogi_maddr, (u8[6]) FC_FCOE_FLOGI_MAC, ETH_ALEN);
Jiri Pirkoa748ee22010-04-01 21:22:09 +0000355 dev_uc_add(netdev, flogi_maddr);
Chris Leech54b649f2009-08-25 14:00:07 -0700356 if (fip->spma)
Jiri Pirkoa748ee22010-04-01 21:22:09 +0000357 dev_uc_add(netdev, fip->ctl_src_addr);
Joe Eykholte10f8c62010-07-20 15:20:30 -0700358 if (fip->mode == FIP_MODE_VN2VN) {
359 dev_mc_add(netdev, FIP_ALL_VN2VN_MACS);
360 dev_mc_add(netdev, FIP_ALL_P2P_MACS);
361 } else
362 dev_mc_add(netdev, FIP_ALL_ENODE_MACS);
Chris Leech54b649f2009-08-25 14:00:07 -0700363
364 /*
365 * setup the receive function from ethernet driver
366 * on the ethertype for the given device
367 */
368 fcoe->fcoe_packet_type.func = fcoe_rcv;
369 fcoe->fcoe_packet_type.type = __constant_htons(ETH_P_FCOE);
370 fcoe->fcoe_packet_type.dev = netdev;
371 dev_add_pack(&fcoe->fcoe_packet_type);
372
373 fcoe->fip_packet_type.func = fcoe_fip_recv;
374 fcoe->fip_packet_type.type = htons(ETH_P_FIP);
375 fcoe->fip_packet_type.dev = netdev;
376 dev_add_pack(&fcoe->fip_packet_type);
377
378 return 0;
379}
380
Vasu Dev7f349142009-03-27 09:06:31 -0700381/**
Robert Love1875f272009-11-03 11:47:50 -0800382 * fcoe_interface_create() - Create a FCoE interface on a net device
383 * @netdev: The net device to create the FCoE interface on
Joe Eykholt1dd454d2010-07-20 15:20:46 -0700384 * @fip_mode: The mode to use for FIP
Chris Leech030f4e02009-08-25 14:00:02 -0700385 *
386 * Returns: pointer to a struct fcoe_interface or NULL on error
387 */
Joe Eykholt1dd454d2010-07-20 15:20:46 -0700388static struct fcoe_interface *fcoe_interface_create(struct net_device *netdev,
389 enum fip_state fip_mode)
Chris Leech030f4e02009-08-25 14:00:02 -0700390{
Robert Love8d55e502012-05-22 19:06:26 -0700391 struct fcoe_ctlr_device *ctlr_dev;
Robert Love619fe4b2012-05-22 19:06:10 -0700392 struct fcoe_ctlr *ctlr;
Chris Leech030f4e02009-08-25 14:00:02 -0700393 struct fcoe_interface *fcoe;
Robert Love619fe4b2012-05-22 19:06:10 -0700394 int size;
john fastabend59d92512009-11-03 11:48:44 -0800395 int err;
Chris Leech030f4e02009-08-25 14:00:02 -0700396
Robert Love7287fb92011-01-28 16:03:47 -0800397 if (!try_module_get(THIS_MODULE)) {
398 FCOE_NETDEV_DBG(netdev,
399 "Could not get a reference to the module\n");
400 fcoe = ERR_PTR(-EBUSY);
401 goto out;
402 }
403
Robert Love619fe4b2012-05-22 19:06:10 -0700404 size = sizeof(struct fcoe_ctlr) + sizeof(struct fcoe_interface);
Robert Love8d55e502012-05-22 19:06:26 -0700405 ctlr_dev = fcoe_ctlr_device_add(&netdev->dev, &fcoe_sysfs_templ,
406 size);
407 if (!ctlr_dev) {
408 FCOE_DBG("Failed to add fcoe_ctlr_device\n");
Robert Love7287fb92011-01-28 16:03:47 -0800409 fcoe = ERR_PTR(-ENOMEM);
Robert Love6f687942012-02-10 17:18:36 -0800410 goto out_putmod;
Chris Leech030f4e02009-08-25 14:00:02 -0700411 }
412
Robert Love8d55e502012-05-22 19:06:26 -0700413 ctlr = fcoe_ctlr_device_priv(ctlr_dev);
414 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);
Robert Love8d55e502012-05-22 19:06:26 -0700496 struct fcoe_ctlr_device *ctlr_dev = fcoe_ctlr_to_ctlr_dev(fip);
Vasu Dev433eba02012-04-20 12:16:27 -0700497
498 rtnl_lock();
499 if (!fcoe->removed)
500 fcoe_interface_remove(fcoe);
Robert Love848e7d52011-08-25 12:40:47 -0700501 rtnl_unlock();
502
Neerav Parikhb2085a42011-06-20 16:59:51 -0700503 /* Release the self-reference taken during fcoe_interface_create() */
Robert Love1a8ef412012-02-10 17:18:46 -0800504 /* tear-down the FCoE controller */
505 fcoe_ctlr_destroy(fip);
Robert Love619fe4b2012-05-22 19:06:10 -0700506 scsi_host_put(fip->lp->host);
Robert Love8d55e502012-05-22 19:06:26 -0700507 fcoe_ctlr_device_delete(ctlr_dev);
Robert Love1a8ef412012-02-10 17:18:46 -0800508 dev_put(netdev);
509 module_put(THIS_MODULE);
Vasu Devf04ca1b2011-04-01 16:06:45 -0700510}
511
512/**
Robert Love1875f272009-11-03 11:47:50 -0800513 * fcoe_fip_recv() - Handler for received FIP frames
514 * @skb: The receive skb
515 * @netdev: The associated net device
516 * @ptype: The packet_type structure which was used to register this handler
517 * @orig_dev: The original net_device the the skb was received on.
518 * (in case dev is a bond)
Vasu Devab6b85c2009-05-17 12:33:08 +0000519 *
520 * Returns: 0 for success
521 */
Robert Love1875f272009-11-03 11:47:50 -0800522static int fcoe_fip_recv(struct sk_buff *skb, struct net_device *netdev,
Vasu Devab6b85c2009-05-17 12:33:08 +0000523 struct packet_type *ptype,
524 struct net_device *orig_dev)
525{
Chris Leech259ad852009-08-25 13:59:41 -0700526 struct fcoe_interface *fcoe;
Robert Love619fe4b2012-05-22 19:06:10 -0700527 struct fcoe_ctlr *ctlr;
Vasu Devab6b85c2009-05-17 12:33:08 +0000528
Chris Leech259ad852009-08-25 13:59:41 -0700529 fcoe = container_of(ptype, struct fcoe_interface, fip_packet_type);
Robert Love619fe4b2012-05-22 19:06:10 -0700530 ctlr = fcoe_to_ctlr(fcoe);
531 fcoe_ctlr_recv(ctlr, skb);
Vasu Devab6b85c2009-05-17 12:33:08 +0000532 return 0;
533}
534
535/**
Vasu Dev980f5152011-07-27 15:11:05 -0700536 * fcoe_port_send() - Send an Ethernet-encapsulated FIP/FCoE frame
537 * @port: The FCoE port
538 * @skb: The FIP/FCoE packet to be sent
539 */
540static void fcoe_port_send(struct fcoe_port *port, struct sk_buff *skb)
541{
542 if (port->fcoe_pending_queue.qlen)
543 fcoe_check_wait_queue(port->lport, skb);
544 else if (fcoe_start_io(skb))
545 fcoe_check_wait_queue(port->lport, skb);
546}
547
548/**
Robert Love1875f272009-11-03 11:47:50 -0800549 * fcoe_fip_send() - Send an Ethernet-encapsulated FIP frame
550 * @fip: The FCoE controller
551 * @skb: The FIP packet to be sent
Vasu Devab6b85c2009-05-17 12:33:08 +0000552 */
553static void fcoe_fip_send(struct fcoe_ctlr *fip, struct sk_buff *skb)
554{
Chris Leech3fe9a0b2009-08-25 13:59:46 -0700555 skb->dev = fcoe_from_ctlr(fip)->netdev;
Vasu Dev980f5152011-07-27 15:11:05 -0700556 fcoe_port_send(lport_priv(fip->lp), skb);
Vasu Devab6b85c2009-05-17 12:33:08 +0000557}
558
559/**
Robert Love1875f272009-11-03 11:47:50 -0800560 * fcoe_update_src_mac() - Update the Ethernet MAC filters
561 * @lport: The local port to update the source MAC on
562 * @addr: Unicast MAC address to add
Vasu Devab6b85c2009-05-17 12:33:08 +0000563 *
564 * Remove any previously-set unicast MAC filter.
565 * Add secondary FCoE MAC address filter for our OUI.
566 */
Chris Leech11b56182009-11-03 11:46:29 -0800567static void fcoe_update_src_mac(struct fc_lport *lport, u8 *addr)
Vasu Devab6b85c2009-05-17 12:33:08 +0000568{
Chris Leech11b56182009-11-03 11:46:29 -0800569 struct fcoe_port *port = lport_priv(lport);
Bhanu Prakash Gollapudi8597ae82011-01-28 16:05:37 -0800570 struct fcoe_interface *fcoe = port->priv;
Vasu Devab6b85c2009-05-17 12:33:08 +0000571
Chris Leech11b56182009-11-03 11:46:29 -0800572 if (!is_zero_ether_addr(port->data_src_addr))
Jiri Pirkoa748ee22010-04-01 21:22:09 +0000573 dev_uc_del(fcoe->netdev, port->data_src_addr);
Chris Leech11b56182009-11-03 11:46:29 -0800574 if (!is_zero_ether_addr(addr))
Jiri Pirkoa748ee22010-04-01 21:22:09 +0000575 dev_uc_add(fcoe->netdev, addr);
Chris Leech11b56182009-11-03 11:46:29 -0800576 memcpy(port->data_src_addr, addr, ETH_ALEN);
Vasu Devab6b85c2009-05-17 12:33:08 +0000577}
578
579/**
Chris Leech11b56182009-11-03 11:46:29 -0800580 * fcoe_get_src_mac() - return the Ethernet source address for an lport
581 * @lport: libfc lport
582 */
583static u8 *fcoe_get_src_mac(struct fc_lport *lport)
584{
585 struct fcoe_port *port = lport_priv(lport);
586
587 return port->data_src_addr;
588}
589
590/**
Robert Love1875f272009-11-03 11:47:50 -0800591 * fcoe_lport_config() - Set up a local port
592 * @lport: The local port to be setup
Vasu Dev7f349142009-03-27 09:06:31 -0700593 *
594 * Returns: 0 for success
595 */
Robert Love1875f272009-11-03 11:47:50 -0800596static int fcoe_lport_config(struct fc_lport *lport)
Vasu Dev7f349142009-03-27 09:06:31 -0700597{
Robert Love1875f272009-11-03 11:47:50 -0800598 lport->link_up = 0;
599 lport->qfull = 0;
600 lport->max_retry_count = 3;
601 lport->max_rport_retry_count = 3;
602 lport->e_d_tov = 2 * 1000; /* FC-FS default */
603 lport->r_a_tov = 2 * 2 * 1000;
604 lport->service_params = (FCP_SPPF_INIT_FCN | FCP_SPPF_RD_XRDY_DIS |
605 FCP_SPPF_RETRY | FCP_SPPF_CONF_COMPL);
606 lport->does_npiv = 1;
Vasu Dev7f349142009-03-27 09:06:31 -0700607
Robert Love1875f272009-11-03 11:47:50 -0800608 fc_lport_init_stats(lport);
Vasu Dev7f349142009-03-27 09:06:31 -0700609
610 /* lport fc_lport related configuration */
Robert Love1875f272009-11-03 11:47:50 -0800611 fc_lport_config(lport);
Vasu Dev7f349142009-03-27 09:06:31 -0700612
613 /* offload related configuration */
Robert Love1875f272009-11-03 11:47:50 -0800614 lport->crc_offload = 0;
615 lport->seq_offload = 0;
616 lport->lro_enabled = 0;
617 lport->lro_xid = 0;
618 lport->lso_max = 0;
Vasu Dev7f349142009-03-27 09:06:31 -0700619
620 return 0;
621}
622
623/**
Yi Zou54a5b212010-07-20 15:21:17 -0700624 * fcoe_netdev_features_change - Updates the lport's offload flags based
625 * on the LLD netdev's FCoE feature flags
626 */
627static void fcoe_netdev_features_change(struct fc_lport *lport,
628 struct net_device *netdev)
629{
630 mutex_lock(&lport->lp_mutex);
631
632 if (netdev->features & NETIF_F_SG)
633 lport->sg_supp = 1;
634 else
635 lport->sg_supp = 0;
636
637 if (netdev->features & NETIF_F_FCOE_CRC) {
638 lport->crc_offload = 1;
639 FCOE_NETDEV_DBG(netdev, "Supports FCCRC offload\n");
640 } else {
641 lport->crc_offload = 0;
642 }
643
644 if (netdev->features & NETIF_F_FSO) {
645 lport->seq_offload = 1;
646 lport->lso_max = netdev->gso_max_size;
647 FCOE_NETDEV_DBG(netdev, "Supports LSO for max len 0x%x\n",
648 lport->lso_max);
649 } else {
650 lport->seq_offload = 0;
651 lport->lso_max = 0;
652 }
653
654 if (netdev->fcoe_ddp_xid) {
655 lport->lro_enabled = 1;
656 lport->lro_xid = netdev->fcoe_ddp_xid;
657 FCOE_NETDEV_DBG(netdev, "Supports LRO for max xid 0x%x\n",
658 lport->lro_xid);
659 } else {
660 lport->lro_enabled = 0;
661 lport->lro_xid = 0;
662 }
663
664 mutex_unlock(&lport->lp_mutex);
665}
666
667/**
Robert Love1875f272009-11-03 11:47:50 -0800668 * fcoe_netdev_config() - Set up net devive for SW FCoE
669 * @lport: The local port that is associated with the net device
670 * @netdev: The associated net device
Vasu Dev7f349142009-03-27 09:06:31 -0700671 *
Robert Love1875f272009-11-03 11:47:50 -0800672 * Must be called after fcoe_lport_config() as it will use local port mutex
Vasu Dev7f349142009-03-27 09:06:31 -0700673 *
Robert Love1875f272009-11-03 11:47:50 -0800674 * Returns: 0 for success
Vasu Dev7f349142009-03-27 09:06:31 -0700675 */
Robert Love1875f272009-11-03 11:47:50 -0800676static int fcoe_netdev_config(struct fc_lport *lport, struct net_device *netdev)
Vasu Dev7f349142009-03-27 09:06:31 -0700677{
678 u32 mfs;
679 u64 wwnn, wwpn;
Chris Leech25024982009-08-25 13:59:35 -0700680 struct fcoe_interface *fcoe;
Robert Love619fe4b2012-05-22 19:06:10 -0700681 struct fcoe_ctlr *ctlr;
Chris Leech014f5c32009-08-25 13:59:30 -0700682 struct fcoe_port *port;
Vasu Dev7f349142009-03-27 09:06:31 -0700683
684 /* Setup lport private data to point to fcoe softc */
Robert Love1875f272009-11-03 11:47:50 -0800685 port = lport_priv(lport);
Bhanu Prakash Gollapudi8597ae82011-01-28 16:05:37 -0800686 fcoe = port->priv;
Robert Love619fe4b2012-05-22 19:06:10 -0700687 ctlr = fcoe_to_ctlr(fcoe);
Vasu Dev7f349142009-03-27 09:06:31 -0700688
689 /*
690 * Determine max frame size based on underlying device and optional
691 * user-configured limit. If the MFS is too low, fcoe_link_ok()
692 * will return 0, so do this first.
693 */
Yi Zou7221d7e2009-10-21 16:27:52 -0700694 mfs = netdev->mtu;
695 if (netdev->features & NETIF_F_FCOE_MTU) {
696 mfs = FCOE_MTU;
697 FCOE_NETDEV_DBG(netdev, "Supports FCOE_MTU of %d bytes\n", mfs);
698 }
699 mfs -= (sizeof(struct fcoe_hdr) + sizeof(struct fcoe_crc_eof));
Robert Love1875f272009-11-03 11:47:50 -0800700 if (fc_set_mfs(lport, mfs))
Vasu Dev7f349142009-03-27 09:06:31 -0700701 return -EINVAL;
702
Vasu Dev7f349142009-03-27 09:06:31 -0700703 /* offload features support */
Yi Zou54a5b212010-07-20 15:21:17 -0700704 fcoe_netdev_features_change(lport, netdev);
Vasu Dev7f349142009-03-27 09:06:31 -0700705
Chris Leech014f5c32009-08-25 13:59:30 -0700706 skb_queue_head_init(&port->fcoe_pending_queue);
707 port->fcoe_pending_queue_active = 0;
Robert Love1875f272009-11-03 11:47:50 -0800708 setup_timer(&port->timer, fcoe_queue_timer, (unsigned long)lport);
Vasu Dev7f349142009-03-27 09:06:31 -0700709
Robert Love5e4f8fe2010-05-07 15:18:35 -0700710 fcoe_link_speed_update(lport);
711
Robert Love1875f272009-11-03 11:47:50 -0800712 if (!lport->vport) {
Yi Zoudcece412009-11-20 15:22:21 -0800713 if (fcoe_get_wwn(netdev, &wwnn, NETDEV_FCOE_WWNN))
Robert Love619fe4b2012-05-22 19:06:10 -0700714 wwnn = fcoe_wwn_from_mac(ctlr->ctl_src_addr, 1, 0);
Robert Love1875f272009-11-03 11:47:50 -0800715 fc_set_wwnn(lport, wwnn);
Yi Zoudcece412009-11-20 15:22:21 -0800716 if (fcoe_get_wwn(netdev, &wwpn, NETDEV_FCOE_WWPN))
Robert Love619fe4b2012-05-22 19:06:10 -0700717 wwpn = fcoe_wwn_from_mac(ctlr->ctl_src_addr,
Vasu Devcf4aebca2010-07-20 15:21:22 -0700718 2, 0);
Robert Love1875f272009-11-03 11:47:50 -0800719 fc_set_wwpn(lport, wwpn);
Chris Leech9a057532009-11-03 11:46:40 -0800720 }
Vasu Dev7f349142009-03-27 09:06:31 -0700721
Vasu Dev7f349142009-03-27 09:06:31 -0700722 return 0;
723}
724
725/**
Robert Love1875f272009-11-03 11:47:50 -0800726 * fcoe_shost_config() - Set up the SCSI host associated with a local port
727 * @lport: The local port
Robert Love1875f272009-11-03 11:47:50 -0800728 * @dev: The device associated with the SCSI host
Vasu Dev7f349142009-03-27 09:06:31 -0700729 *
730 * Must be called after fcoe_lport_config() and fcoe_netdev_config()
731 *
Robert Love1875f272009-11-03 11:47:50 -0800732 * Returns: 0 for success
Vasu Dev7f349142009-03-27 09:06:31 -0700733 */
Vasu Dev8ba00a42010-04-09 14:22:54 -0700734static int fcoe_shost_config(struct fc_lport *lport, struct device *dev)
Vasu Dev7f349142009-03-27 09:06:31 -0700735{
736 int rc = 0;
737
738 /* lport scsi host config */
Robert Love1875f272009-11-03 11:47:50 -0800739 lport->host->max_lun = FCOE_MAX_LUN;
740 lport->host->max_id = FCOE_MAX_FCP_TARGET;
741 lport->host->max_channel = 0;
Vasu Devda87bfa2010-04-09 14:22:59 -0700742 lport->host->max_cmd_len = FCOE_MAX_CMD_LEN;
743
Robert Love1875f272009-11-03 11:47:50 -0800744 if (lport->vport)
Yi Zou8ca86f82011-01-28 16:05:11 -0800745 lport->host->transportt = fcoe_vport_scsi_transport;
Chris Leeche9084bb2009-11-03 11:46:34 -0800746 else
Yi Zou8ca86f82011-01-28 16:05:11 -0800747 lport->host->transportt = fcoe_nport_scsi_transport;
Vasu Dev7f349142009-03-27 09:06:31 -0700748
749 /* add the new host to the SCSI-ml */
Robert Love1875f272009-11-03 11:47:50 -0800750 rc = scsi_add_host(lport->host, dev);
Vasu Dev7f349142009-03-27 09:06:31 -0700751 if (rc) {
Robert Love1875f272009-11-03 11:47:50 -0800752 FCOE_NETDEV_DBG(fcoe_netdev(lport), "fcoe_shost_config: "
Robert Loved5488eb2009-06-10 15:30:59 -0700753 "error on scsi_add_host\n");
Vasu Dev7f349142009-03-27 09:06:31 -0700754 return rc;
755 }
Chris Leech9a057532009-11-03 11:46:40 -0800756
Robert Love1875f272009-11-03 11:47:50 -0800757 if (!lport->vport)
Alexey Dobriyan4be929b2010-05-24 14:33:03 -0700758 fc_host_max_npiv_vports(lport->host) = USHRT_MAX;
Chris Leech9a057532009-11-03 11:46:40 -0800759
Robert Love1875f272009-11-03 11:47:50 -0800760 snprintf(fc_host_symbolic_name(lport->host), FC_SYMBOLIC_NAME_SIZE,
Chris Leech5baa17c2009-11-03 11:46:56 -0800761 "%s v%s over %s", FCOE_NAME, FCOE_VERSION,
Robert Love1875f272009-11-03 11:47:50 -0800762 fcoe_netdev(lport)->name);
Vasu Dev7f349142009-03-27 09:06:31 -0700763
764 return 0;
765}
766
Neerav Parikh6fef3902012-01-22 17:30:10 -0800767
768/**
769 * fcoe_fdmi_info() - Get FDMI related info from net devive for SW FCoE
770 * @lport: The local port that is associated with the net device
771 * @netdev: The associated net device
772 *
773 * Must be called after fcoe_shost_config() as it will use local port mutex
774 *
775 */
776static void fcoe_fdmi_info(struct fc_lport *lport, struct net_device *netdev)
777{
778 struct fcoe_interface *fcoe;
779 struct fcoe_port *port;
780 struct net_device *realdev;
781 int rc;
782 struct netdev_fcoe_hbainfo fdmi;
783
784 port = lport_priv(lport);
785 fcoe = port->priv;
786 realdev = fcoe->realdev;
787
788 if (!realdev)
789 return;
790
791 /* No FDMI state m/c for NPIV ports */
792 if (lport->vport)
793 return;
794
795 if (realdev->netdev_ops->ndo_fcoe_get_hbainfo) {
796 memset(&fdmi, 0, sizeof(fdmi));
797 rc = realdev->netdev_ops->ndo_fcoe_get_hbainfo(realdev,
798 &fdmi);
799 if (rc) {
800 printk(KERN_INFO "fcoe: Failed to retrieve FDMI "
801 "information from netdev.\n");
802 return;
803 }
804
805 snprintf(fc_host_serial_number(lport->host),
806 FC_SERIAL_NUMBER_SIZE,
807 "%s",
808 fdmi.serial_number);
809 snprintf(fc_host_manufacturer(lport->host),
810 FC_SERIAL_NUMBER_SIZE,
811 "%s",
812 fdmi.manufacturer);
813 snprintf(fc_host_model(lport->host),
814 FC_SYMBOLIC_NAME_SIZE,
815 "%s",
816 fdmi.model);
817 snprintf(fc_host_model_description(lport->host),
818 FC_SYMBOLIC_NAME_SIZE,
819 "%s",
820 fdmi.model_description);
821 snprintf(fc_host_hardware_version(lport->host),
822 FC_VERSION_STRING_SIZE,
823 "%s",
824 fdmi.hardware_version);
825 snprintf(fc_host_driver_version(lport->host),
826 FC_VERSION_STRING_SIZE,
827 "%s",
828 fdmi.driver_version);
829 snprintf(fc_host_optionrom_version(lport->host),
830 FC_VERSION_STRING_SIZE,
831 "%s",
832 fdmi.optionrom_version);
833 snprintf(fc_host_firmware_version(lport->host),
834 FC_VERSION_STRING_SIZE,
835 "%s",
836 fdmi.firmware_version);
837
838 /* Enable FDMI lport states */
839 lport->fdmi_enabled = 1;
840 } else {
841 lport->fdmi_enabled = 0;
842 printk(KERN_INFO "fcoe: No FDMI support.\n");
843 }
844}
845
Robert Love1875f272009-11-03 11:47:50 -0800846/**
847 * fcoe_oem_match() - The match routine for the offloaded exchange manager
848 * @fp: The I/O frame
Vasu Devd7179682009-07-29 17:05:21 -0700849 *
Robert Love1875f272009-11-03 11:47:50 -0800850 * This routine will be associated with an exchange manager (EM). When
851 * the libfc exchange handling code is looking for an EM to use it will
852 * call this routine and pass it the frame that it wishes to send. This
853 * routine will return True if the associated EM is to be used and False
854 * if the echange code should continue looking for an EM.
855 *
856 * The offload EM that this routine is associated with will handle any
857 * packets that are for SCSI read requests.
858 *
Kiran Patil1ff99182011-06-20 16:59:15 -0700859 * This has been enhanced to work when FCoE stack is operating in target
860 * mode.
861 *
Robert Love1875f272009-11-03 11:47:50 -0800862 * Returns: True for read types I/O, otherwise returns false.
Vasu Devd7179682009-07-29 17:05:21 -0700863 */
Bart Van Assche7c9c6842012-01-13 17:26:25 -0800864static bool fcoe_oem_match(struct fc_frame *fp)
Vasu Devd7179682009-07-29 17:05:21 -0700865{
Kiran Patil1ff99182011-06-20 16:59:15 -0700866 struct fc_frame_header *fh = fc_frame_header_get(fp);
867 struct fcp_cmnd *fcp;
868
869 if (fc_fcp_is_read(fr_fsp(fp)) &&
870 (fr_fsp(fp)->data_len > fcoe_ddp_min))
871 return true;
Yi Zoua762dce2012-01-13 17:26:15 -0800872 else if ((fr_fsp(fp) == NULL) &&
873 (fh->fh_r_ctl == FC_RCTL_DD_UNSOL_CMD) &&
874 (ntohs(fh->fh_rx_id) == FC_XID_UNKNOWN)) {
Kiran Patil1ff99182011-06-20 16:59:15 -0700875 fcp = fc_frame_payload_get(fp, sizeof(*fcp));
Yi Zoua762dce2012-01-13 17:26:15 -0800876 if ((fcp->fc_flags & FCP_CFL_WRDATA) &&
877 (ntohl(fcp->fc_dl) > fcoe_ddp_min))
Kiran Patil1ff99182011-06-20 16:59:15 -0700878 return true;
879 }
880 return false;
Vasu Devd7179682009-07-29 17:05:21 -0700881}
882
Vasu Dev7f349142009-03-27 09:06:31 -0700883/**
Robert Love1875f272009-11-03 11:47:50 -0800884 * fcoe_em_config() - Allocate and configure an exchange manager
885 * @lport: The local port that the new EM will be associated with
Vasu Dev7f349142009-03-27 09:06:31 -0700886 *
Robert Love1875f272009-11-03 11:47:50 -0800887 * Returns: 0 on success
Vasu Dev7f349142009-03-27 09:06:31 -0700888 */
Robert Love1875f272009-11-03 11:47:50 -0800889static inline int fcoe_em_config(struct fc_lport *lport)
Vasu Dev7f349142009-03-27 09:06:31 -0700890{
Robert Love1875f272009-11-03 11:47:50 -0800891 struct fcoe_port *port = lport_priv(lport);
Bhanu Prakash Gollapudi8597ae82011-01-28 16:05:37 -0800892 struct fcoe_interface *fcoe = port->priv;
Chris Leech25024982009-08-25 13:59:35 -0700893 struct fcoe_interface *oldfcoe = NULL;
Vasu Dev1d1b88d2009-07-29 17:05:45 -0700894 struct net_device *old_real_dev, *cur_real_dev;
Vasu Devd7179682009-07-29 17:05:21 -0700895 u16 min_xid = FCOE_MIN_XID;
896 u16 max_xid = FCOE_MAX_XID;
897
898 /*
899 * Check if need to allocate an em instance for
900 * offload exchange ids to be shared across all VN_PORTs/lport.
901 */
Robert Love1875f272009-11-03 11:47:50 -0800902 if (!lport->lro_enabled || !lport->lro_xid ||
903 (lport->lro_xid >= max_xid)) {
904 lport->lro_xid = 0;
Vasu Devd7179682009-07-29 17:05:21 -0700905 goto skip_oem;
906 }
907
908 /*
909 * Reuse existing offload em instance in case
Vasu Dev1d1b88d2009-07-29 17:05:45 -0700910 * it is already allocated on real eth device
Vasu Devd7179682009-07-29 17:05:21 -0700911 */
Chris Leech25024982009-08-25 13:59:35 -0700912 if (fcoe->netdev->priv_flags & IFF_802_1Q_VLAN)
913 cur_real_dev = vlan_dev_real_dev(fcoe->netdev);
Vasu Dev1d1b88d2009-07-29 17:05:45 -0700914 else
Chris Leech25024982009-08-25 13:59:35 -0700915 cur_real_dev = fcoe->netdev;
Vasu Dev1d1b88d2009-07-29 17:05:45 -0700916
Chris Leech25024982009-08-25 13:59:35 -0700917 list_for_each_entry(oldfcoe, &fcoe_hostlist, list) {
Chris Leech25024982009-08-25 13:59:35 -0700918 if (oldfcoe->netdev->priv_flags & IFF_802_1Q_VLAN)
919 old_real_dev = vlan_dev_real_dev(oldfcoe->netdev);
Vasu Dev1d1b88d2009-07-29 17:05:45 -0700920 else
Chris Leech25024982009-08-25 13:59:35 -0700921 old_real_dev = oldfcoe->netdev;
Vasu Dev1d1b88d2009-07-29 17:05:45 -0700922
923 if (cur_real_dev == old_real_dev) {
Chris Leech991cbb62009-08-25 13:59:51 -0700924 fcoe->oem = oldfcoe->oem;
Vasu Devd7179682009-07-29 17:05:21 -0700925 break;
926 }
927 }
928
Chris Leech991cbb62009-08-25 13:59:51 -0700929 if (fcoe->oem) {
Robert Love1875f272009-11-03 11:47:50 -0800930 if (!fc_exch_mgr_add(lport, fcoe->oem, fcoe_oem_match)) {
Vasu Devd7179682009-07-29 17:05:21 -0700931 printk(KERN_ERR "fcoe_em_config: failed to add "
932 "offload em:%p on interface:%s\n",
Chris Leech991cbb62009-08-25 13:59:51 -0700933 fcoe->oem, fcoe->netdev->name);
Vasu Devd7179682009-07-29 17:05:21 -0700934 return -ENOMEM;
935 }
936 } else {
Robert Love1875f272009-11-03 11:47:50 -0800937 fcoe->oem = fc_exch_mgr_alloc(lport, FC_CLASS_3,
938 FCOE_MIN_XID, lport->lro_xid,
939 fcoe_oem_match);
Chris Leech991cbb62009-08-25 13:59:51 -0700940 if (!fcoe->oem) {
Vasu Devd7179682009-07-29 17:05:21 -0700941 printk(KERN_ERR "fcoe_em_config: failed to allocate "
942 "em for offload exches on interface:%s\n",
Chris Leech25024982009-08-25 13:59:35 -0700943 fcoe->netdev->name);
Vasu Devd7179682009-07-29 17:05:21 -0700944 return -ENOMEM;
945 }
946 }
947
948 /*
949 * Exclude offload EM xid range from next EM xid range.
950 */
Robert Love1875f272009-11-03 11:47:50 -0800951 min_xid += lport->lro_xid + 1;
Vasu Devd7179682009-07-29 17:05:21 -0700952
953skip_oem:
Robert Love1875f272009-11-03 11:47:50 -0800954 if (!fc_exch_mgr_alloc(lport, FC_CLASS_3, min_xid, max_xid, NULL)) {
Vasu Devd7179682009-07-29 17:05:21 -0700955 printk(KERN_ERR "fcoe_em_config: failed to "
Chris Leech25024982009-08-25 13:59:35 -0700956 "allocate em on interface %s\n", fcoe->netdev->name);
Vasu Dev7f349142009-03-27 09:06:31 -0700957 return -ENOMEM;
Vasu Devd7179682009-07-29 17:05:21 -0700958 }
Vasu Dev7f349142009-03-27 09:06:31 -0700959
960 return 0;
961}
962
963/**
Robert Love1875f272009-11-03 11:47:50 -0800964 * fcoe_if_destroy() - Tear down a SW FCoE instance
965 * @lport: The local port to be destroyed
Vasu Dev34ce27b2010-05-07 15:18:46 -0700966 *
Vasu Dev7f349142009-03-27 09:06:31 -0700967 */
Chris Leechaf7f85d2009-08-25 13:59:24 -0700968static void fcoe_if_destroy(struct fc_lport *lport)
Vasu Dev7f349142009-03-27 09:06:31 -0700969{
Neerav Parikhb2085a42011-06-20 16:59:51 -0700970 struct fcoe_port *port = lport_priv(lport);
971 struct fcoe_interface *fcoe = port->priv;
972 struct net_device *netdev = fcoe->netdev;
973
974 FCOE_NETDEV_DBG(netdev, "Destroying interface\n");
975
976 /* Logout of the fabric */
977 fc_fabric_logoff(lport);
978
979 /* Cleanup the fc_lport */
980 fc_lport_destroy(lport);
981
982 /* Stop the transmit retry timer */
983 del_timer_sync(&port->timer);
984
985 /* Free existing transmit skbs */
986 fcoe_clean_pending_queue(lport);
987
988 rtnl_lock();
989 if (!is_zero_ether_addr(port->data_src_addr))
990 dev_uc_del(netdev, port->data_src_addr);
Vasu Dev433eba02012-04-20 12:16:27 -0700991 if (lport->vport)
992 synchronize_net();
993 else
994 fcoe_interface_remove(fcoe);
Neerav Parikhb2085a42011-06-20 16:59:51 -0700995 rtnl_unlock();
996
Chris Leech54b649f2009-08-25 14:00:07 -0700997 /* Free queued packets for the per-CPU receive threads */
998 fcoe_percpu_clean(lport);
999
Vasu Dev7f349142009-03-27 09:06:31 -07001000 /* Detach from the scsi-ml */
Chris Leechaf7f85d2009-08-25 13:59:24 -07001001 fc_remove_host(lport->host);
1002 scsi_remove_host(lport->host);
Vasu Dev7f349142009-03-27 09:06:31 -07001003
Yi Zou80e736f2010-11-30 16:18:07 -08001004 /* Destroy lport scsi_priv */
1005 fc_fcp_destroy(lport);
1006
Vasu Dev7f349142009-03-27 09:06:31 -07001007 /* There are no more rports or I/O, free the EM */
Chris Leechaf7f85d2009-08-25 13:59:24 -07001008 fc_exch_mgr_free(lport);
Vasu Dev7f349142009-03-27 09:06:31 -07001009
Vasu Dev7f349142009-03-27 09:06:31 -07001010 /* Free memory used by statistical counters */
Chris Leechaf7f85d2009-08-25 13:59:24 -07001011 fc_lport_free_stats(lport);
Vasu Dev7f349142009-03-27 09:06:31 -07001012
Vasu Dev3cab4462012-04-20 12:16:38 -07001013 /*
1014 * Release the Scsi_Host for vport but hold on to
1015 * master lport until it fcoe interface fully cleaned-up.
1016 */
1017 if (lport->vport)
1018 scsi_host_put(lport->host);
Vasu Dev7f349142009-03-27 09:06:31 -07001019}
1020
Robert Love1875f272009-11-03 11:47:50 -08001021/**
1022 * fcoe_ddp_setup() - Call a LLD's ddp_setup through the net device
1023 * @lport: The local port to setup DDP for
1024 * @xid: The exchange ID for this DDP transfer
1025 * @sgl: The scatterlist describing this transfer
1026 * @sgc: The number of sg items
Vasu Dev7f349142009-03-27 09:06:31 -07001027 *
Robert Love1875f272009-11-03 11:47:50 -08001028 * Returns: 0 if the DDP context was not configured
Vasu Dev7f349142009-03-27 09:06:31 -07001029 */
Robert Love1875f272009-11-03 11:47:50 -08001030static int fcoe_ddp_setup(struct fc_lport *lport, u16 xid,
1031 struct scatterlist *sgl, unsigned int sgc)
Vasu Dev7f349142009-03-27 09:06:31 -07001032{
Robert Love1875f272009-11-03 11:47:50 -08001033 struct net_device *netdev = fcoe_netdev(lport);
Vasu Dev7f349142009-03-27 09:06:31 -07001034
Robert Love1875f272009-11-03 11:47:50 -08001035 if (netdev->netdev_ops->ndo_fcoe_ddp_setup)
1036 return netdev->netdev_ops->ndo_fcoe_ddp_setup(netdev,
1037 xid, sgl,
1038 sgc);
Vasu Dev7f349142009-03-27 09:06:31 -07001039
1040 return 0;
1041}
1042
Vasu Dev7f349142009-03-27 09:06:31 -07001043/**
Yi Zou71f89492011-06-20 16:59:10 -07001044 * fcoe_ddp_target() - Call a LLD's ddp_target through the net device
1045 * @lport: The local port to setup DDP for
1046 * @xid: The exchange ID for this DDP transfer
1047 * @sgl: The scatterlist describing this transfer
1048 * @sgc: The number of sg items
1049 *
1050 * Returns: 0 if the DDP context was not configured
1051 */
1052static int fcoe_ddp_target(struct fc_lport *lport, u16 xid,
1053 struct scatterlist *sgl, unsigned int sgc)
1054{
1055 struct net_device *netdev = fcoe_netdev(lport);
1056
1057 if (netdev->netdev_ops->ndo_fcoe_ddp_target)
1058 return netdev->netdev_ops->ndo_fcoe_ddp_target(netdev, xid,
1059 sgl, sgc);
1060
1061 return 0;
1062}
1063
1064
1065/**
Robert Love1875f272009-11-03 11:47:50 -08001066 * fcoe_ddp_done() - Call a LLD's ddp_done through the net device
1067 * @lport: The local port to complete DDP on
1068 * @xid: The exchange ID for this DDP transfer
Vasu Dev7f349142009-03-27 09:06:31 -07001069 *
Robert Love1875f272009-11-03 11:47:50 -08001070 * Returns: the length of data that have been completed by DDP
1071 */
1072static int fcoe_ddp_done(struct fc_lport *lport, u16 xid)
1073{
1074 struct net_device *netdev = fcoe_netdev(lport);
1075
1076 if (netdev->netdev_ops->ndo_fcoe_ddp_done)
1077 return netdev->netdev_ops->ndo_fcoe_ddp_done(netdev, xid);
1078 return 0;
1079}
1080
1081/**
1082 * fcoe_if_create() - Create a FCoE instance on an interface
1083 * @fcoe: The FCoE interface to create a local port on
1084 * @parent: The device pointer to be the parent in sysfs for the SCSI host
1085 * @npiv: Indicates if the port is a vport or not
Vasu Dev7f349142009-03-27 09:06:31 -07001086 *
Robert Love1875f272009-11-03 11:47:50 -08001087 * Creates a fc_lport instance and a Scsi_Host instance and configure them.
1088 *
1089 * Returns: The allocated fc_lport or an error pointer
Vasu Dev7f349142009-03-27 09:06:31 -07001090 */
Chris Leech030f4e02009-08-25 14:00:02 -07001091static struct fc_lport *fcoe_if_create(struct fcoe_interface *fcoe,
Chris Leech9a057532009-11-03 11:46:40 -08001092 struct device *parent, int npiv)
Vasu Dev7f349142009-03-27 09:06:31 -07001093{
Robert Love619fe4b2012-05-22 19:06:10 -07001094 struct fcoe_ctlr *ctlr = fcoe_to_ctlr(fcoe);
Robert Love1875f272009-11-03 11:47:50 -08001095 struct net_device *netdev = fcoe->netdev;
Vasu Dev72fa3962011-02-25 15:03:01 -08001096 struct fc_lport *lport, *n_port;
Chris Leech014f5c32009-08-25 13:59:30 -07001097 struct fcoe_port *port;
Vasu Dev72fa3962011-02-25 15:03:01 -08001098 struct Scsi_Host *shost;
Robert Love1875f272009-11-03 11:47:50 -08001099 int rc;
Chris Leech9a057532009-11-03 11:46:40 -08001100 /*
1101 * parent is only a vport if npiv is 1,
1102 * but we'll only use vport in that case so go ahead and set it
1103 */
1104 struct fc_vport *vport = dev_to_vport(parent);
Vasu Dev7f349142009-03-27 09:06:31 -07001105
Robert Loved5488eb2009-06-10 15:30:59 -07001106 FCOE_NETDEV_DBG(netdev, "Create Interface\n");
Vasu Dev7f349142009-03-27 09:06:31 -07001107
Vasu Dev72fa3962011-02-25 15:03:01 -08001108 if (!npiv)
1109 lport = libfc_host_alloc(&fcoe_shost_template, sizeof(*port));
1110 else
1111 lport = libfc_vport_create(vport, sizeof(*port));
1112
Chris Leech86221962009-11-03 11:46:08 -08001113 if (!lport) {
Chris Leech014f5c32009-08-25 13:59:30 -07001114 FCOE_NETDEV_DBG(netdev, "Could not allocate host structure\n");
1115 rc = -ENOMEM;
Chris Leech030f4e02009-08-25 14:00:02 -07001116 goto out;
Chris Leech014f5c32009-08-25 13:59:30 -07001117 }
Chris Leech014f5c32009-08-25 13:59:30 -07001118 port = lport_priv(lport);
Chris Leech2e70e242009-08-25 14:00:23 -07001119 port->lport = lport;
Bhanu Prakash Gollapudi8597ae82011-01-28 16:05:37 -08001120 port->priv = fcoe;
1121 port->max_queue_depth = FCOE_MAX_QUEUE_DEPTH;
1122 port->min_queue_depth = FCOE_MIN_QUEUE_DEPTH;
Chris Leech2e70e242009-08-25 14:00:23 -07001123 INIT_WORK(&port->destroy_work, fcoe_destroy_work);
Vasu Dev7f349142009-03-27 09:06:31 -07001124
Robert Love1875f272009-11-03 11:47:50 -08001125 /* configure a fc_lport including the exchange manager */
Chris Leechaf7f85d2009-08-25 13:59:24 -07001126 rc = fcoe_lport_config(lport);
Vasu Dev7f349142009-03-27 09:06:31 -07001127 if (rc) {
Robert Loved5488eb2009-06-10 15:30:59 -07001128 FCOE_NETDEV_DBG(netdev, "Could not configure lport for the "
1129 "interface\n");
Vasu Dev7f349142009-03-27 09:06:31 -07001130 goto out_host_put;
1131 }
1132
Chris Leech9a057532009-11-03 11:46:40 -08001133 if (npiv) {
Chris Leech9f8f3aa2010-04-09 14:23:16 -07001134 FCOE_NETDEV_DBG(netdev, "Setting vport names, "
1135 "%16.16llx %16.16llx\n",
Robert Love1875f272009-11-03 11:47:50 -08001136 vport->node_name, vport->port_name);
Chris Leech9a057532009-11-03 11:46:40 -08001137 fc_set_wwnn(lport, vport->node_name);
1138 fc_set_wwpn(lport, vport->port_name);
1139 }
1140
Vasu Devab6b85c2009-05-17 12:33:08 +00001141 /* configure lport network properties */
Chris Leechaf7f85d2009-08-25 13:59:24 -07001142 rc = fcoe_netdev_config(lport, netdev);
Vasu Devab6b85c2009-05-17 12:33:08 +00001143 if (rc) {
Robert Loved5488eb2009-06-10 15:30:59 -07001144 FCOE_NETDEV_DBG(netdev, "Could not configure netdev for the "
1145 "interface\n");
Chris Leech54b649f2009-08-25 14:00:07 -07001146 goto out_lp_destroy;
Vasu Devab6b85c2009-05-17 12:33:08 +00001147 }
Joe Eykholt97c83892009-03-17 11:42:40 -07001148
Vasu Dev7f349142009-03-27 09:06:31 -07001149 /* configure lport scsi host properties */
Vasu Dev8ba00a42010-04-09 14:22:54 -07001150 rc = fcoe_shost_config(lport, parent);
Vasu Dev7f349142009-03-27 09:06:31 -07001151 if (rc) {
Robert Loved5488eb2009-06-10 15:30:59 -07001152 FCOE_NETDEV_DBG(netdev, "Could not configure shost for the "
1153 "interface\n");
Chris Leech54b649f2009-08-25 14:00:07 -07001154 goto out_lp_destroy;
Vasu Dev7f349142009-03-27 09:06:31 -07001155 }
1156
Vasu Dev7f349142009-03-27 09:06:31 -07001157 /* Initialize the library */
Robert Love619fe4b2012-05-22 19:06:10 -07001158 rc = fcoe_libfc_config(lport, ctlr, &fcoe_libfc_fcn_templ, 1);
Vasu Dev7f349142009-03-27 09:06:31 -07001159 if (rc) {
Robert Loved5488eb2009-06-10 15:30:59 -07001160 FCOE_NETDEV_DBG(netdev, "Could not configure libfc for the "
1161 "interface\n");
Vasu Dev7f349142009-03-27 09:06:31 -07001162 goto out_lp_destroy;
1163 }
1164
Neerav Parikh6fef3902012-01-22 17:30:10 -08001165 /* Initialized FDMI information */
1166 fcoe_fdmi_info(lport, netdev);
1167
Vasu Dev72fa3962011-02-25 15:03:01 -08001168 /*
1169 * fcoe_em_alloc() and fcoe_hostlist_add() both
1170 * need to be atomic with respect to other changes to the
1171 * hostlist since fcoe_em_alloc() looks for an existing EM
1172 * instance on host list updated by fcoe_hostlist_add().
1173 *
1174 * This is currently handled through the fcoe_config_mutex
1175 * begin held.
1176 */
1177 if (!npiv)
Chris Leech9a057532009-11-03 11:46:40 -08001178 /* lport exch manager allocation */
1179 rc = fcoe_em_config(lport);
Vasu Dev72fa3962011-02-25 15:03:01 -08001180 else {
1181 shost = vport_to_shost(vport);
1182 n_port = shost_priv(shost);
1183 rc = fc_exch_mgr_list_clone(n_port, lport);
1184 }
1185
1186 if (rc) {
1187 FCOE_NETDEV_DBG(netdev, "Could not configure the EM\n");
1188 goto out_lp_destroy;
Vasu Dev96316092009-07-29 17:05:00 -07001189 }
1190
Chris Leechaf7f85d2009-08-25 13:59:24 -07001191 return lport;
Vasu Dev7f349142009-03-27 09:06:31 -07001192
1193out_lp_destroy:
Chris Leechaf7f85d2009-08-25 13:59:24 -07001194 fc_exch_mgr_free(lport);
Vasu Dev7f349142009-03-27 09:06:31 -07001195out_host_put:
Chris Leechaf7f85d2009-08-25 13:59:24 -07001196 scsi_host_put(lport->host);
1197out:
1198 return ERR_PTR(rc);
Vasu Dev7f349142009-03-27 09:06:31 -07001199}
1200
1201/**
Robert Love1875f272009-11-03 11:47:50 -08001202 * fcoe_if_init() - Initialization routine for fcoe.ko
Vasu Dev7f349142009-03-27 09:06:31 -07001203 *
Robert Love1875f272009-11-03 11:47:50 -08001204 * Attaches the SW FCoE transport to the FC transport
1205 *
1206 * Returns: 0 on success
Vasu Dev7f349142009-03-27 09:06:31 -07001207 */
1208static int __init fcoe_if_init(void)
1209{
1210 /* attach to scsi transport */
Yi Zou8ca86f82011-01-28 16:05:11 -08001211 fcoe_nport_scsi_transport =
1212 fc_attach_transport(&fcoe_nport_fc_functions);
1213 fcoe_vport_scsi_transport =
1214 fc_attach_transport(&fcoe_vport_fc_functions);
Vasu Dev7f349142009-03-27 09:06:31 -07001215
Yi Zou8ca86f82011-01-28 16:05:11 -08001216 if (!fcoe_nport_scsi_transport) {
Robert Loved5488eb2009-06-10 15:30:59 -07001217 printk(KERN_ERR "fcoe: Failed to attach to the FC transport\n");
Vasu Dev7f349142009-03-27 09:06:31 -07001218 return -ENODEV;
1219 }
1220
1221 return 0;
1222}
1223
1224/**
Robert Love1875f272009-11-03 11:47:50 -08001225 * fcoe_if_exit() - Tear down fcoe.ko
Vasu Dev7f349142009-03-27 09:06:31 -07001226 *
Robert Love1875f272009-11-03 11:47:50 -08001227 * Detaches the SW FCoE transport from the FC transport
1228 *
1229 * Returns: 0 on success
Vasu Dev7f349142009-03-27 09:06:31 -07001230 */
Bart Van Assche7c9c6842012-01-13 17:26:25 -08001231static int __exit fcoe_if_exit(void)
Vasu Dev7f349142009-03-27 09:06:31 -07001232{
Yi Zou8ca86f82011-01-28 16:05:11 -08001233 fc_release_transport(fcoe_nport_scsi_transport);
1234 fc_release_transport(fcoe_vport_scsi_transport);
1235 fcoe_nport_scsi_transport = NULL;
1236 fcoe_vport_scsi_transport = NULL;
Vasu Dev7f349142009-03-27 09:06:31 -07001237 return 0;
1238}
1239
Robert Love85b4aa42008-12-09 15:10:24 -08001240/**
Robert Love1875f272009-11-03 11:47:50 -08001241 * fcoe_percpu_thread_create() - Create a receive thread for an online CPU
1242 * @cpu: The CPU index of the CPU to create a receive thread for
Robert Love8976f422009-03-17 11:41:46 -07001243 */
1244static void fcoe_percpu_thread_create(unsigned int cpu)
1245{
1246 struct fcoe_percpu_s *p;
1247 struct task_struct *thread;
1248
1249 p = &per_cpu(fcoe_percpu, cpu);
1250
Eric Dumazet5c609ff2011-09-27 21:37:52 -07001251 thread = kthread_create_on_node(fcoe_percpu_receive_thread,
1252 (void *)p, cpu_to_node(cpu),
1253 "fcoethread/%d", cpu);
Robert Love8976f422009-03-17 11:41:46 -07001254
Joe Eykholte7a51992009-08-25 14:04:08 -07001255 if (likely(!IS_ERR(thread))) {
Robert Love8976f422009-03-17 11:41:46 -07001256 kthread_bind(thread, cpu);
1257 wake_up_process(thread);
1258
1259 spin_lock_bh(&p->fcoe_rx_list.lock);
1260 p->thread = thread;
1261 spin_unlock_bh(&p->fcoe_rx_list.lock);
1262 }
1263}
1264
1265/**
Robert Love1875f272009-11-03 11:47:50 -08001266 * fcoe_percpu_thread_destroy() - Remove the receive thread of a CPU
1267 * @cpu: The CPU index of the CPU whose receive thread is to be destroyed
Robert Love8976f422009-03-17 11:41:46 -07001268 *
1269 * Destroys a per-CPU Rx thread. Any pending skbs are moved to the
1270 * current CPU's Rx thread. If the thread being destroyed is bound to
1271 * the CPU processing this context the skbs will be freed.
1272 */
1273static void fcoe_percpu_thread_destroy(unsigned int cpu)
1274{
1275 struct fcoe_percpu_s *p;
1276 struct task_struct *thread;
1277 struct page *crc_eof;
1278 struct sk_buff *skb;
1279#ifdef CONFIG_SMP
1280 struct fcoe_percpu_s *p0;
Joe Eykholtf018b732010-03-12 16:08:55 -08001281 unsigned targ_cpu = get_cpu();
Robert Love8976f422009-03-17 11:41:46 -07001282#endif /* CONFIG_SMP */
1283
Robert Loved5488eb2009-06-10 15:30:59 -07001284 FCOE_DBG("Destroying receive thread for CPU %d\n", cpu);
Robert Love8976f422009-03-17 11:41:46 -07001285
1286 /* Prevent any new skbs from being queued for this CPU. */
1287 p = &per_cpu(fcoe_percpu, cpu);
1288 spin_lock_bh(&p->fcoe_rx_list.lock);
1289 thread = p->thread;
1290 p->thread = NULL;
1291 crc_eof = p->crc_eof_page;
1292 p->crc_eof_page = NULL;
1293 p->crc_eof_offset = 0;
1294 spin_unlock_bh(&p->fcoe_rx_list.lock);
1295
1296#ifdef CONFIG_SMP
1297 /*
1298 * Don't bother moving the skb's if this context is running
1299 * on the same CPU that is having its thread destroyed. This
1300 * can easily happen when the module is removed.
1301 */
1302 if (cpu != targ_cpu) {
1303 p0 = &per_cpu(fcoe_percpu, targ_cpu);
1304 spin_lock_bh(&p0->fcoe_rx_list.lock);
1305 if (p0->thread) {
Robert Loved5488eb2009-06-10 15:30:59 -07001306 FCOE_DBG("Moving frames from CPU %d to CPU %d\n",
1307 cpu, targ_cpu);
Robert Love8976f422009-03-17 11:41:46 -07001308
1309 while ((skb = __skb_dequeue(&p->fcoe_rx_list)) != NULL)
1310 __skb_queue_tail(&p0->fcoe_rx_list, skb);
1311 spin_unlock_bh(&p0->fcoe_rx_list.lock);
1312 } else {
1313 /*
1314 * The targeted CPU is not initialized and cannot accept
Robert Love1875f272009-11-03 11:47:50 -08001315 * new skbs. Unlock the targeted CPU and drop the skbs
Robert Love8976f422009-03-17 11:41:46 -07001316 * on the CPU that is going offline.
1317 */
1318 while ((skb = __skb_dequeue(&p->fcoe_rx_list)) != NULL)
1319 kfree_skb(skb);
1320 spin_unlock_bh(&p0->fcoe_rx_list.lock);
1321 }
1322 } else {
1323 /*
1324 * This scenario occurs when the module is being removed
1325 * and all threads are being destroyed. skbs will continue
1326 * to be shifted from the CPU thread that is being removed
1327 * to the CPU thread associated with the CPU that is processing
1328 * the module removal. Once there is only one CPU Rx thread it
1329 * will reach this case and we will drop all skbs and later
1330 * stop the thread.
1331 */
1332 spin_lock_bh(&p->fcoe_rx_list.lock);
1333 while ((skb = __skb_dequeue(&p->fcoe_rx_list)) != NULL)
1334 kfree_skb(skb);
1335 spin_unlock_bh(&p->fcoe_rx_list.lock);
1336 }
Joe Eykholtf018b732010-03-12 16:08:55 -08001337 put_cpu();
Robert Love8976f422009-03-17 11:41:46 -07001338#else
1339 /*
Chris Leechdd3fd722009-04-21 16:27:36 -07001340 * This a non-SMP scenario where the singular Rx thread is
Robert Love8976f422009-03-17 11:41:46 -07001341 * being removed. Free all skbs and stop the thread.
1342 */
1343 spin_lock_bh(&p->fcoe_rx_list.lock);
1344 while ((skb = __skb_dequeue(&p->fcoe_rx_list)) != NULL)
1345 kfree_skb(skb);
1346 spin_unlock_bh(&p->fcoe_rx_list.lock);
1347#endif
1348
1349 if (thread)
1350 kthread_stop(thread);
1351
1352 if (crc_eof)
1353 put_page(crc_eof);
1354}
1355
1356/**
Robert Love1875f272009-11-03 11:47:50 -08001357 * fcoe_cpu_callback() - Handler for CPU hotplug events
1358 * @nfb: The callback data block
1359 * @action: The event triggering the callback
1360 * @hcpu: The index of the CPU that the event is for
Robert Love8976f422009-03-17 11:41:46 -07001361 *
Robert Love1875f272009-11-03 11:47:50 -08001362 * This creates or destroys per-CPU data for fcoe
Robert Love8976f422009-03-17 11:41:46 -07001363 *
1364 * Returns NOTIFY_OK always.
1365 */
1366static int fcoe_cpu_callback(struct notifier_block *nfb,
1367 unsigned long action, void *hcpu)
1368{
1369 unsigned cpu = (unsigned long)hcpu;
1370
1371 switch (action) {
1372 case CPU_ONLINE:
1373 case CPU_ONLINE_FROZEN:
Robert Loved5488eb2009-06-10 15:30:59 -07001374 FCOE_DBG("CPU %x online: Create Rx thread\n", cpu);
Robert Love8976f422009-03-17 11:41:46 -07001375 fcoe_percpu_thread_create(cpu);
1376 break;
1377 case CPU_DEAD:
1378 case CPU_DEAD_FROZEN:
Robert Loved5488eb2009-06-10 15:30:59 -07001379 FCOE_DBG("CPU %x offline: Remove Rx thread\n", cpu);
Robert Love8976f422009-03-17 11:41:46 -07001380 fcoe_percpu_thread_destroy(cpu);
1381 break;
1382 default:
1383 break;
1384 }
1385 return NOTIFY_OK;
1386}
1387
Robert Love8976f422009-03-17 11:41:46 -07001388/**
Kiran Patil064287e2011-06-20 16:59:20 -07001389 * fcoe_select_cpu() - Selects CPU to handle post-processing of incoming
1390 * command.
Kiran Patil064287e2011-06-20 16:59:20 -07001391 *
Vasu Devd2722812011-07-27 15:11:10 -07001392 * This routine selects next CPU based on cpumask to distribute
1393 * incoming requests in round robin.
Kiran Patil064287e2011-06-20 16:59:20 -07001394 *
Vasu Devd2722812011-07-27 15:11:10 -07001395 * Returns: int CPU number
Kiran Patil064287e2011-06-20 16:59:20 -07001396 */
Vasu Devd2722812011-07-27 15:11:10 -07001397static inline unsigned int fcoe_select_cpu(void)
Kiran Patil064287e2011-06-20 16:59:20 -07001398{
1399 static unsigned int selected_cpu;
1400
Vasu Devd2722812011-07-27 15:11:10 -07001401 selected_cpu = cpumask_next(selected_cpu, cpu_online_mask);
1402 if (selected_cpu >= nr_cpu_ids)
1403 selected_cpu = cpumask_first(cpu_online_mask);
1404
Kiran Patil064287e2011-06-20 16:59:20 -07001405 return selected_cpu;
1406}
1407
1408/**
Robert Love1875f272009-11-03 11:47:50 -08001409 * fcoe_rcv() - Receive packets from a net device
1410 * @skb: The received packet
1411 * @netdev: The net device that the packet was received on
1412 * @ptype: The packet type context
1413 * @olddev: The last device net device
Robert Love85b4aa42008-12-09 15:10:24 -08001414 *
Robert Love1875f272009-11-03 11:47:50 -08001415 * This routine is called by NET_RX_SOFTIRQ. It receives a packet, builds a
1416 * FC frame and passes the frame to libfc.
Robert Love85b4aa42008-12-09 15:10:24 -08001417 *
1418 * Returns: 0 for success
Robert Love34f42a02009-02-27 10:55:45 -08001419 */
Bart Van Assche7c9c6842012-01-13 17:26:25 -08001420static int fcoe_rcv(struct sk_buff *skb, struct net_device *netdev,
Robert Love85b4aa42008-12-09 15:10:24 -08001421 struct packet_type *ptype, struct net_device *olddev)
1422{
Robert Love1875f272009-11-03 11:47:50 -08001423 struct fc_lport *lport;
Robert Love85b4aa42008-12-09 15:10:24 -08001424 struct fcoe_rcv_info *fr;
Robert Love619fe4b2012-05-22 19:06:10 -07001425 struct fcoe_ctlr *ctlr;
Chris Leech259ad852009-08-25 13:59:41 -07001426 struct fcoe_interface *fcoe;
Robert Love85b4aa42008-12-09 15:10:24 -08001427 struct fc_frame_header *fh;
Robert Love85b4aa42008-12-09 15:10:24 -08001428 struct fcoe_percpu_s *fps;
Vasu Dev519e5132010-07-20 15:19:32 -07001429 struct ethhdr *eh;
Vasu Devb2f00912009-08-25 13:58:53 -07001430 unsigned int cpu;
Robert Love85b4aa42008-12-09 15:10:24 -08001431
Chris Leech259ad852009-08-25 13:59:41 -07001432 fcoe = container_of(ptype, struct fcoe_interface, fcoe_packet_type);
Robert Love619fe4b2012-05-22 19:06:10 -07001433 ctlr = fcoe_to_ctlr(fcoe);
1434 lport = ctlr->lp;
Robert Love1875f272009-11-03 11:47:50 -08001435 if (unlikely(!lport)) {
1436 FCOE_NETDEV_DBG(netdev, "Cannot find hba structure");
Robert Love85b4aa42008-12-09 15:10:24 -08001437 goto err2;
1438 }
Robert Love1875f272009-11-03 11:47:50 -08001439 if (!lport->link_up)
Joe Eykholt97c83892009-03-17 11:42:40 -07001440 goto err2;
Robert Love85b4aa42008-12-09 15:10:24 -08001441
Robert Love1875f272009-11-03 11:47:50 -08001442 FCOE_NETDEV_DBG(netdev, "skb_info: len:%d data_len:%d head:%p "
Robert Loved5488eb2009-06-10 15:30:59 -07001443 "data:%p tail:%p end:%p sum:%d dev:%s",
1444 skb->len, skb->data_len, skb->head, skb->data,
1445 skb_tail_pointer(skb), skb_end_pointer(skb),
1446 skb->csum, skb->dev ? skb->dev->name : "<NULL>");
Robert Love85b4aa42008-12-09 15:10:24 -08001447
Vasu Dev519e5132010-07-20 15:19:32 -07001448 eh = eth_hdr(skb);
Vasu Dev519e5132010-07-20 15:19:32 -07001449
Robert Love619fe4b2012-05-22 19:06:10 -07001450 if (is_fip_mode(ctlr) &&
1451 compare_ether_addr(eh->h_source, ctlr->dest_addr)) {
Vasu Dev519e5132010-07-20 15:19:32 -07001452 FCOE_NETDEV_DBG(netdev, "wrong source mac address:%pM\n",
1453 eh->h_source);
Robert Love85b4aa42008-12-09 15:10:24 -08001454 goto err;
1455 }
1456
1457 /*
1458 * Check for minimum frame length, and make sure required FCoE
1459 * and FC headers are pulled into the linear data area.
1460 */
1461 if (unlikely((skb->len < FCOE_MIN_FRAME) ||
Robert Love1875f272009-11-03 11:47:50 -08001462 !pskb_may_pull(skb, FCOE_HEADER_LEN)))
Robert Love85b4aa42008-12-09 15:10:24 -08001463 goto err;
1464
1465 skb_set_transport_header(skb, sizeof(struct fcoe_hdr));
1466 fh = (struct fc_frame_header *) skb_transport_header(skb);
1467
Robert Love0ee31cb2010-10-08 17:12:46 -07001468 if (ntoh24(&eh->h_dest[3]) != ntoh24(fh->fh_d_id)) {
1469 FCOE_NETDEV_DBG(netdev, "FC frame d_id mismatch with MAC:%pM\n",
1470 eh->h_dest);
1471 goto err;
1472 }
1473
Robert Love85b4aa42008-12-09 15:10:24 -08001474 fr = fcoe_dev_from_skb(skb);
Robert Love1875f272009-11-03 11:47:50 -08001475 fr->fr_dev = lport;
Robert Love5e5e92d2009-03-17 11:41:35 -07001476
Robert Love85b4aa42008-12-09 15:10:24 -08001477 /*
Vasu Devb2f00912009-08-25 13:58:53 -07001478 * In case the incoming frame's exchange is originated from
1479 * the initiator, then received frame's exchange id is ANDed
1480 * with fc_cpu_mask bits to get the same cpu on which exchange
Vasu Devd2722812011-07-27 15:11:10 -07001481 * was originated, otherwise select cpu using rx exchange id
1482 * or fcoe_select_cpu().
Robert Love85b4aa42008-12-09 15:10:24 -08001483 */
Vasu Devb2f00912009-08-25 13:58:53 -07001484 if (ntoh24(fh->fh_f_ctl) & FC_FC_EX_CTX)
1485 cpu = ntohs(fh->fh_ox_id) & fc_cpu_mask;
Kiran Patil064287e2011-06-20 16:59:20 -07001486 else {
Vasu Devd2722812011-07-27 15:11:10 -07001487 if (ntohs(fh->fh_rx_id) == FC_XID_UNKNOWN)
1488 cpu = fcoe_select_cpu();
1489 else
Kiran Patil29bdd2b2011-06-20 16:59:25 -07001490 cpu = ntohs(fh->fh_rx_id) & fc_cpu_mask;
Kiran Patil064287e2011-06-20 16:59:20 -07001491 }
Vasu Dev324f6672011-07-27 15:10:39 -07001492
1493 if (cpu >= nr_cpu_ids)
1494 goto err;
1495
Robert Love8976f422009-03-17 11:41:46 -07001496 fps = &per_cpu(fcoe_percpu, cpu);
Neil Horman94aa29f2012-03-09 14:50:08 -08001497 spin_lock(&fps->fcoe_rx_list.lock);
Robert Love8976f422009-03-17 11:41:46 -07001498 if (unlikely(!fps->thread)) {
1499 /*
1500 * The targeted CPU is not ready, let's target
1501 * the first CPU now. For non-SMP systems this
1502 * will check the same CPU twice.
1503 */
Robert Love1875f272009-11-03 11:47:50 -08001504 FCOE_NETDEV_DBG(netdev, "CPU is online, but no receive thread "
Robert Loved5488eb2009-06-10 15:30:59 -07001505 "ready for incoming skb- using first online "
1506 "CPU.\n");
Robert Love8976f422009-03-17 11:41:46 -07001507
Neil Horman94aa29f2012-03-09 14:50:08 -08001508 spin_unlock(&fps->fcoe_rx_list.lock);
Rusty Russell69571772009-12-17 11:43:14 -06001509 cpu = cpumask_first(cpu_online_mask);
Robert Love8976f422009-03-17 11:41:46 -07001510 fps = &per_cpu(fcoe_percpu, cpu);
Neil Horman94aa29f2012-03-09 14:50:08 -08001511 spin_lock(&fps->fcoe_rx_list.lock);
Robert Love8976f422009-03-17 11:41:46 -07001512 if (!fps->thread) {
Neil Horman94aa29f2012-03-09 14:50:08 -08001513 spin_unlock(&fps->fcoe_rx_list.lock);
Robert Love8976f422009-03-17 11:41:46 -07001514 goto err;
1515 }
1516 }
1517
1518 /*
1519 * We now have a valid CPU that we're targeting for
1520 * this skb. We also have this receive thread locked,
1521 * so we're free to queue skbs into it's queue.
1522 */
Robert Love85b4aa42008-12-09 15:10:24 -08001523
Neil Horman5e70c4c2012-03-09 14:49:48 -08001524 /*
1525 * Note: We used to have a set of conditions under which we would
1526 * call fcoe_recv_frame directly, rather than queuing to the rx list
1527 * as it could save a few cycles, but doing so is prohibited, as
1528 * fcoe_recv_frame has several paths that may sleep, which is forbidden
1529 * in softirq context.
Chris Leech859b7b62009-11-20 14:54:47 -08001530 */
Neil Horman5e70c4c2012-03-09 14:49:48 -08001531 __skb_queue_tail(&fps->fcoe_rx_list, skb);
Neil Horman20dc3812012-03-09 14:50:24 -08001532 if (fps->thread->state == TASK_INTERRUPTIBLE)
Neil Horman5e70c4c2012-03-09 14:49:48 -08001533 wake_up_process(fps->thread);
Neil Horman94aa29f2012-03-09 14:50:08 -08001534 spin_unlock(&fps->fcoe_rx_list.lock);
Robert Love85b4aa42008-12-09 15:10:24 -08001535
1536 return 0;
1537err:
Vasu Dev1bd49b42012-05-25 10:26:43 -07001538 per_cpu_ptr(lport->stats, get_cpu())->ErrorFrames++;
Joe Eykholtf018b732010-03-12 16:08:55 -08001539 put_cpu();
Robert Love85b4aa42008-12-09 15:10:24 -08001540err2:
1541 kfree_skb(skb);
1542 return -1;
1543}
Robert Love85b4aa42008-12-09 15:10:24 -08001544
1545/**
Bhanu Prakash Gollapudi8597ae82011-01-28 16:05:37 -08001546 * fcoe_alloc_paged_crc_eof() - Allocate a page to be used for the trailer CRC
Robert Love1875f272009-11-03 11:47:50 -08001547 * @skb: The packet to be transmitted
1548 * @tlen: The total length of the trailer
1549 *
Robert Love85b4aa42008-12-09 15:10:24 -08001550 * Returns: 0 for success
Robert Love34f42a02009-02-27 10:55:45 -08001551 */
Bhanu Prakash Gollapudi8597ae82011-01-28 16:05:37 -08001552static int fcoe_alloc_paged_crc_eof(struct sk_buff *skb, int tlen)
Robert Love85b4aa42008-12-09 15:10:24 -08001553{
1554 struct fcoe_percpu_s *fps;
Bhanu Prakash Gollapudi8597ae82011-01-28 16:05:37 -08001555 int rc;
Robert Love85b4aa42008-12-09 15:10:24 -08001556
Robert Love5e5e92d2009-03-17 11:41:35 -07001557 fps = &get_cpu_var(fcoe_percpu);
Bhanu Prakash Gollapudi8597ae82011-01-28 16:05:37 -08001558 rc = fcoe_get_paged_crc_eof(skb, tlen, fps);
Robert Love5e5e92d2009-03-17 11:41:35 -07001559 put_cpu_var(fcoe_percpu);
Robert Love85b4aa42008-12-09 15:10:24 -08001560
Bhanu Prakash Gollapudi8597ae82011-01-28 16:05:37 -08001561 return rc;
Robert Love85b4aa42008-12-09 15:10:24 -08001562}
Robert Love85b4aa42008-12-09 15:10:24 -08001563
1564/**
Robert Love1875f272009-11-03 11:47:50 -08001565 * fcoe_xmit() - Transmit a FCoE frame
1566 * @lport: The local port that the frame is to be transmitted for
1567 * @fp: The frame to be transmitted
Robert Love85b4aa42008-12-09 15:10:24 -08001568 *
Robert Love1875f272009-11-03 11:47:50 -08001569 * Return: 0 for success
Robert Love34f42a02009-02-27 10:55:45 -08001570 */
Bart Van Assche7c9c6842012-01-13 17:26:25 -08001571static int fcoe_xmit(struct fc_lport *lport, struct fc_frame *fp)
Robert Love85b4aa42008-12-09 15:10:24 -08001572{
Vasu Dev4bb6b512009-05-06 10:52:34 -07001573 int wlen;
Robert Love85b4aa42008-12-09 15:10:24 -08001574 u32 crc;
1575 struct ethhdr *eh;
1576 struct fcoe_crc_eof *cp;
1577 struct sk_buff *skb;
Vasu Dev1bd49b42012-05-25 10:26:43 -07001578 struct fc_stats *stats;
Robert Love85b4aa42008-12-09 15:10:24 -08001579 struct fc_frame_header *fh;
1580 unsigned int hlen; /* header length implies the version */
1581 unsigned int tlen; /* trailer length */
1582 unsigned int elen; /* eth header, may include vlan */
Robert Love1875f272009-11-03 11:47:50 -08001583 struct fcoe_port *port = lport_priv(lport);
Bhanu Prakash Gollapudi8597ae82011-01-28 16:05:37 -08001584 struct fcoe_interface *fcoe = port->priv;
Robert Love619fe4b2012-05-22 19:06:10 -07001585 struct fcoe_ctlr *ctlr = fcoe_to_ctlr(fcoe);
Robert Love85b4aa42008-12-09 15:10:24 -08001586 u8 sof, eof;
1587 struct fcoe_hdr *hp;
1588
1589 WARN_ON((fr_len(fp) % sizeof(u32)) != 0);
1590
Robert Love85b4aa42008-12-09 15:10:24 -08001591 fh = fc_frame_header_get(fp);
Joe Eykholt97c83892009-03-17 11:42:40 -07001592 skb = fp_skb(fp);
1593 wlen = skb->len / FCOE_WORD_TO_BYTE;
1594
Robert Love1875f272009-11-03 11:47:50 -08001595 if (!lport->link_up) {
Dan Carpenter3caf02e2009-04-21 16:27:25 -07001596 kfree_skb(skb);
Joe Eykholt97c83892009-03-17 11:42:40 -07001597 return 0;
Robert Love85b4aa42008-12-09 15:10:24 -08001598 }
1599
Joe Eykholt9860eeb2010-03-12 16:07:52 -08001600 if (unlikely(fh->fh_type == FC_TYPE_ELS) &&
Robert Love619fe4b2012-05-22 19:06:10 -07001601 fcoe_ctlr_els_send(ctlr, lport, skb))
Joe Eykholt97c83892009-03-17 11:42:40 -07001602 return 0;
1603
Robert Love85b4aa42008-12-09 15:10:24 -08001604 sof = fr_sof(fp);
1605 eof = fr_eof(fp);
1606
Vasu Dev4e57e1c2009-05-06 10:52:46 -07001607 elen = sizeof(struct ethhdr);
Robert Love85b4aa42008-12-09 15:10:24 -08001608 hlen = sizeof(struct fcoe_hdr);
1609 tlen = sizeof(struct fcoe_crc_eof);
1610 wlen = (skb->len - tlen + sizeof(crc)) / FCOE_WORD_TO_BYTE;
1611
1612 /* crc offload */
Robert Love1875f272009-11-03 11:47:50 -08001613 if (likely(lport->crc_offload)) {
Yi Zoucf64bc82012-03-16 23:08:12 +00001614 skb->ip_summed = CHECKSUM_UNNECESSARY;
Robert Love85b4aa42008-12-09 15:10:24 -08001615 skb->csum_start = skb_headroom(skb);
1616 skb->csum_offset = skb->len;
1617 crc = 0;
1618 } else {
1619 skb->ip_summed = CHECKSUM_NONE;
1620 crc = fcoe_fc_crc(fp);
1621 }
1622
Chris Leech014f5c32009-08-25 13:59:30 -07001623 /* copy port crc and eof to the skb buff */
Robert Love85b4aa42008-12-09 15:10:24 -08001624 if (skb_is_nonlinear(skb)) {
1625 skb_frag_t *frag;
Bhanu Prakash Gollapudi8597ae82011-01-28 16:05:37 -08001626 if (fcoe_alloc_paged_crc_eof(skb, tlen)) {
Roel Kluine9041582009-02-27 10:56:22 -08001627 kfree_skb(skb);
Robert Love85b4aa42008-12-09 15:10:24 -08001628 return -ENOMEM;
1629 }
1630 frag = &skb_shinfo(skb)->frags[skb_shinfo(skb)->nr_frags - 1];
Cong Wang77dfce02011-11-25 23:14:23 +08001631 cp = kmap_atomic(skb_frag_page(frag))
Robert Love85b4aa42008-12-09 15:10:24 -08001632 + frag->page_offset;
1633 } else {
1634 cp = (struct fcoe_crc_eof *)skb_put(skb, tlen);
1635 }
1636
1637 memset(cp, 0, sizeof(*cp));
1638 cp->fcoe_eof = eof;
1639 cp->fcoe_crc32 = cpu_to_le32(~crc);
1640
1641 if (skb_is_nonlinear(skb)) {
Cong Wang77dfce02011-11-25 23:14:23 +08001642 kunmap_atomic(cp);
Robert Love85b4aa42008-12-09 15:10:24 -08001643 cp = NULL;
1644 }
1645
Chris Leech014f5c32009-08-25 13:59:30 -07001646 /* adjust skb network/transport offsets to match mac/fcoe/port */
Robert Love85b4aa42008-12-09 15:10:24 -08001647 skb_push(skb, elen + hlen);
1648 skb_reset_mac_header(skb);
1649 skb_reset_network_header(skb);
1650 skb->mac_len = elen;
Yi Zou211c7382009-02-27 14:06:37 -08001651 skb->protocol = htons(ETH_P_FCOE);
Neerav Parikh31c37a62012-09-24 11:52:45 -07001652 skb->priority = fcoe->priority;
john fastabend6f6c2aa2011-11-18 13:35:56 -08001653
Vasu Devd1483bb2011-09-27 21:38:13 -07001654 if (fcoe->netdev->priv_flags & IFF_802_1Q_VLAN &&
1655 fcoe->realdev->features & NETIF_F_HW_VLAN_TX) {
1656 skb->vlan_tci = VLAN_TAG_PRESENT |
1657 vlan_dev_vlan_id(fcoe->netdev);
1658 skb->dev = fcoe->realdev;
1659 } else
1660 skb->dev = fcoe->netdev;
Robert Love85b4aa42008-12-09 15:10:24 -08001661
1662 /* fill up mac and fcoe headers */
1663 eh = eth_hdr(skb);
1664 eh->h_proto = htons(ETH_P_FCOE);
Robert Love619fe4b2012-05-22 19:06:10 -07001665 memcpy(eh->h_dest, ctlr->dest_addr, ETH_ALEN);
1666 if (ctlr->map_dest)
Joe Eykholtcd229e42010-07-20 15:20:40 -07001667 memcpy(eh->h_dest + 3, fh->fh_d_id, 3);
Robert Love85b4aa42008-12-09 15:10:24 -08001668
Robert Love619fe4b2012-05-22 19:06:10 -07001669 if (unlikely(ctlr->flogi_oxid != FC_XID_UNKNOWN))
1670 memcpy(eh->h_source, ctlr->ctl_src_addr, ETH_ALEN);
Robert Love85b4aa42008-12-09 15:10:24 -08001671 else
Chris Leech11b56182009-11-03 11:46:29 -08001672 memcpy(eh->h_source, port->data_src_addr, ETH_ALEN);
Robert Love85b4aa42008-12-09 15:10:24 -08001673
1674 hp = (struct fcoe_hdr *)(eh + 1);
1675 memset(hp, 0, sizeof(*hp));
1676 if (FC_FCOE_VER)
1677 FC_FCOE_ENCAPS_VER(hp, FC_FCOE_VER);
1678 hp->fcoe_sof = sof;
1679
Yi Zou39ca9a02009-02-27 14:07:15 -08001680 /* fcoe lso, mss is in max_payload which is non-zero for FCP data */
Robert Love1875f272009-11-03 11:47:50 -08001681 if (lport->seq_offload && fr_max_payload(fp)) {
Yi Zou39ca9a02009-02-27 14:07:15 -08001682 skb_shinfo(skb)->gso_type = SKB_GSO_FCOE;
1683 skb_shinfo(skb)->gso_size = fr_max_payload(fp);
1684 } else {
1685 skb_shinfo(skb)->gso_type = 0;
1686 skb_shinfo(skb)->gso_size = 0;
1687 }
Robert Love85b4aa42008-12-09 15:10:24 -08001688 /* update tx stats: regardless if LLD fails */
Vasu Dev1bd49b42012-05-25 10:26:43 -07001689 stats = per_cpu_ptr(lport->stats, get_cpu());
Robert Love582b45b2009-03-31 15:51:50 -07001690 stats->TxFrames++;
1691 stats->TxWords += wlen;
Joe Eykholtf018b732010-03-12 16:08:55 -08001692 put_cpu();
Robert Love85b4aa42008-12-09 15:10:24 -08001693
1694 /* send down to lld */
Robert Love1875f272009-11-03 11:47:50 -08001695 fr_dev(fp) = lport;
Vasu Dev980f5152011-07-27 15:11:05 -07001696 fcoe_port_send(port, skb);
Robert Love85b4aa42008-12-09 15:10:24 -08001697 return 0;
1698}
Robert Love85b4aa42008-12-09 15:10:24 -08001699
Robert Love34f42a02009-02-27 10:55:45 -08001700/**
Robert Love1875f272009-11-03 11:47:50 -08001701 * fcoe_percpu_flush_done() - Indicate per-CPU queue flush completion
1702 * @skb: The completed skb (argument required by destructor)
Joe Eykholte7a51992009-08-25 14:04:08 -07001703 */
1704static void fcoe_percpu_flush_done(struct sk_buff *skb)
1705{
1706 complete(&fcoe_flush_completion);
1707}
1708
1709/**
Vasu Dev52ee83212011-01-28 16:03:52 -08001710 * fcoe_filter_frames() - filter out bad fcoe frames, i.e. bad CRC
1711 * @lport: The local port the frame was received on
1712 * @fp: The received frame
1713 *
1714 * Return: 0 on passing filtering checks
1715 */
1716static inline int fcoe_filter_frames(struct fc_lport *lport,
1717 struct fc_frame *fp)
1718{
Robert Love619fe4b2012-05-22 19:06:10 -07001719 struct fcoe_ctlr *ctlr;
Vasu Dev52ee83212011-01-28 16:03:52 -08001720 struct fcoe_interface *fcoe;
1721 struct fc_frame_header *fh;
1722 struct sk_buff *skb = (struct sk_buff *)fp;
Vasu Dev1bd49b42012-05-25 10:26:43 -07001723 struct fc_stats *stats;
Vasu Dev52ee83212011-01-28 16:03:52 -08001724
1725 /*
1726 * We only check CRC if no offload is available and if it is
1727 * it's solicited data, in which case, the FCP layer would
1728 * check it during the copy.
1729 */
1730 if (lport->crc_offload && skb->ip_summed == CHECKSUM_UNNECESSARY)
1731 fr_flags(fp) &= ~FCPHF_CRC_UNCHECKED;
1732 else
1733 fr_flags(fp) |= FCPHF_CRC_UNCHECKED;
1734
1735 fh = (struct fc_frame_header *) skb_transport_header(skb);
1736 fh = fc_frame_header_get(fp);
1737 if (fh->fh_r_ctl == FC_RCTL_DD_SOL_DATA && fh->fh_type == FC_TYPE_FCP)
1738 return 0;
1739
Bhanu Prakash Gollapudi8597ae82011-01-28 16:05:37 -08001740 fcoe = ((struct fcoe_port *)lport_priv(lport))->priv;
Robert Love619fe4b2012-05-22 19:06:10 -07001741 ctlr = fcoe_to_ctlr(fcoe);
1742 if (is_fip_mode(ctlr) && fc_frame_payload_op(fp) == ELS_LOGO &&
Vasu Dev52ee83212011-01-28 16:03:52 -08001743 ntoh24(fh->fh_s_id) == FC_FID_FLOGI) {
1744 FCOE_DBG("fcoe: dropping FCoE lport LOGO in fip mode\n");
1745 return -EINVAL;
1746 }
1747
Dan Carpenterf2f96d22011-02-25 15:03:23 -08001748 if (!(fr_flags(fp) & FCPHF_CRC_UNCHECKED) ||
Vasu Dev52ee83212011-01-28 16:03:52 -08001749 le32_to_cpu(fr_crc(fp)) == ~crc32(~0, skb->data, skb->len)) {
1750 fr_flags(fp) &= ~FCPHF_CRC_UNCHECKED;
1751 return 0;
1752 }
1753
Vasu Dev1bd49b42012-05-25 10:26:43 -07001754 stats = per_cpu_ptr(lport->stats, get_cpu());
Vasu Dev52ee83212011-01-28 16:03:52 -08001755 stats->InvalidCRCCount++;
1756 if (stats->InvalidCRCCount < 5)
1757 printk(KERN_WARNING "fcoe: dropping frame with CRC error\n");
Thomas Gleixner7e1e7ea2011-11-11 20:52:01 +01001758 put_cpu();
Vasu Dev52ee83212011-01-28 16:03:52 -08001759 return -EINVAL;
1760}
1761
1762/**
Chris Leech859b7b62009-11-20 14:54:47 -08001763 * fcoe_recv_frame() - process a single received frame
1764 * @skb: frame to process
1765 */
1766static void fcoe_recv_frame(struct sk_buff *skb)
1767{
1768 u32 fr_len;
1769 struct fc_lport *lport;
1770 struct fcoe_rcv_info *fr;
Vasu Dev1bd49b42012-05-25 10:26:43 -07001771 struct fc_stats *stats;
Chris Leech859b7b62009-11-20 14:54:47 -08001772 struct fcoe_crc_eof crc_eof;
1773 struct fc_frame *fp;
Chris Leech859b7b62009-11-20 14:54:47 -08001774 struct fcoe_port *port;
1775 struct fcoe_hdr *hp;
1776
1777 fr = fcoe_dev_from_skb(skb);
1778 lport = fr->fr_dev;
1779 if (unlikely(!lport)) {
1780 if (skb->destructor != fcoe_percpu_flush_done)
1781 FCOE_NETDEV_DBG(skb->dev, "NULL lport in skb");
1782 kfree_skb(skb);
1783 return;
1784 }
1785
1786 FCOE_NETDEV_DBG(skb->dev, "skb_info: len:%d data_len:%d "
1787 "head:%p data:%p tail:%p end:%p sum:%d dev:%s",
1788 skb->len, skb->data_len,
1789 skb->head, skb->data, skb_tail_pointer(skb),
1790 skb_end_pointer(skb), skb->csum,
1791 skb->dev ? skb->dev->name : "<NULL>");
1792
Chris Leech859b7b62009-11-20 14:54:47 -08001793 port = lport_priv(lport);
Robert Lovef1633012011-12-16 14:24:49 -08001794 skb_linearize(skb); /* check for skb_is_nonlinear is within skb_linearize */
Chris Leech859b7b62009-11-20 14:54:47 -08001795
1796 /*
1797 * Frame length checks and setting up the header pointers
1798 * was done in fcoe_rcv already.
1799 */
1800 hp = (struct fcoe_hdr *) skb_network_header(skb);
Chris Leech859b7b62009-11-20 14:54:47 -08001801
Vasu Dev1bd49b42012-05-25 10:26:43 -07001802 stats = per_cpu_ptr(lport->stats, get_cpu());
Chris Leech859b7b62009-11-20 14:54:47 -08001803 if (unlikely(FC_FCOE_DECAPS_VER(hp) != FC_FCOE_VER)) {
1804 if (stats->ErrorFrames < 5)
1805 printk(KERN_WARNING "fcoe: FCoE version "
1806 "mismatch: The frame has "
1807 "version %x, but the "
1808 "initiator supports version "
1809 "%x\n", FC_FCOE_DECAPS_VER(hp),
1810 FC_FCOE_VER);
Joe Eykholtf018b732010-03-12 16:08:55 -08001811 goto drop;
Chris Leech859b7b62009-11-20 14:54:47 -08001812 }
1813
1814 skb_pull(skb, sizeof(struct fcoe_hdr));
1815 fr_len = skb->len - sizeof(struct fcoe_crc_eof);
1816
1817 stats->RxFrames++;
1818 stats->RxWords += fr_len / FCOE_WORD_TO_BYTE;
1819
1820 fp = (struct fc_frame *)skb;
1821 fc_frame_init(fp);
1822 fr_dev(fp) = lport;
1823 fr_sof(fp) = hp->fcoe_sof;
1824
1825 /* Copy out the CRC and EOF trailer for access */
Joe Eykholtf018b732010-03-12 16:08:55 -08001826 if (skb_copy_bits(skb, fr_len, &crc_eof, sizeof(crc_eof)))
1827 goto drop;
Chris Leech859b7b62009-11-20 14:54:47 -08001828 fr_eof(fp) = crc_eof.fcoe_eof;
1829 fr_crc(fp) = crc_eof.fcoe_crc32;
Joe Eykholtf018b732010-03-12 16:08:55 -08001830 if (pskb_trim(skb, fr_len))
1831 goto drop;
Chris Leech859b7b62009-11-20 14:54:47 -08001832
Vasu Dev52ee83212011-01-28 16:03:52 -08001833 if (!fcoe_filter_frames(lport, fp)) {
1834 put_cpu();
1835 fc_exch_recv(lport, fp);
1836 return;
Chris Leech859b7b62009-11-20 14:54:47 -08001837 }
Joe Eykholtf018b732010-03-12 16:08:55 -08001838drop:
1839 stats->ErrorFrames++;
1840 put_cpu();
1841 kfree_skb(skb);
Chris Leech859b7b62009-11-20 14:54:47 -08001842}
1843
1844/**
Robert Love1875f272009-11-03 11:47:50 -08001845 * fcoe_percpu_receive_thread() - The per-CPU packet receive thread
1846 * @arg: The per-CPU context
Robert Love85b4aa42008-12-09 15:10:24 -08001847 *
1848 * Return: 0 for success
Robert Love85b4aa42008-12-09 15:10:24 -08001849 */
Bart Van Assche7c9c6842012-01-13 17:26:25 -08001850static int fcoe_percpu_receive_thread(void *arg)
Robert Love85b4aa42008-12-09 15:10:24 -08001851{
1852 struct fcoe_percpu_s *p = arg;
Robert Love85b4aa42008-12-09 15:10:24 -08001853 struct sk_buff *skb;
Neil Horman20dc3812012-03-09 14:50:24 -08001854 struct sk_buff_head tmp;
1855
1856 skb_queue_head_init(&tmp);
Robert Love85b4aa42008-12-09 15:10:24 -08001857
Robert Love4469c192009-02-27 10:56:38 -08001858 set_user_nice(current, -20);
Robert Love85b4aa42008-12-09 15:10:24 -08001859
Neil Horman95fdd5e2012-07-06 10:40:05 -07001860retry:
Robert Love85b4aa42008-12-09 15:10:24 -08001861 while (!kthread_should_stop()) {
1862
1863 spin_lock_bh(&p->fcoe_rx_list.lock);
Neil Horman20dc3812012-03-09 14:50:24 -08001864 skb_queue_splice_init(&p->fcoe_rx_list, &tmp);
Neil Horman95fdd5e2012-07-06 10:40:05 -07001865
1866 if (!skb_queue_len(&tmp)) {
1867 set_current_state(TASK_INTERRUPTIBLE);
1868 spin_unlock_bh(&p->fcoe_rx_list.lock);
1869 schedule();
1870 set_current_state(TASK_RUNNING);
1871 goto retry;
1872 }
1873
Neil Horman20dc3812012-03-09 14:50:24 -08001874 spin_unlock_bh(&p->fcoe_rx_list.lock);
1875
1876 while ((skb = __skb_dequeue(&tmp)) != NULL)
1877 fcoe_recv_frame(skb);
1878
Robert Love85b4aa42008-12-09 15:10:24 -08001879 }
1880 return 0;
1881}
1882
1883/**
Robert Love1875f272009-11-03 11:47:50 -08001884 * fcoe_dev_setup() - Setup the link change notification interface
Robert Love34f42a02009-02-27 10:55:45 -08001885 */
Randy Dunlapb0d428a2009-04-27 21:49:31 -07001886static void fcoe_dev_setup(void)
Robert Love85b4aa42008-12-09 15:10:24 -08001887{
john fastabend6f6c2aa2011-11-18 13:35:56 -08001888 register_dcbevent_notifier(&dcb_notifier);
Robert Love85b4aa42008-12-09 15:10:24 -08001889 register_netdevice_notifier(&fcoe_notifier);
1890}
1891
1892/**
Robert Love1875f272009-11-03 11:47:50 -08001893 * fcoe_dev_cleanup() - Cleanup the link change notification interface
Robert Love34f42a02009-02-27 10:55:45 -08001894 */
Robert Love85b4aa42008-12-09 15:10:24 -08001895static void fcoe_dev_cleanup(void)
1896{
john fastabend6f6c2aa2011-11-18 13:35:56 -08001897 unregister_dcbevent_notifier(&dcb_notifier);
Robert Love85b4aa42008-12-09 15:10:24 -08001898 unregister_netdevice_notifier(&fcoe_notifier);
1899}
1900
john fastabend6f6c2aa2011-11-18 13:35:56 -08001901static struct fcoe_interface *
1902fcoe_hostlist_lookup_realdev_port(struct net_device *netdev)
1903{
1904 struct fcoe_interface *fcoe;
1905 struct net_device *real_dev;
1906
1907 list_for_each_entry(fcoe, &fcoe_hostlist, list) {
1908 if (fcoe->netdev->priv_flags & IFF_802_1Q_VLAN)
1909 real_dev = vlan_dev_real_dev(fcoe->netdev);
1910 else
1911 real_dev = fcoe->netdev;
1912
1913 if (netdev == real_dev)
1914 return fcoe;
1915 }
1916 return NULL;
1917}
1918
1919static int fcoe_dcb_app_notification(struct notifier_block *notifier,
1920 ulong event, void *ptr)
1921{
1922 struct dcb_app_type *entry = ptr;
Robert Love619fe4b2012-05-22 19:06:10 -07001923 struct fcoe_ctlr *ctlr;
john fastabend6f6c2aa2011-11-18 13:35:56 -08001924 struct fcoe_interface *fcoe;
1925 struct net_device *netdev;
john fastabend6f6c2aa2011-11-18 13:35:56 -08001926 int prio;
1927
1928 if (entry->app.selector != DCB_APP_IDTYPE_ETHTYPE)
1929 return NOTIFY_OK;
1930
1931 netdev = dev_get_by_index(&init_net, entry->ifindex);
1932 if (!netdev)
1933 return NOTIFY_OK;
1934
1935 fcoe = fcoe_hostlist_lookup_realdev_port(netdev);
1936 dev_put(netdev);
1937 if (!fcoe)
1938 return NOTIFY_OK;
1939
Robert Love619fe4b2012-05-22 19:06:10 -07001940 ctlr = fcoe_to_ctlr(fcoe);
1941
john fastabend6f6c2aa2011-11-18 13:35:56 -08001942 if (entry->dcbx & DCB_CAP_DCBX_VER_CEE)
1943 prio = ffs(entry->app.priority) - 1;
1944 else
1945 prio = entry->app.priority;
1946
1947 if (prio < 0)
1948 return NOTIFY_OK;
1949
1950 if (entry->app.protocol == ETH_P_FIP ||
1951 entry->app.protocol == ETH_P_FCOE)
Robert Love619fe4b2012-05-22 19:06:10 -07001952 ctlr->priority = prio;
john fastabend6f6c2aa2011-11-18 13:35:56 -08001953
Neerav Parikh31c37a62012-09-24 11:52:45 -07001954 if (entry->app.protocol == ETH_P_FCOE)
1955 fcoe->priority = prio;
john fastabend6f6c2aa2011-11-18 13:35:56 -08001956
1957 return NOTIFY_OK;
1958}
1959
Robert Love85b4aa42008-12-09 15:10:24 -08001960/**
Robert Love1875f272009-11-03 11:47:50 -08001961 * fcoe_device_notification() - Handler for net device events
1962 * @notifier: The context of the notification
1963 * @event: The type of event
1964 * @ptr: The net device that the event was on
Robert Love85b4aa42008-12-09 15:10:24 -08001965 *
Robert Love1875f272009-11-03 11:47:50 -08001966 * This function is called by the Ethernet driver in case of link change event.
Robert Love85b4aa42008-12-09 15:10:24 -08001967 *
1968 * Returns: 0 for success
Robert Love34f42a02009-02-27 10:55:45 -08001969 */
Robert Love85b4aa42008-12-09 15:10:24 -08001970static int fcoe_device_notification(struct notifier_block *notifier,
1971 ulong event, void *ptr)
1972{
Robert Love435c8662012-11-27 06:53:35 +00001973 struct fcoe_ctlr_device *cdev;
Robert Love1875f272009-11-03 11:47:50 -08001974 struct fc_lport *lport = NULL;
Vasu Dev1d1b88d2009-07-29 17:05:45 -07001975 struct net_device *netdev = ptr;
Robert Love619fe4b2012-05-22 19:06:10 -07001976 struct fcoe_ctlr *ctlr;
Chris Leech014f5c32009-08-25 13:59:30 -07001977 struct fcoe_interface *fcoe;
Chris Leech2e70e242009-08-25 14:00:23 -07001978 struct fcoe_port *port;
Vasu Dev1bd49b42012-05-25 10:26:43 -07001979 struct fc_stats *stats;
Joe Eykholt97c83892009-03-17 11:42:40 -07001980 u32 link_possible = 1;
Robert Love85b4aa42008-12-09 15:10:24 -08001981 u32 mfs;
1982 int rc = NOTIFY_OK;
1983
Chris Leech014f5c32009-08-25 13:59:30 -07001984 list_for_each_entry(fcoe, &fcoe_hostlist, list) {
Chris Leech25024982009-08-25 13:59:35 -07001985 if (fcoe->netdev == netdev) {
Robert Love619fe4b2012-05-22 19:06:10 -07001986 ctlr = fcoe_to_ctlr(fcoe);
1987 lport = ctlr->lp;
Robert Love85b4aa42008-12-09 15:10:24 -08001988 break;
1989 }
1990 }
Robert Love1875f272009-11-03 11:47:50 -08001991 if (!lport) {
Robert Love85b4aa42008-12-09 15:10:24 -08001992 rc = NOTIFY_DONE;
1993 goto out;
1994 }
1995
Robert Love85b4aa42008-12-09 15:10:24 -08001996 switch (event) {
1997 case NETDEV_DOWN:
1998 case NETDEV_GOING_DOWN:
Joe Eykholt97c83892009-03-17 11:42:40 -07001999 link_possible = 0;
Robert Love85b4aa42008-12-09 15:10:24 -08002000 break;
2001 case NETDEV_UP:
2002 case NETDEV_CHANGE:
Robert Love85b4aa42008-12-09 15:10:24 -08002003 break;
2004 case NETDEV_CHANGEMTU:
Yi Zou7221d7e2009-10-21 16:27:52 -07002005 if (netdev->features & NETIF_F_FCOE_MTU)
2006 break;
Vasu Dev1d1b88d2009-07-29 17:05:45 -07002007 mfs = netdev->mtu - (sizeof(struct fcoe_hdr) +
2008 sizeof(struct fcoe_crc_eof));
Robert Love85b4aa42008-12-09 15:10:24 -08002009 if (mfs >= FC_MIN_MAX_FRAME)
Robert Love1875f272009-11-03 11:47:50 -08002010 fc_set_mfs(lport, mfs);
Robert Love85b4aa42008-12-09 15:10:24 -08002011 break;
2012 case NETDEV_REGISTER:
2013 break;
Chris Leech2e70e242009-08-25 14:00:23 -07002014 case NETDEV_UNREGISTER:
2015 list_del(&fcoe->list);
Robert Love619fe4b2012-05-22 19:06:10 -07002016 port = lport_priv(ctlr->lp);
Tejun Heo2ca32b42011-01-28 16:05:32 -08002017 queue_work(fcoe_wq, &port->destroy_work);
Chris Leech2e70e242009-08-25 14:00:23 -07002018 goto out;
2019 break;
Yi Zou54a5b212010-07-20 15:21:17 -07002020 case NETDEV_FEAT_CHANGE:
2021 fcoe_netdev_features_change(lport, netdev);
2022 break;
Robert Love85b4aa42008-12-09 15:10:24 -08002023 default:
Vasu Dev1d1b88d2009-07-29 17:05:45 -07002024 FCOE_NETDEV_DBG(netdev, "Unknown event %ld "
Robert Loved5488eb2009-06-10 15:30:59 -07002025 "from netdev netlink\n", event);
Robert Love85b4aa42008-12-09 15:10:24 -08002026 }
Robert Love5e4f8fe2010-05-07 15:18:35 -07002027
2028 fcoe_link_speed_update(lport);
2029
Robert Love435c8662012-11-27 06:53:35 +00002030 cdev = fcoe_ctlr_to_ctlr_dev(ctlr);
2031
2032 if (link_possible && !fcoe_link_ok(lport)) {
2033 switch (cdev->enabled) {
2034 case FCOE_CTLR_DISABLED:
2035 pr_info("Link up while interface is disabled.\n");
2036 break;
2037 case FCOE_CTLR_ENABLED:
2038 case FCOE_CTLR_UNUSED:
2039 fcoe_ctlr_link_up(ctlr);
2040 };
2041 } else if (fcoe_ctlr_link_down(ctlr)) {
2042 switch (cdev->enabled) {
2043 case FCOE_CTLR_DISABLED:
2044 pr_info("Link down while interface is disabled.\n");
2045 break;
2046 case FCOE_CTLR_ENABLED:
2047 case FCOE_CTLR_UNUSED:
2048 stats = per_cpu_ptr(lport->stats, get_cpu());
2049 stats->LinkFailureCount++;
2050 put_cpu();
2051 fcoe_clean_pending_queue(lport);
2052 };
Robert Love85b4aa42008-12-09 15:10:24 -08002053 }
2054out:
2055 return rc;
2056}
2057
2058/**
Vasu Dev55a66d32009-12-10 09:59:31 -08002059 * fcoe_disable() - Disables a FCoE interface
Yi Zou78a58242011-01-28 16:05:16 -08002060 * @netdev : The net_device object the Ethernet interface to create on
Vasu Dev55a66d32009-12-10 09:59:31 -08002061 *
Yi Zou78a58242011-01-28 16:05:16 -08002062 * Called from fcoe transport.
Vasu Dev55a66d32009-12-10 09:59:31 -08002063 *
2064 * Returns: 0 for success
Robert Love435c8662012-11-27 06:53:35 +00002065 *
2066 * Deprecated: use fcoe_ctlr_enabled()
Vasu Dev55a66d32009-12-10 09:59:31 -08002067 */
Yi Zou78a58242011-01-28 16:05:16 -08002068static int fcoe_disable(struct net_device *netdev)
Vasu Dev55a66d32009-12-10 09:59:31 -08002069{
Robert Love619fe4b2012-05-22 19:06:10 -07002070 struct fcoe_ctlr *ctlr;
Vasu Dev55a66d32009-12-10 09:59:31 -08002071 struct fcoe_interface *fcoe;
Vasu Dev55a66d32009-12-10 09:59:31 -08002072 int rc = 0;
2073
2074 mutex_lock(&fcoe_config_mutex);
Vasu Dev55a66d32009-12-10 09:59:31 -08002075
Robert Loveee5df622011-04-01 16:05:59 -07002076 rtnl_lock();
Vasu Dev55a66d32009-12-10 09:59:31 -08002077 fcoe = fcoe_hostlist_lookup_port(netdev);
2078 rtnl_unlock();
2079
Chris Leech9ee50e42010-04-09 14:22:23 -07002080 if (fcoe) {
Robert Love619fe4b2012-05-22 19:06:10 -07002081 ctlr = fcoe_to_ctlr(fcoe);
2082 fcoe_ctlr_link_down(ctlr);
2083 fcoe_clean_pending_queue(ctlr->lp);
Chris Leech9ee50e42010-04-09 14:22:23 -07002084 } else
Vasu Dev55a66d32009-12-10 09:59:31 -08002085 rc = -ENODEV;
2086
Vasu Dev55a66d32009-12-10 09:59:31 -08002087 mutex_unlock(&fcoe_config_mutex);
2088 return rc;
2089}
2090
2091/**
2092 * fcoe_enable() - Enables a FCoE interface
Yi Zou78a58242011-01-28 16:05:16 -08002093 * @netdev : The net_device object the Ethernet interface to create on
Vasu Dev55a66d32009-12-10 09:59:31 -08002094 *
Yi Zou78a58242011-01-28 16:05:16 -08002095 * Called from fcoe transport.
Vasu Dev55a66d32009-12-10 09:59:31 -08002096 *
2097 * Returns: 0 for success
2098 */
Yi Zou78a58242011-01-28 16:05:16 -08002099static int fcoe_enable(struct net_device *netdev)
Vasu Dev55a66d32009-12-10 09:59:31 -08002100{
Robert Love619fe4b2012-05-22 19:06:10 -07002101 struct fcoe_ctlr *ctlr;
Vasu Dev55a66d32009-12-10 09:59:31 -08002102 struct fcoe_interface *fcoe;
Vasu Dev55a66d32009-12-10 09:59:31 -08002103 int rc = 0;
2104
2105 mutex_lock(&fcoe_config_mutex);
Robert Loveee5df622011-04-01 16:05:59 -07002106 rtnl_lock();
Vasu Dev55a66d32009-12-10 09:59:31 -08002107 fcoe = fcoe_hostlist_lookup_port(netdev);
2108 rtnl_unlock();
2109
Robert Love619fe4b2012-05-22 19:06:10 -07002110 if (!fcoe) {
Vasu Dev55a66d32009-12-10 09:59:31 -08002111 rc = -ENODEV;
Robert Love619fe4b2012-05-22 19:06:10 -07002112 goto out;
2113 }
Vasu Dev55a66d32009-12-10 09:59:31 -08002114
Robert Love619fe4b2012-05-22 19:06:10 -07002115 ctlr = fcoe_to_ctlr(fcoe);
2116
2117 if (!fcoe_link_ok(ctlr->lp))
2118 fcoe_ctlr_link_up(ctlr);
2119
2120out:
Vasu Dev55a66d32009-12-10 09:59:31 -08002121 mutex_unlock(&fcoe_config_mutex);
2122 return rc;
2123}
2124
2125/**
Robert Love435c8662012-11-27 06:53:35 +00002126 * fcoe_ctlr_enabled() - Enable or disable an FCoE Controller
2127 * @cdev: The FCoE Controller that is being enabled or disabled
2128 *
2129 * fcoe_sysfs will ensure that the state of 'enabled' has
2130 * changed, so no checking is necessary here. This routine simply
2131 * calls fcoe_enable or fcoe_disable, both of which are deprecated.
2132 * When those routines are removed the functionality can be merged
2133 * here.
2134 */
2135static int fcoe_ctlr_enabled(struct fcoe_ctlr_device *cdev)
2136{
2137 struct fcoe_ctlr *ctlr = fcoe_ctlr_device_priv(cdev);
2138 struct fc_lport *lport = ctlr->lp;
2139 struct net_device *netdev = fcoe_netdev(lport);
2140
2141 switch (cdev->enabled) {
2142 case FCOE_CTLR_ENABLED:
2143 return fcoe_enable(netdev);
2144 case FCOE_CTLR_DISABLED:
2145 return fcoe_disable(netdev);
2146 case FCOE_CTLR_UNUSED:
2147 default:
2148 return -ENOTSUPP;
2149 };
2150}
2151
2152/**
Robert Love1875f272009-11-03 11:47:50 -08002153 * fcoe_destroy() - Destroy a FCoE interface
Yi Zou78a58242011-01-28 16:05:16 -08002154 * @netdev : The net_device object the Ethernet interface to create on
Robert Love1875f272009-11-03 11:47:50 -08002155 *
Yi Zou78a58242011-01-28 16:05:16 -08002156 * Called from fcoe transport
Robert Love85b4aa42008-12-09 15:10:24 -08002157 *
2158 * Returns: 0 for success
Robert Love34f42a02009-02-27 10:55:45 -08002159 */
Yi Zou78a58242011-01-28 16:05:16 -08002160static int fcoe_destroy(struct net_device *netdev)
Robert Love85b4aa42008-12-09 15:10:24 -08002161{
Robert Love619fe4b2012-05-22 19:06:10 -07002162 struct fcoe_ctlr *ctlr;
Chris Leech030f4e02009-08-25 14:00:02 -07002163 struct fcoe_interface *fcoe;
Vasu Devf04ca1b2011-04-01 16:06:45 -07002164 struct fc_lport *lport;
Neerav Parikhb2085a42011-06-20 16:59:51 -07002165 struct fcoe_port *port;
Mike Christie8eca3552009-10-21 16:27:44 -07002166 int rc = 0;
Robert Love85b4aa42008-12-09 15:10:24 -08002167
Chris Leechdfc1d0f2009-08-25 14:00:13 -07002168 mutex_lock(&fcoe_config_mutex);
Robert Loveee5df622011-04-01 16:05:59 -07002169 rtnl_lock();
Chris Leech2e70e242009-08-25 14:00:23 -07002170 fcoe = fcoe_hostlist_lookup_port(netdev);
2171 if (!fcoe) {
Robert Love85b4aa42008-12-09 15:10:24 -08002172 rc = -ENODEV;
Yi Zou78a58242011-01-28 16:05:16 -08002173 goto out_nodev;
Robert Love85b4aa42008-12-09 15:10:24 -08002174 }
Robert Love619fe4b2012-05-22 19:06:10 -07002175 ctlr = fcoe_to_ctlr(fcoe);
2176 lport = ctlr->lp;
Neerav Parikhb2085a42011-06-20 16:59:51 -07002177 port = lport_priv(lport);
Yi Zou54a5b212010-07-20 15:21:17 -07002178 list_del(&fcoe->list);
Neerav Parikhb2085a42011-06-20 16:59:51 -07002179 queue_work(fcoe_wq, &port->destroy_work);
Robert Love85b4aa42008-12-09 15:10:24 -08002180out_nodev:
Neerav Parikhb2085a42011-06-20 16:59:51 -07002181 rtnl_unlock();
Chris Leechdfc1d0f2009-08-25 14:00:13 -07002182 mutex_unlock(&fcoe_config_mutex);
Robert Love85b4aa42008-12-09 15:10:24 -08002183 return rc;
2184}
2185
Robert Love1875f272009-11-03 11:47:50 -08002186/**
2187 * fcoe_destroy_work() - Destroy a FCoE port in a deferred work context
2188 * @work: Handle to the FCoE port to be destroyed
2189 */
Chris Leech2e70e242009-08-25 14:00:23 -07002190static void fcoe_destroy_work(struct work_struct *work)
2191{
2192 struct fcoe_port *port;
Neerav Parikhb2085a42011-06-20 16:59:51 -07002193 struct fcoe_interface *fcoe;
Chris Leech2e70e242009-08-25 14:00:23 -07002194
2195 port = container_of(work, struct fcoe_port, destroy_work);
2196 mutex_lock(&fcoe_config_mutex);
Neerav Parikhb2085a42011-06-20 16:59:51 -07002197
Neerav Parikhb2085a42011-06-20 16:59:51 -07002198 fcoe = port->priv;
Chris Leech2e70e242009-08-25 14:00:23 -07002199 fcoe_if_destroy(port->lport);
Robert Loveccefd232012-02-10 17:18:41 -08002200 fcoe_interface_cleanup(fcoe);
Neerav Parikhb2085a42011-06-20 16:59:51 -07002201
Chris Leech2e70e242009-08-25 14:00:23 -07002202 mutex_unlock(&fcoe_config_mutex);
2203}
2204
Robert Love85b4aa42008-12-09 15:10:24 -08002205/**
Yi Zou78a58242011-01-28 16:05:16 -08002206 * fcoe_match() - Check if the FCoE is supported on the given netdevice
2207 * @netdev : The net_device object the Ethernet interface to create on
Robert Love1875f272009-11-03 11:47:50 -08002208 *
Yi Zou78a58242011-01-28 16:05:16 -08002209 * Called from fcoe transport.
2210 *
2211 * Returns: always returns true as this is the default FCoE transport,
2212 * i.e., support all netdevs.
2213 */
2214static bool fcoe_match(struct net_device *netdev)
2215{
2216 return true;
2217}
2218
2219/**
john fastabend6f6c2aa2011-11-18 13:35:56 -08002220 * fcoe_dcb_create() - Initialize DCB attributes and hooks
2221 * @netdev: The net_device object of the L2 link that should be queried
2222 * @port: The fcoe_port to bind FCoE APP priority with
2223 * @
2224 */
2225static void fcoe_dcb_create(struct fcoe_interface *fcoe)
2226{
2227#ifdef CONFIG_DCB
2228 int dcbx;
2229 u8 fup, up;
2230 struct net_device *netdev = fcoe->realdev;
Robert Love619fe4b2012-05-22 19:06:10 -07002231 struct fcoe_ctlr *ctlr = fcoe_to_ctlr(fcoe);
john fastabend6f6c2aa2011-11-18 13:35:56 -08002232 struct dcb_app app = {
2233 .priority = 0,
2234 .protocol = ETH_P_FCOE
2235 };
2236
2237 /* setup DCB priority attributes. */
2238 if (netdev && netdev->dcbnl_ops && netdev->dcbnl_ops->getdcbx) {
2239 dcbx = netdev->dcbnl_ops->getdcbx(netdev);
2240
2241 if (dcbx & DCB_CAP_DCBX_VER_IEEE) {
2242 app.selector = IEEE_8021QAZ_APP_SEL_ETHERTYPE;
2243 up = dcb_ieee_getapp_mask(netdev, &app);
2244 app.protocol = ETH_P_FIP;
2245 fup = dcb_ieee_getapp_mask(netdev, &app);
2246 } else {
2247 app.selector = DCB_APP_IDTYPE_ETHTYPE;
2248 up = dcb_getapp(netdev, &app);
2249 app.protocol = ETH_P_FIP;
2250 fup = dcb_getapp(netdev, &app);
2251 }
2252
Neerav Parikh31c37a62012-09-24 11:52:45 -07002253 fcoe->priority = ffs(up) ? ffs(up) - 1 : 0;
2254 ctlr->priority = ffs(fup) ? ffs(fup) - 1 : fcoe->priority;
john fastabend6f6c2aa2011-11-18 13:35:56 -08002255 }
2256#endif
2257}
2258
Robert Love435c8662012-11-27 06:53:35 +00002259enum fcoe_create_link_state {
2260 FCOE_CREATE_LINK_DOWN,
2261 FCOE_CREATE_LINK_UP,
2262};
2263
john fastabend6f6c2aa2011-11-18 13:35:56 -08002264/**
Robert Love435c8662012-11-27 06:53:35 +00002265 * _fcoe_create() - (internal) Create a fcoe interface
2266 * @netdev : The net_device object the Ethernet interface to create on
2267 * @fip_mode: The FIP mode for this creation
2268 * @link_state: The ctlr link state on creation
Yi Zou78a58242011-01-28 16:05:16 -08002269 *
Robert Love435c8662012-11-27 06:53:35 +00002270 * Called from either the libfcoe 'create' module parameter
2271 * via fcoe_create or from fcoe_syfs's ctlr_create file.
Robert Love85b4aa42008-12-09 15:10:24 -08002272 *
Robert Love435c8662012-11-27 06:53:35 +00002273 * libfcoe's 'create' module parameter is deprecated so some
2274 * consolidation of code can be done when that interface is
2275 * removed.
Robert Love34f42a02009-02-27 10:55:45 -08002276 */
Robert Love435c8662012-11-27 06:53:35 +00002277static int _fcoe_create(struct net_device *netdev, enum fip_state fip_mode,
2278 enum fcoe_create_link_state link_state)
Robert Love85b4aa42008-12-09 15:10:24 -08002279{
Neerav Parikhb2085a42011-06-20 16:59:51 -07002280 int rc = 0;
Robert Love8d55e502012-05-22 19:06:26 -07002281 struct fcoe_ctlr_device *ctlr_dev;
Robert Love619fe4b2012-05-22 19:06:10 -07002282 struct fcoe_ctlr *ctlr;
Chris Leech030f4e02009-08-25 14:00:02 -07002283 struct fcoe_interface *fcoe;
Chris Leechaf7f85d2009-08-25 13:59:24 -07002284 struct fc_lport *lport;
Robert Love85b4aa42008-12-09 15:10:24 -08002285
Chris Leechdfc1d0f2009-08-25 14:00:13 -07002286 mutex_lock(&fcoe_config_mutex);
Robert Loveee5df622011-04-01 16:05:59 -07002287 rtnl_lock();
Vasu Dev34ce27b2010-05-07 15:18:46 -07002288
Robert Love85b4aa42008-12-09 15:10:24 -08002289 /* look for existing lport */
2290 if (fcoe_hostlist_lookup(netdev)) {
2291 rc = -EEXIST;
Yi Zou78a58242011-01-28 16:05:16 -08002292 goto out_nodev;
Robert Love85b4aa42008-12-09 15:10:24 -08002293 }
Robert Love85b4aa42008-12-09 15:10:24 -08002294
Joe Eykholt1dd454d2010-07-20 15:20:46 -07002295 fcoe = fcoe_interface_create(netdev, fip_mode);
Robert Love7287fb92011-01-28 16:03:47 -08002296 if (IS_ERR(fcoe)) {
2297 rc = PTR_ERR(fcoe);
Yi Zou78a58242011-01-28 16:05:16 -08002298 goto out_nodev;
Chris Leech030f4e02009-08-25 14:00:02 -07002299 }
2300
Robert Love619fe4b2012-05-22 19:06:10 -07002301 ctlr = fcoe_to_ctlr(fcoe);
Robert Love8d55e502012-05-22 19:06:26 -07002302 ctlr_dev = fcoe_ctlr_to_ctlr_dev(ctlr);
2303 lport = fcoe_if_create(fcoe, &ctlr_dev->dev, 0);
Chris Leechaf7f85d2009-08-25 13:59:24 -07002304 if (IS_ERR(lport)) {
Robert Loved5488eb2009-06-10 15:30:59 -07002305 printk(KERN_ERR "fcoe: Failed to create interface (%s)\n",
Robert Love85b4aa42008-12-09 15:10:24 -08002306 netdev->name);
Robert Love85b4aa42008-12-09 15:10:24 -08002307 rc = -EIO;
Robert Love848e7d52011-08-25 12:40:47 -07002308 rtnl_unlock();
Chris Leech2e70e242009-08-25 14:00:23 -07002309 fcoe_interface_cleanup(fcoe);
Robert Love848e7d52011-08-25 12:40:47 -07002310 goto out_nortnl;
Robert Love85b4aa42008-12-09 15:10:24 -08002311 }
Chris Leech030f4e02009-08-25 14:00:02 -07002312
Chris Leech54b649f2009-08-25 14:00:07 -07002313 /* Make this the "master" N_Port */
Robert Love619fe4b2012-05-22 19:06:10 -07002314 ctlr->lp = lport;
Chris Leech030f4e02009-08-25 14:00:02 -07002315
john fastabend6f6c2aa2011-11-18 13:35:56 -08002316 /* setup DCB priority attributes. */
2317 fcoe_dcb_create(fcoe);
2318
Chris Leechc863df32009-08-25 14:00:18 -07002319 /* add to lports list */
2320 fcoe_hostlist_add(lport);
2321
Chris Leech54b649f2009-08-25 14:00:07 -07002322 /* start FIP Discovery and FLOGI */
2323 lport->boot_time = jiffies;
2324 fc_fabric_login(lport);
Robert Love435c8662012-11-27 06:53:35 +00002325
2326 /*
2327 * If the fcoe_ctlr_device is to be set to DISABLED
2328 * it must be done after the lport is added to the
2329 * hostlist, but before the rtnl_lock is released.
2330 * This is because the rtnl_lock protects the
2331 * hostlist that fcoe_device_notification uses. If
2332 * the FCoE Controller is intended to be created
2333 * DISABLED then 'enabled' needs to be considered
2334 * handling link events. 'enabled' must be set
2335 * before the lport can be found in the hostlist
2336 * when a link up event is received.
2337 */
2338 if (link_state == FCOE_CREATE_LINK_UP)
2339 ctlr_dev->enabled = FCOE_CTLR_ENABLED;
2340 else
2341 ctlr_dev->enabled = FCOE_CTLR_DISABLED;
2342
2343 if (link_state == FCOE_CREATE_LINK_UP &&
2344 !fcoe_link_ok(lport)) {
Robert Love22805122012-03-13 18:22:12 -07002345 rtnl_unlock();
Robert Love619fe4b2012-05-22 19:06:10 -07002346 fcoe_ctlr_link_up(ctlr);
Robert Love22805122012-03-13 18:22:12 -07002347 mutex_unlock(&fcoe_config_mutex);
2348 return rc;
2349 }
Chris Leech54b649f2009-08-25 14:00:07 -07002350
Robert Love85b4aa42008-12-09 15:10:24 -08002351out_nodev:
Vasu Dev34ce27b2010-05-07 15:18:46 -07002352 rtnl_unlock();
Robert Love848e7d52011-08-25 12:40:47 -07002353out_nortnl:
Chris Leechdfc1d0f2009-08-25 14:00:13 -07002354 mutex_unlock(&fcoe_config_mutex);
Robert Love85b4aa42008-12-09 15:10:24 -08002355 return rc;
2356}
2357
Robert Love34f42a02009-02-27 10:55:45 -08002358/**
Robert Love435c8662012-11-27 06:53:35 +00002359 * fcoe_create() - Create a fcoe interface
2360 * @netdev : The net_device object the Ethernet interface to create on
2361 * @fip_mode: The FIP mode for this creation
2362 *
2363 * Called from fcoe transport
2364 *
2365 * Returns: 0 for success
2366 */
2367static int fcoe_create(struct net_device *netdev, enum fip_state fip_mode)
2368{
2369 return _fcoe_create(netdev, fip_mode, FCOE_CREATE_LINK_UP);
2370}
2371
2372/**
2373 * fcoe_ctlr_alloc() - Allocate a fcoe interface from fcoe_sysfs
2374 * @netdev: The net_device to be used by the allocated FCoE Controller
2375 *
2376 * This routine is called from fcoe_sysfs. It will start the fcoe_ctlr
2377 * in a link_down state. The allows the user an opportunity to configure
2378 * the FCoE Controller from sysfs before enabling the FCoE Controller.
2379 *
2380 * Creating in with this routine starts the FCoE Controller in Fabric
2381 * mode. The user can change to VN2VN or another mode before enabling.
2382 */
2383static int fcoe_ctlr_alloc(struct net_device *netdev)
2384{
2385 return _fcoe_create(netdev, FIP_MODE_FABRIC,
2386 FCOE_CREATE_LINK_DOWN);
2387}
2388
2389/**
Robert Love5e4f8fe2010-05-07 15:18:35 -07002390 * fcoe_link_speed_update() - Update the supported and actual link speeds
2391 * @lport: The local port to update speeds for
Robert Love85b4aa42008-12-09 15:10:24 -08002392 *
Robert Love5e4f8fe2010-05-07 15:18:35 -07002393 * Returns: 0 if the ethtool query was successful
2394 * -1 if the ethtool query failed
Robert Love85b4aa42008-12-09 15:10:24 -08002395 */
Bart Van Assche7c9c6842012-01-13 17:26:25 -08002396static int fcoe_link_speed_update(struct fc_lport *lport)
Robert Love85b4aa42008-12-09 15:10:24 -08002397{
Bhanu Prakash Gollapudi8597ae82011-01-28 16:05:37 -08002398 struct net_device *netdev = fcoe_netdev(lport);
David Decotigny8ae6dac2011-04-27 18:32:38 +00002399 struct ethtool_cmd ecmd;
Robert Love85b4aa42008-12-09 15:10:24 -08002400
Jiri Pirko4bc71cb2011-09-03 03:34:30 +00002401 if (!__ethtool_get_settings(netdev, &ecmd)) {
Robert Love1875f272009-11-03 11:47:50 -08002402 lport->link_supported_speeds &=
Yi Zou2f718d62009-07-29 17:04:01 -07002403 ~(FC_PORTSPEED_1GBIT | FC_PORTSPEED_10GBIT);
2404 if (ecmd.supported & (SUPPORTED_1000baseT_Half |
2405 SUPPORTED_1000baseT_Full))
Robert Love1875f272009-11-03 11:47:50 -08002406 lport->link_supported_speeds |= FC_PORTSPEED_1GBIT;
Yi Zou2f718d62009-07-29 17:04:01 -07002407 if (ecmd.supported & SUPPORTED_10000baseT_Full)
Robert Love1875f272009-11-03 11:47:50 -08002408 lport->link_supported_speeds |=
Yi Zou2f718d62009-07-29 17:04:01 -07002409 FC_PORTSPEED_10GBIT;
David Decotigny8ae6dac2011-04-27 18:32:38 +00002410 switch (ethtool_cmd_speed(&ecmd)) {
2411 case SPEED_1000:
Robert Love1875f272009-11-03 11:47:50 -08002412 lport->link_speed = FC_PORTSPEED_1GBIT;
David Decotigny8ae6dac2011-04-27 18:32:38 +00002413 break;
2414 case SPEED_10000:
Robert Love1875f272009-11-03 11:47:50 -08002415 lport->link_speed = FC_PORTSPEED_10GBIT;
David Decotigny8ae6dac2011-04-27 18:32:38 +00002416 break;
2417 }
Yi Zou2f718d62009-07-29 17:04:01 -07002418 return 0;
2419 }
2420 return -1;
Robert Love85b4aa42008-12-09 15:10:24 -08002421}
Robert Love85b4aa42008-12-09 15:10:24 -08002422
Robert Love34f42a02009-02-27 10:55:45 -08002423/**
Robert Love5e4f8fe2010-05-07 15:18:35 -07002424 * fcoe_link_ok() - Check if the link is OK for a local port
2425 * @lport: The local port to check link on
2426 *
2427 * Returns: 0 if link is UP and OK, -1 if not
2428 *
2429 */
Bart Van Assche7c9c6842012-01-13 17:26:25 -08002430static int fcoe_link_ok(struct fc_lport *lport)
Robert Love5e4f8fe2010-05-07 15:18:35 -07002431{
Bhanu Prakash Gollapudi8597ae82011-01-28 16:05:37 -08002432 struct net_device *netdev = fcoe_netdev(lport);
Robert Love5e4f8fe2010-05-07 15:18:35 -07002433
2434 if (netif_oper_up(netdev))
2435 return 0;
2436 return -1;
2437}
2438
2439/**
Robert Love1875f272009-11-03 11:47:50 -08002440 * fcoe_percpu_clean() - Clear all pending skbs for an local port
2441 * @lport: The local port whose skbs are to be cleared
Joe Eykholte7a51992009-08-25 14:04:08 -07002442 *
2443 * Must be called with fcoe_create_mutex held to single-thread completion.
2444 *
2445 * This flushes the pending skbs by adding a new skb to each queue and
2446 * waiting until they are all freed. This assures us that not only are
2447 * there no packets that will be handled by the lport, but also that any
2448 * threads already handling packet have returned.
Robert Love85b4aa42008-12-09 15:10:24 -08002449 */
Bart Van Assche7c9c6842012-01-13 17:26:25 -08002450static void fcoe_percpu_clean(struct fc_lport *lport)
Robert Love85b4aa42008-12-09 15:10:24 -08002451{
Robert Love85b4aa42008-12-09 15:10:24 -08002452 struct fcoe_percpu_s *pp;
Neil Hormandd060e72012-03-09 14:50:19 -08002453 struct sk_buff *skb;
Robert Love5e5e92d2009-03-17 11:41:35 -07002454 unsigned int cpu;
Robert Love85b4aa42008-12-09 15:10:24 -08002455
Robert Love5e5e92d2009-03-17 11:41:35 -07002456 for_each_possible_cpu(cpu) {
2457 pp = &per_cpu(fcoe_percpu, cpu);
Joe Eykholte7a51992009-08-25 14:04:08 -07002458
Neil Hormandd060e72012-03-09 14:50:19 -08002459 if (!pp->thread || !cpu_online(cpu))
Joe Eykholte7a51992009-08-25 14:04:08 -07002460 continue;
Joe Eykholte7a51992009-08-25 14:04:08 -07002461
2462 skb = dev_alloc_skb(0);
Dan Carpenterb3b8abd2012-04-20 12:16:54 -07002463 if (!skb)
Joe Eykholte7a51992009-08-25 14:04:08 -07002464 continue;
Dan Carpenterb3b8abd2012-04-20 12:16:54 -07002465
Joe Eykholte7a51992009-08-25 14:04:08 -07002466 skb->destructor = fcoe_percpu_flush_done;
2467
Neil Hormandd060e72012-03-09 14:50:19 -08002468 spin_lock_bh(&pp->fcoe_rx_list.lock);
Joe Eykholte7a51992009-08-25 14:04:08 -07002469 __skb_queue_tail(&pp->fcoe_rx_list, skb);
2470 if (pp->fcoe_rx_list.qlen == 1)
2471 wake_up_process(pp->thread);
Robert Love5e5e92d2009-03-17 11:41:35 -07002472 spin_unlock_bh(&pp->fcoe_rx_list.lock);
Joe Eykholte7a51992009-08-25 14:04:08 -07002473
2474 wait_for_completion(&fcoe_flush_completion);
Robert Love85b4aa42008-12-09 15:10:24 -08002475 }
2476}
Robert Love85b4aa42008-12-09 15:10:24 -08002477
2478/**
Robert Love1875f272009-11-03 11:47:50 -08002479 * fcoe_reset() - Reset a local port
2480 * @shost: The SCSI host associated with the local port to be reset
Robert Love85b4aa42008-12-09 15:10:24 -08002481 *
Robert Love1875f272009-11-03 11:47:50 -08002482 * Returns: Always 0 (return value required by FC transport template)
Robert Love85b4aa42008-12-09 15:10:24 -08002483 */
Bart Van Assche7c9c6842012-01-13 17:26:25 -08002484static int fcoe_reset(struct Scsi_Host *shost)
Robert Love85b4aa42008-12-09 15:10:24 -08002485{
2486 struct fc_lport *lport = shost_priv(shost);
Vasu Devd2f80952011-02-25 15:03:28 -08002487 struct fcoe_port *port = lport_priv(lport);
2488 struct fcoe_interface *fcoe = port->priv;
Robert Love619fe4b2012-05-22 19:06:10 -07002489 struct fcoe_ctlr *ctlr = fcoe_to_ctlr(fcoe);
Robert Love435c8662012-11-27 06:53:35 +00002490 struct fcoe_ctlr_device *cdev = fcoe_ctlr_to_ctlr_dev(ctlr);
Vasu Devd2f80952011-02-25 15:03:28 -08002491
Robert Love619fe4b2012-05-22 19:06:10 -07002492 fcoe_ctlr_link_down(ctlr);
2493 fcoe_clean_pending_queue(ctlr->lp);
Robert Love435c8662012-11-27 06:53:35 +00002494
2495 if (cdev->enabled != FCOE_CTLR_DISABLED &&
2496 !fcoe_link_ok(ctlr->lp))
Robert Love619fe4b2012-05-22 19:06:10 -07002497 fcoe_ctlr_link_up(ctlr);
Robert Love85b4aa42008-12-09 15:10:24 -08002498 return 0;
2499}
Robert Love85b4aa42008-12-09 15:10:24 -08002500
Robert Love34f42a02009-02-27 10:55:45 -08002501/**
Robert Love1875f272009-11-03 11:47:50 -08002502 * fcoe_hostlist_lookup_port() - Find the FCoE interface associated with a net device
2503 * @netdev: The net device used as a key
Robert Love85b4aa42008-12-09 15:10:24 -08002504 *
Robert Love1875f272009-11-03 11:47:50 -08002505 * Locking: Must be called with the RNL mutex held.
2506 *
2507 * Returns: NULL or the FCoE interface
Robert Love85b4aa42008-12-09 15:10:24 -08002508 */
Chris Leech014f5c32009-08-25 13:59:30 -07002509static struct fcoe_interface *
Robert Love1875f272009-11-03 11:47:50 -08002510fcoe_hostlist_lookup_port(const struct net_device *netdev)
Robert Love85b4aa42008-12-09 15:10:24 -08002511{
Chris Leech014f5c32009-08-25 13:59:30 -07002512 struct fcoe_interface *fcoe;
Robert Love85b4aa42008-12-09 15:10:24 -08002513
Chris Leech014f5c32009-08-25 13:59:30 -07002514 list_for_each_entry(fcoe, &fcoe_hostlist, list) {
Robert Love1875f272009-11-03 11:47:50 -08002515 if (fcoe->netdev == netdev)
Chris Leech014f5c32009-08-25 13:59:30 -07002516 return fcoe;
Robert Love85b4aa42008-12-09 15:10:24 -08002517 }
Robert Love85b4aa42008-12-09 15:10:24 -08002518 return NULL;
2519}
2520
Robert Love34f42a02009-02-27 10:55:45 -08002521/**
Robert Love1875f272009-11-03 11:47:50 -08002522 * fcoe_hostlist_lookup() - Find the local port associated with a
2523 * given net device
2524 * @netdev: The netdevice used as a key
Robert Love85b4aa42008-12-09 15:10:24 -08002525 *
Robert Love1875f272009-11-03 11:47:50 -08002526 * Locking: Must be called with the RTNL mutex held
2527 *
2528 * Returns: NULL or the local port
Robert Love85b4aa42008-12-09 15:10:24 -08002529 */
Chris Leech090eb6c2009-08-25 14:00:28 -07002530static struct fc_lport *fcoe_hostlist_lookup(const struct net_device *netdev)
Robert Love85b4aa42008-12-09 15:10:24 -08002531{
Robert Love619fe4b2012-05-22 19:06:10 -07002532 struct fcoe_ctlr *ctlr;
Chris Leech014f5c32009-08-25 13:59:30 -07002533 struct fcoe_interface *fcoe;
Robert Love85b4aa42008-12-09 15:10:24 -08002534
Chris Leech014f5c32009-08-25 13:59:30 -07002535 fcoe = fcoe_hostlist_lookup_port(netdev);
Robert Love619fe4b2012-05-22 19:06:10 -07002536 ctlr = fcoe_to_ctlr(fcoe);
2537 return (fcoe) ? ctlr->lp : NULL;
Robert Love85b4aa42008-12-09 15:10:24 -08002538}
Robert Love85b4aa42008-12-09 15:10:24 -08002539
Robert Love34f42a02009-02-27 10:55:45 -08002540/**
Robert Love1875f272009-11-03 11:47:50 -08002541 * fcoe_hostlist_add() - Add the FCoE interface identified by a local
2542 * port to the hostlist
2543 * @lport: The local port that identifies the FCoE interface to be added
2544 *
2545 * Locking: must be called with the RTNL mutex held
Robert Love85b4aa42008-12-09 15:10:24 -08002546 *
2547 * Returns: 0 for success
2548 */
Chris Leech090eb6c2009-08-25 14:00:28 -07002549static int fcoe_hostlist_add(const struct fc_lport *lport)
Robert Love85b4aa42008-12-09 15:10:24 -08002550{
Chris Leech014f5c32009-08-25 13:59:30 -07002551 struct fcoe_interface *fcoe;
2552 struct fcoe_port *port;
Robert Love85b4aa42008-12-09 15:10:24 -08002553
Chris Leech014f5c32009-08-25 13:59:30 -07002554 fcoe = fcoe_hostlist_lookup_port(fcoe_netdev(lport));
2555 if (!fcoe) {
2556 port = lport_priv(lport);
Bhanu Prakash Gollapudi8597ae82011-01-28 16:05:37 -08002557 fcoe = port->priv;
Chris Leech014f5c32009-08-25 13:59:30 -07002558 list_add_tail(&fcoe->list, &fcoe_hostlist);
Robert Love85b4aa42008-12-09 15:10:24 -08002559 }
2560 return 0;
2561}
Robert Love85b4aa42008-12-09 15:10:24 -08002562
Yi Zou78a58242011-01-28 16:05:16 -08002563
2564static struct fcoe_transport fcoe_sw_transport = {
2565 .name = {FCOE_TRANSPORT_DEFAULT},
2566 .attached = false,
2567 .list = LIST_HEAD_INIT(fcoe_sw_transport.list),
2568 .match = fcoe_match,
Robert Love435c8662012-11-27 06:53:35 +00002569 .alloc = fcoe_ctlr_alloc,
Yi Zou78a58242011-01-28 16:05:16 -08002570 .create = fcoe_create,
2571 .destroy = fcoe_destroy,
2572 .enable = fcoe_enable,
2573 .disable = fcoe_disable,
2574};
2575
Robert Love34f42a02009-02-27 10:55:45 -08002576/**
Robert Love1875f272009-11-03 11:47:50 -08002577 * fcoe_init() - Initialize fcoe.ko
Robert Love85b4aa42008-12-09 15:10:24 -08002578 *
Robert Love1875f272009-11-03 11:47:50 -08002579 * Returns: 0 on success, or a negative value on failure
Robert Love34f42a02009-02-27 10:55:45 -08002580 */
Robert Love85b4aa42008-12-09 15:10:24 -08002581static int __init fcoe_init(void)
2582{
Robert Love1875f272009-11-03 11:47:50 -08002583 struct fcoe_percpu_s *p;
Robert Love38eccab2009-03-17 11:41:30 -07002584 unsigned int cpu;
Robert Love8976f422009-03-17 11:41:46 -07002585 int rc = 0;
Robert Love85b4aa42008-12-09 15:10:24 -08002586
Tejun Heo2ca32b42011-01-28 16:05:32 -08002587 fcoe_wq = alloc_workqueue("fcoe", 0, 0);
2588 if (!fcoe_wq)
2589 return -ENOMEM;
2590
Yi Zou78a58242011-01-28 16:05:16 -08002591 /* register as a fcoe transport */
2592 rc = fcoe_transport_attach(&fcoe_sw_transport);
2593 if (rc) {
2594 printk(KERN_ERR "failed to register an fcoe transport, check "
2595 "if libfcoe is loaded\n");
2596 return rc;
2597 }
2598
Chris Leechdfc1d0f2009-08-25 14:00:13 -07002599 mutex_lock(&fcoe_config_mutex);
2600
Robert Love38eccab2009-03-17 11:41:30 -07002601 for_each_possible_cpu(cpu) {
Robert Love5e5e92d2009-03-17 11:41:35 -07002602 p = &per_cpu(fcoe_percpu, cpu);
Robert Love38eccab2009-03-17 11:41:30 -07002603 skb_queue_head_init(&p->fcoe_rx_list);
2604 }
2605
Robert Love8976f422009-03-17 11:41:46 -07002606 for_each_online_cpu(cpu)
2607 fcoe_percpu_thread_create(cpu);
Robert Love85b4aa42008-12-09 15:10:24 -08002608
Robert Love8976f422009-03-17 11:41:46 -07002609 /* Initialize per CPU interrupt thread */
2610 rc = register_hotcpu_notifier(&fcoe_cpu_notifier);
2611 if (rc)
2612 goto out_free;
Robert Love85b4aa42008-12-09 15:10:24 -08002613
Robert Love8976f422009-03-17 11:41:46 -07002614 /* Setup link change notification */
Robert Love85b4aa42008-12-09 15:10:24 -08002615 fcoe_dev_setup();
2616
Chris Leech5892c322009-08-25 13:59:14 -07002617 rc = fcoe_if_init();
2618 if (rc)
2619 goto out_free;
Robert Love85b4aa42008-12-09 15:10:24 -08002620
Chris Leechdfc1d0f2009-08-25 14:00:13 -07002621 mutex_unlock(&fcoe_config_mutex);
Robert Love85b4aa42008-12-09 15:10:24 -08002622 return 0;
Robert Love8976f422009-03-17 11:41:46 -07002623
2624out_free:
2625 for_each_online_cpu(cpu) {
2626 fcoe_percpu_thread_destroy(cpu);
2627 }
Chris Leechdfc1d0f2009-08-25 14:00:13 -07002628 mutex_unlock(&fcoe_config_mutex);
Tejun Heo2ca32b42011-01-28 16:05:32 -08002629 destroy_workqueue(fcoe_wq);
Robert Love8976f422009-03-17 11:41:46 -07002630 return rc;
Robert Love85b4aa42008-12-09 15:10:24 -08002631}
2632module_init(fcoe_init);
2633
2634/**
Robert Love1875f272009-11-03 11:47:50 -08002635 * fcoe_exit() - Clean up fcoe.ko
Robert Love85b4aa42008-12-09 15:10:24 -08002636 *
Robert Love1875f272009-11-03 11:47:50 -08002637 * Returns: 0 on success or a negative value on failure
Robert Love34f42a02009-02-27 10:55:45 -08002638 */
Robert Love85b4aa42008-12-09 15:10:24 -08002639static void __exit fcoe_exit(void)
2640{
Chris Leech014f5c32009-08-25 13:59:30 -07002641 struct fcoe_interface *fcoe, *tmp;
Robert Love619fe4b2012-05-22 19:06:10 -07002642 struct fcoe_ctlr *ctlr;
Chris Leech2e70e242009-08-25 14:00:23 -07002643 struct fcoe_port *port;
Robert Love1875f272009-11-03 11:47:50 -08002644 unsigned int cpu;
Robert Love85b4aa42008-12-09 15:10:24 -08002645
Chris Leechdfc1d0f2009-08-25 14:00:13 -07002646 mutex_lock(&fcoe_config_mutex);
2647
Robert Love85b4aa42008-12-09 15:10:24 -08002648 fcoe_dev_cleanup();
2649
Vasu Dev5919a592009-03-27 09:03:29 -07002650 /* releases the associated fcoe hosts */
Chris Leech090eb6c2009-08-25 14:00:28 -07002651 rtnl_lock();
2652 list_for_each_entry_safe(fcoe, tmp, &fcoe_hostlist, list) {
Chris Leechc863df32009-08-25 14:00:18 -07002653 list_del(&fcoe->list);
Robert Love619fe4b2012-05-22 19:06:10 -07002654 ctlr = fcoe_to_ctlr(fcoe);
2655 port = lport_priv(ctlr->lp);
Tejun Heo2ca32b42011-01-28 16:05:32 -08002656 queue_work(fcoe_wq, &port->destroy_work);
Chris Leechc863df32009-08-25 14:00:18 -07002657 }
Chris Leech090eb6c2009-08-25 14:00:28 -07002658 rtnl_unlock();
Robert Love85b4aa42008-12-09 15:10:24 -08002659
Robert Love8976f422009-03-17 11:41:46 -07002660 unregister_hotcpu_notifier(&fcoe_cpu_notifier);
2661
Chris Leech014f5c32009-08-25 13:59:30 -07002662 for_each_online_cpu(cpu)
Robert Love8976f422009-03-17 11:41:46 -07002663 fcoe_percpu_thread_destroy(cpu);
Robert Love85b4aa42008-12-09 15:10:24 -08002664
Chris Leechdfc1d0f2009-08-25 14:00:13 -07002665 mutex_unlock(&fcoe_config_mutex);
Chris Leech2e70e242009-08-25 14:00:23 -07002666
Tejun Heo2ca32b42011-01-28 16:05:32 -08002667 /*
2668 * destroy_work's may be chained but destroy_workqueue()
2669 * can take care of them. Just kill the fcoe_wq.
2670 */
2671 destroy_workqueue(fcoe_wq);
Chris Leech2e70e242009-08-25 14:00:23 -07002672
Tejun Heo2ca32b42011-01-28 16:05:32 -08002673 /*
2674 * Detaching from the scsi transport must happen after all
2675 * destroys are done on the fcoe_wq. destroy_workqueue will
2676 * enusre the fcoe_wq is flushed.
2677 */
Chris Leech2e70e242009-08-25 14:00:23 -07002678 fcoe_if_exit();
Yi Zou78a58242011-01-28 16:05:16 -08002679
2680 /* detach from fcoe transport */
2681 fcoe_transport_detach(&fcoe_sw_transport);
Robert Love85b4aa42008-12-09 15:10:24 -08002682}
2683module_exit(fcoe_exit);
Chris Leech11b56182009-11-03 11:46:29 -08002684
2685/**
2686 * fcoe_flogi_resp() - FCoE specific FLOGI and FDISC response handler
2687 * @seq: active sequence in the FLOGI or FDISC exchange
2688 * @fp: response frame, or error encoded in a pointer (timeout)
2689 * @arg: pointer the the fcoe_ctlr structure
2690 *
Uwe Kleine-König65155b32010-06-11 12:17:01 +02002691 * This handles MAC address management for FCoE, then passes control on to
Chris Leech11b56182009-11-03 11:46:29 -08002692 * the libfc FLOGI response handler.
2693 */
2694static void fcoe_flogi_resp(struct fc_seq *seq, struct fc_frame *fp, void *arg)
2695{
2696 struct fcoe_ctlr *fip = arg;
2697 struct fc_exch *exch = fc_seq_exch(seq);
2698 struct fc_lport *lport = exch->lp;
2699 u8 *mac;
2700
2701 if (IS_ERR(fp))
2702 goto done;
2703
2704 mac = fr_cb(fp)->granted_mac;
Vasu Dev907c07d2011-10-28 11:34:23 -07002705 /* pre-FIP */
2706 if (is_zero_ether_addr(mac))
2707 fcoe_ctlr_recv_flogi(fip, lport, fp);
2708 if (!is_zero_ether_addr(mac))
2709 fcoe_update_src_mac(lport, mac);
Chris Leech11b56182009-11-03 11:46:29 -08002710done:
2711 fc_lport_flogi_resp(seq, fp, lport);
2712}
2713
2714/**
2715 * fcoe_logo_resp() - FCoE specific LOGO response handler
2716 * @seq: active sequence in the LOGO exchange
2717 * @fp: response frame, or error encoded in a pointer (timeout)
2718 * @arg: pointer the the fcoe_ctlr structure
2719 *
Uwe Kleine-König65155b32010-06-11 12:17:01 +02002720 * This handles MAC address management for FCoE, then passes control on to
Chris Leech11b56182009-11-03 11:46:29 -08002721 * the libfc LOGO response handler.
2722 */
2723static void fcoe_logo_resp(struct fc_seq *seq, struct fc_frame *fp, void *arg)
2724{
Joe Eykholt386309c2009-11-03 11:49:16 -08002725 struct fc_lport *lport = arg;
Chris Leech11b56182009-11-03 11:46:29 -08002726 static u8 zero_mac[ETH_ALEN] = { 0 };
2727
2728 if (!IS_ERR(fp))
Joe Eykholt386309c2009-11-03 11:49:16 -08002729 fcoe_update_src_mac(lport, zero_mac);
Chris Leech11b56182009-11-03 11:46:29 -08002730 fc_lport_logo_resp(seq, fp, lport);
2731}
2732
2733/**
2734 * fcoe_elsct_send - FCoE specific ELS handler
2735 *
2736 * This does special case handling of FIP encapsualted ELS exchanges for FCoE,
2737 * using FCoE specific response handlers and passing the FIP controller as
2738 * the argument (the lport is still available from the exchange).
2739 *
2740 * Most of the work here is just handed off to the libfc routine.
2741 */
Robert Love1875f272009-11-03 11:47:50 -08002742static struct fc_seq *fcoe_elsct_send(struct fc_lport *lport, u32 did,
2743 struct fc_frame *fp, unsigned int op,
2744 void (*resp)(struct fc_seq *,
2745 struct fc_frame *,
2746 void *),
2747 void *arg, u32 timeout)
Chris Leech11b56182009-11-03 11:46:29 -08002748{
2749 struct fcoe_port *port = lport_priv(lport);
Bhanu Prakash Gollapudi8597ae82011-01-28 16:05:37 -08002750 struct fcoe_interface *fcoe = port->priv;
Robert Love619fe4b2012-05-22 19:06:10 -07002751 struct fcoe_ctlr *fip = fcoe_to_ctlr(fcoe);
Chris Leech11b56182009-11-03 11:46:29 -08002752 struct fc_frame_header *fh = fc_frame_header_get(fp);
2753
2754 switch (op) {
2755 case ELS_FLOGI:
2756 case ELS_FDISC:
Joe Eykholte10f8c62010-07-20 15:20:30 -07002757 if (lport->point_to_multipoint)
2758 break;
Chris Leech11b56182009-11-03 11:46:29 -08002759 return fc_elsct_send(lport, did, fp, op, fcoe_flogi_resp,
2760 fip, timeout);
2761 case ELS_LOGO:
2762 /* only hook onto fabric logouts, not port logouts */
2763 if (ntoh24(fh->fh_d_id) != FC_FID_FLOGI)
2764 break;
2765 return fc_elsct_send(lport, did, fp, op, fcoe_logo_resp,
Joe Eykholt386309c2009-11-03 11:49:16 -08002766 lport, timeout);
Chris Leech11b56182009-11-03 11:46:29 -08002767 }
2768 return fc_elsct_send(lport, did, fp, op, resp, arg, timeout);
2769}
2770
Chris Leech9a057532009-11-03 11:46:40 -08002771/**
2772 * fcoe_vport_create() - create an fc_host/scsi_host for a vport
2773 * @vport: fc_vport object to create a new fc_host for
2774 * @disabled: start the new fc_host in a disabled state by default?
2775 *
2776 * Returns: 0 for success
2777 */
2778static int fcoe_vport_create(struct fc_vport *vport, bool disabled)
2779{
2780 struct Scsi_Host *shost = vport_to_shost(vport);
2781 struct fc_lport *n_port = shost_priv(shost);
2782 struct fcoe_port *port = lport_priv(n_port);
Bhanu Prakash Gollapudi8597ae82011-01-28 16:05:37 -08002783 struct fcoe_interface *fcoe = port->priv;
Chris Leech9a057532009-11-03 11:46:40 -08002784 struct net_device *netdev = fcoe->netdev;
2785 struct fc_lport *vn_port;
Neerav Parikhbdf25212011-05-16 16:45:29 -07002786 int rc;
2787 char buf[32];
2788
2789 rc = fcoe_validate_vport_create(vport);
2790 if (rc) {
Bhanu Prakash Gollapudid8348952011-08-04 17:38:49 -07002791 fcoe_wwn_to_str(vport->port_name, buf, sizeof(buf));
Neerav Parikhbdf25212011-05-16 16:45:29 -07002792 printk(KERN_ERR "fcoe: Failed to create vport, "
2793 "WWPN (0x%s) already exists\n",
2794 buf);
2795 return rc;
2796 }
Chris Leech9a057532009-11-03 11:46:40 -08002797
2798 mutex_lock(&fcoe_config_mutex);
Jiri Pirko4bc71cb2011-09-03 03:34:30 +00002799 rtnl_lock();
Chris Leech9a057532009-11-03 11:46:40 -08002800 vn_port = fcoe_if_create(fcoe, &vport->dev, 1);
Jiri Pirko4bc71cb2011-09-03 03:34:30 +00002801 rtnl_unlock();
Chris Leech9a057532009-11-03 11:46:40 -08002802 mutex_unlock(&fcoe_config_mutex);
2803
2804 if (IS_ERR(vn_port)) {
2805 printk(KERN_ERR "fcoe: fcoe_vport_create(%s) failed\n",
2806 netdev->name);
2807 return -EIO;
2808 }
2809
2810 if (disabled) {
2811 fc_vport_set_state(vport, FC_VPORT_DISABLED);
2812 } else {
2813 vn_port->boot_time = jiffies;
2814 fc_fabric_login(vn_port);
2815 fc_vport_setlink(vn_port);
2816 }
2817 return 0;
2818}
2819
2820/**
2821 * fcoe_vport_destroy() - destroy the fc_host/scsi_host for a vport
2822 * @vport: fc_vport object that is being destroyed
2823 *
2824 * Returns: 0 for success
2825 */
2826static int fcoe_vport_destroy(struct fc_vport *vport)
2827{
2828 struct Scsi_Host *shost = vport_to_shost(vport);
2829 struct fc_lport *n_port = shost_priv(shost);
2830 struct fc_lport *vn_port = vport->dd_data;
Chris Leech9a057532009-11-03 11:46:40 -08002831
2832 mutex_lock(&n_port->lp_mutex);
2833 list_del(&vn_port->list);
2834 mutex_unlock(&n_port->lp_mutex);
Robert Loveccefd232012-02-10 17:18:41 -08002835
2836 mutex_lock(&fcoe_config_mutex);
2837 fcoe_if_destroy(vn_port);
2838 mutex_unlock(&fcoe_config_mutex);
2839
Chris Leech9a057532009-11-03 11:46:40 -08002840 return 0;
2841}
2842
2843/**
2844 * fcoe_vport_disable() - change vport state
2845 * @vport: vport to bring online/offline
2846 * @disable: should the vport be disabled?
2847 */
2848static int fcoe_vport_disable(struct fc_vport *vport, bool disable)
2849{
2850 struct fc_lport *lport = vport->dd_data;
2851
2852 if (disable) {
2853 fc_vport_set_state(vport, FC_VPORT_DISABLED);
2854 fc_fabric_logoff(lport);
2855 } else {
2856 lport->boot_time = jiffies;
2857 fc_fabric_login(lport);
2858 fc_vport_setlink(lport);
2859 }
2860
2861 return 0;
2862}
2863
Chris Leechdc8596d2009-11-03 11:47:18 -08002864/**
2865 * fcoe_vport_set_symbolic_name() - append vport string to symbolic name
2866 * @vport: fc_vport with a new symbolic name string
2867 *
2868 * After generating a new symbolic name string, a new RSPN_ID request is
2869 * sent to the name server. There is no response handler, so if it fails
2870 * for some reason it will not be retried.
2871 */
2872static void fcoe_set_vport_symbolic_name(struct fc_vport *vport)
2873{
2874 struct fc_lport *lport = vport->dd_data;
2875 struct fc_frame *fp;
2876 size_t len;
2877
2878 snprintf(fc_host_symbolic_name(lport->host), FC_SYMBOLIC_NAME_SIZE,
2879 "%s v%s over %s : %s", FCOE_NAME, FCOE_VERSION,
2880 fcoe_netdev(lport)->name, vport->symbolic_name);
2881
2882 if (lport->state != LPORT_ST_READY)
2883 return;
2884
2885 len = strnlen(fc_host_symbolic_name(lport->host), 255);
2886 fp = fc_frame_alloc(lport,
2887 sizeof(struct fc_ct_hdr) +
2888 sizeof(struct fc_ns_rspn) + len);
2889 if (!fp)
2890 return;
2891 lport->tt.elsct_send(lport, FC_FID_DIR_SERV, fp, FC_NS_RSPN_ID,
Joe Eykholtb94f8952009-11-03 11:50:21 -08002892 NULL, NULL, 3 * lport->r_a_tov);
Chris Leechdc8596d2009-11-03 11:47:18 -08002893}
Yi Zoub84056b2009-11-20 14:55:19 -08002894
2895/**
2896 * fcoe_get_lesb() - Fill the FCoE Link Error Status Block
2897 * @lport: the local port
2898 * @fc_lesb: the link error status block
2899 */
2900static void fcoe_get_lesb(struct fc_lport *lport,
2901 struct fc_els_lesb *fc_lesb)
2902{
Yi Zoub84056b2009-11-20 14:55:19 -08002903 struct net_device *netdev = fcoe_netdev(lport);
2904
Bhanu Prakash Gollapudi814740d2011-10-03 16:45:01 -07002905 __fcoe_get_lesb(lport, fc_lesb, netdev);
Yi Zoub84056b2009-11-20 14:55:19 -08002906}
Joe Eykholt7d65b0d2010-03-12 16:08:02 -08002907
Robert Love8d55e502012-05-22 19:06:26 -07002908static void fcoe_ctlr_get_lesb(struct fcoe_ctlr_device *ctlr_dev)
2909{
2910 struct fcoe_ctlr *fip = fcoe_ctlr_device_priv(ctlr_dev);
2911 struct net_device *netdev = fcoe_netdev(fip->lp);
2912 struct fcoe_fc_els_lesb *fcoe_lesb;
2913 struct fc_els_lesb fc_lesb;
2914
2915 __fcoe_get_lesb(fip->lp, &fc_lesb, netdev);
2916 fcoe_lesb = (struct fcoe_fc_els_lesb *)(&fc_lesb);
2917
2918 ctlr_dev->lesb.lesb_link_fail =
2919 ntohl(fcoe_lesb->lesb_link_fail);
2920 ctlr_dev->lesb.lesb_vlink_fail =
2921 ntohl(fcoe_lesb->lesb_vlink_fail);
2922 ctlr_dev->lesb.lesb_miss_fka =
2923 ntohl(fcoe_lesb->lesb_miss_fka);
2924 ctlr_dev->lesb.lesb_symb_err =
2925 ntohl(fcoe_lesb->lesb_symb_err);
2926 ctlr_dev->lesb.lesb_err_block =
2927 ntohl(fcoe_lesb->lesb_err_block);
2928 ctlr_dev->lesb.lesb_fcs_error =
2929 ntohl(fcoe_lesb->lesb_fcs_error);
2930}
2931
2932static void fcoe_fcf_get_vlan_id(struct fcoe_fcf_device *fcf_dev)
2933{
2934 struct fcoe_ctlr_device *ctlr_dev =
2935 fcoe_fcf_dev_to_ctlr_dev(fcf_dev);
2936 struct fcoe_ctlr *ctlr = fcoe_ctlr_device_priv(ctlr_dev);
2937 struct fcoe_interface *fcoe = fcoe_ctlr_priv(ctlr);
2938
2939 fcf_dev->vlan_id = vlan_dev_vlan_id(fcoe->netdev);
2940}
2941
Joe Eykholt7d65b0d2010-03-12 16:08:02 -08002942/**
2943 * fcoe_set_port_id() - Callback from libfc when Port_ID is set.
2944 * @lport: the local port
2945 * @port_id: the port ID
2946 * @fp: the received frame, if any, that caused the port_id to be set.
2947 *
2948 * This routine handles the case where we received a FLOGI and are
2949 * entering point-to-point mode. We need to call fcoe_ctlr_recv_flogi()
2950 * so it can set the non-mapped mode and gateway address.
2951 *
2952 * The FLOGI LS_ACC is handled by fcoe_flogi_resp().
2953 */
2954static void fcoe_set_port_id(struct fc_lport *lport,
2955 u32 port_id, struct fc_frame *fp)
2956{
2957 struct fcoe_port *port = lport_priv(lport);
Bhanu Prakash Gollapudi8597ae82011-01-28 16:05:37 -08002958 struct fcoe_interface *fcoe = port->priv;
Robert Love619fe4b2012-05-22 19:06:10 -07002959 struct fcoe_ctlr *ctlr = fcoe_to_ctlr(fcoe);
Joe Eykholt7d65b0d2010-03-12 16:08:02 -08002960
2961 if (fp && fc_frame_payload_op(fp) == ELS_FLOGI)
Robert Love619fe4b2012-05-22 19:06:10 -07002962 fcoe_ctlr_recv_flogi(ctlr, lport, fp);
Joe Eykholt7d65b0d2010-03-12 16:08:02 -08002963}