1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Virtio vhost-user driver
5 * Copyright(c) 2019 Intel Corporation
7 * This driver allows virtio devices to be used over a vhost-user socket.
9 * Guest devices can be instantiated by kernel module or command line
10 * parameters. One device will be created for each parameter. Syntax:
12 * virtio_uml.device=<socket>:<virtio_id>[:<platform_id>]
14 * <socket> := vhost-user socket path to connect
15 * <virtio_id> := virtio device id (as in virtio_ids.h)
16 * <platform_id> := (optional) platform device id
19 * virtio_uml.device=/var/uml.socket:1
21 * Based on Virtio MMIO driver by Pawel Moll, copyright 2011-2014, ARM Ltd.
23 #include <linux/module.h>
25 #include <linux/platform_device.h>
26 #include <linux/slab.h>
27 #include <linux/virtio.h>
28 #include <linux/virtio_config.h>
29 #include <linux/virtio_ring.h>
30 #include <linux/time-internal.h>
31 #include <linux/virtio-uml.h>
32 #include <shared/as-layout.h>
36 #include "vhost_user.h"
38 #define MAX_SUPPORTED_QUEUE_SIZE 256
40 #define to_virtio_uml_device(_vdev) \
41 container_of(_vdev, struct virtio_uml_device, vdev)
43 struct virtio_uml_platform_data
{
45 const char *socket_path
;
46 struct work_struct conn_broken_wk
;
47 struct platform_device
*pdev
;
50 struct virtio_uml_device
{
51 struct virtio_device vdev
;
52 struct platform_device
*pdev
;
53 struct virtio_uml_platform_data
*pdata
;
56 int sock
, req_fd
, irq
;
58 u64 protocol_features
;
65 u8 config_changed_irq
:1;
66 uint64_t vq_irq_vq_map
;
70 struct virtio_uml_vq_info
{
76 #define vu_err(vu_dev, ...) dev_err(&(vu_dev)->pdev->dev, ##__VA_ARGS__)
78 /* Vhost-user protocol */
80 static int full_sendmsg_fds(int fd
, const void *buf
, unsigned int len
,
81 const int *fds
, unsigned int fds_num
)
86 rc
= os_sendmsg_fds(fd
, buf
, len
, fds
, fds_num
);
93 } while (len
&& (rc
>= 0 || rc
== -EINTR
));
100 static int full_read(int fd
, void *buf
, int len
, bool abortable
)
108 rc
= os_read_file(fd
, buf
, len
);
113 } while (len
&& (rc
> 0 || rc
== -EINTR
|| (!abortable
&& rc
== -EAGAIN
)));
122 static int vhost_user_recv_header(int fd
, struct vhost_user_msg
*msg
)
124 return full_read(fd
, msg
, sizeof(msg
->header
), true);
127 static int vhost_user_recv(struct virtio_uml_device
*vu_dev
,
128 int fd
, struct vhost_user_msg
*msg
,
129 size_t max_payload_size
, bool wait
)
135 * In virtio time-travel mode, we're handling all the vhost-user
136 * FDs by polling them whenever appropriate. However, we may get
137 * into a situation where we're sending out an interrupt message
138 * to a device (e.g. a net device) and need to handle a simulation
139 * time message while doing so, e.g. one that tells us to update
140 * our idea of how long we can run without scheduling.
142 * Thus, we need to not just read() from the given fd, but need
143 * to also handle messages for the simulation time - this function
144 * does that for us while waiting for the given fd to be readable.
147 time_travel_wait_readable(fd
);
149 rc
= vhost_user_recv_header(fd
, msg
);
153 size
= msg
->header
.size
;
154 if (size
> max_payload_size
)
156 return full_read(fd
, &msg
->payload
, size
, false);
159 static void vhost_user_check_reset(struct virtio_uml_device
*vu_dev
,
162 struct virtio_uml_platform_data
*pdata
= vu_dev
->pdata
;
164 if (rc
!= -ECONNRESET
)
167 if (!vu_dev
->registered
)
170 vu_dev
->registered
= 0;
172 schedule_work(&pdata
->conn_broken_wk
);
175 static int vhost_user_recv_resp(struct virtio_uml_device
*vu_dev
,
176 struct vhost_user_msg
*msg
,
177 size_t max_payload_size
)
179 int rc
= vhost_user_recv(vu_dev
, vu_dev
->sock
, msg
,
180 max_payload_size
, true);
183 vhost_user_check_reset(vu_dev
, rc
);
187 if (msg
->header
.flags
!= (VHOST_USER_FLAG_REPLY
| VHOST_USER_VERSION
))
193 static int vhost_user_recv_u64(struct virtio_uml_device
*vu_dev
,
196 struct vhost_user_msg msg
;
197 int rc
= vhost_user_recv_resp(vu_dev
, &msg
,
198 sizeof(msg
.payload
.integer
));
202 if (msg
.header
.size
!= sizeof(msg
.payload
.integer
))
204 *value
= msg
.payload
.integer
;
208 static int vhost_user_recv_req(struct virtio_uml_device
*vu_dev
,
209 struct vhost_user_msg
*msg
,
210 size_t max_payload_size
)
212 int rc
= vhost_user_recv(vu_dev
, vu_dev
->req_fd
, msg
,
213 max_payload_size
, false);
218 if ((msg
->header
.flags
& ~VHOST_USER_FLAG_NEED_REPLY
) !=
225 static int vhost_user_send(struct virtio_uml_device
*vu_dev
,
226 bool need_response
, struct vhost_user_msg
*msg
,
227 int *fds
, size_t num_fds
)
229 size_t size
= sizeof(msg
->header
) + msg
->header
.size
;
234 msg
->header
.flags
|= VHOST_USER_VERSION
;
237 * The need_response flag indicates that we already need a response,
238 * e.g. to read the features. In these cases, don't request an ACK as
239 * it is meaningless. Also request an ACK only if supported.
241 request_ack
= !need_response
;
242 if (!(vu_dev
->protocol_features
&
243 BIT_ULL(VHOST_USER_PROTOCOL_F_REPLY_ACK
)))
247 msg
->header
.flags
|= VHOST_USER_FLAG_NEED_REPLY
;
249 spin_lock_irqsave(&vu_dev
->sock_lock
, flags
);
250 rc
= full_sendmsg_fds(vu_dev
->sock
, msg
, size
, fds
, num_fds
);
257 rc
= vhost_user_recv_u64(vu_dev
, &status
);
262 vu_err(vu_dev
, "slave reports error: %llu\n", status
);
269 spin_unlock_irqrestore(&vu_dev
->sock_lock
, flags
);
273 static int vhost_user_send_no_payload(struct virtio_uml_device
*vu_dev
,
274 bool need_response
, u32 request
)
276 struct vhost_user_msg msg
= {
277 .header
.request
= request
,
280 return vhost_user_send(vu_dev
, need_response
, &msg
, NULL
, 0);
283 static int vhost_user_send_no_payload_fd(struct virtio_uml_device
*vu_dev
,
286 struct vhost_user_msg msg
= {
287 .header
.request
= request
,
290 return vhost_user_send(vu_dev
, false, &msg
, &fd
, 1);
293 static int vhost_user_send_u64(struct virtio_uml_device
*vu_dev
,
294 u32 request
, u64 value
)
296 struct vhost_user_msg msg
= {
297 .header
.request
= request
,
298 .header
.size
= sizeof(msg
.payload
.integer
),
299 .payload
.integer
= value
,
302 return vhost_user_send(vu_dev
, false, &msg
, NULL
, 0);
305 static int vhost_user_set_owner(struct virtio_uml_device
*vu_dev
)
307 return vhost_user_send_no_payload(vu_dev
, false, VHOST_USER_SET_OWNER
);
310 static int vhost_user_get_features(struct virtio_uml_device
*vu_dev
,
313 int rc
= vhost_user_send_no_payload(vu_dev
, true,
314 VHOST_USER_GET_FEATURES
);
318 return vhost_user_recv_u64(vu_dev
, features
);
321 static int vhost_user_set_features(struct virtio_uml_device
*vu_dev
,
324 return vhost_user_send_u64(vu_dev
, VHOST_USER_SET_FEATURES
, features
);
327 static int vhost_user_get_protocol_features(struct virtio_uml_device
*vu_dev
,
328 u64
*protocol_features
)
330 int rc
= vhost_user_send_no_payload(vu_dev
, true,
331 VHOST_USER_GET_PROTOCOL_FEATURES
);
335 return vhost_user_recv_u64(vu_dev
, protocol_features
);
338 static int vhost_user_set_protocol_features(struct virtio_uml_device
*vu_dev
,
339 u64 protocol_features
)
341 return vhost_user_send_u64(vu_dev
, VHOST_USER_SET_PROTOCOL_FEATURES
,
345 static int vhost_user_get_queue_num(struct virtio_uml_device
*vu_dev
,
348 int rc
= vhost_user_send_no_payload(vu_dev
, true,
349 VHOST_USER_GET_QUEUE_NUM
);
353 return vhost_user_recv_u64(vu_dev
, queue_num
);
356 static void vhost_user_reply(struct virtio_uml_device
*vu_dev
,
357 struct vhost_user_msg
*msg
, int response
)
359 struct vhost_user_msg reply
= {
360 .payload
.integer
= response
,
362 size_t size
= sizeof(reply
.header
) + sizeof(reply
.payload
.integer
);
365 reply
.header
= msg
->header
;
366 reply
.header
.flags
&= ~VHOST_USER_FLAG_NEED_REPLY
;
367 reply
.header
.flags
|= VHOST_USER_FLAG_REPLY
;
368 reply
.header
.size
= sizeof(reply
.payload
.integer
);
370 rc
= full_sendmsg_fds(vu_dev
->req_fd
, &reply
, size
, NULL
, 0);
374 "sending reply to slave request failed: %d (size %zu)\n",
378 static irqreturn_t
vu_req_read_message(struct virtio_uml_device
*vu_dev
,
379 struct time_travel_event
*ev
)
381 struct virtqueue
*vq
;
384 struct vhost_user_msg msg
;
385 u8 extra_payload
[512];
388 irqreturn_t irq_rc
= IRQ_NONE
;
391 rc
= vhost_user_recv_req(vu_dev
, &msg
.msg
,
392 sizeof(msg
.msg
.payload
) +
393 sizeof(msg
.extra_payload
));
397 switch (msg
.msg
.header
.request
) {
398 case VHOST_USER_SLAVE_CONFIG_CHANGE_MSG
:
399 vu_dev
->config_changed_irq
= true;
402 case VHOST_USER_SLAVE_VRING_CALL
:
403 virtio_device_for_each_vq((&vu_dev
->vdev
), vq
) {
404 if (vq
->index
== msg
.msg
.payload
.vring_state
.index
) {
406 vu_dev
->vq_irq_vq_map
|= BIT_ULL(vq
->index
);
411 case VHOST_USER_SLAVE_IOTLB_MSG
:
412 /* not supported - VIRTIO_F_ACCESS_PLATFORM */
413 case VHOST_USER_SLAVE_VRING_HOST_NOTIFIER_MSG
:
414 /* not supported - VHOST_USER_PROTOCOL_F_HOST_NOTIFIER */
416 vu_err(vu_dev
, "unexpected slave request %d\n",
417 msg
.msg
.header
.request
);
420 if (ev
&& !vu_dev
->suspended
)
421 time_travel_add_irq_event(ev
);
423 if (msg
.msg
.header
.flags
& VHOST_USER_FLAG_NEED_REPLY
)
424 vhost_user_reply(vu_dev
, &msg
.msg
, response
);
425 irq_rc
= IRQ_HANDLED
;
427 /* mask EAGAIN as we try non-blocking read until socket is empty */
428 vu_dev
->recv_rc
= (rc
== -EAGAIN
) ? 0 : rc
;
432 static irqreturn_t
vu_req_interrupt(int irq
, void *data
)
434 struct virtio_uml_device
*vu_dev
= data
;
435 irqreturn_t ret
= IRQ_HANDLED
;
437 if (!um_irq_timetravel_handler_used())
438 ret
= vu_req_read_message(vu_dev
, NULL
);
440 if (vu_dev
->recv_rc
) {
441 vhost_user_check_reset(vu_dev
, vu_dev
->recv_rc
);
442 } else if (vu_dev
->vq_irq_vq_map
) {
443 struct virtqueue
*vq
;
445 virtio_device_for_each_vq((&vu_dev
->vdev
), vq
) {
446 if (vu_dev
->vq_irq_vq_map
& BIT_ULL(vq
->index
))
447 vring_interrupt(0 /* ignored */, vq
);
449 vu_dev
->vq_irq_vq_map
= 0;
450 } else if (vu_dev
->config_changed_irq
) {
451 virtio_config_changed(&vu_dev
->vdev
);
452 vu_dev
->config_changed_irq
= false;
458 static void vu_req_interrupt_comm_handler(int irq
, int fd
, void *data
,
459 struct time_travel_event
*ev
)
461 vu_req_read_message(data
, ev
);
464 static int vhost_user_init_slave_req(struct virtio_uml_device
*vu_dev
)
468 /* Use a pipe for slave req fd, SIGIO is not supported for eventfd */
469 rc
= os_pipe(req_fds
, true, true);
472 vu_dev
->req_fd
= req_fds
[0];
474 rc
= um_request_irq_tt(UM_IRQ_ALLOC
, vu_dev
->req_fd
, IRQ_READ
,
475 vu_req_interrupt
, IRQF_SHARED
,
476 vu_dev
->pdev
->name
, vu_dev
,
477 vu_req_interrupt_comm_handler
);
483 rc
= vhost_user_send_no_payload_fd(vu_dev
, VHOST_USER_SET_SLAVE_REQ_FD
,
491 um_free_irq(vu_dev
->irq
, vu_dev
);
493 os_close_file(req_fds
[0]);
495 /* Close unused write end of request fds */
496 os_close_file(req_fds
[1]);
500 static int vhost_user_init(struct virtio_uml_device
*vu_dev
)
502 int rc
= vhost_user_set_owner(vu_dev
);
506 rc
= vhost_user_get_features(vu_dev
, &vu_dev
->features
);
510 if (vu_dev
->features
& BIT_ULL(VHOST_USER_F_PROTOCOL_FEATURES
)) {
511 rc
= vhost_user_get_protocol_features(vu_dev
,
512 &vu_dev
->protocol_features
);
515 vu_dev
->protocol_features
&= VHOST_USER_SUPPORTED_PROTOCOL_F
;
516 rc
= vhost_user_set_protocol_features(vu_dev
,
517 vu_dev
->protocol_features
);
522 if (vu_dev
->protocol_features
&
523 BIT_ULL(VHOST_USER_PROTOCOL_F_SLAVE_REQ
)) {
524 rc
= vhost_user_init_slave_req(vu_dev
);
529 if (vu_dev
->protocol_features
&
530 BIT_ULL(VHOST_USER_PROTOCOL_F_MQ
)) {
531 rc
= vhost_user_get_queue_num(vu_dev
, &vu_dev
->max_vqs
);
535 vu_dev
->max_vqs
= U64_MAX
;
541 static void vhost_user_get_config(struct virtio_uml_device
*vu_dev
,
542 u32 offset
, void *buf
, u32 len
)
544 u32 cfg_size
= offset
+ len
;
545 struct vhost_user_msg
*msg
;
546 size_t payload_size
= sizeof(msg
->payload
.config
) + cfg_size
;
547 size_t msg_size
= sizeof(msg
->header
) + payload_size
;
550 if (!(vu_dev
->protocol_features
&
551 BIT_ULL(VHOST_USER_PROTOCOL_F_CONFIG
)))
554 msg
= kzalloc(msg_size
, GFP_KERNEL
);
557 msg
->header
.request
= VHOST_USER_GET_CONFIG
;
558 msg
->header
.size
= payload_size
;
559 msg
->payload
.config
.offset
= 0;
560 msg
->payload
.config
.size
= cfg_size
;
562 rc
= vhost_user_send(vu_dev
, true, msg
, NULL
, 0);
564 vu_err(vu_dev
, "sending VHOST_USER_GET_CONFIG failed: %d\n",
569 rc
= vhost_user_recv_resp(vu_dev
, msg
, msg_size
);
572 "receiving VHOST_USER_GET_CONFIG response failed: %d\n",
577 if (msg
->header
.size
!= payload_size
||
578 msg
->payload
.config
.size
!= cfg_size
) {
581 "Invalid VHOST_USER_GET_CONFIG sizes (payload %d expected %zu, config %u expected %u)\n",
582 msg
->header
.size
, payload_size
,
583 msg
->payload
.config
.size
, cfg_size
);
586 memcpy(buf
, msg
->payload
.config
.payload
+ offset
, len
);
592 static void vhost_user_set_config(struct virtio_uml_device
*vu_dev
,
593 u32 offset
, const void *buf
, u32 len
)
595 struct vhost_user_msg
*msg
;
596 size_t payload_size
= sizeof(msg
->payload
.config
) + len
;
597 size_t msg_size
= sizeof(msg
->header
) + payload_size
;
600 if (!(vu_dev
->protocol_features
&
601 BIT_ULL(VHOST_USER_PROTOCOL_F_CONFIG
)))
604 msg
= kzalloc(msg_size
, GFP_KERNEL
);
607 msg
->header
.request
= VHOST_USER_SET_CONFIG
;
608 msg
->header
.size
= payload_size
;
609 msg
->payload
.config
.offset
= offset
;
610 msg
->payload
.config
.size
= len
;
611 memcpy(msg
->payload
.config
.payload
, buf
, len
);
613 rc
= vhost_user_send(vu_dev
, false, msg
, NULL
, 0);
615 vu_err(vu_dev
, "sending VHOST_USER_SET_CONFIG failed: %d\n",
621 static int vhost_user_init_mem_region(u64 addr
, u64 size
, int *fd_out
,
622 struct vhost_user_mem_region
*region_out
)
624 unsigned long long mem_offset
;
625 int rc
= phys_mapping(addr
, &mem_offset
);
627 if (WARN(rc
< 0, "phys_mapping of 0x%llx returned %d\n", addr
, rc
))
630 region_out
->guest_addr
= addr
;
631 region_out
->user_addr
= addr
;
632 region_out
->size
= size
;
633 region_out
->mmap_offset
= mem_offset
;
635 /* Ensure mapping is valid for the entire region */
636 rc
= phys_mapping(addr
+ size
- 1, &mem_offset
);
637 if (WARN(rc
!= *fd_out
, "phys_mapping of 0x%llx failed: %d != %d\n",
638 addr
+ size
- 1, rc
, *fd_out
))
643 static int vhost_user_set_mem_table(struct virtio_uml_device
*vu_dev
)
645 struct vhost_user_msg msg
= {
646 .header
.request
= VHOST_USER_SET_MEM_TABLE
,
647 .header
.size
= offsetof(typeof(msg
.payload
.mem_regions
), regions
[1]),
648 .payload
.mem_regions
.num
= 1,
650 unsigned long reserved
= uml_reserved
- uml_physmem
;
655 * This is a bit tricky, see also the comment with setup_physmem().
657 * Essentially, setup_physmem() uses a file to mmap() our physmem,
658 * but the code and data we *already* have is omitted. To us, this
659 * is no difference, since they both become part of our address
660 * space and memory consumption. To somebody looking in from the
661 * outside, however, it is different because the part of our memory
662 * consumption that's already part of the binary (code/data) is not
663 * mapped from the file, so it's not visible to another mmap from
664 * the file descriptor.
666 * Thus, don't advertise this space to the vhost-user slave. This
667 * means that the slave will likely abort or similar when we give
668 * it an address from the hidden range, since it's not marked as
669 * a valid address, but at least that way we detect the issue and
670 * don't just have the slave read an all-zeroes buffer from the
671 * shared memory file, or write something there that we can never
672 * see (depending on the direction of the virtqueue traffic.)
674 * Since we usually don't want to use .text for virtio buffers,
675 * this effectively means that you cannot use
676 * 1) global variables, which are in the .bss and not in the shm
678 * 2) the stack in some processes, depending on where they have
679 * their stack (or maybe only no interrupt stack?)
681 * The stack is already not typically valid for DMA, so this isn't
682 * much of a restriction, but global variables might be encountered.
684 * It might be possible to fix it by copying around the data that's
685 * between bss_start and where we map the file now, but it's not
686 * something that you typically encounter with virtio drivers, so
687 * it didn't seem worthwhile.
689 rc
= vhost_user_init_mem_region(reserved
, physmem_size
- reserved
,
691 &msg
.payload
.mem_regions
.regions
[0]);
696 return vhost_user_send(vu_dev
, false, &msg
, fds
,
697 msg
.payload
.mem_regions
.num
);
700 static int vhost_user_set_vring_state(struct virtio_uml_device
*vu_dev
,
701 u32 request
, u32 index
, u32 num
)
703 struct vhost_user_msg msg
= {
704 .header
.request
= request
,
705 .header
.size
= sizeof(msg
.payload
.vring_state
),
706 .payload
.vring_state
.index
= index
,
707 .payload
.vring_state
.num
= num
,
710 return vhost_user_send(vu_dev
, false, &msg
, NULL
, 0);
713 static int vhost_user_set_vring_num(struct virtio_uml_device
*vu_dev
,
716 return vhost_user_set_vring_state(vu_dev
, VHOST_USER_SET_VRING_NUM
,
720 static int vhost_user_set_vring_base(struct virtio_uml_device
*vu_dev
,
721 u32 index
, u32 offset
)
723 return vhost_user_set_vring_state(vu_dev
, VHOST_USER_SET_VRING_BASE
,
727 static int vhost_user_set_vring_addr(struct virtio_uml_device
*vu_dev
,
728 u32 index
, u64 desc
, u64 used
, u64 avail
,
731 struct vhost_user_msg msg
= {
732 .header
.request
= VHOST_USER_SET_VRING_ADDR
,
733 .header
.size
= sizeof(msg
.payload
.vring_addr
),
734 .payload
.vring_addr
.index
= index
,
735 .payload
.vring_addr
.desc
= desc
,
736 .payload
.vring_addr
.used
= used
,
737 .payload
.vring_addr
.avail
= avail
,
738 .payload
.vring_addr
.log
= log
,
741 return vhost_user_send(vu_dev
, false, &msg
, NULL
, 0);
744 static int vhost_user_set_vring_fd(struct virtio_uml_device
*vu_dev
,
745 u32 request
, int index
, int fd
)
747 struct vhost_user_msg msg
= {
748 .header
.request
= request
,
749 .header
.size
= sizeof(msg
.payload
.integer
),
750 .payload
.integer
= index
,
753 if (index
& ~VHOST_USER_VRING_INDEX_MASK
)
756 msg
.payload
.integer
|= VHOST_USER_VRING_POLL_MASK
;
757 return vhost_user_send(vu_dev
, false, &msg
, NULL
, 0);
759 return vhost_user_send(vu_dev
, false, &msg
, &fd
, 1);
762 static int vhost_user_set_vring_call(struct virtio_uml_device
*vu_dev
,
765 return vhost_user_set_vring_fd(vu_dev
, VHOST_USER_SET_VRING_CALL
,
769 static int vhost_user_set_vring_kick(struct virtio_uml_device
*vu_dev
,
772 return vhost_user_set_vring_fd(vu_dev
, VHOST_USER_SET_VRING_KICK
,
776 static int vhost_user_set_vring_enable(struct virtio_uml_device
*vu_dev
,
777 u32 index
, bool enable
)
779 if (!(vu_dev
->features
& BIT_ULL(VHOST_USER_F_PROTOCOL_FEATURES
)))
782 return vhost_user_set_vring_state(vu_dev
, VHOST_USER_SET_VRING_ENABLE
,
787 /* Virtio interface */
789 static bool vu_notify(struct virtqueue
*vq
)
791 struct virtio_uml_vq_info
*info
= vq
->priv
;
792 const uint64_t n
= 1;
798 time_travel_propagate_time();
800 if (info
->kick_fd
< 0) {
801 struct virtio_uml_device
*vu_dev
;
803 vu_dev
= to_virtio_uml_device(vq
->vdev
);
805 return vhost_user_set_vring_state(vu_dev
, VHOST_USER_VRING_KICK
,
810 rc
= os_write_file(info
->kick_fd
, &n
, sizeof(n
));
811 } while (rc
== -EINTR
);
812 return !WARN(rc
!= sizeof(n
), "write returned %d\n", rc
);
815 static irqreturn_t
vu_interrupt(int irq
, void *opaque
)
817 struct virtqueue
*vq
= opaque
;
818 struct virtio_uml_vq_info
*info
= vq
->priv
;
821 irqreturn_t ret
= IRQ_NONE
;
824 rc
= os_read_file(info
->call_fd
, &n
, sizeof(n
));
826 ret
|= vring_interrupt(irq
, vq
);
827 } while (rc
== sizeof(n
) || rc
== -EINTR
);
828 WARN(rc
!= -EAGAIN
, "read returned %d\n", rc
);
833 static void vu_get(struct virtio_device
*vdev
, unsigned offset
,
834 void *buf
, unsigned len
)
836 struct virtio_uml_device
*vu_dev
= to_virtio_uml_device(vdev
);
838 vhost_user_get_config(vu_dev
, offset
, buf
, len
);
841 static void vu_set(struct virtio_device
*vdev
, unsigned offset
,
842 const void *buf
, unsigned len
)
844 struct virtio_uml_device
*vu_dev
= to_virtio_uml_device(vdev
);
846 vhost_user_set_config(vu_dev
, offset
, buf
, len
);
849 static u8
vu_get_status(struct virtio_device
*vdev
)
851 struct virtio_uml_device
*vu_dev
= to_virtio_uml_device(vdev
);
853 return vu_dev
->status
;
856 static void vu_set_status(struct virtio_device
*vdev
, u8 status
)
858 struct virtio_uml_device
*vu_dev
= to_virtio_uml_device(vdev
);
860 vu_dev
->status
= status
;
863 static void vu_reset(struct virtio_device
*vdev
)
865 struct virtio_uml_device
*vu_dev
= to_virtio_uml_device(vdev
);
870 static void vu_del_vq(struct virtqueue
*vq
)
872 struct virtio_uml_vq_info
*info
= vq
->priv
;
874 if (info
->call_fd
>= 0) {
875 struct virtio_uml_device
*vu_dev
;
877 vu_dev
= to_virtio_uml_device(vq
->vdev
);
879 um_free_irq(vu_dev
->irq
, vq
);
880 os_close_file(info
->call_fd
);
883 if (info
->kick_fd
>= 0)
884 os_close_file(info
->kick_fd
);
886 vring_del_virtqueue(vq
);
890 static void vu_del_vqs(struct virtio_device
*vdev
)
892 struct virtio_uml_device
*vu_dev
= to_virtio_uml_device(vdev
);
893 struct virtqueue
*vq
, *n
;
896 /* Note: reverse order as a workaround to a decoding bug in snabb */
897 list_for_each_entry_reverse(vq
, &vdev
->vqs
, list
)
898 WARN_ON(vhost_user_set_vring_enable(vu_dev
, vq
->index
, false));
900 /* Ensure previous messages have been processed */
901 WARN_ON(vhost_user_get_features(vu_dev
, &features
));
903 list_for_each_entry_safe(vq
, n
, &vdev
->vqs
, list
)
907 static int vu_setup_vq_call_fd(struct virtio_uml_device
*vu_dev
,
908 struct virtqueue
*vq
)
910 struct virtio_uml_vq_info
*info
= vq
->priv
;
914 /* no call FD needed/desired in this case */
915 if (vu_dev
->protocol_features
&
916 BIT_ULL(VHOST_USER_PROTOCOL_F_INBAND_NOTIFICATIONS
) &&
917 vu_dev
->protocol_features
&
918 BIT_ULL(VHOST_USER_PROTOCOL_F_SLAVE_REQ
)) {
923 /* Use a pipe for call fd, since SIGIO is not supported for eventfd */
924 rc
= os_pipe(call_fds
, true, true);
928 info
->call_fd
= call_fds
[0];
929 irq
= um_request_irq(vu_dev
->irq
, info
->call_fd
, IRQ_READ
,
930 vu_interrupt
, IRQF_SHARED
, info
->name
, vq
);
936 rc
= vhost_user_set_vring_call(vu_dev
, vq
->index
, call_fds
[1]);
945 um_free_irq(irq
, vq
);
947 os_close_file(call_fds
[0]);
949 /* Close (unused) write end of call fds */
950 os_close_file(call_fds
[1]);
955 static struct virtqueue
*vu_setup_vq(struct virtio_device
*vdev
,
956 unsigned index
, vq_callback_t
*callback
,
957 const char *name
, bool ctx
)
959 struct virtio_uml_device
*vu_dev
= to_virtio_uml_device(vdev
);
960 struct platform_device
*pdev
= vu_dev
->pdev
;
961 struct virtio_uml_vq_info
*info
;
962 struct virtqueue
*vq
;
963 int num
= MAX_SUPPORTED_QUEUE_SIZE
;
966 info
= kzalloc(sizeof(*info
), GFP_KERNEL
);
971 snprintf(info
->name
, sizeof(info
->name
), "%s.%d-%s", pdev
->name
,
974 vq
= vring_create_virtqueue(index
, num
, PAGE_SIZE
, vdev
, true, true,
975 ctx
, vu_notify
, callback
, info
->name
);
982 num
= virtqueue_get_vring_size(vq
);
984 if (vu_dev
->protocol_features
&
985 BIT_ULL(VHOST_USER_PROTOCOL_F_INBAND_NOTIFICATIONS
)) {
988 rc
= os_eventfd(0, 0);
994 rc
= vu_setup_vq_call_fd(vu_dev
, vq
);
998 rc
= vhost_user_set_vring_num(vu_dev
, index
, num
);
1002 rc
= vhost_user_set_vring_base(vu_dev
, index
, 0);
1006 rc
= vhost_user_set_vring_addr(vu_dev
, index
,
1007 virtqueue_get_desc_addr(vq
),
1008 virtqueue_get_used_addr(vq
),
1009 virtqueue_get_avail_addr(vq
),
1017 if (info
->call_fd
>= 0) {
1018 um_free_irq(vu_dev
->irq
, vq
);
1019 os_close_file(info
->call_fd
);
1022 if (info
->kick_fd
>= 0)
1023 os_close_file(info
->kick_fd
);
1025 vring_del_virtqueue(vq
);
1032 static int vu_find_vqs(struct virtio_device
*vdev
, unsigned nvqs
,
1033 struct virtqueue
*vqs
[],
1034 struct virtqueue_info vqs_info
[],
1035 struct irq_affinity
*desc
)
1037 struct virtio_uml_device
*vu_dev
= to_virtio_uml_device(vdev
);
1038 int i
, queue_idx
= 0, rc
;
1039 struct virtqueue
*vq
;
1041 /* not supported for now */
1042 if (WARN(nvqs
> 64 || nvqs
> vu_dev
->max_vqs
,
1043 "%d VQs requested, only up to 64 or %lld supported\n",
1044 nvqs
, vu_dev
->max_vqs
))
1047 rc
= vhost_user_set_mem_table(vu_dev
);
1051 for (i
= 0; i
< nvqs
; ++i
) {
1052 struct virtqueue_info
*vqi
= &vqs_info
[i
];
1059 vqs
[i
] = vu_setup_vq(vdev
, queue_idx
++, vqi
->callback
,
1060 vqi
->name
, vqi
->ctx
);
1061 if (IS_ERR(vqs
[i
])) {
1062 rc
= PTR_ERR(vqs
[i
]);
1067 list_for_each_entry(vq
, &vdev
->vqs
, list
) {
1068 struct virtio_uml_vq_info
*info
= vq
->priv
;
1070 if (info
->kick_fd
>= 0) {
1071 rc
= vhost_user_set_vring_kick(vu_dev
, vq
->index
,
1077 rc
= vhost_user_set_vring_enable(vu_dev
, vq
->index
, true);
1089 static u64
vu_get_features(struct virtio_device
*vdev
)
1091 struct virtio_uml_device
*vu_dev
= to_virtio_uml_device(vdev
);
1093 return vu_dev
->features
;
1096 static int vu_finalize_features(struct virtio_device
*vdev
)
1098 struct virtio_uml_device
*vu_dev
= to_virtio_uml_device(vdev
);
1099 u64 supported
= vdev
->features
& VHOST_USER_SUPPORTED_F
;
1101 vring_transport_features(vdev
);
1102 vu_dev
->features
= vdev
->features
| supported
;
1104 return vhost_user_set_features(vu_dev
, vu_dev
->features
);
1107 static const char *vu_bus_name(struct virtio_device
*vdev
)
1109 struct virtio_uml_device
*vu_dev
= to_virtio_uml_device(vdev
);
1111 return vu_dev
->pdev
->name
;
1114 static const struct virtio_config_ops virtio_uml_config_ops
= {
1117 .get_status
= vu_get_status
,
1118 .set_status
= vu_set_status
,
1120 .find_vqs
= vu_find_vqs
,
1121 .del_vqs
= vu_del_vqs
,
1122 .get_features
= vu_get_features
,
1123 .finalize_features
= vu_finalize_features
,
1124 .bus_name
= vu_bus_name
,
1127 static void virtio_uml_release_dev(struct device
*d
)
1129 struct virtio_device
*vdev
=
1130 container_of(d
, struct virtio_device
, dev
);
1131 struct virtio_uml_device
*vu_dev
= to_virtio_uml_device(vdev
);
1133 time_travel_propagate_time();
1135 /* might not have been opened due to not negotiating the feature */
1136 if (vu_dev
->req_fd
>= 0) {
1137 um_free_irq(vu_dev
->irq
, vu_dev
);
1138 os_close_file(vu_dev
->req_fd
);
1141 os_close_file(vu_dev
->sock
);
1145 void virtio_uml_set_no_vq_suspend(struct virtio_device
*vdev
,
1148 struct virtio_uml_device
*vu_dev
= to_virtio_uml_device(vdev
);
1150 if (WARN_ON(vdev
->config
!= &virtio_uml_config_ops
))
1153 vu_dev
->no_vq_suspend
= no_vq_suspend
;
1154 dev_info(&vdev
->dev
, "%sabled VQ suspend\n",
1155 no_vq_suspend
? "dis" : "en");
1158 static void vu_of_conn_broken(struct work_struct
*wk
)
1160 struct virtio_uml_platform_data
*pdata
;
1161 struct virtio_uml_device
*vu_dev
;
1163 pdata
= container_of(wk
, struct virtio_uml_platform_data
, conn_broken_wk
);
1165 vu_dev
= platform_get_drvdata(pdata
->pdev
);
1167 virtio_break_device(&vu_dev
->vdev
);
1170 * We can't remove the device from the devicetree so the only thing we
1176 /* Platform device */
1178 static struct virtio_uml_platform_data
*
1179 virtio_uml_create_pdata(struct platform_device
*pdev
)
1181 struct device_node
*np
= pdev
->dev
.of_node
;
1182 struct virtio_uml_platform_data
*pdata
;
1186 return ERR_PTR(-EINVAL
);
1188 pdata
= devm_kzalloc(&pdev
->dev
, sizeof(*pdata
), GFP_KERNEL
);
1190 return ERR_PTR(-ENOMEM
);
1192 INIT_WORK(&pdata
->conn_broken_wk
, vu_of_conn_broken
);
1195 ret
= of_property_read_string(np
, "socket-path", &pdata
->socket_path
);
1197 return ERR_PTR(ret
);
1199 ret
= of_property_read_u32(np
, "virtio-device-id",
1200 &pdata
->virtio_device_id
);
1202 return ERR_PTR(ret
);
1207 static int virtio_uml_probe(struct platform_device
*pdev
)
1209 struct virtio_uml_platform_data
*pdata
= pdev
->dev
.platform_data
;
1210 struct virtio_uml_device
*vu_dev
;
1214 pdata
= virtio_uml_create_pdata(pdev
);
1216 return PTR_ERR(pdata
);
1219 vu_dev
= kzalloc(sizeof(*vu_dev
), GFP_KERNEL
);
1223 vu_dev
->pdata
= pdata
;
1224 vu_dev
->vdev
.dev
.parent
= &pdev
->dev
;
1225 vu_dev
->vdev
.dev
.release
= virtio_uml_release_dev
;
1226 vu_dev
->vdev
.config
= &virtio_uml_config_ops
;
1227 vu_dev
->vdev
.id
.device
= pdata
->virtio_device_id
;
1228 vu_dev
->vdev
.id
.vendor
= VIRTIO_DEV_ANY_ID
;
1229 vu_dev
->pdev
= pdev
;
1230 vu_dev
->req_fd
= -1;
1231 vu_dev
->irq
= UM_IRQ_ALLOC
;
1233 time_travel_propagate_time();
1236 rc
= os_connect_socket(pdata
->socket_path
);
1237 } while (rc
== -EINTR
);
1242 spin_lock_init(&vu_dev
->sock_lock
);
1244 rc
= vhost_user_init(vu_dev
);
1248 platform_set_drvdata(pdev
, vu_dev
);
1250 device_set_wakeup_capable(&vu_dev
->vdev
.dev
, true);
1252 rc
= register_virtio_device(&vu_dev
->vdev
);
1254 put_device(&vu_dev
->vdev
.dev
);
1255 vu_dev
->registered
= 1;
1259 os_close_file(vu_dev
->sock
);
1265 static void virtio_uml_remove(struct platform_device
*pdev
)
1267 struct virtio_uml_device
*vu_dev
= platform_get_drvdata(pdev
);
1269 unregister_virtio_device(&vu_dev
->vdev
);
1272 /* Command line device list */
1274 static void vu_cmdline_release_dev(struct device
*d
)
1278 static struct device vu_cmdline_parent
= {
1279 .init_name
= "virtio-uml-cmdline",
1280 .release
= vu_cmdline_release_dev
,
1283 static bool vu_cmdline_parent_registered
;
1284 static int vu_cmdline_id
;
1286 static int vu_unregister_cmdline_device(struct device
*dev
, void *data
)
1288 struct platform_device
*pdev
= to_platform_device(dev
);
1289 struct virtio_uml_platform_data
*pdata
= pdev
->dev
.platform_data
;
1291 kfree(pdata
->socket_path
);
1292 platform_device_unregister(pdev
);
1296 static void vu_conn_broken(struct work_struct
*wk
)
1298 struct virtio_uml_platform_data
*pdata
;
1299 struct virtio_uml_device
*vu_dev
;
1301 pdata
= container_of(wk
, struct virtio_uml_platform_data
, conn_broken_wk
);
1303 vu_dev
= platform_get_drvdata(pdata
->pdev
);
1305 virtio_break_device(&vu_dev
->vdev
);
1307 vu_unregister_cmdline_device(&pdata
->pdev
->dev
, NULL
);
1310 static int vu_cmdline_set(const char *device
, const struct kernel_param
*kp
)
1312 const char *ids
= strchr(device
, ':');
1313 unsigned int virtio_device_id
;
1314 int processed
, consumed
, err
;
1316 struct virtio_uml_platform_data pdata
, *ppdata
;
1317 struct platform_device
*pdev
;
1319 if (!ids
|| ids
== device
)
1322 processed
= sscanf(ids
, ":%u%n:%d%n",
1323 &virtio_device_id
, &consumed
,
1324 &vu_cmdline_id
, &consumed
);
1326 if (processed
< 1 || ids
[consumed
])
1329 if (!vu_cmdline_parent_registered
) {
1330 err
= device_register(&vu_cmdline_parent
);
1332 pr_err("Failed to register parent device!\n");
1333 put_device(&vu_cmdline_parent
);
1336 vu_cmdline_parent_registered
= true;
1339 socket_path
= kmemdup_nul(device
, ids
- device
, GFP_KERNEL
);
1343 pdata
.virtio_device_id
= (u32
) virtio_device_id
;
1344 pdata
.socket_path
= socket_path
;
1346 pr_info("Registering device virtio-uml.%d id=%d at %s\n",
1347 vu_cmdline_id
, virtio_device_id
, socket_path
);
1349 pdev
= platform_device_register_data(&vu_cmdline_parent
, "virtio-uml",
1350 vu_cmdline_id
++, &pdata
,
1352 err
= PTR_ERR_OR_ZERO(pdev
);
1356 ppdata
= pdev
->dev
.platform_data
;
1357 ppdata
->pdev
= pdev
;
1358 INIT_WORK(&ppdata
->conn_broken_wk
, vu_conn_broken
);
1367 static int vu_cmdline_get_device(struct device
*dev
, void *data
)
1369 struct platform_device
*pdev
= to_platform_device(dev
);
1370 struct virtio_uml_platform_data
*pdata
= pdev
->dev
.platform_data
;
1371 char *buffer
= data
;
1372 unsigned int len
= strlen(buffer
);
1374 snprintf(buffer
+ len
, PAGE_SIZE
- len
, "%s:%d:%d\n",
1375 pdata
->socket_path
, pdata
->virtio_device_id
, pdev
->id
);
1379 static int vu_cmdline_get(char *buffer
, const struct kernel_param
*kp
)
1382 if (vu_cmdline_parent_registered
)
1383 device_for_each_child(&vu_cmdline_parent
, buffer
,
1384 vu_cmdline_get_device
);
1385 return strlen(buffer
) + 1;
1388 static const struct kernel_param_ops vu_cmdline_param_ops
= {
1389 .set
= vu_cmdline_set
,
1390 .get
= vu_cmdline_get
,
1393 device_param_cb(device
, &vu_cmdline_param_ops
, NULL
, S_IRUSR
);
1394 __uml_help(vu_cmdline_param_ops
,
1395 "virtio_uml.device=<socket>:<virtio_id>[:<platform_id>]\n"
1396 " Configure a virtio device over a vhost-user socket.\n"
1397 " See virtio_ids.h for a list of possible virtio device id values.\n"
1398 " Optionally use a specific platform_device id.\n\n"
1402 static void vu_unregister_cmdline_devices(void)
1404 if (vu_cmdline_parent_registered
) {
1405 device_for_each_child(&vu_cmdline_parent
, NULL
,
1406 vu_unregister_cmdline_device
);
1407 device_unregister(&vu_cmdline_parent
);
1408 vu_cmdline_parent_registered
= false;
1412 /* Platform driver */
1414 static const struct of_device_id virtio_uml_match
[] = {
1415 { .compatible
= "virtio,uml", },
1418 MODULE_DEVICE_TABLE(of
, virtio_uml_match
);
1420 static int virtio_uml_suspend(struct platform_device
*pdev
, pm_message_t state
)
1422 struct virtio_uml_device
*vu_dev
= platform_get_drvdata(pdev
);
1424 if (!vu_dev
->no_vq_suspend
) {
1425 struct virtqueue
*vq
;
1427 virtio_device_for_each_vq((&vu_dev
->vdev
), vq
) {
1428 struct virtio_uml_vq_info
*info
= vq
->priv
;
1430 info
->suspended
= true;
1431 vhost_user_set_vring_enable(vu_dev
, vq
->index
, false);
1435 if (!device_may_wakeup(&vu_dev
->vdev
.dev
)) {
1436 vu_dev
->suspended
= true;
1440 return irq_set_irq_wake(vu_dev
->irq
, 1);
1443 static int virtio_uml_resume(struct platform_device
*pdev
)
1445 struct virtio_uml_device
*vu_dev
= platform_get_drvdata(pdev
);
1447 if (!vu_dev
->no_vq_suspend
) {
1448 struct virtqueue
*vq
;
1450 virtio_device_for_each_vq((&vu_dev
->vdev
), vq
) {
1451 struct virtio_uml_vq_info
*info
= vq
->priv
;
1453 info
->suspended
= false;
1454 vhost_user_set_vring_enable(vu_dev
, vq
->index
, true);
1458 vu_dev
->suspended
= false;
1460 if (!device_may_wakeup(&vu_dev
->vdev
.dev
))
1463 return irq_set_irq_wake(vu_dev
->irq
, 0);
1466 static struct platform_driver virtio_uml_driver
= {
1467 .probe
= virtio_uml_probe
,
1468 .remove
= virtio_uml_remove
,
1470 .name
= "virtio-uml",
1471 .of_match_table
= virtio_uml_match
,
1473 .suspend
= virtio_uml_suspend
,
1474 .resume
= virtio_uml_resume
,
1477 static int __init
virtio_uml_init(void)
1479 return platform_driver_register(&virtio_uml_driver
);
1482 static void __exit
virtio_uml_exit(void)
1484 platform_driver_unregister(&virtio_uml_driver
);
1485 vu_unregister_cmdline_devices();
1488 module_init(virtio_uml_init
);
1489 module_exit(virtio_uml_exit
);
1490 __uml_exitcall(virtio_uml_exit
);
1492 MODULE_DESCRIPTION("UML driver for vhost-user virtio devices");
1493 MODULE_LICENSE("GPL");