blob: c30fa27402d9eb1fb73b9aac5d37a1c22e0d0fa7 [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>
21#include <linux/version.h>
Robert Love85b4aa42008-12-09 15:10:24 -080022#include <linux/spinlock.h>
Robert Love85b4aa42008-12-09 15:10:24 -080023#include <linux/netdevice.h>
24#include <linux/etherdevice.h>
25#include <linux/ethtool.h>
26#include <linux/if_ether.h>
27#include <linux/if_vlan.h>
Robert Love85b4aa42008-12-09 15:10:24 -080028#include <linux/crc32.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090029#include <linux/slab.h>
Robert Love85b4aa42008-12-09 15:10:24 -080030#include <linux/cpu.h>
31#include <linux/fs.h>
32#include <linux/sysfs.h>
33#include <linux/ctype.h>
Tejun Heo2ca32b42011-01-28 16:05:32 -080034#include <linux/workqueue.h>
Robert Love85b4aa42008-12-09 15:10:24 -080035#include <scsi/scsi_tcq.h>
36#include <scsi/scsicam.h>
37#include <scsi/scsi_transport.h>
38#include <scsi/scsi_transport_fc.h>
39#include <net/rtnetlink.h>
40
41#include <scsi/fc/fc_encaps.h>
Joe Eykholt97c83892009-03-17 11:42:40 -070042#include <scsi/fc/fc_fip.h>
Robert Love85b4aa42008-12-09 15:10:24 -080043
44#include <scsi/libfc.h>
45#include <scsi/fc_frame.h>
46#include <scsi/libfcoe.h>
Robert Love85b4aa42008-12-09 15:10:24 -080047
Vasu Devfdd78022009-03-17 11:42:24 -070048#include "fcoe.h"
49
Robert Love85b4aa42008-12-09 15:10:24 -080050MODULE_AUTHOR("Open-FCoE.org");
51MODULE_DESCRIPTION("FCoE");
Vasu Dev9b34ecf2009-03-17 11:42:13 -070052MODULE_LICENSE("GPL v2");
Robert Love85b4aa42008-12-09 15:10:24 -080053
Yi Zou05cc7392009-08-25 13:59:03 -070054/* Performance tuning parameters for fcoe */
55static unsigned int fcoe_ddp_min;
56module_param_named(ddp_min, fcoe_ddp_min, uint, S_IRUGO | S_IWUSR);
57MODULE_PARM_DESC(ddp_min, "Minimum I/O size in bytes for " \
58 "Direct Data Placement (DDP).");
59
Chris Leechdfc1d0f2009-08-25 14:00:13 -070060DEFINE_MUTEX(fcoe_config_mutex);
61
Tejun Heo2ca32b42011-01-28 16:05:32 -080062static struct workqueue_struct *fcoe_wq;
63
Joe Eykholte7a51992009-08-25 14:04:08 -070064/* fcoe_percpu_clean completion. Waiter protected by fcoe_create_mutex */
65static DECLARE_COMPLETION(fcoe_flush_completion);
66
Robert Love85b4aa42008-12-09 15:10:24 -080067/* fcoe host list */
Chris Leech090eb6c2009-08-25 14:00:28 -070068/* must only by accessed under the RTNL mutex */
Robert Love85b4aa42008-12-09 15:10:24 -080069LIST_HEAD(fcoe_hostlist);
Robert Love5e5e92d2009-03-17 11:41:35 -070070DEFINE_PER_CPU(struct fcoe_percpu_s, fcoe_percpu);
Robert Love85b4aa42008-12-09 15:10:24 -080071
Chris Leechdd3fd722009-04-21 16:27:36 -070072/* Function Prototypes */
Robert Love1875f272009-11-03 11:47:50 -080073static int fcoe_reset(struct Scsi_Host *);
Vasu Devfdd78022009-03-17 11:42:24 -070074static int fcoe_xmit(struct fc_lport *, struct fc_frame *);
75static int fcoe_rcv(struct sk_buff *, struct net_device *,
76 struct packet_type *, struct net_device *);
Robert Love1875f272009-11-03 11:47:50 -080077static int fcoe_percpu_receive_thread(void *);
Robert Love1875f272009-11-03 11:47:50 -080078static void fcoe_percpu_clean(struct fc_lport *);
Robert Love5e4f8fe2010-05-07 15:18:35 -070079static int fcoe_link_speed_update(struct fc_lport *);
Robert Love1875f272009-11-03 11:47:50 -080080static int fcoe_link_ok(struct fc_lport *);
Vasu Devfdd78022009-03-17 11:42:24 -070081
82static struct fc_lport *fcoe_hostlist_lookup(const struct net_device *);
83static int fcoe_hostlist_add(const struct fc_lport *);
Vasu Devfdd78022009-03-17 11:42:24 -070084
Robert Love85b4aa42008-12-09 15:10:24 -080085static int fcoe_device_notification(struct notifier_block *, ulong, void *);
86static void fcoe_dev_setup(void);
87static void fcoe_dev_cleanup(void);
Robert Love1875f272009-11-03 11:47:50 -080088static struct fcoe_interface
89*fcoe_hostlist_lookup_port(const struct net_device *);
Robert Love85b4aa42008-12-09 15:10:24 -080090
Robert Love1875f272009-11-03 11:47:50 -080091static int fcoe_fip_recv(struct sk_buff *, struct net_device *,
92 struct packet_type *, struct net_device *);
93
94static void fcoe_fip_send(struct fcoe_ctlr *, struct sk_buff *);
95static void fcoe_update_src_mac(struct fc_lport *, u8 *);
96static u8 *fcoe_get_src_mac(struct fc_lport *);
97static void fcoe_destroy_work(struct work_struct *);
98
99static int fcoe_ddp_setup(struct fc_lport *, u16, struct scatterlist *,
100 unsigned int);
101static int fcoe_ddp_done(struct fc_lport *, u16);
Yi Zou71f89492011-06-20 16:59:10 -0700102static int fcoe_ddp_target(struct fc_lport *, u16, struct scatterlist *,
103 unsigned int);
Robert Love1875f272009-11-03 11:47:50 -0800104static int fcoe_cpu_callback(struct notifier_block *, unsigned long, void *);
105
Yi Zou78a58242011-01-28 16:05:16 -0800106static bool fcoe_match(struct net_device *netdev);
107static int fcoe_create(struct net_device *netdev, enum fip_state fip_mode);
108static int fcoe_destroy(struct net_device *netdev);
109static int fcoe_enable(struct net_device *netdev);
110static int fcoe_disable(struct net_device *netdev);
Robert Love1875f272009-11-03 11:47:50 -0800111
Robert Love1875f272009-11-03 11:47:50 -0800112static struct fc_seq *fcoe_elsct_send(struct fc_lport *,
113 u32 did, struct fc_frame *,
114 unsigned int op,
115 void (*resp)(struct fc_seq *,
116 struct fc_frame *,
117 void *),
118 void *, u32 timeout);
Chris Leech859b7b62009-11-20 14:54:47 -0800119static void fcoe_recv_frame(struct sk_buff *skb);
Robert Love1875f272009-11-03 11:47:50 -0800120
Yi Zoub84056b2009-11-20 14:55:19 -0800121static void fcoe_get_lesb(struct fc_lport *, struct fc_els_lesb *);
122
Robert Love1875f272009-11-03 11:47:50 -0800123/* notification function for packets from net device */
Robert Love85b4aa42008-12-09 15:10:24 -0800124static struct notifier_block fcoe_notifier = {
125 .notifier_call = fcoe_device_notification,
126};
127
Robert Love1875f272009-11-03 11:47:50 -0800128/* notification function for CPU hotplug events */
129static struct notifier_block fcoe_cpu_notifier = {
130 .notifier_call = fcoe_cpu_callback,
131};
132
Yi Zou8ca86f82011-01-28 16:05:11 -0800133static struct scsi_transport_template *fcoe_nport_scsi_transport;
134static struct scsi_transport_template *fcoe_vport_scsi_transport;
Vasu Dev7f349142009-03-27 09:06:31 -0700135
Robert Love1875f272009-11-03 11:47:50 -0800136static int fcoe_vport_destroy(struct fc_vport *);
137static int fcoe_vport_create(struct fc_vport *, bool disabled);
138static int fcoe_vport_disable(struct fc_vport *, bool disable);
139static void fcoe_set_vport_symbolic_name(struct fc_vport *);
Joe Eykholt7d65b0d2010-03-12 16:08:02 -0800140static void fcoe_set_port_id(struct fc_lport *, u32, struct fc_frame *);
Robert Love1875f272009-11-03 11:47:50 -0800141
142static struct libfc_function_template fcoe_libfc_fcn_templ = {
143 .frame_send = fcoe_xmit,
144 .ddp_setup = fcoe_ddp_setup,
145 .ddp_done = fcoe_ddp_done,
Yi Zou71f89492011-06-20 16:59:10 -0700146 .ddp_target = fcoe_ddp_target,
Robert Love1875f272009-11-03 11:47:50 -0800147 .elsct_send = fcoe_elsct_send,
Yi Zoub84056b2009-11-20 14:55:19 -0800148 .get_lesb = fcoe_get_lesb,
Joe Eykholt7d65b0d2010-03-12 16:08:02 -0800149 .lport_set_port_id = fcoe_set_port_id,
Robert Love1875f272009-11-03 11:47:50 -0800150};
Chris Leech9a057532009-11-03 11:46:40 -0800151
Yi Zou8ca86f82011-01-28 16:05:11 -0800152struct fc_function_template fcoe_nport_fc_functions = {
Vasu Dev7f349142009-03-27 09:06:31 -0700153 .show_host_node_name = 1,
154 .show_host_port_name = 1,
155 .show_host_supported_classes = 1,
156 .show_host_supported_fc4s = 1,
157 .show_host_active_fc4s = 1,
158 .show_host_maxframe_size = 1,
159
160 .show_host_port_id = 1,
161 .show_host_supported_speeds = 1,
162 .get_host_speed = fc_get_host_speed,
163 .show_host_speed = 1,
164 .show_host_port_type = 1,
165 .get_host_port_state = fc_get_host_port_state,
166 .show_host_port_state = 1,
167 .show_host_symbolic_name = 1,
168
169 .dd_fcrport_size = sizeof(struct fc_rport_libfc_priv),
170 .show_rport_maxframe_size = 1,
171 .show_rport_supported_classes = 1,
172
173 .show_host_fabric_name = 1,
174 .show_starget_node_name = 1,
175 .show_starget_port_name = 1,
176 .show_starget_port_id = 1,
177 .set_rport_dev_loss_tmo = fc_set_rport_loss_tmo,
178 .show_rport_dev_loss_tmo = 1,
179 .get_fc_host_stats = fc_get_host_stats,
180 .issue_fc_host_lip = fcoe_reset,
181
182 .terminate_rport_io = fc_rport_terminate_io,
Chris Leech9a057532009-11-03 11:46:40 -0800183
184 .vport_create = fcoe_vport_create,
185 .vport_delete = fcoe_vport_destroy,
186 .vport_disable = fcoe_vport_disable,
Chris Leechdc8596d2009-11-03 11:47:18 -0800187 .set_vport_symbolic_name = fcoe_set_vport_symbolic_name,
Steve Maa51ab392009-11-03 11:47:34 -0800188
189 .bsg_request = fc_lport_bsg_request,
Vasu Dev7f349142009-03-27 09:06:31 -0700190};
191
Yi Zou8ca86f82011-01-28 16:05:11 -0800192struct fc_function_template fcoe_vport_fc_functions = {
Chris Leeche9084bb2009-11-03 11:46:34 -0800193 .show_host_node_name = 1,
194 .show_host_port_name = 1,
195 .show_host_supported_classes = 1,
196 .show_host_supported_fc4s = 1,
197 .show_host_active_fc4s = 1,
198 .show_host_maxframe_size = 1,
199
200 .show_host_port_id = 1,
201 .show_host_supported_speeds = 1,
202 .get_host_speed = fc_get_host_speed,
203 .show_host_speed = 1,
204 .show_host_port_type = 1,
205 .get_host_port_state = fc_get_host_port_state,
206 .show_host_port_state = 1,
207 .show_host_symbolic_name = 1,
208
209 .dd_fcrport_size = sizeof(struct fc_rport_libfc_priv),
210 .show_rport_maxframe_size = 1,
211 .show_rport_supported_classes = 1,
212
213 .show_host_fabric_name = 1,
214 .show_starget_node_name = 1,
215 .show_starget_port_name = 1,
216 .show_starget_port_id = 1,
217 .set_rport_dev_loss_tmo = fc_set_rport_loss_tmo,
218 .show_rport_dev_loss_tmo = 1,
219 .get_fc_host_stats = fc_get_host_stats,
220 .issue_fc_host_lip = fcoe_reset,
221
222 .terminate_rport_io = fc_rport_terminate_io,
Steve Maa51ab392009-11-03 11:47:34 -0800223
224 .bsg_request = fc_lport_bsg_request,
Chris Leeche9084bb2009-11-03 11:46:34 -0800225};
226
Vasu Dev7f349142009-03-27 09:06:31 -0700227static struct scsi_host_template fcoe_shost_template = {
228 .module = THIS_MODULE,
229 .name = "FCoE Driver",
230 .proc_name = FCOE_NAME,
231 .queuecommand = fc_queuecommand,
232 .eh_abort_handler = fc_eh_abort,
233 .eh_device_reset_handler = fc_eh_device_reset,
234 .eh_host_reset_handler = fc_eh_host_reset,
235 .slave_alloc = fc_slave_alloc,
236 .change_queue_depth = fc_change_queue_depth,
237 .change_queue_type = fc_change_queue_type,
238 .this_id = -1,
Vasu Dev14caf442009-10-15 17:46:55 -0700239 .cmd_per_lun = 3,
Vasu Dev7f349142009-03-27 09:06:31 -0700240 .can_queue = FCOE_MAX_OUTSTANDING_COMMANDS,
241 .use_clustering = ENABLE_CLUSTERING,
242 .sg_tablesize = SG_ALL,
243 .max_sectors = 0xffff,
244};
245
Chris Leech54b649f2009-08-25 14:00:07 -0700246/**
Robert Love1875f272009-11-03 11:47:50 -0800247 * fcoe_interface_setup() - Setup a FCoE interface
248 * @fcoe: The new FCoE interface
249 * @netdev: The net device that the fcoe interface is on
Chris Leech54b649f2009-08-25 14:00:07 -0700250 *
251 * Returns : 0 for success
Chris Leech2e70e242009-08-25 14:00:23 -0700252 * Locking: must be called with the RTNL mutex held
Chris Leech54b649f2009-08-25 14:00:07 -0700253 */
254static int fcoe_interface_setup(struct fcoe_interface *fcoe,
255 struct net_device *netdev)
256{
257 struct fcoe_ctlr *fip = &fcoe->ctlr;
258 struct netdev_hw_addr *ha;
Yi Zou5bab87e2009-11-03 11:49:43 -0800259 struct net_device *real_dev;
Chris Leech54b649f2009-08-25 14:00:07 -0700260 u8 flogi_maddr[ETH_ALEN];
Yi Zoub7a727f2009-10-21 16:28:03 -0700261 const struct net_device_ops *ops;
Chris Leech54b649f2009-08-25 14:00:07 -0700262
263 fcoe->netdev = netdev;
264
Yi Zoub7a727f2009-10-21 16:28:03 -0700265 /* Let LLD initialize for FCoE */
266 ops = netdev->netdev_ops;
267 if (ops->ndo_fcoe_enable) {
268 if (ops->ndo_fcoe_enable(netdev))
269 FCOE_NETDEV_DBG(netdev, "Failed to enable FCoE"
270 " specific feature for LLD.\n");
271 }
272
Chris Leech54b649f2009-08-25 14:00:07 -0700273 /* Do not support for bonding device */
Jiri Pirkocc8bdf02011-03-01 20:05:35 +0000274 if (netdev->priv_flags & IFF_BONDING && netdev->flags & IFF_MASTER) {
john fastabend59d92512009-11-03 11:48:44 -0800275 FCOE_NETDEV_DBG(netdev, "Bonded interfaces not supported\n");
Chris Leech54b649f2009-08-25 14:00:07 -0700276 return -EOPNOTSUPP;
277 }
278
279 /* look for SAN MAC address, if multiple SAN MACs exist, only
280 * use the first one for SPMA */
Yi Zou5bab87e2009-11-03 11:49:43 -0800281 real_dev = (netdev->priv_flags & IFF_802_1Q_VLAN) ?
282 vlan_dev_real_dev(netdev) : netdev;
Chris Leech54b649f2009-08-25 14:00:07 -0700283 rcu_read_lock();
Yi Zou5bab87e2009-11-03 11:49:43 -0800284 for_each_dev_addr(real_dev, ha) {
Chris Leech54b649f2009-08-25 14:00:07 -0700285 if ((ha->type == NETDEV_HW_ADDR_T_SAN) &&
Yi Zoubf361702009-11-03 11:49:38 -0800286 (is_valid_ether_addr(ha->addr))) {
Chris Leech54b649f2009-08-25 14:00:07 -0700287 memcpy(fip->ctl_src_addr, ha->addr, ETH_ALEN);
288 fip->spma = 1;
289 break;
290 }
291 }
292 rcu_read_unlock();
293
294 /* setup Source Mac Address */
295 if (!fip->spma)
296 memcpy(fip->ctl_src_addr, netdev->dev_addr, netdev->addr_len);
297
298 /*
299 * Add FCoE MAC address as second unicast MAC address
300 * or enter promiscuous mode if not capable of listening
301 * for multiple unicast MACs.
302 */
Chris Leech54b649f2009-08-25 14:00:07 -0700303 memcpy(flogi_maddr, (u8[6]) FC_FCOE_FLOGI_MAC, ETH_ALEN);
Jiri Pirkoa748ee22010-04-01 21:22:09 +0000304 dev_uc_add(netdev, flogi_maddr);
Chris Leech54b649f2009-08-25 14:00:07 -0700305 if (fip->spma)
Jiri Pirkoa748ee22010-04-01 21:22:09 +0000306 dev_uc_add(netdev, fip->ctl_src_addr);
Joe Eykholte10f8c62010-07-20 15:20:30 -0700307 if (fip->mode == FIP_MODE_VN2VN) {
308 dev_mc_add(netdev, FIP_ALL_VN2VN_MACS);
309 dev_mc_add(netdev, FIP_ALL_P2P_MACS);
310 } else
311 dev_mc_add(netdev, FIP_ALL_ENODE_MACS);
Chris Leech54b649f2009-08-25 14:00:07 -0700312
313 /*
314 * setup the receive function from ethernet driver
315 * on the ethertype for the given device
316 */
317 fcoe->fcoe_packet_type.func = fcoe_rcv;
318 fcoe->fcoe_packet_type.type = __constant_htons(ETH_P_FCOE);
319 fcoe->fcoe_packet_type.dev = netdev;
320 dev_add_pack(&fcoe->fcoe_packet_type);
321
322 fcoe->fip_packet_type.func = fcoe_fip_recv;
323 fcoe->fip_packet_type.type = htons(ETH_P_FIP);
324 fcoe->fip_packet_type.dev = netdev;
325 dev_add_pack(&fcoe->fip_packet_type);
326
327 return 0;
328}
329
Vasu Dev7f349142009-03-27 09:06:31 -0700330/**
Robert Love1875f272009-11-03 11:47:50 -0800331 * fcoe_interface_create() - Create a FCoE interface on a net device
332 * @netdev: The net device to create the FCoE interface on
Joe Eykholt1dd454d2010-07-20 15:20:46 -0700333 * @fip_mode: The mode to use for FIP
Chris Leech030f4e02009-08-25 14:00:02 -0700334 *
335 * Returns: pointer to a struct fcoe_interface or NULL on error
336 */
Joe Eykholt1dd454d2010-07-20 15:20:46 -0700337static struct fcoe_interface *fcoe_interface_create(struct net_device *netdev,
338 enum fip_state fip_mode)
Chris Leech030f4e02009-08-25 14:00:02 -0700339{
340 struct fcoe_interface *fcoe;
john fastabend59d92512009-11-03 11:48:44 -0800341 int err;
Chris Leech030f4e02009-08-25 14:00:02 -0700342
Robert Love7287fb92011-01-28 16:03:47 -0800343 if (!try_module_get(THIS_MODULE)) {
344 FCOE_NETDEV_DBG(netdev,
345 "Could not get a reference to the module\n");
346 fcoe = ERR_PTR(-EBUSY);
347 goto out;
348 }
349
Chris Leech030f4e02009-08-25 14:00:02 -0700350 fcoe = kzalloc(sizeof(*fcoe), GFP_KERNEL);
351 if (!fcoe) {
352 FCOE_NETDEV_DBG(netdev, "Could not allocate fcoe structure\n");
Robert Love7287fb92011-01-28 16:03:47 -0800353 fcoe = ERR_PTR(-ENOMEM);
354 goto out_nomod;
Chris Leech030f4e02009-08-25 14:00:02 -0700355 }
356
Chris Leech2e70e242009-08-25 14:00:23 -0700357 dev_hold(netdev);
Chris Leech030f4e02009-08-25 14:00:02 -0700358 kref_init(&fcoe->kref);
Chris Leech54b649f2009-08-25 14:00:07 -0700359
360 /*
361 * Initialize FIP.
362 */
Joe Eykholt1dd454d2010-07-20 15:20:46 -0700363 fcoe_ctlr_init(&fcoe->ctlr, fip_mode);
Chris Leech54b649f2009-08-25 14:00:07 -0700364 fcoe->ctlr.send = fcoe_fip_send;
365 fcoe->ctlr.update_mac = fcoe_update_src_mac;
Chris Leech11b56182009-11-03 11:46:29 -0800366 fcoe->ctlr.get_src_addr = fcoe_get_src_mac;
Chris Leech54b649f2009-08-25 14:00:07 -0700367
john fastabend59d92512009-11-03 11:48:44 -0800368 err = fcoe_interface_setup(fcoe, netdev);
369 if (err) {
370 fcoe_ctlr_destroy(&fcoe->ctlr);
371 kfree(fcoe);
372 dev_put(netdev);
Robert Love7287fb92011-01-28 16:03:47 -0800373 fcoe = ERR_PTR(err);
374 goto out_nomod;
john fastabend59d92512009-11-03 11:48:44 -0800375 }
Chris Leech030f4e02009-08-25 14:00:02 -0700376
Robert Love7287fb92011-01-28 16:03:47 -0800377 goto out;
378
379out_nomod:
380 module_put(THIS_MODULE);
381out:
Chris Leech030f4e02009-08-25 14:00:02 -0700382 return fcoe;
383}
384
385/**
386 * fcoe_interface_release() - fcoe_port kref release function
Robert Love1875f272009-11-03 11:47:50 -0800387 * @kref: Embedded reference count in an fcoe_interface struct
Chris Leech030f4e02009-08-25 14:00:02 -0700388 */
389static void fcoe_interface_release(struct kref *kref)
390{
391 struct fcoe_interface *fcoe;
Chris Leech2e70e242009-08-25 14:00:23 -0700392 struct net_device *netdev;
Chris Leech030f4e02009-08-25 14:00:02 -0700393
394 fcoe = container_of(kref, struct fcoe_interface, kref);
Chris Leech2e70e242009-08-25 14:00:23 -0700395 netdev = fcoe->netdev;
396 /* tear-down the FCoE controller */
397 fcoe_ctlr_destroy(&fcoe->ctlr);
Chris Leech030f4e02009-08-25 14:00:02 -0700398 kfree(fcoe);
Chris Leech2e70e242009-08-25 14:00:23 -0700399 dev_put(netdev);
Robert Love7287fb92011-01-28 16:03:47 -0800400 module_put(THIS_MODULE);
Chris Leech030f4e02009-08-25 14:00:02 -0700401}
402
403/**
Robert Love1875f272009-11-03 11:47:50 -0800404 * fcoe_interface_get() - Get a reference to a FCoE interface
405 * @fcoe: The FCoE interface to be held
Chris Leech030f4e02009-08-25 14:00:02 -0700406 */
407static inline void fcoe_interface_get(struct fcoe_interface *fcoe)
408{
409 kref_get(&fcoe->kref);
410}
411
412/**
Robert Love1875f272009-11-03 11:47:50 -0800413 * fcoe_interface_put() - Put a reference to a FCoE interface
414 * @fcoe: The FCoE interface to be released
Chris Leech030f4e02009-08-25 14:00:02 -0700415 */
416static inline void fcoe_interface_put(struct fcoe_interface *fcoe)
417{
418 kref_put(&fcoe->kref, fcoe_interface_release);
419}
420
421/**
Vasu Devf04ca1b2011-04-01 16:06:45 -0700422 * fcoe_interface_cleanup() - Clean up a FCoE interface
423 * @fcoe: The FCoE interface to be cleaned up
424 *
425 * Caller must be holding the RTNL mutex
426 */
427void fcoe_interface_cleanup(struct fcoe_interface *fcoe)
428{
429 struct net_device *netdev = fcoe->netdev;
430 struct fcoe_ctlr *fip = &fcoe->ctlr;
431 u8 flogi_maddr[ETH_ALEN];
432 const struct net_device_ops *ops;
Vasu Devf04ca1b2011-04-01 16:06:45 -0700433
434 /*
435 * Don't listen for Ethernet packets anymore.
436 * synchronize_net() ensures that the packet handlers are not running
437 * on another CPU. dev_remove_pack() would do that, this calls the
438 * unsyncronized version __dev_remove_pack() to avoid multiple delays.
439 */
440 __dev_remove_pack(&fcoe->fcoe_packet_type);
441 __dev_remove_pack(&fcoe->fip_packet_type);
442 synchronize_net();
443
444 /* Delete secondary MAC addresses */
445 memcpy(flogi_maddr, (u8[6]) FC_FCOE_FLOGI_MAC, ETH_ALEN);
446 dev_uc_del(netdev, flogi_maddr);
447 if (fip->spma)
448 dev_uc_del(netdev, fip->ctl_src_addr);
449 if (fip->mode == FIP_MODE_VN2VN) {
450 dev_mc_del(netdev, FIP_ALL_VN2VN_MACS);
451 dev_mc_del(netdev, FIP_ALL_P2P_MACS);
452 } else
453 dev_mc_del(netdev, FIP_ALL_ENODE_MACS);
454
Vasu Devf04ca1b2011-04-01 16:06:45 -0700455 /* Tell the LLD we are done w/ FCoE */
456 ops = netdev->netdev_ops;
457 if (ops->ndo_fcoe_disable) {
458 if (ops->ndo_fcoe_disable(netdev))
459 FCOE_NETDEV_DBG(netdev, "Failed to disable FCoE"
460 " specific feature for LLD.\n");
461 }
Neerav Parikhb2085a42011-06-20 16:59:51 -0700462
463 /* Release the self-reference taken during fcoe_interface_create() */
Vasu Devf04ca1b2011-04-01 16:06:45 -0700464 fcoe_interface_put(fcoe);
465}
466
467/**
Robert Love1875f272009-11-03 11:47:50 -0800468 * fcoe_fip_recv() - Handler for received FIP frames
469 * @skb: The receive skb
470 * @netdev: The associated net device
471 * @ptype: The packet_type structure which was used to register this handler
472 * @orig_dev: The original net_device the the skb was received on.
473 * (in case dev is a bond)
Vasu Devab6b85c2009-05-17 12:33:08 +0000474 *
475 * Returns: 0 for success
476 */
Robert Love1875f272009-11-03 11:47:50 -0800477static int fcoe_fip_recv(struct sk_buff *skb, struct net_device *netdev,
Vasu Devab6b85c2009-05-17 12:33:08 +0000478 struct packet_type *ptype,
479 struct net_device *orig_dev)
480{
Chris Leech259ad852009-08-25 13:59:41 -0700481 struct fcoe_interface *fcoe;
Vasu Devab6b85c2009-05-17 12:33:08 +0000482
Chris Leech259ad852009-08-25 13:59:41 -0700483 fcoe = container_of(ptype, struct fcoe_interface, fip_packet_type);
Chris Leech3fe9a0b2009-08-25 13:59:46 -0700484 fcoe_ctlr_recv(&fcoe->ctlr, skb);
Vasu Devab6b85c2009-05-17 12:33:08 +0000485 return 0;
486}
487
488/**
Vasu Dev980f5152011-07-27 15:11:05 -0700489 * fcoe_port_send() - Send an Ethernet-encapsulated FIP/FCoE frame
490 * @port: The FCoE port
491 * @skb: The FIP/FCoE packet to be sent
492 */
493static void fcoe_port_send(struct fcoe_port *port, struct sk_buff *skb)
494{
495 if (port->fcoe_pending_queue.qlen)
496 fcoe_check_wait_queue(port->lport, skb);
497 else if (fcoe_start_io(skb))
498 fcoe_check_wait_queue(port->lport, skb);
499}
500
501/**
Robert Love1875f272009-11-03 11:47:50 -0800502 * fcoe_fip_send() - Send an Ethernet-encapsulated FIP frame
503 * @fip: The FCoE controller
504 * @skb: The FIP packet to be sent
Vasu Devab6b85c2009-05-17 12:33:08 +0000505 */
506static void fcoe_fip_send(struct fcoe_ctlr *fip, struct sk_buff *skb)
507{
Chris Leech3fe9a0b2009-08-25 13:59:46 -0700508 skb->dev = fcoe_from_ctlr(fip)->netdev;
Vasu Dev980f5152011-07-27 15:11:05 -0700509 fcoe_port_send(lport_priv(fip->lp), skb);
Vasu Devab6b85c2009-05-17 12:33:08 +0000510}
511
512/**
Robert Love1875f272009-11-03 11:47:50 -0800513 * fcoe_update_src_mac() - Update the Ethernet MAC filters
514 * @lport: The local port to update the source MAC on
515 * @addr: Unicast MAC address to add
Vasu Devab6b85c2009-05-17 12:33:08 +0000516 *
517 * Remove any previously-set unicast MAC filter.
518 * Add secondary FCoE MAC address filter for our OUI.
519 */
Chris Leech11b56182009-11-03 11:46:29 -0800520static void fcoe_update_src_mac(struct fc_lport *lport, u8 *addr)
Vasu Devab6b85c2009-05-17 12:33:08 +0000521{
Chris Leech11b56182009-11-03 11:46:29 -0800522 struct fcoe_port *port = lport_priv(lport);
Bhanu Prakash Gollapudi8597ae82011-01-28 16:05:37 -0800523 struct fcoe_interface *fcoe = port->priv;
Vasu Devab6b85c2009-05-17 12:33:08 +0000524
Vasu Devab6b85c2009-05-17 12:33:08 +0000525 rtnl_lock();
Chris Leech11b56182009-11-03 11:46:29 -0800526 if (!is_zero_ether_addr(port->data_src_addr))
Jiri Pirkoa748ee22010-04-01 21:22:09 +0000527 dev_uc_del(fcoe->netdev, port->data_src_addr);
Chris Leech11b56182009-11-03 11:46:29 -0800528 if (!is_zero_ether_addr(addr))
Jiri Pirkoa748ee22010-04-01 21:22:09 +0000529 dev_uc_add(fcoe->netdev, addr);
Chris Leech11b56182009-11-03 11:46:29 -0800530 memcpy(port->data_src_addr, addr, ETH_ALEN);
Vasu Devab6b85c2009-05-17 12:33:08 +0000531 rtnl_unlock();
532}
533
534/**
Chris Leech11b56182009-11-03 11:46:29 -0800535 * fcoe_get_src_mac() - return the Ethernet source address for an lport
536 * @lport: libfc lport
537 */
538static u8 *fcoe_get_src_mac(struct fc_lport *lport)
539{
540 struct fcoe_port *port = lport_priv(lport);
541
542 return port->data_src_addr;
543}
544
545/**
Robert Love1875f272009-11-03 11:47:50 -0800546 * fcoe_lport_config() - Set up a local port
547 * @lport: The local port to be setup
Vasu Dev7f349142009-03-27 09:06:31 -0700548 *
549 * Returns: 0 for success
550 */
Robert Love1875f272009-11-03 11:47:50 -0800551static int fcoe_lport_config(struct fc_lport *lport)
Vasu Dev7f349142009-03-27 09:06:31 -0700552{
Robert Love1875f272009-11-03 11:47:50 -0800553 lport->link_up = 0;
554 lport->qfull = 0;
555 lport->max_retry_count = 3;
556 lport->max_rport_retry_count = 3;
557 lport->e_d_tov = 2 * 1000; /* FC-FS default */
558 lport->r_a_tov = 2 * 2 * 1000;
559 lport->service_params = (FCP_SPPF_INIT_FCN | FCP_SPPF_RD_XRDY_DIS |
560 FCP_SPPF_RETRY | FCP_SPPF_CONF_COMPL);
561 lport->does_npiv = 1;
Vasu Dev7f349142009-03-27 09:06:31 -0700562
Robert Love1875f272009-11-03 11:47:50 -0800563 fc_lport_init_stats(lport);
Vasu Dev7f349142009-03-27 09:06:31 -0700564
565 /* lport fc_lport related configuration */
Robert Love1875f272009-11-03 11:47:50 -0800566 fc_lport_config(lport);
Vasu Dev7f349142009-03-27 09:06:31 -0700567
568 /* offload related configuration */
Robert Love1875f272009-11-03 11:47:50 -0800569 lport->crc_offload = 0;
570 lport->seq_offload = 0;
571 lport->lro_enabled = 0;
572 lport->lro_xid = 0;
573 lport->lso_max = 0;
Vasu Dev7f349142009-03-27 09:06:31 -0700574
575 return 0;
576}
577
578/**
Yi Zou54a5b212010-07-20 15:21:17 -0700579 * fcoe_netdev_features_change - Updates the lport's offload flags based
580 * on the LLD netdev's FCoE feature flags
581 */
582static void fcoe_netdev_features_change(struct fc_lport *lport,
583 struct net_device *netdev)
584{
585 mutex_lock(&lport->lp_mutex);
586
587 if (netdev->features & NETIF_F_SG)
588 lport->sg_supp = 1;
589 else
590 lport->sg_supp = 0;
591
592 if (netdev->features & NETIF_F_FCOE_CRC) {
593 lport->crc_offload = 1;
594 FCOE_NETDEV_DBG(netdev, "Supports FCCRC offload\n");
595 } else {
596 lport->crc_offload = 0;
597 }
598
599 if (netdev->features & NETIF_F_FSO) {
600 lport->seq_offload = 1;
601 lport->lso_max = netdev->gso_max_size;
602 FCOE_NETDEV_DBG(netdev, "Supports LSO for max len 0x%x\n",
603 lport->lso_max);
604 } else {
605 lport->seq_offload = 0;
606 lport->lso_max = 0;
607 }
608
609 if (netdev->fcoe_ddp_xid) {
610 lport->lro_enabled = 1;
611 lport->lro_xid = netdev->fcoe_ddp_xid;
612 FCOE_NETDEV_DBG(netdev, "Supports LRO for max xid 0x%x\n",
613 lport->lro_xid);
614 } else {
615 lport->lro_enabled = 0;
616 lport->lro_xid = 0;
617 }
618
619 mutex_unlock(&lport->lp_mutex);
620}
621
622/**
Robert Love1875f272009-11-03 11:47:50 -0800623 * fcoe_netdev_config() - Set up net devive for SW FCoE
624 * @lport: The local port that is associated with the net device
625 * @netdev: The associated net device
Vasu Dev7f349142009-03-27 09:06:31 -0700626 *
Robert Love1875f272009-11-03 11:47:50 -0800627 * Must be called after fcoe_lport_config() as it will use local port mutex
Vasu Dev7f349142009-03-27 09:06:31 -0700628 *
Robert Love1875f272009-11-03 11:47:50 -0800629 * Returns: 0 for success
Vasu Dev7f349142009-03-27 09:06:31 -0700630 */
Robert Love1875f272009-11-03 11:47:50 -0800631static int fcoe_netdev_config(struct fc_lport *lport, struct net_device *netdev)
Vasu Dev7f349142009-03-27 09:06:31 -0700632{
633 u32 mfs;
634 u64 wwnn, wwpn;
Chris Leech25024982009-08-25 13:59:35 -0700635 struct fcoe_interface *fcoe;
Chris Leech014f5c32009-08-25 13:59:30 -0700636 struct fcoe_port *port;
Vasu Dev7f349142009-03-27 09:06:31 -0700637
638 /* Setup lport private data to point to fcoe softc */
Robert Love1875f272009-11-03 11:47:50 -0800639 port = lport_priv(lport);
Bhanu Prakash Gollapudi8597ae82011-01-28 16:05:37 -0800640 fcoe = port->priv;
Vasu Dev7f349142009-03-27 09:06:31 -0700641
642 /*
643 * Determine max frame size based on underlying device and optional
644 * user-configured limit. If the MFS is too low, fcoe_link_ok()
645 * will return 0, so do this first.
646 */
Yi Zou7221d7e2009-10-21 16:27:52 -0700647 mfs = netdev->mtu;
648 if (netdev->features & NETIF_F_FCOE_MTU) {
649 mfs = FCOE_MTU;
650 FCOE_NETDEV_DBG(netdev, "Supports FCOE_MTU of %d bytes\n", mfs);
651 }
652 mfs -= (sizeof(struct fcoe_hdr) + sizeof(struct fcoe_crc_eof));
Robert Love1875f272009-11-03 11:47:50 -0800653 if (fc_set_mfs(lport, mfs))
Vasu Dev7f349142009-03-27 09:06:31 -0700654 return -EINVAL;
655
Vasu Dev7f349142009-03-27 09:06:31 -0700656 /* offload features support */
Yi Zou54a5b212010-07-20 15:21:17 -0700657 fcoe_netdev_features_change(lport, netdev);
Vasu Dev7f349142009-03-27 09:06:31 -0700658
Chris Leech014f5c32009-08-25 13:59:30 -0700659 skb_queue_head_init(&port->fcoe_pending_queue);
660 port->fcoe_pending_queue_active = 0;
Robert Love1875f272009-11-03 11:47:50 -0800661 setup_timer(&port->timer, fcoe_queue_timer, (unsigned long)lport);
Vasu Dev7f349142009-03-27 09:06:31 -0700662
Robert Love5e4f8fe2010-05-07 15:18:35 -0700663 fcoe_link_speed_update(lport);
664
Robert Love1875f272009-11-03 11:47:50 -0800665 if (!lport->vport) {
Yi Zoudcece412009-11-20 15:22:21 -0800666 if (fcoe_get_wwn(netdev, &wwnn, NETDEV_FCOE_WWNN))
667 wwnn = fcoe_wwn_from_mac(fcoe->ctlr.ctl_src_addr, 1, 0);
Robert Love1875f272009-11-03 11:47:50 -0800668 fc_set_wwnn(lport, wwnn);
Yi Zoudcece412009-11-20 15:22:21 -0800669 if (fcoe_get_wwn(netdev, &wwpn, NETDEV_FCOE_WWPN))
670 wwpn = fcoe_wwn_from_mac(fcoe->ctlr.ctl_src_addr,
Vasu Devcf4aebc2010-07-20 15:21:22 -0700671 2, 0);
Robert Love1875f272009-11-03 11:47:50 -0800672 fc_set_wwpn(lport, wwpn);
Chris Leech9a057532009-11-03 11:46:40 -0800673 }
Vasu Dev7f349142009-03-27 09:06:31 -0700674
Vasu Dev7f349142009-03-27 09:06:31 -0700675 return 0;
676}
677
678/**
Robert Love1875f272009-11-03 11:47:50 -0800679 * fcoe_shost_config() - Set up the SCSI host associated with a local port
680 * @lport: The local port
Robert Love1875f272009-11-03 11:47:50 -0800681 * @dev: The device associated with the SCSI host
Vasu Dev7f349142009-03-27 09:06:31 -0700682 *
683 * Must be called after fcoe_lport_config() and fcoe_netdev_config()
684 *
Robert Love1875f272009-11-03 11:47:50 -0800685 * Returns: 0 for success
Vasu Dev7f349142009-03-27 09:06:31 -0700686 */
Vasu Dev8ba00a42010-04-09 14:22:54 -0700687static int fcoe_shost_config(struct fc_lport *lport, struct device *dev)
Vasu Dev7f349142009-03-27 09:06:31 -0700688{
689 int rc = 0;
690
691 /* lport scsi host config */
Robert Love1875f272009-11-03 11:47:50 -0800692 lport->host->max_lun = FCOE_MAX_LUN;
693 lport->host->max_id = FCOE_MAX_FCP_TARGET;
694 lport->host->max_channel = 0;
Vasu Devda87bfa2010-04-09 14:22:59 -0700695 lport->host->max_cmd_len = FCOE_MAX_CMD_LEN;
696
Robert Love1875f272009-11-03 11:47:50 -0800697 if (lport->vport)
Yi Zou8ca86f82011-01-28 16:05:11 -0800698 lport->host->transportt = fcoe_vport_scsi_transport;
Chris Leeche9084bb2009-11-03 11:46:34 -0800699 else
Yi Zou8ca86f82011-01-28 16:05:11 -0800700 lport->host->transportt = fcoe_nport_scsi_transport;
Vasu Dev7f349142009-03-27 09:06:31 -0700701
702 /* add the new host to the SCSI-ml */
Robert Love1875f272009-11-03 11:47:50 -0800703 rc = scsi_add_host(lport->host, dev);
Vasu Dev7f349142009-03-27 09:06:31 -0700704 if (rc) {
Robert Love1875f272009-11-03 11:47:50 -0800705 FCOE_NETDEV_DBG(fcoe_netdev(lport), "fcoe_shost_config: "
Robert Loved5488eb2009-06-10 15:30:59 -0700706 "error on scsi_add_host\n");
Vasu Dev7f349142009-03-27 09:06:31 -0700707 return rc;
708 }
Chris Leech9a057532009-11-03 11:46:40 -0800709
Robert Love1875f272009-11-03 11:47:50 -0800710 if (!lport->vport)
Alexey Dobriyan4be929b2010-05-24 14:33:03 -0700711 fc_host_max_npiv_vports(lport->host) = USHRT_MAX;
Chris Leech9a057532009-11-03 11:46:40 -0800712
Robert Love1875f272009-11-03 11:47:50 -0800713 snprintf(fc_host_symbolic_name(lport->host), FC_SYMBOLIC_NAME_SIZE,
Chris Leech5baa17c2009-11-03 11:46:56 -0800714 "%s v%s over %s", FCOE_NAME, FCOE_VERSION,
Robert Love1875f272009-11-03 11:47:50 -0800715 fcoe_netdev(lport)->name);
Vasu Dev7f349142009-03-27 09:06:31 -0700716
717 return 0;
718}
719
Robert Love1875f272009-11-03 11:47:50 -0800720/**
721 * fcoe_oem_match() - The match routine for the offloaded exchange manager
722 * @fp: The I/O frame
Vasu Devd7179682009-07-29 17:05:21 -0700723 *
Robert Love1875f272009-11-03 11:47:50 -0800724 * This routine will be associated with an exchange manager (EM). When
725 * the libfc exchange handling code is looking for an EM to use it will
726 * call this routine and pass it the frame that it wishes to send. This
727 * routine will return True if the associated EM is to be used and False
728 * if the echange code should continue looking for an EM.
729 *
730 * The offload EM that this routine is associated with will handle any
731 * packets that are for SCSI read requests.
732 *
Kiran Patil1ff99182011-06-20 16:59:15 -0700733 * This has been enhanced to work when FCoE stack is operating in target
734 * mode.
735 *
Robert Love1875f272009-11-03 11:47:50 -0800736 * Returns: True for read types I/O, otherwise returns false.
Vasu Devd7179682009-07-29 17:05:21 -0700737 */
738bool fcoe_oem_match(struct fc_frame *fp)
739{
Kiran Patil1ff99182011-06-20 16:59:15 -0700740 struct fc_frame_header *fh = fc_frame_header_get(fp);
741 struct fcp_cmnd *fcp;
742
743 if (fc_fcp_is_read(fr_fsp(fp)) &&
744 (fr_fsp(fp)->data_len > fcoe_ddp_min))
745 return true;
746 else if (!(ntoh24(fh->fh_f_ctl) & FC_FC_EX_CTX)) {
747 fcp = fc_frame_payload_get(fp, sizeof(*fcp));
748 if (ntohs(fh->fh_rx_id) == FC_XID_UNKNOWN &&
749 fcp && (ntohl(fcp->fc_dl) > fcoe_ddp_min) &&
750 (fcp->fc_flags & FCP_CFL_WRDATA))
751 return true;
752 }
753 return false;
Vasu Devd7179682009-07-29 17:05:21 -0700754}
755
Vasu Dev7f349142009-03-27 09:06:31 -0700756/**
Robert Love1875f272009-11-03 11:47:50 -0800757 * fcoe_em_config() - Allocate and configure an exchange manager
758 * @lport: The local port that the new EM will be associated with
Vasu Dev7f349142009-03-27 09:06:31 -0700759 *
Robert Love1875f272009-11-03 11:47:50 -0800760 * Returns: 0 on success
Vasu Dev7f349142009-03-27 09:06:31 -0700761 */
Robert Love1875f272009-11-03 11:47:50 -0800762static inline int fcoe_em_config(struct fc_lport *lport)
Vasu Dev7f349142009-03-27 09:06:31 -0700763{
Robert Love1875f272009-11-03 11:47:50 -0800764 struct fcoe_port *port = lport_priv(lport);
Bhanu Prakash Gollapudi8597ae82011-01-28 16:05:37 -0800765 struct fcoe_interface *fcoe = port->priv;
Chris Leech25024982009-08-25 13:59:35 -0700766 struct fcoe_interface *oldfcoe = NULL;
Vasu Dev1d1b88d2009-07-29 17:05:45 -0700767 struct net_device *old_real_dev, *cur_real_dev;
Vasu Devd7179682009-07-29 17:05:21 -0700768 u16 min_xid = FCOE_MIN_XID;
769 u16 max_xid = FCOE_MAX_XID;
770
771 /*
772 * Check if need to allocate an em instance for
773 * offload exchange ids to be shared across all VN_PORTs/lport.
774 */
Robert Love1875f272009-11-03 11:47:50 -0800775 if (!lport->lro_enabled || !lport->lro_xid ||
776 (lport->lro_xid >= max_xid)) {
777 lport->lro_xid = 0;
Vasu Devd7179682009-07-29 17:05:21 -0700778 goto skip_oem;
779 }
780
781 /*
782 * Reuse existing offload em instance in case
Vasu Dev1d1b88d2009-07-29 17:05:45 -0700783 * it is already allocated on real eth device
Vasu Devd7179682009-07-29 17:05:21 -0700784 */
Chris Leech25024982009-08-25 13:59:35 -0700785 if (fcoe->netdev->priv_flags & IFF_802_1Q_VLAN)
786 cur_real_dev = vlan_dev_real_dev(fcoe->netdev);
Vasu Dev1d1b88d2009-07-29 17:05:45 -0700787 else
Chris Leech25024982009-08-25 13:59:35 -0700788 cur_real_dev = fcoe->netdev;
Vasu Dev1d1b88d2009-07-29 17:05:45 -0700789
Chris Leech25024982009-08-25 13:59:35 -0700790 list_for_each_entry(oldfcoe, &fcoe_hostlist, list) {
Chris Leech25024982009-08-25 13:59:35 -0700791 if (oldfcoe->netdev->priv_flags & IFF_802_1Q_VLAN)
792 old_real_dev = vlan_dev_real_dev(oldfcoe->netdev);
Vasu Dev1d1b88d2009-07-29 17:05:45 -0700793 else
Chris Leech25024982009-08-25 13:59:35 -0700794 old_real_dev = oldfcoe->netdev;
Vasu Dev1d1b88d2009-07-29 17:05:45 -0700795
796 if (cur_real_dev == old_real_dev) {
Chris Leech991cbb62009-08-25 13:59:51 -0700797 fcoe->oem = oldfcoe->oem;
Vasu Devd7179682009-07-29 17:05:21 -0700798 break;
799 }
800 }
801
Chris Leech991cbb62009-08-25 13:59:51 -0700802 if (fcoe->oem) {
Robert Love1875f272009-11-03 11:47:50 -0800803 if (!fc_exch_mgr_add(lport, fcoe->oem, fcoe_oem_match)) {
Vasu Devd7179682009-07-29 17:05:21 -0700804 printk(KERN_ERR "fcoe_em_config: failed to add "
805 "offload em:%p on interface:%s\n",
Chris Leech991cbb62009-08-25 13:59:51 -0700806 fcoe->oem, fcoe->netdev->name);
Vasu Devd7179682009-07-29 17:05:21 -0700807 return -ENOMEM;
808 }
809 } else {
Robert Love1875f272009-11-03 11:47:50 -0800810 fcoe->oem = fc_exch_mgr_alloc(lport, FC_CLASS_3,
811 FCOE_MIN_XID, lport->lro_xid,
812 fcoe_oem_match);
Chris Leech991cbb62009-08-25 13:59:51 -0700813 if (!fcoe->oem) {
Vasu Devd7179682009-07-29 17:05:21 -0700814 printk(KERN_ERR "fcoe_em_config: failed to allocate "
815 "em for offload exches on interface:%s\n",
Chris Leech25024982009-08-25 13:59:35 -0700816 fcoe->netdev->name);
Vasu Devd7179682009-07-29 17:05:21 -0700817 return -ENOMEM;
818 }
819 }
820
821 /*
822 * Exclude offload EM xid range from next EM xid range.
823 */
Robert Love1875f272009-11-03 11:47:50 -0800824 min_xid += lport->lro_xid + 1;
Vasu Devd7179682009-07-29 17:05:21 -0700825
826skip_oem:
Robert Love1875f272009-11-03 11:47:50 -0800827 if (!fc_exch_mgr_alloc(lport, FC_CLASS_3, min_xid, max_xid, NULL)) {
Vasu Devd7179682009-07-29 17:05:21 -0700828 printk(KERN_ERR "fcoe_em_config: failed to "
Chris Leech25024982009-08-25 13:59:35 -0700829 "allocate em on interface %s\n", fcoe->netdev->name);
Vasu Dev7f349142009-03-27 09:06:31 -0700830 return -ENOMEM;
Vasu Devd7179682009-07-29 17:05:21 -0700831 }
Vasu Dev7f349142009-03-27 09:06:31 -0700832
833 return 0;
834}
835
836/**
Robert Love1875f272009-11-03 11:47:50 -0800837 * fcoe_if_destroy() - Tear down a SW FCoE instance
838 * @lport: The local port to be destroyed
Vasu Dev34ce27b2010-05-07 15:18:46 -0700839 *
Vasu Dev7f349142009-03-27 09:06:31 -0700840 */
Chris Leechaf7f85d2009-08-25 13:59:24 -0700841static void fcoe_if_destroy(struct fc_lport *lport)
Vasu Dev7f349142009-03-27 09:06:31 -0700842{
Neerav Parikhb2085a42011-06-20 16:59:51 -0700843 struct fcoe_port *port = lport_priv(lport);
844 struct fcoe_interface *fcoe = port->priv;
845 struct net_device *netdev = fcoe->netdev;
846
847 FCOE_NETDEV_DBG(netdev, "Destroying interface\n");
848
849 /* Logout of the fabric */
850 fc_fabric_logoff(lport);
851
852 /* Cleanup the fc_lport */
853 fc_lport_destroy(lport);
854
855 /* Stop the transmit retry timer */
856 del_timer_sync(&port->timer);
857
858 /* Free existing transmit skbs */
859 fcoe_clean_pending_queue(lport);
860
861 rtnl_lock();
862 if (!is_zero_ether_addr(port->data_src_addr))
863 dev_uc_del(netdev, port->data_src_addr);
864 rtnl_unlock();
865
866 /* Release reference held in fcoe_if_create() */
867 fcoe_interface_put(fcoe);
868
Chris Leech54b649f2009-08-25 14:00:07 -0700869 /* Free queued packets for the per-CPU receive threads */
870 fcoe_percpu_clean(lport);
871
Vasu Dev7f349142009-03-27 09:06:31 -0700872 /* Detach from the scsi-ml */
Chris Leechaf7f85d2009-08-25 13:59:24 -0700873 fc_remove_host(lport->host);
874 scsi_remove_host(lport->host);
Vasu Dev7f349142009-03-27 09:06:31 -0700875
Yi Zou80e736f2010-11-30 16:18:07 -0800876 /* Destroy lport scsi_priv */
877 fc_fcp_destroy(lport);
878
Vasu Dev7f349142009-03-27 09:06:31 -0700879 /* There are no more rports or I/O, free the EM */
Chris Leechaf7f85d2009-08-25 13:59:24 -0700880 fc_exch_mgr_free(lport);
Vasu Dev7f349142009-03-27 09:06:31 -0700881
Vasu Dev7f349142009-03-27 09:06:31 -0700882 /* Free memory used by statistical counters */
Chris Leechaf7f85d2009-08-25 13:59:24 -0700883 fc_lport_free_stats(lport);
Vasu Dev7f349142009-03-27 09:06:31 -0700884
Chris Leech2e70e242009-08-25 14:00:23 -0700885 /* Release the Scsi_Host */
Chris Leechaf7f85d2009-08-25 13:59:24 -0700886 scsi_host_put(lport->host);
Vasu Dev7f349142009-03-27 09:06:31 -0700887}
888
Robert Love1875f272009-11-03 11:47:50 -0800889/**
890 * fcoe_ddp_setup() - Call a LLD's ddp_setup through the net device
891 * @lport: The local port to setup DDP for
892 * @xid: The exchange ID for this DDP transfer
893 * @sgl: The scatterlist describing this transfer
894 * @sgc: The number of sg items
Vasu Dev7f349142009-03-27 09:06:31 -0700895 *
Robert Love1875f272009-11-03 11:47:50 -0800896 * Returns: 0 if the DDP context was not configured
Vasu Dev7f349142009-03-27 09:06:31 -0700897 */
Robert Love1875f272009-11-03 11:47:50 -0800898static int fcoe_ddp_setup(struct fc_lport *lport, u16 xid,
899 struct scatterlist *sgl, unsigned int sgc)
Vasu Dev7f349142009-03-27 09:06:31 -0700900{
Robert Love1875f272009-11-03 11:47:50 -0800901 struct net_device *netdev = fcoe_netdev(lport);
Vasu Dev7f349142009-03-27 09:06:31 -0700902
Robert Love1875f272009-11-03 11:47:50 -0800903 if (netdev->netdev_ops->ndo_fcoe_ddp_setup)
904 return netdev->netdev_ops->ndo_fcoe_ddp_setup(netdev,
905 xid, sgl,
906 sgc);
Vasu Dev7f349142009-03-27 09:06:31 -0700907
908 return 0;
909}
910
Vasu Dev7f349142009-03-27 09:06:31 -0700911/**
Yi Zou71f89492011-06-20 16:59:10 -0700912 * fcoe_ddp_target() - Call a LLD's ddp_target through the net device
913 * @lport: The local port to setup DDP for
914 * @xid: The exchange ID for this DDP transfer
915 * @sgl: The scatterlist describing this transfer
916 * @sgc: The number of sg items
917 *
918 * Returns: 0 if the DDP context was not configured
919 */
920static int fcoe_ddp_target(struct fc_lport *lport, u16 xid,
921 struct scatterlist *sgl, unsigned int sgc)
922{
923 struct net_device *netdev = fcoe_netdev(lport);
924
925 if (netdev->netdev_ops->ndo_fcoe_ddp_target)
926 return netdev->netdev_ops->ndo_fcoe_ddp_target(netdev, xid,
927 sgl, sgc);
928
929 return 0;
930}
931
932
933/**
Robert Love1875f272009-11-03 11:47:50 -0800934 * fcoe_ddp_done() - Call a LLD's ddp_done through the net device
935 * @lport: The local port to complete DDP on
936 * @xid: The exchange ID for this DDP transfer
Vasu Dev7f349142009-03-27 09:06:31 -0700937 *
Robert Love1875f272009-11-03 11:47:50 -0800938 * Returns: the length of data that have been completed by DDP
939 */
940static int fcoe_ddp_done(struct fc_lport *lport, u16 xid)
941{
942 struct net_device *netdev = fcoe_netdev(lport);
943
944 if (netdev->netdev_ops->ndo_fcoe_ddp_done)
945 return netdev->netdev_ops->ndo_fcoe_ddp_done(netdev, xid);
946 return 0;
947}
948
949/**
950 * fcoe_if_create() - Create a FCoE instance on an interface
951 * @fcoe: The FCoE interface to create a local port on
952 * @parent: The device pointer to be the parent in sysfs for the SCSI host
953 * @npiv: Indicates if the port is a vport or not
Vasu Dev7f349142009-03-27 09:06:31 -0700954 *
Robert Love1875f272009-11-03 11:47:50 -0800955 * Creates a fc_lport instance and a Scsi_Host instance and configure them.
956 *
957 * Returns: The allocated fc_lport or an error pointer
Vasu Dev7f349142009-03-27 09:06:31 -0700958 */
Chris Leech030f4e02009-08-25 14:00:02 -0700959static struct fc_lport *fcoe_if_create(struct fcoe_interface *fcoe,
Chris Leech9a057532009-11-03 11:46:40 -0800960 struct device *parent, int npiv)
Vasu Dev7f349142009-03-27 09:06:31 -0700961{
Robert Love1875f272009-11-03 11:47:50 -0800962 struct net_device *netdev = fcoe->netdev;
Vasu Dev72fa3962011-02-25 15:03:01 -0800963 struct fc_lport *lport, *n_port;
Chris Leech014f5c32009-08-25 13:59:30 -0700964 struct fcoe_port *port;
Vasu Dev72fa3962011-02-25 15:03:01 -0800965 struct Scsi_Host *shost;
Robert Love1875f272009-11-03 11:47:50 -0800966 int rc;
Chris Leech9a057532009-11-03 11:46:40 -0800967 /*
968 * parent is only a vport if npiv is 1,
969 * but we'll only use vport in that case so go ahead and set it
970 */
971 struct fc_vport *vport = dev_to_vport(parent);
Vasu Dev7f349142009-03-27 09:06:31 -0700972
Robert Loved5488eb2009-06-10 15:30:59 -0700973 FCOE_NETDEV_DBG(netdev, "Create Interface\n");
Vasu Dev7f349142009-03-27 09:06:31 -0700974
Vasu Dev72fa3962011-02-25 15:03:01 -0800975 if (!npiv)
976 lport = libfc_host_alloc(&fcoe_shost_template, sizeof(*port));
977 else
978 lport = libfc_vport_create(vport, sizeof(*port));
979
Chris Leech86221962009-11-03 11:46:08 -0800980 if (!lport) {
Chris Leech014f5c32009-08-25 13:59:30 -0700981 FCOE_NETDEV_DBG(netdev, "Could not allocate host structure\n");
982 rc = -ENOMEM;
Chris Leech030f4e02009-08-25 14:00:02 -0700983 goto out;
Chris Leech014f5c32009-08-25 13:59:30 -0700984 }
Chris Leech014f5c32009-08-25 13:59:30 -0700985 port = lport_priv(lport);
Chris Leech2e70e242009-08-25 14:00:23 -0700986 port->lport = lport;
Bhanu Prakash Gollapudi8597ae82011-01-28 16:05:37 -0800987 port->priv = fcoe;
988 port->max_queue_depth = FCOE_MAX_QUEUE_DEPTH;
989 port->min_queue_depth = FCOE_MIN_QUEUE_DEPTH;
Chris Leech2e70e242009-08-25 14:00:23 -0700990 INIT_WORK(&port->destroy_work, fcoe_destroy_work);
Vasu Dev7f349142009-03-27 09:06:31 -0700991
Robert Love1875f272009-11-03 11:47:50 -0800992 /* configure a fc_lport including the exchange manager */
Chris Leechaf7f85d2009-08-25 13:59:24 -0700993 rc = fcoe_lport_config(lport);
Vasu Dev7f349142009-03-27 09:06:31 -0700994 if (rc) {
Robert Loved5488eb2009-06-10 15:30:59 -0700995 FCOE_NETDEV_DBG(netdev, "Could not configure lport for the "
996 "interface\n");
Vasu Dev7f349142009-03-27 09:06:31 -0700997 goto out_host_put;
998 }
999
Chris Leech9a057532009-11-03 11:46:40 -08001000 if (npiv) {
Chris Leech9f8f3aa2010-04-09 14:23:16 -07001001 FCOE_NETDEV_DBG(netdev, "Setting vport names, "
1002 "%16.16llx %16.16llx\n",
Robert Love1875f272009-11-03 11:47:50 -08001003 vport->node_name, vport->port_name);
Chris Leech9a057532009-11-03 11:46:40 -08001004 fc_set_wwnn(lport, vport->node_name);
1005 fc_set_wwpn(lport, vport->port_name);
1006 }
1007
Vasu Devab6b85c2009-05-17 12:33:08 +00001008 /* configure lport network properties */
Chris Leechaf7f85d2009-08-25 13:59:24 -07001009 rc = fcoe_netdev_config(lport, netdev);
Vasu Devab6b85c2009-05-17 12:33:08 +00001010 if (rc) {
Robert Loved5488eb2009-06-10 15:30:59 -07001011 FCOE_NETDEV_DBG(netdev, "Could not configure netdev for the "
1012 "interface\n");
Chris Leech54b649f2009-08-25 14:00:07 -07001013 goto out_lp_destroy;
Vasu Devab6b85c2009-05-17 12:33:08 +00001014 }
Joe Eykholt97c83892009-03-17 11:42:40 -07001015
Vasu Dev7f349142009-03-27 09:06:31 -07001016 /* configure lport scsi host properties */
Vasu Dev8ba00a42010-04-09 14:22:54 -07001017 rc = fcoe_shost_config(lport, parent);
Vasu Dev7f349142009-03-27 09:06:31 -07001018 if (rc) {
Robert Loved5488eb2009-06-10 15:30:59 -07001019 FCOE_NETDEV_DBG(netdev, "Could not configure shost for the "
1020 "interface\n");
Chris Leech54b649f2009-08-25 14:00:07 -07001021 goto out_lp_destroy;
Vasu Dev7f349142009-03-27 09:06:31 -07001022 }
1023
Vasu Dev7f349142009-03-27 09:06:31 -07001024 /* Initialize the library */
Joe Eykholte10f8c62010-07-20 15:20:30 -07001025 rc = fcoe_libfc_config(lport, &fcoe->ctlr, &fcoe_libfc_fcn_templ, 1);
Vasu Dev7f349142009-03-27 09:06:31 -07001026 if (rc) {
Robert Loved5488eb2009-06-10 15:30:59 -07001027 FCOE_NETDEV_DBG(netdev, "Could not configure libfc for the "
1028 "interface\n");
Vasu Dev7f349142009-03-27 09:06:31 -07001029 goto out_lp_destroy;
1030 }
1031
Vasu Dev72fa3962011-02-25 15:03:01 -08001032 /*
1033 * fcoe_em_alloc() and fcoe_hostlist_add() both
1034 * need to be atomic with respect to other changes to the
1035 * hostlist since fcoe_em_alloc() looks for an existing EM
1036 * instance on host list updated by fcoe_hostlist_add().
1037 *
1038 * This is currently handled through the fcoe_config_mutex
1039 * begin held.
1040 */
1041 if (!npiv)
Chris Leech9a057532009-11-03 11:46:40 -08001042 /* lport exch manager allocation */
1043 rc = fcoe_em_config(lport);
Vasu Dev72fa3962011-02-25 15:03:01 -08001044 else {
1045 shost = vport_to_shost(vport);
1046 n_port = shost_priv(shost);
1047 rc = fc_exch_mgr_list_clone(n_port, lport);
1048 }
1049
1050 if (rc) {
1051 FCOE_NETDEV_DBG(netdev, "Could not configure the EM\n");
1052 goto out_lp_destroy;
Vasu Dev96316092009-07-29 17:05:00 -07001053 }
1054
Chris Leech030f4e02009-08-25 14:00:02 -07001055 fcoe_interface_get(fcoe);
Chris Leechaf7f85d2009-08-25 13:59:24 -07001056 return lport;
Vasu Dev7f349142009-03-27 09:06:31 -07001057
1058out_lp_destroy:
Chris Leechaf7f85d2009-08-25 13:59:24 -07001059 fc_exch_mgr_free(lport);
Vasu Dev7f349142009-03-27 09:06:31 -07001060out_host_put:
Chris Leechaf7f85d2009-08-25 13:59:24 -07001061 scsi_host_put(lport->host);
1062out:
1063 return ERR_PTR(rc);
Vasu Dev7f349142009-03-27 09:06:31 -07001064}
1065
1066/**
Robert Love1875f272009-11-03 11:47:50 -08001067 * fcoe_if_init() - Initialization routine for fcoe.ko
Vasu Dev7f349142009-03-27 09:06:31 -07001068 *
Robert Love1875f272009-11-03 11:47:50 -08001069 * Attaches the SW FCoE transport to the FC transport
1070 *
1071 * Returns: 0 on success
Vasu Dev7f349142009-03-27 09:06:31 -07001072 */
1073static int __init fcoe_if_init(void)
1074{
1075 /* attach to scsi transport */
Yi Zou8ca86f82011-01-28 16:05:11 -08001076 fcoe_nport_scsi_transport =
1077 fc_attach_transport(&fcoe_nport_fc_functions);
1078 fcoe_vport_scsi_transport =
1079 fc_attach_transport(&fcoe_vport_fc_functions);
Vasu Dev7f349142009-03-27 09:06:31 -07001080
Yi Zou8ca86f82011-01-28 16:05:11 -08001081 if (!fcoe_nport_scsi_transport) {
Robert Loved5488eb2009-06-10 15:30:59 -07001082 printk(KERN_ERR "fcoe: Failed to attach to the FC transport\n");
Vasu Dev7f349142009-03-27 09:06:31 -07001083 return -ENODEV;
1084 }
1085
1086 return 0;
1087}
1088
1089/**
Robert Love1875f272009-11-03 11:47:50 -08001090 * fcoe_if_exit() - Tear down fcoe.ko
Vasu Dev7f349142009-03-27 09:06:31 -07001091 *
Robert Love1875f272009-11-03 11:47:50 -08001092 * Detaches the SW FCoE transport from the FC transport
1093 *
1094 * Returns: 0 on success
Vasu Dev7f349142009-03-27 09:06:31 -07001095 */
1096int __exit fcoe_if_exit(void)
1097{
Yi Zou8ca86f82011-01-28 16:05:11 -08001098 fc_release_transport(fcoe_nport_scsi_transport);
1099 fc_release_transport(fcoe_vport_scsi_transport);
1100 fcoe_nport_scsi_transport = NULL;
1101 fcoe_vport_scsi_transport = NULL;
Vasu Dev7f349142009-03-27 09:06:31 -07001102 return 0;
1103}
1104
Robert Love85b4aa42008-12-09 15:10:24 -08001105/**
Robert Love1875f272009-11-03 11:47:50 -08001106 * fcoe_percpu_thread_create() - Create a receive thread for an online CPU
1107 * @cpu: The CPU index of the CPU to create a receive thread for
Robert Love8976f422009-03-17 11:41:46 -07001108 */
1109static void fcoe_percpu_thread_create(unsigned int cpu)
1110{
1111 struct fcoe_percpu_s *p;
1112 struct task_struct *thread;
1113
1114 p = &per_cpu(fcoe_percpu, cpu);
1115
1116 thread = kthread_create(fcoe_percpu_receive_thread,
1117 (void *)p, "fcoethread/%d", cpu);
1118
Joe Eykholte7a51992009-08-25 14:04:08 -07001119 if (likely(!IS_ERR(thread))) {
Robert Love8976f422009-03-17 11:41:46 -07001120 kthread_bind(thread, cpu);
1121 wake_up_process(thread);
1122
1123 spin_lock_bh(&p->fcoe_rx_list.lock);
1124 p->thread = thread;
1125 spin_unlock_bh(&p->fcoe_rx_list.lock);
1126 }
1127}
1128
1129/**
Robert Love1875f272009-11-03 11:47:50 -08001130 * fcoe_percpu_thread_destroy() - Remove the receive thread of a CPU
1131 * @cpu: The CPU index of the CPU whose receive thread is to be destroyed
Robert Love8976f422009-03-17 11:41:46 -07001132 *
1133 * Destroys a per-CPU Rx thread. Any pending skbs are moved to the
1134 * current CPU's Rx thread. If the thread being destroyed is bound to
1135 * the CPU processing this context the skbs will be freed.
1136 */
1137static void fcoe_percpu_thread_destroy(unsigned int cpu)
1138{
1139 struct fcoe_percpu_s *p;
1140 struct task_struct *thread;
1141 struct page *crc_eof;
1142 struct sk_buff *skb;
1143#ifdef CONFIG_SMP
1144 struct fcoe_percpu_s *p0;
Joe Eykholtf018b732010-03-12 16:08:55 -08001145 unsigned targ_cpu = get_cpu();
Robert Love8976f422009-03-17 11:41:46 -07001146#endif /* CONFIG_SMP */
1147
Robert Loved5488eb2009-06-10 15:30:59 -07001148 FCOE_DBG("Destroying receive thread for CPU %d\n", cpu);
Robert Love8976f422009-03-17 11:41:46 -07001149
1150 /* Prevent any new skbs from being queued for this CPU. */
1151 p = &per_cpu(fcoe_percpu, cpu);
1152 spin_lock_bh(&p->fcoe_rx_list.lock);
1153 thread = p->thread;
1154 p->thread = NULL;
1155 crc_eof = p->crc_eof_page;
1156 p->crc_eof_page = NULL;
1157 p->crc_eof_offset = 0;
1158 spin_unlock_bh(&p->fcoe_rx_list.lock);
1159
1160#ifdef CONFIG_SMP
1161 /*
1162 * Don't bother moving the skb's if this context is running
1163 * on the same CPU that is having its thread destroyed. This
1164 * can easily happen when the module is removed.
1165 */
1166 if (cpu != targ_cpu) {
1167 p0 = &per_cpu(fcoe_percpu, targ_cpu);
1168 spin_lock_bh(&p0->fcoe_rx_list.lock);
1169 if (p0->thread) {
Robert Loved5488eb2009-06-10 15:30:59 -07001170 FCOE_DBG("Moving frames from CPU %d to CPU %d\n",
1171 cpu, targ_cpu);
Robert Love8976f422009-03-17 11:41:46 -07001172
1173 while ((skb = __skb_dequeue(&p->fcoe_rx_list)) != NULL)
1174 __skb_queue_tail(&p0->fcoe_rx_list, skb);
1175 spin_unlock_bh(&p0->fcoe_rx_list.lock);
1176 } else {
1177 /*
1178 * The targeted CPU is not initialized and cannot accept
Robert Love1875f272009-11-03 11:47:50 -08001179 * new skbs. Unlock the targeted CPU and drop the skbs
Robert Love8976f422009-03-17 11:41:46 -07001180 * on the CPU that is going offline.
1181 */
1182 while ((skb = __skb_dequeue(&p->fcoe_rx_list)) != NULL)
1183 kfree_skb(skb);
1184 spin_unlock_bh(&p0->fcoe_rx_list.lock);
1185 }
1186 } else {
1187 /*
1188 * This scenario occurs when the module is being removed
1189 * and all threads are being destroyed. skbs will continue
1190 * to be shifted from the CPU thread that is being removed
1191 * to the CPU thread associated with the CPU that is processing
1192 * the module removal. Once there is only one CPU Rx thread it
1193 * will reach this case and we will drop all skbs and later
1194 * stop the thread.
1195 */
1196 spin_lock_bh(&p->fcoe_rx_list.lock);
1197 while ((skb = __skb_dequeue(&p->fcoe_rx_list)) != NULL)
1198 kfree_skb(skb);
1199 spin_unlock_bh(&p->fcoe_rx_list.lock);
1200 }
Joe Eykholtf018b732010-03-12 16:08:55 -08001201 put_cpu();
Robert Love8976f422009-03-17 11:41:46 -07001202#else
1203 /*
Chris Leechdd3fd722009-04-21 16:27:36 -07001204 * This a non-SMP scenario where the singular Rx thread is
Robert Love8976f422009-03-17 11:41:46 -07001205 * being removed. Free all skbs and stop the thread.
1206 */
1207 spin_lock_bh(&p->fcoe_rx_list.lock);
1208 while ((skb = __skb_dequeue(&p->fcoe_rx_list)) != NULL)
1209 kfree_skb(skb);
1210 spin_unlock_bh(&p->fcoe_rx_list.lock);
1211#endif
1212
1213 if (thread)
1214 kthread_stop(thread);
1215
1216 if (crc_eof)
1217 put_page(crc_eof);
1218}
1219
1220/**
Robert Love1875f272009-11-03 11:47:50 -08001221 * fcoe_cpu_callback() - Handler for CPU hotplug events
1222 * @nfb: The callback data block
1223 * @action: The event triggering the callback
1224 * @hcpu: The index of the CPU that the event is for
Robert Love8976f422009-03-17 11:41:46 -07001225 *
Robert Love1875f272009-11-03 11:47:50 -08001226 * This creates or destroys per-CPU data for fcoe
Robert Love8976f422009-03-17 11:41:46 -07001227 *
1228 * Returns NOTIFY_OK always.
1229 */
1230static int fcoe_cpu_callback(struct notifier_block *nfb,
1231 unsigned long action, void *hcpu)
1232{
1233 unsigned cpu = (unsigned long)hcpu;
1234
1235 switch (action) {
1236 case CPU_ONLINE:
1237 case CPU_ONLINE_FROZEN:
Robert Loved5488eb2009-06-10 15:30:59 -07001238 FCOE_DBG("CPU %x online: Create Rx thread\n", cpu);
Robert Love8976f422009-03-17 11:41:46 -07001239 fcoe_percpu_thread_create(cpu);
1240 break;
1241 case CPU_DEAD:
1242 case CPU_DEAD_FROZEN:
Robert Loved5488eb2009-06-10 15:30:59 -07001243 FCOE_DBG("CPU %x offline: Remove Rx thread\n", cpu);
Robert Love8976f422009-03-17 11:41:46 -07001244 fcoe_percpu_thread_destroy(cpu);
1245 break;
1246 default:
1247 break;
1248 }
1249 return NOTIFY_OK;
1250}
1251
Robert Love8976f422009-03-17 11:41:46 -07001252/**
Kiran Patil064287e2011-06-20 16:59:20 -07001253 * fcoe_select_cpu() - Selects CPU to handle post-processing of incoming
1254 * command.
Kiran Patil064287e2011-06-20 16:59:20 -07001255 *
Vasu Devd2722812011-07-27 15:11:10 -07001256 * This routine selects next CPU based on cpumask to distribute
1257 * incoming requests in round robin.
Kiran Patil064287e2011-06-20 16:59:20 -07001258 *
Vasu Devd2722812011-07-27 15:11:10 -07001259 * Returns: int CPU number
Kiran Patil064287e2011-06-20 16:59:20 -07001260 */
Vasu Devd2722812011-07-27 15:11:10 -07001261static inline unsigned int fcoe_select_cpu(void)
Kiran Patil064287e2011-06-20 16:59:20 -07001262{
1263 static unsigned int selected_cpu;
1264
Vasu Devd2722812011-07-27 15:11:10 -07001265 selected_cpu = cpumask_next(selected_cpu, cpu_online_mask);
1266 if (selected_cpu >= nr_cpu_ids)
1267 selected_cpu = cpumask_first(cpu_online_mask);
1268
Kiran Patil064287e2011-06-20 16:59:20 -07001269 return selected_cpu;
1270}
1271
1272/**
Robert Love1875f272009-11-03 11:47:50 -08001273 * fcoe_rcv() - Receive packets from a net device
1274 * @skb: The received packet
1275 * @netdev: The net device that the packet was received on
1276 * @ptype: The packet type context
1277 * @olddev: The last device net device
Robert Love85b4aa42008-12-09 15:10:24 -08001278 *
Robert Love1875f272009-11-03 11:47:50 -08001279 * This routine is called by NET_RX_SOFTIRQ. It receives a packet, builds a
1280 * FC frame and passes the frame to libfc.
Robert Love85b4aa42008-12-09 15:10:24 -08001281 *
1282 * Returns: 0 for success
Robert Love34f42a02009-02-27 10:55:45 -08001283 */
Robert Love1875f272009-11-03 11:47:50 -08001284int fcoe_rcv(struct sk_buff *skb, struct net_device *netdev,
Robert Love85b4aa42008-12-09 15:10:24 -08001285 struct packet_type *ptype, struct net_device *olddev)
1286{
Robert Love1875f272009-11-03 11:47:50 -08001287 struct fc_lport *lport;
Robert Love85b4aa42008-12-09 15:10:24 -08001288 struct fcoe_rcv_info *fr;
Chris Leech259ad852009-08-25 13:59:41 -07001289 struct fcoe_interface *fcoe;
Robert Love85b4aa42008-12-09 15:10:24 -08001290 struct fc_frame_header *fh;
Robert Love85b4aa42008-12-09 15:10:24 -08001291 struct fcoe_percpu_s *fps;
Vasu Dev519e5132010-07-20 15:19:32 -07001292 struct ethhdr *eh;
Vasu Devb2f00912009-08-25 13:58:53 -07001293 unsigned int cpu;
Robert Love85b4aa42008-12-09 15:10:24 -08001294
Chris Leech259ad852009-08-25 13:59:41 -07001295 fcoe = container_of(ptype, struct fcoe_interface, fcoe_packet_type);
Robert Love1875f272009-11-03 11:47:50 -08001296 lport = fcoe->ctlr.lp;
1297 if (unlikely(!lport)) {
1298 FCOE_NETDEV_DBG(netdev, "Cannot find hba structure");
Robert Love85b4aa42008-12-09 15:10:24 -08001299 goto err2;
1300 }
Robert Love1875f272009-11-03 11:47:50 -08001301 if (!lport->link_up)
Joe Eykholt97c83892009-03-17 11:42:40 -07001302 goto err2;
Robert Love85b4aa42008-12-09 15:10:24 -08001303
Robert Love1875f272009-11-03 11:47:50 -08001304 FCOE_NETDEV_DBG(netdev, "skb_info: len:%d data_len:%d head:%p "
Robert Loved5488eb2009-06-10 15:30:59 -07001305 "data:%p tail:%p end:%p sum:%d dev:%s",
1306 skb->len, skb->data_len, skb->head, skb->data,
1307 skb_tail_pointer(skb), skb_end_pointer(skb),
1308 skb->csum, skb->dev ? skb->dev->name : "<NULL>");
Robert Love85b4aa42008-12-09 15:10:24 -08001309
Vasu Dev519e5132010-07-20 15:19:32 -07001310 eh = eth_hdr(skb);
Vasu Dev519e5132010-07-20 15:19:32 -07001311
1312 if (is_fip_mode(&fcoe->ctlr) &&
1313 compare_ether_addr(eh->h_source, fcoe->ctlr.dest_addr)) {
1314 FCOE_NETDEV_DBG(netdev, "wrong source mac address:%pM\n",
1315 eh->h_source);
Robert Love85b4aa42008-12-09 15:10:24 -08001316 goto err;
1317 }
1318
1319 /*
1320 * Check for minimum frame length, and make sure required FCoE
1321 * and FC headers are pulled into the linear data area.
1322 */
1323 if (unlikely((skb->len < FCOE_MIN_FRAME) ||
Robert Love1875f272009-11-03 11:47:50 -08001324 !pskb_may_pull(skb, FCOE_HEADER_LEN)))
Robert Love85b4aa42008-12-09 15:10:24 -08001325 goto err;
1326
1327 skb_set_transport_header(skb, sizeof(struct fcoe_hdr));
1328 fh = (struct fc_frame_header *) skb_transport_header(skb);
1329
Robert Love0ee31cb2010-10-08 17:12:46 -07001330 if (ntoh24(&eh->h_dest[3]) != ntoh24(fh->fh_d_id)) {
1331 FCOE_NETDEV_DBG(netdev, "FC frame d_id mismatch with MAC:%pM\n",
1332 eh->h_dest);
1333 goto err;
1334 }
1335
Robert Love85b4aa42008-12-09 15:10:24 -08001336 fr = fcoe_dev_from_skb(skb);
Robert Love1875f272009-11-03 11:47:50 -08001337 fr->fr_dev = lport;
Robert Love5e5e92d2009-03-17 11:41:35 -07001338
Robert Love85b4aa42008-12-09 15:10:24 -08001339 /*
Vasu Devb2f00912009-08-25 13:58:53 -07001340 * In case the incoming frame's exchange is originated from
1341 * the initiator, then received frame's exchange id is ANDed
1342 * with fc_cpu_mask bits to get the same cpu on which exchange
Vasu Devd2722812011-07-27 15:11:10 -07001343 * was originated, otherwise select cpu using rx exchange id
1344 * or fcoe_select_cpu().
Robert Love85b4aa42008-12-09 15:10:24 -08001345 */
Vasu Devb2f00912009-08-25 13:58:53 -07001346 if (ntoh24(fh->fh_f_ctl) & FC_FC_EX_CTX)
1347 cpu = ntohs(fh->fh_ox_id) & fc_cpu_mask;
Kiran Patil064287e2011-06-20 16:59:20 -07001348 else {
Vasu Devd2722812011-07-27 15:11:10 -07001349 if (ntohs(fh->fh_rx_id) == FC_XID_UNKNOWN)
1350 cpu = fcoe_select_cpu();
1351 else
Kiran Patil29bdd2b2011-06-20 16:59:25 -07001352 cpu = ntohs(fh->fh_rx_id) & fc_cpu_mask;
Kiran Patil064287e2011-06-20 16:59:20 -07001353 }
Vasu Dev324f6672011-07-27 15:10:39 -07001354
1355 if (cpu >= nr_cpu_ids)
1356 goto err;
1357
Robert Love8976f422009-03-17 11:41:46 -07001358 fps = &per_cpu(fcoe_percpu, cpu);
Robert Love85b4aa42008-12-09 15:10:24 -08001359 spin_lock_bh(&fps->fcoe_rx_list.lock);
Robert Love8976f422009-03-17 11:41:46 -07001360 if (unlikely(!fps->thread)) {
1361 /*
1362 * The targeted CPU is not ready, let's target
1363 * the first CPU now. For non-SMP systems this
1364 * will check the same CPU twice.
1365 */
Robert Love1875f272009-11-03 11:47:50 -08001366 FCOE_NETDEV_DBG(netdev, "CPU is online, but no receive thread "
Robert Loved5488eb2009-06-10 15:30:59 -07001367 "ready for incoming skb- using first online "
1368 "CPU.\n");
Robert Love8976f422009-03-17 11:41:46 -07001369
1370 spin_unlock_bh(&fps->fcoe_rx_list.lock);
Rusty Russell69571772009-12-17 11:43:14 -06001371 cpu = cpumask_first(cpu_online_mask);
Robert Love8976f422009-03-17 11:41:46 -07001372 fps = &per_cpu(fcoe_percpu, cpu);
1373 spin_lock_bh(&fps->fcoe_rx_list.lock);
1374 if (!fps->thread) {
1375 spin_unlock_bh(&fps->fcoe_rx_list.lock);
1376 goto err;
1377 }
1378 }
1379
1380 /*
1381 * We now have a valid CPU that we're targeting for
1382 * this skb. We also have this receive thread locked,
1383 * so we're free to queue skbs into it's queue.
1384 */
Robert Love85b4aa42008-12-09 15:10:24 -08001385
Chris Leech859b7b62009-11-20 14:54:47 -08001386 /* If this is a SCSI-FCP frame, and this is already executing on the
1387 * correct CPU, and the queue for this CPU is empty, then go ahead
1388 * and process the frame directly in the softirq context.
1389 * This lets us process completions without context switching from the
1390 * NET_RX softirq, to our receive processing thread, and then back to
1391 * BLOCK softirq context.
1392 */
1393 if (fh->fh_type == FC_TYPE_FCP &&
1394 cpu == smp_processor_id() &&
1395 skb_queue_empty(&fps->fcoe_rx_list)) {
1396 spin_unlock_bh(&fps->fcoe_rx_list.lock);
1397 fcoe_recv_frame(skb);
1398 } else {
1399 __skb_queue_tail(&fps->fcoe_rx_list, skb);
1400 if (fps->fcoe_rx_list.qlen == 1)
1401 wake_up_process(fps->thread);
1402 spin_unlock_bh(&fps->fcoe_rx_list.lock);
1403 }
Robert Love85b4aa42008-12-09 15:10:24 -08001404
1405 return 0;
1406err:
Joe Eykholtf018b732010-03-12 16:08:55 -08001407 per_cpu_ptr(lport->dev_stats, get_cpu())->ErrorFrames++;
1408 put_cpu();
Robert Love85b4aa42008-12-09 15:10:24 -08001409err2:
1410 kfree_skb(skb);
1411 return -1;
1412}
Robert Love85b4aa42008-12-09 15:10:24 -08001413
1414/**
Bhanu Prakash Gollapudi8597ae82011-01-28 16:05:37 -08001415 * fcoe_alloc_paged_crc_eof() - Allocate a page to be used for the trailer CRC
Robert Love1875f272009-11-03 11:47:50 -08001416 * @skb: The packet to be transmitted
1417 * @tlen: The total length of the trailer
1418 *
Robert Love85b4aa42008-12-09 15:10:24 -08001419 * Returns: 0 for success
Robert Love34f42a02009-02-27 10:55:45 -08001420 */
Bhanu Prakash Gollapudi8597ae82011-01-28 16:05:37 -08001421static int fcoe_alloc_paged_crc_eof(struct sk_buff *skb, int tlen)
Robert Love85b4aa42008-12-09 15:10:24 -08001422{
1423 struct fcoe_percpu_s *fps;
Bhanu Prakash Gollapudi8597ae82011-01-28 16:05:37 -08001424 int rc;
Robert Love85b4aa42008-12-09 15:10:24 -08001425
Robert Love5e5e92d2009-03-17 11:41:35 -07001426 fps = &get_cpu_var(fcoe_percpu);
Bhanu Prakash Gollapudi8597ae82011-01-28 16:05:37 -08001427 rc = fcoe_get_paged_crc_eof(skb, tlen, fps);
Robert Love5e5e92d2009-03-17 11:41:35 -07001428 put_cpu_var(fcoe_percpu);
Robert Love85b4aa42008-12-09 15:10:24 -08001429
Bhanu Prakash Gollapudi8597ae82011-01-28 16:05:37 -08001430 return rc;
Robert Love85b4aa42008-12-09 15:10:24 -08001431}
Robert Love85b4aa42008-12-09 15:10:24 -08001432
1433/**
Robert Love1875f272009-11-03 11:47:50 -08001434 * fcoe_xmit() - Transmit a FCoE frame
1435 * @lport: The local port that the frame is to be transmitted for
1436 * @fp: The frame to be transmitted
Robert Love85b4aa42008-12-09 15:10:24 -08001437 *
Robert Love1875f272009-11-03 11:47:50 -08001438 * Return: 0 for success
Robert Love34f42a02009-02-27 10:55:45 -08001439 */
Robert Love1875f272009-11-03 11:47:50 -08001440int fcoe_xmit(struct fc_lport *lport, struct fc_frame *fp)
Robert Love85b4aa42008-12-09 15:10:24 -08001441{
Vasu Dev4bb6b512009-05-06 10:52:34 -07001442 int wlen;
Robert Love85b4aa42008-12-09 15:10:24 -08001443 u32 crc;
1444 struct ethhdr *eh;
1445 struct fcoe_crc_eof *cp;
1446 struct sk_buff *skb;
1447 struct fcoe_dev_stats *stats;
1448 struct fc_frame_header *fh;
1449 unsigned int hlen; /* header length implies the version */
1450 unsigned int tlen; /* trailer length */
1451 unsigned int elen; /* eth header, may include vlan */
Robert Love1875f272009-11-03 11:47:50 -08001452 struct fcoe_port *port = lport_priv(lport);
Bhanu Prakash Gollapudi8597ae82011-01-28 16:05:37 -08001453 struct fcoe_interface *fcoe = port->priv;
Robert Love85b4aa42008-12-09 15:10:24 -08001454 u8 sof, eof;
1455 struct fcoe_hdr *hp;
1456
1457 WARN_ON((fr_len(fp) % sizeof(u32)) != 0);
1458
Robert Love85b4aa42008-12-09 15:10:24 -08001459 fh = fc_frame_header_get(fp);
Joe Eykholt97c83892009-03-17 11:42:40 -07001460 skb = fp_skb(fp);
1461 wlen = skb->len / FCOE_WORD_TO_BYTE;
1462
Robert Love1875f272009-11-03 11:47:50 -08001463 if (!lport->link_up) {
Dan Carpenter3caf02e2009-04-21 16:27:25 -07001464 kfree_skb(skb);
Joe Eykholt97c83892009-03-17 11:42:40 -07001465 return 0;
Robert Love85b4aa42008-12-09 15:10:24 -08001466 }
1467
Joe Eykholt9860eeb2010-03-12 16:07:52 -08001468 if (unlikely(fh->fh_type == FC_TYPE_ELS) &&
Robert Love1875f272009-11-03 11:47:50 -08001469 fcoe_ctlr_els_send(&fcoe->ctlr, lport, skb))
Joe Eykholt97c83892009-03-17 11:42:40 -07001470 return 0;
1471
Robert Love85b4aa42008-12-09 15:10:24 -08001472 sof = fr_sof(fp);
1473 eof = fr_eof(fp);
1474
Vasu Dev4e57e1c2009-05-06 10:52:46 -07001475 elen = sizeof(struct ethhdr);
Robert Love85b4aa42008-12-09 15:10:24 -08001476 hlen = sizeof(struct fcoe_hdr);
1477 tlen = sizeof(struct fcoe_crc_eof);
1478 wlen = (skb->len - tlen + sizeof(crc)) / FCOE_WORD_TO_BYTE;
1479
1480 /* crc offload */
Robert Love1875f272009-11-03 11:47:50 -08001481 if (likely(lport->crc_offload)) {
Yi Zou39ca9a02009-02-27 14:07:15 -08001482 skb->ip_summed = CHECKSUM_PARTIAL;
Robert Love85b4aa42008-12-09 15:10:24 -08001483 skb->csum_start = skb_headroom(skb);
1484 skb->csum_offset = skb->len;
1485 crc = 0;
1486 } else {
1487 skb->ip_summed = CHECKSUM_NONE;
1488 crc = fcoe_fc_crc(fp);
1489 }
1490
Chris Leech014f5c32009-08-25 13:59:30 -07001491 /* copy port crc and eof to the skb buff */
Robert Love85b4aa42008-12-09 15:10:24 -08001492 if (skb_is_nonlinear(skb)) {
1493 skb_frag_t *frag;
Bhanu Prakash Gollapudi8597ae82011-01-28 16:05:37 -08001494 if (fcoe_alloc_paged_crc_eof(skb, tlen)) {
Roel Kluine9041582009-02-27 10:56:22 -08001495 kfree_skb(skb);
Robert Love85b4aa42008-12-09 15:10:24 -08001496 return -ENOMEM;
1497 }
1498 frag = &skb_shinfo(skb)->frags[skb_shinfo(skb)->nr_frags - 1];
1499 cp = kmap_atomic(frag->page, KM_SKB_DATA_SOFTIRQ)
1500 + frag->page_offset;
1501 } else {
1502 cp = (struct fcoe_crc_eof *)skb_put(skb, tlen);
1503 }
1504
1505 memset(cp, 0, sizeof(*cp));
1506 cp->fcoe_eof = eof;
1507 cp->fcoe_crc32 = cpu_to_le32(~crc);
1508
1509 if (skb_is_nonlinear(skb)) {
1510 kunmap_atomic(cp, KM_SKB_DATA_SOFTIRQ);
1511 cp = NULL;
1512 }
1513
Chris Leech014f5c32009-08-25 13:59:30 -07001514 /* adjust skb network/transport offsets to match mac/fcoe/port */
Robert Love85b4aa42008-12-09 15:10:24 -08001515 skb_push(skb, elen + hlen);
1516 skb_reset_mac_header(skb);
1517 skb_reset_network_header(skb);
1518 skb->mac_len = elen;
Yi Zou211c7382009-02-27 14:06:37 -08001519 skb->protocol = htons(ETH_P_FCOE);
Chris Leech3fe9a0b2009-08-25 13:59:46 -07001520 skb->dev = fcoe->netdev;
Robert Love85b4aa42008-12-09 15:10:24 -08001521
1522 /* fill up mac and fcoe headers */
1523 eh = eth_hdr(skb);
1524 eh->h_proto = htons(ETH_P_FCOE);
Joe Eykholtcd229e42010-07-20 15:20:40 -07001525 memcpy(eh->h_dest, fcoe->ctlr.dest_addr, ETH_ALEN);
Chris Leech3fe9a0b2009-08-25 13:59:46 -07001526 if (fcoe->ctlr.map_dest)
Joe Eykholtcd229e42010-07-20 15:20:40 -07001527 memcpy(eh->h_dest + 3, fh->fh_d_id, 3);
Robert Love85b4aa42008-12-09 15:10:24 -08001528
Chris Leech3fe9a0b2009-08-25 13:59:46 -07001529 if (unlikely(fcoe->ctlr.flogi_oxid != FC_XID_UNKNOWN))
1530 memcpy(eh->h_source, fcoe->ctlr.ctl_src_addr, ETH_ALEN);
Robert Love85b4aa42008-12-09 15:10:24 -08001531 else
Chris Leech11b56182009-11-03 11:46:29 -08001532 memcpy(eh->h_source, port->data_src_addr, ETH_ALEN);
Robert Love85b4aa42008-12-09 15:10:24 -08001533
1534 hp = (struct fcoe_hdr *)(eh + 1);
1535 memset(hp, 0, sizeof(*hp));
1536 if (FC_FCOE_VER)
1537 FC_FCOE_ENCAPS_VER(hp, FC_FCOE_VER);
1538 hp->fcoe_sof = sof;
1539
Yi Zou39ca9a02009-02-27 14:07:15 -08001540 /* fcoe lso, mss is in max_payload which is non-zero for FCP data */
Robert Love1875f272009-11-03 11:47:50 -08001541 if (lport->seq_offload && fr_max_payload(fp)) {
Yi Zou39ca9a02009-02-27 14:07:15 -08001542 skb_shinfo(skb)->gso_type = SKB_GSO_FCOE;
1543 skb_shinfo(skb)->gso_size = fr_max_payload(fp);
1544 } else {
1545 skb_shinfo(skb)->gso_type = 0;
1546 skb_shinfo(skb)->gso_size = 0;
1547 }
Robert Love85b4aa42008-12-09 15:10:24 -08001548 /* update tx stats: regardless if LLD fails */
Joe Eykholtf018b732010-03-12 16:08:55 -08001549 stats = per_cpu_ptr(lport->dev_stats, get_cpu());
Robert Love582b45b2009-03-31 15:51:50 -07001550 stats->TxFrames++;
1551 stats->TxWords += wlen;
Joe Eykholtf018b732010-03-12 16:08:55 -08001552 put_cpu();
Robert Love85b4aa42008-12-09 15:10:24 -08001553
1554 /* send down to lld */
Robert Love1875f272009-11-03 11:47:50 -08001555 fr_dev(fp) = lport;
Vasu Dev980f5152011-07-27 15:11:05 -07001556 fcoe_port_send(port, skb);
Robert Love85b4aa42008-12-09 15:10:24 -08001557 return 0;
1558}
Robert Love85b4aa42008-12-09 15:10:24 -08001559
Robert Love34f42a02009-02-27 10:55:45 -08001560/**
Robert Love1875f272009-11-03 11:47:50 -08001561 * fcoe_percpu_flush_done() - Indicate per-CPU queue flush completion
1562 * @skb: The completed skb (argument required by destructor)
Joe Eykholte7a51992009-08-25 14:04:08 -07001563 */
1564static void fcoe_percpu_flush_done(struct sk_buff *skb)
1565{
1566 complete(&fcoe_flush_completion);
1567}
1568
1569/**
Vasu Dev52ee8322011-01-28 16:03:52 -08001570 * fcoe_filter_frames() - filter out bad fcoe frames, i.e. bad CRC
1571 * @lport: The local port the frame was received on
1572 * @fp: The received frame
1573 *
1574 * Return: 0 on passing filtering checks
1575 */
1576static inline int fcoe_filter_frames(struct fc_lport *lport,
1577 struct fc_frame *fp)
1578{
1579 struct fcoe_interface *fcoe;
1580 struct fc_frame_header *fh;
1581 struct sk_buff *skb = (struct sk_buff *)fp;
1582 struct fcoe_dev_stats *stats;
1583
1584 /*
1585 * We only check CRC if no offload is available and if it is
1586 * it's solicited data, in which case, the FCP layer would
1587 * check it during the copy.
1588 */
1589 if (lport->crc_offload && skb->ip_summed == CHECKSUM_UNNECESSARY)
1590 fr_flags(fp) &= ~FCPHF_CRC_UNCHECKED;
1591 else
1592 fr_flags(fp) |= FCPHF_CRC_UNCHECKED;
1593
1594 fh = (struct fc_frame_header *) skb_transport_header(skb);
1595 fh = fc_frame_header_get(fp);
1596 if (fh->fh_r_ctl == FC_RCTL_DD_SOL_DATA && fh->fh_type == FC_TYPE_FCP)
1597 return 0;
1598
Bhanu Prakash Gollapudi8597ae82011-01-28 16:05:37 -08001599 fcoe = ((struct fcoe_port *)lport_priv(lport))->priv;
Vasu Dev52ee8322011-01-28 16:03:52 -08001600 if (is_fip_mode(&fcoe->ctlr) && fc_frame_payload_op(fp) == ELS_LOGO &&
1601 ntoh24(fh->fh_s_id) == FC_FID_FLOGI) {
1602 FCOE_DBG("fcoe: dropping FCoE lport LOGO in fip mode\n");
1603 return -EINVAL;
1604 }
1605
Dan Carpenterf2f96d22011-02-25 15:03:23 -08001606 if (!(fr_flags(fp) & FCPHF_CRC_UNCHECKED) ||
Vasu Dev52ee8322011-01-28 16:03:52 -08001607 le32_to_cpu(fr_crc(fp)) == ~crc32(~0, skb->data, skb->len)) {
1608 fr_flags(fp) &= ~FCPHF_CRC_UNCHECKED;
1609 return 0;
1610 }
1611
1612 stats = per_cpu_ptr(lport->dev_stats, get_cpu());
1613 stats->InvalidCRCCount++;
1614 if (stats->InvalidCRCCount < 5)
1615 printk(KERN_WARNING "fcoe: dropping frame with CRC error\n");
1616 return -EINVAL;
1617}
1618
1619/**
Chris Leech859b7b62009-11-20 14:54:47 -08001620 * fcoe_recv_frame() - process a single received frame
1621 * @skb: frame to process
1622 */
1623static void fcoe_recv_frame(struct sk_buff *skb)
1624{
1625 u32 fr_len;
1626 struct fc_lport *lport;
1627 struct fcoe_rcv_info *fr;
1628 struct fcoe_dev_stats *stats;
Chris Leech859b7b62009-11-20 14:54:47 -08001629 struct fcoe_crc_eof crc_eof;
1630 struct fc_frame *fp;
Chris Leech859b7b62009-11-20 14:54:47 -08001631 struct fcoe_port *port;
1632 struct fcoe_hdr *hp;
1633
1634 fr = fcoe_dev_from_skb(skb);
1635 lport = fr->fr_dev;
1636 if (unlikely(!lport)) {
1637 if (skb->destructor != fcoe_percpu_flush_done)
1638 FCOE_NETDEV_DBG(skb->dev, "NULL lport in skb");
1639 kfree_skb(skb);
1640 return;
1641 }
1642
1643 FCOE_NETDEV_DBG(skb->dev, "skb_info: len:%d data_len:%d "
1644 "head:%p data:%p tail:%p end:%p sum:%d dev:%s",
1645 skb->len, skb->data_len,
1646 skb->head, skb->data, skb_tail_pointer(skb),
1647 skb_end_pointer(skb), skb->csum,
1648 skb->dev ? skb->dev->name : "<NULL>");
1649
Chris Leech859b7b62009-11-20 14:54:47 -08001650 port = lport_priv(lport);
1651 if (skb_is_nonlinear(skb))
1652 skb_linearize(skb); /* not ideal */
Chris Leech859b7b62009-11-20 14:54:47 -08001653
1654 /*
1655 * Frame length checks and setting up the header pointers
1656 * was done in fcoe_rcv already.
1657 */
1658 hp = (struct fcoe_hdr *) skb_network_header(skb);
Chris Leech859b7b62009-11-20 14:54:47 -08001659
Joe Eykholtf018b732010-03-12 16:08:55 -08001660 stats = per_cpu_ptr(lport->dev_stats, get_cpu());
Chris Leech859b7b62009-11-20 14:54:47 -08001661 if (unlikely(FC_FCOE_DECAPS_VER(hp) != FC_FCOE_VER)) {
1662 if (stats->ErrorFrames < 5)
1663 printk(KERN_WARNING "fcoe: FCoE version "
1664 "mismatch: The frame has "
1665 "version %x, but the "
1666 "initiator supports version "
1667 "%x\n", FC_FCOE_DECAPS_VER(hp),
1668 FC_FCOE_VER);
Joe Eykholtf018b732010-03-12 16:08:55 -08001669 goto drop;
Chris Leech859b7b62009-11-20 14:54:47 -08001670 }
1671
1672 skb_pull(skb, sizeof(struct fcoe_hdr));
1673 fr_len = skb->len - sizeof(struct fcoe_crc_eof);
1674
1675 stats->RxFrames++;
1676 stats->RxWords += fr_len / FCOE_WORD_TO_BYTE;
1677
1678 fp = (struct fc_frame *)skb;
1679 fc_frame_init(fp);
1680 fr_dev(fp) = lport;
1681 fr_sof(fp) = hp->fcoe_sof;
1682
1683 /* Copy out the CRC and EOF trailer for access */
Joe Eykholtf018b732010-03-12 16:08:55 -08001684 if (skb_copy_bits(skb, fr_len, &crc_eof, sizeof(crc_eof)))
1685 goto drop;
Chris Leech859b7b62009-11-20 14:54:47 -08001686 fr_eof(fp) = crc_eof.fcoe_eof;
1687 fr_crc(fp) = crc_eof.fcoe_crc32;
Joe Eykholtf018b732010-03-12 16:08:55 -08001688 if (pskb_trim(skb, fr_len))
1689 goto drop;
Chris Leech859b7b62009-11-20 14:54:47 -08001690
Vasu Dev52ee8322011-01-28 16:03:52 -08001691 if (!fcoe_filter_frames(lport, fp)) {
1692 put_cpu();
1693 fc_exch_recv(lport, fp);
1694 return;
Chris Leech859b7b62009-11-20 14:54:47 -08001695 }
Joe Eykholtf018b732010-03-12 16:08:55 -08001696drop:
1697 stats->ErrorFrames++;
1698 put_cpu();
1699 kfree_skb(skb);
Chris Leech859b7b62009-11-20 14:54:47 -08001700}
1701
1702/**
Robert Love1875f272009-11-03 11:47:50 -08001703 * fcoe_percpu_receive_thread() - The per-CPU packet receive thread
1704 * @arg: The per-CPU context
Robert Love85b4aa42008-12-09 15:10:24 -08001705 *
1706 * Return: 0 for success
Robert Love85b4aa42008-12-09 15:10:24 -08001707 */
1708int fcoe_percpu_receive_thread(void *arg)
1709{
1710 struct fcoe_percpu_s *p = arg;
Robert Love85b4aa42008-12-09 15:10:24 -08001711 struct sk_buff *skb;
Robert Love85b4aa42008-12-09 15:10:24 -08001712
Robert Love4469c192009-02-27 10:56:38 -08001713 set_user_nice(current, -20);
Robert Love85b4aa42008-12-09 15:10:24 -08001714
1715 while (!kthread_should_stop()) {
1716
1717 spin_lock_bh(&p->fcoe_rx_list.lock);
1718 while ((skb = __skb_dequeue(&p->fcoe_rx_list)) == NULL) {
1719 set_current_state(TASK_INTERRUPTIBLE);
1720 spin_unlock_bh(&p->fcoe_rx_list.lock);
1721 schedule();
1722 set_current_state(TASK_RUNNING);
1723 if (kthread_should_stop())
1724 return 0;
1725 spin_lock_bh(&p->fcoe_rx_list.lock);
1726 }
1727 spin_unlock_bh(&p->fcoe_rx_list.lock);
Chris Leech859b7b62009-11-20 14:54:47 -08001728 fcoe_recv_frame(skb);
Robert Love85b4aa42008-12-09 15:10:24 -08001729 }
1730 return 0;
1731}
1732
1733/**
Robert Love1875f272009-11-03 11:47:50 -08001734 * fcoe_dev_setup() - Setup the link change notification interface
Robert Love34f42a02009-02-27 10:55:45 -08001735 */
Randy Dunlapb0d428a2009-04-27 21:49:31 -07001736static void fcoe_dev_setup(void)
Robert Love85b4aa42008-12-09 15:10:24 -08001737{
Robert Love85b4aa42008-12-09 15:10:24 -08001738 register_netdevice_notifier(&fcoe_notifier);
1739}
1740
1741/**
Robert Love1875f272009-11-03 11:47:50 -08001742 * fcoe_dev_cleanup() - Cleanup the link change notification interface
Robert Love34f42a02009-02-27 10:55:45 -08001743 */
Robert Love85b4aa42008-12-09 15:10:24 -08001744static void fcoe_dev_cleanup(void)
1745{
1746 unregister_netdevice_notifier(&fcoe_notifier);
1747}
1748
1749/**
Robert Love1875f272009-11-03 11:47:50 -08001750 * fcoe_device_notification() - Handler for net device events
1751 * @notifier: The context of the notification
1752 * @event: The type of event
1753 * @ptr: The net device that the event was on
Robert Love85b4aa42008-12-09 15:10:24 -08001754 *
Robert Love1875f272009-11-03 11:47:50 -08001755 * This function is called by the Ethernet driver in case of link change event.
Robert Love85b4aa42008-12-09 15:10:24 -08001756 *
1757 * Returns: 0 for success
Robert Love34f42a02009-02-27 10:55:45 -08001758 */
Robert Love85b4aa42008-12-09 15:10:24 -08001759static int fcoe_device_notification(struct notifier_block *notifier,
1760 ulong event, void *ptr)
1761{
Robert Love1875f272009-11-03 11:47:50 -08001762 struct fc_lport *lport = NULL;
Vasu Dev1d1b88d2009-07-29 17:05:45 -07001763 struct net_device *netdev = ptr;
Chris Leech014f5c32009-08-25 13:59:30 -07001764 struct fcoe_interface *fcoe;
Chris Leech2e70e242009-08-25 14:00:23 -07001765 struct fcoe_port *port;
Robert Love85b4aa42008-12-09 15:10:24 -08001766 struct fcoe_dev_stats *stats;
Joe Eykholt97c83892009-03-17 11:42:40 -07001767 u32 link_possible = 1;
Robert Love85b4aa42008-12-09 15:10:24 -08001768 u32 mfs;
1769 int rc = NOTIFY_OK;
1770
Chris Leech014f5c32009-08-25 13:59:30 -07001771 list_for_each_entry(fcoe, &fcoe_hostlist, list) {
Chris Leech25024982009-08-25 13:59:35 -07001772 if (fcoe->netdev == netdev) {
Robert Love1875f272009-11-03 11:47:50 -08001773 lport = fcoe->ctlr.lp;
Robert Love85b4aa42008-12-09 15:10:24 -08001774 break;
1775 }
1776 }
Robert Love1875f272009-11-03 11:47:50 -08001777 if (!lport) {
Robert Love85b4aa42008-12-09 15:10:24 -08001778 rc = NOTIFY_DONE;
1779 goto out;
1780 }
1781
Robert Love85b4aa42008-12-09 15:10:24 -08001782 switch (event) {
1783 case NETDEV_DOWN:
1784 case NETDEV_GOING_DOWN:
Joe Eykholt97c83892009-03-17 11:42:40 -07001785 link_possible = 0;
Robert Love85b4aa42008-12-09 15:10:24 -08001786 break;
1787 case NETDEV_UP:
1788 case NETDEV_CHANGE:
Robert Love85b4aa42008-12-09 15:10:24 -08001789 break;
1790 case NETDEV_CHANGEMTU:
Yi Zou7221d7e2009-10-21 16:27:52 -07001791 if (netdev->features & NETIF_F_FCOE_MTU)
1792 break;
Vasu Dev1d1b88d2009-07-29 17:05:45 -07001793 mfs = netdev->mtu - (sizeof(struct fcoe_hdr) +
1794 sizeof(struct fcoe_crc_eof));
Robert Love85b4aa42008-12-09 15:10:24 -08001795 if (mfs >= FC_MIN_MAX_FRAME)
Robert Love1875f272009-11-03 11:47:50 -08001796 fc_set_mfs(lport, mfs);
Robert Love85b4aa42008-12-09 15:10:24 -08001797 break;
1798 case NETDEV_REGISTER:
1799 break;
Chris Leech2e70e242009-08-25 14:00:23 -07001800 case NETDEV_UNREGISTER:
1801 list_del(&fcoe->list);
1802 port = lport_priv(fcoe->ctlr.lp);
Tejun Heo2ca32b42011-01-28 16:05:32 -08001803 queue_work(fcoe_wq, &port->destroy_work);
Chris Leech2e70e242009-08-25 14:00:23 -07001804 goto out;
1805 break;
Yi Zou54a5b212010-07-20 15:21:17 -07001806 case NETDEV_FEAT_CHANGE:
1807 fcoe_netdev_features_change(lport, netdev);
1808 break;
Robert Love85b4aa42008-12-09 15:10:24 -08001809 default:
Vasu Dev1d1b88d2009-07-29 17:05:45 -07001810 FCOE_NETDEV_DBG(netdev, "Unknown event %ld "
Robert Loved5488eb2009-06-10 15:30:59 -07001811 "from netdev netlink\n", event);
Robert Love85b4aa42008-12-09 15:10:24 -08001812 }
Robert Love5e4f8fe2010-05-07 15:18:35 -07001813
1814 fcoe_link_speed_update(lport);
1815
Robert Love1875f272009-11-03 11:47:50 -08001816 if (link_possible && !fcoe_link_ok(lport))
Chris Leech3fe9a0b2009-08-25 13:59:46 -07001817 fcoe_ctlr_link_up(&fcoe->ctlr);
1818 else if (fcoe_ctlr_link_down(&fcoe->ctlr)) {
Joe Eykholtf018b732010-03-12 16:08:55 -08001819 stats = per_cpu_ptr(lport->dev_stats, get_cpu());
Joe Eykholt97c83892009-03-17 11:42:40 -07001820 stats->LinkFailureCount++;
Joe Eykholtf018b732010-03-12 16:08:55 -08001821 put_cpu();
Robert Love1875f272009-11-03 11:47:50 -08001822 fcoe_clean_pending_queue(lport);
Robert Love85b4aa42008-12-09 15:10:24 -08001823 }
1824out:
1825 return rc;
1826}
1827
1828/**
Vasu Dev55a66d32009-12-10 09:59:31 -08001829 * fcoe_disable() - Disables a FCoE interface
Yi Zou78a58242011-01-28 16:05:16 -08001830 * @netdev : The net_device object the Ethernet interface to create on
Vasu Dev55a66d32009-12-10 09:59:31 -08001831 *
Yi Zou78a58242011-01-28 16:05:16 -08001832 * Called from fcoe transport.
Vasu Dev55a66d32009-12-10 09:59:31 -08001833 *
1834 * Returns: 0 for success
1835 */
Yi Zou78a58242011-01-28 16:05:16 -08001836static int fcoe_disable(struct net_device *netdev)
Vasu Dev55a66d32009-12-10 09:59:31 -08001837{
1838 struct fcoe_interface *fcoe;
Vasu Dev55a66d32009-12-10 09:59:31 -08001839 int rc = 0;
1840
1841 mutex_lock(&fcoe_config_mutex);
Vasu Dev55a66d32009-12-10 09:59:31 -08001842
Robert Loveee5df622011-04-01 16:05:59 -07001843 rtnl_lock();
Vasu Dev55a66d32009-12-10 09:59:31 -08001844 fcoe = fcoe_hostlist_lookup_port(netdev);
1845 rtnl_unlock();
1846
Chris Leech9ee50e42010-04-09 14:22:23 -07001847 if (fcoe) {
Chris Leech9ee50e42010-04-09 14:22:23 -07001848 fcoe_ctlr_link_down(&fcoe->ctlr);
Vasu Dev9d4cbc02010-07-20 15:19:26 -07001849 fcoe_clean_pending_queue(fcoe->ctlr.lp);
Chris Leech9ee50e42010-04-09 14:22:23 -07001850 } else
Vasu Dev55a66d32009-12-10 09:59:31 -08001851 rc = -ENODEV;
1852
Vasu Dev55a66d32009-12-10 09:59:31 -08001853 mutex_unlock(&fcoe_config_mutex);
1854 return rc;
1855}
1856
1857/**
1858 * fcoe_enable() - Enables a FCoE interface
Yi Zou78a58242011-01-28 16:05:16 -08001859 * @netdev : The net_device object the Ethernet interface to create on
Vasu Dev55a66d32009-12-10 09:59:31 -08001860 *
Yi Zou78a58242011-01-28 16:05:16 -08001861 * Called from fcoe transport.
Vasu Dev55a66d32009-12-10 09:59:31 -08001862 *
1863 * Returns: 0 for success
1864 */
Yi Zou78a58242011-01-28 16:05:16 -08001865static int fcoe_enable(struct net_device *netdev)
Vasu Dev55a66d32009-12-10 09:59:31 -08001866{
1867 struct fcoe_interface *fcoe;
Vasu Dev55a66d32009-12-10 09:59:31 -08001868 int rc = 0;
1869
1870 mutex_lock(&fcoe_config_mutex);
Robert Loveee5df622011-04-01 16:05:59 -07001871 rtnl_lock();
Vasu Dev55a66d32009-12-10 09:59:31 -08001872 fcoe = fcoe_hostlist_lookup_port(netdev);
1873 rtnl_unlock();
1874
Vasu Dev9d4cbc02010-07-20 15:19:26 -07001875 if (!fcoe)
Vasu Dev55a66d32009-12-10 09:59:31 -08001876 rc = -ENODEV;
Vasu Dev9d4cbc02010-07-20 15:19:26 -07001877 else if (!fcoe_link_ok(fcoe->ctlr.lp))
1878 fcoe_ctlr_link_up(&fcoe->ctlr);
Vasu Dev55a66d32009-12-10 09:59:31 -08001879
Vasu Dev55a66d32009-12-10 09:59:31 -08001880 mutex_unlock(&fcoe_config_mutex);
1881 return rc;
1882}
1883
1884/**
Robert Love1875f272009-11-03 11:47:50 -08001885 * fcoe_destroy() - Destroy a FCoE interface
Yi Zou78a58242011-01-28 16:05:16 -08001886 * @netdev : The net_device object the Ethernet interface to create on
Robert Love1875f272009-11-03 11:47:50 -08001887 *
Yi Zou78a58242011-01-28 16:05:16 -08001888 * Called from fcoe transport
Robert Love85b4aa42008-12-09 15:10:24 -08001889 *
1890 * Returns: 0 for success
Robert Love34f42a02009-02-27 10:55:45 -08001891 */
Yi Zou78a58242011-01-28 16:05:16 -08001892static int fcoe_destroy(struct net_device *netdev)
Robert Love85b4aa42008-12-09 15:10:24 -08001893{
Chris Leech030f4e02009-08-25 14:00:02 -07001894 struct fcoe_interface *fcoe;
Vasu Devf04ca1b2011-04-01 16:06:45 -07001895 struct fc_lport *lport;
Neerav Parikhb2085a42011-06-20 16:59:51 -07001896 struct fcoe_port *port;
Mike Christie8eca3552009-10-21 16:27:44 -07001897 int rc = 0;
Robert Love85b4aa42008-12-09 15:10:24 -08001898
Chris Leechdfc1d0f2009-08-25 14:00:13 -07001899 mutex_lock(&fcoe_config_mutex);
Robert Loveee5df622011-04-01 16:05:59 -07001900 rtnl_lock();
Chris Leech2e70e242009-08-25 14:00:23 -07001901 fcoe = fcoe_hostlist_lookup_port(netdev);
1902 if (!fcoe) {
Robert Love85b4aa42008-12-09 15:10:24 -08001903 rc = -ENODEV;
Yi Zou78a58242011-01-28 16:05:16 -08001904 goto out_nodev;
Robert Love85b4aa42008-12-09 15:10:24 -08001905 }
Vasu Devf04ca1b2011-04-01 16:06:45 -07001906 lport = fcoe->ctlr.lp;
Neerav Parikhb2085a42011-06-20 16:59:51 -07001907 port = lport_priv(lport);
Yi Zou54a5b212010-07-20 15:21:17 -07001908 list_del(&fcoe->list);
Neerav Parikhb2085a42011-06-20 16:59:51 -07001909 queue_work(fcoe_wq, &port->destroy_work);
Robert Love85b4aa42008-12-09 15:10:24 -08001910out_nodev:
Neerav Parikhb2085a42011-06-20 16:59:51 -07001911 rtnl_unlock();
Chris Leechdfc1d0f2009-08-25 14:00:13 -07001912 mutex_unlock(&fcoe_config_mutex);
Robert Love85b4aa42008-12-09 15:10:24 -08001913 return rc;
1914}
1915
Robert Love1875f272009-11-03 11:47:50 -08001916/**
1917 * fcoe_destroy_work() - Destroy a FCoE port in a deferred work context
1918 * @work: Handle to the FCoE port to be destroyed
1919 */
Chris Leech2e70e242009-08-25 14:00:23 -07001920static void fcoe_destroy_work(struct work_struct *work)
1921{
1922 struct fcoe_port *port;
Neerav Parikhb2085a42011-06-20 16:59:51 -07001923 struct fcoe_interface *fcoe;
1924 int npiv = 0;
Chris Leech2e70e242009-08-25 14:00:23 -07001925
1926 port = container_of(work, struct fcoe_port, destroy_work);
1927 mutex_lock(&fcoe_config_mutex);
Neerav Parikhb2085a42011-06-20 16:59:51 -07001928
1929 /* set if this is an NPIV port */
1930 npiv = port->lport->vport ? 1 : 0;
1931
1932 fcoe = port->priv;
Chris Leech2e70e242009-08-25 14:00:23 -07001933 fcoe_if_destroy(port->lport);
Neerav Parikhb2085a42011-06-20 16:59:51 -07001934
1935 /* Do not tear down the fcoe interface for NPIV port */
1936 if (!npiv) {
1937 rtnl_lock();
1938 fcoe_interface_cleanup(fcoe);
1939 rtnl_unlock();
1940 }
1941
Chris Leech2e70e242009-08-25 14:00:23 -07001942 mutex_unlock(&fcoe_config_mutex);
1943}
1944
Robert Love85b4aa42008-12-09 15:10:24 -08001945/**
Yi Zou78a58242011-01-28 16:05:16 -08001946 * fcoe_match() - Check if the FCoE is supported on the given netdevice
1947 * @netdev : The net_device object the Ethernet interface to create on
Robert Love1875f272009-11-03 11:47:50 -08001948 *
Yi Zou78a58242011-01-28 16:05:16 -08001949 * Called from fcoe transport.
1950 *
1951 * Returns: always returns true as this is the default FCoE transport,
1952 * i.e., support all netdevs.
1953 */
1954static bool fcoe_match(struct net_device *netdev)
1955{
1956 return true;
1957}
1958
1959/**
1960 * fcoe_create() - Create a fcoe interface
1961 * @netdev : The net_device object the Ethernet interface to create on
1962 * @fip_mode: The FIP mode for this creation
1963 *
1964 * Called from fcoe transport
Robert Love85b4aa42008-12-09 15:10:24 -08001965 *
1966 * Returns: 0 for success
Robert Love34f42a02009-02-27 10:55:45 -08001967 */
Yi Zou78a58242011-01-28 16:05:16 -08001968static int fcoe_create(struct net_device *netdev, enum fip_state fip_mode)
Robert Love85b4aa42008-12-09 15:10:24 -08001969{
Neerav Parikhb2085a42011-06-20 16:59:51 -07001970 int rc = 0;
Chris Leech030f4e02009-08-25 14:00:02 -07001971 struct fcoe_interface *fcoe;
Chris Leechaf7f85d2009-08-25 13:59:24 -07001972 struct fc_lport *lport;
Robert Love85b4aa42008-12-09 15:10:24 -08001973
Chris Leechdfc1d0f2009-08-25 14:00:13 -07001974 mutex_lock(&fcoe_config_mutex);
Robert Loveee5df622011-04-01 16:05:59 -07001975 rtnl_lock();
Vasu Dev34ce27b2010-05-07 15:18:46 -07001976
Robert Love85b4aa42008-12-09 15:10:24 -08001977 /* look for existing lport */
1978 if (fcoe_hostlist_lookup(netdev)) {
1979 rc = -EEXIST;
Yi Zou78a58242011-01-28 16:05:16 -08001980 goto out_nodev;
Robert Love85b4aa42008-12-09 15:10:24 -08001981 }
Robert Love85b4aa42008-12-09 15:10:24 -08001982
Joe Eykholt1dd454d2010-07-20 15:20:46 -07001983 fcoe = fcoe_interface_create(netdev, fip_mode);
Robert Love7287fb92011-01-28 16:03:47 -08001984 if (IS_ERR(fcoe)) {
1985 rc = PTR_ERR(fcoe);
Yi Zou78a58242011-01-28 16:05:16 -08001986 goto out_nodev;
Chris Leech030f4e02009-08-25 14:00:02 -07001987 }
1988
Chris Leech9a057532009-11-03 11:46:40 -08001989 lport = fcoe_if_create(fcoe, &netdev->dev, 0);
Chris Leechaf7f85d2009-08-25 13:59:24 -07001990 if (IS_ERR(lport)) {
Robert Loved5488eb2009-06-10 15:30:59 -07001991 printk(KERN_ERR "fcoe: Failed to create interface (%s)\n",
Robert Love85b4aa42008-12-09 15:10:24 -08001992 netdev->name);
Robert Love85b4aa42008-12-09 15:10:24 -08001993 rc = -EIO;
Chris Leech2e70e242009-08-25 14:00:23 -07001994 fcoe_interface_cleanup(fcoe);
Neerav Parikhb2085a42011-06-20 16:59:51 -07001995 goto out_nodev;
Robert Love85b4aa42008-12-09 15:10:24 -08001996 }
Chris Leech030f4e02009-08-25 14:00:02 -07001997
Chris Leech54b649f2009-08-25 14:00:07 -07001998 /* Make this the "master" N_Port */
1999 fcoe->ctlr.lp = lport;
Chris Leech030f4e02009-08-25 14:00:02 -07002000
Chris Leechc863df32009-08-25 14:00:18 -07002001 /* add to lports list */
2002 fcoe_hostlist_add(lport);
2003
Chris Leech54b649f2009-08-25 14:00:07 -07002004 /* start FIP Discovery and FLOGI */
2005 lport->boot_time = jiffies;
2006 fc_fabric_login(lport);
2007 if (!fcoe_link_ok(lport))
2008 fcoe_ctlr_link_up(&fcoe->ctlr);
2009
Robert Love85b4aa42008-12-09 15:10:24 -08002010out_nodev:
Vasu Dev34ce27b2010-05-07 15:18:46 -07002011 rtnl_unlock();
Chris Leechdfc1d0f2009-08-25 14:00:13 -07002012 mutex_unlock(&fcoe_config_mutex);
Robert Love85b4aa42008-12-09 15:10:24 -08002013 return rc;
2014}
2015
Robert Love34f42a02009-02-27 10:55:45 -08002016/**
Robert Love5e4f8fe2010-05-07 15:18:35 -07002017 * fcoe_link_speed_update() - Update the supported and actual link speeds
2018 * @lport: The local port to update speeds for
Robert Love85b4aa42008-12-09 15:10:24 -08002019 *
Robert Love5e4f8fe2010-05-07 15:18:35 -07002020 * Returns: 0 if the ethtool query was successful
2021 * -1 if the ethtool query failed
Robert Love85b4aa42008-12-09 15:10:24 -08002022 */
Robert Love5e4f8fe2010-05-07 15:18:35 -07002023int fcoe_link_speed_update(struct fc_lport *lport)
Robert Love85b4aa42008-12-09 15:10:24 -08002024{
Bhanu Prakash Gollapudi8597ae82011-01-28 16:05:37 -08002025 struct net_device *netdev = fcoe_netdev(lport);
David Decotigny8ae6dac2011-04-27 18:32:38 +00002026 struct ethtool_cmd ecmd;
Robert Love85b4aa42008-12-09 15:10:24 -08002027
Robert Love5e4f8fe2010-05-07 15:18:35 -07002028 if (!dev_ethtool_get_settings(netdev, &ecmd)) {
Robert Love1875f272009-11-03 11:47:50 -08002029 lport->link_supported_speeds &=
Yi Zou2f718d62009-07-29 17:04:01 -07002030 ~(FC_PORTSPEED_1GBIT | FC_PORTSPEED_10GBIT);
2031 if (ecmd.supported & (SUPPORTED_1000baseT_Half |
2032 SUPPORTED_1000baseT_Full))
Robert Love1875f272009-11-03 11:47:50 -08002033 lport->link_supported_speeds |= FC_PORTSPEED_1GBIT;
Yi Zou2f718d62009-07-29 17:04:01 -07002034 if (ecmd.supported & SUPPORTED_10000baseT_Full)
Robert Love1875f272009-11-03 11:47:50 -08002035 lport->link_supported_speeds |=
Yi Zou2f718d62009-07-29 17:04:01 -07002036 FC_PORTSPEED_10GBIT;
David Decotigny8ae6dac2011-04-27 18:32:38 +00002037 switch (ethtool_cmd_speed(&ecmd)) {
2038 case SPEED_1000:
Robert Love1875f272009-11-03 11:47:50 -08002039 lport->link_speed = FC_PORTSPEED_1GBIT;
David Decotigny8ae6dac2011-04-27 18:32:38 +00002040 break;
2041 case SPEED_10000:
Robert Love1875f272009-11-03 11:47:50 -08002042 lport->link_speed = FC_PORTSPEED_10GBIT;
David Decotigny8ae6dac2011-04-27 18:32:38 +00002043 break;
2044 }
Yi Zou2f718d62009-07-29 17:04:01 -07002045 return 0;
2046 }
2047 return -1;
Robert Love85b4aa42008-12-09 15:10:24 -08002048}
Robert Love85b4aa42008-12-09 15:10:24 -08002049
Robert Love34f42a02009-02-27 10:55:45 -08002050/**
Robert Love5e4f8fe2010-05-07 15:18:35 -07002051 * fcoe_link_ok() - Check if the link is OK for a local port
2052 * @lport: The local port to check link on
2053 *
2054 * Returns: 0 if link is UP and OK, -1 if not
2055 *
2056 */
2057int fcoe_link_ok(struct fc_lport *lport)
2058{
Bhanu Prakash Gollapudi8597ae82011-01-28 16:05:37 -08002059 struct net_device *netdev = fcoe_netdev(lport);
Robert Love5e4f8fe2010-05-07 15:18:35 -07002060
2061 if (netif_oper_up(netdev))
2062 return 0;
2063 return -1;
2064}
2065
2066/**
Robert Love1875f272009-11-03 11:47:50 -08002067 * fcoe_percpu_clean() - Clear all pending skbs for an local port
2068 * @lport: The local port whose skbs are to be cleared
Joe Eykholte7a51992009-08-25 14:04:08 -07002069 *
2070 * Must be called with fcoe_create_mutex held to single-thread completion.
2071 *
2072 * This flushes the pending skbs by adding a new skb to each queue and
2073 * waiting until they are all freed. This assures us that not only are
2074 * there no packets that will be handled by the lport, but also that any
2075 * threads already handling packet have returned.
Robert Love85b4aa42008-12-09 15:10:24 -08002076 */
Robert Love1875f272009-11-03 11:47:50 -08002077void fcoe_percpu_clean(struct fc_lport *lport)
Robert Love85b4aa42008-12-09 15:10:24 -08002078{
Robert Love85b4aa42008-12-09 15:10:24 -08002079 struct fcoe_percpu_s *pp;
2080 struct fcoe_rcv_info *fr;
2081 struct sk_buff_head *list;
2082 struct sk_buff *skb, *next;
2083 struct sk_buff *head;
Robert Love5e5e92d2009-03-17 11:41:35 -07002084 unsigned int cpu;
Robert Love85b4aa42008-12-09 15:10:24 -08002085
Robert Love5e5e92d2009-03-17 11:41:35 -07002086 for_each_possible_cpu(cpu) {
2087 pp = &per_cpu(fcoe_percpu, cpu);
2088 spin_lock_bh(&pp->fcoe_rx_list.lock);
2089 list = &pp->fcoe_rx_list;
2090 head = list->next;
2091 for (skb = head; skb != (struct sk_buff *)list;
2092 skb = next) {
2093 next = skb->next;
2094 fr = fcoe_dev_from_skb(skb);
Robert Love1875f272009-11-03 11:47:50 -08002095 if (fr->fr_dev == lport) {
Robert Love5e5e92d2009-03-17 11:41:35 -07002096 __skb_unlink(skb, list);
2097 kfree_skb(skb);
Robert Love85b4aa42008-12-09 15:10:24 -08002098 }
Robert Love85b4aa42008-12-09 15:10:24 -08002099 }
Joe Eykholte7a51992009-08-25 14:04:08 -07002100
2101 if (!pp->thread || !cpu_online(cpu)) {
2102 spin_unlock_bh(&pp->fcoe_rx_list.lock);
2103 continue;
2104 }
2105
2106 skb = dev_alloc_skb(0);
2107 if (!skb) {
2108 spin_unlock_bh(&pp->fcoe_rx_list.lock);
2109 continue;
2110 }
2111 skb->destructor = fcoe_percpu_flush_done;
2112
2113 __skb_queue_tail(&pp->fcoe_rx_list, skb);
2114 if (pp->fcoe_rx_list.qlen == 1)
2115 wake_up_process(pp->thread);
Robert Love5e5e92d2009-03-17 11:41:35 -07002116 spin_unlock_bh(&pp->fcoe_rx_list.lock);
Joe Eykholte7a51992009-08-25 14:04:08 -07002117
2118 wait_for_completion(&fcoe_flush_completion);
Robert Love85b4aa42008-12-09 15:10:24 -08002119 }
2120}
Robert Love85b4aa42008-12-09 15:10:24 -08002121
2122/**
Robert Love1875f272009-11-03 11:47:50 -08002123 * fcoe_reset() - Reset a local port
2124 * @shost: The SCSI host associated with the local port to be reset
Robert Love85b4aa42008-12-09 15:10:24 -08002125 *
Robert Love1875f272009-11-03 11:47:50 -08002126 * Returns: Always 0 (return value required by FC transport template)
Robert Love85b4aa42008-12-09 15:10:24 -08002127 */
2128int fcoe_reset(struct Scsi_Host *shost)
2129{
2130 struct fc_lport *lport = shost_priv(shost);
Vasu Devd2f80952011-02-25 15:03:28 -08002131 struct fcoe_port *port = lport_priv(lport);
2132 struct fcoe_interface *fcoe = port->priv;
2133
2134 fcoe_ctlr_link_down(&fcoe->ctlr);
2135 fcoe_clean_pending_queue(fcoe->ctlr.lp);
2136 if (!fcoe_link_ok(fcoe->ctlr.lp))
2137 fcoe_ctlr_link_up(&fcoe->ctlr);
Robert Love85b4aa42008-12-09 15:10:24 -08002138 return 0;
2139}
Robert Love85b4aa42008-12-09 15:10:24 -08002140
Robert Love34f42a02009-02-27 10:55:45 -08002141/**
Robert Love1875f272009-11-03 11:47:50 -08002142 * fcoe_hostlist_lookup_port() - Find the FCoE interface associated with a net device
2143 * @netdev: The net device used as a key
Robert Love85b4aa42008-12-09 15:10:24 -08002144 *
Robert Love1875f272009-11-03 11:47:50 -08002145 * Locking: Must be called with the RNL mutex held.
2146 *
2147 * Returns: NULL or the FCoE interface
Robert Love85b4aa42008-12-09 15:10:24 -08002148 */
Chris Leech014f5c32009-08-25 13:59:30 -07002149static struct fcoe_interface *
Robert Love1875f272009-11-03 11:47:50 -08002150fcoe_hostlist_lookup_port(const struct net_device *netdev)
Robert Love85b4aa42008-12-09 15:10:24 -08002151{
Chris Leech014f5c32009-08-25 13:59:30 -07002152 struct fcoe_interface *fcoe;
Robert Love85b4aa42008-12-09 15:10:24 -08002153
Chris Leech014f5c32009-08-25 13:59:30 -07002154 list_for_each_entry(fcoe, &fcoe_hostlist, list) {
Robert Love1875f272009-11-03 11:47:50 -08002155 if (fcoe->netdev == netdev)
Chris Leech014f5c32009-08-25 13:59:30 -07002156 return fcoe;
Robert Love85b4aa42008-12-09 15:10:24 -08002157 }
Robert Love85b4aa42008-12-09 15:10:24 -08002158 return NULL;
2159}
2160
Robert Love34f42a02009-02-27 10:55:45 -08002161/**
Robert Love1875f272009-11-03 11:47:50 -08002162 * fcoe_hostlist_lookup() - Find the local port associated with a
2163 * given net device
2164 * @netdev: The netdevice used as a key
Robert Love85b4aa42008-12-09 15:10:24 -08002165 *
Robert Love1875f272009-11-03 11:47:50 -08002166 * Locking: Must be called with the RTNL mutex held
2167 *
2168 * Returns: NULL or the local port
Robert Love85b4aa42008-12-09 15:10:24 -08002169 */
Chris Leech090eb6c2009-08-25 14:00:28 -07002170static struct fc_lport *fcoe_hostlist_lookup(const struct net_device *netdev)
Robert Love85b4aa42008-12-09 15:10:24 -08002171{
Chris Leech014f5c32009-08-25 13:59:30 -07002172 struct fcoe_interface *fcoe;
Robert Love85b4aa42008-12-09 15:10:24 -08002173
Chris Leech014f5c32009-08-25 13:59:30 -07002174 fcoe = fcoe_hostlist_lookup_port(netdev);
Chris Leech3fe9a0b2009-08-25 13:59:46 -07002175 return (fcoe) ? fcoe->ctlr.lp : NULL;
Robert Love85b4aa42008-12-09 15:10:24 -08002176}
Robert Love85b4aa42008-12-09 15:10:24 -08002177
Robert Love34f42a02009-02-27 10:55:45 -08002178/**
Robert Love1875f272009-11-03 11:47:50 -08002179 * fcoe_hostlist_add() - Add the FCoE interface identified by a local
2180 * port to the hostlist
2181 * @lport: The local port that identifies the FCoE interface to be added
2182 *
2183 * Locking: must be called with the RTNL mutex held
Robert Love85b4aa42008-12-09 15:10:24 -08002184 *
2185 * Returns: 0 for success
2186 */
Chris Leech090eb6c2009-08-25 14:00:28 -07002187static int fcoe_hostlist_add(const struct fc_lport *lport)
Robert Love85b4aa42008-12-09 15:10:24 -08002188{
Chris Leech014f5c32009-08-25 13:59:30 -07002189 struct fcoe_interface *fcoe;
2190 struct fcoe_port *port;
Robert Love85b4aa42008-12-09 15:10:24 -08002191
Chris Leech014f5c32009-08-25 13:59:30 -07002192 fcoe = fcoe_hostlist_lookup_port(fcoe_netdev(lport));
2193 if (!fcoe) {
2194 port = lport_priv(lport);
Bhanu Prakash Gollapudi8597ae82011-01-28 16:05:37 -08002195 fcoe = port->priv;
Chris Leech014f5c32009-08-25 13:59:30 -07002196 list_add_tail(&fcoe->list, &fcoe_hostlist);
Robert Love85b4aa42008-12-09 15:10:24 -08002197 }
2198 return 0;
2199}
Robert Love85b4aa42008-12-09 15:10:24 -08002200
Yi Zou78a58242011-01-28 16:05:16 -08002201
2202static struct fcoe_transport fcoe_sw_transport = {
2203 .name = {FCOE_TRANSPORT_DEFAULT},
2204 .attached = false,
2205 .list = LIST_HEAD_INIT(fcoe_sw_transport.list),
2206 .match = fcoe_match,
2207 .create = fcoe_create,
2208 .destroy = fcoe_destroy,
2209 .enable = fcoe_enable,
2210 .disable = fcoe_disable,
2211};
2212
Robert Love34f42a02009-02-27 10:55:45 -08002213/**
Robert Love1875f272009-11-03 11:47:50 -08002214 * fcoe_init() - Initialize fcoe.ko
Robert Love85b4aa42008-12-09 15:10:24 -08002215 *
Robert Love1875f272009-11-03 11:47:50 -08002216 * Returns: 0 on success, or a negative value on failure
Robert Love34f42a02009-02-27 10:55:45 -08002217 */
Robert Love85b4aa42008-12-09 15:10:24 -08002218static int __init fcoe_init(void)
2219{
Robert Love1875f272009-11-03 11:47:50 -08002220 struct fcoe_percpu_s *p;
Robert Love38eccab2009-03-17 11:41:30 -07002221 unsigned int cpu;
Robert Love8976f422009-03-17 11:41:46 -07002222 int rc = 0;
Robert Love85b4aa42008-12-09 15:10:24 -08002223
Tejun Heo2ca32b42011-01-28 16:05:32 -08002224 fcoe_wq = alloc_workqueue("fcoe", 0, 0);
2225 if (!fcoe_wq)
2226 return -ENOMEM;
2227
Yi Zou78a58242011-01-28 16:05:16 -08002228 /* register as a fcoe transport */
2229 rc = fcoe_transport_attach(&fcoe_sw_transport);
2230 if (rc) {
2231 printk(KERN_ERR "failed to register an fcoe transport, check "
2232 "if libfcoe is loaded\n");
2233 return rc;
2234 }
2235
Chris Leechdfc1d0f2009-08-25 14:00:13 -07002236 mutex_lock(&fcoe_config_mutex);
2237
Robert Love38eccab2009-03-17 11:41:30 -07002238 for_each_possible_cpu(cpu) {
Robert Love5e5e92d2009-03-17 11:41:35 -07002239 p = &per_cpu(fcoe_percpu, cpu);
Robert Love38eccab2009-03-17 11:41:30 -07002240 skb_queue_head_init(&p->fcoe_rx_list);
2241 }
2242
Robert Love8976f422009-03-17 11:41:46 -07002243 for_each_online_cpu(cpu)
2244 fcoe_percpu_thread_create(cpu);
Robert Love85b4aa42008-12-09 15:10:24 -08002245
Robert Love8976f422009-03-17 11:41:46 -07002246 /* Initialize per CPU interrupt thread */
2247 rc = register_hotcpu_notifier(&fcoe_cpu_notifier);
2248 if (rc)
2249 goto out_free;
Robert Love85b4aa42008-12-09 15:10:24 -08002250
Robert Love8976f422009-03-17 11:41:46 -07002251 /* Setup link change notification */
Robert Love85b4aa42008-12-09 15:10:24 -08002252 fcoe_dev_setup();
2253
Chris Leech5892c322009-08-25 13:59:14 -07002254 rc = fcoe_if_init();
2255 if (rc)
2256 goto out_free;
Robert Love85b4aa42008-12-09 15:10:24 -08002257
Chris Leechdfc1d0f2009-08-25 14:00:13 -07002258 mutex_unlock(&fcoe_config_mutex);
Robert Love85b4aa42008-12-09 15:10:24 -08002259 return 0;
Robert Love8976f422009-03-17 11:41:46 -07002260
2261out_free:
2262 for_each_online_cpu(cpu) {
2263 fcoe_percpu_thread_destroy(cpu);
2264 }
Chris Leechdfc1d0f2009-08-25 14:00:13 -07002265 mutex_unlock(&fcoe_config_mutex);
Tejun Heo2ca32b42011-01-28 16:05:32 -08002266 destroy_workqueue(fcoe_wq);
Robert Love8976f422009-03-17 11:41:46 -07002267 return rc;
Robert Love85b4aa42008-12-09 15:10:24 -08002268}
2269module_init(fcoe_init);
2270
2271/**
Robert Love1875f272009-11-03 11:47:50 -08002272 * fcoe_exit() - Clean up fcoe.ko
Robert Love85b4aa42008-12-09 15:10:24 -08002273 *
Robert Love1875f272009-11-03 11:47:50 -08002274 * Returns: 0 on success or a negative value on failure
Robert Love34f42a02009-02-27 10:55:45 -08002275 */
Robert Love85b4aa42008-12-09 15:10:24 -08002276static void __exit fcoe_exit(void)
2277{
Chris Leech014f5c32009-08-25 13:59:30 -07002278 struct fcoe_interface *fcoe, *tmp;
Chris Leech2e70e242009-08-25 14:00:23 -07002279 struct fcoe_port *port;
Robert Love1875f272009-11-03 11:47:50 -08002280 unsigned int cpu;
Robert Love85b4aa42008-12-09 15:10:24 -08002281
Chris Leechdfc1d0f2009-08-25 14:00:13 -07002282 mutex_lock(&fcoe_config_mutex);
2283
Robert Love85b4aa42008-12-09 15:10:24 -08002284 fcoe_dev_cleanup();
2285
Vasu Dev5919a592009-03-27 09:03:29 -07002286 /* releases the associated fcoe hosts */
Chris Leech090eb6c2009-08-25 14:00:28 -07002287 rtnl_lock();
2288 list_for_each_entry_safe(fcoe, tmp, &fcoe_hostlist, list) {
Chris Leechc863df32009-08-25 14:00:18 -07002289 list_del(&fcoe->list);
Chris Leech2e70e242009-08-25 14:00:23 -07002290 port = lport_priv(fcoe->ctlr.lp);
Tejun Heo2ca32b42011-01-28 16:05:32 -08002291 queue_work(fcoe_wq, &port->destroy_work);
Chris Leechc863df32009-08-25 14:00:18 -07002292 }
Chris Leech090eb6c2009-08-25 14:00:28 -07002293 rtnl_unlock();
Robert Love85b4aa42008-12-09 15:10:24 -08002294
Robert Love8976f422009-03-17 11:41:46 -07002295 unregister_hotcpu_notifier(&fcoe_cpu_notifier);
2296
Chris Leech014f5c32009-08-25 13:59:30 -07002297 for_each_online_cpu(cpu)
Robert Love8976f422009-03-17 11:41:46 -07002298 fcoe_percpu_thread_destroy(cpu);
Robert Love85b4aa42008-12-09 15:10:24 -08002299
Chris Leechdfc1d0f2009-08-25 14:00:13 -07002300 mutex_unlock(&fcoe_config_mutex);
Chris Leech2e70e242009-08-25 14:00:23 -07002301
Tejun Heo2ca32b42011-01-28 16:05:32 -08002302 /*
2303 * destroy_work's may be chained but destroy_workqueue()
2304 * can take care of them. Just kill the fcoe_wq.
2305 */
2306 destroy_workqueue(fcoe_wq);
Chris Leech2e70e242009-08-25 14:00:23 -07002307
Tejun Heo2ca32b42011-01-28 16:05:32 -08002308 /*
2309 * Detaching from the scsi transport must happen after all
2310 * destroys are done on the fcoe_wq. destroy_workqueue will
2311 * enusre the fcoe_wq is flushed.
2312 */
Chris Leech2e70e242009-08-25 14:00:23 -07002313 fcoe_if_exit();
Yi Zou78a58242011-01-28 16:05:16 -08002314
2315 /* detach from fcoe transport */
2316 fcoe_transport_detach(&fcoe_sw_transport);
Robert Love85b4aa42008-12-09 15:10:24 -08002317}
2318module_exit(fcoe_exit);
Chris Leech11b56182009-11-03 11:46:29 -08002319
2320/**
2321 * fcoe_flogi_resp() - FCoE specific FLOGI and FDISC response handler
2322 * @seq: active sequence in the FLOGI or FDISC exchange
2323 * @fp: response frame, or error encoded in a pointer (timeout)
2324 * @arg: pointer the the fcoe_ctlr structure
2325 *
Uwe Kleine-König65155b32010-06-11 12:17:01 +02002326 * This handles MAC address management for FCoE, then passes control on to
Chris Leech11b56182009-11-03 11:46:29 -08002327 * the libfc FLOGI response handler.
2328 */
2329static void fcoe_flogi_resp(struct fc_seq *seq, struct fc_frame *fp, void *arg)
2330{
2331 struct fcoe_ctlr *fip = arg;
2332 struct fc_exch *exch = fc_seq_exch(seq);
2333 struct fc_lport *lport = exch->lp;
2334 u8 *mac;
2335
2336 if (IS_ERR(fp))
2337 goto done;
2338
2339 mac = fr_cb(fp)->granted_mac;
2340 if (is_zero_ether_addr(mac)) {
2341 /* pre-FIP */
Joe Eykholt386309c2009-11-03 11:49:16 -08002342 if (fcoe_ctlr_recv_flogi(fip, lport, fp)) {
Chris Leech11b56182009-11-03 11:46:29 -08002343 fc_frame_free(fp);
2344 return;
2345 }
Chris Leech11b56182009-11-03 11:46:29 -08002346 }
Joe Eykholt386309c2009-11-03 11:49:16 -08002347 fcoe_update_src_mac(lport, mac);
Chris Leech11b56182009-11-03 11:46:29 -08002348done:
2349 fc_lport_flogi_resp(seq, fp, lport);
2350}
2351
2352/**
2353 * fcoe_logo_resp() - FCoE specific LOGO response handler
2354 * @seq: active sequence in the LOGO exchange
2355 * @fp: response frame, or error encoded in a pointer (timeout)
2356 * @arg: pointer the the fcoe_ctlr structure
2357 *
Uwe Kleine-König65155b32010-06-11 12:17:01 +02002358 * This handles MAC address management for FCoE, then passes control on to
Chris Leech11b56182009-11-03 11:46:29 -08002359 * the libfc LOGO response handler.
2360 */
2361static void fcoe_logo_resp(struct fc_seq *seq, struct fc_frame *fp, void *arg)
2362{
Joe Eykholt386309c2009-11-03 11:49:16 -08002363 struct fc_lport *lport = arg;
Chris Leech11b56182009-11-03 11:46:29 -08002364 static u8 zero_mac[ETH_ALEN] = { 0 };
2365
2366 if (!IS_ERR(fp))
Joe Eykholt386309c2009-11-03 11:49:16 -08002367 fcoe_update_src_mac(lport, zero_mac);
Chris Leech11b56182009-11-03 11:46:29 -08002368 fc_lport_logo_resp(seq, fp, lport);
2369}
2370
2371/**
2372 * fcoe_elsct_send - FCoE specific ELS handler
2373 *
2374 * This does special case handling of FIP encapsualted ELS exchanges for FCoE,
2375 * using FCoE specific response handlers and passing the FIP controller as
2376 * the argument (the lport is still available from the exchange).
2377 *
2378 * Most of the work here is just handed off to the libfc routine.
2379 */
Robert Love1875f272009-11-03 11:47:50 -08002380static struct fc_seq *fcoe_elsct_send(struct fc_lport *lport, u32 did,
2381 struct fc_frame *fp, unsigned int op,
2382 void (*resp)(struct fc_seq *,
2383 struct fc_frame *,
2384 void *),
2385 void *arg, u32 timeout)
Chris Leech11b56182009-11-03 11:46:29 -08002386{
2387 struct fcoe_port *port = lport_priv(lport);
Bhanu Prakash Gollapudi8597ae82011-01-28 16:05:37 -08002388 struct fcoe_interface *fcoe = port->priv;
Chris Leech11b56182009-11-03 11:46:29 -08002389 struct fcoe_ctlr *fip = &fcoe->ctlr;
2390 struct fc_frame_header *fh = fc_frame_header_get(fp);
2391
2392 switch (op) {
2393 case ELS_FLOGI:
2394 case ELS_FDISC:
Joe Eykholte10f8c62010-07-20 15:20:30 -07002395 if (lport->point_to_multipoint)
2396 break;
Chris Leech11b56182009-11-03 11:46:29 -08002397 return fc_elsct_send(lport, did, fp, op, fcoe_flogi_resp,
2398 fip, timeout);
2399 case ELS_LOGO:
2400 /* only hook onto fabric logouts, not port logouts */
2401 if (ntoh24(fh->fh_d_id) != FC_FID_FLOGI)
2402 break;
2403 return fc_elsct_send(lport, did, fp, op, fcoe_logo_resp,
Joe Eykholt386309c2009-11-03 11:49:16 -08002404 lport, timeout);
Chris Leech11b56182009-11-03 11:46:29 -08002405 }
2406 return fc_elsct_send(lport, did, fp, op, resp, arg, timeout);
2407}
2408
Chris Leech9a057532009-11-03 11:46:40 -08002409/**
2410 * fcoe_vport_create() - create an fc_host/scsi_host for a vport
2411 * @vport: fc_vport object to create a new fc_host for
2412 * @disabled: start the new fc_host in a disabled state by default?
2413 *
2414 * Returns: 0 for success
2415 */
2416static int fcoe_vport_create(struct fc_vport *vport, bool disabled)
2417{
2418 struct Scsi_Host *shost = vport_to_shost(vport);
2419 struct fc_lport *n_port = shost_priv(shost);
2420 struct fcoe_port *port = lport_priv(n_port);
Bhanu Prakash Gollapudi8597ae82011-01-28 16:05:37 -08002421 struct fcoe_interface *fcoe = port->priv;
Chris Leech9a057532009-11-03 11:46:40 -08002422 struct net_device *netdev = fcoe->netdev;
2423 struct fc_lport *vn_port;
Neerav Parikhbdf25212011-05-16 16:45:29 -07002424 int rc;
2425 char buf[32];
2426
2427 rc = fcoe_validate_vport_create(vport);
2428 if (rc) {
Bhanu Prakash Gollapudid8348952011-08-04 17:38:49 -07002429 fcoe_wwn_to_str(vport->port_name, buf, sizeof(buf));
Neerav Parikhbdf25212011-05-16 16:45:29 -07002430 printk(KERN_ERR "fcoe: Failed to create vport, "
2431 "WWPN (0x%s) already exists\n",
2432 buf);
2433 return rc;
2434 }
Chris Leech9a057532009-11-03 11:46:40 -08002435
2436 mutex_lock(&fcoe_config_mutex);
2437 vn_port = fcoe_if_create(fcoe, &vport->dev, 1);
2438 mutex_unlock(&fcoe_config_mutex);
2439
2440 if (IS_ERR(vn_port)) {
2441 printk(KERN_ERR "fcoe: fcoe_vport_create(%s) failed\n",
2442 netdev->name);
2443 return -EIO;
2444 }
2445
2446 if (disabled) {
2447 fc_vport_set_state(vport, FC_VPORT_DISABLED);
2448 } else {
2449 vn_port->boot_time = jiffies;
2450 fc_fabric_login(vn_port);
2451 fc_vport_setlink(vn_port);
2452 }
2453 return 0;
2454}
2455
2456/**
2457 * fcoe_vport_destroy() - destroy the fc_host/scsi_host for a vport
2458 * @vport: fc_vport object that is being destroyed
2459 *
2460 * Returns: 0 for success
2461 */
2462static int fcoe_vport_destroy(struct fc_vport *vport)
2463{
2464 struct Scsi_Host *shost = vport_to_shost(vport);
2465 struct fc_lport *n_port = shost_priv(shost);
2466 struct fc_lport *vn_port = vport->dd_data;
2467 struct fcoe_port *port = lport_priv(vn_port);
2468
2469 mutex_lock(&n_port->lp_mutex);
2470 list_del(&vn_port->list);
2471 mutex_unlock(&n_port->lp_mutex);
Tejun Heo2ca32b42011-01-28 16:05:32 -08002472 queue_work(fcoe_wq, &port->destroy_work);
Chris Leech9a057532009-11-03 11:46:40 -08002473 return 0;
2474}
2475
2476/**
2477 * fcoe_vport_disable() - change vport state
2478 * @vport: vport to bring online/offline
2479 * @disable: should the vport be disabled?
2480 */
2481static int fcoe_vport_disable(struct fc_vport *vport, bool disable)
2482{
2483 struct fc_lport *lport = vport->dd_data;
2484
2485 if (disable) {
2486 fc_vport_set_state(vport, FC_VPORT_DISABLED);
2487 fc_fabric_logoff(lport);
2488 } else {
2489 lport->boot_time = jiffies;
2490 fc_fabric_login(lport);
2491 fc_vport_setlink(lport);
2492 }
2493
2494 return 0;
2495}
2496
Chris Leechdc8596d2009-11-03 11:47:18 -08002497/**
2498 * fcoe_vport_set_symbolic_name() - append vport string to symbolic name
2499 * @vport: fc_vport with a new symbolic name string
2500 *
2501 * After generating a new symbolic name string, a new RSPN_ID request is
2502 * sent to the name server. There is no response handler, so if it fails
2503 * for some reason it will not be retried.
2504 */
2505static void fcoe_set_vport_symbolic_name(struct fc_vport *vport)
2506{
2507 struct fc_lport *lport = vport->dd_data;
2508 struct fc_frame *fp;
2509 size_t len;
2510
2511 snprintf(fc_host_symbolic_name(lport->host), FC_SYMBOLIC_NAME_SIZE,
2512 "%s v%s over %s : %s", FCOE_NAME, FCOE_VERSION,
2513 fcoe_netdev(lport)->name, vport->symbolic_name);
2514
2515 if (lport->state != LPORT_ST_READY)
2516 return;
2517
2518 len = strnlen(fc_host_symbolic_name(lport->host), 255);
2519 fp = fc_frame_alloc(lport,
2520 sizeof(struct fc_ct_hdr) +
2521 sizeof(struct fc_ns_rspn) + len);
2522 if (!fp)
2523 return;
2524 lport->tt.elsct_send(lport, FC_FID_DIR_SERV, fp, FC_NS_RSPN_ID,
Joe Eykholtb94f8952009-11-03 11:50:21 -08002525 NULL, NULL, 3 * lport->r_a_tov);
Chris Leechdc8596d2009-11-03 11:47:18 -08002526}
Yi Zoub84056b2009-11-20 14:55:19 -08002527
2528/**
2529 * fcoe_get_lesb() - Fill the FCoE Link Error Status Block
2530 * @lport: the local port
2531 * @fc_lesb: the link error status block
2532 */
2533static void fcoe_get_lesb(struct fc_lport *lport,
2534 struct fc_els_lesb *fc_lesb)
2535{
2536 unsigned int cpu;
2537 u32 lfc, vlfc, mdac;
2538 struct fcoe_dev_stats *devst;
2539 struct fcoe_fc_els_lesb *lesb;
Eric Dumazet28172732010-07-07 14:58:56 -07002540 struct rtnl_link_stats64 temp;
Yi Zoub84056b2009-11-20 14:55:19 -08002541 struct net_device *netdev = fcoe_netdev(lport);
2542
2543 lfc = 0;
2544 vlfc = 0;
2545 mdac = 0;
2546 lesb = (struct fcoe_fc_els_lesb *)fc_lesb;
2547 memset(lesb, 0, sizeof(*lesb));
2548 for_each_possible_cpu(cpu) {
2549 devst = per_cpu_ptr(lport->dev_stats, cpu);
2550 lfc += devst->LinkFailureCount;
2551 vlfc += devst->VLinkFailureCount;
2552 mdac += devst->MissDiscAdvCount;
2553 }
2554 lesb->lesb_link_fail = htonl(lfc);
2555 lesb->lesb_vlink_fail = htonl(vlfc);
2556 lesb->lesb_miss_fka = htonl(mdac);
Eric Dumazet28172732010-07-07 14:58:56 -07002557 lesb->lesb_fcs_error = htonl(dev_get_stats(netdev, &temp)->rx_crc_errors);
Yi Zoub84056b2009-11-20 14:55:19 -08002558}
Joe Eykholt7d65b0d2010-03-12 16:08:02 -08002559
2560/**
2561 * fcoe_set_port_id() - Callback from libfc when Port_ID is set.
2562 * @lport: the local port
2563 * @port_id: the port ID
2564 * @fp: the received frame, if any, that caused the port_id to be set.
2565 *
2566 * This routine handles the case where we received a FLOGI and are
2567 * entering point-to-point mode. We need to call fcoe_ctlr_recv_flogi()
2568 * so it can set the non-mapped mode and gateway address.
2569 *
2570 * The FLOGI LS_ACC is handled by fcoe_flogi_resp().
2571 */
2572static void fcoe_set_port_id(struct fc_lport *lport,
2573 u32 port_id, struct fc_frame *fp)
2574{
2575 struct fcoe_port *port = lport_priv(lport);
Bhanu Prakash Gollapudi8597ae82011-01-28 16:05:37 -08002576 struct fcoe_interface *fcoe = port->priv;
Joe Eykholt7d65b0d2010-03-12 16:08:02 -08002577
2578 if (fp && fc_frame_payload_op(fp) == ELS_FLOGI)
2579 fcoe_ctlr_recv_flogi(&fcoe->ctlr, lport, fp);
2580}