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