1dbddf429SAlex Dewar // SPDX-License-Identifier: GPL-2.0
21da177e4SLinus Torvalds /*
3e99525f9SJeff Dike * Copyright (C) 2001 - 2007 Jeff Dike (jdike@{linux.intel,addtoit}.com)
41da177e4SLinus Torvalds */
51da177e4SLinus Torvalds
637185b33SAl Viro #include <linux/completion.h>
737185b33SAl Viro #include <linux/interrupt.h>
837185b33SAl Viro #include <linux/list.h>
937185b33SAl Viro #include <linux/mutex.h>
1037185b33SAl Viro #include <linux/slab.h>
1137185b33SAl Viro #include <linux/workqueue.h>
1237185b33SAl Viro #include <asm/atomic.h>
1337185b33SAl Viro #include <init.h>
1437185b33SAl Viro #include <irq_kern.h>
1537185b33SAl Viro #include <os.h>
16e99525f9SJeff Dike #include "port.h"
171da177e4SLinus Torvalds
181da177e4SLinus Torvalds struct port_list {
191da177e4SLinus Torvalds struct list_head list;
201da177e4SLinus Torvalds atomic_t wait_count;
211da177e4SLinus Torvalds int has_connection;
221da177e4SLinus Torvalds struct completion done;
231da177e4SLinus Torvalds int port;
241da177e4SLinus Torvalds int fd;
251da177e4SLinus Torvalds spinlock_t lock;
261da177e4SLinus Torvalds struct list_head pending;
271da177e4SLinus Torvalds struct list_head connections;
281da177e4SLinus Torvalds };
291da177e4SLinus Torvalds
301da177e4SLinus Torvalds struct port_dev {
311da177e4SLinus Torvalds struct port_list *port;
321da177e4SLinus Torvalds int helper_pid;
331da177e4SLinus Torvalds int telnetd_pid;
341da177e4SLinus Torvalds };
351da177e4SLinus Torvalds
361da177e4SLinus Torvalds struct connection {
371da177e4SLinus Torvalds struct list_head list;
381da177e4SLinus Torvalds int fd;
391da177e4SLinus Torvalds int helper_pid;
401da177e4SLinus Torvalds int socket[2];
411da177e4SLinus Torvalds int telnetd_pid;
421da177e4SLinus Torvalds struct port_list *port;
431da177e4SLinus Torvalds };
441da177e4SLinus Torvalds
pipe_interrupt(int irq,void * data)457bea96fdSAl Viro static irqreturn_t pipe_interrupt(int irq, void *data)
461da177e4SLinus Torvalds {
471da177e4SLinus Torvalds struct connection *conn = data;
481da177e4SLinus Torvalds int fd;
491da177e4SLinus Torvalds
501da177e4SLinus Torvalds fd = os_rcv_fd(conn->socket[0], &conn->helper_pid);
511da177e4SLinus Torvalds if (fd < 0) {
521da177e4SLinus Torvalds if (fd == -EAGAIN)
5367608e0cSJeff Dike return IRQ_NONE;
541da177e4SLinus Torvalds
551da177e4SLinus Torvalds printk(KERN_ERR "pipe_interrupt : os_rcv_fd returned %d\n",
561da177e4SLinus Torvalds -fd);
571da177e4SLinus Torvalds os_close_file(conn->fd);
581da177e4SLinus Torvalds }
591da177e4SLinus Torvalds
601da177e4SLinus Torvalds list_del(&conn->list);
611da177e4SLinus Torvalds
621da177e4SLinus Torvalds conn->fd = fd;
631da177e4SLinus Torvalds list_add(&conn->list, &conn->port->connections);
641da177e4SLinus Torvalds
651da177e4SLinus Torvalds complete(&conn->port->done);
6667608e0cSJeff Dike return IRQ_HANDLED;
671da177e4SLinus Torvalds }
681da177e4SLinus Torvalds
691da177e4SLinus Torvalds #define NO_WAITER_MSG \
701da177e4SLinus Torvalds "****\n" \
711da177e4SLinus Torvalds "There are currently no UML consoles waiting for port connections.\n" \
721da177e4SLinus Torvalds "Either disconnect from one to make it available or activate some more\n" \
731da177e4SLinus Torvalds "by enabling more consoles in the UML /etc/inittab.\n" \
741da177e4SLinus Torvalds "****\n"
751da177e4SLinus Torvalds
port_accept(struct port_list * port)761da177e4SLinus Torvalds static int port_accept(struct port_list *port)
771da177e4SLinus Torvalds {
781da177e4SLinus Torvalds struct connection *conn;
79e99525f9SJeff Dike int fd, socket[2], pid;
801da177e4SLinus Torvalds
811da177e4SLinus Torvalds fd = port_connection(port->fd, socket, &pid);
821da177e4SLinus Torvalds if (fd < 0) {
831da177e4SLinus Torvalds if (fd != -EAGAIN)
841da177e4SLinus Torvalds printk(KERN_ERR "port_accept : port_connection "
851da177e4SLinus Torvalds "returned %d\n", -fd);
861da177e4SLinus Torvalds goto out;
871da177e4SLinus Torvalds }
881da177e4SLinus Torvalds
891da177e4SLinus Torvalds conn = kmalloc(sizeof(*conn), GFP_ATOMIC);
901da177e4SLinus Torvalds if (conn == NULL) {
911da177e4SLinus Torvalds printk(KERN_ERR "port_accept : failed to allocate "
921da177e4SLinus Torvalds "connection\n");
931da177e4SLinus Torvalds goto out_close;
941da177e4SLinus Torvalds }
951da177e4SLinus Torvalds *conn = ((struct connection)
961da177e4SLinus Torvalds { .list = LIST_HEAD_INIT(conn->list),
971da177e4SLinus Torvalds .fd = fd,
981da177e4SLinus Torvalds .socket = { socket[0], socket[1] },
991da177e4SLinus Torvalds .telnetd_pid = pid,
1001da177e4SLinus Torvalds .port = port });
1011da177e4SLinus Torvalds
1021da177e4SLinus Torvalds if (um_request_irq(TELNETD_IRQ, socket[0], IRQ_READ, pipe_interrupt,
10336d46a59SJohannes Berg IRQF_SHARED, "telnetd", conn) < 0) {
1041da177e4SLinus Torvalds printk(KERN_ERR "port_accept : failed to get IRQ for "
1051da177e4SLinus Torvalds "telnetd\n");
1061da177e4SLinus Torvalds goto out_free;
1071da177e4SLinus Torvalds }
1081da177e4SLinus Torvalds
1091da177e4SLinus Torvalds if (atomic_read(&port->wait_count) == 0) {
110a6ea4cceSJeff Dike os_write_file(fd, NO_WAITER_MSG, sizeof(NO_WAITER_MSG));
111e99525f9SJeff Dike printk(KERN_ERR "No one waiting for port\n");
1121da177e4SLinus Torvalds }
1131da177e4SLinus Torvalds list_add(&conn->list, &port->pending);
11467608e0cSJeff Dike return 1;
1151da177e4SLinus Torvalds
1161da177e4SLinus Torvalds out_free:
1171da177e4SLinus Torvalds kfree(conn);
1181da177e4SLinus Torvalds out_close:
1191da177e4SLinus Torvalds os_close_file(fd);
1201da177e4SLinus Torvalds os_kill_process(pid, 1);
1211da177e4SLinus Torvalds out:
122e99525f9SJeff Dike return 0;
1231da177e4SLinus Torvalds }
1241da177e4SLinus Torvalds
1252aa9c5dbSDaniel Walker static DEFINE_MUTEX(ports_mutex);
126c59bce62SJeff Dike static LIST_HEAD(ports);
1271da177e4SLinus Torvalds
port_work_proc(struct work_struct * unused)128e99525f9SJeff Dike static void port_work_proc(struct work_struct *unused)
1291da177e4SLinus Torvalds {
1301da177e4SLinus Torvalds struct port_list *port;
1311da177e4SLinus Torvalds struct list_head *ele;
1321da177e4SLinus Torvalds unsigned long flags;
1331da177e4SLinus Torvalds
1341da177e4SLinus Torvalds local_irq_save(flags);
1351da177e4SLinus Torvalds list_for_each(ele, &ports) {
1361da177e4SLinus Torvalds port = list_entry(ele, struct port_list, list);
1371da177e4SLinus Torvalds if (!port->has_connection)
1381da177e4SLinus Torvalds continue;
139e99525f9SJeff Dike
140e99525f9SJeff Dike while (port_accept(port))
141e99525f9SJeff Dike ;
1421da177e4SLinus Torvalds port->has_connection = 0;
1431da177e4SLinus Torvalds }
1441da177e4SLinus Torvalds local_irq_restore(flags);
1451da177e4SLinus Torvalds }
1461da177e4SLinus Torvalds
147*4b038701SNick Desaulniers static DECLARE_WORK(port_work, port_work_proc);
1481da177e4SLinus Torvalds
port_interrupt(int irq,void * data)1497bea96fdSAl Viro static irqreturn_t port_interrupt(int irq, void *data)
1501da177e4SLinus Torvalds {
1511da177e4SLinus Torvalds struct port_list *port = data;
1521da177e4SLinus Torvalds
1531da177e4SLinus Torvalds port->has_connection = 1;
1541da177e4SLinus Torvalds schedule_work(&port_work);
15567608e0cSJeff Dike return IRQ_HANDLED;
1561da177e4SLinus Torvalds }
1571da177e4SLinus Torvalds
port_data(int port_num)1581da177e4SLinus Torvalds void *port_data(int port_num)
1591da177e4SLinus Torvalds {
1601da177e4SLinus Torvalds struct list_head *ele;
1611da177e4SLinus Torvalds struct port_list *port;
1621da177e4SLinus Torvalds struct port_dev *dev = NULL;
1631da177e4SLinus Torvalds int fd;
1641da177e4SLinus Torvalds
1652aa9c5dbSDaniel Walker mutex_lock(&ports_mutex);
1661da177e4SLinus Torvalds list_for_each(ele, &ports) {
1671da177e4SLinus Torvalds port = list_entry(ele, struct port_list, list);
168e99525f9SJeff Dike if (port->port == port_num)
169e99525f9SJeff Dike goto found;
1701da177e4SLinus Torvalds }
1711da177e4SLinus Torvalds port = kmalloc(sizeof(struct port_list), GFP_KERNEL);
1721da177e4SLinus Torvalds if (port == NULL) {
1731da177e4SLinus Torvalds printk(KERN_ERR "Allocation of port list failed\n");
1741da177e4SLinus Torvalds goto out;
1751da177e4SLinus Torvalds }
1761da177e4SLinus Torvalds
1771da177e4SLinus Torvalds fd = port_listen_fd(port_num);
1781da177e4SLinus Torvalds if (fd < 0) {
1791da177e4SLinus Torvalds printk(KERN_ERR "binding to port %d failed, errno = %d\n",
1801da177e4SLinus Torvalds port_num, -fd);
1811da177e4SLinus Torvalds goto out_free;
1821da177e4SLinus Torvalds }
183e99525f9SJeff Dike
1841da177e4SLinus Torvalds if (um_request_irq(ACCEPT_IRQ, fd, IRQ_READ, port_interrupt,
18536d46a59SJohannes Berg IRQF_SHARED, "port", port) < 0) {
1861da177e4SLinus Torvalds printk(KERN_ERR "Failed to get IRQ for port %d\n", port_num);
1871da177e4SLinus Torvalds goto out_close;
1881da177e4SLinus Torvalds }
1891da177e4SLinus Torvalds
1901da177e4SLinus Torvalds *port = ((struct port_list)
1911da177e4SLinus Torvalds { .list = LIST_HEAD_INIT(port->list),
1921da177e4SLinus Torvalds .wait_count = ATOMIC_INIT(0),
1931da177e4SLinus Torvalds .has_connection = 0,
1941da177e4SLinus Torvalds .port = port_num,
1951da177e4SLinus Torvalds .fd = fd,
1961da177e4SLinus Torvalds .pending = LIST_HEAD_INIT(port->pending),
1971da177e4SLinus Torvalds .connections = LIST_HEAD_INIT(port->connections) });
1981da177e4SLinus Torvalds spin_lock_init(&port->lock);
1991da177e4SLinus Torvalds init_completion(&port->done);
2001da177e4SLinus Torvalds list_add(&port->list, &ports);
2011da177e4SLinus Torvalds
2021da177e4SLinus Torvalds found:
2031da177e4SLinus Torvalds dev = kmalloc(sizeof(struct port_dev), GFP_KERNEL);
2041da177e4SLinus Torvalds if (dev == NULL) {
2051da177e4SLinus Torvalds printk(KERN_ERR "Allocation of port device entry failed\n");
2061da177e4SLinus Torvalds goto out;
2071da177e4SLinus Torvalds }
2081da177e4SLinus Torvalds
2091da177e4SLinus Torvalds *dev = ((struct port_dev) { .port = port,
2101da177e4SLinus Torvalds .helper_pid = -1,
2111da177e4SLinus Torvalds .telnetd_pid = -1 });
2121da177e4SLinus Torvalds goto out;
2131da177e4SLinus Torvalds
2141da177e4SLinus Torvalds out_close:
2151da177e4SLinus Torvalds os_close_file(fd);
21679f66233SJeff Dike out_free:
21779f66233SJeff Dike kfree(port);
2181da177e4SLinus Torvalds out:
2192aa9c5dbSDaniel Walker mutex_unlock(&ports_mutex);
22067608e0cSJeff Dike return dev;
2211da177e4SLinus Torvalds }
2221da177e4SLinus Torvalds
port_wait(void * data)2231da177e4SLinus Torvalds int port_wait(void *data)
2241da177e4SLinus Torvalds {
2251da177e4SLinus Torvalds struct port_dev *dev = data;
2261da177e4SLinus Torvalds struct connection *conn;
2271da177e4SLinus Torvalds struct port_list *port = dev->port;
2281da177e4SLinus Torvalds int fd;
2291da177e4SLinus Torvalds
2301da177e4SLinus Torvalds atomic_inc(&port->wait_count);
2311da177e4SLinus Torvalds while (1) {
2321da177e4SLinus Torvalds fd = -ERESTARTSYS;
2331da177e4SLinus Torvalds if (wait_for_completion_interruptible(&port->done))
2341da177e4SLinus Torvalds goto out;
2351da177e4SLinus Torvalds
2361da177e4SLinus Torvalds spin_lock(&port->lock);
2371da177e4SLinus Torvalds
2381da177e4SLinus Torvalds conn = list_entry(port->connections.next, struct connection,
2391da177e4SLinus Torvalds list);
2401da177e4SLinus Torvalds list_del(&conn->list);
2411da177e4SLinus Torvalds spin_unlock(&port->lock);
2421da177e4SLinus Torvalds
2431da177e4SLinus Torvalds os_shutdown_socket(conn->socket[0], 1, 1);
2441da177e4SLinus Torvalds os_close_file(conn->socket[0]);
2451da177e4SLinus Torvalds os_shutdown_socket(conn->socket[1], 1, 1);
2461da177e4SLinus Torvalds os_close_file(conn->socket[1]);
2471da177e4SLinus Torvalds
2481da177e4SLinus Torvalds /* This is done here because freeing an IRQ can't be done
2491da177e4SLinus Torvalds * within the IRQ handler. So, pipe_interrupt always ups
2501da177e4SLinus Torvalds * the semaphore regardless of whether it got a successful
2511da177e4SLinus Torvalds * connection. Then we loop here throwing out failed
2521da177e4SLinus Torvalds * connections until a good one is found.
2531da177e4SLinus Torvalds */
254fa7a0449SRichard Weinberger um_free_irq(TELNETD_IRQ, conn);
2551da177e4SLinus Torvalds
256e99525f9SJeff Dike if (conn->fd >= 0)
257e99525f9SJeff Dike break;
2581da177e4SLinus Torvalds os_close_file(conn->fd);
2591da177e4SLinus Torvalds kfree(conn);
2601da177e4SLinus Torvalds }
2611da177e4SLinus Torvalds
2621da177e4SLinus Torvalds fd = conn->fd;
2631da177e4SLinus Torvalds dev->helper_pid = conn->helper_pid;
2641da177e4SLinus Torvalds dev->telnetd_pid = conn->telnetd_pid;
2651da177e4SLinus Torvalds kfree(conn);
2661da177e4SLinus Torvalds out:
2671da177e4SLinus Torvalds atomic_dec(&port->wait_count);
2681da177e4SLinus Torvalds return fd;
2691da177e4SLinus Torvalds }
2701da177e4SLinus Torvalds
port_remove_dev(void * d)2711da177e4SLinus Torvalds void port_remove_dev(void *d)
2721da177e4SLinus Torvalds {
2731da177e4SLinus Torvalds struct port_dev *dev = d;
2741da177e4SLinus Torvalds
2751da177e4SLinus Torvalds if (dev->helper_pid != -1)
2761da177e4SLinus Torvalds os_kill_process(dev->helper_pid, 0);
2771da177e4SLinus Torvalds if (dev->telnetd_pid != -1)
2781da177e4SLinus Torvalds os_kill_process(dev->telnetd_pid, 1);
2791da177e4SLinus Torvalds dev->helper_pid = -1;
2801da177e4SLinus Torvalds dev->telnetd_pid = -1;
2811da177e4SLinus Torvalds }
2821da177e4SLinus Torvalds
port_kern_free(void * d)2831da177e4SLinus Torvalds void port_kern_free(void *d)
2841da177e4SLinus Torvalds {
2851da177e4SLinus Torvalds struct port_dev *dev = d;
2861da177e4SLinus Torvalds
2871da177e4SLinus Torvalds port_remove_dev(dev);
2881da177e4SLinus Torvalds kfree(dev);
2891da177e4SLinus Torvalds }
2901da177e4SLinus Torvalds
free_port(void)2911da177e4SLinus Torvalds static void free_port(void)
2921da177e4SLinus Torvalds {
2931da177e4SLinus Torvalds struct list_head *ele;
2941da177e4SLinus Torvalds struct port_list *port;
2951da177e4SLinus Torvalds
2961da177e4SLinus Torvalds list_for_each(ele, &ports) {
2971da177e4SLinus Torvalds port = list_entry(ele, struct port_list, list);
2981da177e4SLinus Torvalds free_irq_by_fd(port->fd);
2991da177e4SLinus Torvalds os_close_file(port->fd);
3001da177e4SLinus Torvalds }
3011da177e4SLinus Torvalds }
3021da177e4SLinus Torvalds
3031da177e4SLinus Torvalds __uml_exitcall(free_port);
304