1 /*-
2 * SPDX-License-Identifier: BSD-2-Clause
3 *
4 * Copyright (c) 2016 iXsystems Inc.
5 * All rights reserved.
6 *
7 * This software was developed by Jakub Klama <[email protected]>
8 * under sponsorship from iXsystems Inc.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
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.
19 *
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
30 * SUCH DAMAGE.
31 */
32
33 #include <sys/cdefs.h>
34 #include <sys/param.h>
35 #ifndef WITHOUT_CAPSICUM
36 #include <sys/capsicum.h>
37 #endif
38 #include <sys/linker_set.h>
39 #include <sys/uio.h>
40 #include <sys/types.h>
41 #include <sys/socket.h>
42 #include <sys/un.h>
43
44 #ifndef WITHOUT_CAPSICUM
45 #include <capsicum_helpers.h>
46 #endif
47 #include <err.h>
48 #include <errno.h>
49 #include <fcntl.h>
50 #include <stdio.h>
51 #include <stdlib.h>
52 #include <stdbool.h>
53 #include <string.h>
54 #include <unistd.h>
55 #include <assert.h>
56 #include <pthread.h>
57 #include <libgen.h>
58 #include <sysexits.h>
59
60 #include "bhyverun.h"
61 #include "config.h"
62 #include "debug.h"
63 #include "pci_emul.h"
64 #include "virtio.h"
65 #include "mevent.h"
66 #include "sockstream.h"
67
68 #define VTCON_RINGSZ 64
69 #define VTCON_MAXPORTS 16
70 #define VTCON_MAXQ (VTCON_MAXPORTS * 2 + 2)
71
72 #define VTCON_DEVICE_READY 0
73 #define VTCON_DEVICE_ADD 1
74 #define VTCON_DEVICE_REMOVE 2
75 #define VTCON_PORT_READY 3
76 #define VTCON_CONSOLE_PORT 4
77 #define VTCON_CONSOLE_RESIZE 5
78 #define VTCON_PORT_OPEN 6
79 #define VTCON_PORT_NAME 7
80
81 #define VTCON_F_SIZE 0
82 #define VTCON_F_MULTIPORT 1
83 #define VTCON_F_EMERG_WRITE 2
84 #define VTCON_S_HOSTCAPS \
85 (VTCON_F_SIZE | VTCON_F_MULTIPORT | VTCON_F_EMERG_WRITE)
86
87 static int pci_vtcon_debug;
88 #define DPRINTF(params) if (pci_vtcon_debug) PRINTLN params
89 #define WPRINTF(params) PRINTLN params
90
91 struct pci_vtcon_softc;
92 struct pci_vtcon_port;
93 struct pci_vtcon_config;
94 typedef void (pci_vtcon_cb_t)(struct pci_vtcon_port *, void *, struct iovec *,
95 int);
96
97 struct pci_vtcon_port {
98 struct pci_vtcon_softc * vsp_sc;
99 int vsp_id;
100 const char * vsp_name;
101 bool vsp_enabled;
102 bool vsp_console;
103 bool vsp_rx_ready;
104 bool vsp_open;
105 int vsp_rxq;
106 int vsp_txq;
107 void * vsp_arg;
108 pci_vtcon_cb_t * vsp_cb;
109 };
110
111 struct pci_vtcon_sock
112 {
113 struct pci_vtcon_port * vss_port;
114 const char * vss_path;
115 struct mevent * vss_server_evp;
116 struct mevent * vss_conn_evp;
117 int vss_server_fd;
118 int vss_conn_fd;
119 bool vss_open;
120 };
121
122 struct pci_vtcon_softc {
123 struct virtio_softc vsc_vs;
124 struct vqueue_info vsc_queues[VTCON_MAXQ];
125 pthread_mutex_t vsc_mtx;
126 uint64_t vsc_cfg;
127 uint64_t vsc_features;
128 char * vsc_rootdir;
129 int vsc_kq;
130 bool vsc_ready;
131 struct pci_vtcon_port vsc_control_port;
132 struct pci_vtcon_port vsc_ports[VTCON_MAXPORTS];
133 struct pci_vtcon_config *vsc_config;
134 };
135
136 struct pci_vtcon_config {
137 uint16_t cols;
138 uint16_t rows;
139 uint32_t max_nr_ports;
140 uint32_t emerg_wr;
141 } __attribute__((packed));
142
143 struct pci_vtcon_control {
144 uint32_t id;
145 uint16_t event;
146 uint16_t value;
147 } __attribute__((packed));
148
149 struct pci_vtcon_console_resize {
150 uint16_t cols;
151 uint16_t rows;
152 } __attribute__((packed));
153
154 static void pci_vtcon_reset(void *);
155 static void pci_vtcon_notify_rx(void *, struct vqueue_info *);
156 static void pci_vtcon_notify_tx(void *, struct vqueue_info *);
157 static int pci_vtcon_cfgread(void *, int, int, uint32_t *);
158 static int pci_vtcon_cfgwrite(void *, int, int, uint32_t);
159 static void pci_vtcon_neg_features(void *, uint64_t);
160 static void pci_vtcon_sock_accept(int, enum ev_type, void *);
161 static void pci_vtcon_sock_rx(int, enum ev_type, void *);
162 static void pci_vtcon_sock_tx(struct pci_vtcon_port *, void *, struct iovec *,
163 int);
164 static void pci_vtcon_control_send(struct pci_vtcon_softc *,
165 struct pci_vtcon_control *, const void *, size_t);
166 static void pci_vtcon_announce_port(struct pci_vtcon_port *);
167 static void pci_vtcon_open_port(struct pci_vtcon_port *, bool);
168
169 static struct virtio_consts vtcon_vi_consts = {
170 .vc_name = "vtcon",
171 .vc_nvq = VTCON_MAXQ,
172 .vc_cfgsize = sizeof(struct pci_vtcon_config),
173 .vc_reset = pci_vtcon_reset,
174 .vc_cfgread = pci_vtcon_cfgread,
175 .vc_cfgwrite = pci_vtcon_cfgwrite,
176 .vc_apply_features = pci_vtcon_neg_features,
177 .vc_hv_caps = VTCON_S_HOSTCAPS,
178 };
179
180 static void
pci_vtcon_reset(void * vsc)181 pci_vtcon_reset(void *vsc)
182 {
183 struct pci_vtcon_softc *sc;
184
185 sc = vsc;
186
187 DPRINTF(("vtcon: device reset requested!"));
188 vi_reset_dev(&sc->vsc_vs);
189 }
190
191 static void
pci_vtcon_neg_features(void * vsc,uint64_t negotiated_features)192 pci_vtcon_neg_features(void *vsc, uint64_t negotiated_features)
193 {
194 struct pci_vtcon_softc *sc = vsc;
195
196 sc->vsc_features = negotiated_features;
197 }
198
199 static int
pci_vtcon_cfgread(void * vsc,int offset,int size,uint32_t * retval)200 pci_vtcon_cfgread(void *vsc, int offset, int size, uint32_t *retval)
201 {
202 struct pci_vtcon_softc *sc = vsc;
203 void *ptr;
204
205 ptr = (uint8_t *)sc->vsc_config + offset;
206 memcpy(retval, ptr, size);
207 return (0);
208 }
209
210 static int
pci_vtcon_cfgwrite(void * vsc __unused,int offset __unused,int size __unused,uint32_t val __unused)211 pci_vtcon_cfgwrite(void *vsc __unused, int offset __unused, int size __unused,
212 uint32_t val __unused)
213 {
214 return (0);
215 }
216
217 static inline struct pci_vtcon_port *
pci_vtcon_vq_to_port(struct pci_vtcon_softc * sc,struct vqueue_info * vq)218 pci_vtcon_vq_to_port(struct pci_vtcon_softc *sc, struct vqueue_info *vq)
219 {
220 uint16_t num = vq->vq_num;
221
222 if (num == 0 || num == 1)
223 return (&sc->vsc_ports[0]);
224
225 if (num == 2 || num == 3)
226 return (&sc->vsc_control_port);
227
228 return (&sc->vsc_ports[(num / 2) - 1]);
229 }
230
231 static inline struct vqueue_info *
pci_vtcon_port_to_vq(struct pci_vtcon_port * port,bool tx_queue)232 pci_vtcon_port_to_vq(struct pci_vtcon_port *port, bool tx_queue)
233 {
234 int qnum;
235
236 qnum = tx_queue ? port->vsp_txq : port->vsp_rxq;
237 return (&port->vsp_sc->vsc_queues[qnum]);
238 }
239
240 static struct pci_vtcon_port *
pci_vtcon_port_add(struct pci_vtcon_softc * sc,int port_id,const char * name,pci_vtcon_cb_t * cb,void * arg)241 pci_vtcon_port_add(struct pci_vtcon_softc *sc, int port_id, const char *name,
242 pci_vtcon_cb_t *cb, void *arg)
243 {
244 struct pci_vtcon_port *port;
245
246 port = &sc->vsc_ports[port_id];
247 if (port->vsp_enabled) {
248 errno = EBUSY;
249 return (NULL);
250 }
251 port->vsp_id = port_id;
252 port->vsp_sc = sc;
253 port->vsp_name = name;
254 port->vsp_cb = cb;
255 port->vsp_arg = arg;
256
257 if (port->vsp_id == 0) {
258 /* port0 */
259 port->vsp_txq = 0;
260 port->vsp_rxq = 1;
261 } else {
262 port->vsp_txq = (port_id + 1) * 2;
263 port->vsp_rxq = port->vsp_txq + 1;
264 }
265
266 port->vsp_enabled = true;
267 return (port);
268 }
269
270 static int
pci_vtcon_sock_add(struct pci_vtcon_softc * sc,const char * port_name,const nvlist_t * nvl)271 pci_vtcon_sock_add(struct pci_vtcon_softc *sc, const char *port_name,
272 const nvlist_t *nvl)
273 {
274 struct pci_vtcon_sock *sock = NULL;
275 struct sockaddr_un sun;
276 const char *name, *path;
277 char *cp, *pathcopy;
278 long port;
279 int s = -1, fd = -1, error = 0;
280 #ifndef WITHOUT_CAPSICUM
281 cap_rights_t rights;
282 #endif
283
284 port = strtol(port_name, &cp, 0);
285 if (*cp != '\0' || port < 0 || port >= VTCON_MAXPORTS) {
286 EPRINTLN("vtcon: Invalid port %s", port_name);
287 error = -1;
288 goto out;
289 }
290
291 path = get_config_value_node(nvl, "path");
292 if (path == NULL) {
293 EPRINTLN("vtcon: required path missing for port %ld", port);
294 error = -1;
295 goto out;
296 }
297
298 sock = calloc(1, sizeof(struct pci_vtcon_sock));
299 if (sock == NULL) {
300 error = -1;
301 goto out;
302 }
303
304 s = socket(AF_UNIX, SOCK_STREAM, 0);
305 if (s < 0) {
306 error = -1;
307 goto out;
308 }
309
310 pathcopy = strdup(path);
311 if (pathcopy == NULL) {
312 error = -1;
313 goto out;
314 }
315
316 fd = open(dirname(pathcopy), O_RDONLY | O_DIRECTORY);
317 if (fd < 0) {
318 free(pathcopy);
319 error = -1;
320 goto out;
321 }
322
323 sun.sun_family = AF_UNIX;
324 sun.sun_len = sizeof(struct sockaddr_un);
325 strcpy(pathcopy, path);
326 strlcpy(sun.sun_path, basename(pathcopy), sizeof(sun.sun_path));
327 free(pathcopy);
328
329 if (bindat(fd, s, (struct sockaddr *)&sun, sun.sun_len) < 0) {
330 error = -1;
331 goto out;
332 }
333
334 if (fcntl(s, F_SETFL, O_NONBLOCK) < 0) {
335 error = -1;
336 goto out;
337 }
338
339 if (listen(s, 1) < 0) {
340 error = -1;
341 goto out;
342 }
343
344 #ifndef WITHOUT_CAPSICUM
345 cap_rights_init(&rights, CAP_ACCEPT, CAP_EVENT, CAP_READ, CAP_WRITE);
346 if (caph_rights_limit(s, &rights) == -1)
347 errx(EX_OSERR, "Unable to apply rights for sandbox");
348 #endif
349
350 name = get_config_value_node(nvl, "name");
351 if (name == NULL) {
352 EPRINTLN("vtcon: required name missing for port %ld", port);
353 error = -1;
354 goto out;
355 }
356 sock->vss_port = pci_vtcon_port_add(sc, port, name, pci_vtcon_sock_tx, sock);
357 if (sock->vss_port == NULL) {
358 error = -1;
359 goto out;
360 }
361
362 sock->vss_open = false;
363 sock->vss_conn_fd = -1;
364 sock->vss_server_fd = s;
365 sock->vss_server_evp = mevent_add(s, EVF_READ, pci_vtcon_sock_accept,
366 sock);
367
368 if (sock->vss_server_evp == NULL) {
369 error = -1;
370 goto out;
371 }
372
373 out:
374 if (fd != -1)
375 close(fd);
376
377 if (error != 0) {
378 if (s != -1)
379 close(s);
380 free(sock);
381 }
382
383 return (error);
384 }
385
386 static void
pci_vtcon_sock_accept(int fd __unused,enum ev_type t __unused,void * arg)387 pci_vtcon_sock_accept(int fd __unused, enum ev_type t __unused, void *arg)
388 {
389 struct pci_vtcon_sock *sock = (struct pci_vtcon_sock *)arg;
390 int s;
391
392 s = accept(sock->vss_server_fd, NULL, NULL);
393 if (s < 0)
394 return;
395
396 if (sock->vss_open) {
397 close(s);
398 return;
399 }
400
401 sock->vss_open = true;
402 sock->vss_conn_fd = s;
403 sock->vss_conn_evp = mevent_add(s, EVF_READ, pci_vtcon_sock_rx, sock);
404
405 pci_vtcon_open_port(sock->vss_port, true);
406 }
407
408 static void
pci_vtcon_sock_rx(int fd __unused,enum ev_type t __unused,void * arg)409 pci_vtcon_sock_rx(int fd __unused, enum ev_type t __unused, void *arg)
410 {
411 struct pci_vtcon_port *port;
412 struct pci_vtcon_sock *sock = (struct pci_vtcon_sock *)arg;
413 struct vqueue_info *vq;
414 struct vi_req req;
415 struct iovec iov;
416 static char dummybuf[2048];
417 int len, n;
418
419 port = sock->vss_port;
420 vq = pci_vtcon_port_to_vq(port, true);
421
422 if (!sock->vss_open || !port->vsp_rx_ready) {
423 len = read(sock->vss_conn_fd, dummybuf, sizeof(dummybuf));
424 if (len == 0)
425 goto close;
426
427 return;
428 }
429
430 if (!vq_has_descs(vq)) {
431 len = read(sock->vss_conn_fd, dummybuf, sizeof(dummybuf));
432 vq_endchains(vq, 1);
433 if (len == 0)
434 goto close;
435
436 return;
437 }
438
439 do {
440 n = vq_getchain(vq, &iov, 1, &req);
441 assert(n == 1);
442 len = readv(sock->vss_conn_fd, &iov, n);
443
444 if (len == 0 || (len < 0 && errno == EWOULDBLOCK)) {
445 vq_retchains(vq, 1);
446 vq_endchains(vq, 0);
447 if (len == 0)
448 goto close;
449
450 return;
451 }
452
453 vq_relchain(vq, req.idx, len);
454 } while (vq_has_descs(vq));
455
456 vq_endchains(vq, 1);
457
458 close:
459 mevent_delete_close(sock->vss_conn_evp);
460 sock->vss_conn_fd = -1;
461 sock->vss_open = false;
462 }
463
464 static void
pci_vtcon_sock_tx(struct pci_vtcon_port * port __unused,void * arg __unused,struct iovec * iov,int niov)465 pci_vtcon_sock_tx(struct pci_vtcon_port *port __unused, void *arg __unused,
466 struct iovec *iov, int niov)
467 {
468 struct pci_vtcon_sock *sock;
469 int i, ret;
470
471 sock = (struct pci_vtcon_sock *)arg;
472
473 if (sock->vss_conn_fd == -1)
474 return;
475
476 for (i = 0; i < niov; i++) {
477 ret = stream_write(sock->vss_conn_fd, iov[i].iov_base,
478 iov[i].iov_len);
479 if (ret <= 0)
480 break;
481 }
482
483 if (ret <= 0) {
484 mevent_delete_close(sock->vss_conn_evp);
485 sock->vss_conn_fd = -1;
486 sock->vss_open = false;
487 }
488 }
489
490 static void
pci_vtcon_control_tx(struct pci_vtcon_port * port,void * arg __unused,struct iovec * iov,int niov)491 pci_vtcon_control_tx(struct pci_vtcon_port *port, void *arg __unused,
492 struct iovec *iov, int niov)
493 {
494 struct pci_vtcon_softc *sc;
495 struct pci_vtcon_port *tmp;
496 struct pci_vtcon_control resp, *ctrl;
497 int i;
498
499 assert(niov == 1);
500
501 sc = port->vsp_sc;
502 ctrl = (struct pci_vtcon_control *)iov->iov_base;
503
504 switch (ctrl->event) {
505 case VTCON_DEVICE_READY:
506 sc->vsc_ready = true;
507 /* set port ready events for registered ports */
508 for (i = 0; i < VTCON_MAXPORTS; i++) {
509 tmp = &sc->vsc_ports[i];
510 if (tmp->vsp_enabled)
511 pci_vtcon_announce_port(tmp);
512
513 if (tmp->vsp_open)
514 pci_vtcon_open_port(tmp, true);
515 }
516 break;
517
518 case VTCON_PORT_READY:
519 tmp = &sc->vsc_ports[ctrl->id];
520 if (ctrl->id >= VTCON_MAXPORTS || !tmp->vsp_enabled) {
521 WPRINTF(("VTCON_PORT_READY event for unknown port %d",
522 ctrl->id));
523 return;
524 }
525
526 if (tmp->vsp_console) {
527 resp.event = VTCON_CONSOLE_PORT;
528 resp.id = ctrl->id;
529 resp.value = 1;
530 pci_vtcon_control_send(sc, &resp, NULL, 0);
531 }
532 break;
533 }
534 }
535
536 static void
pci_vtcon_announce_port(struct pci_vtcon_port * port)537 pci_vtcon_announce_port(struct pci_vtcon_port *port)
538 {
539 struct pci_vtcon_control event;
540
541 event.id = port->vsp_id;
542 event.event = VTCON_DEVICE_ADD;
543 event.value = 1;
544 pci_vtcon_control_send(port->vsp_sc, &event, NULL, 0);
545
546 event.event = VTCON_PORT_NAME;
547 pci_vtcon_control_send(port->vsp_sc, &event, port->vsp_name,
548 strlen(port->vsp_name));
549 }
550
551 static void
pci_vtcon_open_port(struct pci_vtcon_port * port,bool open)552 pci_vtcon_open_port(struct pci_vtcon_port *port, bool open)
553 {
554 struct pci_vtcon_control event;
555
556 if (!port->vsp_sc->vsc_ready) {
557 port->vsp_open = true;
558 return;
559 }
560
561 event.id = port->vsp_id;
562 event.event = VTCON_PORT_OPEN;
563 event.value = (int)open;
564 pci_vtcon_control_send(port->vsp_sc, &event, NULL, 0);
565 }
566
567 static void
pci_vtcon_control_send(struct pci_vtcon_softc * sc,struct pci_vtcon_control * ctrl,const void * payload,size_t len)568 pci_vtcon_control_send(struct pci_vtcon_softc *sc,
569 struct pci_vtcon_control *ctrl, const void *payload, size_t len)
570 {
571 struct vqueue_info *vq;
572 struct vi_req req;
573 struct iovec iov;
574 int n;
575
576 if (len > SIZE_T_MAX - sizeof(struct pci_vtcon_control))
577 return;
578
579 vq = pci_vtcon_port_to_vq(&sc->vsc_control_port, true);
580
581 if (!vq_has_descs(vq))
582 return;
583
584 n = vq_getchain(vq, &iov, 1, &req);
585 assert(n == 1);
586
587 if (iov.iov_len < sizeof(struct pci_vtcon_control) + len)
588 goto out;
589
590 memcpy(iov.iov_base, ctrl, sizeof(struct pci_vtcon_control));
591 if (len > 0)
592 memcpy((uint8_t *)iov.iov_base +
593 sizeof(struct pci_vtcon_control), payload, len);
594
595 out:
596 vq_relchain(vq, req.idx, sizeof(struct pci_vtcon_control) + len);
597 vq_endchains(vq, 1);
598 }
599
600
601 static void
pci_vtcon_notify_tx(void * vsc,struct vqueue_info * vq)602 pci_vtcon_notify_tx(void *vsc, struct vqueue_info *vq)
603 {
604 struct pci_vtcon_softc *sc;
605 struct pci_vtcon_port *port;
606 struct iovec iov[1];
607 struct vi_req req;
608 int n;
609
610 sc = vsc;
611 port = pci_vtcon_vq_to_port(sc, vq);
612
613 while (vq_has_descs(vq)) {
614 n = vq_getchain(vq, iov, 1, &req);
615 assert(n == 1);
616 if (port != NULL)
617 port->vsp_cb(port, port->vsp_arg, iov, 1);
618
619 /*
620 * Release this chain and handle more
621 */
622 vq_relchain(vq, req.idx, 0);
623 }
624 vq_endchains(vq, 1); /* Generate interrupt if appropriate. */
625 }
626
627 static void
pci_vtcon_notify_rx(void * vsc,struct vqueue_info * vq)628 pci_vtcon_notify_rx(void *vsc, struct vqueue_info *vq)
629 {
630 struct pci_vtcon_softc *sc;
631 struct pci_vtcon_port *port;
632
633 sc = vsc;
634 port = pci_vtcon_vq_to_port(sc, vq);
635
636 if (!port->vsp_rx_ready) {
637 port->vsp_rx_ready = 1;
638 vq_kick_disable(vq);
639 }
640 }
641
642 /*
643 * Each console device has a "port" node which contains nodes for
644 * each port. Ports are numbered starting at 0.
645 */
646 static int
pci_vtcon_legacy_config_port(nvlist_t * nvl,int port,char * opt)647 pci_vtcon_legacy_config_port(nvlist_t *nvl, int port, char *opt)
648 {
649 char *name, *path;
650 char node_name[sizeof("XX")];
651 nvlist_t *port_nvl;
652
653 name = strsep(&opt, "=");
654 path = opt;
655 if (path == NULL) {
656 EPRINTLN("vtcon: port %s requires a path", name);
657 return (-1);
658 }
659 if (port >= VTCON_MAXPORTS) {
660 EPRINTLN("vtcon: too many ports");
661 return (-1);
662 }
663 snprintf(node_name, sizeof(node_name), "%d", port);
664 port_nvl = create_relative_config_node(nvl, node_name);
665 set_config_value_node(port_nvl, "name", name);
666 set_config_value_node(port_nvl, "path", path);
667 return (0);
668 }
669
670 static int
pci_vtcon_legacy_config(nvlist_t * nvl,const char * opts)671 pci_vtcon_legacy_config(nvlist_t *nvl, const char *opts)
672 {
673 char *opt, *str, *tofree;
674 nvlist_t *ports_nvl;
675 int error, port;
676
677 ports_nvl = create_relative_config_node(nvl, "port");
678 tofree = str = strdup(opts);
679 error = 0;
680 port = 0;
681 while ((opt = strsep(&str, ",")) != NULL) {
682 error = pci_vtcon_legacy_config_port(ports_nvl, port, opt);
683 if (error)
684 break;
685 port++;
686 }
687 free(tofree);
688 return (error);
689 }
690
691 static int
pci_vtcon_init(struct pci_devinst * pi,nvlist_t * nvl)692 pci_vtcon_init(struct pci_devinst *pi, nvlist_t *nvl)
693 {
694 struct pci_vtcon_softc *sc;
695 nvlist_t *ports_nvl;
696 int i;
697
698 sc = calloc(1, sizeof(struct pci_vtcon_softc));
699 sc->vsc_config = calloc(1, sizeof(struct pci_vtcon_config));
700 sc->vsc_config->max_nr_ports = VTCON_MAXPORTS;
701 sc->vsc_config->cols = 80;
702 sc->vsc_config->rows = 25;
703
704 pthread_mutex_init(&sc->vsc_mtx, NULL);
705
706 vi_softc_linkup(&sc->vsc_vs, &vtcon_vi_consts, sc, pi, sc->vsc_queues);
707 sc->vsc_vs.vs_mtx = &sc->vsc_mtx;
708
709 for (i = 0; i < VTCON_MAXQ; i++) {
710 sc->vsc_queues[i].vq_qsize = VTCON_RINGSZ;
711 sc->vsc_queues[i].vq_notify = i % 2 == 0
712 ? pci_vtcon_notify_rx
713 : pci_vtcon_notify_tx;
714 }
715
716 /* initialize config space */
717 pci_set_cfgdata16(pi, PCIR_DEVICE, VIRTIO_DEV_CONSOLE);
718 pci_set_cfgdata16(pi, PCIR_VENDOR, VIRTIO_VENDOR);
719 pci_set_cfgdata8(pi, PCIR_CLASS, PCIC_SIMPLECOMM);
720 pci_set_cfgdata16(pi, PCIR_SUBDEV_0, VIRTIO_ID_CONSOLE);
721 pci_set_cfgdata16(pi, PCIR_SUBVEND_0, VIRTIO_VENDOR);
722
723 if (vi_intr_init(&sc->vsc_vs, 1, fbsdrun_virtio_msix()))
724 return (1);
725 vi_set_io_bar(&sc->vsc_vs, 0);
726
727 /* create control port */
728 sc->vsc_control_port.vsp_sc = sc;
729 sc->vsc_control_port.vsp_txq = 2;
730 sc->vsc_control_port.vsp_rxq = 3;
731 sc->vsc_control_port.vsp_cb = pci_vtcon_control_tx;
732 sc->vsc_control_port.vsp_enabled = true;
733
734 ports_nvl = find_relative_config_node(nvl, "port");
735 if (ports_nvl != NULL) {
736 const char *name;
737 void *cookie;
738 int type;
739
740 cookie = NULL;
741 while ((name = nvlist_next(ports_nvl, &type, &cookie)) !=
742 NULL) {
743 if (type != NV_TYPE_NVLIST)
744 continue;
745
746 if (pci_vtcon_sock_add(sc, name,
747 nvlist_get_nvlist(ports_nvl, name)) < 0) {
748 EPRINTLN("cannot create port %s: %s",
749 name, strerror(errno));
750 return (1);
751 }
752 }
753 }
754
755 return (0);
756 }
757
758 static const struct pci_devemu pci_de_vcon = {
759 .pe_emu = "virtio-console",
760 .pe_init = pci_vtcon_init,
761 .pe_barwrite = vi_pci_write,
762 .pe_barread = vi_pci_read,
763 .pe_legacy_config = pci_vtcon_legacy_config,
764 };
765 PCI_EMUL_SET(pci_de_vcon);
766