3 * Copyright (c) 2009, Microsoft Corporation.
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.
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
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.
19 * Hank Janssen <hjanssen@microsoft.com>
23 #include <linux/init.h>
24 #include <linux/module.h>
25 #include <linux/highmem.h>
26 #include <linux/device.h>
28 #include <linux/delay.h>
29 #include <linux/netdevice.h>
30 #include <linux/inetdevice.h>
31 #include <linux/etherdevice.h>
32 #include <linux/skbuff.h>
35 #include <net/route.h>
37 #include <net/pkt_sched.h>
40 #include "include/logging.h"
41 #include "include/vmbus.h"
43 #include "include/NetVscApi.h"
45 MODULE_LICENSE("GPL");
50 static int netvsc_probe(struct device *device);
51 static int netvsc_remove(struct device *device);
52 static int netvsc_open(struct net_device *net);
53 static void netvsc_xmit_completion(void *context);
54 static int netvsc_start_xmit (struct sk_buff *skb, struct net_device *net);
55 static int netvsc_recv_callback(struct hv_device *device_obj, struct hv_netvsc_packet *Packet);
56 static int netvsc_close(struct net_device *net);
57 static struct net_device_stats *netvsc_get_stats(struct net_device *net);
58 static void netvsc_linkstatus_callback(struct hv_device *device_obj, unsigned int status);
63 struct net_device_context {
64 struct device_context *device_ctx; /* point back to our device context */
65 struct net_device_stats stats;
68 struct netvsc_driver_context {
69 /* !! These must be the first 2 fields !! */
70 struct driver_context drv_ctx;
71 NETVSC_DRIVER_OBJECT drv_obj;
78 static int netvsc_ringbuffer_size = NETVSC_DEVICE_RING_BUFFER_SIZE;
80 /* The one and only one */
81 static struct netvsc_driver_context g_netvsc_drv;
89 Name: netvsc_drv_init()
91 Desc: NetVsc driver initialization
94 static int netvsc_drv_init(PFN_DRIVERINITIALIZE pfn_drv_init)
97 NETVSC_DRIVER_OBJECT *net_drv_obj=&g_netvsc_drv.drv_obj;
98 struct driver_context *drv_ctx=&g_netvsc_drv.drv_ctx;
100 DPRINT_ENTER(NETVSC_DRV);
102 vmbus_get_interface(&net_drv_obj->Base.VmbusChannelInterface);
104 net_drv_obj->RingBufferSize = netvsc_ringbuffer_size;
105 net_drv_obj->OnReceiveCallback = netvsc_recv_callback;
106 net_drv_obj->OnLinkStatusChanged = netvsc_linkstatus_callback;
108 /* Callback to client driver to complete the initialization */
109 pfn_drv_init(&net_drv_obj->Base);
111 drv_ctx->driver.name = net_drv_obj->Base.name;
112 memcpy(&drv_ctx->class_id, &net_drv_obj->Base.deviceType, sizeof(struct hv_guid));
114 drv_ctx->probe = netvsc_probe;
115 drv_ctx->remove = netvsc_remove;
117 /* The driver belongs to vmbus */
118 ret = vmbus_child_driver_register(drv_ctx);
120 DPRINT_EXIT(NETVSC_DRV);
127 Name: netvsc_get_stats()
129 Desc: Get the network stats
132 static struct net_device_stats *netvsc_get_stats(struct net_device *net)
134 struct net_device_context *net_device_ctx = netdev_priv(net);
136 return &net_device_ctx->stats;
141 Name: netvsc_set_multicast_list()
143 Desc: Set the multicast list
147 static void netvsc_set_multicast_list(struct net_device *net)
152 static const struct net_device_ops device_ops = {
153 .ndo_open = netvsc_open,
154 .ndo_stop = netvsc_close,
155 .ndo_start_xmit = netvsc_start_xmit,
156 .ndo_get_stats = netvsc_get_stats,
157 .ndo_set_multicast_list = netvsc_set_multicast_list,
164 Desc: Add the specified new device to this driver
167 static int netvsc_probe(struct device *device)
171 struct driver_context *driver_ctx = driver_to_driver_context(device->driver);
172 struct netvsc_driver_context *net_drv_ctx = (struct netvsc_driver_context*)driver_ctx;
173 NETVSC_DRIVER_OBJECT *net_drv_obj = &net_drv_ctx->drv_obj;
175 struct device_context *device_ctx = device_to_device_context(device);
176 struct hv_device *device_obj = &device_ctx->device_obj;
178 struct net_device *net = NULL;
179 struct net_device_context *net_device_ctx;
180 NETVSC_DEVICE_INFO device_info;
182 DPRINT_ENTER(NETVSC_DRV);
184 if (!net_drv_obj->Base.OnDeviceAdd)
189 net = alloc_netdev(sizeof(struct net_device_context), "seth%d", ether_setup);
190 /* net = alloc_etherdev(sizeof(struct net_device_context)); */
196 /* Set initial state */
197 netif_carrier_off(net);
198 netif_stop_queue(net);
200 net_device_ctx = netdev_priv(net);
201 net_device_ctx->device_ctx = device_ctx;
202 dev_set_drvdata(device, net);
204 /* Notify the netvsc driver of the new device */
205 ret = net_drv_obj->Base.OnDeviceAdd(device_obj, (void*)&device_info);
209 dev_set_drvdata(device, NULL);
211 DPRINT_ERR(NETVSC_DRV, "unable to add netvsc device (ret %d)", ret);
215 /* If carrier is still off ie we did not get a link status callback, update it if necessary */
216 /* FIXME: We should use a atomic or test/set instead to avoid getting out of sync with the device's link status */
217 if (!netif_carrier_ok(net))
219 if (!device_info.LinkState)
221 netif_carrier_on(net);
225 memcpy(net->dev_addr, device_info.MacAddr, ETH_ALEN);
227 net->netdev_ops = &device_ops;
229 SET_NETDEV_DEV(net, device);
231 ret = register_netdev(net);
234 /* Remove the device and release the resource */
235 net_drv_obj->Base.OnDeviceRemove(device_obj);
239 DPRINT_EXIT(NETVSC_DRV);
244 static int netvsc_remove(struct device *device)
247 struct driver_context *driver_ctx = driver_to_driver_context(device->driver);
248 struct netvsc_driver_context *net_drv_ctx = (struct netvsc_driver_context*)driver_ctx;
249 NETVSC_DRIVER_OBJECT *net_drv_obj = &net_drv_ctx->drv_obj;
251 struct device_context *device_ctx = device_to_device_context(device);
252 struct net_device *net = dev_get_drvdata(&device_ctx->device);
253 struct hv_device *device_obj = &device_ctx->device_obj;
255 DPRINT_ENTER(NETVSC_DRV);
259 DPRINT_INFO(NETVSC, "no net device to remove");
260 DPRINT_EXIT(NETVSC_DRV);
264 if (!net_drv_obj->Base.OnDeviceRemove)
266 DPRINT_EXIT(NETVSC_DRV);
270 /* Stop outbound asap */
271 netif_stop_queue(net);
272 /* netif_carrier_off(net); */
274 unregister_netdev(net);
276 /* Call to the vsc driver to let it know that the device is being removed */
277 ret = net_drv_obj->Base.OnDeviceRemove(device_obj);
281 DPRINT_ERR(NETVSC, "unable to remove vsc device (ret %d)", ret);
286 DPRINT_EXIT(NETVSC_DRV);
295 Desc: Open the specified interface device
298 static int netvsc_open(struct net_device *net)
301 struct net_device_context *net_device_ctx = netdev_priv(net);
302 struct driver_context *driver_ctx = driver_to_driver_context(net_device_ctx->device_ctx->device.driver);
303 struct netvsc_driver_context *net_drv_ctx = (struct netvsc_driver_context*)driver_ctx;
304 NETVSC_DRIVER_OBJECT *net_drv_obj = &net_drv_ctx->drv_obj;
306 struct hv_device *device_obj = &net_device_ctx->device_ctx->device_obj;
308 DPRINT_ENTER(NETVSC_DRV);
310 if (netif_carrier_ok(net))
312 memset(&net_device_ctx->stats, 0 , sizeof(struct net_device_stats));
314 /* Open up the device */
315 ret = net_drv_obj->OnOpen(device_obj);
318 DPRINT_ERR(NETVSC_DRV, "unable to open device (ret %d).", ret);
322 netif_start_queue(net);
326 DPRINT_ERR(NETVSC_DRV, "unable to open device...link is down.");
329 DPRINT_EXIT(NETVSC_DRV);
337 Desc: Close the specified interface device
340 static int netvsc_close(struct net_device *net)
343 struct net_device_context *net_device_ctx = netdev_priv(net);
344 struct driver_context *driver_ctx = driver_to_driver_context(net_device_ctx->device_ctx->device.driver);
345 struct netvsc_driver_context *net_drv_ctx = (struct netvsc_driver_context*)driver_ctx;
346 NETVSC_DRIVER_OBJECT *net_drv_obj = &net_drv_ctx->drv_obj;
348 struct hv_device *device_obj = &net_device_ctx->device_ctx->device_obj;
350 DPRINT_ENTER(NETVSC_DRV);
352 netif_stop_queue(net);
354 ret = net_drv_obj->OnClose(device_obj);
357 DPRINT_ERR(NETVSC_DRV, "unable to close device (ret %d).", ret);
360 DPRINT_EXIT(NETVSC_DRV);
368 Name: netvsc_xmit_completion()
370 Desc: Send completion processing
373 static void netvsc_xmit_completion(void *context)
375 struct hv_netvsc_packet *packet = (struct hv_netvsc_packet *)context;
376 struct sk_buff *skb = (struct sk_buff *)(unsigned long)packet->Completion.Send.SendCompletionTid;
377 struct net_device* net;
379 DPRINT_ENTER(NETVSC_DRV);
387 dev_kfree_skb_any(skb);
389 if (netif_queue_stopped(net))
391 DPRINT_INFO(NETVSC_DRV, "net device (%p) waking up...", net);
393 netif_wake_queue(net);
397 DPRINT_EXIT(NETVSC_DRV);
402 Name: netvsc_start_xmit()
407 static int netvsc_start_xmit (struct sk_buff *skb, struct net_device *net)
410 struct net_device_context *net_device_ctx = netdev_priv(net);
411 struct driver_context *driver_ctx = driver_to_driver_context(net_device_ctx->device_ctx->device.driver);
412 struct netvsc_driver_context *net_drv_ctx = (struct netvsc_driver_context*)driver_ctx;
413 NETVSC_DRIVER_OBJECT *net_drv_obj = &net_drv_ctx->drv_obj;
416 struct hv_netvsc_packet *packet;
420 DPRINT_ENTER(NETVSC_DRV);
422 /* Support only 1 chain of frags */
423 ASSERT(skb_shinfo(skb)->frag_list == NULL);
424 ASSERT(skb->dev == net);
426 DPRINT_DBG(NETVSC_DRV, "xmit packet - len %d data_len %d", skb->len, skb->data_len);
428 /* Add 1 for skb->data and any additional ones requested */
429 num_frags = skb_shinfo(skb)->nr_frags + 1 + net_drv_obj->AdditionalRequestPageBufferCount;
431 /* Allocate a netvsc packet based on # of frags. */
432 packet = kzalloc(sizeof(struct hv_netvsc_packet) + (num_frags * sizeof(PAGE_BUFFER)) + net_drv_obj->RequestExtSize, GFP_ATOMIC);
435 DPRINT_ERR(NETVSC_DRV, "unable to allocate hv_netvsc_packet");
439 packet->Extension = (void*)(unsigned long)packet + sizeof(struct hv_netvsc_packet) + (num_frags * sizeof(PAGE_BUFFER)) ;
441 /* Setup the rndis header */
442 packet->PageBufferCount = num_frags;
444 /* TODO: Flush all write buffers/ memory fence ??? */
447 /* Initialize it from the skb */
449 packet->TotalDataBufferLength = skb->len;
451 /* Start filling in the page buffers starting at AdditionalRequestPageBufferCount offset */
452 packet->PageBuffers[net_drv_obj->AdditionalRequestPageBufferCount].Pfn = virt_to_phys(skb->data) >> PAGE_SHIFT;
453 packet->PageBuffers[net_drv_obj->AdditionalRequestPageBufferCount].Offset = (unsigned long)skb->data & (PAGE_SIZE -1);
454 packet->PageBuffers[net_drv_obj->AdditionalRequestPageBufferCount].Length = skb->len - skb->data_len;
456 ASSERT((skb->len - skb->data_len) <= PAGE_SIZE);
458 for (i=net_drv_obj->AdditionalRequestPageBufferCount+1; i<num_frags; i++)
460 packet->PageBuffers[i].Pfn = page_to_pfn(skb_shinfo(skb)->frags[i-(net_drv_obj->AdditionalRequestPageBufferCount+1)].page);
461 packet->PageBuffers[i].Offset = skb_shinfo(skb)->frags[i-(net_drv_obj->AdditionalRequestPageBufferCount+1)].page_offset;
462 packet->PageBuffers[i].Length = skb_shinfo(skb)->frags[i-(net_drv_obj->AdditionalRequestPageBufferCount+1)].size;
465 /* Set the completion routine */
466 packet->Completion.Send.OnSendCompletion = netvsc_xmit_completion;
467 packet->Completion.Send.SendCompletionContext = packet;
468 packet->Completion.Send.SendCompletionTid = (unsigned long)skb;
471 ret = net_drv_obj->OnSend(&net_device_ctx->device_ctx->device_obj, packet);
476 net_device_ctx->stats.tx_bytes += skb->len;
477 net_device_ctx->stats.tx_packets++;
484 DPRINT_ERR(NETVSC_DRV, "unable to send...retrying %d...", retries);
489 /* no more room or we are shutting down */
490 DPRINT_ERR(NETVSC_DRV, "unable to send (%d)...marking net device (%p) busy", ret, net);
491 DPRINT_INFO(NETVSC_DRV, "net device (%p) stopping", net);
493 ret = NETDEV_TX_BUSY;
494 net_device_ctx->stats.tx_dropped++;
496 netif_stop_queue(net);
498 /* Null it since the caller will free it instead of the completion routine */
499 packet->Completion.Send.SendCompletionTid = 0;
501 /* Release the resources since we will not get any send completion */
502 netvsc_xmit_completion((void*)packet);
505 DPRINT_DBG(NETVSC_DRV, "# of xmits %lu total size %lu", net_device_ctx->stats.tx_packets, net_device_ctx->stats.tx_bytes);
507 DPRINT_EXIT(NETVSC_DRV);
514 Name: netvsc_linkstatus_callback()
516 Desc: Link up/down notification
519 static void netvsc_linkstatus_callback(struct hv_device *device_obj, unsigned int status)
521 struct device_context* device_ctx = to_device_context(device_obj);
522 struct net_device* net = dev_get_drvdata(&device_ctx->device);
524 DPRINT_ENTER(NETVSC_DRV);
528 DPRINT_ERR(NETVSC_DRV, "got link status but net device not initialized yet");
534 netif_carrier_on(net);
535 netif_wake_queue(net);
539 netif_carrier_off(net);
540 netif_stop_queue(net);
542 DPRINT_EXIT(NETVSC_DRV);
548 Name: netvsc_recv_callback()
550 Desc: Callback when we receive a packet from the "wire" on the specify device
553 static int netvsc_recv_callback(struct hv_device *device_obj, struct hv_netvsc_packet *packet)
556 struct device_context *device_ctx = to_device_context(device_obj);
557 struct net_device *net = dev_get_drvdata(&device_ctx->device);
558 struct net_device_context *net_device_ctx;
565 DPRINT_ENTER(NETVSC_DRV);
569 DPRINT_ERR(NETVSC_DRV, "got receive callback but net device not initialized yet");
573 net_device_ctx = netdev_priv(net);
575 /* Allocate a skb - TODO preallocate this */
576 /* skb = alloc_skb(packet->TotalDataBufferLength, GFP_ATOMIC); */
577 skb = dev_alloc_skb(packet->TotalDataBufferLength + 2); /* Pad 2-bytes to align IP header to 16 bytes */
582 /* for kmap_atomic */
583 local_irq_save(flags);
585 /* Copy to skb. This copy is needed here since the memory pointed by hv_netvsc_packet */
586 /* cannot be deallocated */
587 for (i=0; i<packet->PageBufferCount; i++)
589 data = kmap_atomic(pfn_to_page(packet->PageBuffers[i].Pfn), KM_IRQ1);
590 data = (void*)(unsigned long)data + packet->PageBuffers[i].Offset;
592 memcpy(skb_put(skb, packet->PageBuffers[i].Length), data, packet->PageBuffers[i].Length);
594 kunmap_atomic((void*)((unsigned long)data - packet->PageBuffers[i].Offset), KM_IRQ1);
597 local_irq_restore(flags);
599 skb->protocol = eth_type_trans(skb, net);
601 skb->ip_summed = CHECKSUM_NONE;
603 /* Pass the skb back up. Network stack will deallocate the skb when it is done */
609 net_device_ctx->stats.rx_dropped++;
612 net_device_ctx->stats.rx_packets++;
613 net_device_ctx->stats.rx_bytes += skb->len;
617 DPRINT_DBG(NETVSC_DRV, "# of recvs %lu total size %lu", net_device_ctx->stats.rx_packets, net_device_ctx->stats.rx_bytes);
619 DPRINT_EXIT(NETVSC_DRV);
624 static int netvsc_drv_exit_cb(struct device *dev, void *data)
626 struct device **curr = (struct device **)data;
628 return 1; /* stop iterating */
633 Name: netvsc_drv_exit()
638 static void netvsc_drv_exit(void)
640 NETVSC_DRIVER_OBJECT *netvsc_drv_obj=&g_netvsc_drv.drv_obj;
641 struct driver_context *drv_ctx=&g_netvsc_drv.drv_ctx;
642 struct device *current_dev=NULL;
645 DPRINT_ENTER(NETVSC_DRV);
652 ret = driver_for_each_device(&drv_ctx->driver, NULL,
653 (void *) ¤t_dev,
657 DPRINT_WARN(NETVSC_DRV,
658 "driver_for_each_device returned %d", ret);
661 if (current_dev == NULL)
664 /* Initiate removal from the top-down */
665 DPRINT_INFO(NETVSC_DRV, "unregistering device (%p)...", current_dev);
667 device_unregister(current_dev);
670 if (netvsc_drv_obj->Base.OnCleanup)
671 netvsc_drv_obj->Base.OnCleanup(&netvsc_drv_obj->Base);
673 vmbus_child_driver_unregister(drv_ctx);
675 DPRINT_EXIT(NETVSC_DRV);
680 static int __init netvsc_init(void)
684 DPRINT_ENTER(NETVSC_DRV);
685 DPRINT_INFO(NETVSC_DRV, "Netvsc initializing....");
687 ret = netvsc_drv_init(NetVscInitialize);
689 DPRINT_EXIT(NETVSC_DRV);
694 static void __exit netvsc_exit(void)
696 DPRINT_ENTER(NETVSC_DRV);
700 DPRINT_EXIT(NETVSC_DRV);
703 module_param(netvsc_ringbuffer_size, int, S_IRUGO);
705 module_init(netvsc_init);
706 module_exit(netvsc_exit);