Staging: hv: move osd.h
[safe/jmp/linux-2.6] / drivers / staging / hv / netvsc_drv.c
1 /*
2  *
3  * Copyright (c) 2009, Microsoft Corporation.
4  *
5  * This program is free software; you can redistribute it and/or modify it
6  * under the terms and conditions of the GNU General Public License,
7  * version 2, as published by the Free Software Foundation.
8  *
9  * This program is distributed in the hope it will be useful, but WITHOUT
10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
12  * more details.
13  *
14  * You should have received a copy of the GNU General Public License along with
15  * this program; if not, write to the Free Software Foundation, Inc., 59 Temple
16  * Place - Suite 330, Boston, MA 02111-1307 USA.
17  *
18  * Authors:
19  *   Hank Janssen  <hjanssen@microsoft.com>
20  *
21  */
22
23 #include <linux/init.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/in.h>
34 #include <net/arp.h>
35 #include <net/route.h>
36 #include <net/sock.h>
37 #include <net/pkt_sched.h>
38
39 #include "osd.h"
40 #include "include/logging.h"
41 #include "include/vmbus.h"
42
43 #include "include/NetVscApi.h"
44
45 MODULE_LICENSE("GPL");
46
47
48 /* Static decl */
49
50 static int netvsc_probe(struct device *device);
51 static int netvsc_remove(struct device *device);
52 static int netvsc_open(struct net_device *net);
53 static void netvsc_xmit_completion(void *context);
54 static int netvsc_start_xmit (struct sk_buff *skb, struct net_device *net);
55 static int netvsc_recv_callback(struct hv_device *device_obj, struct hv_netvsc_packet *Packet);
56 static int netvsc_close(struct net_device *net);
57 static struct net_device_stats *netvsc_get_stats(struct net_device *net);
58 static void netvsc_linkstatus_callback(struct hv_device *device_obj, unsigned int status);
59
60
61 /* Data types */
62
63 struct net_device_context {
64         struct device_context   *device_ctx; /* point back to our device context */
65         struct net_device_stats stats;
66 };
67
68 struct netvsc_driver_context {
69         /* !! These must be the first 2 fields !! */
70         struct driver_context   drv_ctx;
71         NETVSC_DRIVER_OBJECT    drv_obj;
72 };
73
74
75 /* Globals */
76
77
78 static int netvsc_ringbuffer_size = NETVSC_DEVICE_RING_BUFFER_SIZE;
79
80 /* The one and only one */
81 static struct netvsc_driver_context g_netvsc_drv;
82
83
84 /* Routines */
85
86
87 /*++
88
89 Name:   netvsc_drv_init()
90
91 Desc:   NetVsc driver initialization
92
93 --*/
94 static int netvsc_drv_init(PFN_DRIVERINITIALIZE pfn_drv_init)
95 {
96         int ret=0;
97         NETVSC_DRIVER_OBJECT *net_drv_obj=&g_netvsc_drv.drv_obj;
98         struct driver_context *drv_ctx=&g_netvsc_drv.drv_ctx;
99
100         DPRINT_ENTER(NETVSC_DRV);
101
102         vmbus_get_interface(&net_drv_obj->Base.VmbusChannelInterface);
103
104         net_drv_obj->RingBufferSize = netvsc_ringbuffer_size;
105         net_drv_obj->OnReceiveCallback = netvsc_recv_callback;
106         net_drv_obj->OnLinkStatusChanged = netvsc_linkstatus_callback;
107
108         /* Callback to client driver to complete the initialization */
109         pfn_drv_init(&net_drv_obj->Base);
110
111         drv_ctx->driver.name = net_drv_obj->Base.name;
112         memcpy(&drv_ctx->class_id, &net_drv_obj->Base.deviceType, sizeof(GUID));
113
114         drv_ctx->probe = netvsc_probe;
115         drv_ctx->remove = netvsc_remove;
116
117         /* The driver belongs to vmbus */
118         ret = vmbus_child_driver_register(drv_ctx);
119
120         DPRINT_EXIT(NETVSC_DRV);
121
122         return ret;
123 }
124
125 /*++
126
127 Name:   netvsc_get_stats()
128
129 Desc:   Get the network stats
130
131 --*/
132 static struct net_device_stats *netvsc_get_stats(struct net_device *net)
133 {
134         struct net_device_context *net_device_ctx = netdev_priv(net);
135
136         return &net_device_ctx->stats;
137 }
138
139 /*++
140
141 Name:   netvsc_set_multicast_list()
142
143 Desc:   Set the multicast list
144
145 Remark: No-op here
146 --*/
147 static void netvsc_set_multicast_list(struct net_device *net)
148 {
149 }
150
151
152 static const struct net_device_ops device_ops = {
153         .ndo_open = netvsc_open,
154         .ndo_stop = netvsc_close,
155         .ndo_start_xmit = netvsc_start_xmit,
156         .ndo_get_stats = netvsc_get_stats,
157         .ndo_set_multicast_list = netvsc_set_multicast_list,
158 };
159
160 /*++
161
162 Name:   netvsc_probe()
163
164 Desc:   Add the specified new device to this driver
165
166 --*/
167 static int netvsc_probe(struct device *device)
168 {
169         int ret=0;
170
171         struct driver_context *driver_ctx = driver_to_driver_context(device->driver);
172         struct netvsc_driver_context *net_drv_ctx = (struct netvsc_driver_context*)driver_ctx;
173         NETVSC_DRIVER_OBJECT *net_drv_obj = &net_drv_ctx->drv_obj;
174
175         struct device_context *device_ctx = device_to_device_context(device);
176         struct hv_device *device_obj = &device_ctx->device_obj;
177
178         struct net_device *net = NULL;
179         struct net_device_context *net_device_ctx;
180         NETVSC_DEVICE_INFO device_info;
181
182         DPRINT_ENTER(NETVSC_DRV);
183
184         if (!net_drv_obj->Base.OnDeviceAdd)
185         {
186                 return -1;
187         }
188
189         net = alloc_netdev(sizeof(struct net_device_context), "seth%d", ether_setup);
190         /* net = alloc_etherdev(sizeof(struct net_device_context)); */
191         if (!net)
192         {
193                 return -1;
194         }
195
196         /* Set initial state */
197         netif_carrier_off(net);
198         netif_stop_queue(net);
199
200         net_device_ctx = netdev_priv(net);
201         net_device_ctx->device_ctx = device_ctx;
202         dev_set_drvdata(device, net);
203
204         /* Notify the netvsc driver of the new device */
205         ret = net_drv_obj->Base.OnDeviceAdd(device_obj, (void*)&device_info);
206         if (ret != 0)
207         {
208                 free_netdev(net);
209                 dev_set_drvdata(device, NULL);
210
211                 DPRINT_ERR(NETVSC_DRV, "unable to add netvsc device (ret %d)", ret);
212                 return ret;
213         }
214
215         /* If carrier is still off ie we did not get a link status callback, update it if necessary */
216         /* FIXME: We should use a atomic or test/set instead to avoid getting out of sync with the device's link status */
217         if (!netif_carrier_ok(net))
218         {
219                 if (!device_info.LinkState)
220                 {
221                         netif_carrier_on(net);
222                 }
223         }
224
225         memcpy(net->dev_addr, device_info.MacAddr, ETH_ALEN);
226
227         net->netdev_ops = &device_ops;
228
229         SET_NETDEV_DEV(net, device);
230
231         ret = register_netdev(net);
232         if (ret != 0)
233         {
234                 /* Remove the device and release the resource */
235                 net_drv_obj->Base.OnDeviceRemove(device_obj);
236                 free_netdev(net);
237         }
238
239         DPRINT_EXIT(NETVSC_DRV);
240
241         return ret;
242 }
243
244 static int netvsc_remove(struct device *device)
245 {
246         int ret=0;
247         struct driver_context *driver_ctx = driver_to_driver_context(device->driver);
248         struct netvsc_driver_context *net_drv_ctx = (struct netvsc_driver_context*)driver_ctx;
249         NETVSC_DRIVER_OBJECT *net_drv_obj = &net_drv_ctx->drv_obj;
250
251         struct device_context *device_ctx = device_to_device_context(device);
252         struct net_device *net = dev_get_drvdata(&device_ctx->device);
253         struct hv_device *device_obj = &device_ctx->device_obj;
254
255         DPRINT_ENTER(NETVSC_DRV);
256
257         if (net == NULL)
258         {
259                 DPRINT_INFO(NETVSC, "no net device to remove");
260                 DPRINT_EXIT(NETVSC_DRV);
261                 return 0;
262         }
263
264         if (!net_drv_obj->Base.OnDeviceRemove)
265         {
266                 DPRINT_EXIT(NETVSC_DRV);
267                 return -1;
268         }
269
270         /* Stop outbound asap */
271         netif_stop_queue(net);
272         /* netif_carrier_off(net); */
273
274         unregister_netdev(net);
275
276         /* Call to the vsc driver to let it know that the device is being removed */
277         ret = net_drv_obj->Base.OnDeviceRemove(device_obj);
278         if (ret != 0)
279         {
280                 /* TODO: */
281                 DPRINT_ERR(NETVSC, "unable to remove vsc device (ret %d)", ret);
282         }
283
284         free_netdev(net);
285
286         DPRINT_EXIT(NETVSC_DRV);
287
288         return ret;
289 }
290
291 /*++
292
293 Name:   netvsc_open()
294
295 Desc:   Open the specified interface device
296
297 --*/
298 static int netvsc_open(struct net_device *net)
299 {
300         int ret=0;
301         struct net_device_context *net_device_ctx = netdev_priv(net);
302         struct driver_context *driver_ctx = driver_to_driver_context(net_device_ctx->device_ctx->device.driver);
303         struct netvsc_driver_context *net_drv_ctx = (struct netvsc_driver_context*)driver_ctx;
304         NETVSC_DRIVER_OBJECT *net_drv_obj = &net_drv_ctx->drv_obj;
305
306         struct hv_device *device_obj = &net_device_ctx->device_ctx->device_obj;
307
308         DPRINT_ENTER(NETVSC_DRV);
309
310         if (netif_carrier_ok(net))
311         {
312                 memset(&net_device_ctx->stats, 0 , sizeof(struct net_device_stats));
313
314                 /* Open up the device */
315                 ret = net_drv_obj->OnOpen(device_obj);
316                 if (ret != 0)
317                 {
318                         DPRINT_ERR(NETVSC_DRV, "unable to open device (ret %d).", ret);
319                         return ret;
320                 }
321
322                 netif_start_queue(net);
323         }
324         else
325         {
326                 DPRINT_ERR(NETVSC_DRV, "unable to open device...link is down.");
327         }
328
329         DPRINT_EXIT(NETVSC_DRV);
330         return ret;
331 }
332
333 /*++
334
335 Name:   netvsc_close()
336
337 Desc:   Close the specified interface device
338
339 --*/
340 static int netvsc_close(struct net_device *net)
341 {
342         int ret=0;
343         struct net_device_context *net_device_ctx = netdev_priv(net);
344         struct driver_context *driver_ctx = driver_to_driver_context(net_device_ctx->device_ctx->device.driver);
345         struct netvsc_driver_context *net_drv_ctx = (struct netvsc_driver_context*)driver_ctx;
346         NETVSC_DRIVER_OBJECT *net_drv_obj = &net_drv_ctx->drv_obj;
347
348         struct hv_device *device_obj = &net_device_ctx->device_ctx->device_obj;
349
350         DPRINT_ENTER(NETVSC_DRV);
351
352         netif_stop_queue(net);
353
354         ret = net_drv_obj->OnClose(device_obj);
355         if (ret != 0)
356         {
357                 DPRINT_ERR(NETVSC_DRV, "unable to close device (ret %d).", ret);
358         }
359
360         DPRINT_EXIT(NETVSC_DRV);
361
362         return ret;
363 }
364
365
366 /*++
367
368 Name:   netvsc_xmit_completion()
369
370 Desc:   Send completion processing
371
372 --*/
373 static void netvsc_xmit_completion(void *context)
374 {
375         struct hv_netvsc_packet *packet = (struct hv_netvsc_packet *)context;
376         struct sk_buff *skb = (struct sk_buff *)(unsigned long)packet->Completion.Send.SendCompletionTid;
377         struct net_device* net;
378
379         DPRINT_ENTER(NETVSC_DRV);
380
381         kfree(packet);
382
383         if (skb)
384         {
385                 net = skb->dev;
386
387                 dev_kfree_skb_any(skb);
388
389                 if (netif_queue_stopped(net))
390                 {
391                         DPRINT_INFO(NETVSC_DRV, "net device (%p) waking up...", net);
392
393                         netif_wake_queue(net);
394                 }
395         }
396
397         DPRINT_EXIT(NETVSC_DRV);
398 }
399
400 /*++
401
402 Name:   netvsc_start_xmit()
403
404 Desc:   Start a send
405
406 --*/
407 static int netvsc_start_xmit (struct sk_buff *skb, struct net_device *net)
408 {
409         int ret=0;
410         struct net_device_context *net_device_ctx = netdev_priv(net);
411         struct driver_context *driver_ctx = driver_to_driver_context(net_device_ctx->device_ctx->device.driver);
412         struct netvsc_driver_context *net_drv_ctx = (struct netvsc_driver_context*)driver_ctx;
413         NETVSC_DRIVER_OBJECT *net_drv_obj = &net_drv_ctx->drv_obj;
414
415         int i=0;
416         struct hv_netvsc_packet *packet;
417         int num_frags;
418         int retries=0;
419
420         DPRINT_ENTER(NETVSC_DRV);
421
422         /* Support only 1 chain of frags */
423         ASSERT(skb_shinfo(skb)->frag_list == NULL);
424         ASSERT(skb->dev == net);
425
426         DPRINT_DBG(NETVSC_DRV, "xmit packet - len %d data_len %d", skb->len, skb->data_len);
427
428         /* Add 1 for skb->data and any additional ones requested */
429         num_frags = skb_shinfo(skb)->nr_frags + 1 + net_drv_obj->AdditionalRequestPageBufferCount;
430
431         /* Allocate a netvsc packet based on # of frags. */
432         packet = kzalloc(sizeof(struct hv_netvsc_packet) + (num_frags * sizeof(PAGE_BUFFER)) + net_drv_obj->RequestExtSize, GFP_ATOMIC);
433         if (!packet)
434         {
435                 DPRINT_ERR(NETVSC_DRV, "unable to allocate hv_netvsc_packet");
436                 return -1;
437         }
438
439         packet->Extension = (void*)(unsigned long)packet + sizeof(struct hv_netvsc_packet) + (num_frags * sizeof(PAGE_BUFFER)) ;
440
441         /* Setup the rndis header */
442         packet->PageBufferCount = num_frags;
443
444         /* TODO: Flush all write buffers/ memory fence ??? */
445         /* wmb(); */
446
447         /* Initialize it from the skb */
448         ASSERT(skb->data);
449         packet->TotalDataBufferLength   = skb->len;
450
451         /* Start filling in the page buffers starting at AdditionalRequestPageBufferCount offset */
452         packet->PageBuffers[net_drv_obj->AdditionalRequestPageBufferCount].Pfn          = virt_to_phys(skb->data) >> PAGE_SHIFT;
453         packet->PageBuffers[net_drv_obj->AdditionalRequestPageBufferCount].Offset       = (unsigned long)skb->data & (PAGE_SIZE -1);
454         packet->PageBuffers[net_drv_obj->AdditionalRequestPageBufferCount].Length       = skb->len - skb->data_len;
455
456         ASSERT((skb->len - skb->data_len) <= PAGE_SIZE);
457
458         for (i=net_drv_obj->AdditionalRequestPageBufferCount+1; i<num_frags; i++)
459         {
460                 packet->PageBuffers[i].Pfn              = page_to_pfn(skb_shinfo(skb)->frags[i-(net_drv_obj->AdditionalRequestPageBufferCount+1)].page);
461                 packet->PageBuffers[i].Offset   = skb_shinfo(skb)->frags[i-(net_drv_obj->AdditionalRequestPageBufferCount+1)].page_offset;
462                 packet->PageBuffers[i].Length   = skb_shinfo(skb)->frags[i-(net_drv_obj->AdditionalRequestPageBufferCount+1)].size;
463         }
464
465         /* Set the completion routine */
466         packet->Completion.Send.OnSendCompletion = netvsc_xmit_completion;
467         packet->Completion.Send.SendCompletionContext = packet;
468         packet->Completion.Send.SendCompletionTid = (unsigned long)skb;
469
470 retry_send:
471         ret = net_drv_obj->OnSend(&net_device_ctx->device_ctx->device_obj, packet);
472
473         if (ret == 0)
474         {
475                 ret = NETDEV_TX_OK;
476                 net_device_ctx->stats.tx_bytes += skb->len;
477                 net_device_ctx->stats.tx_packets++;
478         }
479         else
480         {
481                 retries++;
482                 if (retries < 4)
483                 {
484                         DPRINT_ERR(NETVSC_DRV, "unable to send...retrying %d...", retries);
485                         udelay(100);
486                         goto retry_send;
487                 }
488
489                 /* no more room or we are shutting down */
490                 DPRINT_ERR(NETVSC_DRV, "unable to send (%d)...marking net device (%p) busy", ret, net);
491                 DPRINT_INFO(NETVSC_DRV, "net device (%p) stopping", net);
492
493                 ret = NETDEV_TX_BUSY;
494                 net_device_ctx->stats.tx_dropped++;
495
496                 netif_stop_queue(net);
497
498                 /* Null it since the caller will free it instead of the completion routine */
499                 packet->Completion.Send.SendCompletionTid = 0;
500
501                 /* Release the resources since we will not get any send completion */
502                 netvsc_xmit_completion((void*)packet);
503         }
504
505         DPRINT_DBG(NETVSC_DRV, "# of xmits %lu total size %lu", net_device_ctx->stats.tx_packets, net_device_ctx->stats.tx_bytes);
506
507         DPRINT_EXIT(NETVSC_DRV);
508         return ret;
509 }
510
511
512 /*++
513
514 Name:   netvsc_linkstatus_callback()
515
516 Desc:   Link up/down notification
517
518 --*/
519 static void netvsc_linkstatus_callback(struct hv_device *device_obj, unsigned int status)
520 {
521         struct device_context* device_ctx = to_device_context(device_obj);
522         struct net_device* net = dev_get_drvdata(&device_ctx->device);
523
524         DPRINT_ENTER(NETVSC_DRV);
525
526         if (!net)
527         {
528                 DPRINT_ERR(NETVSC_DRV, "got link status but net device not initialized yet");
529                 return;
530         }
531
532         if (status == 1)
533         {
534                 netif_carrier_on(net);
535                 netif_wake_queue(net);
536         }
537         else
538         {
539                 netif_carrier_off(net);
540                 netif_stop_queue(net);
541         }
542         DPRINT_EXIT(NETVSC_DRV);
543 }
544
545
546 /*++
547
548 Name:   netvsc_recv_callback()
549
550 Desc:   Callback when we receive a packet from the "wire" on the specify device
551
552 --*/
553 static int netvsc_recv_callback(struct hv_device *device_obj, struct hv_netvsc_packet *packet)
554 {
555         int ret=0;
556         struct device_context *device_ctx = to_device_context(device_obj);
557         struct net_device *net = dev_get_drvdata(&device_ctx->device);
558         struct net_device_context *net_device_ctx;
559
560         struct sk_buff *skb;
561         void *data;
562         int i=0;
563         unsigned long flags;
564
565         DPRINT_ENTER(NETVSC_DRV);
566
567         if (!net)
568         {
569                 DPRINT_ERR(NETVSC_DRV, "got receive callback but net device not initialized yet");
570                 return 0;
571         }
572
573         net_device_ctx = netdev_priv(net);
574
575         /* Allocate a skb - TODO preallocate this */
576         /* skb = alloc_skb(packet->TotalDataBufferLength, GFP_ATOMIC); */
577         skb = dev_alloc_skb(packet->TotalDataBufferLength + 2); /* Pad 2-bytes to align IP header to 16 bytes */
578         ASSERT(skb);
579         skb_reserve(skb, 2);
580         skb->dev = net;
581
582         /* for kmap_atomic */
583         local_irq_save(flags);
584
585         /* Copy to skb. This copy is needed here since the memory pointed by hv_netvsc_packet */
586         /* cannot be deallocated */
587         for (i=0; i<packet->PageBufferCount; i++)
588         {
589             data = kmap_atomic(pfn_to_page(packet->PageBuffers[i].Pfn), KM_IRQ1);
590             data = (void*)(unsigned long)data + packet->PageBuffers[i].Offset;
591
592             memcpy(skb_put(skb, packet->PageBuffers[i].Length), data, packet->PageBuffers[i].Length);
593
594             kunmap_atomic((void*)((unsigned long)data - packet->PageBuffers[i].Offset), KM_IRQ1);
595         }
596
597         local_irq_restore(flags);
598
599         skb->protocol = eth_type_trans(skb, net);
600
601         skb->ip_summed = CHECKSUM_NONE;
602
603         /* Pass the skb back up. Network stack will deallocate the skb when it is done */
604         ret = netif_rx(skb);
605
606         switch (ret)
607         {
608         case NET_RX_DROP:
609                 net_device_ctx->stats.rx_dropped++;
610                 break;
611         default:
612                 net_device_ctx->stats.rx_packets++;
613                 net_device_ctx->stats.rx_bytes += skb->len;
614                 break;
615
616         }
617         DPRINT_DBG(NETVSC_DRV, "# of recvs %lu total size %lu", net_device_ctx->stats.rx_packets, net_device_ctx->stats.rx_bytes);
618
619         DPRINT_EXIT(NETVSC_DRV);
620
621         return 0;
622 }
623
624 static int netvsc_drv_exit_cb(struct device *dev, void *data)
625 {
626         struct device **curr = (struct device **)data;
627         *curr = dev;
628         return 1; /* stop iterating */
629 }
630
631 /*++
632
633 Name:   netvsc_drv_exit()
634
635 Desc:
636
637 --*/
638 static void netvsc_drv_exit(void)
639 {
640         NETVSC_DRIVER_OBJECT *netvsc_drv_obj=&g_netvsc_drv.drv_obj;
641         struct driver_context *drv_ctx=&g_netvsc_drv.drv_ctx;
642         struct device *current_dev=NULL;
643         int ret;
644
645         DPRINT_ENTER(NETVSC_DRV);
646
647         while (1)
648         {
649                 current_dev = NULL;
650
651                 /* Get the device */
652                 ret = driver_for_each_device(&drv_ctx->driver, NULL,
653                                              (void *) &current_dev,
654                                              netvsc_drv_exit_cb);
655
656                 if (ret)
657                         DPRINT_WARN(NETVSC_DRV,
658                                     "driver_for_each_device returned %d", ret);
659
660
661                 if (current_dev == NULL)
662                         break;
663
664                 /* Initiate removal from the top-down */
665                 DPRINT_INFO(NETVSC_DRV, "unregistering device (%p)...", current_dev);
666
667                 device_unregister(current_dev);
668         }
669
670         if (netvsc_drv_obj->Base.OnCleanup)
671                 netvsc_drv_obj->Base.OnCleanup(&netvsc_drv_obj->Base);
672
673         vmbus_child_driver_unregister(drv_ctx);
674
675         DPRINT_EXIT(NETVSC_DRV);
676
677         return;
678 }
679
680 static int __init netvsc_init(void)
681 {
682         int ret;
683
684         DPRINT_ENTER(NETVSC_DRV);
685         DPRINT_INFO(NETVSC_DRV, "Netvsc initializing....");
686
687         ret = netvsc_drv_init(NetVscInitialize);
688
689         DPRINT_EXIT(NETVSC_DRV);
690
691         return ret;
692 }
693
694 static void __exit netvsc_exit(void)
695 {
696         DPRINT_ENTER(NETVSC_DRV);
697
698         netvsc_drv_exit();
699
700         DPRINT_EXIT(NETVSC_DRV);
701 }
702
703 module_param(netvsc_ringbuffer_size, int, S_IRUGO);
704
705 module_init(netvsc_init);
706 module_exit(netvsc_exit);