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