Hank Janssen | fceaf24 | 2009-07-13 15:34:54 -0700 | [diff] [blame] | 1 | /* |
| 2 | * |
| 3 | * Copyright (c) 2009, Microsoft Corporation. |
| 4 | * |
| 5 | * This program is free software; you can redistribute it and/or modify it |
| 6 | * under the terms and conditions of the GNU General Public License, |
| 7 | * version 2, as published by the Free Software Foundation. |
| 8 | * |
| 9 | * This program is distributed in the hope it will be useful, but WITHOUT |
| 10 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 11 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for |
| 12 | * more details. |
| 13 | * |
| 14 | * You should have received a copy of the GNU General Public License along with |
| 15 | * this program; if not, write to the Free Software Foundation, Inc., 59 Temple |
| 16 | * Place - Suite 330, Boston, MA 02111-1307 USA. |
| 17 | * |
| 18 | * Authors: |
| 19 | * Hank Janssen <hjanssen@microsoft.com> |
| 20 | * |
| 21 | */ |
| 22 | |
Greg Kroah-Hartman | c86f3e2 | 2009-07-14 10:59:56 -0700 | [diff] [blame] | 23 | #define KERNEL_2_6_27 |
Hank Janssen | fceaf24 | 2009-07-13 15:34:54 -0700 | [diff] [blame] | 24 | |
| 25 | #include <linux/init.h> |
| 26 | #include <linux/module.h> |
| 27 | #include <linux/highmem.h> |
| 28 | #include <linux/device.h> |
| 29 | #if defined(KERNEL_2_6_5) || defined(KERNEL_2_6_9) |
| 30 | #include <asm/io.h> |
| 31 | #else |
| 32 | #include <linux/io.h> |
| 33 | #endif |
| 34 | #include <linux/delay.h> |
| 35 | #include <linux/netdevice.h> |
| 36 | #include <linux/inetdevice.h> |
| 37 | #include <linux/etherdevice.h> |
| 38 | #include <linux/skbuff.h> |
| 39 | #include <linux/in.h> |
| 40 | #include <net/arp.h> |
| 41 | #include <net/route.h> |
| 42 | #include <net/sock.h> |
| 43 | #include <net/pkt_sched.h> |
| 44 | |
Greg Kroah-Hartman | c86f3e2 | 2009-07-14 10:59:56 -0700 | [diff] [blame] | 45 | #include "include/logging.h" |
| 46 | #include "include/vmbus.h" |
Hank Janssen | fceaf24 | 2009-07-13 15:34:54 -0700 | [diff] [blame] | 47 | |
Greg Kroah-Hartman | c86f3e2 | 2009-07-14 10:59:56 -0700 | [diff] [blame] | 48 | #include "include/NetVscApi.h" |
Hank Janssen | fceaf24 | 2009-07-13 15:34:54 -0700 | [diff] [blame] | 49 | |
| 50 | MODULE_LICENSE("GPL"); |
| 51 | |
| 52 | // |
| 53 | // Static decl |
| 54 | // |
| 55 | static int netvsc_probe(struct device *device); |
| 56 | static int netvsc_remove(struct device *device); |
| 57 | static int netvsc_open(struct net_device *net); |
| 58 | static void netvsc_xmit_completion(void *context); |
| 59 | static int netvsc_start_xmit (struct sk_buff *skb, struct net_device *net); |
| 60 | static int netvsc_recv_callback(DEVICE_OBJECT *device_obj, NETVSC_PACKET* Packet); |
| 61 | static int netvsc_close(struct net_device *net); |
| 62 | static struct net_device_stats *netvsc_get_stats(struct net_device *net); |
| 63 | static void netvsc_linkstatus_callback(DEVICE_OBJECT *device_obj, unsigned int status); |
| 64 | |
| 65 | // |
| 66 | // Data types |
| 67 | // |
| 68 | struct net_device_context { |
| 69 | struct device_context *device_ctx; // point back to our device context |
| 70 | struct net_device_stats stats; |
| 71 | }; |
| 72 | |
| 73 | struct netvsc_driver_context { |
| 74 | // !! These must be the first 2 fields !! |
| 75 | struct driver_context drv_ctx; |
| 76 | NETVSC_DRIVER_OBJECT drv_obj; |
| 77 | }; |
| 78 | |
| 79 | // |
| 80 | // Globals |
| 81 | // |
| 82 | |
| 83 | static int netvsc_ringbuffer_size = NETVSC_DEVICE_RING_BUFFER_SIZE; |
| 84 | |
| 85 | // The one and only one |
| 86 | static struct netvsc_driver_context g_netvsc_drv; |
| 87 | |
| 88 | // |
| 89 | // Routines |
| 90 | // |
| 91 | |
| 92 | /*++ |
| 93 | |
| 94 | Name: netvsc_drv_init() |
| 95 | |
| 96 | Desc: NetVsc driver initialization |
| 97 | |
| 98 | --*/ |
| 99 | int netvsc_drv_init(PFN_DRIVERINITIALIZE pfn_drv_init) |
| 100 | { |
| 101 | int ret=0; |
| 102 | NETVSC_DRIVER_OBJECT *net_drv_obj=&g_netvsc_drv.drv_obj; |
| 103 | struct driver_context *drv_ctx=&g_netvsc_drv.drv_ctx; |
| 104 | |
| 105 | DPRINT_ENTER(NETVSC_DRV); |
| 106 | |
| 107 | vmbus_get_interface(&net_drv_obj->Base.VmbusChannelInterface); |
| 108 | |
| 109 | net_drv_obj->RingBufferSize = netvsc_ringbuffer_size; |
| 110 | net_drv_obj->OnReceiveCallback = netvsc_recv_callback; |
| 111 | net_drv_obj->OnLinkStatusChanged = netvsc_linkstatus_callback; |
| 112 | |
| 113 | // Callback to client driver to complete the initialization |
| 114 | pfn_drv_init(&net_drv_obj->Base); |
| 115 | |
| 116 | drv_ctx->driver.name = net_drv_obj->Base.name; |
| 117 | memcpy(&drv_ctx->class_id, &net_drv_obj->Base.deviceType, sizeof(GUID)); |
| 118 | |
| 119 | #if defined(KERNEL_2_6_5) || defined(KERNEL_2_6_9) |
| 120 | drv_ctx->driver.probe = netvsc_probe; |
| 121 | drv_ctx->driver.remove = netvsc_remove; |
| 122 | #else |
| 123 | drv_ctx->probe = netvsc_probe; |
| 124 | drv_ctx->remove = netvsc_remove; |
| 125 | #endif |
| 126 | |
| 127 | // The driver belongs to vmbus |
| 128 | vmbus_child_driver_register(drv_ctx); |
| 129 | |
| 130 | DPRINT_EXIT(NETVSC_DRV); |
| 131 | |
| 132 | return ret; |
| 133 | } |
| 134 | |
| 135 | /*++ |
| 136 | |
| 137 | Name: netvsc_get_stats() |
| 138 | |
| 139 | Desc: Get the network stats |
| 140 | |
| 141 | --*/ |
| 142 | static struct net_device_stats *netvsc_get_stats(struct net_device *net) |
| 143 | { |
| 144 | struct net_device_context *net_device_ctx = netdev_priv(net); |
| 145 | |
| 146 | return &net_device_ctx->stats; |
| 147 | } |
| 148 | |
| 149 | /*++ |
| 150 | |
| 151 | Name: netvsc_set_multicast_list() |
| 152 | |
| 153 | Desc: Set the multicast list |
| 154 | |
| 155 | Remark: No-op here |
| 156 | --*/ |
| 157 | static void netvsc_set_multicast_list(UNUSED_VAR(struct net_device *net)) |
| 158 | { |
| 159 | } |
| 160 | |
| 161 | |
Greg Kroah-Hartman | c86f3e2 | 2009-07-14 10:59:56 -0700 | [diff] [blame] | 162 | static const struct net_device_ops device_ops = { |
| 163 | .ndo_open = netvsc_open, |
| 164 | .ndo_stop = netvsc_close, |
| 165 | .ndo_start_xmit = netvsc_start_xmit, |
| 166 | .ndo_get_stats = netvsc_get_stats, |
| 167 | .ndo_set_multicast_list = netvsc_set_multicast_list, |
| 168 | }; |
| 169 | |
Hank Janssen | fceaf24 | 2009-07-13 15:34:54 -0700 | [diff] [blame] | 170 | /*++ |
| 171 | |
| 172 | Name: netvsc_probe() |
| 173 | |
| 174 | Desc: Add the specified new device to this driver |
| 175 | |
| 176 | --*/ |
| 177 | static int netvsc_probe(struct device *device) |
| 178 | { |
| 179 | int ret=0; |
| 180 | |
| 181 | struct driver_context *driver_ctx = driver_to_driver_context(device->driver); |
| 182 | struct netvsc_driver_context *net_drv_ctx = (struct netvsc_driver_context*)driver_ctx; |
| 183 | NETVSC_DRIVER_OBJECT *net_drv_obj = &net_drv_ctx->drv_obj; |
| 184 | |
| 185 | struct device_context *device_ctx = device_to_device_context(device); |
| 186 | DEVICE_OBJECT *device_obj = &device_ctx->device_obj; |
| 187 | |
| 188 | struct net_device *net = NULL; |
| 189 | struct net_device_context *net_device_ctx; |
| 190 | NETVSC_DEVICE_INFO device_info; |
| 191 | |
| 192 | DPRINT_ENTER(NETVSC_DRV); |
| 193 | |
| 194 | if (!net_drv_obj->Base.OnDeviceAdd) |
| 195 | { |
| 196 | return -1; |
| 197 | } |
| 198 | |
| 199 | net = alloc_netdev(sizeof(struct net_device_context), "seth%d", ether_setup); |
| 200 | //net = alloc_etherdev(sizeof(struct net_device_context)); |
| 201 | if (!net) |
| 202 | { |
| 203 | return -1; |
| 204 | } |
| 205 | |
| 206 | // Set initial state |
| 207 | netif_carrier_off(net); |
| 208 | netif_stop_queue(net); |
| 209 | |
| 210 | net_device_ctx = netdev_priv(net); |
| 211 | net_device_ctx->device_ctx = device_ctx; |
Greg Kroah-Hartman | 621d7fb | 2009-07-24 11:00:39 -0700 | [diff] [blame^] | 212 | dev_set_drvdata(device, net); |
Hank Janssen | fceaf24 | 2009-07-13 15:34:54 -0700 | [diff] [blame] | 213 | |
| 214 | // Notify the netvsc driver of the new device |
| 215 | ret = net_drv_obj->Base.OnDeviceAdd(device_obj, (void*)&device_info); |
| 216 | if (ret != 0) |
| 217 | { |
| 218 | free_netdev(net); |
Greg Kroah-Hartman | 621d7fb | 2009-07-24 11:00:39 -0700 | [diff] [blame^] | 219 | dev_set_drvdata(device, NULL); |
Hank Janssen | fceaf24 | 2009-07-13 15:34:54 -0700 | [diff] [blame] | 220 | |
| 221 | DPRINT_ERR(NETVSC_DRV, "unable to add netvsc device (ret %d)", ret); |
| 222 | return ret; |
| 223 | } |
| 224 | |
| 225 | // If carrier is still off ie we did not get a link status callback, update it if necessary |
| 226 | // FIXME: We should use a atomic or test/set instead to avoid getting out of sync with the device's link status |
| 227 | if (!netif_carrier_ok(net)) |
| 228 | { |
| 229 | if (!device_info.LinkState) |
| 230 | { |
| 231 | netif_carrier_on(net); |
| 232 | } |
| 233 | } |
| 234 | |
| 235 | memcpy(net->dev_addr, device_info.MacAddr, ETH_ALEN); |
| 236 | |
Greg Kroah-Hartman | c86f3e2 | 2009-07-14 10:59:56 -0700 | [diff] [blame] | 237 | net->netdev_ops = &device_ops; |
Hank Janssen | fceaf24 | 2009-07-13 15:34:54 -0700 | [diff] [blame] | 238 | |
| 239 | #if !defined(KERNEL_2_6_27) |
| 240 | SET_MODULE_OWNER(net); |
| 241 | #endif |
| 242 | SET_NETDEV_DEV(net, device); |
| 243 | |
| 244 | ret = register_netdev(net); |
| 245 | if (ret != 0) |
| 246 | { |
| 247 | // Remove the device and release the resource |
| 248 | net_drv_obj->Base.OnDeviceRemove(device_obj); |
| 249 | free_netdev(net); |
| 250 | } |
| 251 | |
| 252 | DPRINT_EXIT(NETVSC_DRV); |
| 253 | |
| 254 | return ret; |
| 255 | } |
| 256 | |
| 257 | static int netvsc_remove(struct device *device) |
| 258 | { |
| 259 | int ret=0; |
| 260 | struct driver_context *driver_ctx = driver_to_driver_context(device->driver); |
| 261 | struct netvsc_driver_context *net_drv_ctx = (struct netvsc_driver_context*)driver_ctx; |
| 262 | NETVSC_DRIVER_OBJECT *net_drv_obj = &net_drv_ctx->drv_obj; |
| 263 | |
| 264 | struct device_context *device_ctx = device_to_device_context(device); |
Greg Kroah-Hartman | 621d7fb | 2009-07-24 11:00:39 -0700 | [diff] [blame^] | 265 | struct net_device *net = dev_get_drvdata(&device_ctx->device); |
Hank Janssen | fceaf24 | 2009-07-13 15:34:54 -0700 | [diff] [blame] | 266 | DEVICE_OBJECT *device_obj = &device_ctx->device_obj; |
| 267 | |
| 268 | DPRINT_ENTER(NETVSC_DRV); |
| 269 | |
| 270 | if (net == NULL) |
| 271 | { |
| 272 | DPRINT_INFO(NETVSC, "no net device to remove"); |
| 273 | DPRINT_EXIT(NETVSC_DRV); |
| 274 | return 0; |
| 275 | } |
| 276 | |
| 277 | if (!net_drv_obj->Base.OnDeviceRemove) |
| 278 | { |
| 279 | DPRINT_EXIT(NETVSC_DRV); |
| 280 | return -1; |
| 281 | } |
| 282 | |
| 283 | // Stop outbound asap |
| 284 | netif_stop_queue(net); |
| 285 | //netif_carrier_off(net); |
| 286 | |
| 287 | unregister_netdev(net); |
| 288 | |
| 289 | // Call to the vsc driver to let it know that the device is being removed |
| 290 | ret = net_drv_obj->Base.OnDeviceRemove(device_obj); |
| 291 | if (ret != 0) |
| 292 | { |
| 293 | // TODO: |
| 294 | DPRINT_ERR(NETVSC, "unable to remove vsc device (ret %d)", ret); |
| 295 | } |
| 296 | |
| 297 | free_netdev(net); |
| 298 | |
| 299 | DPRINT_EXIT(NETVSC_DRV); |
| 300 | |
| 301 | return ret; |
| 302 | } |
| 303 | |
| 304 | /*++ |
| 305 | |
| 306 | Name: netvsc_open() |
| 307 | |
| 308 | Desc: Open the specified interface device |
| 309 | |
| 310 | --*/ |
| 311 | static int netvsc_open(struct net_device *net) |
| 312 | { |
| 313 | int ret=0; |
| 314 | struct net_device_context *net_device_ctx = netdev_priv(net); |
| 315 | struct driver_context *driver_ctx = driver_to_driver_context(net_device_ctx->device_ctx->device.driver); |
| 316 | struct netvsc_driver_context *net_drv_ctx = (struct netvsc_driver_context*)driver_ctx; |
| 317 | NETVSC_DRIVER_OBJECT *net_drv_obj = &net_drv_ctx->drv_obj; |
| 318 | |
| 319 | DEVICE_OBJECT *device_obj = &net_device_ctx->device_ctx->device_obj; |
| 320 | |
| 321 | DPRINT_ENTER(NETVSC_DRV); |
| 322 | |
| 323 | if (netif_carrier_ok(net)) |
| 324 | { |
| 325 | memset(&net_device_ctx->stats, 0 , sizeof(struct net_device_stats)); |
| 326 | |
| 327 | // Open up the device |
| 328 | ret = net_drv_obj->OnOpen(device_obj); |
| 329 | if (ret != 0) |
| 330 | { |
| 331 | DPRINT_ERR(NETVSC_DRV, "unable to open device (ret %d).", ret); |
| 332 | return ret; |
| 333 | } |
| 334 | |
| 335 | netif_start_queue(net); |
| 336 | } |
| 337 | else |
| 338 | { |
| 339 | DPRINT_ERR(NETVSC_DRV, "unable to open device...link is down."); |
| 340 | } |
| 341 | |
| 342 | DPRINT_EXIT(NETVSC_DRV); |
| 343 | return ret; |
| 344 | } |
| 345 | |
| 346 | /*++ |
| 347 | |
| 348 | Name: netvsc_close() |
| 349 | |
| 350 | Desc: Close the specified interface device |
| 351 | |
| 352 | --*/ |
| 353 | static int netvsc_close(struct net_device *net) |
| 354 | { |
| 355 | int ret=0; |
| 356 | struct net_device_context *net_device_ctx = netdev_priv(net); |
| 357 | struct driver_context *driver_ctx = driver_to_driver_context(net_device_ctx->device_ctx->device.driver); |
| 358 | struct netvsc_driver_context *net_drv_ctx = (struct netvsc_driver_context*)driver_ctx; |
| 359 | NETVSC_DRIVER_OBJECT *net_drv_obj = &net_drv_ctx->drv_obj; |
| 360 | |
| 361 | DEVICE_OBJECT *device_obj = &net_device_ctx->device_ctx->device_obj; |
| 362 | |
| 363 | DPRINT_ENTER(NETVSC_DRV); |
| 364 | |
| 365 | netif_stop_queue(net); |
| 366 | |
| 367 | ret = net_drv_obj->OnClose(device_obj); |
| 368 | if (ret != 0) |
| 369 | { |
| 370 | DPRINT_ERR(NETVSC_DRV, "unable to close device (ret %d).", ret); |
| 371 | } |
| 372 | |
| 373 | DPRINT_EXIT(NETVSC_DRV); |
| 374 | |
| 375 | return ret; |
| 376 | } |
| 377 | |
| 378 | |
| 379 | /*++ |
| 380 | |
| 381 | Name: netvsc_xmit_completion() |
| 382 | |
| 383 | Desc: Send completion processing |
| 384 | |
| 385 | --*/ |
| 386 | static void netvsc_xmit_completion(void *context) |
| 387 | { |
| 388 | NETVSC_PACKET *packet = (NETVSC_PACKET *)context; |
| 389 | struct sk_buff *skb = (struct sk_buff *)(ULONG_PTR)packet->Completion.Send.SendCompletionTid; |
| 390 | struct net_device* net; |
| 391 | |
| 392 | DPRINT_ENTER(NETVSC_DRV); |
| 393 | |
| 394 | kfree(packet); |
| 395 | |
| 396 | if (skb) |
| 397 | { |
| 398 | net = skb->dev; |
| 399 | |
| 400 | dev_kfree_skb_any(skb); |
| 401 | |
| 402 | if (netif_queue_stopped(net)) |
| 403 | { |
| 404 | DPRINT_INFO(NETVSC_DRV, "net device (%p) waking up...", net); |
| 405 | |
| 406 | netif_wake_queue(net); |
| 407 | } |
| 408 | } |
| 409 | |
| 410 | DPRINT_EXIT(NETVSC_DRV); |
| 411 | } |
| 412 | |
| 413 | /*++ |
| 414 | |
| 415 | Name: netvsc_start_xmit() |
| 416 | |
| 417 | Desc: Start a send |
| 418 | |
| 419 | --*/ |
| 420 | static int netvsc_start_xmit (struct sk_buff *skb, struct net_device *net) |
| 421 | { |
| 422 | int ret=0; |
| 423 | struct net_device_context *net_device_ctx = netdev_priv(net); |
| 424 | struct driver_context *driver_ctx = driver_to_driver_context(net_device_ctx->device_ctx->device.driver); |
| 425 | struct netvsc_driver_context *net_drv_ctx = (struct netvsc_driver_context*)driver_ctx; |
| 426 | NETVSC_DRIVER_OBJECT *net_drv_obj = &net_drv_ctx->drv_obj; |
| 427 | |
| 428 | int i=0; |
| 429 | NETVSC_PACKET* packet; |
| 430 | int num_frags; |
| 431 | int retries=0; |
| 432 | |
| 433 | DPRINT_ENTER(NETVSC_DRV); |
| 434 | |
| 435 | // Support only 1 chain of frags |
| 436 | ASSERT(skb_shinfo(skb)->frag_list == NULL); |
| 437 | ASSERT(skb->dev == net); |
| 438 | |
| 439 | DPRINT_DBG(NETVSC_DRV, "xmit packet - len %d data_len %d", skb->len, skb->data_len); |
| 440 | |
| 441 | // Add 1 for skb->data and any additional ones requested |
| 442 | num_frags = skb_shinfo(skb)->nr_frags + 1 + net_drv_obj->AdditionalRequestPageBufferCount; |
| 443 | |
| 444 | // Allocate a netvsc packet based on # of frags. |
| 445 | packet = kzalloc(sizeof(NETVSC_PACKET) + (num_frags * sizeof(PAGE_BUFFER)) + net_drv_obj->RequestExtSize, GFP_ATOMIC); |
| 446 | if (!packet) |
| 447 | { |
| 448 | DPRINT_ERR(NETVSC_DRV, "unable to allocate NETVSC_PACKET"); |
| 449 | return -1; |
| 450 | } |
| 451 | |
| 452 | packet->Extension = (void*)(unsigned long)packet + sizeof(NETVSC_PACKET) + (num_frags * sizeof(PAGE_BUFFER)) ; |
| 453 | |
| 454 | // Setup the rndis header |
| 455 | packet->PageBufferCount = num_frags; |
| 456 | |
| 457 | // TODO: Flush all write buffers/ memory fence ??? |
| 458 | //wmb(); |
| 459 | |
| 460 | // Initialize it from the skb |
| 461 | ASSERT(skb->data); |
| 462 | packet->TotalDataBufferLength = skb->len; |
| 463 | |
| 464 | // Start filling in the page buffers starting at AdditionalRequestPageBufferCount offset |
| 465 | packet->PageBuffers[net_drv_obj->AdditionalRequestPageBufferCount].Pfn = virt_to_phys(skb->data) >> PAGE_SHIFT; |
| 466 | packet->PageBuffers[net_drv_obj->AdditionalRequestPageBufferCount].Offset = (unsigned long)skb->data & (PAGE_SIZE -1); |
| 467 | packet->PageBuffers[net_drv_obj->AdditionalRequestPageBufferCount].Length = skb->len - skb->data_len; |
| 468 | |
| 469 | ASSERT((skb->len - skb->data_len) <= PAGE_SIZE); |
| 470 | |
| 471 | for (i=net_drv_obj->AdditionalRequestPageBufferCount+1; i<num_frags; i++) |
| 472 | { |
| 473 | packet->PageBuffers[i].Pfn = page_to_pfn(skb_shinfo(skb)->frags[i-(net_drv_obj->AdditionalRequestPageBufferCount+1)].page); |
| 474 | packet->PageBuffers[i].Offset = skb_shinfo(skb)->frags[i-(net_drv_obj->AdditionalRequestPageBufferCount+1)].page_offset; |
| 475 | packet->PageBuffers[i].Length = skb_shinfo(skb)->frags[i-(net_drv_obj->AdditionalRequestPageBufferCount+1)].size; |
| 476 | } |
| 477 | |
| 478 | // Set the completion routine |
| 479 | packet->Completion.Send.OnSendCompletion = netvsc_xmit_completion; |
| 480 | packet->Completion.Send.SendCompletionContext = packet; |
| 481 | packet->Completion.Send.SendCompletionTid = (ULONG_PTR)skb; |
| 482 | |
| 483 | retry_send: |
| 484 | ret = net_drv_obj->OnSend(&net_device_ctx->device_ctx->device_obj, packet); |
| 485 | |
| 486 | if (ret == 0) |
| 487 | { |
| 488 | #ifdef KERNEL_2_6_5 |
| 489 | #define NETDEV_TX_OK 0 |
| 490 | #define NETDEV_TX_BUSY 0 |
| 491 | #endif |
| 492 | ret = NETDEV_TX_OK; |
| 493 | net_device_ctx->stats.tx_bytes += skb->len; |
| 494 | net_device_ctx->stats.tx_packets++; |
| 495 | } |
| 496 | else |
| 497 | { |
| 498 | retries++; |
| 499 | if (retries < 4) |
| 500 | { |
| 501 | DPRINT_ERR(NETVSC_DRV, "unable to send...retrying %d...", retries); |
| 502 | udelay(100); |
| 503 | goto retry_send; |
| 504 | } |
| 505 | |
| 506 | // no more room or we are shutting down |
| 507 | DPRINT_ERR(NETVSC_DRV, "unable to send (%d)...marking net device (%p) busy", ret, net); |
| 508 | DPRINT_INFO(NETVSC_DRV, "net device (%p) stopping", net); |
| 509 | |
| 510 | ret = NETDEV_TX_BUSY; |
| 511 | net_device_ctx->stats.tx_dropped++; |
| 512 | |
| 513 | netif_stop_queue(net); |
| 514 | |
| 515 | // Null it since the caller will free it instead of the completion routine |
| 516 | packet->Completion.Send.SendCompletionTid = 0; |
| 517 | |
| 518 | // Release the resources since we will not get any send completion |
| 519 | netvsc_xmit_completion((void*)packet); |
| 520 | } |
| 521 | |
| 522 | DPRINT_DBG(NETVSC_DRV, "# of xmits %lu total size %lu", net_device_ctx->stats.tx_packets, net_device_ctx->stats.tx_bytes); |
| 523 | |
| 524 | DPRINT_EXIT(NETVSC_DRV); |
| 525 | return ret; |
| 526 | } |
| 527 | |
| 528 | |
| 529 | /*++ |
| 530 | |
| 531 | Name: netvsc_linkstatus_callback() |
| 532 | |
| 533 | Desc: Link up/down notification |
| 534 | |
| 535 | --*/ |
| 536 | static void netvsc_linkstatus_callback(DEVICE_OBJECT *device_obj, unsigned int status) |
| 537 | { |
| 538 | struct device_context* device_ctx = to_device_context(device_obj); |
Greg Kroah-Hartman | 621d7fb | 2009-07-24 11:00:39 -0700 | [diff] [blame^] | 539 | struct net_device* net = dev_get_drvdata(&device_ctx->device); |
Hank Janssen | fceaf24 | 2009-07-13 15:34:54 -0700 | [diff] [blame] | 540 | |
| 541 | DPRINT_ENTER(NETVSC_DRV); |
| 542 | |
| 543 | if (!net) |
| 544 | { |
| 545 | DPRINT_ERR(NETVSC_DRV, "got link status but net device not initialized yet"); |
| 546 | return; |
| 547 | } |
| 548 | |
| 549 | if (status == 1) |
| 550 | { |
| 551 | netif_carrier_on(net); |
| 552 | netif_wake_queue(net); |
| 553 | } |
| 554 | else |
| 555 | { |
| 556 | netif_carrier_off(net); |
| 557 | netif_stop_queue(net); |
| 558 | } |
| 559 | DPRINT_EXIT(NETVSC_DRV); |
| 560 | } |
| 561 | |
| 562 | |
| 563 | /*++ |
| 564 | |
| 565 | Name: netvsc_recv_callback() |
| 566 | |
| 567 | Desc: Callback when we receive a packet from the "wire" on the specify device |
| 568 | |
| 569 | --*/ |
| 570 | static int netvsc_recv_callback(DEVICE_OBJECT *device_obj, NETVSC_PACKET* packet) |
| 571 | { |
| 572 | int ret=0; |
| 573 | struct device_context *device_ctx = to_device_context(device_obj); |
Greg Kroah-Hartman | 621d7fb | 2009-07-24 11:00:39 -0700 | [diff] [blame^] | 574 | struct net_device *net = dev_get_drvdata(&device_ctx->device); |
Hank Janssen | fceaf24 | 2009-07-13 15:34:54 -0700 | [diff] [blame] | 575 | struct net_device_context *net_device_ctx; |
| 576 | |
| 577 | struct sk_buff *skb; |
| 578 | void *data; |
| 579 | int i=0; |
| 580 | unsigned long flags; |
| 581 | |
| 582 | DPRINT_ENTER(NETVSC_DRV); |
| 583 | |
| 584 | if (!net) |
| 585 | { |
| 586 | DPRINT_ERR(NETVSC_DRV, "got receive callback but net device not initialized yet"); |
| 587 | return 0; |
| 588 | } |
| 589 | |
| 590 | net_device_ctx = netdev_priv(net); |
| 591 | |
| 592 | // Allocate a skb - TODO preallocate this |
| 593 | //skb = alloc_skb(packet->TotalDataBufferLength, GFP_ATOMIC); |
| 594 | skb = dev_alloc_skb(packet->TotalDataBufferLength + 2); // Pad 2-bytes to align IP header to 16 bytes |
| 595 | ASSERT(skb); |
| 596 | skb_reserve(skb, 2); |
| 597 | skb->dev = net; |
| 598 | |
| 599 | // for kmap_atomic |
| 600 | local_irq_save(flags); |
| 601 | |
| 602 | // Copy to skb. This copy is needed here since the memory pointed by NETVSC_PACKET |
| 603 | // cannot be deallocated |
| 604 | for (i=0; i<packet->PageBufferCount; i++) |
| 605 | { |
| 606 | data = kmap_atomic(pfn_to_page(packet->PageBuffers[i].Pfn), KM_IRQ1); |
| 607 | data = (void*)(unsigned long)data + packet->PageBuffers[i].Offset; |
| 608 | |
| 609 | memcpy(skb_put(skb, packet->PageBuffers[i].Length), data, packet->PageBuffers[i].Length); |
| 610 | |
| 611 | kunmap_atomic((void*)((unsigned long)data - packet->PageBuffers[i].Offset), KM_IRQ1); |
| 612 | } |
| 613 | |
| 614 | local_irq_restore(flags); |
| 615 | |
| 616 | skb->protocol = eth_type_trans(skb, net); |
| 617 | |
| 618 | skb->ip_summed = CHECKSUM_NONE; |
| 619 | |
| 620 | // Pass the skb back up. Network stack will deallocate the skb when it is done |
| 621 | ret = netif_rx(skb); |
| 622 | |
| 623 | switch (ret) |
| 624 | { |
| 625 | case NET_RX_DROP: |
| 626 | net_device_ctx->stats.rx_dropped++; |
| 627 | break; |
| 628 | default: |
| 629 | net_device_ctx->stats.rx_packets++; |
| 630 | net_device_ctx->stats.rx_bytes += skb->len; |
| 631 | break; |
| 632 | |
| 633 | } |
| 634 | DPRINT_DBG(NETVSC_DRV, "# of recvs %lu total size %lu", net_device_ctx->stats.rx_packets, net_device_ctx->stats.rx_bytes); |
| 635 | |
| 636 | DPRINT_EXIT(NETVSC_DRV); |
| 637 | |
| 638 | return 0; |
| 639 | } |
| 640 | |
| 641 | static int netvsc_drv_exit_cb(struct device *dev, void *data) |
| 642 | { |
| 643 | struct device **curr = (struct device **)data; |
| 644 | *curr = dev; |
| 645 | return 1; // stop iterating |
| 646 | } |
| 647 | |
| 648 | /*++ |
| 649 | |
| 650 | Name: netvsc_drv_exit() |
| 651 | |
| 652 | Desc: |
| 653 | |
| 654 | --*/ |
| 655 | void netvsc_drv_exit(void) |
| 656 | { |
| 657 | NETVSC_DRIVER_OBJECT *netvsc_drv_obj=&g_netvsc_drv.drv_obj; |
| 658 | struct driver_context *drv_ctx=&g_netvsc_drv.drv_ctx; |
| 659 | |
| 660 | struct device *current_dev=NULL; |
| 661 | #if defined(KERNEL_2_6_5) || defined(KERNEL_2_6_9) |
| 662 | #define driver_for_each_device(drv, start, data, fn) \ |
| 663 | struct list_head *ptr, *n; \ |
| 664 | list_for_each_safe(ptr, n, &((drv)->devices)) {\ |
| 665 | struct device *curr_dev;\ |
| 666 | curr_dev = list_entry(ptr, struct device, driver_list);\ |
| 667 | fn(curr_dev, data);\ |
| 668 | } |
| 669 | #endif |
| 670 | |
| 671 | DPRINT_ENTER(NETVSC_DRV); |
| 672 | |
| 673 | while (1) |
| 674 | { |
| 675 | current_dev = NULL; |
| 676 | |
| 677 | // Get the device |
| 678 | driver_for_each_device(&drv_ctx->driver, NULL, (void*)¤t_dev, netvsc_drv_exit_cb); |
| 679 | |
| 680 | if (current_dev == NULL) |
| 681 | break; |
| 682 | |
| 683 | // Initiate removal from the top-down |
| 684 | DPRINT_INFO(NETVSC_DRV, "unregistering device (%p)...", current_dev); |
| 685 | |
| 686 | device_unregister(current_dev); |
| 687 | } |
| 688 | |
| 689 | if (netvsc_drv_obj->Base.OnCleanup) |
| 690 | netvsc_drv_obj->Base.OnCleanup(&netvsc_drv_obj->Base); |
| 691 | |
| 692 | vmbus_child_driver_unregister(drv_ctx); |
| 693 | |
| 694 | DPRINT_EXIT(NETVSC_DRV); |
| 695 | |
| 696 | return; |
| 697 | } |
| 698 | |
| 699 | static int __init netvsc_init(void) |
| 700 | { |
| 701 | int ret; |
| 702 | |
| 703 | DPRINT_ENTER(NETVSC_DRV); |
| 704 | DPRINT_INFO(NETVSC_DRV, "Netvsc initializing...."); |
| 705 | |
| 706 | ret = netvsc_drv_init(NetVscInitialize); |
| 707 | |
| 708 | DPRINT_EXIT(NETVSC_DRV); |
| 709 | |
| 710 | return ret; |
| 711 | } |
| 712 | |
| 713 | static void __exit netvsc_exit(void) |
| 714 | { |
| 715 | DPRINT_ENTER(NETVSC_DRV); |
| 716 | |
| 717 | netvsc_drv_exit(); |
| 718 | |
| 719 | DPRINT_EXIT(NETVSC_DRV); |
| 720 | } |
| 721 | |
| 722 | module_param(netvsc_ringbuffer_size, int, S_IRUGO); |
| 723 | |
| 724 | module_init(netvsc_init); |
| 725 | module_exit(netvsc_exit); |