1 // SPDX-License-Identifier: GPL-2.0
3 * uio_hv_generic - generic UIO driver for VMBus
5 * Copyright (c) 2013-2016 Brocade Communications Systems, Inc.
6 * Copyright (c) 2016, Microsoft Corporation.
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:
11 * Associate Network GUID with UIO device
12 * # echo "f8615163-df3e-46c5-913f-f2d2f965ed0e" \
13 * > /sys/bus/vmbus/drivers/uio_hv_generic/new_id
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
21 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
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>
34 #include "../hv/hyperv_vmbus.h"
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"
40 #define HV_RING_SIZE 512 /* pages */
41 #define SEND_BUFFER_SIZE (15 * 1024 * 1024)
42 #define RECV_BUFFER_SIZE (15 * 1024 * 1024)
45 * List of resources to be mapped to user space
46 * can be extended up to MAX_UIO_MAPS(5) items
56 struct hv_uio_private_data
{
58 struct hv_device
*device
;
62 char recv_name
[32]; /* "recv_4294967295" */
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.
74 * pointer to uio_info.
76 * state value. 1 to enable interrupt, 0 to disable interrupt.
79 hv_uio_irqcontrol(struct uio_info
*info
, s32 irq_state
)
81 struct hv_uio_private_data
*pdata
= info
->priv
;
82 struct hv_device
*dev
= pdata
->device
;
84 dev
->channel
->inbound
.ring_buffer
->interrupt_mask
= !irq_state
;
91 * Callback from vmbus_event when something is in inbound ring.
93 static void hv_uio_channel_cb(void *context
)
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
);
99 chan
->inbound
.ring_buffer
->interrupt_mask
= 1;
102 uio_event_notify(&pdata
->info
);
106 * Callback from vmbus_event when channel is rescinded.
108 static void hv_uio_rescind(struct vmbus_channel
*channel
)
110 struct hv_device
*hv_dev
= channel
->primary_channel
->device_obj
;
111 struct hv_uio_private_data
*pdata
= hv_get_drvdata(hv_dev
);
114 * Turn off the interrupt file handle
115 * Next read for event will return -EIO
120 uio_event_notify(&pdata
->info
);
123 /* Sysfs API to allow mmap of the ring buffers
124 * The ring buffer is allocated as contiguous memory by vmbus_open
126 static int hv_uio_ring_mmap(struct file
*filp
, struct kobject
*kobj
,
127 struct bin_attribute
*attr
,
128 struct vm_area_struct
*vma
)
130 struct vmbus_channel
*channel
131 = container_of(kobj
, struct vmbus_channel
, kobj
);
132 struct hv_device
*dev
= channel
->primary_channel
->device_obj
;
133 u16 q_idx
= channel
->offermsg
.offer
.sub_channel_index
;
134 void *ring_buffer
= page_address(channel
->ringbuffer_page
);
136 dev_dbg(&dev
->device
, "mmap channel %u pages %#lx at %#lx\n",
137 q_idx
, vma_pages(vma
), vma
->vm_pgoff
);
139 return vm_iomap_memory(vma
, virt_to_phys(ring_buffer
),
140 channel
->ringbuffer_pagecount
<< PAGE_SHIFT
);
143 static const struct bin_attribute ring_buffer_bin_attr
= {
148 .size
= 2 * HV_RING_SIZE
* PAGE_SIZE
,
149 .mmap
= hv_uio_ring_mmap
,
152 /* Callback from VMBUS subsystem when new channel created. */
154 hv_uio_new_channel(struct vmbus_channel
*new_sc
)
156 struct hv_device
*hv_dev
= new_sc
->primary_channel
->device_obj
;
157 struct device
*device
= &hv_dev
->device
;
158 const size_t ring_bytes
= HV_RING_SIZE
* PAGE_SIZE
;
161 /* Create host communication ring */
162 ret
= vmbus_open(new_sc
, ring_bytes
, ring_bytes
, NULL
, 0,
163 hv_uio_channel_cb
, new_sc
);
165 dev_err(device
, "vmbus_open subchannel failed: %d\n", ret
);
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
);
173 ret
= sysfs_create_bin_file(&new_sc
->kobj
, &ring_buffer_bin_attr
);
175 dev_err(device
, "sysfs create ring bin file failed; %d\n", ret
);
181 hv_uio_cleanup(struct hv_device
*dev
, struct hv_uio_private_data
*pdata
)
183 if (pdata
->send_gpadl
)
184 vmbus_teardown_gpadl(dev
->channel
, pdata
->send_gpadl
);
185 vfree(pdata
->send_buf
);
187 if (pdata
->recv_gpadl
)
188 vmbus_teardown_gpadl(dev
->channel
, pdata
->recv_gpadl
);
189 vfree(pdata
->recv_buf
);
193 hv_uio_probe(struct hv_device
*dev
,
194 const struct hv_vmbus_device_id
*dev_id
)
196 struct hv_uio_private_data
*pdata
;
199 pdata
= kzalloc(sizeof(*pdata
), GFP_KERNEL
);
203 ret
= vmbus_open(dev
->channel
, HV_RING_SIZE
* PAGE_SIZE
,
204 HV_RING_SIZE
* PAGE_SIZE
, NULL
, 0,
205 hv_uio_channel_cb
, dev
->channel
);
209 /* Communicating with host has to be via shared memory not hypercall */
210 if (!dev
->channel
->offermsg
.monitor_allocated
) {
211 dev_err(&dev
->device
, "vmbus channel requires hypercall\n");
216 dev
->channel
->inbound
.ring_buffer
->interrupt_mask
= 1;
217 set_channel_read_mode(dev
->channel
, HV_CALL_ISR
);
219 /* Fill general uio info */
220 pdata
->info
.name
= "uio_hv_generic";
221 pdata
->info
.version
= DRIVER_VERSION
;
222 pdata
->info
.irqcontrol
= hv_uio_irqcontrol
;
223 pdata
->info
.irq
= UIO_IRQ_CUSTOM
;
226 pdata
->info
.mem
[TXRX_RING_MAP
].name
= "txrx_rings";
227 pdata
->info
.mem
[TXRX_RING_MAP
].addr
228 = (uintptr_t)page_address(dev
->channel
->ringbuffer_page
);
229 pdata
->info
.mem
[TXRX_RING_MAP
].size
230 = dev
->channel
->ringbuffer_pagecount
<< PAGE_SHIFT
;
231 pdata
->info
.mem
[TXRX_RING_MAP
].memtype
= UIO_MEM_LOGICAL
;
233 pdata
->info
.mem
[INT_PAGE_MAP
].name
= "int_page";
234 pdata
->info
.mem
[INT_PAGE_MAP
].addr
235 = (uintptr_t)vmbus_connection
.int_page
;
236 pdata
->info
.mem
[INT_PAGE_MAP
].size
= PAGE_SIZE
;
237 pdata
->info
.mem
[INT_PAGE_MAP
].memtype
= UIO_MEM_LOGICAL
;
239 pdata
->info
.mem
[MON_PAGE_MAP
].name
= "monitor_page";
240 pdata
->info
.mem
[MON_PAGE_MAP
].addr
241 = (uintptr_t)vmbus_connection
.monitor_pages
[1];
242 pdata
->info
.mem
[MON_PAGE_MAP
].size
= PAGE_SIZE
;
243 pdata
->info
.mem
[MON_PAGE_MAP
].memtype
= UIO_MEM_LOGICAL
;
245 pdata
->recv_buf
= vzalloc(RECV_BUFFER_SIZE
);
246 if (pdata
->recv_buf
== NULL
) {
251 ret
= vmbus_establish_gpadl(dev
->channel
, pdata
->recv_buf
,
252 RECV_BUFFER_SIZE
, &pdata
->recv_gpadl
);
256 /* put Global Physical Address Label in name */
257 snprintf(pdata
->recv_name
, sizeof(pdata
->recv_name
),
258 "recv:%u", pdata
->recv_gpadl
);
259 pdata
->info
.mem
[RECV_BUF_MAP
].name
= pdata
->recv_name
;
260 pdata
->info
.mem
[RECV_BUF_MAP
].addr
261 = (uintptr_t)pdata
->recv_buf
;
262 pdata
->info
.mem
[RECV_BUF_MAP
].size
= RECV_BUFFER_SIZE
;
263 pdata
->info
.mem
[RECV_BUF_MAP
].memtype
= UIO_MEM_VIRTUAL
;
266 pdata
->send_buf
= vzalloc(SEND_BUFFER_SIZE
);
267 if (pdata
->send_buf
== NULL
) {
272 ret
= vmbus_establish_gpadl(dev
->channel
, pdata
->send_buf
,
273 SEND_BUFFER_SIZE
, &pdata
->send_gpadl
);
277 snprintf(pdata
->send_name
, sizeof(pdata
->send_name
),
278 "send:%u", pdata
->send_gpadl
);
279 pdata
->info
.mem
[SEND_BUF_MAP
].name
= pdata
->send_name
;
280 pdata
->info
.mem
[SEND_BUF_MAP
].addr
281 = (uintptr_t)pdata
->send_buf
;
282 pdata
->info
.mem
[SEND_BUF_MAP
].size
= SEND_BUFFER_SIZE
;
283 pdata
->info
.mem
[SEND_BUF_MAP
].memtype
= UIO_MEM_VIRTUAL
;
285 pdata
->info
.priv
= pdata
;
288 ret
= uio_register_device(&dev
->device
, &pdata
->info
);
290 dev_err(&dev
->device
, "hv_uio register failed\n");
294 vmbus_set_chn_rescind_callback(dev
->channel
, hv_uio_rescind
);
295 vmbus_set_sc_create_callback(dev
->channel
, hv_uio_new_channel
);
297 ret
= sysfs_create_bin_file(&dev
->channel
->kobj
, &ring_buffer_bin_attr
);
299 dev_notice(&dev
->device
,
300 "sysfs create ring bin file failed; %d\n", ret
);
302 hv_set_drvdata(dev
, pdata
);
307 hv_uio_cleanup(dev
, pdata
);
308 vmbus_close(dev
->channel
);
316 hv_uio_remove(struct hv_device
*dev
)
318 struct hv_uio_private_data
*pdata
= hv_get_drvdata(dev
);
323 uio_unregister_device(&pdata
->info
);
324 hv_uio_cleanup(dev
, pdata
);
325 hv_set_drvdata(dev
, NULL
);
326 vmbus_close(dev
->channel
);
331 static struct hv_driver hv_uio_drv
= {
332 .name
= "uio_hv_generic",
333 .id_table
= NULL
, /* only dynamic id's */
334 .probe
= hv_uio_probe
,
335 .remove
= hv_uio_remove
,
339 hyperv_module_init(void)
341 return vmbus_driver_register(&hv_uio_drv
);
345 hyperv_module_exit(void)
347 vmbus_driver_unregister(&hv_uio_drv
);
350 module_init(hyperv_module_init
);
351 module_exit(hyperv_module_exit
);
353 MODULE_VERSION(DRIVER_VERSION
);
354 MODULE_LICENSE("GPL v2");
355 MODULE_AUTHOR(DRIVER_AUTHOR
);
356 MODULE_DESCRIPTION(DRIVER_DESC
);