23 #include <linux/device.h>
30 #include <linux/list.h>
31 #include <linux/poll.h>
32 #include <linux/sched.h>
33 #include <linux/slab.h>
35 #include <linux/virtio.h>
36 #include <linux/virtio_console.h>
37 #include <linux/wait.h>
39 #include <linux/module.h>
40 #include "../tty/hvc/hvc_console.h"
233 static int (*early_put_chars)(
u32,
const char *,
int);
235 static struct port *find_port_by_vtermno(
u32 vtermno)
243 if (cons->
vtermno == vtermno) {
250 spin_unlock_irqrestore(&pdrvdata_lock, flags);
262 if (port->
cdev->dev == dev)
266 spin_unlock_irqrestore(&portdev->ports_lock, flags);
279 port = find_port_by_devt_in_portdev(portdev, dev);
285 spin_unlock_irqrestore(&pdrvdata_lock, flags);
289 static struct port *find_port_by_id(
struct ports_device *portdev,
u32 id)
300 spin_unlock_irqrestore(&portdev->ports_lock, flags);
317 spin_unlock_irqrestore(&portdev->ports_lock, flags);
321 static
bool is_console_port(
struct port *port)
328 static inline bool use_multiport(
struct ports_device *portdev)
367 static struct port_buffer *get_inbuf(
struct port *port)
403 static void discard_port_data(
struct port *port)
412 buf = get_inbuf(port);
417 if (add_inbuf(port->
in_vq, buf) < 0) {
422 buf = get_inbuf(port);
425 dev_warn(port->
dev,
"Errors adding %d buffers back to vq\n",
429 static bool port_has_data(
struct port *port)
436 port->
inbuf = get_inbuf(port);
440 spin_unlock_irqrestore(&port->
inbuf_lock, flags);
452 if (!use_multiport(portdev))
470 static ssize_t send_control_msg(
struct port *port,
unsigned int event,
475 return __send_control_msg(port->
portdev, port->
id, event, value);
493 for (i = 0; i <
nrpages; i++) {
494 page = sg_page(&sg[i]);
503 static void reclaim_consumed_buffers(
struct port *port)
523 int nents,
size_t in_count,
535 reclaim_consumed_buffers(port);
565 spin_unlock_irqrestore(&port->
outvq_lock, flags);
567 port->
stats.bytes_sent += in_count;
575 static ssize_t send_buf(
struct port *port,
void *in_buf,
size_t in_count,
589 return __send_to_port(port, sg, 1, in_count, tok, nonblock);
593 size_t in_count,
bool nonblock)
603 return __send_to_port(port, sg, nents, in_count, tok, nonblock);
610 static ssize_t fill_readbuf(
struct port *port,
char *out_buf,
size_t out_count,
616 if (!out_count || !port_has_data(port))
642 if (add_inbuf(port->
in_vq, buf) < 0)
645 spin_unlock_irqrestore(&port->
inbuf_lock, flags);
652 static bool will_read_block(
struct port *port)
661 static bool will_write_block(
struct port *port)
677 reclaim_consumed_buffers(port);
684 static ssize_t port_fops_read(
struct file *filp,
char __user *ubuf,
685 size_t count, loff_t *offp)
692 if (!port_has_data(port)) {
704 !will_read_block(port));
724 return fill_readbuf(port, ubuf, count,
true);
727 static int wait_port_writable(
struct port *port,
bool nonblock)
731 if (will_write_block(port)) {
736 !will_write_block(port));
747 static ssize_t port_fops_write(
struct file *filp,
const char __user *ubuf,
748 size_t count, loff_t *offp)
763 ret = wait_port_writable(port, nonblock);
767 count =
min((
size_t)(32 * 1024), count);
787 ret = send_buf(port, buf, count, nonblock);
789 if (nonblock && ret > 0)
811 if (sgl->
n == sgl->
size)
815 if (buf->
ops->steal(pipe, buf) == 0) {
821 sg_set_page(&(sgl->
sg[sgl->
n]), buf->
page, len, buf->
offset);
825 char *
src = buf->
ops->map(pipe, buf, 1);
841 buf->
ops->unmap(pipe, buf, src);
843 sg_set_page(&(sgl->
sg[sgl->
n]), page, len, offset);
853 struct file *filp, loff_t *ppos,
854 size_t len,
unsigned int flags)
880 ret = send_pages(port, sgl.
sg, sgl.
n, sgl.
len,
true);
898 if (!will_read_block(port))
900 if (!will_write_block(port))
908 static void remove_port(
struct kref *
kref);
910 static int port_fops_release(
struct inode *
inode,
struct file *filp)
922 discard_port_data(port);
927 reclaim_consumed_buffers(port);
938 kref_put(&port->
kref, remove_port);
949 port = find_port_by_devt(cdev->
dev);
953 spin_lock_irq(&port->
portdev->ports_lock);
954 kref_get(&port->
kref);
955 spin_unlock_irq(&port->
portdev->ports_lock);
961 if (is_console_port(port)) {
983 reclaim_consumed_buffers(port);
993 kref_put(&port->
kref, remove_port);
997 static int port_fops_fasync(
int fd,
struct file *filp,
int mode)
1013 .open = port_fops_open,
1014 .read = port_fops_read,
1015 .write = port_fops_write,
1016 .splice_write = port_fops_splice_write,
1017 .poll = port_fops_poll,
1018 .release = port_fops_release,
1019 .fasync = port_fops_fasync,
1031 static int put_chars(
u32 vtermno,
const char *buf,
int count)
1036 return early_put_chars(vtermno, buf, count);
1038 port = find_port_by_vtermno(vtermno);
1042 return send_buf(port, (
void *)buf, count,
false);
1052 static int get_chars(
u32 vtermno,
char *buf,
int count)
1060 port = find_port_by_vtermno(vtermno);
1067 return fill_readbuf(port, buf, count,
false);
1070 static void resize_console(
struct port *port)
1075 if (!port || !is_console_port(port))
1080 hvc_resize(port->
cons.hvc, port->
cons.ws);
1088 port = find_port_by_vtermno(hp->
vtermno);
1093 resize_console(port);
1098 static void notifier_del_vio(
struct hvc_struct *hp,
int data)
1107 .notifier_add = notifier_add_vio,
1108 .notifier_del = notifier_del_vio,
1109 .notifier_hangup = notifier_del_vio,
1148 port->
cons.vtermno = pdrvdata.next_vtermno;
1151 if (IS_ERR(port->
cons.hvc)) {
1152 ret = PTR_ERR(port->
cons.hvc);
1154 "error %d allocating hvc for port\n", ret);
1158 spin_lock_irq(&pdrvdata_lock);
1159 pdrvdata.next_vtermno++;
1161 spin_unlock_irq(&pdrvdata_lock);
1168 if (early_put_chars)
1169 early_put_chars =
NULL;
1189 static struct attribute *port_sysfs_entries[] = {
1190 &dev_attr_name.attr,
1196 .attrs = port_sysfs_entries,
1199 static ssize_t debugfs_read(
struct file *filp,
char __user *ubuf,
1200 size_t count, loff_t *offp)
1213 out_offset +=
snprintf(buf + out_offset, out_count,
1214 "name: %s\n", port->
name ? port->
name :
"");
1215 out_offset +=
snprintf(buf + out_offset, out_count - out_offset,
1217 out_offset +=
snprintf(buf + out_offset, out_count - out_offset,
1219 out_offset +=
snprintf(buf + out_offset, out_count - out_offset,
1221 out_offset +=
snprintf(buf + out_offset, out_count - out_offset,
1222 "bytes_sent: %lu\n", port->
stats.bytes_sent);
1223 out_offset +=
snprintf(buf + out_offset, out_count - out_offset,
1224 "bytes_received: %lu\n",
1225 port->
stats.bytes_received);
1226 out_offset +=
snprintf(buf + out_offset, out_count - out_offset,
1227 "bytes_discarded: %lu\n",
1228 port->
stats.bytes_discarded);
1229 out_offset +=
snprintf(buf + out_offset, out_count - out_offset,
1231 is_console_port(port) ?
"yes" :
"no");
1232 out_offset +=
snprintf(buf + out_offset, out_count - out_offset,
1233 "console_vtermno: %u\n", port->
cons.vtermno);
1243 .read = debugfs_read,
1246 static void set_console_size(
struct port *port,
u16 rows,
u16 cols)
1248 if (!port || !is_console_port(port))
1258 unsigned int nr_added_bufs;
1267 spin_lock_irq(lock);
1268 ret = add_inbuf(vq, buf);
1270 spin_unlock_irq(lock);
1275 spin_unlock_irq(lock);
1278 return nr_added_bufs;
1281 static void send_sigio_to_port(
struct port *port)
1289 char debugfs_name[16];
1293 unsigned int nr_added_bufs;
1301 kref_init(&port->
kref);
1311 port->
cons.ws.ws_row = port->
cons.ws.ws_col = 0;
1327 port->
cdev->ops = &port_fops;
1333 "Error %d adding cdev for port %u\n", err,
id);
1337 devt, port,
"vport%up%u",
1338 port->
portdev->drv_index,
id);
1339 if (IS_ERR(port->
dev)) {
1340 err = PTR_ERR(port->
dev);
1342 "Error %d creating device for port %u\n",
1353 if (!nr_added_bufs) {
1354 dev_err(port->
dev,
"Error allocating inbufs\n");
1362 if (!use_multiport(port->
portdev)) {
1379 if (pdrvdata.debugfs_dir) {
1384 sprintf(debugfs_name,
"vport%up%u",
1385 port->
portdev->drv_index,
id);
1387 pdrvdata.debugfs_dir,
1409 static void remove_port(
struct kref *
kref)
1426 static void remove_port_data(
struct port *port)
1431 discard_port_data(port);
1433 reclaim_consumed_buffers(port);
1445 static void unplug_port(
struct port *port)
1447 spin_lock_irq(&port->
portdev->ports_lock);
1449 spin_unlock_irq(&port->
portdev->ports_lock);
1457 send_sigio_to_port(port);
1460 if (is_console_port(port)) {
1461 spin_lock_irq(&pdrvdata_lock);
1463 spin_unlock_irq(&pdrvdata_lock);
1467 remove_port_data(port);
1481 kref_put(&port->
kref, remove_port);
1485 static void handle_control_message(
struct ports_device *portdev,
1495 port = find_port_by_id(portdev, cpkt->
id);
1499 "Invalid index %u in control packet\n", cpkt->
id);
1503 switch (cpkt->
event) {
1507 "Port %u already added\n", port->
id);
1511 if (cpkt->
id >= portdev->
config.max_nr_ports) {
1513 "Request for adding port with out-of-bound id %u, max. supported id: %u\n",
1514 cpkt->
id, portdev->
config.max_nr_ports - 1);
1517 add_port(portdev, cpkt->
id);
1525 if (is_console_port(port))
1541 if (!is_console_port(port))
1546 set_console_size(port,
size.rows,
size.cols);
1548 port->
cons.hvc->irq_requested = 1;
1549 resize_console(port);
1561 reclaim_consumed_buffers(port);
1568 send_sigio_to_port(port);
1582 name_size = buf->
len - buf->
offset -
sizeof(*cpkt) + 1;
1587 "Not enough space to store port name\n");
1592 port->
name[name_size - 1] = 0;
1599 &port_attribute_group);
1602 "Error %d creating sysfs device attributes\n",
1624 vq = portdev->
c_ivq;
1633 handle_control_message(portdev, buf);
1636 if (add_inbuf(portdev->
c_ivq, buf) < 0) {
1638 "Error adding buffer to queue\n");
1645 static void out_intr(
struct virtqueue *vq)
1649 port = find_port_by_vq(vq->
vdev->priv, vq);
1656 static void in_intr(
struct virtqueue *vq)
1659 unsigned long flags;
1661 port = find_port_by_vq(vq->
vdev->priv, vq);
1666 port->
inbuf = get_inbuf(port);
1676 discard_port_data(port);
1678 spin_unlock_irqrestore(&port->
inbuf_lock, flags);
1683 send_sigio_to_port(port);
1689 static void control_intr(
struct virtqueue *vq)
1693 portdev = vq->
vdev->priv;
1701 portdev = vdev->
priv;
1703 if (!use_multiport(portdev)) {
1709 &cols,
sizeof(
u16));
1712 &rows,
sizeof(
u16));
1714 port = find_port_by_id(portdev, 0);
1715 set_console_size(port, rows, cols);
1724 resize_console(port);
1733 u32 i,
j, nr_ports, nr_queues;
1736 nr_ports = portdev->
config.max_nr_ports;
1737 nr_queues = use_multiport(portdev) ? (nr_ports + 1) * 2 : 2;
1746 if (!vqs || !io_callbacks || !io_names || !portdev->
in_vqs ||
1758 io_callbacks[
j] = in_intr;
1759 io_callbacks[j + 1] = out_intr;
1760 io_names[
j] =
"input";
1761 io_names[j + 1] =
"output";
1764 if (use_multiport(portdev)) {
1765 io_callbacks[
j] = control_intr;
1766 io_callbacks[j + 1] =
NULL;
1767 io_names[
j] =
"control-i";
1768 io_names[j + 1] =
"control-o";
1770 for (i = 1; i < nr_ports; i++) {
1772 io_callbacks[
j] = in_intr;
1773 io_callbacks[j + 1] = out_intr;
1774 io_names[
j] =
"input";
1775 io_names[j + 1] =
"output";
1779 err = portdev->
vdev->config->find_vqs(portdev->
vdev, nr_queues, vqs,
1781 (
const char **)io_names);
1786 portdev->
in_vqs[0] = vqs[0];
1789 if (use_multiport(portdev)) {
1791 portdev->
c_ovq = vqs[j + 1];
1793 for (i = 1; i < nr_ports; i++) {
1800 kfree(io_callbacks);
1809 kfree(io_callbacks);
1821 portdev->
vdev->config->del_vqs(portdev->
vdev);
1826 static void remove_controlq_data(
struct ports_device *portdev)
1831 if (!use_multiport(portdev))
1854 bool early = early_put_chars !=
NULL;
1867 vdev->
priv = portdev;
1869 spin_lock_irq(&pdrvdata_lock);
1871 spin_unlock_irq(&pdrvdata_lock);
1873 portdev->
chr_major = register_chrdev(0,
"virtio-portsdev",
1877 "Error %d registering chrdev for device %u\n",
1884 portdev->
config.max_nr_ports = 1;
1888 &portdev->
config.max_nr_ports) == 0)
1891 err = init_vqs(portdev);
1893 dev_err(&vdev->
dev,
"Error %d initializing vqs\n", err);
1898 INIT_LIST_HEAD(&portdev->
ports);
1901 unsigned int nr_added_bufs;
1906 nr_added_bufs = fill_queue(portdev->
c_ivq, &portdev->
cvq_lock);
1907 if (!nr_added_bufs) {
1909 "Error allocating buffers for control queue\n");
1918 add_port(portdev, 0);
1921 spin_lock_irq(&pdrvdata_lock);
1923 spin_unlock_irq(&pdrvdata_lock);
1937 if (multiport && early)
1946 remove_vqs(portdev);
1948 unregister_chrdev(portdev->
chr_major,
"virtio-portsdev");
1958 struct port *
port, *port2;
1960 portdev = vdev->
priv;
1962 spin_lock_irq(&pdrvdata_lock);
1964 spin_unlock_irq(&pdrvdata_lock);
1967 vdev->
config->reset(vdev);
1974 unregister_chrdev(portdev->chr_major, "virtio-portsdev");
1984 remove_controlq_data(portdev);
1985 remove_vqs(portdev);
2005 portdev = vdev->
priv;
2007 vdev->
config->reset(vdev);
2016 remove_controlq_data(portdev);
2026 remove_port_data(port);
2028 remove_vqs(portdev);
2039 portdev = vdev->
priv;
2041 ret = init_vqs(portdev);
2045 if (use_multiport(portdev))
2071 .driver.name = KBUILD_MODNAME,
2074 .probe = virtcons_probe,
2075 .remove = virtcons_remove,
2076 .config_changed = config_intr,
2078 .freeze = virtcons_freeze,
2079 .restore = virtcons_restore,
2088 if (IS_ERR(pdrvdata.class)) {
2089 err = PTR_ERR(pdrvdata.class);
2090 pr_err(
"Error %d creating virtio-ports class\n", err);
2095 if (!pdrvdata.debugfs_dir) {
2096 pr_warning(
"Error %ld creating debugfs dir for virtio-ports\n",
2097 PTR_ERR(pdrvdata.debugfs_dir));
2099 INIT_LIST_HEAD(&pdrvdata.consoles);
2100 INIT_LIST_HEAD(&pdrvdata.portdevs);
2104 pr_err(
"Error %d registering virtio driver\n", err);
2109 if (pdrvdata.debugfs_dir)
2115 static void __exit fini(
void)
2120 if (pdrvdata.debugfs_dir)