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