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>
24 #include <linux/init.h>
25 #include <linux/module.h>
26 #include <linux/highmem.h>
27 #include <linux/device.h>
28 #if defined(KERNEL_2_6_5) || defined(KERNEL_2_6_9)
33 #include <linux/delay.h>
34 #include <linux/netdevice.h>
35 #include <linux/inetdevice.h>
36 #include <linux/etherdevice.h>
37 #include <linux/skbuff.h>
40 #include <net/route.h>
42 #include <net/pkt_sched.h>
47 #include "NetVscApi.h"
49 MODULE_LICENSE("GPL");
54 static int netvsc_probe(struct device
*device
);
55 static int netvsc_remove(struct device
*device
);
56 static int netvsc_open(struct net_device
*net
);
57 static void netvsc_xmit_completion(void *context
);
58 static int netvsc_start_xmit (struct sk_buff
*skb
, struct net_device
*net
);
59 static int netvsc_recv_callback(DEVICE_OBJECT
*device_obj
, NETVSC_PACKET
* Packet
);
60 static int netvsc_close(struct net_device
*net
);
61 static struct net_device_stats
*netvsc_get_stats(struct net_device
*net
);
62 static void netvsc_linkstatus_callback(DEVICE_OBJECT
*device_obj
, unsigned int status
);
67 struct net_device_context
{
68 struct device_context
*device_ctx
; // point back to our device context
69 struct net_device_stats stats
;
72 struct netvsc_driver_context
{
73 // !! These must be the first 2 fields !!
74 struct driver_context drv_ctx
;
75 NETVSC_DRIVER_OBJECT drv_obj
;
82 static int netvsc_ringbuffer_size
= NETVSC_DEVICE_RING_BUFFER_SIZE
;
84 // The one and only one
85 static struct netvsc_driver_context g_netvsc_drv
;
93 Name: netvsc_drv_init()
95 Desc: NetVsc driver initialization
98 int netvsc_drv_init(PFN_DRIVERINITIALIZE pfn_drv_init
)
101 NETVSC_DRIVER_OBJECT
*net_drv_obj
=&g_netvsc_drv
.drv_obj
;
102 struct driver_context
*drv_ctx
=&g_netvsc_drv
.drv_ctx
;
104 DPRINT_ENTER(NETVSC_DRV
);
106 vmbus_get_interface(&net_drv_obj
->Base
.VmbusChannelInterface
);
108 net_drv_obj
->RingBufferSize
= netvsc_ringbuffer_size
;
109 net_drv_obj
->OnReceiveCallback
= netvsc_recv_callback
;
110 net_drv_obj
->OnLinkStatusChanged
= netvsc_linkstatus_callback
;
112 // Callback to client driver to complete the initialization
113 pfn_drv_init(&net_drv_obj
->Base
);
115 drv_ctx
->driver
.name
= net_drv_obj
->Base
.name
;
116 memcpy(&drv_ctx
->class_id
, &net_drv_obj
->Base
.deviceType
, sizeof(GUID
));
118 #if defined(KERNEL_2_6_5) || defined(KERNEL_2_6_9)
119 drv_ctx
->driver
.probe
= netvsc_probe
;
120 drv_ctx
->driver
.remove
= netvsc_remove
;
122 drv_ctx
->probe
= netvsc_probe
;
123 drv_ctx
->remove
= netvsc_remove
;
126 // The driver belongs to vmbus
127 vmbus_child_driver_register(drv_ctx
);
129 DPRINT_EXIT(NETVSC_DRV
);
136 Name: netvsc_get_stats()
138 Desc: Get the network stats
141 static struct net_device_stats
*netvsc_get_stats(struct net_device
*net
)
143 struct net_device_context
*net_device_ctx
= netdev_priv(net
);
145 return &net_device_ctx
->stats
;
150 Name: netvsc_set_multicast_list()
152 Desc: Set the multicast list
156 static void netvsc_set_multicast_list(UNUSED_VAR(struct net_device
*net
))
165 Desc: Add the specified new device to this driver
168 static int netvsc_probe(struct device
*device
)
172 struct driver_context
*driver_ctx
= driver_to_driver_context(device
->driver
);
173 struct netvsc_driver_context
*net_drv_ctx
= (struct netvsc_driver_context
*)driver_ctx
;
174 NETVSC_DRIVER_OBJECT
*net_drv_obj
= &net_drv_ctx
->drv_obj
;
176 struct device_context
*device_ctx
= device_to_device_context(device
);
177 DEVICE_OBJECT
*device_obj
= &device_ctx
->device_obj
;
179 struct net_device
*net
= NULL
;
180 struct net_device_context
*net_device_ctx
;
181 NETVSC_DEVICE_INFO device_info
;
183 DPRINT_ENTER(NETVSC_DRV
);
185 if (!net_drv_obj
->Base
.OnDeviceAdd
)
190 net
= alloc_netdev(sizeof(struct net_device_context
), "seth%d", ether_setup
);
191 //net = alloc_etherdev(sizeof(struct net_device_context));
198 netif_carrier_off(net
);
199 netif_stop_queue(net
);
201 net_device_ctx
= netdev_priv(net
);
202 net_device_ctx
->device_ctx
= device_ctx
;
203 device
->driver_data
= net
;
205 // Notify the netvsc driver of the new device
206 ret
= net_drv_obj
->Base
.OnDeviceAdd(device_obj
, (void*)&device_info
);
210 device
->driver_data
= NULL
;
212 DPRINT_ERR(NETVSC_DRV
, "unable to add netvsc device (ret %d)", ret
);
216 // If carrier is still off ie we did not get a link status callback, update it if necessary
217 // FIXME: We should use a atomic or test/set instead to avoid getting out of sync with the device's link status
218 if (!netif_carrier_ok(net
))
220 if (!device_info
.LinkState
)
222 netif_carrier_on(net
);
226 memcpy(net
->dev_addr
, device_info
.MacAddr
, ETH_ALEN
);
228 net
->open
= netvsc_open
;
229 net
->hard_start_xmit
= netvsc_start_xmit
;
230 net
->stop
= netvsc_close
;
231 net
->get_stats
= netvsc_get_stats
;
232 net
->set_multicast_list
= netvsc_set_multicast_list
;
234 #if !defined(KERNEL_2_6_27)
235 SET_MODULE_OWNER(net
);
237 SET_NETDEV_DEV(net
, device
);
239 ret
= register_netdev(net
);
242 // Remove the device and release the resource
243 net_drv_obj
->Base
.OnDeviceRemove(device_obj
);
247 DPRINT_EXIT(NETVSC_DRV
);
252 static int netvsc_remove(struct device
*device
)
255 struct driver_context
*driver_ctx
= driver_to_driver_context(device
->driver
);
256 struct netvsc_driver_context
*net_drv_ctx
= (struct netvsc_driver_context
*)driver_ctx
;
257 NETVSC_DRIVER_OBJECT
*net_drv_obj
= &net_drv_ctx
->drv_obj
;
259 struct device_context
*device_ctx
= device_to_device_context(device
);
260 struct net_device
*net
= (struct net_device
*)device_ctx
->device
.driver_data
;
261 DEVICE_OBJECT
*device_obj
= &device_ctx
->device_obj
;
263 DPRINT_ENTER(NETVSC_DRV
);
267 DPRINT_INFO(NETVSC
, "no net device to remove");
268 DPRINT_EXIT(NETVSC_DRV
);
272 if (!net_drv_obj
->Base
.OnDeviceRemove
)
274 DPRINT_EXIT(NETVSC_DRV
);
278 // Stop outbound asap
279 netif_stop_queue(net
);
280 //netif_carrier_off(net);
282 unregister_netdev(net
);
284 // Call to the vsc driver to let it know that the device is being removed
285 ret
= net_drv_obj
->Base
.OnDeviceRemove(device_obj
);
289 DPRINT_ERR(NETVSC
, "unable to remove vsc device (ret %d)", ret
);
294 DPRINT_EXIT(NETVSC_DRV
);
303 Desc: Open the specified interface device
306 static int netvsc_open(struct net_device
*net
)
309 struct net_device_context
*net_device_ctx
= netdev_priv(net
);
310 struct driver_context
*driver_ctx
= driver_to_driver_context(net_device_ctx
->device_ctx
->device
.driver
);
311 struct netvsc_driver_context
*net_drv_ctx
= (struct netvsc_driver_context
*)driver_ctx
;
312 NETVSC_DRIVER_OBJECT
*net_drv_obj
= &net_drv_ctx
->drv_obj
;
314 DEVICE_OBJECT
*device_obj
= &net_device_ctx
->device_ctx
->device_obj
;
316 DPRINT_ENTER(NETVSC_DRV
);
318 if (netif_carrier_ok(net
))
320 memset(&net_device_ctx
->stats
, 0 , sizeof(struct net_device_stats
));
322 // Open up the device
323 ret
= net_drv_obj
->OnOpen(device_obj
);
326 DPRINT_ERR(NETVSC_DRV
, "unable to open device (ret %d).", ret
);
330 netif_start_queue(net
);
334 DPRINT_ERR(NETVSC_DRV
, "unable to open device...link is down.");
337 DPRINT_EXIT(NETVSC_DRV
);
345 Desc: Close the specified interface device
348 static int netvsc_close(struct net_device
*net
)
351 struct net_device_context
*net_device_ctx
= netdev_priv(net
);
352 struct driver_context
*driver_ctx
= driver_to_driver_context(net_device_ctx
->device_ctx
->device
.driver
);
353 struct netvsc_driver_context
*net_drv_ctx
= (struct netvsc_driver_context
*)driver_ctx
;
354 NETVSC_DRIVER_OBJECT
*net_drv_obj
= &net_drv_ctx
->drv_obj
;
356 DEVICE_OBJECT
*device_obj
= &net_device_ctx
->device_ctx
->device_obj
;
358 DPRINT_ENTER(NETVSC_DRV
);
360 netif_stop_queue(net
);
362 ret
= net_drv_obj
->OnClose(device_obj
);
365 DPRINT_ERR(NETVSC_DRV
, "unable to close device (ret %d).", ret
);
368 DPRINT_EXIT(NETVSC_DRV
);
376 Name: netvsc_xmit_completion()
378 Desc: Send completion processing
381 static void netvsc_xmit_completion(void *context
)
383 NETVSC_PACKET
*packet
= (NETVSC_PACKET
*)context
;
384 struct sk_buff
*skb
= (struct sk_buff
*)(ULONG_PTR
)packet
->Completion
.Send
.SendCompletionTid
;
385 struct net_device
* net
;
387 DPRINT_ENTER(NETVSC_DRV
);
395 dev_kfree_skb_any(skb
);
397 if (netif_queue_stopped(net
))
399 DPRINT_INFO(NETVSC_DRV
, "net device (%p) waking up...", net
);
401 netif_wake_queue(net
);
405 DPRINT_EXIT(NETVSC_DRV
);
410 Name: netvsc_start_xmit()
415 static int netvsc_start_xmit (struct sk_buff
*skb
, struct net_device
*net
)
418 struct net_device_context
*net_device_ctx
= netdev_priv(net
);
419 struct driver_context
*driver_ctx
= driver_to_driver_context(net_device_ctx
->device_ctx
->device
.driver
);
420 struct netvsc_driver_context
*net_drv_ctx
= (struct netvsc_driver_context
*)driver_ctx
;
421 NETVSC_DRIVER_OBJECT
*net_drv_obj
= &net_drv_ctx
->drv_obj
;
424 NETVSC_PACKET
* packet
;
428 DPRINT_ENTER(NETVSC_DRV
);
430 // Support only 1 chain of frags
431 ASSERT(skb_shinfo(skb
)->frag_list
== NULL
);
432 ASSERT(skb
->dev
== net
);
434 DPRINT_DBG(NETVSC_DRV
, "xmit packet - len %d data_len %d", skb
->len
, skb
->data_len
);
436 // Add 1 for skb->data and any additional ones requested
437 num_frags
= skb_shinfo(skb
)->nr_frags
+ 1 + net_drv_obj
->AdditionalRequestPageBufferCount
;
439 // Allocate a netvsc packet based on # of frags.
440 packet
= kzalloc(sizeof(NETVSC_PACKET
) + (num_frags
* sizeof(PAGE_BUFFER
)) + net_drv_obj
->RequestExtSize
, GFP_ATOMIC
);
443 DPRINT_ERR(NETVSC_DRV
, "unable to allocate NETVSC_PACKET");
447 packet
->Extension
= (void*)(unsigned long)packet
+ sizeof(NETVSC_PACKET
) + (num_frags
* sizeof(PAGE_BUFFER
)) ;
449 // Setup the rndis header
450 packet
->PageBufferCount
= num_frags
;
452 // TODO: Flush all write buffers/ memory fence ???
455 // Initialize it from the skb
457 packet
->TotalDataBufferLength
= skb
->len
;
459 // Start filling in the page buffers starting at AdditionalRequestPageBufferCount offset
460 packet
->PageBuffers
[net_drv_obj
->AdditionalRequestPageBufferCount
].Pfn
= virt_to_phys(skb
->data
) >> PAGE_SHIFT
;
461 packet
->PageBuffers
[net_drv_obj
->AdditionalRequestPageBufferCount
].Offset
= (unsigned long)skb
->data
& (PAGE_SIZE
-1);
462 packet
->PageBuffers
[net_drv_obj
->AdditionalRequestPageBufferCount
].Length
= skb
->len
- skb
->data_len
;
464 ASSERT((skb
->len
- skb
->data_len
) <= PAGE_SIZE
);
466 for (i
=net_drv_obj
->AdditionalRequestPageBufferCount
+1; i
<num_frags
; i
++)
468 packet
->PageBuffers
[i
].Pfn
= page_to_pfn(skb_shinfo(skb
)->frags
[i
-(net_drv_obj
->AdditionalRequestPageBufferCount
+1)].page
);
469 packet
->PageBuffers
[i
].Offset
= skb_shinfo(skb
)->frags
[i
-(net_drv_obj
->AdditionalRequestPageBufferCount
+1)].page_offset
;
470 packet
->PageBuffers
[i
].Length
= skb_shinfo(skb
)->frags
[i
-(net_drv_obj
->AdditionalRequestPageBufferCount
+1)].size
;
473 // Set the completion routine
474 packet
->Completion
.Send
.OnSendCompletion
= netvsc_xmit_completion
;
475 packet
->Completion
.Send
.SendCompletionContext
= packet
;
476 packet
->Completion
.Send
.SendCompletionTid
= (ULONG_PTR
)skb
;
479 ret
= net_drv_obj
->OnSend(&net_device_ctx
->device_ctx
->device_obj
, packet
);
484 #define NETDEV_TX_OK 0
485 #define NETDEV_TX_BUSY 0
488 net_device_ctx
->stats
.tx_bytes
+= skb
->len
;
489 net_device_ctx
->stats
.tx_packets
++;
496 DPRINT_ERR(NETVSC_DRV
, "unable to send...retrying %d...", retries
);
501 // no more room or we are shutting down
502 DPRINT_ERR(NETVSC_DRV
, "unable to send (%d)...marking net device (%p) busy", ret
, net
);
503 DPRINT_INFO(NETVSC_DRV
, "net device (%p) stopping", net
);
505 ret
= NETDEV_TX_BUSY
;
506 net_device_ctx
->stats
.tx_dropped
++;
508 netif_stop_queue(net
);
510 // Null it since the caller will free it instead of the completion routine
511 packet
->Completion
.Send
.SendCompletionTid
= 0;
513 // Release the resources since we will not get any send completion
514 netvsc_xmit_completion((void*)packet
);
517 DPRINT_DBG(NETVSC_DRV
, "# of xmits %lu total size %lu", net_device_ctx
->stats
.tx_packets
, net_device_ctx
->stats
.tx_bytes
);
519 DPRINT_EXIT(NETVSC_DRV
);
526 Name: netvsc_linkstatus_callback()
528 Desc: Link up/down notification
531 static void netvsc_linkstatus_callback(DEVICE_OBJECT
*device_obj
, unsigned int status
)
533 struct device_context
* device_ctx
= to_device_context(device_obj
);
534 struct net_device
* net
= (struct net_device
*)device_ctx
->device
.driver_data
;
536 DPRINT_ENTER(NETVSC_DRV
);
540 DPRINT_ERR(NETVSC_DRV
, "got link status but net device not initialized yet");
546 netif_carrier_on(net
);
547 netif_wake_queue(net
);
551 netif_carrier_off(net
);
552 netif_stop_queue(net
);
554 DPRINT_EXIT(NETVSC_DRV
);
560 Name: netvsc_recv_callback()
562 Desc: Callback when we receive a packet from the "wire" on the specify device
565 static int netvsc_recv_callback(DEVICE_OBJECT
*device_obj
, NETVSC_PACKET
* packet
)
568 struct device_context
*device_ctx
= to_device_context(device_obj
);
569 struct net_device
*net
= (struct net_device
*)device_ctx
->device
.driver_data
;
570 struct net_device_context
*net_device_ctx
;
577 DPRINT_ENTER(NETVSC_DRV
);
581 DPRINT_ERR(NETVSC_DRV
, "got receive callback but net device not initialized yet");
585 net_device_ctx
= netdev_priv(net
);
587 // Allocate a skb - TODO preallocate this
588 //skb = alloc_skb(packet->TotalDataBufferLength, GFP_ATOMIC);
589 skb
= dev_alloc_skb(packet
->TotalDataBufferLength
+ 2); // Pad 2-bytes to align IP header to 16 bytes
595 local_irq_save(flags
);
597 // Copy to skb. This copy is needed here since the memory pointed by NETVSC_PACKET
598 // cannot be deallocated
599 for (i
=0; i
<packet
->PageBufferCount
; i
++)
601 data
= kmap_atomic(pfn_to_page(packet
->PageBuffers
[i
].Pfn
), KM_IRQ1
);
602 data
= (void*)(unsigned long)data
+ packet
->PageBuffers
[i
].Offset
;
604 memcpy(skb_put(skb
, packet
->PageBuffers
[i
].Length
), data
, packet
->PageBuffers
[i
].Length
);
606 kunmap_atomic((void*)((unsigned long)data
- packet
->PageBuffers
[i
].Offset
), KM_IRQ1
);
609 local_irq_restore(flags
);
611 skb
->protocol
= eth_type_trans(skb
, net
);
613 skb
->ip_summed
= CHECKSUM_NONE
;
615 // Pass the skb back up. Network stack will deallocate the skb when it is done
621 net_device_ctx
->stats
.rx_dropped
++;
624 net_device_ctx
->stats
.rx_packets
++;
625 net_device_ctx
->stats
.rx_bytes
+= skb
->len
;
629 DPRINT_DBG(NETVSC_DRV
, "# of recvs %lu total size %lu", net_device_ctx
->stats
.rx_packets
, net_device_ctx
->stats
.rx_bytes
);
631 DPRINT_EXIT(NETVSC_DRV
);
636 static int netvsc_drv_exit_cb(struct device
*dev
, void *data
)
638 struct device
**curr
= (struct device
**)data
;
640 return 1; // stop iterating
645 Name: netvsc_drv_exit()
650 void netvsc_drv_exit(void)
652 NETVSC_DRIVER_OBJECT
*netvsc_drv_obj
=&g_netvsc_drv
.drv_obj
;
653 struct driver_context
*drv_ctx
=&g_netvsc_drv
.drv_ctx
;
655 struct device
*current_dev
=NULL
;
656 #if defined(KERNEL_2_6_5) || defined(KERNEL_2_6_9)
657 #define driver_for_each_device(drv, start, data, fn) \
658 struct list_head *ptr, *n; \
659 list_for_each_safe(ptr, n, &((drv)->devices)) {\
660 struct device *curr_dev;\
661 curr_dev = list_entry(ptr, struct device, driver_list);\
666 DPRINT_ENTER(NETVSC_DRV
);
673 driver_for_each_device(&drv_ctx
->driver
, NULL
, (void*)¤t_dev
, netvsc_drv_exit_cb
);
675 if (current_dev
== NULL
)
678 // Initiate removal from the top-down
679 DPRINT_INFO(NETVSC_DRV
, "unregistering device (%p)...", current_dev
);
681 device_unregister(current_dev
);
684 if (netvsc_drv_obj
->Base
.OnCleanup
)
685 netvsc_drv_obj
->Base
.OnCleanup(&netvsc_drv_obj
->Base
);
687 vmbus_child_driver_unregister(drv_ctx
);
689 DPRINT_EXIT(NETVSC_DRV
);
694 static int __init
netvsc_init(void)
698 DPRINT_ENTER(NETVSC_DRV
);
699 DPRINT_INFO(NETVSC_DRV
, "Netvsc initializing....");
701 ret
= netvsc_drv_init(NetVscInitialize
);
703 DPRINT_EXIT(NETVSC_DRV
);
708 static void __exit
netvsc_exit(void)
710 DPRINT_ENTER(NETVSC_DRV
);
714 DPRINT_EXIT(NETVSC_DRV
);
717 module_param(netvsc_ringbuffer_size
, int, S_IRUGO
);
719 module_init(netvsc_init
);
720 module_exit(netvsc_exit
);