First Support on Ginger and OMAP TI
[linux-ginger.git] / drivers / staging / hv / netvsc_drv.c
blob3192d50f72511a897627191babeb5e7b6c97e3fd
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 * Hank Janssen <hjanssen@microsoft.com>
20 #include <linux/init.h>
21 #include <linux/module.h>
22 #include <linux/highmem.h>
23 #include <linux/device.h>
24 #include <linux/io.h>
25 #include <linux/delay.h>
26 #include <linux/netdevice.h>
27 #include <linux/inetdevice.h>
28 #include <linux/etherdevice.h>
29 #include <linux/skbuff.h>
30 #include <linux/in.h>
31 #include <net/arp.h>
32 #include <net/route.h>
33 #include <net/sock.h>
34 #include <net/pkt_sched.h>
35 #include "osd.h"
36 #include "logging.h"
37 #include "vmbus.h"
38 #include "NetVscApi.h"
40 MODULE_LICENSE("GPL");
42 struct net_device_context {
43 /* point back to our device context */
44 struct device_context *device_ctx;
45 struct net_device_stats stats;
48 struct netvsc_driver_context {
49 /* !! These must be the first 2 fields !! */
50 /* Which is a bug FIXME! */
51 struct driver_context drv_ctx;
52 struct netvsc_driver drv_obj;
55 static int netvsc_ringbuffer_size = NETVSC_DEVICE_RING_BUFFER_SIZE;
57 /* The one and only one */
58 static struct netvsc_driver_context g_netvsc_drv;
60 static struct net_device_stats *netvsc_get_stats(struct net_device *net)
62 struct net_device_context *net_device_ctx = netdev_priv(net);
64 return &net_device_ctx->stats;
67 static void netvsc_set_multicast_list(struct net_device *net)
71 static int netvsc_open(struct net_device *net)
73 struct net_device_context *net_device_ctx = netdev_priv(net);
74 struct driver_context *driver_ctx =
75 driver_to_driver_context(net_device_ctx->device_ctx->device.driver);
76 struct netvsc_driver_context *net_drv_ctx =
77 (struct netvsc_driver_context *)driver_ctx;
78 struct netvsc_driver *net_drv_obj = &net_drv_ctx->drv_obj;
79 struct hv_device *device_obj = &net_device_ctx->device_ctx->device_obj;
80 int ret = 0;
82 DPRINT_ENTER(NETVSC_DRV);
84 if (netif_carrier_ok(net)) {
85 memset(&net_device_ctx->stats, 0,
86 sizeof(struct net_device_stats));
88 /* Open up the device */
89 ret = net_drv_obj->OnOpen(device_obj);
90 if (ret != 0) {
91 DPRINT_ERR(NETVSC_DRV,
92 "unable to open device (ret %d).", ret);
93 return ret;
96 netif_start_queue(net);
97 } else {
98 DPRINT_ERR(NETVSC_DRV, "unable to open device...link is down.");
101 DPRINT_EXIT(NETVSC_DRV);
102 return ret;
105 static int netvsc_close(struct net_device *net)
107 struct net_device_context *net_device_ctx = netdev_priv(net);
108 struct driver_context *driver_ctx =
109 driver_to_driver_context(net_device_ctx->device_ctx->device.driver);
110 struct netvsc_driver_context *net_drv_ctx =
111 (struct netvsc_driver_context *)driver_ctx;
112 struct netvsc_driver *net_drv_obj = &net_drv_ctx->drv_obj;
113 struct hv_device *device_obj = &net_device_ctx->device_ctx->device_obj;
114 int ret;
116 DPRINT_ENTER(NETVSC_DRV);
118 netif_stop_queue(net);
120 ret = net_drv_obj->OnClose(device_obj);
121 if (ret != 0)
122 DPRINT_ERR(NETVSC_DRV, "unable to close device (ret %d).", ret);
124 DPRINT_EXIT(NETVSC_DRV);
126 return ret;
129 static void netvsc_xmit_completion(void *context)
131 struct hv_netvsc_packet *packet = (struct hv_netvsc_packet *)context;
132 struct sk_buff *skb = (struct sk_buff *)
133 (unsigned long)packet->Completion.Send.SendCompletionTid;
134 struct net_device *net;
136 DPRINT_ENTER(NETVSC_DRV);
138 kfree(packet);
140 if (skb) {
141 net = skb->dev;
142 dev_kfree_skb_any(skb);
144 if (netif_queue_stopped(net)) {
145 DPRINT_INFO(NETVSC_DRV, "net device (%p) waking up...",
146 net);
148 netif_wake_queue(net);
152 DPRINT_EXIT(NETVSC_DRV);
155 static int netvsc_start_xmit(struct sk_buff *skb, struct net_device *net)
157 struct net_device_context *net_device_ctx = netdev_priv(net);
158 struct driver_context *driver_ctx =
159 driver_to_driver_context(net_device_ctx->device_ctx->device.driver);
160 struct netvsc_driver_context *net_drv_ctx =
161 (struct netvsc_driver_context *)driver_ctx;
162 struct netvsc_driver *net_drv_obj = &net_drv_ctx->drv_obj;
163 struct hv_netvsc_packet *packet;
164 int i;
165 int ret;
166 int num_frags;
167 int retries = 0;
169 DPRINT_ENTER(NETVSC_DRV);
171 /* Support only 1 chain of frags */
172 ASSERT(skb_shinfo(skb)->frag_list == NULL);
173 ASSERT(skb->dev == net);
175 DPRINT_DBG(NETVSC_DRV, "xmit packet - len %d data_len %d",
176 skb->len, skb->data_len);
178 /* Add 1 for skb->data and any additional ones requested */
179 num_frags = skb_shinfo(skb)->nr_frags + 1 +
180 net_drv_obj->AdditionalRequestPageBufferCount;
182 /* Allocate a netvsc packet based on # of frags. */
183 packet = kzalloc(sizeof(struct hv_netvsc_packet) +
184 (num_frags * sizeof(struct hv_page_buffer)) +
185 net_drv_obj->RequestExtSize, GFP_ATOMIC);
186 if (!packet) {
187 DPRINT_ERR(NETVSC_DRV, "unable to allocate hv_netvsc_packet");
188 return -1;
191 packet->Extension = (void *)(unsigned long)packet +
192 sizeof(struct hv_netvsc_packet) +
193 (num_frags * sizeof(struct hv_page_buffer));
195 /* Setup the rndis header */
196 packet->PageBufferCount = num_frags;
198 /* TODO: Flush all write buffers/ memory fence ??? */
199 /* wmb(); */
201 /* Initialize it from the skb */
202 ASSERT(skb->data);
203 packet->TotalDataBufferLength = skb->len;
206 * Start filling in the page buffers starting at
207 * AdditionalRequestPageBufferCount offset
209 packet->PageBuffers[net_drv_obj->AdditionalRequestPageBufferCount].Pfn = virt_to_phys(skb->data) >> PAGE_SHIFT;
210 packet->PageBuffers[net_drv_obj->AdditionalRequestPageBufferCount].Offset = (unsigned long)skb->data & (PAGE_SIZE - 1);
211 packet->PageBuffers[net_drv_obj->AdditionalRequestPageBufferCount].Length = skb->len - skb->data_len;
213 ASSERT((skb->len - skb->data_len) <= PAGE_SIZE);
215 for (i = net_drv_obj->AdditionalRequestPageBufferCount + 1;
216 i < num_frags; i++) {
217 packet->PageBuffers[i].Pfn =
218 page_to_pfn(skb_shinfo(skb)->frags[i-(net_drv_obj->AdditionalRequestPageBufferCount+1)].page);
219 packet->PageBuffers[i].Offset =
220 skb_shinfo(skb)->frags[i-(net_drv_obj->AdditionalRequestPageBufferCount+1)].page_offset;
221 packet->PageBuffers[i].Length =
222 skb_shinfo(skb)->frags[i-(net_drv_obj->AdditionalRequestPageBufferCount+1)].size;
225 /* Set the completion routine */
226 packet->Completion.Send.OnSendCompletion = netvsc_xmit_completion;
227 packet->Completion.Send.SendCompletionContext = packet;
228 packet->Completion.Send.SendCompletionTid = (unsigned long)skb;
230 retry_send:
231 ret = net_drv_obj->OnSend(&net_device_ctx->device_ctx->device_obj,
232 packet);
234 if (ret == 0) {
235 ret = NETDEV_TX_OK;
236 net_device_ctx->stats.tx_bytes += skb->len;
237 net_device_ctx->stats.tx_packets++;
238 } else {
239 retries++;
240 if (retries < 4) {
241 DPRINT_ERR(NETVSC_DRV, "unable to send..."
242 "retrying %d...", retries);
243 udelay(100);
244 goto retry_send;
247 /* no more room or we are shutting down */
248 DPRINT_ERR(NETVSC_DRV, "unable to send (%d)..."
249 "marking net device (%p) busy", ret, net);
250 DPRINT_INFO(NETVSC_DRV, "net device (%p) stopping", net);
252 ret = NETDEV_TX_BUSY;
253 net_device_ctx->stats.tx_dropped++;
255 netif_stop_queue(net);
258 * Null it since the caller will free it instead of the
259 * completion routine
261 packet->Completion.Send.SendCompletionTid = 0;
264 * Release the resources since we will not get any send
265 * completion
267 netvsc_xmit_completion((void *)packet);
270 DPRINT_DBG(NETVSC_DRV, "# of xmits %lu total size %lu",
271 net_device_ctx->stats.tx_packets,
272 net_device_ctx->stats.tx_bytes);
274 DPRINT_EXIT(NETVSC_DRV);
275 return ret;
279 * netvsc_linkstatus_callback - Link up/down notification
281 static void netvsc_linkstatus_callback(struct hv_device *device_obj,
282 unsigned int status)
284 struct device_context *device_ctx = to_device_context(device_obj);
285 struct net_device *net = dev_get_drvdata(&device_ctx->device);
287 DPRINT_ENTER(NETVSC_DRV);
289 if (!net) {
290 DPRINT_ERR(NETVSC_DRV, "got link status but net device "
291 "not initialized yet");
292 return;
295 if (status == 1) {
296 netif_carrier_on(net);
297 netif_wake_queue(net);
298 } else {
299 netif_carrier_off(net);
300 netif_stop_queue(net);
302 DPRINT_EXIT(NETVSC_DRV);
306 * netvsc_recv_callback - Callback when we receive a packet from the "wire" on the specified device.
308 static int netvsc_recv_callback(struct hv_device *device_obj,
309 struct hv_netvsc_packet *packet)
311 struct device_context *device_ctx = to_device_context(device_obj);
312 struct net_device *net = dev_get_drvdata(&device_ctx->device);
313 struct net_device_context *net_device_ctx;
314 struct sk_buff *skb;
315 void *data;
316 int ret;
317 int i;
318 unsigned long flags;
320 DPRINT_ENTER(NETVSC_DRV);
322 if (!net) {
323 DPRINT_ERR(NETVSC_DRV, "got receive callback but net device "
324 "not initialized yet");
325 return 0;
328 net_device_ctx = netdev_priv(net);
330 /* Allocate a skb - TODO preallocate this */
331 /* Pad 2-bytes to align IP header to 16 bytes */
332 skb = dev_alloc_skb(packet->TotalDataBufferLength + 2);
333 ASSERT(skb);
334 skb_reserve(skb, 2);
335 skb->dev = net;
337 /* for kmap_atomic */
338 local_irq_save(flags);
341 * Copy to skb. This copy is needed here since the memory pointed by
342 * hv_netvsc_packet cannot be deallocated
344 for (i = 0; i < packet->PageBufferCount; i++) {
345 data = kmap_atomic(pfn_to_page(packet->PageBuffers[i].Pfn),
346 KM_IRQ1);
347 data = (void *)(unsigned long)data +
348 packet->PageBuffers[i].Offset;
350 memcpy(skb_put(skb, packet->PageBuffers[i].Length), data,
351 packet->PageBuffers[i].Length);
353 kunmap_atomic((void *)((unsigned long)data -
354 packet->PageBuffers[i].Offset), KM_IRQ1);
357 local_irq_restore(flags);
359 skb->protocol = eth_type_trans(skb, net);
361 skb->ip_summed = CHECKSUM_NONE;
364 * Pass the skb back up. Network stack will deallocate the skb when it
365 * is done
367 ret = netif_rx(skb);
369 switch (ret) {
370 case NET_RX_DROP:
371 net_device_ctx->stats.rx_dropped++;
372 break;
373 default:
374 net_device_ctx->stats.rx_packets++;
375 net_device_ctx->stats.rx_bytes += skb->len;
376 break;
379 DPRINT_DBG(NETVSC_DRV, "# of recvs %lu total size %lu",
380 net_device_ctx->stats.rx_packets,
381 net_device_ctx->stats.rx_bytes);
383 DPRINT_EXIT(NETVSC_DRV);
385 return 0;
388 static const struct net_device_ops device_ops = {
389 .ndo_open = netvsc_open,
390 .ndo_stop = netvsc_close,
391 .ndo_start_xmit = netvsc_start_xmit,
392 .ndo_get_stats = netvsc_get_stats,
393 .ndo_set_multicast_list = netvsc_set_multicast_list,
396 static int netvsc_probe(struct device *device)
398 struct driver_context *driver_ctx =
399 driver_to_driver_context(device->driver);
400 struct netvsc_driver_context *net_drv_ctx =
401 (struct netvsc_driver_context *)driver_ctx;
402 struct netvsc_driver *net_drv_obj = &net_drv_ctx->drv_obj;
403 struct device_context *device_ctx = device_to_device_context(device);
404 struct hv_device *device_obj = &device_ctx->device_obj;
405 struct net_device *net = NULL;
406 struct net_device_context *net_device_ctx;
407 struct netvsc_device_info device_info;
408 int ret;
410 DPRINT_ENTER(NETVSC_DRV);
412 if (!net_drv_obj->Base.OnDeviceAdd)
413 return -1;
415 net = alloc_netdev(sizeof(struct net_device_context), "seth%d",
416 ether_setup);
417 if (!net)
418 return -1;
420 /* Set initial state */
421 netif_carrier_off(net);
422 netif_stop_queue(net);
424 net_device_ctx = netdev_priv(net);
425 net_device_ctx->device_ctx = device_ctx;
426 dev_set_drvdata(device, net);
428 /* Notify the netvsc driver of the new device */
429 ret = net_drv_obj->Base.OnDeviceAdd(device_obj, &device_info);
430 if (ret != 0) {
431 free_netdev(net);
432 dev_set_drvdata(device, NULL);
434 DPRINT_ERR(NETVSC_DRV, "unable to add netvsc device (ret %d)",
435 ret);
436 return ret;
440 * If carrier is still off ie we did not get a link status callback,
441 * update it if necessary
444 * FIXME: We should use a atomic or test/set instead to avoid getting
445 * out of sync with the device's link status
447 if (!netif_carrier_ok(net))
448 if (!device_info.LinkState)
449 netif_carrier_on(net);
451 memcpy(net->dev_addr, device_info.MacAddr, ETH_ALEN);
453 net->netdev_ops = &device_ops;
455 SET_NETDEV_DEV(net, device);
457 ret = register_netdev(net);
458 if (ret != 0) {
459 /* Remove the device and release the resource */
460 net_drv_obj->Base.OnDeviceRemove(device_obj);
461 free_netdev(net);
464 DPRINT_EXIT(NETVSC_DRV);
465 return ret;
468 static int netvsc_remove(struct device *device)
470 struct driver_context *driver_ctx =
471 driver_to_driver_context(device->driver);
472 struct netvsc_driver_context *net_drv_ctx =
473 (struct netvsc_driver_context *)driver_ctx;
474 struct netvsc_driver *net_drv_obj = &net_drv_ctx->drv_obj;
475 struct device_context *device_ctx = device_to_device_context(device);
476 struct net_device *net = dev_get_drvdata(&device_ctx->device);
477 struct hv_device *device_obj = &device_ctx->device_obj;
478 int ret;
480 DPRINT_ENTER(NETVSC_DRV);
482 if (net == NULL) {
483 DPRINT_INFO(NETVSC, "no net device to remove");
484 DPRINT_EXIT(NETVSC_DRV);
485 return 0;
488 if (!net_drv_obj->Base.OnDeviceRemove) {
489 DPRINT_EXIT(NETVSC_DRV);
490 return -1;
493 /* Stop outbound asap */
494 netif_stop_queue(net);
495 /* netif_carrier_off(net); */
497 unregister_netdev(net);
500 * Call to the vsc driver to let it know that the device is being
501 * removed
503 ret = net_drv_obj->Base.OnDeviceRemove(device_obj);
504 if (ret != 0) {
505 /* TODO: */
506 DPRINT_ERR(NETVSC, "unable to remove vsc device (ret %d)", ret);
509 free_netdev(net);
510 DPRINT_EXIT(NETVSC_DRV);
511 return ret;
514 static int netvsc_drv_exit_cb(struct device *dev, void *data)
516 struct device **curr = (struct device **)data;
518 *curr = dev;
519 /* stop iterating */
520 return 1;
523 static void netvsc_drv_exit(void)
525 struct netvsc_driver *netvsc_drv_obj = &g_netvsc_drv.drv_obj;
526 struct driver_context *drv_ctx = &g_netvsc_drv.drv_ctx;
527 struct device *current_dev;
528 int ret;
530 DPRINT_ENTER(NETVSC_DRV);
532 while (1) {
533 current_dev = NULL;
535 /* Get the device */
536 ret = driver_for_each_device(&drv_ctx->driver, NULL,
537 &current_dev, netvsc_drv_exit_cb);
538 if (ret)
539 DPRINT_WARN(NETVSC_DRV,
540 "driver_for_each_device returned %d", ret);
542 if (current_dev == NULL)
543 break;
545 /* Initiate removal from the top-down */
546 DPRINT_INFO(NETVSC_DRV, "unregistering device (%p)...",
547 current_dev);
549 device_unregister(current_dev);
552 if (netvsc_drv_obj->Base.OnCleanup)
553 netvsc_drv_obj->Base.OnCleanup(&netvsc_drv_obj->Base);
555 vmbus_child_driver_unregister(drv_ctx);
557 DPRINT_EXIT(NETVSC_DRV);
559 return;
562 static int netvsc_drv_init(int (*drv_init)(struct hv_driver *drv))
564 struct netvsc_driver *net_drv_obj = &g_netvsc_drv.drv_obj;
565 struct driver_context *drv_ctx = &g_netvsc_drv.drv_ctx;
566 int ret;
568 DPRINT_ENTER(NETVSC_DRV);
570 vmbus_get_interface(&net_drv_obj->Base.VmbusChannelInterface);
572 net_drv_obj->RingBufferSize = netvsc_ringbuffer_size;
573 net_drv_obj->OnReceiveCallback = netvsc_recv_callback;
574 net_drv_obj->OnLinkStatusChanged = netvsc_linkstatus_callback;
576 /* Callback to client driver to complete the initialization */
577 drv_init(&net_drv_obj->Base);
579 drv_ctx->driver.name = net_drv_obj->Base.name;
580 memcpy(&drv_ctx->class_id, &net_drv_obj->Base.deviceType,
581 sizeof(struct hv_guid));
583 drv_ctx->probe = netvsc_probe;
584 drv_ctx->remove = netvsc_remove;
586 /* The driver belongs to vmbus */
587 ret = vmbus_child_driver_register(drv_ctx);
589 DPRINT_EXIT(NETVSC_DRV);
591 return ret;
594 static int __init netvsc_init(void)
596 int ret;
598 DPRINT_ENTER(NETVSC_DRV);
599 DPRINT_INFO(NETVSC_DRV, "Netvsc initializing....");
601 ret = netvsc_drv_init(NetVscInitialize);
603 DPRINT_EXIT(NETVSC_DRV);
605 return ret;
608 static void __exit netvsc_exit(void)
610 DPRINT_ENTER(NETVSC_DRV);
611 netvsc_drv_exit();
612 DPRINT_EXIT(NETVSC_DRV);
615 module_param(netvsc_ringbuffer_size, int, S_IRUGO);
617 module_init(netvsc_init);
618 module_exit(netvsc_exit);