2 * Copyright (c) 2009, Microsoft Corporation.
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.
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
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., 59 Temple
15 * Place - Suite 330, Boston, MA 02111-1307 USA.
18 * Haiyang Zhang <haiyangz@microsoft.com>
19 * Hank Janssen <hjanssen@microsoft.com>
21 #include <linux/init.h>
22 #include <linux/module.h>
23 #include <linux/highmem.h>
24 #include <linux/device.h>
26 #include <linux/delay.h>
27 #include <linux/netdevice.h>
28 #include <linux/inetdevice.h>
29 #include <linux/etherdevice.h>
30 #include <linux/skbuff.h>
32 #include <linux/slab.h>
34 #include <net/route.h>
36 #include <net/pkt_sched.h>
39 #include "VersionInfo.h"
41 #include "NetVscApi.h"
43 struct net_device_context {
44 /* point back to our device context */
45 struct vm_device *device_ctx;
46 struct net_device_stats stats;
49 struct netvsc_driver_context {
50 /* !! These must be the first 2 fields !! */
51 /* Which is a bug FIXME! */
52 struct driver_context drv_ctx;
53 struct netvsc_driver drv_obj;
56 static int netvsc_ringbuffer_size = NETVSC_DEVICE_RING_BUFFER_SIZE;
58 /* The one and only one */
59 static struct netvsc_driver_context g_netvsc_drv;
61 static struct net_device_stats *netvsc_get_stats(struct net_device *net)
63 struct net_device_context *net_device_ctx = netdev_priv(net);
65 return &net_device_ctx->stats;
68 static void netvsc_set_multicast_list(struct net_device *net)
72 static int netvsc_open(struct net_device *net)
74 struct net_device_context *net_device_ctx = netdev_priv(net);
75 struct hv_device *device_obj = &net_device_ctx->device_ctx->device_obj;
78 DPRINT_ENTER(NETVSC_DRV);
80 if (netif_carrier_ok(net)) {
81 memset(&net_device_ctx->stats, 0,
82 sizeof(struct net_device_stats));
84 /* Open up the device */
85 ret = RndisFilterOnOpen(device_obj);
87 DPRINT_ERR(NETVSC_DRV,
88 "unable to open device (ret %d).", ret);
92 netif_start_queue(net);
94 DPRINT_ERR(NETVSC_DRV, "unable to open device...link is down.");
97 DPRINT_EXIT(NETVSC_DRV);
101 static int netvsc_close(struct net_device *net)
103 struct net_device_context *net_device_ctx = netdev_priv(net);
104 struct hv_device *device_obj = &net_device_ctx->device_ctx->device_obj;
107 DPRINT_ENTER(NETVSC_DRV);
109 netif_stop_queue(net);
111 ret = RndisFilterOnClose(device_obj);
113 DPRINT_ERR(NETVSC_DRV, "unable to close device (ret %d).", ret);
115 DPRINT_EXIT(NETVSC_DRV);
120 static void netvsc_xmit_completion(void *context)
122 struct hv_netvsc_packet *packet = (struct hv_netvsc_packet *)context;
123 struct sk_buff *skb = (struct sk_buff *)
124 (unsigned long)packet->Completion.Send.SendCompletionTid;
125 struct net_device *net;
127 DPRINT_ENTER(NETVSC_DRV);
133 dev_kfree_skb_any(skb);
135 if (netif_queue_stopped(net)) {
136 DPRINT_INFO(NETVSC_DRV, "net device (%p) waking up...",
139 netif_wake_queue(net);
143 DPRINT_EXIT(NETVSC_DRV);
146 static int netvsc_start_xmit(struct sk_buff *skb, struct net_device *net)
148 struct net_device_context *net_device_ctx = netdev_priv(net);
149 struct driver_context *driver_ctx =
150 driver_to_driver_context(net_device_ctx->device_ctx->device.driver);
151 struct netvsc_driver_context *net_drv_ctx =
152 (struct netvsc_driver_context *)driver_ctx;
153 struct netvsc_driver *net_drv_obj = &net_drv_ctx->drv_obj;
154 struct hv_netvsc_packet *packet;
160 DPRINT_ENTER(NETVSC_DRV);
162 /* Support only 1 chain of frags */
163 ASSERT(skb_shinfo(skb)->frag_list == NULL);
164 ASSERT(skb->dev == net);
166 DPRINT_DBG(NETVSC_DRV, "xmit packet - len %d data_len %d",
167 skb->len, skb->data_len);
169 /* Add 1 for skb->data and any additional ones requested */
170 num_frags = skb_shinfo(skb)->nr_frags + 1 +
171 net_drv_obj->AdditionalRequestPageBufferCount;
173 /* Allocate a netvsc packet based on # of frags. */
174 packet = kzalloc(sizeof(struct hv_netvsc_packet) +
175 (num_frags * sizeof(struct hv_page_buffer)) +
176 net_drv_obj->RequestExtSize, GFP_ATOMIC);
178 DPRINT_ERR(NETVSC_DRV, "unable to allocate hv_netvsc_packet");
182 packet->Extension = (void *)(unsigned long)packet +
183 sizeof(struct hv_netvsc_packet) +
184 (num_frags * sizeof(struct hv_page_buffer));
186 /* Setup the rndis header */
187 packet->PageBufferCount = num_frags;
189 /* TODO: Flush all write buffers/ memory fence ??? */
192 /* Initialize it from the skb */
194 packet->TotalDataBufferLength = skb->len;
197 * Start filling in the page buffers starting at
198 * AdditionalRequestPageBufferCount offset
200 packet->PageBuffers[net_drv_obj->AdditionalRequestPageBufferCount].Pfn = virt_to_phys(skb->data) >> PAGE_SHIFT;
201 packet->PageBuffers[net_drv_obj->AdditionalRequestPageBufferCount].Offset = (unsigned long)skb->data & (PAGE_SIZE - 1);
202 packet->PageBuffers[net_drv_obj->AdditionalRequestPageBufferCount].Length = skb->len - skb->data_len;
204 ASSERT((skb->len - skb->data_len) <= PAGE_SIZE);
206 for (i = net_drv_obj->AdditionalRequestPageBufferCount + 1;
207 i < num_frags; i++) {
208 packet->PageBuffers[i].Pfn =
209 page_to_pfn(skb_shinfo(skb)->frags[i-(net_drv_obj->AdditionalRequestPageBufferCount+1)].page);
210 packet->PageBuffers[i].Offset =
211 skb_shinfo(skb)->frags[i-(net_drv_obj->AdditionalRequestPageBufferCount+1)].page_offset;
212 packet->PageBuffers[i].Length =
213 skb_shinfo(skb)->frags[i-(net_drv_obj->AdditionalRequestPageBufferCount+1)].size;
216 /* Set the completion routine */
217 packet->Completion.Send.OnSendCompletion = netvsc_xmit_completion;
218 packet->Completion.Send.SendCompletionContext = packet;
219 packet->Completion.Send.SendCompletionTid = (unsigned long)skb;
222 ret = net_drv_obj->OnSend(&net_device_ctx->device_ctx->device_obj,
227 net_device_ctx->stats.tx_bytes += skb->len;
228 net_device_ctx->stats.tx_packets++;
232 DPRINT_ERR(NETVSC_DRV, "unable to send..."
233 "retrying %d...", retries);
238 /* no more room or we are shutting down */
239 DPRINT_ERR(NETVSC_DRV, "unable to send (%d)..."
240 "marking net device (%p) busy", ret, net);
241 DPRINT_INFO(NETVSC_DRV, "net device (%p) stopping", net);
243 ret = NETDEV_TX_BUSY;
244 net_device_ctx->stats.tx_dropped++;
246 netif_stop_queue(net);
249 * Null it since the caller will free it instead of the
252 packet->Completion.Send.SendCompletionTid = 0;
255 * Release the resources since we will not get any send
258 netvsc_xmit_completion((void *)packet);
261 DPRINT_DBG(NETVSC_DRV, "# of xmits %lu total size %lu",
262 net_device_ctx->stats.tx_packets,
263 net_device_ctx->stats.tx_bytes);
265 DPRINT_EXIT(NETVSC_DRV);
270 * netvsc_linkstatus_callback - Link up/down notification
272 static void netvsc_linkstatus_callback(struct hv_device *device_obj,
275 struct vm_device *device_ctx = to_vm_device(device_obj);
276 struct net_device *net = dev_get_drvdata(&device_ctx->device);
278 DPRINT_ENTER(NETVSC_DRV);
281 DPRINT_ERR(NETVSC_DRV, "got link status but net device "
282 "not initialized yet");
287 netif_carrier_on(net);
288 netif_wake_queue(net);
290 netif_carrier_off(net);
291 netif_stop_queue(net);
293 DPRINT_EXIT(NETVSC_DRV);
297 * netvsc_recv_callback - Callback when we receive a packet from the "wire" on the specified device.
299 static int netvsc_recv_callback(struct hv_device *device_obj,
300 struct hv_netvsc_packet *packet)
302 struct vm_device *device_ctx = to_vm_device(device_obj);
303 struct net_device *net = dev_get_drvdata(&device_ctx->device);
304 struct net_device_context *net_device_ctx;
311 DPRINT_ENTER(NETVSC_DRV);
314 DPRINT_ERR(NETVSC_DRV, "got receive callback but net device "
315 "not initialized yet");
319 net_device_ctx = netdev_priv(net);
321 /* Allocate a skb - TODO preallocate this */
322 /* Pad 2-bytes to align IP header to 16 bytes */
323 skb = dev_alloc_skb(packet->TotalDataBufferLength + 2);
328 /* for kmap_atomic */
329 local_irq_save(flags);
332 * Copy to skb. This copy is needed here since the memory pointed by
333 * hv_netvsc_packet cannot be deallocated
335 for (i = 0; i < packet->PageBufferCount; i++) {
336 data = kmap_atomic(pfn_to_page(packet->PageBuffers[i].Pfn),
338 data = (void *)(unsigned long)data +
339 packet->PageBuffers[i].Offset;
341 memcpy(skb_put(skb, packet->PageBuffers[i].Length), data,
342 packet->PageBuffers[i].Length);
344 kunmap_atomic((void *)((unsigned long)data -
345 packet->PageBuffers[i].Offset), KM_IRQ1);
348 local_irq_restore(flags);
350 skb->protocol = eth_type_trans(skb, net);
352 skb->ip_summed = CHECKSUM_NONE;
355 * Pass the skb back up. Network stack will deallocate the skb when it
362 net_device_ctx->stats.rx_dropped++;
365 net_device_ctx->stats.rx_packets++;
366 net_device_ctx->stats.rx_bytes += skb->len;
370 DPRINT_DBG(NETVSC_DRV, "# of recvs %lu total size %lu",
371 net_device_ctx->stats.rx_packets,
372 net_device_ctx->stats.rx_bytes);
374 DPRINT_EXIT(NETVSC_DRV);
379 static const struct net_device_ops device_ops = {
380 .ndo_open = netvsc_open,
381 .ndo_stop = netvsc_close,
382 .ndo_start_xmit = netvsc_start_xmit,
383 .ndo_get_stats = netvsc_get_stats,
384 .ndo_set_multicast_list = netvsc_set_multicast_list,
387 static int netvsc_probe(struct device *device)
389 struct driver_context *driver_ctx =
390 driver_to_driver_context(device->driver);
391 struct netvsc_driver_context *net_drv_ctx =
392 (struct netvsc_driver_context *)driver_ctx;
393 struct netvsc_driver *net_drv_obj = &net_drv_ctx->drv_obj;
394 struct vm_device *device_ctx = device_to_vm_device(device);
395 struct hv_device *device_obj = &device_ctx->device_obj;
396 struct net_device *net = NULL;
397 struct net_device_context *net_device_ctx;
398 struct netvsc_device_info device_info;
401 DPRINT_ENTER(NETVSC_DRV);
403 if (!net_drv_obj->Base.OnDeviceAdd)
406 net = alloc_etherdev(sizeof(struct net_device_context));
410 /* Set initial state */
411 netif_carrier_off(net);
412 netif_stop_queue(net);
414 net_device_ctx = netdev_priv(net);
415 net_device_ctx->device_ctx = device_ctx;
416 dev_set_drvdata(device, net);
418 /* Notify the netvsc driver of the new device */
419 ret = net_drv_obj->Base.OnDeviceAdd(device_obj, &device_info);
422 dev_set_drvdata(device, NULL);
424 DPRINT_ERR(NETVSC_DRV, "unable to add netvsc device (ret %d)",
430 * If carrier is still off ie we did not get a link status callback,
431 * update it if necessary
434 * FIXME: We should use a atomic or test/set instead to avoid getting
435 * out of sync with the device's link status
437 if (!netif_carrier_ok(net))
438 if (!device_info.LinkState)
439 netif_carrier_on(net);
441 memcpy(net->dev_addr, device_info.MacAddr, ETH_ALEN);
443 net->netdev_ops = &device_ops;
445 SET_NETDEV_DEV(net, device);
447 ret = register_netdev(net);
449 /* Remove the device and release the resource */
450 net_drv_obj->Base.OnDeviceRemove(device_obj);
454 DPRINT_EXIT(NETVSC_DRV);
458 static int netvsc_remove(struct device *device)
460 struct driver_context *driver_ctx =
461 driver_to_driver_context(device->driver);
462 struct netvsc_driver_context *net_drv_ctx =
463 (struct netvsc_driver_context *)driver_ctx;
464 struct netvsc_driver *net_drv_obj = &net_drv_ctx->drv_obj;
465 struct vm_device *device_ctx = device_to_vm_device(device);
466 struct net_device *net = dev_get_drvdata(&device_ctx->device);
467 struct hv_device *device_obj = &device_ctx->device_obj;
470 DPRINT_ENTER(NETVSC_DRV);
473 DPRINT_INFO(NETVSC, "no net device to remove");
474 DPRINT_EXIT(NETVSC_DRV);
478 if (!net_drv_obj->Base.OnDeviceRemove) {
479 DPRINT_EXIT(NETVSC_DRV);
483 /* Stop outbound asap */
484 netif_stop_queue(net);
485 /* netif_carrier_off(net); */
487 unregister_netdev(net);
490 * Call to the vsc driver to let it know that the device is being
493 ret = net_drv_obj->Base.OnDeviceRemove(device_obj);
496 DPRINT_ERR(NETVSC, "unable to remove vsc device (ret %d)", ret);
500 DPRINT_EXIT(NETVSC_DRV);
504 static int netvsc_drv_exit_cb(struct device *dev, void *data)
506 struct device **curr = (struct device **)data;
513 static void netvsc_drv_exit(void)
515 struct netvsc_driver *netvsc_drv_obj = &g_netvsc_drv.drv_obj;
516 struct driver_context *drv_ctx = &g_netvsc_drv.drv_ctx;
517 struct device *current_dev;
520 DPRINT_ENTER(NETVSC_DRV);
526 ret = driver_for_each_device(&drv_ctx->driver, NULL,
527 ¤t_dev, netvsc_drv_exit_cb);
529 DPRINT_WARN(NETVSC_DRV,
530 "driver_for_each_device returned %d", ret);
532 if (current_dev == NULL)
535 /* Initiate removal from the top-down */
536 DPRINT_INFO(NETVSC_DRV, "unregistering device (%p)...",
539 device_unregister(current_dev);
542 if (netvsc_drv_obj->Base.OnCleanup)
543 netvsc_drv_obj->Base.OnCleanup(&netvsc_drv_obj->Base);
545 vmbus_child_driver_unregister(drv_ctx);
547 DPRINT_EXIT(NETVSC_DRV);
552 static int netvsc_drv_init(int (*drv_init)(struct hv_driver *drv))
554 struct netvsc_driver *net_drv_obj = &g_netvsc_drv.drv_obj;
555 struct driver_context *drv_ctx = &g_netvsc_drv.drv_ctx;
558 DPRINT_ENTER(NETVSC_DRV);
560 vmbus_get_interface(&net_drv_obj->Base.VmbusChannelInterface);
562 net_drv_obj->RingBufferSize = netvsc_ringbuffer_size;
563 net_drv_obj->OnReceiveCallback = netvsc_recv_callback;
564 net_drv_obj->OnLinkStatusChanged = netvsc_linkstatus_callback;
566 /* Callback to client driver to complete the initialization */
567 drv_init(&net_drv_obj->Base);
569 drv_ctx->driver.name = net_drv_obj->Base.name;
570 memcpy(&drv_ctx->class_id, &net_drv_obj->Base.deviceType,
571 sizeof(struct hv_guid));
573 drv_ctx->probe = netvsc_probe;
574 drv_ctx->remove = netvsc_remove;
576 /* The driver belongs to vmbus */
577 ret = vmbus_child_driver_register(drv_ctx);
579 DPRINT_EXIT(NETVSC_DRV);
584 static int __init netvsc_init(void)
588 DPRINT_ENTER(NETVSC_DRV);
589 DPRINT_INFO(NETVSC_DRV, "Netvsc initializing....");
591 ret = netvsc_drv_init(NetVscInitialize);
593 DPRINT_EXIT(NETVSC_DRV);
598 static void __exit netvsc_exit(void)
600 DPRINT_ENTER(NETVSC_DRV);
602 DPRINT_EXIT(NETVSC_DRV);
605 MODULE_LICENSE("GPL");
606 MODULE_VERSION(HV_DRV_VERSION);
607 module_param(netvsc_ringbuffer_size, int, S_IRUGO);
609 module_init(netvsc_init);
610 module_exit(netvsc_exit);