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