blob: 83e78f922054e9626622319b1647d593b9153040 [file] [log] [blame]
Yi Zoufdecf312011-01-28 16:04:55 -08001/*
2 * Copyright(c) 2008 - 2011 Intel Corporation. All rights reserved.
3 *
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms and conditions of the GNU General Public License,
6 * version 2, as published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope it will be useful, but WITHOUT
9 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
10 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
11 * more details.
12 *
13 * You should have received a copy of the GNU General Public License along with
14 * this program; if not, write to the Free Software Foundation, Inc.,
15 * 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
16 *
17 * Maintained at www.Open-FCoE.org
18 */
19
20#include <linux/types.h>
21#include <linux/module.h>
22#include <linux/kernel.h>
23#include <linux/list.h>
24#include <linux/netdevice.h>
25#include <linux/errno.h>
Bhanu Prakash Gollapudi8597ae82011-01-28 16:05:37 -080026#include <linux/crc32.h>
Yi Zoufdecf312011-01-28 16:04:55 -080027#include <scsi/libfcoe.h>
28
29#include "libfcoe.h"
30
Yi Zoue01efc32011-01-28 16:05:06 -080031MODULE_AUTHOR("Open-FCoE.org");
32MODULE_DESCRIPTION("FIP discovery protocol and FCoE transport for FCoE HBAs");
33MODULE_LICENSE("GPL v2");
34
Yi Zoufdecf312011-01-28 16:04:55 -080035static int fcoe_transport_create(const char *, struct kernel_param *);
36static int fcoe_transport_destroy(const char *, struct kernel_param *);
37static int fcoe_transport_show(char *buffer, const struct kernel_param *kp);
38static struct fcoe_transport *fcoe_transport_lookup(struct net_device *device);
39static struct fcoe_transport *fcoe_netdev_map_lookup(struct net_device *device);
40static int fcoe_transport_enable(const char *, struct kernel_param *);
41static int fcoe_transport_disable(const char *, struct kernel_param *);
Bhanu Prakash Gollapudi70be6342011-02-25 15:03:17 -080042static int libfcoe_device_notification(struct notifier_block *notifier,
43 ulong event, void *ptr);
Yi Zoufdecf312011-01-28 16:04:55 -080044
45static LIST_HEAD(fcoe_transports);
Yi Zoufdecf312011-01-28 16:04:55 -080046static DEFINE_MUTEX(ft_mutex);
Bhanu Prakash Gollapudi70be6342011-02-25 15:03:17 -080047static LIST_HEAD(fcoe_netdevs);
48static DEFINE_MUTEX(fn_mutex);
Yi Zoufdecf312011-01-28 16:04:55 -080049
Yi Zoue01efc32011-01-28 16:05:06 -080050unsigned int libfcoe_debug_logging;
51module_param_named(debug_logging, libfcoe_debug_logging, int, S_IRUGO|S_IWUSR);
52MODULE_PARM_DESC(debug_logging, "a bit mask of logging levels");
53
Yi Zoufdecf312011-01-28 16:04:55 -080054module_param_call(show, NULL, fcoe_transport_show, NULL, S_IRUSR);
55__MODULE_PARM_TYPE(show, "string");
56MODULE_PARM_DESC(show, " Show attached FCoE transports");
57
58module_param_call(create, fcoe_transport_create, NULL,
59 (void *)FIP_MODE_FABRIC, S_IWUSR);
60__MODULE_PARM_TYPE(create, "string");
61MODULE_PARM_DESC(create, " Creates fcoe instance on a ethernet interface");
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");
71MODULE_PARM_DESC(destroy, " Destroys fcoe instance on a ethernet interface");
72
73module_param_call(enable, fcoe_transport_enable, NULL, NULL, S_IWUSR);
74__MODULE_PARM_TYPE(enable, "string");
75MODULE_PARM_DESC(enable, " Enables fcoe on a ethernet interface.");
76
77module_param_call(disable, fcoe_transport_disable, NULL, NULL, S_IWUSR);
78__MODULE_PARM_TYPE(disable, "string");
79MODULE_PARM_DESC(disable, " Disables fcoe on a ethernet interface.");
80
Bhanu Prakash Gollapudi70be6342011-02-25 15:03:17 -080081/* notification function for packets from net device */
82static struct notifier_block libfcoe_notifier = {
83 .notifier_call = libfcoe_device_notification,
84};
85
Bhanu Prakash Gollapudi814740d2011-10-03 16:45:01 -070086void __fcoe_get_lesb(struct fc_lport *lport,
87 struct fc_els_lesb *fc_lesb,
88 struct net_device *netdev)
89{
90 unsigned int cpu;
91 u32 lfc, vlfc, mdac;
Vasu Dev1bd49b42012-05-25 10:26:43 -070092 struct fc_stats *stats;
Bhanu Prakash Gollapudi814740d2011-10-03 16:45:01 -070093 struct fcoe_fc_els_lesb *lesb;
94 struct rtnl_link_stats64 temp;
95
96 lfc = 0;
97 vlfc = 0;
98 mdac = 0;
99 lesb = (struct fcoe_fc_els_lesb *)fc_lesb;
100 memset(lesb, 0, sizeof(*lesb));
101 for_each_possible_cpu(cpu) {
Vasu Dev1bd49b42012-05-25 10:26:43 -0700102 stats = per_cpu_ptr(lport->stats, cpu);
103 lfc += stats->LinkFailureCount;
104 vlfc += stats->VLinkFailureCount;
105 mdac += stats->MissDiscAdvCount;
Bhanu Prakash Gollapudi814740d2011-10-03 16:45:01 -0700106 }
107 lesb->lesb_link_fail = htonl(lfc);
108 lesb->lesb_vlink_fail = htonl(vlfc);
109 lesb->lesb_miss_fka = htonl(mdac);
110 lesb->lesb_fcs_error =
111 htonl(dev_get_stats(netdev, &temp)->rx_crc_errors);
112}
113EXPORT_SYMBOL_GPL(__fcoe_get_lesb);
114
Bhanu Prakash Gollapudid8348952011-08-04 17:38:49 -0700115void fcoe_wwn_to_str(u64 wwn, char *buf, int len)
116{
117 u8 wwpn[8];
118
119 u64_to_wwn(wwn, wwpn);
120 snprintf(buf, len, "%02x%02x%02x%02x%02x%02x%02x%02x",
121 wwpn[0], wwpn[1], wwpn[2], wwpn[3],
122 wwpn[4], wwpn[5], wwpn[6], wwpn[7]);
123}
124EXPORT_SYMBOL_GPL(fcoe_wwn_to_str);
125
126/**
127 * fcoe_validate_vport_create() - Validate a vport before creating it
128 * @vport: NPIV port to be created
129 *
130 * This routine is meant to add validation for a vport before creating it
131 * via fcoe_vport_create().
132 * Current validations are:
133 * - WWPN supplied is unique for given lport
134 */
135int fcoe_validate_vport_create(struct fc_vport *vport)
136{
137 struct Scsi_Host *shost = vport_to_shost(vport);
138 struct fc_lport *n_port = shost_priv(shost);
139 struct fc_lport *vn_port;
140 int rc = 0;
141 char buf[32];
142
143 mutex_lock(&n_port->lp_mutex);
144
145 fcoe_wwn_to_str(vport->port_name, buf, sizeof(buf));
146 /* Check if the wwpn is not same as that of the lport */
147 if (!memcmp(&n_port->wwpn, &vport->port_name, sizeof(u64))) {
148 LIBFCOE_TRANSPORT_DBG("vport WWPN 0x%s is same as that of the "
149 "base port WWPN\n", buf);
150 rc = -EINVAL;
151 goto out;
152 }
153
154 /* Check if there is any existing vport with same wwpn */
155 list_for_each_entry(vn_port, &n_port->vports, list) {
156 if (!memcmp(&vn_port->wwpn, &vport->port_name, sizeof(u64))) {
157 LIBFCOE_TRANSPORT_DBG("vport with given WWPN 0x%s "
158 "already exists\n", buf);
159 rc = -EINVAL;
160 break;
161 }
162 }
163out:
164 mutex_unlock(&n_port->lp_mutex);
165 return rc;
166}
167EXPORT_SYMBOL_GPL(fcoe_validate_vport_create);
168
169/**
170 * fcoe_get_wwn() - Get the world wide name from LLD if it supports it
171 * @netdev: the associated net device
172 * @wwn: the output WWN
173 * @type: the type of WWN (WWPN or WWNN)
174 *
175 * Returns: 0 for success
176 */
177int fcoe_get_wwn(struct net_device *netdev, u64 *wwn, int type)
178{
179 const struct net_device_ops *ops = netdev->netdev_ops;
180
181 if (ops->ndo_fcoe_get_wwn)
182 return ops->ndo_fcoe_get_wwn(netdev, wwn, type);
183 return -EINVAL;
184}
185EXPORT_SYMBOL_GPL(fcoe_get_wwn);
186
Yi Zoufdecf312011-01-28 16:04:55 -0800187/**
Bhanu Prakash Gollapudi8597ae82011-01-28 16:05:37 -0800188 * fcoe_fc_crc() - Calculates the CRC for a given frame
189 * @fp: The frame to be checksumed
190 *
191 * This uses crc32() routine to calculate the CRC for a frame
192 *
193 * Return: The 32 bit CRC value
194 */
195u32 fcoe_fc_crc(struct fc_frame *fp)
196{
197 struct sk_buff *skb = fp_skb(fp);
198 struct skb_frag_struct *frag;
199 unsigned char *data;
200 unsigned long off, len, clen;
201 u32 crc;
202 unsigned i;
203
204 crc = crc32(~0, skb->data, skb_headlen(skb));
205
206 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
207 frag = &skb_shinfo(skb)->frags[i];
208 off = frag->page_offset;
Eric Dumazet9e903e02011-10-18 21:00:24 +0000209 len = skb_frag_size(frag);
Bhanu Prakash Gollapudi8597ae82011-01-28 16:05:37 -0800210 while (len > 0) {
211 clen = min(len, PAGE_SIZE - (off & ~PAGE_MASK));
Ian Campbell165c68d2011-08-24 22:28:15 +0000212 data = kmap_atomic(
Cong Wang77dfce02011-11-25 23:14:23 +0800213 skb_frag_page(frag) + (off >> PAGE_SHIFT));
Bhanu Prakash Gollapudi8597ae82011-01-28 16:05:37 -0800214 crc = crc32(crc, data + (off & ~PAGE_MASK), clen);
Cong Wang77dfce02011-11-25 23:14:23 +0800215 kunmap_atomic(data);
Bhanu Prakash Gollapudi8597ae82011-01-28 16:05:37 -0800216 off += clen;
217 len -= clen;
218 }
219 }
220 return crc;
221}
222EXPORT_SYMBOL_GPL(fcoe_fc_crc);
223
224/**
225 * fcoe_start_io() - Start FCoE I/O
226 * @skb: The packet to be transmitted
227 *
228 * This routine is called from the net device to start transmitting
229 * FCoE packets.
230 *
231 * Returns: 0 for success
232 */
233int fcoe_start_io(struct sk_buff *skb)
234{
235 struct sk_buff *nskb;
236 int rc;
237
238 nskb = skb_clone(skb, GFP_ATOMIC);
239 if (!nskb)
240 return -ENOMEM;
241 rc = dev_queue_xmit(nskb);
242 if (rc != 0)
243 return rc;
244 kfree_skb(skb);
245 return 0;
246}
247EXPORT_SYMBOL_GPL(fcoe_start_io);
248
249
250/**
251 * fcoe_clean_pending_queue() - Dequeue a skb and free it
252 * @lport: The local port to dequeue a skb on
253 */
254void fcoe_clean_pending_queue(struct fc_lport *lport)
255{
256 struct fcoe_port *port = lport_priv(lport);
257 struct sk_buff *skb;
258
259 spin_lock_bh(&port->fcoe_pending_queue.lock);
260 while ((skb = __skb_dequeue(&port->fcoe_pending_queue)) != NULL) {
261 spin_unlock_bh(&port->fcoe_pending_queue.lock);
262 kfree_skb(skb);
263 spin_lock_bh(&port->fcoe_pending_queue.lock);
264 }
265 spin_unlock_bh(&port->fcoe_pending_queue.lock);
266}
267EXPORT_SYMBOL_GPL(fcoe_clean_pending_queue);
268
269/**
270 * fcoe_check_wait_queue() - Attempt to clear the transmit backlog
271 * @lport: The local port whose backlog is to be cleared
272 *
273 * This empties the wait_queue, dequeues the head of the wait_queue queue
274 * and calls fcoe_start_io() for each packet. If all skb have been
275 * transmitted it returns the qlen. If an error occurs it restores
276 * wait_queue (to try again later) and returns -1.
277 *
278 * The wait_queue is used when the skb transmit fails. The failed skb
279 * will go in the wait_queue which will be emptied by the timer function or
280 * by the next skb transmit.
281 */
282void fcoe_check_wait_queue(struct fc_lport *lport, struct sk_buff *skb)
283{
284 struct fcoe_port *port = lport_priv(lport);
285 int rc;
286
287 spin_lock_bh(&port->fcoe_pending_queue.lock);
288
289 if (skb)
290 __skb_queue_tail(&port->fcoe_pending_queue, skb);
291
292 if (port->fcoe_pending_queue_active)
293 goto out;
294 port->fcoe_pending_queue_active = 1;
295
296 while (port->fcoe_pending_queue.qlen) {
297 /* keep qlen > 0 until fcoe_start_io succeeds */
298 port->fcoe_pending_queue.qlen++;
299 skb = __skb_dequeue(&port->fcoe_pending_queue);
300
301 spin_unlock_bh(&port->fcoe_pending_queue.lock);
302 rc = fcoe_start_io(skb);
303 spin_lock_bh(&port->fcoe_pending_queue.lock);
304
305 if (rc) {
306 __skb_queue_head(&port->fcoe_pending_queue, skb);
307 /* undo temporary increment above */
308 port->fcoe_pending_queue.qlen--;
309 break;
310 }
311 /* undo temporary increment above */
312 port->fcoe_pending_queue.qlen--;
313 }
314
315 if (port->fcoe_pending_queue.qlen < port->min_queue_depth)
316 lport->qfull = 0;
317 if (port->fcoe_pending_queue.qlen && !timer_pending(&port->timer))
318 mod_timer(&port->timer, jiffies + 2);
319 port->fcoe_pending_queue_active = 0;
320out:
321 if (port->fcoe_pending_queue.qlen > port->max_queue_depth)
322 lport->qfull = 1;
323 spin_unlock_bh(&port->fcoe_pending_queue.lock);
324}
325EXPORT_SYMBOL_GPL(fcoe_check_wait_queue);
326
327/**
328 * fcoe_queue_timer() - The fcoe queue timer
329 * @lport: The local port
330 *
331 * Calls fcoe_check_wait_queue on timeout
332 */
333void fcoe_queue_timer(ulong lport)
334{
335 fcoe_check_wait_queue((struct fc_lport *)lport, NULL);
336}
337EXPORT_SYMBOL_GPL(fcoe_queue_timer);
338
339/**
340 * fcoe_get_paged_crc_eof() - Allocate a page to be used for the trailer CRC
341 * @skb: The packet to be transmitted
342 * @tlen: The total length of the trailer
343 * @fps: The fcoe context
344 *
345 * This routine allocates a page for frame trailers. The page is re-used if
346 * there is enough room left on it for the current trailer. If there isn't
347 * enough buffer left a new page is allocated for the trailer. Reference to
348 * the page from this function as well as the skbs using the page fragments
349 * ensure that the page is freed at the appropriate time.
350 *
351 * Returns: 0 for success
352 */
353int fcoe_get_paged_crc_eof(struct sk_buff *skb, int tlen,
354 struct fcoe_percpu_s *fps)
355{
356 struct page *page;
357
358 page = fps->crc_eof_page;
359 if (!page) {
360 page = alloc_page(GFP_ATOMIC);
361 if (!page)
362 return -ENOMEM;
363
364 fps->crc_eof_page = page;
365 fps->crc_eof_offset = 0;
366 }
367
368 get_page(page);
369 skb_fill_page_desc(skb, skb_shinfo(skb)->nr_frags, page,
370 fps->crc_eof_offset, tlen);
371 skb->len += tlen;
372 skb->data_len += tlen;
373 skb->truesize += tlen;
374 fps->crc_eof_offset += sizeof(struct fcoe_crc_eof);
375
376 if (fps->crc_eof_offset >= PAGE_SIZE) {
377 fps->crc_eof_page = NULL;
378 fps->crc_eof_offset = 0;
379 put_page(page);
380 }
381
382 return 0;
383}
384EXPORT_SYMBOL_GPL(fcoe_get_paged_crc_eof);
385
386/**
Yi Zoufdecf312011-01-28 16:04:55 -0800387 * fcoe_transport_lookup - find an fcoe transport that matches a netdev
388 * @netdev: The netdev to look for from all attached transports
389 *
390 * Returns : ptr to the fcoe transport that supports this netdev or NULL
391 * if not found.
392 *
393 * The ft_mutex should be held when this is called
394 */
395static struct fcoe_transport *fcoe_transport_lookup(struct net_device *netdev)
396{
397 struct fcoe_transport *ft = NULL;
398
399 list_for_each_entry(ft, &fcoe_transports, list)
400 if (ft->match && ft->match(netdev))
401 return ft;
402 return NULL;
403}
404
405/**
406 * fcoe_transport_attach - Attaches an FCoE transport
407 * @ft: The fcoe transport to be attached
408 *
409 * Returns : 0 for success
410 */
411int fcoe_transport_attach(struct fcoe_transport *ft)
412{
413 int rc = 0;
414
415 mutex_lock(&ft_mutex);
416 if (ft->attached) {
417 LIBFCOE_TRANSPORT_DBG("transport %s already attached\n",
418 ft->name);
419 rc = -EEXIST;
420 goto out_attach;
421 }
422
423 /* Add default transport to the tail */
424 if (strcmp(ft->name, FCOE_TRANSPORT_DEFAULT))
425 list_add(&ft->list, &fcoe_transports);
426 else
427 list_add_tail(&ft->list, &fcoe_transports);
428
429 ft->attached = true;
430 LIBFCOE_TRANSPORT_DBG("attaching transport %s\n", ft->name);
431
432out_attach:
433 mutex_unlock(&ft_mutex);
434 return rc;
435}
436EXPORT_SYMBOL(fcoe_transport_attach);
437
438/**
Yi Zou4ef7fb12011-04-01 16:06:30 -0700439 * fcoe_transport_detach - Detaches an FCoE transport
Yi Zoufdecf312011-01-28 16:04:55 -0800440 * @ft: The fcoe transport to be attached
441 *
442 * Returns : 0 for success
443 */
444int fcoe_transport_detach(struct fcoe_transport *ft)
445{
446 int rc = 0;
Yi Zou69922fc2011-04-01 16:06:19 -0700447 struct fcoe_netdev_mapping *nm = NULL, *tmp;
Yi Zoufdecf312011-01-28 16:04:55 -0800448
449 mutex_lock(&ft_mutex);
450 if (!ft->attached) {
451 LIBFCOE_TRANSPORT_DBG("transport %s already detached\n",
452 ft->name);
453 rc = -ENODEV;
454 goto out_attach;
455 }
456
Yi Zou69922fc2011-04-01 16:06:19 -0700457 /* remove netdev mapping for this transport as it is going away */
458 mutex_lock(&fn_mutex);
459 list_for_each_entry_safe(nm, tmp, &fcoe_netdevs, list) {
460 if (nm->ft == ft) {
461 LIBFCOE_TRANSPORT_DBG("transport %s going away, "
462 "remove its netdev mapping for %s\n",
463 ft->name, nm->netdev->name);
464 list_del(&nm->list);
465 kfree(nm);
466 }
467 }
468 mutex_unlock(&fn_mutex);
469
Yi Zoufdecf312011-01-28 16:04:55 -0800470 list_del(&ft->list);
471 ft->attached = false;
472 LIBFCOE_TRANSPORT_DBG("detaching transport %s\n", ft->name);
473
474out_attach:
475 mutex_unlock(&ft_mutex);
476 return rc;
477
478}
479EXPORT_SYMBOL(fcoe_transport_detach);
480
481static int fcoe_transport_show(char *buffer, const struct kernel_param *kp)
482{
483 int i, j;
484 struct fcoe_transport *ft = NULL;
485
486 i = j = sprintf(buffer, "Attached FCoE transports:");
487 mutex_lock(&ft_mutex);
488 list_for_each_entry(ft, &fcoe_transports, list) {
Yi Zoua01a5a52011-04-01 16:06:25 -0700489 if (i >= PAGE_SIZE - IFNAMSIZ)
Yi Zoufdecf312011-01-28 16:04:55 -0800490 break;
Yi Zoua01a5a52011-04-01 16:06:25 -0700491 i += snprintf(&buffer[i], IFNAMSIZ, "%s ", ft->name);
Yi Zoufdecf312011-01-28 16:04:55 -0800492 }
493 mutex_unlock(&ft_mutex);
494 if (i == j)
495 i += snprintf(&buffer[i], IFNAMSIZ, "none");
496 return i;
497}
498
499static int __init fcoe_transport_init(void)
500{
Bhanu Prakash Gollapudi70be6342011-02-25 15:03:17 -0800501 register_netdevice_notifier(&libfcoe_notifier);
Yi Zoufdecf312011-01-28 16:04:55 -0800502 return 0;
503}
504
Mark Rustad87098bd2012-06-06 11:59:48 -0700505static int fcoe_transport_exit(void)
Yi Zoufdecf312011-01-28 16:04:55 -0800506{
507 struct fcoe_transport *ft;
508
Bhanu Prakash Gollapudi70be6342011-02-25 15:03:17 -0800509 unregister_netdevice_notifier(&libfcoe_notifier);
Yi Zoufdecf312011-01-28 16:04:55 -0800510 mutex_lock(&ft_mutex);
511 list_for_each_entry(ft, &fcoe_transports, list)
512 printk(KERN_ERR "FCoE transport %s is still attached!\n",
513 ft->name);
514 mutex_unlock(&ft_mutex);
515 return 0;
516}
517
518
519static int fcoe_add_netdev_mapping(struct net_device *netdev,
520 struct fcoe_transport *ft)
521{
522 struct fcoe_netdev_mapping *nm;
523
524 nm = kmalloc(sizeof(*nm), GFP_KERNEL);
525 if (!nm) {
526 printk(KERN_ERR "Unable to allocate netdev_mapping");
527 return -ENOMEM;
528 }
529
530 nm->netdev = netdev;
531 nm->ft = ft;
532
Bhanu Prakash Gollapudi70be6342011-02-25 15:03:17 -0800533 mutex_lock(&fn_mutex);
Yi Zoufdecf312011-01-28 16:04:55 -0800534 list_add(&nm->list, &fcoe_netdevs);
Bhanu Prakash Gollapudi70be6342011-02-25 15:03:17 -0800535 mutex_unlock(&fn_mutex);
Yi Zoufdecf312011-01-28 16:04:55 -0800536 return 0;
537}
538
539
540static void fcoe_del_netdev_mapping(struct net_device *netdev)
541{
542 struct fcoe_netdev_mapping *nm = NULL, *tmp;
543
Bhanu Prakash Gollapudi70be6342011-02-25 15:03:17 -0800544 mutex_lock(&fn_mutex);
Yi Zoufdecf312011-01-28 16:04:55 -0800545 list_for_each_entry_safe(nm, tmp, &fcoe_netdevs, list) {
546 if (nm->netdev == netdev) {
547 list_del(&nm->list);
548 kfree(nm);
Bhanu Prakash Gollapudi70be6342011-02-25 15:03:17 -0800549 mutex_unlock(&fn_mutex);
Yi Zoufdecf312011-01-28 16:04:55 -0800550 return;
551 }
552 }
Bhanu Prakash Gollapudi70be6342011-02-25 15:03:17 -0800553 mutex_unlock(&fn_mutex);
Yi Zoufdecf312011-01-28 16:04:55 -0800554}
555
556
557/**
558 * fcoe_netdev_map_lookup - find the fcoe transport that matches the netdev on which
559 * it was created
560 *
561 * Returns : ptr to the fcoe transport that supports this netdev or NULL
562 * if not found.
563 *
564 * The ft_mutex should be held when this is called
565 */
566static struct fcoe_transport *fcoe_netdev_map_lookup(struct net_device *netdev)
567{
568 struct fcoe_transport *ft = NULL;
569 struct fcoe_netdev_mapping *nm;
570
Bhanu Prakash Gollapudi70be6342011-02-25 15:03:17 -0800571 mutex_lock(&fn_mutex);
Yi Zoufdecf312011-01-28 16:04:55 -0800572 list_for_each_entry(nm, &fcoe_netdevs, list) {
573 if (netdev == nm->netdev) {
574 ft = nm->ft;
Bhanu Prakash Gollapudi70be6342011-02-25 15:03:17 -0800575 mutex_unlock(&fn_mutex);
Yi Zoufdecf312011-01-28 16:04:55 -0800576 return ft;
577 }
578 }
579
Bhanu Prakash Gollapudi70be6342011-02-25 15:03:17 -0800580 mutex_unlock(&fn_mutex);
Yi Zoufdecf312011-01-28 16:04:55 -0800581 return NULL;
582}
583
584/**
585 * fcoe_if_to_netdev() - Parse a name buffer to get a net device
586 * @buffer: The name of the net device
587 *
588 * Returns: NULL or a ptr to net_device
589 */
590static struct net_device *fcoe_if_to_netdev(const char *buffer)
591{
592 char *cp;
593 char ifname[IFNAMSIZ + 2];
594
595 if (buffer) {
596 strlcpy(ifname, buffer, IFNAMSIZ);
597 cp = ifname + strlen(ifname);
598 while (--cp >= ifname && *cp == '\n')
599 *cp = '\0';
600 return dev_get_by_name(&init_net, ifname);
601 }
602 return NULL;
603}
604
605/**
Bhanu Prakash Gollapudi70be6342011-02-25 15:03:17 -0800606 * libfcoe_device_notification() - Handler for net device events
607 * @notifier: The context of the notification
608 * @event: The type of event
609 * @ptr: The net device that the event was on
610 *
611 * This function is called by the Ethernet driver in case of link change event.
612 *
613 * Returns: 0 for success
614 */
615static int libfcoe_device_notification(struct notifier_block *notifier,
616 ulong event, void *ptr)
617{
618 struct net_device *netdev = ptr;
619
620 switch (event) {
621 case NETDEV_UNREGISTER:
Robert Loveb99fbf62012-02-10 17:17:59 -0800622 LIBFCOE_TRANSPORT_DBG("NETDEV_UNREGISTER %s\n",
623 netdev->name);
Bhanu Prakash Gollapudi70be6342011-02-25 15:03:17 -0800624 fcoe_del_netdev_mapping(netdev);
625 break;
626 }
627 return NOTIFY_OK;
628}
629
Robert Love6a891b02012-11-27 06:53:30 +0000630ssize_t fcoe_ctlr_create_store(struct bus_type *bus,
631 const char *buf, size_t count)
632{
633 struct net_device *netdev = NULL;
634 struct fcoe_transport *ft = NULL;
635 struct fcoe_ctlr_device *ctlr_dev = NULL;
636 int rc = 0;
637 int err;
638
639 mutex_lock(&ft_mutex);
640
641 netdev = fcoe_if_to_netdev(buf);
642 if (!netdev) {
643 LIBFCOE_TRANSPORT_DBG("Invalid device %s.\n", buf);
644 rc = -ENODEV;
645 goto out_nodev;
646 }
647
648 ft = fcoe_netdev_map_lookup(netdev);
649 if (ft) {
650 LIBFCOE_TRANSPORT_DBG("transport %s already has existing "
651 "FCoE instance on %s.\n",
652 ft->name, netdev->name);
653 rc = -EEXIST;
654 goto out_putdev;
655 }
656
657 ft = fcoe_transport_lookup(netdev);
658 if (!ft) {
659 LIBFCOE_TRANSPORT_DBG("no FCoE transport found for %s.\n",
660 netdev->name);
661 rc = -ENODEV;
662 goto out_putdev;
663 }
664
665 /* pass to transport create */
666 err = ft->alloc ? ft->alloc(netdev) : -ENODEV;
667 if (err) {
668 fcoe_del_netdev_mapping(netdev);
669 rc = -ENOMEM;
670 goto out_putdev;
671 }
672
673 err = fcoe_add_netdev_mapping(netdev, ft);
674 if (err) {
675 LIBFCOE_TRANSPORT_DBG("failed to add new netdev mapping "
676 "for FCoE transport %s for %s.\n",
677 ft->name, netdev->name);
678 rc = -ENODEV;
679 goto out_putdev;
680 }
681
682 LIBFCOE_TRANSPORT_DBG("transport %s %s to create fcoe on %s.\n",
683 ft->name, (ctlr_dev) ? "succeeded" : "failed",
684 netdev->name);
685
686out_putdev:
687 dev_put(netdev);
688out_nodev:
689 mutex_unlock(&ft_mutex);
690 if (rc)
691 return rc;
692 return count;
693}
694
695ssize_t fcoe_ctlr_destroy_store(struct bus_type *bus,
696 const char *buf, size_t count)
697{
698 int rc = -ENODEV;
699 struct net_device *netdev = NULL;
700 struct fcoe_transport *ft = NULL;
701
702 mutex_lock(&ft_mutex);
703
704 netdev = fcoe_if_to_netdev(buf);
705 if (!netdev) {
706 LIBFCOE_TRANSPORT_DBG("invalid device %s.\n", buf);
707 goto out_nodev;
708 }
709
710 ft = fcoe_netdev_map_lookup(netdev);
711 if (!ft) {
712 LIBFCOE_TRANSPORT_DBG("no FCoE transport found for %s.\n",
713 netdev->name);
714 goto out_putdev;
715 }
716
717 /* pass to transport destroy */
718 rc = ft->destroy(netdev);
719 if (rc)
720 goto out_putdev;
721
722 fcoe_del_netdev_mapping(netdev);
723 LIBFCOE_TRANSPORT_DBG("transport %s %s to destroy fcoe on %s.\n",
724 ft->name, (rc) ? "failed" : "succeeded",
725 netdev->name);
726 rc = count; /* required for successful return */
727out_putdev:
728 dev_put(netdev);
729out_nodev:
730 mutex_unlock(&ft_mutex);
731 return rc;
732}
733EXPORT_SYMBOL(fcoe_ctlr_destroy_store);
Bhanu Prakash Gollapudi70be6342011-02-25 15:03:17 -0800734
735/**
Yi Zoufdecf312011-01-28 16:04:55 -0800736 * fcoe_transport_create() - Create a fcoe interface
737 * @buffer: The name of the Ethernet interface to create on
738 * @kp: The associated kernel param
739 *
740 * Called from sysfs. This holds the ft_mutex while calling the
741 * registered fcoe transport's create function.
742 *
743 * Returns: 0 for success
744 */
745static int fcoe_transport_create(const char *buffer, struct kernel_param *kp)
746{
747 int rc = -ENODEV;
748 struct net_device *netdev = NULL;
749 struct fcoe_transport *ft = NULL;
750 enum fip_state fip_mode = (enum fip_state)(long)kp->arg;
751
Robert Loveb3960af2011-04-01 16:05:53 -0700752 mutex_lock(&ft_mutex);
753
Yi Zoufdecf312011-01-28 16:04:55 -0800754 netdev = fcoe_if_to_netdev(buffer);
755 if (!netdev) {
756 LIBFCOE_TRANSPORT_DBG("Invalid device %s.\n", buffer);
757 goto out_nodev;
758 }
759
760 ft = fcoe_netdev_map_lookup(netdev);
761 if (ft) {
762 LIBFCOE_TRANSPORT_DBG("transport %s already has existing "
763 "FCoE instance on %s.\n",
764 ft->name, netdev->name);
765 rc = -EEXIST;
766 goto out_putdev;
767 }
768
769 ft = fcoe_transport_lookup(netdev);
770 if (!ft) {
771 LIBFCOE_TRANSPORT_DBG("no FCoE transport found for %s.\n",
772 netdev->name);
773 goto out_putdev;
774 }
775
776 rc = fcoe_add_netdev_mapping(netdev, ft);
777 if (rc) {
778 LIBFCOE_TRANSPORT_DBG("failed to add new netdev mapping "
779 "for FCoE transport %s for %s.\n",
780 ft->name, netdev->name);
781 goto out_putdev;
782 }
783
784 /* pass to transport create */
785 rc = ft->create ? ft->create(netdev, fip_mode) : -ENODEV;
786 if (rc)
787 fcoe_del_netdev_mapping(netdev);
788
789 LIBFCOE_TRANSPORT_DBG("transport %s %s to create fcoe on %s.\n",
790 ft->name, (rc) ? "failed" : "succeeded",
791 netdev->name);
792
793out_putdev:
794 dev_put(netdev);
795out_nodev:
796 mutex_unlock(&ft_mutex);
Robert Loveb3960af2011-04-01 16:05:53 -0700797 return rc;
Yi Zoufdecf312011-01-28 16:04:55 -0800798}
799
800/**
801 * fcoe_transport_destroy() - Destroy a FCoE interface
802 * @buffer: The name of the Ethernet interface to be destroyed
803 * @kp: The associated kernel parameter
804 *
805 * Called from sysfs. This holds the ft_mutex while calling the
806 * registered fcoe transport's destroy function.
807 *
808 * Returns: 0 for success
809 */
810static int fcoe_transport_destroy(const char *buffer, struct kernel_param *kp)
811{
812 int rc = -ENODEV;
813 struct net_device *netdev = NULL;
814 struct fcoe_transport *ft = NULL;
815
Robert Loveb3960af2011-04-01 16:05:53 -0700816 mutex_lock(&ft_mutex);
817
Yi Zoufdecf312011-01-28 16:04:55 -0800818 netdev = fcoe_if_to_netdev(buffer);
819 if (!netdev) {
820 LIBFCOE_TRANSPORT_DBG("invalid device %s.\n", buffer);
821 goto out_nodev;
822 }
823
824 ft = fcoe_netdev_map_lookup(netdev);
825 if (!ft) {
826 LIBFCOE_TRANSPORT_DBG("no FCoE transport found for %s.\n",
827 netdev->name);
828 goto out_putdev;
829 }
830
831 /* pass to transport destroy */
832 rc = ft->destroy ? ft->destroy(netdev) : -ENODEV;
833 fcoe_del_netdev_mapping(netdev);
834 LIBFCOE_TRANSPORT_DBG("transport %s %s to destroy fcoe on %s.\n",
835 ft->name, (rc) ? "failed" : "succeeded",
836 netdev->name);
837
838out_putdev:
839 dev_put(netdev);
840out_nodev:
841 mutex_unlock(&ft_mutex);
Robert Loveb3960af2011-04-01 16:05:53 -0700842 return rc;
Yi Zoufdecf312011-01-28 16:04:55 -0800843}
844
845/**
846 * fcoe_transport_disable() - Disables a FCoE interface
847 * @buffer: The name of the Ethernet interface to be disabled
848 * @kp: The associated kernel parameter
849 *
850 * Called from sysfs.
851 *
852 * Returns: 0 for success
853 */
854static int fcoe_transport_disable(const char *buffer, struct kernel_param *kp)
855{
856 int rc = -ENODEV;
857 struct net_device *netdev = NULL;
858 struct fcoe_transport *ft = NULL;
859
Robert Loveb3960af2011-04-01 16:05:53 -0700860 mutex_lock(&ft_mutex);
861
Yi Zoufdecf312011-01-28 16:04:55 -0800862 netdev = fcoe_if_to_netdev(buffer);
863 if (!netdev)
864 goto out_nodev;
865
866 ft = fcoe_netdev_map_lookup(netdev);
867 if (!ft)
868 goto out_putdev;
869
870 rc = ft->disable ? ft->disable(netdev) : -ENODEV;
871
872out_putdev:
873 dev_put(netdev);
874out_nodev:
875 mutex_unlock(&ft_mutex);
876
877 if (rc == -ERESTARTSYS)
878 return restart_syscall();
879 else
880 return rc;
881}
882
883/**
884 * fcoe_transport_enable() - Enables a FCoE interface
885 * @buffer: The name of the Ethernet interface to be enabled
886 * @kp: The associated kernel parameter
887 *
888 * Called from sysfs.
889 *
890 * Returns: 0 for success
891 */
892static int fcoe_transport_enable(const char *buffer, struct kernel_param *kp)
893{
894 int rc = -ENODEV;
895 struct net_device *netdev = NULL;
896 struct fcoe_transport *ft = NULL;
897
Robert Loveb3960af2011-04-01 16:05:53 -0700898 mutex_lock(&ft_mutex);
899
Yi Zoufdecf312011-01-28 16:04:55 -0800900 netdev = fcoe_if_to_netdev(buffer);
901 if (!netdev)
902 goto out_nodev;
903
904 ft = fcoe_netdev_map_lookup(netdev);
905 if (!ft)
906 goto out_putdev;
907
908 rc = ft->enable ? ft->enable(netdev) : -ENODEV;
909
910out_putdev:
911 dev_put(netdev);
912out_nodev:
913 mutex_unlock(&ft_mutex);
Robert Loveb3960af2011-04-01 16:05:53 -0700914 return rc;
Yi Zoufdecf312011-01-28 16:04:55 -0800915}
916
917/**
918 * libfcoe_init() - Initialization routine for libfcoe.ko
919 */
920static int __init libfcoe_init(void)
921{
Robert Love9a74e882012-05-22 19:06:21 -0700922 int rc = 0;
Yi Zoufdecf312011-01-28 16:04:55 -0800923
Robert Love9a74e882012-05-22 19:06:21 -0700924 rc = fcoe_transport_init();
925 if (rc)
926 return rc;
927
928 rc = fcoe_sysfs_setup();
929 if (rc)
930 fcoe_transport_exit();
931
932 return rc;
Yi Zoufdecf312011-01-28 16:04:55 -0800933}
934module_init(libfcoe_init);
935
936/**
937 * libfcoe_exit() - Tear down libfcoe.ko
938 */
939static void __exit libfcoe_exit(void)
940{
Robert Love9a74e882012-05-22 19:06:21 -0700941 fcoe_sysfs_teardown();
Yi Zoufdecf312011-01-28 16:04:55 -0800942 fcoe_transport_exit();
943}
944module_exit(libfcoe_exit);