2 * uio_hv_generic - generic UIO driver for VMBus
4 * Copyright (c) 2013-2016 Brocade Communications Systems, Inc.
5 * Copyright (c) 2016, Microsoft Corporation.
8 * This work is licensed under the terms of the GNU GPL, version 2.
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:
13 * Associate Network GUID with UIO device
14 * # echo "f8615163-df3e-46c5-913f-f2d2f965ed0e" \
15 * > /sys/bus/vmbus/drivers/uio_hv_generic/new_id
17 * # echo -n "ed963694-e847-4b2a-85af-bc9cfc11d6f3" \
18 * > /sys/bus/vmbus/drivers/hv_netvsc/unbind
19 * # echo -n "ed963694-e847-4b2a-85af-bc9cfc11d6f3" \
20 * > /sys/bus/vmbus/drivers/uio_hv_generic/bind
23 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
25 #include <linux/device.h>
26 #include <linux/kernel.h>
27 #include <linux/module.h>
28 #include <linux/uio_driver.h>
29 #include <linux/netdevice.h>
30 #include <linux/if_ether.h>
31 #include <linux/skbuff.h>
32 #include <linux/hyperv.h>
33 #include <linux/vmalloc.h>
34 #include <linux/slab.h>
36 #include "../hv/hyperv_vmbus.h"
38 #define DRIVER_VERSION "0.02.0"
39 #define DRIVER_AUTHOR "Stephen Hemminger <sthemmin at microsoft.com>"
40 #define DRIVER_DESC "Generic UIO driver for VMBus devices"
42 #define HV_RING_SIZE 512 /* pages */
43 #define SEND_BUFFER_SIZE (15 * 1024 * 1024)
44 #define RECV_BUFFER_SIZE (15 * 1024 * 1024)
47 * List of resources to be mapped to user space
48 * can be extended up to MAX_UIO_MAPS(5) items
58 struct hv_uio_private_data {
60 struct hv_device *device;
64 char recv_name[32]; /* "recv_4294967295" */
72 * This is the irqcontrol callback to be registered to uio_info.
73 * It can be used to disable/enable interrupt from user space processes.
76 * pointer to uio_info.
78 * state value. 1 to enable interrupt, 0 to disable interrupt.
81 hv_uio_irqcontrol(struct uio_info *info, s32 irq_state)
83 struct hv_uio_private_data *pdata = info->priv;
84 struct hv_device *dev = pdata->device;
86 dev->channel->inbound.ring_buffer->interrupt_mask = !irq_state;
93 * Callback from vmbus_event when something is in inbound ring.
95 static void hv_uio_channel_cb(void *context)
97 struct hv_uio_private_data *pdata = context;
98 struct hv_device *dev = pdata->device;
100 dev->channel->inbound.ring_buffer->interrupt_mask = 1;
103 uio_event_notify(&pdata->info);
107 * Callback from vmbus_event when channel is rescinded.
109 static void hv_uio_rescind(struct vmbus_channel *channel)
111 struct hv_device *hv_dev = channel->primary_channel->device_obj;
112 struct hv_uio_private_data *pdata = hv_get_drvdata(hv_dev);
115 * Turn off the interrupt file handle
116 * Next read for event will return -EIO
121 uio_event_notify(&pdata->info);
125 * Handle fault when looking for sub channel ring buffer
126 * Subchannel ring buffer is same as resource 0 which is main ring buffer
127 * This is derived from uio_vma_fault
129 static int hv_uio_vma_fault(struct vm_fault *vmf)
131 struct vm_area_struct *vma = vmf->vma;
132 void *ring_buffer = vma->vm_private_data;
136 addr = ring_buffer + (vmf->pgoff << PAGE_SHIFT);
137 page = virt_to_page(addr);
143 static const struct vm_operations_struct hv_uio_vm_ops = {
144 .fault = hv_uio_vma_fault,
147 /* Sysfs API to allow mmap of the ring buffers */
148 static int hv_uio_ring_mmap(struct file *filp, struct kobject *kobj,
149 struct bin_attribute *attr,
150 struct vm_area_struct *vma)
152 struct vmbus_channel *channel
153 = container_of(kobj, struct vmbus_channel, kobj);
154 unsigned long requested_pages, actual_pages;
156 if (vma->vm_end < vma->vm_start)
159 /* only allow 0 for now */
160 if (vma->vm_pgoff > 0)
163 requested_pages = vma_pages(vma);
164 actual_pages = 2 * HV_RING_SIZE;
165 if (requested_pages > actual_pages)
168 vma->vm_private_data = channel->ringbuffer_pages;
169 vma->vm_flags |= VM_DONTEXPAND | VM_DONTDUMP;
170 vma->vm_ops = &hv_uio_vm_ops;
174 static struct bin_attribute ring_buffer_bin_attr __ro_after_init = {
178 /* size is set at init time */
180 .mmap = hv_uio_ring_mmap,
183 /* Callback from VMBUS subystem when new channel created. */
185 hv_uio_new_channel(struct vmbus_channel *new_sc)
187 struct hv_device *hv_dev = new_sc->primary_channel->device_obj;
188 struct device *device = &hv_dev->device;
189 struct hv_uio_private_data *pdata = hv_get_drvdata(hv_dev);
190 const size_t ring_bytes = HV_RING_SIZE * PAGE_SIZE;
193 /* Create host communication ring */
194 ret = vmbus_open(new_sc, ring_bytes, ring_bytes, NULL, 0,
195 hv_uio_channel_cb, pdata);
197 dev_err(device, "vmbus_open subchannel failed: %d\n", ret);
201 /* Disable interrupts on sub channel */
202 new_sc->inbound.ring_buffer->interrupt_mask = 1;
203 set_channel_read_mode(new_sc, HV_CALL_ISR);
205 ret = sysfs_create_bin_file(&new_sc->kobj, &ring_buffer_bin_attr);
207 dev_err(device, "sysfs create ring bin file failed; %d\n", ret);
213 hv_uio_cleanup(struct hv_device *dev, struct hv_uio_private_data *pdata)
215 if (pdata->send_gpadl)
216 vmbus_teardown_gpadl(dev->channel, pdata->send_gpadl);
217 vfree(pdata->send_buf);
219 if (pdata->recv_gpadl)
220 vmbus_teardown_gpadl(dev->channel, pdata->recv_gpadl);
221 vfree(pdata->recv_buf);
225 hv_uio_probe(struct hv_device *dev,
226 const struct hv_vmbus_device_id *dev_id)
228 struct hv_uio_private_data *pdata;
231 pdata = kzalloc(sizeof(*pdata), GFP_KERNEL);
235 ret = vmbus_open(dev->channel, HV_RING_SIZE * PAGE_SIZE,
236 HV_RING_SIZE * PAGE_SIZE, NULL, 0,
237 hv_uio_channel_cb, pdata);
241 /* Communicating with host has to be via shared memory not hypercall */
242 if (!dev->channel->offermsg.monitor_allocated) {
243 dev_err(&dev->device, "vmbus channel requires hypercall\n");
248 dev->channel->inbound.ring_buffer->interrupt_mask = 1;
249 set_channel_read_mode(dev->channel, HV_CALL_ISR);
251 /* Fill general uio info */
252 pdata->info.name = "uio_hv_generic";
253 pdata->info.version = DRIVER_VERSION;
254 pdata->info.irqcontrol = hv_uio_irqcontrol;
255 pdata->info.irq = UIO_IRQ_CUSTOM;
258 pdata->info.mem[TXRX_RING_MAP].name = "txrx_rings";
259 pdata->info.mem[TXRX_RING_MAP].addr
260 = (uintptr_t)dev->channel->ringbuffer_pages;
261 pdata->info.mem[TXRX_RING_MAP].size
262 = dev->channel->ringbuffer_pagecount << PAGE_SHIFT;
263 pdata->info.mem[TXRX_RING_MAP].memtype = UIO_MEM_LOGICAL;
265 pdata->info.mem[INT_PAGE_MAP].name = "int_page";
266 pdata->info.mem[INT_PAGE_MAP].addr
267 = (uintptr_t)vmbus_connection.int_page;
268 pdata->info.mem[INT_PAGE_MAP].size = PAGE_SIZE;
269 pdata->info.mem[INT_PAGE_MAP].memtype = UIO_MEM_LOGICAL;
271 pdata->info.mem[MON_PAGE_MAP].name = "monitor_page";
272 pdata->info.mem[MON_PAGE_MAP].addr
273 = (uintptr_t)vmbus_connection.monitor_pages[1];
274 pdata->info.mem[MON_PAGE_MAP].size = PAGE_SIZE;
275 pdata->info.mem[MON_PAGE_MAP].memtype = UIO_MEM_LOGICAL;
277 pdata->recv_buf = vzalloc(RECV_BUFFER_SIZE);
278 if (pdata->recv_buf == NULL) {
283 ret = vmbus_establish_gpadl(dev->channel, pdata->recv_buf,
284 RECV_BUFFER_SIZE, &pdata->recv_gpadl);
288 /* put Global Physical Address Label in name */
289 snprintf(pdata->recv_name, sizeof(pdata->recv_name),
290 "recv:%u", pdata->recv_gpadl);
291 pdata->info.mem[RECV_BUF_MAP].name = pdata->recv_name;
292 pdata->info.mem[RECV_BUF_MAP].addr
293 = (uintptr_t)pdata->recv_buf;
294 pdata->info.mem[RECV_BUF_MAP].size = RECV_BUFFER_SIZE;
295 pdata->info.mem[RECV_BUF_MAP].memtype = UIO_MEM_VIRTUAL;
298 pdata->send_buf = vzalloc(SEND_BUFFER_SIZE);
299 if (pdata->send_buf == NULL) {
304 ret = vmbus_establish_gpadl(dev->channel, pdata->send_buf,
305 SEND_BUFFER_SIZE, &pdata->send_gpadl);
309 snprintf(pdata->send_name, sizeof(pdata->send_name),
310 "send:%u", pdata->send_gpadl);
311 pdata->info.mem[SEND_BUF_MAP].name = pdata->send_name;
312 pdata->info.mem[SEND_BUF_MAP].addr
313 = (uintptr_t)pdata->send_buf;
314 pdata->info.mem[SEND_BUF_MAP].size = SEND_BUFFER_SIZE;
315 pdata->info.mem[SEND_BUF_MAP].memtype = UIO_MEM_VIRTUAL;
317 pdata->info.priv = pdata;
320 ret = uio_register_device(&dev->device, &pdata->info);
322 dev_err(&dev->device, "hv_uio register failed\n");
326 vmbus_set_chn_rescind_callback(dev->channel, hv_uio_rescind);
327 vmbus_set_sc_create_callback(dev->channel, hv_uio_new_channel);
329 hv_set_drvdata(dev, pdata);
334 hv_uio_cleanup(dev, pdata);
335 vmbus_close(dev->channel);
343 hv_uio_remove(struct hv_device *dev)
345 struct hv_uio_private_data *pdata = hv_get_drvdata(dev);
350 uio_unregister_device(&pdata->info);
351 hv_uio_cleanup(dev, pdata);
352 hv_set_drvdata(dev, NULL);
353 vmbus_close(dev->channel);
358 static struct hv_driver hv_uio_drv = {
359 .name = "uio_hv_generic",
360 .id_table = NULL, /* only dynamic id's */
361 .probe = hv_uio_probe,
362 .remove = hv_uio_remove,
366 hyperv_module_init(void)
368 return vmbus_driver_register(&hv_uio_drv);
372 hyperv_module_exit(void)
374 vmbus_driver_unregister(&hv_uio_drv);
377 module_init(hyperv_module_init);
378 module_exit(hyperv_module_exit);
380 MODULE_VERSION(DRIVER_VERSION);
381 MODULE_LICENSE("GPL v2");
382 MODULE_AUTHOR(DRIVER_AUTHOR);
383 MODULE_DESCRIPTION(DRIVER_DESC);