Lines Matching refs:port

166 struct port {  struct
233 static struct port *find_port_by_vtermno(u32 vtermno) in find_port_by_vtermno() argument
235 struct port *port; in find_port_by_vtermno() local
242 port = container_of(cons, struct port, cons); in find_port_by_vtermno()
246 port = NULL; in find_port_by_vtermno()
249 return port; in find_port_by_vtermno()
252 static struct port *find_port_by_devt_in_portdev(struct ports_device *portdev, in find_port_by_devt_in_portdev()
255 struct port *port; in find_port_by_devt_in_portdev() local
259 list_for_each_entry(port, &portdev->ports, list) { in find_port_by_devt_in_portdev()
260 if (port->cdev->dev == dev) { in find_port_by_devt_in_portdev()
261 kref_get(&port->kref); in find_port_by_devt_in_portdev()
265 port = NULL; in find_port_by_devt_in_portdev()
269 return port; in find_port_by_devt_in_portdev()
272 static struct port *find_port_by_devt(dev_t dev) in find_port_by_devt()
275 struct port *port; in find_port_by_devt() local
280 port = find_port_by_devt_in_portdev(portdev, dev); in find_port_by_devt()
281 if (port) in find_port_by_devt()
284 port = NULL; in find_port_by_devt()
287 return port; in find_port_by_devt()
290 static struct port *find_port_by_id(struct ports_device *portdev, u32 id) in find_port_by_id()
292 struct port *port; in find_port_by_id() local
296 list_for_each_entry(port, &portdev->ports, list) in find_port_by_id()
297 if (port->id == id) in find_port_by_id()
299 port = NULL; in find_port_by_id()
303 return port; in find_port_by_id()
306 static struct port *find_port_by_vq(struct ports_device *portdev, in find_port_by_vq()
309 struct port *port; in find_port_by_vq() local
313 list_for_each_entry(port, &portdev->ports, list) in find_port_by_vq()
314 if (port->in_vq == vq || port->out_vq == vq) in find_port_by_vq()
316 port = NULL; in find_port_by_vq()
319 return port; in find_port_by_vq()
322 static bool is_console_port(struct port *port) in is_console_port() argument
324 if (port->cons.hvc) in is_console_port()
459 static struct port_buffer *get_inbuf(struct port *port) in get_inbuf() argument
464 if (port->inbuf) in get_inbuf()
465 return port->inbuf; in get_inbuf()
467 buf = virtqueue_get_buf(port->in_vq, &len); in get_inbuf()
471 port->stats.bytes_received += len; in get_inbuf()
497 static void discard_port_data(struct port *port) in discard_port_data() argument
502 if (!port->portdev) { in discard_port_data()
506 buf = get_inbuf(port); in discard_port_data()
510 port->stats.bytes_discarded += buf->len - buf->offset; in discard_port_data()
511 if (add_inbuf(port->in_vq, buf) < 0) { in discard_port_data()
515 port->inbuf = NULL; in discard_port_data()
516 buf = get_inbuf(port); in discard_port_data()
519 dev_warn(port->dev, "Errors adding %d buffers back to vq\n", in discard_port_data()
523 static bool port_has_data(struct port *port) in port_has_data() argument
529 spin_lock_irqsave(&port->inbuf_lock, flags); in port_has_data()
530 port->inbuf = get_inbuf(port); in port_has_data()
531 if (port->inbuf) in port_has_data()
534 spin_unlock_irqrestore(&port->inbuf_lock, flags); in port_has_data()
569 static ssize_t send_control_msg(struct port *port, unsigned int event, in send_control_msg() argument
573 if (port->portdev) in send_control_msg()
574 return __send_control_msg(port->portdev, port->id, event, value); in send_control_msg()
580 static void reclaim_consumed_buffers(struct port *port) in reclaim_consumed_buffers() argument
585 if (!port->portdev) { in reclaim_consumed_buffers()
589 while ((buf = virtqueue_get_buf(port->out_vq, &len))) { in reclaim_consumed_buffers()
591 port->outvq_full = false; in reclaim_consumed_buffers()
595 static ssize_t __send_to_port(struct port *port, struct scatterlist *sg, in __send_to_port() argument
604 out_vq = port->out_vq; in __send_to_port()
606 spin_lock_irqsave(&port->outvq_lock, flags); in __send_to_port()
608 reclaim_consumed_buffers(port); in __send_to_port()
621 port->outvq_full = true; in __send_to_port()
639 spin_unlock_irqrestore(&port->outvq_lock, flags); in __send_to_port()
641 port->stats.bytes_sent += in_count; in __send_to_port()
653 static ssize_t fill_readbuf(struct port *port, u8 __user *out_buf, in fill_readbuf() argument
659 if (!out_count || !port_has_data(port)) in fill_readbuf()
662 buf = port->inbuf; in fill_readbuf()
683 spin_lock_irqsave(&port->inbuf_lock, flags); in fill_readbuf()
684 port->inbuf = NULL; in fill_readbuf()
686 if (add_inbuf(port->in_vq, buf) < 0) in fill_readbuf()
687 dev_warn(port->dev, "failed add_buf\n"); in fill_readbuf()
689 spin_unlock_irqrestore(&port->inbuf_lock, flags); in fill_readbuf()
696 static bool will_read_block(struct port *port) in will_read_block() argument
698 if (!port->guest_connected) { in will_read_block()
702 return !port_has_data(port) && port->host_connected; in will_read_block()
705 static bool will_write_block(struct port *port) in will_write_block() argument
709 if (!port->guest_connected) { in will_write_block()
713 if (!port->host_connected) in will_write_block()
716 spin_lock_irq(&port->outvq_lock); in will_write_block()
721 reclaim_consumed_buffers(port); in will_write_block()
722 ret = port->outvq_full; in will_write_block()
723 spin_unlock_irq(&port->outvq_lock); in will_write_block()
731 struct port *port; in port_fops_read() local
734 port = filp->private_data; in port_fops_read()
737 if (!port->guest_connected) in port_fops_read()
740 if (!port_has_data(port)) { in port_fops_read()
746 if (!port->host_connected) in port_fops_read()
751 ret = wait_event_freezable(port->waitqueue, in port_fops_read()
752 !will_read_block(port)); in port_fops_read()
757 if (!port->guest_connected) in port_fops_read()
769 if (!port_has_data(port) && !port->host_connected) in port_fops_read()
772 return fill_readbuf(port, ubuf, count, true); in port_fops_read()
775 static int wait_port_writable(struct port *port, bool nonblock) in wait_port_writable() argument
779 if (will_write_block(port)) { in wait_port_writable()
783 ret = wait_event_freezable(port->waitqueue, in wait_port_writable()
784 !will_write_block(port)); in wait_port_writable()
789 if (!port->guest_connected) in wait_port_writable()
798 struct port *port; in port_fops_write() local
808 port = filp->private_data; in port_fops_write()
812 ret = wait_port_writable(port, nonblock); in port_fops_write()
818 buf = alloc_buf(port->portdev->vdev, count, 0); in port_fops_write()
837 ret = __send_to_port(port, sg, 1, count, buf, nonblock); in port_fops_write()
903 struct port *port = filp->private_data; in port_fops_splice_write() local
921 if (is_rproc_serial(port->out_vq->vdev)) in port_fops_splice_write()
929 ret = wait_port_writable(port, filp->f_flags & O_NONBLOCK); in port_fops_splice_write()
934 buf = alloc_buf(port->portdev->vdev, 0, occupancy); in port_fops_splice_write()
949 ret = __send_to_port(port, buf->sg, sgl.n, sgl.len, buf, true); in port_fops_splice_write()
962 struct port *port; in port_fops_poll() local
965 port = filp->private_data; in port_fops_poll()
966 poll_wait(filp, &port->waitqueue, wait); in port_fops_poll()
968 if (!port->guest_connected) { in port_fops_poll()
973 if (!will_read_block(port)) in port_fops_poll()
975 if (!will_write_block(port)) in port_fops_poll()
977 if (!port->host_connected) in port_fops_poll()
987 struct port *port; in port_fops_release() local
989 port = filp->private_data; in port_fops_release()
992 send_control_msg(port, VIRTIO_CONSOLE_PORT_OPEN, 0); in port_fops_release()
994 spin_lock_irq(&port->inbuf_lock); in port_fops_release()
995 port->guest_connected = false; in port_fops_release()
997 discard_port_data(port); in port_fops_release()
999 spin_unlock_irq(&port->inbuf_lock); in port_fops_release()
1001 spin_lock_irq(&port->outvq_lock); in port_fops_release()
1002 reclaim_consumed_buffers(port); in port_fops_release()
1003 spin_unlock_irq(&port->outvq_lock); in port_fops_release()
1014 kref_put(&port->kref, remove_port); in port_fops_release()
1022 struct port *port; in port_fops_open() local
1026 port = find_port_by_devt(cdev->dev); in port_fops_open()
1027 if (!port) { in port_fops_open()
1031 filp->private_data = port; in port_fops_open()
1037 if (is_console_port(port)) { in port_fops_open()
1043 spin_lock_irq(&port->inbuf_lock); in port_fops_open()
1044 if (port->guest_connected) { in port_fops_open()
1045 spin_unlock_irq(&port->inbuf_lock); in port_fops_open()
1050 port->guest_connected = true; in port_fops_open()
1051 spin_unlock_irq(&port->inbuf_lock); in port_fops_open()
1053 spin_lock_irq(&port->outvq_lock); in port_fops_open()
1059 reclaim_consumed_buffers(port); in port_fops_open()
1060 spin_unlock_irq(&port->outvq_lock); in port_fops_open()
1069 kref_put(&port->kref, remove_port); in port_fops_open()
1075 struct port *port; in port_fops_fasync() local
1077 port = filp->private_data; in port_fops_fasync()
1078 return fasync_helper(fd, filp, mode, &port->async_queue); in port_fops_fasync()
1108 struct port *port; in put_chars() local
1113 port = find_port_by_vtermno(vtermno); in put_chars()
1114 if (!port) in put_chars()
1122 ret = __send_to_port(port, sg, 1, count, data, false); in put_chars()
1136 struct port *port; in get_chars() local
1138 port = find_port_by_vtermno(vtermno); in get_chars()
1139 if (!port) in get_chars()
1143 BUG_ON(!port->in_vq); in get_chars()
1145 return fill_readbuf(port, (__force u8 __user *)buf, count, false); in get_chars()
1148 static void resize_console(struct port *port) in resize_console() argument
1153 if (!port || !is_console_port(port)) in resize_console()
1156 vdev = port->portdev->vdev; in resize_console()
1161 hvc_resize(port->cons.hvc, port->cons.ws); in resize_console()
1167 struct port *port; in notifier_add_vio() local
1169 port = find_port_by_vtermno(hp->vtermno); in notifier_add_vio()
1170 if (!port) in notifier_add_vio()
1174 resize_console(port); in notifier_add_vio()
1193 static int init_port_console(struct port *port) in init_port_console() argument
1218 port->cons.vtermno = ret; in init_port_console()
1219 port->cons.hvc = hvc_alloc(port->cons.vtermno, 0, &hv_ops, PAGE_SIZE); in init_port_console()
1220 if (IS_ERR(port->cons.hvc)) { in init_port_console()
1221 ret = PTR_ERR(port->cons.hvc); in init_port_console()
1222 dev_err(port->dev, in init_port_console()
1224 port->cons.hvc = NULL; in init_port_console()
1225 ida_free(&vtermno_ida, port->cons.vtermno); in init_port_console()
1229 list_add_tail(&port->cons.list, &pdrvdata.consoles); in init_port_console()
1231 port->guest_connected = true; in init_port_console()
1234 send_control_msg(port, VIRTIO_CONSOLE_PORT_OPEN, 1); in init_port_console()
1242 struct port *port; in show_port_name() local
1244 port = dev_get_drvdata(dev); in show_port_name()
1246 return sprintf(buffer, "%s\n", port->name); in show_port_name()
1263 struct port *port = s->private; in port_debugfs_show() local
1265 seq_printf(s, "name: %s\n", port->name ? port->name : ""); in port_debugfs_show()
1266 seq_printf(s, "guest_connected: %d\n", port->guest_connected); in port_debugfs_show()
1267 seq_printf(s, "host_connected: %d\n", port->host_connected); in port_debugfs_show()
1268 seq_printf(s, "outvq_full: %d\n", port->outvq_full); in port_debugfs_show()
1269 seq_printf(s, "bytes_sent: %lu\n", port->stats.bytes_sent); in port_debugfs_show()
1270 seq_printf(s, "bytes_received: %lu\n", port->stats.bytes_received); in port_debugfs_show()
1271 seq_printf(s, "bytes_discarded: %lu\n", port->stats.bytes_discarded); in port_debugfs_show()
1273 is_console_port(port) ? "yes" : "no"); in port_debugfs_show()
1274 seq_printf(s, "console_vtermno: %u\n", port->cons.vtermno); in port_debugfs_show()
1281 static void set_console_size(struct port *port, u16 rows, u16 cols) in set_console_size() argument
1283 if (!port || !is_console_port(port)) in set_console_size()
1286 port->cons.ws.ws_row = rows; in set_console_size()
1287 port->cons.ws.ws_col = cols; in set_console_size()
1316 static void send_sigio_to_port(struct port *port) in send_sigio_to_port() argument
1318 if (port->async_queue && port->guest_connected) in send_sigio_to_port()
1319 kill_fasync(&port->async_queue, SIGIO, POLL_OUT); in send_sigio_to_port()
1325 struct port *port; in add_port() local
1329 port = kmalloc(sizeof(*port), GFP_KERNEL); in add_port()
1330 if (!port) { in add_port()
1334 kref_init(&port->kref); in add_port()
1336 port->portdev = portdev; in add_port()
1337 port->id = id; in add_port()
1339 port->name = NULL; in add_port()
1340 port->inbuf = NULL; in add_port()
1341 port->cons.hvc = NULL; in add_port()
1342 port->async_queue = NULL; in add_port()
1344 port->cons.ws.ws_row = port->cons.ws.ws_col = 0; in add_port()
1345 port->cons.vtermno = 0; in add_port()
1347 port->host_connected = port->guest_connected = false; in add_port()
1348 port->stats = (struct port_stats) { 0 }; in add_port()
1350 port->outvq_full = false; in add_port()
1352 port->in_vq = portdev->in_vqs[port->id]; in add_port()
1353 port->out_vq = portdev->out_vqs[port->id]; in add_port()
1355 port->cdev = cdev_alloc(); in add_port()
1356 if (!port->cdev) { in add_port()
1357 dev_err(&port->portdev->vdev->dev, "Error allocating cdev\n"); in add_port()
1361 port->cdev->ops = &port_fops; in add_port()
1364 err = cdev_add(port->cdev, devt, 1); in add_port()
1366 dev_err(&port->portdev->vdev->dev, in add_port()
1370 port->dev = device_create(&port_class, &port->portdev->vdev->dev, in add_port()
1371 devt, port, "vport%up%u", in add_port()
1372 port->portdev->vdev->index, id); in add_port()
1373 if (IS_ERR(port->dev)) { in add_port()
1374 err = PTR_ERR(port->dev); in add_port()
1375 dev_err(&port->portdev->vdev->dev, in add_port()
1381 spin_lock_init(&port->inbuf_lock); in add_port()
1382 spin_lock_init(&port->outvq_lock); in add_port()
1383 init_waitqueue_head(&port->waitqueue); in add_port()
1389 err = fill_queue(port->in_vq, &port->inbuf_lock); in add_port()
1391 dev_err(port->dev, "Error allocating inbufs\n"); in add_port()
1395 if (is_rproc_serial(port->portdev->vdev)) in add_port()
1401 port->host_connected = true; in add_port()
1402 else if (!use_multiport(port->portdev)) { in add_port()
1407 err = init_port_console(port); in add_port()
1413 list_add_tail(&port->list, &port->portdev->ports); in add_port()
1421 send_control_msg(port, VIRTIO_CONSOLE_PORT_READY, 1); in add_port()
1428 port->portdev->vdev->index, id); in add_port()
1429 port->debugfs_file = debugfs_create_file(debugfs_name, 0444, in add_port()
1431 port, &port_debugfs_fops); in add_port()
1436 device_destroy(&port_class, port->dev->devt); in add_port()
1438 cdev_del(port->cdev); in add_port()
1440 kfree(port); in add_port()
1450 struct port *port; in remove_port() local
1452 port = container_of(kref, struct port, kref); in remove_port()
1454 kfree(port); in remove_port()
1457 static void remove_port_data(struct port *port) in remove_port_data() argument
1459 spin_lock_irq(&port->inbuf_lock); in remove_port_data()
1461 discard_port_data(port); in remove_port_data()
1462 spin_unlock_irq(&port->inbuf_lock); in remove_port_data()
1464 spin_lock_irq(&port->outvq_lock); in remove_port_data()
1465 reclaim_consumed_buffers(port); in remove_port_data()
1466 spin_unlock_irq(&port->outvq_lock); in remove_port_data()
1474 static void unplug_port(struct port *port) in unplug_port() argument
1476 spin_lock_irq(&port->portdev->ports_lock); in unplug_port()
1477 list_del(&port->list); in unplug_port()
1478 spin_unlock_irq(&port->portdev->ports_lock); in unplug_port()
1480 spin_lock_irq(&port->inbuf_lock); in unplug_port()
1481 if (port->guest_connected) { in unplug_port()
1483 send_sigio_to_port(port); in unplug_port()
1486 port->guest_connected = false; in unplug_port()
1487 port->host_connected = false; in unplug_port()
1489 wake_up_interruptible(&port->waitqueue); in unplug_port()
1491 spin_unlock_irq(&port->inbuf_lock); in unplug_port()
1493 if (is_console_port(port)) { in unplug_port()
1495 list_del(&port->cons.list); in unplug_port()
1497 hvc_remove(port->cons.hvc); in unplug_port()
1498 ida_free(&vtermno_ida, port->cons.vtermno); in unplug_port()
1501 remove_port_data(port); in unplug_port()
1508 port->portdev = NULL; in unplug_port()
1510 sysfs_remove_group(&port->dev->kobj, &port_attribute_group); in unplug_port()
1511 device_destroy(&port_class, port->dev->devt); in unplug_port()
1512 cdev_del(port->cdev); in unplug_port()
1514 debugfs_remove(port->debugfs_file); in unplug_port()
1515 kfree(port->name); in unplug_port()
1522 kref_put(&port->kref, remove_port); in unplug_port()
1531 struct port *port; in handle_control_message() local
1537 port = find_port_by_id(portdev, virtio32_to_cpu(vdev, cpkt->id)); in handle_control_message()
1538 if (!port && in handle_control_message()
1548 if (port) { in handle_control_message()
1550 "Port %u already added\n", port->id); in handle_control_message()
1551 send_control_msg(port, VIRTIO_CONSOLE_PORT_READY, 1); in handle_control_message()
1565 unplug_port(port); in handle_control_message()
1570 if (is_console_port(port)) in handle_control_message()
1573 init_port_console(port); in handle_control_message()
1586 if (!is_console_port(port)) in handle_control_message()
1591 set_console_size(port, size.rows, size.cols); in handle_control_message()
1593 port->cons.hvc->irq_requested = 1; in handle_control_message()
1594 resize_console(port); in handle_control_message()
1598 port->host_connected = virtio16_to_cpu(vdev, cpkt->value); in handle_control_message()
1599 wake_up_interruptible(&port->waitqueue); in handle_control_message()
1605 spin_lock_irq(&port->outvq_lock); in handle_control_message()
1606 reclaim_consumed_buffers(port); in handle_control_message()
1607 spin_unlock_irq(&port->outvq_lock); in handle_control_message()
1613 spin_lock_irq(&port->inbuf_lock); in handle_control_message()
1614 send_sigio_to_port(port); in handle_control_message()
1615 spin_unlock_irq(&port->inbuf_lock); in handle_control_message()
1622 if (port->name) in handle_control_message()
1631 port->name = kmalloc(name_size, GFP_KERNEL); in handle_control_message()
1632 if (!port->name) { in handle_control_message()
1633 dev_err(port->dev, in handle_control_message()
1637 strscpy(port->name, buf->buf + buf->offset + sizeof(*cpkt), in handle_control_message()
1644 err = sysfs_create_group(&port->dev->kobj, in handle_control_message()
1647 dev_err(port->dev, in handle_control_message()
1656 kobject_uevent(&port->dev->kobj, KOBJ_CHANGE); in handle_control_message()
1702 struct port *port; in out_intr() local
1704 port = find_port_by_vq(vq->vdev->priv, vq); in out_intr()
1705 if (!port) { in out_intr()
1710 wake_up_interruptible(&port->waitqueue); in out_intr()
1715 struct port *port; in in_intr() local
1718 port = find_port_by_vq(vq->vdev->priv, vq); in in_intr()
1719 if (!port) { in in_intr()
1724 spin_lock_irqsave(&port->inbuf_lock, flags); in in_intr()
1725 port->inbuf = get_inbuf(port); in in_intr()
1744 if (!port->guest_connected && !is_rproc_serial(port->portdev->vdev)) in in_intr()
1745 discard_port_data(port); in in_intr()
1748 send_sigio_to_port(port); in in_intr()
1750 spin_unlock_irqrestore(&port->inbuf_lock, flags); in in_intr()
1752 wake_up_interruptible(&port->waitqueue); in in_intr()
1754 if (is_console_port(port) && hvc_poll(port->cons.hvc)) in in_intr()
1783 struct port *port; in config_work_handler() local
1790 port = find_port_by_id(portdev, 0); in config_work_handler()
1791 set_console_size(port, rows, cols); in config_work_handler()
1800 resize_console(port); in config_work_handler()
1907 struct port *port, *port2; in virtcons_remove() local
1930 list_for_each_entry_safe(port, port2, &portdev->ports, list) in virtcons_remove()
1931 unplug_port(port); in virtcons_remove()
2095 struct port *port; in virtcons_freeze() local
2112 list_for_each_entry(port, &portdev->ports, list) { in virtcons_freeze()
2113 virtqueue_disable_cb(port->in_vq); in virtcons_freeze()
2114 virtqueue_disable_cb(port->out_vq); in virtcons_freeze()
2119 port->host_connected = false; in virtcons_freeze()
2120 remove_port_data(port); in virtcons_freeze()
2130 struct port *port; in virtcons_restore() local
2144 list_for_each_entry(port, &portdev->ports, list) { in virtcons_restore()
2145 port->in_vq = portdev->in_vqs[port->id]; in virtcons_restore()
2146 port->out_vq = portdev->out_vqs[port->id]; in virtcons_restore()
2148 fill_queue(port->in_vq, &port->inbuf_lock); in virtcons_restore()
2151 send_control_msg(port, VIRTIO_CONSOLE_PORT_READY, 1); in virtcons_restore()
2157 if (port->guest_connected) in virtcons_restore()
2158 send_control_msg(port, VIRTIO_CONSOLE_PORT_OPEN, 1); in virtcons_restore()