Yi Zou | fdecf31 | 2011-01-28 16:04:55 -0800 | [diff] [blame] | 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> |
Bhanu Prakash Gollapudi | 8597ae8 | 2011-01-28 16:05:37 -0800 | [diff] [blame^] | 26 | #include <linux/crc32.h> |
Yi Zou | fdecf31 | 2011-01-28 16:04:55 -0800 | [diff] [blame] | 27 | #include <scsi/libfcoe.h> |
| 28 | |
| 29 | #include "libfcoe.h" |
| 30 | |
Yi Zou | e01efc3 | 2011-01-28 16:05:06 -0800 | [diff] [blame] | 31 | MODULE_AUTHOR("Open-FCoE.org"); |
| 32 | MODULE_DESCRIPTION("FIP discovery protocol and FCoE transport for FCoE HBAs"); |
| 33 | MODULE_LICENSE("GPL v2"); |
| 34 | |
Yi Zou | fdecf31 | 2011-01-28 16:04:55 -0800 | [diff] [blame] | 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 | |
| 43 | static LIST_HEAD(fcoe_transports); |
| 44 | static LIST_HEAD(fcoe_netdevs); |
| 45 | static DEFINE_MUTEX(ft_mutex); |
| 46 | |
Yi Zou | e01efc3 | 2011-01-28 16:05:06 -0800 | [diff] [blame] | 47 | unsigned int libfcoe_debug_logging; |
| 48 | module_param_named(debug_logging, libfcoe_debug_logging, int, S_IRUGO|S_IWUSR); |
| 49 | MODULE_PARM_DESC(debug_logging, "a bit mask of logging levels"); |
| 50 | |
Yi Zou | fdecf31 | 2011-01-28 16:04:55 -0800 | [diff] [blame] | 51 | module_param_call(show, NULL, fcoe_transport_show, NULL, S_IRUSR); |
| 52 | __MODULE_PARM_TYPE(show, "string"); |
| 53 | MODULE_PARM_DESC(show, " Show attached FCoE transports"); |
| 54 | |
| 55 | module_param_call(create, fcoe_transport_create, NULL, |
| 56 | (void *)FIP_MODE_FABRIC, S_IWUSR); |
| 57 | __MODULE_PARM_TYPE(create, "string"); |
| 58 | MODULE_PARM_DESC(create, " Creates fcoe instance on a ethernet interface"); |
| 59 | |
| 60 | module_param_call(create_vn2vn, fcoe_transport_create, NULL, |
| 61 | (void *)FIP_MODE_VN2VN, S_IWUSR); |
| 62 | __MODULE_PARM_TYPE(create_vn2vn, "string"); |
| 63 | MODULE_PARM_DESC(create_vn2vn, " Creates a VN_node to VN_node FCoE instance " |
| 64 | "on an Ethernet interface"); |
| 65 | |
| 66 | module_param_call(destroy, fcoe_transport_destroy, NULL, NULL, S_IWUSR); |
| 67 | __MODULE_PARM_TYPE(destroy, "string"); |
| 68 | MODULE_PARM_DESC(destroy, " Destroys fcoe instance on a ethernet interface"); |
| 69 | |
| 70 | module_param_call(enable, fcoe_transport_enable, NULL, NULL, S_IWUSR); |
| 71 | __MODULE_PARM_TYPE(enable, "string"); |
| 72 | MODULE_PARM_DESC(enable, " Enables fcoe on a ethernet interface."); |
| 73 | |
| 74 | module_param_call(disable, fcoe_transport_disable, NULL, NULL, S_IWUSR); |
| 75 | __MODULE_PARM_TYPE(disable, "string"); |
| 76 | MODULE_PARM_DESC(disable, " Disables fcoe on a ethernet interface."); |
| 77 | |
| 78 | /** |
Bhanu Prakash Gollapudi | 8597ae8 | 2011-01-28 16:05:37 -0800 | [diff] [blame^] | 79 | * fcoe_fc_crc() - Calculates the CRC for a given frame |
| 80 | * @fp: The frame to be checksumed |
| 81 | * |
| 82 | * This uses crc32() routine to calculate the CRC for a frame |
| 83 | * |
| 84 | * Return: The 32 bit CRC value |
| 85 | */ |
| 86 | u32 fcoe_fc_crc(struct fc_frame *fp) |
| 87 | { |
| 88 | struct sk_buff *skb = fp_skb(fp); |
| 89 | struct skb_frag_struct *frag; |
| 90 | unsigned char *data; |
| 91 | unsigned long off, len, clen; |
| 92 | u32 crc; |
| 93 | unsigned i; |
| 94 | |
| 95 | crc = crc32(~0, skb->data, skb_headlen(skb)); |
| 96 | |
| 97 | for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) { |
| 98 | frag = &skb_shinfo(skb)->frags[i]; |
| 99 | off = frag->page_offset; |
| 100 | len = frag->size; |
| 101 | while (len > 0) { |
| 102 | clen = min(len, PAGE_SIZE - (off & ~PAGE_MASK)); |
| 103 | data = kmap_atomic(frag->page + (off >> PAGE_SHIFT), |
| 104 | KM_SKB_DATA_SOFTIRQ); |
| 105 | crc = crc32(crc, data + (off & ~PAGE_MASK), clen); |
| 106 | kunmap_atomic(data, KM_SKB_DATA_SOFTIRQ); |
| 107 | off += clen; |
| 108 | len -= clen; |
| 109 | } |
| 110 | } |
| 111 | return crc; |
| 112 | } |
| 113 | EXPORT_SYMBOL_GPL(fcoe_fc_crc); |
| 114 | |
| 115 | /** |
| 116 | * fcoe_start_io() - Start FCoE I/O |
| 117 | * @skb: The packet to be transmitted |
| 118 | * |
| 119 | * This routine is called from the net device to start transmitting |
| 120 | * FCoE packets. |
| 121 | * |
| 122 | * Returns: 0 for success |
| 123 | */ |
| 124 | int fcoe_start_io(struct sk_buff *skb) |
| 125 | { |
| 126 | struct sk_buff *nskb; |
| 127 | int rc; |
| 128 | |
| 129 | nskb = skb_clone(skb, GFP_ATOMIC); |
| 130 | if (!nskb) |
| 131 | return -ENOMEM; |
| 132 | rc = dev_queue_xmit(nskb); |
| 133 | if (rc != 0) |
| 134 | return rc; |
| 135 | kfree_skb(skb); |
| 136 | return 0; |
| 137 | } |
| 138 | EXPORT_SYMBOL_GPL(fcoe_start_io); |
| 139 | |
| 140 | |
| 141 | /** |
| 142 | * fcoe_clean_pending_queue() - Dequeue a skb and free it |
| 143 | * @lport: The local port to dequeue a skb on |
| 144 | */ |
| 145 | void fcoe_clean_pending_queue(struct fc_lport *lport) |
| 146 | { |
| 147 | struct fcoe_port *port = lport_priv(lport); |
| 148 | struct sk_buff *skb; |
| 149 | |
| 150 | spin_lock_bh(&port->fcoe_pending_queue.lock); |
| 151 | while ((skb = __skb_dequeue(&port->fcoe_pending_queue)) != NULL) { |
| 152 | spin_unlock_bh(&port->fcoe_pending_queue.lock); |
| 153 | kfree_skb(skb); |
| 154 | spin_lock_bh(&port->fcoe_pending_queue.lock); |
| 155 | } |
| 156 | spin_unlock_bh(&port->fcoe_pending_queue.lock); |
| 157 | } |
| 158 | EXPORT_SYMBOL_GPL(fcoe_clean_pending_queue); |
| 159 | |
| 160 | /** |
| 161 | * fcoe_check_wait_queue() - Attempt to clear the transmit backlog |
| 162 | * @lport: The local port whose backlog is to be cleared |
| 163 | * |
| 164 | * This empties the wait_queue, dequeues the head of the wait_queue queue |
| 165 | * and calls fcoe_start_io() for each packet. If all skb have been |
| 166 | * transmitted it returns the qlen. If an error occurs it restores |
| 167 | * wait_queue (to try again later) and returns -1. |
| 168 | * |
| 169 | * The wait_queue is used when the skb transmit fails. The failed skb |
| 170 | * will go in the wait_queue which will be emptied by the timer function or |
| 171 | * by the next skb transmit. |
| 172 | */ |
| 173 | void fcoe_check_wait_queue(struct fc_lport *lport, struct sk_buff *skb) |
| 174 | { |
| 175 | struct fcoe_port *port = lport_priv(lport); |
| 176 | int rc; |
| 177 | |
| 178 | spin_lock_bh(&port->fcoe_pending_queue.lock); |
| 179 | |
| 180 | if (skb) |
| 181 | __skb_queue_tail(&port->fcoe_pending_queue, skb); |
| 182 | |
| 183 | if (port->fcoe_pending_queue_active) |
| 184 | goto out; |
| 185 | port->fcoe_pending_queue_active = 1; |
| 186 | |
| 187 | while (port->fcoe_pending_queue.qlen) { |
| 188 | /* keep qlen > 0 until fcoe_start_io succeeds */ |
| 189 | port->fcoe_pending_queue.qlen++; |
| 190 | skb = __skb_dequeue(&port->fcoe_pending_queue); |
| 191 | |
| 192 | spin_unlock_bh(&port->fcoe_pending_queue.lock); |
| 193 | rc = fcoe_start_io(skb); |
| 194 | spin_lock_bh(&port->fcoe_pending_queue.lock); |
| 195 | |
| 196 | if (rc) { |
| 197 | __skb_queue_head(&port->fcoe_pending_queue, skb); |
| 198 | /* undo temporary increment above */ |
| 199 | port->fcoe_pending_queue.qlen--; |
| 200 | break; |
| 201 | } |
| 202 | /* undo temporary increment above */ |
| 203 | port->fcoe_pending_queue.qlen--; |
| 204 | } |
| 205 | |
| 206 | if (port->fcoe_pending_queue.qlen < port->min_queue_depth) |
| 207 | lport->qfull = 0; |
| 208 | if (port->fcoe_pending_queue.qlen && !timer_pending(&port->timer)) |
| 209 | mod_timer(&port->timer, jiffies + 2); |
| 210 | port->fcoe_pending_queue_active = 0; |
| 211 | out: |
| 212 | if (port->fcoe_pending_queue.qlen > port->max_queue_depth) |
| 213 | lport->qfull = 1; |
| 214 | spin_unlock_bh(&port->fcoe_pending_queue.lock); |
| 215 | } |
| 216 | EXPORT_SYMBOL_GPL(fcoe_check_wait_queue); |
| 217 | |
| 218 | /** |
| 219 | * fcoe_queue_timer() - The fcoe queue timer |
| 220 | * @lport: The local port |
| 221 | * |
| 222 | * Calls fcoe_check_wait_queue on timeout |
| 223 | */ |
| 224 | void fcoe_queue_timer(ulong lport) |
| 225 | { |
| 226 | fcoe_check_wait_queue((struct fc_lport *)lport, NULL); |
| 227 | } |
| 228 | EXPORT_SYMBOL_GPL(fcoe_queue_timer); |
| 229 | |
| 230 | /** |
| 231 | * fcoe_get_paged_crc_eof() - Allocate a page to be used for the trailer CRC |
| 232 | * @skb: The packet to be transmitted |
| 233 | * @tlen: The total length of the trailer |
| 234 | * @fps: The fcoe context |
| 235 | * |
| 236 | * This routine allocates a page for frame trailers. The page is re-used if |
| 237 | * there is enough room left on it for the current trailer. If there isn't |
| 238 | * enough buffer left a new page is allocated for the trailer. Reference to |
| 239 | * the page from this function as well as the skbs using the page fragments |
| 240 | * ensure that the page is freed at the appropriate time. |
| 241 | * |
| 242 | * Returns: 0 for success |
| 243 | */ |
| 244 | int fcoe_get_paged_crc_eof(struct sk_buff *skb, int tlen, |
| 245 | struct fcoe_percpu_s *fps) |
| 246 | { |
| 247 | struct page *page; |
| 248 | |
| 249 | page = fps->crc_eof_page; |
| 250 | if (!page) { |
| 251 | page = alloc_page(GFP_ATOMIC); |
| 252 | if (!page) |
| 253 | return -ENOMEM; |
| 254 | |
| 255 | fps->crc_eof_page = page; |
| 256 | fps->crc_eof_offset = 0; |
| 257 | } |
| 258 | |
| 259 | get_page(page); |
| 260 | skb_fill_page_desc(skb, skb_shinfo(skb)->nr_frags, page, |
| 261 | fps->crc_eof_offset, tlen); |
| 262 | skb->len += tlen; |
| 263 | skb->data_len += tlen; |
| 264 | skb->truesize += tlen; |
| 265 | fps->crc_eof_offset += sizeof(struct fcoe_crc_eof); |
| 266 | |
| 267 | if (fps->crc_eof_offset >= PAGE_SIZE) { |
| 268 | fps->crc_eof_page = NULL; |
| 269 | fps->crc_eof_offset = 0; |
| 270 | put_page(page); |
| 271 | } |
| 272 | |
| 273 | return 0; |
| 274 | } |
| 275 | EXPORT_SYMBOL_GPL(fcoe_get_paged_crc_eof); |
| 276 | |
| 277 | /** |
Yi Zou | fdecf31 | 2011-01-28 16:04:55 -0800 | [diff] [blame] | 278 | * fcoe_transport_lookup - find an fcoe transport that matches a netdev |
| 279 | * @netdev: The netdev to look for from all attached transports |
| 280 | * |
| 281 | * Returns : ptr to the fcoe transport that supports this netdev or NULL |
| 282 | * if not found. |
| 283 | * |
| 284 | * The ft_mutex should be held when this is called |
| 285 | */ |
| 286 | static struct fcoe_transport *fcoe_transport_lookup(struct net_device *netdev) |
| 287 | { |
| 288 | struct fcoe_transport *ft = NULL; |
| 289 | |
| 290 | list_for_each_entry(ft, &fcoe_transports, list) |
| 291 | if (ft->match && ft->match(netdev)) |
| 292 | return ft; |
| 293 | return NULL; |
| 294 | } |
| 295 | |
| 296 | /** |
| 297 | * fcoe_transport_attach - Attaches an FCoE transport |
| 298 | * @ft: The fcoe transport to be attached |
| 299 | * |
| 300 | * Returns : 0 for success |
| 301 | */ |
| 302 | int fcoe_transport_attach(struct fcoe_transport *ft) |
| 303 | { |
| 304 | int rc = 0; |
| 305 | |
| 306 | mutex_lock(&ft_mutex); |
| 307 | if (ft->attached) { |
| 308 | LIBFCOE_TRANSPORT_DBG("transport %s already attached\n", |
| 309 | ft->name); |
| 310 | rc = -EEXIST; |
| 311 | goto out_attach; |
| 312 | } |
| 313 | |
| 314 | /* Add default transport to the tail */ |
| 315 | if (strcmp(ft->name, FCOE_TRANSPORT_DEFAULT)) |
| 316 | list_add(&ft->list, &fcoe_transports); |
| 317 | else |
| 318 | list_add_tail(&ft->list, &fcoe_transports); |
| 319 | |
| 320 | ft->attached = true; |
| 321 | LIBFCOE_TRANSPORT_DBG("attaching transport %s\n", ft->name); |
| 322 | |
| 323 | out_attach: |
| 324 | mutex_unlock(&ft_mutex); |
| 325 | return rc; |
| 326 | } |
| 327 | EXPORT_SYMBOL(fcoe_transport_attach); |
| 328 | |
| 329 | /** |
| 330 | * fcoe_transport_attach - Detaches an FCoE transport |
| 331 | * @ft: The fcoe transport to be attached |
| 332 | * |
| 333 | * Returns : 0 for success |
| 334 | */ |
| 335 | int fcoe_transport_detach(struct fcoe_transport *ft) |
| 336 | { |
| 337 | int rc = 0; |
| 338 | |
| 339 | mutex_lock(&ft_mutex); |
| 340 | if (!ft->attached) { |
| 341 | LIBFCOE_TRANSPORT_DBG("transport %s already detached\n", |
| 342 | ft->name); |
| 343 | rc = -ENODEV; |
| 344 | goto out_attach; |
| 345 | } |
| 346 | |
| 347 | list_del(&ft->list); |
| 348 | ft->attached = false; |
| 349 | LIBFCOE_TRANSPORT_DBG("detaching transport %s\n", ft->name); |
| 350 | |
| 351 | out_attach: |
| 352 | mutex_unlock(&ft_mutex); |
| 353 | return rc; |
| 354 | |
| 355 | } |
| 356 | EXPORT_SYMBOL(fcoe_transport_detach); |
| 357 | |
| 358 | static int fcoe_transport_show(char *buffer, const struct kernel_param *kp) |
| 359 | { |
| 360 | int i, j; |
| 361 | struct fcoe_transport *ft = NULL; |
| 362 | |
| 363 | i = j = sprintf(buffer, "Attached FCoE transports:"); |
| 364 | mutex_lock(&ft_mutex); |
| 365 | list_for_each_entry(ft, &fcoe_transports, list) { |
| 366 | i += snprintf(&buffer[i], IFNAMSIZ, "%s ", ft->name); |
| 367 | if (i >= PAGE_SIZE) |
| 368 | break; |
| 369 | } |
| 370 | mutex_unlock(&ft_mutex); |
| 371 | if (i == j) |
| 372 | i += snprintf(&buffer[i], IFNAMSIZ, "none"); |
| 373 | return i; |
| 374 | } |
| 375 | |
| 376 | static int __init fcoe_transport_init(void) |
| 377 | { |
| 378 | return 0; |
| 379 | } |
| 380 | |
| 381 | static int __exit fcoe_transport_exit(void) |
| 382 | { |
| 383 | struct fcoe_transport *ft; |
| 384 | |
| 385 | mutex_lock(&ft_mutex); |
| 386 | list_for_each_entry(ft, &fcoe_transports, list) |
| 387 | printk(KERN_ERR "FCoE transport %s is still attached!\n", |
| 388 | ft->name); |
| 389 | mutex_unlock(&ft_mutex); |
| 390 | return 0; |
| 391 | } |
| 392 | |
| 393 | |
| 394 | static int fcoe_add_netdev_mapping(struct net_device *netdev, |
| 395 | struct fcoe_transport *ft) |
| 396 | { |
| 397 | struct fcoe_netdev_mapping *nm; |
| 398 | |
| 399 | nm = kmalloc(sizeof(*nm), GFP_KERNEL); |
| 400 | if (!nm) { |
| 401 | printk(KERN_ERR "Unable to allocate netdev_mapping"); |
| 402 | return -ENOMEM; |
| 403 | } |
| 404 | |
| 405 | nm->netdev = netdev; |
| 406 | nm->ft = ft; |
| 407 | |
| 408 | list_add(&nm->list, &fcoe_netdevs); |
| 409 | return 0; |
| 410 | } |
| 411 | |
| 412 | |
| 413 | static void fcoe_del_netdev_mapping(struct net_device *netdev) |
| 414 | { |
| 415 | struct fcoe_netdev_mapping *nm = NULL, *tmp; |
| 416 | |
| 417 | list_for_each_entry_safe(nm, tmp, &fcoe_netdevs, list) { |
| 418 | if (nm->netdev == netdev) { |
| 419 | list_del(&nm->list); |
| 420 | kfree(nm); |
| 421 | return; |
| 422 | } |
| 423 | } |
| 424 | } |
| 425 | |
| 426 | |
| 427 | /** |
| 428 | * fcoe_netdev_map_lookup - find the fcoe transport that matches the netdev on which |
| 429 | * it was created |
| 430 | * |
| 431 | * Returns : ptr to the fcoe transport that supports this netdev or NULL |
| 432 | * if not found. |
| 433 | * |
| 434 | * The ft_mutex should be held when this is called |
| 435 | */ |
| 436 | static struct fcoe_transport *fcoe_netdev_map_lookup(struct net_device *netdev) |
| 437 | { |
| 438 | struct fcoe_transport *ft = NULL; |
| 439 | struct fcoe_netdev_mapping *nm; |
| 440 | |
| 441 | list_for_each_entry(nm, &fcoe_netdevs, list) { |
| 442 | if (netdev == nm->netdev) { |
| 443 | ft = nm->ft; |
| 444 | return ft; |
| 445 | } |
| 446 | } |
| 447 | |
| 448 | return NULL; |
| 449 | } |
| 450 | |
| 451 | /** |
| 452 | * fcoe_if_to_netdev() - Parse a name buffer to get a net device |
| 453 | * @buffer: The name of the net device |
| 454 | * |
| 455 | * Returns: NULL or a ptr to net_device |
| 456 | */ |
| 457 | static struct net_device *fcoe_if_to_netdev(const char *buffer) |
| 458 | { |
| 459 | char *cp; |
| 460 | char ifname[IFNAMSIZ + 2]; |
| 461 | |
| 462 | if (buffer) { |
| 463 | strlcpy(ifname, buffer, IFNAMSIZ); |
| 464 | cp = ifname + strlen(ifname); |
| 465 | while (--cp >= ifname && *cp == '\n') |
| 466 | *cp = '\0'; |
| 467 | return dev_get_by_name(&init_net, ifname); |
| 468 | } |
| 469 | return NULL; |
| 470 | } |
| 471 | |
| 472 | /** |
| 473 | * fcoe_transport_create() - Create a fcoe interface |
| 474 | * @buffer: The name of the Ethernet interface to create on |
| 475 | * @kp: The associated kernel param |
| 476 | * |
| 477 | * Called from sysfs. This holds the ft_mutex while calling the |
| 478 | * registered fcoe transport's create function. |
| 479 | * |
| 480 | * Returns: 0 for success |
| 481 | */ |
| 482 | static int fcoe_transport_create(const char *buffer, struct kernel_param *kp) |
| 483 | { |
| 484 | int rc = -ENODEV; |
| 485 | struct net_device *netdev = NULL; |
| 486 | struct fcoe_transport *ft = NULL; |
| 487 | enum fip_state fip_mode = (enum fip_state)(long)kp->arg; |
| 488 | |
| 489 | if (!mutex_trylock(&ft_mutex)) |
| 490 | return restart_syscall(); |
| 491 | |
| 492 | #ifdef CONFIG_LIBFCOE_MODULE |
| 493 | /* |
| 494 | * Make sure the module has been initialized, and is not about to be |
| 495 | * removed. Module parameter sysfs files are writable before the |
| 496 | * module_init function is called and after module_exit. |
| 497 | */ |
| 498 | if (THIS_MODULE->state != MODULE_STATE_LIVE) |
| 499 | goto out_nodev; |
| 500 | #endif |
| 501 | |
| 502 | netdev = fcoe_if_to_netdev(buffer); |
| 503 | if (!netdev) { |
| 504 | LIBFCOE_TRANSPORT_DBG("Invalid device %s.\n", buffer); |
| 505 | goto out_nodev; |
| 506 | } |
| 507 | |
| 508 | ft = fcoe_netdev_map_lookup(netdev); |
| 509 | if (ft) { |
| 510 | LIBFCOE_TRANSPORT_DBG("transport %s already has existing " |
| 511 | "FCoE instance on %s.\n", |
| 512 | ft->name, netdev->name); |
| 513 | rc = -EEXIST; |
| 514 | goto out_putdev; |
| 515 | } |
| 516 | |
| 517 | ft = fcoe_transport_lookup(netdev); |
| 518 | if (!ft) { |
| 519 | LIBFCOE_TRANSPORT_DBG("no FCoE transport found for %s.\n", |
| 520 | netdev->name); |
| 521 | goto out_putdev; |
| 522 | } |
| 523 | |
| 524 | rc = fcoe_add_netdev_mapping(netdev, ft); |
| 525 | if (rc) { |
| 526 | LIBFCOE_TRANSPORT_DBG("failed to add new netdev mapping " |
| 527 | "for FCoE transport %s for %s.\n", |
| 528 | ft->name, netdev->name); |
| 529 | goto out_putdev; |
| 530 | } |
| 531 | |
| 532 | /* pass to transport create */ |
| 533 | rc = ft->create ? ft->create(netdev, fip_mode) : -ENODEV; |
| 534 | if (rc) |
| 535 | fcoe_del_netdev_mapping(netdev); |
| 536 | |
| 537 | LIBFCOE_TRANSPORT_DBG("transport %s %s to create fcoe on %s.\n", |
| 538 | ft->name, (rc) ? "failed" : "succeeded", |
| 539 | netdev->name); |
| 540 | |
| 541 | out_putdev: |
| 542 | dev_put(netdev); |
| 543 | out_nodev: |
| 544 | mutex_unlock(&ft_mutex); |
| 545 | if (rc == -ERESTARTSYS) |
| 546 | return restart_syscall(); |
| 547 | else |
| 548 | return rc; |
| 549 | } |
| 550 | |
| 551 | /** |
| 552 | * fcoe_transport_destroy() - Destroy a FCoE interface |
| 553 | * @buffer: The name of the Ethernet interface to be destroyed |
| 554 | * @kp: The associated kernel parameter |
| 555 | * |
| 556 | * Called from sysfs. This holds the ft_mutex while calling the |
| 557 | * registered fcoe transport's destroy function. |
| 558 | * |
| 559 | * Returns: 0 for success |
| 560 | */ |
| 561 | static int fcoe_transport_destroy(const char *buffer, struct kernel_param *kp) |
| 562 | { |
| 563 | int rc = -ENODEV; |
| 564 | struct net_device *netdev = NULL; |
| 565 | struct fcoe_transport *ft = NULL; |
| 566 | |
| 567 | if (!mutex_trylock(&ft_mutex)) |
| 568 | return restart_syscall(); |
| 569 | |
| 570 | #ifdef CONFIG_LIBFCOE_MODULE |
| 571 | /* |
| 572 | * Make sure the module has been initialized, and is not about to be |
| 573 | * removed. Module parameter sysfs files are writable before the |
| 574 | * module_init function is called and after module_exit. |
| 575 | */ |
| 576 | if (THIS_MODULE->state != MODULE_STATE_LIVE) |
| 577 | goto out_nodev; |
| 578 | #endif |
| 579 | |
| 580 | netdev = fcoe_if_to_netdev(buffer); |
| 581 | if (!netdev) { |
| 582 | LIBFCOE_TRANSPORT_DBG("invalid device %s.\n", buffer); |
| 583 | goto out_nodev; |
| 584 | } |
| 585 | |
| 586 | ft = fcoe_netdev_map_lookup(netdev); |
| 587 | if (!ft) { |
| 588 | LIBFCOE_TRANSPORT_DBG("no FCoE transport found for %s.\n", |
| 589 | netdev->name); |
| 590 | goto out_putdev; |
| 591 | } |
| 592 | |
| 593 | /* pass to transport destroy */ |
| 594 | rc = ft->destroy ? ft->destroy(netdev) : -ENODEV; |
| 595 | fcoe_del_netdev_mapping(netdev); |
| 596 | LIBFCOE_TRANSPORT_DBG("transport %s %s to destroy fcoe on %s.\n", |
| 597 | ft->name, (rc) ? "failed" : "succeeded", |
| 598 | netdev->name); |
| 599 | |
| 600 | out_putdev: |
| 601 | dev_put(netdev); |
| 602 | out_nodev: |
| 603 | mutex_unlock(&ft_mutex); |
| 604 | |
| 605 | if (rc == -ERESTARTSYS) |
| 606 | return restart_syscall(); |
| 607 | else |
| 608 | return rc; |
| 609 | } |
| 610 | |
| 611 | /** |
| 612 | * fcoe_transport_disable() - Disables a FCoE interface |
| 613 | * @buffer: The name of the Ethernet interface to be disabled |
| 614 | * @kp: The associated kernel parameter |
| 615 | * |
| 616 | * Called from sysfs. |
| 617 | * |
| 618 | * Returns: 0 for success |
| 619 | */ |
| 620 | static int fcoe_transport_disable(const char *buffer, struct kernel_param *kp) |
| 621 | { |
| 622 | int rc = -ENODEV; |
| 623 | struct net_device *netdev = NULL; |
| 624 | struct fcoe_transport *ft = NULL; |
| 625 | |
| 626 | if (!mutex_trylock(&ft_mutex)) |
| 627 | return restart_syscall(); |
| 628 | |
| 629 | #ifdef CONFIG_LIBFCOE_MODULE |
| 630 | /* |
| 631 | * Make sure the module has been initialized, and is not about to be |
| 632 | * removed. Module parameter sysfs files are writable before the |
| 633 | * module_init function is called and after module_exit. |
| 634 | */ |
| 635 | if (THIS_MODULE->state != MODULE_STATE_LIVE) |
| 636 | goto out_nodev; |
| 637 | #endif |
| 638 | |
| 639 | netdev = fcoe_if_to_netdev(buffer); |
| 640 | if (!netdev) |
| 641 | goto out_nodev; |
| 642 | |
| 643 | ft = fcoe_netdev_map_lookup(netdev); |
| 644 | if (!ft) |
| 645 | goto out_putdev; |
| 646 | |
| 647 | rc = ft->disable ? ft->disable(netdev) : -ENODEV; |
| 648 | |
| 649 | out_putdev: |
| 650 | dev_put(netdev); |
| 651 | out_nodev: |
| 652 | mutex_unlock(&ft_mutex); |
| 653 | |
| 654 | if (rc == -ERESTARTSYS) |
| 655 | return restart_syscall(); |
| 656 | else |
| 657 | return rc; |
| 658 | } |
| 659 | |
| 660 | /** |
| 661 | * fcoe_transport_enable() - Enables a FCoE interface |
| 662 | * @buffer: The name of the Ethernet interface to be enabled |
| 663 | * @kp: The associated kernel parameter |
| 664 | * |
| 665 | * Called from sysfs. |
| 666 | * |
| 667 | * Returns: 0 for success |
| 668 | */ |
| 669 | static int fcoe_transport_enable(const char *buffer, struct kernel_param *kp) |
| 670 | { |
| 671 | int rc = -ENODEV; |
| 672 | struct net_device *netdev = NULL; |
| 673 | struct fcoe_transport *ft = NULL; |
| 674 | |
| 675 | if (!mutex_trylock(&ft_mutex)) |
| 676 | return restart_syscall(); |
| 677 | |
| 678 | #ifdef CONFIG_LIBFCOE_MODULE |
| 679 | /* |
| 680 | * Make sure the module has been initialized, and is not about to be |
| 681 | * removed. Module parameter sysfs files are writable before the |
| 682 | * module_init function is called and after module_exit. |
| 683 | */ |
| 684 | if (THIS_MODULE->state != MODULE_STATE_LIVE) |
| 685 | goto out_nodev; |
| 686 | #endif |
| 687 | |
| 688 | netdev = fcoe_if_to_netdev(buffer); |
| 689 | if (!netdev) |
| 690 | goto out_nodev; |
| 691 | |
| 692 | ft = fcoe_netdev_map_lookup(netdev); |
| 693 | if (!ft) |
| 694 | goto out_putdev; |
| 695 | |
| 696 | rc = ft->enable ? ft->enable(netdev) : -ENODEV; |
| 697 | |
| 698 | out_putdev: |
| 699 | dev_put(netdev); |
| 700 | out_nodev: |
| 701 | mutex_unlock(&ft_mutex); |
| 702 | if (rc == -ERESTARTSYS) |
| 703 | return restart_syscall(); |
| 704 | else |
| 705 | return rc; |
| 706 | } |
| 707 | |
| 708 | /** |
| 709 | * libfcoe_init() - Initialization routine for libfcoe.ko |
| 710 | */ |
| 711 | static int __init libfcoe_init(void) |
| 712 | { |
| 713 | fcoe_transport_init(); |
| 714 | |
| 715 | return 0; |
| 716 | } |
| 717 | module_init(libfcoe_init); |
| 718 | |
| 719 | /** |
| 720 | * libfcoe_exit() - Tear down libfcoe.ko |
| 721 | */ |
| 722 | static void __exit libfcoe_exit(void) |
| 723 | { |
| 724 | fcoe_transport_exit(); |
| 725 | } |
| 726 | module_exit(libfcoe_exit); |