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 */
233 static struct port *find_port_by_vtermno(u32 vtermno)
235 struct port *port;
242 port = container_of(cons, struct port, cons);
246 port = NULL;
249 return port;
252 static struct port *find_port_by_devt_in_portdev(struct ports_device *portdev,
255 struct port *port;
259 list_for_each_entry(port, &portdev->ports, list) {
260 if (port->cdev->dev == dev) {
261 kref_get(&port->kref);
265 port = NULL;
269 return port;
272 static struct port *find_port_by_devt(dev_t dev)
275 struct port *port;
280 port = find_port_by_devt_in_portdev(portdev, dev);
281 if (port)
284 port = NULL;
287 return port;
290 static struct port *find_port_by_id(struct ports_device *portdev, u32 id)
292 struct port *port;
296 list_for_each_entry(port, &portdev->ports, list)
297 if (port->id == id)
299 port = NULL;
303 return port;
306 static struct port *find_port_by_vq(struct ports_device *portdev,
309 struct port *port;
313 list_for_each_entry(port, &portdev->ports, list)
314 if (port->in_vq == vq || port->out_vq == vq)
316 port = NULL;
319 return port;
322 static bool is_console_port(struct port *port)
324 if (port->cons.hvc)
459 static struct port_buffer *get_inbuf(struct port *port)
464 if (port->inbuf)
465 return port->inbuf;
467 buf = virtqueue_get_buf(port->in_vq, &len);
471 port->stats.bytes_received += len;
496 /* Discard any unread data this port has. Callers lockers. */
497 static void discard_port_data(struct port *port)
502 if (!port->portdev) {
506 buf = get_inbuf(port);
510 port->stats.bytes_discarded += buf->len - buf->offset;
511 if (add_inbuf(port->in_vq, buf) < 0) {
515 port->inbuf = NULL;
516 buf = get_inbuf(port);
519 dev_warn(port->dev, "Errors adding %d buffers back to vq\n",
523 static bool port_has_data(struct port *port)
529 spin_lock_irqsave(&port->inbuf_lock, flags);
530 port->inbuf = get_inbuf(port);
531 if (port->inbuf)
534 spin_unlock_irqrestore(&port->inbuf_lock, flags);
569 static ssize_t send_control_msg(struct port *port, unsigned int event,
572 /* Did the port get unplugged before userspace closed it? */
573 if (port->portdev)
574 return __send_control_msg(port->portdev, port->id, event, value);
579 /* Callers must take the port->outvq_lock */
580 static void reclaim_consumed_buffers(struct port *port)
585 if (!port->portdev) {
589 while ((buf = virtqueue_get_buf(port->out_vq, &len))) {
591 port->outvq_full = false;
595 static ssize_t __send_to_port(struct port *port, struct scatterlist *sg,
604 out_vq = port->out_vq;
606 spin_lock_irqsave(&port->outvq_lock, flags);
608 reclaim_consumed_buffers(port);
621 port->outvq_full = true;
639 spin_unlock_irqrestore(&port->outvq_lock, flags);
641 port->stats.bytes_sent += in_count;
653 static ssize_t fill_readbuf(struct port *port, u8 __user *out_buf,
659 if (!out_count || !port_has_data(port))
662 buf = port->inbuf;
683 spin_lock_irqsave(&port->inbuf_lock, flags);
684 port->inbuf = NULL;
686 if (add_inbuf(port->in_vq, buf) < 0)
687 dev_warn(port->dev, "failed add_buf\n");
689 spin_unlock_irqrestore(&port->inbuf_lock, flags);
696 static bool will_read_block(struct port *port)
698 if (!port->guest_connected) {
702 return !port_has_data(port) && port->host_connected;
705 static bool will_write_block(struct port *port)
709 if (!port->guest_connected) {
713 if (!port->host_connected)
716 spin_lock_irq(&port->outvq_lock);
721 reclaim_consumed_buffers(port);
722 ret = port->outvq_full;
723 spin_unlock_irq(&port->outvq_lock);
731 struct port *port;
734 port = filp->private_data;
737 if (!port->guest_connected)
740 if (!port_has_data(port)) {
746 if (!port->host_connected)
751 ret = wait_event_freezable(port->waitqueue,
752 !will_read_block(port));
757 if (!port->guest_connected)
769 if (!port_has_data(port) && !port->host_connected)
772 return fill_readbuf(port, ubuf, count, true);
775 static int wait_port_writable(struct port *port, bool nonblock)
779 if (will_write_block(port)) {
783 ret = wait_event_freezable(port->waitqueue,
784 !will_write_block(port));
789 if (!port->guest_connected)
798 struct port *port;
808 port = filp->private_data;
812 ret = wait_port_writable(port, nonblock);
818 buf = alloc_buf(port->portdev->vdev, count, 0);
837 ret = __send_to_port(port, sg, 1, count, buf, nonblock);
903 struct port *port = filp->private_data;
921 if (is_rproc_serial(port->out_vq->vdev))
929 ret = wait_port_writable(port, filp->f_flags & O_NONBLOCK);
934 buf = alloc_buf(port->portdev->vdev, 0, occupancy);
949 ret = __send_to_port(port, buf->sg, sgl.n, sgl.len, buf, true);
962 struct port *port;
965 port = filp->private_data;
966 poll_wait(filp, &port->waitqueue, wait);
968 if (!port->guest_connected) {
973 if (!will_read_block(port))
975 if (!will_write_block(port))
977 if (!port->host_connected)
987 struct port *port;
989 port = filp->private_data;
991 /* Notify host of port being closed */
992 send_control_msg(port, VIRTIO_CONSOLE_PORT_OPEN, 0);
994 spin_lock_irq(&port->inbuf_lock);
995 port->guest_connected = false;
997 discard_port_data(port);
999 spin_unlock_irq(&port->inbuf_lock);
1001 spin_lock_irq(&port->outvq_lock);
1002 reclaim_consumed_buffers(port);
1003 spin_unlock_irq(&port->outvq_lock);
1007 * Locks aren't necessary here as a port can't be opened after
1008 * unplug, and if a port isn't unplugged, a kref would already
1009 * exist for the port. Plus, taking ports_lock here would
1011 * inside remove_port if we're the last holder of the port,
1014 kref_put(&port->kref, remove_port);
1022 struct port *port;
1025 /* We get the port with a kref here */
1026 port = find_port_by_devt(cdev->dev);
1027 if (!port) {
1031 filp->private_data = port;
1034 * Don't allow opening of console port devices -- that's done
1037 if (is_console_port(port)) {
1042 /* Allow only one process to open a particular port at a time */
1043 spin_lock_irq(&port->inbuf_lock);
1044 if (port->guest_connected) {
1045 spin_unlock_irq(&port->inbuf_lock);
1050 port->guest_connected = true;
1051 spin_unlock_irq(&port->inbuf_lock);
1053 spin_lock_irq(&port->outvq_lock);
1056 * buffers in the window of the port getting previously closed
1059 reclaim_consumed_buffers(port);
1060 spin_unlock_irq(&port->outvq_lock);
1064 /* Notify host of port being opened */
1069 kref_put(&port->kref, remove_port);
1075 struct port *port;
1077 port = filp->private_data;
1078 return fasync_helper(fd, filp, mode, &port->async_queue);
1085 * /dev/vport<device number>p<port number>
1109 struct port *port;
1114 port = find_port_by_vtermno(vtermno);
1115 if (!port)
1123 ret = __send_to_port(port, sg, 1, count, data, false);
1137 struct port *port;
1139 port = find_port_by_vtermno(vtermno);
1140 if (!port)
1144 BUG_ON(!port->in_vq);
1146 return fill_readbuf(port, (__force u8 __user *)buf, count, false);
1149 static void resize_console(struct port *port)
1153 /* The port could have been hot-unplugged */
1154 if (!port || !is_console_port(port))
1157 vdev = port->portdev->vdev;
1162 hvc_resize(port->cons.hvc, port->cons.ws);
1168 struct port *port;
1170 port = find_port_by_vtermno(hp->vtermno);
1171 if (!port)
1175 resize_console(port);
1194 static int init_port_console(struct port *port)
1199 * The Host's telling us this port is a console port. Hook it
1219 port->cons.vtermno = ret;
1220 port->cons.hvc = hvc_alloc(port->cons.vtermno, 0, &hv_ops, PAGE_SIZE);
1221 if (IS_ERR(port->cons.hvc)) {
1222 ret = PTR_ERR(port->cons.hvc);
1223 dev_err(port->dev,
1224 "error %d allocating hvc for port\n", ret);
1225 port->cons.hvc = NULL;
1226 ida_free(&vtermno_ida, port->cons.vtermno);
1230 list_add_tail(&port->cons.list, &pdrvdata.consoles);
1232 port->guest_connected = true;
1234 /* Notify host of port being opened */
1235 send_control_msg(port, VIRTIO_CONSOLE_PORT_OPEN, 1);
1243 struct port *port;
1245 port = dev_get_drvdata(dev);
1247 return sprintf(buffer, "%s\n", port->name);
1264 struct port *port = s->private;
1266 seq_printf(s, "name: %s\n", port->name ? port->name : "");
1267 seq_printf(s, "guest_connected: %d\n", port->guest_connected);
1268 seq_printf(s, "host_connected: %d\n", port->host_connected);
1269 seq_printf(s, "outvq_full: %d\n", port->outvq_full);
1270 seq_printf(s, "bytes_sent: %lu\n", port->stats.bytes_sent);
1271 seq_printf(s, "bytes_received: %lu\n", port->stats.bytes_received);
1272 seq_printf(s, "bytes_discarded: %lu\n", port->stats.bytes_discarded);
1274 is_console_port(port) ? "yes" : "no");
1275 seq_printf(s, "console_vtermno: %u\n", port->cons.vtermno);
1282 static void set_console_size(struct port *port, u16 rows, u16 cols)
1284 if (!port || !is_console_port(port))
1287 port->cons.ws.ws_row = rows;
1288 port->cons.ws.ws_col = cols;
1317 static void send_sigio_to_port(struct port *port)
1319 if (port->async_queue && port->guest_connected)
1320 kill_fasync(&port->async_queue, SIGIO, POLL_OUT);
1326 struct port *port;
1330 port = kmalloc(sizeof(*port), GFP_KERNEL);
1331 if (!port) {
1335 kref_init(&port->kref);
1337 port->portdev = portdev;
1338 port->id = id;
1340 port->name = NULL;
1341 port->inbuf = NULL;
1342 port->cons.hvc = NULL;
1343 port->async_queue = NULL;
1345 port->cons.ws.ws_row = port->cons.ws.ws_col = 0;
1346 port->cons.vtermno = 0;
1348 port->host_connected = port->guest_connected = false;
1349 port->stats = (struct port_stats) { 0 };
1351 port->outvq_full = false;
1353 port->in_vq = portdev->in_vqs[port->id];
1354 port->out_vq = portdev->out_vqs[port->id];
1356 port->cdev = cdev_alloc();
1357 if (!port->cdev) {
1358 dev_err(&port->portdev->vdev->dev, "Error allocating cdev\n");
1362 port->cdev->ops = &port_fops;
1365 err = cdev_add(port->cdev, devt, 1);
1367 dev_err(&port->portdev->vdev->dev,
1368 "Error %d adding cdev for port %u\n", err, id);
1371 port->dev = device_create(&port_class, &port->portdev->vdev->dev,
1372 devt, port, "vport%up%u",
1373 port->portdev->vdev->index, id);
1374 if (IS_ERR(port->dev)) {
1375 err = PTR_ERR(port->dev);
1376 dev_err(&port->portdev->vdev->dev,
1377 "Error %d creating device for port %u\n",
1382 spin_lock_init(&port->inbuf_lock);
1383 spin_lock_init(&port->outvq_lock);
1384 init_waitqueue_head(&port->waitqueue);
1390 err = fill_queue(port->in_vq, &port->inbuf_lock);
1392 dev_err(port->dev, "Error allocating inbufs\n");
1396 if (is_rproc_serial(port->portdev->vdev))
1399 * rproc_serial does not want the console port, only
1400 * the generic port implementation.
1402 port->host_connected = true;
1403 else if (!use_multiport(port->portdev)) {
1406 * this has to be a console port.
1408 err = init_port_console(port);
1414 list_add_tail(&port->list, &port->portdev->ports);
1419 * configuration parameters for this port (eg, port name,
1420 * caching, whether this is a console port, etc.)
1422 send_control_msg(port, VIRTIO_CONSOLE_PORT_READY, 1);
1426 * inspect a port's state at any time
1429 port->portdev->vdev->index, id);
1430 port->debugfs_file = debugfs_create_file(debugfs_name, 0444,
1432 port, &port_debugfs_fops);
1437 device_destroy(&port_class, port->dev->devt);
1439 cdev_del(port->cdev);
1441 kfree(port);
1443 /* The host might want to notify management sw about port add failure */
1448 /* No users remain, remove all port-specific data. */
1451 struct port *port;
1453 port = container_of(kref, struct port, kref);
1455 kfree(port);
1458 static void remove_port_data(struct port *port)
1460 spin_lock_irq(&port->inbuf_lock);
1461 /* Remove unused data this port might have received. */
1462 discard_port_data(port);
1463 spin_unlock_irq(&port->inbuf_lock);
1465 spin_lock_irq(&port->outvq_lock);
1466 reclaim_consumed_buffers(port);
1467 spin_unlock_irq(&port->outvq_lock);
1471 * Port got unplugged. Remove port from portdev's list and drop the
1472 * kref reference. If no userspace has this port opened, it will
1473 * result in immediate removal the port.
1475 static void unplug_port(struct port *port)
1477 spin_lock_irq(&port->portdev->ports_lock);
1478 list_del(&port->list);
1479 spin_unlock_irq(&port->portdev->ports_lock);
1481 spin_lock_irq(&port->inbuf_lock);
1482 if (port->guest_connected) {
1483 /* Let the app know the port is going down. */
1484 send_sigio_to_port(port);
1487 port->guest_connected = false;
1488 port->host_connected = false;
1490 wake_up_interruptible(&port->waitqueue);
1492 spin_unlock_irq(&port->inbuf_lock);
1494 if (is_console_port(port)) {
1496 list_del(&port->cons.list);
1498 hvc_remove(port->cons.hvc);
1499 ida_free(&vtermno_ida, port->cons.vtermno);
1502 remove_port_data(port);
1506 * else a close on an open port later will try to send out a
1509 port->portdev = NULL;
1511 sysfs_remove_group(&port->dev->kobj, &port_attribute_group);
1512 device_destroy(&port_class, port->dev->devt);
1513 cdev_del(port->cdev);
1515 debugfs_remove(port->debugfs_file);
1516 kfree(port->name);
1519 * Locks around here are not necessary - a port can't be
1520 * opened after we removed the port struct from ports_list
1523 kref_put(&port->kref, remove_port);
1532 struct port *port;
1538 port = find_port_by_id(portdev, virtio32_to_cpu(vdev, cpkt->id));
1539 if (!port &&
1549 if (port) {
1551 "Port %u already added\n", port->id);
1552 send_control_msg(port, VIRTIO_CONSOLE_PORT_READY, 1);
1558 "Request for adding port with "
1566 unplug_port(port);
1571 if (is_console_port(port))
1574 init_port_console(port);
1577 * Could remove the port here in case init fails - but
1587 if (!is_console_port(port))
1592 set_console_size(port, size.rows, size.cols);
1594 port->cons.hvc->irq_requested = 1;
1595 resize_console(port);
1599 port->host_connected = virtio16_to_cpu(vdev, cpkt->value);
1600 wake_up_interruptible(&port->waitqueue);
1602 * If the host port got closed and the host had any
1606 spin_lock_irq(&port->outvq_lock);
1607 reclaim_consumed_buffers(port);
1608 spin_unlock_irq(&port->outvq_lock);
1614 spin_lock_irq(&port->inbuf_lock);
1615 send_sigio_to_port(port);
1616 spin_unlock_irq(&port->inbuf_lock);
1623 if (port->name)
1632 port->name = kmalloc(name_size, GFP_KERNEL);
1633 if (!port->name) {
1634 dev_err(port->dev,
1635 "Not enough space to store port name\n");
1638 strscpy(port->name, buf->buf + buf->offset + sizeof(*cpkt),
1643 * create it only if we have a name for the port.
1645 err = sysfs_create_group(&port->dev->kobj,
1648 dev_err(port->dev,
1657 kobject_uevent(&port->dev->kobj, KOBJ_CHANGE);
1703 struct port *port;
1705 port = find_port_by_vq(vq->vdev->priv, vq);
1706 if (!port) {
1711 wake_up_interruptible(&port->waitqueue);
1716 struct port *port;
1719 port = find_port_by_vq(vq->vdev->priv, vq);
1720 if (!port) {
1725 spin_lock_irqsave(&port->inbuf_lock, flags);
1726 port->inbuf = get_inbuf(port);
1729 * Normally the port should not accept data when the port is
1732 * can be reached when a console port is not yet connected (no
1737 * A generic serial port will discard data if not connected,
1745 if (!port->guest_connected && !is_rproc_serial(port->portdev->vdev))
1746 discard_port_data(port);
1749 send_sigio_to_port(port);
1751 spin_unlock_irqrestore(&port->inbuf_lock, flags);
1753 wake_up_interruptible(&port->waitqueue);
1755 if (is_console_port(port) && hvc_poll(port->cons.hvc))
1784 struct port *port;
1791 port = find_port_by_id(portdev, 0);
1792 set_console_size(port, rows, cols);
1799 * done per-port.
1801 resize_console(port);
1832 * spawns a console port first and also inits the vqs for port
1917 struct port *port, *port2;
1940 list_for_each_entry_safe(port, port2, &portdev->ports, list)
1941 unplug_port(port);
1950 * have to just stop using the port, as the vqs are going
1963 * initialize each port found.
2054 * For backward compatibility: Create a console port
2103 struct port *port;
2120 list_for_each_entry(port, &portdev->ports, list) {
2121 virtqueue_disable_cb(port->in_vq);
2122 virtqueue_disable_cb(port->out_vq);
2125 * the port opened or closed.
2127 port->host_connected = false;
2128 remove_port_data(port);
2138 struct port *port;
2152 list_for_each_entry(port, &portdev->ports, list) {
2153 port->in_vq = portdev->in_vqs[port->id];
2154 port->out_vq = portdev->out_vqs[port->id];
2156 fill_queue(port->in_vq, &port->inbuf_lock);
2158 /* Get port open/close status on the host */
2159 send_control_msg(port, VIRTIO_CONSOLE_PORT_READY, 1);
2162 * If a port was open at the time of suspending, we
2165 if (port->guest_connected)
2166 send_control_msg(port, VIRTIO_CONSOLE_PORT_OPEN, 1);