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