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