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