]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blob - drivers/uio/uio_hv_generic.c
UBUNTU: [Config] arm64: snapdragon: DRM_MSM=m
[mirror_ubuntu-bionic-kernel.git] / drivers / uio / uio_hv_generic.c
1 /*
2 * uio_hv_generic - generic UIO driver for VMBus
3 *
4 * Copyright (c) 2013-2016 Brocade Communications Systems, Inc.
5 * Copyright (c) 2016, Microsoft Corporation.
6 *
7 *
8 * This work is licensed under the terms of the GNU GPL, version 2.
9 *
10 * Since the driver does not declare any device ids, you must allocate
11 * id and bind the device to the driver yourself. For example:
12 *
13 * # echo "f8615163-df3e-46c5-913f-f2d2f965ed0e" \
14 * > /sys/bus/vmbus/drivers/uio_hv_generic
15 * # echo -n vmbus-ed963694-e847-4b2a-85af-bc9cfc11d6f3 \
16 * > /sys/bus/vmbus/drivers/hv_netvsc/unbind
17 * # echo -n vmbus-ed963694-e847-4b2a-85af-bc9cfc11d6f3 \
18 * > /sys/bus/vmbus/drivers/uio_hv_generic/bind
19 */
20
21 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
22
23 #include <linux/device.h>
24 #include <linux/kernel.h>
25 #include <linux/module.h>
26 #include <linux/uio_driver.h>
27 #include <linux/netdevice.h>
28 #include <linux/if_ether.h>
29 #include <linux/skbuff.h>
30 #include <linux/hyperv.h>
31 #include <linux/vmalloc.h>
32 #include <linux/slab.h>
33
34 #include "../hv/hyperv_vmbus.h"
35
36 #define DRIVER_VERSION "0.02.0"
37 #define DRIVER_AUTHOR "Stephen Hemminger <sthemmin at microsoft.com>"
38 #define DRIVER_DESC "Generic UIO driver for VMBus devices"
39
40 /*
41 * List of resources to be mapped to user space
42 * can be extended up to MAX_UIO_MAPS(5) items
43 */
44 enum hv_uio_map {
45 TXRX_RING_MAP = 0,
46 INT_PAGE_MAP,
47 MON_PAGE_MAP,
48 };
49
50 #define HV_RING_SIZE 512
51
52 struct hv_uio_private_data {
53 struct uio_info info;
54 struct hv_device *device;
55 };
56
57 static int
58 hv_uio_mmap(struct uio_info *info, struct vm_area_struct *vma)
59 {
60 int mi;
61
62 if (vma->vm_pgoff >= MAX_UIO_MAPS)
63 return -EINVAL;
64
65 if (info->mem[vma->vm_pgoff].size == 0)
66 return -EINVAL;
67
68 mi = (int)vma->vm_pgoff;
69
70 return remap_pfn_range(vma, vma->vm_start,
71 info->mem[mi].addr >> PAGE_SHIFT,
72 vma->vm_end - vma->vm_start, vma->vm_page_prot);
73 }
74
75 /*
76 * This is the irqcontrol callback to be registered to uio_info.
77 * It can be used to disable/enable interrupt from user space processes.
78 *
79 * @param info
80 * pointer to uio_info.
81 * @param irq_state
82 * state value. 1 to enable interrupt, 0 to disable interrupt.
83 */
84 static int
85 hv_uio_irqcontrol(struct uio_info *info, s32 irq_state)
86 {
87 struct hv_uio_private_data *pdata = info->priv;
88 struct hv_device *dev = pdata->device;
89
90 dev->channel->inbound.ring_buffer->interrupt_mask = !irq_state;
91 virt_mb();
92
93 return 0;
94 }
95
96 /*
97 * Callback from vmbus_event when something is in inbound ring.
98 */
99 static void hv_uio_channel_cb(void *context)
100 {
101 struct hv_uio_private_data *pdata = context;
102 struct hv_device *dev = pdata->device;
103
104 dev->channel->inbound.ring_buffer->interrupt_mask = 1;
105 virt_mb();
106
107 uio_event_notify(&pdata->info);
108 }
109
110 static int
111 hv_uio_probe(struct hv_device *dev,
112 const struct hv_vmbus_device_id *dev_id)
113 {
114 struct hv_uio_private_data *pdata;
115 int ret;
116
117 pdata = kzalloc(sizeof(*pdata), GFP_KERNEL);
118 if (!pdata)
119 return -ENOMEM;
120
121 ret = vmbus_open(dev->channel, HV_RING_SIZE * PAGE_SIZE,
122 HV_RING_SIZE * PAGE_SIZE, NULL, 0,
123 hv_uio_channel_cb, pdata);
124 if (ret)
125 goto fail;
126
127 /* Communicating with host has to be via shared memory not hypercall */
128 if (!dev->channel->offermsg.monitor_allocated) {
129 dev_err(&dev->device, "vmbus channel requires hypercall\n");
130 ret = -ENOTSUPP;
131 goto fail_close;
132 }
133
134 dev->channel->inbound.ring_buffer->interrupt_mask = 1;
135 set_channel_read_mode(dev->channel, HV_CALL_DIRECT);
136
137 /* Fill general uio info */
138 pdata->info.name = "uio_hv_generic";
139 pdata->info.version = DRIVER_VERSION;
140 pdata->info.irqcontrol = hv_uio_irqcontrol;
141 pdata->info.mmap = hv_uio_mmap;
142 pdata->info.irq = UIO_IRQ_CUSTOM;
143
144 /* mem resources */
145 pdata->info.mem[TXRX_RING_MAP].name = "txrx_rings";
146 pdata->info.mem[TXRX_RING_MAP].addr
147 = virt_to_phys(dev->channel->ringbuffer_pages);
148 pdata->info.mem[TXRX_RING_MAP].size
149 = dev->channel->ringbuffer_pagecount * PAGE_SIZE;
150 pdata->info.mem[TXRX_RING_MAP].memtype = UIO_MEM_LOGICAL;
151
152 pdata->info.mem[INT_PAGE_MAP].name = "int_page";
153 pdata->info.mem[INT_PAGE_MAP].addr =
154 virt_to_phys(vmbus_connection.int_page);
155 pdata->info.mem[INT_PAGE_MAP].size = PAGE_SIZE;
156 pdata->info.mem[INT_PAGE_MAP].memtype = UIO_MEM_LOGICAL;
157
158 pdata->info.mem[MON_PAGE_MAP].name = "monitor_pages";
159 pdata->info.mem[MON_PAGE_MAP].addr =
160 virt_to_phys(vmbus_connection.monitor_pages[1]);
161 pdata->info.mem[MON_PAGE_MAP].size = PAGE_SIZE;
162 pdata->info.mem[MON_PAGE_MAP].memtype = UIO_MEM_LOGICAL;
163
164 pdata->info.priv = pdata;
165 pdata->device = dev;
166
167 ret = uio_register_device(&dev->device, &pdata->info);
168 if (ret) {
169 dev_err(&dev->device, "hv_uio register failed\n");
170 goto fail_close;
171 }
172
173 hv_set_drvdata(dev, pdata);
174
175 return 0;
176
177 fail_close:
178 vmbus_close(dev->channel);
179 fail:
180 kfree(pdata);
181
182 return ret;
183 }
184
185 static int
186 hv_uio_remove(struct hv_device *dev)
187 {
188 struct hv_uio_private_data *pdata = hv_get_drvdata(dev);
189
190 if (!pdata)
191 return 0;
192
193 uio_unregister_device(&pdata->info);
194 hv_set_drvdata(dev, NULL);
195 vmbus_close(dev->channel);
196 kfree(pdata);
197 return 0;
198 }
199
200 static struct hv_driver hv_uio_drv = {
201 .name = "uio_hv_generic",
202 .id_table = NULL, /* only dynamic id's */
203 .probe = hv_uio_probe,
204 .remove = hv_uio_remove,
205 };
206
207 static int __init
208 hyperv_module_init(void)
209 {
210 return vmbus_driver_register(&hv_uio_drv);
211 }
212
213 static void __exit
214 hyperv_module_exit(void)
215 {
216 vmbus_driver_unregister(&hv_uio_drv);
217 }
218
219 module_init(hyperv_module_init);
220 module_exit(hyperv_module_exit);
221
222 MODULE_VERSION(DRIVER_VERSION);
223 MODULE_LICENSE("GPL v2");
224 MODULE_AUTHOR(DRIVER_AUTHOR);
225 MODULE_DESCRIPTION(DRIVER_DESC);