blob: 010432596eafd693ee9e72ed7854f91bb7fe6fd2 [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
Eric Dumazet5c609ff92011-09-27 21:37:52 -07001116 thread = kthread_create_on_node(fcoe_percpu_receive_thread,
1117 (void *)p, cpu_to_node(cpu),
1118 "fcoethread/%d", cpu);
Robert Love8976f422009-03-17 11:41:46 -07001119
Joe Eykholte7a51992009-08-25 14:04:08 -07001120 if (likely(!IS_ERR(thread))) {
Robert Love8976f422009-03-17 11:41:46 -07001121 kthread_bind(thread, cpu);
1122 wake_up_process(thread);
1123
1124 spin_lock_bh(&p->fcoe_rx_list.lock);
1125 p->thread = thread;
1126 spin_unlock_bh(&p->fcoe_rx_list.lock);
1127 }
1128}
1129
1130/**
Robert Love1875f272009-11-03 11:47:50 -08001131 * fcoe_percpu_thread_destroy() - Remove the receive thread of a CPU
1132 * @cpu: The CPU index of the CPU whose receive thread is to be destroyed
Robert Love8976f422009-03-17 11:41:46 -07001133 *
1134 * Destroys a per-CPU Rx thread. Any pending skbs are moved to the
1135 * current CPU's Rx thread. If the thread being destroyed is bound to
1136 * the CPU processing this context the skbs will be freed.
1137 */
1138static void fcoe_percpu_thread_destroy(unsigned int cpu)
1139{
1140 struct fcoe_percpu_s *p;
1141 struct task_struct *thread;
1142 struct page *crc_eof;
1143 struct sk_buff *skb;
1144#ifdef CONFIG_SMP
1145 struct fcoe_percpu_s *p0;
Joe Eykholtf018b732010-03-12 16:08:55 -08001146 unsigned targ_cpu = get_cpu();
Robert Love8976f422009-03-17 11:41:46 -07001147#endif /* CONFIG_SMP */
1148
Robert Loved5488eb2009-06-10 15:30:59 -07001149 FCOE_DBG("Destroying receive thread for CPU %d\n", cpu);
Robert Love8976f422009-03-17 11:41:46 -07001150
1151 /* Prevent any new skbs from being queued for this CPU. */
1152 p = &per_cpu(fcoe_percpu, cpu);
1153 spin_lock_bh(&p->fcoe_rx_list.lock);
1154 thread = p->thread;
1155 p->thread = NULL;
1156 crc_eof = p->crc_eof_page;
1157 p->crc_eof_page = NULL;
1158 p->crc_eof_offset = 0;
1159 spin_unlock_bh(&p->fcoe_rx_list.lock);
1160
1161#ifdef CONFIG_SMP
1162 /*
1163 * Don't bother moving the skb's if this context is running
1164 * on the same CPU that is having its thread destroyed. This
1165 * can easily happen when the module is removed.
1166 */
1167 if (cpu != targ_cpu) {
1168 p0 = &per_cpu(fcoe_percpu, targ_cpu);
1169 spin_lock_bh(&p0->fcoe_rx_list.lock);
1170 if (p0->thread) {
Robert Loved5488eb2009-06-10 15:30:59 -07001171 FCOE_DBG("Moving frames from CPU %d to CPU %d\n",
1172 cpu, targ_cpu);
Robert Love8976f422009-03-17 11:41:46 -07001173
1174 while ((skb = __skb_dequeue(&p->fcoe_rx_list)) != NULL)
1175 __skb_queue_tail(&p0->fcoe_rx_list, skb);
1176 spin_unlock_bh(&p0->fcoe_rx_list.lock);
1177 } else {
1178 /*
1179 * The targeted CPU is not initialized and cannot accept
Robert Love1875f272009-11-03 11:47:50 -08001180 * new skbs. Unlock the targeted CPU and drop the skbs
Robert Love8976f422009-03-17 11:41:46 -07001181 * on the CPU that is going offline.
1182 */
1183 while ((skb = __skb_dequeue(&p->fcoe_rx_list)) != NULL)
1184 kfree_skb(skb);
1185 spin_unlock_bh(&p0->fcoe_rx_list.lock);
1186 }
1187 } else {
1188 /*
1189 * This scenario occurs when the module is being removed
1190 * and all threads are being destroyed. skbs will continue
1191 * to be shifted from the CPU thread that is being removed
1192 * to the CPU thread associated with the CPU that is processing
1193 * the module removal. Once there is only one CPU Rx thread it
1194 * will reach this case and we will drop all skbs and later
1195 * stop the thread.
1196 */
1197 spin_lock_bh(&p->fcoe_rx_list.lock);
1198 while ((skb = __skb_dequeue(&p->fcoe_rx_list)) != NULL)
1199 kfree_skb(skb);
1200 spin_unlock_bh(&p->fcoe_rx_list.lock);
1201 }
Joe Eykholtf018b732010-03-12 16:08:55 -08001202 put_cpu();
Robert Love8976f422009-03-17 11:41:46 -07001203#else
1204 /*
Chris Leechdd3fd722009-04-21 16:27:36 -07001205 * This a non-SMP scenario where the singular Rx thread is
Robert Love8976f422009-03-17 11:41:46 -07001206 * being removed. Free all skbs and stop the thread.
1207 */
1208 spin_lock_bh(&p->fcoe_rx_list.lock);
1209 while ((skb = __skb_dequeue(&p->fcoe_rx_list)) != NULL)
1210 kfree_skb(skb);
1211 spin_unlock_bh(&p->fcoe_rx_list.lock);
1212#endif
1213
1214 if (thread)
1215 kthread_stop(thread);
1216
1217 if (crc_eof)
1218 put_page(crc_eof);
1219}
1220
1221/**
Robert Love1875f272009-11-03 11:47:50 -08001222 * fcoe_cpu_callback() - Handler for CPU hotplug events
1223 * @nfb: The callback data block
1224 * @action: The event triggering the callback
1225 * @hcpu: The index of the CPU that the event is for
Robert Love8976f422009-03-17 11:41:46 -07001226 *
Robert Love1875f272009-11-03 11:47:50 -08001227 * This creates or destroys per-CPU data for fcoe
Robert Love8976f422009-03-17 11:41:46 -07001228 *
1229 * Returns NOTIFY_OK always.
1230 */
1231static int fcoe_cpu_callback(struct notifier_block *nfb,
1232 unsigned long action, void *hcpu)
1233{
1234 unsigned cpu = (unsigned long)hcpu;
1235
1236 switch (action) {
1237 case CPU_ONLINE:
1238 case CPU_ONLINE_FROZEN:
Robert Loved5488eb2009-06-10 15:30:59 -07001239 FCOE_DBG("CPU %x online: Create Rx thread\n", cpu);
Robert Love8976f422009-03-17 11:41:46 -07001240 fcoe_percpu_thread_create(cpu);
1241 break;
1242 case CPU_DEAD:
1243 case CPU_DEAD_FROZEN:
Robert Loved5488eb2009-06-10 15:30:59 -07001244 FCOE_DBG("CPU %x offline: Remove Rx thread\n", cpu);
Robert Love8976f422009-03-17 11:41:46 -07001245 fcoe_percpu_thread_destroy(cpu);
1246 break;
1247 default:
1248 break;
1249 }
1250 return NOTIFY_OK;
1251}
1252
Robert Love8976f422009-03-17 11:41:46 -07001253/**
Kiran Patil064287e2011-06-20 16:59:20 -07001254 * fcoe_select_cpu() - Selects CPU to handle post-processing of incoming
1255 * command.
Kiran Patil064287e2011-06-20 16:59:20 -07001256 *
Vasu Devd2722812011-07-27 15:11:10 -07001257 * This routine selects next CPU based on cpumask to distribute
1258 * incoming requests in round robin.
Kiran Patil064287e2011-06-20 16:59:20 -07001259 *
Vasu Devd2722812011-07-27 15:11:10 -07001260 * Returns: int CPU number
Kiran Patil064287e2011-06-20 16:59:20 -07001261 */
Vasu Devd2722812011-07-27 15:11:10 -07001262static inline unsigned int fcoe_select_cpu(void)
Kiran Patil064287e2011-06-20 16:59:20 -07001263{
1264 static unsigned int selected_cpu;
1265
Vasu Devd2722812011-07-27 15:11:10 -07001266 selected_cpu = cpumask_next(selected_cpu, cpu_online_mask);
1267 if (selected_cpu >= nr_cpu_ids)
1268 selected_cpu = cpumask_first(cpu_online_mask);
1269
Kiran Patil064287e2011-06-20 16:59:20 -07001270 return selected_cpu;
1271}
1272
1273/**
Robert Love1875f272009-11-03 11:47:50 -08001274 * fcoe_rcv() - Receive packets from a net device
1275 * @skb: The received packet
1276 * @netdev: The net device that the packet was received on
1277 * @ptype: The packet type context
1278 * @olddev: The last device net device
Robert Love85b4aa42008-12-09 15:10:24 -08001279 *
Robert Love1875f272009-11-03 11:47:50 -08001280 * This routine is called by NET_RX_SOFTIRQ. It receives a packet, builds a
1281 * FC frame and passes the frame to libfc.
Robert Love85b4aa42008-12-09 15:10:24 -08001282 *
1283 * Returns: 0 for success
Robert Love34f42a02009-02-27 10:55:45 -08001284 */
Robert Love1875f272009-11-03 11:47:50 -08001285int fcoe_rcv(struct sk_buff *skb, struct net_device *netdev,
Robert Love85b4aa42008-12-09 15:10:24 -08001286 struct packet_type *ptype, struct net_device *olddev)
1287{
Robert Love1875f272009-11-03 11:47:50 -08001288 struct fc_lport *lport;
Robert Love85b4aa42008-12-09 15:10:24 -08001289 struct fcoe_rcv_info *fr;
Chris Leech259ad852009-08-25 13:59:41 -07001290 struct fcoe_interface *fcoe;
Robert Love85b4aa42008-12-09 15:10:24 -08001291 struct fc_frame_header *fh;
Robert Love85b4aa42008-12-09 15:10:24 -08001292 struct fcoe_percpu_s *fps;
Vasu Dev519e5132010-07-20 15:19:32 -07001293 struct ethhdr *eh;
Vasu Devb2f00912009-08-25 13:58:53 -07001294 unsigned int cpu;
Robert Love85b4aa42008-12-09 15:10:24 -08001295
Chris Leech259ad852009-08-25 13:59:41 -07001296 fcoe = container_of(ptype, struct fcoe_interface, fcoe_packet_type);
Robert Love1875f272009-11-03 11:47:50 -08001297 lport = fcoe->ctlr.lp;
1298 if (unlikely(!lport)) {
1299 FCOE_NETDEV_DBG(netdev, "Cannot find hba structure");
Robert Love85b4aa42008-12-09 15:10:24 -08001300 goto err2;
1301 }
Robert Love1875f272009-11-03 11:47:50 -08001302 if (!lport->link_up)
Joe Eykholt97c83892009-03-17 11:42:40 -07001303 goto err2;
Robert Love85b4aa42008-12-09 15:10:24 -08001304
Robert Love1875f272009-11-03 11:47:50 -08001305 FCOE_NETDEV_DBG(netdev, "skb_info: len:%d data_len:%d head:%p "
Robert Loved5488eb2009-06-10 15:30:59 -07001306 "data:%p tail:%p end:%p sum:%d dev:%s",
1307 skb->len, skb->data_len, skb->head, skb->data,
1308 skb_tail_pointer(skb), skb_end_pointer(skb),
1309 skb->csum, skb->dev ? skb->dev->name : "<NULL>");
Robert Love85b4aa42008-12-09 15:10:24 -08001310
Vasu Dev519e5132010-07-20 15:19:32 -07001311 eh = eth_hdr(skb);
Vasu Dev519e5132010-07-20 15:19:32 -07001312
1313 if (is_fip_mode(&fcoe->ctlr) &&
1314 compare_ether_addr(eh->h_source, fcoe->ctlr.dest_addr)) {
1315 FCOE_NETDEV_DBG(netdev, "wrong source mac address:%pM\n",
1316 eh->h_source);
Robert Love85b4aa42008-12-09 15:10:24 -08001317 goto err;
1318 }
1319
1320 /*
1321 * Check for minimum frame length, and make sure required FCoE
1322 * and FC headers are pulled into the linear data area.
1323 */
1324 if (unlikely((skb->len < FCOE_MIN_FRAME) ||
Robert Love1875f272009-11-03 11:47:50 -08001325 !pskb_may_pull(skb, FCOE_HEADER_LEN)))
Robert Love85b4aa42008-12-09 15:10:24 -08001326 goto err;
1327
1328 skb_set_transport_header(skb, sizeof(struct fcoe_hdr));
1329 fh = (struct fc_frame_header *) skb_transport_header(skb);
1330
Robert Love0ee31cb2010-10-08 17:12:46 -07001331 if (ntoh24(&eh->h_dest[3]) != ntoh24(fh->fh_d_id)) {
1332 FCOE_NETDEV_DBG(netdev, "FC frame d_id mismatch with MAC:%pM\n",
1333 eh->h_dest);
1334 goto err;
1335 }
1336
Robert Love85b4aa42008-12-09 15:10:24 -08001337 fr = fcoe_dev_from_skb(skb);
Robert Love1875f272009-11-03 11:47:50 -08001338 fr->fr_dev = lport;
Robert Love5e5e92d2009-03-17 11:41:35 -07001339
Robert Love85b4aa42008-12-09 15:10:24 -08001340 /*
Vasu Devb2f00912009-08-25 13:58:53 -07001341 * In case the incoming frame's exchange is originated from
1342 * the initiator, then received frame's exchange id is ANDed
1343 * with fc_cpu_mask bits to get the same cpu on which exchange
Vasu Devd2722812011-07-27 15:11:10 -07001344 * was originated, otherwise select cpu using rx exchange id
1345 * or fcoe_select_cpu().
Robert Love85b4aa42008-12-09 15:10:24 -08001346 */
Vasu Devb2f00912009-08-25 13:58:53 -07001347 if (ntoh24(fh->fh_f_ctl) & FC_FC_EX_CTX)
1348 cpu = ntohs(fh->fh_ox_id) & fc_cpu_mask;
Kiran Patil064287e2011-06-20 16:59:20 -07001349 else {
Vasu Devd2722812011-07-27 15:11:10 -07001350 if (ntohs(fh->fh_rx_id) == FC_XID_UNKNOWN)
1351 cpu = fcoe_select_cpu();
1352 else
Kiran Patil29bdd2b2011-06-20 16:59:25 -07001353 cpu = ntohs(fh->fh_rx_id) & fc_cpu_mask;
Kiran Patil064287e2011-06-20 16:59:20 -07001354 }
Vasu Dev324f6672011-07-27 15:10:39 -07001355
1356 if (cpu >= nr_cpu_ids)
1357 goto err;
1358
Robert Love8976f422009-03-17 11:41:46 -07001359 fps = &per_cpu(fcoe_percpu, cpu);
Robert Love85b4aa42008-12-09 15:10:24 -08001360 spin_lock_bh(&fps->fcoe_rx_list.lock);
Robert Love8976f422009-03-17 11:41:46 -07001361 if (unlikely(!fps->thread)) {
1362 /*
1363 * The targeted CPU is not ready, let's target
1364 * the first CPU now. For non-SMP systems this
1365 * will check the same CPU twice.
1366 */
Robert Love1875f272009-11-03 11:47:50 -08001367 FCOE_NETDEV_DBG(netdev, "CPU is online, but no receive thread "
Robert Loved5488eb2009-06-10 15:30:59 -07001368 "ready for incoming skb- using first online "
1369 "CPU.\n");
Robert Love8976f422009-03-17 11:41:46 -07001370
1371 spin_unlock_bh(&fps->fcoe_rx_list.lock);
Rusty Russell69571772009-12-17 11:43:14 -06001372 cpu = cpumask_first(cpu_online_mask);
Robert Love8976f422009-03-17 11:41:46 -07001373 fps = &per_cpu(fcoe_percpu, cpu);
1374 spin_lock_bh(&fps->fcoe_rx_list.lock);
1375 if (!fps->thread) {
1376 spin_unlock_bh(&fps->fcoe_rx_list.lock);
1377 goto err;
1378 }
1379 }
1380
1381 /*
1382 * We now have a valid CPU that we're targeting for
1383 * this skb. We also have this receive thread locked,
1384 * so we're free to queue skbs into it's queue.
1385 */
Robert Love85b4aa42008-12-09 15:10:24 -08001386
Chris Leech859b7b62009-11-20 14:54:47 -08001387 /* If this is a SCSI-FCP frame, and this is already executing on the
1388 * correct CPU, and the queue for this CPU is empty, then go ahead
1389 * and process the frame directly in the softirq context.
1390 * This lets us process completions without context switching from the
1391 * NET_RX softirq, to our receive processing thread, and then back to
1392 * BLOCK softirq context.
1393 */
1394 if (fh->fh_type == FC_TYPE_FCP &&
1395 cpu == smp_processor_id() &&
1396 skb_queue_empty(&fps->fcoe_rx_list)) {
1397 spin_unlock_bh(&fps->fcoe_rx_list.lock);
1398 fcoe_recv_frame(skb);
1399 } else {
1400 __skb_queue_tail(&fps->fcoe_rx_list, skb);
1401 if (fps->fcoe_rx_list.qlen == 1)
1402 wake_up_process(fps->thread);
1403 spin_unlock_bh(&fps->fcoe_rx_list.lock);
1404 }
Robert Love85b4aa42008-12-09 15:10:24 -08001405
1406 return 0;
1407err:
Joe Eykholtf018b732010-03-12 16:08:55 -08001408 per_cpu_ptr(lport->dev_stats, get_cpu())->ErrorFrames++;
1409 put_cpu();
Robert Love85b4aa42008-12-09 15:10:24 -08001410err2:
1411 kfree_skb(skb);
1412 return -1;
1413}
Robert Love85b4aa42008-12-09 15:10:24 -08001414
1415/**
Bhanu Prakash Gollapudi8597ae82011-01-28 16:05:37 -08001416 * fcoe_alloc_paged_crc_eof() - Allocate a page to be used for the trailer CRC
Robert Love1875f272009-11-03 11:47:50 -08001417 * @skb: The packet to be transmitted
1418 * @tlen: The total length of the trailer
1419 *
Robert Love85b4aa42008-12-09 15:10:24 -08001420 * Returns: 0 for success
Robert Love34f42a02009-02-27 10:55:45 -08001421 */
Bhanu Prakash Gollapudi8597ae82011-01-28 16:05:37 -08001422static int fcoe_alloc_paged_crc_eof(struct sk_buff *skb, int tlen)
Robert Love85b4aa42008-12-09 15:10:24 -08001423{
1424 struct fcoe_percpu_s *fps;
Bhanu Prakash Gollapudi8597ae82011-01-28 16:05:37 -08001425 int rc;
Robert Love85b4aa42008-12-09 15:10:24 -08001426
Robert Love5e5e92d2009-03-17 11:41:35 -07001427 fps = &get_cpu_var(fcoe_percpu);
Bhanu Prakash Gollapudi8597ae82011-01-28 16:05:37 -08001428 rc = fcoe_get_paged_crc_eof(skb, tlen, fps);
Robert Love5e5e92d2009-03-17 11:41:35 -07001429 put_cpu_var(fcoe_percpu);
Robert Love85b4aa42008-12-09 15:10:24 -08001430
Bhanu Prakash Gollapudi8597ae82011-01-28 16:05:37 -08001431 return rc;
Robert Love85b4aa42008-12-09 15:10:24 -08001432}
Robert Love85b4aa42008-12-09 15:10:24 -08001433
1434/**
Robert Love1875f272009-11-03 11:47:50 -08001435 * fcoe_xmit() - Transmit a FCoE frame
1436 * @lport: The local port that the frame is to be transmitted for
1437 * @fp: The frame to be transmitted
Robert Love85b4aa42008-12-09 15:10:24 -08001438 *
Robert Love1875f272009-11-03 11:47:50 -08001439 * Return: 0 for success
Robert Love34f42a02009-02-27 10:55:45 -08001440 */
Robert Love1875f272009-11-03 11:47:50 -08001441int fcoe_xmit(struct fc_lport *lport, struct fc_frame *fp)
Robert Love85b4aa42008-12-09 15:10:24 -08001442{
Vasu Dev4bb6b512009-05-06 10:52:34 -07001443 int wlen;
Robert Love85b4aa42008-12-09 15:10:24 -08001444 u32 crc;
1445 struct ethhdr *eh;
1446 struct fcoe_crc_eof *cp;
1447 struct sk_buff *skb;
1448 struct fcoe_dev_stats *stats;
1449 struct fc_frame_header *fh;
1450 unsigned int hlen; /* header length implies the version */
1451 unsigned int tlen; /* trailer length */
1452 unsigned int elen; /* eth header, may include vlan */
Robert Love1875f272009-11-03 11:47:50 -08001453 struct fcoe_port *port = lport_priv(lport);
Bhanu Prakash Gollapudi8597ae82011-01-28 16:05:37 -08001454 struct fcoe_interface *fcoe = port->priv;
Robert Love85b4aa42008-12-09 15:10:24 -08001455 u8 sof, eof;
1456 struct fcoe_hdr *hp;
1457
1458 WARN_ON((fr_len(fp) % sizeof(u32)) != 0);
1459
Robert Love85b4aa42008-12-09 15:10:24 -08001460 fh = fc_frame_header_get(fp);
Joe Eykholt97c83892009-03-17 11:42:40 -07001461 skb = fp_skb(fp);
1462 wlen = skb->len / FCOE_WORD_TO_BYTE;
1463
Robert Love1875f272009-11-03 11:47:50 -08001464 if (!lport->link_up) {
Dan Carpenter3caf02e2009-04-21 16:27:25 -07001465 kfree_skb(skb);
Joe Eykholt97c83892009-03-17 11:42:40 -07001466 return 0;
Robert Love85b4aa42008-12-09 15:10:24 -08001467 }
1468
Joe Eykholt9860eeb2010-03-12 16:07:52 -08001469 if (unlikely(fh->fh_type == FC_TYPE_ELS) &&
Robert Love1875f272009-11-03 11:47:50 -08001470 fcoe_ctlr_els_send(&fcoe->ctlr, lport, skb))
Joe Eykholt97c83892009-03-17 11:42:40 -07001471 return 0;
1472
Robert Love85b4aa42008-12-09 15:10:24 -08001473 sof = fr_sof(fp);
1474 eof = fr_eof(fp);
1475
Vasu Dev4e57e1c2009-05-06 10:52:46 -07001476 elen = sizeof(struct ethhdr);
Robert Love85b4aa42008-12-09 15:10:24 -08001477 hlen = sizeof(struct fcoe_hdr);
1478 tlen = sizeof(struct fcoe_crc_eof);
1479 wlen = (skb->len - tlen + sizeof(crc)) / FCOE_WORD_TO_BYTE;
1480
1481 /* crc offload */
Robert Love1875f272009-11-03 11:47:50 -08001482 if (likely(lport->crc_offload)) {
Yi Zou39ca9a02009-02-27 14:07:15 -08001483 skb->ip_summed = CHECKSUM_PARTIAL;
Robert Love85b4aa42008-12-09 15:10:24 -08001484 skb->csum_start = skb_headroom(skb);
1485 skb->csum_offset = skb->len;
1486 crc = 0;
1487 } else {
1488 skb->ip_summed = CHECKSUM_NONE;
1489 crc = fcoe_fc_crc(fp);
1490 }
1491
Chris Leech014f5c32009-08-25 13:59:30 -07001492 /* copy port crc and eof to the skb buff */
Robert Love85b4aa42008-12-09 15:10:24 -08001493 if (skb_is_nonlinear(skb)) {
1494 skb_frag_t *frag;
Bhanu Prakash Gollapudi8597ae82011-01-28 16:05:37 -08001495 if (fcoe_alloc_paged_crc_eof(skb, tlen)) {
Roel Kluine9041582009-02-27 10:56:22 -08001496 kfree_skb(skb);
Robert Love85b4aa42008-12-09 15:10:24 -08001497 return -ENOMEM;
1498 }
1499 frag = &skb_shinfo(skb)->frags[skb_shinfo(skb)->nr_frags - 1];
1500 cp = kmap_atomic(frag->page, KM_SKB_DATA_SOFTIRQ)
1501 + frag->page_offset;
1502 } else {
1503 cp = (struct fcoe_crc_eof *)skb_put(skb, tlen);
1504 }
1505
1506 memset(cp, 0, sizeof(*cp));
1507 cp->fcoe_eof = eof;
1508 cp->fcoe_crc32 = cpu_to_le32(~crc);
1509
1510 if (skb_is_nonlinear(skb)) {
1511 kunmap_atomic(cp, KM_SKB_DATA_SOFTIRQ);
1512 cp = NULL;
1513 }
1514
Chris Leech014f5c32009-08-25 13:59:30 -07001515 /* adjust skb network/transport offsets to match mac/fcoe/port */
Robert Love85b4aa42008-12-09 15:10:24 -08001516 skb_push(skb, elen + hlen);
1517 skb_reset_mac_header(skb);
1518 skb_reset_network_header(skb);
1519 skb->mac_len = elen;
Yi Zou211c7382009-02-27 14:06:37 -08001520 skb->protocol = htons(ETH_P_FCOE);
Chris Leech3fe9a0b2009-08-25 13:59:46 -07001521 skb->dev = fcoe->netdev;
Robert Love85b4aa42008-12-09 15:10:24 -08001522
1523 /* fill up mac and fcoe headers */
1524 eh = eth_hdr(skb);
1525 eh->h_proto = htons(ETH_P_FCOE);
Joe Eykholtcd229e42010-07-20 15:20:40 -07001526 memcpy(eh->h_dest, fcoe->ctlr.dest_addr, ETH_ALEN);
Chris Leech3fe9a0b2009-08-25 13:59:46 -07001527 if (fcoe->ctlr.map_dest)
Joe Eykholtcd229e42010-07-20 15:20:40 -07001528 memcpy(eh->h_dest + 3, fh->fh_d_id, 3);
Robert Love85b4aa42008-12-09 15:10:24 -08001529
Chris Leech3fe9a0b2009-08-25 13:59:46 -07001530 if (unlikely(fcoe->ctlr.flogi_oxid != FC_XID_UNKNOWN))
1531 memcpy(eh->h_source, fcoe->ctlr.ctl_src_addr, ETH_ALEN);
Robert Love85b4aa42008-12-09 15:10:24 -08001532 else
Chris Leech11b56182009-11-03 11:46:29 -08001533 memcpy(eh->h_source, port->data_src_addr, ETH_ALEN);
Robert Love85b4aa42008-12-09 15:10:24 -08001534
1535 hp = (struct fcoe_hdr *)(eh + 1);
1536 memset(hp, 0, sizeof(*hp));
1537 if (FC_FCOE_VER)
1538 FC_FCOE_ENCAPS_VER(hp, FC_FCOE_VER);
1539 hp->fcoe_sof = sof;
1540
Yi Zou39ca9a02009-02-27 14:07:15 -08001541 /* fcoe lso, mss is in max_payload which is non-zero for FCP data */
Robert Love1875f272009-11-03 11:47:50 -08001542 if (lport->seq_offload && fr_max_payload(fp)) {
Yi Zou39ca9a02009-02-27 14:07:15 -08001543 skb_shinfo(skb)->gso_type = SKB_GSO_FCOE;
1544 skb_shinfo(skb)->gso_size = fr_max_payload(fp);
1545 } else {
1546 skb_shinfo(skb)->gso_type = 0;
1547 skb_shinfo(skb)->gso_size = 0;
1548 }
Robert Love85b4aa42008-12-09 15:10:24 -08001549 /* update tx stats: regardless if LLD fails */
Joe Eykholtf018b732010-03-12 16:08:55 -08001550 stats = per_cpu_ptr(lport->dev_stats, get_cpu());
Robert Love582b45b2009-03-31 15:51:50 -07001551 stats->TxFrames++;
1552 stats->TxWords += wlen;
Joe Eykholtf018b732010-03-12 16:08:55 -08001553 put_cpu();
Robert Love85b4aa42008-12-09 15:10:24 -08001554
1555 /* send down to lld */
Robert Love1875f272009-11-03 11:47:50 -08001556 fr_dev(fp) = lport;
Vasu Dev980f5152011-07-27 15:11:05 -07001557 fcoe_port_send(port, skb);
Robert Love85b4aa42008-12-09 15:10:24 -08001558 return 0;
1559}
Robert Love85b4aa42008-12-09 15:10:24 -08001560
Robert Love34f42a02009-02-27 10:55:45 -08001561/**
Robert Love1875f272009-11-03 11:47:50 -08001562 * fcoe_percpu_flush_done() - Indicate per-CPU queue flush completion
1563 * @skb: The completed skb (argument required by destructor)
Joe Eykholte7a51992009-08-25 14:04:08 -07001564 */
1565static void fcoe_percpu_flush_done(struct sk_buff *skb)
1566{
1567 complete(&fcoe_flush_completion);
1568}
1569
1570/**
Vasu Dev52ee8322011-01-28 16:03:52 -08001571 * fcoe_filter_frames() - filter out bad fcoe frames, i.e. bad CRC
1572 * @lport: The local port the frame was received on
1573 * @fp: The received frame
1574 *
1575 * Return: 0 on passing filtering checks
1576 */
1577static inline int fcoe_filter_frames(struct fc_lport *lport,
1578 struct fc_frame *fp)
1579{
1580 struct fcoe_interface *fcoe;
1581 struct fc_frame_header *fh;
1582 struct sk_buff *skb = (struct sk_buff *)fp;
1583 struct fcoe_dev_stats *stats;
1584
1585 /*
1586 * We only check CRC if no offload is available and if it is
1587 * it's solicited data, in which case, the FCP layer would
1588 * check it during the copy.
1589 */
1590 if (lport->crc_offload && skb->ip_summed == CHECKSUM_UNNECESSARY)
1591 fr_flags(fp) &= ~FCPHF_CRC_UNCHECKED;
1592 else
1593 fr_flags(fp) |= FCPHF_CRC_UNCHECKED;
1594
1595 fh = (struct fc_frame_header *) skb_transport_header(skb);
1596 fh = fc_frame_header_get(fp);
1597 if (fh->fh_r_ctl == FC_RCTL_DD_SOL_DATA && fh->fh_type == FC_TYPE_FCP)
1598 return 0;
1599
Bhanu Prakash Gollapudi8597ae82011-01-28 16:05:37 -08001600 fcoe = ((struct fcoe_port *)lport_priv(lport))->priv;
Vasu Dev52ee8322011-01-28 16:03:52 -08001601 if (is_fip_mode(&fcoe->ctlr) && fc_frame_payload_op(fp) == ELS_LOGO &&
1602 ntoh24(fh->fh_s_id) == FC_FID_FLOGI) {
1603 FCOE_DBG("fcoe: dropping FCoE lport LOGO in fip mode\n");
1604 return -EINVAL;
1605 }
1606
Dan Carpenterf2f96d22011-02-25 15:03:23 -08001607 if (!(fr_flags(fp) & FCPHF_CRC_UNCHECKED) ||
Vasu Dev52ee8322011-01-28 16:03:52 -08001608 le32_to_cpu(fr_crc(fp)) == ~crc32(~0, skb->data, skb->len)) {
1609 fr_flags(fp) &= ~FCPHF_CRC_UNCHECKED;
1610 return 0;
1611 }
1612
1613 stats = per_cpu_ptr(lport->dev_stats, get_cpu());
1614 stats->InvalidCRCCount++;
1615 if (stats->InvalidCRCCount < 5)
1616 printk(KERN_WARNING "fcoe: dropping frame with CRC error\n");
1617 return -EINVAL;
1618}
1619
1620/**
Chris Leech859b7b62009-11-20 14:54:47 -08001621 * fcoe_recv_frame() - process a single received frame
1622 * @skb: frame to process
1623 */
1624static void fcoe_recv_frame(struct sk_buff *skb)
1625{
1626 u32 fr_len;
1627 struct fc_lport *lport;
1628 struct fcoe_rcv_info *fr;
1629 struct fcoe_dev_stats *stats;
Chris Leech859b7b62009-11-20 14:54:47 -08001630 struct fcoe_crc_eof crc_eof;
1631 struct fc_frame *fp;
Chris Leech859b7b62009-11-20 14:54:47 -08001632 struct fcoe_port *port;
1633 struct fcoe_hdr *hp;
1634
1635 fr = fcoe_dev_from_skb(skb);
1636 lport = fr->fr_dev;
1637 if (unlikely(!lport)) {
1638 if (skb->destructor != fcoe_percpu_flush_done)
1639 FCOE_NETDEV_DBG(skb->dev, "NULL lport in skb");
1640 kfree_skb(skb);
1641 return;
1642 }
1643
1644 FCOE_NETDEV_DBG(skb->dev, "skb_info: len:%d data_len:%d "
1645 "head:%p data:%p tail:%p end:%p sum:%d dev:%s",
1646 skb->len, skb->data_len,
1647 skb->head, skb->data, skb_tail_pointer(skb),
1648 skb_end_pointer(skb), skb->csum,
1649 skb->dev ? skb->dev->name : "<NULL>");
1650
Chris Leech859b7b62009-11-20 14:54:47 -08001651 port = lport_priv(lport);
1652 if (skb_is_nonlinear(skb))
1653 skb_linearize(skb); /* not ideal */
Chris Leech859b7b62009-11-20 14:54:47 -08001654
1655 /*
1656 * Frame length checks and setting up the header pointers
1657 * was done in fcoe_rcv already.
1658 */
1659 hp = (struct fcoe_hdr *) skb_network_header(skb);
Chris Leech859b7b62009-11-20 14:54:47 -08001660
Joe Eykholtf018b732010-03-12 16:08:55 -08001661 stats = per_cpu_ptr(lport->dev_stats, get_cpu());
Chris Leech859b7b62009-11-20 14:54:47 -08001662 if (unlikely(FC_FCOE_DECAPS_VER(hp) != FC_FCOE_VER)) {
1663 if (stats->ErrorFrames < 5)
1664 printk(KERN_WARNING "fcoe: FCoE version "
1665 "mismatch: The frame has "
1666 "version %x, but the "
1667 "initiator supports version "
1668 "%x\n", FC_FCOE_DECAPS_VER(hp),
1669 FC_FCOE_VER);
Joe Eykholtf018b732010-03-12 16:08:55 -08001670 goto drop;
Chris Leech859b7b62009-11-20 14:54:47 -08001671 }
1672
1673 skb_pull(skb, sizeof(struct fcoe_hdr));
1674 fr_len = skb->len - sizeof(struct fcoe_crc_eof);
1675
1676 stats->RxFrames++;
1677 stats->RxWords += fr_len / FCOE_WORD_TO_BYTE;
1678
1679 fp = (struct fc_frame *)skb;
1680 fc_frame_init(fp);
1681 fr_dev(fp) = lport;
1682 fr_sof(fp) = hp->fcoe_sof;
1683
1684 /* Copy out the CRC and EOF trailer for access */
Joe Eykholtf018b732010-03-12 16:08:55 -08001685 if (skb_copy_bits(skb, fr_len, &crc_eof, sizeof(crc_eof)))
1686 goto drop;
Chris Leech859b7b62009-11-20 14:54:47 -08001687 fr_eof(fp) = crc_eof.fcoe_eof;
1688 fr_crc(fp) = crc_eof.fcoe_crc32;
Joe Eykholtf018b732010-03-12 16:08:55 -08001689 if (pskb_trim(skb, fr_len))
1690 goto drop;
Chris Leech859b7b62009-11-20 14:54:47 -08001691
Vasu Dev52ee8322011-01-28 16:03:52 -08001692 if (!fcoe_filter_frames(lport, fp)) {
1693 put_cpu();
1694 fc_exch_recv(lport, fp);
1695 return;
Chris Leech859b7b62009-11-20 14:54:47 -08001696 }
Joe Eykholtf018b732010-03-12 16:08:55 -08001697drop:
1698 stats->ErrorFrames++;
1699 put_cpu();
1700 kfree_skb(skb);
Chris Leech859b7b62009-11-20 14:54:47 -08001701}
1702
1703/**
Robert Love1875f272009-11-03 11:47:50 -08001704 * fcoe_percpu_receive_thread() - The per-CPU packet receive thread
1705 * @arg: The per-CPU context
Robert Love85b4aa42008-12-09 15:10:24 -08001706 *
1707 * Return: 0 for success
Robert Love85b4aa42008-12-09 15:10:24 -08001708 */
1709int fcoe_percpu_receive_thread(void *arg)
1710{
1711 struct fcoe_percpu_s *p = arg;
Robert Love85b4aa42008-12-09 15:10:24 -08001712 struct sk_buff *skb;
Robert Love85b4aa42008-12-09 15:10:24 -08001713
Robert Love4469c192009-02-27 10:56:38 -08001714 set_user_nice(current, -20);
Robert Love85b4aa42008-12-09 15:10:24 -08001715
1716 while (!kthread_should_stop()) {
1717
1718 spin_lock_bh(&p->fcoe_rx_list.lock);
1719 while ((skb = __skb_dequeue(&p->fcoe_rx_list)) == NULL) {
1720 set_current_state(TASK_INTERRUPTIBLE);
1721 spin_unlock_bh(&p->fcoe_rx_list.lock);
1722 schedule();
1723 set_current_state(TASK_RUNNING);
1724 if (kthread_should_stop())
1725 return 0;
1726 spin_lock_bh(&p->fcoe_rx_list.lock);
1727 }
1728 spin_unlock_bh(&p->fcoe_rx_list.lock);
Chris Leech859b7b62009-11-20 14:54:47 -08001729 fcoe_recv_frame(skb);
Robert Love85b4aa42008-12-09 15:10:24 -08001730 }
1731 return 0;
1732}
1733
1734/**
Robert Love1875f272009-11-03 11:47:50 -08001735 * fcoe_dev_setup() - Setup the link change notification interface
Robert Love34f42a02009-02-27 10:55:45 -08001736 */
Randy Dunlapb0d428a2009-04-27 21:49:31 -07001737static void fcoe_dev_setup(void)
Robert Love85b4aa42008-12-09 15:10:24 -08001738{
Robert Love85b4aa42008-12-09 15:10:24 -08001739 register_netdevice_notifier(&fcoe_notifier);
1740}
1741
1742/**
Robert Love1875f272009-11-03 11:47:50 -08001743 * fcoe_dev_cleanup() - Cleanup the link change notification interface
Robert Love34f42a02009-02-27 10:55:45 -08001744 */
Robert Love85b4aa42008-12-09 15:10:24 -08001745static void fcoe_dev_cleanup(void)
1746{
1747 unregister_netdevice_notifier(&fcoe_notifier);
1748}
1749
1750/**
Robert Love1875f272009-11-03 11:47:50 -08001751 * fcoe_device_notification() - Handler for net device events
1752 * @notifier: The context of the notification
1753 * @event: The type of event
1754 * @ptr: The net device that the event was on
Robert Love85b4aa42008-12-09 15:10:24 -08001755 *
Robert Love1875f272009-11-03 11:47:50 -08001756 * This function is called by the Ethernet driver in case of link change event.
Robert Love85b4aa42008-12-09 15:10:24 -08001757 *
1758 * Returns: 0 for success
Robert Love34f42a02009-02-27 10:55:45 -08001759 */
Robert Love85b4aa42008-12-09 15:10:24 -08001760static int fcoe_device_notification(struct notifier_block *notifier,
1761 ulong event, void *ptr)
1762{
Robert Love1875f272009-11-03 11:47:50 -08001763 struct fc_lport *lport = NULL;
Vasu Dev1d1b88d2009-07-29 17:05:45 -07001764 struct net_device *netdev = ptr;
Chris Leech014f5c32009-08-25 13:59:30 -07001765 struct fcoe_interface *fcoe;
Chris Leech2e70e242009-08-25 14:00:23 -07001766 struct fcoe_port *port;
Robert Love85b4aa42008-12-09 15:10:24 -08001767 struct fcoe_dev_stats *stats;
Joe Eykholt97c83892009-03-17 11:42:40 -07001768 u32 link_possible = 1;
Robert Love85b4aa42008-12-09 15:10:24 -08001769 u32 mfs;
1770 int rc = NOTIFY_OK;
1771
Chris Leech014f5c32009-08-25 13:59:30 -07001772 list_for_each_entry(fcoe, &fcoe_hostlist, list) {
Chris Leech25024982009-08-25 13:59:35 -07001773 if (fcoe->netdev == netdev) {
Robert Love1875f272009-11-03 11:47:50 -08001774 lport = fcoe->ctlr.lp;
Robert Love85b4aa42008-12-09 15:10:24 -08001775 break;
1776 }
1777 }
Robert Love1875f272009-11-03 11:47:50 -08001778 if (!lport) {
Robert Love85b4aa42008-12-09 15:10:24 -08001779 rc = NOTIFY_DONE;
1780 goto out;
1781 }
1782
Robert Love85b4aa42008-12-09 15:10:24 -08001783 switch (event) {
1784 case NETDEV_DOWN:
1785 case NETDEV_GOING_DOWN:
Joe Eykholt97c83892009-03-17 11:42:40 -07001786 link_possible = 0;
Robert Love85b4aa42008-12-09 15:10:24 -08001787 break;
1788 case NETDEV_UP:
1789 case NETDEV_CHANGE:
Robert Love85b4aa42008-12-09 15:10:24 -08001790 break;
1791 case NETDEV_CHANGEMTU:
Yi Zou7221d7e2009-10-21 16:27:52 -07001792 if (netdev->features & NETIF_F_FCOE_MTU)
1793 break;
Vasu Dev1d1b88d2009-07-29 17:05:45 -07001794 mfs = netdev->mtu - (sizeof(struct fcoe_hdr) +
1795 sizeof(struct fcoe_crc_eof));
Robert Love85b4aa42008-12-09 15:10:24 -08001796 if (mfs >= FC_MIN_MAX_FRAME)
Robert Love1875f272009-11-03 11:47:50 -08001797 fc_set_mfs(lport, mfs);
Robert Love85b4aa42008-12-09 15:10:24 -08001798 break;
1799 case NETDEV_REGISTER:
1800 break;
Chris Leech2e70e242009-08-25 14:00:23 -07001801 case NETDEV_UNREGISTER:
1802 list_del(&fcoe->list);
1803 port = lport_priv(fcoe->ctlr.lp);
Tejun Heo2ca32b42011-01-28 16:05:32 -08001804 queue_work(fcoe_wq, &port->destroy_work);
Chris Leech2e70e242009-08-25 14:00:23 -07001805 goto out;
1806 break;
Yi Zou54a5b212010-07-20 15:21:17 -07001807 case NETDEV_FEAT_CHANGE:
1808 fcoe_netdev_features_change(lport, netdev);
1809 break;
Robert Love85b4aa42008-12-09 15:10:24 -08001810 default:
Vasu Dev1d1b88d2009-07-29 17:05:45 -07001811 FCOE_NETDEV_DBG(netdev, "Unknown event %ld "
Robert Loved5488eb2009-06-10 15:30:59 -07001812 "from netdev netlink\n", event);
Robert Love85b4aa42008-12-09 15:10:24 -08001813 }
Robert Love5e4f8fe2010-05-07 15:18:35 -07001814
1815 fcoe_link_speed_update(lport);
1816
Robert Love1875f272009-11-03 11:47:50 -08001817 if (link_possible && !fcoe_link_ok(lport))
Chris Leech3fe9a0b2009-08-25 13:59:46 -07001818 fcoe_ctlr_link_up(&fcoe->ctlr);
1819 else if (fcoe_ctlr_link_down(&fcoe->ctlr)) {
Joe Eykholtf018b732010-03-12 16:08:55 -08001820 stats = per_cpu_ptr(lport->dev_stats, get_cpu());
Joe Eykholt97c83892009-03-17 11:42:40 -07001821 stats->LinkFailureCount++;
Joe Eykholtf018b732010-03-12 16:08:55 -08001822 put_cpu();
Robert Love1875f272009-11-03 11:47:50 -08001823 fcoe_clean_pending_queue(lport);
Robert Love85b4aa42008-12-09 15:10:24 -08001824 }
1825out:
1826 return rc;
1827}
1828
1829/**
Vasu Dev55a66d32009-12-10 09:59:31 -08001830 * fcoe_disable() - Disables a FCoE interface
Yi Zou78a58242011-01-28 16:05:16 -08001831 * @netdev : The net_device object the Ethernet interface to create on
Vasu Dev55a66d32009-12-10 09:59:31 -08001832 *
Yi Zou78a58242011-01-28 16:05:16 -08001833 * Called from fcoe transport.
Vasu Dev55a66d32009-12-10 09:59:31 -08001834 *
1835 * Returns: 0 for success
1836 */
Yi Zou78a58242011-01-28 16:05:16 -08001837static int fcoe_disable(struct net_device *netdev)
Vasu Dev55a66d32009-12-10 09:59:31 -08001838{
1839 struct fcoe_interface *fcoe;
Vasu Dev55a66d32009-12-10 09:59:31 -08001840 int rc = 0;
1841
1842 mutex_lock(&fcoe_config_mutex);
Vasu Dev55a66d32009-12-10 09:59:31 -08001843
Robert Loveee5df622011-04-01 16:05:59 -07001844 rtnl_lock();
Vasu Dev55a66d32009-12-10 09:59:31 -08001845 fcoe = fcoe_hostlist_lookup_port(netdev);
1846 rtnl_unlock();
1847
Chris Leech9ee50e42010-04-09 14:22:23 -07001848 if (fcoe) {
Chris Leech9ee50e42010-04-09 14:22:23 -07001849 fcoe_ctlr_link_down(&fcoe->ctlr);
Vasu Dev9d4cbc02010-07-20 15:19:26 -07001850 fcoe_clean_pending_queue(fcoe->ctlr.lp);
Chris Leech9ee50e42010-04-09 14:22:23 -07001851 } else
Vasu Dev55a66d32009-12-10 09:59:31 -08001852 rc = -ENODEV;
1853
Vasu Dev55a66d32009-12-10 09:59:31 -08001854 mutex_unlock(&fcoe_config_mutex);
1855 return rc;
1856}
1857
1858/**
1859 * fcoe_enable() - Enables a FCoE interface
Yi Zou78a58242011-01-28 16:05:16 -08001860 * @netdev : The net_device object the Ethernet interface to create on
Vasu Dev55a66d32009-12-10 09:59:31 -08001861 *
Yi Zou78a58242011-01-28 16:05:16 -08001862 * Called from fcoe transport.
Vasu Dev55a66d32009-12-10 09:59:31 -08001863 *
1864 * Returns: 0 for success
1865 */
Yi Zou78a58242011-01-28 16:05:16 -08001866static int fcoe_enable(struct net_device *netdev)
Vasu Dev55a66d32009-12-10 09:59:31 -08001867{
1868 struct fcoe_interface *fcoe;
Vasu Dev55a66d32009-12-10 09:59:31 -08001869 int rc = 0;
1870
1871 mutex_lock(&fcoe_config_mutex);
Robert Loveee5df622011-04-01 16:05:59 -07001872 rtnl_lock();
Vasu Dev55a66d32009-12-10 09:59:31 -08001873 fcoe = fcoe_hostlist_lookup_port(netdev);
1874 rtnl_unlock();
1875
Vasu Dev9d4cbc02010-07-20 15:19:26 -07001876 if (!fcoe)
Vasu Dev55a66d32009-12-10 09:59:31 -08001877 rc = -ENODEV;
Vasu Dev9d4cbc02010-07-20 15:19:26 -07001878 else if (!fcoe_link_ok(fcoe->ctlr.lp))
1879 fcoe_ctlr_link_up(&fcoe->ctlr);
Vasu Dev55a66d32009-12-10 09:59:31 -08001880
Vasu Dev55a66d32009-12-10 09:59:31 -08001881 mutex_unlock(&fcoe_config_mutex);
1882 return rc;
1883}
1884
1885/**
Robert Love1875f272009-11-03 11:47:50 -08001886 * fcoe_destroy() - Destroy a FCoE interface
Yi Zou78a58242011-01-28 16:05:16 -08001887 * @netdev : The net_device object the Ethernet interface to create on
Robert Love1875f272009-11-03 11:47:50 -08001888 *
Yi Zou78a58242011-01-28 16:05:16 -08001889 * Called from fcoe transport
Robert Love85b4aa42008-12-09 15:10:24 -08001890 *
1891 * Returns: 0 for success
Robert Love34f42a02009-02-27 10:55:45 -08001892 */
Yi Zou78a58242011-01-28 16:05:16 -08001893static int fcoe_destroy(struct net_device *netdev)
Robert Love85b4aa42008-12-09 15:10:24 -08001894{
Chris Leech030f4e02009-08-25 14:00:02 -07001895 struct fcoe_interface *fcoe;
Vasu Devf04ca1b2011-04-01 16:06:45 -07001896 struct fc_lport *lport;
Neerav Parikhb2085a42011-06-20 16:59:51 -07001897 struct fcoe_port *port;
Mike Christie8eca3552009-10-21 16:27:44 -07001898 int rc = 0;
Robert Love85b4aa42008-12-09 15:10:24 -08001899
Chris Leechdfc1d0f2009-08-25 14:00:13 -07001900 mutex_lock(&fcoe_config_mutex);
Robert Loveee5df622011-04-01 16:05:59 -07001901 rtnl_lock();
Chris Leech2e70e242009-08-25 14:00:23 -07001902 fcoe = fcoe_hostlist_lookup_port(netdev);
1903 if (!fcoe) {
Robert Love85b4aa42008-12-09 15:10:24 -08001904 rc = -ENODEV;
Yi Zou78a58242011-01-28 16:05:16 -08001905 goto out_nodev;
Robert Love85b4aa42008-12-09 15:10:24 -08001906 }
Vasu Devf04ca1b2011-04-01 16:06:45 -07001907 lport = fcoe->ctlr.lp;
Neerav Parikhb2085a42011-06-20 16:59:51 -07001908 port = lport_priv(lport);
Yi Zou54a5b212010-07-20 15:21:17 -07001909 list_del(&fcoe->list);
Neerav Parikhb2085a42011-06-20 16:59:51 -07001910 queue_work(fcoe_wq, &port->destroy_work);
Robert Love85b4aa42008-12-09 15:10:24 -08001911out_nodev:
Neerav Parikhb2085a42011-06-20 16:59:51 -07001912 rtnl_unlock();
Chris Leechdfc1d0f2009-08-25 14:00:13 -07001913 mutex_unlock(&fcoe_config_mutex);
Robert Love85b4aa42008-12-09 15:10:24 -08001914 return rc;
1915}
1916
Robert Love1875f272009-11-03 11:47:50 -08001917/**
1918 * fcoe_destroy_work() - Destroy a FCoE port in a deferred work context
1919 * @work: Handle to the FCoE port to be destroyed
1920 */
Chris Leech2e70e242009-08-25 14:00:23 -07001921static void fcoe_destroy_work(struct work_struct *work)
1922{
1923 struct fcoe_port *port;
Neerav Parikhb2085a42011-06-20 16:59:51 -07001924 struct fcoe_interface *fcoe;
1925 int npiv = 0;
Chris Leech2e70e242009-08-25 14:00:23 -07001926
1927 port = container_of(work, struct fcoe_port, destroy_work);
1928 mutex_lock(&fcoe_config_mutex);
Neerav Parikhb2085a42011-06-20 16:59:51 -07001929
1930 /* set if this is an NPIV port */
1931 npiv = port->lport->vport ? 1 : 0;
1932
1933 fcoe = port->priv;
Chris Leech2e70e242009-08-25 14:00:23 -07001934 fcoe_if_destroy(port->lport);
Neerav Parikhb2085a42011-06-20 16:59:51 -07001935
1936 /* Do not tear down the fcoe interface for NPIV port */
1937 if (!npiv) {
1938 rtnl_lock();
1939 fcoe_interface_cleanup(fcoe);
1940 rtnl_unlock();
1941 }
1942
Chris Leech2e70e242009-08-25 14:00:23 -07001943 mutex_unlock(&fcoe_config_mutex);
1944}
1945
Robert Love85b4aa42008-12-09 15:10:24 -08001946/**
Yi Zou78a58242011-01-28 16:05:16 -08001947 * fcoe_match() - Check if the FCoE is supported on the given netdevice
1948 * @netdev : The net_device object the Ethernet interface to create on
Robert Love1875f272009-11-03 11:47:50 -08001949 *
Yi Zou78a58242011-01-28 16:05:16 -08001950 * Called from fcoe transport.
1951 *
1952 * Returns: always returns true as this is the default FCoE transport,
1953 * i.e., support all netdevs.
1954 */
1955static bool fcoe_match(struct net_device *netdev)
1956{
1957 return true;
1958}
1959
1960/**
1961 * fcoe_create() - Create a fcoe interface
1962 * @netdev : The net_device object the Ethernet interface to create on
1963 * @fip_mode: The FIP mode for this creation
1964 *
1965 * Called from fcoe transport
Robert Love85b4aa42008-12-09 15:10:24 -08001966 *
1967 * Returns: 0 for success
Robert Love34f42a02009-02-27 10:55:45 -08001968 */
Yi Zou78a58242011-01-28 16:05:16 -08001969static int fcoe_create(struct net_device *netdev, enum fip_state fip_mode)
Robert Love85b4aa42008-12-09 15:10:24 -08001970{
Neerav Parikhb2085a42011-06-20 16:59:51 -07001971 int rc = 0;
Chris Leech030f4e02009-08-25 14:00:02 -07001972 struct fcoe_interface *fcoe;
Chris Leechaf7f85d2009-08-25 13:59:24 -07001973 struct fc_lport *lport;
Robert Love85b4aa42008-12-09 15:10:24 -08001974
Chris Leechdfc1d0f2009-08-25 14:00:13 -07001975 mutex_lock(&fcoe_config_mutex);
Robert Loveee5df622011-04-01 16:05:59 -07001976 rtnl_lock();
Vasu Dev34ce27b2010-05-07 15:18:46 -07001977
Robert Love85b4aa42008-12-09 15:10:24 -08001978 /* look for existing lport */
1979 if (fcoe_hostlist_lookup(netdev)) {
1980 rc = -EEXIST;
Yi Zou78a58242011-01-28 16:05:16 -08001981 goto out_nodev;
Robert Love85b4aa42008-12-09 15:10:24 -08001982 }
Robert Love85b4aa42008-12-09 15:10:24 -08001983
Joe Eykholt1dd454d2010-07-20 15:20:46 -07001984 fcoe = fcoe_interface_create(netdev, fip_mode);
Robert Love7287fb92011-01-28 16:03:47 -08001985 if (IS_ERR(fcoe)) {
1986 rc = PTR_ERR(fcoe);
Yi Zou78a58242011-01-28 16:05:16 -08001987 goto out_nodev;
Chris Leech030f4e02009-08-25 14:00:02 -07001988 }
1989
Chris Leech9a057532009-11-03 11:46:40 -08001990 lport = fcoe_if_create(fcoe, &netdev->dev, 0);
Chris Leechaf7f85d2009-08-25 13:59:24 -07001991 if (IS_ERR(lport)) {
Robert Loved5488eb2009-06-10 15:30:59 -07001992 printk(KERN_ERR "fcoe: Failed to create interface (%s)\n",
Robert Love85b4aa42008-12-09 15:10:24 -08001993 netdev->name);
Robert Love85b4aa42008-12-09 15:10:24 -08001994 rc = -EIO;
Chris Leech2e70e242009-08-25 14:00:23 -07001995 fcoe_interface_cleanup(fcoe);
Neerav Parikhb2085a42011-06-20 16:59:51 -07001996 goto out_nodev;
Robert Love85b4aa42008-12-09 15:10:24 -08001997 }
Chris Leech030f4e02009-08-25 14:00:02 -07001998
Chris Leech54b649f2009-08-25 14:00:07 -07001999 /* Make this the "master" N_Port */
2000 fcoe->ctlr.lp = lport;
Chris Leech030f4e02009-08-25 14:00:02 -07002001
Chris Leechc863df32009-08-25 14:00:18 -07002002 /* add to lports list */
2003 fcoe_hostlist_add(lport);
2004
Chris Leech54b649f2009-08-25 14:00:07 -07002005 /* start FIP Discovery and FLOGI */
2006 lport->boot_time = jiffies;
2007 fc_fabric_login(lport);
2008 if (!fcoe_link_ok(lport))
2009 fcoe_ctlr_link_up(&fcoe->ctlr);
2010
Robert Love85b4aa42008-12-09 15:10:24 -08002011out_nodev:
Vasu Dev34ce27b2010-05-07 15:18:46 -07002012 rtnl_unlock();
Chris Leechdfc1d0f2009-08-25 14:00:13 -07002013 mutex_unlock(&fcoe_config_mutex);
Robert Love85b4aa42008-12-09 15:10:24 -08002014 return rc;
2015}
2016
Robert Love34f42a02009-02-27 10:55:45 -08002017/**
Robert Love5e4f8fe2010-05-07 15:18:35 -07002018 * fcoe_link_speed_update() - Update the supported and actual link speeds
2019 * @lport: The local port to update speeds for
Robert Love85b4aa42008-12-09 15:10:24 -08002020 *
Robert Love5e4f8fe2010-05-07 15:18:35 -07002021 * Returns: 0 if the ethtool query was successful
2022 * -1 if the ethtool query failed
Robert Love85b4aa42008-12-09 15:10:24 -08002023 */
Robert Love5e4f8fe2010-05-07 15:18:35 -07002024int fcoe_link_speed_update(struct fc_lport *lport)
Robert Love85b4aa42008-12-09 15:10:24 -08002025{
Bhanu Prakash Gollapudi8597ae82011-01-28 16:05:37 -08002026 struct net_device *netdev = fcoe_netdev(lport);
David Decotigny8ae6dac2011-04-27 18:32:38 +00002027 struct ethtool_cmd ecmd;
Robert Love85b4aa42008-12-09 15:10:24 -08002028
Robert Love5e4f8fe2010-05-07 15:18:35 -07002029 if (!dev_ethtool_get_settings(netdev, &ecmd)) {
Robert Love1875f272009-11-03 11:47:50 -08002030 lport->link_supported_speeds &=
Yi Zou2f718d62009-07-29 17:04:01 -07002031 ~(FC_PORTSPEED_1GBIT | FC_PORTSPEED_10GBIT);
2032 if (ecmd.supported & (SUPPORTED_1000baseT_Half |
2033 SUPPORTED_1000baseT_Full))
Robert Love1875f272009-11-03 11:47:50 -08002034 lport->link_supported_speeds |= FC_PORTSPEED_1GBIT;
Yi Zou2f718d62009-07-29 17:04:01 -07002035 if (ecmd.supported & SUPPORTED_10000baseT_Full)
Robert Love1875f272009-11-03 11:47:50 -08002036 lport->link_supported_speeds |=
Yi Zou2f718d62009-07-29 17:04:01 -07002037 FC_PORTSPEED_10GBIT;
David Decotigny8ae6dac2011-04-27 18:32:38 +00002038 switch (ethtool_cmd_speed(&ecmd)) {
2039 case SPEED_1000:
Robert Love1875f272009-11-03 11:47:50 -08002040 lport->link_speed = FC_PORTSPEED_1GBIT;
David Decotigny8ae6dac2011-04-27 18:32:38 +00002041 break;
2042 case SPEED_10000:
Robert Love1875f272009-11-03 11:47:50 -08002043 lport->link_speed = FC_PORTSPEED_10GBIT;
David Decotigny8ae6dac2011-04-27 18:32:38 +00002044 break;
2045 }
Yi Zou2f718d62009-07-29 17:04:01 -07002046 return 0;
2047 }
2048 return -1;
Robert Love85b4aa42008-12-09 15:10:24 -08002049}
Robert Love85b4aa42008-12-09 15:10:24 -08002050
Robert Love34f42a02009-02-27 10:55:45 -08002051/**
Robert Love5e4f8fe2010-05-07 15:18:35 -07002052 * fcoe_link_ok() - Check if the link is OK for a local port
2053 * @lport: The local port to check link on
2054 *
2055 * Returns: 0 if link is UP and OK, -1 if not
2056 *
2057 */
2058int fcoe_link_ok(struct fc_lport *lport)
2059{
Bhanu Prakash Gollapudi8597ae82011-01-28 16:05:37 -08002060 struct net_device *netdev = fcoe_netdev(lport);
Robert Love5e4f8fe2010-05-07 15:18:35 -07002061
2062 if (netif_oper_up(netdev))
2063 return 0;
2064 return -1;
2065}
2066
2067/**
Robert Love1875f272009-11-03 11:47:50 -08002068 * fcoe_percpu_clean() - Clear all pending skbs for an local port
2069 * @lport: The local port whose skbs are to be cleared
Joe Eykholte7a51992009-08-25 14:04:08 -07002070 *
2071 * Must be called with fcoe_create_mutex held to single-thread completion.
2072 *
2073 * This flushes the pending skbs by adding a new skb to each queue and
2074 * waiting until they are all freed. This assures us that not only are
2075 * there no packets that will be handled by the lport, but also that any
2076 * threads already handling packet have returned.
Robert Love85b4aa42008-12-09 15:10:24 -08002077 */
Robert Love1875f272009-11-03 11:47:50 -08002078void fcoe_percpu_clean(struct fc_lport *lport)
Robert Love85b4aa42008-12-09 15:10:24 -08002079{
Robert Love85b4aa42008-12-09 15:10:24 -08002080 struct fcoe_percpu_s *pp;
2081 struct fcoe_rcv_info *fr;
2082 struct sk_buff_head *list;
2083 struct sk_buff *skb, *next;
2084 struct sk_buff *head;
Robert Love5e5e92d2009-03-17 11:41:35 -07002085 unsigned int cpu;
Robert Love85b4aa42008-12-09 15:10:24 -08002086
Robert Love5e5e92d2009-03-17 11:41:35 -07002087 for_each_possible_cpu(cpu) {
2088 pp = &per_cpu(fcoe_percpu, cpu);
2089 spin_lock_bh(&pp->fcoe_rx_list.lock);
2090 list = &pp->fcoe_rx_list;
2091 head = list->next;
2092 for (skb = head; skb != (struct sk_buff *)list;
2093 skb = next) {
2094 next = skb->next;
2095 fr = fcoe_dev_from_skb(skb);
Robert Love1875f272009-11-03 11:47:50 -08002096 if (fr->fr_dev == lport) {
Robert Love5e5e92d2009-03-17 11:41:35 -07002097 __skb_unlink(skb, list);
2098 kfree_skb(skb);
Robert Love85b4aa42008-12-09 15:10:24 -08002099 }
Robert Love85b4aa42008-12-09 15:10:24 -08002100 }
Joe Eykholte7a51992009-08-25 14:04:08 -07002101
2102 if (!pp->thread || !cpu_online(cpu)) {
2103 spin_unlock_bh(&pp->fcoe_rx_list.lock);
2104 continue;
2105 }
2106
2107 skb = dev_alloc_skb(0);
2108 if (!skb) {
2109 spin_unlock_bh(&pp->fcoe_rx_list.lock);
2110 continue;
2111 }
2112 skb->destructor = fcoe_percpu_flush_done;
2113
2114 __skb_queue_tail(&pp->fcoe_rx_list, skb);
2115 if (pp->fcoe_rx_list.qlen == 1)
2116 wake_up_process(pp->thread);
Robert Love5e5e92d2009-03-17 11:41:35 -07002117 spin_unlock_bh(&pp->fcoe_rx_list.lock);
Joe Eykholte7a51992009-08-25 14:04:08 -07002118
2119 wait_for_completion(&fcoe_flush_completion);
Robert Love85b4aa42008-12-09 15:10:24 -08002120 }
2121}
Robert Love85b4aa42008-12-09 15:10:24 -08002122
2123/**
Robert Love1875f272009-11-03 11:47:50 -08002124 * fcoe_reset() - Reset a local port
2125 * @shost: The SCSI host associated with the local port to be reset
Robert Love85b4aa42008-12-09 15:10:24 -08002126 *
Robert Love1875f272009-11-03 11:47:50 -08002127 * Returns: Always 0 (return value required by FC transport template)
Robert Love85b4aa42008-12-09 15:10:24 -08002128 */
2129int fcoe_reset(struct Scsi_Host *shost)
2130{
2131 struct fc_lport *lport = shost_priv(shost);
Vasu Devd2f80952011-02-25 15:03:28 -08002132 struct fcoe_port *port = lport_priv(lport);
2133 struct fcoe_interface *fcoe = port->priv;
2134
2135 fcoe_ctlr_link_down(&fcoe->ctlr);
2136 fcoe_clean_pending_queue(fcoe->ctlr.lp);
2137 if (!fcoe_link_ok(fcoe->ctlr.lp))
2138 fcoe_ctlr_link_up(&fcoe->ctlr);
Robert Love85b4aa42008-12-09 15:10:24 -08002139 return 0;
2140}
Robert Love85b4aa42008-12-09 15:10:24 -08002141
Robert Love34f42a02009-02-27 10:55:45 -08002142/**
Robert Love1875f272009-11-03 11:47:50 -08002143 * fcoe_hostlist_lookup_port() - Find the FCoE interface associated with a net device
2144 * @netdev: The net device used as a key
Robert Love85b4aa42008-12-09 15:10:24 -08002145 *
Robert Love1875f272009-11-03 11:47:50 -08002146 * Locking: Must be called with the RNL mutex held.
2147 *
2148 * Returns: NULL or the FCoE interface
Robert Love85b4aa42008-12-09 15:10:24 -08002149 */
Chris Leech014f5c32009-08-25 13:59:30 -07002150static struct fcoe_interface *
Robert Love1875f272009-11-03 11:47:50 -08002151fcoe_hostlist_lookup_port(const struct net_device *netdev)
Robert Love85b4aa42008-12-09 15:10:24 -08002152{
Chris Leech014f5c32009-08-25 13:59:30 -07002153 struct fcoe_interface *fcoe;
Robert Love85b4aa42008-12-09 15:10:24 -08002154
Chris Leech014f5c32009-08-25 13:59:30 -07002155 list_for_each_entry(fcoe, &fcoe_hostlist, list) {
Robert Love1875f272009-11-03 11:47:50 -08002156 if (fcoe->netdev == netdev)
Chris Leech014f5c32009-08-25 13:59:30 -07002157 return fcoe;
Robert Love85b4aa42008-12-09 15:10:24 -08002158 }
Robert Love85b4aa42008-12-09 15:10:24 -08002159 return NULL;
2160}
2161
Robert Love34f42a02009-02-27 10:55:45 -08002162/**
Robert Love1875f272009-11-03 11:47:50 -08002163 * fcoe_hostlist_lookup() - Find the local port associated with a
2164 * given net device
2165 * @netdev: The netdevice used as a key
Robert Love85b4aa42008-12-09 15:10:24 -08002166 *
Robert Love1875f272009-11-03 11:47:50 -08002167 * Locking: Must be called with the RTNL mutex held
2168 *
2169 * Returns: NULL or the local port
Robert Love85b4aa42008-12-09 15:10:24 -08002170 */
Chris Leech090eb6c2009-08-25 14:00:28 -07002171static struct fc_lport *fcoe_hostlist_lookup(const struct net_device *netdev)
Robert Love85b4aa42008-12-09 15:10:24 -08002172{
Chris Leech014f5c32009-08-25 13:59:30 -07002173 struct fcoe_interface *fcoe;
Robert Love85b4aa42008-12-09 15:10:24 -08002174
Chris Leech014f5c32009-08-25 13:59:30 -07002175 fcoe = fcoe_hostlist_lookup_port(netdev);
Chris Leech3fe9a0b2009-08-25 13:59:46 -07002176 return (fcoe) ? fcoe->ctlr.lp : NULL;
Robert Love85b4aa42008-12-09 15:10:24 -08002177}
Robert Love85b4aa42008-12-09 15:10:24 -08002178
Robert Love34f42a02009-02-27 10:55:45 -08002179/**
Robert Love1875f272009-11-03 11:47:50 -08002180 * fcoe_hostlist_add() - Add the FCoE interface identified by a local
2181 * port to the hostlist
2182 * @lport: The local port that identifies the FCoE interface to be added
2183 *
2184 * Locking: must be called with the RTNL mutex held
Robert Love85b4aa42008-12-09 15:10:24 -08002185 *
2186 * Returns: 0 for success
2187 */
Chris Leech090eb6c2009-08-25 14:00:28 -07002188static int fcoe_hostlist_add(const struct fc_lport *lport)
Robert Love85b4aa42008-12-09 15:10:24 -08002189{
Chris Leech014f5c32009-08-25 13:59:30 -07002190 struct fcoe_interface *fcoe;
2191 struct fcoe_port *port;
Robert Love85b4aa42008-12-09 15:10:24 -08002192
Chris Leech014f5c32009-08-25 13:59:30 -07002193 fcoe = fcoe_hostlist_lookup_port(fcoe_netdev(lport));
2194 if (!fcoe) {
2195 port = lport_priv(lport);
Bhanu Prakash Gollapudi8597ae82011-01-28 16:05:37 -08002196 fcoe = port->priv;
Chris Leech014f5c32009-08-25 13:59:30 -07002197 list_add_tail(&fcoe->list, &fcoe_hostlist);
Robert Love85b4aa42008-12-09 15:10:24 -08002198 }
2199 return 0;
2200}
Robert Love85b4aa42008-12-09 15:10:24 -08002201
Yi Zou78a58242011-01-28 16:05:16 -08002202
2203static struct fcoe_transport fcoe_sw_transport = {
2204 .name = {FCOE_TRANSPORT_DEFAULT},
2205 .attached = false,
2206 .list = LIST_HEAD_INIT(fcoe_sw_transport.list),
2207 .match = fcoe_match,
2208 .create = fcoe_create,
2209 .destroy = fcoe_destroy,
2210 .enable = fcoe_enable,
2211 .disable = fcoe_disable,
2212};
2213
Robert Love34f42a02009-02-27 10:55:45 -08002214/**
Robert Love1875f272009-11-03 11:47:50 -08002215 * fcoe_init() - Initialize fcoe.ko
Robert Love85b4aa42008-12-09 15:10:24 -08002216 *
Robert Love1875f272009-11-03 11:47:50 -08002217 * Returns: 0 on success, or a negative value on failure
Robert Love34f42a02009-02-27 10:55:45 -08002218 */
Robert Love85b4aa42008-12-09 15:10:24 -08002219static int __init fcoe_init(void)
2220{
Robert Love1875f272009-11-03 11:47:50 -08002221 struct fcoe_percpu_s *p;
Robert Love38eccab2009-03-17 11:41:30 -07002222 unsigned int cpu;
Robert Love8976f422009-03-17 11:41:46 -07002223 int rc = 0;
Robert Love85b4aa42008-12-09 15:10:24 -08002224
Tejun Heo2ca32b42011-01-28 16:05:32 -08002225 fcoe_wq = alloc_workqueue("fcoe", 0, 0);
2226 if (!fcoe_wq)
2227 return -ENOMEM;
2228
Yi Zou78a58242011-01-28 16:05:16 -08002229 /* register as a fcoe transport */
2230 rc = fcoe_transport_attach(&fcoe_sw_transport);
2231 if (rc) {
2232 printk(KERN_ERR "failed to register an fcoe transport, check "
2233 "if libfcoe is loaded\n");
2234 return rc;
2235 }
2236
Chris Leechdfc1d0f2009-08-25 14:00:13 -07002237 mutex_lock(&fcoe_config_mutex);
2238
Robert Love38eccab2009-03-17 11:41:30 -07002239 for_each_possible_cpu(cpu) {
Robert Love5e5e92d2009-03-17 11:41:35 -07002240 p = &per_cpu(fcoe_percpu, cpu);
Robert Love38eccab2009-03-17 11:41:30 -07002241 skb_queue_head_init(&p->fcoe_rx_list);
2242 }
2243
Robert Love8976f422009-03-17 11:41:46 -07002244 for_each_online_cpu(cpu)
2245 fcoe_percpu_thread_create(cpu);
Robert Love85b4aa42008-12-09 15:10:24 -08002246
Robert Love8976f422009-03-17 11:41:46 -07002247 /* Initialize per CPU interrupt thread */
2248 rc = register_hotcpu_notifier(&fcoe_cpu_notifier);
2249 if (rc)
2250 goto out_free;
Robert Love85b4aa42008-12-09 15:10:24 -08002251
Robert Love8976f422009-03-17 11:41:46 -07002252 /* Setup link change notification */
Robert Love85b4aa42008-12-09 15:10:24 -08002253 fcoe_dev_setup();
2254
Chris Leech5892c322009-08-25 13:59:14 -07002255 rc = fcoe_if_init();
2256 if (rc)
2257 goto out_free;
Robert Love85b4aa42008-12-09 15:10:24 -08002258
Chris Leechdfc1d0f2009-08-25 14:00:13 -07002259 mutex_unlock(&fcoe_config_mutex);
Robert Love85b4aa42008-12-09 15:10:24 -08002260 return 0;
Robert Love8976f422009-03-17 11:41:46 -07002261
2262out_free:
2263 for_each_online_cpu(cpu) {
2264 fcoe_percpu_thread_destroy(cpu);
2265 }
Chris Leechdfc1d0f2009-08-25 14:00:13 -07002266 mutex_unlock(&fcoe_config_mutex);
Tejun Heo2ca32b42011-01-28 16:05:32 -08002267 destroy_workqueue(fcoe_wq);
Robert Love8976f422009-03-17 11:41:46 -07002268 return rc;
Robert Love85b4aa42008-12-09 15:10:24 -08002269}
2270module_init(fcoe_init);
2271
2272/**
Robert Love1875f272009-11-03 11:47:50 -08002273 * fcoe_exit() - Clean up fcoe.ko
Robert Love85b4aa42008-12-09 15:10:24 -08002274 *
Robert Love1875f272009-11-03 11:47:50 -08002275 * Returns: 0 on success or a negative value on failure
Robert Love34f42a02009-02-27 10:55:45 -08002276 */
Robert Love85b4aa42008-12-09 15:10:24 -08002277static void __exit fcoe_exit(void)
2278{
Chris Leech014f5c32009-08-25 13:59:30 -07002279 struct fcoe_interface *fcoe, *tmp;
Chris Leech2e70e242009-08-25 14:00:23 -07002280 struct fcoe_port *port;
Robert Love1875f272009-11-03 11:47:50 -08002281 unsigned int cpu;
Robert Love85b4aa42008-12-09 15:10:24 -08002282
Chris Leechdfc1d0f2009-08-25 14:00:13 -07002283 mutex_lock(&fcoe_config_mutex);
2284
Robert Love85b4aa42008-12-09 15:10:24 -08002285 fcoe_dev_cleanup();
2286
Vasu Dev5919a592009-03-27 09:03:29 -07002287 /* releases the associated fcoe hosts */
Chris Leech090eb6c2009-08-25 14:00:28 -07002288 rtnl_lock();
2289 list_for_each_entry_safe(fcoe, tmp, &fcoe_hostlist, list) {
Chris Leechc863df32009-08-25 14:00:18 -07002290 list_del(&fcoe->list);
Chris Leech2e70e242009-08-25 14:00:23 -07002291 port = lport_priv(fcoe->ctlr.lp);
Tejun Heo2ca32b42011-01-28 16:05:32 -08002292 queue_work(fcoe_wq, &port->destroy_work);
Chris Leechc863df32009-08-25 14:00:18 -07002293 }
Chris Leech090eb6c2009-08-25 14:00:28 -07002294 rtnl_unlock();
Robert Love85b4aa42008-12-09 15:10:24 -08002295
Robert Love8976f422009-03-17 11:41:46 -07002296 unregister_hotcpu_notifier(&fcoe_cpu_notifier);
2297
Chris Leech014f5c32009-08-25 13:59:30 -07002298 for_each_online_cpu(cpu)
Robert Love8976f422009-03-17 11:41:46 -07002299 fcoe_percpu_thread_destroy(cpu);
Robert Love85b4aa42008-12-09 15:10:24 -08002300
Chris Leechdfc1d0f2009-08-25 14:00:13 -07002301 mutex_unlock(&fcoe_config_mutex);
Chris Leech2e70e242009-08-25 14:00:23 -07002302
Tejun Heo2ca32b42011-01-28 16:05:32 -08002303 /*
2304 * destroy_work's may be chained but destroy_workqueue()
2305 * can take care of them. Just kill the fcoe_wq.
2306 */
2307 destroy_workqueue(fcoe_wq);
Chris Leech2e70e242009-08-25 14:00:23 -07002308
Tejun Heo2ca32b42011-01-28 16:05:32 -08002309 /*
2310 * Detaching from the scsi transport must happen after all
2311 * destroys are done on the fcoe_wq. destroy_workqueue will
2312 * enusre the fcoe_wq is flushed.
2313 */
Chris Leech2e70e242009-08-25 14:00:23 -07002314 fcoe_if_exit();
Yi Zou78a58242011-01-28 16:05:16 -08002315
2316 /* detach from fcoe transport */
2317 fcoe_transport_detach(&fcoe_sw_transport);
Robert Love85b4aa42008-12-09 15:10:24 -08002318}
2319module_exit(fcoe_exit);
Chris Leech11b56182009-11-03 11:46:29 -08002320
2321/**
2322 * fcoe_flogi_resp() - FCoE specific FLOGI and FDISC response handler
2323 * @seq: active sequence in the FLOGI or FDISC exchange
2324 * @fp: response frame, or error encoded in a pointer (timeout)
2325 * @arg: pointer the the fcoe_ctlr structure
2326 *
Uwe Kleine-König65155b32010-06-11 12:17:01 +02002327 * This handles MAC address management for FCoE, then passes control on to
Chris Leech11b56182009-11-03 11:46:29 -08002328 * the libfc FLOGI response handler.
2329 */
2330static void fcoe_flogi_resp(struct fc_seq *seq, struct fc_frame *fp, void *arg)
2331{
2332 struct fcoe_ctlr *fip = arg;
2333 struct fc_exch *exch = fc_seq_exch(seq);
2334 struct fc_lport *lport = exch->lp;
2335 u8 *mac;
2336
2337 if (IS_ERR(fp))
2338 goto done;
2339
2340 mac = fr_cb(fp)->granted_mac;
2341 if (is_zero_ether_addr(mac)) {
2342 /* pre-FIP */
Joe Eykholt386309c2009-11-03 11:49:16 -08002343 if (fcoe_ctlr_recv_flogi(fip, lport, fp)) {
Chris Leech11b56182009-11-03 11:46:29 -08002344 fc_frame_free(fp);
2345 return;
2346 }
Chris Leech11b56182009-11-03 11:46:29 -08002347 }
Joe Eykholt386309c2009-11-03 11:49:16 -08002348 fcoe_update_src_mac(lport, mac);
Chris Leech11b56182009-11-03 11:46:29 -08002349done:
2350 fc_lport_flogi_resp(seq, fp, lport);
2351}
2352
2353/**
2354 * fcoe_logo_resp() - FCoE specific LOGO response handler
2355 * @seq: active sequence in the LOGO exchange
2356 * @fp: response frame, or error encoded in a pointer (timeout)
2357 * @arg: pointer the the fcoe_ctlr structure
2358 *
Uwe Kleine-König65155b32010-06-11 12:17:01 +02002359 * This handles MAC address management for FCoE, then passes control on to
Chris Leech11b56182009-11-03 11:46:29 -08002360 * the libfc LOGO response handler.
2361 */
2362static void fcoe_logo_resp(struct fc_seq *seq, struct fc_frame *fp, void *arg)
2363{
Joe Eykholt386309c2009-11-03 11:49:16 -08002364 struct fc_lport *lport = arg;
Chris Leech11b56182009-11-03 11:46:29 -08002365 static u8 zero_mac[ETH_ALEN] = { 0 };
2366
2367 if (!IS_ERR(fp))
Joe Eykholt386309c2009-11-03 11:49:16 -08002368 fcoe_update_src_mac(lport, zero_mac);
Chris Leech11b56182009-11-03 11:46:29 -08002369 fc_lport_logo_resp(seq, fp, lport);
2370}
2371
2372/**
2373 * fcoe_elsct_send - FCoE specific ELS handler
2374 *
2375 * This does special case handling of FIP encapsualted ELS exchanges for FCoE,
2376 * using FCoE specific response handlers and passing the FIP controller as
2377 * the argument (the lport is still available from the exchange).
2378 *
2379 * Most of the work here is just handed off to the libfc routine.
2380 */
Robert Love1875f272009-11-03 11:47:50 -08002381static struct fc_seq *fcoe_elsct_send(struct fc_lport *lport, u32 did,
2382 struct fc_frame *fp, unsigned int op,
2383 void (*resp)(struct fc_seq *,
2384 struct fc_frame *,
2385 void *),
2386 void *arg, u32 timeout)
Chris Leech11b56182009-11-03 11:46:29 -08002387{
2388 struct fcoe_port *port = lport_priv(lport);
Bhanu Prakash Gollapudi8597ae82011-01-28 16:05:37 -08002389 struct fcoe_interface *fcoe = port->priv;
Chris Leech11b56182009-11-03 11:46:29 -08002390 struct fcoe_ctlr *fip = &fcoe->ctlr;
2391 struct fc_frame_header *fh = fc_frame_header_get(fp);
2392
2393 switch (op) {
2394 case ELS_FLOGI:
2395 case ELS_FDISC:
Joe Eykholte10f8c62010-07-20 15:20:30 -07002396 if (lport->point_to_multipoint)
2397 break;
Chris Leech11b56182009-11-03 11:46:29 -08002398 return fc_elsct_send(lport, did, fp, op, fcoe_flogi_resp,
2399 fip, timeout);
2400 case ELS_LOGO:
2401 /* only hook onto fabric logouts, not port logouts */
2402 if (ntoh24(fh->fh_d_id) != FC_FID_FLOGI)
2403 break;
2404 return fc_elsct_send(lport, did, fp, op, fcoe_logo_resp,
Joe Eykholt386309c2009-11-03 11:49:16 -08002405 lport, timeout);
Chris Leech11b56182009-11-03 11:46:29 -08002406 }
2407 return fc_elsct_send(lport, did, fp, op, resp, arg, timeout);
2408}
2409
Chris Leech9a057532009-11-03 11:46:40 -08002410/**
2411 * fcoe_vport_create() - create an fc_host/scsi_host for a vport
2412 * @vport: fc_vport object to create a new fc_host for
2413 * @disabled: start the new fc_host in a disabled state by default?
2414 *
2415 * Returns: 0 for success
2416 */
2417static int fcoe_vport_create(struct fc_vport *vport, bool disabled)
2418{
2419 struct Scsi_Host *shost = vport_to_shost(vport);
2420 struct fc_lport *n_port = shost_priv(shost);
2421 struct fcoe_port *port = lport_priv(n_port);
Bhanu Prakash Gollapudi8597ae82011-01-28 16:05:37 -08002422 struct fcoe_interface *fcoe = port->priv;
Chris Leech9a057532009-11-03 11:46:40 -08002423 struct net_device *netdev = fcoe->netdev;
2424 struct fc_lport *vn_port;
Neerav Parikhbdf25212011-05-16 16:45:29 -07002425 int rc;
2426 char buf[32];
2427
2428 rc = fcoe_validate_vport_create(vport);
2429 if (rc) {
Bhanu Prakash Gollapudid8348952011-08-04 17:38:49 -07002430 fcoe_wwn_to_str(vport->port_name, buf, sizeof(buf));
Neerav Parikhbdf25212011-05-16 16:45:29 -07002431 printk(KERN_ERR "fcoe: Failed to create vport, "
2432 "WWPN (0x%s) already exists\n",
2433 buf);
2434 return rc;
2435 }
Chris Leech9a057532009-11-03 11:46:40 -08002436
2437 mutex_lock(&fcoe_config_mutex);
2438 vn_port = fcoe_if_create(fcoe, &vport->dev, 1);
2439 mutex_unlock(&fcoe_config_mutex);
2440
2441 if (IS_ERR(vn_port)) {
2442 printk(KERN_ERR "fcoe: fcoe_vport_create(%s) failed\n",
2443 netdev->name);
2444 return -EIO;
2445 }
2446
2447 if (disabled) {
2448 fc_vport_set_state(vport, FC_VPORT_DISABLED);
2449 } else {
2450 vn_port->boot_time = jiffies;
2451 fc_fabric_login(vn_port);
2452 fc_vport_setlink(vn_port);
2453 }
2454 return 0;
2455}
2456
2457/**
2458 * fcoe_vport_destroy() - destroy the fc_host/scsi_host for a vport
2459 * @vport: fc_vport object that is being destroyed
2460 *
2461 * Returns: 0 for success
2462 */
2463static int fcoe_vport_destroy(struct fc_vport *vport)
2464{
2465 struct Scsi_Host *shost = vport_to_shost(vport);
2466 struct fc_lport *n_port = shost_priv(shost);
2467 struct fc_lport *vn_port = vport->dd_data;
2468 struct fcoe_port *port = lport_priv(vn_port);
2469
2470 mutex_lock(&n_port->lp_mutex);
2471 list_del(&vn_port->list);
2472 mutex_unlock(&n_port->lp_mutex);
Tejun Heo2ca32b42011-01-28 16:05:32 -08002473 queue_work(fcoe_wq, &port->destroy_work);
Chris Leech9a057532009-11-03 11:46:40 -08002474 return 0;
2475}
2476
2477/**
2478 * fcoe_vport_disable() - change vport state
2479 * @vport: vport to bring online/offline
2480 * @disable: should the vport be disabled?
2481 */
2482static int fcoe_vport_disable(struct fc_vport *vport, bool disable)
2483{
2484 struct fc_lport *lport = vport->dd_data;
2485
2486 if (disable) {
2487 fc_vport_set_state(vport, FC_VPORT_DISABLED);
2488 fc_fabric_logoff(lport);
2489 } else {
2490 lport->boot_time = jiffies;
2491 fc_fabric_login(lport);
2492 fc_vport_setlink(lport);
2493 }
2494
2495 return 0;
2496}
2497
Chris Leechdc8596d2009-11-03 11:47:18 -08002498/**
2499 * fcoe_vport_set_symbolic_name() - append vport string to symbolic name
2500 * @vport: fc_vport with a new symbolic name string
2501 *
2502 * After generating a new symbolic name string, a new RSPN_ID request is
2503 * sent to the name server. There is no response handler, so if it fails
2504 * for some reason it will not be retried.
2505 */
2506static void fcoe_set_vport_symbolic_name(struct fc_vport *vport)
2507{
2508 struct fc_lport *lport = vport->dd_data;
2509 struct fc_frame *fp;
2510 size_t len;
2511
2512 snprintf(fc_host_symbolic_name(lport->host), FC_SYMBOLIC_NAME_SIZE,
2513 "%s v%s over %s : %s", FCOE_NAME, FCOE_VERSION,
2514 fcoe_netdev(lport)->name, vport->symbolic_name);
2515
2516 if (lport->state != LPORT_ST_READY)
2517 return;
2518
2519 len = strnlen(fc_host_symbolic_name(lport->host), 255);
2520 fp = fc_frame_alloc(lport,
2521 sizeof(struct fc_ct_hdr) +
2522 sizeof(struct fc_ns_rspn) + len);
2523 if (!fp)
2524 return;
2525 lport->tt.elsct_send(lport, FC_FID_DIR_SERV, fp, FC_NS_RSPN_ID,
Joe Eykholtb94f8952009-11-03 11:50:21 -08002526 NULL, NULL, 3 * lport->r_a_tov);
Chris Leechdc8596d2009-11-03 11:47:18 -08002527}
Yi Zoub84056b2009-11-20 14:55:19 -08002528
2529/**
2530 * fcoe_get_lesb() - Fill the FCoE Link Error Status Block
2531 * @lport: the local port
2532 * @fc_lesb: the link error status block
2533 */
2534static void fcoe_get_lesb(struct fc_lport *lport,
2535 struct fc_els_lesb *fc_lesb)
2536{
2537 unsigned int cpu;
2538 u32 lfc, vlfc, mdac;
2539 struct fcoe_dev_stats *devst;
2540 struct fcoe_fc_els_lesb *lesb;
Eric Dumazet28172732010-07-07 14:58:56 -07002541 struct rtnl_link_stats64 temp;
Yi Zoub84056b2009-11-20 14:55:19 -08002542 struct net_device *netdev = fcoe_netdev(lport);
2543
2544 lfc = 0;
2545 vlfc = 0;
2546 mdac = 0;
2547 lesb = (struct fcoe_fc_els_lesb *)fc_lesb;
2548 memset(lesb, 0, sizeof(*lesb));
2549 for_each_possible_cpu(cpu) {
2550 devst = per_cpu_ptr(lport->dev_stats, cpu);
2551 lfc += devst->LinkFailureCount;
2552 vlfc += devst->VLinkFailureCount;
2553 mdac += devst->MissDiscAdvCount;
2554 }
2555 lesb->lesb_link_fail = htonl(lfc);
2556 lesb->lesb_vlink_fail = htonl(vlfc);
2557 lesb->lesb_miss_fka = htonl(mdac);
Eric Dumazet28172732010-07-07 14:58:56 -07002558 lesb->lesb_fcs_error = htonl(dev_get_stats(netdev, &temp)->rx_crc_errors);
Yi Zoub84056b2009-11-20 14:55:19 -08002559}
Joe Eykholt7d65b0d2010-03-12 16:08:02 -08002560
2561/**
2562 * fcoe_set_port_id() - Callback from libfc when Port_ID is set.
2563 * @lport: the local port
2564 * @port_id: the port ID
2565 * @fp: the received frame, if any, that caused the port_id to be set.
2566 *
2567 * This routine handles the case where we received a FLOGI and are
2568 * entering point-to-point mode. We need to call fcoe_ctlr_recv_flogi()
2569 * so it can set the non-mapped mode and gateway address.
2570 *
2571 * The FLOGI LS_ACC is handled by fcoe_flogi_resp().
2572 */
2573static void fcoe_set_port_id(struct fc_lport *lport,
2574 u32 port_id, struct fc_frame *fp)
2575{
2576 struct fcoe_port *port = lport_priv(lport);
Bhanu Prakash Gollapudi8597ae82011-01-28 16:05:37 -08002577 struct fcoe_interface *fcoe = port->priv;
Joe Eykholt7d65b0d2010-03-12 16:08:02 -08002578
2579 if (fp && fc_frame_payload_op(fp) == ELS_FLOGI)
2580 fcoe_ctlr_recv_flogi(&fcoe->ctlr, lport, fp);
2581}