virtio_ctl_msg.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * virtio-snd: Virtio sound device
  4. * Copyright (C) 2021 OpenSynergy GmbH
  5. */
  6. #include <linux/moduleparam.h>
  7. #include <linux/virtio_config.h>
  8. #include "virtio_card.h"
  9. /**
  10. * struct virtio_snd_msg - Control message.
  11. * @sg_request: Scattergather list containing a device request (header).
  12. * @sg_response: Scattergather list containing a device response (status).
  13. * @list: Pending message list entry.
  14. * @notify: Request completed notification.
  15. * @ref_count: Reference count used to manage a message lifetime.
  16. */
  17. struct virtio_snd_msg {
  18. struct scatterlist sg_request;
  19. struct scatterlist sg_response;
  20. struct list_head list;
  21. struct completion notify;
  22. refcount_t ref_count;
  23. };
  24. /**
  25. * virtsnd_ctl_msg_ref() - Increment reference counter for the message.
  26. * @msg: Control message.
  27. *
  28. * Context: Any context.
  29. */
  30. void virtsnd_ctl_msg_ref(struct virtio_snd_msg *msg)
  31. {
  32. refcount_inc(&msg->ref_count);
  33. }
  34. /**
  35. * virtsnd_ctl_msg_unref() - Decrement reference counter for the message.
  36. * @msg: Control message.
  37. *
  38. * The message will be freed when the ref_count value is 0.
  39. *
  40. * Context: Any context.
  41. */
  42. void virtsnd_ctl_msg_unref(struct virtio_snd_msg *msg)
  43. {
  44. if (refcount_dec_and_test(&msg->ref_count))
  45. kfree(msg);
  46. }
  47. /**
  48. * virtsnd_ctl_msg_request() - Get a pointer to the request header.
  49. * @msg: Control message.
  50. *
  51. * Context: Any context.
  52. */
  53. void *virtsnd_ctl_msg_request(struct virtio_snd_msg *msg)
  54. {
  55. return sg_virt(&msg->sg_request);
  56. }
  57. /**
  58. * virtsnd_ctl_msg_response() - Get a pointer to the response header.
  59. * @msg: Control message.
  60. *
  61. * Context: Any context.
  62. */
  63. void *virtsnd_ctl_msg_response(struct virtio_snd_msg *msg)
  64. {
  65. return sg_virt(&msg->sg_response);
  66. }
  67. /**
  68. * virtsnd_ctl_msg_alloc() - Allocate and initialize a control message.
  69. * @request_size: Size of request header.
  70. * @response_size: Size of response header.
  71. * @gfp: Kernel flags for memory allocation.
  72. *
  73. * The message will be automatically freed when the ref_count value is 0.
  74. *
  75. * Context: Any context. May sleep if @gfp flags permit.
  76. * Return: Allocated message on success, NULL on failure.
  77. */
  78. struct virtio_snd_msg *virtsnd_ctl_msg_alloc(size_t request_size,
  79. size_t response_size, gfp_t gfp)
  80. {
  81. struct virtio_snd_msg *msg;
  82. if (!request_size || !response_size)
  83. return NULL;
  84. msg = kzalloc(sizeof(*msg) + request_size + response_size, gfp);
  85. if (!msg)
  86. return NULL;
  87. sg_init_one(&msg->sg_request, (u8 *)msg + sizeof(*msg), request_size);
  88. sg_init_one(&msg->sg_response, (u8 *)msg + sizeof(*msg) + request_size,
  89. response_size);
  90. INIT_LIST_HEAD(&msg->list);
  91. init_completion(&msg->notify);
  92. /* This reference is dropped in virtsnd_ctl_msg_complete(). */
  93. refcount_set(&msg->ref_count, 1);
  94. return msg;
  95. }
  96. /**
  97. * virtsnd_ctl_msg_send() - Send a control message.
  98. * @snd: VirtIO sound device.
  99. * @msg: Control message.
  100. * @out_sgs: Additional sg-list to attach to the request header (may be NULL).
  101. * @in_sgs: Additional sg-list to attach to the response header (may be NULL).
  102. * @nowait: Flag indicating whether to wait for completion.
  103. *
  104. * Context: Any context. Takes and releases the control queue spinlock.
  105. * May sleep if @nowait is false.
  106. * Return: 0 on success, -errno on failure.
  107. */
  108. int virtsnd_ctl_msg_send(struct virtio_snd *snd, struct virtio_snd_msg *msg,
  109. struct scatterlist *out_sgs,
  110. struct scatterlist *in_sgs, bool nowait)
  111. {
  112. struct virtio_device *vdev = snd->vdev;
  113. struct virtio_snd_queue *queue = virtsnd_control_queue(snd);
  114. unsigned int js = msecs_to_jiffies(virtsnd_msg_timeout_ms);
  115. struct virtio_snd_hdr *request = virtsnd_ctl_msg_request(msg);
  116. struct virtio_snd_hdr *response = virtsnd_ctl_msg_response(msg);
  117. unsigned int nouts = 0;
  118. unsigned int nins = 0;
  119. struct scatterlist *psgs[4];
  120. bool notify = false;
  121. int rc;
  122. virtsnd_ctl_msg_ref(msg);
  123. /* Set the default status in case the message was canceled. */
  124. response->code = cpu_to_le32(VIRTIO_SND_S_IO_ERR);
  125. psgs[nouts++] = &msg->sg_request;
  126. if (out_sgs)
  127. psgs[nouts++] = out_sgs;
  128. psgs[nouts + nins++] = &msg->sg_response;
  129. if (in_sgs)
  130. psgs[nouts + nins++] = in_sgs;
  131. scoped_guard(spinlock_irqsave, &queue->lock) {
  132. rc = virtqueue_add_sgs(queue->vqueue, psgs, nouts, nins, msg,
  133. GFP_ATOMIC);
  134. if (!rc) {
  135. notify = virtqueue_kick_prepare(queue->vqueue);
  136. list_add_tail(&msg->list, &snd->ctl_msgs);
  137. }
  138. }
  139. if (rc) {
  140. dev_err(&vdev->dev, "failed to send control message (0x%08x)\n",
  141. le32_to_cpu(request->code));
  142. /*
  143. * Since in this case virtsnd_ctl_msg_complete() will not be
  144. * called, it is necessary to decrement the reference count.
  145. */
  146. virtsnd_ctl_msg_unref(msg);
  147. goto on_exit;
  148. }
  149. if (notify)
  150. virtqueue_notify(queue->vqueue);
  151. if (nowait)
  152. goto on_exit;
  153. rc = wait_for_completion_interruptible_timeout(&msg->notify, js);
  154. if (rc <= 0) {
  155. if (!rc) {
  156. dev_err(&vdev->dev,
  157. "control message (0x%08x) timeout\n",
  158. le32_to_cpu(request->code));
  159. rc = -ETIMEDOUT;
  160. }
  161. goto on_exit;
  162. }
  163. switch (le32_to_cpu(response->code)) {
  164. case VIRTIO_SND_S_OK:
  165. rc = 0;
  166. break;
  167. case VIRTIO_SND_S_NOT_SUPP:
  168. rc = -EOPNOTSUPP;
  169. break;
  170. case VIRTIO_SND_S_IO_ERR:
  171. rc = -EIO;
  172. break;
  173. default:
  174. rc = -EINVAL;
  175. break;
  176. }
  177. on_exit:
  178. virtsnd_ctl_msg_unref(msg);
  179. return rc;
  180. }
  181. /**
  182. * virtsnd_ctl_msg_complete() - Complete a control message.
  183. * @msg: Control message.
  184. *
  185. * Context: Any context. Expects the control queue spinlock to be held by
  186. * caller.
  187. */
  188. void virtsnd_ctl_msg_complete(struct virtio_snd_msg *msg)
  189. {
  190. list_del(&msg->list);
  191. complete(&msg->notify);
  192. virtsnd_ctl_msg_unref(msg);
  193. }
  194. /**
  195. * virtsnd_ctl_msg_cancel_all() - Cancel all pending control messages.
  196. * @snd: VirtIO sound device.
  197. *
  198. * Context: Any context.
  199. */
  200. void virtsnd_ctl_msg_cancel_all(struct virtio_snd *snd)
  201. {
  202. struct virtio_snd_queue *queue = virtsnd_control_queue(snd);
  203. guard(spinlock_irqsave)(&queue->lock);
  204. while (!list_empty(&snd->ctl_msgs)) {
  205. struct virtio_snd_msg *msg =
  206. list_first_entry(&snd->ctl_msgs, struct virtio_snd_msg,
  207. list);
  208. virtsnd_ctl_msg_complete(msg);
  209. }
  210. }
  211. /**
  212. * virtsnd_ctl_query_info() - Query the item configuration from the device.
  213. * @snd: VirtIO sound device.
  214. * @command: Control request code (VIRTIO_SND_R_XXX_INFO).
  215. * @start_id: Item start identifier.
  216. * @count: Item count to query.
  217. * @size: Item information size in bytes.
  218. * @info: Buffer for storing item information.
  219. *
  220. * Context: Any context that permits to sleep.
  221. * Return: 0 on success, -errno on failure.
  222. */
  223. int virtsnd_ctl_query_info(struct virtio_snd *snd, int command, int start_id,
  224. int count, size_t size, void *info)
  225. {
  226. struct virtio_snd_msg *msg;
  227. struct virtio_snd_query_info *query;
  228. struct scatterlist sg;
  229. msg = virtsnd_ctl_msg_alloc(sizeof(*query),
  230. sizeof(struct virtio_snd_hdr), GFP_KERNEL);
  231. if (!msg)
  232. return -ENOMEM;
  233. query = virtsnd_ctl_msg_request(msg);
  234. query->hdr.code = cpu_to_le32(command);
  235. query->start_id = cpu_to_le32(start_id);
  236. query->count = cpu_to_le32(count);
  237. query->size = cpu_to_le32(size);
  238. sg_init_one(&sg, info, count * size);
  239. return virtsnd_ctl_msg_send(snd, msg, NULL, &sg, false);
  240. }
  241. /**
  242. * virtsnd_ctl_notify_cb() - Process all completed control messages.
  243. * @vqueue: Underlying control virtqueue.
  244. *
  245. * This callback function is called upon a vring interrupt request from the
  246. * device.
  247. *
  248. * Context: Interrupt context. Takes and releases the control queue spinlock.
  249. */
  250. void virtsnd_ctl_notify_cb(struct virtqueue *vqueue)
  251. {
  252. struct virtio_snd *snd = vqueue->vdev->priv;
  253. struct virtio_snd_queue *queue = virtsnd_control_queue(snd);
  254. struct virtio_snd_msg *msg;
  255. u32 length;
  256. guard(spinlock_irqsave)(&queue->lock);
  257. do {
  258. virtqueue_disable_cb(vqueue);
  259. while ((msg = virtqueue_get_buf(vqueue, &length)))
  260. virtsnd_ctl_msg_complete(msg);
  261. } while (!virtqueue_enable_cb(vqueue));
  262. }