blob: 1f7b12f9e6fbf2b83c4323939d876e5f38d3d7a6 [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
Jeff Kirsheradf8d3f2013-12-06 06:28:47 -080014 * this program; if not, see <http://www.gnu.org/licenses/>.
Hank Janssenfceaf242009-07-13 15:34:54 -070015 *
16 * Authors:
Haiyang Zhangd0e94d12009-11-23 17:00:22 +000017 * Haiyang Zhang <haiyangz@microsoft.com>
Hank Janssenfceaf242009-07-13 15:34:54 -070018 * Hank Janssen <hjanssen@microsoft.com>
Hank Janssenfceaf242009-07-13 15:34:54 -070019 */
Hank Jansseneb335bc2011-03-29 13:58:48 -070020#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
21
Hank Janssenfceaf242009-07-13 15:34:54 -070022#include <linux/init.h>
K. Y. Srinivasan9079ce62011-06-16 13:16:37 -070023#include <linux/atomic.h>
Hank Janssenfceaf242009-07-13 15:34:54 -070024#include <linux/module.h>
25#include <linux/highmem.h>
26#include <linux/device.h>
Hank Janssenfceaf242009-07-13 15:34:54 -070027#include <linux/io.h>
Hank Janssenfceaf242009-07-13 15:34:54 -070028#include <linux/delay.h>
29#include <linux/netdevice.h>
30#include <linux/inetdevice.h>
31#include <linux/etherdevice.h>
32#include <linux/skbuff.h>
Haiyang Zhangc802db12013-05-28 06:15:56 +000033#include <linux/if_vlan.h>
Hank Janssenfceaf242009-07-13 15:34:54 -070034#include <linux/in.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090035#include <linux/slab.h>
Hank Janssenfceaf242009-07-13 15:34:54 -070036#include <net/arp.h>
37#include <net/route.h>
38#include <net/sock.h>
39#include <net/pkt_sched.h>
K. Y. Srinivasan3f335ea2011-05-12 19:34:15 -070040
K. Y. Srinivasan5ca72522011-05-12 19:34:37 -070041#include "hyperv_net.h"
Hank Janssenfceaf242009-07-13 15:34:54 -070042
Hank Janssenfceaf242009-07-13 15:34:54 -070043struct net_device_context {
Greg Kroah-Hartman02fafbc2009-08-31 21:09:45 -070044 /* point back to our device context */
K. Y. Srinivasan6bad88d2011-03-07 13:35:48 -080045 struct hv_device *device_ctx;
Haiyang Zhang122a5f62011-05-27 06:21:55 -070046 struct delayed_work dwork;
Wenqi Ma792df872012-04-19 00:39:37 +000047 struct work_struct work;
Hank Janssenfceaf242009-07-13 15:34:54 -070048};
49
Haiyang Zhangfa85a6c2012-07-25 08:08:41 +000050#define RING_SIZE_MIN 64
Hank Janssen99c8da02010-10-12 10:45:23 -070051static int ring_size = 128;
Stephen Hemminger450d7a42010-05-04 09:58:53 -070052module_param(ring_size, int, S_IRUGO);
53MODULE_PARM_DESC(ring_size, "Ring buffer size (# of pages)");
Hank Janssenfceaf242009-07-13 15:34:54 -070054
Haiyang Zhangd426b2e2011-11-30 07:19:08 -080055static void do_set_multicast(struct work_struct *w)
56{
Wenqi Ma792df872012-04-19 00:39:37 +000057 struct net_device_context *ndevctx =
58 container_of(w, struct net_device_context, work);
Haiyang Zhangd426b2e2011-11-30 07:19:08 -080059 struct netvsc_device *nvdev;
60 struct rndis_device *rdev;
61
62 nvdev = hv_get_drvdata(ndevctx->device_ctx);
Wenqi Ma792df872012-04-19 00:39:37 +000063 if (nvdev == NULL || nvdev->ndev == NULL)
64 return;
Haiyang Zhangd426b2e2011-11-30 07:19:08 -080065
66 rdev = nvdev->extension;
67 if (rdev == NULL)
Wenqi Ma792df872012-04-19 00:39:37 +000068 return;
Haiyang Zhangd426b2e2011-11-30 07:19:08 -080069
Wenqi Ma792df872012-04-19 00:39:37 +000070 if (nvdev->ndev->flags & IFF_PROMISC)
Haiyang Zhangd426b2e2011-11-30 07:19:08 -080071 rndis_filter_set_packet_filter(rdev,
72 NDIS_PACKET_TYPE_PROMISCUOUS);
73 else
74 rndis_filter_set_packet_filter(rdev,
75 NDIS_PACKET_TYPE_BROADCAST |
76 NDIS_PACKET_TYPE_ALL_MULTICAST |
77 NDIS_PACKET_TYPE_DIRECTED);
Haiyang Zhangd426b2e2011-11-30 07:19:08 -080078}
79
Greg Kroah-Hartman4e9bfef2009-07-15 12:45:20 -070080static void netvsc_set_multicast_list(struct net_device *net)
Hank Janssenfceaf242009-07-13 15:34:54 -070081{
Wenqi Ma792df872012-04-19 00:39:37 +000082 struct net_device_context *net_device_ctx = netdev_priv(net);
Haiyang Zhangd426b2e2011-11-30 07:19:08 -080083
Wenqi Ma792df872012-04-19 00:39:37 +000084 schedule_work(&net_device_ctx->work);
Hank Janssenfceaf242009-07-13 15:34:54 -070085}
86
Hank Janssenfceaf242009-07-13 15:34:54 -070087static int netvsc_open(struct net_device *net)
88{
Hank Janssenfceaf242009-07-13 15:34:54 -070089 struct net_device_context *net_device_ctx = netdev_priv(net);
K. Y. Srinivasan6bad88d2011-03-07 13:35:48 -080090 struct hv_device *device_obj = net_device_ctx->device_ctx;
Haiyang Zhang891de742014-02-12 16:54:27 -080091 struct netvsc_device *nvdev;
92 struct rndis_device *rdev;
Greg Kroah-Hartman02fafbc2009-08-31 21:09:45 -070093 int ret = 0;
Hank Janssenfceaf242009-07-13 15:34:54 -070094
Haiyang Zhang891de742014-02-12 16:54:27 -080095 netif_carrier_off(net);
96
Haiyang Zhangd515d0f2011-09-28 13:24:15 -070097 /* Open up the device */
98 ret = rndis_filter_open(device_obj);
99 if (ret != 0) {
100 netdev_err(net, "unable to open device (ret %d).\n", ret);
101 return ret;
Hank Janssenfceaf242009-07-13 15:34:54 -0700102 }
103
Haiyang Zhangd515d0f2011-09-28 13:24:15 -0700104 netif_start_queue(net);
105
Haiyang Zhang891de742014-02-12 16:54:27 -0800106 nvdev = hv_get_drvdata(device_obj);
107 rdev = nvdev->extension;
108 if (!rdev->link_state)
109 netif_carrier_on(net);
110
Hank Janssenfceaf242009-07-13 15:34:54 -0700111 return ret;
112}
113
Hank Janssenfceaf242009-07-13 15:34:54 -0700114static int netvsc_close(struct net_device *net)
115{
Hank Janssenfceaf242009-07-13 15:34:54 -0700116 struct net_device_context *net_device_ctx = netdev_priv(net);
K. Y. Srinivasan6bad88d2011-03-07 13:35:48 -0800117 struct hv_device *device_obj = net_device_ctx->device_ctx;
Greg Kroah-Hartman02fafbc2009-08-31 21:09:45 -0700118 int ret;
Hank Janssenfceaf242009-07-13 15:34:54 -0700119
Haiyang Zhang0a282532012-02-02 07:17:59 +0000120 netif_tx_disable(net);
Hank Janssenfceaf242009-07-13 15:34:54 -0700121
Wenqi Ma792df872012-04-19 00:39:37 +0000122 /* Make sure netvsc_set_multicast_list doesn't re-enable filter! */
123 cancel_work_sync(&net_device_ctx->work);
Haiyang Zhang9c26aa02010-12-10 12:03:57 -0800124 ret = rndis_filter_close(device_obj);
Hank Janssenfceaf242009-07-13 15:34:54 -0700125 if (ret != 0)
Hank Jansseneb335bc2011-03-29 13:58:48 -0700126 netdev_err(net, "unable to close device (ret %d).\n", ret);
Hank Janssenfceaf242009-07-13 15:34:54 -0700127
Hank Janssenfceaf242009-07-13 15:34:54 -0700128 return ret;
129}
130
Hank Janssenfceaf242009-07-13 15:34:54 -0700131static void netvsc_xmit_completion(void *context)
132{
Nicolas Palix4193d4f2009-07-29 14:10:10 +0200133 struct hv_netvsc_packet *packet = (struct hv_netvsc_packet *)context;
Greg Kroah-Hartman02fafbc2009-08-31 21:09:45 -0700134 struct sk_buff *skb = (struct sk_buff *)
Haiyang Zhang72a2f5b2010-12-10 12:03:58 -0800135 (unsigned long)packet->completion.send.send_completion_tid;
Hank Janssenfceaf242009-07-13 15:34:54 -0700136
Hank Janssenfceaf242009-07-13 15:34:54 -0700137 kfree(packet);
138
Haiyang Zhang1d068252011-12-02 11:56:25 -0800139 if (skb)
Hank Janssenfceaf242009-07-13 15:34:54 -0700140 dev_kfree_skb_any(skb);
Hank Janssenfceaf242009-07-13 15:34:54 -0700141}
142
KY Srinivasan54a73572014-03-08 19:23:13 -0800143static u32 fill_pg_buf(struct page *page, u32 offset, u32 len,
144 struct hv_page_buffer *pb)
145{
146 int j = 0;
147
148 /* Deal with compund pages by ignoring unused part
149 * of the page.
150 */
151 page += (offset >> PAGE_SHIFT);
152 offset &= ~PAGE_MASK;
153
154 while (len > 0) {
155 unsigned long bytes;
156
157 bytes = PAGE_SIZE - offset;
158 if (bytes > len)
159 bytes = len;
160 pb[j].pfn = page_to_pfn(page);
161 pb[j].offset = offset;
162 pb[j].len = bytes;
163
164 offset += bytes;
165 len -= bytes;
166
167 if (offset == PAGE_SIZE && len) {
168 page++;
169 offset = 0;
170 j++;
171 }
172 }
173
174 return j + 1;
175}
176
177static void init_page_array(void *hdr, u32 len, struct sk_buff *skb,
178 struct hv_page_buffer *pb)
179{
180 u32 slots_used = 0;
181 char *data = skb->data;
182 int frags = skb_shinfo(skb)->nr_frags;
183 int i;
184
185 /* The packet is laid out thus:
186 * 1. hdr
187 * 2. skb linear data
188 * 3. skb fragment data
189 */
190 if (hdr != NULL)
191 slots_used += fill_pg_buf(virt_to_page(hdr),
192 offset_in_page(hdr),
193 len, &pb[slots_used]);
194
195 slots_used += fill_pg_buf(virt_to_page(data),
196 offset_in_page(data),
197 skb_headlen(skb), &pb[slots_used]);
198
199 for (i = 0; i < frags; i++) {
200 skb_frag_t *frag = skb_shinfo(skb)->frags + i;
201
202 slots_used += fill_pg_buf(skb_frag_page(frag),
203 frag->page_offset,
204 skb_frag_size(frag), &pb[slots_used]);
205 }
206}
207
208static int count_skb_frag_slots(struct sk_buff *skb)
209{
210 int i, frags = skb_shinfo(skb)->nr_frags;
211 int pages = 0;
212
213 for (i = 0; i < frags; i++) {
214 skb_frag_t *frag = skb_shinfo(skb)->frags + i;
215 unsigned long size = skb_frag_size(frag);
216 unsigned long offset = frag->page_offset;
217
218 /* Skip unused frames from start of page */
219 offset &= ~PAGE_MASK;
220 pages += PFN_UP(offset + size);
221 }
222 return pages;
223}
224
225static int netvsc_get_slots(struct sk_buff *skb)
226{
227 char *data = skb->data;
228 unsigned int offset = offset_in_page(data);
229 unsigned int len = skb_headlen(skb);
230 int slots;
231 int frag_slots;
232
233 slots = DIV_ROUND_UP(offset + len, PAGE_SIZE);
234 frag_slots = count_skb_frag_slots(skb);
235 return slots + frag_slots;
236}
237
Greg Kroah-Hartman02fafbc2009-08-31 21:09:45 -0700238static int netvsc_start_xmit(struct sk_buff *skb, struct net_device *net)
Hank Janssenfceaf242009-07-13 15:34:54 -0700239{
Hank Janssenfceaf242009-07-13 15:34:54 -0700240 struct net_device_context *net_device_ctx = netdev_priv(net);
Nicolas Palix4193d4f2009-07-29 14:10:10 +0200241 struct hv_netvsc_packet *packet;
Greg Kroah-Hartman02fafbc2009-08-31 21:09:45 -0700242 int ret;
KY Srinivasan54a73572014-03-08 19:23:13 -0800243 unsigned int num_data_pages;
Hank Janssenfceaf242009-07-13 15:34:54 -0700244
KY Srinivasan54a73572014-03-08 19:23:13 -0800245 /* We will atmost need two pages to describe the rndis
246 * header. We can only transmit MAX_PAGE_BUFFER_COUNT number
247 * of pages in a single packet.
248 */
249 num_data_pages = netvsc_get_slots(skb) + 2;
250 if (num_data_pages > MAX_PAGE_BUFFER_COUNT) {
251 netdev_err(net, "Packet too big: %u\n", skb->len);
252 dev_kfree_skb(skb);
253 net->stats.tx_dropped++;
254 return NETDEV_TX_OK;
255 }
Hank Janssenfceaf242009-07-13 15:34:54 -0700256
Bill Pemberton454f18a2009-07-27 16:47:24 -0400257 /* Allocate a netvsc packet based on # of frags. */
Greg Kroah-Hartman02fafbc2009-08-31 21:09:45 -0700258 packet = kzalloc(sizeof(struct hv_netvsc_packet) +
KY Srinivasan54a73572014-03-08 19:23:13 -0800259 (num_data_pages * sizeof(struct hv_page_buffer)) +
KY Srinivasan86eedac2014-02-16 16:38:43 -0800260 sizeof(struct rndis_message) +
Haiyang Zhang1f5f3a72012-03-12 10:20:50 +0000261 NDIS_VLAN_PPI_SIZE, GFP_ATOMIC);
Greg Kroah-Hartman02fafbc2009-08-31 21:09:45 -0700262 if (!packet) {
Haiyang Zhangbf769372011-09-01 12:19:46 -0700263 /* out of memory, drop packet */
Hank Jansseneb335bc2011-03-29 13:58:48 -0700264 netdev_err(net, "unable to allocate hv_netvsc_packet\n");
Stephen Hemmingerb220f5f2010-05-04 09:58:56 -0700265
266 dev_kfree_skb(skb);
267 net->stats.tx_dropped++;
Eric Dumazetbb6d5e72012-03-14 08:53:34 +0000268 return NETDEV_TX_OK;
Hank Janssenfceaf242009-07-13 15:34:54 -0700269 }
270
Haiyang Zhang1f5f3a72012-03-12 10:20:50 +0000271 packet->vlan_tci = skb->vlan_tci;
272
Haiyang Zhang72a2f5b2010-12-10 12:03:58 -0800273 packet->extension = (void *)(unsigned long)packet +
KY Srinivasan54a73572014-03-08 19:23:13 -0800274 sizeof(struct hv_netvsc_packet) +
275 (num_data_pages * sizeof(struct hv_page_buffer));
Hank Janssenfceaf242009-07-13 15:34:54 -0700276
Haiyang Zhangc31c1512012-02-02 07:18:00 +0000277 /* If the rndis msg goes beyond 1 page, we will add 1 later */
KY Srinivasan54a73572014-03-08 19:23:13 -0800278 packet->page_buf_cnt = num_data_pages - 1;
Hank Janssenfceaf242009-07-13 15:34:54 -0700279
Bill Pemberton454f18a2009-07-27 16:47:24 -0400280 /* Initialize it from the skb */
Haiyang Zhang4d447c92011-12-15 13:45:17 -0800281 packet->total_data_buflen = skb->len;
Hank Janssenfceaf242009-07-13 15:34:54 -0700282
Stephen Hemminger60487182010-05-04 09:58:55 -0700283 /* Start filling in the page buffers starting after RNDIS buffer. */
KY Srinivasan54a73572014-03-08 19:23:13 -0800284 init_page_array(NULL, 0, skb, &packet->page_buf[1]);
Hank Janssenfceaf242009-07-13 15:34:54 -0700285
Bill Pemberton454f18a2009-07-27 16:47:24 -0400286 /* Set the completion routine */
Haiyang Zhang72a2f5b2010-12-10 12:03:58 -0800287 packet->completion.send.send_completion = netvsc_xmit_completion;
288 packet->completion.send.send_completion_ctx = packet;
289 packet->completion.send.send_completion_tid = (unsigned long)skb;
Hank Janssenfceaf242009-07-13 15:34:54 -0700290
K. Y. Srinivasan55acb692011-05-12 19:34:54 -0700291 ret = rndis_filter_send(net_device_ctx->device_ctx,
Greg Kroah-Hartman02fafbc2009-08-31 21:09:45 -0700292 packet);
Greg Kroah-Hartman02fafbc2009-08-31 21:09:45 -0700293 if (ret == 0) {
Stephen Hemmingerb852fdc2010-03-09 17:42:33 -0800294 net->stats.tx_bytes += skb->len;
295 net->stats.tx_packets++;
Greg Kroah-Hartman02fafbc2009-08-31 21:09:45 -0700296 } else {
Haiyang Zhang8a5f9ed2011-09-01 12:19:45 -0700297 kfree(packet);
Haiyang Zhang33be96e2012-03-27 13:20:45 +0000298 if (ret != -EAGAIN) {
299 dev_kfree_skb_any(skb);
300 net->stats.tx_dropped++;
301 }
Hank Janssenfceaf242009-07-13 15:34:54 -0700302 }
303
Haiyang Zhang33be96e2012-03-27 13:20:45 +0000304 return (ret == -EAGAIN) ? NETDEV_TX_BUSY : NETDEV_TX_OK;
Hank Janssenfceaf242009-07-13 15:34:54 -0700305}
306
Hank Janssen3e189512010-03-04 22:11:00 +0000307/*
Greg Kroah-Hartman02fafbc2009-08-31 21:09:45 -0700308 * netvsc_linkstatus_callback - Link up/down notification
309 */
K. Y. Srinivasan90ef1172011-05-12 19:34:50 -0700310void netvsc_linkstatus_callback(struct hv_device *device_obj,
Greg Kroah-Hartman02fafbc2009-08-31 21:09:45 -0700311 unsigned int status)
Hank Janssenfceaf242009-07-13 15:34:54 -0700312{
K. Y. Srinivasan2ddd5e52011-09-13 10:59:49 -0700313 struct net_device *net;
Haiyang Zhangc996edc2011-04-06 15:18:00 -0700314 struct net_device_context *ndev_ctx;
K. Y. Srinivasan2ddd5e52011-09-13 10:59:49 -0700315 struct netvsc_device *net_device;
Haiyang Zhang891de742014-02-12 16:54:27 -0800316 struct rndis_device *rdev;
K. Y. Srinivasan2ddd5e52011-09-13 10:59:49 -0700317
318 net_device = hv_get_drvdata(device_obj);
Haiyang Zhang891de742014-02-12 16:54:27 -0800319 rdev = net_device->extension;
320
321 rdev->link_state = status != 1;
322
K. Y. Srinivasan2ddd5e52011-09-13 10:59:49 -0700323 net = net_device->ndev;
Hank Janssenfceaf242009-07-13 15:34:54 -0700324
Haiyang Zhang891de742014-02-12 16:54:27 -0800325 if (!net || net->reg_state != NETREG_REGISTERED)
Hank Janssenfceaf242009-07-13 15:34:54 -0700326 return;
Hank Janssenfceaf242009-07-13 15:34:54 -0700327
Haiyang Zhang891de742014-02-12 16:54:27 -0800328 ndev_ctx = netdev_priv(net);
Greg Kroah-Hartman02fafbc2009-08-31 21:09:45 -0700329 if (status == 1) {
Haiyang Zhangc4b6a2e2011-09-01 12:19:42 -0700330 schedule_delayed_work(&ndev_ctx->dwork, 0);
Haiyang Zhang122a5f62011-05-27 06:21:55 -0700331 schedule_delayed_work(&ndev_ctx->dwork, msecs_to_jiffies(20));
Greg Kroah-Hartman02fafbc2009-08-31 21:09:45 -0700332 } else {
Haiyang Zhang891de742014-02-12 16:54:27 -0800333 schedule_delayed_work(&ndev_ctx->dwork, 0);
Hank Janssenfceaf242009-07-13 15:34:54 -0700334 }
Hank Janssenfceaf242009-07-13 15:34:54 -0700335}
336
Hank Janssen3e189512010-03-04 22:11:00 +0000337/*
338 * netvsc_recv_callback - Callback when we receive a packet from the
339 * "wire" on the specified device.
Greg Kroah-Hartman02fafbc2009-08-31 21:09:45 -0700340 */
K. Y. Srinivasanf79adf82011-05-12 19:34:51 -0700341int netvsc_recv_callback(struct hv_device *device_obj,
Greg Kroah-Hartman02fafbc2009-08-31 21:09:45 -0700342 struct hv_netvsc_packet *packet)
Hank Janssenfceaf242009-07-13 15:34:54 -0700343{
Haiyang Zhang6f4c4442012-02-05 12:13:09 +0000344 struct net_device *net;
Hank Janssenfceaf242009-07-13 15:34:54 -0700345 struct sk_buff *skb;
K. Y. Srinivasan2ddd5e52011-09-13 10:59:49 -0700346
Haiyang Zhang6f4c4442012-02-05 12:13:09 +0000347 net = ((struct netvsc_device *)hv_get_drvdata(device_obj))->ndev;
Haiyang Zhanga68f9612013-12-20 16:52:31 -0800348 if (!net || net->reg_state != NETREG_REGISTERED) {
Haiyang Zhang63f69212012-10-02 05:30:23 +0000349 packet->status = NVSP_STAT_FAIL;
Hank Janssenfceaf242009-07-13 15:34:54 -0700350 return 0;
351 }
352
Stephen Hemminger9495c282010-03-09 17:42:17 -0800353 /* Allocate a skb - TODO direct I/O to pages? */
Haiyang Zhang72a2f5b2010-12-10 12:03:58 -0800354 skb = netdev_alloc_skb_ip_align(net, packet->total_data_buflen);
Stephen Hemminger9495c282010-03-09 17:42:17 -0800355 if (unlikely(!skb)) {
356 ++net->stats.rx_dropped;
Haiyang Zhang63f69212012-10-02 05:30:23 +0000357 packet->status = NVSP_STAT_FAIL;
Stephen Hemminger9495c282010-03-09 17:42:17 -0800358 return 0;
359 }
Hank Janssenfceaf242009-07-13 15:34:54 -0700360
Greg Kroah-Hartman02fafbc2009-08-31 21:09:45 -0700361 /*
362 * Copy to skb. This copy is needed here since the memory pointed by
363 * hv_netvsc_packet cannot be deallocated
364 */
Haiyang Zhang45326342011-12-15 13:45:15 -0800365 memcpy(skb_put(skb, packet->total_data_buflen), packet->data,
366 packet->total_data_buflen);
Hank Janssenfceaf242009-07-13 15:34:54 -0700367
368 skb->protocol = eth_type_trans(skb, net);
Hank Janssenfceaf242009-07-13 15:34:54 -0700369 skb->ip_summed = CHECKSUM_NONE;
Haiyang Zhang93725cb2013-06-17 15:36:49 -0700370 if (packet->vlan_tci & VLAN_TAG_PRESENT)
371 __vlan_hwaccel_put_tag(skb, htons(ETH_P_8021Q),
372 packet->vlan_tci);
Hank Janssenfceaf242009-07-13 15:34:54 -0700373
Stephen Hemminger9495c282010-03-09 17:42:17 -0800374 net->stats.rx_packets++;
Wei Yongjun48c38832012-01-29 22:14:02 +0000375 net->stats.rx_bytes += packet->total_data_buflen;
Stephen Hemminger9495c282010-03-09 17:42:17 -0800376
Greg Kroah-Hartman02fafbc2009-08-31 21:09:45 -0700377 /*
378 * Pass the skb back up. Network stack will deallocate the skb when it
Stephen Hemminger9495c282010-03-09 17:42:17 -0800379 * is done.
380 * TODO - use NAPI?
Greg Kroah-Hartman02fafbc2009-08-31 21:09:45 -0700381 */
Stephen Hemminger9495c282010-03-09 17:42:17 -0800382 netif_rx(skb);
Hank Janssenfceaf242009-07-13 15:34:54 -0700383
Hank Janssenfceaf242009-07-13 15:34:54 -0700384 return 0;
385}
386
Stephen Hemmingerf82f4ad2010-05-04 09:58:57 -0700387static void netvsc_get_drvinfo(struct net_device *net,
388 struct ethtool_drvinfo *info)
389{
Jiri Pirko7826d432013-01-06 00:44:26 +0000390 strlcpy(info->driver, KBUILD_MODNAME, sizeof(info->driver));
Jiri Pirko7826d432013-01-06 00:44:26 +0000391 strlcpy(info->fw_version, "N/A", sizeof(info->fw_version));
Stephen Hemmingerf82f4ad2010-05-04 09:58:57 -0700392}
393
Haiyang Zhang4d447c92011-12-15 13:45:17 -0800394static int netvsc_change_mtu(struct net_device *ndev, int mtu)
395{
396 struct net_device_context *ndevctx = netdev_priv(ndev);
397 struct hv_device *hdev = ndevctx->device_ctx;
398 struct netvsc_device *nvdev = hv_get_drvdata(hdev);
399 struct netvsc_device_info device_info;
400 int limit = ETH_DATA_LEN;
401
402 if (nvdev == NULL || nvdev->destroy)
403 return -ENODEV;
404
Haiyang Zhanga1eabb02014-02-19 15:49:45 -0800405 if (nvdev->nvsp_version >= NVSP_PROTOCOL_VERSION_2)
Haiyang Zhang4d447c92011-12-15 13:45:17 -0800406 limit = NETVSC_MTU;
407
408 if (mtu < 68 || mtu > limit)
409 return -EINVAL;
410
411 nvdev->start_remove = true;
Wenqi Ma792df872012-04-19 00:39:37 +0000412 cancel_work_sync(&ndevctx->work);
Haiyang Zhang0a282532012-02-02 07:17:59 +0000413 netif_tx_disable(ndev);
Haiyang Zhang4d447c92011-12-15 13:45:17 -0800414 rndis_filter_device_remove(hdev);
415
416 ndev->mtu = mtu;
417
418 ndevctx->device_ctx = hdev;
419 hv_set_drvdata(hdev, ndev);
420 device_info.ring_size = ring_size;
421 rndis_filter_device_add(hdev, &device_info);
422 netif_wake_queue(ndev);
423
424 return 0;
425}
426
Haiyang Zhang1ce09e82012-07-10 07:19:22 +0000427
428static int netvsc_set_mac_addr(struct net_device *ndev, void *p)
429{
430 struct net_device_context *ndevctx = netdev_priv(ndev);
431 struct hv_device *hdev = ndevctx->device_ctx;
432 struct sockaddr *addr = p;
Jianjun Kong9a4c8312013-01-18 16:52:09 +0000433 char save_adr[ETH_ALEN];
Haiyang Zhang1ce09e82012-07-10 07:19:22 +0000434 unsigned char save_aatype;
435 int err;
436
437 memcpy(save_adr, ndev->dev_addr, ETH_ALEN);
438 save_aatype = ndev->addr_assign_type;
439
440 err = eth_mac_addr(ndev, p);
441 if (err != 0)
442 return err;
443
444 err = rndis_filter_set_device_mac(hdev, addr->sa_data);
445 if (err != 0) {
446 /* roll back to saved MAC */
447 memcpy(ndev->dev_addr, save_adr, ETH_ALEN);
448 ndev->addr_assign_type = save_aatype;
449 }
450
451 return err;
452}
453
454
Stephen Hemmingerf82f4ad2010-05-04 09:58:57 -0700455static const struct ethtool_ops ethtool_ops = {
456 .get_drvinfo = netvsc_get_drvinfo,
Stephen Hemmingerf82f4ad2010-05-04 09:58:57 -0700457 .get_link = ethtool_op_get_link,
458};
459
Greg Kroah-Hartmandf2fff22009-08-31 21:11:12 -0700460static const struct net_device_ops device_ops = {
461 .ndo_open = netvsc_open,
462 .ndo_stop = netvsc_close,
463 .ndo_start_xmit = netvsc_start_xmit,
Jiri Pirkoafc4b132011-08-16 06:29:01 +0000464 .ndo_set_rx_mode = netvsc_set_multicast_list,
Haiyang Zhang4d447c92011-12-15 13:45:17 -0800465 .ndo_change_mtu = netvsc_change_mtu,
Haiyang Zhangb681b582010-08-03 19:15:31 +0000466 .ndo_validate_addr = eth_validate_addr,
Haiyang Zhang1ce09e82012-07-10 07:19:22 +0000467 .ndo_set_mac_address = netvsc_set_mac_addr,
Greg Kroah-Hartmandf2fff22009-08-31 21:11:12 -0700468};
469
Haiyang Zhangc996edc2011-04-06 15:18:00 -0700470/*
471 * Send GARP packet to network peers after migrations.
472 * After Quick Migration, the network is not immediately operational in the
473 * current context when receiving RNDIS_STATUS_MEDIA_CONNECT event. So, add
Haiyang Zhang122a5f62011-05-27 06:21:55 -0700474 * another netif_notify_peers() into a delayed work, otherwise GARP packet
Haiyang Zhangc996edc2011-04-06 15:18:00 -0700475 * will not be sent after quick migration, and cause network disconnection.
Haiyang Zhang891de742014-02-12 16:54:27 -0800476 * Also, we update the carrier status here.
Haiyang Zhangc996edc2011-04-06 15:18:00 -0700477 */
Haiyang Zhang891de742014-02-12 16:54:27 -0800478static void netvsc_link_change(struct work_struct *w)
Haiyang Zhangc996edc2011-04-06 15:18:00 -0700479{
480 struct net_device_context *ndev_ctx;
481 struct net_device *net;
K. Y. Srinivasan2ddd5e52011-09-13 10:59:49 -0700482 struct netvsc_device *net_device;
Haiyang Zhang891de742014-02-12 16:54:27 -0800483 struct rndis_device *rdev;
484 bool notify;
485
486 rtnl_lock();
Haiyang Zhangc996edc2011-04-06 15:18:00 -0700487
Haiyang Zhang122a5f62011-05-27 06:21:55 -0700488 ndev_ctx = container_of(w, struct net_device_context, dwork.work);
K. Y. Srinivasan2ddd5e52011-09-13 10:59:49 -0700489 net_device = hv_get_drvdata(ndev_ctx->device_ctx);
Haiyang Zhang891de742014-02-12 16:54:27 -0800490 rdev = net_device->extension;
K. Y. Srinivasan2ddd5e52011-09-13 10:59:49 -0700491 net = net_device->ndev;
Haiyang Zhang891de742014-02-12 16:54:27 -0800492
493 if (rdev->link_state) {
494 netif_carrier_off(net);
495 notify = false;
496 } else {
497 netif_carrier_on(net);
498 notify = true;
499 }
500
501 rtnl_unlock();
502
503 if (notify)
504 netdev_notify_peers(net);
Haiyang Zhangc996edc2011-04-06 15:18:00 -0700505}
506
507
K. Y. Srinivasan84946892011-09-13 10:59:38 -0700508static int netvsc_probe(struct hv_device *dev,
509 const struct hv_vmbus_device_id *dev_id)
Greg Kroah-Hartmandf2fff22009-08-31 21:11:12 -0700510{
Greg Kroah-Hartmandf2fff22009-08-31 21:11:12 -0700511 struct net_device *net = NULL;
512 struct net_device_context *net_device_ctx;
513 struct netvsc_device_info device_info;
514 int ret;
515
Stephen Hemminger546d9e12010-03-11 09:11:37 -0800516 net = alloc_etherdev(sizeof(struct net_device_context));
Greg Kroah-Hartmandf2fff22009-08-31 21:11:12 -0700517 if (!net)
K. Y. Srinivasan51a805d2011-08-25 09:49:11 -0700518 return -ENOMEM;
Greg Kroah-Hartmandf2fff22009-08-31 21:11:12 -0700519
Greg Kroah-Hartmandf2fff22009-08-31 21:11:12 -0700520 net_device_ctx = netdev_priv(net);
K. Y. Srinivasan9efd21e2011-04-29 13:45:10 -0700521 net_device_ctx->device_ctx = dev;
K. Y. Srinivasan2ddd5e52011-09-13 10:59:49 -0700522 hv_set_drvdata(dev, net);
Haiyang Zhang891de742014-02-12 16:54:27 -0800523 INIT_DELAYED_WORK(&net_device_ctx->dwork, netvsc_link_change);
Wenqi Ma792df872012-04-19 00:39:37 +0000524 INIT_WORK(&net_device_ctx->work, do_set_multicast);
Greg Kroah-Hartmandf2fff22009-08-31 21:11:12 -0700525
Greg Kroah-Hartmandf2fff22009-08-31 21:11:12 -0700526 net->netdev_ops = &device_ops;
527
Stephen Hemminger60487182010-05-04 09:58:55 -0700528 /* TODO: Add GSO and Checksum offload */
KY Srinivasan54a73572014-03-08 19:23:13 -0800529 net->hw_features = NETIF_F_SG;
530 net->features = NETIF_F_HW_VLAN_CTAG_TX | NETIF_F_SG;
Stephen Hemminger60487182010-05-04 09:58:55 -0700531
Stephen Hemmingerf82f4ad2010-05-04 09:58:57 -0700532 SET_ETHTOOL_OPS(net, &ethtool_ops);
K. Y. Srinivasan9efd21e2011-04-29 13:45:10 -0700533 SET_NETDEV_DEV(net, &dev->device);
Greg Kroah-Hartmandf2fff22009-08-31 21:11:12 -0700534
Haiyang Zhang692e0842011-09-01 12:19:43 -0700535 /* Notify the netvsc driver of the new device */
536 device_info.ring_size = ring_size;
537 ret = rndis_filter_device_add(dev, &device_info);
538 if (ret != 0) {
539 netdev_err(net, "unable to add netvsc device (ret %d)\n", ret);
Haiyang Zhang692e0842011-09-01 12:19:43 -0700540 free_netdev(net);
K. Y. Srinivasan2ddd5e52011-09-13 10:59:49 -0700541 hv_set_drvdata(dev, NULL);
Haiyang Zhang692e0842011-09-01 12:19:43 -0700542 return ret;
543 }
544 memcpy(net->dev_addr, device_info.mac_adr, ETH_ALEN);
545
Haiyang Zhanga68f9612013-12-20 16:52:31 -0800546 ret = register_netdev(net);
547 if (ret != 0) {
548 pr_err("Unable to register netdev.\n");
549 rndis_filter_device_remove(dev);
550 free_netdev(net);
551 }
552
Greg Kroah-Hartmandf2fff22009-08-31 21:11:12 -0700553 return ret;
554}
555
K. Y. Srinivasan415b0232011-04-29 13:45:12 -0700556static int netvsc_remove(struct hv_device *dev)
Greg Kroah-Hartmandf2fff22009-08-31 21:11:12 -0700557{
K. Y. Srinivasan2ddd5e52011-09-13 10:59:49 -0700558 struct net_device *net;
Haiyang Zhang122a5f62011-05-27 06:21:55 -0700559 struct net_device_context *ndev_ctx;
K. Y. Srinivasan2ddd5e52011-09-13 10:59:49 -0700560 struct netvsc_device *net_device;
561
562 net_device = hv_get_drvdata(dev);
563 net = net_device->ndev;
Greg Kroah-Hartmandf2fff22009-08-31 21:11:12 -0700564
Greg Kroah-Hartmandf2fff22009-08-31 21:11:12 -0700565 if (net == NULL) {
K. Y. Srinivasan415b0232011-04-29 13:45:12 -0700566 dev_err(&dev->device, "No net device to remove\n");
Greg Kroah-Hartmandf2fff22009-08-31 21:11:12 -0700567 return 0;
568 }
569
Haiyang Zhang4d447c92011-12-15 13:45:17 -0800570 net_device->start_remove = true;
571
Haiyang Zhang122a5f62011-05-27 06:21:55 -0700572 ndev_ctx = netdev_priv(net);
573 cancel_delayed_work_sync(&ndev_ctx->dwork);
Wenqi Ma792df872012-04-19 00:39:37 +0000574 cancel_work_sync(&ndev_ctx->work);
Haiyang Zhang122a5f62011-05-27 06:21:55 -0700575
Greg Kroah-Hartmandf2fff22009-08-31 21:11:12 -0700576 /* Stop outbound asap */
Haiyang Zhang0a282532012-02-02 07:17:59 +0000577 netif_tx_disable(net);
Greg Kroah-Hartmandf2fff22009-08-31 21:11:12 -0700578
579 unregister_netdev(net);
580
581 /*
582 * Call to the vsc driver to let it know that the device is being
583 * removed
584 */
Haiyang Zhangdf06bcf2011-05-23 09:03:47 -0700585 rndis_filter_device_remove(dev);
Greg Kroah-Hartmandf2fff22009-08-31 21:11:12 -0700586
587 free_netdev(net);
Haiyang Zhangdf06bcf2011-05-23 09:03:47 -0700588 return 0;
Greg Kroah-Hartmandf2fff22009-08-31 21:11:12 -0700589}
590
K. Y. Srinivasan345c4cc2011-08-25 09:48:34 -0700591static const struct hv_vmbus_device_id id_table[] = {
Greg Kroah-Hartmanc45cf2d2011-08-25 11:41:33 -0700592 /* Network guid */
K. Y. Srinivasan8f505942013-01-23 17:42:42 -0800593 { HV_NIC_GUID, },
Greg Kroah-Hartmanc45cf2d2011-08-25 11:41:33 -0700594 { },
K. Y. Srinivasan345c4cc2011-08-25 09:48:34 -0700595};
596
597MODULE_DEVICE_TABLE(vmbus, id_table);
598
K. Y. Srinivasanf1542a62011-05-10 07:55:16 -0700599/* The one and only one */
K. Y. Srinivasanfde0ef92011-05-12 19:35:08 -0700600static struct hv_driver netvsc_drv = {
Haiyang Zhangd31b20f2012-03-07 10:02:00 +0000601 .name = KBUILD_MODNAME,
K. Y. Srinivasan345c4cc2011-08-25 09:48:34 -0700602 .id_table = id_table,
K. Y. Srinivasanfde0ef92011-05-12 19:35:08 -0700603 .probe = netvsc_probe,
604 .remove = netvsc_remove,
K. Y. Srinivasand4890972011-05-10 07:55:17 -0700605};
K. Y. Srinivasanf1542a62011-05-10 07:55:16 -0700606
K. Y. Srinivasana9869c92011-05-12 19:35:17 -0700607static void __exit netvsc_drv_exit(void)
Hank Janssenfceaf242009-07-13 15:34:54 -0700608{
Greg Kroah-Hartman768fa212011-08-25 15:07:32 -0700609 vmbus_driver_unregister(&netvsc_drv);
Hank Janssenfceaf242009-07-13 15:34:54 -0700610}
611
K. Y. Srinivasan1fde28c2011-05-12 19:35:16 -0700612static int __init netvsc_drv_init(void)
Greg Kroah-Hartmandf2fff22009-08-31 21:11:12 -0700613{
Haiyang Zhangfa85a6c2012-07-25 08:08:41 +0000614 if (ring_size < RING_SIZE_MIN) {
615 ring_size = RING_SIZE_MIN;
616 pr_info("Increased ring_size to %d (min allowed)\n",
617 ring_size);
618 }
Greg Kroah-Hartman768fa212011-08-25 15:07:32 -0700619 return vmbus_driver_register(&netvsc_drv);
Greg Kroah-Hartmandf2fff22009-08-31 21:11:12 -0700620}
621
Hank Janssen26c14cc2010-02-11 23:02:42 +0000622MODULE_LICENSE("GPL");
Stephen Hemminger7880fc54c2010-05-04 09:58:52 -0700623MODULE_DESCRIPTION("Microsoft Hyper-V network driver");
Hank Janssenfceaf242009-07-13 15:34:54 -0700624
K. Y. Srinivasan1fde28c2011-05-12 19:35:16 -0700625module_init(netvsc_drv_init);
K. Y. Srinivasana9869c92011-05-12 19:35:17 -0700626module_exit(netvsc_drv_exit);