blob: 19aa154e727faabce75a76c362e213db656c029c [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 */
Vasu Dev860eca22011-09-27 21:38:18 -070055static unsigned int fcoe_ddp_min = 4096;
Yi Zou05cc7392009-08-25 13:59:03 -070056module_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;
Vasu Devd1483bb2011-09-27 21:38:13 -0700283 fcoe->realdev = real_dev;
Chris Leech54b649f2009-08-25 14:00:07 -0700284 rcu_read_lock();
Yi Zou5bab87e2009-11-03 11:49:43 -0800285 for_each_dev_addr(real_dev, ha) {
Chris Leech54b649f2009-08-25 14:00:07 -0700286 if ((ha->type == NETDEV_HW_ADDR_T_SAN) &&
Yi Zoubf361702009-11-03 11:49:38 -0800287 (is_valid_ether_addr(ha->addr))) {
Chris Leech54b649f2009-08-25 14:00:07 -0700288 memcpy(fip->ctl_src_addr, ha->addr, ETH_ALEN);
289 fip->spma = 1;
290 break;
291 }
292 }
293 rcu_read_unlock();
294
295 /* setup Source Mac Address */
296 if (!fip->spma)
297 memcpy(fip->ctl_src_addr, netdev->dev_addr, netdev->addr_len);
298
299 /*
300 * Add FCoE MAC address as second unicast MAC address
301 * or enter promiscuous mode if not capable of listening
302 * for multiple unicast MACs.
303 */
Chris Leech54b649f2009-08-25 14:00:07 -0700304 memcpy(flogi_maddr, (u8[6]) FC_FCOE_FLOGI_MAC, ETH_ALEN);
Jiri Pirkoa748ee22010-04-01 21:22:09 +0000305 dev_uc_add(netdev, flogi_maddr);
Chris Leech54b649f2009-08-25 14:00:07 -0700306 if (fip->spma)
Jiri Pirkoa748ee22010-04-01 21:22:09 +0000307 dev_uc_add(netdev, fip->ctl_src_addr);
Joe Eykholte10f8c62010-07-20 15:20:30 -0700308 if (fip->mode == FIP_MODE_VN2VN) {
309 dev_mc_add(netdev, FIP_ALL_VN2VN_MACS);
310 dev_mc_add(netdev, FIP_ALL_P2P_MACS);
311 } else
312 dev_mc_add(netdev, FIP_ALL_ENODE_MACS);
Chris Leech54b649f2009-08-25 14:00:07 -0700313
314 /*
315 * setup the receive function from ethernet driver
316 * on the ethertype for the given device
317 */
318 fcoe->fcoe_packet_type.func = fcoe_rcv;
319 fcoe->fcoe_packet_type.type = __constant_htons(ETH_P_FCOE);
320 fcoe->fcoe_packet_type.dev = netdev;
321 dev_add_pack(&fcoe->fcoe_packet_type);
322
323 fcoe->fip_packet_type.func = fcoe_fip_recv;
324 fcoe->fip_packet_type.type = htons(ETH_P_FIP);
325 fcoe->fip_packet_type.dev = netdev;
326 dev_add_pack(&fcoe->fip_packet_type);
327
328 return 0;
329}
330
Vasu Dev7f349142009-03-27 09:06:31 -0700331/**
Robert Love1875f272009-11-03 11:47:50 -0800332 * fcoe_interface_create() - Create a FCoE interface on a net device
333 * @netdev: The net device to create the FCoE interface on
Joe Eykholt1dd454d2010-07-20 15:20:46 -0700334 * @fip_mode: The mode to use for FIP
Chris Leech030f4e02009-08-25 14:00:02 -0700335 *
336 * Returns: pointer to a struct fcoe_interface or NULL on error
337 */
Joe Eykholt1dd454d2010-07-20 15:20:46 -0700338static struct fcoe_interface *fcoe_interface_create(struct net_device *netdev,
339 enum fip_state fip_mode)
Chris Leech030f4e02009-08-25 14:00:02 -0700340{
341 struct fcoe_interface *fcoe;
john fastabend59d92512009-11-03 11:48:44 -0800342 int err;
Chris Leech030f4e02009-08-25 14:00:02 -0700343
Robert Love7287fb92011-01-28 16:03:47 -0800344 if (!try_module_get(THIS_MODULE)) {
345 FCOE_NETDEV_DBG(netdev,
346 "Could not get a reference to the module\n");
347 fcoe = ERR_PTR(-EBUSY);
348 goto out;
349 }
350
Chris Leech030f4e02009-08-25 14:00:02 -0700351 fcoe = kzalloc(sizeof(*fcoe), GFP_KERNEL);
352 if (!fcoe) {
353 FCOE_NETDEV_DBG(netdev, "Could not allocate fcoe structure\n");
Robert Love7287fb92011-01-28 16:03:47 -0800354 fcoe = ERR_PTR(-ENOMEM);
355 goto out_nomod;
Chris Leech030f4e02009-08-25 14:00:02 -0700356 }
357
Chris Leech2e70e242009-08-25 14:00:23 -0700358 dev_hold(netdev);
Chris Leech030f4e02009-08-25 14:00:02 -0700359 kref_init(&fcoe->kref);
Chris Leech54b649f2009-08-25 14:00:07 -0700360
361 /*
362 * Initialize FIP.
363 */
Joe Eykholt1dd454d2010-07-20 15:20:46 -0700364 fcoe_ctlr_init(&fcoe->ctlr, fip_mode);
Chris Leech54b649f2009-08-25 14:00:07 -0700365 fcoe->ctlr.send = fcoe_fip_send;
366 fcoe->ctlr.update_mac = fcoe_update_src_mac;
Chris Leech11b56182009-11-03 11:46:29 -0800367 fcoe->ctlr.get_src_addr = fcoe_get_src_mac;
Chris Leech54b649f2009-08-25 14:00:07 -0700368
john fastabend59d92512009-11-03 11:48:44 -0800369 err = fcoe_interface_setup(fcoe, netdev);
370 if (err) {
371 fcoe_ctlr_destroy(&fcoe->ctlr);
372 kfree(fcoe);
373 dev_put(netdev);
Robert Love7287fb92011-01-28 16:03:47 -0800374 fcoe = ERR_PTR(err);
375 goto out_nomod;
john fastabend59d92512009-11-03 11:48:44 -0800376 }
Chris Leech030f4e02009-08-25 14:00:02 -0700377
Robert Love7287fb92011-01-28 16:03:47 -0800378 goto out;
379
380out_nomod:
381 module_put(THIS_MODULE);
382out:
Chris Leech030f4e02009-08-25 14:00:02 -0700383 return fcoe;
384}
385
386/**
387 * fcoe_interface_release() - fcoe_port kref release function
Robert Love1875f272009-11-03 11:47:50 -0800388 * @kref: Embedded reference count in an fcoe_interface struct
Chris Leech030f4e02009-08-25 14:00:02 -0700389 */
390static void fcoe_interface_release(struct kref *kref)
391{
392 struct fcoe_interface *fcoe;
Chris Leech2e70e242009-08-25 14:00:23 -0700393 struct net_device *netdev;
Chris Leech030f4e02009-08-25 14:00:02 -0700394
395 fcoe = container_of(kref, struct fcoe_interface, kref);
Chris Leech2e70e242009-08-25 14:00:23 -0700396 netdev = fcoe->netdev;
397 /* tear-down the FCoE controller */
398 fcoe_ctlr_destroy(&fcoe->ctlr);
Chris Leech030f4e02009-08-25 14:00:02 -0700399 kfree(fcoe);
Chris Leech2e70e242009-08-25 14:00:23 -0700400 dev_put(netdev);
Robert Love7287fb92011-01-28 16:03:47 -0800401 module_put(THIS_MODULE);
Chris Leech030f4e02009-08-25 14:00:02 -0700402}
403
404/**
Robert Love1875f272009-11-03 11:47:50 -0800405 * fcoe_interface_get() - Get a reference to a FCoE interface
406 * @fcoe: The FCoE interface to be held
Chris Leech030f4e02009-08-25 14:00:02 -0700407 */
408static inline void fcoe_interface_get(struct fcoe_interface *fcoe)
409{
410 kref_get(&fcoe->kref);
411}
412
413/**
Robert Love1875f272009-11-03 11:47:50 -0800414 * fcoe_interface_put() - Put a reference to a FCoE interface
415 * @fcoe: The FCoE interface to be released
Chris Leech030f4e02009-08-25 14:00:02 -0700416 */
417static inline void fcoe_interface_put(struct fcoe_interface *fcoe)
418{
419 kref_put(&fcoe->kref, fcoe_interface_release);
420}
421
422/**
Vasu Devf04ca1b2011-04-01 16:06:45 -0700423 * fcoe_interface_cleanup() - Clean up a FCoE interface
424 * @fcoe: The FCoE interface to be cleaned up
425 *
426 * Caller must be holding the RTNL mutex
427 */
428void fcoe_interface_cleanup(struct fcoe_interface *fcoe)
429{
430 struct net_device *netdev = fcoe->netdev;
431 struct fcoe_ctlr *fip = &fcoe->ctlr;
432 u8 flogi_maddr[ETH_ALEN];
433 const struct net_device_ops *ops;
Vasu Devf04ca1b2011-04-01 16:06:45 -0700434
435 /*
436 * Don't listen for Ethernet packets anymore.
437 * synchronize_net() ensures that the packet handlers are not running
438 * on another CPU. dev_remove_pack() would do that, this calls the
439 * unsyncronized version __dev_remove_pack() to avoid multiple delays.
440 */
441 __dev_remove_pack(&fcoe->fcoe_packet_type);
442 __dev_remove_pack(&fcoe->fip_packet_type);
443 synchronize_net();
444
445 /* Delete secondary MAC addresses */
446 memcpy(flogi_maddr, (u8[6]) FC_FCOE_FLOGI_MAC, ETH_ALEN);
447 dev_uc_del(netdev, flogi_maddr);
448 if (fip->spma)
449 dev_uc_del(netdev, fip->ctl_src_addr);
450 if (fip->mode == FIP_MODE_VN2VN) {
451 dev_mc_del(netdev, FIP_ALL_VN2VN_MACS);
452 dev_mc_del(netdev, FIP_ALL_P2P_MACS);
453 } else
454 dev_mc_del(netdev, FIP_ALL_ENODE_MACS);
455
Vasu Devf04ca1b2011-04-01 16:06:45 -0700456 /* Tell the LLD we are done w/ FCoE */
457 ops = netdev->netdev_ops;
458 if (ops->ndo_fcoe_disable) {
459 if (ops->ndo_fcoe_disable(netdev))
460 FCOE_NETDEV_DBG(netdev, "Failed to disable FCoE"
461 " specific feature for LLD.\n");
462 }
Neerav Parikhb2085a42011-06-20 16:59:51 -0700463
464 /* Release the self-reference taken during fcoe_interface_create() */
Vasu Devf04ca1b2011-04-01 16:06:45 -0700465 fcoe_interface_put(fcoe);
466}
467
468/**
Robert Love1875f272009-11-03 11:47:50 -0800469 * fcoe_fip_recv() - Handler for received FIP frames
470 * @skb: The receive skb
471 * @netdev: The associated net device
472 * @ptype: The packet_type structure which was used to register this handler
473 * @orig_dev: The original net_device the the skb was received on.
474 * (in case dev is a bond)
Vasu Devab6b85c2009-05-17 12:33:08 +0000475 *
476 * Returns: 0 for success
477 */
Robert Love1875f272009-11-03 11:47:50 -0800478static int fcoe_fip_recv(struct sk_buff *skb, struct net_device *netdev,
Vasu Devab6b85c2009-05-17 12:33:08 +0000479 struct packet_type *ptype,
480 struct net_device *orig_dev)
481{
Chris Leech259ad852009-08-25 13:59:41 -0700482 struct fcoe_interface *fcoe;
Vasu Devab6b85c2009-05-17 12:33:08 +0000483
Chris Leech259ad852009-08-25 13:59:41 -0700484 fcoe = container_of(ptype, struct fcoe_interface, fip_packet_type);
Chris Leech3fe9a0b2009-08-25 13:59:46 -0700485 fcoe_ctlr_recv(&fcoe->ctlr, skb);
Vasu Devab6b85c2009-05-17 12:33:08 +0000486 return 0;
487}
488
489/**
Vasu Dev980f5152011-07-27 15:11:05 -0700490 * fcoe_port_send() - Send an Ethernet-encapsulated FIP/FCoE frame
491 * @port: The FCoE port
492 * @skb: The FIP/FCoE packet to be sent
493 */
494static void fcoe_port_send(struct fcoe_port *port, struct sk_buff *skb)
495{
496 if (port->fcoe_pending_queue.qlen)
497 fcoe_check_wait_queue(port->lport, skb);
498 else if (fcoe_start_io(skb))
499 fcoe_check_wait_queue(port->lport, skb);
500}
501
502/**
Robert Love1875f272009-11-03 11:47:50 -0800503 * fcoe_fip_send() - Send an Ethernet-encapsulated FIP frame
504 * @fip: The FCoE controller
505 * @skb: The FIP packet to be sent
Vasu Devab6b85c2009-05-17 12:33:08 +0000506 */
507static void fcoe_fip_send(struct fcoe_ctlr *fip, struct sk_buff *skb)
508{
Chris Leech3fe9a0b2009-08-25 13:59:46 -0700509 skb->dev = fcoe_from_ctlr(fip)->netdev;
Vasu Dev980f5152011-07-27 15:11:05 -0700510 fcoe_port_send(lport_priv(fip->lp), skb);
Vasu Devab6b85c2009-05-17 12:33:08 +0000511}
512
513/**
Robert Love1875f272009-11-03 11:47:50 -0800514 * fcoe_update_src_mac() - Update the Ethernet MAC filters
515 * @lport: The local port to update the source MAC on
516 * @addr: Unicast MAC address to add
Vasu Devab6b85c2009-05-17 12:33:08 +0000517 *
518 * Remove any previously-set unicast MAC filter.
519 * Add secondary FCoE MAC address filter for our OUI.
520 */
Chris Leech11b56182009-11-03 11:46:29 -0800521static void fcoe_update_src_mac(struct fc_lport *lport, u8 *addr)
Vasu Devab6b85c2009-05-17 12:33:08 +0000522{
Chris Leech11b56182009-11-03 11:46:29 -0800523 struct fcoe_port *port = lport_priv(lport);
Bhanu Prakash Gollapudi8597ae82011-01-28 16:05:37 -0800524 struct fcoe_interface *fcoe = port->priv;
Vasu Devab6b85c2009-05-17 12:33:08 +0000525
Vasu Devab6b85c2009-05-17 12:33:08 +0000526 rtnl_lock();
Chris Leech11b56182009-11-03 11:46:29 -0800527 if (!is_zero_ether_addr(port->data_src_addr))
Jiri Pirkoa748ee22010-04-01 21:22:09 +0000528 dev_uc_del(fcoe->netdev, port->data_src_addr);
Chris Leech11b56182009-11-03 11:46:29 -0800529 if (!is_zero_ether_addr(addr))
Jiri Pirkoa748ee22010-04-01 21:22:09 +0000530 dev_uc_add(fcoe->netdev, addr);
Chris Leech11b56182009-11-03 11:46:29 -0800531 memcpy(port->data_src_addr, addr, ETH_ALEN);
Vasu Devab6b85c2009-05-17 12:33:08 +0000532 rtnl_unlock();
533}
534
535/**
Chris Leech11b56182009-11-03 11:46:29 -0800536 * fcoe_get_src_mac() - return the Ethernet source address for an lport
537 * @lport: libfc lport
538 */
539static u8 *fcoe_get_src_mac(struct fc_lport *lport)
540{
541 struct fcoe_port *port = lport_priv(lport);
542
543 return port->data_src_addr;
544}
545
546/**
Robert Love1875f272009-11-03 11:47:50 -0800547 * fcoe_lport_config() - Set up a local port
548 * @lport: The local port to be setup
Vasu Dev7f349142009-03-27 09:06:31 -0700549 *
550 * Returns: 0 for success
551 */
Robert Love1875f272009-11-03 11:47:50 -0800552static int fcoe_lport_config(struct fc_lport *lport)
Vasu Dev7f349142009-03-27 09:06:31 -0700553{
Robert Love1875f272009-11-03 11:47:50 -0800554 lport->link_up = 0;
555 lport->qfull = 0;
556 lport->max_retry_count = 3;
557 lport->max_rport_retry_count = 3;
558 lport->e_d_tov = 2 * 1000; /* FC-FS default */
559 lport->r_a_tov = 2 * 2 * 1000;
560 lport->service_params = (FCP_SPPF_INIT_FCN | FCP_SPPF_RD_XRDY_DIS |
561 FCP_SPPF_RETRY | FCP_SPPF_CONF_COMPL);
562 lport->does_npiv = 1;
Vasu Dev7f349142009-03-27 09:06:31 -0700563
Robert Love1875f272009-11-03 11:47:50 -0800564 fc_lport_init_stats(lport);
Vasu Dev7f349142009-03-27 09:06:31 -0700565
566 /* lport fc_lport related configuration */
Robert Love1875f272009-11-03 11:47:50 -0800567 fc_lport_config(lport);
Vasu Dev7f349142009-03-27 09:06:31 -0700568
569 /* offload related configuration */
Robert Love1875f272009-11-03 11:47:50 -0800570 lport->crc_offload = 0;
571 lport->seq_offload = 0;
572 lport->lro_enabled = 0;
573 lport->lro_xid = 0;
574 lport->lso_max = 0;
Vasu Dev7f349142009-03-27 09:06:31 -0700575
576 return 0;
577}
578
579/**
Yi Zou54a5b212010-07-20 15:21:17 -0700580 * fcoe_netdev_features_change - Updates the lport's offload flags based
581 * on the LLD netdev's FCoE feature flags
582 */
583static void fcoe_netdev_features_change(struct fc_lport *lport,
584 struct net_device *netdev)
585{
586 mutex_lock(&lport->lp_mutex);
587
588 if (netdev->features & NETIF_F_SG)
589 lport->sg_supp = 1;
590 else
591 lport->sg_supp = 0;
592
593 if (netdev->features & NETIF_F_FCOE_CRC) {
594 lport->crc_offload = 1;
595 FCOE_NETDEV_DBG(netdev, "Supports FCCRC offload\n");
596 } else {
597 lport->crc_offload = 0;
598 }
599
600 if (netdev->features & NETIF_F_FSO) {
601 lport->seq_offload = 1;
602 lport->lso_max = netdev->gso_max_size;
603 FCOE_NETDEV_DBG(netdev, "Supports LSO for max len 0x%x\n",
604 lport->lso_max);
605 } else {
606 lport->seq_offload = 0;
607 lport->lso_max = 0;
608 }
609
610 if (netdev->fcoe_ddp_xid) {
611 lport->lro_enabled = 1;
612 lport->lro_xid = netdev->fcoe_ddp_xid;
613 FCOE_NETDEV_DBG(netdev, "Supports LRO for max xid 0x%x\n",
614 lport->lro_xid);
615 } else {
616 lport->lro_enabled = 0;
617 lport->lro_xid = 0;
618 }
619
620 mutex_unlock(&lport->lp_mutex);
621}
622
623/**
Robert Love1875f272009-11-03 11:47:50 -0800624 * fcoe_netdev_config() - Set up net devive for SW FCoE
625 * @lport: The local port that is associated with the net device
626 * @netdev: The associated net device
Vasu Dev7f349142009-03-27 09:06:31 -0700627 *
Robert Love1875f272009-11-03 11:47:50 -0800628 * Must be called after fcoe_lport_config() as it will use local port mutex
Vasu Dev7f349142009-03-27 09:06:31 -0700629 *
Robert Love1875f272009-11-03 11:47:50 -0800630 * Returns: 0 for success
Vasu Dev7f349142009-03-27 09:06:31 -0700631 */
Robert Love1875f272009-11-03 11:47:50 -0800632static int fcoe_netdev_config(struct fc_lport *lport, struct net_device *netdev)
Vasu Dev7f349142009-03-27 09:06:31 -0700633{
634 u32 mfs;
635 u64 wwnn, wwpn;
Chris Leech25024982009-08-25 13:59:35 -0700636 struct fcoe_interface *fcoe;
Chris Leech014f5c32009-08-25 13:59:30 -0700637 struct fcoe_port *port;
Vasu Dev7f349142009-03-27 09:06:31 -0700638
639 /* Setup lport private data to point to fcoe softc */
Robert Love1875f272009-11-03 11:47:50 -0800640 port = lport_priv(lport);
Bhanu Prakash Gollapudi8597ae82011-01-28 16:05:37 -0800641 fcoe = port->priv;
Vasu Dev7f349142009-03-27 09:06:31 -0700642
643 /*
644 * Determine max frame size based on underlying device and optional
645 * user-configured limit. If the MFS is too low, fcoe_link_ok()
646 * will return 0, so do this first.
647 */
Yi Zou7221d7e2009-10-21 16:27:52 -0700648 mfs = netdev->mtu;
649 if (netdev->features & NETIF_F_FCOE_MTU) {
650 mfs = FCOE_MTU;
651 FCOE_NETDEV_DBG(netdev, "Supports FCOE_MTU of %d bytes\n", mfs);
652 }
653 mfs -= (sizeof(struct fcoe_hdr) + sizeof(struct fcoe_crc_eof));
Robert Love1875f272009-11-03 11:47:50 -0800654 if (fc_set_mfs(lport, mfs))
Vasu Dev7f349142009-03-27 09:06:31 -0700655 return -EINVAL;
656
Vasu Dev7f349142009-03-27 09:06:31 -0700657 /* offload features support */
Yi Zou54a5b212010-07-20 15:21:17 -0700658 fcoe_netdev_features_change(lport, netdev);
Vasu Dev7f349142009-03-27 09:06:31 -0700659
Chris Leech014f5c32009-08-25 13:59:30 -0700660 skb_queue_head_init(&port->fcoe_pending_queue);
661 port->fcoe_pending_queue_active = 0;
Robert Love1875f272009-11-03 11:47:50 -0800662 setup_timer(&port->timer, fcoe_queue_timer, (unsigned long)lport);
Vasu Dev7f349142009-03-27 09:06:31 -0700663
Robert Love5e4f8fe2010-05-07 15:18:35 -0700664 fcoe_link_speed_update(lport);
665
Robert Love1875f272009-11-03 11:47:50 -0800666 if (!lport->vport) {
Yi Zoudcece412009-11-20 15:22:21 -0800667 if (fcoe_get_wwn(netdev, &wwnn, NETDEV_FCOE_WWNN))
668 wwnn = fcoe_wwn_from_mac(fcoe->ctlr.ctl_src_addr, 1, 0);
Robert Love1875f272009-11-03 11:47:50 -0800669 fc_set_wwnn(lport, wwnn);
Yi Zoudcece412009-11-20 15:22:21 -0800670 if (fcoe_get_wwn(netdev, &wwpn, NETDEV_FCOE_WWPN))
671 wwpn = fcoe_wwn_from_mac(fcoe->ctlr.ctl_src_addr,
Vasu Devcf4aebca2010-07-20 15:21:22 -0700672 2, 0);
Robert Love1875f272009-11-03 11:47:50 -0800673 fc_set_wwpn(lport, wwpn);
Chris Leech9a057532009-11-03 11:46:40 -0800674 }
Vasu Dev7f349142009-03-27 09:06:31 -0700675
Vasu Dev7f349142009-03-27 09:06:31 -0700676 return 0;
677}
678
679/**
Robert Love1875f272009-11-03 11:47:50 -0800680 * fcoe_shost_config() - Set up the SCSI host associated with a local port
681 * @lport: The local port
Robert Love1875f272009-11-03 11:47:50 -0800682 * @dev: The device associated with the SCSI host
Vasu Dev7f349142009-03-27 09:06:31 -0700683 *
684 * Must be called after fcoe_lport_config() and fcoe_netdev_config()
685 *
Robert Love1875f272009-11-03 11:47:50 -0800686 * Returns: 0 for success
Vasu Dev7f349142009-03-27 09:06:31 -0700687 */
Vasu Dev8ba00a42010-04-09 14:22:54 -0700688static int fcoe_shost_config(struct fc_lport *lport, struct device *dev)
Vasu Dev7f349142009-03-27 09:06:31 -0700689{
690 int rc = 0;
691
692 /* lport scsi host config */
Robert Love1875f272009-11-03 11:47:50 -0800693 lport->host->max_lun = FCOE_MAX_LUN;
694 lport->host->max_id = FCOE_MAX_FCP_TARGET;
695 lport->host->max_channel = 0;
Vasu Devda87bfa2010-04-09 14:22:59 -0700696 lport->host->max_cmd_len = FCOE_MAX_CMD_LEN;
697
Robert Love1875f272009-11-03 11:47:50 -0800698 if (lport->vport)
Yi Zou8ca86f82011-01-28 16:05:11 -0800699 lport->host->transportt = fcoe_vport_scsi_transport;
Chris Leeche9084bb2009-11-03 11:46:34 -0800700 else
Yi Zou8ca86f82011-01-28 16:05:11 -0800701 lport->host->transportt = fcoe_nport_scsi_transport;
Vasu Dev7f349142009-03-27 09:06:31 -0700702
703 /* add the new host to the SCSI-ml */
Robert Love1875f272009-11-03 11:47:50 -0800704 rc = scsi_add_host(lport->host, dev);
Vasu Dev7f349142009-03-27 09:06:31 -0700705 if (rc) {
Robert Love1875f272009-11-03 11:47:50 -0800706 FCOE_NETDEV_DBG(fcoe_netdev(lport), "fcoe_shost_config: "
Robert Loved5488eb2009-06-10 15:30:59 -0700707 "error on scsi_add_host\n");
Vasu Dev7f349142009-03-27 09:06:31 -0700708 return rc;
709 }
Chris Leech9a057532009-11-03 11:46:40 -0800710
Robert Love1875f272009-11-03 11:47:50 -0800711 if (!lport->vport)
Alexey Dobriyan4be929b2010-05-24 14:33:03 -0700712 fc_host_max_npiv_vports(lport->host) = USHRT_MAX;
Chris Leech9a057532009-11-03 11:46:40 -0800713
Robert Love1875f272009-11-03 11:47:50 -0800714 snprintf(fc_host_symbolic_name(lport->host), FC_SYMBOLIC_NAME_SIZE,
Chris Leech5baa17c2009-11-03 11:46:56 -0800715 "%s v%s over %s", FCOE_NAME, FCOE_VERSION,
Robert Love1875f272009-11-03 11:47:50 -0800716 fcoe_netdev(lport)->name);
Vasu Dev7f349142009-03-27 09:06:31 -0700717
718 return 0;
719}
720
Robert Love1875f272009-11-03 11:47:50 -0800721/**
722 * fcoe_oem_match() - The match routine for the offloaded exchange manager
723 * @fp: The I/O frame
Vasu Devd7179682009-07-29 17:05:21 -0700724 *
Robert Love1875f272009-11-03 11:47:50 -0800725 * This routine will be associated with an exchange manager (EM). When
726 * the libfc exchange handling code is looking for an EM to use it will
727 * call this routine and pass it the frame that it wishes to send. This
728 * routine will return True if the associated EM is to be used and False
729 * if the echange code should continue looking for an EM.
730 *
731 * The offload EM that this routine is associated with will handle any
732 * packets that are for SCSI read requests.
733 *
Kiran Patil1ff99182011-06-20 16:59:15 -0700734 * This has been enhanced to work when FCoE stack is operating in target
735 * mode.
736 *
Robert Love1875f272009-11-03 11:47:50 -0800737 * Returns: True for read types I/O, otherwise returns false.
Vasu Devd7179682009-07-29 17:05:21 -0700738 */
739bool fcoe_oem_match(struct fc_frame *fp)
740{
Kiran Patil1ff99182011-06-20 16:59:15 -0700741 struct fc_frame_header *fh = fc_frame_header_get(fp);
742 struct fcp_cmnd *fcp;
743
744 if (fc_fcp_is_read(fr_fsp(fp)) &&
745 (fr_fsp(fp)->data_len > fcoe_ddp_min))
746 return true;
747 else if (!(ntoh24(fh->fh_f_ctl) & FC_FC_EX_CTX)) {
748 fcp = fc_frame_payload_get(fp, sizeof(*fcp));
749 if (ntohs(fh->fh_rx_id) == FC_XID_UNKNOWN &&
750 fcp && (ntohl(fcp->fc_dl) > fcoe_ddp_min) &&
751 (fcp->fc_flags & FCP_CFL_WRDATA))
752 return true;
753 }
754 return false;
Vasu Devd7179682009-07-29 17:05:21 -0700755}
756
Vasu Dev7f349142009-03-27 09:06:31 -0700757/**
Robert Love1875f272009-11-03 11:47:50 -0800758 * fcoe_em_config() - Allocate and configure an exchange manager
759 * @lport: The local port that the new EM will be associated with
Vasu Dev7f349142009-03-27 09:06:31 -0700760 *
Robert Love1875f272009-11-03 11:47:50 -0800761 * Returns: 0 on success
Vasu Dev7f349142009-03-27 09:06:31 -0700762 */
Robert Love1875f272009-11-03 11:47:50 -0800763static inline int fcoe_em_config(struct fc_lport *lport)
Vasu Dev7f349142009-03-27 09:06:31 -0700764{
Robert Love1875f272009-11-03 11:47:50 -0800765 struct fcoe_port *port = lport_priv(lport);
Bhanu Prakash Gollapudi8597ae82011-01-28 16:05:37 -0800766 struct fcoe_interface *fcoe = port->priv;
Chris Leech25024982009-08-25 13:59:35 -0700767 struct fcoe_interface *oldfcoe = NULL;
Vasu Dev1d1b88d2009-07-29 17:05:45 -0700768 struct net_device *old_real_dev, *cur_real_dev;
Vasu Devd7179682009-07-29 17:05:21 -0700769 u16 min_xid = FCOE_MIN_XID;
770 u16 max_xid = FCOE_MAX_XID;
771
772 /*
773 * Check if need to allocate an em instance for
774 * offload exchange ids to be shared across all VN_PORTs/lport.
775 */
Robert Love1875f272009-11-03 11:47:50 -0800776 if (!lport->lro_enabled || !lport->lro_xid ||
777 (lport->lro_xid >= max_xid)) {
778 lport->lro_xid = 0;
Vasu Devd7179682009-07-29 17:05:21 -0700779 goto skip_oem;
780 }
781
782 /*
783 * Reuse existing offload em instance in case
Vasu Dev1d1b88d2009-07-29 17:05:45 -0700784 * it is already allocated on real eth device
Vasu Devd7179682009-07-29 17:05:21 -0700785 */
Chris Leech25024982009-08-25 13:59:35 -0700786 if (fcoe->netdev->priv_flags & IFF_802_1Q_VLAN)
787 cur_real_dev = vlan_dev_real_dev(fcoe->netdev);
Vasu Dev1d1b88d2009-07-29 17:05:45 -0700788 else
Chris Leech25024982009-08-25 13:59:35 -0700789 cur_real_dev = fcoe->netdev;
Vasu Dev1d1b88d2009-07-29 17:05:45 -0700790
Chris Leech25024982009-08-25 13:59:35 -0700791 list_for_each_entry(oldfcoe, &fcoe_hostlist, list) {
Chris Leech25024982009-08-25 13:59:35 -0700792 if (oldfcoe->netdev->priv_flags & IFF_802_1Q_VLAN)
793 old_real_dev = vlan_dev_real_dev(oldfcoe->netdev);
Vasu Dev1d1b88d2009-07-29 17:05:45 -0700794 else
Chris Leech25024982009-08-25 13:59:35 -0700795 old_real_dev = oldfcoe->netdev;
Vasu Dev1d1b88d2009-07-29 17:05:45 -0700796
797 if (cur_real_dev == old_real_dev) {
Chris Leech991cbb62009-08-25 13:59:51 -0700798 fcoe->oem = oldfcoe->oem;
Vasu Devd7179682009-07-29 17:05:21 -0700799 break;
800 }
801 }
802
Chris Leech991cbb62009-08-25 13:59:51 -0700803 if (fcoe->oem) {
Robert Love1875f272009-11-03 11:47:50 -0800804 if (!fc_exch_mgr_add(lport, fcoe->oem, fcoe_oem_match)) {
Vasu Devd7179682009-07-29 17:05:21 -0700805 printk(KERN_ERR "fcoe_em_config: failed to add "
806 "offload em:%p on interface:%s\n",
Chris Leech991cbb62009-08-25 13:59:51 -0700807 fcoe->oem, fcoe->netdev->name);
Vasu Devd7179682009-07-29 17:05:21 -0700808 return -ENOMEM;
809 }
810 } else {
Robert Love1875f272009-11-03 11:47:50 -0800811 fcoe->oem = fc_exch_mgr_alloc(lport, FC_CLASS_3,
812 FCOE_MIN_XID, lport->lro_xid,
813 fcoe_oem_match);
Chris Leech991cbb62009-08-25 13:59:51 -0700814 if (!fcoe->oem) {
Vasu Devd7179682009-07-29 17:05:21 -0700815 printk(KERN_ERR "fcoe_em_config: failed to allocate "
816 "em for offload exches on interface:%s\n",
Chris Leech25024982009-08-25 13:59:35 -0700817 fcoe->netdev->name);
Vasu Devd7179682009-07-29 17:05:21 -0700818 return -ENOMEM;
819 }
820 }
821
822 /*
823 * Exclude offload EM xid range from next EM xid range.
824 */
Robert Love1875f272009-11-03 11:47:50 -0800825 min_xid += lport->lro_xid + 1;
Vasu Devd7179682009-07-29 17:05:21 -0700826
827skip_oem:
Robert Love1875f272009-11-03 11:47:50 -0800828 if (!fc_exch_mgr_alloc(lport, FC_CLASS_3, min_xid, max_xid, NULL)) {
Vasu Devd7179682009-07-29 17:05:21 -0700829 printk(KERN_ERR "fcoe_em_config: failed to "
Chris Leech25024982009-08-25 13:59:35 -0700830 "allocate em on interface %s\n", fcoe->netdev->name);
Vasu Dev7f349142009-03-27 09:06:31 -0700831 return -ENOMEM;
Vasu Devd7179682009-07-29 17:05:21 -0700832 }
Vasu Dev7f349142009-03-27 09:06:31 -0700833
834 return 0;
835}
836
837/**
Robert Love1875f272009-11-03 11:47:50 -0800838 * fcoe_if_destroy() - Tear down a SW FCoE instance
839 * @lport: The local port to be destroyed
Vasu Dev34ce27b2010-05-07 15:18:46 -0700840 *
Vasu Dev7f349142009-03-27 09:06:31 -0700841 */
Chris Leechaf7f85d2009-08-25 13:59:24 -0700842static void fcoe_if_destroy(struct fc_lport *lport)
Vasu Dev7f349142009-03-27 09:06:31 -0700843{
Neerav Parikhb2085a42011-06-20 16:59:51 -0700844 struct fcoe_port *port = lport_priv(lport);
845 struct fcoe_interface *fcoe = port->priv;
846 struct net_device *netdev = fcoe->netdev;
847
848 FCOE_NETDEV_DBG(netdev, "Destroying interface\n");
849
850 /* Logout of the fabric */
851 fc_fabric_logoff(lport);
852
853 /* Cleanup the fc_lport */
854 fc_lport_destroy(lport);
855
856 /* Stop the transmit retry timer */
857 del_timer_sync(&port->timer);
858
859 /* Free existing transmit skbs */
860 fcoe_clean_pending_queue(lport);
861
862 rtnl_lock();
863 if (!is_zero_ether_addr(port->data_src_addr))
864 dev_uc_del(netdev, port->data_src_addr);
865 rtnl_unlock();
866
867 /* Release reference held in fcoe_if_create() */
868 fcoe_interface_put(fcoe);
869
Chris Leech54b649f2009-08-25 14:00:07 -0700870 /* Free queued packets for the per-CPU receive threads */
871 fcoe_percpu_clean(lport);
872
Vasu Dev7f349142009-03-27 09:06:31 -0700873 /* Detach from the scsi-ml */
Chris Leechaf7f85d2009-08-25 13:59:24 -0700874 fc_remove_host(lport->host);
875 scsi_remove_host(lport->host);
Vasu Dev7f349142009-03-27 09:06:31 -0700876
Yi Zou80e736f2010-11-30 16:18:07 -0800877 /* Destroy lport scsi_priv */
878 fc_fcp_destroy(lport);
879
Vasu Dev7f349142009-03-27 09:06:31 -0700880 /* There are no more rports or I/O, free the EM */
Chris Leechaf7f85d2009-08-25 13:59:24 -0700881 fc_exch_mgr_free(lport);
Vasu Dev7f349142009-03-27 09:06:31 -0700882
Vasu Dev7f349142009-03-27 09:06:31 -0700883 /* Free memory used by statistical counters */
Chris Leechaf7f85d2009-08-25 13:59:24 -0700884 fc_lport_free_stats(lport);
Vasu Dev7f349142009-03-27 09:06:31 -0700885
Chris Leech2e70e242009-08-25 14:00:23 -0700886 /* Release the Scsi_Host */
Chris Leechaf7f85d2009-08-25 13:59:24 -0700887 scsi_host_put(lport->host);
Vasu Dev7f349142009-03-27 09:06:31 -0700888}
889
Robert Love1875f272009-11-03 11:47:50 -0800890/**
891 * fcoe_ddp_setup() - Call a LLD's ddp_setup through the net device
892 * @lport: The local port to setup DDP for
893 * @xid: The exchange ID for this DDP transfer
894 * @sgl: The scatterlist describing this transfer
895 * @sgc: The number of sg items
Vasu Dev7f349142009-03-27 09:06:31 -0700896 *
Robert Love1875f272009-11-03 11:47:50 -0800897 * Returns: 0 if the DDP context was not configured
Vasu Dev7f349142009-03-27 09:06:31 -0700898 */
Robert Love1875f272009-11-03 11:47:50 -0800899static int fcoe_ddp_setup(struct fc_lport *lport, u16 xid,
900 struct scatterlist *sgl, unsigned int sgc)
Vasu Dev7f349142009-03-27 09:06:31 -0700901{
Robert Love1875f272009-11-03 11:47:50 -0800902 struct net_device *netdev = fcoe_netdev(lport);
Vasu Dev7f349142009-03-27 09:06:31 -0700903
Robert Love1875f272009-11-03 11:47:50 -0800904 if (netdev->netdev_ops->ndo_fcoe_ddp_setup)
905 return netdev->netdev_ops->ndo_fcoe_ddp_setup(netdev,
906 xid, sgl,
907 sgc);
Vasu Dev7f349142009-03-27 09:06:31 -0700908
909 return 0;
910}
911
Vasu Dev7f349142009-03-27 09:06:31 -0700912/**
Yi Zou71f89492011-06-20 16:59:10 -0700913 * fcoe_ddp_target() - Call a LLD's ddp_target through the net device
914 * @lport: The local port to setup DDP for
915 * @xid: The exchange ID for this DDP transfer
916 * @sgl: The scatterlist describing this transfer
917 * @sgc: The number of sg items
918 *
919 * Returns: 0 if the DDP context was not configured
920 */
921static int fcoe_ddp_target(struct fc_lport *lport, u16 xid,
922 struct scatterlist *sgl, unsigned int sgc)
923{
924 struct net_device *netdev = fcoe_netdev(lport);
925
926 if (netdev->netdev_ops->ndo_fcoe_ddp_target)
927 return netdev->netdev_ops->ndo_fcoe_ddp_target(netdev, xid,
928 sgl, sgc);
929
930 return 0;
931}
932
933
934/**
Robert Love1875f272009-11-03 11:47:50 -0800935 * fcoe_ddp_done() - Call a LLD's ddp_done through the net device
936 * @lport: The local port to complete DDP on
937 * @xid: The exchange ID for this DDP transfer
Vasu Dev7f349142009-03-27 09:06:31 -0700938 *
Robert Love1875f272009-11-03 11:47:50 -0800939 * Returns: the length of data that have been completed by DDP
940 */
941static int fcoe_ddp_done(struct fc_lport *lport, u16 xid)
942{
943 struct net_device *netdev = fcoe_netdev(lport);
944
945 if (netdev->netdev_ops->ndo_fcoe_ddp_done)
946 return netdev->netdev_ops->ndo_fcoe_ddp_done(netdev, xid);
947 return 0;
948}
949
950/**
951 * fcoe_if_create() - Create a FCoE instance on an interface
952 * @fcoe: The FCoE interface to create a local port on
953 * @parent: The device pointer to be the parent in sysfs for the SCSI host
954 * @npiv: Indicates if the port is a vport or not
Vasu Dev7f349142009-03-27 09:06:31 -0700955 *
Robert Love1875f272009-11-03 11:47:50 -0800956 * Creates a fc_lport instance and a Scsi_Host instance and configure them.
957 *
958 * Returns: The allocated fc_lport or an error pointer
Vasu Dev7f349142009-03-27 09:06:31 -0700959 */
Chris Leech030f4e02009-08-25 14:00:02 -0700960static struct fc_lport *fcoe_if_create(struct fcoe_interface *fcoe,
Chris Leech9a057532009-11-03 11:46:40 -0800961 struct device *parent, int npiv)
Vasu Dev7f349142009-03-27 09:06:31 -0700962{
Robert Love1875f272009-11-03 11:47:50 -0800963 struct net_device *netdev = fcoe->netdev;
Vasu Dev72fa3962011-02-25 15:03:01 -0800964 struct fc_lport *lport, *n_port;
Chris Leech014f5c32009-08-25 13:59:30 -0700965 struct fcoe_port *port;
Vasu Dev72fa3962011-02-25 15:03:01 -0800966 struct Scsi_Host *shost;
Robert Love1875f272009-11-03 11:47:50 -0800967 int rc;
Chris Leech9a057532009-11-03 11:46:40 -0800968 /*
969 * parent is only a vport if npiv is 1,
970 * but we'll only use vport in that case so go ahead and set it
971 */
972 struct fc_vport *vport = dev_to_vport(parent);
Vasu Dev7f349142009-03-27 09:06:31 -0700973
Robert Loved5488eb2009-06-10 15:30:59 -0700974 FCOE_NETDEV_DBG(netdev, "Create Interface\n");
Vasu Dev7f349142009-03-27 09:06:31 -0700975
Vasu Dev72fa3962011-02-25 15:03:01 -0800976 if (!npiv)
977 lport = libfc_host_alloc(&fcoe_shost_template, sizeof(*port));
978 else
979 lport = libfc_vport_create(vport, sizeof(*port));
980
Chris Leech86221962009-11-03 11:46:08 -0800981 if (!lport) {
Chris Leech014f5c32009-08-25 13:59:30 -0700982 FCOE_NETDEV_DBG(netdev, "Could not allocate host structure\n");
983 rc = -ENOMEM;
Chris Leech030f4e02009-08-25 14:00:02 -0700984 goto out;
Chris Leech014f5c32009-08-25 13:59:30 -0700985 }
Chris Leech014f5c32009-08-25 13:59:30 -0700986 port = lport_priv(lport);
Chris Leech2e70e242009-08-25 14:00:23 -0700987 port->lport = lport;
Bhanu Prakash Gollapudi8597ae82011-01-28 16:05:37 -0800988 port->priv = fcoe;
989 port->max_queue_depth = FCOE_MAX_QUEUE_DEPTH;
990 port->min_queue_depth = FCOE_MIN_QUEUE_DEPTH;
Chris Leech2e70e242009-08-25 14:00:23 -0700991 INIT_WORK(&port->destroy_work, fcoe_destroy_work);
Vasu Dev7f349142009-03-27 09:06:31 -0700992
Robert Love1875f272009-11-03 11:47:50 -0800993 /* configure a fc_lport including the exchange manager */
Chris Leechaf7f85d2009-08-25 13:59:24 -0700994 rc = fcoe_lport_config(lport);
Vasu Dev7f349142009-03-27 09:06:31 -0700995 if (rc) {
Robert Loved5488eb2009-06-10 15:30:59 -0700996 FCOE_NETDEV_DBG(netdev, "Could not configure lport for the "
997 "interface\n");
Vasu Dev7f349142009-03-27 09:06:31 -0700998 goto out_host_put;
999 }
1000
Chris Leech9a057532009-11-03 11:46:40 -08001001 if (npiv) {
Chris Leech9f8f3aa2010-04-09 14:23:16 -07001002 FCOE_NETDEV_DBG(netdev, "Setting vport names, "
1003 "%16.16llx %16.16llx\n",
Robert Love1875f272009-11-03 11:47:50 -08001004 vport->node_name, vport->port_name);
Chris Leech9a057532009-11-03 11:46:40 -08001005 fc_set_wwnn(lport, vport->node_name);
1006 fc_set_wwpn(lport, vport->port_name);
1007 }
1008
Vasu Devab6b85c2009-05-17 12:33:08 +00001009 /* configure lport network properties */
Chris Leechaf7f85d2009-08-25 13:59:24 -07001010 rc = fcoe_netdev_config(lport, netdev);
Vasu Devab6b85c2009-05-17 12:33:08 +00001011 if (rc) {
Robert Loved5488eb2009-06-10 15:30:59 -07001012 FCOE_NETDEV_DBG(netdev, "Could not configure netdev for the "
1013 "interface\n");
Chris Leech54b649f2009-08-25 14:00:07 -07001014 goto out_lp_destroy;
Vasu Devab6b85c2009-05-17 12:33:08 +00001015 }
Joe Eykholt97c83892009-03-17 11:42:40 -07001016
Vasu Dev7f349142009-03-27 09:06:31 -07001017 /* configure lport scsi host properties */
Vasu Dev8ba00a42010-04-09 14:22:54 -07001018 rc = fcoe_shost_config(lport, parent);
Vasu Dev7f349142009-03-27 09:06:31 -07001019 if (rc) {
Robert Loved5488eb2009-06-10 15:30:59 -07001020 FCOE_NETDEV_DBG(netdev, "Could not configure shost for the "
1021 "interface\n");
Chris Leech54b649f2009-08-25 14:00:07 -07001022 goto out_lp_destroy;
Vasu Dev7f349142009-03-27 09:06:31 -07001023 }
1024
Vasu Dev7f349142009-03-27 09:06:31 -07001025 /* Initialize the library */
Joe Eykholte10f8c62010-07-20 15:20:30 -07001026 rc = fcoe_libfc_config(lport, &fcoe->ctlr, &fcoe_libfc_fcn_templ, 1);
Vasu Dev7f349142009-03-27 09:06:31 -07001027 if (rc) {
Robert Loved5488eb2009-06-10 15:30:59 -07001028 FCOE_NETDEV_DBG(netdev, "Could not configure libfc for the "
1029 "interface\n");
Vasu Dev7f349142009-03-27 09:06:31 -07001030 goto out_lp_destroy;
1031 }
1032
Vasu Dev72fa3962011-02-25 15:03:01 -08001033 /*
1034 * fcoe_em_alloc() and fcoe_hostlist_add() both
1035 * need to be atomic with respect to other changes to the
1036 * hostlist since fcoe_em_alloc() looks for an existing EM
1037 * instance on host list updated by fcoe_hostlist_add().
1038 *
1039 * This is currently handled through the fcoe_config_mutex
1040 * begin held.
1041 */
1042 if (!npiv)
Chris Leech9a057532009-11-03 11:46:40 -08001043 /* lport exch manager allocation */
1044 rc = fcoe_em_config(lport);
Vasu Dev72fa3962011-02-25 15:03:01 -08001045 else {
1046 shost = vport_to_shost(vport);
1047 n_port = shost_priv(shost);
1048 rc = fc_exch_mgr_list_clone(n_port, lport);
1049 }
1050
1051 if (rc) {
1052 FCOE_NETDEV_DBG(netdev, "Could not configure the EM\n");
1053 goto out_lp_destroy;
Vasu Dev96316092009-07-29 17:05:00 -07001054 }
1055
Chris Leech030f4e02009-08-25 14:00:02 -07001056 fcoe_interface_get(fcoe);
Chris Leechaf7f85d2009-08-25 13:59:24 -07001057 return lport;
Vasu Dev7f349142009-03-27 09:06:31 -07001058
1059out_lp_destroy:
Chris Leechaf7f85d2009-08-25 13:59:24 -07001060 fc_exch_mgr_free(lport);
Vasu Dev7f349142009-03-27 09:06:31 -07001061out_host_put:
Chris Leechaf7f85d2009-08-25 13:59:24 -07001062 scsi_host_put(lport->host);
1063out:
1064 return ERR_PTR(rc);
Vasu Dev7f349142009-03-27 09:06:31 -07001065}
1066
1067/**
Robert Love1875f272009-11-03 11:47:50 -08001068 * fcoe_if_init() - Initialization routine for fcoe.ko
Vasu Dev7f349142009-03-27 09:06:31 -07001069 *
Robert Love1875f272009-11-03 11:47:50 -08001070 * Attaches the SW FCoE transport to the FC transport
1071 *
1072 * Returns: 0 on success
Vasu Dev7f349142009-03-27 09:06:31 -07001073 */
1074static int __init fcoe_if_init(void)
1075{
1076 /* attach to scsi transport */
Yi Zou8ca86f82011-01-28 16:05:11 -08001077 fcoe_nport_scsi_transport =
1078 fc_attach_transport(&fcoe_nport_fc_functions);
1079 fcoe_vport_scsi_transport =
1080 fc_attach_transport(&fcoe_vport_fc_functions);
Vasu Dev7f349142009-03-27 09:06:31 -07001081
Yi Zou8ca86f82011-01-28 16:05:11 -08001082 if (!fcoe_nport_scsi_transport) {
Robert Loved5488eb2009-06-10 15:30:59 -07001083 printk(KERN_ERR "fcoe: Failed to attach to the FC transport\n");
Vasu Dev7f349142009-03-27 09:06:31 -07001084 return -ENODEV;
1085 }
1086
1087 return 0;
1088}
1089
1090/**
Robert Love1875f272009-11-03 11:47:50 -08001091 * fcoe_if_exit() - Tear down fcoe.ko
Vasu Dev7f349142009-03-27 09:06:31 -07001092 *
Robert Love1875f272009-11-03 11:47:50 -08001093 * Detaches the SW FCoE transport from the FC transport
1094 *
1095 * Returns: 0 on success
Vasu Dev7f349142009-03-27 09:06:31 -07001096 */
1097int __exit fcoe_if_exit(void)
1098{
Yi Zou8ca86f82011-01-28 16:05:11 -08001099 fc_release_transport(fcoe_nport_scsi_transport);
1100 fc_release_transport(fcoe_vport_scsi_transport);
1101 fcoe_nport_scsi_transport = NULL;
1102 fcoe_vport_scsi_transport = NULL;
Vasu Dev7f349142009-03-27 09:06:31 -07001103 return 0;
1104}
1105
Robert Love85b4aa42008-12-09 15:10:24 -08001106/**
Robert Love1875f272009-11-03 11:47:50 -08001107 * fcoe_percpu_thread_create() - Create a receive thread for an online CPU
1108 * @cpu: The CPU index of the CPU to create a receive thread for
Robert Love8976f422009-03-17 11:41:46 -07001109 */
1110static void fcoe_percpu_thread_create(unsigned int cpu)
1111{
1112 struct fcoe_percpu_s *p;
1113 struct task_struct *thread;
1114
1115 p = &per_cpu(fcoe_percpu, cpu);
1116
Eric Dumazet5c609ff2011-09-27 21:37:52 -07001117 thread = kthread_create_on_node(fcoe_percpu_receive_thread,
1118 (void *)p, cpu_to_node(cpu),
1119 "fcoethread/%d", cpu);
Robert Love8976f422009-03-17 11:41:46 -07001120
Joe Eykholte7a51992009-08-25 14:04:08 -07001121 if (likely(!IS_ERR(thread))) {
Robert Love8976f422009-03-17 11:41:46 -07001122 kthread_bind(thread, cpu);
1123 wake_up_process(thread);
1124
1125 spin_lock_bh(&p->fcoe_rx_list.lock);
1126 p->thread = thread;
1127 spin_unlock_bh(&p->fcoe_rx_list.lock);
1128 }
1129}
1130
1131/**
Robert Love1875f272009-11-03 11:47:50 -08001132 * fcoe_percpu_thread_destroy() - Remove the receive thread of a CPU
1133 * @cpu: The CPU index of the CPU whose receive thread is to be destroyed
Robert Love8976f422009-03-17 11:41:46 -07001134 *
1135 * Destroys a per-CPU Rx thread. Any pending skbs are moved to the
1136 * current CPU's Rx thread. If the thread being destroyed is bound to
1137 * the CPU processing this context the skbs will be freed.
1138 */
1139static void fcoe_percpu_thread_destroy(unsigned int cpu)
1140{
1141 struct fcoe_percpu_s *p;
1142 struct task_struct *thread;
1143 struct page *crc_eof;
1144 struct sk_buff *skb;
1145#ifdef CONFIG_SMP
1146 struct fcoe_percpu_s *p0;
Joe Eykholtf018b732010-03-12 16:08:55 -08001147 unsigned targ_cpu = get_cpu();
Robert Love8976f422009-03-17 11:41:46 -07001148#endif /* CONFIG_SMP */
1149
Robert Loved5488eb2009-06-10 15:30:59 -07001150 FCOE_DBG("Destroying receive thread for CPU %d\n", cpu);
Robert Love8976f422009-03-17 11:41:46 -07001151
1152 /* Prevent any new skbs from being queued for this CPU. */
1153 p = &per_cpu(fcoe_percpu, cpu);
1154 spin_lock_bh(&p->fcoe_rx_list.lock);
1155 thread = p->thread;
1156 p->thread = NULL;
1157 crc_eof = p->crc_eof_page;
1158 p->crc_eof_page = NULL;
1159 p->crc_eof_offset = 0;
1160 spin_unlock_bh(&p->fcoe_rx_list.lock);
1161
1162#ifdef CONFIG_SMP
1163 /*
1164 * Don't bother moving the skb's if this context is running
1165 * on the same CPU that is having its thread destroyed. This
1166 * can easily happen when the module is removed.
1167 */
1168 if (cpu != targ_cpu) {
1169 p0 = &per_cpu(fcoe_percpu, targ_cpu);
1170 spin_lock_bh(&p0->fcoe_rx_list.lock);
1171 if (p0->thread) {
Robert Loved5488eb2009-06-10 15:30:59 -07001172 FCOE_DBG("Moving frames from CPU %d to CPU %d\n",
1173 cpu, targ_cpu);
Robert Love8976f422009-03-17 11:41:46 -07001174
1175 while ((skb = __skb_dequeue(&p->fcoe_rx_list)) != NULL)
1176 __skb_queue_tail(&p0->fcoe_rx_list, skb);
1177 spin_unlock_bh(&p0->fcoe_rx_list.lock);
1178 } else {
1179 /*
1180 * The targeted CPU is not initialized and cannot accept
Robert Love1875f272009-11-03 11:47:50 -08001181 * new skbs. Unlock the targeted CPU and drop the skbs
Robert Love8976f422009-03-17 11:41:46 -07001182 * on the CPU that is going offline.
1183 */
1184 while ((skb = __skb_dequeue(&p->fcoe_rx_list)) != NULL)
1185 kfree_skb(skb);
1186 spin_unlock_bh(&p0->fcoe_rx_list.lock);
1187 }
1188 } else {
1189 /*
1190 * This scenario occurs when the module is being removed
1191 * and all threads are being destroyed. skbs will continue
1192 * to be shifted from the CPU thread that is being removed
1193 * to the CPU thread associated with the CPU that is processing
1194 * the module removal. Once there is only one CPU Rx thread it
1195 * will reach this case and we will drop all skbs and later
1196 * stop the thread.
1197 */
1198 spin_lock_bh(&p->fcoe_rx_list.lock);
1199 while ((skb = __skb_dequeue(&p->fcoe_rx_list)) != NULL)
1200 kfree_skb(skb);
1201 spin_unlock_bh(&p->fcoe_rx_list.lock);
1202 }
Joe Eykholtf018b732010-03-12 16:08:55 -08001203 put_cpu();
Robert Love8976f422009-03-17 11:41:46 -07001204#else
1205 /*
Chris Leechdd3fd722009-04-21 16:27:36 -07001206 * This a non-SMP scenario where the singular Rx thread is
Robert Love8976f422009-03-17 11:41:46 -07001207 * being removed. Free all skbs and stop the thread.
1208 */
1209 spin_lock_bh(&p->fcoe_rx_list.lock);
1210 while ((skb = __skb_dequeue(&p->fcoe_rx_list)) != NULL)
1211 kfree_skb(skb);
1212 spin_unlock_bh(&p->fcoe_rx_list.lock);
1213#endif
1214
1215 if (thread)
1216 kthread_stop(thread);
1217
1218 if (crc_eof)
1219 put_page(crc_eof);
1220}
1221
1222/**
Robert Love1875f272009-11-03 11:47:50 -08001223 * fcoe_cpu_callback() - Handler for CPU hotplug events
1224 * @nfb: The callback data block
1225 * @action: The event triggering the callback
1226 * @hcpu: The index of the CPU that the event is for
Robert Love8976f422009-03-17 11:41:46 -07001227 *
Robert Love1875f272009-11-03 11:47:50 -08001228 * This creates or destroys per-CPU data for fcoe
Robert Love8976f422009-03-17 11:41:46 -07001229 *
1230 * Returns NOTIFY_OK always.
1231 */
1232static int fcoe_cpu_callback(struct notifier_block *nfb,
1233 unsigned long action, void *hcpu)
1234{
1235 unsigned cpu = (unsigned long)hcpu;
1236
1237 switch (action) {
1238 case CPU_ONLINE:
1239 case CPU_ONLINE_FROZEN:
Robert Loved5488eb2009-06-10 15:30:59 -07001240 FCOE_DBG("CPU %x online: Create Rx thread\n", cpu);
Robert Love8976f422009-03-17 11:41:46 -07001241 fcoe_percpu_thread_create(cpu);
1242 break;
1243 case CPU_DEAD:
1244 case CPU_DEAD_FROZEN:
Robert Loved5488eb2009-06-10 15:30:59 -07001245 FCOE_DBG("CPU %x offline: Remove Rx thread\n", cpu);
Robert Love8976f422009-03-17 11:41:46 -07001246 fcoe_percpu_thread_destroy(cpu);
1247 break;
1248 default:
1249 break;
1250 }
1251 return NOTIFY_OK;
1252}
1253
Robert Love8976f422009-03-17 11:41:46 -07001254/**
Kiran Patil064287e2011-06-20 16:59:20 -07001255 * fcoe_select_cpu() - Selects CPU to handle post-processing of incoming
1256 * command.
Kiran Patil064287e2011-06-20 16:59:20 -07001257 *
Vasu Devd2722812011-07-27 15:11:10 -07001258 * This routine selects next CPU based on cpumask to distribute
1259 * incoming requests in round robin.
Kiran Patil064287e2011-06-20 16:59:20 -07001260 *
Vasu Devd2722812011-07-27 15:11:10 -07001261 * Returns: int CPU number
Kiran Patil064287e2011-06-20 16:59:20 -07001262 */
Vasu Devd2722812011-07-27 15:11:10 -07001263static inline unsigned int fcoe_select_cpu(void)
Kiran Patil064287e2011-06-20 16:59:20 -07001264{
1265 static unsigned int selected_cpu;
1266
Vasu Devd2722812011-07-27 15:11:10 -07001267 selected_cpu = cpumask_next(selected_cpu, cpu_online_mask);
1268 if (selected_cpu >= nr_cpu_ids)
1269 selected_cpu = cpumask_first(cpu_online_mask);
1270
Kiran Patil064287e2011-06-20 16:59:20 -07001271 return selected_cpu;
1272}
1273
1274/**
Robert Love1875f272009-11-03 11:47:50 -08001275 * fcoe_rcv() - Receive packets from a net device
1276 * @skb: The received packet
1277 * @netdev: The net device that the packet was received on
1278 * @ptype: The packet type context
1279 * @olddev: The last device net device
Robert Love85b4aa42008-12-09 15:10:24 -08001280 *
Robert Love1875f272009-11-03 11:47:50 -08001281 * This routine is called by NET_RX_SOFTIRQ. It receives a packet, builds a
1282 * FC frame and passes the frame to libfc.
Robert Love85b4aa42008-12-09 15:10:24 -08001283 *
1284 * Returns: 0 for success
Robert Love34f42a02009-02-27 10:55:45 -08001285 */
Robert Love1875f272009-11-03 11:47:50 -08001286int fcoe_rcv(struct sk_buff *skb, struct net_device *netdev,
Robert Love85b4aa42008-12-09 15:10:24 -08001287 struct packet_type *ptype, struct net_device *olddev)
1288{
Robert Love1875f272009-11-03 11:47:50 -08001289 struct fc_lport *lport;
Robert Love85b4aa42008-12-09 15:10:24 -08001290 struct fcoe_rcv_info *fr;
Chris Leech259ad852009-08-25 13:59:41 -07001291 struct fcoe_interface *fcoe;
Robert Love85b4aa42008-12-09 15:10:24 -08001292 struct fc_frame_header *fh;
Robert Love85b4aa42008-12-09 15:10:24 -08001293 struct fcoe_percpu_s *fps;
Vasu Dev519e5132010-07-20 15:19:32 -07001294 struct ethhdr *eh;
Vasu Devb2f00912009-08-25 13:58:53 -07001295 unsigned int cpu;
Robert Love85b4aa42008-12-09 15:10:24 -08001296
Chris Leech259ad852009-08-25 13:59:41 -07001297 fcoe = container_of(ptype, struct fcoe_interface, fcoe_packet_type);
Robert Love1875f272009-11-03 11:47:50 -08001298 lport = fcoe->ctlr.lp;
1299 if (unlikely(!lport)) {
1300 FCOE_NETDEV_DBG(netdev, "Cannot find hba structure");
Robert Love85b4aa42008-12-09 15:10:24 -08001301 goto err2;
1302 }
Robert Love1875f272009-11-03 11:47:50 -08001303 if (!lport->link_up)
Joe Eykholt97c83892009-03-17 11:42:40 -07001304 goto err2;
Robert Love85b4aa42008-12-09 15:10:24 -08001305
Robert Love1875f272009-11-03 11:47:50 -08001306 FCOE_NETDEV_DBG(netdev, "skb_info: len:%d data_len:%d head:%p "
Robert Loved5488eb2009-06-10 15:30:59 -07001307 "data:%p tail:%p end:%p sum:%d dev:%s",
1308 skb->len, skb->data_len, skb->head, skb->data,
1309 skb_tail_pointer(skb), skb_end_pointer(skb),
1310 skb->csum, skb->dev ? skb->dev->name : "<NULL>");
Robert Love85b4aa42008-12-09 15:10:24 -08001311
Vasu Dev519e5132010-07-20 15:19:32 -07001312 eh = eth_hdr(skb);
Vasu Dev519e5132010-07-20 15:19:32 -07001313
1314 if (is_fip_mode(&fcoe->ctlr) &&
1315 compare_ether_addr(eh->h_source, fcoe->ctlr.dest_addr)) {
1316 FCOE_NETDEV_DBG(netdev, "wrong source mac address:%pM\n",
1317 eh->h_source);
Robert Love85b4aa42008-12-09 15:10:24 -08001318 goto err;
1319 }
1320
1321 /*
1322 * Check for minimum frame length, and make sure required FCoE
1323 * and FC headers are pulled into the linear data area.
1324 */
1325 if (unlikely((skb->len < FCOE_MIN_FRAME) ||
Robert Love1875f272009-11-03 11:47:50 -08001326 !pskb_may_pull(skb, FCOE_HEADER_LEN)))
Robert Love85b4aa42008-12-09 15:10:24 -08001327 goto err;
1328
1329 skb_set_transport_header(skb, sizeof(struct fcoe_hdr));
1330 fh = (struct fc_frame_header *) skb_transport_header(skb);
1331
Robert Love0ee31cb2010-10-08 17:12:46 -07001332 if (ntoh24(&eh->h_dest[3]) != ntoh24(fh->fh_d_id)) {
1333 FCOE_NETDEV_DBG(netdev, "FC frame d_id mismatch with MAC:%pM\n",
1334 eh->h_dest);
1335 goto err;
1336 }
1337
Robert Love85b4aa42008-12-09 15:10:24 -08001338 fr = fcoe_dev_from_skb(skb);
Robert Love1875f272009-11-03 11:47:50 -08001339 fr->fr_dev = lport;
Robert Love5e5e92d2009-03-17 11:41:35 -07001340
Robert Love85b4aa42008-12-09 15:10:24 -08001341 /*
Vasu Devb2f00912009-08-25 13:58:53 -07001342 * In case the incoming frame's exchange is originated from
1343 * the initiator, then received frame's exchange id is ANDed
1344 * with fc_cpu_mask bits to get the same cpu on which exchange
Vasu Devd2722812011-07-27 15:11:10 -07001345 * was originated, otherwise select cpu using rx exchange id
1346 * or fcoe_select_cpu().
Robert Love85b4aa42008-12-09 15:10:24 -08001347 */
Vasu Devb2f00912009-08-25 13:58:53 -07001348 if (ntoh24(fh->fh_f_ctl) & FC_FC_EX_CTX)
1349 cpu = ntohs(fh->fh_ox_id) & fc_cpu_mask;
Kiran Patil064287e2011-06-20 16:59:20 -07001350 else {
Vasu Devd2722812011-07-27 15:11:10 -07001351 if (ntohs(fh->fh_rx_id) == FC_XID_UNKNOWN)
1352 cpu = fcoe_select_cpu();
1353 else
Kiran Patil29bdd2b2011-06-20 16:59:25 -07001354 cpu = ntohs(fh->fh_rx_id) & fc_cpu_mask;
Kiran Patil064287e2011-06-20 16:59:20 -07001355 }
Vasu Dev324f6672011-07-27 15:10:39 -07001356
1357 if (cpu >= nr_cpu_ids)
1358 goto err;
1359
Robert Love8976f422009-03-17 11:41:46 -07001360 fps = &per_cpu(fcoe_percpu, cpu);
Robert Love85b4aa42008-12-09 15:10:24 -08001361 spin_lock_bh(&fps->fcoe_rx_list.lock);
Robert Love8976f422009-03-17 11:41:46 -07001362 if (unlikely(!fps->thread)) {
1363 /*
1364 * The targeted CPU is not ready, let's target
1365 * the first CPU now. For non-SMP systems this
1366 * will check the same CPU twice.
1367 */
Robert Love1875f272009-11-03 11:47:50 -08001368 FCOE_NETDEV_DBG(netdev, "CPU is online, but no receive thread "
Robert Loved5488eb2009-06-10 15:30:59 -07001369 "ready for incoming skb- using first online "
1370 "CPU.\n");
Robert Love8976f422009-03-17 11:41:46 -07001371
1372 spin_unlock_bh(&fps->fcoe_rx_list.lock);
Rusty Russell69571772009-12-17 11:43:14 -06001373 cpu = cpumask_first(cpu_online_mask);
Robert Love8976f422009-03-17 11:41:46 -07001374 fps = &per_cpu(fcoe_percpu, cpu);
1375 spin_lock_bh(&fps->fcoe_rx_list.lock);
1376 if (!fps->thread) {
1377 spin_unlock_bh(&fps->fcoe_rx_list.lock);
1378 goto err;
1379 }
1380 }
1381
1382 /*
1383 * We now have a valid CPU that we're targeting for
1384 * this skb. We also have this receive thread locked,
1385 * so we're free to queue skbs into it's queue.
1386 */
Robert Love85b4aa42008-12-09 15:10:24 -08001387
Chris Leech859b7b62009-11-20 14:54:47 -08001388 /* If this is a SCSI-FCP frame, and this is already executing on the
1389 * correct CPU, and the queue for this CPU is empty, then go ahead
1390 * and process the frame directly in the softirq context.
1391 * This lets us process completions without context switching from the
1392 * NET_RX softirq, to our receive processing thread, and then back to
1393 * BLOCK softirq context.
1394 */
1395 if (fh->fh_type == FC_TYPE_FCP &&
1396 cpu == smp_processor_id() &&
1397 skb_queue_empty(&fps->fcoe_rx_list)) {
1398 spin_unlock_bh(&fps->fcoe_rx_list.lock);
1399 fcoe_recv_frame(skb);
1400 } else {
1401 __skb_queue_tail(&fps->fcoe_rx_list, skb);
1402 if (fps->fcoe_rx_list.qlen == 1)
1403 wake_up_process(fps->thread);
1404 spin_unlock_bh(&fps->fcoe_rx_list.lock);
1405 }
Robert Love85b4aa42008-12-09 15:10:24 -08001406
1407 return 0;
1408err:
Joe Eykholtf018b732010-03-12 16:08:55 -08001409 per_cpu_ptr(lport->dev_stats, get_cpu())->ErrorFrames++;
1410 put_cpu();
Robert Love85b4aa42008-12-09 15:10:24 -08001411err2:
1412 kfree_skb(skb);
1413 return -1;
1414}
Robert Love85b4aa42008-12-09 15:10:24 -08001415
1416/**
Bhanu Prakash Gollapudi8597ae82011-01-28 16:05:37 -08001417 * fcoe_alloc_paged_crc_eof() - Allocate a page to be used for the trailer CRC
Robert Love1875f272009-11-03 11:47:50 -08001418 * @skb: The packet to be transmitted
1419 * @tlen: The total length of the trailer
1420 *
Robert Love85b4aa42008-12-09 15:10:24 -08001421 * Returns: 0 for success
Robert Love34f42a02009-02-27 10:55:45 -08001422 */
Bhanu Prakash Gollapudi8597ae82011-01-28 16:05:37 -08001423static int fcoe_alloc_paged_crc_eof(struct sk_buff *skb, int tlen)
Robert Love85b4aa42008-12-09 15:10:24 -08001424{
1425 struct fcoe_percpu_s *fps;
Bhanu Prakash Gollapudi8597ae82011-01-28 16:05:37 -08001426 int rc;
Robert Love85b4aa42008-12-09 15:10:24 -08001427
Robert Love5e5e92d2009-03-17 11:41:35 -07001428 fps = &get_cpu_var(fcoe_percpu);
Bhanu Prakash Gollapudi8597ae82011-01-28 16:05:37 -08001429 rc = fcoe_get_paged_crc_eof(skb, tlen, fps);
Robert Love5e5e92d2009-03-17 11:41:35 -07001430 put_cpu_var(fcoe_percpu);
Robert Love85b4aa42008-12-09 15:10:24 -08001431
Bhanu Prakash Gollapudi8597ae82011-01-28 16:05:37 -08001432 return rc;
Robert Love85b4aa42008-12-09 15:10:24 -08001433}
Robert Love85b4aa42008-12-09 15:10:24 -08001434
1435/**
Robert Love1875f272009-11-03 11:47:50 -08001436 * fcoe_xmit() - Transmit a FCoE frame
1437 * @lport: The local port that the frame is to be transmitted for
1438 * @fp: The frame to be transmitted
Robert Love85b4aa42008-12-09 15:10:24 -08001439 *
Robert Love1875f272009-11-03 11:47:50 -08001440 * Return: 0 for success
Robert Love34f42a02009-02-27 10:55:45 -08001441 */
Robert Love1875f272009-11-03 11:47:50 -08001442int fcoe_xmit(struct fc_lport *lport, struct fc_frame *fp)
Robert Love85b4aa42008-12-09 15:10:24 -08001443{
Vasu Dev4bb6b512009-05-06 10:52:34 -07001444 int wlen;
Robert Love85b4aa42008-12-09 15:10:24 -08001445 u32 crc;
1446 struct ethhdr *eh;
1447 struct fcoe_crc_eof *cp;
1448 struct sk_buff *skb;
1449 struct fcoe_dev_stats *stats;
1450 struct fc_frame_header *fh;
1451 unsigned int hlen; /* header length implies the version */
1452 unsigned int tlen; /* trailer length */
1453 unsigned int elen; /* eth header, may include vlan */
Robert Love1875f272009-11-03 11:47:50 -08001454 struct fcoe_port *port = lport_priv(lport);
Bhanu Prakash Gollapudi8597ae82011-01-28 16:05:37 -08001455 struct fcoe_interface *fcoe = port->priv;
Robert Love85b4aa42008-12-09 15:10:24 -08001456 u8 sof, eof;
1457 struct fcoe_hdr *hp;
1458
1459 WARN_ON((fr_len(fp) % sizeof(u32)) != 0);
1460
Robert Love85b4aa42008-12-09 15:10:24 -08001461 fh = fc_frame_header_get(fp);
Joe Eykholt97c83892009-03-17 11:42:40 -07001462 skb = fp_skb(fp);
1463 wlen = skb->len / FCOE_WORD_TO_BYTE;
1464
Robert Love1875f272009-11-03 11:47:50 -08001465 if (!lport->link_up) {
Dan Carpenter3caf02e2009-04-21 16:27:25 -07001466 kfree_skb(skb);
Joe Eykholt97c83892009-03-17 11:42:40 -07001467 return 0;
Robert Love85b4aa42008-12-09 15:10:24 -08001468 }
1469
Joe Eykholt9860eeb2010-03-12 16:07:52 -08001470 if (unlikely(fh->fh_type == FC_TYPE_ELS) &&
Robert Love1875f272009-11-03 11:47:50 -08001471 fcoe_ctlr_els_send(&fcoe->ctlr, lport, skb))
Joe Eykholt97c83892009-03-17 11:42:40 -07001472 return 0;
1473
Robert Love85b4aa42008-12-09 15:10:24 -08001474 sof = fr_sof(fp);
1475 eof = fr_eof(fp);
1476
Vasu Dev4e57e1c2009-05-06 10:52:46 -07001477 elen = sizeof(struct ethhdr);
Robert Love85b4aa42008-12-09 15:10:24 -08001478 hlen = sizeof(struct fcoe_hdr);
1479 tlen = sizeof(struct fcoe_crc_eof);
1480 wlen = (skb->len - tlen + sizeof(crc)) / FCOE_WORD_TO_BYTE;
1481
1482 /* crc offload */
Robert Love1875f272009-11-03 11:47:50 -08001483 if (likely(lport->crc_offload)) {
Yi Zou39ca9a02009-02-27 14:07:15 -08001484 skb->ip_summed = CHECKSUM_PARTIAL;
Robert Love85b4aa42008-12-09 15:10:24 -08001485 skb->csum_start = skb_headroom(skb);
1486 skb->csum_offset = skb->len;
1487 crc = 0;
1488 } else {
1489 skb->ip_summed = CHECKSUM_NONE;
1490 crc = fcoe_fc_crc(fp);
1491 }
1492
Chris Leech014f5c32009-08-25 13:59:30 -07001493 /* copy port crc and eof to the skb buff */
Robert Love85b4aa42008-12-09 15:10:24 -08001494 if (skb_is_nonlinear(skb)) {
1495 skb_frag_t *frag;
Bhanu Prakash Gollapudi8597ae82011-01-28 16:05:37 -08001496 if (fcoe_alloc_paged_crc_eof(skb, tlen)) {
Roel Kluine9041582009-02-27 10:56:22 -08001497 kfree_skb(skb);
Robert Love85b4aa42008-12-09 15:10:24 -08001498 return -ENOMEM;
1499 }
1500 frag = &skb_shinfo(skb)->frags[skb_shinfo(skb)->nr_frags - 1];
1501 cp = kmap_atomic(frag->page, KM_SKB_DATA_SOFTIRQ)
1502 + frag->page_offset;
1503 } else {
1504 cp = (struct fcoe_crc_eof *)skb_put(skb, tlen);
1505 }
1506
1507 memset(cp, 0, sizeof(*cp));
1508 cp->fcoe_eof = eof;
1509 cp->fcoe_crc32 = cpu_to_le32(~crc);
1510
1511 if (skb_is_nonlinear(skb)) {
1512 kunmap_atomic(cp, KM_SKB_DATA_SOFTIRQ);
1513 cp = NULL;
1514 }
1515
Chris Leech014f5c32009-08-25 13:59:30 -07001516 /* adjust skb network/transport offsets to match mac/fcoe/port */
Robert Love85b4aa42008-12-09 15:10:24 -08001517 skb_push(skb, elen + hlen);
1518 skb_reset_mac_header(skb);
1519 skb_reset_network_header(skb);
1520 skb->mac_len = elen;
Yi Zou211c7382009-02-27 14:06:37 -08001521 skb->protocol = htons(ETH_P_FCOE);
Vasu Devd1483bb2011-09-27 21:38:13 -07001522 if (fcoe->netdev->priv_flags & IFF_802_1Q_VLAN &&
1523 fcoe->realdev->features & NETIF_F_HW_VLAN_TX) {
1524 skb->vlan_tci = VLAN_TAG_PRESENT |
1525 vlan_dev_vlan_id(fcoe->netdev);
1526 skb->dev = fcoe->realdev;
1527 } else
1528 skb->dev = fcoe->netdev;
Robert Love85b4aa42008-12-09 15:10:24 -08001529
1530 /* fill up mac and fcoe headers */
1531 eh = eth_hdr(skb);
1532 eh->h_proto = htons(ETH_P_FCOE);
Joe Eykholtcd229e42010-07-20 15:20:40 -07001533 memcpy(eh->h_dest, fcoe->ctlr.dest_addr, ETH_ALEN);
Chris Leech3fe9a0b2009-08-25 13:59:46 -07001534 if (fcoe->ctlr.map_dest)
Joe Eykholtcd229e42010-07-20 15:20:40 -07001535 memcpy(eh->h_dest + 3, fh->fh_d_id, 3);
Robert Love85b4aa42008-12-09 15:10:24 -08001536
Chris Leech3fe9a0b2009-08-25 13:59:46 -07001537 if (unlikely(fcoe->ctlr.flogi_oxid != FC_XID_UNKNOWN))
1538 memcpy(eh->h_source, fcoe->ctlr.ctl_src_addr, ETH_ALEN);
Robert Love85b4aa42008-12-09 15:10:24 -08001539 else
Chris Leech11b56182009-11-03 11:46:29 -08001540 memcpy(eh->h_source, port->data_src_addr, ETH_ALEN);
Robert Love85b4aa42008-12-09 15:10:24 -08001541
1542 hp = (struct fcoe_hdr *)(eh + 1);
1543 memset(hp, 0, sizeof(*hp));
1544 if (FC_FCOE_VER)
1545 FC_FCOE_ENCAPS_VER(hp, FC_FCOE_VER);
1546 hp->fcoe_sof = sof;
1547
Yi Zou39ca9a02009-02-27 14:07:15 -08001548 /* fcoe lso, mss is in max_payload which is non-zero for FCP data */
Robert Love1875f272009-11-03 11:47:50 -08001549 if (lport->seq_offload && fr_max_payload(fp)) {
Yi Zou39ca9a02009-02-27 14:07:15 -08001550 skb_shinfo(skb)->gso_type = SKB_GSO_FCOE;
1551 skb_shinfo(skb)->gso_size = fr_max_payload(fp);
1552 } else {
1553 skb_shinfo(skb)->gso_type = 0;
1554 skb_shinfo(skb)->gso_size = 0;
1555 }
Robert Love85b4aa42008-12-09 15:10:24 -08001556 /* update tx stats: regardless if LLD fails */
Joe Eykholtf018b732010-03-12 16:08:55 -08001557 stats = per_cpu_ptr(lport->dev_stats, get_cpu());
Robert Love582b45b2009-03-31 15:51:50 -07001558 stats->TxFrames++;
1559 stats->TxWords += wlen;
Joe Eykholtf018b732010-03-12 16:08:55 -08001560 put_cpu();
Robert Love85b4aa42008-12-09 15:10:24 -08001561
1562 /* send down to lld */
Robert Love1875f272009-11-03 11:47:50 -08001563 fr_dev(fp) = lport;
Vasu Dev980f5152011-07-27 15:11:05 -07001564 fcoe_port_send(port, skb);
Robert Love85b4aa42008-12-09 15:10:24 -08001565 return 0;
1566}
Robert Love85b4aa42008-12-09 15:10:24 -08001567
Robert Love34f42a02009-02-27 10:55:45 -08001568/**
Robert Love1875f272009-11-03 11:47:50 -08001569 * fcoe_percpu_flush_done() - Indicate per-CPU queue flush completion
1570 * @skb: The completed skb (argument required by destructor)
Joe Eykholte7a51992009-08-25 14:04:08 -07001571 */
1572static void fcoe_percpu_flush_done(struct sk_buff *skb)
1573{
1574 complete(&fcoe_flush_completion);
1575}
1576
1577/**
Vasu Dev52ee8322011-01-28 16:03:52 -08001578 * fcoe_filter_frames() - filter out bad fcoe frames, i.e. bad CRC
1579 * @lport: The local port the frame was received on
1580 * @fp: The received frame
1581 *
1582 * Return: 0 on passing filtering checks
1583 */
1584static inline int fcoe_filter_frames(struct fc_lport *lport,
1585 struct fc_frame *fp)
1586{
1587 struct fcoe_interface *fcoe;
1588 struct fc_frame_header *fh;
1589 struct sk_buff *skb = (struct sk_buff *)fp;
1590 struct fcoe_dev_stats *stats;
1591
1592 /*
1593 * We only check CRC if no offload is available and if it is
1594 * it's solicited data, in which case, the FCP layer would
1595 * check it during the copy.
1596 */
1597 if (lport->crc_offload && skb->ip_summed == CHECKSUM_UNNECESSARY)
1598 fr_flags(fp) &= ~FCPHF_CRC_UNCHECKED;
1599 else
1600 fr_flags(fp) |= FCPHF_CRC_UNCHECKED;
1601
1602 fh = (struct fc_frame_header *) skb_transport_header(skb);
1603 fh = fc_frame_header_get(fp);
1604 if (fh->fh_r_ctl == FC_RCTL_DD_SOL_DATA && fh->fh_type == FC_TYPE_FCP)
1605 return 0;
1606
Bhanu Prakash Gollapudi8597ae82011-01-28 16:05:37 -08001607 fcoe = ((struct fcoe_port *)lport_priv(lport))->priv;
Vasu Dev52ee8322011-01-28 16:03:52 -08001608 if (is_fip_mode(&fcoe->ctlr) && fc_frame_payload_op(fp) == ELS_LOGO &&
1609 ntoh24(fh->fh_s_id) == FC_FID_FLOGI) {
1610 FCOE_DBG("fcoe: dropping FCoE lport LOGO in fip mode\n");
1611 return -EINVAL;
1612 }
1613
Dan Carpenterf2f96d22011-02-25 15:03:23 -08001614 if (!(fr_flags(fp) & FCPHF_CRC_UNCHECKED) ||
Vasu Dev52ee8322011-01-28 16:03:52 -08001615 le32_to_cpu(fr_crc(fp)) == ~crc32(~0, skb->data, skb->len)) {
1616 fr_flags(fp) &= ~FCPHF_CRC_UNCHECKED;
1617 return 0;
1618 }
1619
1620 stats = per_cpu_ptr(lport->dev_stats, get_cpu());
1621 stats->InvalidCRCCount++;
1622 if (stats->InvalidCRCCount < 5)
1623 printk(KERN_WARNING "fcoe: dropping frame with CRC error\n");
1624 return -EINVAL;
1625}
1626
1627/**
Chris Leech859b7b62009-11-20 14:54:47 -08001628 * fcoe_recv_frame() - process a single received frame
1629 * @skb: frame to process
1630 */
1631static void fcoe_recv_frame(struct sk_buff *skb)
1632{
1633 u32 fr_len;
1634 struct fc_lport *lport;
1635 struct fcoe_rcv_info *fr;
1636 struct fcoe_dev_stats *stats;
Chris Leech859b7b62009-11-20 14:54:47 -08001637 struct fcoe_crc_eof crc_eof;
1638 struct fc_frame *fp;
Chris Leech859b7b62009-11-20 14:54:47 -08001639 struct fcoe_port *port;
1640 struct fcoe_hdr *hp;
1641
1642 fr = fcoe_dev_from_skb(skb);
1643 lport = fr->fr_dev;
1644 if (unlikely(!lport)) {
1645 if (skb->destructor != fcoe_percpu_flush_done)
1646 FCOE_NETDEV_DBG(skb->dev, "NULL lport in skb");
1647 kfree_skb(skb);
1648 return;
1649 }
1650
1651 FCOE_NETDEV_DBG(skb->dev, "skb_info: len:%d data_len:%d "
1652 "head:%p data:%p tail:%p end:%p sum:%d dev:%s",
1653 skb->len, skb->data_len,
1654 skb->head, skb->data, skb_tail_pointer(skb),
1655 skb_end_pointer(skb), skb->csum,
1656 skb->dev ? skb->dev->name : "<NULL>");
1657
Chris Leech859b7b62009-11-20 14:54:47 -08001658 port = lport_priv(lport);
1659 if (skb_is_nonlinear(skb))
1660 skb_linearize(skb); /* not ideal */
Chris Leech859b7b62009-11-20 14:54:47 -08001661
1662 /*
1663 * Frame length checks and setting up the header pointers
1664 * was done in fcoe_rcv already.
1665 */
1666 hp = (struct fcoe_hdr *) skb_network_header(skb);
Chris Leech859b7b62009-11-20 14:54:47 -08001667
Joe Eykholtf018b732010-03-12 16:08:55 -08001668 stats = per_cpu_ptr(lport->dev_stats, get_cpu());
Chris Leech859b7b62009-11-20 14:54:47 -08001669 if (unlikely(FC_FCOE_DECAPS_VER(hp) != FC_FCOE_VER)) {
1670 if (stats->ErrorFrames < 5)
1671 printk(KERN_WARNING "fcoe: FCoE version "
1672 "mismatch: The frame has "
1673 "version %x, but the "
1674 "initiator supports version "
1675 "%x\n", FC_FCOE_DECAPS_VER(hp),
1676 FC_FCOE_VER);
Joe Eykholtf018b732010-03-12 16:08:55 -08001677 goto drop;
Chris Leech859b7b62009-11-20 14:54:47 -08001678 }
1679
1680 skb_pull(skb, sizeof(struct fcoe_hdr));
1681 fr_len = skb->len - sizeof(struct fcoe_crc_eof);
1682
1683 stats->RxFrames++;
1684 stats->RxWords += fr_len / FCOE_WORD_TO_BYTE;
1685
1686 fp = (struct fc_frame *)skb;
1687 fc_frame_init(fp);
1688 fr_dev(fp) = lport;
1689 fr_sof(fp) = hp->fcoe_sof;
1690
1691 /* Copy out the CRC and EOF trailer for access */
Joe Eykholtf018b732010-03-12 16:08:55 -08001692 if (skb_copy_bits(skb, fr_len, &crc_eof, sizeof(crc_eof)))
1693 goto drop;
Chris Leech859b7b62009-11-20 14:54:47 -08001694 fr_eof(fp) = crc_eof.fcoe_eof;
1695 fr_crc(fp) = crc_eof.fcoe_crc32;
Joe Eykholtf018b732010-03-12 16:08:55 -08001696 if (pskb_trim(skb, fr_len))
1697 goto drop;
Chris Leech859b7b62009-11-20 14:54:47 -08001698
Vasu Dev52ee8322011-01-28 16:03:52 -08001699 if (!fcoe_filter_frames(lport, fp)) {
1700 put_cpu();
1701 fc_exch_recv(lport, fp);
1702 return;
Chris Leech859b7b62009-11-20 14:54:47 -08001703 }
Joe Eykholtf018b732010-03-12 16:08:55 -08001704drop:
1705 stats->ErrorFrames++;
1706 put_cpu();
1707 kfree_skb(skb);
Chris Leech859b7b62009-11-20 14:54:47 -08001708}
1709
1710/**
Robert Love1875f272009-11-03 11:47:50 -08001711 * fcoe_percpu_receive_thread() - The per-CPU packet receive thread
1712 * @arg: The per-CPU context
Robert Love85b4aa42008-12-09 15:10:24 -08001713 *
1714 * Return: 0 for success
Robert Love85b4aa42008-12-09 15:10:24 -08001715 */
1716int fcoe_percpu_receive_thread(void *arg)
1717{
1718 struct fcoe_percpu_s *p = arg;
Robert Love85b4aa42008-12-09 15:10:24 -08001719 struct sk_buff *skb;
Robert Love85b4aa42008-12-09 15:10:24 -08001720
Robert Love4469c192009-02-27 10:56:38 -08001721 set_user_nice(current, -20);
Robert Love85b4aa42008-12-09 15:10:24 -08001722
1723 while (!kthread_should_stop()) {
1724
1725 spin_lock_bh(&p->fcoe_rx_list.lock);
1726 while ((skb = __skb_dequeue(&p->fcoe_rx_list)) == NULL) {
1727 set_current_state(TASK_INTERRUPTIBLE);
1728 spin_unlock_bh(&p->fcoe_rx_list.lock);
1729 schedule();
1730 set_current_state(TASK_RUNNING);
1731 if (kthread_should_stop())
1732 return 0;
1733 spin_lock_bh(&p->fcoe_rx_list.lock);
1734 }
1735 spin_unlock_bh(&p->fcoe_rx_list.lock);
Chris Leech859b7b62009-11-20 14:54:47 -08001736 fcoe_recv_frame(skb);
Robert Love85b4aa42008-12-09 15:10:24 -08001737 }
1738 return 0;
1739}
1740
1741/**
Robert Love1875f272009-11-03 11:47:50 -08001742 * fcoe_dev_setup() - Setup the link change notification interface
Robert Love34f42a02009-02-27 10:55:45 -08001743 */
Randy Dunlapb0d428a2009-04-27 21:49:31 -07001744static void fcoe_dev_setup(void)
Robert Love85b4aa42008-12-09 15:10:24 -08001745{
Robert Love85b4aa42008-12-09 15:10:24 -08001746 register_netdevice_notifier(&fcoe_notifier);
1747}
1748
1749/**
Robert Love1875f272009-11-03 11:47:50 -08001750 * fcoe_dev_cleanup() - Cleanup the link change notification interface
Robert Love34f42a02009-02-27 10:55:45 -08001751 */
Robert Love85b4aa42008-12-09 15:10:24 -08001752static void fcoe_dev_cleanup(void)
1753{
1754 unregister_netdevice_notifier(&fcoe_notifier);
1755}
1756
1757/**
Robert Love1875f272009-11-03 11:47:50 -08001758 * fcoe_device_notification() - Handler for net device events
1759 * @notifier: The context of the notification
1760 * @event: The type of event
1761 * @ptr: The net device that the event was on
Robert Love85b4aa42008-12-09 15:10:24 -08001762 *
Robert Love1875f272009-11-03 11:47:50 -08001763 * This function is called by the Ethernet driver in case of link change event.
Robert Love85b4aa42008-12-09 15:10:24 -08001764 *
1765 * Returns: 0 for success
Robert Love34f42a02009-02-27 10:55:45 -08001766 */
Robert Love85b4aa42008-12-09 15:10:24 -08001767static int fcoe_device_notification(struct notifier_block *notifier,
1768 ulong event, void *ptr)
1769{
Robert Love1875f272009-11-03 11:47:50 -08001770 struct fc_lport *lport = NULL;
Vasu Dev1d1b88d2009-07-29 17:05:45 -07001771 struct net_device *netdev = ptr;
Chris Leech014f5c32009-08-25 13:59:30 -07001772 struct fcoe_interface *fcoe;
Chris Leech2e70e242009-08-25 14:00:23 -07001773 struct fcoe_port *port;
Robert Love85b4aa42008-12-09 15:10:24 -08001774 struct fcoe_dev_stats *stats;
Joe Eykholt97c83892009-03-17 11:42:40 -07001775 u32 link_possible = 1;
Robert Love85b4aa42008-12-09 15:10:24 -08001776 u32 mfs;
1777 int rc = NOTIFY_OK;
1778
Chris Leech014f5c32009-08-25 13:59:30 -07001779 list_for_each_entry(fcoe, &fcoe_hostlist, list) {
Chris Leech25024982009-08-25 13:59:35 -07001780 if (fcoe->netdev == netdev) {
Robert Love1875f272009-11-03 11:47:50 -08001781 lport = fcoe->ctlr.lp;
Robert Love85b4aa42008-12-09 15:10:24 -08001782 break;
1783 }
1784 }
Robert Love1875f272009-11-03 11:47:50 -08001785 if (!lport) {
Robert Love85b4aa42008-12-09 15:10:24 -08001786 rc = NOTIFY_DONE;
1787 goto out;
1788 }
1789
Robert Love85b4aa42008-12-09 15:10:24 -08001790 switch (event) {
1791 case NETDEV_DOWN:
1792 case NETDEV_GOING_DOWN:
Joe Eykholt97c83892009-03-17 11:42:40 -07001793 link_possible = 0;
Robert Love85b4aa42008-12-09 15:10:24 -08001794 break;
1795 case NETDEV_UP:
1796 case NETDEV_CHANGE:
Robert Love85b4aa42008-12-09 15:10:24 -08001797 break;
1798 case NETDEV_CHANGEMTU:
Yi Zou7221d7e2009-10-21 16:27:52 -07001799 if (netdev->features & NETIF_F_FCOE_MTU)
1800 break;
Vasu Dev1d1b88d2009-07-29 17:05:45 -07001801 mfs = netdev->mtu - (sizeof(struct fcoe_hdr) +
1802 sizeof(struct fcoe_crc_eof));
Robert Love85b4aa42008-12-09 15:10:24 -08001803 if (mfs >= FC_MIN_MAX_FRAME)
Robert Love1875f272009-11-03 11:47:50 -08001804 fc_set_mfs(lport, mfs);
Robert Love85b4aa42008-12-09 15:10:24 -08001805 break;
1806 case NETDEV_REGISTER:
1807 break;
Chris Leech2e70e242009-08-25 14:00:23 -07001808 case NETDEV_UNREGISTER:
1809 list_del(&fcoe->list);
1810 port = lport_priv(fcoe->ctlr.lp);
Tejun Heo2ca32b42011-01-28 16:05:32 -08001811 queue_work(fcoe_wq, &port->destroy_work);
Chris Leech2e70e242009-08-25 14:00:23 -07001812 goto out;
1813 break;
Yi Zou54a5b212010-07-20 15:21:17 -07001814 case NETDEV_FEAT_CHANGE:
1815 fcoe_netdev_features_change(lport, netdev);
1816 break;
Robert Love85b4aa42008-12-09 15:10:24 -08001817 default:
Vasu Dev1d1b88d2009-07-29 17:05:45 -07001818 FCOE_NETDEV_DBG(netdev, "Unknown event %ld "
Robert Loved5488eb2009-06-10 15:30:59 -07001819 "from netdev netlink\n", event);
Robert Love85b4aa42008-12-09 15:10:24 -08001820 }
Robert Love5e4f8fe2010-05-07 15:18:35 -07001821
1822 fcoe_link_speed_update(lport);
1823
Robert Love1875f272009-11-03 11:47:50 -08001824 if (link_possible && !fcoe_link_ok(lport))
Chris Leech3fe9a0b2009-08-25 13:59:46 -07001825 fcoe_ctlr_link_up(&fcoe->ctlr);
1826 else if (fcoe_ctlr_link_down(&fcoe->ctlr)) {
Joe Eykholtf018b732010-03-12 16:08:55 -08001827 stats = per_cpu_ptr(lport->dev_stats, get_cpu());
Joe Eykholt97c83892009-03-17 11:42:40 -07001828 stats->LinkFailureCount++;
Joe Eykholtf018b732010-03-12 16:08:55 -08001829 put_cpu();
Robert Love1875f272009-11-03 11:47:50 -08001830 fcoe_clean_pending_queue(lport);
Robert Love85b4aa42008-12-09 15:10:24 -08001831 }
1832out:
1833 return rc;
1834}
1835
1836/**
Vasu Dev55a66d32009-12-10 09:59:31 -08001837 * fcoe_disable() - Disables a FCoE interface
Yi Zou78a58242011-01-28 16:05:16 -08001838 * @netdev : The net_device object the Ethernet interface to create on
Vasu Dev55a66d32009-12-10 09:59:31 -08001839 *
Yi Zou78a58242011-01-28 16:05:16 -08001840 * Called from fcoe transport.
Vasu Dev55a66d32009-12-10 09:59:31 -08001841 *
1842 * Returns: 0 for success
1843 */
Yi Zou78a58242011-01-28 16:05:16 -08001844static int fcoe_disable(struct net_device *netdev)
Vasu Dev55a66d32009-12-10 09:59:31 -08001845{
1846 struct fcoe_interface *fcoe;
Vasu Dev55a66d32009-12-10 09:59:31 -08001847 int rc = 0;
1848
1849 mutex_lock(&fcoe_config_mutex);
Vasu Dev55a66d32009-12-10 09:59:31 -08001850
Robert Loveee5df622011-04-01 16:05:59 -07001851 rtnl_lock();
Vasu Dev55a66d32009-12-10 09:59:31 -08001852 fcoe = fcoe_hostlist_lookup_port(netdev);
1853 rtnl_unlock();
1854
Chris Leech9ee50e42010-04-09 14:22:23 -07001855 if (fcoe) {
Chris Leech9ee50e42010-04-09 14:22:23 -07001856 fcoe_ctlr_link_down(&fcoe->ctlr);
Vasu Dev9d4cbc02010-07-20 15:19:26 -07001857 fcoe_clean_pending_queue(fcoe->ctlr.lp);
Chris Leech9ee50e42010-04-09 14:22:23 -07001858 } else
Vasu Dev55a66d32009-12-10 09:59:31 -08001859 rc = -ENODEV;
1860
Vasu Dev55a66d32009-12-10 09:59:31 -08001861 mutex_unlock(&fcoe_config_mutex);
1862 return rc;
1863}
1864
1865/**
1866 * fcoe_enable() - Enables a FCoE interface
Yi Zou78a58242011-01-28 16:05:16 -08001867 * @netdev : The net_device object the Ethernet interface to create on
Vasu Dev55a66d32009-12-10 09:59:31 -08001868 *
Yi Zou78a58242011-01-28 16:05:16 -08001869 * Called from fcoe transport.
Vasu Dev55a66d32009-12-10 09:59:31 -08001870 *
1871 * Returns: 0 for success
1872 */
Yi Zou78a58242011-01-28 16:05:16 -08001873static int fcoe_enable(struct net_device *netdev)
Vasu Dev55a66d32009-12-10 09:59:31 -08001874{
1875 struct fcoe_interface *fcoe;
Vasu Dev55a66d32009-12-10 09:59:31 -08001876 int rc = 0;
1877
1878 mutex_lock(&fcoe_config_mutex);
Robert Loveee5df622011-04-01 16:05:59 -07001879 rtnl_lock();
Vasu Dev55a66d32009-12-10 09:59:31 -08001880 fcoe = fcoe_hostlist_lookup_port(netdev);
1881 rtnl_unlock();
1882
Vasu Dev9d4cbc02010-07-20 15:19:26 -07001883 if (!fcoe)
Vasu Dev55a66d32009-12-10 09:59:31 -08001884 rc = -ENODEV;
Vasu Dev9d4cbc02010-07-20 15:19:26 -07001885 else if (!fcoe_link_ok(fcoe->ctlr.lp))
1886 fcoe_ctlr_link_up(&fcoe->ctlr);
Vasu Dev55a66d32009-12-10 09:59:31 -08001887
Vasu Dev55a66d32009-12-10 09:59:31 -08001888 mutex_unlock(&fcoe_config_mutex);
1889 return rc;
1890}
1891
1892/**
Robert Love1875f272009-11-03 11:47:50 -08001893 * fcoe_destroy() - Destroy a FCoE interface
Yi Zou78a58242011-01-28 16:05:16 -08001894 * @netdev : The net_device object the Ethernet interface to create on
Robert Love1875f272009-11-03 11:47:50 -08001895 *
Yi Zou78a58242011-01-28 16:05:16 -08001896 * Called from fcoe transport
Robert Love85b4aa42008-12-09 15:10:24 -08001897 *
1898 * Returns: 0 for success
Robert Love34f42a02009-02-27 10:55:45 -08001899 */
Yi Zou78a58242011-01-28 16:05:16 -08001900static int fcoe_destroy(struct net_device *netdev)
Robert Love85b4aa42008-12-09 15:10:24 -08001901{
Chris Leech030f4e02009-08-25 14:00:02 -07001902 struct fcoe_interface *fcoe;
Vasu Devf04ca1b2011-04-01 16:06:45 -07001903 struct fc_lport *lport;
Neerav Parikhb2085a42011-06-20 16:59:51 -07001904 struct fcoe_port *port;
Mike Christie8eca355f2009-10-21 16:27:44 -07001905 int rc = 0;
Robert Love85b4aa42008-12-09 15:10:24 -08001906
Chris Leechdfc1d0f2009-08-25 14:00:13 -07001907 mutex_lock(&fcoe_config_mutex);
Robert Loveee5df622011-04-01 16:05:59 -07001908 rtnl_lock();
Chris Leech2e70e242009-08-25 14:00:23 -07001909 fcoe = fcoe_hostlist_lookup_port(netdev);
1910 if (!fcoe) {
Robert Love85b4aa42008-12-09 15:10:24 -08001911 rc = -ENODEV;
Yi Zou78a58242011-01-28 16:05:16 -08001912 goto out_nodev;
Robert Love85b4aa42008-12-09 15:10:24 -08001913 }
Vasu Devf04ca1b2011-04-01 16:06:45 -07001914 lport = fcoe->ctlr.lp;
Neerav Parikhb2085a42011-06-20 16:59:51 -07001915 port = lport_priv(lport);
Yi Zou54a5b212010-07-20 15:21:17 -07001916 list_del(&fcoe->list);
Neerav Parikhb2085a42011-06-20 16:59:51 -07001917 queue_work(fcoe_wq, &port->destroy_work);
Robert Love85b4aa42008-12-09 15:10:24 -08001918out_nodev:
Neerav Parikhb2085a42011-06-20 16:59:51 -07001919 rtnl_unlock();
Chris Leechdfc1d0f2009-08-25 14:00:13 -07001920 mutex_unlock(&fcoe_config_mutex);
Robert Love85b4aa42008-12-09 15:10:24 -08001921 return rc;
1922}
1923
Robert Love1875f272009-11-03 11:47:50 -08001924/**
1925 * fcoe_destroy_work() - Destroy a FCoE port in a deferred work context
1926 * @work: Handle to the FCoE port to be destroyed
1927 */
Chris Leech2e70e242009-08-25 14:00:23 -07001928static void fcoe_destroy_work(struct work_struct *work)
1929{
1930 struct fcoe_port *port;
Neerav Parikhb2085a42011-06-20 16:59:51 -07001931 struct fcoe_interface *fcoe;
1932 int npiv = 0;
Chris Leech2e70e242009-08-25 14:00:23 -07001933
1934 port = container_of(work, struct fcoe_port, destroy_work);
1935 mutex_lock(&fcoe_config_mutex);
Neerav Parikhb2085a42011-06-20 16:59:51 -07001936
1937 /* set if this is an NPIV port */
1938 npiv = port->lport->vport ? 1 : 0;
1939
1940 fcoe = port->priv;
Chris Leech2e70e242009-08-25 14:00:23 -07001941 fcoe_if_destroy(port->lport);
Neerav Parikhb2085a42011-06-20 16:59:51 -07001942
1943 /* Do not tear down the fcoe interface for NPIV port */
1944 if (!npiv) {
1945 rtnl_lock();
1946 fcoe_interface_cleanup(fcoe);
1947 rtnl_unlock();
1948 }
1949
Chris Leech2e70e242009-08-25 14:00:23 -07001950 mutex_unlock(&fcoe_config_mutex);
1951}
1952
Robert Love85b4aa42008-12-09 15:10:24 -08001953/**
Yi Zou78a58242011-01-28 16:05:16 -08001954 * fcoe_match() - Check if the FCoE is supported on the given netdevice
1955 * @netdev : The net_device object the Ethernet interface to create on
Robert Love1875f272009-11-03 11:47:50 -08001956 *
Yi Zou78a58242011-01-28 16:05:16 -08001957 * Called from fcoe transport.
1958 *
1959 * Returns: always returns true as this is the default FCoE transport,
1960 * i.e., support all netdevs.
1961 */
1962static bool fcoe_match(struct net_device *netdev)
1963{
1964 return true;
1965}
1966
1967/**
1968 * fcoe_create() - Create a fcoe interface
1969 * @netdev : The net_device object the Ethernet interface to create on
1970 * @fip_mode: The FIP mode for this creation
1971 *
1972 * Called from fcoe transport
Robert Love85b4aa42008-12-09 15:10:24 -08001973 *
1974 * Returns: 0 for success
Robert Love34f42a02009-02-27 10:55:45 -08001975 */
Yi Zou78a58242011-01-28 16:05:16 -08001976static int fcoe_create(struct net_device *netdev, enum fip_state fip_mode)
Robert Love85b4aa42008-12-09 15:10:24 -08001977{
Neerav Parikhb2085a42011-06-20 16:59:51 -07001978 int rc = 0;
Chris Leech030f4e02009-08-25 14:00:02 -07001979 struct fcoe_interface *fcoe;
Chris Leechaf7f85d2009-08-25 13:59:24 -07001980 struct fc_lport *lport;
Robert Love85b4aa42008-12-09 15:10:24 -08001981
Chris Leechdfc1d0f2009-08-25 14:00:13 -07001982 mutex_lock(&fcoe_config_mutex);
Robert Loveee5df622011-04-01 16:05:59 -07001983 rtnl_lock();
Vasu Dev34ce27b2010-05-07 15:18:46 -07001984
Robert Love85b4aa42008-12-09 15:10:24 -08001985 /* look for existing lport */
1986 if (fcoe_hostlist_lookup(netdev)) {
1987 rc = -EEXIST;
Yi Zou78a58242011-01-28 16:05:16 -08001988 goto out_nodev;
Robert Love85b4aa42008-12-09 15:10:24 -08001989 }
Robert Love85b4aa42008-12-09 15:10:24 -08001990
Joe Eykholt1dd454d2010-07-20 15:20:46 -07001991 fcoe = fcoe_interface_create(netdev, fip_mode);
Robert Love7287fb92011-01-28 16:03:47 -08001992 if (IS_ERR(fcoe)) {
1993 rc = PTR_ERR(fcoe);
Yi Zou78a58242011-01-28 16:05:16 -08001994 goto out_nodev;
Chris Leech030f4e02009-08-25 14:00:02 -07001995 }
1996
Chris Leech9a057532009-11-03 11:46:40 -08001997 lport = fcoe_if_create(fcoe, &netdev->dev, 0);
Chris Leechaf7f85d2009-08-25 13:59:24 -07001998 if (IS_ERR(lport)) {
Robert Loved5488eb2009-06-10 15:30:59 -07001999 printk(KERN_ERR "fcoe: Failed to create interface (%s)\n",
Robert Love85b4aa42008-12-09 15:10:24 -08002000 netdev->name);
Robert Love85b4aa42008-12-09 15:10:24 -08002001 rc = -EIO;
Chris Leech2e70e242009-08-25 14:00:23 -07002002 fcoe_interface_cleanup(fcoe);
Neerav Parikhb2085a42011-06-20 16:59:51 -07002003 goto out_nodev;
Robert Love85b4aa42008-12-09 15:10:24 -08002004 }
Chris Leech030f4e02009-08-25 14:00:02 -07002005
Chris Leech54b649f2009-08-25 14:00:07 -07002006 /* Make this the "master" N_Port */
2007 fcoe->ctlr.lp = lport;
Chris Leech030f4e02009-08-25 14:00:02 -07002008
Chris Leechc863df32009-08-25 14:00:18 -07002009 /* add to lports list */
2010 fcoe_hostlist_add(lport);
2011
Chris Leech54b649f2009-08-25 14:00:07 -07002012 /* start FIP Discovery and FLOGI */
2013 lport->boot_time = jiffies;
2014 fc_fabric_login(lport);
2015 if (!fcoe_link_ok(lport))
2016 fcoe_ctlr_link_up(&fcoe->ctlr);
2017
Robert Love85b4aa42008-12-09 15:10:24 -08002018out_nodev:
Vasu Dev34ce27b2010-05-07 15:18:46 -07002019 rtnl_unlock();
Chris Leechdfc1d0f2009-08-25 14:00:13 -07002020 mutex_unlock(&fcoe_config_mutex);
Robert Love85b4aa42008-12-09 15:10:24 -08002021 return rc;
2022}
2023
Robert Love34f42a02009-02-27 10:55:45 -08002024/**
Robert Love5e4f8fe2010-05-07 15:18:35 -07002025 * fcoe_link_speed_update() - Update the supported and actual link speeds
2026 * @lport: The local port to update speeds for
Robert Love85b4aa42008-12-09 15:10:24 -08002027 *
Robert Love5e4f8fe2010-05-07 15:18:35 -07002028 * Returns: 0 if the ethtool query was successful
2029 * -1 if the ethtool query failed
Robert Love85b4aa42008-12-09 15:10:24 -08002030 */
Robert Love5e4f8fe2010-05-07 15:18:35 -07002031int fcoe_link_speed_update(struct fc_lport *lport)
Robert Love85b4aa42008-12-09 15:10:24 -08002032{
Bhanu Prakash Gollapudi8597ae82011-01-28 16:05:37 -08002033 struct net_device *netdev = fcoe_netdev(lport);
David Decotigny8ae6daca2011-04-27 18:32:38 +00002034 struct ethtool_cmd ecmd;
Robert Love85b4aa42008-12-09 15:10:24 -08002035
Robert Love5e4f8fe2010-05-07 15:18:35 -07002036 if (!dev_ethtool_get_settings(netdev, &ecmd)) {
Robert Love1875f272009-11-03 11:47:50 -08002037 lport->link_supported_speeds &=
Yi Zou2f718d62009-07-29 17:04:01 -07002038 ~(FC_PORTSPEED_1GBIT | FC_PORTSPEED_10GBIT);
2039 if (ecmd.supported & (SUPPORTED_1000baseT_Half |
2040 SUPPORTED_1000baseT_Full))
Robert Love1875f272009-11-03 11:47:50 -08002041 lport->link_supported_speeds |= FC_PORTSPEED_1GBIT;
Yi Zou2f718d62009-07-29 17:04:01 -07002042 if (ecmd.supported & SUPPORTED_10000baseT_Full)
Robert Love1875f272009-11-03 11:47:50 -08002043 lport->link_supported_speeds |=
Yi Zou2f718d62009-07-29 17:04:01 -07002044 FC_PORTSPEED_10GBIT;
David Decotigny8ae6daca2011-04-27 18:32:38 +00002045 switch (ethtool_cmd_speed(&ecmd)) {
2046 case SPEED_1000:
Robert Love1875f272009-11-03 11:47:50 -08002047 lport->link_speed = FC_PORTSPEED_1GBIT;
David Decotigny8ae6daca2011-04-27 18:32:38 +00002048 break;
2049 case SPEED_10000:
Robert Love1875f272009-11-03 11:47:50 -08002050 lport->link_speed = FC_PORTSPEED_10GBIT;
David Decotigny8ae6daca2011-04-27 18:32:38 +00002051 break;
2052 }
Yi Zou2f718d62009-07-29 17:04:01 -07002053 return 0;
2054 }
2055 return -1;
Robert Love85b4aa42008-12-09 15:10:24 -08002056}
Robert Love85b4aa42008-12-09 15:10:24 -08002057
Robert Love34f42a02009-02-27 10:55:45 -08002058/**
Robert Love5e4f8fe2010-05-07 15:18:35 -07002059 * fcoe_link_ok() - Check if the link is OK for a local port
2060 * @lport: The local port to check link on
2061 *
2062 * Returns: 0 if link is UP and OK, -1 if not
2063 *
2064 */
2065int fcoe_link_ok(struct fc_lport *lport)
2066{
Bhanu Prakash Gollapudi8597ae82011-01-28 16:05:37 -08002067 struct net_device *netdev = fcoe_netdev(lport);
Robert Love5e4f8fe2010-05-07 15:18:35 -07002068
2069 if (netif_oper_up(netdev))
2070 return 0;
2071 return -1;
2072}
2073
2074/**
Robert Love1875f272009-11-03 11:47:50 -08002075 * fcoe_percpu_clean() - Clear all pending skbs for an local port
2076 * @lport: The local port whose skbs are to be cleared
Joe Eykholte7a51992009-08-25 14:04:08 -07002077 *
2078 * Must be called with fcoe_create_mutex held to single-thread completion.
2079 *
2080 * This flushes the pending skbs by adding a new skb to each queue and
2081 * waiting until they are all freed. This assures us that not only are
2082 * there no packets that will be handled by the lport, but also that any
2083 * threads already handling packet have returned.
Robert Love85b4aa42008-12-09 15:10:24 -08002084 */
Robert Love1875f272009-11-03 11:47:50 -08002085void fcoe_percpu_clean(struct fc_lport *lport)
Robert Love85b4aa42008-12-09 15:10:24 -08002086{
Robert Love85b4aa42008-12-09 15:10:24 -08002087 struct fcoe_percpu_s *pp;
2088 struct fcoe_rcv_info *fr;
2089 struct sk_buff_head *list;
2090 struct sk_buff *skb, *next;
2091 struct sk_buff *head;
Robert Love5e5e92d2009-03-17 11:41:35 -07002092 unsigned int cpu;
Robert Love85b4aa42008-12-09 15:10:24 -08002093
Robert Love5e5e92d2009-03-17 11:41:35 -07002094 for_each_possible_cpu(cpu) {
2095 pp = &per_cpu(fcoe_percpu, cpu);
2096 spin_lock_bh(&pp->fcoe_rx_list.lock);
2097 list = &pp->fcoe_rx_list;
2098 head = list->next;
2099 for (skb = head; skb != (struct sk_buff *)list;
2100 skb = next) {
2101 next = skb->next;
2102 fr = fcoe_dev_from_skb(skb);
Robert Love1875f272009-11-03 11:47:50 -08002103 if (fr->fr_dev == lport) {
Robert Love5e5e92d2009-03-17 11:41:35 -07002104 __skb_unlink(skb, list);
2105 kfree_skb(skb);
Robert Love85b4aa42008-12-09 15:10:24 -08002106 }
Robert Love85b4aa42008-12-09 15:10:24 -08002107 }
Joe Eykholte7a51992009-08-25 14:04:08 -07002108
2109 if (!pp->thread || !cpu_online(cpu)) {
2110 spin_unlock_bh(&pp->fcoe_rx_list.lock);
2111 continue;
2112 }
2113
2114 skb = dev_alloc_skb(0);
2115 if (!skb) {
2116 spin_unlock_bh(&pp->fcoe_rx_list.lock);
2117 continue;
2118 }
2119 skb->destructor = fcoe_percpu_flush_done;
2120
2121 __skb_queue_tail(&pp->fcoe_rx_list, skb);
2122 if (pp->fcoe_rx_list.qlen == 1)
2123 wake_up_process(pp->thread);
Robert Love5e5e92d2009-03-17 11:41:35 -07002124 spin_unlock_bh(&pp->fcoe_rx_list.lock);
Joe Eykholte7a51992009-08-25 14:04:08 -07002125
2126 wait_for_completion(&fcoe_flush_completion);
Robert Love85b4aa42008-12-09 15:10:24 -08002127 }
2128}
Robert Love85b4aa42008-12-09 15:10:24 -08002129
2130/**
Robert Love1875f272009-11-03 11:47:50 -08002131 * fcoe_reset() - Reset a local port
2132 * @shost: The SCSI host associated with the local port to be reset
Robert Love85b4aa42008-12-09 15:10:24 -08002133 *
Robert Love1875f272009-11-03 11:47:50 -08002134 * Returns: Always 0 (return value required by FC transport template)
Robert Love85b4aa42008-12-09 15:10:24 -08002135 */
2136int fcoe_reset(struct Scsi_Host *shost)
2137{
2138 struct fc_lport *lport = shost_priv(shost);
Vasu Devd2f80952011-02-25 15:03:28 -08002139 struct fcoe_port *port = lport_priv(lport);
2140 struct fcoe_interface *fcoe = port->priv;
2141
2142 fcoe_ctlr_link_down(&fcoe->ctlr);
2143 fcoe_clean_pending_queue(fcoe->ctlr.lp);
2144 if (!fcoe_link_ok(fcoe->ctlr.lp))
2145 fcoe_ctlr_link_up(&fcoe->ctlr);
Robert Love85b4aa42008-12-09 15:10:24 -08002146 return 0;
2147}
Robert Love85b4aa42008-12-09 15:10:24 -08002148
Robert Love34f42a02009-02-27 10:55:45 -08002149/**
Robert Love1875f272009-11-03 11:47:50 -08002150 * fcoe_hostlist_lookup_port() - Find the FCoE interface associated with a net device
2151 * @netdev: The net device used as a key
Robert Love85b4aa42008-12-09 15:10:24 -08002152 *
Robert Love1875f272009-11-03 11:47:50 -08002153 * Locking: Must be called with the RNL mutex held.
2154 *
2155 * Returns: NULL or the FCoE interface
Robert Love85b4aa42008-12-09 15:10:24 -08002156 */
Chris Leech014f5c32009-08-25 13:59:30 -07002157static struct fcoe_interface *
Robert Love1875f272009-11-03 11:47:50 -08002158fcoe_hostlist_lookup_port(const struct net_device *netdev)
Robert Love85b4aa42008-12-09 15:10:24 -08002159{
Chris Leech014f5c32009-08-25 13:59:30 -07002160 struct fcoe_interface *fcoe;
Robert Love85b4aa42008-12-09 15:10:24 -08002161
Chris Leech014f5c32009-08-25 13:59:30 -07002162 list_for_each_entry(fcoe, &fcoe_hostlist, list) {
Robert Love1875f272009-11-03 11:47:50 -08002163 if (fcoe->netdev == netdev)
Chris Leech014f5c32009-08-25 13:59:30 -07002164 return fcoe;
Robert Love85b4aa42008-12-09 15:10:24 -08002165 }
Robert Love85b4aa42008-12-09 15:10:24 -08002166 return NULL;
2167}
2168
Robert Love34f42a02009-02-27 10:55:45 -08002169/**
Robert Love1875f272009-11-03 11:47:50 -08002170 * fcoe_hostlist_lookup() - Find the local port associated with a
2171 * given net device
2172 * @netdev: The netdevice used as a key
Robert Love85b4aa42008-12-09 15:10:24 -08002173 *
Robert Love1875f272009-11-03 11:47:50 -08002174 * Locking: Must be called with the RTNL mutex held
2175 *
2176 * Returns: NULL or the local port
Robert Love85b4aa42008-12-09 15:10:24 -08002177 */
Chris Leech090eb6c2009-08-25 14:00:28 -07002178static struct fc_lport *fcoe_hostlist_lookup(const struct net_device *netdev)
Robert Love85b4aa42008-12-09 15:10:24 -08002179{
Chris Leech014f5c32009-08-25 13:59:30 -07002180 struct fcoe_interface *fcoe;
Robert Love85b4aa42008-12-09 15:10:24 -08002181
Chris Leech014f5c32009-08-25 13:59:30 -07002182 fcoe = fcoe_hostlist_lookup_port(netdev);
Chris Leech3fe9a0b2009-08-25 13:59:46 -07002183 return (fcoe) ? fcoe->ctlr.lp : NULL;
Robert Love85b4aa42008-12-09 15:10:24 -08002184}
Robert Love85b4aa42008-12-09 15:10:24 -08002185
Robert Love34f42a02009-02-27 10:55:45 -08002186/**
Robert Love1875f272009-11-03 11:47:50 -08002187 * fcoe_hostlist_add() - Add the FCoE interface identified by a local
2188 * port to the hostlist
2189 * @lport: The local port that identifies the FCoE interface to be added
2190 *
2191 * Locking: must be called with the RTNL mutex held
Robert Love85b4aa42008-12-09 15:10:24 -08002192 *
2193 * Returns: 0 for success
2194 */
Chris Leech090eb6c2009-08-25 14:00:28 -07002195static int fcoe_hostlist_add(const struct fc_lport *lport)
Robert Love85b4aa42008-12-09 15:10:24 -08002196{
Chris Leech014f5c32009-08-25 13:59:30 -07002197 struct fcoe_interface *fcoe;
2198 struct fcoe_port *port;
Robert Love85b4aa42008-12-09 15:10:24 -08002199
Chris Leech014f5c32009-08-25 13:59:30 -07002200 fcoe = fcoe_hostlist_lookup_port(fcoe_netdev(lport));
2201 if (!fcoe) {
2202 port = lport_priv(lport);
Bhanu Prakash Gollapudi8597ae82011-01-28 16:05:37 -08002203 fcoe = port->priv;
Chris Leech014f5c32009-08-25 13:59:30 -07002204 list_add_tail(&fcoe->list, &fcoe_hostlist);
Robert Love85b4aa42008-12-09 15:10:24 -08002205 }
2206 return 0;
2207}
Robert Love85b4aa42008-12-09 15:10:24 -08002208
Yi Zou78a58242011-01-28 16:05:16 -08002209
2210static struct fcoe_transport fcoe_sw_transport = {
2211 .name = {FCOE_TRANSPORT_DEFAULT},
2212 .attached = false,
2213 .list = LIST_HEAD_INIT(fcoe_sw_transport.list),
2214 .match = fcoe_match,
2215 .create = fcoe_create,
2216 .destroy = fcoe_destroy,
2217 .enable = fcoe_enable,
2218 .disable = fcoe_disable,
2219};
2220
Robert Love34f42a02009-02-27 10:55:45 -08002221/**
Robert Love1875f272009-11-03 11:47:50 -08002222 * fcoe_init() - Initialize fcoe.ko
Robert Love85b4aa42008-12-09 15:10:24 -08002223 *
Robert Love1875f272009-11-03 11:47:50 -08002224 * Returns: 0 on success, or a negative value on failure
Robert Love34f42a02009-02-27 10:55:45 -08002225 */
Robert Love85b4aa42008-12-09 15:10:24 -08002226static int __init fcoe_init(void)
2227{
Robert Love1875f272009-11-03 11:47:50 -08002228 struct fcoe_percpu_s *p;
Robert Love38eccab2009-03-17 11:41:30 -07002229 unsigned int cpu;
Robert Love8976f422009-03-17 11:41:46 -07002230 int rc = 0;
Robert Love85b4aa42008-12-09 15:10:24 -08002231
Tejun Heo2ca32b42011-01-28 16:05:32 -08002232 fcoe_wq = alloc_workqueue("fcoe", 0, 0);
2233 if (!fcoe_wq)
2234 return -ENOMEM;
2235
Yi Zou78a58242011-01-28 16:05:16 -08002236 /* register as a fcoe transport */
2237 rc = fcoe_transport_attach(&fcoe_sw_transport);
2238 if (rc) {
2239 printk(KERN_ERR "failed to register an fcoe transport, check "
2240 "if libfcoe is loaded\n");
2241 return rc;
2242 }
2243
Chris Leechdfc1d0f2009-08-25 14:00:13 -07002244 mutex_lock(&fcoe_config_mutex);
2245
Robert Love38eccab2009-03-17 11:41:30 -07002246 for_each_possible_cpu(cpu) {
Robert Love5e5e92d2009-03-17 11:41:35 -07002247 p = &per_cpu(fcoe_percpu, cpu);
Robert Love38eccab2009-03-17 11:41:30 -07002248 skb_queue_head_init(&p->fcoe_rx_list);
2249 }
2250
Robert Love8976f422009-03-17 11:41:46 -07002251 for_each_online_cpu(cpu)
2252 fcoe_percpu_thread_create(cpu);
Robert Love85b4aa42008-12-09 15:10:24 -08002253
Robert Love8976f422009-03-17 11:41:46 -07002254 /* Initialize per CPU interrupt thread */
2255 rc = register_hotcpu_notifier(&fcoe_cpu_notifier);
2256 if (rc)
2257 goto out_free;
Robert Love85b4aa42008-12-09 15:10:24 -08002258
Robert Love8976f422009-03-17 11:41:46 -07002259 /* Setup link change notification */
Robert Love85b4aa42008-12-09 15:10:24 -08002260 fcoe_dev_setup();
2261
Chris Leech5892c322009-08-25 13:59:14 -07002262 rc = fcoe_if_init();
2263 if (rc)
2264 goto out_free;
Robert Love85b4aa42008-12-09 15:10:24 -08002265
Chris Leechdfc1d0f2009-08-25 14:00:13 -07002266 mutex_unlock(&fcoe_config_mutex);
Robert Love85b4aa42008-12-09 15:10:24 -08002267 return 0;
Robert Love8976f422009-03-17 11:41:46 -07002268
2269out_free:
2270 for_each_online_cpu(cpu) {
2271 fcoe_percpu_thread_destroy(cpu);
2272 }
Chris Leechdfc1d0f2009-08-25 14:00:13 -07002273 mutex_unlock(&fcoe_config_mutex);
Tejun Heo2ca32b42011-01-28 16:05:32 -08002274 destroy_workqueue(fcoe_wq);
Robert Love8976f422009-03-17 11:41:46 -07002275 return rc;
Robert Love85b4aa42008-12-09 15:10:24 -08002276}
2277module_init(fcoe_init);
2278
2279/**
Robert Love1875f272009-11-03 11:47:50 -08002280 * fcoe_exit() - Clean up fcoe.ko
Robert Love85b4aa42008-12-09 15:10:24 -08002281 *
Robert Love1875f272009-11-03 11:47:50 -08002282 * Returns: 0 on success or a negative value on failure
Robert Love34f42a02009-02-27 10:55:45 -08002283 */
Robert Love85b4aa42008-12-09 15:10:24 -08002284static void __exit fcoe_exit(void)
2285{
Chris Leech014f5c32009-08-25 13:59:30 -07002286 struct fcoe_interface *fcoe, *tmp;
Chris Leech2e70e242009-08-25 14:00:23 -07002287 struct fcoe_port *port;
Robert Love1875f272009-11-03 11:47:50 -08002288 unsigned int cpu;
Robert Love85b4aa42008-12-09 15:10:24 -08002289
Chris Leechdfc1d0f2009-08-25 14:00:13 -07002290 mutex_lock(&fcoe_config_mutex);
2291
Robert Love85b4aa42008-12-09 15:10:24 -08002292 fcoe_dev_cleanup();
2293
Vasu Dev5919a592009-03-27 09:03:29 -07002294 /* releases the associated fcoe hosts */
Chris Leech090eb6c2009-08-25 14:00:28 -07002295 rtnl_lock();
2296 list_for_each_entry_safe(fcoe, tmp, &fcoe_hostlist, list) {
Chris Leechc863df32009-08-25 14:00:18 -07002297 list_del(&fcoe->list);
Chris Leech2e70e242009-08-25 14:00:23 -07002298 port = lport_priv(fcoe->ctlr.lp);
Tejun Heo2ca32b42011-01-28 16:05:32 -08002299 queue_work(fcoe_wq, &port->destroy_work);
Chris Leechc863df32009-08-25 14:00:18 -07002300 }
Chris Leech090eb6c2009-08-25 14:00:28 -07002301 rtnl_unlock();
Robert Love85b4aa42008-12-09 15:10:24 -08002302
Robert Love8976f422009-03-17 11:41:46 -07002303 unregister_hotcpu_notifier(&fcoe_cpu_notifier);
2304
Chris Leech014f5c32009-08-25 13:59:30 -07002305 for_each_online_cpu(cpu)
Robert Love8976f422009-03-17 11:41:46 -07002306 fcoe_percpu_thread_destroy(cpu);
Robert Love85b4aa42008-12-09 15:10:24 -08002307
Chris Leechdfc1d0f2009-08-25 14:00:13 -07002308 mutex_unlock(&fcoe_config_mutex);
Chris Leech2e70e242009-08-25 14:00:23 -07002309
Tejun Heo2ca32b42011-01-28 16:05:32 -08002310 /*
2311 * destroy_work's may be chained but destroy_workqueue()
2312 * can take care of them. Just kill the fcoe_wq.
2313 */
2314 destroy_workqueue(fcoe_wq);
Chris Leech2e70e242009-08-25 14:00:23 -07002315
Tejun Heo2ca32b42011-01-28 16:05:32 -08002316 /*
2317 * Detaching from the scsi transport must happen after all
2318 * destroys are done on the fcoe_wq. destroy_workqueue will
2319 * enusre the fcoe_wq is flushed.
2320 */
Chris Leech2e70e242009-08-25 14:00:23 -07002321 fcoe_if_exit();
Yi Zou78a58242011-01-28 16:05:16 -08002322
2323 /* detach from fcoe transport */
2324 fcoe_transport_detach(&fcoe_sw_transport);
Robert Love85b4aa42008-12-09 15:10:24 -08002325}
2326module_exit(fcoe_exit);
Chris Leech11b56182009-11-03 11:46:29 -08002327
2328/**
2329 * fcoe_flogi_resp() - FCoE specific FLOGI and FDISC response handler
2330 * @seq: active sequence in the FLOGI or FDISC exchange
2331 * @fp: response frame, or error encoded in a pointer (timeout)
2332 * @arg: pointer the the fcoe_ctlr structure
2333 *
Uwe Kleine-König65155b32010-06-11 12:17:01 +02002334 * This handles MAC address management for FCoE, then passes control on to
Chris Leech11b56182009-11-03 11:46:29 -08002335 * the libfc FLOGI response handler.
2336 */
2337static void fcoe_flogi_resp(struct fc_seq *seq, struct fc_frame *fp, void *arg)
2338{
2339 struct fcoe_ctlr *fip = arg;
2340 struct fc_exch *exch = fc_seq_exch(seq);
2341 struct fc_lport *lport = exch->lp;
2342 u8 *mac;
2343
2344 if (IS_ERR(fp))
2345 goto done;
2346
2347 mac = fr_cb(fp)->granted_mac;
2348 if (is_zero_ether_addr(mac)) {
2349 /* pre-FIP */
Joe Eykholt386309ce2009-11-03 11:49:16 -08002350 if (fcoe_ctlr_recv_flogi(fip, lport, fp)) {
Chris Leech11b56182009-11-03 11:46:29 -08002351 fc_frame_free(fp);
2352 return;
2353 }
Chris Leech11b56182009-11-03 11:46:29 -08002354 }
Joe Eykholt386309ce2009-11-03 11:49:16 -08002355 fcoe_update_src_mac(lport, mac);
Chris Leech11b56182009-11-03 11:46:29 -08002356done:
2357 fc_lport_flogi_resp(seq, fp, lport);
2358}
2359
2360/**
2361 * fcoe_logo_resp() - FCoE specific LOGO response handler
2362 * @seq: active sequence in the LOGO exchange
2363 * @fp: response frame, or error encoded in a pointer (timeout)
2364 * @arg: pointer the the fcoe_ctlr structure
2365 *
Uwe Kleine-König65155b32010-06-11 12:17:01 +02002366 * This handles MAC address management for FCoE, then passes control on to
Chris Leech11b56182009-11-03 11:46:29 -08002367 * the libfc LOGO response handler.
2368 */
2369static void fcoe_logo_resp(struct fc_seq *seq, struct fc_frame *fp, void *arg)
2370{
Joe Eykholt386309ce2009-11-03 11:49:16 -08002371 struct fc_lport *lport = arg;
Chris Leech11b56182009-11-03 11:46:29 -08002372 static u8 zero_mac[ETH_ALEN] = { 0 };
2373
2374 if (!IS_ERR(fp))
Joe Eykholt386309ce2009-11-03 11:49:16 -08002375 fcoe_update_src_mac(lport, zero_mac);
Chris Leech11b56182009-11-03 11:46:29 -08002376 fc_lport_logo_resp(seq, fp, lport);
2377}
2378
2379/**
2380 * fcoe_elsct_send - FCoE specific ELS handler
2381 *
2382 * This does special case handling of FIP encapsualted ELS exchanges for FCoE,
2383 * using FCoE specific response handlers and passing the FIP controller as
2384 * the argument (the lport is still available from the exchange).
2385 *
2386 * Most of the work here is just handed off to the libfc routine.
2387 */
Robert Love1875f272009-11-03 11:47:50 -08002388static struct fc_seq *fcoe_elsct_send(struct fc_lport *lport, u32 did,
2389 struct fc_frame *fp, unsigned int op,
2390 void (*resp)(struct fc_seq *,
2391 struct fc_frame *,
2392 void *),
2393 void *arg, u32 timeout)
Chris Leech11b56182009-11-03 11:46:29 -08002394{
2395 struct fcoe_port *port = lport_priv(lport);
Bhanu Prakash Gollapudi8597ae82011-01-28 16:05:37 -08002396 struct fcoe_interface *fcoe = port->priv;
Chris Leech11b56182009-11-03 11:46:29 -08002397 struct fcoe_ctlr *fip = &fcoe->ctlr;
2398 struct fc_frame_header *fh = fc_frame_header_get(fp);
2399
2400 switch (op) {
2401 case ELS_FLOGI:
2402 case ELS_FDISC:
Joe Eykholte10f8c62010-07-20 15:20:30 -07002403 if (lport->point_to_multipoint)
2404 break;
Chris Leech11b56182009-11-03 11:46:29 -08002405 return fc_elsct_send(lport, did, fp, op, fcoe_flogi_resp,
2406 fip, timeout);
2407 case ELS_LOGO:
2408 /* only hook onto fabric logouts, not port logouts */
2409 if (ntoh24(fh->fh_d_id) != FC_FID_FLOGI)
2410 break;
2411 return fc_elsct_send(lport, did, fp, op, fcoe_logo_resp,
Joe Eykholt386309ce2009-11-03 11:49:16 -08002412 lport, timeout);
Chris Leech11b56182009-11-03 11:46:29 -08002413 }
2414 return fc_elsct_send(lport, did, fp, op, resp, arg, timeout);
2415}
2416
Chris Leech9a057532009-11-03 11:46:40 -08002417/**
2418 * fcoe_vport_create() - create an fc_host/scsi_host for a vport
2419 * @vport: fc_vport object to create a new fc_host for
2420 * @disabled: start the new fc_host in a disabled state by default?
2421 *
2422 * Returns: 0 for success
2423 */
2424static int fcoe_vport_create(struct fc_vport *vport, bool disabled)
2425{
2426 struct Scsi_Host *shost = vport_to_shost(vport);
2427 struct fc_lport *n_port = shost_priv(shost);
2428 struct fcoe_port *port = lport_priv(n_port);
Bhanu Prakash Gollapudi8597ae82011-01-28 16:05:37 -08002429 struct fcoe_interface *fcoe = port->priv;
Chris Leech9a057532009-11-03 11:46:40 -08002430 struct net_device *netdev = fcoe->netdev;
2431 struct fc_lport *vn_port;
Neerav Parikhbdf25212011-05-16 16:45:29 -07002432 int rc;
2433 char buf[32];
2434
2435 rc = fcoe_validate_vport_create(vport);
2436 if (rc) {
Bhanu Prakash Gollapudid8348952011-08-04 17:38:49 -07002437 fcoe_wwn_to_str(vport->port_name, buf, sizeof(buf));
Neerav Parikhbdf25212011-05-16 16:45:29 -07002438 printk(KERN_ERR "fcoe: Failed to create vport, "
2439 "WWPN (0x%s) already exists\n",
2440 buf);
2441 return rc;
2442 }
Chris Leech9a057532009-11-03 11:46:40 -08002443
2444 mutex_lock(&fcoe_config_mutex);
2445 vn_port = fcoe_if_create(fcoe, &vport->dev, 1);
2446 mutex_unlock(&fcoe_config_mutex);
2447
2448 if (IS_ERR(vn_port)) {
2449 printk(KERN_ERR "fcoe: fcoe_vport_create(%s) failed\n",
2450 netdev->name);
2451 return -EIO;
2452 }
2453
2454 if (disabled) {
2455 fc_vport_set_state(vport, FC_VPORT_DISABLED);
2456 } else {
2457 vn_port->boot_time = jiffies;
2458 fc_fabric_login(vn_port);
2459 fc_vport_setlink(vn_port);
2460 }
2461 return 0;
2462}
2463
2464/**
2465 * fcoe_vport_destroy() - destroy the fc_host/scsi_host for a vport
2466 * @vport: fc_vport object that is being destroyed
2467 *
2468 * Returns: 0 for success
2469 */
2470static int fcoe_vport_destroy(struct fc_vport *vport)
2471{
2472 struct Scsi_Host *shost = vport_to_shost(vport);
2473 struct fc_lport *n_port = shost_priv(shost);
2474 struct fc_lport *vn_port = vport->dd_data;
2475 struct fcoe_port *port = lport_priv(vn_port);
2476
2477 mutex_lock(&n_port->lp_mutex);
2478 list_del(&vn_port->list);
2479 mutex_unlock(&n_port->lp_mutex);
Tejun Heo2ca32b42011-01-28 16:05:32 -08002480 queue_work(fcoe_wq, &port->destroy_work);
Chris Leech9a057532009-11-03 11:46:40 -08002481 return 0;
2482}
2483
2484/**
2485 * fcoe_vport_disable() - change vport state
2486 * @vport: vport to bring online/offline
2487 * @disable: should the vport be disabled?
2488 */
2489static int fcoe_vport_disable(struct fc_vport *vport, bool disable)
2490{
2491 struct fc_lport *lport = vport->dd_data;
2492
2493 if (disable) {
2494 fc_vport_set_state(vport, FC_VPORT_DISABLED);
2495 fc_fabric_logoff(lport);
2496 } else {
2497 lport->boot_time = jiffies;
2498 fc_fabric_login(lport);
2499 fc_vport_setlink(lport);
2500 }
2501
2502 return 0;
2503}
2504
Chris Leechdc8596d2009-11-03 11:47:18 -08002505/**
2506 * fcoe_vport_set_symbolic_name() - append vport string to symbolic name
2507 * @vport: fc_vport with a new symbolic name string
2508 *
2509 * After generating a new symbolic name string, a new RSPN_ID request is
2510 * sent to the name server. There is no response handler, so if it fails
2511 * for some reason it will not be retried.
2512 */
2513static void fcoe_set_vport_symbolic_name(struct fc_vport *vport)
2514{
2515 struct fc_lport *lport = vport->dd_data;
2516 struct fc_frame *fp;
2517 size_t len;
2518
2519 snprintf(fc_host_symbolic_name(lport->host), FC_SYMBOLIC_NAME_SIZE,
2520 "%s v%s over %s : %s", FCOE_NAME, FCOE_VERSION,
2521 fcoe_netdev(lport)->name, vport->symbolic_name);
2522
2523 if (lport->state != LPORT_ST_READY)
2524 return;
2525
2526 len = strnlen(fc_host_symbolic_name(lport->host), 255);
2527 fp = fc_frame_alloc(lport,
2528 sizeof(struct fc_ct_hdr) +
2529 sizeof(struct fc_ns_rspn) + len);
2530 if (!fp)
2531 return;
2532 lport->tt.elsct_send(lport, FC_FID_DIR_SERV, fp, FC_NS_RSPN_ID,
Joe Eykholtb94f8952009-11-03 11:50:21 -08002533 NULL, NULL, 3 * lport->r_a_tov);
Chris Leechdc8596d2009-11-03 11:47:18 -08002534}
Yi Zoub84056b2009-11-20 14:55:19 -08002535
2536/**
2537 * fcoe_get_lesb() - Fill the FCoE Link Error Status Block
2538 * @lport: the local port
2539 * @fc_lesb: the link error status block
2540 */
2541static void fcoe_get_lesb(struct fc_lport *lport,
2542 struct fc_els_lesb *fc_lesb)
2543{
Yi Zoub84056b2009-11-20 14:55:19 -08002544 struct net_device *netdev = fcoe_netdev(lport);
2545
Bhanu Prakash Gollapudi814740d2011-10-03 16:45:01 -07002546 __fcoe_get_lesb(lport, fc_lesb, netdev);
Yi Zoub84056b2009-11-20 14:55:19 -08002547}
Joe Eykholt7d65b0d2010-03-12 16:08:02 -08002548
2549/**
2550 * fcoe_set_port_id() - Callback from libfc when Port_ID is set.
2551 * @lport: the local port
2552 * @port_id: the port ID
2553 * @fp: the received frame, if any, that caused the port_id to be set.
2554 *
2555 * This routine handles the case where we received a FLOGI and are
2556 * entering point-to-point mode. We need to call fcoe_ctlr_recv_flogi()
2557 * so it can set the non-mapped mode and gateway address.
2558 *
2559 * The FLOGI LS_ACC is handled by fcoe_flogi_resp().
2560 */
2561static void fcoe_set_port_id(struct fc_lport *lport,
2562 u32 port_id, struct fc_frame *fp)
2563{
2564 struct fcoe_port *port = lport_priv(lport);
Bhanu Prakash Gollapudi8597ae82011-01-28 16:05:37 -08002565 struct fcoe_interface *fcoe = port->priv;
Joe Eykholt7d65b0d2010-03-12 16:08:02 -08002566
2567 if (fp && fc_frame_payload_op(fp) == ELS_FLOGI)
2568 fcoe_ctlr_recv_flogi(&fcoe->ctlr, lport, fp);
2569}