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>
32 #include <linux/slab.h>
34 #include <net/route.h>
36 #include <net/pkt_sched.h>
39 #include "VersionInfo.h"
41 #include "NetVscApi.h"
43 struct net_device_context {
44 /* point back to our device context */
45 struct vm_device *device_ctx;
49 struct netvsc_driver_context {
50 /* !! These must be the first 2 fields !! */
51 /* Which is a bug FIXME! */
52 struct driver_context drv_ctx;
53 struct netvsc_driver drv_obj;
56 #define PACKET_PAGES_LOWATER 8
57 /* Need this many pages to handle worst case fragmented packet */
58 #define PACKET_PAGES_HIWATER (MAX_SKB_FRAGS + 2)
60 static int ring_size = roundup_pow_of_two(2*MAX_SKB_FRAGS+1);
61 module_param(ring_size, int, S_IRUGO);
62 MODULE_PARM_DESC(ring_size, "Ring buffer size (# of pages)");
64 /* The one and only one */
65 static struct netvsc_driver_context g_netvsc_drv;
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 hv_device *device_obj = &net_device_ctx->device_ctx->device_obj;
77 DPRINT_ENTER(NETVSC_DRV);
79 if (netif_carrier_ok(net)) {
80 /* Open up the device */
81 ret = RndisFilterOnOpen(device_obj);
83 DPRINT_ERR(NETVSC_DRV,
84 "unable to open device (ret %d).", ret);
88 netif_start_queue(net);
90 DPRINT_ERR(NETVSC_DRV, "unable to open device...link is down.");
93 DPRINT_EXIT(NETVSC_DRV);
97 static int netvsc_close(struct net_device *net)
99 struct net_device_context *net_device_ctx = netdev_priv(net);
100 struct hv_device *device_obj = &net_device_ctx->device_ctx->device_obj;
103 DPRINT_ENTER(NETVSC_DRV);
105 netif_stop_queue(net);
107 ret = RndisFilterOnClose(device_obj);
109 DPRINT_ERR(NETVSC_DRV, "unable to close device (ret %d).", ret);
111 DPRINT_EXIT(NETVSC_DRV);
116 static void netvsc_xmit_completion(void *context)
118 struct hv_netvsc_packet *packet = (struct hv_netvsc_packet *)context;
119 struct sk_buff *skb = (struct sk_buff *)
120 (unsigned long)packet->Completion.Send.SendCompletionTid;
122 DPRINT_ENTER(NETVSC_DRV);
127 struct net_device *net = skb->dev;
128 struct net_device_context *net_device_ctx = netdev_priv(net);
129 unsigned int num_pages = skb_shinfo(skb)->nr_frags + 2;
131 dev_kfree_skb_any(skb);
133 if ((net_device_ctx->avail += num_pages) >= PACKET_PAGES_HIWATER)
134 netif_wake_queue(net);
137 DPRINT_EXIT(NETVSC_DRV);
140 static int netvsc_start_xmit(struct sk_buff *skb, struct net_device *net)
142 struct net_device_context *net_device_ctx = netdev_priv(net);
143 struct driver_context *driver_ctx =
144 driver_to_driver_context(net_device_ctx->device_ctx->device.driver);
145 struct netvsc_driver_context *net_drv_ctx =
146 (struct netvsc_driver_context *)driver_ctx;
147 struct netvsc_driver *net_drv_obj = &net_drv_ctx->drv_obj;
148 struct hv_netvsc_packet *packet;
150 unsigned int i, num_pages;
152 DPRINT_ENTER(NETVSC_DRV);
154 DPRINT_DBG(NETVSC_DRV, "xmit packet - len %d data_len %d",
155 skb->len, skb->data_len);
157 /* Add 1 for skb->data and additional one for RNDIS */
158 num_pages = skb_shinfo(skb)->nr_frags + 1 + 1;
159 if (num_pages > net_device_ctx->avail)
160 return NETDEV_TX_BUSY;
162 /* Allocate a netvsc packet based on # of frags. */
163 packet = kzalloc(sizeof(struct hv_netvsc_packet) +
164 (num_pages * sizeof(struct hv_page_buffer)) +
165 net_drv_obj->RequestExtSize, GFP_ATOMIC);
167 /* out of memory, silently drop packet */
168 DPRINT_ERR(NETVSC_DRV, "unable to allocate hv_netvsc_packet");
171 net->stats.tx_dropped++;
175 packet->Extension = (void *)(unsigned long)packet +
176 sizeof(struct hv_netvsc_packet) +
177 (num_pages * sizeof(struct hv_page_buffer));
179 /* Setup the rndis header */
180 packet->PageBufferCount = num_pages;
182 /* TODO: Flush all write buffers/ memory fence ??? */
185 /* Initialize it from the skb */
186 packet->TotalDataBufferLength = skb->len;
188 /* Start filling in the page buffers starting after RNDIS buffer. */
189 packet->PageBuffers[1].Pfn = virt_to_phys(skb->data) >> PAGE_SHIFT;
190 packet->PageBuffers[1].Offset
191 = (unsigned long)skb->data & (PAGE_SIZE - 1);
192 packet->PageBuffers[1].Length = skb_headlen(skb);
194 /* Additional fragments are after SKB data */
195 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
196 skb_frag_t *f = &skb_shinfo(skb)->frags[i];
198 packet->PageBuffers[i+2].Pfn = page_to_pfn(f->page);
199 packet->PageBuffers[i+2].Offset = f->page_offset;
200 packet->PageBuffers[i+2].Length = f->size;
203 /* Set the completion routine */
204 packet->Completion.Send.OnSendCompletion = netvsc_xmit_completion;
205 packet->Completion.Send.SendCompletionContext = packet;
206 packet->Completion.Send.SendCompletionTid = (unsigned long)skb;
208 ret = net_drv_obj->OnSend(&net_device_ctx->device_ctx->device_obj,
211 net->stats.tx_bytes += skb->len;
212 net->stats.tx_packets++;
214 DPRINT_DBG(NETVSC_DRV, "# of xmits %lu total size %lu",
215 net->stats.tx_packets,
216 net->stats.tx_bytes);
218 if ((net_device_ctx->avail -= num_pages) < PACKET_PAGES_LOWATER)
219 netif_stop_queue(net);
221 /* we are shutting down or bus overloaded, just drop packet */
222 net->stats.tx_dropped++;
223 netvsc_xmit_completion(packet);
226 DPRINT_EXIT(NETVSC_DRV);
231 * netvsc_linkstatus_callback - Link up/down notification
233 static void netvsc_linkstatus_callback(struct hv_device *device_obj,
236 struct vm_device *device_ctx = to_vm_device(device_obj);
237 struct net_device *net = dev_get_drvdata(&device_ctx->device);
239 DPRINT_ENTER(NETVSC_DRV);
242 DPRINT_ERR(NETVSC_DRV, "got link status but net device "
243 "not initialized yet");
248 netif_carrier_on(net);
249 netif_wake_queue(net);
251 netif_carrier_off(net);
252 netif_stop_queue(net);
254 DPRINT_EXIT(NETVSC_DRV);
258 * netvsc_recv_callback - Callback when we receive a packet from the
259 * "wire" on the specified device.
261 static int netvsc_recv_callback(struct hv_device *device_obj,
262 struct hv_netvsc_packet *packet)
264 struct vm_device *device_ctx = to_vm_device(device_obj);
265 struct net_device *net = dev_get_drvdata(&device_ctx->device);
271 DPRINT_ENTER(NETVSC_DRV);
274 DPRINT_ERR(NETVSC_DRV, "got receive callback but net device "
275 "not initialized yet");
279 /* Allocate a skb - TODO direct I/O to pages? */
280 skb = netdev_alloc_skb_ip_align(net, packet->TotalDataBufferLength);
281 if (unlikely(!skb)) {
282 ++net->stats.rx_dropped;
286 /* for kmap_atomic */
287 local_irq_save(flags);
290 * Copy to skb. This copy is needed here since the memory pointed by
291 * hv_netvsc_packet cannot be deallocated
293 for (i = 0; i < packet->PageBufferCount; i++) {
294 data = kmap_atomic(pfn_to_page(packet->PageBuffers[i].Pfn),
296 data = (void *)(unsigned long)data +
297 packet->PageBuffers[i].Offset;
299 memcpy(skb_put(skb, packet->PageBuffers[i].Length), data,
300 packet->PageBuffers[i].Length);
302 kunmap_atomic((void *)((unsigned long)data -
303 packet->PageBuffers[i].Offset), KM_IRQ1);
306 local_irq_restore(flags);
308 skb->protocol = eth_type_trans(skb, net);
309 skb->ip_summed = CHECKSUM_NONE;
311 net->stats.rx_packets++;
312 net->stats.rx_bytes += skb->len;
315 * Pass the skb back up. Network stack will deallocate the skb when it
321 DPRINT_DBG(NETVSC_DRV, "# of recvs %lu total size %lu",
322 net->stats.rx_packets, net->stats.rx_bytes);
324 DPRINT_EXIT(NETVSC_DRV);
329 static void netvsc_get_drvinfo(struct net_device *net,
330 struct ethtool_drvinfo *info)
332 strcpy(info->driver, "hv_netvsc");
333 strcpy(info->version, HV_DRV_VERSION);
334 strcpy(info->fw_version, "N/A");
337 static const struct ethtool_ops ethtool_ops = {
338 .get_drvinfo = netvsc_get_drvinfo,
339 .get_sg = ethtool_op_get_sg,
340 .set_sg = ethtool_op_set_sg,
341 .get_link = ethtool_op_get_link,
344 static const struct net_device_ops device_ops = {
345 .ndo_open = netvsc_open,
346 .ndo_stop = netvsc_close,
347 .ndo_start_xmit = netvsc_start_xmit,
348 .ndo_set_multicast_list = netvsc_set_multicast_list,
351 static int netvsc_probe(struct device *device)
353 struct driver_context *driver_ctx =
354 driver_to_driver_context(device->driver);
355 struct netvsc_driver_context *net_drv_ctx =
356 (struct netvsc_driver_context *)driver_ctx;
357 struct netvsc_driver *net_drv_obj = &net_drv_ctx->drv_obj;
358 struct vm_device *device_ctx = device_to_vm_device(device);
359 struct hv_device *device_obj = &device_ctx->device_obj;
360 struct net_device *net = NULL;
361 struct net_device_context *net_device_ctx;
362 struct netvsc_device_info device_info;
365 DPRINT_ENTER(NETVSC_DRV);
367 if (!net_drv_obj->Base.OnDeviceAdd)
370 net = alloc_netdev(sizeof(struct net_device_context), "seth%d",
375 /* Set initial state */
376 netif_carrier_off(net);
377 netif_stop_queue(net);
379 net_device_ctx = netdev_priv(net);
380 net_device_ctx->device_ctx = device_ctx;
381 net_device_ctx->avail = ring_size;
382 dev_set_drvdata(device, net);
384 /* Notify the netvsc driver of the new device */
385 ret = net_drv_obj->Base.OnDeviceAdd(device_obj, &device_info);
388 dev_set_drvdata(device, NULL);
390 DPRINT_ERR(NETVSC_DRV, "unable to add netvsc device (ret %d)",
396 * If carrier is still off ie we did not get a link status callback,
397 * update it if necessary
400 * FIXME: We should use a atomic or test/set instead to avoid getting
401 * out of sync with the device's link status
403 if (!netif_carrier_ok(net))
404 if (!device_info.LinkState)
405 netif_carrier_on(net);
407 memcpy(net->dev_addr, device_info.MacAddr, ETH_ALEN);
409 net->netdev_ops = &device_ops;
411 /* TODO: Add GSO and Checksum offload */
412 net->features = NETIF_F_SG;
414 SET_ETHTOOL_OPS(net, ðtool_ops);
415 SET_NETDEV_DEV(net, device);
417 ret = register_netdev(net);
419 /* Remove the device and release the resource */
420 net_drv_obj->Base.OnDeviceRemove(device_obj);
424 DPRINT_EXIT(NETVSC_DRV);
428 static int netvsc_remove(struct device *device)
430 struct driver_context *driver_ctx =
431 driver_to_driver_context(device->driver);
432 struct netvsc_driver_context *net_drv_ctx =
433 (struct netvsc_driver_context *)driver_ctx;
434 struct netvsc_driver *net_drv_obj = &net_drv_ctx->drv_obj;
435 struct vm_device *device_ctx = device_to_vm_device(device);
436 struct net_device *net = dev_get_drvdata(&device_ctx->device);
437 struct hv_device *device_obj = &device_ctx->device_obj;
440 DPRINT_ENTER(NETVSC_DRV);
443 DPRINT_INFO(NETVSC, "no net device to remove");
444 DPRINT_EXIT(NETVSC_DRV);
448 if (!net_drv_obj->Base.OnDeviceRemove) {
449 DPRINT_EXIT(NETVSC_DRV);
453 /* Stop outbound asap */
454 netif_stop_queue(net);
455 /* netif_carrier_off(net); */
457 unregister_netdev(net);
460 * Call to the vsc driver to let it know that the device is being
463 ret = net_drv_obj->Base.OnDeviceRemove(device_obj);
466 DPRINT_ERR(NETVSC, "unable to remove vsc device (ret %d)", ret);
470 DPRINT_EXIT(NETVSC_DRV);
474 static int netvsc_drv_exit_cb(struct device *dev, void *data)
476 struct device **curr = (struct device **)data;
483 static void netvsc_drv_exit(void)
485 struct netvsc_driver *netvsc_drv_obj = &g_netvsc_drv.drv_obj;
486 struct driver_context *drv_ctx = &g_netvsc_drv.drv_ctx;
487 struct device *current_dev;
490 DPRINT_ENTER(NETVSC_DRV);
496 ret = driver_for_each_device(&drv_ctx->driver, NULL,
497 ¤t_dev, netvsc_drv_exit_cb);
499 DPRINT_WARN(NETVSC_DRV,
500 "driver_for_each_device returned %d", ret);
502 if (current_dev == NULL)
505 /* Initiate removal from the top-down */
506 DPRINT_INFO(NETVSC_DRV, "unregistering device (%p)...",
509 device_unregister(current_dev);
512 if (netvsc_drv_obj->Base.OnCleanup)
513 netvsc_drv_obj->Base.OnCleanup(&netvsc_drv_obj->Base);
515 vmbus_child_driver_unregister(drv_ctx);
517 DPRINT_EXIT(NETVSC_DRV);
522 static int netvsc_drv_init(int (*drv_init)(struct hv_driver *drv))
524 struct netvsc_driver *net_drv_obj = &g_netvsc_drv.drv_obj;
525 struct driver_context *drv_ctx = &g_netvsc_drv.drv_ctx;
528 DPRINT_ENTER(NETVSC_DRV);
530 vmbus_get_interface(&net_drv_obj->Base.VmbusChannelInterface);
532 net_drv_obj->RingBufferSize = ring_size * PAGE_SIZE;
533 net_drv_obj->OnReceiveCallback = netvsc_recv_callback;
534 net_drv_obj->OnLinkStatusChanged = netvsc_linkstatus_callback;
536 /* Callback to client driver to complete the initialization */
537 drv_init(&net_drv_obj->Base);
539 drv_ctx->driver.name = net_drv_obj->Base.name;
540 memcpy(&drv_ctx->class_id, &net_drv_obj->Base.deviceType,
541 sizeof(struct hv_guid));
543 drv_ctx->probe = netvsc_probe;
544 drv_ctx->remove = netvsc_remove;
546 /* The driver belongs to vmbus */
547 ret = vmbus_child_driver_register(drv_ctx);
549 DPRINT_EXIT(NETVSC_DRV);
554 static int __init netvsc_init(void)
558 DPRINT_ENTER(NETVSC_DRV);
559 DPRINT_INFO(NETVSC_DRV, "Netvsc initializing....");
561 ret = netvsc_drv_init(NetVscInitialize);
563 DPRINT_EXIT(NETVSC_DRV);
568 static void __exit netvsc_exit(void)
570 DPRINT_ENTER(NETVSC_DRV);
572 DPRINT_EXIT(NETVSC_DRV);
575 MODULE_LICENSE("GPL");
576 MODULE_VERSION(HV_DRV_VERSION);
577 MODULE_DESCRIPTION("Microsoft Hyper-V network driver");
579 module_init(netvsc_init);
580 module_exit(netvsc_exit);