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