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