2 * Copyright(c) 2008 - 2011 Intel Corporation. All rights reserved.
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.
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
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.
17 * Maintained at www.Open-FCoE.org
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>
26 #include <linux/crc32.h>
27 #include <scsi/libfcoe.h>
31 MODULE_AUTHOR("Open-FCoE.org");
32 MODULE_DESCRIPTION("FIP discovery protocol and FCoE transport for FCoE HBAs");
33 MODULE_LICENSE("GPL v2");
35 static int fcoe_transport_create(const char *, const struct kernel_param
*);
36 static int fcoe_transport_destroy(const char *, const struct kernel_param
*);
37 static int fcoe_transport_show(char *buffer
, const struct kernel_param
*kp
);
38 static struct fcoe_transport
*fcoe_transport_lookup(struct net_device
*device
);
39 static struct fcoe_transport
*fcoe_netdev_map_lookup(struct net_device
*device
);
40 static int fcoe_transport_enable(const char *, const struct kernel_param
*);
41 static int fcoe_transport_disable(const char *, const struct kernel_param
*);
42 static int libfcoe_device_notification(struct notifier_block
*notifier
,
43 ulong event
, void *ptr
);
45 static LIST_HEAD(fcoe_transports
);
46 static DEFINE_MUTEX(ft_mutex
);
47 static LIST_HEAD(fcoe_netdevs
);
48 static DEFINE_MUTEX(fn_mutex
);
50 unsigned int libfcoe_debug_logging
;
51 module_param_named(debug_logging
, libfcoe_debug_logging
, int, S_IRUGO
|S_IWUSR
);
52 MODULE_PARM_DESC(debug_logging
, "a bit mask of logging levels");
54 module_param_call(show
, NULL
, fcoe_transport_show
, NULL
, S_IRUSR
);
55 __MODULE_PARM_TYPE(show
, "string");
56 MODULE_PARM_DESC(show
, " Show attached FCoE transports");
58 module_param_call(create
, fcoe_transport_create
, NULL
,
59 (void *)FIP_MODE_FABRIC
, S_IWUSR
);
60 __MODULE_PARM_TYPE(create
, "string");
61 MODULE_PARM_DESC(create
, " Creates fcoe instance on an ethernet interface");
63 module_param_call(create_vn2vn
, fcoe_transport_create
, NULL
,
64 (void *)FIP_MODE_VN2VN
, S_IWUSR
);
65 __MODULE_PARM_TYPE(create_vn2vn
, "string");
66 MODULE_PARM_DESC(create_vn2vn
, " Creates a VN_node to VN_node FCoE instance "
67 "on an Ethernet interface");
69 module_param_call(destroy
, fcoe_transport_destroy
, NULL
, NULL
, S_IWUSR
);
70 __MODULE_PARM_TYPE(destroy
, "string");
71 MODULE_PARM_DESC(destroy
, " Destroys fcoe instance on an ethernet interface");
73 module_param_call(enable
, fcoe_transport_enable
, NULL
, NULL
, S_IWUSR
);
74 __MODULE_PARM_TYPE(enable
, "string");
75 MODULE_PARM_DESC(enable
, " Enables fcoe on an ethernet interface.");
77 module_param_call(disable
, fcoe_transport_disable
, NULL
, NULL
, S_IWUSR
);
78 __MODULE_PARM_TYPE(disable
, "string");
79 MODULE_PARM_DESC(disable
, " Disables fcoe on an ethernet interface.");
81 /* notification function for packets from net device */
82 static struct notifier_block libfcoe_notifier
= {
83 .notifier_call
= libfcoe_device_notification
,
88 #define SPEED_2000 2000
89 #define SPEED_4000 4000
90 #define SPEED_8000 8000
91 #define SPEED_16000 16000
92 #define SPEED_32000 32000
94 } fcoe_port_speed_mapping
[] = {
95 { FC_PORTSPEED_1GBIT
, SPEED_1000
},
96 { FC_PORTSPEED_2GBIT
, SPEED_2000
},
97 { FC_PORTSPEED_4GBIT
, SPEED_4000
},
98 { FC_PORTSPEED_8GBIT
, SPEED_8000
},
99 { FC_PORTSPEED_10GBIT
, SPEED_10000
},
100 { FC_PORTSPEED_16GBIT
, SPEED_16000
},
101 { FC_PORTSPEED_20GBIT
, SPEED_20000
},
102 { FC_PORTSPEED_25GBIT
, SPEED_25000
},
103 { FC_PORTSPEED_32GBIT
, SPEED_32000
},
104 { FC_PORTSPEED_40GBIT
, SPEED_40000
},
105 { FC_PORTSPEED_50GBIT
, SPEED_50000
},
106 { FC_PORTSPEED_100GBIT
, SPEED_100000
},
109 static inline u32
eth2fc_speed(u32 eth_port_speed
)
113 for (i
= 0; i
< ARRAY_SIZE(fcoe_port_speed_mapping
); i
++) {
114 if (fcoe_port_speed_mapping
[i
].eth_port_speed
== eth_port_speed
)
115 return fcoe_port_speed_mapping
[i
].fc_port_speed
;
118 return FC_PORTSPEED_UNKNOWN
;
122 * fcoe_link_speed_update() - Update the supported and actual link speeds
123 * @lport: The local port to update speeds for
125 * Returns: 0 if the ethtool query was successful
126 * -1 if the ethtool query failed
128 int fcoe_link_speed_update(struct fc_lport
*lport
)
130 struct net_device
*netdev
= fcoe_get_netdev(lport
);
131 struct ethtool_link_ksettings ecmd
;
133 if (!__ethtool_get_link_ksettings(netdev
, &ecmd
)) {
134 lport
->link_supported_speeds
&= ~(FC_PORTSPEED_1GBIT
|
135 FC_PORTSPEED_10GBIT
|
136 FC_PORTSPEED_20GBIT
|
137 FC_PORTSPEED_40GBIT
);
139 if (ecmd
.link_modes
.supported
[0] & (
140 SUPPORTED_1000baseT_Half
|
141 SUPPORTED_1000baseT_Full
|
142 SUPPORTED_1000baseKX_Full
))
143 lport
->link_supported_speeds
|= FC_PORTSPEED_1GBIT
;
145 if (ecmd
.link_modes
.supported
[0] & (
146 SUPPORTED_10000baseT_Full
|
147 SUPPORTED_10000baseKX4_Full
|
148 SUPPORTED_10000baseKR_Full
|
149 SUPPORTED_10000baseR_FEC
))
150 lport
->link_supported_speeds
|= FC_PORTSPEED_10GBIT
;
152 if (ecmd
.link_modes
.supported
[0] & (
153 SUPPORTED_20000baseMLD2_Full
|
154 SUPPORTED_20000baseKR2_Full
))
155 lport
->link_supported_speeds
|= FC_PORTSPEED_20GBIT
;
157 if (ecmd
.link_modes
.supported
[0] & (
158 SUPPORTED_40000baseKR4_Full
|
159 SUPPORTED_40000baseCR4_Full
|
160 SUPPORTED_40000baseSR4_Full
|
161 SUPPORTED_40000baseLR4_Full
))
162 lport
->link_supported_speeds
|= FC_PORTSPEED_40GBIT
;
164 lport
->link_speed
= eth2fc_speed(ecmd
.base
.speed
);
169 EXPORT_SYMBOL_GPL(fcoe_link_speed_update
);
172 * __fcoe_get_lesb() - Get the Link Error Status Block (LESB) for a given lport
173 * @lport: The local port to update speeds for
174 * @fc_lesb: Pointer to the LESB to be filled up
175 * @netdev: Pointer to the netdev that is associated with the lport
177 * Note, the Link Error Status Block (LESB) for FCoE is defined in FC-BB-6
178 * Clause 7.11 in v1.04.
180 void __fcoe_get_lesb(struct fc_lport
*lport
,
181 struct fc_els_lesb
*fc_lesb
,
182 struct net_device
*netdev
)
186 struct fc_stats
*stats
;
187 struct fcoe_fc_els_lesb
*lesb
;
188 struct rtnl_link_stats64 temp
;
193 lesb
= (struct fcoe_fc_els_lesb
*)fc_lesb
;
194 memset(lesb
, 0, sizeof(*lesb
));
195 for_each_possible_cpu(cpu
) {
196 stats
= per_cpu_ptr(lport
->stats
, cpu
);
197 lfc
+= stats
->LinkFailureCount
;
198 vlfc
+= stats
->VLinkFailureCount
;
199 mdac
+= stats
->MissDiscAdvCount
;
201 lesb
->lesb_link_fail
= htonl(lfc
);
202 lesb
->lesb_vlink_fail
= htonl(vlfc
);
203 lesb
->lesb_miss_fka
= htonl(mdac
);
204 lesb
->lesb_fcs_error
=
205 htonl(dev_get_stats(netdev
, &temp
)->rx_crc_errors
);
207 EXPORT_SYMBOL_GPL(__fcoe_get_lesb
);
210 * fcoe_get_lesb() - Fill the FCoE Link Error Status Block
211 * @lport: the local port
212 * @fc_lesb: the link error status block
214 void fcoe_get_lesb(struct fc_lport
*lport
,
215 struct fc_els_lesb
*fc_lesb
)
217 struct net_device
*netdev
= fcoe_get_netdev(lport
);
219 __fcoe_get_lesb(lport
, fc_lesb
, netdev
);
221 EXPORT_SYMBOL_GPL(fcoe_get_lesb
);
224 * fcoe_ctlr_get_lesb() - Get the Link Error Status Block (LESB) for a given
225 * fcoe controller device
226 * @ctlr_dev: The given fcoe controller device
229 void fcoe_ctlr_get_lesb(struct fcoe_ctlr_device
*ctlr_dev
)
231 struct fcoe_ctlr
*fip
= fcoe_ctlr_device_priv(ctlr_dev
);
232 struct net_device
*netdev
= fcoe_get_netdev(fip
->lp
);
233 struct fc_els_lesb
*fc_lesb
;
235 fc_lesb
= (struct fc_els_lesb
*)(&ctlr_dev
->lesb
);
236 __fcoe_get_lesb(fip
->lp
, fc_lesb
, netdev
);
238 EXPORT_SYMBOL_GPL(fcoe_ctlr_get_lesb
);
240 void fcoe_wwn_to_str(u64 wwn
, char *buf
, int len
)
244 u64_to_wwn(wwn
, wwpn
);
245 snprintf(buf
, len
, "%02x%02x%02x%02x%02x%02x%02x%02x",
246 wwpn
[0], wwpn
[1], wwpn
[2], wwpn
[3],
247 wwpn
[4], wwpn
[5], wwpn
[6], wwpn
[7]);
249 EXPORT_SYMBOL_GPL(fcoe_wwn_to_str
);
252 * fcoe_validate_vport_create() - Validate a vport before creating it
253 * @vport: NPIV port to be created
255 * This routine is meant to add validation for a vport before creating it
256 * via fcoe_vport_create().
257 * Current validations are:
258 * - WWPN supplied is unique for given lport
260 int fcoe_validate_vport_create(struct fc_vport
*vport
)
262 struct Scsi_Host
*shost
= vport_to_shost(vport
);
263 struct fc_lport
*n_port
= shost_priv(shost
);
264 struct fc_lport
*vn_port
;
268 mutex_lock(&n_port
->lp_mutex
);
270 fcoe_wwn_to_str(vport
->port_name
, buf
, sizeof(buf
));
271 /* Check if the wwpn is not same as that of the lport */
272 if (!memcmp(&n_port
->wwpn
, &vport
->port_name
, sizeof(u64
))) {
273 LIBFCOE_TRANSPORT_DBG("vport WWPN 0x%s is same as that of the "
274 "base port WWPN\n", buf
);
279 /* Check if there is any existing vport with same wwpn */
280 list_for_each_entry(vn_port
, &n_port
->vports
, list
) {
281 if (!memcmp(&vn_port
->wwpn
, &vport
->port_name
, sizeof(u64
))) {
282 LIBFCOE_TRANSPORT_DBG("vport with given WWPN 0x%s "
283 "already exists\n", buf
);
289 mutex_unlock(&n_port
->lp_mutex
);
292 EXPORT_SYMBOL_GPL(fcoe_validate_vport_create
);
295 * fcoe_get_wwn() - Get the world wide name from LLD if it supports it
296 * @netdev: the associated net device
297 * @wwn: the output WWN
298 * @type: the type of WWN (WWPN or WWNN)
300 * Returns: 0 for success
302 int fcoe_get_wwn(struct net_device
*netdev
, u64
*wwn
, int type
)
304 const struct net_device_ops
*ops
= netdev
->netdev_ops
;
306 if (ops
->ndo_fcoe_get_wwn
)
307 return ops
->ndo_fcoe_get_wwn(netdev
, wwn
, type
);
310 EXPORT_SYMBOL_GPL(fcoe_get_wwn
);
313 * fcoe_fc_crc() - Calculates the CRC for a given frame
314 * @fp: The frame to be checksumed
316 * This uses crc32() routine to calculate the CRC for a frame
318 * Return: The 32 bit CRC value
320 u32
fcoe_fc_crc(struct fc_frame
*fp
)
322 struct sk_buff
*skb
= fp_skb(fp
);
323 struct skb_frag_struct
*frag
;
325 unsigned long off
, len
, clen
;
329 crc
= crc32(~0, skb
->data
, skb_headlen(skb
));
331 for (i
= 0; i
< skb_shinfo(skb
)->nr_frags
; i
++) {
332 frag
= &skb_shinfo(skb
)->frags
[i
];
333 off
= frag
->page_offset
;
334 len
= skb_frag_size(frag
);
336 clen
= min(len
, PAGE_SIZE
- (off
& ~PAGE_MASK
));
338 skb_frag_page(frag
) + (off
>> PAGE_SHIFT
));
339 crc
= crc32(crc
, data
+ (off
& ~PAGE_MASK
), clen
);
347 EXPORT_SYMBOL_GPL(fcoe_fc_crc
);
350 * fcoe_start_io() - Start FCoE I/O
351 * @skb: The packet to be transmitted
353 * This routine is called from the net device to start transmitting
356 * Returns: 0 for success
358 int fcoe_start_io(struct sk_buff
*skb
)
360 struct sk_buff
*nskb
;
363 nskb
= skb_clone(skb
, GFP_ATOMIC
);
366 rc
= dev_queue_xmit(nskb
);
372 EXPORT_SYMBOL_GPL(fcoe_start_io
);
376 * fcoe_clean_pending_queue() - Dequeue a skb and free it
377 * @lport: The local port to dequeue a skb on
379 void fcoe_clean_pending_queue(struct fc_lport
*lport
)
381 struct fcoe_port
*port
= lport_priv(lport
);
384 spin_lock_bh(&port
->fcoe_pending_queue
.lock
);
385 while ((skb
= __skb_dequeue(&port
->fcoe_pending_queue
)) != NULL
) {
386 spin_unlock_bh(&port
->fcoe_pending_queue
.lock
);
388 spin_lock_bh(&port
->fcoe_pending_queue
.lock
);
390 spin_unlock_bh(&port
->fcoe_pending_queue
.lock
);
392 EXPORT_SYMBOL_GPL(fcoe_clean_pending_queue
);
395 * fcoe_check_wait_queue() - Attempt to clear the transmit backlog
396 * @lport: The local port whose backlog is to be cleared
398 * This empties the wait_queue, dequeues the head of the wait_queue queue
399 * and calls fcoe_start_io() for each packet. If all skb have been
400 * transmitted it returns the qlen. If an error occurs it restores
401 * wait_queue (to try again later) and returns -1.
403 * The wait_queue is used when the skb transmit fails. The failed skb
404 * will go in the wait_queue which will be emptied by the timer function or
405 * by the next skb transmit.
407 void fcoe_check_wait_queue(struct fc_lport
*lport
, struct sk_buff
*skb
)
409 struct fcoe_port
*port
= lport_priv(lport
);
412 spin_lock_bh(&port
->fcoe_pending_queue
.lock
);
415 __skb_queue_tail(&port
->fcoe_pending_queue
, skb
);
417 if (port
->fcoe_pending_queue_active
)
419 port
->fcoe_pending_queue_active
= 1;
421 while (port
->fcoe_pending_queue
.qlen
) {
422 /* keep qlen > 0 until fcoe_start_io succeeds */
423 port
->fcoe_pending_queue
.qlen
++;
424 skb
= __skb_dequeue(&port
->fcoe_pending_queue
);
426 spin_unlock_bh(&port
->fcoe_pending_queue
.lock
);
427 rc
= fcoe_start_io(skb
);
428 spin_lock_bh(&port
->fcoe_pending_queue
.lock
);
431 __skb_queue_head(&port
->fcoe_pending_queue
, skb
);
432 /* undo temporary increment above */
433 port
->fcoe_pending_queue
.qlen
--;
436 /* undo temporary increment above */
437 port
->fcoe_pending_queue
.qlen
--;
440 if (port
->fcoe_pending_queue
.qlen
< port
->min_queue_depth
)
442 if (port
->fcoe_pending_queue
.qlen
&& !timer_pending(&port
->timer
))
443 mod_timer(&port
->timer
, jiffies
+ 2);
444 port
->fcoe_pending_queue_active
= 0;
446 if (port
->fcoe_pending_queue
.qlen
> port
->max_queue_depth
)
448 spin_unlock_bh(&port
->fcoe_pending_queue
.lock
);
450 EXPORT_SYMBOL_GPL(fcoe_check_wait_queue
);
453 * fcoe_queue_timer() - The fcoe queue timer
454 * @lport: The local port
456 * Calls fcoe_check_wait_queue on timeout
458 void fcoe_queue_timer(struct timer_list
*t
)
460 struct fcoe_port
*port
= from_timer(port
, t
, timer
);
462 fcoe_check_wait_queue(port
->lport
, NULL
);
464 EXPORT_SYMBOL_GPL(fcoe_queue_timer
);
467 * fcoe_get_paged_crc_eof() - Allocate a page to be used for the trailer CRC
468 * @skb: The packet to be transmitted
469 * @tlen: The total length of the trailer
470 * @fps: The fcoe context
472 * This routine allocates a page for frame trailers. The page is re-used if
473 * there is enough room left on it for the current trailer. If there isn't
474 * enough buffer left a new page is allocated for the trailer. Reference to
475 * the page from this function as well as the skbs using the page fragments
476 * ensure that the page is freed at the appropriate time.
478 * Returns: 0 for success
480 int fcoe_get_paged_crc_eof(struct sk_buff
*skb
, int tlen
,
481 struct fcoe_percpu_s
*fps
)
485 page
= fps
->crc_eof_page
;
487 page
= alloc_page(GFP_ATOMIC
);
491 fps
->crc_eof_page
= page
;
492 fps
->crc_eof_offset
= 0;
496 skb_fill_page_desc(skb
, skb_shinfo(skb
)->nr_frags
, page
,
497 fps
->crc_eof_offset
, tlen
);
499 skb
->data_len
+= tlen
;
500 skb
->truesize
+= tlen
;
501 fps
->crc_eof_offset
+= sizeof(struct fcoe_crc_eof
);
503 if (fps
->crc_eof_offset
>= PAGE_SIZE
) {
504 fps
->crc_eof_page
= NULL
;
505 fps
->crc_eof_offset
= 0;
511 EXPORT_SYMBOL_GPL(fcoe_get_paged_crc_eof
);
514 * fcoe_transport_lookup - find an fcoe transport that matches a netdev
515 * @netdev: The netdev to look for from all attached transports
517 * Returns : ptr to the fcoe transport that supports this netdev or NULL
520 * The ft_mutex should be held when this is called
522 static struct fcoe_transport
*fcoe_transport_lookup(struct net_device
*netdev
)
524 struct fcoe_transport
*ft
= NULL
;
526 list_for_each_entry(ft
, &fcoe_transports
, list
)
527 if (ft
->match
&& ft
->match(netdev
))
533 * fcoe_transport_attach - Attaches an FCoE transport
534 * @ft: The fcoe transport to be attached
536 * Returns : 0 for success
538 int fcoe_transport_attach(struct fcoe_transport
*ft
)
542 mutex_lock(&ft_mutex
);
544 LIBFCOE_TRANSPORT_DBG("transport %s already attached\n",
550 /* Add default transport to the tail */
551 if (strcmp(ft
->name
, FCOE_TRANSPORT_DEFAULT
))
552 list_add(&ft
->list
, &fcoe_transports
);
554 list_add_tail(&ft
->list
, &fcoe_transports
);
557 LIBFCOE_TRANSPORT_DBG("attaching transport %s\n", ft
->name
);
560 mutex_unlock(&ft_mutex
);
563 EXPORT_SYMBOL(fcoe_transport_attach
);
566 * fcoe_transport_detach - Detaches an FCoE transport
567 * @ft: The fcoe transport to be attached
569 * Returns : 0 for success
571 int fcoe_transport_detach(struct fcoe_transport
*ft
)
574 struct fcoe_netdev_mapping
*nm
= NULL
, *tmp
;
576 mutex_lock(&ft_mutex
);
578 LIBFCOE_TRANSPORT_DBG("transport %s already detached\n",
584 /* remove netdev mapping for this transport as it is going away */
585 mutex_lock(&fn_mutex
);
586 list_for_each_entry_safe(nm
, tmp
, &fcoe_netdevs
, list
) {
588 LIBFCOE_TRANSPORT_DBG("transport %s going away, "
589 "remove its netdev mapping for %s\n",
590 ft
->name
, nm
->netdev
->name
);
595 mutex_unlock(&fn_mutex
);
598 ft
->attached
= false;
599 LIBFCOE_TRANSPORT_DBG("detaching transport %s\n", ft
->name
);
602 mutex_unlock(&ft_mutex
);
606 EXPORT_SYMBOL(fcoe_transport_detach
);
608 static int fcoe_transport_show(char *buffer
, const struct kernel_param
*kp
)
611 struct fcoe_transport
*ft
= NULL
;
613 i
= j
= sprintf(buffer
, "Attached FCoE transports:");
614 mutex_lock(&ft_mutex
);
615 list_for_each_entry(ft
, &fcoe_transports
, list
) {
616 if (i
>= PAGE_SIZE
- IFNAMSIZ
)
618 i
+= snprintf(&buffer
[i
], IFNAMSIZ
, "%s ", ft
->name
);
620 mutex_unlock(&ft_mutex
);
622 i
+= snprintf(&buffer
[i
], IFNAMSIZ
, "none");
626 static int __init
fcoe_transport_init(void)
628 register_netdevice_notifier(&libfcoe_notifier
);
632 static int fcoe_transport_exit(void)
634 struct fcoe_transport
*ft
;
636 unregister_netdevice_notifier(&libfcoe_notifier
);
637 mutex_lock(&ft_mutex
);
638 list_for_each_entry(ft
, &fcoe_transports
, list
)
639 printk(KERN_ERR
"FCoE transport %s is still attached!\n",
641 mutex_unlock(&ft_mutex
);
646 static int fcoe_add_netdev_mapping(struct net_device
*netdev
,
647 struct fcoe_transport
*ft
)
649 struct fcoe_netdev_mapping
*nm
;
651 nm
= kmalloc(sizeof(*nm
), GFP_KERNEL
);
653 printk(KERN_ERR
"Unable to allocate netdev_mapping");
660 mutex_lock(&fn_mutex
);
661 list_add(&nm
->list
, &fcoe_netdevs
);
662 mutex_unlock(&fn_mutex
);
667 static void fcoe_del_netdev_mapping(struct net_device
*netdev
)
669 struct fcoe_netdev_mapping
*nm
= NULL
, *tmp
;
671 mutex_lock(&fn_mutex
);
672 list_for_each_entry_safe(nm
, tmp
, &fcoe_netdevs
, list
) {
673 if (nm
->netdev
== netdev
) {
676 mutex_unlock(&fn_mutex
);
680 mutex_unlock(&fn_mutex
);
685 * fcoe_netdev_map_lookup - find the fcoe transport that matches the netdev on which
688 * Returns : ptr to the fcoe transport that supports this netdev or NULL
691 * The ft_mutex should be held when this is called
693 static struct fcoe_transport
*fcoe_netdev_map_lookup(struct net_device
*netdev
)
695 struct fcoe_transport
*ft
= NULL
;
696 struct fcoe_netdev_mapping
*nm
;
698 mutex_lock(&fn_mutex
);
699 list_for_each_entry(nm
, &fcoe_netdevs
, list
) {
700 if (netdev
== nm
->netdev
) {
702 mutex_unlock(&fn_mutex
);
707 mutex_unlock(&fn_mutex
);
712 * fcoe_if_to_netdev() - Parse a name buffer to get a net device
713 * @buffer: The name of the net device
715 * Returns: NULL or a ptr to net_device
717 static struct net_device
*fcoe_if_to_netdev(const char *buffer
)
720 char ifname
[IFNAMSIZ
+ 2];
723 strlcpy(ifname
, buffer
, IFNAMSIZ
);
724 cp
= ifname
+ strlen(ifname
);
725 while (--cp
>= ifname
&& *cp
== '\n')
727 return dev_get_by_name(&init_net
, ifname
);
733 * libfcoe_device_notification() - Handler for net device events
734 * @notifier: The context of the notification
735 * @event: The type of event
736 * @ptr: The net device that the event was on
738 * This function is called by the Ethernet driver in case of link change event.
740 * Returns: 0 for success
742 static int libfcoe_device_notification(struct notifier_block
*notifier
,
743 ulong event
, void *ptr
)
745 struct net_device
*netdev
= netdev_notifier_info_to_dev(ptr
);
748 case NETDEV_UNREGISTER
:
749 LIBFCOE_TRANSPORT_DBG("NETDEV_UNREGISTER %s\n",
751 fcoe_del_netdev_mapping(netdev
);
757 ssize_t
fcoe_ctlr_create_store(struct bus_type
*bus
,
758 const char *buf
, size_t count
)
760 struct net_device
*netdev
= NULL
;
761 struct fcoe_transport
*ft
= NULL
;
765 mutex_lock(&ft_mutex
);
767 netdev
= fcoe_if_to_netdev(buf
);
769 LIBFCOE_TRANSPORT_DBG("Invalid device %s.\n", buf
);
774 ft
= fcoe_netdev_map_lookup(netdev
);
776 LIBFCOE_TRANSPORT_DBG("transport %s already has existing "
777 "FCoE instance on %s.\n",
778 ft
->name
, netdev
->name
);
783 ft
= fcoe_transport_lookup(netdev
);
785 LIBFCOE_TRANSPORT_DBG("no FCoE transport found for %s.\n",
791 /* pass to transport create */
792 err
= ft
->alloc
? ft
->alloc(netdev
) : -ENODEV
;
794 fcoe_del_netdev_mapping(netdev
);
799 err
= fcoe_add_netdev_mapping(netdev
, ft
);
801 LIBFCOE_TRANSPORT_DBG("failed to add new netdev mapping "
802 "for FCoE transport %s for %s.\n",
803 ft
->name
, netdev
->name
);
808 LIBFCOE_TRANSPORT_DBG("transport %s succeeded to create fcoe on %s.\n",
809 ft
->name
, netdev
->name
);
814 mutex_unlock(&ft_mutex
);
820 ssize_t
fcoe_ctlr_destroy_store(struct bus_type
*bus
,
821 const char *buf
, size_t count
)
824 struct net_device
*netdev
= NULL
;
825 struct fcoe_transport
*ft
= NULL
;
827 mutex_lock(&ft_mutex
);
829 netdev
= fcoe_if_to_netdev(buf
);
831 LIBFCOE_TRANSPORT_DBG("invalid device %s.\n", buf
);
835 ft
= fcoe_netdev_map_lookup(netdev
);
837 LIBFCOE_TRANSPORT_DBG("no FCoE transport found for %s.\n",
842 /* pass to transport destroy */
843 rc
= ft
->destroy(netdev
);
847 fcoe_del_netdev_mapping(netdev
);
848 LIBFCOE_TRANSPORT_DBG("transport %s %s to destroy fcoe on %s.\n",
849 ft
->name
, (rc
) ? "failed" : "succeeded",
851 rc
= count
; /* required for successful return */
855 mutex_unlock(&ft_mutex
);
858 EXPORT_SYMBOL(fcoe_ctlr_destroy_store
);
861 * fcoe_transport_create() - Create a fcoe interface
862 * @buffer: The name of the Ethernet interface to create on
863 * @kp: The associated kernel param
865 * Called from sysfs. This holds the ft_mutex while calling the
866 * registered fcoe transport's create function.
868 * Returns: 0 for success
870 static int fcoe_transport_create(const char *buffer
,
871 const struct kernel_param
*kp
)
874 struct net_device
*netdev
= NULL
;
875 struct fcoe_transport
*ft
= NULL
;
876 enum fip_state fip_mode
= (enum fip_state
)(long)kp
->arg
;
878 mutex_lock(&ft_mutex
);
880 netdev
= fcoe_if_to_netdev(buffer
);
882 LIBFCOE_TRANSPORT_DBG("Invalid device %s.\n", buffer
);
886 ft
= fcoe_netdev_map_lookup(netdev
);
888 LIBFCOE_TRANSPORT_DBG("transport %s already has existing "
889 "FCoE instance on %s.\n",
890 ft
->name
, netdev
->name
);
895 ft
= fcoe_transport_lookup(netdev
);
897 LIBFCOE_TRANSPORT_DBG("no FCoE transport found for %s.\n",
902 rc
= fcoe_add_netdev_mapping(netdev
, ft
);
904 LIBFCOE_TRANSPORT_DBG("failed to add new netdev mapping "
905 "for FCoE transport %s for %s.\n",
906 ft
->name
, netdev
->name
);
910 /* pass to transport create */
911 rc
= ft
->create
? ft
->create(netdev
, fip_mode
) : -ENODEV
;
913 fcoe_del_netdev_mapping(netdev
);
915 LIBFCOE_TRANSPORT_DBG("transport %s %s to create fcoe on %s.\n",
916 ft
->name
, (rc
) ? "failed" : "succeeded",
922 mutex_unlock(&ft_mutex
);
927 * fcoe_transport_destroy() - Destroy a FCoE interface
928 * @buffer: The name of the Ethernet interface to be destroyed
929 * @kp: The associated kernel parameter
931 * Called from sysfs. This holds the ft_mutex while calling the
932 * registered fcoe transport's destroy function.
934 * Returns: 0 for success
936 static int fcoe_transport_destroy(const char *buffer
,
937 const struct kernel_param
*kp
)
940 struct net_device
*netdev
= NULL
;
941 struct fcoe_transport
*ft
= NULL
;
943 mutex_lock(&ft_mutex
);
945 netdev
= fcoe_if_to_netdev(buffer
);
947 LIBFCOE_TRANSPORT_DBG("invalid device %s.\n", buffer
);
951 ft
= fcoe_netdev_map_lookup(netdev
);
953 LIBFCOE_TRANSPORT_DBG("no FCoE transport found for %s.\n",
958 /* pass to transport destroy */
959 rc
= ft
->destroy
? ft
->destroy(netdev
) : -ENODEV
;
960 fcoe_del_netdev_mapping(netdev
);
961 LIBFCOE_TRANSPORT_DBG("transport %s %s to destroy fcoe on %s.\n",
962 ft
->name
, (rc
) ? "failed" : "succeeded",
968 mutex_unlock(&ft_mutex
);
973 * fcoe_transport_disable() - Disables a FCoE interface
974 * @buffer: The name of the Ethernet interface to be disabled
975 * @kp: The associated kernel parameter
979 * Returns: 0 for success
981 static int fcoe_transport_disable(const char *buffer
,
982 const struct kernel_param
*kp
)
985 struct net_device
*netdev
= NULL
;
986 struct fcoe_transport
*ft
= NULL
;
988 mutex_lock(&ft_mutex
);
990 netdev
= fcoe_if_to_netdev(buffer
);
994 ft
= fcoe_netdev_map_lookup(netdev
);
998 rc
= ft
->disable
? ft
->disable(netdev
) : -ENODEV
;
1003 mutex_unlock(&ft_mutex
);
1008 * fcoe_transport_enable() - Enables a FCoE interface
1009 * @buffer: The name of the Ethernet interface to be enabled
1010 * @kp: The associated kernel parameter
1012 * Called from sysfs.
1014 * Returns: 0 for success
1016 static int fcoe_transport_enable(const char *buffer
,
1017 const struct kernel_param
*kp
)
1020 struct net_device
*netdev
= NULL
;
1021 struct fcoe_transport
*ft
= NULL
;
1023 mutex_lock(&ft_mutex
);
1025 netdev
= fcoe_if_to_netdev(buffer
);
1029 ft
= fcoe_netdev_map_lookup(netdev
);
1033 rc
= ft
->enable
? ft
->enable(netdev
) : -ENODEV
;
1038 mutex_unlock(&ft_mutex
);
1043 * libfcoe_init() - Initialization routine for libfcoe.ko
1045 static int __init
libfcoe_init(void)
1049 rc
= fcoe_transport_init();
1053 rc
= fcoe_sysfs_setup();
1055 fcoe_transport_exit();
1059 module_init(libfcoe_init
);
1062 * libfcoe_exit() - Tear down libfcoe.ko
1064 static void __exit
libfcoe_exit(void)
1066 fcoe_sysfs_teardown();
1067 fcoe_transport_exit();
1069 module_exit(libfcoe_exit
);