uio_hv_generic.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403
  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 SEND_BUFFER_SIZE (16 * 1024 * 1024)
  36. #define RECV_BUFFER_SIZE (31 * 1024 * 1024)
  37. /*
  38. * List of resources to be mapped to user space
  39. * can be extended up to MAX_UIO_MAPS(5) items
  40. */
  41. enum hv_uio_map {
  42. TXRX_RING_MAP = 0,
  43. INT_PAGE_MAP,
  44. MON_PAGE_MAP,
  45. RECV_BUF_MAP,
  46. SEND_BUF_MAP
  47. };
  48. struct hv_uio_private_data {
  49. struct uio_info info;
  50. struct hv_device *device;
  51. atomic_t refcnt;
  52. void *recv_buf;
  53. struct vmbus_gpadl recv_gpadl;
  54. char recv_name[32]; /* "recv_4294967295" */
  55. void *send_buf;
  56. struct vmbus_gpadl send_gpadl;
  57. char send_name[32];
  58. };
  59. /*
  60. * This is the irqcontrol callback to be registered to uio_info.
  61. * It can be used to disable/enable interrupt from user space processes.
  62. *
  63. * @param info
  64. * pointer to uio_info.
  65. * @param irq_state
  66. * state value. 1 to enable interrupt, 0 to disable interrupt.
  67. */
  68. static int
  69. hv_uio_irqcontrol(struct uio_info *info, s32 irq_state)
  70. {
  71. struct hv_uio_private_data *pdata = info->priv;
  72. struct hv_device *dev = pdata->device;
  73. dev->channel->inbound.ring_buffer->interrupt_mask = !irq_state;
  74. virt_mb();
  75. if (!dev->channel->offermsg.monitor_allocated && irq_state)
  76. vmbus_setevent(dev->channel);
  77. return 0;
  78. }
  79. /*
  80. * Callback from vmbus_event when something is in inbound ring.
  81. */
  82. static void hv_uio_channel_cb(void *context)
  83. {
  84. struct vmbus_channel *chan = context;
  85. struct hv_device *hv_dev = chan->device_obj;
  86. struct hv_uio_private_data *pdata = hv_get_drvdata(hv_dev);
  87. virt_mb();
  88. uio_event_notify(&pdata->info);
  89. }
  90. /*
  91. * Callback from vmbus_event when channel is rescinded.
  92. * It is meant for rescind of primary channels only.
  93. */
  94. static void hv_uio_rescind(struct vmbus_channel *channel)
  95. {
  96. struct hv_device *hv_dev = channel->device_obj;
  97. struct hv_uio_private_data *pdata = hv_get_drvdata(hv_dev);
  98. /*
  99. * Turn off the interrupt file handle
  100. * Next read for event will return -EIO
  101. */
  102. pdata->info.irq = 0;
  103. /* Wake up reader */
  104. uio_event_notify(&pdata->info);
  105. /*
  106. * With rescind callback registered, rescind path will not unregister the device
  107. * from vmbus when the primary channel is rescinded.
  108. * Without it, rescind handling is incomplete and next onoffer msg does not come.
  109. * Unregister the device from vmbus here.
  110. */
  111. vmbus_device_unregister(channel->device_obj);
  112. }
  113. /* Function used for mmap of ring buffer sysfs interface.
  114. * The ring buffer is allocated as contiguous memory by vmbus_open
  115. */
  116. static int
  117. hv_uio_ring_mmap(struct vmbus_channel *channel, struct vm_area_struct *vma)
  118. {
  119. void *ring_buffer = page_address(channel->ringbuffer_page);
  120. if (channel->state != CHANNEL_OPENED_STATE)
  121. return -ENODEV;
  122. return vm_iomap_memory(vma, virt_to_phys(ring_buffer),
  123. channel->ringbuffer_pagecount << PAGE_SHIFT);
  124. }
  125. /* Callback from VMBUS subsystem when new channel created. */
  126. static void
  127. hv_uio_new_channel(struct vmbus_channel *new_sc)
  128. {
  129. struct hv_device *hv_dev = new_sc->primary_channel->device_obj;
  130. struct device *device = &hv_dev->device;
  131. const size_t ring_bytes = SZ_2M;
  132. int ret;
  133. /* Create host communication ring */
  134. ret = vmbus_open(new_sc, ring_bytes, ring_bytes, NULL, 0,
  135. hv_uio_channel_cb, new_sc);
  136. if (ret) {
  137. dev_err(device, "vmbus_open subchannel failed: %d\n", ret);
  138. return;
  139. }
  140. set_channel_read_mode(new_sc, HV_CALL_ISR);
  141. ret = hv_create_ring_sysfs(new_sc, hv_uio_ring_mmap);
  142. if (ret) {
  143. dev_err(device, "sysfs create ring bin file failed; %d\n", ret);
  144. vmbus_close(new_sc);
  145. }
  146. }
  147. /* free the reserved buffers for send and receive */
  148. static void
  149. hv_uio_cleanup(struct hv_device *dev, struct hv_uio_private_data *pdata)
  150. {
  151. if (pdata->send_gpadl.gpadl_handle) {
  152. vmbus_teardown_gpadl(dev->channel, &pdata->send_gpadl);
  153. if (!pdata->send_gpadl.decrypted)
  154. vfree(pdata->send_buf);
  155. }
  156. if (pdata->recv_gpadl.gpadl_handle) {
  157. vmbus_teardown_gpadl(dev->channel, &pdata->recv_gpadl);
  158. if (!pdata->recv_gpadl.decrypted)
  159. vfree(pdata->recv_buf);
  160. }
  161. }
  162. /* VMBus primary channel is opened on first use */
  163. static int
  164. hv_uio_open(struct uio_info *info, struct inode *inode)
  165. {
  166. struct hv_uio_private_data *pdata
  167. = container_of(info, struct hv_uio_private_data, info);
  168. struct hv_device *dev = pdata->device;
  169. int ret;
  170. if (atomic_inc_return(&pdata->refcnt) != 1)
  171. return 0;
  172. vmbus_set_chn_rescind_callback(dev->channel, hv_uio_rescind);
  173. vmbus_set_sc_create_callback(dev->channel, hv_uio_new_channel);
  174. ret = vmbus_connect_ring(dev->channel,
  175. hv_uio_channel_cb, dev->channel);
  176. if (ret)
  177. atomic_dec(&pdata->refcnt);
  178. return ret;
  179. }
  180. /* VMBus primary channel is closed on last close */
  181. static int
  182. hv_uio_release(struct uio_info *info, struct inode *inode)
  183. {
  184. struct hv_uio_private_data *pdata
  185. = container_of(info, struct hv_uio_private_data, info);
  186. struct hv_device *dev = pdata->device;
  187. int ret = 0;
  188. if (atomic_dec_and_test(&pdata->refcnt))
  189. ret = vmbus_disconnect_ring(dev->channel);
  190. return ret;
  191. }
  192. static int
  193. hv_uio_probe(struct hv_device *dev,
  194. const struct hv_vmbus_device_id *dev_id)
  195. {
  196. struct vmbus_channel *channel = dev->channel;
  197. struct hv_uio_private_data *pdata;
  198. void *ring_buffer;
  199. int ret;
  200. size_t ring_size = hv_dev_ring_size(channel);
  201. if (!ring_size)
  202. ring_size = SZ_2M;
  203. /* Adjust ring size if necessary to have it page aligned */
  204. ring_size = VMBUS_RING_SIZE(ring_size);
  205. pdata = devm_kzalloc(&dev->device, sizeof(*pdata), GFP_KERNEL);
  206. if (!pdata)
  207. return -ENOMEM;
  208. ret = vmbus_alloc_ring(channel, ring_size, ring_size);
  209. if (ret)
  210. return ret;
  211. set_channel_read_mode(channel, HV_CALL_ISR);
  212. /* Fill general uio info */
  213. pdata->info.name = "uio_hv_generic";
  214. pdata->info.version = DRIVER_VERSION;
  215. pdata->info.irqcontrol = hv_uio_irqcontrol;
  216. pdata->info.open = hv_uio_open;
  217. pdata->info.release = hv_uio_release;
  218. pdata->info.irq = UIO_IRQ_CUSTOM;
  219. atomic_set(&pdata->refcnt, 0);
  220. /* mem resources */
  221. pdata->info.mem[TXRX_RING_MAP].name = "txrx_rings";
  222. ring_buffer = page_address(channel->ringbuffer_page);
  223. pdata->info.mem[TXRX_RING_MAP].addr
  224. = (uintptr_t)virt_to_phys(ring_buffer);
  225. pdata->info.mem[TXRX_RING_MAP].size
  226. = channel->ringbuffer_pagecount << PAGE_SHIFT;
  227. pdata->info.mem[TXRX_RING_MAP].memtype = UIO_MEM_IOVA;
  228. pdata->info.mem[INT_PAGE_MAP].name = "int_page";
  229. pdata->info.mem[INT_PAGE_MAP].addr
  230. = (uintptr_t)vmbus_connection.int_page;
  231. pdata->info.mem[INT_PAGE_MAP].size = HV_HYP_PAGE_SIZE;
  232. pdata->info.mem[INT_PAGE_MAP].memtype = UIO_MEM_LOGICAL;
  233. pdata->info.mem[MON_PAGE_MAP].name = "monitor_page";
  234. pdata->info.mem[MON_PAGE_MAP].addr
  235. = (uintptr_t)vmbus_connection.monitor_pages[1];
  236. pdata->info.mem[MON_PAGE_MAP].size = HV_HYP_PAGE_SIZE;
  237. pdata->info.mem[MON_PAGE_MAP].memtype = UIO_MEM_LOGICAL;
  238. pdata->recv_buf = vzalloc(RECV_BUFFER_SIZE);
  239. if (pdata->recv_buf == NULL) {
  240. ret = -ENOMEM;
  241. goto fail_free_ring;
  242. }
  243. ret = vmbus_establish_gpadl(channel, pdata->recv_buf,
  244. RECV_BUFFER_SIZE, &pdata->recv_gpadl);
  245. if (ret) {
  246. if (!pdata->recv_gpadl.decrypted)
  247. vfree(pdata->recv_buf);
  248. goto fail_close;
  249. }
  250. /* put Global Physical Address Label in name */
  251. snprintf(pdata->recv_name, sizeof(pdata->recv_name),
  252. "recv:%u", pdata->recv_gpadl.gpadl_handle);
  253. pdata->info.mem[RECV_BUF_MAP].name = pdata->recv_name;
  254. pdata->info.mem[RECV_BUF_MAP].addr
  255. = (uintptr_t)pdata->recv_buf;
  256. pdata->info.mem[RECV_BUF_MAP].size = RECV_BUFFER_SIZE;
  257. pdata->info.mem[RECV_BUF_MAP].memtype = UIO_MEM_VIRTUAL;
  258. pdata->send_buf = vzalloc(SEND_BUFFER_SIZE);
  259. if (pdata->send_buf == NULL) {
  260. ret = -ENOMEM;
  261. goto fail_close;
  262. }
  263. ret = vmbus_establish_gpadl(channel, pdata->send_buf,
  264. SEND_BUFFER_SIZE, &pdata->send_gpadl);
  265. if (ret) {
  266. if (!pdata->send_gpadl.decrypted)
  267. vfree(pdata->send_buf);
  268. goto fail_close;
  269. }
  270. snprintf(pdata->send_name, sizeof(pdata->send_name),
  271. "send:%u", pdata->send_gpadl.gpadl_handle);
  272. pdata->info.mem[SEND_BUF_MAP].name = pdata->send_name;
  273. pdata->info.mem[SEND_BUF_MAP].addr
  274. = (uintptr_t)pdata->send_buf;
  275. pdata->info.mem[SEND_BUF_MAP].size = SEND_BUFFER_SIZE;
  276. pdata->info.mem[SEND_BUF_MAP].memtype = UIO_MEM_VIRTUAL;
  277. pdata->info.priv = pdata;
  278. pdata->device = dev;
  279. ret = uio_register_device(&dev->device, &pdata->info);
  280. if (ret) {
  281. dev_err(&dev->device, "hv_uio register failed\n");
  282. goto fail_close;
  283. }
  284. /*
  285. * This internally calls sysfs_update_group, which returns a non-zero value if it executes
  286. * before sysfs_create_group. This is expected as the 'ring' will be created later in
  287. * vmbus_device_register() -> vmbus_add_channel_kobj(). Thus, no need to check the return
  288. * value and print warning.
  289. *
  290. * Creating/exposing sysfs in driver probe is not encouraged as it can lead to race
  291. * conditions with userspace. For backward compatibility, "ring" sysfs could not be removed
  292. * or decoupled from uio_hv_generic probe. Userspace programs can make use of inotify
  293. * APIs to make sure that ring is created.
  294. */
  295. hv_create_ring_sysfs(channel, hv_uio_ring_mmap);
  296. hv_set_drvdata(dev, pdata);
  297. return 0;
  298. fail_close:
  299. hv_uio_cleanup(dev, pdata);
  300. fail_free_ring:
  301. vmbus_free_ring(dev->channel);
  302. return ret;
  303. }
  304. static void
  305. hv_uio_remove(struct hv_device *dev)
  306. {
  307. struct hv_uio_private_data *pdata = hv_get_drvdata(dev);
  308. if (!pdata)
  309. return;
  310. hv_remove_ring_sysfs(dev->channel);
  311. uio_unregister_device(&pdata->info);
  312. hv_uio_cleanup(dev, pdata);
  313. vmbus_free_ring(dev->channel);
  314. }
  315. static struct hv_driver hv_uio_drv = {
  316. .name = "uio_hv_generic",
  317. .id_table = NULL, /* only dynamic id's */
  318. .probe = hv_uio_probe,
  319. .remove = hv_uio_remove,
  320. };
  321. static int __init
  322. hyperv_module_init(void)
  323. {
  324. return vmbus_driver_register(&hv_uio_drv);
  325. }
  326. static void __exit
  327. hyperv_module_exit(void)
  328. {
  329. vmbus_driver_unregister(&hv_uio_drv);
  330. }
  331. module_init(hyperv_module_init);
  332. module_exit(hyperv_module_exit);
  333. MODULE_VERSION(DRIVER_VERSION);
  334. MODULE_LICENSE("GPL v2");
  335. MODULE_AUTHOR(DRIVER_AUTHOR);
  336. MODULE_DESCRIPTION(DRIVER_DESC);