blob: 2359889a35a0a6711964d35df3bce342e6bc2f54 [file] [log] [blame]
Thomas Gleixner1a59d1b82019-05-27 08:55:05 +02001// SPDX-License-Identifier: GPL-2.0-or-later
Rusty Russella23ea922010-01-18 19:14:55 +05302/*
3 * Copyright (C) 2006, 2007, 2009 Rusty Russell, IBM Corporation
Amit Shah5084f892011-01-31 13:06:37 +05304 * Copyright (C) 2009, 2010, 2011 Red Hat, Inc.
5 * Copyright (C) 2009, 2010, 2011 Amit Shah <amit.shah@redhat.com>
Rusty Russell31610432007-10-22 11:03:39 +10006 */
Amit Shahfb08bd22009-12-21 21:36:04 +05307#include <linux/cdev.h>
Amit Shahd99393e2009-12-21 22:36:21 +05308#include <linux/debugfs.h>
Christian Borntraeger5e384832011-09-22 23:44:23 +05309#include <linux/completion.h>
Amit Shahfb08bd22009-12-21 21:36:04 +053010#include <linux/device.h>
Rusty Russell31610432007-10-22 11:03:39 +100011#include <linux/err.h>
Amit Shaha08fa922011-09-14 13:06:41 +053012#include <linux/freezer.h>
Amit Shah2030fa42009-12-21 21:49:30 +053013#include <linux/fs.h>
Masami Hiramatsueb5e89f2012-08-09 21:30:39 +090014#include <linux/splice.h>
15#include <linux/pagemap.h>
Rusty Russell31610432007-10-22 11:03:39 +100016#include <linux/init.h>
Amit Shah38edf582010-01-18 19:15:05 +053017#include <linux/list.h>
Amit Shah2030fa42009-12-21 21:49:30 +053018#include <linux/poll.h>
19#include <linux/sched.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090020#include <linux/slab.h>
Amit Shah38edf582010-01-18 19:15:05 +053021#include <linux/spinlock.h>
Rusty Russell31610432007-10-22 11:03:39 +100022#include <linux/virtio.h>
23#include <linux/virtio_console.h>
Amit Shah2030fa42009-12-21 21:49:30 +053024#include <linux/wait.h>
Amit Shah17634ba2009-12-21 21:03:25 +053025#include <linux/workqueue.h>
Paul Gortmakerc22405c2011-07-03 13:35:48 -040026#include <linux/module.h>
Sjur Brændeland1b637042012-12-14 14:40:51 +103027#include <linux/dma-mapping.h>
Amit Shah51df0ac2011-02-01 09:31:25 +053028#include "../tty/hvc/hvc_console.h"
Rusty Russell31610432007-10-22 11:03:39 +100029
Sjur Brændeland1b637042012-12-14 14:40:51 +103030#define is_rproc_enabled IS_ENABLED(CONFIG_REMOTEPROC)
Jason Wang28962ec2021-10-19 15:01:44 +080031#define VIRTCONS_MAX_PORTS 0x8000
Sjur Brændeland1b637042012-12-14 14:40:51 +103032
Amit Shah38edf582010-01-18 19:15:05 +053033/*
34 * This is a global struct for storing common data for all the devices
35 * this driver handles.
36 *
37 * Mainly, it has a linked list for all the consoles in one place so
38 * that callbacks from hvc for get_chars(), put_chars() work properly
39 * across multiple devices and multiple ports per device.
40 */
41struct ports_driver_data {
Amit Shahfb08bd22009-12-21 21:36:04 +053042 /* Used for registering chardevs */
43 struct class *class;
44
Amit Shahd99393e2009-12-21 22:36:21 +053045 /* Used for exporting per-port information to debugfs */
46 struct dentry *debugfs_dir;
47
Amit Shah6bdf2af2010-09-02 18:11:49 +053048 /* List of all the devices we're handling */
49 struct list_head portdevs;
50
Rusty Russelld8a02bd2010-01-18 19:15:06 +053051 /*
52 * This is used to keep track of the number of hvc consoles
53 * spawned by this driver. This number is given as the first
54 * argument to hvc_alloc(). To correctly map an initial
55 * console spawned via hvc_instantiate to the console being
56 * hooked up via hvc_alloc, we need to pass the same vtermno.
57 *
58 * We also just assume the first console being initialised was
59 * the first one that got used as the initial console.
60 */
61 unsigned int next_vtermno;
62
Amit Shah38edf582010-01-18 19:15:05 +053063 /* All the console devices handled by this driver */
64 struct list_head consoles;
65};
Pankaj Gupta4b0a2c52019-03-19 11:34:06 +053066static struct ports_driver_data pdrvdata = { .next_vtermno = 1};
Amit Shah38edf582010-01-18 19:15:05 +053067
Wei Yongjun38268352013-04-08 16:13:59 +093068static DEFINE_SPINLOCK(pdrvdata_lock);
69static DECLARE_COMPLETION(early_console_added);
Amit Shah38edf582010-01-18 19:15:05 +053070
Amit Shah4f23c572010-01-18 19:15:09 +053071/* This struct holds information that's relevant only for console ports */
72struct console {
73 /* We'll place all consoles in a list in the pdrvdata struct */
74 struct list_head list;
75
76 /* The hvc device associated with this console port */
77 struct hvc_struct *hvc;
78
Amit Shah97788292010-05-06 02:05:08 +053079 /* The size of the console */
80 struct winsize ws;
81
Amit Shah4f23c572010-01-18 19:15:09 +053082 /*
83 * This number identifies the number that we used to register
84 * with hvc in hvc_instantiate() and hvc_alloc(); this is the
85 * number passed on by the hvc callbacks to us to
86 * differentiate between the other console ports handled by
87 * this driver
88 */
89 u32 vtermno;
90};
91
Amit Shahfdb9a052010-01-18 19:15:01 +053092struct port_buffer {
93 char *buf;
94
95 /* size of the buffer in *buf above */
96 size_t size;
97
98 /* used length of the buffer */
99 size_t len;
100 /* offset in the buf from which to consume data */
101 size_t offset;
Sjur Brændeland276a3e952012-12-14 13:46:42 +1030102
Sjur Brændeland1b637042012-12-14 14:40:51 +1030103 /* DMA address of buffer */
104 dma_addr_t dma;
105
106 /* Device we got DMA memory from */
107 struct device *dev;
108
109 /* List of pending dma buffers to free */
110 struct list_head list;
111
Sjur Brændeland276a3e952012-12-14 13:46:42 +1030112 /* If sgpages == 0 then buf is used */
113 unsigned int sgpages;
114
115 /* sg is used if spages > 0. sg must be the last in is struct */
Gustavo A. R. Silva1031eb92020-02-11 16:29:41 -0600116 struct scatterlist sg[];
Amit Shahfdb9a052010-01-18 19:15:01 +0530117};
118
Amit Shah17634ba2009-12-21 21:03:25 +0530119/*
120 * This is a per-device struct that stores data common to all the
121 * ports for that device (vdev->priv).
122 */
123struct ports_device {
Amit Shah6bdf2af2010-09-02 18:11:49 +0530124 /* Next portdev in the list, head is in the pdrvdata struct */
125 struct list_head list;
126
Amit Shah17634ba2009-12-21 21:03:25 +0530127 /*
128 * Workqueue handlers where we process deferred work after
129 * notification
130 */
131 struct work_struct control_work;
Michael S. Tsirkineeb8a7e2015-03-05 10:45:49 +1030132 struct work_struct config_work;
Amit Shah17634ba2009-12-21 21:03:25 +0530133
134 struct list_head ports;
135
136 /* To protect the list of ports */
137 spinlock_t ports_lock;
138
139 /* To protect the vq operations for the control channel */
Amit Shah165b1b82013-03-29 16:30:07 +0530140 spinlock_t c_ivq_lock;
Amit Shah9ba5c802013-03-29 16:30:08 +0530141 spinlock_t c_ovq_lock;
Amit Shah17634ba2009-12-21 21:03:25 +0530142
Michael S. Tsirkin7328fa62016-12-05 21:39:42 +0200143 /* max. number of ports this device can hold */
144 u32 max_nr_ports;
Amit Shah17634ba2009-12-21 21:03:25 +0530145
146 /* The virtio device we're associated with */
147 struct virtio_device *vdev;
148
149 /*
150 * A couple of virtqueues for the control channel: one for
151 * guest->host transfers, one for host->guest transfers
152 */
153 struct virtqueue *c_ivq, *c_ovq;
154
Andy Lutomirski5e59d9a2016-08-30 08:04:15 -0700155 /*
156 * A control packet buffer for guest->host requests, protected
157 * by c_ovq_lock.
158 */
159 struct virtio_console_control cpkt;
160
Amit Shah17634ba2009-12-21 21:03:25 +0530161 /* Array of per-port IO virtqueues */
162 struct virtqueue **in_vqs, **out_vqs;
Amit Shahfb08bd22009-12-21 21:36:04 +0530163
Amit Shahfb08bd22009-12-21 21:36:04 +0530164 /* Major number for this device. Ports will be created as minors. */
165 int chr_major;
Amit Shah17634ba2009-12-21 21:03:25 +0530166};
167
Amit Shah17e5b4f2011-09-14 13:06:46 +0530168struct port_stats {
169 unsigned long bytes_sent, bytes_received, bytes_discarded;
170};
171
Amit Shah1c85bf32010-01-18 19:15:07 +0530172/* This struct holds the per-port data */
Rusty Russell21206ed2010-01-18 19:15:00 +0530173struct port {
Amit Shah17634ba2009-12-21 21:03:25 +0530174 /* Next port in the list, head is in the ports_device */
175 struct list_head list;
176
Amit Shah1c85bf32010-01-18 19:15:07 +0530177 /* Pointer to the parent virtio_console device */
178 struct ports_device *portdev;
Amit Shahfdb9a052010-01-18 19:15:01 +0530179
180 /* The current buffer from which data has to be fed to readers */
181 struct port_buffer *inbuf;
Rusty Russell31610432007-10-22 11:03:39 +1000182
Amit Shah203baab2010-01-18 19:15:12 +0530183 /*
184 * To protect the operations on the in_vq associated with this
185 * port. Has to be a spinlock because it can be called from
186 * interrupt context (get_char()).
187 */
188 spinlock_t inbuf_lock;
189
Amit Shahcdfadfc2010-05-19 22:15:50 -0600190 /* Protect the operations on the out_vq. */
191 spinlock_t outvq_lock;
192
Amit Shah1c85bf32010-01-18 19:15:07 +0530193 /* The IO vqs for this port */
194 struct virtqueue *in_vq, *out_vq;
195
Amit Shahd99393e2009-12-21 22:36:21 +0530196 /* File in the debugfs directory that exposes this port's information */
197 struct dentry *debugfs_file;
198
Amit Shah4f23c572010-01-18 19:15:09 +0530199 /*
Amit Shah17e5b4f2011-09-14 13:06:46 +0530200 * Keep count of the bytes sent, received and discarded for
201 * this port for accounting and debugging purposes. These
202 * counts are not reset across port open / close events.
203 */
204 struct port_stats stats;
205
206 /*
Amit Shah4f23c572010-01-18 19:15:09 +0530207 * The entries in this struct will be valid if this port is
208 * hooked up to an hvc console
209 */
210 struct console cons;
Amit Shah17634ba2009-12-21 21:03:25 +0530211
Amit Shahfb08bd22009-12-21 21:36:04 +0530212 /* Each port associates with a separate char device */
Amit Shahd22a6982010-09-02 18:20:59 +0530213 struct cdev *cdev;
Amit Shahfb08bd22009-12-21 21:36:04 +0530214 struct device *dev;
215
Amit Shahb353a6b2010-09-02 18:38:29 +0530216 /* Reference-counting to handle port hot-unplugs and file operations */
217 struct kref kref;
218
Amit Shah2030fa42009-12-21 21:49:30 +0530219 /* A waitqueue for poll() or blocking read operations */
220 wait_queue_head_t waitqueue;
221
Amit Shah431edb82009-12-21 21:57:40 +0530222 /* The 'name' of the port that we expose via sysfs properties */
223 char *name;
224
Amit Shah3eae0ad2010-09-02 18:47:52 +0530225 /* We can notify apps of host connect / disconnect events via SIGIO */
226 struct fasync_struct *async_queue;
227
Amit Shah17634ba2009-12-21 21:03:25 +0530228 /* The 'id' to identify the port with the Host */
229 u32 id;
Amit Shah2030fa42009-12-21 21:49:30 +0530230
Amit Shahcdfadfc2010-05-19 22:15:50 -0600231 bool outvq_full;
232
Amit Shah2030fa42009-12-21 21:49:30 +0530233 /* Is the host device open */
234 bool host_connected;
Amit Shah3c7969c2009-11-26 11:25:38 +0530235
236 /* We should allow only one process to open a port */
237 bool guest_connected;
Rusty Russell21206ed2010-01-18 19:15:00 +0530238};
Rusty Russell31610432007-10-22 11:03:39 +1000239
Rusty Russell971f3392010-01-18 19:14:56 +0530240/* This is the very early arch-specified put chars function. */
241static int (*early_put_chars)(u32, const char *, int);
242
Amit Shah38edf582010-01-18 19:15:05 +0530243static struct port *find_port_by_vtermno(u32 vtermno)
244{
245 struct port *port;
Amit Shah4f23c572010-01-18 19:15:09 +0530246 struct console *cons;
Amit Shah38edf582010-01-18 19:15:05 +0530247 unsigned long flags;
248
249 spin_lock_irqsave(&pdrvdata_lock, flags);
Amit Shah4f23c572010-01-18 19:15:09 +0530250 list_for_each_entry(cons, &pdrvdata.consoles, list) {
251 if (cons->vtermno == vtermno) {
252 port = container_of(cons, struct port, cons);
Amit Shah38edf582010-01-18 19:15:05 +0530253 goto out;
Amit Shah4f23c572010-01-18 19:15:09 +0530254 }
Amit Shah38edf582010-01-18 19:15:05 +0530255 }
256 port = NULL;
257out:
258 spin_unlock_irqrestore(&pdrvdata_lock, flags);
259 return port;
260}
261
Amit Shah04950cd2010-09-02 18:20:58 +0530262static struct port *find_port_by_devt_in_portdev(struct ports_device *portdev,
263 dev_t dev)
264{
265 struct port *port;
266 unsigned long flags;
267
268 spin_lock_irqsave(&portdev->ports_lock, flags);
Amit Shah057b82b2013-07-29 14:16:13 +0930269 list_for_each_entry(port, &portdev->ports, list) {
270 if (port->cdev->dev == dev) {
271 kref_get(&port->kref);
Amit Shah04950cd2010-09-02 18:20:58 +0530272 goto out;
Amit Shah057b82b2013-07-29 14:16:13 +0930273 }
274 }
Amit Shah04950cd2010-09-02 18:20:58 +0530275 port = NULL;
276out:
277 spin_unlock_irqrestore(&portdev->ports_lock, flags);
278
279 return port;
280}
281
282static struct port *find_port_by_devt(dev_t dev)
283{
284 struct ports_device *portdev;
285 struct port *port;
286 unsigned long flags;
287
288 spin_lock_irqsave(&pdrvdata_lock, flags);
289 list_for_each_entry(portdev, &pdrvdata.portdevs, list) {
290 port = find_port_by_devt_in_portdev(portdev, dev);
291 if (port)
292 goto out;
293 }
294 port = NULL;
295out:
296 spin_unlock_irqrestore(&pdrvdata_lock, flags);
297 return port;
298}
299
Amit Shah17634ba2009-12-21 21:03:25 +0530300static struct port *find_port_by_id(struct ports_device *portdev, u32 id)
301{
302 struct port *port;
303 unsigned long flags;
304
305 spin_lock_irqsave(&portdev->ports_lock, flags);
306 list_for_each_entry(port, &portdev->ports, list)
307 if (port->id == id)
308 goto out;
309 port = NULL;
310out:
311 spin_unlock_irqrestore(&portdev->ports_lock, flags);
312
313 return port;
314}
315
Amit Shah203baab2010-01-18 19:15:12 +0530316static struct port *find_port_by_vq(struct ports_device *portdev,
317 struct virtqueue *vq)
318{
319 struct port *port;
Amit Shah203baab2010-01-18 19:15:12 +0530320 unsigned long flags;
321
Amit Shah17634ba2009-12-21 21:03:25 +0530322 spin_lock_irqsave(&portdev->ports_lock, flags);
323 list_for_each_entry(port, &portdev->ports, list)
Amit Shah203baab2010-01-18 19:15:12 +0530324 if (port->in_vq == vq || port->out_vq == vq)
325 goto out;
Amit Shah203baab2010-01-18 19:15:12 +0530326 port = NULL;
327out:
Amit Shah17634ba2009-12-21 21:03:25 +0530328 spin_unlock_irqrestore(&portdev->ports_lock, flags);
Amit Shah203baab2010-01-18 19:15:12 +0530329 return port;
330}
331
Amit Shah17634ba2009-12-21 21:03:25 +0530332static bool is_console_port(struct port *port)
333{
334 if (port->cons.hvc)
335 return true;
336 return false;
337}
338
Sjur Brændeland1b637042012-12-14 14:40:51 +1030339static bool is_rproc_serial(const struct virtio_device *vdev)
340{
341 return is_rproc_enabled && vdev->id.device == VIRTIO_ID_RPROC_SERIAL;
342}
343
Amit Shah17634ba2009-12-21 21:03:25 +0530344static inline bool use_multiport(struct ports_device *portdev)
345{
346 /*
347 * This condition can be true when put_chars is called from
348 * early_init
349 */
350 if (!portdev->vdev)
Joe Perchesf580d732015-03-30 16:46:10 -0700351 return false;
Michael S. Tsirkine16e12b2014-10-07 16:39:42 +0200352 return __virtio_test_bit(portdev->vdev, VIRTIO_CONSOLE_F_MULTIPORT);
Amit Shah17634ba2009-12-21 21:03:25 +0530353}
354
Sjur Brændeland1b637042012-12-14 14:40:51 +1030355static DEFINE_SPINLOCK(dma_bufs_lock);
356static LIST_HEAD(pending_free_dma_bufs);
357
358static void free_buf(struct port_buffer *buf, bool can_sleep)
Amit Shahfdb9a052010-01-18 19:15:01 +0530359{
Sjur Brændeland276a3e952012-12-14 13:46:42 +1030360 unsigned int i;
361
Sjur Brændeland276a3e952012-12-14 13:46:42 +1030362 for (i = 0; i < buf->sgpages; i++) {
363 struct page *page = sg_page(&buf->sg[i]);
364 if (!page)
365 break;
366 put_page(page);
367 }
368
Sjur Brændeland1b637042012-12-14 14:40:51 +1030369 if (!buf->dev) {
370 kfree(buf->buf);
371 } else if (is_rproc_enabled) {
372 unsigned long flags;
373
374 /* dma_free_coherent requires interrupts to be enabled. */
375 if (!can_sleep) {
376 /* queue up dma-buffers to be freed later */
377 spin_lock_irqsave(&dma_bufs_lock, flags);
378 list_add_tail(&buf->list, &pending_free_dma_bufs);
379 spin_unlock_irqrestore(&dma_bufs_lock, flags);
380 return;
381 }
382 dma_free_coherent(buf->dev, buf->size, buf->buf, buf->dma);
383
384 /* Release device refcnt and allow it to be freed */
385 put_device(buf->dev);
386 }
387
Amit Shahfdb9a052010-01-18 19:15:01 +0530388 kfree(buf);
389}
390
Sjur Brændeland1b637042012-12-14 14:40:51 +1030391static void reclaim_dma_bufs(void)
392{
393 unsigned long flags;
394 struct port_buffer *buf, *tmp;
395 LIST_HEAD(tmp_list);
396
397 if (list_empty(&pending_free_dma_bufs))
398 return;
399
400 /* Create a copy of the pending_free_dma_bufs while holding the lock */
401 spin_lock_irqsave(&dma_bufs_lock, flags);
402 list_cut_position(&tmp_list, &pending_free_dma_bufs,
403 pending_free_dma_bufs.prev);
404 spin_unlock_irqrestore(&dma_bufs_lock, flags);
405
406 /* Release the dma buffers, without irqs enabled */
407 list_for_each_entry_safe(buf, tmp, &tmp_list, list) {
408 list_del(&buf->list);
409 free_buf(buf, true);
410 }
411}
412
Michael S. Tsirkin2855b332018-04-20 19:54:23 +0300413static struct port_buffer *alloc_buf(struct virtio_device *vdev, size_t buf_size,
Sjur Brændeland276a3e952012-12-14 13:46:42 +1030414 int pages)
Amit Shahfdb9a052010-01-18 19:15:01 +0530415{
416 struct port_buffer *buf;
417
Sjur Brændeland1b637042012-12-14 14:40:51 +1030418 reclaim_dma_bufs();
419
Sjur Brændeland276a3e952012-12-14 13:46:42 +1030420 /*
421 * Allocate buffer and the sg list. The sg list array is allocated
422 * directly after the port_buffer struct.
423 */
Matthew Wilcox5b572e22018-06-07 07:57:15 -0700424 buf = kmalloc(struct_size(buf, sg, pages), GFP_KERNEL);
Amit Shahfdb9a052010-01-18 19:15:01 +0530425 if (!buf)
426 goto fail;
Sjur Brændeland276a3e952012-12-14 13:46:42 +1030427
428 buf->sgpages = pages;
429 if (pages > 0) {
Sjur Brændeland1b637042012-12-14 14:40:51 +1030430 buf->dev = NULL;
Sjur Brændeland276a3e952012-12-14 13:46:42 +1030431 buf->buf = NULL;
432 return buf;
433 }
434
Michael S. Tsirkin2855b332018-04-20 19:54:23 +0300435 if (is_rproc_serial(vdev)) {
Sjur Brændeland1b637042012-12-14 14:40:51 +1030436 /*
437 * Allocate DMA memory from ancestor. When a virtio
438 * device is created by remoteproc, the DMA memory is
Alexander Lobakin9d516aa2020-11-04 15:31:36 +0000439 * associated with the parent device:
440 * virtioY => remoteprocX#vdevYbuffer.
Sjur Brændeland1b637042012-12-14 14:40:51 +1030441 */
Alexander Lobakin9d516aa2020-11-04 15:31:36 +0000442 buf->dev = vdev->dev.parent;
443 if (!buf->dev)
Sjur Brændeland1b637042012-12-14 14:40:51 +1030444 goto free_buf;
Sjur Brændeland1b637042012-12-14 14:40:51 +1030445
446 /* Increase device refcnt to avoid freeing it */
447 get_device(buf->dev);
448 buf->buf = dma_alloc_coherent(buf->dev, buf_size, &buf->dma,
449 GFP_KERNEL);
450 } else {
451 buf->dev = NULL;
452 buf->buf = kmalloc(buf_size, GFP_KERNEL);
453 }
454
Amit Shahfdb9a052010-01-18 19:15:01 +0530455 if (!buf->buf)
456 goto free_buf;
457 buf->len = 0;
458 buf->offset = 0;
459 buf->size = buf_size;
460 return buf;
461
462free_buf:
463 kfree(buf);
464fail:
465 return NULL;
466}
467
Amit Shaha3cde442010-01-18 19:15:03 +0530468/* Callers should take appropriate locks */
Amit Shahdefde662011-09-14 13:06:42 +0530469static struct port_buffer *get_inbuf(struct port *port)
Amit Shaha3cde442010-01-18 19:15:03 +0530470{
471 struct port_buffer *buf;
Amit Shaha3cde442010-01-18 19:15:03 +0530472 unsigned int len;
473
Amit Shahd25a9dd2011-09-14 13:06:43 +0530474 if (port->inbuf)
475 return port->inbuf;
476
477 buf = virtqueue_get_buf(port->in_vq, &len);
Amit Shaha3cde442010-01-18 19:15:03 +0530478 if (buf) {
Xie Yongjid00d8da2021-05-25 20:56:22 +0800479 buf->len = min_t(size_t, len, buf->size);
Amit Shaha3cde442010-01-18 19:15:03 +0530480 buf->offset = 0;
Amit Shah17e5b4f2011-09-14 13:06:46 +0530481 port->stats.bytes_received += len;
Amit Shaha3cde442010-01-18 19:15:03 +0530482 }
483 return buf;
484}
485
Rusty Russella23ea922010-01-18 19:14:55 +0530486/*
Amit Shahe27b5192010-01-18 19:15:02 +0530487 * Create a scatter-gather list representing our input buffer and put
488 * it in the queue.
489 *
490 * Callers should take appropriate locks.
491 */
Amit Shah203baab2010-01-18 19:15:12 +0530492static int add_inbuf(struct virtqueue *vq, struct port_buffer *buf)
Amit Shahe27b5192010-01-18 19:15:02 +0530493{
494 struct scatterlist sg[1];
Amit Shah203baab2010-01-18 19:15:12 +0530495 int ret;
Amit Shah1c85bf32010-01-18 19:15:07 +0530496
Amit Shahe27b5192010-01-18 19:15:02 +0530497 sg_init_one(sg, buf->buf, buf->size);
498
Rusty Russell67979992013-03-20 15:44:29 +1030499 ret = virtqueue_add_inbuf(vq, sg, 1, buf, GFP_ATOMIC);
Michael S. Tsirkin505b0452010-04-12 16:18:32 +0300500 virtqueue_kick(vq);
Amit Shah49e86f12012-12-10 09:45:12 +1030501 if (!ret)
502 ret = vq->num_free;
Amit Shah203baab2010-01-18 19:15:12 +0530503 return ret;
504}
505
Amit Shah88f251a2009-12-21 22:15:30 +0530506/* Discard any unread data this port has. Callers lockers. */
507static void discard_port_data(struct port *port)
508{
509 struct port_buffer *buf;
Amit Shah2d24cda2011-09-14 13:06:45 +0530510 unsigned int err;
Amit Shah88f251a2009-12-21 22:15:30 +0530511
Amit Shahd7a62cd2011-03-04 14:04:33 +1030512 if (!port->portdev) {
513 /* Device has been unplugged. vqs are already gone. */
514 return;
515 }
Amit Shah2d24cda2011-09-14 13:06:45 +0530516 buf = get_inbuf(port);
Amit Shah88f251a2009-12-21 22:15:30 +0530517
Amit Shahce072a02011-09-14 13:06:44 +0530518 err = 0;
Amit Shahd6933562010-02-12 10:32:18 +0530519 while (buf) {
Amit Shah17e5b4f2011-09-14 13:06:46 +0530520 port->stats.bytes_discarded += buf->len - buf->offset;
Amit Shah2d24cda2011-09-14 13:06:45 +0530521 if (add_inbuf(port->in_vq, buf) < 0) {
Amit Shahce072a02011-09-14 13:06:44 +0530522 err++;
Sjur Brændeland1b637042012-12-14 14:40:51 +1030523 free_buf(buf, false);
Amit Shahd6933562010-02-12 10:32:18 +0530524 }
Amit Shah2d24cda2011-09-14 13:06:45 +0530525 port->inbuf = NULL;
526 buf = get_inbuf(port);
Amit Shah88f251a2009-12-21 22:15:30 +0530527 }
Amit Shahce072a02011-09-14 13:06:44 +0530528 if (err)
Amit Shahd6933562010-02-12 10:32:18 +0530529 dev_warn(port->dev, "Errors adding %d buffers back to vq\n",
Amit Shahce072a02011-09-14 13:06:44 +0530530 err);
Amit Shah88f251a2009-12-21 22:15:30 +0530531}
532
Amit Shah203baab2010-01-18 19:15:12 +0530533static bool port_has_data(struct port *port)
534{
535 unsigned long flags;
536 bool ret;
537
Amit Shahd6933562010-02-12 10:32:18 +0530538 ret = false;
Amit Shahd25a9dd2011-09-14 13:06:43 +0530539 spin_lock_irqsave(&port->inbuf_lock, flags);
540 port->inbuf = get_inbuf(port);
541 if (port->inbuf)
542 ret = true;
543
Amit Shah203baab2010-01-18 19:15:12 +0530544 spin_unlock_irqrestore(&port->inbuf_lock, flags);
Amit Shah203baab2010-01-18 19:15:12 +0530545 return ret;
546}
547
Amit Shah3425e702010-05-19 22:15:46 -0600548static ssize_t __send_control_msg(struct ports_device *portdev, u32 port_id,
549 unsigned int event, unsigned int value)
Amit Shah17634ba2009-12-21 21:03:25 +0530550{
551 struct scatterlist sg[1];
Amit Shah17634ba2009-12-21 21:03:25 +0530552 struct virtqueue *vq;
Amit Shah604b2ad2010-02-24 10:36:51 +0530553 unsigned int len;
Amit Shah17634ba2009-12-21 21:03:25 +0530554
Amit Shah3425e702010-05-19 22:15:46 -0600555 if (!use_multiport(portdev))
Amit Shah17634ba2009-12-21 21:03:25 +0530556 return 0;
557
Amit Shah3425e702010-05-19 22:15:46 -0600558 vq = portdev->c_ovq;
Amit Shah17634ba2009-12-21 21:03:25 +0530559
Amit Shah9ba5c802013-03-29 16:30:08 +0530560 spin_lock(&portdev->c_ovq_lock);
Andy Lutomirski5e59d9a2016-08-30 08:04:15 -0700561
562 portdev->cpkt.id = cpu_to_virtio32(portdev->vdev, port_id);
563 portdev->cpkt.event = cpu_to_virtio16(portdev->vdev, event);
564 portdev->cpkt.value = cpu_to_virtio16(portdev->vdev, value);
565
566 sg_init_one(sg, &portdev->cpkt, sizeof(struct virtio_console_control));
567
568 if (virtqueue_add_outbuf(vq, sg, 1, &portdev->cpkt, GFP_ATOMIC) == 0) {
Michael S. Tsirkin505b0452010-04-12 16:18:32 +0300569 virtqueue_kick(vq);
Heinz Graalfs40e4dc52013-10-29 09:40:39 +1030570 while (!virtqueue_get_buf(vq, &len)
571 && !virtqueue_is_broken(vq))
Amit Shah17634ba2009-12-21 21:03:25 +0530572 cpu_relax();
573 }
Andy Lutomirski5e59d9a2016-08-30 08:04:15 -0700574
Amit Shah9ba5c802013-03-29 16:30:08 +0530575 spin_unlock(&portdev->c_ovq_lock);
Amit Shah17634ba2009-12-21 21:03:25 +0530576 return 0;
577}
578
Amit Shah3425e702010-05-19 22:15:46 -0600579static ssize_t send_control_msg(struct port *port, unsigned int event,
580 unsigned int value)
581{
Amit Shah84ec06c2010-09-02 18:11:42 +0530582 /* Did the port get unplugged before userspace closed it? */
583 if (port->portdev)
584 return __send_control_msg(port->portdev, port->id, event, value);
585 return 0;
Amit Shah3425e702010-05-19 22:15:46 -0600586}
587
Masami Hiramatsueb5e89f2012-08-09 21:30:39 +0900588
Amit Shahcdfadfc2010-05-19 22:15:50 -0600589/* Callers must take the port->outvq_lock */
590static void reclaim_consumed_buffers(struct port *port)
591{
Sjur Brændeland276a3e952012-12-14 13:46:42 +1030592 struct port_buffer *buf;
Amit Shahcdfadfc2010-05-19 22:15:50 -0600593 unsigned int len;
594
Amit Shahd7a62cd2011-03-04 14:04:33 +1030595 if (!port->portdev) {
596 /* Device has been unplugged. vqs are already gone. */
597 return;
598 }
Sjur Brændeland276a3e952012-12-14 13:46:42 +1030599 while ((buf = virtqueue_get_buf(port->out_vq, &len))) {
Sjur Brændeland1b637042012-12-14 14:40:51 +1030600 free_buf(buf, false);
Amit Shahcdfadfc2010-05-19 22:15:50 -0600601 port->outvq_full = false;
602 }
603}
604
Masami Hiramatsueb5e89f2012-08-09 21:30:39 +0900605static ssize_t __send_to_port(struct port *port, struct scatterlist *sg,
606 int nents, size_t in_count,
Sjur Brændeland276a3e952012-12-14 13:46:42 +1030607 void *data, bool nonblock)
Amit Shahf997f00b2009-12-21 17:28:51 +0530608{
Amit Shahf997f00b2009-12-21 17:28:51 +0530609 struct virtqueue *out_vq;
Rusty Russell589575a2012-10-16 23:56:15 +1030610 int err;
Amit Shahcdfadfc2010-05-19 22:15:50 -0600611 unsigned long flags;
Amit Shahf997f00b2009-12-21 17:28:51 +0530612 unsigned int len;
613
614 out_vq = port->out_vq;
615
Amit Shahcdfadfc2010-05-19 22:15:50 -0600616 spin_lock_irqsave(&port->outvq_lock, flags);
617
618 reclaim_consumed_buffers(port);
619
Rusty Russell67979992013-03-20 15:44:29 +1030620 err = virtqueue_add_outbuf(out_vq, sg, nents, data, GFP_ATOMIC);
Amit Shahf997f00b2009-12-21 17:28:51 +0530621
622 /* Tell Host to go! */
Michael S. Tsirkin505b0452010-04-12 16:18:32 +0300623 virtqueue_kick(out_vq);
Amit Shahf997f00b2009-12-21 17:28:51 +0530624
Rusty Russell589575a2012-10-16 23:56:15 +1030625 if (err) {
Rusty Russell9ff4cfa2010-04-08 09:46:16 -0600626 in_count = 0;
Amit Shahcdfadfc2010-05-19 22:15:50 -0600627 goto done;
Amit Shahf997f00b2009-12-21 17:28:51 +0530628 }
629
Rusty Russell589575a2012-10-16 23:56:15 +1030630 if (out_vq->num_free == 0)
Amit Shahcdfadfc2010-05-19 22:15:50 -0600631 port->outvq_full = true;
632
633 if (nonblock)
634 goto done;
635
636 /*
637 * Wait till the host acknowledges it pushed out the data we
Amit Shah531295e62010-10-20 13:45:43 +1030638 * sent. This is done for data from the hvc_console; the tty
639 * operations are performed with spinlocks held so we can't
640 * sleep here. An alternative would be to copy the data to a
641 * buffer and relax the spinning requirement. The downside is
642 * we need to kmalloc a GFP_ATOMIC buffer each time the
643 * console driver writes something out.
Amit Shahcdfadfc2010-05-19 22:15:50 -0600644 */
Heinz Graalfs40e4dc52013-10-29 09:40:39 +1030645 while (!virtqueue_get_buf(out_vq, &len)
646 && !virtqueue_is_broken(out_vq))
Amit Shahf997f00b2009-12-21 17:28:51 +0530647 cpu_relax();
Amit Shahcdfadfc2010-05-19 22:15:50 -0600648done:
649 spin_unlock_irqrestore(&port->outvq_lock, flags);
Amit Shah17e5b4f2011-09-14 13:06:46 +0530650
651 port->stats.bytes_sent += in_count;
Amit Shahcdfadfc2010-05-19 22:15:50 -0600652 /*
653 * We're expected to return the amount of data we wrote -- all
654 * of it
655 */
Rusty Russell9ff4cfa2010-04-08 09:46:16 -0600656 return in_count;
Amit Shahf997f00b2009-12-21 17:28:51 +0530657}
658
Amit Shah203baab2010-01-18 19:15:12 +0530659/*
660 * Give out the data that's requested from the buffer that we have
661 * queued up.
662 */
Michael S. Tsirkin48b36062014-12-01 13:31:45 +0200663static ssize_t fill_readbuf(struct port *port, char __user *out_buf,
664 size_t out_count, bool to_user)
Amit Shah203baab2010-01-18 19:15:12 +0530665{
666 struct port_buffer *buf;
667 unsigned long flags;
668
669 if (!out_count || !port_has_data(port))
670 return 0;
671
672 buf = port->inbuf;
Amit Shahb766cee2009-12-21 21:26:45 +0530673 out_count = min(out_count, buf->len - buf->offset);
Amit Shah203baab2010-01-18 19:15:12 +0530674
Amit Shahb766cee2009-12-21 21:26:45 +0530675 if (to_user) {
676 ssize_t ret;
Amit Shah203baab2010-01-18 19:15:12 +0530677
Amit Shahb766cee2009-12-21 21:26:45 +0530678 ret = copy_to_user(out_buf, buf->buf + buf->offset, out_count);
679 if (ret)
680 return -EFAULT;
681 } else {
Michael S. Tsirkin48b36062014-12-01 13:31:45 +0200682 memcpy((__force char *)out_buf, buf->buf + buf->offset,
683 out_count);
Amit Shahb766cee2009-12-21 21:26:45 +0530684 }
685
Amit Shah203baab2010-01-18 19:15:12 +0530686 buf->offset += out_count;
687
688 if (buf->offset == buf->len) {
689 /*
690 * We're done using all the data in this buffer.
691 * Re-queue so that the Host can send us more data.
692 */
693 spin_lock_irqsave(&port->inbuf_lock, flags);
694 port->inbuf = NULL;
695
696 if (add_inbuf(port->in_vq, buf) < 0)
Amit Shahfb08bd22009-12-21 21:36:04 +0530697 dev_warn(port->dev, "failed add_buf\n");
Amit Shah203baab2010-01-18 19:15:12 +0530698
699 spin_unlock_irqrestore(&port->inbuf_lock, flags);
700 }
Amit Shahb766cee2009-12-21 21:26:45 +0530701 /* Return the number of bytes actually copied */
Amit Shah203baab2010-01-18 19:15:12 +0530702 return out_count;
Amit Shahe27b5192010-01-18 19:15:02 +0530703}
704
Amit Shah2030fa42009-12-21 21:49:30 +0530705/* The condition that must be true for polling to end */
Amit Shah60caacd2010-05-19 22:15:49 -0600706static bool will_read_block(struct port *port)
Amit Shah2030fa42009-12-21 21:49:30 +0530707{
Amit Shah3709ea72010-09-02 18:11:43 +0530708 if (!port->guest_connected) {
709 /* Port got hot-unplugged. Let's exit. */
710 return false;
711 }
Amit Shah60caacd2010-05-19 22:15:49 -0600712 return !port_has_data(port) && port->host_connected;
Amit Shah2030fa42009-12-21 21:49:30 +0530713}
714
Amit Shahcdfadfc2010-05-19 22:15:50 -0600715static bool will_write_block(struct port *port)
716{
717 bool ret;
718
Amit Shah60e5e0b2010-05-27 13:24:40 +0530719 if (!port->guest_connected) {
720 /* Port got hot-unplugged. Let's exit. */
721 return false;
722 }
Amit Shahcdfadfc2010-05-19 22:15:50 -0600723 if (!port->host_connected)
724 return true;
725
726 spin_lock_irq(&port->outvq_lock);
727 /*
728 * Check if the Host has consumed any buffers since we last
729 * sent data (this is only applicable for nonblocking ports).
730 */
731 reclaim_consumed_buffers(port);
732 ret = port->outvq_full;
733 spin_unlock_irq(&port->outvq_lock);
734
735 return ret;
736}
737
Amit Shah2030fa42009-12-21 21:49:30 +0530738static ssize_t port_fops_read(struct file *filp, char __user *ubuf,
739 size_t count, loff_t *offp)
740{
741 struct port *port;
742 ssize_t ret;
743
744 port = filp->private_data;
745
Amit Shah96f97a82013-07-29 14:23:21 +0930746 /* Port is hot-unplugged. */
747 if (!port->guest_connected)
748 return -ENODEV;
749
Amit Shah2030fa42009-12-21 21:49:30 +0530750 if (!port_has_data(port)) {
751 /*
752 * If nothing's connected on the host just return 0 in
753 * case of list_empty; this tells the userspace app
754 * that there's no connection
755 */
756 if (!port->host_connected)
757 return 0;
758 if (filp->f_flags & O_NONBLOCK)
759 return -EAGAIN;
760
Amit Shaha08fa922011-09-14 13:06:41 +0530761 ret = wait_event_freezable(port->waitqueue,
762 !will_read_block(port));
Amit Shah2030fa42009-12-21 21:49:30 +0530763 if (ret < 0)
764 return ret;
765 }
Amit Shah96f97a82013-07-29 14:23:21 +0930766 /* Port got hot-unplugged while we were waiting above. */
Amit Shahb3dddb92010-09-02 18:11:45 +0530767 if (!port->guest_connected)
768 return -ENODEV;
Amit Shah2030fa42009-12-21 21:49:30 +0530769 /*
770 * We could've received a disconnection message while we were
771 * waiting for more data.
772 *
773 * This check is not clubbed in the if() statement above as we
774 * might receive some data as well as the host could get
775 * disconnected after we got woken up from our wait. So we
776 * really want to give off whatever data we have and only then
777 * check for host_connected.
778 */
779 if (!port_has_data(port) && !port->host_connected)
780 return 0;
781
782 return fill_readbuf(port, ubuf, count, true);
783}
784
Masami Hiramatsuefe75d22012-08-09 21:31:00 +0900785static int wait_port_writable(struct port *port, bool nonblock)
786{
787 int ret;
788
789 if (will_write_block(port)) {
790 if (nonblock)
791 return -EAGAIN;
792
793 ret = wait_event_freezable(port->waitqueue,
794 !will_write_block(port));
795 if (ret < 0)
796 return ret;
797 }
798 /* Port got hot-unplugged. */
799 if (!port->guest_connected)
800 return -ENODEV;
801
802 return 0;
803}
804
Amit Shah2030fa42009-12-21 21:49:30 +0530805static ssize_t port_fops_write(struct file *filp, const char __user *ubuf,
806 size_t count, loff_t *offp)
807{
808 struct port *port;
Sjur Brændeland276a3e952012-12-14 13:46:42 +1030809 struct port_buffer *buf;
Amit Shah2030fa42009-12-21 21:49:30 +0530810 ssize_t ret;
Amit Shahcdfadfc2010-05-19 22:15:50 -0600811 bool nonblock;
Sjur Brændeland276a3e952012-12-14 13:46:42 +1030812 struct scatterlist sg[1];
Amit Shah2030fa42009-12-21 21:49:30 +0530813
Amit Shah65745422010-09-14 13:26:16 +0530814 /* Userspace could be out to fool us */
815 if (!count)
816 return 0;
817
Amit Shah2030fa42009-12-21 21:49:30 +0530818 port = filp->private_data;
819
Amit Shahcdfadfc2010-05-19 22:15:50 -0600820 nonblock = filp->f_flags & O_NONBLOCK;
821
Masami Hiramatsuefe75d22012-08-09 21:31:00 +0900822 ret = wait_port_writable(port, nonblock);
823 if (ret < 0)
824 return ret;
Amit Shahcdfadfc2010-05-19 22:15:50 -0600825
Amit Shah2030fa42009-12-21 21:49:30 +0530826 count = min((size_t)(32 * 1024), count);
827
Michael S. Tsirkin2855b332018-04-20 19:54:23 +0300828 buf = alloc_buf(port->portdev->vdev, count, 0);
Amit Shah2030fa42009-12-21 21:49:30 +0530829 if (!buf)
830 return -ENOMEM;
831
Sjur Brændeland276a3e952012-12-14 13:46:42 +1030832 ret = copy_from_user(buf->buf, ubuf, count);
Amit Shah2030fa42009-12-21 21:49:30 +0530833 if (ret) {
834 ret = -EFAULT;
835 goto free_buf;
836 }
837
Amit Shah531295e62010-10-20 13:45:43 +1030838 /*
839 * We now ask send_buf() to not spin for generic ports -- we
840 * can re-use the same code path that non-blocking file
841 * descriptors take for blocking file descriptors since the
842 * wait is already done and we're certain the write will go
843 * through to the host.
844 */
845 nonblock = true;
Sjur Brændeland276a3e952012-12-14 13:46:42 +1030846 sg_init_one(sg, buf->buf, count);
847 ret = __send_to_port(port, sg, 1, count, buf, nonblock);
Amit Shahcdfadfc2010-05-19 22:15:50 -0600848
849 if (nonblock && ret > 0)
850 goto out;
851
Amit Shah2030fa42009-12-21 21:49:30 +0530852free_buf:
Sjur Brændeland1b637042012-12-14 14:40:51 +1030853 free_buf(buf, true);
Amit Shahcdfadfc2010-05-19 22:15:50 -0600854out:
Amit Shah2030fa42009-12-21 21:49:30 +0530855 return ret;
856}
857
Masami Hiramatsueb5e89f2012-08-09 21:30:39 +0900858struct sg_list {
859 unsigned int n;
Masami Hiramatsu8ca84a52012-08-09 21:31:20 +0900860 unsigned int size;
Masami Hiramatsueb5e89f2012-08-09 21:30:39 +0900861 size_t len;
862 struct scatterlist *sg;
863};
864
865static int pipe_to_sg(struct pipe_inode_info *pipe, struct pipe_buffer *buf,
866 struct splice_desc *sd)
867{
868 struct sg_list *sgl = sd->u.data;
Masami Hiramatsuec8fc872012-08-09 21:30:50 +0900869 unsigned int offset, len;
Masami Hiramatsueb5e89f2012-08-09 21:30:39 +0900870
Masami Hiramatsu8ca84a52012-08-09 21:31:20 +0900871 if (sgl->n == sgl->size)
Masami Hiramatsueb5e89f2012-08-09 21:30:39 +0900872 return 0;
873
874 /* Try lock this page */
Christoph Hellwigc928f642020-05-20 17:58:16 +0200875 if (pipe_buf_try_steal(pipe, buf)) {
Masami Hiramatsueb5e89f2012-08-09 21:30:39 +0900876 /* Get reference and unlock page for moving */
877 get_page(buf->page);
878 unlock_page(buf->page);
879
880 len = min(buf->len, sd->len);
881 sg_set_page(&(sgl->sg[sgl->n]), buf->page, len, buf->offset);
Masami Hiramatsuec8fc872012-08-09 21:30:50 +0900882 } else {
883 /* Failback to copying a page */
884 struct page *page = alloc_page(GFP_KERNEL);
Al Viroc9efe512014-02-02 07:05:05 -0500885 char *src;
Masami Hiramatsuec8fc872012-08-09 21:30:50 +0900886
887 if (!page)
888 return -ENOMEM;
Masami Hiramatsuec8fc872012-08-09 21:30:50 +0900889
890 offset = sd->pos & ~PAGE_MASK;
891
892 len = sd->len;
893 if (len + offset > PAGE_SIZE)
894 len = PAGE_SIZE - offset;
895
Al Virofbb32752014-02-02 21:09:54 -0500896 src = kmap_atomic(buf->page);
Al Viroc9efe512014-02-02 07:05:05 -0500897 memcpy(page_address(page) + offset, src + buf->offset, len);
Al Virofbb32752014-02-02 21:09:54 -0500898 kunmap_atomic(src);
Masami Hiramatsuec8fc872012-08-09 21:30:50 +0900899
900 sg_set_page(&(sgl->sg[sgl->n]), page, len, offset);
Masami Hiramatsueb5e89f2012-08-09 21:30:39 +0900901 }
Masami Hiramatsuec8fc872012-08-09 21:30:50 +0900902 sgl->n++;
903 sgl->len += len;
Masami Hiramatsueb5e89f2012-08-09 21:30:39 +0900904
905 return len;
906}
907
908/* Faster zero-copy write by splicing */
909static ssize_t port_fops_splice_write(struct pipe_inode_info *pipe,
910 struct file *filp, loff_t *ppos,
911 size_t len, unsigned int flags)
912{
913 struct port *port = filp->private_data;
914 struct sg_list sgl;
915 ssize_t ret;
Sjur Brændeland276a3e952012-12-14 13:46:42 +1030916 struct port_buffer *buf;
Masami Hiramatsueb5e89f2012-08-09 21:30:39 +0900917 struct splice_desc sd = {
918 .total_len = len,
919 .flags = flags,
920 .pos = *ppos,
921 .u.data = &sgl,
922 };
David Howells8cefc102019-11-15 13:30:32 +0000923 unsigned int occupancy;
Masami Hiramatsueb5e89f2012-08-09 21:30:39 +0900924
Sjur Brændeland1b637042012-12-14 14:40:51 +1030925 /*
926 * Rproc_serial does not yet support splice. To support splice
927 * pipe_to_sg() must allocate dma-buffers and copy content from
928 * regular pages to dma pages. And alloc_buf and free_buf must
929 * support allocating and freeing such a list of dma-buffers.
930 */
931 if (is_rproc_serial(port->out_vq->vdev))
932 return -EINVAL;
933
Yoshihiro YUNOMAE2b4fbf02013-07-23 11:30:49 +0930934 pipe_lock(pipe);
David Howells8cefc102019-11-15 13:30:32 +0000935 ret = 0;
936 if (pipe_empty(pipe->head, pipe->tail))
Yoshihiro YUNOMAE2b4fbf02013-07-23 11:30:49 +0930937 goto error_out;
Yoshihiro YUNOMAE68c034f2013-07-23 11:30:49 +0930938
Masami Hiramatsuefe75d22012-08-09 21:31:00 +0900939 ret = wait_port_writable(port, filp->f_flags & O_NONBLOCK);
940 if (ret < 0)
Yoshihiro YUNOMAE2b4fbf02013-07-23 11:30:49 +0930941 goto error_out;
Masami Hiramatsuefe75d22012-08-09 21:31:00 +0900942
David Howells8cefc102019-11-15 13:30:32 +0000943 occupancy = pipe_occupancy(pipe->head, pipe->tail);
944 buf = alloc_buf(port->portdev->vdev, 0, occupancy);
Linus Torvalds6a965662019-11-30 14:12:13 -0800945
Yoshihiro YUNOMAE2b4fbf02013-07-23 11:30:49 +0930946 if (!buf) {
947 ret = -ENOMEM;
948 goto error_out;
949 }
Sjur Brændeland276a3e952012-12-14 13:46:42 +1030950
Masami Hiramatsueb5e89f2012-08-09 21:30:39 +0900951 sgl.n = 0;
952 sgl.len = 0;
David Howells8cefc102019-11-15 13:30:32 +0000953 sgl.size = occupancy;
Sjur Brændeland276a3e952012-12-14 13:46:42 +1030954 sgl.sg = buf->sg;
Masami Hiramatsu8ca84a52012-08-09 21:31:20 +0900955 sg_init_table(sgl.sg, sgl.size);
Masami Hiramatsueb5e89f2012-08-09 21:30:39 +0900956 ret = __splice_from_pipe(pipe, &sd, pipe_to_sg);
Yoshihiro YUNOMAE2b4fbf02013-07-23 11:30:49 +0930957 pipe_unlock(pipe);
Masami Hiramatsueb5e89f2012-08-09 21:30:39 +0900958 if (likely(ret > 0))
Sjur Brændeland276a3e952012-12-14 13:46:42 +1030959 ret = __send_to_port(port, buf->sg, sgl.n, sgl.len, buf, true);
Masami Hiramatsueb5e89f2012-08-09 21:30:39 +0900960
Sjur Brændelandfe529532012-10-15 09:57:33 +0200961 if (unlikely(ret <= 0))
Sjur Brændeland1b637042012-12-14 14:40:51 +1030962 free_buf(buf, true);
Masami Hiramatsueb5e89f2012-08-09 21:30:39 +0900963 return ret;
Yoshihiro YUNOMAE2b4fbf02013-07-23 11:30:49 +0930964
965error_out:
966 pipe_unlock(pipe);
967 return ret;
Masami Hiramatsueb5e89f2012-08-09 21:30:39 +0900968}
969
Al Viroafc9a422017-07-03 06:39:46 -0400970static __poll_t port_fops_poll(struct file *filp, poll_table *wait)
Amit Shah2030fa42009-12-21 21:49:30 +0530971{
972 struct port *port;
Al Viroafc9a422017-07-03 06:39:46 -0400973 __poll_t ret;
Amit Shah2030fa42009-12-21 21:49:30 +0530974
975 port = filp->private_data;
976 poll_wait(filp, &port->waitqueue, wait);
977
Amit Shah8529a502010-09-02 18:11:44 +0530978 if (!port->guest_connected) {
979 /* Port got unplugged */
Linus Torvaldsa9a08842018-02-11 14:34:03 -0800980 return EPOLLHUP;
Amit Shah8529a502010-09-02 18:11:44 +0530981 }
Amit Shah2030fa42009-12-21 21:49:30 +0530982 ret = 0;
Hans de Goede6df7aad2010-09-16 14:43:08 +0530983 if (!will_read_block(port))
Linus Torvaldsa9a08842018-02-11 14:34:03 -0800984 ret |= EPOLLIN | EPOLLRDNORM;
Amit Shahcdfadfc2010-05-19 22:15:50 -0600985 if (!will_write_block(port))
Linus Torvaldsa9a08842018-02-11 14:34:03 -0800986 ret |= EPOLLOUT;
Amit Shah2030fa42009-12-21 21:49:30 +0530987 if (!port->host_connected)
Linus Torvaldsa9a08842018-02-11 14:34:03 -0800988 ret |= EPOLLHUP;
Amit Shah2030fa42009-12-21 21:49:30 +0530989
990 return ret;
991}
992
Amit Shahb353a6b2010-09-02 18:38:29 +0530993static void remove_port(struct kref *kref);
994
Amit Shah2030fa42009-12-21 21:49:30 +0530995static int port_fops_release(struct inode *inode, struct file *filp)
996{
997 struct port *port;
998
999 port = filp->private_data;
1000
1001 /* Notify host of port being closed */
1002 send_control_msg(port, VIRTIO_CONSOLE_PORT_OPEN, 0);
1003
Amit Shah88f251a2009-12-21 22:15:30 +05301004 spin_lock_irq(&port->inbuf_lock);
Amit Shah3c7969c2009-11-26 11:25:38 +05301005 port->guest_connected = false;
1006
Amit Shah88f251a2009-12-21 22:15:30 +05301007 discard_port_data(port);
1008
1009 spin_unlock_irq(&port->inbuf_lock);
1010
Amit Shahcdfadfc2010-05-19 22:15:50 -06001011 spin_lock_irq(&port->outvq_lock);
1012 reclaim_consumed_buffers(port);
1013 spin_unlock_irq(&port->outvq_lock);
1014
Sjur Brændeland1b637042012-12-14 14:40:51 +10301015 reclaim_dma_bufs();
Amit Shahb353a6b2010-09-02 18:38:29 +05301016 /*
1017 * Locks aren't necessary here as a port can't be opened after
1018 * unplug, and if a port isn't unplugged, a kref would already
1019 * exist for the port. Plus, taking ports_lock here would
1020 * create a dependency on other locks taken by functions
1021 * inside remove_port if we're the last holder of the port,
1022 * creating many problems.
1023 */
1024 kref_put(&port->kref, remove_port);
1025
Amit Shah2030fa42009-12-21 21:49:30 +05301026 return 0;
1027}
1028
1029static int port_fops_open(struct inode *inode, struct file *filp)
1030{
1031 struct cdev *cdev = inode->i_cdev;
1032 struct port *port;
Amit Shah8ad37e82010-09-02 18:11:48 +05301033 int ret;
Amit Shah2030fa42009-12-21 21:49:30 +05301034
Amit Shah057b82b2013-07-29 14:16:13 +09301035 /* We get the port with a kref here */
Amit Shah04950cd2010-09-02 18:20:58 +05301036 port = find_port_by_devt(cdev->dev);
Amit Shah671bdea2013-07-29 14:17:13 +09301037 if (!port) {
1038 /* Port was unplugged before we could proceed */
1039 return -ENXIO;
1040 }
Amit Shah2030fa42009-12-21 21:49:30 +05301041 filp->private_data = port;
1042
1043 /*
1044 * Don't allow opening of console port devices -- that's done
1045 * via /dev/hvc
1046 */
Amit Shah8ad37e82010-09-02 18:11:48 +05301047 if (is_console_port(port)) {
1048 ret = -ENXIO;
1049 goto out;
1050 }
Amit Shah2030fa42009-12-21 21:49:30 +05301051
Amit Shah3c7969c2009-11-26 11:25:38 +05301052 /* Allow only one process to open a particular port at a time */
1053 spin_lock_irq(&port->inbuf_lock);
1054 if (port->guest_connected) {
1055 spin_unlock_irq(&port->inbuf_lock);
Amit Shah74ff5822013-04-15 12:00:15 +09301056 ret = -EBUSY;
Amit Shah8ad37e82010-09-02 18:11:48 +05301057 goto out;
Amit Shah3c7969c2009-11-26 11:25:38 +05301058 }
1059
1060 port->guest_connected = true;
1061 spin_unlock_irq(&port->inbuf_lock);
1062
Amit Shahcdfadfc2010-05-19 22:15:50 -06001063 spin_lock_irq(&port->outvq_lock);
1064 /*
1065 * There might be a chance that we missed reclaiming a few
1066 * buffers in the window of the port getting previously closed
1067 * and opening now.
1068 */
1069 reclaim_consumed_buffers(port);
1070 spin_unlock_irq(&port->outvq_lock);
1071
Amit Shah299fb612010-09-16 14:43:09 +05301072 nonseekable_open(inode, filp);
1073
Amit Shah2030fa42009-12-21 21:49:30 +05301074 /* Notify host of port being opened */
1075 send_control_msg(filp->private_data, VIRTIO_CONSOLE_PORT_OPEN, 1);
1076
1077 return 0;
Amit Shah8ad37e82010-09-02 18:11:48 +05301078out:
Amit Shahb353a6b2010-09-02 18:38:29 +05301079 kref_put(&port->kref, remove_port);
Amit Shah8ad37e82010-09-02 18:11:48 +05301080 return ret;
Amit Shah2030fa42009-12-21 21:49:30 +05301081}
1082
Amit Shah3eae0ad2010-09-02 18:47:52 +05301083static int port_fops_fasync(int fd, struct file *filp, int mode)
1084{
1085 struct port *port;
1086
1087 port = filp->private_data;
1088 return fasync_helper(fd, filp, mode, &port->async_queue);
1089}
1090
Amit Shah2030fa42009-12-21 21:49:30 +05301091/*
1092 * The file operations that we support: programs in the guest can open
1093 * a console device, read from it, write to it, poll for data and
1094 * close it. The devices are at
1095 * /dev/vport<device number>p<port number>
1096 */
1097static const struct file_operations port_fops = {
1098 .owner = THIS_MODULE,
1099 .open = port_fops_open,
1100 .read = port_fops_read,
1101 .write = port_fops_write,
Masami Hiramatsueb5e89f2012-08-09 21:30:39 +09001102 .splice_write = port_fops_splice_write,
Amit Shah2030fa42009-12-21 21:49:30 +05301103 .poll = port_fops_poll,
1104 .release = port_fops_release,
Amit Shah3eae0ad2010-09-02 18:47:52 +05301105 .fasync = port_fops_fasync,
Amit Shah299fb612010-09-16 14:43:09 +05301106 .llseek = no_llseek,
Amit Shah2030fa42009-12-21 21:49:30 +05301107};
1108
Amit Shahe27b5192010-01-18 19:15:02 +05301109/*
Rusty Russella23ea922010-01-18 19:14:55 +05301110 * The put_chars() callback is pretty straightforward.
Rusty Russell31610432007-10-22 11:03:39 +10001111 *
Rusty Russella23ea922010-01-18 19:14:55 +05301112 * We turn the characters into a scatter-gather list, add it to the
1113 * output queue and then kick the Host. Then we sit here waiting for
1114 * it to finish: inefficient in theory, but in practice
Juergen Grossecda85e2017-08-16 19:31:57 +02001115 * implementations will do it immediately.
Rusty Russella23ea922010-01-18 19:14:55 +05301116 */
Rusty Russell31610432007-10-22 11:03:39 +10001117static int put_chars(u32 vtermno, const char *buf, int count)
1118{
Rusty Russell21206ed2010-01-18 19:15:00 +05301119 struct port *port;
Sjur Brændeland276a3e952012-12-14 13:46:42 +10301120 struct scatterlist sg[1];
Omar Sandovalc4baad52017-02-01 00:02:27 -08001121 void *data;
1122 int ret;
Amit Shah38edf582010-01-18 19:15:05 +05301123
François Diakhaté162a6892010-03-23 18:23:15 +05301124 if (unlikely(early_put_chars))
1125 return early_put_chars(vtermno, buf, count);
1126
Amit Shah38edf582010-01-18 19:15:05 +05301127 port = find_port_by_vtermno(vtermno);
1128 if (!port)
Amit Shah6dc69f972010-05-19 22:15:47 -06001129 return -EPIPE;
Rusty Russell31610432007-10-22 11:03:39 +10001130
Omar Sandovalc4baad52017-02-01 00:02:27 -08001131 data = kmemdup(buf, count, GFP_ATOMIC);
1132 if (!data)
1133 return -ENOMEM;
1134
1135 sg_init_one(sg, data, count);
1136 ret = __send_to_port(port, sg, 1, count, data, false);
1137 kfree(data);
1138 return ret;
Rusty Russell31610432007-10-22 11:03:39 +10001139}
1140
Rusty Russella23ea922010-01-18 19:14:55 +05301141/*
Rusty Russella23ea922010-01-18 19:14:55 +05301142 * get_chars() is the callback from the hvc_console infrastructure
1143 * when an interrupt is received.
Rusty Russell31610432007-10-22 11:03:39 +10001144 *
Amit Shah203baab2010-01-18 19:15:12 +05301145 * We call out to fill_readbuf that gets us the required data from the
1146 * buffers that are queued up.
Rusty Russella23ea922010-01-18 19:14:55 +05301147 */
Rusty Russell31610432007-10-22 11:03:39 +10001148static int get_chars(u32 vtermno, char *buf, int count)
1149{
Rusty Russell21206ed2010-01-18 19:15:00 +05301150 struct port *port;
Rusty Russell31610432007-10-22 11:03:39 +10001151
Amit Shah6dc69f972010-05-19 22:15:47 -06001152 /* If we've not set up the port yet, we have no input to give. */
1153 if (unlikely(early_put_chars))
1154 return 0;
1155
Amit Shah38edf582010-01-18 19:15:05 +05301156 port = find_port_by_vtermno(vtermno);
1157 if (!port)
Amit Shah6dc69f972010-05-19 22:15:47 -06001158 return -EPIPE;
Rusty Russell21206ed2010-01-18 19:15:00 +05301159
1160 /* If we don't have an input queue yet, we can't get input. */
1161 BUG_ON(!port->in_vq);
1162
Michael S. Tsirkin48b36062014-12-01 13:31:45 +02001163 return fill_readbuf(port, (__force char __user *)buf, count, false);
Rusty Russell31610432007-10-22 11:03:39 +10001164}
Rusty Russell31610432007-10-22 11:03:39 +10001165
Amit Shahcb06e362010-01-18 19:15:08 +05301166static void resize_console(struct port *port)
Christian Borntraegerc2983452008-11-25 13:36:26 +01001167{
Amit Shahcb06e362010-01-18 19:15:08 +05301168 struct virtio_device *vdev;
Christian Borntraegerc2983452008-11-25 13:36:26 +01001169
Amit Shah2de16a42010-03-19 17:36:44 +05301170 /* The port could have been hot-unplugged */
Amit Shah97788292010-05-06 02:05:08 +05301171 if (!port || !is_console_port(port))
Amit Shah2de16a42010-03-19 17:36:44 +05301172 return;
1173
Amit Shahcb06e362010-01-18 19:15:08 +05301174 vdev = port->portdev->vdev;
Sjur Brændeland1b637042012-12-14 14:40:51 +10301175
1176 /* Don't test F_SIZE at all if we're rproc: not a valid feature! */
1177 if (!is_rproc_serial(vdev) &&
1178 virtio_has_feature(vdev, VIRTIO_CONSOLE_F_SIZE))
Amit Shah97788292010-05-06 02:05:08 +05301179 hvc_resize(port->cons.hvc, port->cons.ws);
Christian Borntraegerc2983452008-11-25 13:36:26 +01001180}
1181
Amit Shah38edf582010-01-18 19:15:05 +05301182/* We set the configuration at this point, since we now have a tty */
Christian Borntraeger91fcad12008-06-20 15:24:15 +02001183static int notifier_add_vio(struct hvc_struct *hp, int data)
1184{
Amit Shah38edf582010-01-18 19:15:05 +05301185 struct port *port;
1186
1187 port = find_port_by_vtermno(hp->vtermno);
1188 if (!port)
1189 return -EINVAL;
1190
Christian Borntraeger91fcad12008-06-20 15:24:15 +02001191 hp->irq_requested = 1;
Amit Shahcb06e362010-01-18 19:15:08 +05301192 resize_console(port);
Christian Borntraegerc2983452008-11-25 13:36:26 +01001193
Christian Borntraeger91fcad12008-06-20 15:24:15 +02001194 return 0;
1195}
1196
1197static void notifier_del_vio(struct hvc_struct *hp, int data)
1198{
1199 hp->irq_requested = 0;
1200}
1201
Amit Shah17634ba2009-12-21 21:03:25 +05301202/* The operations for console ports. */
Rusty Russell1dff3992009-11-28 12:20:26 +05301203static const struct hv_ops hv_ops = {
Rusty Russell971f3392010-01-18 19:14:56 +05301204 .get_chars = get_chars,
1205 .put_chars = put_chars,
1206 .notifier_add = notifier_add_vio,
1207 .notifier_del = notifier_del_vio,
1208 .notifier_hangup = notifier_del_vio,
1209};
1210
1211/*
1212 * Console drivers are initialized very early so boot messages can go
1213 * out, so we do things slightly differently from the generic virtio
1214 * initialization of the net and block drivers.
1215 *
1216 * At this stage, the console is output-only. It's too early to set
1217 * up a virtqueue, so we let the drivers do some boutique early-output
1218 * thing.
1219 */
1220int __init virtio_cons_early_init(int (*put_chars)(u32, const char *, int))
1221{
1222 early_put_chars = put_chars;
1223 return hvc_instantiate(0, 0, &hv_ops);
1224}
1225
Wei Yongjun38268352013-04-08 16:13:59 +09301226static int init_port_console(struct port *port)
Amit Shahcfa6d372010-01-18 19:15:10 +05301227{
1228 int ret;
1229
1230 /*
1231 * The Host's telling us this port is a console port. Hook it
1232 * up with an hvc console.
1233 *
1234 * To set up and manage our virtual console, we call
1235 * hvc_alloc().
1236 *
1237 * The first argument of hvc_alloc() is the virtual console
1238 * number. The second argument is the parameter for the
1239 * notification mechanism (like irq number). We currently
1240 * leave this as zero, virtqueues have implicit notifications.
1241 *
1242 * The third argument is a "struct hv_ops" containing the
1243 * put_chars() get_chars(), notifier_add() and notifier_del()
1244 * pointers. The final argument is the output buffer size: we
1245 * can do any size, so we put PAGE_SIZE here.
1246 */
1247 port->cons.vtermno = pdrvdata.next_vtermno;
1248
1249 port->cons.hvc = hvc_alloc(port->cons.vtermno, 0, &hv_ops, PAGE_SIZE);
1250 if (IS_ERR(port->cons.hvc)) {
1251 ret = PTR_ERR(port->cons.hvc);
Amit Shah298add72010-01-18 16:35:23 +05301252 dev_err(port->dev,
1253 "error %d allocating hvc for port\n", ret);
Amit Shahcfa6d372010-01-18 19:15:10 +05301254 port->cons.hvc = NULL;
1255 return ret;
1256 }
1257 spin_lock_irq(&pdrvdata_lock);
1258 pdrvdata.next_vtermno++;
1259 list_add_tail(&port->cons.list, &pdrvdata.consoles);
1260 spin_unlock_irq(&pdrvdata_lock);
Amit Shah3c7969c2009-11-26 11:25:38 +05301261 port->guest_connected = true;
Amit Shahcfa6d372010-01-18 19:15:10 +05301262
Amit Shah1d051602010-05-19 22:15:49 -06001263 /*
1264 * Start using the new console output if this is the first
1265 * console to come up.
1266 */
1267 if (early_put_chars)
1268 early_put_chars = NULL;
1269
Amit Shah2030fa42009-12-21 21:49:30 +05301270 /* Notify host of port being opened */
1271 send_control_msg(port, VIRTIO_CONSOLE_PORT_OPEN, 1);
1272
Amit Shahcfa6d372010-01-18 19:15:10 +05301273 return 0;
1274}
1275
Amit Shah431edb82009-12-21 21:57:40 +05301276static ssize_t show_port_name(struct device *dev,
1277 struct device_attribute *attr, char *buffer)
1278{
1279 struct port *port;
1280
1281 port = dev_get_drvdata(dev);
1282
1283 return sprintf(buffer, "%s\n", port->name);
1284}
1285
1286static DEVICE_ATTR(name, S_IRUGO, show_port_name, NULL);
1287
1288static struct attribute *port_sysfs_entries[] = {
1289 &dev_attr_name.attr,
1290 NULL
1291};
1292
Arvind Yadavac317e22017-08-02 16:51:08 +05301293static const struct attribute_group port_attribute_group = {
Amit Shah431edb82009-12-21 21:57:40 +05301294 .name = NULL, /* put in device directory */
1295 .attrs = port_sysfs_entries,
1296};
1297
Yangtao Liddfa7282018-11-30 21:41:28 -05001298static int port_debugfs_show(struct seq_file *s, void *data)
Amit Shahd99393e2009-12-21 22:36:21 +05301299{
Tuomas Tynkkynen8d62fe92018-07-13 00:39:56 +03001300 struct port *port = s->private;
Amit Shahd99393e2009-12-21 22:36:21 +05301301
Tuomas Tynkkynen8d62fe92018-07-13 00:39:56 +03001302 seq_printf(s, "name: %s\n", port->name ? port->name : "");
1303 seq_printf(s, "guest_connected: %d\n", port->guest_connected);
1304 seq_printf(s, "host_connected: %d\n", port->host_connected);
1305 seq_printf(s, "outvq_full: %d\n", port->outvq_full);
1306 seq_printf(s, "bytes_sent: %lu\n", port->stats.bytes_sent);
1307 seq_printf(s, "bytes_received: %lu\n", port->stats.bytes_received);
1308 seq_printf(s, "bytes_discarded: %lu\n", port->stats.bytes_discarded);
1309 seq_printf(s, "is_console: %s\n",
1310 is_console_port(port) ? "yes" : "no");
1311 seq_printf(s, "console_vtermno: %u\n", port->cons.vtermno);
Amit Shahd99393e2009-12-21 22:36:21 +05301312
Tuomas Tynkkynen8d62fe92018-07-13 00:39:56 +03001313 return 0;
1314}
Amit Shahd99393e2009-12-21 22:36:21 +05301315
Yangtao Liddfa7282018-11-30 21:41:28 -05001316DEFINE_SHOW_ATTRIBUTE(port_debugfs);
Amit Shahd99393e2009-12-21 22:36:21 +05301317
Amit Shah97788292010-05-06 02:05:08 +05301318static void set_console_size(struct port *port, u16 rows, u16 cols)
1319{
1320 if (!port || !is_console_port(port))
1321 return;
1322
1323 port->cons.ws.ws_row = rows;
1324 port->cons.ws.ws_col = cols;
1325}
1326
Laurent Vivierd791cfc2019-11-14 13:25:48 +01001327static int fill_queue(struct virtqueue *vq, spinlock_t *lock)
Amit Shahc446f8f2010-05-19 22:15:48 -06001328{
1329 struct port_buffer *buf;
Laurent Vivierd791cfc2019-11-14 13:25:48 +01001330 int nr_added_bufs;
Amit Shahc446f8f2010-05-19 22:15:48 -06001331 int ret;
1332
1333 nr_added_bufs = 0;
1334 do {
Michael S. Tsirkin2855b332018-04-20 19:54:23 +03001335 buf = alloc_buf(vq->vdev, PAGE_SIZE, 0);
Amit Shahc446f8f2010-05-19 22:15:48 -06001336 if (!buf)
Laurent Vivierd791cfc2019-11-14 13:25:48 +01001337 return -ENOMEM;
Amit Shahc446f8f2010-05-19 22:15:48 -06001338
1339 spin_lock_irq(lock);
1340 ret = add_inbuf(vq, buf);
1341 if (ret < 0) {
1342 spin_unlock_irq(lock);
Sjur Brændeland1b637042012-12-14 14:40:51 +10301343 free_buf(buf, true);
Laurent Vivierd791cfc2019-11-14 13:25:48 +01001344 return ret;
Amit Shahc446f8f2010-05-19 22:15:48 -06001345 }
1346 nr_added_bufs++;
1347 spin_unlock_irq(lock);
1348 } while (ret > 0);
1349
1350 return nr_added_bufs;
1351}
1352
Amit Shah3eae0ad2010-09-02 18:47:52 +05301353static void send_sigio_to_port(struct port *port)
1354{
1355 if (port->async_queue && port->guest_connected)
1356 kill_fasync(&port->async_queue, SIGIO, POLL_OUT);
1357}
1358
Amit Shahc446f8f2010-05-19 22:15:48 -06001359static int add_port(struct ports_device *portdev, u32 id)
1360{
1361 char debugfs_name[16];
1362 struct port *port;
Amit Shahc446f8f2010-05-19 22:15:48 -06001363 dev_t devt;
Amit Shahc446f8f2010-05-19 22:15:48 -06001364 int err;
1365
1366 port = kmalloc(sizeof(*port), GFP_KERNEL);
1367 if (!port) {
1368 err = -ENOMEM;
1369 goto fail;
1370 }
Amit Shahb353a6b2010-09-02 18:38:29 +05301371 kref_init(&port->kref);
Amit Shahc446f8f2010-05-19 22:15:48 -06001372
1373 port->portdev = portdev;
1374 port->id = id;
1375
1376 port->name = NULL;
1377 port->inbuf = NULL;
1378 port->cons.hvc = NULL;
Amit Shah3eae0ad2010-09-02 18:47:52 +05301379 port->async_queue = NULL;
Amit Shahc446f8f2010-05-19 22:15:48 -06001380
Amit Shah97788292010-05-06 02:05:08 +05301381 port->cons.ws.ws_row = port->cons.ws.ws_col = 0;
Pankaj Gupta4b0a2c52019-03-19 11:34:06 +05301382 port->cons.vtermno = 0;
Amit Shah97788292010-05-06 02:05:08 +05301383
Amit Shahc446f8f2010-05-19 22:15:48 -06001384 port->host_connected = port->guest_connected = false;
Amit Shah17e5b4f2011-09-14 13:06:46 +05301385 port->stats = (struct port_stats) { 0 };
Amit Shahc446f8f2010-05-19 22:15:48 -06001386
Amit Shahcdfadfc2010-05-19 22:15:50 -06001387 port->outvq_full = false;
1388
Amit Shahc446f8f2010-05-19 22:15:48 -06001389 port->in_vq = portdev->in_vqs[port->id];
1390 port->out_vq = portdev->out_vqs[port->id];
1391
Amit Shahd22a6982010-09-02 18:20:59 +05301392 port->cdev = cdev_alloc();
1393 if (!port->cdev) {
1394 dev_err(&port->portdev->vdev->dev, "Error allocating cdev\n");
1395 err = -ENOMEM;
1396 goto free_port;
1397 }
1398 port->cdev->ops = &port_fops;
Amit Shahc446f8f2010-05-19 22:15:48 -06001399
1400 devt = MKDEV(portdev->chr_major, id);
Amit Shahd22a6982010-09-02 18:20:59 +05301401 err = cdev_add(port->cdev, devt, 1);
Amit Shahc446f8f2010-05-19 22:15:48 -06001402 if (err < 0) {
1403 dev_err(&port->portdev->vdev->dev,
1404 "Error %d adding cdev for port %u\n", err, id);
Amit Shahd22a6982010-09-02 18:20:59 +05301405 goto free_cdev;
Amit Shahc446f8f2010-05-19 22:15:48 -06001406 }
1407 port->dev = device_create(pdrvdata.class, &port->portdev->vdev->dev,
1408 devt, port, "vport%up%u",
Sjur Brændelanddc18f082013-02-12 16:24:59 +10301409 port->portdev->vdev->index, id);
Amit Shahc446f8f2010-05-19 22:15:48 -06001410 if (IS_ERR(port->dev)) {
1411 err = PTR_ERR(port->dev);
1412 dev_err(&port->portdev->vdev->dev,
1413 "Error %d creating device for port %u\n",
1414 err, id);
1415 goto free_cdev;
1416 }
1417
1418 spin_lock_init(&port->inbuf_lock);
Amit Shahcdfadfc2010-05-19 22:15:50 -06001419 spin_lock_init(&port->outvq_lock);
Amit Shahc446f8f2010-05-19 22:15:48 -06001420 init_waitqueue_head(&port->waitqueue);
1421
Laurent Vivierd791cfc2019-11-14 13:25:48 +01001422 /* We can safely ignore ENOSPC because it means
1423 * the queue already has buffers. Buffers are removed
1424 * only by virtcons_remove(), not by unplug_port()
1425 */
1426 err = fill_queue(port->in_vq, &port->inbuf_lock);
1427 if (err < 0 && err != -ENOSPC) {
Amit Shahc446f8f2010-05-19 22:15:48 -06001428 dev_err(port->dev, "Error allocating inbufs\n");
Amit Shahc446f8f2010-05-19 22:15:48 -06001429 goto free_device;
1430 }
1431
Sjur Brændeland1b637042012-12-14 14:40:51 +10301432 if (is_rproc_serial(port->portdev->vdev))
1433 /*
1434 * For rproc_serial assume remote processor is connected.
1435 * rproc_serial does not want the console port, only
1436 * the generic port implementation.
1437 */
Sjur Brændelandaabd6a82013-03-18 19:19:14 +10301438 port->host_connected = true;
Sjur Brændeland1b637042012-12-14 14:40:51 +10301439 else if (!use_multiport(port->portdev)) {
1440 /*
1441 * If we're not using multiport support,
1442 * this has to be a console port.
1443 */
Amit Shahc446f8f2010-05-19 22:15:48 -06001444 err = init_port_console(port);
1445 if (err)
1446 goto free_inbufs;
1447 }
1448
1449 spin_lock_irq(&portdev->ports_lock);
1450 list_add_tail(&port->list, &port->portdev->ports);
1451 spin_unlock_irq(&portdev->ports_lock);
1452
1453 /*
1454 * Tell the Host we're set so that it can send us various
1455 * configuration parameters for this port (eg, port name,
1456 * caching, whether this is a console port, etc.)
1457 */
1458 send_control_msg(port, VIRTIO_CONSOLE_PORT_READY, 1);
1459
Greg Kroah-Hartmanfb11de92021-02-16 16:04:10 +01001460 /*
1461 * Finally, create the debugfs file that we can use to
1462 * inspect a port's state at any time
1463 */
1464 snprintf(debugfs_name, sizeof(debugfs_name), "vport%up%u",
1465 port->portdev->vdev->index, id);
1466 port->debugfs_file = debugfs_create_file(debugfs_name, 0444,
1467 pdrvdata.debugfs_dir,
1468 port, &port_debugfs_fops);
Amit Shahc446f8f2010-05-19 22:15:48 -06001469 return 0;
1470
1471free_inbufs:
Amit Shahc446f8f2010-05-19 22:15:48 -06001472free_device:
1473 device_destroy(pdrvdata.class, port->dev->devt);
1474free_cdev:
Amit Shahd22a6982010-09-02 18:20:59 +05301475 cdev_del(port->cdev);
Amit Shahc446f8f2010-05-19 22:15:48 -06001476free_port:
1477 kfree(port);
1478fail:
1479 /* The host might want to notify management sw about port add failure */
Julia Lawall0643e4c2010-05-15 11:45:53 +02001480 __send_control_msg(portdev, id, VIRTIO_CONSOLE_PORT_READY, 0);
Amit Shahc446f8f2010-05-19 22:15:48 -06001481 return err;
1482}
1483
Amit Shahb353a6b2010-09-02 18:38:29 +05301484/* No users remain, remove all port-specific data. */
1485static void remove_port(struct kref *kref)
1486{
1487 struct port *port;
1488
1489 port = container_of(kref, struct port, kref);
1490
Amit Shahb353a6b2010-09-02 18:38:29 +05301491 kfree(port);
1492}
1493
Amit Shaha0e2dbf2011-12-22 16:58:27 +05301494static void remove_port_data(struct port *port)
1495{
Amit Shahc6017e72013-07-29 14:23:46 +09301496 spin_lock_irq(&port->inbuf_lock);
Amit Shaha0e2dbf2011-12-22 16:58:27 +05301497 /* Remove unused data this port might have received. */
1498 discard_port_data(port);
Matt Redfearn34563762016-10-11 12:05:15 +01001499 spin_unlock_irq(&port->inbuf_lock);
Amit Shaha0e2dbf2011-12-22 16:58:27 +05301500
Amit Shahc6017e72013-07-29 14:23:46 +09301501 spin_lock_irq(&port->outvq_lock);
1502 reclaim_consumed_buffers(port);
Matt Redfearn34563762016-10-11 12:05:15 +01001503 spin_unlock_irq(&port->outvq_lock);
Amit Shaha0e2dbf2011-12-22 16:58:27 +05301504}
1505
Amit Shahb353a6b2010-09-02 18:38:29 +05301506/*
1507 * Port got unplugged. Remove port from portdev's list and drop the
1508 * kref reference. If no userspace has this port opened, it will
1509 * result in immediate removal the port.
1510 */
1511static void unplug_port(struct port *port)
Amit Shah1f7aa422009-12-21 22:27:31 +05301512{
Amit Shahb353a6b2010-09-02 18:38:29 +05301513 spin_lock_irq(&port->portdev->ports_lock);
1514 list_del(&port->list);
1515 spin_unlock_irq(&port->portdev->ports_lock);
1516
Amit Shah5549fb22013-07-29 14:24:15 +09301517 spin_lock_irq(&port->inbuf_lock);
Amit Shah00476342010-05-27 13:24:39 +05301518 if (port->guest_connected) {
Amit Shaha461e112010-09-02 18:47:54 +05301519 /* Let the app know the port is going down. */
1520 send_sigio_to_port(port);
Amit Shah92d34532013-07-29 14:21:32 +09301521
1522 /* Do this after sigio is actually sent */
1523 port->guest_connected = false;
1524 port->host_connected = false;
1525
1526 wake_up_interruptible(&port->waitqueue);
Amit Shah00476342010-05-27 13:24:39 +05301527 }
Amit Shah5549fb22013-07-29 14:24:15 +09301528 spin_unlock_irq(&port->inbuf_lock);
Amit Shah00476342010-05-27 13:24:39 +05301529
Amit Shah1f7aa422009-12-21 22:27:31 +05301530 if (is_console_port(port)) {
1531 spin_lock_irq(&pdrvdata_lock);
1532 list_del(&port->cons.list);
1533 spin_unlock_irq(&pdrvdata_lock);
1534 hvc_remove(port->cons.hvc);
1535 }
Amit Shah1f7aa422009-12-21 22:27:31 +05301536
Amit Shaha0e2dbf2011-12-22 16:58:27 +05301537 remove_port_data(port);
Amit Shaha9cdd482010-02-12 10:32:15 +05301538
Amit Shahb353a6b2010-09-02 18:38:29 +05301539 /*
1540 * We should just assume the device itself has gone off --
1541 * else a close on an open port later will try to send out a
1542 * control message.
1543 */
1544 port->portdev = NULL;
Amit Shah1f7aa422009-12-21 22:27:31 +05301545
Amit Shahea3768b2013-07-29 14:20:29 +09301546 sysfs_remove_group(&port->dev->kobj, &port_attribute_group);
1547 device_destroy(pdrvdata.class, port->dev->devt);
1548 cdev_del(port->cdev);
1549
Amit Shahea3768b2013-07-29 14:20:29 +09301550 debugfs_remove(port->debugfs_file);
Amit Shah3b868a42013-08-07 15:54:17 +09301551 kfree(port->name);
Amit Shahea3768b2013-07-29 14:20:29 +09301552
Amit Shahb353a6b2010-09-02 18:38:29 +05301553 /*
1554 * Locks around here are not necessary - a port can't be
1555 * opened after we removed the port struct from ports_list
1556 * above.
1557 */
1558 kref_put(&port->kref, remove_port);
Amit Shah1f7aa422009-12-21 22:27:31 +05301559}
1560
Amit Shah17634ba2009-12-21 21:03:25 +05301561/* Any private messages that the Host and Guest want to share */
Michael S. Tsirkin1f0f9102014-12-01 13:17:40 +02001562static void handle_control_message(struct virtio_device *vdev,
1563 struct ports_device *portdev,
Amit Shah17634ba2009-12-21 21:03:25 +05301564 struct port_buffer *buf)
1565{
1566 struct virtio_console_control *cpkt;
1567 struct port *port;
Amit Shah431edb82009-12-21 21:57:40 +05301568 size_t name_size;
1569 int err;
Amit Shah17634ba2009-12-21 21:03:25 +05301570
1571 cpkt = (struct virtio_console_control *)(buf->buf + buf->offset);
1572
Michael S. Tsirkin1f0f9102014-12-01 13:17:40 +02001573 port = find_port_by_id(portdev, virtio32_to_cpu(vdev, cpkt->id));
1574 if (!port &&
1575 cpkt->event != cpu_to_virtio16(vdev, VIRTIO_CONSOLE_PORT_ADD)) {
Amit Shah17634ba2009-12-21 21:03:25 +05301576 /* No valid header at start of buffer. Drop it. */
1577 dev_dbg(&portdev->vdev->dev,
1578 "Invalid index %u in control packet\n", cpkt->id);
1579 return;
1580 }
1581
Michael S. Tsirkin1f0f9102014-12-01 13:17:40 +02001582 switch (virtio16_to_cpu(vdev, cpkt->event)) {
Amit Shahf909f852010-05-19 22:15:48 -06001583 case VIRTIO_CONSOLE_PORT_ADD:
1584 if (port) {
Amit Shah1d051602010-05-19 22:15:49 -06001585 dev_dbg(&portdev->vdev->dev,
1586 "Port %u already added\n", port->id);
Amit Shahf909f852010-05-19 22:15:48 -06001587 send_control_msg(port, VIRTIO_CONSOLE_PORT_READY, 1);
1588 break;
1589 }
Michael S. Tsirkin1f0f9102014-12-01 13:17:40 +02001590 if (virtio32_to_cpu(vdev, cpkt->id) >=
Michael S. Tsirkin7328fa62016-12-05 21:39:42 +02001591 portdev->max_nr_ports) {
Amit Shahf909f852010-05-19 22:15:48 -06001592 dev_warn(&portdev->vdev->dev,
Michael S. Tsirkin1f0f9102014-12-01 13:17:40 +02001593 "Request for adding port with "
1594 "out-of-bound id %u, max. supported id: %u\n",
Michael S. Tsirkin7328fa62016-12-05 21:39:42 +02001595 cpkt->id, portdev->max_nr_ports - 1);
Amit Shahf909f852010-05-19 22:15:48 -06001596 break;
1597 }
Michael S. Tsirkin1f0f9102014-12-01 13:17:40 +02001598 add_port(portdev, virtio32_to_cpu(vdev, cpkt->id));
Amit Shahf909f852010-05-19 22:15:48 -06001599 break;
1600 case VIRTIO_CONSOLE_PORT_REMOVE:
Amit Shahb353a6b2010-09-02 18:38:29 +05301601 unplug_port(port);
Amit Shahf909f852010-05-19 22:15:48 -06001602 break;
Amit Shah17634ba2009-12-21 21:03:25 +05301603 case VIRTIO_CONSOLE_CONSOLE_PORT:
1604 if (!cpkt->value)
1605 break;
1606 if (is_console_port(port))
1607 break;
1608
1609 init_port_console(port);
Christian Borntraeger5e384832011-09-22 23:44:23 +05301610 complete(&early_console_added);
Amit Shah17634ba2009-12-21 21:03:25 +05301611 /*
1612 * Could remove the port here in case init fails - but
1613 * have to notify the host first.
1614 */
1615 break;
Amit Shah8345adb2010-05-06 02:05:09 +05301616 case VIRTIO_CONSOLE_RESIZE: {
1617 struct {
1618 __u16 rows;
1619 __u16 cols;
1620 } size;
1621
Amit Shah17634ba2009-12-21 21:03:25 +05301622 if (!is_console_port(port))
1623 break;
Amit Shah8345adb2010-05-06 02:05:09 +05301624
1625 memcpy(&size, buf->buf + buf->offset + sizeof(*cpkt),
1626 sizeof(size));
1627 set_console_size(port, size.rows, size.cols);
1628
Amit Shah17634ba2009-12-21 21:03:25 +05301629 port->cons.hvc->irq_requested = 1;
1630 resize_console(port);
1631 break;
Amit Shah8345adb2010-05-06 02:05:09 +05301632 }
Amit Shah2030fa42009-12-21 21:49:30 +05301633 case VIRTIO_CONSOLE_PORT_OPEN:
Michael S. Tsirkin1f0f9102014-12-01 13:17:40 +02001634 port->host_connected = virtio16_to_cpu(vdev, cpkt->value);
Amit Shah2030fa42009-12-21 21:49:30 +05301635 wake_up_interruptible(&port->waitqueue);
Amit Shahcdfadfc2010-05-19 22:15:50 -06001636 /*
1637 * If the host port got closed and the host had any
1638 * unconsumed buffers, we'll be able to reclaim them
1639 * now.
1640 */
1641 spin_lock_irq(&port->outvq_lock);
1642 reclaim_consumed_buffers(port);
1643 spin_unlock_irq(&port->outvq_lock);
Amit Shah3eae0ad2010-09-02 18:47:52 +05301644
1645 /*
1646 * If the guest is connected, it'll be interested in
1647 * knowing the host connection state changed.
1648 */
Amit Shah314081f2013-07-29 14:25:38 +09301649 spin_lock_irq(&port->inbuf_lock);
Amit Shah3eae0ad2010-09-02 18:47:52 +05301650 send_sigio_to_port(port);
Amit Shah314081f2013-07-29 14:25:38 +09301651 spin_unlock_irq(&port->inbuf_lock);
Amit Shah2030fa42009-12-21 21:49:30 +05301652 break;
Amit Shah431edb82009-12-21 21:57:40 +05301653 case VIRTIO_CONSOLE_PORT_NAME:
1654 /*
Amit Shah291024e2011-09-14 13:06:40 +05301655 * If we woke up after hibernation, we can get this
1656 * again. Skip it in that case.
1657 */
1658 if (port->name)
1659 break;
1660
1661 /*
Amit Shah431edb82009-12-21 21:57:40 +05301662 * Skip the size of the header and the cpkt to get the size
1663 * of the name that was sent
1664 */
1665 name_size = buf->len - buf->offset - sizeof(*cpkt) + 1;
1666
1667 port->name = kmalloc(name_size, GFP_KERNEL);
1668 if (!port->name) {
1669 dev_err(port->dev,
1670 "Not enough space to store port name\n");
1671 break;
1672 }
1673 strncpy(port->name, buf->buf + buf->offset + sizeof(*cpkt),
1674 name_size - 1);
1675 port->name[name_size - 1] = 0;
1676
1677 /*
1678 * Since we only have one sysfs attribute, 'name',
1679 * create it only if we have a name for the port.
1680 */
1681 err = sysfs_create_group(&port->dev->kobj,
1682 &port_attribute_group);
Amit Shahec642132010-03-19 17:36:43 +05301683 if (err) {
Amit Shah431edb82009-12-21 21:57:40 +05301684 dev_err(port->dev,
1685 "Error %d creating sysfs device attributes\n",
1686 err);
Amit Shahec642132010-03-19 17:36:43 +05301687 } else {
1688 /*
1689 * Generate a udev event so that appropriate
1690 * symlinks can be created based on udev
1691 * rules.
1692 */
1693 kobject_uevent(&port->dev->kobj, KOBJ_CHANGE);
1694 }
Amit Shah431edb82009-12-21 21:57:40 +05301695 break;
Amit Shah17634ba2009-12-21 21:03:25 +05301696 }
1697}
1698
1699static void control_work_handler(struct work_struct *work)
1700{
1701 struct ports_device *portdev;
1702 struct virtqueue *vq;
1703 struct port_buffer *buf;
1704 unsigned int len;
1705
1706 portdev = container_of(work, struct ports_device, control_work);
1707 vq = portdev->c_ivq;
1708
Amit Shah165b1b82013-03-29 16:30:07 +05301709 spin_lock(&portdev->c_ivq_lock);
Michael S. Tsirkin505b0452010-04-12 16:18:32 +03001710 while ((buf = virtqueue_get_buf(vq, &len))) {
Amit Shah165b1b82013-03-29 16:30:07 +05301711 spin_unlock(&portdev->c_ivq_lock);
Amit Shah17634ba2009-12-21 21:03:25 +05301712
Xie Yongjid00d8da2021-05-25 20:56:22 +08001713 buf->len = min_t(size_t, len, buf->size);
Amit Shah17634ba2009-12-21 21:03:25 +05301714 buf->offset = 0;
1715
Michael S. Tsirkin1f0f9102014-12-01 13:17:40 +02001716 handle_control_message(vq->vdev, portdev, buf);
Amit Shah17634ba2009-12-21 21:03:25 +05301717
Amit Shah165b1b82013-03-29 16:30:07 +05301718 spin_lock(&portdev->c_ivq_lock);
Amit Shah17634ba2009-12-21 21:03:25 +05301719 if (add_inbuf(portdev->c_ivq, buf) < 0) {
1720 dev_warn(&portdev->vdev->dev,
1721 "Error adding buffer to queue\n");
Sjur Brændeland1b637042012-12-14 14:40:51 +10301722 free_buf(buf, false);
Amit Shah17634ba2009-12-21 21:03:25 +05301723 }
1724 }
Amit Shah165b1b82013-03-29 16:30:07 +05301725 spin_unlock(&portdev->c_ivq_lock);
Amit Shah17634ba2009-12-21 21:03:25 +05301726}
1727
Michael S. Tsirkina7a69ec2018-04-20 20:24:23 +03001728static void flush_bufs(struct virtqueue *vq, bool can_sleep)
1729{
1730 struct port_buffer *buf;
1731 unsigned int len;
1732
1733 while ((buf = virtqueue_get_buf(vq, &len)))
1734 free_buf(buf, can_sleep);
1735}
1736
Amit Shah2770c5e2011-01-31 13:06:36 +05301737static void out_intr(struct virtqueue *vq)
1738{
1739 struct port *port;
1740
1741 port = find_port_by_vq(vq->vdev->priv, vq);
Michael S. Tsirkina7a69ec2018-04-20 20:24:23 +03001742 if (!port) {
1743 flush_bufs(vq, false);
Amit Shah2770c5e2011-01-31 13:06:36 +05301744 return;
Michael S. Tsirkina7a69ec2018-04-20 20:24:23 +03001745 }
Amit Shah2770c5e2011-01-31 13:06:36 +05301746
1747 wake_up_interruptible(&port->waitqueue);
1748}
1749
Amit Shah17634ba2009-12-21 21:03:25 +05301750static void in_intr(struct virtqueue *vq)
1751{
1752 struct port *port;
1753 unsigned long flags;
1754
1755 port = find_port_by_vq(vq->vdev->priv, vq);
Michael S. Tsirkina7a69ec2018-04-20 20:24:23 +03001756 if (!port) {
1757 flush_bufs(vq, false);
Amit Shah17634ba2009-12-21 21:03:25 +05301758 return;
Michael S. Tsirkina7a69ec2018-04-20 20:24:23 +03001759 }
Amit Shah17634ba2009-12-21 21:03:25 +05301760
1761 spin_lock_irqsave(&port->inbuf_lock, flags);
Amit Shahd25a9dd2011-09-14 13:06:43 +05301762 port->inbuf = get_inbuf(port);
Amit Shah17634ba2009-12-21 21:03:25 +05301763
Amit Shah88f251a2009-12-21 22:15:30 +05301764 /*
Sjur Brændelandaabd6a82013-03-18 19:19:14 +10301765 * Normally the port should not accept data when the port is
1766 * closed. For generic serial ports, the host won't (shouldn't)
1767 * send data till the guest is connected. But this condition
Amit Shah88f251a2009-12-21 22:15:30 +05301768 * can be reached when a console port is not yet connected (no
Sjur Brændelandaabd6a82013-03-18 19:19:14 +10301769 * tty is spawned) and the other side sends out data over the
1770 * vring, or when a remote devices start sending data before
1771 * the ports are opened.
1772 *
1773 * A generic serial port will discard data if not connected,
1774 * while console ports and rproc-serial ports accepts data at
1775 * any time. rproc-serial is initiated with guest_connected to
1776 * false because port_fops_open expects this. Console ports are
1777 * hooked up with an HVC console and is initialized with
1778 * guest_connected to true.
Amit Shah88f251a2009-12-21 22:15:30 +05301779 */
Sjur Brændelandaabd6a82013-03-18 19:19:14 +10301780
1781 if (!port->guest_connected && !is_rproc_serial(port->portdev->vdev))
Amit Shah88f251a2009-12-21 22:15:30 +05301782 discard_port_data(port);
1783
Amit Shah314081f2013-07-29 14:25:38 +09301784 /* Send a SIGIO indicating new data in case the process asked for it */
1785 send_sigio_to_port(port);
1786
Amit Shah17634ba2009-12-21 21:03:25 +05301787 spin_unlock_irqrestore(&port->inbuf_lock, flags);
1788
Amit Shah2030fa42009-12-21 21:49:30 +05301789 wake_up_interruptible(&port->waitqueue);
1790
Amit Shah17634ba2009-12-21 21:03:25 +05301791 if (is_console_port(port) && hvc_poll(port->cons.hvc))
1792 hvc_kick();
1793}
1794
1795static void control_intr(struct virtqueue *vq)
1796{
1797 struct ports_device *portdev;
1798
1799 portdev = vq->vdev->priv;
1800 schedule_work(&portdev->control_work);
1801}
1802
Amit Shah7f5d8102009-12-21 22:22:08 +05301803static void config_intr(struct virtio_device *vdev)
1804{
1805 struct ports_device *portdev;
1806
1807 portdev = vdev->priv;
Amit Shah99f905f2010-05-19 22:15:48 -06001808
Michael S. Tsirkineeb8a7e2015-03-05 10:45:49 +10301809 if (!use_multiport(portdev))
1810 schedule_work(&portdev->config_work);
1811}
1812
1813static void config_work_handler(struct work_struct *work)
1814{
1815 struct ports_device *portdev;
1816
G. Campana8379cad2017-01-19 23:37:46 +02001817 portdev = container_of(work, struct ports_device, config_work);
Amit Shah4038f5b72010-05-06 02:05:07 +05301818 if (!use_multiport(portdev)) {
Michael S. Tsirkineeb8a7e2015-03-05 10:45:49 +10301819 struct virtio_device *vdev;
Amit Shah97788292010-05-06 02:05:08 +05301820 struct port *port;
1821 u16 rows, cols;
1822
Michael S. Tsirkineeb8a7e2015-03-05 10:45:49 +10301823 vdev = portdev->vdev;
Rusty Russell855e0c52013-10-14 18:11:51 +10301824 virtio_cread(vdev, struct virtio_console_config, cols, &cols);
1825 virtio_cread(vdev, struct virtio_console_config, rows, &rows);
Amit Shah97788292010-05-06 02:05:08 +05301826
1827 port = find_port_by_id(portdev, 0);
1828 set_console_size(port, rows, cols);
1829
Amit Shah4038f5b72010-05-06 02:05:07 +05301830 /*
1831 * We'll use this way of resizing only for legacy
1832 * support. For newer userspace
1833 * (VIRTIO_CONSOLE_F_MULTPORT+), use control messages
1834 * to indicate console size changes so that it can be
1835 * done per-port.
1836 */
Amit Shah97788292010-05-06 02:05:08 +05301837 resize_console(port);
Amit Shah4038f5b72010-05-06 02:05:07 +05301838 }
Amit Shah7f5d8102009-12-21 22:22:08 +05301839}
1840
Amit Shah2658a79a2010-01-18 19:15:11 +05301841static int init_vqs(struct ports_device *portdev)
1842{
1843 vq_callback_t **io_callbacks;
1844 char **io_names;
1845 struct virtqueue **vqs;
Amit Shah17634ba2009-12-21 21:03:25 +05301846 u32 i, j, nr_ports, nr_queues;
Amit Shah2658a79a2010-01-18 19:15:11 +05301847 int err;
1848
Michael S. Tsirkin7328fa62016-12-05 21:39:42 +02001849 nr_ports = portdev->max_nr_ports;
Amit Shah17634ba2009-12-21 21:03:25 +05301850 nr_queues = use_multiport(portdev) ? (nr_ports + 1) * 2 : 2;
Amit Shah2658a79a2010-01-18 19:15:11 +05301851
Kees Cook6da2ec52018-06-12 13:55:00 -07001852 vqs = kmalloc_array(nr_queues, sizeof(struct virtqueue *), GFP_KERNEL);
1853 io_callbacks = kmalloc_array(nr_queues, sizeof(vq_callback_t *),
1854 GFP_KERNEL);
1855 io_names = kmalloc_array(nr_queues, sizeof(char *), GFP_KERNEL);
1856 portdev->in_vqs = kmalloc_array(nr_ports, sizeof(struct virtqueue *),
1857 GFP_KERNEL);
1858 portdev->out_vqs = kmalloc_array(nr_ports, sizeof(struct virtqueue *),
1859 GFP_KERNEL);
Jiri Slaby22e132f2010-11-06 10:06:50 +01001860 if (!vqs || !io_callbacks || !io_names || !portdev->in_vqs ||
Amit Shah286f9a22011-09-14 13:06:39 +05301861 !portdev->out_vqs) {
Amit Shah2658a79a2010-01-18 19:15:11 +05301862 err = -ENOMEM;
Jiri Slaby22e132f2010-11-06 10:06:50 +01001863 goto free;
Amit Shah2658a79a2010-01-18 19:15:11 +05301864 }
1865
Amit Shah17634ba2009-12-21 21:03:25 +05301866 /*
1867 * For backward compat (newer host but older guest), the host
1868 * spawns a console port first and also inits the vqs for port
1869 * 0 before others.
1870 */
1871 j = 0;
1872 io_callbacks[j] = in_intr;
Amit Shah2770c5e2011-01-31 13:06:36 +05301873 io_callbacks[j + 1] = out_intr;
Amit Shah17634ba2009-12-21 21:03:25 +05301874 io_names[j] = "input";
1875 io_names[j + 1] = "output";
1876 j += 2;
Amit Shah2658a79a2010-01-18 19:15:11 +05301877
Amit Shah17634ba2009-12-21 21:03:25 +05301878 if (use_multiport(portdev)) {
1879 io_callbacks[j] = control_intr;
1880 io_callbacks[j + 1] = NULL;
1881 io_names[j] = "control-i";
1882 io_names[j + 1] = "control-o";
1883
1884 for (i = 1; i < nr_ports; i++) {
1885 j += 2;
1886 io_callbacks[j] = in_intr;
Amit Shah2770c5e2011-01-31 13:06:36 +05301887 io_callbacks[j + 1] = out_intr;
Amit Shah17634ba2009-12-21 21:03:25 +05301888 io_names[j] = "input";
1889 io_names[j + 1] = "output";
1890 }
1891 }
Amit Shah2658a79a2010-01-18 19:15:11 +05301892 /* Find the queues. */
Michael S. Tsirkin9b2bbdb2017-03-06 18:19:39 +02001893 err = virtio_find_vqs(portdev->vdev, nr_queues, vqs,
1894 io_callbacks,
1895 (const char **)io_names, NULL);
Amit Shah2658a79a2010-01-18 19:15:11 +05301896 if (err)
Jiri Slaby22e132f2010-11-06 10:06:50 +01001897 goto free;
Amit Shah2658a79a2010-01-18 19:15:11 +05301898
Amit Shah17634ba2009-12-21 21:03:25 +05301899 j = 0;
Amit Shah2658a79a2010-01-18 19:15:11 +05301900 portdev->in_vqs[0] = vqs[0];
1901 portdev->out_vqs[0] = vqs[1];
Amit Shah17634ba2009-12-21 21:03:25 +05301902 j += 2;
1903 if (use_multiport(portdev)) {
1904 portdev->c_ivq = vqs[j];
1905 portdev->c_ovq = vqs[j + 1];
Amit Shah2658a79a2010-01-18 19:15:11 +05301906
Amit Shah17634ba2009-12-21 21:03:25 +05301907 for (i = 1; i < nr_ports; i++) {
1908 j += 2;
1909 portdev->in_vqs[i] = vqs[j];
1910 portdev->out_vqs[i] = vqs[j + 1];
1911 }
1912 }
Amit Shah2658a79a2010-01-18 19:15:11 +05301913 kfree(io_names);
Jiri Slaby22e132f2010-11-06 10:06:50 +01001914 kfree(io_callbacks);
Amit Shah2658a79a2010-01-18 19:15:11 +05301915 kfree(vqs);
1916
1917 return 0;
1918
Jiri Slaby22e132f2010-11-06 10:06:50 +01001919free:
Amit Shah2658a79a2010-01-18 19:15:11 +05301920 kfree(portdev->out_vqs);
Amit Shah2658a79a2010-01-18 19:15:11 +05301921 kfree(portdev->in_vqs);
Jiri Slaby22e132f2010-11-06 10:06:50 +01001922 kfree(io_names);
1923 kfree(io_callbacks);
Amit Shah2658a79a2010-01-18 19:15:11 +05301924 kfree(vqs);
Jiri Slaby22e132f2010-11-06 10:06:50 +01001925
Amit Shah2658a79a2010-01-18 19:15:11 +05301926 return err;
1927}
1928
Amit Shahfb08bd22009-12-21 21:36:04 +05301929static const struct file_operations portdev_fops = {
1930 .owner = THIS_MODULE,
1931};
1932
Amit Shaha0e2dbf2011-12-22 16:58:27 +05301933static void remove_vqs(struct ports_device *portdev)
1934{
Michael S. Tsirkina7a69ec2018-04-20 20:24:23 +03001935 struct virtqueue *vq;
1936
1937 virtio_device_for_each_vq(portdev->vdev, vq) {
1938 struct port_buffer *buf;
1939
1940 flush_bufs(vq, true);
1941 while ((buf = virtqueue_detach_unused_buf(vq)))
1942 free_buf(buf, true);
1943 }
Amit Shaha0e2dbf2011-12-22 16:58:27 +05301944 portdev->vdev->config->del_vqs(portdev->vdev);
1945 kfree(portdev->in_vqs);
1946 kfree(portdev->out_vqs);
1947}
1948
Michael S. Tsirkinaa44ec82018-04-20 20:51:18 +03001949static void virtcons_remove(struct virtio_device *vdev)
1950{
1951 struct ports_device *portdev;
1952 struct port *port, *port2;
1953
1954 portdev = vdev->priv;
1955
1956 spin_lock_irq(&pdrvdata_lock);
1957 list_del(&portdev->list);
1958 spin_unlock_irq(&pdrvdata_lock);
1959
1960 /* Disable interrupts for vqs */
Michael S. Tsirkind9679d02021-10-13 06:55:44 -04001961 virtio_reset_device(vdev);
Michael S. Tsirkinaa44ec82018-04-20 20:51:18 +03001962 /* Finish up work that's lined up */
1963 if (use_multiport(portdev))
1964 cancel_work_sync(&portdev->control_work);
1965 else
1966 cancel_work_sync(&portdev->config_work);
1967
1968 list_for_each_entry_safe(port, port2, &portdev->ports, list)
1969 unplug_port(port);
1970
1971 unregister_chrdev(portdev->chr_major, "virtio-portsdev");
1972
1973 /*
1974 * When yanking out a device, we immediately lose the
1975 * (device-side) queues. So there's no point in keeping the
1976 * guest side around till we drop our final reference. This
1977 * also means that any ports which are in an open state will
1978 * have to just stop using the port, as the vqs are going
1979 * away.
1980 */
1981 remove_vqs(portdev);
1982 kfree(portdev);
1983}
1984
Amit Shah1c85bf32010-01-18 19:15:07 +05301985/*
1986 * Once we're further in boot, we get probed like any other virtio
1987 * device.
Amit Shah17634ba2009-12-21 21:03:25 +05301988 *
1989 * If the host also supports multiple console ports, we check the
1990 * config space to see how many ports the host has spawned. We
1991 * initialize each port found.
Amit Shah1c85bf32010-01-18 19:15:07 +05301992 */
Bill Pemberton2223cbe2012-11-19 13:22:51 -05001993static int virtcons_probe(struct virtio_device *vdev)
Amit Shah1c85bf32010-01-18 19:15:07 +05301994{
Amit Shah1c85bf32010-01-18 19:15:07 +05301995 struct ports_device *portdev;
1996 int err;
Amit Shah17634ba2009-12-21 21:03:25 +05301997 bool multiport;
Christian Borntraeger5e384832011-09-22 23:44:23 +05301998 bool early = early_put_chars != NULL;
1999
Rusty Russellbe8ff592015-02-11 15:01:14 +10302000 /* We only need a config space if features are offered */
2001 if (!vdev->config->get &&
2002 (virtio_has_feature(vdev, VIRTIO_CONSOLE_F_SIZE)
2003 || virtio_has_feature(vdev, VIRTIO_CONSOLE_F_MULTIPORT))) {
Michael S. Tsirkin011f0e72015-01-12 16:23:37 +02002004 dev_err(&vdev->dev, "%s failure: config access disabled\n",
2005 __func__);
2006 return -EINVAL;
2007 }
2008
Christian Borntraeger5e384832011-09-22 23:44:23 +05302009 /* Ensure to read early_put_chars now */
2010 barrier();
Amit Shah1c85bf32010-01-18 19:15:07 +05302011
2012 portdev = kmalloc(sizeof(*portdev), GFP_KERNEL);
2013 if (!portdev) {
2014 err = -ENOMEM;
2015 goto fail;
2016 }
2017
2018 /* Attach this portdev to this virtio_device, and vice-versa. */
2019 portdev->vdev = vdev;
2020 vdev->priv = portdev;
2021
Amit Shahfb08bd22009-12-21 21:36:04 +05302022 portdev->chr_major = register_chrdev(0, "virtio-portsdev",
2023 &portdev_fops);
2024 if (portdev->chr_major < 0) {
2025 dev_err(&vdev->dev,
2026 "Error %d registering chrdev for device %u\n",
Sjur Brændelanddc18f082013-02-12 16:24:59 +10302027 portdev->chr_major, vdev->index);
Amit Shahfb08bd22009-12-21 21:36:04 +05302028 err = portdev->chr_major;
2029 goto free;
2030 }
2031
Amit Shah17634ba2009-12-21 21:03:25 +05302032 multiport = false;
Michael S. Tsirkin7328fa62016-12-05 21:39:42 +02002033 portdev->max_nr_ports = 1;
Sjur Brændeland1b637042012-12-14 14:40:51 +10302034
2035 /* Don't test MULTIPORT at all if we're rproc: not a valid feature! */
2036 if (!is_rproc_serial(vdev) &&
Rusty Russell855e0c52013-10-14 18:11:51 +10302037 virtio_cread_feature(vdev, VIRTIO_CONSOLE_F_MULTIPORT,
2038 struct virtio_console_config, max_nr_ports,
Michael S. Tsirkin7328fa62016-12-05 21:39:42 +02002039 &portdev->max_nr_ports) == 0) {
Jason Wang28962ec2021-10-19 15:01:44 +08002040 if (portdev->max_nr_ports == 0 ||
2041 portdev->max_nr_ports > VIRTCONS_MAX_PORTS) {
2042 dev_err(&vdev->dev,
2043 "Invalidate max_nr_ports %d",
2044 portdev->max_nr_ports);
2045 err = -EINVAL;
2046 goto free;
2047 }
Amit Shah17634ba2009-12-21 21:03:25 +05302048 multiport = true;
Sjur Brændeland1b637042012-12-14 14:40:51 +10302049 }
Amit Shah17634ba2009-12-21 21:03:25 +05302050
Amit Shah2658a79a2010-01-18 19:15:11 +05302051 err = init_vqs(portdev);
2052 if (err < 0) {
2053 dev_err(&vdev->dev, "Error %d initializing vqs\n", err);
Amit Shahfb08bd22009-12-21 21:36:04 +05302054 goto free_chrdev;
Amit Shah2658a79a2010-01-18 19:15:11 +05302055 }
Amit Shah1c85bf32010-01-18 19:15:07 +05302056
Amit Shah17634ba2009-12-21 21:03:25 +05302057 spin_lock_init(&portdev->ports_lock);
2058 INIT_LIST_HEAD(&portdev->ports);
Michael S. Tsirkin5c603002018-04-20 21:00:13 +03002059 INIT_LIST_HEAD(&portdev->list);
Amit Shah17634ba2009-12-21 21:03:25 +05302060
Cornelia Huck65eca3a2014-10-20 15:58:49 +02002061 virtio_device_ready(portdev->vdev);
2062
Michael S. Tsirkineeb8a7e2015-03-05 10:45:49 +10302063 INIT_WORK(&portdev->config_work, &config_work_handler);
Michael S. Tsirkin4f6e24e2015-03-05 10:45:30 +10302064 INIT_WORK(&portdev->control_work, &control_work_handler);
2065
Amit Shah17634ba2009-12-21 21:03:25 +05302066 if (multiport) {
Amit Shah165b1b82013-03-29 16:30:07 +05302067 spin_lock_init(&portdev->c_ivq_lock);
Amit Shah9ba5c802013-03-29 16:30:08 +05302068 spin_lock_init(&portdev->c_ovq_lock);
Amit Shah17634ba2009-12-21 21:03:25 +05302069
Laurent Vivierd791cfc2019-11-14 13:25:48 +01002070 err = fill_queue(portdev->c_ivq, &portdev->c_ivq_lock);
2071 if (err < 0) {
Amit Shah22a29ea2010-02-12 10:32:17 +05302072 dev_err(&vdev->dev,
2073 "Error allocating buffers for control queue\n");
Michael S. Tsirkin5c603002018-04-20 21:00:13 +03002074 /*
2075 * The host might want to notify mgmt sw about device
2076 * add failure.
2077 */
2078 __send_control_msg(portdev, VIRTIO_CONSOLE_BAD_ID,
2079 VIRTIO_CONSOLE_DEVICE_READY, 0);
2080 /* Device was functional: we need full cleanup. */
2081 virtcons_remove(vdev);
Laurent Vivierd791cfc2019-11-14 13:25:48 +01002082 return err;
Amit Shah22a29ea2010-02-12 10:32:17 +05302083 }
Amit Shah1d051602010-05-19 22:15:49 -06002084 } else {
2085 /*
2086 * For backward compatibility: Create a console port
2087 * if we're running on older host.
2088 */
2089 add_port(portdev, 0);
Amit Shah17634ba2009-12-21 21:03:25 +05302090 }
2091
Amit Shah6bdf2af2010-09-02 18:11:49 +05302092 spin_lock_irq(&pdrvdata_lock);
2093 list_add_tail(&portdev->list, &pdrvdata.portdevs);
2094 spin_unlock_irq(&pdrvdata_lock);
2095
Amit Shahf909f852010-05-19 22:15:48 -06002096 __send_control_msg(portdev, VIRTIO_CONSOLE_BAD_ID,
2097 VIRTIO_CONSOLE_DEVICE_READY, 1);
Christian Borntraeger5e384832011-09-22 23:44:23 +05302098
2099 /*
2100 * If there was an early virtio console, assume that there are no
2101 * other consoles. We need to wait until the hvc_alloc matches the
2102 * hvc_instantiate, otherwise tty_open will complain, resulting in
2103 * a "Warning: unable to open an initial console" boot failure.
2104 * Without multiport this is done in add_port above. With multiport
2105 * this might take some host<->guest communication - thus we have to
2106 * wait.
2107 */
2108 if (multiport && early)
2109 wait_for_completion(&early_console_added);
2110
Rusty Russell31610432007-10-22 11:03:39 +10002111 return 0;
2112
Amit Shahfb08bd22009-12-21 21:36:04 +05302113free_chrdev:
2114 unregister_chrdev(portdev->chr_major, "virtio-portsdev");
Rusty Russell31610432007-10-22 11:03:39 +10002115free:
Amit Shah1c85bf32010-01-18 19:15:07 +05302116 kfree(portdev);
Rusty Russell31610432007-10-22 11:03:39 +10002117fail:
2118 return err;
2119}
2120
Rikard Falkeborn8b66c912020-07-01 22:09:50 +02002121static const struct virtio_device_id id_table[] = {
Rusty Russell31610432007-10-22 11:03:39 +10002122 { VIRTIO_ID_CONSOLE, VIRTIO_DEV_ANY_ID },
2123 { 0 },
2124};
Alexander Lobakin897c44f2020-06-23 11:09:33 +00002125MODULE_DEVICE_TABLE(virtio, id_table);
Rusty Russell31610432007-10-22 11:03:39 +10002126
Rikard Falkeborn8b66c912020-07-01 22:09:50 +02002127static const unsigned int features[] = {
Christian Borntraegerc2983452008-11-25 13:36:26 +01002128 VIRTIO_CONSOLE_F_SIZE,
Amit Shahb99fa812010-05-19 22:15:46 -06002129 VIRTIO_CONSOLE_F_MULTIPORT,
Christian Borntraegerc2983452008-11-25 13:36:26 +01002130};
2131
Rikard Falkeborn8b66c912020-07-01 22:09:50 +02002132static const struct virtio_device_id rproc_serial_id_table[] = {
Sjur Brændeland1b637042012-12-14 14:40:51 +10302133#if IS_ENABLED(CONFIG_REMOTEPROC)
2134 { VIRTIO_ID_RPROC_SERIAL, VIRTIO_DEV_ANY_ID },
2135#endif
2136 { 0 },
2137};
Alexander Lobakin897c44f2020-06-23 11:09:33 +00002138MODULE_DEVICE_TABLE(virtio, rproc_serial_id_table);
Sjur Brændeland1b637042012-12-14 14:40:51 +10302139
Rikard Falkeborn8b66c912020-07-01 22:09:50 +02002140static const unsigned int rproc_serial_features[] = {
Sjur Brændeland1b637042012-12-14 14:40:51 +10302141};
2142
Aaron Lu89107002013-09-17 09:25:23 +09302143#ifdef CONFIG_PM_SLEEP
Amit Shah2b8f41d2011-12-22 16:58:28 +05302144static int virtcons_freeze(struct virtio_device *vdev)
2145{
2146 struct ports_device *portdev;
2147 struct port *port;
2148
2149 portdev = vdev->priv;
2150
Michael S. Tsirkind9679d02021-10-13 06:55:44 -04002151 virtio_reset_device(vdev);
Amit Shah2b8f41d2011-12-22 16:58:28 +05302152
Michael S. Tsirkin20559972017-03-29 23:22:04 +03002153 if (use_multiport(portdev))
2154 virtqueue_disable_cb(portdev->c_ivq);
Amit Shah2b8f41d2011-12-22 16:58:28 +05302155 cancel_work_sync(&portdev->control_work);
Michael S. Tsirkineeb8a7e2015-03-05 10:45:49 +10302156 cancel_work_sync(&portdev->config_work);
Amit Shahc743d092012-01-06 16:19:08 +05302157 /*
2158 * Once more: if control_work_handler() was running, it would
2159 * enable the cb as the last step.
2160 */
Michael S. Tsirkin20559972017-03-29 23:22:04 +03002161 if (use_multiport(portdev))
2162 virtqueue_disable_cb(portdev->c_ivq);
Amit Shah2b8f41d2011-12-22 16:58:28 +05302163
2164 list_for_each_entry(port, &portdev->ports, list) {
Amit Shahc743d092012-01-06 16:19:08 +05302165 virtqueue_disable_cb(port->in_vq);
2166 virtqueue_disable_cb(port->out_vq);
Amit Shah2b8f41d2011-12-22 16:58:28 +05302167 /*
2168 * We'll ask the host later if the new invocation has
2169 * the port opened or closed.
2170 */
2171 port->host_connected = false;
2172 remove_port_data(port);
2173 }
2174 remove_vqs(portdev);
2175
2176 return 0;
2177}
2178
2179static int virtcons_restore(struct virtio_device *vdev)
2180{
2181 struct ports_device *portdev;
2182 struct port *port;
2183 int ret;
2184
2185 portdev = vdev->priv;
2186
2187 ret = init_vqs(portdev);
2188 if (ret)
2189 return ret;
2190
Michael S. Tsirkin401bbdc2014-10-15 10:22:32 +10302191 virtio_device_ready(portdev->vdev);
2192
Amit Shah2b8f41d2011-12-22 16:58:28 +05302193 if (use_multiport(portdev))
Amit Shah165b1b82013-03-29 16:30:07 +05302194 fill_queue(portdev->c_ivq, &portdev->c_ivq_lock);
Amit Shah2b8f41d2011-12-22 16:58:28 +05302195
2196 list_for_each_entry(port, &portdev->ports, list) {
2197 port->in_vq = portdev->in_vqs[port->id];
2198 port->out_vq = portdev->out_vqs[port->id];
2199
2200 fill_queue(port->in_vq, &port->inbuf_lock);
2201
2202 /* Get port open/close status on the host */
2203 send_control_msg(port, VIRTIO_CONSOLE_PORT_READY, 1);
Amit Shahfa8b66c2012-04-25 14:40:39 +05302204
2205 /*
2206 * If a port was open at the time of suspending, we
2207 * have to let the host know that it's still open.
2208 */
2209 if (port->guest_connected)
2210 send_control_msg(port, VIRTIO_CONSOLE_PORT_OPEN, 1);
Amit Shah2b8f41d2011-12-22 16:58:28 +05302211 }
2212 return 0;
2213}
2214#endif
2215
Rusty Russell31610432007-10-22 11:03:39 +10002216static struct virtio_driver virtio_console = {
Christian Borntraegerc2983452008-11-25 13:36:26 +01002217 .feature_table = features,
2218 .feature_table_size = ARRAY_SIZE(features),
Rusty Russell31610432007-10-22 11:03:39 +10002219 .driver.name = KBUILD_MODNAME,
2220 .driver.owner = THIS_MODULE,
2221 .id_table = id_table,
2222 .probe = virtcons_probe,
Amit Shah71778762010-02-12 10:32:16 +05302223 .remove = virtcons_remove,
Amit Shah7f5d8102009-12-21 22:22:08 +05302224 .config_changed = config_intr,
Aaron Lu89107002013-09-17 09:25:23 +09302225#ifdef CONFIG_PM_SLEEP
Amit Shah2b8f41d2011-12-22 16:58:28 +05302226 .freeze = virtcons_freeze,
2227 .restore = virtcons_restore,
2228#endif
Rusty Russell31610432007-10-22 11:03:39 +10002229};
2230
Greg Kroah-Hartmanbcd29822012-12-21 15:12:08 -08002231static struct virtio_driver virtio_rproc_serial = {
Sjur Brændeland1b637042012-12-14 14:40:51 +10302232 .feature_table = rproc_serial_features,
2233 .feature_table_size = ARRAY_SIZE(rproc_serial_features),
2234 .driver.name = "virtio_rproc_serial",
2235 .driver.owner = THIS_MODULE,
2236 .id_table = rproc_serial_id_table,
2237 .probe = virtcons_probe,
2238 .remove = virtcons_remove,
2239};
2240
Rusty Russell31610432007-10-22 11:03:39 +10002241static int __init init(void)
2242{
Amit Shahfb08bd22009-12-21 21:36:04 +05302243 int err;
2244
2245 pdrvdata.class = class_create(THIS_MODULE, "virtio-ports");
2246 if (IS_ERR(pdrvdata.class)) {
2247 err = PTR_ERR(pdrvdata.class);
2248 pr_err("Error %d creating virtio-ports class\n", err);
2249 return err;
2250 }
Amit Shahd99393e2009-12-21 22:36:21 +05302251
2252 pdrvdata.debugfs_dir = debugfs_create_dir("virtio-ports", NULL);
Amit Shah38edf582010-01-18 19:15:05 +05302253 INIT_LIST_HEAD(&pdrvdata.consoles);
Amit Shah6bdf2af2010-09-02 18:11:49 +05302254 INIT_LIST_HEAD(&pdrvdata.portdevs);
Amit Shah38edf582010-01-18 19:15:05 +05302255
Alexey Khoroshilov33e1afc2012-09-01 23:49:37 +04002256 err = register_virtio_driver(&virtio_console);
2257 if (err < 0) {
2258 pr_err("Error %d registering virtio driver\n", err);
2259 goto free;
2260 }
Sjur Brændeland1b637042012-12-14 14:40:51 +10302261 err = register_virtio_driver(&virtio_rproc_serial);
2262 if (err < 0) {
2263 pr_err("Error %d registering virtio rproc serial driver\n",
2264 err);
2265 goto unregister;
2266 }
Alexey Khoroshilov33e1afc2012-09-01 23:49:37 +04002267 return 0;
Sjur Brændeland1b637042012-12-14 14:40:51 +10302268unregister:
2269 unregister_virtio_driver(&virtio_console);
Alexey Khoroshilov33e1afc2012-09-01 23:49:37 +04002270free:
Fabian Frederick5885e482014-07-27 07:30:01 +09302271 debugfs_remove_recursive(pdrvdata.debugfs_dir);
Alexey Khoroshilov33e1afc2012-09-01 23:49:37 +04002272 class_destroy(pdrvdata.class);
2273 return err;
Rusty Russell31610432007-10-22 11:03:39 +10002274}
Amit Shah71778762010-02-12 10:32:16 +05302275
2276static void __exit fini(void)
2277{
Sjur Brændeland1b637042012-12-14 14:40:51 +10302278 reclaim_dma_bufs();
2279
Amit Shah71778762010-02-12 10:32:16 +05302280 unregister_virtio_driver(&virtio_console);
Sjur Brændeland1b637042012-12-14 14:40:51 +10302281 unregister_virtio_driver(&virtio_rproc_serial);
Amit Shah71778762010-02-12 10:32:16 +05302282
2283 class_destroy(pdrvdata.class);
Fabian Frederick5885e482014-07-27 07:30:01 +09302284 debugfs_remove_recursive(pdrvdata.debugfs_dir);
Amit Shah71778762010-02-12 10:32:16 +05302285}
Rusty Russell31610432007-10-22 11:03:39 +10002286module_init(init);
Amit Shah71778762010-02-12 10:32:16 +05302287module_exit(fini);
Rusty Russell31610432007-10-22 11:03:39 +10002288
Rusty Russell31610432007-10-22 11:03:39 +10002289MODULE_DESCRIPTION("Virtio console driver");
2290MODULE_LICENSE("GPL");