1 From d4c8a3a4b9de5a25b6963f3ae1b8a5cb32081de5 Mon Sep 17 00:00:00 2001
2 From: Anton Yakovlev <anton.yakovlev@opensynergy.com>
3 Date: Tue, 2 Mar 2021 17:47:03 +0100
4 Subject: [PATCH] ALSA: virtio: handling control messages
6 The control queue can be used by different parts of the driver to send
7 commands to the device. Control messages can be either synchronous or
8 asynchronous. The lifetime of a message is controlled by a reference
11 Introduce a module parameter to set the message completion timeout:
12 msg_timeout_ms [=1000]
14 Signed-off-by: Anton Yakovlev <anton.yakovlev@opensynergy.com>
15 Link: https://lore.kernel.org/r/20210302164709.3142702-4-anton.yakovlev@opensynergy.com
16 Signed-off-by: Takashi Iwai <tiwai@suse.de>
17 Signed-off-by: Vasyl Vavrychuk <vasyl.vavrychuk@opensynergy.com>
19 sound/virtio/Makefile | 3 +-
20 sound/virtio/virtio_card.c | 13 ++
21 sound/virtio/virtio_card.h | 7 +
22 sound/virtio/virtio_ctl_msg.c | 310 ++++++++++++++++++++++++++++++++++
23 sound/virtio/virtio_ctl_msg.h | 78 +++++++++
24 5 files changed, 410 insertions(+), 1 deletion(-)
25 create mode 100644 sound/virtio/virtio_ctl_msg.c
26 create mode 100644 sound/virtio/virtio_ctl_msg.h
28 diff --git a/sound/virtio/Makefile b/sound/virtio/Makefile
29 index 8c87ebb9982b..dc551e637441 100644
30 --- a/sound/virtio/Makefile
31 +++ b/sound/virtio/Makefile
33 obj-$(CONFIG_SND_VIRTIO) += virtio_snd.o
40 diff --git a/sound/virtio/virtio_card.c b/sound/virtio/virtio_card.c
41 index 5a37056858e9..b757b2444078 100644
42 --- a/sound/virtio/virtio_card.c
43 +++ b/sound/virtio/virtio_card.c
46 #include "virtio_card.h"
48 +u32 virtsnd_msg_timeout_ms = MSEC_PER_SEC;
49 +module_param_named(msg_timeout_ms, virtsnd_msg_timeout_ms, uint, 0644);
50 +MODULE_PARM_DESC(msg_timeout_ms, "Message completion timeout in milliseconds");
52 static void virtsnd_remove(struct virtio_device *vdev);
55 @@ -96,9 +100,11 @@ static int virtsnd_find_vqs(struct virtio_snd *snd)
57 struct virtio_device *vdev = snd->vdev;
58 static vq_callback_t *callbacks[VIRTIO_SND_VQ_MAX] = {
59 + [VIRTIO_SND_VQ_CONTROL] = virtsnd_ctl_notify_cb,
60 [VIRTIO_SND_VQ_EVENT] = virtsnd_event_notify_cb
62 static const char *names[VIRTIO_SND_VQ_MAX] = {
63 + [VIRTIO_SND_VQ_CONTROL] = "virtsnd-ctl",
64 [VIRTIO_SND_VQ_EVENT] = "virtsnd-event"
66 struct virtqueue *vqs[VIRTIO_SND_VQ_MAX] = { 0 };
67 @@ -226,6 +232,11 @@ static int virtsnd_validate(struct virtio_device *vdev)
71 + if (!virtsnd_msg_timeout_ms) {
72 + dev_err(&vdev->dev, "msg_timeout_ms value cannot be zero\n");
79 @@ -247,6 +258,7 @@ static int virtsnd_probe(struct virtio_device *vdev)
83 + INIT_LIST_HEAD(&snd->ctl_msgs);
87 @@ -283,6 +295,7 @@ static void virtsnd_remove(struct virtio_device *vdev)
88 struct virtio_snd *snd = vdev->priv;
90 virtsnd_disable_event_vq(snd);
91 + virtsnd_ctl_msg_cancel_all(snd);
94 snd_card_free(snd->card);
95 diff --git a/sound/virtio/virtio_card.h b/sound/virtio/virtio_card.h
96 index b903b1b12e90..1e76eeff160f 100644
97 --- a/sound/virtio/virtio_card.h
98 +++ b/sound/virtio/virtio_card.h
100 #include <sound/core.h>
101 #include <uapi/linux/virtio_snd.h>
103 +#include "virtio_ctl_msg.h"
105 #define VIRTIO_SND_CARD_DRIVER "virtio-snd"
106 #define VIRTIO_SND_CARD_NAME "VirtIO SoundCard"
108 @@ -29,15 +31,20 @@ struct virtio_snd_queue {
109 * @vdev: Underlying virtio device.
110 * @queues: Virtqueue wrappers.
111 * @card: ALSA sound card.
112 + * @ctl_msgs: Pending control request list.
113 * @event_msgs: Device events.
116 struct virtio_device *vdev;
117 struct virtio_snd_queue queues[VIRTIO_SND_VQ_MAX];
118 struct snd_card *card;
119 + struct list_head ctl_msgs;
120 struct virtio_snd_event *event_msgs;
123 +/* Message completion timeout in milliseconds (module parameter). */
124 +extern u32 virtsnd_msg_timeout_ms;
126 static inline struct virtio_snd_queue *
127 virtsnd_control_queue(struct virtio_snd *snd)
129 diff --git a/sound/virtio/virtio_ctl_msg.c b/sound/virtio/virtio_ctl_msg.c
131 index 000000000000..26ff7e7cc041
133 +++ b/sound/virtio/virtio_ctl_msg.c
135 +// SPDX-License-Identifier: GPL-2.0+
137 + * virtio-snd: Virtio sound device
138 + * Copyright (C) 2021 OpenSynergy GmbH
140 +#include <linux/moduleparam.h>
141 +#include <linux/virtio_config.h>
143 +#include "virtio_card.h"
146 + * struct virtio_snd_msg - Control message.
147 + * @sg_request: Scattergather list containing a device request (header).
148 + * @sg_response: Scattergather list containing a device response (status).
149 + * @list: Pending message list entry.
150 + * @notify: Request completed notification.
151 + * @ref_count: Reference count used to manage a message lifetime.
153 +struct virtio_snd_msg {
154 + struct scatterlist sg_request;
155 + struct scatterlist sg_response;
156 + struct list_head list;
157 + struct completion notify;
158 + refcount_t ref_count;
162 + * virtsnd_ctl_msg_ref() - Increment reference counter for the message.
163 + * @msg: Control message.
165 + * Context: Any context.
167 +void virtsnd_ctl_msg_ref(struct virtio_snd_msg *msg)
169 + refcount_inc(&msg->ref_count);
173 + * virtsnd_ctl_msg_unref() - Decrement reference counter for the message.
174 + * @msg: Control message.
176 + * The message will be freed when the ref_count value is 0.
178 + * Context: Any context.
180 +void virtsnd_ctl_msg_unref(struct virtio_snd_msg *msg)
182 + if (refcount_dec_and_test(&msg->ref_count))
187 + * virtsnd_ctl_msg_request() - Get a pointer to the request header.
188 + * @msg: Control message.
190 + * Context: Any context.
192 +void *virtsnd_ctl_msg_request(struct virtio_snd_msg *msg)
194 + return sg_virt(&msg->sg_request);
198 + * virtsnd_ctl_msg_request() - Get a pointer to the response header.
199 + * @msg: Control message.
201 + * Context: Any context.
203 +void *virtsnd_ctl_msg_response(struct virtio_snd_msg *msg)
205 + return sg_virt(&msg->sg_response);
209 + * virtsnd_ctl_msg_alloc() - Allocate and initialize a control message.
210 + * @request_size: Size of request header.
211 + * @response_size: Size of response header.
212 + * @gfp: Kernel flags for memory allocation.
214 + * The message will be automatically freed when the ref_count value is 0.
216 + * Context: Any context. May sleep if @gfp flags permit.
217 + * Return: Allocated message on success, NULL on failure.
219 +struct virtio_snd_msg *virtsnd_ctl_msg_alloc(size_t request_size,
220 + size_t response_size, gfp_t gfp)
222 + struct virtio_snd_msg *msg;
224 + if (!request_size || !response_size)
227 + msg = kzalloc(sizeof(*msg) + request_size + response_size, gfp);
231 + sg_init_one(&msg->sg_request, (u8 *)msg + sizeof(*msg), request_size);
232 + sg_init_one(&msg->sg_response, (u8 *)msg + sizeof(*msg) + request_size,
235 + INIT_LIST_HEAD(&msg->list);
236 + init_completion(&msg->notify);
237 + /* This reference is dropped in virtsnd_ctl_msg_complete(). */
238 + refcount_set(&msg->ref_count, 1);
244 + * virtsnd_ctl_msg_send() - Send a control message.
245 + * @snd: VirtIO sound device.
246 + * @msg: Control message.
247 + * @out_sgs: Additional sg-list to attach to the request header (may be NULL).
248 + * @in_sgs: Additional sg-list to attach to the response header (may be NULL).
249 + * @nowait: Flag indicating whether to wait for completion.
251 + * Context: Any context. Takes and releases the control queue spinlock.
252 + * May sleep if @nowait is false.
253 + * Return: 0 on success, -errno on failure.
255 +int virtsnd_ctl_msg_send(struct virtio_snd *snd, struct virtio_snd_msg *msg,
256 + struct scatterlist *out_sgs,
257 + struct scatterlist *in_sgs, bool nowait)
259 + struct virtio_device *vdev = snd->vdev;
260 + struct virtio_snd_queue *queue = virtsnd_control_queue(snd);
261 + unsigned int js = msecs_to_jiffies(virtsnd_msg_timeout_ms);
262 + struct virtio_snd_hdr *request = virtsnd_ctl_msg_request(msg);
263 + struct virtio_snd_hdr *response = virtsnd_ctl_msg_response(msg);
264 + unsigned int nouts = 0;
265 + unsigned int nins = 0;
266 + struct scatterlist *psgs[4];
267 + bool notify = false;
268 + unsigned long flags;
271 + virtsnd_ctl_msg_ref(msg);
273 + /* Set the default status in case the message was canceled. */
274 + response->code = cpu_to_le32(VIRTIO_SND_S_IO_ERR);
276 + psgs[nouts++] = &msg->sg_request;
278 + psgs[nouts++] = out_sgs;
280 + psgs[nouts + nins++] = &msg->sg_response;
282 + psgs[nouts + nins++] = in_sgs;
284 + spin_lock_irqsave(&queue->lock, flags);
285 + rc = virtqueue_add_sgs(queue->vqueue, psgs, nouts, nins, msg,
288 + notify = virtqueue_kick_prepare(queue->vqueue);
290 + list_add_tail(&msg->list, &snd->ctl_msgs);
292 + spin_unlock_irqrestore(&queue->lock, flags);
295 + dev_err(&vdev->dev, "failed to send control message (0x%08x)\n",
296 + le32_to_cpu(request->code));
299 + * Since in this case virtsnd_ctl_msg_complete() will not be
300 + * called, it is necessary to decrement the reference count.
302 + virtsnd_ctl_msg_unref(msg);
308 + virtqueue_notify(queue->vqueue);
313 + rc = wait_for_completion_interruptible_timeout(&msg->notify, js);
316 + dev_err(&vdev->dev,
317 + "control message (0x%08x) timeout\n",
318 + le32_to_cpu(request->code));
325 + switch (le32_to_cpu(response->code)) {
326 + case VIRTIO_SND_S_OK:
329 + case VIRTIO_SND_S_NOT_SUPP:
332 + case VIRTIO_SND_S_IO_ERR:
341 + virtsnd_ctl_msg_unref(msg);
347 + * virtsnd_ctl_msg_complete() - Complete a control message.
348 + * @msg: Control message.
350 + * Context: Any context. Expects the control queue spinlock to be held by
353 +void virtsnd_ctl_msg_complete(struct virtio_snd_msg *msg)
355 + list_del(&msg->list);
356 + complete(&msg->notify);
358 + virtsnd_ctl_msg_unref(msg);
362 + * virtsnd_ctl_msg_cancel_all() - Cancel all pending control messages.
363 + * @snd: VirtIO sound device.
365 + * Context: Any context.
367 +void virtsnd_ctl_msg_cancel_all(struct virtio_snd *snd)
369 + struct virtio_snd_queue *queue = virtsnd_control_queue(snd);
370 + unsigned long flags;
372 + spin_lock_irqsave(&queue->lock, flags);
373 + while (!list_empty(&snd->ctl_msgs)) {
374 + struct virtio_snd_msg *msg =
375 + list_first_entry(&snd->ctl_msgs, struct virtio_snd_msg,
378 + virtsnd_ctl_msg_complete(msg);
380 + spin_unlock_irqrestore(&queue->lock, flags);
384 + * virtsnd_ctl_query_info() - Query the item configuration from the device.
385 + * @snd: VirtIO sound device.
386 + * @command: Control request code (VIRTIO_SND_R_XXX_INFO).
387 + * @start_id: Item start identifier.
388 + * @count: Item count to query.
389 + * @size: Item information size in bytes.
390 + * @info: Buffer for storing item information.
392 + * Context: Any context that permits to sleep.
393 + * Return: 0 on success, -errno on failure.
395 +int virtsnd_ctl_query_info(struct virtio_snd *snd, int command, int start_id,
396 + int count, size_t size, void *info)
398 + struct virtio_snd_msg *msg;
399 + struct virtio_snd_query_info *query;
400 + struct scatterlist sg;
402 + msg = virtsnd_ctl_msg_alloc(sizeof(*query),
403 + sizeof(struct virtio_snd_hdr), GFP_KERNEL);
407 + query = virtsnd_ctl_msg_request(msg);
408 + query->hdr.code = cpu_to_le32(command);
409 + query->start_id = cpu_to_le32(start_id);
410 + query->count = cpu_to_le32(count);
411 + query->size = cpu_to_le32(size);
413 + sg_init_one(&sg, info, count * size);
415 + return virtsnd_ctl_msg_send(snd, msg, NULL, &sg, false);
419 + * virtsnd_ctl_notify_cb() - Process all completed control messages.
420 + * @vqueue: Underlying control virtqueue.
422 + * This callback function is called upon a vring interrupt request from the
425 + * Context: Interrupt context. Takes and releases the control queue spinlock.
427 +void virtsnd_ctl_notify_cb(struct virtqueue *vqueue)
429 + struct virtio_snd *snd = vqueue->vdev->priv;
430 + struct virtio_snd_queue *queue = virtsnd_control_queue(snd);
431 + struct virtio_snd_msg *msg;
433 + unsigned long flags;
435 + spin_lock_irqsave(&queue->lock, flags);
437 + virtqueue_disable_cb(vqueue);
438 + while ((msg = virtqueue_get_buf(vqueue, &length)))
439 + virtsnd_ctl_msg_complete(msg);
440 + if (unlikely(virtqueue_is_broken(vqueue)))
442 + } while (!virtqueue_enable_cb(vqueue));
443 + spin_unlock_irqrestore(&queue->lock, flags);
445 diff --git a/sound/virtio/virtio_ctl_msg.h b/sound/virtio/virtio_ctl_msg.h
447 index 000000000000..7f4db044f28e
449 +++ b/sound/virtio/virtio_ctl_msg.h
451 +/* SPDX-License-Identifier: GPL-2.0+ */
453 + * virtio-snd: Virtio sound device
454 + * Copyright (C) 2021 OpenSynergy GmbH
456 +#ifndef VIRTIO_SND_MSG_H
457 +#define VIRTIO_SND_MSG_H
459 +#include <linux/atomic.h>
460 +#include <linux/virtio.h>
463 +struct virtio_snd_msg;
465 +void virtsnd_ctl_msg_ref(struct virtio_snd_msg *msg);
467 +void virtsnd_ctl_msg_unref(struct virtio_snd_msg *msg);
469 +void *virtsnd_ctl_msg_request(struct virtio_snd_msg *msg);
471 +void *virtsnd_ctl_msg_response(struct virtio_snd_msg *msg);
473 +struct virtio_snd_msg *virtsnd_ctl_msg_alloc(size_t request_size,
474 + size_t response_size, gfp_t gfp);
476 +int virtsnd_ctl_msg_send(struct virtio_snd *snd, struct virtio_snd_msg *msg,
477 + struct scatterlist *out_sgs,
478 + struct scatterlist *in_sgs, bool nowait);
481 + * virtsnd_ctl_msg_send_sync() - Simplified sending of synchronous message.
482 + * @snd: VirtIO sound device.
483 + * @msg: Control message.
485 + * After returning from this function, the message will be deleted. If message
486 + * content is still needed, the caller must additionally to
487 + * virtsnd_ctl_msg_ref/unref() it.
489 + * The msg_timeout_ms module parameter defines the message completion timeout.
490 + * If the message is not completed within this time, the function will return an
493 + * Context: Any context that permits to sleep.
494 + * Return: 0 on success, -errno on failure.
496 + * The return value is a message status code (VIRTIO_SND_S_XXX) converted to an
497 + * appropriate -errno value.
499 +static inline int virtsnd_ctl_msg_send_sync(struct virtio_snd *snd,
500 + struct virtio_snd_msg *msg)
502 + return virtsnd_ctl_msg_send(snd, msg, NULL, NULL, false);
506 + * virtsnd_ctl_msg_send_async() - Simplified sending of asynchronous message.
507 + * @snd: VirtIO sound device.
508 + * @msg: Control message.
510 + * Context: Any context.
511 + * Return: 0 on success, -errno on failure.
513 +static inline int virtsnd_ctl_msg_send_async(struct virtio_snd *snd,
514 + struct virtio_snd_msg *msg)
516 + return virtsnd_ctl_msg_send(snd, msg, NULL, NULL, true);
519 +void virtsnd_ctl_msg_cancel_all(struct virtio_snd *snd);
521 +void virtsnd_ctl_msg_complete(struct virtio_snd_msg *msg);
523 +int virtsnd_ctl_query_info(struct virtio_snd *snd, int command, int start_id,
524 + int count, size_t size, void *info);
526 +void virtsnd_ctl_notify_cb(struct virtqueue *vqueue);
528 +#endif /* VIRTIO_SND_MSG_H */