blob: d37d5739799ca52f41973a8844bbd69901c1f7fe [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>
29#include <linux/cpu.h>
30#include <linux/fs.h>
31#include <linux/sysfs.h>
32#include <linux/ctype.h>
33#include <scsi/scsi_tcq.h>
34#include <scsi/scsicam.h>
35#include <scsi/scsi_transport.h>
36#include <scsi/scsi_transport_fc.h>
37#include <net/rtnetlink.h>
38
39#include <scsi/fc/fc_encaps.h>
Joe Eykholt97c83892009-03-17 11:42:40 -070040#include <scsi/fc/fc_fip.h>
Robert Love85b4aa42008-12-09 15:10:24 -080041
42#include <scsi/libfc.h>
43#include <scsi/fc_frame.h>
44#include <scsi/libfcoe.h>
Robert Love85b4aa42008-12-09 15:10:24 -080045
Vasu Devfdd78022009-03-17 11:42:24 -070046#include "fcoe.h"
47
Robert Love85b4aa42008-12-09 15:10:24 -080048MODULE_AUTHOR("Open-FCoE.org");
49MODULE_DESCRIPTION("FCoE");
Vasu Dev9b34ecf2009-03-17 11:42:13 -070050MODULE_LICENSE("GPL v2");
Robert Love85b4aa42008-12-09 15:10:24 -080051
Yi Zou05cc7392009-08-25 13:59:03 -070052/* Performance tuning parameters for fcoe */
53static unsigned int fcoe_ddp_min;
54module_param_named(ddp_min, fcoe_ddp_min, uint, S_IRUGO | S_IWUSR);
55MODULE_PARM_DESC(ddp_min, "Minimum I/O size in bytes for " \
56 "Direct Data Placement (DDP).");
57
Chris Leechdfc1d0f2009-08-25 14:00:13 -070058DEFINE_MUTEX(fcoe_config_mutex);
59
Joe Eykholte7a51992009-08-25 14:04:08 -070060/* fcoe_percpu_clean completion. Waiter protected by fcoe_create_mutex */
61static DECLARE_COMPLETION(fcoe_flush_completion);
62
Robert Love85b4aa42008-12-09 15:10:24 -080063/* fcoe host list */
Chris Leech090eb6c2009-08-25 14:00:28 -070064/* must only by accessed under the RTNL mutex */
Robert Love85b4aa42008-12-09 15:10:24 -080065LIST_HEAD(fcoe_hostlist);
Robert Love5e5e92d2009-03-17 11:41:35 -070066DEFINE_PER_CPU(struct fcoe_percpu_s, fcoe_percpu);
Robert Love85b4aa42008-12-09 15:10:24 -080067
Chris Leechdd3fd722009-04-21 16:27:36 -070068/* Function Prototypes */
Vasu Devfdd78022009-03-17 11:42:24 -070069static int fcoe_reset(struct Scsi_Host *shost);
70static int fcoe_xmit(struct fc_lport *, struct fc_frame *);
71static int fcoe_rcv(struct sk_buff *, struct net_device *,
72 struct packet_type *, struct net_device *);
73static int fcoe_percpu_receive_thread(void *arg);
74static void fcoe_clean_pending_queue(struct fc_lport *lp);
75static void fcoe_percpu_clean(struct fc_lport *lp);
76static int fcoe_link_ok(struct fc_lport *lp);
77
78static struct fc_lport *fcoe_hostlist_lookup(const struct net_device *);
79static int fcoe_hostlist_add(const struct fc_lport *);
Vasu Devfdd78022009-03-17 11:42:24 -070080
Vasu Dev4bb6b512009-05-06 10:52:34 -070081static void fcoe_check_wait_queue(struct fc_lport *, struct sk_buff *);
Robert Love85b4aa42008-12-09 15:10:24 -080082static int fcoe_device_notification(struct notifier_block *, ulong, void *);
83static void fcoe_dev_setup(void);
84static void fcoe_dev_cleanup(void);
Chris Leech2e70e242009-08-25 14:00:23 -070085static struct fcoe_interface *
86 fcoe_hostlist_lookup_port(const struct net_device *dev);
Robert Love85b4aa42008-12-09 15:10:24 -080087
88/* notification function from net device */
89static struct notifier_block fcoe_notifier = {
90 .notifier_call = fcoe_device_notification,
91};
92
Chris Leeche9084bb2009-11-03 11:46:34 -080093static struct scsi_transport_template *fcoe_transport_template;
94static struct scsi_transport_template *fcoe_vport_transport_template;
Vasu Dev7f349142009-03-27 09:06:31 -070095
96struct fc_function_template fcoe_transport_function = {
97 .show_host_node_name = 1,
98 .show_host_port_name = 1,
99 .show_host_supported_classes = 1,
100 .show_host_supported_fc4s = 1,
101 .show_host_active_fc4s = 1,
102 .show_host_maxframe_size = 1,
103
104 .show_host_port_id = 1,
105 .show_host_supported_speeds = 1,
106 .get_host_speed = fc_get_host_speed,
107 .show_host_speed = 1,
108 .show_host_port_type = 1,
109 .get_host_port_state = fc_get_host_port_state,
110 .show_host_port_state = 1,
111 .show_host_symbolic_name = 1,
112
113 .dd_fcrport_size = sizeof(struct fc_rport_libfc_priv),
114 .show_rport_maxframe_size = 1,
115 .show_rport_supported_classes = 1,
116
117 .show_host_fabric_name = 1,
118 .show_starget_node_name = 1,
119 .show_starget_port_name = 1,
120 .show_starget_port_id = 1,
121 .set_rport_dev_loss_tmo = fc_set_rport_loss_tmo,
122 .show_rport_dev_loss_tmo = 1,
123 .get_fc_host_stats = fc_get_host_stats,
124 .issue_fc_host_lip = fcoe_reset,
125
126 .terminate_rport_io = fc_rport_terminate_io,
127};
128
Chris Leeche9084bb2009-11-03 11:46:34 -0800129struct fc_function_template fcoe_vport_transport_function = {
130 .show_host_node_name = 1,
131 .show_host_port_name = 1,
132 .show_host_supported_classes = 1,
133 .show_host_supported_fc4s = 1,
134 .show_host_active_fc4s = 1,
135 .show_host_maxframe_size = 1,
136
137 .show_host_port_id = 1,
138 .show_host_supported_speeds = 1,
139 .get_host_speed = fc_get_host_speed,
140 .show_host_speed = 1,
141 .show_host_port_type = 1,
142 .get_host_port_state = fc_get_host_port_state,
143 .show_host_port_state = 1,
144 .show_host_symbolic_name = 1,
145
146 .dd_fcrport_size = sizeof(struct fc_rport_libfc_priv),
147 .show_rport_maxframe_size = 1,
148 .show_rport_supported_classes = 1,
149
150 .show_host_fabric_name = 1,
151 .show_starget_node_name = 1,
152 .show_starget_port_name = 1,
153 .show_starget_port_id = 1,
154 .set_rport_dev_loss_tmo = fc_set_rport_loss_tmo,
155 .show_rport_dev_loss_tmo = 1,
156 .get_fc_host_stats = fc_get_host_stats,
157 .issue_fc_host_lip = fcoe_reset,
158
159 .terminate_rport_io = fc_rport_terminate_io,
160};
161
Vasu Dev7f349142009-03-27 09:06:31 -0700162static struct scsi_host_template fcoe_shost_template = {
163 .module = THIS_MODULE,
164 .name = "FCoE Driver",
165 .proc_name = FCOE_NAME,
166 .queuecommand = fc_queuecommand,
167 .eh_abort_handler = fc_eh_abort,
168 .eh_device_reset_handler = fc_eh_device_reset,
169 .eh_host_reset_handler = fc_eh_host_reset,
170 .slave_alloc = fc_slave_alloc,
171 .change_queue_depth = fc_change_queue_depth,
172 .change_queue_type = fc_change_queue_type,
173 .this_id = -1,
Vasu Dev14caf442009-10-15 17:46:55 -0700174 .cmd_per_lun = 3,
Vasu Dev7f349142009-03-27 09:06:31 -0700175 .can_queue = FCOE_MAX_OUTSTANDING_COMMANDS,
176 .use_clustering = ENABLE_CLUSTERING,
177 .sg_tablesize = SG_ALL,
178 .max_sectors = 0xffff,
179};
180
Chris Leech54b649f2009-08-25 14:00:07 -0700181static int fcoe_fip_recv(struct sk_buff *skb, struct net_device *dev,
182 struct packet_type *ptype,
183 struct net_device *orig_dev);
184/**
185 * fcoe_interface_setup()
186 * @fcoe: new fcoe_interface
187 * @netdev : ptr to the associated netdevice struct
188 *
189 * Returns : 0 for success
Chris Leech2e70e242009-08-25 14:00:23 -0700190 * Locking: must be called with the RTNL mutex held
Chris Leech54b649f2009-08-25 14:00:07 -0700191 */
192static int fcoe_interface_setup(struct fcoe_interface *fcoe,
193 struct net_device *netdev)
194{
195 struct fcoe_ctlr *fip = &fcoe->ctlr;
196 struct netdev_hw_addr *ha;
197 u8 flogi_maddr[ETH_ALEN];
Yi Zoub7a727f2009-10-21 16:28:03 -0700198 const struct net_device_ops *ops;
Chris Leech54b649f2009-08-25 14:00:07 -0700199
200 fcoe->netdev = netdev;
201
Yi Zoub7a727f2009-10-21 16:28:03 -0700202 /* Let LLD initialize for FCoE */
203 ops = netdev->netdev_ops;
204 if (ops->ndo_fcoe_enable) {
205 if (ops->ndo_fcoe_enable(netdev))
206 FCOE_NETDEV_DBG(netdev, "Failed to enable FCoE"
207 " specific feature for LLD.\n");
208 }
209
Chris Leech54b649f2009-08-25 14:00:07 -0700210 /* Do not support for bonding device */
211 if ((netdev->priv_flags & IFF_MASTER_ALB) ||
212 (netdev->priv_flags & IFF_SLAVE_INACTIVE) ||
213 (netdev->priv_flags & IFF_MASTER_8023AD)) {
214 return -EOPNOTSUPP;
215 }
216
217 /* look for SAN MAC address, if multiple SAN MACs exist, only
218 * use the first one for SPMA */
219 rcu_read_lock();
220 for_each_dev_addr(netdev, ha) {
221 if ((ha->type == NETDEV_HW_ADDR_T_SAN) &&
222 (is_valid_ether_addr(fip->ctl_src_addr))) {
223 memcpy(fip->ctl_src_addr, ha->addr, ETH_ALEN);
224 fip->spma = 1;
225 break;
226 }
227 }
228 rcu_read_unlock();
229
230 /* setup Source Mac Address */
231 if (!fip->spma)
232 memcpy(fip->ctl_src_addr, netdev->dev_addr, netdev->addr_len);
233
234 /*
235 * Add FCoE MAC address as second unicast MAC address
236 * or enter promiscuous mode if not capable of listening
237 * for multiple unicast MACs.
238 */
Chris Leech54b649f2009-08-25 14:00:07 -0700239 memcpy(flogi_maddr, (u8[6]) FC_FCOE_FLOGI_MAC, ETH_ALEN);
240 dev_unicast_add(netdev, flogi_maddr);
241 if (fip->spma)
242 dev_unicast_add(netdev, fip->ctl_src_addr);
243 dev_mc_add(netdev, FIP_ALL_ENODE_MACS, ETH_ALEN, 0);
Chris Leech54b649f2009-08-25 14:00:07 -0700244
245 /*
246 * setup the receive function from ethernet driver
247 * on the ethertype for the given device
248 */
249 fcoe->fcoe_packet_type.func = fcoe_rcv;
250 fcoe->fcoe_packet_type.type = __constant_htons(ETH_P_FCOE);
251 fcoe->fcoe_packet_type.dev = netdev;
252 dev_add_pack(&fcoe->fcoe_packet_type);
253
254 fcoe->fip_packet_type.func = fcoe_fip_recv;
255 fcoe->fip_packet_type.type = htons(ETH_P_FIP);
256 fcoe->fip_packet_type.dev = netdev;
257 dev_add_pack(&fcoe->fip_packet_type);
258
259 return 0;
260}
261
262static void fcoe_fip_send(struct fcoe_ctlr *fip, struct sk_buff *skb);
Chris Leech11b56182009-11-03 11:46:29 -0800263static void fcoe_update_src_mac(struct fc_lport *lport, u8 *addr);
264static u8 *fcoe_get_src_mac(struct fc_lport *lport);
Chris Leech2e70e242009-08-25 14:00:23 -0700265static void fcoe_destroy_work(struct work_struct *work);
Chris Leech54b649f2009-08-25 14:00:07 -0700266
Vasu Dev7f349142009-03-27 09:06:31 -0700267/**
Chris Leech030f4e02009-08-25 14:00:02 -0700268 * fcoe_interface_create()
269 * @netdev: network interface
270 *
271 * Returns: pointer to a struct fcoe_interface or NULL on error
272 */
273static struct fcoe_interface *fcoe_interface_create(struct net_device *netdev)
274{
275 struct fcoe_interface *fcoe;
276
277 fcoe = kzalloc(sizeof(*fcoe), GFP_KERNEL);
278 if (!fcoe) {
279 FCOE_NETDEV_DBG(netdev, "Could not allocate fcoe structure\n");
280 return NULL;
281 }
282
Chris Leech2e70e242009-08-25 14:00:23 -0700283 dev_hold(netdev);
Chris Leech030f4e02009-08-25 14:00:02 -0700284 kref_init(&fcoe->kref);
Chris Leech54b649f2009-08-25 14:00:07 -0700285
286 /*
287 * Initialize FIP.
288 */
289 fcoe_ctlr_init(&fcoe->ctlr);
290 fcoe->ctlr.send = fcoe_fip_send;
291 fcoe->ctlr.update_mac = fcoe_update_src_mac;
Chris Leech11b56182009-11-03 11:46:29 -0800292 fcoe->ctlr.get_src_addr = fcoe_get_src_mac;
Chris Leech54b649f2009-08-25 14:00:07 -0700293
294 fcoe_interface_setup(fcoe, netdev);
Chris Leech030f4e02009-08-25 14:00:02 -0700295
296 return fcoe;
297}
298
299/**
Chris Leech54b649f2009-08-25 14:00:07 -0700300 * fcoe_interface_cleanup() - clean up netdev configurations
301 * @fcoe:
Chris Leech2e70e242009-08-25 14:00:23 -0700302 *
303 * Caller must be holding the RTNL mutex
Chris Leech54b649f2009-08-25 14:00:07 -0700304 */
305void fcoe_interface_cleanup(struct fcoe_interface *fcoe)
306{
307 struct net_device *netdev = fcoe->netdev;
308 struct fcoe_ctlr *fip = &fcoe->ctlr;
309 u8 flogi_maddr[ETH_ALEN];
Yi Zoub7a727f2009-10-21 16:28:03 -0700310 const struct net_device_ops *ops;
Chris Leech54b649f2009-08-25 14:00:07 -0700311
312 /*
313 * Don't listen for Ethernet packets anymore.
314 * synchronize_net() ensures that the packet handlers are not running
315 * on another CPU. dev_remove_pack() would do that, this calls the
316 * unsyncronized version __dev_remove_pack() to avoid multiple delays.
317 */
318 __dev_remove_pack(&fcoe->fcoe_packet_type);
319 __dev_remove_pack(&fcoe->fip_packet_type);
320 synchronize_net();
321
Chris Leech54b649f2009-08-25 14:00:07 -0700322 /* Delete secondary MAC addresses */
Chris Leech54b649f2009-08-25 14:00:07 -0700323 memcpy(flogi_maddr, (u8[6]) FC_FCOE_FLOGI_MAC, ETH_ALEN);
324 dev_unicast_delete(netdev, flogi_maddr);
Chris Leech54b649f2009-08-25 14:00:07 -0700325 if (fip->spma)
326 dev_unicast_delete(netdev, fip->ctl_src_addr);
327 dev_mc_delete(netdev, FIP_ALL_ENODE_MACS, ETH_ALEN, 0);
Yi Zoub7a727f2009-10-21 16:28:03 -0700328
329 /* Tell the LLD we are done w/ FCoE */
330 ops = netdev->netdev_ops;
331 if (ops->ndo_fcoe_disable) {
332 if (ops->ndo_fcoe_disable(netdev))
333 FCOE_NETDEV_DBG(netdev, "Failed to disable FCoE"
334 " specific feature for LLD.\n");
335 }
Chris Leech54b649f2009-08-25 14:00:07 -0700336}
337
338/**
Chris Leech030f4e02009-08-25 14:00:02 -0700339 * fcoe_interface_release() - fcoe_port kref release function
340 * @kref: embedded reference count in an fcoe_interface struct
341 */
342static void fcoe_interface_release(struct kref *kref)
343{
344 struct fcoe_interface *fcoe;
Chris Leech2e70e242009-08-25 14:00:23 -0700345 struct net_device *netdev;
Chris Leech030f4e02009-08-25 14:00:02 -0700346
347 fcoe = container_of(kref, struct fcoe_interface, kref);
Chris Leech2e70e242009-08-25 14:00:23 -0700348 netdev = fcoe->netdev;
349 /* tear-down the FCoE controller */
350 fcoe_ctlr_destroy(&fcoe->ctlr);
Chris Leech030f4e02009-08-25 14:00:02 -0700351 kfree(fcoe);
Chris Leech2e70e242009-08-25 14:00:23 -0700352 dev_put(netdev);
Chris Leech030f4e02009-08-25 14:00:02 -0700353}
354
355/**
356 * fcoe_interface_get()
357 * @fcoe:
358 */
359static inline void fcoe_interface_get(struct fcoe_interface *fcoe)
360{
361 kref_get(&fcoe->kref);
362}
363
364/**
365 * fcoe_interface_put()
366 * @fcoe:
367 */
368static inline void fcoe_interface_put(struct fcoe_interface *fcoe)
369{
370 kref_put(&fcoe->kref, fcoe_interface_release);
371}
372
373/**
Vasu Devab6b85c2009-05-17 12:33:08 +0000374 * fcoe_fip_recv - handle a received FIP frame.
375 * @skb: the receive skb
376 * @dev: associated &net_device
377 * @ptype: the &packet_type structure which was used to register this handler.
378 * @orig_dev: original receive &net_device, in case @dev is a bond.
379 *
380 * Returns: 0 for success
381 */
382static int fcoe_fip_recv(struct sk_buff *skb, struct net_device *dev,
383 struct packet_type *ptype,
384 struct net_device *orig_dev)
385{
Chris Leech259ad852009-08-25 13:59:41 -0700386 struct fcoe_interface *fcoe;
Vasu Devab6b85c2009-05-17 12:33:08 +0000387
Chris Leech259ad852009-08-25 13:59:41 -0700388 fcoe = container_of(ptype, struct fcoe_interface, fip_packet_type);
Chris Leech3fe9a0b2009-08-25 13:59:46 -0700389 fcoe_ctlr_recv(&fcoe->ctlr, skb);
Vasu Devab6b85c2009-05-17 12:33:08 +0000390 return 0;
391}
392
393/**
394 * fcoe_fip_send() - send an Ethernet-encapsulated FIP frame.
395 * @fip: FCoE controller.
396 * @skb: FIP Packet.
397 */
398static void fcoe_fip_send(struct fcoe_ctlr *fip, struct sk_buff *skb)
399{
Chris Leech3fe9a0b2009-08-25 13:59:46 -0700400 skb->dev = fcoe_from_ctlr(fip)->netdev;
Vasu Devab6b85c2009-05-17 12:33:08 +0000401 dev_queue_xmit(skb);
402}
403
404/**
405 * fcoe_update_src_mac() - Update Ethernet MAC filters.
Chris Leech11b56182009-11-03 11:46:29 -0800406 * @lport: libfc lport
407 * @addr: Unicast MAC address to add.
Vasu Devab6b85c2009-05-17 12:33:08 +0000408 *
409 * Remove any previously-set unicast MAC filter.
410 * Add secondary FCoE MAC address filter for our OUI.
411 */
Chris Leech11b56182009-11-03 11:46:29 -0800412static void fcoe_update_src_mac(struct fc_lport *lport, u8 *addr)
Vasu Devab6b85c2009-05-17 12:33:08 +0000413{
Chris Leech11b56182009-11-03 11:46:29 -0800414 struct fcoe_port *port = lport_priv(lport);
415 struct fcoe_interface *fcoe = port->fcoe;
Vasu Devab6b85c2009-05-17 12:33:08 +0000416
Vasu Devab6b85c2009-05-17 12:33:08 +0000417 rtnl_lock();
Chris Leech11b56182009-11-03 11:46:29 -0800418 if (!is_zero_ether_addr(port->data_src_addr))
419 dev_unicast_delete(fcoe->netdev, port->data_src_addr);
420 if (!is_zero_ether_addr(addr))
421 dev_unicast_add(fcoe->netdev, addr);
422 memcpy(port->data_src_addr, addr, ETH_ALEN);
Vasu Devab6b85c2009-05-17 12:33:08 +0000423 rtnl_unlock();
424}
425
426/**
Chris Leech11b56182009-11-03 11:46:29 -0800427 * fcoe_get_src_mac() - return the Ethernet source address for an lport
428 * @lport: libfc lport
429 */
430static u8 *fcoe_get_src_mac(struct fc_lport *lport)
431{
432 struct fcoe_port *port = lport_priv(lport);
433
434 return port->data_src_addr;
435}
436
437/**
Vasu Dev7f349142009-03-27 09:06:31 -0700438 * fcoe_lport_config() - sets up the fc_lport
439 * @lp: ptr to the fc_lport
Vasu Dev7f349142009-03-27 09:06:31 -0700440 *
441 * Returns: 0 for success
442 */
443static int fcoe_lport_config(struct fc_lport *lp)
444{
445 lp->link_up = 0;
446 lp->qfull = 0;
447 lp->max_retry_count = 3;
Abhijeet Joglekara3666952009-05-01 10:01:26 -0700448 lp->max_rport_retry_count = 3;
Vasu Dev7f349142009-03-27 09:06:31 -0700449 lp->e_d_tov = 2 * 1000; /* FC-FS default */
450 lp->r_a_tov = 2 * 2 * 1000;
451 lp->service_params = (FCP_SPPF_INIT_FCN | FCP_SPPF_RD_XRDY_DIS |
452 FCP_SPPF_RETRY | FCP_SPPF_CONF_COMPL);
453
454 fc_lport_init_stats(lp);
455
456 /* lport fc_lport related configuration */
457 fc_lport_config(lp);
458
459 /* offload related configuration */
460 lp->crc_offload = 0;
461 lp->seq_offload = 0;
462 lp->lro_enabled = 0;
463 lp->lro_xid = 0;
464 lp->lso_max = 0;
465
466 return 0;
467}
468
469/**
Vasu Dev1047f222009-05-06 10:52:40 -0700470 * fcoe_queue_timer() - fcoe queue timer
471 * @lp: the fc_lport pointer
472 *
473 * Calls fcoe_check_wait_queue on timeout
474 *
475 */
476static void fcoe_queue_timer(ulong lp)
477{
478 fcoe_check_wait_queue((struct fc_lport *)lp, NULL);
479}
480
481/**
Vasu Dev7f349142009-03-27 09:06:31 -0700482 * fcoe_netdev_config() - Set up netdev for SW FCoE
483 * @lp : ptr to the fc_lport
484 * @netdev : ptr to the associated netdevice struct
485 *
486 * Must be called after fcoe_lport_config() as it will use lport mutex
487 *
488 * Returns : 0 for success
489 */
490static int fcoe_netdev_config(struct fc_lport *lp, struct net_device *netdev)
491{
492 u32 mfs;
493 u64 wwnn, wwpn;
Chris Leech25024982009-08-25 13:59:35 -0700494 struct fcoe_interface *fcoe;
Chris Leech014f5c32009-08-25 13:59:30 -0700495 struct fcoe_port *port;
Vasu Dev7f349142009-03-27 09:06:31 -0700496
497 /* Setup lport private data to point to fcoe softc */
Chris Leech014f5c32009-08-25 13:59:30 -0700498 port = lport_priv(lp);
Chris Leech25024982009-08-25 13:59:35 -0700499 fcoe = port->fcoe;
Vasu Dev7f349142009-03-27 09:06:31 -0700500
501 /*
502 * Determine max frame size based on underlying device and optional
503 * user-configured limit. If the MFS is too low, fcoe_link_ok()
504 * will return 0, so do this first.
505 */
Yi Zou7221d7e2009-10-21 16:27:52 -0700506 mfs = netdev->mtu;
507 if (netdev->features & NETIF_F_FCOE_MTU) {
508 mfs = FCOE_MTU;
509 FCOE_NETDEV_DBG(netdev, "Supports FCOE_MTU of %d bytes\n", mfs);
510 }
511 mfs -= (sizeof(struct fcoe_hdr) + sizeof(struct fcoe_crc_eof));
Vasu Dev7f349142009-03-27 09:06:31 -0700512 if (fc_set_mfs(lp, mfs))
513 return -EINVAL;
514
Vasu Dev7f349142009-03-27 09:06:31 -0700515 /* offload features support */
Vasu Dev1d1b88d2009-07-29 17:05:45 -0700516 if (netdev->features & NETIF_F_SG)
Vasu Dev7f349142009-03-27 09:06:31 -0700517 lp->sg_supp = 1;
518
Vasu Dev7f349142009-03-27 09:06:31 -0700519 if (netdev->features & NETIF_F_FCOE_CRC) {
520 lp->crc_offload = 1;
Robert Loved5488eb2009-06-10 15:30:59 -0700521 FCOE_NETDEV_DBG(netdev, "Supports FCCRC offload\n");
Vasu Dev7f349142009-03-27 09:06:31 -0700522 }
Vasu Dev7f349142009-03-27 09:06:31 -0700523 if (netdev->features & NETIF_F_FSO) {
524 lp->seq_offload = 1;
525 lp->lso_max = netdev->gso_max_size;
Robert Loved5488eb2009-06-10 15:30:59 -0700526 FCOE_NETDEV_DBG(netdev, "Supports LSO for max len 0x%x\n",
527 lp->lso_max);
Vasu Dev7f349142009-03-27 09:06:31 -0700528 }
Vasu Dev7f349142009-03-27 09:06:31 -0700529 if (netdev->fcoe_ddp_xid) {
530 lp->lro_enabled = 1;
531 lp->lro_xid = netdev->fcoe_ddp_xid;
Robert Loved5488eb2009-06-10 15:30:59 -0700532 FCOE_NETDEV_DBG(netdev, "Supports LRO for max xid 0x%x\n",
533 lp->lro_xid);
Vasu Dev7f349142009-03-27 09:06:31 -0700534 }
Chris Leech014f5c32009-08-25 13:59:30 -0700535 skb_queue_head_init(&port->fcoe_pending_queue);
536 port->fcoe_pending_queue_active = 0;
537 setup_timer(&port->timer, fcoe_queue_timer, (unsigned long)lp);
Vasu Dev7f349142009-03-27 09:06:31 -0700538
Vasu Dev1d1b88d2009-07-29 17:05:45 -0700539 wwnn = fcoe_wwn_from_mac(netdev->dev_addr, 1, 0);
Vasu Dev7f349142009-03-27 09:06:31 -0700540 fc_set_wwnn(lp, wwnn);
541 /* XXX - 3rd arg needs to be vlan id */
Vasu Dev1d1b88d2009-07-29 17:05:45 -0700542 wwpn = fcoe_wwn_from_mac(netdev->dev_addr, 2, 0);
Vasu Dev7f349142009-03-27 09:06:31 -0700543 fc_set_wwpn(lp, wwpn);
544
Vasu Dev7f349142009-03-27 09:06:31 -0700545 return 0;
546}
547
548/**
549 * fcoe_shost_config() - Sets up fc_lport->host
550 * @lp : ptr to the fc_lport
551 * @shost : ptr to the associated scsi host
552 * @dev : device associated to scsi host
553 *
554 * Must be called after fcoe_lport_config() and fcoe_netdev_config()
555 *
556 * Returns : 0 for success
557 */
558static int fcoe_shost_config(struct fc_lport *lp, struct Scsi_Host *shost,
559 struct device *dev)
560{
561 int rc = 0;
562
563 /* lport scsi host config */
Vasu Dev7f349142009-03-27 09:06:31 -0700564 lp->host->max_lun = FCOE_MAX_LUN;
565 lp->host->max_id = FCOE_MAX_FCP_TARGET;
566 lp->host->max_channel = 0;
Chris Leeche9084bb2009-11-03 11:46:34 -0800567 if (lp->vport)
568 lp->host->transportt = fcoe_vport_transport_template;
569 else
570 lp->host->transportt = fcoe_transport_template;
Vasu Dev7f349142009-03-27 09:06:31 -0700571
572 /* add the new host to the SCSI-ml */
573 rc = scsi_add_host(lp->host, dev);
574 if (rc) {
Robert Loved5488eb2009-06-10 15:30:59 -0700575 FCOE_NETDEV_DBG(fcoe_netdev(lp), "fcoe_shost_config: "
576 "error on scsi_add_host\n");
Vasu Dev7f349142009-03-27 09:06:31 -0700577 return rc;
578 }
579 sprintf(fc_host_symbolic_name(lp->host), "%s v%s over %s",
580 FCOE_NAME, FCOE_VERSION,
581 fcoe_netdev(lp)->name);
582
583 return 0;
584}
585
Vasu Devd7179682009-07-29 17:05:21 -0700586/*
587 * fcoe_oem_match() - match for read types IO
588 * @fp: the fc_frame for new IO.
589 *
590 * Returns : true for read types IO, otherwise returns false.
591 */
592bool fcoe_oem_match(struct fc_frame *fp)
593{
Yi Zou05cc7392009-08-25 13:59:03 -0700594 return fc_fcp_is_read(fr_fsp(fp)) &&
595 (fr_fsp(fp)->data_len > fcoe_ddp_min);
Vasu Devd7179682009-07-29 17:05:21 -0700596}
597
Vasu Dev7f349142009-03-27 09:06:31 -0700598/**
599 * fcoe_em_config() - allocates em for this lport
Chris Leech014f5c32009-08-25 13:59:30 -0700600 * @lp: the fcoe that em is to allocated for
Vasu Dev7f349142009-03-27 09:06:31 -0700601 *
602 * Returns : 0 on success
603 */
604static inline int fcoe_em_config(struct fc_lport *lp)
605{
Chris Leech014f5c32009-08-25 13:59:30 -0700606 struct fcoe_port *port = lport_priv(lp);
Chris Leech25024982009-08-25 13:59:35 -0700607 struct fcoe_interface *fcoe = port->fcoe;
608 struct fcoe_interface *oldfcoe = NULL;
Vasu Dev1d1b88d2009-07-29 17:05:45 -0700609 struct net_device *old_real_dev, *cur_real_dev;
Vasu Devd7179682009-07-29 17:05:21 -0700610 u16 min_xid = FCOE_MIN_XID;
611 u16 max_xid = FCOE_MAX_XID;
612
613 /*
614 * Check if need to allocate an em instance for
615 * offload exchange ids to be shared across all VN_PORTs/lport.
616 */
617 if (!lp->lro_enabled || !lp->lro_xid || (lp->lro_xid >= max_xid)) {
618 lp->lro_xid = 0;
619 goto skip_oem;
620 }
621
622 /*
623 * Reuse existing offload em instance in case
Vasu Dev1d1b88d2009-07-29 17:05:45 -0700624 * it is already allocated on real eth device
Vasu Devd7179682009-07-29 17:05:21 -0700625 */
Chris Leech25024982009-08-25 13:59:35 -0700626 if (fcoe->netdev->priv_flags & IFF_802_1Q_VLAN)
627 cur_real_dev = vlan_dev_real_dev(fcoe->netdev);
Vasu Dev1d1b88d2009-07-29 17:05:45 -0700628 else
Chris Leech25024982009-08-25 13:59:35 -0700629 cur_real_dev = fcoe->netdev;
Vasu Dev1d1b88d2009-07-29 17:05:45 -0700630
Chris Leech25024982009-08-25 13:59:35 -0700631 list_for_each_entry(oldfcoe, &fcoe_hostlist, list) {
Chris Leech25024982009-08-25 13:59:35 -0700632 if (oldfcoe->netdev->priv_flags & IFF_802_1Q_VLAN)
633 old_real_dev = vlan_dev_real_dev(oldfcoe->netdev);
Vasu Dev1d1b88d2009-07-29 17:05:45 -0700634 else
Chris Leech25024982009-08-25 13:59:35 -0700635 old_real_dev = oldfcoe->netdev;
Vasu Dev1d1b88d2009-07-29 17:05:45 -0700636
637 if (cur_real_dev == old_real_dev) {
Chris Leech991cbb62009-08-25 13:59:51 -0700638 fcoe->oem = oldfcoe->oem;
Vasu Devd7179682009-07-29 17:05:21 -0700639 break;
640 }
641 }
642
Chris Leech991cbb62009-08-25 13:59:51 -0700643 if (fcoe->oem) {
644 if (!fc_exch_mgr_add(lp, fcoe->oem, fcoe_oem_match)) {
Vasu Devd7179682009-07-29 17:05:21 -0700645 printk(KERN_ERR "fcoe_em_config: failed to add "
646 "offload em:%p on interface:%s\n",
Chris Leech991cbb62009-08-25 13:59:51 -0700647 fcoe->oem, fcoe->netdev->name);
Vasu Devd7179682009-07-29 17:05:21 -0700648 return -ENOMEM;
649 }
650 } else {
Chris Leech991cbb62009-08-25 13:59:51 -0700651 fcoe->oem = fc_exch_mgr_alloc(lp, FC_CLASS_3,
Vasu Devd7179682009-07-29 17:05:21 -0700652 FCOE_MIN_XID, lp->lro_xid,
653 fcoe_oem_match);
Chris Leech991cbb62009-08-25 13:59:51 -0700654 if (!fcoe->oem) {
Vasu Devd7179682009-07-29 17:05:21 -0700655 printk(KERN_ERR "fcoe_em_config: failed to allocate "
656 "em for offload exches on interface:%s\n",
Chris Leech25024982009-08-25 13:59:35 -0700657 fcoe->netdev->name);
Vasu Devd7179682009-07-29 17:05:21 -0700658 return -ENOMEM;
659 }
660 }
661
662 /*
663 * Exclude offload EM xid range from next EM xid range.
664 */
665 min_xid += lp->lro_xid + 1;
666
667skip_oem:
668 if (!fc_exch_mgr_alloc(lp, FC_CLASS_3, min_xid, max_xid, NULL)) {
669 printk(KERN_ERR "fcoe_em_config: failed to "
Chris Leech25024982009-08-25 13:59:35 -0700670 "allocate em on interface %s\n", fcoe->netdev->name);
Vasu Dev7f349142009-03-27 09:06:31 -0700671 return -ENOMEM;
Vasu Devd7179682009-07-29 17:05:21 -0700672 }
Vasu Dev7f349142009-03-27 09:06:31 -0700673
674 return 0;
675}
676
677/**
678 * fcoe_if_destroy() - FCoE software HBA tear-down function
Chris Leechaf7f85d2009-08-25 13:59:24 -0700679 * @lport: fc_lport to destroy
Vasu Dev7f349142009-03-27 09:06:31 -0700680 */
Chris Leechaf7f85d2009-08-25 13:59:24 -0700681static void fcoe_if_destroy(struct fc_lport *lport)
Vasu Dev7f349142009-03-27 09:06:31 -0700682{
Chris Leech014f5c32009-08-25 13:59:30 -0700683 struct fcoe_port *port = lport_priv(lport);
684 struct fcoe_interface *fcoe = port->fcoe;
Chris Leech25024982009-08-25 13:59:35 -0700685 struct net_device *netdev = fcoe->netdev;
Vasu Dev7f349142009-03-27 09:06:31 -0700686
Robert Loved5488eb2009-06-10 15:30:59 -0700687 FCOE_NETDEV_DBG(netdev, "Destroying interface\n");
Vasu Dev7f349142009-03-27 09:06:31 -0700688
Vasu Dev7f349142009-03-27 09:06:31 -0700689 /* Logout of the fabric */
Chris Leechaf7f85d2009-08-25 13:59:24 -0700690 fc_fabric_logoff(lport);
Vasu Dev7f349142009-03-27 09:06:31 -0700691
Vasu Dev7f349142009-03-27 09:06:31 -0700692 /* Cleanup the fc_lport */
Chris Leechaf7f85d2009-08-25 13:59:24 -0700693 fc_lport_destroy(lport);
694 fc_fcp_destroy(lport);
Vasu Dev7f349142009-03-27 09:06:31 -0700695
Chris Leech54b649f2009-08-25 14:00:07 -0700696 /* Stop the transmit retry timer */
697 del_timer_sync(&port->timer);
698
699 /* Free existing transmit skbs */
700 fcoe_clean_pending_queue(lport);
701
Chris Leech11b56182009-11-03 11:46:29 -0800702 rtnl_lock();
703 if (!is_zero_ether_addr(port->data_src_addr))
704 dev_unicast_delete(netdev, port->data_src_addr);
705 rtnl_unlock();
706
Chris Leech54b649f2009-08-25 14:00:07 -0700707 /* receives may not be stopped until after this */
708 fcoe_interface_put(fcoe);
709
710 /* Free queued packets for the per-CPU receive threads */
711 fcoe_percpu_clean(lport);
712
Vasu Dev7f349142009-03-27 09:06:31 -0700713 /* Detach from the scsi-ml */
Chris Leechaf7f85d2009-08-25 13:59:24 -0700714 fc_remove_host(lport->host);
715 scsi_remove_host(lport->host);
Vasu Dev7f349142009-03-27 09:06:31 -0700716
717 /* There are no more rports or I/O, free the EM */
Chris Leechaf7f85d2009-08-25 13:59:24 -0700718 fc_exch_mgr_free(lport);
Vasu Dev7f349142009-03-27 09:06:31 -0700719
Vasu Dev7f349142009-03-27 09:06:31 -0700720 /* Free memory used by statistical counters */
Chris Leechaf7f85d2009-08-25 13:59:24 -0700721 fc_lport_free_stats(lport);
Vasu Dev7f349142009-03-27 09:06:31 -0700722
Chris Leech2e70e242009-08-25 14:00:23 -0700723 /* Release the Scsi_Host */
Chris Leechaf7f85d2009-08-25 13:59:24 -0700724 scsi_host_put(lport->host);
Vasu Dev7f349142009-03-27 09:06:31 -0700725}
726
727/*
728 * fcoe_ddp_setup - calls LLD's ddp_setup through net_device
729 * @lp: the corresponding fc_lport
730 * @xid: the exchange id for this ddp transfer
731 * @sgl: the scatterlist describing this transfer
732 * @sgc: number of sg items
733 *
734 * Returns : 0 no ddp
735 */
736static int fcoe_ddp_setup(struct fc_lport *lp, u16 xid,
737 struct scatterlist *sgl, unsigned int sgc)
738{
739 struct net_device *n = fcoe_netdev(lp);
740
Yi Zoub04d0232009-10-21 16:26:55 -0700741 if (n->netdev_ops->ndo_fcoe_ddp_setup)
Vasu Dev7f349142009-03-27 09:06:31 -0700742 return n->netdev_ops->ndo_fcoe_ddp_setup(n, xid, sgl, sgc);
743
744 return 0;
745}
746
747/*
748 * fcoe_ddp_done - calls LLD's ddp_done through net_device
749 * @lp: the corresponding fc_lport
750 * @xid: the exchange id for this ddp transfer
751 *
752 * Returns : the length of data that have been completed by ddp
753 */
754static int fcoe_ddp_done(struct fc_lport *lp, u16 xid)
755{
756 struct net_device *n = fcoe_netdev(lp);
757
Yi Zoub04d0232009-10-21 16:26:55 -0700758 if (n->netdev_ops->ndo_fcoe_ddp_done)
Vasu Dev7f349142009-03-27 09:06:31 -0700759 return n->netdev_ops->ndo_fcoe_ddp_done(n, xid);
760 return 0;
761}
762
Chris Leech11b56182009-11-03 11:46:29 -0800763static struct fc_seq *fcoe_elsct_send(struct fc_lport *lport,
764 u32 did, struct fc_frame *fp, unsigned int op,
765 void (*resp)(struct fc_seq *, struct fc_frame *, void *),
766 void *arg, u32 timeout);
767
Vasu Dev7f349142009-03-27 09:06:31 -0700768static struct libfc_function_template fcoe_libfc_fcn_templ = {
769 .frame_send = fcoe_xmit,
770 .ddp_setup = fcoe_ddp_setup,
771 .ddp_done = fcoe_ddp_done,
Chris Leech11b56182009-11-03 11:46:29 -0800772 .elsct_send = fcoe_elsct_send,
Vasu Dev7f349142009-03-27 09:06:31 -0700773};
774
775/**
Chris Leech030f4e02009-08-25 14:00:02 -0700776 * fcoe_if_create() - this function creates the fcoe port
777 * @fcoe: fcoe_interface structure to create an fc_lport instance on
Chris Leechaf7f85d2009-08-25 13:59:24 -0700778 * @parent: device pointer to be the parent in sysfs for the SCSI host
Vasu Dev7f349142009-03-27 09:06:31 -0700779 *
Chris Leech54b649f2009-08-25 14:00:07 -0700780 * Creates fc_lport struct and scsi_host for lport, configures lport.
Vasu Dev7f349142009-03-27 09:06:31 -0700781 *
Chris Leechaf7f85d2009-08-25 13:59:24 -0700782 * Returns : The allocated fc_lport or an error pointer
Vasu Dev7f349142009-03-27 09:06:31 -0700783 */
Chris Leech030f4e02009-08-25 14:00:02 -0700784static struct fc_lport *fcoe_if_create(struct fcoe_interface *fcoe,
Chris Leechaf7f85d2009-08-25 13:59:24 -0700785 struct device *parent)
Vasu Dev7f349142009-03-27 09:06:31 -0700786{
787 int rc;
Chris Leechaf7f85d2009-08-25 13:59:24 -0700788 struct fc_lport *lport = NULL;
Chris Leech014f5c32009-08-25 13:59:30 -0700789 struct fcoe_port *port;
Vasu Dev7f349142009-03-27 09:06:31 -0700790 struct Scsi_Host *shost;
Chris Leech030f4e02009-08-25 14:00:02 -0700791 struct net_device *netdev = fcoe->netdev;
Vasu Dev7f349142009-03-27 09:06:31 -0700792
Robert Loved5488eb2009-06-10 15:30:59 -0700793 FCOE_NETDEV_DBG(netdev, "Create Interface\n");
Vasu Dev7f349142009-03-27 09:06:31 -0700794
Chris Leech86221962009-11-03 11:46:08 -0800795 lport = libfc_host_alloc(&fcoe_shost_template,
Chris Leech014f5c32009-08-25 13:59:30 -0700796 sizeof(struct fcoe_port));
Chris Leech86221962009-11-03 11:46:08 -0800797 if (!lport) {
Chris Leech014f5c32009-08-25 13:59:30 -0700798 FCOE_NETDEV_DBG(netdev, "Could not allocate host structure\n");
799 rc = -ENOMEM;
Chris Leech030f4e02009-08-25 14:00:02 -0700800 goto out;
Chris Leech014f5c32009-08-25 13:59:30 -0700801 }
Chris Leech86221962009-11-03 11:46:08 -0800802 shost = lport->host;
Chris Leech014f5c32009-08-25 13:59:30 -0700803 port = lport_priv(lport);
Chris Leech2e70e242009-08-25 14:00:23 -0700804 port->lport = lport;
Chris Leech014f5c32009-08-25 13:59:30 -0700805 port->fcoe = fcoe;
Chris Leech2e70e242009-08-25 14:00:23 -0700806 INIT_WORK(&port->destroy_work, fcoe_destroy_work);
Vasu Dev7f349142009-03-27 09:06:31 -0700807
808 /* configure fc_lport, e.g., em */
Chris Leechaf7f85d2009-08-25 13:59:24 -0700809 rc = fcoe_lport_config(lport);
Vasu Dev7f349142009-03-27 09:06:31 -0700810 if (rc) {
Robert Loved5488eb2009-06-10 15:30:59 -0700811 FCOE_NETDEV_DBG(netdev, "Could not configure lport for the "
812 "interface\n");
Vasu Dev7f349142009-03-27 09:06:31 -0700813 goto out_host_put;
814 }
815
Vasu Devab6b85c2009-05-17 12:33:08 +0000816 /* configure lport network properties */
Chris Leechaf7f85d2009-08-25 13:59:24 -0700817 rc = fcoe_netdev_config(lport, netdev);
Vasu Devab6b85c2009-05-17 12:33:08 +0000818 if (rc) {
Robert Loved5488eb2009-06-10 15:30:59 -0700819 FCOE_NETDEV_DBG(netdev, "Could not configure netdev for the "
820 "interface\n");
Chris Leech54b649f2009-08-25 14:00:07 -0700821 goto out_lp_destroy;
Vasu Devab6b85c2009-05-17 12:33:08 +0000822 }
Joe Eykholt97c83892009-03-17 11:42:40 -0700823
Vasu Dev7f349142009-03-27 09:06:31 -0700824 /* configure lport scsi host properties */
Chris Leechaf7f85d2009-08-25 13:59:24 -0700825 rc = fcoe_shost_config(lport, shost, parent);
Vasu Dev7f349142009-03-27 09:06:31 -0700826 if (rc) {
Robert Loved5488eb2009-06-10 15:30:59 -0700827 FCOE_NETDEV_DBG(netdev, "Could not configure shost for the "
828 "interface\n");
Chris Leech54b649f2009-08-25 14:00:07 -0700829 goto out_lp_destroy;
Vasu Dev7f349142009-03-27 09:06:31 -0700830 }
831
Vasu Dev7f349142009-03-27 09:06:31 -0700832 /* Initialize the library */
Chris Leechaf7f85d2009-08-25 13:59:24 -0700833 rc = fcoe_libfc_config(lport, &fcoe_libfc_fcn_templ);
Vasu Dev7f349142009-03-27 09:06:31 -0700834 if (rc) {
Robert Loved5488eb2009-06-10 15:30:59 -0700835 FCOE_NETDEV_DBG(netdev, "Could not configure libfc for the "
836 "interface\n");
Vasu Dev7f349142009-03-27 09:06:31 -0700837 goto out_lp_destroy;
838 }
839
Vasu Deve8af4d42009-07-29 17:05:15 -0700840 /*
841 * fcoe_em_alloc() and fcoe_hostlist_add() both
Chris Leechc863df32009-08-25 14:00:18 -0700842 * need to be atomic with respect to other changes to the hostlist
Vasu Deve8af4d42009-07-29 17:05:15 -0700843 * since fcoe_em_alloc() looks for an existing EM
844 * instance on host list updated by fcoe_hostlist_add().
Chris Leechc863df32009-08-25 14:00:18 -0700845 *
846 * This is currently handled through the fcoe_config_mutex begin held.
Vasu Deve8af4d42009-07-29 17:05:15 -0700847 */
Chris Leechc863df32009-08-25 14:00:18 -0700848
Vasu Dev96316092009-07-29 17:05:00 -0700849 /* lport exch manager allocation */
Chris Leechaf7f85d2009-08-25 13:59:24 -0700850 rc = fcoe_em_config(lport);
Vasu Dev96316092009-07-29 17:05:00 -0700851 if (rc) {
852 FCOE_NETDEV_DBG(netdev, "Could not configure the EM for the "
853 "interface\n");
854 goto out_lp_destroy;
855 }
856
Chris Leech030f4e02009-08-25 14:00:02 -0700857 fcoe_interface_get(fcoe);
Chris Leechaf7f85d2009-08-25 13:59:24 -0700858 return lport;
Vasu Dev7f349142009-03-27 09:06:31 -0700859
860out_lp_destroy:
Chris Leechaf7f85d2009-08-25 13:59:24 -0700861 fc_exch_mgr_free(lport);
Vasu Dev7f349142009-03-27 09:06:31 -0700862out_host_put:
Chris Leechaf7f85d2009-08-25 13:59:24 -0700863 scsi_host_put(lport->host);
864out:
865 return ERR_PTR(rc);
Vasu Dev7f349142009-03-27 09:06:31 -0700866}
867
868/**
869 * fcoe_if_init() - attach to scsi transport
870 *
871 * Returns : 0 on success
872 */
873static int __init fcoe_if_init(void)
874{
875 /* attach to scsi transport */
Chris Leeche9084bb2009-11-03 11:46:34 -0800876 fcoe_transport_template = fc_attach_transport(&fcoe_transport_function);
877 fcoe_vport_transport_template =
878 fc_attach_transport(&fcoe_vport_transport_function);
Vasu Dev7f349142009-03-27 09:06:31 -0700879
Chris Leeche9084bb2009-11-03 11:46:34 -0800880 if (!fcoe_transport_template) {
Robert Loved5488eb2009-06-10 15:30:59 -0700881 printk(KERN_ERR "fcoe: Failed to attach to the FC transport\n");
Vasu Dev7f349142009-03-27 09:06:31 -0700882 return -ENODEV;
883 }
884
885 return 0;
886}
887
888/**
889 * fcoe_if_exit() - detach from scsi transport
890 *
891 * Returns : 0 on success
892 */
893int __exit fcoe_if_exit(void)
894{
Chris Leeche9084bb2009-11-03 11:46:34 -0800895 fc_release_transport(fcoe_transport_template);
896 fc_release_transport(fcoe_vport_transport_template);
897 fcoe_transport_template = NULL;
898 fcoe_vport_transport_template = NULL;
Vasu Dev7f349142009-03-27 09:06:31 -0700899 return 0;
900}
901
Robert Love85b4aa42008-12-09 15:10:24 -0800902/**
Robert Love8976f422009-03-17 11:41:46 -0700903 * fcoe_percpu_thread_create() - Create a receive thread for an online cpu
904 * @cpu: cpu index for the online cpu
905 */
906static void fcoe_percpu_thread_create(unsigned int cpu)
907{
908 struct fcoe_percpu_s *p;
909 struct task_struct *thread;
910
911 p = &per_cpu(fcoe_percpu, cpu);
912
913 thread = kthread_create(fcoe_percpu_receive_thread,
914 (void *)p, "fcoethread/%d", cpu);
915
Joe Eykholte7a51992009-08-25 14:04:08 -0700916 if (likely(!IS_ERR(thread))) {
Robert Love8976f422009-03-17 11:41:46 -0700917 kthread_bind(thread, cpu);
918 wake_up_process(thread);
919
920 spin_lock_bh(&p->fcoe_rx_list.lock);
921 p->thread = thread;
922 spin_unlock_bh(&p->fcoe_rx_list.lock);
923 }
924}
925
926/**
927 * fcoe_percpu_thread_destroy() - removes the rx thread for the given cpu
928 * @cpu: cpu index the rx thread is to be removed
929 *
930 * Destroys a per-CPU Rx thread. Any pending skbs are moved to the
931 * current CPU's Rx thread. If the thread being destroyed is bound to
932 * the CPU processing this context the skbs will be freed.
933 */
934static void fcoe_percpu_thread_destroy(unsigned int cpu)
935{
936 struct fcoe_percpu_s *p;
937 struct task_struct *thread;
938 struct page *crc_eof;
939 struct sk_buff *skb;
940#ifdef CONFIG_SMP
941 struct fcoe_percpu_s *p0;
942 unsigned targ_cpu = smp_processor_id();
943#endif /* CONFIG_SMP */
944
Robert Loved5488eb2009-06-10 15:30:59 -0700945 FCOE_DBG("Destroying receive thread for CPU %d\n", cpu);
Robert Love8976f422009-03-17 11:41:46 -0700946
947 /* Prevent any new skbs from being queued for this CPU. */
948 p = &per_cpu(fcoe_percpu, cpu);
949 spin_lock_bh(&p->fcoe_rx_list.lock);
950 thread = p->thread;
951 p->thread = NULL;
952 crc_eof = p->crc_eof_page;
953 p->crc_eof_page = NULL;
954 p->crc_eof_offset = 0;
955 spin_unlock_bh(&p->fcoe_rx_list.lock);
956
957#ifdef CONFIG_SMP
958 /*
959 * Don't bother moving the skb's if this context is running
960 * on the same CPU that is having its thread destroyed. This
961 * can easily happen when the module is removed.
962 */
963 if (cpu != targ_cpu) {
964 p0 = &per_cpu(fcoe_percpu, targ_cpu);
965 spin_lock_bh(&p0->fcoe_rx_list.lock);
966 if (p0->thread) {
Robert Loved5488eb2009-06-10 15:30:59 -0700967 FCOE_DBG("Moving frames from CPU %d to CPU %d\n",
968 cpu, targ_cpu);
Robert Love8976f422009-03-17 11:41:46 -0700969
970 while ((skb = __skb_dequeue(&p->fcoe_rx_list)) != NULL)
971 __skb_queue_tail(&p0->fcoe_rx_list, skb);
972 spin_unlock_bh(&p0->fcoe_rx_list.lock);
973 } else {
974 /*
975 * The targeted CPU is not initialized and cannot accept
976 * new skbs. Unlock the targeted CPU and drop the skbs
977 * on the CPU that is going offline.
978 */
979 while ((skb = __skb_dequeue(&p->fcoe_rx_list)) != NULL)
980 kfree_skb(skb);
981 spin_unlock_bh(&p0->fcoe_rx_list.lock);
982 }
983 } else {
984 /*
985 * This scenario occurs when the module is being removed
986 * and all threads are being destroyed. skbs will continue
987 * to be shifted from the CPU thread that is being removed
988 * to the CPU thread associated with the CPU that is processing
989 * the module removal. Once there is only one CPU Rx thread it
990 * will reach this case and we will drop all skbs and later
991 * stop the thread.
992 */
993 spin_lock_bh(&p->fcoe_rx_list.lock);
994 while ((skb = __skb_dequeue(&p->fcoe_rx_list)) != NULL)
995 kfree_skb(skb);
996 spin_unlock_bh(&p->fcoe_rx_list.lock);
997 }
998#else
999 /*
Chris Leechdd3fd722009-04-21 16:27:36 -07001000 * This a non-SMP scenario where the singular Rx thread is
Robert Love8976f422009-03-17 11:41:46 -07001001 * being removed. Free all skbs and stop the thread.
1002 */
1003 spin_lock_bh(&p->fcoe_rx_list.lock);
1004 while ((skb = __skb_dequeue(&p->fcoe_rx_list)) != NULL)
1005 kfree_skb(skb);
1006 spin_unlock_bh(&p->fcoe_rx_list.lock);
1007#endif
1008
1009 if (thread)
1010 kthread_stop(thread);
1011
1012 if (crc_eof)
1013 put_page(crc_eof);
1014}
1015
1016/**
1017 * fcoe_cpu_callback() - fcoe cpu hotplug event callback
1018 * @nfb: callback data block
1019 * @action: event triggering the callback
1020 * @hcpu: index for the cpu of this event
1021 *
1022 * This creates or destroys per cpu data for fcoe
1023 *
1024 * Returns NOTIFY_OK always.
1025 */
1026static int fcoe_cpu_callback(struct notifier_block *nfb,
1027 unsigned long action, void *hcpu)
1028{
1029 unsigned cpu = (unsigned long)hcpu;
1030
1031 switch (action) {
1032 case CPU_ONLINE:
1033 case CPU_ONLINE_FROZEN:
Robert Loved5488eb2009-06-10 15:30:59 -07001034 FCOE_DBG("CPU %x online: Create Rx thread\n", cpu);
Robert Love8976f422009-03-17 11:41:46 -07001035 fcoe_percpu_thread_create(cpu);
1036 break;
1037 case CPU_DEAD:
1038 case CPU_DEAD_FROZEN:
Robert Loved5488eb2009-06-10 15:30:59 -07001039 FCOE_DBG("CPU %x offline: Remove Rx thread\n", cpu);
Robert Love8976f422009-03-17 11:41:46 -07001040 fcoe_percpu_thread_destroy(cpu);
1041 break;
1042 default:
1043 break;
1044 }
1045 return NOTIFY_OK;
1046}
1047
1048static struct notifier_block fcoe_cpu_notifier = {
1049 .notifier_call = fcoe_cpu_callback,
1050};
1051
1052/**
Robert Love34f42a02009-02-27 10:55:45 -08001053 * fcoe_rcv() - this is the fcoe receive function called by NET_RX_SOFTIRQ
Robert Love85b4aa42008-12-09 15:10:24 -08001054 * @skb: the receive skb
1055 * @dev: associated net device
1056 * @ptype: context
Chris Leechdd3fd722009-04-21 16:27:36 -07001057 * @olddev: last device
Robert Love85b4aa42008-12-09 15:10:24 -08001058 *
1059 * this function will receive the packet and build fc frame and pass it up
1060 *
1061 * Returns: 0 for success
Robert Love34f42a02009-02-27 10:55:45 -08001062 */
Robert Love85b4aa42008-12-09 15:10:24 -08001063int fcoe_rcv(struct sk_buff *skb, struct net_device *dev,
1064 struct packet_type *ptype, struct net_device *olddev)
1065{
1066 struct fc_lport *lp;
1067 struct fcoe_rcv_info *fr;
Chris Leech259ad852009-08-25 13:59:41 -07001068 struct fcoe_interface *fcoe;
Robert Love85b4aa42008-12-09 15:10:24 -08001069 struct fc_frame_header *fh;
Robert Love85b4aa42008-12-09 15:10:24 -08001070 struct fcoe_percpu_s *fps;
Vasu Devb2f00912009-08-25 13:58:53 -07001071 unsigned int cpu;
Robert Love85b4aa42008-12-09 15:10:24 -08001072
Chris Leech259ad852009-08-25 13:59:41 -07001073 fcoe = container_of(ptype, struct fcoe_interface, fcoe_packet_type);
Chris Leech3fe9a0b2009-08-25 13:59:46 -07001074 lp = fcoe->ctlr.lp;
Robert Love85b4aa42008-12-09 15:10:24 -08001075 if (unlikely(lp == NULL)) {
Robert Loved5488eb2009-06-10 15:30:59 -07001076 FCOE_NETDEV_DBG(dev, "Cannot find hba structure");
Robert Love85b4aa42008-12-09 15:10:24 -08001077 goto err2;
1078 }
Joe Eykholt97c83892009-03-17 11:42:40 -07001079 if (!lp->link_up)
1080 goto err2;
Robert Love85b4aa42008-12-09 15:10:24 -08001081
Robert Loved5488eb2009-06-10 15:30:59 -07001082 FCOE_NETDEV_DBG(dev, "skb_info: len:%d data_len:%d head:%p "
1083 "data:%p tail:%p end:%p sum:%d dev:%s",
1084 skb->len, skb->data_len, skb->head, skb->data,
1085 skb_tail_pointer(skb), skb_end_pointer(skb),
1086 skb->csum, skb->dev ? skb->dev->name : "<NULL>");
Robert Love85b4aa42008-12-09 15:10:24 -08001087
1088 /* check for FCOE packet type */
1089 if (unlikely(eth_hdr(skb)->h_proto != htons(ETH_P_FCOE))) {
Robert Loved5488eb2009-06-10 15:30:59 -07001090 FCOE_NETDEV_DBG(dev, "Wrong FC type frame");
Robert Love85b4aa42008-12-09 15:10:24 -08001091 goto err;
1092 }
1093
1094 /*
1095 * Check for minimum frame length, and make sure required FCoE
1096 * and FC headers are pulled into the linear data area.
1097 */
1098 if (unlikely((skb->len < FCOE_MIN_FRAME) ||
1099 !pskb_may_pull(skb, FCOE_HEADER_LEN)))
1100 goto err;
1101
1102 skb_set_transport_header(skb, sizeof(struct fcoe_hdr));
1103 fh = (struct fc_frame_header *) skb_transport_header(skb);
1104
Robert Love85b4aa42008-12-09 15:10:24 -08001105 fr = fcoe_dev_from_skb(skb);
1106 fr->fr_dev = lp;
1107 fr->ptype = ptype;
Robert Love5e5e92d2009-03-17 11:41:35 -07001108
Robert Love85b4aa42008-12-09 15:10:24 -08001109 /*
Vasu Devb2f00912009-08-25 13:58:53 -07001110 * In case the incoming frame's exchange is originated from
1111 * the initiator, then received frame's exchange id is ANDed
1112 * with fc_cpu_mask bits to get the same cpu on which exchange
1113 * was originated, otherwise just use the current cpu.
Robert Love85b4aa42008-12-09 15:10:24 -08001114 */
Vasu Devb2f00912009-08-25 13:58:53 -07001115 if (ntoh24(fh->fh_f_ctl) & FC_FC_EX_CTX)
1116 cpu = ntohs(fh->fh_ox_id) & fc_cpu_mask;
1117 else
1118 cpu = smp_processor_id();
Robert Love5e5e92d2009-03-17 11:41:35 -07001119
Robert Love8976f422009-03-17 11:41:46 -07001120 fps = &per_cpu(fcoe_percpu, cpu);
Robert Love85b4aa42008-12-09 15:10:24 -08001121 spin_lock_bh(&fps->fcoe_rx_list.lock);
Robert Love8976f422009-03-17 11:41:46 -07001122 if (unlikely(!fps->thread)) {
1123 /*
1124 * The targeted CPU is not ready, let's target
1125 * the first CPU now. For non-SMP systems this
1126 * will check the same CPU twice.
1127 */
Robert Loved5488eb2009-06-10 15:30:59 -07001128 FCOE_NETDEV_DBG(dev, "CPU is online, but no receive thread "
1129 "ready for incoming skb- using first online "
1130 "CPU.\n");
Robert Love8976f422009-03-17 11:41:46 -07001131
1132 spin_unlock_bh(&fps->fcoe_rx_list.lock);
1133 cpu = first_cpu(cpu_online_map);
1134 fps = &per_cpu(fcoe_percpu, cpu);
1135 spin_lock_bh(&fps->fcoe_rx_list.lock);
1136 if (!fps->thread) {
1137 spin_unlock_bh(&fps->fcoe_rx_list.lock);
1138 goto err;
1139 }
1140 }
1141
1142 /*
1143 * We now have a valid CPU that we're targeting for
1144 * this skb. We also have this receive thread locked,
1145 * so we're free to queue skbs into it's queue.
1146 */
Robert Love85b4aa42008-12-09 15:10:24 -08001147 __skb_queue_tail(&fps->fcoe_rx_list, skb);
1148 if (fps->fcoe_rx_list.qlen == 1)
1149 wake_up_process(fps->thread);
1150
1151 spin_unlock_bh(&fps->fcoe_rx_list.lock);
1152
1153 return 0;
1154err:
Robert Love582b45b2009-03-31 15:51:50 -07001155 fc_lport_get_stats(lp)->ErrorFrames++;
Robert Love85b4aa42008-12-09 15:10:24 -08001156
1157err2:
1158 kfree_skb(skb);
1159 return -1;
1160}
Robert Love85b4aa42008-12-09 15:10:24 -08001161
1162/**
Robert Love34f42a02009-02-27 10:55:45 -08001163 * fcoe_start_io() - pass to netdev to start xmit for fcoe
Robert Love85b4aa42008-12-09 15:10:24 -08001164 * @skb: the skb to be xmitted
1165 *
1166 * Returns: 0 for success
Robert Love34f42a02009-02-27 10:55:45 -08001167 */
Robert Love85b4aa42008-12-09 15:10:24 -08001168static inline int fcoe_start_io(struct sk_buff *skb)
1169{
1170 int rc;
1171
1172 skb_get(skb);
1173 rc = dev_queue_xmit(skb);
1174 if (rc != 0)
1175 return rc;
1176 kfree_skb(skb);
1177 return 0;
1178}
1179
1180/**
Chris Leechdd3fd722009-04-21 16:27:36 -07001181 * fcoe_get_paged_crc_eof() - in case we need to alloc a page for crc_eof
Robert Love85b4aa42008-12-09 15:10:24 -08001182 * @skb: the skb to be xmitted
1183 * @tlen: total len
1184 *
1185 * Returns: 0 for success
Robert Love34f42a02009-02-27 10:55:45 -08001186 */
Robert Love85b4aa42008-12-09 15:10:24 -08001187static int fcoe_get_paged_crc_eof(struct sk_buff *skb, int tlen)
1188{
1189 struct fcoe_percpu_s *fps;
1190 struct page *page;
Robert Love85b4aa42008-12-09 15:10:24 -08001191
Robert Love5e5e92d2009-03-17 11:41:35 -07001192 fps = &get_cpu_var(fcoe_percpu);
Robert Love85b4aa42008-12-09 15:10:24 -08001193 page = fps->crc_eof_page;
1194 if (!page) {
1195 page = alloc_page(GFP_ATOMIC);
1196 if (!page) {
Robert Love5e5e92d2009-03-17 11:41:35 -07001197 put_cpu_var(fcoe_percpu);
Robert Love85b4aa42008-12-09 15:10:24 -08001198 return -ENOMEM;
1199 }
1200 fps->crc_eof_page = page;
Robert Love8976f422009-03-17 11:41:46 -07001201 fps->crc_eof_offset = 0;
Robert Love85b4aa42008-12-09 15:10:24 -08001202 }
1203
1204 get_page(page);
1205 skb_fill_page_desc(skb, skb_shinfo(skb)->nr_frags, page,
1206 fps->crc_eof_offset, tlen);
1207 skb->len += tlen;
1208 skb->data_len += tlen;
1209 skb->truesize += tlen;
1210 fps->crc_eof_offset += sizeof(struct fcoe_crc_eof);
1211
1212 if (fps->crc_eof_offset >= PAGE_SIZE) {
1213 fps->crc_eof_page = NULL;
1214 fps->crc_eof_offset = 0;
1215 put_page(page);
1216 }
Robert Love5e5e92d2009-03-17 11:41:35 -07001217 put_cpu_var(fcoe_percpu);
Robert Love85b4aa42008-12-09 15:10:24 -08001218 return 0;
1219}
1220
1221/**
Robert Love34f42a02009-02-27 10:55:45 -08001222 * fcoe_fc_crc() - calculates FC CRC in this fcoe skb
Chris Leechdd3fd722009-04-21 16:27:36 -07001223 * @fp: the fc_frame containing data to be checksummed
Robert Love85b4aa42008-12-09 15:10:24 -08001224 *
Chris Leech014f5c32009-08-25 13:59:30 -07001225 * This uses crc32() to calculate the crc for port frame
Robert Love85b4aa42008-12-09 15:10:24 -08001226 * Return : 32 bit crc
Robert Love34f42a02009-02-27 10:55:45 -08001227 */
Robert Love85b4aa42008-12-09 15:10:24 -08001228u32 fcoe_fc_crc(struct fc_frame *fp)
1229{
1230 struct sk_buff *skb = fp_skb(fp);
1231 struct skb_frag_struct *frag;
1232 unsigned char *data;
1233 unsigned long off, len, clen;
1234 u32 crc;
1235 unsigned i;
1236
1237 crc = crc32(~0, skb->data, skb_headlen(skb));
1238
1239 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
1240 frag = &skb_shinfo(skb)->frags[i];
1241 off = frag->page_offset;
1242 len = frag->size;
1243 while (len > 0) {
1244 clen = min(len, PAGE_SIZE - (off & ~PAGE_MASK));
1245 data = kmap_atomic(frag->page + (off >> PAGE_SHIFT),
1246 KM_SKB_DATA_SOFTIRQ);
1247 crc = crc32(crc, data + (off & ~PAGE_MASK), clen);
1248 kunmap_atomic(data, KM_SKB_DATA_SOFTIRQ);
1249 off += clen;
1250 len -= clen;
1251 }
1252 }
1253 return crc;
1254}
Robert Love85b4aa42008-12-09 15:10:24 -08001255
1256/**
Robert Love34f42a02009-02-27 10:55:45 -08001257 * fcoe_xmit() - FCoE frame transmit function
Chris Leech014f5c32009-08-25 13:59:30 -07001258 * @lp: the associated local fcoe
Robert Love85b4aa42008-12-09 15:10:24 -08001259 * @fp: the fc_frame to be transmitted
1260 *
1261 * Return : 0 for success
Robert Love34f42a02009-02-27 10:55:45 -08001262 */
Robert Love85b4aa42008-12-09 15:10:24 -08001263int fcoe_xmit(struct fc_lport *lp, struct fc_frame *fp)
1264{
Vasu Dev4bb6b512009-05-06 10:52:34 -07001265 int wlen;
Robert Love85b4aa42008-12-09 15:10:24 -08001266 u32 crc;
1267 struct ethhdr *eh;
1268 struct fcoe_crc_eof *cp;
1269 struct sk_buff *skb;
1270 struct fcoe_dev_stats *stats;
1271 struct fc_frame_header *fh;
1272 unsigned int hlen; /* header length implies the version */
1273 unsigned int tlen; /* trailer length */
1274 unsigned int elen; /* eth header, may include vlan */
Chris Leech3fe9a0b2009-08-25 13:59:46 -07001275 struct fcoe_port *port = lport_priv(lp);
1276 struct fcoe_interface *fcoe = port->fcoe;
Robert Love85b4aa42008-12-09 15:10:24 -08001277 u8 sof, eof;
1278 struct fcoe_hdr *hp;
1279
1280 WARN_ON((fr_len(fp) % sizeof(u32)) != 0);
1281
Robert Love85b4aa42008-12-09 15:10:24 -08001282 fh = fc_frame_header_get(fp);
Joe Eykholt97c83892009-03-17 11:42:40 -07001283 skb = fp_skb(fp);
1284 wlen = skb->len / FCOE_WORD_TO_BYTE;
1285
1286 if (!lp->link_up) {
Dan Carpenter3caf02e2009-04-21 16:27:25 -07001287 kfree_skb(skb);
Joe Eykholt97c83892009-03-17 11:42:40 -07001288 return 0;
Robert Love85b4aa42008-12-09 15:10:24 -08001289 }
1290
Joe Eykholt97c83892009-03-17 11:42:40 -07001291 if (unlikely(fh->fh_r_ctl == FC_RCTL_ELS_REQ) &&
Chris Leech11b56182009-11-03 11:46:29 -08001292 fcoe_ctlr_els_send(&fcoe->ctlr, lp, skb))
Joe Eykholt97c83892009-03-17 11:42:40 -07001293 return 0;
1294
Robert Love85b4aa42008-12-09 15:10:24 -08001295 sof = fr_sof(fp);
1296 eof = fr_eof(fp);
1297
Vasu Dev4e57e1c2009-05-06 10:52:46 -07001298 elen = sizeof(struct ethhdr);
Robert Love85b4aa42008-12-09 15:10:24 -08001299 hlen = sizeof(struct fcoe_hdr);
1300 tlen = sizeof(struct fcoe_crc_eof);
1301 wlen = (skb->len - tlen + sizeof(crc)) / FCOE_WORD_TO_BYTE;
1302
1303 /* crc offload */
1304 if (likely(lp->crc_offload)) {
Yi Zou39ca9a02009-02-27 14:07:15 -08001305 skb->ip_summed = CHECKSUM_PARTIAL;
Robert Love85b4aa42008-12-09 15:10:24 -08001306 skb->csum_start = skb_headroom(skb);
1307 skb->csum_offset = skb->len;
1308 crc = 0;
1309 } else {
1310 skb->ip_summed = CHECKSUM_NONE;
1311 crc = fcoe_fc_crc(fp);
1312 }
1313
Chris Leech014f5c32009-08-25 13:59:30 -07001314 /* copy port crc and eof to the skb buff */
Robert Love85b4aa42008-12-09 15:10:24 -08001315 if (skb_is_nonlinear(skb)) {
1316 skb_frag_t *frag;
1317 if (fcoe_get_paged_crc_eof(skb, tlen)) {
Roel Kluine9041582009-02-27 10:56:22 -08001318 kfree_skb(skb);
Robert Love85b4aa42008-12-09 15:10:24 -08001319 return -ENOMEM;
1320 }
1321 frag = &skb_shinfo(skb)->frags[skb_shinfo(skb)->nr_frags - 1];
1322 cp = kmap_atomic(frag->page, KM_SKB_DATA_SOFTIRQ)
1323 + frag->page_offset;
1324 } else {
1325 cp = (struct fcoe_crc_eof *)skb_put(skb, tlen);
1326 }
1327
1328 memset(cp, 0, sizeof(*cp));
1329 cp->fcoe_eof = eof;
1330 cp->fcoe_crc32 = cpu_to_le32(~crc);
1331
1332 if (skb_is_nonlinear(skb)) {
1333 kunmap_atomic(cp, KM_SKB_DATA_SOFTIRQ);
1334 cp = NULL;
1335 }
1336
Chris Leech014f5c32009-08-25 13:59:30 -07001337 /* adjust skb network/transport offsets to match mac/fcoe/port */
Robert Love85b4aa42008-12-09 15:10:24 -08001338 skb_push(skb, elen + hlen);
1339 skb_reset_mac_header(skb);
1340 skb_reset_network_header(skb);
1341 skb->mac_len = elen;
Yi Zou211c7382009-02-27 14:06:37 -08001342 skb->protocol = htons(ETH_P_FCOE);
Chris Leech3fe9a0b2009-08-25 13:59:46 -07001343 skb->dev = fcoe->netdev;
Robert Love85b4aa42008-12-09 15:10:24 -08001344
1345 /* fill up mac and fcoe headers */
1346 eh = eth_hdr(skb);
1347 eh->h_proto = htons(ETH_P_FCOE);
Chris Leech3fe9a0b2009-08-25 13:59:46 -07001348 if (fcoe->ctlr.map_dest)
Robert Love85b4aa42008-12-09 15:10:24 -08001349 fc_fcoe_set_mac(eh->h_dest, fh->fh_d_id);
1350 else
1351 /* insert GW address */
Chris Leech3fe9a0b2009-08-25 13:59:46 -07001352 memcpy(eh->h_dest, fcoe->ctlr.dest_addr, ETH_ALEN);
Robert Love85b4aa42008-12-09 15:10:24 -08001353
Chris Leech3fe9a0b2009-08-25 13:59:46 -07001354 if (unlikely(fcoe->ctlr.flogi_oxid != FC_XID_UNKNOWN))
1355 memcpy(eh->h_source, fcoe->ctlr.ctl_src_addr, ETH_ALEN);
Robert Love85b4aa42008-12-09 15:10:24 -08001356 else
Chris Leech11b56182009-11-03 11:46:29 -08001357 memcpy(eh->h_source, port->data_src_addr, ETH_ALEN);
Robert Love85b4aa42008-12-09 15:10:24 -08001358
1359 hp = (struct fcoe_hdr *)(eh + 1);
1360 memset(hp, 0, sizeof(*hp));
1361 if (FC_FCOE_VER)
1362 FC_FCOE_ENCAPS_VER(hp, FC_FCOE_VER);
1363 hp->fcoe_sof = sof;
1364
Yi Zou39ca9a02009-02-27 14:07:15 -08001365 /* fcoe lso, mss is in max_payload which is non-zero for FCP data */
1366 if (lp->seq_offload && fr_max_payload(fp)) {
1367 skb_shinfo(skb)->gso_type = SKB_GSO_FCOE;
1368 skb_shinfo(skb)->gso_size = fr_max_payload(fp);
1369 } else {
1370 skb_shinfo(skb)->gso_type = 0;
1371 skb_shinfo(skb)->gso_size = 0;
1372 }
Robert Love85b4aa42008-12-09 15:10:24 -08001373 /* update tx stats: regardless if LLD fails */
Robert Love582b45b2009-03-31 15:51:50 -07001374 stats = fc_lport_get_stats(lp);
1375 stats->TxFrames++;
1376 stats->TxWords += wlen;
Robert Love85b4aa42008-12-09 15:10:24 -08001377
1378 /* send down to lld */
1379 fr_dev(fp) = lp;
Chris Leech014f5c32009-08-25 13:59:30 -07001380 if (port->fcoe_pending_queue.qlen)
Vasu Dev4bb6b512009-05-06 10:52:34 -07001381 fcoe_check_wait_queue(lp, skb);
1382 else if (fcoe_start_io(skb))
1383 fcoe_check_wait_queue(lp, skb);
Robert Love85b4aa42008-12-09 15:10:24 -08001384
1385 return 0;
1386}
Robert Love85b4aa42008-12-09 15:10:24 -08001387
Robert Love34f42a02009-02-27 10:55:45 -08001388/**
Joe Eykholte7a51992009-08-25 14:04:08 -07001389 * fcoe_percpu_flush_done() - Indicate percpu queue flush completion.
1390 * @skb: the skb being completed.
1391 */
1392static void fcoe_percpu_flush_done(struct sk_buff *skb)
1393{
1394 complete(&fcoe_flush_completion);
1395}
1396
1397/**
Robert Love34f42a02009-02-27 10:55:45 -08001398 * fcoe_percpu_receive_thread() - recv thread per cpu
Robert Love85b4aa42008-12-09 15:10:24 -08001399 * @arg: ptr to the fcoe per cpu struct
1400 *
1401 * Return: 0 for success
Robert Love85b4aa42008-12-09 15:10:24 -08001402 */
1403int fcoe_percpu_receive_thread(void *arg)
1404{
1405 struct fcoe_percpu_s *p = arg;
1406 u32 fr_len;
1407 struct fc_lport *lp;
1408 struct fcoe_rcv_info *fr;
1409 struct fcoe_dev_stats *stats;
1410 struct fc_frame_header *fh;
1411 struct sk_buff *skb;
1412 struct fcoe_crc_eof crc_eof;
1413 struct fc_frame *fp;
1414 u8 *mac = NULL;
Chris Leech014f5c32009-08-25 13:59:30 -07001415 struct fcoe_port *port;
Robert Love85b4aa42008-12-09 15:10:24 -08001416 struct fcoe_hdr *hp;
1417
Robert Love4469c192009-02-27 10:56:38 -08001418 set_user_nice(current, -20);
Robert Love85b4aa42008-12-09 15:10:24 -08001419
1420 while (!kthread_should_stop()) {
1421
1422 spin_lock_bh(&p->fcoe_rx_list.lock);
1423 while ((skb = __skb_dequeue(&p->fcoe_rx_list)) == NULL) {
1424 set_current_state(TASK_INTERRUPTIBLE);
1425 spin_unlock_bh(&p->fcoe_rx_list.lock);
1426 schedule();
1427 set_current_state(TASK_RUNNING);
1428 if (kthread_should_stop())
1429 return 0;
1430 spin_lock_bh(&p->fcoe_rx_list.lock);
1431 }
1432 spin_unlock_bh(&p->fcoe_rx_list.lock);
1433 fr = fcoe_dev_from_skb(skb);
1434 lp = fr->fr_dev;
1435 if (unlikely(lp == NULL)) {
Joe Eykholte7a51992009-08-25 14:04:08 -07001436 if (skb->destructor != fcoe_percpu_flush_done)
1437 FCOE_NETDEV_DBG(skb->dev, "NULL lport in skb");
Robert Love85b4aa42008-12-09 15:10:24 -08001438 kfree_skb(skb);
1439 continue;
1440 }
1441
Robert Loved5488eb2009-06-10 15:30:59 -07001442 FCOE_NETDEV_DBG(skb->dev, "skb_info: len:%d data_len:%d "
1443 "head:%p data:%p tail:%p end:%p sum:%d dev:%s",
1444 skb->len, skb->data_len,
1445 skb->head, skb->data, skb_tail_pointer(skb),
1446 skb_end_pointer(skb), skb->csum,
1447 skb->dev ? skb->dev->name : "<NULL>");
Robert Love85b4aa42008-12-09 15:10:24 -08001448
1449 /*
1450 * Save source MAC address before discarding header.
1451 */
Chris Leech014f5c32009-08-25 13:59:30 -07001452 port = lport_priv(lp);
Robert Love85b4aa42008-12-09 15:10:24 -08001453 if (skb_is_nonlinear(skb))
1454 skb_linearize(skb); /* not ideal */
Joe Eykholt97c83892009-03-17 11:42:40 -07001455 mac = eth_hdr(skb)->h_source;
Robert Love85b4aa42008-12-09 15:10:24 -08001456
1457 /*
1458 * Frame length checks and setting up the header pointers
1459 * was done in fcoe_rcv already.
1460 */
1461 hp = (struct fcoe_hdr *) skb_network_header(skb);
1462 fh = (struct fc_frame_header *) skb_transport_header(skb);
1463
Robert Love582b45b2009-03-31 15:51:50 -07001464 stats = fc_lport_get_stats(lp);
Robert Love85b4aa42008-12-09 15:10:24 -08001465 if (unlikely(FC_FCOE_DECAPS_VER(hp) != FC_FCOE_VER)) {
Robert Love582b45b2009-03-31 15:51:50 -07001466 if (stats->ErrorFrames < 5)
Robert Loved5488eb2009-06-10 15:30:59 -07001467 printk(KERN_WARNING "fcoe: FCoE version "
Robert Love582b45b2009-03-31 15:51:50 -07001468 "mismatch: The frame has "
1469 "version %x, but the "
1470 "initiator supports version "
1471 "%x\n", FC_FCOE_DECAPS_VER(hp),
1472 FC_FCOE_VER);
1473 stats->ErrorFrames++;
Robert Love85b4aa42008-12-09 15:10:24 -08001474 kfree_skb(skb);
1475 continue;
1476 }
1477
1478 skb_pull(skb, sizeof(struct fcoe_hdr));
1479 fr_len = skb->len - sizeof(struct fcoe_crc_eof);
1480
Robert Love582b45b2009-03-31 15:51:50 -07001481 stats->RxFrames++;
1482 stats->RxWords += fr_len / FCOE_WORD_TO_BYTE;
Robert Love85b4aa42008-12-09 15:10:24 -08001483
1484 fp = (struct fc_frame *)skb;
1485 fc_frame_init(fp);
1486 fr_dev(fp) = lp;
1487 fr_sof(fp) = hp->fcoe_sof;
1488
1489 /* Copy out the CRC and EOF trailer for access */
1490 if (skb_copy_bits(skb, fr_len, &crc_eof, sizeof(crc_eof))) {
1491 kfree_skb(skb);
1492 continue;
1493 }
1494 fr_eof(fp) = crc_eof.fcoe_eof;
1495 fr_crc(fp) = crc_eof.fcoe_crc32;
1496 if (pskb_trim(skb, fr_len)) {
1497 kfree_skb(skb);
1498 continue;
1499 }
1500
1501 /*
1502 * We only check CRC if no offload is available and if it is
1503 * it's solicited data, in which case, the FCP layer would
1504 * check it during the copy.
1505 */
Yi Zou07c00ec2009-02-27 14:07:31 -08001506 if (lp->crc_offload && skb->ip_summed == CHECKSUM_UNNECESSARY)
Robert Love85b4aa42008-12-09 15:10:24 -08001507 fr_flags(fp) &= ~FCPHF_CRC_UNCHECKED;
1508 else
1509 fr_flags(fp) |= FCPHF_CRC_UNCHECKED;
1510
1511 fh = fc_frame_header_get(fp);
1512 if (fh->fh_r_ctl == FC_RCTL_DD_SOL_DATA &&
1513 fh->fh_type == FC_TYPE_FCP) {
Vasu Dev52ff8782009-07-29 17:05:10 -07001514 fc_exch_recv(lp, fp);
Robert Love85b4aa42008-12-09 15:10:24 -08001515 continue;
1516 }
1517 if (fr_flags(fp) & FCPHF_CRC_UNCHECKED) {
1518 if (le32_to_cpu(fr_crc(fp)) !=
1519 ~crc32(~0, skb->data, fr_len)) {
Robert Loved5488eb2009-06-10 15:30:59 -07001520 if (stats->InvalidCRCCount < 5)
Robert Love85b4aa42008-12-09 15:10:24 -08001521 printk(KERN_WARNING "fcoe: dropping "
1522 "frame with CRC error\n");
1523 stats->InvalidCRCCount++;
1524 stats->ErrorFrames++;
1525 fc_frame_free(fp);
1526 continue;
1527 }
1528 fr_flags(fp) &= ~FCPHF_CRC_UNCHECKED;
1529 }
Vasu Dev52ff8782009-07-29 17:05:10 -07001530 fc_exch_recv(lp, fp);
Robert Love85b4aa42008-12-09 15:10:24 -08001531 }
1532 return 0;
1533}
1534
1535/**
Chris Leechdd3fd722009-04-21 16:27:36 -07001536 * fcoe_check_wait_queue() - attempt to clear the transmit backlog
1537 * @lp: the fc_lport
Robert Love85b4aa42008-12-09 15:10:24 -08001538 *
1539 * This empties the wait_queue, dequeue the head of the wait_queue queue
1540 * and calls fcoe_start_io() for each packet, if all skb have been
Vasu Devc826a312009-02-27 10:56:27 -08001541 * transmitted, return qlen or -1 if a error occurs, then restore
Chris Leechdd3fd722009-04-21 16:27:36 -07001542 * wait_queue and try again later.
Robert Love85b4aa42008-12-09 15:10:24 -08001543 *
1544 * The wait_queue is used when the skb transmit fails. skb will go
Chris Leechdd3fd722009-04-21 16:27:36 -07001545 * in the wait_queue which will be emptied by the timer function or
Robert Love85b4aa42008-12-09 15:10:24 -08001546 * by the next skb transmit.
Robert Love34f42a02009-02-27 10:55:45 -08001547 */
Vasu Dev4bb6b512009-05-06 10:52:34 -07001548static void fcoe_check_wait_queue(struct fc_lport *lp, struct sk_buff *skb)
Robert Love85b4aa42008-12-09 15:10:24 -08001549{
Chris Leech014f5c32009-08-25 13:59:30 -07001550 struct fcoe_port *port = lport_priv(lp);
Vasu Dev4bb6b512009-05-06 10:52:34 -07001551 int rc;
Robert Love85b4aa42008-12-09 15:10:24 -08001552
Chris Leech014f5c32009-08-25 13:59:30 -07001553 spin_lock_bh(&port->fcoe_pending_queue.lock);
Vasu Dev4bb6b512009-05-06 10:52:34 -07001554
1555 if (skb)
Chris Leech014f5c32009-08-25 13:59:30 -07001556 __skb_queue_tail(&port->fcoe_pending_queue, skb);
Vasu Dev4bb6b512009-05-06 10:52:34 -07001557
Chris Leech014f5c32009-08-25 13:59:30 -07001558 if (port->fcoe_pending_queue_active)
Vasu Devc826a312009-02-27 10:56:27 -08001559 goto out;
Chris Leech014f5c32009-08-25 13:59:30 -07001560 port->fcoe_pending_queue_active = 1;
Chris Leech55c8baf2009-02-27 10:56:32 -08001561
Chris Leech014f5c32009-08-25 13:59:30 -07001562 while (port->fcoe_pending_queue.qlen) {
Chris Leech55c8baf2009-02-27 10:56:32 -08001563 /* keep qlen > 0 until fcoe_start_io succeeds */
Chris Leech014f5c32009-08-25 13:59:30 -07001564 port->fcoe_pending_queue.qlen++;
1565 skb = __skb_dequeue(&port->fcoe_pending_queue);
Chris Leech55c8baf2009-02-27 10:56:32 -08001566
Chris Leech014f5c32009-08-25 13:59:30 -07001567 spin_unlock_bh(&port->fcoe_pending_queue.lock);
Chris Leech55c8baf2009-02-27 10:56:32 -08001568 rc = fcoe_start_io(skb);
Chris Leech014f5c32009-08-25 13:59:30 -07001569 spin_lock_bh(&port->fcoe_pending_queue.lock);
Chris Leech55c8baf2009-02-27 10:56:32 -08001570
1571 if (rc) {
Chris Leech014f5c32009-08-25 13:59:30 -07001572 __skb_queue_head(&port->fcoe_pending_queue, skb);
Chris Leech55c8baf2009-02-27 10:56:32 -08001573 /* undo temporary increment above */
Chris Leech014f5c32009-08-25 13:59:30 -07001574 port->fcoe_pending_queue.qlen--;
Chris Leech55c8baf2009-02-27 10:56:32 -08001575 break;
Robert Love85b4aa42008-12-09 15:10:24 -08001576 }
Chris Leech55c8baf2009-02-27 10:56:32 -08001577 /* undo temporary increment above */
Chris Leech014f5c32009-08-25 13:59:30 -07001578 port->fcoe_pending_queue.qlen--;
Robert Love85b4aa42008-12-09 15:10:24 -08001579 }
Chris Leech55c8baf2009-02-27 10:56:32 -08001580
Chris Leech014f5c32009-08-25 13:59:30 -07001581 if (port->fcoe_pending_queue.qlen < FCOE_LOW_QUEUE_DEPTH)
Chris Leech55c8baf2009-02-27 10:56:32 -08001582 lp->qfull = 0;
Chris Leech014f5c32009-08-25 13:59:30 -07001583 if (port->fcoe_pending_queue.qlen && !timer_pending(&port->timer))
1584 mod_timer(&port->timer, jiffies + 2);
1585 port->fcoe_pending_queue_active = 0;
Vasu Devc826a312009-02-27 10:56:27 -08001586out:
Chris Leech014f5c32009-08-25 13:59:30 -07001587 if (port->fcoe_pending_queue.qlen > FCOE_MAX_QUEUE_DEPTH)
Vasu Dev4bb6b512009-05-06 10:52:34 -07001588 lp->qfull = 1;
Chris Leech014f5c32009-08-25 13:59:30 -07001589 spin_unlock_bh(&port->fcoe_pending_queue.lock);
Vasu Dev4bb6b512009-05-06 10:52:34 -07001590 return;
Robert Love85b4aa42008-12-09 15:10:24 -08001591}
1592
1593/**
Robert Love34f42a02009-02-27 10:55:45 -08001594 * fcoe_dev_setup() - setup link change notification interface
1595 */
Randy Dunlapb0d428a2009-04-27 21:49:31 -07001596static void fcoe_dev_setup(void)
Robert Love85b4aa42008-12-09 15:10:24 -08001597{
Robert Love85b4aa42008-12-09 15:10:24 -08001598 register_netdevice_notifier(&fcoe_notifier);
1599}
1600
1601/**
Randy Dunlapb0d428a2009-04-27 21:49:31 -07001602 * fcoe_dev_cleanup() - cleanup link change notification interface
Robert Love34f42a02009-02-27 10:55:45 -08001603 */
Robert Love85b4aa42008-12-09 15:10:24 -08001604static void fcoe_dev_cleanup(void)
1605{
1606 unregister_netdevice_notifier(&fcoe_notifier);
1607}
1608
1609/**
Robert Love34f42a02009-02-27 10:55:45 -08001610 * fcoe_device_notification() - netdev event notification callback
Robert Love85b4aa42008-12-09 15:10:24 -08001611 * @notifier: context of the notification
1612 * @event: type of event
1613 * @ptr: fixed array for output parsed ifname
1614 *
1615 * This function is called by the ethernet driver in case of link change event
1616 *
1617 * Returns: 0 for success
Robert Love34f42a02009-02-27 10:55:45 -08001618 */
Robert Love85b4aa42008-12-09 15:10:24 -08001619static int fcoe_device_notification(struct notifier_block *notifier,
1620 ulong event, void *ptr)
1621{
1622 struct fc_lport *lp = NULL;
Vasu Dev1d1b88d2009-07-29 17:05:45 -07001623 struct net_device *netdev = ptr;
Chris Leech014f5c32009-08-25 13:59:30 -07001624 struct fcoe_interface *fcoe;
Chris Leech2e70e242009-08-25 14:00:23 -07001625 struct fcoe_port *port;
Robert Love85b4aa42008-12-09 15:10:24 -08001626 struct fcoe_dev_stats *stats;
Joe Eykholt97c83892009-03-17 11:42:40 -07001627 u32 link_possible = 1;
Robert Love85b4aa42008-12-09 15:10:24 -08001628 u32 mfs;
1629 int rc = NOTIFY_OK;
1630
Chris Leech014f5c32009-08-25 13:59:30 -07001631 list_for_each_entry(fcoe, &fcoe_hostlist, list) {
Chris Leech25024982009-08-25 13:59:35 -07001632 if (fcoe->netdev == netdev) {
Chris Leech3fe9a0b2009-08-25 13:59:46 -07001633 lp = fcoe->ctlr.lp;
Robert Love85b4aa42008-12-09 15:10:24 -08001634 break;
1635 }
1636 }
Robert Love85b4aa42008-12-09 15:10:24 -08001637 if (lp == NULL) {
1638 rc = NOTIFY_DONE;
1639 goto out;
1640 }
1641
Robert Love85b4aa42008-12-09 15:10:24 -08001642 switch (event) {
1643 case NETDEV_DOWN:
1644 case NETDEV_GOING_DOWN:
Joe Eykholt97c83892009-03-17 11:42:40 -07001645 link_possible = 0;
Robert Love85b4aa42008-12-09 15:10:24 -08001646 break;
1647 case NETDEV_UP:
1648 case NETDEV_CHANGE:
Robert Love85b4aa42008-12-09 15:10:24 -08001649 break;
1650 case NETDEV_CHANGEMTU:
Yi Zou7221d7e2009-10-21 16:27:52 -07001651 if (netdev->features & NETIF_F_FCOE_MTU)
1652 break;
Vasu Dev1d1b88d2009-07-29 17:05:45 -07001653 mfs = netdev->mtu - (sizeof(struct fcoe_hdr) +
1654 sizeof(struct fcoe_crc_eof));
Robert Love85b4aa42008-12-09 15:10:24 -08001655 if (mfs >= FC_MIN_MAX_FRAME)
1656 fc_set_mfs(lp, mfs);
Robert Love85b4aa42008-12-09 15:10:24 -08001657 break;
1658 case NETDEV_REGISTER:
1659 break;
Chris Leech2e70e242009-08-25 14:00:23 -07001660 case NETDEV_UNREGISTER:
1661 list_del(&fcoe->list);
1662 port = lport_priv(fcoe->ctlr.lp);
1663 fcoe_interface_cleanup(fcoe);
1664 schedule_work(&port->destroy_work);
1665 goto out;
1666 break;
Robert Love85b4aa42008-12-09 15:10:24 -08001667 default:
Vasu Dev1d1b88d2009-07-29 17:05:45 -07001668 FCOE_NETDEV_DBG(netdev, "Unknown event %ld "
Robert Loved5488eb2009-06-10 15:30:59 -07001669 "from netdev netlink\n", event);
Robert Love85b4aa42008-12-09 15:10:24 -08001670 }
Joe Eykholt97c83892009-03-17 11:42:40 -07001671 if (link_possible && !fcoe_link_ok(lp))
Chris Leech3fe9a0b2009-08-25 13:59:46 -07001672 fcoe_ctlr_link_up(&fcoe->ctlr);
1673 else if (fcoe_ctlr_link_down(&fcoe->ctlr)) {
Joe Eykholt97c83892009-03-17 11:42:40 -07001674 stats = fc_lport_get_stats(lp);
1675 stats->LinkFailureCount++;
1676 fcoe_clean_pending_queue(lp);
Robert Love85b4aa42008-12-09 15:10:24 -08001677 }
1678out:
1679 return rc;
1680}
1681
1682/**
Robert Love34f42a02009-02-27 10:55:45 -08001683 * fcoe_if_to_netdev() - parse a name buffer to get netdev
Robert Love85b4aa42008-12-09 15:10:24 -08001684 * @buffer: incoming buffer to be copied
1685 *
Chris Leechdd3fd722009-04-21 16:27:36 -07001686 * Returns: NULL or ptr to net_device
Robert Love34f42a02009-02-27 10:55:45 -08001687 */
Robert Love85b4aa42008-12-09 15:10:24 -08001688static struct net_device *fcoe_if_to_netdev(const char *buffer)
1689{
1690 char *cp;
1691 char ifname[IFNAMSIZ + 2];
1692
1693 if (buffer) {
1694 strlcpy(ifname, buffer, IFNAMSIZ);
1695 cp = ifname + strlen(ifname);
1696 while (--cp >= ifname && *cp == '\n')
1697 *cp = '\0';
1698 return dev_get_by_name(&init_net, ifname);
1699 }
1700 return NULL;
1701}
1702
1703/**
Robert Love34f42a02009-02-27 10:55:45 -08001704 * fcoe_destroy() - handles the destroy from sysfs
Chris Leechdd3fd722009-04-21 16:27:36 -07001705 * @buffer: expected to be an eth if name
Robert Love85b4aa42008-12-09 15:10:24 -08001706 * @kp: associated kernel param
1707 *
1708 * Returns: 0 for success
Robert Love34f42a02009-02-27 10:55:45 -08001709 */
Robert Love85b4aa42008-12-09 15:10:24 -08001710static int fcoe_destroy(const char *buffer, struct kernel_param *kp)
1711{
Chris Leech030f4e02009-08-25 14:00:02 -07001712 struct fcoe_interface *fcoe;
Chris Leech2e70e242009-08-25 14:00:23 -07001713 struct net_device *netdev;
Mike Christie8eca3552009-10-21 16:27:44 -07001714 int rc = 0;
Robert Love85b4aa42008-12-09 15:10:24 -08001715
Chris Leechdfc1d0f2009-08-25 14:00:13 -07001716 mutex_lock(&fcoe_config_mutex);
1717#ifdef CONFIG_FCOE_MODULE
1718 /*
1719 * Make sure the module has been initialized, and is not about to be
1720 * removed. Module paramter sysfs files are writable before the
1721 * module_init function is called and after module_exit.
1722 */
1723 if (THIS_MODULE->state != MODULE_STATE_LIVE) {
1724 rc = -ENODEV;
1725 goto out_nodev;
1726 }
1727#endif
1728
Robert Love85b4aa42008-12-09 15:10:24 -08001729 netdev = fcoe_if_to_netdev(buffer);
1730 if (!netdev) {
1731 rc = -ENODEV;
1732 goto out_nodev;
1733 }
Chris Leech2e70e242009-08-25 14:00:23 -07001734
Chris Leech090eb6c2009-08-25 14:00:28 -07001735 rtnl_lock();
Chris Leech2e70e242009-08-25 14:00:23 -07001736 fcoe = fcoe_hostlist_lookup_port(netdev);
1737 if (!fcoe) {
Chris Leech090eb6c2009-08-25 14:00:28 -07001738 rtnl_unlock();
Robert Love85b4aa42008-12-09 15:10:24 -08001739 rc = -ENODEV;
1740 goto out_putdev;
1741 }
Chris Leech2e70e242009-08-25 14:00:23 -07001742 list_del(&fcoe->list);
Chris Leech2e70e242009-08-25 14:00:23 -07001743 fcoe_interface_cleanup(fcoe);
1744 rtnl_unlock();
1745 fcoe_if_destroy(fcoe->ctlr.lp);
Robert Love85b4aa42008-12-09 15:10:24 -08001746out_putdev:
1747 dev_put(netdev);
1748out_nodev:
Chris Leechdfc1d0f2009-08-25 14:00:13 -07001749 mutex_unlock(&fcoe_config_mutex);
Robert Love85b4aa42008-12-09 15:10:24 -08001750 return rc;
1751}
1752
Chris Leech2e70e242009-08-25 14:00:23 -07001753static void fcoe_destroy_work(struct work_struct *work)
1754{
1755 struct fcoe_port *port;
1756
1757 port = container_of(work, struct fcoe_port, destroy_work);
1758 mutex_lock(&fcoe_config_mutex);
1759 fcoe_if_destroy(port->lport);
1760 mutex_unlock(&fcoe_config_mutex);
1761}
1762
Robert Love85b4aa42008-12-09 15:10:24 -08001763/**
Robert Love34f42a02009-02-27 10:55:45 -08001764 * fcoe_create() - Handles the create call from sysfs
Chris Leechdd3fd722009-04-21 16:27:36 -07001765 * @buffer: expected to be an eth if name
Robert Love85b4aa42008-12-09 15:10:24 -08001766 * @kp: associated kernel param
1767 *
1768 * Returns: 0 for success
Robert Love34f42a02009-02-27 10:55:45 -08001769 */
Robert Love85b4aa42008-12-09 15:10:24 -08001770static int fcoe_create(const char *buffer, struct kernel_param *kp)
1771{
1772 int rc;
Chris Leech030f4e02009-08-25 14:00:02 -07001773 struct fcoe_interface *fcoe;
Chris Leechaf7f85d2009-08-25 13:59:24 -07001774 struct fc_lport *lport;
Robert Love85b4aa42008-12-09 15:10:24 -08001775 struct net_device *netdev;
1776
Chris Leechdfc1d0f2009-08-25 14:00:13 -07001777 mutex_lock(&fcoe_config_mutex);
1778#ifdef CONFIG_FCOE_MODULE
1779 /*
1780 * Make sure the module has been initialized, and is not about to be
1781 * removed. Module paramter sysfs files are writable before the
1782 * module_init function is called and after module_exit.
1783 */
1784 if (THIS_MODULE->state != MODULE_STATE_LIVE) {
1785 rc = -ENODEV;
1786 goto out_nodev;
1787 }
1788#endif
1789
Chris Leech2e70e242009-08-25 14:00:23 -07001790 rtnl_lock();
Robert Love85b4aa42008-12-09 15:10:24 -08001791 netdev = fcoe_if_to_netdev(buffer);
1792 if (!netdev) {
1793 rc = -ENODEV;
1794 goto out_nodev;
1795 }
Chris Leech2e70e242009-08-25 14:00:23 -07001796
Robert Love85b4aa42008-12-09 15:10:24 -08001797 /* look for existing lport */
1798 if (fcoe_hostlist_lookup(netdev)) {
1799 rc = -EEXIST;
1800 goto out_putdev;
1801 }
Robert Love85b4aa42008-12-09 15:10:24 -08001802
Chris Leech030f4e02009-08-25 14:00:02 -07001803 fcoe = fcoe_interface_create(netdev);
1804 if (!fcoe) {
1805 rc = -ENOMEM;
1806 goto out_putdev;
1807 }
1808
1809 lport = fcoe_if_create(fcoe, &netdev->dev);
Chris Leechaf7f85d2009-08-25 13:59:24 -07001810 if (IS_ERR(lport)) {
Robert Loved5488eb2009-06-10 15:30:59 -07001811 printk(KERN_ERR "fcoe: Failed to create interface (%s)\n",
Robert Love85b4aa42008-12-09 15:10:24 -08001812 netdev->name);
Robert Love85b4aa42008-12-09 15:10:24 -08001813 rc = -EIO;
Chris Leech2e70e242009-08-25 14:00:23 -07001814 fcoe_interface_cleanup(fcoe);
Chris Leech030f4e02009-08-25 14:00:02 -07001815 goto out_free;
Robert Love85b4aa42008-12-09 15:10:24 -08001816 }
Chris Leech030f4e02009-08-25 14:00:02 -07001817
Chris Leech54b649f2009-08-25 14:00:07 -07001818 /* Make this the "master" N_Port */
1819 fcoe->ctlr.lp = lport;
Chris Leech030f4e02009-08-25 14:00:02 -07001820
Chris Leechc863df32009-08-25 14:00:18 -07001821 /* add to lports list */
1822 fcoe_hostlist_add(lport);
1823
Chris Leech54b649f2009-08-25 14:00:07 -07001824 /* start FIP Discovery and FLOGI */
1825 lport->boot_time = jiffies;
1826 fc_fabric_login(lport);
1827 if (!fcoe_link_ok(lport))
1828 fcoe_ctlr_link_up(&fcoe->ctlr);
1829
1830 rc = 0;
Chris Leech030f4e02009-08-25 14:00:02 -07001831out_free:
Chris Leech54b649f2009-08-25 14:00:07 -07001832 /*
1833 * Release from init in fcoe_interface_create(), on success lport
1834 * should be holding a reference taken in fcoe_if_create().
1835 */
Chris Leech030f4e02009-08-25 14:00:02 -07001836 fcoe_interface_put(fcoe);
Robert Love85b4aa42008-12-09 15:10:24 -08001837out_putdev:
1838 dev_put(netdev);
1839out_nodev:
Chris Leech2e70e242009-08-25 14:00:23 -07001840 rtnl_unlock();
Chris Leechdfc1d0f2009-08-25 14:00:13 -07001841 mutex_unlock(&fcoe_config_mutex);
Robert Love85b4aa42008-12-09 15:10:24 -08001842 return rc;
1843}
1844
1845module_param_call(create, fcoe_create, NULL, NULL, S_IWUSR);
1846__MODULE_PARM_TYPE(create, "string");
Chris Leech014f5c32009-08-25 13:59:30 -07001847MODULE_PARM_DESC(create, "Create fcoe fcoe using net device passed in.");
Robert Love85b4aa42008-12-09 15:10:24 -08001848module_param_call(destroy, fcoe_destroy, NULL, NULL, S_IWUSR);
1849__MODULE_PARM_TYPE(destroy, "string");
Chris Leech014f5c32009-08-25 13:59:30 -07001850MODULE_PARM_DESC(destroy, "Destroy fcoe fcoe");
Robert Love85b4aa42008-12-09 15:10:24 -08001851
Robert Love34f42a02009-02-27 10:55:45 -08001852/**
1853 * fcoe_link_ok() - Check if link is ok for the fc_lport
Robert Love85b4aa42008-12-09 15:10:24 -08001854 * @lp: ptr to the fc_lport
1855 *
1856 * Any permanently-disqualifying conditions have been previously checked.
1857 * This also updates the speed setting, which may change with link for 100/1000.
1858 *
1859 * This function should probably be checking for PAUSE support at some point
1860 * in the future. Currently Per-priority-pause is not determinable using
1861 * ethtool, so we shouldn't be restrictive until that problem is resolved.
1862 *
1863 * Returns: 0 if link is OK for use by FCoE.
1864 *
1865 */
1866int fcoe_link_ok(struct fc_lport *lp)
1867{
Chris Leech014f5c32009-08-25 13:59:30 -07001868 struct fcoe_port *port = lport_priv(lp);
Chris Leech25024982009-08-25 13:59:35 -07001869 struct net_device *dev = port->fcoe->netdev;
Robert Love85b4aa42008-12-09 15:10:24 -08001870 struct ethtool_cmd ecmd = { ETHTOOL_GSET };
Robert Love85b4aa42008-12-09 15:10:24 -08001871
Yi Zou2f718d62009-07-29 17:04:01 -07001872 if ((dev->flags & IFF_UP) && netif_carrier_ok(dev) &&
1873 (!dev_ethtool_get_settings(dev, &ecmd))) {
1874 lp->link_supported_speeds &=
1875 ~(FC_PORTSPEED_1GBIT | FC_PORTSPEED_10GBIT);
1876 if (ecmd.supported & (SUPPORTED_1000baseT_Half |
1877 SUPPORTED_1000baseT_Full))
1878 lp->link_supported_speeds |= FC_PORTSPEED_1GBIT;
1879 if (ecmd.supported & SUPPORTED_10000baseT_Full)
1880 lp->link_supported_speeds |=
1881 FC_PORTSPEED_10GBIT;
1882 if (ecmd.speed == SPEED_1000)
1883 lp->link_speed = FC_PORTSPEED_1GBIT;
1884 if (ecmd.speed == SPEED_10000)
1885 lp->link_speed = FC_PORTSPEED_10GBIT;
Robert Love85b4aa42008-12-09 15:10:24 -08001886
Yi Zou2f718d62009-07-29 17:04:01 -07001887 return 0;
1888 }
1889 return -1;
Robert Love85b4aa42008-12-09 15:10:24 -08001890}
Robert Love85b4aa42008-12-09 15:10:24 -08001891
Robert Love34f42a02009-02-27 10:55:45 -08001892/**
1893 * fcoe_percpu_clean() - Clear the pending skbs for an lport
Robert Love85b4aa42008-12-09 15:10:24 -08001894 * @lp: the fc_lport
Joe Eykholte7a51992009-08-25 14:04:08 -07001895 *
1896 * Must be called with fcoe_create_mutex held to single-thread completion.
1897 *
1898 * This flushes the pending skbs by adding a new skb to each queue and
1899 * waiting until they are all freed. This assures us that not only are
1900 * there no packets that will be handled by the lport, but also that any
1901 * threads already handling packet have returned.
Robert Love85b4aa42008-12-09 15:10:24 -08001902 */
1903void fcoe_percpu_clean(struct fc_lport *lp)
1904{
Robert Love85b4aa42008-12-09 15:10:24 -08001905 struct fcoe_percpu_s *pp;
1906 struct fcoe_rcv_info *fr;
1907 struct sk_buff_head *list;
1908 struct sk_buff *skb, *next;
1909 struct sk_buff *head;
Robert Love5e5e92d2009-03-17 11:41:35 -07001910 unsigned int cpu;
Robert Love85b4aa42008-12-09 15:10:24 -08001911
Robert Love5e5e92d2009-03-17 11:41:35 -07001912 for_each_possible_cpu(cpu) {
1913 pp = &per_cpu(fcoe_percpu, cpu);
1914 spin_lock_bh(&pp->fcoe_rx_list.lock);
1915 list = &pp->fcoe_rx_list;
1916 head = list->next;
1917 for (skb = head; skb != (struct sk_buff *)list;
1918 skb = next) {
1919 next = skb->next;
1920 fr = fcoe_dev_from_skb(skb);
1921 if (fr->fr_dev == lp) {
1922 __skb_unlink(skb, list);
1923 kfree_skb(skb);
Robert Love85b4aa42008-12-09 15:10:24 -08001924 }
Robert Love85b4aa42008-12-09 15:10:24 -08001925 }
Joe Eykholte7a51992009-08-25 14:04:08 -07001926
1927 if (!pp->thread || !cpu_online(cpu)) {
1928 spin_unlock_bh(&pp->fcoe_rx_list.lock);
1929 continue;
1930 }
1931
1932 skb = dev_alloc_skb(0);
1933 if (!skb) {
1934 spin_unlock_bh(&pp->fcoe_rx_list.lock);
1935 continue;
1936 }
1937 skb->destructor = fcoe_percpu_flush_done;
1938
1939 __skb_queue_tail(&pp->fcoe_rx_list, skb);
1940 if (pp->fcoe_rx_list.qlen == 1)
1941 wake_up_process(pp->thread);
Robert Love5e5e92d2009-03-17 11:41:35 -07001942 spin_unlock_bh(&pp->fcoe_rx_list.lock);
Joe Eykholte7a51992009-08-25 14:04:08 -07001943
1944 wait_for_completion(&fcoe_flush_completion);
Robert Love85b4aa42008-12-09 15:10:24 -08001945 }
1946}
Robert Love85b4aa42008-12-09 15:10:24 -08001947
1948/**
Robert Love34f42a02009-02-27 10:55:45 -08001949 * fcoe_clean_pending_queue() - Dequeue a skb and free it
Robert Love85b4aa42008-12-09 15:10:24 -08001950 * @lp: the corresponding fc_lport
1951 *
1952 * Returns: none
Robert Love34f42a02009-02-27 10:55:45 -08001953 */
Robert Love85b4aa42008-12-09 15:10:24 -08001954void fcoe_clean_pending_queue(struct fc_lport *lp)
1955{
Chris Leech014f5c32009-08-25 13:59:30 -07001956 struct fcoe_port *port = lport_priv(lp);
Robert Love85b4aa42008-12-09 15:10:24 -08001957 struct sk_buff *skb;
1958
Chris Leech014f5c32009-08-25 13:59:30 -07001959 spin_lock_bh(&port->fcoe_pending_queue.lock);
1960 while ((skb = __skb_dequeue(&port->fcoe_pending_queue)) != NULL) {
1961 spin_unlock_bh(&port->fcoe_pending_queue.lock);
Robert Love85b4aa42008-12-09 15:10:24 -08001962 kfree_skb(skb);
Chris Leech014f5c32009-08-25 13:59:30 -07001963 spin_lock_bh(&port->fcoe_pending_queue.lock);
Robert Love85b4aa42008-12-09 15:10:24 -08001964 }
Chris Leech014f5c32009-08-25 13:59:30 -07001965 spin_unlock_bh(&port->fcoe_pending_queue.lock);
Robert Love85b4aa42008-12-09 15:10:24 -08001966}
Robert Love85b4aa42008-12-09 15:10:24 -08001967
1968/**
Robert Love34f42a02009-02-27 10:55:45 -08001969 * fcoe_reset() - Resets the fcoe
Robert Love85b4aa42008-12-09 15:10:24 -08001970 * @shost: shost the reset is from
1971 *
1972 * Returns: always 0
1973 */
1974int fcoe_reset(struct Scsi_Host *shost)
1975{
1976 struct fc_lport *lport = shost_priv(shost);
1977 fc_lport_reset(lport);
1978 return 0;
1979}
Robert Love85b4aa42008-12-09 15:10:24 -08001980
Robert Love34f42a02009-02-27 10:55:45 -08001981/**
Chris Leech014f5c32009-08-25 13:59:30 -07001982 * fcoe_hostlist_lookup_port() - find the corresponding lport by a given device
Chris Leechdd3fd722009-04-21 16:27:36 -07001983 * @dev: this is currently ptr to net_device
Robert Love85b4aa42008-12-09 15:10:24 -08001984 *
Chris Leech014f5c32009-08-25 13:59:30 -07001985 * Returns: NULL or the located fcoe_port
Chris Leech090eb6c2009-08-25 14:00:28 -07001986 * Locking: must be called with the RNL mutex held
Robert Love85b4aa42008-12-09 15:10:24 -08001987 */
Chris Leech014f5c32009-08-25 13:59:30 -07001988static struct fcoe_interface *
1989fcoe_hostlist_lookup_port(const struct net_device *dev)
Robert Love85b4aa42008-12-09 15:10:24 -08001990{
Chris Leech014f5c32009-08-25 13:59:30 -07001991 struct fcoe_interface *fcoe;
Robert Love85b4aa42008-12-09 15:10:24 -08001992
Chris Leech014f5c32009-08-25 13:59:30 -07001993 list_for_each_entry(fcoe, &fcoe_hostlist, list) {
Chris Leech25024982009-08-25 13:59:35 -07001994 if (fcoe->netdev == dev)
Chris Leech014f5c32009-08-25 13:59:30 -07001995 return fcoe;
Robert Love85b4aa42008-12-09 15:10:24 -08001996 }
Robert Love85b4aa42008-12-09 15:10:24 -08001997 return NULL;
1998}
1999
Robert Love34f42a02009-02-27 10:55:45 -08002000/**
2001 * fcoe_hostlist_lookup() - Find the corresponding lport by netdev
Robert Love85b4aa42008-12-09 15:10:24 -08002002 * @netdev: ptr to net_device
2003 *
2004 * Returns: 0 for success
Chris Leech090eb6c2009-08-25 14:00:28 -07002005 * Locking: must be called with the RTNL mutex held
Robert Love85b4aa42008-12-09 15:10:24 -08002006 */
Chris Leech090eb6c2009-08-25 14:00:28 -07002007static struct fc_lport *fcoe_hostlist_lookup(const struct net_device *netdev)
Robert Love85b4aa42008-12-09 15:10:24 -08002008{
Chris Leech014f5c32009-08-25 13:59:30 -07002009 struct fcoe_interface *fcoe;
Robert Love85b4aa42008-12-09 15:10:24 -08002010
Chris Leech014f5c32009-08-25 13:59:30 -07002011 fcoe = fcoe_hostlist_lookup_port(netdev);
Chris Leech3fe9a0b2009-08-25 13:59:46 -07002012 return (fcoe) ? fcoe->ctlr.lp : NULL;
Robert Love85b4aa42008-12-09 15:10:24 -08002013}
Robert Love85b4aa42008-12-09 15:10:24 -08002014
Robert Love34f42a02009-02-27 10:55:45 -08002015/**
2016 * fcoe_hostlist_add() - Add a lport to lports list
Chris Leechdd3fd722009-04-21 16:27:36 -07002017 * @lp: ptr to the fc_lport to be added
Robert Love85b4aa42008-12-09 15:10:24 -08002018 *
2019 * Returns: 0 for success
Chris Leech090eb6c2009-08-25 14:00:28 -07002020 * Locking: must be called with the RTNL mutex held
Robert Love85b4aa42008-12-09 15:10:24 -08002021 */
Chris Leech090eb6c2009-08-25 14:00:28 -07002022static int fcoe_hostlist_add(const struct fc_lport *lport)
Robert Love85b4aa42008-12-09 15:10:24 -08002023{
Chris Leech014f5c32009-08-25 13:59:30 -07002024 struct fcoe_interface *fcoe;
2025 struct fcoe_port *port;
Robert Love85b4aa42008-12-09 15:10:24 -08002026
Chris Leech014f5c32009-08-25 13:59:30 -07002027 fcoe = fcoe_hostlist_lookup_port(fcoe_netdev(lport));
2028 if (!fcoe) {
2029 port = lport_priv(lport);
2030 fcoe = port->fcoe;
2031 list_add_tail(&fcoe->list, &fcoe_hostlist);
Robert Love85b4aa42008-12-09 15:10:24 -08002032 }
2033 return 0;
2034}
Robert Love85b4aa42008-12-09 15:10:24 -08002035
Robert Love34f42a02009-02-27 10:55:45 -08002036/**
Robert Love34f42a02009-02-27 10:55:45 -08002037 * fcoe_init() - fcoe module loading initialization
Robert Love85b4aa42008-12-09 15:10:24 -08002038 *
Robert Love85b4aa42008-12-09 15:10:24 -08002039 * Returns 0 on success, negative on failure
Robert Love34f42a02009-02-27 10:55:45 -08002040 */
Robert Love85b4aa42008-12-09 15:10:24 -08002041static int __init fcoe_init(void)
2042{
Robert Love38eccab2009-03-17 11:41:30 -07002043 unsigned int cpu;
Robert Love8976f422009-03-17 11:41:46 -07002044 int rc = 0;
Robert Love85b4aa42008-12-09 15:10:24 -08002045 struct fcoe_percpu_s *p;
2046
Chris Leechdfc1d0f2009-08-25 14:00:13 -07002047 mutex_lock(&fcoe_config_mutex);
2048
Robert Love38eccab2009-03-17 11:41:30 -07002049 for_each_possible_cpu(cpu) {
Robert Love5e5e92d2009-03-17 11:41:35 -07002050 p = &per_cpu(fcoe_percpu, cpu);
Robert Love38eccab2009-03-17 11:41:30 -07002051 skb_queue_head_init(&p->fcoe_rx_list);
2052 }
2053
Robert Love8976f422009-03-17 11:41:46 -07002054 for_each_online_cpu(cpu)
2055 fcoe_percpu_thread_create(cpu);
Robert Love85b4aa42008-12-09 15:10:24 -08002056
Robert Love8976f422009-03-17 11:41:46 -07002057 /* Initialize per CPU interrupt thread */
2058 rc = register_hotcpu_notifier(&fcoe_cpu_notifier);
2059 if (rc)
2060 goto out_free;
Robert Love85b4aa42008-12-09 15:10:24 -08002061
Robert Love8976f422009-03-17 11:41:46 -07002062 /* Setup link change notification */
Robert Love85b4aa42008-12-09 15:10:24 -08002063 fcoe_dev_setup();
2064
Chris Leech5892c322009-08-25 13:59:14 -07002065 rc = fcoe_if_init();
2066 if (rc)
2067 goto out_free;
Robert Love85b4aa42008-12-09 15:10:24 -08002068
Chris Leechdfc1d0f2009-08-25 14:00:13 -07002069 mutex_unlock(&fcoe_config_mutex);
Robert Love85b4aa42008-12-09 15:10:24 -08002070 return 0;
Robert Love8976f422009-03-17 11:41:46 -07002071
2072out_free:
2073 for_each_online_cpu(cpu) {
2074 fcoe_percpu_thread_destroy(cpu);
2075 }
Chris Leechdfc1d0f2009-08-25 14:00:13 -07002076 mutex_unlock(&fcoe_config_mutex);
Robert Love8976f422009-03-17 11:41:46 -07002077 return rc;
Robert Love85b4aa42008-12-09 15:10:24 -08002078}
2079module_init(fcoe_init);
2080
2081/**
Robert Love34f42a02009-02-27 10:55:45 -08002082 * fcoe_exit() - fcoe module unloading cleanup
Robert Love85b4aa42008-12-09 15:10:24 -08002083 *
2084 * Returns 0 on success, negative on failure
Robert Love34f42a02009-02-27 10:55:45 -08002085 */
Robert Love85b4aa42008-12-09 15:10:24 -08002086static void __exit fcoe_exit(void)
2087{
Robert Love5e5e92d2009-03-17 11:41:35 -07002088 unsigned int cpu;
Chris Leech014f5c32009-08-25 13:59:30 -07002089 struct fcoe_interface *fcoe, *tmp;
Chris Leech2e70e242009-08-25 14:00:23 -07002090 struct fcoe_port *port;
Robert Love85b4aa42008-12-09 15:10:24 -08002091
Chris Leechdfc1d0f2009-08-25 14:00:13 -07002092 mutex_lock(&fcoe_config_mutex);
2093
Robert Love85b4aa42008-12-09 15:10:24 -08002094 fcoe_dev_cleanup();
2095
Vasu Dev5919a592009-03-27 09:03:29 -07002096 /* releases the associated fcoe hosts */
Chris Leech090eb6c2009-08-25 14:00:28 -07002097 rtnl_lock();
2098 list_for_each_entry_safe(fcoe, tmp, &fcoe_hostlist, list) {
Chris Leechc863df32009-08-25 14:00:18 -07002099 list_del(&fcoe->list);
Chris Leech2e70e242009-08-25 14:00:23 -07002100 port = lport_priv(fcoe->ctlr.lp);
Chris Leech2e70e242009-08-25 14:00:23 -07002101 fcoe_interface_cleanup(fcoe);
Chris Leech2e70e242009-08-25 14:00:23 -07002102 schedule_work(&port->destroy_work);
Chris Leechc863df32009-08-25 14:00:18 -07002103 }
Chris Leech090eb6c2009-08-25 14:00:28 -07002104 rtnl_unlock();
Robert Love85b4aa42008-12-09 15:10:24 -08002105
Robert Love8976f422009-03-17 11:41:46 -07002106 unregister_hotcpu_notifier(&fcoe_cpu_notifier);
2107
Chris Leech014f5c32009-08-25 13:59:30 -07002108 for_each_online_cpu(cpu)
Robert Love8976f422009-03-17 11:41:46 -07002109 fcoe_percpu_thread_destroy(cpu);
Robert Love85b4aa42008-12-09 15:10:24 -08002110
Chris Leechdfc1d0f2009-08-25 14:00:13 -07002111 mutex_unlock(&fcoe_config_mutex);
Chris Leech2e70e242009-08-25 14:00:23 -07002112
2113 /* flush any asyncronous interface destroys,
2114 * this should happen after the netdev notifier is unregistered */
2115 flush_scheduled_work();
2116
2117 /* detach from scsi transport
2118 * must happen after all destroys are done, therefor after the flush */
2119 fcoe_if_exit();
Robert Love85b4aa42008-12-09 15:10:24 -08002120}
2121module_exit(fcoe_exit);
Chris Leech11b56182009-11-03 11:46:29 -08002122
2123/**
2124 * fcoe_flogi_resp() - FCoE specific FLOGI and FDISC response handler
2125 * @seq: active sequence in the FLOGI or FDISC exchange
2126 * @fp: response frame, or error encoded in a pointer (timeout)
2127 * @arg: pointer the the fcoe_ctlr structure
2128 *
2129 * This handles MAC address managment for FCoE, then passes control on to
2130 * the libfc FLOGI response handler.
2131 */
2132static void fcoe_flogi_resp(struct fc_seq *seq, struct fc_frame *fp, void *arg)
2133{
2134 struct fcoe_ctlr *fip = arg;
2135 struct fc_exch *exch = fc_seq_exch(seq);
2136 struct fc_lport *lport = exch->lp;
2137 u8 *mac;
2138
2139 if (IS_ERR(fp))
2140 goto done;
2141
2142 mac = fr_cb(fp)->granted_mac;
2143 if (is_zero_ether_addr(mac)) {
2144 /* pre-FIP */
2145 mac = eth_hdr(&fp->skb)->h_source;
2146 if (fcoe_ctlr_recv_flogi(fip, lport, fp, mac)) {
2147 fc_frame_free(fp);
2148 return;
2149 }
2150 } else {
2151 /* FIP, libfcoe has already seen it */
2152 fip->update_mac(lport, fr_cb(fp)->granted_mac);
2153 }
2154done:
2155 fc_lport_flogi_resp(seq, fp, lport);
2156}
2157
2158/**
2159 * fcoe_logo_resp() - FCoE specific LOGO response handler
2160 * @seq: active sequence in the LOGO exchange
2161 * @fp: response frame, or error encoded in a pointer (timeout)
2162 * @arg: pointer the the fcoe_ctlr structure
2163 *
2164 * This handles MAC address managment for FCoE, then passes control on to
2165 * the libfc LOGO response handler.
2166 */
2167static void fcoe_logo_resp(struct fc_seq *seq, struct fc_frame *fp, void *arg)
2168{
2169 struct fcoe_ctlr *fip = arg;
2170 struct fc_exch *exch = fc_seq_exch(seq);
2171 struct fc_lport *lport = exch->lp;
2172 static u8 zero_mac[ETH_ALEN] = { 0 };
2173
2174 if (!IS_ERR(fp))
2175 fip->update_mac(lport, zero_mac);
2176 fc_lport_logo_resp(seq, fp, lport);
2177}
2178
2179/**
2180 * fcoe_elsct_send - FCoE specific ELS handler
2181 *
2182 * This does special case handling of FIP encapsualted ELS exchanges for FCoE,
2183 * using FCoE specific response handlers and passing the FIP controller as
2184 * the argument (the lport is still available from the exchange).
2185 *
2186 * Most of the work here is just handed off to the libfc routine.
2187 */
2188static struct fc_seq *fcoe_elsct_send(struct fc_lport *lport,
2189 u32 did, struct fc_frame *fp, unsigned int op,
2190 void (*resp)(struct fc_seq *, struct fc_frame *, void *),
2191 void *arg, u32 timeout)
2192{
2193 struct fcoe_port *port = lport_priv(lport);
2194 struct fcoe_interface *fcoe = port->fcoe;
2195 struct fcoe_ctlr *fip = &fcoe->ctlr;
2196 struct fc_frame_header *fh = fc_frame_header_get(fp);
2197
2198 switch (op) {
2199 case ELS_FLOGI:
2200 case ELS_FDISC:
2201 return fc_elsct_send(lport, did, fp, op, fcoe_flogi_resp,
2202 fip, timeout);
2203 case ELS_LOGO:
2204 /* only hook onto fabric logouts, not port logouts */
2205 if (ntoh24(fh->fh_d_id) != FC_FID_FLOGI)
2206 break;
2207 return fc_elsct_send(lport, did, fp, op, fcoe_logo_resp,
2208 fip, timeout);
2209 }
2210 return fc_elsct_send(lport, did, fp, op, resp, arg, timeout);
2211}
2212