3 * Copyright (c) 2009, Microsoft Corporation.
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms and conditions of the GNU General Public License,
7 * version 2, as published by the Free Software Foundation.
9 * This program is distributed in the hope it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
14 * You should have received a copy of the GNU General Public License along with
15 * this program; if not, write to the Free Software Foundation, Inc., 59 Temple
16 * Place - Suite 330, Boston, MA 02111-1307 USA.
19 * Hank Janssen <hjanssen@microsoft.com>
23 #include <linux/init.h>
24 #include <linux/module.h>
25 #include <linux/highmem.h>
26 #include <linux/device.h>
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>
35 #include <net/route.h>
37 #include <net/pkt_sched.h>
39 #include "include/logging.h"
40 #include "include/vmbus.h"
42 #include "include/NetVscApi.h"
44 MODULE_LICENSE("GPL");
49 static int netvsc_probe(struct device
*device
);
50 static int netvsc_remove(struct device
*device
);
51 static int netvsc_open(struct net_device
*net
);
52 static void netvsc_xmit_completion(void *context
);
53 static int netvsc_start_xmit (struct sk_buff
*skb
, struct net_device
*net
);
54 static int netvsc_recv_callback(DEVICE_OBJECT
*device_obj
, NETVSC_PACKET
* Packet
);
55 static int netvsc_close(struct net_device
*net
);
56 static struct net_device_stats
*netvsc_get_stats(struct net_device
*net
);
57 static void netvsc_linkstatus_callback(DEVICE_OBJECT
*device_obj
, unsigned int status
);
62 struct net_device_context
{
63 struct device_context
*device_ctx
; /* point back to our device context */
64 struct net_device_stats stats
;
67 struct netvsc_driver_context
{
68 /* !! These must be the first 2 fields !! */
69 struct driver_context drv_ctx
;
70 NETVSC_DRIVER_OBJECT drv_obj
;
77 static int netvsc_ringbuffer_size
= NETVSC_DEVICE_RING_BUFFER_SIZE
;
79 /* The one and only one */
80 static struct netvsc_driver_context g_netvsc_drv
;
88 Name: netvsc_drv_init()
90 Desc: NetVsc driver initialization
93 int netvsc_drv_init(PFN_DRIVERINITIALIZE pfn_drv_init
)
96 NETVSC_DRIVER_OBJECT
*net_drv_obj
=&g_netvsc_drv
.drv_obj
;
97 struct driver_context
*drv_ctx
=&g_netvsc_drv
.drv_ctx
;
99 DPRINT_ENTER(NETVSC_DRV
);
101 vmbus_get_interface(&net_drv_obj
->Base
.VmbusChannelInterface
);
103 net_drv_obj
->RingBufferSize
= netvsc_ringbuffer_size
;
104 net_drv_obj
->OnReceiveCallback
= netvsc_recv_callback
;
105 net_drv_obj
->OnLinkStatusChanged
= netvsc_linkstatus_callback
;
107 /* Callback to client driver to complete the initialization */
108 pfn_drv_init(&net_drv_obj
->Base
);
110 drv_ctx
->driver
.name
= net_drv_obj
->Base
.name
;
111 memcpy(&drv_ctx
->class_id
, &net_drv_obj
->Base
.deviceType
, sizeof(GUID
));
113 drv_ctx
->probe
= netvsc_probe
;
114 drv_ctx
->remove
= netvsc_remove
;
116 /* The driver belongs to vmbus */
117 ret
= vmbus_child_driver_register(drv_ctx
);
119 DPRINT_EXIT(NETVSC_DRV
);
126 Name: netvsc_get_stats()
128 Desc: Get the network stats
131 static struct net_device_stats
*netvsc_get_stats(struct net_device
*net
)
133 struct net_device_context
*net_device_ctx
= netdev_priv(net
);
135 return &net_device_ctx
->stats
;
140 Name: netvsc_set_multicast_list()
142 Desc: Set the multicast list
146 static void netvsc_set_multicast_list(struct net_device
*net
)
151 static const struct net_device_ops device_ops
= {
152 .ndo_open
= netvsc_open
,
153 .ndo_stop
= netvsc_close
,
154 .ndo_start_xmit
= netvsc_start_xmit
,
155 .ndo_get_stats
= netvsc_get_stats
,
156 .ndo_set_multicast_list
= netvsc_set_multicast_list
,
163 Desc: Add the specified new device to this driver
166 static int netvsc_probe(struct device
*device
)
170 struct driver_context
*driver_ctx
= driver_to_driver_context(device
->driver
);
171 struct netvsc_driver_context
*net_drv_ctx
= (struct netvsc_driver_context
*)driver_ctx
;
172 NETVSC_DRIVER_OBJECT
*net_drv_obj
= &net_drv_ctx
->drv_obj
;
174 struct device_context
*device_ctx
= device_to_device_context(device
);
175 DEVICE_OBJECT
*device_obj
= &device_ctx
->device_obj
;
177 struct net_device
*net
= NULL
;
178 struct net_device_context
*net_device_ctx
;
179 NETVSC_DEVICE_INFO device_info
;
181 DPRINT_ENTER(NETVSC_DRV
);
183 if (!net_drv_obj
->Base
.OnDeviceAdd
)
188 net
= alloc_netdev(sizeof(struct net_device_context
), "seth%d", ether_setup
);
189 /* net = alloc_etherdev(sizeof(struct net_device_context)); */
195 /* Set initial state */
196 netif_carrier_off(net
);
197 netif_stop_queue(net
);
199 net_device_ctx
= netdev_priv(net
);
200 net_device_ctx
->device_ctx
= device_ctx
;
201 dev_set_drvdata(device
, net
);
203 /* Notify the netvsc driver of the new device */
204 ret
= net_drv_obj
->Base
.OnDeviceAdd(device_obj
, (void*)&device_info
);
208 dev_set_drvdata(device
, NULL
);
210 DPRINT_ERR(NETVSC_DRV
, "unable to add netvsc device (ret %d)", ret
);
214 /* If carrier is still off ie we did not get a link status callback, update it if necessary */
215 /* FIXME: We should use a atomic or test/set instead to avoid getting out of sync with the device's link status */
216 if (!netif_carrier_ok(net
))
218 if (!device_info
.LinkState
)
220 netif_carrier_on(net
);
224 memcpy(net
->dev_addr
, device_info
.MacAddr
, ETH_ALEN
);
226 net
->netdev_ops
= &device_ops
;
228 SET_NETDEV_DEV(net
, device
);
230 ret
= register_netdev(net
);
233 /* Remove the device and release the resource */
234 net_drv_obj
->Base
.OnDeviceRemove(device_obj
);
238 DPRINT_EXIT(NETVSC_DRV
);
243 static int netvsc_remove(struct device
*device
)
246 struct driver_context
*driver_ctx
= driver_to_driver_context(device
->driver
);
247 struct netvsc_driver_context
*net_drv_ctx
= (struct netvsc_driver_context
*)driver_ctx
;
248 NETVSC_DRIVER_OBJECT
*net_drv_obj
= &net_drv_ctx
->drv_obj
;
250 struct device_context
*device_ctx
= device_to_device_context(device
);
251 struct net_device
*net
= dev_get_drvdata(&device_ctx
->device
);
252 DEVICE_OBJECT
*device_obj
= &device_ctx
->device_obj
;
254 DPRINT_ENTER(NETVSC_DRV
);
258 DPRINT_INFO(NETVSC
, "no net device to remove");
259 DPRINT_EXIT(NETVSC_DRV
);
263 if (!net_drv_obj
->Base
.OnDeviceRemove
)
265 DPRINT_EXIT(NETVSC_DRV
);
269 /* Stop outbound asap */
270 netif_stop_queue(net
);
271 /* netif_carrier_off(net); */
273 unregister_netdev(net
);
275 /* Call to the vsc driver to let it know that the device is being removed */
276 ret
= net_drv_obj
->Base
.OnDeviceRemove(device_obj
);
280 DPRINT_ERR(NETVSC
, "unable to remove vsc device (ret %d)", ret
);
285 DPRINT_EXIT(NETVSC_DRV
);
294 Desc: Open the specified interface device
297 static int netvsc_open(struct net_device
*net
)
300 struct net_device_context
*net_device_ctx
= netdev_priv(net
);
301 struct driver_context
*driver_ctx
= driver_to_driver_context(net_device_ctx
->device_ctx
->device
.driver
);
302 struct netvsc_driver_context
*net_drv_ctx
= (struct netvsc_driver_context
*)driver_ctx
;
303 NETVSC_DRIVER_OBJECT
*net_drv_obj
= &net_drv_ctx
->drv_obj
;
305 DEVICE_OBJECT
*device_obj
= &net_device_ctx
->device_ctx
->device_obj
;
307 DPRINT_ENTER(NETVSC_DRV
);
309 if (netif_carrier_ok(net
))
311 memset(&net_device_ctx
->stats
, 0 , sizeof(struct net_device_stats
));
313 /* Open up the device */
314 ret
= net_drv_obj
->OnOpen(device_obj
);
317 DPRINT_ERR(NETVSC_DRV
, "unable to open device (ret %d).", ret
);
321 netif_start_queue(net
);
325 DPRINT_ERR(NETVSC_DRV
, "unable to open device...link is down.");
328 DPRINT_EXIT(NETVSC_DRV
);
336 Desc: Close the specified interface device
339 static int netvsc_close(struct net_device
*net
)
342 struct net_device_context
*net_device_ctx
= netdev_priv(net
);
343 struct driver_context
*driver_ctx
= driver_to_driver_context(net_device_ctx
->device_ctx
->device
.driver
);
344 struct netvsc_driver_context
*net_drv_ctx
= (struct netvsc_driver_context
*)driver_ctx
;
345 NETVSC_DRIVER_OBJECT
*net_drv_obj
= &net_drv_ctx
->drv_obj
;
347 DEVICE_OBJECT
*device_obj
= &net_device_ctx
->device_ctx
->device_obj
;
349 DPRINT_ENTER(NETVSC_DRV
);
351 netif_stop_queue(net
);
353 ret
= net_drv_obj
->OnClose(device_obj
);
356 DPRINT_ERR(NETVSC_DRV
, "unable to close device (ret %d).", ret
);
359 DPRINT_EXIT(NETVSC_DRV
);
367 Name: netvsc_xmit_completion()
369 Desc: Send completion processing
372 static void netvsc_xmit_completion(void *context
)
374 NETVSC_PACKET
*packet
= (NETVSC_PACKET
*)context
;
375 struct sk_buff
*skb
= (struct sk_buff
*)(unsigned long)packet
->Completion
.Send
.SendCompletionTid
;
376 struct net_device
* net
;
378 DPRINT_ENTER(NETVSC_DRV
);
386 dev_kfree_skb_any(skb
);
388 if (netif_queue_stopped(net
))
390 DPRINT_INFO(NETVSC_DRV
, "net device (%p) waking up...", net
);
392 netif_wake_queue(net
);
396 DPRINT_EXIT(NETVSC_DRV
);
401 Name: netvsc_start_xmit()
406 static int netvsc_start_xmit (struct sk_buff
*skb
, struct net_device
*net
)
409 struct net_device_context
*net_device_ctx
= netdev_priv(net
);
410 struct driver_context
*driver_ctx
= driver_to_driver_context(net_device_ctx
->device_ctx
->device
.driver
);
411 struct netvsc_driver_context
*net_drv_ctx
= (struct netvsc_driver_context
*)driver_ctx
;
412 NETVSC_DRIVER_OBJECT
*net_drv_obj
= &net_drv_ctx
->drv_obj
;
415 NETVSC_PACKET
* packet
;
419 DPRINT_ENTER(NETVSC_DRV
);
421 /* Support only 1 chain of frags */
422 ASSERT(skb_shinfo(skb
)->frag_list
== NULL
);
423 ASSERT(skb
->dev
== net
);
425 DPRINT_DBG(NETVSC_DRV
, "xmit packet - len %d data_len %d", skb
->len
, skb
->data_len
);
427 /* Add 1 for skb->data and any additional ones requested */
428 num_frags
= skb_shinfo(skb
)->nr_frags
+ 1 + net_drv_obj
->AdditionalRequestPageBufferCount
;
430 /* Allocate a netvsc packet based on # of frags. */
431 packet
= kzalloc(sizeof(NETVSC_PACKET
) + (num_frags
* sizeof(PAGE_BUFFER
)) + net_drv_obj
->RequestExtSize
, GFP_ATOMIC
);
434 DPRINT_ERR(NETVSC_DRV
, "unable to allocate NETVSC_PACKET");
438 packet
->Extension
= (void*)(unsigned long)packet
+ sizeof(NETVSC_PACKET
) + (num_frags
* sizeof(PAGE_BUFFER
)) ;
440 /* Setup the rndis header */
441 packet
->PageBufferCount
= num_frags
;
443 /* TODO: Flush all write buffers/ memory fence ??? */
446 /* Initialize it from the skb */
448 packet
->TotalDataBufferLength
= skb
->len
;
450 /* Start filling in the page buffers starting at AdditionalRequestPageBufferCount offset */
451 packet
->PageBuffers
[net_drv_obj
->AdditionalRequestPageBufferCount
].Pfn
= virt_to_phys(skb
->data
) >> PAGE_SHIFT
;
452 packet
->PageBuffers
[net_drv_obj
->AdditionalRequestPageBufferCount
].Offset
= (unsigned long)skb
->data
& (PAGE_SIZE
-1);
453 packet
->PageBuffers
[net_drv_obj
->AdditionalRequestPageBufferCount
].Length
= skb
->len
- skb
->data_len
;
455 ASSERT((skb
->len
- skb
->data_len
) <= PAGE_SIZE
);
457 for (i
=net_drv_obj
->AdditionalRequestPageBufferCount
+1; i
<num_frags
; i
++)
459 packet
->PageBuffers
[i
].Pfn
= page_to_pfn(skb_shinfo(skb
)->frags
[i
-(net_drv_obj
->AdditionalRequestPageBufferCount
+1)].page
);
460 packet
->PageBuffers
[i
].Offset
= skb_shinfo(skb
)->frags
[i
-(net_drv_obj
->AdditionalRequestPageBufferCount
+1)].page_offset
;
461 packet
->PageBuffers
[i
].Length
= skb_shinfo(skb
)->frags
[i
-(net_drv_obj
->AdditionalRequestPageBufferCount
+1)].size
;
464 /* Set the completion routine */
465 packet
->Completion
.Send
.OnSendCompletion
= netvsc_xmit_completion
;
466 packet
->Completion
.Send
.SendCompletionContext
= packet
;
467 packet
->Completion
.Send
.SendCompletionTid
= (unsigned long)skb
;
470 ret
= net_drv_obj
->OnSend(&net_device_ctx
->device_ctx
->device_obj
, packet
);
475 net_device_ctx
->stats
.tx_bytes
+= skb
->len
;
476 net_device_ctx
->stats
.tx_packets
++;
483 DPRINT_ERR(NETVSC_DRV
, "unable to send...retrying %d...", retries
);
488 /* no more room or we are shutting down */
489 DPRINT_ERR(NETVSC_DRV
, "unable to send (%d)...marking net device (%p) busy", ret
, net
);
490 DPRINT_INFO(NETVSC_DRV
, "net device (%p) stopping", net
);
492 ret
= NETDEV_TX_BUSY
;
493 net_device_ctx
->stats
.tx_dropped
++;
495 netif_stop_queue(net
);
497 /* Null it since the caller will free it instead of the completion routine */
498 packet
->Completion
.Send
.SendCompletionTid
= 0;
500 /* Release the resources since we will not get any send completion */
501 netvsc_xmit_completion((void*)packet
);
504 DPRINT_DBG(NETVSC_DRV
, "# of xmits %lu total size %lu", net_device_ctx
->stats
.tx_packets
, net_device_ctx
->stats
.tx_bytes
);
506 DPRINT_EXIT(NETVSC_DRV
);
513 Name: netvsc_linkstatus_callback()
515 Desc: Link up/down notification
518 static void netvsc_linkstatus_callback(DEVICE_OBJECT
*device_obj
, unsigned int status
)
520 struct device_context
* device_ctx
= to_device_context(device_obj
);
521 struct net_device
* net
= dev_get_drvdata(&device_ctx
->device
);
523 DPRINT_ENTER(NETVSC_DRV
);
527 DPRINT_ERR(NETVSC_DRV
, "got link status but net device not initialized yet");
533 netif_carrier_on(net
);
534 netif_wake_queue(net
);
538 netif_carrier_off(net
);
539 netif_stop_queue(net
);
541 DPRINT_EXIT(NETVSC_DRV
);
547 Name: netvsc_recv_callback()
549 Desc: Callback when we receive a packet from the "wire" on the specify device
552 static int netvsc_recv_callback(DEVICE_OBJECT
*device_obj
, NETVSC_PACKET
* packet
)
555 struct device_context
*device_ctx
= to_device_context(device_obj
);
556 struct net_device
*net
= dev_get_drvdata(&device_ctx
->device
);
557 struct net_device_context
*net_device_ctx
;
564 DPRINT_ENTER(NETVSC_DRV
);
568 DPRINT_ERR(NETVSC_DRV
, "got receive callback but net device not initialized yet");
572 net_device_ctx
= netdev_priv(net
);
574 /* Allocate a skb - TODO preallocate this */
575 /* skb = alloc_skb(packet->TotalDataBufferLength, GFP_ATOMIC); */
576 skb
= dev_alloc_skb(packet
->TotalDataBufferLength
+ 2); /* Pad 2-bytes to align IP header to 16 bytes */
581 /* for kmap_atomic */
582 local_irq_save(flags
);
584 /* Copy to skb. This copy is needed here since the memory pointed by NETVSC_PACKET */
585 /* cannot be deallocated */
586 for (i
=0; i
<packet
->PageBufferCount
; i
++)
588 data
= kmap_atomic(pfn_to_page(packet
->PageBuffers
[i
].Pfn
), KM_IRQ1
);
589 data
= (void*)(unsigned long)data
+ packet
->PageBuffers
[i
].Offset
;
591 memcpy(skb_put(skb
, packet
->PageBuffers
[i
].Length
), data
, packet
->PageBuffers
[i
].Length
);
593 kunmap_atomic((void*)((unsigned long)data
- packet
->PageBuffers
[i
].Offset
), KM_IRQ1
);
596 local_irq_restore(flags
);
598 skb
->protocol
= eth_type_trans(skb
, net
);
600 skb
->ip_summed
= CHECKSUM_NONE
;
602 /* Pass the skb back up. Network stack will deallocate the skb when it is done */
608 net_device_ctx
->stats
.rx_dropped
++;
611 net_device_ctx
->stats
.rx_packets
++;
612 net_device_ctx
->stats
.rx_bytes
+= skb
->len
;
616 DPRINT_DBG(NETVSC_DRV
, "# of recvs %lu total size %lu", net_device_ctx
->stats
.rx_packets
, net_device_ctx
->stats
.rx_bytes
);
618 DPRINT_EXIT(NETVSC_DRV
);
623 static int netvsc_drv_exit_cb(struct device
*dev
, void *data
)
625 struct device
**curr
= (struct device
**)data
;
627 return 1; /* stop iterating */
632 Name: netvsc_drv_exit()
637 void netvsc_drv_exit(void)
639 NETVSC_DRIVER_OBJECT
*netvsc_drv_obj
=&g_netvsc_drv
.drv_obj
;
640 struct driver_context
*drv_ctx
=&g_netvsc_drv
.drv_ctx
;
642 struct device
*current_dev
=NULL
;
644 DPRINT_ENTER(NETVSC_DRV
);
651 driver_for_each_device(&drv_ctx
->driver
, NULL
, (void*)¤t_dev
, netvsc_drv_exit_cb
);
653 if (current_dev
== NULL
)
656 /* Initiate removal from the top-down */
657 DPRINT_INFO(NETVSC_DRV
, "unregistering device (%p)...", current_dev
);
659 device_unregister(current_dev
);
662 if (netvsc_drv_obj
->Base
.OnCleanup
)
663 netvsc_drv_obj
->Base
.OnCleanup(&netvsc_drv_obj
->Base
);
665 vmbus_child_driver_unregister(drv_ctx
);
667 DPRINT_EXIT(NETVSC_DRV
);
672 static int __init
netvsc_init(void)
676 DPRINT_ENTER(NETVSC_DRV
);
677 DPRINT_INFO(NETVSC_DRV
, "Netvsc initializing....");
679 ret
= netvsc_drv_init(NetVscInitialize
);
681 DPRINT_EXIT(NETVSC_DRV
);
686 static void __exit
netvsc_exit(void)
688 DPRINT_ENTER(NETVSC_DRV
);
692 DPRINT_EXIT(NETVSC_DRV
);
695 module_param(netvsc_ringbuffer_size
, int, S_IRUGO
);
697 module_init(netvsc_init
);
698 module_exit(netvsc_exit
);