uio_hv_generic.c 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356
  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 DEBUG 1
  21. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  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. #include "../hv/hyperv_vmbus.h"
  33. #define DRIVER_VERSION "0.02.0"
  34. #define DRIVER_AUTHOR "Stephen Hemminger <sthemmin at microsoft.com>"
  35. #define DRIVER_DESC "Generic UIO driver for VMBus devices"
  36. #define HV_RING_SIZE 512 /* pages */
  37. #define SEND_BUFFER_SIZE (15 * 1024 * 1024)
  38. #define RECV_BUFFER_SIZE (15 * 1024 * 1024)
  39. /*
  40. * List of resources to be mapped to user space
  41. * can be extended up to MAX_UIO_MAPS(5) items
  42. */
  43. enum hv_uio_map {
  44. TXRX_RING_MAP = 0,
  45. INT_PAGE_MAP,
  46. MON_PAGE_MAP,
  47. RECV_BUF_MAP,
  48. SEND_BUF_MAP
  49. };
  50. struct hv_uio_private_data {
  51. struct uio_info info;
  52. struct hv_device *device;
  53. void *recv_buf;
  54. u32 recv_gpadl;
  55. char recv_name[32]; /* "recv_4294967295" */
  56. void *send_buf;
  57. u32 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. struct hv_device *dev = channel->primary_channel->device_obj;
  115. u16 q_idx = channel->offermsg.offer.sub_channel_index;
  116. void *ring_buffer = page_address(channel->ringbuffer_page);
  117. dev_dbg(&dev->device, "mmap channel %u pages %#lx at %#lx\n",
  118. q_idx, vma_pages(vma), vma->vm_pgoff);
  119. return vm_iomap_memory(vma, virt_to_phys(ring_buffer),
  120. channel->ringbuffer_pagecount << PAGE_SHIFT);
  121. }
  122. static const struct bin_attribute ring_buffer_bin_attr = {
  123. .attr = {
  124. .name = "ring",
  125. .mode = 0600,
  126. },
  127. .size = 2 * HV_RING_SIZE * PAGE_SIZE,
  128. .mmap = hv_uio_ring_mmap,
  129. };
  130. /* Callback from VMBUS subsystem when new channel created. */
  131. static void
  132. hv_uio_new_channel(struct vmbus_channel *new_sc)
  133. {
  134. struct hv_device *hv_dev = new_sc->primary_channel->device_obj;
  135. struct device *device = &hv_dev->device;
  136. const size_t ring_bytes = HV_RING_SIZE * PAGE_SIZE;
  137. int ret;
  138. /* Create host communication ring */
  139. ret = vmbus_open(new_sc, ring_bytes, ring_bytes, NULL, 0,
  140. hv_uio_channel_cb, new_sc);
  141. if (ret) {
  142. dev_err(device, "vmbus_open subchannel failed: %d\n", ret);
  143. return;
  144. }
  145. /* Disable interrupts on sub channel */
  146. new_sc->inbound.ring_buffer->interrupt_mask = 1;
  147. set_channel_read_mode(new_sc, HV_CALL_ISR);
  148. ret = sysfs_create_bin_file(&new_sc->kobj, &ring_buffer_bin_attr);
  149. if (ret) {
  150. dev_err(device, "sysfs create ring bin file failed; %d\n", ret);
  151. vmbus_close(new_sc);
  152. }
  153. }
  154. static void
  155. hv_uio_cleanup(struct hv_device *dev, struct hv_uio_private_data *pdata)
  156. {
  157. if (pdata->send_gpadl)
  158. vmbus_teardown_gpadl(dev->channel, pdata->send_gpadl);
  159. vfree(pdata->send_buf);
  160. if (pdata->recv_gpadl)
  161. vmbus_teardown_gpadl(dev->channel, pdata->recv_gpadl);
  162. vfree(pdata->recv_buf);
  163. }
  164. static int
  165. hv_uio_probe(struct hv_device *dev,
  166. const struct hv_vmbus_device_id *dev_id)
  167. {
  168. struct hv_uio_private_data *pdata;
  169. int ret;
  170. pdata = kzalloc(sizeof(*pdata), GFP_KERNEL);
  171. if (!pdata)
  172. return -ENOMEM;
  173. ret = vmbus_open(dev->channel, HV_RING_SIZE * PAGE_SIZE,
  174. HV_RING_SIZE * PAGE_SIZE, NULL, 0,
  175. hv_uio_channel_cb, dev->channel);
  176. if (ret)
  177. goto fail;
  178. /* Communicating with host has to be via shared memory not hypercall */
  179. if (!dev->channel->offermsg.monitor_allocated) {
  180. dev_err(&dev->device, "vmbus channel requires hypercall\n");
  181. ret = -ENOTSUPP;
  182. goto fail_close;
  183. }
  184. dev->channel->inbound.ring_buffer->interrupt_mask = 1;
  185. set_channel_read_mode(dev->channel, HV_CALL_ISR);
  186. /* Fill general uio info */
  187. pdata->info.name = "uio_hv_generic";
  188. pdata->info.version = DRIVER_VERSION;
  189. pdata->info.irqcontrol = hv_uio_irqcontrol;
  190. pdata->info.irq = UIO_IRQ_CUSTOM;
  191. /* mem resources */
  192. pdata->info.mem[TXRX_RING_MAP].name = "txrx_rings";
  193. pdata->info.mem[TXRX_RING_MAP].addr
  194. = (uintptr_t)page_address(dev->channel->ringbuffer_page);
  195. pdata->info.mem[TXRX_RING_MAP].size
  196. = dev->channel->ringbuffer_pagecount << PAGE_SHIFT;
  197. pdata->info.mem[TXRX_RING_MAP].memtype = UIO_MEM_LOGICAL;
  198. pdata->info.mem[INT_PAGE_MAP].name = "int_page";
  199. pdata->info.mem[INT_PAGE_MAP].addr
  200. = (uintptr_t)vmbus_connection.int_page;
  201. pdata->info.mem[INT_PAGE_MAP].size = PAGE_SIZE;
  202. pdata->info.mem[INT_PAGE_MAP].memtype = UIO_MEM_LOGICAL;
  203. pdata->info.mem[MON_PAGE_MAP].name = "monitor_page";
  204. pdata->info.mem[MON_PAGE_MAP].addr
  205. = (uintptr_t)vmbus_connection.monitor_pages[1];
  206. pdata->info.mem[MON_PAGE_MAP].size = PAGE_SIZE;
  207. pdata->info.mem[MON_PAGE_MAP].memtype = UIO_MEM_LOGICAL;
  208. pdata->recv_buf = vzalloc(RECV_BUFFER_SIZE);
  209. if (pdata->recv_buf == NULL) {
  210. ret = -ENOMEM;
  211. goto fail_close;
  212. }
  213. ret = vmbus_establish_gpadl(dev->channel, pdata->recv_buf,
  214. RECV_BUFFER_SIZE, &pdata->recv_gpadl);
  215. if (ret)
  216. goto fail_close;
  217. /* put Global Physical Address Label in name */
  218. snprintf(pdata->recv_name, sizeof(pdata->recv_name),
  219. "recv:%u", pdata->recv_gpadl);
  220. pdata->info.mem[RECV_BUF_MAP].name = pdata->recv_name;
  221. pdata->info.mem[RECV_BUF_MAP].addr
  222. = (uintptr_t)pdata->recv_buf;
  223. pdata->info.mem[RECV_BUF_MAP].size = RECV_BUFFER_SIZE;
  224. pdata->info.mem[RECV_BUF_MAP].memtype = UIO_MEM_VIRTUAL;
  225. pdata->send_buf = vzalloc(SEND_BUFFER_SIZE);
  226. if (pdata->send_buf == NULL) {
  227. ret = -ENOMEM;
  228. goto fail_close;
  229. }
  230. ret = vmbus_establish_gpadl(dev->channel, pdata->send_buf,
  231. SEND_BUFFER_SIZE, &pdata->send_gpadl);
  232. if (ret)
  233. goto fail_close;
  234. snprintf(pdata->send_name, sizeof(pdata->send_name),
  235. "send:%u", pdata->send_gpadl);
  236. pdata->info.mem[SEND_BUF_MAP].name = pdata->send_name;
  237. pdata->info.mem[SEND_BUF_MAP].addr
  238. = (uintptr_t)pdata->send_buf;
  239. pdata->info.mem[SEND_BUF_MAP].size = SEND_BUFFER_SIZE;
  240. pdata->info.mem[SEND_BUF_MAP].memtype = UIO_MEM_VIRTUAL;
  241. pdata->info.priv = pdata;
  242. pdata->device = dev;
  243. ret = uio_register_device(&dev->device, &pdata->info);
  244. if (ret) {
  245. dev_err(&dev->device, "hv_uio register failed\n");
  246. goto fail_close;
  247. }
  248. vmbus_set_chn_rescind_callback(dev->channel, hv_uio_rescind);
  249. vmbus_set_sc_create_callback(dev->channel, hv_uio_new_channel);
  250. ret = sysfs_create_bin_file(&dev->channel->kobj, &ring_buffer_bin_attr);
  251. if (ret)
  252. dev_notice(&dev->device,
  253. "sysfs create ring bin file failed; %d\n", ret);
  254. hv_set_drvdata(dev, pdata);
  255. return 0;
  256. fail_close:
  257. hv_uio_cleanup(dev, pdata);
  258. vmbus_close(dev->channel);
  259. fail:
  260. kfree(pdata);
  261. return ret;
  262. }
  263. static int
  264. hv_uio_remove(struct hv_device *dev)
  265. {
  266. struct hv_uio_private_data *pdata = hv_get_drvdata(dev);
  267. if (!pdata)
  268. return 0;
  269. uio_unregister_device(&pdata->info);
  270. hv_uio_cleanup(dev, pdata);
  271. hv_set_drvdata(dev, NULL);
  272. vmbus_close(dev->channel);
  273. kfree(pdata);
  274. return 0;
  275. }
  276. static struct hv_driver hv_uio_drv = {
  277. .name = "uio_hv_generic",
  278. .id_table = NULL, /* only dynamic id's */
  279. .probe = hv_uio_probe,
  280. .remove = hv_uio_remove,
  281. };
  282. static int __init
  283. hyperv_module_init(void)
  284. {
  285. return vmbus_driver_register(&hv_uio_drv);
  286. }
  287. static void __exit
  288. hyperv_module_exit(void)
  289. {
  290. vmbus_driver_unregister(&hv_uio_drv);
  291. }
  292. module_init(hyperv_module_init);
  293. module_exit(hyperv_module_exit);
  294. MODULE_VERSION(DRIVER_VERSION);
  295. MODULE_LICENSE("GPL v2");
  296. MODULE_AUTHOR(DRIVER_AUTHOR);
  297. MODULE_DESCRIPTION(DRIVER_DESC);