Merge tag 'v3.10.107' into update
[GitHub/mt8127/android_kernel_alcatel_ttab.git] / drivers / net / hyperv / netvsc_drv.c
CommitLineData
fceaf24a 1/*
fceaf24a
HJ
2 * 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:
d0e94d17 18 * Haiyang Zhang <haiyangz@microsoft.com>
fceaf24a 19 * Hank Janssen <hjanssen@microsoft.com>
fceaf24a 20 */
eb335bc4
HJ
21#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
22
fceaf24a 23#include <linux/init.h>
9079ce69 24#include <linux/atomic.h>
fceaf24a
HJ
25#include <linux/module.h>
26#include <linux/highmem.h>
27#include <linux/device.h>
fceaf24a 28#include <linux/io.h>
fceaf24a
HJ
29#include <linux/delay.h>
30#include <linux/netdevice.h>
31#include <linux/inetdevice.h>
32#include <linux/etherdevice.h>
33#include <linux/skbuff.h>
c802db11 34#include <linux/if_vlan.h>
fceaf24a 35#include <linux/in.h>
5a0e3ad6 36#include <linux/slab.h>
fceaf24a
HJ
37#include <net/arp.h>
38#include <net/route.h>
39#include <net/sock.h>
40#include <net/pkt_sched.h>
3f335ea2 41
5ca7252a 42#include "hyperv_net.h"
fceaf24a 43
fceaf24a 44struct net_device_context {
02fafbc6 45 /* point back to our device context */
6bad88da 46 struct hv_device *device_ctx;
122a5f64 47 struct delayed_work dwork;
792df872 48 struct work_struct work;
fceaf24a
HJ
49};
50
43752984 51/* Restrict GSO size to account for NVGRE */
52#define NETVSC_GSO_MAX_SIZE 62768
53
fa85a6c2 54#define RING_SIZE_MIN 64
99c8da0f 55static int ring_size = 128;
450d7a4b
SH
56module_param(ring_size, int, S_IRUGO);
57MODULE_PARM_DESC(ring_size, "Ring buffer size (# of pages)");
fceaf24a 58
d426b2e3
HZ
59static void do_set_multicast(struct work_struct *w)
60{
792df872
WM
61 struct net_device_context *ndevctx =
62 container_of(w, struct net_device_context, work);
d426b2e3
HZ
63 struct netvsc_device *nvdev;
64 struct rndis_device *rdev;
65
66 nvdev = hv_get_drvdata(ndevctx->device_ctx);
792df872
WM
67 if (nvdev == NULL || nvdev->ndev == NULL)
68 return;
d426b2e3
HZ
69
70 rdev = nvdev->extension;
71 if (rdev == NULL)
792df872 72 return;
d426b2e3 73
792df872 74 if (nvdev->ndev->flags & IFF_PROMISC)
d426b2e3
HZ
75 rndis_filter_set_packet_filter(rdev,
76 NDIS_PACKET_TYPE_PROMISCUOUS);
77 else
78 rndis_filter_set_packet_filter(rdev,
79 NDIS_PACKET_TYPE_BROADCAST |
80 NDIS_PACKET_TYPE_ALL_MULTICAST |
81 NDIS_PACKET_TYPE_DIRECTED);
d426b2e3
HZ
82}
83
4e9bfefa 84static void netvsc_set_multicast_list(struct net_device *net)
fceaf24a 85{
792df872 86 struct net_device_context *net_device_ctx = netdev_priv(net);
d426b2e3 87
792df872 88 schedule_work(&net_device_ctx->work);
fceaf24a
HJ
89}
90
fceaf24a
HJ
91static int netvsc_open(struct net_device *net)
92{
fceaf24a 93 struct net_device_context *net_device_ctx = netdev_priv(net);
6bad88da 94 struct hv_device *device_obj = net_device_ctx->device_ctx;
02fafbc6 95 int ret = 0;
fceaf24a 96
d515d0ff
HZ
97 /* 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;
fceaf24a
HJ
102 }
103
d515d0ff
HZ
104 netif_start_queue(net);
105
fceaf24a
HJ
106 return ret;
107}
108
fceaf24a
HJ
109static int netvsc_close(struct net_device *net)
110{
fceaf24a 111 struct net_device_context *net_device_ctx = netdev_priv(net);
6bad88da 112 struct hv_device *device_obj = net_device_ctx->device_ctx;
02fafbc6 113 int ret;
fceaf24a 114
0a282538 115 netif_tx_disable(net);
fceaf24a 116
792df872
WM
117 /* Make sure netvsc_set_multicast_list doesn't re-enable filter! */
118 cancel_work_sync(&net_device_ctx->work);
9c26aa0d 119 ret = rndis_filter_close(device_obj);
fceaf24a 120 if (ret != 0)
eb335bc4 121 netdev_err(net, "unable to close device (ret %d).\n", ret);
fceaf24a 122
fceaf24a
HJ
123 return ret;
124}
125
fceaf24a
HJ
126static void netvsc_xmit_completion(void *context)
127{
4193d4f4 128 struct hv_netvsc_packet *packet = (struct hv_netvsc_packet *)context;
02fafbc6 129 struct sk_buff *skb = (struct sk_buff *)
72a2f5bd 130 (unsigned long)packet->completion.send.send_completion_tid;
fceaf24a 131
fceaf24a
HJ
132 kfree(packet);
133
1d06825b 134 if (skb)
b220f5f9 135 dev_kfree_skb_any(skb);
fceaf24a
HJ
136}
137
02fafbc6 138static int netvsc_start_xmit(struct sk_buff *skb, struct net_device *net)
fceaf24a 139{
fceaf24a 140 struct net_device_context *net_device_ctx = netdev_priv(net);
4193d4f4 141 struct hv_netvsc_packet *packet;
02fafbc6 142 int ret;
4d447c9a 143 unsigned int i, num_pages, npg_data;
f9aceca3 144 u32 skb_length = skb->len;
fceaf24a 145
c31c151b 146 /* Add multipages for skb->data and additional 2 for RNDIS */
4d447c9a
HZ
147 npg_data = (((unsigned long)skb->data + skb_headlen(skb) - 1)
148 >> PAGE_SHIFT) - ((unsigned long)skb->data >> PAGE_SHIFT) + 1;
c31c151b 149 num_pages = skb_shinfo(skb)->nr_frags + npg_data + 2;
fceaf24a 150
454f18a9 151 /* Allocate a netvsc packet based on # of frags. */
02fafbc6 152 packet = kzalloc(sizeof(struct hv_netvsc_packet) +
6048718d 153 (num_pages * sizeof(struct hv_page_buffer)) +
1f5f3a75
HZ
154 sizeof(struct rndis_filter_packet) +
155 NDIS_VLAN_PPI_SIZE, GFP_ATOMIC);
02fafbc6 156 if (!packet) {
bf769375 157 /* out of memory, drop packet */
eb335bc4 158 netdev_err(net, "unable to allocate hv_netvsc_packet\n");
b220f5f9
SH
159
160 dev_kfree_skb(skb);
161 net->stats.tx_dropped++;
bb6d5e76 162 return NETDEV_TX_OK;
fceaf24a
HJ
163 }
164
1f5f3a75
HZ
165 packet->vlan_tci = skb->vlan_tci;
166
72a2f5bd 167 packet->extension = (void *)(unsigned long)packet +
02fafbc6 168 sizeof(struct hv_netvsc_packet) +
6048718d 169 (num_pages * sizeof(struct hv_page_buffer));
fceaf24a 170
c31c151b
HZ
171 /* If the rndis msg goes beyond 1 page, we will add 1 later */
172 packet->page_buf_cnt = num_pages - 1;
fceaf24a 173
454f18a9 174 /* Initialize it from the skb */
4d447c9a 175 packet->total_data_buflen = skb->len;
fceaf24a 176
6048718d 177 /* Start filling in the page buffers starting after RNDIS buffer. */
ca623ad3
HZ
178 packet->page_buf[1].pfn = virt_to_phys(skb->data) >> PAGE_SHIFT;
179 packet->page_buf[1].offset
6048718d 180 = (unsigned long)skb->data & (PAGE_SIZE - 1);
4d447c9a
HZ
181 if (npg_data == 1)
182 packet->page_buf[1].len = skb_headlen(skb);
183 else
184 packet->page_buf[1].len = PAGE_SIZE
185 - packet->page_buf[1].offset;
186
187 for (i = 2; i <= npg_data; i++) {
188 packet->page_buf[i].pfn = virt_to_phys(skb->data
189 + PAGE_SIZE * (i-1)) >> PAGE_SHIFT;
190 packet->page_buf[i].offset = 0;
191 packet->page_buf[i].len = PAGE_SIZE;
192 }
193 if (npg_data > 1)
194 packet->page_buf[npg_data].len = (((unsigned long)skb->data
195 + skb_headlen(skb) - 1) & (PAGE_SIZE - 1)) + 1;
6048718d
SH
196
197 /* Additional fragments are after SKB data */
198 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
9e903e08 199 const skb_frag_t *f = &skb_shinfo(skb)->frags[i];
6048718d 200
4d447c9a
HZ
201 packet->page_buf[i+npg_data+1].pfn =
202 page_to_pfn(skb_frag_page(f));
203 packet->page_buf[i+npg_data+1].offset = f->page_offset;
204 packet->page_buf[i+npg_data+1].len = skb_frag_size(f);
fceaf24a
HJ
205 }
206
454f18a9 207 /* Set the completion routine */
72a2f5bd
HZ
208 packet->completion.send.send_completion = netvsc_xmit_completion;
209 packet->completion.send.send_completion_ctx = packet;
210 packet->completion.send.send_completion_tid = (unsigned long)skb;
fceaf24a 211
55acb696 212 ret = rndis_filter_send(net_device_ctx->device_ctx,
02fafbc6 213 packet);
02fafbc6 214 if (ret == 0) {
f9aceca3 215 net->stats.tx_bytes += skb_length;
b852fdce 216 net->stats.tx_packets++;
b220f5f9 217 } else {
8a5f9edc 218 kfree(packet);
33be96e4
HZ
219 if (ret != -EAGAIN) {
220 dev_kfree_skb_any(skb);
221 net->stats.tx_dropped++;
222 }
fceaf24a
HJ
223 }
224
33be96e4 225 return (ret == -EAGAIN) ? NETDEV_TX_BUSY : NETDEV_TX_OK;
fceaf24a
HJ
226}
227
3e189519 228/*
02fafbc6
GKH
229 * netvsc_linkstatus_callback - Link up/down notification
230 */
90ef117a 231void netvsc_linkstatus_callback(struct hv_device *device_obj,
02fafbc6 232 unsigned int status)
fceaf24a 233{
2ddd5e5f 234 struct net_device *net;
c996edcf 235 struct net_device_context *ndev_ctx;
2ddd5e5f
S
236 struct netvsc_device *net_device;
237
238 net_device = hv_get_drvdata(device_obj);
239 net = net_device->ndev;
fceaf24a 240
02fafbc6 241 if (!net) {
eb335bc4
HJ
242 netdev_err(net, "got link status but net device "
243 "not initialized yet\n");
fceaf24a
HJ
244 return;
245 }
246
02fafbc6 247 if (status == 1) {
fceaf24a 248 netif_carrier_on(net);
c996edcf 249 ndev_ctx = netdev_priv(net);
c4b6a2ea 250 schedule_delayed_work(&ndev_ctx->dwork, 0);
122a5f64 251 schedule_delayed_work(&ndev_ctx->dwork, msecs_to_jiffies(20));
02fafbc6 252 } else {
fceaf24a 253 netif_carrier_off(net);
fceaf24a 254 }
fceaf24a
HJ
255}
256
3e189519
HJ
257/*
258 * netvsc_recv_callback - Callback when we receive a packet from the
259 * "wire" on the specified device.
02fafbc6 260 */
f79adf8f 261int netvsc_recv_callback(struct hv_device *device_obj,
02fafbc6 262 struct hv_netvsc_packet *packet)
fceaf24a 263{
6f4c4446 264 struct net_device *net;
fceaf24a 265 struct sk_buff *skb;
fceaf24a 266
6f4c4446 267 net = ((struct netvsc_device *)hv_get_drvdata(device_obj))->ndev;
02fafbc6 268 if (!net) {
eb335bc4
HJ
269 netdev_err(net, "got receive callback but net device"
270 " not initialized yet\n");
63f6921d 271 packet->status = NVSP_STAT_FAIL;
fceaf24a
HJ
272 return 0;
273 }
274
9495c282 275 /* Allocate a skb - TODO direct I/O to pages? */
72a2f5bd 276 skb = netdev_alloc_skb_ip_align(net, packet->total_data_buflen);
9495c282
SH
277 if (unlikely(!skb)) {
278 ++net->stats.rx_dropped;
63f6921d 279 packet->status = NVSP_STAT_FAIL;
9495c282
SH
280 return 0;
281 }
fceaf24a 282
02fafbc6
GKH
283 /*
284 * Copy to skb. This copy is needed here since the memory pointed by
285 * hv_netvsc_packet cannot be deallocated
286 */
45326342
HZ
287 memcpy(skb_put(skb, packet->total_data_buflen), packet->data,
288 packet->total_data_buflen);
fceaf24a
HJ
289
290 skb->protocol = eth_type_trans(skb, net);
fceaf24a 291 skb->ip_summed = CHECKSUM_NONE;
93725cbd
HZ
292 if (packet->vlan_tci & VLAN_TAG_PRESENT)
293 __vlan_hwaccel_put_tag(skb, htons(ETH_P_8021Q),
294 packet->vlan_tci);
fceaf24a 295
9495c282 296 net->stats.rx_packets++;
48c38839 297 net->stats.rx_bytes += packet->total_data_buflen;
9495c282 298
02fafbc6
GKH
299 /*
300 * Pass the skb back up. Network stack will deallocate the skb when it
9495c282
SH
301 * is done.
302 * TODO - use NAPI?
02fafbc6 303 */
9495c282 304 netif_rx(skb);
fceaf24a 305
fceaf24a
HJ
306 return 0;
307}
308
f82f4ad7
SH
309static void netvsc_get_drvinfo(struct net_device *net,
310 struct ethtool_drvinfo *info)
311{
7826d43f
JP
312 strlcpy(info->driver, KBUILD_MODNAME, sizeof(info->driver));
313 strlcpy(info->version, HV_DRV_VERSION, sizeof(info->version));
314 strlcpy(info->fw_version, "N/A", sizeof(info->fw_version));
f82f4ad7
SH
315}
316
4d447c9a
HZ
317static int netvsc_change_mtu(struct net_device *ndev, int mtu)
318{
319 struct net_device_context *ndevctx = netdev_priv(ndev);
320 struct hv_device *hdev = ndevctx->device_ctx;
321 struct netvsc_device *nvdev = hv_get_drvdata(hdev);
322 struct netvsc_device_info device_info;
323 int limit = ETH_DATA_LEN;
324
325 if (nvdev == NULL || nvdev->destroy)
326 return -ENODEV;
327
328 if (nvdev->nvsp_version == NVSP_PROTOCOL_VERSION_2)
329 limit = NETVSC_MTU;
330
331 if (mtu < 68 || mtu > limit)
332 return -EINVAL;
333
334 nvdev->start_remove = true;
792df872 335 cancel_work_sync(&ndevctx->work);
0a282538 336 netif_tx_disable(ndev);
4d447c9a
HZ
337 rndis_filter_device_remove(hdev);
338
339 ndev->mtu = mtu;
340
341 ndevctx->device_ctx = hdev;
342 hv_set_drvdata(hdev, ndev);
343 device_info.ring_size = ring_size;
344 rndis_filter_device_add(hdev, &device_info);
345 netif_wake_queue(ndev);
346
347 return 0;
348}
349
1ce09e89
HZ
350
351static int netvsc_set_mac_addr(struct net_device *ndev, void *p)
352{
353 struct net_device_context *ndevctx = netdev_priv(ndev);
354 struct hv_device *hdev = ndevctx->device_ctx;
355 struct sockaddr *addr = p;
9a4c831e 356 char save_adr[ETH_ALEN];
1ce09e89
HZ
357 unsigned char save_aatype;
358 int err;
359
360 memcpy(save_adr, ndev->dev_addr, ETH_ALEN);
361 save_aatype = ndev->addr_assign_type;
362
363 err = eth_mac_addr(ndev, p);
364 if (err != 0)
365 return err;
366
367 err = rndis_filter_set_device_mac(hdev, addr->sa_data);
368 if (err != 0) {
369 /* roll back to saved MAC */
370 memcpy(ndev->dev_addr, save_adr, ETH_ALEN);
371 ndev->addr_assign_type = save_aatype;
372 }
373
374 return err;
375}
376
377
f82f4ad7
SH
378static const struct ethtool_ops ethtool_ops = {
379 .get_drvinfo = netvsc_get_drvinfo,
f82f4ad7
SH
380 .get_link = ethtool_op_get_link,
381};
382
df2fff28
GKH
383static const struct net_device_ops device_ops = {
384 .ndo_open = netvsc_open,
385 .ndo_stop = netvsc_close,
386 .ndo_start_xmit = netvsc_start_xmit,
afc4b13d 387 .ndo_set_rx_mode = netvsc_set_multicast_list,
4d447c9a 388 .ndo_change_mtu = netvsc_change_mtu,
b681b588 389 .ndo_validate_addr = eth_validate_addr,
1ce09e89 390 .ndo_set_mac_address = netvsc_set_mac_addr,
df2fff28
GKH
391};
392
c996edcf
HZ
393/*
394 * Send GARP packet to network peers after migrations.
395 * After Quick Migration, the network is not immediately operational in the
396 * current context when receiving RNDIS_STATUS_MEDIA_CONNECT event. So, add
122a5f64 397 * another netif_notify_peers() into a delayed work, otherwise GARP packet
c996edcf
HZ
398 * will not be sent after quick migration, and cause network disconnection.
399 */
400static void netvsc_send_garp(struct work_struct *w)
401{
402 struct net_device_context *ndev_ctx;
403 struct net_device *net;
2ddd5e5f 404 struct netvsc_device *net_device;
c996edcf 405
122a5f64 406 ndev_ctx = container_of(w, struct net_device_context, dwork.work);
2ddd5e5f
S
407 net_device = hv_get_drvdata(ndev_ctx->device_ctx);
408 net = net_device->ndev;
ee89bab1 409 netdev_notify_peers(net);
c996edcf
HZ
410}
411
412
84946899
S
413static int netvsc_probe(struct hv_device *dev,
414 const struct hv_vmbus_device_id *dev_id)
df2fff28 415{
df2fff28
GKH
416 struct net_device *net = NULL;
417 struct net_device_context *net_device_ctx;
418 struct netvsc_device_info device_info;
419 int ret;
420
546d9e10 421 net = alloc_etherdev(sizeof(struct net_device_context));
df2fff28 422 if (!net)
51a805d0 423 return -ENOMEM;
df2fff28
GKH
424
425 /* Set initial state */
426 netif_carrier_off(net);
df2fff28
GKH
427
428 net_device_ctx = netdev_priv(net);
9efd21e1 429 net_device_ctx->device_ctx = dev;
2ddd5e5f 430 hv_set_drvdata(dev, net);
122a5f64 431 INIT_DELAYED_WORK(&net_device_ctx->dwork, netvsc_send_garp);
792df872 432 INIT_WORK(&net_device_ctx->work, do_set_multicast);
df2fff28 433
df2fff28
GKH
434 net->netdev_ops = &device_ops;
435
6048718d 436 /* TODO: Add GSO and Checksum offload */
98bec4a1
HZ
437 net->hw_features = 0;
438 net->features = NETIF_F_HW_VLAN_CTAG_TX;
6048718d 439
f82f4ad7 440 SET_ETHTOOL_OPS(net, &ethtool_ops);
9efd21e1 441 SET_NETDEV_DEV(net, &dev->device);
43752984 442 netif_set_gso_max_size(net, NETVSC_GSO_MAX_SIZE);
df2fff28
GKH
443
444 ret = register_netdev(net);
445 if (ret != 0) {
692e084e 446 pr_err("Unable to register netdev.\n");
df2fff28 447 free_netdev(net);
692e084e 448 goto out;
df2fff28
GKH
449 }
450
692e084e
HZ
451 /* Notify the netvsc driver of the new device */
452 device_info.ring_size = ring_size;
453 ret = rndis_filter_device_add(dev, &device_info);
454 if (ret != 0) {
455 netdev_err(net, "unable to add netvsc device (ret %d)\n", ret);
456 unregister_netdev(net);
df2fff28 457 free_netdev(net);
2ddd5e5f 458 hv_set_drvdata(dev, NULL);
692e084e 459 return ret;
df2fff28 460 }
692e084e
HZ
461 memcpy(net->dev_addr, device_info.mac_adr, ETH_ALEN);
462
463 netif_carrier_on(net);
df2fff28 464
692e084e 465out:
df2fff28
GKH
466 return ret;
467}
468
415b023a 469static int netvsc_remove(struct hv_device *dev)
df2fff28 470{
2ddd5e5f 471 struct net_device *net;
122a5f64 472 struct net_device_context *ndev_ctx;
2ddd5e5f
S
473 struct netvsc_device *net_device;
474
475 net_device = hv_get_drvdata(dev);
476 net = net_device->ndev;
df2fff28 477
df2fff28 478 if (net == NULL) {
415b023a 479 dev_err(&dev->device, "No net device to remove\n");
df2fff28
GKH
480 return 0;
481 }
482
4d447c9a
HZ
483 net_device->start_remove = true;
484
122a5f64
HZ
485 ndev_ctx = netdev_priv(net);
486 cancel_delayed_work_sync(&ndev_ctx->dwork);
792df872 487 cancel_work_sync(&ndev_ctx->work);
122a5f64 488
df2fff28 489 /* Stop outbound asap */
0a282538 490 netif_tx_disable(net);
df2fff28
GKH
491
492 unregister_netdev(net);
493
494 /*
495 * Call to the vsc driver to let it know that the device is being
496 * removed
497 */
df06bcff 498 rndis_filter_device_remove(dev);
df2fff28
GKH
499
500 free_netdev(net);
df06bcff 501 return 0;
df2fff28
GKH
502}
503
345c4cc3 504static const struct hv_vmbus_device_id id_table[] = {
c45cf2d4 505 /* Network guid */
8f505944 506 { HV_NIC_GUID, },
c45cf2d4 507 { },
345c4cc3
S
508};
509
510MODULE_DEVICE_TABLE(vmbus, id_table);
511
f1542a66 512/* The one and only one */
fde0ef9b 513static struct hv_driver netvsc_drv = {
d31b20fc 514 .name = KBUILD_MODNAME,
345c4cc3 515 .id_table = id_table,
fde0ef9b
S
516 .probe = netvsc_probe,
517 .remove = netvsc_remove,
d4890970 518};
f1542a66 519
a9869c94 520static void __exit netvsc_drv_exit(void)
fceaf24a 521{
768fa219 522 vmbus_driver_unregister(&netvsc_drv);
fceaf24a
HJ
523}
524
1fde28cf 525static int __init netvsc_drv_init(void)
df2fff28 526{
fa85a6c2
HZ
527 if (ring_size < RING_SIZE_MIN) {
528 ring_size = RING_SIZE_MIN;
529 pr_info("Increased ring_size to %d (min allowed)\n",
530 ring_size);
531 }
768fa219 532 return vmbus_driver_register(&netvsc_drv);
df2fff28
GKH
533}
534
26c14cc1
HJ
535MODULE_LICENSE("GPL");
536MODULE_VERSION(HV_DRV_VERSION);
7880fc54 537MODULE_DESCRIPTION("Microsoft Hyper-V network driver");
fceaf24a 538
1fde28cf 539module_init(netvsc_drv_init);
a9869c94 540module_exit(netvsc_drv_exit);