blob: f1c126b798af98aa0f6fdcf3ebf72d81fd00e278 [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,
Vasu Dev7f349142009-03-27 09:06:31 -0700137};
138
Chris Leeche9084bb2009-11-03 11:46:34 -0800139struct fc_function_template fcoe_vport_transport_function = {
140 .show_host_node_name = 1,
141 .show_host_port_name = 1,
142 .show_host_supported_classes = 1,
143 .show_host_supported_fc4s = 1,
144 .show_host_active_fc4s = 1,
145 .show_host_maxframe_size = 1,
146
147 .show_host_port_id = 1,
148 .show_host_supported_speeds = 1,
149 .get_host_speed = fc_get_host_speed,
150 .show_host_speed = 1,
151 .show_host_port_type = 1,
152 .get_host_port_state = fc_get_host_port_state,
153 .show_host_port_state = 1,
154 .show_host_symbolic_name = 1,
155
156 .dd_fcrport_size = sizeof(struct fc_rport_libfc_priv),
157 .show_rport_maxframe_size = 1,
158 .show_rport_supported_classes = 1,
159
160 .show_host_fabric_name = 1,
161 .show_starget_node_name = 1,
162 .show_starget_port_name = 1,
163 .show_starget_port_id = 1,
164 .set_rport_dev_loss_tmo = fc_set_rport_loss_tmo,
165 .show_rport_dev_loss_tmo = 1,
166 .get_fc_host_stats = fc_get_host_stats,
167 .issue_fc_host_lip = fcoe_reset,
168
169 .terminate_rport_io = fc_rport_terminate_io,
170};
171
Vasu Dev7f349142009-03-27 09:06:31 -0700172static struct scsi_host_template fcoe_shost_template = {
173 .module = THIS_MODULE,
174 .name = "FCoE Driver",
175 .proc_name = FCOE_NAME,
176 .queuecommand = fc_queuecommand,
177 .eh_abort_handler = fc_eh_abort,
178 .eh_device_reset_handler = fc_eh_device_reset,
179 .eh_host_reset_handler = fc_eh_host_reset,
180 .slave_alloc = fc_slave_alloc,
181 .change_queue_depth = fc_change_queue_depth,
182 .change_queue_type = fc_change_queue_type,
183 .this_id = -1,
Vasu Dev14caf442009-10-15 17:46:55 -0700184 .cmd_per_lun = 3,
Vasu Dev7f349142009-03-27 09:06:31 -0700185 .can_queue = FCOE_MAX_OUTSTANDING_COMMANDS,
186 .use_clustering = ENABLE_CLUSTERING,
187 .sg_tablesize = SG_ALL,
188 .max_sectors = 0xffff,
189};
190
Chris Leech54b649f2009-08-25 14:00:07 -0700191static int fcoe_fip_recv(struct sk_buff *skb, struct net_device *dev,
192 struct packet_type *ptype,
193 struct net_device *orig_dev);
194/**
195 * fcoe_interface_setup()
196 * @fcoe: new fcoe_interface
197 * @netdev : ptr to the associated netdevice struct
198 *
199 * Returns : 0 for success
Chris Leech2e70e242009-08-25 14:00:23 -0700200 * Locking: must be called with the RTNL mutex held
Chris Leech54b649f2009-08-25 14:00:07 -0700201 */
202static int fcoe_interface_setup(struct fcoe_interface *fcoe,
203 struct net_device *netdev)
204{
205 struct fcoe_ctlr *fip = &fcoe->ctlr;
206 struct netdev_hw_addr *ha;
207 u8 flogi_maddr[ETH_ALEN];
Yi Zoub7a727f2009-10-21 16:28:03 -0700208 const struct net_device_ops *ops;
Chris Leech54b649f2009-08-25 14:00:07 -0700209
210 fcoe->netdev = netdev;
211
Yi Zoub7a727f2009-10-21 16:28:03 -0700212 /* Let LLD initialize for FCoE */
213 ops = netdev->netdev_ops;
214 if (ops->ndo_fcoe_enable) {
215 if (ops->ndo_fcoe_enable(netdev))
216 FCOE_NETDEV_DBG(netdev, "Failed to enable FCoE"
217 " specific feature for LLD.\n");
218 }
219
Chris Leech54b649f2009-08-25 14:00:07 -0700220 /* Do not support for bonding device */
221 if ((netdev->priv_flags & IFF_MASTER_ALB) ||
222 (netdev->priv_flags & IFF_SLAVE_INACTIVE) ||
223 (netdev->priv_flags & IFF_MASTER_8023AD)) {
224 return -EOPNOTSUPP;
225 }
226
227 /* look for SAN MAC address, if multiple SAN MACs exist, only
228 * use the first one for SPMA */
229 rcu_read_lock();
230 for_each_dev_addr(netdev, ha) {
231 if ((ha->type == NETDEV_HW_ADDR_T_SAN) &&
232 (is_valid_ether_addr(fip->ctl_src_addr))) {
233 memcpy(fip->ctl_src_addr, ha->addr, ETH_ALEN);
234 fip->spma = 1;
235 break;
236 }
237 }
238 rcu_read_unlock();
239
240 /* setup Source Mac Address */
241 if (!fip->spma)
242 memcpy(fip->ctl_src_addr, netdev->dev_addr, netdev->addr_len);
243
244 /*
245 * Add FCoE MAC address as second unicast MAC address
246 * or enter promiscuous mode if not capable of listening
247 * for multiple unicast MACs.
248 */
Chris Leech54b649f2009-08-25 14:00:07 -0700249 memcpy(flogi_maddr, (u8[6]) FC_FCOE_FLOGI_MAC, ETH_ALEN);
250 dev_unicast_add(netdev, flogi_maddr);
251 if (fip->spma)
252 dev_unicast_add(netdev, fip->ctl_src_addr);
253 dev_mc_add(netdev, FIP_ALL_ENODE_MACS, ETH_ALEN, 0);
Chris Leech54b649f2009-08-25 14:00:07 -0700254
255 /*
256 * setup the receive function from ethernet driver
257 * on the ethertype for the given device
258 */
259 fcoe->fcoe_packet_type.func = fcoe_rcv;
260 fcoe->fcoe_packet_type.type = __constant_htons(ETH_P_FCOE);
261 fcoe->fcoe_packet_type.dev = netdev;
262 dev_add_pack(&fcoe->fcoe_packet_type);
263
264 fcoe->fip_packet_type.func = fcoe_fip_recv;
265 fcoe->fip_packet_type.type = htons(ETH_P_FIP);
266 fcoe->fip_packet_type.dev = netdev;
267 dev_add_pack(&fcoe->fip_packet_type);
268
269 return 0;
270}
271
272static void fcoe_fip_send(struct fcoe_ctlr *fip, struct sk_buff *skb);
Chris Leech11b56182009-11-03 11:46:29 -0800273static void fcoe_update_src_mac(struct fc_lport *lport, u8 *addr);
274static u8 *fcoe_get_src_mac(struct fc_lport *lport);
Chris Leech2e70e242009-08-25 14:00:23 -0700275static void fcoe_destroy_work(struct work_struct *work);
Chris Leech54b649f2009-08-25 14:00:07 -0700276
Vasu Dev7f349142009-03-27 09:06:31 -0700277/**
Chris Leech030f4e02009-08-25 14:00:02 -0700278 * fcoe_interface_create()
279 * @netdev: network interface
280 *
281 * Returns: pointer to a struct fcoe_interface or NULL on error
282 */
283static struct fcoe_interface *fcoe_interface_create(struct net_device *netdev)
284{
285 struct fcoe_interface *fcoe;
286
287 fcoe = kzalloc(sizeof(*fcoe), GFP_KERNEL);
288 if (!fcoe) {
289 FCOE_NETDEV_DBG(netdev, "Could not allocate fcoe structure\n");
290 return NULL;
291 }
292
Chris Leech2e70e242009-08-25 14:00:23 -0700293 dev_hold(netdev);
Chris Leech030f4e02009-08-25 14:00:02 -0700294 kref_init(&fcoe->kref);
Chris Leech54b649f2009-08-25 14:00:07 -0700295
296 /*
297 * Initialize FIP.
298 */
299 fcoe_ctlr_init(&fcoe->ctlr);
300 fcoe->ctlr.send = fcoe_fip_send;
301 fcoe->ctlr.update_mac = fcoe_update_src_mac;
Chris Leech11b56182009-11-03 11:46:29 -0800302 fcoe->ctlr.get_src_addr = fcoe_get_src_mac;
Chris Leech54b649f2009-08-25 14:00:07 -0700303
304 fcoe_interface_setup(fcoe, netdev);
Chris Leech030f4e02009-08-25 14:00:02 -0700305
306 return fcoe;
307}
308
309/**
Chris Leech54b649f2009-08-25 14:00:07 -0700310 * fcoe_interface_cleanup() - clean up netdev configurations
311 * @fcoe:
Chris Leech2e70e242009-08-25 14:00:23 -0700312 *
313 * Caller must be holding the RTNL mutex
Chris Leech54b649f2009-08-25 14:00:07 -0700314 */
315void fcoe_interface_cleanup(struct fcoe_interface *fcoe)
316{
317 struct net_device *netdev = fcoe->netdev;
318 struct fcoe_ctlr *fip = &fcoe->ctlr;
319 u8 flogi_maddr[ETH_ALEN];
Yi Zoub7a727f2009-10-21 16:28:03 -0700320 const struct net_device_ops *ops;
Chris Leech54b649f2009-08-25 14:00:07 -0700321
322 /*
323 * Don't listen for Ethernet packets anymore.
324 * synchronize_net() ensures that the packet handlers are not running
325 * on another CPU. dev_remove_pack() would do that, this calls the
326 * unsyncronized version __dev_remove_pack() to avoid multiple delays.
327 */
328 __dev_remove_pack(&fcoe->fcoe_packet_type);
329 __dev_remove_pack(&fcoe->fip_packet_type);
330 synchronize_net();
331
Chris Leech54b649f2009-08-25 14:00:07 -0700332 /* Delete secondary MAC addresses */
Chris Leech54b649f2009-08-25 14:00:07 -0700333 memcpy(flogi_maddr, (u8[6]) FC_FCOE_FLOGI_MAC, ETH_ALEN);
334 dev_unicast_delete(netdev, flogi_maddr);
Chris Leech54b649f2009-08-25 14:00:07 -0700335 if (fip->spma)
336 dev_unicast_delete(netdev, fip->ctl_src_addr);
337 dev_mc_delete(netdev, FIP_ALL_ENODE_MACS, ETH_ALEN, 0);
Yi Zoub7a727f2009-10-21 16:28:03 -0700338
339 /* Tell the LLD we are done w/ FCoE */
340 ops = netdev->netdev_ops;
341 if (ops->ndo_fcoe_disable) {
342 if (ops->ndo_fcoe_disable(netdev))
343 FCOE_NETDEV_DBG(netdev, "Failed to disable FCoE"
344 " specific feature for LLD.\n");
345 }
Chris Leech54b649f2009-08-25 14:00:07 -0700346}
347
348/**
Chris Leech030f4e02009-08-25 14:00:02 -0700349 * fcoe_interface_release() - fcoe_port kref release function
350 * @kref: embedded reference count in an fcoe_interface struct
351 */
352static void fcoe_interface_release(struct kref *kref)
353{
354 struct fcoe_interface *fcoe;
Chris Leech2e70e242009-08-25 14:00:23 -0700355 struct net_device *netdev;
Chris Leech030f4e02009-08-25 14:00:02 -0700356
357 fcoe = container_of(kref, struct fcoe_interface, kref);
Chris Leech2e70e242009-08-25 14:00:23 -0700358 netdev = fcoe->netdev;
359 /* tear-down the FCoE controller */
360 fcoe_ctlr_destroy(&fcoe->ctlr);
Chris Leech030f4e02009-08-25 14:00:02 -0700361 kfree(fcoe);
Chris Leech2e70e242009-08-25 14:00:23 -0700362 dev_put(netdev);
Chris Leech030f4e02009-08-25 14:00:02 -0700363}
364
365/**
366 * fcoe_interface_get()
367 * @fcoe:
368 */
369static inline void fcoe_interface_get(struct fcoe_interface *fcoe)
370{
371 kref_get(&fcoe->kref);
372}
373
374/**
375 * fcoe_interface_put()
376 * @fcoe:
377 */
378static inline void fcoe_interface_put(struct fcoe_interface *fcoe)
379{
380 kref_put(&fcoe->kref, fcoe_interface_release);
381}
382
383/**
Vasu Devab6b85c2009-05-17 12:33:08 +0000384 * fcoe_fip_recv - handle a received FIP frame.
385 * @skb: the receive skb
386 * @dev: associated &net_device
387 * @ptype: the &packet_type structure which was used to register this handler.
388 * @orig_dev: original receive &net_device, in case @dev is a bond.
389 *
390 * Returns: 0 for success
391 */
392static int fcoe_fip_recv(struct sk_buff *skb, struct net_device *dev,
393 struct packet_type *ptype,
394 struct net_device *orig_dev)
395{
Chris Leech259ad852009-08-25 13:59:41 -0700396 struct fcoe_interface *fcoe;
Vasu Devab6b85c2009-05-17 12:33:08 +0000397
Chris Leech259ad852009-08-25 13:59:41 -0700398 fcoe = container_of(ptype, struct fcoe_interface, fip_packet_type);
Chris Leech3fe9a0b2009-08-25 13:59:46 -0700399 fcoe_ctlr_recv(&fcoe->ctlr, skb);
Vasu Devab6b85c2009-05-17 12:33:08 +0000400 return 0;
401}
402
403/**
404 * fcoe_fip_send() - send an Ethernet-encapsulated FIP frame.
405 * @fip: FCoE controller.
406 * @skb: FIP Packet.
407 */
408static void fcoe_fip_send(struct fcoe_ctlr *fip, struct sk_buff *skb)
409{
Chris Leech3fe9a0b2009-08-25 13:59:46 -0700410 skb->dev = fcoe_from_ctlr(fip)->netdev;
Vasu Devab6b85c2009-05-17 12:33:08 +0000411 dev_queue_xmit(skb);
412}
413
414/**
415 * fcoe_update_src_mac() - Update Ethernet MAC filters.
Chris Leech11b56182009-11-03 11:46:29 -0800416 * @lport: libfc lport
417 * @addr: Unicast MAC address to add.
Vasu Devab6b85c2009-05-17 12:33:08 +0000418 *
419 * Remove any previously-set unicast MAC filter.
420 * Add secondary FCoE MAC address filter for our OUI.
421 */
Chris Leech11b56182009-11-03 11:46:29 -0800422static void fcoe_update_src_mac(struct fc_lport *lport, u8 *addr)
Vasu Devab6b85c2009-05-17 12:33:08 +0000423{
Chris Leech11b56182009-11-03 11:46:29 -0800424 struct fcoe_port *port = lport_priv(lport);
425 struct fcoe_interface *fcoe = port->fcoe;
Vasu Devab6b85c2009-05-17 12:33:08 +0000426
Vasu Devab6b85c2009-05-17 12:33:08 +0000427 rtnl_lock();
Chris Leech11b56182009-11-03 11:46:29 -0800428 if (!is_zero_ether_addr(port->data_src_addr))
429 dev_unicast_delete(fcoe->netdev, port->data_src_addr);
430 if (!is_zero_ether_addr(addr))
431 dev_unicast_add(fcoe->netdev, addr);
432 memcpy(port->data_src_addr, addr, ETH_ALEN);
Vasu Devab6b85c2009-05-17 12:33:08 +0000433 rtnl_unlock();
434}
435
436/**
Chris Leech11b56182009-11-03 11:46:29 -0800437 * fcoe_get_src_mac() - return the Ethernet source address for an lport
438 * @lport: libfc lport
439 */
440static u8 *fcoe_get_src_mac(struct fc_lport *lport)
441{
442 struct fcoe_port *port = lport_priv(lport);
443
444 return port->data_src_addr;
445}
446
447/**
Vasu Dev7f349142009-03-27 09:06:31 -0700448 * fcoe_lport_config() - sets up the fc_lport
449 * @lp: ptr to the fc_lport
Vasu Dev7f349142009-03-27 09:06:31 -0700450 *
451 * Returns: 0 for success
452 */
453static int fcoe_lport_config(struct fc_lport *lp)
454{
455 lp->link_up = 0;
456 lp->qfull = 0;
457 lp->max_retry_count = 3;
Abhijeet Joglekara3666952009-05-01 10:01:26 -0700458 lp->max_rport_retry_count = 3;
Vasu Dev7f349142009-03-27 09:06:31 -0700459 lp->e_d_tov = 2 * 1000; /* FC-FS default */
460 lp->r_a_tov = 2 * 2 * 1000;
461 lp->service_params = (FCP_SPPF_INIT_FCN | FCP_SPPF_RD_XRDY_DIS |
462 FCP_SPPF_RETRY | FCP_SPPF_CONF_COMPL);
Chris Leech9a057532009-11-03 11:46:40 -0800463 lp->does_npiv = 1;
Vasu Dev7f349142009-03-27 09:06:31 -0700464
465 fc_lport_init_stats(lp);
466
467 /* lport fc_lport related configuration */
468 fc_lport_config(lp);
469
470 /* offload related configuration */
471 lp->crc_offload = 0;
472 lp->seq_offload = 0;
473 lp->lro_enabled = 0;
474 lp->lro_xid = 0;
475 lp->lso_max = 0;
476
477 return 0;
478}
479
480/**
Vasu Dev1047f222009-05-06 10:52:40 -0700481 * fcoe_queue_timer() - fcoe queue timer
482 * @lp: the fc_lport pointer
483 *
484 * Calls fcoe_check_wait_queue on timeout
485 *
486 */
487static void fcoe_queue_timer(ulong lp)
488{
489 fcoe_check_wait_queue((struct fc_lport *)lp, NULL);
490}
491
492/**
Vasu Dev7f349142009-03-27 09:06:31 -0700493 * fcoe_netdev_config() - Set up netdev for SW FCoE
494 * @lp : ptr to the fc_lport
495 * @netdev : ptr to the associated netdevice struct
496 *
497 * Must be called after fcoe_lport_config() as it will use lport mutex
498 *
499 * Returns : 0 for success
500 */
501static int fcoe_netdev_config(struct fc_lport *lp, struct net_device *netdev)
502{
503 u32 mfs;
504 u64 wwnn, wwpn;
Chris Leech25024982009-08-25 13:59:35 -0700505 struct fcoe_interface *fcoe;
Chris Leech014f5c32009-08-25 13:59:30 -0700506 struct fcoe_port *port;
Vasu Dev7f349142009-03-27 09:06:31 -0700507
508 /* Setup lport private data to point to fcoe softc */
Chris Leech014f5c32009-08-25 13:59:30 -0700509 port = lport_priv(lp);
Chris Leech25024982009-08-25 13:59:35 -0700510 fcoe = port->fcoe;
Vasu Dev7f349142009-03-27 09:06:31 -0700511
512 /*
513 * Determine max frame size based on underlying device and optional
514 * user-configured limit. If the MFS is too low, fcoe_link_ok()
515 * will return 0, so do this first.
516 */
Yi Zou7221d7e2009-10-21 16:27:52 -0700517 mfs = netdev->mtu;
518 if (netdev->features & NETIF_F_FCOE_MTU) {
519 mfs = FCOE_MTU;
520 FCOE_NETDEV_DBG(netdev, "Supports FCOE_MTU of %d bytes\n", mfs);
521 }
522 mfs -= (sizeof(struct fcoe_hdr) + sizeof(struct fcoe_crc_eof));
Vasu Dev7f349142009-03-27 09:06:31 -0700523 if (fc_set_mfs(lp, mfs))
524 return -EINVAL;
525
Vasu Dev7f349142009-03-27 09:06:31 -0700526 /* offload features support */
Vasu Dev1d1b88d2009-07-29 17:05:45 -0700527 if (netdev->features & NETIF_F_SG)
Vasu Dev7f349142009-03-27 09:06:31 -0700528 lp->sg_supp = 1;
529
Vasu Dev7f349142009-03-27 09:06:31 -0700530 if (netdev->features & NETIF_F_FCOE_CRC) {
531 lp->crc_offload = 1;
Robert Loved5488eb2009-06-10 15:30:59 -0700532 FCOE_NETDEV_DBG(netdev, "Supports FCCRC offload\n");
Vasu Dev7f349142009-03-27 09:06:31 -0700533 }
Vasu Dev7f349142009-03-27 09:06:31 -0700534 if (netdev->features & NETIF_F_FSO) {
535 lp->seq_offload = 1;
536 lp->lso_max = netdev->gso_max_size;
Robert Loved5488eb2009-06-10 15:30:59 -0700537 FCOE_NETDEV_DBG(netdev, "Supports LSO for max len 0x%x\n",
538 lp->lso_max);
Vasu Dev7f349142009-03-27 09:06:31 -0700539 }
Vasu Dev7f349142009-03-27 09:06:31 -0700540 if (netdev->fcoe_ddp_xid) {
541 lp->lro_enabled = 1;
542 lp->lro_xid = netdev->fcoe_ddp_xid;
Robert Loved5488eb2009-06-10 15:30:59 -0700543 FCOE_NETDEV_DBG(netdev, "Supports LRO for max xid 0x%x\n",
544 lp->lro_xid);
Vasu Dev7f349142009-03-27 09:06:31 -0700545 }
Chris Leech014f5c32009-08-25 13:59:30 -0700546 skb_queue_head_init(&port->fcoe_pending_queue);
547 port->fcoe_pending_queue_active = 0;
548 setup_timer(&port->timer, fcoe_queue_timer, (unsigned long)lp);
Vasu Dev7f349142009-03-27 09:06:31 -0700549
Chris Leech9a057532009-11-03 11:46:40 -0800550 if (!lp->vport) {
551 wwnn = fcoe_wwn_from_mac(netdev->dev_addr, 1, 0);
552 fc_set_wwnn(lp, wwnn);
553 /* XXX - 3rd arg needs to be vlan id */
554 wwpn = fcoe_wwn_from_mac(netdev->dev_addr, 2, 0);
555 fc_set_wwpn(lp, wwpn);
556 }
Vasu Dev7f349142009-03-27 09:06:31 -0700557
Vasu Dev7f349142009-03-27 09:06:31 -0700558 return 0;
559}
560
561/**
562 * fcoe_shost_config() - Sets up fc_lport->host
563 * @lp : ptr to the fc_lport
564 * @shost : ptr to the associated scsi host
565 * @dev : device associated to scsi host
566 *
567 * Must be called after fcoe_lport_config() and fcoe_netdev_config()
568 *
569 * Returns : 0 for success
570 */
571static int fcoe_shost_config(struct fc_lport *lp, struct Scsi_Host *shost,
572 struct device *dev)
573{
574 int rc = 0;
575
576 /* lport scsi host config */
Vasu Dev7f349142009-03-27 09:06:31 -0700577 lp->host->max_lun = FCOE_MAX_LUN;
578 lp->host->max_id = FCOE_MAX_FCP_TARGET;
579 lp->host->max_channel = 0;
Chris Leeche9084bb2009-11-03 11:46:34 -0800580 if (lp->vport)
581 lp->host->transportt = fcoe_vport_transport_template;
582 else
583 lp->host->transportt = fcoe_transport_template;
Vasu Dev7f349142009-03-27 09:06:31 -0700584
585 /* add the new host to the SCSI-ml */
586 rc = scsi_add_host(lp->host, dev);
587 if (rc) {
Robert Loved5488eb2009-06-10 15:30:59 -0700588 FCOE_NETDEV_DBG(fcoe_netdev(lp), "fcoe_shost_config: "
589 "error on scsi_add_host\n");
Vasu Dev7f349142009-03-27 09:06:31 -0700590 return rc;
591 }
Chris Leech9a057532009-11-03 11:46:40 -0800592
593 if (!lp->vport)
594 fc_host_max_npiv_vports(lp->host) = USHORT_MAX;
595
Chris Leech5baa17c2009-11-03 11:46:56 -0800596 snprintf(fc_host_symbolic_name(lp->host), FC_SYMBOLIC_NAME_SIZE,
597 "%s v%s over %s", FCOE_NAME, FCOE_VERSION,
598 fcoe_netdev(lp)->name);
Vasu Dev7f349142009-03-27 09:06:31 -0700599
600 return 0;
601}
602
Vasu Devd7179682009-07-29 17:05:21 -0700603/*
604 * fcoe_oem_match() - match for read types IO
605 * @fp: the fc_frame for new IO.
606 *
607 * Returns : true for read types IO, otherwise returns false.
608 */
609bool fcoe_oem_match(struct fc_frame *fp)
610{
Yi Zou05cc7392009-08-25 13:59:03 -0700611 return fc_fcp_is_read(fr_fsp(fp)) &&
612 (fr_fsp(fp)->data_len > fcoe_ddp_min);
Vasu Devd7179682009-07-29 17:05:21 -0700613}
614
Vasu Dev7f349142009-03-27 09:06:31 -0700615/**
616 * fcoe_em_config() - allocates em for this lport
Chris Leech014f5c32009-08-25 13:59:30 -0700617 * @lp: the fcoe that em is to allocated for
Vasu Dev7f349142009-03-27 09:06:31 -0700618 *
619 * Returns : 0 on success
620 */
621static inline int fcoe_em_config(struct fc_lport *lp)
622{
Chris Leech014f5c32009-08-25 13:59:30 -0700623 struct fcoe_port *port = lport_priv(lp);
Chris Leech25024982009-08-25 13:59:35 -0700624 struct fcoe_interface *fcoe = port->fcoe;
625 struct fcoe_interface *oldfcoe = NULL;
Vasu Dev1d1b88d2009-07-29 17:05:45 -0700626 struct net_device *old_real_dev, *cur_real_dev;
Vasu Devd7179682009-07-29 17:05:21 -0700627 u16 min_xid = FCOE_MIN_XID;
628 u16 max_xid = FCOE_MAX_XID;
629
630 /*
631 * Check if need to allocate an em instance for
632 * offload exchange ids to be shared across all VN_PORTs/lport.
633 */
634 if (!lp->lro_enabled || !lp->lro_xid || (lp->lro_xid >= max_xid)) {
635 lp->lro_xid = 0;
636 goto skip_oem;
637 }
638
639 /*
640 * Reuse existing offload em instance in case
Vasu Dev1d1b88d2009-07-29 17:05:45 -0700641 * it is already allocated on real eth device
Vasu Devd7179682009-07-29 17:05:21 -0700642 */
Chris Leech25024982009-08-25 13:59:35 -0700643 if (fcoe->netdev->priv_flags & IFF_802_1Q_VLAN)
644 cur_real_dev = vlan_dev_real_dev(fcoe->netdev);
Vasu Dev1d1b88d2009-07-29 17:05:45 -0700645 else
Chris Leech25024982009-08-25 13:59:35 -0700646 cur_real_dev = fcoe->netdev;
Vasu Dev1d1b88d2009-07-29 17:05:45 -0700647
Chris Leech25024982009-08-25 13:59:35 -0700648 list_for_each_entry(oldfcoe, &fcoe_hostlist, list) {
Chris Leech25024982009-08-25 13:59:35 -0700649 if (oldfcoe->netdev->priv_flags & IFF_802_1Q_VLAN)
650 old_real_dev = vlan_dev_real_dev(oldfcoe->netdev);
Vasu Dev1d1b88d2009-07-29 17:05:45 -0700651 else
Chris Leech25024982009-08-25 13:59:35 -0700652 old_real_dev = oldfcoe->netdev;
Vasu Dev1d1b88d2009-07-29 17:05:45 -0700653
654 if (cur_real_dev == old_real_dev) {
Chris Leech991cbb62009-08-25 13:59:51 -0700655 fcoe->oem = oldfcoe->oem;
Vasu Devd7179682009-07-29 17:05:21 -0700656 break;
657 }
658 }
659
Chris Leech991cbb62009-08-25 13:59:51 -0700660 if (fcoe->oem) {
661 if (!fc_exch_mgr_add(lp, fcoe->oem, fcoe_oem_match)) {
Vasu Devd7179682009-07-29 17:05:21 -0700662 printk(KERN_ERR "fcoe_em_config: failed to add "
663 "offload em:%p on interface:%s\n",
Chris Leech991cbb62009-08-25 13:59:51 -0700664 fcoe->oem, fcoe->netdev->name);
Vasu Devd7179682009-07-29 17:05:21 -0700665 return -ENOMEM;
666 }
667 } else {
Chris Leech991cbb62009-08-25 13:59:51 -0700668 fcoe->oem = fc_exch_mgr_alloc(lp, FC_CLASS_3,
Vasu Devd7179682009-07-29 17:05:21 -0700669 FCOE_MIN_XID, lp->lro_xid,
670 fcoe_oem_match);
Chris Leech991cbb62009-08-25 13:59:51 -0700671 if (!fcoe->oem) {
Vasu Devd7179682009-07-29 17:05:21 -0700672 printk(KERN_ERR "fcoe_em_config: failed to allocate "
673 "em for offload exches on interface:%s\n",
Chris Leech25024982009-08-25 13:59:35 -0700674 fcoe->netdev->name);
Vasu Devd7179682009-07-29 17:05:21 -0700675 return -ENOMEM;
676 }
677 }
678
679 /*
680 * Exclude offload EM xid range from next EM xid range.
681 */
682 min_xid += lp->lro_xid + 1;
683
684skip_oem:
685 if (!fc_exch_mgr_alloc(lp, FC_CLASS_3, min_xid, max_xid, NULL)) {
686 printk(KERN_ERR "fcoe_em_config: failed to "
Chris Leech25024982009-08-25 13:59:35 -0700687 "allocate em on interface %s\n", fcoe->netdev->name);
Vasu Dev7f349142009-03-27 09:06:31 -0700688 return -ENOMEM;
Vasu Devd7179682009-07-29 17:05:21 -0700689 }
Vasu Dev7f349142009-03-27 09:06:31 -0700690
691 return 0;
692}
693
694/**
695 * fcoe_if_destroy() - FCoE software HBA tear-down function
Chris Leechaf7f85d2009-08-25 13:59:24 -0700696 * @lport: fc_lport to destroy
Vasu Dev7f349142009-03-27 09:06:31 -0700697 */
Chris Leechaf7f85d2009-08-25 13:59:24 -0700698static void fcoe_if_destroy(struct fc_lport *lport)
Vasu Dev7f349142009-03-27 09:06:31 -0700699{
Chris Leech014f5c32009-08-25 13:59:30 -0700700 struct fcoe_port *port = lport_priv(lport);
701 struct fcoe_interface *fcoe = port->fcoe;
Chris Leech25024982009-08-25 13:59:35 -0700702 struct net_device *netdev = fcoe->netdev;
Vasu Dev7f349142009-03-27 09:06:31 -0700703
Robert Loved5488eb2009-06-10 15:30:59 -0700704 FCOE_NETDEV_DBG(netdev, "Destroying interface\n");
Vasu Dev7f349142009-03-27 09:06:31 -0700705
Vasu Dev7f349142009-03-27 09:06:31 -0700706 /* Logout of the fabric */
Chris Leechaf7f85d2009-08-25 13:59:24 -0700707 fc_fabric_logoff(lport);
Vasu Dev7f349142009-03-27 09:06:31 -0700708
Vasu Dev7f349142009-03-27 09:06:31 -0700709 /* Cleanup the fc_lport */
Chris Leechaf7f85d2009-08-25 13:59:24 -0700710 fc_lport_destroy(lport);
711 fc_fcp_destroy(lport);
Vasu Dev7f349142009-03-27 09:06:31 -0700712
Chris Leech54b649f2009-08-25 14:00:07 -0700713 /* Stop the transmit retry timer */
714 del_timer_sync(&port->timer);
715
716 /* Free existing transmit skbs */
717 fcoe_clean_pending_queue(lport);
718
Chris Leech11b56182009-11-03 11:46:29 -0800719 rtnl_lock();
720 if (!is_zero_ether_addr(port->data_src_addr))
721 dev_unicast_delete(netdev, port->data_src_addr);
722 rtnl_unlock();
723
Chris Leech54b649f2009-08-25 14:00:07 -0700724 /* receives may not be stopped until after this */
725 fcoe_interface_put(fcoe);
726
727 /* Free queued packets for the per-CPU receive threads */
728 fcoe_percpu_clean(lport);
729
Vasu Dev7f349142009-03-27 09:06:31 -0700730 /* Detach from the scsi-ml */
Chris Leechaf7f85d2009-08-25 13:59:24 -0700731 fc_remove_host(lport->host);
732 scsi_remove_host(lport->host);
Vasu Dev7f349142009-03-27 09:06:31 -0700733
734 /* There are no more rports or I/O, free the EM */
Chris Leechaf7f85d2009-08-25 13:59:24 -0700735 fc_exch_mgr_free(lport);
Vasu Dev7f349142009-03-27 09:06:31 -0700736
Vasu Dev7f349142009-03-27 09:06:31 -0700737 /* Free memory used by statistical counters */
Chris Leechaf7f85d2009-08-25 13:59:24 -0700738 fc_lport_free_stats(lport);
Vasu Dev7f349142009-03-27 09:06:31 -0700739
Chris Leech2e70e242009-08-25 14:00:23 -0700740 /* Release the Scsi_Host */
Chris Leechaf7f85d2009-08-25 13:59:24 -0700741 scsi_host_put(lport->host);
Vasu Dev7f349142009-03-27 09:06:31 -0700742}
743
744/*
745 * fcoe_ddp_setup - calls LLD's ddp_setup through net_device
746 * @lp: the corresponding fc_lport
747 * @xid: the exchange id for this ddp transfer
748 * @sgl: the scatterlist describing this transfer
749 * @sgc: number of sg items
750 *
751 * Returns : 0 no ddp
752 */
753static int fcoe_ddp_setup(struct fc_lport *lp, u16 xid,
754 struct scatterlist *sgl, unsigned int sgc)
755{
756 struct net_device *n = fcoe_netdev(lp);
757
Yi Zoub04d0232009-10-21 16:26:55 -0700758 if (n->netdev_ops->ndo_fcoe_ddp_setup)
Vasu Dev7f349142009-03-27 09:06:31 -0700759 return n->netdev_ops->ndo_fcoe_ddp_setup(n, xid, sgl, sgc);
760
761 return 0;
762}
763
764/*
765 * fcoe_ddp_done - calls LLD's ddp_done through net_device
766 * @lp: the corresponding fc_lport
767 * @xid: the exchange id for this ddp transfer
768 *
769 * Returns : the length of data that have been completed by ddp
770 */
771static int fcoe_ddp_done(struct fc_lport *lp, u16 xid)
772{
773 struct net_device *n = fcoe_netdev(lp);
774
Yi Zoub04d0232009-10-21 16:26:55 -0700775 if (n->netdev_ops->ndo_fcoe_ddp_done)
Vasu Dev7f349142009-03-27 09:06:31 -0700776 return n->netdev_ops->ndo_fcoe_ddp_done(n, xid);
777 return 0;
778}
779
Chris Leech11b56182009-11-03 11:46:29 -0800780static struct fc_seq *fcoe_elsct_send(struct fc_lport *lport,
781 u32 did, struct fc_frame *fp, unsigned int op,
782 void (*resp)(struct fc_seq *, struct fc_frame *, void *),
783 void *arg, u32 timeout);
784
Vasu Dev7f349142009-03-27 09:06:31 -0700785static struct libfc_function_template fcoe_libfc_fcn_templ = {
786 .frame_send = fcoe_xmit,
787 .ddp_setup = fcoe_ddp_setup,
788 .ddp_done = fcoe_ddp_done,
Chris Leech11b56182009-11-03 11:46:29 -0800789 .elsct_send = fcoe_elsct_send,
Vasu Dev7f349142009-03-27 09:06:31 -0700790};
791
792/**
Chris Leech030f4e02009-08-25 14:00:02 -0700793 * fcoe_if_create() - this function creates the fcoe port
794 * @fcoe: fcoe_interface structure to create an fc_lport instance on
Chris Leechaf7f85d2009-08-25 13:59:24 -0700795 * @parent: device pointer to be the parent in sysfs for the SCSI host
Chris Leech9a057532009-11-03 11:46:40 -0800796 * @npiv: is this a vport?
Vasu Dev7f349142009-03-27 09:06:31 -0700797 *
Chris Leech54b649f2009-08-25 14:00:07 -0700798 * Creates fc_lport struct and scsi_host for lport, configures lport.
Vasu Dev7f349142009-03-27 09:06:31 -0700799 *
Chris Leechaf7f85d2009-08-25 13:59:24 -0700800 * Returns : The allocated fc_lport or an error pointer
Vasu Dev7f349142009-03-27 09:06:31 -0700801 */
Chris Leech030f4e02009-08-25 14:00:02 -0700802static struct fc_lport *fcoe_if_create(struct fcoe_interface *fcoe,
Chris Leech9a057532009-11-03 11:46:40 -0800803 struct device *parent, int npiv)
Vasu Dev7f349142009-03-27 09:06:31 -0700804{
805 int rc;
Chris Leechaf7f85d2009-08-25 13:59:24 -0700806 struct fc_lport *lport = NULL;
Chris Leech014f5c32009-08-25 13:59:30 -0700807 struct fcoe_port *port;
Vasu Dev7f349142009-03-27 09:06:31 -0700808 struct Scsi_Host *shost;
Chris Leech030f4e02009-08-25 14:00:02 -0700809 struct net_device *netdev = fcoe->netdev;
Chris Leech9a057532009-11-03 11:46:40 -0800810 /*
811 * parent is only a vport if npiv is 1,
812 * but we'll only use vport in that case so go ahead and set it
813 */
814 struct fc_vport *vport = dev_to_vport(parent);
Vasu Dev7f349142009-03-27 09:06:31 -0700815
Robert Loved5488eb2009-06-10 15:30:59 -0700816 FCOE_NETDEV_DBG(netdev, "Create Interface\n");
Vasu Dev7f349142009-03-27 09:06:31 -0700817
Chris Leech9a057532009-11-03 11:46:40 -0800818 if (!npiv) {
819 lport = libfc_host_alloc(&fcoe_shost_template,
820 sizeof(struct fcoe_port));
821 } else {
822 lport = libfc_vport_create(vport,
823 sizeof(struct fcoe_port));
824 }
Chris Leech86221962009-11-03 11:46:08 -0800825 if (!lport) {
Chris Leech014f5c32009-08-25 13:59:30 -0700826 FCOE_NETDEV_DBG(netdev, "Could not allocate host structure\n");
827 rc = -ENOMEM;
Chris Leech030f4e02009-08-25 14:00:02 -0700828 goto out;
Chris Leech014f5c32009-08-25 13:59:30 -0700829 }
Chris Leech86221962009-11-03 11:46:08 -0800830 shost = lport->host;
Chris Leech014f5c32009-08-25 13:59:30 -0700831 port = lport_priv(lport);
Chris Leech2e70e242009-08-25 14:00:23 -0700832 port->lport = lport;
Chris Leech014f5c32009-08-25 13:59:30 -0700833 port->fcoe = fcoe;
Chris Leech2e70e242009-08-25 14:00:23 -0700834 INIT_WORK(&port->destroy_work, fcoe_destroy_work);
Vasu Dev7f349142009-03-27 09:06:31 -0700835
836 /* configure fc_lport, e.g., em */
Chris Leechaf7f85d2009-08-25 13:59:24 -0700837 rc = fcoe_lport_config(lport);
Vasu Dev7f349142009-03-27 09:06:31 -0700838 if (rc) {
Robert Loved5488eb2009-06-10 15:30:59 -0700839 FCOE_NETDEV_DBG(netdev, "Could not configure lport for the "
840 "interface\n");
Vasu Dev7f349142009-03-27 09:06:31 -0700841 goto out_host_put;
842 }
843
Chris Leech9a057532009-11-03 11:46:40 -0800844 if (npiv) {
845 FCOE_NETDEV_DBG(netdev, "Setting vport names, 0x%llX 0x%llX\n",
846 vport->node_name, vport->port_name);
847 fc_set_wwnn(lport, vport->node_name);
848 fc_set_wwpn(lport, vport->port_name);
849 }
850
Vasu Devab6b85c2009-05-17 12:33:08 +0000851 /* configure lport network properties */
Chris Leechaf7f85d2009-08-25 13:59:24 -0700852 rc = fcoe_netdev_config(lport, netdev);
Vasu Devab6b85c2009-05-17 12:33:08 +0000853 if (rc) {
Robert Loved5488eb2009-06-10 15:30:59 -0700854 FCOE_NETDEV_DBG(netdev, "Could not configure netdev for the "
855 "interface\n");
Chris Leech54b649f2009-08-25 14:00:07 -0700856 goto out_lp_destroy;
Vasu Devab6b85c2009-05-17 12:33:08 +0000857 }
Joe Eykholt97c83892009-03-17 11:42:40 -0700858
Vasu Dev7f349142009-03-27 09:06:31 -0700859 /* configure lport scsi host properties */
Chris Leechaf7f85d2009-08-25 13:59:24 -0700860 rc = fcoe_shost_config(lport, shost, parent);
Vasu Dev7f349142009-03-27 09:06:31 -0700861 if (rc) {
Robert Loved5488eb2009-06-10 15:30:59 -0700862 FCOE_NETDEV_DBG(netdev, "Could not configure shost for the "
863 "interface\n");
Chris Leech54b649f2009-08-25 14:00:07 -0700864 goto out_lp_destroy;
Vasu Dev7f349142009-03-27 09:06:31 -0700865 }
866
Vasu Dev7f349142009-03-27 09:06:31 -0700867 /* Initialize the library */
Chris Leechaf7f85d2009-08-25 13:59:24 -0700868 rc = fcoe_libfc_config(lport, &fcoe_libfc_fcn_templ);
Vasu Dev7f349142009-03-27 09:06:31 -0700869 if (rc) {
Robert Loved5488eb2009-06-10 15:30:59 -0700870 FCOE_NETDEV_DBG(netdev, "Could not configure libfc for the "
871 "interface\n");
Vasu Dev7f349142009-03-27 09:06:31 -0700872 goto out_lp_destroy;
873 }
874
Chris Leech9a057532009-11-03 11:46:40 -0800875 if (!npiv) {
876 /*
877 * fcoe_em_alloc() and fcoe_hostlist_add() both
878 * need to be atomic with respect to other changes to the
879 * hostlist since fcoe_em_alloc() looks for an existing EM
880 * instance on host list updated by fcoe_hostlist_add().
881 *
882 * This is currently handled through the fcoe_config_mutex
883 * begin held.
884 */
Chris Leechc863df32009-08-25 14:00:18 -0700885
Chris Leech9a057532009-11-03 11:46:40 -0800886 /* lport exch manager allocation */
887 rc = fcoe_em_config(lport);
888 if (rc) {
889 FCOE_NETDEV_DBG(netdev, "Could not configure the EM "
890 "for the interface\n");
891 goto out_lp_destroy;
892 }
Vasu Dev96316092009-07-29 17:05:00 -0700893 }
894
Chris Leech030f4e02009-08-25 14:00:02 -0700895 fcoe_interface_get(fcoe);
Chris Leechaf7f85d2009-08-25 13:59:24 -0700896 return lport;
Vasu Dev7f349142009-03-27 09:06:31 -0700897
898out_lp_destroy:
Chris Leechaf7f85d2009-08-25 13:59:24 -0700899 fc_exch_mgr_free(lport);
Vasu Dev7f349142009-03-27 09:06:31 -0700900out_host_put:
Chris Leechaf7f85d2009-08-25 13:59:24 -0700901 scsi_host_put(lport->host);
902out:
903 return ERR_PTR(rc);
Vasu Dev7f349142009-03-27 09:06:31 -0700904}
905
906/**
907 * fcoe_if_init() - attach to scsi transport
908 *
909 * Returns : 0 on success
910 */
911static int __init fcoe_if_init(void)
912{
913 /* attach to scsi transport */
Chris Leeche9084bb2009-11-03 11:46:34 -0800914 fcoe_transport_template = fc_attach_transport(&fcoe_transport_function);
915 fcoe_vport_transport_template =
916 fc_attach_transport(&fcoe_vport_transport_function);
Vasu Dev7f349142009-03-27 09:06:31 -0700917
Chris Leeche9084bb2009-11-03 11:46:34 -0800918 if (!fcoe_transport_template) {
Robert Loved5488eb2009-06-10 15:30:59 -0700919 printk(KERN_ERR "fcoe: Failed to attach to the FC transport\n");
Vasu Dev7f349142009-03-27 09:06:31 -0700920 return -ENODEV;
921 }
922
923 return 0;
924}
925
926/**
927 * fcoe_if_exit() - detach from scsi transport
928 *
929 * Returns : 0 on success
930 */
931int __exit fcoe_if_exit(void)
932{
Chris Leeche9084bb2009-11-03 11:46:34 -0800933 fc_release_transport(fcoe_transport_template);
934 fc_release_transport(fcoe_vport_transport_template);
935 fcoe_transport_template = NULL;
936 fcoe_vport_transport_template = NULL;
Vasu Dev7f349142009-03-27 09:06:31 -0700937 return 0;
938}
939
Robert Love85b4aa42008-12-09 15:10:24 -0800940/**
Robert Love8976f422009-03-17 11:41:46 -0700941 * fcoe_percpu_thread_create() - Create a receive thread for an online cpu
942 * @cpu: cpu index for the online cpu
943 */
944static void fcoe_percpu_thread_create(unsigned int cpu)
945{
946 struct fcoe_percpu_s *p;
947 struct task_struct *thread;
948
949 p = &per_cpu(fcoe_percpu, cpu);
950
951 thread = kthread_create(fcoe_percpu_receive_thread,
952 (void *)p, "fcoethread/%d", cpu);
953
Joe Eykholte7a51992009-08-25 14:04:08 -0700954 if (likely(!IS_ERR(thread))) {
Robert Love8976f422009-03-17 11:41:46 -0700955 kthread_bind(thread, cpu);
956 wake_up_process(thread);
957
958 spin_lock_bh(&p->fcoe_rx_list.lock);
959 p->thread = thread;
960 spin_unlock_bh(&p->fcoe_rx_list.lock);
961 }
962}
963
964/**
965 * fcoe_percpu_thread_destroy() - removes the rx thread for the given cpu
966 * @cpu: cpu index the rx thread is to be removed
967 *
968 * Destroys a per-CPU Rx thread. Any pending skbs are moved to the
969 * current CPU's Rx thread. If the thread being destroyed is bound to
970 * the CPU processing this context the skbs will be freed.
971 */
972static void fcoe_percpu_thread_destroy(unsigned int cpu)
973{
974 struct fcoe_percpu_s *p;
975 struct task_struct *thread;
976 struct page *crc_eof;
977 struct sk_buff *skb;
978#ifdef CONFIG_SMP
979 struct fcoe_percpu_s *p0;
980 unsigned targ_cpu = smp_processor_id();
981#endif /* CONFIG_SMP */
982
Robert Loved5488eb2009-06-10 15:30:59 -0700983 FCOE_DBG("Destroying receive thread for CPU %d\n", cpu);
Robert Love8976f422009-03-17 11:41:46 -0700984
985 /* Prevent any new skbs from being queued for this CPU. */
986 p = &per_cpu(fcoe_percpu, cpu);
987 spin_lock_bh(&p->fcoe_rx_list.lock);
988 thread = p->thread;
989 p->thread = NULL;
990 crc_eof = p->crc_eof_page;
991 p->crc_eof_page = NULL;
992 p->crc_eof_offset = 0;
993 spin_unlock_bh(&p->fcoe_rx_list.lock);
994
995#ifdef CONFIG_SMP
996 /*
997 * Don't bother moving the skb's if this context is running
998 * on the same CPU that is having its thread destroyed. This
999 * can easily happen when the module is removed.
1000 */
1001 if (cpu != targ_cpu) {
1002 p0 = &per_cpu(fcoe_percpu, targ_cpu);
1003 spin_lock_bh(&p0->fcoe_rx_list.lock);
1004 if (p0->thread) {
Robert Loved5488eb2009-06-10 15:30:59 -07001005 FCOE_DBG("Moving frames from CPU %d to CPU %d\n",
1006 cpu, targ_cpu);
Robert Love8976f422009-03-17 11:41:46 -07001007
1008 while ((skb = __skb_dequeue(&p->fcoe_rx_list)) != NULL)
1009 __skb_queue_tail(&p0->fcoe_rx_list, skb);
1010 spin_unlock_bh(&p0->fcoe_rx_list.lock);
1011 } else {
1012 /*
1013 * The targeted CPU is not initialized and cannot accept
1014 * new skbs. Unlock the targeted CPU and drop the skbs
1015 * on the CPU that is going offline.
1016 */
1017 while ((skb = __skb_dequeue(&p->fcoe_rx_list)) != NULL)
1018 kfree_skb(skb);
1019 spin_unlock_bh(&p0->fcoe_rx_list.lock);
1020 }
1021 } else {
1022 /*
1023 * This scenario occurs when the module is being removed
1024 * and all threads are being destroyed. skbs will continue
1025 * to be shifted from the CPU thread that is being removed
1026 * to the CPU thread associated with the CPU that is processing
1027 * the module removal. Once there is only one CPU Rx thread it
1028 * will reach this case and we will drop all skbs and later
1029 * stop the thread.
1030 */
1031 spin_lock_bh(&p->fcoe_rx_list.lock);
1032 while ((skb = __skb_dequeue(&p->fcoe_rx_list)) != NULL)
1033 kfree_skb(skb);
1034 spin_unlock_bh(&p->fcoe_rx_list.lock);
1035 }
1036#else
1037 /*
Chris Leechdd3fd722009-04-21 16:27:36 -07001038 * This a non-SMP scenario where the singular Rx thread is
Robert Love8976f422009-03-17 11:41:46 -07001039 * being removed. Free all skbs and stop the thread.
1040 */
1041 spin_lock_bh(&p->fcoe_rx_list.lock);
1042 while ((skb = __skb_dequeue(&p->fcoe_rx_list)) != NULL)
1043 kfree_skb(skb);
1044 spin_unlock_bh(&p->fcoe_rx_list.lock);
1045#endif
1046
1047 if (thread)
1048 kthread_stop(thread);
1049
1050 if (crc_eof)
1051 put_page(crc_eof);
1052}
1053
1054/**
1055 * fcoe_cpu_callback() - fcoe cpu hotplug event callback
1056 * @nfb: callback data block
1057 * @action: event triggering the callback
1058 * @hcpu: index for the cpu of this event
1059 *
1060 * This creates or destroys per cpu data for fcoe
1061 *
1062 * Returns NOTIFY_OK always.
1063 */
1064static int fcoe_cpu_callback(struct notifier_block *nfb,
1065 unsigned long action, void *hcpu)
1066{
1067 unsigned cpu = (unsigned long)hcpu;
1068
1069 switch (action) {
1070 case CPU_ONLINE:
1071 case CPU_ONLINE_FROZEN:
Robert Loved5488eb2009-06-10 15:30:59 -07001072 FCOE_DBG("CPU %x online: Create Rx thread\n", cpu);
Robert Love8976f422009-03-17 11:41:46 -07001073 fcoe_percpu_thread_create(cpu);
1074 break;
1075 case CPU_DEAD:
1076 case CPU_DEAD_FROZEN:
Robert Loved5488eb2009-06-10 15:30:59 -07001077 FCOE_DBG("CPU %x offline: Remove Rx thread\n", cpu);
Robert Love8976f422009-03-17 11:41:46 -07001078 fcoe_percpu_thread_destroy(cpu);
1079 break;
1080 default:
1081 break;
1082 }
1083 return NOTIFY_OK;
1084}
1085
1086static struct notifier_block fcoe_cpu_notifier = {
1087 .notifier_call = fcoe_cpu_callback,
1088};
1089
1090/**
Robert Love34f42a02009-02-27 10:55:45 -08001091 * fcoe_rcv() - this is the fcoe receive function called by NET_RX_SOFTIRQ
Robert Love85b4aa42008-12-09 15:10:24 -08001092 * @skb: the receive skb
1093 * @dev: associated net device
1094 * @ptype: context
Chris Leechdd3fd722009-04-21 16:27:36 -07001095 * @olddev: last device
Robert Love85b4aa42008-12-09 15:10:24 -08001096 *
1097 * this function will receive the packet and build fc frame and pass it up
1098 *
1099 * Returns: 0 for success
Robert Love34f42a02009-02-27 10:55:45 -08001100 */
Robert Love85b4aa42008-12-09 15:10:24 -08001101int fcoe_rcv(struct sk_buff *skb, struct net_device *dev,
1102 struct packet_type *ptype, struct net_device *olddev)
1103{
1104 struct fc_lport *lp;
1105 struct fcoe_rcv_info *fr;
Chris Leech259ad852009-08-25 13:59:41 -07001106 struct fcoe_interface *fcoe;
Robert Love85b4aa42008-12-09 15:10:24 -08001107 struct fc_frame_header *fh;
Robert Love85b4aa42008-12-09 15:10:24 -08001108 struct fcoe_percpu_s *fps;
Vasu Devb2f00912009-08-25 13:58:53 -07001109 unsigned int cpu;
Robert Love85b4aa42008-12-09 15:10:24 -08001110
Chris Leech259ad852009-08-25 13:59:41 -07001111 fcoe = container_of(ptype, struct fcoe_interface, fcoe_packet_type);
Chris Leech3fe9a0b2009-08-25 13:59:46 -07001112 lp = fcoe->ctlr.lp;
Robert Love85b4aa42008-12-09 15:10:24 -08001113 if (unlikely(lp == NULL)) {
Robert Loved5488eb2009-06-10 15:30:59 -07001114 FCOE_NETDEV_DBG(dev, "Cannot find hba structure");
Robert Love85b4aa42008-12-09 15:10:24 -08001115 goto err2;
1116 }
Joe Eykholt97c83892009-03-17 11:42:40 -07001117 if (!lp->link_up)
1118 goto err2;
Robert Love85b4aa42008-12-09 15:10:24 -08001119
Robert Loved5488eb2009-06-10 15:30:59 -07001120 FCOE_NETDEV_DBG(dev, "skb_info: len:%d data_len:%d head:%p "
1121 "data:%p tail:%p end:%p sum:%d dev:%s",
1122 skb->len, skb->data_len, skb->head, skb->data,
1123 skb_tail_pointer(skb), skb_end_pointer(skb),
1124 skb->csum, skb->dev ? skb->dev->name : "<NULL>");
Robert Love85b4aa42008-12-09 15:10:24 -08001125
1126 /* check for FCOE packet type */
1127 if (unlikely(eth_hdr(skb)->h_proto != htons(ETH_P_FCOE))) {
Robert Loved5488eb2009-06-10 15:30:59 -07001128 FCOE_NETDEV_DBG(dev, "Wrong FC type frame");
Robert Love85b4aa42008-12-09 15:10:24 -08001129 goto err;
1130 }
1131
1132 /*
1133 * Check for minimum frame length, and make sure required FCoE
1134 * and FC headers are pulled into the linear data area.
1135 */
1136 if (unlikely((skb->len < FCOE_MIN_FRAME) ||
1137 !pskb_may_pull(skb, FCOE_HEADER_LEN)))
1138 goto err;
1139
1140 skb_set_transport_header(skb, sizeof(struct fcoe_hdr));
1141 fh = (struct fc_frame_header *) skb_transport_header(skb);
1142
Robert Love85b4aa42008-12-09 15:10:24 -08001143 fr = fcoe_dev_from_skb(skb);
1144 fr->fr_dev = lp;
1145 fr->ptype = ptype;
Robert Love5e5e92d2009-03-17 11:41:35 -07001146
Robert Love85b4aa42008-12-09 15:10:24 -08001147 /*
Vasu Devb2f00912009-08-25 13:58:53 -07001148 * In case the incoming frame's exchange is originated from
1149 * the initiator, then received frame's exchange id is ANDed
1150 * with fc_cpu_mask bits to get the same cpu on which exchange
1151 * was originated, otherwise just use the current cpu.
Robert Love85b4aa42008-12-09 15:10:24 -08001152 */
Vasu Devb2f00912009-08-25 13:58:53 -07001153 if (ntoh24(fh->fh_f_ctl) & FC_FC_EX_CTX)
1154 cpu = ntohs(fh->fh_ox_id) & fc_cpu_mask;
1155 else
1156 cpu = smp_processor_id();
Robert Love5e5e92d2009-03-17 11:41:35 -07001157
Robert Love8976f422009-03-17 11:41:46 -07001158 fps = &per_cpu(fcoe_percpu, cpu);
Robert Love85b4aa42008-12-09 15:10:24 -08001159 spin_lock_bh(&fps->fcoe_rx_list.lock);
Robert Love8976f422009-03-17 11:41:46 -07001160 if (unlikely(!fps->thread)) {
1161 /*
1162 * The targeted CPU is not ready, let's target
1163 * the first CPU now. For non-SMP systems this
1164 * will check the same CPU twice.
1165 */
Robert Loved5488eb2009-06-10 15:30:59 -07001166 FCOE_NETDEV_DBG(dev, "CPU is online, but no receive thread "
1167 "ready for incoming skb- using first online "
1168 "CPU.\n");
Robert Love8976f422009-03-17 11:41:46 -07001169
1170 spin_unlock_bh(&fps->fcoe_rx_list.lock);
1171 cpu = first_cpu(cpu_online_map);
1172 fps = &per_cpu(fcoe_percpu, cpu);
1173 spin_lock_bh(&fps->fcoe_rx_list.lock);
1174 if (!fps->thread) {
1175 spin_unlock_bh(&fps->fcoe_rx_list.lock);
1176 goto err;
1177 }
1178 }
1179
1180 /*
1181 * We now have a valid CPU that we're targeting for
1182 * this skb. We also have this receive thread locked,
1183 * so we're free to queue skbs into it's queue.
1184 */
Robert Love85b4aa42008-12-09 15:10:24 -08001185 __skb_queue_tail(&fps->fcoe_rx_list, skb);
1186 if (fps->fcoe_rx_list.qlen == 1)
1187 wake_up_process(fps->thread);
1188
1189 spin_unlock_bh(&fps->fcoe_rx_list.lock);
1190
1191 return 0;
1192err:
Robert Love582b45b2009-03-31 15:51:50 -07001193 fc_lport_get_stats(lp)->ErrorFrames++;
Robert Love85b4aa42008-12-09 15:10:24 -08001194
1195err2:
1196 kfree_skb(skb);
1197 return -1;
1198}
Robert Love85b4aa42008-12-09 15:10:24 -08001199
1200/**
Robert Love34f42a02009-02-27 10:55:45 -08001201 * fcoe_start_io() - pass to netdev to start xmit for fcoe
Robert Love85b4aa42008-12-09 15:10:24 -08001202 * @skb: the skb to be xmitted
1203 *
1204 * Returns: 0 for success
Robert Love34f42a02009-02-27 10:55:45 -08001205 */
Robert Love85b4aa42008-12-09 15:10:24 -08001206static inline int fcoe_start_io(struct sk_buff *skb)
1207{
1208 int rc;
1209
1210 skb_get(skb);
1211 rc = dev_queue_xmit(skb);
1212 if (rc != 0)
1213 return rc;
1214 kfree_skb(skb);
1215 return 0;
1216}
1217
1218/**
Chris Leechdd3fd722009-04-21 16:27:36 -07001219 * fcoe_get_paged_crc_eof() - in case we need to alloc a page for crc_eof
Robert Love85b4aa42008-12-09 15:10:24 -08001220 * @skb: the skb to be xmitted
1221 * @tlen: total len
1222 *
1223 * Returns: 0 for success
Robert Love34f42a02009-02-27 10:55:45 -08001224 */
Robert Love85b4aa42008-12-09 15:10:24 -08001225static int fcoe_get_paged_crc_eof(struct sk_buff *skb, int tlen)
1226{
1227 struct fcoe_percpu_s *fps;
1228 struct page *page;
Robert Love85b4aa42008-12-09 15:10:24 -08001229
Robert Love5e5e92d2009-03-17 11:41:35 -07001230 fps = &get_cpu_var(fcoe_percpu);
Robert Love85b4aa42008-12-09 15:10:24 -08001231 page = fps->crc_eof_page;
1232 if (!page) {
1233 page = alloc_page(GFP_ATOMIC);
1234 if (!page) {
Robert Love5e5e92d2009-03-17 11:41:35 -07001235 put_cpu_var(fcoe_percpu);
Robert Love85b4aa42008-12-09 15:10:24 -08001236 return -ENOMEM;
1237 }
1238 fps->crc_eof_page = page;
Robert Love8976f422009-03-17 11:41:46 -07001239 fps->crc_eof_offset = 0;
Robert Love85b4aa42008-12-09 15:10:24 -08001240 }
1241
1242 get_page(page);
1243 skb_fill_page_desc(skb, skb_shinfo(skb)->nr_frags, page,
1244 fps->crc_eof_offset, tlen);
1245 skb->len += tlen;
1246 skb->data_len += tlen;
1247 skb->truesize += tlen;
1248 fps->crc_eof_offset += sizeof(struct fcoe_crc_eof);
1249
1250 if (fps->crc_eof_offset >= PAGE_SIZE) {
1251 fps->crc_eof_page = NULL;
1252 fps->crc_eof_offset = 0;
1253 put_page(page);
1254 }
Robert Love5e5e92d2009-03-17 11:41:35 -07001255 put_cpu_var(fcoe_percpu);
Robert Love85b4aa42008-12-09 15:10:24 -08001256 return 0;
1257}
1258
1259/**
Robert Love34f42a02009-02-27 10:55:45 -08001260 * fcoe_fc_crc() - calculates FC CRC in this fcoe skb
Chris Leechdd3fd722009-04-21 16:27:36 -07001261 * @fp: the fc_frame containing data to be checksummed
Robert Love85b4aa42008-12-09 15:10:24 -08001262 *
Chris Leech014f5c32009-08-25 13:59:30 -07001263 * This uses crc32() to calculate the crc for port frame
Robert Love85b4aa42008-12-09 15:10:24 -08001264 * Return : 32 bit crc
Robert Love34f42a02009-02-27 10:55:45 -08001265 */
Robert Love85b4aa42008-12-09 15:10:24 -08001266u32 fcoe_fc_crc(struct fc_frame *fp)
1267{
1268 struct sk_buff *skb = fp_skb(fp);
1269 struct skb_frag_struct *frag;
1270 unsigned char *data;
1271 unsigned long off, len, clen;
1272 u32 crc;
1273 unsigned i;
1274
1275 crc = crc32(~0, skb->data, skb_headlen(skb));
1276
1277 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
1278 frag = &skb_shinfo(skb)->frags[i];
1279 off = frag->page_offset;
1280 len = frag->size;
1281 while (len > 0) {
1282 clen = min(len, PAGE_SIZE - (off & ~PAGE_MASK));
1283 data = kmap_atomic(frag->page + (off >> PAGE_SHIFT),
1284 KM_SKB_DATA_SOFTIRQ);
1285 crc = crc32(crc, data + (off & ~PAGE_MASK), clen);
1286 kunmap_atomic(data, KM_SKB_DATA_SOFTIRQ);
1287 off += clen;
1288 len -= clen;
1289 }
1290 }
1291 return crc;
1292}
Robert Love85b4aa42008-12-09 15:10:24 -08001293
1294/**
Robert Love34f42a02009-02-27 10:55:45 -08001295 * fcoe_xmit() - FCoE frame transmit function
Chris Leech014f5c32009-08-25 13:59:30 -07001296 * @lp: the associated local fcoe
Robert Love85b4aa42008-12-09 15:10:24 -08001297 * @fp: the fc_frame to be transmitted
1298 *
1299 * Return : 0 for success
Robert Love34f42a02009-02-27 10:55:45 -08001300 */
Robert Love85b4aa42008-12-09 15:10:24 -08001301int fcoe_xmit(struct fc_lport *lp, struct fc_frame *fp)
1302{
Vasu Dev4bb6b512009-05-06 10:52:34 -07001303 int wlen;
Robert Love85b4aa42008-12-09 15:10:24 -08001304 u32 crc;
1305 struct ethhdr *eh;
1306 struct fcoe_crc_eof *cp;
1307 struct sk_buff *skb;
1308 struct fcoe_dev_stats *stats;
1309 struct fc_frame_header *fh;
1310 unsigned int hlen; /* header length implies the version */
1311 unsigned int tlen; /* trailer length */
1312 unsigned int elen; /* eth header, may include vlan */
Chris Leech3fe9a0b2009-08-25 13:59:46 -07001313 struct fcoe_port *port = lport_priv(lp);
1314 struct fcoe_interface *fcoe = port->fcoe;
Robert Love85b4aa42008-12-09 15:10:24 -08001315 u8 sof, eof;
1316 struct fcoe_hdr *hp;
1317
1318 WARN_ON((fr_len(fp) % sizeof(u32)) != 0);
1319
Robert Love85b4aa42008-12-09 15:10:24 -08001320 fh = fc_frame_header_get(fp);
Joe Eykholt97c83892009-03-17 11:42:40 -07001321 skb = fp_skb(fp);
1322 wlen = skb->len / FCOE_WORD_TO_BYTE;
1323
1324 if (!lp->link_up) {
Dan Carpenter3caf02e2009-04-21 16:27:25 -07001325 kfree_skb(skb);
Joe Eykholt97c83892009-03-17 11:42:40 -07001326 return 0;
Robert Love85b4aa42008-12-09 15:10:24 -08001327 }
1328
Joe Eykholt97c83892009-03-17 11:42:40 -07001329 if (unlikely(fh->fh_r_ctl == FC_RCTL_ELS_REQ) &&
Chris Leech11b56182009-11-03 11:46:29 -08001330 fcoe_ctlr_els_send(&fcoe->ctlr, lp, skb))
Joe Eykholt97c83892009-03-17 11:42:40 -07001331 return 0;
1332
Robert Love85b4aa42008-12-09 15:10:24 -08001333 sof = fr_sof(fp);
1334 eof = fr_eof(fp);
1335
Vasu Dev4e57e1c2009-05-06 10:52:46 -07001336 elen = sizeof(struct ethhdr);
Robert Love85b4aa42008-12-09 15:10:24 -08001337 hlen = sizeof(struct fcoe_hdr);
1338 tlen = sizeof(struct fcoe_crc_eof);
1339 wlen = (skb->len - tlen + sizeof(crc)) / FCOE_WORD_TO_BYTE;
1340
1341 /* crc offload */
1342 if (likely(lp->crc_offload)) {
Yi Zou39ca9a02009-02-27 14:07:15 -08001343 skb->ip_summed = CHECKSUM_PARTIAL;
Robert Love85b4aa42008-12-09 15:10:24 -08001344 skb->csum_start = skb_headroom(skb);
1345 skb->csum_offset = skb->len;
1346 crc = 0;
1347 } else {
1348 skb->ip_summed = CHECKSUM_NONE;
1349 crc = fcoe_fc_crc(fp);
1350 }
1351
Chris Leech014f5c32009-08-25 13:59:30 -07001352 /* copy port crc and eof to the skb buff */
Robert Love85b4aa42008-12-09 15:10:24 -08001353 if (skb_is_nonlinear(skb)) {
1354 skb_frag_t *frag;
1355 if (fcoe_get_paged_crc_eof(skb, tlen)) {
Roel Kluine9041582009-02-27 10:56:22 -08001356 kfree_skb(skb);
Robert Love85b4aa42008-12-09 15:10:24 -08001357 return -ENOMEM;
1358 }
1359 frag = &skb_shinfo(skb)->frags[skb_shinfo(skb)->nr_frags - 1];
1360 cp = kmap_atomic(frag->page, KM_SKB_DATA_SOFTIRQ)
1361 + frag->page_offset;
1362 } else {
1363 cp = (struct fcoe_crc_eof *)skb_put(skb, tlen);
1364 }
1365
1366 memset(cp, 0, sizeof(*cp));
1367 cp->fcoe_eof = eof;
1368 cp->fcoe_crc32 = cpu_to_le32(~crc);
1369
1370 if (skb_is_nonlinear(skb)) {
1371 kunmap_atomic(cp, KM_SKB_DATA_SOFTIRQ);
1372 cp = NULL;
1373 }
1374
Chris Leech014f5c32009-08-25 13:59:30 -07001375 /* adjust skb network/transport offsets to match mac/fcoe/port */
Robert Love85b4aa42008-12-09 15:10:24 -08001376 skb_push(skb, elen + hlen);
1377 skb_reset_mac_header(skb);
1378 skb_reset_network_header(skb);
1379 skb->mac_len = elen;
Yi Zou211c7382009-02-27 14:06:37 -08001380 skb->protocol = htons(ETH_P_FCOE);
Chris Leech3fe9a0b2009-08-25 13:59:46 -07001381 skb->dev = fcoe->netdev;
Robert Love85b4aa42008-12-09 15:10:24 -08001382
1383 /* fill up mac and fcoe headers */
1384 eh = eth_hdr(skb);
1385 eh->h_proto = htons(ETH_P_FCOE);
Chris Leech3fe9a0b2009-08-25 13:59:46 -07001386 if (fcoe->ctlr.map_dest)
Robert Love85b4aa42008-12-09 15:10:24 -08001387 fc_fcoe_set_mac(eh->h_dest, fh->fh_d_id);
1388 else
1389 /* insert GW address */
Chris Leech3fe9a0b2009-08-25 13:59:46 -07001390 memcpy(eh->h_dest, fcoe->ctlr.dest_addr, ETH_ALEN);
Robert Love85b4aa42008-12-09 15:10:24 -08001391
Chris Leech3fe9a0b2009-08-25 13:59:46 -07001392 if (unlikely(fcoe->ctlr.flogi_oxid != FC_XID_UNKNOWN))
1393 memcpy(eh->h_source, fcoe->ctlr.ctl_src_addr, ETH_ALEN);
Robert Love85b4aa42008-12-09 15:10:24 -08001394 else
Chris Leech11b56182009-11-03 11:46:29 -08001395 memcpy(eh->h_source, port->data_src_addr, ETH_ALEN);
Robert Love85b4aa42008-12-09 15:10:24 -08001396
1397 hp = (struct fcoe_hdr *)(eh + 1);
1398 memset(hp, 0, sizeof(*hp));
1399 if (FC_FCOE_VER)
1400 FC_FCOE_ENCAPS_VER(hp, FC_FCOE_VER);
1401 hp->fcoe_sof = sof;
1402
Yi Zou39ca9a02009-02-27 14:07:15 -08001403 /* fcoe lso, mss is in max_payload which is non-zero for FCP data */
1404 if (lp->seq_offload && fr_max_payload(fp)) {
1405 skb_shinfo(skb)->gso_type = SKB_GSO_FCOE;
1406 skb_shinfo(skb)->gso_size = fr_max_payload(fp);
1407 } else {
1408 skb_shinfo(skb)->gso_type = 0;
1409 skb_shinfo(skb)->gso_size = 0;
1410 }
Robert Love85b4aa42008-12-09 15:10:24 -08001411 /* update tx stats: regardless if LLD fails */
Robert Love582b45b2009-03-31 15:51:50 -07001412 stats = fc_lport_get_stats(lp);
1413 stats->TxFrames++;
1414 stats->TxWords += wlen;
Robert Love85b4aa42008-12-09 15:10:24 -08001415
1416 /* send down to lld */
1417 fr_dev(fp) = lp;
Chris Leech014f5c32009-08-25 13:59:30 -07001418 if (port->fcoe_pending_queue.qlen)
Vasu Dev4bb6b512009-05-06 10:52:34 -07001419 fcoe_check_wait_queue(lp, skb);
1420 else if (fcoe_start_io(skb))
1421 fcoe_check_wait_queue(lp, skb);
Robert Love85b4aa42008-12-09 15:10:24 -08001422
1423 return 0;
1424}
Robert Love85b4aa42008-12-09 15:10:24 -08001425
Robert Love34f42a02009-02-27 10:55:45 -08001426/**
Joe Eykholte7a51992009-08-25 14:04:08 -07001427 * fcoe_percpu_flush_done() - Indicate percpu queue flush completion.
1428 * @skb: the skb being completed.
1429 */
1430static void fcoe_percpu_flush_done(struct sk_buff *skb)
1431{
1432 complete(&fcoe_flush_completion);
1433}
1434
1435/**
Robert Love34f42a02009-02-27 10:55:45 -08001436 * fcoe_percpu_receive_thread() - recv thread per cpu
Robert Love85b4aa42008-12-09 15:10:24 -08001437 * @arg: ptr to the fcoe per cpu struct
1438 *
1439 * Return: 0 for success
Robert Love85b4aa42008-12-09 15:10:24 -08001440 */
1441int fcoe_percpu_receive_thread(void *arg)
1442{
1443 struct fcoe_percpu_s *p = arg;
1444 u32 fr_len;
1445 struct fc_lport *lp;
1446 struct fcoe_rcv_info *fr;
1447 struct fcoe_dev_stats *stats;
1448 struct fc_frame_header *fh;
1449 struct sk_buff *skb;
1450 struct fcoe_crc_eof crc_eof;
1451 struct fc_frame *fp;
1452 u8 *mac = NULL;
Chris Leech014f5c32009-08-25 13:59:30 -07001453 struct fcoe_port *port;
Robert Love85b4aa42008-12-09 15:10:24 -08001454 struct fcoe_hdr *hp;
1455
Robert Love4469c192009-02-27 10:56:38 -08001456 set_user_nice(current, -20);
Robert Love85b4aa42008-12-09 15:10:24 -08001457
1458 while (!kthread_should_stop()) {
1459
1460 spin_lock_bh(&p->fcoe_rx_list.lock);
1461 while ((skb = __skb_dequeue(&p->fcoe_rx_list)) == NULL) {
1462 set_current_state(TASK_INTERRUPTIBLE);
1463 spin_unlock_bh(&p->fcoe_rx_list.lock);
1464 schedule();
1465 set_current_state(TASK_RUNNING);
1466 if (kthread_should_stop())
1467 return 0;
1468 spin_lock_bh(&p->fcoe_rx_list.lock);
1469 }
1470 spin_unlock_bh(&p->fcoe_rx_list.lock);
1471 fr = fcoe_dev_from_skb(skb);
1472 lp = fr->fr_dev;
1473 if (unlikely(lp == NULL)) {
Joe Eykholte7a51992009-08-25 14:04:08 -07001474 if (skb->destructor != fcoe_percpu_flush_done)
1475 FCOE_NETDEV_DBG(skb->dev, "NULL lport in skb");
Robert Love85b4aa42008-12-09 15:10:24 -08001476 kfree_skb(skb);
1477 continue;
1478 }
1479
Robert Loved5488eb2009-06-10 15:30:59 -07001480 FCOE_NETDEV_DBG(skb->dev, "skb_info: len:%d data_len:%d "
1481 "head:%p data:%p tail:%p end:%p sum:%d dev:%s",
1482 skb->len, skb->data_len,
1483 skb->head, skb->data, skb_tail_pointer(skb),
1484 skb_end_pointer(skb), skb->csum,
1485 skb->dev ? skb->dev->name : "<NULL>");
Robert Love85b4aa42008-12-09 15:10:24 -08001486
1487 /*
1488 * Save source MAC address before discarding header.
1489 */
Chris Leech014f5c32009-08-25 13:59:30 -07001490 port = lport_priv(lp);
Robert Love85b4aa42008-12-09 15:10:24 -08001491 if (skb_is_nonlinear(skb))
1492 skb_linearize(skb); /* not ideal */
Joe Eykholt97c83892009-03-17 11:42:40 -07001493 mac = eth_hdr(skb)->h_source;
Robert Love85b4aa42008-12-09 15:10:24 -08001494
1495 /*
1496 * Frame length checks and setting up the header pointers
1497 * was done in fcoe_rcv already.
1498 */
1499 hp = (struct fcoe_hdr *) skb_network_header(skb);
1500 fh = (struct fc_frame_header *) skb_transport_header(skb);
1501
Robert Love582b45b2009-03-31 15:51:50 -07001502 stats = fc_lport_get_stats(lp);
Robert Love85b4aa42008-12-09 15:10:24 -08001503 if (unlikely(FC_FCOE_DECAPS_VER(hp) != FC_FCOE_VER)) {
Robert Love582b45b2009-03-31 15:51:50 -07001504 if (stats->ErrorFrames < 5)
Robert Loved5488eb2009-06-10 15:30:59 -07001505 printk(KERN_WARNING "fcoe: FCoE version "
Robert Love582b45b2009-03-31 15:51:50 -07001506 "mismatch: The frame has "
1507 "version %x, but the "
1508 "initiator supports version "
1509 "%x\n", FC_FCOE_DECAPS_VER(hp),
1510 FC_FCOE_VER);
1511 stats->ErrorFrames++;
Robert Love85b4aa42008-12-09 15:10:24 -08001512 kfree_skb(skb);
1513 continue;
1514 }
1515
1516 skb_pull(skb, sizeof(struct fcoe_hdr));
1517 fr_len = skb->len - sizeof(struct fcoe_crc_eof);
1518
Robert Love582b45b2009-03-31 15:51:50 -07001519 stats->RxFrames++;
1520 stats->RxWords += fr_len / FCOE_WORD_TO_BYTE;
Robert Love85b4aa42008-12-09 15:10:24 -08001521
1522 fp = (struct fc_frame *)skb;
1523 fc_frame_init(fp);
1524 fr_dev(fp) = lp;
1525 fr_sof(fp) = hp->fcoe_sof;
1526
1527 /* Copy out the CRC and EOF trailer for access */
1528 if (skb_copy_bits(skb, fr_len, &crc_eof, sizeof(crc_eof))) {
1529 kfree_skb(skb);
1530 continue;
1531 }
1532 fr_eof(fp) = crc_eof.fcoe_eof;
1533 fr_crc(fp) = crc_eof.fcoe_crc32;
1534 if (pskb_trim(skb, fr_len)) {
1535 kfree_skb(skb);
1536 continue;
1537 }
1538
1539 /*
1540 * We only check CRC if no offload is available and if it is
1541 * it's solicited data, in which case, the FCP layer would
1542 * check it during the copy.
1543 */
Yi Zou07c00ec2009-02-27 14:07:31 -08001544 if (lp->crc_offload && skb->ip_summed == CHECKSUM_UNNECESSARY)
Robert Love85b4aa42008-12-09 15:10:24 -08001545 fr_flags(fp) &= ~FCPHF_CRC_UNCHECKED;
1546 else
1547 fr_flags(fp) |= FCPHF_CRC_UNCHECKED;
1548
1549 fh = fc_frame_header_get(fp);
1550 if (fh->fh_r_ctl == FC_RCTL_DD_SOL_DATA &&
1551 fh->fh_type == FC_TYPE_FCP) {
Vasu Dev52ff8782009-07-29 17:05:10 -07001552 fc_exch_recv(lp, fp);
Robert Love85b4aa42008-12-09 15:10:24 -08001553 continue;
1554 }
1555 if (fr_flags(fp) & FCPHF_CRC_UNCHECKED) {
1556 if (le32_to_cpu(fr_crc(fp)) !=
1557 ~crc32(~0, skb->data, fr_len)) {
Robert Loved5488eb2009-06-10 15:30:59 -07001558 if (stats->InvalidCRCCount < 5)
Robert Love85b4aa42008-12-09 15:10:24 -08001559 printk(KERN_WARNING "fcoe: dropping "
1560 "frame with CRC error\n");
1561 stats->InvalidCRCCount++;
1562 stats->ErrorFrames++;
1563 fc_frame_free(fp);
1564 continue;
1565 }
1566 fr_flags(fp) &= ~FCPHF_CRC_UNCHECKED;
1567 }
Vasu Dev52ff8782009-07-29 17:05:10 -07001568 fc_exch_recv(lp, fp);
Robert Love85b4aa42008-12-09 15:10:24 -08001569 }
1570 return 0;
1571}
1572
1573/**
Chris Leechdd3fd722009-04-21 16:27:36 -07001574 * fcoe_check_wait_queue() - attempt to clear the transmit backlog
1575 * @lp: the fc_lport
Robert Love85b4aa42008-12-09 15:10:24 -08001576 *
1577 * This empties the wait_queue, dequeue the head of the wait_queue queue
1578 * and calls fcoe_start_io() for each packet, if all skb have been
Vasu Devc826a312009-02-27 10:56:27 -08001579 * transmitted, return qlen or -1 if a error occurs, then restore
Chris Leechdd3fd722009-04-21 16:27:36 -07001580 * wait_queue and try again later.
Robert Love85b4aa42008-12-09 15:10:24 -08001581 *
1582 * The wait_queue is used when the skb transmit fails. skb will go
Chris Leechdd3fd722009-04-21 16:27:36 -07001583 * in the wait_queue which will be emptied by the timer function or
Robert Love85b4aa42008-12-09 15:10:24 -08001584 * by the next skb transmit.
Robert Love34f42a02009-02-27 10:55:45 -08001585 */
Vasu Dev4bb6b512009-05-06 10:52:34 -07001586static void fcoe_check_wait_queue(struct fc_lport *lp, struct sk_buff *skb)
Robert Love85b4aa42008-12-09 15:10:24 -08001587{
Chris Leech014f5c32009-08-25 13:59:30 -07001588 struct fcoe_port *port = lport_priv(lp);
Vasu Dev4bb6b512009-05-06 10:52:34 -07001589 int rc;
Robert Love85b4aa42008-12-09 15:10:24 -08001590
Chris Leech014f5c32009-08-25 13:59:30 -07001591 spin_lock_bh(&port->fcoe_pending_queue.lock);
Vasu Dev4bb6b512009-05-06 10:52:34 -07001592
1593 if (skb)
Chris Leech014f5c32009-08-25 13:59:30 -07001594 __skb_queue_tail(&port->fcoe_pending_queue, skb);
Vasu Dev4bb6b512009-05-06 10:52:34 -07001595
Chris Leech014f5c32009-08-25 13:59:30 -07001596 if (port->fcoe_pending_queue_active)
Vasu Devc826a312009-02-27 10:56:27 -08001597 goto out;
Chris Leech014f5c32009-08-25 13:59:30 -07001598 port->fcoe_pending_queue_active = 1;
Chris Leech55c8baf2009-02-27 10:56:32 -08001599
Chris Leech014f5c32009-08-25 13:59:30 -07001600 while (port->fcoe_pending_queue.qlen) {
Chris Leech55c8baf2009-02-27 10:56:32 -08001601 /* keep qlen > 0 until fcoe_start_io succeeds */
Chris Leech014f5c32009-08-25 13:59:30 -07001602 port->fcoe_pending_queue.qlen++;
1603 skb = __skb_dequeue(&port->fcoe_pending_queue);
Chris Leech55c8baf2009-02-27 10:56:32 -08001604
Chris Leech014f5c32009-08-25 13:59:30 -07001605 spin_unlock_bh(&port->fcoe_pending_queue.lock);
Chris Leech55c8baf2009-02-27 10:56:32 -08001606 rc = fcoe_start_io(skb);
Chris Leech014f5c32009-08-25 13:59:30 -07001607 spin_lock_bh(&port->fcoe_pending_queue.lock);
Chris Leech55c8baf2009-02-27 10:56:32 -08001608
1609 if (rc) {
Chris Leech014f5c32009-08-25 13:59:30 -07001610 __skb_queue_head(&port->fcoe_pending_queue, skb);
Chris Leech55c8baf2009-02-27 10:56:32 -08001611 /* undo temporary increment above */
Chris Leech014f5c32009-08-25 13:59:30 -07001612 port->fcoe_pending_queue.qlen--;
Chris Leech55c8baf2009-02-27 10:56:32 -08001613 break;
Robert Love85b4aa42008-12-09 15:10:24 -08001614 }
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--;
Robert Love85b4aa42008-12-09 15:10:24 -08001617 }
Chris Leech55c8baf2009-02-27 10:56:32 -08001618
Chris Leech014f5c32009-08-25 13:59:30 -07001619 if (port->fcoe_pending_queue.qlen < FCOE_LOW_QUEUE_DEPTH)
Chris Leech55c8baf2009-02-27 10:56:32 -08001620 lp->qfull = 0;
Chris Leech014f5c32009-08-25 13:59:30 -07001621 if (port->fcoe_pending_queue.qlen && !timer_pending(&port->timer))
1622 mod_timer(&port->timer, jiffies + 2);
1623 port->fcoe_pending_queue_active = 0;
Vasu Devc826a312009-02-27 10:56:27 -08001624out:
Chris Leech014f5c32009-08-25 13:59:30 -07001625 if (port->fcoe_pending_queue.qlen > FCOE_MAX_QUEUE_DEPTH)
Vasu Dev4bb6b512009-05-06 10:52:34 -07001626 lp->qfull = 1;
Chris Leech014f5c32009-08-25 13:59:30 -07001627 spin_unlock_bh(&port->fcoe_pending_queue.lock);
Vasu Dev4bb6b512009-05-06 10:52:34 -07001628 return;
Robert Love85b4aa42008-12-09 15:10:24 -08001629}
1630
1631/**
Robert Love34f42a02009-02-27 10:55:45 -08001632 * fcoe_dev_setup() - setup link change notification interface
1633 */
Randy Dunlapb0d428a2009-04-27 21:49:31 -07001634static void fcoe_dev_setup(void)
Robert Love85b4aa42008-12-09 15:10:24 -08001635{
Robert Love85b4aa42008-12-09 15:10:24 -08001636 register_netdevice_notifier(&fcoe_notifier);
1637}
1638
1639/**
Randy Dunlapb0d428a2009-04-27 21:49:31 -07001640 * fcoe_dev_cleanup() - cleanup link change notification interface
Robert Love34f42a02009-02-27 10:55:45 -08001641 */
Robert Love85b4aa42008-12-09 15:10:24 -08001642static void fcoe_dev_cleanup(void)
1643{
1644 unregister_netdevice_notifier(&fcoe_notifier);
1645}
1646
1647/**
Robert Love34f42a02009-02-27 10:55:45 -08001648 * fcoe_device_notification() - netdev event notification callback
Robert Love85b4aa42008-12-09 15:10:24 -08001649 * @notifier: context of the notification
1650 * @event: type of event
1651 * @ptr: fixed array for output parsed ifname
1652 *
1653 * This function is called by the ethernet driver in case of link change event
1654 *
1655 * Returns: 0 for success
Robert Love34f42a02009-02-27 10:55:45 -08001656 */
Robert Love85b4aa42008-12-09 15:10:24 -08001657static int fcoe_device_notification(struct notifier_block *notifier,
1658 ulong event, void *ptr)
1659{
1660 struct fc_lport *lp = NULL;
Vasu Dev1d1b88d2009-07-29 17:05:45 -07001661 struct net_device *netdev = ptr;
Chris Leech014f5c32009-08-25 13:59:30 -07001662 struct fcoe_interface *fcoe;
Chris Leech2e70e242009-08-25 14:00:23 -07001663 struct fcoe_port *port;
Robert Love85b4aa42008-12-09 15:10:24 -08001664 struct fcoe_dev_stats *stats;
Joe Eykholt97c83892009-03-17 11:42:40 -07001665 u32 link_possible = 1;
Robert Love85b4aa42008-12-09 15:10:24 -08001666 u32 mfs;
1667 int rc = NOTIFY_OK;
1668
Chris Leech014f5c32009-08-25 13:59:30 -07001669 list_for_each_entry(fcoe, &fcoe_hostlist, list) {
Chris Leech25024982009-08-25 13:59:35 -07001670 if (fcoe->netdev == netdev) {
Chris Leech3fe9a0b2009-08-25 13:59:46 -07001671 lp = fcoe->ctlr.lp;
Robert Love85b4aa42008-12-09 15:10:24 -08001672 break;
1673 }
1674 }
Robert Love85b4aa42008-12-09 15:10:24 -08001675 if (lp == NULL) {
1676 rc = NOTIFY_DONE;
1677 goto out;
1678 }
1679
Robert Love85b4aa42008-12-09 15:10:24 -08001680 switch (event) {
1681 case NETDEV_DOWN:
1682 case NETDEV_GOING_DOWN:
Joe Eykholt97c83892009-03-17 11:42:40 -07001683 link_possible = 0;
Robert Love85b4aa42008-12-09 15:10:24 -08001684 break;
1685 case NETDEV_UP:
1686 case NETDEV_CHANGE:
Robert Love85b4aa42008-12-09 15:10:24 -08001687 break;
1688 case NETDEV_CHANGEMTU:
Yi Zou7221d7e2009-10-21 16:27:52 -07001689 if (netdev->features & NETIF_F_FCOE_MTU)
1690 break;
Vasu Dev1d1b88d2009-07-29 17:05:45 -07001691 mfs = netdev->mtu - (sizeof(struct fcoe_hdr) +
1692 sizeof(struct fcoe_crc_eof));
Robert Love85b4aa42008-12-09 15:10:24 -08001693 if (mfs >= FC_MIN_MAX_FRAME)
1694 fc_set_mfs(lp, mfs);
Robert Love85b4aa42008-12-09 15:10:24 -08001695 break;
1696 case NETDEV_REGISTER:
1697 break;
Chris Leech2e70e242009-08-25 14:00:23 -07001698 case NETDEV_UNREGISTER:
1699 list_del(&fcoe->list);
1700 port = lport_priv(fcoe->ctlr.lp);
1701 fcoe_interface_cleanup(fcoe);
1702 schedule_work(&port->destroy_work);
1703 goto out;
1704 break;
Robert Love85b4aa42008-12-09 15:10:24 -08001705 default:
Vasu Dev1d1b88d2009-07-29 17:05:45 -07001706 FCOE_NETDEV_DBG(netdev, "Unknown event %ld "
Robert Loved5488eb2009-06-10 15:30:59 -07001707 "from netdev netlink\n", event);
Robert Love85b4aa42008-12-09 15:10:24 -08001708 }
Joe Eykholt97c83892009-03-17 11:42:40 -07001709 if (link_possible && !fcoe_link_ok(lp))
Chris Leech3fe9a0b2009-08-25 13:59:46 -07001710 fcoe_ctlr_link_up(&fcoe->ctlr);
1711 else if (fcoe_ctlr_link_down(&fcoe->ctlr)) {
Joe Eykholt97c83892009-03-17 11:42:40 -07001712 stats = fc_lport_get_stats(lp);
1713 stats->LinkFailureCount++;
1714 fcoe_clean_pending_queue(lp);
Robert Love85b4aa42008-12-09 15:10:24 -08001715 }
1716out:
1717 return rc;
1718}
1719
1720/**
Robert Love34f42a02009-02-27 10:55:45 -08001721 * fcoe_if_to_netdev() - parse a name buffer to get netdev
Robert Love85b4aa42008-12-09 15:10:24 -08001722 * @buffer: incoming buffer to be copied
1723 *
Chris Leechdd3fd722009-04-21 16:27:36 -07001724 * Returns: NULL or ptr to net_device
Robert Love34f42a02009-02-27 10:55:45 -08001725 */
Robert Love85b4aa42008-12-09 15:10:24 -08001726static struct net_device *fcoe_if_to_netdev(const char *buffer)
1727{
1728 char *cp;
1729 char ifname[IFNAMSIZ + 2];
1730
1731 if (buffer) {
1732 strlcpy(ifname, buffer, IFNAMSIZ);
1733 cp = ifname + strlen(ifname);
1734 while (--cp >= ifname && *cp == '\n')
1735 *cp = '\0';
1736 return dev_get_by_name(&init_net, ifname);
1737 }
1738 return NULL;
1739}
1740
1741/**
Robert Love34f42a02009-02-27 10:55:45 -08001742 * fcoe_destroy() - handles the destroy from sysfs
Chris Leechdd3fd722009-04-21 16:27:36 -07001743 * @buffer: expected to be an eth if name
Robert Love85b4aa42008-12-09 15:10:24 -08001744 * @kp: associated kernel param
1745 *
1746 * Returns: 0 for success
Robert Love34f42a02009-02-27 10:55:45 -08001747 */
Robert Love85b4aa42008-12-09 15:10:24 -08001748static int fcoe_destroy(const char *buffer, struct kernel_param *kp)
1749{
Chris Leech030f4e02009-08-25 14:00:02 -07001750 struct fcoe_interface *fcoe;
Chris Leech2e70e242009-08-25 14:00:23 -07001751 struct net_device *netdev;
Mike Christie8eca3552009-10-21 16:27:44 -07001752 int rc = 0;
Robert Love85b4aa42008-12-09 15:10:24 -08001753
Chris Leechdfc1d0f2009-08-25 14:00:13 -07001754 mutex_lock(&fcoe_config_mutex);
1755#ifdef CONFIG_FCOE_MODULE
1756 /*
1757 * Make sure the module has been initialized, and is not about to be
1758 * removed. Module paramter sysfs files are writable before the
1759 * module_init function is called and after module_exit.
1760 */
1761 if (THIS_MODULE->state != MODULE_STATE_LIVE) {
1762 rc = -ENODEV;
1763 goto out_nodev;
1764 }
1765#endif
1766
Robert Love85b4aa42008-12-09 15:10:24 -08001767 netdev = fcoe_if_to_netdev(buffer);
1768 if (!netdev) {
1769 rc = -ENODEV;
1770 goto out_nodev;
1771 }
Chris Leech2e70e242009-08-25 14:00:23 -07001772
Chris Leech090eb6c2009-08-25 14:00:28 -07001773 rtnl_lock();
Chris Leech2e70e242009-08-25 14:00:23 -07001774 fcoe = fcoe_hostlist_lookup_port(netdev);
1775 if (!fcoe) {
Chris Leech090eb6c2009-08-25 14:00:28 -07001776 rtnl_unlock();
Robert Love85b4aa42008-12-09 15:10:24 -08001777 rc = -ENODEV;
1778 goto out_putdev;
1779 }
Chris Leech2e70e242009-08-25 14:00:23 -07001780 list_del(&fcoe->list);
Chris Leech2e70e242009-08-25 14:00:23 -07001781 fcoe_interface_cleanup(fcoe);
1782 rtnl_unlock();
1783 fcoe_if_destroy(fcoe->ctlr.lp);
Robert Love85b4aa42008-12-09 15:10:24 -08001784out_putdev:
1785 dev_put(netdev);
1786out_nodev:
Chris Leechdfc1d0f2009-08-25 14:00:13 -07001787 mutex_unlock(&fcoe_config_mutex);
Robert Love85b4aa42008-12-09 15:10:24 -08001788 return rc;
1789}
1790
Chris Leech2e70e242009-08-25 14:00:23 -07001791static void fcoe_destroy_work(struct work_struct *work)
1792{
1793 struct fcoe_port *port;
1794
1795 port = container_of(work, struct fcoe_port, destroy_work);
1796 mutex_lock(&fcoe_config_mutex);
1797 fcoe_if_destroy(port->lport);
1798 mutex_unlock(&fcoe_config_mutex);
1799}
1800
Robert Love85b4aa42008-12-09 15:10:24 -08001801/**
Robert Love34f42a02009-02-27 10:55:45 -08001802 * fcoe_create() - Handles the create call from sysfs
Chris Leechdd3fd722009-04-21 16:27:36 -07001803 * @buffer: expected to be an eth if name
Robert Love85b4aa42008-12-09 15:10:24 -08001804 * @kp: associated kernel param
1805 *
1806 * Returns: 0 for success
Robert Love34f42a02009-02-27 10:55:45 -08001807 */
Robert Love85b4aa42008-12-09 15:10:24 -08001808static int fcoe_create(const char *buffer, struct kernel_param *kp)
1809{
1810 int rc;
Chris Leech030f4e02009-08-25 14:00:02 -07001811 struct fcoe_interface *fcoe;
Chris Leechaf7f85d2009-08-25 13:59:24 -07001812 struct fc_lport *lport;
Robert Love85b4aa42008-12-09 15:10:24 -08001813 struct net_device *netdev;
1814
Chris Leechdfc1d0f2009-08-25 14:00:13 -07001815 mutex_lock(&fcoe_config_mutex);
1816#ifdef CONFIG_FCOE_MODULE
1817 /*
1818 * Make sure the module has been initialized, and is not about to be
1819 * removed. Module paramter sysfs files are writable before the
1820 * module_init function is called and after module_exit.
1821 */
1822 if (THIS_MODULE->state != MODULE_STATE_LIVE) {
1823 rc = -ENODEV;
1824 goto out_nodev;
1825 }
1826#endif
1827
Chris Leech2e70e242009-08-25 14:00:23 -07001828 rtnl_lock();
Robert Love85b4aa42008-12-09 15:10:24 -08001829 netdev = fcoe_if_to_netdev(buffer);
1830 if (!netdev) {
1831 rc = -ENODEV;
1832 goto out_nodev;
1833 }
Chris Leech2e70e242009-08-25 14:00:23 -07001834
Robert Love85b4aa42008-12-09 15:10:24 -08001835 /* look for existing lport */
1836 if (fcoe_hostlist_lookup(netdev)) {
1837 rc = -EEXIST;
1838 goto out_putdev;
1839 }
Robert Love85b4aa42008-12-09 15:10:24 -08001840
Chris Leech030f4e02009-08-25 14:00:02 -07001841 fcoe = fcoe_interface_create(netdev);
1842 if (!fcoe) {
1843 rc = -ENOMEM;
1844 goto out_putdev;
1845 }
1846
Chris Leech9a057532009-11-03 11:46:40 -08001847 lport = fcoe_if_create(fcoe, &netdev->dev, 0);
Chris Leechaf7f85d2009-08-25 13:59:24 -07001848 if (IS_ERR(lport)) {
Robert Loved5488eb2009-06-10 15:30:59 -07001849 printk(KERN_ERR "fcoe: Failed to create interface (%s)\n",
Robert Love85b4aa42008-12-09 15:10:24 -08001850 netdev->name);
Robert Love85b4aa42008-12-09 15:10:24 -08001851 rc = -EIO;
Chris Leech2e70e242009-08-25 14:00:23 -07001852 fcoe_interface_cleanup(fcoe);
Chris Leech030f4e02009-08-25 14:00:02 -07001853 goto out_free;
Robert Love85b4aa42008-12-09 15:10:24 -08001854 }
Chris Leech030f4e02009-08-25 14:00:02 -07001855
Chris Leech54b649f2009-08-25 14:00:07 -07001856 /* Make this the "master" N_Port */
1857 fcoe->ctlr.lp = lport;
Chris Leech030f4e02009-08-25 14:00:02 -07001858
Chris Leechc863df32009-08-25 14:00:18 -07001859 /* add to lports list */
1860 fcoe_hostlist_add(lport);
1861
Chris Leech54b649f2009-08-25 14:00:07 -07001862 /* start FIP Discovery and FLOGI */
1863 lport->boot_time = jiffies;
1864 fc_fabric_login(lport);
1865 if (!fcoe_link_ok(lport))
1866 fcoe_ctlr_link_up(&fcoe->ctlr);
1867
1868 rc = 0;
Chris Leech030f4e02009-08-25 14:00:02 -07001869out_free:
Chris Leech54b649f2009-08-25 14:00:07 -07001870 /*
1871 * Release from init in fcoe_interface_create(), on success lport
1872 * should be holding a reference taken in fcoe_if_create().
1873 */
Chris Leech030f4e02009-08-25 14:00:02 -07001874 fcoe_interface_put(fcoe);
Robert Love85b4aa42008-12-09 15:10:24 -08001875out_putdev:
1876 dev_put(netdev);
1877out_nodev:
Chris Leech2e70e242009-08-25 14:00:23 -07001878 rtnl_unlock();
Chris Leechdfc1d0f2009-08-25 14:00:13 -07001879 mutex_unlock(&fcoe_config_mutex);
Robert Love85b4aa42008-12-09 15:10:24 -08001880 return rc;
1881}
1882
1883module_param_call(create, fcoe_create, NULL, NULL, S_IWUSR);
1884__MODULE_PARM_TYPE(create, "string");
Chris Leech014f5c32009-08-25 13:59:30 -07001885MODULE_PARM_DESC(create, "Create fcoe fcoe using net device passed in.");
Robert Love85b4aa42008-12-09 15:10:24 -08001886module_param_call(destroy, fcoe_destroy, NULL, NULL, S_IWUSR);
1887__MODULE_PARM_TYPE(destroy, "string");
Chris Leech014f5c32009-08-25 13:59:30 -07001888MODULE_PARM_DESC(destroy, "Destroy fcoe fcoe");
Robert Love85b4aa42008-12-09 15:10:24 -08001889
Robert Love34f42a02009-02-27 10:55:45 -08001890/**
1891 * fcoe_link_ok() - Check if link is ok for the fc_lport
Robert Love85b4aa42008-12-09 15:10:24 -08001892 * @lp: ptr to the fc_lport
1893 *
1894 * Any permanently-disqualifying conditions have been previously checked.
1895 * This also updates the speed setting, which may change with link for 100/1000.
1896 *
1897 * This function should probably be checking for PAUSE support at some point
1898 * in the future. Currently Per-priority-pause is not determinable using
1899 * ethtool, so we shouldn't be restrictive until that problem is resolved.
1900 *
1901 * Returns: 0 if link is OK for use by FCoE.
1902 *
1903 */
1904int fcoe_link_ok(struct fc_lport *lp)
1905{
Chris Leech014f5c32009-08-25 13:59:30 -07001906 struct fcoe_port *port = lport_priv(lp);
Chris Leech25024982009-08-25 13:59:35 -07001907 struct net_device *dev = port->fcoe->netdev;
Robert Love85b4aa42008-12-09 15:10:24 -08001908 struct ethtool_cmd ecmd = { ETHTOOL_GSET };
Robert Love85b4aa42008-12-09 15:10:24 -08001909
Yi Zou2f718d62009-07-29 17:04:01 -07001910 if ((dev->flags & IFF_UP) && netif_carrier_ok(dev) &&
1911 (!dev_ethtool_get_settings(dev, &ecmd))) {
1912 lp->link_supported_speeds &=
1913 ~(FC_PORTSPEED_1GBIT | FC_PORTSPEED_10GBIT);
1914 if (ecmd.supported & (SUPPORTED_1000baseT_Half |
1915 SUPPORTED_1000baseT_Full))
1916 lp->link_supported_speeds |= FC_PORTSPEED_1GBIT;
1917 if (ecmd.supported & SUPPORTED_10000baseT_Full)
1918 lp->link_supported_speeds |=
1919 FC_PORTSPEED_10GBIT;
1920 if (ecmd.speed == SPEED_1000)
1921 lp->link_speed = FC_PORTSPEED_1GBIT;
1922 if (ecmd.speed == SPEED_10000)
1923 lp->link_speed = FC_PORTSPEED_10GBIT;
Robert Love85b4aa42008-12-09 15:10:24 -08001924
Yi Zou2f718d62009-07-29 17:04:01 -07001925 return 0;
1926 }
1927 return -1;
Robert Love85b4aa42008-12-09 15:10:24 -08001928}
Robert Love85b4aa42008-12-09 15:10:24 -08001929
Robert Love34f42a02009-02-27 10:55:45 -08001930/**
1931 * fcoe_percpu_clean() - Clear the pending skbs for an lport
Robert Love85b4aa42008-12-09 15:10:24 -08001932 * @lp: the fc_lport
Joe Eykholte7a51992009-08-25 14:04:08 -07001933 *
1934 * Must be called with fcoe_create_mutex held to single-thread completion.
1935 *
1936 * This flushes the pending skbs by adding a new skb to each queue and
1937 * waiting until they are all freed. This assures us that not only are
1938 * there no packets that will be handled by the lport, but also that any
1939 * threads already handling packet have returned.
Robert Love85b4aa42008-12-09 15:10:24 -08001940 */
1941void fcoe_percpu_clean(struct fc_lport *lp)
1942{
Robert Love85b4aa42008-12-09 15:10:24 -08001943 struct fcoe_percpu_s *pp;
1944 struct fcoe_rcv_info *fr;
1945 struct sk_buff_head *list;
1946 struct sk_buff *skb, *next;
1947 struct sk_buff *head;
Robert Love5e5e92d2009-03-17 11:41:35 -07001948 unsigned int cpu;
Robert Love85b4aa42008-12-09 15:10:24 -08001949
Robert Love5e5e92d2009-03-17 11:41:35 -07001950 for_each_possible_cpu(cpu) {
1951 pp = &per_cpu(fcoe_percpu, cpu);
1952 spin_lock_bh(&pp->fcoe_rx_list.lock);
1953 list = &pp->fcoe_rx_list;
1954 head = list->next;
1955 for (skb = head; skb != (struct sk_buff *)list;
1956 skb = next) {
1957 next = skb->next;
1958 fr = fcoe_dev_from_skb(skb);
1959 if (fr->fr_dev == lp) {
1960 __skb_unlink(skb, list);
1961 kfree_skb(skb);
Robert Love85b4aa42008-12-09 15:10:24 -08001962 }
Robert Love85b4aa42008-12-09 15:10:24 -08001963 }
Joe Eykholte7a51992009-08-25 14:04:08 -07001964
1965 if (!pp->thread || !cpu_online(cpu)) {
1966 spin_unlock_bh(&pp->fcoe_rx_list.lock);
1967 continue;
1968 }
1969
1970 skb = dev_alloc_skb(0);
1971 if (!skb) {
1972 spin_unlock_bh(&pp->fcoe_rx_list.lock);
1973 continue;
1974 }
1975 skb->destructor = fcoe_percpu_flush_done;
1976
1977 __skb_queue_tail(&pp->fcoe_rx_list, skb);
1978 if (pp->fcoe_rx_list.qlen == 1)
1979 wake_up_process(pp->thread);
Robert Love5e5e92d2009-03-17 11:41:35 -07001980 spin_unlock_bh(&pp->fcoe_rx_list.lock);
Joe Eykholte7a51992009-08-25 14:04:08 -07001981
1982 wait_for_completion(&fcoe_flush_completion);
Robert Love85b4aa42008-12-09 15:10:24 -08001983 }
1984}
Robert Love85b4aa42008-12-09 15:10:24 -08001985
1986/**
Robert Love34f42a02009-02-27 10:55:45 -08001987 * fcoe_clean_pending_queue() - Dequeue a skb and free it
Robert Love85b4aa42008-12-09 15:10:24 -08001988 * @lp: the corresponding fc_lport
1989 *
1990 * Returns: none
Robert Love34f42a02009-02-27 10:55:45 -08001991 */
Robert Love85b4aa42008-12-09 15:10:24 -08001992void fcoe_clean_pending_queue(struct fc_lport *lp)
1993{
Chris Leech014f5c32009-08-25 13:59:30 -07001994 struct fcoe_port *port = lport_priv(lp);
Robert Love85b4aa42008-12-09 15:10:24 -08001995 struct sk_buff *skb;
1996
Chris Leech014f5c32009-08-25 13:59:30 -07001997 spin_lock_bh(&port->fcoe_pending_queue.lock);
1998 while ((skb = __skb_dequeue(&port->fcoe_pending_queue)) != NULL) {
1999 spin_unlock_bh(&port->fcoe_pending_queue.lock);
Robert Love85b4aa42008-12-09 15:10:24 -08002000 kfree_skb(skb);
Chris Leech014f5c32009-08-25 13:59:30 -07002001 spin_lock_bh(&port->fcoe_pending_queue.lock);
Robert Love85b4aa42008-12-09 15:10:24 -08002002 }
Chris Leech014f5c32009-08-25 13:59:30 -07002003 spin_unlock_bh(&port->fcoe_pending_queue.lock);
Robert Love85b4aa42008-12-09 15:10:24 -08002004}
Robert Love85b4aa42008-12-09 15:10:24 -08002005
2006/**
Robert Love34f42a02009-02-27 10:55:45 -08002007 * fcoe_reset() - Resets the fcoe
Robert Love85b4aa42008-12-09 15:10:24 -08002008 * @shost: shost the reset is from
2009 *
2010 * Returns: always 0
2011 */
2012int fcoe_reset(struct Scsi_Host *shost)
2013{
2014 struct fc_lport *lport = shost_priv(shost);
2015 fc_lport_reset(lport);
2016 return 0;
2017}
Robert Love85b4aa42008-12-09 15:10:24 -08002018
Robert Love34f42a02009-02-27 10:55:45 -08002019/**
Chris Leech014f5c32009-08-25 13:59:30 -07002020 * fcoe_hostlist_lookup_port() - find the corresponding lport by a given device
Chris Leechdd3fd722009-04-21 16:27:36 -07002021 * @dev: this is currently ptr to net_device
Robert Love85b4aa42008-12-09 15:10:24 -08002022 *
Chris Leech014f5c32009-08-25 13:59:30 -07002023 * Returns: NULL or the located fcoe_port
Chris Leech090eb6c2009-08-25 14:00:28 -07002024 * Locking: must be called with the RNL mutex held
Robert Love85b4aa42008-12-09 15:10:24 -08002025 */
Chris Leech014f5c32009-08-25 13:59:30 -07002026static struct fcoe_interface *
2027fcoe_hostlist_lookup_port(const struct net_device *dev)
Robert Love85b4aa42008-12-09 15:10:24 -08002028{
Chris Leech014f5c32009-08-25 13:59:30 -07002029 struct fcoe_interface *fcoe;
Robert Love85b4aa42008-12-09 15:10:24 -08002030
Chris Leech014f5c32009-08-25 13:59:30 -07002031 list_for_each_entry(fcoe, &fcoe_hostlist, list) {
Chris Leech25024982009-08-25 13:59:35 -07002032 if (fcoe->netdev == dev)
Chris Leech014f5c32009-08-25 13:59:30 -07002033 return fcoe;
Robert Love85b4aa42008-12-09 15:10:24 -08002034 }
Robert Love85b4aa42008-12-09 15:10:24 -08002035 return NULL;
2036}
2037
Robert Love34f42a02009-02-27 10:55:45 -08002038/**
2039 * fcoe_hostlist_lookup() - Find the corresponding lport by netdev
Robert Love85b4aa42008-12-09 15:10:24 -08002040 * @netdev: ptr to net_device
2041 *
2042 * Returns: 0 for success
Chris Leech090eb6c2009-08-25 14:00:28 -07002043 * Locking: must be called with the RTNL mutex held
Robert Love85b4aa42008-12-09 15:10:24 -08002044 */
Chris Leech090eb6c2009-08-25 14:00:28 -07002045static struct fc_lport *fcoe_hostlist_lookup(const struct net_device *netdev)
Robert Love85b4aa42008-12-09 15:10:24 -08002046{
Chris Leech014f5c32009-08-25 13:59:30 -07002047 struct fcoe_interface *fcoe;
Robert Love85b4aa42008-12-09 15:10:24 -08002048
Chris Leech014f5c32009-08-25 13:59:30 -07002049 fcoe = fcoe_hostlist_lookup_port(netdev);
Chris Leech3fe9a0b2009-08-25 13:59:46 -07002050 return (fcoe) ? fcoe->ctlr.lp : NULL;
Robert Love85b4aa42008-12-09 15:10:24 -08002051}
Robert Love85b4aa42008-12-09 15:10:24 -08002052
Robert Love34f42a02009-02-27 10:55:45 -08002053/**
2054 * fcoe_hostlist_add() - Add a lport to lports list
Chris Leechdd3fd722009-04-21 16:27:36 -07002055 * @lp: ptr to the fc_lport to be added
Robert Love85b4aa42008-12-09 15:10:24 -08002056 *
2057 * Returns: 0 for success
Chris Leech090eb6c2009-08-25 14:00:28 -07002058 * Locking: must be called with the RTNL mutex held
Robert Love85b4aa42008-12-09 15:10:24 -08002059 */
Chris Leech090eb6c2009-08-25 14:00:28 -07002060static int fcoe_hostlist_add(const struct fc_lport *lport)
Robert Love85b4aa42008-12-09 15:10:24 -08002061{
Chris Leech014f5c32009-08-25 13:59:30 -07002062 struct fcoe_interface *fcoe;
2063 struct fcoe_port *port;
Robert Love85b4aa42008-12-09 15:10:24 -08002064
Chris Leech014f5c32009-08-25 13:59:30 -07002065 fcoe = fcoe_hostlist_lookup_port(fcoe_netdev(lport));
2066 if (!fcoe) {
2067 port = lport_priv(lport);
2068 fcoe = port->fcoe;
2069 list_add_tail(&fcoe->list, &fcoe_hostlist);
Robert Love85b4aa42008-12-09 15:10:24 -08002070 }
2071 return 0;
2072}
Robert Love85b4aa42008-12-09 15:10:24 -08002073
Robert Love34f42a02009-02-27 10:55:45 -08002074/**
Robert Love34f42a02009-02-27 10:55:45 -08002075 * fcoe_init() - fcoe module loading initialization
Robert Love85b4aa42008-12-09 15:10:24 -08002076 *
Robert Love85b4aa42008-12-09 15:10:24 -08002077 * Returns 0 on success, negative on failure
Robert Love34f42a02009-02-27 10:55:45 -08002078 */
Robert Love85b4aa42008-12-09 15:10:24 -08002079static int __init fcoe_init(void)
2080{
Robert Love38eccab2009-03-17 11:41:30 -07002081 unsigned int cpu;
Robert Love8976f422009-03-17 11:41:46 -07002082 int rc = 0;
Robert Love85b4aa42008-12-09 15:10:24 -08002083 struct fcoe_percpu_s *p;
2084
Chris Leechdfc1d0f2009-08-25 14:00:13 -07002085 mutex_lock(&fcoe_config_mutex);
2086
Robert Love38eccab2009-03-17 11:41:30 -07002087 for_each_possible_cpu(cpu) {
Robert Love5e5e92d2009-03-17 11:41:35 -07002088 p = &per_cpu(fcoe_percpu, cpu);
Robert Love38eccab2009-03-17 11:41:30 -07002089 skb_queue_head_init(&p->fcoe_rx_list);
2090 }
2091
Robert Love8976f422009-03-17 11:41:46 -07002092 for_each_online_cpu(cpu)
2093 fcoe_percpu_thread_create(cpu);
Robert Love85b4aa42008-12-09 15:10:24 -08002094
Robert Love8976f422009-03-17 11:41:46 -07002095 /* Initialize per CPU interrupt thread */
2096 rc = register_hotcpu_notifier(&fcoe_cpu_notifier);
2097 if (rc)
2098 goto out_free;
Robert Love85b4aa42008-12-09 15:10:24 -08002099
Robert Love8976f422009-03-17 11:41:46 -07002100 /* Setup link change notification */
Robert Love85b4aa42008-12-09 15:10:24 -08002101 fcoe_dev_setup();
2102
Chris Leech5892c322009-08-25 13:59:14 -07002103 rc = fcoe_if_init();
2104 if (rc)
2105 goto out_free;
Robert Love85b4aa42008-12-09 15:10:24 -08002106
Chris Leechdfc1d0f2009-08-25 14:00:13 -07002107 mutex_unlock(&fcoe_config_mutex);
Robert Love85b4aa42008-12-09 15:10:24 -08002108 return 0;
Robert Love8976f422009-03-17 11:41:46 -07002109
2110out_free:
2111 for_each_online_cpu(cpu) {
2112 fcoe_percpu_thread_destroy(cpu);
2113 }
Chris Leechdfc1d0f2009-08-25 14:00:13 -07002114 mutex_unlock(&fcoe_config_mutex);
Robert Love8976f422009-03-17 11:41:46 -07002115 return rc;
Robert Love85b4aa42008-12-09 15:10:24 -08002116}
2117module_init(fcoe_init);
2118
2119/**
Robert Love34f42a02009-02-27 10:55:45 -08002120 * fcoe_exit() - fcoe module unloading cleanup
Robert Love85b4aa42008-12-09 15:10:24 -08002121 *
2122 * Returns 0 on success, negative on failure
Robert Love34f42a02009-02-27 10:55:45 -08002123 */
Robert Love85b4aa42008-12-09 15:10:24 -08002124static void __exit fcoe_exit(void)
2125{
Robert Love5e5e92d2009-03-17 11:41:35 -07002126 unsigned int cpu;
Chris Leech014f5c32009-08-25 13:59:30 -07002127 struct fcoe_interface *fcoe, *tmp;
Chris Leech2e70e242009-08-25 14:00:23 -07002128 struct fcoe_port *port;
Robert Love85b4aa42008-12-09 15:10:24 -08002129
Chris Leechdfc1d0f2009-08-25 14:00:13 -07002130 mutex_lock(&fcoe_config_mutex);
2131
Robert Love85b4aa42008-12-09 15:10:24 -08002132 fcoe_dev_cleanup();
2133
Vasu Dev5919a592009-03-27 09:03:29 -07002134 /* releases the associated fcoe hosts */
Chris Leech090eb6c2009-08-25 14:00:28 -07002135 rtnl_lock();
2136 list_for_each_entry_safe(fcoe, tmp, &fcoe_hostlist, list) {
Chris Leechc863df32009-08-25 14:00:18 -07002137 list_del(&fcoe->list);
Chris Leech2e70e242009-08-25 14:00:23 -07002138 port = lport_priv(fcoe->ctlr.lp);
Chris Leech2e70e242009-08-25 14:00:23 -07002139 fcoe_interface_cleanup(fcoe);
Chris Leech2e70e242009-08-25 14:00:23 -07002140 schedule_work(&port->destroy_work);
Chris Leechc863df32009-08-25 14:00:18 -07002141 }
Chris Leech090eb6c2009-08-25 14:00:28 -07002142 rtnl_unlock();
Robert Love85b4aa42008-12-09 15:10:24 -08002143
Robert Love8976f422009-03-17 11:41:46 -07002144 unregister_hotcpu_notifier(&fcoe_cpu_notifier);
2145
Chris Leech014f5c32009-08-25 13:59:30 -07002146 for_each_online_cpu(cpu)
Robert Love8976f422009-03-17 11:41:46 -07002147 fcoe_percpu_thread_destroy(cpu);
Robert Love85b4aa42008-12-09 15:10:24 -08002148
Chris Leechdfc1d0f2009-08-25 14:00:13 -07002149 mutex_unlock(&fcoe_config_mutex);
Chris Leech2e70e242009-08-25 14:00:23 -07002150
2151 /* flush any asyncronous interface destroys,
2152 * this should happen after the netdev notifier is unregistered */
2153 flush_scheduled_work();
Chris Leech9a057532009-11-03 11:46:40 -08002154 /* That will flush out all the N_Ports on the hostlist, but now we
2155 * may have NPIV VN_Ports scheduled for destruction */
2156 flush_scheduled_work();
Chris Leech2e70e242009-08-25 14:00:23 -07002157
2158 /* detach from scsi transport
2159 * must happen after all destroys are done, therefor after the flush */
2160 fcoe_if_exit();
Robert Love85b4aa42008-12-09 15:10:24 -08002161}
2162module_exit(fcoe_exit);
Chris Leech11b56182009-11-03 11:46:29 -08002163
2164/**
2165 * fcoe_flogi_resp() - FCoE specific FLOGI and FDISC response handler
2166 * @seq: active sequence in the FLOGI or FDISC exchange
2167 * @fp: response frame, or error encoded in a pointer (timeout)
2168 * @arg: pointer the the fcoe_ctlr structure
2169 *
2170 * This handles MAC address managment for FCoE, then passes control on to
2171 * the libfc FLOGI response handler.
2172 */
2173static void fcoe_flogi_resp(struct fc_seq *seq, struct fc_frame *fp, void *arg)
2174{
2175 struct fcoe_ctlr *fip = arg;
2176 struct fc_exch *exch = fc_seq_exch(seq);
2177 struct fc_lport *lport = exch->lp;
2178 u8 *mac;
2179
2180 if (IS_ERR(fp))
2181 goto done;
2182
2183 mac = fr_cb(fp)->granted_mac;
2184 if (is_zero_ether_addr(mac)) {
2185 /* pre-FIP */
2186 mac = eth_hdr(&fp->skb)->h_source;
2187 if (fcoe_ctlr_recv_flogi(fip, lport, fp, mac)) {
2188 fc_frame_free(fp);
2189 return;
2190 }
2191 } else {
2192 /* FIP, libfcoe has already seen it */
2193 fip->update_mac(lport, fr_cb(fp)->granted_mac);
2194 }
2195done:
2196 fc_lport_flogi_resp(seq, fp, lport);
2197}
2198
2199/**
2200 * fcoe_logo_resp() - FCoE specific LOGO response handler
2201 * @seq: active sequence in the LOGO exchange
2202 * @fp: response frame, or error encoded in a pointer (timeout)
2203 * @arg: pointer the the fcoe_ctlr structure
2204 *
2205 * This handles MAC address managment for FCoE, then passes control on to
2206 * the libfc LOGO response handler.
2207 */
2208static void fcoe_logo_resp(struct fc_seq *seq, struct fc_frame *fp, void *arg)
2209{
2210 struct fcoe_ctlr *fip = arg;
2211 struct fc_exch *exch = fc_seq_exch(seq);
2212 struct fc_lport *lport = exch->lp;
2213 static u8 zero_mac[ETH_ALEN] = { 0 };
2214
2215 if (!IS_ERR(fp))
2216 fip->update_mac(lport, zero_mac);
2217 fc_lport_logo_resp(seq, fp, lport);
2218}
2219
2220/**
2221 * fcoe_elsct_send - FCoE specific ELS handler
2222 *
2223 * This does special case handling of FIP encapsualted ELS exchanges for FCoE,
2224 * using FCoE specific response handlers and passing the FIP controller as
2225 * the argument (the lport is still available from the exchange).
2226 *
2227 * Most of the work here is just handed off to the libfc routine.
2228 */
2229static struct fc_seq *fcoe_elsct_send(struct fc_lport *lport,
2230 u32 did, struct fc_frame *fp, unsigned int op,
2231 void (*resp)(struct fc_seq *, struct fc_frame *, void *),
2232 void *arg, u32 timeout)
2233{
2234 struct fcoe_port *port = lport_priv(lport);
2235 struct fcoe_interface *fcoe = port->fcoe;
2236 struct fcoe_ctlr *fip = &fcoe->ctlr;
2237 struct fc_frame_header *fh = fc_frame_header_get(fp);
2238
2239 switch (op) {
2240 case ELS_FLOGI:
2241 case ELS_FDISC:
2242 return fc_elsct_send(lport, did, fp, op, fcoe_flogi_resp,
2243 fip, timeout);
2244 case ELS_LOGO:
2245 /* only hook onto fabric logouts, not port logouts */
2246 if (ntoh24(fh->fh_d_id) != FC_FID_FLOGI)
2247 break;
2248 return fc_elsct_send(lport, did, fp, op, fcoe_logo_resp,
2249 fip, timeout);
2250 }
2251 return fc_elsct_send(lport, did, fp, op, resp, arg, timeout);
2252}
2253
Chris Leech9a057532009-11-03 11:46:40 -08002254/**
2255 * fcoe_vport_create() - create an fc_host/scsi_host for a vport
2256 * @vport: fc_vport object to create a new fc_host for
2257 * @disabled: start the new fc_host in a disabled state by default?
2258 *
2259 * Returns: 0 for success
2260 */
2261static int fcoe_vport_create(struct fc_vport *vport, bool disabled)
2262{
2263 struct Scsi_Host *shost = vport_to_shost(vport);
2264 struct fc_lport *n_port = shost_priv(shost);
2265 struct fcoe_port *port = lport_priv(n_port);
2266 struct fcoe_interface *fcoe = port->fcoe;
2267 struct net_device *netdev = fcoe->netdev;
2268 struct fc_lport *vn_port;
2269
2270 mutex_lock(&fcoe_config_mutex);
2271 vn_port = fcoe_if_create(fcoe, &vport->dev, 1);
2272 mutex_unlock(&fcoe_config_mutex);
2273
2274 if (IS_ERR(vn_port)) {
2275 printk(KERN_ERR "fcoe: fcoe_vport_create(%s) failed\n",
2276 netdev->name);
2277 return -EIO;
2278 }
2279
2280 if (disabled) {
2281 fc_vport_set_state(vport, FC_VPORT_DISABLED);
2282 } else {
2283 vn_port->boot_time = jiffies;
2284 fc_fabric_login(vn_port);
2285 fc_vport_setlink(vn_port);
2286 }
2287 return 0;
2288}
2289
2290/**
2291 * fcoe_vport_destroy() - destroy the fc_host/scsi_host for a vport
2292 * @vport: fc_vport object that is being destroyed
2293 *
2294 * Returns: 0 for success
2295 */
2296static int fcoe_vport_destroy(struct fc_vport *vport)
2297{
2298 struct Scsi_Host *shost = vport_to_shost(vport);
2299 struct fc_lport *n_port = shost_priv(shost);
2300 struct fc_lport *vn_port = vport->dd_data;
2301 struct fcoe_port *port = lport_priv(vn_port);
2302
2303 mutex_lock(&n_port->lp_mutex);
2304 list_del(&vn_port->list);
2305 mutex_unlock(&n_port->lp_mutex);
2306 schedule_work(&port->destroy_work);
2307 return 0;
2308}
2309
2310/**
2311 * fcoe_vport_disable() - change vport state
2312 * @vport: vport to bring online/offline
2313 * @disable: should the vport be disabled?
2314 */
2315static int fcoe_vport_disable(struct fc_vport *vport, bool disable)
2316{
2317 struct fc_lport *lport = vport->dd_data;
2318
2319 if (disable) {
2320 fc_vport_set_state(vport, FC_VPORT_DISABLED);
2321 fc_fabric_logoff(lport);
2322 } else {
2323 lport->boot_time = jiffies;
2324 fc_fabric_login(lport);
2325 fc_vport_setlink(lport);
2326 }
2327
2328 return 0;
2329}
2330
Chris Leechdc8596d2009-11-03 11:47:18 -08002331/**
2332 * fcoe_vport_set_symbolic_name() - append vport string to symbolic name
2333 * @vport: fc_vport with a new symbolic name string
2334 *
2335 * After generating a new symbolic name string, a new RSPN_ID request is
2336 * sent to the name server. There is no response handler, so if it fails
2337 * for some reason it will not be retried.
2338 */
2339static void fcoe_set_vport_symbolic_name(struct fc_vport *vport)
2340{
2341 struct fc_lport *lport = vport->dd_data;
2342 struct fc_frame *fp;
2343 size_t len;
2344
2345 snprintf(fc_host_symbolic_name(lport->host), FC_SYMBOLIC_NAME_SIZE,
2346 "%s v%s over %s : %s", FCOE_NAME, FCOE_VERSION,
2347 fcoe_netdev(lport)->name, vport->symbolic_name);
2348
2349 if (lport->state != LPORT_ST_READY)
2350 return;
2351
2352 len = strnlen(fc_host_symbolic_name(lport->host), 255);
2353 fp = fc_frame_alloc(lport,
2354 sizeof(struct fc_ct_hdr) +
2355 sizeof(struct fc_ns_rspn) + len);
2356 if (!fp)
2357 return;
2358 lport->tt.elsct_send(lport, FC_FID_DIR_SERV, fp, FC_NS_RSPN_ID,
2359 NULL, NULL, lport->e_d_tov);
2360}
2361