Stephen Hemminger | bce5c2e | 2018-07-21 06:31:39 -0700 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
Stephen Hemminger | 95096f2 | 2016-12-03 12:34:40 -0800 | [diff] [blame] | 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 | * |
Stephen Hemminger | 95096f2 | 2016-12-03 12:34:40 -0800 | [diff] [blame] | 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 | * |
Stephen Hemminger | 4289696 | 2018-01-04 14:13:27 -0800 | [diff] [blame] | 11 | * Associate Network GUID with UIO device |
Stephen Hemminger | 95096f2 | 2016-12-03 12:34:40 -0800 | [diff] [blame] | 12 | * # echo "f8615163-df3e-46c5-913f-f2d2f965ed0e" \ |
Stephen Hemminger | 4289696 | 2018-01-04 14:13:27 -0800 | [diff] [blame] | 13 | * > /sys/bus/vmbus/drivers/uio_hv_generic/new_id |
| 14 | * Then rebind |
| 15 | * # echo -n "ed963694-e847-4b2a-85af-bc9cfc11d6f3" \ |
Stephen Hemminger | 95096f2 | 2016-12-03 12:34:40 -0800 | [diff] [blame] | 16 | * > /sys/bus/vmbus/drivers/hv_netvsc/unbind |
Stephen Hemminger | 4289696 | 2018-01-04 14:13:27 -0800 | [diff] [blame] | 17 | * # echo -n "ed963694-e847-4b2a-85af-bc9cfc11d6f3" \ |
Stephen Hemminger | 95096f2 | 2016-12-03 12:34:40 -0800 | [diff] [blame] | 18 | * > /sys/bus/vmbus/drivers/uio_hv_generic/bind |
| 19 | */ |
Stephen Hemminger | 95096f2 | 2016-12-03 12:34:40 -0800 | [diff] [blame] | 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 | |
Stephen Hemminger | 108ddb8 | 2018-08-10 23:06:09 +0000 | [diff] [blame] | 35 | #define DRIVER_VERSION "0.02.1" |
Stephen Hemminger | 95096f2 | 2016-12-03 12:34:40 -0800 | [diff] [blame] | 36 | #define DRIVER_AUTHOR "Stephen Hemminger <sthemmin at microsoft.com>" |
| 37 | #define DRIVER_DESC "Generic UIO driver for VMBus devices" |
| 38 | |
Stephen Hemminger | e7d2146 | 2018-01-09 12:57:30 -0800 | [diff] [blame] | 39 | #define HV_RING_SIZE 512 /* pages */ |
Stephen Hemminger | 108ddb8 | 2018-08-10 23:06:09 +0000 | [diff] [blame] | 40 | #define SEND_BUFFER_SIZE (16 * 1024 * 1024) |
| 41 | #define RECV_BUFFER_SIZE (31 * 1024 * 1024) |
Stephen Hemminger | e7d2146 | 2018-01-09 12:57:30 -0800 | [diff] [blame] | 42 | |
Stephen Hemminger | 95096f2 | 2016-12-03 12:34:40 -0800 | [diff] [blame] | 43 | /* |
| 44 | * List of resources to be mapped to user space |
| 45 | * can be extended up to MAX_UIO_MAPS(5) items |
| 46 | */ |
| 47 | enum hv_uio_map { |
| 48 | TXRX_RING_MAP = 0, |
| 49 | INT_PAGE_MAP, |
| 50 | MON_PAGE_MAP, |
Stephen Hemminger | e7d2146 | 2018-01-09 12:57:30 -0800 | [diff] [blame] | 51 | RECV_BUF_MAP, |
| 52 | SEND_BUF_MAP |
Stephen Hemminger | 95096f2 | 2016-12-03 12:34:40 -0800 | [diff] [blame] | 53 | }; |
| 54 | |
Stephen Hemminger | 95096f2 | 2016-12-03 12:34:40 -0800 | [diff] [blame] | 55 | struct hv_uio_private_data { |
| 56 | struct uio_info info; |
| 57 | struct hv_device *device; |
Stephen Hemminger | cdfa835 | 2018-09-14 09:10:20 -0700 | [diff] [blame] | 58 | atomic_t refcnt; |
Stephen Hemminger | e7d2146 | 2018-01-09 12:57:30 -0800 | [diff] [blame] | 59 | |
| 60 | void *recv_buf; |
| 61 | u32 recv_gpadl; |
| 62 | char recv_name[32]; /* "recv_4294967295" */ |
| 63 | |
| 64 | void *send_buf; |
| 65 | u32 send_gpadl; |
| 66 | char send_name[32]; |
Stephen Hemminger | 95096f2 | 2016-12-03 12:34:40 -0800 | [diff] [blame] | 67 | }; |
| 68 | |
Stephen Hemminger | 95096f2 | 2016-12-03 12:34:40 -0800 | [diff] [blame] | 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 | */ |
| 78 | static int |
| 79 | hv_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 | */ |
| 93 | static void hv_uio_channel_cb(void *context) |
| 94 | { |
Stephen Hemminger | 135db38 | 2018-04-16 11:19:26 -0700 | [diff] [blame] | 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); |
Stephen Hemminger | 95096f2 | 2016-12-03 12:34:40 -0800 | [diff] [blame] | 98 | |
Stephen Hemminger | 135db38 | 2018-04-16 11:19:26 -0700 | [diff] [blame] | 99 | chan->inbound.ring_buffer->interrupt_mask = 1; |
Stephen Hemminger | 95096f2 | 2016-12-03 12:34:40 -0800 | [diff] [blame] | 100 | virt_mb(); |
| 101 | |
| 102 | uio_event_notify(&pdata->info); |
| 103 | } |
| 104 | |
Stephen Hemminger | ca3cda6 | 2018-01-09 12:57:32 -0800 | [diff] [blame] | 105 | /* |
| 106 | * Callback from vmbus_event when channel is rescinded. |
| 107 | */ |
| 108 | static 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 | } |
Stephen Hemminger | e7d2146 | 2018-01-09 12:57:30 -0800 | [diff] [blame] | 122 | |
Stephen Hemminger | ce3d153 | 2018-04-16 11:19:27 -0700 | [diff] [blame] | 123 | /* Sysfs API to allow mmap of the ring buffers |
| 124 | * The ring buffer is allocated as contiguous memory by vmbus_open |
Stephen Hemminger | 37b96a4 | 2018-02-05 10:40:27 -0800 | [diff] [blame] | 125 | */ |
Stephen Hemminger | 37b96a4 | 2018-02-05 10:40:27 -0800 | [diff] [blame] | 126 | static 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); |
Stephen Hemminger | 52a42c2 | 2018-09-14 09:10:16 -0700 | [diff] [blame] | 132 | void *ring_buffer = page_address(channel->ringbuffer_page); |
Stephen Hemminger | 37b96a4 | 2018-02-05 10:40:27 -0800 | [diff] [blame] | 133 | |
Stephen Hemminger | cdfa835 | 2018-09-14 09:10:20 -0700 | [diff] [blame] | 134 | if (channel->state != CHANNEL_OPENED_STATE) |
| 135 | return -ENODEV; |
Stephen Hemminger | 37b96a4 | 2018-02-05 10:40:27 -0800 | [diff] [blame] | 136 | |
Stephen Hemminger | 52a42c2 | 2018-09-14 09:10:16 -0700 | [diff] [blame] | 137 | return vm_iomap_memory(vma, virt_to_phys(ring_buffer), |
Stephen Hemminger | ce3d153 | 2018-04-16 11:19:27 -0700 | [diff] [blame] | 138 | channel->ringbuffer_pagecount << PAGE_SHIFT); |
Stephen Hemminger | 37b96a4 | 2018-02-05 10:40:27 -0800 | [diff] [blame] | 139 | } |
| 140 | |
Stephen Hemminger | 6e3d66b | 2018-04-16 11:19:24 -0700 | [diff] [blame] | 141 | static const struct bin_attribute ring_buffer_bin_attr = { |
Stephen Hemminger | 37b96a4 | 2018-02-05 10:40:27 -0800 | [diff] [blame] | 142 | .attr = { |
| 143 | .name = "ring", |
| 144 | .mode = 0600, |
Stephen Hemminger | 37b96a4 | 2018-02-05 10:40:27 -0800 | [diff] [blame] | 145 | }, |
Stephen Hemminger | 6e3d66b | 2018-04-16 11:19:24 -0700 | [diff] [blame] | 146 | .size = 2 * HV_RING_SIZE * PAGE_SIZE, |
Stephen Hemminger | 37b96a4 | 2018-02-05 10:40:27 -0800 | [diff] [blame] | 147 | .mmap = hv_uio_ring_mmap, |
| 148 | }; |
| 149 | |
Stephen Hemminger | 135db38 | 2018-04-16 11:19:26 -0700 | [diff] [blame] | 150 | /* Callback from VMBUS subsystem when new channel created. */ |
Stephen Hemminger | 37b96a4 | 2018-02-05 10:40:27 -0800 | [diff] [blame] | 151 | static void |
| 152 | hv_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; |
Stephen Hemminger | 37b96a4 | 2018-02-05 10:40:27 -0800 | [diff] [blame] | 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, |
Stephen Hemminger | 135db38 | 2018-04-16 11:19:26 -0700 | [diff] [blame] | 161 | hv_uio_channel_cb, new_sc); |
Stephen Hemminger | 37b96a4 | 2018-02-05 10:40:27 -0800 | [diff] [blame] | 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 | |
Stephen Hemminger | cdfa835 | 2018-09-14 09:10:20 -0700 | [diff] [blame] | 178 | /* free the reserved buffers for send and receive */ |
Stephen Hemminger | e7d2146 | 2018-01-09 12:57:30 -0800 | [diff] [blame] | 179 | static void |
| 180 | hv_uio_cleanup(struct hv_device *dev, struct hv_uio_private_data *pdata) |
| 181 | { |
Stephen Hemminger | cdfa835 | 2018-09-14 09:10:20 -0700 | [diff] [blame] | 182 | if (pdata->send_gpadl) { |
Stephen Hemminger | e7d2146 | 2018-01-09 12:57:30 -0800 | [diff] [blame] | 183 | vmbus_teardown_gpadl(dev->channel, pdata->send_gpadl); |
Stephen Hemminger | cdfa835 | 2018-09-14 09:10:20 -0700 | [diff] [blame] | 184 | pdata->send_gpadl = 0; |
| 185 | vfree(pdata->send_buf); |
| 186 | } |
Stephen Hemminger | e7d2146 | 2018-01-09 12:57:30 -0800 | [diff] [blame] | 187 | |
Stephen Hemminger | cdfa835 | 2018-09-14 09:10:20 -0700 | [diff] [blame] | 188 | if (pdata->recv_gpadl) { |
Stephen Hemminger | e7d2146 | 2018-01-09 12:57:30 -0800 | [diff] [blame] | 189 | vmbus_teardown_gpadl(dev->channel, pdata->recv_gpadl); |
Stephen Hemminger | cdfa835 | 2018-09-14 09:10:20 -0700 | [diff] [blame] | 190 | pdata->recv_gpadl = 0; |
| 191 | vfree(pdata->recv_buf); |
| 192 | } |
| 193 | } |
| 194 | |
| 195 | /* VMBus primary channel is opened on first use */ |
| 196 | static int |
| 197 | hv_uio_open(struct uio_info *info, struct inode *inode) |
| 198 | { |
| 199 | struct hv_uio_private_data *pdata |
| 200 | = container_of(info, struct hv_uio_private_data, info); |
| 201 | struct hv_device *dev = pdata->device; |
| 202 | int ret; |
| 203 | |
| 204 | if (atomic_inc_return(&pdata->refcnt) != 1) |
| 205 | return 0; |
| 206 | |
Stephen Hemminger | 5e3c420 | 2018-12-10 10:18:19 -0800 | [diff] [blame] | 207 | vmbus_set_chn_rescind_callback(dev->channel, hv_uio_rescind); |
| 208 | vmbus_set_sc_create_callback(dev->channel, hv_uio_new_channel); |
| 209 | |
Stephen Hemminger | cdfa835 | 2018-09-14 09:10:20 -0700 | [diff] [blame] | 210 | ret = vmbus_connect_ring(dev->channel, |
| 211 | hv_uio_channel_cb, dev->channel); |
Stephen Hemminger | cdfa835 | 2018-09-14 09:10:20 -0700 | [diff] [blame] | 212 | if (ret == 0) |
| 213 | dev->channel->inbound.ring_buffer->interrupt_mask = 1; |
| 214 | else |
| 215 | atomic_dec(&pdata->refcnt); |
| 216 | |
| 217 | return ret; |
| 218 | } |
| 219 | |
| 220 | /* VMBus primary channel is closed on last close */ |
| 221 | static int |
| 222 | hv_uio_release(struct uio_info *info, struct inode *inode) |
| 223 | { |
| 224 | struct hv_uio_private_data *pdata |
| 225 | = container_of(info, struct hv_uio_private_data, info); |
| 226 | struct hv_device *dev = pdata->device; |
| 227 | int ret = 0; |
| 228 | |
| 229 | if (atomic_dec_and_test(&pdata->refcnt)) |
| 230 | ret = vmbus_disconnect_ring(dev->channel); |
| 231 | |
| 232 | return ret; |
Stephen Hemminger | e7d2146 | 2018-01-09 12:57:30 -0800 | [diff] [blame] | 233 | } |
| 234 | |
Stephen Hemminger | 95096f2 | 2016-12-03 12:34:40 -0800 | [diff] [blame] | 235 | static int |
| 236 | hv_uio_probe(struct hv_device *dev, |
| 237 | const struct hv_vmbus_device_id *dev_id) |
| 238 | { |
Stephen Hemminger | cdfa835 | 2018-09-14 09:10:20 -0700 | [diff] [blame] | 239 | struct vmbus_channel *channel = dev->channel; |
Stephen Hemminger | 95096f2 | 2016-12-03 12:34:40 -0800 | [diff] [blame] | 240 | struct hv_uio_private_data *pdata; |
Stephen Hemminger | cdfa835 | 2018-09-14 09:10:20 -0700 | [diff] [blame] | 241 | void *ring_buffer; |
Stephen Hemminger | 95096f2 | 2016-12-03 12:34:40 -0800 | [diff] [blame] | 242 | int ret; |
| 243 | |
Stephen Hemminger | cdfa835 | 2018-09-14 09:10:20 -0700 | [diff] [blame] | 244 | /* Communicating with host has to be via shared memory not hypercall */ |
| 245 | if (!channel->offermsg.monitor_allocated) { |
| 246 | dev_err(&dev->device, "vmbus channel requires hypercall\n"); |
| 247 | return -ENOTSUPP; |
| 248 | } |
| 249 | |
Stephen Hemminger | 95096f2 | 2016-12-03 12:34:40 -0800 | [diff] [blame] | 250 | pdata = kzalloc(sizeof(*pdata), GFP_KERNEL); |
| 251 | if (!pdata) |
| 252 | return -ENOMEM; |
| 253 | |
Stephen Hemminger | cdfa835 | 2018-09-14 09:10:20 -0700 | [diff] [blame] | 254 | ret = vmbus_alloc_ring(channel, HV_RING_SIZE * PAGE_SIZE, |
| 255 | HV_RING_SIZE * PAGE_SIZE); |
Stephen Hemminger | 95096f2 | 2016-12-03 12:34:40 -0800 | [diff] [blame] | 256 | if (ret) |
| 257 | goto fail; |
| 258 | |
Stephen Hemminger | cdfa835 | 2018-09-14 09:10:20 -0700 | [diff] [blame] | 259 | set_channel_read_mode(channel, HV_CALL_ISR); |
Stephen Hemminger | 95096f2 | 2016-12-03 12:34:40 -0800 | [diff] [blame] | 260 | |
| 261 | /* Fill general uio info */ |
| 262 | pdata->info.name = "uio_hv_generic"; |
| 263 | pdata->info.version = DRIVER_VERSION; |
| 264 | pdata->info.irqcontrol = hv_uio_irqcontrol; |
Stephen Hemminger | cdfa835 | 2018-09-14 09:10:20 -0700 | [diff] [blame] | 265 | pdata->info.open = hv_uio_open; |
| 266 | pdata->info.release = hv_uio_release; |
Stephen Hemminger | 95096f2 | 2016-12-03 12:34:40 -0800 | [diff] [blame] | 267 | pdata->info.irq = UIO_IRQ_CUSTOM; |
Stephen Hemminger | cdfa835 | 2018-09-14 09:10:20 -0700 | [diff] [blame] | 268 | atomic_set(&pdata->refcnt, 0); |
Stephen Hemminger | 95096f2 | 2016-12-03 12:34:40 -0800 | [diff] [blame] | 269 | |
| 270 | /* mem resources */ |
| 271 | pdata->info.mem[TXRX_RING_MAP].name = "txrx_rings"; |
Stephen Hemminger | cdfa835 | 2018-09-14 09:10:20 -0700 | [diff] [blame] | 272 | ring_buffer = page_address(channel->ringbuffer_page); |
Stephen Hemminger | 95096f2 | 2016-12-03 12:34:40 -0800 | [diff] [blame] | 273 | pdata->info.mem[TXRX_RING_MAP].addr |
Stephen Hemminger | cdfa835 | 2018-09-14 09:10:20 -0700 | [diff] [blame] | 274 | = (uintptr_t)virt_to_phys(ring_buffer); |
Stephen Hemminger | 95096f2 | 2016-12-03 12:34:40 -0800 | [diff] [blame] | 275 | pdata->info.mem[TXRX_RING_MAP].size |
Stephen Hemminger | cdfa835 | 2018-09-14 09:10:20 -0700 | [diff] [blame] | 276 | = channel->ringbuffer_pagecount << PAGE_SHIFT; |
Stephen Hemminger | 9da197f | 2018-09-14 09:10:19 -0700 | [diff] [blame] | 277 | pdata->info.mem[TXRX_RING_MAP].memtype = UIO_MEM_IOVA; |
Stephen Hemminger | 95096f2 | 2016-12-03 12:34:40 -0800 | [diff] [blame] | 278 | |
| 279 | pdata->info.mem[INT_PAGE_MAP].name = "int_page"; |
Stephen Hemminger | 9c40546 | 2018-01-04 14:13:28 -0800 | [diff] [blame] | 280 | pdata->info.mem[INT_PAGE_MAP].addr |
Arnd Bergmann | 72d1465 | 2018-01-10 17:42:38 +0100 | [diff] [blame] | 281 | = (uintptr_t)vmbus_connection.int_page; |
Stephen Hemminger | 95096f2 | 2016-12-03 12:34:40 -0800 | [diff] [blame] | 282 | pdata->info.mem[INT_PAGE_MAP].size = PAGE_SIZE; |
| 283 | pdata->info.mem[INT_PAGE_MAP].memtype = UIO_MEM_LOGICAL; |
| 284 | |
Stephen Hemminger | 9c40546 | 2018-01-04 14:13:28 -0800 | [diff] [blame] | 285 | pdata->info.mem[MON_PAGE_MAP].name = "monitor_page"; |
| 286 | pdata->info.mem[MON_PAGE_MAP].addr |
Arnd Bergmann | 72d1465 | 2018-01-10 17:42:38 +0100 | [diff] [blame] | 287 | = (uintptr_t)vmbus_connection.monitor_pages[1]; |
Stephen Hemminger | 95096f2 | 2016-12-03 12:34:40 -0800 | [diff] [blame] | 288 | pdata->info.mem[MON_PAGE_MAP].size = PAGE_SIZE; |
| 289 | pdata->info.mem[MON_PAGE_MAP].memtype = UIO_MEM_LOGICAL; |
| 290 | |
Stephen Hemminger | e7d2146 | 2018-01-09 12:57:30 -0800 | [diff] [blame] | 291 | pdata->recv_buf = vzalloc(RECV_BUFFER_SIZE); |
| 292 | if (pdata->recv_buf == NULL) { |
| 293 | ret = -ENOMEM; |
| 294 | goto fail_close; |
| 295 | } |
| 296 | |
Stephen Hemminger | cdfa835 | 2018-09-14 09:10:20 -0700 | [diff] [blame] | 297 | ret = vmbus_establish_gpadl(channel, pdata->recv_buf, |
Stephen Hemminger | e7d2146 | 2018-01-09 12:57:30 -0800 | [diff] [blame] | 298 | RECV_BUFFER_SIZE, &pdata->recv_gpadl); |
| 299 | if (ret) |
| 300 | goto fail_close; |
| 301 | |
| 302 | /* put Global Physical Address Label in name */ |
| 303 | snprintf(pdata->recv_name, sizeof(pdata->recv_name), |
| 304 | "recv:%u", pdata->recv_gpadl); |
| 305 | pdata->info.mem[RECV_BUF_MAP].name = pdata->recv_name; |
| 306 | pdata->info.mem[RECV_BUF_MAP].addr |
Arnd Bergmann | d6088e9 | 2018-01-12 16:51:14 +0100 | [diff] [blame] | 307 | = (uintptr_t)pdata->recv_buf; |
Stephen Hemminger | e7d2146 | 2018-01-09 12:57:30 -0800 | [diff] [blame] | 308 | pdata->info.mem[RECV_BUF_MAP].size = RECV_BUFFER_SIZE; |
| 309 | pdata->info.mem[RECV_BUF_MAP].memtype = UIO_MEM_VIRTUAL; |
| 310 | |
Stephen Hemminger | e7d2146 | 2018-01-09 12:57:30 -0800 | [diff] [blame] | 311 | pdata->send_buf = vzalloc(SEND_BUFFER_SIZE); |
| 312 | if (pdata->send_buf == NULL) { |
| 313 | ret = -ENOMEM; |
| 314 | goto fail_close; |
| 315 | } |
| 316 | |
Stephen Hemminger | cdfa835 | 2018-09-14 09:10:20 -0700 | [diff] [blame] | 317 | ret = vmbus_establish_gpadl(channel, pdata->send_buf, |
Stephen Hemminger | e7d2146 | 2018-01-09 12:57:30 -0800 | [diff] [blame] | 318 | SEND_BUFFER_SIZE, &pdata->send_gpadl); |
| 319 | if (ret) |
| 320 | goto fail_close; |
| 321 | |
| 322 | snprintf(pdata->send_name, sizeof(pdata->send_name), |
| 323 | "send:%u", pdata->send_gpadl); |
| 324 | pdata->info.mem[SEND_BUF_MAP].name = pdata->send_name; |
| 325 | pdata->info.mem[SEND_BUF_MAP].addr |
Arnd Bergmann | d6088e9 | 2018-01-12 16:51:14 +0100 | [diff] [blame] | 326 | = (uintptr_t)pdata->send_buf; |
Stephen Hemminger | e7d2146 | 2018-01-09 12:57:30 -0800 | [diff] [blame] | 327 | pdata->info.mem[SEND_BUF_MAP].size = SEND_BUFFER_SIZE; |
| 328 | pdata->info.mem[SEND_BUF_MAP].memtype = UIO_MEM_VIRTUAL; |
| 329 | |
Stephen Hemminger | 95096f2 | 2016-12-03 12:34:40 -0800 | [diff] [blame] | 330 | pdata->info.priv = pdata; |
| 331 | pdata->device = dev; |
| 332 | |
| 333 | ret = uio_register_device(&dev->device, &pdata->info); |
| 334 | if (ret) { |
| 335 | dev_err(&dev->device, "hv_uio register failed\n"); |
| 336 | goto fail_close; |
| 337 | } |
| 338 | |
Stephen Hemminger | cdfa835 | 2018-09-14 09:10:20 -0700 | [diff] [blame] | 339 | ret = sysfs_create_bin_file(&channel->kobj, &ring_buffer_bin_attr); |
Stephen Hemminger | 9ab877a | 2018-04-16 11:19:25 -0700 | [diff] [blame] | 340 | if (ret) |
| 341 | dev_notice(&dev->device, |
| 342 | "sysfs create ring bin file failed; %d\n", ret); |
| 343 | |
Stephen Hemminger | 95096f2 | 2016-12-03 12:34:40 -0800 | [diff] [blame] | 344 | hv_set_drvdata(dev, pdata); |
| 345 | |
| 346 | return 0; |
| 347 | |
| 348 | fail_close: |
Stephen Hemminger | e7d2146 | 2018-01-09 12:57:30 -0800 | [diff] [blame] | 349 | hv_uio_cleanup(dev, pdata); |
Stephen Hemminger | 95096f2 | 2016-12-03 12:34:40 -0800 | [diff] [blame] | 350 | fail: |
| 351 | kfree(pdata); |
| 352 | |
| 353 | return ret; |
| 354 | } |
| 355 | |
| 356 | static int |
| 357 | hv_uio_remove(struct hv_device *dev) |
| 358 | { |
| 359 | struct hv_uio_private_data *pdata = hv_get_drvdata(dev); |
| 360 | |
| 361 | if (!pdata) |
| 362 | return 0; |
| 363 | |
Chuhong Yuan | 7066c2f | 2020-05-07 23:13:43 +0800 | [diff] [blame] | 364 | sysfs_remove_bin_file(&dev->channel->kobj, &ring_buffer_bin_attr); |
Stephen Hemminger | 95096f2 | 2016-12-03 12:34:40 -0800 | [diff] [blame] | 365 | uio_unregister_device(&pdata->info); |
Stephen Hemminger | e7d2146 | 2018-01-09 12:57:30 -0800 | [diff] [blame] | 366 | hv_uio_cleanup(dev, pdata); |
Stephen Hemminger | 95096f2 | 2016-12-03 12:34:40 -0800 | [diff] [blame] | 367 | hv_set_drvdata(dev, NULL); |
Stephen Hemminger | cdfa835 | 2018-09-14 09:10:20 -0700 | [diff] [blame] | 368 | |
| 369 | vmbus_free_ring(dev->channel); |
Stephen Hemminger | 95096f2 | 2016-12-03 12:34:40 -0800 | [diff] [blame] | 370 | kfree(pdata); |
| 371 | return 0; |
| 372 | } |
| 373 | |
| 374 | static struct hv_driver hv_uio_drv = { |
| 375 | .name = "uio_hv_generic", |
| 376 | .id_table = NULL, /* only dynamic id's */ |
| 377 | .probe = hv_uio_probe, |
| 378 | .remove = hv_uio_remove, |
| 379 | }; |
| 380 | |
| 381 | static int __init |
| 382 | hyperv_module_init(void) |
| 383 | { |
| 384 | return vmbus_driver_register(&hv_uio_drv); |
| 385 | } |
| 386 | |
| 387 | static void __exit |
| 388 | hyperv_module_exit(void) |
| 389 | { |
| 390 | vmbus_driver_unregister(&hv_uio_drv); |
| 391 | } |
| 392 | |
| 393 | module_init(hyperv_module_init); |
| 394 | module_exit(hyperv_module_exit); |
| 395 | |
| 396 | MODULE_VERSION(DRIVER_VERSION); |
| 397 | MODULE_LICENSE("GPL v2"); |
| 398 | MODULE_AUTHOR(DRIVER_AUTHOR); |
| 399 | MODULE_DESCRIPTION(DRIVER_DESC); |