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