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>
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)
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>
41 #include <net/route.h>
43 #include <net/pkt_sched.h>
45 #include "include/logging.h"
46 #include "include/vmbus.h"
48 #include "include/NetVscApi.h"
50 MODULE_LICENSE("GPL");
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);
68 struct net_device_context {
69 struct device_context *device_ctx; // point back to our device context
70 struct net_device_stats stats;
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;
83 static int netvsc_ringbuffer_size = NETVSC_DEVICE_RING_BUFFER_SIZE;
85 // The one and only one
86 static struct netvsc_driver_context g_netvsc_drv;
94 Name: netvsc_drv_init()
96 Desc: NetVsc driver initialization
99 int netvsc_drv_init(PFN_DRIVERINITIALIZE pfn_drv_init)
102 NETVSC_DRIVER_OBJECT *net_drv_obj=&g_netvsc_drv.drv_obj;
103 struct driver_context *drv_ctx=&g_netvsc_drv.drv_ctx;
105 DPRINT_ENTER(NETVSC_DRV);
107 vmbus_get_interface(&net_drv_obj->Base.VmbusChannelInterface);
109 net_drv_obj->RingBufferSize = netvsc_ringbuffer_size;
110 net_drv_obj->OnReceiveCallback = netvsc_recv_callback;
111 net_drv_obj->OnLinkStatusChanged = netvsc_linkstatus_callback;
113 // Callback to client driver to complete the initialization
114 pfn_drv_init(&net_drv_obj->Base);
116 drv_ctx->driver.name = net_drv_obj->Base.name;
117 memcpy(&drv_ctx->class_id, &net_drv_obj->Base.deviceType, sizeof(GUID));
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;
123 drv_ctx->probe = netvsc_probe;
124 drv_ctx->remove = netvsc_remove;
127 // The driver belongs to vmbus
128 vmbus_child_driver_register(drv_ctx);
130 DPRINT_EXIT(NETVSC_DRV);
137 Name: netvsc_get_stats()
139 Desc: Get the network stats
142 static struct net_device_stats *netvsc_get_stats(struct net_device *net)
144 struct net_device_context *net_device_ctx = netdev_priv(net);
146 return &net_device_ctx->stats;
151 Name: netvsc_set_multicast_list()
153 Desc: Set the multicast list
157 static void netvsc_set_multicast_list(UNUSED_VAR(struct net_device *net))
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,
174 Desc: Add the specified new device to this driver
177 static int netvsc_probe(struct device *device)
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;
185 struct device_context *device_ctx = device_to_device_context(device);
186 DEVICE_OBJECT *device_obj = &device_ctx->device_obj;
188 struct net_device *net = NULL;
189 struct net_device_context *net_device_ctx;
190 NETVSC_DEVICE_INFO device_info;
192 DPRINT_ENTER(NETVSC_DRV);
194 if (!net_drv_obj->Base.OnDeviceAdd)
199 net = alloc_netdev(sizeof(struct net_device_context), "seth%d", ether_setup);
200 //net = alloc_etherdev(sizeof(struct net_device_context));
207 netif_carrier_off(net);
208 netif_stop_queue(net);
210 net_device_ctx = netdev_priv(net);
211 net_device_ctx->device_ctx = device_ctx;
212 dev_set_drvdata(device, net);
214 // Notify the netvsc driver of the new device
215 ret = net_drv_obj->Base.OnDeviceAdd(device_obj, (void*)&device_info);
219 dev_set_drvdata(device, NULL);
221 DPRINT_ERR(NETVSC_DRV, "unable to add netvsc device (ret %d)", ret);
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))
229 if (!device_info.LinkState)
231 netif_carrier_on(net);
235 memcpy(net->dev_addr, device_info.MacAddr, ETH_ALEN);
237 net->netdev_ops = &device_ops;
239 #if !defined(KERNEL_2_6_27)
240 SET_MODULE_OWNER(net);
242 SET_NETDEV_DEV(net, device);
244 ret = register_netdev(net);
247 // Remove the device and release the resource
248 net_drv_obj->Base.OnDeviceRemove(device_obj);
252 DPRINT_EXIT(NETVSC_DRV);
257 static int netvsc_remove(struct device *device)
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;
264 struct device_context *device_ctx = device_to_device_context(device);
265 struct net_device *net = dev_get_drvdata(&device_ctx->device);
266 DEVICE_OBJECT *device_obj = &device_ctx->device_obj;
268 DPRINT_ENTER(NETVSC_DRV);
272 DPRINT_INFO(NETVSC, "no net device to remove");
273 DPRINT_EXIT(NETVSC_DRV);
277 if (!net_drv_obj->Base.OnDeviceRemove)
279 DPRINT_EXIT(NETVSC_DRV);
283 // Stop outbound asap
284 netif_stop_queue(net);
285 //netif_carrier_off(net);
287 unregister_netdev(net);
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);
294 DPRINT_ERR(NETVSC, "unable to remove vsc device (ret %d)", ret);
299 DPRINT_EXIT(NETVSC_DRV);
308 Desc: Open the specified interface device
311 static int netvsc_open(struct net_device *net)
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;
319 DEVICE_OBJECT *device_obj = &net_device_ctx->device_ctx->device_obj;
321 DPRINT_ENTER(NETVSC_DRV);
323 if (netif_carrier_ok(net))
325 memset(&net_device_ctx->stats, 0 , sizeof(struct net_device_stats));
327 // Open up the device
328 ret = net_drv_obj->OnOpen(device_obj);
331 DPRINT_ERR(NETVSC_DRV, "unable to open device (ret %d).", ret);
335 netif_start_queue(net);
339 DPRINT_ERR(NETVSC_DRV, "unable to open device...link is down.");
342 DPRINT_EXIT(NETVSC_DRV);
350 Desc: Close the specified interface device
353 static int netvsc_close(struct net_device *net)
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;
361 DEVICE_OBJECT *device_obj = &net_device_ctx->device_ctx->device_obj;
363 DPRINT_ENTER(NETVSC_DRV);
365 netif_stop_queue(net);
367 ret = net_drv_obj->OnClose(device_obj);
370 DPRINT_ERR(NETVSC_DRV, "unable to close device (ret %d).", ret);
373 DPRINT_EXIT(NETVSC_DRV);
381 Name: netvsc_xmit_completion()
383 Desc: Send completion processing
386 static void netvsc_xmit_completion(void *context)
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;
392 DPRINT_ENTER(NETVSC_DRV);
400 dev_kfree_skb_any(skb);
402 if (netif_queue_stopped(net))
404 DPRINT_INFO(NETVSC_DRV, "net device (%p) waking up...", net);
406 netif_wake_queue(net);
410 DPRINT_EXIT(NETVSC_DRV);
415 Name: netvsc_start_xmit()
420 static int netvsc_start_xmit (struct sk_buff *skb, struct net_device *net)
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;
429 NETVSC_PACKET* packet;
433 DPRINT_ENTER(NETVSC_DRV);
435 // Support only 1 chain of frags
436 ASSERT(skb_shinfo(skb)->frag_list == NULL);
437 ASSERT(skb->dev == net);
439 DPRINT_DBG(NETVSC_DRV, "xmit packet - len %d data_len %d", skb->len, skb->data_len);
441 // Add 1 for skb->data and any additional ones requested
442 num_frags = skb_shinfo(skb)->nr_frags + 1 + net_drv_obj->AdditionalRequestPageBufferCount;
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);
448 DPRINT_ERR(NETVSC_DRV, "unable to allocate NETVSC_PACKET");
452 packet->Extension = (void*)(unsigned long)packet + sizeof(NETVSC_PACKET) + (num_frags * sizeof(PAGE_BUFFER)) ;
454 // Setup the rndis header
455 packet->PageBufferCount = num_frags;
457 // TODO: Flush all write buffers/ memory fence ???
460 // Initialize it from the skb
462 packet->TotalDataBufferLength = skb->len;
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;
469 ASSERT((skb->len - skb->data_len) <= PAGE_SIZE);
471 for (i=net_drv_obj->AdditionalRequestPageBufferCount+1; i<num_frags; i++)
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;
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;
484 ret = net_drv_obj->OnSend(&net_device_ctx->device_ctx->device_obj, packet);
489 #define NETDEV_TX_OK 0
490 #define NETDEV_TX_BUSY 0
493 net_device_ctx->stats.tx_bytes += skb->len;
494 net_device_ctx->stats.tx_packets++;
501 DPRINT_ERR(NETVSC_DRV, "unable to send...retrying %d...", retries);
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);
510 ret = NETDEV_TX_BUSY;
511 net_device_ctx->stats.tx_dropped++;
513 netif_stop_queue(net);
515 // Null it since the caller will free it instead of the completion routine
516 packet->Completion.Send.SendCompletionTid = 0;
518 // Release the resources since we will not get any send completion
519 netvsc_xmit_completion((void*)packet);
522 DPRINT_DBG(NETVSC_DRV, "# of xmits %lu total size %lu", net_device_ctx->stats.tx_packets, net_device_ctx->stats.tx_bytes);
524 DPRINT_EXIT(NETVSC_DRV);
531 Name: netvsc_linkstatus_callback()
533 Desc: Link up/down notification
536 static void netvsc_linkstatus_callback(DEVICE_OBJECT *device_obj, unsigned int status)
538 struct device_context* device_ctx = to_device_context(device_obj);
539 struct net_device* net = dev_get_drvdata(&device_ctx->device);
541 DPRINT_ENTER(NETVSC_DRV);
545 DPRINT_ERR(NETVSC_DRV, "got link status but net device not initialized yet");
551 netif_carrier_on(net);
552 netif_wake_queue(net);
556 netif_carrier_off(net);
557 netif_stop_queue(net);
559 DPRINT_EXIT(NETVSC_DRV);
565 Name: netvsc_recv_callback()
567 Desc: Callback when we receive a packet from the "wire" on the specify device
570 static int netvsc_recv_callback(DEVICE_OBJECT *device_obj, NETVSC_PACKET* packet)
573 struct device_context *device_ctx = to_device_context(device_obj);
574 struct net_device *net = dev_get_drvdata(&device_ctx->device);
575 struct net_device_context *net_device_ctx;
582 DPRINT_ENTER(NETVSC_DRV);
586 DPRINT_ERR(NETVSC_DRV, "got receive callback but net device not initialized yet");
590 net_device_ctx = netdev_priv(net);
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
600 local_irq_save(flags);
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++)
606 data = kmap_atomic(pfn_to_page(packet->PageBuffers[i].Pfn), KM_IRQ1);
607 data = (void*)(unsigned long)data + packet->PageBuffers[i].Offset;
609 memcpy(skb_put(skb, packet->PageBuffers[i].Length), data, packet->PageBuffers[i].Length);
611 kunmap_atomic((void*)((unsigned long)data - packet->PageBuffers[i].Offset), KM_IRQ1);
614 local_irq_restore(flags);
616 skb->protocol = eth_type_trans(skb, net);
618 skb->ip_summed = CHECKSUM_NONE;
620 // Pass the skb back up. Network stack will deallocate the skb when it is done
626 net_device_ctx->stats.rx_dropped++;
629 net_device_ctx->stats.rx_packets++;
630 net_device_ctx->stats.rx_bytes += skb->len;
634 DPRINT_DBG(NETVSC_DRV, "# of recvs %lu total size %lu", net_device_ctx->stats.rx_packets, net_device_ctx->stats.rx_bytes);
636 DPRINT_EXIT(NETVSC_DRV);
641 static int netvsc_drv_exit_cb(struct device *dev, void *data)
643 struct device **curr = (struct device **)data;
645 return 1; // stop iterating
650 Name: netvsc_drv_exit()
655 void netvsc_drv_exit(void)
657 NETVSC_DRIVER_OBJECT *netvsc_drv_obj=&g_netvsc_drv.drv_obj;
658 struct driver_context *drv_ctx=&g_netvsc_drv.drv_ctx;
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);\
671 DPRINT_ENTER(NETVSC_DRV);
678 driver_for_each_device(&drv_ctx->driver, NULL, (void*)¤t_dev, netvsc_drv_exit_cb);
680 if (current_dev == NULL)
683 // Initiate removal from the top-down
684 DPRINT_INFO(NETVSC_DRV, "unregistering device (%p)...", current_dev);
686 device_unregister(current_dev);
689 if (netvsc_drv_obj->Base.OnCleanup)
690 netvsc_drv_obj->Base.OnCleanup(&netvsc_drv_obj->Base);
692 vmbus_child_driver_unregister(drv_ctx);
694 DPRINT_EXIT(NETVSC_DRV);
699 static int __init netvsc_init(void)
703 DPRINT_ENTER(NETVSC_DRV);
704 DPRINT_INFO(NETVSC_DRV, "Netvsc initializing....");
706 ret = netvsc_drv_init(NetVscInitialize);
708 DPRINT_EXIT(NETVSC_DRV);
713 static void __exit netvsc_exit(void)
715 DPRINT_ENTER(NETVSC_DRV);
719 DPRINT_EXIT(NETVSC_DRV);
722 module_param(netvsc_ringbuffer_size, int, S_IRUGO);
724 module_init(netvsc_init);
725 module_exit(netvsc_exit);