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