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>
43 #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 /* Which is a bug FIXME! */
71 struct driver_context drv_ctx;
72 struct netvsc_driver drv_obj;
79 static int netvsc_ringbuffer_size = NETVSC_DEVICE_RING_BUFFER_SIZE;
81 /* The one and only one */
82 static struct netvsc_driver_context g_netvsc_drv;
90 Name: netvsc_drv_init()
92 Desc: NetVsc driver initialization
95 static int netvsc_drv_init(PFN_DRIVERINITIALIZE pfn_drv_init)
98 struct netvsc_driver *net_drv_obj=&g_netvsc_drv.drv_obj;
99 struct driver_context *drv_ctx=&g_netvsc_drv.drv_ctx;
101 DPRINT_ENTER(NETVSC_DRV);
103 vmbus_get_interface(&net_drv_obj->Base.VmbusChannelInterface);
105 net_drv_obj->RingBufferSize = netvsc_ringbuffer_size;
106 net_drv_obj->OnReceiveCallback = netvsc_recv_callback;
107 net_drv_obj->OnLinkStatusChanged = netvsc_linkstatus_callback;
109 /* Callback to client driver to complete the initialization */
110 pfn_drv_init(&net_drv_obj->Base);
112 drv_ctx->driver.name = net_drv_obj->Base.name;
113 memcpy(&drv_ctx->class_id, &net_drv_obj->Base.deviceType, sizeof(struct hv_guid));
115 drv_ctx->probe = netvsc_probe;
116 drv_ctx->remove = netvsc_remove;
118 /* The driver belongs to vmbus */
119 ret = vmbus_child_driver_register(drv_ctx);
121 DPRINT_EXIT(NETVSC_DRV);
128 Name: netvsc_get_stats()
130 Desc: Get the network stats
133 static struct net_device_stats *netvsc_get_stats(struct net_device *net)
135 struct net_device_context *net_device_ctx = netdev_priv(net);
137 return &net_device_ctx->stats;
142 Name: netvsc_set_multicast_list()
144 Desc: Set the multicast list
148 static void netvsc_set_multicast_list(struct net_device *net)
153 static const struct net_device_ops device_ops = {
154 .ndo_open = netvsc_open,
155 .ndo_stop = netvsc_close,
156 .ndo_start_xmit = netvsc_start_xmit,
157 .ndo_get_stats = netvsc_get_stats,
158 .ndo_set_multicast_list = netvsc_set_multicast_list,
165 Desc: Add the specified new device to this driver
168 static int netvsc_probe(struct device *device)
172 struct driver_context *driver_ctx = driver_to_driver_context(device->driver);
173 struct netvsc_driver_context *net_drv_ctx = (struct netvsc_driver_context*)driver_ctx;
174 struct netvsc_driver *net_drv_obj = &net_drv_ctx->drv_obj;
176 struct device_context *device_ctx = device_to_device_context(device);
177 struct hv_device *device_obj = &device_ctx->device_obj;
179 struct net_device *net = NULL;
180 struct net_device_context *net_device_ctx;
181 struct netvsc_device_info device_info;
183 DPRINT_ENTER(NETVSC_DRV);
185 if (!net_drv_obj->Base.OnDeviceAdd)
190 net = alloc_netdev(sizeof(struct net_device_context), "seth%d", ether_setup);
191 /* net = alloc_etherdev(sizeof(struct net_device_context)); */
197 /* Set initial state */
198 netif_carrier_off(net);
199 netif_stop_queue(net);
201 net_device_ctx = netdev_priv(net);
202 net_device_ctx->device_ctx = device_ctx;
203 dev_set_drvdata(device, net);
205 /* Notify the netvsc driver of the new device */
206 ret = net_drv_obj->Base.OnDeviceAdd(device_obj, (void*)&device_info);
210 dev_set_drvdata(device, NULL);
212 DPRINT_ERR(NETVSC_DRV, "unable to add netvsc device (ret %d)", ret);
216 /* If carrier is still off ie we did not get a link status callback, update it if necessary */
217 /* FIXME: We should use a atomic or test/set instead to avoid getting out of sync with the device's link status */
218 if (!netif_carrier_ok(net))
220 if (!device_info.LinkState)
222 netif_carrier_on(net);
226 memcpy(net->dev_addr, device_info.MacAddr, ETH_ALEN);
228 net->netdev_ops = &device_ops;
230 SET_NETDEV_DEV(net, device);
232 ret = register_netdev(net);
235 /* Remove the device and release the resource */
236 net_drv_obj->Base.OnDeviceRemove(device_obj);
240 DPRINT_EXIT(NETVSC_DRV);
245 static int netvsc_remove(struct device *device)
248 struct driver_context *driver_ctx = driver_to_driver_context(device->driver);
249 struct netvsc_driver_context *net_drv_ctx = (struct netvsc_driver_context*)driver_ctx;
250 struct netvsc_driver *net_drv_obj = &net_drv_ctx->drv_obj;
252 struct device_context *device_ctx = device_to_device_context(device);
253 struct net_device *net = dev_get_drvdata(&device_ctx->device);
254 struct hv_device *device_obj = &device_ctx->device_obj;
256 DPRINT_ENTER(NETVSC_DRV);
260 DPRINT_INFO(NETVSC, "no net device to remove");
261 DPRINT_EXIT(NETVSC_DRV);
265 if (!net_drv_obj->Base.OnDeviceRemove)
267 DPRINT_EXIT(NETVSC_DRV);
271 /* Stop outbound asap */
272 netif_stop_queue(net);
273 /* netif_carrier_off(net); */
275 unregister_netdev(net);
277 /* Call to the vsc driver to let it know that the device is being removed */
278 ret = net_drv_obj->Base.OnDeviceRemove(device_obj);
282 DPRINT_ERR(NETVSC, "unable to remove vsc device (ret %d)", ret);
287 DPRINT_EXIT(NETVSC_DRV);
296 Desc: Open the specified interface device
299 static int netvsc_open(struct net_device *net)
302 struct net_device_context *net_device_ctx = netdev_priv(net);
303 struct driver_context *driver_ctx = driver_to_driver_context(net_device_ctx->device_ctx->device.driver);
304 struct netvsc_driver_context *net_drv_ctx = (struct netvsc_driver_context*)driver_ctx;
305 struct netvsc_driver *net_drv_obj = &net_drv_ctx->drv_obj;
307 struct hv_device *device_obj = &net_device_ctx->device_ctx->device_obj;
309 DPRINT_ENTER(NETVSC_DRV);
311 if (netif_carrier_ok(net))
313 memset(&net_device_ctx->stats, 0 , sizeof(struct net_device_stats));
315 /* Open up the device */
316 ret = net_drv_obj->OnOpen(device_obj);
319 DPRINT_ERR(NETVSC_DRV, "unable to open device (ret %d).", ret);
323 netif_start_queue(net);
327 DPRINT_ERR(NETVSC_DRV, "unable to open device...link is down.");
330 DPRINT_EXIT(NETVSC_DRV);
338 Desc: Close the specified interface device
341 static int netvsc_close(struct net_device *net)
344 struct net_device_context *net_device_ctx = netdev_priv(net);
345 struct driver_context *driver_ctx = driver_to_driver_context(net_device_ctx->device_ctx->device.driver);
346 struct netvsc_driver_context *net_drv_ctx = (struct netvsc_driver_context*)driver_ctx;
347 struct netvsc_driver *net_drv_obj = &net_drv_ctx->drv_obj;
349 struct hv_device *device_obj = &net_device_ctx->device_ctx->device_obj;
351 DPRINT_ENTER(NETVSC_DRV);
353 netif_stop_queue(net);
355 ret = net_drv_obj->OnClose(device_obj);
358 DPRINT_ERR(NETVSC_DRV, "unable to close device (ret %d).", ret);
361 DPRINT_EXIT(NETVSC_DRV);
369 Name: netvsc_xmit_completion()
371 Desc: Send completion processing
374 static void netvsc_xmit_completion(void *context)
376 struct hv_netvsc_packet *packet = (struct hv_netvsc_packet *)context;
377 struct sk_buff *skb = (struct sk_buff *)(unsigned long)packet->Completion.Send.SendCompletionTid;
378 struct net_device* net;
380 DPRINT_ENTER(NETVSC_DRV);
388 dev_kfree_skb_any(skb);
390 if (netif_queue_stopped(net))
392 DPRINT_INFO(NETVSC_DRV, "net device (%p) waking up...", net);
394 netif_wake_queue(net);
398 DPRINT_EXIT(NETVSC_DRV);
403 Name: netvsc_start_xmit()
408 static int netvsc_start_xmit (struct sk_buff *skb, struct net_device *net)
411 struct net_device_context *net_device_ctx = netdev_priv(net);
412 struct driver_context *driver_ctx = driver_to_driver_context(net_device_ctx->device_ctx->device.driver);
413 struct netvsc_driver_context *net_drv_ctx = (struct netvsc_driver_context*)driver_ctx;
414 struct netvsc_driver *net_drv_obj = &net_drv_ctx->drv_obj;
417 struct hv_netvsc_packet *packet;
421 DPRINT_ENTER(NETVSC_DRV);
423 /* Support only 1 chain of frags */
424 ASSERT(skb_shinfo(skb)->frag_list == NULL);
425 ASSERT(skb->dev == net);
427 DPRINT_DBG(NETVSC_DRV, "xmit packet - len %d data_len %d", skb->len, skb->data_len);
429 /* Add 1 for skb->data and any additional ones requested */
430 num_frags = skb_shinfo(skb)->nr_frags + 1 + net_drv_obj->AdditionalRequestPageBufferCount;
432 /* Allocate a netvsc packet based on # of frags. */
433 packet = kzalloc(sizeof(struct hv_netvsc_packet) + (num_frags * sizeof(struct hv_page_buffer)) + net_drv_obj->RequestExtSize, GFP_ATOMIC);
436 DPRINT_ERR(NETVSC_DRV, "unable to allocate hv_netvsc_packet");
440 packet->Extension = (void*)(unsigned long)packet + sizeof(struct hv_netvsc_packet) + (num_frags * sizeof(struct hv_page_buffer)) ;
442 /* Setup the rndis header */
443 packet->PageBufferCount = num_frags;
445 /* TODO: Flush all write buffers/ memory fence ??? */
448 /* Initialize it from the skb */
450 packet->TotalDataBufferLength = skb->len;
452 /* Start filling in the page buffers starting at AdditionalRequestPageBufferCount offset */
453 packet->PageBuffers[net_drv_obj->AdditionalRequestPageBufferCount].Pfn = virt_to_phys(skb->data) >> PAGE_SHIFT;
454 packet->PageBuffers[net_drv_obj->AdditionalRequestPageBufferCount].Offset = (unsigned long)skb->data & (PAGE_SIZE -1);
455 packet->PageBuffers[net_drv_obj->AdditionalRequestPageBufferCount].Length = skb->len - skb->data_len;
457 ASSERT((skb->len - skb->data_len) <= PAGE_SIZE);
459 for (i=net_drv_obj->AdditionalRequestPageBufferCount+1; i<num_frags; i++)
461 packet->PageBuffers[i].Pfn = page_to_pfn(skb_shinfo(skb)->frags[i-(net_drv_obj->AdditionalRequestPageBufferCount+1)].page);
462 packet->PageBuffers[i].Offset = skb_shinfo(skb)->frags[i-(net_drv_obj->AdditionalRequestPageBufferCount+1)].page_offset;
463 packet->PageBuffers[i].Length = skb_shinfo(skb)->frags[i-(net_drv_obj->AdditionalRequestPageBufferCount+1)].size;
466 /* Set the completion routine */
467 packet->Completion.Send.OnSendCompletion = netvsc_xmit_completion;
468 packet->Completion.Send.SendCompletionContext = packet;
469 packet->Completion.Send.SendCompletionTid = (unsigned long)skb;
472 ret = net_drv_obj->OnSend(&net_device_ctx->device_ctx->device_obj, packet);
477 net_device_ctx->stats.tx_bytes += skb->len;
478 net_device_ctx->stats.tx_packets++;
485 DPRINT_ERR(NETVSC_DRV, "unable to send...retrying %d...", retries);
490 /* no more room or we are shutting down */
491 DPRINT_ERR(NETVSC_DRV, "unable to send (%d)...marking net device (%p) busy", ret, net);
492 DPRINT_INFO(NETVSC_DRV, "net device (%p) stopping", net);
494 ret = NETDEV_TX_BUSY;
495 net_device_ctx->stats.tx_dropped++;
497 netif_stop_queue(net);
499 /* Null it since the caller will free it instead of the completion routine */
500 packet->Completion.Send.SendCompletionTid = 0;
502 /* Release the resources since we will not get any send completion */
503 netvsc_xmit_completion((void*)packet);
506 DPRINT_DBG(NETVSC_DRV, "# of xmits %lu total size %lu", net_device_ctx->stats.tx_packets, net_device_ctx->stats.tx_bytes);
508 DPRINT_EXIT(NETVSC_DRV);
515 Name: netvsc_linkstatus_callback()
517 Desc: Link up/down notification
520 static void netvsc_linkstatus_callback(struct hv_device *device_obj, unsigned int status)
522 struct device_context* device_ctx = to_device_context(device_obj);
523 struct net_device* net = dev_get_drvdata(&device_ctx->device);
525 DPRINT_ENTER(NETVSC_DRV);
529 DPRINT_ERR(NETVSC_DRV, "got link status but net device not initialized yet");
535 netif_carrier_on(net);
536 netif_wake_queue(net);
540 netif_carrier_off(net);
541 netif_stop_queue(net);
543 DPRINT_EXIT(NETVSC_DRV);
549 Name: netvsc_recv_callback()
551 Desc: Callback when we receive a packet from the "wire" on the specify device
554 static int netvsc_recv_callback(struct hv_device *device_obj, struct hv_netvsc_packet *packet)
557 struct device_context *device_ctx = to_device_context(device_obj);
558 struct net_device *net = dev_get_drvdata(&device_ctx->device);
559 struct net_device_context *net_device_ctx;
566 DPRINT_ENTER(NETVSC_DRV);
570 DPRINT_ERR(NETVSC_DRV, "got receive callback but net device not initialized yet");
574 net_device_ctx = netdev_priv(net);
576 /* Allocate a skb - TODO preallocate this */
577 /* skb = alloc_skb(packet->TotalDataBufferLength, GFP_ATOMIC); */
578 skb = dev_alloc_skb(packet->TotalDataBufferLength + 2); /* Pad 2-bytes to align IP header to 16 bytes */
583 /* for kmap_atomic */
584 local_irq_save(flags);
586 /* Copy to skb. This copy is needed here since the memory pointed by hv_netvsc_packet */
587 /* cannot be deallocated */
588 for (i=0; i<packet->PageBufferCount; i++)
590 data = kmap_atomic(pfn_to_page(packet->PageBuffers[i].Pfn), KM_IRQ1);
591 data = (void*)(unsigned long)data + packet->PageBuffers[i].Offset;
593 memcpy(skb_put(skb, packet->PageBuffers[i].Length), data, packet->PageBuffers[i].Length);
595 kunmap_atomic((void*)((unsigned long)data - packet->PageBuffers[i].Offset), KM_IRQ1);
598 local_irq_restore(flags);
600 skb->protocol = eth_type_trans(skb, net);
602 skb->ip_summed = CHECKSUM_NONE;
604 /* Pass the skb back up. Network stack will deallocate the skb when it is done */
610 net_device_ctx->stats.rx_dropped++;
613 net_device_ctx->stats.rx_packets++;
614 net_device_ctx->stats.rx_bytes += skb->len;
618 DPRINT_DBG(NETVSC_DRV, "# of recvs %lu total size %lu", net_device_ctx->stats.rx_packets, net_device_ctx->stats.rx_bytes);
620 DPRINT_EXIT(NETVSC_DRV);
625 static int netvsc_drv_exit_cb(struct device *dev, void *data)
627 struct device **curr = (struct device **)data;
629 return 1; /* stop iterating */
634 Name: netvsc_drv_exit()
639 static void netvsc_drv_exit(void)
641 struct netvsc_driver *netvsc_drv_obj=&g_netvsc_drv.drv_obj;
642 struct driver_context *drv_ctx=&g_netvsc_drv.drv_ctx;
643 struct device *current_dev=NULL;
646 DPRINT_ENTER(NETVSC_DRV);
653 ret = driver_for_each_device(&drv_ctx->driver, NULL,
654 (void *) ¤t_dev,
658 DPRINT_WARN(NETVSC_DRV,
659 "driver_for_each_device returned %d", ret);
662 if (current_dev == NULL)
665 /* Initiate removal from the top-down */
666 DPRINT_INFO(NETVSC_DRV, "unregistering device (%p)...", current_dev);
668 device_unregister(current_dev);
671 if (netvsc_drv_obj->Base.OnCleanup)
672 netvsc_drv_obj->Base.OnCleanup(&netvsc_drv_obj->Base);
674 vmbus_child_driver_unregister(drv_ctx);
676 DPRINT_EXIT(NETVSC_DRV);
681 static int __init netvsc_init(void)
685 DPRINT_ENTER(NETVSC_DRV);
686 DPRINT_INFO(NETVSC_DRV, "Netvsc initializing....");
688 ret = netvsc_drv_init(NetVscInitialize);
690 DPRINT_EXIT(NETVSC_DRV);
695 static void __exit netvsc_exit(void)
697 DPRINT_ENTER(NETVSC_DRV);
701 DPRINT_EXIT(NETVSC_DRV);
704 module_param(netvsc_ringbuffer_size, int, S_IRUGO);
706 module_init(netvsc_init);
707 module_exit(netvsc_exit);