]> git.karo-electronics.de Git - karo-tx-linux.git/blob - drivers/scsi/fcoe/fcoe_transport.c
libfcoe, fcoe: move fcoe_link_speed_update() to libfcoe and export it
[karo-tx-linux.git] / drivers / scsi / fcoe / fcoe_transport.c
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>
26 #include <linux/crc32.h>
27 #include <scsi/libfcoe.h>
28
29 #include "libfcoe.h"
30
31 MODULE_AUTHOR("Open-FCoE.org");
32 MODULE_DESCRIPTION("FIP discovery protocol and FCoE transport for FCoE HBAs");
33 MODULE_LICENSE("GPL v2");
34
35 static int fcoe_transport_create(const char *, struct kernel_param *);
36 static int fcoe_transport_destroy(const char *, 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 *, struct kernel_param *);
41 static int fcoe_transport_disable(const char *, struct kernel_param *);
42 static int libfcoe_device_notification(struct notifier_block *notifier,
43                                     ulong event, void *ptr);
44
45 static LIST_HEAD(fcoe_transports);
46 static DEFINE_MUTEX(ft_mutex);
47 static LIST_HEAD(fcoe_netdevs);
48 static DEFINE_MUTEX(fn_mutex);
49
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");
53
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");
57
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 a ethernet interface");
62
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");
68
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 a ethernet interface");
72
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 a ethernet interface.");
76
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 a ethernet interface.");
80
81 /* notification function for packets from net device */
82 static struct notifier_block libfcoe_notifier = {
83         .notifier_call = libfcoe_device_notification,
84 };
85
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  */
93 int fcoe_link_speed_update(struct fc_lport *lport)
94 {
95         struct net_device *netdev = fcoe_get_netdev(lport);
96         struct ethtool_cmd ecmd;
97
98         if (!__ethtool_get_settings(netdev, &ecmd)) {
99                 lport->link_supported_speeds &=
100                         ~(FC_PORTSPEED_1GBIT | FC_PORTSPEED_10GBIT);
101                 if (ecmd.supported & (SUPPORTED_1000baseT_Half |
102                                       SUPPORTED_1000baseT_Full))
103                         lport->link_supported_speeds |= FC_PORTSPEED_1GBIT;
104                 if (ecmd.supported & SUPPORTED_10000baseT_Full)
105                         lport->link_supported_speeds |=
106                                 FC_PORTSPEED_10GBIT;
107                 switch (ethtool_cmd_speed(&ecmd)) {
108                 case SPEED_1000:
109                         lport->link_speed = FC_PORTSPEED_1GBIT;
110                         break;
111                 case SPEED_10000:
112                         lport->link_speed = FC_PORTSPEED_10GBIT;
113                         break;
114                 }
115                 return 0;
116         }
117         return -1;
118 }
119 EXPORT_SYMBOL_GPL(fcoe_link_speed_update);
120
121 void __fcoe_get_lesb(struct fc_lport *lport,
122                      struct fc_els_lesb *fc_lesb,
123                      struct net_device *netdev)
124 {
125         unsigned int cpu;
126         u32 lfc, vlfc, mdac;
127         struct fc_stats *stats;
128         struct fcoe_fc_els_lesb *lesb;
129         struct rtnl_link_stats64 temp;
130
131         lfc = 0;
132         vlfc = 0;
133         mdac = 0;
134         lesb = (struct fcoe_fc_els_lesb *)fc_lesb;
135         memset(lesb, 0, sizeof(*lesb));
136         for_each_possible_cpu(cpu) {
137                 stats = per_cpu_ptr(lport->stats, cpu);
138                 lfc += stats->LinkFailureCount;
139                 vlfc += stats->VLinkFailureCount;
140                 mdac += stats->MissDiscAdvCount;
141         }
142         lesb->lesb_link_fail = htonl(lfc);
143         lesb->lesb_vlink_fail = htonl(vlfc);
144         lesb->lesb_miss_fka = htonl(mdac);
145         lesb->lesb_fcs_error =
146                         htonl(dev_get_stats(netdev, &temp)->rx_crc_errors);
147 }
148 EXPORT_SYMBOL_GPL(__fcoe_get_lesb);
149
150 void fcoe_wwn_to_str(u64 wwn, char *buf, int len)
151 {
152         u8 wwpn[8];
153
154         u64_to_wwn(wwn, wwpn);
155         snprintf(buf, len, "%02x%02x%02x%02x%02x%02x%02x%02x",
156                  wwpn[0], wwpn[1], wwpn[2], wwpn[3],
157                  wwpn[4], wwpn[5], wwpn[6], wwpn[7]);
158 }
159 EXPORT_SYMBOL_GPL(fcoe_wwn_to_str);
160
161 /**
162  * fcoe_validate_vport_create() - Validate a vport before creating it
163  * @vport: NPIV port to be created
164  *
165  * This routine is meant to add validation for a vport before creating it
166  * via fcoe_vport_create().
167  * Current validations are:
168  *      - WWPN supplied is unique for given lport
169  */
170 int fcoe_validate_vport_create(struct fc_vport *vport)
171 {
172         struct Scsi_Host *shost = vport_to_shost(vport);
173         struct fc_lport *n_port = shost_priv(shost);
174         struct fc_lport *vn_port;
175         int rc = 0;
176         char buf[32];
177
178         mutex_lock(&n_port->lp_mutex);
179
180         fcoe_wwn_to_str(vport->port_name, buf, sizeof(buf));
181         /* Check if the wwpn is not same as that of the lport */
182         if (!memcmp(&n_port->wwpn, &vport->port_name, sizeof(u64))) {
183                 LIBFCOE_TRANSPORT_DBG("vport WWPN 0x%s is same as that of the "
184                                       "base port WWPN\n", buf);
185                 rc = -EINVAL;
186                 goto out;
187         }
188
189         /* Check if there is any existing vport with same wwpn */
190         list_for_each_entry(vn_port, &n_port->vports, list) {
191                 if (!memcmp(&vn_port->wwpn, &vport->port_name, sizeof(u64))) {
192                         LIBFCOE_TRANSPORT_DBG("vport with given WWPN 0x%s "
193                                               "already exists\n", buf);
194                         rc = -EINVAL;
195                         break;
196                 }
197         }
198 out:
199         mutex_unlock(&n_port->lp_mutex);
200         return rc;
201 }
202 EXPORT_SYMBOL_GPL(fcoe_validate_vport_create);
203
204 /**
205  * fcoe_get_wwn() - Get the world wide name from LLD if it supports it
206  * @netdev: the associated net device
207  * @wwn: the output WWN
208  * @type: the type of WWN (WWPN or WWNN)
209  *
210  * Returns: 0 for success
211  */
212 int fcoe_get_wwn(struct net_device *netdev, u64 *wwn, int type)
213 {
214         const struct net_device_ops *ops = netdev->netdev_ops;
215
216         if (ops->ndo_fcoe_get_wwn)
217                 return ops->ndo_fcoe_get_wwn(netdev, wwn, type);
218         return -EINVAL;
219 }
220 EXPORT_SYMBOL_GPL(fcoe_get_wwn);
221
222 /**
223  * fcoe_fc_crc() - Calculates the CRC for a given frame
224  * @fp: The frame to be checksumed
225  *
226  * This uses crc32() routine to calculate the CRC for a frame
227  *
228  * Return: The 32 bit CRC value
229  */
230 u32 fcoe_fc_crc(struct fc_frame *fp)
231 {
232         struct sk_buff *skb = fp_skb(fp);
233         struct skb_frag_struct *frag;
234         unsigned char *data;
235         unsigned long off, len, clen;
236         u32 crc;
237         unsigned i;
238
239         crc = crc32(~0, skb->data, skb_headlen(skb));
240
241         for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
242                 frag = &skb_shinfo(skb)->frags[i];
243                 off = frag->page_offset;
244                 len = skb_frag_size(frag);
245                 while (len > 0) {
246                         clen = min(len, PAGE_SIZE - (off & ~PAGE_MASK));
247                         data = kmap_atomic(
248                                 skb_frag_page(frag) + (off >> PAGE_SHIFT));
249                         crc = crc32(crc, data + (off & ~PAGE_MASK), clen);
250                         kunmap_atomic(data);
251                         off += clen;
252                         len -= clen;
253                 }
254         }
255         return crc;
256 }
257 EXPORT_SYMBOL_GPL(fcoe_fc_crc);
258
259 /**
260  * fcoe_start_io() - Start FCoE I/O
261  * @skb: The packet to be transmitted
262  *
263  * This routine is called from the net device to start transmitting
264  * FCoE packets.
265  *
266  * Returns: 0 for success
267  */
268 int fcoe_start_io(struct sk_buff *skb)
269 {
270         struct sk_buff *nskb;
271         int rc;
272
273         nskb = skb_clone(skb, GFP_ATOMIC);
274         if (!nskb)
275                 return -ENOMEM;
276         rc = dev_queue_xmit(nskb);
277         if (rc != 0)
278                 return rc;
279         kfree_skb(skb);
280         return 0;
281 }
282 EXPORT_SYMBOL_GPL(fcoe_start_io);
283
284
285 /**
286  * fcoe_clean_pending_queue() - Dequeue a skb and free it
287  * @lport: The local port to dequeue a skb on
288  */
289 void fcoe_clean_pending_queue(struct fc_lport *lport)
290 {
291         struct fcoe_port  *port = lport_priv(lport);
292         struct sk_buff *skb;
293
294         spin_lock_bh(&port->fcoe_pending_queue.lock);
295         while ((skb = __skb_dequeue(&port->fcoe_pending_queue)) != NULL) {
296                 spin_unlock_bh(&port->fcoe_pending_queue.lock);
297                 kfree_skb(skb);
298                 spin_lock_bh(&port->fcoe_pending_queue.lock);
299         }
300         spin_unlock_bh(&port->fcoe_pending_queue.lock);
301 }
302 EXPORT_SYMBOL_GPL(fcoe_clean_pending_queue);
303
304 /**
305  * fcoe_check_wait_queue() - Attempt to clear the transmit backlog
306  * @lport: The local port whose backlog is to be cleared
307  *
308  * This empties the wait_queue, dequeues the head of the wait_queue queue
309  * and calls fcoe_start_io() for each packet. If all skb have been
310  * transmitted it returns the qlen. If an error occurs it restores
311  * wait_queue (to try again later) and returns -1.
312  *
313  * The wait_queue is used when the skb transmit fails. The failed skb
314  * will go in the wait_queue which will be emptied by the timer function or
315  * by the next skb transmit.
316  */
317 void fcoe_check_wait_queue(struct fc_lport *lport, struct sk_buff *skb)
318 {
319         struct fcoe_port *port = lport_priv(lport);
320         int rc;
321
322         spin_lock_bh(&port->fcoe_pending_queue.lock);
323
324         if (skb)
325                 __skb_queue_tail(&port->fcoe_pending_queue, skb);
326
327         if (port->fcoe_pending_queue_active)
328                 goto out;
329         port->fcoe_pending_queue_active = 1;
330
331         while (port->fcoe_pending_queue.qlen) {
332                 /* keep qlen > 0 until fcoe_start_io succeeds */
333                 port->fcoe_pending_queue.qlen++;
334                 skb = __skb_dequeue(&port->fcoe_pending_queue);
335
336                 spin_unlock_bh(&port->fcoe_pending_queue.lock);
337                 rc = fcoe_start_io(skb);
338                 spin_lock_bh(&port->fcoe_pending_queue.lock);
339
340                 if (rc) {
341                         __skb_queue_head(&port->fcoe_pending_queue, skb);
342                         /* undo temporary increment above */
343                         port->fcoe_pending_queue.qlen--;
344                         break;
345                 }
346                 /* undo temporary increment above */
347                 port->fcoe_pending_queue.qlen--;
348         }
349
350         if (port->fcoe_pending_queue.qlen < port->min_queue_depth)
351                 lport->qfull = 0;
352         if (port->fcoe_pending_queue.qlen && !timer_pending(&port->timer))
353                 mod_timer(&port->timer, jiffies + 2);
354         port->fcoe_pending_queue_active = 0;
355 out:
356         if (port->fcoe_pending_queue.qlen > port->max_queue_depth)
357                 lport->qfull = 1;
358         spin_unlock_bh(&port->fcoe_pending_queue.lock);
359 }
360 EXPORT_SYMBOL_GPL(fcoe_check_wait_queue);
361
362 /**
363  * fcoe_queue_timer() - The fcoe queue timer
364  * @lport: The local port
365  *
366  * Calls fcoe_check_wait_queue on timeout
367  */
368 void fcoe_queue_timer(ulong lport)
369 {
370         fcoe_check_wait_queue((struct fc_lport *)lport, NULL);
371 }
372 EXPORT_SYMBOL_GPL(fcoe_queue_timer);
373
374 /**
375  * fcoe_get_paged_crc_eof() - Allocate a page to be used for the trailer CRC
376  * @skb:  The packet to be transmitted
377  * @tlen: The total length of the trailer
378  * @fps:  The fcoe context
379  *
380  * This routine allocates a page for frame trailers. The page is re-used if
381  * there is enough room left on it for the current trailer. If there isn't
382  * enough buffer left a new page is allocated for the trailer. Reference to
383  * the page from this function as well as the skbs using the page fragments
384  * ensure that the page is freed at the appropriate time.
385  *
386  * Returns: 0 for success
387  */
388 int fcoe_get_paged_crc_eof(struct sk_buff *skb, int tlen,
389                            struct fcoe_percpu_s *fps)
390 {
391         struct page *page;
392
393         page = fps->crc_eof_page;
394         if (!page) {
395                 page = alloc_page(GFP_ATOMIC);
396                 if (!page)
397                         return -ENOMEM;
398
399                 fps->crc_eof_page = page;
400                 fps->crc_eof_offset = 0;
401         }
402
403         get_page(page);
404         skb_fill_page_desc(skb, skb_shinfo(skb)->nr_frags, page,
405                            fps->crc_eof_offset, tlen);
406         skb->len += tlen;
407         skb->data_len += tlen;
408         skb->truesize += tlen;
409         fps->crc_eof_offset += sizeof(struct fcoe_crc_eof);
410
411         if (fps->crc_eof_offset >= PAGE_SIZE) {
412                 fps->crc_eof_page = NULL;
413                 fps->crc_eof_offset = 0;
414                 put_page(page);
415         }
416
417         return 0;
418 }
419 EXPORT_SYMBOL_GPL(fcoe_get_paged_crc_eof);
420
421 /**
422  * fcoe_transport_lookup - find an fcoe transport that matches a netdev
423  * @netdev: The netdev to look for from all attached transports
424  *
425  * Returns : ptr to the fcoe transport that supports this netdev or NULL
426  * if not found.
427  *
428  * The ft_mutex should be held when this is called
429  */
430 static struct fcoe_transport *fcoe_transport_lookup(struct net_device *netdev)
431 {
432         struct fcoe_transport *ft = NULL;
433
434         list_for_each_entry(ft, &fcoe_transports, list)
435                 if (ft->match && ft->match(netdev))
436                         return ft;
437         return NULL;
438 }
439
440 /**
441  * fcoe_transport_attach - Attaches an FCoE transport
442  * @ft: The fcoe transport to be attached
443  *
444  * Returns : 0 for success
445  */
446 int fcoe_transport_attach(struct fcoe_transport *ft)
447 {
448         int rc = 0;
449
450         mutex_lock(&ft_mutex);
451         if (ft->attached) {
452                 LIBFCOE_TRANSPORT_DBG("transport %s already attached\n",
453                                        ft->name);
454                 rc = -EEXIST;
455                 goto out_attach;
456         }
457
458         /* Add default transport to the tail */
459         if (strcmp(ft->name, FCOE_TRANSPORT_DEFAULT))
460                 list_add(&ft->list, &fcoe_transports);
461         else
462                 list_add_tail(&ft->list, &fcoe_transports);
463
464         ft->attached = true;
465         LIBFCOE_TRANSPORT_DBG("attaching transport %s\n", ft->name);
466
467 out_attach:
468         mutex_unlock(&ft_mutex);
469         return rc;
470 }
471 EXPORT_SYMBOL(fcoe_transport_attach);
472
473 /**
474  * fcoe_transport_detach - Detaches an FCoE transport
475  * @ft: The fcoe transport to be attached
476  *
477  * Returns : 0 for success
478  */
479 int fcoe_transport_detach(struct fcoe_transport *ft)
480 {
481         int rc = 0;
482         struct fcoe_netdev_mapping *nm = NULL, *tmp;
483
484         mutex_lock(&ft_mutex);
485         if (!ft->attached) {
486                 LIBFCOE_TRANSPORT_DBG("transport %s already detached\n",
487                         ft->name);
488                 rc = -ENODEV;
489                 goto out_attach;
490         }
491
492         /* remove netdev mapping for this transport as it is going away */
493         mutex_lock(&fn_mutex);
494         list_for_each_entry_safe(nm, tmp, &fcoe_netdevs, list) {
495                 if (nm->ft == ft) {
496                         LIBFCOE_TRANSPORT_DBG("transport %s going away, "
497                                 "remove its netdev mapping for %s\n",
498                                 ft->name, nm->netdev->name);
499                         list_del(&nm->list);
500                         kfree(nm);
501                 }
502         }
503         mutex_unlock(&fn_mutex);
504
505         list_del(&ft->list);
506         ft->attached = false;
507         LIBFCOE_TRANSPORT_DBG("detaching transport %s\n", ft->name);
508
509 out_attach:
510         mutex_unlock(&ft_mutex);
511         return rc;
512
513 }
514 EXPORT_SYMBOL(fcoe_transport_detach);
515
516 static int fcoe_transport_show(char *buffer, const struct kernel_param *kp)
517 {
518         int i, j;
519         struct fcoe_transport *ft = NULL;
520
521         i = j = sprintf(buffer, "Attached FCoE transports:");
522         mutex_lock(&ft_mutex);
523         list_for_each_entry(ft, &fcoe_transports, list) {
524                 if (i >= PAGE_SIZE - IFNAMSIZ)
525                         break;
526                 i += snprintf(&buffer[i], IFNAMSIZ, "%s ", ft->name);
527         }
528         mutex_unlock(&ft_mutex);
529         if (i == j)
530                 i += snprintf(&buffer[i], IFNAMSIZ, "none");
531         return i;
532 }
533
534 static int __init fcoe_transport_init(void)
535 {
536         register_netdevice_notifier(&libfcoe_notifier);
537         return 0;
538 }
539
540 static int fcoe_transport_exit(void)
541 {
542         struct fcoe_transport *ft;
543
544         unregister_netdevice_notifier(&libfcoe_notifier);
545         mutex_lock(&ft_mutex);
546         list_for_each_entry(ft, &fcoe_transports, list)
547                 printk(KERN_ERR "FCoE transport %s is still attached!\n",
548                       ft->name);
549         mutex_unlock(&ft_mutex);
550         return 0;
551 }
552
553
554 static int fcoe_add_netdev_mapping(struct net_device *netdev,
555                                         struct fcoe_transport *ft)
556 {
557         struct fcoe_netdev_mapping *nm;
558
559         nm = kmalloc(sizeof(*nm), GFP_KERNEL);
560         if (!nm) {
561                 printk(KERN_ERR "Unable to allocate netdev_mapping");
562                 return -ENOMEM;
563         }
564
565         nm->netdev = netdev;
566         nm->ft = ft;
567
568         mutex_lock(&fn_mutex);
569         list_add(&nm->list, &fcoe_netdevs);
570         mutex_unlock(&fn_mutex);
571         return 0;
572 }
573
574
575 static void fcoe_del_netdev_mapping(struct net_device *netdev)
576 {
577         struct fcoe_netdev_mapping *nm = NULL, *tmp;
578
579         mutex_lock(&fn_mutex);
580         list_for_each_entry_safe(nm, tmp, &fcoe_netdevs, list) {
581                 if (nm->netdev == netdev) {
582                         list_del(&nm->list);
583                         kfree(nm);
584                         mutex_unlock(&fn_mutex);
585                         return;
586                 }
587         }
588         mutex_unlock(&fn_mutex);
589 }
590
591
592 /**
593  * fcoe_netdev_map_lookup - find the fcoe transport that matches the netdev on which
594  * it was created
595  *
596  * Returns : ptr to the fcoe transport that supports this netdev or NULL
597  * if not found.
598  *
599  * The ft_mutex should be held when this is called
600  */
601 static struct fcoe_transport *fcoe_netdev_map_lookup(struct net_device *netdev)
602 {
603         struct fcoe_transport *ft = NULL;
604         struct fcoe_netdev_mapping *nm;
605
606         mutex_lock(&fn_mutex);
607         list_for_each_entry(nm, &fcoe_netdevs, list) {
608                 if (netdev == nm->netdev) {
609                         ft = nm->ft;
610                         mutex_unlock(&fn_mutex);
611                         return ft;
612                 }
613         }
614
615         mutex_unlock(&fn_mutex);
616         return NULL;
617 }
618
619 /**
620  * fcoe_if_to_netdev() - Parse a name buffer to get a net device
621  * @buffer: The name of the net device
622  *
623  * Returns: NULL or a ptr to net_device
624  */
625 static struct net_device *fcoe_if_to_netdev(const char *buffer)
626 {
627         char *cp;
628         char ifname[IFNAMSIZ + 2];
629
630         if (buffer) {
631                 strlcpy(ifname, buffer, IFNAMSIZ);
632                 cp = ifname + strlen(ifname);
633                 while (--cp >= ifname && *cp == '\n')
634                         *cp = '\0';
635                 return dev_get_by_name(&init_net, ifname);
636         }
637         return NULL;
638 }
639
640 /**
641  * libfcoe_device_notification() - Handler for net device events
642  * @notifier: The context of the notification
643  * @event:    The type of event
644  * @ptr:      The net device that the event was on
645  *
646  * This function is called by the Ethernet driver in case of link change event.
647  *
648  * Returns: 0 for success
649  */
650 static int libfcoe_device_notification(struct notifier_block *notifier,
651                                     ulong event, void *ptr)
652 {
653         struct net_device *netdev = ptr;
654
655         switch (event) {
656         case NETDEV_UNREGISTER:
657                 LIBFCOE_TRANSPORT_DBG("NETDEV_UNREGISTER %s\n",
658                                       netdev->name);
659                 fcoe_del_netdev_mapping(netdev);
660                 break;
661         }
662         return NOTIFY_OK;
663 }
664
665 ssize_t fcoe_ctlr_create_store(struct bus_type *bus,
666                                const char *buf, size_t count)
667 {
668         struct net_device *netdev = NULL;
669         struct fcoe_transport *ft = NULL;
670         struct fcoe_ctlr_device *ctlr_dev = NULL;
671         int rc = 0;
672         int err;
673
674         mutex_lock(&ft_mutex);
675
676         netdev = fcoe_if_to_netdev(buf);
677         if (!netdev) {
678                 LIBFCOE_TRANSPORT_DBG("Invalid device %s.\n", buf);
679                 rc = -ENODEV;
680                 goto out_nodev;
681         }
682
683         ft = fcoe_netdev_map_lookup(netdev);
684         if (ft) {
685                 LIBFCOE_TRANSPORT_DBG("transport %s already has existing "
686                                       "FCoE instance on %s.\n",
687                                       ft->name, netdev->name);
688                 rc = -EEXIST;
689                 goto out_putdev;
690         }
691
692         ft = fcoe_transport_lookup(netdev);
693         if (!ft) {
694                 LIBFCOE_TRANSPORT_DBG("no FCoE transport found for %s.\n",
695                                       netdev->name);
696                 rc = -ENODEV;
697                 goto out_putdev;
698         }
699
700         /* pass to transport create */
701         err = ft->alloc ? ft->alloc(netdev) : -ENODEV;
702         if (err) {
703                 fcoe_del_netdev_mapping(netdev);
704                 rc = -ENOMEM;
705                 goto out_putdev;
706         }
707
708         err = fcoe_add_netdev_mapping(netdev, ft);
709         if (err) {
710                 LIBFCOE_TRANSPORT_DBG("failed to add new netdev mapping "
711                                       "for FCoE transport %s for %s.\n",
712                                       ft->name, netdev->name);
713                 rc = -ENODEV;
714                 goto out_putdev;
715         }
716
717         LIBFCOE_TRANSPORT_DBG("transport %s %s to create fcoe on %s.\n",
718                               ft->name, (ctlr_dev) ? "succeeded" : "failed",
719                               netdev->name);
720
721 out_putdev:
722         dev_put(netdev);
723 out_nodev:
724         mutex_unlock(&ft_mutex);
725         if (rc)
726                 return rc;
727         return count;
728 }
729
730 ssize_t fcoe_ctlr_destroy_store(struct bus_type *bus,
731                                 const char *buf, size_t count)
732 {
733         int rc = -ENODEV;
734         struct net_device *netdev = NULL;
735         struct fcoe_transport *ft = NULL;
736
737         mutex_lock(&ft_mutex);
738
739         netdev = fcoe_if_to_netdev(buf);
740         if (!netdev) {
741                 LIBFCOE_TRANSPORT_DBG("invalid device %s.\n", buf);
742                 goto out_nodev;
743         }
744
745         ft = fcoe_netdev_map_lookup(netdev);
746         if (!ft) {
747                 LIBFCOE_TRANSPORT_DBG("no FCoE transport found for %s.\n",
748                                       netdev->name);
749                 goto out_putdev;
750         }
751
752         /* pass to transport destroy */
753         rc = ft->destroy(netdev);
754         if (rc)
755                 goto out_putdev;
756
757         fcoe_del_netdev_mapping(netdev);
758         LIBFCOE_TRANSPORT_DBG("transport %s %s to destroy fcoe on %s.\n",
759                               ft->name, (rc) ? "failed" : "succeeded",
760                               netdev->name);
761         rc = count; /* required for successful return */
762 out_putdev:
763         dev_put(netdev);
764 out_nodev:
765         mutex_unlock(&ft_mutex);
766         return rc;
767 }
768 EXPORT_SYMBOL(fcoe_ctlr_destroy_store);
769
770 /**
771  * fcoe_transport_create() - Create a fcoe interface
772  * @buffer: The name of the Ethernet interface to create on
773  * @kp:     The associated kernel param
774  *
775  * Called from sysfs. This holds the ft_mutex while calling the
776  * registered fcoe transport's create function.
777  *
778  * Returns: 0 for success
779  */
780 static int fcoe_transport_create(const char *buffer, struct kernel_param *kp)
781 {
782         int rc = -ENODEV;
783         struct net_device *netdev = NULL;
784         struct fcoe_transport *ft = NULL;
785         enum fip_state fip_mode = (enum fip_state)(long)kp->arg;
786
787         mutex_lock(&ft_mutex);
788
789         netdev = fcoe_if_to_netdev(buffer);
790         if (!netdev) {
791                 LIBFCOE_TRANSPORT_DBG("Invalid device %s.\n", buffer);
792                 goto out_nodev;
793         }
794
795         ft = fcoe_netdev_map_lookup(netdev);
796         if (ft) {
797                 LIBFCOE_TRANSPORT_DBG("transport %s already has existing "
798                                       "FCoE instance on %s.\n",
799                                       ft->name, netdev->name);
800                 rc = -EEXIST;
801                 goto out_putdev;
802         }
803
804         ft = fcoe_transport_lookup(netdev);
805         if (!ft) {
806                 LIBFCOE_TRANSPORT_DBG("no FCoE transport found for %s.\n",
807                                       netdev->name);
808                 goto out_putdev;
809         }
810
811         rc = fcoe_add_netdev_mapping(netdev, ft);
812         if (rc) {
813                 LIBFCOE_TRANSPORT_DBG("failed to add new netdev mapping "
814                                       "for FCoE transport %s for %s.\n",
815                                       ft->name, netdev->name);
816                 goto out_putdev;
817         }
818
819         /* pass to transport create */
820         rc = ft->create ? ft->create(netdev, fip_mode) : -ENODEV;
821         if (rc)
822                 fcoe_del_netdev_mapping(netdev);
823
824         LIBFCOE_TRANSPORT_DBG("transport %s %s to create fcoe on %s.\n",
825                               ft->name, (rc) ? "failed" : "succeeded",
826                               netdev->name);
827
828 out_putdev:
829         dev_put(netdev);
830 out_nodev:
831         mutex_unlock(&ft_mutex);
832         return rc;
833 }
834
835 /**
836  * fcoe_transport_destroy() - Destroy a FCoE interface
837  * @buffer: The name of the Ethernet interface to be destroyed
838  * @kp:     The associated kernel parameter
839  *
840  * Called from sysfs. This holds the ft_mutex while calling the
841  * registered fcoe transport's destroy function.
842  *
843  * Returns: 0 for success
844  */
845 static int fcoe_transport_destroy(const char *buffer, struct kernel_param *kp)
846 {
847         int rc = -ENODEV;
848         struct net_device *netdev = NULL;
849         struct fcoe_transport *ft = NULL;
850
851         mutex_lock(&ft_mutex);
852
853         netdev = fcoe_if_to_netdev(buffer);
854         if (!netdev) {
855                 LIBFCOE_TRANSPORT_DBG("invalid device %s.\n", buffer);
856                 goto out_nodev;
857         }
858
859         ft = fcoe_netdev_map_lookup(netdev);
860         if (!ft) {
861                 LIBFCOE_TRANSPORT_DBG("no FCoE transport found for %s.\n",
862                                       netdev->name);
863                 goto out_putdev;
864         }
865
866         /* pass to transport destroy */
867         rc = ft->destroy ? ft->destroy(netdev) : -ENODEV;
868         fcoe_del_netdev_mapping(netdev);
869         LIBFCOE_TRANSPORT_DBG("transport %s %s to destroy fcoe on %s.\n",
870                               ft->name, (rc) ? "failed" : "succeeded",
871                               netdev->name);
872
873 out_putdev:
874         dev_put(netdev);
875 out_nodev:
876         mutex_unlock(&ft_mutex);
877         return rc;
878 }
879
880 /**
881  * fcoe_transport_disable() - Disables a FCoE interface
882  * @buffer: The name of the Ethernet interface to be disabled
883  * @kp:     The associated kernel parameter
884  *
885  * Called from sysfs.
886  *
887  * Returns: 0 for success
888  */
889 static int fcoe_transport_disable(const char *buffer, struct kernel_param *kp)
890 {
891         int rc = -ENODEV;
892         struct net_device *netdev = NULL;
893         struct fcoe_transport *ft = NULL;
894
895         mutex_lock(&ft_mutex);
896
897         netdev = fcoe_if_to_netdev(buffer);
898         if (!netdev)
899                 goto out_nodev;
900
901         ft = fcoe_netdev_map_lookup(netdev);
902         if (!ft)
903                 goto out_putdev;
904
905         rc = ft->disable ? ft->disable(netdev) : -ENODEV;
906
907 out_putdev:
908         dev_put(netdev);
909 out_nodev:
910         mutex_unlock(&ft_mutex);
911
912         if (rc == -ERESTARTSYS)
913                 return restart_syscall();
914         else
915                 return rc;
916 }
917
918 /**
919  * fcoe_transport_enable() - Enables a FCoE interface
920  * @buffer: The name of the Ethernet interface to be enabled
921  * @kp:     The associated kernel parameter
922  *
923  * Called from sysfs.
924  *
925  * Returns: 0 for success
926  */
927 static int fcoe_transport_enable(const char *buffer, struct kernel_param *kp)
928 {
929         int rc = -ENODEV;
930         struct net_device *netdev = NULL;
931         struct fcoe_transport *ft = NULL;
932
933         mutex_lock(&ft_mutex);
934
935         netdev = fcoe_if_to_netdev(buffer);
936         if (!netdev)
937                 goto out_nodev;
938
939         ft = fcoe_netdev_map_lookup(netdev);
940         if (!ft)
941                 goto out_putdev;
942
943         rc = ft->enable ? ft->enable(netdev) : -ENODEV;
944
945 out_putdev:
946         dev_put(netdev);
947 out_nodev:
948         mutex_unlock(&ft_mutex);
949         return rc;
950 }
951
952 /**
953  * libfcoe_init() - Initialization routine for libfcoe.ko
954  */
955 static int __init libfcoe_init(void)
956 {
957         int rc = 0;
958
959         rc = fcoe_transport_init();
960         if (rc)
961                 return rc;
962
963         rc = fcoe_sysfs_setup();
964         if (rc)
965                 fcoe_transport_exit();
966
967         return rc;
968 }
969 module_init(libfcoe_init);
970
971 /**
972  * libfcoe_exit() - Tear down libfcoe.ko
973  */
974 static void __exit libfcoe_exit(void)
975 {
976         fcoe_sysfs_teardown();
977         fcoe_transport_exit();
978 }
979 module_exit(libfcoe_exit);