blob: ba67a52675578e9282d64a3706549f992a9216bb [file] [log] [blame]
Stephen Hemmingerbce5c2e2018-07-21 06:31:39 -07001// SPDX-License-Identifier: GPL-2.0
Stephen Hemminger95096f22016-12-03 12:34:40 -08002/*
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 *
Stephen Hemminger95096f22016-12-03 12:34:40 -08008 * 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 *
Stephen Hemminger42896962018-01-04 14:13:27 -080011 * Associate Network GUID with UIO device
Stephen Hemminger95096f22016-12-03 12:34:40 -080012 * # echo "f8615163-df3e-46c5-913f-f2d2f965ed0e" \
Stephen Hemminger42896962018-01-04 14:13:27 -080013 * > /sys/bus/vmbus/drivers/uio_hv_generic/new_id
14 * Then rebind
15 * # echo -n "ed963694-e847-4b2a-85af-bc9cfc11d6f3" \
Stephen Hemminger95096f22016-12-03 12:34:40 -080016 * > /sys/bus/vmbus/drivers/hv_netvsc/unbind
Stephen Hemminger42896962018-01-04 14:13:27 -080017 * # echo -n "ed963694-e847-4b2a-85af-bc9cfc11d6f3" \
Stephen Hemminger95096f22016-12-03 12:34:40 -080018 * > /sys/bus/vmbus/drivers/uio_hv_generic/bind
19 */
Stephen Hemminger95096f22016-12-03 12:34:40 -080020#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
Stephen Hemminger108ddb82018-08-10 23:06:09 +000035#define DRIVER_VERSION "0.02.1"
Stephen Hemminger95096f22016-12-03 12:34:40 -080036#define DRIVER_AUTHOR "Stephen Hemminger <sthemmin at microsoft.com>"
37#define DRIVER_DESC "Generic UIO driver for VMBus devices"
38
Stephen Hemmingere7d21462018-01-09 12:57:30 -080039#define HV_RING_SIZE 512 /* pages */
Stephen Hemminger108ddb82018-08-10 23:06:09 +000040#define SEND_BUFFER_SIZE (16 * 1024 * 1024)
41#define RECV_BUFFER_SIZE (31 * 1024 * 1024)
Stephen Hemmingere7d21462018-01-09 12:57:30 -080042
Stephen Hemminger95096f22016-12-03 12:34:40 -080043/*
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,
Stephen Hemmingere7d21462018-01-09 12:57:30 -080051 RECV_BUF_MAP,
52 SEND_BUF_MAP
Stephen Hemminger95096f22016-12-03 12:34:40 -080053};
54
Stephen Hemminger95096f22016-12-03 12:34:40 -080055struct hv_uio_private_data {
56 struct uio_info info;
57 struct hv_device *device;
Stephen Hemmingere7d21462018-01-09 12:57:30 -080058
59 void *recv_buf;
60 u32 recv_gpadl;
61 char recv_name[32]; /* "recv_4294967295" */
62
63 void *send_buf;
64 u32 send_gpadl;
65 char send_name[32];
Stephen Hemminger95096f22016-12-03 12:34:40 -080066};
67
Stephen Hemminger95096f22016-12-03 12:34:40 -080068/*
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
86 return 0;
87}
88
89/*
90 * Callback from vmbus_event when something is in inbound ring.
91 */
92static void hv_uio_channel_cb(void *context)
93{
Stephen Hemminger135db382018-04-16 11:19:26 -070094 struct vmbus_channel *chan = context;
95 struct hv_device *hv_dev = chan->device_obj;
96 struct hv_uio_private_data *pdata = hv_get_drvdata(hv_dev);
Stephen Hemminger95096f22016-12-03 12:34:40 -080097
Stephen Hemminger135db382018-04-16 11:19:26 -070098 chan->inbound.ring_buffer->interrupt_mask = 1;
Stephen Hemminger95096f22016-12-03 12:34:40 -080099 virt_mb();
100
101 uio_event_notify(&pdata->info);
102}
103
Stephen Hemmingerca3cda62018-01-09 12:57:32 -0800104/*
105 * Callback from vmbus_event when channel is rescinded.
106 */
107static void hv_uio_rescind(struct vmbus_channel *channel)
108{
109 struct hv_device *hv_dev = channel->primary_channel->device_obj;
110 struct hv_uio_private_data *pdata = hv_get_drvdata(hv_dev);
111
112 /*
113 * Turn off the interrupt file handle
114 * Next read for event will return -EIO
115 */
116 pdata->info.irq = 0;
117
118 /* Wake up reader */
119 uio_event_notify(&pdata->info);
120}
Stephen Hemmingere7d21462018-01-09 12:57:30 -0800121
Stephen Hemmingerce3d1532018-04-16 11:19:27 -0700122/* Sysfs API to allow mmap of the ring buffers
123 * The ring buffer is allocated as contiguous memory by vmbus_open
Stephen Hemminger37b96a42018-02-05 10:40:27 -0800124 */
Stephen Hemminger37b96a42018-02-05 10:40:27 -0800125static int hv_uio_ring_mmap(struct file *filp, struct kobject *kobj,
126 struct bin_attribute *attr,
127 struct vm_area_struct *vma)
128{
129 struct vmbus_channel *channel
130 = container_of(kobj, struct vmbus_channel, kobj);
Stephen Hemmingerce3d1532018-04-16 11:19:27 -0700131 struct hv_device *dev = channel->primary_channel->device_obj;
132 u16 q_idx = channel->offermsg.offer.sub_channel_index;
Stephen Hemminger52a42c22018-09-14 09:10:16 -0700133 void *ring_buffer = page_address(channel->ringbuffer_page);
Stephen Hemminger37b96a42018-02-05 10:40:27 -0800134
Stephen Hemmingerce3d1532018-04-16 11:19:27 -0700135 dev_dbg(&dev->device, "mmap channel %u pages %#lx at %#lx\n",
136 q_idx, vma_pages(vma), vma->vm_pgoff);
Stephen Hemminger37b96a42018-02-05 10:40:27 -0800137
Stephen Hemminger52a42c22018-09-14 09:10:16 -0700138 return vm_iomap_memory(vma, virt_to_phys(ring_buffer),
Stephen Hemmingerce3d1532018-04-16 11:19:27 -0700139 channel->ringbuffer_pagecount << PAGE_SHIFT);
Stephen Hemminger37b96a42018-02-05 10:40:27 -0800140}
141
Stephen Hemminger6e3d66b2018-04-16 11:19:24 -0700142static const struct bin_attribute ring_buffer_bin_attr = {
Stephen Hemminger37b96a42018-02-05 10:40:27 -0800143 .attr = {
144 .name = "ring",
145 .mode = 0600,
Stephen Hemminger37b96a42018-02-05 10:40:27 -0800146 },
Stephen Hemminger6e3d66b2018-04-16 11:19:24 -0700147 .size = 2 * HV_RING_SIZE * PAGE_SIZE,
Stephen Hemminger37b96a42018-02-05 10:40:27 -0800148 .mmap = hv_uio_ring_mmap,
149};
150
Stephen Hemminger135db382018-04-16 11:19:26 -0700151/* Callback from VMBUS subsystem when new channel created. */
Stephen Hemminger37b96a42018-02-05 10:40:27 -0800152static 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;
Stephen Hemminger37b96a42018-02-05 10:40:27 -0800157 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,
Stephen Hemminger135db382018-04-16 11:19:26 -0700162 hv_uio_channel_cb, new_sc);
Stephen Hemminger37b96a42018-02-05 10:40:27 -0800163 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
Stephen Hemmingere7d21462018-01-09 12:57:30 -0800179static void
180hv_uio_cleanup(struct hv_device *dev, struct hv_uio_private_data *pdata)
181{
182 if (pdata->send_gpadl)
183 vmbus_teardown_gpadl(dev->channel, pdata->send_gpadl);
184 vfree(pdata->send_buf);
185
186 if (pdata->recv_gpadl)
187 vmbus_teardown_gpadl(dev->channel, pdata->recv_gpadl);
188 vfree(pdata->recv_buf);
189}
190
Stephen Hemminger95096f22016-12-03 12:34:40 -0800191static int
192hv_uio_probe(struct hv_device *dev,
193 const struct hv_vmbus_device_id *dev_id)
194{
195 struct hv_uio_private_data *pdata;
196 int ret;
197
198 pdata = kzalloc(sizeof(*pdata), GFP_KERNEL);
199 if (!pdata)
200 return -ENOMEM;
201
202 ret = vmbus_open(dev->channel, HV_RING_SIZE * PAGE_SIZE,
203 HV_RING_SIZE * PAGE_SIZE, NULL, 0,
Stephen Hemminger135db382018-04-16 11:19:26 -0700204 hv_uio_channel_cb, dev->channel);
Stephen Hemminger95096f22016-12-03 12:34:40 -0800205 if (ret)
206 goto fail;
207
Stephen Hemminger06028d12018-01-09 12:57:31 -0800208 /* Communicating with host has to be via shared memory not hypercall */
209 if (!dev->channel->offermsg.monitor_allocated) {
210 dev_err(&dev->device, "vmbus channel requires hypercall\n");
211 ret = -ENOTSUPP;
212 goto fail_close;
213 }
214
Stephen Hemminger95096f22016-12-03 12:34:40 -0800215 dev->channel->inbound.ring_buffer->interrupt_mask = 1;
Stephen Hemminger2141a842018-01-04 14:13:31 -0800216 set_channel_read_mode(dev->channel, HV_CALL_ISR);
Stephen Hemminger95096f22016-12-03 12:34:40 -0800217
218 /* Fill general uio info */
219 pdata->info.name = "uio_hv_generic";
220 pdata->info.version = DRIVER_VERSION;
221 pdata->info.irqcontrol = hv_uio_irqcontrol;
Stephen Hemminger95096f22016-12-03 12:34:40 -0800222 pdata->info.irq = UIO_IRQ_CUSTOM;
223
224 /* mem resources */
225 pdata->info.mem[TXRX_RING_MAP].name = "txrx_rings";
226 pdata->info.mem[TXRX_RING_MAP].addr
Stephen Hemminger52a42c22018-09-14 09:10:16 -0700227 = (uintptr_t)page_address(dev->channel->ringbuffer_page);
Stephen Hemminger95096f22016-12-03 12:34:40 -0800228 pdata->info.mem[TXRX_RING_MAP].size
Stephen Hemminger9c405462018-01-04 14:13:28 -0800229 = dev->channel->ringbuffer_pagecount << PAGE_SHIFT;
Stephen Hemminger95096f22016-12-03 12:34:40 -0800230 pdata->info.mem[TXRX_RING_MAP].memtype = UIO_MEM_LOGICAL;
231
232 pdata->info.mem[INT_PAGE_MAP].name = "int_page";
Stephen Hemminger9c405462018-01-04 14:13:28 -0800233 pdata->info.mem[INT_PAGE_MAP].addr
Arnd Bergmann72d14652018-01-10 17:42:38 +0100234 = (uintptr_t)vmbus_connection.int_page;
Stephen Hemminger95096f22016-12-03 12:34:40 -0800235 pdata->info.mem[INT_PAGE_MAP].size = PAGE_SIZE;
236 pdata->info.mem[INT_PAGE_MAP].memtype = UIO_MEM_LOGICAL;
237
Stephen Hemminger9c405462018-01-04 14:13:28 -0800238 pdata->info.mem[MON_PAGE_MAP].name = "monitor_page";
239 pdata->info.mem[MON_PAGE_MAP].addr
Arnd Bergmann72d14652018-01-10 17:42:38 +0100240 = (uintptr_t)vmbus_connection.monitor_pages[1];
Stephen Hemminger95096f22016-12-03 12:34:40 -0800241 pdata->info.mem[MON_PAGE_MAP].size = PAGE_SIZE;
242 pdata->info.mem[MON_PAGE_MAP].memtype = UIO_MEM_LOGICAL;
243
Stephen Hemmingere7d21462018-01-09 12:57:30 -0800244 pdata->recv_buf = vzalloc(RECV_BUFFER_SIZE);
245 if (pdata->recv_buf == NULL) {
246 ret = -ENOMEM;
247 goto fail_close;
248 }
249
250 ret = vmbus_establish_gpadl(dev->channel, pdata->recv_buf,
251 RECV_BUFFER_SIZE, &pdata->recv_gpadl);
252 if (ret)
253 goto fail_close;
254
255 /* put Global Physical Address Label in name */
256 snprintf(pdata->recv_name, sizeof(pdata->recv_name),
257 "recv:%u", pdata->recv_gpadl);
258 pdata->info.mem[RECV_BUF_MAP].name = pdata->recv_name;
259 pdata->info.mem[RECV_BUF_MAP].addr
Arnd Bergmannd6088e92018-01-12 16:51:14 +0100260 = (uintptr_t)pdata->recv_buf;
Stephen Hemmingere7d21462018-01-09 12:57:30 -0800261 pdata->info.mem[RECV_BUF_MAP].size = RECV_BUFFER_SIZE;
262 pdata->info.mem[RECV_BUF_MAP].memtype = UIO_MEM_VIRTUAL;
263
264
265 pdata->send_buf = vzalloc(SEND_BUFFER_SIZE);
266 if (pdata->send_buf == NULL) {
267 ret = -ENOMEM;
268 goto fail_close;
269 }
270
271 ret = vmbus_establish_gpadl(dev->channel, pdata->send_buf,
272 SEND_BUFFER_SIZE, &pdata->send_gpadl);
273 if (ret)
274 goto fail_close;
275
276 snprintf(pdata->send_name, sizeof(pdata->send_name),
277 "send:%u", pdata->send_gpadl);
278 pdata->info.mem[SEND_BUF_MAP].name = pdata->send_name;
279 pdata->info.mem[SEND_BUF_MAP].addr
Arnd Bergmannd6088e92018-01-12 16:51:14 +0100280 = (uintptr_t)pdata->send_buf;
Stephen Hemmingere7d21462018-01-09 12:57:30 -0800281 pdata->info.mem[SEND_BUF_MAP].size = SEND_BUFFER_SIZE;
282 pdata->info.mem[SEND_BUF_MAP].memtype = UIO_MEM_VIRTUAL;
283
Stephen Hemminger95096f22016-12-03 12:34:40 -0800284 pdata->info.priv = pdata;
285 pdata->device = dev;
286
287 ret = uio_register_device(&dev->device, &pdata->info);
288 if (ret) {
289 dev_err(&dev->device, "hv_uio register failed\n");
290 goto fail_close;
291 }
292
Stephen Hemmingerca3cda62018-01-09 12:57:32 -0800293 vmbus_set_chn_rescind_callback(dev->channel, hv_uio_rescind);
Stephen Hemminger37b96a42018-02-05 10:40:27 -0800294 vmbus_set_sc_create_callback(dev->channel, hv_uio_new_channel);
Stephen Hemmingerca3cda62018-01-09 12:57:32 -0800295
Stephen Hemminger9ab877a2018-04-16 11:19:25 -0700296 ret = sysfs_create_bin_file(&dev->channel->kobj, &ring_buffer_bin_attr);
297 if (ret)
298 dev_notice(&dev->device,
299 "sysfs create ring bin file failed; %d\n", ret);
300
Stephen Hemminger95096f22016-12-03 12:34:40 -0800301 hv_set_drvdata(dev, pdata);
302
303 return 0;
304
305fail_close:
Stephen Hemmingere7d21462018-01-09 12:57:30 -0800306 hv_uio_cleanup(dev, pdata);
Stephen Hemminger95096f22016-12-03 12:34:40 -0800307 vmbus_close(dev->channel);
308fail:
309 kfree(pdata);
310
311 return ret;
312}
313
314static int
315hv_uio_remove(struct hv_device *dev)
316{
317 struct hv_uio_private_data *pdata = hv_get_drvdata(dev);
318
319 if (!pdata)
320 return 0;
321
322 uio_unregister_device(&pdata->info);
Stephen Hemmingere7d21462018-01-09 12:57:30 -0800323 hv_uio_cleanup(dev, pdata);
Stephen Hemminger95096f22016-12-03 12:34:40 -0800324 hv_set_drvdata(dev, NULL);
325 vmbus_close(dev->channel);
326 kfree(pdata);
327 return 0;
328}
329
330static struct hv_driver hv_uio_drv = {
331 .name = "uio_hv_generic",
332 .id_table = NULL, /* only dynamic id's */
333 .probe = hv_uio_probe,
334 .remove = hv_uio_remove,
335};
336
337static int __init
338hyperv_module_init(void)
339{
340 return vmbus_driver_register(&hv_uio_drv);
341}
342
343static void __exit
344hyperv_module_exit(void)
345{
346 vmbus_driver_unregister(&hv_uio_drv);
347}
348
349module_init(hyperv_module_init);
350module_exit(hyperv_module_exit);
351
352MODULE_VERSION(DRIVER_VERSION);
353MODULE_LICENSE("GPL v2");
354MODULE_AUTHOR(DRIVER_AUTHOR);
355MODULE_DESCRIPTION(DRIVER_DESC);