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>
33 #include <linux/dmi.h>
34 #include <linux/pci.h>
36 #include <net/route.h>
38 #include <net/pkt_sched.h>
41 #include "version_info.h"
43 #include "netvsc_api.h"
45 struct net_device_context {
46 /* point back to our device context */
47 struct vm_device *device_ctx;
51 struct netvsc_driver_context {
52 /* !! These must be the first 2 fields !! */
53 /* Which is a bug FIXME! */
54 struct driver_context drv_ctx;
55 struct netvsc_driver drv_obj;
58 #define PACKET_PAGES_LOWATER 8
59 /* Need this many pages to handle worst case fragmented packet */
60 #define PACKET_PAGES_HIWATER (MAX_SKB_FRAGS + 2)
62 static int ring_size = roundup_pow_of_two(2*MAX_SKB_FRAGS+1);
63 module_param(ring_size, int, S_IRUGO);
64 MODULE_PARM_DESC(ring_size, "Ring buffer size (# of pages)");
66 /* The one and only one */
67 static struct netvsc_driver_context g_netvsc_drv;
69 static void netvsc_set_multicast_list(struct net_device *net)
73 static int netvsc_open(struct net_device *net)
75 struct net_device_context *net_device_ctx = netdev_priv(net);
76 struct hv_device *device_obj = &net_device_ctx->device_ctx->device_obj;
79 DPRINT_ENTER(NETVSC_DRV);
81 if (netif_carrier_ok(net)) {
82 /* Open up the device */
83 ret = RndisFilterOnOpen(device_obj);
85 DPRINT_ERR(NETVSC_DRV,
86 "unable to open device (ret %d).", ret);
90 netif_start_queue(net);
92 DPRINT_ERR(NETVSC_DRV, "unable to open device...link is down.");
95 DPRINT_EXIT(NETVSC_DRV);
99 static int netvsc_close(struct net_device *net)
101 struct net_device_context *net_device_ctx = netdev_priv(net);
102 struct hv_device *device_obj = &net_device_ctx->device_ctx->device_obj;
105 DPRINT_ENTER(NETVSC_DRV);
107 netif_stop_queue(net);
109 ret = RndisFilterOnClose(device_obj);
111 DPRINT_ERR(NETVSC_DRV, "unable to close device (ret %d).", ret);
113 DPRINT_EXIT(NETVSC_DRV);
118 static void netvsc_xmit_completion(void *context)
120 struct hv_netvsc_packet *packet = (struct hv_netvsc_packet *)context;
121 struct sk_buff *skb = (struct sk_buff *)
122 (unsigned long)packet->Completion.Send.SendCompletionTid;
124 DPRINT_ENTER(NETVSC_DRV);
129 struct net_device *net = skb->dev;
130 struct net_device_context *net_device_ctx = netdev_priv(net);
131 unsigned int num_pages = skb_shinfo(skb)->nr_frags + 2;
133 dev_kfree_skb_any(skb);
135 if ((net_device_ctx->avail += num_pages) >= PACKET_PAGES_HIWATER)
136 netif_wake_queue(net);
139 DPRINT_EXIT(NETVSC_DRV);
142 static int netvsc_start_xmit(struct sk_buff *skb, struct net_device *net)
144 struct net_device_context *net_device_ctx = netdev_priv(net);
145 struct driver_context *driver_ctx =
146 driver_to_driver_context(net_device_ctx->device_ctx->device.driver);
147 struct netvsc_driver_context *net_drv_ctx =
148 (struct netvsc_driver_context *)driver_ctx;
149 struct netvsc_driver *net_drv_obj = &net_drv_ctx->drv_obj;
150 struct hv_netvsc_packet *packet;
152 unsigned int i, num_pages;
154 DPRINT_ENTER(NETVSC_DRV);
156 DPRINT_DBG(NETVSC_DRV, "xmit packet - len %d data_len %d",
157 skb->len, skb->data_len);
159 /* Add 1 for skb->data and additional one for RNDIS */
160 num_pages = skb_shinfo(skb)->nr_frags + 1 + 1;
161 if (num_pages > net_device_ctx->avail)
162 return NETDEV_TX_BUSY;
164 /* Allocate a netvsc packet based on # of frags. */
165 packet = kzalloc(sizeof(struct hv_netvsc_packet) +
166 (num_pages * sizeof(struct hv_page_buffer)) +
167 net_drv_obj->RequestExtSize, GFP_ATOMIC);
169 /* out of memory, silently drop packet */
170 DPRINT_ERR(NETVSC_DRV, "unable to allocate hv_netvsc_packet");
173 net->stats.tx_dropped++;
177 packet->Extension = (void *)(unsigned long)packet +
178 sizeof(struct hv_netvsc_packet) +
179 (num_pages * sizeof(struct hv_page_buffer));
181 /* Setup the rndis header */
182 packet->PageBufferCount = num_pages;
184 /* TODO: Flush all write buffers/ memory fence ??? */
187 /* Initialize it from the skb */
188 packet->TotalDataBufferLength = skb->len;
190 /* Start filling in the page buffers starting after RNDIS buffer. */
191 packet->PageBuffers[1].Pfn = virt_to_phys(skb->data) >> PAGE_SHIFT;
192 packet->PageBuffers[1].Offset
193 = (unsigned long)skb->data & (PAGE_SIZE - 1);
194 packet->PageBuffers[1].Length = skb_headlen(skb);
196 /* Additional fragments are after SKB data */
197 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
198 skb_frag_t *f = &skb_shinfo(skb)->frags[i];
200 packet->PageBuffers[i+2].Pfn = page_to_pfn(f->page);
201 packet->PageBuffers[i+2].Offset = f->page_offset;
202 packet->PageBuffers[i+2].Length = f->size;
205 /* Set the completion routine */
206 packet->Completion.Send.OnSendCompletion = netvsc_xmit_completion;
207 packet->Completion.Send.SendCompletionContext = packet;
208 packet->Completion.Send.SendCompletionTid = (unsigned long)skb;
210 ret = net_drv_obj->OnSend(&net_device_ctx->device_ctx->device_obj,
213 net->stats.tx_bytes += skb->len;
214 net->stats.tx_packets++;
216 DPRINT_DBG(NETVSC_DRV, "# of xmits %lu total size %lu",
217 net->stats.tx_packets,
218 net->stats.tx_bytes);
220 if ((net_device_ctx->avail -= num_pages) < PACKET_PAGES_LOWATER)
221 netif_stop_queue(net);
223 /* we are shutting down or bus overloaded, just drop packet */
224 net->stats.tx_dropped++;
225 netvsc_xmit_completion(packet);
228 DPRINT_EXIT(NETVSC_DRV);
233 * netvsc_linkstatus_callback - Link up/down notification
235 static void netvsc_linkstatus_callback(struct hv_device *device_obj,
238 struct vm_device *device_ctx = to_vm_device(device_obj);
239 struct net_device *net = dev_get_drvdata(&device_ctx->device);
241 DPRINT_ENTER(NETVSC_DRV);
244 DPRINT_ERR(NETVSC_DRV, "got link status but net device "
245 "not initialized yet");
250 netif_carrier_on(net);
251 netif_wake_queue(net);
253 netif_carrier_off(net);
254 netif_stop_queue(net);
256 DPRINT_EXIT(NETVSC_DRV);
260 * netvsc_recv_callback - Callback when we receive a packet from the
261 * "wire" on the specified device.
263 static int netvsc_recv_callback(struct hv_device *device_obj,
264 struct hv_netvsc_packet *packet)
266 struct vm_device *device_ctx = to_vm_device(device_obj);
267 struct net_device *net = dev_get_drvdata(&device_ctx->device);
273 DPRINT_ENTER(NETVSC_DRV);
276 DPRINT_ERR(NETVSC_DRV, "got receive callback but net device "
277 "not initialized yet");
281 /* Allocate a skb - TODO direct I/O to pages? */
282 skb = netdev_alloc_skb_ip_align(net, packet->TotalDataBufferLength);
283 if (unlikely(!skb)) {
284 ++net->stats.rx_dropped;
288 /* for kmap_atomic */
289 local_irq_save(flags);
292 * Copy to skb. This copy is needed here since the memory pointed by
293 * hv_netvsc_packet cannot be deallocated
295 for (i = 0; i < packet->PageBufferCount; i++) {
296 data = kmap_atomic(pfn_to_page(packet->PageBuffers[i].Pfn),
298 data = (void *)(unsigned long)data +
299 packet->PageBuffers[i].Offset;
301 memcpy(skb_put(skb, packet->PageBuffers[i].Length), data,
302 packet->PageBuffers[i].Length);
304 kunmap_atomic((void *)((unsigned long)data -
305 packet->PageBuffers[i].Offset), KM_IRQ1);
308 local_irq_restore(flags);
310 skb->protocol = eth_type_trans(skb, net);
311 skb->ip_summed = CHECKSUM_NONE;
313 net->stats.rx_packets++;
314 net->stats.rx_bytes += skb->len;
317 * Pass the skb back up. Network stack will deallocate the skb when it
323 DPRINT_DBG(NETVSC_DRV, "# of recvs %lu total size %lu",
324 net->stats.rx_packets, net->stats.rx_bytes);
326 DPRINT_EXIT(NETVSC_DRV);
331 static void netvsc_get_drvinfo(struct net_device *net,
332 struct ethtool_drvinfo *info)
334 strcpy(info->driver, "hv_netvsc");
335 strcpy(info->version, HV_DRV_VERSION);
336 strcpy(info->fw_version, "N/A");
339 static const struct ethtool_ops ethtool_ops = {
340 .get_drvinfo = netvsc_get_drvinfo,
341 .get_sg = ethtool_op_get_sg,
342 .set_sg = ethtool_op_set_sg,
343 .get_link = ethtool_op_get_link,
346 static const struct net_device_ops device_ops = {
347 .ndo_open = netvsc_open,
348 .ndo_stop = netvsc_close,
349 .ndo_start_xmit = netvsc_start_xmit,
350 .ndo_set_multicast_list = netvsc_set_multicast_list,
353 static int netvsc_probe(struct device *device)
355 struct driver_context *driver_ctx =
356 driver_to_driver_context(device->driver);
357 struct netvsc_driver_context *net_drv_ctx =
358 (struct netvsc_driver_context *)driver_ctx;
359 struct netvsc_driver *net_drv_obj = &net_drv_ctx->drv_obj;
360 struct vm_device *device_ctx = device_to_vm_device(device);
361 struct hv_device *device_obj = &device_ctx->device_obj;
362 struct net_device *net = NULL;
363 struct net_device_context *net_device_ctx;
364 struct netvsc_device_info device_info;
367 DPRINT_ENTER(NETVSC_DRV);
369 if (!net_drv_obj->Base.OnDeviceAdd)
372 net = alloc_etherdev(sizeof(struct net_device_context));
376 /* Set initial state */
377 netif_carrier_off(net);
378 netif_stop_queue(net);
380 net_device_ctx = netdev_priv(net);
381 net_device_ctx->device_ctx = device_ctx;
382 net_device_ctx->avail = ring_size;
383 dev_set_drvdata(device, net);
385 /* Notify the netvsc driver of the new device */
386 ret = net_drv_obj->Base.OnDeviceAdd(device_obj, &device_info);
389 dev_set_drvdata(device, NULL);
391 DPRINT_ERR(NETVSC_DRV, "unable to add netvsc device (ret %d)",
397 * If carrier is still off ie we did not get a link status callback,
398 * update it if necessary
401 * FIXME: We should use a atomic or test/set instead to avoid getting
402 * out of sync with the device's link status
404 if (!netif_carrier_ok(net))
405 if (!device_info.LinkState)
406 netif_carrier_on(net);
408 memcpy(net->dev_addr, device_info.MacAddr, ETH_ALEN);
410 net->netdev_ops = &device_ops;
412 /* TODO: Add GSO and Checksum offload */
413 net->features = NETIF_F_SG;
415 SET_ETHTOOL_OPS(net, ðtool_ops);
416 SET_NETDEV_DEV(net, device);
418 ret = register_netdev(net);
420 /* Remove the device and release the resource */
421 net_drv_obj->Base.OnDeviceRemove(device_obj);
425 DPRINT_EXIT(NETVSC_DRV);
429 static int netvsc_remove(struct device *device)
431 struct driver_context *driver_ctx =
432 driver_to_driver_context(device->driver);
433 struct netvsc_driver_context *net_drv_ctx =
434 (struct netvsc_driver_context *)driver_ctx;
435 struct netvsc_driver *net_drv_obj = &net_drv_ctx->drv_obj;
436 struct vm_device *device_ctx = device_to_vm_device(device);
437 struct net_device *net = dev_get_drvdata(&device_ctx->device);
438 struct hv_device *device_obj = &device_ctx->device_obj;
441 DPRINT_ENTER(NETVSC_DRV);
444 DPRINT_INFO(NETVSC, "no net device to remove");
445 DPRINT_EXIT(NETVSC_DRV);
449 if (!net_drv_obj->Base.OnDeviceRemove) {
450 DPRINT_EXIT(NETVSC_DRV);
454 /* Stop outbound asap */
455 netif_stop_queue(net);
456 /* netif_carrier_off(net); */
458 unregister_netdev(net);
461 * Call to the vsc driver to let it know that the device is being
464 ret = net_drv_obj->Base.OnDeviceRemove(device_obj);
467 DPRINT_ERR(NETVSC, "unable to remove vsc device (ret %d)", ret);
471 DPRINT_EXIT(NETVSC_DRV);
475 static int netvsc_drv_exit_cb(struct device *dev, void *data)
477 struct device **curr = (struct device **)data;
484 static void netvsc_drv_exit(void)
486 struct netvsc_driver *netvsc_drv_obj = &g_netvsc_drv.drv_obj;
487 struct driver_context *drv_ctx = &g_netvsc_drv.drv_ctx;
488 struct device *current_dev;
491 DPRINT_ENTER(NETVSC_DRV);
497 ret = driver_for_each_device(&drv_ctx->driver, NULL,
498 ¤t_dev, netvsc_drv_exit_cb);
500 DPRINT_WARN(NETVSC_DRV,
501 "driver_for_each_device returned %d", ret);
503 if (current_dev == NULL)
506 /* Initiate removal from the top-down */
507 DPRINT_INFO(NETVSC_DRV, "unregistering device (%p)...",
510 device_unregister(current_dev);
513 if (netvsc_drv_obj->Base.OnCleanup)
514 netvsc_drv_obj->Base.OnCleanup(&netvsc_drv_obj->Base);
516 vmbus_child_driver_unregister(drv_ctx);
518 DPRINT_EXIT(NETVSC_DRV);
523 static int netvsc_drv_init(int (*drv_init)(struct hv_driver *drv))
525 struct netvsc_driver *net_drv_obj = &g_netvsc_drv.drv_obj;
526 struct driver_context *drv_ctx = &g_netvsc_drv.drv_ctx;
529 DPRINT_ENTER(NETVSC_DRV);
531 vmbus_get_interface(&net_drv_obj->Base.VmbusChannelInterface);
533 net_drv_obj->RingBufferSize = ring_size * PAGE_SIZE;
534 net_drv_obj->OnReceiveCallback = netvsc_recv_callback;
535 net_drv_obj->OnLinkStatusChanged = netvsc_linkstatus_callback;
537 /* Callback to client driver to complete the initialization */
538 drv_init(&net_drv_obj->Base);
540 drv_ctx->driver.name = net_drv_obj->Base.name;
541 memcpy(&drv_ctx->class_id, &net_drv_obj->Base.deviceType,
542 sizeof(struct hv_guid));
544 drv_ctx->probe = netvsc_probe;
545 drv_ctx->remove = netvsc_remove;
547 /* The driver belongs to vmbus */
548 ret = vmbus_child_driver_register(drv_ctx);
550 DPRINT_EXIT(NETVSC_DRV);
555 static const struct dmi_system_id __initconst
556 hv_netvsc_dmi_table[] __maybe_unused = {
560 DMI_MATCH(DMI_SYS_VENDOR, "Microsoft Corporation"),
561 DMI_MATCH(DMI_PRODUCT_NAME, "Virtual Machine"),
562 DMI_MATCH(DMI_BOARD_NAME, "Virtual Machine"),
567 MODULE_DEVICE_TABLE(dmi, hv_netvsc_dmi_table);
569 static int __init netvsc_init(void)
573 DPRINT_ENTER(NETVSC_DRV);
574 DPRINT_INFO(NETVSC_DRV, "Netvsc initializing....");
576 if (!dmi_check_system(hv_netvsc_dmi_table))
579 ret = netvsc_drv_init(NetVscInitialize);
581 DPRINT_EXIT(NETVSC_DRV);
586 static void __exit netvsc_exit(void)
588 DPRINT_ENTER(NETVSC_DRV);
590 DPRINT_EXIT(NETVSC_DRV);
593 static const struct pci_device_id __initconst
594 hv_netvsc_pci_table[] __maybe_unused = {
595 { PCI_DEVICE(0x1414, 0x5353) }, /* VGA compatible controller */
598 MODULE_DEVICE_TABLE(pci, hv_netvsc_pci_table);
600 MODULE_LICENSE("GPL");
601 MODULE_VERSION(HV_DRV_VERSION);
602 MODULE_DESCRIPTION("Microsoft Hyper-V network driver");
604 module_init(netvsc_init);
605 module_exit(netvsc_exit);