blob: 495456fe4520a30d5e4c7ef232aeb4f562e5625b [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 Devfdd780272009-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 Devfdd780272009-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 Devfdd780272009-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 Devfdd780272009-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 *);
Robert Love1875f272009-11-03 11:47:50 -0800140
141static struct libfc_function_template fcoe_libfc_fcn_templ = {
142 .frame_send = fcoe_xmit,
143 .ddp_setup = fcoe_ddp_setup,
144 .ddp_done = fcoe_ddp_done,
145 .elsct_send = fcoe_elsct_send,
Yi Zoub84056b2009-11-20 14:55:19 -0800146 .get_lesb = fcoe_get_lesb,
Joe Eykholt7d65b0d2010-03-12 16:08:02 -0800147 .lport_set_port_id = fcoe_set_port_id,
Robert Love1875f272009-11-03 11:47:50 -0800148};
Chris Leech9a057532009-11-03 11:46:40 -0800149
Yi Zou8ca86f82011-01-28 16:05:11 -0800150struct fc_function_template fcoe_nport_fc_functions = {
Vasu Dev7f349142009-03-27 09:06:31 -0700151 .show_host_node_name = 1,
152 .show_host_port_name = 1,
153 .show_host_supported_classes = 1,
154 .show_host_supported_fc4s = 1,
155 .show_host_active_fc4s = 1,
156 .show_host_maxframe_size = 1,
157
158 .show_host_port_id = 1,
159 .show_host_supported_speeds = 1,
160 .get_host_speed = fc_get_host_speed,
161 .show_host_speed = 1,
162 .show_host_port_type = 1,
163 .get_host_port_state = fc_get_host_port_state,
164 .show_host_port_state = 1,
165 .show_host_symbolic_name = 1,
166
167 .dd_fcrport_size = sizeof(struct fc_rport_libfc_priv),
168 .show_rport_maxframe_size = 1,
169 .show_rport_supported_classes = 1,
170
171 .show_host_fabric_name = 1,
172 .show_starget_node_name = 1,
173 .show_starget_port_name = 1,
174 .show_starget_port_id = 1,
175 .set_rport_dev_loss_tmo = fc_set_rport_loss_tmo,
176 .show_rport_dev_loss_tmo = 1,
177 .get_fc_host_stats = fc_get_host_stats,
178 .issue_fc_host_lip = fcoe_reset,
179
180 .terminate_rport_io = fc_rport_terminate_io,
Chris Leech9a057532009-11-03 11:46:40 -0800181
182 .vport_create = fcoe_vport_create,
183 .vport_delete = fcoe_vport_destroy,
184 .vport_disable = fcoe_vport_disable,
Chris Leechdc8596d2009-11-03 11:47:18 -0800185 .set_vport_symbolic_name = fcoe_set_vport_symbolic_name,
Steve Maa51ab392009-11-03 11:47:34 -0800186
187 .bsg_request = fc_lport_bsg_request,
Vasu Dev7f349142009-03-27 09:06:31 -0700188};
189
Yi Zou8ca86f82011-01-28 16:05:11 -0800190struct fc_function_template fcoe_vport_fc_functions = {
Chris Leeche9084bb2009-11-03 11:46:34 -0800191 .show_host_node_name = 1,
192 .show_host_port_name = 1,
193 .show_host_supported_classes = 1,
194 .show_host_supported_fc4s = 1,
195 .show_host_active_fc4s = 1,
196 .show_host_maxframe_size = 1,
197
198 .show_host_port_id = 1,
199 .show_host_supported_speeds = 1,
200 .get_host_speed = fc_get_host_speed,
201 .show_host_speed = 1,
202 .show_host_port_type = 1,
203 .get_host_port_state = fc_get_host_port_state,
204 .show_host_port_state = 1,
205 .show_host_symbolic_name = 1,
206
207 .dd_fcrport_size = sizeof(struct fc_rport_libfc_priv),
208 .show_rport_maxframe_size = 1,
209 .show_rport_supported_classes = 1,
210
211 .show_host_fabric_name = 1,
212 .show_starget_node_name = 1,
213 .show_starget_port_name = 1,
214 .show_starget_port_id = 1,
215 .set_rport_dev_loss_tmo = fc_set_rport_loss_tmo,
216 .show_rport_dev_loss_tmo = 1,
217 .get_fc_host_stats = fc_get_host_stats,
218 .issue_fc_host_lip = fcoe_reset,
219
220 .terminate_rport_io = fc_rport_terminate_io,
Steve Maa51ab392009-11-03 11:47:34 -0800221
222 .bsg_request = fc_lport_bsg_request,
Chris Leeche9084bb2009-11-03 11:46:34 -0800223};
224
Vasu Dev7f349142009-03-27 09:06:31 -0700225static struct scsi_host_template fcoe_shost_template = {
226 .module = THIS_MODULE,
227 .name = "FCoE Driver",
228 .proc_name = FCOE_NAME,
229 .queuecommand = fc_queuecommand,
230 .eh_abort_handler = fc_eh_abort,
231 .eh_device_reset_handler = fc_eh_device_reset,
232 .eh_host_reset_handler = fc_eh_host_reset,
233 .slave_alloc = fc_slave_alloc,
234 .change_queue_depth = fc_change_queue_depth,
235 .change_queue_type = fc_change_queue_type,
236 .this_id = -1,
Vasu Dev14caf442009-10-15 17:46:55 -0700237 .cmd_per_lun = 3,
Vasu Dev7f349142009-03-27 09:06:31 -0700238 .can_queue = FCOE_MAX_OUTSTANDING_COMMANDS,
239 .use_clustering = ENABLE_CLUSTERING,
240 .sg_tablesize = SG_ALL,
241 .max_sectors = 0xffff,
242};
243
Chris Leech54b649f2009-08-25 14:00:07 -0700244/**
Robert Love1875f272009-11-03 11:47:50 -0800245 * fcoe_interface_setup() - Setup a FCoE interface
246 * @fcoe: The new FCoE interface
247 * @netdev: The net device that the fcoe interface is on
Chris Leech54b649f2009-08-25 14:00:07 -0700248 *
249 * Returns : 0 for success
Chris Leech2e70e242009-08-25 14:00:23 -0700250 * Locking: must be called with the RTNL mutex held
Chris Leech54b649f2009-08-25 14:00:07 -0700251 */
252static int fcoe_interface_setup(struct fcoe_interface *fcoe,
253 struct net_device *netdev)
254{
255 struct fcoe_ctlr *fip = &fcoe->ctlr;
256 struct netdev_hw_addr *ha;
Yi Zou5bab87e2009-11-03 11:49:43 -0800257 struct net_device *real_dev;
Chris Leech54b649f2009-08-25 14:00:07 -0700258 u8 flogi_maddr[ETH_ALEN];
Yi Zoub7a727f2009-10-21 16:28:03 -0700259 const struct net_device_ops *ops;
Chris Leech54b649f2009-08-25 14:00:07 -0700260
261 fcoe->netdev = netdev;
262
Yi Zoub7a727f2009-10-21 16:28:03 -0700263 /* Let LLD initialize for FCoE */
264 ops = netdev->netdev_ops;
265 if (ops->ndo_fcoe_enable) {
266 if (ops->ndo_fcoe_enable(netdev))
267 FCOE_NETDEV_DBG(netdev, "Failed to enable FCoE"
268 " specific feature for LLD.\n");
269 }
270
Chris Leech54b649f2009-08-25 14:00:07 -0700271 /* Do not support for bonding device */
272 if ((netdev->priv_flags & IFF_MASTER_ALB) ||
273 (netdev->priv_flags & IFF_SLAVE_INACTIVE) ||
274 (netdev->priv_flags & IFF_MASTER_8023AD)) {
john fastabend59d92512009-11-03 11:48:44 -0800275 FCOE_NETDEV_DBG(netdev, "Bonded interfaces not supported\n");
Chris Leech54b649f2009-08-25 14:00:07 -0700276 return -EOPNOTSUPP;
277 }
278
279 /* look for SAN MAC address, if multiple SAN MACs exist, only
280 * use the first one for SPMA */
Yi Zou5bab87e2009-11-03 11:49:43 -0800281 real_dev = (netdev->priv_flags & IFF_802_1Q_VLAN) ?
282 vlan_dev_real_dev(netdev) : netdev;
Chris Leech54b649f2009-08-25 14:00:07 -0700283 rcu_read_lock();
Yi Zou5bab87e2009-11-03 11:49:43 -0800284 for_each_dev_addr(real_dev, ha) {
Chris Leech54b649f2009-08-25 14:00:07 -0700285 if ((ha->type == NETDEV_HW_ADDR_T_SAN) &&
Yi Zoubf361702009-11-03 11:49:38 -0800286 (is_valid_ether_addr(ha->addr))) {
Chris Leech54b649f2009-08-25 14:00:07 -0700287 memcpy(fip->ctl_src_addr, ha->addr, ETH_ALEN);
288 fip->spma = 1;
289 break;
290 }
291 }
292 rcu_read_unlock();
293
294 /* setup Source Mac Address */
295 if (!fip->spma)
296 memcpy(fip->ctl_src_addr, netdev->dev_addr, netdev->addr_len);
297
298 /*
299 * Add FCoE MAC address as second unicast MAC address
300 * or enter promiscuous mode if not capable of listening
301 * for multiple unicast MACs.
302 */
Chris Leech54b649f2009-08-25 14:00:07 -0700303 memcpy(flogi_maddr, (u8[6]) FC_FCOE_FLOGI_MAC, ETH_ALEN);
Jiri Pirkoa748ee22010-04-01 21:22:09 +0000304 dev_uc_add(netdev, flogi_maddr);
Chris Leech54b649f2009-08-25 14:00:07 -0700305 if (fip->spma)
Jiri Pirkoa748ee22010-04-01 21:22:09 +0000306 dev_uc_add(netdev, fip->ctl_src_addr);
Joe Eykholte10f8c62010-07-20 15:20:30 -0700307 if (fip->mode == FIP_MODE_VN2VN) {
308 dev_mc_add(netdev, FIP_ALL_VN2VN_MACS);
309 dev_mc_add(netdev, FIP_ALL_P2P_MACS);
310 } else
311 dev_mc_add(netdev, FIP_ALL_ENODE_MACS);
Chris Leech54b649f2009-08-25 14:00:07 -0700312
313 /*
314 * setup the receive function from ethernet driver
315 * on the ethertype for the given device
316 */
317 fcoe->fcoe_packet_type.func = fcoe_rcv;
318 fcoe->fcoe_packet_type.type = __constant_htons(ETH_P_FCOE);
319 fcoe->fcoe_packet_type.dev = netdev;
320 dev_add_pack(&fcoe->fcoe_packet_type);
321
322 fcoe->fip_packet_type.func = fcoe_fip_recv;
323 fcoe->fip_packet_type.type = htons(ETH_P_FIP);
324 fcoe->fip_packet_type.dev = netdev;
325 dev_add_pack(&fcoe->fip_packet_type);
326
327 return 0;
328}
329
Vasu Dev7f349142009-03-27 09:06:31 -0700330/**
Robert Love1875f272009-11-03 11:47:50 -0800331 * fcoe_interface_create() - Create a FCoE interface on a net device
332 * @netdev: The net device to create the FCoE interface on
Joe Eykholt1dd454d2010-07-20 15:20:46 -0700333 * @fip_mode: The mode to use for FIP
Chris Leech030f4e02009-08-25 14:00:02 -0700334 *
335 * Returns: pointer to a struct fcoe_interface or NULL on error
336 */
Joe Eykholt1dd454d2010-07-20 15:20:46 -0700337static struct fcoe_interface *fcoe_interface_create(struct net_device *netdev,
338 enum fip_state fip_mode)
Chris Leech030f4e02009-08-25 14:00:02 -0700339{
340 struct fcoe_interface *fcoe;
john fastabend59d92512009-11-03 11:48:44 -0800341 int err;
Chris Leech030f4e02009-08-25 14:00:02 -0700342
Robert Love7287fb92011-01-28 16:03:47 -0800343 if (!try_module_get(THIS_MODULE)) {
344 FCOE_NETDEV_DBG(netdev,
345 "Could not get a reference to the module\n");
346 fcoe = ERR_PTR(-EBUSY);
347 goto out;
348 }
349
Chris Leech030f4e02009-08-25 14:00:02 -0700350 fcoe = kzalloc(sizeof(*fcoe), GFP_KERNEL);
351 if (!fcoe) {
352 FCOE_NETDEV_DBG(netdev, "Could not allocate fcoe structure\n");
Robert Love7287fb92011-01-28 16:03:47 -0800353 fcoe = ERR_PTR(-ENOMEM);
354 goto out_nomod;
Chris Leech030f4e02009-08-25 14:00:02 -0700355 }
356
Chris Leech2e70e242009-08-25 14:00:23 -0700357 dev_hold(netdev);
Chris Leech030f4e02009-08-25 14:00:02 -0700358 kref_init(&fcoe->kref);
Chris Leech54b649f2009-08-25 14:00:07 -0700359
360 /*
361 * Initialize FIP.
362 */
Joe Eykholt1dd454d2010-07-20 15:20:46 -0700363 fcoe_ctlr_init(&fcoe->ctlr, fip_mode);
Chris Leech54b649f2009-08-25 14:00:07 -0700364 fcoe->ctlr.send = fcoe_fip_send;
365 fcoe->ctlr.update_mac = fcoe_update_src_mac;
Chris Leech11b56182009-11-03 11:46:29 -0800366 fcoe->ctlr.get_src_addr = fcoe_get_src_mac;
Chris Leech54b649f2009-08-25 14:00:07 -0700367
john fastabend59d92512009-11-03 11:48:44 -0800368 err = fcoe_interface_setup(fcoe, netdev);
369 if (err) {
370 fcoe_ctlr_destroy(&fcoe->ctlr);
371 kfree(fcoe);
372 dev_put(netdev);
Robert Love7287fb92011-01-28 16:03:47 -0800373 fcoe = ERR_PTR(err);
374 goto out_nomod;
john fastabend59d92512009-11-03 11:48:44 -0800375 }
Chris Leech030f4e02009-08-25 14:00:02 -0700376
Robert Love7287fb92011-01-28 16:03:47 -0800377 goto out;
378
379out_nomod:
380 module_put(THIS_MODULE);
381out:
Chris Leech030f4e02009-08-25 14:00:02 -0700382 return fcoe;
383}
384
385/**
Robert Love1875f272009-11-03 11:47:50 -0800386 * fcoe_interface_cleanup() - Clean up a FCoE interface
387 * @fcoe: The FCoE interface to be cleaned up
Chris Leech2e70e242009-08-25 14:00:23 -0700388 *
389 * Caller must be holding the RTNL mutex
Chris Leech54b649f2009-08-25 14:00:07 -0700390 */
391void fcoe_interface_cleanup(struct fcoe_interface *fcoe)
392{
393 struct net_device *netdev = fcoe->netdev;
394 struct fcoe_ctlr *fip = &fcoe->ctlr;
395 u8 flogi_maddr[ETH_ALEN];
Yi Zoub7a727f2009-10-21 16:28:03 -0700396 const struct net_device_ops *ops;
Chris Leech54b649f2009-08-25 14:00:07 -0700397
398 /*
399 * Don't listen for Ethernet packets anymore.
400 * synchronize_net() ensures that the packet handlers are not running
401 * on another CPU. dev_remove_pack() would do that, this calls the
402 * unsyncronized version __dev_remove_pack() to avoid multiple delays.
403 */
404 __dev_remove_pack(&fcoe->fcoe_packet_type);
405 __dev_remove_pack(&fcoe->fip_packet_type);
406 synchronize_net();
407
Chris Leech54b649f2009-08-25 14:00:07 -0700408 /* Delete secondary MAC addresses */
Chris Leech54b649f2009-08-25 14:00:07 -0700409 memcpy(flogi_maddr, (u8[6]) FC_FCOE_FLOGI_MAC, ETH_ALEN);
Jiri Pirkoa748ee22010-04-01 21:22:09 +0000410 dev_uc_del(netdev, flogi_maddr);
Chris Leech54b649f2009-08-25 14:00:07 -0700411 if (fip->spma)
Jiri Pirkoa748ee22010-04-01 21:22:09 +0000412 dev_uc_del(netdev, fip->ctl_src_addr);
Joe Eykholte10f8c62010-07-20 15:20:30 -0700413 if (fip->mode == FIP_MODE_VN2VN) {
414 dev_mc_del(netdev, FIP_ALL_VN2VN_MACS);
415 dev_mc_del(netdev, FIP_ALL_P2P_MACS);
416 } else
417 dev_mc_del(netdev, FIP_ALL_ENODE_MACS);
Yi Zoub7a727f2009-10-21 16:28:03 -0700418
419 /* Tell the LLD we are done w/ FCoE */
420 ops = netdev->netdev_ops;
421 if (ops->ndo_fcoe_disable) {
422 if (ops->ndo_fcoe_disable(netdev))
423 FCOE_NETDEV_DBG(netdev, "Failed to disable FCoE"
424 " specific feature for LLD.\n");
425 }
Chris Leech54b649f2009-08-25 14:00:07 -0700426}
427
428/**
Chris Leech030f4e02009-08-25 14:00:02 -0700429 * fcoe_interface_release() - fcoe_port kref release function
Robert Love1875f272009-11-03 11:47:50 -0800430 * @kref: Embedded reference count in an fcoe_interface struct
Chris Leech030f4e02009-08-25 14:00:02 -0700431 */
432static void fcoe_interface_release(struct kref *kref)
433{
434 struct fcoe_interface *fcoe;
Chris Leech2e70e242009-08-25 14:00:23 -0700435 struct net_device *netdev;
Chris Leech030f4e02009-08-25 14:00:02 -0700436
437 fcoe = container_of(kref, struct fcoe_interface, kref);
Chris Leech2e70e242009-08-25 14:00:23 -0700438 netdev = fcoe->netdev;
439 /* tear-down the FCoE controller */
440 fcoe_ctlr_destroy(&fcoe->ctlr);
Chris Leech030f4e02009-08-25 14:00:02 -0700441 kfree(fcoe);
Chris Leech2e70e242009-08-25 14:00:23 -0700442 dev_put(netdev);
Robert Love7287fb92011-01-28 16:03:47 -0800443 module_put(THIS_MODULE);
Chris Leech030f4e02009-08-25 14:00:02 -0700444}
445
446/**
Robert Love1875f272009-11-03 11:47:50 -0800447 * fcoe_interface_get() - Get a reference to a FCoE interface
448 * @fcoe: The FCoE interface to be held
Chris Leech030f4e02009-08-25 14:00:02 -0700449 */
450static inline void fcoe_interface_get(struct fcoe_interface *fcoe)
451{
452 kref_get(&fcoe->kref);
453}
454
455/**
Robert Love1875f272009-11-03 11:47:50 -0800456 * fcoe_interface_put() - Put a reference to a FCoE interface
457 * @fcoe: The FCoE interface to be released
Chris Leech030f4e02009-08-25 14:00:02 -0700458 */
459static inline void fcoe_interface_put(struct fcoe_interface *fcoe)
460{
461 kref_put(&fcoe->kref, fcoe_interface_release);
462}
463
464/**
Robert Love1875f272009-11-03 11:47:50 -0800465 * fcoe_fip_recv() - Handler for received FIP frames
466 * @skb: The receive skb
467 * @netdev: The associated net device
468 * @ptype: The packet_type structure which was used to register this handler
469 * @orig_dev: The original net_device the the skb was received on.
470 * (in case dev is a bond)
Vasu Devab6b85c2009-05-17 12:33:08 +0000471 *
472 * Returns: 0 for success
473 */
Robert Love1875f272009-11-03 11:47:50 -0800474static int fcoe_fip_recv(struct sk_buff *skb, struct net_device *netdev,
Vasu Devab6b85c2009-05-17 12:33:08 +0000475 struct packet_type *ptype,
476 struct net_device *orig_dev)
477{
Chris Leech259ad852009-08-25 13:59:41 -0700478 struct fcoe_interface *fcoe;
Vasu Devab6b85c2009-05-17 12:33:08 +0000479
Chris Leech259ad852009-08-25 13:59:41 -0700480 fcoe = container_of(ptype, struct fcoe_interface, fip_packet_type);
Chris Leech3fe9a0b2009-08-25 13:59:46 -0700481 fcoe_ctlr_recv(&fcoe->ctlr, skb);
Vasu Devab6b85c2009-05-17 12:33:08 +0000482 return 0;
483}
484
485/**
Robert Love1875f272009-11-03 11:47:50 -0800486 * fcoe_fip_send() - Send an Ethernet-encapsulated FIP frame
487 * @fip: The FCoE controller
488 * @skb: The FIP packet to be sent
Vasu Devab6b85c2009-05-17 12:33:08 +0000489 */
490static void fcoe_fip_send(struct fcoe_ctlr *fip, struct sk_buff *skb)
491{
Chris Leech3fe9a0b2009-08-25 13:59:46 -0700492 skb->dev = fcoe_from_ctlr(fip)->netdev;
Vasu Devab6b85c2009-05-17 12:33:08 +0000493 dev_queue_xmit(skb);
494}
495
496/**
Robert Love1875f272009-11-03 11:47:50 -0800497 * fcoe_update_src_mac() - Update the Ethernet MAC filters
498 * @lport: The local port to update the source MAC on
499 * @addr: Unicast MAC address to add
Vasu Devab6b85c2009-05-17 12:33:08 +0000500 *
501 * Remove any previously-set unicast MAC filter.
502 * Add secondary FCoE MAC address filter for our OUI.
503 */
Chris Leech11b56182009-11-03 11:46:29 -0800504static void fcoe_update_src_mac(struct fc_lport *lport, u8 *addr)
Vasu Devab6b85c2009-05-17 12:33:08 +0000505{
Chris Leech11b56182009-11-03 11:46:29 -0800506 struct fcoe_port *port = lport_priv(lport);
Bhanu Prakash Gollapudi8597ae82011-01-28 16:05:37 -0800507 struct fcoe_interface *fcoe = port->priv;
Vasu Devab6b85c2009-05-17 12:33:08 +0000508
Vasu Devab6b85c2009-05-17 12:33:08 +0000509 rtnl_lock();
Chris Leech11b56182009-11-03 11:46:29 -0800510 if (!is_zero_ether_addr(port->data_src_addr))
Jiri Pirkoa748ee22010-04-01 21:22:09 +0000511 dev_uc_del(fcoe->netdev, port->data_src_addr);
Chris Leech11b56182009-11-03 11:46:29 -0800512 if (!is_zero_ether_addr(addr))
Jiri Pirkoa748ee22010-04-01 21:22:09 +0000513 dev_uc_add(fcoe->netdev, addr);
Chris Leech11b56182009-11-03 11:46:29 -0800514 memcpy(port->data_src_addr, addr, ETH_ALEN);
Vasu Devab6b85c2009-05-17 12:33:08 +0000515 rtnl_unlock();
516}
517
518/**
Chris Leech11b56182009-11-03 11:46:29 -0800519 * fcoe_get_src_mac() - return the Ethernet source address for an lport
520 * @lport: libfc lport
521 */
522static u8 *fcoe_get_src_mac(struct fc_lport *lport)
523{
524 struct fcoe_port *port = lport_priv(lport);
525
526 return port->data_src_addr;
527}
528
529/**
Robert Love1875f272009-11-03 11:47:50 -0800530 * fcoe_lport_config() - Set up a local port
531 * @lport: The local port to be setup
Vasu Dev7f349142009-03-27 09:06:31 -0700532 *
533 * Returns: 0 for success
534 */
Robert Love1875f272009-11-03 11:47:50 -0800535static int fcoe_lport_config(struct fc_lport *lport)
Vasu Dev7f349142009-03-27 09:06:31 -0700536{
Robert Love1875f272009-11-03 11:47:50 -0800537 lport->link_up = 0;
538 lport->qfull = 0;
539 lport->max_retry_count = 3;
540 lport->max_rport_retry_count = 3;
541 lport->e_d_tov = 2 * 1000; /* FC-FS default */
542 lport->r_a_tov = 2 * 2 * 1000;
543 lport->service_params = (FCP_SPPF_INIT_FCN | FCP_SPPF_RD_XRDY_DIS |
544 FCP_SPPF_RETRY | FCP_SPPF_CONF_COMPL);
545 lport->does_npiv = 1;
Vasu Dev7f349142009-03-27 09:06:31 -0700546
Robert Love1875f272009-11-03 11:47:50 -0800547 fc_lport_init_stats(lport);
Vasu Dev7f349142009-03-27 09:06:31 -0700548
549 /* lport fc_lport related configuration */
Robert Love1875f272009-11-03 11:47:50 -0800550 fc_lport_config(lport);
Vasu Dev7f349142009-03-27 09:06:31 -0700551
552 /* offload related configuration */
Robert Love1875f272009-11-03 11:47:50 -0800553 lport->crc_offload = 0;
554 lport->seq_offload = 0;
555 lport->lro_enabled = 0;
556 lport->lro_xid = 0;
557 lport->lso_max = 0;
Vasu Dev7f349142009-03-27 09:06:31 -0700558
559 return 0;
560}
561
562/**
Yi Zoudcece412009-11-20 15:22:21 -0800563 * fcoe_get_wwn() - Get the world wide name from LLD if it supports it
564 * @netdev: the associated net device
565 * @wwn: the output WWN
566 * @type: the type of WWN (WWPN or WWNN)
567 *
568 * Returns: 0 for success
569 */
570static int fcoe_get_wwn(struct net_device *netdev, u64 *wwn, int type)
571{
572 const struct net_device_ops *ops = netdev->netdev_ops;
573
574 if (ops->ndo_fcoe_get_wwn)
575 return ops->ndo_fcoe_get_wwn(netdev, wwn, type);
576 return -EINVAL;
577}
578
579/**
Yi Zou54a5b212010-07-20 15:21:17 -0700580 * fcoe_netdev_features_change - Updates the lport's offload flags based
581 * on the LLD netdev's FCoE feature flags
582 */
583static void fcoe_netdev_features_change(struct fc_lport *lport,
584 struct net_device *netdev)
585{
586 mutex_lock(&lport->lp_mutex);
587
588 if (netdev->features & NETIF_F_SG)
589 lport->sg_supp = 1;
590 else
591 lport->sg_supp = 0;
592
593 if (netdev->features & NETIF_F_FCOE_CRC) {
594 lport->crc_offload = 1;
595 FCOE_NETDEV_DBG(netdev, "Supports FCCRC offload\n");
596 } else {
597 lport->crc_offload = 0;
598 }
599
600 if (netdev->features & NETIF_F_FSO) {
601 lport->seq_offload = 1;
602 lport->lso_max = netdev->gso_max_size;
603 FCOE_NETDEV_DBG(netdev, "Supports LSO for max len 0x%x\n",
604 lport->lso_max);
605 } else {
606 lport->seq_offload = 0;
607 lport->lso_max = 0;
608 }
609
610 if (netdev->fcoe_ddp_xid) {
611 lport->lro_enabled = 1;
612 lport->lro_xid = netdev->fcoe_ddp_xid;
613 FCOE_NETDEV_DBG(netdev, "Supports LRO for max xid 0x%x\n",
614 lport->lro_xid);
615 } else {
616 lport->lro_enabled = 0;
617 lport->lro_xid = 0;
618 }
619
620 mutex_unlock(&lport->lp_mutex);
621}
622
623/**
Robert Love1875f272009-11-03 11:47:50 -0800624 * fcoe_netdev_config() - Set up net devive for SW FCoE
625 * @lport: The local port that is associated with the net device
626 * @netdev: The associated net device
Vasu Dev7f349142009-03-27 09:06:31 -0700627 *
Robert Love1875f272009-11-03 11:47:50 -0800628 * Must be called after fcoe_lport_config() as it will use local port mutex
Vasu Dev7f349142009-03-27 09:06:31 -0700629 *
Robert Love1875f272009-11-03 11:47:50 -0800630 * Returns: 0 for success
Vasu Dev7f349142009-03-27 09:06:31 -0700631 */
Robert Love1875f272009-11-03 11:47:50 -0800632static int fcoe_netdev_config(struct fc_lport *lport, struct net_device *netdev)
Vasu Dev7f349142009-03-27 09:06:31 -0700633{
634 u32 mfs;
635 u64 wwnn, wwpn;
Chris Leech25024982009-08-25 13:59:35 -0700636 struct fcoe_interface *fcoe;
Chris Leech014f5c32009-08-25 13:59:30 -0700637 struct fcoe_port *port;
Vasu Dev7f349142009-03-27 09:06:31 -0700638
639 /* Setup lport private data to point to fcoe softc */
Robert Love1875f272009-11-03 11:47:50 -0800640 port = lport_priv(lport);
Bhanu Prakash Gollapudi8597ae82011-01-28 16:05:37 -0800641 fcoe = port->priv;
Vasu Dev7f349142009-03-27 09:06:31 -0700642
643 /*
644 * Determine max frame size based on underlying device and optional
645 * user-configured limit. If the MFS is too low, fcoe_link_ok()
646 * will return 0, so do this first.
647 */
Yi Zou7221d7e2009-10-21 16:27:52 -0700648 mfs = netdev->mtu;
649 if (netdev->features & NETIF_F_FCOE_MTU) {
650 mfs = FCOE_MTU;
651 FCOE_NETDEV_DBG(netdev, "Supports FCOE_MTU of %d bytes\n", mfs);
652 }
653 mfs -= (sizeof(struct fcoe_hdr) + sizeof(struct fcoe_crc_eof));
Robert Love1875f272009-11-03 11:47:50 -0800654 if (fc_set_mfs(lport, mfs))
Vasu Dev7f349142009-03-27 09:06:31 -0700655 return -EINVAL;
656
Vasu Dev7f349142009-03-27 09:06:31 -0700657 /* offload features support */
Yi Zou54a5b212010-07-20 15:21:17 -0700658 fcoe_netdev_features_change(lport, netdev);
Vasu Dev7f349142009-03-27 09:06:31 -0700659
Chris Leech014f5c32009-08-25 13:59:30 -0700660 skb_queue_head_init(&port->fcoe_pending_queue);
661 port->fcoe_pending_queue_active = 0;
Robert Love1875f272009-11-03 11:47:50 -0800662 setup_timer(&port->timer, fcoe_queue_timer, (unsigned long)lport);
Vasu Dev7f349142009-03-27 09:06:31 -0700663
Robert Love5e4f8fe2010-05-07 15:18:35 -0700664 fcoe_link_speed_update(lport);
665
Robert Love1875f272009-11-03 11:47:50 -0800666 if (!lport->vport) {
Yi Zoudcece412009-11-20 15:22:21 -0800667 if (fcoe_get_wwn(netdev, &wwnn, NETDEV_FCOE_WWNN))
668 wwnn = fcoe_wwn_from_mac(fcoe->ctlr.ctl_src_addr, 1, 0);
Robert Love1875f272009-11-03 11:47:50 -0800669 fc_set_wwnn(lport, wwnn);
Yi Zoudcece412009-11-20 15:22:21 -0800670 if (fcoe_get_wwn(netdev, &wwpn, NETDEV_FCOE_WWPN))
671 wwpn = fcoe_wwn_from_mac(fcoe->ctlr.ctl_src_addr,
Vasu Devcf4aebca2010-07-20 15:21:22 -0700672 2, 0);
Robert Love1875f272009-11-03 11:47:50 -0800673 fc_set_wwpn(lport, wwpn);
Chris Leech9a057532009-11-03 11:46:40 -0800674 }
Vasu Dev7f349142009-03-27 09:06:31 -0700675
Vasu Dev7f349142009-03-27 09:06:31 -0700676 return 0;
677}
678
679/**
Robert Love1875f272009-11-03 11:47:50 -0800680 * fcoe_shost_config() - Set up the SCSI host associated with a local port
681 * @lport: The local port
Robert Love1875f272009-11-03 11:47:50 -0800682 * @dev: The device associated with the SCSI host
Vasu Dev7f349142009-03-27 09:06:31 -0700683 *
684 * Must be called after fcoe_lport_config() and fcoe_netdev_config()
685 *
Robert Love1875f272009-11-03 11:47:50 -0800686 * Returns: 0 for success
Vasu Dev7f349142009-03-27 09:06:31 -0700687 */
Vasu Dev8ba00a42010-04-09 14:22:54 -0700688static int fcoe_shost_config(struct fc_lport *lport, struct device *dev)
Vasu Dev7f349142009-03-27 09:06:31 -0700689{
690 int rc = 0;
691
692 /* lport scsi host config */
Robert Love1875f272009-11-03 11:47:50 -0800693 lport->host->max_lun = FCOE_MAX_LUN;
694 lport->host->max_id = FCOE_MAX_FCP_TARGET;
695 lport->host->max_channel = 0;
Vasu Devda87bfa2010-04-09 14:22:59 -0700696 lport->host->max_cmd_len = FCOE_MAX_CMD_LEN;
697
Robert Love1875f272009-11-03 11:47:50 -0800698 if (lport->vport)
Yi Zou8ca86f82011-01-28 16:05:11 -0800699 lport->host->transportt = fcoe_vport_scsi_transport;
Chris Leeche9084bb2009-11-03 11:46:34 -0800700 else
Yi Zou8ca86f82011-01-28 16:05:11 -0800701 lport->host->transportt = fcoe_nport_scsi_transport;
Vasu Dev7f349142009-03-27 09:06:31 -0700702
703 /* add the new host to the SCSI-ml */
Robert Love1875f272009-11-03 11:47:50 -0800704 rc = scsi_add_host(lport->host, dev);
Vasu Dev7f349142009-03-27 09:06:31 -0700705 if (rc) {
Robert Love1875f272009-11-03 11:47:50 -0800706 FCOE_NETDEV_DBG(fcoe_netdev(lport), "fcoe_shost_config: "
Robert Loved5488eb2009-06-10 15:30:59 -0700707 "error on scsi_add_host\n");
Vasu Dev7f349142009-03-27 09:06:31 -0700708 return rc;
709 }
Chris Leech9a057532009-11-03 11:46:40 -0800710
Robert Love1875f272009-11-03 11:47:50 -0800711 if (!lport->vport)
Alexey Dobriyan4be929b2010-05-24 14:33:03 -0700712 fc_host_max_npiv_vports(lport->host) = USHRT_MAX;
Chris Leech9a057532009-11-03 11:46:40 -0800713
Robert Love1875f272009-11-03 11:47:50 -0800714 snprintf(fc_host_symbolic_name(lport->host), FC_SYMBOLIC_NAME_SIZE,
Chris Leech5baa17c2009-11-03 11:46:56 -0800715 "%s v%s over %s", FCOE_NAME, FCOE_VERSION,
Robert Love1875f272009-11-03 11:47:50 -0800716 fcoe_netdev(lport)->name);
Vasu Dev7f349142009-03-27 09:06:31 -0700717
718 return 0;
719}
720
Robert Love1875f272009-11-03 11:47:50 -0800721/**
722 * fcoe_oem_match() - The match routine for the offloaded exchange manager
723 * @fp: The I/O frame
Vasu Devd7179682009-07-29 17:05:21 -0700724 *
Robert Love1875f272009-11-03 11:47:50 -0800725 * This routine will be associated with an exchange manager (EM). When
726 * the libfc exchange handling code is looking for an EM to use it will
727 * call this routine and pass it the frame that it wishes to send. This
728 * routine will return True if the associated EM is to be used and False
729 * if the echange code should continue looking for an EM.
730 *
731 * The offload EM that this routine is associated with will handle any
732 * packets that are for SCSI read requests.
733 *
734 * Returns: True for read types I/O, otherwise returns false.
Vasu Devd7179682009-07-29 17:05:21 -0700735 */
736bool fcoe_oem_match(struct fc_frame *fp)
737{
Yi Zou05cc7392009-08-25 13:59:03 -0700738 return fc_fcp_is_read(fr_fsp(fp)) &&
739 (fr_fsp(fp)->data_len > fcoe_ddp_min);
Vasu Devd7179682009-07-29 17:05:21 -0700740}
741
Vasu Dev7f349142009-03-27 09:06:31 -0700742/**
Robert Love1875f272009-11-03 11:47:50 -0800743 * fcoe_em_config() - Allocate and configure an exchange manager
744 * @lport: The local port that the new EM will be associated with
Vasu Dev7f349142009-03-27 09:06:31 -0700745 *
Robert Love1875f272009-11-03 11:47:50 -0800746 * Returns: 0 on success
Vasu Dev7f349142009-03-27 09:06:31 -0700747 */
Robert Love1875f272009-11-03 11:47:50 -0800748static inline int fcoe_em_config(struct fc_lport *lport)
Vasu Dev7f349142009-03-27 09:06:31 -0700749{
Robert Love1875f272009-11-03 11:47:50 -0800750 struct fcoe_port *port = lport_priv(lport);
Bhanu Prakash Gollapudi8597ae82011-01-28 16:05:37 -0800751 struct fcoe_interface *fcoe = port->priv;
Chris Leech25024982009-08-25 13:59:35 -0700752 struct fcoe_interface *oldfcoe = NULL;
Vasu Dev1d1b88d2009-07-29 17:05:45 -0700753 struct net_device *old_real_dev, *cur_real_dev;
Vasu Devd7179682009-07-29 17:05:21 -0700754 u16 min_xid = FCOE_MIN_XID;
755 u16 max_xid = FCOE_MAX_XID;
756
757 /*
758 * Check if need to allocate an em instance for
759 * offload exchange ids to be shared across all VN_PORTs/lport.
760 */
Robert Love1875f272009-11-03 11:47:50 -0800761 if (!lport->lro_enabled || !lport->lro_xid ||
762 (lport->lro_xid >= max_xid)) {
763 lport->lro_xid = 0;
Vasu Devd7179682009-07-29 17:05:21 -0700764 goto skip_oem;
765 }
766
767 /*
768 * Reuse existing offload em instance in case
Vasu Dev1d1b88d2009-07-29 17:05:45 -0700769 * it is already allocated on real eth device
Vasu Devd7179682009-07-29 17:05:21 -0700770 */
Chris Leech25024982009-08-25 13:59:35 -0700771 if (fcoe->netdev->priv_flags & IFF_802_1Q_VLAN)
772 cur_real_dev = vlan_dev_real_dev(fcoe->netdev);
Vasu Dev1d1b88d2009-07-29 17:05:45 -0700773 else
Chris Leech25024982009-08-25 13:59:35 -0700774 cur_real_dev = fcoe->netdev;
Vasu Dev1d1b88d2009-07-29 17:05:45 -0700775
Chris Leech25024982009-08-25 13:59:35 -0700776 list_for_each_entry(oldfcoe, &fcoe_hostlist, list) {
Chris Leech25024982009-08-25 13:59:35 -0700777 if (oldfcoe->netdev->priv_flags & IFF_802_1Q_VLAN)
778 old_real_dev = vlan_dev_real_dev(oldfcoe->netdev);
Vasu Dev1d1b88d2009-07-29 17:05:45 -0700779 else
Chris Leech25024982009-08-25 13:59:35 -0700780 old_real_dev = oldfcoe->netdev;
Vasu Dev1d1b88d2009-07-29 17:05:45 -0700781
782 if (cur_real_dev == old_real_dev) {
Chris Leech991cbb62009-08-25 13:59:51 -0700783 fcoe->oem = oldfcoe->oem;
Vasu Devd7179682009-07-29 17:05:21 -0700784 break;
785 }
786 }
787
Chris Leech991cbb62009-08-25 13:59:51 -0700788 if (fcoe->oem) {
Robert Love1875f272009-11-03 11:47:50 -0800789 if (!fc_exch_mgr_add(lport, fcoe->oem, fcoe_oem_match)) {
Vasu Devd7179682009-07-29 17:05:21 -0700790 printk(KERN_ERR "fcoe_em_config: failed to add "
791 "offload em:%p on interface:%s\n",
Chris Leech991cbb62009-08-25 13:59:51 -0700792 fcoe->oem, fcoe->netdev->name);
Vasu Devd7179682009-07-29 17:05:21 -0700793 return -ENOMEM;
794 }
795 } else {
Robert Love1875f272009-11-03 11:47:50 -0800796 fcoe->oem = fc_exch_mgr_alloc(lport, FC_CLASS_3,
797 FCOE_MIN_XID, lport->lro_xid,
798 fcoe_oem_match);
Chris Leech991cbb62009-08-25 13:59:51 -0700799 if (!fcoe->oem) {
Vasu Devd7179682009-07-29 17:05:21 -0700800 printk(KERN_ERR "fcoe_em_config: failed to allocate "
801 "em for offload exches on interface:%s\n",
Chris Leech25024982009-08-25 13:59:35 -0700802 fcoe->netdev->name);
Vasu Devd7179682009-07-29 17:05:21 -0700803 return -ENOMEM;
804 }
805 }
806
807 /*
808 * Exclude offload EM xid range from next EM xid range.
809 */
Robert Love1875f272009-11-03 11:47:50 -0800810 min_xid += lport->lro_xid + 1;
Vasu Devd7179682009-07-29 17:05:21 -0700811
812skip_oem:
Robert Love1875f272009-11-03 11:47:50 -0800813 if (!fc_exch_mgr_alloc(lport, FC_CLASS_3, min_xid, max_xid, NULL)) {
Vasu Devd7179682009-07-29 17:05:21 -0700814 printk(KERN_ERR "fcoe_em_config: failed to "
Chris Leech25024982009-08-25 13:59:35 -0700815 "allocate em on interface %s\n", fcoe->netdev->name);
Vasu Dev7f349142009-03-27 09:06:31 -0700816 return -ENOMEM;
Vasu Devd7179682009-07-29 17:05:21 -0700817 }
Vasu Dev7f349142009-03-27 09:06:31 -0700818
819 return 0;
820}
821
822/**
Robert Love1875f272009-11-03 11:47:50 -0800823 * fcoe_if_destroy() - Tear down a SW FCoE instance
824 * @lport: The local port to be destroyed
Vasu Dev34ce27b2010-05-07 15:18:46 -0700825 *
826 * Locking: must be called with the RTNL mutex held and RTNL mutex
827 * needed to be dropped by this function since not dropping RTNL
828 * would cause circular locking warning on synchronous fip worker
829 * cancelling thru fcoe_interface_put invoked by this function.
830 *
Vasu Dev7f349142009-03-27 09:06:31 -0700831 */
Chris Leechaf7f85d2009-08-25 13:59:24 -0700832static void fcoe_if_destroy(struct fc_lport *lport)
Vasu Dev7f349142009-03-27 09:06:31 -0700833{
Chris Leech014f5c32009-08-25 13:59:30 -0700834 struct fcoe_port *port = lport_priv(lport);
Bhanu Prakash Gollapudi8597ae82011-01-28 16:05:37 -0800835 struct fcoe_interface *fcoe = port->priv;
Chris Leech25024982009-08-25 13:59:35 -0700836 struct net_device *netdev = fcoe->netdev;
Vasu Dev7f349142009-03-27 09:06:31 -0700837
Robert Loved5488eb2009-06-10 15:30:59 -0700838 FCOE_NETDEV_DBG(netdev, "Destroying interface\n");
Vasu Dev7f349142009-03-27 09:06:31 -0700839
Vasu Dev7f349142009-03-27 09:06:31 -0700840 /* Logout of the fabric */
Chris Leechaf7f85d2009-08-25 13:59:24 -0700841 fc_fabric_logoff(lport);
Vasu Dev7f349142009-03-27 09:06:31 -0700842
Vasu Dev7f349142009-03-27 09:06:31 -0700843 /* Cleanup the fc_lport */
Chris Leechaf7f85d2009-08-25 13:59:24 -0700844 fc_lport_destroy(lport);
Vasu Dev7f349142009-03-27 09:06:31 -0700845
Chris Leech54b649f2009-08-25 14:00:07 -0700846 /* Stop the transmit retry timer */
847 del_timer_sync(&port->timer);
848
849 /* Free existing transmit skbs */
850 fcoe_clean_pending_queue(lport);
851
Chris Leech11b56182009-11-03 11:46:29 -0800852 if (!is_zero_ether_addr(port->data_src_addr))
Jiri Pirkoa748ee22010-04-01 21:22:09 +0000853 dev_uc_del(netdev, port->data_src_addr);
Chris Leech11b56182009-11-03 11:46:29 -0800854 rtnl_unlock();
855
Chris Leech54b649f2009-08-25 14:00:07 -0700856 /* receives may not be stopped until after this */
857 fcoe_interface_put(fcoe);
858
859 /* Free queued packets for the per-CPU receive threads */
860 fcoe_percpu_clean(lport);
861
Vasu Dev7f349142009-03-27 09:06:31 -0700862 /* Detach from the scsi-ml */
Chris Leechaf7f85d2009-08-25 13:59:24 -0700863 fc_remove_host(lport->host);
864 scsi_remove_host(lport->host);
Vasu Dev7f349142009-03-27 09:06:31 -0700865
Yi Zou80e736f2010-11-30 16:18:07 -0800866 /* Destroy lport scsi_priv */
867 fc_fcp_destroy(lport);
868
Vasu Dev7f349142009-03-27 09:06:31 -0700869 /* There are no more rports or I/O, free the EM */
Chris Leechaf7f85d2009-08-25 13:59:24 -0700870 fc_exch_mgr_free(lport);
Vasu Dev7f349142009-03-27 09:06:31 -0700871
Vasu Dev7f349142009-03-27 09:06:31 -0700872 /* Free memory used by statistical counters */
Chris Leechaf7f85d2009-08-25 13:59:24 -0700873 fc_lport_free_stats(lport);
Vasu Dev7f349142009-03-27 09:06:31 -0700874
Chris Leech2e70e242009-08-25 14:00:23 -0700875 /* Release the Scsi_Host */
Chris Leechaf7f85d2009-08-25 13:59:24 -0700876 scsi_host_put(lport->host);
Vasu Dev7f349142009-03-27 09:06:31 -0700877}
878
Robert Love1875f272009-11-03 11:47:50 -0800879/**
880 * fcoe_ddp_setup() - Call a LLD's ddp_setup through the net device
881 * @lport: The local port to setup DDP for
882 * @xid: The exchange ID for this DDP transfer
883 * @sgl: The scatterlist describing this transfer
884 * @sgc: The number of sg items
Vasu Dev7f349142009-03-27 09:06:31 -0700885 *
Robert Love1875f272009-11-03 11:47:50 -0800886 * Returns: 0 if the DDP context was not configured
Vasu Dev7f349142009-03-27 09:06:31 -0700887 */
Robert Love1875f272009-11-03 11:47:50 -0800888static int fcoe_ddp_setup(struct fc_lport *lport, u16 xid,
889 struct scatterlist *sgl, unsigned int sgc)
Vasu Dev7f349142009-03-27 09:06:31 -0700890{
Robert Love1875f272009-11-03 11:47:50 -0800891 struct net_device *netdev = fcoe_netdev(lport);
Vasu Dev7f349142009-03-27 09:06:31 -0700892
Robert Love1875f272009-11-03 11:47:50 -0800893 if (netdev->netdev_ops->ndo_fcoe_ddp_setup)
894 return netdev->netdev_ops->ndo_fcoe_ddp_setup(netdev,
895 xid, sgl,
896 sgc);
Vasu Dev7f349142009-03-27 09:06:31 -0700897
898 return 0;
899}
900
Vasu Dev7f349142009-03-27 09:06:31 -0700901/**
Robert Love1875f272009-11-03 11:47:50 -0800902 * fcoe_ddp_done() - Call a LLD's ddp_done through the net device
903 * @lport: The local port to complete DDP on
904 * @xid: The exchange ID for this DDP transfer
Vasu Dev7f349142009-03-27 09:06:31 -0700905 *
Robert Love1875f272009-11-03 11:47:50 -0800906 * Returns: the length of data that have been completed by DDP
907 */
908static int fcoe_ddp_done(struct fc_lport *lport, u16 xid)
909{
910 struct net_device *netdev = fcoe_netdev(lport);
911
912 if (netdev->netdev_ops->ndo_fcoe_ddp_done)
913 return netdev->netdev_ops->ndo_fcoe_ddp_done(netdev, xid);
914 return 0;
915}
916
917/**
918 * fcoe_if_create() - Create a FCoE instance on an interface
919 * @fcoe: The FCoE interface to create a local port on
920 * @parent: The device pointer to be the parent in sysfs for the SCSI host
921 * @npiv: Indicates if the port is a vport or not
Vasu Dev7f349142009-03-27 09:06:31 -0700922 *
Robert Love1875f272009-11-03 11:47:50 -0800923 * Creates a fc_lport instance and a Scsi_Host instance and configure them.
924 *
925 * Returns: The allocated fc_lport or an error pointer
Vasu Dev7f349142009-03-27 09:06:31 -0700926 */
Chris Leech030f4e02009-08-25 14:00:02 -0700927static struct fc_lport *fcoe_if_create(struct fcoe_interface *fcoe,
Chris Leech9a057532009-11-03 11:46:40 -0800928 struct device *parent, int npiv)
Vasu Dev7f349142009-03-27 09:06:31 -0700929{
Robert Love1875f272009-11-03 11:47:50 -0800930 struct net_device *netdev = fcoe->netdev;
Chris Leechaf7f85d2009-08-25 13:59:24 -0700931 struct fc_lport *lport = NULL;
Chris Leech014f5c32009-08-25 13:59:30 -0700932 struct fcoe_port *port;
Robert Love1875f272009-11-03 11:47:50 -0800933 int rc;
Chris Leech9a057532009-11-03 11:46:40 -0800934 /*
935 * parent is only a vport if npiv is 1,
936 * but we'll only use vport in that case so go ahead and set it
937 */
938 struct fc_vport *vport = dev_to_vport(parent);
Vasu Dev7f349142009-03-27 09:06:31 -0700939
Robert Loved5488eb2009-06-10 15:30:59 -0700940 FCOE_NETDEV_DBG(netdev, "Create Interface\n");
Vasu Dev7f349142009-03-27 09:06:31 -0700941
Chris Leech9a057532009-11-03 11:46:40 -0800942 if (!npiv) {
943 lport = libfc_host_alloc(&fcoe_shost_template,
944 sizeof(struct fcoe_port));
945 } else {
946 lport = libfc_vport_create(vport,
947 sizeof(struct fcoe_port));
948 }
Chris Leech86221962009-11-03 11:46:08 -0800949 if (!lport) {
Chris Leech014f5c32009-08-25 13:59:30 -0700950 FCOE_NETDEV_DBG(netdev, "Could not allocate host structure\n");
951 rc = -ENOMEM;
Chris Leech030f4e02009-08-25 14:00:02 -0700952 goto out;
Chris Leech014f5c32009-08-25 13:59:30 -0700953 }
Chris Leech014f5c32009-08-25 13:59:30 -0700954 port = lport_priv(lport);
Chris Leech2e70e242009-08-25 14:00:23 -0700955 port->lport = lport;
Bhanu Prakash Gollapudi8597ae82011-01-28 16:05:37 -0800956 port->priv = fcoe;
957 port->max_queue_depth = FCOE_MAX_QUEUE_DEPTH;
958 port->min_queue_depth = FCOE_MIN_QUEUE_DEPTH;
Chris Leech2e70e242009-08-25 14:00:23 -0700959 INIT_WORK(&port->destroy_work, fcoe_destroy_work);
Vasu Dev7f349142009-03-27 09:06:31 -0700960
Robert Love1875f272009-11-03 11:47:50 -0800961 /* configure a fc_lport including the exchange manager */
Chris Leechaf7f85d2009-08-25 13:59:24 -0700962 rc = fcoe_lport_config(lport);
Vasu Dev7f349142009-03-27 09:06:31 -0700963 if (rc) {
Robert Loved5488eb2009-06-10 15:30:59 -0700964 FCOE_NETDEV_DBG(netdev, "Could not configure lport for the "
965 "interface\n");
Vasu Dev7f349142009-03-27 09:06:31 -0700966 goto out_host_put;
967 }
968
Chris Leech9a057532009-11-03 11:46:40 -0800969 if (npiv) {
Chris Leech9f8f3aa2010-04-09 14:23:16 -0700970 FCOE_NETDEV_DBG(netdev, "Setting vport names, "
971 "%16.16llx %16.16llx\n",
Robert Love1875f272009-11-03 11:47:50 -0800972 vport->node_name, vport->port_name);
Chris Leech9a057532009-11-03 11:46:40 -0800973 fc_set_wwnn(lport, vport->node_name);
974 fc_set_wwpn(lport, vport->port_name);
975 }
976
Vasu Devab6b85c2009-05-17 12:33:08 +0000977 /* configure lport network properties */
Chris Leechaf7f85d2009-08-25 13:59:24 -0700978 rc = fcoe_netdev_config(lport, netdev);
Vasu Devab6b85c2009-05-17 12:33:08 +0000979 if (rc) {
Robert Loved5488eb2009-06-10 15:30:59 -0700980 FCOE_NETDEV_DBG(netdev, "Could not configure netdev for the "
981 "interface\n");
Chris Leech54b649f2009-08-25 14:00:07 -0700982 goto out_lp_destroy;
Vasu Devab6b85c2009-05-17 12:33:08 +0000983 }
Joe Eykholt97c83892009-03-17 11:42:40 -0700984
Vasu Dev7f349142009-03-27 09:06:31 -0700985 /* configure lport scsi host properties */
Vasu Dev8ba00a42010-04-09 14:22:54 -0700986 rc = fcoe_shost_config(lport, parent);
Vasu Dev7f349142009-03-27 09:06:31 -0700987 if (rc) {
Robert Loved5488eb2009-06-10 15:30:59 -0700988 FCOE_NETDEV_DBG(netdev, "Could not configure shost for the "
989 "interface\n");
Chris Leech54b649f2009-08-25 14:00:07 -0700990 goto out_lp_destroy;
Vasu Dev7f349142009-03-27 09:06:31 -0700991 }
992
Vasu Dev7f349142009-03-27 09:06:31 -0700993 /* Initialize the library */
Joe Eykholte10f8c62010-07-20 15:20:30 -0700994 rc = fcoe_libfc_config(lport, &fcoe->ctlr, &fcoe_libfc_fcn_templ, 1);
Vasu Dev7f349142009-03-27 09:06:31 -0700995 if (rc) {
Robert Loved5488eb2009-06-10 15:30:59 -0700996 FCOE_NETDEV_DBG(netdev, "Could not configure libfc for the "
997 "interface\n");
Vasu Dev7f349142009-03-27 09:06:31 -0700998 goto out_lp_destroy;
999 }
1000
Chris Leech9a057532009-11-03 11:46:40 -08001001 if (!npiv) {
1002 /*
1003 * fcoe_em_alloc() and fcoe_hostlist_add() both
1004 * need to be atomic with respect to other changes to the
1005 * hostlist since fcoe_em_alloc() looks for an existing EM
1006 * instance on host list updated by fcoe_hostlist_add().
1007 *
1008 * This is currently handled through the fcoe_config_mutex
1009 * begin held.
1010 */
Chris Leechc863df32009-08-25 14:00:18 -07001011
Chris Leech9a057532009-11-03 11:46:40 -08001012 /* lport exch manager allocation */
1013 rc = fcoe_em_config(lport);
1014 if (rc) {
1015 FCOE_NETDEV_DBG(netdev, "Could not configure the EM "
Robert Love1875f272009-11-03 11:47:50 -08001016 "for the interface\n");
Chris Leech9a057532009-11-03 11:46:40 -08001017 goto out_lp_destroy;
1018 }
Vasu Dev96316092009-07-29 17:05:00 -07001019 }
1020
Chris Leech030f4e02009-08-25 14:00:02 -07001021 fcoe_interface_get(fcoe);
Chris Leechaf7f85d2009-08-25 13:59:24 -07001022 return lport;
Vasu Dev7f349142009-03-27 09:06:31 -07001023
1024out_lp_destroy:
Chris Leechaf7f85d2009-08-25 13:59:24 -07001025 fc_exch_mgr_free(lport);
Vasu Dev7f349142009-03-27 09:06:31 -07001026out_host_put:
Chris Leechaf7f85d2009-08-25 13:59:24 -07001027 scsi_host_put(lport->host);
1028out:
1029 return ERR_PTR(rc);
Vasu Dev7f349142009-03-27 09:06:31 -07001030}
1031
1032/**
Robert Love1875f272009-11-03 11:47:50 -08001033 * fcoe_if_init() - Initialization routine for fcoe.ko
Vasu Dev7f349142009-03-27 09:06:31 -07001034 *
Robert Love1875f272009-11-03 11:47:50 -08001035 * Attaches the SW FCoE transport to the FC transport
1036 *
1037 * Returns: 0 on success
Vasu Dev7f349142009-03-27 09:06:31 -07001038 */
1039static int __init fcoe_if_init(void)
1040{
1041 /* attach to scsi transport */
Yi Zou8ca86f82011-01-28 16:05:11 -08001042 fcoe_nport_scsi_transport =
1043 fc_attach_transport(&fcoe_nport_fc_functions);
1044 fcoe_vport_scsi_transport =
1045 fc_attach_transport(&fcoe_vport_fc_functions);
Vasu Dev7f349142009-03-27 09:06:31 -07001046
Yi Zou8ca86f82011-01-28 16:05:11 -08001047 if (!fcoe_nport_scsi_transport) {
Robert Loved5488eb2009-06-10 15:30:59 -07001048 printk(KERN_ERR "fcoe: Failed to attach to the FC transport\n");
Vasu Dev7f349142009-03-27 09:06:31 -07001049 return -ENODEV;
1050 }
1051
1052 return 0;
1053}
1054
1055/**
Robert Love1875f272009-11-03 11:47:50 -08001056 * fcoe_if_exit() - Tear down fcoe.ko
Vasu Dev7f349142009-03-27 09:06:31 -07001057 *
Robert Love1875f272009-11-03 11:47:50 -08001058 * Detaches the SW FCoE transport from the FC transport
1059 *
1060 * Returns: 0 on success
Vasu Dev7f349142009-03-27 09:06:31 -07001061 */
1062int __exit fcoe_if_exit(void)
1063{
Yi Zou8ca86f82011-01-28 16:05:11 -08001064 fc_release_transport(fcoe_nport_scsi_transport);
1065 fc_release_transport(fcoe_vport_scsi_transport);
1066 fcoe_nport_scsi_transport = NULL;
1067 fcoe_vport_scsi_transport = NULL;
Vasu Dev7f349142009-03-27 09:06:31 -07001068 return 0;
1069}
1070
Robert Love85b4aa42008-12-09 15:10:24 -08001071/**
Robert Love1875f272009-11-03 11:47:50 -08001072 * fcoe_percpu_thread_create() - Create a receive thread for an online CPU
1073 * @cpu: The CPU index of the CPU to create a receive thread for
Robert Love8976f422009-03-17 11:41:46 -07001074 */
1075static void fcoe_percpu_thread_create(unsigned int cpu)
1076{
1077 struct fcoe_percpu_s *p;
1078 struct task_struct *thread;
1079
1080 p = &per_cpu(fcoe_percpu, cpu);
1081
1082 thread = kthread_create(fcoe_percpu_receive_thread,
1083 (void *)p, "fcoethread/%d", cpu);
1084
Joe Eykholte7a51992009-08-25 14:04:08 -07001085 if (likely(!IS_ERR(thread))) {
Robert Love8976f422009-03-17 11:41:46 -07001086 kthread_bind(thread, cpu);
1087 wake_up_process(thread);
1088
1089 spin_lock_bh(&p->fcoe_rx_list.lock);
1090 p->thread = thread;
1091 spin_unlock_bh(&p->fcoe_rx_list.lock);
1092 }
1093}
1094
1095/**
Robert Love1875f272009-11-03 11:47:50 -08001096 * fcoe_percpu_thread_destroy() - Remove the receive thread of a CPU
1097 * @cpu: The CPU index of the CPU whose receive thread is to be destroyed
Robert Love8976f422009-03-17 11:41:46 -07001098 *
1099 * Destroys a per-CPU Rx thread. Any pending skbs are moved to the
1100 * current CPU's Rx thread. If the thread being destroyed is bound to
1101 * the CPU processing this context the skbs will be freed.
1102 */
1103static void fcoe_percpu_thread_destroy(unsigned int cpu)
1104{
1105 struct fcoe_percpu_s *p;
1106 struct task_struct *thread;
1107 struct page *crc_eof;
1108 struct sk_buff *skb;
1109#ifdef CONFIG_SMP
1110 struct fcoe_percpu_s *p0;
Joe Eykholtf018b732010-03-12 16:08:55 -08001111 unsigned targ_cpu = get_cpu();
Robert Love8976f422009-03-17 11:41:46 -07001112#endif /* CONFIG_SMP */
1113
Robert Loved5488eb2009-06-10 15:30:59 -07001114 FCOE_DBG("Destroying receive thread for CPU %d\n", cpu);
Robert Love8976f422009-03-17 11:41:46 -07001115
1116 /* Prevent any new skbs from being queued for this CPU. */
1117 p = &per_cpu(fcoe_percpu, cpu);
1118 spin_lock_bh(&p->fcoe_rx_list.lock);
1119 thread = p->thread;
1120 p->thread = NULL;
1121 crc_eof = p->crc_eof_page;
1122 p->crc_eof_page = NULL;
1123 p->crc_eof_offset = 0;
1124 spin_unlock_bh(&p->fcoe_rx_list.lock);
1125
1126#ifdef CONFIG_SMP
1127 /*
1128 * Don't bother moving the skb's if this context is running
1129 * on the same CPU that is having its thread destroyed. This
1130 * can easily happen when the module is removed.
1131 */
1132 if (cpu != targ_cpu) {
1133 p0 = &per_cpu(fcoe_percpu, targ_cpu);
1134 spin_lock_bh(&p0->fcoe_rx_list.lock);
1135 if (p0->thread) {
Robert Loved5488eb2009-06-10 15:30:59 -07001136 FCOE_DBG("Moving frames from CPU %d to CPU %d\n",
1137 cpu, targ_cpu);
Robert Love8976f422009-03-17 11:41:46 -07001138
1139 while ((skb = __skb_dequeue(&p->fcoe_rx_list)) != NULL)
1140 __skb_queue_tail(&p0->fcoe_rx_list, skb);
1141 spin_unlock_bh(&p0->fcoe_rx_list.lock);
1142 } else {
1143 /*
1144 * The targeted CPU is not initialized and cannot accept
Robert Love1875f272009-11-03 11:47:50 -08001145 * new skbs. Unlock the targeted CPU and drop the skbs
Robert Love8976f422009-03-17 11:41:46 -07001146 * on the CPU that is going offline.
1147 */
1148 while ((skb = __skb_dequeue(&p->fcoe_rx_list)) != NULL)
1149 kfree_skb(skb);
1150 spin_unlock_bh(&p0->fcoe_rx_list.lock);
1151 }
1152 } else {
1153 /*
1154 * This scenario occurs when the module is being removed
1155 * and all threads are being destroyed. skbs will continue
1156 * to be shifted from the CPU thread that is being removed
1157 * to the CPU thread associated with the CPU that is processing
1158 * the module removal. Once there is only one CPU Rx thread it
1159 * will reach this case and we will drop all skbs and later
1160 * stop the thread.
1161 */
1162 spin_lock_bh(&p->fcoe_rx_list.lock);
1163 while ((skb = __skb_dequeue(&p->fcoe_rx_list)) != NULL)
1164 kfree_skb(skb);
1165 spin_unlock_bh(&p->fcoe_rx_list.lock);
1166 }
Joe Eykholtf018b732010-03-12 16:08:55 -08001167 put_cpu();
Robert Love8976f422009-03-17 11:41:46 -07001168#else
1169 /*
Chris Leechdd3fd722009-04-21 16:27:36 -07001170 * This a non-SMP scenario where the singular Rx thread is
Robert Love8976f422009-03-17 11:41:46 -07001171 * being removed. Free all skbs and stop the thread.
1172 */
1173 spin_lock_bh(&p->fcoe_rx_list.lock);
1174 while ((skb = __skb_dequeue(&p->fcoe_rx_list)) != NULL)
1175 kfree_skb(skb);
1176 spin_unlock_bh(&p->fcoe_rx_list.lock);
1177#endif
1178
1179 if (thread)
1180 kthread_stop(thread);
1181
1182 if (crc_eof)
1183 put_page(crc_eof);
1184}
1185
1186/**
Robert Love1875f272009-11-03 11:47:50 -08001187 * fcoe_cpu_callback() - Handler for CPU hotplug events
1188 * @nfb: The callback data block
1189 * @action: The event triggering the callback
1190 * @hcpu: The index of the CPU that the event is for
Robert Love8976f422009-03-17 11:41:46 -07001191 *
Robert Love1875f272009-11-03 11:47:50 -08001192 * This creates or destroys per-CPU data for fcoe
Robert Love8976f422009-03-17 11:41:46 -07001193 *
1194 * Returns NOTIFY_OK always.
1195 */
1196static int fcoe_cpu_callback(struct notifier_block *nfb,
1197 unsigned long action, void *hcpu)
1198{
1199 unsigned cpu = (unsigned long)hcpu;
1200
1201 switch (action) {
1202 case CPU_ONLINE:
1203 case CPU_ONLINE_FROZEN:
Robert Loved5488eb2009-06-10 15:30:59 -07001204 FCOE_DBG("CPU %x online: Create Rx thread\n", cpu);
Robert Love8976f422009-03-17 11:41:46 -07001205 fcoe_percpu_thread_create(cpu);
1206 break;
1207 case CPU_DEAD:
1208 case CPU_DEAD_FROZEN:
Robert Loved5488eb2009-06-10 15:30:59 -07001209 FCOE_DBG("CPU %x offline: Remove Rx thread\n", cpu);
Robert Love8976f422009-03-17 11:41:46 -07001210 fcoe_percpu_thread_destroy(cpu);
1211 break;
1212 default:
1213 break;
1214 }
1215 return NOTIFY_OK;
1216}
1217
Robert Love8976f422009-03-17 11:41:46 -07001218/**
Robert Love1875f272009-11-03 11:47:50 -08001219 * fcoe_rcv() - Receive packets from a net device
1220 * @skb: The received packet
1221 * @netdev: The net device that the packet was received on
1222 * @ptype: The packet type context
1223 * @olddev: The last device net device
Robert Love85b4aa42008-12-09 15:10:24 -08001224 *
Robert Love1875f272009-11-03 11:47:50 -08001225 * This routine is called by NET_RX_SOFTIRQ. It receives a packet, builds a
1226 * FC frame and passes the frame to libfc.
Robert Love85b4aa42008-12-09 15:10:24 -08001227 *
1228 * Returns: 0 for success
Robert Love34f42a02009-02-27 10:55:45 -08001229 */
Robert Love1875f272009-11-03 11:47:50 -08001230int fcoe_rcv(struct sk_buff *skb, struct net_device *netdev,
Robert Love85b4aa42008-12-09 15:10:24 -08001231 struct packet_type *ptype, struct net_device *olddev)
1232{
Robert Love1875f272009-11-03 11:47:50 -08001233 struct fc_lport *lport;
Robert Love85b4aa42008-12-09 15:10:24 -08001234 struct fcoe_rcv_info *fr;
Chris Leech259ad852009-08-25 13:59:41 -07001235 struct fcoe_interface *fcoe;
Robert Love85b4aa42008-12-09 15:10:24 -08001236 struct fc_frame_header *fh;
Robert Love85b4aa42008-12-09 15:10:24 -08001237 struct fcoe_percpu_s *fps;
Vasu Dev519e5132010-07-20 15:19:32 -07001238 struct ethhdr *eh;
Vasu Devb2f00912009-08-25 13:58:53 -07001239 unsigned int cpu;
Robert Love85b4aa42008-12-09 15:10:24 -08001240
Chris Leech259ad852009-08-25 13:59:41 -07001241 fcoe = container_of(ptype, struct fcoe_interface, fcoe_packet_type);
Robert Love1875f272009-11-03 11:47:50 -08001242 lport = fcoe->ctlr.lp;
1243 if (unlikely(!lport)) {
1244 FCOE_NETDEV_DBG(netdev, "Cannot find hba structure");
Robert Love85b4aa42008-12-09 15:10:24 -08001245 goto err2;
1246 }
Robert Love1875f272009-11-03 11:47:50 -08001247 if (!lport->link_up)
Joe Eykholt97c83892009-03-17 11:42:40 -07001248 goto err2;
Robert Love85b4aa42008-12-09 15:10:24 -08001249
Robert Love1875f272009-11-03 11:47:50 -08001250 FCOE_NETDEV_DBG(netdev, "skb_info: len:%d data_len:%d head:%p "
Robert Loved5488eb2009-06-10 15:30:59 -07001251 "data:%p tail:%p end:%p sum:%d dev:%s",
1252 skb->len, skb->data_len, skb->head, skb->data,
1253 skb_tail_pointer(skb), skb_end_pointer(skb),
1254 skb->csum, skb->dev ? skb->dev->name : "<NULL>");
Robert Love85b4aa42008-12-09 15:10:24 -08001255
Vasu Dev519e5132010-07-20 15:19:32 -07001256 eh = eth_hdr(skb);
Vasu Dev519e5132010-07-20 15:19:32 -07001257
1258 if (is_fip_mode(&fcoe->ctlr) &&
1259 compare_ether_addr(eh->h_source, fcoe->ctlr.dest_addr)) {
1260 FCOE_NETDEV_DBG(netdev, "wrong source mac address:%pM\n",
1261 eh->h_source);
Robert Love85b4aa42008-12-09 15:10:24 -08001262 goto err;
1263 }
1264
1265 /*
1266 * Check for minimum frame length, and make sure required FCoE
1267 * and FC headers are pulled into the linear data area.
1268 */
1269 if (unlikely((skb->len < FCOE_MIN_FRAME) ||
Robert Love1875f272009-11-03 11:47:50 -08001270 !pskb_may_pull(skb, FCOE_HEADER_LEN)))
Robert Love85b4aa42008-12-09 15:10:24 -08001271 goto err;
1272
1273 skb_set_transport_header(skb, sizeof(struct fcoe_hdr));
1274 fh = (struct fc_frame_header *) skb_transport_header(skb);
1275
Robert Love0ee31cb2010-10-08 17:12:46 -07001276 if (ntoh24(&eh->h_dest[3]) != ntoh24(fh->fh_d_id)) {
1277 FCOE_NETDEV_DBG(netdev, "FC frame d_id mismatch with MAC:%pM\n",
1278 eh->h_dest);
1279 goto err;
1280 }
1281
Robert Love85b4aa42008-12-09 15:10:24 -08001282 fr = fcoe_dev_from_skb(skb);
Robert Love1875f272009-11-03 11:47:50 -08001283 fr->fr_dev = lport;
Robert Love85b4aa42008-12-09 15:10:24 -08001284 fr->ptype = ptype;
Robert Love5e5e92d2009-03-17 11:41:35 -07001285
Robert Love85b4aa42008-12-09 15:10:24 -08001286 /*
Vasu Devb2f00912009-08-25 13:58:53 -07001287 * In case the incoming frame's exchange is originated from
1288 * the initiator, then received frame's exchange id is ANDed
1289 * with fc_cpu_mask bits to get the same cpu on which exchange
1290 * was originated, otherwise just use the current cpu.
Robert Love85b4aa42008-12-09 15:10:24 -08001291 */
Vasu Devb2f00912009-08-25 13:58:53 -07001292 if (ntoh24(fh->fh_f_ctl) & FC_FC_EX_CTX)
1293 cpu = ntohs(fh->fh_ox_id) & fc_cpu_mask;
1294 else
1295 cpu = smp_processor_id();
Robert Love5e5e92d2009-03-17 11:41:35 -07001296
Robert Love8976f422009-03-17 11:41:46 -07001297 fps = &per_cpu(fcoe_percpu, cpu);
Robert Love85b4aa42008-12-09 15:10:24 -08001298 spin_lock_bh(&fps->fcoe_rx_list.lock);
Robert Love8976f422009-03-17 11:41:46 -07001299 if (unlikely(!fps->thread)) {
1300 /*
1301 * The targeted CPU is not ready, let's target
1302 * the first CPU now. For non-SMP systems this
1303 * will check the same CPU twice.
1304 */
Robert Love1875f272009-11-03 11:47:50 -08001305 FCOE_NETDEV_DBG(netdev, "CPU is online, but no receive thread "
Robert Loved5488eb2009-06-10 15:30:59 -07001306 "ready for incoming skb- using first online "
1307 "CPU.\n");
Robert Love8976f422009-03-17 11:41:46 -07001308
1309 spin_unlock_bh(&fps->fcoe_rx_list.lock);
Rusty Russell69571772009-12-17 11:43:14 -06001310 cpu = cpumask_first(cpu_online_mask);
Robert Love8976f422009-03-17 11:41:46 -07001311 fps = &per_cpu(fcoe_percpu, cpu);
1312 spin_lock_bh(&fps->fcoe_rx_list.lock);
1313 if (!fps->thread) {
1314 spin_unlock_bh(&fps->fcoe_rx_list.lock);
1315 goto err;
1316 }
1317 }
1318
1319 /*
1320 * We now have a valid CPU that we're targeting for
1321 * this skb. We also have this receive thread locked,
1322 * so we're free to queue skbs into it's queue.
1323 */
Robert Love85b4aa42008-12-09 15:10:24 -08001324
Chris Leech859b7b62009-11-20 14:54:47 -08001325 /* If this is a SCSI-FCP frame, and this is already executing on the
1326 * correct CPU, and the queue for this CPU is empty, then go ahead
1327 * and process the frame directly in the softirq context.
1328 * This lets us process completions without context switching from the
1329 * NET_RX softirq, to our receive processing thread, and then back to
1330 * BLOCK softirq context.
1331 */
1332 if (fh->fh_type == FC_TYPE_FCP &&
1333 cpu == smp_processor_id() &&
1334 skb_queue_empty(&fps->fcoe_rx_list)) {
1335 spin_unlock_bh(&fps->fcoe_rx_list.lock);
1336 fcoe_recv_frame(skb);
1337 } else {
1338 __skb_queue_tail(&fps->fcoe_rx_list, skb);
1339 if (fps->fcoe_rx_list.qlen == 1)
1340 wake_up_process(fps->thread);
1341 spin_unlock_bh(&fps->fcoe_rx_list.lock);
1342 }
Robert Love85b4aa42008-12-09 15:10:24 -08001343
1344 return 0;
1345err:
Joe Eykholtf018b732010-03-12 16:08:55 -08001346 per_cpu_ptr(lport->dev_stats, get_cpu())->ErrorFrames++;
1347 put_cpu();
Robert Love85b4aa42008-12-09 15:10:24 -08001348err2:
1349 kfree_skb(skb);
1350 return -1;
1351}
Robert Love85b4aa42008-12-09 15:10:24 -08001352
1353/**
Bhanu Prakash Gollapudi8597ae82011-01-28 16:05:37 -08001354 * fcoe_alloc_paged_crc_eof() - Allocate a page to be used for the trailer CRC
Robert Love1875f272009-11-03 11:47:50 -08001355 * @skb: The packet to be transmitted
1356 * @tlen: The total length of the trailer
1357 *
Robert Love85b4aa42008-12-09 15:10:24 -08001358 * Returns: 0 for success
Robert Love34f42a02009-02-27 10:55:45 -08001359 */
Bhanu Prakash Gollapudi8597ae82011-01-28 16:05:37 -08001360static int fcoe_alloc_paged_crc_eof(struct sk_buff *skb, int tlen)
Robert Love85b4aa42008-12-09 15:10:24 -08001361{
1362 struct fcoe_percpu_s *fps;
Bhanu Prakash Gollapudi8597ae82011-01-28 16:05:37 -08001363 int rc;
Robert Love85b4aa42008-12-09 15:10:24 -08001364
Robert Love5e5e92d2009-03-17 11:41:35 -07001365 fps = &get_cpu_var(fcoe_percpu);
Bhanu Prakash Gollapudi8597ae82011-01-28 16:05:37 -08001366 rc = fcoe_get_paged_crc_eof(skb, tlen, fps);
Robert Love5e5e92d2009-03-17 11:41:35 -07001367 put_cpu_var(fcoe_percpu);
Robert Love85b4aa42008-12-09 15:10:24 -08001368
Bhanu Prakash Gollapudi8597ae82011-01-28 16:05:37 -08001369 return rc;
Robert Love85b4aa42008-12-09 15:10:24 -08001370}
Robert Love85b4aa42008-12-09 15:10:24 -08001371
1372/**
Robert Love1875f272009-11-03 11:47:50 -08001373 * fcoe_xmit() - Transmit a FCoE frame
1374 * @lport: The local port that the frame is to be transmitted for
1375 * @fp: The frame to be transmitted
Robert Love85b4aa42008-12-09 15:10:24 -08001376 *
Robert Love1875f272009-11-03 11:47:50 -08001377 * Return: 0 for success
Robert Love34f42a02009-02-27 10:55:45 -08001378 */
Robert Love1875f272009-11-03 11:47:50 -08001379int fcoe_xmit(struct fc_lport *lport, struct fc_frame *fp)
Robert Love85b4aa42008-12-09 15:10:24 -08001380{
Vasu Dev4bb6b512009-05-06 10:52:34 -07001381 int wlen;
Robert Love85b4aa42008-12-09 15:10:24 -08001382 u32 crc;
1383 struct ethhdr *eh;
1384 struct fcoe_crc_eof *cp;
1385 struct sk_buff *skb;
1386 struct fcoe_dev_stats *stats;
1387 struct fc_frame_header *fh;
1388 unsigned int hlen; /* header length implies the version */
1389 unsigned int tlen; /* trailer length */
1390 unsigned int elen; /* eth header, may include vlan */
Robert Love1875f272009-11-03 11:47:50 -08001391 struct fcoe_port *port = lport_priv(lport);
Bhanu Prakash Gollapudi8597ae82011-01-28 16:05:37 -08001392 struct fcoe_interface *fcoe = port->priv;
Robert Love85b4aa42008-12-09 15:10:24 -08001393 u8 sof, eof;
1394 struct fcoe_hdr *hp;
1395
1396 WARN_ON((fr_len(fp) % sizeof(u32)) != 0);
1397
Robert Love85b4aa42008-12-09 15:10:24 -08001398 fh = fc_frame_header_get(fp);
Joe Eykholt97c83892009-03-17 11:42:40 -07001399 skb = fp_skb(fp);
1400 wlen = skb->len / FCOE_WORD_TO_BYTE;
1401
Robert Love1875f272009-11-03 11:47:50 -08001402 if (!lport->link_up) {
Dan Carpenter3caf02e2009-04-21 16:27:25 -07001403 kfree_skb(skb);
Joe Eykholt97c83892009-03-17 11:42:40 -07001404 return 0;
Robert Love85b4aa42008-12-09 15:10:24 -08001405 }
1406
Joe Eykholt9860eeb2010-03-12 16:07:52 -08001407 if (unlikely(fh->fh_type == FC_TYPE_ELS) &&
Robert Love1875f272009-11-03 11:47:50 -08001408 fcoe_ctlr_els_send(&fcoe->ctlr, lport, skb))
Joe Eykholt97c83892009-03-17 11:42:40 -07001409 return 0;
1410
Robert Love85b4aa42008-12-09 15:10:24 -08001411 sof = fr_sof(fp);
1412 eof = fr_eof(fp);
1413
Vasu Dev4e57e1c2009-05-06 10:52:46 -07001414 elen = sizeof(struct ethhdr);
Robert Love85b4aa42008-12-09 15:10:24 -08001415 hlen = sizeof(struct fcoe_hdr);
1416 tlen = sizeof(struct fcoe_crc_eof);
1417 wlen = (skb->len - tlen + sizeof(crc)) / FCOE_WORD_TO_BYTE;
1418
1419 /* crc offload */
Robert Love1875f272009-11-03 11:47:50 -08001420 if (likely(lport->crc_offload)) {
Yi Zou39ca9a02009-02-27 14:07:15 -08001421 skb->ip_summed = CHECKSUM_PARTIAL;
Robert Love85b4aa42008-12-09 15:10:24 -08001422 skb->csum_start = skb_headroom(skb);
1423 skb->csum_offset = skb->len;
1424 crc = 0;
1425 } else {
1426 skb->ip_summed = CHECKSUM_NONE;
1427 crc = fcoe_fc_crc(fp);
1428 }
1429
Chris Leech014f5c32009-08-25 13:59:30 -07001430 /* copy port crc and eof to the skb buff */
Robert Love85b4aa42008-12-09 15:10:24 -08001431 if (skb_is_nonlinear(skb)) {
1432 skb_frag_t *frag;
Bhanu Prakash Gollapudi8597ae82011-01-28 16:05:37 -08001433 if (fcoe_alloc_paged_crc_eof(skb, tlen)) {
Roel Kluine9041582009-02-27 10:56:22 -08001434 kfree_skb(skb);
Robert Love85b4aa42008-12-09 15:10:24 -08001435 return -ENOMEM;
1436 }
1437 frag = &skb_shinfo(skb)->frags[skb_shinfo(skb)->nr_frags - 1];
1438 cp = kmap_atomic(frag->page, KM_SKB_DATA_SOFTIRQ)
1439 + frag->page_offset;
1440 } else {
1441 cp = (struct fcoe_crc_eof *)skb_put(skb, tlen);
1442 }
1443
1444 memset(cp, 0, sizeof(*cp));
1445 cp->fcoe_eof = eof;
1446 cp->fcoe_crc32 = cpu_to_le32(~crc);
1447
1448 if (skb_is_nonlinear(skb)) {
1449 kunmap_atomic(cp, KM_SKB_DATA_SOFTIRQ);
1450 cp = NULL;
1451 }
1452
Chris Leech014f5c32009-08-25 13:59:30 -07001453 /* adjust skb network/transport offsets to match mac/fcoe/port */
Robert Love85b4aa42008-12-09 15:10:24 -08001454 skb_push(skb, elen + hlen);
1455 skb_reset_mac_header(skb);
1456 skb_reset_network_header(skb);
1457 skb->mac_len = elen;
Yi Zou211c7382009-02-27 14:06:37 -08001458 skb->protocol = htons(ETH_P_FCOE);
Chris Leech3fe9a0b2009-08-25 13:59:46 -07001459 skb->dev = fcoe->netdev;
Robert Love85b4aa42008-12-09 15:10:24 -08001460
1461 /* fill up mac and fcoe headers */
1462 eh = eth_hdr(skb);
1463 eh->h_proto = htons(ETH_P_FCOE);
Joe Eykholtcd229e42010-07-20 15:20:40 -07001464 memcpy(eh->h_dest, fcoe->ctlr.dest_addr, ETH_ALEN);
Chris Leech3fe9a0b2009-08-25 13:59:46 -07001465 if (fcoe->ctlr.map_dest)
Joe Eykholtcd229e42010-07-20 15:20:40 -07001466 memcpy(eh->h_dest + 3, fh->fh_d_id, 3);
Robert Love85b4aa42008-12-09 15:10:24 -08001467
Chris Leech3fe9a0b2009-08-25 13:59:46 -07001468 if (unlikely(fcoe->ctlr.flogi_oxid != FC_XID_UNKNOWN))
1469 memcpy(eh->h_source, fcoe->ctlr.ctl_src_addr, ETH_ALEN);
Robert Love85b4aa42008-12-09 15:10:24 -08001470 else
Chris Leech11b56182009-11-03 11:46:29 -08001471 memcpy(eh->h_source, port->data_src_addr, ETH_ALEN);
Robert Love85b4aa42008-12-09 15:10:24 -08001472
1473 hp = (struct fcoe_hdr *)(eh + 1);
1474 memset(hp, 0, sizeof(*hp));
1475 if (FC_FCOE_VER)
1476 FC_FCOE_ENCAPS_VER(hp, FC_FCOE_VER);
1477 hp->fcoe_sof = sof;
1478
Yi Zou39ca9a02009-02-27 14:07:15 -08001479 /* fcoe lso, mss is in max_payload which is non-zero for FCP data */
Robert Love1875f272009-11-03 11:47:50 -08001480 if (lport->seq_offload && fr_max_payload(fp)) {
Yi Zou39ca9a02009-02-27 14:07:15 -08001481 skb_shinfo(skb)->gso_type = SKB_GSO_FCOE;
1482 skb_shinfo(skb)->gso_size = fr_max_payload(fp);
1483 } else {
1484 skb_shinfo(skb)->gso_type = 0;
1485 skb_shinfo(skb)->gso_size = 0;
1486 }
Robert Love85b4aa42008-12-09 15:10:24 -08001487 /* update tx stats: regardless if LLD fails */
Joe Eykholtf018b732010-03-12 16:08:55 -08001488 stats = per_cpu_ptr(lport->dev_stats, get_cpu());
Robert Love582b45b2009-03-31 15:51:50 -07001489 stats->TxFrames++;
1490 stats->TxWords += wlen;
Joe Eykholtf018b732010-03-12 16:08:55 -08001491 put_cpu();
Robert Love85b4aa42008-12-09 15:10:24 -08001492
1493 /* send down to lld */
Robert Love1875f272009-11-03 11:47:50 -08001494 fr_dev(fp) = lport;
Chris Leech014f5c32009-08-25 13:59:30 -07001495 if (port->fcoe_pending_queue.qlen)
Robert Love1875f272009-11-03 11:47:50 -08001496 fcoe_check_wait_queue(lport, skb);
Vasu Dev4bb6b512009-05-06 10:52:34 -07001497 else if (fcoe_start_io(skb))
Robert Love1875f272009-11-03 11:47:50 -08001498 fcoe_check_wait_queue(lport, skb);
Robert Love85b4aa42008-12-09 15:10:24 -08001499
1500 return 0;
1501}
Robert Love85b4aa42008-12-09 15:10:24 -08001502
Robert Love34f42a02009-02-27 10:55:45 -08001503/**
Robert Love1875f272009-11-03 11:47:50 -08001504 * fcoe_percpu_flush_done() - Indicate per-CPU queue flush completion
1505 * @skb: The completed skb (argument required by destructor)
Joe Eykholte7a51992009-08-25 14:04:08 -07001506 */
1507static void fcoe_percpu_flush_done(struct sk_buff *skb)
1508{
1509 complete(&fcoe_flush_completion);
1510}
1511
1512/**
Vasu Dev52ee8322011-01-28 16:03:52 -08001513 * fcoe_filter_frames() - filter out bad fcoe frames, i.e. bad CRC
1514 * @lport: The local port the frame was received on
1515 * @fp: The received frame
1516 *
1517 * Return: 0 on passing filtering checks
1518 */
1519static inline int fcoe_filter_frames(struct fc_lport *lport,
1520 struct fc_frame *fp)
1521{
1522 struct fcoe_interface *fcoe;
1523 struct fc_frame_header *fh;
1524 struct sk_buff *skb = (struct sk_buff *)fp;
1525 struct fcoe_dev_stats *stats;
1526
1527 /*
1528 * We only check CRC if no offload is available and if it is
1529 * it's solicited data, in which case, the FCP layer would
1530 * check it during the copy.
1531 */
1532 if (lport->crc_offload && skb->ip_summed == CHECKSUM_UNNECESSARY)
1533 fr_flags(fp) &= ~FCPHF_CRC_UNCHECKED;
1534 else
1535 fr_flags(fp) |= FCPHF_CRC_UNCHECKED;
1536
1537 fh = (struct fc_frame_header *) skb_transport_header(skb);
1538 fh = fc_frame_header_get(fp);
1539 if (fh->fh_r_ctl == FC_RCTL_DD_SOL_DATA && fh->fh_type == FC_TYPE_FCP)
1540 return 0;
1541
Bhanu Prakash Gollapudi8597ae82011-01-28 16:05:37 -08001542 fcoe = ((struct fcoe_port *)lport_priv(lport))->priv;
Vasu Dev52ee8322011-01-28 16:03:52 -08001543 if (is_fip_mode(&fcoe->ctlr) && fc_frame_payload_op(fp) == ELS_LOGO &&
1544 ntoh24(fh->fh_s_id) == FC_FID_FLOGI) {
1545 FCOE_DBG("fcoe: dropping FCoE lport LOGO in fip mode\n");
1546 return -EINVAL;
1547 }
1548
1549 if (!fr_flags(fp) & FCPHF_CRC_UNCHECKED ||
1550 le32_to_cpu(fr_crc(fp)) == ~crc32(~0, skb->data, skb->len)) {
1551 fr_flags(fp) &= ~FCPHF_CRC_UNCHECKED;
1552 return 0;
1553 }
1554
1555 stats = per_cpu_ptr(lport->dev_stats, get_cpu());
1556 stats->InvalidCRCCount++;
1557 if (stats->InvalidCRCCount < 5)
1558 printk(KERN_WARNING "fcoe: dropping frame with CRC error\n");
1559 return -EINVAL;
1560}
1561
1562/**
Chris Leech859b7b62009-11-20 14:54:47 -08001563 * fcoe_recv_frame() - process a single received frame
1564 * @skb: frame to process
1565 */
1566static void fcoe_recv_frame(struct sk_buff *skb)
1567{
1568 u32 fr_len;
1569 struct fc_lport *lport;
1570 struct fcoe_rcv_info *fr;
1571 struct fcoe_dev_stats *stats;
Chris Leech859b7b62009-11-20 14:54:47 -08001572 struct fcoe_crc_eof crc_eof;
1573 struct fc_frame *fp;
Chris Leech859b7b62009-11-20 14:54:47 -08001574 struct fcoe_port *port;
1575 struct fcoe_hdr *hp;
1576
1577 fr = fcoe_dev_from_skb(skb);
1578 lport = fr->fr_dev;
1579 if (unlikely(!lport)) {
1580 if (skb->destructor != fcoe_percpu_flush_done)
1581 FCOE_NETDEV_DBG(skb->dev, "NULL lport in skb");
1582 kfree_skb(skb);
1583 return;
1584 }
1585
1586 FCOE_NETDEV_DBG(skb->dev, "skb_info: len:%d data_len:%d "
1587 "head:%p data:%p tail:%p end:%p sum:%d dev:%s",
1588 skb->len, skb->data_len,
1589 skb->head, skb->data, skb_tail_pointer(skb),
1590 skb_end_pointer(skb), skb->csum,
1591 skb->dev ? skb->dev->name : "<NULL>");
1592
Chris Leech859b7b62009-11-20 14:54:47 -08001593 port = lport_priv(lport);
1594 if (skb_is_nonlinear(skb))
1595 skb_linearize(skb); /* not ideal */
Chris Leech859b7b62009-11-20 14:54:47 -08001596
1597 /*
1598 * Frame length checks and setting up the header pointers
1599 * was done in fcoe_rcv already.
1600 */
1601 hp = (struct fcoe_hdr *) skb_network_header(skb);
Chris Leech859b7b62009-11-20 14:54:47 -08001602
Joe Eykholtf018b732010-03-12 16:08:55 -08001603 stats = per_cpu_ptr(lport->dev_stats, get_cpu());
Chris Leech859b7b62009-11-20 14:54:47 -08001604 if (unlikely(FC_FCOE_DECAPS_VER(hp) != FC_FCOE_VER)) {
1605 if (stats->ErrorFrames < 5)
1606 printk(KERN_WARNING "fcoe: FCoE version "
1607 "mismatch: The frame has "
1608 "version %x, but the "
1609 "initiator supports version "
1610 "%x\n", FC_FCOE_DECAPS_VER(hp),
1611 FC_FCOE_VER);
Joe Eykholtf018b732010-03-12 16:08:55 -08001612 goto drop;
Chris Leech859b7b62009-11-20 14:54:47 -08001613 }
1614
1615 skb_pull(skb, sizeof(struct fcoe_hdr));
1616 fr_len = skb->len - sizeof(struct fcoe_crc_eof);
1617
1618 stats->RxFrames++;
1619 stats->RxWords += fr_len / FCOE_WORD_TO_BYTE;
1620
1621 fp = (struct fc_frame *)skb;
1622 fc_frame_init(fp);
1623 fr_dev(fp) = lport;
1624 fr_sof(fp) = hp->fcoe_sof;
1625
1626 /* Copy out the CRC and EOF trailer for access */
Joe Eykholtf018b732010-03-12 16:08:55 -08001627 if (skb_copy_bits(skb, fr_len, &crc_eof, sizeof(crc_eof)))
1628 goto drop;
Chris Leech859b7b62009-11-20 14:54:47 -08001629 fr_eof(fp) = crc_eof.fcoe_eof;
1630 fr_crc(fp) = crc_eof.fcoe_crc32;
Joe Eykholtf018b732010-03-12 16:08:55 -08001631 if (pskb_trim(skb, fr_len))
1632 goto drop;
Chris Leech859b7b62009-11-20 14:54:47 -08001633
Vasu Dev52ee8322011-01-28 16:03:52 -08001634 if (!fcoe_filter_frames(lport, fp)) {
1635 put_cpu();
1636 fc_exch_recv(lport, fp);
1637 return;
Chris Leech859b7b62009-11-20 14:54:47 -08001638 }
Joe Eykholtf018b732010-03-12 16:08:55 -08001639drop:
1640 stats->ErrorFrames++;
1641 put_cpu();
1642 kfree_skb(skb);
Chris Leech859b7b62009-11-20 14:54:47 -08001643}
1644
1645/**
Robert Love1875f272009-11-03 11:47:50 -08001646 * fcoe_percpu_receive_thread() - The per-CPU packet receive thread
1647 * @arg: The per-CPU context
Robert Love85b4aa42008-12-09 15:10:24 -08001648 *
1649 * Return: 0 for success
Robert Love85b4aa42008-12-09 15:10:24 -08001650 */
1651int fcoe_percpu_receive_thread(void *arg)
1652{
1653 struct fcoe_percpu_s *p = arg;
Robert Love85b4aa42008-12-09 15:10:24 -08001654 struct sk_buff *skb;
Robert Love85b4aa42008-12-09 15:10:24 -08001655
Robert Love4469c192009-02-27 10:56:38 -08001656 set_user_nice(current, -20);
Robert Love85b4aa42008-12-09 15:10:24 -08001657
1658 while (!kthread_should_stop()) {
1659
1660 spin_lock_bh(&p->fcoe_rx_list.lock);
1661 while ((skb = __skb_dequeue(&p->fcoe_rx_list)) == NULL) {
1662 set_current_state(TASK_INTERRUPTIBLE);
1663 spin_unlock_bh(&p->fcoe_rx_list.lock);
1664 schedule();
1665 set_current_state(TASK_RUNNING);
1666 if (kthread_should_stop())
1667 return 0;
1668 spin_lock_bh(&p->fcoe_rx_list.lock);
1669 }
1670 spin_unlock_bh(&p->fcoe_rx_list.lock);
Chris Leech859b7b62009-11-20 14:54:47 -08001671 fcoe_recv_frame(skb);
Robert Love85b4aa42008-12-09 15:10:24 -08001672 }
1673 return 0;
1674}
1675
1676/**
Robert Love1875f272009-11-03 11:47:50 -08001677 * fcoe_dev_setup() - Setup the link change notification interface
Robert Love34f42a02009-02-27 10:55:45 -08001678 */
Randy Dunlapb0d428a2009-04-27 21:49:31 -07001679static void fcoe_dev_setup(void)
Robert Love85b4aa42008-12-09 15:10:24 -08001680{
Robert Love85b4aa42008-12-09 15:10:24 -08001681 register_netdevice_notifier(&fcoe_notifier);
1682}
1683
1684/**
Robert Love1875f272009-11-03 11:47:50 -08001685 * fcoe_dev_cleanup() - Cleanup the link change notification interface
Robert Love34f42a02009-02-27 10:55:45 -08001686 */
Robert Love85b4aa42008-12-09 15:10:24 -08001687static void fcoe_dev_cleanup(void)
1688{
1689 unregister_netdevice_notifier(&fcoe_notifier);
1690}
1691
1692/**
Robert Love1875f272009-11-03 11:47:50 -08001693 * fcoe_device_notification() - Handler for net device events
1694 * @notifier: The context of the notification
1695 * @event: The type of event
1696 * @ptr: The net device that the event was on
Robert Love85b4aa42008-12-09 15:10:24 -08001697 *
Robert Love1875f272009-11-03 11:47:50 -08001698 * This function is called by the Ethernet driver in case of link change event.
Robert Love85b4aa42008-12-09 15:10:24 -08001699 *
1700 * Returns: 0 for success
Robert Love34f42a02009-02-27 10:55:45 -08001701 */
Robert Love85b4aa42008-12-09 15:10:24 -08001702static int fcoe_device_notification(struct notifier_block *notifier,
1703 ulong event, void *ptr)
1704{
Robert Love1875f272009-11-03 11:47:50 -08001705 struct fc_lport *lport = NULL;
Vasu Dev1d1b88d2009-07-29 17:05:45 -07001706 struct net_device *netdev = ptr;
Chris Leech014f5c32009-08-25 13:59:30 -07001707 struct fcoe_interface *fcoe;
Chris Leech2e70e242009-08-25 14:00:23 -07001708 struct fcoe_port *port;
Robert Love85b4aa42008-12-09 15:10:24 -08001709 struct fcoe_dev_stats *stats;
Joe Eykholt97c83892009-03-17 11:42:40 -07001710 u32 link_possible = 1;
Robert Love85b4aa42008-12-09 15:10:24 -08001711 u32 mfs;
1712 int rc = NOTIFY_OK;
1713
Chris Leech014f5c32009-08-25 13:59:30 -07001714 list_for_each_entry(fcoe, &fcoe_hostlist, list) {
Chris Leech25024982009-08-25 13:59:35 -07001715 if (fcoe->netdev == netdev) {
Robert Love1875f272009-11-03 11:47:50 -08001716 lport = fcoe->ctlr.lp;
Robert Love85b4aa42008-12-09 15:10:24 -08001717 break;
1718 }
1719 }
Robert Love1875f272009-11-03 11:47:50 -08001720 if (!lport) {
Robert Love85b4aa42008-12-09 15:10:24 -08001721 rc = NOTIFY_DONE;
1722 goto out;
1723 }
1724
Robert Love85b4aa42008-12-09 15:10:24 -08001725 switch (event) {
1726 case NETDEV_DOWN:
1727 case NETDEV_GOING_DOWN:
Joe Eykholt97c83892009-03-17 11:42:40 -07001728 link_possible = 0;
Robert Love85b4aa42008-12-09 15:10:24 -08001729 break;
1730 case NETDEV_UP:
1731 case NETDEV_CHANGE:
Robert Love85b4aa42008-12-09 15:10:24 -08001732 break;
1733 case NETDEV_CHANGEMTU:
Yi Zou7221d7e2009-10-21 16:27:52 -07001734 if (netdev->features & NETIF_F_FCOE_MTU)
1735 break;
Vasu Dev1d1b88d2009-07-29 17:05:45 -07001736 mfs = netdev->mtu - (sizeof(struct fcoe_hdr) +
1737 sizeof(struct fcoe_crc_eof));
Robert Love85b4aa42008-12-09 15:10:24 -08001738 if (mfs >= FC_MIN_MAX_FRAME)
Robert Love1875f272009-11-03 11:47:50 -08001739 fc_set_mfs(lport, mfs);
Robert Love85b4aa42008-12-09 15:10:24 -08001740 break;
1741 case NETDEV_REGISTER:
1742 break;
Chris Leech2e70e242009-08-25 14:00:23 -07001743 case NETDEV_UNREGISTER:
1744 list_del(&fcoe->list);
1745 port = lport_priv(fcoe->ctlr.lp);
1746 fcoe_interface_cleanup(fcoe);
Tejun Heo2ca32b42011-01-28 16:05:32 -08001747 queue_work(fcoe_wq, &port->destroy_work);
Chris Leech2e70e242009-08-25 14:00:23 -07001748 goto out;
1749 break;
Yi Zou54a5b212010-07-20 15:21:17 -07001750 case NETDEV_FEAT_CHANGE:
1751 fcoe_netdev_features_change(lport, netdev);
1752 break;
Robert Love85b4aa42008-12-09 15:10:24 -08001753 default:
Vasu Dev1d1b88d2009-07-29 17:05:45 -07001754 FCOE_NETDEV_DBG(netdev, "Unknown event %ld "
Robert Loved5488eb2009-06-10 15:30:59 -07001755 "from netdev netlink\n", event);
Robert Love85b4aa42008-12-09 15:10:24 -08001756 }
Robert Love5e4f8fe2010-05-07 15:18:35 -07001757
1758 fcoe_link_speed_update(lport);
1759
Robert Love1875f272009-11-03 11:47:50 -08001760 if (link_possible && !fcoe_link_ok(lport))
Chris Leech3fe9a0b2009-08-25 13:59:46 -07001761 fcoe_ctlr_link_up(&fcoe->ctlr);
1762 else if (fcoe_ctlr_link_down(&fcoe->ctlr)) {
Joe Eykholtf018b732010-03-12 16:08:55 -08001763 stats = per_cpu_ptr(lport->dev_stats, get_cpu());
Joe Eykholt97c83892009-03-17 11:42:40 -07001764 stats->LinkFailureCount++;
Joe Eykholtf018b732010-03-12 16:08:55 -08001765 put_cpu();
Robert Love1875f272009-11-03 11:47:50 -08001766 fcoe_clean_pending_queue(lport);
Robert Love85b4aa42008-12-09 15:10:24 -08001767 }
1768out:
1769 return rc;
1770}
1771
1772/**
Vasu Dev55a66d32009-12-10 09:59:31 -08001773 * fcoe_disable() - Disables a FCoE interface
Yi Zou78a58242011-01-28 16:05:16 -08001774 * @netdev : The net_device object the Ethernet interface to create on
Vasu Dev55a66d32009-12-10 09:59:31 -08001775 *
Yi Zou78a58242011-01-28 16:05:16 -08001776 * Called from fcoe transport.
Vasu Dev55a66d32009-12-10 09:59:31 -08001777 *
1778 * Returns: 0 for success
1779 */
Yi Zou78a58242011-01-28 16:05:16 -08001780static int fcoe_disable(struct net_device *netdev)
Vasu Dev55a66d32009-12-10 09:59:31 -08001781{
1782 struct fcoe_interface *fcoe;
Vasu Dev55a66d32009-12-10 09:59:31 -08001783 int rc = 0;
1784
1785 mutex_lock(&fcoe_config_mutex);
1786#ifdef CONFIG_FCOE_MODULE
1787 /*
1788 * Make sure the module has been initialized, and is not about to be
1789 * removed. Module paramter sysfs files are writable before the
1790 * module_init function is called and after module_exit.
1791 */
1792 if (THIS_MODULE->state != MODULE_STATE_LIVE) {
1793 rc = -ENODEV;
1794 goto out_nodev;
1795 }
1796#endif
1797
Vasu Dev34ce27b2010-05-07 15:18:46 -07001798 if (!rtnl_trylock()) {
Vasu Dev34ce27b2010-05-07 15:18:46 -07001799 mutex_unlock(&fcoe_config_mutex);
Yi Zou78a58242011-01-28 16:05:16 -08001800 return -ERESTARTSYS;
Vasu Dev34ce27b2010-05-07 15:18:46 -07001801 }
1802
Vasu Dev55a66d32009-12-10 09:59:31 -08001803 fcoe = fcoe_hostlist_lookup_port(netdev);
1804 rtnl_unlock();
1805
Chris Leech9ee50e42010-04-09 14:22:23 -07001806 if (fcoe) {
Chris Leech9ee50e42010-04-09 14:22:23 -07001807 fcoe_ctlr_link_down(&fcoe->ctlr);
Vasu Dev9d4cbc02010-07-20 15:19:26 -07001808 fcoe_clean_pending_queue(fcoe->ctlr.lp);
Chris Leech9ee50e42010-04-09 14:22:23 -07001809 } else
Vasu Dev55a66d32009-12-10 09:59:31 -08001810 rc = -ENODEV;
1811
Vasu Dev55a66d32009-12-10 09:59:31 -08001812out_nodev:
1813 mutex_unlock(&fcoe_config_mutex);
1814 return rc;
1815}
1816
1817/**
1818 * fcoe_enable() - Enables a FCoE interface
Yi Zou78a58242011-01-28 16:05:16 -08001819 * @netdev : The net_device object the Ethernet interface to create on
Vasu Dev55a66d32009-12-10 09:59:31 -08001820 *
Yi Zou78a58242011-01-28 16:05:16 -08001821 * Called from fcoe transport.
Vasu Dev55a66d32009-12-10 09:59:31 -08001822 *
1823 * Returns: 0 for success
1824 */
Yi Zou78a58242011-01-28 16:05:16 -08001825static int fcoe_enable(struct net_device *netdev)
Vasu Dev55a66d32009-12-10 09:59:31 -08001826{
1827 struct fcoe_interface *fcoe;
Vasu Dev55a66d32009-12-10 09:59:31 -08001828 int rc = 0;
1829
1830 mutex_lock(&fcoe_config_mutex);
1831#ifdef CONFIG_FCOE_MODULE
1832 /*
1833 * Make sure the module has been initialized, and is not about to be
1834 * removed. Module paramter sysfs files are writable before the
1835 * module_init function is called and after module_exit.
1836 */
1837 if (THIS_MODULE->state != MODULE_STATE_LIVE) {
1838 rc = -ENODEV;
1839 goto out_nodev;
1840 }
1841#endif
Vasu Dev34ce27b2010-05-07 15:18:46 -07001842 if (!rtnl_trylock()) {
Vasu Dev34ce27b2010-05-07 15:18:46 -07001843 mutex_unlock(&fcoe_config_mutex);
Yi Zou78a58242011-01-28 16:05:16 -08001844 return -ERESTARTSYS;
Vasu Dev34ce27b2010-05-07 15:18:46 -07001845 }
1846
Vasu Dev55a66d32009-12-10 09:59:31 -08001847 fcoe = fcoe_hostlist_lookup_port(netdev);
1848 rtnl_unlock();
1849
Vasu Dev9d4cbc02010-07-20 15:19:26 -07001850 if (!fcoe)
Vasu Dev55a66d32009-12-10 09:59:31 -08001851 rc = -ENODEV;
Vasu Dev9d4cbc02010-07-20 15:19:26 -07001852 else if (!fcoe_link_ok(fcoe->ctlr.lp))
1853 fcoe_ctlr_link_up(&fcoe->ctlr);
Vasu Dev55a66d32009-12-10 09:59:31 -08001854
Vasu Dev55a66d32009-12-10 09:59:31 -08001855out_nodev:
1856 mutex_unlock(&fcoe_config_mutex);
1857 return rc;
1858}
1859
1860/**
Robert Love1875f272009-11-03 11:47:50 -08001861 * fcoe_destroy() - Destroy a FCoE interface
Yi Zou78a58242011-01-28 16:05:16 -08001862 * @netdev : The net_device object the Ethernet interface to create on
Robert Love1875f272009-11-03 11:47:50 -08001863 *
Yi Zou78a58242011-01-28 16:05:16 -08001864 * Called from fcoe transport
Robert Love85b4aa42008-12-09 15:10:24 -08001865 *
1866 * Returns: 0 for success
Robert Love34f42a02009-02-27 10:55:45 -08001867 */
Yi Zou78a58242011-01-28 16:05:16 -08001868static int fcoe_destroy(struct net_device *netdev)
Robert Love85b4aa42008-12-09 15:10:24 -08001869{
Chris Leech030f4e02009-08-25 14:00:02 -07001870 struct fcoe_interface *fcoe;
Mike Christie8eca355f2009-10-21 16:27:44 -07001871 int rc = 0;
Robert Love85b4aa42008-12-09 15:10:24 -08001872
Chris Leechdfc1d0f2009-08-25 14:00:13 -07001873 mutex_lock(&fcoe_config_mutex);
1874#ifdef CONFIG_FCOE_MODULE
1875 /*
1876 * Make sure the module has been initialized, and is not about to be
1877 * removed. Module paramter sysfs files are writable before the
1878 * module_init function is called and after module_exit.
1879 */
1880 if (THIS_MODULE->state != MODULE_STATE_LIVE) {
1881 rc = -ENODEV;
1882 goto out_nodev;
1883 }
1884#endif
Vasu Dev34ce27b2010-05-07 15:18:46 -07001885 if (!rtnl_trylock()) {
Vasu Dev34ce27b2010-05-07 15:18:46 -07001886 mutex_unlock(&fcoe_config_mutex);
Yi Zou78a58242011-01-28 16:05:16 -08001887 return -ERESTARTSYS;
Vasu Dev34ce27b2010-05-07 15:18:46 -07001888 }
1889
Chris Leech2e70e242009-08-25 14:00:23 -07001890 fcoe = fcoe_hostlist_lookup_port(netdev);
1891 if (!fcoe) {
Chris Leech090eb6c2009-08-25 14:00:28 -07001892 rtnl_unlock();
Robert Love85b4aa42008-12-09 15:10:24 -08001893 rc = -ENODEV;
Yi Zou78a58242011-01-28 16:05:16 -08001894 goto out_nodev;
Robert Love85b4aa42008-12-09 15:10:24 -08001895 }
Chris Leech2e70e242009-08-25 14:00:23 -07001896 fcoe_interface_cleanup(fcoe);
Yi Zou54a5b212010-07-20 15:21:17 -07001897 list_del(&fcoe->list);
Vasu Dev34ce27b2010-05-07 15:18:46 -07001898 /* RTNL mutex is dropped by fcoe_if_destroy */
Chris Leech2e70e242009-08-25 14:00:23 -07001899 fcoe_if_destroy(fcoe->ctlr.lp);
Robert Love85b4aa42008-12-09 15:10:24 -08001900out_nodev:
Chris Leechdfc1d0f2009-08-25 14:00:13 -07001901 mutex_unlock(&fcoe_config_mutex);
Robert Love85b4aa42008-12-09 15:10:24 -08001902 return rc;
1903}
1904
Robert Love1875f272009-11-03 11:47:50 -08001905/**
1906 * fcoe_destroy_work() - Destroy a FCoE port in a deferred work context
1907 * @work: Handle to the FCoE port to be destroyed
1908 */
Chris Leech2e70e242009-08-25 14:00:23 -07001909static void fcoe_destroy_work(struct work_struct *work)
1910{
1911 struct fcoe_port *port;
1912
1913 port = container_of(work, struct fcoe_port, destroy_work);
1914 mutex_lock(&fcoe_config_mutex);
Vasu Dev34ce27b2010-05-07 15:18:46 -07001915 rtnl_lock();
1916 /* RTNL mutex is dropped by fcoe_if_destroy */
Chris Leech2e70e242009-08-25 14:00:23 -07001917 fcoe_if_destroy(port->lport);
1918 mutex_unlock(&fcoe_config_mutex);
1919}
1920
Robert Love85b4aa42008-12-09 15:10:24 -08001921/**
Yi Zou78a58242011-01-28 16:05:16 -08001922 * fcoe_match() - Check if the FCoE is supported on the given netdevice
1923 * @netdev : The net_device object the Ethernet interface to create on
Robert Love1875f272009-11-03 11:47:50 -08001924 *
Yi Zou78a58242011-01-28 16:05:16 -08001925 * Called from fcoe transport.
1926 *
1927 * Returns: always returns true as this is the default FCoE transport,
1928 * i.e., support all netdevs.
1929 */
1930static bool fcoe_match(struct net_device *netdev)
1931{
1932 return true;
1933}
1934
1935/**
1936 * fcoe_create() - Create a fcoe interface
1937 * @netdev : The net_device object the Ethernet interface to create on
1938 * @fip_mode: The FIP mode for this creation
1939 *
1940 * Called from fcoe transport
Robert Love85b4aa42008-12-09 15:10:24 -08001941 *
1942 * Returns: 0 for success
Robert Love34f42a02009-02-27 10:55:45 -08001943 */
Yi Zou78a58242011-01-28 16:05:16 -08001944static int fcoe_create(struct net_device *netdev, enum fip_state fip_mode)
Robert Love85b4aa42008-12-09 15:10:24 -08001945{
1946 int rc;
Chris Leech030f4e02009-08-25 14:00:02 -07001947 struct fcoe_interface *fcoe;
Chris Leechaf7f85d2009-08-25 13:59:24 -07001948 struct fc_lport *lport;
Robert Love85b4aa42008-12-09 15:10:24 -08001949
Chris Leechdfc1d0f2009-08-25 14:00:13 -07001950 mutex_lock(&fcoe_config_mutex);
Vasu Dev34ce27b2010-05-07 15:18:46 -07001951
1952 if (!rtnl_trylock()) {
1953 mutex_unlock(&fcoe_config_mutex);
Yi Zou78a58242011-01-28 16:05:16 -08001954 return -ERESTARTSYS;
Vasu Dev34ce27b2010-05-07 15:18:46 -07001955 }
1956
Chris Leechdfc1d0f2009-08-25 14:00:13 -07001957#ifdef CONFIG_FCOE_MODULE
1958 /*
1959 * Make sure the module has been initialized, and is not about to be
1960 * removed. Module paramter sysfs files are writable before the
1961 * module_init function is called and after module_exit.
1962 */
1963 if (THIS_MODULE->state != MODULE_STATE_LIVE) {
1964 rc = -ENODEV;
Robert Love7287fb92011-01-28 16:03:47 -08001965 goto out_nodev;
Chris Leechdfc1d0f2009-08-25 14:00:13 -07001966 }
1967#endif
1968
Robert Love85b4aa42008-12-09 15:10:24 -08001969 /* look for existing lport */
1970 if (fcoe_hostlist_lookup(netdev)) {
1971 rc = -EEXIST;
Yi Zou78a58242011-01-28 16:05:16 -08001972 goto out_nodev;
Robert Love85b4aa42008-12-09 15:10:24 -08001973 }
Robert Love85b4aa42008-12-09 15:10:24 -08001974
Joe Eykholt1dd454d2010-07-20 15:20:46 -07001975 fcoe = fcoe_interface_create(netdev, fip_mode);
Robert Love7287fb92011-01-28 16:03:47 -08001976 if (IS_ERR(fcoe)) {
1977 rc = PTR_ERR(fcoe);
Yi Zou78a58242011-01-28 16:05:16 -08001978 goto out_nodev;
Chris Leech030f4e02009-08-25 14:00:02 -07001979 }
1980
Chris Leech9a057532009-11-03 11:46:40 -08001981 lport = fcoe_if_create(fcoe, &netdev->dev, 0);
Chris Leechaf7f85d2009-08-25 13:59:24 -07001982 if (IS_ERR(lport)) {
Robert Loved5488eb2009-06-10 15:30:59 -07001983 printk(KERN_ERR "fcoe: Failed to create interface (%s)\n",
Robert Love85b4aa42008-12-09 15:10:24 -08001984 netdev->name);
Robert Love85b4aa42008-12-09 15:10:24 -08001985 rc = -EIO;
Chris Leech2e70e242009-08-25 14:00:23 -07001986 fcoe_interface_cleanup(fcoe);
Chris Leech030f4e02009-08-25 14:00:02 -07001987 goto out_free;
Robert Love85b4aa42008-12-09 15:10:24 -08001988 }
Chris Leech030f4e02009-08-25 14:00:02 -07001989
Chris Leech54b649f2009-08-25 14:00:07 -07001990 /* Make this the "master" N_Port */
1991 fcoe->ctlr.lp = lport;
Chris Leech030f4e02009-08-25 14:00:02 -07001992
Chris Leechc863df32009-08-25 14:00:18 -07001993 /* add to lports list */
1994 fcoe_hostlist_add(lport);
1995
Chris Leech54b649f2009-08-25 14:00:07 -07001996 /* start FIP Discovery and FLOGI */
1997 lport->boot_time = jiffies;
1998 fc_fabric_login(lport);
1999 if (!fcoe_link_ok(lport))
2000 fcoe_ctlr_link_up(&fcoe->ctlr);
2001
Chris Leech54b649f2009-08-25 14:00:07 -07002002 /*
2003 * Release from init in fcoe_interface_create(), on success lport
2004 * should be holding a reference taken in fcoe_if_create().
2005 */
Chris Leech030f4e02009-08-25 14:00:02 -07002006 fcoe_interface_put(fcoe);
Rob Love6409ea652010-01-21 10:16:05 -08002007 rtnl_unlock();
2008 mutex_unlock(&fcoe_config_mutex);
2009
2010 return 0;
2011out_free:
2012 fcoe_interface_put(fcoe);
Robert Love85b4aa42008-12-09 15:10:24 -08002013out_nodev:
Vasu Dev34ce27b2010-05-07 15:18:46 -07002014 rtnl_unlock();
Chris Leechdfc1d0f2009-08-25 14:00:13 -07002015 mutex_unlock(&fcoe_config_mutex);
Robert Love85b4aa42008-12-09 15:10:24 -08002016 return rc;
2017}
2018
Robert Love34f42a02009-02-27 10:55:45 -08002019/**
Robert Love5e4f8fe2010-05-07 15:18:35 -07002020 * fcoe_link_speed_update() - Update the supported and actual link speeds
2021 * @lport: The local port to update speeds for
Robert Love85b4aa42008-12-09 15:10:24 -08002022 *
Robert Love5e4f8fe2010-05-07 15:18:35 -07002023 * Returns: 0 if the ethtool query was successful
2024 * -1 if the ethtool query failed
Robert Love85b4aa42008-12-09 15:10:24 -08002025 */
Robert Love5e4f8fe2010-05-07 15:18:35 -07002026int fcoe_link_speed_update(struct fc_lport *lport)
Robert Love85b4aa42008-12-09 15:10:24 -08002027{
Bhanu Prakash Gollapudi8597ae82011-01-28 16:05:37 -08002028 struct net_device *netdev = fcoe_netdev(lport);
Robert Love85b4aa42008-12-09 15:10:24 -08002029 struct ethtool_cmd ecmd = { ETHTOOL_GSET };
Robert Love85b4aa42008-12-09 15:10:24 -08002030
Robert Love5e4f8fe2010-05-07 15:18:35 -07002031 if (!dev_ethtool_get_settings(netdev, &ecmd)) {
Robert Love1875f272009-11-03 11:47:50 -08002032 lport->link_supported_speeds &=
Yi Zou2f718d62009-07-29 17:04:01 -07002033 ~(FC_PORTSPEED_1GBIT | FC_PORTSPEED_10GBIT);
2034 if (ecmd.supported & (SUPPORTED_1000baseT_Half |
2035 SUPPORTED_1000baseT_Full))
Robert Love1875f272009-11-03 11:47:50 -08002036 lport->link_supported_speeds |= FC_PORTSPEED_1GBIT;
Yi Zou2f718d62009-07-29 17:04:01 -07002037 if (ecmd.supported & SUPPORTED_10000baseT_Full)
Robert Love1875f272009-11-03 11:47:50 -08002038 lport->link_supported_speeds |=
Yi Zou2f718d62009-07-29 17:04:01 -07002039 FC_PORTSPEED_10GBIT;
2040 if (ecmd.speed == SPEED_1000)
Robert Love1875f272009-11-03 11:47:50 -08002041 lport->link_speed = FC_PORTSPEED_1GBIT;
Yi Zou2f718d62009-07-29 17:04:01 -07002042 if (ecmd.speed == SPEED_10000)
Robert Love1875f272009-11-03 11:47:50 -08002043 lport->link_speed = FC_PORTSPEED_10GBIT;
Robert Love85b4aa42008-12-09 15:10:24 -08002044
Yi Zou2f718d62009-07-29 17:04:01 -07002045 return 0;
2046 }
2047 return -1;
Robert Love85b4aa42008-12-09 15:10:24 -08002048}
Robert Love85b4aa42008-12-09 15:10:24 -08002049
Robert Love34f42a02009-02-27 10:55:45 -08002050/**
Robert Love5e4f8fe2010-05-07 15:18:35 -07002051 * fcoe_link_ok() - Check if the link is OK for a local port
2052 * @lport: The local port to check link on
2053 *
2054 * Returns: 0 if link is UP and OK, -1 if not
2055 *
2056 */
2057int fcoe_link_ok(struct fc_lport *lport)
2058{
Bhanu Prakash Gollapudi8597ae82011-01-28 16:05:37 -08002059 struct net_device *netdev = fcoe_netdev(lport);
Robert Love5e4f8fe2010-05-07 15:18:35 -07002060
2061 if (netif_oper_up(netdev))
2062 return 0;
2063 return -1;
2064}
2065
2066/**
Robert Love1875f272009-11-03 11:47:50 -08002067 * fcoe_percpu_clean() - Clear all pending skbs for an local port
2068 * @lport: The local port whose skbs are to be cleared
Joe Eykholte7a51992009-08-25 14:04:08 -07002069 *
2070 * Must be called with fcoe_create_mutex held to single-thread completion.
2071 *
2072 * This flushes the pending skbs by adding a new skb to each queue and
2073 * waiting until they are all freed. This assures us that not only are
2074 * there no packets that will be handled by the lport, but also that any
2075 * threads already handling packet have returned.
Robert Love85b4aa42008-12-09 15:10:24 -08002076 */
Robert Love1875f272009-11-03 11:47:50 -08002077void fcoe_percpu_clean(struct fc_lport *lport)
Robert Love85b4aa42008-12-09 15:10:24 -08002078{
Robert Love85b4aa42008-12-09 15:10:24 -08002079 struct fcoe_percpu_s *pp;
2080 struct fcoe_rcv_info *fr;
2081 struct sk_buff_head *list;
2082 struct sk_buff *skb, *next;
2083 struct sk_buff *head;
Robert Love5e5e92d2009-03-17 11:41:35 -07002084 unsigned int cpu;
Robert Love85b4aa42008-12-09 15:10:24 -08002085
Robert Love5e5e92d2009-03-17 11:41:35 -07002086 for_each_possible_cpu(cpu) {
2087 pp = &per_cpu(fcoe_percpu, cpu);
2088 spin_lock_bh(&pp->fcoe_rx_list.lock);
2089 list = &pp->fcoe_rx_list;
2090 head = list->next;
2091 for (skb = head; skb != (struct sk_buff *)list;
2092 skb = next) {
2093 next = skb->next;
2094 fr = fcoe_dev_from_skb(skb);
Robert Love1875f272009-11-03 11:47:50 -08002095 if (fr->fr_dev == lport) {
Robert Love5e5e92d2009-03-17 11:41:35 -07002096 __skb_unlink(skb, list);
2097 kfree_skb(skb);
Robert Love85b4aa42008-12-09 15:10:24 -08002098 }
Robert Love85b4aa42008-12-09 15:10:24 -08002099 }
Joe Eykholte7a51992009-08-25 14:04:08 -07002100
2101 if (!pp->thread || !cpu_online(cpu)) {
2102 spin_unlock_bh(&pp->fcoe_rx_list.lock);
2103 continue;
2104 }
2105
2106 skb = dev_alloc_skb(0);
2107 if (!skb) {
2108 spin_unlock_bh(&pp->fcoe_rx_list.lock);
2109 continue;
2110 }
2111 skb->destructor = fcoe_percpu_flush_done;
2112
2113 __skb_queue_tail(&pp->fcoe_rx_list, skb);
2114 if (pp->fcoe_rx_list.qlen == 1)
2115 wake_up_process(pp->thread);
Robert Love5e5e92d2009-03-17 11:41:35 -07002116 spin_unlock_bh(&pp->fcoe_rx_list.lock);
Joe Eykholte7a51992009-08-25 14:04:08 -07002117
2118 wait_for_completion(&fcoe_flush_completion);
Robert Love85b4aa42008-12-09 15:10:24 -08002119 }
2120}
Robert Love85b4aa42008-12-09 15:10:24 -08002121
2122/**
Robert Love1875f272009-11-03 11:47:50 -08002123 * fcoe_reset() - Reset a local port
2124 * @shost: The SCSI host associated with the local port to be reset
Robert Love85b4aa42008-12-09 15:10:24 -08002125 *
Robert Love1875f272009-11-03 11:47:50 -08002126 * Returns: Always 0 (return value required by FC transport template)
Robert Love85b4aa42008-12-09 15:10:24 -08002127 */
2128int fcoe_reset(struct Scsi_Host *shost)
2129{
2130 struct fc_lport *lport = shost_priv(shost);
2131 fc_lport_reset(lport);
2132 return 0;
2133}
Robert Love85b4aa42008-12-09 15:10:24 -08002134
Robert Love34f42a02009-02-27 10:55:45 -08002135/**
Robert Love1875f272009-11-03 11:47:50 -08002136 * fcoe_hostlist_lookup_port() - Find the FCoE interface associated with a net device
2137 * @netdev: The net device used as a key
Robert Love85b4aa42008-12-09 15:10:24 -08002138 *
Robert Love1875f272009-11-03 11:47:50 -08002139 * Locking: Must be called with the RNL mutex held.
2140 *
2141 * Returns: NULL or the FCoE interface
Robert Love85b4aa42008-12-09 15:10:24 -08002142 */
Chris Leech014f5c32009-08-25 13:59:30 -07002143static struct fcoe_interface *
Robert Love1875f272009-11-03 11:47:50 -08002144fcoe_hostlist_lookup_port(const struct net_device *netdev)
Robert Love85b4aa42008-12-09 15:10:24 -08002145{
Chris Leech014f5c32009-08-25 13:59:30 -07002146 struct fcoe_interface *fcoe;
Robert Love85b4aa42008-12-09 15:10:24 -08002147
Chris Leech014f5c32009-08-25 13:59:30 -07002148 list_for_each_entry(fcoe, &fcoe_hostlist, list) {
Robert Love1875f272009-11-03 11:47:50 -08002149 if (fcoe->netdev == netdev)
Chris Leech014f5c32009-08-25 13:59:30 -07002150 return fcoe;
Robert Love85b4aa42008-12-09 15:10:24 -08002151 }
Robert Love85b4aa42008-12-09 15:10:24 -08002152 return NULL;
2153}
2154
Robert Love34f42a02009-02-27 10:55:45 -08002155/**
Robert Love1875f272009-11-03 11:47:50 -08002156 * fcoe_hostlist_lookup() - Find the local port associated with a
2157 * given net device
2158 * @netdev: The netdevice used as a key
Robert Love85b4aa42008-12-09 15:10:24 -08002159 *
Robert Love1875f272009-11-03 11:47:50 -08002160 * Locking: Must be called with the RTNL mutex held
2161 *
2162 * Returns: NULL or the local port
Robert Love85b4aa42008-12-09 15:10:24 -08002163 */
Chris Leech090eb6c2009-08-25 14:00:28 -07002164static struct fc_lport *fcoe_hostlist_lookup(const struct net_device *netdev)
Robert Love85b4aa42008-12-09 15:10:24 -08002165{
Chris Leech014f5c32009-08-25 13:59:30 -07002166 struct fcoe_interface *fcoe;
Robert Love85b4aa42008-12-09 15:10:24 -08002167
Chris Leech014f5c32009-08-25 13:59:30 -07002168 fcoe = fcoe_hostlist_lookup_port(netdev);
Chris Leech3fe9a0b2009-08-25 13:59:46 -07002169 return (fcoe) ? fcoe->ctlr.lp : NULL;
Robert Love85b4aa42008-12-09 15:10:24 -08002170}
Robert Love85b4aa42008-12-09 15:10:24 -08002171
Robert Love34f42a02009-02-27 10:55:45 -08002172/**
Robert Love1875f272009-11-03 11:47:50 -08002173 * fcoe_hostlist_add() - Add the FCoE interface identified by a local
2174 * port to the hostlist
2175 * @lport: The local port that identifies the FCoE interface to be added
2176 *
2177 * Locking: must be called with the RTNL mutex held
Robert Love85b4aa42008-12-09 15:10:24 -08002178 *
2179 * Returns: 0 for success
2180 */
Chris Leech090eb6c2009-08-25 14:00:28 -07002181static int fcoe_hostlist_add(const struct fc_lport *lport)
Robert Love85b4aa42008-12-09 15:10:24 -08002182{
Chris Leech014f5c32009-08-25 13:59:30 -07002183 struct fcoe_interface *fcoe;
2184 struct fcoe_port *port;
Robert Love85b4aa42008-12-09 15:10:24 -08002185
Chris Leech014f5c32009-08-25 13:59:30 -07002186 fcoe = fcoe_hostlist_lookup_port(fcoe_netdev(lport));
2187 if (!fcoe) {
2188 port = lport_priv(lport);
Bhanu Prakash Gollapudi8597ae82011-01-28 16:05:37 -08002189 fcoe = port->priv;
Chris Leech014f5c32009-08-25 13:59:30 -07002190 list_add_tail(&fcoe->list, &fcoe_hostlist);
Robert Love85b4aa42008-12-09 15:10:24 -08002191 }
2192 return 0;
2193}
Robert Love85b4aa42008-12-09 15:10:24 -08002194
Yi Zou78a58242011-01-28 16:05:16 -08002195
2196static struct fcoe_transport fcoe_sw_transport = {
2197 .name = {FCOE_TRANSPORT_DEFAULT},
2198 .attached = false,
2199 .list = LIST_HEAD_INIT(fcoe_sw_transport.list),
2200 .match = fcoe_match,
2201 .create = fcoe_create,
2202 .destroy = fcoe_destroy,
2203 .enable = fcoe_enable,
2204 .disable = fcoe_disable,
2205};
2206
Robert Love34f42a02009-02-27 10:55:45 -08002207/**
Robert Love1875f272009-11-03 11:47:50 -08002208 * fcoe_init() - Initialize fcoe.ko
Robert Love85b4aa42008-12-09 15:10:24 -08002209 *
Robert Love1875f272009-11-03 11:47:50 -08002210 * Returns: 0 on success, or a negative value on failure
Robert Love34f42a02009-02-27 10:55:45 -08002211 */
Robert Love85b4aa42008-12-09 15:10:24 -08002212static int __init fcoe_init(void)
2213{
Robert Love1875f272009-11-03 11:47:50 -08002214 struct fcoe_percpu_s *p;
Robert Love38eccab2009-03-17 11:41:30 -07002215 unsigned int cpu;
Robert Love8976f422009-03-17 11:41:46 -07002216 int rc = 0;
Robert Love85b4aa42008-12-09 15:10:24 -08002217
Tejun Heo2ca32b42011-01-28 16:05:32 -08002218 fcoe_wq = alloc_workqueue("fcoe", 0, 0);
2219 if (!fcoe_wq)
2220 return -ENOMEM;
2221
Yi Zou78a58242011-01-28 16:05:16 -08002222 /* register as a fcoe transport */
2223 rc = fcoe_transport_attach(&fcoe_sw_transport);
2224 if (rc) {
2225 printk(KERN_ERR "failed to register an fcoe transport, check "
2226 "if libfcoe is loaded\n");
2227 return rc;
2228 }
2229
Chris Leechdfc1d0f2009-08-25 14:00:13 -07002230 mutex_lock(&fcoe_config_mutex);
2231
Robert Love38eccab2009-03-17 11:41:30 -07002232 for_each_possible_cpu(cpu) {
Robert Love5e5e92d2009-03-17 11:41:35 -07002233 p = &per_cpu(fcoe_percpu, cpu);
Robert Love38eccab2009-03-17 11:41:30 -07002234 skb_queue_head_init(&p->fcoe_rx_list);
2235 }
2236
Robert Love8976f422009-03-17 11:41:46 -07002237 for_each_online_cpu(cpu)
2238 fcoe_percpu_thread_create(cpu);
Robert Love85b4aa42008-12-09 15:10:24 -08002239
Robert Love8976f422009-03-17 11:41:46 -07002240 /* Initialize per CPU interrupt thread */
2241 rc = register_hotcpu_notifier(&fcoe_cpu_notifier);
2242 if (rc)
2243 goto out_free;
Robert Love85b4aa42008-12-09 15:10:24 -08002244
Robert Love8976f422009-03-17 11:41:46 -07002245 /* Setup link change notification */
Robert Love85b4aa42008-12-09 15:10:24 -08002246 fcoe_dev_setup();
2247
Chris Leech5892c322009-08-25 13:59:14 -07002248 rc = fcoe_if_init();
2249 if (rc)
2250 goto out_free;
Robert Love85b4aa42008-12-09 15:10:24 -08002251
Chris Leechdfc1d0f2009-08-25 14:00:13 -07002252 mutex_unlock(&fcoe_config_mutex);
Robert Love85b4aa42008-12-09 15:10:24 -08002253 return 0;
Robert Love8976f422009-03-17 11:41:46 -07002254
2255out_free:
2256 for_each_online_cpu(cpu) {
2257 fcoe_percpu_thread_destroy(cpu);
2258 }
Chris Leechdfc1d0f2009-08-25 14:00:13 -07002259 mutex_unlock(&fcoe_config_mutex);
Tejun Heo2ca32b42011-01-28 16:05:32 -08002260 destroy_workqueue(fcoe_wq);
Robert Love8976f422009-03-17 11:41:46 -07002261 return rc;
Robert Love85b4aa42008-12-09 15:10:24 -08002262}
2263module_init(fcoe_init);
2264
2265/**
Robert Love1875f272009-11-03 11:47:50 -08002266 * fcoe_exit() - Clean up fcoe.ko
Robert Love85b4aa42008-12-09 15:10:24 -08002267 *
Robert Love1875f272009-11-03 11:47:50 -08002268 * Returns: 0 on success or a negative value on failure
Robert Love34f42a02009-02-27 10:55:45 -08002269 */
Robert Love85b4aa42008-12-09 15:10:24 -08002270static void __exit fcoe_exit(void)
2271{
Chris Leech014f5c32009-08-25 13:59:30 -07002272 struct fcoe_interface *fcoe, *tmp;
Chris Leech2e70e242009-08-25 14:00:23 -07002273 struct fcoe_port *port;
Robert Love1875f272009-11-03 11:47:50 -08002274 unsigned int cpu;
Robert Love85b4aa42008-12-09 15:10:24 -08002275
Chris Leechdfc1d0f2009-08-25 14:00:13 -07002276 mutex_lock(&fcoe_config_mutex);
2277
Robert Love85b4aa42008-12-09 15:10:24 -08002278 fcoe_dev_cleanup();
2279
Vasu Dev5919a592009-03-27 09:03:29 -07002280 /* releases the associated fcoe hosts */
Chris Leech090eb6c2009-08-25 14:00:28 -07002281 rtnl_lock();
2282 list_for_each_entry_safe(fcoe, tmp, &fcoe_hostlist, list) {
Chris Leechc863df32009-08-25 14:00:18 -07002283 list_del(&fcoe->list);
Chris Leech2e70e242009-08-25 14:00:23 -07002284 port = lport_priv(fcoe->ctlr.lp);
Chris Leech2e70e242009-08-25 14:00:23 -07002285 fcoe_interface_cleanup(fcoe);
Tejun Heo2ca32b42011-01-28 16:05:32 -08002286 queue_work(fcoe_wq, &port->destroy_work);
Chris Leechc863df32009-08-25 14:00:18 -07002287 }
Chris Leech090eb6c2009-08-25 14:00:28 -07002288 rtnl_unlock();
Robert Love85b4aa42008-12-09 15:10:24 -08002289
Robert Love8976f422009-03-17 11:41:46 -07002290 unregister_hotcpu_notifier(&fcoe_cpu_notifier);
2291
Chris Leech014f5c32009-08-25 13:59:30 -07002292 for_each_online_cpu(cpu)
Robert Love8976f422009-03-17 11:41:46 -07002293 fcoe_percpu_thread_destroy(cpu);
Robert Love85b4aa42008-12-09 15:10:24 -08002294
Chris Leechdfc1d0f2009-08-25 14:00:13 -07002295 mutex_unlock(&fcoe_config_mutex);
Chris Leech2e70e242009-08-25 14:00:23 -07002296
Tejun Heo2ca32b42011-01-28 16:05:32 -08002297 /*
2298 * destroy_work's may be chained but destroy_workqueue()
2299 * can take care of them. Just kill the fcoe_wq.
2300 */
2301 destroy_workqueue(fcoe_wq);
Chris Leech2e70e242009-08-25 14:00:23 -07002302
Tejun Heo2ca32b42011-01-28 16:05:32 -08002303 /*
2304 * Detaching from the scsi transport must happen after all
2305 * destroys are done on the fcoe_wq. destroy_workqueue will
2306 * enusre the fcoe_wq is flushed.
2307 */
Chris Leech2e70e242009-08-25 14:00:23 -07002308 fcoe_if_exit();
Yi Zou78a58242011-01-28 16:05:16 -08002309
2310 /* detach from fcoe transport */
2311 fcoe_transport_detach(&fcoe_sw_transport);
Robert Love85b4aa42008-12-09 15:10:24 -08002312}
2313module_exit(fcoe_exit);
Chris Leech11b56182009-11-03 11:46:29 -08002314
2315/**
2316 * fcoe_flogi_resp() - FCoE specific FLOGI and FDISC response handler
2317 * @seq: active sequence in the FLOGI or FDISC exchange
2318 * @fp: response frame, or error encoded in a pointer (timeout)
2319 * @arg: pointer the the fcoe_ctlr structure
2320 *
Uwe Kleine-König65155b32010-06-11 12:17:01 +02002321 * This handles MAC address management for FCoE, then passes control on to
Chris Leech11b56182009-11-03 11:46:29 -08002322 * the libfc FLOGI response handler.
2323 */
2324static void fcoe_flogi_resp(struct fc_seq *seq, struct fc_frame *fp, void *arg)
2325{
2326 struct fcoe_ctlr *fip = arg;
2327 struct fc_exch *exch = fc_seq_exch(seq);
2328 struct fc_lport *lport = exch->lp;
2329 u8 *mac;
2330
2331 if (IS_ERR(fp))
2332 goto done;
2333
2334 mac = fr_cb(fp)->granted_mac;
2335 if (is_zero_ether_addr(mac)) {
2336 /* pre-FIP */
Joe Eykholt386309ce2009-11-03 11:49:16 -08002337 if (fcoe_ctlr_recv_flogi(fip, lport, fp)) {
Chris Leech11b56182009-11-03 11:46:29 -08002338 fc_frame_free(fp);
2339 return;
2340 }
Chris Leech11b56182009-11-03 11:46:29 -08002341 }
Joe Eykholt386309ce2009-11-03 11:49:16 -08002342 fcoe_update_src_mac(lport, mac);
Chris Leech11b56182009-11-03 11:46:29 -08002343done:
2344 fc_lport_flogi_resp(seq, fp, lport);
2345}
2346
2347/**
2348 * fcoe_logo_resp() - FCoE specific LOGO response handler
2349 * @seq: active sequence in the LOGO exchange
2350 * @fp: response frame, or error encoded in a pointer (timeout)
2351 * @arg: pointer the the fcoe_ctlr structure
2352 *
Uwe Kleine-König65155b32010-06-11 12:17:01 +02002353 * This handles MAC address management for FCoE, then passes control on to
Chris Leech11b56182009-11-03 11:46:29 -08002354 * the libfc LOGO response handler.
2355 */
2356static void fcoe_logo_resp(struct fc_seq *seq, struct fc_frame *fp, void *arg)
2357{
Joe Eykholt386309ce2009-11-03 11:49:16 -08002358 struct fc_lport *lport = arg;
Chris Leech11b56182009-11-03 11:46:29 -08002359 static u8 zero_mac[ETH_ALEN] = { 0 };
2360
2361 if (!IS_ERR(fp))
Joe Eykholt386309ce2009-11-03 11:49:16 -08002362 fcoe_update_src_mac(lport, zero_mac);
Chris Leech11b56182009-11-03 11:46:29 -08002363 fc_lport_logo_resp(seq, fp, lport);
2364}
2365
2366/**
2367 * fcoe_elsct_send - FCoE specific ELS handler
2368 *
2369 * This does special case handling of FIP encapsualted ELS exchanges for FCoE,
2370 * using FCoE specific response handlers and passing the FIP controller as
2371 * the argument (the lport is still available from the exchange).
2372 *
2373 * Most of the work here is just handed off to the libfc routine.
2374 */
Robert Love1875f272009-11-03 11:47:50 -08002375static struct fc_seq *fcoe_elsct_send(struct fc_lport *lport, u32 did,
2376 struct fc_frame *fp, unsigned int op,
2377 void (*resp)(struct fc_seq *,
2378 struct fc_frame *,
2379 void *),
2380 void *arg, u32 timeout)
Chris Leech11b56182009-11-03 11:46:29 -08002381{
2382 struct fcoe_port *port = lport_priv(lport);
Bhanu Prakash Gollapudi8597ae82011-01-28 16:05:37 -08002383 struct fcoe_interface *fcoe = port->priv;
Chris Leech11b56182009-11-03 11:46:29 -08002384 struct fcoe_ctlr *fip = &fcoe->ctlr;
2385 struct fc_frame_header *fh = fc_frame_header_get(fp);
2386
2387 switch (op) {
2388 case ELS_FLOGI:
2389 case ELS_FDISC:
Joe Eykholte10f8c62010-07-20 15:20:30 -07002390 if (lport->point_to_multipoint)
2391 break;
Chris Leech11b56182009-11-03 11:46:29 -08002392 return fc_elsct_send(lport, did, fp, op, fcoe_flogi_resp,
2393 fip, timeout);
2394 case ELS_LOGO:
2395 /* only hook onto fabric logouts, not port logouts */
2396 if (ntoh24(fh->fh_d_id) != FC_FID_FLOGI)
2397 break;
2398 return fc_elsct_send(lport, did, fp, op, fcoe_logo_resp,
Joe Eykholt386309ce2009-11-03 11:49:16 -08002399 lport, timeout);
Chris Leech11b56182009-11-03 11:46:29 -08002400 }
2401 return fc_elsct_send(lport, did, fp, op, resp, arg, timeout);
2402}
2403
Chris Leech9a057532009-11-03 11:46:40 -08002404/**
2405 * fcoe_vport_create() - create an fc_host/scsi_host for a vport
2406 * @vport: fc_vport object to create a new fc_host for
2407 * @disabled: start the new fc_host in a disabled state by default?
2408 *
2409 * Returns: 0 for success
2410 */
2411static int fcoe_vport_create(struct fc_vport *vport, bool disabled)
2412{
2413 struct Scsi_Host *shost = vport_to_shost(vport);
2414 struct fc_lport *n_port = shost_priv(shost);
2415 struct fcoe_port *port = lport_priv(n_port);
Bhanu Prakash Gollapudi8597ae82011-01-28 16:05:37 -08002416 struct fcoe_interface *fcoe = port->priv;
Chris Leech9a057532009-11-03 11:46:40 -08002417 struct net_device *netdev = fcoe->netdev;
2418 struct fc_lport *vn_port;
2419
2420 mutex_lock(&fcoe_config_mutex);
2421 vn_port = fcoe_if_create(fcoe, &vport->dev, 1);
2422 mutex_unlock(&fcoe_config_mutex);
2423
2424 if (IS_ERR(vn_port)) {
2425 printk(KERN_ERR "fcoe: fcoe_vport_create(%s) failed\n",
2426 netdev->name);
2427 return -EIO;
2428 }
2429
2430 if (disabled) {
2431 fc_vport_set_state(vport, FC_VPORT_DISABLED);
2432 } else {
2433 vn_port->boot_time = jiffies;
2434 fc_fabric_login(vn_port);
2435 fc_vport_setlink(vn_port);
2436 }
2437 return 0;
2438}
2439
2440/**
2441 * fcoe_vport_destroy() - destroy the fc_host/scsi_host for a vport
2442 * @vport: fc_vport object that is being destroyed
2443 *
2444 * Returns: 0 for success
2445 */
2446static int fcoe_vport_destroy(struct fc_vport *vport)
2447{
2448 struct Scsi_Host *shost = vport_to_shost(vport);
2449 struct fc_lport *n_port = shost_priv(shost);
2450 struct fc_lport *vn_port = vport->dd_data;
2451 struct fcoe_port *port = lport_priv(vn_port);
2452
2453 mutex_lock(&n_port->lp_mutex);
2454 list_del(&vn_port->list);
2455 mutex_unlock(&n_port->lp_mutex);
Tejun Heo2ca32b42011-01-28 16:05:32 -08002456 queue_work(fcoe_wq, &port->destroy_work);
Chris Leech9a057532009-11-03 11:46:40 -08002457 return 0;
2458}
2459
2460/**
2461 * fcoe_vport_disable() - change vport state
2462 * @vport: vport to bring online/offline
2463 * @disable: should the vport be disabled?
2464 */
2465static int fcoe_vport_disable(struct fc_vport *vport, bool disable)
2466{
2467 struct fc_lport *lport = vport->dd_data;
2468
2469 if (disable) {
2470 fc_vport_set_state(vport, FC_VPORT_DISABLED);
2471 fc_fabric_logoff(lport);
2472 } else {
2473 lport->boot_time = jiffies;
2474 fc_fabric_login(lport);
2475 fc_vport_setlink(lport);
2476 }
2477
2478 return 0;
2479}
2480
Chris Leechdc8596d2009-11-03 11:47:18 -08002481/**
2482 * fcoe_vport_set_symbolic_name() - append vport string to symbolic name
2483 * @vport: fc_vport with a new symbolic name string
2484 *
2485 * After generating a new symbolic name string, a new RSPN_ID request is
2486 * sent to the name server. There is no response handler, so if it fails
2487 * for some reason it will not be retried.
2488 */
2489static void fcoe_set_vport_symbolic_name(struct fc_vport *vport)
2490{
2491 struct fc_lport *lport = vport->dd_data;
2492 struct fc_frame *fp;
2493 size_t len;
2494
2495 snprintf(fc_host_symbolic_name(lport->host), FC_SYMBOLIC_NAME_SIZE,
2496 "%s v%s over %s : %s", FCOE_NAME, FCOE_VERSION,
2497 fcoe_netdev(lport)->name, vport->symbolic_name);
2498
2499 if (lport->state != LPORT_ST_READY)
2500 return;
2501
2502 len = strnlen(fc_host_symbolic_name(lport->host), 255);
2503 fp = fc_frame_alloc(lport,
2504 sizeof(struct fc_ct_hdr) +
2505 sizeof(struct fc_ns_rspn) + len);
2506 if (!fp)
2507 return;
2508 lport->tt.elsct_send(lport, FC_FID_DIR_SERV, fp, FC_NS_RSPN_ID,
Joe Eykholtb94f8952009-11-03 11:50:21 -08002509 NULL, NULL, 3 * lport->r_a_tov);
Chris Leechdc8596d2009-11-03 11:47:18 -08002510}
Yi Zoub84056b2009-11-20 14:55:19 -08002511
2512/**
2513 * fcoe_get_lesb() - Fill the FCoE Link Error Status Block
2514 * @lport: the local port
2515 * @fc_lesb: the link error status block
2516 */
2517static void fcoe_get_lesb(struct fc_lport *lport,
2518 struct fc_els_lesb *fc_lesb)
2519{
2520 unsigned int cpu;
2521 u32 lfc, vlfc, mdac;
2522 struct fcoe_dev_stats *devst;
2523 struct fcoe_fc_els_lesb *lesb;
Eric Dumazet28172732010-07-07 14:58:56 -07002524 struct rtnl_link_stats64 temp;
Yi Zoub84056b2009-11-20 14:55:19 -08002525 struct net_device *netdev = fcoe_netdev(lport);
2526
2527 lfc = 0;
2528 vlfc = 0;
2529 mdac = 0;
2530 lesb = (struct fcoe_fc_els_lesb *)fc_lesb;
2531 memset(lesb, 0, sizeof(*lesb));
2532 for_each_possible_cpu(cpu) {
2533 devst = per_cpu_ptr(lport->dev_stats, cpu);
2534 lfc += devst->LinkFailureCount;
2535 vlfc += devst->VLinkFailureCount;
2536 mdac += devst->MissDiscAdvCount;
2537 }
2538 lesb->lesb_link_fail = htonl(lfc);
2539 lesb->lesb_vlink_fail = htonl(vlfc);
2540 lesb->lesb_miss_fka = htonl(mdac);
Eric Dumazet28172732010-07-07 14:58:56 -07002541 lesb->lesb_fcs_error = htonl(dev_get_stats(netdev, &temp)->rx_crc_errors);
Yi Zoub84056b2009-11-20 14:55:19 -08002542}
Joe Eykholt7d65b0d2010-03-12 16:08:02 -08002543
2544/**
2545 * fcoe_set_port_id() - Callback from libfc when Port_ID is set.
2546 * @lport: the local port
2547 * @port_id: the port ID
2548 * @fp: the received frame, if any, that caused the port_id to be set.
2549 *
2550 * This routine handles the case where we received a FLOGI and are
2551 * entering point-to-point mode. We need to call fcoe_ctlr_recv_flogi()
2552 * so it can set the non-mapped mode and gateway address.
2553 *
2554 * The FLOGI LS_ACC is handled by fcoe_flogi_resp().
2555 */
2556static void fcoe_set_port_id(struct fc_lport *lport,
2557 u32 port_id, struct fc_frame *fp)
2558{
2559 struct fcoe_port *port = lport_priv(lport);
Bhanu Prakash Gollapudi8597ae82011-01-28 16:05:37 -08002560 struct fcoe_interface *fcoe = port->priv;
Joe Eykholt7d65b0d2010-03-12 16:08:02 -08002561
2562 if (fp && fc_frame_payload_op(fp) == ELS_FLOGI)
2563 fcoe_ctlr_recv_flogi(&fcoe->ctlr, lport, fp);
2564}