Lines Matching refs:port
43 /* Used for exporting per-port information to debugfs */
67 /* The hvc device associated with this console port */
154 /* Array of per-port IO virtqueues */
165 /* This struct holds the per-port data */
166 struct port {
167 /* Next port in the list, head is in the ports_device */
178 * port. Has to be a spinlock because it can be called from
186 /* The IO vqs for this port */
189 /* File in the debugfs directory that exposes this port's information */
194 * this port for accounting and debugging purposes. These
195 * counts are not reset across port open / close events.
200 * The entries in this struct will be valid if this port is
205 /* Each port associates with a separate char device */
209 /* Reference-counting to handle port hot-unplugs and file operations */
215 /* The 'name' of the port that we expose via sysfs properties */
221 /* The 'id' to identify the port with the Host */
229 /* We should allow only one process to open a port */
236 static struct port *find_port_by_vtermno(u32 vtermno)
238 struct port *port;
245 port = container_of(cons, struct port, cons);
249 port = NULL;
252 return port;
255 static struct port *find_port_by_devt_in_portdev(struct ports_device *portdev,
258 struct port *port;
262 list_for_each_entry(port, &portdev->ports, list) {
263 if (port->cdev->dev == dev) {
264 kref_get(&port->kref);
268 port = NULL;
272 return port;
275 static struct port *find_port_by_devt(dev_t dev)
278 struct port *port;
283 port = find_port_by_devt_in_portdev(portdev, dev);
284 if (port)
287 port = NULL;
290 return port;
293 static struct port *find_port_by_id(struct ports_device *portdev, u32 id)
295 struct port *port;
299 list_for_each_entry(port, &portdev->ports, list)
300 if (port->id == id)
302 port = NULL;
306 return port;
309 static struct port *find_port_by_vq(struct ports_device *portdev,
312 struct port *port;
316 list_for_each_entry(port, &portdev->ports, list)
317 if (port->in_vq == vq || port->out_vq == vq)
319 port = NULL;
322 return port;
325 static bool is_console_port(struct port *port)
327 if (port->cons.hvc)
462 static struct port_buffer *get_inbuf(struct port *port)
467 if (port->inbuf)
468 return port->inbuf;
470 buf = virtqueue_get_buf(port->in_vq, &len);
474 port->stats.bytes_received += len;
499 /* Discard any unread data this port has. Callers lockers. */
500 static void discard_port_data(struct port *port)
505 if (!port->portdev) {
509 buf = get_inbuf(port);
513 port->stats.bytes_discarded += buf->len - buf->offset;
514 if (add_inbuf(port->in_vq, buf) < 0) {
518 port->inbuf = NULL;
519 buf = get_inbuf(port);
522 dev_warn(port->dev, "Errors adding %d buffers back to vq\n",
526 static bool port_has_data(struct port *port)
532 spin_lock_irqsave(&port->inbuf_lock, flags);
533 port->inbuf = get_inbuf(port);
534 if (port->inbuf)
537 spin_unlock_irqrestore(&port->inbuf_lock, flags);
572 static ssize_t send_control_msg(struct port *port, unsigned int event,
575 /* Did the port get unplugged before userspace closed it? */
576 if (port->portdev)
577 return __send_control_msg(port->portdev, port->id, event, value);
582 /* Callers must take the port->outvq_lock */
583 static void reclaim_consumed_buffers(struct port *port)
588 if (!port->portdev) {
592 while ((buf = virtqueue_get_buf(port->out_vq, &len))) {
594 port->outvq_full = false;
598 static ssize_t __send_to_port(struct port *port, struct scatterlist *sg,
607 out_vq = port->out_vq;
609 spin_lock_irqsave(&port->outvq_lock, flags);
611 reclaim_consumed_buffers(port);
624 port->outvq_full = true;
642 spin_unlock_irqrestore(&port->outvq_lock, flags);
644 port->stats.bytes_sent += in_count;
656 static ssize_t fill_readbuf(struct port *port, char __user *out_buf,
662 if (!out_count || !port_has_data(port))
665 buf = port->inbuf;
686 spin_lock_irqsave(&port->inbuf_lock, flags);
687 port->inbuf = NULL;
689 if (add_inbuf(port->in_vq, buf) < 0)
690 dev_warn(port->dev, "failed add_buf\n");
692 spin_unlock_irqrestore(&port->inbuf_lock, flags);
699 static bool will_read_block(struct port *port)
701 if (!port->guest_connected) {
705 return !port_has_data(port) && port->host_connected;
708 static bool will_write_block(struct port *port)
712 if (!port->guest_connected) {
716 if (!port->host_connected)
719 spin_lock_irq(&port->outvq_lock);
724 reclaim_consumed_buffers(port);
725 ret = port->outvq_full;
726 spin_unlock_irq(&port->outvq_lock);
734 struct port *port;
737 port = filp->private_data;
740 if (!port->guest_connected)
743 if (!port_has_data(port)) {
749 if (!port->host_connected)
754 ret = wait_event_freezable(port->waitqueue,
755 !will_read_block(port));
760 if (!port->guest_connected)
772 if (!port_has_data(port) && !port->host_connected)
775 return fill_readbuf(port, ubuf, count, true);
778 static int wait_port_writable(struct port *port, bool nonblock)
782 if (will_write_block(port)) {
786 ret = wait_event_freezable(port->waitqueue,
787 !will_write_block(port));
792 if (!port->guest_connected)
801 struct port *port;
811 port = filp->private_data;
815 ret = wait_port_writable(port, nonblock);
821 buf = alloc_buf(port->portdev->vdev, count, 0);
840 ret = __send_to_port(port, sg, 1, count, buf, nonblock);
906 struct port *port = filp->private_data;
924 if (is_rproc_serial(port->out_vq->vdev))
932 ret = wait_port_writable(port, filp->f_flags & O_NONBLOCK);
937 buf = alloc_buf(port->portdev->vdev, 0, occupancy);
952 ret = __send_to_port(port, buf->sg, sgl.n, sgl.len, buf, true);
965 struct port *port;
968 port = filp->private_data;
969 poll_wait(filp, &port->waitqueue, wait);
971 if (!port->guest_connected) {
976 if (!will_read_block(port))
978 if (!will_write_block(port))
980 if (!port->host_connected)
990 struct port *port;
992 port = filp->private_data;
994 /* Notify host of port being closed */
995 send_control_msg(port, VIRTIO_CONSOLE_PORT_OPEN, 0);
997 spin_lock_irq(&port->inbuf_lock);
998 port->guest_connected = false;
1000 discard_port_data(port);
1002 spin_unlock_irq(&port->inbuf_lock);
1004 spin_lock_irq(&port->outvq_lock);
1005 reclaim_consumed_buffers(port);
1006 spin_unlock_irq(&port->outvq_lock);
1010 * Locks aren't necessary here as a port can't be opened after
1011 * unplug, and if a port isn't unplugged, a kref would already
1012 * exist for the port. Plus, taking ports_lock here would
1014 * inside remove_port if we're the last holder of the port,
1017 kref_put(&port->kref, remove_port);
1025 struct port *port;
1028 /* We get the port with a kref here */
1029 port = find_port_by_devt(cdev->dev);
1030 if (!port) {
1034 filp->private_data = port;
1037 * Don't allow opening of console port devices -- that's done
1040 if (is_console_port(port)) {
1045 /* Allow only one process to open a particular port at a time */
1046 spin_lock_irq(&port->inbuf_lock);
1047 if (port->guest_connected) {
1048 spin_unlock_irq(&port->inbuf_lock);
1053 port->guest_connected = true;
1054 spin_unlock_irq(&port->inbuf_lock);
1056 spin_lock_irq(&port->outvq_lock);
1059 * buffers in the window of the port getting previously closed
1062 reclaim_consumed_buffers(port);
1063 spin_unlock_irq(&port->outvq_lock);
1067 /* Notify host of port being opened */
1072 kref_put(&port->kref, remove_port);
1078 struct port *port;
1080 port = filp->private_data;
1081 return fasync_helper(fd, filp, mode, &port->async_queue);
1088 * /dev/vport<device number>p<port number>
1112 struct port *port;
1120 port = find_port_by_vtermno(vtermno);
1121 if (!port)
1129 ret = __send_to_port(port, sg, 1, count, data, false);
1143 struct port *port;
1145 /* If we've not set up the port yet, we have no input to give. */
1149 port = find_port_by_vtermno(vtermno);
1150 if (!port)
1154 BUG_ON(!port->in_vq);
1156 return fill_readbuf(port, (__force char __user *)buf, count, false);
1159 static void resize_console(struct port *port)
1163 /* The port could have been hot-unplugged */
1164 if (!port || !is_console_port(port))
1167 vdev = port->portdev->vdev;
1172 hvc_resize(port->cons.hvc, port->cons.ws);
1178 struct port *port;
1180 port = find_port_by_vtermno(hp->vtermno);
1181 if (!port)
1185 resize_console(port);
1219 static int init_port_console(struct port *port)
1224 * The Host's telling us this port is a console port. Hook it
1244 port->cons.vtermno = ret;
1245 port->cons.hvc = hvc_alloc(port->cons.vtermno, 0, &hv_ops, PAGE_SIZE);
1246 if (IS_ERR(port->cons.hvc)) {
1247 ret = PTR_ERR(port->cons.hvc);
1248 dev_err(port->dev,
1249 "error %d allocating hvc for port\n", ret);
1250 port->cons.hvc = NULL;
1251 ida_free(&vtermno_ida, port->cons.vtermno);
1255 list_add_tail(&port->cons.list, &pdrvdata.consoles);
1257 port->guest_connected = true;
1266 /* Notify host of port being opened */
1267 send_control_msg(port, VIRTIO_CONSOLE_PORT_OPEN, 1);
1275 struct port *port;
1277 port = dev_get_drvdata(dev);
1279 return sprintf(buffer, "%s\n", port->name);
1296 struct port *port = s->private;
1298 seq_printf(s, "name: %s\n", port->name ? port->name : "");
1299 seq_printf(s, "guest_connected: %d\n", port->guest_connected);
1300 seq_printf(s, "host_connected: %d\n", port->host_connected);
1301 seq_printf(s, "outvq_full: %d\n", port->outvq_full);
1302 seq_printf(s, "bytes_sent: %lu\n", port->stats.bytes_sent);
1303 seq_printf(s, "bytes_received: %lu\n", port->stats.bytes_received);
1304 seq_printf(s, "bytes_discarded: %lu\n", port->stats.bytes_discarded);
1306 is_console_port(port) ? "yes" : "no");
1307 seq_printf(s, "console_vtermno: %u\n", port->cons.vtermno);
1314 static void set_console_size(struct port *port, u16 rows, u16 cols)
1316 if (!port || !is_console_port(port))
1319 port->cons.ws.ws_row = rows;
1320 port->cons.ws.ws_col = cols;
1349 static void send_sigio_to_port(struct port *port)
1351 if (port->async_queue && port->guest_connected)
1352 kill_fasync(&port->async_queue, SIGIO, POLL_OUT);
1358 struct port *port;
1362 port = kmalloc(sizeof(*port), GFP_KERNEL);
1363 if (!port) {
1367 kref_init(&port->kref);
1369 port->portdev = portdev;
1370 port->id = id;
1372 port->name = NULL;
1373 port->inbuf = NULL;
1374 port->cons.hvc = NULL;
1375 port->async_queue = NULL;
1377 port->cons.ws.ws_row = port->cons.ws.ws_col = 0;
1378 port->cons.vtermno = 0;
1380 port->host_connected = port->guest_connected = false;
1381 port->stats = (struct port_stats) { 0 };
1383 port->outvq_full = false;
1385 port->in_vq = portdev->in_vqs[port->id];
1386 port->out_vq = portdev->out_vqs[port->id];
1388 port->cdev = cdev_alloc();
1389 if (!port->cdev) {
1390 dev_err(&port->portdev->vdev->dev, "Error allocating cdev\n");
1394 port->cdev->ops = &port_fops;
1397 err = cdev_add(port->cdev, devt, 1);
1399 dev_err(&port->portdev->vdev->dev,
1400 "Error %d adding cdev for port %u\n", err, id);
1403 port->dev = device_create(&port_class, &port->portdev->vdev->dev,
1404 devt, port, "vport%up%u",
1405 port->portdev->vdev->index, id);
1406 if (IS_ERR(port->dev)) {
1407 err = PTR_ERR(port->dev);
1408 dev_err(&port->portdev->vdev->dev,
1409 "Error %d creating device for port %u\n",
1414 spin_lock_init(&port->inbuf_lock);
1415 spin_lock_init(&port->outvq_lock);
1416 init_waitqueue_head(&port->waitqueue);
1422 err = fill_queue(port->in_vq, &port->inbuf_lock);
1424 dev_err(port->dev, "Error allocating inbufs\n");
1428 if (is_rproc_serial(port->portdev->vdev))
1431 * rproc_serial does not want the console port, only
1432 * the generic port implementation.
1434 port->host_connected = true;
1435 else if (!use_multiport(port->portdev)) {
1438 * this has to be a console port.
1440 err = init_port_console(port);
1446 list_add_tail(&port->list, &port->portdev->ports);
1451 * configuration parameters for this port (eg, port name,
1452 * caching, whether this is a console port, etc.)
1454 send_control_msg(port, VIRTIO_CONSOLE_PORT_READY, 1);
1458 * inspect a port's state at any time
1461 port->portdev->vdev->index, id);
1462 port->debugfs_file = debugfs_create_file(debugfs_name, 0444,
1464 port, &port_debugfs_fops);
1469 device_destroy(&port_class, port->dev->devt);
1471 cdev_del(port->cdev);
1473 kfree(port);
1475 /* The host might want to notify management sw about port add failure */
1480 /* No users remain, remove all port-specific data. */
1483 struct port *port;
1485 port = container_of(kref, struct port, kref);
1487 kfree(port);
1490 static void remove_port_data(struct port *port)
1492 spin_lock_irq(&port->inbuf_lock);
1493 /* Remove unused data this port might have received. */
1494 discard_port_data(port);
1495 spin_unlock_irq(&port->inbuf_lock);
1497 spin_lock_irq(&port->outvq_lock);
1498 reclaim_consumed_buffers(port);
1499 spin_unlock_irq(&port->outvq_lock);
1503 * Port got unplugged. Remove port from portdev's list and drop the
1504 * kref reference. If no userspace has this port opened, it will
1505 * result in immediate removal the port.
1507 static void unplug_port(struct port *port)
1509 spin_lock_irq(&port->portdev->ports_lock);
1510 list_del(&port->list);
1511 spin_unlock_irq(&port->portdev->ports_lock);
1513 spin_lock_irq(&port->inbuf_lock);
1514 if (port->guest_connected) {
1515 /* Let the app know the port is going down. */
1516 send_sigio_to_port(port);
1519 port->guest_connected = false;
1520 port->host_connected = false;
1522 wake_up_interruptible(&port->waitqueue);
1524 spin_unlock_irq(&port->inbuf_lock);
1526 if (is_console_port(port)) {
1528 list_del(&port->cons.list);
1530 hvc_remove(port->cons.hvc);
1531 ida_free(&vtermno_ida, port->cons.vtermno);
1534 remove_port_data(port);
1538 * else a close on an open port later will try to send out a
1541 port->portdev = NULL;
1543 sysfs_remove_group(&port->dev->kobj, &port_attribute_group);
1544 device_destroy(&port_class, port->dev->devt);
1545 cdev_del(port->cdev);
1547 debugfs_remove(port->debugfs_file);
1548 kfree(port->name);
1551 * Locks around here are not necessary - a port can't be
1552 * opened after we removed the port struct from ports_list
1555 kref_put(&port->kref, remove_port);
1564 struct port *port;
1570 port = find_port_by_id(portdev, virtio32_to_cpu(vdev, cpkt->id));
1571 if (!port &&
1581 if (port) {
1583 "Port %u already added\n", port->id);
1584 send_control_msg(port, VIRTIO_CONSOLE_PORT_READY, 1);
1590 "Request for adding port with "
1598 unplug_port(port);
1603 if (is_console_port(port))
1606 init_port_console(port);
1609 * Could remove the port here in case init fails - but
1619 if (!is_console_port(port))
1624 set_console_size(port, size.rows, size.cols);
1626 port->cons.hvc->irq_requested = 1;
1627 resize_console(port);
1631 port->host_connected = virtio16_to_cpu(vdev, cpkt->value);
1632 wake_up_interruptible(&port->waitqueue);
1634 * If the host port got closed and the host had any
1638 spin_lock_irq(&port->outvq_lock);
1639 reclaim_consumed_buffers(port);
1640 spin_unlock_irq(&port->outvq_lock);
1646 spin_lock_irq(&port->inbuf_lock);
1647 send_sigio_to_port(port);
1648 spin_unlock_irq(&port->inbuf_lock);
1655 if (port->name)
1664 port->name = kmalloc(name_size, GFP_KERNEL);
1665 if (!port->name) {
1666 dev_err(port->dev,
1667 "Not enough space to store port name\n");
1670 strscpy(port->name, buf->buf + buf->offset + sizeof(*cpkt),
1675 * create it only if we have a name for the port.
1677 err = sysfs_create_group(&port->dev->kobj,
1680 dev_err(port->dev,
1689 kobject_uevent(&port->dev->kobj, KOBJ_CHANGE);
1735 struct port *port;
1737 port = find_port_by_vq(vq->vdev->priv, vq);
1738 if (!port) {
1743 wake_up_interruptible(&port->waitqueue);
1748 struct port *port;
1751 port = find_port_by_vq(vq->vdev->priv, vq);
1752 if (!port) {
1757 spin_lock_irqsave(&port->inbuf_lock, flags);
1758 port->inbuf = get_inbuf(port);
1761 * Normally the port should not accept data when the port is
1764 * can be reached when a console port is not yet connected (no
1769 * A generic serial port will discard data if not connected,
1777 if (!port->guest_connected && !is_rproc_serial(port->portdev->vdev))
1778 discard_port_data(port);
1781 send_sigio_to_port(port);
1783 spin_unlock_irqrestore(&port->inbuf_lock, flags);
1785 wake_up_interruptible(&port->waitqueue);
1787 if (is_console_port(port) && hvc_poll(port->cons.hvc))
1816 struct port *port;
1823 port = find_port_by_id(portdev, 0);
1824 set_console_size(port, rows, cols);
1831 * done per-port.
1833 resize_console(port);
1864 * spawns a console port first and also inits the vqs for port
1949 struct port *port, *port2;
1972 list_for_each_entry_safe(port, port2, &portdev->ports, list)
1973 unplug_port(port);
1982 * have to just stop using the port, as the vqs are going
1995 * initialize each port found.
2090 * For backward compatibility: Create a console port
2151 struct port *port;
2168 list_for_each_entry(port, &portdev->ports, list) {
2169 virtqueue_disable_cb(port->in_vq);
2170 virtqueue_disable_cb(port->out_vq);
2173 * the port opened or closed.
2175 port->host_connected = false;
2176 remove_port_data(port);
2186 struct port *port;
2200 list_for_each_entry(port, &portdev->ports, list) {
2201 port->in_vq = portdev->in_vqs[port->id];
2202 port->out_vq = portdev->out_vqs[port->id];
2204 fill_queue(port->in_vq, &port->inbuf_lock);
2206 /* Get port open/close status on the host */
2207 send_control_msg(port, VIRTIO_CONSOLE_PORT_READY, 1);
2210 * If a port was open at the time of suspending, we
2213 if (port->guest_connected)
2214 send_control_msg(port, VIRTIO_CONSOLE_PORT_OPEN, 1);