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
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.
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>
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>
33 #include <net/route.h>
35 #include <net/pkt_sched.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
;
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
);
93 DPRINT_ERR(NETVSC_DRV
,
94 "unable to open device (ret %d).", ret
);
98 netif_start_queue(net
);
100 DPRINT_ERR(NETVSC_DRV
, "unable to open device...link is down.");
103 DPRINT_EXIT(NETVSC_DRV
);
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
;
118 DPRINT_ENTER(NETVSC_DRV
);
120 netif_stop_queue(net
);
122 ret
= net_drv_obj
->OnClose(device_obj
);
124 DPRINT_ERR(NETVSC_DRV
, "unable to close device (ret %d).", ret
);
126 DPRINT_EXIT(NETVSC_DRV
);
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
);
144 dev_kfree_skb_any(skb
);
146 if (netif_queue_stopped(net
)) {
147 DPRINT_INFO(NETVSC_DRV
, "net device (%p) waking up...",
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
;
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
);
189 DPRINT_ERR(NETVSC_DRV
, "unable to allocate hv_netvsc_packet");
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 ??? */
203 /* Initialize it from the skb */
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
;
233 ret
= net_drv_obj
->OnSend(&net_device_ctx
->device_ctx
->device_obj
,
238 net_device_ctx
->stats
.tx_bytes
+= skb
->len
;
239 net_device_ctx
->stats
.tx_packets
++;
243 DPRINT_ERR(NETVSC_DRV
, "unable to send..."
244 "retrying %d...", retries
);
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
263 packet
->Completion
.Send
.SendCompletionTid
= 0;
266 * Release the resources since we will not get any send
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
);
281 * netvsc_linkstatus_callback - Link up/down notification
283 static void netvsc_linkstatus_callback(struct hv_device
*device_obj
,
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
);
293 DPRINT_ERR(NETVSC_DRV
, "got link status but net device "
294 "not initialized yet");
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
);
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
;
326 DPRINT_ENTER(NETVSC_DRV
);
329 DPRINT_ERR(NETVSC_DRV
, "got receive callback but net device "
330 "not initialized yet");
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);
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
),
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
377 net_device_ctx
->stats
.rx_dropped
++;
380 net_device_ctx
->stats
.rx_packets
++;
381 net_device_ctx
->stats
.rx_bytes
+= skb
->len
;
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
);
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
;
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
;
438 DPRINT_ENTER(NETVSC_DRV
);
440 if (!net_drv_obj
->Base
.OnDeviceAdd
)
443 net
= alloc_etherdev(sizeof(struct net_device_context
));
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
);
460 dev_set_drvdata(device
, NULL
);
462 DPRINT_ERR(NETVSC_DRV
, "unable to add netvsc device (ret %d)",
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
);
487 /* Remove the device and release the resource */
488 net_drv_obj
->Base
.OnDeviceRemove(device_obj
);
492 DPRINT_EXIT(NETVSC_DRV
);
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
;
508 DPRINT_ENTER(NETVSC_DRV
);
511 DPRINT_INFO(NETVSC
, "no net device to remove");
512 DPRINT_EXIT(NETVSC_DRV
);
516 if (!net_drv_obj
->Base
.OnDeviceRemove
) {
517 DPRINT_EXIT(NETVSC_DRV
);
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
531 ret
= net_drv_obj
->Base
.OnDeviceRemove(device_obj
);
534 DPRINT_ERR(NETVSC
, "unable to remove vsc device (ret %d)", ret
);
538 DPRINT_EXIT(NETVSC_DRV
);
542 static int netvsc_drv_exit_cb(struct device
*dev
, void *data
)
544 struct device
**curr
= (struct device
**)data
;
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
;
558 DPRINT_ENTER(NETVSC_DRV
);
564 ret
= driver_for_each_device(&drv_ctx
->driver
, NULL
,
565 ¤t_dev
, netvsc_drv_exit_cb
);
567 DPRINT_WARN(NETVSC_DRV
,
568 "driver_for_each_device returned %d", ret
);
570 if (current_dev
== NULL
)
573 /* Initiate removal from the top-down */
574 DPRINT_INFO(NETVSC_DRV
, "unregistering device (%p)...",
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
);
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
;
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
);
622 static int __init
netvsc_init(void)
626 DPRINT_ENTER(NETVSC_DRV
);
627 DPRINT_INFO(NETVSC_DRV
, "Netvsc initializing....");
629 ret
= netvsc_drv_init(NetVscInitialize
);
631 DPRINT_EXIT(NETVSC_DRV
);
636 static void __exit
netvsc_exit(void)
638 DPRINT_ENTER(NETVSC_DRV
);
640 DPRINT_EXIT(NETVSC_DRV
);
643 module_param(netvsc_ringbuffer_size
, int, S_IRUGO
);
645 module_init(netvsc_init
);
646 module_exit(netvsc_exit
);