blob: 8f078d306a0a192bf3d28521d1b88543a8054ac1 [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
Chris Leech9a057532009-11-03 11:46:40 -080096static int fcoe_vport_destroy(struct fc_vport *vport);
97static int fcoe_vport_create(struct fc_vport *vport, bool disabled);
98static int fcoe_vport_disable(struct fc_vport *vport, bool disable);
Chris Leechdc8596d2009-11-03 11:47:18 -080099static void fcoe_set_vport_symbolic_name(struct fc_vport *vport);
Chris Leech9a057532009-11-03 11:46:40 -0800100
Vasu Dev7f349142009-03-27 09:06:31 -0700101struct fc_function_template fcoe_transport_function = {
102 .show_host_node_name = 1,
103 .show_host_port_name = 1,
104 .show_host_supported_classes = 1,
105 .show_host_supported_fc4s = 1,
106 .show_host_active_fc4s = 1,
107 .show_host_maxframe_size = 1,
108
109 .show_host_port_id = 1,
110 .show_host_supported_speeds = 1,
111 .get_host_speed = fc_get_host_speed,
112 .show_host_speed = 1,
113 .show_host_port_type = 1,
114 .get_host_port_state = fc_get_host_port_state,
115 .show_host_port_state = 1,
116 .show_host_symbolic_name = 1,
117
118 .dd_fcrport_size = sizeof(struct fc_rport_libfc_priv),
119 .show_rport_maxframe_size = 1,
120 .show_rport_supported_classes = 1,
121
122 .show_host_fabric_name = 1,
123 .show_starget_node_name = 1,
124 .show_starget_port_name = 1,
125 .show_starget_port_id = 1,
126 .set_rport_dev_loss_tmo = fc_set_rport_loss_tmo,
127 .show_rport_dev_loss_tmo = 1,
128 .get_fc_host_stats = fc_get_host_stats,
129 .issue_fc_host_lip = fcoe_reset,
130
131 .terminate_rport_io = fc_rport_terminate_io,
Chris Leech9a057532009-11-03 11:46:40 -0800132
133 .vport_create = fcoe_vport_create,
134 .vport_delete = fcoe_vport_destroy,
135 .vport_disable = fcoe_vport_disable,
Chris Leechdc8596d2009-11-03 11:47:18 -0800136 .set_vport_symbolic_name = fcoe_set_vport_symbolic_name,
Steve Maa51ab392009-11-03 11:47:34 -0800137
138 .bsg_request = fc_lport_bsg_request,
Vasu Dev7f349142009-03-27 09:06:31 -0700139};
140
Chris Leeche9084bb2009-11-03 11:46:34 -0800141struct fc_function_template fcoe_vport_transport_function = {
142 .show_host_node_name = 1,
143 .show_host_port_name = 1,
144 .show_host_supported_classes = 1,
145 .show_host_supported_fc4s = 1,
146 .show_host_active_fc4s = 1,
147 .show_host_maxframe_size = 1,
148
149 .show_host_port_id = 1,
150 .show_host_supported_speeds = 1,
151 .get_host_speed = fc_get_host_speed,
152 .show_host_speed = 1,
153 .show_host_port_type = 1,
154 .get_host_port_state = fc_get_host_port_state,
155 .show_host_port_state = 1,
156 .show_host_symbolic_name = 1,
157
158 .dd_fcrport_size = sizeof(struct fc_rport_libfc_priv),
159 .show_rport_maxframe_size = 1,
160 .show_rport_supported_classes = 1,
161
162 .show_host_fabric_name = 1,
163 .show_starget_node_name = 1,
164 .show_starget_port_name = 1,
165 .show_starget_port_id = 1,
166 .set_rport_dev_loss_tmo = fc_set_rport_loss_tmo,
167 .show_rport_dev_loss_tmo = 1,
168 .get_fc_host_stats = fc_get_host_stats,
169 .issue_fc_host_lip = fcoe_reset,
170
171 .terminate_rport_io = fc_rport_terminate_io,
Steve Maa51ab392009-11-03 11:47:34 -0800172
173 .bsg_request = fc_lport_bsg_request,
Chris Leeche9084bb2009-11-03 11:46:34 -0800174};
175
Vasu Dev7f349142009-03-27 09:06:31 -0700176static struct scsi_host_template fcoe_shost_template = {
177 .module = THIS_MODULE,
178 .name = "FCoE Driver",
179 .proc_name = FCOE_NAME,
180 .queuecommand = fc_queuecommand,
181 .eh_abort_handler = fc_eh_abort,
182 .eh_device_reset_handler = fc_eh_device_reset,
183 .eh_host_reset_handler = fc_eh_host_reset,
184 .slave_alloc = fc_slave_alloc,
185 .change_queue_depth = fc_change_queue_depth,
186 .change_queue_type = fc_change_queue_type,
187 .this_id = -1,
Vasu Dev14caf442009-10-15 17:46:55 -0700188 .cmd_per_lun = 3,
Vasu Dev7f349142009-03-27 09:06:31 -0700189 .can_queue = FCOE_MAX_OUTSTANDING_COMMANDS,
190 .use_clustering = ENABLE_CLUSTERING,
191 .sg_tablesize = SG_ALL,
192 .max_sectors = 0xffff,
193};
194
Chris Leech54b649f2009-08-25 14:00:07 -0700195static int fcoe_fip_recv(struct sk_buff *skb, struct net_device *dev,
196 struct packet_type *ptype,
197 struct net_device *orig_dev);
198/**
199 * fcoe_interface_setup()
200 * @fcoe: new fcoe_interface
201 * @netdev : ptr to the associated netdevice struct
202 *
203 * Returns : 0 for success
Chris Leech2e70e242009-08-25 14:00:23 -0700204 * Locking: must be called with the RTNL mutex held
Chris Leech54b649f2009-08-25 14:00:07 -0700205 */
206static int fcoe_interface_setup(struct fcoe_interface *fcoe,
207 struct net_device *netdev)
208{
209 struct fcoe_ctlr *fip = &fcoe->ctlr;
210 struct netdev_hw_addr *ha;
211 u8 flogi_maddr[ETH_ALEN];
Yi Zoub7a727f2009-10-21 16:28:03 -0700212 const struct net_device_ops *ops;
Chris Leech54b649f2009-08-25 14:00:07 -0700213
214 fcoe->netdev = netdev;
215
Yi Zoub7a727f2009-10-21 16:28:03 -0700216 /* Let LLD initialize for FCoE */
217 ops = netdev->netdev_ops;
218 if (ops->ndo_fcoe_enable) {
219 if (ops->ndo_fcoe_enable(netdev))
220 FCOE_NETDEV_DBG(netdev, "Failed to enable FCoE"
221 " specific feature for LLD.\n");
222 }
223
Chris Leech54b649f2009-08-25 14:00:07 -0700224 /* Do not support for bonding device */
225 if ((netdev->priv_flags & IFF_MASTER_ALB) ||
226 (netdev->priv_flags & IFF_SLAVE_INACTIVE) ||
227 (netdev->priv_flags & IFF_MASTER_8023AD)) {
228 return -EOPNOTSUPP;
229 }
230
231 /* look for SAN MAC address, if multiple SAN MACs exist, only
232 * use the first one for SPMA */
233 rcu_read_lock();
234 for_each_dev_addr(netdev, ha) {
235 if ((ha->type == NETDEV_HW_ADDR_T_SAN) &&
236 (is_valid_ether_addr(fip->ctl_src_addr))) {
237 memcpy(fip->ctl_src_addr, ha->addr, ETH_ALEN);
238 fip->spma = 1;
239 break;
240 }
241 }
242 rcu_read_unlock();
243
244 /* setup Source Mac Address */
245 if (!fip->spma)
246 memcpy(fip->ctl_src_addr, netdev->dev_addr, netdev->addr_len);
247
248 /*
249 * Add FCoE MAC address as second unicast MAC address
250 * or enter promiscuous mode if not capable of listening
251 * for multiple unicast MACs.
252 */
Chris Leech54b649f2009-08-25 14:00:07 -0700253 memcpy(flogi_maddr, (u8[6]) FC_FCOE_FLOGI_MAC, ETH_ALEN);
254 dev_unicast_add(netdev, flogi_maddr);
255 if (fip->spma)
256 dev_unicast_add(netdev, fip->ctl_src_addr);
257 dev_mc_add(netdev, FIP_ALL_ENODE_MACS, ETH_ALEN, 0);
Chris Leech54b649f2009-08-25 14:00:07 -0700258
259 /*
260 * setup the receive function from ethernet driver
261 * on the ethertype for the given device
262 */
263 fcoe->fcoe_packet_type.func = fcoe_rcv;
264 fcoe->fcoe_packet_type.type = __constant_htons(ETH_P_FCOE);
265 fcoe->fcoe_packet_type.dev = netdev;
266 dev_add_pack(&fcoe->fcoe_packet_type);
267
268 fcoe->fip_packet_type.func = fcoe_fip_recv;
269 fcoe->fip_packet_type.type = htons(ETH_P_FIP);
270 fcoe->fip_packet_type.dev = netdev;
271 dev_add_pack(&fcoe->fip_packet_type);
272
273 return 0;
274}
275
276static void fcoe_fip_send(struct fcoe_ctlr *fip, struct sk_buff *skb);
Chris Leech11b56182009-11-03 11:46:29 -0800277static void fcoe_update_src_mac(struct fc_lport *lport, u8 *addr);
278static u8 *fcoe_get_src_mac(struct fc_lport *lport);
Chris Leech2e70e242009-08-25 14:00:23 -0700279static void fcoe_destroy_work(struct work_struct *work);
Chris Leech54b649f2009-08-25 14:00:07 -0700280
Vasu Dev7f349142009-03-27 09:06:31 -0700281/**
Chris Leech030f4e02009-08-25 14:00:02 -0700282 * fcoe_interface_create()
283 * @netdev: network interface
284 *
285 * Returns: pointer to a struct fcoe_interface or NULL on error
286 */
287static struct fcoe_interface *fcoe_interface_create(struct net_device *netdev)
288{
289 struct fcoe_interface *fcoe;
290
291 fcoe = kzalloc(sizeof(*fcoe), GFP_KERNEL);
292 if (!fcoe) {
293 FCOE_NETDEV_DBG(netdev, "Could not allocate fcoe structure\n");
294 return NULL;
295 }
296
Chris Leech2e70e242009-08-25 14:00:23 -0700297 dev_hold(netdev);
Chris Leech030f4e02009-08-25 14:00:02 -0700298 kref_init(&fcoe->kref);
Chris Leech54b649f2009-08-25 14:00:07 -0700299
300 /*
301 * Initialize FIP.
302 */
303 fcoe_ctlr_init(&fcoe->ctlr);
304 fcoe->ctlr.send = fcoe_fip_send;
305 fcoe->ctlr.update_mac = fcoe_update_src_mac;
Chris Leech11b56182009-11-03 11:46:29 -0800306 fcoe->ctlr.get_src_addr = fcoe_get_src_mac;
Chris Leech54b649f2009-08-25 14:00:07 -0700307
308 fcoe_interface_setup(fcoe, netdev);
Chris Leech030f4e02009-08-25 14:00:02 -0700309
310 return fcoe;
311}
312
313/**
Chris Leech54b649f2009-08-25 14:00:07 -0700314 * fcoe_interface_cleanup() - clean up netdev configurations
315 * @fcoe:
Chris Leech2e70e242009-08-25 14:00:23 -0700316 *
317 * Caller must be holding the RTNL mutex
Chris Leech54b649f2009-08-25 14:00:07 -0700318 */
319void fcoe_interface_cleanup(struct fcoe_interface *fcoe)
320{
321 struct net_device *netdev = fcoe->netdev;
322 struct fcoe_ctlr *fip = &fcoe->ctlr;
323 u8 flogi_maddr[ETH_ALEN];
Yi Zoub7a727f2009-10-21 16:28:03 -0700324 const struct net_device_ops *ops;
Chris Leech54b649f2009-08-25 14:00:07 -0700325
326 /*
327 * Don't listen for Ethernet packets anymore.
328 * synchronize_net() ensures that the packet handlers are not running
329 * on another CPU. dev_remove_pack() would do that, this calls the
330 * unsyncronized version __dev_remove_pack() to avoid multiple delays.
331 */
332 __dev_remove_pack(&fcoe->fcoe_packet_type);
333 __dev_remove_pack(&fcoe->fip_packet_type);
334 synchronize_net();
335
Chris Leech54b649f2009-08-25 14:00:07 -0700336 /* Delete secondary MAC addresses */
Chris Leech54b649f2009-08-25 14:00:07 -0700337 memcpy(flogi_maddr, (u8[6]) FC_FCOE_FLOGI_MAC, ETH_ALEN);
338 dev_unicast_delete(netdev, flogi_maddr);
Chris Leech54b649f2009-08-25 14:00:07 -0700339 if (fip->spma)
340 dev_unicast_delete(netdev, fip->ctl_src_addr);
341 dev_mc_delete(netdev, FIP_ALL_ENODE_MACS, ETH_ALEN, 0);
Yi Zoub7a727f2009-10-21 16:28:03 -0700342
343 /* Tell the LLD we are done w/ FCoE */
344 ops = netdev->netdev_ops;
345 if (ops->ndo_fcoe_disable) {
346 if (ops->ndo_fcoe_disable(netdev))
347 FCOE_NETDEV_DBG(netdev, "Failed to disable FCoE"
348 " specific feature for LLD.\n");
349 }
Chris Leech54b649f2009-08-25 14:00:07 -0700350}
351
352/**
Chris Leech030f4e02009-08-25 14:00:02 -0700353 * fcoe_interface_release() - fcoe_port kref release function
354 * @kref: embedded reference count in an fcoe_interface struct
355 */
356static void fcoe_interface_release(struct kref *kref)
357{
358 struct fcoe_interface *fcoe;
Chris Leech2e70e242009-08-25 14:00:23 -0700359 struct net_device *netdev;
Chris Leech030f4e02009-08-25 14:00:02 -0700360
361 fcoe = container_of(kref, struct fcoe_interface, kref);
Chris Leech2e70e242009-08-25 14:00:23 -0700362 netdev = fcoe->netdev;
363 /* tear-down the FCoE controller */
364 fcoe_ctlr_destroy(&fcoe->ctlr);
Chris Leech030f4e02009-08-25 14:00:02 -0700365 kfree(fcoe);
Chris Leech2e70e242009-08-25 14:00:23 -0700366 dev_put(netdev);
Chris Leech030f4e02009-08-25 14:00:02 -0700367}
368
369/**
370 * fcoe_interface_get()
371 * @fcoe:
372 */
373static inline void fcoe_interface_get(struct fcoe_interface *fcoe)
374{
375 kref_get(&fcoe->kref);
376}
377
378/**
379 * fcoe_interface_put()
380 * @fcoe:
381 */
382static inline void fcoe_interface_put(struct fcoe_interface *fcoe)
383{
384 kref_put(&fcoe->kref, fcoe_interface_release);
385}
386
387/**
Vasu Devab6b85c2009-05-17 12:33:08 +0000388 * fcoe_fip_recv - handle a received FIP frame.
389 * @skb: the receive skb
390 * @dev: associated &net_device
391 * @ptype: the &packet_type structure which was used to register this handler.
392 * @orig_dev: original receive &net_device, in case @dev is a bond.
393 *
394 * Returns: 0 for success
395 */
396static int fcoe_fip_recv(struct sk_buff *skb, struct net_device *dev,
397 struct packet_type *ptype,
398 struct net_device *orig_dev)
399{
Chris Leech259ad852009-08-25 13:59:41 -0700400 struct fcoe_interface *fcoe;
Vasu Devab6b85c2009-05-17 12:33:08 +0000401
Chris Leech259ad852009-08-25 13:59:41 -0700402 fcoe = container_of(ptype, struct fcoe_interface, fip_packet_type);
Chris Leech3fe9a0b2009-08-25 13:59:46 -0700403 fcoe_ctlr_recv(&fcoe->ctlr, skb);
Vasu Devab6b85c2009-05-17 12:33:08 +0000404 return 0;
405}
406
407/**
408 * fcoe_fip_send() - send an Ethernet-encapsulated FIP frame.
409 * @fip: FCoE controller.
410 * @skb: FIP Packet.
411 */
412static void fcoe_fip_send(struct fcoe_ctlr *fip, struct sk_buff *skb)
413{
Chris Leech3fe9a0b2009-08-25 13:59:46 -0700414 skb->dev = fcoe_from_ctlr(fip)->netdev;
Vasu Devab6b85c2009-05-17 12:33:08 +0000415 dev_queue_xmit(skb);
416}
417
418/**
419 * fcoe_update_src_mac() - Update Ethernet MAC filters.
Chris Leech11b56182009-11-03 11:46:29 -0800420 * @lport: libfc lport
421 * @addr: Unicast MAC address to add.
Vasu Devab6b85c2009-05-17 12:33:08 +0000422 *
423 * Remove any previously-set unicast MAC filter.
424 * Add secondary FCoE MAC address filter for our OUI.
425 */
Chris Leech11b56182009-11-03 11:46:29 -0800426static void fcoe_update_src_mac(struct fc_lport *lport, u8 *addr)
Vasu Devab6b85c2009-05-17 12:33:08 +0000427{
Chris Leech11b56182009-11-03 11:46:29 -0800428 struct fcoe_port *port = lport_priv(lport);
429 struct fcoe_interface *fcoe = port->fcoe;
Vasu Devab6b85c2009-05-17 12:33:08 +0000430
Vasu Devab6b85c2009-05-17 12:33:08 +0000431 rtnl_lock();
Chris Leech11b56182009-11-03 11:46:29 -0800432 if (!is_zero_ether_addr(port->data_src_addr))
433 dev_unicast_delete(fcoe->netdev, port->data_src_addr);
434 if (!is_zero_ether_addr(addr))
435 dev_unicast_add(fcoe->netdev, addr);
436 memcpy(port->data_src_addr, addr, ETH_ALEN);
Vasu Devab6b85c2009-05-17 12:33:08 +0000437 rtnl_unlock();
438}
439
440/**
Chris Leech11b56182009-11-03 11:46:29 -0800441 * fcoe_get_src_mac() - return the Ethernet source address for an lport
442 * @lport: libfc lport
443 */
444static u8 *fcoe_get_src_mac(struct fc_lport *lport)
445{
446 struct fcoe_port *port = lport_priv(lport);
447
448 return port->data_src_addr;
449}
450
451/**
Vasu Dev7f349142009-03-27 09:06:31 -0700452 * fcoe_lport_config() - sets up the fc_lport
453 * @lp: ptr to the fc_lport
Vasu Dev7f349142009-03-27 09:06:31 -0700454 *
455 * Returns: 0 for success
456 */
457static int fcoe_lport_config(struct fc_lport *lp)
458{
459 lp->link_up = 0;
460 lp->qfull = 0;
461 lp->max_retry_count = 3;
Abhijeet Joglekara3666952009-05-01 10:01:26 -0700462 lp->max_rport_retry_count = 3;
Vasu Dev7f349142009-03-27 09:06:31 -0700463 lp->e_d_tov = 2 * 1000; /* FC-FS default */
464 lp->r_a_tov = 2 * 2 * 1000;
465 lp->service_params = (FCP_SPPF_INIT_FCN | FCP_SPPF_RD_XRDY_DIS |
466 FCP_SPPF_RETRY | FCP_SPPF_CONF_COMPL);
Chris Leech9a057532009-11-03 11:46:40 -0800467 lp->does_npiv = 1;
Vasu Dev7f349142009-03-27 09:06:31 -0700468
469 fc_lport_init_stats(lp);
470
471 /* lport fc_lport related configuration */
472 fc_lport_config(lp);
473
474 /* offload related configuration */
475 lp->crc_offload = 0;
476 lp->seq_offload = 0;
477 lp->lro_enabled = 0;
478 lp->lro_xid = 0;
479 lp->lso_max = 0;
480
481 return 0;
482}
483
484/**
Vasu Dev1047f222009-05-06 10:52:40 -0700485 * fcoe_queue_timer() - fcoe queue timer
486 * @lp: the fc_lport pointer
487 *
488 * Calls fcoe_check_wait_queue on timeout
489 *
490 */
491static void fcoe_queue_timer(ulong lp)
492{
493 fcoe_check_wait_queue((struct fc_lport *)lp, NULL);
494}
495
496/**
Vasu Dev7f349142009-03-27 09:06:31 -0700497 * fcoe_netdev_config() - Set up netdev for SW FCoE
498 * @lp : ptr to the fc_lport
499 * @netdev : ptr to the associated netdevice struct
500 *
501 * Must be called after fcoe_lport_config() as it will use lport mutex
502 *
503 * Returns : 0 for success
504 */
505static int fcoe_netdev_config(struct fc_lport *lp, struct net_device *netdev)
506{
507 u32 mfs;
508 u64 wwnn, wwpn;
Chris Leech25024982009-08-25 13:59:35 -0700509 struct fcoe_interface *fcoe;
Chris Leech014f5c32009-08-25 13:59:30 -0700510 struct fcoe_port *port;
Vasu Dev7f349142009-03-27 09:06:31 -0700511
512 /* Setup lport private data to point to fcoe softc */
Chris Leech014f5c32009-08-25 13:59:30 -0700513 port = lport_priv(lp);
Chris Leech25024982009-08-25 13:59:35 -0700514 fcoe = port->fcoe;
Vasu Dev7f349142009-03-27 09:06:31 -0700515
516 /*
517 * Determine max frame size based on underlying device and optional
518 * user-configured limit. If the MFS is too low, fcoe_link_ok()
519 * will return 0, so do this first.
520 */
Yi Zou7221d7e2009-10-21 16:27:52 -0700521 mfs = netdev->mtu;
522 if (netdev->features & NETIF_F_FCOE_MTU) {
523 mfs = FCOE_MTU;
524 FCOE_NETDEV_DBG(netdev, "Supports FCOE_MTU of %d bytes\n", mfs);
525 }
526 mfs -= (sizeof(struct fcoe_hdr) + sizeof(struct fcoe_crc_eof));
Vasu Dev7f349142009-03-27 09:06:31 -0700527 if (fc_set_mfs(lp, mfs))
528 return -EINVAL;
529
Vasu Dev7f349142009-03-27 09:06:31 -0700530 /* offload features support */
Vasu Dev1d1b88d2009-07-29 17:05:45 -0700531 if (netdev->features & NETIF_F_SG)
Vasu Dev7f349142009-03-27 09:06:31 -0700532 lp->sg_supp = 1;
533
Vasu Dev7f349142009-03-27 09:06:31 -0700534 if (netdev->features & NETIF_F_FCOE_CRC) {
535 lp->crc_offload = 1;
Robert Loved5488eb2009-06-10 15:30:59 -0700536 FCOE_NETDEV_DBG(netdev, "Supports FCCRC offload\n");
Vasu Dev7f349142009-03-27 09:06:31 -0700537 }
Vasu Dev7f349142009-03-27 09:06:31 -0700538 if (netdev->features & NETIF_F_FSO) {
539 lp->seq_offload = 1;
540 lp->lso_max = netdev->gso_max_size;
Robert Loved5488eb2009-06-10 15:30:59 -0700541 FCOE_NETDEV_DBG(netdev, "Supports LSO for max len 0x%x\n",
542 lp->lso_max);
Vasu Dev7f349142009-03-27 09:06:31 -0700543 }
Vasu Dev7f349142009-03-27 09:06:31 -0700544 if (netdev->fcoe_ddp_xid) {
545 lp->lro_enabled = 1;
546 lp->lro_xid = netdev->fcoe_ddp_xid;
Robert Loved5488eb2009-06-10 15:30:59 -0700547 FCOE_NETDEV_DBG(netdev, "Supports LRO for max xid 0x%x\n",
548 lp->lro_xid);
Vasu Dev7f349142009-03-27 09:06:31 -0700549 }
Chris Leech014f5c32009-08-25 13:59:30 -0700550 skb_queue_head_init(&port->fcoe_pending_queue);
551 port->fcoe_pending_queue_active = 0;
552 setup_timer(&port->timer, fcoe_queue_timer, (unsigned long)lp);
Vasu Dev7f349142009-03-27 09:06:31 -0700553
Chris Leech9a057532009-11-03 11:46:40 -0800554 if (!lp->vport) {
555 wwnn = fcoe_wwn_from_mac(netdev->dev_addr, 1, 0);
556 fc_set_wwnn(lp, wwnn);
557 /* XXX - 3rd arg needs to be vlan id */
558 wwpn = fcoe_wwn_from_mac(netdev->dev_addr, 2, 0);
559 fc_set_wwpn(lp, wwpn);
560 }
Vasu Dev7f349142009-03-27 09:06:31 -0700561
Vasu Dev7f349142009-03-27 09:06:31 -0700562 return 0;
563}
564
565/**
566 * fcoe_shost_config() - Sets up fc_lport->host
567 * @lp : ptr to the fc_lport
568 * @shost : ptr to the associated scsi host
569 * @dev : device associated to scsi host
570 *
571 * Must be called after fcoe_lport_config() and fcoe_netdev_config()
572 *
573 * Returns : 0 for success
574 */
575static int fcoe_shost_config(struct fc_lport *lp, struct Scsi_Host *shost,
576 struct device *dev)
577{
578 int rc = 0;
579
580 /* lport scsi host config */
Vasu Dev7f349142009-03-27 09:06:31 -0700581 lp->host->max_lun = FCOE_MAX_LUN;
582 lp->host->max_id = FCOE_MAX_FCP_TARGET;
583 lp->host->max_channel = 0;
Chris Leeche9084bb2009-11-03 11:46:34 -0800584 if (lp->vport)
585 lp->host->transportt = fcoe_vport_transport_template;
586 else
587 lp->host->transportt = fcoe_transport_template;
Vasu Dev7f349142009-03-27 09:06:31 -0700588
589 /* add the new host to the SCSI-ml */
590 rc = scsi_add_host(lp->host, dev);
591 if (rc) {
Robert Loved5488eb2009-06-10 15:30:59 -0700592 FCOE_NETDEV_DBG(fcoe_netdev(lp), "fcoe_shost_config: "
593 "error on scsi_add_host\n");
Vasu Dev7f349142009-03-27 09:06:31 -0700594 return rc;
595 }
Chris Leech9a057532009-11-03 11:46:40 -0800596
597 if (!lp->vport)
598 fc_host_max_npiv_vports(lp->host) = USHORT_MAX;
599
Chris Leech5baa17c2009-11-03 11:46:56 -0800600 snprintf(fc_host_symbolic_name(lp->host), FC_SYMBOLIC_NAME_SIZE,
601 "%s v%s over %s", FCOE_NAME, FCOE_VERSION,
602 fcoe_netdev(lp)->name);
Vasu Dev7f349142009-03-27 09:06:31 -0700603
604 return 0;
605}
606
Vasu Devd7179682009-07-29 17:05:21 -0700607/*
608 * fcoe_oem_match() - match for read types IO
609 * @fp: the fc_frame for new IO.
610 *
611 * Returns : true for read types IO, otherwise returns false.
612 */
613bool fcoe_oem_match(struct fc_frame *fp)
614{
Yi Zou05cc7392009-08-25 13:59:03 -0700615 return fc_fcp_is_read(fr_fsp(fp)) &&
616 (fr_fsp(fp)->data_len > fcoe_ddp_min);
Vasu Devd7179682009-07-29 17:05:21 -0700617}
618
Vasu Dev7f349142009-03-27 09:06:31 -0700619/**
620 * fcoe_em_config() - allocates em for this lport
Chris Leech014f5c32009-08-25 13:59:30 -0700621 * @lp: the fcoe that em is to allocated for
Vasu Dev7f349142009-03-27 09:06:31 -0700622 *
623 * Returns : 0 on success
624 */
625static inline int fcoe_em_config(struct fc_lport *lp)
626{
Chris Leech014f5c32009-08-25 13:59:30 -0700627 struct fcoe_port *port = lport_priv(lp);
Chris Leech25024982009-08-25 13:59:35 -0700628 struct fcoe_interface *fcoe = port->fcoe;
629 struct fcoe_interface *oldfcoe = NULL;
Vasu Dev1d1b88d2009-07-29 17:05:45 -0700630 struct net_device *old_real_dev, *cur_real_dev;
Vasu Devd7179682009-07-29 17:05:21 -0700631 u16 min_xid = FCOE_MIN_XID;
632 u16 max_xid = FCOE_MAX_XID;
633
634 /*
635 * Check if need to allocate an em instance for
636 * offload exchange ids to be shared across all VN_PORTs/lport.
637 */
638 if (!lp->lro_enabled || !lp->lro_xid || (lp->lro_xid >= max_xid)) {
639 lp->lro_xid = 0;
640 goto skip_oem;
641 }
642
643 /*
644 * Reuse existing offload em instance in case
Vasu Dev1d1b88d2009-07-29 17:05:45 -0700645 * it is already allocated on real eth device
Vasu Devd7179682009-07-29 17:05:21 -0700646 */
Chris Leech25024982009-08-25 13:59:35 -0700647 if (fcoe->netdev->priv_flags & IFF_802_1Q_VLAN)
648 cur_real_dev = vlan_dev_real_dev(fcoe->netdev);
Vasu Dev1d1b88d2009-07-29 17:05:45 -0700649 else
Chris Leech25024982009-08-25 13:59:35 -0700650 cur_real_dev = fcoe->netdev;
Vasu Dev1d1b88d2009-07-29 17:05:45 -0700651
Chris Leech25024982009-08-25 13:59:35 -0700652 list_for_each_entry(oldfcoe, &fcoe_hostlist, list) {
Chris Leech25024982009-08-25 13:59:35 -0700653 if (oldfcoe->netdev->priv_flags & IFF_802_1Q_VLAN)
654 old_real_dev = vlan_dev_real_dev(oldfcoe->netdev);
Vasu Dev1d1b88d2009-07-29 17:05:45 -0700655 else
Chris Leech25024982009-08-25 13:59:35 -0700656 old_real_dev = oldfcoe->netdev;
Vasu Dev1d1b88d2009-07-29 17:05:45 -0700657
658 if (cur_real_dev == old_real_dev) {
Chris Leech991cbb62009-08-25 13:59:51 -0700659 fcoe->oem = oldfcoe->oem;
Vasu Devd7179682009-07-29 17:05:21 -0700660 break;
661 }
662 }
663
Chris Leech991cbb62009-08-25 13:59:51 -0700664 if (fcoe->oem) {
665 if (!fc_exch_mgr_add(lp, fcoe->oem, fcoe_oem_match)) {
Vasu Devd7179682009-07-29 17:05:21 -0700666 printk(KERN_ERR "fcoe_em_config: failed to add "
667 "offload em:%p on interface:%s\n",
Chris Leech991cbb62009-08-25 13:59:51 -0700668 fcoe->oem, fcoe->netdev->name);
Vasu Devd7179682009-07-29 17:05:21 -0700669 return -ENOMEM;
670 }
671 } else {
Chris Leech991cbb62009-08-25 13:59:51 -0700672 fcoe->oem = fc_exch_mgr_alloc(lp, FC_CLASS_3,
Vasu Devd7179682009-07-29 17:05:21 -0700673 FCOE_MIN_XID, lp->lro_xid,
674 fcoe_oem_match);
Chris Leech991cbb62009-08-25 13:59:51 -0700675 if (!fcoe->oem) {
Vasu Devd7179682009-07-29 17:05:21 -0700676 printk(KERN_ERR "fcoe_em_config: failed to allocate "
677 "em for offload exches on interface:%s\n",
Chris Leech25024982009-08-25 13:59:35 -0700678 fcoe->netdev->name);
Vasu Devd7179682009-07-29 17:05:21 -0700679 return -ENOMEM;
680 }
681 }
682
683 /*
684 * Exclude offload EM xid range from next EM xid range.
685 */
686 min_xid += lp->lro_xid + 1;
687
688skip_oem:
689 if (!fc_exch_mgr_alloc(lp, FC_CLASS_3, min_xid, max_xid, NULL)) {
690 printk(KERN_ERR "fcoe_em_config: failed to "
Chris Leech25024982009-08-25 13:59:35 -0700691 "allocate em on interface %s\n", fcoe->netdev->name);
Vasu Dev7f349142009-03-27 09:06:31 -0700692 return -ENOMEM;
Vasu Devd7179682009-07-29 17:05:21 -0700693 }
Vasu Dev7f349142009-03-27 09:06:31 -0700694
695 return 0;
696}
697
698/**
699 * fcoe_if_destroy() - FCoE software HBA tear-down function
Chris Leechaf7f85d2009-08-25 13:59:24 -0700700 * @lport: fc_lport to destroy
Vasu Dev7f349142009-03-27 09:06:31 -0700701 */
Chris Leechaf7f85d2009-08-25 13:59:24 -0700702static void fcoe_if_destroy(struct fc_lport *lport)
Vasu Dev7f349142009-03-27 09:06:31 -0700703{
Chris Leech014f5c32009-08-25 13:59:30 -0700704 struct fcoe_port *port = lport_priv(lport);
705 struct fcoe_interface *fcoe = port->fcoe;
Chris Leech25024982009-08-25 13:59:35 -0700706 struct net_device *netdev = fcoe->netdev;
Vasu Dev7f349142009-03-27 09:06:31 -0700707
Robert Loved5488eb2009-06-10 15:30:59 -0700708 FCOE_NETDEV_DBG(netdev, "Destroying interface\n");
Vasu Dev7f349142009-03-27 09:06:31 -0700709
Vasu Dev7f349142009-03-27 09:06:31 -0700710 /* Logout of the fabric */
Chris Leechaf7f85d2009-08-25 13:59:24 -0700711 fc_fabric_logoff(lport);
Vasu Dev7f349142009-03-27 09:06:31 -0700712
Vasu Dev7f349142009-03-27 09:06:31 -0700713 /* Cleanup the fc_lport */
Chris Leechaf7f85d2009-08-25 13:59:24 -0700714 fc_lport_destroy(lport);
715 fc_fcp_destroy(lport);
Vasu Dev7f349142009-03-27 09:06:31 -0700716
Chris Leech54b649f2009-08-25 14:00:07 -0700717 /* Stop the transmit retry timer */
718 del_timer_sync(&port->timer);
719
720 /* Free existing transmit skbs */
721 fcoe_clean_pending_queue(lport);
722
Chris Leech11b56182009-11-03 11:46:29 -0800723 rtnl_lock();
724 if (!is_zero_ether_addr(port->data_src_addr))
725 dev_unicast_delete(netdev, port->data_src_addr);
726 rtnl_unlock();
727
Chris Leech54b649f2009-08-25 14:00:07 -0700728 /* receives may not be stopped until after this */
729 fcoe_interface_put(fcoe);
730
731 /* Free queued packets for the per-CPU receive threads */
732 fcoe_percpu_clean(lport);
733
Vasu Dev7f349142009-03-27 09:06:31 -0700734 /* Detach from the scsi-ml */
Chris Leechaf7f85d2009-08-25 13:59:24 -0700735 fc_remove_host(lport->host);
736 scsi_remove_host(lport->host);
Vasu Dev7f349142009-03-27 09:06:31 -0700737
738 /* There are no more rports or I/O, free the EM */
Chris Leechaf7f85d2009-08-25 13:59:24 -0700739 fc_exch_mgr_free(lport);
Vasu Dev7f349142009-03-27 09:06:31 -0700740
Vasu Dev7f349142009-03-27 09:06:31 -0700741 /* Free memory used by statistical counters */
Chris Leechaf7f85d2009-08-25 13:59:24 -0700742 fc_lport_free_stats(lport);
Vasu Dev7f349142009-03-27 09:06:31 -0700743
Chris Leech2e70e242009-08-25 14:00:23 -0700744 /* Release the Scsi_Host */
Chris Leechaf7f85d2009-08-25 13:59:24 -0700745 scsi_host_put(lport->host);
Vasu Dev7f349142009-03-27 09:06:31 -0700746}
747
748/*
749 * fcoe_ddp_setup - calls LLD's ddp_setup through net_device
750 * @lp: the corresponding fc_lport
751 * @xid: the exchange id for this ddp transfer
752 * @sgl: the scatterlist describing this transfer
753 * @sgc: number of sg items
754 *
755 * Returns : 0 no ddp
756 */
757static int fcoe_ddp_setup(struct fc_lport *lp, u16 xid,
758 struct scatterlist *sgl, unsigned int sgc)
759{
760 struct net_device *n = fcoe_netdev(lp);
761
Yi Zoub04d0232009-10-21 16:26:55 -0700762 if (n->netdev_ops->ndo_fcoe_ddp_setup)
Vasu Dev7f349142009-03-27 09:06:31 -0700763 return n->netdev_ops->ndo_fcoe_ddp_setup(n, xid, sgl, sgc);
764
765 return 0;
766}
767
768/*
769 * fcoe_ddp_done - calls LLD's ddp_done through net_device
770 * @lp: the corresponding fc_lport
771 * @xid: the exchange id for this ddp transfer
772 *
773 * Returns : the length of data that have been completed by ddp
774 */
775static int fcoe_ddp_done(struct fc_lport *lp, u16 xid)
776{
777 struct net_device *n = fcoe_netdev(lp);
778
Yi Zoub04d0232009-10-21 16:26:55 -0700779 if (n->netdev_ops->ndo_fcoe_ddp_done)
Vasu Dev7f349142009-03-27 09:06:31 -0700780 return n->netdev_ops->ndo_fcoe_ddp_done(n, xid);
781 return 0;
782}
783
Chris Leech11b56182009-11-03 11:46:29 -0800784static struct fc_seq *fcoe_elsct_send(struct fc_lport *lport,
785 u32 did, struct fc_frame *fp, unsigned int op,
786 void (*resp)(struct fc_seq *, struct fc_frame *, void *),
787 void *arg, u32 timeout);
788
Vasu Dev7f349142009-03-27 09:06:31 -0700789static struct libfc_function_template fcoe_libfc_fcn_templ = {
790 .frame_send = fcoe_xmit,
791 .ddp_setup = fcoe_ddp_setup,
792 .ddp_done = fcoe_ddp_done,
Chris Leech11b56182009-11-03 11:46:29 -0800793 .elsct_send = fcoe_elsct_send,
Vasu Dev7f349142009-03-27 09:06:31 -0700794};
795
796/**
Chris Leech030f4e02009-08-25 14:00:02 -0700797 * fcoe_if_create() - this function creates the fcoe port
798 * @fcoe: fcoe_interface structure to create an fc_lport instance on
Chris Leechaf7f85d2009-08-25 13:59:24 -0700799 * @parent: device pointer to be the parent in sysfs for the SCSI host
Chris Leech9a057532009-11-03 11:46:40 -0800800 * @npiv: is this a vport?
Vasu Dev7f349142009-03-27 09:06:31 -0700801 *
Chris Leech54b649f2009-08-25 14:00:07 -0700802 * Creates fc_lport struct and scsi_host for lport, configures lport.
Vasu Dev7f349142009-03-27 09:06:31 -0700803 *
Chris Leechaf7f85d2009-08-25 13:59:24 -0700804 * Returns : The allocated fc_lport or an error pointer
Vasu Dev7f349142009-03-27 09:06:31 -0700805 */
Chris Leech030f4e02009-08-25 14:00:02 -0700806static struct fc_lport *fcoe_if_create(struct fcoe_interface *fcoe,
Chris Leech9a057532009-11-03 11:46:40 -0800807 struct device *parent, int npiv)
Vasu Dev7f349142009-03-27 09:06:31 -0700808{
809 int rc;
Chris Leechaf7f85d2009-08-25 13:59:24 -0700810 struct fc_lport *lport = NULL;
Chris Leech014f5c32009-08-25 13:59:30 -0700811 struct fcoe_port *port;
Vasu Dev7f349142009-03-27 09:06:31 -0700812 struct Scsi_Host *shost;
Chris Leech030f4e02009-08-25 14:00:02 -0700813 struct net_device *netdev = fcoe->netdev;
Chris Leech9a057532009-11-03 11:46:40 -0800814 /*
815 * parent is only a vport if npiv is 1,
816 * but we'll only use vport in that case so go ahead and set it
817 */
818 struct fc_vport *vport = dev_to_vport(parent);
Vasu Dev7f349142009-03-27 09:06:31 -0700819
Robert Loved5488eb2009-06-10 15:30:59 -0700820 FCOE_NETDEV_DBG(netdev, "Create Interface\n");
Vasu Dev7f349142009-03-27 09:06:31 -0700821
Chris Leech9a057532009-11-03 11:46:40 -0800822 if (!npiv) {
823 lport = libfc_host_alloc(&fcoe_shost_template,
824 sizeof(struct fcoe_port));
825 } else {
826 lport = libfc_vport_create(vport,
827 sizeof(struct fcoe_port));
828 }
Chris Leech86221962009-11-03 11:46:08 -0800829 if (!lport) {
Chris Leech014f5c32009-08-25 13:59:30 -0700830 FCOE_NETDEV_DBG(netdev, "Could not allocate host structure\n");
831 rc = -ENOMEM;
Chris Leech030f4e02009-08-25 14:00:02 -0700832 goto out;
Chris Leech014f5c32009-08-25 13:59:30 -0700833 }
Chris Leech86221962009-11-03 11:46:08 -0800834 shost = lport->host;
Chris Leech014f5c32009-08-25 13:59:30 -0700835 port = lport_priv(lport);
Chris Leech2e70e242009-08-25 14:00:23 -0700836 port->lport = lport;
Chris Leech014f5c32009-08-25 13:59:30 -0700837 port->fcoe = fcoe;
Chris Leech2e70e242009-08-25 14:00:23 -0700838 INIT_WORK(&port->destroy_work, fcoe_destroy_work);
Vasu Dev7f349142009-03-27 09:06:31 -0700839
840 /* configure fc_lport, e.g., em */
Chris Leechaf7f85d2009-08-25 13:59:24 -0700841 rc = fcoe_lport_config(lport);
Vasu Dev7f349142009-03-27 09:06:31 -0700842 if (rc) {
Robert Loved5488eb2009-06-10 15:30:59 -0700843 FCOE_NETDEV_DBG(netdev, "Could not configure lport for the "
844 "interface\n");
Vasu Dev7f349142009-03-27 09:06:31 -0700845 goto out_host_put;
846 }
847
Chris Leech9a057532009-11-03 11:46:40 -0800848 if (npiv) {
849 FCOE_NETDEV_DBG(netdev, "Setting vport names, 0x%llX 0x%llX\n",
850 vport->node_name, vport->port_name);
851 fc_set_wwnn(lport, vport->node_name);
852 fc_set_wwpn(lport, vport->port_name);
853 }
854
Vasu Devab6b85c2009-05-17 12:33:08 +0000855 /* configure lport network properties */
Chris Leechaf7f85d2009-08-25 13:59:24 -0700856 rc = fcoe_netdev_config(lport, netdev);
Vasu Devab6b85c2009-05-17 12:33:08 +0000857 if (rc) {
Robert Loved5488eb2009-06-10 15:30:59 -0700858 FCOE_NETDEV_DBG(netdev, "Could not configure netdev for the "
859 "interface\n");
Chris Leech54b649f2009-08-25 14:00:07 -0700860 goto out_lp_destroy;
Vasu Devab6b85c2009-05-17 12:33:08 +0000861 }
Joe Eykholt97c83892009-03-17 11:42:40 -0700862
Vasu Dev7f349142009-03-27 09:06:31 -0700863 /* configure lport scsi host properties */
Chris Leechaf7f85d2009-08-25 13:59:24 -0700864 rc = fcoe_shost_config(lport, shost, parent);
Vasu Dev7f349142009-03-27 09:06:31 -0700865 if (rc) {
Robert Loved5488eb2009-06-10 15:30:59 -0700866 FCOE_NETDEV_DBG(netdev, "Could not configure shost for the "
867 "interface\n");
Chris Leech54b649f2009-08-25 14:00:07 -0700868 goto out_lp_destroy;
Vasu Dev7f349142009-03-27 09:06:31 -0700869 }
870
Vasu Dev7f349142009-03-27 09:06:31 -0700871 /* Initialize the library */
Chris Leechaf7f85d2009-08-25 13:59:24 -0700872 rc = fcoe_libfc_config(lport, &fcoe_libfc_fcn_templ);
Vasu Dev7f349142009-03-27 09:06:31 -0700873 if (rc) {
Robert Loved5488eb2009-06-10 15:30:59 -0700874 FCOE_NETDEV_DBG(netdev, "Could not configure libfc for the "
875 "interface\n");
Vasu Dev7f349142009-03-27 09:06:31 -0700876 goto out_lp_destroy;
877 }
878
Chris Leech9a057532009-11-03 11:46:40 -0800879 if (!npiv) {
880 /*
881 * fcoe_em_alloc() and fcoe_hostlist_add() both
882 * need to be atomic with respect to other changes to the
883 * hostlist since fcoe_em_alloc() looks for an existing EM
884 * instance on host list updated by fcoe_hostlist_add().
885 *
886 * This is currently handled through the fcoe_config_mutex
887 * begin held.
888 */
Chris Leechc863df32009-08-25 14:00:18 -0700889
Chris Leech9a057532009-11-03 11:46:40 -0800890 /* lport exch manager allocation */
891 rc = fcoe_em_config(lport);
892 if (rc) {
893 FCOE_NETDEV_DBG(netdev, "Could not configure the EM "
894 "for the interface\n");
895 goto out_lp_destroy;
896 }
Vasu Dev96316092009-07-29 17:05:00 -0700897 }
898
Chris Leech030f4e02009-08-25 14:00:02 -0700899 fcoe_interface_get(fcoe);
Chris Leechaf7f85d2009-08-25 13:59:24 -0700900 return lport;
Vasu Dev7f349142009-03-27 09:06:31 -0700901
902out_lp_destroy:
Chris Leechaf7f85d2009-08-25 13:59:24 -0700903 fc_exch_mgr_free(lport);
Vasu Dev7f349142009-03-27 09:06:31 -0700904out_host_put:
Chris Leechaf7f85d2009-08-25 13:59:24 -0700905 scsi_host_put(lport->host);
906out:
907 return ERR_PTR(rc);
Vasu Dev7f349142009-03-27 09:06:31 -0700908}
909
910/**
911 * fcoe_if_init() - attach to scsi transport
912 *
913 * Returns : 0 on success
914 */
915static int __init fcoe_if_init(void)
916{
917 /* attach to scsi transport */
Chris Leeche9084bb2009-11-03 11:46:34 -0800918 fcoe_transport_template = fc_attach_transport(&fcoe_transport_function);
919 fcoe_vport_transport_template =
920 fc_attach_transport(&fcoe_vport_transport_function);
Vasu Dev7f349142009-03-27 09:06:31 -0700921
Chris Leeche9084bb2009-11-03 11:46:34 -0800922 if (!fcoe_transport_template) {
Robert Loved5488eb2009-06-10 15:30:59 -0700923 printk(KERN_ERR "fcoe: Failed to attach to the FC transport\n");
Vasu Dev7f349142009-03-27 09:06:31 -0700924 return -ENODEV;
925 }
926
927 return 0;
928}
929
930/**
931 * fcoe_if_exit() - detach from scsi transport
932 *
933 * Returns : 0 on success
934 */
935int __exit fcoe_if_exit(void)
936{
Chris Leeche9084bb2009-11-03 11:46:34 -0800937 fc_release_transport(fcoe_transport_template);
938 fc_release_transport(fcoe_vport_transport_template);
939 fcoe_transport_template = NULL;
940 fcoe_vport_transport_template = NULL;
Vasu Dev7f349142009-03-27 09:06:31 -0700941 return 0;
942}
943
Robert Love85b4aa42008-12-09 15:10:24 -0800944/**
Robert Love8976f422009-03-17 11:41:46 -0700945 * fcoe_percpu_thread_create() - Create a receive thread for an online cpu
946 * @cpu: cpu index for the online cpu
947 */
948static void fcoe_percpu_thread_create(unsigned int cpu)
949{
950 struct fcoe_percpu_s *p;
951 struct task_struct *thread;
952
953 p = &per_cpu(fcoe_percpu, cpu);
954
955 thread = kthread_create(fcoe_percpu_receive_thread,
956 (void *)p, "fcoethread/%d", cpu);
957
Joe Eykholte7a51992009-08-25 14:04:08 -0700958 if (likely(!IS_ERR(thread))) {
Robert Love8976f422009-03-17 11:41:46 -0700959 kthread_bind(thread, cpu);
960 wake_up_process(thread);
961
962 spin_lock_bh(&p->fcoe_rx_list.lock);
963 p->thread = thread;
964 spin_unlock_bh(&p->fcoe_rx_list.lock);
965 }
966}
967
968/**
969 * fcoe_percpu_thread_destroy() - removes the rx thread for the given cpu
970 * @cpu: cpu index the rx thread is to be removed
971 *
972 * Destroys a per-CPU Rx thread. Any pending skbs are moved to the
973 * current CPU's Rx thread. If the thread being destroyed is bound to
974 * the CPU processing this context the skbs will be freed.
975 */
976static void fcoe_percpu_thread_destroy(unsigned int cpu)
977{
978 struct fcoe_percpu_s *p;
979 struct task_struct *thread;
980 struct page *crc_eof;
981 struct sk_buff *skb;
982#ifdef CONFIG_SMP
983 struct fcoe_percpu_s *p0;
984 unsigned targ_cpu = smp_processor_id();
985#endif /* CONFIG_SMP */
986
Robert Loved5488eb2009-06-10 15:30:59 -0700987 FCOE_DBG("Destroying receive thread for CPU %d\n", cpu);
Robert Love8976f422009-03-17 11:41:46 -0700988
989 /* Prevent any new skbs from being queued for this CPU. */
990 p = &per_cpu(fcoe_percpu, cpu);
991 spin_lock_bh(&p->fcoe_rx_list.lock);
992 thread = p->thread;
993 p->thread = NULL;
994 crc_eof = p->crc_eof_page;
995 p->crc_eof_page = NULL;
996 p->crc_eof_offset = 0;
997 spin_unlock_bh(&p->fcoe_rx_list.lock);
998
999#ifdef CONFIG_SMP
1000 /*
1001 * Don't bother moving the skb's if this context is running
1002 * on the same CPU that is having its thread destroyed. This
1003 * can easily happen when the module is removed.
1004 */
1005 if (cpu != targ_cpu) {
1006 p0 = &per_cpu(fcoe_percpu, targ_cpu);
1007 spin_lock_bh(&p0->fcoe_rx_list.lock);
1008 if (p0->thread) {
Robert Loved5488eb2009-06-10 15:30:59 -07001009 FCOE_DBG("Moving frames from CPU %d to CPU %d\n",
1010 cpu, targ_cpu);
Robert Love8976f422009-03-17 11:41:46 -07001011
1012 while ((skb = __skb_dequeue(&p->fcoe_rx_list)) != NULL)
1013 __skb_queue_tail(&p0->fcoe_rx_list, skb);
1014 spin_unlock_bh(&p0->fcoe_rx_list.lock);
1015 } else {
1016 /*
1017 * The targeted CPU is not initialized and cannot accept
1018 * new skbs. Unlock the targeted CPU and drop the skbs
1019 * on the CPU that is going offline.
1020 */
1021 while ((skb = __skb_dequeue(&p->fcoe_rx_list)) != NULL)
1022 kfree_skb(skb);
1023 spin_unlock_bh(&p0->fcoe_rx_list.lock);
1024 }
1025 } else {
1026 /*
1027 * This scenario occurs when the module is being removed
1028 * and all threads are being destroyed. skbs will continue
1029 * to be shifted from the CPU thread that is being removed
1030 * to the CPU thread associated with the CPU that is processing
1031 * the module removal. Once there is only one CPU Rx thread it
1032 * will reach this case and we will drop all skbs and later
1033 * stop the thread.
1034 */
1035 spin_lock_bh(&p->fcoe_rx_list.lock);
1036 while ((skb = __skb_dequeue(&p->fcoe_rx_list)) != NULL)
1037 kfree_skb(skb);
1038 spin_unlock_bh(&p->fcoe_rx_list.lock);
1039 }
1040#else
1041 /*
Chris Leechdd3fd722009-04-21 16:27:36 -07001042 * This a non-SMP scenario where the singular Rx thread is
Robert Love8976f422009-03-17 11:41:46 -07001043 * being removed. Free all skbs and stop the thread.
1044 */
1045 spin_lock_bh(&p->fcoe_rx_list.lock);
1046 while ((skb = __skb_dequeue(&p->fcoe_rx_list)) != NULL)
1047 kfree_skb(skb);
1048 spin_unlock_bh(&p->fcoe_rx_list.lock);
1049#endif
1050
1051 if (thread)
1052 kthread_stop(thread);
1053
1054 if (crc_eof)
1055 put_page(crc_eof);
1056}
1057
1058/**
1059 * fcoe_cpu_callback() - fcoe cpu hotplug event callback
1060 * @nfb: callback data block
1061 * @action: event triggering the callback
1062 * @hcpu: index for the cpu of this event
1063 *
1064 * This creates or destroys per cpu data for fcoe
1065 *
1066 * Returns NOTIFY_OK always.
1067 */
1068static int fcoe_cpu_callback(struct notifier_block *nfb,
1069 unsigned long action, void *hcpu)
1070{
1071 unsigned cpu = (unsigned long)hcpu;
1072
1073 switch (action) {
1074 case CPU_ONLINE:
1075 case CPU_ONLINE_FROZEN:
Robert Loved5488eb2009-06-10 15:30:59 -07001076 FCOE_DBG("CPU %x online: Create Rx thread\n", cpu);
Robert Love8976f422009-03-17 11:41:46 -07001077 fcoe_percpu_thread_create(cpu);
1078 break;
1079 case CPU_DEAD:
1080 case CPU_DEAD_FROZEN:
Robert Loved5488eb2009-06-10 15:30:59 -07001081 FCOE_DBG("CPU %x offline: Remove Rx thread\n", cpu);
Robert Love8976f422009-03-17 11:41:46 -07001082 fcoe_percpu_thread_destroy(cpu);
1083 break;
1084 default:
1085 break;
1086 }
1087 return NOTIFY_OK;
1088}
1089
1090static struct notifier_block fcoe_cpu_notifier = {
1091 .notifier_call = fcoe_cpu_callback,
1092};
1093
1094/**
Robert Love34f42a02009-02-27 10:55:45 -08001095 * fcoe_rcv() - this is the fcoe receive function called by NET_RX_SOFTIRQ
Robert Love85b4aa42008-12-09 15:10:24 -08001096 * @skb: the receive skb
1097 * @dev: associated net device
1098 * @ptype: context
Chris Leechdd3fd722009-04-21 16:27:36 -07001099 * @olddev: last device
Robert Love85b4aa42008-12-09 15:10:24 -08001100 *
1101 * this function will receive the packet and build fc frame and pass it up
1102 *
1103 * Returns: 0 for success
Robert Love34f42a02009-02-27 10:55:45 -08001104 */
Robert Love85b4aa42008-12-09 15:10:24 -08001105int fcoe_rcv(struct sk_buff *skb, struct net_device *dev,
1106 struct packet_type *ptype, struct net_device *olddev)
1107{
1108 struct fc_lport *lp;
1109 struct fcoe_rcv_info *fr;
Chris Leech259ad852009-08-25 13:59:41 -07001110 struct fcoe_interface *fcoe;
Robert Love85b4aa42008-12-09 15:10:24 -08001111 struct fc_frame_header *fh;
Robert Love85b4aa42008-12-09 15:10:24 -08001112 struct fcoe_percpu_s *fps;
Vasu Devb2f00912009-08-25 13:58:53 -07001113 unsigned int cpu;
Robert Love85b4aa42008-12-09 15:10:24 -08001114
Chris Leech259ad852009-08-25 13:59:41 -07001115 fcoe = container_of(ptype, struct fcoe_interface, fcoe_packet_type);
Chris Leech3fe9a0b2009-08-25 13:59:46 -07001116 lp = fcoe->ctlr.lp;
Robert Love85b4aa42008-12-09 15:10:24 -08001117 if (unlikely(lp == NULL)) {
Robert Loved5488eb2009-06-10 15:30:59 -07001118 FCOE_NETDEV_DBG(dev, "Cannot find hba structure");
Robert Love85b4aa42008-12-09 15:10:24 -08001119 goto err2;
1120 }
Joe Eykholt97c83892009-03-17 11:42:40 -07001121 if (!lp->link_up)
1122 goto err2;
Robert Love85b4aa42008-12-09 15:10:24 -08001123
Robert Loved5488eb2009-06-10 15:30:59 -07001124 FCOE_NETDEV_DBG(dev, "skb_info: len:%d data_len:%d head:%p "
1125 "data:%p tail:%p end:%p sum:%d dev:%s",
1126 skb->len, skb->data_len, skb->head, skb->data,
1127 skb_tail_pointer(skb), skb_end_pointer(skb),
1128 skb->csum, skb->dev ? skb->dev->name : "<NULL>");
Robert Love85b4aa42008-12-09 15:10:24 -08001129
1130 /* check for FCOE packet type */
1131 if (unlikely(eth_hdr(skb)->h_proto != htons(ETH_P_FCOE))) {
Robert Loved5488eb2009-06-10 15:30:59 -07001132 FCOE_NETDEV_DBG(dev, "Wrong FC type frame");
Robert Love85b4aa42008-12-09 15:10:24 -08001133 goto err;
1134 }
1135
1136 /*
1137 * Check for minimum frame length, and make sure required FCoE
1138 * and FC headers are pulled into the linear data area.
1139 */
1140 if (unlikely((skb->len < FCOE_MIN_FRAME) ||
1141 !pskb_may_pull(skb, FCOE_HEADER_LEN)))
1142 goto err;
1143
1144 skb_set_transport_header(skb, sizeof(struct fcoe_hdr));
1145 fh = (struct fc_frame_header *) skb_transport_header(skb);
1146
Robert Love85b4aa42008-12-09 15:10:24 -08001147 fr = fcoe_dev_from_skb(skb);
1148 fr->fr_dev = lp;
1149 fr->ptype = ptype;
Robert Love5e5e92d2009-03-17 11:41:35 -07001150
Robert Love85b4aa42008-12-09 15:10:24 -08001151 /*
Vasu Devb2f00912009-08-25 13:58:53 -07001152 * In case the incoming frame's exchange is originated from
1153 * the initiator, then received frame's exchange id is ANDed
1154 * with fc_cpu_mask bits to get the same cpu on which exchange
1155 * was originated, otherwise just use the current cpu.
Robert Love85b4aa42008-12-09 15:10:24 -08001156 */
Vasu Devb2f00912009-08-25 13:58:53 -07001157 if (ntoh24(fh->fh_f_ctl) & FC_FC_EX_CTX)
1158 cpu = ntohs(fh->fh_ox_id) & fc_cpu_mask;
1159 else
1160 cpu = smp_processor_id();
Robert Love5e5e92d2009-03-17 11:41:35 -07001161
Robert Love8976f422009-03-17 11:41:46 -07001162 fps = &per_cpu(fcoe_percpu, cpu);
Robert Love85b4aa42008-12-09 15:10:24 -08001163 spin_lock_bh(&fps->fcoe_rx_list.lock);
Robert Love8976f422009-03-17 11:41:46 -07001164 if (unlikely(!fps->thread)) {
1165 /*
1166 * The targeted CPU is not ready, let's target
1167 * the first CPU now. For non-SMP systems this
1168 * will check the same CPU twice.
1169 */
Robert Loved5488eb2009-06-10 15:30:59 -07001170 FCOE_NETDEV_DBG(dev, "CPU is online, but no receive thread "
1171 "ready for incoming skb- using first online "
1172 "CPU.\n");
Robert Love8976f422009-03-17 11:41:46 -07001173
1174 spin_unlock_bh(&fps->fcoe_rx_list.lock);
1175 cpu = first_cpu(cpu_online_map);
1176 fps = &per_cpu(fcoe_percpu, cpu);
1177 spin_lock_bh(&fps->fcoe_rx_list.lock);
1178 if (!fps->thread) {
1179 spin_unlock_bh(&fps->fcoe_rx_list.lock);
1180 goto err;
1181 }
1182 }
1183
1184 /*
1185 * We now have a valid CPU that we're targeting for
1186 * this skb. We also have this receive thread locked,
1187 * so we're free to queue skbs into it's queue.
1188 */
Robert Love85b4aa42008-12-09 15:10:24 -08001189 __skb_queue_tail(&fps->fcoe_rx_list, skb);
1190 if (fps->fcoe_rx_list.qlen == 1)
1191 wake_up_process(fps->thread);
1192
1193 spin_unlock_bh(&fps->fcoe_rx_list.lock);
1194
1195 return 0;
1196err:
Robert Love582b45b2009-03-31 15:51:50 -07001197 fc_lport_get_stats(lp)->ErrorFrames++;
Robert Love85b4aa42008-12-09 15:10:24 -08001198
1199err2:
1200 kfree_skb(skb);
1201 return -1;
1202}
Robert Love85b4aa42008-12-09 15:10:24 -08001203
1204/**
Robert Love34f42a02009-02-27 10:55:45 -08001205 * fcoe_start_io() - pass to netdev to start xmit for fcoe
Robert Love85b4aa42008-12-09 15:10:24 -08001206 * @skb: the skb to be xmitted
1207 *
1208 * Returns: 0 for success
Robert Love34f42a02009-02-27 10:55:45 -08001209 */
Robert Love85b4aa42008-12-09 15:10:24 -08001210static inline int fcoe_start_io(struct sk_buff *skb)
1211{
1212 int rc;
1213
1214 skb_get(skb);
1215 rc = dev_queue_xmit(skb);
1216 if (rc != 0)
1217 return rc;
1218 kfree_skb(skb);
1219 return 0;
1220}
1221
1222/**
Chris Leechdd3fd722009-04-21 16:27:36 -07001223 * fcoe_get_paged_crc_eof() - in case we need to alloc a page for crc_eof
Robert Love85b4aa42008-12-09 15:10:24 -08001224 * @skb: the skb to be xmitted
1225 * @tlen: total len
1226 *
1227 * Returns: 0 for success
Robert Love34f42a02009-02-27 10:55:45 -08001228 */
Robert Love85b4aa42008-12-09 15:10:24 -08001229static int fcoe_get_paged_crc_eof(struct sk_buff *skb, int tlen)
1230{
1231 struct fcoe_percpu_s *fps;
1232 struct page *page;
Robert Love85b4aa42008-12-09 15:10:24 -08001233
Robert Love5e5e92d2009-03-17 11:41:35 -07001234 fps = &get_cpu_var(fcoe_percpu);
Robert Love85b4aa42008-12-09 15:10:24 -08001235 page = fps->crc_eof_page;
1236 if (!page) {
1237 page = alloc_page(GFP_ATOMIC);
1238 if (!page) {
Robert Love5e5e92d2009-03-17 11:41:35 -07001239 put_cpu_var(fcoe_percpu);
Robert Love85b4aa42008-12-09 15:10:24 -08001240 return -ENOMEM;
1241 }
1242 fps->crc_eof_page = page;
Robert Love8976f422009-03-17 11:41:46 -07001243 fps->crc_eof_offset = 0;
Robert Love85b4aa42008-12-09 15:10:24 -08001244 }
1245
1246 get_page(page);
1247 skb_fill_page_desc(skb, skb_shinfo(skb)->nr_frags, page,
1248 fps->crc_eof_offset, tlen);
1249 skb->len += tlen;
1250 skb->data_len += tlen;
1251 skb->truesize += tlen;
1252 fps->crc_eof_offset += sizeof(struct fcoe_crc_eof);
1253
1254 if (fps->crc_eof_offset >= PAGE_SIZE) {
1255 fps->crc_eof_page = NULL;
1256 fps->crc_eof_offset = 0;
1257 put_page(page);
1258 }
Robert Love5e5e92d2009-03-17 11:41:35 -07001259 put_cpu_var(fcoe_percpu);
Robert Love85b4aa42008-12-09 15:10:24 -08001260 return 0;
1261}
1262
1263/**
Robert Love34f42a02009-02-27 10:55:45 -08001264 * fcoe_fc_crc() - calculates FC CRC in this fcoe skb
Chris Leechdd3fd722009-04-21 16:27:36 -07001265 * @fp: the fc_frame containing data to be checksummed
Robert Love85b4aa42008-12-09 15:10:24 -08001266 *
Chris Leech014f5c32009-08-25 13:59:30 -07001267 * This uses crc32() to calculate the crc for port frame
Robert Love85b4aa42008-12-09 15:10:24 -08001268 * Return : 32 bit crc
Robert Love34f42a02009-02-27 10:55:45 -08001269 */
Robert Love85b4aa42008-12-09 15:10:24 -08001270u32 fcoe_fc_crc(struct fc_frame *fp)
1271{
1272 struct sk_buff *skb = fp_skb(fp);
1273 struct skb_frag_struct *frag;
1274 unsigned char *data;
1275 unsigned long off, len, clen;
1276 u32 crc;
1277 unsigned i;
1278
1279 crc = crc32(~0, skb->data, skb_headlen(skb));
1280
1281 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
1282 frag = &skb_shinfo(skb)->frags[i];
1283 off = frag->page_offset;
1284 len = frag->size;
1285 while (len > 0) {
1286 clen = min(len, PAGE_SIZE - (off & ~PAGE_MASK));
1287 data = kmap_atomic(frag->page + (off >> PAGE_SHIFT),
1288 KM_SKB_DATA_SOFTIRQ);
1289 crc = crc32(crc, data + (off & ~PAGE_MASK), clen);
1290 kunmap_atomic(data, KM_SKB_DATA_SOFTIRQ);
1291 off += clen;
1292 len -= clen;
1293 }
1294 }
1295 return crc;
1296}
Robert Love85b4aa42008-12-09 15:10:24 -08001297
1298/**
Robert Love34f42a02009-02-27 10:55:45 -08001299 * fcoe_xmit() - FCoE frame transmit function
Chris Leech014f5c32009-08-25 13:59:30 -07001300 * @lp: the associated local fcoe
Robert Love85b4aa42008-12-09 15:10:24 -08001301 * @fp: the fc_frame to be transmitted
1302 *
1303 * Return : 0 for success
Robert Love34f42a02009-02-27 10:55:45 -08001304 */
Robert Love85b4aa42008-12-09 15:10:24 -08001305int fcoe_xmit(struct fc_lport *lp, struct fc_frame *fp)
1306{
Vasu Dev4bb6b512009-05-06 10:52:34 -07001307 int wlen;
Robert Love85b4aa42008-12-09 15:10:24 -08001308 u32 crc;
1309 struct ethhdr *eh;
1310 struct fcoe_crc_eof *cp;
1311 struct sk_buff *skb;
1312 struct fcoe_dev_stats *stats;
1313 struct fc_frame_header *fh;
1314 unsigned int hlen; /* header length implies the version */
1315 unsigned int tlen; /* trailer length */
1316 unsigned int elen; /* eth header, may include vlan */
Chris Leech3fe9a0b2009-08-25 13:59:46 -07001317 struct fcoe_port *port = lport_priv(lp);
1318 struct fcoe_interface *fcoe = port->fcoe;
Robert Love85b4aa42008-12-09 15:10:24 -08001319 u8 sof, eof;
1320 struct fcoe_hdr *hp;
1321
1322 WARN_ON((fr_len(fp) % sizeof(u32)) != 0);
1323
Robert Love85b4aa42008-12-09 15:10:24 -08001324 fh = fc_frame_header_get(fp);
Joe Eykholt97c83892009-03-17 11:42:40 -07001325 skb = fp_skb(fp);
1326 wlen = skb->len / FCOE_WORD_TO_BYTE;
1327
1328 if (!lp->link_up) {
Dan Carpenter3caf02e2009-04-21 16:27:25 -07001329 kfree_skb(skb);
Joe Eykholt97c83892009-03-17 11:42:40 -07001330 return 0;
Robert Love85b4aa42008-12-09 15:10:24 -08001331 }
1332
Joe Eykholt97c83892009-03-17 11:42:40 -07001333 if (unlikely(fh->fh_r_ctl == FC_RCTL_ELS_REQ) &&
Chris Leech11b56182009-11-03 11:46:29 -08001334 fcoe_ctlr_els_send(&fcoe->ctlr, lp, skb))
Joe Eykholt97c83892009-03-17 11:42:40 -07001335 return 0;
1336
Robert Love85b4aa42008-12-09 15:10:24 -08001337 sof = fr_sof(fp);
1338 eof = fr_eof(fp);
1339
Vasu Dev4e57e1c2009-05-06 10:52:46 -07001340 elen = sizeof(struct ethhdr);
Robert Love85b4aa42008-12-09 15:10:24 -08001341 hlen = sizeof(struct fcoe_hdr);
1342 tlen = sizeof(struct fcoe_crc_eof);
1343 wlen = (skb->len - tlen + sizeof(crc)) / FCOE_WORD_TO_BYTE;
1344
1345 /* crc offload */
1346 if (likely(lp->crc_offload)) {
Yi Zou39ca9a02009-02-27 14:07:15 -08001347 skb->ip_summed = CHECKSUM_PARTIAL;
Robert Love85b4aa42008-12-09 15:10:24 -08001348 skb->csum_start = skb_headroom(skb);
1349 skb->csum_offset = skb->len;
1350 crc = 0;
1351 } else {
1352 skb->ip_summed = CHECKSUM_NONE;
1353 crc = fcoe_fc_crc(fp);
1354 }
1355
Chris Leech014f5c32009-08-25 13:59:30 -07001356 /* copy port crc and eof to the skb buff */
Robert Love85b4aa42008-12-09 15:10:24 -08001357 if (skb_is_nonlinear(skb)) {
1358 skb_frag_t *frag;
1359 if (fcoe_get_paged_crc_eof(skb, tlen)) {
Roel Kluine9041582009-02-27 10:56:22 -08001360 kfree_skb(skb);
Robert Love85b4aa42008-12-09 15:10:24 -08001361 return -ENOMEM;
1362 }
1363 frag = &skb_shinfo(skb)->frags[skb_shinfo(skb)->nr_frags - 1];
1364 cp = kmap_atomic(frag->page, KM_SKB_DATA_SOFTIRQ)
1365 + frag->page_offset;
1366 } else {
1367 cp = (struct fcoe_crc_eof *)skb_put(skb, tlen);
1368 }
1369
1370 memset(cp, 0, sizeof(*cp));
1371 cp->fcoe_eof = eof;
1372 cp->fcoe_crc32 = cpu_to_le32(~crc);
1373
1374 if (skb_is_nonlinear(skb)) {
1375 kunmap_atomic(cp, KM_SKB_DATA_SOFTIRQ);
1376 cp = NULL;
1377 }
1378
Chris Leech014f5c32009-08-25 13:59:30 -07001379 /* adjust skb network/transport offsets to match mac/fcoe/port */
Robert Love85b4aa42008-12-09 15:10:24 -08001380 skb_push(skb, elen + hlen);
1381 skb_reset_mac_header(skb);
1382 skb_reset_network_header(skb);
1383 skb->mac_len = elen;
Yi Zou211c7382009-02-27 14:06:37 -08001384 skb->protocol = htons(ETH_P_FCOE);
Chris Leech3fe9a0b2009-08-25 13:59:46 -07001385 skb->dev = fcoe->netdev;
Robert Love85b4aa42008-12-09 15:10:24 -08001386
1387 /* fill up mac and fcoe headers */
1388 eh = eth_hdr(skb);
1389 eh->h_proto = htons(ETH_P_FCOE);
Chris Leech3fe9a0b2009-08-25 13:59:46 -07001390 if (fcoe->ctlr.map_dest)
Robert Love85b4aa42008-12-09 15:10:24 -08001391 fc_fcoe_set_mac(eh->h_dest, fh->fh_d_id);
1392 else
1393 /* insert GW address */
Chris Leech3fe9a0b2009-08-25 13:59:46 -07001394 memcpy(eh->h_dest, fcoe->ctlr.dest_addr, ETH_ALEN);
Robert Love85b4aa42008-12-09 15:10:24 -08001395
Chris Leech3fe9a0b2009-08-25 13:59:46 -07001396 if (unlikely(fcoe->ctlr.flogi_oxid != FC_XID_UNKNOWN))
1397 memcpy(eh->h_source, fcoe->ctlr.ctl_src_addr, ETH_ALEN);
Robert Love85b4aa42008-12-09 15:10:24 -08001398 else
Chris Leech11b56182009-11-03 11:46:29 -08001399 memcpy(eh->h_source, port->data_src_addr, ETH_ALEN);
Robert Love85b4aa42008-12-09 15:10:24 -08001400
1401 hp = (struct fcoe_hdr *)(eh + 1);
1402 memset(hp, 0, sizeof(*hp));
1403 if (FC_FCOE_VER)
1404 FC_FCOE_ENCAPS_VER(hp, FC_FCOE_VER);
1405 hp->fcoe_sof = sof;
1406
Yi Zou39ca9a02009-02-27 14:07:15 -08001407 /* fcoe lso, mss is in max_payload which is non-zero for FCP data */
1408 if (lp->seq_offload && fr_max_payload(fp)) {
1409 skb_shinfo(skb)->gso_type = SKB_GSO_FCOE;
1410 skb_shinfo(skb)->gso_size = fr_max_payload(fp);
1411 } else {
1412 skb_shinfo(skb)->gso_type = 0;
1413 skb_shinfo(skb)->gso_size = 0;
1414 }
Robert Love85b4aa42008-12-09 15:10:24 -08001415 /* update tx stats: regardless if LLD fails */
Robert Love582b45b2009-03-31 15:51:50 -07001416 stats = fc_lport_get_stats(lp);
1417 stats->TxFrames++;
1418 stats->TxWords += wlen;
Robert Love85b4aa42008-12-09 15:10:24 -08001419
1420 /* send down to lld */
1421 fr_dev(fp) = lp;
Chris Leech014f5c32009-08-25 13:59:30 -07001422 if (port->fcoe_pending_queue.qlen)
Vasu Dev4bb6b512009-05-06 10:52:34 -07001423 fcoe_check_wait_queue(lp, skb);
1424 else if (fcoe_start_io(skb))
1425 fcoe_check_wait_queue(lp, skb);
Robert Love85b4aa42008-12-09 15:10:24 -08001426
1427 return 0;
1428}
Robert Love85b4aa42008-12-09 15:10:24 -08001429
Robert Love34f42a02009-02-27 10:55:45 -08001430/**
Joe Eykholte7a51992009-08-25 14:04:08 -07001431 * fcoe_percpu_flush_done() - Indicate percpu queue flush completion.
1432 * @skb: the skb being completed.
1433 */
1434static void fcoe_percpu_flush_done(struct sk_buff *skb)
1435{
1436 complete(&fcoe_flush_completion);
1437}
1438
1439/**
Robert Love34f42a02009-02-27 10:55:45 -08001440 * fcoe_percpu_receive_thread() - recv thread per cpu
Robert Love85b4aa42008-12-09 15:10:24 -08001441 * @arg: ptr to the fcoe per cpu struct
1442 *
1443 * Return: 0 for success
Robert Love85b4aa42008-12-09 15:10:24 -08001444 */
1445int fcoe_percpu_receive_thread(void *arg)
1446{
1447 struct fcoe_percpu_s *p = arg;
1448 u32 fr_len;
1449 struct fc_lport *lp;
1450 struct fcoe_rcv_info *fr;
1451 struct fcoe_dev_stats *stats;
1452 struct fc_frame_header *fh;
1453 struct sk_buff *skb;
1454 struct fcoe_crc_eof crc_eof;
1455 struct fc_frame *fp;
1456 u8 *mac = NULL;
Chris Leech014f5c32009-08-25 13:59:30 -07001457 struct fcoe_port *port;
Robert Love85b4aa42008-12-09 15:10:24 -08001458 struct fcoe_hdr *hp;
1459
Robert Love4469c192009-02-27 10:56:38 -08001460 set_user_nice(current, -20);
Robert Love85b4aa42008-12-09 15:10:24 -08001461
1462 while (!kthread_should_stop()) {
1463
1464 spin_lock_bh(&p->fcoe_rx_list.lock);
1465 while ((skb = __skb_dequeue(&p->fcoe_rx_list)) == NULL) {
1466 set_current_state(TASK_INTERRUPTIBLE);
1467 spin_unlock_bh(&p->fcoe_rx_list.lock);
1468 schedule();
1469 set_current_state(TASK_RUNNING);
1470 if (kthread_should_stop())
1471 return 0;
1472 spin_lock_bh(&p->fcoe_rx_list.lock);
1473 }
1474 spin_unlock_bh(&p->fcoe_rx_list.lock);
1475 fr = fcoe_dev_from_skb(skb);
1476 lp = fr->fr_dev;
1477 if (unlikely(lp == NULL)) {
Joe Eykholte7a51992009-08-25 14:04:08 -07001478 if (skb->destructor != fcoe_percpu_flush_done)
1479 FCOE_NETDEV_DBG(skb->dev, "NULL lport in skb");
Robert Love85b4aa42008-12-09 15:10:24 -08001480 kfree_skb(skb);
1481 continue;
1482 }
1483
Robert Loved5488eb2009-06-10 15:30:59 -07001484 FCOE_NETDEV_DBG(skb->dev, "skb_info: len:%d data_len:%d "
1485 "head:%p data:%p tail:%p end:%p sum:%d dev:%s",
1486 skb->len, skb->data_len,
1487 skb->head, skb->data, skb_tail_pointer(skb),
1488 skb_end_pointer(skb), skb->csum,
1489 skb->dev ? skb->dev->name : "<NULL>");
Robert Love85b4aa42008-12-09 15:10:24 -08001490
1491 /*
1492 * Save source MAC address before discarding header.
1493 */
Chris Leech014f5c32009-08-25 13:59:30 -07001494 port = lport_priv(lp);
Robert Love85b4aa42008-12-09 15:10:24 -08001495 if (skb_is_nonlinear(skb))
1496 skb_linearize(skb); /* not ideal */
Joe Eykholt97c83892009-03-17 11:42:40 -07001497 mac = eth_hdr(skb)->h_source;
Robert Love85b4aa42008-12-09 15:10:24 -08001498
1499 /*
1500 * Frame length checks and setting up the header pointers
1501 * was done in fcoe_rcv already.
1502 */
1503 hp = (struct fcoe_hdr *) skb_network_header(skb);
1504 fh = (struct fc_frame_header *) skb_transport_header(skb);
1505
Robert Love582b45b2009-03-31 15:51:50 -07001506 stats = fc_lport_get_stats(lp);
Robert Love85b4aa42008-12-09 15:10:24 -08001507 if (unlikely(FC_FCOE_DECAPS_VER(hp) != FC_FCOE_VER)) {
Robert Love582b45b2009-03-31 15:51:50 -07001508 if (stats->ErrorFrames < 5)
Robert Loved5488eb2009-06-10 15:30:59 -07001509 printk(KERN_WARNING "fcoe: FCoE version "
Robert Love582b45b2009-03-31 15:51:50 -07001510 "mismatch: The frame has "
1511 "version %x, but the "
1512 "initiator supports version "
1513 "%x\n", FC_FCOE_DECAPS_VER(hp),
1514 FC_FCOE_VER);
1515 stats->ErrorFrames++;
Robert Love85b4aa42008-12-09 15:10:24 -08001516 kfree_skb(skb);
1517 continue;
1518 }
1519
1520 skb_pull(skb, sizeof(struct fcoe_hdr));
1521 fr_len = skb->len - sizeof(struct fcoe_crc_eof);
1522
Robert Love582b45b2009-03-31 15:51:50 -07001523 stats->RxFrames++;
1524 stats->RxWords += fr_len / FCOE_WORD_TO_BYTE;
Robert Love85b4aa42008-12-09 15:10:24 -08001525
1526 fp = (struct fc_frame *)skb;
1527 fc_frame_init(fp);
1528 fr_dev(fp) = lp;
1529 fr_sof(fp) = hp->fcoe_sof;
1530
1531 /* Copy out the CRC and EOF trailer for access */
1532 if (skb_copy_bits(skb, fr_len, &crc_eof, sizeof(crc_eof))) {
1533 kfree_skb(skb);
1534 continue;
1535 }
1536 fr_eof(fp) = crc_eof.fcoe_eof;
1537 fr_crc(fp) = crc_eof.fcoe_crc32;
1538 if (pskb_trim(skb, fr_len)) {
1539 kfree_skb(skb);
1540 continue;
1541 }
1542
1543 /*
1544 * We only check CRC if no offload is available and if it is
1545 * it's solicited data, in which case, the FCP layer would
1546 * check it during the copy.
1547 */
Yi Zou07c00ec2009-02-27 14:07:31 -08001548 if (lp->crc_offload && skb->ip_summed == CHECKSUM_UNNECESSARY)
Robert Love85b4aa42008-12-09 15:10:24 -08001549 fr_flags(fp) &= ~FCPHF_CRC_UNCHECKED;
1550 else
1551 fr_flags(fp) |= FCPHF_CRC_UNCHECKED;
1552
1553 fh = fc_frame_header_get(fp);
1554 if (fh->fh_r_ctl == FC_RCTL_DD_SOL_DATA &&
1555 fh->fh_type == FC_TYPE_FCP) {
Vasu Dev52ff8782009-07-29 17:05:10 -07001556 fc_exch_recv(lp, fp);
Robert Love85b4aa42008-12-09 15:10:24 -08001557 continue;
1558 }
1559 if (fr_flags(fp) & FCPHF_CRC_UNCHECKED) {
1560 if (le32_to_cpu(fr_crc(fp)) !=
1561 ~crc32(~0, skb->data, fr_len)) {
Robert Loved5488eb2009-06-10 15:30:59 -07001562 if (stats->InvalidCRCCount < 5)
Robert Love85b4aa42008-12-09 15:10:24 -08001563 printk(KERN_WARNING "fcoe: dropping "
1564 "frame with CRC error\n");
1565 stats->InvalidCRCCount++;
1566 stats->ErrorFrames++;
1567 fc_frame_free(fp);
1568 continue;
1569 }
1570 fr_flags(fp) &= ~FCPHF_CRC_UNCHECKED;
1571 }
Vasu Dev52ff8782009-07-29 17:05:10 -07001572 fc_exch_recv(lp, fp);
Robert Love85b4aa42008-12-09 15:10:24 -08001573 }
1574 return 0;
1575}
1576
1577/**
Chris Leechdd3fd722009-04-21 16:27:36 -07001578 * fcoe_check_wait_queue() - attempt to clear the transmit backlog
1579 * @lp: the fc_lport
Robert Love85b4aa42008-12-09 15:10:24 -08001580 *
1581 * This empties the wait_queue, dequeue the head of the wait_queue queue
1582 * and calls fcoe_start_io() for each packet, if all skb have been
Vasu Devc826a312009-02-27 10:56:27 -08001583 * transmitted, return qlen or -1 if a error occurs, then restore
Chris Leechdd3fd722009-04-21 16:27:36 -07001584 * wait_queue and try again later.
Robert Love85b4aa42008-12-09 15:10:24 -08001585 *
1586 * The wait_queue is used when the skb transmit fails. skb will go
Chris Leechdd3fd722009-04-21 16:27:36 -07001587 * in the wait_queue which will be emptied by the timer function or
Robert Love85b4aa42008-12-09 15:10:24 -08001588 * by the next skb transmit.
Robert Love34f42a02009-02-27 10:55:45 -08001589 */
Vasu Dev4bb6b512009-05-06 10:52:34 -07001590static void fcoe_check_wait_queue(struct fc_lport *lp, struct sk_buff *skb)
Robert Love85b4aa42008-12-09 15:10:24 -08001591{
Chris Leech014f5c32009-08-25 13:59:30 -07001592 struct fcoe_port *port = lport_priv(lp);
Vasu Dev4bb6b512009-05-06 10:52:34 -07001593 int rc;
Robert Love85b4aa42008-12-09 15:10:24 -08001594
Chris Leech014f5c32009-08-25 13:59:30 -07001595 spin_lock_bh(&port->fcoe_pending_queue.lock);
Vasu Dev4bb6b512009-05-06 10:52:34 -07001596
1597 if (skb)
Chris Leech014f5c32009-08-25 13:59:30 -07001598 __skb_queue_tail(&port->fcoe_pending_queue, skb);
Vasu Dev4bb6b512009-05-06 10:52:34 -07001599
Chris Leech014f5c32009-08-25 13:59:30 -07001600 if (port->fcoe_pending_queue_active)
Vasu Devc826a312009-02-27 10:56:27 -08001601 goto out;
Chris Leech014f5c32009-08-25 13:59:30 -07001602 port->fcoe_pending_queue_active = 1;
Chris Leech55c8baf2009-02-27 10:56:32 -08001603
Chris Leech014f5c32009-08-25 13:59:30 -07001604 while (port->fcoe_pending_queue.qlen) {
Chris Leech55c8baf2009-02-27 10:56:32 -08001605 /* keep qlen > 0 until fcoe_start_io succeeds */
Chris Leech014f5c32009-08-25 13:59:30 -07001606 port->fcoe_pending_queue.qlen++;
1607 skb = __skb_dequeue(&port->fcoe_pending_queue);
Chris Leech55c8baf2009-02-27 10:56:32 -08001608
Chris Leech014f5c32009-08-25 13:59:30 -07001609 spin_unlock_bh(&port->fcoe_pending_queue.lock);
Chris Leech55c8baf2009-02-27 10:56:32 -08001610 rc = fcoe_start_io(skb);
Chris Leech014f5c32009-08-25 13:59:30 -07001611 spin_lock_bh(&port->fcoe_pending_queue.lock);
Chris Leech55c8baf2009-02-27 10:56:32 -08001612
1613 if (rc) {
Chris Leech014f5c32009-08-25 13:59:30 -07001614 __skb_queue_head(&port->fcoe_pending_queue, skb);
Chris Leech55c8baf2009-02-27 10:56:32 -08001615 /* undo temporary increment above */
Chris Leech014f5c32009-08-25 13:59:30 -07001616 port->fcoe_pending_queue.qlen--;
Chris Leech55c8baf2009-02-27 10:56:32 -08001617 break;
Robert Love85b4aa42008-12-09 15:10:24 -08001618 }
Chris Leech55c8baf2009-02-27 10:56:32 -08001619 /* undo temporary increment above */
Chris Leech014f5c32009-08-25 13:59:30 -07001620 port->fcoe_pending_queue.qlen--;
Robert Love85b4aa42008-12-09 15:10:24 -08001621 }
Chris Leech55c8baf2009-02-27 10:56:32 -08001622
Chris Leech014f5c32009-08-25 13:59:30 -07001623 if (port->fcoe_pending_queue.qlen < FCOE_LOW_QUEUE_DEPTH)
Chris Leech55c8baf2009-02-27 10:56:32 -08001624 lp->qfull = 0;
Chris Leech014f5c32009-08-25 13:59:30 -07001625 if (port->fcoe_pending_queue.qlen && !timer_pending(&port->timer))
1626 mod_timer(&port->timer, jiffies + 2);
1627 port->fcoe_pending_queue_active = 0;
Vasu Devc826a312009-02-27 10:56:27 -08001628out:
Chris Leech014f5c32009-08-25 13:59:30 -07001629 if (port->fcoe_pending_queue.qlen > FCOE_MAX_QUEUE_DEPTH)
Vasu Dev4bb6b512009-05-06 10:52:34 -07001630 lp->qfull = 1;
Chris Leech014f5c32009-08-25 13:59:30 -07001631 spin_unlock_bh(&port->fcoe_pending_queue.lock);
Vasu Dev4bb6b512009-05-06 10:52:34 -07001632 return;
Robert Love85b4aa42008-12-09 15:10:24 -08001633}
1634
1635/**
Robert Love34f42a02009-02-27 10:55:45 -08001636 * fcoe_dev_setup() - setup link change notification interface
1637 */
Randy Dunlapb0d428a2009-04-27 21:49:31 -07001638static void fcoe_dev_setup(void)
Robert Love85b4aa42008-12-09 15:10:24 -08001639{
Robert Love85b4aa42008-12-09 15:10:24 -08001640 register_netdevice_notifier(&fcoe_notifier);
1641}
1642
1643/**
Randy Dunlapb0d428a2009-04-27 21:49:31 -07001644 * fcoe_dev_cleanup() - cleanup link change notification interface
Robert Love34f42a02009-02-27 10:55:45 -08001645 */
Robert Love85b4aa42008-12-09 15:10:24 -08001646static void fcoe_dev_cleanup(void)
1647{
1648 unregister_netdevice_notifier(&fcoe_notifier);
1649}
1650
1651/**
Robert Love34f42a02009-02-27 10:55:45 -08001652 * fcoe_device_notification() - netdev event notification callback
Robert Love85b4aa42008-12-09 15:10:24 -08001653 * @notifier: context of the notification
1654 * @event: type of event
1655 * @ptr: fixed array for output parsed ifname
1656 *
1657 * This function is called by the ethernet driver in case of link change event
1658 *
1659 * Returns: 0 for success
Robert Love34f42a02009-02-27 10:55:45 -08001660 */
Robert Love85b4aa42008-12-09 15:10:24 -08001661static int fcoe_device_notification(struct notifier_block *notifier,
1662 ulong event, void *ptr)
1663{
1664 struct fc_lport *lp = NULL;
Vasu Dev1d1b88d2009-07-29 17:05:45 -07001665 struct net_device *netdev = ptr;
Chris Leech014f5c32009-08-25 13:59:30 -07001666 struct fcoe_interface *fcoe;
Chris Leech2e70e242009-08-25 14:00:23 -07001667 struct fcoe_port *port;
Robert Love85b4aa42008-12-09 15:10:24 -08001668 struct fcoe_dev_stats *stats;
Joe Eykholt97c83892009-03-17 11:42:40 -07001669 u32 link_possible = 1;
Robert Love85b4aa42008-12-09 15:10:24 -08001670 u32 mfs;
1671 int rc = NOTIFY_OK;
1672
Chris Leech014f5c32009-08-25 13:59:30 -07001673 list_for_each_entry(fcoe, &fcoe_hostlist, list) {
Chris Leech25024982009-08-25 13:59:35 -07001674 if (fcoe->netdev == netdev) {
Chris Leech3fe9a0b2009-08-25 13:59:46 -07001675 lp = fcoe->ctlr.lp;
Robert Love85b4aa42008-12-09 15:10:24 -08001676 break;
1677 }
1678 }
Robert Love85b4aa42008-12-09 15:10:24 -08001679 if (lp == NULL) {
1680 rc = NOTIFY_DONE;
1681 goto out;
1682 }
1683
Robert Love85b4aa42008-12-09 15:10:24 -08001684 switch (event) {
1685 case NETDEV_DOWN:
1686 case NETDEV_GOING_DOWN:
Joe Eykholt97c83892009-03-17 11:42:40 -07001687 link_possible = 0;
Robert Love85b4aa42008-12-09 15:10:24 -08001688 break;
1689 case NETDEV_UP:
1690 case NETDEV_CHANGE:
Robert Love85b4aa42008-12-09 15:10:24 -08001691 break;
1692 case NETDEV_CHANGEMTU:
Yi Zou7221d7e2009-10-21 16:27:52 -07001693 if (netdev->features & NETIF_F_FCOE_MTU)
1694 break;
Vasu Dev1d1b88d2009-07-29 17:05:45 -07001695 mfs = netdev->mtu - (sizeof(struct fcoe_hdr) +
1696 sizeof(struct fcoe_crc_eof));
Robert Love85b4aa42008-12-09 15:10:24 -08001697 if (mfs >= FC_MIN_MAX_FRAME)
1698 fc_set_mfs(lp, mfs);
Robert Love85b4aa42008-12-09 15:10:24 -08001699 break;
1700 case NETDEV_REGISTER:
1701 break;
Chris Leech2e70e242009-08-25 14:00:23 -07001702 case NETDEV_UNREGISTER:
1703 list_del(&fcoe->list);
1704 port = lport_priv(fcoe->ctlr.lp);
1705 fcoe_interface_cleanup(fcoe);
1706 schedule_work(&port->destroy_work);
1707 goto out;
1708 break;
Robert Love85b4aa42008-12-09 15:10:24 -08001709 default:
Vasu Dev1d1b88d2009-07-29 17:05:45 -07001710 FCOE_NETDEV_DBG(netdev, "Unknown event %ld "
Robert Loved5488eb2009-06-10 15:30:59 -07001711 "from netdev netlink\n", event);
Robert Love85b4aa42008-12-09 15:10:24 -08001712 }
Joe Eykholt97c83892009-03-17 11:42:40 -07001713 if (link_possible && !fcoe_link_ok(lp))
Chris Leech3fe9a0b2009-08-25 13:59:46 -07001714 fcoe_ctlr_link_up(&fcoe->ctlr);
1715 else if (fcoe_ctlr_link_down(&fcoe->ctlr)) {
Joe Eykholt97c83892009-03-17 11:42:40 -07001716 stats = fc_lport_get_stats(lp);
1717 stats->LinkFailureCount++;
1718 fcoe_clean_pending_queue(lp);
Robert Love85b4aa42008-12-09 15:10:24 -08001719 }
1720out:
1721 return rc;
1722}
1723
1724/**
Robert Love34f42a02009-02-27 10:55:45 -08001725 * fcoe_if_to_netdev() - parse a name buffer to get netdev
Robert Love85b4aa42008-12-09 15:10:24 -08001726 * @buffer: incoming buffer to be copied
1727 *
Chris Leechdd3fd722009-04-21 16:27:36 -07001728 * Returns: NULL or ptr to net_device
Robert Love34f42a02009-02-27 10:55:45 -08001729 */
Robert Love85b4aa42008-12-09 15:10:24 -08001730static struct net_device *fcoe_if_to_netdev(const char *buffer)
1731{
1732 char *cp;
1733 char ifname[IFNAMSIZ + 2];
1734
1735 if (buffer) {
1736 strlcpy(ifname, buffer, IFNAMSIZ);
1737 cp = ifname + strlen(ifname);
1738 while (--cp >= ifname && *cp == '\n')
1739 *cp = '\0';
1740 return dev_get_by_name(&init_net, ifname);
1741 }
1742 return NULL;
1743}
1744
1745/**
Robert Love34f42a02009-02-27 10:55:45 -08001746 * fcoe_destroy() - handles the destroy from sysfs
Chris Leechdd3fd722009-04-21 16:27:36 -07001747 * @buffer: expected to be an eth if name
Robert Love85b4aa42008-12-09 15:10:24 -08001748 * @kp: associated kernel param
1749 *
1750 * Returns: 0 for success
Robert Love34f42a02009-02-27 10:55:45 -08001751 */
Robert Love85b4aa42008-12-09 15:10:24 -08001752static int fcoe_destroy(const char *buffer, struct kernel_param *kp)
1753{
Chris Leech030f4e02009-08-25 14:00:02 -07001754 struct fcoe_interface *fcoe;
Chris Leech2e70e242009-08-25 14:00:23 -07001755 struct net_device *netdev;
Mike Christie8eca3552009-10-21 16:27:44 -07001756 int rc = 0;
Robert Love85b4aa42008-12-09 15:10:24 -08001757
Chris Leechdfc1d0f2009-08-25 14:00:13 -07001758 mutex_lock(&fcoe_config_mutex);
1759#ifdef CONFIG_FCOE_MODULE
1760 /*
1761 * Make sure the module has been initialized, and is not about to be
1762 * removed. Module paramter sysfs files are writable before the
1763 * module_init function is called and after module_exit.
1764 */
1765 if (THIS_MODULE->state != MODULE_STATE_LIVE) {
1766 rc = -ENODEV;
1767 goto out_nodev;
1768 }
1769#endif
1770
Robert Love85b4aa42008-12-09 15:10:24 -08001771 netdev = fcoe_if_to_netdev(buffer);
1772 if (!netdev) {
1773 rc = -ENODEV;
1774 goto out_nodev;
1775 }
Chris Leech2e70e242009-08-25 14:00:23 -07001776
Chris Leech090eb6c2009-08-25 14:00:28 -07001777 rtnl_lock();
Chris Leech2e70e242009-08-25 14:00:23 -07001778 fcoe = fcoe_hostlist_lookup_port(netdev);
1779 if (!fcoe) {
Chris Leech090eb6c2009-08-25 14:00:28 -07001780 rtnl_unlock();
Robert Love85b4aa42008-12-09 15:10:24 -08001781 rc = -ENODEV;
1782 goto out_putdev;
1783 }
Chris Leech2e70e242009-08-25 14:00:23 -07001784 list_del(&fcoe->list);
Chris Leech2e70e242009-08-25 14:00:23 -07001785 fcoe_interface_cleanup(fcoe);
1786 rtnl_unlock();
1787 fcoe_if_destroy(fcoe->ctlr.lp);
Robert Love85b4aa42008-12-09 15:10:24 -08001788out_putdev:
1789 dev_put(netdev);
1790out_nodev:
Chris Leechdfc1d0f2009-08-25 14:00:13 -07001791 mutex_unlock(&fcoe_config_mutex);
Robert Love85b4aa42008-12-09 15:10:24 -08001792 return rc;
1793}
1794
Chris Leech2e70e242009-08-25 14:00:23 -07001795static void fcoe_destroy_work(struct work_struct *work)
1796{
1797 struct fcoe_port *port;
1798
1799 port = container_of(work, struct fcoe_port, destroy_work);
1800 mutex_lock(&fcoe_config_mutex);
1801 fcoe_if_destroy(port->lport);
1802 mutex_unlock(&fcoe_config_mutex);
1803}
1804
Robert Love85b4aa42008-12-09 15:10:24 -08001805/**
Robert Love34f42a02009-02-27 10:55:45 -08001806 * fcoe_create() - Handles the create call from sysfs
Chris Leechdd3fd722009-04-21 16:27:36 -07001807 * @buffer: expected to be an eth if name
Robert Love85b4aa42008-12-09 15:10:24 -08001808 * @kp: associated kernel param
1809 *
1810 * Returns: 0 for success
Robert Love34f42a02009-02-27 10:55:45 -08001811 */
Robert Love85b4aa42008-12-09 15:10:24 -08001812static int fcoe_create(const char *buffer, struct kernel_param *kp)
1813{
1814 int rc;
Chris Leech030f4e02009-08-25 14:00:02 -07001815 struct fcoe_interface *fcoe;
Chris Leechaf7f85d2009-08-25 13:59:24 -07001816 struct fc_lport *lport;
Robert Love85b4aa42008-12-09 15:10:24 -08001817 struct net_device *netdev;
1818
Chris Leechdfc1d0f2009-08-25 14:00:13 -07001819 mutex_lock(&fcoe_config_mutex);
1820#ifdef CONFIG_FCOE_MODULE
1821 /*
1822 * Make sure the module has been initialized, and is not about to be
1823 * removed. Module paramter sysfs files are writable before the
1824 * module_init function is called and after module_exit.
1825 */
1826 if (THIS_MODULE->state != MODULE_STATE_LIVE) {
1827 rc = -ENODEV;
1828 goto out_nodev;
1829 }
1830#endif
1831
Chris Leech2e70e242009-08-25 14:00:23 -07001832 rtnl_lock();
Robert Love85b4aa42008-12-09 15:10:24 -08001833 netdev = fcoe_if_to_netdev(buffer);
1834 if (!netdev) {
1835 rc = -ENODEV;
1836 goto out_nodev;
1837 }
Chris Leech2e70e242009-08-25 14:00:23 -07001838
Robert Love85b4aa42008-12-09 15:10:24 -08001839 /* look for existing lport */
1840 if (fcoe_hostlist_lookup(netdev)) {
1841 rc = -EEXIST;
1842 goto out_putdev;
1843 }
Robert Love85b4aa42008-12-09 15:10:24 -08001844
Chris Leech030f4e02009-08-25 14:00:02 -07001845 fcoe = fcoe_interface_create(netdev);
1846 if (!fcoe) {
1847 rc = -ENOMEM;
1848 goto out_putdev;
1849 }
1850
Chris Leech9a057532009-11-03 11:46:40 -08001851 lport = fcoe_if_create(fcoe, &netdev->dev, 0);
Chris Leechaf7f85d2009-08-25 13:59:24 -07001852 if (IS_ERR(lport)) {
Robert Loved5488eb2009-06-10 15:30:59 -07001853 printk(KERN_ERR "fcoe: Failed to create interface (%s)\n",
Robert Love85b4aa42008-12-09 15:10:24 -08001854 netdev->name);
Robert Love85b4aa42008-12-09 15:10:24 -08001855 rc = -EIO;
Chris Leech2e70e242009-08-25 14:00:23 -07001856 fcoe_interface_cleanup(fcoe);
Chris Leech030f4e02009-08-25 14:00:02 -07001857 goto out_free;
Robert Love85b4aa42008-12-09 15:10:24 -08001858 }
Chris Leech030f4e02009-08-25 14:00:02 -07001859
Chris Leech54b649f2009-08-25 14:00:07 -07001860 /* Make this the "master" N_Port */
1861 fcoe->ctlr.lp = lport;
Chris Leech030f4e02009-08-25 14:00:02 -07001862
Chris Leechc863df32009-08-25 14:00:18 -07001863 /* add to lports list */
1864 fcoe_hostlist_add(lport);
1865
Chris Leech54b649f2009-08-25 14:00:07 -07001866 /* start FIP Discovery and FLOGI */
1867 lport->boot_time = jiffies;
1868 fc_fabric_login(lport);
1869 if (!fcoe_link_ok(lport))
1870 fcoe_ctlr_link_up(&fcoe->ctlr);
1871
1872 rc = 0;
Chris Leech030f4e02009-08-25 14:00:02 -07001873out_free:
Chris Leech54b649f2009-08-25 14:00:07 -07001874 /*
1875 * Release from init in fcoe_interface_create(), on success lport
1876 * should be holding a reference taken in fcoe_if_create().
1877 */
Chris Leech030f4e02009-08-25 14:00:02 -07001878 fcoe_interface_put(fcoe);
Robert Love85b4aa42008-12-09 15:10:24 -08001879out_putdev:
1880 dev_put(netdev);
1881out_nodev:
Chris Leech2e70e242009-08-25 14:00:23 -07001882 rtnl_unlock();
Chris Leechdfc1d0f2009-08-25 14:00:13 -07001883 mutex_unlock(&fcoe_config_mutex);
Robert Love85b4aa42008-12-09 15:10:24 -08001884 return rc;
1885}
1886
1887module_param_call(create, fcoe_create, NULL, NULL, S_IWUSR);
1888__MODULE_PARM_TYPE(create, "string");
Chris Leech014f5c32009-08-25 13:59:30 -07001889MODULE_PARM_DESC(create, "Create fcoe fcoe using net device passed in.");
Robert Love85b4aa42008-12-09 15:10:24 -08001890module_param_call(destroy, fcoe_destroy, NULL, NULL, S_IWUSR);
1891__MODULE_PARM_TYPE(destroy, "string");
Chris Leech014f5c32009-08-25 13:59:30 -07001892MODULE_PARM_DESC(destroy, "Destroy fcoe fcoe");
Robert Love85b4aa42008-12-09 15:10:24 -08001893
Robert Love34f42a02009-02-27 10:55:45 -08001894/**
1895 * fcoe_link_ok() - Check if link is ok for the fc_lport
Robert Love85b4aa42008-12-09 15:10:24 -08001896 * @lp: ptr to the fc_lport
1897 *
1898 * Any permanently-disqualifying conditions have been previously checked.
1899 * This also updates the speed setting, which may change with link for 100/1000.
1900 *
1901 * This function should probably be checking for PAUSE support at some point
1902 * in the future. Currently Per-priority-pause is not determinable using
1903 * ethtool, so we shouldn't be restrictive until that problem is resolved.
1904 *
1905 * Returns: 0 if link is OK for use by FCoE.
1906 *
1907 */
1908int fcoe_link_ok(struct fc_lport *lp)
1909{
Chris Leech014f5c32009-08-25 13:59:30 -07001910 struct fcoe_port *port = lport_priv(lp);
Chris Leech25024982009-08-25 13:59:35 -07001911 struct net_device *dev = port->fcoe->netdev;
Robert Love85b4aa42008-12-09 15:10:24 -08001912 struct ethtool_cmd ecmd = { ETHTOOL_GSET };
Robert Love85b4aa42008-12-09 15:10:24 -08001913
Yi Zou2f718d62009-07-29 17:04:01 -07001914 if ((dev->flags & IFF_UP) && netif_carrier_ok(dev) &&
1915 (!dev_ethtool_get_settings(dev, &ecmd))) {
1916 lp->link_supported_speeds &=
1917 ~(FC_PORTSPEED_1GBIT | FC_PORTSPEED_10GBIT);
1918 if (ecmd.supported & (SUPPORTED_1000baseT_Half |
1919 SUPPORTED_1000baseT_Full))
1920 lp->link_supported_speeds |= FC_PORTSPEED_1GBIT;
1921 if (ecmd.supported & SUPPORTED_10000baseT_Full)
1922 lp->link_supported_speeds |=
1923 FC_PORTSPEED_10GBIT;
1924 if (ecmd.speed == SPEED_1000)
1925 lp->link_speed = FC_PORTSPEED_1GBIT;
1926 if (ecmd.speed == SPEED_10000)
1927 lp->link_speed = FC_PORTSPEED_10GBIT;
Robert Love85b4aa42008-12-09 15:10:24 -08001928
Yi Zou2f718d62009-07-29 17:04:01 -07001929 return 0;
1930 }
1931 return -1;
Robert Love85b4aa42008-12-09 15:10:24 -08001932}
Robert Love85b4aa42008-12-09 15:10:24 -08001933
Robert Love34f42a02009-02-27 10:55:45 -08001934/**
1935 * fcoe_percpu_clean() - Clear the pending skbs for an lport
Robert Love85b4aa42008-12-09 15:10:24 -08001936 * @lp: the fc_lport
Joe Eykholte7a51992009-08-25 14:04:08 -07001937 *
1938 * Must be called with fcoe_create_mutex held to single-thread completion.
1939 *
1940 * This flushes the pending skbs by adding a new skb to each queue and
1941 * waiting until they are all freed. This assures us that not only are
1942 * there no packets that will be handled by the lport, but also that any
1943 * threads already handling packet have returned.
Robert Love85b4aa42008-12-09 15:10:24 -08001944 */
1945void fcoe_percpu_clean(struct fc_lport *lp)
1946{
Robert Love85b4aa42008-12-09 15:10:24 -08001947 struct fcoe_percpu_s *pp;
1948 struct fcoe_rcv_info *fr;
1949 struct sk_buff_head *list;
1950 struct sk_buff *skb, *next;
1951 struct sk_buff *head;
Robert Love5e5e92d2009-03-17 11:41:35 -07001952 unsigned int cpu;
Robert Love85b4aa42008-12-09 15:10:24 -08001953
Robert Love5e5e92d2009-03-17 11:41:35 -07001954 for_each_possible_cpu(cpu) {
1955 pp = &per_cpu(fcoe_percpu, cpu);
1956 spin_lock_bh(&pp->fcoe_rx_list.lock);
1957 list = &pp->fcoe_rx_list;
1958 head = list->next;
1959 for (skb = head; skb != (struct sk_buff *)list;
1960 skb = next) {
1961 next = skb->next;
1962 fr = fcoe_dev_from_skb(skb);
1963 if (fr->fr_dev == lp) {
1964 __skb_unlink(skb, list);
1965 kfree_skb(skb);
Robert Love85b4aa42008-12-09 15:10:24 -08001966 }
Robert Love85b4aa42008-12-09 15:10:24 -08001967 }
Joe Eykholte7a51992009-08-25 14:04:08 -07001968
1969 if (!pp->thread || !cpu_online(cpu)) {
1970 spin_unlock_bh(&pp->fcoe_rx_list.lock);
1971 continue;
1972 }
1973
1974 skb = dev_alloc_skb(0);
1975 if (!skb) {
1976 spin_unlock_bh(&pp->fcoe_rx_list.lock);
1977 continue;
1978 }
1979 skb->destructor = fcoe_percpu_flush_done;
1980
1981 __skb_queue_tail(&pp->fcoe_rx_list, skb);
1982 if (pp->fcoe_rx_list.qlen == 1)
1983 wake_up_process(pp->thread);
Robert Love5e5e92d2009-03-17 11:41:35 -07001984 spin_unlock_bh(&pp->fcoe_rx_list.lock);
Joe Eykholte7a51992009-08-25 14:04:08 -07001985
1986 wait_for_completion(&fcoe_flush_completion);
Robert Love85b4aa42008-12-09 15:10:24 -08001987 }
1988}
Robert Love85b4aa42008-12-09 15:10:24 -08001989
1990/**
Robert Love34f42a02009-02-27 10:55:45 -08001991 * fcoe_clean_pending_queue() - Dequeue a skb and free it
Robert Love85b4aa42008-12-09 15:10:24 -08001992 * @lp: the corresponding fc_lport
1993 *
1994 * Returns: none
Robert Love34f42a02009-02-27 10:55:45 -08001995 */
Robert Love85b4aa42008-12-09 15:10:24 -08001996void fcoe_clean_pending_queue(struct fc_lport *lp)
1997{
Chris Leech014f5c32009-08-25 13:59:30 -07001998 struct fcoe_port *port = lport_priv(lp);
Robert Love85b4aa42008-12-09 15:10:24 -08001999 struct sk_buff *skb;
2000
Chris Leech014f5c32009-08-25 13:59:30 -07002001 spin_lock_bh(&port->fcoe_pending_queue.lock);
2002 while ((skb = __skb_dequeue(&port->fcoe_pending_queue)) != NULL) {
2003 spin_unlock_bh(&port->fcoe_pending_queue.lock);
Robert Love85b4aa42008-12-09 15:10:24 -08002004 kfree_skb(skb);
Chris Leech014f5c32009-08-25 13:59:30 -07002005 spin_lock_bh(&port->fcoe_pending_queue.lock);
Robert Love85b4aa42008-12-09 15:10:24 -08002006 }
Chris Leech014f5c32009-08-25 13:59:30 -07002007 spin_unlock_bh(&port->fcoe_pending_queue.lock);
Robert Love85b4aa42008-12-09 15:10:24 -08002008}
Robert Love85b4aa42008-12-09 15:10:24 -08002009
2010/**
Robert Love34f42a02009-02-27 10:55:45 -08002011 * fcoe_reset() - Resets the fcoe
Robert Love85b4aa42008-12-09 15:10:24 -08002012 * @shost: shost the reset is from
2013 *
2014 * Returns: always 0
2015 */
2016int fcoe_reset(struct Scsi_Host *shost)
2017{
2018 struct fc_lport *lport = shost_priv(shost);
2019 fc_lport_reset(lport);
2020 return 0;
2021}
Robert Love85b4aa42008-12-09 15:10:24 -08002022
Robert Love34f42a02009-02-27 10:55:45 -08002023/**
Chris Leech014f5c32009-08-25 13:59:30 -07002024 * fcoe_hostlist_lookup_port() - find the corresponding lport by a given device
Chris Leechdd3fd722009-04-21 16:27:36 -07002025 * @dev: this is currently ptr to net_device
Robert Love85b4aa42008-12-09 15:10:24 -08002026 *
Chris Leech014f5c32009-08-25 13:59:30 -07002027 * Returns: NULL or the located fcoe_port
Chris Leech090eb6c2009-08-25 14:00:28 -07002028 * Locking: must be called with the RNL mutex held
Robert Love85b4aa42008-12-09 15:10:24 -08002029 */
Chris Leech014f5c32009-08-25 13:59:30 -07002030static struct fcoe_interface *
2031fcoe_hostlist_lookup_port(const struct net_device *dev)
Robert Love85b4aa42008-12-09 15:10:24 -08002032{
Chris Leech014f5c32009-08-25 13:59:30 -07002033 struct fcoe_interface *fcoe;
Robert Love85b4aa42008-12-09 15:10:24 -08002034
Chris Leech014f5c32009-08-25 13:59:30 -07002035 list_for_each_entry(fcoe, &fcoe_hostlist, list) {
Chris Leech25024982009-08-25 13:59:35 -07002036 if (fcoe->netdev == dev)
Chris Leech014f5c32009-08-25 13:59:30 -07002037 return fcoe;
Robert Love85b4aa42008-12-09 15:10:24 -08002038 }
Robert Love85b4aa42008-12-09 15:10:24 -08002039 return NULL;
2040}
2041
Robert Love34f42a02009-02-27 10:55:45 -08002042/**
2043 * fcoe_hostlist_lookup() - Find the corresponding lport by netdev
Robert Love85b4aa42008-12-09 15:10:24 -08002044 * @netdev: ptr to net_device
2045 *
2046 * Returns: 0 for success
Chris Leech090eb6c2009-08-25 14:00:28 -07002047 * Locking: must be called with the RTNL mutex held
Robert Love85b4aa42008-12-09 15:10:24 -08002048 */
Chris Leech090eb6c2009-08-25 14:00:28 -07002049static struct fc_lport *fcoe_hostlist_lookup(const struct net_device *netdev)
Robert Love85b4aa42008-12-09 15:10:24 -08002050{
Chris Leech014f5c32009-08-25 13:59:30 -07002051 struct fcoe_interface *fcoe;
Robert Love85b4aa42008-12-09 15:10:24 -08002052
Chris Leech014f5c32009-08-25 13:59:30 -07002053 fcoe = fcoe_hostlist_lookup_port(netdev);
Chris Leech3fe9a0b2009-08-25 13:59:46 -07002054 return (fcoe) ? fcoe->ctlr.lp : NULL;
Robert Love85b4aa42008-12-09 15:10:24 -08002055}
Robert Love85b4aa42008-12-09 15:10:24 -08002056
Robert Love34f42a02009-02-27 10:55:45 -08002057/**
2058 * fcoe_hostlist_add() - Add a lport to lports list
Chris Leechdd3fd722009-04-21 16:27:36 -07002059 * @lp: ptr to the fc_lport to be added
Robert Love85b4aa42008-12-09 15:10:24 -08002060 *
2061 * Returns: 0 for success
Chris Leech090eb6c2009-08-25 14:00:28 -07002062 * Locking: must be called with the RTNL mutex held
Robert Love85b4aa42008-12-09 15:10:24 -08002063 */
Chris Leech090eb6c2009-08-25 14:00:28 -07002064static int fcoe_hostlist_add(const struct fc_lport *lport)
Robert Love85b4aa42008-12-09 15:10:24 -08002065{
Chris Leech014f5c32009-08-25 13:59:30 -07002066 struct fcoe_interface *fcoe;
2067 struct fcoe_port *port;
Robert Love85b4aa42008-12-09 15:10:24 -08002068
Chris Leech014f5c32009-08-25 13:59:30 -07002069 fcoe = fcoe_hostlist_lookup_port(fcoe_netdev(lport));
2070 if (!fcoe) {
2071 port = lport_priv(lport);
2072 fcoe = port->fcoe;
2073 list_add_tail(&fcoe->list, &fcoe_hostlist);
Robert Love85b4aa42008-12-09 15:10:24 -08002074 }
2075 return 0;
2076}
Robert Love85b4aa42008-12-09 15:10:24 -08002077
Robert Love34f42a02009-02-27 10:55:45 -08002078/**
Robert Love34f42a02009-02-27 10:55:45 -08002079 * fcoe_init() - fcoe module loading initialization
Robert Love85b4aa42008-12-09 15:10:24 -08002080 *
Robert Love85b4aa42008-12-09 15:10:24 -08002081 * Returns 0 on success, negative on failure
Robert Love34f42a02009-02-27 10:55:45 -08002082 */
Robert Love85b4aa42008-12-09 15:10:24 -08002083static int __init fcoe_init(void)
2084{
Robert Love38eccab2009-03-17 11:41:30 -07002085 unsigned int cpu;
Robert Love8976f422009-03-17 11:41:46 -07002086 int rc = 0;
Robert Love85b4aa42008-12-09 15:10:24 -08002087 struct fcoe_percpu_s *p;
2088
Chris Leechdfc1d0f2009-08-25 14:00:13 -07002089 mutex_lock(&fcoe_config_mutex);
2090
Robert Love38eccab2009-03-17 11:41:30 -07002091 for_each_possible_cpu(cpu) {
Robert Love5e5e92d2009-03-17 11:41:35 -07002092 p = &per_cpu(fcoe_percpu, cpu);
Robert Love38eccab2009-03-17 11:41:30 -07002093 skb_queue_head_init(&p->fcoe_rx_list);
2094 }
2095
Robert Love8976f422009-03-17 11:41:46 -07002096 for_each_online_cpu(cpu)
2097 fcoe_percpu_thread_create(cpu);
Robert Love85b4aa42008-12-09 15:10:24 -08002098
Robert Love8976f422009-03-17 11:41:46 -07002099 /* Initialize per CPU interrupt thread */
2100 rc = register_hotcpu_notifier(&fcoe_cpu_notifier);
2101 if (rc)
2102 goto out_free;
Robert Love85b4aa42008-12-09 15:10:24 -08002103
Robert Love8976f422009-03-17 11:41:46 -07002104 /* Setup link change notification */
Robert Love85b4aa42008-12-09 15:10:24 -08002105 fcoe_dev_setup();
2106
Chris Leech5892c322009-08-25 13:59:14 -07002107 rc = fcoe_if_init();
2108 if (rc)
2109 goto out_free;
Robert Love85b4aa42008-12-09 15:10:24 -08002110
Chris Leechdfc1d0f2009-08-25 14:00:13 -07002111 mutex_unlock(&fcoe_config_mutex);
Robert Love85b4aa42008-12-09 15:10:24 -08002112 return 0;
Robert Love8976f422009-03-17 11:41:46 -07002113
2114out_free:
2115 for_each_online_cpu(cpu) {
2116 fcoe_percpu_thread_destroy(cpu);
2117 }
Chris Leechdfc1d0f2009-08-25 14:00:13 -07002118 mutex_unlock(&fcoe_config_mutex);
Robert Love8976f422009-03-17 11:41:46 -07002119 return rc;
Robert Love85b4aa42008-12-09 15:10:24 -08002120}
2121module_init(fcoe_init);
2122
2123/**
Robert Love34f42a02009-02-27 10:55:45 -08002124 * fcoe_exit() - fcoe module unloading cleanup
Robert Love85b4aa42008-12-09 15:10:24 -08002125 *
2126 * Returns 0 on success, negative on failure
Robert Love34f42a02009-02-27 10:55:45 -08002127 */
Robert Love85b4aa42008-12-09 15:10:24 -08002128static void __exit fcoe_exit(void)
2129{
Robert Love5e5e92d2009-03-17 11:41:35 -07002130 unsigned int cpu;
Chris Leech014f5c32009-08-25 13:59:30 -07002131 struct fcoe_interface *fcoe, *tmp;
Chris Leech2e70e242009-08-25 14:00:23 -07002132 struct fcoe_port *port;
Robert Love85b4aa42008-12-09 15:10:24 -08002133
Chris Leechdfc1d0f2009-08-25 14:00:13 -07002134 mutex_lock(&fcoe_config_mutex);
2135
Robert Love85b4aa42008-12-09 15:10:24 -08002136 fcoe_dev_cleanup();
2137
Vasu Dev5919a592009-03-27 09:03:29 -07002138 /* releases the associated fcoe hosts */
Chris Leech090eb6c2009-08-25 14:00:28 -07002139 rtnl_lock();
2140 list_for_each_entry_safe(fcoe, tmp, &fcoe_hostlist, list) {
Chris Leechc863df32009-08-25 14:00:18 -07002141 list_del(&fcoe->list);
Chris Leech2e70e242009-08-25 14:00:23 -07002142 port = lport_priv(fcoe->ctlr.lp);
Chris Leech2e70e242009-08-25 14:00:23 -07002143 fcoe_interface_cleanup(fcoe);
Chris Leech2e70e242009-08-25 14:00:23 -07002144 schedule_work(&port->destroy_work);
Chris Leechc863df32009-08-25 14:00:18 -07002145 }
Chris Leech090eb6c2009-08-25 14:00:28 -07002146 rtnl_unlock();
Robert Love85b4aa42008-12-09 15:10:24 -08002147
Robert Love8976f422009-03-17 11:41:46 -07002148 unregister_hotcpu_notifier(&fcoe_cpu_notifier);
2149
Chris Leech014f5c32009-08-25 13:59:30 -07002150 for_each_online_cpu(cpu)
Robert Love8976f422009-03-17 11:41:46 -07002151 fcoe_percpu_thread_destroy(cpu);
Robert Love85b4aa42008-12-09 15:10:24 -08002152
Chris Leechdfc1d0f2009-08-25 14:00:13 -07002153 mutex_unlock(&fcoe_config_mutex);
Chris Leech2e70e242009-08-25 14:00:23 -07002154
2155 /* flush any asyncronous interface destroys,
2156 * this should happen after the netdev notifier is unregistered */
2157 flush_scheduled_work();
Chris Leech9a057532009-11-03 11:46:40 -08002158 /* That will flush out all the N_Ports on the hostlist, but now we
2159 * may have NPIV VN_Ports scheduled for destruction */
2160 flush_scheduled_work();
Chris Leech2e70e242009-08-25 14:00:23 -07002161
2162 /* detach from scsi transport
2163 * must happen after all destroys are done, therefor after the flush */
2164 fcoe_if_exit();
Robert Love85b4aa42008-12-09 15:10:24 -08002165}
2166module_exit(fcoe_exit);
Chris Leech11b56182009-11-03 11:46:29 -08002167
2168/**
2169 * fcoe_flogi_resp() - FCoE specific FLOGI and FDISC response handler
2170 * @seq: active sequence in the FLOGI or FDISC exchange
2171 * @fp: response frame, or error encoded in a pointer (timeout)
2172 * @arg: pointer the the fcoe_ctlr structure
2173 *
2174 * This handles MAC address managment for FCoE, then passes control on to
2175 * the libfc FLOGI response handler.
2176 */
2177static void fcoe_flogi_resp(struct fc_seq *seq, struct fc_frame *fp, void *arg)
2178{
2179 struct fcoe_ctlr *fip = arg;
2180 struct fc_exch *exch = fc_seq_exch(seq);
2181 struct fc_lport *lport = exch->lp;
2182 u8 *mac;
2183
2184 if (IS_ERR(fp))
2185 goto done;
2186
2187 mac = fr_cb(fp)->granted_mac;
2188 if (is_zero_ether_addr(mac)) {
2189 /* pre-FIP */
2190 mac = eth_hdr(&fp->skb)->h_source;
2191 if (fcoe_ctlr_recv_flogi(fip, lport, fp, mac)) {
2192 fc_frame_free(fp);
2193 return;
2194 }
2195 } else {
2196 /* FIP, libfcoe has already seen it */
2197 fip->update_mac(lport, fr_cb(fp)->granted_mac);
2198 }
2199done:
2200 fc_lport_flogi_resp(seq, fp, lport);
2201}
2202
2203/**
2204 * fcoe_logo_resp() - FCoE specific LOGO response handler
2205 * @seq: active sequence in the LOGO exchange
2206 * @fp: response frame, or error encoded in a pointer (timeout)
2207 * @arg: pointer the the fcoe_ctlr structure
2208 *
2209 * This handles MAC address managment for FCoE, then passes control on to
2210 * the libfc LOGO response handler.
2211 */
2212static void fcoe_logo_resp(struct fc_seq *seq, struct fc_frame *fp, void *arg)
2213{
2214 struct fcoe_ctlr *fip = arg;
2215 struct fc_exch *exch = fc_seq_exch(seq);
2216 struct fc_lport *lport = exch->lp;
2217 static u8 zero_mac[ETH_ALEN] = { 0 };
2218
2219 if (!IS_ERR(fp))
2220 fip->update_mac(lport, zero_mac);
2221 fc_lport_logo_resp(seq, fp, lport);
2222}
2223
2224/**
2225 * fcoe_elsct_send - FCoE specific ELS handler
2226 *
2227 * This does special case handling of FIP encapsualted ELS exchanges for FCoE,
2228 * using FCoE specific response handlers and passing the FIP controller as
2229 * the argument (the lport is still available from the exchange).
2230 *
2231 * Most of the work here is just handed off to the libfc routine.
2232 */
2233static struct fc_seq *fcoe_elsct_send(struct fc_lport *lport,
2234 u32 did, struct fc_frame *fp, unsigned int op,
2235 void (*resp)(struct fc_seq *, struct fc_frame *, void *),
2236 void *arg, u32 timeout)
2237{
2238 struct fcoe_port *port = lport_priv(lport);
2239 struct fcoe_interface *fcoe = port->fcoe;
2240 struct fcoe_ctlr *fip = &fcoe->ctlr;
2241 struct fc_frame_header *fh = fc_frame_header_get(fp);
2242
2243 switch (op) {
2244 case ELS_FLOGI:
2245 case ELS_FDISC:
2246 return fc_elsct_send(lport, did, fp, op, fcoe_flogi_resp,
2247 fip, timeout);
2248 case ELS_LOGO:
2249 /* only hook onto fabric logouts, not port logouts */
2250 if (ntoh24(fh->fh_d_id) != FC_FID_FLOGI)
2251 break;
2252 return fc_elsct_send(lport, did, fp, op, fcoe_logo_resp,
2253 fip, timeout);
2254 }
2255 return fc_elsct_send(lport, did, fp, op, resp, arg, timeout);
2256}
2257
Chris Leech9a057532009-11-03 11:46:40 -08002258/**
2259 * fcoe_vport_create() - create an fc_host/scsi_host for a vport
2260 * @vport: fc_vport object to create a new fc_host for
2261 * @disabled: start the new fc_host in a disabled state by default?
2262 *
2263 * Returns: 0 for success
2264 */
2265static int fcoe_vport_create(struct fc_vport *vport, bool disabled)
2266{
2267 struct Scsi_Host *shost = vport_to_shost(vport);
2268 struct fc_lport *n_port = shost_priv(shost);
2269 struct fcoe_port *port = lport_priv(n_port);
2270 struct fcoe_interface *fcoe = port->fcoe;
2271 struct net_device *netdev = fcoe->netdev;
2272 struct fc_lport *vn_port;
2273
2274 mutex_lock(&fcoe_config_mutex);
2275 vn_port = fcoe_if_create(fcoe, &vport->dev, 1);
2276 mutex_unlock(&fcoe_config_mutex);
2277
2278 if (IS_ERR(vn_port)) {
2279 printk(KERN_ERR "fcoe: fcoe_vport_create(%s) failed\n",
2280 netdev->name);
2281 return -EIO;
2282 }
2283
2284 if (disabled) {
2285 fc_vport_set_state(vport, FC_VPORT_DISABLED);
2286 } else {
2287 vn_port->boot_time = jiffies;
2288 fc_fabric_login(vn_port);
2289 fc_vport_setlink(vn_port);
2290 }
2291 return 0;
2292}
2293
2294/**
2295 * fcoe_vport_destroy() - destroy the fc_host/scsi_host for a vport
2296 * @vport: fc_vport object that is being destroyed
2297 *
2298 * Returns: 0 for success
2299 */
2300static int fcoe_vport_destroy(struct fc_vport *vport)
2301{
2302 struct Scsi_Host *shost = vport_to_shost(vport);
2303 struct fc_lport *n_port = shost_priv(shost);
2304 struct fc_lport *vn_port = vport->dd_data;
2305 struct fcoe_port *port = lport_priv(vn_port);
2306
2307 mutex_lock(&n_port->lp_mutex);
2308 list_del(&vn_port->list);
2309 mutex_unlock(&n_port->lp_mutex);
2310 schedule_work(&port->destroy_work);
2311 return 0;
2312}
2313
2314/**
2315 * fcoe_vport_disable() - change vport state
2316 * @vport: vport to bring online/offline
2317 * @disable: should the vport be disabled?
2318 */
2319static int fcoe_vport_disable(struct fc_vport *vport, bool disable)
2320{
2321 struct fc_lport *lport = vport->dd_data;
2322
2323 if (disable) {
2324 fc_vport_set_state(vport, FC_VPORT_DISABLED);
2325 fc_fabric_logoff(lport);
2326 } else {
2327 lport->boot_time = jiffies;
2328 fc_fabric_login(lport);
2329 fc_vport_setlink(lport);
2330 }
2331
2332 return 0;
2333}
2334
Chris Leechdc8596d2009-11-03 11:47:18 -08002335/**
2336 * fcoe_vport_set_symbolic_name() - append vport string to symbolic name
2337 * @vport: fc_vport with a new symbolic name string
2338 *
2339 * After generating a new symbolic name string, a new RSPN_ID request is
2340 * sent to the name server. There is no response handler, so if it fails
2341 * for some reason it will not be retried.
2342 */
2343static void fcoe_set_vport_symbolic_name(struct fc_vport *vport)
2344{
2345 struct fc_lport *lport = vport->dd_data;
2346 struct fc_frame *fp;
2347 size_t len;
2348
2349 snprintf(fc_host_symbolic_name(lport->host), FC_SYMBOLIC_NAME_SIZE,
2350 "%s v%s over %s : %s", FCOE_NAME, FCOE_VERSION,
2351 fcoe_netdev(lport)->name, vport->symbolic_name);
2352
2353 if (lport->state != LPORT_ST_READY)
2354 return;
2355
2356 len = strnlen(fc_host_symbolic_name(lport->host), 255);
2357 fp = fc_frame_alloc(lport,
2358 sizeof(struct fc_ct_hdr) +
2359 sizeof(struct fc_ns_rspn) + len);
2360 if (!fp)
2361 return;
2362 lport->tt.elsct_send(lport, FC_FID_DIR_SERV, fp, FC_NS_RSPN_ID,
2363 NULL, NULL, lport->e_d_tov);
2364}
2365