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 an 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 an 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 an 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 an 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_link_ksettings ecmd;
97 
98 	if (!__ethtool_get_link_ksettings(netdev, &ecmd)) {
99 		lport->link_supported_speeds &= ~(FC_PORTSPEED_1GBIT  |
100 		                                  FC_PORTSPEED_10GBIT |
101 		                                  FC_PORTSPEED_20GBIT |
102 		                                  FC_PORTSPEED_40GBIT);
103 
104 		if (ecmd.link_modes.supported[0] & (
105 			    SUPPORTED_1000baseT_Half |
106 			    SUPPORTED_1000baseT_Full |
107 			    SUPPORTED_1000baseKX_Full))
108 			lport->link_supported_speeds |= FC_PORTSPEED_1GBIT;
109 
110 		if (ecmd.link_modes.supported[0] & (
111 			    SUPPORTED_10000baseT_Full   |
112 			    SUPPORTED_10000baseKX4_Full |
113 			    SUPPORTED_10000baseKR_Full  |
114 			    SUPPORTED_10000baseR_FEC))
115 			lport->link_supported_speeds |= FC_PORTSPEED_10GBIT;
116 
117 		if (ecmd.link_modes.supported[0] & (
118 			    SUPPORTED_20000baseMLD2_Full |
119 			    SUPPORTED_20000baseKR2_Full))
120 			lport->link_supported_speeds |= FC_PORTSPEED_20GBIT;
121 
122 		if (ecmd.link_modes.supported[0] & (
123 			    SUPPORTED_40000baseKR4_Full |
124 			    SUPPORTED_40000baseCR4_Full |
125 			    SUPPORTED_40000baseSR4_Full |
126 			    SUPPORTED_40000baseLR4_Full))
127 			lport->link_supported_speeds |= FC_PORTSPEED_40GBIT;
128 
129 		switch (ecmd.base.speed) {
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;
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;
145 		}
146 		return 0;
147 	}
148 	return -1;
149 }
150 EXPORT_SYMBOL_GPL(fcoe_link_speed_update);
151 
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  */
161 void __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;
167 	struct fc_stats *stats;
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) {
177 		stats = per_cpu_ptr(lport->stats, cpu);
178 		lfc += stats->LinkFailureCount;
179 		vlfc += stats->VLinkFailureCount;
180 		mdac += stats->MissDiscAdvCount;
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 }
188 EXPORT_SYMBOL_GPL(__fcoe_get_lesb);
189 
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  */
195 void 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 }
202 EXPORT_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  */
210 void 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);
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);
218 }
219 EXPORT_SYMBOL_GPL(fcoe_ctlr_get_lesb);
220 
221 void 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 }
230 EXPORT_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  */
241 int 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 	}
269 out:
270 	mutex_unlock(&n_port->lp_mutex);
271 	return rc;
272 }
273 EXPORT_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  */
283 int 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 }
291 EXPORT_SYMBOL_GPL(fcoe_get_wwn);
292 
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  */
301 u32 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;
315 		len = skb_frag_size(frag);
316 		while (len > 0) {
317 			clen = min(len, PAGE_SIZE - (off & ~PAGE_MASK));
318 			data = kmap_atomic(
319 				skb_frag_page(frag) + (off >> PAGE_SHIFT));
320 			crc = crc32(crc, data + (off & ~PAGE_MASK), clen);
321 			kunmap_atomic(data);
322 			off += clen;
323 			len -= clen;
324 		}
325 	}
326 	return crc;
327 }
328 EXPORT_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  */
339 int 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 }
353 EXPORT_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  */
360 void 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 }
373 EXPORT_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  */
388 void 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;
426 out:
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 }
431 EXPORT_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  */
439 void fcoe_queue_timer(ulong lport)
440 {
441 	fcoe_check_wait_queue((struct fc_lport *)lport, NULL);
442 }
443 EXPORT_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  */
459 int 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 }
490 EXPORT_SYMBOL_GPL(fcoe_get_paged_crc_eof);
491 
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  */
501 static 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  */
517 int 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 
538 out_attach:
539 	mutex_unlock(&ft_mutex);
540 	return rc;
541 }
542 EXPORT_SYMBOL(fcoe_transport_attach);
543 
544 /**
545  * fcoe_transport_detach - Detaches an FCoE transport
546  * @ft: The fcoe transport to be attached
547  *
548  * Returns : 0 for success
549  */
550 int fcoe_transport_detach(struct fcoe_transport *ft)
551 {
552 	int rc = 0;
553 	struct fcoe_netdev_mapping *nm = NULL, *tmp;
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 
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 
576 	list_del(&ft->list);
577 	ft->attached = false;
578 	LIBFCOE_TRANSPORT_DBG("detaching transport %s\n", ft->name);
579 
580 out_attach:
581 	mutex_unlock(&ft_mutex);
582 	return rc;
583 
584 }
585 EXPORT_SYMBOL(fcoe_transport_detach);
586 
587 static 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) {
595 		if (i >= PAGE_SIZE - IFNAMSIZ)
596 			break;
597 		i += snprintf(&buffer[i], IFNAMSIZ, "%s ", ft->name);
598 	}
599 	mutex_unlock(&ft_mutex);
600 	if (i == j)
601 		i += snprintf(&buffer[i], IFNAMSIZ, "none");
602 	return i;
603 }
604 
605 static int __init fcoe_transport_init(void)
606 {
607 	register_netdevice_notifier(&libfcoe_notifier);
608 	return 0;
609 }
610 
611 static int fcoe_transport_exit(void)
612 {
613 	struct fcoe_transport *ft;
614 
615 	unregister_netdevice_notifier(&libfcoe_notifier);
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 
625 static 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 
639 	mutex_lock(&fn_mutex);
640 	list_add(&nm->list, &fcoe_netdevs);
641 	mutex_unlock(&fn_mutex);
642 	return 0;
643 }
644 
645 
646 static void fcoe_del_netdev_mapping(struct net_device *netdev)
647 {
648 	struct fcoe_netdev_mapping *nm = NULL, *tmp;
649 
650 	mutex_lock(&fn_mutex);
651 	list_for_each_entry_safe(nm, tmp, &fcoe_netdevs, list) {
652 		if (nm->netdev == netdev) {
653 			list_del(&nm->list);
654 			kfree(nm);
655 			mutex_unlock(&fn_mutex);
656 			return;
657 		}
658 	}
659 	mutex_unlock(&fn_mutex);
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  */
672 static 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 
677 	mutex_lock(&fn_mutex);
678 	list_for_each_entry(nm, &fcoe_netdevs, list) {
679 		if (netdev == nm->netdev) {
680 			ft = nm->ft;
681 			mutex_unlock(&fn_mutex);
682 			return ft;
683 		}
684 	}
685 
686 	mutex_unlock(&fn_mutex);
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  */
696 static 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 /**
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  */
721 static int libfcoe_device_notification(struct notifier_block *notifier,
722 				    ulong event, void *ptr)
723 {
724 	struct net_device *netdev = netdev_notifier_info_to_dev(ptr);
725 
726 	switch (event) {
727 	case NETDEV_UNREGISTER:
728 		LIBFCOE_TRANSPORT_DBG("NETDEV_UNREGISTER %s\n",
729 				      netdev->name);
730 		fcoe_del_netdev_mapping(netdev);
731 		break;
732 	}
733 	return NOTIFY_OK;
734 }
735 
736 ssize_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;
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 
787 	LIBFCOE_TRANSPORT_DBG("transport %s succeeded to create fcoe on %s.\n",
788 			      ft->name, netdev->name);
789 
790 out_putdev:
791 	dev_put(netdev);
792 out_nodev:
793 	mutex_unlock(&ft_mutex);
794 	if (rc)
795 		return rc;
796 	return count;
797 }
798 
799 ssize_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 */
831 out_putdev:
832 	dev_put(netdev);
833 out_nodev:
834 	mutex_unlock(&ft_mutex);
835 	return rc;
836 }
837 EXPORT_SYMBOL(fcoe_ctlr_destroy_store);
838 
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  */
849 static 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 
856 	mutex_lock(&ft_mutex);
857 
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 
897 out_putdev:
898 	dev_put(netdev);
899 out_nodev:
900 	mutex_unlock(&ft_mutex);
901 	return rc;
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  */
914 static 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 
920 	mutex_lock(&ft_mutex);
921 
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 
942 out_putdev:
943 	dev_put(netdev);
944 out_nodev:
945 	mutex_unlock(&ft_mutex);
946 	return rc;
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  */
958 static 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 
964 	mutex_lock(&ft_mutex);
965 
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 
976 out_putdev:
977 	dev_put(netdev);
978 out_nodev:
979 	mutex_unlock(&ft_mutex);
980 	return rc;
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  */
992 static 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 
998 	mutex_lock(&ft_mutex);
999 
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 
1010 out_putdev:
1011 	dev_put(netdev);
1012 out_nodev:
1013 	mutex_unlock(&ft_mutex);
1014 	return rc;
1015 }
1016 
1017 /**
1018  * libfcoe_init() - Initialization routine for libfcoe.ko
1019  */
1020 static int __init libfcoe_init(void)
1021 {
1022 	int rc = 0;
1023 
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;
1033 }
1034 module_init(libfcoe_init);
1035 
1036 /**
1037  * libfcoe_exit() - Tear down libfcoe.ko
1038  */
1039 static void __exit libfcoe_exit(void)
1040 {
1041 	fcoe_sysfs_teardown();
1042 	fcoe_transport_exit();
1043 }
1044 module_exit(libfcoe_exit);
1045