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