2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2016 iXsystems Inc.
7 * This software was developed by Jakub Klama <jceel@FreeBSD.org>
8 * under sponsorship from iXsystems Inc.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer
15 * in this position and unchanged.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
20 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 #include <sys/cdefs.h>
34 __FBSDID("$FreeBSD$");
36 #include <sys/param.h>
37 #include <sys/linker_set.h>
39 #include <sys/types.h>
40 #include <sys/socket.h>
58 #include "sockstream.h"
60 #define VTCON_RINGSZ 64
61 #define VTCON_MAXPORTS 16
62 #define VTCON_MAXQ (VTCON_MAXPORTS * 2 + 2)
64 #define VTCON_DEVICE_READY 0
65 #define VTCON_DEVICE_ADD 1
66 #define VTCON_DEVICE_REMOVE 2
67 #define VTCON_PORT_READY 3
68 #define VTCON_CONSOLE_PORT 4
69 #define VTCON_CONSOLE_RESIZE 5
70 #define VTCON_PORT_OPEN 6
71 #define VTCON_PORT_NAME 7
73 #define VTCON_F_SIZE 0
74 #define VTCON_F_MULTIPORT 1
75 #define VTCON_F_EMERG_WRITE 2
76 #define VTCON_S_HOSTCAPS \
77 (VTCON_F_SIZE | VTCON_F_MULTIPORT | VTCON_F_EMERG_WRITE)
79 static int pci_vtcon_debug;
80 #define DPRINTF(params) if (pci_vtcon_debug) printf params
81 #define WPRINTF(params) printf params
83 struct pci_vtcon_softc;
84 struct pci_vtcon_port;
85 struct pci_vtcon_config;
86 typedef void (pci_vtcon_cb_t)(struct pci_vtcon_port *, void *, struct iovec *,
89 struct pci_vtcon_port {
90 struct pci_vtcon_softc * vsp_sc;
92 const char * vsp_name;
100 pci_vtcon_cb_t * vsp_cb;
103 struct pci_vtcon_sock
105 struct pci_vtcon_port * vss_port;
106 const char * vss_path;
107 struct mevent * vss_server_evp;
108 struct mevent * vss_conn_evp;
114 struct pci_vtcon_softc {
115 struct virtio_softc vsc_vs;
116 struct vqueue_info vsc_queues[VTCON_MAXQ];
117 pthread_mutex_t vsc_mtx;
119 uint64_t vsc_features;
124 struct pci_vtcon_port vsc_control_port;
125 struct pci_vtcon_port vsc_ports[VTCON_MAXPORTS];
126 struct pci_vtcon_config *vsc_config;
129 struct pci_vtcon_config {
132 uint32_t max_nr_ports;
134 } __attribute__((packed));
136 struct pci_vtcon_control {
140 } __attribute__((packed));
142 struct pci_vtcon_console_resize {
145 } __attribute__((packed));
147 static void pci_vtcon_reset(void *);
148 static void pci_vtcon_notify_rx(void *, struct vqueue_info *);
149 static void pci_vtcon_notify_tx(void *, struct vqueue_info *);
150 static int pci_vtcon_cfgread(void *, int, int, uint32_t *);
151 static int pci_vtcon_cfgwrite(void *, int, int, uint32_t);
152 static void pci_vtcon_neg_features(void *, uint64_t);
153 static void pci_vtcon_sock_accept(int, enum ev_type, void *);
154 static void pci_vtcon_sock_rx(int, enum ev_type, void *);
155 static void pci_vtcon_sock_tx(struct pci_vtcon_port *, void *, struct iovec *,
157 static void pci_vtcon_control_send(struct pci_vtcon_softc *,
158 struct pci_vtcon_control *, const void *, size_t);
159 static void pci_vtcon_announce_port(struct pci_vtcon_port *);
160 static void pci_vtcon_open_port(struct pci_vtcon_port *, bool);
162 static struct virtio_consts vtcon_vi_consts = {
163 "vtcon", /* our name */
164 VTCON_MAXQ, /* we support VTCON_MAXQ virtqueues */
165 sizeof(struct pci_vtcon_config), /* config reg size */
166 pci_vtcon_reset, /* reset */
167 NULL, /* device-wide qnotify */
168 pci_vtcon_cfgread, /* read virtio config */
169 pci_vtcon_cfgwrite, /* write virtio config */
170 pci_vtcon_neg_features, /* apply negotiated features */
171 VTCON_S_HOSTCAPS, /* our capabilities */
176 pci_vtcon_reset(void *vsc)
178 struct pci_vtcon_softc *sc;
182 DPRINTF(("vtcon: device reset requested!\n"));
183 vi_reset_dev(&sc->vsc_vs);
187 pci_vtcon_neg_features(void *vsc, uint64_t negotiated_features)
189 struct pci_vtcon_softc *sc = vsc;
191 sc->vsc_features = negotiated_features;
195 pci_vtcon_cfgread(void *vsc, int offset, int size, uint32_t *retval)
197 struct pci_vtcon_softc *sc = vsc;
200 ptr = (uint8_t *)sc->vsc_config + offset;
201 memcpy(retval, ptr, size);
206 pci_vtcon_cfgwrite(void *vsc, int offset, int size, uint32_t val)
212 static inline struct pci_vtcon_port *
213 pci_vtcon_vq_to_port(struct pci_vtcon_softc *sc, struct vqueue_info *vq)
215 uint16_t num = vq->vq_num;
217 if (num == 0 || num == 1)
218 return (&sc->vsc_ports[0]);
220 if (num == 2 || num == 3)
221 return (&sc->vsc_control_port);
223 return (&sc->vsc_ports[(num / 2) - 1]);
226 static inline struct vqueue_info *
227 pci_vtcon_port_to_vq(struct pci_vtcon_port *port, bool tx_queue)
231 qnum = tx_queue ? port->vsp_txq : port->vsp_rxq;
232 return (&port->vsp_sc->vsc_queues[qnum]);
235 static struct pci_vtcon_port *
236 pci_vtcon_port_add(struct pci_vtcon_softc *sc, const char *name,
237 pci_vtcon_cb_t *cb, void *arg)
239 struct pci_vtcon_port *port;
241 if (sc->vsc_nports == VTCON_MAXPORTS) {
246 port = &sc->vsc_ports[sc->vsc_nports++];
247 port->vsp_id = sc->vsc_nports - 1;
249 port->vsp_name = name;
253 if (port->vsp_id == 0) {
258 port->vsp_txq = sc->vsc_nports * 2;
259 port->vsp_rxq = port->vsp_txq + 1;
262 port->vsp_enabled = true;
267 pci_vtcon_sock_add(struct pci_vtcon_softc *sc, const char *name,
270 struct pci_vtcon_sock *sock;
271 struct sockaddr_un sun;
272 int s = -1, fd = -1, error = 0;
274 sock = calloc(1, sizeof(struct pci_vtcon_sock));
280 s = socket(AF_UNIX, SOCK_STREAM, 0);
286 fd = open(dirname(path), O_RDONLY | O_DIRECTORY);
292 sun.sun_family = AF_UNIX;
293 sun.sun_len = sizeof(struct sockaddr_un);
294 strncpy(sun.sun_path, basename((char *)path), sizeof(sun.sun_path));
296 if (bindat(fd, s, (struct sockaddr *)&sun, sun.sun_len) < 0) {
301 if (fcntl(s, F_SETFL, O_NONBLOCK) < 0) {
306 if (listen(s, 1) < 0) {
312 sock->vss_port = pci_vtcon_port_add(sc, name, pci_vtcon_sock_tx, sock);
313 if (sock->vss_port == NULL) {
318 sock->vss_open = false;
319 sock->vss_conn_fd = -1;
320 sock->vss_server_fd = s;
321 sock->vss_server_evp = mevent_add(s, EVF_READ, pci_vtcon_sock_accept,
324 if (sock->vss_server_evp == NULL) {
333 if (error != 0 && s != -1)
340 pci_vtcon_sock_accept(int fd __unused, enum ev_type t __unused, void *arg)
342 struct pci_vtcon_sock *sock = (struct pci_vtcon_sock *)arg;
345 s = accept(sock->vss_server_fd, NULL, NULL);
349 if (sock->vss_open) {
354 sock->vss_open = true;
355 sock->vss_conn_fd = s;
356 sock->vss_conn_evp = mevent_add(s, EVF_READ, pci_vtcon_sock_rx, sock);
358 pci_vtcon_open_port(sock->vss_port, true);
362 pci_vtcon_sock_rx(int fd __unused, enum ev_type t __unused, void *arg)
364 struct pci_vtcon_port *port;
365 struct pci_vtcon_sock *sock = (struct pci_vtcon_sock *)arg;
366 struct vqueue_info *vq;
368 static char dummybuf[2048];
372 port = sock->vss_port;
373 vq = pci_vtcon_port_to_vq(port, true);
375 if (!sock->vss_open || !port->vsp_rx_ready) {
376 len = read(sock->vss_conn_fd, dummybuf, sizeof(dummybuf));
383 if (!vq_has_descs(vq)) {
384 len = read(sock->vss_conn_fd, dummybuf, sizeof(dummybuf));
393 n = vq_getchain(vq, &idx, &iov, 1, NULL);
394 len = readv(sock->vss_conn_fd, &iov, n);
396 if (len == 0 || (len < 0 && errno == EWOULDBLOCK)) {
405 vq_relchain(vq, idx, len);
406 } while (vq_has_descs(vq));
411 mevent_delete_close(sock->vss_conn_evp);
412 sock->vss_conn_fd = -1;
413 sock->vss_open = false;
417 pci_vtcon_sock_tx(struct pci_vtcon_port *port, void *arg, struct iovec *iov,
420 struct pci_vtcon_sock *sock;
423 sock = (struct pci_vtcon_sock *)arg;
425 if (sock->vss_conn_fd == -1)
428 for (i = 0; i < niov; i++) {
429 ret = stream_write(sock->vss_conn_fd, iov[i].iov_base,
436 mevent_delete_close(sock->vss_conn_evp);
437 sock->vss_conn_fd = -1;
438 sock->vss_open = false;
443 pci_vtcon_control_tx(struct pci_vtcon_port *port, void *arg, struct iovec *iov,
446 struct pci_vtcon_softc *sc;
447 struct pci_vtcon_port *tmp;
448 struct pci_vtcon_control resp, *ctrl;
454 ctrl = (struct pci_vtcon_control *)iov->iov_base;
456 switch (ctrl->event) {
457 case VTCON_DEVICE_READY:
458 sc->vsc_ready = true;
459 /* set port ready events for registered ports */
460 for (i = 0; i < VTCON_MAXPORTS; i++) {
461 tmp = &sc->vsc_ports[i];
462 if (tmp->vsp_enabled)
463 pci_vtcon_announce_port(tmp);
466 pci_vtcon_open_port(tmp, true);
470 case VTCON_PORT_READY:
471 if (ctrl->id >= sc->vsc_nports) {
472 WPRINTF(("VTCON_PORT_READY event for unknown port %d\n",
477 tmp = &sc->vsc_ports[ctrl->id];
478 if (tmp->vsp_console) {
479 resp.event = VTCON_CONSOLE_PORT;
482 pci_vtcon_control_send(sc, &resp, NULL, 0);
489 pci_vtcon_announce_port(struct pci_vtcon_port *port)
491 struct pci_vtcon_control event;
493 event.id = port->vsp_id;
494 event.event = VTCON_DEVICE_ADD;
496 pci_vtcon_control_send(port->vsp_sc, &event, NULL, 0);
498 event.event = VTCON_PORT_NAME;
499 pci_vtcon_control_send(port->vsp_sc, &event, port->vsp_name,
500 strlen(port->vsp_name));
504 pci_vtcon_open_port(struct pci_vtcon_port *port, bool open)
506 struct pci_vtcon_control event;
508 if (!port->vsp_sc->vsc_ready) {
509 port->vsp_open = true;
513 event.id = port->vsp_id;
514 event.event = VTCON_PORT_OPEN;
515 event.value = (int)open;
516 pci_vtcon_control_send(port->vsp_sc, &event, NULL, 0);
520 pci_vtcon_control_send(struct pci_vtcon_softc *sc,
521 struct pci_vtcon_control *ctrl, const void *payload, size_t len)
523 struct vqueue_info *vq;
528 vq = pci_vtcon_port_to_vq(&sc->vsc_control_port, true);
530 if (!vq_has_descs(vq))
533 n = vq_getchain(vq, &idx, &iov, 1, NULL);
537 memcpy(iov.iov_base, ctrl, sizeof(struct pci_vtcon_control));
538 if (payload != NULL && len > 0)
539 memcpy(iov.iov_base + sizeof(struct pci_vtcon_control),
542 vq_relchain(vq, idx, sizeof(struct pci_vtcon_control) + len);
548 pci_vtcon_notify_tx(void *vsc, struct vqueue_info *vq)
550 struct pci_vtcon_softc *sc;
551 struct pci_vtcon_port *port;
557 port = pci_vtcon_vq_to_port(sc, vq);
559 while (vq_has_descs(vq)) {
560 n = vq_getchain(vq, &idx, iov, 1, flags);
563 port->vsp_cb(port, port->vsp_arg, iov, 1);
566 * Release this chain and handle more
568 vq_relchain(vq, idx, 0);
570 vq_endchains(vq, 1); /* Generate interrupt if appropriate. */
574 pci_vtcon_notify_rx(void *vsc, struct vqueue_info *vq)
576 struct pci_vtcon_softc *sc;
577 struct pci_vtcon_port *port;
580 port = pci_vtcon_vq_to_port(sc, vq);
582 if (!port->vsp_rx_ready) {
583 port->vsp_rx_ready = 1;
584 vq->vq_used->vu_flags |= VRING_USED_F_NO_NOTIFY;
589 pci_vtcon_init(struct vmctx *ctx, struct pci_devinst *pi, char *opts)
591 struct pci_vtcon_softc *sc;
592 char *portname = NULL;
593 char *portpath = NULL;
597 sc = calloc(1, sizeof(struct pci_vtcon_softc));
598 sc->vsc_config = calloc(1, sizeof(struct pci_vtcon_config));
599 sc->vsc_config->max_nr_ports = VTCON_MAXPORTS;
600 sc->vsc_config->cols = 80;
601 sc->vsc_config->rows = 25;
603 vi_softc_linkup(&sc->vsc_vs, &vtcon_vi_consts, sc, pi, sc->vsc_queues);
604 sc->vsc_vs.vs_mtx = &sc->vsc_mtx;
606 for (i = 0; i < VTCON_MAXQ; i++) {
607 sc->vsc_queues[i].vq_qsize = VTCON_RINGSZ;
608 sc->vsc_queues[i].vq_notify = i % 2 == 0
609 ? pci_vtcon_notify_rx
610 : pci_vtcon_notify_tx;
613 /* initialize config space */
614 pci_set_cfgdata16(pi, PCIR_DEVICE, VIRTIO_DEV_CONSOLE);
615 pci_set_cfgdata16(pi, PCIR_VENDOR, VIRTIO_VENDOR);
616 pci_set_cfgdata8(pi, PCIR_CLASS, PCIC_SIMPLECOMM);
617 pci_set_cfgdata16(pi, PCIR_SUBDEV_0, VIRTIO_TYPE_CONSOLE);
618 pci_set_cfgdata16(pi, PCIR_SUBVEND_0, VIRTIO_VENDOR);
620 if (vi_intr_init(&sc->vsc_vs, 1, fbsdrun_virtio_msix()))
622 vi_set_io_bar(&sc->vsc_vs, 0);
624 /* create control port */
625 sc->vsc_control_port.vsp_sc = sc;
626 sc->vsc_control_port.vsp_txq = 2;
627 sc->vsc_control_port.vsp_rxq = 3;
628 sc->vsc_control_port.vsp_cb = pci_vtcon_control_tx;
629 sc->vsc_control_port.vsp_enabled = true;
631 while ((opt = strsep(&opts, ",")) != NULL) {
632 portname = strsep(&opt, "=");
636 if (pci_vtcon_sock_add(sc, portname, portpath) < 0) {
637 fprintf(stderr, "cannot create port %s: %s\n",
638 portname, strerror(errno));
646 struct pci_devemu pci_de_vcon = {
647 .pe_emu = "virtio-console",
648 .pe_init = pci_vtcon_init,
649 .pe_barwrite = vi_pci_write,
650 .pe_barread = vi_pci_read
652 PCI_EMUL_SET(pci_de_vcon);