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/atomic.h>
23 #include <linux/module.h>
24 #include <linux/highmem.h>
25 #include <linux/device.h>
27 #include <linux/delay.h>
28 #include <linux/netdevice.h>
29 #include <linux/inetdevice.h>
30 #include <linux/etherdevice.h>
31 #include <linux/skbuff.h>
33 #include <linux/slab.h>
34 #include <linux/dmi.h>
35 #include <linux/pci.h>
37 #include <net/route.h>
39 #include <net/pkt_sched.h>
42 #include "version_info.h"
44 #include "netvsc_api.h"
46 struct net_device_context {
47 /* point back to our device context */
48 struct vm_device *device_ctx;
50 struct work_struct work;
53 struct netvsc_driver_context {
54 /* !! These must be the first 2 fields !! */
55 /* Which is a bug FIXME! */
56 struct driver_context drv_ctx;
57 struct netvsc_driver drv_obj;
60 #define PACKET_PAGES_LOWATER 8
61 /* Need this many pages to handle worst case fragmented packet */
62 #define PACKET_PAGES_HIWATER (MAX_SKB_FRAGS + 2)
64 static int ring_size = roundup_pow_of_two(2*MAX_SKB_FRAGS+1);
65 module_param(ring_size, int, S_IRUGO);
66 MODULE_PARM_DESC(ring_size, "Ring buffer size (# of pages)");
68 /* The one and only one */
69 static struct netvsc_driver_context g_netvsc_drv;
71 static void netvsc_set_multicast_list(struct net_device *net)
75 static int netvsc_open(struct net_device *net)
77 struct net_device_context *net_device_ctx = netdev_priv(net);
78 struct hv_device *device_obj = &net_device_ctx->device_ctx->device_obj;
81 DPRINT_ENTER(NETVSC_DRV);
83 if (netif_carrier_ok(net)) {
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;
126 DPRINT_ENTER(NETVSC_DRV);
131 struct net_device *net = skb->dev;
132 struct net_device_context *net_device_ctx = netdev_priv(net);
133 unsigned int num_pages = skb_shinfo(skb)->nr_frags + 2;
135 dev_kfree_skb_any(skb);
137 atomic_add(num_pages, &net_device_ctx->avail);
138 if (atomic_read(&net_device_ctx->avail) >=
139 PACKET_PAGES_HIWATER)
140 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;
156 unsigned int i, num_pages;
158 DPRINT_ENTER(NETVSC_DRV);
160 DPRINT_DBG(NETVSC_DRV, "xmit packet - len %d data_len %d",
161 skb->len, skb->data_len);
163 /* Add 1 for skb->data and additional one for RNDIS */
164 num_pages = skb_shinfo(skb)->nr_frags + 1 + 1;
165 if (num_pages > atomic_read(&net_device_ctx->avail))
166 return NETDEV_TX_BUSY;
168 /* Allocate a netvsc packet based on # of frags. */
169 packet = kzalloc(sizeof(struct hv_netvsc_packet) +
170 (num_pages * sizeof(struct hv_page_buffer)) +
171 net_drv_obj->RequestExtSize, GFP_ATOMIC);
173 /* out of memory, silently drop packet */
174 DPRINT_ERR(NETVSC_DRV, "unable to allocate hv_netvsc_packet");
177 net->stats.tx_dropped++;
181 packet->Extension = (void *)(unsigned long)packet +
182 sizeof(struct hv_netvsc_packet) +
183 (num_pages * sizeof(struct hv_page_buffer));
185 /* Setup the rndis header */
186 packet->PageBufferCount = num_pages;
188 /* TODO: Flush all write buffers/ memory fence ??? */
191 /* Initialize it from the skb */
192 packet->TotalDataBufferLength = skb->len;
194 /* Start filling in the page buffers starting after RNDIS buffer. */
195 packet->PageBuffers[1].Pfn = virt_to_phys(skb->data) >> PAGE_SHIFT;
196 packet->PageBuffers[1].Offset
197 = (unsigned long)skb->data & (PAGE_SIZE - 1);
198 packet->PageBuffers[1].Length = skb_headlen(skb);
200 /* Additional fragments are after SKB data */
201 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
202 skb_frag_t *f = &skb_shinfo(skb)->frags[i];
204 packet->PageBuffers[i+2].Pfn = page_to_pfn(f->page);
205 packet->PageBuffers[i+2].Offset = f->page_offset;
206 packet->PageBuffers[i+2].Length = f->size;
209 /* Set the completion routine */
210 packet->Completion.Send.OnSendCompletion = netvsc_xmit_completion;
211 packet->Completion.Send.SendCompletionContext = packet;
212 packet->Completion.Send.SendCompletionTid = (unsigned long)skb;
214 ret = net_drv_obj->OnSend(&net_device_ctx->device_ctx->device_obj,
217 net->stats.tx_bytes += skb->len;
218 net->stats.tx_packets++;
220 DPRINT_DBG(NETVSC_DRV, "# of xmits %lu total size %lu",
221 net->stats.tx_packets,
222 net->stats.tx_bytes);
224 atomic_sub(num_pages, &net_device_ctx->avail);
225 if (atomic_read(&net_device_ctx->avail) < PACKET_PAGES_LOWATER)
226 netif_stop_queue(net);
228 /* we are shutting down or bus overloaded, just drop packet */
229 net->stats.tx_dropped++;
230 netvsc_xmit_completion(packet);
233 DPRINT_EXIT(NETVSC_DRV);
238 * netvsc_linkstatus_callback - Link up/down notification
240 static void netvsc_linkstatus_callback(struct hv_device *device_obj,
243 struct vm_device *device_ctx = to_vm_device(device_obj);
244 struct net_device *net = dev_get_drvdata(&device_ctx->device);
245 struct net_device_context *ndev_ctx;
247 DPRINT_ENTER(NETVSC_DRV);
250 DPRINT_ERR(NETVSC_DRV, "got link status but net device "
251 "not initialized yet");
256 netif_carrier_on(net);
257 netif_wake_queue(net);
258 netif_notify_peers(net);
259 ndev_ctx = netdev_priv(net);
260 schedule_work(&ndev_ctx->work);
262 netif_carrier_off(net);
263 netif_stop_queue(net);
265 DPRINT_EXIT(NETVSC_DRV);
269 * netvsc_recv_callback - Callback when we receive a packet from the
270 * "wire" on the specified device.
272 static int netvsc_recv_callback(struct hv_device *device_obj,
273 struct hv_netvsc_packet *packet)
275 struct vm_device *device_ctx = to_vm_device(device_obj);
276 struct net_device *net = dev_get_drvdata(&device_ctx->device);
282 DPRINT_ENTER(NETVSC_DRV);
285 DPRINT_ERR(NETVSC_DRV, "got receive callback but net device "
286 "not initialized yet");
290 /* Allocate a skb - TODO direct I/O to pages? */
291 skb = netdev_alloc_skb_ip_align(net, packet->TotalDataBufferLength);
292 if (unlikely(!skb)) {
293 ++net->stats.rx_dropped;
297 /* for kmap_atomic */
298 local_irq_save(flags);
301 * Copy to skb. This copy is needed here since the memory pointed by
302 * hv_netvsc_packet cannot be deallocated
304 for (i = 0; i < packet->PageBufferCount; i++) {
305 data = kmap_atomic(pfn_to_page(packet->PageBuffers[i].Pfn),
307 data = (void *)(unsigned long)data +
308 packet->PageBuffers[i].Offset;
310 memcpy(skb_put(skb, packet->PageBuffers[i].Length), data,
311 packet->PageBuffers[i].Length);
313 kunmap_atomic((void *)((unsigned long)data -
314 packet->PageBuffers[i].Offset), KM_IRQ1);
317 local_irq_restore(flags);
319 skb->protocol = eth_type_trans(skb, net);
320 skb->ip_summed = CHECKSUM_NONE;
322 net->stats.rx_packets++;
323 net->stats.rx_bytes += skb->len;
326 * Pass the skb back up. Network stack will deallocate the skb when it
332 DPRINT_DBG(NETVSC_DRV, "# of recvs %lu total size %lu",
333 net->stats.rx_packets, net->stats.rx_bytes);
335 DPRINT_EXIT(NETVSC_DRV);
340 static void netvsc_get_drvinfo(struct net_device *net,
341 struct ethtool_drvinfo *info)
343 strcpy(info->driver, "hv_netvsc");
344 strcpy(info->version, HV_DRV_VERSION);
345 strcpy(info->fw_version, "N/A");
348 static const struct ethtool_ops ethtool_ops = {
349 .get_drvinfo = netvsc_get_drvinfo,
350 .get_sg = ethtool_op_get_sg,
351 .set_sg = ethtool_op_set_sg,
352 .get_link = ethtool_op_get_link,
355 static const struct net_device_ops device_ops = {
356 .ndo_open = netvsc_open,
357 .ndo_stop = netvsc_close,
358 .ndo_start_xmit = netvsc_start_xmit,
359 .ndo_set_multicast_list = netvsc_set_multicast_list,
360 .ndo_change_mtu = eth_change_mtu,
361 .ndo_validate_addr = eth_validate_addr,
362 .ndo_set_mac_address = eth_mac_addr,
366 * Send GARP packet to network peers after migrations.
367 * After Quick Migration, the network is not immediately operational in the
368 * current context when receiving RNDIS_STATUS_MEDIA_CONNECT event. So, add
369 * another netif_notify_peers() into a scheduled work, otherwise GARP packet
370 * will not be sent after quick migration, and cause network disconnection.
372 static void netvsc_send_garp(struct work_struct *w)
374 struct net_device_context *ndev_ctx;
375 struct net_device *net;
378 ndev_ctx = container_of(w, struct net_device_context, work);
379 net = dev_get_drvdata(&ndev_ctx->device_ctx->device);
380 netif_notify_peers(net);
384 static int netvsc_probe(struct device *device)
386 struct driver_context *driver_ctx =
387 driver_to_driver_context(device->driver);
388 struct netvsc_driver_context *net_drv_ctx =
389 (struct netvsc_driver_context *)driver_ctx;
390 struct netvsc_driver *net_drv_obj = &net_drv_ctx->drv_obj;
391 struct vm_device *device_ctx = device_to_vm_device(device);
392 struct hv_device *device_obj = &device_ctx->device_obj;
393 struct net_device *net = NULL;
394 struct net_device_context *net_device_ctx;
395 struct netvsc_device_info device_info;
398 DPRINT_ENTER(NETVSC_DRV);
400 if (!net_drv_obj->Base.OnDeviceAdd)
403 net = alloc_etherdev(sizeof(struct net_device_context));
407 /* Set initial state */
408 netif_carrier_off(net);
409 netif_stop_queue(net);
411 net_device_ctx = netdev_priv(net);
412 net_device_ctx->device_ctx = device_ctx;
413 atomic_set(&net_device_ctx->avail, ring_size);
414 dev_set_drvdata(device, net);
415 INIT_WORK(&net_device_ctx->work, netvsc_send_garp);
417 /* Notify the netvsc driver of the new device */
418 ret = net_drv_obj->Base.OnDeviceAdd(device_obj, &device_info);
421 dev_set_drvdata(device, NULL);
423 DPRINT_ERR(NETVSC_DRV, "unable to add netvsc device (ret %d)",
429 * If carrier is still off ie we did not get a link status callback,
430 * update it if necessary
433 * FIXME: We should use a atomic or test/set instead to avoid getting
434 * out of sync with the device's link status
436 if (!netif_carrier_ok(net))
437 if (!device_info.LinkState)
438 netif_carrier_on(net);
440 memcpy(net->dev_addr, device_info.MacAddr, ETH_ALEN);
442 net->netdev_ops = &device_ops;
444 /* TODO: Add GSO and Checksum offload */
445 net->features = NETIF_F_SG;
447 SET_ETHTOOL_OPS(net, ðtool_ops);
448 SET_NETDEV_DEV(net, device);
450 ret = register_netdev(net);
452 /* Remove the device and release the resource */
453 net_drv_obj->Base.OnDeviceRemove(device_obj);
457 DPRINT_EXIT(NETVSC_DRV);
461 static int netvsc_remove(struct device *device)
463 struct driver_context *driver_ctx =
464 driver_to_driver_context(device->driver);
465 struct netvsc_driver_context *net_drv_ctx =
466 (struct netvsc_driver_context *)driver_ctx;
467 struct netvsc_driver *net_drv_obj = &net_drv_ctx->drv_obj;
468 struct vm_device *device_ctx = device_to_vm_device(device);
469 struct net_device *net = dev_get_drvdata(&device_ctx->device);
470 struct hv_device *device_obj = &device_ctx->device_obj;
473 DPRINT_ENTER(NETVSC_DRV);
476 DPRINT_INFO(NETVSC, "no net device to remove");
477 DPRINT_EXIT(NETVSC_DRV);
481 if (!net_drv_obj->Base.OnDeviceRemove) {
482 DPRINT_EXIT(NETVSC_DRV);
486 /* Stop outbound asap */
487 netif_stop_queue(net);
488 /* netif_carrier_off(net); */
490 unregister_netdev(net);
493 * Call to the vsc driver to let it know that the device is being
496 ret = net_drv_obj->Base.OnDeviceRemove(device_obj);
499 DPRINT_ERR(NETVSC, "unable to remove vsc device (ret %d)", ret);
503 DPRINT_EXIT(NETVSC_DRV);
507 static int netvsc_drv_exit_cb(struct device *dev, void *data)
509 struct device **curr = (struct device **)data;
516 static void netvsc_drv_exit(void)
518 struct netvsc_driver *netvsc_drv_obj = &g_netvsc_drv.drv_obj;
519 struct driver_context *drv_ctx = &g_netvsc_drv.drv_ctx;
520 struct device *current_dev;
523 DPRINT_ENTER(NETVSC_DRV);
529 ret = driver_for_each_device(&drv_ctx->driver, NULL,
530 ¤t_dev, netvsc_drv_exit_cb);
532 DPRINT_WARN(NETVSC_DRV,
533 "driver_for_each_device returned %d", ret);
535 if (current_dev == NULL)
538 /* Initiate removal from the top-down */
539 DPRINT_INFO(NETVSC_DRV, "unregistering device (%p)...",
542 device_unregister(current_dev);
545 if (netvsc_drv_obj->Base.OnCleanup)
546 netvsc_drv_obj->Base.OnCleanup(&netvsc_drv_obj->Base);
548 vmbus_child_driver_unregister(drv_ctx);
550 DPRINT_EXIT(NETVSC_DRV);
555 static int netvsc_drv_init(int (*drv_init)(struct hv_driver *drv))
557 struct netvsc_driver *net_drv_obj = &g_netvsc_drv.drv_obj;
558 struct driver_context *drv_ctx = &g_netvsc_drv.drv_ctx;
561 DPRINT_ENTER(NETVSC_DRV);
563 vmbus_get_interface(&net_drv_obj->Base.VmbusChannelInterface);
565 net_drv_obj->RingBufferSize = ring_size * PAGE_SIZE;
566 net_drv_obj->OnReceiveCallback = netvsc_recv_callback;
567 net_drv_obj->OnLinkStatusChanged = netvsc_linkstatus_callback;
569 /* Callback to client driver to complete the initialization */
570 drv_init(&net_drv_obj->Base);
572 drv_ctx->driver.name = net_drv_obj->Base.name;
573 memcpy(&drv_ctx->class_id, &net_drv_obj->Base.deviceType,
574 sizeof(struct hv_guid));
576 drv_ctx->probe = netvsc_probe;
577 drv_ctx->remove = netvsc_remove;
579 /* The driver belongs to vmbus */
580 ret = vmbus_child_driver_register(drv_ctx);
582 DPRINT_EXIT(NETVSC_DRV);
587 static const struct dmi_system_id __initconst
588 hv_netvsc_dmi_table[] __maybe_unused = {
592 DMI_MATCH(DMI_SYS_VENDOR, "Microsoft Corporation"),
593 DMI_MATCH(DMI_PRODUCT_NAME, "Virtual Machine"),
594 DMI_MATCH(DMI_BOARD_NAME, "Virtual Machine"),
599 MODULE_DEVICE_TABLE(dmi, hv_netvsc_dmi_table);
601 static int __init netvsc_init(void)
605 DPRINT_ENTER(NETVSC_DRV);
606 DPRINT_INFO(NETVSC_DRV, "Netvsc initializing....");
608 if (!dmi_check_system(hv_netvsc_dmi_table))
611 ret = netvsc_drv_init(NetVscInitialize);
613 DPRINT_EXIT(NETVSC_DRV);
618 static void __exit netvsc_exit(void)
620 DPRINT_ENTER(NETVSC_DRV);
622 DPRINT_EXIT(NETVSC_DRV);
625 static const struct pci_device_id __initconst
626 hv_netvsc_pci_table[] __maybe_unused = {
627 { PCI_DEVICE(0x1414, 0x5353) }, /* VGA compatible controller */
630 MODULE_DEVICE_TABLE(pci, hv_netvsc_pci_table);
632 MODULE_LICENSE("GPL");
633 MODULE_VERSION(HV_DRV_VERSION);
634 MODULE_DESCRIPTION("Microsoft Hyper-V network driver");
636 module_init(netvsc_init);
637 module_exit(netvsc_exit);