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 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
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>
34 #include <linux/slab.h>
35 #include <linux/dmi.h>
36 #include <linux/pci.h>
38 #include <net/route.h>
40 #include <net/pkt_sched.h>
43 #include "version_info.h"
45 #include "netvsc_api.h"
47 struct net_device_context {
48 /* point back to our device context */
49 struct hv_device *device_ctx;
51 struct work_struct work;
55 #define PACKET_PAGES_LOWATER 8
56 /* Need this many pages to handle worst case fragmented packet */
57 #define PACKET_PAGES_HIWATER (MAX_SKB_FRAGS + 2)
59 static int ring_size = 128;
60 module_param(ring_size, int, S_IRUGO);
61 MODULE_PARM_DESC(ring_size, "Ring buffer size (# of pages)");
63 /* The one and only one */
64 static struct netvsc_driver g_netvsc_drv;
66 /* no-op so the netdev core doesn't return -EINVAL when modifying the the
67 * multicast address list in SIOCADDMULTI. hv is setup to get all multicast
68 * when it calls RndisFilterOnOpen() */
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;
79 if (netif_carrier_ok(net)) {
80 /* Open up the device */
81 ret = rndis_filter_open(device_obj);
83 netdev_err(net, "unable to open device (ret %d).\n",
88 netif_start_queue(net);
90 netdev_err(net, "unable to open device...link is down.\n");
96 static int netvsc_close(struct net_device *net)
98 struct net_device_context *net_device_ctx = netdev_priv(net);
99 struct hv_device *device_obj = net_device_ctx->device_ctx;
102 netif_stop_queue(net);
104 ret = rndis_filter_close(device_obj);
106 netdev_err(net, "unable to close device (ret %d).\n", ret);
111 static void netvsc_xmit_completion(void *context)
113 struct hv_netvsc_packet *packet = (struct hv_netvsc_packet *)context;
114 struct sk_buff *skb = (struct sk_buff *)
115 (unsigned long)packet->completion.send.send_completion_tid;
120 struct net_device *net = skb->dev;
121 struct net_device_context *net_device_ctx = netdev_priv(net);
122 unsigned int num_pages = skb_shinfo(skb)->nr_frags + 2;
124 dev_kfree_skb_any(skb);
126 net_device_ctx->avail += num_pages;
127 if (net_device_ctx->avail >= PACKET_PAGES_HIWATER)
128 netif_wake_queue(net);
132 static int netvsc_start_xmit(struct sk_buff *skb, struct net_device *net)
134 struct net_device_context *net_device_ctx = netdev_priv(net);
135 struct hv_driver *drv =
136 drv_to_hv_drv(net_device_ctx->device_ctx->device.driver);
137 struct netvsc_driver *net_drv_obj = drv->priv;
138 struct hv_netvsc_packet *packet;
140 unsigned int i, num_pages;
142 /* Add 1 for skb->data and additional one for RNDIS */
143 num_pages = skb_shinfo(skb)->nr_frags + 1 + 1;
144 if (num_pages > net_device_ctx->avail)
145 return NETDEV_TX_BUSY;
147 /* Allocate a netvsc packet based on # of frags. */
148 packet = kzalloc(sizeof(struct hv_netvsc_packet) +
149 (num_pages * sizeof(struct hv_page_buffer)) +
150 net_drv_obj->req_ext_size, GFP_ATOMIC);
152 /* out of memory, silently drop packet */
153 netdev_err(net, "unable to allocate hv_netvsc_packet\n");
156 net->stats.tx_dropped++;
160 packet->extension = (void *)(unsigned long)packet +
161 sizeof(struct hv_netvsc_packet) +
162 (num_pages * sizeof(struct hv_page_buffer));
164 /* Setup the rndis header */
165 packet->page_buf_cnt = num_pages;
167 /* TODO: Flush all write buffers/ memory fence ??? */
170 /* Initialize it from the skb */
171 packet->total_data_buflen = skb->len;
173 /* Start filling in the page buffers starting after RNDIS buffer. */
174 packet->page_buf[1].pfn = virt_to_phys(skb->data) >> PAGE_SHIFT;
175 packet->page_buf[1].offset
176 = (unsigned long)skb->data & (PAGE_SIZE - 1);
177 packet->page_buf[1].len = skb_headlen(skb);
179 /* Additional fragments are after SKB data */
180 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
181 skb_frag_t *f = &skb_shinfo(skb)->frags[i];
183 packet->page_buf[i+2].pfn = page_to_pfn(f->page);
184 packet->page_buf[i+2].offset = f->page_offset;
185 packet->page_buf[i+2].len = f->size;
188 /* Set the completion routine */
189 packet->completion.send.send_completion = netvsc_xmit_completion;
190 packet->completion.send.send_completion_ctx = packet;
191 packet->completion.send.send_completion_tid = (unsigned long)skb;
193 ret = net_drv_obj->send(net_device_ctx->device_ctx,
196 net->stats.tx_bytes += skb->len;
197 net->stats.tx_packets++;
199 net_device_ctx->avail -= num_pages;
200 if (net_device_ctx->avail < PACKET_PAGES_LOWATER)
201 netif_stop_queue(net);
203 /* we are shutting down or bus overloaded, just drop packet */
204 net->stats.tx_dropped++;
205 netvsc_xmit_completion(packet);
212 * netvsc_linkstatus_callback - Link up/down notification
214 static void netvsc_linkstatus_callback(struct hv_device *device_obj,
217 struct net_device *net = dev_get_drvdata(&device_obj->device);
218 struct net_device_context *ndev_ctx;
221 netdev_err(net, "got link status but net device "
222 "not initialized yet\n");
227 netif_carrier_on(net);
228 netif_wake_queue(net);
229 netif_notify_peers(net);
230 ndev_ctx = netdev_priv(net);
231 schedule_work(&ndev_ctx->work);
233 netif_carrier_off(net);
234 netif_stop_queue(net);
239 * netvsc_recv_callback - Callback when we receive a packet from the
240 * "wire" on the specified device.
242 static int netvsc_recv_callback(struct hv_device *device_obj,
243 struct hv_netvsc_packet *packet)
245 struct net_device *net = dev_get_drvdata(&device_obj->device);
252 netdev_err(net, "got receive callback but net device"
253 " not initialized yet\n");
257 /* Allocate a skb - TODO direct I/O to pages? */
258 skb = netdev_alloc_skb_ip_align(net, packet->total_data_buflen);
259 if (unlikely(!skb)) {
260 ++net->stats.rx_dropped;
264 /* for kmap_atomic */
265 local_irq_save(flags);
268 * Copy to skb. This copy is needed here since the memory pointed by
269 * hv_netvsc_packet cannot be deallocated
271 for (i = 0; i < packet->page_buf_cnt; i++) {
272 data = kmap_atomic(pfn_to_page(packet->page_buf[i].pfn),
274 data = (void *)(unsigned long)data +
275 packet->page_buf[i].offset;
277 memcpy(skb_put(skb, packet->page_buf[i].len), data,
278 packet->page_buf[i].len);
280 kunmap_atomic((void *)((unsigned long)data -
281 packet->page_buf[i].offset), KM_IRQ1);
284 local_irq_restore(flags);
286 skb->protocol = eth_type_trans(skb, net);
287 skb->ip_summed = CHECKSUM_NONE;
289 net->stats.rx_packets++;
290 net->stats.rx_bytes += skb->len;
293 * Pass the skb back up. Network stack will deallocate the skb when it
302 static void netvsc_get_drvinfo(struct net_device *net,
303 struct ethtool_drvinfo *info)
305 strcpy(info->driver, "hv_netvsc");
306 strcpy(info->version, HV_DRV_VERSION);
307 strcpy(info->fw_version, "N/A");
310 static const struct ethtool_ops ethtool_ops = {
311 .get_drvinfo = netvsc_get_drvinfo,
312 .get_link = ethtool_op_get_link,
315 static const struct net_device_ops device_ops = {
316 .ndo_open = netvsc_open,
317 .ndo_stop = netvsc_close,
318 .ndo_start_xmit = netvsc_start_xmit,
319 .ndo_set_multicast_list = netvsc_set_multicast_list,
320 .ndo_change_mtu = eth_change_mtu,
321 .ndo_validate_addr = eth_validate_addr,
322 .ndo_set_mac_address = eth_mac_addr,
326 * Send GARP packet to network peers after migrations.
327 * After Quick Migration, the network is not immediately operational in the
328 * current context when receiving RNDIS_STATUS_MEDIA_CONNECT event. So, add
329 * another netif_notify_peers() into a scheduled work, otherwise GARP packet
330 * will not be sent after quick migration, and cause network disconnection.
332 static void netvsc_send_garp(struct work_struct *w)
334 struct net_device_context *ndev_ctx;
335 struct net_device *net;
338 ndev_ctx = container_of(w, struct net_device_context, work);
339 net = dev_get_drvdata(&ndev_ctx->device_ctx->device);
340 netif_notify_peers(net);
344 static int netvsc_probe(struct device *device)
346 struct hv_driver *drv =
347 drv_to_hv_drv(device->driver);
348 struct netvsc_driver *net_drv_obj = drv->priv;
349 struct hv_device *device_obj = device_to_hv_device(device);
350 struct net_device *net = NULL;
351 struct net_device_context *net_device_ctx;
352 struct netvsc_device_info device_info;
355 if (!net_drv_obj->base.dev_add)
358 net = alloc_etherdev(sizeof(struct net_device_context));
362 /* Set initial state */
363 netif_carrier_off(net);
365 net_device_ctx = netdev_priv(net);
366 net_device_ctx->device_ctx = device_obj;
367 net_device_ctx->avail = ring_size;
368 dev_set_drvdata(device, net);
369 INIT_WORK(&net_device_ctx->work, netvsc_send_garp);
371 /* Notify the netvsc driver of the new device */
372 ret = net_drv_obj->base.dev_add(device_obj, &device_info);
375 dev_set_drvdata(device, NULL);
377 netdev_err(net, "unable to add netvsc device (ret %d)\n", ret);
382 * If carrier is still off ie we did not get a link status callback,
383 * update it if necessary
386 * FIXME: We should use a atomic or test/set instead to avoid getting
387 * out of sync with the device's link status
389 if (!netif_carrier_ok(net))
390 if (!device_info.link_state)
391 netif_carrier_on(net);
393 memcpy(net->dev_addr, device_info.mac_adr, ETH_ALEN);
395 net->netdev_ops = &device_ops;
397 /* TODO: Add GSO and Checksum offload */
398 net->hw_features = NETIF_F_SG;
399 net->features = NETIF_F_SG;
401 SET_ETHTOOL_OPS(net, ðtool_ops);
402 SET_NETDEV_DEV(net, device);
404 ret = register_netdev(net);
406 /* Remove the device and release the resource */
407 net_drv_obj->base.dev_rm(device_obj);
414 static int netvsc_remove(struct device *device)
416 struct hv_driver *drv =
417 drv_to_hv_drv(device->driver);
418 struct netvsc_driver *net_drv_obj = drv->priv;
419 struct hv_device *device_obj = device_to_hv_device(device);
420 struct net_device *net = dev_get_drvdata(&device_obj->device);
424 dev_err(device, "No net device to remove\n");
428 if (!net_drv_obj->base.dev_rm)
431 /* Stop outbound asap */
432 netif_stop_queue(net);
433 /* netif_carrier_off(net); */
435 unregister_netdev(net);
438 * Call to the vsc driver to let it know that the device is being
441 ret = net_drv_obj->base.dev_rm(device_obj);
444 netdev_err(net, "unable to remove vsc device (ret %d)\n", ret);
451 static int netvsc_drv_exit_cb(struct device *dev, void *data)
453 struct device **curr = (struct device **)data;
460 static void netvsc_drv_exit(void)
462 struct netvsc_driver *netvsc_drv_obj = &g_netvsc_drv;
463 struct hv_driver *drv = &g_netvsc_drv.base;
464 struct device *current_dev;
471 ret = driver_for_each_device(&drv->driver, NULL,
472 ¤t_dev, netvsc_drv_exit_cb);
474 if (current_dev == NULL)
477 /* Initiate removal from the top-down */
478 dev_err(current_dev, "unregistering device (%s)...\n",
479 dev_name(current_dev));
481 device_unregister(current_dev);
484 if (netvsc_drv_obj->base.cleanup)
485 netvsc_drv_obj->base.cleanup(&netvsc_drv_obj->base);
487 vmbus_child_driver_unregister(&drv->driver);
492 static int netvsc_drv_init(int (*drv_init)(struct hv_driver *drv))
494 struct netvsc_driver *net_drv_obj = &g_netvsc_drv;
495 struct hv_driver *drv = &g_netvsc_drv.base;
498 net_drv_obj->ring_buf_size = ring_size * PAGE_SIZE;
499 net_drv_obj->recv_cb = netvsc_recv_callback;
500 net_drv_obj->link_status_change = netvsc_linkstatus_callback;
501 drv->priv = net_drv_obj;
503 /* Callback to client driver to complete the initialization */
504 drv_init(&net_drv_obj->base);
506 drv->driver.name = net_drv_obj->base.name;
508 drv->driver.probe = netvsc_probe;
509 drv->driver.remove = netvsc_remove;
511 /* The driver belongs to vmbus */
512 ret = vmbus_child_driver_register(&drv->driver);
517 static const struct dmi_system_id __initconst
518 hv_netvsc_dmi_table[] __maybe_unused = {
522 DMI_MATCH(DMI_SYS_VENDOR, "Microsoft Corporation"),
523 DMI_MATCH(DMI_PRODUCT_NAME, "Virtual Machine"),
524 DMI_MATCH(DMI_BOARD_NAME, "Virtual Machine"),
529 MODULE_DEVICE_TABLE(dmi, hv_netvsc_dmi_table);
531 static int __init netvsc_init(void)
533 pr_info("initializing....");
535 if (!dmi_check_system(hv_netvsc_dmi_table))
538 return netvsc_drv_init(netvsc_initialize);
541 static void __exit netvsc_exit(void)
546 static const struct pci_device_id __initconst
547 hv_netvsc_pci_table[] __maybe_unused = {
548 { PCI_DEVICE(0x1414, 0x5353) }, /* VGA compatible controller */
551 MODULE_DEVICE_TABLE(pci, hv_netvsc_pci_table);
553 MODULE_LICENSE("GPL");
554 MODULE_VERSION(HV_DRV_VERSION);
555 MODULE_DESCRIPTION("Microsoft Hyper-V network driver");
557 module_init(netvsc_init);
558 module_exit(netvsc_exit);