blob: bfd4c81c410f3774636f545b19ba09682df998d6 [file] [log] [blame]
Hank Janssenfceaf242009-07-13 15:34:54 -07001/*
Hank Janssenfceaf242009-07-13 15:34:54 -07002 * Copyright (c) 2009, Microsoft Corporation.
3 *
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.
7 *
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
11 * more details.
12 *
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.
16 *
17 * Authors:
Haiyang Zhangd0e94d12009-11-23 17:00:22 +000018 * Haiyang Zhang <haiyangz@microsoft.com>
Hank Janssenfceaf242009-07-13 15:34:54 -070019 * Hank Janssen <hjanssen@microsoft.com>
Hank Janssenfceaf242009-07-13 15:34:54 -070020 */
Hank Jansseneb335bc2011-03-29 13:58:48 -070021#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
22
Hank Janssenfceaf242009-07-13 15:34:54 -070023#include <linux/init.h>
K. Y. Srinivasan9079ce62011-06-16 13:16:37 -070024#include <linux/atomic.h>
Hank Janssenfceaf242009-07-13 15:34:54 -070025#include <linux/module.h>
26#include <linux/highmem.h>
27#include <linux/device.h>
Hank Janssenfceaf242009-07-13 15:34:54 -070028#include <linux/io.h>
Hank Janssenfceaf242009-07-13 15:34:54 -070029#include <linux/delay.h>
30#include <linux/netdevice.h>
31#include <linux/inetdevice.h>
32#include <linux/etherdevice.h>
33#include <linux/skbuff.h>
34#include <linux/in.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090035#include <linux/slab.h>
K. Y. Srinivasan06e719d2010-05-07 17:52:17 -060036#include <linux/dmi.h>
37#include <linux/pci.h>
Hank Janssenfceaf242009-07-13 15:34:54 -070038#include <net/arp.h>
39#include <net/route.h>
40#include <net/sock.h>
41#include <net/pkt_sched.h>
K. Y. Srinivasan3f335ea2011-05-12 19:34:15 -070042
43#include "hyperv.h"
K. Y. Srinivasan5ca72522011-05-12 19:34:37 -070044#include "hyperv_net.h"
Hank Janssenfceaf242009-07-13 15:34:54 -070045
Hank Janssenfceaf242009-07-13 15:34:54 -070046struct net_device_context {
Greg Kroah-Hartman02fafbc2009-08-31 21:09:45 -070047 /* point back to our device context */
K. Y. Srinivasan6bad88d2011-03-07 13:35:48 -080048 struct hv_device *device_ctx;
K. Y. Srinivasan9079ce62011-06-16 13:16:37 -070049 atomic_t avail;
Haiyang Zhang122a5f62011-05-27 06:21:55 -070050 struct delayed_work dwork;
Hank Janssenfceaf242009-07-13 15:34:54 -070051};
52
Hank Janssenfceaf242009-07-13 15:34:54 -070053
Stephen Hemmingerb220f5f2010-05-04 09:58:56 -070054#define PACKET_PAGES_LOWATER 8
55/* Need this many pages to handle worst case fragmented packet */
56#define PACKET_PAGES_HIWATER (MAX_SKB_FRAGS + 2)
57
Hank Janssen99c8da02010-10-12 10:45:23 -070058static int ring_size = 128;
Stephen Hemminger450d7a42010-05-04 09:58:53 -070059module_param(ring_size, int, S_IRUGO);
60MODULE_PARM_DESC(ring_size, "Ring buffer size (# of pages)");
Hank Janssenfceaf242009-07-13 15:34:54 -070061
Brandon Philips0ff36f62010-11-15 11:55:33 -080062/* no-op so the netdev core doesn't return -EINVAL when modifying the the
63 * multicast address list in SIOCADDMULTI. hv is setup to get all multicast
64 * when it calls RndisFilterOnOpen() */
Greg Kroah-Hartman4e9bfef2009-07-15 12:45:20 -070065static void netvsc_set_multicast_list(struct net_device *net)
Hank Janssenfceaf242009-07-13 15:34:54 -070066{
67}
68
Hank Janssenfceaf242009-07-13 15:34:54 -070069static int netvsc_open(struct net_device *net)
70{
Hank Janssenfceaf242009-07-13 15:34:54 -070071 struct net_device_context *net_device_ctx = netdev_priv(net);
K. Y. Srinivasan6bad88d2011-03-07 13:35:48 -080072 struct hv_device *device_obj = net_device_ctx->device_ctx;
Greg Kroah-Hartman02fafbc2009-08-31 21:09:45 -070073 int ret = 0;
Hank Janssenfceaf242009-07-13 15:34:54 -070074
Greg Kroah-Hartman02fafbc2009-08-31 21:09:45 -070075 if (netif_carrier_ok(net)) {
Bill Pemberton454f18a2009-07-27 16:47:24 -040076 /* Open up the device */
Haiyang Zhang9c26aa02010-12-10 12:03:57 -080077 ret = rndis_filter_open(device_obj);
Greg Kroah-Hartman02fafbc2009-08-31 21:09:45 -070078 if (ret != 0) {
Hank Jansseneb335bc2011-03-29 13:58:48 -070079 netdev_err(net, "unable to open device (ret %d).\n",
80 ret);
Hank Janssenfceaf242009-07-13 15:34:54 -070081 return ret;
82 }
83
84 netif_start_queue(net);
Greg Kroah-Hartman02fafbc2009-08-31 21:09:45 -070085 } else {
Hank Jansseneb335bc2011-03-29 13:58:48 -070086 netdev_err(net, "unable to open device...link is down.\n");
Hank Janssenfceaf242009-07-13 15:34:54 -070087 }
88
Hank Janssenfceaf242009-07-13 15:34:54 -070089 return ret;
90}
91
Hank Janssenfceaf242009-07-13 15:34:54 -070092static int netvsc_close(struct net_device *net)
93{
Hank Janssenfceaf242009-07-13 15:34:54 -070094 struct net_device_context *net_device_ctx = netdev_priv(net);
K. Y. Srinivasan6bad88d2011-03-07 13:35:48 -080095 struct hv_device *device_obj = net_device_ctx->device_ctx;
Greg Kroah-Hartman02fafbc2009-08-31 21:09:45 -070096 int ret;
Hank Janssenfceaf242009-07-13 15:34:54 -070097
Hank Janssenfceaf242009-07-13 15:34:54 -070098 netif_stop_queue(net);
99
Haiyang Zhang9c26aa02010-12-10 12:03:57 -0800100 ret = rndis_filter_close(device_obj);
Hank Janssenfceaf242009-07-13 15:34:54 -0700101 if (ret != 0)
Hank Jansseneb335bc2011-03-29 13:58:48 -0700102 netdev_err(net, "unable to close device (ret %d).\n", ret);
Hank Janssenfceaf242009-07-13 15:34:54 -0700103
Hank Janssenfceaf242009-07-13 15:34:54 -0700104 return ret;
105}
106
Hank Janssenfceaf242009-07-13 15:34:54 -0700107static void netvsc_xmit_completion(void *context)
108{
Nicolas Palix4193d4f2009-07-29 14:10:10 +0200109 struct hv_netvsc_packet *packet = (struct hv_netvsc_packet *)context;
Greg Kroah-Hartman02fafbc2009-08-31 21:09:45 -0700110 struct sk_buff *skb = (struct sk_buff *)
Haiyang Zhang72a2f5b2010-12-10 12:03:58 -0800111 (unsigned long)packet->completion.send.send_completion_tid;
Hank Janssenfceaf242009-07-13 15:34:54 -0700112
Hank Janssenfceaf242009-07-13 15:34:54 -0700113 kfree(packet);
114
Greg Kroah-Hartman02fafbc2009-08-31 21:09:45 -0700115 if (skb) {
Stephen Hemminger7880fc52010-05-04 09:58:52 -0700116 struct net_device *net = skb->dev;
Stephen Hemmingerb220f5f2010-05-04 09:58:56 -0700117 struct net_device_context *net_device_ctx = netdev_priv(net);
118 unsigned int num_pages = skb_shinfo(skb)->nr_frags + 2;
119
Hank Janssenfceaf242009-07-13 15:34:54 -0700120 dev_kfree_skb_any(skb);
121
K. Y. Srinivasan9079ce62011-06-16 13:16:37 -0700122 atomic_add(num_pages, &net_device_ctx->avail);
123 if (atomic_read(&net_device_ctx->avail) >=
124 PACKET_PAGES_HIWATER)
Haiyang Zhange4d59ac2011-06-17 07:58:04 -0700125 netif_wake_queue(net);
Hank Janssenfceaf242009-07-13 15:34:54 -0700126 }
Hank Janssenfceaf242009-07-13 15:34:54 -0700127}
128
Greg Kroah-Hartman02fafbc2009-08-31 21:09:45 -0700129static int netvsc_start_xmit(struct sk_buff *skb, struct net_device *net)
Hank Janssenfceaf242009-07-13 15:34:54 -0700130{
Hank Janssenfceaf242009-07-13 15:34:54 -0700131 struct net_device_context *net_device_ctx = netdev_priv(net);
Nicolas Palix4193d4f2009-07-29 14:10:10 +0200132 struct hv_netvsc_packet *packet;
Greg Kroah-Hartman02fafbc2009-08-31 21:09:45 -0700133 int ret;
Stephen Hemminger60487182010-05-04 09:58:55 -0700134 unsigned int i, num_pages;
Hank Janssenfceaf242009-07-13 15:34:54 -0700135
Stephen Hemminger60487182010-05-04 09:58:55 -0700136 /* Add 1 for skb->data and additional one for RNDIS */
137 num_pages = skb_shinfo(skb)->nr_frags + 1 + 1;
K. Y. Srinivasan9079ce62011-06-16 13:16:37 -0700138 if (num_pages > atomic_read(&net_device_ctx->avail))
Stephen Hemmingerb220f5f2010-05-04 09:58:56 -0700139 return NETDEV_TX_BUSY;
Hank Janssenfceaf242009-07-13 15:34:54 -0700140
Bill Pemberton454f18a2009-07-27 16:47:24 -0400141 /* Allocate a netvsc packet based on # of frags. */
Greg Kroah-Hartman02fafbc2009-08-31 21:09:45 -0700142 packet = kzalloc(sizeof(struct hv_netvsc_packet) +
Stephen Hemminger60487182010-05-04 09:58:55 -0700143 (num_pages * sizeof(struct hv_page_buffer)) +
K. Y. Srinivasanf8ba8c72011-05-12 19:35:01 -0700144 sizeof(struct rndis_filter_packet), GFP_ATOMIC);
Greg Kroah-Hartman02fafbc2009-08-31 21:09:45 -0700145 if (!packet) {
Stephen Hemmingerb220f5f2010-05-04 09:58:56 -0700146 /* out of memory, silently drop packet */
Hank Jansseneb335bc2011-03-29 13:58:48 -0700147 netdev_err(net, "unable to allocate hv_netvsc_packet\n");
Stephen Hemmingerb220f5f2010-05-04 09:58:56 -0700148
149 dev_kfree_skb(skb);
150 net->stats.tx_dropped++;
151 return NETDEV_TX_OK;
Hank Janssenfceaf242009-07-13 15:34:54 -0700152 }
153
Haiyang Zhang72a2f5b2010-12-10 12:03:58 -0800154 packet->extension = (void *)(unsigned long)packet +
Greg Kroah-Hartman02fafbc2009-08-31 21:09:45 -0700155 sizeof(struct hv_netvsc_packet) +
Stephen Hemminger60487182010-05-04 09:58:55 -0700156 (num_pages * sizeof(struct hv_page_buffer));
Hank Janssenfceaf242009-07-13 15:34:54 -0700157
Bill Pemberton454f18a2009-07-27 16:47:24 -0400158 /* Setup the rndis header */
Haiyang Zhang72a2f5b2010-12-10 12:03:58 -0800159 packet->page_buf_cnt = num_pages;
Hank Janssenfceaf242009-07-13 15:34:54 -0700160
Bill Pemberton454f18a2009-07-27 16:47:24 -0400161 /* Initialize it from the skb */
Haiyang Zhang72a2f5b2010-12-10 12:03:58 -0800162 packet->total_data_buflen = skb->len;
Hank Janssenfceaf242009-07-13 15:34:54 -0700163
Stephen Hemminger60487182010-05-04 09:58:55 -0700164 /* Start filling in the page buffers starting after RNDIS buffer. */
Haiyang Zhangca623ad2011-01-26 12:12:11 -0800165 packet->page_buf[1].pfn = virt_to_phys(skb->data) >> PAGE_SHIFT;
166 packet->page_buf[1].offset
Stephen Hemminger60487182010-05-04 09:58:55 -0700167 = (unsigned long)skb->data & (PAGE_SIZE - 1);
Haiyang Zhangca623ad2011-01-26 12:12:11 -0800168 packet->page_buf[1].len = skb_headlen(skb);
Hank Janssenfceaf242009-07-13 15:34:54 -0700169
Stephen Hemminger60487182010-05-04 09:58:55 -0700170 /* Additional fragments are after SKB data */
171 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
172 skb_frag_t *f = &skb_shinfo(skb)->frags[i];
Hank Janssenfceaf242009-07-13 15:34:54 -0700173
Haiyang Zhangca623ad2011-01-26 12:12:11 -0800174 packet->page_buf[i+2].pfn = page_to_pfn(f->page);
175 packet->page_buf[i+2].offset = f->page_offset;
176 packet->page_buf[i+2].len = f->size;
Hank Janssenfceaf242009-07-13 15:34:54 -0700177 }
178
Bill Pemberton454f18a2009-07-27 16:47:24 -0400179 /* Set the completion routine */
Haiyang Zhang72a2f5b2010-12-10 12:03:58 -0800180 packet->completion.send.send_completion = netvsc_xmit_completion;
181 packet->completion.send.send_completion_ctx = packet;
182 packet->completion.send.send_completion_tid = (unsigned long)skb;
Hank Janssenfceaf242009-07-13 15:34:54 -0700183
K. Y. Srinivasan55acb692011-05-12 19:34:54 -0700184 ret = rndis_filter_send(net_device_ctx->device_ctx,
Greg Kroah-Hartman02fafbc2009-08-31 21:09:45 -0700185 packet);
Greg Kroah-Hartman02fafbc2009-08-31 21:09:45 -0700186 if (ret == 0) {
Stephen Hemmingerb852fdc2010-03-09 17:42:33 -0800187 net->stats.tx_bytes += skb->len;
188 net->stats.tx_packets++;
Stephen Hemmingerb220f5f2010-05-04 09:58:56 -0700189
K. Y. Srinivasan9079ce62011-06-16 13:16:37 -0700190 atomic_sub(num_pages, &net_device_ctx->avail);
191 if (atomic_read(&net_device_ctx->avail) < PACKET_PAGES_LOWATER)
Stephen Hemmingerb220f5f2010-05-04 09:58:56 -0700192 netif_stop_queue(net);
Greg Kroah-Hartman02fafbc2009-08-31 21:09:45 -0700193 } else {
Stephen Hemmingerb220f5f2010-05-04 09:58:56 -0700194 /* we are shutting down or bus overloaded, just drop packet */
Stephen Hemmingerb852fdc2010-03-09 17:42:33 -0800195 net->stats.tx_dropped++;
Stephen Hemmingerb220f5f2010-05-04 09:58:56 -0700196 netvsc_xmit_completion(packet);
Hank Janssenfceaf242009-07-13 15:34:54 -0700197 }
198
Stephen Hemmingerb220f5f2010-05-04 09:58:56 -0700199 return NETDEV_TX_OK;
Hank Janssenfceaf242009-07-13 15:34:54 -0700200}
201
Hank Janssen3e189512010-03-04 22:11:00 +0000202/*
Greg Kroah-Hartman02fafbc2009-08-31 21:09:45 -0700203 * netvsc_linkstatus_callback - Link up/down notification
204 */
K. Y. Srinivasan90ef1172011-05-12 19:34:50 -0700205void netvsc_linkstatus_callback(struct hv_device *device_obj,
Greg Kroah-Hartman02fafbc2009-08-31 21:09:45 -0700206 unsigned int status)
Hank Janssenfceaf242009-07-13 15:34:54 -0700207{
K. Y. Srinivasan6bad88d2011-03-07 13:35:48 -0800208 struct net_device *net = dev_get_drvdata(&device_obj->device);
Haiyang Zhangc996edc2011-04-06 15:18:00 -0700209 struct net_device_context *ndev_ctx;
Hank Janssenfceaf242009-07-13 15:34:54 -0700210
Greg Kroah-Hartman02fafbc2009-08-31 21:09:45 -0700211 if (!net) {
Hank Jansseneb335bc2011-03-29 13:58:48 -0700212 netdev_err(net, "got link status but net device "
213 "not initialized yet\n");
Hank Janssenfceaf242009-07-13 15:34:54 -0700214 return;
215 }
216
Greg Kroah-Hartman02fafbc2009-08-31 21:09:45 -0700217 if (status == 1) {
Hank Janssenfceaf242009-07-13 15:34:54 -0700218 netif_carrier_on(net);
219 netif_wake_queue(net);
Haiyang Zhang7c161d02011-02-02 13:42:58 -0800220 netif_notify_peers(net);
Haiyang Zhangc996edc2011-04-06 15:18:00 -0700221 ndev_ctx = netdev_priv(net);
Haiyang Zhang122a5f62011-05-27 06:21:55 -0700222 schedule_delayed_work(&ndev_ctx->dwork, msecs_to_jiffies(20));
Greg Kroah-Hartman02fafbc2009-08-31 21:09:45 -0700223 } else {
Hank Janssenfceaf242009-07-13 15:34:54 -0700224 netif_carrier_off(net);
225 netif_stop_queue(net);
226 }
Hank Janssenfceaf242009-07-13 15:34:54 -0700227}
228
Hank Janssen3e189512010-03-04 22:11:00 +0000229/*
230 * netvsc_recv_callback - Callback when we receive a packet from the
231 * "wire" on the specified device.
Greg Kroah-Hartman02fafbc2009-08-31 21:09:45 -0700232 */
K. Y. Srinivasanf79adf82011-05-12 19:34:51 -0700233int netvsc_recv_callback(struct hv_device *device_obj,
Greg Kroah-Hartman02fafbc2009-08-31 21:09:45 -0700234 struct hv_netvsc_packet *packet)
Hank Janssenfceaf242009-07-13 15:34:54 -0700235{
K. Y. Srinivasan6bad88d2011-03-07 13:35:48 -0800236 struct net_device *net = dev_get_drvdata(&device_obj->device);
Hank Janssenfceaf242009-07-13 15:34:54 -0700237 struct sk_buff *skb;
238 void *data;
Greg Kroah-Hartman02fafbc2009-08-31 21:09:45 -0700239 int i;
Hank Janssenfceaf242009-07-13 15:34:54 -0700240 unsigned long flags;
241
Greg Kroah-Hartman02fafbc2009-08-31 21:09:45 -0700242 if (!net) {
Hank Jansseneb335bc2011-03-29 13:58:48 -0700243 netdev_err(net, "got receive callback but net device"
244 " not initialized yet\n");
Hank Janssenfceaf242009-07-13 15:34:54 -0700245 return 0;
246 }
247
Stephen Hemminger9495c282010-03-09 17:42:17 -0800248 /* Allocate a skb - TODO direct I/O to pages? */
Haiyang Zhang72a2f5b2010-12-10 12:03:58 -0800249 skb = netdev_alloc_skb_ip_align(net, packet->total_data_buflen);
Stephen Hemminger9495c282010-03-09 17:42:17 -0800250 if (unlikely(!skb)) {
251 ++net->stats.rx_dropped;
252 return 0;
253 }
Hank Janssenfceaf242009-07-13 15:34:54 -0700254
Bill Pemberton454f18a2009-07-27 16:47:24 -0400255 /* for kmap_atomic */
Hank Janssenfceaf242009-07-13 15:34:54 -0700256 local_irq_save(flags);
257
Greg Kroah-Hartman02fafbc2009-08-31 21:09:45 -0700258 /*
259 * Copy to skb. This copy is needed here since the memory pointed by
260 * hv_netvsc_packet cannot be deallocated
261 */
Haiyang Zhang72a2f5b2010-12-10 12:03:58 -0800262 for (i = 0; i < packet->page_buf_cnt; i++) {
Haiyang Zhangca623ad2011-01-26 12:12:11 -0800263 data = kmap_atomic(pfn_to_page(packet->page_buf[i].pfn),
Greg Kroah-Hartman02fafbc2009-08-31 21:09:45 -0700264 KM_IRQ1);
265 data = (void *)(unsigned long)data +
Haiyang Zhangca623ad2011-01-26 12:12:11 -0800266 packet->page_buf[i].offset;
Hank Janssenfceaf242009-07-13 15:34:54 -0700267
Haiyang Zhangca623ad2011-01-26 12:12:11 -0800268 memcpy(skb_put(skb, packet->page_buf[i].len), data,
269 packet->page_buf[i].len);
Hank Janssenfceaf242009-07-13 15:34:54 -0700270
Greg Kroah-Hartman02fafbc2009-08-31 21:09:45 -0700271 kunmap_atomic((void *)((unsigned long)data -
Haiyang Zhangca623ad2011-01-26 12:12:11 -0800272 packet->page_buf[i].offset), KM_IRQ1);
Hank Janssenfceaf242009-07-13 15:34:54 -0700273 }
274
275 local_irq_restore(flags);
276
277 skb->protocol = eth_type_trans(skb, net);
Hank Janssenfceaf242009-07-13 15:34:54 -0700278 skb->ip_summed = CHECKSUM_NONE;
279
Stephen Hemminger9495c282010-03-09 17:42:17 -0800280 net->stats.rx_packets++;
281 net->stats.rx_bytes += skb->len;
282
Greg Kroah-Hartman02fafbc2009-08-31 21:09:45 -0700283 /*
284 * Pass the skb back up. Network stack will deallocate the skb when it
Stephen Hemminger9495c282010-03-09 17:42:17 -0800285 * is done.
286 * TODO - use NAPI?
Greg Kroah-Hartman02fafbc2009-08-31 21:09:45 -0700287 */
Stephen Hemminger9495c282010-03-09 17:42:17 -0800288 netif_rx(skb);
Hank Janssenfceaf242009-07-13 15:34:54 -0700289
Hank Janssenfceaf242009-07-13 15:34:54 -0700290 return 0;
291}
292
Stephen Hemmingerf82f4ad2010-05-04 09:58:57 -0700293static void netvsc_get_drvinfo(struct net_device *net,
294 struct ethtool_drvinfo *info)
295{
296 strcpy(info->driver, "hv_netvsc");
297 strcpy(info->version, HV_DRV_VERSION);
298 strcpy(info->fw_version, "N/A");
299}
300
301static const struct ethtool_ops ethtool_ops = {
302 .get_drvinfo = netvsc_get_drvinfo,
Stephen Hemmingerf82f4ad2010-05-04 09:58:57 -0700303 .get_link = ethtool_op_get_link,
304};
305
Greg Kroah-Hartmandf2fff22009-08-31 21:11:12 -0700306static const struct net_device_ops device_ops = {
307 .ndo_open = netvsc_open,
308 .ndo_stop = netvsc_close,
309 .ndo_start_xmit = netvsc_start_xmit,
Jiri Pirkoafc4b132011-08-16 06:29:01 +0000310 .ndo_set_rx_mode = netvsc_set_multicast_list,
Haiyang Zhangb681b582010-08-03 19:15:31 +0000311 .ndo_change_mtu = eth_change_mtu,
312 .ndo_validate_addr = eth_validate_addr,
313 .ndo_set_mac_address = eth_mac_addr,
Greg Kroah-Hartmandf2fff22009-08-31 21:11:12 -0700314};
315
Haiyang Zhangc996edc2011-04-06 15:18:00 -0700316/*
317 * Send GARP packet to network peers after migrations.
318 * After Quick Migration, the network is not immediately operational in the
319 * current context when receiving RNDIS_STATUS_MEDIA_CONNECT event. So, add
Haiyang Zhang122a5f62011-05-27 06:21:55 -0700320 * another netif_notify_peers() into a delayed work, otherwise GARP packet
Haiyang Zhangc996edc2011-04-06 15:18:00 -0700321 * will not be sent after quick migration, and cause network disconnection.
322 */
323static void netvsc_send_garp(struct work_struct *w)
324{
325 struct net_device_context *ndev_ctx;
326 struct net_device *net;
327
Haiyang Zhang122a5f62011-05-27 06:21:55 -0700328 ndev_ctx = container_of(w, struct net_device_context, dwork.work);
Haiyang Zhangc996edc2011-04-06 15:18:00 -0700329 net = dev_get_drvdata(&ndev_ctx->device_ctx->device);
330 netif_notify_peers(net);
331}
332
333
K. Y. Srinivasan9efd21e2011-04-29 13:45:10 -0700334static int netvsc_probe(struct hv_device *dev)
Greg Kroah-Hartmandf2fff22009-08-31 21:11:12 -0700335{
Greg Kroah-Hartmandf2fff22009-08-31 21:11:12 -0700336 struct net_device *net = NULL;
337 struct net_device_context *net_device_ctx;
338 struct netvsc_device_info device_info;
339 int ret;
340
Stephen Hemminger546d9e12010-03-11 09:11:37 -0800341 net = alloc_etherdev(sizeof(struct net_device_context));
Greg Kroah-Hartmandf2fff22009-08-31 21:11:12 -0700342 if (!net)
343 return -1;
344
345 /* Set initial state */
346 netif_carrier_off(net);
Greg Kroah-Hartmandf2fff22009-08-31 21:11:12 -0700347
348 net_device_ctx = netdev_priv(net);
K. Y. Srinivasan9efd21e2011-04-29 13:45:10 -0700349 net_device_ctx->device_ctx = dev;
K. Y. Srinivasan9079ce62011-06-16 13:16:37 -0700350 atomic_set(&net_device_ctx->avail, ring_size);
K. Y. Srinivasan9efd21e2011-04-29 13:45:10 -0700351 dev_set_drvdata(&dev->device, net);
Haiyang Zhang122a5f62011-05-27 06:21:55 -0700352 INIT_DELAYED_WORK(&net_device_ctx->dwork, netvsc_send_garp);
Greg Kroah-Hartmandf2fff22009-08-31 21:11:12 -0700353
354 /* Notify the netvsc driver of the new device */
K. Y. Srinivasanaae23982011-05-12 19:35:05 -0700355 device_info.ring_size = ring_size;
Haiyang Zhangbdbad572011-05-23 09:03:49 -0700356 ret = rndis_filter_device_add(dev, &device_info);
Greg Kroah-Hartmandf2fff22009-08-31 21:11:12 -0700357 if (ret != 0) {
358 free_netdev(net);
K. Y. Srinivasan9efd21e2011-04-29 13:45:10 -0700359 dev_set_drvdata(&dev->device, NULL);
Greg Kroah-Hartmandf2fff22009-08-31 21:11:12 -0700360
Hank Jansseneb335bc2011-03-29 13:58:48 -0700361 netdev_err(net, "unable to add netvsc device (ret %d)\n", ret);
Greg Kroah-Hartmandf2fff22009-08-31 21:11:12 -0700362 return ret;
363 }
364
Haiyang Zhangb1956a82011-05-23 09:03:45 -0700365 netif_carrier_on(net);
Greg Kroah-Hartmandf2fff22009-08-31 21:11:12 -0700366
Haiyang Zhang72a2f5b2010-12-10 12:03:58 -0800367 memcpy(net->dev_addr, device_info.mac_adr, ETH_ALEN);
Greg Kroah-Hartmandf2fff22009-08-31 21:11:12 -0700368
369 net->netdev_ops = &device_ops;
370
Stephen Hemminger60487182010-05-04 09:58:55 -0700371 /* TODO: Add GSO and Checksum offload */
Michał Mirosław877a3442011-04-19 12:43:20 +0200372 net->hw_features = NETIF_F_SG;
Stephen Hemminger60487182010-05-04 09:58:55 -0700373 net->features = NETIF_F_SG;
374
Stephen Hemmingerf82f4ad2010-05-04 09:58:57 -0700375 SET_ETHTOOL_OPS(net, &ethtool_ops);
K. Y. Srinivasan9efd21e2011-04-29 13:45:10 -0700376 SET_NETDEV_DEV(net, &dev->device);
Greg Kroah-Hartmandf2fff22009-08-31 21:11:12 -0700377
378 ret = register_netdev(net);
379 if (ret != 0) {
380 /* Remove the device and release the resource */
K. Y. Srinivasan58de3fc2011-05-10 07:55:01 -0700381 rndis_filter_device_remove(dev);
Greg Kroah-Hartmandf2fff22009-08-31 21:11:12 -0700382 free_netdev(net);
383 }
384
Greg Kroah-Hartmandf2fff22009-08-31 21:11:12 -0700385 return ret;
386}
387
K. Y. Srinivasan415b0232011-04-29 13:45:12 -0700388static int netvsc_remove(struct hv_device *dev)
Greg Kroah-Hartmandf2fff22009-08-31 21:11:12 -0700389{
K. Y. Srinivasan415b0232011-04-29 13:45:12 -0700390 struct net_device *net = dev_get_drvdata(&dev->device);
Haiyang Zhang122a5f62011-05-27 06:21:55 -0700391 struct net_device_context *ndev_ctx;
Greg Kroah-Hartmandf2fff22009-08-31 21:11:12 -0700392
Greg Kroah-Hartmandf2fff22009-08-31 21:11:12 -0700393 if (net == NULL) {
K. Y. Srinivasan415b0232011-04-29 13:45:12 -0700394 dev_err(&dev->device, "No net device to remove\n");
Greg Kroah-Hartmandf2fff22009-08-31 21:11:12 -0700395 return 0;
396 }
397
Haiyang Zhang122a5f62011-05-27 06:21:55 -0700398 ndev_ctx = netdev_priv(net);
399 cancel_delayed_work_sync(&ndev_ctx->dwork);
400
Greg Kroah-Hartmandf2fff22009-08-31 21:11:12 -0700401 /* Stop outbound asap */
402 netif_stop_queue(net);
Greg Kroah-Hartmandf2fff22009-08-31 21:11:12 -0700403
404 unregister_netdev(net);
405
406 /*
407 * Call to the vsc driver to let it know that the device is being
408 * removed
409 */
Haiyang Zhangdf06bcf2011-05-23 09:03:47 -0700410 rndis_filter_device_remove(dev);
Greg Kroah-Hartmandf2fff22009-08-31 21:11:12 -0700411
412 free_netdev(net);
Haiyang Zhangdf06bcf2011-05-23 09:03:47 -0700413 return 0;
Greg Kroah-Hartmandf2fff22009-08-31 21:11:12 -0700414}
415
K. Y. Srinivasanf1542a62011-05-10 07:55:16 -0700416/* The one and only one */
K. Y. Srinivasanfde0ef92011-05-12 19:35:08 -0700417static struct hv_driver netvsc_drv = {
418 .probe = netvsc_probe,
419 .remove = netvsc_remove,
K. Y. Srinivasand4890972011-05-10 07:55:17 -0700420};
K. Y. Srinivasanf1542a62011-05-10 07:55:16 -0700421
K. Y. Srinivasana9869c92011-05-12 19:35:17 -0700422static void __exit netvsc_drv_exit(void)
Hank Janssenfceaf242009-07-13 15:34:54 -0700423{
K. Y. Srinivasanfde0ef92011-05-12 19:35:08 -0700424 vmbus_child_driver_unregister(&netvsc_drv.driver);
Hank Janssenfceaf242009-07-13 15:34:54 -0700425}
426
K. Y. Srinivasan4753ff62011-05-12 19:35:13 -0700427
428static const struct dmi_system_id __initconst
429hv_netvsc_dmi_table[] __maybe_unused = {
430 {
431 .ident = "Hyper-V",
432 .matches = {
433 DMI_MATCH(DMI_SYS_VENDOR, "Microsoft Corporation"),
434 DMI_MATCH(DMI_PRODUCT_NAME, "Virtual Machine"),
435 DMI_MATCH(DMI_BOARD_NAME, "Virtual Machine"),
436 },
437 },
438 { },
439};
440MODULE_DEVICE_TABLE(dmi, hv_netvsc_dmi_table);
441
K. Y. Srinivasan1fde28c2011-05-12 19:35:16 -0700442static int __init netvsc_drv_init(void)
Greg Kroah-Hartmandf2fff22009-08-31 21:11:12 -0700443{
K. Y. Srinivasanfde0ef92011-05-12 19:35:08 -0700444 struct hv_driver *drv = &netvsc_drv;
Greg Kroah-Hartmandf2fff22009-08-31 21:11:12 -0700445 int ret;
446
K. Y. Srinivasanbb546d02011-05-12 19:35:15 -0700447 pr_info("initializing....");
448
449 if (!dmi_check_system(hv_netvsc_dmi_table))
450 return -ENODEV;
451
452
Greg Kroah-Hartmandf2fff22009-08-31 21:11:12 -0700453 /* Callback to client driver to complete the initialization */
K. Y. Srinivasan073aad32011-05-12 19:35:14 -0700454 netvsc_initialize(drv);
Greg Kroah-Hartmandf2fff22009-08-31 21:11:12 -0700455
K. Y. Srinivasanfde0ef92011-05-12 19:35:08 -0700456 drv->driver.name = drv->name;
Greg Kroah-Hartmandf2fff22009-08-31 21:11:12 -0700457
Greg Kroah-Hartmandf2fff22009-08-31 21:11:12 -0700458 /* The driver belongs to vmbus */
K. Y. Srinivasan150f9392011-03-07 13:32:31 -0800459 ret = vmbus_child_driver_register(&drv->driver);
Greg Kroah-Hartmandf2fff22009-08-31 21:11:12 -0700460
Greg Kroah-Hartmandf2fff22009-08-31 21:11:12 -0700461 return ret;
462}
463
K. Y. Srinivasan06e719d2010-05-07 17:52:17 -0600464static const struct pci_device_id __initconst
465hv_netvsc_pci_table[] __maybe_unused = {
466 { PCI_DEVICE(0x1414, 0x5353) }, /* VGA compatible controller */
467 { 0 }
468};
469MODULE_DEVICE_TABLE(pci, hv_netvsc_pci_table);
470
Hank Janssen26c14cc2010-02-11 23:02:42 +0000471MODULE_LICENSE("GPL");
472MODULE_VERSION(HV_DRV_VERSION);
Stephen Hemminger7880fc52010-05-04 09:58:52 -0700473MODULE_DESCRIPTION("Microsoft Hyper-V network driver");
Hank Janssenfceaf242009-07-13 15:34:54 -0700474
K. Y. Srinivasan1fde28c2011-05-12 19:35:16 -0700475module_init(netvsc_drv_init);
K. Y. Srinivasana9869c92011-05-12 19:35:17 -0700476module_exit(netvsc_drv_exit);