Merge tag 'input-for-v6.10-rc5' of git://git.kernel.org/pub/scm/linux/kernel/git...
[linux-2.6-block.git] / drivers / uio / uio_hv_generic.c
CommitLineData
bce5c2ea 1// SPDX-License-Identifier: GPL-2.0
95096f2f
SH
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 *
95096f2f
SH
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 *
42896968 11 * Associate Network GUID with UIO device
95096f2f 12 * # echo "f8615163-df3e-46c5-913f-f2d2f965ed0e" \
42896968
SH
13 * > /sys/bus/vmbus/drivers/uio_hv_generic/new_id
14 * Then rebind
15 * # echo -n "ed963694-e847-4b2a-85af-bc9cfc11d6f3" \
95096f2f 16 * > /sys/bus/vmbus/drivers/hv_netvsc/unbind
42896968 17 * # echo -n "ed963694-e847-4b2a-85af-bc9cfc11d6f3" \
95096f2f
SH
18 * > /sys/bus/vmbus/drivers/uio_hv_generic/bind
19 */
95096f2f
SH
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
108ddb8f 35#define DRIVER_VERSION "0.02.1"
95096f2f
SH
36#define DRIVER_AUTHOR "Stephen Hemminger <sthemmin at microsoft.com>"
37#define DRIVER_DESC "Generic UIO driver for VMBus devices"
38
108ddb8f
SH
39#define SEND_BUFFER_SIZE (16 * 1024 * 1024)
40#define RECV_BUFFER_SIZE (31 * 1024 * 1024)
e7d21464 41
95096f2f
SH
42/*
43 * List of resources to be mapped to user space
44 * can be extended up to MAX_UIO_MAPS(5) items
45 */
46enum hv_uio_map {
47 TXRX_RING_MAP = 0,
48 INT_PAGE_MAP,
49 MON_PAGE_MAP,
e7d21464
SH
50 RECV_BUF_MAP,
51 SEND_BUF_MAP
95096f2f
SH
52};
53
95096f2f
SH
54struct hv_uio_private_data {
55 struct uio_info info;
56 struct hv_device *device;
cdfa835c 57 atomic_t refcnt;
e7d21464
SH
58
59 void *recv_buf;
d4dccf35 60 struct vmbus_gpadl recv_gpadl;
e7d21464
SH
61 char recv_name[32]; /* "recv_4294967295" */
62
63 void *send_buf;
d4dccf35 64 struct vmbus_gpadl send_gpadl;
e7d21464 65 char send_name[32];
95096f2f
SH
66};
67
95096f2f
SH
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 */
77static int
78hv_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
547fa4ff
SS
86 if (!dev->channel->offermsg.monitor_allocated && irq_state)
87 vmbus_setevent(dev->channel);
88
95096f2f
SH
89 return 0;
90}
91
92/*
93 * Callback from vmbus_event when something is in inbound ring.
94 */
95static void hv_uio_channel_cb(void *context)
96{
135db384
SH
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);
95096f2f 100
135db384 101 chan->inbound.ring_buffer->interrupt_mask = 1;
95096f2f
SH
102 virt_mb();
103
104 uio_event_notify(&pdata->info);
105}
106
ca3cda6f
SH
107/*
108 * Callback from vmbus_event when channel is rescinded.
109 */
110static void hv_uio_rescind(struct vmbus_channel *channel)
111{
112 struct hv_device *hv_dev = channel->primary_channel->device_obj;
113 struct hv_uio_private_data *pdata = hv_get_drvdata(hv_dev);
114
115 /*
116 * Turn off the interrupt file handle
117 * Next read for event will return -EIO
118 */
119 pdata->info.irq = 0;
120
121 /* Wake up reader */
122 uio_event_notify(&pdata->info);
123}
e7d21464 124
ce3d1536
SH
125/* Sysfs API to allow mmap of the ring buffers
126 * The ring buffer is allocated as contiguous memory by vmbus_open
37b96a49 127 */
37b96a49
SH
128static int hv_uio_ring_mmap(struct file *filp, struct kobject *kobj,
129 struct bin_attribute *attr,
130 struct vm_area_struct *vma)
131{
132 struct vmbus_channel *channel
133 = container_of(kobj, struct vmbus_channel, kobj);
52a42c2a 134 void *ring_buffer = page_address(channel->ringbuffer_page);
37b96a49 135
cdfa835c
SH
136 if (channel->state != CHANNEL_OPENED_STATE)
137 return -ENODEV;
37b96a49 138
52a42c2a 139 return vm_iomap_memory(vma, virt_to_phys(ring_buffer),
ce3d1536 140 channel->ringbuffer_pagecount << PAGE_SHIFT);
37b96a49
SH
141}
142
6e3d66b8 143static const struct bin_attribute ring_buffer_bin_attr = {
37b96a49
SH
144 .attr = {
145 .name = "ring",
146 .mode = 0600,
37b96a49 147 },
76457f9a 148 .size = 2 * SZ_2M,
37b96a49
SH
149 .mmap = hv_uio_ring_mmap,
150};
151
135db384 152/* Callback from VMBUS subsystem when new channel created. */
37b96a49
SH
153static void
154hv_uio_new_channel(struct vmbus_channel *new_sc)
155{
156 struct hv_device *hv_dev = new_sc->primary_channel->device_obj;
157 struct device *device = &hv_dev->device;
76457f9a 158 const size_t ring_bytes = SZ_2M;
37b96a49
SH
159 int ret;
160
161 /* Create host communication ring */
162 ret = vmbus_open(new_sc, ring_bytes, ring_bytes, NULL, 0,
135db384 163 hv_uio_channel_cb, new_sc);
37b96a49
SH
164 if (ret) {
165 dev_err(device, "vmbus_open subchannel failed: %d\n", ret);
166 return;
167 }
168
169 /* Disable interrupts on sub channel */
170 new_sc->inbound.ring_buffer->interrupt_mask = 1;
171 set_channel_read_mode(new_sc, HV_CALL_ISR);
172
173 ret = sysfs_create_bin_file(&new_sc->kobj, &ring_buffer_bin_attr);
174 if (ret) {
175 dev_err(device, "sysfs create ring bin file failed; %d\n", ret);
176 vmbus_close(new_sc);
177 }
178}
179
cdfa835c 180/* free the reserved buffers for send and receive */
e7d21464
SH
181static void
182hv_uio_cleanup(struct hv_device *dev, struct hv_uio_private_data *pdata)
183{
d4dccf35
TL
184 if (pdata->send_gpadl.gpadl_handle) {
185 vmbus_teardown_gpadl(dev->channel, &pdata->send_gpadl);
3d788b2f
RE
186 if (!pdata->send_gpadl.decrypted)
187 vfree(pdata->send_buf);
cdfa835c 188 }
e7d21464 189
d4dccf35
TL
190 if (pdata->recv_gpadl.gpadl_handle) {
191 vmbus_teardown_gpadl(dev->channel, &pdata->recv_gpadl);
3d788b2f
RE
192 if (!pdata->recv_gpadl.decrypted)
193 vfree(pdata->recv_buf);
cdfa835c
SH
194 }
195}
196
197/* VMBus primary channel is opened on first use */
198static int
199hv_uio_open(struct uio_info *info, struct inode *inode)
200{
201 struct hv_uio_private_data *pdata
202 = container_of(info, struct hv_uio_private_data, info);
203 struct hv_device *dev = pdata->device;
204 int ret;
205
206 if (atomic_inc_return(&pdata->refcnt) != 1)
207 return 0;
208
5e3c420d
SH
209 vmbus_set_chn_rescind_callback(dev->channel, hv_uio_rescind);
210 vmbus_set_sc_create_callback(dev->channel, hv_uio_new_channel);
211
cdfa835c
SH
212 ret = vmbus_connect_ring(dev->channel,
213 hv_uio_channel_cb, dev->channel);
cdfa835c
SH
214 if (ret == 0)
215 dev->channel->inbound.ring_buffer->interrupt_mask = 1;
216 else
217 atomic_dec(&pdata->refcnt);
218
219 return ret;
220}
221
222/* VMBus primary channel is closed on last close */
223static int
224hv_uio_release(struct uio_info *info, struct inode *inode)
225{
226 struct hv_uio_private_data *pdata
227 = container_of(info, struct hv_uio_private_data, info);
228 struct hv_device *dev = pdata->device;
229 int ret = 0;
230
231 if (atomic_dec_and_test(&pdata->refcnt))
232 ret = vmbus_disconnect_ring(dev->channel);
233
234 return ret;
e7d21464
SH
235}
236
95096f2f
SH
237static int
238hv_uio_probe(struct hv_device *dev,
239 const struct hv_vmbus_device_id *dev_id)
240{
cdfa835c 241 struct vmbus_channel *channel = dev->channel;
95096f2f 242 struct hv_uio_private_data *pdata;
cdfa835c 243 void *ring_buffer;
95096f2f 244 int ret;
e566ed5b 245 size_t ring_size = hv_dev_ring_size(channel);
95096f2f 246
e566ed5b 247 if (!ring_size)
76457f9a 248 ring_size = SZ_2M;
e566ed5b 249
74e71964 250 pdata = devm_kzalloc(&dev->device, sizeof(*pdata), GFP_KERNEL);
95096f2f
SH
251 if (!pdata)
252 return -ENOMEM;
253
e566ed5b 254 ret = vmbus_alloc_ring(channel, ring_size, ring_size);
95096f2f 255 if (ret)
74e71964 256 return ret;
95096f2f 257
cdfa835c 258 set_channel_read_mode(channel, HV_CALL_ISR);
95096f2f
SH
259
260 /* Fill general uio info */
261 pdata->info.name = "uio_hv_generic";
262 pdata->info.version = DRIVER_VERSION;
263 pdata->info.irqcontrol = hv_uio_irqcontrol;
cdfa835c
SH
264 pdata->info.open = hv_uio_open;
265 pdata->info.release = hv_uio_release;
95096f2f 266 pdata->info.irq = UIO_IRQ_CUSTOM;
cdfa835c 267 atomic_set(&pdata->refcnt, 0);
95096f2f
SH
268
269 /* mem resources */
270 pdata->info.mem[TXRX_RING_MAP].name = "txrx_rings";
cdfa835c 271 ring_buffer = page_address(channel->ringbuffer_page);
95096f2f 272 pdata->info.mem[TXRX_RING_MAP].addr
cdfa835c 273 = (uintptr_t)virt_to_phys(ring_buffer);
95096f2f 274 pdata->info.mem[TXRX_RING_MAP].size
cdfa835c 275 = channel->ringbuffer_pagecount << PAGE_SHIFT;
9da197f1 276 pdata->info.mem[TXRX_RING_MAP].memtype = UIO_MEM_IOVA;
95096f2f
SH
277
278 pdata->info.mem[INT_PAGE_MAP].name = "int_page";
9c40546c 279 pdata->info.mem[INT_PAGE_MAP].addr
72d14657 280 = (uintptr_t)vmbus_connection.int_page;
95096f2f
SH
281 pdata->info.mem[INT_PAGE_MAP].size = PAGE_SIZE;
282 pdata->info.mem[INT_PAGE_MAP].memtype = UIO_MEM_LOGICAL;
283
9c40546c
SH
284 pdata->info.mem[MON_PAGE_MAP].name = "monitor_page";
285 pdata->info.mem[MON_PAGE_MAP].addr
72d14657 286 = (uintptr_t)vmbus_connection.monitor_pages[1];
95096f2f
SH
287 pdata->info.mem[MON_PAGE_MAP].size = PAGE_SIZE;
288 pdata->info.mem[MON_PAGE_MAP].memtype = UIO_MEM_LOGICAL;
289
e7d21464
SH
290 pdata->recv_buf = vzalloc(RECV_BUFFER_SIZE);
291 if (pdata->recv_buf == NULL) {
292 ret = -ENOMEM;
0b0226be 293 goto fail_free_ring;
e7d21464
SH
294 }
295
cdfa835c 296 ret = vmbus_establish_gpadl(channel, pdata->recv_buf,
e7d21464 297 RECV_BUFFER_SIZE, &pdata->recv_gpadl);
3ee098f9 298 if (ret) {
3d788b2f
RE
299 if (!pdata->recv_gpadl.decrypted)
300 vfree(pdata->recv_buf);
e7d21464 301 goto fail_close;
3ee098f9 302 }
e7d21464
SH
303
304 /* put Global Physical Address Label in name */
305 snprintf(pdata->recv_name, sizeof(pdata->recv_name),
d4dccf35 306 "recv:%u", pdata->recv_gpadl.gpadl_handle);
e7d21464
SH
307 pdata->info.mem[RECV_BUF_MAP].name = pdata->recv_name;
308 pdata->info.mem[RECV_BUF_MAP].addr
d6088e9a 309 = (uintptr_t)pdata->recv_buf;
e7d21464
SH
310 pdata->info.mem[RECV_BUF_MAP].size = RECV_BUFFER_SIZE;
311 pdata->info.mem[RECV_BUF_MAP].memtype = UIO_MEM_VIRTUAL;
312
e7d21464
SH
313 pdata->send_buf = vzalloc(SEND_BUFFER_SIZE);
314 if (pdata->send_buf == NULL) {
315 ret = -ENOMEM;
316 goto fail_close;
317 }
318
cdfa835c 319 ret = vmbus_establish_gpadl(channel, pdata->send_buf,
e7d21464 320 SEND_BUFFER_SIZE, &pdata->send_gpadl);
3ee098f9 321 if (ret) {
3d788b2f
RE
322 if (!pdata->send_gpadl.decrypted)
323 vfree(pdata->send_buf);
e7d21464 324 goto fail_close;
3ee098f9 325 }
e7d21464
SH
326
327 snprintf(pdata->send_name, sizeof(pdata->send_name),
d4dccf35 328 "send:%u", pdata->send_gpadl.gpadl_handle);
e7d21464
SH
329 pdata->info.mem[SEND_BUF_MAP].name = pdata->send_name;
330 pdata->info.mem[SEND_BUF_MAP].addr
d6088e9a 331 = (uintptr_t)pdata->send_buf;
e7d21464
SH
332 pdata->info.mem[SEND_BUF_MAP].size = SEND_BUFFER_SIZE;
333 pdata->info.mem[SEND_BUF_MAP].memtype = UIO_MEM_VIRTUAL;
334
95096f2f
SH
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
cdfa835c 344 ret = sysfs_create_bin_file(&channel->kobj, &ring_buffer_bin_attr);
9ab877a6
SH
345 if (ret)
346 dev_notice(&dev->device,
347 "sysfs create ring bin file failed; %d\n", ret);
348
95096f2f
SH
349 hv_set_drvdata(dev, pdata);
350
351 return 0;
352
353fail_close:
e7d21464 354 hv_uio_cleanup(dev, pdata);
0b0226be
CJ
355fail_free_ring:
356 vmbus_free_ring(dev->channel);
95096f2f
SH
357
358 return ret;
359}
360
96ec2939 361static void
95096f2f
SH
362hv_uio_remove(struct hv_device *dev)
363{
364 struct hv_uio_private_data *pdata = hv_get_drvdata(dev);
365
366 if (!pdata)
96ec2939 367 return;
95096f2f 368
7066c2f6 369 sysfs_remove_bin_file(&dev->channel->kobj, &ring_buffer_bin_attr);
95096f2f 370 uio_unregister_device(&pdata->info);
e7d21464 371 hv_uio_cleanup(dev, pdata);
cdfa835c
SH
372
373 vmbus_free_ring(dev->channel);
95096f2f
SH
374}
375
376static struct hv_driver hv_uio_drv = {
377 .name = "uio_hv_generic",
378 .id_table = NULL, /* only dynamic id's */
379 .probe = hv_uio_probe,
380 .remove = hv_uio_remove,
381};
382
383static int __init
384hyperv_module_init(void)
385{
386 return vmbus_driver_register(&hv_uio_drv);
387}
388
389static void __exit
390hyperv_module_exit(void)
391{
392 vmbus_driver_unregister(&hv_uio_drv);
393}
394
395module_init(hyperv_module_init);
396module_exit(hyperv_module_exit);
397
398MODULE_VERSION(DRIVER_VERSION);
399MODULE_LICENSE("GPL v2");
400MODULE_AUTHOR(DRIVER_AUTHOR);
401MODULE_DESCRIPTION(DRIVER_DESC);