Merge branch 'for-linus' of git://git.kernel.dk/linux-block
[pandora-kernel.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 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
22
23 #include <linux/init.h>
24 #include <linux/atomic.h>
25 #include <linux/module.h>
26 #include <linux/highmem.h>
27 #include <linux/device.h>
28 #include <linux/io.h>
29 #include <linux/delay.h>
30 #include <linux/netdevice.h>
31 #include <linux/inetdevice.h>
32 #include <linux/etherdevice.h>
33 #include <linux/skbuff.h>
34 #include <linux/in.h>
35 #include <linux/slab.h>
36 #include <linux/dmi.h>
37 #include <linux/pci.h>
38 #include <net/arp.h>
39 #include <net/route.h>
40 #include <net/sock.h>
41 #include <net/pkt_sched.h>
42
43 #include "hyperv.h"
44 #include "hyperv_net.h"
45
46 struct net_device_context {
47         /* point back to our device context */
48         struct hv_device *device_ctx;
49         atomic_t avail;
50         struct delayed_work dwork;
51 };
52
53
54 #define PACKET_PAGES_LOWATER  8
55 /* Need this many pages to handle worst case fragmented packet */
56 #define PACKET_PAGES_HIWATER  (MAX_SKB_FRAGS + 2)
57
58 static int ring_size = 128;
59 module_param(ring_size, int, S_IRUGO);
60 MODULE_PARM_DESC(ring_size, "Ring buffer size (# of pages)");
61
62 /* no-op so the netdev core doesn't return -EINVAL when modifying the the
63  * multicast address list in SIOCADDMULTI. hv is setup to get all multicast
64  * when it calls RndisFilterOnOpen() */
65 static void netvsc_set_multicast_list(struct net_device *net)
66 {
67 }
68
69 static int netvsc_open(struct net_device *net)
70 {
71         struct net_device_context *net_device_ctx = netdev_priv(net);
72         struct hv_device *device_obj = net_device_ctx->device_ctx;
73         int ret = 0;
74
75         if (netif_carrier_ok(net)) {
76                 /* Open up the device */
77                 ret = rndis_filter_open(device_obj);
78                 if (ret != 0) {
79                         netdev_err(net, "unable to open device (ret %d).\n",
80                                    ret);
81                         return ret;
82                 }
83
84                 netif_start_queue(net);
85         } else {
86                 netdev_err(net, "unable to open device...link is down.\n");
87         }
88
89         return ret;
90 }
91
92 static int netvsc_close(struct net_device *net)
93 {
94         struct net_device_context *net_device_ctx = netdev_priv(net);
95         struct hv_device *device_obj = net_device_ctx->device_ctx;
96         int ret;
97
98         netif_stop_queue(net);
99
100         ret = rndis_filter_close(device_obj);
101         if (ret != 0)
102                 netdev_err(net, "unable to close device (ret %d).\n", ret);
103
104         return ret;
105 }
106
107 static void netvsc_xmit_completion(void *context)
108 {
109         struct hv_netvsc_packet *packet = (struct hv_netvsc_packet *)context;
110         struct sk_buff *skb = (struct sk_buff *)
111                 (unsigned long)packet->completion.send.send_completion_tid;
112
113         kfree(packet);
114
115         if (skb) {
116                 struct net_device *net = skb->dev;
117                 struct net_device_context *net_device_ctx = netdev_priv(net);
118                 unsigned int num_pages = skb_shinfo(skb)->nr_frags + 2;
119
120                 dev_kfree_skb_any(skb);
121
122                 atomic_add(num_pages, &net_device_ctx->avail);
123                 if (atomic_read(&net_device_ctx->avail) >=
124                                 PACKET_PAGES_HIWATER)
125                         netif_wake_queue(net);
126         }
127 }
128
129 static int netvsc_start_xmit(struct sk_buff *skb, struct net_device *net)
130 {
131         struct net_device_context *net_device_ctx = netdev_priv(net);
132         struct hv_netvsc_packet *packet;
133         int ret;
134         unsigned int i, num_pages;
135
136         /* Add 1 for skb->data and additional one for RNDIS */
137         num_pages = skb_shinfo(skb)->nr_frags + 1 + 1;
138         if (num_pages > atomic_read(&net_device_ctx->avail))
139                 return NETDEV_TX_BUSY;
140
141         /* Allocate a netvsc packet based on # of frags. */
142         packet = kzalloc(sizeof(struct hv_netvsc_packet) +
143                          (num_pages * sizeof(struct hv_page_buffer)) +
144                          sizeof(struct rndis_filter_packet), GFP_ATOMIC);
145         if (!packet) {
146                 /* out of memory, silently drop packet */
147                 netdev_err(net, "unable to allocate hv_netvsc_packet\n");
148
149                 dev_kfree_skb(skb);
150                 net->stats.tx_dropped++;
151                 return NETDEV_TX_OK;
152         }
153
154         packet->extension = (void *)(unsigned long)packet +
155                                 sizeof(struct hv_netvsc_packet) +
156                                     (num_pages * sizeof(struct hv_page_buffer));
157
158         /* Setup the rndis header */
159         packet->page_buf_cnt = num_pages;
160
161         /* Initialize it from the skb */
162         packet->total_data_buflen       = skb->len;
163
164         /* Start filling in the page buffers starting after RNDIS buffer. */
165         packet->page_buf[1].pfn = virt_to_phys(skb->data) >> PAGE_SHIFT;
166         packet->page_buf[1].offset
167                 = (unsigned long)skb->data & (PAGE_SIZE - 1);
168         packet->page_buf[1].len = skb_headlen(skb);
169
170         /* Additional fragments are after SKB data */
171         for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
172                 skb_frag_t *f = &skb_shinfo(skb)->frags[i];
173
174                 packet->page_buf[i+2].pfn = page_to_pfn(f->page);
175                 packet->page_buf[i+2].offset = f->page_offset;
176                 packet->page_buf[i+2].len = f->size;
177         }
178
179         /* Set the completion routine */
180         packet->completion.send.send_completion = netvsc_xmit_completion;
181         packet->completion.send.send_completion_ctx = packet;
182         packet->completion.send.send_completion_tid = (unsigned long)skb;
183
184         ret = rndis_filter_send(net_device_ctx->device_ctx,
185                                   packet);
186         if (ret == 0) {
187                 net->stats.tx_bytes += skb->len;
188                 net->stats.tx_packets++;
189
190                 atomic_sub(num_pages, &net_device_ctx->avail);
191                 if (atomic_read(&net_device_ctx->avail) < PACKET_PAGES_LOWATER)
192                         netif_stop_queue(net);
193         } else {
194                 /* we are shutting down or bus overloaded, just drop packet */
195                 net->stats.tx_dropped++;
196                 netvsc_xmit_completion(packet);
197         }
198
199         return NETDEV_TX_OK;
200 }
201
202 /*
203  * netvsc_linkstatus_callback - Link up/down notification
204  */
205 void netvsc_linkstatus_callback(struct hv_device *device_obj,
206                                        unsigned int status)
207 {
208         struct net_device *net = dev_get_drvdata(&device_obj->device);
209         struct net_device_context *ndev_ctx;
210
211         if (!net) {
212                 netdev_err(net, "got link status but net device "
213                                 "not initialized yet\n");
214                 return;
215         }
216
217         if (status == 1) {
218                 netif_carrier_on(net);
219                 netif_wake_queue(net);
220                 netif_notify_peers(net);
221                 ndev_ctx = netdev_priv(net);
222                 schedule_delayed_work(&ndev_ctx->dwork, msecs_to_jiffies(20));
223         } else {
224                 netif_carrier_off(net);
225                 netif_stop_queue(net);
226         }
227 }
228
229 /*
230  * netvsc_recv_callback -  Callback when we receive a packet from the
231  * "wire" on the specified device.
232  */
233 int netvsc_recv_callback(struct hv_device *device_obj,
234                                 struct hv_netvsc_packet *packet)
235 {
236         struct net_device *net = dev_get_drvdata(&device_obj->device);
237         struct sk_buff *skb;
238         void *data;
239         int i;
240         unsigned long flags;
241
242         if (!net) {
243                 netdev_err(net, "got receive callback but net device"
244                         " not initialized yet\n");
245                 return 0;
246         }
247
248         /* Allocate a skb - TODO direct I/O to pages? */
249         skb = netdev_alloc_skb_ip_align(net, packet->total_data_buflen);
250         if (unlikely(!skb)) {
251                 ++net->stats.rx_dropped;
252                 return 0;
253         }
254
255         /* for kmap_atomic */
256         local_irq_save(flags);
257
258         /*
259          * Copy to skb. This copy is needed here since the memory pointed by
260          * hv_netvsc_packet cannot be deallocated
261          */
262         for (i = 0; i < packet->page_buf_cnt; i++) {
263                 data = kmap_atomic(pfn_to_page(packet->page_buf[i].pfn),
264                                                KM_IRQ1);
265                 data = (void *)(unsigned long)data +
266                                 packet->page_buf[i].offset;
267
268                 memcpy(skb_put(skb, packet->page_buf[i].len), data,
269                        packet->page_buf[i].len);
270
271                 kunmap_atomic((void *)((unsigned long)data -
272                                        packet->page_buf[i].offset), KM_IRQ1);
273         }
274
275         local_irq_restore(flags);
276
277         skb->protocol = eth_type_trans(skb, net);
278         skb->ip_summed = CHECKSUM_NONE;
279
280         net->stats.rx_packets++;
281         net->stats.rx_bytes += skb->len;
282
283         /*
284          * Pass the skb back up. Network stack will deallocate the skb when it
285          * is done.
286          * TODO - use NAPI?
287          */
288         netif_rx(skb);
289
290         return 0;
291 }
292
293 static void netvsc_get_drvinfo(struct net_device *net,
294                                struct ethtool_drvinfo *info)
295 {
296         strcpy(info->driver, "hv_netvsc");
297         strcpy(info->version, HV_DRV_VERSION);
298         strcpy(info->fw_version, "N/A");
299 }
300
301 static const struct ethtool_ops ethtool_ops = {
302         .get_drvinfo    = netvsc_get_drvinfo,
303         .get_link       = ethtool_op_get_link,
304 };
305
306 static const struct net_device_ops device_ops = {
307         .ndo_open =                     netvsc_open,
308         .ndo_stop =                     netvsc_close,
309         .ndo_start_xmit =               netvsc_start_xmit,
310         .ndo_set_multicast_list =       netvsc_set_multicast_list,
311         .ndo_change_mtu =               eth_change_mtu,
312         .ndo_validate_addr =            eth_validate_addr,
313         .ndo_set_mac_address =          eth_mac_addr,
314 };
315
316 /*
317  * Send GARP packet to network peers after migrations.
318  * After Quick Migration, the network is not immediately operational in the
319  * current context when receiving RNDIS_STATUS_MEDIA_CONNECT event. So, add
320  * another netif_notify_peers() into a delayed work, otherwise GARP packet
321  * will not be sent after quick migration, and cause network disconnection.
322  */
323 static void netvsc_send_garp(struct work_struct *w)
324 {
325         struct net_device_context *ndev_ctx;
326         struct net_device *net;
327
328         ndev_ctx = container_of(w, struct net_device_context, dwork.work);
329         net = dev_get_drvdata(&ndev_ctx->device_ctx->device);
330         netif_notify_peers(net);
331 }
332
333
334 static int netvsc_probe(struct hv_device *dev)
335 {
336         struct net_device *net = NULL;
337         struct net_device_context *net_device_ctx;
338         struct netvsc_device_info device_info;
339         int ret;
340
341         net = alloc_etherdev(sizeof(struct net_device_context));
342         if (!net)
343                 return -1;
344
345         /* Set initial state */
346         netif_carrier_off(net);
347
348         net_device_ctx = netdev_priv(net);
349         net_device_ctx->device_ctx = dev;
350         atomic_set(&net_device_ctx->avail, ring_size);
351         dev_set_drvdata(&dev->device, net);
352         INIT_DELAYED_WORK(&net_device_ctx->dwork, netvsc_send_garp);
353
354         /* Notify the netvsc driver of the new device */
355         device_info.ring_size = ring_size;
356         ret = rndis_filter_device_add(dev, &device_info);
357         if (ret != 0) {
358                 free_netdev(net);
359                 dev_set_drvdata(&dev->device, NULL);
360
361                 netdev_err(net, "unable to add netvsc device (ret %d)\n", ret);
362                 return ret;
363         }
364
365         netif_carrier_on(net);
366
367         memcpy(net->dev_addr, device_info.mac_adr, ETH_ALEN);
368
369         net->netdev_ops = &device_ops;
370
371         /* TODO: Add GSO and Checksum offload */
372         net->hw_features = NETIF_F_SG;
373         net->features = NETIF_F_SG;
374
375         SET_ETHTOOL_OPS(net, &ethtool_ops);
376         SET_NETDEV_DEV(net, &dev->device);
377
378         ret = register_netdev(net);
379         if (ret != 0) {
380                 /* Remove the device and release the resource */
381                 rndis_filter_device_remove(dev);
382                 free_netdev(net);
383         }
384
385         return ret;
386 }
387
388 static int netvsc_remove(struct hv_device *dev)
389 {
390         struct net_device *net = dev_get_drvdata(&dev->device);
391         struct net_device_context *ndev_ctx;
392
393         if (net == NULL) {
394                 dev_err(&dev->device, "No net device to remove\n");
395                 return 0;
396         }
397
398         ndev_ctx = netdev_priv(net);
399         cancel_delayed_work_sync(&ndev_ctx->dwork);
400
401         /* Stop outbound asap */
402         netif_stop_queue(net);
403
404         unregister_netdev(net);
405
406         /*
407          * Call to the vsc driver to let it know that the device is being
408          * removed
409          */
410         rndis_filter_device_remove(dev);
411
412         free_netdev(net);
413         return 0;
414 }
415
416 /* The one and only one */
417 static struct  hv_driver netvsc_drv = {
418         .probe = netvsc_probe,
419         .remove = netvsc_remove,
420 };
421
422 static void __exit netvsc_drv_exit(void)
423 {
424         vmbus_child_driver_unregister(&netvsc_drv.driver);
425 }
426
427
428 static const struct dmi_system_id __initconst
429 hv_netvsc_dmi_table[] __maybe_unused  = {
430         {
431                 .ident = "Hyper-V",
432                 .matches = {
433                         DMI_MATCH(DMI_SYS_VENDOR, "Microsoft Corporation"),
434                         DMI_MATCH(DMI_PRODUCT_NAME, "Virtual Machine"),
435                         DMI_MATCH(DMI_BOARD_NAME, "Virtual Machine"),
436                 },
437         },
438         { },
439 };
440 MODULE_DEVICE_TABLE(dmi, hv_netvsc_dmi_table);
441
442 static int __init netvsc_drv_init(void)
443 {
444         struct hv_driver *drv = &netvsc_drv;
445         int ret;
446
447         pr_info("initializing....");
448
449         if (!dmi_check_system(hv_netvsc_dmi_table))
450                 return -ENODEV;
451
452
453         /* Callback to client driver to complete the initialization */
454         netvsc_initialize(drv);
455
456         drv->driver.name = drv->name;
457
458         /* The driver belongs to vmbus */
459         ret = vmbus_child_driver_register(&drv->driver);
460
461         return ret;
462 }
463
464 static const struct pci_device_id __initconst
465 hv_netvsc_pci_table[] __maybe_unused = {
466         { PCI_DEVICE(0x1414, 0x5353) }, /* VGA compatible controller */
467         { 0 }
468 };
469 MODULE_DEVICE_TABLE(pci, hv_netvsc_pci_table);
470
471 MODULE_LICENSE("GPL");
472 MODULE_VERSION(HV_DRV_VERSION);
473 MODULE_DESCRIPTION("Microsoft Hyper-V network driver");
474
475 module_init(netvsc_drv_init);
476 module_exit(netvsc_drv_exit);