Lines Matching refs:port
44 /* Used for exporting per-port information to debugfs */
75 /* The hvc device associated with this console port */
160 /* Array of per-port IO virtqueues */
171 /* This struct holds the per-port data */
172 struct port {
173 /* Next port in the list, head is in the ports_device */
184 * port. Has to be a spinlock because it can be called from
192 /* The IO vqs for this port */
195 /* File in the debugfs directory that exposes this port's information */
200 * this port for accounting and debugging purposes. These
201 * counts are not reset across port open / close events.
206 * The entries in this struct will be valid if this port is
211 /* Each port associates with a separate char device */
215 /* Reference-counting to handle port hot-unplugs and file operations */
221 /* The 'name' of the port that we expose via sysfs properties */
227 /* The 'id' to identify the port with the Host */
235 /* We should allow only one process to open a port */
242 static struct port *find_port_by_vtermno(u32 vtermno)
244 struct port *port;
251 port = container_of(cons, struct port, cons);
255 port = NULL;
258 return port;
261 static struct port *find_port_by_devt_in_portdev(struct ports_device *portdev,
264 struct port *port;
268 list_for_each_entry(port, &portdev->ports, list) {
269 if (port->cdev->dev == dev) {
270 kref_get(&port->kref);
274 port = NULL;
278 return port;
281 static struct port *find_port_by_devt(dev_t dev)
284 struct port *port;
289 port = find_port_by_devt_in_portdev(portdev, dev);
290 if (port)
293 port = NULL;
296 return port;
299 static struct port *find_port_by_id(struct ports_device *portdev, u32 id)
301 struct port *port;
305 list_for_each_entry(port, &portdev->ports, list)
306 if (port->id == id)
308 port = NULL;
312 return port;
315 static struct port *find_port_by_vq(struct ports_device *portdev,
318 struct port *port;
322 list_for_each_entry(port, &portdev->ports, list)
323 if (port->in_vq == vq || port->out_vq == vq)
325 port = NULL;
328 return port;
331 static bool is_console_port(struct port *port)
333 if (port->cons.hvc)
468 static struct port_buffer *get_inbuf(struct port *port)
473 if (port->inbuf)
474 return port->inbuf;
476 buf = virtqueue_get_buf(port->in_vq, &len);
480 port->stats.bytes_received += len;
505 /* Discard any unread data this port has. Callers lockers. */
506 static void discard_port_data(struct port *port)
511 if (!port->portdev) {
515 buf = get_inbuf(port);
519 port->stats.bytes_discarded += buf->len - buf->offset;
520 if (add_inbuf(port->in_vq, buf) < 0) {
524 port->inbuf = NULL;
525 buf = get_inbuf(port);
528 dev_warn(port->dev, "Errors adding %d buffers back to vq\n",
532 static bool port_has_data(struct port *port)
538 spin_lock_irqsave(&port->inbuf_lock, flags);
539 port->inbuf = get_inbuf(port);
540 if (port->inbuf)
543 spin_unlock_irqrestore(&port->inbuf_lock, flags);
578 static ssize_t send_control_msg(struct port *port, unsigned int event,
581 /* Did the port get unplugged before userspace closed it? */
582 if (port->portdev)
583 return __send_control_msg(port->portdev, port->id, event, value);
588 /* Callers must take the port->outvq_lock */
589 static void reclaim_consumed_buffers(struct port *port)
594 if (!port->portdev) {
598 while ((buf = virtqueue_get_buf(port->out_vq, &len))) {
600 port->outvq_full = false;
604 static ssize_t __send_to_port(struct port *port, struct scatterlist *sg,
613 out_vq = port->out_vq;
615 spin_lock_irqsave(&port->outvq_lock, flags);
617 reclaim_consumed_buffers(port);
630 port->outvq_full = true;
648 spin_unlock_irqrestore(&port->outvq_lock, flags);
650 port->stats.bytes_sent += in_count;
662 static ssize_t fill_readbuf(struct port *port, char __user *out_buf,
668 if (!out_count || !port_has_data(port))
671 buf = port->inbuf;
692 spin_lock_irqsave(&port->inbuf_lock, flags);
693 port->inbuf = NULL;
695 if (add_inbuf(port->in_vq, buf) < 0)
696 dev_warn(port->dev, "failed add_buf\n");
698 spin_unlock_irqrestore(&port->inbuf_lock, flags);
705 static bool will_read_block(struct port *port)
707 if (!port->guest_connected) {
711 return !port_has_data(port) && port->host_connected;
714 static bool will_write_block(struct port *port)
718 if (!port->guest_connected) {
722 if (!port->host_connected)
725 spin_lock_irq(&port->outvq_lock);
730 reclaim_consumed_buffers(port);
731 ret = port->outvq_full;
732 spin_unlock_irq(&port->outvq_lock);
740 struct port *port;
743 port = filp->private_data;
746 if (!port->guest_connected)
749 if (!port_has_data(port)) {
755 if (!port->host_connected)
760 ret = wait_event_freezable(port->waitqueue,
761 !will_read_block(port));
766 if (!port->guest_connected)
778 if (!port_has_data(port) && !port->host_connected)
781 return fill_readbuf(port, ubuf, count, true);
784 static int wait_port_writable(struct port *port, bool nonblock)
788 if (will_write_block(port)) {
792 ret = wait_event_freezable(port->waitqueue,
793 !will_write_block(port));
798 if (!port->guest_connected)
807 struct port *port;
817 port = filp->private_data;
821 ret = wait_port_writable(port, nonblock);
827 buf = alloc_buf(port->portdev->vdev, count, 0);
846 ret = __send_to_port(port, sg, 1, count, buf, nonblock);
912 struct port *port = filp->private_data;
930 if (is_rproc_serial(port->out_vq->vdev))
938 ret = wait_port_writable(port, filp->f_flags & O_NONBLOCK);
943 buf = alloc_buf(port->portdev->vdev, 0, occupancy);
958 ret = __send_to_port(port, buf->sg, sgl.n, sgl.len, buf, true);
971 struct port *port;
974 port = filp->private_data;
975 poll_wait(filp, &port->waitqueue, wait);
977 if (!port->guest_connected) {
982 if (!will_read_block(port))
984 if (!will_write_block(port))
986 if (!port->host_connected)
996 struct port *port;
998 port = filp->private_data;
1000 /* Notify host of port being closed */
1001 send_control_msg(port, VIRTIO_CONSOLE_PORT_OPEN, 0);
1003 spin_lock_irq(&port->inbuf_lock);
1004 port->guest_connected = false;
1006 discard_port_data(port);
1008 spin_unlock_irq(&port->inbuf_lock);
1010 spin_lock_irq(&port->outvq_lock);
1011 reclaim_consumed_buffers(port);
1012 spin_unlock_irq(&port->outvq_lock);
1016 * Locks aren't necessary here as a port can't be opened after
1017 * unplug, and if a port isn't unplugged, a kref would already
1018 * exist for the port. Plus, taking ports_lock here would
1020 * inside remove_port if we're the last holder of the port,
1023 kref_put(&port->kref, remove_port);
1031 struct port *port;
1034 /* We get the port with a kref here */
1035 port = find_port_by_devt(cdev->dev);
1036 if (!port) {
1040 filp->private_data = port;
1043 * Don't allow opening of console port devices -- that's done
1046 if (is_console_port(port)) {
1051 /* Allow only one process to open a particular port at a time */
1052 spin_lock_irq(&port->inbuf_lock);
1053 if (port->guest_connected) {
1054 spin_unlock_irq(&port->inbuf_lock);
1059 port->guest_connected = true;
1060 spin_unlock_irq(&port->inbuf_lock);
1062 spin_lock_irq(&port->outvq_lock);
1065 * buffers in the window of the port getting previously closed
1068 reclaim_consumed_buffers(port);
1069 spin_unlock_irq(&port->outvq_lock);
1073 /* Notify host of port being opened */
1078 kref_put(&port->kref, remove_port);
1084 struct port *port;
1086 port = filp->private_data;
1087 return fasync_helper(fd, filp, mode, &port->async_queue);
1094 * /dev/vport<device number>p<port number>
1118 struct port *port;
1126 port = find_port_by_vtermno(vtermno);
1127 if (!port)
1135 ret = __send_to_port(port, sg, 1, count, data, false);
1149 struct port *port;
1151 /* If we've not set up the port yet, we have no input to give. */
1155 port = find_port_by_vtermno(vtermno);
1156 if (!port)
1160 BUG_ON(!port->in_vq);
1162 return fill_readbuf(port, (__force char __user *)buf, count, false);
1165 static void resize_console(struct port *port)
1169 /* The port could have been hot-unplugged */
1170 if (!port || !is_console_port(port))
1173 vdev = port->portdev->vdev;
1178 hvc_resize(port->cons.hvc, port->cons.ws);
1184 struct port *port;
1186 port = find_port_by_vtermno(hp->vtermno);
1187 if (!port)
1191 resize_console(port);
1225 static int init_port_console(struct port *port)
1230 * The Host's telling us this port is a console port. Hook it
1246 port->cons.vtermno = pdrvdata.next_vtermno;
1248 port->cons.hvc = hvc_alloc(port->cons.vtermno, 0, &hv_ops, PAGE_SIZE);
1249 if (IS_ERR(port->cons.hvc)) {
1250 ret = PTR_ERR(port->cons.hvc);
1251 dev_err(port->dev,
1252 "error %d allocating hvc for port\n", ret);
1253 port->cons.hvc = NULL;
1258 list_add_tail(&port->cons.list, &pdrvdata.consoles);
1260 port->guest_connected = true;
1269 /* Notify host of port being opened */
1270 send_control_msg(port, VIRTIO_CONSOLE_PORT_OPEN, 1);
1278 struct port *port;
1280 port = dev_get_drvdata(dev);
1282 return sprintf(buffer, "%s\n", port->name);
1299 struct port *port = s->private;
1301 seq_printf(s, "name: %s\n", port->name ? port->name : "");
1302 seq_printf(s, "guest_connected: %d\n", port->guest_connected);
1303 seq_printf(s, "host_connected: %d\n", port->host_connected);
1304 seq_printf(s, "outvq_full: %d\n", port->outvq_full);
1305 seq_printf(s, "bytes_sent: %lu\n", port->stats.bytes_sent);
1306 seq_printf(s, "bytes_received: %lu\n", port->stats.bytes_received);
1307 seq_printf(s, "bytes_discarded: %lu\n", port->stats.bytes_discarded);
1309 is_console_port(port) ? "yes" : "no");
1310 seq_printf(s, "console_vtermno: %u\n", port->cons.vtermno);
1317 static void set_console_size(struct port *port, u16 rows, u16 cols)
1319 if (!port || !is_console_port(port))
1322 port->cons.ws.ws_row = rows;
1323 port->cons.ws.ws_col = cols;
1352 static void send_sigio_to_port(struct port *port)
1354 if (port->async_queue && port->guest_connected)
1355 kill_fasync(&port->async_queue, SIGIO, POLL_OUT);
1361 struct port *port;
1365 port = kmalloc(sizeof(*port), GFP_KERNEL);
1366 if (!port) {
1370 kref_init(&port->kref);
1372 port->portdev = portdev;
1373 port->id = id;
1375 port->name = NULL;
1376 port->inbuf = NULL;
1377 port->cons.hvc = NULL;
1378 port->async_queue = NULL;
1380 port->cons.ws.ws_row = port->cons.ws.ws_col = 0;
1381 port->cons.vtermno = 0;
1383 port->host_connected = port->guest_connected = false;
1384 port->stats = (struct port_stats) { 0 };
1386 port->outvq_full = false;
1388 port->in_vq = portdev->in_vqs[port->id];
1389 port->out_vq = portdev->out_vqs[port->id];
1391 port->cdev = cdev_alloc();
1392 if (!port->cdev) {
1393 dev_err(&port->portdev->vdev->dev, "Error allocating cdev\n");
1397 port->cdev->ops = &port_fops;
1400 err = cdev_add(port->cdev, devt, 1);
1402 dev_err(&port->portdev->vdev->dev,
1403 "Error %d adding cdev for port %u\n", err, id);
1406 port->dev = device_create(pdrvdata.class, &port->portdev->vdev->dev,
1407 devt, port, "vport%up%u",
1408 port->portdev->vdev->index, id);
1409 if (IS_ERR(port->dev)) {
1410 err = PTR_ERR(port->dev);
1411 dev_err(&port->portdev->vdev->dev,
1412 "Error %d creating device for port %u\n",
1417 spin_lock_init(&port->inbuf_lock);
1418 spin_lock_init(&port->outvq_lock);
1419 init_waitqueue_head(&port->waitqueue);
1425 err = fill_queue(port->in_vq, &port->inbuf_lock);
1427 dev_err(port->dev, "Error allocating inbufs\n");
1431 if (is_rproc_serial(port->portdev->vdev))
1434 * rproc_serial does not want the console port, only
1435 * the generic port implementation.
1437 port->host_connected = true;
1438 else if (!use_multiport(port->portdev)) {
1441 * this has to be a console port.
1443 err = init_port_console(port);
1449 list_add_tail(&port->list, &port->portdev->ports);
1454 * configuration parameters for this port (eg, port name,
1455 * caching, whether this is a console port, etc.)
1457 send_control_msg(port, VIRTIO_CONSOLE_PORT_READY, 1);
1462 * inspect a port's state at any time
1465 port->portdev->vdev->index, id);
1466 port->debugfs_file = debugfs_create_file(debugfs_name, 0444,
1468 port,
1475 device_destroy(pdrvdata.class, port->dev->devt);
1477 cdev_del(port->cdev);
1479 kfree(port);
1481 /* The host might want to notify management sw about port add failure */
1486 /* No users remain, remove all port-specific data. */
1489 struct port *port;
1491 port = container_of(kref, struct port, kref);
1493 kfree(port);
1496 static void remove_port_data(struct port *port)
1498 spin_lock_irq(&port->inbuf_lock);
1499 /* Remove unused data this port might have received. */
1500 discard_port_data(port);
1501 spin_unlock_irq(&port->inbuf_lock);
1503 spin_lock_irq(&port->outvq_lock);
1504 reclaim_consumed_buffers(port);
1505 spin_unlock_irq(&port->outvq_lock);
1509 * Port got unplugged. Remove port from portdev's list and drop the
1510 * kref reference. If no userspace has this port opened, it will
1511 * result in immediate removal the port.
1513 static void unplug_port(struct port *port)
1515 spin_lock_irq(&port->portdev->ports_lock);
1516 list_del(&port->list);
1517 spin_unlock_irq(&port->portdev->ports_lock);
1519 spin_lock_irq(&port->inbuf_lock);
1520 if (port->guest_connected) {
1521 /* Let the app know the port is going down. */
1522 send_sigio_to_port(port);
1525 port->guest_connected = false;
1526 port->host_connected = false;
1528 wake_up_interruptible(&port->waitqueue);
1530 spin_unlock_irq(&port->inbuf_lock);
1532 if (is_console_port(port)) {
1534 list_del(&port->cons.list);
1536 hvc_remove(port->cons.hvc);
1539 remove_port_data(port);
1543 * else a close on an open port later will try to send out a
1546 port->portdev = NULL;
1548 sysfs_remove_group(&port->dev->kobj, &port_attribute_group);
1549 device_destroy(pdrvdata.class, port->dev->devt);
1550 cdev_del(port->cdev);
1552 debugfs_remove(port->debugfs_file);
1553 kfree(port->name);
1556 * Locks around here are not necessary - a port can't be
1557 * opened after we removed the port struct from ports_list
1560 kref_put(&port->kref, remove_port);
1569 struct port *port;
1575 port = find_port_by_id(portdev, virtio32_to_cpu(vdev, cpkt->id));
1576 if (!port &&
1586 if (port) {
1588 "Port %u already added\n", port->id);
1589 send_control_msg(port, VIRTIO_CONSOLE_PORT_READY, 1);
1595 "Request for adding port with "
1603 unplug_port(port);
1608 if (is_console_port(port))
1611 init_port_console(port);
1614 * Could remove the port here in case init fails - but
1624 if (!is_console_port(port))
1629 set_console_size(port, size.rows, size.cols);
1631 port->cons.hvc->irq_requested = 1;
1632 resize_console(port);
1636 port->host_connected = virtio16_to_cpu(vdev, cpkt->value);
1637 wake_up_interruptible(&port->waitqueue);
1639 * If the host port got closed and the host had any
1643 spin_lock_irq(&port->outvq_lock);
1644 reclaim_consumed_buffers(port);
1645 spin_unlock_irq(&port->outvq_lock);
1651 spin_lock_irq(&port->inbuf_lock);
1652 send_sigio_to_port(port);
1653 spin_unlock_irq(&port->inbuf_lock);
1660 if (port->name)
1669 port->name = kmalloc(name_size, GFP_KERNEL);
1670 if (!port->name) {
1671 dev_err(port->dev,
1672 "Not enough space to store port name\n");
1675 strncpy(port->name, buf->buf + buf->offset + sizeof(*cpkt),
1677 port->name[name_size - 1] = 0;
1681 * create it only if we have a name for the port.
1683 err = sysfs_create_group(&port->dev->kobj,
1686 dev_err(port->dev,
1695 kobject_uevent(&port->dev->kobj, KOBJ_CHANGE);
1741 struct port *port;
1743 port = find_port_by_vq(vq->vdev->priv, vq);
1744 if (!port) {
1749 wake_up_interruptible(&port->waitqueue);
1754 struct port *port;
1757 port = find_port_by_vq(vq->vdev->priv, vq);
1758 if (!port) {
1763 spin_lock_irqsave(&port->inbuf_lock, flags);
1764 port->inbuf = get_inbuf(port);
1767 * Normally the port should not accept data when the port is
1770 * can be reached when a console port is not yet connected (no
1775 * A generic serial port will discard data if not connected,
1783 if (!port->guest_connected && !is_rproc_serial(port->portdev->vdev))
1784 discard_port_data(port);
1787 send_sigio_to_port(port);
1789 spin_unlock_irqrestore(&port->inbuf_lock, flags);
1791 wake_up_interruptible(&port->waitqueue);
1793 if (is_console_port(port) && hvc_poll(port->cons.hvc))
1822 struct port *port;
1829 port = find_port_by_id(portdev, 0);
1830 set_console_size(port, rows, cols);
1837 * done per-port.
1839 resize_console(port);
1870 * spawns a console port first and also inits the vqs for port
1954 struct port *port, *port2;
1977 list_for_each_entry_safe(port, port2, &portdev->ports, list)
1978 unplug_port(port);
1987 * have to just stop using the port, as the vqs are going
2000 * initialize each port found.
2087 * For backward compatibility: Create a console port
2148 struct port *port;
2165 list_for_each_entry(port, &portdev->ports, list) {
2166 virtqueue_disable_cb(port->in_vq);
2167 virtqueue_disable_cb(port->out_vq);
2170 * the port opened or closed.
2172 port->host_connected = false;
2173 remove_port_data(port);
2183 struct port *port;
2197 list_for_each_entry(port, &portdev->ports, list) {
2198 port->in_vq = portdev->in_vqs[port->id];
2199 port->out_vq = portdev->out_vqs[port->id];
2201 fill_queue(port->in_vq, &port->inbuf_lock);
2203 /* Get port open/close status on the host */
2204 send_control_msg(port, VIRTIO_CONSOLE_PORT_READY, 1);
2207 * If a port was open at the time of suspending, we
2210 if (port->guest_connected)
2211 send_control_msg(port, VIRTIO_CONSOLE_PORT_OPEN, 1);