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