ACPI: thinkpad-acpi: add development version tag
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / staging / hv / netvsc_drv.c
blob44d8d6fa93dc78c0ef52256c112a9b515ee1cb69
1 /*
2 * Copyright (c) 2009, Microsoft Corporation.
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.
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.
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.
17 * Authors:
18 * Haiyang Zhang <haiyangz@microsoft.com>
19 * Hank Janssen <hjanssen@microsoft.com>
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 <net/arp.h>
33 #include <net/route.h>
34 #include <net/sock.h>
35 #include <net/pkt_sched.h>
36 #include "osd.h"
37 #include "logging.h"
38 #include "vmbus.h"
39 #include "NetVscApi.h"
41 MODULE_LICENSE("GPL");
43 struct net_device_context {
44 /* point back to our device context */
45 struct device_context *device_ctx;
46 struct net_device_stats stats;
47 struct work_struct work;
50 struct netvsc_driver_context {
51 /* !! These must be the first 2 fields !! */
52 /* Which is a bug FIXME! */
53 struct driver_context drv_ctx;
54 struct netvsc_driver drv_obj;
57 static int netvsc_ringbuffer_size = NETVSC_DEVICE_RING_BUFFER_SIZE;
59 /* The one and only one */
60 static struct netvsc_driver_context g_netvsc_drv;
62 static struct net_device_stats *netvsc_get_stats(struct net_device *net)
64 struct net_device_context *net_device_ctx = netdev_priv(net);
66 return &net_device_ctx->stats;
69 static void netvsc_set_multicast_list(struct net_device *net)
73 static int netvsc_open(struct net_device *net)
75 struct net_device_context *net_device_ctx = netdev_priv(net);
76 struct driver_context *driver_ctx =
77 driver_to_driver_context(net_device_ctx->device_ctx->device.driver);
78 struct netvsc_driver_context *net_drv_ctx =
79 (struct netvsc_driver_context *)driver_ctx;
80 struct netvsc_driver *net_drv_obj = &net_drv_ctx->drv_obj;
81 struct hv_device *device_obj = &net_device_ctx->device_ctx->device_obj;
82 int ret = 0;
84 DPRINT_ENTER(NETVSC_DRV);
86 if (netif_carrier_ok(net)) {
87 memset(&net_device_ctx->stats, 0,
88 sizeof(struct net_device_stats));
90 /* Open up the device */
91 ret = net_drv_obj->OnOpen(device_obj);
92 if (ret != 0) {
93 DPRINT_ERR(NETVSC_DRV,
94 "unable to open device (ret %d).", ret);
95 return ret;
98 netif_start_queue(net);
99 } else {
100 DPRINT_ERR(NETVSC_DRV, "unable to open device...link is down.");
103 DPRINT_EXIT(NETVSC_DRV);
104 return ret;
107 static int netvsc_close(struct net_device *net)
109 struct net_device_context *net_device_ctx = netdev_priv(net);
110 struct driver_context *driver_ctx =
111 driver_to_driver_context(net_device_ctx->device_ctx->device.driver);
112 struct netvsc_driver_context *net_drv_ctx =
113 (struct netvsc_driver_context *)driver_ctx;
114 struct netvsc_driver *net_drv_obj = &net_drv_ctx->drv_obj;
115 struct hv_device *device_obj = &net_device_ctx->device_ctx->device_obj;
116 int ret;
118 DPRINT_ENTER(NETVSC_DRV);
120 netif_stop_queue(net);
122 ret = net_drv_obj->OnClose(device_obj);
123 if (ret != 0)
124 DPRINT_ERR(NETVSC_DRV, "unable to close device (ret %d).", ret);
126 DPRINT_EXIT(NETVSC_DRV);
128 return ret;
131 static void netvsc_xmit_completion(void *context)
133 struct hv_netvsc_packet *packet = (struct hv_netvsc_packet *)context;
134 struct sk_buff *skb = (struct sk_buff *)
135 (unsigned long)packet->Completion.Send.SendCompletionTid;
136 struct net_device *net;
138 DPRINT_ENTER(NETVSC_DRV);
140 kfree(packet);
142 if (skb) {
143 net = skb->dev;
144 dev_kfree_skb_any(skb);
146 if (netif_queue_stopped(net)) {
147 DPRINT_INFO(NETVSC_DRV, "net device (%p) waking up...",
148 net);
150 netif_wake_queue(net);
154 DPRINT_EXIT(NETVSC_DRV);
157 static int netvsc_start_xmit(struct sk_buff *skb, struct net_device *net)
159 struct net_device_context *net_device_ctx = netdev_priv(net);
160 struct driver_context *driver_ctx =
161 driver_to_driver_context(net_device_ctx->device_ctx->device.driver);
162 struct netvsc_driver_context *net_drv_ctx =
163 (struct netvsc_driver_context *)driver_ctx;
164 struct netvsc_driver *net_drv_obj = &net_drv_ctx->drv_obj;
165 struct hv_netvsc_packet *packet;
166 int i;
167 int ret;
168 int num_frags;
169 int retries = 0;
171 DPRINT_ENTER(NETVSC_DRV);
173 /* Support only 1 chain of frags */
174 ASSERT(skb_shinfo(skb)->frag_list == NULL);
175 ASSERT(skb->dev == net);
177 DPRINT_DBG(NETVSC_DRV, "xmit packet - len %d data_len %d",
178 skb->len, skb->data_len);
180 /* Add 1 for skb->data and any additional ones requested */
181 num_frags = skb_shinfo(skb)->nr_frags + 1 +
182 net_drv_obj->AdditionalRequestPageBufferCount;
184 /* Allocate a netvsc packet based on # of frags. */
185 packet = kzalloc(sizeof(struct hv_netvsc_packet) +
186 (num_frags * sizeof(struct hv_page_buffer)) +
187 net_drv_obj->RequestExtSize, GFP_ATOMIC);
188 if (!packet) {
189 DPRINT_ERR(NETVSC_DRV, "unable to allocate hv_netvsc_packet");
190 return -1;
193 packet->Extension = (void *)(unsigned long)packet +
194 sizeof(struct hv_netvsc_packet) +
195 (num_frags * sizeof(struct hv_page_buffer));
197 /* Setup the rndis header */
198 packet->PageBufferCount = num_frags;
200 /* TODO: Flush all write buffers/ memory fence ??? */
201 /* wmb(); */
203 /* Initialize it from the skb */
204 ASSERT(skb->data);
205 packet->TotalDataBufferLength = skb->len;
208 * Start filling in the page buffers starting at
209 * AdditionalRequestPageBufferCount offset
211 packet->PageBuffers[net_drv_obj->AdditionalRequestPageBufferCount].Pfn = virt_to_phys(skb->data) >> PAGE_SHIFT;
212 packet->PageBuffers[net_drv_obj->AdditionalRequestPageBufferCount].Offset = (unsigned long)skb->data & (PAGE_SIZE - 1);
213 packet->PageBuffers[net_drv_obj->AdditionalRequestPageBufferCount].Length = skb->len - skb->data_len;
215 ASSERT((skb->len - skb->data_len) <= PAGE_SIZE);
217 for (i = net_drv_obj->AdditionalRequestPageBufferCount + 1;
218 i < num_frags; i++) {
219 packet->PageBuffers[i].Pfn =
220 page_to_pfn(skb_shinfo(skb)->frags[i-(net_drv_obj->AdditionalRequestPageBufferCount+1)].page);
221 packet->PageBuffers[i].Offset =
222 skb_shinfo(skb)->frags[i-(net_drv_obj->AdditionalRequestPageBufferCount+1)].page_offset;
223 packet->PageBuffers[i].Length =
224 skb_shinfo(skb)->frags[i-(net_drv_obj->AdditionalRequestPageBufferCount+1)].size;
227 /* Set the completion routine */
228 packet->Completion.Send.OnSendCompletion = netvsc_xmit_completion;
229 packet->Completion.Send.SendCompletionContext = packet;
230 packet->Completion.Send.SendCompletionTid = (unsigned long)skb;
232 retry_send:
233 ret = net_drv_obj->OnSend(&net_device_ctx->device_ctx->device_obj,
234 packet);
236 if (ret == 0) {
237 ret = NETDEV_TX_OK;
238 net_device_ctx->stats.tx_bytes += skb->len;
239 net_device_ctx->stats.tx_packets++;
240 } else {
241 retries++;
242 if (retries < 4) {
243 DPRINT_ERR(NETVSC_DRV, "unable to send..."
244 "retrying %d...", retries);
245 udelay(100);
246 goto retry_send;
249 /* no more room or we are shutting down */
250 DPRINT_ERR(NETVSC_DRV, "unable to send (%d)..."
251 "marking net device (%p) busy", ret, net);
252 DPRINT_INFO(NETVSC_DRV, "net device (%p) stopping", net);
254 ret = NETDEV_TX_BUSY;
255 net_device_ctx->stats.tx_dropped++;
257 netif_stop_queue(net);
260 * Null it since the caller will free it instead of the
261 * completion routine
263 packet->Completion.Send.SendCompletionTid = 0;
266 * Release the resources since we will not get any send
267 * completion
269 netvsc_xmit_completion((void *)packet);
272 DPRINT_DBG(NETVSC_DRV, "# of xmits %lu total size %lu",
273 net_device_ctx->stats.tx_packets,
274 net_device_ctx->stats.tx_bytes);
276 DPRINT_EXIT(NETVSC_DRV);
277 return ret;
281 * netvsc_linkstatus_callback - Link up/down notification
283 static void netvsc_linkstatus_callback(struct hv_device *device_obj,
284 unsigned int status)
286 struct device_context *device_ctx = to_device_context(device_obj);
287 struct net_device *net = dev_get_drvdata(&device_ctx->device);
288 struct net_device_context *ndev_ctx;
290 DPRINT_ENTER(NETVSC_DRV);
292 if (!net) {
293 DPRINT_ERR(NETVSC_DRV, "got link status but net device "
294 "not initialized yet");
295 return;
298 if (status == 1) {
299 netif_carrier_on(net);
300 netif_wake_queue(net);
301 netif_notify_peers(net);
302 ndev_ctx = netdev_priv(net);
303 schedule_work(&ndev_ctx->work);
304 } else {
305 netif_carrier_off(net);
306 netif_stop_queue(net);
308 DPRINT_EXIT(NETVSC_DRV);
312 * netvsc_recv_callback - Callback when we receive a packet from the "wire" on the specified device.
314 static int netvsc_recv_callback(struct hv_device *device_obj,
315 struct hv_netvsc_packet *packet)
317 struct device_context *device_ctx = to_device_context(device_obj);
318 struct net_device *net = dev_get_drvdata(&device_ctx->device);
319 struct net_device_context *net_device_ctx;
320 struct sk_buff *skb;
321 void *data;
322 int ret;
323 int i;
324 unsigned long flags;
326 DPRINT_ENTER(NETVSC_DRV);
328 if (!net) {
329 DPRINT_ERR(NETVSC_DRV, "got receive callback but net device "
330 "not initialized yet");
331 return 0;
334 net_device_ctx = netdev_priv(net);
336 /* Allocate a skb - TODO preallocate this */
337 /* Pad 2-bytes to align IP header to 16 bytes */
338 skb = dev_alloc_skb(packet->TotalDataBufferLength + 2);
339 ASSERT(skb);
340 skb_reserve(skb, 2);
341 skb->dev = net;
343 /* for kmap_atomic */
344 local_irq_save(flags);
347 * Copy to skb. This copy is needed here since the memory pointed by
348 * hv_netvsc_packet cannot be deallocated
350 for (i = 0; i < packet->PageBufferCount; i++) {
351 data = kmap_atomic(pfn_to_page(packet->PageBuffers[i].Pfn),
352 KM_IRQ1);
353 data = (void *)(unsigned long)data +
354 packet->PageBuffers[i].Offset;
356 memcpy(skb_put(skb, packet->PageBuffers[i].Length), data,
357 packet->PageBuffers[i].Length);
359 kunmap_atomic((void *)((unsigned long)data -
360 packet->PageBuffers[i].Offset), KM_IRQ1);
363 local_irq_restore(flags);
365 skb->protocol = eth_type_trans(skb, net);
367 skb->ip_summed = CHECKSUM_NONE;
370 * Pass the skb back up. Network stack will deallocate the skb when it
371 * is done
373 ret = netif_rx(skb);
375 switch (ret) {
376 case NET_RX_DROP:
377 net_device_ctx->stats.rx_dropped++;
378 break;
379 default:
380 net_device_ctx->stats.rx_packets++;
381 net_device_ctx->stats.rx_bytes += skb->len;
382 break;
385 DPRINT_DBG(NETVSC_DRV, "# of recvs %lu total size %lu",
386 net_device_ctx->stats.rx_packets,
387 net_device_ctx->stats.rx_bytes);
389 DPRINT_EXIT(NETVSC_DRV);
391 return 0;
394 static const struct net_device_ops device_ops = {
395 .ndo_open = netvsc_open,
396 .ndo_stop = netvsc_close,
397 .ndo_start_xmit = netvsc_start_xmit,
398 .ndo_get_stats = netvsc_get_stats,
399 .ndo_set_multicast_list = netvsc_set_multicast_list,
400 .ndo_change_mtu = eth_change_mtu,
401 .ndo_validate_addr = eth_validate_addr,
402 .ndo_set_mac_address = eth_mac_addr,
406 * Send GARP packet to network peers after migrations.
407 * After Quick Migration, the network is not immediately operational in the
408 * current context when receiving RNDIS_STATUS_MEDIA_CONNECT event. So, add
409 * another netif_notify_peers() into a scheduled work, otherwise GARP packet
410 * will not be sent after quick migration, and cause network disconnection.
412 static void netvsc_send_garp(struct work_struct *w)
414 struct net_device_context *ndev_ctx;
415 struct net_device *net;
417 msleep(20);
418 ndev_ctx = container_of(w, struct net_device_context, work);
419 net = dev_get_drvdata(&ndev_ctx->device_ctx->device);
420 netif_notify_peers(net);
424 static int netvsc_probe(struct device *device)
426 struct driver_context *driver_ctx =
427 driver_to_driver_context(device->driver);
428 struct netvsc_driver_context *net_drv_ctx =
429 (struct netvsc_driver_context *)driver_ctx;
430 struct netvsc_driver *net_drv_obj = &net_drv_ctx->drv_obj;
431 struct device_context *device_ctx = device_to_device_context(device);
432 struct hv_device *device_obj = &device_ctx->device_obj;
433 struct net_device *net = NULL;
434 struct net_device_context *net_device_ctx;
435 struct netvsc_device_info device_info;
436 int ret;
438 DPRINT_ENTER(NETVSC_DRV);
440 if (!net_drv_obj->Base.OnDeviceAdd)
441 return -1;
443 net = alloc_etherdev(sizeof(struct net_device_context));
444 if (!net)
445 return -1;
447 /* Set initial state */
448 netif_carrier_off(net);
449 netif_stop_queue(net);
451 net_device_ctx = netdev_priv(net);
452 net_device_ctx->device_ctx = device_ctx;
453 dev_set_drvdata(device, net);
454 INIT_WORK(&net_device_ctx->work, netvsc_send_garp);
456 /* Notify the netvsc driver of the new device */
457 ret = net_drv_obj->Base.OnDeviceAdd(device_obj, &device_info);
458 if (ret != 0) {
459 free_netdev(net);
460 dev_set_drvdata(device, NULL);
462 DPRINT_ERR(NETVSC_DRV, "unable to add netvsc device (ret %d)",
463 ret);
464 return ret;
468 * If carrier is still off ie we did not get a link status callback,
469 * update it if necessary
472 * FIXME: We should use a atomic or test/set instead to avoid getting
473 * out of sync with the device's link status
475 if (!netif_carrier_ok(net))
476 if (!device_info.LinkState)
477 netif_carrier_on(net);
479 memcpy(net->dev_addr, device_info.MacAddr, ETH_ALEN);
481 net->netdev_ops = &device_ops;
483 SET_NETDEV_DEV(net, device);
485 ret = register_netdev(net);
486 if (ret != 0) {
487 /* Remove the device and release the resource */
488 net_drv_obj->Base.OnDeviceRemove(device_obj);
489 free_netdev(net);
492 DPRINT_EXIT(NETVSC_DRV);
493 return ret;
496 static int netvsc_remove(struct device *device)
498 struct driver_context *driver_ctx =
499 driver_to_driver_context(device->driver);
500 struct netvsc_driver_context *net_drv_ctx =
501 (struct netvsc_driver_context *)driver_ctx;
502 struct netvsc_driver *net_drv_obj = &net_drv_ctx->drv_obj;
503 struct device_context *device_ctx = device_to_device_context(device);
504 struct net_device *net = dev_get_drvdata(&device_ctx->device);
505 struct hv_device *device_obj = &device_ctx->device_obj;
506 int ret;
508 DPRINT_ENTER(NETVSC_DRV);
510 if (net == NULL) {
511 DPRINT_INFO(NETVSC, "no net device to remove");
512 DPRINT_EXIT(NETVSC_DRV);
513 return 0;
516 if (!net_drv_obj->Base.OnDeviceRemove) {
517 DPRINT_EXIT(NETVSC_DRV);
518 return -1;
521 /* Stop outbound asap */
522 netif_stop_queue(net);
523 /* netif_carrier_off(net); */
525 unregister_netdev(net);
528 * Call to the vsc driver to let it know that the device is being
529 * removed
531 ret = net_drv_obj->Base.OnDeviceRemove(device_obj);
532 if (ret != 0) {
533 /* TODO: */
534 DPRINT_ERR(NETVSC, "unable to remove vsc device (ret %d)", ret);
537 free_netdev(net);
538 DPRINT_EXIT(NETVSC_DRV);
539 return ret;
542 static int netvsc_drv_exit_cb(struct device *dev, void *data)
544 struct device **curr = (struct device **)data;
546 *curr = dev;
547 /* stop iterating */
548 return 1;
551 static void netvsc_drv_exit(void)
553 struct netvsc_driver *netvsc_drv_obj = &g_netvsc_drv.drv_obj;
554 struct driver_context *drv_ctx = &g_netvsc_drv.drv_ctx;
555 struct device *current_dev;
556 int ret;
558 DPRINT_ENTER(NETVSC_DRV);
560 while (1) {
561 current_dev = NULL;
563 /* Get the device */
564 ret = driver_for_each_device(&drv_ctx->driver, NULL,
565 &current_dev, netvsc_drv_exit_cb);
566 if (ret)
567 DPRINT_WARN(NETVSC_DRV,
568 "driver_for_each_device returned %d", ret);
570 if (current_dev == NULL)
571 break;
573 /* Initiate removal from the top-down */
574 DPRINT_INFO(NETVSC_DRV, "unregistering device (%p)...",
575 current_dev);
577 device_unregister(current_dev);
580 if (netvsc_drv_obj->Base.OnCleanup)
581 netvsc_drv_obj->Base.OnCleanup(&netvsc_drv_obj->Base);
583 vmbus_child_driver_unregister(drv_ctx);
585 DPRINT_EXIT(NETVSC_DRV);
587 return;
590 static int netvsc_drv_init(int (*drv_init)(struct hv_driver *drv))
592 struct netvsc_driver *net_drv_obj = &g_netvsc_drv.drv_obj;
593 struct driver_context *drv_ctx = &g_netvsc_drv.drv_ctx;
594 int ret;
596 DPRINT_ENTER(NETVSC_DRV);
598 vmbus_get_interface(&net_drv_obj->Base.VmbusChannelInterface);
600 net_drv_obj->RingBufferSize = netvsc_ringbuffer_size;
601 net_drv_obj->OnReceiveCallback = netvsc_recv_callback;
602 net_drv_obj->OnLinkStatusChanged = netvsc_linkstatus_callback;
604 /* Callback to client driver to complete the initialization */
605 drv_init(&net_drv_obj->Base);
607 drv_ctx->driver.name = net_drv_obj->Base.name;
608 memcpy(&drv_ctx->class_id, &net_drv_obj->Base.deviceType,
609 sizeof(struct hv_guid));
611 drv_ctx->probe = netvsc_probe;
612 drv_ctx->remove = netvsc_remove;
614 /* The driver belongs to vmbus */
615 ret = vmbus_child_driver_register(drv_ctx);
617 DPRINT_EXIT(NETVSC_DRV);
619 return ret;
622 static int __init netvsc_init(void)
624 int ret;
626 DPRINT_ENTER(NETVSC_DRV);
627 DPRINT_INFO(NETVSC_DRV, "Netvsc initializing....");
629 ret = netvsc_drv_init(NetVscInitialize);
631 DPRINT_EXIT(NETVSC_DRV);
633 return ret;
636 static void __exit netvsc_exit(void)
638 DPRINT_ENTER(NETVSC_DRV);
639 netvsc_drv_exit();
640 DPRINT_EXIT(NETVSC_DRV);
643 module_param(netvsc_ringbuffer_size, int, S_IRUGO);
645 module_init(netvsc_init);
646 module_exit(netvsc_exit);