blob: ee6d862ef4bd446e27c9c255de0ab873bfcca78f [file] [log] [blame]
Stephen Hemminger95096f22016-12-03 12:34:40 -08001/*
2 * uio_hv_generic - generic UIO driver for VMBus
3 *
4 * Copyright (c) 2013-2016 Brocade Communications Systems, Inc.
5 * Copyright (c) 2016, Microsoft Corporation.
6 *
7 *
8 * This work is licensed under the terms of the GNU GPL, version 2.
9 *
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:
12 *
Stephen Hemminger42896962018-01-04 14:13:27 -080013 * Associate Network GUID with UIO device
Stephen Hemminger95096f22016-12-03 12:34:40 -080014 * # echo "f8615163-df3e-46c5-913f-f2d2f965ed0e" \
Stephen Hemminger42896962018-01-04 14:13:27 -080015 * > /sys/bus/vmbus/drivers/uio_hv_generic/new_id
16 * Then rebind
17 * # echo -n "ed963694-e847-4b2a-85af-bc9cfc11d6f3" \
Stephen Hemminger95096f22016-12-03 12:34:40 -080018 * > /sys/bus/vmbus/drivers/hv_netvsc/unbind
Stephen Hemminger42896962018-01-04 14:13:27 -080019 * # echo -n "ed963694-e847-4b2a-85af-bc9cfc11d6f3" \
Stephen Hemminger95096f22016-12-03 12:34:40 -080020 * > /sys/bus/vmbus/drivers/uio_hv_generic/bind
21 */
22
23#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
24
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>
35
36#include "../hv/hyperv_vmbus.h"
37
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"
41
Stephen Hemmingere7d21462018-01-09 12:57:30 -080042#define HV_RING_SIZE 512 /* pages */
43#define SEND_BUFFER_SIZE (15 * 1024 * 1024)
44#define RECV_BUFFER_SIZE (15 * 1024 * 1024)
45
Stephen Hemminger95096f22016-12-03 12:34:40 -080046/*
47 * List of resources to be mapped to user space
48 * can be extended up to MAX_UIO_MAPS(5) items
49 */
50enum hv_uio_map {
51 TXRX_RING_MAP = 0,
52 INT_PAGE_MAP,
53 MON_PAGE_MAP,
Stephen Hemmingere7d21462018-01-09 12:57:30 -080054 RECV_BUF_MAP,
55 SEND_BUF_MAP
Stephen Hemminger95096f22016-12-03 12:34:40 -080056};
57
Stephen Hemminger95096f22016-12-03 12:34:40 -080058struct hv_uio_private_data {
59 struct uio_info info;
60 struct hv_device *device;
Stephen Hemmingere7d21462018-01-09 12:57:30 -080061
62 void *recv_buf;
63 u32 recv_gpadl;
64 char recv_name[32]; /* "recv_4294967295" */
65
66 void *send_buf;
67 u32 send_gpadl;
68 char send_name[32];
Stephen Hemminger95096f22016-12-03 12:34:40 -080069};
70
Stephen Hemminger95096f22016-12-03 12:34:40 -080071/*
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.
74 *
75 * @param info
76 * pointer to uio_info.
77 * @param irq_state
78 * state value. 1 to enable interrupt, 0 to disable interrupt.
79 */
80static int
81hv_uio_irqcontrol(struct uio_info *info, s32 irq_state)
82{
83 struct hv_uio_private_data *pdata = info->priv;
84 struct hv_device *dev = pdata->device;
85
86 dev->channel->inbound.ring_buffer->interrupt_mask = !irq_state;
87 virt_mb();
88
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{
97 struct hv_uio_private_data *pdata = context;
98 struct hv_device *dev = pdata->device;
99
100 dev->channel->inbound.ring_buffer->interrupt_mask = 1;
101 virt_mb();
102
103 uio_event_notify(&pdata->info);
104}
105
Stephen Hemmingere7d21462018-01-09 12:57:30 -0800106
107static void
108hv_uio_cleanup(struct hv_device *dev, struct hv_uio_private_data *pdata)
109{
110 if (pdata->send_gpadl)
111 vmbus_teardown_gpadl(dev->channel, pdata->send_gpadl);
112 vfree(pdata->send_buf);
113
114 if (pdata->recv_gpadl)
115 vmbus_teardown_gpadl(dev->channel, pdata->recv_gpadl);
116 vfree(pdata->recv_buf);
117}
118
Stephen Hemminger95096f22016-12-03 12:34:40 -0800119static int
120hv_uio_probe(struct hv_device *dev,
121 const struct hv_vmbus_device_id *dev_id)
122{
123 struct hv_uio_private_data *pdata;
124 int ret;
125
126 pdata = kzalloc(sizeof(*pdata), GFP_KERNEL);
127 if (!pdata)
128 return -ENOMEM;
129
130 ret = vmbus_open(dev->channel, HV_RING_SIZE * PAGE_SIZE,
131 HV_RING_SIZE * PAGE_SIZE, NULL, 0,
132 hv_uio_channel_cb, pdata);
133 if (ret)
134 goto fail;
135
Stephen Hemminger06028d12018-01-09 12:57:31 -0800136 /* Communicating with host has to be via shared memory not hypercall */
137 if (!dev->channel->offermsg.monitor_allocated) {
138 dev_err(&dev->device, "vmbus channel requires hypercall\n");
139 ret = -ENOTSUPP;
140 goto fail_close;
141 }
142
Stephen Hemminger95096f22016-12-03 12:34:40 -0800143 dev->channel->inbound.ring_buffer->interrupt_mask = 1;
Stephen Hemminger2141a842018-01-04 14:13:31 -0800144 set_channel_read_mode(dev->channel, HV_CALL_ISR);
Stephen Hemminger95096f22016-12-03 12:34:40 -0800145
146 /* Fill general uio info */
147 pdata->info.name = "uio_hv_generic";
148 pdata->info.version = DRIVER_VERSION;
149 pdata->info.irqcontrol = hv_uio_irqcontrol;
Stephen Hemminger95096f22016-12-03 12:34:40 -0800150 pdata->info.irq = UIO_IRQ_CUSTOM;
151
152 /* mem resources */
153 pdata->info.mem[TXRX_RING_MAP].name = "txrx_rings";
154 pdata->info.mem[TXRX_RING_MAP].addr
Stephen Hemminger9c405462018-01-04 14:13:28 -0800155 = (phys_addr_t)dev->channel->ringbuffer_pages;
Stephen Hemminger95096f22016-12-03 12:34:40 -0800156 pdata->info.mem[TXRX_RING_MAP].size
Stephen Hemminger9c405462018-01-04 14:13:28 -0800157 = dev->channel->ringbuffer_pagecount << PAGE_SHIFT;
Stephen Hemminger95096f22016-12-03 12:34:40 -0800158 pdata->info.mem[TXRX_RING_MAP].memtype = UIO_MEM_LOGICAL;
159
160 pdata->info.mem[INT_PAGE_MAP].name = "int_page";
Stephen Hemminger9c405462018-01-04 14:13:28 -0800161 pdata->info.mem[INT_PAGE_MAP].addr
162 = (phys_addr_t)vmbus_connection.int_page;
Stephen Hemminger95096f22016-12-03 12:34:40 -0800163 pdata->info.mem[INT_PAGE_MAP].size = PAGE_SIZE;
164 pdata->info.mem[INT_PAGE_MAP].memtype = UIO_MEM_LOGICAL;
165
Stephen Hemminger9c405462018-01-04 14:13:28 -0800166 pdata->info.mem[MON_PAGE_MAP].name = "monitor_page";
167 pdata->info.mem[MON_PAGE_MAP].addr
168 = (phys_addr_t)vmbus_connection.monitor_pages[1];
Stephen Hemminger95096f22016-12-03 12:34:40 -0800169 pdata->info.mem[MON_PAGE_MAP].size = PAGE_SIZE;
170 pdata->info.mem[MON_PAGE_MAP].memtype = UIO_MEM_LOGICAL;
171
Stephen Hemmingere7d21462018-01-09 12:57:30 -0800172 pdata->recv_buf = vzalloc(RECV_BUFFER_SIZE);
173 if (pdata->recv_buf == NULL) {
174 ret = -ENOMEM;
175 goto fail_close;
176 }
177
178 ret = vmbus_establish_gpadl(dev->channel, pdata->recv_buf,
179 RECV_BUFFER_SIZE, &pdata->recv_gpadl);
180 if (ret)
181 goto fail_close;
182
183 /* put Global Physical Address Label in name */
184 snprintf(pdata->recv_name, sizeof(pdata->recv_name),
185 "recv:%u", pdata->recv_gpadl);
186 pdata->info.mem[RECV_BUF_MAP].name = pdata->recv_name;
187 pdata->info.mem[RECV_BUF_MAP].addr
188 = (phys_addr_t)pdata->recv_buf;
189 pdata->info.mem[RECV_BUF_MAP].size = RECV_BUFFER_SIZE;
190 pdata->info.mem[RECV_BUF_MAP].memtype = UIO_MEM_VIRTUAL;
191
192
193 pdata->send_buf = vzalloc(SEND_BUFFER_SIZE);
194 if (pdata->send_buf == NULL) {
195 ret = -ENOMEM;
196 goto fail_close;
197 }
198
199 ret = vmbus_establish_gpadl(dev->channel, pdata->send_buf,
200 SEND_BUFFER_SIZE, &pdata->send_gpadl);
201 if (ret)
202 goto fail_close;
203
204 snprintf(pdata->send_name, sizeof(pdata->send_name),
205 "send:%u", pdata->send_gpadl);
206 pdata->info.mem[SEND_BUF_MAP].name = pdata->send_name;
207 pdata->info.mem[SEND_BUF_MAP].addr
208 = (phys_addr_t)pdata->send_buf;
209 pdata->info.mem[SEND_BUF_MAP].size = SEND_BUFFER_SIZE;
210 pdata->info.mem[SEND_BUF_MAP].memtype = UIO_MEM_VIRTUAL;
211
Stephen Hemminger95096f22016-12-03 12:34:40 -0800212 pdata->info.priv = pdata;
213 pdata->device = dev;
214
215 ret = uio_register_device(&dev->device, &pdata->info);
216 if (ret) {
217 dev_err(&dev->device, "hv_uio register failed\n");
218 goto fail_close;
219 }
220
221 hv_set_drvdata(dev, pdata);
222
223 return 0;
224
225fail_close:
Stephen Hemmingere7d21462018-01-09 12:57:30 -0800226 hv_uio_cleanup(dev, pdata);
Stephen Hemminger95096f22016-12-03 12:34:40 -0800227 vmbus_close(dev->channel);
228fail:
229 kfree(pdata);
230
231 return ret;
232}
233
234static int
235hv_uio_remove(struct hv_device *dev)
236{
237 struct hv_uio_private_data *pdata = hv_get_drvdata(dev);
238
239 if (!pdata)
240 return 0;
241
242 uio_unregister_device(&pdata->info);
Stephen Hemmingere7d21462018-01-09 12:57:30 -0800243 hv_uio_cleanup(dev, pdata);
Stephen Hemminger95096f22016-12-03 12:34:40 -0800244 hv_set_drvdata(dev, NULL);
245 vmbus_close(dev->channel);
246 kfree(pdata);
247 return 0;
248}
249
250static struct hv_driver hv_uio_drv = {
251 .name = "uio_hv_generic",
252 .id_table = NULL, /* only dynamic id's */
253 .probe = hv_uio_probe,
254 .remove = hv_uio_remove,
255};
256
257static int __init
258hyperv_module_init(void)
259{
260 return vmbus_driver_register(&hv_uio_drv);
261}
262
263static void __exit
264hyperv_module_exit(void)
265{
266 vmbus_driver_unregister(&hv_uio_drv);
267}
268
269module_init(hyperv_module_init);
270module_exit(hyperv_module_exit);
271
272MODULE_VERSION(DRIVER_VERSION);
273MODULE_LICENSE("GPL v2");
274MODULE_AUTHOR(DRIVER_AUTHOR);
275MODULE_DESCRIPTION(DRIVER_DESC);