blob: 8a1005d117b71ca42f678f1679346a5bd32286af [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>
34#include <scsi/scsi_tcq.h>
35#include <scsi/scsicam.h>
36#include <scsi/scsi_transport.h>
37#include <scsi/scsi_transport_fc.h>
38#include <net/rtnetlink.h>
39
40#include <scsi/fc/fc_encaps.h>
Joe Eykholt97c83892009-03-17 11:42:40 -070041#include <scsi/fc/fc_fip.h>
Robert Love85b4aa42008-12-09 15:10:24 -080042
43#include <scsi/libfc.h>
44#include <scsi/fc_frame.h>
45#include <scsi/libfcoe.h>
Robert Love85b4aa42008-12-09 15:10:24 -080046
Vasu Devfdd78022009-03-17 11:42:24 -070047#include "fcoe.h"
48
Robert Love85b4aa42008-12-09 15:10:24 -080049MODULE_AUTHOR("Open-FCoE.org");
50MODULE_DESCRIPTION("FCoE");
Vasu Dev9b34ecf2009-03-17 11:42:13 -070051MODULE_LICENSE("GPL v2");
Robert Love85b4aa42008-12-09 15:10:24 -080052
Yi Zou05cc7392009-08-25 13:59:03 -070053/* Performance tuning parameters for fcoe */
54static unsigned int fcoe_ddp_min;
55module_param_named(ddp_min, fcoe_ddp_min, uint, S_IRUGO | S_IWUSR);
56MODULE_PARM_DESC(ddp_min, "Minimum I/O size in bytes for " \
57 "Direct Data Placement (DDP).");
58
Chris Leechdfc1d0f2009-08-25 14:00:13 -070059DEFINE_MUTEX(fcoe_config_mutex);
60
Joe Eykholte7a51992009-08-25 14:04:08 -070061/* fcoe_percpu_clean completion. Waiter protected by fcoe_create_mutex */
62static DECLARE_COMPLETION(fcoe_flush_completion);
63
Robert Love85b4aa42008-12-09 15:10:24 -080064/* fcoe host list */
Chris Leech090eb6c2009-08-25 14:00:28 -070065/* must only by accessed under the RTNL mutex */
Robert Love85b4aa42008-12-09 15:10:24 -080066LIST_HEAD(fcoe_hostlist);
Robert Love5e5e92d2009-03-17 11:41:35 -070067DEFINE_PER_CPU(struct fcoe_percpu_s, fcoe_percpu);
Robert Love85b4aa42008-12-09 15:10:24 -080068
Chris Leechdd3fd722009-04-21 16:27:36 -070069/* Function Prototypes */
Robert Love1875f272009-11-03 11:47:50 -080070static int fcoe_reset(struct Scsi_Host *);
Vasu Devfdd78022009-03-17 11:42:24 -070071static int fcoe_xmit(struct fc_lport *, struct fc_frame *);
72static int fcoe_rcv(struct sk_buff *, struct net_device *,
73 struct packet_type *, struct net_device *);
Robert Love1875f272009-11-03 11:47:50 -080074static int fcoe_percpu_receive_thread(void *);
75static void fcoe_clean_pending_queue(struct fc_lport *);
76static void fcoe_percpu_clean(struct fc_lport *);
Robert Love5e4f8fe2010-05-07 15:18:35 -070077static int fcoe_link_speed_update(struct fc_lport *);
Robert Love1875f272009-11-03 11:47:50 -080078static int fcoe_link_ok(struct fc_lport *);
Vasu Devfdd78022009-03-17 11:42:24 -070079
80static struct fc_lport *fcoe_hostlist_lookup(const struct net_device *);
81static int fcoe_hostlist_add(const struct fc_lport *);
Vasu Devfdd78022009-03-17 11:42:24 -070082
Vasu Dev4bb6b512009-05-06 10:52:34 -070083static void fcoe_check_wait_queue(struct fc_lport *, struct sk_buff *);
Robert Love85b4aa42008-12-09 15:10:24 -080084static int fcoe_device_notification(struct notifier_block *, ulong, void *);
85static void fcoe_dev_setup(void);
86static void fcoe_dev_cleanup(void);
Robert Love1875f272009-11-03 11:47:50 -080087static struct fcoe_interface
88*fcoe_hostlist_lookup_port(const struct net_device *);
Robert Love85b4aa42008-12-09 15:10:24 -080089
Robert Love1875f272009-11-03 11:47:50 -080090static int fcoe_fip_recv(struct sk_buff *, struct net_device *,
91 struct packet_type *, struct net_device *);
92
93static void fcoe_fip_send(struct fcoe_ctlr *, struct sk_buff *);
94static void fcoe_update_src_mac(struct fc_lport *, u8 *);
95static u8 *fcoe_get_src_mac(struct fc_lport *);
96static void fcoe_destroy_work(struct work_struct *);
97
98static int fcoe_ddp_setup(struct fc_lport *, u16, struct scatterlist *,
99 unsigned int);
100static int fcoe_ddp_done(struct fc_lport *, u16);
101
102static int fcoe_cpu_callback(struct notifier_block *, unsigned long, void *);
103
Yi Zou78a58242011-01-28 16:05:16 -0800104static bool fcoe_match(struct net_device *netdev);
105static int fcoe_create(struct net_device *netdev, enum fip_state fip_mode);
106static int fcoe_destroy(struct net_device *netdev);
107static int fcoe_enable(struct net_device *netdev);
108static int fcoe_disable(struct net_device *netdev);
Robert Love1875f272009-11-03 11:47:50 -0800109
Robert Love1875f272009-11-03 11:47:50 -0800110static struct fc_seq *fcoe_elsct_send(struct fc_lport *,
111 u32 did, struct fc_frame *,
112 unsigned int op,
113 void (*resp)(struct fc_seq *,
114 struct fc_frame *,
115 void *),
116 void *, u32 timeout);
Chris Leech859b7b62009-11-20 14:54:47 -0800117static void fcoe_recv_frame(struct sk_buff *skb);
Robert Love1875f272009-11-03 11:47:50 -0800118
Yi Zoub84056b2009-11-20 14:55:19 -0800119static void fcoe_get_lesb(struct fc_lport *, struct fc_els_lesb *);
120
Robert Love1875f272009-11-03 11:47:50 -0800121/* notification function for packets from net device */
Robert Love85b4aa42008-12-09 15:10:24 -0800122static struct notifier_block fcoe_notifier = {
123 .notifier_call = fcoe_device_notification,
124};
125
Robert Love1875f272009-11-03 11:47:50 -0800126/* notification function for CPU hotplug events */
127static struct notifier_block fcoe_cpu_notifier = {
128 .notifier_call = fcoe_cpu_callback,
129};
130
Yi Zou8ca86f82011-01-28 16:05:11 -0800131static struct scsi_transport_template *fcoe_nport_scsi_transport;
132static struct scsi_transport_template *fcoe_vport_scsi_transport;
Vasu Dev7f349142009-03-27 09:06:31 -0700133
Robert Love1875f272009-11-03 11:47:50 -0800134static int fcoe_vport_destroy(struct fc_vport *);
135static int fcoe_vport_create(struct fc_vport *, bool disabled);
136static int fcoe_vport_disable(struct fc_vport *, bool disable);
137static void fcoe_set_vport_symbolic_name(struct fc_vport *);
Joe Eykholt7d65b0d2010-03-12 16:08:02 -0800138static void fcoe_set_port_id(struct fc_lport *, u32, struct fc_frame *);
Robert Love1875f272009-11-03 11:47:50 -0800139
140static struct libfc_function_template fcoe_libfc_fcn_templ = {
141 .frame_send = fcoe_xmit,
142 .ddp_setup = fcoe_ddp_setup,
143 .ddp_done = fcoe_ddp_done,
144 .elsct_send = fcoe_elsct_send,
Yi Zoub84056b2009-11-20 14:55:19 -0800145 .get_lesb = fcoe_get_lesb,
Joe Eykholt7d65b0d2010-03-12 16:08:02 -0800146 .lport_set_port_id = fcoe_set_port_id,
Robert Love1875f272009-11-03 11:47:50 -0800147};
Chris Leech9a057532009-11-03 11:46:40 -0800148
Yi Zou8ca86f82011-01-28 16:05:11 -0800149struct fc_function_template fcoe_nport_fc_functions = {
Vasu Dev7f349142009-03-27 09:06:31 -0700150 .show_host_node_name = 1,
151 .show_host_port_name = 1,
152 .show_host_supported_classes = 1,
153 .show_host_supported_fc4s = 1,
154 .show_host_active_fc4s = 1,
155 .show_host_maxframe_size = 1,
156
157 .show_host_port_id = 1,
158 .show_host_supported_speeds = 1,
159 .get_host_speed = fc_get_host_speed,
160 .show_host_speed = 1,
161 .show_host_port_type = 1,
162 .get_host_port_state = fc_get_host_port_state,
163 .show_host_port_state = 1,
164 .show_host_symbolic_name = 1,
165
166 .dd_fcrport_size = sizeof(struct fc_rport_libfc_priv),
167 .show_rport_maxframe_size = 1,
168 .show_rport_supported_classes = 1,
169
170 .show_host_fabric_name = 1,
171 .show_starget_node_name = 1,
172 .show_starget_port_name = 1,
173 .show_starget_port_id = 1,
174 .set_rport_dev_loss_tmo = fc_set_rport_loss_tmo,
175 .show_rport_dev_loss_tmo = 1,
176 .get_fc_host_stats = fc_get_host_stats,
177 .issue_fc_host_lip = fcoe_reset,
178
179 .terminate_rport_io = fc_rport_terminate_io,
Chris Leech9a057532009-11-03 11:46:40 -0800180
181 .vport_create = fcoe_vport_create,
182 .vport_delete = fcoe_vport_destroy,
183 .vport_disable = fcoe_vport_disable,
Chris Leechdc8596d2009-11-03 11:47:18 -0800184 .set_vport_symbolic_name = fcoe_set_vport_symbolic_name,
Steve Maa51ab392009-11-03 11:47:34 -0800185
186 .bsg_request = fc_lport_bsg_request,
Vasu Dev7f349142009-03-27 09:06:31 -0700187};
188
Yi Zou8ca86f82011-01-28 16:05:11 -0800189struct fc_function_template fcoe_vport_fc_functions = {
Chris Leeche9084bb2009-11-03 11:46:34 -0800190 .show_host_node_name = 1,
191 .show_host_port_name = 1,
192 .show_host_supported_classes = 1,
193 .show_host_supported_fc4s = 1,
194 .show_host_active_fc4s = 1,
195 .show_host_maxframe_size = 1,
196
197 .show_host_port_id = 1,
198 .show_host_supported_speeds = 1,
199 .get_host_speed = fc_get_host_speed,
200 .show_host_speed = 1,
201 .show_host_port_type = 1,
202 .get_host_port_state = fc_get_host_port_state,
203 .show_host_port_state = 1,
204 .show_host_symbolic_name = 1,
205
206 .dd_fcrport_size = sizeof(struct fc_rport_libfc_priv),
207 .show_rport_maxframe_size = 1,
208 .show_rport_supported_classes = 1,
209
210 .show_host_fabric_name = 1,
211 .show_starget_node_name = 1,
212 .show_starget_port_name = 1,
213 .show_starget_port_id = 1,
214 .set_rport_dev_loss_tmo = fc_set_rport_loss_tmo,
215 .show_rport_dev_loss_tmo = 1,
216 .get_fc_host_stats = fc_get_host_stats,
217 .issue_fc_host_lip = fcoe_reset,
218
219 .terminate_rport_io = fc_rport_terminate_io,
Steve Maa51ab392009-11-03 11:47:34 -0800220
221 .bsg_request = fc_lport_bsg_request,
Chris Leeche9084bb2009-11-03 11:46:34 -0800222};
223
Vasu Dev7f349142009-03-27 09:06:31 -0700224static struct scsi_host_template fcoe_shost_template = {
225 .module = THIS_MODULE,
226 .name = "FCoE Driver",
227 .proc_name = FCOE_NAME,
228 .queuecommand = fc_queuecommand,
229 .eh_abort_handler = fc_eh_abort,
230 .eh_device_reset_handler = fc_eh_device_reset,
231 .eh_host_reset_handler = fc_eh_host_reset,
232 .slave_alloc = fc_slave_alloc,
233 .change_queue_depth = fc_change_queue_depth,
234 .change_queue_type = fc_change_queue_type,
235 .this_id = -1,
Vasu Dev14caf442009-10-15 17:46:55 -0700236 .cmd_per_lun = 3,
Vasu Dev7f349142009-03-27 09:06:31 -0700237 .can_queue = FCOE_MAX_OUTSTANDING_COMMANDS,
238 .use_clustering = ENABLE_CLUSTERING,
239 .sg_tablesize = SG_ALL,
240 .max_sectors = 0xffff,
241};
242
Chris Leech54b649f2009-08-25 14:00:07 -0700243/**
Robert Love1875f272009-11-03 11:47:50 -0800244 * fcoe_interface_setup() - Setup a FCoE interface
245 * @fcoe: The new FCoE interface
246 * @netdev: The net device that the fcoe interface is on
Chris Leech54b649f2009-08-25 14:00:07 -0700247 *
248 * Returns : 0 for success
Chris Leech2e70e242009-08-25 14:00:23 -0700249 * Locking: must be called with the RTNL mutex held
Chris Leech54b649f2009-08-25 14:00:07 -0700250 */
251static int fcoe_interface_setup(struct fcoe_interface *fcoe,
252 struct net_device *netdev)
253{
254 struct fcoe_ctlr *fip = &fcoe->ctlr;
255 struct netdev_hw_addr *ha;
Yi Zou5bab87e2009-11-03 11:49:43 -0800256 struct net_device *real_dev;
Chris Leech54b649f2009-08-25 14:00:07 -0700257 u8 flogi_maddr[ETH_ALEN];
Yi Zoub7a727f2009-10-21 16:28:03 -0700258 const struct net_device_ops *ops;
Chris Leech54b649f2009-08-25 14:00:07 -0700259
260 fcoe->netdev = netdev;
261
Yi Zoub7a727f2009-10-21 16:28:03 -0700262 /* Let LLD initialize for FCoE */
263 ops = netdev->netdev_ops;
264 if (ops->ndo_fcoe_enable) {
265 if (ops->ndo_fcoe_enable(netdev))
266 FCOE_NETDEV_DBG(netdev, "Failed to enable FCoE"
267 " specific feature for LLD.\n");
268 }
269
Chris Leech54b649f2009-08-25 14:00:07 -0700270 /* Do not support for bonding device */
271 if ((netdev->priv_flags & IFF_MASTER_ALB) ||
272 (netdev->priv_flags & IFF_SLAVE_INACTIVE) ||
273 (netdev->priv_flags & IFF_MASTER_8023AD)) {
john fastabend59d92512009-11-03 11:48:44 -0800274 FCOE_NETDEV_DBG(netdev, "Bonded interfaces not supported\n");
Chris Leech54b649f2009-08-25 14:00:07 -0700275 return -EOPNOTSUPP;
276 }
277
278 /* look for SAN MAC address, if multiple SAN MACs exist, only
279 * use the first one for SPMA */
Yi Zou5bab87e2009-11-03 11:49:43 -0800280 real_dev = (netdev->priv_flags & IFF_802_1Q_VLAN) ?
281 vlan_dev_real_dev(netdev) : netdev;
Chris Leech54b649f2009-08-25 14:00:07 -0700282 rcu_read_lock();
Yi Zou5bab87e2009-11-03 11:49:43 -0800283 for_each_dev_addr(real_dev, ha) {
Chris Leech54b649f2009-08-25 14:00:07 -0700284 if ((ha->type == NETDEV_HW_ADDR_T_SAN) &&
Yi Zoubf361702009-11-03 11:49:38 -0800285 (is_valid_ether_addr(ha->addr))) {
Chris Leech54b649f2009-08-25 14:00:07 -0700286 memcpy(fip->ctl_src_addr, ha->addr, ETH_ALEN);
287 fip->spma = 1;
288 break;
289 }
290 }
291 rcu_read_unlock();
292
293 /* setup Source Mac Address */
294 if (!fip->spma)
295 memcpy(fip->ctl_src_addr, netdev->dev_addr, netdev->addr_len);
296
297 /*
298 * Add FCoE MAC address as second unicast MAC address
299 * or enter promiscuous mode if not capable of listening
300 * for multiple unicast MACs.
301 */
Chris Leech54b649f2009-08-25 14:00:07 -0700302 memcpy(flogi_maddr, (u8[6]) FC_FCOE_FLOGI_MAC, ETH_ALEN);
Jiri Pirkoa748ee22010-04-01 21:22:09 +0000303 dev_uc_add(netdev, flogi_maddr);
Chris Leech54b649f2009-08-25 14:00:07 -0700304 if (fip->spma)
Jiri Pirkoa748ee22010-04-01 21:22:09 +0000305 dev_uc_add(netdev, fip->ctl_src_addr);
Joe Eykholte10f8c62010-07-20 15:20:30 -0700306 if (fip->mode == FIP_MODE_VN2VN) {
307 dev_mc_add(netdev, FIP_ALL_VN2VN_MACS);
308 dev_mc_add(netdev, FIP_ALL_P2P_MACS);
309 } else
310 dev_mc_add(netdev, FIP_ALL_ENODE_MACS);
Chris Leech54b649f2009-08-25 14:00:07 -0700311
312 /*
313 * setup the receive function from ethernet driver
314 * on the ethertype for the given device
315 */
316 fcoe->fcoe_packet_type.func = fcoe_rcv;
317 fcoe->fcoe_packet_type.type = __constant_htons(ETH_P_FCOE);
318 fcoe->fcoe_packet_type.dev = netdev;
319 dev_add_pack(&fcoe->fcoe_packet_type);
320
321 fcoe->fip_packet_type.func = fcoe_fip_recv;
322 fcoe->fip_packet_type.type = htons(ETH_P_FIP);
323 fcoe->fip_packet_type.dev = netdev;
324 dev_add_pack(&fcoe->fip_packet_type);
325
326 return 0;
327}
328
Vasu Dev7f349142009-03-27 09:06:31 -0700329/**
Robert Love1875f272009-11-03 11:47:50 -0800330 * fcoe_interface_create() - Create a FCoE interface on a net device
331 * @netdev: The net device to create the FCoE interface on
Joe Eykholt1dd454d2010-07-20 15:20:46 -0700332 * @fip_mode: The mode to use for FIP
Chris Leech030f4e02009-08-25 14:00:02 -0700333 *
334 * Returns: pointer to a struct fcoe_interface or NULL on error
335 */
Joe Eykholt1dd454d2010-07-20 15:20:46 -0700336static struct fcoe_interface *fcoe_interface_create(struct net_device *netdev,
337 enum fip_state fip_mode)
Chris Leech030f4e02009-08-25 14:00:02 -0700338{
339 struct fcoe_interface *fcoe;
john fastabend59d92512009-11-03 11:48:44 -0800340 int err;
Chris Leech030f4e02009-08-25 14:00:02 -0700341
Robert Love7287fb92011-01-28 16:03:47 -0800342 if (!try_module_get(THIS_MODULE)) {
343 FCOE_NETDEV_DBG(netdev,
344 "Could not get a reference to the module\n");
345 fcoe = ERR_PTR(-EBUSY);
346 goto out;
347 }
348
Chris Leech030f4e02009-08-25 14:00:02 -0700349 fcoe = kzalloc(sizeof(*fcoe), GFP_KERNEL);
350 if (!fcoe) {
351 FCOE_NETDEV_DBG(netdev, "Could not allocate fcoe structure\n");
Robert Love7287fb92011-01-28 16:03:47 -0800352 fcoe = ERR_PTR(-ENOMEM);
353 goto out_nomod;
Chris Leech030f4e02009-08-25 14:00:02 -0700354 }
355
Chris Leech2e70e242009-08-25 14:00:23 -0700356 dev_hold(netdev);
Chris Leech030f4e02009-08-25 14:00:02 -0700357 kref_init(&fcoe->kref);
Chris Leech54b649f2009-08-25 14:00:07 -0700358
359 /*
360 * Initialize FIP.
361 */
Joe Eykholt1dd454d2010-07-20 15:20:46 -0700362 fcoe_ctlr_init(&fcoe->ctlr, fip_mode);
Chris Leech54b649f2009-08-25 14:00:07 -0700363 fcoe->ctlr.send = fcoe_fip_send;
364 fcoe->ctlr.update_mac = fcoe_update_src_mac;
Chris Leech11b56182009-11-03 11:46:29 -0800365 fcoe->ctlr.get_src_addr = fcoe_get_src_mac;
Chris Leech54b649f2009-08-25 14:00:07 -0700366
john fastabend59d92512009-11-03 11:48:44 -0800367 err = fcoe_interface_setup(fcoe, netdev);
368 if (err) {
369 fcoe_ctlr_destroy(&fcoe->ctlr);
370 kfree(fcoe);
371 dev_put(netdev);
Robert Love7287fb92011-01-28 16:03:47 -0800372 fcoe = ERR_PTR(err);
373 goto out_nomod;
john fastabend59d92512009-11-03 11:48:44 -0800374 }
Chris Leech030f4e02009-08-25 14:00:02 -0700375
Robert Love7287fb92011-01-28 16:03:47 -0800376 goto out;
377
378out_nomod:
379 module_put(THIS_MODULE);
380out:
Chris Leech030f4e02009-08-25 14:00:02 -0700381 return fcoe;
382}
383
384/**
Robert Love1875f272009-11-03 11:47:50 -0800385 * fcoe_interface_cleanup() - Clean up a FCoE interface
386 * @fcoe: The FCoE interface to be cleaned up
Chris Leech2e70e242009-08-25 14:00:23 -0700387 *
388 * Caller must be holding the RTNL mutex
Chris Leech54b649f2009-08-25 14:00:07 -0700389 */
390void fcoe_interface_cleanup(struct fcoe_interface *fcoe)
391{
392 struct net_device *netdev = fcoe->netdev;
393 struct fcoe_ctlr *fip = &fcoe->ctlr;
394 u8 flogi_maddr[ETH_ALEN];
Yi Zoub7a727f2009-10-21 16:28:03 -0700395 const struct net_device_ops *ops;
Chris Leech54b649f2009-08-25 14:00:07 -0700396
397 /*
398 * Don't listen for Ethernet packets anymore.
399 * synchronize_net() ensures that the packet handlers are not running
400 * on another CPU. dev_remove_pack() would do that, this calls the
401 * unsyncronized version __dev_remove_pack() to avoid multiple delays.
402 */
403 __dev_remove_pack(&fcoe->fcoe_packet_type);
404 __dev_remove_pack(&fcoe->fip_packet_type);
405 synchronize_net();
406
Chris Leech54b649f2009-08-25 14:00:07 -0700407 /* Delete secondary MAC addresses */
Chris Leech54b649f2009-08-25 14:00:07 -0700408 memcpy(flogi_maddr, (u8[6]) FC_FCOE_FLOGI_MAC, ETH_ALEN);
Jiri Pirkoa748ee22010-04-01 21:22:09 +0000409 dev_uc_del(netdev, flogi_maddr);
Chris Leech54b649f2009-08-25 14:00:07 -0700410 if (fip->spma)
Jiri Pirkoa748ee22010-04-01 21:22:09 +0000411 dev_uc_del(netdev, fip->ctl_src_addr);
Joe Eykholte10f8c62010-07-20 15:20:30 -0700412 if (fip->mode == FIP_MODE_VN2VN) {
413 dev_mc_del(netdev, FIP_ALL_VN2VN_MACS);
414 dev_mc_del(netdev, FIP_ALL_P2P_MACS);
415 } else
416 dev_mc_del(netdev, FIP_ALL_ENODE_MACS);
Yi Zoub7a727f2009-10-21 16:28:03 -0700417
418 /* Tell the LLD we are done w/ FCoE */
419 ops = netdev->netdev_ops;
420 if (ops->ndo_fcoe_disable) {
421 if (ops->ndo_fcoe_disable(netdev))
422 FCOE_NETDEV_DBG(netdev, "Failed to disable FCoE"
423 " specific feature for LLD.\n");
424 }
Chris Leech54b649f2009-08-25 14:00:07 -0700425}
426
427/**
Chris Leech030f4e02009-08-25 14:00:02 -0700428 * fcoe_interface_release() - fcoe_port kref release function
Robert Love1875f272009-11-03 11:47:50 -0800429 * @kref: Embedded reference count in an fcoe_interface struct
Chris Leech030f4e02009-08-25 14:00:02 -0700430 */
431static void fcoe_interface_release(struct kref *kref)
432{
433 struct fcoe_interface *fcoe;
Chris Leech2e70e242009-08-25 14:00:23 -0700434 struct net_device *netdev;
Chris Leech030f4e02009-08-25 14:00:02 -0700435
436 fcoe = container_of(kref, struct fcoe_interface, kref);
Chris Leech2e70e242009-08-25 14:00:23 -0700437 netdev = fcoe->netdev;
438 /* tear-down the FCoE controller */
439 fcoe_ctlr_destroy(&fcoe->ctlr);
Chris Leech030f4e02009-08-25 14:00:02 -0700440 kfree(fcoe);
Chris Leech2e70e242009-08-25 14:00:23 -0700441 dev_put(netdev);
Robert Love7287fb92011-01-28 16:03:47 -0800442 module_put(THIS_MODULE);
Chris Leech030f4e02009-08-25 14:00:02 -0700443}
444
445/**
Robert Love1875f272009-11-03 11:47:50 -0800446 * fcoe_interface_get() - Get a reference to a FCoE interface
447 * @fcoe: The FCoE interface to be held
Chris Leech030f4e02009-08-25 14:00:02 -0700448 */
449static inline void fcoe_interface_get(struct fcoe_interface *fcoe)
450{
451 kref_get(&fcoe->kref);
452}
453
454/**
Robert Love1875f272009-11-03 11:47:50 -0800455 * fcoe_interface_put() - Put a reference to a FCoE interface
456 * @fcoe: The FCoE interface to be released
Chris Leech030f4e02009-08-25 14:00:02 -0700457 */
458static inline void fcoe_interface_put(struct fcoe_interface *fcoe)
459{
460 kref_put(&fcoe->kref, fcoe_interface_release);
461}
462
463/**
Robert Love1875f272009-11-03 11:47:50 -0800464 * fcoe_fip_recv() - Handler for received FIP frames
465 * @skb: The receive skb
466 * @netdev: The associated net device
467 * @ptype: The packet_type structure which was used to register this handler
468 * @orig_dev: The original net_device the the skb was received on.
469 * (in case dev is a bond)
Vasu Devab6b85c2009-05-17 12:33:08 +0000470 *
471 * Returns: 0 for success
472 */
Robert Love1875f272009-11-03 11:47:50 -0800473static int fcoe_fip_recv(struct sk_buff *skb, struct net_device *netdev,
Vasu Devab6b85c2009-05-17 12:33:08 +0000474 struct packet_type *ptype,
475 struct net_device *orig_dev)
476{
Chris Leech259ad852009-08-25 13:59:41 -0700477 struct fcoe_interface *fcoe;
Vasu Devab6b85c2009-05-17 12:33:08 +0000478
Chris Leech259ad852009-08-25 13:59:41 -0700479 fcoe = container_of(ptype, struct fcoe_interface, fip_packet_type);
Chris Leech3fe9a0b2009-08-25 13:59:46 -0700480 fcoe_ctlr_recv(&fcoe->ctlr, skb);
Vasu Devab6b85c2009-05-17 12:33:08 +0000481 return 0;
482}
483
484/**
Robert Love1875f272009-11-03 11:47:50 -0800485 * fcoe_fip_send() - Send an Ethernet-encapsulated FIP frame
486 * @fip: The FCoE controller
487 * @skb: The FIP packet to be sent
Vasu Devab6b85c2009-05-17 12:33:08 +0000488 */
489static void fcoe_fip_send(struct fcoe_ctlr *fip, struct sk_buff *skb)
490{
Chris Leech3fe9a0b2009-08-25 13:59:46 -0700491 skb->dev = fcoe_from_ctlr(fip)->netdev;
Vasu Devab6b85c2009-05-17 12:33:08 +0000492 dev_queue_xmit(skb);
493}
494
495/**
Robert Love1875f272009-11-03 11:47:50 -0800496 * fcoe_update_src_mac() - Update the Ethernet MAC filters
497 * @lport: The local port to update the source MAC on
498 * @addr: Unicast MAC address to add
Vasu Devab6b85c2009-05-17 12:33:08 +0000499 *
500 * Remove any previously-set unicast MAC filter.
501 * Add secondary FCoE MAC address filter for our OUI.
502 */
Chris Leech11b56182009-11-03 11:46:29 -0800503static void fcoe_update_src_mac(struct fc_lport *lport, u8 *addr)
Vasu Devab6b85c2009-05-17 12:33:08 +0000504{
Chris Leech11b56182009-11-03 11:46:29 -0800505 struct fcoe_port *port = lport_priv(lport);
506 struct fcoe_interface *fcoe = port->fcoe;
Vasu Devab6b85c2009-05-17 12:33:08 +0000507
Vasu Devab6b85c2009-05-17 12:33:08 +0000508 rtnl_lock();
Chris Leech11b56182009-11-03 11:46:29 -0800509 if (!is_zero_ether_addr(port->data_src_addr))
Jiri Pirkoa748ee22010-04-01 21:22:09 +0000510 dev_uc_del(fcoe->netdev, port->data_src_addr);
Chris Leech11b56182009-11-03 11:46:29 -0800511 if (!is_zero_ether_addr(addr))
Jiri Pirkoa748ee22010-04-01 21:22:09 +0000512 dev_uc_add(fcoe->netdev, addr);
Chris Leech11b56182009-11-03 11:46:29 -0800513 memcpy(port->data_src_addr, addr, ETH_ALEN);
Vasu Devab6b85c2009-05-17 12:33:08 +0000514 rtnl_unlock();
515}
516
517/**
Chris Leech11b56182009-11-03 11:46:29 -0800518 * fcoe_get_src_mac() - return the Ethernet source address for an lport
519 * @lport: libfc lport
520 */
521static u8 *fcoe_get_src_mac(struct fc_lport *lport)
522{
523 struct fcoe_port *port = lport_priv(lport);
524
525 return port->data_src_addr;
526}
527
528/**
Robert Love1875f272009-11-03 11:47:50 -0800529 * fcoe_lport_config() - Set up a local port
530 * @lport: The local port to be setup
Vasu Dev7f349142009-03-27 09:06:31 -0700531 *
532 * Returns: 0 for success
533 */
Robert Love1875f272009-11-03 11:47:50 -0800534static int fcoe_lport_config(struct fc_lport *lport)
Vasu Dev7f349142009-03-27 09:06:31 -0700535{
Robert Love1875f272009-11-03 11:47:50 -0800536 lport->link_up = 0;
537 lport->qfull = 0;
538 lport->max_retry_count = 3;
539 lport->max_rport_retry_count = 3;
540 lport->e_d_tov = 2 * 1000; /* FC-FS default */
541 lport->r_a_tov = 2 * 2 * 1000;
542 lport->service_params = (FCP_SPPF_INIT_FCN | FCP_SPPF_RD_XRDY_DIS |
543 FCP_SPPF_RETRY | FCP_SPPF_CONF_COMPL);
544 lport->does_npiv = 1;
Vasu Dev7f349142009-03-27 09:06:31 -0700545
Robert Love1875f272009-11-03 11:47:50 -0800546 fc_lport_init_stats(lport);
Vasu Dev7f349142009-03-27 09:06:31 -0700547
548 /* lport fc_lport related configuration */
Robert Love1875f272009-11-03 11:47:50 -0800549 fc_lport_config(lport);
Vasu Dev7f349142009-03-27 09:06:31 -0700550
551 /* offload related configuration */
Robert Love1875f272009-11-03 11:47:50 -0800552 lport->crc_offload = 0;
553 lport->seq_offload = 0;
554 lport->lro_enabled = 0;
555 lport->lro_xid = 0;
556 lport->lso_max = 0;
Vasu Dev7f349142009-03-27 09:06:31 -0700557
558 return 0;
559}
560
561/**
Robert Love1875f272009-11-03 11:47:50 -0800562 * fcoe_queue_timer() - The fcoe queue timer
563 * @lport: The local port
Vasu Dev1047f222009-05-06 10:52:40 -0700564 *
565 * Calls fcoe_check_wait_queue on timeout
Vasu Dev1047f222009-05-06 10:52:40 -0700566 */
Robert Love1875f272009-11-03 11:47:50 -0800567static void fcoe_queue_timer(ulong lport)
Vasu Dev1047f222009-05-06 10:52:40 -0700568{
Robert Love1875f272009-11-03 11:47:50 -0800569 fcoe_check_wait_queue((struct fc_lport *)lport, NULL);
Vasu Dev1047f222009-05-06 10:52:40 -0700570}
571
572/**
Yi Zoudcece412009-11-20 15:22:21 -0800573 * fcoe_get_wwn() - Get the world wide name from LLD if it supports it
574 * @netdev: the associated net device
575 * @wwn: the output WWN
576 * @type: the type of WWN (WWPN or WWNN)
577 *
578 * Returns: 0 for success
579 */
580static int fcoe_get_wwn(struct net_device *netdev, u64 *wwn, int type)
581{
582 const struct net_device_ops *ops = netdev->netdev_ops;
583
584 if (ops->ndo_fcoe_get_wwn)
585 return ops->ndo_fcoe_get_wwn(netdev, wwn, type);
586 return -EINVAL;
587}
588
589/**
Yi Zou54a5b212010-07-20 15:21:17 -0700590 * fcoe_netdev_features_change - Updates the lport's offload flags based
591 * on the LLD netdev's FCoE feature flags
592 */
593static void fcoe_netdev_features_change(struct fc_lport *lport,
594 struct net_device *netdev)
595{
596 mutex_lock(&lport->lp_mutex);
597
598 if (netdev->features & NETIF_F_SG)
599 lport->sg_supp = 1;
600 else
601 lport->sg_supp = 0;
602
603 if (netdev->features & NETIF_F_FCOE_CRC) {
604 lport->crc_offload = 1;
605 FCOE_NETDEV_DBG(netdev, "Supports FCCRC offload\n");
606 } else {
607 lport->crc_offload = 0;
608 }
609
610 if (netdev->features & NETIF_F_FSO) {
611 lport->seq_offload = 1;
612 lport->lso_max = netdev->gso_max_size;
613 FCOE_NETDEV_DBG(netdev, "Supports LSO for max len 0x%x\n",
614 lport->lso_max);
615 } else {
616 lport->seq_offload = 0;
617 lport->lso_max = 0;
618 }
619
620 if (netdev->fcoe_ddp_xid) {
621 lport->lro_enabled = 1;
622 lport->lro_xid = netdev->fcoe_ddp_xid;
623 FCOE_NETDEV_DBG(netdev, "Supports LRO for max xid 0x%x\n",
624 lport->lro_xid);
625 } else {
626 lport->lro_enabled = 0;
627 lport->lro_xid = 0;
628 }
629
630 mutex_unlock(&lport->lp_mutex);
631}
632
633/**
Robert Love1875f272009-11-03 11:47:50 -0800634 * fcoe_netdev_config() - Set up net devive for SW FCoE
635 * @lport: The local port that is associated with the net device
636 * @netdev: The associated net device
Vasu Dev7f349142009-03-27 09:06:31 -0700637 *
Robert Love1875f272009-11-03 11:47:50 -0800638 * Must be called after fcoe_lport_config() as it will use local port mutex
Vasu Dev7f349142009-03-27 09:06:31 -0700639 *
Robert Love1875f272009-11-03 11:47:50 -0800640 * Returns: 0 for success
Vasu Dev7f349142009-03-27 09:06:31 -0700641 */
Robert Love1875f272009-11-03 11:47:50 -0800642static int fcoe_netdev_config(struct fc_lport *lport, struct net_device *netdev)
Vasu Dev7f349142009-03-27 09:06:31 -0700643{
644 u32 mfs;
645 u64 wwnn, wwpn;
Chris Leech25024982009-08-25 13:59:35 -0700646 struct fcoe_interface *fcoe;
Chris Leech014f5c32009-08-25 13:59:30 -0700647 struct fcoe_port *port;
Vasu Dev7f349142009-03-27 09:06:31 -0700648
649 /* Setup lport private data to point to fcoe softc */
Robert Love1875f272009-11-03 11:47:50 -0800650 port = lport_priv(lport);
Chris Leech25024982009-08-25 13:59:35 -0700651 fcoe = port->fcoe;
Vasu Dev7f349142009-03-27 09:06:31 -0700652
653 /*
654 * Determine max frame size based on underlying device and optional
655 * user-configured limit. If the MFS is too low, fcoe_link_ok()
656 * will return 0, so do this first.
657 */
Yi Zou7221d7e2009-10-21 16:27:52 -0700658 mfs = netdev->mtu;
659 if (netdev->features & NETIF_F_FCOE_MTU) {
660 mfs = FCOE_MTU;
661 FCOE_NETDEV_DBG(netdev, "Supports FCOE_MTU of %d bytes\n", mfs);
662 }
663 mfs -= (sizeof(struct fcoe_hdr) + sizeof(struct fcoe_crc_eof));
Robert Love1875f272009-11-03 11:47:50 -0800664 if (fc_set_mfs(lport, mfs))
Vasu Dev7f349142009-03-27 09:06:31 -0700665 return -EINVAL;
666
Vasu Dev7f349142009-03-27 09:06:31 -0700667 /* offload features support */
Yi Zou54a5b212010-07-20 15:21:17 -0700668 fcoe_netdev_features_change(lport, netdev);
Vasu Dev7f349142009-03-27 09:06:31 -0700669
Chris Leech014f5c32009-08-25 13:59:30 -0700670 skb_queue_head_init(&port->fcoe_pending_queue);
671 port->fcoe_pending_queue_active = 0;
Robert Love1875f272009-11-03 11:47:50 -0800672 setup_timer(&port->timer, fcoe_queue_timer, (unsigned long)lport);
Vasu Dev7f349142009-03-27 09:06:31 -0700673
Robert Love5e4f8fe2010-05-07 15:18:35 -0700674 fcoe_link_speed_update(lport);
675
Robert Love1875f272009-11-03 11:47:50 -0800676 if (!lport->vport) {
Yi Zoudcece412009-11-20 15:22:21 -0800677 if (fcoe_get_wwn(netdev, &wwnn, NETDEV_FCOE_WWNN))
678 wwnn = fcoe_wwn_from_mac(fcoe->ctlr.ctl_src_addr, 1, 0);
Robert Love1875f272009-11-03 11:47:50 -0800679 fc_set_wwnn(lport, wwnn);
Yi Zoudcece412009-11-20 15:22:21 -0800680 if (fcoe_get_wwn(netdev, &wwpn, NETDEV_FCOE_WWPN))
681 wwpn = fcoe_wwn_from_mac(fcoe->ctlr.ctl_src_addr,
Vasu Devcf4aebca2010-07-20 15:21:22 -0700682 2, 0);
Robert Love1875f272009-11-03 11:47:50 -0800683 fc_set_wwpn(lport, wwpn);
Chris Leech9a057532009-11-03 11:46:40 -0800684 }
Vasu Dev7f349142009-03-27 09:06:31 -0700685
Vasu Dev7f349142009-03-27 09:06:31 -0700686 return 0;
687}
688
689/**
Robert Love1875f272009-11-03 11:47:50 -0800690 * fcoe_shost_config() - Set up the SCSI host associated with a local port
691 * @lport: The local port
Robert Love1875f272009-11-03 11:47:50 -0800692 * @dev: The device associated with the SCSI host
Vasu Dev7f349142009-03-27 09:06:31 -0700693 *
694 * Must be called after fcoe_lport_config() and fcoe_netdev_config()
695 *
Robert Love1875f272009-11-03 11:47:50 -0800696 * Returns: 0 for success
Vasu Dev7f349142009-03-27 09:06:31 -0700697 */
Vasu Dev8ba00a42010-04-09 14:22:54 -0700698static int fcoe_shost_config(struct fc_lport *lport, struct device *dev)
Vasu Dev7f349142009-03-27 09:06:31 -0700699{
700 int rc = 0;
701
702 /* lport scsi host config */
Robert Love1875f272009-11-03 11:47:50 -0800703 lport->host->max_lun = FCOE_MAX_LUN;
704 lport->host->max_id = FCOE_MAX_FCP_TARGET;
705 lport->host->max_channel = 0;
Vasu Devda87bfa2010-04-09 14:22:59 -0700706 lport->host->max_cmd_len = FCOE_MAX_CMD_LEN;
707
Robert Love1875f272009-11-03 11:47:50 -0800708 if (lport->vport)
Yi Zou8ca86f82011-01-28 16:05:11 -0800709 lport->host->transportt = fcoe_vport_scsi_transport;
Chris Leeche9084bb2009-11-03 11:46:34 -0800710 else
Yi Zou8ca86f82011-01-28 16:05:11 -0800711 lport->host->transportt = fcoe_nport_scsi_transport;
Vasu Dev7f349142009-03-27 09:06:31 -0700712
713 /* add the new host to the SCSI-ml */
Robert Love1875f272009-11-03 11:47:50 -0800714 rc = scsi_add_host(lport->host, dev);
Vasu Dev7f349142009-03-27 09:06:31 -0700715 if (rc) {
Robert Love1875f272009-11-03 11:47:50 -0800716 FCOE_NETDEV_DBG(fcoe_netdev(lport), "fcoe_shost_config: "
Robert Loved5488eb2009-06-10 15:30:59 -0700717 "error on scsi_add_host\n");
Vasu Dev7f349142009-03-27 09:06:31 -0700718 return rc;
719 }
Chris Leech9a057532009-11-03 11:46:40 -0800720
Robert Love1875f272009-11-03 11:47:50 -0800721 if (!lport->vport)
Alexey Dobriyan4be929b2010-05-24 14:33:03 -0700722 fc_host_max_npiv_vports(lport->host) = USHRT_MAX;
Chris Leech9a057532009-11-03 11:46:40 -0800723
Robert Love1875f272009-11-03 11:47:50 -0800724 snprintf(fc_host_symbolic_name(lport->host), FC_SYMBOLIC_NAME_SIZE,
Chris Leech5baa17c2009-11-03 11:46:56 -0800725 "%s v%s over %s", FCOE_NAME, FCOE_VERSION,
Robert Love1875f272009-11-03 11:47:50 -0800726 fcoe_netdev(lport)->name);
Vasu Dev7f349142009-03-27 09:06:31 -0700727
728 return 0;
729}
730
Robert Love1875f272009-11-03 11:47:50 -0800731/**
732 * fcoe_oem_match() - The match routine for the offloaded exchange manager
733 * @fp: The I/O frame
Vasu Devd7179682009-07-29 17:05:21 -0700734 *
Robert Love1875f272009-11-03 11:47:50 -0800735 * This routine will be associated with an exchange manager (EM). When
736 * the libfc exchange handling code is looking for an EM to use it will
737 * call this routine and pass it the frame that it wishes to send. This
738 * routine will return True if the associated EM is to be used and False
739 * if the echange code should continue looking for an EM.
740 *
741 * The offload EM that this routine is associated with will handle any
742 * packets that are for SCSI read requests.
743 *
744 * Returns: True for read types I/O, otherwise returns false.
Vasu Devd7179682009-07-29 17:05:21 -0700745 */
746bool fcoe_oem_match(struct fc_frame *fp)
747{
Yi Zou05cc7392009-08-25 13:59:03 -0700748 return fc_fcp_is_read(fr_fsp(fp)) &&
749 (fr_fsp(fp)->data_len > fcoe_ddp_min);
Vasu Devd7179682009-07-29 17:05:21 -0700750}
751
Vasu Dev7f349142009-03-27 09:06:31 -0700752/**
Robert Love1875f272009-11-03 11:47:50 -0800753 * fcoe_em_config() - Allocate and configure an exchange manager
754 * @lport: The local port that the new EM will be associated with
Vasu Dev7f349142009-03-27 09:06:31 -0700755 *
Robert Love1875f272009-11-03 11:47:50 -0800756 * Returns: 0 on success
Vasu Dev7f349142009-03-27 09:06:31 -0700757 */
Robert Love1875f272009-11-03 11:47:50 -0800758static inline int fcoe_em_config(struct fc_lport *lport)
Vasu Dev7f349142009-03-27 09:06:31 -0700759{
Robert Love1875f272009-11-03 11:47:50 -0800760 struct fcoe_port *port = lport_priv(lport);
Chris Leech25024982009-08-25 13:59:35 -0700761 struct fcoe_interface *fcoe = port->fcoe;
762 struct fcoe_interface *oldfcoe = NULL;
Vasu Dev1d1b88d2009-07-29 17:05:45 -0700763 struct net_device *old_real_dev, *cur_real_dev;
Vasu Devd7179682009-07-29 17:05:21 -0700764 u16 min_xid = FCOE_MIN_XID;
765 u16 max_xid = FCOE_MAX_XID;
766
767 /*
768 * Check if need to allocate an em instance for
769 * offload exchange ids to be shared across all VN_PORTs/lport.
770 */
Robert Love1875f272009-11-03 11:47:50 -0800771 if (!lport->lro_enabled || !lport->lro_xid ||
772 (lport->lro_xid >= max_xid)) {
773 lport->lro_xid = 0;
Vasu Devd7179682009-07-29 17:05:21 -0700774 goto skip_oem;
775 }
776
777 /*
778 * Reuse existing offload em instance in case
Vasu Dev1d1b88d2009-07-29 17:05:45 -0700779 * it is already allocated on real eth device
Vasu Devd7179682009-07-29 17:05:21 -0700780 */
Chris Leech25024982009-08-25 13:59:35 -0700781 if (fcoe->netdev->priv_flags & IFF_802_1Q_VLAN)
782 cur_real_dev = vlan_dev_real_dev(fcoe->netdev);
Vasu Dev1d1b88d2009-07-29 17:05:45 -0700783 else
Chris Leech25024982009-08-25 13:59:35 -0700784 cur_real_dev = fcoe->netdev;
Vasu Dev1d1b88d2009-07-29 17:05:45 -0700785
Chris Leech25024982009-08-25 13:59:35 -0700786 list_for_each_entry(oldfcoe, &fcoe_hostlist, list) {
Chris Leech25024982009-08-25 13:59:35 -0700787 if (oldfcoe->netdev->priv_flags & IFF_802_1Q_VLAN)
788 old_real_dev = vlan_dev_real_dev(oldfcoe->netdev);
Vasu Dev1d1b88d2009-07-29 17:05:45 -0700789 else
Chris Leech25024982009-08-25 13:59:35 -0700790 old_real_dev = oldfcoe->netdev;
Vasu Dev1d1b88d2009-07-29 17:05:45 -0700791
792 if (cur_real_dev == old_real_dev) {
Chris Leech991cbb62009-08-25 13:59:51 -0700793 fcoe->oem = oldfcoe->oem;
Vasu Devd7179682009-07-29 17:05:21 -0700794 break;
795 }
796 }
797
Chris Leech991cbb62009-08-25 13:59:51 -0700798 if (fcoe->oem) {
Robert Love1875f272009-11-03 11:47:50 -0800799 if (!fc_exch_mgr_add(lport, fcoe->oem, fcoe_oem_match)) {
Vasu Devd7179682009-07-29 17:05:21 -0700800 printk(KERN_ERR "fcoe_em_config: failed to add "
801 "offload em:%p on interface:%s\n",
Chris Leech991cbb62009-08-25 13:59:51 -0700802 fcoe->oem, fcoe->netdev->name);
Vasu Devd7179682009-07-29 17:05:21 -0700803 return -ENOMEM;
804 }
805 } else {
Robert Love1875f272009-11-03 11:47:50 -0800806 fcoe->oem = fc_exch_mgr_alloc(lport, FC_CLASS_3,
807 FCOE_MIN_XID, lport->lro_xid,
808 fcoe_oem_match);
Chris Leech991cbb62009-08-25 13:59:51 -0700809 if (!fcoe->oem) {
Vasu Devd7179682009-07-29 17:05:21 -0700810 printk(KERN_ERR "fcoe_em_config: failed to allocate "
811 "em for offload exches on interface:%s\n",
Chris Leech25024982009-08-25 13:59:35 -0700812 fcoe->netdev->name);
Vasu Devd7179682009-07-29 17:05:21 -0700813 return -ENOMEM;
814 }
815 }
816
817 /*
818 * Exclude offload EM xid range from next EM xid range.
819 */
Robert Love1875f272009-11-03 11:47:50 -0800820 min_xid += lport->lro_xid + 1;
Vasu Devd7179682009-07-29 17:05:21 -0700821
822skip_oem:
Robert Love1875f272009-11-03 11:47:50 -0800823 if (!fc_exch_mgr_alloc(lport, FC_CLASS_3, min_xid, max_xid, NULL)) {
Vasu Devd7179682009-07-29 17:05:21 -0700824 printk(KERN_ERR "fcoe_em_config: failed to "
Chris Leech25024982009-08-25 13:59:35 -0700825 "allocate em on interface %s\n", fcoe->netdev->name);
Vasu Dev7f349142009-03-27 09:06:31 -0700826 return -ENOMEM;
Vasu Devd7179682009-07-29 17:05:21 -0700827 }
Vasu Dev7f349142009-03-27 09:06:31 -0700828
829 return 0;
830}
831
832/**
Robert Love1875f272009-11-03 11:47:50 -0800833 * fcoe_if_destroy() - Tear down a SW FCoE instance
834 * @lport: The local port to be destroyed
Vasu Dev34ce27b2010-05-07 15:18:46 -0700835 *
836 * Locking: must be called with the RTNL mutex held and RTNL mutex
837 * needed to be dropped by this function since not dropping RTNL
838 * would cause circular locking warning on synchronous fip worker
839 * cancelling thru fcoe_interface_put invoked by this function.
840 *
Vasu Dev7f349142009-03-27 09:06:31 -0700841 */
Chris Leechaf7f85d2009-08-25 13:59:24 -0700842static void fcoe_if_destroy(struct fc_lport *lport)
Vasu Dev7f349142009-03-27 09:06:31 -0700843{
Chris Leech014f5c32009-08-25 13:59:30 -0700844 struct fcoe_port *port = lport_priv(lport);
845 struct fcoe_interface *fcoe = port->fcoe;
Chris Leech25024982009-08-25 13:59:35 -0700846 struct net_device *netdev = fcoe->netdev;
Vasu Dev7f349142009-03-27 09:06:31 -0700847
Robert Loved5488eb2009-06-10 15:30:59 -0700848 FCOE_NETDEV_DBG(netdev, "Destroying interface\n");
Vasu Dev7f349142009-03-27 09:06:31 -0700849
Vasu Dev7f349142009-03-27 09:06:31 -0700850 /* Logout of the fabric */
Chris Leechaf7f85d2009-08-25 13:59:24 -0700851 fc_fabric_logoff(lport);
Vasu Dev7f349142009-03-27 09:06:31 -0700852
Vasu Dev7f349142009-03-27 09:06:31 -0700853 /* Cleanup the fc_lport */
Chris Leechaf7f85d2009-08-25 13:59:24 -0700854 fc_lport_destroy(lport);
Vasu Dev7f349142009-03-27 09:06:31 -0700855
Chris Leech54b649f2009-08-25 14:00:07 -0700856 /* Stop the transmit retry timer */
857 del_timer_sync(&port->timer);
858
859 /* Free existing transmit skbs */
860 fcoe_clean_pending_queue(lport);
861
Chris Leech11b56182009-11-03 11:46:29 -0800862 if (!is_zero_ether_addr(port->data_src_addr))
Jiri Pirkoa748ee22010-04-01 21:22:09 +0000863 dev_uc_del(netdev, port->data_src_addr);
Chris Leech11b56182009-11-03 11:46:29 -0800864 rtnl_unlock();
865
Chris Leech54b649f2009-08-25 14:00:07 -0700866 /* receives may not be stopped until after this */
867 fcoe_interface_put(fcoe);
868
869 /* Free queued packets for the per-CPU receive threads */
870 fcoe_percpu_clean(lport);
871
Vasu Dev7f349142009-03-27 09:06:31 -0700872 /* Detach from the scsi-ml */
Chris Leechaf7f85d2009-08-25 13:59:24 -0700873 fc_remove_host(lport->host);
874 scsi_remove_host(lport->host);
Vasu Dev7f349142009-03-27 09:06:31 -0700875
Yi Zou80e736f2010-11-30 16:18:07 -0800876 /* Destroy lport scsi_priv */
877 fc_fcp_destroy(lport);
878
Vasu Dev7f349142009-03-27 09:06:31 -0700879 /* There are no more rports or I/O, free the EM */
Chris Leechaf7f85d2009-08-25 13:59:24 -0700880 fc_exch_mgr_free(lport);
Vasu Dev7f349142009-03-27 09:06:31 -0700881
Vasu Dev7f349142009-03-27 09:06:31 -0700882 /* Free memory used by statistical counters */
Chris Leechaf7f85d2009-08-25 13:59:24 -0700883 fc_lport_free_stats(lport);
Vasu Dev7f349142009-03-27 09:06:31 -0700884
Chris Leech2e70e242009-08-25 14:00:23 -0700885 /* Release the Scsi_Host */
Chris Leechaf7f85d2009-08-25 13:59:24 -0700886 scsi_host_put(lport->host);
Vasu Dev7f349142009-03-27 09:06:31 -0700887}
888
Robert Love1875f272009-11-03 11:47:50 -0800889/**
890 * fcoe_ddp_setup() - Call a LLD's ddp_setup through the net device
891 * @lport: The local port to setup DDP for
892 * @xid: The exchange ID for this DDP transfer
893 * @sgl: The scatterlist describing this transfer
894 * @sgc: The number of sg items
Vasu Dev7f349142009-03-27 09:06:31 -0700895 *
Robert Love1875f272009-11-03 11:47:50 -0800896 * Returns: 0 if the DDP context was not configured
Vasu Dev7f349142009-03-27 09:06:31 -0700897 */
Robert Love1875f272009-11-03 11:47:50 -0800898static int fcoe_ddp_setup(struct fc_lport *lport, u16 xid,
899 struct scatterlist *sgl, unsigned int sgc)
Vasu Dev7f349142009-03-27 09:06:31 -0700900{
Robert Love1875f272009-11-03 11:47:50 -0800901 struct net_device *netdev = fcoe_netdev(lport);
Vasu Dev7f349142009-03-27 09:06:31 -0700902
Robert Love1875f272009-11-03 11:47:50 -0800903 if (netdev->netdev_ops->ndo_fcoe_ddp_setup)
904 return netdev->netdev_ops->ndo_fcoe_ddp_setup(netdev,
905 xid, sgl,
906 sgc);
Vasu Dev7f349142009-03-27 09:06:31 -0700907
908 return 0;
909}
910
Vasu Dev7f349142009-03-27 09:06:31 -0700911/**
Robert Love1875f272009-11-03 11:47:50 -0800912 * fcoe_ddp_done() - Call a LLD's ddp_done through the net device
913 * @lport: The local port to complete DDP on
914 * @xid: The exchange ID for this DDP transfer
Vasu Dev7f349142009-03-27 09:06:31 -0700915 *
Robert Love1875f272009-11-03 11:47:50 -0800916 * Returns: the length of data that have been completed by DDP
917 */
918static int fcoe_ddp_done(struct fc_lport *lport, u16 xid)
919{
920 struct net_device *netdev = fcoe_netdev(lport);
921
922 if (netdev->netdev_ops->ndo_fcoe_ddp_done)
923 return netdev->netdev_ops->ndo_fcoe_ddp_done(netdev, xid);
924 return 0;
925}
926
927/**
928 * fcoe_if_create() - Create a FCoE instance on an interface
929 * @fcoe: The FCoE interface to create a local port on
930 * @parent: The device pointer to be the parent in sysfs for the SCSI host
931 * @npiv: Indicates if the port is a vport or not
Vasu Dev7f349142009-03-27 09:06:31 -0700932 *
Robert Love1875f272009-11-03 11:47:50 -0800933 * Creates a fc_lport instance and a Scsi_Host instance and configure them.
934 *
935 * Returns: The allocated fc_lport or an error pointer
Vasu Dev7f349142009-03-27 09:06:31 -0700936 */
Chris Leech030f4e02009-08-25 14:00:02 -0700937static struct fc_lport *fcoe_if_create(struct fcoe_interface *fcoe,
Chris Leech9a057532009-11-03 11:46:40 -0800938 struct device *parent, int npiv)
Vasu Dev7f349142009-03-27 09:06:31 -0700939{
Robert Love1875f272009-11-03 11:47:50 -0800940 struct net_device *netdev = fcoe->netdev;
Chris Leechaf7f85d2009-08-25 13:59:24 -0700941 struct fc_lport *lport = NULL;
Chris Leech014f5c32009-08-25 13:59:30 -0700942 struct fcoe_port *port;
Robert Love1875f272009-11-03 11:47:50 -0800943 int rc;
Chris Leech9a057532009-11-03 11:46:40 -0800944 /*
945 * parent is only a vport if npiv is 1,
946 * but we'll only use vport in that case so go ahead and set it
947 */
948 struct fc_vport *vport = dev_to_vport(parent);
Vasu Dev7f349142009-03-27 09:06:31 -0700949
Robert Loved5488eb2009-06-10 15:30:59 -0700950 FCOE_NETDEV_DBG(netdev, "Create Interface\n");
Vasu Dev7f349142009-03-27 09:06:31 -0700951
Chris Leech9a057532009-11-03 11:46:40 -0800952 if (!npiv) {
953 lport = libfc_host_alloc(&fcoe_shost_template,
954 sizeof(struct fcoe_port));
955 } else {
956 lport = libfc_vport_create(vport,
957 sizeof(struct fcoe_port));
958 }
Chris Leech86221962009-11-03 11:46:08 -0800959 if (!lport) {
Chris Leech014f5c32009-08-25 13:59:30 -0700960 FCOE_NETDEV_DBG(netdev, "Could not allocate host structure\n");
961 rc = -ENOMEM;
Chris Leech030f4e02009-08-25 14:00:02 -0700962 goto out;
Chris Leech014f5c32009-08-25 13:59:30 -0700963 }
Chris Leech014f5c32009-08-25 13:59:30 -0700964 port = lport_priv(lport);
Chris Leech2e70e242009-08-25 14:00:23 -0700965 port->lport = lport;
Chris Leech014f5c32009-08-25 13:59:30 -0700966 port->fcoe = fcoe;
Chris Leech2e70e242009-08-25 14:00:23 -0700967 INIT_WORK(&port->destroy_work, fcoe_destroy_work);
Vasu Dev7f349142009-03-27 09:06:31 -0700968
Robert Love1875f272009-11-03 11:47:50 -0800969 /* configure a fc_lport including the exchange manager */
Chris Leechaf7f85d2009-08-25 13:59:24 -0700970 rc = fcoe_lport_config(lport);
Vasu Dev7f349142009-03-27 09:06:31 -0700971 if (rc) {
Robert Loved5488eb2009-06-10 15:30:59 -0700972 FCOE_NETDEV_DBG(netdev, "Could not configure lport for the "
973 "interface\n");
Vasu Dev7f349142009-03-27 09:06:31 -0700974 goto out_host_put;
975 }
976
Chris Leech9a057532009-11-03 11:46:40 -0800977 if (npiv) {
Chris Leech9f8f3aa2010-04-09 14:23:16 -0700978 FCOE_NETDEV_DBG(netdev, "Setting vport names, "
979 "%16.16llx %16.16llx\n",
Robert Love1875f272009-11-03 11:47:50 -0800980 vport->node_name, vport->port_name);
Chris Leech9a057532009-11-03 11:46:40 -0800981 fc_set_wwnn(lport, vport->node_name);
982 fc_set_wwpn(lport, vport->port_name);
983 }
984
Vasu Devab6b85c2009-05-17 12:33:08 +0000985 /* configure lport network properties */
Chris Leechaf7f85d2009-08-25 13:59:24 -0700986 rc = fcoe_netdev_config(lport, netdev);
Vasu Devab6b85c2009-05-17 12:33:08 +0000987 if (rc) {
Robert Loved5488eb2009-06-10 15:30:59 -0700988 FCOE_NETDEV_DBG(netdev, "Could not configure netdev for the "
989 "interface\n");
Chris Leech54b649f2009-08-25 14:00:07 -0700990 goto out_lp_destroy;
Vasu Devab6b85c2009-05-17 12:33:08 +0000991 }
Joe Eykholt97c83892009-03-17 11:42:40 -0700992
Vasu Dev7f349142009-03-27 09:06:31 -0700993 /* configure lport scsi host properties */
Vasu Dev8ba00a42010-04-09 14:22:54 -0700994 rc = fcoe_shost_config(lport, parent);
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 shost for the "
997 "interface\n");
Chris Leech54b649f2009-08-25 14:00:07 -0700998 goto out_lp_destroy;
Vasu Dev7f349142009-03-27 09:06:31 -0700999 }
1000
Vasu Dev7f349142009-03-27 09:06:31 -07001001 /* Initialize the library */
Joe Eykholte10f8c62010-07-20 15:20:30 -07001002 rc = fcoe_libfc_config(lport, &fcoe->ctlr, &fcoe_libfc_fcn_templ, 1);
Vasu Dev7f349142009-03-27 09:06:31 -07001003 if (rc) {
Robert Loved5488eb2009-06-10 15:30:59 -07001004 FCOE_NETDEV_DBG(netdev, "Could not configure libfc for the "
1005 "interface\n");
Vasu Dev7f349142009-03-27 09:06:31 -07001006 goto out_lp_destroy;
1007 }
1008
Chris Leech9a057532009-11-03 11:46:40 -08001009 if (!npiv) {
1010 /*
1011 * fcoe_em_alloc() and fcoe_hostlist_add() both
1012 * need to be atomic with respect to other changes to the
1013 * hostlist since fcoe_em_alloc() looks for an existing EM
1014 * instance on host list updated by fcoe_hostlist_add().
1015 *
1016 * This is currently handled through the fcoe_config_mutex
1017 * begin held.
1018 */
Chris Leechc863df32009-08-25 14:00:18 -07001019
Chris Leech9a057532009-11-03 11:46:40 -08001020 /* lport exch manager allocation */
1021 rc = fcoe_em_config(lport);
1022 if (rc) {
1023 FCOE_NETDEV_DBG(netdev, "Could not configure the EM "
Robert Love1875f272009-11-03 11:47:50 -08001024 "for the interface\n");
Chris Leech9a057532009-11-03 11:46:40 -08001025 goto out_lp_destroy;
1026 }
Vasu Dev96316092009-07-29 17:05:00 -07001027 }
1028
Chris Leech030f4e02009-08-25 14:00:02 -07001029 fcoe_interface_get(fcoe);
Chris Leechaf7f85d2009-08-25 13:59:24 -07001030 return lport;
Vasu Dev7f349142009-03-27 09:06:31 -07001031
1032out_lp_destroy:
Chris Leechaf7f85d2009-08-25 13:59:24 -07001033 fc_exch_mgr_free(lport);
Vasu Dev7f349142009-03-27 09:06:31 -07001034out_host_put:
Chris Leechaf7f85d2009-08-25 13:59:24 -07001035 scsi_host_put(lport->host);
1036out:
1037 return ERR_PTR(rc);
Vasu Dev7f349142009-03-27 09:06:31 -07001038}
1039
1040/**
Robert Love1875f272009-11-03 11:47:50 -08001041 * fcoe_if_init() - Initialization routine for fcoe.ko
Vasu Dev7f349142009-03-27 09:06:31 -07001042 *
Robert Love1875f272009-11-03 11:47:50 -08001043 * Attaches the SW FCoE transport to the FC transport
1044 *
1045 * Returns: 0 on success
Vasu Dev7f349142009-03-27 09:06:31 -07001046 */
1047static int __init fcoe_if_init(void)
1048{
1049 /* attach to scsi transport */
Yi Zou8ca86f82011-01-28 16:05:11 -08001050 fcoe_nport_scsi_transport =
1051 fc_attach_transport(&fcoe_nport_fc_functions);
1052 fcoe_vport_scsi_transport =
1053 fc_attach_transport(&fcoe_vport_fc_functions);
Vasu Dev7f349142009-03-27 09:06:31 -07001054
Yi Zou8ca86f82011-01-28 16:05:11 -08001055 if (!fcoe_nport_scsi_transport) {
Robert Loved5488eb2009-06-10 15:30:59 -07001056 printk(KERN_ERR "fcoe: Failed to attach to the FC transport\n");
Vasu Dev7f349142009-03-27 09:06:31 -07001057 return -ENODEV;
1058 }
1059
1060 return 0;
1061}
1062
1063/**
Robert Love1875f272009-11-03 11:47:50 -08001064 * fcoe_if_exit() - Tear down fcoe.ko
Vasu Dev7f349142009-03-27 09:06:31 -07001065 *
Robert Love1875f272009-11-03 11:47:50 -08001066 * Detaches the SW FCoE transport from the FC transport
1067 *
1068 * Returns: 0 on success
Vasu Dev7f349142009-03-27 09:06:31 -07001069 */
1070int __exit fcoe_if_exit(void)
1071{
Yi Zou8ca86f82011-01-28 16:05:11 -08001072 fc_release_transport(fcoe_nport_scsi_transport);
1073 fc_release_transport(fcoe_vport_scsi_transport);
1074 fcoe_nport_scsi_transport = NULL;
1075 fcoe_vport_scsi_transport = NULL;
Vasu Dev7f349142009-03-27 09:06:31 -07001076 return 0;
1077}
1078
Robert Love85b4aa42008-12-09 15:10:24 -08001079/**
Robert Love1875f272009-11-03 11:47:50 -08001080 * fcoe_percpu_thread_create() - Create a receive thread for an online CPU
1081 * @cpu: The CPU index of the CPU to create a receive thread for
Robert Love8976f422009-03-17 11:41:46 -07001082 */
1083static void fcoe_percpu_thread_create(unsigned int cpu)
1084{
1085 struct fcoe_percpu_s *p;
1086 struct task_struct *thread;
1087
1088 p = &per_cpu(fcoe_percpu, cpu);
1089
1090 thread = kthread_create(fcoe_percpu_receive_thread,
1091 (void *)p, "fcoethread/%d", cpu);
1092
Joe Eykholte7a51992009-08-25 14:04:08 -07001093 if (likely(!IS_ERR(thread))) {
Robert Love8976f422009-03-17 11:41:46 -07001094 kthread_bind(thread, cpu);
1095 wake_up_process(thread);
1096
1097 spin_lock_bh(&p->fcoe_rx_list.lock);
1098 p->thread = thread;
1099 spin_unlock_bh(&p->fcoe_rx_list.lock);
1100 }
1101}
1102
1103/**
Robert Love1875f272009-11-03 11:47:50 -08001104 * fcoe_percpu_thread_destroy() - Remove the receive thread of a CPU
1105 * @cpu: The CPU index of the CPU whose receive thread is to be destroyed
Robert Love8976f422009-03-17 11:41:46 -07001106 *
1107 * Destroys a per-CPU Rx thread. Any pending skbs are moved to the
1108 * current CPU's Rx thread. If the thread being destroyed is bound to
1109 * the CPU processing this context the skbs will be freed.
1110 */
1111static void fcoe_percpu_thread_destroy(unsigned int cpu)
1112{
1113 struct fcoe_percpu_s *p;
1114 struct task_struct *thread;
1115 struct page *crc_eof;
1116 struct sk_buff *skb;
1117#ifdef CONFIG_SMP
1118 struct fcoe_percpu_s *p0;
Joe Eykholtf018b732010-03-12 16:08:55 -08001119 unsigned targ_cpu = get_cpu();
Robert Love8976f422009-03-17 11:41:46 -07001120#endif /* CONFIG_SMP */
1121
Robert Loved5488eb2009-06-10 15:30:59 -07001122 FCOE_DBG("Destroying receive thread for CPU %d\n", cpu);
Robert Love8976f422009-03-17 11:41:46 -07001123
1124 /* Prevent any new skbs from being queued for this CPU. */
1125 p = &per_cpu(fcoe_percpu, cpu);
1126 spin_lock_bh(&p->fcoe_rx_list.lock);
1127 thread = p->thread;
1128 p->thread = NULL;
1129 crc_eof = p->crc_eof_page;
1130 p->crc_eof_page = NULL;
1131 p->crc_eof_offset = 0;
1132 spin_unlock_bh(&p->fcoe_rx_list.lock);
1133
1134#ifdef CONFIG_SMP
1135 /*
1136 * Don't bother moving the skb's if this context is running
1137 * on the same CPU that is having its thread destroyed. This
1138 * can easily happen when the module is removed.
1139 */
1140 if (cpu != targ_cpu) {
1141 p0 = &per_cpu(fcoe_percpu, targ_cpu);
1142 spin_lock_bh(&p0->fcoe_rx_list.lock);
1143 if (p0->thread) {
Robert Loved5488eb2009-06-10 15:30:59 -07001144 FCOE_DBG("Moving frames from CPU %d to CPU %d\n",
1145 cpu, targ_cpu);
Robert Love8976f422009-03-17 11:41:46 -07001146
1147 while ((skb = __skb_dequeue(&p->fcoe_rx_list)) != NULL)
1148 __skb_queue_tail(&p0->fcoe_rx_list, skb);
1149 spin_unlock_bh(&p0->fcoe_rx_list.lock);
1150 } else {
1151 /*
1152 * The targeted CPU is not initialized and cannot accept
Robert Love1875f272009-11-03 11:47:50 -08001153 * new skbs. Unlock the targeted CPU and drop the skbs
Robert Love8976f422009-03-17 11:41:46 -07001154 * on the CPU that is going offline.
1155 */
1156 while ((skb = __skb_dequeue(&p->fcoe_rx_list)) != NULL)
1157 kfree_skb(skb);
1158 spin_unlock_bh(&p0->fcoe_rx_list.lock);
1159 }
1160 } else {
1161 /*
1162 * This scenario occurs when the module is being removed
1163 * and all threads are being destroyed. skbs will continue
1164 * to be shifted from the CPU thread that is being removed
1165 * to the CPU thread associated with the CPU that is processing
1166 * the module removal. Once there is only one CPU Rx thread it
1167 * will reach this case and we will drop all skbs and later
1168 * stop the thread.
1169 */
1170 spin_lock_bh(&p->fcoe_rx_list.lock);
1171 while ((skb = __skb_dequeue(&p->fcoe_rx_list)) != NULL)
1172 kfree_skb(skb);
1173 spin_unlock_bh(&p->fcoe_rx_list.lock);
1174 }
Joe Eykholtf018b732010-03-12 16:08:55 -08001175 put_cpu();
Robert Love8976f422009-03-17 11:41:46 -07001176#else
1177 /*
Chris Leechdd3fd722009-04-21 16:27:36 -07001178 * This a non-SMP scenario where the singular Rx thread is
Robert Love8976f422009-03-17 11:41:46 -07001179 * being removed. Free all skbs and stop the thread.
1180 */
1181 spin_lock_bh(&p->fcoe_rx_list.lock);
1182 while ((skb = __skb_dequeue(&p->fcoe_rx_list)) != NULL)
1183 kfree_skb(skb);
1184 spin_unlock_bh(&p->fcoe_rx_list.lock);
1185#endif
1186
1187 if (thread)
1188 kthread_stop(thread);
1189
1190 if (crc_eof)
1191 put_page(crc_eof);
1192}
1193
1194/**
Robert Love1875f272009-11-03 11:47:50 -08001195 * fcoe_cpu_callback() - Handler for CPU hotplug events
1196 * @nfb: The callback data block
1197 * @action: The event triggering the callback
1198 * @hcpu: The index of the CPU that the event is for
Robert Love8976f422009-03-17 11:41:46 -07001199 *
Robert Love1875f272009-11-03 11:47:50 -08001200 * This creates or destroys per-CPU data for fcoe
Robert Love8976f422009-03-17 11:41:46 -07001201 *
1202 * Returns NOTIFY_OK always.
1203 */
1204static int fcoe_cpu_callback(struct notifier_block *nfb,
1205 unsigned long action, void *hcpu)
1206{
1207 unsigned cpu = (unsigned long)hcpu;
1208
1209 switch (action) {
1210 case CPU_ONLINE:
1211 case CPU_ONLINE_FROZEN:
Robert Loved5488eb2009-06-10 15:30:59 -07001212 FCOE_DBG("CPU %x online: Create Rx thread\n", cpu);
Robert Love8976f422009-03-17 11:41:46 -07001213 fcoe_percpu_thread_create(cpu);
1214 break;
1215 case CPU_DEAD:
1216 case CPU_DEAD_FROZEN:
Robert Loved5488eb2009-06-10 15:30:59 -07001217 FCOE_DBG("CPU %x offline: Remove Rx thread\n", cpu);
Robert Love8976f422009-03-17 11:41:46 -07001218 fcoe_percpu_thread_destroy(cpu);
1219 break;
1220 default:
1221 break;
1222 }
1223 return NOTIFY_OK;
1224}
1225
Robert Love8976f422009-03-17 11:41:46 -07001226/**
Robert Love1875f272009-11-03 11:47:50 -08001227 * fcoe_rcv() - Receive packets from a net device
1228 * @skb: The received packet
1229 * @netdev: The net device that the packet was received on
1230 * @ptype: The packet type context
1231 * @olddev: The last device net device
Robert Love85b4aa42008-12-09 15:10:24 -08001232 *
Robert Love1875f272009-11-03 11:47:50 -08001233 * This routine is called by NET_RX_SOFTIRQ. It receives a packet, builds a
1234 * FC frame and passes the frame to libfc.
Robert Love85b4aa42008-12-09 15:10:24 -08001235 *
1236 * Returns: 0 for success
Robert Love34f42a02009-02-27 10:55:45 -08001237 */
Robert Love1875f272009-11-03 11:47:50 -08001238int fcoe_rcv(struct sk_buff *skb, struct net_device *netdev,
Robert Love85b4aa42008-12-09 15:10:24 -08001239 struct packet_type *ptype, struct net_device *olddev)
1240{
Robert Love1875f272009-11-03 11:47:50 -08001241 struct fc_lport *lport;
Robert Love85b4aa42008-12-09 15:10:24 -08001242 struct fcoe_rcv_info *fr;
Chris Leech259ad852009-08-25 13:59:41 -07001243 struct fcoe_interface *fcoe;
Robert Love85b4aa42008-12-09 15:10:24 -08001244 struct fc_frame_header *fh;
Robert Love85b4aa42008-12-09 15:10:24 -08001245 struct fcoe_percpu_s *fps;
Vasu Dev519e5132010-07-20 15:19:32 -07001246 struct ethhdr *eh;
Vasu Devb2f00912009-08-25 13:58:53 -07001247 unsigned int cpu;
Robert Love85b4aa42008-12-09 15:10:24 -08001248
Chris Leech259ad852009-08-25 13:59:41 -07001249 fcoe = container_of(ptype, struct fcoe_interface, fcoe_packet_type);
Robert Love1875f272009-11-03 11:47:50 -08001250 lport = fcoe->ctlr.lp;
1251 if (unlikely(!lport)) {
1252 FCOE_NETDEV_DBG(netdev, "Cannot find hba structure");
Robert Love85b4aa42008-12-09 15:10:24 -08001253 goto err2;
1254 }
Robert Love1875f272009-11-03 11:47:50 -08001255 if (!lport->link_up)
Joe Eykholt97c83892009-03-17 11:42:40 -07001256 goto err2;
Robert Love85b4aa42008-12-09 15:10:24 -08001257
Robert Love1875f272009-11-03 11:47:50 -08001258 FCOE_NETDEV_DBG(netdev, "skb_info: len:%d data_len:%d head:%p "
Robert Loved5488eb2009-06-10 15:30:59 -07001259 "data:%p tail:%p end:%p sum:%d dev:%s",
1260 skb->len, skb->data_len, skb->head, skb->data,
1261 skb_tail_pointer(skb), skb_end_pointer(skb),
1262 skb->csum, skb->dev ? skb->dev->name : "<NULL>");
Robert Love85b4aa42008-12-09 15:10:24 -08001263
Vasu Dev519e5132010-07-20 15:19:32 -07001264 eh = eth_hdr(skb);
Vasu Dev519e5132010-07-20 15:19:32 -07001265
1266 if (is_fip_mode(&fcoe->ctlr) &&
1267 compare_ether_addr(eh->h_source, fcoe->ctlr.dest_addr)) {
1268 FCOE_NETDEV_DBG(netdev, "wrong source mac address:%pM\n",
1269 eh->h_source);
Robert Love85b4aa42008-12-09 15:10:24 -08001270 goto err;
1271 }
1272
1273 /*
1274 * Check for minimum frame length, and make sure required FCoE
1275 * and FC headers are pulled into the linear data area.
1276 */
1277 if (unlikely((skb->len < FCOE_MIN_FRAME) ||
Robert Love1875f272009-11-03 11:47:50 -08001278 !pskb_may_pull(skb, FCOE_HEADER_LEN)))
Robert Love85b4aa42008-12-09 15:10:24 -08001279 goto err;
1280
1281 skb_set_transport_header(skb, sizeof(struct fcoe_hdr));
1282 fh = (struct fc_frame_header *) skb_transport_header(skb);
1283
Robert Love0ee31cb2010-10-08 17:12:46 -07001284 if (ntoh24(&eh->h_dest[3]) != ntoh24(fh->fh_d_id)) {
1285 FCOE_NETDEV_DBG(netdev, "FC frame d_id mismatch with MAC:%pM\n",
1286 eh->h_dest);
1287 goto err;
1288 }
1289
Robert Love85b4aa42008-12-09 15:10:24 -08001290 fr = fcoe_dev_from_skb(skb);
Robert Love1875f272009-11-03 11:47:50 -08001291 fr->fr_dev = lport;
Robert Love85b4aa42008-12-09 15:10:24 -08001292 fr->ptype = ptype;
Robert Love5e5e92d2009-03-17 11:41:35 -07001293
Robert Love85b4aa42008-12-09 15:10:24 -08001294 /*
Vasu Devb2f00912009-08-25 13:58:53 -07001295 * In case the incoming frame's exchange is originated from
1296 * the initiator, then received frame's exchange id is ANDed
1297 * with fc_cpu_mask bits to get the same cpu on which exchange
1298 * was originated, otherwise just use the current cpu.
Robert Love85b4aa42008-12-09 15:10:24 -08001299 */
Vasu Devb2f00912009-08-25 13:58:53 -07001300 if (ntoh24(fh->fh_f_ctl) & FC_FC_EX_CTX)
1301 cpu = ntohs(fh->fh_ox_id) & fc_cpu_mask;
1302 else
1303 cpu = smp_processor_id();
Robert Love5e5e92d2009-03-17 11:41:35 -07001304
Robert Love8976f422009-03-17 11:41:46 -07001305 fps = &per_cpu(fcoe_percpu, cpu);
Robert Love85b4aa42008-12-09 15:10:24 -08001306 spin_lock_bh(&fps->fcoe_rx_list.lock);
Robert Love8976f422009-03-17 11:41:46 -07001307 if (unlikely(!fps->thread)) {
1308 /*
1309 * The targeted CPU is not ready, let's target
1310 * the first CPU now. For non-SMP systems this
1311 * will check the same CPU twice.
1312 */
Robert Love1875f272009-11-03 11:47:50 -08001313 FCOE_NETDEV_DBG(netdev, "CPU is online, but no receive thread "
Robert Loved5488eb2009-06-10 15:30:59 -07001314 "ready for incoming skb- using first online "
1315 "CPU.\n");
Robert Love8976f422009-03-17 11:41:46 -07001316
1317 spin_unlock_bh(&fps->fcoe_rx_list.lock);
Rusty Russell69571772009-12-17 11:43:14 -06001318 cpu = cpumask_first(cpu_online_mask);
Robert Love8976f422009-03-17 11:41:46 -07001319 fps = &per_cpu(fcoe_percpu, cpu);
1320 spin_lock_bh(&fps->fcoe_rx_list.lock);
1321 if (!fps->thread) {
1322 spin_unlock_bh(&fps->fcoe_rx_list.lock);
1323 goto err;
1324 }
1325 }
1326
1327 /*
1328 * We now have a valid CPU that we're targeting for
1329 * this skb. We also have this receive thread locked,
1330 * so we're free to queue skbs into it's queue.
1331 */
Robert Love85b4aa42008-12-09 15:10:24 -08001332
Chris Leech859b7b62009-11-20 14:54:47 -08001333 /* If this is a SCSI-FCP frame, and this is already executing on the
1334 * correct CPU, and the queue for this CPU is empty, then go ahead
1335 * and process the frame directly in the softirq context.
1336 * This lets us process completions without context switching from the
1337 * NET_RX softirq, to our receive processing thread, and then back to
1338 * BLOCK softirq context.
1339 */
1340 if (fh->fh_type == FC_TYPE_FCP &&
1341 cpu == smp_processor_id() &&
1342 skb_queue_empty(&fps->fcoe_rx_list)) {
1343 spin_unlock_bh(&fps->fcoe_rx_list.lock);
1344 fcoe_recv_frame(skb);
1345 } else {
1346 __skb_queue_tail(&fps->fcoe_rx_list, skb);
1347 if (fps->fcoe_rx_list.qlen == 1)
1348 wake_up_process(fps->thread);
1349 spin_unlock_bh(&fps->fcoe_rx_list.lock);
1350 }
Robert Love85b4aa42008-12-09 15:10:24 -08001351
1352 return 0;
1353err:
Joe Eykholtf018b732010-03-12 16:08:55 -08001354 per_cpu_ptr(lport->dev_stats, get_cpu())->ErrorFrames++;
1355 put_cpu();
Robert Love85b4aa42008-12-09 15:10:24 -08001356err2:
1357 kfree_skb(skb);
1358 return -1;
1359}
Robert Love85b4aa42008-12-09 15:10:24 -08001360
1361/**
Robert Love1875f272009-11-03 11:47:50 -08001362 * fcoe_start_io() - Start FCoE I/O
1363 * @skb: The packet to be transmitted
1364 *
1365 * This routine is called from the net device to start transmitting
1366 * FCoE packets.
Robert Love85b4aa42008-12-09 15:10:24 -08001367 *
1368 * Returns: 0 for success
Robert Love34f42a02009-02-27 10:55:45 -08001369 */
Robert Love85b4aa42008-12-09 15:10:24 -08001370static inline int fcoe_start_io(struct sk_buff *skb)
1371{
Chris Leech18fa11e2009-11-03 11:50:05 -08001372 struct sk_buff *nskb;
Robert Love85b4aa42008-12-09 15:10:24 -08001373 int rc;
1374
Chris Leech18fa11e2009-11-03 11:50:05 -08001375 nskb = skb_clone(skb, GFP_ATOMIC);
1376 rc = dev_queue_xmit(nskb);
Robert Love85b4aa42008-12-09 15:10:24 -08001377 if (rc != 0)
1378 return rc;
1379 kfree_skb(skb);
1380 return 0;
1381}
1382
1383/**
Robert Love1875f272009-11-03 11:47:50 -08001384 * fcoe_get_paged_crc_eof() - Allocate a page to be used for the trailer CRC
1385 * @skb: The packet to be transmitted
1386 * @tlen: The total length of the trailer
1387 *
1388 * This routine allocates a page for frame trailers. The page is re-used if
1389 * there is enough room left on it for the current trailer. If there isn't
1390 * enough buffer left a new page is allocated for the trailer. Reference to
1391 * the page from this function as well as the skbs using the page fragments
1392 * ensure that the page is freed at the appropriate time.
Robert Love85b4aa42008-12-09 15:10:24 -08001393 *
1394 * Returns: 0 for success
Robert Love34f42a02009-02-27 10:55:45 -08001395 */
Robert Love85b4aa42008-12-09 15:10:24 -08001396static int fcoe_get_paged_crc_eof(struct sk_buff *skb, int tlen)
1397{
1398 struct fcoe_percpu_s *fps;
1399 struct page *page;
Robert Love85b4aa42008-12-09 15:10:24 -08001400
Robert Love5e5e92d2009-03-17 11:41:35 -07001401 fps = &get_cpu_var(fcoe_percpu);
Robert Love85b4aa42008-12-09 15:10:24 -08001402 page = fps->crc_eof_page;
1403 if (!page) {
1404 page = alloc_page(GFP_ATOMIC);
1405 if (!page) {
Robert Love5e5e92d2009-03-17 11:41:35 -07001406 put_cpu_var(fcoe_percpu);
Robert Love85b4aa42008-12-09 15:10:24 -08001407 return -ENOMEM;
1408 }
1409 fps->crc_eof_page = page;
Robert Love8976f422009-03-17 11:41:46 -07001410 fps->crc_eof_offset = 0;
Robert Love85b4aa42008-12-09 15:10:24 -08001411 }
1412
1413 get_page(page);
1414 skb_fill_page_desc(skb, skb_shinfo(skb)->nr_frags, page,
1415 fps->crc_eof_offset, tlen);
1416 skb->len += tlen;
1417 skb->data_len += tlen;
1418 skb->truesize += tlen;
1419 fps->crc_eof_offset += sizeof(struct fcoe_crc_eof);
1420
1421 if (fps->crc_eof_offset >= PAGE_SIZE) {
1422 fps->crc_eof_page = NULL;
1423 fps->crc_eof_offset = 0;
1424 put_page(page);
1425 }
Robert Love5e5e92d2009-03-17 11:41:35 -07001426 put_cpu_var(fcoe_percpu);
Robert Love85b4aa42008-12-09 15:10:24 -08001427 return 0;
1428}
1429
1430/**
Robert Love1875f272009-11-03 11:47:50 -08001431 * fcoe_fc_crc() - Calculates the CRC for a given frame
1432 * @fp: The frame to be checksumed
Robert Love85b4aa42008-12-09 15:10:24 -08001433 *
Robert Love1875f272009-11-03 11:47:50 -08001434 * This uses crc32() routine to calculate the CRC for a frame
1435 *
1436 * Return: The 32 bit CRC value
Robert Love34f42a02009-02-27 10:55:45 -08001437 */
Robert Love85b4aa42008-12-09 15:10:24 -08001438u32 fcoe_fc_crc(struct fc_frame *fp)
1439{
1440 struct sk_buff *skb = fp_skb(fp);
1441 struct skb_frag_struct *frag;
1442 unsigned char *data;
1443 unsigned long off, len, clen;
1444 u32 crc;
1445 unsigned i;
1446
1447 crc = crc32(~0, skb->data, skb_headlen(skb));
1448
1449 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
1450 frag = &skb_shinfo(skb)->frags[i];
1451 off = frag->page_offset;
1452 len = frag->size;
1453 while (len > 0) {
1454 clen = min(len, PAGE_SIZE - (off & ~PAGE_MASK));
1455 data = kmap_atomic(frag->page + (off >> PAGE_SHIFT),
1456 KM_SKB_DATA_SOFTIRQ);
1457 crc = crc32(crc, data + (off & ~PAGE_MASK), clen);
1458 kunmap_atomic(data, KM_SKB_DATA_SOFTIRQ);
1459 off += clen;
1460 len -= clen;
1461 }
1462 }
1463 return crc;
1464}
Robert Love85b4aa42008-12-09 15:10:24 -08001465
1466/**
Robert Love1875f272009-11-03 11:47:50 -08001467 * fcoe_xmit() - Transmit a FCoE frame
1468 * @lport: The local port that the frame is to be transmitted for
1469 * @fp: The frame to be transmitted
Robert Love85b4aa42008-12-09 15:10:24 -08001470 *
Robert Love1875f272009-11-03 11:47:50 -08001471 * Return: 0 for success
Robert Love34f42a02009-02-27 10:55:45 -08001472 */
Robert Love1875f272009-11-03 11:47:50 -08001473int fcoe_xmit(struct fc_lport *lport, struct fc_frame *fp)
Robert Love85b4aa42008-12-09 15:10:24 -08001474{
Vasu Dev4bb6b512009-05-06 10:52:34 -07001475 int wlen;
Robert Love85b4aa42008-12-09 15:10:24 -08001476 u32 crc;
1477 struct ethhdr *eh;
1478 struct fcoe_crc_eof *cp;
1479 struct sk_buff *skb;
1480 struct fcoe_dev_stats *stats;
1481 struct fc_frame_header *fh;
1482 unsigned int hlen; /* header length implies the version */
1483 unsigned int tlen; /* trailer length */
1484 unsigned int elen; /* eth header, may include vlan */
Robert Love1875f272009-11-03 11:47:50 -08001485 struct fcoe_port *port = lport_priv(lport);
Chris Leech3fe9a0b2009-08-25 13:59:46 -07001486 struct fcoe_interface *fcoe = port->fcoe;
Robert Love85b4aa42008-12-09 15:10:24 -08001487 u8 sof, eof;
1488 struct fcoe_hdr *hp;
1489
1490 WARN_ON((fr_len(fp) % sizeof(u32)) != 0);
1491
Robert Love85b4aa42008-12-09 15:10:24 -08001492 fh = fc_frame_header_get(fp);
Joe Eykholt97c83892009-03-17 11:42:40 -07001493 skb = fp_skb(fp);
1494 wlen = skb->len / FCOE_WORD_TO_BYTE;
1495
Robert Love1875f272009-11-03 11:47:50 -08001496 if (!lport->link_up) {
Dan Carpenter3caf02e2009-04-21 16:27:25 -07001497 kfree_skb(skb);
Joe Eykholt97c83892009-03-17 11:42:40 -07001498 return 0;
Robert Love85b4aa42008-12-09 15:10:24 -08001499 }
1500
Joe Eykholt9860eeb2010-03-12 16:07:52 -08001501 if (unlikely(fh->fh_type == FC_TYPE_ELS) &&
Robert Love1875f272009-11-03 11:47:50 -08001502 fcoe_ctlr_els_send(&fcoe->ctlr, lport, skb))
Joe Eykholt97c83892009-03-17 11:42:40 -07001503 return 0;
1504
Robert Love85b4aa42008-12-09 15:10:24 -08001505 sof = fr_sof(fp);
1506 eof = fr_eof(fp);
1507
Vasu Dev4e57e1c2009-05-06 10:52:46 -07001508 elen = sizeof(struct ethhdr);
Robert Love85b4aa42008-12-09 15:10:24 -08001509 hlen = sizeof(struct fcoe_hdr);
1510 tlen = sizeof(struct fcoe_crc_eof);
1511 wlen = (skb->len - tlen + sizeof(crc)) / FCOE_WORD_TO_BYTE;
1512
1513 /* crc offload */
Robert Love1875f272009-11-03 11:47:50 -08001514 if (likely(lport->crc_offload)) {
Yi Zou39ca9a02009-02-27 14:07:15 -08001515 skb->ip_summed = CHECKSUM_PARTIAL;
Robert Love85b4aa42008-12-09 15:10:24 -08001516 skb->csum_start = skb_headroom(skb);
1517 skb->csum_offset = skb->len;
1518 crc = 0;
1519 } else {
1520 skb->ip_summed = CHECKSUM_NONE;
1521 crc = fcoe_fc_crc(fp);
1522 }
1523
Chris Leech014f5c32009-08-25 13:59:30 -07001524 /* copy port crc and eof to the skb buff */
Robert Love85b4aa42008-12-09 15:10:24 -08001525 if (skb_is_nonlinear(skb)) {
1526 skb_frag_t *frag;
1527 if (fcoe_get_paged_crc_eof(skb, tlen)) {
Roel Kluine9041582009-02-27 10:56:22 -08001528 kfree_skb(skb);
Robert Love85b4aa42008-12-09 15:10:24 -08001529 return -ENOMEM;
1530 }
1531 frag = &skb_shinfo(skb)->frags[skb_shinfo(skb)->nr_frags - 1];
1532 cp = kmap_atomic(frag->page, KM_SKB_DATA_SOFTIRQ)
1533 + frag->page_offset;
1534 } else {
1535 cp = (struct fcoe_crc_eof *)skb_put(skb, tlen);
1536 }
1537
1538 memset(cp, 0, sizeof(*cp));
1539 cp->fcoe_eof = eof;
1540 cp->fcoe_crc32 = cpu_to_le32(~crc);
1541
1542 if (skb_is_nonlinear(skb)) {
1543 kunmap_atomic(cp, KM_SKB_DATA_SOFTIRQ);
1544 cp = NULL;
1545 }
1546
Chris Leech014f5c32009-08-25 13:59:30 -07001547 /* adjust skb network/transport offsets to match mac/fcoe/port */
Robert Love85b4aa42008-12-09 15:10:24 -08001548 skb_push(skb, elen + hlen);
1549 skb_reset_mac_header(skb);
1550 skb_reset_network_header(skb);
1551 skb->mac_len = elen;
Yi Zou211c7382009-02-27 14:06:37 -08001552 skb->protocol = htons(ETH_P_FCOE);
Chris Leech3fe9a0b2009-08-25 13:59:46 -07001553 skb->dev = fcoe->netdev;
Robert Love85b4aa42008-12-09 15:10:24 -08001554
1555 /* fill up mac and fcoe headers */
1556 eh = eth_hdr(skb);
1557 eh->h_proto = htons(ETH_P_FCOE);
Joe Eykholtcd229e42010-07-20 15:20:40 -07001558 memcpy(eh->h_dest, fcoe->ctlr.dest_addr, ETH_ALEN);
Chris Leech3fe9a0b2009-08-25 13:59:46 -07001559 if (fcoe->ctlr.map_dest)
Joe Eykholtcd229e42010-07-20 15:20:40 -07001560 memcpy(eh->h_dest + 3, fh->fh_d_id, 3);
Robert Love85b4aa42008-12-09 15:10:24 -08001561
Chris Leech3fe9a0b2009-08-25 13:59:46 -07001562 if (unlikely(fcoe->ctlr.flogi_oxid != FC_XID_UNKNOWN))
1563 memcpy(eh->h_source, fcoe->ctlr.ctl_src_addr, ETH_ALEN);
Robert Love85b4aa42008-12-09 15:10:24 -08001564 else
Chris Leech11b56182009-11-03 11:46:29 -08001565 memcpy(eh->h_source, port->data_src_addr, ETH_ALEN);
Robert Love85b4aa42008-12-09 15:10:24 -08001566
1567 hp = (struct fcoe_hdr *)(eh + 1);
1568 memset(hp, 0, sizeof(*hp));
1569 if (FC_FCOE_VER)
1570 FC_FCOE_ENCAPS_VER(hp, FC_FCOE_VER);
1571 hp->fcoe_sof = sof;
1572
Yi Zou39ca9a02009-02-27 14:07:15 -08001573 /* fcoe lso, mss is in max_payload which is non-zero for FCP data */
Robert Love1875f272009-11-03 11:47:50 -08001574 if (lport->seq_offload && fr_max_payload(fp)) {
Yi Zou39ca9a02009-02-27 14:07:15 -08001575 skb_shinfo(skb)->gso_type = SKB_GSO_FCOE;
1576 skb_shinfo(skb)->gso_size = fr_max_payload(fp);
1577 } else {
1578 skb_shinfo(skb)->gso_type = 0;
1579 skb_shinfo(skb)->gso_size = 0;
1580 }
Robert Love85b4aa42008-12-09 15:10:24 -08001581 /* update tx stats: regardless if LLD fails */
Joe Eykholtf018b732010-03-12 16:08:55 -08001582 stats = per_cpu_ptr(lport->dev_stats, get_cpu());
Robert Love582b45b2009-03-31 15:51:50 -07001583 stats->TxFrames++;
1584 stats->TxWords += wlen;
Joe Eykholtf018b732010-03-12 16:08:55 -08001585 put_cpu();
Robert Love85b4aa42008-12-09 15:10:24 -08001586
1587 /* send down to lld */
Robert Love1875f272009-11-03 11:47:50 -08001588 fr_dev(fp) = lport;
Chris Leech014f5c32009-08-25 13:59:30 -07001589 if (port->fcoe_pending_queue.qlen)
Robert Love1875f272009-11-03 11:47:50 -08001590 fcoe_check_wait_queue(lport, skb);
Vasu Dev4bb6b512009-05-06 10:52:34 -07001591 else if (fcoe_start_io(skb))
Robert Love1875f272009-11-03 11:47:50 -08001592 fcoe_check_wait_queue(lport, skb);
Robert Love85b4aa42008-12-09 15:10:24 -08001593
1594 return 0;
1595}
Robert Love85b4aa42008-12-09 15:10:24 -08001596
Robert Love34f42a02009-02-27 10:55:45 -08001597/**
Robert Love1875f272009-11-03 11:47:50 -08001598 * fcoe_percpu_flush_done() - Indicate per-CPU queue flush completion
1599 * @skb: The completed skb (argument required by destructor)
Joe Eykholte7a51992009-08-25 14:04:08 -07001600 */
1601static void fcoe_percpu_flush_done(struct sk_buff *skb)
1602{
1603 complete(&fcoe_flush_completion);
1604}
1605
1606/**
Vasu Dev52ee83212011-01-28 16:03:52 -08001607 * fcoe_filter_frames() - filter out bad fcoe frames, i.e. bad CRC
1608 * @lport: The local port the frame was received on
1609 * @fp: The received frame
1610 *
1611 * Return: 0 on passing filtering checks
1612 */
1613static inline int fcoe_filter_frames(struct fc_lport *lport,
1614 struct fc_frame *fp)
1615{
1616 struct fcoe_interface *fcoe;
1617 struct fc_frame_header *fh;
1618 struct sk_buff *skb = (struct sk_buff *)fp;
1619 struct fcoe_dev_stats *stats;
1620
1621 /*
1622 * We only check CRC if no offload is available and if it is
1623 * it's solicited data, in which case, the FCP layer would
1624 * check it during the copy.
1625 */
1626 if (lport->crc_offload && skb->ip_summed == CHECKSUM_UNNECESSARY)
1627 fr_flags(fp) &= ~FCPHF_CRC_UNCHECKED;
1628 else
1629 fr_flags(fp) |= FCPHF_CRC_UNCHECKED;
1630
1631 fh = (struct fc_frame_header *) skb_transport_header(skb);
1632 fh = fc_frame_header_get(fp);
1633 if (fh->fh_r_ctl == FC_RCTL_DD_SOL_DATA && fh->fh_type == FC_TYPE_FCP)
1634 return 0;
1635
1636 fcoe = ((struct fcoe_port *)lport_priv(lport))->fcoe;
1637 if (is_fip_mode(&fcoe->ctlr) && fc_frame_payload_op(fp) == ELS_LOGO &&
1638 ntoh24(fh->fh_s_id) == FC_FID_FLOGI) {
1639 FCOE_DBG("fcoe: dropping FCoE lport LOGO in fip mode\n");
1640 return -EINVAL;
1641 }
1642
1643 if (!fr_flags(fp) & FCPHF_CRC_UNCHECKED ||
1644 le32_to_cpu(fr_crc(fp)) == ~crc32(~0, skb->data, skb->len)) {
1645 fr_flags(fp) &= ~FCPHF_CRC_UNCHECKED;
1646 return 0;
1647 }
1648
1649 stats = per_cpu_ptr(lport->dev_stats, get_cpu());
1650 stats->InvalidCRCCount++;
1651 if (stats->InvalidCRCCount < 5)
1652 printk(KERN_WARNING "fcoe: dropping frame with CRC error\n");
1653 return -EINVAL;
1654}
1655
1656/**
Chris Leech859b7b62009-11-20 14:54:47 -08001657 * fcoe_recv_frame() - process a single received frame
1658 * @skb: frame to process
1659 */
1660static void fcoe_recv_frame(struct sk_buff *skb)
1661{
1662 u32 fr_len;
1663 struct fc_lport *lport;
1664 struct fcoe_rcv_info *fr;
1665 struct fcoe_dev_stats *stats;
Chris Leech859b7b62009-11-20 14:54:47 -08001666 struct fcoe_crc_eof crc_eof;
1667 struct fc_frame *fp;
Chris Leech859b7b62009-11-20 14:54:47 -08001668 struct fcoe_port *port;
1669 struct fcoe_hdr *hp;
1670
1671 fr = fcoe_dev_from_skb(skb);
1672 lport = fr->fr_dev;
1673 if (unlikely(!lport)) {
1674 if (skb->destructor != fcoe_percpu_flush_done)
1675 FCOE_NETDEV_DBG(skb->dev, "NULL lport in skb");
1676 kfree_skb(skb);
1677 return;
1678 }
1679
1680 FCOE_NETDEV_DBG(skb->dev, "skb_info: len:%d data_len:%d "
1681 "head:%p data:%p tail:%p end:%p sum:%d dev:%s",
1682 skb->len, skb->data_len,
1683 skb->head, skb->data, skb_tail_pointer(skb),
1684 skb_end_pointer(skb), skb->csum,
1685 skb->dev ? skb->dev->name : "<NULL>");
1686
Chris Leech859b7b62009-11-20 14:54:47 -08001687 port = lport_priv(lport);
1688 if (skb_is_nonlinear(skb))
1689 skb_linearize(skb); /* not ideal */
Chris Leech859b7b62009-11-20 14:54:47 -08001690
1691 /*
1692 * Frame length checks and setting up the header pointers
1693 * was done in fcoe_rcv already.
1694 */
1695 hp = (struct fcoe_hdr *) skb_network_header(skb);
Chris Leech859b7b62009-11-20 14:54:47 -08001696
Joe Eykholtf018b732010-03-12 16:08:55 -08001697 stats = per_cpu_ptr(lport->dev_stats, get_cpu());
Chris Leech859b7b62009-11-20 14:54:47 -08001698 if (unlikely(FC_FCOE_DECAPS_VER(hp) != FC_FCOE_VER)) {
1699 if (stats->ErrorFrames < 5)
1700 printk(KERN_WARNING "fcoe: FCoE version "
1701 "mismatch: The frame has "
1702 "version %x, but the "
1703 "initiator supports version "
1704 "%x\n", FC_FCOE_DECAPS_VER(hp),
1705 FC_FCOE_VER);
Joe Eykholtf018b732010-03-12 16:08:55 -08001706 goto drop;
Chris Leech859b7b62009-11-20 14:54:47 -08001707 }
1708
1709 skb_pull(skb, sizeof(struct fcoe_hdr));
1710 fr_len = skb->len - sizeof(struct fcoe_crc_eof);
1711
1712 stats->RxFrames++;
1713 stats->RxWords += fr_len / FCOE_WORD_TO_BYTE;
1714
1715 fp = (struct fc_frame *)skb;
1716 fc_frame_init(fp);
1717 fr_dev(fp) = lport;
1718 fr_sof(fp) = hp->fcoe_sof;
1719
1720 /* Copy out the CRC and EOF trailer for access */
Joe Eykholtf018b732010-03-12 16:08:55 -08001721 if (skb_copy_bits(skb, fr_len, &crc_eof, sizeof(crc_eof)))
1722 goto drop;
Chris Leech859b7b62009-11-20 14:54:47 -08001723 fr_eof(fp) = crc_eof.fcoe_eof;
1724 fr_crc(fp) = crc_eof.fcoe_crc32;
Joe Eykholtf018b732010-03-12 16:08:55 -08001725 if (pskb_trim(skb, fr_len))
1726 goto drop;
Chris Leech859b7b62009-11-20 14:54:47 -08001727
Vasu Dev52ee83212011-01-28 16:03:52 -08001728 if (!fcoe_filter_frames(lport, fp)) {
1729 put_cpu();
1730 fc_exch_recv(lport, fp);
1731 return;
Chris Leech859b7b62009-11-20 14:54:47 -08001732 }
Joe Eykholtf018b732010-03-12 16:08:55 -08001733drop:
1734 stats->ErrorFrames++;
1735 put_cpu();
1736 kfree_skb(skb);
Chris Leech859b7b62009-11-20 14:54:47 -08001737}
1738
1739/**
Robert Love1875f272009-11-03 11:47:50 -08001740 * fcoe_percpu_receive_thread() - The per-CPU packet receive thread
1741 * @arg: The per-CPU context
Robert Love85b4aa42008-12-09 15:10:24 -08001742 *
1743 * Return: 0 for success
Robert Love85b4aa42008-12-09 15:10:24 -08001744 */
1745int fcoe_percpu_receive_thread(void *arg)
1746{
1747 struct fcoe_percpu_s *p = arg;
Robert Love85b4aa42008-12-09 15:10:24 -08001748 struct sk_buff *skb;
Robert Love85b4aa42008-12-09 15:10:24 -08001749
Robert Love4469c192009-02-27 10:56:38 -08001750 set_user_nice(current, -20);
Robert Love85b4aa42008-12-09 15:10:24 -08001751
1752 while (!kthread_should_stop()) {
1753
1754 spin_lock_bh(&p->fcoe_rx_list.lock);
1755 while ((skb = __skb_dequeue(&p->fcoe_rx_list)) == NULL) {
1756 set_current_state(TASK_INTERRUPTIBLE);
1757 spin_unlock_bh(&p->fcoe_rx_list.lock);
1758 schedule();
1759 set_current_state(TASK_RUNNING);
1760 if (kthread_should_stop())
1761 return 0;
1762 spin_lock_bh(&p->fcoe_rx_list.lock);
1763 }
1764 spin_unlock_bh(&p->fcoe_rx_list.lock);
Chris Leech859b7b62009-11-20 14:54:47 -08001765 fcoe_recv_frame(skb);
Robert Love85b4aa42008-12-09 15:10:24 -08001766 }
1767 return 0;
1768}
1769
1770/**
Robert Love1875f272009-11-03 11:47:50 -08001771 * fcoe_check_wait_queue() - Attempt to clear the transmit backlog
1772 * @lport: The local port whose backlog is to be cleared
Robert Love85b4aa42008-12-09 15:10:24 -08001773 *
Robert Love1875f272009-11-03 11:47:50 -08001774 * This empties the wait_queue, dequeues the head of the wait_queue queue
1775 * and calls fcoe_start_io() for each packet. If all skb have been
1776 * transmitted it returns the qlen. If an error occurs it restores
1777 * wait_queue (to try again later) and returns -1.
Robert Love85b4aa42008-12-09 15:10:24 -08001778 *
Robert Love1875f272009-11-03 11:47:50 -08001779 * The wait_queue is used when the skb transmit fails. The failed skb
1780 * will go in the wait_queue which will be emptied by the timer function or
Robert Love85b4aa42008-12-09 15:10:24 -08001781 * by the next skb transmit.
Robert Love34f42a02009-02-27 10:55:45 -08001782 */
Robert Love1875f272009-11-03 11:47:50 -08001783static void fcoe_check_wait_queue(struct fc_lport *lport, struct sk_buff *skb)
Robert Love85b4aa42008-12-09 15:10:24 -08001784{
Robert Love1875f272009-11-03 11:47:50 -08001785 struct fcoe_port *port = lport_priv(lport);
Vasu Dev4bb6b512009-05-06 10:52:34 -07001786 int rc;
Robert Love85b4aa42008-12-09 15:10:24 -08001787
Chris Leech014f5c32009-08-25 13:59:30 -07001788 spin_lock_bh(&port->fcoe_pending_queue.lock);
Vasu Dev4bb6b512009-05-06 10:52:34 -07001789
1790 if (skb)
Chris Leech014f5c32009-08-25 13:59:30 -07001791 __skb_queue_tail(&port->fcoe_pending_queue, skb);
Vasu Dev4bb6b512009-05-06 10:52:34 -07001792
Chris Leech014f5c32009-08-25 13:59:30 -07001793 if (port->fcoe_pending_queue_active)
Vasu Devc826a312009-02-27 10:56:27 -08001794 goto out;
Chris Leech014f5c32009-08-25 13:59:30 -07001795 port->fcoe_pending_queue_active = 1;
Chris Leech55c8baf2009-02-27 10:56:32 -08001796
Chris Leech014f5c32009-08-25 13:59:30 -07001797 while (port->fcoe_pending_queue.qlen) {
Chris Leech55c8baf2009-02-27 10:56:32 -08001798 /* keep qlen > 0 until fcoe_start_io succeeds */
Chris Leech014f5c32009-08-25 13:59:30 -07001799 port->fcoe_pending_queue.qlen++;
1800 skb = __skb_dequeue(&port->fcoe_pending_queue);
Chris Leech55c8baf2009-02-27 10:56:32 -08001801
Chris Leech014f5c32009-08-25 13:59:30 -07001802 spin_unlock_bh(&port->fcoe_pending_queue.lock);
Chris Leech55c8baf2009-02-27 10:56:32 -08001803 rc = fcoe_start_io(skb);
Chris Leech014f5c32009-08-25 13:59:30 -07001804 spin_lock_bh(&port->fcoe_pending_queue.lock);
Chris Leech55c8baf2009-02-27 10:56:32 -08001805
1806 if (rc) {
Chris Leech014f5c32009-08-25 13:59:30 -07001807 __skb_queue_head(&port->fcoe_pending_queue, skb);
Chris Leech55c8baf2009-02-27 10:56:32 -08001808 /* undo temporary increment above */
Chris Leech014f5c32009-08-25 13:59:30 -07001809 port->fcoe_pending_queue.qlen--;
Chris Leech55c8baf2009-02-27 10:56:32 -08001810 break;
Robert Love85b4aa42008-12-09 15:10:24 -08001811 }
Chris Leech55c8baf2009-02-27 10:56:32 -08001812 /* undo temporary increment above */
Chris Leech014f5c32009-08-25 13:59:30 -07001813 port->fcoe_pending_queue.qlen--;
Robert Love85b4aa42008-12-09 15:10:24 -08001814 }
Chris Leech55c8baf2009-02-27 10:56:32 -08001815
Chris Leech014f5c32009-08-25 13:59:30 -07001816 if (port->fcoe_pending_queue.qlen < FCOE_LOW_QUEUE_DEPTH)
Robert Love1875f272009-11-03 11:47:50 -08001817 lport->qfull = 0;
Chris Leech014f5c32009-08-25 13:59:30 -07001818 if (port->fcoe_pending_queue.qlen && !timer_pending(&port->timer))
1819 mod_timer(&port->timer, jiffies + 2);
1820 port->fcoe_pending_queue_active = 0;
Vasu Devc826a312009-02-27 10:56:27 -08001821out:
Chris Leech014f5c32009-08-25 13:59:30 -07001822 if (port->fcoe_pending_queue.qlen > FCOE_MAX_QUEUE_DEPTH)
Robert Love1875f272009-11-03 11:47:50 -08001823 lport->qfull = 1;
Chris Leech014f5c32009-08-25 13:59:30 -07001824 spin_unlock_bh(&port->fcoe_pending_queue.lock);
Vasu Dev4bb6b512009-05-06 10:52:34 -07001825 return;
Robert Love85b4aa42008-12-09 15:10:24 -08001826}
1827
1828/**
Robert Love1875f272009-11-03 11:47:50 -08001829 * fcoe_dev_setup() - Setup the link change notification interface
Robert Love34f42a02009-02-27 10:55:45 -08001830 */
Randy Dunlapb0d428a2009-04-27 21:49:31 -07001831static void fcoe_dev_setup(void)
Robert Love85b4aa42008-12-09 15:10:24 -08001832{
Robert Love85b4aa42008-12-09 15:10:24 -08001833 register_netdevice_notifier(&fcoe_notifier);
1834}
1835
1836/**
Robert Love1875f272009-11-03 11:47:50 -08001837 * fcoe_dev_cleanup() - Cleanup the link change notification interface
Robert Love34f42a02009-02-27 10:55:45 -08001838 */
Robert Love85b4aa42008-12-09 15:10:24 -08001839static void fcoe_dev_cleanup(void)
1840{
1841 unregister_netdevice_notifier(&fcoe_notifier);
1842}
1843
1844/**
Robert Love1875f272009-11-03 11:47:50 -08001845 * fcoe_device_notification() - Handler for net device events
1846 * @notifier: The context of the notification
1847 * @event: The type of event
1848 * @ptr: The net device that the event was on
Robert Love85b4aa42008-12-09 15:10:24 -08001849 *
Robert Love1875f272009-11-03 11:47:50 -08001850 * This function is called by the Ethernet driver in case of link change event.
Robert Love85b4aa42008-12-09 15:10:24 -08001851 *
1852 * Returns: 0 for success
Robert Love34f42a02009-02-27 10:55:45 -08001853 */
Robert Love85b4aa42008-12-09 15:10:24 -08001854static int fcoe_device_notification(struct notifier_block *notifier,
1855 ulong event, void *ptr)
1856{
Robert Love1875f272009-11-03 11:47:50 -08001857 struct fc_lport *lport = NULL;
Vasu Dev1d1b88d2009-07-29 17:05:45 -07001858 struct net_device *netdev = ptr;
Chris Leech014f5c32009-08-25 13:59:30 -07001859 struct fcoe_interface *fcoe;
Chris Leech2e70e242009-08-25 14:00:23 -07001860 struct fcoe_port *port;
Robert Love85b4aa42008-12-09 15:10:24 -08001861 struct fcoe_dev_stats *stats;
Joe Eykholt97c83892009-03-17 11:42:40 -07001862 u32 link_possible = 1;
Robert Love85b4aa42008-12-09 15:10:24 -08001863 u32 mfs;
1864 int rc = NOTIFY_OK;
1865
Chris Leech014f5c32009-08-25 13:59:30 -07001866 list_for_each_entry(fcoe, &fcoe_hostlist, list) {
Chris Leech25024982009-08-25 13:59:35 -07001867 if (fcoe->netdev == netdev) {
Robert Love1875f272009-11-03 11:47:50 -08001868 lport = fcoe->ctlr.lp;
Robert Love85b4aa42008-12-09 15:10:24 -08001869 break;
1870 }
1871 }
Robert Love1875f272009-11-03 11:47:50 -08001872 if (!lport) {
Robert Love85b4aa42008-12-09 15:10:24 -08001873 rc = NOTIFY_DONE;
1874 goto out;
1875 }
1876
Robert Love85b4aa42008-12-09 15:10:24 -08001877 switch (event) {
1878 case NETDEV_DOWN:
1879 case NETDEV_GOING_DOWN:
Joe Eykholt97c83892009-03-17 11:42:40 -07001880 link_possible = 0;
Robert Love85b4aa42008-12-09 15:10:24 -08001881 break;
1882 case NETDEV_UP:
1883 case NETDEV_CHANGE:
Robert Love85b4aa42008-12-09 15:10:24 -08001884 break;
1885 case NETDEV_CHANGEMTU:
Yi Zou7221d7e2009-10-21 16:27:52 -07001886 if (netdev->features & NETIF_F_FCOE_MTU)
1887 break;
Vasu Dev1d1b88d2009-07-29 17:05:45 -07001888 mfs = netdev->mtu - (sizeof(struct fcoe_hdr) +
1889 sizeof(struct fcoe_crc_eof));
Robert Love85b4aa42008-12-09 15:10:24 -08001890 if (mfs >= FC_MIN_MAX_FRAME)
Robert Love1875f272009-11-03 11:47:50 -08001891 fc_set_mfs(lport, mfs);
Robert Love85b4aa42008-12-09 15:10:24 -08001892 break;
1893 case NETDEV_REGISTER:
1894 break;
Chris Leech2e70e242009-08-25 14:00:23 -07001895 case NETDEV_UNREGISTER:
1896 list_del(&fcoe->list);
1897 port = lport_priv(fcoe->ctlr.lp);
1898 fcoe_interface_cleanup(fcoe);
1899 schedule_work(&port->destroy_work);
1900 goto out;
1901 break;
Yi Zou54a5b212010-07-20 15:21:17 -07001902 case NETDEV_FEAT_CHANGE:
1903 fcoe_netdev_features_change(lport, netdev);
1904 break;
Robert Love85b4aa42008-12-09 15:10:24 -08001905 default:
Vasu Dev1d1b88d2009-07-29 17:05:45 -07001906 FCOE_NETDEV_DBG(netdev, "Unknown event %ld "
Robert Loved5488eb2009-06-10 15:30:59 -07001907 "from netdev netlink\n", event);
Robert Love85b4aa42008-12-09 15:10:24 -08001908 }
Robert Love5e4f8fe2010-05-07 15:18:35 -07001909
1910 fcoe_link_speed_update(lport);
1911
Robert Love1875f272009-11-03 11:47:50 -08001912 if (link_possible && !fcoe_link_ok(lport))
Chris Leech3fe9a0b2009-08-25 13:59:46 -07001913 fcoe_ctlr_link_up(&fcoe->ctlr);
1914 else if (fcoe_ctlr_link_down(&fcoe->ctlr)) {
Joe Eykholtf018b732010-03-12 16:08:55 -08001915 stats = per_cpu_ptr(lport->dev_stats, get_cpu());
Joe Eykholt97c83892009-03-17 11:42:40 -07001916 stats->LinkFailureCount++;
Joe Eykholtf018b732010-03-12 16:08:55 -08001917 put_cpu();
Robert Love1875f272009-11-03 11:47:50 -08001918 fcoe_clean_pending_queue(lport);
Robert Love85b4aa42008-12-09 15:10:24 -08001919 }
1920out:
1921 return rc;
1922}
1923
1924/**
Vasu Dev55a66d32009-12-10 09:59:31 -08001925 * fcoe_disable() - Disables a FCoE interface
Yi Zou78a58242011-01-28 16:05:16 -08001926 * @netdev : The net_device object the Ethernet interface to create on
Vasu Dev55a66d32009-12-10 09:59:31 -08001927 *
Yi Zou78a58242011-01-28 16:05:16 -08001928 * Called from fcoe transport.
Vasu Dev55a66d32009-12-10 09:59:31 -08001929 *
1930 * Returns: 0 for success
1931 */
Yi Zou78a58242011-01-28 16:05:16 -08001932static int fcoe_disable(struct net_device *netdev)
Vasu Dev55a66d32009-12-10 09:59:31 -08001933{
1934 struct fcoe_interface *fcoe;
Vasu Dev55a66d32009-12-10 09:59:31 -08001935 int rc = 0;
1936
1937 mutex_lock(&fcoe_config_mutex);
1938#ifdef CONFIG_FCOE_MODULE
1939 /*
1940 * Make sure the module has been initialized, and is not about to be
1941 * removed. Module paramter sysfs files are writable before the
1942 * module_init function is called and after module_exit.
1943 */
1944 if (THIS_MODULE->state != MODULE_STATE_LIVE) {
1945 rc = -ENODEV;
1946 goto out_nodev;
1947 }
1948#endif
1949
Vasu Dev34ce27b2010-05-07 15:18:46 -07001950 if (!rtnl_trylock()) {
Vasu Dev34ce27b2010-05-07 15:18:46 -07001951 mutex_unlock(&fcoe_config_mutex);
Yi Zou78a58242011-01-28 16:05:16 -08001952 return -ERESTARTSYS;
Vasu Dev34ce27b2010-05-07 15:18:46 -07001953 }
1954
Vasu Dev55a66d32009-12-10 09:59:31 -08001955 fcoe = fcoe_hostlist_lookup_port(netdev);
1956 rtnl_unlock();
1957
Chris Leech9ee50e42010-04-09 14:22:23 -07001958 if (fcoe) {
Chris Leech9ee50e42010-04-09 14:22:23 -07001959 fcoe_ctlr_link_down(&fcoe->ctlr);
Vasu Dev9d4cbc02010-07-20 15:19:26 -07001960 fcoe_clean_pending_queue(fcoe->ctlr.lp);
Chris Leech9ee50e42010-04-09 14:22:23 -07001961 } else
Vasu Dev55a66d32009-12-10 09:59:31 -08001962 rc = -ENODEV;
1963
Vasu Dev55a66d32009-12-10 09:59:31 -08001964out_nodev:
1965 mutex_unlock(&fcoe_config_mutex);
1966 return rc;
1967}
1968
1969/**
1970 * fcoe_enable() - Enables a FCoE interface
Yi Zou78a58242011-01-28 16:05:16 -08001971 * @netdev : The net_device object the Ethernet interface to create on
Vasu Dev55a66d32009-12-10 09:59:31 -08001972 *
Yi Zou78a58242011-01-28 16:05:16 -08001973 * Called from fcoe transport.
Vasu Dev55a66d32009-12-10 09:59:31 -08001974 *
1975 * Returns: 0 for success
1976 */
Yi Zou78a58242011-01-28 16:05:16 -08001977static int fcoe_enable(struct net_device *netdev)
Vasu Dev55a66d32009-12-10 09:59:31 -08001978{
1979 struct fcoe_interface *fcoe;
Vasu Dev55a66d32009-12-10 09:59:31 -08001980 int rc = 0;
1981
1982 mutex_lock(&fcoe_config_mutex);
1983#ifdef CONFIG_FCOE_MODULE
1984 /*
1985 * Make sure the module has been initialized, and is not about to be
1986 * removed. Module paramter sysfs files are writable before the
1987 * module_init function is called and after module_exit.
1988 */
1989 if (THIS_MODULE->state != MODULE_STATE_LIVE) {
1990 rc = -ENODEV;
1991 goto out_nodev;
1992 }
1993#endif
Vasu Dev34ce27b2010-05-07 15:18:46 -07001994 if (!rtnl_trylock()) {
Vasu Dev34ce27b2010-05-07 15:18:46 -07001995 mutex_unlock(&fcoe_config_mutex);
Yi Zou78a58242011-01-28 16:05:16 -08001996 return -ERESTARTSYS;
Vasu Dev34ce27b2010-05-07 15:18:46 -07001997 }
1998
Vasu Dev55a66d32009-12-10 09:59:31 -08001999 fcoe = fcoe_hostlist_lookup_port(netdev);
2000 rtnl_unlock();
2001
Vasu Dev9d4cbc02010-07-20 15:19:26 -07002002 if (!fcoe)
Vasu Dev55a66d32009-12-10 09:59:31 -08002003 rc = -ENODEV;
Vasu Dev9d4cbc02010-07-20 15:19:26 -07002004 else if (!fcoe_link_ok(fcoe->ctlr.lp))
2005 fcoe_ctlr_link_up(&fcoe->ctlr);
Vasu Dev55a66d32009-12-10 09:59:31 -08002006
Vasu Dev55a66d32009-12-10 09:59:31 -08002007out_nodev:
2008 mutex_unlock(&fcoe_config_mutex);
2009 return rc;
2010}
2011
2012/**
Robert Love1875f272009-11-03 11:47:50 -08002013 * fcoe_destroy() - Destroy a FCoE interface
Yi Zou78a58242011-01-28 16:05:16 -08002014 * @netdev : The net_device object the Ethernet interface to create on
Robert Love1875f272009-11-03 11:47:50 -08002015 *
Yi Zou78a58242011-01-28 16:05:16 -08002016 * Called from fcoe transport
Robert Love85b4aa42008-12-09 15:10:24 -08002017 *
2018 * Returns: 0 for success
Robert Love34f42a02009-02-27 10:55:45 -08002019 */
Yi Zou78a58242011-01-28 16:05:16 -08002020static int fcoe_destroy(struct net_device *netdev)
Robert Love85b4aa42008-12-09 15:10:24 -08002021{
Chris Leech030f4e02009-08-25 14:00:02 -07002022 struct fcoe_interface *fcoe;
Mike Christie8eca3552009-10-21 16:27:44 -07002023 int rc = 0;
Robert Love85b4aa42008-12-09 15:10:24 -08002024
Chris Leechdfc1d0f2009-08-25 14:00:13 -07002025 mutex_lock(&fcoe_config_mutex);
2026#ifdef CONFIG_FCOE_MODULE
2027 /*
2028 * Make sure the module has been initialized, and is not about to be
2029 * removed. Module paramter sysfs files are writable before the
2030 * module_init function is called and after module_exit.
2031 */
2032 if (THIS_MODULE->state != MODULE_STATE_LIVE) {
2033 rc = -ENODEV;
2034 goto out_nodev;
2035 }
2036#endif
Vasu Dev34ce27b2010-05-07 15:18:46 -07002037 if (!rtnl_trylock()) {
Vasu Dev34ce27b2010-05-07 15:18:46 -07002038 mutex_unlock(&fcoe_config_mutex);
Yi Zou78a58242011-01-28 16:05:16 -08002039 return -ERESTARTSYS;
Vasu Dev34ce27b2010-05-07 15:18:46 -07002040 }
2041
Chris Leech2e70e242009-08-25 14:00:23 -07002042 fcoe = fcoe_hostlist_lookup_port(netdev);
2043 if (!fcoe) {
Chris Leech090eb6c2009-08-25 14:00:28 -07002044 rtnl_unlock();
Robert Love85b4aa42008-12-09 15:10:24 -08002045 rc = -ENODEV;
Yi Zou78a58242011-01-28 16:05:16 -08002046 goto out_nodev;
Robert Love85b4aa42008-12-09 15:10:24 -08002047 }
Chris Leech2e70e242009-08-25 14:00:23 -07002048 fcoe_interface_cleanup(fcoe);
Yi Zou54a5b212010-07-20 15:21:17 -07002049 list_del(&fcoe->list);
Vasu Dev34ce27b2010-05-07 15:18:46 -07002050 /* RTNL mutex is dropped by fcoe_if_destroy */
Chris Leech2e70e242009-08-25 14:00:23 -07002051 fcoe_if_destroy(fcoe->ctlr.lp);
Robert Love85b4aa42008-12-09 15:10:24 -08002052out_nodev:
Chris Leechdfc1d0f2009-08-25 14:00:13 -07002053 mutex_unlock(&fcoe_config_mutex);
Robert Love85b4aa42008-12-09 15:10:24 -08002054 return rc;
2055}
2056
Robert Love1875f272009-11-03 11:47:50 -08002057/**
2058 * fcoe_destroy_work() - Destroy a FCoE port in a deferred work context
2059 * @work: Handle to the FCoE port to be destroyed
2060 */
Chris Leech2e70e242009-08-25 14:00:23 -07002061static void fcoe_destroy_work(struct work_struct *work)
2062{
2063 struct fcoe_port *port;
2064
2065 port = container_of(work, struct fcoe_port, destroy_work);
2066 mutex_lock(&fcoe_config_mutex);
Vasu Dev34ce27b2010-05-07 15:18:46 -07002067 rtnl_lock();
2068 /* RTNL mutex is dropped by fcoe_if_destroy */
Chris Leech2e70e242009-08-25 14:00:23 -07002069 fcoe_if_destroy(port->lport);
2070 mutex_unlock(&fcoe_config_mutex);
2071}
2072
Robert Love85b4aa42008-12-09 15:10:24 -08002073/**
Yi Zou78a58242011-01-28 16:05:16 -08002074 * fcoe_match() - Check if the FCoE is supported on the given netdevice
2075 * @netdev : The net_device object the Ethernet interface to create on
Robert Love1875f272009-11-03 11:47:50 -08002076 *
Yi Zou78a58242011-01-28 16:05:16 -08002077 * Called from fcoe transport.
2078 *
2079 * Returns: always returns true as this is the default FCoE transport,
2080 * i.e., support all netdevs.
2081 */
2082static bool fcoe_match(struct net_device *netdev)
2083{
2084 return true;
2085}
2086
2087/**
2088 * fcoe_create() - Create a fcoe interface
2089 * @netdev : The net_device object the Ethernet interface to create on
2090 * @fip_mode: The FIP mode for this creation
2091 *
2092 * Called from fcoe transport
Robert Love85b4aa42008-12-09 15:10:24 -08002093 *
2094 * Returns: 0 for success
Robert Love34f42a02009-02-27 10:55:45 -08002095 */
Yi Zou78a58242011-01-28 16:05:16 -08002096static int fcoe_create(struct net_device *netdev, enum fip_state fip_mode)
Robert Love85b4aa42008-12-09 15:10:24 -08002097{
2098 int rc;
Chris Leech030f4e02009-08-25 14:00:02 -07002099 struct fcoe_interface *fcoe;
Chris Leechaf7f85d2009-08-25 13:59:24 -07002100 struct fc_lport *lport;
Robert Love85b4aa42008-12-09 15:10:24 -08002101
Chris Leechdfc1d0f2009-08-25 14:00:13 -07002102 mutex_lock(&fcoe_config_mutex);
Vasu Dev34ce27b2010-05-07 15:18:46 -07002103
2104 if (!rtnl_trylock()) {
2105 mutex_unlock(&fcoe_config_mutex);
Yi Zou78a58242011-01-28 16:05:16 -08002106 return -ERESTARTSYS;
Vasu Dev34ce27b2010-05-07 15:18:46 -07002107 }
2108
Chris Leechdfc1d0f2009-08-25 14:00:13 -07002109#ifdef CONFIG_FCOE_MODULE
2110 /*
2111 * Make sure the module has been initialized, and is not about to be
2112 * removed. Module paramter sysfs files are writable before the
2113 * module_init function is called and after module_exit.
2114 */
2115 if (THIS_MODULE->state != MODULE_STATE_LIVE) {
2116 rc = -ENODEV;
Robert Love7287fb92011-01-28 16:03:47 -08002117 goto out_nodev;
Chris Leechdfc1d0f2009-08-25 14:00:13 -07002118 }
2119#endif
2120
Robert Love85b4aa42008-12-09 15:10:24 -08002121 /* look for existing lport */
2122 if (fcoe_hostlist_lookup(netdev)) {
2123 rc = -EEXIST;
Yi Zou78a58242011-01-28 16:05:16 -08002124 goto out_nodev;
Robert Love85b4aa42008-12-09 15:10:24 -08002125 }
Robert Love85b4aa42008-12-09 15:10:24 -08002126
Joe Eykholt1dd454d2010-07-20 15:20:46 -07002127 fcoe = fcoe_interface_create(netdev, fip_mode);
Robert Love7287fb92011-01-28 16:03:47 -08002128 if (IS_ERR(fcoe)) {
2129 rc = PTR_ERR(fcoe);
Yi Zou78a58242011-01-28 16:05:16 -08002130 goto out_nodev;
Chris Leech030f4e02009-08-25 14:00:02 -07002131 }
2132
Chris Leech9a057532009-11-03 11:46:40 -08002133 lport = fcoe_if_create(fcoe, &netdev->dev, 0);
Chris Leechaf7f85d2009-08-25 13:59:24 -07002134 if (IS_ERR(lport)) {
Robert Loved5488eb2009-06-10 15:30:59 -07002135 printk(KERN_ERR "fcoe: Failed to create interface (%s)\n",
Robert Love85b4aa42008-12-09 15:10:24 -08002136 netdev->name);
Robert Love85b4aa42008-12-09 15:10:24 -08002137 rc = -EIO;
Chris Leech2e70e242009-08-25 14:00:23 -07002138 fcoe_interface_cleanup(fcoe);
Chris Leech030f4e02009-08-25 14:00:02 -07002139 goto out_free;
Robert Love85b4aa42008-12-09 15:10:24 -08002140 }
Chris Leech030f4e02009-08-25 14:00:02 -07002141
Chris Leech54b649f2009-08-25 14:00:07 -07002142 /* Make this the "master" N_Port */
2143 fcoe->ctlr.lp = lport;
Chris Leech030f4e02009-08-25 14:00:02 -07002144
Chris Leechc863df32009-08-25 14:00:18 -07002145 /* add to lports list */
2146 fcoe_hostlist_add(lport);
2147
Chris Leech54b649f2009-08-25 14:00:07 -07002148 /* start FIP Discovery and FLOGI */
2149 lport->boot_time = jiffies;
2150 fc_fabric_login(lport);
2151 if (!fcoe_link_ok(lport))
2152 fcoe_ctlr_link_up(&fcoe->ctlr);
2153
Chris Leech54b649f2009-08-25 14:00:07 -07002154 /*
2155 * Release from init in fcoe_interface_create(), on success lport
2156 * should be holding a reference taken in fcoe_if_create().
2157 */
Chris Leech030f4e02009-08-25 14:00:02 -07002158 fcoe_interface_put(fcoe);
Rob Love6409ea62010-01-21 10:16:05 -08002159 rtnl_unlock();
2160 mutex_unlock(&fcoe_config_mutex);
2161
2162 return 0;
2163out_free:
2164 fcoe_interface_put(fcoe);
Robert Love85b4aa42008-12-09 15:10:24 -08002165out_nodev:
Vasu Dev34ce27b2010-05-07 15:18:46 -07002166 rtnl_unlock();
Chris Leechdfc1d0f2009-08-25 14:00:13 -07002167 mutex_unlock(&fcoe_config_mutex);
Robert Love85b4aa42008-12-09 15:10:24 -08002168 return rc;
2169}
2170
Robert Love34f42a02009-02-27 10:55:45 -08002171/**
Robert Love5e4f8fe2010-05-07 15:18:35 -07002172 * fcoe_link_speed_update() - Update the supported and actual link speeds
2173 * @lport: The local port to update speeds for
Robert Love85b4aa42008-12-09 15:10:24 -08002174 *
Robert Love5e4f8fe2010-05-07 15:18:35 -07002175 * Returns: 0 if the ethtool query was successful
2176 * -1 if the ethtool query failed
Robert Love85b4aa42008-12-09 15:10:24 -08002177 */
Robert Love5e4f8fe2010-05-07 15:18:35 -07002178int fcoe_link_speed_update(struct fc_lport *lport)
Robert Love85b4aa42008-12-09 15:10:24 -08002179{
Robert Love1875f272009-11-03 11:47:50 -08002180 struct fcoe_port *port = lport_priv(lport);
2181 struct net_device *netdev = port->fcoe->netdev;
Robert Love85b4aa42008-12-09 15:10:24 -08002182 struct ethtool_cmd ecmd = { ETHTOOL_GSET };
Robert Love85b4aa42008-12-09 15:10:24 -08002183
Robert Love5e4f8fe2010-05-07 15:18:35 -07002184 if (!dev_ethtool_get_settings(netdev, &ecmd)) {
Robert Love1875f272009-11-03 11:47:50 -08002185 lport->link_supported_speeds &=
Yi Zou2f718d62009-07-29 17:04:01 -07002186 ~(FC_PORTSPEED_1GBIT | FC_PORTSPEED_10GBIT);
2187 if (ecmd.supported & (SUPPORTED_1000baseT_Half |
2188 SUPPORTED_1000baseT_Full))
Robert Love1875f272009-11-03 11:47:50 -08002189 lport->link_supported_speeds |= FC_PORTSPEED_1GBIT;
Yi Zou2f718d62009-07-29 17:04:01 -07002190 if (ecmd.supported & SUPPORTED_10000baseT_Full)
Robert Love1875f272009-11-03 11:47:50 -08002191 lport->link_supported_speeds |=
Yi Zou2f718d62009-07-29 17:04:01 -07002192 FC_PORTSPEED_10GBIT;
2193 if (ecmd.speed == SPEED_1000)
Robert Love1875f272009-11-03 11:47:50 -08002194 lport->link_speed = FC_PORTSPEED_1GBIT;
Yi Zou2f718d62009-07-29 17:04:01 -07002195 if (ecmd.speed == SPEED_10000)
Robert Love1875f272009-11-03 11:47:50 -08002196 lport->link_speed = FC_PORTSPEED_10GBIT;
Robert Love85b4aa42008-12-09 15:10:24 -08002197
Yi Zou2f718d62009-07-29 17:04:01 -07002198 return 0;
2199 }
2200 return -1;
Robert Love85b4aa42008-12-09 15:10:24 -08002201}
Robert Love85b4aa42008-12-09 15:10:24 -08002202
Robert Love34f42a02009-02-27 10:55:45 -08002203/**
Robert Love5e4f8fe2010-05-07 15:18:35 -07002204 * fcoe_link_ok() - Check if the link is OK for a local port
2205 * @lport: The local port to check link on
2206 *
2207 * Returns: 0 if link is UP and OK, -1 if not
2208 *
2209 */
2210int fcoe_link_ok(struct fc_lport *lport)
2211{
2212 struct fcoe_port *port = lport_priv(lport);
2213 struct net_device *netdev = port->fcoe->netdev;
2214
2215 if (netif_oper_up(netdev))
2216 return 0;
2217 return -1;
2218}
2219
2220/**
Robert Love1875f272009-11-03 11:47:50 -08002221 * fcoe_percpu_clean() - Clear all pending skbs for an local port
2222 * @lport: The local port whose skbs are to be cleared
Joe Eykholte7a51992009-08-25 14:04:08 -07002223 *
2224 * Must be called with fcoe_create_mutex held to single-thread completion.
2225 *
2226 * This flushes the pending skbs by adding a new skb to each queue and
2227 * waiting until they are all freed. This assures us that not only are
2228 * there no packets that will be handled by the lport, but also that any
2229 * threads already handling packet have returned.
Robert Love85b4aa42008-12-09 15:10:24 -08002230 */
Robert Love1875f272009-11-03 11:47:50 -08002231void fcoe_percpu_clean(struct fc_lport *lport)
Robert Love85b4aa42008-12-09 15:10:24 -08002232{
Robert Love85b4aa42008-12-09 15:10:24 -08002233 struct fcoe_percpu_s *pp;
2234 struct fcoe_rcv_info *fr;
2235 struct sk_buff_head *list;
2236 struct sk_buff *skb, *next;
2237 struct sk_buff *head;
Robert Love5e5e92d2009-03-17 11:41:35 -07002238 unsigned int cpu;
Robert Love85b4aa42008-12-09 15:10:24 -08002239
Robert Love5e5e92d2009-03-17 11:41:35 -07002240 for_each_possible_cpu(cpu) {
2241 pp = &per_cpu(fcoe_percpu, cpu);
2242 spin_lock_bh(&pp->fcoe_rx_list.lock);
2243 list = &pp->fcoe_rx_list;
2244 head = list->next;
2245 for (skb = head; skb != (struct sk_buff *)list;
2246 skb = next) {
2247 next = skb->next;
2248 fr = fcoe_dev_from_skb(skb);
Robert Love1875f272009-11-03 11:47:50 -08002249 if (fr->fr_dev == lport) {
Robert Love5e5e92d2009-03-17 11:41:35 -07002250 __skb_unlink(skb, list);
2251 kfree_skb(skb);
Robert Love85b4aa42008-12-09 15:10:24 -08002252 }
Robert Love85b4aa42008-12-09 15:10:24 -08002253 }
Joe Eykholte7a51992009-08-25 14:04:08 -07002254
2255 if (!pp->thread || !cpu_online(cpu)) {
2256 spin_unlock_bh(&pp->fcoe_rx_list.lock);
2257 continue;
2258 }
2259
2260 skb = dev_alloc_skb(0);
2261 if (!skb) {
2262 spin_unlock_bh(&pp->fcoe_rx_list.lock);
2263 continue;
2264 }
2265 skb->destructor = fcoe_percpu_flush_done;
2266
2267 __skb_queue_tail(&pp->fcoe_rx_list, skb);
2268 if (pp->fcoe_rx_list.qlen == 1)
2269 wake_up_process(pp->thread);
Robert Love5e5e92d2009-03-17 11:41:35 -07002270 spin_unlock_bh(&pp->fcoe_rx_list.lock);
Joe Eykholte7a51992009-08-25 14:04:08 -07002271
2272 wait_for_completion(&fcoe_flush_completion);
Robert Love85b4aa42008-12-09 15:10:24 -08002273 }
2274}
Robert Love85b4aa42008-12-09 15:10:24 -08002275
2276/**
Robert Love34f42a02009-02-27 10:55:45 -08002277 * fcoe_clean_pending_queue() - Dequeue a skb and free it
Robert Love1875f272009-11-03 11:47:50 -08002278 * @lport: The local port to dequeue a skb on
Robert Love34f42a02009-02-27 10:55:45 -08002279 */
Robert Love1875f272009-11-03 11:47:50 -08002280void fcoe_clean_pending_queue(struct fc_lport *lport)
Robert Love85b4aa42008-12-09 15:10:24 -08002281{
Robert Love1875f272009-11-03 11:47:50 -08002282 struct fcoe_port *port = lport_priv(lport);
Robert Love85b4aa42008-12-09 15:10:24 -08002283 struct sk_buff *skb;
2284
Chris Leech014f5c32009-08-25 13:59:30 -07002285 spin_lock_bh(&port->fcoe_pending_queue.lock);
2286 while ((skb = __skb_dequeue(&port->fcoe_pending_queue)) != NULL) {
2287 spin_unlock_bh(&port->fcoe_pending_queue.lock);
Robert Love85b4aa42008-12-09 15:10:24 -08002288 kfree_skb(skb);
Chris Leech014f5c32009-08-25 13:59:30 -07002289 spin_lock_bh(&port->fcoe_pending_queue.lock);
Robert Love85b4aa42008-12-09 15:10:24 -08002290 }
Chris Leech014f5c32009-08-25 13:59:30 -07002291 spin_unlock_bh(&port->fcoe_pending_queue.lock);
Robert Love85b4aa42008-12-09 15:10:24 -08002292}
Robert Love85b4aa42008-12-09 15:10:24 -08002293
2294/**
Robert Love1875f272009-11-03 11:47:50 -08002295 * fcoe_reset() - Reset a local port
2296 * @shost: The SCSI host associated with the local port to be reset
Robert Love85b4aa42008-12-09 15:10:24 -08002297 *
Robert Love1875f272009-11-03 11:47:50 -08002298 * Returns: Always 0 (return value required by FC transport template)
Robert Love85b4aa42008-12-09 15:10:24 -08002299 */
2300int fcoe_reset(struct Scsi_Host *shost)
2301{
2302 struct fc_lport *lport = shost_priv(shost);
2303 fc_lport_reset(lport);
2304 return 0;
2305}
Robert Love85b4aa42008-12-09 15:10:24 -08002306
Robert Love34f42a02009-02-27 10:55:45 -08002307/**
Robert Love1875f272009-11-03 11:47:50 -08002308 * fcoe_hostlist_lookup_port() - Find the FCoE interface associated with a net device
2309 * @netdev: The net device used as a key
Robert Love85b4aa42008-12-09 15:10:24 -08002310 *
Robert Love1875f272009-11-03 11:47:50 -08002311 * Locking: Must be called with the RNL mutex held.
2312 *
2313 * Returns: NULL or the FCoE interface
Robert Love85b4aa42008-12-09 15:10:24 -08002314 */
Chris Leech014f5c32009-08-25 13:59:30 -07002315static struct fcoe_interface *
Robert Love1875f272009-11-03 11:47:50 -08002316fcoe_hostlist_lookup_port(const struct net_device *netdev)
Robert Love85b4aa42008-12-09 15:10:24 -08002317{
Chris Leech014f5c32009-08-25 13:59:30 -07002318 struct fcoe_interface *fcoe;
Robert Love85b4aa42008-12-09 15:10:24 -08002319
Chris Leech014f5c32009-08-25 13:59:30 -07002320 list_for_each_entry(fcoe, &fcoe_hostlist, list) {
Robert Love1875f272009-11-03 11:47:50 -08002321 if (fcoe->netdev == netdev)
Chris Leech014f5c32009-08-25 13:59:30 -07002322 return fcoe;
Robert Love85b4aa42008-12-09 15:10:24 -08002323 }
Robert Love85b4aa42008-12-09 15:10:24 -08002324 return NULL;
2325}
2326
Robert Love34f42a02009-02-27 10:55:45 -08002327/**
Robert Love1875f272009-11-03 11:47:50 -08002328 * fcoe_hostlist_lookup() - Find the local port associated with a
2329 * given net device
2330 * @netdev: The netdevice used as a key
Robert Love85b4aa42008-12-09 15:10:24 -08002331 *
Robert Love1875f272009-11-03 11:47:50 -08002332 * Locking: Must be called with the RTNL mutex held
2333 *
2334 * Returns: NULL or the local port
Robert Love85b4aa42008-12-09 15:10:24 -08002335 */
Chris Leech090eb6c2009-08-25 14:00:28 -07002336static struct fc_lport *fcoe_hostlist_lookup(const struct net_device *netdev)
Robert Love85b4aa42008-12-09 15:10:24 -08002337{
Chris Leech014f5c32009-08-25 13:59:30 -07002338 struct fcoe_interface *fcoe;
Robert Love85b4aa42008-12-09 15:10:24 -08002339
Chris Leech014f5c32009-08-25 13:59:30 -07002340 fcoe = fcoe_hostlist_lookup_port(netdev);
Chris Leech3fe9a0b2009-08-25 13:59:46 -07002341 return (fcoe) ? fcoe->ctlr.lp : NULL;
Robert Love85b4aa42008-12-09 15:10:24 -08002342}
Robert Love85b4aa42008-12-09 15:10:24 -08002343
Robert Love34f42a02009-02-27 10:55:45 -08002344/**
Robert Love1875f272009-11-03 11:47:50 -08002345 * fcoe_hostlist_add() - Add the FCoE interface identified by a local
2346 * port to the hostlist
2347 * @lport: The local port that identifies the FCoE interface to be added
2348 *
2349 * Locking: must be called with the RTNL mutex held
Robert Love85b4aa42008-12-09 15:10:24 -08002350 *
2351 * Returns: 0 for success
2352 */
Chris Leech090eb6c2009-08-25 14:00:28 -07002353static int fcoe_hostlist_add(const struct fc_lport *lport)
Robert Love85b4aa42008-12-09 15:10:24 -08002354{
Chris Leech014f5c32009-08-25 13:59:30 -07002355 struct fcoe_interface *fcoe;
2356 struct fcoe_port *port;
Robert Love85b4aa42008-12-09 15:10:24 -08002357
Chris Leech014f5c32009-08-25 13:59:30 -07002358 fcoe = fcoe_hostlist_lookup_port(fcoe_netdev(lport));
2359 if (!fcoe) {
2360 port = lport_priv(lport);
2361 fcoe = port->fcoe;
2362 list_add_tail(&fcoe->list, &fcoe_hostlist);
Robert Love85b4aa42008-12-09 15:10:24 -08002363 }
2364 return 0;
2365}
Robert Love85b4aa42008-12-09 15:10:24 -08002366
Yi Zou78a58242011-01-28 16:05:16 -08002367
2368static struct fcoe_transport fcoe_sw_transport = {
2369 .name = {FCOE_TRANSPORT_DEFAULT},
2370 .attached = false,
2371 .list = LIST_HEAD_INIT(fcoe_sw_transport.list),
2372 .match = fcoe_match,
2373 .create = fcoe_create,
2374 .destroy = fcoe_destroy,
2375 .enable = fcoe_enable,
2376 .disable = fcoe_disable,
2377};
2378
Robert Love34f42a02009-02-27 10:55:45 -08002379/**
Robert Love1875f272009-11-03 11:47:50 -08002380 * fcoe_init() - Initialize fcoe.ko
Robert Love85b4aa42008-12-09 15:10:24 -08002381 *
Robert Love1875f272009-11-03 11:47:50 -08002382 * Returns: 0 on success, or a negative value on failure
Robert Love34f42a02009-02-27 10:55:45 -08002383 */
Robert Love85b4aa42008-12-09 15:10:24 -08002384static int __init fcoe_init(void)
2385{
Robert Love1875f272009-11-03 11:47:50 -08002386 struct fcoe_percpu_s *p;
Robert Love38eccab2009-03-17 11:41:30 -07002387 unsigned int cpu;
Robert Love8976f422009-03-17 11:41:46 -07002388 int rc = 0;
Robert Love85b4aa42008-12-09 15:10:24 -08002389
Yi Zou78a58242011-01-28 16:05:16 -08002390 /* register as a fcoe transport */
2391 rc = fcoe_transport_attach(&fcoe_sw_transport);
2392 if (rc) {
2393 printk(KERN_ERR "failed to register an fcoe transport, check "
2394 "if libfcoe is loaded\n");
2395 return rc;
2396 }
2397
Chris Leechdfc1d0f2009-08-25 14:00:13 -07002398 mutex_lock(&fcoe_config_mutex);
2399
Robert Love38eccab2009-03-17 11:41:30 -07002400 for_each_possible_cpu(cpu) {
Robert Love5e5e92d2009-03-17 11:41:35 -07002401 p = &per_cpu(fcoe_percpu, cpu);
Robert Love38eccab2009-03-17 11:41:30 -07002402 skb_queue_head_init(&p->fcoe_rx_list);
2403 }
2404
Robert Love8976f422009-03-17 11:41:46 -07002405 for_each_online_cpu(cpu)
2406 fcoe_percpu_thread_create(cpu);
Robert Love85b4aa42008-12-09 15:10:24 -08002407
Robert Love8976f422009-03-17 11:41:46 -07002408 /* Initialize per CPU interrupt thread */
2409 rc = register_hotcpu_notifier(&fcoe_cpu_notifier);
2410 if (rc)
2411 goto out_free;
Robert Love85b4aa42008-12-09 15:10:24 -08002412
Robert Love8976f422009-03-17 11:41:46 -07002413 /* Setup link change notification */
Robert Love85b4aa42008-12-09 15:10:24 -08002414 fcoe_dev_setup();
2415
Chris Leech5892c322009-08-25 13:59:14 -07002416 rc = fcoe_if_init();
2417 if (rc)
2418 goto out_free;
Robert Love85b4aa42008-12-09 15:10:24 -08002419
Chris Leechdfc1d0f2009-08-25 14:00:13 -07002420 mutex_unlock(&fcoe_config_mutex);
Robert Love85b4aa42008-12-09 15:10:24 -08002421 return 0;
Robert Love8976f422009-03-17 11:41:46 -07002422
2423out_free:
2424 for_each_online_cpu(cpu) {
2425 fcoe_percpu_thread_destroy(cpu);
2426 }
Chris Leechdfc1d0f2009-08-25 14:00:13 -07002427 mutex_unlock(&fcoe_config_mutex);
Robert Love8976f422009-03-17 11:41:46 -07002428 return rc;
Robert Love85b4aa42008-12-09 15:10:24 -08002429}
2430module_init(fcoe_init);
2431
2432/**
Robert Love1875f272009-11-03 11:47:50 -08002433 * fcoe_exit() - Clean up fcoe.ko
Robert Love85b4aa42008-12-09 15:10:24 -08002434 *
Robert Love1875f272009-11-03 11:47:50 -08002435 * Returns: 0 on success or a negative value on failure
Robert Love34f42a02009-02-27 10:55:45 -08002436 */
Robert Love85b4aa42008-12-09 15:10:24 -08002437static void __exit fcoe_exit(void)
2438{
Chris Leech014f5c32009-08-25 13:59:30 -07002439 struct fcoe_interface *fcoe, *tmp;
Chris Leech2e70e242009-08-25 14:00:23 -07002440 struct fcoe_port *port;
Robert Love1875f272009-11-03 11:47:50 -08002441 unsigned int cpu;
Robert Love85b4aa42008-12-09 15:10:24 -08002442
Chris Leechdfc1d0f2009-08-25 14:00:13 -07002443 mutex_lock(&fcoe_config_mutex);
2444
Robert Love85b4aa42008-12-09 15:10:24 -08002445 fcoe_dev_cleanup();
2446
Vasu Dev5919a592009-03-27 09:03:29 -07002447 /* releases the associated fcoe hosts */
Chris Leech090eb6c2009-08-25 14:00:28 -07002448 rtnl_lock();
2449 list_for_each_entry_safe(fcoe, tmp, &fcoe_hostlist, list) {
Chris Leechc863df32009-08-25 14:00:18 -07002450 list_del(&fcoe->list);
Chris Leech2e70e242009-08-25 14:00:23 -07002451 port = lport_priv(fcoe->ctlr.lp);
Chris Leech2e70e242009-08-25 14:00:23 -07002452 fcoe_interface_cleanup(fcoe);
Chris Leech2e70e242009-08-25 14:00:23 -07002453 schedule_work(&port->destroy_work);
Chris Leechc863df32009-08-25 14:00:18 -07002454 }
Chris Leech090eb6c2009-08-25 14:00:28 -07002455 rtnl_unlock();
Robert Love85b4aa42008-12-09 15:10:24 -08002456
Robert Love8976f422009-03-17 11:41:46 -07002457 unregister_hotcpu_notifier(&fcoe_cpu_notifier);
2458
Chris Leech014f5c32009-08-25 13:59:30 -07002459 for_each_online_cpu(cpu)
Robert Love8976f422009-03-17 11:41:46 -07002460 fcoe_percpu_thread_destroy(cpu);
Robert Love85b4aa42008-12-09 15:10:24 -08002461
Chris Leechdfc1d0f2009-08-25 14:00:13 -07002462 mutex_unlock(&fcoe_config_mutex);
Chris Leech2e70e242009-08-25 14:00:23 -07002463
2464 /* flush any asyncronous interface destroys,
2465 * this should happen after the netdev notifier is unregistered */
2466 flush_scheduled_work();
Chris Leech9a057532009-11-03 11:46:40 -08002467 /* That will flush out all the N_Ports on the hostlist, but now we
2468 * may have NPIV VN_Ports scheduled for destruction */
2469 flush_scheduled_work();
Chris Leech2e70e242009-08-25 14:00:23 -07002470
2471 /* detach from scsi transport
2472 * must happen after all destroys are done, therefor after the flush */
2473 fcoe_if_exit();
Yi Zou78a58242011-01-28 16:05:16 -08002474
2475 /* detach from fcoe transport */
2476 fcoe_transport_detach(&fcoe_sw_transport);
Robert Love85b4aa42008-12-09 15:10:24 -08002477}
2478module_exit(fcoe_exit);
Chris Leech11b56182009-11-03 11:46:29 -08002479
2480/**
2481 * fcoe_flogi_resp() - FCoE specific FLOGI and FDISC response handler
2482 * @seq: active sequence in the FLOGI or FDISC exchange
2483 * @fp: response frame, or error encoded in a pointer (timeout)
2484 * @arg: pointer the the fcoe_ctlr structure
2485 *
Uwe Kleine-König65155b32010-06-11 12:17:01 +02002486 * This handles MAC address management for FCoE, then passes control on to
Chris Leech11b56182009-11-03 11:46:29 -08002487 * the libfc FLOGI response handler.
2488 */
2489static void fcoe_flogi_resp(struct fc_seq *seq, struct fc_frame *fp, void *arg)
2490{
2491 struct fcoe_ctlr *fip = arg;
2492 struct fc_exch *exch = fc_seq_exch(seq);
2493 struct fc_lport *lport = exch->lp;
2494 u8 *mac;
2495
2496 if (IS_ERR(fp))
2497 goto done;
2498
2499 mac = fr_cb(fp)->granted_mac;
2500 if (is_zero_ether_addr(mac)) {
2501 /* pre-FIP */
Joe Eykholt386309c2009-11-03 11:49:16 -08002502 if (fcoe_ctlr_recv_flogi(fip, lport, fp)) {
Chris Leech11b56182009-11-03 11:46:29 -08002503 fc_frame_free(fp);
2504 return;
2505 }
Chris Leech11b56182009-11-03 11:46:29 -08002506 }
Joe Eykholt386309c2009-11-03 11:49:16 -08002507 fcoe_update_src_mac(lport, mac);
Chris Leech11b56182009-11-03 11:46:29 -08002508done:
2509 fc_lport_flogi_resp(seq, fp, lport);
2510}
2511
2512/**
2513 * fcoe_logo_resp() - FCoE specific LOGO response handler
2514 * @seq: active sequence in the LOGO exchange
2515 * @fp: response frame, or error encoded in a pointer (timeout)
2516 * @arg: pointer the the fcoe_ctlr structure
2517 *
Uwe Kleine-König65155b32010-06-11 12:17:01 +02002518 * This handles MAC address management for FCoE, then passes control on to
Chris Leech11b56182009-11-03 11:46:29 -08002519 * the libfc LOGO response handler.
2520 */
2521static void fcoe_logo_resp(struct fc_seq *seq, struct fc_frame *fp, void *arg)
2522{
Joe Eykholt386309c2009-11-03 11:49:16 -08002523 struct fc_lport *lport = arg;
Chris Leech11b56182009-11-03 11:46:29 -08002524 static u8 zero_mac[ETH_ALEN] = { 0 };
2525
2526 if (!IS_ERR(fp))
Joe Eykholt386309c2009-11-03 11:49:16 -08002527 fcoe_update_src_mac(lport, zero_mac);
Chris Leech11b56182009-11-03 11:46:29 -08002528 fc_lport_logo_resp(seq, fp, lport);
2529}
2530
2531/**
2532 * fcoe_elsct_send - FCoE specific ELS handler
2533 *
2534 * This does special case handling of FIP encapsualted ELS exchanges for FCoE,
2535 * using FCoE specific response handlers and passing the FIP controller as
2536 * the argument (the lport is still available from the exchange).
2537 *
2538 * Most of the work here is just handed off to the libfc routine.
2539 */
Robert Love1875f272009-11-03 11:47:50 -08002540static struct fc_seq *fcoe_elsct_send(struct fc_lport *lport, u32 did,
2541 struct fc_frame *fp, unsigned int op,
2542 void (*resp)(struct fc_seq *,
2543 struct fc_frame *,
2544 void *),
2545 void *arg, u32 timeout)
Chris Leech11b56182009-11-03 11:46:29 -08002546{
2547 struct fcoe_port *port = lport_priv(lport);
2548 struct fcoe_interface *fcoe = port->fcoe;
2549 struct fcoe_ctlr *fip = &fcoe->ctlr;
2550 struct fc_frame_header *fh = fc_frame_header_get(fp);
2551
2552 switch (op) {
2553 case ELS_FLOGI:
2554 case ELS_FDISC:
Joe Eykholte10f8c62010-07-20 15:20:30 -07002555 if (lport->point_to_multipoint)
2556 break;
Chris Leech11b56182009-11-03 11:46:29 -08002557 return fc_elsct_send(lport, did, fp, op, fcoe_flogi_resp,
2558 fip, timeout);
2559 case ELS_LOGO:
2560 /* only hook onto fabric logouts, not port logouts */
2561 if (ntoh24(fh->fh_d_id) != FC_FID_FLOGI)
2562 break;
2563 return fc_elsct_send(lport, did, fp, op, fcoe_logo_resp,
Joe Eykholt386309c2009-11-03 11:49:16 -08002564 lport, timeout);
Chris Leech11b56182009-11-03 11:46:29 -08002565 }
2566 return fc_elsct_send(lport, did, fp, op, resp, arg, timeout);
2567}
2568
Chris Leech9a057532009-11-03 11:46:40 -08002569/**
2570 * fcoe_vport_create() - create an fc_host/scsi_host for a vport
2571 * @vport: fc_vport object to create a new fc_host for
2572 * @disabled: start the new fc_host in a disabled state by default?
2573 *
2574 * Returns: 0 for success
2575 */
2576static int fcoe_vport_create(struct fc_vport *vport, bool disabled)
2577{
2578 struct Scsi_Host *shost = vport_to_shost(vport);
2579 struct fc_lport *n_port = shost_priv(shost);
2580 struct fcoe_port *port = lport_priv(n_port);
2581 struct fcoe_interface *fcoe = port->fcoe;
2582 struct net_device *netdev = fcoe->netdev;
2583 struct fc_lport *vn_port;
2584
2585 mutex_lock(&fcoe_config_mutex);
2586 vn_port = fcoe_if_create(fcoe, &vport->dev, 1);
2587 mutex_unlock(&fcoe_config_mutex);
2588
2589 if (IS_ERR(vn_port)) {
2590 printk(KERN_ERR "fcoe: fcoe_vport_create(%s) failed\n",
2591 netdev->name);
2592 return -EIO;
2593 }
2594
2595 if (disabled) {
2596 fc_vport_set_state(vport, FC_VPORT_DISABLED);
2597 } else {
2598 vn_port->boot_time = jiffies;
2599 fc_fabric_login(vn_port);
2600 fc_vport_setlink(vn_port);
2601 }
2602 return 0;
2603}
2604
2605/**
2606 * fcoe_vport_destroy() - destroy the fc_host/scsi_host for a vport
2607 * @vport: fc_vport object that is being destroyed
2608 *
2609 * Returns: 0 for success
2610 */
2611static int fcoe_vport_destroy(struct fc_vport *vport)
2612{
2613 struct Scsi_Host *shost = vport_to_shost(vport);
2614 struct fc_lport *n_port = shost_priv(shost);
2615 struct fc_lport *vn_port = vport->dd_data;
2616 struct fcoe_port *port = lport_priv(vn_port);
2617
2618 mutex_lock(&n_port->lp_mutex);
2619 list_del(&vn_port->list);
2620 mutex_unlock(&n_port->lp_mutex);
2621 schedule_work(&port->destroy_work);
2622 return 0;
2623}
2624
2625/**
2626 * fcoe_vport_disable() - change vport state
2627 * @vport: vport to bring online/offline
2628 * @disable: should the vport be disabled?
2629 */
2630static int fcoe_vport_disable(struct fc_vport *vport, bool disable)
2631{
2632 struct fc_lport *lport = vport->dd_data;
2633
2634 if (disable) {
2635 fc_vport_set_state(vport, FC_VPORT_DISABLED);
2636 fc_fabric_logoff(lport);
2637 } else {
2638 lport->boot_time = jiffies;
2639 fc_fabric_login(lport);
2640 fc_vport_setlink(lport);
2641 }
2642
2643 return 0;
2644}
2645
Chris Leechdc8596d2009-11-03 11:47:18 -08002646/**
2647 * fcoe_vport_set_symbolic_name() - append vport string to symbolic name
2648 * @vport: fc_vport with a new symbolic name string
2649 *
2650 * After generating a new symbolic name string, a new RSPN_ID request is
2651 * sent to the name server. There is no response handler, so if it fails
2652 * for some reason it will not be retried.
2653 */
2654static void fcoe_set_vport_symbolic_name(struct fc_vport *vport)
2655{
2656 struct fc_lport *lport = vport->dd_data;
2657 struct fc_frame *fp;
2658 size_t len;
2659
2660 snprintf(fc_host_symbolic_name(lport->host), FC_SYMBOLIC_NAME_SIZE,
2661 "%s v%s over %s : %s", FCOE_NAME, FCOE_VERSION,
2662 fcoe_netdev(lport)->name, vport->symbolic_name);
2663
2664 if (lport->state != LPORT_ST_READY)
2665 return;
2666
2667 len = strnlen(fc_host_symbolic_name(lport->host), 255);
2668 fp = fc_frame_alloc(lport,
2669 sizeof(struct fc_ct_hdr) +
2670 sizeof(struct fc_ns_rspn) + len);
2671 if (!fp)
2672 return;
2673 lport->tt.elsct_send(lport, FC_FID_DIR_SERV, fp, FC_NS_RSPN_ID,
Joe Eykholtb94f8952009-11-03 11:50:21 -08002674 NULL, NULL, 3 * lport->r_a_tov);
Chris Leechdc8596d2009-11-03 11:47:18 -08002675}
Yi Zoub84056b2009-11-20 14:55:19 -08002676
2677/**
2678 * fcoe_get_lesb() - Fill the FCoE Link Error Status Block
2679 * @lport: the local port
2680 * @fc_lesb: the link error status block
2681 */
2682static void fcoe_get_lesb(struct fc_lport *lport,
2683 struct fc_els_lesb *fc_lesb)
2684{
2685 unsigned int cpu;
2686 u32 lfc, vlfc, mdac;
2687 struct fcoe_dev_stats *devst;
2688 struct fcoe_fc_els_lesb *lesb;
Eric Dumazet28172732010-07-07 14:58:56 -07002689 struct rtnl_link_stats64 temp;
Yi Zoub84056b2009-11-20 14:55:19 -08002690 struct net_device *netdev = fcoe_netdev(lport);
2691
2692 lfc = 0;
2693 vlfc = 0;
2694 mdac = 0;
2695 lesb = (struct fcoe_fc_els_lesb *)fc_lesb;
2696 memset(lesb, 0, sizeof(*lesb));
2697 for_each_possible_cpu(cpu) {
2698 devst = per_cpu_ptr(lport->dev_stats, cpu);
2699 lfc += devst->LinkFailureCount;
2700 vlfc += devst->VLinkFailureCount;
2701 mdac += devst->MissDiscAdvCount;
2702 }
2703 lesb->lesb_link_fail = htonl(lfc);
2704 lesb->lesb_vlink_fail = htonl(vlfc);
2705 lesb->lesb_miss_fka = htonl(mdac);
Eric Dumazet28172732010-07-07 14:58:56 -07002706 lesb->lesb_fcs_error = htonl(dev_get_stats(netdev, &temp)->rx_crc_errors);
Yi Zoub84056b2009-11-20 14:55:19 -08002707}
Joe Eykholt7d65b0d2010-03-12 16:08:02 -08002708
2709/**
2710 * fcoe_set_port_id() - Callback from libfc when Port_ID is set.
2711 * @lport: the local port
2712 * @port_id: the port ID
2713 * @fp: the received frame, if any, that caused the port_id to be set.
2714 *
2715 * This routine handles the case where we received a FLOGI and are
2716 * entering point-to-point mode. We need to call fcoe_ctlr_recv_flogi()
2717 * so it can set the non-mapped mode and gateway address.
2718 *
2719 * The FLOGI LS_ACC is handled by fcoe_flogi_resp().
2720 */
2721static void fcoe_set_port_id(struct fc_lport *lport,
2722 u32 port_id, struct fc_frame *fp)
2723{
2724 struct fcoe_port *port = lport_priv(lport);
2725 struct fcoe_interface *fcoe = port->fcoe;
2726
2727 if (fp && fc_frame_payload_op(fp) == ELS_FLOGI)
2728 fcoe_ctlr_recv_flogi(&fcoe->ctlr, lport, fp);
2729}