blob: 8885b3ef369aed632421136b8b39324d56c271d6 [file] [log] [blame]
Robert Love85b4aa42008-12-09 15:10:24 -08001/*
Chris Leechaf7f85d2009-08-25 13:59:24 -07002 * Copyright(c) 2007 - 2009 Intel Corporation. All rights reserved.
Robert Love85b4aa42008-12-09 15:10:24 -08003 *
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms and conditions of the GNU General Public License,
6 * version 2, as published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope it will be useful, but WITHOUT
9 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
10 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
11 * more details.
12 *
13 * You should have received a copy of the GNU General Public License along with
14 * this program; if not, write to the Free Software Foundation, Inc.,
15 * 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
16 *
17 * Maintained at www.Open-FCoE.org
18 */
19
20#include <linux/module.h>
21#include <linux/version.h>
Robert Love85b4aa42008-12-09 15:10:24 -080022#include <linux/spinlock.h>
Robert Love85b4aa42008-12-09 15:10:24 -080023#include <linux/netdevice.h>
24#include <linux/etherdevice.h>
25#include <linux/ethtool.h>
26#include <linux/if_ether.h>
27#include <linux/if_vlan.h>
Robert Love85b4aa42008-12-09 15:10:24 -080028#include <linux/crc32.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090029#include <linux/slab.h>
Robert Love85b4aa42008-12-09 15:10:24 -080030#include <linux/cpu.h>
31#include <linux/fs.h>
32#include <linux/sysfs.h>
33#include <linux/ctype.h>
Tejun Heo2ca32b42011-01-28 16:05:32 -080034#include <linux/workqueue.h>
Robert Love85b4aa42008-12-09 15:10:24 -080035#include <scsi/scsi_tcq.h>
36#include <scsi/scsicam.h>
37#include <scsi/scsi_transport.h>
38#include <scsi/scsi_transport_fc.h>
39#include <net/rtnetlink.h>
40
41#include <scsi/fc/fc_encaps.h>
Joe Eykholt97c83892009-03-17 11:42:40 -070042#include <scsi/fc/fc_fip.h>
Robert Love85b4aa42008-12-09 15:10:24 -080043
44#include <scsi/libfc.h>
45#include <scsi/fc_frame.h>
46#include <scsi/libfcoe.h>
Robert Love85b4aa42008-12-09 15:10:24 -080047
Vasu Devfdd78022009-03-17 11:42:24 -070048#include "fcoe.h"
49
Robert Love85b4aa42008-12-09 15:10:24 -080050MODULE_AUTHOR("Open-FCoE.org");
51MODULE_DESCRIPTION("FCoE");
Vasu Dev9b34ecf2009-03-17 11:42:13 -070052MODULE_LICENSE("GPL v2");
Robert Love85b4aa42008-12-09 15:10:24 -080053
Yi Zou05cc7392009-08-25 13:59:03 -070054/* Performance tuning parameters for fcoe */
55static unsigned int fcoe_ddp_min;
56module_param_named(ddp_min, fcoe_ddp_min, uint, S_IRUGO | S_IWUSR);
57MODULE_PARM_DESC(ddp_min, "Minimum I/O size in bytes for " \
58 "Direct Data Placement (DDP).");
59
Chris Leechdfc1d0f2009-08-25 14:00:13 -070060DEFINE_MUTEX(fcoe_config_mutex);
61
Tejun Heo2ca32b42011-01-28 16:05:32 -080062static struct workqueue_struct *fcoe_wq;
63
Joe Eykholte7a51992009-08-25 14:04:08 -070064/* fcoe_percpu_clean completion. Waiter protected by fcoe_create_mutex */
65static DECLARE_COMPLETION(fcoe_flush_completion);
66
Robert Love85b4aa42008-12-09 15:10:24 -080067/* fcoe host list */
Chris Leech090eb6c2009-08-25 14:00:28 -070068/* must only by accessed under the RTNL mutex */
Robert Love85b4aa42008-12-09 15:10:24 -080069LIST_HEAD(fcoe_hostlist);
Robert Love5e5e92d2009-03-17 11:41:35 -070070DEFINE_PER_CPU(struct fcoe_percpu_s, fcoe_percpu);
Robert Love85b4aa42008-12-09 15:10:24 -080071
Chris Leechdd3fd722009-04-21 16:27:36 -070072/* Function Prototypes */
Robert Love1875f272009-11-03 11:47:50 -080073static int fcoe_reset(struct Scsi_Host *);
Vasu Devfdd78022009-03-17 11:42:24 -070074static int fcoe_xmit(struct fc_lport *, struct fc_frame *);
75static int fcoe_rcv(struct sk_buff *, struct net_device *,
76 struct packet_type *, struct net_device *);
Robert Love1875f272009-11-03 11:47:50 -080077static int fcoe_percpu_receive_thread(void *);
Robert Love1875f272009-11-03 11:47:50 -080078static void fcoe_percpu_clean(struct fc_lport *);
Robert Love5e4f8fe2010-05-07 15:18:35 -070079static int fcoe_link_speed_update(struct fc_lport *);
Robert Love1875f272009-11-03 11:47:50 -080080static int fcoe_link_ok(struct fc_lport *);
Vasu Devfdd78022009-03-17 11:42:24 -070081
82static struct fc_lport *fcoe_hostlist_lookup(const struct net_device *);
83static int fcoe_hostlist_add(const struct fc_lport *);
Vasu Devfdd78022009-03-17 11:42:24 -070084
Robert Love85b4aa42008-12-09 15:10:24 -080085static int fcoe_device_notification(struct notifier_block *, ulong, void *);
86static void fcoe_dev_setup(void);
87static void fcoe_dev_cleanup(void);
Robert Love1875f272009-11-03 11:47:50 -080088static struct fcoe_interface
89*fcoe_hostlist_lookup_port(const struct net_device *);
Robert Love85b4aa42008-12-09 15:10:24 -080090
Robert Love1875f272009-11-03 11:47:50 -080091static int fcoe_fip_recv(struct sk_buff *, struct net_device *,
92 struct packet_type *, struct net_device *);
93
94static void fcoe_fip_send(struct fcoe_ctlr *, struct sk_buff *);
95static void fcoe_update_src_mac(struct fc_lport *, u8 *);
96static u8 *fcoe_get_src_mac(struct fc_lport *);
97static void fcoe_destroy_work(struct work_struct *);
98
99static int fcoe_ddp_setup(struct fc_lport *, u16, struct scatterlist *,
100 unsigned int);
101static int fcoe_ddp_done(struct fc_lport *, u16);
102
103static int fcoe_cpu_callback(struct notifier_block *, unsigned long, void *);
104
Yi Zou78a58242011-01-28 16:05:16 -0800105static bool fcoe_match(struct net_device *netdev);
106static int fcoe_create(struct net_device *netdev, enum fip_state fip_mode);
107static int fcoe_destroy(struct net_device *netdev);
108static int fcoe_enable(struct net_device *netdev);
109static int fcoe_disable(struct net_device *netdev);
Robert Love1875f272009-11-03 11:47:50 -0800110
Robert Love1875f272009-11-03 11:47:50 -0800111static struct fc_seq *fcoe_elsct_send(struct fc_lport *,
112 u32 did, struct fc_frame *,
113 unsigned int op,
114 void (*resp)(struct fc_seq *,
115 struct fc_frame *,
116 void *),
117 void *, u32 timeout);
Chris Leech859b7b62009-11-20 14:54:47 -0800118static void fcoe_recv_frame(struct sk_buff *skb);
Robert Love1875f272009-11-03 11:47:50 -0800119
Yi Zoub84056b2009-11-20 14:55:19 -0800120static void fcoe_get_lesb(struct fc_lport *, struct fc_els_lesb *);
121
Robert Love1875f272009-11-03 11:47:50 -0800122/* notification function for packets from net device */
Robert Love85b4aa42008-12-09 15:10:24 -0800123static struct notifier_block fcoe_notifier = {
124 .notifier_call = fcoe_device_notification,
125};
126
Robert Love1875f272009-11-03 11:47:50 -0800127/* notification function for CPU hotplug events */
128static struct notifier_block fcoe_cpu_notifier = {
129 .notifier_call = fcoe_cpu_callback,
130};
131
Yi Zou8ca86f82011-01-28 16:05:11 -0800132static struct scsi_transport_template *fcoe_nport_scsi_transport;
133static struct scsi_transport_template *fcoe_vport_scsi_transport;
Vasu Dev7f349142009-03-27 09:06:31 -0700134
Robert Love1875f272009-11-03 11:47:50 -0800135static int fcoe_vport_destroy(struct fc_vport *);
136static int fcoe_vport_create(struct fc_vport *, bool disabled);
137static int fcoe_vport_disable(struct fc_vport *, bool disable);
138static void fcoe_set_vport_symbolic_name(struct fc_vport *);
Joe Eykholt7d65b0d2010-03-12 16:08:02 -0800139static void fcoe_set_port_id(struct fc_lport *, u32, struct fc_frame *);
Neerav Parikhbdf25212011-05-16 16:45:29 -0700140static int fcoe_validate_vport_create(struct fc_vport *);
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,
146 .elsct_send = fcoe_elsct_send,
Yi Zoub84056b2009-11-20 14:55:19 -0800147 .get_lesb = fcoe_get_lesb,
Joe Eykholt7d65b0d2010-03-12 16:08:02 -0800148 .lport_set_port_id = fcoe_set_port_id,
Robert Love1875f272009-11-03 11:47:50 -0800149};
Chris Leech9a057532009-11-03 11:46:40 -0800150
Yi Zou8ca86f82011-01-28 16:05:11 -0800151struct fc_function_template fcoe_nport_fc_functions = {
Vasu Dev7f349142009-03-27 09:06:31 -0700152 .show_host_node_name = 1,
153 .show_host_port_name = 1,
154 .show_host_supported_classes = 1,
155 .show_host_supported_fc4s = 1,
156 .show_host_active_fc4s = 1,
157 .show_host_maxframe_size = 1,
158
159 .show_host_port_id = 1,
160 .show_host_supported_speeds = 1,
161 .get_host_speed = fc_get_host_speed,
162 .show_host_speed = 1,
163 .show_host_port_type = 1,
164 .get_host_port_state = fc_get_host_port_state,
165 .show_host_port_state = 1,
166 .show_host_symbolic_name = 1,
167
168 .dd_fcrport_size = sizeof(struct fc_rport_libfc_priv),
169 .show_rport_maxframe_size = 1,
170 .show_rport_supported_classes = 1,
171
172 .show_host_fabric_name = 1,
173 .show_starget_node_name = 1,
174 .show_starget_port_name = 1,
175 .show_starget_port_id = 1,
176 .set_rport_dev_loss_tmo = fc_set_rport_loss_tmo,
177 .show_rport_dev_loss_tmo = 1,
178 .get_fc_host_stats = fc_get_host_stats,
179 .issue_fc_host_lip = fcoe_reset,
180
181 .terminate_rport_io = fc_rport_terminate_io,
Chris Leech9a057532009-11-03 11:46:40 -0800182
183 .vport_create = fcoe_vport_create,
184 .vport_delete = fcoe_vport_destroy,
185 .vport_disable = fcoe_vport_disable,
Chris Leechdc8596d2009-11-03 11:47:18 -0800186 .set_vport_symbolic_name = fcoe_set_vport_symbolic_name,
Steve Maa51ab392009-11-03 11:47:34 -0800187
188 .bsg_request = fc_lport_bsg_request,
Vasu Dev7f349142009-03-27 09:06:31 -0700189};
190
Yi Zou8ca86f82011-01-28 16:05:11 -0800191struct fc_function_template fcoe_vport_fc_functions = {
Chris Leeche9084bb2009-11-03 11:46:34 -0800192 .show_host_node_name = 1,
193 .show_host_port_name = 1,
194 .show_host_supported_classes = 1,
195 .show_host_supported_fc4s = 1,
196 .show_host_active_fc4s = 1,
197 .show_host_maxframe_size = 1,
198
199 .show_host_port_id = 1,
200 .show_host_supported_speeds = 1,
201 .get_host_speed = fc_get_host_speed,
202 .show_host_speed = 1,
203 .show_host_port_type = 1,
204 .get_host_port_state = fc_get_host_port_state,
205 .show_host_port_state = 1,
206 .show_host_symbolic_name = 1,
207
208 .dd_fcrport_size = sizeof(struct fc_rport_libfc_priv),
209 .show_rport_maxframe_size = 1,
210 .show_rport_supported_classes = 1,
211
212 .show_host_fabric_name = 1,
213 .show_starget_node_name = 1,
214 .show_starget_port_name = 1,
215 .show_starget_port_id = 1,
216 .set_rport_dev_loss_tmo = fc_set_rport_loss_tmo,
217 .show_rport_dev_loss_tmo = 1,
218 .get_fc_host_stats = fc_get_host_stats,
219 .issue_fc_host_lip = fcoe_reset,
220
221 .terminate_rport_io = fc_rport_terminate_io,
Steve Maa51ab392009-11-03 11:47:34 -0800222
223 .bsg_request = fc_lport_bsg_request,
Chris Leeche9084bb2009-11-03 11:46:34 -0800224};
225
Vasu Dev7f349142009-03-27 09:06:31 -0700226static struct scsi_host_template fcoe_shost_template = {
227 .module = THIS_MODULE,
228 .name = "FCoE Driver",
229 .proc_name = FCOE_NAME,
230 .queuecommand = fc_queuecommand,
231 .eh_abort_handler = fc_eh_abort,
232 .eh_device_reset_handler = fc_eh_device_reset,
233 .eh_host_reset_handler = fc_eh_host_reset,
234 .slave_alloc = fc_slave_alloc,
235 .change_queue_depth = fc_change_queue_depth,
236 .change_queue_type = fc_change_queue_type,
237 .this_id = -1,
Vasu Dev14caf442009-10-15 17:46:55 -0700238 .cmd_per_lun = 3,
Vasu Dev7f349142009-03-27 09:06:31 -0700239 .can_queue = FCOE_MAX_OUTSTANDING_COMMANDS,
240 .use_clustering = ENABLE_CLUSTERING,
241 .sg_tablesize = SG_ALL,
242 .max_sectors = 0xffff,
243};
244
Chris Leech54b649f2009-08-25 14:00:07 -0700245/**
Robert Love1875f272009-11-03 11:47:50 -0800246 * fcoe_interface_setup() - Setup a FCoE interface
247 * @fcoe: The new FCoE interface
248 * @netdev: The net device that the fcoe interface is on
Chris Leech54b649f2009-08-25 14:00:07 -0700249 *
250 * Returns : 0 for success
Chris Leech2e70e242009-08-25 14:00:23 -0700251 * Locking: must be called with the RTNL mutex held
Chris Leech54b649f2009-08-25 14:00:07 -0700252 */
253static int fcoe_interface_setup(struct fcoe_interface *fcoe,
254 struct net_device *netdev)
255{
256 struct fcoe_ctlr *fip = &fcoe->ctlr;
257 struct netdev_hw_addr *ha;
Yi Zou5bab87e2009-11-03 11:49:43 -0800258 struct net_device *real_dev;
Chris Leech54b649f2009-08-25 14:00:07 -0700259 u8 flogi_maddr[ETH_ALEN];
Yi Zoub7a727f2009-10-21 16:28:03 -0700260 const struct net_device_ops *ops;
Chris Leech54b649f2009-08-25 14:00:07 -0700261
262 fcoe->netdev = netdev;
263
Yi Zoub7a727f2009-10-21 16:28:03 -0700264 /* Let LLD initialize for FCoE */
265 ops = netdev->netdev_ops;
266 if (ops->ndo_fcoe_enable) {
267 if (ops->ndo_fcoe_enable(netdev))
268 FCOE_NETDEV_DBG(netdev, "Failed to enable FCoE"
269 " specific feature for LLD.\n");
270 }
271
Chris Leech54b649f2009-08-25 14:00:07 -0700272 /* Do not support for bonding device */
Jiri Pirkocc8bdf02011-03-01 20:05:35 +0000273 if (netdev->priv_flags & IFF_BONDING && netdev->flags & IFF_MASTER) {
john fastabend59d92512009-11-03 11:48:44 -0800274 FCOE_NETDEV_DBG(netdev, "Bonded interfaces not supported\n");
Chris Leech54b649f2009-08-25 14:00:07 -0700275 return -EOPNOTSUPP;
276 }
277
278 /* look for SAN MAC address, if multiple SAN MACs exist, only
279 * use the first one for SPMA */
Yi Zou5bab87e2009-11-03 11:49:43 -0800280 real_dev = (netdev->priv_flags & IFF_802_1Q_VLAN) ?
281 vlan_dev_real_dev(netdev) : netdev;
Chris Leech54b649f2009-08-25 14:00:07 -0700282 rcu_read_lock();
Yi Zou5bab87e2009-11-03 11:49:43 -0800283 for_each_dev_addr(real_dev, ha) {
Chris Leech54b649f2009-08-25 14:00:07 -0700284 if ((ha->type == NETDEV_HW_ADDR_T_SAN) &&
Yi Zoubf361702009-11-03 11:49:38 -0800285 (is_valid_ether_addr(ha->addr))) {
Chris Leech54b649f2009-08-25 14:00:07 -0700286 memcpy(fip->ctl_src_addr, ha->addr, ETH_ALEN);
287 fip->spma = 1;
288 break;
289 }
290 }
291 rcu_read_unlock();
292
293 /* setup Source Mac Address */
294 if (!fip->spma)
295 memcpy(fip->ctl_src_addr, netdev->dev_addr, netdev->addr_len);
296
297 /*
298 * Add FCoE MAC address as second unicast MAC address
299 * or enter promiscuous mode if not capable of listening
300 * for multiple unicast MACs.
301 */
Chris Leech54b649f2009-08-25 14:00:07 -0700302 memcpy(flogi_maddr, (u8[6]) FC_FCOE_FLOGI_MAC, ETH_ALEN);
Jiri Pirkoa748ee22010-04-01 21:22:09 +0000303 dev_uc_add(netdev, flogi_maddr);
Chris Leech54b649f2009-08-25 14:00:07 -0700304 if (fip->spma)
Jiri Pirkoa748ee22010-04-01 21:22:09 +0000305 dev_uc_add(netdev, fip->ctl_src_addr);
Joe Eykholte10f8c62010-07-20 15:20:30 -0700306 if (fip->mode == FIP_MODE_VN2VN) {
307 dev_mc_add(netdev, FIP_ALL_VN2VN_MACS);
308 dev_mc_add(netdev, FIP_ALL_P2P_MACS);
309 } else
310 dev_mc_add(netdev, FIP_ALL_ENODE_MACS);
Chris Leech54b649f2009-08-25 14:00:07 -0700311
312 /*
313 * setup the receive function from ethernet driver
314 * on the ethertype for the given device
315 */
316 fcoe->fcoe_packet_type.func = fcoe_rcv;
317 fcoe->fcoe_packet_type.type = __constant_htons(ETH_P_FCOE);
318 fcoe->fcoe_packet_type.dev = netdev;
319 dev_add_pack(&fcoe->fcoe_packet_type);
320
321 fcoe->fip_packet_type.func = fcoe_fip_recv;
322 fcoe->fip_packet_type.type = htons(ETH_P_FIP);
323 fcoe->fip_packet_type.dev = netdev;
324 dev_add_pack(&fcoe->fip_packet_type);
325
326 return 0;
327}
328
Vasu Dev7f349142009-03-27 09:06:31 -0700329/**
Robert Love1875f272009-11-03 11:47:50 -0800330 * fcoe_interface_create() - Create a FCoE interface on a net device
331 * @netdev: The net device to create the FCoE interface on
Joe Eykholt1dd454d2010-07-20 15:20:46 -0700332 * @fip_mode: The mode to use for FIP
Chris Leech030f4e02009-08-25 14:00:02 -0700333 *
334 * Returns: pointer to a struct fcoe_interface or NULL on error
335 */
Joe Eykholt1dd454d2010-07-20 15:20:46 -0700336static struct fcoe_interface *fcoe_interface_create(struct net_device *netdev,
337 enum fip_state fip_mode)
Chris Leech030f4e02009-08-25 14:00:02 -0700338{
339 struct fcoe_interface *fcoe;
john fastabend59d92512009-11-03 11:48:44 -0800340 int err;
Chris Leech030f4e02009-08-25 14:00:02 -0700341
Robert Love7287fb92011-01-28 16:03:47 -0800342 if (!try_module_get(THIS_MODULE)) {
343 FCOE_NETDEV_DBG(netdev,
344 "Could not get a reference to the module\n");
345 fcoe = ERR_PTR(-EBUSY);
346 goto out;
347 }
348
Chris Leech030f4e02009-08-25 14:00:02 -0700349 fcoe = kzalloc(sizeof(*fcoe), GFP_KERNEL);
350 if (!fcoe) {
351 FCOE_NETDEV_DBG(netdev, "Could not allocate fcoe structure\n");
Robert Love7287fb92011-01-28 16:03:47 -0800352 fcoe = ERR_PTR(-ENOMEM);
353 goto out_nomod;
Chris Leech030f4e02009-08-25 14:00:02 -0700354 }
355
Chris Leech2e70e242009-08-25 14:00:23 -0700356 dev_hold(netdev);
Chris Leech030f4e02009-08-25 14:00:02 -0700357 kref_init(&fcoe->kref);
Chris Leech54b649f2009-08-25 14:00:07 -0700358
359 /*
360 * Initialize FIP.
361 */
Joe Eykholt1dd454d2010-07-20 15:20:46 -0700362 fcoe_ctlr_init(&fcoe->ctlr, fip_mode);
Chris Leech54b649f2009-08-25 14:00:07 -0700363 fcoe->ctlr.send = fcoe_fip_send;
364 fcoe->ctlr.update_mac = fcoe_update_src_mac;
Chris Leech11b56182009-11-03 11:46:29 -0800365 fcoe->ctlr.get_src_addr = fcoe_get_src_mac;
Chris Leech54b649f2009-08-25 14:00:07 -0700366
john fastabend59d92512009-11-03 11:48:44 -0800367 err = fcoe_interface_setup(fcoe, netdev);
368 if (err) {
369 fcoe_ctlr_destroy(&fcoe->ctlr);
370 kfree(fcoe);
371 dev_put(netdev);
Robert Love7287fb92011-01-28 16:03:47 -0800372 fcoe = ERR_PTR(err);
373 goto out_nomod;
john fastabend59d92512009-11-03 11:48:44 -0800374 }
Chris Leech030f4e02009-08-25 14:00:02 -0700375
Robert Love7287fb92011-01-28 16:03:47 -0800376 goto out;
377
378out_nomod:
379 module_put(THIS_MODULE);
380out:
Chris Leech030f4e02009-08-25 14:00:02 -0700381 return fcoe;
382}
383
384/**
385 * fcoe_interface_release() - fcoe_port kref release function
Robert Love1875f272009-11-03 11:47:50 -0800386 * @kref: Embedded reference count in an fcoe_interface struct
Chris Leech030f4e02009-08-25 14:00:02 -0700387 */
388static void fcoe_interface_release(struct kref *kref)
389{
390 struct fcoe_interface *fcoe;
Chris Leech2e70e242009-08-25 14:00:23 -0700391 struct net_device *netdev;
Chris Leech030f4e02009-08-25 14:00:02 -0700392
393 fcoe = container_of(kref, struct fcoe_interface, kref);
Chris Leech2e70e242009-08-25 14:00:23 -0700394 netdev = fcoe->netdev;
395 /* tear-down the FCoE controller */
396 fcoe_ctlr_destroy(&fcoe->ctlr);
Chris Leech030f4e02009-08-25 14:00:02 -0700397 kfree(fcoe);
Chris Leech2e70e242009-08-25 14:00:23 -0700398 dev_put(netdev);
Robert Love7287fb92011-01-28 16:03:47 -0800399 module_put(THIS_MODULE);
Chris Leech030f4e02009-08-25 14:00:02 -0700400}
401
402/**
Robert Love1875f272009-11-03 11:47:50 -0800403 * fcoe_interface_get() - Get a reference to a FCoE interface
404 * @fcoe: The FCoE interface to be held
Chris Leech030f4e02009-08-25 14:00:02 -0700405 */
406static inline void fcoe_interface_get(struct fcoe_interface *fcoe)
407{
408 kref_get(&fcoe->kref);
409}
410
411/**
Robert Love1875f272009-11-03 11:47:50 -0800412 * fcoe_interface_put() - Put a reference to a FCoE interface
413 * @fcoe: The FCoE interface to be released
Chris Leech030f4e02009-08-25 14:00:02 -0700414 */
415static inline void fcoe_interface_put(struct fcoe_interface *fcoe)
416{
417 kref_put(&fcoe->kref, fcoe_interface_release);
418}
419
420/**
Vasu Devf04ca1b2011-04-01 16:06:45 -0700421 * fcoe_interface_cleanup() - Clean up a FCoE interface
422 * @fcoe: The FCoE interface to be cleaned up
423 *
424 * Caller must be holding the RTNL mutex
425 */
426void fcoe_interface_cleanup(struct fcoe_interface *fcoe)
427{
428 struct net_device *netdev = fcoe->netdev;
429 struct fcoe_ctlr *fip = &fcoe->ctlr;
430 u8 flogi_maddr[ETH_ALEN];
431 const struct net_device_ops *ops;
432 struct fcoe_port *port = lport_priv(fcoe->ctlr.lp);
433
434 FCOE_NETDEV_DBG(netdev, "Destroying interface\n");
435
436 /* Logout of the fabric */
437 fc_fabric_logoff(fcoe->ctlr.lp);
438
439 /* Cleanup the fc_lport */
440 fc_lport_destroy(fcoe->ctlr.lp);
441
442 /* Stop the transmit retry timer */
443 del_timer_sync(&port->timer);
444
445 /* Free existing transmit skbs */
446 fcoe_clean_pending_queue(fcoe->ctlr.lp);
447
448 /*
449 * Don't listen for Ethernet packets anymore.
450 * synchronize_net() ensures that the packet handlers are not running
451 * on another CPU. dev_remove_pack() would do that, this calls the
452 * unsyncronized version __dev_remove_pack() to avoid multiple delays.
453 */
454 __dev_remove_pack(&fcoe->fcoe_packet_type);
455 __dev_remove_pack(&fcoe->fip_packet_type);
456 synchronize_net();
457
458 /* Delete secondary MAC addresses */
459 memcpy(flogi_maddr, (u8[6]) FC_FCOE_FLOGI_MAC, ETH_ALEN);
460 dev_uc_del(netdev, flogi_maddr);
461 if (fip->spma)
462 dev_uc_del(netdev, fip->ctl_src_addr);
463 if (fip->mode == FIP_MODE_VN2VN) {
464 dev_mc_del(netdev, FIP_ALL_VN2VN_MACS);
465 dev_mc_del(netdev, FIP_ALL_P2P_MACS);
466 } else
467 dev_mc_del(netdev, FIP_ALL_ENODE_MACS);
468
469 if (!is_zero_ether_addr(port->data_src_addr))
470 dev_uc_del(netdev, port->data_src_addr);
471
472 /* Tell the LLD we are done w/ FCoE */
473 ops = netdev->netdev_ops;
474 if (ops->ndo_fcoe_disable) {
475 if (ops->ndo_fcoe_disable(netdev))
476 FCOE_NETDEV_DBG(netdev, "Failed to disable FCoE"
477 " specific feature for LLD.\n");
478 }
479 fcoe_interface_put(fcoe);
480}
481
482/**
Robert Love1875f272009-11-03 11:47:50 -0800483 * fcoe_fip_recv() - Handler for received FIP frames
484 * @skb: The receive skb
485 * @netdev: The associated net device
486 * @ptype: The packet_type structure which was used to register this handler
487 * @orig_dev: The original net_device the the skb was received on.
488 * (in case dev is a bond)
Vasu Devab6b85c2009-05-17 12:33:08 +0000489 *
490 * Returns: 0 for success
491 */
Robert Love1875f272009-11-03 11:47:50 -0800492static int fcoe_fip_recv(struct sk_buff *skb, struct net_device *netdev,
Vasu Devab6b85c2009-05-17 12:33:08 +0000493 struct packet_type *ptype,
494 struct net_device *orig_dev)
495{
Chris Leech259ad852009-08-25 13:59:41 -0700496 struct fcoe_interface *fcoe;
Vasu Devab6b85c2009-05-17 12:33:08 +0000497
Chris Leech259ad852009-08-25 13:59:41 -0700498 fcoe = container_of(ptype, struct fcoe_interface, fip_packet_type);
Chris Leech3fe9a0b2009-08-25 13:59:46 -0700499 fcoe_ctlr_recv(&fcoe->ctlr, skb);
Vasu Devab6b85c2009-05-17 12:33:08 +0000500 return 0;
501}
502
503/**
Robert Love1875f272009-11-03 11:47:50 -0800504 * fcoe_fip_send() - Send an Ethernet-encapsulated FIP frame
505 * @fip: The FCoE controller
506 * @skb: The FIP packet to be sent
Vasu Devab6b85c2009-05-17 12:33:08 +0000507 */
508static void fcoe_fip_send(struct fcoe_ctlr *fip, struct sk_buff *skb)
509{
Chris Leech3fe9a0b2009-08-25 13:59:46 -0700510 skb->dev = fcoe_from_ctlr(fip)->netdev;
Vasu Devab6b85c2009-05-17 12:33:08 +0000511 dev_queue_xmit(skb);
512}
513
514/**
Robert Love1875f272009-11-03 11:47:50 -0800515 * fcoe_update_src_mac() - Update the Ethernet MAC filters
516 * @lport: The local port to update the source MAC on
517 * @addr: Unicast MAC address to add
Vasu Devab6b85c2009-05-17 12:33:08 +0000518 *
519 * Remove any previously-set unicast MAC filter.
520 * Add secondary FCoE MAC address filter for our OUI.
521 */
Chris Leech11b56182009-11-03 11:46:29 -0800522static void fcoe_update_src_mac(struct fc_lport *lport, u8 *addr)
Vasu Devab6b85c2009-05-17 12:33:08 +0000523{
Chris Leech11b56182009-11-03 11:46:29 -0800524 struct fcoe_port *port = lport_priv(lport);
Bhanu Prakash Gollapudi8597ae82011-01-28 16:05:37 -0800525 struct fcoe_interface *fcoe = port->priv;
Vasu Devab6b85c2009-05-17 12:33:08 +0000526
Vasu Devab6b85c2009-05-17 12:33:08 +0000527 rtnl_lock();
Chris Leech11b56182009-11-03 11:46:29 -0800528 if (!is_zero_ether_addr(port->data_src_addr))
Jiri Pirkoa748ee22010-04-01 21:22:09 +0000529 dev_uc_del(fcoe->netdev, port->data_src_addr);
Chris Leech11b56182009-11-03 11:46:29 -0800530 if (!is_zero_ether_addr(addr))
Jiri Pirkoa748ee22010-04-01 21:22:09 +0000531 dev_uc_add(fcoe->netdev, addr);
Chris Leech11b56182009-11-03 11:46:29 -0800532 memcpy(port->data_src_addr, addr, ETH_ALEN);
Vasu Devab6b85c2009-05-17 12:33:08 +0000533 rtnl_unlock();
534}
535
536/**
Chris Leech11b56182009-11-03 11:46:29 -0800537 * fcoe_get_src_mac() - return the Ethernet source address for an lport
538 * @lport: libfc lport
539 */
540static u8 *fcoe_get_src_mac(struct fc_lport *lport)
541{
542 struct fcoe_port *port = lport_priv(lport);
543
544 return port->data_src_addr;
545}
546
547/**
Robert Love1875f272009-11-03 11:47:50 -0800548 * fcoe_lport_config() - Set up a local port
549 * @lport: The local port to be setup
Vasu Dev7f349142009-03-27 09:06:31 -0700550 *
551 * Returns: 0 for success
552 */
Robert Love1875f272009-11-03 11:47:50 -0800553static int fcoe_lport_config(struct fc_lport *lport)
Vasu Dev7f349142009-03-27 09:06:31 -0700554{
Robert Love1875f272009-11-03 11:47:50 -0800555 lport->link_up = 0;
556 lport->qfull = 0;
557 lport->max_retry_count = 3;
558 lport->max_rport_retry_count = 3;
559 lport->e_d_tov = 2 * 1000; /* FC-FS default */
560 lport->r_a_tov = 2 * 2 * 1000;
561 lport->service_params = (FCP_SPPF_INIT_FCN | FCP_SPPF_RD_XRDY_DIS |
562 FCP_SPPF_RETRY | FCP_SPPF_CONF_COMPL);
563 lport->does_npiv = 1;
Vasu Dev7f349142009-03-27 09:06:31 -0700564
Robert Love1875f272009-11-03 11:47:50 -0800565 fc_lport_init_stats(lport);
Vasu Dev7f349142009-03-27 09:06:31 -0700566
567 /* lport fc_lport related configuration */
Robert Love1875f272009-11-03 11:47:50 -0800568 fc_lport_config(lport);
Vasu Dev7f349142009-03-27 09:06:31 -0700569
570 /* offload related configuration */
Robert Love1875f272009-11-03 11:47:50 -0800571 lport->crc_offload = 0;
572 lport->seq_offload = 0;
573 lport->lro_enabled = 0;
574 lport->lro_xid = 0;
575 lport->lso_max = 0;
Vasu Dev7f349142009-03-27 09:06:31 -0700576
577 return 0;
578}
579
580/**
Yi Zoudcece412009-11-20 15:22:21 -0800581 * fcoe_get_wwn() - Get the world wide name from LLD if it supports it
582 * @netdev: the associated net device
583 * @wwn: the output WWN
584 * @type: the type of WWN (WWPN or WWNN)
585 *
586 * Returns: 0 for success
587 */
588static int fcoe_get_wwn(struct net_device *netdev, u64 *wwn, int type)
589{
590 const struct net_device_ops *ops = netdev->netdev_ops;
591
592 if (ops->ndo_fcoe_get_wwn)
593 return ops->ndo_fcoe_get_wwn(netdev, wwn, type);
594 return -EINVAL;
595}
596
597/**
Yi Zou54a5b212010-07-20 15:21:17 -0700598 * fcoe_netdev_features_change - Updates the lport's offload flags based
599 * on the LLD netdev's FCoE feature flags
600 */
601static void fcoe_netdev_features_change(struct fc_lport *lport,
602 struct net_device *netdev)
603{
604 mutex_lock(&lport->lp_mutex);
605
606 if (netdev->features & NETIF_F_SG)
607 lport->sg_supp = 1;
608 else
609 lport->sg_supp = 0;
610
611 if (netdev->features & NETIF_F_FCOE_CRC) {
612 lport->crc_offload = 1;
613 FCOE_NETDEV_DBG(netdev, "Supports FCCRC offload\n");
614 } else {
615 lport->crc_offload = 0;
616 }
617
618 if (netdev->features & NETIF_F_FSO) {
619 lport->seq_offload = 1;
620 lport->lso_max = netdev->gso_max_size;
621 FCOE_NETDEV_DBG(netdev, "Supports LSO for max len 0x%x\n",
622 lport->lso_max);
623 } else {
624 lport->seq_offload = 0;
625 lport->lso_max = 0;
626 }
627
628 if (netdev->fcoe_ddp_xid) {
629 lport->lro_enabled = 1;
630 lport->lro_xid = netdev->fcoe_ddp_xid;
631 FCOE_NETDEV_DBG(netdev, "Supports LRO for max xid 0x%x\n",
632 lport->lro_xid);
633 } else {
634 lport->lro_enabled = 0;
635 lport->lro_xid = 0;
636 }
637
638 mutex_unlock(&lport->lp_mutex);
639}
640
641/**
Robert Love1875f272009-11-03 11:47:50 -0800642 * fcoe_netdev_config() - Set up net devive for SW FCoE
643 * @lport: The local port that is associated with the net device
644 * @netdev: The associated net device
Vasu Dev7f349142009-03-27 09:06:31 -0700645 *
Robert Love1875f272009-11-03 11:47:50 -0800646 * Must be called after fcoe_lport_config() as it will use local port mutex
Vasu Dev7f349142009-03-27 09:06:31 -0700647 *
Robert Love1875f272009-11-03 11:47:50 -0800648 * Returns: 0 for success
Vasu Dev7f349142009-03-27 09:06:31 -0700649 */
Robert Love1875f272009-11-03 11:47:50 -0800650static int fcoe_netdev_config(struct fc_lport *lport, struct net_device *netdev)
Vasu Dev7f349142009-03-27 09:06:31 -0700651{
652 u32 mfs;
653 u64 wwnn, wwpn;
Chris Leech25024982009-08-25 13:59:35 -0700654 struct fcoe_interface *fcoe;
Chris Leech014f5c32009-08-25 13:59:30 -0700655 struct fcoe_port *port;
Vasu Dev7f349142009-03-27 09:06:31 -0700656
657 /* Setup lport private data to point to fcoe softc */
Robert Love1875f272009-11-03 11:47:50 -0800658 port = lport_priv(lport);
Bhanu Prakash Gollapudi8597ae82011-01-28 16:05:37 -0800659 fcoe = port->priv;
Vasu Dev7f349142009-03-27 09:06:31 -0700660
661 /*
662 * Determine max frame size based on underlying device and optional
663 * user-configured limit. If the MFS is too low, fcoe_link_ok()
664 * will return 0, so do this first.
665 */
Yi Zou7221d7e2009-10-21 16:27:52 -0700666 mfs = netdev->mtu;
667 if (netdev->features & NETIF_F_FCOE_MTU) {
668 mfs = FCOE_MTU;
669 FCOE_NETDEV_DBG(netdev, "Supports FCOE_MTU of %d bytes\n", mfs);
670 }
671 mfs -= (sizeof(struct fcoe_hdr) + sizeof(struct fcoe_crc_eof));
Robert Love1875f272009-11-03 11:47:50 -0800672 if (fc_set_mfs(lport, mfs))
Vasu Dev7f349142009-03-27 09:06:31 -0700673 return -EINVAL;
674
Vasu Dev7f349142009-03-27 09:06:31 -0700675 /* offload features support */
Yi Zou54a5b212010-07-20 15:21:17 -0700676 fcoe_netdev_features_change(lport, netdev);
Vasu Dev7f349142009-03-27 09:06:31 -0700677
Chris Leech014f5c32009-08-25 13:59:30 -0700678 skb_queue_head_init(&port->fcoe_pending_queue);
679 port->fcoe_pending_queue_active = 0;
Robert Love1875f272009-11-03 11:47:50 -0800680 setup_timer(&port->timer, fcoe_queue_timer, (unsigned long)lport);
Vasu Dev7f349142009-03-27 09:06:31 -0700681
Robert Love5e4f8fe2010-05-07 15:18:35 -0700682 fcoe_link_speed_update(lport);
683
Robert Love1875f272009-11-03 11:47:50 -0800684 if (!lport->vport) {
Yi Zoudcece412009-11-20 15:22:21 -0800685 if (fcoe_get_wwn(netdev, &wwnn, NETDEV_FCOE_WWNN))
686 wwnn = fcoe_wwn_from_mac(fcoe->ctlr.ctl_src_addr, 1, 0);
Robert Love1875f272009-11-03 11:47:50 -0800687 fc_set_wwnn(lport, wwnn);
Yi Zoudcece412009-11-20 15:22:21 -0800688 if (fcoe_get_wwn(netdev, &wwpn, NETDEV_FCOE_WWPN))
689 wwpn = fcoe_wwn_from_mac(fcoe->ctlr.ctl_src_addr,
Vasu Devcf4aebc2010-07-20 15:21:22 -0700690 2, 0);
Robert Love1875f272009-11-03 11:47:50 -0800691 fc_set_wwpn(lport, wwpn);
Chris Leech9a057532009-11-03 11:46:40 -0800692 }
Vasu Dev7f349142009-03-27 09:06:31 -0700693
Vasu Dev7f349142009-03-27 09:06:31 -0700694 return 0;
695}
696
697/**
Robert Love1875f272009-11-03 11:47:50 -0800698 * fcoe_shost_config() - Set up the SCSI host associated with a local port
699 * @lport: The local port
Robert Love1875f272009-11-03 11:47:50 -0800700 * @dev: The device associated with the SCSI host
Vasu Dev7f349142009-03-27 09:06:31 -0700701 *
702 * Must be called after fcoe_lport_config() and fcoe_netdev_config()
703 *
Robert Love1875f272009-11-03 11:47:50 -0800704 * Returns: 0 for success
Vasu Dev7f349142009-03-27 09:06:31 -0700705 */
Vasu Dev8ba00a42010-04-09 14:22:54 -0700706static int fcoe_shost_config(struct fc_lport *lport, struct device *dev)
Vasu Dev7f349142009-03-27 09:06:31 -0700707{
708 int rc = 0;
709
710 /* lport scsi host config */
Robert Love1875f272009-11-03 11:47:50 -0800711 lport->host->max_lun = FCOE_MAX_LUN;
712 lport->host->max_id = FCOE_MAX_FCP_TARGET;
713 lport->host->max_channel = 0;
Vasu Devda87bfa2010-04-09 14:22:59 -0700714 lport->host->max_cmd_len = FCOE_MAX_CMD_LEN;
715
Robert Love1875f272009-11-03 11:47:50 -0800716 if (lport->vport)
Yi Zou8ca86f82011-01-28 16:05:11 -0800717 lport->host->transportt = fcoe_vport_scsi_transport;
Chris Leeche9084bb2009-11-03 11:46:34 -0800718 else
Yi Zou8ca86f82011-01-28 16:05:11 -0800719 lport->host->transportt = fcoe_nport_scsi_transport;
Vasu Dev7f349142009-03-27 09:06:31 -0700720
721 /* add the new host to the SCSI-ml */
Robert Love1875f272009-11-03 11:47:50 -0800722 rc = scsi_add_host(lport->host, dev);
Vasu Dev7f349142009-03-27 09:06:31 -0700723 if (rc) {
Robert Love1875f272009-11-03 11:47:50 -0800724 FCOE_NETDEV_DBG(fcoe_netdev(lport), "fcoe_shost_config: "
Robert Loved5488eb2009-06-10 15:30:59 -0700725 "error on scsi_add_host\n");
Vasu Dev7f349142009-03-27 09:06:31 -0700726 return rc;
727 }
Chris Leech9a057532009-11-03 11:46:40 -0800728
Robert Love1875f272009-11-03 11:47:50 -0800729 if (!lport->vport)
Alexey Dobriyan4be929b2010-05-24 14:33:03 -0700730 fc_host_max_npiv_vports(lport->host) = USHRT_MAX;
Chris Leech9a057532009-11-03 11:46:40 -0800731
Robert Love1875f272009-11-03 11:47:50 -0800732 snprintf(fc_host_symbolic_name(lport->host), FC_SYMBOLIC_NAME_SIZE,
Chris Leech5baa17c2009-11-03 11:46:56 -0800733 "%s v%s over %s", FCOE_NAME, FCOE_VERSION,
Robert Love1875f272009-11-03 11:47:50 -0800734 fcoe_netdev(lport)->name);
Vasu Dev7f349142009-03-27 09:06:31 -0700735
736 return 0;
737}
738
Robert Love1875f272009-11-03 11:47:50 -0800739/**
740 * fcoe_oem_match() - The match routine for the offloaded exchange manager
741 * @fp: The I/O frame
Vasu Devd7179682009-07-29 17:05:21 -0700742 *
Robert Love1875f272009-11-03 11:47:50 -0800743 * This routine will be associated with an exchange manager (EM). When
744 * the libfc exchange handling code is looking for an EM to use it will
745 * call this routine and pass it the frame that it wishes to send. This
746 * routine will return True if the associated EM is to be used and False
747 * if the echange code should continue looking for an EM.
748 *
749 * The offload EM that this routine is associated with will handle any
750 * packets that are for SCSI read requests.
751 *
Kiran Patil68cde1e2011-06-20 16:59:15 -0700752 * This has been enhanced to work when FCoE stack is operating in target
753 * mode.
754 *
Robert Love1875f272009-11-03 11:47:50 -0800755 * Returns: True for read types I/O, otherwise returns false.
Vasu Devd7179682009-07-29 17:05:21 -0700756 */
757bool fcoe_oem_match(struct fc_frame *fp)
758{
Kiran Patil68cde1e2011-06-20 16:59:15 -0700759 struct fc_frame_header *fh = fc_frame_header_get(fp);
760 struct fcp_cmnd *fcp;
761
762 if (fc_fcp_is_read(fr_fsp(fp)) &&
763 (fr_fsp(fp)->data_len > fcoe_ddp_min))
764 return true;
765 else if (!(ntoh24(fh->fh_f_ctl) & FC_FC_EX_CTX)) {
766 fcp = fc_frame_payload_get(fp, sizeof(*fcp));
767 if (ntohs(fh->fh_rx_id) == FC_XID_UNKNOWN &&
768 fcp && (ntohl(fcp->fc_dl) > fcoe_ddp_min) &&
769 (fcp->fc_flags & FCP_CFL_WRDATA))
770 return true;
771 }
772 return false;
Vasu Devd7179682009-07-29 17:05:21 -0700773}
774
Vasu Dev7f349142009-03-27 09:06:31 -0700775/**
Robert Love1875f272009-11-03 11:47:50 -0800776 * fcoe_em_config() - Allocate and configure an exchange manager
777 * @lport: The local port that the new EM will be associated with
Vasu Dev7f349142009-03-27 09:06:31 -0700778 *
Robert Love1875f272009-11-03 11:47:50 -0800779 * Returns: 0 on success
Vasu Dev7f349142009-03-27 09:06:31 -0700780 */
Robert Love1875f272009-11-03 11:47:50 -0800781static inline int fcoe_em_config(struct fc_lport *lport)
Vasu Dev7f349142009-03-27 09:06:31 -0700782{
Robert Love1875f272009-11-03 11:47:50 -0800783 struct fcoe_port *port = lport_priv(lport);
Bhanu Prakash Gollapudi8597ae82011-01-28 16:05:37 -0800784 struct fcoe_interface *fcoe = port->priv;
Chris Leech25024982009-08-25 13:59:35 -0700785 struct fcoe_interface *oldfcoe = NULL;
Vasu Dev1d1b88d2009-07-29 17:05:45 -0700786 struct net_device *old_real_dev, *cur_real_dev;
Vasu Devd7179682009-07-29 17:05:21 -0700787 u16 min_xid = FCOE_MIN_XID;
788 u16 max_xid = FCOE_MAX_XID;
789
790 /*
791 * Check if need to allocate an em instance for
792 * offload exchange ids to be shared across all VN_PORTs/lport.
793 */
Robert Love1875f272009-11-03 11:47:50 -0800794 if (!lport->lro_enabled || !lport->lro_xid ||
795 (lport->lro_xid >= max_xid)) {
796 lport->lro_xid = 0;
Vasu Devd7179682009-07-29 17:05:21 -0700797 goto skip_oem;
798 }
799
800 /*
801 * Reuse existing offload em instance in case
Vasu Dev1d1b88d2009-07-29 17:05:45 -0700802 * it is already allocated on real eth device
Vasu Devd7179682009-07-29 17:05:21 -0700803 */
Chris Leech25024982009-08-25 13:59:35 -0700804 if (fcoe->netdev->priv_flags & IFF_802_1Q_VLAN)
805 cur_real_dev = vlan_dev_real_dev(fcoe->netdev);
Vasu Dev1d1b88d2009-07-29 17:05:45 -0700806 else
Chris Leech25024982009-08-25 13:59:35 -0700807 cur_real_dev = fcoe->netdev;
Vasu Dev1d1b88d2009-07-29 17:05:45 -0700808
Chris Leech25024982009-08-25 13:59:35 -0700809 list_for_each_entry(oldfcoe, &fcoe_hostlist, list) {
Chris Leech25024982009-08-25 13:59:35 -0700810 if (oldfcoe->netdev->priv_flags & IFF_802_1Q_VLAN)
811 old_real_dev = vlan_dev_real_dev(oldfcoe->netdev);
Vasu Dev1d1b88d2009-07-29 17:05:45 -0700812 else
Chris Leech25024982009-08-25 13:59:35 -0700813 old_real_dev = oldfcoe->netdev;
Vasu Dev1d1b88d2009-07-29 17:05:45 -0700814
815 if (cur_real_dev == old_real_dev) {
Chris Leech991cbb62009-08-25 13:59:51 -0700816 fcoe->oem = oldfcoe->oem;
Vasu Devd7179682009-07-29 17:05:21 -0700817 break;
818 }
819 }
820
Chris Leech991cbb62009-08-25 13:59:51 -0700821 if (fcoe->oem) {
Robert Love1875f272009-11-03 11:47:50 -0800822 if (!fc_exch_mgr_add(lport, fcoe->oem, fcoe_oem_match)) {
Vasu Devd7179682009-07-29 17:05:21 -0700823 printk(KERN_ERR "fcoe_em_config: failed to add "
824 "offload em:%p on interface:%s\n",
Chris Leech991cbb62009-08-25 13:59:51 -0700825 fcoe->oem, fcoe->netdev->name);
Vasu Devd7179682009-07-29 17:05:21 -0700826 return -ENOMEM;
827 }
828 } else {
Robert Love1875f272009-11-03 11:47:50 -0800829 fcoe->oem = fc_exch_mgr_alloc(lport, FC_CLASS_3,
830 FCOE_MIN_XID, lport->lro_xid,
831 fcoe_oem_match);
Chris Leech991cbb62009-08-25 13:59:51 -0700832 if (!fcoe->oem) {
Vasu Devd7179682009-07-29 17:05:21 -0700833 printk(KERN_ERR "fcoe_em_config: failed to allocate "
834 "em for offload exches on interface:%s\n",
Chris Leech25024982009-08-25 13:59:35 -0700835 fcoe->netdev->name);
Vasu Devd7179682009-07-29 17:05:21 -0700836 return -ENOMEM;
837 }
838 }
839
840 /*
841 * Exclude offload EM xid range from next EM xid range.
842 */
Robert Love1875f272009-11-03 11:47:50 -0800843 min_xid += lport->lro_xid + 1;
Vasu Devd7179682009-07-29 17:05:21 -0700844
845skip_oem:
Robert Love1875f272009-11-03 11:47:50 -0800846 if (!fc_exch_mgr_alloc(lport, FC_CLASS_3, min_xid, max_xid, NULL)) {
Vasu Devd7179682009-07-29 17:05:21 -0700847 printk(KERN_ERR "fcoe_em_config: failed to "
Chris Leech25024982009-08-25 13:59:35 -0700848 "allocate em on interface %s\n", fcoe->netdev->name);
Vasu Dev7f349142009-03-27 09:06:31 -0700849 return -ENOMEM;
Vasu Devd7179682009-07-29 17:05:21 -0700850 }
Vasu Dev7f349142009-03-27 09:06:31 -0700851
852 return 0;
853}
854
855/**
Robert Love1875f272009-11-03 11:47:50 -0800856 * fcoe_if_destroy() - Tear down a SW FCoE instance
857 * @lport: The local port to be destroyed
Vasu Dev34ce27b2010-05-07 15:18:46 -0700858 *
Vasu Dev7f349142009-03-27 09:06:31 -0700859 */
Chris Leechaf7f85d2009-08-25 13:59:24 -0700860static void fcoe_if_destroy(struct fc_lport *lport)
Vasu Dev7f349142009-03-27 09:06:31 -0700861{
Chris Leech54b649f2009-08-25 14:00:07 -0700862 /* Free queued packets for the per-CPU receive threads */
863 fcoe_percpu_clean(lport);
864
Vasu Dev7f349142009-03-27 09:06:31 -0700865 /* Detach from the scsi-ml */
Chris Leechaf7f85d2009-08-25 13:59:24 -0700866 fc_remove_host(lport->host);
867 scsi_remove_host(lport->host);
Vasu Dev7f349142009-03-27 09:06:31 -0700868
Yi Zou80e736f2010-11-30 16:18:07 -0800869 /* Destroy lport scsi_priv */
870 fc_fcp_destroy(lport);
871
Vasu Dev7f349142009-03-27 09:06:31 -0700872 /* There are no more rports or I/O, free the EM */
Chris Leechaf7f85d2009-08-25 13:59:24 -0700873 fc_exch_mgr_free(lport);
Vasu Dev7f349142009-03-27 09:06:31 -0700874
Vasu Dev7f349142009-03-27 09:06:31 -0700875 /* Free memory used by statistical counters */
Chris Leechaf7f85d2009-08-25 13:59:24 -0700876 fc_lport_free_stats(lport);
Vasu Dev7f349142009-03-27 09:06:31 -0700877
Chris Leech2e70e242009-08-25 14:00:23 -0700878 /* Release the Scsi_Host */
Chris Leechaf7f85d2009-08-25 13:59:24 -0700879 scsi_host_put(lport->host);
Vasu Dev7f349142009-03-27 09:06:31 -0700880}
881
Robert Love1875f272009-11-03 11:47:50 -0800882/**
883 * fcoe_ddp_setup() - Call a LLD's ddp_setup through the net device
884 * @lport: The local port to setup DDP for
885 * @xid: The exchange ID for this DDP transfer
886 * @sgl: The scatterlist describing this transfer
887 * @sgc: The number of sg items
Vasu Dev7f349142009-03-27 09:06:31 -0700888 *
Robert Love1875f272009-11-03 11:47:50 -0800889 * Returns: 0 if the DDP context was not configured
Vasu Dev7f349142009-03-27 09:06:31 -0700890 */
Robert Love1875f272009-11-03 11:47:50 -0800891static int fcoe_ddp_setup(struct fc_lport *lport, u16 xid,
892 struct scatterlist *sgl, unsigned int sgc)
Vasu Dev7f349142009-03-27 09:06:31 -0700893{
Robert Love1875f272009-11-03 11:47:50 -0800894 struct net_device *netdev = fcoe_netdev(lport);
Vasu Dev7f349142009-03-27 09:06:31 -0700895
Robert Love1875f272009-11-03 11:47:50 -0800896 if (netdev->netdev_ops->ndo_fcoe_ddp_setup)
897 return netdev->netdev_ops->ndo_fcoe_ddp_setup(netdev,
898 xid, sgl,
899 sgc);
Vasu Dev7f349142009-03-27 09:06:31 -0700900
901 return 0;
902}
903
Vasu Dev7f349142009-03-27 09:06:31 -0700904/**
Robert Love1875f272009-11-03 11:47:50 -0800905 * fcoe_ddp_done() - Call a LLD's ddp_done through the net device
906 * @lport: The local port to complete DDP on
907 * @xid: The exchange ID for this DDP transfer
Vasu Dev7f349142009-03-27 09:06:31 -0700908 *
Robert Love1875f272009-11-03 11:47:50 -0800909 * Returns: the length of data that have been completed by DDP
910 */
911static int fcoe_ddp_done(struct fc_lport *lport, u16 xid)
912{
913 struct net_device *netdev = fcoe_netdev(lport);
914
915 if (netdev->netdev_ops->ndo_fcoe_ddp_done)
916 return netdev->netdev_ops->ndo_fcoe_ddp_done(netdev, xid);
917 return 0;
918}
919
920/**
921 * fcoe_if_create() - Create a FCoE instance on an interface
922 * @fcoe: The FCoE interface to create a local port on
923 * @parent: The device pointer to be the parent in sysfs for the SCSI host
924 * @npiv: Indicates if the port is a vport or not
Vasu Dev7f349142009-03-27 09:06:31 -0700925 *
Robert Love1875f272009-11-03 11:47:50 -0800926 * Creates a fc_lport instance and a Scsi_Host instance and configure them.
927 *
928 * Returns: The allocated fc_lport or an error pointer
Vasu Dev7f349142009-03-27 09:06:31 -0700929 */
Chris Leech030f4e02009-08-25 14:00:02 -0700930static struct fc_lport *fcoe_if_create(struct fcoe_interface *fcoe,
Chris Leech9a057532009-11-03 11:46:40 -0800931 struct device *parent, int npiv)
Vasu Dev7f349142009-03-27 09:06:31 -0700932{
Robert Love1875f272009-11-03 11:47:50 -0800933 struct net_device *netdev = fcoe->netdev;
Vasu Dev72fa3962011-02-25 15:03:01 -0800934 struct fc_lport *lport, *n_port;
Chris Leech014f5c32009-08-25 13:59:30 -0700935 struct fcoe_port *port;
Vasu Dev72fa3962011-02-25 15:03:01 -0800936 struct Scsi_Host *shost;
Robert Love1875f272009-11-03 11:47:50 -0800937 int rc;
Chris Leech9a057532009-11-03 11:46:40 -0800938 /*
939 * parent is only a vport if npiv is 1,
940 * but we'll only use vport in that case so go ahead and set it
941 */
942 struct fc_vport *vport = dev_to_vport(parent);
Vasu Dev7f349142009-03-27 09:06:31 -0700943
Robert Loved5488eb2009-06-10 15:30:59 -0700944 FCOE_NETDEV_DBG(netdev, "Create Interface\n");
Vasu Dev7f349142009-03-27 09:06:31 -0700945
Vasu Dev72fa3962011-02-25 15:03:01 -0800946 if (!npiv)
947 lport = libfc_host_alloc(&fcoe_shost_template, sizeof(*port));
948 else
949 lport = libfc_vport_create(vport, sizeof(*port));
950
Chris Leech86221962009-11-03 11:46:08 -0800951 if (!lport) {
Chris Leech014f5c32009-08-25 13:59:30 -0700952 FCOE_NETDEV_DBG(netdev, "Could not allocate host structure\n");
953 rc = -ENOMEM;
Chris Leech030f4e02009-08-25 14:00:02 -0700954 goto out;
Chris Leech014f5c32009-08-25 13:59:30 -0700955 }
Chris Leech014f5c32009-08-25 13:59:30 -0700956 port = lport_priv(lport);
Chris Leech2e70e242009-08-25 14:00:23 -0700957 port->lport = lport;
Bhanu Prakash Gollapudi8597ae82011-01-28 16:05:37 -0800958 port->priv = fcoe;
959 port->max_queue_depth = FCOE_MAX_QUEUE_DEPTH;
960 port->min_queue_depth = FCOE_MIN_QUEUE_DEPTH;
Chris Leech2e70e242009-08-25 14:00:23 -0700961 INIT_WORK(&port->destroy_work, fcoe_destroy_work);
Vasu Dev7f349142009-03-27 09:06:31 -0700962
Robert Love1875f272009-11-03 11:47:50 -0800963 /* configure a fc_lport including the exchange manager */
Chris Leechaf7f85d2009-08-25 13:59:24 -0700964 rc = fcoe_lport_config(lport);
Vasu Dev7f349142009-03-27 09:06:31 -0700965 if (rc) {
Robert Loved5488eb2009-06-10 15:30:59 -0700966 FCOE_NETDEV_DBG(netdev, "Could not configure lport for the "
967 "interface\n");
Vasu Dev7f349142009-03-27 09:06:31 -0700968 goto out_host_put;
969 }
970
Chris Leech9a057532009-11-03 11:46:40 -0800971 if (npiv) {
Chris Leech9f8f3aa2010-04-09 14:23:16 -0700972 FCOE_NETDEV_DBG(netdev, "Setting vport names, "
973 "%16.16llx %16.16llx\n",
Robert Love1875f272009-11-03 11:47:50 -0800974 vport->node_name, vport->port_name);
Chris Leech9a057532009-11-03 11:46:40 -0800975 fc_set_wwnn(lport, vport->node_name);
976 fc_set_wwpn(lport, vport->port_name);
977 }
978
Vasu Devab6b85c2009-05-17 12:33:08 +0000979 /* configure lport network properties */
Chris Leechaf7f85d2009-08-25 13:59:24 -0700980 rc = fcoe_netdev_config(lport, netdev);
Vasu Devab6b85c2009-05-17 12:33:08 +0000981 if (rc) {
Robert Loved5488eb2009-06-10 15:30:59 -0700982 FCOE_NETDEV_DBG(netdev, "Could not configure netdev for the "
983 "interface\n");
Chris Leech54b649f2009-08-25 14:00:07 -0700984 goto out_lp_destroy;
Vasu Devab6b85c2009-05-17 12:33:08 +0000985 }
Joe Eykholt97c83892009-03-17 11:42:40 -0700986
Vasu Dev7f349142009-03-27 09:06:31 -0700987 /* configure lport scsi host properties */
Vasu Dev8ba00a42010-04-09 14:22:54 -0700988 rc = fcoe_shost_config(lport, parent);
Vasu Dev7f349142009-03-27 09:06:31 -0700989 if (rc) {
Robert Loved5488eb2009-06-10 15:30:59 -0700990 FCOE_NETDEV_DBG(netdev, "Could not configure shost for the "
991 "interface\n");
Chris Leech54b649f2009-08-25 14:00:07 -0700992 goto out_lp_destroy;
Vasu Dev7f349142009-03-27 09:06:31 -0700993 }
994
Vasu Dev7f349142009-03-27 09:06:31 -0700995 /* Initialize the library */
Joe Eykholte10f8c62010-07-20 15:20:30 -0700996 rc = fcoe_libfc_config(lport, &fcoe->ctlr, &fcoe_libfc_fcn_templ, 1);
Vasu Dev7f349142009-03-27 09:06:31 -0700997 if (rc) {
Robert Loved5488eb2009-06-10 15:30:59 -0700998 FCOE_NETDEV_DBG(netdev, "Could not configure libfc for the "
999 "interface\n");
Vasu Dev7f349142009-03-27 09:06:31 -07001000 goto out_lp_destroy;
1001 }
1002
Vasu Dev72fa3962011-02-25 15:03:01 -08001003 /*
1004 * fcoe_em_alloc() and fcoe_hostlist_add() both
1005 * need to be atomic with respect to other changes to the
1006 * hostlist since fcoe_em_alloc() looks for an existing EM
1007 * instance on host list updated by fcoe_hostlist_add().
1008 *
1009 * This is currently handled through the fcoe_config_mutex
1010 * begin held.
1011 */
1012 if (!npiv)
Chris Leech9a057532009-11-03 11:46:40 -08001013 /* lport exch manager allocation */
1014 rc = fcoe_em_config(lport);
Vasu Dev72fa3962011-02-25 15:03:01 -08001015 else {
1016 shost = vport_to_shost(vport);
1017 n_port = shost_priv(shost);
1018 rc = fc_exch_mgr_list_clone(n_port, lport);
1019 }
1020
1021 if (rc) {
1022 FCOE_NETDEV_DBG(netdev, "Could not configure the EM\n");
1023 goto out_lp_destroy;
Vasu Dev96316092009-07-29 17:05:00 -07001024 }
1025
Chris Leech030f4e02009-08-25 14:00:02 -07001026 fcoe_interface_get(fcoe);
Chris Leechaf7f85d2009-08-25 13:59:24 -07001027 return lport;
Vasu Dev7f349142009-03-27 09:06:31 -07001028
1029out_lp_destroy:
Chris Leechaf7f85d2009-08-25 13:59:24 -07001030 fc_exch_mgr_free(lport);
Vasu Dev7f349142009-03-27 09:06:31 -07001031out_host_put:
Chris Leechaf7f85d2009-08-25 13:59:24 -07001032 scsi_host_put(lport->host);
1033out:
1034 return ERR_PTR(rc);
Vasu Dev7f349142009-03-27 09:06:31 -07001035}
1036
1037/**
Robert Love1875f272009-11-03 11:47:50 -08001038 * fcoe_if_init() - Initialization routine for fcoe.ko
Vasu Dev7f349142009-03-27 09:06:31 -07001039 *
Robert Love1875f272009-11-03 11:47:50 -08001040 * Attaches the SW FCoE transport to the FC transport
1041 *
1042 * Returns: 0 on success
Vasu Dev7f349142009-03-27 09:06:31 -07001043 */
1044static int __init fcoe_if_init(void)
1045{
1046 /* attach to scsi transport */
Yi Zou8ca86f82011-01-28 16:05:11 -08001047 fcoe_nport_scsi_transport =
1048 fc_attach_transport(&fcoe_nport_fc_functions);
1049 fcoe_vport_scsi_transport =
1050 fc_attach_transport(&fcoe_vport_fc_functions);
Vasu Dev7f349142009-03-27 09:06:31 -07001051
Yi Zou8ca86f82011-01-28 16:05:11 -08001052 if (!fcoe_nport_scsi_transport) {
Robert Loved5488eb2009-06-10 15:30:59 -07001053 printk(KERN_ERR "fcoe: Failed to attach to the FC transport\n");
Vasu Dev7f349142009-03-27 09:06:31 -07001054 return -ENODEV;
1055 }
1056
1057 return 0;
1058}
1059
1060/**
Robert Love1875f272009-11-03 11:47:50 -08001061 * fcoe_if_exit() - Tear down fcoe.ko
Vasu Dev7f349142009-03-27 09:06:31 -07001062 *
Robert Love1875f272009-11-03 11:47:50 -08001063 * Detaches the SW FCoE transport from the FC transport
1064 *
1065 * Returns: 0 on success
Vasu Dev7f349142009-03-27 09:06:31 -07001066 */
1067int __exit fcoe_if_exit(void)
1068{
Yi Zou8ca86f82011-01-28 16:05:11 -08001069 fc_release_transport(fcoe_nport_scsi_transport);
1070 fc_release_transport(fcoe_vport_scsi_transport);
1071 fcoe_nport_scsi_transport = NULL;
1072 fcoe_vport_scsi_transport = NULL;
Vasu Dev7f349142009-03-27 09:06:31 -07001073 return 0;
1074}
1075
Robert Love85b4aa42008-12-09 15:10:24 -08001076/**
Robert Love1875f272009-11-03 11:47:50 -08001077 * fcoe_percpu_thread_create() - Create a receive thread for an online CPU
1078 * @cpu: The CPU index of the CPU to create a receive thread for
Robert Love8976f422009-03-17 11:41:46 -07001079 */
1080static void fcoe_percpu_thread_create(unsigned int cpu)
1081{
1082 struct fcoe_percpu_s *p;
1083 struct task_struct *thread;
1084
1085 p = &per_cpu(fcoe_percpu, cpu);
1086
1087 thread = kthread_create(fcoe_percpu_receive_thread,
1088 (void *)p, "fcoethread/%d", cpu);
1089
Joe Eykholte7a51992009-08-25 14:04:08 -07001090 if (likely(!IS_ERR(thread))) {
Robert Love8976f422009-03-17 11:41:46 -07001091 kthread_bind(thread, cpu);
1092 wake_up_process(thread);
1093
1094 spin_lock_bh(&p->fcoe_rx_list.lock);
1095 p->thread = thread;
1096 spin_unlock_bh(&p->fcoe_rx_list.lock);
1097 }
1098}
1099
1100/**
Robert Love1875f272009-11-03 11:47:50 -08001101 * fcoe_percpu_thread_destroy() - Remove the receive thread of a CPU
1102 * @cpu: The CPU index of the CPU whose receive thread is to be destroyed
Robert Love8976f422009-03-17 11:41:46 -07001103 *
1104 * Destroys a per-CPU Rx thread. Any pending skbs are moved to the
1105 * current CPU's Rx thread. If the thread being destroyed is bound to
1106 * the CPU processing this context the skbs will be freed.
1107 */
1108static void fcoe_percpu_thread_destroy(unsigned int cpu)
1109{
1110 struct fcoe_percpu_s *p;
1111 struct task_struct *thread;
1112 struct page *crc_eof;
1113 struct sk_buff *skb;
1114#ifdef CONFIG_SMP
1115 struct fcoe_percpu_s *p0;
Joe Eykholtf018b732010-03-12 16:08:55 -08001116 unsigned targ_cpu = get_cpu();
Robert Love8976f422009-03-17 11:41:46 -07001117#endif /* CONFIG_SMP */
1118
Robert Loved5488eb2009-06-10 15:30:59 -07001119 FCOE_DBG("Destroying receive thread for CPU %d\n", cpu);
Robert Love8976f422009-03-17 11:41:46 -07001120
1121 /* Prevent any new skbs from being queued for this CPU. */
1122 p = &per_cpu(fcoe_percpu, cpu);
1123 spin_lock_bh(&p->fcoe_rx_list.lock);
1124 thread = p->thread;
1125 p->thread = NULL;
1126 crc_eof = p->crc_eof_page;
1127 p->crc_eof_page = NULL;
1128 p->crc_eof_offset = 0;
1129 spin_unlock_bh(&p->fcoe_rx_list.lock);
1130
1131#ifdef CONFIG_SMP
1132 /*
1133 * Don't bother moving the skb's if this context is running
1134 * on the same CPU that is having its thread destroyed. This
1135 * can easily happen when the module is removed.
1136 */
1137 if (cpu != targ_cpu) {
1138 p0 = &per_cpu(fcoe_percpu, targ_cpu);
1139 spin_lock_bh(&p0->fcoe_rx_list.lock);
1140 if (p0->thread) {
Robert Loved5488eb2009-06-10 15:30:59 -07001141 FCOE_DBG("Moving frames from CPU %d to CPU %d\n",
1142 cpu, targ_cpu);
Robert Love8976f422009-03-17 11:41:46 -07001143
1144 while ((skb = __skb_dequeue(&p->fcoe_rx_list)) != NULL)
1145 __skb_queue_tail(&p0->fcoe_rx_list, skb);
1146 spin_unlock_bh(&p0->fcoe_rx_list.lock);
1147 } else {
1148 /*
1149 * The targeted CPU is not initialized and cannot accept
Robert Love1875f272009-11-03 11:47:50 -08001150 * new skbs. Unlock the targeted CPU and drop the skbs
Robert Love8976f422009-03-17 11:41:46 -07001151 * on the CPU that is going offline.
1152 */
1153 while ((skb = __skb_dequeue(&p->fcoe_rx_list)) != NULL)
1154 kfree_skb(skb);
1155 spin_unlock_bh(&p0->fcoe_rx_list.lock);
1156 }
1157 } else {
1158 /*
1159 * This scenario occurs when the module is being removed
1160 * and all threads are being destroyed. skbs will continue
1161 * to be shifted from the CPU thread that is being removed
1162 * to the CPU thread associated with the CPU that is processing
1163 * the module removal. Once there is only one CPU Rx thread it
1164 * will reach this case and we will drop all skbs and later
1165 * stop the thread.
1166 */
1167 spin_lock_bh(&p->fcoe_rx_list.lock);
1168 while ((skb = __skb_dequeue(&p->fcoe_rx_list)) != NULL)
1169 kfree_skb(skb);
1170 spin_unlock_bh(&p->fcoe_rx_list.lock);
1171 }
Joe Eykholtf018b732010-03-12 16:08:55 -08001172 put_cpu();
Robert Love8976f422009-03-17 11:41:46 -07001173#else
1174 /*
Chris Leechdd3fd722009-04-21 16:27:36 -07001175 * This a non-SMP scenario where the singular Rx thread is
Robert Love8976f422009-03-17 11:41:46 -07001176 * being removed. Free all skbs and stop the thread.
1177 */
1178 spin_lock_bh(&p->fcoe_rx_list.lock);
1179 while ((skb = __skb_dequeue(&p->fcoe_rx_list)) != NULL)
1180 kfree_skb(skb);
1181 spin_unlock_bh(&p->fcoe_rx_list.lock);
1182#endif
1183
1184 if (thread)
1185 kthread_stop(thread);
1186
1187 if (crc_eof)
1188 put_page(crc_eof);
1189}
1190
1191/**
Robert Love1875f272009-11-03 11:47:50 -08001192 * fcoe_cpu_callback() - Handler for CPU hotplug events
1193 * @nfb: The callback data block
1194 * @action: The event triggering the callback
1195 * @hcpu: The index of the CPU that the event is for
Robert Love8976f422009-03-17 11:41:46 -07001196 *
Robert Love1875f272009-11-03 11:47:50 -08001197 * This creates or destroys per-CPU data for fcoe
Robert Love8976f422009-03-17 11:41:46 -07001198 *
1199 * Returns NOTIFY_OK always.
1200 */
1201static int fcoe_cpu_callback(struct notifier_block *nfb,
1202 unsigned long action, void *hcpu)
1203{
1204 unsigned cpu = (unsigned long)hcpu;
1205
1206 switch (action) {
1207 case CPU_ONLINE:
1208 case CPU_ONLINE_FROZEN:
Robert Loved5488eb2009-06-10 15:30:59 -07001209 FCOE_DBG("CPU %x online: Create Rx thread\n", cpu);
Robert Love8976f422009-03-17 11:41:46 -07001210 fcoe_percpu_thread_create(cpu);
1211 break;
1212 case CPU_DEAD:
1213 case CPU_DEAD_FROZEN:
Robert Loved5488eb2009-06-10 15:30:59 -07001214 FCOE_DBG("CPU %x offline: Remove Rx thread\n", cpu);
Robert Love8976f422009-03-17 11:41:46 -07001215 fcoe_percpu_thread_destroy(cpu);
1216 break;
1217 default:
1218 break;
1219 }
1220 return NOTIFY_OK;
1221}
1222
Robert Love8976f422009-03-17 11:41:46 -07001223/**
Robert Love1875f272009-11-03 11:47:50 -08001224 * fcoe_rcv() - Receive packets from a net device
1225 * @skb: The received packet
1226 * @netdev: The net device that the packet was received on
1227 * @ptype: The packet type context
1228 * @olddev: The last device net device
Robert Love85b4aa42008-12-09 15:10:24 -08001229 *
Robert Love1875f272009-11-03 11:47:50 -08001230 * This routine is called by NET_RX_SOFTIRQ. It receives a packet, builds a
1231 * FC frame and passes the frame to libfc.
Robert Love85b4aa42008-12-09 15:10:24 -08001232 *
1233 * Returns: 0 for success
Robert Love34f42a02009-02-27 10:55:45 -08001234 */
Robert Love1875f272009-11-03 11:47:50 -08001235int fcoe_rcv(struct sk_buff *skb, struct net_device *netdev,
Robert Love85b4aa42008-12-09 15:10:24 -08001236 struct packet_type *ptype, struct net_device *olddev)
1237{
Robert Love1875f272009-11-03 11:47:50 -08001238 struct fc_lport *lport;
Robert Love85b4aa42008-12-09 15:10:24 -08001239 struct fcoe_rcv_info *fr;
Chris Leech259ad852009-08-25 13:59:41 -07001240 struct fcoe_interface *fcoe;
Robert Love85b4aa42008-12-09 15:10:24 -08001241 struct fc_frame_header *fh;
Robert Love85b4aa42008-12-09 15:10:24 -08001242 struct fcoe_percpu_s *fps;
Vasu Dev519e5132010-07-20 15:19:32 -07001243 struct ethhdr *eh;
Vasu Devb2f00912009-08-25 13:58:53 -07001244 unsigned int cpu;
Robert Love85b4aa42008-12-09 15:10:24 -08001245
Chris Leech259ad852009-08-25 13:59:41 -07001246 fcoe = container_of(ptype, struct fcoe_interface, fcoe_packet_type);
Robert Love1875f272009-11-03 11:47:50 -08001247 lport = fcoe->ctlr.lp;
1248 if (unlikely(!lport)) {
1249 FCOE_NETDEV_DBG(netdev, "Cannot find hba structure");
Robert Love85b4aa42008-12-09 15:10:24 -08001250 goto err2;
1251 }
Robert Love1875f272009-11-03 11:47:50 -08001252 if (!lport->link_up)
Joe Eykholt97c83892009-03-17 11:42:40 -07001253 goto err2;
Robert Love85b4aa42008-12-09 15:10:24 -08001254
Robert Love1875f272009-11-03 11:47:50 -08001255 FCOE_NETDEV_DBG(netdev, "skb_info: len:%d data_len:%d head:%p "
Robert Loved5488eb2009-06-10 15:30:59 -07001256 "data:%p tail:%p end:%p sum:%d dev:%s",
1257 skb->len, skb->data_len, skb->head, skb->data,
1258 skb_tail_pointer(skb), skb_end_pointer(skb),
1259 skb->csum, skb->dev ? skb->dev->name : "<NULL>");
Robert Love85b4aa42008-12-09 15:10:24 -08001260
Vasu Dev519e5132010-07-20 15:19:32 -07001261 eh = eth_hdr(skb);
Vasu Dev519e5132010-07-20 15:19:32 -07001262
1263 if (is_fip_mode(&fcoe->ctlr) &&
1264 compare_ether_addr(eh->h_source, fcoe->ctlr.dest_addr)) {
1265 FCOE_NETDEV_DBG(netdev, "wrong source mac address:%pM\n",
1266 eh->h_source);
Robert Love85b4aa42008-12-09 15:10:24 -08001267 goto err;
1268 }
1269
1270 /*
1271 * Check for minimum frame length, and make sure required FCoE
1272 * and FC headers are pulled into the linear data area.
1273 */
1274 if (unlikely((skb->len < FCOE_MIN_FRAME) ||
Robert Love1875f272009-11-03 11:47:50 -08001275 !pskb_may_pull(skb, FCOE_HEADER_LEN)))
Robert Love85b4aa42008-12-09 15:10:24 -08001276 goto err;
1277
1278 skb_set_transport_header(skb, sizeof(struct fcoe_hdr));
1279 fh = (struct fc_frame_header *) skb_transport_header(skb);
1280
Robert Love0ee31cb2010-10-08 17:12:46 -07001281 if (ntoh24(&eh->h_dest[3]) != ntoh24(fh->fh_d_id)) {
1282 FCOE_NETDEV_DBG(netdev, "FC frame d_id mismatch with MAC:%pM\n",
1283 eh->h_dest);
1284 goto err;
1285 }
1286
Robert Love85b4aa42008-12-09 15:10:24 -08001287 fr = fcoe_dev_from_skb(skb);
Robert Love1875f272009-11-03 11:47:50 -08001288 fr->fr_dev = lport;
Robert Love85b4aa42008-12-09 15:10:24 -08001289 fr->ptype = ptype;
Robert Love5e5e92d2009-03-17 11:41:35 -07001290
Robert Love85b4aa42008-12-09 15:10:24 -08001291 /*
Vasu Devb2f00912009-08-25 13:58:53 -07001292 * In case the incoming frame's exchange is originated from
1293 * the initiator, then received frame's exchange id is ANDed
1294 * with fc_cpu_mask bits to get the same cpu on which exchange
1295 * was originated, otherwise just use the current cpu.
Robert Love85b4aa42008-12-09 15:10:24 -08001296 */
Vasu Devb2f00912009-08-25 13:58:53 -07001297 if (ntoh24(fh->fh_f_ctl) & FC_FC_EX_CTX)
1298 cpu = ntohs(fh->fh_ox_id) & fc_cpu_mask;
1299 else
1300 cpu = smp_processor_id();
Robert Love5e5e92d2009-03-17 11:41:35 -07001301
Robert Love8976f422009-03-17 11:41:46 -07001302 fps = &per_cpu(fcoe_percpu, cpu);
Robert Love85b4aa42008-12-09 15:10:24 -08001303 spin_lock_bh(&fps->fcoe_rx_list.lock);
Robert Love8976f422009-03-17 11:41:46 -07001304 if (unlikely(!fps->thread)) {
1305 /*
1306 * The targeted CPU is not ready, let's target
1307 * the first CPU now. For non-SMP systems this
1308 * will check the same CPU twice.
1309 */
Robert Love1875f272009-11-03 11:47:50 -08001310 FCOE_NETDEV_DBG(netdev, "CPU is online, but no receive thread "
Robert Loved5488eb2009-06-10 15:30:59 -07001311 "ready for incoming skb- using first online "
1312 "CPU.\n");
Robert Love8976f422009-03-17 11:41:46 -07001313
1314 spin_unlock_bh(&fps->fcoe_rx_list.lock);
Rusty Russell69571772009-12-17 11:43:14 -06001315 cpu = cpumask_first(cpu_online_mask);
Robert Love8976f422009-03-17 11:41:46 -07001316 fps = &per_cpu(fcoe_percpu, cpu);
1317 spin_lock_bh(&fps->fcoe_rx_list.lock);
1318 if (!fps->thread) {
1319 spin_unlock_bh(&fps->fcoe_rx_list.lock);
1320 goto err;
1321 }
1322 }
1323
1324 /*
1325 * We now have a valid CPU that we're targeting for
1326 * this skb. We also have this receive thread locked,
1327 * so we're free to queue skbs into it's queue.
1328 */
Robert Love85b4aa42008-12-09 15:10:24 -08001329
Chris Leech859b7b62009-11-20 14:54:47 -08001330 /* If this is a SCSI-FCP frame, and this is already executing on the
1331 * correct CPU, and the queue for this CPU is empty, then go ahead
1332 * and process the frame directly in the softirq context.
1333 * This lets us process completions without context switching from the
1334 * NET_RX softirq, to our receive processing thread, and then back to
1335 * BLOCK softirq context.
1336 */
1337 if (fh->fh_type == FC_TYPE_FCP &&
1338 cpu == smp_processor_id() &&
1339 skb_queue_empty(&fps->fcoe_rx_list)) {
1340 spin_unlock_bh(&fps->fcoe_rx_list.lock);
1341 fcoe_recv_frame(skb);
1342 } else {
1343 __skb_queue_tail(&fps->fcoe_rx_list, skb);
1344 if (fps->fcoe_rx_list.qlen == 1)
1345 wake_up_process(fps->thread);
1346 spin_unlock_bh(&fps->fcoe_rx_list.lock);
1347 }
Robert Love85b4aa42008-12-09 15:10:24 -08001348
1349 return 0;
1350err:
Joe Eykholtf018b732010-03-12 16:08:55 -08001351 per_cpu_ptr(lport->dev_stats, get_cpu())->ErrorFrames++;
1352 put_cpu();
Robert Love85b4aa42008-12-09 15:10:24 -08001353err2:
1354 kfree_skb(skb);
1355 return -1;
1356}
Robert Love85b4aa42008-12-09 15:10:24 -08001357
1358/**
Bhanu Prakash Gollapudi8597ae82011-01-28 16:05:37 -08001359 * fcoe_alloc_paged_crc_eof() - Allocate a page to be used for the trailer CRC
Robert Love1875f272009-11-03 11:47:50 -08001360 * @skb: The packet to be transmitted
1361 * @tlen: The total length of the trailer
1362 *
Robert Love85b4aa42008-12-09 15:10:24 -08001363 * Returns: 0 for success
Robert Love34f42a02009-02-27 10:55:45 -08001364 */
Bhanu Prakash Gollapudi8597ae82011-01-28 16:05:37 -08001365static int fcoe_alloc_paged_crc_eof(struct sk_buff *skb, int tlen)
Robert Love85b4aa42008-12-09 15:10:24 -08001366{
1367 struct fcoe_percpu_s *fps;
Bhanu Prakash Gollapudi8597ae82011-01-28 16:05:37 -08001368 int rc;
Robert Love85b4aa42008-12-09 15:10:24 -08001369
Robert Love5e5e92d2009-03-17 11:41:35 -07001370 fps = &get_cpu_var(fcoe_percpu);
Bhanu Prakash Gollapudi8597ae82011-01-28 16:05:37 -08001371 rc = fcoe_get_paged_crc_eof(skb, tlen, fps);
Robert Love5e5e92d2009-03-17 11:41:35 -07001372 put_cpu_var(fcoe_percpu);
Robert Love85b4aa42008-12-09 15:10:24 -08001373
Bhanu Prakash Gollapudi8597ae82011-01-28 16:05:37 -08001374 return rc;
Robert Love85b4aa42008-12-09 15:10:24 -08001375}
Robert Love85b4aa42008-12-09 15:10:24 -08001376
1377/**
Robert Love1875f272009-11-03 11:47:50 -08001378 * fcoe_xmit() - Transmit a FCoE frame
1379 * @lport: The local port that the frame is to be transmitted for
1380 * @fp: The frame to be transmitted
Robert Love85b4aa42008-12-09 15:10:24 -08001381 *
Robert Love1875f272009-11-03 11:47:50 -08001382 * Return: 0 for success
Robert Love34f42a02009-02-27 10:55:45 -08001383 */
Robert Love1875f272009-11-03 11:47:50 -08001384int fcoe_xmit(struct fc_lport *lport, struct fc_frame *fp)
Robert Love85b4aa42008-12-09 15:10:24 -08001385{
Vasu Dev4bb6b512009-05-06 10:52:34 -07001386 int wlen;
Robert Love85b4aa42008-12-09 15:10:24 -08001387 u32 crc;
1388 struct ethhdr *eh;
1389 struct fcoe_crc_eof *cp;
1390 struct sk_buff *skb;
1391 struct fcoe_dev_stats *stats;
1392 struct fc_frame_header *fh;
1393 unsigned int hlen; /* header length implies the version */
1394 unsigned int tlen; /* trailer length */
1395 unsigned int elen; /* eth header, may include vlan */
Robert Love1875f272009-11-03 11:47:50 -08001396 struct fcoe_port *port = lport_priv(lport);
Bhanu Prakash Gollapudi8597ae82011-01-28 16:05:37 -08001397 struct fcoe_interface *fcoe = port->priv;
Robert Love85b4aa42008-12-09 15:10:24 -08001398 u8 sof, eof;
1399 struct fcoe_hdr *hp;
1400
1401 WARN_ON((fr_len(fp) % sizeof(u32)) != 0);
1402
Robert Love85b4aa42008-12-09 15:10:24 -08001403 fh = fc_frame_header_get(fp);
Joe Eykholt97c83892009-03-17 11:42:40 -07001404 skb = fp_skb(fp);
1405 wlen = skb->len / FCOE_WORD_TO_BYTE;
1406
Robert Love1875f272009-11-03 11:47:50 -08001407 if (!lport->link_up) {
Dan Carpenter3caf02e2009-04-21 16:27:25 -07001408 kfree_skb(skb);
Joe Eykholt97c83892009-03-17 11:42:40 -07001409 return 0;
Robert Love85b4aa42008-12-09 15:10:24 -08001410 }
1411
Joe Eykholt9860eeb2010-03-12 16:07:52 -08001412 if (unlikely(fh->fh_type == FC_TYPE_ELS) &&
Robert Love1875f272009-11-03 11:47:50 -08001413 fcoe_ctlr_els_send(&fcoe->ctlr, lport, skb))
Joe Eykholt97c83892009-03-17 11:42:40 -07001414 return 0;
1415
Robert Love85b4aa42008-12-09 15:10:24 -08001416 sof = fr_sof(fp);
1417 eof = fr_eof(fp);
1418
Vasu Dev4e57e1c2009-05-06 10:52:46 -07001419 elen = sizeof(struct ethhdr);
Robert Love85b4aa42008-12-09 15:10:24 -08001420 hlen = sizeof(struct fcoe_hdr);
1421 tlen = sizeof(struct fcoe_crc_eof);
1422 wlen = (skb->len - tlen + sizeof(crc)) / FCOE_WORD_TO_BYTE;
1423
1424 /* crc offload */
Robert Love1875f272009-11-03 11:47:50 -08001425 if (likely(lport->crc_offload)) {
Yi Zou39ca9a02009-02-27 14:07:15 -08001426 skb->ip_summed = CHECKSUM_PARTIAL;
Robert Love85b4aa42008-12-09 15:10:24 -08001427 skb->csum_start = skb_headroom(skb);
1428 skb->csum_offset = skb->len;
1429 crc = 0;
1430 } else {
1431 skb->ip_summed = CHECKSUM_NONE;
1432 crc = fcoe_fc_crc(fp);
1433 }
1434
Chris Leech014f5c32009-08-25 13:59:30 -07001435 /* copy port crc and eof to the skb buff */
Robert Love85b4aa42008-12-09 15:10:24 -08001436 if (skb_is_nonlinear(skb)) {
1437 skb_frag_t *frag;
Bhanu Prakash Gollapudi8597ae82011-01-28 16:05:37 -08001438 if (fcoe_alloc_paged_crc_eof(skb, tlen)) {
Roel Kluine9041582009-02-27 10:56:22 -08001439 kfree_skb(skb);
Robert Love85b4aa42008-12-09 15:10:24 -08001440 return -ENOMEM;
1441 }
1442 frag = &skb_shinfo(skb)->frags[skb_shinfo(skb)->nr_frags - 1];
1443 cp = kmap_atomic(frag->page, KM_SKB_DATA_SOFTIRQ)
1444 + frag->page_offset;
1445 } else {
1446 cp = (struct fcoe_crc_eof *)skb_put(skb, tlen);
1447 }
1448
1449 memset(cp, 0, sizeof(*cp));
1450 cp->fcoe_eof = eof;
1451 cp->fcoe_crc32 = cpu_to_le32(~crc);
1452
1453 if (skb_is_nonlinear(skb)) {
1454 kunmap_atomic(cp, KM_SKB_DATA_SOFTIRQ);
1455 cp = NULL;
1456 }
1457
Chris Leech014f5c32009-08-25 13:59:30 -07001458 /* adjust skb network/transport offsets to match mac/fcoe/port */
Robert Love85b4aa42008-12-09 15:10:24 -08001459 skb_push(skb, elen + hlen);
1460 skb_reset_mac_header(skb);
1461 skb_reset_network_header(skb);
1462 skb->mac_len = elen;
Yi Zou211c7382009-02-27 14:06:37 -08001463 skb->protocol = htons(ETH_P_FCOE);
Chris Leech3fe9a0b2009-08-25 13:59:46 -07001464 skb->dev = fcoe->netdev;
Robert Love85b4aa42008-12-09 15:10:24 -08001465
1466 /* fill up mac and fcoe headers */
1467 eh = eth_hdr(skb);
1468 eh->h_proto = htons(ETH_P_FCOE);
Joe Eykholtcd229e42010-07-20 15:20:40 -07001469 memcpy(eh->h_dest, fcoe->ctlr.dest_addr, ETH_ALEN);
Chris Leech3fe9a0b2009-08-25 13:59:46 -07001470 if (fcoe->ctlr.map_dest)
Joe Eykholtcd229e42010-07-20 15:20:40 -07001471 memcpy(eh->h_dest + 3, fh->fh_d_id, 3);
Robert Love85b4aa42008-12-09 15:10:24 -08001472
Chris Leech3fe9a0b2009-08-25 13:59:46 -07001473 if (unlikely(fcoe->ctlr.flogi_oxid != FC_XID_UNKNOWN))
1474 memcpy(eh->h_source, fcoe->ctlr.ctl_src_addr, ETH_ALEN);
Robert Love85b4aa42008-12-09 15:10:24 -08001475 else
Chris Leech11b56182009-11-03 11:46:29 -08001476 memcpy(eh->h_source, port->data_src_addr, ETH_ALEN);
Robert Love85b4aa42008-12-09 15:10:24 -08001477
1478 hp = (struct fcoe_hdr *)(eh + 1);
1479 memset(hp, 0, sizeof(*hp));
1480 if (FC_FCOE_VER)
1481 FC_FCOE_ENCAPS_VER(hp, FC_FCOE_VER);
1482 hp->fcoe_sof = sof;
1483
Yi Zou39ca9a02009-02-27 14:07:15 -08001484 /* fcoe lso, mss is in max_payload which is non-zero for FCP data */
Robert Love1875f272009-11-03 11:47:50 -08001485 if (lport->seq_offload && fr_max_payload(fp)) {
Yi Zou39ca9a02009-02-27 14:07:15 -08001486 skb_shinfo(skb)->gso_type = SKB_GSO_FCOE;
1487 skb_shinfo(skb)->gso_size = fr_max_payload(fp);
1488 } else {
1489 skb_shinfo(skb)->gso_type = 0;
1490 skb_shinfo(skb)->gso_size = 0;
1491 }
Robert Love85b4aa42008-12-09 15:10:24 -08001492 /* update tx stats: regardless if LLD fails */
Joe Eykholtf018b732010-03-12 16:08:55 -08001493 stats = per_cpu_ptr(lport->dev_stats, get_cpu());
Robert Love582b45b2009-03-31 15:51:50 -07001494 stats->TxFrames++;
1495 stats->TxWords += wlen;
Joe Eykholtf018b732010-03-12 16:08:55 -08001496 put_cpu();
Robert Love85b4aa42008-12-09 15:10:24 -08001497
1498 /* send down to lld */
Robert Love1875f272009-11-03 11:47:50 -08001499 fr_dev(fp) = lport;
Chris Leech014f5c32009-08-25 13:59:30 -07001500 if (port->fcoe_pending_queue.qlen)
Robert Love1875f272009-11-03 11:47:50 -08001501 fcoe_check_wait_queue(lport, skb);
Vasu Dev4bb6b512009-05-06 10:52:34 -07001502 else if (fcoe_start_io(skb))
Robert Love1875f272009-11-03 11:47:50 -08001503 fcoe_check_wait_queue(lport, skb);
Robert Love85b4aa42008-12-09 15:10:24 -08001504
1505 return 0;
1506}
Robert Love85b4aa42008-12-09 15:10:24 -08001507
Robert Love34f42a02009-02-27 10:55:45 -08001508/**
Robert Love1875f272009-11-03 11:47:50 -08001509 * fcoe_percpu_flush_done() - Indicate per-CPU queue flush completion
1510 * @skb: The completed skb (argument required by destructor)
Joe Eykholte7a51992009-08-25 14:04:08 -07001511 */
1512static void fcoe_percpu_flush_done(struct sk_buff *skb)
1513{
1514 complete(&fcoe_flush_completion);
1515}
1516
1517/**
Vasu Dev52ee8322011-01-28 16:03:52 -08001518 * fcoe_filter_frames() - filter out bad fcoe frames, i.e. bad CRC
1519 * @lport: The local port the frame was received on
1520 * @fp: The received frame
1521 *
1522 * Return: 0 on passing filtering checks
1523 */
1524static inline int fcoe_filter_frames(struct fc_lport *lport,
1525 struct fc_frame *fp)
1526{
1527 struct fcoe_interface *fcoe;
1528 struct fc_frame_header *fh;
1529 struct sk_buff *skb = (struct sk_buff *)fp;
1530 struct fcoe_dev_stats *stats;
1531
1532 /*
1533 * We only check CRC if no offload is available and if it is
1534 * it's solicited data, in which case, the FCP layer would
1535 * check it during the copy.
1536 */
1537 if (lport->crc_offload && skb->ip_summed == CHECKSUM_UNNECESSARY)
1538 fr_flags(fp) &= ~FCPHF_CRC_UNCHECKED;
1539 else
1540 fr_flags(fp) |= FCPHF_CRC_UNCHECKED;
1541
1542 fh = (struct fc_frame_header *) skb_transport_header(skb);
1543 fh = fc_frame_header_get(fp);
1544 if (fh->fh_r_ctl == FC_RCTL_DD_SOL_DATA && fh->fh_type == FC_TYPE_FCP)
1545 return 0;
1546
Bhanu Prakash Gollapudi8597ae82011-01-28 16:05:37 -08001547 fcoe = ((struct fcoe_port *)lport_priv(lport))->priv;
Vasu Dev52ee8322011-01-28 16:03:52 -08001548 if (is_fip_mode(&fcoe->ctlr) && fc_frame_payload_op(fp) == ELS_LOGO &&
1549 ntoh24(fh->fh_s_id) == FC_FID_FLOGI) {
1550 FCOE_DBG("fcoe: dropping FCoE lport LOGO in fip mode\n");
1551 return -EINVAL;
1552 }
1553
Dan Carpenterf2f96d22011-02-25 15:03:23 -08001554 if (!(fr_flags(fp) & FCPHF_CRC_UNCHECKED) ||
Vasu Dev52ee8322011-01-28 16:03:52 -08001555 le32_to_cpu(fr_crc(fp)) == ~crc32(~0, skb->data, skb->len)) {
1556 fr_flags(fp) &= ~FCPHF_CRC_UNCHECKED;
1557 return 0;
1558 }
1559
1560 stats = per_cpu_ptr(lport->dev_stats, get_cpu());
1561 stats->InvalidCRCCount++;
1562 if (stats->InvalidCRCCount < 5)
1563 printk(KERN_WARNING "fcoe: dropping frame with CRC error\n");
1564 return -EINVAL;
1565}
1566
1567/**
Chris Leech859b7b62009-11-20 14:54:47 -08001568 * fcoe_recv_frame() - process a single received frame
1569 * @skb: frame to process
1570 */
1571static void fcoe_recv_frame(struct sk_buff *skb)
1572{
1573 u32 fr_len;
1574 struct fc_lport *lport;
1575 struct fcoe_rcv_info *fr;
1576 struct fcoe_dev_stats *stats;
Chris Leech859b7b62009-11-20 14:54:47 -08001577 struct fcoe_crc_eof crc_eof;
1578 struct fc_frame *fp;
Chris Leech859b7b62009-11-20 14:54:47 -08001579 struct fcoe_port *port;
1580 struct fcoe_hdr *hp;
1581
1582 fr = fcoe_dev_from_skb(skb);
1583 lport = fr->fr_dev;
1584 if (unlikely(!lport)) {
1585 if (skb->destructor != fcoe_percpu_flush_done)
1586 FCOE_NETDEV_DBG(skb->dev, "NULL lport in skb");
1587 kfree_skb(skb);
1588 return;
1589 }
1590
1591 FCOE_NETDEV_DBG(skb->dev, "skb_info: len:%d data_len:%d "
1592 "head:%p data:%p tail:%p end:%p sum:%d dev:%s",
1593 skb->len, skb->data_len,
1594 skb->head, skb->data, skb_tail_pointer(skb),
1595 skb_end_pointer(skb), skb->csum,
1596 skb->dev ? skb->dev->name : "<NULL>");
1597
Chris Leech859b7b62009-11-20 14:54:47 -08001598 port = lport_priv(lport);
1599 if (skb_is_nonlinear(skb))
1600 skb_linearize(skb); /* not ideal */
Chris Leech859b7b62009-11-20 14:54:47 -08001601
1602 /*
1603 * Frame length checks and setting up the header pointers
1604 * was done in fcoe_rcv already.
1605 */
1606 hp = (struct fcoe_hdr *) skb_network_header(skb);
Chris Leech859b7b62009-11-20 14:54:47 -08001607
Joe Eykholtf018b732010-03-12 16:08:55 -08001608 stats = per_cpu_ptr(lport->dev_stats, get_cpu());
Chris Leech859b7b62009-11-20 14:54:47 -08001609 if (unlikely(FC_FCOE_DECAPS_VER(hp) != FC_FCOE_VER)) {
1610 if (stats->ErrorFrames < 5)
1611 printk(KERN_WARNING "fcoe: FCoE version "
1612 "mismatch: The frame has "
1613 "version %x, but the "
1614 "initiator supports version "
1615 "%x\n", FC_FCOE_DECAPS_VER(hp),
1616 FC_FCOE_VER);
Joe Eykholtf018b732010-03-12 16:08:55 -08001617 goto drop;
Chris Leech859b7b62009-11-20 14:54:47 -08001618 }
1619
1620 skb_pull(skb, sizeof(struct fcoe_hdr));
1621 fr_len = skb->len - sizeof(struct fcoe_crc_eof);
1622
1623 stats->RxFrames++;
1624 stats->RxWords += fr_len / FCOE_WORD_TO_BYTE;
1625
1626 fp = (struct fc_frame *)skb;
1627 fc_frame_init(fp);
1628 fr_dev(fp) = lport;
1629 fr_sof(fp) = hp->fcoe_sof;
1630
1631 /* Copy out the CRC and EOF trailer for access */
Joe Eykholtf018b732010-03-12 16:08:55 -08001632 if (skb_copy_bits(skb, fr_len, &crc_eof, sizeof(crc_eof)))
1633 goto drop;
Chris Leech859b7b62009-11-20 14:54:47 -08001634 fr_eof(fp) = crc_eof.fcoe_eof;
1635 fr_crc(fp) = crc_eof.fcoe_crc32;
Joe Eykholtf018b732010-03-12 16:08:55 -08001636 if (pskb_trim(skb, fr_len))
1637 goto drop;
Chris Leech859b7b62009-11-20 14:54:47 -08001638
Vasu Dev52ee8322011-01-28 16:03:52 -08001639 if (!fcoe_filter_frames(lport, fp)) {
1640 put_cpu();
1641 fc_exch_recv(lport, fp);
1642 return;
Chris Leech859b7b62009-11-20 14:54:47 -08001643 }
Joe Eykholtf018b732010-03-12 16:08:55 -08001644drop:
1645 stats->ErrorFrames++;
1646 put_cpu();
1647 kfree_skb(skb);
Chris Leech859b7b62009-11-20 14:54:47 -08001648}
1649
1650/**
Robert Love1875f272009-11-03 11:47:50 -08001651 * fcoe_percpu_receive_thread() - The per-CPU packet receive thread
1652 * @arg: The per-CPU context
Robert Love85b4aa42008-12-09 15:10:24 -08001653 *
1654 * Return: 0 for success
Robert Love85b4aa42008-12-09 15:10:24 -08001655 */
1656int fcoe_percpu_receive_thread(void *arg)
1657{
1658 struct fcoe_percpu_s *p = arg;
Robert Love85b4aa42008-12-09 15:10:24 -08001659 struct sk_buff *skb;
Robert Love85b4aa42008-12-09 15:10:24 -08001660
Robert Love4469c192009-02-27 10:56:38 -08001661 set_user_nice(current, -20);
Robert Love85b4aa42008-12-09 15:10:24 -08001662
1663 while (!kthread_should_stop()) {
1664
1665 spin_lock_bh(&p->fcoe_rx_list.lock);
1666 while ((skb = __skb_dequeue(&p->fcoe_rx_list)) == NULL) {
1667 set_current_state(TASK_INTERRUPTIBLE);
1668 spin_unlock_bh(&p->fcoe_rx_list.lock);
1669 schedule();
1670 set_current_state(TASK_RUNNING);
1671 if (kthread_should_stop())
1672 return 0;
1673 spin_lock_bh(&p->fcoe_rx_list.lock);
1674 }
1675 spin_unlock_bh(&p->fcoe_rx_list.lock);
Chris Leech859b7b62009-11-20 14:54:47 -08001676 fcoe_recv_frame(skb);
Robert Love85b4aa42008-12-09 15:10:24 -08001677 }
1678 return 0;
1679}
1680
1681/**
Robert Love1875f272009-11-03 11:47:50 -08001682 * fcoe_dev_setup() - Setup the link change notification interface
Robert Love34f42a02009-02-27 10:55:45 -08001683 */
Randy Dunlapb0d428a2009-04-27 21:49:31 -07001684static void fcoe_dev_setup(void)
Robert Love85b4aa42008-12-09 15:10:24 -08001685{
Robert Love85b4aa42008-12-09 15:10:24 -08001686 register_netdevice_notifier(&fcoe_notifier);
1687}
1688
1689/**
Robert Love1875f272009-11-03 11:47:50 -08001690 * fcoe_dev_cleanup() - Cleanup the link change notification interface
Robert Love34f42a02009-02-27 10:55:45 -08001691 */
Robert Love85b4aa42008-12-09 15:10:24 -08001692static void fcoe_dev_cleanup(void)
1693{
1694 unregister_netdevice_notifier(&fcoe_notifier);
1695}
1696
1697/**
Robert Love1875f272009-11-03 11:47:50 -08001698 * fcoe_device_notification() - Handler for net device events
1699 * @notifier: The context of the notification
1700 * @event: The type of event
1701 * @ptr: The net device that the event was on
Robert Love85b4aa42008-12-09 15:10:24 -08001702 *
Robert Love1875f272009-11-03 11:47:50 -08001703 * This function is called by the Ethernet driver in case of link change event.
Robert Love85b4aa42008-12-09 15:10:24 -08001704 *
1705 * Returns: 0 for success
Robert Love34f42a02009-02-27 10:55:45 -08001706 */
Robert Love85b4aa42008-12-09 15:10:24 -08001707static int fcoe_device_notification(struct notifier_block *notifier,
1708 ulong event, void *ptr)
1709{
Robert Love1875f272009-11-03 11:47:50 -08001710 struct fc_lport *lport = NULL;
Vasu Dev1d1b88d2009-07-29 17:05:45 -07001711 struct net_device *netdev = ptr;
Chris Leech014f5c32009-08-25 13:59:30 -07001712 struct fcoe_interface *fcoe;
Chris Leech2e70e242009-08-25 14:00:23 -07001713 struct fcoe_port *port;
Robert Love85b4aa42008-12-09 15:10:24 -08001714 struct fcoe_dev_stats *stats;
Joe Eykholt97c83892009-03-17 11:42:40 -07001715 u32 link_possible = 1;
Robert Love85b4aa42008-12-09 15:10:24 -08001716 u32 mfs;
1717 int rc = NOTIFY_OK;
1718
Chris Leech014f5c32009-08-25 13:59:30 -07001719 list_for_each_entry(fcoe, &fcoe_hostlist, list) {
Chris Leech25024982009-08-25 13:59:35 -07001720 if (fcoe->netdev == netdev) {
Robert Love1875f272009-11-03 11:47:50 -08001721 lport = fcoe->ctlr.lp;
Robert Love85b4aa42008-12-09 15:10:24 -08001722 break;
1723 }
1724 }
Robert Love1875f272009-11-03 11:47:50 -08001725 if (!lport) {
Robert Love85b4aa42008-12-09 15:10:24 -08001726 rc = NOTIFY_DONE;
1727 goto out;
1728 }
1729
Robert Love85b4aa42008-12-09 15:10:24 -08001730 switch (event) {
1731 case NETDEV_DOWN:
1732 case NETDEV_GOING_DOWN:
Joe Eykholt97c83892009-03-17 11:42:40 -07001733 link_possible = 0;
Robert Love85b4aa42008-12-09 15:10:24 -08001734 break;
1735 case NETDEV_UP:
1736 case NETDEV_CHANGE:
Robert Love85b4aa42008-12-09 15:10:24 -08001737 break;
1738 case NETDEV_CHANGEMTU:
Yi Zou7221d7e2009-10-21 16:27:52 -07001739 if (netdev->features & NETIF_F_FCOE_MTU)
1740 break;
Vasu Dev1d1b88d2009-07-29 17:05:45 -07001741 mfs = netdev->mtu - (sizeof(struct fcoe_hdr) +
1742 sizeof(struct fcoe_crc_eof));
Robert Love85b4aa42008-12-09 15:10:24 -08001743 if (mfs >= FC_MIN_MAX_FRAME)
Robert Love1875f272009-11-03 11:47:50 -08001744 fc_set_mfs(lport, mfs);
Robert Love85b4aa42008-12-09 15:10:24 -08001745 break;
1746 case NETDEV_REGISTER:
1747 break;
Chris Leech2e70e242009-08-25 14:00:23 -07001748 case NETDEV_UNREGISTER:
1749 list_del(&fcoe->list);
1750 port = lport_priv(fcoe->ctlr.lp);
1751 fcoe_interface_cleanup(fcoe);
Tejun Heo2ca32b42011-01-28 16:05:32 -08001752 queue_work(fcoe_wq, &port->destroy_work);
Chris Leech2e70e242009-08-25 14:00:23 -07001753 goto out;
1754 break;
Yi Zou54a5b212010-07-20 15:21:17 -07001755 case NETDEV_FEAT_CHANGE:
1756 fcoe_netdev_features_change(lport, netdev);
1757 break;
Robert Love85b4aa42008-12-09 15:10:24 -08001758 default:
Vasu Dev1d1b88d2009-07-29 17:05:45 -07001759 FCOE_NETDEV_DBG(netdev, "Unknown event %ld "
Robert Loved5488eb2009-06-10 15:30:59 -07001760 "from netdev netlink\n", event);
Robert Love85b4aa42008-12-09 15:10:24 -08001761 }
Robert Love5e4f8fe2010-05-07 15:18:35 -07001762
1763 fcoe_link_speed_update(lport);
1764
Robert Love1875f272009-11-03 11:47:50 -08001765 if (link_possible && !fcoe_link_ok(lport))
Chris Leech3fe9a0b2009-08-25 13:59:46 -07001766 fcoe_ctlr_link_up(&fcoe->ctlr);
1767 else if (fcoe_ctlr_link_down(&fcoe->ctlr)) {
Joe Eykholtf018b732010-03-12 16:08:55 -08001768 stats = per_cpu_ptr(lport->dev_stats, get_cpu());
Joe Eykholt97c83892009-03-17 11:42:40 -07001769 stats->LinkFailureCount++;
Joe Eykholtf018b732010-03-12 16:08:55 -08001770 put_cpu();
Robert Love1875f272009-11-03 11:47:50 -08001771 fcoe_clean_pending_queue(lport);
Robert Love85b4aa42008-12-09 15:10:24 -08001772 }
1773out:
1774 return rc;
1775}
1776
1777/**
Vasu Dev55a66d32009-12-10 09:59:31 -08001778 * fcoe_disable() - Disables a FCoE interface
Yi Zou78a58242011-01-28 16:05:16 -08001779 * @netdev : The net_device object the Ethernet interface to create on
Vasu Dev55a66d32009-12-10 09:59:31 -08001780 *
Yi Zou78a58242011-01-28 16:05:16 -08001781 * Called from fcoe transport.
Vasu Dev55a66d32009-12-10 09:59:31 -08001782 *
1783 * Returns: 0 for success
1784 */
Yi Zou78a58242011-01-28 16:05:16 -08001785static int fcoe_disable(struct net_device *netdev)
Vasu Dev55a66d32009-12-10 09:59:31 -08001786{
1787 struct fcoe_interface *fcoe;
Vasu Dev55a66d32009-12-10 09:59:31 -08001788 int rc = 0;
1789
1790 mutex_lock(&fcoe_config_mutex);
Vasu Dev55a66d32009-12-10 09:59:31 -08001791
Robert Loveee5df622011-04-01 16:05:59 -07001792 rtnl_lock();
Vasu Dev55a66d32009-12-10 09:59:31 -08001793 fcoe = fcoe_hostlist_lookup_port(netdev);
1794 rtnl_unlock();
1795
Chris Leech9ee50e42010-04-09 14:22:23 -07001796 if (fcoe) {
Chris Leech9ee50e42010-04-09 14:22:23 -07001797 fcoe_ctlr_link_down(&fcoe->ctlr);
Vasu Dev9d4cbc02010-07-20 15:19:26 -07001798 fcoe_clean_pending_queue(fcoe->ctlr.lp);
Chris Leech9ee50e42010-04-09 14:22:23 -07001799 } else
Vasu Dev55a66d32009-12-10 09:59:31 -08001800 rc = -ENODEV;
1801
Vasu Dev55a66d32009-12-10 09:59:31 -08001802 mutex_unlock(&fcoe_config_mutex);
1803 return rc;
1804}
1805
1806/**
1807 * fcoe_enable() - Enables a FCoE interface
Yi Zou78a58242011-01-28 16:05:16 -08001808 * @netdev : The net_device object the Ethernet interface to create on
Vasu Dev55a66d32009-12-10 09:59:31 -08001809 *
Yi Zou78a58242011-01-28 16:05:16 -08001810 * Called from fcoe transport.
Vasu Dev55a66d32009-12-10 09:59:31 -08001811 *
1812 * Returns: 0 for success
1813 */
Yi Zou78a58242011-01-28 16:05:16 -08001814static int fcoe_enable(struct net_device *netdev)
Vasu Dev55a66d32009-12-10 09:59:31 -08001815{
1816 struct fcoe_interface *fcoe;
Vasu Dev55a66d32009-12-10 09:59:31 -08001817 int rc = 0;
1818
1819 mutex_lock(&fcoe_config_mutex);
Robert Loveee5df622011-04-01 16:05:59 -07001820 rtnl_lock();
Vasu Dev55a66d32009-12-10 09:59:31 -08001821 fcoe = fcoe_hostlist_lookup_port(netdev);
1822 rtnl_unlock();
1823
Vasu Dev9d4cbc02010-07-20 15:19:26 -07001824 if (!fcoe)
Vasu Dev55a66d32009-12-10 09:59:31 -08001825 rc = -ENODEV;
Vasu Dev9d4cbc02010-07-20 15:19:26 -07001826 else if (!fcoe_link_ok(fcoe->ctlr.lp))
1827 fcoe_ctlr_link_up(&fcoe->ctlr);
Vasu Dev55a66d32009-12-10 09:59:31 -08001828
Vasu Dev55a66d32009-12-10 09:59:31 -08001829 mutex_unlock(&fcoe_config_mutex);
1830 return rc;
1831}
1832
1833/**
Robert Love1875f272009-11-03 11:47:50 -08001834 * fcoe_destroy() - Destroy a FCoE interface
Yi Zou78a58242011-01-28 16:05:16 -08001835 * @netdev : The net_device object the Ethernet interface to create on
Robert Love1875f272009-11-03 11:47:50 -08001836 *
Yi Zou78a58242011-01-28 16:05:16 -08001837 * Called from fcoe transport
Robert Love85b4aa42008-12-09 15:10:24 -08001838 *
1839 * Returns: 0 for success
Robert Love34f42a02009-02-27 10:55:45 -08001840 */
Yi Zou78a58242011-01-28 16:05:16 -08001841static int fcoe_destroy(struct net_device *netdev)
Robert Love85b4aa42008-12-09 15:10:24 -08001842{
Chris Leech030f4e02009-08-25 14:00:02 -07001843 struct fcoe_interface *fcoe;
Vasu Devf04ca1b2011-04-01 16:06:45 -07001844 struct fc_lport *lport;
Mike Christie8eca3552009-10-21 16:27:44 -07001845 int rc = 0;
Robert Love85b4aa42008-12-09 15:10:24 -08001846
Chris Leechdfc1d0f2009-08-25 14:00:13 -07001847 mutex_lock(&fcoe_config_mutex);
Robert Loveee5df622011-04-01 16:05:59 -07001848 rtnl_lock();
Chris Leech2e70e242009-08-25 14:00:23 -07001849 fcoe = fcoe_hostlist_lookup_port(netdev);
1850 if (!fcoe) {
Chris Leech090eb6c2009-08-25 14:00:28 -07001851 rtnl_unlock();
Robert Love85b4aa42008-12-09 15:10:24 -08001852 rc = -ENODEV;
Yi Zou78a58242011-01-28 16:05:16 -08001853 goto out_nodev;
Robert Love85b4aa42008-12-09 15:10:24 -08001854 }
Vasu Devf04ca1b2011-04-01 16:06:45 -07001855 lport = fcoe->ctlr.lp;
Yi Zou54a5b212010-07-20 15:21:17 -07001856 list_del(&fcoe->list);
Vasu Devf04ca1b2011-04-01 16:06:45 -07001857 fcoe_interface_cleanup(fcoe);
1858 rtnl_unlock();
1859 fcoe_if_destroy(lport);
Robert Love85b4aa42008-12-09 15:10:24 -08001860out_nodev:
Chris Leechdfc1d0f2009-08-25 14:00:13 -07001861 mutex_unlock(&fcoe_config_mutex);
Robert Love85b4aa42008-12-09 15:10:24 -08001862 return rc;
1863}
1864
Robert Love1875f272009-11-03 11:47:50 -08001865/**
1866 * fcoe_destroy_work() - Destroy a FCoE port in a deferred work context
1867 * @work: Handle to the FCoE port to be destroyed
1868 */
Chris Leech2e70e242009-08-25 14:00:23 -07001869static void fcoe_destroy_work(struct work_struct *work)
1870{
1871 struct fcoe_port *port;
1872
1873 port = container_of(work, struct fcoe_port, destroy_work);
1874 mutex_lock(&fcoe_config_mutex);
1875 fcoe_if_destroy(port->lport);
1876 mutex_unlock(&fcoe_config_mutex);
1877}
1878
Robert Love85b4aa42008-12-09 15:10:24 -08001879/**
Yi Zou78a58242011-01-28 16:05:16 -08001880 * fcoe_match() - Check if the FCoE is supported on the given netdevice
1881 * @netdev : The net_device object the Ethernet interface to create on
Robert Love1875f272009-11-03 11:47:50 -08001882 *
Yi Zou78a58242011-01-28 16:05:16 -08001883 * Called from fcoe transport.
1884 *
1885 * Returns: always returns true as this is the default FCoE transport,
1886 * i.e., support all netdevs.
1887 */
1888static bool fcoe_match(struct net_device *netdev)
1889{
1890 return true;
1891}
1892
1893/**
1894 * fcoe_create() - Create a fcoe interface
1895 * @netdev : The net_device object the Ethernet interface to create on
1896 * @fip_mode: The FIP mode for this creation
1897 *
1898 * Called from fcoe transport
Robert Love85b4aa42008-12-09 15:10:24 -08001899 *
1900 * Returns: 0 for success
Robert Love34f42a02009-02-27 10:55:45 -08001901 */
Yi Zou78a58242011-01-28 16:05:16 -08001902static int fcoe_create(struct net_device *netdev, enum fip_state fip_mode)
Robert Love85b4aa42008-12-09 15:10:24 -08001903{
1904 int rc;
Chris Leech030f4e02009-08-25 14:00:02 -07001905 struct fcoe_interface *fcoe;
Chris Leechaf7f85d2009-08-25 13:59:24 -07001906 struct fc_lport *lport;
Robert Love85b4aa42008-12-09 15:10:24 -08001907
Chris Leechdfc1d0f2009-08-25 14:00:13 -07001908 mutex_lock(&fcoe_config_mutex);
Robert Loveee5df622011-04-01 16:05:59 -07001909 rtnl_lock();
Vasu Dev34ce27b2010-05-07 15:18:46 -07001910
Robert Love85b4aa42008-12-09 15:10:24 -08001911 /* look for existing lport */
1912 if (fcoe_hostlist_lookup(netdev)) {
1913 rc = -EEXIST;
Yi Zou78a58242011-01-28 16:05:16 -08001914 goto out_nodev;
Robert Love85b4aa42008-12-09 15:10:24 -08001915 }
Robert Love85b4aa42008-12-09 15:10:24 -08001916
Joe Eykholt1dd454d2010-07-20 15:20:46 -07001917 fcoe = fcoe_interface_create(netdev, fip_mode);
Robert Love7287fb92011-01-28 16:03:47 -08001918 if (IS_ERR(fcoe)) {
1919 rc = PTR_ERR(fcoe);
Yi Zou78a58242011-01-28 16:05:16 -08001920 goto out_nodev;
Chris Leech030f4e02009-08-25 14:00:02 -07001921 }
1922
Chris Leech9a057532009-11-03 11:46:40 -08001923 lport = fcoe_if_create(fcoe, &netdev->dev, 0);
Chris Leechaf7f85d2009-08-25 13:59:24 -07001924 if (IS_ERR(lport)) {
Robert Loved5488eb2009-06-10 15:30:59 -07001925 printk(KERN_ERR "fcoe: Failed to create interface (%s)\n",
Robert Love85b4aa42008-12-09 15:10:24 -08001926 netdev->name);
Robert Love85b4aa42008-12-09 15:10:24 -08001927 rc = -EIO;
Chris Leech2e70e242009-08-25 14:00:23 -07001928 fcoe_interface_cleanup(fcoe);
Chris Leech030f4e02009-08-25 14:00:02 -07001929 goto out_free;
Robert Love85b4aa42008-12-09 15:10:24 -08001930 }
Chris Leech030f4e02009-08-25 14:00:02 -07001931
Chris Leech54b649f2009-08-25 14:00:07 -07001932 /* Make this the "master" N_Port */
1933 fcoe->ctlr.lp = lport;
Chris Leech030f4e02009-08-25 14:00:02 -07001934
Chris Leechc863df32009-08-25 14:00:18 -07001935 /* add to lports list */
1936 fcoe_hostlist_add(lport);
1937
Chris Leech54b649f2009-08-25 14:00:07 -07001938 /* start FIP Discovery and FLOGI */
1939 lport->boot_time = jiffies;
1940 fc_fabric_login(lport);
1941 if (!fcoe_link_ok(lport))
1942 fcoe_ctlr_link_up(&fcoe->ctlr);
1943
Chris Leech54b649f2009-08-25 14:00:07 -07001944 /*
1945 * Release from init in fcoe_interface_create(), on success lport
1946 * should be holding a reference taken in fcoe_if_create().
1947 */
Chris Leech030f4e02009-08-25 14:00:02 -07001948 fcoe_interface_put(fcoe);
Rob Love6409ea62010-01-21 10:16:05 -08001949 rtnl_unlock();
1950 mutex_unlock(&fcoe_config_mutex);
1951
1952 return 0;
1953out_free:
1954 fcoe_interface_put(fcoe);
Robert Love85b4aa42008-12-09 15:10:24 -08001955out_nodev:
Vasu Dev34ce27b2010-05-07 15:18:46 -07001956 rtnl_unlock();
Chris Leechdfc1d0f2009-08-25 14:00:13 -07001957 mutex_unlock(&fcoe_config_mutex);
Robert Love85b4aa42008-12-09 15:10:24 -08001958 return rc;
1959}
1960
Robert Love34f42a02009-02-27 10:55:45 -08001961/**
Robert Love5e4f8fe2010-05-07 15:18:35 -07001962 * fcoe_link_speed_update() - Update the supported and actual link speeds
1963 * @lport: The local port to update speeds for
Robert Love85b4aa42008-12-09 15:10:24 -08001964 *
Robert Love5e4f8fe2010-05-07 15:18:35 -07001965 * Returns: 0 if the ethtool query was successful
1966 * -1 if the ethtool query failed
Robert Love85b4aa42008-12-09 15:10:24 -08001967 */
Robert Love5e4f8fe2010-05-07 15:18:35 -07001968int fcoe_link_speed_update(struct fc_lport *lport)
Robert Love85b4aa42008-12-09 15:10:24 -08001969{
Bhanu Prakash Gollapudi8597ae82011-01-28 16:05:37 -08001970 struct net_device *netdev = fcoe_netdev(lport);
David Decotigny8ae6dac2011-04-27 18:32:38 +00001971 struct ethtool_cmd ecmd;
Robert Love85b4aa42008-12-09 15:10:24 -08001972
Robert Love5e4f8fe2010-05-07 15:18:35 -07001973 if (!dev_ethtool_get_settings(netdev, &ecmd)) {
Robert Love1875f272009-11-03 11:47:50 -08001974 lport->link_supported_speeds &=
Yi Zou2f718d62009-07-29 17:04:01 -07001975 ~(FC_PORTSPEED_1GBIT | FC_PORTSPEED_10GBIT);
1976 if (ecmd.supported & (SUPPORTED_1000baseT_Half |
1977 SUPPORTED_1000baseT_Full))
Robert Love1875f272009-11-03 11:47:50 -08001978 lport->link_supported_speeds |= FC_PORTSPEED_1GBIT;
Yi Zou2f718d62009-07-29 17:04:01 -07001979 if (ecmd.supported & SUPPORTED_10000baseT_Full)
Robert Love1875f272009-11-03 11:47:50 -08001980 lport->link_supported_speeds |=
Yi Zou2f718d62009-07-29 17:04:01 -07001981 FC_PORTSPEED_10GBIT;
David Decotigny8ae6dac2011-04-27 18:32:38 +00001982 switch (ethtool_cmd_speed(&ecmd)) {
1983 case SPEED_1000:
Robert Love1875f272009-11-03 11:47:50 -08001984 lport->link_speed = FC_PORTSPEED_1GBIT;
David Decotigny8ae6dac2011-04-27 18:32:38 +00001985 break;
1986 case SPEED_10000:
Robert Love1875f272009-11-03 11:47:50 -08001987 lport->link_speed = FC_PORTSPEED_10GBIT;
David Decotigny8ae6dac2011-04-27 18:32:38 +00001988 break;
1989 }
Yi Zou2f718d62009-07-29 17:04:01 -07001990 return 0;
1991 }
1992 return -1;
Robert Love85b4aa42008-12-09 15:10:24 -08001993}
Robert Love85b4aa42008-12-09 15:10:24 -08001994
Robert Love34f42a02009-02-27 10:55:45 -08001995/**
Robert Love5e4f8fe2010-05-07 15:18:35 -07001996 * fcoe_link_ok() - Check if the link is OK for a local port
1997 * @lport: The local port to check link on
1998 *
1999 * Returns: 0 if link is UP and OK, -1 if not
2000 *
2001 */
2002int fcoe_link_ok(struct fc_lport *lport)
2003{
Bhanu Prakash Gollapudi8597ae82011-01-28 16:05:37 -08002004 struct net_device *netdev = fcoe_netdev(lport);
Robert Love5e4f8fe2010-05-07 15:18:35 -07002005
2006 if (netif_oper_up(netdev))
2007 return 0;
2008 return -1;
2009}
2010
2011/**
Robert Love1875f272009-11-03 11:47:50 -08002012 * fcoe_percpu_clean() - Clear all pending skbs for an local port
2013 * @lport: The local port whose skbs are to be cleared
Joe Eykholte7a51992009-08-25 14:04:08 -07002014 *
2015 * Must be called with fcoe_create_mutex held to single-thread completion.
2016 *
2017 * This flushes the pending skbs by adding a new skb to each queue and
2018 * waiting until they are all freed. This assures us that not only are
2019 * there no packets that will be handled by the lport, but also that any
2020 * threads already handling packet have returned.
Robert Love85b4aa42008-12-09 15:10:24 -08002021 */
Robert Love1875f272009-11-03 11:47:50 -08002022void fcoe_percpu_clean(struct fc_lport *lport)
Robert Love85b4aa42008-12-09 15:10:24 -08002023{
Robert Love85b4aa42008-12-09 15:10:24 -08002024 struct fcoe_percpu_s *pp;
2025 struct fcoe_rcv_info *fr;
2026 struct sk_buff_head *list;
2027 struct sk_buff *skb, *next;
2028 struct sk_buff *head;
Robert Love5e5e92d2009-03-17 11:41:35 -07002029 unsigned int cpu;
Robert Love85b4aa42008-12-09 15:10:24 -08002030
Robert Love5e5e92d2009-03-17 11:41:35 -07002031 for_each_possible_cpu(cpu) {
2032 pp = &per_cpu(fcoe_percpu, cpu);
2033 spin_lock_bh(&pp->fcoe_rx_list.lock);
2034 list = &pp->fcoe_rx_list;
2035 head = list->next;
2036 for (skb = head; skb != (struct sk_buff *)list;
2037 skb = next) {
2038 next = skb->next;
2039 fr = fcoe_dev_from_skb(skb);
Robert Love1875f272009-11-03 11:47:50 -08002040 if (fr->fr_dev == lport) {
Robert Love5e5e92d2009-03-17 11:41:35 -07002041 __skb_unlink(skb, list);
2042 kfree_skb(skb);
Robert Love85b4aa42008-12-09 15:10:24 -08002043 }
Robert Love85b4aa42008-12-09 15:10:24 -08002044 }
Joe Eykholte7a51992009-08-25 14:04:08 -07002045
2046 if (!pp->thread || !cpu_online(cpu)) {
2047 spin_unlock_bh(&pp->fcoe_rx_list.lock);
2048 continue;
2049 }
2050
2051 skb = dev_alloc_skb(0);
2052 if (!skb) {
2053 spin_unlock_bh(&pp->fcoe_rx_list.lock);
2054 continue;
2055 }
2056 skb->destructor = fcoe_percpu_flush_done;
2057
2058 __skb_queue_tail(&pp->fcoe_rx_list, skb);
2059 if (pp->fcoe_rx_list.qlen == 1)
2060 wake_up_process(pp->thread);
Robert Love5e5e92d2009-03-17 11:41:35 -07002061 spin_unlock_bh(&pp->fcoe_rx_list.lock);
Joe Eykholte7a51992009-08-25 14:04:08 -07002062
2063 wait_for_completion(&fcoe_flush_completion);
Robert Love85b4aa42008-12-09 15:10:24 -08002064 }
2065}
Robert Love85b4aa42008-12-09 15:10:24 -08002066
2067/**
Robert Love1875f272009-11-03 11:47:50 -08002068 * fcoe_reset() - Reset a local port
2069 * @shost: The SCSI host associated with the local port to be reset
Robert Love85b4aa42008-12-09 15:10:24 -08002070 *
Robert Love1875f272009-11-03 11:47:50 -08002071 * Returns: Always 0 (return value required by FC transport template)
Robert Love85b4aa42008-12-09 15:10:24 -08002072 */
2073int fcoe_reset(struct Scsi_Host *shost)
2074{
2075 struct fc_lport *lport = shost_priv(shost);
Vasu Devd2f80952011-02-25 15:03:28 -08002076 struct fcoe_port *port = lport_priv(lport);
2077 struct fcoe_interface *fcoe = port->priv;
2078
2079 fcoe_ctlr_link_down(&fcoe->ctlr);
2080 fcoe_clean_pending_queue(fcoe->ctlr.lp);
2081 if (!fcoe_link_ok(fcoe->ctlr.lp))
2082 fcoe_ctlr_link_up(&fcoe->ctlr);
Robert Love85b4aa42008-12-09 15:10:24 -08002083 return 0;
2084}
Robert Love85b4aa42008-12-09 15:10:24 -08002085
Robert Love34f42a02009-02-27 10:55:45 -08002086/**
Robert Love1875f272009-11-03 11:47:50 -08002087 * fcoe_hostlist_lookup_port() - Find the FCoE interface associated with a net device
2088 * @netdev: The net device used as a key
Robert Love85b4aa42008-12-09 15:10:24 -08002089 *
Robert Love1875f272009-11-03 11:47:50 -08002090 * Locking: Must be called with the RNL mutex held.
2091 *
2092 * Returns: NULL or the FCoE interface
Robert Love85b4aa42008-12-09 15:10:24 -08002093 */
Chris Leech014f5c32009-08-25 13:59:30 -07002094static struct fcoe_interface *
Robert Love1875f272009-11-03 11:47:50 -08002095fcoe_hostlist_lookup_port(const struct net_device *netdev)
Robert Love85b4aa42008-12-09 15:10:24 -08002096{
Chris Leech014f5c32009-08-25 13:59:30 -07002097 struct fcoe_interface *fcoe;
Robert Love85b4aa42008-12-09 15:10:24 -08002098
Chris Leech014f5c32009-08-25 13:59:30 -07002099 list_for_each_entry(fcoe, &fcoe_hostlist, list) {
Robert Love1875f272009-11-03 11:47:50 -08002100 if (fcoe->netdev == netdev)
Chris Leech014f5c32009-08-25 13:59:30 -07002101 return fcoe;
Robert Love85b4aa42008-12-09 15:10:24 -08002102 }
Robert Love85b4aa42008-12-09 15:10:24 -08002103 return NULL;
2104}
2105
Robert Love34f42a02009-02-27 10:55:45 -08002106/**
Robert Love1875f272009-11-03 11:47:50 -08002107 * fcoe_hostlist_lookup() - Find the local port associated with a
2108 * given net device
2109 * @netdev: The netdevice used as a key
Robert Love85b4aa42008-12-09 15:10:24 -08002110 *
Robert Love1875f272009-11-03 11:47:50 -08002111 * Locking: Must be called with the RTNL mutex held
2112 *
2113 * Returns: NULL or the local port
Robert Love85b4aa42008-12-09 15:10:24 -08002114 */
Chris Leech090eb6c2009-08-25 14:00:28 -07002115static struct fc_lport *fcoe_hostlist_lookup(const struct net_device *netdev)
Robert Love85b4aa42008-12-09 15:10:24 -08002116{
Chris Leech014f5c32009-08-25 13:59:30 -07002117 struct fcoe_interface *fcoe;
Robert Love85b4aa42008-12-09 15:10:24 -08002118
Chris Leech014f5c32009-08-25 13:59:30 -07002119 fcoe = fcoe_hostlist_lookup_port(netdev);
Chris Leech3fe9a0b2009-08-25 13:59:46 -07002120 return (fcoe) ? fcoe->ctlr.lp : NULL;
Robert Love85b4aa42008-12-09 15:10:24 -08002121}
Robert Love85b4aa42008-12-09 15:10:24 -08002122
Robert Love34f42a02009-02-27 10:55:45 -08002123/**
Robert Love1875f272009-11-03 11:47:50 -08002124 * fcoe_hostlist_add() - Add the FCoE interface identified by a local
2125 * port to the hostlist
2126 * @lport: The local port that identifies the FCoE interface to be added
2127 *
2128 * Locking: must be called with the RTNL mutex held
Robert Love85b4aa42008-12-09 15:10:24 -08002129 *
2130 * Returns: 0 for success
2131 */
Chris Leech090eb6c2009-08-25 14:00:28 -07002132static int fcoe_hostlist_add(const struct fc_lport *lport)
Robert Love85b4aa42008-12-09 15:10:24 -08002133{
Chris Leech014f5c32009-08-25 13:59:30 -07002134 struct fcoe_interface *fcoe;
2135 struct fcoe_port *port;
Robert Love85b4aa42008-12-09 15:10:24 -08002136
Chris Leech014f5c32009-08-25 13:59:30 -07002137 fcoe = fcoe_hostlist_lookup_port(fcoe_netdev(lport));
2138 if (!fcoe) {
2139 port = lport_priv(lport);
Bhanu Prakash Gollapudi8597ae82011-01-28 16:05:37 -08002140 fcoe = port->priv;
Chris Leech014f5c32009-08-25 13:59:30 -07002141 list_add_tail(&fcoe->list, &fcoe_hostlist);
Robert Love85b4aa42008-12-09 15:10:24 -08002142 }
2143 return 0;
2144}
Robert Love85b4aa42008-12-09 15:10:24 -08002145
Yi Zou78a58242011-01-28 16:05:16 -08002146
2147static struct fcoe_transport fcoe_sw_transport = {
2148 .name = {FCOE_TRANSPORT_DEFAULT},
2149 .attached = false,
2150 .list = LIST_HEAD_INIT(fcoe_sw_transport.list),
2151 .match = fcoe_match,
2152 .create = fcoe_create,
2153 .destroy = fcoe_destroy,
2154 .enable = fcoe_enable,
2155 .disable = fcoe_disable,
2156};
2157
Robert Love34f42a02009-02-27 10:55:45 -08002158/**
Robert Love1875f272009-11-03 11:47:50 -08002159 * fcoe_init() - Initialize fcoe.ko
Robert Love85b4aa42008-12-09 15:10:24 -08002160 *
Robert Love1875f272009-11-03 11:47:50 -08002161 * Returns: 0 on success, or a negative value on failure
Robert Love34f42a02009-02-27 10:55:45 -08002162 */
Robert Love85b4aa42008-12-09 15:10:24 -08002163static int __init fcoe_init(void)
2164{
Robert Love1875f272009-11-03 11:47:50 -08002165 struct fcoe_percpu_s *p;
Robert Love38eccab2009-03-17 11:41:30 -07002166 unsigned int cpu;
Robert Love8976f422009-03-17 11:41:46 -07002167 int rc = 0;
Robert Love85b4aa42008-12-09 15:10:24 -08002168
Tejun Heo2ca32b42011-01-28 16:05:32 -08002169 fcoe_wq = alloc_workqueue("fcoe", 0, 0);
2170 if (!fcoe_wq)
2171 return -ENOMEM;
2172
Yi Zou78a58242011-01-28 16:05:16 -08002173 /* register as a fcoe transport */
2174 rc = fcoe_transport_attach(&fcoe_sw_transport);
2175 if (rc) {
2176 printk(KERN_ERR "failed to register an fcoe transport, check "
2177 "if libfcoe is loaded\n");
2178 return rc;
2179 }
2180
Chris Leechdfc1d0f2009-08-25 14:00:13 -07002181 mutex_lock(&fcoe_config_mutex);
2182
Robert Love38eccab2009-03-17 11:41:30 -07002183 for_each_possible_cpu(cpu) {
Robert Love5e5e92d2009-03-17 11:41:35 -07002184 p = &per_cpu(fcoe_percpu, cpu);
Robert Love38eccab2009-03-17 11:41:30 -07002185 skb_queue_head_init(&p->fcoe_rx_list);
2186 }
2187
Robert Love8976f422009-03-17 11:41:46 -07002188 for_each_online_cpu(cpu)
2189 fcoe_percpu_thread_create(cpu);
Robert Love85b4aa42008-12-09 15:10:24 -08002190
Robert Love8976f422009-03-17 11:41:46 -07002191 /* Initialize per CPU interrupt thread */
2192 rc = register_hotcpu_notifier(&fcoe_cpu_notifier);
2193 if (rc)
2194 goto out_free;
Robert Love85b4aa42008-12-09 15:10:24 -08002195
Robert Love8976f422009-03-17 11:41:46 -07002196 /* Setup link change notification */
Robert Love85b4aa42008-12-09 15:10:24 -08002197 fcoe_dev_setup();
2198
Chris Leech5892c322009-08-25 13:59:14 -07002199 rc = fcoe_if_init();
2200 if (rc)
2201 goto out_free;
Robert Love85b4aa42008-12-09 15:10:24 -08002202
Chris Leechdfc1d0f2009-08-25 14:00:13 -07002203 mutex_unlock(&fcoe_config_mutex);
Robert Love85b4aa42008-12-09 15:10:24 -08002204 return 0;
Robert Love8976f422009-03-17 11:41:46 -07002205
2206out_free:
2207 for_each_online_cpu(cpu) {
2208 fcoe_percpu_thread_destroy(cpu);
2209 }
Chris Leechdfc1d0f2009-08-25 14:00:13 -07002210 mutex_unlock(&fcoe_config_mutex);
Tejun Heo2ca32b42011-01-28 16:05:32 -08002211 destroy_workqueue(fcoe_wq);
Robert Love8976f422009-03-17 11:41:46 -07002212 return rc;
Robert Love85b4aa42008-12-09 15:10:24 -08002213}
2214module_init(fcoe_init);
2215
2216/**
Robert Love1875f272009-11-03 11:47:50 -08002217 * fcoe_exit() - Clean up fcoe.ko
Robert Love85b4aa42008-12-09 15:10:24 -08002218 *
Robert Love1875f272009-11-03 11:47:50 -08002219 * Returns: 0 on success or a negative value on failure
Robert Love34f42a02009-02-27 10:55:45 -08002220 */
Robert Love85b4aa42008-12-09 15:10:24 -08002221static void __exit fcoe_exit(void)
2222{
Chris Leech014f5c32009-08-25 13:59:30 -07002223 struct fcoe_interface *fcoe, *tmp;
Chris Leech2e70e242009-08-25 14:00:23 -07002224 struct fcoe_port *port;
Robert Love1875f272009-11-03 11:47:50 -08002225 unsigned int cpu;
Robert Love85b4aa42008-12-09 15:10:24 -08002226
Chris Leechdfc1d0f2009-08-25 14:00:13 -07002227 mutex_lock(&fcoe_config_mutex);
2228
Robert Love85b4aa42008-12-09 15:10:24 -08002229 fcoe_dev_cleanup();
2230
Vasu Dev5919a592009-03-27 09:03:29 -07002231 /* releases the associated fcoe hosts */
Chris Leech090eb6c2009-08-25 14:00:28 -07002232 rtnl_lock();
2233 list_for_each_entry_safe(fcoe, tmp, &fcoe_hostlist, list) {
Chris Leechc863df32009-08-25 14:00:18 -07002234 list_del(&fcoe->list);
Chris Leech2e70e242009-08-25 14:00:23 -07002235 port = lport_priv(fcoe->ctlr.lp);
Chris Leech2e70e242009-08-25 14:00:23 -07002236 fcoe_interface_cleanup(fcoe);
Tejun Heo2ca32b42011-01-28 16:05:32 -08002237 queue_work(fcoe_wq, &port->destroy_work);
Chris Leechc863df32009-08-25 14:00:18 -07002238 }
Chris Leech090eb6c2009-08-25 14:00:28 -07002239 rtnl_unlock();
Robert Love85b4aa42008-12-09 15:10:24 -08002240
Robert Love8976f422009-03-17 11:41:46 -07002241 unregister_hotcpu_notifier(&fcoe_cpu_notifier);
2242
Chris Leech014f5c32009-08-25 13:59:30 -07002243 for_each_online_cpu(cpu)
Robert Love8976f422009-03-17 11:41:46 -07002244 fcoe_percpu_thread_destroy(cpu);
Robert Love85b4aa42008-12-09 15:10:24 -08002245
Chris Leechdfc1d0f2009-08-25 14:00:13 -07002246 mutex_unlock(&fcoe_config_mutex);
Chris Leech2e70e242009-08-25 14:00:23 -07002247
Tejun Heo2ca32b42011-01-28 16:05:32 -08002248 /*
2249 * destroy_work's may be chained but destroy_workqueue()
2250 * can take care of them. Just kill the fcoe_wq.
2251 */
2252 destroy_workqueue(fcoe_wq);
Chris Leech2e70e242009-08-25 14:00:23 -07002253
Tejun Heo2ca32b42011-01-28 16:05:32 -08002254 /*
2255 * Detaching from the scsi transport must happen after all
2256 * destroys are done on the fcoe_wq. destroy_workqueue will
2257 * enusre the fcoe_wq is flushed.
2258 */
Chris Leech2e70e242009-08-25 14:00:23 -07002259 fcoe_if_exit();
Yi Zou78a58242011-01-28 16:05:16 -08002260
2261 /* detach from fcoe transport */
2262 fcoe_transport_detach(&fcoe_sw_transport);
Robert Love85b4aa42008-12-09 15:10:24 -08002263}
2264module_exit(fcoe_exit);
Chris Leech11b56182009-11-03 11:46:29 -08002265
2266/**
2267 * fcoe_flogi_resp() - FCoE specific FLOGI and FDISC response handler
2268 * @seq: active sequence in the FLOGI or FDISC exchange
2269 * @fp: response frame, or error encoded in a pointer (timeout)
2270 * @arg: pointer the the fcoe_ctlr structure
2271 *
Uwe Kleine-König65155b32010-06-11 12:17:01 +02002272 * This handles MAC address management for FCoE, then passes control on to
Chris Leech11b56182009-11-03 11:46:29 -08002273 * the libfc FLOGI response handler.
2274 */
2275static void fcoe_flogi_resp(struct fc_seq *seq, struct fc_frame *fp, void *arg)
2276{
2277 struct fcoe_ctlr *fip = arg;
2278 struct fc_exch *exch = fc_seq_exch(seq);
2279 struct fc_lport *lport = exch->lp;
2280 u8 *mac;
2281
2282 if (IS_ERR(fp))
2283 goto done;
2284
2285 mac = fr_cb(fp)->granted_mac;
2286 if (is_zero_ether_addr(mac)) {
2287 /* pre-FIP */
Joe Eykholt386309c2009-11-03 11:49:16 -08002288 if (fcoe_ctlr_recv_flogi(fip, lport, fp)) {
Chris Leech11b56182009-11-03 11:46:29 -08002289 fc_frame_free(fp);
2290 return;
2291 }
Chris Leech11b56182009-11-03 11:46:29 -08002292 }
Joe Eykholt386309c2009-11-03 11:49:16 -08002293 fcoe_update_src_mac(lport, mac);
Chris Leech11b56182009-11-03 11:46:29 -08002294done:
2295 fc_lport_flogi_resp(seq, fp, lport);
2296}
2297
2298/**
2299 * fcoe_logo_resp() - FCoE specific LOGO response handler
2300 * @seq: active sequence in the LOGO exchange
2301 * @fp: response frame, or error encoded in a pointer (timeout)
2302 * @arg: pointer the the fcoe_ctlr structure
2303 *
Uwe Kleine-König65155b32010-06-11 12:17:01 +02002304 * This handles MAC address management for FCoE, then passes control on to
Chris Leech11b56182009-11-03 11:46:29 -08002305 * the libfc LOGO response handler.
2306 */
2307static void fcoe_logo_resp(struct fc_seq *seq, struct fc_frame *fp, void *arg)
2308{
Joe Eykholt386309c2009-11-03 11:49:16 -08002309 struct fc_lport *lport = arg;
Chris Leech11b56182009-11-03 11:46:29 -08002310 static u8 zero_mac[ETH_ALEN] = { 0 };
2311
2312 if (!IS_ERR(fp))
Joe Eykholt386309c2009-11-03 11:49:16 -08002313 fcoe_update_src_mac(lport, zero_mac);
Chris Leech11b56182009-11-03 11:46:29 -08002314 fc_lport_logo_resp(seq, fp, lport);
2315}
2316
2317/**
2318 * fcoe_elsct_send - FCoE specific ELS handler
2319 *
2320 * This does special case handling of FIP encapsualted ELS exchanges for FCoE,
2321 * using FCoE specific response handlers and passing the FIP controller as
2322 * the argument (the lport is still available from the exchange).
2323 *
2324 * Most of the work here is just handed off to the libfc routine.
2325 */
Robert Love1875f272009-11-03 11:47:50 -08002326static struct fc_seq *fcoe_elsct_send(struct fc_lport *lport, u32 did,
2327 struct fc_frame *fp, unsigned int op,
2328 void (*resp)(struct fc_seq *,
2329 struct fc_frame *,
2330 void *),
2331 void *arg, u32 timeout)
Chris Leech11b56182009-11-03 11:46:29 -08002332{
2333 struct fcoe_port *port = lport_priv(lport);
Bhanu Prakash Gollapudi8597ae82011-01-28 16:05:37 -08002334 struct fcoe_interface *fcoe = port->priv;
Chris Leech11b56182009-11-03 11:46:29 -08002335 struct fcoe_ctlr *fip = &fcoe->ctlr;
2336 struct fc_frame_header *fh = fc_frame_header_get(fp);
2337
2338 switch (op) {
2339 case ELS_FLOGI:
2340 case ELS_FDISC:
Joe Eykholte10f8c62010-07-20 15:20:30 -07002341 if (lport->point_to_multipoint)
2342 break;
Chris Leech11b56182009-11-03 11:46:29 -08002343 return fc_elsct_send(lport, did, fp, op, fcoe_flogi_resp,
2344 fip, timeout);
2345 case ELS_LOGO:
2346 /* only hook onto fabric logouts, not port logouts */
2347 if (ntoh24(fh->fh_d_id) != FC_FID_FLOGI)
2348 break;
2349 return fc_elsct_send(lport, did, fp, op, fcoe_logo_resp,
Joe Eykholt386309c2009-11-03 11:49:16 -08002350 lport, timeout);
Chris Leech11b56182009-11-03 11:46:29 -08002351 }
2352 return fc_elsct_send(lport, did, fp, op, resp, arg, timeout);
2353}
2354
Chris Leech9a057532009-11-03 11:46:40 -08002355/**
2356 * fcoe_vport_create() - create an fc_host/scsi_host for a vport
2357 * @vport: fc_vport object to create a new fc_host for
2358 * @disabled: start the new fc_host in a disabled state by default?
2359 *
2360 * Returns: 0 for success
2361 */
2362static int fcoe_vport_create(struct fc_vport *vport, bool disabled)
2363{
2364 struct Scsi_Host *shost = vport_to_shost(vport);
2365 struct fc_lport *n_port = shost_priv(shost);
2366 struct fcoe_port *port = lport_priv(n_port);
Bhanu Prakash Gollapudi8597ae82011-01-28 16:05:37 -08002367 struct fcoe_interface *fcoe = port->priv;
Chris Leech9a057532009-11-03 11:46:40 -08002368 struct net_device *netdev = fcoe->netdev;
2369 struct fc_lport *vn_port;
Neerav Parikhbdf25212011-05-16 16:45:29 -07002370 int rc;
2371 char buf[32];
2372
2373 rc = fcoe_validate_vport_create(vport);
2374 if (rc) {
2375 wwn_to_str(vport->port_name, buf, sizeof(buf));
2376 printk(KERN_ERR "fcoe: Failed to create vport, "
2377 "WWPN (0x%s) already exists\n",
2378 buf);
2379 return rc;
2380 }
Chris Leech9a057532009-11-03 11:46:40 -08002381
2382 mutex_lock(&fcoe_config_mutex);
2383 vn_port = fcoe_if_create(fcoe, &vport->dev, 1);
2384 mutex_unlock(&fcoe_config_mutex);
2385
2386 if (IS_ERR(vn_port)) {
2387 printk(KERN_ERR "fcoe: fcoe_vport_create(%s) failed\n",
2388 netdev->name);
2389 return -EIO;
2390 }
2391
2392 if (disabled) {
2393 fc_vport_set_state(vport, FC_VPORT_DISABLED);
2394 } else {
2395 vn_port->boot_time = jiffies;
2396 fc_fabric_login(vn_port);
2397 fc_vport_setlink(vn_port);
2398 }
2399 return 0;
2400}
2401
2402/**
2403 * fcoe_vport_destroy() - destroy the fc_host/scsi_host for a vport
2404 * @vport: fc_vport object that is being destroyed
2405 *
2406 * Returns: 0 for success
2407 */
2408static int fcoe_vport_destroy(struct fc_vport *vport)
2409{
2410 struct Scsi_Host *shost = vport_to_shost(vport);
2411 struct fc_lport *n_port = shost_priv(shost);
2412 struct fc_lport *vn_port = vport->dd_data;
2413 struct fcoe_port *port = lport_priv(vn_port);
2414
2415 mutex_lock(&n_port->lp_mutex);
2416 list_del(&vn_port->list);
2417 mutex_unlock(&n_port->lp_mutex);
Tejun Heo2ca32b42011-01-28 16:05:32 -08002418 queue_work(fcoe_wq, &port->destroy_work);
Chris Leech9a057532009-11-03 11:46:40 -08002419 return 0;
2420}
2421
2422/**
2423 * fcoe_vport_disable() - change vport state
2424 * @vport: vport to bring online/offline
2425 * @disable: should the vport be disabled?
2426 */
2427static int fcoe_vport_disable(struct fc_vport *vport, bool disable)
2428{
2429 struct fc_lport *lport = vport->dd_data;
2430
2431 if (disable) {
2432 fc_vport_set_state(vport, FC_VPORT_DISABLED);
2433 fc_fabric_logoff(lport);
2434 } else {
2435 lport->boot_time = jiffies;
2436 fc_fabric_login(lport);
2437 fc_vport_setlink(lport);
2438 }
2439
2440 return 0;
2441}
2442
Chris Leechdc8596d2009-11-03 11:47:18 -08002443/**
2444 * fcoe_vport_set_symbolic_name() - append vport string to symbolic name
2445 * @vport: fc_vport with a new symbolic name string
2446 *
2447 * After generating a new symbolic name string, a new RSPN_ID request is
2448 * sent to the name server. There is no response handler, so if it fails
2449 * for some reason it will not be retried.
2450 */
2451static void fcoe_set_vport_symbolic_name(struct fc_vport *vport)
2452{
2453 struct fc_lport *lport = vport->dd_data;
2454 struct fc_frame *fp;
2455 size_t len;
2456
2457 snprintf(fc_host_symbolic_name(lport->host), FC_SYMBOLIC_NAME_SIZE,
2458 "%s v%s over %s : %s", FCOE_NAME, FCOE_VERSION,
2459 fcoe_netdev(lport)->name, vport->symbolic_name);
2460
2461 if (lport->state != LPORT_ST_READY)
2462 return;
2463
2464 len = strnlen(fc_host_symbolic_name(lport->host), 255);
2465 fp = fc_frame_alloc(lport,
2466 sizeof(struct fc_ct_hdr) +
2467 sizeof(struct fc_ns_rspn) + len);
2468 if (!fp)
2469 return;
2470 lport->tt.elsct_send(lport, FC_FID_DIR_SERV, fp, FC_NS_RSPN_ID,
Joe Eykholtb94f8952009-11-03 11:50:21 -08002471 NULL, NULL, 3 * lport->r_a_tov);
Chris Leechdc8596d2009-11-03 11:47:18 -08002472}
Yi Zoub84056b2009-11-20 14:55:19 -08002473
2474/**
2475 * fcoe_get_lesb() - Fill the FCoE Link Error Status Block
2476 * @lport: the local port
2477 * @fc_lesb: the link error status block
2478 */
2479static void fcoe_get_lesb(struct fc_lport *lport,
2480 struct fc_els_lesb *fc_lesb)
2481{
2482 unsigned int cpu;
2483 u32 lfc, vlfc, mdac;
2484 struct fcoe_dev_stats *devst;
2485 struct fcoe_fc_els_lesb *lesb;
Eric Dumazet28172732010-07-07 14:58:56 -07002486 struct rtnl_link_stats64 temp;
Yi Zoub84056b2009-11-20 14:55:19 -08002487 struct net_device *netdev = fcoe_netdev(lport);
2488
2489 lfc = 0;
2490 vlfc = 0;
2491 mdac = 0;
2492 lesb = (struct fcoe_fc_els_lesb *)fc_lesb;
2493 memset(lesb, 0, sizeof(*lesb));
2494 for_each_possible_cpu(cpu) {
2495 devst = per_cpu_ptr(lport->dev_stats, cpu);
2496 lfc += devst->LinkFailureCount;
2497 vlfc += devst->VLinkFailureCount;
2498 mdac += devst->MissDiscAdvCount;
2499 }
2500 lesb->lesb_link_fail = htonl(lfc);
2501 lesb->lesb_vlink_fail = htonl(vlfc);
2502 lesb->lesb_miss_fka = htonl(mdac);
Eric Dumazet28172732010-07-07 14:58:56 -07002503 lesb->lesb_fcs_error = htonl(dev_get_stats(netdev, &temp)->rx_crc_errors);
Yi Zoub84056b2009-11-20 14:55:19 -08002504}
Joe Eykholt7d65b0d2010-03-12 16:08:02 -08002505
2506/**
2507 * fcoe_set_port_id() - Callback from libfc when Port_ID is set.
2508 * @lport: the local port
2509 * @port_id: the port ID
2510 * @fp: the received frame, if any, that caused the port_id to be set.
2511 *
2512 * This routine handles the case where we received a FLOGI and are
2513 * entering point-to-point mode. We need to call fcoe_ctlr_recv_flogi()
2514 * so it can set the non-mapped mode and gateway address.
2515 *
2516 * The FLOGI LS_ACC is handled by fcoe_flogi_resp().
2517 */
2518static void fcoe_set_port_id(struct fc_lport *lport,
2519 u32 port_id, struct fc_frame *fp)
2520{
2521 struct fcoe_port *port = lport_priv(lport);
Bhanu Prakash Gollapudi8597ae82011-01-28 16:05:37 -08002522 struct fcoe_interface *fcoe = port->priv;
Joe Eykholt7d65b0d2010-03-12 16:08:02 -08002523
2524 if (fp && fc_frame_payload_op(fp) == ELS_FLOGI)
2525 fcoe_ctlr_recv_flogi(&fcoe->ctlr, lport, fp);
2526}
Neerav Parikhbdf25212011-05-16 16:45:29 -07002527
2528/**
2529 * fcoe_validate_vport_create() - Validate a vport before creating it
2530 * @vport: NPIV port to be created
2531 *
2532 * This routine is meant to add validation for a vport before creating it
2533 * via fcoe_vport_create().
2534 * Current validations are:
2535 * - WWPN supplied is unique for given lport
2536 *
2537 *
2538*/
2539static int fcoe_validate_vport_create(struct fc_vport *vport)
2540{
2541 struct Scsi_Host *shost = vport_to_shost(vport);
2542 struct fc_lport *n_port = shost_priv(shost);
2543 struct fc_lport *vn_port;
2544 int rc = 0;
2545 char buf[32];
2546
2547 mutex_lock(&n_port->lp_mutex);
2548
2549 wwn_to_str(vport->port_name, buf, sizeof(buf));
2550 /* Check if the wwpn is not same as that of the lport */
2551 if (!memcmp(&n_port->wwpn, &vport->port_name, sizeof(u64))) {
2552 FCOE_DBG("vport WWPN 0x%s is same as that of the "
2553 "base port WWPN\n", buf);
2554 rc = -EINVAL;
2555 goto out;
2556 }
2557
2558 /* Check if there is any existing vport with same wwpn */
2559 list_for_each_entry(vn_port, &n_port->vports, list) {
2560 if (!memcmp(&vn_port->wwpn, &vport->port_name, sizeof(u64))) {
2561 FCOE_DBG("vport with given WWPN 0x%s already "
2562 "exists\n", buf);
2563 rc = -EINVAL;
2564 break;
2565 }
2566 }
2567
2568out:
2569 mutex_unlock(&n_port->lp_mutex);
2570
2571 return rc;
2572}