]> rtime.felk.cvut.cz Git - linux-imx.git/blob - drivers/staging/hv/netvsc_drv.c
Staging: hv: netvsc: Fix a bug in accounting transmit slots
[linux-imx.git] / drivers / staging / hv / 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, write to the Free Software Foundation, Inc., 59 Temple
15  * Place - Suite 330, Boston, MA 02111-1307 USA.
16  *
17  * Authors:
18  *   Haiyang Zhang <haiyangz@microsoft.com>
19  *   Hank Janssen  <hjanssen@microsoft.com>
20  */
21 #include <linux/init.h>
22 #include <linux/atomic.h>
23 #include <linux/module.h>
24 #include <linux/highmem.h>
25 #include <linux/device.h>
26 #include <linux/io.h>
27 #include <linux/delay.h>
28 #include <linux/netdevice.h>
29 #include <linux/inetdevice.h>
30 #include <linux/etherdevice.h>
31 #include <linux/skbuff.h>
32 #include <linux/in.h>
33 #include <linux/slab.h>
34 #include <linux/dmi.h>
35 #include <linux/pci.h>
36 #include <net/arp.h>
37 #include <net/route.h>
38 #include <net/sock.h>
39 #include <net/pkt_sched.h>
40 #include "osd.h"
41 #include "logging.h"
42 #include "version_info.h"
43 #include "vmbus.h"
44 #include "netvsc_api.h"
45
46 struct net_device_context {
47         /* point back to our device context */
48         struct vm_device *device_ctx;
49         atomic_t avail;
50         struct work_struct work;
51 };
52
53 struct netvsc_driver_context {
54         /* !! These must be the first 2 fields !! */
55         /* Which is a bug FIXME! */
56         struct driver_context drv_ctx;
57         struct netvsc_driver drv_obj;
58 };
59
60 #define PACKET_PAGES_LOWATER  8
61 /* Need this many pages to handle worst case fragmented packet */
62 #define PACKET_PAGES_HIWATER  (MAX_SKB_FRAGS + 2)
63
64 static int ring_size = roundup_pow_of_two(2*MAX_SKB_FRAGS+1);
65 module_param(ring_size, int, S_IRUGO);
66 MODULE_PARM_DESC(ring_size, "Ring buffer size (# of pages)");
67
68 /* The one and only one */
69 static struct netvsc_driver_context g_netvsc_drv;
70
71 static void netvsc_set_multicast_list(struct net_device *net)
72 {
73 }
74
75 static int netvsc_open(struct net_device *net)
76 {
77         struct net_device_context *net_device_ctx = netdev_priv(net);
78         struct hv_device *device_obj = &net_device_ctx->device_ctx->device_obj;
79         int ret = 0;
80
81         DPRINT_ENTER(NETVSC_DRV);
82
83         if (netif_carrier_ok(net)) {
84                 /* Open up the device */
85                 ret = RndisFilterOnOpen(device_obj);
86                 if (ret != 0) {
87                         DPRINT_ERR(NETVSC_DRV,
88                                    "unable to open device (ret %d).", ret);
89                         return ret;
90                 }
91
92                 netif_start_queue(net);
93         } else {
94                 DPRINT_ERR(NETVSC_DRV, "unable to open device...link is down.");
95         }
96
97         DPRINT_EXIT(NETVSC_DRV);
98         return ret;
99 }
100
101 static int netvsc_close(struct net_device *net)
102 {
103         struct net_device_context *net_device_ctx = netdev_priv(net);
104         struct hv_device *device_obj = &net_device_ctx->device_ctx->device_obj;
105         int ret;
106
107         DPRINT_ENTER(NETVSC_DRV);
108
109         netif_stop_queue(net);
110
111         ret = RndisFilterOnClose(device_obj);
112         if (ret != 0)
113                 DPRINT_ERR(NETVSC_DRV, "unable to close device (ret %d).", ret);
114
115         DPRINT_EXIT(NETVSC_DRV);
116
117         return ret;
118 }
119
120 static void netvsc_xmit_completion(void *context)
121 {
122         struct hv_netvsc_packet *packet = (struct hv_netvsc_packet *)context;
123         struct sk_buff *skb = (struct sk_buff *)
124                 (unsigned long)packet->Completion.Send.SendCompletionTid;
125
126         DPRINT_ENTER(NETVSC_DRV);
127
128         kfree(packet);
129
130         if (skb) {
131                 struct net_device *net = skb->dev;
132                 struct net_device_context *net_device_ctx = netdev_priv(net);
133                 unsigned int num_pages = skb_shinfo(skb)->nr_frags + 2;
134
135                 dev_kfree_skb_any(skb);
136
137                 atomic_add(num_pages, &net_device_ctx->avail);
138                 if (atomic_read(&net_device_ctx->avail) >=
139                                 PACKET_PAGES_HIWATER)
140                         netif_wake_queue(net);
141         }
142
143         DPRINT_EXIT(NETVSC_DRV);
144 }
145
146 static int netvsc_start_xmit(struct sk_buff *skb, struct net_device *net)
147 {
148         struct net_device_context *net_device_ctx = netdev_priv(net);
149         struct driver_context *driver_ctx =
150             driver_to_driver_context(net_device_ctx->device_ctx->device.driver);
151         struct netvsc_driver_context *net_drv_ctx =
152                 (struct netvsc_driver_context *)driver_ctx;
153         struct netvsc_driver *net_drv_obj = &net_drv_ctx->drv_obj;
154         struct hv_netvsc_packet *packet;
155         int ret;
156         unsigned int i, num_pages;
157
158         DPRINT_ENTER(NETVSC_DRV);
159
160         DPRINT_DBG(NETVSC_DRV, "xmit packet - len %d data_len %d",
161                    skb->len, skb->data_len);
162
163         /* Add 1 for skb->data and additional one for RNDIS */
164         num_pages = skb_shinfo(skb)->nr_frags + 1 + 1;
165         if (num_pages > atomic_read(&net_device_ctx->avail))
166                 return NETDEV_TX_BUSY;
167
168         /* Allocate a netvsc packet based on # of frags. */
169         packet = kzalloc(sizeof(struct hv_netvsc_packet) +
170                          (num_pages * sizeof(struct hv_page_buffer)) +
171                          net_drv_obj->RequestExtSize, GFP_ATOMIC);
172         if (!packet) {
173                 /* out of memory, silently drop packet */
174                 DPRINT_ERR(NETVSC_DRV, "unable to allocate hv_netvsc_packet");
175
176                 dev_kfree_skb(skb);
177                 net->stats.tx_dropped++;
178                 return NETDEV_TX_OK;
179         }
180
181         packet->Extension = (void *)(unsigned long)packet +
182                                 sizeof(struct hv_netvsc_packet) +
183                                     (num_pages * sizeof(struct hv_page_buffer));
184
185         /* Setup the rndis header */
186         packet->PageBufferCount = num_pages;
187
188         /* TODO: Flush all write buffers/ memory fence ??? */
189         /* wmb(); */
190
191         /* Initialize it from the skb */
192         packet->TotalDataBufferLength   = skb->len;
193
194         /* Start filling in the page buffers starting after RNDIS buffer. */
195         packet->PageBuffers[1].Pfn = virt_to_phys(skb->data) >> PAGE_SHIFT;
196         packet->PageBuffers[1].Offset
197                 = (unsigned long)skb->data & (PAGE_SIZE - 1);
198         packet->PageBuffers[1].Length = skb_headlen(skb);
199
200         /* Additional fragments are after SKB data */
201         for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
202                 skb_frag_t *f = &skb_shinfo(skb)->frags[i];
203
204                 packet->PageBuffers[i+2].Pfn = page_to_pfn(f->page);
205                 packet->PageBuffers[i+2].Offset = f->page_offset;
206                 packet->PageBuffers[i+2].Length = f->size;
207         }
208
209         /* Set the completion routine */
210         packet->Completion.Send.OnSendCompletion = netvsc_xmit_completion;
211         packet->Completion.Send.SendCompletionContext = packet;
212         packet->Completion.Send.SendCompletionTid = (unsigned long)skb;
213
214         ret = net_drv_obj->OnSend(&net_device_ctx->device_ctx->device_obj,
215                                   packet);
216         if (ret == 0) {
217                 net->stats.tx_bytes += skb->len;
218                 net->stats.tx_packets++;
219
220                 DPRINT_DBG(NETVSC_DRV, "# of xmits %lu total size %lu",
221                            net->stats.tx_packets,
222                            net->stats.tx_bytes);
223
224                 atomic_sub(num_pages, &net_device_ctx->avail);
225                 if (atomic_read(&net_device_ctx->avail) < PACKET_PAGES_LOWATER)
226                         netif_stop_queue(net);
227         } else {
228                 /* we are shutting down or bus overloaded, just drop packet */
229                 net->stats.tx_dropped++;
230                 netvsc_xmit_completion(packet);
231         }
232
233         DPRINT_EXIT(NETVSC_DRV);
234         return NETDEV_TX_OK;
235 }
236
237 /*
238  * netvsc_linkstatus_callback - Link up/down notification
239  */
240 static void netvsc_linkstatus_callback(struct hv_device *device_obj,
241                                        unsigned int status)
242 {
243         struct vm_device *device_ctx = to_vm_device(device_obj);
244         struct net_device *net = dev_get_drvdata(&device_ctx->device);
245         struct net_device_context *ndev_ctx;
246
247         DPRINT_ENTER(NETVSC_DRV);
248
249         if (!net) {
250                 DPRINT_ERR(NETVSC_DRV, "got link status but net device "
251                                 "not initialized yet");
252                 return;
253         }
254
255         if (status == 1) {
256                 netif_carrier_on(net);
257                 netif_wake_queue(net);
258                 netif_notify_peers(net);
259                 ndev_ctx = netdev_priv(net);
260                 schedule_work(&ndev_ctx->work);
261         } else {
262                 netif_carrier_off(net);
263                 netif_stop_queue(net);
264         }
265         DPRINT_EXIT(NETVSC_DRV);
266 }
267
268 /*
269  * netvsc_recv_callback -  Callback when we receive a packet from the
270  * "wire" on the specified device.
271  */
272 static int netvsc_recv_callback(struct hv_device *device_obj,
273                                 struct hv_netvsc_packet *packet)
274 {
275         struct vm_device *device_ctx = to_vm_device(device_obj);
276         struct net_device *net = dev_get_drvdata(&device_ctx->device);
277         struct sk_buff *skb;
278         void *data;
279         int i;
280         unsigned long flags;
281
282         DPRINT_ENTER(NETVSC_DRV);
283
284         if (!net) {
285                 DPRINT_ERR(NETVSC_DRV, "got receive callback but net device "
286                                 "not initialized yet");
287                 return 0;
288         }
289
290         /* Allocate a skb - TODO direct I/O to pages? */
291         skb = netdev_alloc_skb_ip_align(net, packet->TotalDataBufferLength);
292         if (unlikely(!skb)) {
293                 ++net->stats.rx_dropped;
294                 return 0;
295         }
296
297         /* for kmap_atomic */
298         local_irq_save(flags);
299
300         /*
301          * Copy to skb. This copy is needed here since the memory pointed by
302          * hv_netvsc_packet cannot be deallocated
303          */
304         for (i = 0; i < packet->PageBufferCount; i++) {
305                 data = kmap_atomic(pfn_to_page(packet->PageBuffers[i].Pfn),
306                                                KM_IRQ1);
307                 data = (void *)(unsigned long)data +
308                                 packet->PageBuffers[i].Offset;
309
310                 memcpy(skb_put(skb, packet->PageBuffers[i].Length), data,
311                        packet->PageBuffers[i].Length);
312
313                 kunmap_atomic((void *)((unsigned long)data -
314                                        packet->PageBuffers[i].Offset), KM_IRQ1);
315         }
316
317         local_irq_restore(flags);
318
319         skb->protocol = eth_type_trans(skb, net);
320         skb->ip_summed = CHECKSUM_NONE;
321
322         net->stats.rx_packets++;
323         net->stats.rx_bytes += skb->len;
324
325         /*
326          * Pass the skb back up. Network stack will deallocate the skb when it
327          * is done.
328          * TODO - use NAPI?
329          */
330         netif_rx(skb);
331
332         DPRINT_DBG(NETVSC_DRV, "# of recvs %lu total size %lu",
333                    net->stats.rx_packets, net->stats.rx_bytes);
334
335         DPRINT_EXIT(NETVSC_DRV);
336
337         return 0;
338 }
339
340 static void netvsc_get_drvinfo(struct net_device *net,
341                                struct ethtool_drvinfo *info)
342 {
343         strcpy(info->driver, "hv_netvsc");
344         strcpy(info->version, HV_DRV_VERSION);
345         strcpy(info->fw_version, "N/A");
346 }
347
348 static const struct ethtool_ops ethtool_ops = {
349         .get_drvinfo    = netvsc_get_drvinfo,
350         .get_sg         = ethtool_op_get_sg,
351         .set_sg         = ethtool_op_set_sg,
352         .get_link       = ethtool_op_get_link,
353 };
354
355 static const struct net_device_ops device_ops = {
356         .ndo_open =                     netvsc_open,
357         .ndo_stop =                     netvsc_close,
358         .ndo_start_xmit =               netvsc_start_xmit,
359         .ndo_set_multicast_list =       netvsc_set_multicast_list,
360         .ndo_change_mtu =               eth_change_mtu,
361         .ndo_validate_addr =            eth_validate_addr,
362         .ndo_set_mac_address =          eth_mac_addr,
363 };
364
365 /*
366  * Send GARP packet to network peers after migrations.
367  * After Quick Migration, the network is not immediately operational in the
368  * current context when receiving RNDIS_STATUS_MEDIA_CONNECT event. So, add
369  * another netif_notify_peers() into a scheduled work, otherwise GARP packet
370  * will not be sent after quick migration, and cause network disconnection.
371  */
372 static void netvsc_send_garp(struct work_struct *w)
373 {
374         struct net_device_context *ndev_ctx;
375         struct net_device *net;
376
377         msleep(20);
378         ndev_ctx = container_of(w, struct net_device_context, work);
379         net = dev_get_drvdata(&ndev_ctx->device_ctx->device);
380         netif_notify_peers(net);
381 }
382
383
384 static int netvsc_probe(struct device *device)
385 {
386         struct driver_context *driver_ctx =
387                 driver_to_driver_context(device->driver);
388         struct netvsc_driver_context *net_drv_ctx =
389                 (struct netvsc_driver_context *)driver_ctx;
390         struct netvsc_driver *net_drv_obj = &net_drv_ctx->drv_obj;
391         struct vm_device *device_ctx = device_to_vm_device(device);
392         struct hv_device *device_obj = &device_ctx->device_obj;
393         struct net_device *net = NULL;
394         struct net_device_context *net_device_ctx;
395         struct netvsc_device_info device_info;
396         int ret;
397
398         DPRINT_ENTER(NETVSC_DRV);
399
400         if (!net_drv_obj->Base.OnDeviceAdd)
401                 return -1;
402
403         net = alloc_etherdev(sizeof(struct net_device_context));
404         if (!net)
405                 return -1;
406
407         /* Set initial state */
408         netif_carrier_off(net);
409         netif_stop_queue(net);
410
411         net_device_ctx = netdev_priv(net);
412         net_device_ctx->device_ctx = device_ctx;
413         atomic_set(&net_device_ctx->avail, ring_size);
414         dev_set_drvdata(device, net);
415         INIT_WORK(&net_device_ctx->work, netvsc_send_garp);
416
417         /* Notify the netvsc driver of the new device */
418         ret = net_drv_obj->Base.OnDeviceAdd(device_obj, &device_info);
419         if (ret != 0) {
420                 free_netdev(net);
421                 dev_set_drvdata(device, NULL);
422
423                 DPRINT_ERR(NETVSC_DRV, "unable to add netvsc device (ret %d)",
424                            ret);
425                 return ret;
426         }
427
428         /*
429          * If carrier is still off ie we did not get a link status callback,
430          * update it if necessary
431          */
432         /*
433          * FIXME: We should use a atomic or test/set instead to avoid getting
434          * out of sync with the device's link status
435          */
436         if (!netif_carrier_ok(net))
437                 if (!device_info.LinkState)
438                         netif_carrier_on(net);
439
440         memcpy(net->dev_addr, device_info.MacAddr, ETH_ALEN);
441
442         net->netdev_ops = &device_ops;
443
444         /* TODO: Add GSO and Checksum offload */
445         net->features = NETIF_F_SG;
446
447         SET_ETHTOOL_OPS(net, &ethtool_ops);
448         SET_NETDEV_DEV(net, device);
449
450         ret = register_netdev(net);
451         if (ret != 0) {
452                 /* Remove the device and release the resource */
453                 net_drv_obj->Base.OnDeviceRemove(device_obj);
454                 free_netdev(net);
455         }
456
457         DPRINT_EXIT(NETVSC_DRV);
458         return ret;
459 }
460
461 static int netvsc_remove(struct device *device)
462 {
463         struct driver_context *driver_ctx =
464                 driver_to_driver_context(device->driver);
465         struct netvsc_driver_context *net_drv_ctx =
466                 (struct netvsc_driver_context *)driver_ctx;
467         struct netvsc_driver *net_drv_obj = &net_drv_ctx->drv_obj;
468         struct vm_device *device_ctx = device_to_vm_device(device);
469         struct net_device *net = dev_get_drvdata(&device_ctx->device);
470         struct hv_device *device_obj = &device_ctx->device_obj;
471         int ret;
472
473         DPRINT_ENTER(NETVSC_DRV);
474
475         if (net == NULL) {
476                 DPRINT_INFO(NETVSC, "no net device to remove");
477                 DPRINT_EXIT(NETVSC_DRV);
478                 return 0;
479         }
480
481         if (!net_drv_obj->Base.OnDeviceRemove) {
482                 DPRINT_EXIT(NETVSC_DRV);
483                 return -1;
484         }
485
486         /* Stop outbound asap */
487         netif_stop_queue(net);
488         /* netif_carrier_off(net); */
489
490         unregister_netdev(net);
491
492         /*
493          * Call to the vsc driver to let it know that the device is being
494          * removed
495          */
496         ret = net_drv_obj->Base.OnDeviceRemove(device_obj);
497         if (ret != 0) {
498                 /* TODO: */
499                 DPRINT_ERR(NETVSC, "unable to remove vsc device (ret %d)", ret);
500         }
501
502         free_netdev(net);
503         DPRINT_EXIT(NETVSC_DRV);
504         return ret;
505 }
506
507 static int netvsc_drv_exit_cb(struct device *dev, void *data)
508 {
509         struct device **curr = (struct device **)data;
510
511         *curr = dev;
512         /* stop iterating */
513         return 1;
514 }
515
516 static void netvsc_drv_exit(void)
517 {
518         struct netvsc_driver *netvsc_drv_obj = &g_netvsc_drv.drv_obj;
519         struct driver_context *drv_ctx = &g_netvsc_drv.drv_ctx;
520         struct device *current_dev;
521         int ret;
522
523         DPRINT_ENTER(NETVSC_DRV);
524
525         while (1) {
526                 current_dev = NULL;
527
528                 /* Get the device */
529                 ret = driver_for_each_device(&drv_ctx->driver, NULL,
530                                              &current_dev, netvsc_drv_exit_cb);
531                 if (ret)
532                         DPRINT_WARN(NETVSC_DRV,
533                                     "driver_for_each_device returned %d", ret);
534
535                 if (current_dev == NULL)
536                         break;
537
538                 /* Initiate removal from the top-down */
539                 DPRINT_INFO(NETVSC_DRV, "unregistering device (%p)...",
540                             current_dev);
541
542                 device_unregister(current_dev);
543         }
544
545         if (netvsc_drv_obj->Base.OnCleanup)
546                 netvsc_drv_obj->Base.OnCleanup(&netvsc_drv_obj->Base);
547
548         vmbus_child_driver_unregister(drv_ctx);
549
550         DPRINT_EXIT(NETVSC_DRV);
551
552         return;
553 }
554
555 static int netvsc_drv_init(int (*drv_init)(struct hv_driver *drv))
556 {
557         struct netvsc_driver *net_drv_obj = &g_netvsc_drv.drv_obj;
558         struct driver_context *drv_ctx = &g_netvsc_drv.drv_ctx;
559         int ret;
560
561         DPRINT_ENTER(NETVSC_DRV);
562
563         vmbus_get_interface(&net_drv_obj->Base.VmbusChannelInterface);
564
565         net_drv_obj->RingBufferSize = ring_size * PAGE_SIZE;
566         net_drv_obj->OnReceiveCallback = netvsc_recv_callback;
567         net_drv_obj->OnLinkStatusChanged = netvsc_linkstatus_callback;
568
569         /* Callback to client driver to complete the initialization */
570         drv_init(&net_drv_obj->Base);
571
572         drv_ctx->driver.name = net_drv_obj->Base.name;
573         memcpy(&drv_ctx->class_id, &net_drv_obj->Base.deviceType,
574                sizeof(struct hv_guid));
575
576         drv_ctx->probe = netvsc_probe;
577         drv_ctx->remove = netvsc_remove;
578
579         /* The driver belongs to vmbus */
580         ret = vmbus_child_driver_register(drv_ctx);
581
582         DPRINT_EXIT(NETVSC_DRV);
583
584         return ret;
585 }
586
587 static const struct dmi_system_id __initconst
588 hv_netvsc_dmi_table[] __maybe_unused  = {
589         {
590                 .ident = "Hyper-V",
591                 .matches = {
592                         DMI_MATCH(DMI_SYS_VENDOR, "Microsoft Corporation"),
593                         DMI_MATCH(DMI_PRODUCT_NAME, "Virtual Machine"),
594                         DMI_MATCH(DMI_BOARD_NAME, "Virtual Machine"),
595                 },
596         },
597         { },
598 };
599 MODULE_DEVICE_TABLE(dmi, hv_netvsc_dmi_table);
600
601 static int __init netvsc_init(void)
602 {
603         int ret;
604
605         DPRINT_ENTER(NETVSC_DRV);
606         DPRINT_INFO(NETVSC_DRV, "Netvsc initializing....");
607
608         if (!dmi_check_system(hv_netvsc_dmi_table))
609                 return -ENODEV;
610
611         ret = netvsc_drv_init(NetVscInitialize);
612
613         DPRINT_EXIT(NETVSC_DRV);
614
615         return ret;
616 }
617
618 static void __exit netvsc_exit(void)
619 {
620         DPRINT_ENTER(NETVSC_DRV);
621         netvsc_drv_exit();
622         DPRINT_EXIT(NETVSC_DRV);
623 }
624
625 static const struct pci_device_id __initconst
626 hv_netvsc_pci_table[] __maybe_unused = {
627         { PCI_DEVICE(0x1414, 0x5353) }, /* VGA compatible controller */
628         { 0 }
629 };
630 MODULE_DEVICE_TABLE(pci, hv_netvsc_pci_table);
631
632 MODULE_LICENSE("GPL");
633 MODULE_VERSION(HV_DRV_VERSION);
634 MODULE_DESCRIPTION("Microsoft Hyper-V network driver");
635
636 module_init(netvsc_init);
637 module_exit(netvsc_exit);