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 HV_RING_SIZE	 512	/* pages */
40#define SEND_BUFFER_SIZE (16 * 1024 * 1024)
41#define RECV_BUFFER_SIZE (31 * 1024 * 1024)
42
43/*
44 * List of resources to be mapped to user space
45 * can be extended up to MAX_UIO_MAPS(5) items
46 */
47enum hv_uio_map {
48	TXRX_RING_MAP = 0,
49	INT_PAGE_MAP,
50	MON_PAGE_MAP,
51	RECV_BUF_MAP,
52	SEND_BUF_MAP
53};
54
55struct hv_uio_private_data {
56	struct uio_info info;
57	struct hv_device *device;
58	atomic_t refcnt;
59
60	void	*recv_buf;
61	u32	recv_gpadl;
62	char	recv_name[32];	/* "recv_4294967295" */
63
64	void	*send_buf;
65	u32	send_gpadl;
66	char	send_name[32];
67};
68
69/*
70 * This is the irqcontrol callback to be registered to uio_info.
71 * It can be used to disable/enable interrupt from user space processes.
72 *
73 * @param info
74 *  pointer to uio_info.
75 * @param irq_state
76 *  state value. 1 to enable interrupt, 0 to disable interrupt.
77 */
78static int
79hv_uio_irqcontrol(struct uio_info *info, s32 irq_state)
80{
81	struct hv_uio_private_data *pdata = info->priv;
82	struct hv_device *dev = pdata->device;
83
84	dev->channel->inbound.ring_buffer->interrupt_mask = !irq_state;
85	virt_mb();
86
87	return 0;
88}
89
90/*
91 * Callback from vmbus_event when something is in inbound ring.
92 */
93static void hv_uio_channel_cb(void *context)
94{
95	struct vmbus_channel *chan = context;
96	struct hv_device *hv_dev = chan->device_obj;
97	struct hv_uio_private_data *pdata = hv_get_drvdata(hv_dev);
98
99	chan->inbound.ring_buffer->interrupt_mask = 1;
100	virt_mb();
101
102	uio_event_notify(&pdata->info);
103}
104
105/*
106 * Callback from vmbus_event when channel is rescinded.
107 * It is meant for rescind of primary channels only.
108 */
109static void hv_uio_rescind(struct vmbus_channel *channel)
110{
111	struct hv_device *hv_dev = channel->device_obj;
112	struct hv_uio_private_data *pdata = hv_get_drvdata(hv_dev);
113
114	/*
115	 * Turn off the interrupt file handle
116	 * Next read for event will return -EIO
117	 */
118	pdata->info.irq = 0;
119
120	/* Wake up reader */
121	uio_event_notify(&pdata->info);
122}
123
124/* Sysfs API to allow mmap of the ring buffers
125 * The ring buffer is allocated as contiguous memory by vmbus_open
126 */
127static int hv_uio_ring_mmap(struct file *filp, struct kobject *kobj,
128			    struct bin_attribute *attr,
129			    struct vm_area_struct *vma)
130{
131	struct vmbus_channel *channel
132		= container_of(kobj, struct vmbus_channel, kobj);
133	void *ring_buffer = page_address(channel->ringbuffer_page);
134
135	if (channel->state != CHANNEL_OPENED_STATE)
136		return -ENODEV;
137
138	return vm_iomap_memory(vma, virt_to_phys(ring_buffer),
139			       channel->ringbuffer_pagecount << PAGE_SHIFT);
140}
141
142static const struct bin_attribute ring_buffer_bin_attr = {
143	.attr = {
144		.name = "ring",
145		.mode = 0600,
146	},
147	.size = 2 * HV_RING_SIZE * PAGE_SIZE,
148	.mmap = hv_uio_ring_mmap,
149};
150
151/* Callback from VMBUS subsystem when new channel created. */
152static void
153hv_uio_new_channel(struct vmbus_channel *new_sc)
154{
155	struct hv_device *hv_dev = new_sc->primary_channel->device_obj;
156	struct device *device = &hv_dev->device;
157	const size_t ring_bytes = HV_RING_SIZE * PAGE_SIZE;
158	int ret;
159
160	/* Create host communication ring */
161	ret = vmbus_open(new_sc, ring_bytes, ring_bytes, NULL, 0,
162			 hv_uio_channel_cb, new_sc);
163	if (ret) {
164		dev_err(device, "vmbus_open subchannel failed: %d\n", ret);
165		return;
166	}
167
168	/* Disable interrupts on sub channel */
169	new_sc->inbound.ring_buffer->interrupt_mask = 1;
170	set_channel_read_mode(new_sc, HV_CALL_ISR);
171
172	ret = sysfs_create_bin_file(&new_sc->kobj, &ring_buffer_bin_attr);
173	if (ret) {
174		dev_err(device, "sysfs create ring bin file failed; %d\n", ret);
175		vmbus_close(new_sc);
176	}
177}
178
179/* free the reserved buffers for send and receive */
180static void
181hv_uio_cleanup(struct hv_device *dev, struct hv_uio_private_data *pdata)
182{
183	if (pdata->send_gpadl) {
184		vmbus_teardown_gpadl(dev->channel, pdata->send_gpadl);
185		pdata->send_gpadl = 0;
186		vfree(pdata->send_buf);
187	}
188
189	if (pdata->recv_gpadl) {
190		vmbus_teardown_gpadl(dev->channel, pdata->recv_gpadl);
191		pdata->recv_gpadl = 0;
192		vfree(pdata->recv_buf);
193	}
194}
195
196/* VMBus primary channel is opened on first use */
197static int
198hv_uio_open(struct uio_info *info, struct inode *inode)
199{
200	struct hv_uio_private_data *pdata
201		= container_of(info, struct hv_uio_private_data, info);
202	struct hv_device *dev = pdata->device;
203	int ret;
204
205	if (atomic_inc_return(&pdata->refcnt) != 1)
206		return 0;
207
208	vmbus_set_chn_rescind_callback(dev->channel, hv_uio_rescind);
209	vmbus_set_sc_create_callback(dev->channel, hv_uio_new_channel);
210
211	ret = vmbus_connect_ring(dev->channel,
212				 hv_uio_channel_cb, dev->channel);
213	if (ret == 0)
214		dev->channel->inbound.ring_buffer->interrupt_mask = 1;
215	else
216		atomic_dec(&pdata->refcnt);
217
218	return ret;
219}
220
221/* VMBus primary channel is closed on last close */
222static int
223hv_uio_release(struct uio_info *info, struct inode *inode)
224{
225	struct hv_uio_private_data *pdata
226		= container_of(info, struct hv_uio_private_data, info);
227	struct hv_device *dev = pdata->device;
228	int ret = 0;
229
230	if (atomic_dec_and_test(&pdata->refcnt))
231		ret = vmbus_disconnect_ring(dev->channel);
232
233	return ret;
234}
235
236static int
237hv_uio_probe(struct hv_device *dev,
238	     const struct hv_vmbus_device_id *dev_id)
239{
240	struct vmbus_channel *channel = dev->channel;
241	struct hv_uio_private_data *pdata;
242	void *ring_buffer;
243	int ret;
244
245	/* Communicating with host has to be via shared memory not hypercall */
246	if (!channel->offermsg.monitor_allocated) {
247		dev_err(&dev->device, "vmbus channel requires hypercall\n");
248		return -ENOTSUPP;
249	}
250
251	pdata = kzalloc(sizeof(*pdata), GFP_KERNEL);
252	if (!pdata)
253		return -ENOMEM;
254
255	ret = vmbus_alloc_ring(channel, HV_RING_SIZE * PAGE_SIZE,
256			       HV_RING_SIZE * PAGE_SIZE);
257	if (ret)
258		goto fail;
259
260	set_channel_read_mode(channel, HV_CALL_ISR);
261
262	/* Fill general uio info */
263	pdata->info.name = "uio_hv_generic";
264	pdata->info.version = DRIVER_VERSION;
265	pdata->info.irqcontrol = hv_uio_irqcontrol;
266	pdata->info.open = hv_uio_open;
267	pdata->info.release = hv_uio_release;
268	pdata->info.irq = UIO_IRQ_CUSTOM;
269	atomic_set(&pdata->refcnt, 0);
270
271	/* mem resources */
272	pdata->info.mem[TXRX_RING_MAP].name = "txrx_rings";
273	ring_buffer = page_address(channel->ringbuffer_page);
274	pdata->info.mem[TXRX_RING_MAP].addr
275		= (uintptr_t)virt_to_phys(ring_buffer);
276	pdata->info.mem[TXRX_RING_MAP].size
277		= channel->ringbuffer_pagecount << PAGE_SHIFT;
278	pdata->info.mem[TXRX_RING_MAP].memtype = UIO_MEM_IOVA;
279
280	pdata->info.mem[INT_PAGE_MAP].name = "int_page";
281	pdata->info.mem[INT_PAGE_MAP].addr
282		= (uintptr_t)vmbus_connection.int_page;
283	pdata->info.mem[INT_PAGE_MAP].size = PAGE_SIZE;
284	pdata->info.mem[INT_PAGE_MAP].memtype = UIO_MEM_LOGICAL;
285
286	pdata->info.mem[MON_PAGE_MAP].name = "monitor_page";
287	pdata->info.mem[MON_PAGE_MAP].addr
288		= (uintptr_t)vmbus_connection.monitor_pages[1];
289	pdata->info.mem[MON_PAGE_MAP].size = PAGE_SIZE;
290	pdata->info.mem[MON_PAGE_MAP].memtype = UIO_MEM_LOGICAL;
291
292	pdata->recv_buf = vzalloc(RECV_BUFFER_SIZE);
293	if (pdata->recv_buf == NULL) {
294		ret = -ENOMEM;
295		goto fail_close;
296	}
297
298	ret = vmbus_establish_gpadl(channel, pdata->recv_buf,
299				    RECV_BUFFER_SIZE, &pdata->recv_gpadl);
300	if (ret) {
301		vfree(pdata->recv_buf);
302		goto fail_close;
303	}
304
305	/* put Global Physical Address Label in name */
306	snprintf(pdata->recv_name, sizeof(pdata->recv_name),
307		 "recv:%u", pdata->recv_gpadl);
308	pdata->info.mem[RECV_BUF_MAP].name = pdata->recv_name;
309	pdata->info.mem[RECV_BUF_MAP].addr
310		= (uintptr_t)pdata->recv_buf;
311	pdata->info.mem[RECV_BUF_MAP].size = RECV_BUFFER_SIZE;
312	pdata->info.mem[RECV_BUF_MAP].memtype = UIO_MEM_VIRTUAL;
313
314	pdata->send_buf = vzalloc(SEND_BUFFER_SIZE);
315	if (pdata->send_buf == NULL) {
316		ret = -ENOMEM;
317		goto fail_close;
318	}
319
320	ret = vmbus_establish_gpadl(channel, pdata->send_buf,
321				    SEND_BUFFER_SIZE, &pdata->send_gpadl);
322	if (ret) {
323		vfree(pdata->send_buf);
324		goto fail_close;
325	}
326
327	snprintf(pdata->send_name, sizeof(pdata->send_name),
328		 "send:%u", pdata->send_gpadl);
329	pdata->info.mem[SEND_BUF_MAP].name = pdata->send_name;
330	pdata->info.mem[SEND_BUF_MAP].addr
331		= (uintptr_t)pdata->send_buf;
332	pdata->info.mem[SEND_BUF_MAP].size = SEND_BUFFER_SIZE;
333	pdata->info.mem[SEND_BUF_MAP].memtype = UIO_MEM_VIRTUAL;
334
335	pdata->info.priv = pdata;
336	pdata->device = dev;
337
338	ret = uio_register_device(&dev->device, &pdata->info);
339	if (ret) {
340		dev_err(&dev->device, "hv_uio register failed\n");
341		goto fail_close;
342	}
343
344	ret = sysfs_create_bin_file(&channel->kobj, &ring_buffer_bin_attr);
345	if (ret)
346		dev_notice(&dev->device,
347			   "sysfs create ring bin file failed; %d\n", ret);
348
349	hv_set_drvdata(dev, pdata);
350
351	return 0;
352
353fail_close:
354	hv_uio_cleanup(dev, pdata);
355fail:
356	kfree(pdata);
357
358	return ret;
359}
360
361static int
362hv_uio_remove(struct hv_device *dev)
363{
364	struct hv_uio_private_data *pdata = hv_get_drvdata(dev);
365
366	if (!pdata)
367		return 0;
368
369	sysfs_remove_bin_file(&dev->channel->kobj, &ring_buffer_bin_attr);
370	uio_unregister_device(&pdata->info);
371	hv_uio_cleanup(dev, pdata);
372	hv_set_drvdata(dev, NULL);
373
374	vmbus_free_ring(dev->channel);
375	kfree(pdata);
376	return 0;
377}
378
379static struct hv_driver hv_uio_drv = {
380	.name = "uio_hv_generic",
381	.id_table = NULL, /* only dynamic id's */
382	.probe = hv_uio_probe,
383	.remove = hv_uio_remove,
384};
385
386static int __init
387hyperv_module_init(void)
388{
389	return vmbus_driver_register(&hv_uio_drv);
390}
391
392static void __exit
393hyperv_module_exit(void)
394{
395	vmbus_driver_unregister(&hv_uio_drv);
396}
397
398module_init(hyperv_module_init);
399module_exit(hyperv_module_exit);
400
401MODULE_VERSION(DRIVER_VERSION);
402MODULE_LICENSE("GPL v2");
403MODULE_AUTHOR(DRIVER_AUTHOR);
404MODULE_DESCRIPTION(DRIVER_DESC);
405