1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * uio_hv_generic - generic UIO driver for VMBus
4 *
5 * Copyright (c) 2013-2016 Brocade Communications Systems, Inc.
6 * Copyright (c) 2016, Microsoft Corporation.
7 *
8 * Since the driver does not declare any device ids, you must allocate
9 * id and bind the device to the driver yourself. For example:
10 *
11 * Associate Network GUID with UIO device
12 * # echo "f8615163-df3e-46c5-913f-f2d2f965ed0e" \
13 * > /sys/bus/vmbus/drivers/uio_hv_generic/new_id
14 * Then rebind
15 * # echo -n "ed963694-e847-4b2a-85af-bc9cfc11d6f3" \
16 * > /sys/bus/vmbus/drivers/hv_netvsc/unbind
17 * # echo -n "ed963694-e847-4b2a-85af-bc9cfc11d6f3" \
18 * > /sys/bus/vmbus/drivers/uio_hv_generic/bind
19 */
20 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
21
22 #include <linux/device.h>
23 #include <linux/kernel.h>
24 #include <linux/module.h>
25 #include <linux/uio_driver.h>
26 #include <linux/netdevice.h>
27 #include <linux/if_ether.h>
28 #include <linux/skbuff.h>
29 #include <linux/hyperv.h>
30 #include <linux/vmalloc.h>
31 #include <linux/slab.h>
32
33 #include "../hv/hyperv_vmbus.h"
34
35 #define DRIVER_VERSION "0.02.1"
36 #define DRIVER_AUTHOR "Stephen Hemminger <sthemmin at microsoft.com>"
37 #define DRIVER_DESC "Generic UIO driver for VMBus devices"
38
39 #define SEND_BUFFER_SIZE (16 * 1024 * 1024)
40 #define RECV_BUFFER_SIZE (31 * 1024 * 1024)
41
42 /*
43 * List of resources to be mapped to user space
44 * can be extended up to MAX_UIO_MAPS(5) items
45 */
46 enum hv_uio_map {
47 TXRX_RING_MAP = 0,
48 INT_PAGE_MAP,
49 MON_PAGE_MAP,
50 RECV_BUF_MAP,
51 SEND_BUF_MAP
52 };
53
54 struct hv_uio_private_data {
55 struct uio_info info;
56 struct hv_device *device;
57 atomic_t refcnt;
58
59 void *recv_buf;
60 struct vmbus_gpadl recv_gpadl;
61 char recv_name[32]; /* "recv_4294967295" */
62
63 void *send_buf;
64 struct vmbus_gpadl send_gpadl;
65 char send_name[32];
66 };
67
68 /*
69 * This is the irqcontrol callback to be registered to uio_info.
70 * It can be used to disable/enable interrupt from user space processes.
71 *
72 * @param info
73 * pointer to uio_info.
74 * @param irq_state
75 * state value. 1 to enable interrupt, 0 to disable interrupt.
76 */
77 static int
hv_uio_irqcontrol(struct uio_info * info,s32 irq_state)78 hv_uio_irqcontrol(struct uio_info *info, s32 irq_state)
79 {
80 struct hv_uio_private_data *pdata = info->priv;
81 struct hv_device *dev = pdata->device;
82
83 dev->channel->inbound.ring_buffer->interrupt_mask = !irq_state;
84 virt_mb();
85
86 if (!dev->channel->offermsg.monitor_allocated && irq_state)
87 vmbus_setevent(dev->channel);
88
89 return 0;
90 }
91
92 /*
93 * Callback from vmbus_event when something is in inbound ring.
94 */
hv_uio_channel_cb(void * context)95 static void hv_uio_channel_cb(void *context)
96 {
97 struct vmbus_channel *chan = context;
98 struct hv_device *hv_dev = chan->device_obj;
99 struct hv_uio_private_data *pdata = hv_get_drvdata(hv_dev);
100
101 chan->inbound.ring_buffer->interrupt_mask = 1;
102 virt_mb();
103
104 uio_event_notify(&pdata->info);
105 }
106
107 /*
108 * Callback from vmbus_event when channel is rescinded.
109 * It is meant for rescind of primary channels only.
110 */
hv_uio_rescind(struct vmbus_channel * channel)111 static void hv_uio_rescind(struct vmbus_channel *channel)
112 {
113 struct hv_device *hv_dev = channel->device_obj;
114 struct hv_uio_private_data *pdata = hv_get_drvdata(hv_dev);
115
116 /*
117 * Turn off the interrupt file handle
118 * Next read for event will return -EIO
119 */
120 pdata->info.irq = 0;
121
122 /* Wake up reader */
123 uio_event_notify(&pdata->info);
124
125 /*
126 * With rescind callback registered, rescind path will not unregister the device
127 * from vmbus when the primary channel is rescinded.
128 * Without it, rescind handling is incomplete and next onoffer msg does not come.
129 * Unregister the device from vmbus here.
130 */
131 vmbus_device_unregister(channel->device_obj);
132 }
133
134 /* Function used for mmap of ring buffer sysfs interface.
135 * The ring buffer is allocated as contiguous memory by vmbus_open
136 */
137 static int
hv_uio_ring_mmap(struct vmbus_channel * channel,struct vm_area_struct * vma)138 hv_uio_ring_mmap(struct vmbus_channel *channel, struct vm_area_struct *vma)
139 {
140 void *ring_buffer = page_address(channel->ringbuffer_page);
141
142 if (channel->state != CHANNEL_OPENED_STATE)
143 return -ENODEV;
144
145 return vm_iomap_memory(vma, virt_to_phys(ring_buffer),
146 channel->ringbuffer_pagecount << PAGE_SHIFT);
147 }
148
149 /* Callback from VMBUS subsystem when new channel created. */
150 static void
hv_uio_new_channel(struct vmbus_channel * new_sc)151 hv_uio_new_channel(struct vmbus_channel *new_sc)
152 {
153 struct hv_device *hv_dev = new_sc->primary_channel->device_obj;
154 struct device *device = &hv_dev->device;
155 const size_t ring_bytes = SZ_2M;
156 int ret;
157
158 /* Create host communication ring */
159 ret = vmbus_open(new_sc, ring_bytes, ring_bytes, NULL, 0,
160 hv_uio_channel_cb, new_sc);
161 if (ret) {
162 dev_err(device, "vmbus_open subchannel failed: %d\n", ret);
163 return;
164 }
165
166 /* Disable interrupts on sub channel */
167 new_sc->inbound.ring_buffer->interrupt_mask = 1;
168 set_channel_read_mode(new_sc, HV_CALL_ISR);
169 ret = hv_create_ring_sysfs(new_sc, hv_uio_ring_mmap);
170 if (ret) {
171 dev_err(device, "sysfs create ring bin file failed; %d\n", ret);
172 vmbus_close(new_sc);
173 }
174 }
175
176 /* free the reserved buffers for send and receive */
177 static void
hv_uio_cleanup(struct hv_device * dev,struct hv_uio_private_data * pdata)178 hv_uio_cleanup(struct hv_device *dev, struct hv_uio_private_data *pdata)
179 {
180 if (pdata->send_gpadl.gpadl_handle) {
181 vmbus_teardown_gpadl(dev->channel, &pdata->send_gpadl);
182 if (!pdata->send_gpadl.decrypted)
183 vfree(pdata->send_buf);
184 }
185
186 if (pdata->recv_gpadl.gpadl_handle) {
187 vmbus_teardown_gpadl(dev->channel, &pdata->recv_gpadl);
188 if (!pdata->recv_gpadl.decrypted)
189 vfree(pdata->recv_buf);
190 }
191 }
192
193 /* VMBus primary channel is opened on first use */
194 static int
hv_uio_open(struct uio_info * info,struct inode * inode)195 hv_uio_open(struct uio_info *info, struct inode *inode)
196 {
197 struct hv_uio_private_data *pdata
198 = container_of(info, struct hv_uio_private_data, info);
199 struct hv_device *dev = pdata->device;
200 int ret;
201
202 if (atomic_inc_return(&pdata->refcnt) != 1)
203 return 0;
204
205 vmbus_set_chn_rescind_callback(dev->channel, hv_uio_rescind);
206 vmbus_set_sc_create_callback(dev->channel, hv_uio_new_channel);
207
208 ret = vmbus_connect_ring(dev->channel,
209 hv_uio_channel_cb, dev->channel);
210 if (ret == 0)
211 dev->channel->inbound.ring_buffer->interrupt_mask = 1;
212 else
213 atomic_dec(&pdata->refcnt);
214
215 return ret;
216 }
217
218 /* VMBus primary channel is closed on last close */
219 static int
hv_uio_release(struct uio_info * info,struct inode * inode)220 hv_uio_release(struct uio_info *info, struct inode *inode)
221 {
222 struct hv_uio_private_data *pdata
223 = container_of(info, struct hv_uio_private_data, info);
224 struct hv_device *dev = pdata->device;
225 int ret = 0;
226
227 if (atomic_dec_and_test(&pdata->refcnt))
228 ret = vmbus_disconnect_ring(dev->channel);
229
230 return ret;
231 }
232
233 static int
hv_uio_probe(struct hv_device * dev,const struct hv_vmbus_device_id * dev_id)234 hv_uio_probe(struct hv_device *dev,
235 const struct hv_vmbus_device_id *dev_id)
236 {
237 struct vmbus_channel *channel = dev->channel;
238 struct hv_uio_private_data *pdata;
239 void *ring_buffer;
240 int ret;
241 size_t ring_size = hv_dev_ring_size(channel);
242
243 if (!ring_size)
244 ring_size = SZ_2M;
245
246 pdata = devm_kzalloc(&dev->device, sizeof(*pdata), GFP_KERNEL);
247 if (!pdata)
248 return -ENOMEM;
249
250 ret = vmbus_alloc_ring(channel, ring_size, ring_size);
251 if (ret)
252 return ret;
253
254 set_channel_read_mode(channel, HV_CALL_ISR);
255
256 /* Fill general uio info */
257 pdata->info.name = "uio_hv_generic";
258 pdata->info.version = DRIVER_VERSION;
259 pdata->info.irqcontrol = hv_uio_irqcontrol;
260 pdata->info.open = hv_uio_open;
261 pdata->info.release = hv_uio_release;
262 pdata->info.irq = UIO_IRQ_CUSTOM;
263 atomic_set(&pdata->refcnt, 0);
264
265 /* mem resources */
266 pdata->info.mem[TXRX_RING_MAP].name = "txrx_rings";
267 ring_buffer = page_address(channel->ringbuffer_page);
268 pdata->info.mem[TXRX_RING_MAP].addr
269 = (uintptr_t)virt_to_phys(ring_buffer);
270 pdata->info.mem[TXRX_RING_MAP].size
271 = channel->ringbuffer_pagecount << PAGE_SHIFT;
272 pdata->info.mem[TXRX_RING_MAP].memtype = UIO_MEM_IOVA;
273
274 pdata->info.mem[INT_PAGE_MAP].name = "int_page";
275 pdata->info.mem[INT_PAGE_MAP].addr
276 = (uintptr_t)vmbus_connection.int_page;
277 pdata->info.mem[INT_PAGE_MAP].size = PAGE_SIZE;
278 pdata->info.mem[INT_PAGE_MAP].memtype = UIO_MEM_LOGICAL;
279
280 pdata->info.mem[MON_PAGE_MAP].name = "monitor_page";
281 pdata->info.mem[MON_PAGE_MAP].addr
282 = (uintptr_t)vmbus_connection.monitor_pages[1];
283 pdata->info.mem[MON_PAGE_MAP].size = PAGE_SIZE;
284 pdata->info.mem[MON_PAGE_MAP].memtype = UIO_MEM_LOGICAL;
285
286 if (channel->device_id == HV_NIC) {
287 pdata->recv_buf = vzalloc(RECV_BUFFER_SIZE);
288 if (!pdata->recv_buf) {
289 ret = -ENOMEM;
290 goto fail_free_ring;
291 }
292
293 ret = vmbus_establish_gpadl(channel, pdata->recv_buf,
294 RECV_BUFFER_SIZE, &pdata->recv_gpadl);
295 if (ret) {
296 if (!pdata->recv_gpadl.decrypted)
297 vfree(pdata->recv_buf);
298 goto fail_close;
299 }
300
301 /* put Global Physical Address Label in name */
302 snprintf(pdata->recv_name, sizeof(pdata->recv_name),
303 "recv:%u", pdata->recv_gpadl.gpadl_handle);
304 pdata->info.mem[RECV_BUF_MAP].name = pdata->recv_name;
305 pdata->info.mem[RECV_BUF_MAP].addr = (uintptr_t)pdata->recv_buf;
306 pdata->info.mem[RECV_BUF_MAP].size = RECV_BUFFER_SIZE;
307 pdata->info.mem[RECV_BUF_MAP].memtype = UIO_MEM_VIRTUAL;
308
309 pdata->send_buf = vzalloc(SEND_BUFFER_SIZE);
310 if (!pdata->send_buf) {
311 ret = -ENOMEM;
312 goto fail_close;
313 }
314
315 ret = vmbus_establish_gpadl(channel, pdata->send_buf,
316 SEND_BUFFER_SIZE, &pdata->send_gpadl);
317 if (ret) {
318 if (!pdata->send_gpadl.decrypted)
319 vfree(pdata->send_buf);
320 goto fail_close;
321 }
322
323 snprintf(pdata->send_name, sizeof(pdata->send_name),
324 "send:%u", pdata->send_gpadl.gpadl_handle);
325 pdata->info.mem[SEND_BUF_MAP].name = pdata->send_name;
326 pdata->info.mem[SEND_BUF_MAP].addr = (uintptr_t)pdata->send_buf;
327 pdata->info.mem[SEND_BUF_MAP].size = SEND_BUFFER_SIZE;
328 pdata->info.mem[SEND_BUF_MAP].memtype = UIO_MEM_VIRTUAL;
329 }
330
331 pdata->info.priv = pdata;
332 pdata->device = dev;
333
334 ret = uio_register_device(&dev->device, &pdata->info);
335 if (ret) {
336 dev_err(&dev->device, "hv_uio register failed\n");
337 goto fail_close;
338 }
339
340 /*
341 * This internally calls sysfs_update_group, which returns a non-zero value if it executes
342 * before sysfs_create_group. This is expected as the 'ring' will be created later in
343 * vmbus_device_register() -> vmbus_add_channel_kobj(). Thus, no need to check the return
344 * value and print warning.
345 *
346 * Creating/exposing sysfs in driver probe is not encouraged as it can lead to race
347 * conditions with userspace. For backward compatibility, "ring" sysfs could not be removed
348 * or decoupled from uio_hv_generic probe. Userspace programs can make use of inotify
349 * APIs to make sure that ring is created.
350 */
351 hv_create_ring_sysfs(channel, hv_uio_ring_mmap);
352
353 hv_set_drvdata(dev, pdata);
354
355 return 0;
356
357 fail_close:
358 hv_uio_cleanup(dev, pdata);
359 fail_free_ring:
360 vmbus_free_ring(dev->channel);
361
362 return ret;
363 }
364
365 static void
hv_uio_remove(struct hv_device * dev)366 hv_uio_remove(struct hv_device *dev)
367 {
368 struct hv_uio_private_data *pdata = hv_get_drvdata(dev);
369
370 if (!pdata)
371 return;
372
373 hv_remove_ring_sysfs(dev->channel);
374 uio_unregister_device(&pdata->info);
375 hv_uio_cleanup(dev, pdata);
376
377 vmbus_free_ring(dev->channel);
378 }
379
380 static struct hv_driver hv_uio_drv = {
381 .name = "uio_hv_generic",
382 .id_table = NULL, /* only dynamic id's */
383 .probe = hv_uio_probe,
384 .remove = hv_uio_remove,
385 };
386
387 static int __init
hyperv_module_init(void)388 hyperv_module_init(void)
389 {
390 return vmbus_driver_register(&hv_uio_drv);
391 }
392
393 static void __exit
hyperv_module_exit(void)394 hyperv_module_exit(void)
395 {
396 vmbus_driver_unregister(&hv_uio_drv);
397 }
398
399 module_init(hyperv_module_init);
400 module_exit(hyperv_module_exit);
401
402 MODULE_VERSION(DRIVER_VERSION);
403 MODULE_LICENSE("GPL v2");
404 MODULE_AUTHOR(DRIVER_AUTHOR);
405 MODULE_DESCRIPTION(DRIVER_DESC);
406