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 * 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>
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>
32 #include <net/route.h>
34 #include <net/pkt_sched.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;
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);
91 DPRINT_ERR(NETVSC_DRV,
92 "unable to open device (ret %d).", ret);
96 netif_start_queue(net);
98 DPRINT_ERR(NETVSC_DRV, "unable to open device...link is down.");
101 DPRINT_EXIT(NETVSC_DRV);
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;
116 DPRINT_ENTER(NETVSC_DRV);
118 netif_stop_queue(net);
120 ret = net_drv_obj->OnClose(device_obj);
122 DPRINT_ERR(NETVSC_DRV, "unable to close device (ret %d).", ret);
124 DPRINT_EXIT(NETVSC_DRV);
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);
142 dev_kfree_skb_any(skb);
144 if (netif_queue_stopped(net)) {
145 DPRINT_INFO(NETVSC_DRV, "net device (%p) waking up...",
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;
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);
187 DPRINT_ERR(NETVSC_DRV, "unable to allocate hv_netvsc_packet");
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 ??? */
201 /* Initialize it from the skb */
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;
231 ret = net_drv_obj->OnSend(&net_device_ctx->device_ctx->device_obj,
236 net_device_ctx->stats.tx_bytes += skb->len;
237 net_device_ctx->stats.tx_packets++;
241 DPRINT_ERR(NETVSC_DRV, "unable to send..."
242 "retrying %d...", retries);
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
261 packet->Completion.Send.SendCompletionTid = 0;
264 * Release the resources since we will not get any send
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);
279 * netvsc_linkstatus_callback - Link up/down notification
281 static void netvsc_linkstatus_callback(struct hv_device *device_obj,
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);
290 DPRINT_ERR(NETVSC_DRV, "got link status but net device "
291 "not initialized yet");
296 netif_carrier_on(net);
297 netif_wake_queue(net);
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;
320 DPRINT_ENTER(NETVSC_DRV);
323 DPRINT_ERR(NETVSC_DRV, "got receive callback but net device "
324 "not initialized yet");
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);
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),
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
371 net_device_ctx->stats.rx_dropped++;
374 net_device_ctx->stats.rx_packets++;
375 net_device_ctx->stats.rx_bytes += skb->len;
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);
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;
410 DPRINT_ENTER(NETVSC_DRV);
412 if (!net_drv_obj->Base.OnDeviceAdd)
415 net = alloc_netdev(sizeof(struct net_device_context), "seth%d",
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);
432 dev_set_drvdata(device, NULL);
434 DPRINT_ERR(NETVSC_DRV, "unable to add netvsc device (ret %d)",
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);
459 /* Remove the device and release the resource */
460 net_drv_obj->Base.OnDeviceRemove(device_obj);
464 DPRINT_EXIT(NETVSC_DRV);
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;
480 DPRINT_ENTER(NETVSC_DRV);
483 DPRINT_INFO(NETVSC, "no net device to remove");
484 DPRINT_EXIT(NETVSC_DRV);
488 if (!net_drv_obj->Base.OnDeviceRemove) {
489 DPRINT_EXIT(NETVSC_DRV);
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
503 ret = net_drv_obj->Base.OnDeviceRemove(device_obj);
506 DPRINT_ERR(NETVSC, "unable to remove vsc device (ret %d)", ret);
510 DPRINT_EXIT(NETVSC_DRV);
514 static int netvsc_drv_exit_cb(struct device *dev, void *data)
516 struct device **curr = (struct device **)data;
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;
530 DPRINT_ENTER(NETVSC_DRV);
536 ret = driver_for_each_device(&drv_ctx->driver, NULL,
537 ¤t_dev, netvsc_drv_exit_cb);
539 DPRINT_WARN(NETVSC_DRV,
540 "driver_for_each_device returned %d", ret);
542 if (current_dev == NULL)
545 /* Initiate removal from the top-down */
546 DPRINT_INFO(NETVSC_DRV, "unregistering device (%p)...",
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);
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;
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);
594 static int __init netvsc_init(void)
598 DPRINT_ENTER(NETVSC_DRV);
599 DPRINT_INFO(NETVSC_DRV, "Netvsc initializing....");
601 ret = netvsc_drv_init(NetVscInitialize);
603 DPRINT_EXIT(NETVSC_DRV);
608 static void __exit netvsc_exit(void)
610 DPRINT_ENTER(NETVSC_DRV);
612 DPRINT_EXIT(NETVSC_DRV);
615 module_param(netvsc_ringbuffer_size, int, S_IRUGO);
617 module_init(netvsc_init);
618 module_exit(netvsc_exit);