]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blob - drivers/staging/hv/netvsc_drv.c
Staging: Merge branch 'tidspbridge-for-2.6.39' of git://dev.omapzoom.org/pub/scm...
[mirror_ubuntu-artful-kernel.git] / drivers / staging / hv / netvsc_drv.c
1 /*
2 * Copyright (c) 2009, Microsoft Corporation.
3 *
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.
7 *
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
11 * more details.
12 *
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.
16 *
17 * Authors:
18 * Haiyang Zhang <haiyangz@microsoft.com>
19 * Hank Janssen <hjanssen@microsoft.com>
20 */
21 #include <linux/init.h>
22 #include <linux/module.h>
23 #include <linux/highmem.h>
24 #include <linux/device.h>
25 #include <linux/io.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>
31 #include <linux/in.h>
32 #include <linux/slab.h>
33 #include <linux/dmi.h>
34 #include <linux/pci.h>
35 #include <net/arp.h>
36 #include <net/route.h>
37 #include <net/sock.h>
38 #include <net/pkt_sched.h>
39 #include "osd.h"
40 #include "logging.h"
41 #include "version_info.h"
42 #include "vmbus.h"
43 #include "netvsc_api.h"
44
45 struct net_device_context {
46 /* point back to our device context */
47 struct vm_device *device_ctx;
48 unsigned long avail;
49 };
50
51 struct netvsc_driver_context {
52 /* !! These must be the first 2 fields !! */
53 /* Which is a bug FIXME! */
54 struct driver_context drv_ctx;
55 struct netvsc_driver drv_obj;
56 };
57
58 #define PACKET_PAGES_LOWATER 8
59 /* Need this many pages to handle worst case fragmented packet */
60 #define PACKET_PAGES_HIWATER (MAX_SKB_FRAGS + 2)
61
62 static int ring_size = 128;
63 module_param(ring_size, int, S_IRUGO);
64 MODULE_PARM_DESC(ring_size, "Ring buffer size (# of pages)");
65
66 /* The one and only one */
67 static struct netvsc_driver_context g_netvsc_drv;
68
69 /* no-op so the netdev core doesn't return -EINVAL when modifying the the
70 * multicast address list in SIOCADDMULTI. hv is setup to get all multicast
71 * when it calls RndisFilterOnOpen() */
72 static void netvsc_set_multicast_list(struct net_device *net)
73 {
74 }
75
76 static int netvsc_open(struct net_device *net)
77 {
78 struct net_device_context *net_device_ctx = netdev_priv(net);
79 struct hv_device *device_obj = &net_device_ctx->device_ctx->device_obj;
80 int ret = 0;
81
82 if (netif_carrier_ok(net)) {
83 /* Open up the device */
84 ret = rndis_filter_open(device_obj);
85 if (ret != 0) {
86 DPRINT_ERR(NETVSC_DRV,
87 "unable to open device (ret %d).", ret);
88 return ret;
89 }
90
91 netif_start_queue(net);
92 } else {
93 DPRINT_ERR(NETVSC_DRV, "unable to open device...link is down.");
94 }
95
96 return ret;
97 }
98
99 static int netvsc_close(struct net_device *net)
100 {
101 struct net_device_context *net_device_ctx = netdev_priv(net);
102 struct hv_device *device_obj = &net_device_ctx->device_ctx->device_obj;
103 int ret;
104
105 netif_stop_queue(net);
106
107 ret = rndis_filter_close(device_obj);
108 if (ret != 0)
109 DPRINT_ERR(NETVSC_DRV, "unable to close device (ret %d).", ret);
110
111 return ret;
112 }
113
114 static void netvsc_xmit_completion(void *context)
115 {
116 struct hv_netvsc_packet *packet = (struct hv_netvsc_packet *)context;
117 struct sk_buff *skb = (struct sk_buff *)
118 (unsigned long)packet->completion.send.send_completion_tid;
119
120 kfree(packet);
121
122 if (skb) {
123 struct net_device *net = skb->dev;
124 struct net_device_context *net_device_ctx = netdev_priv(net);
125 unsigned int num_pages = skb_shinfo(skb)->nr_frags + 2;
126
127 dev_kfree_skb_any(skb);
128
129 net_device_ctx->avail += num_pages;
130 if (net_device_ctx->avail >= PACKET_PAGES_HIWATER)
131 netif_wake_queue(net);
132 }
133 }
134
135 static int netvsc_start_xmit(struct sk_buff *skb, struct net_device *net)
136 {
137 struct net_device_context *net_device_ctx = netdev_priv(net);
138 struct driver_context *driver_ctx =
139 driver_to_driver_context(net_device_ctx->device_ctx->device.driver);
140 struct netvsc_driver_context *net_drv_ctx =
141 (struct netvsc_driver_context *)driver_ctx;
142 struct netvsc_driver *net_drv_obj = &net_drv_ctx->drv_obj;
143 struct hv_netvsc_packet *packet;
144 int ret;
145 unsigned int i, num_pages;
146
147 DPRINT_DBG(NETVSC_DRV, "xmit packet - len %d data_len %d",
148 skb->len, skb->data_len);
149
150 /* Add 1 for skb->data and additional one for RNDIS */
151 num_pages = skb_shinfo(skb)->nr_frags + 1 + 1;
152 if (num_pages > net_device_ctx->avail)
153 return NETDEV_TX_BUSY;
154
155 /* Allocate a netvsc packet based on # of frags. */
156 packet = kzalloc(sizeof(struct hv_netvsc_packet) +
157 (num_pages * sizeof(struct hv_page_buffer)) +
158 net_drv_obj->req_ext_size, GFP_ATOMIC);
159 if (!packet) {
160 /* out of memory, silently drop packet */
161 DPRINT_ERR(NETVSC_DRV, "unable to allocate hv_netvsc_packet");
162
163 dev_kfree_skb(skb);
164 net->stats.tx_dropped++;
165 return NETDEV_TX_OK;
166 }
167
168 packet->extension = (void *)(unsigned long)packet +
169 sizeof(struct hv_netvsc_packet) +
170 (num_pages * sizeof(struct hv_page_buffer));
171
172 /* Setup the rndis header */
173 packet->page_buf_cnt = num_pages;
174
175 /* TODO: Flush all write buffers/ memory fence ??? */
176 /* wmb(); */
177
178 /* Initialize it from the skb */
179 packet->total_data_buflen = skb->len;
180
181 /* Start filling in the page buffers starting after RNDIS buffer. */
182 packet->page_buf[1].pfn = virt_to_phys(skb->data) >> PAGE_SHIFT;
183 packet->page_buf[1].offset
184 = (unsigned long)skb->data & (PAGE_SIZE - 1);
185 packet->page_buf[1].len = skb_headlen(skb);
186
187 /* Additional fragments are after SKB data */
188 for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
189 skb_frag_t *f = &skb_shinfo(skb)->frags[i];
190
191 packet->page_buf[i+2].pfn = page_to_pfn(f->page);
192 packet->page_buf[i+2].offset = f->page_offset;
193 packet->page_buf[i+2].len = f->size;
194 }
195
196 /* Set the completion routine */
197 packet->completion.send.send_completion = netvsc_xmit_completion;
198 packet->completion.send.send_completion_ctx = packet;
199 packet->completion.send.send_completion_tid = (unsigned long)skb;
200
201 ret = net_drv_obj->send(&net_device_ctx->device_ctx->device_obj,
202 packet);
203 if (ret == 0) {
204 net->stats.tx_bytes += skb->len;
205 net->stats.tx_packets++;
206
207 DPRINT_DBG(NETVSC_DRV, "# of xmits %lu total size %lu",
208 net->stats.tx_packets,
209 net->stats.tx_bytes);
210
211 net_device_ctx->avail -= num_pages;
212 if (net_device_ctx->avail < PACKET_PAGES_LOWATER)
213 netif_stop_queue(net);
214 } else {
215 /* we are shutting down or bus overloaded, just drop packet */
216 net->stats.tx_dropped++;
217 netvsc_xmit_completion(packet);
218 }
219
220 return NETDEV_TX_OK;
221 }
222
223 /*
224 * netvsc_linkstatus_callback - Link up/down notification
225 */
226 static void netvsc_linkstatus_callback(struct hv_device *device_obj,
227 unsigned int status)
228 {
229 struct vm_device *device_ctx = to_vm_device(device_obj);
230 struct net_device *net = dev_get_drvdata(&device_ctx->device);
231
232 if (!net) {
233 DPRINT_ERR(NETVSC_DRV, "got link status but net device "
234 "not initialized yet");
235 return;
236 }
237
238 if (status == 1) {
239 netif_carrier_on(net);
240 netif_wake_queue(net);
241 } else {
242 netif_carrier_off(net);
243 netif_stop_queue(net);
244 }
245 }
246
247 /*
248 * netvsc_recv_callback - Callback when we receive a packet from the
249 * "wire" on the specified device.
250 */
251 static int netvsc_recv_callback(struct hv_device *device_obj,
252 struct hv_netvsc_packet *packet)
253 {
254 struct vm_device *device_ctx = to_vm_device(device_obj);
255 struct net_device *net = dev_get_drvdata(&device_ctx->device);
256 struct sk_buff *skb;
257 void *data;
258 int i;
259 unsigned long flags;
260
261 if (!net) {
262 DPRINT_ERR(NETVSC_DRV, "got receive callback but net device "
263 "not initialized yet");
264 return 0;
265 }
266
267 /* Allocate a skb - TODO direct I/O to pages? */
268 skb = netdev_alloc_skb_ip_align(net, packet->total_data_buflen);
269 if (unlikely(!skb)) {
270 ++net->stats.rx_dropped;
271 return 0;
272 }
273
274 /* for kmap_atomic */
275 local_irq_save(flags);
276
277 /*
278 * Copy to skb. This copy is needed here since the memory pointed by
279 * hv_netvsc_packet cannot be deallocated
280 */
281 for (i = 0; i < packet->page_buf_cnt; i++) {
282 data = kmap_atomic(pfn_to_page(packet->page_buf[i].pfn),
283 KM_IRQ1);
284 data = (void *)(unsigned long)data +
285 packet->page_buf[i].offset;
286
287 memcpy(skb_put(skb, packet->page_buf[i].len), data,
288 packet->page_buf[i].len);
289
290 kunmap_atomic((void *)((unsigned long)data -
291 packet->page_buf[i].offset), KM_IRQ1);
292 }
293
294 local_irq_restore(flags);
295
296 skb->protocol = eth_type_trans(skb, net);
297 skb->ip_summed = CHECKSUM_NONE;
298
299 net->stats.rx_packets++;
300 net->stats.rx_bytes += skb->len;
301
302 /*
303 * Pass the skb back up. Network stack will deallocate the skb when it
304 * is done.
305 * TODO - use NAPI?
306 */
307 netif_rx(skb);
308
309 DPRINT_DBG(NETVSC_DRV, "# of recvs %lu total size %lu",
310 net->stats.rx_packets, net->stats.rx_bytes);
311
312 return 0;
313 }
314
315 static void netvsc_get_drvinfo(struct net_device *net,
316 struct ethtool_drvinfo *info)
317 {
318 strcpy(info->driver, "hv_netvsc");
319 strcpy(info->version, HV_DRV_VERSION);
320 strcpy(info->fw_version, "N/A");
321 }
322
323 static const struct ethtool_ops ethtool_ops = {
324 .get_drvinfo = netvsc_get_drvinfo,
325 .get_sg = ethtool_op_get_sg,
326 .set_sg = ethtool_op_set_sg,
327 .get_link = ethtool_op_get_link,
328 };
329
330 static const struct net_device_ops device_ops = {
331 .ndo_open = netvsc_open,
332 .ndo_stop = netvsc_close,
333 .ndo_start_xmit = netvsc_start_xmit,
334 .ndo_set_multicast_list = netvsc_set_multicast_list,
335 .ndo_change_mtu = eth_change_mtu,
336 .ndo_validate_addr = eth_validate_addr,
337 .ndo_set_mac_address = eth_mac_addr,
338 };
339
340 static int netvsc_probe(struct device *device)
341 {
342 struct driver_context *driver_ctx =
343 driver_to_driver_context(device->driver);
344 struct netvsc_driver_context *net_drv_ctx =
345 (struct netvsc_driver_context *)driver_ctx;
346 struct netvsc_driver *net_drv_obj = &net_drv_ctx->drv_obj;
347 struct vm_device *device_ctx = device_to_vm_device(device);
348 struct hv_device *device_obj = &device_ctx->device_obj;
349 struct net_device *net = NULL;
350 struct net_device_context *net_device_ctx;
351 struct netvsc_device_info device_info;
352 int ret;
353
354 if (!net_drv_obj->base.dev_add)
355 return -1;
356
357 net = alloc_etherdev(sizeof(struct net_device_context));
358 if (!net)
359 return -1;
360
361 /* Set initial state */
362 netif_carrier_off(net);
363
364 net_device_ctx = netdev_priv(net);
365 net_device_ctx->device_ctx = device_ctx;
366 net_device_ctx->avail = ring_size;
367 dev_set_drvdata(device, net);
368
369 /* Notify the netvsc driver of the new device */
370 ret = net_drv_obj->base.dev_add(device_obj, &device_info);
371 if (ret != 0) {
372 free_netdev(net);
373 dev_set_drvdata(device, NULL);
374
375 DPRINT_ERR(NETVSC_DRV, "unable to add netvsc device (ret %d)",
376 ret);
377 return ret;
378 }
379
380 /*
381 * If carrier is still off ie we did not get a link status callback,
382 * update it if necessary
383 */
384 /*
385 * FIXME: We should use a atomic or test/set instead to avoid getting
386 * out of sync with the device's link status
387 */
388 if (!netif_carrier_ok(net))
389 if (!device_info.link_state)
390 netif_carrier_on(net);
391
392 memcpy(net->dev_addr, device_info.mac_adr, ETH_ALEN);
393
394 net->netdev_ops = &device_ops;
395
396 /* TODO: Add GSO and Checksum offload */
397 net->features = NETIF_F_SG;
398
399 SET_ETHTOOL_OPS(net, &ethtool_ops);
400 SET_NETDEV_DEV(net, device);
401
402 ret = register_netdev(net);
403 if (ret != 0) {
404 /* Remove the device and release the resource */
405 net_drv_obj->base.dev_rm(device_obj);
406 free_netdev(net);
407 }
408
409 return ret;
410 }
411
412 static int netvsc_remove(struct device *device)
413 {
414 struct driver_context *driver_ctx =
415 driver_to_driver_context(device->driver);
416 struct netvsc_driver_context *net_drv_ctx =
417 (struct netvsc_driver_context *)driver_ctx;
418 struct netvsc_driver *net_drv_obj = &net_drv_ctx->drv_obj;
419 struct vm_device *device_ctx = device_to_vm_device(device);
420 struct net_device *net = dev_get_drvdata(&device_ctx->device);
421 struct hv_device *device_obj = &device_ctx->device_obj;
422 int ret;
423
424 if (net == NULL) {
425 DPRINT_INFO(NETVSC, "no net device to remove");
426 return 0;
427 }
428
429 if (!net_drv_obj->base.dev_rm)
430 return -1;
431
432 /* Stop outbound asap */
433 netif_stop_queue(net);
434 /* netif_carrier_off(net); */
435
436 unregister_netdev(net);
437
438 /*
439 * Call to the vsc driver to let it know that the device is being
440 * removed
441 */
442 ret = net_drv_obj->base.dev_rm(device_obj);
443 if (ret != 0) {
444 /* TODO: */
445 DPRINT_ERR(NETVSC, "unable to remove vsc device (ret %d)", ret);
446 }
447
448 free_netdev(net);
449 return ret;
450 }
451
452 static int netvsc_drv_exit_cb(struct device *dev, void *data)
453 {
454 struct device **curr = (struct device **)data;
455
456 *curr = dev;
457 /* stop iterating */
458 return 1;
459 }
460
461 static void netvsc_drv_exit(void)
462 {
463 struct netvsc_driver *netvsc_drv_obj = &g_netvsc_drv.drv_obj;
464 struct driver_context *drv_ctx = &g_netvsc_drv.drv_ctx;
465 struct device *current_dev;
466 int ret;
467
468 while (1) {
469 current_dev = NULL;
470
471 /* Get the device */
472 ret = driver_for_each_device(&drv_ctx->driver, NULL,
473 &current_dev, netvsc_drv_exit_cb);
474 if (ret)
475 DPRINT_WARN(NETVSC_DRV,
476 "driver_for_each_device returned %d", ret);
477
478 if (current_dev == NULL)
479 break;
480
481 /* Initiate removal from the top-down */
482 DPRINT_INFO(NETVSC_DRV, "unregistering device (%p)...",
483 current_dev);
484
485 device_unregister(current_dev);
486 }
487
488 if (netvsc_drv_obj->base.cleanup)
489 netvsc_drv_obj->base.cleanup(&netvsc_drv_obj->base);
490
491 vmbus_child_driver_unregister(drv_ctx);
492
493 return;
494 }
495
496 static int netvsc_drv_init(int (*drv_init)(struct hv_driver *drv))
497 {
498 struct netvsc_driver *net_drv_obj = &g_netvsc_drv.drv_obj;
499 struct driver_context *drv_ctx = &g_netvsc_drv.drv_ctx;
500 int ret;
501
502 net_drv_obj->ring_buf_size = ring_size * PAGE_SIZE;
503 net_drv_obj->recv_cb = netvsc_recv_callback;
504 net_drv_obj->link_status_change = netvsc_linkstatus_callback;
505
506 /* Callback to client driver to complete the initialization */
507 drv_init(&net_drv_obj->base);
508
509 drv_ctx->driver.name = net_drv_obj->base.name;
510 memcpy(&drv_ctx->class_id, &net_drv_obj->base.dev_type,
511 sizeof(struct hv_guid));
512
513 drv_ctx->probe = netvsc_probe;
514 drv_ctx->remove = netvsc_remove;
515
516 /* The driver belongs to vmbus */
517 ret = vmbus_child_driver_register(drv_ctx);
518
519 return ret;
520 }
521
522 static const struct dmi_system_id __initconst
523 hv_netvsc_dmi_table[] __maybe_unused = {
524 {
525 .ident = "Hyper-V",
526 .matches = {
527 DMI_MATCH(DMI_SYS_VENDOR, "Microsoft Corporation"),
528 DMI_MATCH(DMI_PRODUCT_NAME, "Virtual Machine"),
529 DMI_MATCH(DMI_BOARD_NAME, "Virtual Machine"),
530 },
531 },
532 { },
533 };
534 MODULE_DEVICE_TABLE(dmi, hv_netvsc_dmi_table);
535
536 static int __init netvsc_init(void)
537 {
538 DPRINT_INFO(NETVSC_DRV, "Netvsc initializing....");
539
540 if (!dmi_check_system(hv_netvsc_dmi_table))
541 return -ENODEV;
542
543 return netvsc_drv_init(netvsc_initialize);
544 }
545
546 static void __exit netvsc_exit(void)
547 {
548 netvsc_drv_exit();
549 }
550
551 static const struct pci_device_id __initconst
552 hv_netvsc_pci_table[] __maybe_unused = {
553 { PCI_DEVICE(0x1414, 0x5353) }, /* VGA compatible controller */
554 { 0 }
555 };
556 MODULE_DEVICE_TABLE(pci, hv_netvsc_pci_table);
557
558 MODULE_LICENSE("GPL");
559 MODULE_VERSION(HV_DRV_VERSION);
560 MODULE_DESCRIPTION("Microsoft Hyper-V network driver");
561
562 module_init(netvsc_init);
563 module_exit(netvsc_exit);