Lines Matching +full:console +full:- +full:size
1 // SPDX-License-Identifier: GPL-2.0-or-later
28 #include <linux/dma-mapping.h>
43 /* Used for exporting per-port information to debugfs */
49 /* All the console devices handled by this driver */
56 .name = "virtio-ports",
62 /* This struct holds information that's relevant only for console ports */
63 struct console { struct
67 /* The hvc device associated with this console port */ argument
70 /* The size of the console */ argument
77 * differentiate between the other console ports handled by argument
88 /* size of the buffer in *buf above */
89 size_t size; member
113 * This is a per-device struct that stores data common to all the
114 * ports for that device (vdev->priv).
144 * guest->host transfers, one for host->guest transfers
149 * A control packet buffer for guest->host requests, protected
154 /* Array of per-port IO virtqueues */
165 /* This struct holds the per-port data */
201 * hooked up to an hvc console
203 struct console cons;
209 /* Reference-counting to handle port hot-unplugs and file operations */
233 /* This is the very early arch-specified put chars function. */
239 struct console *cons; in find_port_by_vtermno()
244 if (cons->vtermno == vtermno) { in find_port_by_vtermno()
261 spin_lock_irqsave(&portdev->ports_lock, flags); in find_port_by_devt_in_portdev()
262 list_for_each_entry(port, &portdev->ports, list) { in find_port_by_devt_in_portdev()
263 if (port->cdev->dev == dev) { in find_port_by_devt_in_portdev()
264 kref_get(&port->kref); in find_port_by_devt_in_portdev()
270 spin_unlock_irqrestore(&portdev->ports_lock, flags); in find_port_by_devt_in_portdev()
298 spin_lock_irqsave(&portdev->ports_lock, flags); in find_port_by_id()
299 list_for_each_entry(port, &portdev->ports, list) in find_port_by_id()
300 if (port->id == id) in find_port_by_id()
304 spin_unlock_irqrestore(&portdev->ports_lock, flags); in find_port_by_id()
315 spin_lock_irqsave(&portdev->ports_lock, flags); in find_port_by_vq()
316 list_for_each_entry(port, &portdev->ports, list) in find_port_by_vq()
317 if (port->in_vq == vq || port->out_vq == vq) in find_port_by_vq()
321 spin_unlock_irqrestore(&portdev->ports_lock, flags); in find_port_by_vq()
327 if (port->cons.hvc) in is_console_port()
334 return is_rproc_enabled && vdev->id.device == VIRTIO_ID_RPROC_SERIAL; in is_rproc_serial()
343 if (!portdev->vdev) in use_multiport()
345 return __virtio_test_bit(portdev->vdev, VIRTIO_CONSOLE_F_MULTIPORT); in use_multiport()
355 for (i = 0; i < buf->sgpages; i++) { in free_buf()
356 struct page *page = sg_page(&buf->sg[i]); in free_buf()
362 if (!buf->dev) { in free_buf()
363 kfree(buf->buf); in free_buf()
369 /* queue up dma-buffers to be freed later */ in free_buf()
371 list_add_tail(&buf->list, &pending_free_dma_bufs); in free_buf()
375 dma_free_coherent(buf->dev, buf->size, buf->buf, buf->dma); in free_buf()
378 put_device(buf->dev); in free_buf()
401 list_del(&buf->list); in reclaim_dma_bufs()
421 buf->sgpages = pages; in alloc_buf()
423 buf->dev = NULL; in alloc_buf()
424 buf->buf = NULL; in alloc_buf()
435 buf->dev = vdev->dev.parent; in alloc_buf()
436 if (!buf->dev) in alloc_buf()
440 get_device(buf->dev); in alloc_buf()
441 buf->buf = dma_alloc_coherent(buf->dev, buf_size, &buf->dma, in alloc_buf()
444 buf->dev = NULL; in alloc_buf()
445 buf->buf = kmalloc(buf_size, GFP_KERNEL); in alloc_buf()
448 if (!buf->buf) in alloc_buf()
450 buf->len = 0; in alloc_buf()
451 buf->offset = 0; in alloc_buf()
452 buf->size = buf_size; in alloc_buf()
467 if (port->inbuf) in get_inbuf()
468 return port->inbuf; in get_inbuf()
470 buf = virtqueue_get_buf(port->in_vq, &len); in get_inbuf()
472 buf->len = min_t(size_t, len, buf->size); in get_inbuf()
473 buf->offset = 0; in get_inbuf()
474 port->stats.bytes_received += len; in get_inbuf()
480 * Create a scatter-gather list representing our input buffer and put
490 sg_init_one(sg, buf->buf, buf->size); in add_inbuf()
495 ret = vq->num_free; in add_inbuf()
505 if (!port->portdev) { in discard_port_data()
513 port->stats.bytes_discarded += buf->len - buf->offset; in discard_port_data()
514 if (add_inbuf(port->in_vq, buf) < 0) { in discard_port_data()
518 port->inbuf = NULL; in discard_port_data()
522 dev_warn(port->dev, "Errors adding %d buffers back to vq\n", in discard_port_data()
532 spin_lock_irqsave(&port->inbuf_lock, flags); in port_has_data()
533 port->inbuf = get_inbuf(port); in port_has_data()
534 if (port->inbuf) in port_has_data()
537 spin_unlock_irqrestore(&port->inbuf_lock, flags); in port_has_data()
551 vq = portdev->c_ovq; in __send_control_msg()
553 spin_lock(&portdev->c_ovq_lock); in __send_control_msg()
555 portdev->cpkt.id = cpu_to_virtio32(portdev->vdev, port_id); in __send_control_msg()
556 portdev->cpkt.event = cpu_to_virtio16(portdev->vdev, event); in __send_control_msg()
557 portdev->cpkt.value = cpu_to_virtio16(portdev->vdev, value); in __send_control_msg()
559 sg_init_one(sg, &portdev->cpkt, sizeof(struct virtio_console_control)); in __send_control_msg()
561 if (virtqueue_add_outbuf(vq, sg, 1, &portdev->cpkt, GFP_ATOMIC) == 0) { in __send_control_msg()
568 spin_unlock(&portdev->c_ovq_lock); in __send_control_msg()
576 if (port->portdev) in send_control_msg()
577 return __send_control_msg(port->portdev, port->id, event, value); in send_control_msg()
582 /* Callers must take the port->outvq_lock */
588 if (!port->portdev) { in reclaim_consumed_buffers()
592 while ((buf = virtqueue_get_buf(port->out_vq, &len))) { in reclaim_consumed_buffers()
594 port->outvq_full = false; in reclaim_consumed_buffers()
607 out_vq = port->out_vq; in __send_to_port()
609 spin_lock_irqsave(&port->outvq_lock, flags); in __send_to_port()
623 if (out_vq->num_free == 0) in __send_to_port()
624 port->outvq_full = true; in __send_to_port()
636 * console driver writes something out. in __send_to_port()
642 spin_unlock_irqrestore(&port->outvq_lock, flags); in __send_to_port()
644 port->stats.bytes_sent += in_count; in __send_to_port()
646 * We're expected to return the amount of data we wrote -- all in __send_to_port()
665 buf = port->inbuf; in fill_readbuf()
666 out_count = min(out_count, buf->len - buf->offset); in fill_readbuf()
671 ret = copy_to_user(out_buf, buf->buf + buf->offset, out_count); in fill_readbuf()
673 return -EFAULT; in fill_readbuf()
675 memcpy((__force char *)out_buf, buf->buf + buf->offset, in fill_readbuf()
679 buf->offset += out_count; in fill_readbuf()
681 if (buf->offset == buf->len) { in fill_readbuf()
684 * Re-queue so that the Host can send us more data. in fill_readbuf()
686 spin_lock_irqsave(&port->inbuf_lock, flags); in fill_readbuf()
687 port->inbuf = NULL; in fill_readbuf()
689 if (add_inbuf(port->in_vq, buf) < 0) in fill_readbuf()
690 dev_warn(port->dev, "failed add_buf\n"); in fill_readbuf()
692 spin_unlock_irqrestore(&port->inbuf_lock, flags); in fill_readbuf()
701 if (!port->guest_connected) { in will_read_block()
702 /* Port got hot-unplugged. Let's exit. */ in will_read_block()
705 return !port_has_data(port) && port->host_connected; in will_read_block()
712 if (!port->guest_connected) { in will_write_block()
713 /* Port got hot-unplugged. Let's exit. */ in will_write_block()
716 if (!port->host_connected) in will_write_block()
719 spin_lock_irq(&port->outvq_lock); in will_write_block()
725 ret = port->outvq_full; in will_write_block()
726 spin_unlock_irq(&port->outvq_lock); in will_write_block()
737 port = filp->private_data; in port_fops_read()
739 /* Port is hot-unplugged. */ in port_fops_read()
740 if (!port->guest_connected) in port_fops_read()
741 return -ENODEV; in port_fops_read()
749 if (!port->host_connected) in port_fops_read()
751 if (filp->f_flags & O_NONBLOCK) in port_fops_read()
752 return -EAGAIN; in port_fops_read()
754 ret = wait_event_freezable(port->waitqueue, in port_fops_read()
759 /* Port got hot-unplugged while we were waiting above. */ in port_fops_read()
760 if (!port->guest_connected) in port_fops_read()
761 return -ENODEV; in port_fops_read()
772 if (!port_has_data(port) && !port->host_connected) in port_fops_read()
784 return -EAGAIN; in wait_port_writable()
786 ret = wait_event_freezable(port->waitqueue, in wait_port_writable()
791 /* Port got hot-unplugged. */ in wait_port_writable()
792 if (!port->guest_connected) in wait_port_writable()
793 return -ENODEV; in wait_port_writable()
811 port = filp->private_data; in port_fops_write()
813 nonblock = filp->f_flags & O_NONBLOCK; in port_fops_write()
821 buf = alloc_buf(port->portdev->vdev, count, 0); in port_fops_write()
823 return -ENOMEM; in port_fops_write()
825 ret = copy_from_user(buf->buf, ubuf, count); in port_fops_write()
827 ret = -EFAULT; in port_fops_write()
832 * We now ask send_buf() to not spin for generic ports -- we in port_fops_write()
833 * can re-use the same code path that non-blocking file in port_fops_write()
839 sg_init_one(sg, buf->buf, count); in port_fops_write()
853 unsigned int size; member
861 struct sg_list *sgl = sd->u.data; in pipe_to_sg()
864 if (sgl->n == sgl->size) in pipe_to_sg()
870 get_page(buf->page); in pipe_to_sg()
871 unlock_page(buf->page); in pipe_to_sg()
873 len = min(buf->len, sd->len); in pipe_to_sg()
874 sg_set_page(&(sgl->sg[sgl->n]), buf->page, len, buf->offset); in pipe_to_sg()
881 return -ENOMEM; in pipe_to_sg()
883 offset = sd->pos & ~PAGE_MASK; in pipe_to_sg()
885 len = sd->len; in pipe_to_sg()
887 len = PAGE_SIZE - offset; in pipe_to_sg()
889 src = kmap_atomic(buf->page); in pipe_to_sg()
890 memcpy(page_address(page) + offset, src + buf->offset, len); in pipe_to_sg()
893 sg_set_page(&(sgl->sg[sgl->n]), page, len, offset); in pipe_to_sg()
895 sgl->n++; in pipe_to_sg()
896 sgl->len += len; in pipe_to_sg()
901 /* Faster zero-copy write by splicing */
906 struct port *port = filp->private_data; in port_fops_splice_write()
920 * pipe_to_sg() must allocate dma-buffers and copy content from in port_fops_splice_write()
922 * support allocating and freeing such a list of dma-buffers. in port_fops_splice_write()
924 if (is_rproc_serial(port->out_vq->vdev)) in port_fops_splice_write()
925 return -EINVAL; in port_fops_splice_write()
929 if (pipe_empty(pipe->head, pipe->tail)) in port_fops_splice_write()
932 ret = wait_port_writable(port, filp->f_flags & O_NONBLOCK); in port_fops_splice_write()
936 occupancy = pipe_occupancy(pipe->head, pipe->tail); in port_fops_splice_write()
937 buf = alloc_buf(port->portdev->vdev, 0, occupancy); in port_fops_splice_write()
940 ret = -ENOMEM; in port_fops_splice_write()
946 sgl.size = occupancy; in port_fops_splice_write()
947 sgl.sg = buf->sg; in port_fops_splice_write()
948 sg_init_table(sgl.sg, sgl.size); in port_fops_splice_write()
952 ret = __send_to_port(port, buf->sg, sgl.n, sgl.len, buf, true); in port_fops_splice_write()
968 port = filp->private_data; in port_fops_poll()
969 poll_wait(filp, &port->waitqueue, wait); in port_fops_poll()
971 if (!port->guest_connected) { in port_fops_poll()
980 if (!port->host_connected) in port_fops_poll()
992 port = filp->private_data; in port_fops_release()
997 spin_lock_irq(&port->inbuf_lock); in port_fops_release()
998 port->guest_connected = false; in port_fops_release()
1002 spin_unlock_irq(&port->inbuf_lock); in port_fops_release()
1004 spin_lock_irq(&port->outvq_lock); in port_fops_release()
1006 spin_unlock_irq(&port->outvq_lock); in port_fops_release()
1017 kref_put(&port->kref, remove_port); in port_fops_release()
1024 struct cdev *cdev = inode->i_cdev; in port_fops_open()
1029 port = find_port_by_devt(cdev->dev); in port_fops_open()
1032 return -ENXIO; in port_fops_open()
1034 filp->private_data = port; in port_fops_open()
1037 * Don't allow opening of console port devices -- that's done in port_fops_open()
1041 ret = -ENXIO; in port_fops_open()
1046 spin_lock_irq(&port->inbuf_lock); in port_fops_open()
1047 if (port->guest_connected) { in port_fops_open()
1048 spin_unlock_irq(&port->inbuf_lock); in port_fops_open()
1049 ret = -EBUSY; in port_fops_open()
1053 port->guest_connected = true; in port_fops_open()
1054 spin_unlock_irq(&port->inbuf_lock); in port_fops_open()
1056 spin_lock_irq(&port->outvq_lock); in port_fops_open()
1063 spin_unlock_irq(&port->outvq_lock); in port_fops_open()
1068 send_control_msg(filp->private_data, VIRTIO_CONSOLE_PORT_OPEN, 1); in port_fops_open()
1072 kref_put(&port->kref, remove_port); in port_fops_open()
1080 port = filp->private_data; in port_fops_fasync()
1081 return fasync_helper(fd, filp, mode, &port->async_queue); in port_fops_fasync()
1086 * a console device, read from it, write to it, poll for data and
1105 * We turn the characters into a scatter-gather list, add it to the
1122 return -EPIPE; in put_chars()
1126 return -ENOMEM; in put_chars()
1151 return -EPIPE; in get_chars()
1154 BUG_ON(!port->in_vq); in get_chars()
1163 /* The port could have been hot-unplugged */ in resize_console()
1167 vdev = port->portdev->vdev; in resize_console()
1172 hvc_resize(port->cons.hvc, port->cons.ws); in resize_console()
1180 port = find_port_by_vtermno(hp->vtermno); in notifier_add_vio()
1182 return -EINVAL; in notifier_add_vio()
1184 hp->irq_requested = 1; in notifier_add_vio()
1192 hp->irq_requested = 0; in notifier_del_vio()
1195 /* The operations for console ports. */
1205 * Console drivers are initialized very early so boot messages can go
1209 * At this stage, the console is output-only. It's too early to set
1210 * up a virtqueue, so we let the drivers do some boutique early-output
1224 * The Host's telling us this port is a console port. Hook it in init_port_console()
1225 * up with an hvc console. in init_port_console()
1227 * To set up and manage our virtual console, we call in init_port_console()
1230 * The first argument of hvc_alloc() is the virtual console in init_port_console()
1237 * pointers. The final argument is the output buffer size: we in init_port_console()
1238 * can do any size, so we put PAGE_SIZE here. in init_port_console()
1244 port->cons.vtermno = ret; in init_port_console()
1245 port->cons.hvc = hvc_alloc(port->cons.vtermno, 0, &hv_ops, PAGE_SIZE); in init_port_console()
1246 if (IS_ERR(port->cons.hvc)) { in init_port_console()
1247 ret = PTR_ERR(port->cons.hvc); in init_port_console()
1248 dev_err(port->dev, in init_port_console()
1250 port->cons.hvc = NULL; in init_port_console()
1251 ida_free(&vtermno_ida, port->cons.vtermno); in init_port_console()
1255 list_add_tail(&port->cons.list, &pdrvdata.consoles); in init_port_console()
1257 port->guest_connected = true; in init_port_console()
1260 * Start using the new console output if this is the first in init_port_console()
1261 * console to come up. in init_port_console()
1279 return sprintf(buffer, "%s\n", port->name); in show_port_name()
1296 struct port *port = s->private; in port_debugfs_show()
1298 seq_printf(s, "name: %s\n", port->name ? port->name : ""); in port_debugfs_show()
1299 seq_printf(s, "guest_connected: %d\n", port->guest_connected); in port_debugfs_show()
1300 seq_printf(s, "host_connected: %d\n", port->host_connected); in port_debugfs_show()
1301 seq_printf(s, "outvq_full: %d\n", port->outvq_full); in port_debugfs_show()
1302 seq_printf(s, "bytes_sent: %lu\n", port->stats.bytes_sent); in port_debugfs_show()
1303 seq_printf(s, "bytes_received: %lu\n", port->stats.bytes_received); in port_debugfs_show()
1304 seq_printf(s, "bytes_discarded: %lu\n", port->stats.bytes_discarded); in port_debugfs_show()
1307 seq_printf(s, "console_vtermno: %u\n", port->cons.vtermno); in port_debugfs_show()
1319 port->cons.ws.ws_row = rows; in set_console_size()
1320 port->cons.ws.ws_col = cols; in set_console_size()
1331 buf = alloc_buf(vq->vdev, PAGE_SIZE, 0); in fill_queue()
1333 return -ENOMEM; in fill_queue()
1351 if (port->async_queue && port->guest_connected) in send_sigio_to_port()
1352 kill_fasync(&port->async_queue, SIGIO, POLL_OUT); in send_sigio_to_port()
1364 err = -ENOMEM; in add_port()
1367 kref_init(&port->kref); in add_port()
1369 port->portdev = portdev; in add_port()
1370 port->id = id; in add_port()
1372 port->name = NULL; in add_port()
1373 port->inbuf = NULL; in add_port()
1374 port->cons.hvc = NULL; in add_port()
1375 port->async_queue = NULL; in add_port()
1377 port->cons.ws.ws_row = port->cons.ws.ws_col = 0; in add_port()
1378 port->cons.vtermno = 0; in add_port()
1380 port->host_connected = port->guest_connected = false; in add_port()
1381 port->stats = (struct port_stats) { 0 }; in add_port()
1383 port->outvq_full = false; in add_port()
1385 port->in_vq = portdev->in_vqs[port->id]; in add_port()
1386 port->out_vq = portdev->out_vqs[port->id]; in add_port()
1388 port->cdev = cdev_alloc(); in add_port()
1389 if (!port->cdev) { in add_port()
1390 dev_err(&port->portdev->vdev->dev, "Error allocating cdev\n"); in add_port()
1391 err = -ENOMEM; in add_port()
1394 port->cdev->ops = &port_fops; in add_port()
1396 devt = MKDEV(portdev->chr_major, id); in add_port()
1397 err = cdev_add(port->cdev, devt, 1); in add_port()
1399 dev_err(&port->portdev->vdev->dev, in add_port()
1403 port->dev = device_create(&port_class, &port->portdev->vdev->dev, in add_port()
1405 port->portdev->vdev->index, id); in add_port()
1406 if (IS_ERR(port->dev)) { in add_port()
1407 err = PTR_ERR(port->dev); in add_port()
1408 dev_err(&port->portdev->vdev->dev, in add_port()
1414 spin_lock_init(&port->inbuf_lock); in add_port()
1415 spin_lock_init(&port->outvq_lock); in add_port()
1416 init_waitqueue_head(&port->waitqueue); in add_port()
1422 err = fill_queue(port->in_vq, &port->inbuf_lock); in add_port()
1423 if (err < 0 && err != -ENOSPC) { in add_port()
1424 dev_err(port->dev, "Error allocating inbufs\n"); in add_port()
1428 if (is_rproc_serial(port->portdev->vdev)) in add_port()
1431 * rproc_serial does not want the console port, only in add_port()
1434 port->host_connected = true; in add_port()
1435 else if (!use_multiport(port->portdev)) { in add_port()
1438 * this has to be a console port. in add_port()
1445 spin_lock_irq(&portdev->ports_lock); in add_port()
1446 list_add_tail(&port->list, &port->portdev->ports); in add_port()
1447 spin_unlock_irq(&portdev->ports_lock); in add_port()
1452 * caching, whether this is a console port, etc.) in add_port()
1461 port->portdev->vdev->index, id); in add_port()
1462 port->debugfs_file = debugfs_create_file(debugfs_name, 0444, in add_port()
1469 device_destroy(&port_class, port->dev->devt); in add_port()
1471 cdev_del(port->cdev); in add_port()
1480 /* No users remain, remove all port-specific data. */
1492 spin_lock_irq(&port->inbuf_lock); in remove_port_data()
1495 spin_unlock_irq(&port->inbuf_lock); in remove_port_data()
1497 spin_lock_irq(&port->outvq_lock); in remove_port_data()
1499 spin_unlock_irq(&port->outvq_lock); in remove_port_data()
1509 spin_lock_irq(&port->portdev->ports_lock); in unplug_port()
1510 list_del(&port->list); in unplug_port()
1511 spin_unlock_irq(&port->portdev->ports_lock); in unplug_port()
1513 spin_lock_irq(&port->inbuf_lock); in unplug_port()
1514 if (port->guest_connected) { in unplug_port()
1519 port->guest_connected = false; in unplug_port()
1520 port->host_connected = false; in unplug_port()
1522 wake_up_interruptible(&port->waitqueue); in unplug_port()
1524 spin_unlock_irq(&port->inbuf_lock); in unplug_port()
1528 list_del(&port->cons.list); in unplug_port()
1530 hvc_remove(port->cons.hvc); in unplug_port()
1531 ida_free(&vtermno_ida, port->cons.vtermno); in unplug_port()
1537 * We should just assume the device itself has gone off -- in unplug_port()
1541 port->portdev = NULL; in unplug_port()
1543 sysfs_remove_group(&port->dev->kobj, &port_attribute_group); in unplug_port()
1544 device_destroy(&port_class, port->dev->devt); in unplug_port()
1545 cdev_del(port->cdev); in unplug_port()
1547 debugfs_remove(port->debugfs_file); in unplug_port()
1548 kfree(port->name); in unplug_port()
1551 * Locks around here are not necessary - a port can't be in unplug_port()
1555 kref_put(&port->kref, remove_port); in unplug_port()
1568 cpkt = (struct virtio_console_control *)(buf->buf + buf->offset); in handle_control_message()
1570 port = find_port_by_id(portdev, virtio32_to_cpu(vdev, cpkt->id)); in handle_control_message()
1572 cpkt->event != cpu_to_virtio16(vdev, VIRTIO_CONSOLE_PORT_ADD)) { in handle_control_message()
1574 dev_dbg(&portdev->vdev->dev, in handle_control_message()
1575 "Invalid index %u in control packet\n", cpkt->id); in handle_control_message()
1579 switch (virtio16_to_cpu(vdev, cpkt->event)) { in handle_control_message()
1582 dev_dbg(&portdev->vdev->dev, in handle_control_message()
1583 "Port %u already added\n", port->id); in handle_control_message()
1587 if (virtio32_to_cpu(vdev, cpkt->id) >= in handle_control_message()
1588 portdev->max_nr_ports) { in handle_control_message()
1589 dev_warn(&portdev->vdev->dev, in handle_control_message()
1591 "out-of-bound id %u, max. supported id: %u\n", in handle_control_message()
1592 cpkt->id, portdev->max_nr_ports - 1); in handle_control_message()
1595 add_port(portdev, virtio32_to_cpu(vdev, cpkt->id)); in handle_control_message()
1601 if (!cpkt->value) in handle_control_message()
1609 * Could remove the port here in case init fails - but in handle_control_message()
1617 } size; in handle_control_message() local
1622 memcpy(&size, buf->buf + buf->offset + sizeof(*cpkt), in handle_control_message()
1623 sizeof(size)); in handle_control_message()
1624 set_console_size(port, size.rows, size.cols); in handle_control_message()
1626 port->cons.hvc->irq_requested = 1; in handle_control_message()
1631 port->host_connected = virtio16_to_cpu(vdev, cpkt->value); in handle_control_message()
1632 wake_up_interruptible(&port->waitqueue); in handle_control_message()
1638 spin_lock_irq(&port->outvq_lock); in handle_control_message()
1640 spin_unlock_irq(&port->outvq_lock); in handle_control_message()
1646 spin_lock_irq(&port->inbuf_lock); in handle_control_message()
1648 spin_unlock_irq(&port->inbuf_lock); in handle_control_message()
1655 if (port->name) in handle_control_message()
1659 * Skip the size of the header and the cpkt to get the size in handle_control_message()
1662 name_size = buf->len - buf->offset - sizeof(*cpkt) + 1; in handle_control_message()
1664 port->name = kmalloc(name_size, GFP_KERNEL); in handle_control_message()
1665 if (!port->name) { in handle_control_message()
1666 dev_err(port->dev, in handle_control_message()
1670 strscpy(port->name, buf->buf + buf->offset + sizeof(*cpkt), in handle_control_message()
1677 err = sysfs_create_group(&port->dev->kobj, in handle_control_message()
1680 dev_err(port->dev, in handle_control_message()
1689 kobject_uevent(&port->dev->kobj, KOBJ_CHANGE); in handle_control_message()
1703 vq = portdev->c_ivq; in control_work_handler()
1705 spin_lock(&portdev->c_ivq_lock); in control_work_handler()
1707 spin_unlock(&portdev->c_ivq_lock); in control_work_handler()
1709 buf->len = min_t(size_t, len, buf->size); in control_work_handler()
1710 buf->offset = 0; in control_work_handler()
1712 handle_control_message(vq->vdev, portdev, buf); in control_work_handler()
1714 spin_lock(&portdev->c_ivq_lock); in control_work_handler()
1715 if (add_inbuf(portdev->c_ivq, buf) < 0) { in control_work_handler()
1716 dev_warn(&portdev->vdev->dev, in control_work_handler()
1721 spin_unlock(&portdev->c_ivq_lock); in control_work_handler()
1737 port = find_port_by_vq(vq->vdev->priv, vq); in out_intr()
1743 wake_up_interruptible(&port->waitqueue); in out_intr()
1751 port = find_port_by_vq(vq->vdev->priv, vq); in in_intr()
1757 spin_lock_irqsave(&port->inbuf_lock, flags); in in_intr()
1758 port->inbuf = get_inbuf(port); in in_intr()
1764 * can be reached when a console port is not yet connected (no in in_intr()
1770 * while console ports and rproc-serial ports accepts data at in in_intr()
1771 * any time. rproc-serial is initiated with guest_connected to in in_intr()
1772 * false because port_fops_open expects this. Console ports are in in_intr()
1773 * hooked up with an HVC console and is initialized with in in_intr()
1777 if (!port->guest_connected && !is_rproc_serial(port->portdev->vdev)) in in_intr()
1783 spin_unlock_irqrestore(&port->inbuf_lock, flags); in in_intr()
1785 wake_up_interruptible(&port->waitqueue); in in_intr()
1787 if (is_console_port(port) && hvc_poll(port->cons.hvc)) in in_intr()
1795 portdev = vq->vdev->priv; in control_intr()
1796 schedule_work(&portdev->control_work); in control_intr()
1803 portdev = vdev->priv; in config_intr()
1806 schedule_work(&portdev->config_work); in config_intr()
1819 vdev = portdev->vdev; in config_work_handler()
1830 * to indicate console size changes so that it can be in config_work_handler()
1831 * done per-port. in config_work_handler()
1845 nr_ports = portdev->max_nr_ports; in init_vqs()
1852 portdev->in_vqs = kmalloc_array(nr_ports, sizeof(struct virtqueue *), in init_vqs()
1854 portdev->out_vqs = kmalloc_array(nr_ports, sizeof(struct virtqueue *), in init_vqs()
1856 if (!vqs || !io_callbacks || !io_names || !portdev->in_vqs || in init_vqs()
1857 !portdev->out_vqs) { in init_vqs()
1858 err = -ENOMEM; in init_vqs()
1864 * spawns a console port first and also inits the vqs for port in init_vqs()
1877 io_names[j] = "control-i"; in init_vqs()
1878 io_names[j + 1] = "control-o"; in init_vqs()
1889 err = virtio_find_vqs(portdev->vdev, nr_queues, vqs, in init_vqs()
1896 portdev->in_vqs[0] = vqs[0]; in init_vqs()
1897 portdev->out_vqs[0] = vqs[1]; in init_vqs()
1900 portdev->c_ivq = vqs[j]; in init_vqs()
1901 portdev->c_ovq = vqs[j + 1]; in init_vqs()
1905 portdev->in_vqs[i] = vqs[j]; in init_vqs()
1906 portdev->out_vqs[i] = vqs[j + 1]; in init_vqs()
1916 kfree(portdev->out_vqs); in init_vqs()
1917 kfree(portdev->in_vqs); in init_vqs()
1933 virtio_device_for_each_vq(portdev->vdev, vq) { in remove_vqs()
1941 portdev->vdev->config->del_vqs(portdev->vdev); in remove_vqs()
1942 kfree(portdev->in_vqs); in remove_vqs()
1943 kfree(portdev->out_vqs); in remove_vqs()
1951 portdev = vdev->priv; in virtcons_remove()
1954 list_del(&portdev->list); in virtcons_remove()
1960 flush_work(&portdev->control_work); in virtcons_remove()
1962 flush_work(&portdev->config_work); in virtcons_remove()
1968 cancel_work_sync(&portdev->control_work); in virtcons_remove()
1970 cancel_work_sync(&portdev->config_work); in virtcons_remove()
1972 list_for_each_entry_safe(port, port2, &portdev->ports, list) in virtcons_remove()
1975 unregister_chrdev(portdev->chr_major, "virtio-portsdev"); in virtcons_remove()
1979 * (device-side) queues. So there's no point in keeping the in virtcons_remove()
1993 * If the host also supports multiple console ports, we check the
2005 if (!vdev->config->get && in virtcons_probe()
2008 dev_err(&vdev->dev, "%s failure: config access disabled\n", in virtcons_probe()
2010 return -EINVAL; in virtcons_probe()
2018 err = -ENOMEM; in virtcons_probe()
2022 /* Attach this portdev to this virtio_device, and vice-versa. */ in virtcons_probe()
2023 portdev->vdev = vdev; in virtcons_probe()
2024 vdev->priv = portdev; in virtcons_probe()
2026 portdev->chr_major = register_chrdev(0, "virtio-portsdev", in virtcons_probe()
2028 if (portdev->chr_major < 0) { in virtcons_probe()
2029 dev_err(&vdev->dev, in virtcons_probe()
2031 portdev->chr_major, vdev->index); in virtcons_probe()
2032 err = portdev->chr_major; in virtcons_probe()
2037 portdev->max_nr_ports = 1; in virtcons_probe()
2043 &portdev->max_nr_ports) == 0) { in virtcons_probe()
2044 if (portdev->max_nr_ports == 0 || in virtcons_probe()
2045 portdev->max_nr_ports > VIRTCONS_MAX_PORTS) { in virtcons_probe()
2046 dev_err(&vdev->dev, in virtcons_probe()
2048 portdev->max_nr_ports); in virtcons_probe()
2049 err = -EINVAL; in virtcons_probe()
2057 dev_err(&vdev->dev, "Error %d initializing vqs\n", err); in virtcons_probe()
2061 spin_lock_init(&portdev->ports_lock); in virtcons_probe()
2062 INIT_LIST_HEAD(&portdev->ports); in virtcons_probe()
2063 INIT_LIST_HEAD(&portdev->list); in virtcons_probe()
2065 virtio_device_ready(portdev->vdev); in virtcons_probe()
2067 INIT_WORK(&portdev->config_work, &config_work_handler); in virtcons_probe()
2068 INIT_WORK(&portdev->control_work, &control_work_handler); in virtcons_probe()
2071 spin_lock_init(&portdev->c_ivq_lock); in virtcons_probe()
2072 spin_lock_init(&portdev->c_ovq_lock); in virtcons_probe()
2074 err = fill_queue(portdev->c_ivq, &portdev->c_ivq_lock); in virtcons_probe()
2076 dev_err(&vdev->dev, in virtcons_probe()
2090 * For backward compatibility: Create a console port in virtcons_probe()
2097 list_add_tail(&portdev->list, &pdrvdata.portdevs); in virtcons_probe()
2104 * If there was an early virtio console, assume that there are no in virtcons_probe()
2107 * a "Warning: unable to open an initial console" boot failure. in virtcons_probe()
2109 * this might take some host<->guest communication - thus we have to in virtcons_probe()
2118 unregister_chrdev(portdev->chr_major, "virtio-portsdev"); in virtcons_probe()
2153 portdev = vdev->priv; in virtcons_freeze()
2158 virtqueue_disable_cb(portdev->c_ivq); in virtcons_freeze()
2159 cancel_work_sync(&portdev->control_work); in virtcons_freeze()
2160 cancel_work_sync(&portdev->config_work); in virtcons_freeze()
2166 virtqueue_disable_cb(portdev->c_ivq); in virtcons_freeze()
2168 list_for_each_entry(port, &portdev->ports, list) { in virtcons_freeze()
2169 virtqueue_disable_cb(port->in_vq); in virtcons_freeze()
2170 virtqueue_disable_cb(port->out_vq); in virtcons_freeze()
2175 port->host_connected = false; in virtcons_freeze()
2189 portdev = vdev->priv; in virtcons_restore()
2195 virtio_device_ready(portdev->vdev); in virtcons_restore()
2198 fill_queue(portdev->c_ivq, &portdev->c_ivq_lock); in virtcons_restore()
2200 list_for_each_entry(port, &portdev->ports, list) { in virtcons_restore()
2201 port->in_vq = portdev->in_vqs[port->id]; in virtcons_restore()
2202 port->out_vq = portdev->out_vqs[port->id]; in virtcons_restore()
2204 fill_queue(port->in_vq, &port->inbuf_lock); in virtcons_restore()
2213 if (port->guest_connected) in virtcons_restore()
2253 pdrvdata.debugfs_dir = debugfs_create_dir("virtio-ports", NULL); in virtio_console_init()
2290 MODULE_DESCRIPTION("Virtio console driver");