uio_hv_generic.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399
  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. #include <linux/device.h>
  22. #include <linux/kernel.h>
  23. #include <linux/module.h>
  24. #include <linux/uio_driver.h>
  25. #include <linux/netdevice.h>
  26. #include <linux/if_ether.h>
  27. #include <linux/skbuff.h>
  28. #include <linux/hyperv.h>
  29. #include <linux/vmalloc.h>
  30. #include <linux/slab.h>
  31. #include "../hv/hyperv_vmbus.h"
  32. #define DRIVER_VERSION "0.02.1"
  33. #define DRIVER_AUTHOR "Stephen Hemminger <sthemmin at microsoft.com>"
  34. #define DRIVER_DESC "Generic UIO driver for VMBus devices"
  35. #define HV_RING_SIZE 512 /* pages */
  36. #define SEND_BUFFER_SIZE (16 * 1024 * 1024)
  37. #define RECV_BUFFER_SIZE (31 * 1024 * 1024)
  38. /*
  39. * List of resources to be mapped to user space
  40. * can be extended up to MAX_UIO_MAPS(5) items
  41. */
  42. enum hv_uio_map {
  43. TXRX_RING_MAP = 0,
  44. INT_PAGE_MAP,
  45. MON_PAGE_MAP,
  46. RECV_BUF_MAP,
  47. SEND_BUF_MAP
  48. };
  49. struct hv_uio_private_data {
  50. struct uio_info info;
  51. struct hv_device *device;
  52. atomic_t refcnt;
  53. void *recv_buf;
  54. struct vmbus_gpadl recv_gpadl;
  55. char recv_name[32]; /* "recv_4294967295" */
  56. void *send_buf;
  57. struct vmbus_gpadl send_gpadl;
  58. char send_name[32];
  59. };
  60. /*
  61. * This is the irqcontrol callback to be registered to uio_info.
  62. * It can be used to disable/enable interrupt from user space processes.
  63. *
  64. * @param info
  65. * pointer to uio_info.
  66. * @param irq_state
  67. * state value. 1 to enable interrupt, 0 to disable interrupt.
  68. */
  69. static int
  70. hv_uio_irqcontrol(struct uio_info *info, s32 irq_state)
  71. {
  72. struct hv_uio_private_data *pdata = info->priv;
  73. struct hv_device *dev = pdata->device;
  74. dev->channel->inbound.ring_buffer->interrupt_mask = !irq_state;
  75. virt_mb();
  76. return 0;
  77. }
  78. /*
  79. * Callback from vmbus_event when something is in inbound ring.
  80. */
  81. static void hv_uio_channel_cb(void *context)
  82. {
  83. struct vmbus_channel *chan = context;
  84. struct hv_device *hv_dev = chan->device_obj;
  85. struct hv_uio_private_data *pdata = hv_get_drvdata(hv_dev);
  86. chan->inbound.ring_buffer->interrupt_mask = 1;
  87. virt_mb();
  88. uio_event_notify(&pdata->info);
  89. }
  90. /*
  91. * Callback from vmbus_event when channel is rescinded.
  92. */
  93. static void hv_uio_rescind(struct vmbus_channel *channel)
  94. {
  95. struct hv_device *hv_dev = channel->primary_channel->device_obj;
  96. struct hv_uio_private_data *pdata = hv_get_drvdata(hv_dev);
  97. /*
  98. * Turn off the interrupt file handle
  99. * Next read for event will return -EIO
  100. */
  101. pdata->info.irq = 0;
  102. /* Wake up reader */
  103. uio_event_notify(&pdata->info);
  104. }
  105. /* Sysfs API to allow mmap of the ring buffers
  106. * The ring buffer is allocated as contiguous memory by vmbus_open
  107. */
  108. static int hv_uio_ring_mmap(struct file *filp, struct kobject *kobj,
  109. struct bin_attribute *attr,
  110. struct vm_area_struct *vma)
  111. {
  112. struct vmbus_channel *channel
  113. = container_of(kobj, struct vmbus_channel, kobj);
  114. void *ring_buffer = page_address(channel->ringbuffer_page);
  115. if (channel->state != CHANNEL_OPENED_STATE)
  116. return -ENODEV;
  117. return vm_iomap_memory(vma, virt_to_phys(ring_buffer),
  118. channel->ringbuffer_pagecount << PAGE_SHIFT);
  119. }
  120. static const struct bin_attribute ring_buffer_bin_attr = {
  121. .attr = {
  122. .name = "ring",
  123. .mode = 0600,
  124. },
  125. .size = 2 * HV_RING_SIZE * PAGE_SIZE,
  126. .mmap = hv_uio_ring_mmap,
  127. };
  128. /* Callback from VMBUS subsystem when new channel created. */
  129. static void
  130. hv_uio_new_channel(struct vmbus_channel *new_sc)
  131. {
  132. struct hv_device *hv_dev = new_sc->primary_channel->device_obj;
  133. struct device *device = &hv_dev->device;
  134. const size_t ring_bytes = HV_RING_SIZE * PAGE_SIZE;
  135. int ret;
  136. /* Create host communication ring */
  137. ret = vmbus_open(new_sc, ring_bytes, ring_bytes, NULL, 0,
  138. hv_uio_channel_cb, new_sc);
  139. if (ret) {
  140. dev_err(device, "vmbus_open subchannel failed: %d\n", ret);
  141. return;
  142. }
  143. /* Disable interrupts on sub channel */
  144. new_sc->inbound.ring_buffer->interrupt_mask = 1;
  145. set_channel_read_mode(new_sc, HV_CALL_ISR);
  146. ret = sysfs_create_bin_file(&new_sc->kobj, &ring_buffer_bin_attr);
  147. if (ret) {
  148. dev_err(device, "sysfs create ring bin file failed; %d\n", ret);
  149. vmbus_close(new_sc);
  150. }
  151. }
  152. /* free the reserved buffers for send and receive */
  153. static void
  154. hv_uio_cleanup(struct hv_device *dev, struct hv_uio_private_data *pdata)
  155. {
  156. if (pdata->send_gpadl.gpadl_handle) {
  157. vmbus_teardown_gpadl(dev->channel, &pdata->send_gpadl);
  158. vfree(pdata->send_buf);
  159. }
  160. if (pdata->recv_gpadl.gpadl_handle) {
  161. vmbus_teardown_gpadl(dev->channel, &pdata->recv_gpadl);
  162. vfree(pdata->recv_buf);
  163. }
  164. }
  165. /* VMBus primary channel is opened on first use */
  166. static int
  167. hv_uio_open(struct uio_info *info, struct inode *inode)
  168. {
  169. struct hv_uio_private_data *pdata
  170. = container_of(info, struct hv_uio_private_data, info);
  171. struct hv_device *dev = pdata->device;
  172. int ret;
  173. if (atomic_inc_return(&pdata->refcnt) != 1)
  174. return 0;
  175. vmbus_set_chn_rescind_callback(dev->channel, hv_uio_rescind);
  176. vmbus_set_sc_create_callback(dev->channel, hv_uio_new_channel);
  177. ret = vmbus_connect_ring(dev->channel,
  178. hv_uio_channel_cb, dev->channel);
  179. if (ret == 0)
  180. dev->channel->inbound.ring_buffer->interrupt_mask = 1;
  181. else
  182. atomic_dec(&pdata->refcnt);
  183. return ret;
  184. }
  185. /* VMBus primary channel is closed on last close */
  186. static int
  187. hv_uio_release(struct uio_info *info, struct inode *inode)
  188. {
  189. struct hv_uio_private_data *pdata
  190. = container_of(info, struct hv_uio_private_data, info);
  191. struct hv_device *dev = pdata->device;
  192. int ret = 0;
  193. if (atomic_dec_and_test(&pdata->refcnt))
  194. ret = vmbus_disconnect_ring(dev->channel);
  195. return ret;
  196. }
  197. static int
  198. hv_uio_probe(struct hv_device *dev,
  199. const struct hv_vmbus_device_id *dev_id)
  200. {
  201. struct vmbus_channel *channel = dev->channel;
  202. struct hv_uio_private_data *pdata;
  203. void *ring_buffer;
  204. int ret;
  205. /* Communicating with host has to be via shared memory not hypercall */
  206. if (!channel->offermsg.monitor_allocated) {
  207. dev_err(&dev->device, "vmbus channel requires hypercall\n");
  208. return -ENOTSUPP;
  209. }
  210. pdata = devm_kzalloc(&dev->device, sizeof(*pdata), GFP_KERNEL);
  211. if (!pdata)
  212. return -ENOMEM;
  213. ret = vmbus_alloc_ring(channel, HV_RING_SIZE * PAGE_SIZE,
  214. HV_RING_SIZE * PAGE_SIZE);
  215. if (ret)
  216. return ret;
  217. set_channel_read_mode(channel, HV_CALL_ISR);
  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;
  222. pdata->info.open = hv_uio_open;
  223. pdata->info.release = hv_uio_release;
  224. pdata->info.irq = UIO_IRQ_CUSTOM;
  225. atomic_set(&pdata->refcnt, 0);
  226. /* mem resources */
  227. pdata->info.mem[TXRX_RING_MAP].name = "txrx_rings";
  228. ring_buffer = page_address(channel->ringbuffer_page);
  229. pdata->info.mem[TXRX_RING_MAP].addr
  230. = (uintptr_t)virt_to_phys(ring_buffer);
  231. pdata->info.mem[TXRX_RING_MAP].size
  232. = channel->ringbuffer_pagecount << PAGE_SHIFT;
  233. pdata->info.mem[TXRX_RING_MAP].memtype = UIO_MEM_IOVA;
  234. pdata->info.mem[INT_PAGE_MAP].name = "int_page";
  235. pdata->info.mem[INT_PAGE_MAP].addr
  236. = (uintptr_t)vmbus_connection.int_page;
  237. pdata->info.mem[INT_PAGE_MAP].size = PAGE_SIZE;
  238. pdata->info.mem[INT_PAGE_MAP].memtype = UIO_MEM_LOGICAL;
  239. pdata->info.mem[MON_PAGE_MAP].name = "monitor_page";
  240. pdata->info.mem[MON_PAGE_MAP].addr
  241. = (uintptr_t)vmbus_connection.monitor_pages[1];
  242. pdata->info.mem[MON_PAGE_MAP].size = PAGE_SIZE;
  243. pdata->info.mem[MON_PAGE_MAP].memtype = UIO_MEM_LOGICAL;
  244. pdata->recv_buf = vzalloc(RECV_BUFFER_SIZE);
  245. if (pdata->recv_buf == NULL) {
  246. ret = -ENOMEM;
  247. goto fail_free_ring;
  248. }
  249. ret = vmbus_establish_gpadl(channel, pdata->recv_buf,
  250. RECV_BUFFER_SIZE, &pdata->recv_gpadl);
  251. if (ret) {
  252. vfree(pdata->recv_buf);
  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.gpadl_handle);
  258. pdata->info.mem[RECV_BUF_MAP].name = pdata->recv_name;
  259. pdata->info.mem[RECV_BUF_MAP].addr
  260. = (uintptr_t)pdata->recv_buf;
  261. pdata->info.mem[RECV_BUF_MAP].size = RECV_BUFFER_SIZE;
  262. pdata->info.mem[RECV_BUF_MAP].memtype = UIO_MEM_VIRTUAL;
  263. pdata->send_buf = vzalloc(SEND_BUFFER_SIZE);
  264. if (pdata->send_buf == NULL) {
  265. ret = -ENOMEM;
  266. goto fail_close;
  267. }
  268. ret = vmbus_establish_gpadl(channel, pdata->send_buf,
  269. SEND_BUFFER_SIZE, &pdata->send_gpadl);
  270. if (ret) {
  271. vfree(pdata->send_buf);
  272. goto fail_close;
  273. }
  274. snprintf(pdata->send_name, sizeof(pdata->send_name),
  275. "send:%u", pdata->send_gpadl.gpadl_handle);
  276. pdata->info.mem[SEND_BUF_MAP].name = pdata->send_name;
  277. pdata->info.mem[SEND_BUF_MAP].addr
  278. = (uintptr_t)pdata->send_buf;
  279. pdata->info.mem[SEND_BUF_MAP].size = SEND_BUFFER_SIZE;
  280. pdata->info.mem[SEND_BUF_MAP].memtype = UIO_MEM_VIRTUAL;
  281. pdata->info.priv = pdata;
  282. pdata->device = dev;
  283. ret = uio_register_device(&dev->device, &pdata->info);
  284. if (ret) {
  285. dev_err(&dev->device, "hv_uio register failed\n");
  286. goto fail_close;
  287. }
  288. ret = sysfs_create_bin_file(&channel->kobj, &ring_buffer_bin_attr);
  289. if (ret)
  290. dev_notice(&dev->device,
  291. "sysfs create ring bin file failed; %d\n", ret);
  292. hv_set_drvdata(dev, pdata);
  293. return 0;
  294. fail_close:
  295. hv_uio_cleanup(dev, pdata);
  296. fail_free_ring:
  297. vmbus_free_ring(dev->channel);
  298. return ret;
  299. }
  300. static int
  301. hv_uio_remove(struct hv_device *dev)
  302. {
  303. struct hv_uio_private_data *pdata = hv_get_drvdata(dev);
  304. if (!pdata)
  305. return 0;
  306. sysfs_remove_bin_file(&dev->channel->kobj, &ring_buffer_bin_attr);
  307. uio_unregister_device(&pdata->info);
  308. hv_uio_cleanup(dev, pdata);
  309. vmbus_free_ring(dev->channel);
  310. return 0;
  311. }
  312. static struct hv_driver hv_uio_drv = {
  313. .name = "uio_hv_generic",
  314. .id_table = NULL, /* only dynamic id's */
  315. .probe = hv_uio_probe,
  316. .remove = hv_uio_remove,
  317. };
  318. static int __init
  319. hyperv_module_init(void)
  320. {
  321. return vmbus_driver_register(&hv_uio_drv);
  322. }
  323. static void __exit
  324. hyperv_module_exit(void)
  325. {
  326. vmbus_driver_unregister(&hv_uio_drv);
  327. }
  328. module_init(hyperv_module_init);
  329. module_exit(hyperv_module_exit);
  330. MODULE_VERSION(DRIVER_VERSION);
  331. MODULE_LICENSE("GPL v2");
  332. MODULE_AUTHOR(DRIVER_AUTHOR);
  333. MODULE_DESCRIPTION(DRIVER_DESC);