Linux 6.16-rc6
[linux-2.6-block.git] / drivers / char / virtio_console.c
CommitLineData
1a59d1b8 1// SPDX-License-Identifier: GPL-2.0-or-later
a23ea924
RR
2/*
3 * Copyright (C) 2006, 2007, 2009 Rusty Russell, IBM Corporation
5084f893
AS
4 * Copyright (C) 2009, 2010, 2011 Red Hat, Inc.
5 * Copyright (C) 2009, 2010, 2011 Amit Shah <amit.shah@redhat.com>
31610434 6 */
fb08bd27 7#include <linux/cdev.h>
d99393ef 8#include <linux/debugfs.h>
5e38483b 9#include <linux/completion.h>
fb08bd27 10#include <linux/device.h>
31610434 11#include <linux/err.h>
a08fa92d 12#include <linux/freezer.h>
2030fa49 13#include <linux/fs.h>
eb5e89fc
MH
14#include <linux/splice.h>
15#include <linux/pagemap.h>
e6278a54 16#include <linux/idr.h>
31610434 17#include <linux/init.h>
38edf58d 18#include <linux/list.h>
2030fa49
AS
19#include <linux/poll.h>
20#include <linux/sched.h>
5a0e3ad6 21#include <linux/slab.h>
38edf58d 22#include <linux/spinlock.h>
31610434
RR
23#include <linux/virtio.h>
24#include <linux/virtio_console.h>
2030fa49 25#include <linux/wait.h>
17634ba2 26#include <linux/workqueue.h>
c22405c9 27#include <linux/module.h>
1b637046 28#include <linux/dma-mapping.h>
790b2f24 29#include <linux/string_choices.h>
51df0acc 30#include "../tty/hvc/hvc_console.h"
31610434 31
1b637046 32#define is_rproc_enabled IS_ENABLED(CONFIG_REMOTEPROC)
28962ec5 33#define VIRTCONS_MAX_PORTS 0x8000
1b637046 34
38edf58d
AS
35/*
36 * This is a global struct for storing common data for all the devices
37 * this driver handles.
38 *
39 * Mainly, it has a linked list for all the consoles in one place so
40 * that callbacks from hvc for get_chars(), put_chars() work properly
41 * across multiple devices and multiple ports per device.
42 */
43struct ports_driver_data {
d99393ef
AS
44 /* Used for exporting per-port information to debugfs */
45 struct dentry *debugfs_dir;
46
6bdf2afd
AS
47 /* List of all the devices we're handling */
48 struct list_head portdevs;
49
38edf58d
AS
50 /* All the console devices handled by this driver */
51 struct list_head consoles;
52};
e6278a54
CLG
53
54static struct ports_driver_data pdrvdata;
38edf58d 55
11680fdf
IO
56static const struct class port_class = {
57 .name = "virtio-ports",
58};
59
3826835a
WY
60static DEFINE_SPINLOCK(pdrvdata_lock);
61static DECLARE_COMPLETION(early_console_added);
38edf58d 62
4f23c573
AS
63/* This struct holds information that's relevant only for console ports */
64struct console {
65 /* We'll place all consoles in a list in the pdrvdata struct */
66 struct list_head list;
67
68 /* The hvc device associated with this console port */
69 struct hvc_struct *hvc;
70
9778829c
AS
71 /* The size of the console */
72 struct winsize ws;
73
4f23c573
AS
74 /*
75 * This number identifies the number that we used to register
76 * with hvc in hvc_instantiate() and hvc_alloc(); this is the
77 * number passed on by the hvc callbacks to us to
78 * differentiate between the other console ports handled by
79 * this driver
80 */
81 u32 vtermno;
82};
83
e6278a54
CLG
84static DEFINE_IDA(vtermno_ida);
85
fdb9a054
AS
86struct port_buffer {
87 char *buf;
88
89 /* size of the buffer in *buf above */
90 size_t size;
91
92 /* used length of the buffer */
93 size_t len;
94 /* offset in the buf from which to consume data */
95 size_t offset;
276a3e95 96
1b637046
SB
97 /* DMA address of buffer */
98 dma_addr_t dma;
99
100 /* Device we got DMA memory from */
101 struct device *dev;
102
103 /* List of pending dma buffers to free */
104 struct list_head list;
105
276a3e95
SB
106 /* If sgpages == 0 then buf is used */
107 unsigned int sgpages;
108
109 /* sg is used if spages > 0. sg must be the last in is struct */
bf5abc17 110 struct scatterlist sg[] __counted_by(sgpages);
fdb9a054
AS
111};
112
17634ba2
AS
113/*
114 * This is a per-device struct that stores data common to all the
115 * ports for that device (vdev->priv).
116 */
117struct ports_device {
6bdf2afd
AS
118 /* Next portdev in the list, head is in the pdrvdata struct */
119 struct list_head list;
120
17634ba2
AS
121 /*
122 * Workqueue handlers where we process deferred work after
123 * notification
124 */
125 struct work_struct control_work;
eeb8a7e8 126 struct work_struct config_work;
17634ba2
AS
127
128 struct list_head ports;
129
130 /* To protect the list of ports */
131 spinlock_t ports_lock;
132
133 /* To protect the vq operations for the control channel */
165b1b8b 134 spinlock_t c_ivq_lock;
9ba5c80b 135 spinlock_t c_ovq_lock;
17634ba2 136
7328fa64
MT
137 /* max. number of ports this device can hold */
138 u32 max_nr_ports;
17634ba2
AS
139
140 /* The virtio device we're associated with */
141 struct virtio_device *vdev;
142
143 /*
144 * A couple of virtqueues for the control channel: one for
145 * guest->host transfers, one for host->guest transfers
146 */
147 struct virtqueue *c_ivq, *c_ovq;
148
5e59d9a1
AL
149 /*
150 * A control packet buffer for guest->host requests, protected
151 * by c_ovq_lock.
152 */
153 struct virtio_console_control cpkt;
154
17634ba2
AS
155 /* Array of per-port IO virtqueues */
156 struct virtqueue **in_vqs, **out_vqs;
fb08bd27 157
fb08bd27
AS
158 /* Major number for this device. Ports will be created as minors. */
159 int chr_major;
17634ba2
AS
160};
161
17e5b4f2
AS
162struct port_stats {
163 unsigned long bytes_sent, bytes_received, bytes_discarded;
164};
165
1c85bf35 166/* This struct holds the per-port data */
21206ede 167struct port {
17634ba2
AS
168 /* Next port in the list, head is in the ports_device */
169 struct list_head list;
170
1c85bf35
AS
171 /* Pointer to the parent virtio_console device */
172 struct ports_device *portdev;
fdb9a054
AS
173
174 /* The current buffer from which data has to be fed to readers */
175 struct port_buffer *inbuf;
21206ede 176
203baab8
AS
177 /*
178 * To protect the operations on the in_vq associated with this
179 * port. Has to be a spinlock because it can be called from
180 * interrupt context (get_char()).
181 */
182 spinlock_t inbuf_lock;
183
cdfadfc1
AS
184 /* Protect the operations on the out_vq. */
185 spinlock_t outvq_lock;
186
1c85bf35
AS
187 /* The IO vqs for this port */
188 struct virtqueue *in_vq, *out_vq;
189
d99393ef
AS
190 /* File in the debugfs directory that exposes this port's information */
191 struct dentry *debugfs_file;
192
17e5b4f2
AS
193 /*
194 * Keep count of the bytes sent, received and discarded for
195 * this port for accounting and debugging purposes. These
196 * counts are not reset across port open / close events.
197 */
198 struct port_stats stats;
199
4f23c573
AS
200 /*
201 * The entries in this struct will be valid if this port is
202 * hooked up to an hvc console
203 */
204 struct console cons;
17634ba2 205
fb08bd27 206 /* Each port associates with a separate char device */
d22a6989 207 struct cdev *cdev;
fb08bd27
AS
208 struct device *dev;
209
b353a6b8
AS
210 /* Reference-counting to handle port hot-unplugs and file operations */
211 struct kref kref;
212
2030fa49
AS
213 /* A waitqueue for poll() or blocking read operations */
214 wait_queue_head_t waitqueue;
215
431edb8a
AS
216 /* The 'name' of the port that we expose via sysfs properties */
217 char *name;
218
3eae0ade
AS
219 /* We can notify apps of host connect / disconnect events via SIGIO */
220 struct fasync_struct *async_queue;
221
17634ba2
AS
222 /* The 'id' to identify the port with the Host */
223 u32 id;
2030fa49 224
cdfadfc1
AS
225 bool outvq_full;
226
2030fa49
AS
227 /* Is the host device open */
228 bool host_connected;
3c7969cc
AS
229
230 /* We should allow only one process to open a port */
231 bool guest_connected;
21206ede 232};
31610434 233
38edf58d
AS
234static struct port *find_port_by_vtermno(u32 vtermno)
235{
236 struct port *port;
4f23c573 237 struct console *cons;
38edf58d
AS
238 unsigned long flags;
239
240 spin_lock_irqsave(&pdrvdata_lock, flags);
4f23c573
AS
241 list_for_each_entry(cons, &pdrvdata.consoles, list) {
242 if (cons->vtermno == vtermno) {
243 port = container_of(cons, struct port, cons);
38edf58d 244 goto out;
4f23c573 245 }
38edf58d
AS
246 }
247 port = NULL;
248out:
249 spin_unlock_irqrestore(&pdrvdata_lock, flags);
250 return port;
251}
252
04950cdf
AS
253static struct port *find_port_by_devt_in_portdev(struct ports_device *portdev,
254 dev_t dev)
255{
256 struct port *port;
257 unsigned long flags;
258
259 spin_lock_irqsave(&portdev->ports_lock, flags);
057b82be
AS
260 list_for_each_entry(port, &portdev->ports, list) {
261 if (port->cdev->dev == dev) {
262 kref_get(&port->kref);
04950cdf 263 goto out;
057b82be
AS
264 }
265 }
04950cdf
AS
266 port = NULL;
267out:
268 spin_unlock_irqrestore(&portdev->ports_lock, flags);
269
270 return port;
271}
272
273static struct port *find_port_by_devt(dev_t dev)
274{
275 struct ports_device *portdev;
276 struct port *port;
277 unsigned long flags;
278
279 spin_lock_irqsave(&pdrvdata_lock, flags);
280 list_for_each_entry(portdev, &pdrvdata.portdevs, list) {
281 port = find_port_by_devt_in_portdev(portdev, dev);
282 if (port)
283 goto out;
284 }
285 port = NULL;
286out:
287 spin_unlock_irqrestore(&pdrvdata_lock, flags);
288 return port;
289}
290
17634ba2
AS
291static struct port *find_port_by_id(struct ports_device *portdev, u32 id)
292{
293 struct port *port;
294 unsigned long flags;
295
296 spin_lock_irqsave(&portdev->ports_lock, flags);
297 list_for_each_entry(port, &portdev->ports, list)
298 if (port->id == id)
299 goto out;
300 port = NULL;
301out:
302 spin_unlock_irqrestore(&portdev->ports_lock, flags);
303
304 return port;
305}
306
203baab8
AS
307static struct port *find_port_by_vq(struct ports_device *portdev,
308 struct virtqueue *vq)
309{
310 struct port *port;
203baab8
AS
311 unsigned long flags;
312
17634ba2
AS
313 spin_lock_irqsave(&portdev->ports_lock, flags);
314 list_for_each_entry(port, &portdev->ports, list)
203baab8
AS
315 if (port->in_vq == vq || port->out_vq == vq)
316 goto out;
203baab8
AS
317 port = NULL;
318out:
17634ba2 319 spin_unlock_irqrestore(&portdev->ports_lock, flags);
203baab8
AS
320 return port;
321}
322
17634ba2
AS
323static bool is_console_port(struct port *port)
324{
325 if (port->cons.hvc)
326 return true;
327 return false;
328}
329
1b637046
SB
330static bool is_rproc_serial(const struct virtio_device *vdev)
331{
332 return is_rproc_enabled && vdev->id.device == VIRTIO_ID_RPROC_SERIAL;
333}
334
17634ba2
AS
335static inline bool use_multiport(struct ports_device *portdev)
336{
337 /*
338 * This condition can be true when put_chars is called from
339 * early_init
340 */
341 if (!portdev->vdev)
f580d730 342 return false;
e16e12be 343 return __virtio_test_bit(portdev->vdev, VIRTIO_CONSOLE_F_MULTIPORT);
17634ba2
AS
344}
345
1b637046
SB
346static DEFINE_SPINLOCK(dma_bufs_lock);
347static LIST_HEAD(pending_free_dma_bufs);
348
349static void free_buf(struct port_buffer *buf, bool can_sleep)
fdb9a054 350{
276a3e95
SB
351 unsigned int i;
352
276a3e95
SB
353 for (i = 0; i < buf->sgpages; i++) {
354 struct page *page = sg_page(&buf->sg[i]);
355 if (!page)
356 break;
357 put_page(page);
358 }
359
1b637046
SB
360 if (!buf->dev) {
361 kfree(buf->buf);
362 } else if (is_rproc_enabled) {
363 unsigned long flags;
364
365 /* dma_free_coherent requires interrupts to be enabled. */
366 if (!can_sleep) {
367 /* queue up dma-buffers to be freed later */
368 spin_lock_irqsave(&dma_bufs_lock, flags);
369 list_add_tail(&buf->list, &pending_free_dma_bufs);
370 spin_unlock_irqrestore(&dma_bufs_lock, flags);
371 return;
372 }
373 dma_free_coherent(buf->dev, buf->size, buf->buf, buf->dma);
374
375 /* Release device refcnt and allow it to be freed */
376 put_device(buf->dev);
377 }
378
fdb9a054
AS
379 kfree(buf);
380}
381
1b637046
SB
382static void reclaim_dma_bufs(void)
383{
384 unsigned long flags;
385 struct port_buffer *buf, *tmp;
386 LIST_HEAD(tmp_list);
387
388 if (list_empty(&pending_free_dma_bufs))
389 return;
390
391 /* Create a copy of the pending_free_dma_bufs while holding the lock */
392 spin_lock_irqsave(&dma_bufs_lock, flags);
393 list_cut_position(&tmp_list, &pending_free_dma_bufs,
394 pending_free_dma_bufs.prev);
395 spin_unlock_irqrestore(&dma_bufs_lock, flags);
396
397 /* Release the dma buffers, without irqs enabled */
398 list_for_each_entry_safe(buf, tmp, &tmp_list, list) {
399 list_del(&buf->list);
400 free_buf(buf, true);
401 }
402}
403
2855b335 404static struct port_buffer *alloc_buf(struct virtio_device *vdev, size_t buf_size,
276a3e95 405 int pages)
fdb9a054
AS
406{
407 struct port_buffer *buf;
408
1b637046
SB
409 reclaim_dma_bufs();
410
276a3e95
SB
411 /*
412 * Allocate buffer and the sg list. The sg list array is allocated
413 * directly after the port_buffer struct.
414 */
5b572e25 415 buf = kmalloc(struct_size(buf, sg, pages), GFP_KERNEL);
fdb9a054
AS
416 if (!buf)
417 goto fail;
276a3e95
SB
418
419 buf->sgpages = pages;
420 if (pages > 0) {
1b637046 421 buf->dev = NULL;
276a3e95
SB
422 buf->buf = NULL;
423 return buf;
424 }
425
2855b335 426 if (is_rproc_serial(vdev)) {
1b637046
SB
427 /*
428 * Allocate DMA memory from ancestor. When a virtio
429 * device is created by remoteproc, the DMA memory is
9d516aa8
AL
430 * associated with the parent device:
431 * virtioY => remoteprocX#vdevYbuffer.
1b637046 432 */
9d516aa8
AL
433 buf->dev = vdev->dev.parent;
434 if (!buf->dev)
1b637046 435 goto free_buf;
1b637046
SB
436
437 /* Increase device refcnt to avoid freeing it */
438 get_device(buf->dev);
439 buf->buf = dma_alloc_coherent(buf->dev, buf_size, &buf->dma,
440 GFP_KERNEL);
441 } else {
442 buf->dev = NULL;
443 buf->buf = kmalloc(buf_size, GFP_KERNEL);
444 }
445
fdb9a054
AS
446 if (!buf->buf)
447 goto free_buf;
448 buf->len = 0;
449 buf->offset = 0;
450 buf->size = buf_size;
451 return buf;
452
453free_buf:
454 kfree(buf);
455fail:
456 return NULL;
457}
458
a3cde449 459/* Callers should take appropriate locks */
defde669 460static struct port_buffer *get_inbuf(struct port *port)
a3cde449
AS
461{
462 struct port_buffer *buf;
a3cde449
AS
463 unsigned int len;
464
d25a9dda
AS
465 if (port->inbuf)
466 return port->inbuf;
467
468 buf = virtqueue_get_buf(port->in_vq, &len);
a3cde449 469 if (buf) {
d00d8da5 470 buf->len = min_t(size_t, len, buf->size);
a3cde449 471 buf->offset = 0;
17e5b4f2 472 port->stats.bytes_received += len;
a3cde449
AS
473 }
474 return buf;
475}
476
e27b5198
AS
477/*
478 * Create a scatter-gather list representing our input buffer and put
479 * it in the queue.
480 *
481 * Callers should take appropriate locks.
482 */
203baab8 483static int add_inbuf(struct virtqueue *vq, struct port_buffer *buf)
e27b5198
AS
484{
485 struct scatterlist sg[1];
203baab8 486 int ret;
1c85bf35 487
e27b5198
AS
488 sg_init_one(sg, buf->buf, buf->size);
489
6797999d 490 ret = virtqueue_add_inbuf(vq, sg, 1, buf, GFP_ATOMIC);
505b0451 491 virtqueue_kick(vq);
49e86f16
AS
492 if (!ret)
493 ret = vq->num_free;
203baab8
AS
494 return ret;
495}
496
88f251ac
AS
497/* Discard any unread data this port has. Callers lockers. */
498static void discard_port_data(struct port *port)
499{
500 struct port_buffer *buf;
2d24cdaa 501 unsigned int err;
88f251ac 502
d7a62cd0
AS
503 if (!port->portdev) {
504 /* Device has been unplugged. vqs are already gone. */
505 return;
506 }
2d24cdaa 507 buf = get_inbuf(port);
88f251ac 508
ce072a0c 509 err = 0;
d6933561 510 while (buf) {
17e5b4f2 511 port->stats.bytes_discarded += buf->len - buf->offset;
2d24cdaa 512 if (add_inbuf(port->in_vq, buf) < 0) {
ce072a0c 513 err++;
1b637046 514 free_buf(buf, false);
d6933561 515 }
2d24cdaa
AS
516 port->inbuf = NULL;
517 buf = get_inbuf(port);
88f251ac 518 }
ce072a0c 519 if (err)
d6933561 520 dev_warn(port->dev, "Errors adding %d buffers back to vq\n",
ce072a0c 521 err);
88f251ac
AS
522}
523
203baab8
AS
524static bool port_has_data(struct port *port)
525{
526 unsigned long flags;
527 bool ret;
528
d25a9dda 529 ret = false;
203baab8 530 spin_lock_irqsave(&port->inbuf_lock, flags);
d6933561 531 port->inbuf = get_inbuf(port);
d25a9dda 532 if (port->inbuf)
d6933561 533 ret = true;
d25a9dda 534
203baab8 535 spin_unlock_irqrestore(&port->inbuf_lock, flags);
203baab8
AS
536 return ret;
537}
538
3425e706
AS
539static ssize_t __send_control_msg(struct ports_device *portdev, u32 port_id,
540 unsigned int event, unsigned int value)
17634ba2
AS
541{
542 struct scatterlist sg[1];
17634ba2 543 struct virtqueue *vq;
604b2ad7 544 unsigned int len;
17634ba2 545
3425e706 546 if (!use_multiport(portdev))
17634ba2
AS
547 return 0;
548
3425e706 549 vq = portdev->c_ovq;
17634ba2 550
9ba5c80b 551 spin_lock(&portdev->c_ovq_lock);
5e59d9a1
AL
552
553 portdev->cpkt.id = cpu_to_virtio32(portdev->vdev, port_id);
554 portdev->cpkt.event = cpu_to_virtio16(portdev->vdev, event);
555 portdev->cpkt.value = cpu_to_virtio16(portdev->vdev, value);
556
557 sg_init_one(sg, &portdev->cpkt, sizeof(struct virtio_console_control));
558
559 if (virtqueue_add_outbuf(vq, sg, 1, &portdev->cpkt, GFP_ATOMIC) == 0) {
505b0451 560 virtqueue_kick(vq);
40e4dc53
HG
561 while (!virtqueue_get_buf(vq, &len)
562 && !virtqueue_is_broken(vq))
17634ba2
AS
563 cpu_relax();
564 }
5e59d9a1 565
9ba5c80b 566 spin_unlock(&portdev->c_ovq_lock);
17634ba2
AS
567 return 0;
568}
569
3425e706
AS
570static ssize_t send_control_msg(struct port *port, unsigned int event,
571 unsigned int value)
572{
84ec06c5
AS
573 /* Did the port get unplugged before userspace closed it? */
574 if (port->portdev)
575 return __send_control_msg(port->portdev, port->id, event, value);
576 return 0;
3425e706
AS
577}
578
eb5e89fc 579
cdfadfc1
AS
580/* Callers must take the port->outvq_lock */
581static void reclaim_consumed_buffers(struct port *port)
582{
276a3e95 583 struct port_buffer *buf;
cdfadfc1
AS
584 unsigned int len;
585
d7a62cd0
AS
586 if (!port->portdev) {
587 /* Device has been unplugged. vqs are already gone. */
588 return;
589 }
276a3e95 590 while ((buf = virtqueue_get_buf(port->out_vq, &len))) {
1b637046 591 free_buf(buf, false);
cdfadfc1
AS
592 port->outvq_full = false;
593 }
594}
595
eb5e89fc
MH
596static ssize_t __send_to_port(struct port *port, struct scatterlist *sg,
597 int nents, size_t in_count,
276a3e95 598 void *data, bool nonblock)
f997f00b 599{
f997f00b 600 struct virtqueue *out_vq;
589575a2 601 int err;
cdfadfc1 602 unsigned long flags;
f997f00b
AS
603 unsigned int len;
604
605 out_vq = port->out_vq;
606
cdfadfc1
AS
607 spin_lock_irqsave(&port->outvq_lock, flags);
608
609 reclaim_consumed_buffers(port);
610
6797999d 611 err = virtqueue_add_outbuf(out_vq, sg, nents, data, GFP_ATOMIC);
f997f00b
AS
612
613 /* Tell Host to go! */
505b0451 614 virtqueue_kick(out_vq);
f997f00b 615
589575a2 616 if (err) {
9ff4cfab 617 in_count = 0;
cdfadfc1 618 goto done;
f997f00b
AS
619 }
620
589575a2 621 if (out_vq->num_free == 0)
cdfadfc1
AS
622 port->outvq_full = true;
623
624 if (nonblock)
625 goto done;
626
627 /*
628 * Wait till the host acknowledges it pushed out the data we
531295e6
AS
629 * sent. This is done for data from the hvc_console; the tty
630 * operations are performed with spinlocks held so we can't
631 * sleep here. An alternative would be to copy the data to a
632 * buffer and relax the spinning requirement. The downside is
633 * we need to kmalloc a GFP_ATOMIC buffer each time the
634 * console driver writes something out.
cdfadfc1 635 */
40e4dc53
HG
636 while (!virtqueue_get_buf(out_vq, &len)
637 && !virtqueue_is_broken(out_vq))
f997f00b 638 cpu_relax();
cdfadfc1
AS
639done:
640 spin_unlock_irqrestore(&port->outvq_lock, flags);
17e5b4f2
AS
641
642 port->stats.bytes_sent += in_count;
cdfadfc1
AS
643 /*
644 * We're expected to return the amount of data we wrote -- all
645 * of it
646 */
9ff4cfab 647 return in_count;
f997f00b
AS
648}
649
203baab8
AS
650/*
651 * Give out the data that's requested from the buffer that we have
652 * queued up.
653 */
f32fcbed 654static ssize_t fill_readbuf(struct port *port, u8 __user *out_buf,
48b36066 655 size_t out_count, bool to_user)
203baab8
AS
656{
657 struct port_buffer *buf;
658 unsigned long flags;
659
660 if (!out_count || !port_has_data(port))
661 return 0;
662
663 buf = port->inbuf;
b766ceed 664 out_count = min(out_count, buf->len - buf->offset);
203baab8 665
b766ceed
AS
666 if (to_user) {
667 ssize_t ret;
668
669 ret = copy_to_user(out_buf, buf->buf + buf->offset, out_count);
670 if (ret)
671 return -EFAULT;
672 } else {
f32fcbed 673 memcpy((__force u8 *)out_buf, buf->buf + buf->offset,
48b36066 674 out_count);
b766ceed 675 }
203baab8 676
203baab8
AS
677 buf->offset += out_count;
678
679 if (buf->offset == buf->len) {
680 /*
681 * We're done using all the data in this buffer.
682 * Re-queue so that the Host can send us more data.
683 */
684 spin_lock_irqsave(&port->inbuf_lock, flags);
685 port->inbuf = NULL;
686
687 if (add_inbuf(port->in_vq, buf) < 0)
fb08bd27 688 dev_warn(port->dev, "failed add_buf\n");
203baab8
AS
689
690 spin_unlock_irqrestore(&port->inbuf_lock, flags);
691 }
b766ceed 692 /* Return the number of bytes actually copied */
203baab8 693 return out_count;
e27b5198
AS
694}
695
2030fa49 696/* The condition that must be true for polling to end */
60caacd3 697static bool will_read_block(struct port *port)
2030fa49 698{
3709ea7a
AS
699 if (!port->guest_connected) {
700 /* Port got hot-unplugged. Let's exit. */
701 return false;
702 }
60caacd3 703 return !port_has_data(port) && port->host_connected;
2030fa49
AS
704}
705
cdfadfc1
AS
706static bool will_write_block(struct port *port)
707{
708 bool ret;
709
60e5e0b8
AS
710 if (!port->guest_connected) {
711 /* Port got hot-unplugged. Let's exit. */
712 return false;
713 }
cdfadfc1
AS
714 if (!port->host_connected)
715 return true;
716
717 spin_lock_irq(&port->outvq_lock);
718 /*
719 * Check if the Host has consumed any buffers since we last
720 * sent data (this is only applicable for nonblocking ports).
721 */
722 reclaim_consumed_buffers(port);
723 ret = port->outvq_full;
724 spin_unlock_irq(&port->outvq_lock);
725
726 return ret;
727}
728
2030fa49
AS
729static ssize_t port_fops_read(struct file *filp, char __user *ubuf,
730 size_t count, loff_t *offp)
731{
732 struct port *port;
733 ssize_t ret;
734
735 port = filp->private_data;
736
96f97a83
AS
737 /* Port is hot-unplugged. */
738 if (!port->guest_connected)
739 return -ENODEV;
740
2030fa49
AS
741 if (!port_has_data(port)) {
742 /*
743 * If nothing's connected on the host just return 0 in
744 * case of list_empty; this tells the userspace app
745 * that there's no connection
746 */
747 if (!port->host_connected)
748 return 0;
749 if (filp->f_flags & O_NONBLOCK)
750 return -EAGAIN;
751
a08fa92d
AS
752 ret = wait_event_freezable(port->waitqueue,
753 !will_read_block(port));
2030fa49
AS
754 if (ret < 0)
755 return ret;
756 }
96f97a83 757 /* Port got hot-unplugged while we were waiting above. */
b3dddb9e
AS
758 if (!port->guest_connected)
759 return -ENODEV;
2030fa49
AS
760 /*
761 * We could've received a disconnection message while we were
762 * waiting for more data.
763 *
764 * This check is not clubbed in the if() statement above as we
765 * might receive some data as well as the host could get
766 * disconnected after we got woken up from our wait. So we
767 * really want to give off whatever data we have and only then
768 * check for host_connected.
769 */
770 if (!port_has_data(port) && !port->host_connected)
771 return 0;
772
773 return fill_readbuf(port, ubuf, count, true);
774}
775
efe75d24
MH
776static int wait_port_writable(struct port *port, bool nonblock)
777{
778 int ret;
779
780 if (will_write_block(port)) {
781 if (nonblock)
782 return -EAGAIN;
783
784 ret = wait_event_freezable(port->waitqueue,
785 !will_write_block(port));
786 if (ret < 0)
787 return ret;
788 }
789 /* Port got hot-unplugged. */
790 if (!port->guest_connected)
791 return -ENODEV;
792
793 return 0;
794}
795
2030fa49
AS
796static ssize_t port_fops_write(struct file *filp, const char __user *ubuf,
797 size_t count, loff_t *offp)
798{
799 struct port *port;
276a3e95 800 struct port_buffer *buf;
2030fa49 801 ssize_t ret;
cdfadfc1 802 bool nonblock;
276a3e95 803 struct scatterlist sg[1];
2030fa49 804
65745422
AS
805 /* Userspace could be out to fool us */
806 if (!count)
807 return 0;
808
2030fa49
AS
809 port = filp->private_data;
810
cdfadfc1
AS
811 nonblock = filp->f_flags & O_NONBLOCK;
812
efe75d24
MH
813 ret = wait_port_writable(port, nonblock);
814 if (ret < 0)
815 return ret;
cdfadfc1 816
2030fa49
AS
817 count = min((size_t)(32 * 1024), count);
818
2855b335 819 buf = alloc_buf(port->portdev->vdev, count, 0);
2030fa49
AS
820 if (!buf)
821 return -ENOMEM;
822
276a3e95 823 ret = copy_from_user(buf->buf, ubuf, count);
2030fa49
AS
824 if (ret) {
825 ret = -EFAULT;
826 goto free_buf;
827 }
828
531295e6
AS
829 /*
830 * We now ask send_buf() to not spin for generic ports -- we
831 * can re-use the same code path that non-blocking file
832 * descriptors take for blocking file descriptors since the
833 * wait is already done and we're certain the write will go
834 * through to the host.
835 */
836 nonblock = true;
276a3e95
SB
837 sg_init_one(sg, buf->buf, count);
838 ret = __send_to_port(port, sg, 1, count, buf, nonblock);
cdfadfc1
AS
839
840 if (nonblock && ret > 0)
841 goto out;
842
2030fa49 843free_buf:
1b637046 844 free_buf(buf, true);
cdfadfc1 845out:
2030fa49
AS
846 return ret;
847}
848
eb5e89fc
MH
849struct sg_list {
850 unsigned int n;
8ca84a50 851 unsigned int size;
eb5e89fc
MH
852 size_t len;
853 struct scatterlist *sg;
854};
855
856static int pipe_to_sg(struct pipe_inode_info *pipe, struct pipe_buffer *buf,
857 struct splice_desc *sd)
858{
859 struct sg_list *sgl = sd->u.data;
ec8fc870 860 unsigned int offset, len;
eb5e89fc 861
8ca84a50 862 if (sgl->n == sgl->size)
eb5e89fc
MH
863 return 0;
864
865 /* Try lock this page */
c928f642 866 if (pipe_buf_try_steal(pipe, buf)) {
eb5e89fc
MH
867 /* Get reference and unlock page for moving */
868 get_page(buf->page);
869 unlock_page(buf->page);
870
871 len = min(buf->len, sd->len);
872 sg_set_page(&(sgl->sg[sgl->n]), buf->page, len, buf->offset);
ec8fc870
MH
873 } else {
874 /* Failback to copying a page */
875 struct page *page = alloc_page(GFP_KERNEL);
c9efe511 876 char *src;
ec8fc870
MH
877
878 if (!page)
879 return -ENOMEM;
ec8fc870
MH
880
881 offset = sd->pos & ~PAGE_MASK;
882
883 len = sd->len;
884 if (len + offset > PAGE_SIZE)
885 len = PAGE_SIZE - offset;
886
4cabaa05 887 src = kmap_local_page(buf->page);
c9efe511 888 memcpy(page_address(page) + offset, src + buf->offset, len);
4cabaa05 889 kunmap_local(src);
ec8fc870
MH
890
891 sg_set_page(&(sgl->sg[sgl->n]), page, len, offset);
eb5e89fc 892 }
ec8fc870
MH
893 sgl->n++;
894 sgl->len += len;
eb5e89fc
MH
895
896 return len;
897}
898
899/* Faster zero-copy write by splicing */
900static ssize_t port_fops_splice_write(struct pipe_inode_info *pipe,
901 struct file *filp, loff_t *ppos,
902 size_t len, unsigned int flags)
903{
904 struct port *port = filp->private_data;
905 struct sg_list sgl;
906 ssize_t ret;
276a3e95 907 struct port_buffer *buf;
eb5e89fc
MH
908 struct splice_desc sd = {
909 .total_len = len,
910 .flags = flags,
911 .pos = *ppos,
912 .u.data = &sgl,
913 };
8cefc107 914 unsigned int occupancy;
eb5e89fc 915
1b637046
SB
916 /*
917 * Rproc_serial does not yet support splice. To support splice
918 * pipe_to_sg() must allocate dma-buffers and copy content from
919 * regular pages to dma pages. And alloc_buf and free_buf must
920 * support allocating and freeing such a list of dma-buffers.
921 */
922 if (is_rproc_serial(port->out_vq->vdev))
923 return -EINVAL;
924
2b4fbf02 925 pipe_lock(pipe);
8cefc107 926 ret = 0;
00a7d398 927 if (pipe_is_empty(pipe))
2b4fbf02 928 goto error_out;
68c034fe 929
efe75d24
MH
930 ret = wait_port_writable(port, filp->f_flags & O_NONBLOCK);
931 if (ret < 0)
2b4fbf02 932 goto error_out;
efe75d24 933
00a7d398 934 occupancy = pipe_buf_usage(pipe);
8cefc107 935 buf = alloc_buf(port->portdev->vdev, 0, occupancy);
6a965666 936
2b4fbf02
YY
937 if (!buf) {
938 ret = -ENOMEM;
939 goto error_out;
940 }
276a3e95 941
eb5e89fc
MH
942 sgl.n = 0;
943 sgl.len = 0;
8cefc107 944 sgl.size = occupancy;
276a3e95 945 sgl.sg = buf->sg;
8ca84a50 946 sg_init_table(sgl.sg, sgl.size);
eb5e89fc 947 ret = __splice_from_pipe(pipe, &sd, pipe_to_sg);
2b4fbf02 948 pipe_unlock(pipe);
eb5e89fc 949 if (likely(ret > 0))
276a3e95 950 ret = __send_to_port(port, buf->sg, sgl.n, sgl.len, buf, true);
eb5e89fc 951
fe529537 952 if (unlikely(ret <= 0))
1b637046 953 free_buf(buf, true);
eb5e89fc 954 return ret;
2b4fbf02
YY
955
956error_out:
957 pipe_unlock(pipe);
958 return ret;
eb5e89fc
MH
959}
960
afc9a42b 961static __poll_t port_fops_poll(struct file *filp, poll_table *wait)
2030fa49
AS
962{
963 struct port *port;
afc9a42b 964 __poll_t ret;
2030fa49
AS
965
966 port = filp->private_data;
967 poll_wait(filp, &port->waitqueue, wait);
968
8529a504
AS
969 if (!port->guest_connected) {
970 /* Port got unplugged */
a9a08845 971 return EPOLLHUP;
8529a504 972 }
2030fa49 973 ret = 0;
6df7aadc 974 if (!will_read_block(port))
a9a08845 975 ret |= EPOLLIN | EPOLLRDNORM;
cdfadfc1 976 if (!will_write_block(port))
a9a08845 977 ret |= EPOLLOUT;
2030fa49 978 if (!port->host_connected)
a9a08845 979 ret |= EPOLLHUP;
2030fa49
AS
980
981 return ret;
982}
983
b353a6b8
AS
984static void remove_port(struct kref *kref);
985
2030fa49
AS
986static int port_fops_release(struct inode *inode, struct file *filp)
987{
988 struct port *port;
989
990 port = filp->private_data;
991
992 /* Notify host of port being closed */
993 send_control_msg(port, VIRTIO_CONSOLE_PORT_OPEN, 0);
994
88f251ac 995 spin_lock_irq(&port->inbuf_lock);
3c7969cc
AS
996 port->guest_connected = false;
997
88f251ac
AS
998 discard_port_data(port);
999
1000 spin_unlock_irq(&port->inbuf_lock);
1001
cdfadfc1
AS
1002 spin_lock_irq(&port->outvq_lock);
1003 reclaim_consumed_buffers(port);
1004 spin_unlock_irq(&port->outvq_lock);
1005
1b637046 1006 reclaim_dma_bufs();
b353a6b8
AS
1007 /*
1008 * Locks aren't necessary here as a port can't be opened after
1009 * unplug, and if a port isn't unplugged, a kref would already
1010 * exist for the port. Plus, taking ports_lock here would
1011 * create a dependency on other locks taken by functions
1012 * inside remove_port if we're the last holder of the port,
1013 * creating many problems.
1014 */
1015 kref_put(&port->kref, remove_port);
1016
2030fa49
AS
1017 return 0;
1018}
1019
1020static int port_fops_open(struct inode *inode, struct file *filp)
1021{
1022 struct cdev *cdev = inode->i_cdev;
1023 struct port *port;
8ad37e83 1024 int ret;
2030fa49 1025
057b82be 1026 /* We get the port with a kref here */
04950cdf 1027 port = find_port_by_devt(cdev->dev);
671bdea2
AS
1028 if (!port) {
1029 /* Port was unplugged before we could proceed */
1030 return -ENXIO;
1031 }
2030fa49
AS
1032 filp->private_data = port;
1033
1034 /*
1035 * Don't allow opening of console port devices -- that's done
1036 * via /dev/hvc
1037 */
8ad37e83
AS
1038 if (is_console_port(port)) {
1039 ret = -ENXIO;
1040 goto out;
1041 }
2030fa49 1042
3c7969cc
AS
1043 /* Allow only one process to open a particular port at a time */
1044 spin_lock_irq(&port->inbuf_lock);
1045 if (port->guest_connected) {
1046 spin_unlock_irq(&port->inbuf_lock);
74ff582c 1047 ret = -EBUSY;
8ad37e83 1048 goto out;
3c7969cc
AS
1049 }
1050
1051 port->guest_connected = true;
1052 spin_unlock_irq(&port->inbuf_lock);
1053
cdfadfc1
AS
1054 spin_lock_irq(&port->outvq_lock);
1055 /*
1056 * There might be a chance that we missed reclaiming a few
1057 * buffers in the window of the port getting previously closed
1058 * and opening now.
1059 */
1060 reclaim_consumed_buffers(port);
1061 spin_unlock_irq(&port->outvq_lock);
1062
299fb61c
AS
1063 nonseekable_open(inode, filp);
1064
2030fa49
AS
1065 /* Notify host of port being opened */
1066 send_control_msg(filp->private_data, VIRTIO_CONSOLE_PORT_OPEN, 1);
1067
1068 return 0;
8ad37e83 1069out:
b353a6b8 1070 kref_put(&port->kref, remove_port);
8ad37e83 1071 return ret;
2030fa49
AS
1072}
1073
3eae0ade
AS
1074static int port_fops_fasync(int fd, struct file *filp, int mode)
1075{
1076 struct port *port;
1077
1078 port = filp->private_data;
1079 return fasync_helper(fd, filp, mode, &port->async_queue);
1080}
1081
2030fa49
AS
1082/*
1083 * The file operations that we support: programs in the guest can open
1084 * a console device, read from it, write to it, poll for data and
1085 * close it. The devices are at
1086 * /dev/vport<device number>p<port number>
1087 */
1088static const struct file_operations port_fops = {
1089 .owner = THIS_MODULE,
1090 .open = port_fops_open,
1091 .read = port_fops_read,
1092 .write = port_fops_write,
eb5e89fc 1093 .splice_write = port_fops_splice_write,
2030fa49
AS
1094 .poll = port_fops_poll,
1095 .release = port_fops_release,
3eae0ade 1096 .fasync = port_fops_fasync,
2030fa49
AS
1097};
1098
a23ea924
RR
1099/*
1100 * The put_chars() callback is pretty straightforward.
31610434 1101 *
a23ea924
RR
1102 * We turn the characters into a scatter-gather list, add it to the
1103 * output queue and then kick the Host. Then we sit here waiting for
1104 * it to finish: inefficient in theory, but in practice
ecda85e7 1105 * implementations will do it immediately.
a23ea924 1106 */
f32fcbed 1107static ssize_t put_chars(u32 vtermno, const u8 *buf, size_t count)
31610434 1108{
21206ede 1109 struct port *port;
276a3e95 1110 struct scatterlist sg[1];
c4baad50
OS
1111 void *data;
1112 int ret;
38edf58d
AS
1113
1114 port = find_port_by_vtermno(vtermno);
1115 if (!port)
6dc69f97 1116 return -EPIPE;
31610434 1117
c4baad50
OS
1118 data = kmemdup(buf, count, GFP_ATOMIC);
1119 if (!data)
1120 return -ENOMEM;
1121
1122 sg_init_one(sg, data, count);
1123 ret = __send_to_port(port, sg, 1, count, data, false);
1124 kfree(data);
1125 return ret;
31610434
RR
1126}
1127
a23ea924
RR
1128/*
1129 * get_chars() is the callback from the hvc_console infrastructure
1130 * when an interrupt is received.
31610434 1131 *
203baab8
AS
1132 * We call out to fill_readbuf that gets us the required data from the
1133 * buffers that are queued up.
a23ea924 1134 */
f32fcbed 1135static ssize_t get_chars(u32 vtermno, u8 *buf, size_t count)
31610434 1136{
21206ede
RR
1137 struct port *port;
1138
38edf58d
AS
1139 port = find_port_by_vtermno(vtermno);
1140 if (!port)
6dc69f97 1141 return -EPIPE;
21206ede 1142
31610434 1143 /* If we don't have an input queue yet, we can't get input. */
21206ede 1144 BUG_ON(!port->in_vq);
31610434 1145
f32fcbed 1146 return fill_readbuf(port, (__force u8 __user *)buf, count, false);
31610434 1147}
31610434 1148
cb06e367 1149static void resize_console(struct port *port)
c2983458 1150{
cb06e367 1151 struct virtio_device *vdev;
c2983458 1152
2de16a49 1153 /* The port could have been hot-unplugged */
9778829c 1154 if (!port || !is_console_port(port))
2de16a49
AS
1155 return;
1156
cb06e367 1157 vdev = port->portdev->vdev;
1b637046
SB
1158
1159 /* Don't test F_SIZE at all if we're rproc: not a valid feature! */
1160 if (!is_rproc_serial(vdev) &&
1161 virtio_has_feature(vdev, VIRTIO_CONSOLE_F_SIZE))
9778829c 1162 hvc_resize(port->cons.hvc, port->cons.ws);
c2983458
CB
1163}
1164
38edf58d 1165/* We set the configuration at this point, since we now have a tty */
91fcad19
CB
1166static int notifier_add_vio(struct hvc_struct *hp, int data)
1167{
38edf58d
AS
1168 struct port *port;
1169
1170 port = find_port_by_vtermno(hp->vtermno);
1171 if (!port)
1172 return -EINVAL;
1173
91fcad19 1174 hp->irq_requested = 1;
cb06e367 1175 resize_console(port);
c2983458 1176
91fcad19
CB
1177 return 0;
1178}
1179
1180static void notifier_del_vio(struct hvc_struct *hp, int data)
1181{
1182 hp->irq_requested = 0;
1183}
1184
17634ba2 1185/* The operations for console ports. */
1dff3996 1186static const struct hv_ops hv_ops = {
971f3390
RR
1187 .get_chars = get_chars,
1188 .put_chars = put_chars,
1189 .notifier_add = notifier_add_vio,
1190 .notifier_del = notifier_del_vio,
1191 .notifier_hangup = notifier_del_vio,
1192};
1193
3826835a 1194static int init_port_console(struct port *port)
cfa6d379
AS
1195{
1196 int ret;
1197
1198 /*
1199 * The Host's telling us this port is a console port. Hook it
1200 * up with an hvc console.
1201 *
1202 * To set up and manage our virtual console, we call
1203 * hvc_alloc().
1204 *
1205 * The first argument of hvc_alloc() is the virtual console
1206 * number. The second argument is the parameter for the
1207 * notification mechanism (like irq number). We currently
1208 * leave this as zero, virtqueues have implicit notifications.
1209 *
1210 * The third argument is a "struct hv_ops" containing the
1211 * put_chars() get_chars(), notifier_add() and notifier_del()
1212 * pointers. The final argument is the output buffer size: we
1213 * can do any size, so we put PAGE_SIZE here.
1214 */
e6278a54
CLG
1215 ret = ida_alloc_min(&vtermno_ida, 1, GFP_KERNEL);
1216 if (ret < 0)
1217 return ret;
cfa6d379 1218
e6278a54 1219 port->cons.vtermno = ret;
cfa6d379
AS
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);
298add72
AS
1223 dev_err(port->dev,
1224 "error %d allocating hvc for port\n", ret);
cfa6d379 1225 port->cons.hvc = NULL;
e6278a54 1226 ida_free(&vtermno_ida, port->cons.vtermno);
cfa6d379
AS
1227 return ret;
1228 }
1229 spin_lock_irq(&pdrvdata_lock);
cfa6d379
AS
1230 list_add_tail(&port->cons.list, &pdrvdata.consoles);
1231 spin_unlock_irq(&pdrvdata_lock);
3c7969cc 1232 port->guest_connected = true;
cfa6d379 1233
2030fa49
AS
1234 /* Notify host of port being opened */
1235 send_control_msg(port, VIRTIO_CONSOLE_PORT_OPEN, 1);
1236
cfa6d379
AS
1237 return 0;
1238}
1239
431edb8a
AS
1240static ssize_t show_port_name(struct device *dev,
1241 struct device_attribute *attr, char *buffer)
1242{
1243 struct port *port;
1244
1245 port = dev_get_drvdata(dev);
1246
1247 return sprintf(buffer, "%s\n", port->name);
1248}
1249
1250static DEVICE_ATTR(name, S_IRUGO, show_port_name, NULL);
1251
1252static struct attribute *port_sysfs_entries[] = {
1253 &dev_attr_name.attr,
1254 NULL
1255};
1256
ac317e27 1257static const struct attribute_group port_attribute_group = {
431edb8a
AS
1258 .name = NULL, /* put in device directory */
1259 .attrs = port_sysfs_entries,
1260};
1261
ddfa728a 1262static int port_debugfs_show(struct seq_file *s, void *data)
d99393ef 1263{
8d62fe94
TT
1264 struct port *port = s->private;
1265
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);
790b2f24 1273 seq_printf(s, "is_console: %s\n", str_yes_no(is_console_port(port)));
8d62fe94 1274 seq_printf(s, "console_vtermno: %u\n", port->cons.vtermno);
d99393ef 1275
8d62fe94
TT
1276 return 0;
1277}
d99393ef 1278
ddfa728a 1279DEFINE_SHOW_ATTRIBUTE(port_debugfs);
d99393ef 1280
9778829c
AS
1281static void set_console_size(struct port *port, u16 rows, u16 cols)
1282{
1283 if (!port || !is_console_port(port))
1284 return;
1285
1286 port->cons.ws.ws_row = rows;
1287 port->cons.ws.ws_col = cols;
1288}
1289
d791cfcb 1290static int fill_queue(struct virtqueue *vq, spinlock_t *lock)
c446f8fc
AS
1291{
1292 struct port_buffer *buf;
d791cfcb 1293 int nr_added_bufs;
c446f8fc
AS
1294 int ret;
1295
1296 nr_added_bufs = 0;
1297 do {
2855b335 1298 buf = alloc_buf(vq->vdev, PAGE_SIZE, 0);
c446f8fc 1299 if (!buf)
d791cfcb 1300 return -ENOMEM;
c446f8fc
AS
1301
1302 spin_lock_irq(lock);
1303 ret = add_inbuf(vq, buf);
1304 if (ret < 0) {
1305 spin_unlock_irq(lock);
1b637046 1306 free_buf(buf, true);
d791cfcb 1307 return ret;
c446f8fc
AS
1308 }
1309 nr_added_bufs++;
1310 spin_unlock_irq(lock);
1311 } while (ret > 0);
1312
1313 return nr_added_bufs;
1314}
1315
3eae0ade
AS
1316static void send_sigio_to_port(struct port *port)
1317{
1318 if (port->async_queue && port->guest_connected)
1319 kill_fasync(&port->async_queue, SIGIO, POLL_OUT);
1320}
1321
c446f8fc
AS
1322static int add_port(struct ports_device *portdev, u32 id)
1323{
c446f8fc 1324 struct port *port;
c446f8fc 1325 dev_t devt;
c446f8fc
AS
1326 int err;
1327
1328 port = kmalloc(sizeof(*port), GFP_KERNEL);
1329 if (!port) {
1330 err = -ENOMEM;
1331 goto fail;
1332 }
b353a6b8 1333 kref_init(&port->kref);
c446f8fc
AS
1334
1335 port->portdev = portdev;
1336 port->id = id;
1337
1338 port->name = NULL;
1339 port->inbuf = NULL;
1340 port->cons.hvc = NULL;
3eae0ade 1341 port->async_queue = NULL;
c446f8fc 1342
9778829c 1343 port->cons.ws.ws_row = port->cons.ws.ws_col = 0;
4b0a2c5f 1344 port->cons.vtermno = 0;
9778829c 1345
c446f8fc 1346 port->host_connected = port->guest_connected = false;
17e5b4f2 1347 port->stats = (struct port_stats) { 0 };
c446f8fc 1348
cdfadfc1
AS
1349 port->outvq_full = false;
1350
c446f8fc
AS
1351 port->in_vq = portdev->in_vqs[port->id];
1352 port->out_vq = portdev->out_vqs[port->id];
1353
d22a6989
AS
1354 port->cdev = cdev_alloc();
1355 if (!port->cdev) {
1356 dev_err(&port->portdev->vdev->dev, "Error allocating cdev\n");
1357 err = -ENOMEM;
1358 goto free_port;
1359 }
1360 port->cdev->ops = &port_fops;
c446f8fc
AS
1361
1362 devt = MKDEV(portdev->chr_major, id);
d22a6989 1363 err = cdev_add(port->cdev, devt, 1);
c446f8fc
AS
1364 if (err < 0) {
1365 dev_err(&port->portdev->vdev->dev,
1366 "Error %d adding cdev for port %u\n", err, id);
d22a6989 1367 goto free_cdev;
c446f8fc 1368 }
11680fdf 1369 port->dev = device_create(&port_class, &port->portdev->vdev->dev,
c446f8fc 1370 devt, port, "vport%up%u",
dc18f080 1371 port->portdev->vdev->index, id);
c446f8fc
AS
1372 if (IS_ERR(port->dev)) {
1373 err = PTR_ERR(port->dev);
1374 dev_err(&port->portdev->vdev->dev,
1375 "Error %d creating device for port %u\n",
1376 err, id);
1377 goto free_cdev;
1378 }
1379
1380 spin_lock_init(&port->inbuf_lock);
cdfadfc1 1381 spin_lock_init(&port->outvq_lock);
c446f8fc
AS
1382 init_waitqueue_head(&port->waitqueue);
1383
d791cfcb
LV
1384 /* We can safely ignore ENOSPC because it means
1385 * the queue already has buffers. Buffers are removed
1386 * only by virtcons_remove(), not by unplug_port()
1387 */
1388 err = fill_queue(port->in_vq, &port->inbuf_lock);
1389 if (err < 0 && err != -ENOSPC) {
c446f8fc 1390 dev_err(port->dev, "Error allocating inbufs\n");
c446f8fc
AS
1391 goto free_device;
1392 }
1393
1b637046
SB
1394 if (is_rproc_serial(port->portdev->vdev))
1395 /*
1396 * For rproc_serial assume remote processor is connected.
1397 * rproc_serial does not want the console port, only
1398 * the generic port implementation.
1399 */
aabd6a8f 1400 port->host_connected = true;
1b637046
SB
1401 else if (!use_multiport(port->portdev)) {
1402 /*
1403 * If we're not using multiport support,
1404 * this has to be a console port.
1405 */
c446f8fc
AS
1406 err = init_port_console(port);
1407 if (err)
1408 goto free_inbufs;
1409 }
1410
1411 spin_lock_irq(&portdev->ports_lock);
1412 list_add_tail(&port->list, &port->portdev->ports);
1413 spin_unlock_irq(&portdev->ports_lock);
1414
1415 /*
1416 * Tell the Host we're set so that it can send us various
1417 * configuration parameters for this port (eg, port name,
1418 * caching, whether this is a console port, etc.)
1419 */
1420 send_control_msg(port, VIRTIO_CONSOLE_PORT_READY, 1);
1421
fb11de92
GKH
1422 /*
1423 * Finally, create the debugfs file that we can use to
1424 * inspect a port's state at any time
1425 */
17f18e04 1426 port->debugfs_file = debugfs_create_file(dev_name(port->dev), 0444,
fb11de92
GKH
1427 pdrvdata.debugfs_dir,
1428 port, &port_debugfs_fops);
c446f8fc
AS
1429 return 0;
1430
1431free_inbufs:
c446f8fc 1432free_device:
11680fdf 1433 device_destroy(&port_class, port->dev->devt);
c446f8fc 1434free_cdev:
d22a6989 1435 cdev_del(port->cdev);
c446f8fc
AS
1436free_port:
1437 kfree(port);
1438fail:
1439 /* The host might want to notify management sw about port add failure */
0643e4c6 1440 __send_control_msg(portdev, id, VIRTIO_CONSOLE_PORT_READY, 0);
c446f8fc
AS
1441 return err;
1442}
1443
b353a6b8
AS
1444/* No users remain, remove all port-specific data. */
1445static void remove_port(struct kref *kref)
1446{
1447 struct port *port;
1448
1449 port = container_of(kref, struct port, kref);
1450
b353a6b8
AS
1451 kfree(port);
1452}
1453
a0e2dbfc
AS
1454static void remove_port_data(struct port *port)
1455{
c6017e79 1456 spin_lock_irq(&port->inbuf_lock);
a0e2dbfc
AS
1457 /* Remove unused data this port might have received. */
1458 discard_port_data(port);
34563769 1459 spin_unlock_irq(&port->inbuf_lock);
a0e2dbfc 1460
c6017e79
AS
1461 spin_lock_irq(&port->outvq_lock);
1462 reclaim_consumed_buffers(port);
34563769 1463 spin_unlock_irq(&port->outvq_lock);
a0e2dbfc
AS
1464}
1465
b353a6b8
AS
1466/*
1467 * Port got unplugged. Remove port from portdev's list and drop the
1468 * kref reference. If no userspace has this port opened, it will
1469 * result in immediate removal the port.
1470 */
1471static void unplug_port(struct port *port)
1f7aa42d 1472{
b353a6b8
AS
1473 spin_lock_irq(&port->portdev->ports_lock);
1474 list_del(&port->list);
1475 spin_unlock_irq(&port->portdev->ports_lock);
1476
5549fb25 1477 spin_lock_irq(&port->inbuf_lock);
0047634d 1478 if (port->guest_connected) {
92d34538
AS
1479 /* Let the app know the port is going down. */
1480 send_sigio_to_port(port);
1481
1482 /* Do this after sigio is actually sent */
0047634d
AS
1483 port->guest_connected = false;
1484 port->host_connected = false;
a461e11e 1485
92d34538 1486 wake_up_interruptible(&port->waitqueue);
0047634d 1487 }
5549fb25 1488 spin_unlock_irq(&port->inbuf_lock);
0047634d 1489
1f7aa42d
AS
1490 if (is_console_port(port)) {
1491 spin_lock_irq(&pdrvdata_lock);
1492 list_del(&port->cons.list);
1493 spin_unlock_irq(&pdrvdata_lock);
1494 hvc_remove(port->cons.hvc);
e6278a54 1495 ida_free(&vtermno_ida, port->cons.vtermno);
1f7aa42d 1496 }
1f7aa42d 1497
a0e2dbfc 1498 remove_port_data(port);
a9cdd485 1499
b353a6b8
AS
1500 /*
1501 * We should just assume the device itself has gone off --
1502 * else a close on an open port later will try to send out a
1503 * control message.
1504 */
1505 port->portdev = NULL;
d99393ef 1506
ea3768b4 1507 sysfs_remove_group(&port->dev->kobj, &port_attribute_group);
11680fdf 1508 device_destroy(&port_class, port->dev->devt);
ea3768b4
AS
1509 cdev_del(port->cdev);
1510
ea3768b4 1511 debugfs_remove(port->debugfs_file);
3b868a40 1512 kfree(port->name);
ea3768b4 1513
b353a6b8
AS
1514 /*
1515 * Locks around here are not necessary - a port can't be
1516 * opened after we removed the port struct from ports_list
1517 * above.
1518 */
1519 kref_put(&port->kref, remove_port);
1f7aa42d
AS
1520}
1521
17634ba2 1522/* Any private messages that the Host and Guest want to share */
1f0f9106
MT
1523static void handle_control_message(struct virtio_device *vdev,
1524 struct ports_device *portdev,
17634ba2
AS
1525 struct port_buffer *buf)
1526{
1527 struct virtio_console_control *cpkt;
1528 struct port *port;
431edb8a
AS
1529 size_t name_size;
1530 int err;
17634ba2
AS
1531
1532 cpkt = (struct virtio_console_control *)(buf->buf + buf->offset);
1533
1f0f9106
MT
1534 port = find_port_by_id(portdev, virtio32_to_cpu(vdev, cpkt->id));
1535 if (!port &&
1536 cpkt->event != cpu_to_virtio16(vdev, VIRTIO_CONSOLE_PORT_ADD)) {
17634ba2
AS
1537 /* No valid header at start of buffer. Drop it. */
1538 dev_dbg(&portdev->vdev->dev,
1539 "Invalid index %u in control packet\n", cpkt->id);
1540 return;
1541 }
1542
1f0f9106 1543 switch (virtio16_to_cpu(vdev, cpkt->event)) {
f909f850
AS
1544 case VIRTIO_CONSOLE_PORT_ADD:
1545 if (port) {
1d05160b
AS
1546 dev_dbg(&portdev->vdev->dev,
1547 "Port %u already added\n", port->id);
f909f850
AS
1548 send_control_msg(port, VIRTIO_CONSOLE_PORT_READY, 1);
1549 break;
1550 }
1f0f9106 1551 if (virtio32_to_cpu(vdev, cpkt->id) >=
7328fa64 1552 portdev->max_nr_ports) {
f909f850 1553 dev_warn(&portdev->vdev->dev,
1f0f9106
MT
1554 "Request for adding port with "
1555 "out-of-bound id %u, max. supported id: %u\n",
7328fa64 1556 cpkt->id, portdev->max_nr_ports - 1);
f909f850
AS
1557 break;
1558 }
1f0f9106 1559 add_port(portdev, virtio32_to_cpu(vdev, cpkt->id));
f909f850
AS
1560 break;
1561 case VIRTIO_CONSOLE_PORT_REMOVE:
b353a6b8 1562 unplug_port(port);
f909f850 1563 break;
17634ba2
AS
1564 case VIRTIO_CONSOLE_CONSOLE_PORT:
1565 if (!cpkt->value)
1566 break;
1567 if (is_console_port(port))
1568 break;
1569
1570 init_port_console(port);
5e38483b 1571 complete(&early_console_added);
17634ba2
AS
1572 /*
1573 * Could remove the port here in case init fails - but
1574 * have to notify the host first.
1575 */
1576 break;
8345adbf
AS
1577 case VIRTIO_CONSOLE_RESIZE: {
1578 struct {
fbd3039a 1579 __virtio16 cols;
5326ab73 1580 __virtio16 rows;
8345adbf
AS
1581 } size;
1582
17634ba2
AS
1583 if (!is_console_port(port))
1584 break;
8345adbf
AS
1585
1586 memcpy(&size, buf->buf + buf->offset + sizeof(*cpkt),
1587 sizeof(size));
fbd3039a
HP
1588 set_console_size(port, virtio16_to_cpu(vdev, size.rows),
1589 virtio16_to_cpu(vdev, size.cols));
8345adbf 1590
17634ba2
AS
1591 port->cons.hvc->irq_requested = 1;
1592 resize_console(port);
1593 break;
8345adbf 1594 }
2030fa49 1595 case VIRTIO_CONSOLE_PORT_OPEN:
1f0f9106 1596 port->host_connected = virtio16_to_cpu(vdev, cpkt->value);
2030fa49 1597 wake_up_interruptible(&port->waitqueue);
cdfadfc1
AS
1598 /*
1599 * If the host port got closed and the host had any
1600 * unconsumed buffers, we'll be able to reclaim them
1601 * now.
1602 */
1603 spin_lock_irq(&port->outvq_lock);
1604 reclaim_consumed_buffers(port);
1605 spin_unlock_irq(&port->outvq_lock);
3eae0ade
AS
1606
1607 /*
1608 * If the guest is connected, it'll be interested in
1609 * knowing the host connection state changed.
1610 */
314081f1 1611 spin_lock_irq(&port->inbuf_lock);
3eae0ade 1612 send_sigio_to_port(port);
314081f1 1613 spin_unlock_irq(&port->inbuf_lock);
2030fa49 1614 break;
431edb8a 1615 case VIRTIO_CONSOLE_PORT_NAME:
291024ef
AS
1616 /*
1617 * If we woke up after hibernation, we can get this
1618 * again. Skip it in that case.
1619 */
1620 if (port->name)
1621 break;
1622
431edb8a
AS
1623 /*
1624 * Skip the size of the header and the cpkt to get the size
1625 * of the name that was sent
1626 */
1627 name_size = buf->len - buf->offset - sizeof(*cpkt) + 1;
1628
1629 port->name = kmalloc(name_size, GFP_KERNEL);
1630 if (!port->name) {
1631 dev_err(port->dev,
1632 "Not enough space to store port name\n");
1633 break;
1634 }
c7109c72
BL
1635 strscpy(port->name, buf->buf + buf->offset + sizeof(*cpkt),
1636 name_size);
431edb8a
AS
1637
1638 /*
1639 * Since we only have one sysfs attribute, 'name',
1640 * create it only if we have a name for the port.
1641 */
1642 err = sysfs_create_group(&port->dev->kobj,
1643 &port_attribute_group);
ec64213c 1644 if (err) {
431edb8a
AS
1645 dev_err(port->dev,
1646 "Error %d creating sysfs device attributes\n",
1647 err);
ec64213c
AS
1648 } else {
1649 /*
1650 * Generate a udev event so that appropriate
1651 * symlinks can be created based on udev
1652 * rules.
1653 */
1654 kobject_uevent(&port->dev->kobj, KOBJ_CHANGE);
1655 }
431edb8a 1656 break;
17634ba2
AS
1657 }
1658}
1659
1660static void control_work_handler(struct work_struct *work)
1661{
1662 struct ports_device *portdev;
1663 struct virtqueue *vq;
1664 struct port_buffer *buf;
1665 unsigned int len;
1666
1667 portdev = container_of(work, struct ports_device, control_work);
1668 vq = portdev->c_ivq;
1669
165b1b8b 1670 spin_lock(&portdev->c_ivq_lock);
505b0451 1671 while ((buf = virtqueue_get_buf(vq, &len))) {
165b1b8b 1672 spin_unlock(&portdev->c_ivq_lock);
17634ba2 1673
d00d8da5 1674 buf->len = min_t(size_t, len, buf->size);
17634ba2
AS
1675 buf->offset = 0;
1676
1f0f9106 1677 handle_control_message(vq->vdev, portdev, buf);
17634ba2 1678
165b1b8b 1679 spin_lock(&portdev->c_ivq_lock);
17634ba2
AS
1680 if (add_inbuf(portdev->c_ivq, buf) < 0) {
1681 dev_warn(&portdev->vdev->dev,
1682 "Error adding buffer to queue\n");
1b637046 1683 free_buf(buf, false);
17634ba2
AS
1684 }
1685 }
165b1b8b 1686 spin_unlock(&portdev->c_ivq_lock);
17634ba2
AS
1687}
1688
a7a69ec0
MT
1689static void flush_bufs(struct virtqueue *vq, bool can_sleep)
1690{
1691 struct port_buffer *buf;
1692 unsigned int len;
1693
1694 while ((buf = virtqueue_get_buf(vq, &len)))
1695 free_buf(buf, can_sleep);
1696}
1697
2770c5ea
AS
1698static void out_intr(struct virtqueue *vq)
1699{
1700 struct port *port;
1701
1702 port = find_port_by_vq(vq->vdev->priv, vq);
a7a69ec0
MT
1703 if (!port) {
1704 flush_bufs(vq, false);
2770c5ea 1705 return;
a7a69ec0 1706 }
2770c5ea
AS
1707
1708 wake_up_interruptible(&port->waitqueue);
1709}
1710
17634ba2
AS
1711static void in_intr(struct virtqueue *vq)
1712{
1713 struct port *port;
1714 unsigned long flags;
1715
1716 port = find_port_by_vq(vq->vdev->priv, vq);
a7a69ec0
MT
1717 if (!port) {
1718 flush_bufs(vq, false);
17634ba2 1719 return;
a7a69ec0 1720 }
17634ba2
AS
1721
1722 spin_lock_irqsave(&port->inbuf_lock, flags);
d25a9dda 1723 port->inbuf = get_inbuf(port);
17634ba2 1724
88f251ac 1725 /*
aabd6a8f
SB
1726 * Normally the port should not accept data when the port is
1727 * closed. For generic serial ports, the host won't (shouldn't)
1728 * send data till the guest is connected. But this condition
88f251ac 1729 * can be reached when a console port is not yet connected (no
aabd6a8f
SB
1730 * tty is spawned) and the other side sends out data over the
1731 * vring, or when a remote devices start sending data before
1732 * the ports are opened.
1733 *
1734 * A generic serial port will discard data if not connected,
1735 * while console ports and rproc-serial ports accepts data at
1736 * any time. rproc-serial is initiated with guest_connected to
1737 * false because port_fops_open expects this. Console ports are
1738 * hooked up with an HVC console and is initialized with
1739 * guest_connected to true.
88f251ac 1740 */
aabd6a8f
SB
1741
1742 if (!port->guest_connected && !is_rproc_serial(port->portdev->vdev))
88f251ac
AS
1743 discard_port_data(port);
1744
314081f1
AS
1745 /* Send a SIGIO indicating new data in case the process asked for it */
1746 send_sigio_to_port(port);
1747
17634ba2
AS
1748 spin_unlock_irqrestore(&port->inbuf_lock, flags);
1749
2030fa49
AS
1750 wake_up_interruptible(&port->waitqueue);
1751
17634ba2
AS
1752 if (is_console_port(port) && hvc_poll(port->cons.hvc))
1753 hvc_kick();
1754}
1755
1756static void control_intr(struct virtqueue *vq)
1757{
1758 struct ports_device *portdev;
1759
1760 portdev = vq->vdev->priv;
1761 schedule_work(&portdev->control_work);
1762}
1763
7f5d810d
AS
1764static void config_intr(struct virtio_device *vdev)
1765{
1766 struct ports_device *portdev;
1767
1768 portdev = vdev->priv;
99f905f8 1769
eeb8a7e8
MT
1770 if (!use_multiport(portdev))
1771 schedule_work(&portdev->config_work);
1772}
1773
1774static void config_work_handler(struct work_struct *work)
1775{
1776 struct ports_device *portdev;
1777
8379cadf 1778 portdev = container_of(work, struct ports_device, config_work);
4038f5b7 1779 if (!use_multiport(portdev)) {
eeb8a7e8 1780 struct virtio_device *vdev;
9778829c
AS
1781 struct port *port;
1782 u16 rows, cols;
1783
eeb8a7e8 1784 vdev = portdev->vdev;
855e0c52
RR
1785 virtio_cread(vdev, struct virtio_console_config, cols, &cols);
1786 virtio_cread(vdev, struct virtio_console_config, rows, &rows);
9778829c
AS
1787
1788 port = find_port_by_id(portdev, 0);
1789 set_console_size(port, rows, cols);
1790
4038f5b7
AS
1791 /*
1792 * We'll use this way of resizing only for legacy
1793 * support. For newer userspace
1794 * (VIRTIO_CONSOLE_F_MULTPORT+), use control messages
1795 * to indicate console size changes so that it can be
1796 * done per-port.
1797 */
9778829c 1798 resize_console(port);
4038f5b7 1799 }
7f5d810d
AS
1800}
1801
2658a79a
AS
1802static int init_vqs(struct ports_device *portdev)
1803{
cd54c623 1804 struct virtqueue_info *vqs_info;
2658a79a 1805 struct virtqueue **vqs;
17634ba2 1806 u32 i, j, nr_ports, nr_queues;
2658a79a
AS
1807 int err;
1808
7328fa64 1809 nr_ports = portdev->max_nr_ports;
17634ba2 1810 nr_queues = use_multiport(portdev) ? (nr_ports + 1) * 2 : 2;
2658a79a 1811
6da2ec56 1812 vqs = kmalloc_array(nr_queues, sizeof(struct virtqueue *), GFP_KERNEL);
cd54c623 1813 vqs_info = kcalloc(nr_queues, sizeof(*vqs_info), GFP_KERNEL);
6da2ec56
KC
1814 portdev->in_vqs = kmalloc_array(nr_ports, sizeof(struct virtqueue *),
1815 GFP_KERNEL);
1816 portdev->out_vqs = kmalloc_array(nr_ports, sizeof(struct virtqueue *),
1817 GFP_KERNEL);
cd54c623 1818 if (!vqs || !vqs_info || !portdev->in_vqs || !portdev->out_vqs) {
2658a79a 1819 err = -ENOMEM;
22e132ff 1820 goto free;
2658a79a
AS
1821 }
1822
17634ba2
AS
1823 /*
1824 * For backward compat (newer host but older guest), the host
1825 * spawns a console port first and also inits the vqs for port
1826 * 0 before others.
1827 */
1828 j = 0;
cd54c623
JP
1829 vqs_info[j].callback = in_intr;
1830 vqs_info[j + 1].callback = out_intr;
1831 vqs_info[j].name = "input";
1832 vqs_info[j + 1].name = "output";
17634ba2
AS
1833 j += 2;
1834
1835 if (use_multiport(portdev)) {
cd54c623
JP
1836 vqs_info[j].callback = control_intr;
1837 vqs_info[j].name = "control-i";
1838 vqs_info[j + 1].name = "control-o";
17634ba2
AS
1839
1840 for (i = 1; i < nr_ports; i++) {
1841 j += 2;
cd54c623
JP
1842 vqs_info[j].callback = in_intr;
1843 vqs_info[j + 1].callback = out_intr;
1844 vqs_info[j].name = "input";
1845 vqs_info[j + 1].name = "output";
17634ba2
AS
1846 }
1847 }
2658a79a 1848 /* Find the queues. */
6c85d6b6 1849 err = virtio_find_vqs(portdev->vdev, nr_queues, vqs, vqs_info, NULL);
2658a79a 1850 if (err)
22e132ff 1851 goto free;
2658a79a 1852
17634ba2 1853 j = 0;
2658a79a
AS
1854 portdev->in_vqs[0] = vqs[0];
1855 portdev->out_vqs[0] = vqs[1];
17634ba2
AS
1856 j += 2;
1857 if (use_multiport(portdev)) {
1858 portdev->c_ivq = vqs[j];
1859 portdev->c_ovq = vqs[j + 1];
1860
1861 for (i = 1; i < nr_ports; i++) {
1862 j += 2;
1863 portdev->in_vqs[i] = vqs[j];
1864 portdev->out_vqs[i] = vqs[j + 1];
1865 }
1866 }
cd54c623 1867 kfree(vqs_info);
2658a79a
AS
1868 kfree(vqs);
1869
1870 return 0;
1871
22e132ff 1872free:
2658a79a 1873 kfree(portdev->out_vqs);
2658a79a 1874 kfree(portdev->in_vqs);
cd54c623 1875 kfree(vqs_info);
2658a79a 1876 kfree(vqs);
22e132ff 1877
2658a79a
AS
1878 return err;
1879}
1880
fb08bd27
AS
1881static const struct file_operations portdev_fops = {
1882 .owner = THIS_MODULE,
1883};
1884
a0e2dbfc
AS
1885static void remove_vqs(struct ports_device *portdev)
1886{
a7a69ec0
MT
1887 struct virtqueue *vq;
1888
1889 virtio_device_for_each_vq(portdev->vdev, vq) {
1890 struct port_buffer *buf;
1891
1892 flush_bufs(vq, true);
1893 while ((buf = virtqueue_detach_unused_buf(vq)))
1894 free_buf(buf, true);
56b5e65e 1895 cond_resched();
a7a69ec0 1896 }
a0e2dbfc
AS
1897 portdev->vdev->config->del_vqs(portdev->vdev);
1898 kfree(portdev->in_vqs);
1899 kfree(portdev->out_vqs);
1900}
1901
aa44ec86
MT
1902static void virtcons_remove(struct virtio_device *vdev)
1903{
1904 struct ports_device *portdev;
1905 struct port *port, *port2;
1906
1907 portdev = vdev->priv;
1908
1909 spin_lock_irq(&pdrvdata_lock);
1910 list_del(&portdev->list);
1911 spin_unlock_irq(&pdrvdata_lock);
1912
0e7174b9
MT
1913 /* Device is going away, exit any polling for buffers */
1914 virtio_break_device(vdev);
1915 if (use_multiport(portdev))
1916 flush_work(&portdev->control_work);
1917 else
1918 flush_work(&portdev->config_work);
1919
aa44ec86 1920 /* Disable interrupts for vqs */
d9679d00 1921 virtio_reset_device(vdev);
aa44ec86
MT
1922 /* Finish up work that's lined up */
1923 if (use_multiport(portdev))
1924 cancel_work_sync(&portdev->control_work);
1925 else
1926 cancel_work_sync(&portdev->config_work);
1927
1928 list_for_each_entry_safe(port, port2, &portdev->ports, list)
1929 unplug_port(port);
1930
1931 unregister_chrdev(portdev->chr_major, "virtio-portsdev");
1932
1933 /*
1934 * When yanking out a device, we immediately lose the
1935 * (device-side) queues. So there's no point in keeping the
1936 * guest side around till we drop our final reference. This
1937 * also means that any ports which are in an open state will
1938 * have to just stop using the port, as the vqs are going
1939 * away.
1940 */
1941 remove_vqs(portdev);
1942 kfree(portdev);
1943}
1944
1c85bf35
AS
1945/*
1946 * Once we're further in boot, we get probed like any other virtio
1947 * device.
17634ba2
AS
1948 *
1949 * If the host also supports multiple console ports, we check the
1950 * config space to see how many ports the host has spawned. We
1951 * initialize each port found.
1c85bf35 1952 */
2223cbec 1953static int virtcons_probe(struct virtio_device *vdev)
1c85bf35 1954{
1c85bf35
AS
1955 struct ports_device *portdev;
1956 int err;
17634ba2 1957 bool multiport;
5e38483b 1958
be8ff595
RR
1959 /* We only need a config space if features are offered */
1960 if (!vdev->config->get &&
1961 (virtio_has_feature(vdev, VIRTIO_CONSOLE_F_SIZE)
1962 || virtio_has_feature(vdev, VIRTIO_CONSOLE_F_MULTIPORT))) {
011f0e7a
MT
1963 dev_err(&vdev->dev, "%s failure: config access disabled\n",
1964 __func__);
1965 return -EINVAL;
1966 }
1967
1c85bf35
AS
1968 portdev = kmalloc(sizeof(*portdev), GFP_KERNEL);
1969 if (!portdev) {
1970 err = -ENOMEM;
1971 goto fail;
1972 }
1973
1974 /* Attach this portdev to this virtio_device, and vice-versa. */
1975 portdev->vdev = vdev;
1976 vdev->priv = portdev;
1977
fb08bd27
AS
1978 portdev->chr_major = register_chrdev(0, "virtio-portsdev",
1979 &portdev_fops);
1980 if (portdev->chr_major < 0) {
1981 dev_err(&vdev->dev,
1982 "Error %d registering chrdev for device %u\n",
dc18f080 1983 portdev->chr_major, vdev->index);
fb08bd27
AS
1984 err = portdev->chr_major;
1985 goto free;
1986 }
1987
17634ba2 1988 multiport = false;
7328fa64 1989 portdev->max_nr_ports = 1;
1b637046
SB
1990
1991 /* Don't test MULTIPORT at all if we're rproc: not a valid feature! */
1992 if (!is_rproc_serial(vdev) &&
855e0c52
RR
1993 virtio_cread_feature(vdev, VIRTIO_CONSOLE_F_MULTIPORT,
1994 struct virtio_console_config, max_nr_ports,
7328fa64 1995 &portdev->max_nr_ports) == 0) {
28962ec5
JW
1996 if (portdev->max_nr_ports == 0 ||
1997 portdev->max_nr_ports > VIRTCONS_MAX_PORTS) {
1998 dev_err(&vdev->dev,
1999 "Invalidate max_nr_ports %d",
2000 portdev->max_nr_ports);
2001 err = -EINVAL;
2002 goto free;
2003 }
17634ba2 2004 multiport = true;
1b637046 2005 }
17634ba2 2006
17634ba2
AS
2007 spin_lock_init(&portdev->ports_lock);
2008 INIT_LIST_HEAD(&portdev->ports);
5c60300d 2009 INIT_LIST_HEAD(&portdev->list);
17634ba2 2010
eeb8a7e8 2011 INIT_WORK(&portdev->config_work, &config_work_handler);
4f6e24ed
MT
2012 INIT_WORK(&portdev->control_work, &control_work_handler);
2013
17634ba2 2014 if (multiport) {
165b1b8b 2015 spin_lock_init(&portdev->c_ivq_lock);
9ba5c80b 2016 spin_lock_init(&portdev->c_ovq_lock);
b9efbe2b 2017 }
17634ba2 2018
b9efbe2b
MT
2019 err = init_vqs(portdev);
2020 if (err < 0) {
2021 dev_err(&vdev->dev, "Error %d initializing vqs\n", err);
2022 goto free_chrdev;
2023 }
2024
2025 virtio_device_ready(portdev->vdev);
2026
2027 if (multiport) {
d791cfcb
LV
2028 err = fill_queue(portdev->c_ivq, &portdev->c_ivq_lock);
2029 if (err < 0) {
22a29eac
AS
2030 dev_err(&vdev->dev,
2031 "Error allocating buffers for control queue\n");
5c60300d
MT
2032 /*
2033 * The host might want to notify mgmt sw about device
2034 * add failure.
2035 */
2036 __send_control_msg(portdev, VIRTIO_CONSOLE_BAD_ID,
2037 VIRTIO_CONSOLE_DEVICE_READY, 0);
2038 /* Device was functional: we need full cleanup. */
2039 virtcons_remove(vdev);
d791cfcb 2040 return err;
22a29eac 2041 }
1d05160b
AS
2042 } else {
2043 /*
2044 * For backward compatibility: Create a console port
2045 * if we're running on older host.
2046 */
2047 add_port(portdev, 0);
17634ba2
AS
2048 }
2049
6bdf2afd
AS
2050 spin_lock_irq(&pdrvdata_lock);
2051 list_add_tail(&portdev->list, &pdrvdata.portdevs);
2052 spin_unlock_irq(&pdrvdata_lock);
2053
f909f850
AS
2054 __send_control_msg(portdev, VIRTIO_CONSOLE_BAD_ID,
2055 VIRTIO_CONSOLE_DEVICE_READY, 1);
5e38483b 2056
31610434
RR
2057 return 0;
2058
fb08bd27
AS
2059free_chrdev:
2060 unregister_chrdev(portdev->chr_major, "virtio-portsdev");
31610434 2061free:
1c85bf35 2062 kfree(portdev);
31610434
RR
2063fail:
2064 return err;
2065}
2066
8b66c917 2067static const struct virtio_device_id id_table[] = {
31610434
RR
2068 { VIRTIO_ID_CONSOLE, VIRTIO_DEV_ANY_ID },
2069 { 0 },
2070};
897c44f0 2071MODULE_DEVICE_TABLE(virtio, id_table);
31610434 2072
8b66c917 2073static const unsigned int features[] = {
c2983458 2074 VIRTIO_CONSOLE_F_SIZE,
b99fa815 2075 VIRTIO_CONSOLE_F_MULTIPORT,
c2983458
CB
2076};
2077
8b66c917 2078static const struct virtio_device_id rproc_serial_id_table[] = {
1b637046
SB
2079#if IS_ENABLED(CONFIG_REMOTEPROC)
2080 { VIRTIO_ID_RPROC_SERIAL, VIRTIO_DEV_ANY_ID },
2081#endif
2082 { 0 },
2083};
897c44f0 2084MODULE_DEVICE_TABLE(virtio, rproc_serial_id_table);
1b637046 2085
8b66c917 2086static const unsigned int rproc_serial_features[] = {
1b637046
SB
2087};
2088
89107000 2089#ifdef CONFIG_PM_SLEEP
2b8f41d8
AS
2090static int virtcons_freeze(struct virtio_device *vdev)
2091{
2092 struct ports_device *portdev;
2093 struct port *port;
2094
2095 portdev = vdev->priv;
2096
d9679d00 2097 virtio_reset_device(vdev);
2b8f41d8 2098
2055997f
MT
2099 if (use_multiport(portdev))
2100 virtqueue_disable_cb(portdev->c_ivq);
2b8f41d8 2101 cancel_work_sync(&portdev->control_work);
eeb8a7e8 2102 cancel_work_sync(&portdev->config_work);
c743d09d
AS
2103 /*
2104 * Once more: if control_work_handler() was running, it would
2105 * enable the cb as the last step.
2106 */
2055997f
MT
2107 if (use_multiport(portdev))
2108 virtqueue_disable_cb(portdev->c_ivq);
2b8f41d8
AS
2109
2110 list_for_each_entry(port, &portdev->ports, list) {
c743d09d
AS
2111 virtqueue_disable_cb(port->in_vq);
2112 virtqueue_disable_cb(port->out_vq);
2b8f41d8
AS
2113 /*
2114 * We'll ask the host later if the new invocation has
2115 * the port opened or closed.
2116 */
2117 port->host_connected = false;
2118 remove_port_data(port);
2119 }
2120 remove_vqs(portdev);
2121
2122 return 0;
2123}
2124
2125static int virtcons_restore(struct virtio_device *vdev)
2126{
2127 struct ports_device *portdev;
2128 struct port *port;
2129 int ret;
2130
2131 portdev = vdev->priv;
2132
2133 ret = init_vqs(portdev);
2134 if (ret)
2135 return ret;
2136
401bbdc9
MT
2137 virtio_device_ready(portdev->vdev);
2138
2b8f41d8 2139 if (use_multiport(portdev))
165b1b8b 2140 fill_queue(portdev->c_ivq, &portdev->c_ivq_lock);
2b8f41d8
AS
2141
2142 list_for_each_entry(port, &portdev->ports, list) {
2143 port->in_vq = portdev->in_vqs[port->id];
2144 port->out_vq = portdev->out_vqs[port->id];
2145
2146 fill_queue(port->in_vq, &port->inbuf_lock);
2147
2148 /* Get port open/close status on the host */
2149 send_control_msg(port, VIRTIO_CONSOLE_PORT_READY, 1);
fa8b66cc
AS
2150
2151 /*
2152 * If a port was open at the time of suspending, we
2153 * have to let the host know that it's still open.
2154 */
2155 if (port->guest_connected)
2156 send_control_msg(port, VIRTIO_CONSOLE_PORT_OPEN, 1);
2b8f41d8
AS
2157 }
2158 return 0;
2159}
2160#endif
2161
31610434 2162static struct virtio_driver virtio_console = {
c2983458
CB
2163 .feature_table = features,
2164 .feature_table_size = ARRAY_SIZE(features),
31610434 2165 .driver.name = KBUILD_MODNAME,
31610434
RR
2166 .id_table = id_table,
2167 .probe = virtcons_probe,
7177876f 2168 .remove = virtcons_remove,
7f5d810d 2169 .config_changed = config_intr,
89107000 2170#ifdef CONFIG_PM_SLEEP
2b8f41d8
AS
2171 .freeze = virtcons_freeze,
2172 .restore = virtcons_restore,
2173#endif
31610434
RR
2174};
2175
bcd2982a 2176static struct virtio_driver virtio_rproc_serial = {
1b637046
SB
2177 .feature_table = rproc_serial_features,
2178 .feature_table_size = ARRAY_SIZE(rproc_serial_features),
2179 .driver.name = "virtio_rproc_serial",
1b637046
SB
2180 .id_table = rproc_serial_id_table,
2181 .probe = virtcons_probe,
2182 .remove = virtcons_remove,
2183};
2184
fefb8a2a 2185static int __init virtio_console_init(void)
31610434 2186{
fb08bd27
AS
2187 int err;
2188
11680fdf
IO
2189 err = class_register(&port_class);
2190 if (err)
fb08bd27 2191 return err;
d99393ef
AS
2192
2193 pdrvdata.debugfs_dir = debugfs_create_dir("virtio-ports", NULL);
38edf58d 2194 INIT_LIST_HEAD(&pdrvdata.consoles);
6bdf2afd 2195 INIT_LIST_HEAD(&pdrvdata.portdevs);
38edf58d 2196
33e1afc3
AK
2197 err = register_virtio_driver(&virtio_console);
2198 if (err < 0) {
2199 pr_err("Error %d registering virtio driver\n", err);
2200 goto free;
2201 }
1b637046
SB
2202 err = register_virtio_driver(&virtio_rproc_serial);
2203 if (err < 0) {
2204 pr_err("Error %d registering virtio rproc serial driver\n",
2205 err);
2206 goto unregister;
2207 }
33e1afc3 2208 return 0;
1b637046
SB
2209unregister:
2210 unregister_virtio_driver(&virtio_console);
33e1afc3 2211free:
5885e48e 2212 debugfs_remove_recursive(pdrvdata.debugfs_dir);
11680fdf 2213 class_unregister(&port_class);
33e1afc3 2214 return err;
31610434 2215}
7177876f 2216
fefb8a2a 2217static void __exit virtio_console_fini(void)
7177876f 2218{
1b637046
SB
2219 reclaim_dma_bufs();
2220
7177876f 2221 unregister_virtio_driver(&virtio_console);
1b637046 2222 unregister_virtio_driver(&virtio_rproc_serial);
7177876f 2223
11680fdf 2224 class_unregister(&port_class);
5885e48e 2225 debugfs_remove_recursive(pdrvdata.debugfs_dir);
7177876f 2226}
fefb8a2a
RD
2227module_init(virtio_console_init);
2228module_exit(virtio_console_fini);
31610434 2229
31610434
RR
2230MODULE_DESCRIPTION("Virtio console driver");
2231MODULE_LICENSE("GPL");