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 "hyperv_net.h"
45 struct net_device_context
{
46 /* point back to our device context */
47 struct hv_device
*device_ctx
;
49 struct work_struct work
;
53 #define PACKET_PAGES_LOWATER 8
54 /* Need this many pages to handle worst case fragmented packet */
55 #define PACKET_PAGES_HIWATER (MAX_SKB_FRAGS + 2)
57 static int ring_size
= 128;
58 module_param(ring_size
, int, S_IRUGO
);
59 MODULE_PARM_DESC(ring_size
, "Ring buffer size (# of pages)");
61 /* no-op so the netdev core doesn't return -EINVAL when modifying the the
62 * multicast address list in SIOCADDMULTI. hv is setup to get all multicast
63 * when it calls RndisFilterOnOpen() */
64 static void netvsc_set_multicast_list(struct net_device
*net
)
68 static int netvsc_open(struct net_device
*net
)
70 struct net_device_context
*net_device_ctx
= netdev_priv(net
);
71 struct hv_device
*device_obj
= net_device_ctx
->device_ctx
;
74 if (netif_carrier_ok(net
)) {
75 /* Open up the device */
76 ret
= rndis_filter_open(device_obj
);
78 netdev_err(net
, "unable to open device (ret %d).\n",
83 netif_start_queue(net
);
85 netdev_err(net
, "unable to open device...link is down.\n");
91 static int netvsc_close(struct net_device
*net
)
93 struct net_device_context
*net_device_ctx
= netdev_priv(net
);
94 struct hv_device
*device_obj
= net_device_ctx
->device_ctx
;
97 netif_stop_queue(net
);
99 ret
= rndis_filter_close(device_obj
);
101 netdev_err(net
, "unable to close device (ret %d).\n", ret
);
106 static void netvsc_xmit_completion(void *context
)
108 struct hv_netvsc_packet
*packet
= (struct hv_netvsc_packet
*)context
;
109 struct sk_buff
*skb
= (struct sk_buff
*)
110 (unsigned long)packet
->completion
.send
.send_completion_tid
;
115 struct net_device
*net
= skb
->dev
;
116 struct net_device_context
*net_device_ctx
= netdev_priv(net
);
117 unsigned int num_pages
= skb_shinfo(skb
)->nr_frags
+ 2;
119 dev_kfree_skb_any(skb
);
121 net_device_ctx
->avail
+= num_pages
;
122 if (net_device_ctx
->avail
>= PACKET_PAGES_HIWATER
)
123 netif_wake_queue(net
);
127 static int netvsc_start_xmit(struct sk_buff
*skb
, struct net_device
*net
)
129 struct net_device_context
*net_device_ctx
= netdev_priv(net
);
130 struct hv_netvsc_packet
*packet
;
132 unsigned int i
, num_pages
;
134 /* Add 1 for skb->data and additional one for RNDIS */
135 num_pages
= skb_shinfo(skb
)->nr_frags
+ 1 + 1;
136 if (num_pages
> net_device_ctx
->avail
)
137 return NETDEV_TX_BUSY
;
139 /* Allocate a netvsc packet based on # of frags. */
140 packet
= kzalloc(sizeof(struct hv_netvsc_packet
) +
141 (num_pages
* sizeof(struct hv_page_buffer
)) +
142 sizeof(struct rndis_filter_packet
), GFP_ATOMIC
);
144 /* out of memory, silently drop packet */
145 netdev_err(net
, "unable to allocate hv_netvsc_packet\n");
148 net
->stats
.tx_dropped
++;
152 packet
->extension
= (void *)(unsigned long)packet
+
153 sizeof(struct hv_netvsc_packet
) +
154 (num_pages
* sizeof(struct hv_page_buffer
));
156 /* Setup the rndis header */
157 packet
->page_buf_cnt
= num_pages
;
159 /* TODO: Flush all write buffers/ memory fence ??? */
162 /* Initialize it from the skb */
163 packet
->total_data_buflen
= skb
->len
;
165 /* Start filling in the page buffers starting after RNDIS buffer. */
166 packet
->page_buf
[1].pfn
= virt_to_phys(skb
->data
) >> PAGE_SHIFT
;
167 packet
->page_buf
[1].offset
168 = (unsigned long)skb
->data
& (PAGE_SIZE
- 1);
169 packet
->page_buf
[1].len
= skb_headlen(skb
);
171 /* Additional fragments are after SKB data */
172 for (i
= 0; i
< skb_shinfo(skb
)->nr_frags
; i
++) {
173 skb_frag_t
*f
= &skb_shinfo(skb
)->frags
[i
];
175 packet
->page_buf
[i
+2].pfn
= page_to_pfn(f
->page
);
176 packet
->page_buf
[i
+2].offset
= f
->page_offset
;
177 packet
->page_buf
[i
+2].len
= f
->size
;
180 /* Set the completion routine */
181 packet
->completion
.send
.send_completion
= netvsc_xmit_completion
;
182 packet
->completion
.send
.send_completion_ctx
= packet
;
183 packet
->completion
.send
.send_completion_tid
= (unsigned long)skb
;
185 ret
= rndis_filter_send(net_device_ctx
->device_ctx
,
188 net
->stats
.tx_bytes
+= skb
->len
;
189 net
->stats
.tx_packets
++;
191 net_device_ctx
->avail
-= num_pages
;
192 if (net_device_ctx
->avail
< PACKET_PAGES_LOWATER
)
193 netif_stop_queue(net
);
195 /* we are shutting down or bus overloaded, just drop packet */
196 net
->stats
.tx_dropped
++;
197 netvsc_xmit_completion(packet
);
204 * netvsc_linkstatus_callback - Link up/down notification
206 void netvsc_linkstatus_callback(struct hv_device
*device_obj
,
209 struct net_device
*net
= dev_get_drvdata(&device_obj
->device
);
210 struct net_device_context
*ndev_ctx
;
213 netdev_err(net
, "got link status but net device "
214 "not initialized yet\n");
219 netif_carrier_on(net
);
220 netif_wake_queue(net
);
221 netif_notify_peers(net
);
222 ndev_ctx
= netdev_priv(net
);
223 schedule_work(&ndev_ctx
->work
);
225 netif_carrier_off(net
);
226 netif_stop_queue(net
);
231 * netvsc_recv_callback - Callback when we receive a packet from the
232 * "wire" on the specified device.
234 int netvsc_recv_callback(struct hv_device
*device_obj
,
235 struct hv_netvsc_packet
*packet
)
237 struct net_device
*net
= dev_get_drvdata(&device_obj
->device
);
244 netdev_err(net
, "got receive callback but net device"
245 " not initialized yet\n");
249 /* Allocate a skb - TODO direct I/O to pages? */
250 skb
= netdev_alloc_skb_ip_align(net
, packet
->total_data_buflen
);
251 if (unlikely(!skb
)) {
252 ++net
->stats
.rx_dropped
;
256 /* for kmap_atomic */
257 local_irq_save(flags
);
260 * Copy to skb. This copy is needed here since the memory pointed by
261 * hv_netvsc_packet cannot be deallocated
263 for (i
= 0; i
< packet
->page_buf_cnt
; i
++) {
264 data
= kmap_atomic(pfn_to_page(packet
->page_buf
[i
].pfn
),
266 data
= (void *)(unsigned long)data
+
267 packet
->page_buf
[i
].offset
;
269 memcpy(skb_put(skb
, packet
->page_buf
[i
].len
), data
,
270 packet
->page_buf
[i
].len
);
272 kunmap_atomic((void *)((unsigned long)data
-
273 packet
->page_buf
[i
].offset
), KM_IRQ1
);
276 local_irq_restore(flags
);
278 skb
->protocol
= eth_type_trans(skb
, net
);
279 skb
->ip_summed
= CHECKSUM_NONE
;
281 net
->stats
.rx_packets
++;
282 net
->stats
.rx_bytes
+= skb
->len
;
285 * Pass the skb back up. Network stack will deallocate the skb when it
294 static void netvsc_get_drvinfo(struct net_device
*net
,
295 struct ethtool_drvinfo
*info
)
297 strcpy(info
->driver
, "hv_netvsc");
298 strcpy(info
->version
, HV_DRV_VERSION
);
299 strcpy(info
->fw_version
, "N/A");
302 static const struct ethtool_ops ethtool_ops
= {
303 .get_drvinfo
= netvsc_get_drvinfo
,
304 .get_link
= ethtool_op_get_link
,
307 static const struct net_device_ops device_ops
= {
308 .ndo_open
= netvsc_open
,
309 .ndo_stop
= netvsc_close
,
310 .ndo_start_xmit
= netvsc_start_xmit
,
311 .ndo_set_multicast_list
= netvsc_set_multicast_list
,
312 .ndo_change_mtu
= eth_change_mtu
,
313 .ndo_validate_addr
= eth_validate_addr
,
314 .ndo_set_mac_address
= eth_mac_addr
,
318 * Send GARP packet to network peers after migrations.
319 * After Quick Migration, the network is not immediately operational in the
320 * current context when receiving RNDIS_STATUS_MEDIA_CONNECT event. So, add
321 * another netif_notify_peers() into a scheduled work, otherwise GARP packet
322 * will not be sent after quick migration, and cause network disconnection.
324 static void netvsc_send_garp(struct work_struct
*w
)
326 struct net_device_context
*ndev_ctx
;
327 struct net_device
*net
;
330 ndev_ctx
= container_of(w
, struct net_device_context
, work
);
331 net
= dev_get_drvdata(&ndev_ctx
->device_ctx
->device
);
332 netif_notify_peers(net
);
336 static int netvsc_probe(struct hv_device
*dev
)
338 struct net_device
*net
= NULL
;
339 struct net_device_context
*net_device_ctx
;
340 struct netvsc_device_info device_info
;
343 net
= alloc_etherdev(sizeof(struct net_device_context
));
347 /* Set initial state */
348 netif_carrier_off(net
);
350 net_device_ctx
= netdev_priv(net
);
351 net_device_ctx
->device_ctx
= dev
;
352 net_device_ctx
->avail
= ring_size
;
353 dev_set_drvdata(&dev
->device
, net
);
354 INIT_WORK(&net_device_ctx
->work
, netvsc_send_garp
);
356 /* Notify the netvsc driver of the new device */
357 device_info
.ring_size
= ring_size
;
358 ret
= rndis_filte_device_add(dev
, &device_info
);
361 dev_set_drvdata(&dev
->device
, NULL
);
363 netdev_err(net
, "unable to add netvsc device (ret %d)\n", ret
);
368 * If carrier is still off ie we did not get a link status callback,
369 * update it if necessary
372 * FIXME: We should use a atomic or test/set instead to avoid getting
373 * out of sync with the device's link status
375 if (!netif_carrier_ok(net
))
376 if (!device_info
.link_state
)
377 netif_carrier_on(net
);
379 memcpy(net
->dev_addr
, device_info
.mac_adr
, ETH_ALEN
);
381 net
->netdev_ops
= &device_ops
;
383 /* TODO: Add GSO and Checksum offload */
384 net
->hw_features
= NETIF_F_SG
;
385 net
->features
= NETIF_F_SG
;
387 SET_ETHTOOL_OPS(net
, ðtool_ops
);
388 SET_NETDEV_DEV(net
, &dev
->device
);
390 ret
= register_netdev(net
);
392 /* Remove the device and release the resource */
393 rndis_filter_device_remove(dev
);
400 static int netvsc_remove(struct hv_device
*dev
)
402 struct net_device
*net
= dev_get_drvdata(&dev
->device
);
406 dev_err(&dev
->device
, "No net device to remove\n");
410 /* Stop outbound asap */
411 netif_stop_queue(net
);
412 /* netif_carrier_off(net); */
414 unregister_netdev(net
);
417 * Call to the vsc driver to let it know that the device is being
420 ret
= rndis_filter_device_remove(dev
);
423 netdev_err(net
, "unable to remove vsc device (ret %d)\n", ret
);
430 /* The one and only one */
431 static struct hv_driver netvsc_drv
= {
432 .probe
= netvsc_probe
,
433 .remove
= netvsc_remove
,
436 static void __exit
netvsc_drv_exit(void)
438 vmbus_child_driver_unregister(&netvsc_drv
.driver
);
442 static const struct dmi_system_id __initconst
443 hv_netvsc_dmi_table
[] __maybe_unused
= {
447 DMI_MATCH(DMI_SYS_VENDOR
, "Microsoft Corporation"),
448 DMI_MATCH(DMI_PRODUCT_NAME
, "Virtual Machine"),
449 DMI_MATCH(DMI_BOARD_NAME
, "Virtual Machine"),
454 MODULE_DEVICE_TABLE(dmi
, hv_netvsc_dmi_table
);
456 static int __init
netvsc_drv_init(void)
458 struct hv_driver
*drv
= &netvsc_drv
;
461 pr_info("initializing....");
463 if (!dmi_check_system(hv_netvsc_dmi_table
))
467 /* Callback to client driver to complete the initialization */
468 netvsc_initialize(drv
);
470 drv
->driver
.name
= drv
->name
;
472 /* The driver belongs to vmbus */
473 ret
= vmbus_child_driver_register(&drv
->driver
);
478 static const struct pci_device_id __initconst
479 hv_netvsc_pci_table
[] __maybe_unused
= {
480 { PCI_DEVICE(0x1414, 0x5353) }, /* VGA compatible controller */
483 MODULE_DEVICE_TABLE(pci
, hv_netvsc_pci_table
);
485 MODULE_LICENSE("GPL");
486 MODULE_VERSION(HV_DRV_VERSION
);
487 MODULE_DESCRIPTION("Microsoft Hyper-V network driver");
489 module_init(netvsc_drv_init
);
490 module_exit(netvsc_drv_exit
);