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