uio_hv_generic.c 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357
  1. /*
  2. * uio_hv_generic - generic UIO driver for VMBus
  3. *
  4. * Copyright (c) 2013-2016 Brocade Communications Systems, Inc.
  5. * Copyright (c) 2016, Microsoft Corporation.
  6. *
  7. *
  8. * This work is licensed under the terms of the GNU GPL, version 2.
  9. *
  10. * Since the driver does not declare any device ids, you must allocate
  11. * id and bind the device to the driver yourself. For example:
  12. *
  13. * Associate Network GUID with UIO device
  14. * # echo "f8615163-df3e-46c5-913f-f2d2f965ed0e" \
  15. * > /sys/bus/vmbus/drivers/uio_hv_generic/new_id
  16. * Then rebind
  17. * # echo -n "ed963694-e847-4b2a-85af-bc9cfc11d6f3" \
  18. * > /sys/bus/vmbus/drivers/hv_netvsc/unbind
  19. * # echo -n "ed963694-e847-4b2a-85af-bc9cfc11d6f3" \
  20. * > /sys/bus/vmbus/drivers/uio_hv_generic/bind
  21. */
  22. #define DEBUG 1
  23. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  24. #include <linux/device.h>
  25. #include <linux/kernel.h>
  26. #include <linux/module.h>
  27. #include <linux/uio_driver.h>
  28. #include <linux/netdevice.h>
  29. #include <linux/if_ether.h>
  30. #include <linux/skbuff.h>
  31. #include <linux/hyperv.h>
  32. #include <linux/vmalloc.h>
  33. #include <linux/slab.h>
  34. #include "../hv/hyperv_vmbus.h"
  35. #define DRIVER_VERSION "0.02.0"
  36. #define DRIVER_AUTHOR "Stephen Hemminger <sthemmin at microsoft.com>"
  37. #define DRIVER_DESC "Generic UIO driver for VMBus devices"
  38. #define HV_RING_SIZE 512 /* pages */
  39. #define SEND_BUFFER_SIZE (15 * 1024 * 1024)
  40. #define RECV_BUFFER_SIZE (15 * 1024 * 1024)
  41. /*
  42. * List of resources to be mapped to user space
  43. * can be extended up to MAX_UIO_MAPS(5) items
  44. */
  45. enum hv_uio_map {
  46. TXRX_RING_MAP = 0,
  47. INT_PAGE_MAP,
  48. MON_PAGE_MAP,
  49. RECV_BUF_MAP,
  50. SEND_BUF_MAP
  51. };
  52. struct hv_uio_private_data {
  53. struct uio_info info;
  54. struct hv_device *device;
  55. void *recv_buf;
  56. u32 recv_gpadl;
  57. char recv_name[32]; /* "recv_4294967295" */
  58. void *send_buf;
  59. u32 send_gpadl;
  60. char send_name[32];
  61. };
  62. /*
  63. * This is the irqcontrol callback to be registered to uio_info.
  64. * It can be used to disable/enable interrupt from user space processes.
  65. *
  66. * @param info
  67. * pointer to uio_info.
  68. * @param irq_state
  69. * state value. 1 to enable interrupt, 0 to disable interrupt.
  70. */
  71. static int
  72. hv_uio_irqcontrol(struct uio_info *info, s32 irq_state)
  73. {
  74. struct hv_uio_private_data *pdata = info->priv;
  75. struct hv_device *dev = pdata->device;
  76. dev->channel->inbound.ring_buffer->interrupt_mask = !irq_state;
  77. virt_mb();
  78. return 0;
  79. }
  80. /*
  81. * Callback from vmbus_event when something is in inbound ring.
  82. */
  83. static void hv_uio_channel_cb(void *context)
  84. {
  85. struct vmbus_channel *chan = context;
  86. struct hv_device *hv_dev = chan->device_obj;
  87. struct hv_uio_private_data *pdata = hv_get_drvdata(hv_dev);
  88. chan->inbound.ring_buffer->interrupt_mask = 1;
  89. virt_mb();
  90. uio_event_notify(&pdata->info);
  91. }
  92. /*
  93. * Callback from vmbus_event when channel is rescinded.
  94. */
  95. static void hv_uio_rescind(struct vmbus_channel *channel)
  96. {
  97. struct hv_device *hv_dev = channel->primary_channel->device_obj;
  98. struct hv_uio_private_data *pdata = hv_get_drvdata(hv_dev);
  99. /*
  100. * Turn off the interrupt file handle
  101. * Next read for event will return -EIO
  102. */
  103. pdata->info.irq = 0;
  104. /* Wake up reader */
  105. uio_event_notify(&pdata->info);
  106. }
  107. /* Sysfs API to allow mmap of the ring buffers
  108. * The ring buffer is allocated as contiguous memory by vmbus_open
  109. */
  110. static int hv_uio_ring_mmap(struct file *filp, struct kobject *kobj,
  111. struct bin_attribute *attr,
  112. struct vm_area_struct *vma)
  113. {
  114. struct vmbus_channel *channel
  115. = container_of(kobj, struct vmbus_channel, kobj);
  116. struct hv_device *dev = channel->primary_channel->device_obj;
  117. u16 q_idx = channel->offermsg.offer.sub_channel_index;
  118. dev_dbg(&dev->device, "mmap channel %u pages %#lx at %#lx\n",
  119. q_idx, vma_pages(vma), vma->vm_pgoff);
  120. return vm_iomap_memory(vma, virt_to_phys(channel->ringbuffer_pages),
  121. channel->ringbuffer_pagecount << PAGE_SHIFT);
  122. }
  123. static const struct bin_attribute ring_buffer_bin_attr = {
  124. .attr = {
  125. .name = "ring",
  126. .mode = 0600,
  127. },
  128. .size = 2 * HV_RING_SIZE * PAGE_SIZE,
  129. .mmap = hv_uio_ring_mmap,
  130. };
  131. /* Callback from VMBUS subsystem when new channel created. */
  132. static void
  133. hv_uio_new_channel(struct vmbus_channel *new_sc)
  134. {
  135. struct hv_device *hv_dev = new_sc->primary_channel->device_obj;
  136. struct device *device = &hv_dev->device;
  137. const size_t ring_bytes = HV_RING_SIZE * PAGE_SIZE;
  138. int ret;
  139. /* Create host communication ring */
  140. ret = vmbus_open(new_sc, ring_bytes, ring_bytes, NULL, 0,
  141. hv_uio_channel_cb, new_sc);
  142. if (ret) {
  143. dev_err(device, "vmbus_open subchannel failed: %d\n", ret);
  144. return;
  145. }
  146. /* Disable interrupts on sub channel */
  147. new_sc->inbound.ring_buffer->interrupt_mask = 1;
  148. set_channel_read_mode(new_sc, HV_CALL_ISR);
  149. ret = sysfs_create_bin_file(&new_sc->kobj, &ring_buffer_bin_attr);
  150. if (ret) {
  151. dev_err(device, "sysfs create ring bin file failed; %d\n", ret);
  152. vmbus_close(new_sc);
  153. }
  154. }
  155. static void
  156. hv_uio_cleanup(struct hv_device *dev, struct hv_uio_private_data *pdata)
  157. {
  158. if (pdata->send_gpadl)
  159. vmbus_teardown_gpadl(dev->channel, pdata->send_gpadl);
  160. vfree(pdata->send_buf);
  161. if (pdata->recv_gpadl)
  162. vmbus_teardown_gpadl(dev->channel, pdata->recv_gpadl);
  163. vfree(pdata->recv_buf);
  164. }
  165. static int
  166. hv_uio_probe(struct hv_device *dev,
  167. const struct hv_vmbus_device_id *dev_id)
  168. {
  169. struct hv_uio_private_data *pdata;
  170. int ret;
  171. pdata = kzalloc(sizeof(*pdata), GFP_KERNEL);
  172. if (!pdata)
  173. return -ENOMEM;
  174. ret = vmbus_open(dev->channel, HV_RING_SIZE * PAGE_SIZE,
  175. HV_RING_SIZE * PAGE_SIZE, NULL, 0,
  176. hv_uio_channel_cb, dev->channel);
  177. if (ret)
  178. goto fail;
  179. /* Communicating with host has to be via shared memory not hypercall */
  180. if (!dev->channel->offermsg.monitor_allocated) {
  181. dev_err(&dev->device, "vmbus channel requires hypercall\n");
  182. ret = -ENOTSUPP;
  183. goto fail_close;
  184. }
  185. dev->channel->inbound.ring_buffer->interrupt_mask = 1;
  186. set_channel_read_mode(dev->channel, HV_CALL_ISR);
  187. /* Fill general uio info */
  188. pdata->info.name = "uio_hv_generic";
  189. pdata->info.version = DRIVER_VERSION;
  190. pdata->info.irqcontrol = hv_uio_irqcontrol;
  191. pdata->info.irq = UIO_IRQ_CUSTOM;
  192. /* mem resources */
  193. pdata->info.mem[TXRX_RING_MAP].name = "txrx_rings";
  194. pdata->info.mem[TXRX_RING_MAP].addr
  195. = (uintptr_t)dev->channel->ringbuffer_pages;
  196. pdata->info.mem[TXRX_RING_MAP].size
  197. = dev->channel->ringbuffer_pagecount << PAGE_SHIFT;
  198. pdata->info.mem[TXRX_RING_MAP].memtype = UIO_MEM_LOGICAL;
  199. pdata->info.mem[INT_PAGE_MAP].name = "int_page";
  200. pdata->info.mem[INT_PAGE_MAP].addr
  201. = (uintptr_t)vmbus_connection.int_page;
  202. pdata->info.mem[INT_PAGE_MAP].size = PAGE_SIZE;
  203. pdata->info.mem[INT_PAGE_MAP].memtype = UIO_MEM_LOGICAL;
  204. pdata->info.mem[MON_PAGE_MAP].name = "monitor_page";
  205. pdata->info.mem[MON_PAGE_MAP].addr
  206. = (uintptr_t)vmbus_connection.monitor_pages[1];
  207. pdata->info.mem[MON_PAGE_MAP].size = PAGE_SIZE;
  208. pdata->info.mem[MON_PAGE_MAP].memtype = UIO_MEM_LOGICAL;
  209. pdata->recv_buf = vzalloc(RECV_BUFFER_SIZE);
  210. if (pdata->recv_buf == NULL) {
  211. ret = -ENOMEM;
  212. goto fail_close;
  213. }
  214. ret = vmbus_establish_gpadl(dev->channel, pdata->recv_buf,
  215. RECV_BUFFER_SIZE, &pdata->recv_gpadl);
  216. if (ret)
  217. goto fail_close;
  218. /* put Global Physical Address Label in name */
  219. snprintf(pdata->recv_name, sizeof(pdata->recv_name),
  220. "recv:%u", pdata->recv_gpadl);
  221. pdata->info.mem[RECV_BUF_MAP].name = pdata->recv_name;
  222. pdata->info.mem[RECV_BUF_MAP].addr
  223. = (uintptr_t)pdata->recv_buf;
  224. pdata->info.mem[RECV_BUF_MAP].size = RECV_BUFFER_SIZE;
  225. pdata->info.mem[RECV_BUF_MAP].memtype = UIO_MEM_VIRTUAL;
  226. pdata->send_buf = vzalloc(SEND_BUFFER_SIZE);
  227. if (pdata->send_buf == NULL) {
  228. ret = -ENOMEM;
  229. goto fail_close;
  230. }
  231. ret = vmbus_establish_gpadl(dev->channel, pdata->send_buf,
  232. SEND_BUFFER_SIZE, &pdata->send_gpadl);
  233. if (ret)
  234. goto fail_close;
  235. snprintf(pdata->send_name, sizeof(pdata->send_name),
  236. "send:%u", pdata->send_gpadl);
  237. pdata->info.mem[SEND_BUF_MAP].name = pdata->send_name;
  238. pdata->info.mem[SEND_BUF_MAP].addr
  239. = (uintptr_t)pdata->send_buf;
  240. pdata->info.mem[SEND_BUF_MAP].size = SEND_BUFFER_SIZE;
  241. pdata->info.mem[SEND_BUF_MAP].memtype = UIO_MEM_VIRTUAL;
  242. pdata->info.priv = pdata;
  243. pdata->device = dev;
  244. ret = uio_register_device(&dev->device, &pdata->info);
  245. if (ret) {
  246. dev_err(&dev->device, "hv_uio register failed\n");
  247. goto fail_close;
  248. }
  249. vmbus_set_chn_rescind_callback(dev->channel, hv_uio_rescind);
  250. vmbus_set_sc_create_callback(dev->channel, hv_uio_new_channel);
  251. ret = sysfs_create_bin_file(&dev->channel->kobj, &ring_buffer_bin_attr);
  252. if (ret)
  253. dev_notice(&dev->device,
  254. "sysfs create ring bin file failed; %d\n", ret);
  255. hv_set_drvdata(dev, pdata);
  256. return 0;
  257. fail_close:
  258. hv_uio_cleanup(dev, pdata);
  259. vmbus_close(dev->channel);
  260. fail:
  261. kfree(pdata);
  262. return ret;
  263. }
  264. static int
  265. hv_uio_remove(struct hv_device *dev)
  266. {
  267. struct hv_uio_private_data *pdata = hv_get_drvdata(dev);
  268. if (!pdata)
  269. return 0;
  270. uio_unregister_device(&pdata->info);
  271. hv_uio_cleanup(dev, pdata);
  272. hv_set_drvdata(dev, NULL);
  273. vmbus_close(dev->channel);
  274. kfree(pdata);
  275. return 0;
  276. }
  277. static struct hv_driver hv_uio_drv = {
  278. .name = "uio_hv_generic",
  279. .id_table = NULL, /* only dynamic id's */
  280. .probe = hv_uio_probe,
  281. .remove = hv_uio_remove,
  282. };
  283. static int __init
  284. hyperv_module_init(void)
  285. {
  286. return vmbus_driver_register(&hv_uio_drv);
  287. }
  288. static void __exit
  289. hyperv_module_exit(void)
  290. {
  291. vmbus_driver_unregister(&hv_uio_drv);
  292. }
  293. module_init(hyperv_module_init);
  294. module_exit(hyperv_module_exit);
  295. MODULE_VERSION(DRIVER_VERSION);
  296. MODULE_LICENSE("GPL v2");
  297. MODULE_AUTHOR(DRIVER_AUTHOR);
  298. MODULE_DESCRIPTION(DRIVER_DESC);