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