uio_hv_generic.c 9.2 KB

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