Merge tag 'v4.6-rc3' into drm-intel-next-queued
[deliverable/linux.git] / drivers / scsi / fcoe / fcoe_transport.c
CommitLineData
fdecf31b
YZ
1/*
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>
8597ae8b 26#include <linux/crc32.h>
fdecf31b
YZ
27#include <scsi/libfcoe.h>
28
29#include "libfcoe.h"
30
e01efc33
YZ
31MODULE_AUTHOR("Open-FCoE.org");
32MODULE_DESCRIPTION("FIP discovery protocol and FCoE transport for FCoE HBAs");
33MODULE_LICENSE("GPL v2");
34
fdecf31b
YZ
35static 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 *);
70be6344
BPG
42static int libfcoe_device_notification(struct notifier_block *notifier,
43 ulong event, void *ptr);
fdecf31b
YZ
44
45static LIST_HEAD(fcoe_transports);
fdecf31b 46static DEFINE_MUTEX(ft_mutex);
70be6344
BPG
47static LIST_HEAD(fcoe_netdevs);
48static DEFINE_MUTEX(fn_mutex);
fdecf31b 49
e01efc33
YZ
50unsigned 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
fdecf31b
YZ
54module_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");
bd0a1d6c 61MODULE_PARM_DESC(create, " Creates fcoe instance on an ethernet interface");
fdecf31b
YZ
62
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");
bd0a1d6c 71MODULE_PARM_DESC(destroy, " Destroys fcoe instance on an ethernet interface");
fdecf31b
YZ
72
73module_param_call(enable, fcoe_transport_enable, NULL, NULL, S_IWUSR);
74__MODULE_PARM_TYPE(enable, "string");
bd0a1d6c 75MODULE_PARM_DESC(enable, " Enables fcoe on an ethernet interface.");
fdecf31b
YZ
76
77module_param_call(disable, fcoe_transport_disable, NULL, NULL, S_IWUSR);
78__MODULE_PARM_TYPE(disable, "string");
bd0a1d6c 79MODULE_PARM_DESC(disable, " Disables fcoe on an ethernet interface.");
fdecf31b 80
70be6344
BPG
81/* notification function for packets from net device */
82static struct notifier_block libfcoe_notifier = {
83 .notifier_call = libfcoe_device_notification,
84};
85
03702689
YZ
86/**
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);
008eb736 96 struct ethtool_link_ksettings ecmd;
03702689 97
008eb736 98 if (!__ethtool_get_link_ksettings(netdev, &ecmd)) {
b8d23dc6
CL
99 lport->link_supported_speeds &= ~(FC_PORTSPEED_1GBIT |
100 FC_PORTSPEED_10GBIT |
101 FC_PORTSPEED_20GBIT |
102 FC_PORTSPEED_40GBIT);
103
008eb736
DD
104 if (ecmd.link_modes.supported[0] & (
105 SUPPORTED_1000baseT_Half |
106 SUPPORTED_1000baseT_Full |
107 SUPPORTED_1000baseKX_Full))
03702689 108 lport->link_supported_speeds |= FC_PORTSPEED_1GBIT;
b8d23dc6 109
008eb736
DD
110 if (ecmd.link_modes.supported[0] & (
111 SUPPORTED_10000baseT_Full |
112 SUPPORTED_10000baseKX4_Full |
113 SUPPORTED_10000baseKR_Full |
114 SUPPORTED_10000baseR_FEC))
b8d23dc6
CL
115 lport->link_supported_speeds |= FC_PORTSPEED_10GBIT;
116
008eb736
DD
117 if (ecmd.link_modes.supported[0] & (
118 SUPPORTED_20000baseMLD2_Full |
119 SUPPORTED_20000baseKR2_Full))
b8d23dc6
CL
120 lport->link_supported_speeds |= FC_PORTSPEED_20GBIT;
121
008eb736
DD
122 if (ecmd.link_modes.supported[0] & (
123 SUPPORTED_40000baseKR4_Full |
124 SUPPORTED_40000baseCR4_Full |
125 SUPPORTED_40000baseSR4_Full |
126 SUPPORTED_40000baseLR4_Full))
b8d23dc6
CL
127 lport->link_supported_speeds |= FC_PORTSPEED_40GBIT;
128
008eb736 129 switch (ecmd.base.speed) {
03702689
YZ
130 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;
b8d23dc6
CL
136 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;
03702689
YZ
145 }
146 return 0;
147 }
148 return -1;
149}
150EXPORT_SYMBOL_GPL(fcoe_link_speed_update);
151
57c2728f
YZ
152/**
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 */
814740d5
BPG
161void __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;
1bd49b48 167 struct fc_stats *stats;
814740d5
BPG
168 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) {
1bd49b48
VD
177 stats = per_cpu_ptr(lport->stats, cpu);
178 lfc += stats->LinkFailureCount;
179 vlfc += stats->VLinkFailureCount;
180 mdac += stats->MissDiscAdvCount;
814740d5
BPG
181 }
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
57c2728f
YZ
190/**
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);
418a8cfe
YZ
214 struct fc_els_lesb *fc_lesb;
215
216 fc_lesb = (struct fc_els_lesb *)(&ctlr_dev->lesb);
217 __fcoe_get_lesb(fip->lp, fc_lesb, netdev);
57c2728f
YZ
218}
219EXPORT_SYMBOL_GPL(fcoe_ctlr_get_lesb);
220
d834895c
BPG
221void 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
8597ae8b
BPG
293/**
294 * 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;
9e903e08 315 len = skb_frag_size(frag);
8597ae8b
BPG
316 while (len > 0) {
317 clen = min(len, PAGE_SIZE - (off & ~PAGE_MASK));
165c68d5 318 data = kmap_atomic(
77dfce07 319 skb_frag_page(frag) + (off >> PAGE_SHIFT));
8597ae8b 320 crc = crc32(crc, data + (off & ~PAGE_MASK), clen);
77dfce07 321 kunmap_atomic(data);
8597ae8b
BPG
322 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
fdecf31b
YZ
492/**
493 * 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/**
4ef7fb15 545 * fcoe_transport_detach - Detaches an FCoE transport
fdecf31b
YZ
546 * @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;
69922fcd 553 struct fcoe_netdev_mapping *nm = NULL, *tmp;
fdecf31b
YZ
554
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
69922fcd
YZ
563 /* 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
fdecf31b
YZ
576 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) {
a01a5a57 595 if (i >= PAGE_SIZE - IFNAMSIZ)
fdecf31b 596 break;
a01a5a57 597 i += snprintf(&buffer[i], IFNAMSIZ, "%s ", ft->name);
fdecf31b
YZ
598 }
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{
70be6344 607 register_netdevice_notifier(&libfcoe_notifier);
fdecf31b
YZ
608 return 0;
609}
610
87098bdd 611static int fcoe_transport_exit(void)
fdecf31b
YZ
612{
613 struct fcoe_transport *ft;
614
70be6344 615 unregister_netdevice_notifier(&libfcoe_notifier);
fdecf31b
YZ
616 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
70be6344 639 mutex_lock(&fn_mutex);
fdecf31b 640 list_add(&nm->list, &fcoe_netdevs);
70be6344 641 mutex_unlock(&fn_mutex);
fdecf31b
YZ
642 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
70be6344 650 mutex_lock(&fn_mutex);
fdecf31b
YZ
651 list_for_each_entry_safe(nm, tmp, &fcoe_netdevs, list) {
652 if (nm->netdev == netdev) {
653 list_del(&nm->list);
654 kfree(nm);
70be6344 655 mutex_unlock(&fn_mutex);
fdecf31b
YZ
656 return;
657 }
658 }
70be6344 659 mutex_unlock(&fn_mutex);
fdecf31b
YZ
660}
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
70be6344 677 mutex_lock(&fn_mutex);
fdecf31b
YZ
678 list_for_each_entry(nm, &fcoe_netdevs, list) {
679 if (netdev == nm->netdev) {
680 ft = nm->ft;
70be6344 681 mutex_unlock(&fn_mutex);
fdecf31b
YZ
682 return ft;
683 }
684 }
685
70be6344 686 mutex_unlock(&fn_mutex);
fdecf31b
YZ
687 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
70be6344
BPG
711/**
712 * 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{
351638e7 724 struct net_device *netdev = netdev_notifier_info_to_dev(ptr);
70be6344
BPG
725
726 switch (event) {
727 case NETDEV_UNREGISTER:
b99fbf6a
RL
728 LIBFCOE_TRANSPORT_DBG("NETDEV_UNREGISTER %s\n",
729 netdev->name);
70be6344
BPG
730 fcoe_del_netdev_mapping(netdev);
731 break;
732 }
733 return NOTIFY_OK;
734}
735
6a891b07
RL
736ssize_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;
6a891b07
RL
741 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
a2ceb1fb
RL
787 LIBFCOE_TRANSPORT_DBG("transport %s succeeded to create fcoe on %s.\n",
788 ft->name, netdev->name);
6a891b07
RL
789
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);
70be6344 838
fdecf31b
YZ
839/**
840 * 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
b3960afe
RL
856 mutex_lock(&ft_mutex);
857
fdecf31b
YZ
858 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);
b3960afe 901 return rc;
fdecf31b
YZ
902}
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
b3960afe
RL
920 mutex_lock(&ft_mutex);
921
fdecf31b
YZ
922 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);
b3960afe 946 return rc;
fdecf31b
YZ
947}
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
b3960afe
RL
964 mutex_lock(&ft_mutex);
965
fdecf31b
YZ
966 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);
4f670ff8 980 return rc;
fdecf31b
YZ
981}
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
b3960afe
RL
998 mutex_lock(&ft_mutex);
999
fdecf31b
YZ
1000 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);
b3960afe 1014 return rc;
fdecf31b
YZ
1015}
1016
1017/**
1018 * libfcoe_init() - Initialization routine for libfcoe.ko
1019 */
1020static int __init libfcoe_init(void)
1021{
9a74e884 1022 int rc = 0;
fdecf31b 1023
9a74e884
RL
1024 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;
fdecf31b
YZ
1033}
1034module_init(libfcoe_init);
1035
1036/**
1037 * libfcoe_exit() - Tear down libfcoe.ko
1038 */
1039static void __exit libfcoe_exit(void)
1040{
9a74e884 1041 fcoe_sysfs_teardown();
fdecf31b
YZ
1042 fcoe_transport_exit();
1043}
1044module_exit(libfcoe_exit);
This page took 0.348144 seconds and 5 git commands to generate.