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