Commit | Line | Data |
---|---|---|
fdecf31b YZ |
1 | /* |
2 | * Copyright(c) 2008 - 2011 Intel Corporation. All rights reserved. | |
3 | * | |
4 | * This program is free software; you can redistribute it and/or modify it | |
5 | * under the terms and conditions of the GNU General Public License, | |
6 | * version 2, as published by the Free Software Foundation. | |
7 | * | |
8 | * This program is distributed in the hope it will be useful, but WITHOUT | |
9 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | |
10 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for | |
11 | * more details. | |
12 | * | |
13 | * You should have received a copy of the GNU General Public License along with | |
14 | * this program; if not, write to the Free Software Foundation, Inc., | |
15 | * 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA. | |
16 | * | |
17 | * Maintained at www.Open-FCoE.org | |
18 | */ | |
19 | ||
20 | #include <linux/types.h> | |
21 | #include <linux/module.h> | |
22 | #include <linux/kernel.h> | |
23 | #include <linux/list.h> | |
24 | #include <linux/netdevice.h> | |
25 | #include <linux/errno.h> | |
26 | #include <scsi/libfcoe.h> | |
27 | ||
28 | #include "libfcoe.h" | |
29 | ||
e01efc33 YZ |
30 | MODULE_AUTHOR("Open-FCoE.org"); |
31 | MODULE_DESCRIPTION("FIP discovery protocol and FCoE transport for FCoE HBAs"); | |
32 | MODULE_LICENSE("GPL v2"); | |
33 | ||
fdecf31b YZ |
34 | static int fcoe_transport_create(const char *, struct kernel_param *); |
35 | static int fcoe_transport_destroy(const char *, struct kernel_param *); | |
36 | static int fcoe_transport_show(char *buffer, const struct kernel_param *kp); | |
37 | static struct fcoe_transport *fcoe_transport_lookup(struct net_device *device); | |
38 | static struct fcoe_transport *fcoe_netdev_map_lookup(struct net_device *device); | |
39 | static int fcoe_transport_enable(const char *, struct kernel_param *); | |
40 | static int fcoe_transport_disable(const char *, struct kernel_param *); | |
41 | ||
42 | static LIST_HEAD(fcoe_transports); | |
43 | static LIST_HEAD(fcoe_netdevs); | |
44 | static DEFINE_MUTEX(ft_mutex); | |
45 | ||
e01efc33 YZ |
46 | unsigned int libfcoe_debug_logging; |
47 | module_param_named(debug_logging, libfcoe_debug_logging, int, S_IRUGO|S_IWUSR); | |
48 | MODULE_PARM_DESC(debug_logging, "a bit mask of logging levels"); | |
49 | ||
fdecf31b YZ |
50 | module_param_call(show, NULL, fcoe_transport_show, NULL, S_IRUSR); |
51 | __MODULE_PARM_TYPE(show, "string"); | |
52 | MODULE_PARM_DESC(show, " Show attached FCoE transports"); | |
53 | ||
54 | module_param_call(create, fcoe_transport_create, NULL, | |
55 | (void *)FIP_MODE_FABRIC, S_IWUSR); | |
56 | __MODULE_PARM_TYPE(create, "string"); | |
57 | MODULE_PARM_DESC(create, " Creates fcoe instance on a ethernet interface"); | |
58 | ||
59 | module_param_call(create_vn2vn, fcoe_transport_create, NULL, | |
60 | (void *)FIP_MODE_VN2VN, S_IWUSR); | |
61 | __MODULE_PARM_TYPE(create_vn2vn, "string"); | |
62 | MODULE_PARM_DESC(create_vn2vn, " Creates a VN_node to VN_node FCoE instance " | |
63 | "on an Ethernet interface"); | |
64 | ||
65 | module_param_call(destroy, fcoe_transport_destroy, NULL, NULL, S_IWUSR); | |
66 | __MODULE_PARM_TYPE(destroy, "string"); | |
67 | MODULE_PARM_DESC(destroy, " Destroys fcoe instance on a ethernet interface"); | |
68 | ||
69 | module_param_call(enable, fcoe_transport_enable, NULL, NULL, S_IWUSR); | |
70 | __MODULE_PARM_TYPE(enable, "string"); | |
71 | MODULE_PARM_DESC(enable, " Enables fcoe on a ethernet interface."); | |
72 | ||
73 | module_param_call(disable, fcoe_transport_disable, NULL, NULL, S_IWUSR); | |
74 | __MODULE_PARM_TYPE(disable, "string"); | |
75 | MODULE_PARM_DESC(disable, " Disables fcoe on a ethernet interface."); | |
76 | ||
77 | /** | |
78 | * fcoe_transport_lookup - find an fcoe transport that matches a netdev | |
79 | * @netdev: The netdev to look for from all attached transports | |
80 | * | |
81 | * Returns : ptr to the fcoe transport that supports this netdev or NULL | |
82 | * if not found. | |
83 | * | |
84 | * The ft_mutex should be held when this is called | |
85 | */ | |
86 | static struct fcoe_transport *fcoe_transport_lookup(struct net_device *netdev) | |
87 | { | |
88 | struct fcoe_transport *ft = NULL; | |
89 | ||
90 | list_for_each_entry(ft, &fcoe_transports, list) | |
91 | if (ft->match && ft->match(netdev)) | |
92 | return ft; | |
93 | return NULL; | |
94 | } | |
95 | ||
96 | /** | |
97 | * fcoe_transport_attach - Attaches an FCoE transport | |
98 | * @ft: The fcoe transport to be attached | |
99 | * | |
100 | * Returns : 0 for success | |
101 | */ | |
102 | int fcoe_transport_attach(struct fcoe_transport *ft) | |
103 | { | |
104 | int rc = 0; | |
105 | ||
106 | mutex_lock(&ft_mutex); | |
107 | if (ft->attached) { | |
108 | LIBFCOE_TRANSPORT_DBG("transport %s already attached\n", | |
109 | ft->name); | |
110 | rc = -EEXIST; | |
111 | goto out_attach; | |
112 | } | |
113 | ||
114 | /* Add default transport to the tail */ | |
115 | if (strcmp(ft->name, FCOE_TRANSPORT_DEFAULT)) | |
116 | list_add(&ft->list, &fcoe_transports); | |
117 | else | |
118 | list_add_tail(&ft->list, &fcoe_transports); | |
119 | ||
120 | ft->attached = true; | |
121 | LIBFCOE_TRANSPORT_DBG("attaching transport %s\n", ft->name); | |
122 | ||
123 | out_attach: | |
124 | mutex_unlock(&ft_mutex); | |
125 | return rc; | |
126 | } | |
127 | EXPORT_SYMBOL(fcoe_transport_attach); | |
128 | ||
129 | /** | |
130 | * fcoe_transport_attach - Detaches an FCoE transport | |
131 | * @ft: The fcoe transport to be attached | |
132 | * | |
133 | * Returns : 0 for success | |
134 | */ | |
135 | int fcoe_transport_detach(struct fcoe_transport *ft) | |
136 | { | |
137 | int rc = 0; | |
138 | ||
139 | mutex_lock(&ft_mutex); | |
140 | if (!ft->attached) { | |
141 | LIBFCOE_TRANSPORT_DBG("transport %s already detached\n", | |
142 | ft->name); | |
143 | rc = -ENODEV; | |
144 | goto out_attach; | |
145 | } | |
146 | ||
147 | list_del(&ft->list); | |
148 | ft->attached = false; | |
149 | LIBFCOE_TRANSPORT_DBG("detaching transport %s\n", ft->name); | |
150 | ||
151 | out_attach: | |
152 | mutex_unlock(&ft_mutex); | |
153 | return rc; | |
154 | ||
155 | } | |
156 | EXPORT_SYMBOL(fcoe_transport_detach); | |
157 | ||
158 | static int fcoe_transport_show(char *buffer, const struct kernel_param *kp) | |
159 | { | |
160 | int i, j; | |
161 | struct fcoe_transport *ft = NULL; | |
162 | ||
163 | i = j = sprintf(buffer, "Attached FCoE transports:"); | |
164 | mutex_lock(&ft_mutex); | |
165 | list_for_each_entry(ft, &fcoe_transports, list) { | |
166 | i += snprintf(&buffer[i], IFNAMSIZ, "%s ", ft->name); | |
167 | if (i >= PAGE_SIZE) | |
168 | break; | |
169 | } | |
170 | mutex_unlock(&ft_mutex); | |
171 | if (i == j) | |
172 | i += snprintf(&buffer[i], IFNAMSIZ, "none"); | |
173 | return i; | |
174 | } | |
175 | ||
176 | static int __init fcoe_transport_init(void) | |
177 | { | |
178 | return 0; | |
179 | } | |
180 | ||
181 | static int __exit fcoe_transport_exit(void) | |
182 | { | |
183 | struct fcoe_transport *ft; | |
184 | ||
185 | mutex_lock(&ft_mutex); | |
186 | list_for_each_entry(ft, &fcoe_transports, list) | |
187 | printk(KERN_ERR "FCoE transport %s is still attached!\n", | |
188 | ft->name); | |
189 | mutex_unlock(&ft_mutex); | |
190 | return 0; | |
191 | } | |
192 | ||
193 | ||
194 | static int fcoe_add_netdev_mapping(struct net_device *netdev, | |
195 | struct fcoe_transport *ft) | |
196 | { | |
197 | struct fcoe_netdev_mapping *nm; | |
198 | ||
199 | nm = kmalloc(sizeof(*nm), GFP_KERNEL); | |
200 | if (!nm) { | |
201 | printk(KERN_ERR "Unable to allocate netdev_mapping"); | |
202 | return -ENOMEM; | |
203 | } | |
204 | ||
205 | nm->netdev = netdev; | |
206 | nm->ft = ft; | |
207 | ||
208 | list_add(&nm->list, &fcoe_netdevs); | |
209 | return 0; | |
210 | } | |
211 | ||
212 | ||
213 | static void fcoe_del_netdev_mapping(struct net_device *netdev) | |
214 | { | |
215 | struct fcoe_netdev_mapping *nm = NULL, *tmp; | |
216 | ||
217 | list_for_each_entry_safe(nm, tmp, &fcoe_netdevs, list) { | |
218 | if (nm->netdev == netdev) { | |
219 | list_del(&nm->list); | |
220 | kfree(nm); | |
221 | return; | |
222 | } | |
223 | } | |
224 | } | |
225 | ||
226 | ||
227 | /** | |
228 | * fcoe_netdev_map_lookup - find the fcoe transport that matches the netdev on which | |
229 | * it was created | |
230 | * | |
231 | * Returns : ptr to the fcoe transport that supports this netdev or NULL | |
232 | * if not found. | |
233 | * | |
234 | * The ft_mutex should be held when this is called | |
235 | */ | |
236 | static struct fcoe_transport *fcoe_netdev_map_lookup(struct net_device *netdev) | |
237 | { | |
238 | struct fcoe_transport *ft = NULL; | |
239 | struct fcoe_netdev_mapping *nm; | |
240 | ||
241 | list_for_each_entry(nm, &fcoe_netdevs, list) { | |
242 | if (netdev == nm->netdev) { | |
243 | ft = nm->ft; | |
244 | return ft; | |
245 | } | |
246 | } | |
247 | ||
248 | return NULL; | |
249 | } | |
250 | ||
251 | /** | |
252 | * fcoe_if_to_netdev() - Parse a name buffer to get a net device | |
253 | * @buffer: The name of the net device | |
254 | * | |
255 | * Returns: NULL or a ptr to net_device | |
256 | */ | |
257 | static struct net_device *fcoe_if_to_netdev(const char *buffer) | |
258 | { | |
259 | char *cp; | |
260 | char ifname[IFNAMSIZ + 2]; | |
261 | ||
262 | if (buffer) { | |
263 | strlcpy(ifname, buffer, IFNAMSIZ); | |
264 | cp = ifname + strlen(ifname); | |
265 | while (--cp >= ifname && *cp == '\n') | |
266 | *cp = '\0'; | |
267 | return dev_get_by_name(&init_net, ifname); | |
268 | } | |
269 | return NULL; | |
270 | } | |
271 | ||
272 | /** | |
273 | * fcoe_transport_create() - Create a fcoe interface | |
274 | * @buffer: The name of the Ethernet interface to create on | |
275 | * @kp: The associated kernel param | |
276 | * | |
277 | * Called from sysfs. This holds the ft_mutex while calling the | |
278 | * registered fcoe transport's create function. | |
279 | * | |
280 | * Returns: 0 for success | |
281 | */ | |
282 | static int fcoe_transport_create(const char *buffer, struct kernel_param *kp) | |
283 | { | |
284 | int rc = -ENODEV; | |
285 | struct net_device *netdev = NULL; | |
286 | struct fcoe_transport *ft = NULL; | |
287 | enum fip_state fip_mode = (enum fip_state)(long)kp->arg; | |
288 | ||
289 | if (!mutex_trylock(&ft_mutex)) | |
290 | return restart_syscall(); | |
291 | ||
292 | #ifdef CONFIG_LIBFCOE_MODULE | |
293 | /* | |
294 | * Make sure the module has been initialized, and is not about to be | |
295 | * removed. Module parameter sysfs files are writable before the | |
296 | * module_init function is called and after module_exit. | |
297 | */ | |
298 | if (THIS_MODULE->state != MODULE_STATE_LIVE) | |
299 | goto out_nodev; | |
300 | #endif | |
301 | ||
302 | netdev = fcoe_if_to_netdev(buffer); | |
303 | if (!netdev) { | |
304 | LIBFCOE_TRANSPORT_DBG("Invalid device %s.\n", buffer); | |
305 | goto out_nodev; | |
306 | } | |
307 | ||
308 | ft = fcoe_netdev_map_lookup(netdev); | |
309 | if (ft) { | |
310 | LIBFCOE_TRANSPORT_DBG("transport %s already has existing " | |
311 | "FCoE instance on %s.\n", | |
312 | ft->name, netdev->name); | |
313 | rc = -EEXIST; | |
314 | goto out_putdev; | |
315 | } | |
316 | ||
317 | ft = fcoe_transport_lookup(netdev); | |
318 | if (!ft) { | |
319 | LIBFCOE_TRANSPORT_DBG("no FCoE transport found for %s.\n", | |
320 | netdev->name); | |
321 | goto out_putdev; | |
322 | } | |
323 | ||
324 | rc = fcoe_add_netdev_mapping(netdev, ft); | |
325 | if (rc) { | |
326 | LIBFCOE_TRANSPORT_DBG("failed to add new netdev mapping " | |
327 | "for FCoE transport %s for %s.\n", | |
328 | ft->name, netdev->name); | |
329 | goto out_putdev; | |
330 | } | |
331 | ||
332 | /* pass to transport create */ | |
333 | rc = ft->create ? ft->create(netdev, fip_mode) : -ENODEV; | |
334 | if (rc) | |
335 | fcoe_del_netdev_mapping(netdev); | |
336 | ||
337 | LIBFCOE_TRANSPORT_DBG("transport %s %s to create fcoe on %s.\n", | |
338 | ft->name, (rc) ? "failed" : "succeeded", | |
339 | netdev->name); | |
340 | ||
341 | out_putdev: | |
342 | dev_put(netdev); | |
343 | out_nodev: | |
344 | mutex_unlock(&ft_mutex); | |
345 | if (rc == -ERESTARTSYS) | |
346 | return restart_syscall(); | |
347 | else | |
348 | return rc; | |
349 | } | |
350 | ||
351 | /** | |
352 | * fcoe_transport_destroy() - Destroy a FCoE interface | |
353 | * @buffer: The name of the Ethernet interface to be destroyed | |
354 | * @kp: The associated kernel parameter | |
355 | * | |
356 | * Called from sysfs. This holds the ft_mutex while calling the | |
357 | * registered fcoe transport's destroy function. | |
358 | * | |
359 | * Returns: 0 for success | |
360 | */ | |
361 | static int fcoe_transport_destroy(const char *buffer, struct kernel_param *kp) | |
362 | { | |
363 | int rc = -ENODEV; | |
364 | struct net_device *netdev = NULL; | |
365 | struct fcoe_transport *ft = NULL; | |
366 | ||
367 | if (!mutex_trylock(&ft_mutex)) | |
368 | return restart_syscall(); | |
369 | ||
370 | #ifdef CONFIG_LIBFCOE_MODULE | |
371 | /* | |
372 | * Make sure the module has been initialized, and is not about to be | |
373 | * removed. Module parameter sysfs files are writable before the | |
374 | * module_init function is called and after module_exit. | |
375 | */ | |
376 | if (THIS_MODULE->state != MODULE_STATE_LIVE) | |
377 | goto out_nodev; | |
378 | #endif | |
379 | ||
380 | netdev = fcoe_if_to_netdev(buffer); | |
381 | if (!netdev) { | |
382 | LIBFCOE_TRANSPORT_DBG("invalid device %s.\n", buffer); | |
383 | goto out_nodev; | |
384 | } | |
385 | ||
386 | ft = fcoe_netdev_map_lookup(netdev); | |
387 | if (!ft) { | |
388 | LIBFCOE_TRANSPORT_DBG("no FCoE transport found for %s.\n", | |
389 | netdev->name); | |
390 | goto out_putdev; | |
391 | } | |
392 | ||
393 | /* pass to transport destroy */ | |
394 | rc = ft->destroy ? ft->destroy(netdev) : -ENODEV; | |
395 | fcoe_del_netdev_mapping(netdev); | |
396 | LIBFCOE_TRANSPORT_DBG("transport %s %s to destroy fcoe on %s.\n", | |
397 | ft->name, (rc) ? "failed" : "succeeded", | |
398 | netdev->name); | |
399 | ||
400 | out_putdev: | |
401 | dev_put(netdev); | |
402 | out_nodev: | |
403 | mutex_unlock(&ft_mutex); | |
404 | ||
405 | if (rc == -ERESTARTSYS) | |
406 | return restart_syscall(); | |
407 | else | |
408 | return rc; | |
409 | } | |
410 | ||
411 | /** | |
412 | * fcoe_transport_disable() - Disables a FCoE interface | |
413 | * @buffer: The name of the Ethernet interface to be disabled | |
414 | * @kp: The associated kernel parameter | |
415 | * | |
416 | * Called from sysfs. | |
417 | * | |
418 | * Returns: 0 for success | |
419 | */ | |
420 | static int fcoe_transport_disable(const char *buffer, struct kernel_param *kp) | |
421 | { | |
422 | int rc = -ENODEV; | |
423 | struct net_device *netdev = NULL; | |
424 | struct fcoe_transport *ft = NULL; | |
425 | ||
426 | if (!mutex_trylock(&ft_mutex)) | |
427 | return restart_syscall(); | |
428 | ||
429 | #ifdef CONFIG_LIBFCOE_MODULE | |
430 | /* | |
431 | * Make sure the module has been initialized, and is not about to be | |
432 | * removed. Module parameter sysfs files are writable before the | |
433 | * module_init function is called and after module_exit. | |
434 | */ | |
435 | if (THIS_MODULE->state != MODULE_STATE_LIVE) | |
436 | goto out_nodev; | |
437 | #endif | |
438 | ||
439 | netdev = fcoe_if_to_netdev(buffer); | |
440 | if (!netdev) | |
441 | goto out_nodev; | |
442 | ||
443 | ft = fcoe_netdev_map_lookup(netdev); | |
444 | if (!ft) | |
445 | goto out_putdev; | |
446 | ||
447 | rc = ft->disable ? ft->disable(netdev) : -ENODEV; | |
448 | ||
449 | out_putdev: | |
450 | dev_put(netdev); | |
451 | out_nodev: | |
452 | mutex_unlock(&ft_mutex); | |
453 | ||
454 | if (rc == -ERESTARTSYS) | |
455 | return restart_syscall(); | |
456 | else | |
457 | return rc; | |
458 | } | |
459 | ||
460 | /** | |
461 | * fcoe_transport_enable() - Enables a FCoE interface | |
462 | * @buffer: The name of the Ethernet interface to be enabled | |
463 | * @kp: The associated kernel parameter | |
464 | * | |
465 | * Called from sysfs. | |
466 | * | |
467 | * Returns: 0 for success | |
468 | */ | |
469 | static int fcoe_transport_enable(const char *buffer, struct kernel_param *kp) | |
470 | { | |
471 | int rc = -ENODEV; | |
472 | struct net_device *netdev = NULL; | |
473 | struct fcoe_transport *ft = NULL; | |
474 | ||
475 | if (!mutex_trylock(&ft_mutex)) | |
476 | return restart_syscall(); | |
477 | ||
478 | #ifdef CONFIG_LIBFCOE_MODULE | |
479 | /* | |
480 | * Make sure the module has been initialized, and is not about to be | |
481 | * removed. Module parameter sysfs files are writable before the | |
482 | * module_init function is called and after module_exit. | |
483 | */ | |
484 | if (THIS_MODULE->state != MODULE_STATE_LIVE) | |
485 | goto out_nodev; | |
486 | #endif | |
487 | ||
488 | netdev = fcoe_if_to_netdev(buffer); | |
489 | if (!netdev) | |
490 | goto out_nodev; | |
491 | ||
492 | ft = fcoe_netdev_map_lookup(netdev); | |
493 | if (!ft) | |
494 | goto out_putdev; | |
495 | ||
496 | rc = ft->enable ? ft->enable(netdev) : -ENODEV; | |
497 | ||
498 | out_putdev: | |
499 | dev_put(netdev); | |
500 | out_nodev: | |
501 | mutex_unlock(&ft_mutex); | |
502 | if (rc == -ERESTARTSYS) | |
503 | return restart_syscall(); | |
504 | else | |
505 | return rc; | |
506 | } | |
507 | ||
508 | /** | |
509 | * libfcoe_init() - Initialization routine for libfcoe.ko | |
510 | */ | |
511 | static int __init libfcoe_init(void) | |
512 | { | |
513 | fcoe_transport_init(); | |
514 | ||
515 | return 0; | |
516 | } | |
517 | module_init(libfcoe_init); | |
518 | ||
519 | /** | |
520 | * libfcoe_exit() - Tear down libfcoe.ko | |
521 | */ | |
522 | static void __exit libfcoe_exit(void) | |
523 | { | |
524 | fcoe_transport_exit(); | |
525 | } | |
526 | module_exit(libfcoe_exit); |