uio_hv_generic.c 11 KB

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