blob: 641c60e8fda33b03a3f5f32b41cced7ac9eb8943 [file] [log] [blame]
Yi Zoufdecf312011-01-28 16:04:55 -08001/*
2 * Copyright(c) 2008 - 2011 Intel Corporation. All rights reserved.
3 *
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/types.h>
21#include <linux/module.h>
22#include <linux/kernel.h>
23#include <linux/list.h>
24#include <linux/netdevice.h>
25#include <linux/errno.h>
Bhanu Prakash Gollapudi8597ae82011-01-28 16:05:37 -080026#include <linux/crc32.h>
Yi Zoufdecf312011-01-28 16:04:55 -080027#include <scsi/libfcoe.h>
28
29#include "libfcoe.h"
30
Yi Zoue01efc32011-01-28 16:05:06 -080031MODULE_AUTHOR("Open-FCoE.org");
32MODULE_DESCRIPTION("FIP discovery protocol and FCoE transport for FCoE HBAs");
33MODULE_LICENSE("GPL v2");
34
Yi Zoufdecf312011-01-28 16:04:55 -080035static int fcoe_transport_create(const char *, struct kernel_param *);
36static int fcoe_transport_destroy(const char *, struct kernel_param *);
37static int fcoe_transport_show(char *buffer, const struct kernel_param *kp);
38static struct fcoe_transport *fcoe_transport_lookup(struct net_device *device);
39static struct fcoe_transport *fcoe_netdev_map_lookup(struct net_device *device);
40static int fcoe_transport_enable(const char *, struct kernel_param *);
41static int fcoe_transport_disable(const char *, struct kernel_param *);
Bhanu Prakash Gollapudi70be6342011-02-25 15:03:17 -080042static int libfcoe_device_notification(struct notifier_block *notifier,
43 ulong event, void *ptr);
Yi Zoufdecf312011-01-28 16:04:55 -080044
45static LIST_HEAD(fcoe_transports);
Yi Zoufdecf312011-01-28 16:04:55 -080046static DEFINE_MUTEX(ft_mutex);
Bhanu Prakash Gollapudi70be6342011-02-25 15:03:17 -080047static LIST_HEAD(fcoe_netdevs);
48static DEFINE_MUTEX(fn_mutex);
Yi Zoufdecf312011-01-28 16:04:55 -080049
Yi Zoue01efc32011-01-28 16:05:06 -080050unsigned int libfcoe_debug_logging;
51module_param_named(debug_logging, libfcoe_debug_logging, int, S_IRUGO|S_IWUSR);
52MODULE_PARM_DESC(debug_logging, "a bit mask of logging levels");
53
Yi Zoufdecf312011-01-28 16:04:55 -080054module_param_call(show, NULL, fcoe_transport_show, NULL, S_IRUSR);
55__MODULE_PARM_TYPE(show, "string");
56MODULE_PARM_DESC(show, " Show attached FCoE transports");
57
58module_param_call(create, fcoe_transport_create, NULL,
59 (void *)FIP_MODE_FABRIC, S_IWUSR);
60__MODULE_PARM_TYPE(create, "string");
Masanari Iidabd0a1d62015-06-04 00:54:34 +090061MODULE_PARM_DESC(create, " Creates fcoe instance on an ethernet interface");
Yi Zoufdecf312011-01-28 16:04:55 -080062
63module_param_call(create_vn2vn, fcoe_transport_create, NULL,
64 (void *)FIP_MODE_VN2VN, S_IWUSR);
65__MODULE_PARM_TYPE(create_vn2vn, "string");
66MODULE_PARM_DESC(create_vn2vn, " Creates a VN_node to VN_node FCoE instance "
67 "on an Ethernet interface");
68
69module_param_call(destroy, fcoe_transport_destroy, NULL, NULL, S_IWUSR);
70__MODULE_PARM_TYPE(destroy, "string");
Masanari Iidabd0a1d62015-06-04 00:54:34 +090071MODULE_PARM_DESC(destroy, " Destroys fcoe instance on an ethernet interface");
Yi Zoufdecf312011-01-28 16:04:55 -080072
73module_param_call(enable, fcoe_transport_enable, NULL, NULL, S_IWUSR);
74__MODULE_PARM_TYPE(enable, "string");
Masanari Iidabd0a1d62015-06-04 00:54:34 +090075MODULE_PARM_DESC(enable, " Enables fcoe on an ethernet interface.");
Yi Zoufdecf312011-01-28 16:04:55 -080076
77module_param_call(disable, fcoe_transport_disable, NULL, NULL, S_IWUSR);
78__MODULE_PARM_TYPE(disable, "string");
Masanari Iidabd0a1d62015-06-04 00:54:34 +090079MODULE_PARM_DESC(disable, " Disables fcoe on an ethernet interface.");
Yi Zoufdecf312011-01-28 16:04:55 -080080
Bhanu Prakash Gollapudi70be6342011-02-25 15:03:17 -080081/* notification function for packets from net device */
82static struct notifier_block libfcoe_notifier = {
83 .notifier_call = libfcoe_device_notification,
84};
85
Yi Zou03702682012-12-06 06:23:58 +000086/**
87 * fcoe_link_speed_update() - Update the supported and actual link speeds
88 * @lport: The local port to update speeds for
89 *
90 * Returns: 0 if the ethtool query was successful
91 * -1 if the ethtool query failed
92 */
93int fcoe_link_speed_update(struct fc_lport *lport)
94{
95 struct net_device *netdev = fcoe_get_netdev(lport);
David Decotigny008eb732016-02-24 10:58:06 -080096 struct ethtool_link_ksettings ecmd;
Yi Zou03702682012-12-06 06:23:58 +000097
David Decotigny008eb732016-02-24 10:58:06 -080098 if (!__ethtool_get_link_ksettings(netdev, &ecmd)) {
Chris Leechb8d23dc2014-09-25 11:55:42 -070099 lport->link_supported_speeds &= ~(FC_PORTSPEED_1GBIT |
100 FC_PORTSPEED_10GBIT |
101 FC_PORTSPEED_20GBIT |
102 FC_PORTSPEED_40GBIT);
103
David Decotigny008eb732016-02-24 10:58:06 -0800104 if (ecmd.link_modes.supported[0] & (
105 SUPPORTED_1000baseT_Half |
106 SUPPORTED_1000baseT_Full |
107 SUPPORTED_1000baseKX_Full))
Yi Zou03702682012-12-06 06:23:58 +0000108 lport->link_supported_speeds |= FC_PORTSPEED_1GBIT;
Chris Leechb8d23dc2014-09-25 11:55:42 -0700109
David Decotigny008eb732016-02-24 10:58:06 -0800110 if (ecmd.link_modes.supported[0] & (
111 SUPPORTED_10000baseT_Full |
112 SUPPORTED_10000baseKX4_Full |
113 SUPPORTED_10000baseKR_Full |
114 SUPPORTED_10000baseR_FEC))
Chris Leechb8d23dc2014-09-25 11:55:42 -0700115 lport->link_supported_speeds |= FC_PORTSPEED_10GBIT;
116
David Decotigny008eb732016-02-24 10:58:06 -0800117 if (ecmd.link_modes.supported[0] & (
118 SUPPORTED_20000baseMLD2_Full |
119 SUPPORTED_20000baseKR2_Full))
Chris Leechb8d23dc2014-09-25 11:55:42 -0700120 lport->link_supported_speeds |= FC_PORTSPEED_20GBIT;
121
David Decotigny008eb732016-02-24 10:58:06 -0800122 if (ecmd.link_modes.supported[0] & (
123 SUPPORTED_40000baseKR4_Full |
124 SUPPORTED_40000baseCR4_Full |
125 SUPPORTED_40000baseSR4_Full |
126 SUPPORTED_40000baseLR4_Full))
Chris Leechb8d23dc2014-09-25 11:55:42 -0700127 lport->link_supported_speeds |= FC_PORTSPEED_40GBIT;
128
David Decotigny008eb732016-02-24 10:58:06 -0800129 switch (ecmd.base.speed) {
Yi Zou03702682012-12-06 06:23:58 +0000130 case SPEED_1000:
131 lport->link_speed = FC_PORTSPEED_1GBIT;
132 break;
133 case SPEED_10000:
134 lport->link_speed = FC_PORTSPEED_10GBIT;
135 break;
Chris Leechb8d23dc2014-09-25 11:55:42 -0700136 case 20000:
137 lport->link_speed = FC_PORTSPEED_20GBIT;
138 break;
139 case 40000:
140 lport->link_speed = FC_PORTSPEED_40GBIT;
141 break;
142 default:
143 lport->link_speed = FC_PORTSPEED_UNKNOWN;
144 break;
Yi Zou03702682012-12-06 06:23:58 +0000145 }
146 return 0;
147 }
148 return -1;
149}
150EXPORT_SYMBOL_GPL(fcoe_link_speed_update);
151
Yi Zou57c27282012-12-06 06:24:13 +0000152/**
153 * __fcoe_get_lesb() - Get the Link Error Status Block (LESB) for a given lport
154 * @lport: The local port to update speeds for
155 * @fc_lesb: Pointer to the LESB to be filled up
156 * @netdev: Pointer to the netdev that is associated with the lport
157 *
158 * Note, the Link Error Status Block (LESB) for FCoE is defined in FC-BB-6
159 * Clause 7.11 in v1.04.
160 */
Bhanu Prakash Gollapudi814740d2011-10-03 16:45:01 -0700161void __fcoe_get_lesb(struct fc_lport *lport,
162 struct fc_els_lesb *fc_lesb,
163 struct net_device *netdev)
164{
165 unsigned int cpu;
166 u32 lfc, vlfc, mdac;
Vasu Dev1bd49b42012-05-25 10:26:43 -0700167 struct fc_stats *stats;
Bhanu Prakash Gollapudi814740d2011-10-03 16:45:01 -0700168 struct fcoe_fc_els_lesb *lesb;
169 struct rtnl_link_stats64 temp;
170
171 lfc = 0;
172 vlfc = 0;
173 mdac = 0;
174 lesb = (struct fcoe_fc_els_lesb *)fc_lesb;
175 memset(lesb, 0, sizeof(*lesb));
176 for_each_possible_cpu(cpu) {
Vasu Dev1bd49b42012-05-25 10:26:43 -0700177 stats = per_cpu_ptr(lport->stats, cpu);
178 lfc += stats->LinkFailureCount;
179 vlfc += stats->VLinkFailureCount;
180 mdac += stats->MissDiscAdvCount;
Bhanu Prakash Gollapudi814740d2011-10-03 16:45:01 -0700181 }
182 lesb->lesb_link_fail = htonl(lfc);
183 lesb->lesb_vlink_fail = htonl(vlfc);
184 lesb->lesb_miss_fka = htonl(mdac);
185 lesb->lesb_fcs_error =
186 htonl(dev_get_stats(netdev, &temp)->rx_crc_errors);
187}
188EXPORT_SYMBOL_GPL(__fcoe_get_lesb);
189
Yi Zou57c27282012-12-06 06:24:13 +0000190/**
191 * fcoe_get_lesb() - Fill the FCoE Link Error Status Block
192 * @lport: the local port
193 * @fc_lesb: the link error status block
194 */
195void fcoe_get_lesb(struct fc_lport *lport,
196 struct fc_els_lesb *fc_lesb)
197{
198 struct net_device *netdev = fcoe_get_netdev(lport);
199
200 __fcoe_get_lesb(lport, fc_lesb, netdev);
201}
202EXPORT_SYMBOL_GPL(fcoe_get_lesb);
203
204/**
205 * fcoe_ctlr_get_lesb() - Get the Link Error Status Block (LESB) for a given
206 * fcoe controller device
207 * @ctlr_dev: The given fcoe controller device
208 *
209 */
210void fcoe_ctlr_get_lesb(struct fcoe_ctlr_device *ctlr_dev)
211{
212 struct fcoe_ctlr *fip = fcoe_ctlr_device_priv(ctlr_dev);
213 struct net_device *netdev = fcoe_get_netdev(fip->lp);
Yi Zou418a8cfe2013-05-18 06:28:17 +0000214 struct fc_els_lesb *fc_lesb;
Yi Zou57c27282012-12-06 06:24:13 +0000215
Yi Zou418a8cfe2013-05-18 06:28:17 +0000216 fc_lesb = (struct fc_els_lesb *)(&ctlr_dev->lesb);
217 __fcoe_get_lesb(fip->lp, fc_lesb, netdev);
Yi Zou57c27282012-12-06 06:24:13 +0000218}
219EXPORT_SYMBOL_GPL(fcoe_ctlr_get_lesb);
220
Bhanu Prakash Gollapudid8348952011-08-04 17:38:49 -0700221void fcoe_wwn_to_str(u64 wwn, char *buf, int len)
222{
223 u8 wwpn[8];
224
225 u64_to_wwn(wwn, wwpn);
226 snprintf(buf, len, "%02x%02x%02x%02x%02x%02x%02x%02x",
227 wwpn[0], wwpn[1], wwpn[2], wwpn[3],
228 wwpn[4], wwpn[5], wwpn[6], wwpn[7]);
229}
230EXPORT_SYMBOL_GPL(fcoe_wwn_to_str);
231
232/**
233 * fcoe_validate_vport_create() - Validate a vport before creating it
234 * @vport: NPIV port to be created
235 *
236 * This routine is meant to add validation for a vport before creating it
237 * via fcoe_vport_create().
238 * Current validations are:
239 * - WWPN supplied is unique for given lport
240 */
241int fcoe_validate_vport_create(struct fc_vport *vport)
242{
243 struct Scsi_Host *shost = vport_to_shost(vport);
244 struct fc_lport *n_port = shost_priv(shost);
245 struct fc_lport *vn_port;
246 int rc = 0;
247 char buf[32];
248
249 mutex_lock(&n_port->lp_mutex);
250
251 fcoe_wwn_to_str(vport->port_name, buf, sizeof(buf));
252 /* Check if the wwpn is not same as that of the lport */
253 if (!memcmp(&n_port->wwpn, &vport->port_name, sizeof(u64))) {
254 LIBFCOE_TRANSPORT_DBG("vport WWPN 0x%s is same as that of the "
255 "base port WWPN\n", buf);
256 rc = -EINVAL;
257 goto out;
258 }
259
260 /* Check if there is any existing vport with same wwpn */
261 list_for_each_entry(vn_port, &n_port->vports, list) {
262 if (!memcmp(&vn_port->wwpn, &vport->port_name, sizeof(u64))) {
263 LIBFCOE_TRANSPORT_DBG("vport with given WWPN 0x%s "
264 "already exists\n", buf);
265 rc = -EINVAL;
266 break;
267 }
268 }
269out:
270 mutex_unlock(&n_port->lp_mutex);
271 return rc;
272}
273EXPORT_SYMBOL_GPL(fcoe_validate_vport_create);
274
275/**
276 * fcoe_get_wwn() - Get the world wide name from LLD if it supports it
277 * @netdev: the associated net device
278 * @wwn: the output WWN
279 * @type: the type of WWN (WWPN or WWNN)
280 *
281 * Returns: 0 for success
282 */
283int fcoe_get_wwn(struct net_device *netdev, u64 *wwn, int type)
284{
285 const struct net_device_ops *ops = netdev->netdev_ops;
286
287 if (ops->ndo_fcoe_get_wwn)
288 return ops->ndo_fcoe_get_wwn(netdev, wwn, type);
289 return -EINVAL;
290}
291EXPORT_SYMBOL_GPL(fcoe_get_wwn);
292
Yi Zoufdecf312011-01-28 16:04:55 -0800293/**
Bhanu Prakash Gollapudi8597ae82011-01-28 16:05:37 -0800294 * fcoe_fc_crc() - Calculates the CRC for a given frame
295 * @fp: The frame to be checksumed
296 *
297 * This uses crc32() routine to calculate the CRC for a frame
298 *
299 * Return: The 32 bit CRC value
300 */
301u32 fcoe_fc_crc(struct fc_frame *fp)
302{
303 struct sk_buff *skb = fp_skb(fp);
304 struct skb_frag_struct *frag;
305 unsigned char *data;
306 unsigned long off, len, clen;
307 u32 crc;
308 unsigned i;
309
310 crc = crc32(~0, skb->data, skb_headlen(skb));
311
312 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
313 frag = &skb_shinfo(skb)->frags[i];
314 off = frag->page_offset;
Eric Dumazet9e903e02011-10-18 21:00:24 +0000315 len = skb_frag_size(frag);
Bhanu Prakash Gollapudi8597ae82011-01-28 16:05:37 -0800316 while (len > 0) {
317 clen = min(len, PAGE_SIZE - (off & ~PAGE_MASK));
Ian Campbell165c68d2011-08-24 22:28:15 +0000318 data = kmap_atomic(
Cong Wang77dfce02011-11-25 23:14:23 +0800319 skb_frag_page(frag) + (off >> PAGE_SHIFT));
Bhanu Prakash Gollapudi8597ae82011-01-28 16:05:37 -0800320 crc = crc32(crc, data + (off & ~PAGE_MASK), clen);
Cong Wang77dfce02011-11-25 23:14:23 +0800321 kunmap_atomic(data);
Bhanu Prakash Gollapudi8597ae82011-01-28 16:05:37 -0800322 off += clen;
323 len -= clen;
324 }
325 }
326 return crc;
327}
328EXPORT_SYMBOL_GPL(fcoe_fc_crc);
329
330/**
331 * fcoe_start_io() - Start FCoE I/O
332 * @skb: The packet to be transmitted
333 *
334 * This routine is called from the net device to start transmitting
335 * FCoE packets.
336 *
337 * Returns: 0 for success
338 */
339int fcoe_start_io(struct sk_buff *skb)
340{
341 struct sk_buff *nskb;
342 int rc;
343
344 nskb = skb_clone(skb, GFP_ATOMIC);
345 if (!nskb)
346 return -ENOMEM;
347 rc = dev_queue_xmit(nskb);
348 if (rc != 0)
349 return rc;
350 kfree_skb(skb);
351 return 0;
352}
353EXPORT_SYMBOL_GPL(fcoe_start_io);
354
355
356/**
357 * fcoe_clean_pending_queue() - Dequeue a skb and free it
358 * @lport: The local port to dequeue a skb on
359 */
360void fcoe_clean_pending_queue(struct fc_lport *lport)
361{
362 struct fcoe_port *port = lport_priv(lport);
363 struct sk_buff *skb;
364
365 spin_lock_bh(&port->fcoe_pending_queue.lock);
366 while ((skb = __skb_dequeue(&port->fcoe_pending_queue)) != NULL) {
367 spin_unlock_bh(&port->fcoe_pending_queue.lock);
368 kfree_skb(skb);
369 spin_lock_bh(&port->fcoe_pending_queue.lock);
370 }
371 spin_unlock_bh(&port->fcoe_pending_queue.lock);
372}
373EXPORT_SYMBOL_GPL(fcoe_clean_pending_queue);
374
375/**
376 * fcoe_check_wait_queue() - Attempt to clear the transmit backlog
377 * @lport: The local port whose backlog is to be cleared
378 *
379 * This empties the wait_queue, dequeues the head of the wait_queue queue
380 * and calls fcoe_start_io() for each packet. If all skb have been
381 * transmitted it returns the qlen. If an error occurs it restores
382 * wait_queue (to try again later) and returns -1.
383 *
384 * The wait_queue is used when the skb transmit fails. The failed skb
385 * will go in the wait_queue which will be emptied by the timer function or
386 * by the next skb transmit.
387 */
388void fcoe_check_wait_queue(struct fc_lport *lport, struct sk_buff *skb)
389{
390 struct fcoe_port *port = lport_priv(lport);
391 int rc;
392
393 spin_lock_bh(&port->fcoe_pending_queue.lock);
394
395 if (skb)
396 __skb_queue_tail(&port->fcoe_pending_queue, skb);
397
398 if (port->fcoe_pending_queue_active)
399 goto out;
400 port->fcoe_pending_queue_active = 1;
401
402 while (port->fcoe_pending_queue.qlen) {
403 /* keep qlen > 0 until fcoe_start_io succeeds */
404 port->fcoe_pending_queue.qlen++;
405 skb = __skb_dequeue(&port->fcoe_pending_queue);
406
407 spin_unlock_bh(&port->fcoe_pending_queue.lock);
408 rc = fcoe_start_io(skb);
409 spin_lock_bh(&port->fcoe_pending_queue.lock);
410
411 if (rc) {
412 __skb_queue_head(&port->fcoe_pending_queue, skb);
413 /* undo temporary increment above */
414 port->fcoe_pending_queue.qlen--;
415 break;
416 }
417 /* undo temporary increment above */
418 port->fcoe_pending_queue.qlen--;
419 }
420
421 if (port->fcoe_pending_queue.qlen < port->min_queue_depth)
422 lport->qfull = 0;
423 if (port->fcoe_pending_queue.qlen && !timer_pending(&port->timer))
424 mod_timer(&port->timer, jiffies + 2);
425 port->fcoe_pending_queue_active = 0;
426out:
427 if (port->fcoe_pending_queue.qlen > port->max_queue_depth)
428 lport->qfull = 1;
429 spin_unlock_bh(&port->fcoe_pending_queue.lock);
430}
431EXPORT_SYMBOL_GPL(fcoe_check_wait_queue);
432
433/**
434 * fcoe_queue_timer() - The fcoe queue timer
435 * @lport: The local port
436 *
437 * Calls fcoe_check_wait_queue on timeout
438 */
439void fcoe_queue_timer(ulong lport)
440{
441 fcoe_check_wait_queue((struct fc_lport *)lport, NULL);
442}
443EXPORT_SYMBOL_GPL(fcoe_queue_timer);
444
445/**
446 * fcoe_get_paged_crc_eof() - Allocate a page to be used for the trailer CRC
447 * @skb: The packet to be transmitted
448 * @tlen: The total length of the trailer
449 * @fps: The fcoe context
450 *
451 * This routine allocates a page for frame trailers. The page is re-used if
452 * there is enough room left on it for the current trailer. If there isn't
453 * enough buffer left a new page is allocated for the trailer. Reference to
454 * the page from this function as well as the skbs using the page fragments
455 * ensure that the page is freed at the appropriate time.
456 *
457 * Returns: 0 for success
458 */
459int fcoe_get_paged_crc_eof(struct sk_buff *skb, int tlen,
460 struct fcoe_percpu_s *fps)
461{
462 struct page *page;
463
464 page = fps->crc_eof_page;
465 if (!page) {
466 page = alloc_page(GFP_ATOMIC);
467 if (!page)
468 return -ENOMEM;
469
470 fps->crc_eof_page = page;
471 fps->crc_eof_offset = 0;
472 }
473
474 get_page(page);
475 skb_fill_page_desc(skb, skb_shinfo(skb)->nr_frags, page,
476 fps->crc_eof_offset, tlen);
477 skb->len += tlen;
478 skb->data_len += tlen;
479 skb->truesize += tlen;
480 fps->crc_eof_offset += sizeof(struct fcoe_crc_eof);
481
482 if (fps->crc_eof_offset >= PAGE_SIZE) {
483 fps->crc_eof_page = NULL;
484 fps->crc_eof_offset = 0;
485 put_page(page);
486 }
487
488 return 0;
489}
490EXPORT_SYMBOL_GPL(fcoe_get_paged_crc_eof);
491
492/**
Yi Zoufdecf312011-01-28 16:04:55 -0800493 * fcoe_transport_lookup - find an fcoe transport that matches a netdev
494 * @netdev: The netdev to look for from all attached transports
495 *
496 * Returns : ptr to the fcoe transport that supports this netdev or NULL
497 * if not found.
498 *
499 * The ft_mutex should be held when this is called
500 */
501static struct fcoe_transport *fcoe_transport_lookup(struct net_device *netdev)
502{
503 struct fcoe_transport *ft = NULL;
504
505 list_for_each_entry(ft, &fcoe_transports, list)
506 if (ft->match && ft->match(netdev))
507 return ft;
508 return NULL;
509}
510
511/**
512 * fcoe_transport_attach - Attaches an FCoE transport
513 * @ft: The fcoe transport to be attached
514 *
515 * Returns : 0 for success
516 */
517int fcoe_transport_attach(struct fcoe_transport *ft)
518{
519 int rc = 0;
520
521 mutex_lock(&ft_mutex);
522 if (ft->attached) {
523 LIBFCOE_TRANSPORT_DBG("transport %s already attached\n",
524 ft->name);
525 rc = -EEXIST;
526 goto out_attach;
527 }
528
529 /* Add default transport to the tail */
530 if (strcmp(ft->name, FCOE_TRANSPORT_DEFAULT))
531 list_add(&ft->list, &fcoe_transports);
532 else
533 list_add_tail(&ft->list, &fcoe_transports);
534
535 ft->attached = true;
536 LIBFCOE_TRANSPORT_DBG("attaching transport %s\n", ft->name);
537
538out_attach:
539 mutex_unlock(&ft_mutex);
540 return rc;
541}
542EXPORT_SYMBOL(fcoe_transport_attach);
543
544/**
Yi Zou4ef7fb12011-04-01 16:06:30 -0700545 * fcoe_transport_detach - Detaches an FCoE transport
Yi Zoufdecf312011-01-28 16:04:55 -0800546 * @ft: The fcoe transport to be attached
547 *
548 * Returns : 0 for success
549 */
550int fcoe_transport_detach(struct fcoe_transport *ft)
551{
552 int rc = 0;
Yi Zou69922fc2011-04-01 16:06:19 -0700553 struct fcoe_netdev_mapping *nm = NULL, *tmp;
Yi Zoufdecf312011-01-28 16:04:55 -0800554
555 mutex_lock(&ft_mutex);
556 if (!ft->attached) {
557 LIBFCOE_TRANSPORT_DBG("transport %s already detached\n",
558 ft->name);
559 rc = -ENODEV;
560 goto out_attach;
561 }
562
Yi Zou69922fc2011-04-01 16:06:19 -0700563 /* remove netdev mapping for this transport as it is going away */
564 mutex_lock(&fn_mutex);
565 list_for_each_entry_safe(nm, tmp, &fcoe_netdevs, list) {
566 if (nm->ft == ft) {
567 LIBFCOE_TRANSPORT_DBG("transport %s going away, "
568 "remove its netdev mapping for %s\n",
569 ft->name, nm->netdev->name);
570 list_del(&nm->list);
571 kfree(nm);
572 }
573 }
574 mutex_unlock(&fn_mutex);
575
Yi Zoufdecf312011-01-28 16:04:55 -0800576 list_del(&ft->list);
577 ft->attached = false;
578 LIBFCOE_TRANSPORT_DBG("detaching transport %s\n", ft->name);
579
580out_attach:
581 mutex_unlock(&ft_mutex);
582 return rc;
583
584}
585EXPORT_SYMBOL(fcoe_transport_detach);
586
587static int fcoe_transport_show(char *buffer, const struct kernel_param *kp)
588{
589 int i, j;
590 struct fcoe_transport *ft = NULL;
591
592 i = j = sprintf(buffer, "Attached FCoE transports:");
593 mutex_lock(&ft_mutex);
594 list_for_each_entry(ft, &fcoe_transports, list) {
Yi Zoua01a5a52011-04-01 16:06:25 -0700595 if (i >= PAGE_SIZE - IFNAMSIZ)
Yi Zoufdecf312011-01-28 16:04:55 -0800596 break;
Yi Zoua01a5a52011-04-01 16:06:25 -0700597 i += snprintf(&buffer[i], IFNAMSIZ, "%s ", ft->name);
Yi Zoufdecf312011-01-28 16:04:55 -0800598 }
599 mutex_unlock(&ft_mutex);
600 if (i == j)
601 i += snprintf(&buffer[i], IFNAMSIZ, "none");
602 return i;
603}
604
605static int __init fcoe_transport_init(void)
606{
Bhanu Prakash Gollapudi70be6342011-02-25 15:03:17 -0800607 register_netdevice_notifier(&libfcoe_notifier);
Yi Zoufdecf312011-01-28 16:04:55 -0800608 return 0;
609}
610
Mark Rustad87098bd2012-06-06 11:59:48 -0700611static int fcoe_transport_exit(void)
Yi Zoufdecf312011-01-28 16:04:55 -0800612{
613 struct fcoe_transport *ft;
614
Bhanu Prakash Gollapudi70be6342011-02-25 15:03:17 -0800615 unregister_netdevice_notifier(&libfcoe_notifier);
Yi Zoufdecf312011-01-28 16:04:55 -0800616 mutex_lock(&ft_mutex);
617 list_for_each_entry(ft, &fcoe_transports, list)
618 printk(KERN_ERR "FCoE transport %s is still attached!\n",
619 ft->name);
620 mutex_unlock(&ft_mutex);
621 return 0;
622}
623
624
625static int fcoe_add_netdev_mapping(struct net_device *netdev,
626 struct fcoe_transport *ft)
627{
628 struct fcoe_netdev_mapping *nm;
629
630 nm = kmalloc(sizeof(*nm), GFP_KERNEL);
631 if (!nm) {
632 printk(KERN_ERR "Unable to allocate netdev_mapping");
633 return -ENOMEM;
634 }
635
636 nm->netdev = netdev;
637 nm->ft = ft;
638
Bhanu Prakash Gollapudi70be6342011-02-25 15:03:17 -0800639 mutex_lock(&fn_mutex);
Yi Zoufdecf312011-01-28 16:04:55 -0800640 list_add(&nm->list, &fcoe_netdevs);
Bhanu Prakash Gollapudi70be6342011-02-25 15:03:17 -0800641 mutex_unlock(&fn_mutex);
Yi Zoufdecf312011-01-28 16:04:55 -0800642 return 0;
643}
644
645
646static void fcoe_del_netdev_mapping(struct net_device *netdev)
647{
648 struct fcoe_netdev_mapping *nm = NULL, *tmp;
649
Bhanu Prakash Gollapudi70be6342011-02-25 15:03:17 -0800650 mutex_lock(&fn_mutex);
Yi Zoufdecf312011-01-28 16:04:55 -0800651 list_for_each_entry_safe(nm, tmp, &fcoe_netdevs, list) {
652 if (nm->netdev == netdev) {
653 list_del(&nm->list);
654 kfree(nm);
Bhanu Prakash Gollapudi70be6342011-02-25 15:03:17 -0800655 mutex_unlock(&fn_mutex);
Yi Zoufdecf312011-01-28 16:04:55 -0800656 return;
657 }
658 }
Bhanu Prakash Gollapudi70be6342011-02-25 15:03:17 -0800659 mutex_unlock(&fn_mutex);
Yi Zoufdecf312011-01-28 16:04:55 -0800660}
661
662
663/**
664 * fcoe_netdev_map_lookup - find the fcoe transport that matches the netdev on which
665 * it was created
666 *
667 * Returns : ptr to the fcoe transport that supports this netdev or NULL
668 * if not found.
669 *
670 * The ft_mutex should be held when this is called
671 */
672static struct fcoe_transport *fcoe_netdev_map_lookup(struct net_device *netdev)
673{
674 struct fcoe_transport *ft = NULL;
675 struct fcoe_netdev_mapping *nm;
676
Bhanu Prakash Gollapudi70be6342011-02-25 15:03:17 -0800677 mutex_lock(&fn_mutex);
Yi Zoufdecf312011-01-28 16:04:55 -0800678 list_for_each_entry(nm, &fcoe_netdevs, list) {
679 if (netdev == nm->netdev) {
680 ft = nm->ft;
Bhanu Prakash Gollapudi70be6342011-02-25 15:03:17 -0800681 mutex_unlock(&fn_mutex);
Yi Zoufdecf312011-01-28 16:04:55 -0800682 return ft;
683 }
684 }
685
Bhanu Prakash Gollapudi70be6342011-02-25 15:03:17 -0800686 mutex_unlock(&fn_mutex);
Yi Zoufdecf312011-01-28 16:04:55 -0800687 return NULL;
688}
689
690/**
691 * fcoe_if_to_netdev() - Parse a name buffer to get a net device
692 * @buffer: The name of the net device
693 *
694 * Returns: NULL or a ptr to net_device
695 */
696static struct net_device *fcoe_if_to_netdev(const char *buffer)
697{
698 char *cp;
699 char ifname[IFNAMSIZ + 2];
700
701 if (buffer) {
702 strlcpy(ifname, buffer, IFNAMSIZ);
703 cp = ifname + strlen(ifname);
704 while (--cp >= ifname && *cp == '\n')
705 *cp = '\0';
706 return dev_get_by_name(&init_net, ifname);
707 }
708 return NULL;
709}
710
711/**
Bhanu Prakash Gollapudi70be6342011-02-25 15:03:17 -0800712 * libfcoe_device_notification() - Handler for net device events
713 * @notifier: The context of the notification
714 * @event: The type of event
715 * @ptr: The net device that the event was on
716 *
717 * This function is called by the Ethernet driver in case of link change event.
718 *
719 * Returns: 0 for success
720 */
721static int libfcoe_device_notification(struct notifier_block *notifier,
722 ulong event, void *ptr)
723{
Jiri Pirko351638e2013-05-28 01:30:21 +0000724 struct net_device *netdev = netdev_notifier_info_to_dev(ptr);
Bhanu Prakash Gollapudi70be6342011-02-25 15:03:17 -0800725
726 switch (event) {
727 case NETDEV_UNREGISTER:
Robert Loveb99fbf62012-02-10 17:17:59 -0800728 LIBFCOE_TRANSPORT_DBG("NETDEV_UNREGISTER %s\n",
729 netdev->name);
Bhanu Prakash Gollapudi70be6342011-02-25 15:03:17 -0800730 fcoe_del_netdev_mapping(netdev);
731 break;
732 }
733 return NOTIFY_OK;
734}
735
Robert Love6a891b02012-11-27 06:53:30 +0000736ssize_t fcoe_ctlr_create_store(struct bus_type *bus,
737 const char *buf, size_t count)
738{
739 struct net_device *netdev = NULL;
740 struct fcoe_transport *ft = NULL;
Robert Love6a891b02012-11-27 06:53:30 +0000741 int rc = 0;
742 int err;
743
744 mutex_lock(&ft_mutex);
745
746 netdev = fcoe_if_to_netdev(buf);
747 if (!netdev) {
748 LIBFCOE_TRANSPORT_DBG("Invalid device %s.\n", buf);
749 rc = -ENODEV;
750 goto out_nodev;
751 }
752
753 ft = fcoe_netdev_map_lookup(netdev);
754 if (ft) {
755 LIBFCOE_TRANSPORT_DBG("transport %s already has existing "
756 "FCoE instance on %s.\n",
757 ft->name, netdev->name);
758 rc = -EEXIST;
759 goto out_putdev;
760 }
761
762 ft = fcoe_transport_lookup(netdev);
763 if (!ft) {
764 LIBFCOE_TRANSPORT_DBG("no FCoE transport found for %s.\n",
765 netdev->name);
766 rc = -ENODEV;
767 goto out_putdev;
768 }
769
770 /* pass to transport create */
771 err = ft->alloc ? ft->alloc(netdev) : -ENODEV;
772 if (err) {
773 fcoe_del_netdev_mapping(netdev);
774 rc = -ENOMEM;
775 goto out_putdev;
776 }
777
778 err = fcoe_add_netdev_mapping(netdev, ft);
779 if (err) {
780 LIBFCOE_TRANSPORT_DBG("failed to add new netdev mapping "
781 "for FCoE transport %s for %s.\n",
782 ft->name, netdev->name);
783 rc = -ENODEV;
784 goto out_putdev;
785 }
786
Robert Lovea2ceb1f2013-06-11 07:28:19 +0000787 LIBFCOE_TRANSPORT_DBG("transport %s succeeded to create fcoe on %s.\n",
788 ft->name, netdev->name);
Robert Love6a891b02012-11-27 06:53:30 +0000789
790out_putdev:
791 dev_put(netdev);
792out_nodev:
793 mutex_unlock(&ft_mutex);
794 if (rc)
795 return rc;
796 return count;
797}
798
799ssize_t fcoe_ctlr_destroy_store(struct bus_type *bus,
800 const char *buf, size_t count)
801{
802 int rc = -ENODEV;
803 struct net_device *netdev = NULL;
804 struct fcoe_transport *ft = NULL;
805
806 mutex_lock(&ft_mutex);
807
808 netdev = fcoe_if_to_netdev(buf);
809 if (!netdev) {
810 LIBFCOE_TRANSPORT_DBG("invalid device %s.\n", buf);
811 goto out_nodev;
812 }
813
814 ft = fcoe_netdev_map_lookup(netdev);
815 if (!ft) {
816 LIBFCOE_TRANSPORT_DBG("no FCoE transport found for %s.\n",
817 netdev->name);
818 goto out_putdev;
819 }
820
821 /* pass to transport destroy */
822 rc = ft->destroy(netdev);
823 if (rc)
824 goto out_putdev;
825
826 fcoe_del_netdev_mapping(netdev);
827 LIBFCOE_TRANSPORT_DBG("transport %s %s to destroy fcoe on %s.\n",
828 ft->name, (rc) ? "failed" : "succeeded",
829 netdev->name);
830 rc = count; /* required for successful return */
831out_putdev:
832 dev_put(netdev);
833out_nodev:
834 mutex_unlock(&ft_mutex);
835 return rc;
836}
837EXPORT_SYMBOL(fcoe_ctlr_destroy_store);
Bhanu Prakash Gollapudi70be6342011-02-25 15:03:17 -0800838
839/**
Yi Zoufdecf312011-01-28 16:04:55 -0800840 * fcoe_transport_create() - Create a fcoe interface
841 * @buffer: The name of the Ethernet interface to create on
842 * @kp: The associated kernel param
843 *
844 * Called from sysfs. This holds the ft_mutex while calling the
845 * registered fcoe transport's create function.
846 *
847 * Returns: 0 for success
848 */
849static int fcoe_transport_create(const char *buffer, struct kernel_param *kp)
850{
851 int rc = -ENODEV;
852 struct net_device *netdev = NULL;
853 struct fcoe_transport *ft = NULL;
854 enum fip_state fip_mode = (enum fip_state)(long)kp->arg;
855
Robert Loveb3960af2011-04-01 16:05:53 -0700856 mutex_lock(&ft_mutex);
857
Yi Zoufdecf312011-01-28 16:04:55 -0800858 netdev = fcoe_if_to_netdev(buffer);
859 if (!netdev) {
860 LIBFCOE_TRANSPORT_DBG("Invalid device %s.\n", buffer);
861 goto out_nodev;
862 }
863
864 ft = fcoe_netdev_map_lookup(netdev);
865 if (ft) {
866 LIBFCOE_TRANSPORT_DBG("transport %s already has existing "
867 "FCoE instance on %s.\n",
868 ft->name, netdev->name);
869 rc = -EEXIST;
870 goto out_putdev;
871 }
872
873 ft = fcoe_transport_lookup(netdev);
874 if (!ft) {
875 LIBFCOE_TRANSPORT_DBG("no FCoE transport found for %s.\n",
876 netdev->name);
877 goto out_putdev;
878 }
879
880 rc = fcoe_add_netdev_mapping(netdev, ft);
881 if (rc) {
882 LIBFCOE_TRANSPORT_DBG("failed to add new netdev mapping "
883 "for FCoE transport %s for %s.\n",
884 ft->name, netdev->name);
885 goto out_putdev;
886 }
887
888 /* pass to transport create */
889 rc = ft->create ? ft->create(netdev, fip_mode) : -ENODEV;
890 if (rc)
891 fcoe_del_netdev_mapping(netdev);
892
893 LIBFCOE_TRANSPORT_DBG("transport %s %s to create fcoe on %s.\n",
894 ft->name, (rc) ? "failed" : "succeeded",
895 netdev->name);
896
897out_putdev:
898 dev_put(netdev);
899out_nodev:
900 mutex_unlock(&ft_mutex);
Robert Loveb3960af2011-04-01 16:05:53 -0700901 return rc;
Yi Zoufdecf312011-01-28 16:04:55 -0800902}
903
904/**
905 * fcoe_transport_destroy() - Destroy a FCoE interface
906 * @buffer: The name of the Ethernet interface to be destroyed
907 * @kp: The associated kernel parameter
908 *
909 * Called from sysfs. This holds the ft_mutex while calling the
910 * registered fcoe transport's destroy function.
911 *
912 * Returns: 0 for success
913 */
914static int fcoe_transport_destroy(const char *buffer, struct kernel_param *kp)
915{
916 int rc = -ENODEV;
917 struct net_device *netdev = NULL;
918 struct fcoe_transport *ft = NULL;
919
Robert Loveb3960af2011-04-01 16:05:53 -0700920 mutex_lock(&ft_mutex);
921
Yi Zoufdecf312011-01-28 16:04:55 -0800922 netdev = fcoe_if_to_netdev(buffer);
923 if (!netdev) {
924 LIBFCOE_TRANSPORT_DBG("invalid device %s.\n", buffer);
925 goto out_nodev;
926 }
927
928 ft = fcoe_netdev_map_lookup(netdev);
929 if (!ft) {
930 LIBFCOE_TRANSPORT_DBG("no FCoE transport found for %s.\n",
931 netdev->name);
932 goto out_putdev;
933 }
934
935 /* pass to transport destroy */
936 rc = ft->destroy ? ft->destroy(netdev) : -ENODEV;
937 fcoe_del_netdev_mapping(netdev);
938 LIBFCOE_TRANSPORT_DBG("transport %s %s to destroy fcoe on %s.\n",
939 ft->name, (rc) ? "failed" : "succeeded",
940 netdev->name);
941
942out_putdev:
943 dev_put(netdev);
944out_nodev:
945 mutex_unlock(&ft_mutex);
Robert Loveb3960af2011-04-01 16:05:53 -0700946 return rc;
Yi Zoufdecf312011-01-28 16:04:55 -0800947}
948
949/**
950 * fcoe_transport_disable() - Disables a FCoE interface
951 * @buffer: The name of the Ethernet interface to be disabled
952 * @kp: The associated kernel parameter
953 *
954 * Called from sysfs.
955 *
956 * Returns: 0 for success
957 */
958static int fcoe_transport_disable(const char *buffer, struct kernel_param *kp)
959{
960 int rc = -ENODEV;
961 struct net_device *netdev = NULL;
962 struct fcoe_transport *ft = NULL;
963
Robert Loveb3960af2011-04-01 16:05:53 -0700964 mutex_lock(&ft_mutex);
965
Yi Zoufdecf312011-01-28 16:04:55 -0800966 netdev = fcoe_if_to_netdev(buffer);
967 if (!netdev)
968 goto out_nodev;
969
970 ft = fcoe_netdev_map_lookup(netdev);
971 if (!ft)
972 goto out_putdev;
973
974 rc = ft->disable ? ft->disable(netdev) : -ENODEV;
975
976out_putdev:
977 dev_put(netdev);
978out_nodev:
979 mutex_unlock(&ft_mutex);
Al Viro4f670ff2012-12-07 14:10:14 +0000980 return rc;
Yi Zoufdecf312011-01-28 16:04:55 -0800981}
982
983/**
984 * fcoe_transport_enable() - Enables a FCoE interface
985 * @buffer: The name of the Ethernet interface to be enabled
986 * @kp: The associated kernel parameter
987 *
988 * Called from sysfs.
989 *
990 * Returns: 0 for success
991 */
992static int fcoe_transport_enable(const char *buffer, struct kernel_param *kp)
993{
994 int rc = -ENODEV;
995 struct net_device *netdev = NULL;
996 struct fcoe_transport *ft = NULL;
997
Robert Loveb3960af2011-04-01 16:05:53 -0700998 mutex_lock(&ft_mutex);
999
Yi Zoufdecf312011-01-28 16:04:55 -08001000 netdev = fcoe_if_to_netdev(buffer);
1001 if (!netdev)
1002 goto out_nodev;
1003
1004 ft = fcoe_netdev_map_lookup(netdev);
1005 if (!ft)
1006 goto out_putdev;
1007
1008 rc = ft->enable ? ft->enable(netdev) : -ENODEV;
1009
1010out_putdev:
1011 dev_put(netdev);
1012out_nodev:
1013 mutex_unlock(&ft_mutex);
Robert Loveb3960af2011-04-01 16:05:53 -07001014 return rc;
Yi Zoufdecf312011-01-28 16:04:55 -08001015}
1016
1017/**
1018 * libfcoe_init() - Initialization routine for libfcoe.ko
1019 */
1020static int __init libfcoe_init(void)
1021{
Robert Love9a74e882012-05-22 19:06:21 -07001022 int rc = 0;
Yi Zoufdecf312011-01-28 16:04:55 -08001023
Robert Love9a74e882012-05-22 19:06:21 -07001024 rc = fcoe_transport_init();
1025 if (rc)
1026 return rc;
1027
1028 rc = fcoe_sysfs_setup();
1029 if (rc)
1030 fcoe_transport_exit();
1031
1032 return rc;
Yi Zoufdecf312011-01-28 16:04:55 -08001033}
1034module_init(libfcoe_init);
1035
1036/**
1037 * libfcoe_exit() - Tear down libfcoe.ko
1038 */
1039static void __exit libfcoe_exit(void)
1040{
Robert Love9a74e882012-05-22 19:06:21 -07001041 fcoe_sysfs_teardown();
Yi Zoufdecf312011-01-28 16:04:55 -08001042 fcoe_transport_exit();
1043}
1044module_exit(libfcoe_exit);