virtio_ctl_msg.c 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  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. unsigned long flags;
  122. int rc;
  123. virtsnd_ctl_msg_ref(msg);
  124. /* Set the default status in case the message was canceled. */
  125. response->code = cpu_to_le32(VIRTIO_SND_S_IO_ERR);
  126. psgs[nouts++] = &msg->sg_request;
  127. if (out_sgs)
  128. psgs[nouts++] = out_sgs;
  129. psgs[nouts + nins++] = &msg->sg_response;
  130. if (in_sgs)
  131. psgs[nouts + nins++] = in_sgs;
  132. spin_lock_irqsave(&queue->lock, flags);
  133. rc = virtqueue_add_sgs(queue->vqueue, psgs, nouts, nins, msg,
  134. GFP_ATOMIC);
  135. if (!rc) {
  136. notify = virtqueue_kick_prepare(queue->vqueue);
  137. list_add_tail(&msg->list, &snd->ctl_msgs);
  138. }
  139. spin_unlock_irqrestore(&queue->lock, flags);
  140. if (rc) {
  141. dev_err(&vdev->dev, "failed to send control message (0x%08x)\n",
  142. le32_to_cpu(request->code));
  143. /*
  144. * Since in this case virtsnd_ctl_msg_complete() will not be
  145. * called, it is necessary to decrement the reference count.
  146. */
  147. virtsnd_ctl_msg_unref(msg);
  148. goto on_exit;
  149. }
  150. if (notify)
  151. virtqueue_notify(queue->vqueue);
  152. if (nowait)
  153. goto on_exit;
  154. rc = wait_for_completion_interruptible_timeout(&msg->notify, js);
  155. if (rc <= 0) {
  156. if (!rc) {
  157. dev_err(&vdev->dev,
  158. "control message (0x%08x) timeout\n",
  159. le32_to_cpu(request->code));
  160. rc = -ETIMEDOUT;
  161. }
  162. goto on_exit;
  163. }
  164. switch (le32_to_cpu(response->code)) {
  165. case VIRTIO_SND_S_OK:
  166. rc = 0;
  167. break;
  168. case VIRTIO_SND_S_NOT_SUPP:
  169. rc = -EOPNOTSUPP;
  170. break;
  171. case VIRTIO_SND_S_IO_ERR:
  172. rc = -EIO;
  173. break;
  174. default:
  175. rc = -EINVAL;
  176. break;
  177. }
  178. on_exit:
  179. virtsnd_ctl_msg_unref(msg);
  180. return rc;
  181. }
  182. /**
  183. * virtsnd_ctl_msg_complete() - Complete a control message.
  184. * @msg: Control message.
  185. *
  186. * Context: Any context. Expects the control queue spinlock to be held by
  187. * caller.
  188. */
  189. void virtsnd_ctl_msg_complete(struct virtio_snd_msg *msg)
  190. {
  191. list_del(&msg->list);
  192. complete(&msg->notify);
  193. virtsnd_ctl_msg_unref(msg);
  194. }
  195. /**
  196. * virtsnd_ctl_msg_cancel_all() - Cancel all pending control messages.
  197. * @snd: VirtIO sound device.
  198. *
  199. * Context: Any context.
  200. */
  201. void virtsnd_ctl_msg_cancel_all(struct virtio_snd *snd)
  202. {
  203. struct virtio_snd_queue *queue = virtsnd_control_queue(snd);
  204. unsigned long flags;
  205. spin_lock_irqsave(&queue->lock, flags);
  206. while (!list_empty(&snd->ctl_msgs)) {
  207. struct virtio_snd_msg *msg =
  208. list_first_entry(&snd->ctl_msgs, struct virtio_snd_msg,
  209. list);
  210. virtsnd_ctl_msg_complete(msg);
  211. }
  212. spin_unlock_irqrestore(&queue->lock, flags);
  213. }
  214. /**
  215. * virtsnd_ctl_query_info() - Query the item configuration from the device.
  216. * @snd: VirtIO sound device.
  217. * @command: Control request code (VIRTIO_SND_R_XXX_INFO).
  218. * @start_id: Item start identifier.
  219. * @count: Item count to query.
  220. * @size: Item information size in bytes.
  221. * @info: Buffer for storing item information.
  222. *
  223. * Context: Any context that permits to sleep.
  224. * Return: 0 on success, -errno on failure.
  225. */
  226. int virtsnd_ctl_query_info(struct virtio_snd *snd, int command, int start_id,
  227. int count, size_t size, void *info)
  228. {
  229. struct virtio_snd_msg *msg;
  230. struct virtio_snd_query_info *query;
  231. struct scatterlist sg;
  232. msg = virtsnd_ctl_msg_alloc(sizeof(*query),
  233. sizeof(struct virtio_snd_hdr), GFP_KERNEL);
  234. if (!msg)
  235. return -ENOMEM;
  236. query = virtsnd_ctl_msg_request(msg);
  237. query->hdr.code = cpu_to_le32(command);
  238. query->start_id = cpu_to_le32(start_id);
  239. query->count = cpu_to_le32(count);
  240. query->size = cpu_to_le32(size);
  241. sg_init_one(&sg, info, count * size);
  242. return virtsnd_ctl_msg_send(snd, msg, NULL, &sg, false);
  243. }
  244. /**
  245. * virtsnd_ctl_notify_cb() - Process all completed control messages.
  246. * @vqueue: Underlying control virtqueue.
  247. *
  248. * This callback function is called upon a vring interrupt request from the
  249. * device.
  250. *
  251. * Context: Interrupt context. Takes and releases the control queue spinlock.
  252. */
  253. void virtsnd_ctl_notify_cb(struct virtqueue *vqueue)
  254. {
  255. struct virtio_snd *snd = vqueue->vdev->priv;
  256. struct virtio_snd_queue *queue = virtsnd_control_queue(snd);
  257. struct virtio_snd_msg *msg;
  258. u32 length;
  259. unsigned long flags;
  260. spin_lock_irqsave(&queue->lock, flags);
  261. do {
  262. virtqueue_disable_cb(vqueue);
  263. while ((msg = virtqueue_get_buf(vqueue, &length)))
  264. virtsnd_ctl_msg_complete(msg);
  265. if (unlikely(virtqueue_is_broken(vqueue)))
  266. break;
  267. } while (!virtqueue_enable_cb(vqueue));
  268. spin_unlock_irqrestore(&queue->lock, flags);
  269. }