blob: 730816cdeb45c46fb0517ceccec72ecb5993aaa0 [file] [log] [blame]
Rusty Russella23ea922010-01-18 19:14:55 +05301/*
2 * Copyright (C) 2006, 2007, 2009 Rusty Russell, IBM Corporation
Amit Shah5084f892011-01-31 13:06:37 +05303 * Copyright (C) 2009, 2010, 2011 Red Hat, Inc.
4 * Copyright (C) 2009, 2010, 2011 Amit Shah <amit.shah@redhat.com>
Rusty Russell31610432007-10-22 11:03:39 +10005 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 */
Amit Shahfb08bd22009-12-21 21:36:04 +053020#include <linux/cdev.h>
Amit Shahd99393e2009-12-21 22:36:21 +053021#include <linux/debugfs.h>
Christian Borntraeger5e384832011-09-22 23:44:23 +053022#include <linux/completion.h>
Amit Shahfb08bd22009-12-21 21:36:04 +053023#include <linux/device.h>
Rusty Russell31610432007-10-22 11:03:39 +100024#include <linux/err.h>
Amit Shaha08fa922011-09-14 13:06:41 +053025#include <linux/freezer.h>
Amit Shah2030fa42009-12-21 21:49:30 +053026#include <linux/fs.h>
Masami Hiramatsueb5e89f2012-08-09 21:30:39 +090027#include <linux/splice.h>
28#include <linux/pagemap.h>
Rusty Russell31610432007-10-22 11:03:39 +100029#include <linux/init.h>
Amit Shah38edf582010-01-18 19:15:05 +053030#include <linux/list.h>
Amit Shah2030fa42009-12-21 21:49:30 +053031#include <linux/poll.h>
32#include <linux/sched.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090033#include <linux/slab.h>
Amit Shah38edf582010-01-18 19:15:05 +053034#include <linux/spinlock.h>
Rusty Russell31610432007-10-22 11:03:39 +100035#include <linux/virtio.h>
36#include <linux/virtio_console.h>
Amit Shah2030fa42009-12-21 21:49:30 +053037#include <linux/wait.h>
Amit Shah17634ba2009-12-21 21:03:25 +053038#include <linux/workqueue.h>
Paul Gortmakerc22405c2011-07-03 13:35:48 -040039#include <linux/module.h>
Amit Shah51df0ac2011-02-01 09:31:25 +053040#include "../tty/hvc/hvc_console.h"
Rusty Russell31610432007-10-22 11:03:39 +100041
Amit Shah38edf582010-01-18 19:15:05 +053042/*
43 * This is a global struct for storing common data for all the devices
44 * this driver handles.
45 *
46 * Mainly, it has a linked list for all the consoles in one place so
47 * that callbacks from hvc for get_chars(), put_chars() work properly
48 * across multiple devices and multiple ports per device.
49 */
50struct ports_driver_data {
Amit Shahfb08bd22009-12-21 21:36:04 +053051 /* Used for registering chardevs */
52 struct class *class;
53
Amit Shahd99393e2009-12-21 22:36:21 +053054 /* Used for exporting per-port information to debugfs */
55 struct dentry *debugfs_dir;
56
Amit Shah6bdf2af2010-09-02 18:11:49 +053057 /* List of all the devices we're handling */
58 struct list_head portdevs;
59
Amit Shahfb08bd22009-12-21 21:36:04 +053060 /* Number of devices this driver is handling */
61 unsigned int index;
62
Rusty Russelld8a02bd2010-01-18 19:15:06 +053063 /*
64 * This is used to keep track of the number of hvc consoles
65 * spawned by this driver. This number is given as the first
66 * argument to hvc_alloc(). To correctly map an initial
67 * console spawned via hvc_instantiate to the console being
68 * hooked up via hvc_alloc, we need to pass the same vtermno.
69 *
70 * We also just assume the first console being initialised was
71 * the first one that got used as the initial console.
72 */
73 unsigned int next_vtermno;
74
Amit Shah38edf582010-01-18 19:15:05 +053075 /* All the console devices handled by this driver */
76 struct list_head consoles;
77};
78static struct ports_driver_data pdrvdata;
79
80DEFINE_SPINLOCK(pdrvdata_lock);
Christian Borntraeger5e384832011-09-22 23:44:23 +053081DECLARE_COMPLETION(early_console_added);
Amit Shah38edf582010-01-18 19:15:05 +053082
Amit Shah4f23c572010-01-18 19:15:09 +053083/* This struct holds information that's relevant only for console ports */
84struct console {
85 /* We'll place all consoles in a list in the pdrvdata struct */
86 struct list_head list;
87
88 /* The hvc device associated with this console port */
89 struct hvc_struct *hvc;
90
Amit Shah97788292010-05-06 02:05:08 +053091 /* The size of the console */
92 struct winsize ws;
93
Amit Shah4f23c572010-01-18 19:15:09 +053094 /*
95 * This number identifies the number that we used to register
96 * with hvc in hvc_instantiate() and hvc_alloc(); this is the
97 * number passed on by the hvc callbacks to us to
98 * differentiate between the other console ports handled by
99 * this driver
100 */
101 u32 vtermno;
102};
103
Amit Shahfdb9a052010-01-18 19:15:01 +0530104struct port_buffer {
105 char *buf;
106
107 /* size of the buffer in *buf above */
108 size_t size;
109
110 /* used length of the buffer */
111 size_t len;
112 /* offset in the buf from which to consume data */
113 size_t offset;
114};
115
Amit Shah17634ba2009-12-21 21:03:25 +0530116/*
117 * This is a per-device struct that stores data common to all the
118 * ports for that device (vdev->priv).
119 */
120struct ports_device {
Amit Shah6bdf2af2010-09-02 18:11:49 +0530121 /* Next portdev in the list, head is in the pdrvdata struct */
122 struct list_head list;
123
Amit Shah17634ba2009-12-21 21:03:25 +0530124 /*
125 * Workqueue handlers where we process deferred work after
126 * notification
127 */
128 struct work_struct control_work;
129
130 struct list_head ports;
131
132 /* To protect the list of ports */
133 spinlock_t ports_lock;
134
135 /* To protect the vq operations for the control channel */
136 spinlock_t cvq_lock;
137
138 /* The current config space is stored here */
Amit Shahb99fa812010-05-19 22:15:46 -0600139 struct virtio_console_config config;
Amit Shah17634ba2009-12-21 21:03:25 +0530140
141 /* The virtio device we're associated with */
142 struct virtio_device *vdev;
143
144 /*
145 * A couple of virtqueues for the control channel: one for
146 * guest->host transfers, one for host->guest transfers
147 */
148 struct virtqueue *c_ivq, *c_ovq;
149
150 /* Array of per-port IO virtqueues */
151 struct virtqueue **in_vqs, **out_vqs;
Amit Shahfb08bd22009-12-21 21:36:04 +0530152
153 /* Used for numbering devices for sysfs and debugfs */
154 unsigned int drv_index;
155
156 /* Major number for this device. Ports will be created as minors. */
157 int chr_major;
Amit Shah17634ba2009-12-21 21:03:25 +0530158};
159
Amit Shah17e5b4f2011-09-14 13:06:46 +0530160struct port_stats {
161 unsigned long bytes_sent, bytes_received, bytes_discarded;
162};
163
Amit Shah1c85bf32010-01-18 19:15:07 +0530164/* This struct holds the per-port data */
Rusty Russell21206ed2010-01-18 19:15:00 +0530165struct port {
Amit Shah17634ba2009-12-21 21:03:25 +0530166 /* Next port in the list, head is in the ports_device */
167 struct list_head list;
168
Amit Shah1c85bf32010-01-18 19:15:07 +0530169 /* Pointer to the parent virtio_console device */
170 struct ports_device *portdev;
Amit Shahfdb9a052010-01-18 19:15:01 +0530171
172 /* The current buffer from which data has to be fed to readers */
173 struct port_buffer *inbuf;
Rusty Russell31610432007-10-22 11:03:39 +1000174
Amit Shah203baab2010-01-18 19:15:12 +0530175 /*
176 * To protect the operations on the in_vq associated with this
177 * port. Has to be a spinlock because it can be called from
178 * interrupt context (get_char()).
179 */
180 spinlock_t inbuf_lock;
181
Amit Shahcdfadfc2010-05-19 22:15:50 -0600182 /* Protect the operations on the out_vq. */
183 spinlock_t outvq_lock;
184
Amit Shah1c85bf32010-01-18 19:15:07 +0530185 /* The IO vqs for this port */
186 struct virtqueue *in_vq, *out_vq;
187
Amit Shahd99393e2009-12-21 22:36:21 +0530188 /* File in the debugfs directory that exposes this port's information */
189 struct dentry *debugfs_file;
190
Amit Shah4f23c572010-01-18 19:15:09 +0530191 /*
Amit Shah17e5b4f2011-09-14 13:06:46 +0530192 * Keep count of the bytes sent, received and discarded for
193 * this port for accounting and debugging purposes. These
194 * counts are not reset across port open / close events.
195 */
196 struct port_stats stats;
197
198 /*
Amit Shah4f23c572010-01-18 19:15:09 +0530199 * The entries in this struct will be valid if this port is
200 * hooked up to an hvc console
201 */
202 struct console cons;
Amit Shah17634ba2009-12-21 21:03:25 +0530203
Amit Shahfb08bd22009-12-21 21:36:04 +0530204 /* Each port associates with a separate char device */
Amit Shahd22a6982010-09-02 18:20:59 +0530205 struct cdev *cdev;
Amit Shahfb08bd22009-12-21 21:36:04 +0530206 struct device *dev;
207
Amit Shahb353a6b2010-09-02 18:38:29 +0530208 /* Reference-counting to handle port hot-unplugs and file operations */
209 struct kref kref;
210
Amit Shah2030fa42009-12-21 21:49:30 +0530211 /* A waitqueue for poll() or blocking read operations */
212 wait_queue_head_t waitqueue;
213
Amit Shah431edb82009-12-21 21:57:40 +0530214 /* The 'name' of the port that we expose via sysfs properties */
215 char *name;
216
Amit Shah3eae0ad2010-09-02 18:47:52 +0530217 /* We can notify apps of host connect / disconnect events via SIGIO */
218 struct fasync_struct *async_queue;
219
Amit Shah17634ba2009-12-21 21:03:25 +0530220 /* The 'id' to identify the port with the Host */
221 u32 id;
Amit Shah2030fa42009-12-21 21:49:30 +0530222
Amit Shahcdfadfc2010-05-19 22:15:50 -0600223 bool outvq_full;
224
Amit Shah2030fa42009-12-21 21:49:30 +0530225 /* Is the host device open */
226 bool host_connected;
Amit Shah3c7969c2009-11-26 11:25:38 +0530227
228 /* We should allow only one process to open a port */
229 bool guest_connected;
Rusty Russell21206ed2010-01-18 19:15:00 +0530230};
Rusty Russell31610432007-10-22 11:03:39 +1000231
Masami Hiramatsueb5e89f2012-08-09 21:30:39 +0900232#define MAX_SPLICE_PAGES 32
Rusty Russell971f3392010-01-18 19:14:56 +0530233/* This is the very early arch-specified put chars function. */
234static int (*early_put_chars)(u32, const char *, int);
235
Amit Shah38edf582010-01-18 19:15:05 +0530236static struct port *find_port_by_vtermno(u32 vtermno)
237{
238 struct port *port;
Amit Shah4f23c572010-01-18 19:15:09 +0530239 struct console *cons;
Amit Shah38edf582010-01-18 19:15:05 +0530240 unsigned long flags;
241
242 spin_lock_irqsave(&pdrvdata_lock, flags);
Amit Shah4f23c572010-01-18 19:15:09 +0530243 list_for_each_entry(cons, &pdrvdata.consoles, list) {
244 if (cons->vtermno == vtermno) {
245 port = container_of(cons, struct port, cons);
Amit Shah38edf582010-01-18 19:15:05 +0530246 goto out;
Amit Shah4f23c572010-01-18 19:15:09 +0530247 }
Amit Shah38edf582010-01-18 19:15:05 +0530248 }
249 port = NULL;
250out:
251 spin_unlock_irqrestore(&pdrvdata_lock, flags);
252 return port;
253}
254
Amit Shah04950cd2010-09-02 18:20:58 +0530255static struct port *find_port_by_devt_in_portdev(struct ports_device *portdev,
256 dev_t dev)
257{
258 struct port *port;
259 unsigned long flags;
260
261 spin_lock_irqsave(&portdev->ports_lock, flags);
262 list_for_each_entry(port, &portdev->ports, list)
Amit Shahd22a6982010-09-02 18:20:59 +0530263 if (port->cdev->dev == dev)
Amit Shah04950cd2010-09-02 18:20:58 +0530264 goto out;
265 port = NULL;
266out:
267 spin_unlock_irqrestore(&portdev->ports_lock, flags);
268
269 return port;
270}
271
272static struct port *find_port_by_devt(dev_t dev)
273{
274 struct ports_device *portdev;
275 struct port *port;
276 unsigned long flags;
277
278 spin_lock_irqsave(&pdrvdata_lock, flags);
279 list_for_each_entry(portdev, &pdrvdata.portdevs, list) {
280 port = find_port_by_devt_in_portdev(portdev, dev);
281 if (port)
282 goto out;
283 }
284 port = NULL;
285out:
286 spin_unlock_irqrestore(&pdrvdata_lock, flags);
287 return port;
288}
289
Amit Shah17634ba2009-12-21 21:03:25 +0530290static struct port *find_port_by_id(struct ports_device *portdev, u32 id)
291{
292 struct port *port;
293 unsigned long flags;
294
295 spin_lock_irqsave(&portdev->ports_lock, flags);
296 list_for_each_entry(port, &portdev->ports, list)
297 if (port->id == id)
298 goto out;
299 port = NULL;
300out:
301 spin_unlock_irqrestore(&portdev->ports_lock, flags);
302
303 return port;
304}
305
Amit Shah203baab2010-01-18 19:15:12 +0530306static struct port *find_port_by_vq(struct ports_device *portdev,
307 struct virtqueue *vq)
308{
309 struct port *port;
Amit Shah203baab2010-01-18 19:15:12 +0530310 unsigned long flags;
311
Amit Shah17634ba2009-12-21 21:03:25 +0530312 spin_lock_irqsave(&portdev->ports_lock, flags);
313 list_for_each_entry(port, &portdev->ports, list)
Amit Shah203baab2010-01-18 19:15:12 +0530314 if (port->in_vq == vq || port->out_vq == vq)
315 goto out;
Amit Shah203baab2010-01-18 19:15:12 +0530316 port = NULL;
317out:
Amit Shah17634ba2009-12-21 21:03:25 +0530318 spin_unlock_irqrestore(&portdev->ports_lock, flags);
Amit Shah203baab2010-01-18 19:15:12 +0530319 return port;
320}
321
Amit Shah17634ba2009-12-21 21:03:25 +0530322static bool is_console_port(struct port *port)
323{
324 if (port->cons.hvc)
325 return true;
326 return false;
327}
328
329static inline bool use_multiport(struct ports_device *portdev)
330{
331 /*
332 * This condition can be true when put_chars is called from
333 * early_init
334 */
335 if (!portdev->vdev)
336 return 0;
337 return portdev->vdev->features[0] & (1 << VIRTIO_CONSOLE_F_MULTIPORT);
338}
339
Amit Shahfdb9a052010-01-18 19:15:01 +0530340static void free_buf(struct port_buffer *buf)
341{
342 kfree(buf->buf);
343 kfree(buf);
344}
345
346static struct port_buffer *alloc_buf(size_t buf_size)
347{
348 struct port_buffer *buf;
349
350 buf = kmalloc(sizeof(*buf), GFP_KERNEL);
351 if (!buf)
352 goto fail;
353 buf->buf = kzalloc(buf_size, GFP_KERNEL);
354 if (!buf->buf)
355 goto free_buf;
356 buf->len = 0;
357 buf->offset = 0;
358 buf->size = buf_size;
359 return buf;
360
361free_buf:
362 kfree(buf);
363fail:
364 return NULL;
365}
366
Amit Shaha3cde442010-01-18 19:15:03 +0530367/* Callers should take appropriate locks */
Amit Shahdefde662011-09-14 13:06:42 +0530368static struct port_buffer *get_inbuf(struct port *port)
Amit Shaha3cde442010-01-18 19:15:03 +0530369{
370 struct port_buffer *buf;
Amit Shaha3cde442010-01-18 19:15:03 +0530371 unsigned int len;
372
Amit Shahd25a9dd2011-09-14 13:06:43 +0530373 if (port->inbuf)
374 return port->inbuf;
375
376 buf = virtqueue_get_buf(port->in_vq, &len);
Amit Shaha3cde442010-01-18 19:15:03 +0530377 if (buf) {
378 buf->len = len;
379 buf->offset = 0;
Amit Shah17e5b4f2011-09-14 13:06:46 +0530380 port->stats.bytes_received += len;
Amit Shaha3cde442010-01-18 19:15:03 +0530381 }
382 return buf;
383}
384
Rusty Russella23ea922010-01-18 19:14:55 +0530385/*
Amit Shahe27b5192010-01-18 19:15:02 +0530386 * Create a scatter-gather list representing our input buffer and put
387 * it in the queue.
388 *
389 * Callers should take appropriate locks.
390 */
Amit Shah203baab2010-01-18 19:15:12 +0530391static int add_inbuf(struct virtqueue *vq, struct port_buffer *buf)
Amit Shahe27b5192010-01-18 19:15:02 +0530392{
393 struct scatterlist sg[1];
Amit Shah203baab2010-01-18 19:15:12 +0530394 int ret;
Amit Shah1c85bf32010-01-18 19:15:07 +0530395
Amit Shahe27b5192010-01-18 19:15:02 +0530396 sg_init_one(sg, buf->buf, buf->size);
397
Rusty Russellf96fde42012-01-12 15:44:42 +1030398 ret = virtqueue_add_buf(vq, sg, 0, 1, buf, GFP_ATOMIC);
Michael S. Tsirkin505b0452010-04-12 16:18:32 +0300399 virtqueue_kick(vq);
Amit Shah203baab2010-01-18 19:15:12 +0530400 return ret;
401}
402
Amit Shah88f251a2009-12-21 22:15:30 +0530403/* Discard any unread data this port has. Callers lockers. */
404static void discard_port_data(struct port *port)
405{
406 struct port_buffer *buf;
Amit Shah2d24cda2011-09-14 13:06:45 +0530407 unsigned int err;
Amit Shah88f251a2009-12-21 22:15:30 +0530408
Amit Shahd7a62cd2011-03-04 14:04:33 +1030409 if (!port->portdev) {
410 /* Device has been unplugged. vqs are already gone. */
411 return;
412 }
Amit Shah2d24cda2011-09-14 13:06:45 +0530413 buf = get_inbuf(port);
Amit Shah88f251a2009-12-21 22:15:30 +0530414
Amit Shahce072a02011-09-14 13:06:44 +0530415 err = 0;
Amit Shahd6933562010-02-12 10:32:18 +0530416 while (buf) {
Amit Shah17e5b4f2011-09-14 13:06:46 +0530417 port->stats.bytes_discarded += buf->len - buf->offset;
Amit Shah2d24cda2011-09-14 13:06:45 +0530418 if (add_inbuf(port->in_vq, buf) < 0) {
Amit Shahce072a02011-09-14 13:06:44 +0530419 err++;
Amit Shahd6933562010-02-12 10:32:18 +0530420 free_buf(buf);
421 }
Amit Shah2d24cda2011-09-14 13:06:45 +0530422 port->inbuf = NULL;
423 buf = get_inbuf(port);
Amit Shah88f251a2009-12-21 22:15:30 +0530424 }
Amit Shahce072a02011-09-14 13:06:44 +0530425 if (err)
Amit Shahd6933562010-02-12 10:32:18 +0530426 dev_warn(port->dev, "Errors adding %d buffers back to vq\n",
Amit Shahce072a02011-09-14 13:06:44 +0530427 err);
Amit Shah88f251a2009-12-21 22:15:30 +0530428}
429
Amit Shah203baab2010-01-18 19:15:12 +0530430static bool port_has_data(struct port *port)
431{
432 unsigned long flags;
433 bool ret;
434
Amit Shahd6933562010-02-12 10:32:18 +0530435 ret = false;
Amit Shahd25a9dd2011-09-14 13:06:43 +0530436 spin_lock_irqsave(&port->inbuf_lock, flags);
437 port->inbuf = get_inbuf(port);
438 if (port->inbuf)
439 ret = true;
440
Amit Shah203baab2010-01-18 19:15:12 +0530441 spin_unlock_irqrestore(&port->inbuf_lock, flags);
Amit Shah203baab2010-01-18 19:15:12 +0530442 return ret;
443}
444
Amit Shah3425e702010-05-19 22:15:46 -0600445static ssize_t __send_control_msg(struct ports_device *portdev, u32 port_id,
446 unsigned int event, unsigned int value)
Amit Shah17634ba2009-12-21 21:03:25 +0530447{
448 struct scatterlist sg[1];
449 struct virtio_console_control cpkt;
450 struct virtqueue *vq;
Amit Shah604b2ad2010-02-24 10:36:51 +0530451 unsigned int len;
Amit Shah17634ba2009-12-21 21:03:25 +0530452
Amit Shah3425e702010-05-19 22:15:46 -0600453 if (!use_multiport(portdev))
Amit Shah17634ba2009-12-21 21:03:25 +0530454 return 0;
455
Amit Shah3425e702010-05-19 22:15:46 -0600456 cpkt.id = port_id;
Amit Shah17634ba2009-12-21 21:03:25 +0530457 cpkt.event = event;
458 cpkt.value = value;
459
Amit Shah3425e702010-05-19 22:15:46 -0600460 vq = portdev->c_ovq;
Amit Shah17634ba2009-12-21 21:03:25 +0530461
462 sg_init_one(sg, &cpkt, sizeof(cpkt));
Rusty Russellf96fde42012-01-12 15:44:42 +1030463 if (virtqueue_add_buf(vq, sg, 1, 0, &cpkt, GFP_ATOMIC) >= 0) {
Michael S. Tsirkin505b0452010-04-12 16:18:32 +0300464 virtqueue_kick(vq);
465 while (!virtqueue_get_buf(vq, &len))
Amit Shah17634ba2009-12-21 21:03:25 +0530466 cpu_relax();
467 }
468 return 0;
469}
470
Amit Shah3425e702010-05-19 22:15:46 -0600471static ssize_t send_control_msg(struct port *port, unsigned int event,
472 unsigned int value)
473{
Amit Shah84ec06c2010-09-02 18:11:42 +0530474 /* Did the port get unplugged before userspace closed it? */
475 if (port->portdev)
476 return __send_control_msg(port->portdev, port->id, event, value);
477 return 0;
Amit Shah3425e702010-05-19 22:15:46 -0600478}
479
Masami Hiramatsueb5e89f2012-08-09 21:30:39 +0900480struct buffer_token {
481 union {
482 void *buf;
483 struct scatterlist *sg;
484 } u;
485 bool sgpages;
486};
487
488static void reclaim_sg_pages(struct scatterlist *sg)
489{
490 int i;
491 struct page *page;
492
493 for (i = 0; i < MAX_SPLICE_PAGES; i++) {
494 page = sg_page(&sg[i]);
495 if (!page)
496 break;
497 put_page(page);
498 }
499 kfree(sg);
500}
501
Amit Shahcdfadfc2010-05-19 22:15:50 -0600502/* Callers must take the port->outvq_lock */
503static void reclaim_consumed_buffers(struct port *port)
504{
Masami Hiramatsueb5e89f2012-08-09 21:30:39 +0900505 struct buffer_token *tok;
Amit Shahcdfadfc2010-05-19 22:15:50 -0600506 unsigned int len;
507
Amit Shahd7a62cd2011-03-04 14:04:33 +1030508 if (!port->portdev) {
509 /* Device has been unplugged. vqs are already gone. */
510 return;
511 }
Masami Hiramatsueb5e89f2012-08-09 21:30:39 +0900512 while ((tok = virtqueue_get_buf(port->out_vq, &len))) {
513 if (tok->sgpages)
514 reclaim_sg_pages(tok->u.sg);
515 else
516 kfree(tok->u.buf);
517 kfree(tok);
Amit Shahcdfadfc2010-05-19 22:15:50 -0600518 port->outvq_full = false;
519 }
520}
521
Masami Hiramatsueb5e89f2012-08-09 21:30:39 +0900522static ssize_t __send_to_port(struct port *port, struct scatterlist *sg,
523 int nents, size_t in_count,
524 struct buffer_token *tok, bool nonblock)
Amit Shahf997f00b2009-12-21 17:28:51 +0530525{
Amit Shahf997f00b2009-12-21 17:28:51 +0530526 struct virtqueue *out_vq;
527 ssize_t ret;
Amit Shahcdfadfc2010-05-19 22:15:50 -0600528 unsigned long flags;
Amit Shahf997f00b2009-12-21 17:28:51 +0530529 unsigned int len;
530
531 out_vq = port->out_vq;
532
Amit Shahcdfadfc2010-05-19 22:15:50 -0600533 spin_lock_irqsave(&port->outvq_lock, flags);
534
535 reclaim_consumed_buffers(port);
536
Masami Hiramatsueb5e89f2012-08-09 21:30:39 +0900537 ret = virtqueue_add_buf(out_vq, sg, nents, 0, tok, GFP_ATOMIC);
Amit Shahf997f00b2009-12-21 17:28:51 +0530538
539 /* Tell Host to go! */
Michael S. Tsirkin505b0452010-04-12 16:18:32 +0300540 virtqueue_kick(out_vq);
Amit Shahf997f00b2009-12-21 17:28:51 +0530541
542 if (ret < 0) {
Rusty Russell9ff4cfa2010-04-08 09:46:16 -0600543 in_count = 0;
Amit Shahcdfadfc2010-05-19 22:15:50 -0600544 goto done;
Amit Shahf997f00b2009-12-21 17:28:51 +0530545 }
546
Amit Shahcdfadfc2010-05-19 22:15:50 -0600547 if (ret == 0)
548 port->outvq_full = true;
549
550 if (nonblock)
551 goto done;
552
553 /*
554 * Wait till the host acknowledges it pushed out the data we
Amit Shah531295e2010-10-20 13:45:43 +1030555 * sent. This is done for data from the hvc_console; the tty
556 * operations are performed with spinlocks held so we can't
557 * sleep here. An alternative would be to copy the data to a
558 * buffer and relax the spinning requirement. The downside is
559 * we need to kmalloc a GFP_ATOMIC buffer each time the
560 * console driver writes something out.
Amit Shahcdfadfc2010-05-19 22:15:50 -0600561 */
Michael S. Tsirkin505b0452010-04-12 16:18:32 +0300562 while (!virtqueue_get_buf(out_vq, &len))
Amit Shahf997f00b2009-12-21 17:28:51 +0530563 cpu_relax();
Amit Shahcdfadfc2010-05-19 22:15:50 -0600564done:
565 spin_unlock_irqrestore(&port->outvq_lock, flags);
Amit Shah17e5b4f2011-09-14 13:06:46 +0530566
567 port->stats.bytes_sent += in_count;
Amit Shahcdfadfc2010-05-19 22:15:50 -0600568 /*
569 * We're expected to return the amount of data we wrote -- all
570 * of it
571 */
Rusty Russell9ff4cfa2010-04-08 09:46:16 -0600572 return in_count;
Amit Shahf997f00b2009-12-21 17:28:51 +0530573}
574
Masami Hiramatsueb5e89f2012-08-09 21:30:39 +0900575static ssize_t send_buf(struct port *port, void *in_buf, size_t in_count,
576 bool nonblock)
577{
578 struct scatterlist sg[1];
579 struct buffer_token *tok;
580
581 tok = kmalloc(sizeof(*tok), GFP_ATOMIC);
582 if (!tok)
583 return -ENOMEM;
584 tok->sgpages = false;
585 tok->u.buf = in_buf;
586
587 sg_init_one(sg, in_buf, in_count);
588
589 return __send_to_port(port, sg, 1, in_count, tok, nonblock);
590}
591
592static ssize_t send_pages(struct port *port, struct scatterlist *sg, int nents,
593 size_t in_count, bool nonblock)
594{
595 struct buffer_token *tok;
596
597 tok = kmalloc(sizeof(*tok), GFP_ATOMIC);
598 if (!tok)
599 return -ENOMEM;
600 tok->sgpages = true;
601 tok->u.sg = sg;
602
603 return __send_to_port(port, sg, nents, in_count, tok, nonblock);
604}
605
Amit Shah203baab2010-01-18 19:15:12 +0530606/*
607 * Give out the data that's requested from the buffer that we have
608 * queued up.
609 */
Amit Shahb766cee2009-12-21 21:26:45 +0530610static ssize_t fill_readbuf(struct port *port, char *out_buf, size_t out_count,
611 bool to_user)
Amit Shah203baab2010-01-18 19:15:12 +0530612{
613 struct port_buffer *buf;
614 unsigned long flags;
615
616 if (!out_count || !port_has_data(port))
617 return 0;
618
619 buf = port->inbuf;
Amit Shahb766cee2009-12-21 21:26:45 +0530620 out_count = min(out_count, buf->len - buf->offset);
Amit Shah203baab2010-01-18 19:15:12 +0530621
Amit Shahb766cee2009-12-21 21:26:45 +0530622 if (to_user) {
623 ssize_t ret;
Amit Shah203baab2010-01-18 19:15:12 +0530624
Amit Shahb766cee2009-12-21 21:26:45 +0530625 ret = copy_to_user(out_buf, buf->buf + buf->offset, out_count);
626 if (ret)
627 return -EFAULT;
628 } else {
629 memcpy(out_buf, buf->buf + buf->offset, out_count);
630 }
631
Amit Shah203baab2010-01-18 19:15:12 +0530632 buf->offset += out_count;
633
634 if (buf->offset == buf->len) {
635 /*
636 * We're done using all the data in this buffer.
637 * Re-queue so that the Host can send us more data.
638 */
639 spin_lock_irqsave(&port->inbuf_lock, flags);
640 port->inbuf = NULL;
641
642 if (add_inbuf(port->in_vq, buf) < 0)
Amit Shahfb08bd22009-12-21 21:36:04 +0530643 dev_warn(port->dev, "failed add_buf\n");
Amit Shah203baab2010-01-18 19:15:12 +0530644
645 spin_unlock_irqrestore(&port->inbuf_lock, flags);
646 }
Amit Shahb766cee2009-12-21 21:26:45 +0530647 /* Return the number of bytes actually copied */
Amit Shah203baab2010-01-18 19:15:12 +0530648 return out_count;
Amit Shahe27b5192010-01-18 19:15:02 +0530649}
650
Amit Shah2030fa42009-12-21 21:49:30 +0530651/* The condition that must be true for polling to end */
Amit Shah60caacd2010-05-19 22:15:49 -0600652static bool will_read_block(struct port *port)
Amit Shah2030fa42009-12-21 21:49:30 +0530653{
Amit Shah3709ea72010-09-02 18:11:43 +0530654 if (!port->guest_connected) {
655 /* Port got hot-unplugged. Let's exit. */
656 return false;
657 }
Amit Shah60caacd2010-05-19 22:15:49 -0600658 return !port_has_data(port) && port->host_connected;
Amit Shah2030fa42009-12-21 21:49:30 +0530659}
660
Amit Shahcdfadfc2010-05-19 22:15:50 -0600661static bool will_write_block(struct port *port)
662{
663 bool ret;
664
Amit Shah60e5e0b2010-05-27 13:24:40 +0530665 if (!port->guest_connected) {
666 /* Port got hot-unplugged. Let's exit. */
667 return false;
668 }
Amit Shahcdfadfc2010-05-19 22:15:50 -0600669 if (!port->host_connected)
670 return true;
671
672 spin_lock_irq(&port->outvq_lock);
673 /*
674 * Check if the Host has consumed any buffers since we last
675 * sent data (this is only applicable for nonblocking ports).
676 */
677 reclaim_consumed_buffers(port);
678 ret = port->outvq_full;
679 spin_unlock_irq(&port->outvq_lock);
680
681 return ret;
682}
683
Amit Shah2030fa42009-12-21 21:49:30 +0530684static ssize_t port_fops_read(struct file *filp, char __user *ubuf,
685 size_t count, loff_t *offp)
686{
687 struct port *port;
688 ssize_t ret;
689
690 port = filp->private_data;
691
692 if (!port_has_data(port)) {
693 /*
694 * If nothing's connected on the host just return 0 in
695 * case of list_empty; this tells the userspace app
696 * that there's no connection
697 */
698 if (!port->host_connected)
699 return 0;
700 if (filp->f_flags & O_NONBLOCK)
701 return -EAGAIN;
702
Amit Shaha08fa922011-09-14 13:06:41 +0530703 ret = wait_event_freezable(port->waitqueue,
704 !will_read_block(port));
Amit Shah2030fa42009-12-21 21:49:30 +0530705 if (ret < 0)
706 return ret;
707 }
Amit Shahb3dddb92010-09-02 18:11:45 +0530708 /* Port got hot-unplugged. */
709 if (!port->guest_connected)
710 return -ENODEV;
Amit Shah2030fa42009-12-21 21:49:30 +0530711 /*
712 * We could've received a disconnection message while we were
713 * waiting for more data.
714 *
715 * This check is not clubbed in the if() statement above as we
716 * might receive some data as well as the host could get
717 * disconnected after we got woken up from our wait. So we
718 * really want to give off whatever data we have and only then
719 * check for host_connected.
720 */
721 if (!port_has_data(port) && !port->host_connected)
722 return 0;
723
724 return fill_readbuf(port, ubuf, count, true);
725}
726
727static ssize_t port_fops_write(struct file *filp, const char __user *ubuf,
728 size_t count, loff_t *offp)
729{
730 struct port *port;
731 char *buf;
732 ssize_t ret;
Amit Shahcdfadfc2010-05-19 22:15:50 -0600733 bool nonblock;
Amit Shah2030fa42009-12-21 21:49:30 +0530734
Amit Shah65745422010-09-14 13:26:16 +0530735 /* Userspace could be out to fool us */
736 if (!count)
737 return 0;
738
Amit Shah2030fa42009-12-21 21:49:30 +0530739 port = filp->private_data;
740
Amit Shahcdfadfc2010-05-19 22:15:50 -0600741 nonblock = filp->f_flags & O_NONBLOCK;
742
743 if (will_write_block(port)) {
744 if (nonblock)
745 return -EAGAIN;
746
Amit Shaha08fa922011-09-14 13:06:41 +0530747 ret = wait_event_freezable(port->waitqueue,
748 !will_write_block(port));
Amit Shahcdfadfc2010-05-19 22:15:50 -0600749 if (ret < 0)
750 return ret;
751 }
Amit Shahf4028112010-09-02 18:11:46 +0530752 /* Port got hot-unplugged. */
753 if (!port->guest_connected)
754 return -ENODEV;
Amit Shahcdfadfc2010-05-19 22:15:50 -0600755
Amit Shah2030fa42009-12-21 21:49:30 +0530756 count = min((size_t)(32 * 1024), count);
757
758 buf = kmalloc(count, GFP_KERNEL);
759 if (!buf)
760 return -ENOMEM;
761
762 ret = copy_from_user(buf, ubuf, count);
763 if (ret) {
764 ret = -EFAULT;
765 goto free_buf;
766 }
767
Amit Shah531295e2010-10-20 13:45:43 +1030768 /*
769 * We now ask send_buf() to not spin for generic ports -- we
770 * can re-use the same code path that non-blocking file
771 * descriptors take for blocking file descriptors since the
772 * wait is already done and we're certain the write will go
773 * through to the host.
774 */
775 nonblock = true;
Amit Shahcdfadfc2010-05-19 22:15:50 -0600776 ret = send_buf(port, buf, count, nonblock);
777
778 if (nonblock && ret > 0)
779 goto out;
780
Amit Shah2030fa42009-12-21 21:49:30 +0530781free_buf:
782 kfree(buf);
Amit Shahcdfadfc2010-05-19 22:15:50 -0600783out:
Amit Shah2030fa42009-12-21 21:49:30 +0530784 return ret;
785}
786
Masami Hiramatsueb5e89f2012-08-09 21:30:39 +0900787struct sg_list {
788 unsigned int n;
789 size_t len;
790 struct scatterlist *sg;
791};
792
793static int pipe_to_sg(struct pipe_inode_info *pipe, struct pipe_buffer *buf,
794 struct splice_desc *sd)
795{
796 struct sg_list *sgl = sd->u.data;
797 unsigned int len = 0;
798
799 if (sgl->n == MAX_SPLICE_PAGES)
800 return 0;
801
802 /* Try lock this page */
803 if (buf->ops->steal(pipe, buf) == 0) {
804 /* Get reference and unlock page for moving */
805 get_page(buf->page);
806 unlock_page(buf->page);
807
808 len = min(buf->len, sd->len);
809 sg_set_page(&(sgl->sg[sgl->n]), buf->page, len, buf->offset);
810 sgl->n++;
811 sgl->len += len;
812 }
813
814 return len;
815}
816
817/* Faster zero-copy write by splicing */
818static ssize_t port_fops_splice_write(struct pipe_inode_info *pipe,
819 struct file *filp, loff_t *ppos,
820 size_t len, unsigned int flags)
821{
822 struct port *port = filp->private_data;
823 struct sg_list sgl;
824 ssize_t ret;
825 struct splice_desc sd = {
826 .total_len = len,
827 .flags = flags,
828 .pos = *ppos,
829 .u.data = &sgl,
830 };
831
832 sgl.n = 0;
833 sgl.len = 0;
834 sgl.sg = kmalloc(sizeof(struct scatterlist) * MAX_SPLICE_PAGES,
835 GFP_KERNEL);
836 if (unlikely(!sgl.sg))
837 return -ENOMEM;
838
839 sg_init_table(sgl.sg, MAX_SPLICE_PAGES);
840 ret = __splice_from_pipe(pipe, &sd, pipe_to_sg);
841 if (likely(ret > 0))
842 ret = send_pages(port, sgl.sg, sgl.n, sgl.len, true);
843
844 return ret;
845}
846
Amit Shah2030fa42009-12-21 21:49:30 +0530847static unsigned int port_fops_poll(struct file *filp, poll_table *wait)
848{
849 struct port *port;
850 unsigned int ret;
851
852 port = filp->private_data;
853 poll_wait(filp, &port->waitqueue, wait);
854
Amit Shah8529a502010-09-02 18:11:44 +0530855 if (!port->guest_connected) {
856 /* Port got unplugged */
857 return POLLHUP;
858 }
Amit Shah2030fa42009-12-21 21:49:30 +0530859 ret = 0;
Hans de Goede6df7aad2010-09-16 14:43:08 +0530860 if (!will_read_block(port))
Amit Shah2030fa42009-12-21 21:49:30 +0530861 ret |= POLLIN | POLLRDNORM;
Amit Shahcdfadfc2010-05-19 22:15:50 -0600862 if (!will_write_block(port))
Amit Shah2030fa42009-12-21 21:49:30 +0530863 ret |= POLLOUT;
864 if (!port->host_connected)
865 ret |= POLLHUP;
866
867 return ret;
868}
869
Amit Shahb353a6b2010-09-02 18:38:29 +0530870static void remove_port(struct kref *kref);
871
Amit Shah2030fa42009-12-21 21:49:30 +0530872static int port_fops_release(struct inode *inode, struct file *filp)
873{
874 struct port *port;
875
876 port = filp->private_data;
877
878 /* Notify host of port being closed */
879 send_control_msg(port, VIRTIO_CONSOLE_PORT_OPEN, 0);
880
Amit Shah88f251a2009-12-21 22:15:30 +0530881 spin_lock_irq(&port->inbuf_lock);
Amit Shah3c7969c2009-11-26 11:25:38 +0530882 port->guest_connected = false;
883
Amit Shah88f251a2009-12-21 22:15:30 +0530884 discard_port_data(port);
885
886 spin_unlock_irq(&port->inbuf_lock);
887
Amit Shahcdfadfc2010-05-19 22:15:50 -0600888 spin_lock_irq(&port->outvq_lock);
889 reclaim_consumed_buffers(port);
890 spin_unlock_irq(&port->outvq_lock);
891
Amit Shahb353a6b2010-09-02 18:38:29 +0530892 /*
893 * Locks aren't necessary here as a port can't be opened after
894 * unplug, and if a port isn't unplugged, a kref would already
895 * exist for the port. Plus, taking ports_lock here would
896 * create a dependency on other locks taken by functions
897 * inside remove_port if we're the last holder of the port,
898 * creating many problems.
899 */
900 kref_put(&port->kref, remove_port);
901
Amit Shah2030fa42009-12-21 21:49:30 +0530902 return 0;
903}
904
905static int port_fops_open(struct inode *inode, struct file *filp)
906{
907 struct cdev *cdev = inode->i_cdev;
908 struct port *port;
Amit Shah8ad37e82010-09-02 18:11:48 +0530909 int ret;
Amit Shah2030fa42009-12-21 21:49:30 +0530910
Amit Shah04950cd2010-09-02 18:20:58 +0530911 port = find_port_by_devt(cdev->dev);
Amit Shah2030fa42009-12-21 21:49:30 +0530912 filp->private_data = port;
913
Amit Shahb353a6b2010-09-02 18:38:29 +0530914 /* Prevent against a port getting hot-unplugged at the same time */
915 spin_lock_irq(&port->portdev->ports_lock);
916 kref_get(&port->kref);
917 spin_unlock_irq(&port->portdev->ports_lock);
918
Amit Shah2030fa42009-12-21 21:49:30 +0530919 /*
920 * Don't allow opening of console port devices -- that's done
921 * via /dev/hvc
922 */
Amit Shah8ad37e82010-09-02 18:11:48 +0530923 if (is_console_port(port)) {
924 ret = -ENXIO;
925 goto out;
926 }
Amit Shah2030fa42009-12-21 21:49:30 +0530927
Amit Shah3c7969c2009-11-26 11:25:38 +0530928 /* Allow only one process to open a particular port at a time */
929 spin_lock_irq(&port->inbuf_lock);
930 if (port->guest_connected) {
931 spin_unlock_irq(&port->inbuf_lock);
Amit Shah8ad37e82010-09-02 18:11:48 +0530932 ret = -EMFILE;
933 goto out;
Amit Shah3c7969c2009-11-26 11:25:38 +0530934 }
935
936 port->guest_connected = true;
937 spin_unlock_irq(&port->inbuf_lock);
938
Amit Shahcdfadfc2010-05-19 22:15:50 -0600939 spin_lock_irq(&port->outvq_lock);
940 /*
941 * There might be a chance that we missed reclaiming a few
942 * buffers in the window of the port getting previously closed
943 * and opening now.
944 */
945 reclaim_consumed_buffers(port);
946 spin_unlock_irq(&port->outvq_lock);
947
Amit Shah299fb612010-09-16 14:43:09 +0530948 nonseekable_open(inode, filp);
949
Amit Shah2030fa42009-12-21 21:49:30 +0530950 /* Notify host of port being opened */
951 send_control_msg(filp->private_data, VIRTIO_CONSOLE_PORT_OPEN, 1);
952
953 return 0;
Amit Shah8ad37e82010-09-02 18:11:48 +0530954out:
Amit Shahb353a6b2010-09-02 18:38:29 +0530955 kref_put(&port->kref, remove_port);
Amit Shah8ad37e82010-09-02 18:11:48 +0530956 return ret;
Amit Shah2030fa42009-12-21 21:49:30 +0530957}
958
Amit Shah3eae0ad2010-09-02 18:47:52 +0530959static int port_fops_fasync(int fd, struct file *filp, int mode)
960{
961 struct port *port;
962
963 port = filp->private_data;
964 return fasync_helper(fd, filp, mode, &port->async_queue);
965}
966
Amit Shah2030fa42009-12-21 21:49:30 +0530967/*
968 * The file operations that we support: programs in the guest can open
969 * a console device, read from it, write to it, poll for data and
970 * close it. The devices are at
971 * /dev/vport<device number>p<port number>
972 */
973static const struct file_operations port_fops = {
974 .owner = THIS_MODULE,
975 .open = port_fops_open,
976 .read = port_fops_read,
977 .write = port_fops_write,
Masami Hiramatsueb5e89f2012-08-09 21:30:39 +0900978 .splice_write = port_fops_splice_write,
Amit Shah2030fa42009-12-21 21:49:30 +0530979 .poll = port_fops_poll,
980 .release = port_fops_release,
Amit Shah3eae0ad2010-09-02 18:47:52 +0530981 .fasync = port_fops_fasync,
Amit Shah299fb612010-09-16 14:43:09 +0530982 .llseek = no_llseek,
Amit Shah2030fa42009-12-21 21:49:30 +0530983};
984
Amit Shahe27b5192010-01-18 19:15:02 +0530985/*
Rusty Russella23ea922010-01-18 19:14:55 +0530986 * The put_chars() callback is pretty straightforward.
Rusty Russell31610432007-10-22 11:03:39 +1000987 *
Rusty Russella23ea922010-01-18 19:14:55 +0530988 * We turn the characters into a scatter-gather list, add it to the
989 * output queue and then kick the Host. Then we sit here waiting for
990 * it to finish: inefficient in theory, but in practice
991 * implementations will do it immediately (lguest's Launcher does).
992 */
Rusty Russell31610432007-10-22 11:03:39 +1000993static int put_chars(u32 vtermno, const char *buf, int count)
994{
Rusty Russell21206ed2010-01-18 19:15:00 +0530995 struct port *port;
Amit Shah38edf582010-01-18 19:15:05 +0530996
François Diakhaté162a6892010-03-23 18:23:15 +0530997 if (unlikely(early_put_chars))
998 return early_put_chars(vtermno, buf, count);
999
Amit Shah38edf582010-01-18 19:15:05 +05301000 port = find_port_by_vtermno(vtermno);
1001 if (!port)
Amit Shah6dc69f972010-05-19 22:15:47 -06001002 return -EPIPE;
Rusty Russell31610432007-10-22 11:03:39 +10001003
Amit Shahcdfadfc2010-05-19 22:15:50 -06001004 return send_buf(port, (void *)buf, count, false);
Rusty Russell31610432007-10-22 11:03:39 +10001005}
1006
Rusty Russella23ea922010-01-18 19:14:55 +05301007/*
Rusty Russella23ea922010-01-18 19:14:55 +05301008 * get_chars() is the callback from the hvc_console infrastructure
1009 * when an interrupt is received.
Rusty Russell31610432007-10-22 11:03:39 +10001010 *
Amit Shah203baab2010-01-18 19:15:12 +05301011 * We call out to fill_readbuf that gets us the required data from the
1012 * buffers that are queued up.
Rusty Russella23ea922010-01-18 19:14:55 +05301013 */
Rusty Russell31610432007-10-22 11:03:39 +10001014static int get_chars(u32 vtermno, char *buf, int count)
1015{
Rusty Russell21206ed2010-01-18 19:15:00 +05301016 struct port *port;
Rusty Russell31610432007-10-22 11:03:39 +10001017
Amit Shah6dc69f972010-05-19 22:15:47 -06001018 /* If we've not set up the port yet, we have no input to give. */
1019 if (unlikely(early_put_chars))
1020 return 0;
1021
Amit Shah38edf582010-01-18 19:15:05 +05301022 port = find_port_by_vtermno(vtermno);
1023 if (!port)
Amit Shah6dc69f972010-05-19 22:15:47 -06001024 return -EPIPE;
Rusty Russell21206ed2010-01-18 19:15:00 +05301025
1026 /* If we don't have an input queue yet, we can't get input. */
1027 BUG_ON(!port->in_vq);
1028
Amit Shahb766cee2009-12-21 21:26:45 +05301029 return fill_readbuf(port, buf, count, false);
Rusty Russell31610432007-10-22 11:03:39 +10001030}
Rusty Russell31610432007-10-22 11:03:39 +10001031
Amit Shahcb06e362010-01-18 19:15:08 +05301032static void resize_console(struct port *port)
Christian Borntraegerc2983452008-11-25 13:36:26 +01001033{
Amit Shahcb06e362010-01-18 19:15:08 +05301034 struct virtio_device *vdev;
Christian Borntraegerc2983452008-11-25 13:36:26 +01001035
Amit Shah2de16a42010-03-19 17:36:44 +05301036 /* The port could have been hot-unplugged */
Amit Shah97788292010-05-06 02:05:08 +05301037 if (!port || !is_console_port(port))
Amit Shah2de16a42010-03-19 17:36:44 +05301038 return;
1039
Amit Shahcb06e362010-01-18 19:15:08 +05301040 vdev = port->portdev->vdev;
Amit Shah97788292010-05-06 02:05:08 +05301041 if (virtio_has_feature(vdev, VIRTIO_CONSOLE_F_SIZE))
1042 hvc_resize(port->cons.hvc, port->cons.ws);
Christian Borntraegerc2983452008-11-25 13:36:26 +01001043}
1044
Amit Shah38edf582010-01-18 19:15:05 +05301045/* We set the configuration at this point, since we now have a tty */
Christian Borntraeger91fcad12008-06-20 15:24:15 +02001046static int notifier_add_vio(struct hvc_struct *hp, int data)
1047{
Amit Shah38edf582010-01-18 19:15:05 +05301048 struct port *port;
1049
1050 port = find_port_by_vtermno(hp->vtermno);
1051 if (!port)
1052 return -EINVAL;
1053
Christian Borntraeger91fcad12008-06-20 15:24:15 +02001054 hp->irq_requested = 1;
Amit Shahcb06e362010-01-18 19:15:08 +05301055 resize_console(port);
Christian Borntraegerc2983452008-11-25 13:36:26 +01001056
Christian Borntraeger91fcad12008-06-20 15:24:15 +02001057 return 0;
1058}
1059
1060static void notifier_del_vio(struct hvc_struct *hp, int data)
1061{
1062 hp->irq_requested = 0;
1063}
1064
Amit Shah17634ba2009-12-21 21:03:25 +05301065/* The operations for console ports. */
Rusty Russell1dff3992009-11-28 12:20:26 +05301066static const struct hv_ops hv_ops = {
Rusty Russell971f3392010-01-18 19:14:56 +05301067 .get_chars = get_chars,
1068 .put_chars = put_chars,
1069 .notifier_add = notifier_add_vio,
1070 .notifier_del = notifier_del_vio,
1071 .notifier_hangup = notifier_del_vio,
1072};
1073
1074/*
1075 * Console drivers are initialized very early so boot messages can go
1076 * out, so we do things slightly differently from the generic virtio
1077 * initialization of the net and block drivers.
1078 *
1079 * At this stage, the console is output-only. It's too early to set
1080 * up a virtqueue, so we let the drivers do some boutique early-output
1081 * thing.
1082 */
1083int __init virtio_cons_early_init(int (*put_chars)(u32, const char *, int))
1084{
1085 early_put_chars = put_chars;
1086 return hvc_instantiate(0, 0, &hv_ops);
1087}
1088
Amit Shah17634ba2009-12-21 21:03:25 +05301089int init_port_console(struct port *port)
Amit Shahcfa6d372010-01-18 19:15:10 +05301090{
1091 int ret;
1092
1093 /*
1094 * The Host's telling us this port is a console port. Hook it
1095 * up with an hvc console.
1096 *
1097 * To set up and manage our virtual console, we call
1098 * hvc_alloc().
1099 *
1100 * The first argument of hvc_alloc() is the virtual console
1101 * number. The second argument is the parameter for the
1102 * notification mechanism (like irq number). We currently
1103 * leave this as zero, virtqueues have implicit notifications.
1104 *
1105 * The third argument is a "struct hv_ops" containing the
1106 * put_chars() get_chars(), notifier_add() and notifier_del()
1107 * pointers. The final argument is the output buffer size: we
1108 * can do any size, so we put PAGE_SIZE here.
1109 */
1110 port->cons.vtermno = pdrvdata.next_vtermno;
1111
1112 port->cons.hvc = hvc_alloc(port->cons.vtermno, 0, &hv_ops, PAGE_SIZE);
1113 if (IS_ERR(port->cons.hvc)) {
1114 ret = PTR_ERR(port->cons.hvc);
Amit Shah298add72010-01-18 16:35:23 +05301115 dev_err(port->dev,
1116 "error %d allocating hvc for port\n", ret);
Amit Shahcfa6d372010-01-18 19:15:10 +05301117 port->cons.hvc = NULL;
1118 return ret;
1119 }
1120 spin_lock_irq(&pdrvdata_lock);
1121 pdrvdata.next_vtermno++;
1122 list_add_tail(&port->cons.list, &pdrvdata.consoles);
1123 spin_unlock_irq(&pdrvdata_lock);
Amit Shah3c7969c2009-11-26 11:25:38 +05301124 port->guest_connected = true;
Amit Shahcfa6d372010-01-18 19:15:10 +05301125
Amit Shah1d051602010-05-19 22:15:49 -06001126 /*
1127 * Start using the new console output if this is the first
1128 * console to come up.
1129 */
1130 if (early_put_chars)
1131 early_put_chars = NULL;
1132
Amit Shah2030fa42009-12-21 21:49:30 +05301133 /* Notify host of port being opened */
1134 send_control_msg(port, VIRTIO_CONSOLE_PORT_OPEN, 1);
1135
Amit Shahcfa6d372010-01-18 19:15:10 +05301136 return 0;
1137}
1138
Amit Shah431edb82009-12-21 21:57:40 +05301139static ssize_t show_port_name(struct device *dev,
1140 struct device_attribute *attr, char *buffer)
1141{
1142 struct port *port;
1143
1144 port = dev_get_drvdata(dev);
1145
1146 return sprintf(buffer, "%s\n", port->name);
1147}
1148
1149static DEVICE_ATTR(name, S_IRUGO, show_port_name, NULL);
1150
1151static struct attribute *port_sysfs_entries[] = {
1152 &dev_attr_name.attr,
1153 NULL
1154};
1155
1156static struct attribute_group port_attribute_group = {
1157 .name = NULL, /* put in device directory */
1158 .attrs = port_sysfs_entries,
1159};
1160
Amit Shahd99393e2009-12-21 22:36:21 +05301161static ssize_t debugfs_read(struct file *filp, char __user *ubuf,
1162 size_t count, loff_t *offp)
1163{
1164 struct port *port;
1165 char *buf;
1166 ssize_t ret, out_offset, out_count;
1167
1168 out_count = 1024;
1169 buf = kmalloc(out_count, GFP_KERNEL);
1170 if (!buf)
1171 return -ENOMEM;
1172
1173 port = filp->private_data;
1174 out_offset = 0;
1175 out_offset += snprintf(buf + out_offset, out_count,
1176 "name: %s\n", port->name ? port->name : "");
1177 out_offset += snprintf(buf + out_offset, out_count - out_offset,
1178 "guest_connected: %d\n", port->guest_connected);
1179 out_offset += snprintf(buf + out_offset, out_count - out_offset,
1180 "host_connected: %d\n", port->host_connected);
1181 out_offset += snprintf(buf + out_offset, out_count - out_offset,
Amit Shahcdfadfc2010-05-19 22:15:50 -06001182 "outvq_full: %d\n", port->outvq_full);
1183 out_offset += snprintf(buf + out_offset, out_count - out_offset,
Amit Shah17e5b4f2011-09-14 13:06:46 +05301184 "bytes_sent: %lu\n", port->stats.bytes_sent);
1185 out_offset += snprintf(buf + out_offset, out_count - out_offset,
1186 "bytes_received: %lu\n",
1187 port->stats.bytes_received);
1188 out_offset += snprintf(buf + out_offset, out_count - out_offset,
1189 "bytes_discarded: %lu\n",
1190 port->stats.bytes_discarded);
1191 out_offset += snprintf(buf + out_offset, out_count - out_offset,
Amit Shahd99393e2009-12-21 22:36:21 +05301192 "is_console: %s\n",
1193 is_console_port(port) ? "yes" : "no");
1194 out_offset += snprintf(buf + out_offset, out_count - out_offset,
1195 "console_vtermno: %u\n", port->cons.vtermno);
1196
1197 ret = simple_read_from_buffer(ubuf, count, offp, buf, out_offset);
1198 kfree(buf);
1199 return ret;
1200}
1201
1202static const struct file_operations port_debugfs_ops = {
1203 .owner = THIS_MODULE,
Stephen Boyd234e3402012-04-05 14:25:11 -07001204 .open = simple_open,
Amit Shahd99393e2009-12-21 22:36:21 +05301205 .read = debugfs_read,
1206};
1207
Amit Shah97788292010-05-06 02:05:08 +05301208static void set_console_size(struct port *port, u16 rows, u16 cols)
1209{
1210 if (!port || !is_console_port(port))
1211 return;
1212
1213 port->cons.ws.ws_row = rows;
1214 port->cons.ws.ws_col = cols;
1215}
1216
Amit Shahc446f8f2010-05-19 22:15:48 -06001217static unsigned int fill_queue(struct virtqueue *vq, spinlock_t *lock)
1218{
1219 struct port_buffer *buf;
1220 unsigned int nr_added_bufs;
1221 int ret;
1222
1223 nr_added_bufs = 0;
1224 do {
1225 buf = alloc_buf(PAGE_SIZE);
1226 if (!buf)
1227 break;
1228
1229 spin_lock_irq(lock);
1230 ret = add_inbuf(vq, buf);
1231 if (ret < 0) {
1232 spin_unlock_irq(lock);
1233 free_buf(buf);
1234 break;
1235 }
1236 nr_added_bufs++;
1237 spin_unlock_irq(lock);
1238 } while (ret > 0);
1239
1240 return nr_added_bufs;
1241}
1242
Amit Shah3eae0ad2010-09-02 18:47:52 +05301243static void send_sigio_to_port(struct port *port)
1244{
1245 if (port->async_queue && port->guest_connected)
1246 kill_fasync(&port->async_queue, SIGIO, POLL_OUT);
1247}
1248
Amit Shahc446f8f2010-05-19 22:15:48 -06001249static int add_port(struct ports_device *portdev, u32 id)
1250{
1251 char debugfs_name[16];
1252 struct port *port;
1253 struct port_buffer *buf;
1254 dev_t devt;
1255 unsigned int nr_added_bufs;
1256 int err;
1257
1258 port = kmalloc(sizeof(*port), GFP_KERNEL);
1259 if (!port) {
1260 err = -ENOMEM;
1261 goto fail;
1262 }
Amit Shahb353a6b2010-09-02 18:38:29 +05301263 kref_init(&port->kref);
Amit Shahc446f8f2010-05-19 22:15:48 -06001264
1265 port->portdev = portdev;
1266 port->id = id;
1267
1268 port->name = NULL;
1269 port->inbuf = NULL;
1270 port->cons.hvc = NULL;
Amit Shah3eae0ad2010-09-02 18:47:52 +05301271 port->async_queue = NULL;
Amit Shahc446f8f2010-05-19 22:15:48 -06001272
Amit Shah97788292010-05-06 02:05:08 +05301273 port->cons.ws.ws_row = port->cons.ws.ws_col = 0;
1274
Amit Shahc446f8f2010-05-19 22:15:48 -06001275 port->host_connected = port->guest_connected = false;
Amit Shah17e5b4f2011-09-14 13:06:46 +05301276 port->stats = (struct port_stats) { 0 };
Amit Shahc446f8f2010-05-19 22:15:48 -06001277
Amit Shahcdfadfc2010-05-19 22:15:50 -06001278 port->outvq_full = false;
1279
Amit Shahc446f8f2010-05-19 22:15:48 -06001280 port->in_vq = portdev->in_vqs[port->id];
1281 port->out_vq = portdev->out_vqs[port->id];
1282
Amit Shahd22a6982010-09-02 18:20:59 +05301283 port->cdev = cdev_alloc();
1284 if (!port->cdev) {
1285 dev_err(&port->portdev->vdev->dev, "Error allocating cdev\n");
1286 err = -ENOMEM;
1287 goto free_port;
1288 }
1289 port->cdev->ops = &port_fops;
Amit Shahc446f8f2010-05-19 22:15:48 -06001290
1291 devt = MKDEV(portdev->chr_major, id);
Amit Shahd22a6982010-09-02 18:20:59 +05301292 err = cdev_add(port->cdev, devt, 1);
Amit Shahc446f8f2010-05-19 22:15:48 -06001293 if (err < 0) {
1294 dev_err(&port->portdev->vdev->dev,
1295 "Error %d adding cdev for port %u\n", err, id);
Amit Shahd22a6982010-09-02 18:20:59 +05301296 goto free_cdev;
Amit Shahc446f8f2010-05-19 22:15:48 -06001297 }
1298 port->dev = device_create(pdrvdata.class, &port->portdev->vdev->dev,
1299 devt, port, "vport%up%u",
1300 port->portdev->drv_index, id);
1301 if (IS_ERR(port->dev)) {
1302 err = PTR_ERR(port->dev);
1303 dev_err(&port->portdev->vdev->dev,
1304 "Error %d creating device for port %u\n",
1305 err, id);
1306 goto free_cdev;
1307 }
1308
1309 spin_lock_init(&port->inbuf_lock);
Amit Shahcdfadfc2010-05-19 22:15:50 -06001310 spin_lock_init(&port->outvq_lock);
Amit Shahc446f8f2010-05-19 22:15:48 -06001311 init_waitqueue_head(&port->waitqueue);
1312
1313 /* Fill the in_vq with buffers so the host can send us data. */
1314 nr_added_bufs = fill_queue(port->in_vq, &port->inbuf_lock);
1315 if (!nr_added_bufs) {
1316 dev_err(port->dev, "Error allocating inbufs\n");
1317 err = -ENOMEM;
1318 goto free_device;
1319 }
1320
1321 /*
1322 * If we're not using multiport support, this has to be a console port
1323 */
1324 if (!use_multiport(port->portdev)) {
1325 err = init_port_console(port);
1326 if (err)
1327 goto free_inbufs;
1328 }
1329
1330 spin_lock_irq(&portdev->ports_lock);
1331 list_add_tail(&port->list, &port->portdev->ports);
1332 spin_unlock_irq(&portdev->ports_lock);
1333
1334 /*
1335 * Tell the Host we're set so that it can send us various
1336 * configuration parameters for this port (eg, port name,
1337 * caching, whether this is a console port, etc.)
1338 */
1339 send_control_msg(port, VIRTIO_CONSOLE_PORT_READY, 1);
1340
1341 if (pdrvdata.debugfs_dir) {
1342 /*
1343 * Finally, create the debugfs file that we can use to
1344 * inspect a port's state at any time
1345 */
1346 sprintf(debugfs_name, "vport%up%u",
1347 port->portdev->drv_index, id);
1348 port->debugfs_file = debugfs_create_file(debugfs_name, 0444,
1349 pdrvdata.debugfs_dir,
1350 port,
1351 &port_debugfs_ops);
1352 }
1353 return 0;
1354
1355free_inbufs:
1356 while ((buf = virtqueue_detach_unused_buf(port->in_vq)))
1357 free_buf(buf);
1358free_device:
1359 device_destroy(pdrvdata.class, port->dev->devt);
1360free_cdev:
Amit Shahd22a6982010-09-02 18:20:59 +05301361 cdev_del(port->cdev);
Amit Shahc446f8f2010-05-19 22:15:48 -06001362free_port:
1363 kfree(port);
1364fail:
1365 /* The host might want to notify management sw about port add failure */
Julia Lawall0643e4c2010-05-15 11:45:53 +02001366 __send_control_msg(portdev, id, VIRTIO_CONSOLE_PORT_READY, 0);
Amit Shahc446f8f2010-05-19 22:15:48 -06001367 return err;
1368}
1369
Amit Shahb353a6b2010-09-02 18:38:29 +05301370/* No users remain, remove all port-specific data. */
1371static void remove_port(struct kref *kref)
1372{
1373 struct port *port;
1374
1375 port = container_of(kref, struct port, kref);
1376
1377 sysfs_remove_group(&port->dev->kobj, &port_attribute_group);
1378 device_destroy(pdrvdata.class, port->dev->devt);
1379 cdev_del(port->cdev);
1380
1381 kfree(port->name);
1382
1383 debugfs_remove(port->debugfs_file);
1384
1385 kfree(port);
1386}
1387
Amit Shaha0e2dbf2011-12-22 16:58:27 +05301388static void remove_port_data(struct port *port)
1389{
1390 struct port_buffer *buf;
1391
1392 /* Remove unused data this port might have received. */
1393 discard_port_data(port);
1394
1395 reclaim_consumed_buffers(port);
1396
1397 /* Remove buffers we queued up for the Host to send us data in. */
1398 while ((buf = virtqueue_detach_unused_buf(port->in_vq)))
1399 free_buf(buf);
1400}
1401
Amit Shahb353a6b2010-09-02 18:38:29 +05301402/*
1403 * Port got unplugged. Remove port from portdev's list and drop the
1404 * kref reference. If no userspace has this port opened, it will
1405 * result in immediate removal the port.
1406 */
1407static void unplug_port(struct port *port)
Amit Shah1f7aa422009-12-21 22:27:31 +05301408{
Amit Shahb353a6b2010-09-02 18:38:29 +05301409 spin_lock_irq(&port->portdev->ports_lock);
1410 list_del(&port->list);
1411 spin_unlock_irq(&port->portdev->ports_lock);
1412
Amit Shah00476342010-05-27 13:24:39 +05301413 if (port->guest_connected) {
1414 port->guest_connected = false;
1415 port->host_connected = false;
1416 wake_up_interruptible(&port->waitqueue);
Amit Shaha461e112010-09-02 18:47:54 +05301417
1418 /* Let the app know the port is going down. */
1419 send_sigio_to_port(port);
Amit Shah00476342010-05-27 13:24:39 +05301420 }
1421
Amit Shah1f7aa422009-12-21 22:27:31 +05301422 if (is_console_port(port)) {
1423 spin_lock_irq(&pdrvdata_lock);
1424 list_del(&port->cons.list);
1425 spin_unlock_irq(&pdrvdata_lock);
1426 hvc_remove(port->cons.hvc);
1427 }
Amit Shah1f7aa422009-12-21 22:27:31 +05301428
Amit Shaha0e2dbf2011-12-22 16:58:27 +05301429 remove_port_data(port);
Amit Shaha9cdd482010-02-12 10:32:15 +05301430
Amit Shahb353a6b2010-09-02 18:38:29 +05301431 /*
1432 * We should just assume the device itself has gone off --
1433 * else a close on an open port later will try to send out a
1434 * control message.
1435 */
1436 port->portdev = NULL;
Amit Shah1f7aa422009-12-21 22:27:31 +05301437
Amit Shahb353a6b2010-09-02 18:38:29 +05301438 /*
1439 * Locks around here are not necessary - a port can't be
1440 * opened after we removed the port struct from ports_list
1441 * above.
1442 */
1443 kref_put(&port->kref, remove_port);
Amit Shah1f7aa422009-12-21 22:27:31 +05301444}
1445
Amit Shah17634ba2009-12-21 21:03:25 +05301446/* Any private messages that the Host and Guest want to share */
1447static void handle_control_message(struct ports_device *portdev,
1448 struct port_buffer *buf)
1449{
1450 struct virtio_console_control *cpkt;
1451 struct port *port;
Amit Shah431edb82009-12-21 21:57:40 +05301452 size_t name_size;
1453 int err;
Amit Shah17634ba2009-12-21 21:03:25 +05301454
1455 cpkt = (struct virtio_console_control *)(buf->buf + buf->offset);
1456
1457 port = find_port_by_id(portdev, cpkt->id);
Amit Shahf909f852010-05-19 22:15:48 -06001458 if (!port && cpkt->event != VIRTIO_CONSOLE_PORT_ADD) {
Amit Shah17634ba2009-12-21 21:03:25 +05301459 /* No valid header at start of buffer. Drop it. */
1460 dev_dbg(&portdev->vdev->dev,
1461 "Invalid index %u in control packet\n", cpkt->id);
1462 return;
1463 }
1464
1465 switch (cpkt->event) {
Amit Shahf909f852010-05-19 22:15:48 -06001466 case VIRTIO_CONSOLE_PORT_ADD:
1467 if (port) {
Amit Shah1d051602010-05-19 22:15:49 -06001468 dev_dbg(&portdev->vdev->dev,
1469 "Port %u already added\n", port->id);
Amit Shahf909f852010-05-19 22:15:48 -06001470 send_control_msg(port, VIRTIO_CONSOLE_PORT_READY, 1);
1471 break;
1472 }
1473 if (cpkt->id >= portdev->config.max_nr_ports) {
1474 dev_warn(&portdev->vdev->dev,
1475 "Request for adding port with out-of-bound id %u, max. supported id: %u\n",
1476 cpkt->id, portdev->config.max_nr_ports - 1);
1477 break;
1478 }
1479 add_port(portdev, cpkt->id);
1480 break;
1481 case VIRTIO_CONSOLE_PORT_REMOVE:
Amit Shahb353a6b2010-09-02 18:38:29 +05301482 unplug_port(port);
Amit Shahf909f852010-05-19 22:15:48 -06001483 break;
Amit Shah17634ba2009-12-21 21:03:25 +05301484 case VIRTIO_CONSOLE_CONSOLE_PORT:
1485 if (!cpkt->value)
1486 break;
1487 if (is_console_port(port))
1488 break;
1489
1490 init_port_console(port);
Christian Borntraeger5e384832011-09-22 23:44:23 +05301491 complete(&early_console_added);
Amit Shah17634ba2009-12-21 21:03:25 +05301492 /*
1493 * Could remove the port here in case init fails - but
1494 * have to notify the host first.
1495 */
1496 break;
Amit Shah8345adb2010-05-06 02:05:09 +05301497 case VIRTIO_CONSOLE_RESIZE: {
1498 struct {
1499 __u16 rows;
1500 __u16 cols;
1501 } size;
1502
Amit Shah17634ba2009-12-21 21:03:25 +05301503 if (!is_console_port(port))
1504 break;
Amit Shah8345adb2010-05-06 02:05:09 +05301505
1506 memcpy(&size, buf->buf + buf->offset + sizeof(*cpkt),
1507 sizeof(size));
1508 set_console_size(port, size.rows, size.cols);
1509
Amit Shah17634ba2009-12-21 21:03:25 +05301510 port->cons.hvc->irq_requested = 1;
1511 resize_console(port);
1512 break;
Amit Shah8345adb2010-05-06 02:05:09 +05301513 }
Amit Shah2030fa42009-12-21 21:49:30 +05301514 case VIRTIO_CONSOLE_PORT_OPEN:
1515 port->host_connected = cpkt->value;
1516 wake_up_interruptible(&port->waitqueue);
Amit Shahcdfadfc2010-05-19 22:15:50 -06001517 /*
1518 * If the host port got closed and the host had any
1519 * unconsumed buffers, we'll be able to reclaim them
1520 * now.
1521 */
1522 spin_lock_irq(&port->outvq_lock);
1523 reclaim_consumed_buffers(port);
1524 spin_unlock_irq(&port->outvq_lock);
Amit Shah3eae0ad2010-09-02 18:47:52 +05301525
1526 /*
1527 * If the guest is connected, it'll be interested in
1528 * knowing the host connection state changed.
1529 */
1530 send_sigio_to_port(port);
Amit Shah2030fa42009-12-21 21:49:30 +05301531 break;
Amit Shah431edb82009-12-21 21:57:40 +05301532 case VIRTIO_CONSOLE_PORT_NAME:
1533 /*
Amit Shah291024e2011-09-14 13:06:40 +05301534 * If we woke up after hibernation, we can get this
1535 * again. Skip it in that case.
1536 */
1537 if (port->name)
1538 break;
1539
1540 /*
Amit Shah431edb82009-12-21 21:57:40 +05301541 * Skip the size of the header and the cpkt to get the size
1542 * of the name that was sent
1543 */
1544 name_size = buf->len - buf->offset - sizeof(*cpkt) + 1;
1545
1546 port->name = kmalloc(name_size, GFP_KERNEL);
1547 if (!port->name) {
1548 dev_err(port->dev,
1549 "Not enough space to store port name\n");
1550 break;
1551 }
1552 strncpy(port->name, buf->buf + buf->offset + sizeof(*cpkt),
1553 name_size - 1);
1554 port->name[name_size - 1] = 0;
1555
1556 /*
1557 * Since we only have one sysfs attribute, 'name',
1558 * create it only if we have a name for the port.
1559 */
1560 err = sysfs_create_group(&port->dev->kobj,
1561 &port_attribute_group);
Amit Shahec642132010-03-19 17:36:43 +05301562 if (err) {
Amit Shah431edb82009-12-21 21:57:40 +05301563 dev_err(port->dev,
1564 "Error %d creating sysfs device attributes\n",
1565 err);
Amit Shahec642132010-03-19 17:36:43 +05301566 } else {
1567 /*
1568 * Generate a udev event so that appropriate
1569 * symlinks can be created based on udev
1570 * rules.
1571 */
1572 kobject_uevent(&port->dev->kobj, KOBJ_CHANGE);
1573 }
Amit Shah431edb82009-12-21 21:57:40 +05301574 break;
Amit Shah17634ba2009-12-21 21:03:25 +05301575 }
1576}
1577
1578static void control_work_handler(struct work_struct *work)
1579{
1580 struct ports_device *portdev;
1581 struct virtqueue *vq;
1582 struct port_buffer *buf;
1583 unsigned int len;
1584
1585 portdev = container_of(work, struct ports_device, control_work);
1586 vq = portdev->c_ivq;
1587
1588 spin_lock(&portdev->cvq_lock);
Michael S. Tsirkin505b0452010-04-12 16:18:32 +03001589 while ((buf = virtqueue_get_buf(vq, &len))) {
Amit Shah17634ba2009-12-21 21:03:25 +05301590 spin_unlock(&portdev->cvq_lock);
1591
1592 buf->len = len;
1593 buf->offset = 0;
1594
1595 handle_control_message(portdev, buf);
1596
1597 spin_lock(&portdev->cvq_lock);
1598 if (add_inbuf(portdev->c_ivq, buf) < 0) {
1599 dev_warn(&portdev->vdev->dev,
1600 "Error adding buffer to queue\n");
1601 free_buf(buf);
1602 }
1603 }
1604 spin_unlock(&portdev->cvq_lock);
1605}
1606
Amit Shah2770c5e2011-01-31 13:06:36 +05301607static void out_intr(struct virtqueue *vq)
1608{
1609 struct port *port;
1610
1611 port = find_port_by_vq(vq->vdev->priv, vq);
1612 if (!port)
1613 return;
1614
1615 wake_up_interruptible(&port->waitqueue);
1616}
1617
Amit Shah17634ba2009-12-21 21:03:25 +05301618static void in_intr(struct virtqueue *vq)
1619{
1620 struct port *port;
1621 unsigned long flags;
1622
1623 port = find_port_by_vq(vq->vdev->priv, vq);
1624 if (!port)
1625 return;
1626
1627 spin_lock_irqsave(&port->inbuf_lock, flags);
Amit Shahd25a9dd2011-09-14 13:06:43 +05301628 port->inbuf = get_inbuf(port);
Amit Shah17634ba2009-12-21 21:03:25 +05301629
Amit Shah88f251a2009-12-21 22:15:30 +05301630 /*
1631 * Don't queue up data when port is closed. This condition
1632 * can be reached when a console port is not yet connected (no
1633 * tty is spawned) and the host sends out data to console
1634 * ports. For generic serial ports, the host won't
1635 * (shouldn't) send data till the guest is connected.
1636 */
1637 if (!port->guest_connected)
1638 discard_port_data(port);
1639
Amit Shah17634ba2009-12-21 21:03:25 +05301640 spin_unlock_irqrestore(&port->inbuf_lock, flags);
1641
Amit Shah2030fa42009-12-21 21:49:30 +05301642 wake_up_interruptible(&port->waitqueue);
1643
Amit Shah55f6bcc2010-09-02 18:47:53 +05301644 /* Send a SIGIO indicating new data in case the process asked for it */
1645 send_sigio_to_port(port);
1646
Amit Shah17634ba2009-12-21 21:03:25 +05301647 if (is_console_port(port) && hvc_poll(port->cons.hvc))
1648 hvc_kick();
1649}
1650
1651static void control_intr(struct virtqueue *vq)
1652{
1653 struct ports_device *portdev;
1654
1655 portdev = vq->vdev->priv;
1656 schedule_work(&portdev->control_work);
1657}
1658
Amit Shah7f5d8102009-12-21 22:22:08 +05301659static void config_intr(struct virtio_device *vdev)
1660{
1661 struct ports_device *portdev;
1662
1663 portdev = vdev->priv;
Amit Shah99f905f2010-05-19 22:15:48 -06001664
Amit Shah4038f5b72010-05-06 02:05:07 +05301665 if (!use_multiport(portdev)) {
Amit Shah97788292010-05-06 02:05:08 +05301666 struct port *port;
1667 u16 rows, cols;
1668
1669 vdev->config->get(vdev,
1670 offsetof(struct virtio_console_config, cols),
1671 &cols, sizeof(u16));
1672 vdev->config->get(vdev,
1673 offsetof(struct virtio_console_config, rows),
1674 &rows, sizeof(u16));
1675
1676 port = find_port_by_id(portdev, 0);
1677 set_console_size(port, rows, cols);
1678
Amit Shah4038f5b72010-05-06 02:05:07 +05301679 /*
1680 * We'll use this way of resizing only for legacy
1681 * support. For newer userspace
1682 * (VIRTIO_CONSOLE_F_MULTPORT+), use control messages
1683 * to indicate console size changes so that it can be
1684 * done per-port.
1685 */
Amit Shah97788292010-05-06 02:05:08 +05301686 resize_console(port);
Amit Shah4038f5b72010-05-06 02:05:07 +05301687 }
Amit Shah7f5d8102009-12-21 22:22:08 +05301688}
1689
Amit Shah2658a792010-01-18 19:15:11 +05301690static int init_vqs(struct ports_device *portdev)
1691{
1692 vq_callback_t **io_callbacks;
1693 char **io_names;
1694 struct virtqueue **vqs;
Amit Shah17634ba2009-12-21 21:03:25 +05301695 u32 i, j, nr_ports, nr_queues;
Amit Shah2658a792010-01-18 19:15:11 +05301696 int err;
1697
Amit Shah17634ba2009-12-21 21:03:25 +05301698 nr_ports = portdev->config.max_nr_ports;
1699 nr_queues = use_multiport(portdev) ? (nr_ports + 1) * 2 : 2;
Amit Shah2658a792010-01-18 19:15:11 +05301700
1701 vqs = kmalloc(nr_queues * sizeof(struct virtqueue *), GFP_KERNEL);
Amit Shah2658a792010-01-18 19:15:11 +05301702 io_callbacks = kmalloc(nr_queues * sizeof(vq_callback_t *), GFP_KERNEL);
Amit Shah2658a792010-01-18 19:15:11 +05301703 io_names = kmalloc(nr_queues * sizeof(char *), GFP_KERNEL);
Amit Shah2658a792010-01-18 19:15:11 +05301704 portdev->in_vqs = kmalloc(nr_ports * sizeof(struct virtqueue *),
1705 GFP_KERNEL);
Amit Shah2658a792010-01-18 19:15:11 +05301706 portdev->out_vqs = kmalloc(nr_ports * sizeof(struct virtqueue *),
1707 GFP_KERNEL);
Jiri Slaby22e132f2010-11-06 10:06:50 +01001708 if (!vqs || !io_callbacks || !io_names || !portdev->in_vqs ||
Amit Shah286f9a22011-09-14 13:06:39 +05301709 !portdev->out_vqs) {
Amit Shah2658a792010-01-18 19:15:11 +05301710 err = -ENOMEM;
Jiri Slaby22e132f2010-11-06 10:06:50 +01001711 goto free;
Amit Shah2658a792010-01-18 19:15:11 +05301712 }
1713
Amit Shah17634ba2009-12-21 21:03:25 +05301714 /*
1715 * For backward compat (newer host but older guest), the host
1716 * spawns a console port first and also inits the vqs for port
1717 * 0 before others.
1718 */
1719 j = 0;
1720 io_callbacks[j] = in_intr;
Amit Shah2770c5e2011-01-31 13:06:36 +05301721 io_callbacks[j + 1] = out_intr;
Amit Shah17634ba2009-12-21 21:03:25 +05301722 io_names[j] = "input";
1723 io_names[j + 1] = "output";
1724 j += 2;
Amit Shah2658a792010-01-18 19:15:11 +05301725
Amit Shah17634ba2009-12-21 21:03:25 +05301726 if (use_multiport(portdev)) {
1727 io_callbacks[j] = control_intr;
1728 io_callbacks[j + 1] = NULL;
1729 io_names[j] = "control-i";
1730 io_names[j + 1] = "control-o";
1731
1732 for (i = 1; i < nr_ports; i++) {
1733 j += 2;
1734 io_callbacks[j] = in_intr;
Amit Shah2770c5e2011-01-31 13:06:36 +05301735 io_callbacks[j + 1] = out_intr;
Amit Shah17634ba2009-12-21 21:03:25 +05301736 io_names[j] = "input";
1737 io_names[j + 1] = "output";
1738 }
1739 }
Amit Shah2658a792010-01-18 19:15:11 +05301740 /* Find the queues. */
1741 err = portdev->vdev->config->find_vqs(portdev->vdev, nr_queues, vqs,
1742 io_callbacks,
1743 (const char **)io_names);
1744 if (err)
Jiri Slaby22e132f2010-11-06 10:06:50 +01001745 goto free;
Amit Shah2658a792010-01-18 19:15:11 +05301746
Amit Shah17634ba2009-12-21 21:03:25 +05301747 j = 0;
Amit Shah2658a792010-01-18 19:15:11 +05301748 portdev->in_vqs[0] = vqs[0];
1749 portdev->out_vqs[0] = vqs[1];
Amit Shah17634ba2009-12-21 21:03:25 +05301750 j += 2;
1751 if (use_multiport(portdev)) {
1752 portdev->c_ivq = vqs[j];
1753 portdev->c_ovq = vqs[j + 1];
Amit Shah2658a792010-01-18 19:15:11 +05301754
Amit Shah17634ba2009-12-21 21:03:25 +05301755 for (i = 1; i < nr_ports; i++) {
1756 j += 2;
1757 portdev->in_vqs[i] = vqs[j];
1758 portdev->out_vqs[i] = vqs[j + 1];
1759 }
1760 }
Amit Shah2658a792010-01-18 19:15:11 +05301761 kfree(io_names);
Jiri Slaby22e132f2010-11-06 10:06:50 +01001762 kfree(io_callbacks);
Amit Shah2658a792010-01-18 19:15:11 +05301763 kfree(vqs);
1764
1765 return 0;
1766
Jiri Slaby22e132f2010-11-06 10:06:50 +01001767free:
Amit Shah2658a792010-01-18 19:15:11 +05301768 kfree(portdev->out_vqs);
Amit Shah2658a792010-01-18 19:15:11 +05301769 kfree(portdev->in_vqs);
Jiri Slaby22e132f2010-11-06 10:06:50 +01001770 kfree(io_names);
1771 kfree(io_callbacks);
Amit Shah2658a792010-01-18 19:15:11 +05301772 kfree(vqs);
Jiri Slaby22e132f2010-11-06 10:06:50 +01001773
Amit Shah2658a792010-01-18 19:15:11 +05301774 return err;
1775}
1776
Amit Shahfb08bd22009-12-21 21:36:04 +05301777static const struct file_operations portdev_fops = {
1778 .owner = THIS_MODULE,
1779};
1780
Amit Shaha0e2dbf2011-12-22 16:58:27 +05301781static void remove_vqs(struct ports_device *portdev)
1782{
1783 portdev->vdev->config->del_vqs(portdev->vdev);
1784 kfree(portdev->in_vqs);
1785 kfree(portdev->out_vqs);
1786}
1787
1788static void remove_controlq_data(struct ports_device *portdev)
1789{
1790 struct port_buffer *buf;
1791 unsigned int len;
1792
1793 if (!use_multiport(portdev))
1794 return;
1795
1796 while ((buf = virtqueue_get_buf(portdev->c_ivq, &len)))
1797 free_buf(buf);
1798
1799 while ((buf = virtqueue_detach_unused_buf(portdev->c_ivq)))
1800 free_buf(buf);
1801}
1802
Amit Shah1c85bf32010-01-18 19:15:07 +05301803/*
1804 * Once we're further in boot, we get probed like any other virtio
1805 * device.
Amit Shah17634ba2009-12-21 21:03:25 +05301806 *
1807 * If the host also supports multiple console ports, we check the
1808 * config space to see how many ports the host has spawned. We
1809 * initialize each port found.
Amit Shah1c85bf32010-01-18 19:15:07 +05301810 */
1811static int __devinit virtcons_probe(struct virtio_device *vdev)
1812{
Amit Shah1c85bf32010-01-18 19:15:07 +05301813 struct ports_device *portdev;
1814 int err;
Amit Shah17634ba2009-12-21 21:03:25 +05301815 bool multiport;
Christian Borntraeger5e384832011-09-22 23:44:23 +05301816 bool early = early_put_chars != NULL;
1817
1818 /* Ensure to read early_put_chars now */
1819 barrier();
Amit Shah1c85bf32010-01-18 19:15:07 +05301820
1821 portdev = kmalloc(sizeof(*portdev), GFP_KERNEL);
1822 if (!portdev) {
1823 err = -ENOMEM;
1824 goto fail;
1825 }
1826
1827 /* Attach this portdev to this virtio_device, and vice-versa. */
1828 portdev->vdev = vdev;
1829 vdev->priv = portdev;
1830
Amit Shahfb08bd22009-12-21 21:36:04 +05301831 spin_lock_irq(&pdrvdata_lock);
1832 portdev->drv_index = pdrvdata.index++;
1833 spin_unlock_irq(&pdrvdata_lock);
1834
1835 portdev->chr_major = register_chrdev(0, "virtio-portsdev",
1836 &portdev_fops);
1837 if (portdev->chr_major < 0) {
1838 dev_err(&vdev->dev,
1839 "Error %d registering chrdev for device %u\n",
1840 portdev->chr_major, portdev->drv_index);
1841 err = portdev->chr_major;
1842 goto free;
1843 }
1844
Amit Shah17634ba2009-12-21 21:03:25 +05301845 multiport = false;
Amit Shah17634ba2009-12-21 21:03:25 +05301846 portdev->config.max_nr_ports = 1;
Sasha Levin51c6d612011-08-14 17:52:31 +03001847 if (virtio_config_val(vdev, VIRTIO_CONSOLE_F_MULTIPORT,
1848 offsetof(struct virtio_console_config,
1849 max_nr_ports),
1850 &portdev->config.max_nr_ports) == 0)
Amit Shah17634ba2009-12-21 21:03:25 +05301851 multiport = true;
Amit Shah17634ba2009-12-21 21:03:25 +05301852
Amit Shah2658a792010-01-18 19:15:11 +05301853 err = init_vqs(portdev);
1854 if (err < 0) {
1855 dev_err(&vdev->dev, "Error %d initializing vqs\n", err);
Amit Shahfb08bd22009-12-21 21:36:04 +05301856 goto free_chrdev;
Amit Shah2658a792010-01-18 19:15:11 +05301857 }
Amit Shah1c85bf32010-01-18 19:15:07 +05301858
Amit Shah17634ba2009-12-21 21:03:25 +05301859 spin_lock_init(&portdev->ports_lock);
1860 INIT_LIST_HEAD(&portdev->ports);
1861
1862 if (multiport) {
Amit Shah335a64a5c2010-02-24 10:37:44 +05301863 unsigned int nr_added_bufs;
1864
Amit Shah17634ba2009-12-21 21:03:25 +05301865 spin_lock_init(&portdev->cvq_lock);
1866 INIT_WORK(&portdev->control_work, &control_work_handler);
1867
Amit Shah335a64a5c2010-02-24 10:37:44 +05301868 nr_added_bufs = fill_queue(portdev->c_ivq, &portdev->cvq_lock);
1869 if (!nr_added_bufs) {
Amit Shah22a29ea2010-02-12 10:32:17 +05301870 dev_err(&vdev->dev,
1871 "Error allocating buffers for control queue\n");
1872 err = -ENOMEM;
1873 goto free_vqs;
1874 }
Amit Shah1d051602010-05-19 22:15:49 -06001875 } else {
1876 /*
1877 * For backward compatibility: Create a console port
1878 * if we're running on older host.
1879 */
1880 add_port(portdev, 0);
Amit Shah17634ba2009-12-21 21:03:25 +05301881 }
1882
Amit Shah6bdf2af2010-09-02 18:11:49 +05301883 spin_lock_irq(&pdrvdata_lock);
1884 list_add_tail(&portdev->list, &pdrvdata.portdevs);
1885 spin_unlock_irq(&pdrvdata_lock);
1886
Amit Shahf909f852010-05-19 22:15:48 -06001887 __send_control_msg(portdev, VIRTIO_CONSOLE_BAD_ID,
1888 VIRTIO_CONSOLE_DEVICE_READY, 1);
Christian Borntraeger5e384832011-09-22 23:44:23 +05301889
1890 /*
1891 * If there was an early virtio console, assume that there are no
1892 * other consoles. We need to wait until the hvc_alloc matches the
1893 * hvc_instantiate, otherwise tty_open will complain, resulting in
1894 * a "Warning: unable to open an initial console" boot failure.
1895 * Without multiport this is done in add_port above. With multiport
1896 * this might take some host<->guest communication - thus we have to
1897 * wait.
1898 */
1899 if (multiport && early)
1900 wait_for_completion(&early_console_added);
1901
Rusty Russell31610432007-10-22 11:03:39 +10001902 return 0;
1903
Amit Shah22a29ea2010-02-12 10:32:17 +05301904free_vqs:
Julia Lawall0643e4c2010-05-15 11:45:53 +02001905 /* The host might want to notify mgmt sw about device add failure */
1906 __send_control_msg(portdev, VIRTIO_CONSOLE_BAD_ID,
1907 VIRTIO_CONSOLE_DEVICE_READY, 0);
Amit Shaha0e2dbf2011-12-22 16:58:27 +05301908 remove_vqs(portdev);
Amit Shahfb08bd22009-12-21 21:36:04 +05301909free_chrdev:
1910 unregister_chrdev(portdev->chr_major, "virtio-portsdev");
Rusty Russell31610432007-10-22 11:03:39 +10001911free:
Amit Shah1c85bf32010-01-18 19:15:07 +05301912 kfree(portdev);
Rusty Russell31610432007-10-22 11:03:39 +10001913fail:
1914 return err;
1915}
1916
Amit Shah71778762010-02-12 10:32:16 +05301917static void virtcons_remove(struct virtio_device *vdev)
1918{
1919 struct ports_device *portdev;
1920 struct port *port, *port2;
Amit Shah71778762010-02-12 10:32:16 +05301921
1922 portdev = vdev->priv;
1923
Amit Shah6bdf2af2010-09-02 18:11:49 +05301924 spin_lock_irq(&pdrvdata_lock);
1925 list_del(&portdev->list);
1926 spin_unlock_irq(&pdrvdata_lock);
1927
Amit Shah02238952010-09-02 18:11:40 +05301928 /* Disable interrupts for vqs */
1929 vdev->config->reset(vdev);
1930 /* Finish up work that's lined up */
Amit Shah71778762010-02-12 10:32:16 +05301931 cancel_work_sync(&portdev->control_work);
Amit Shah71778762010-02-12 10:32:16 +05301932
1933 list_for_each_entry_safe(port, port2, &portdev->ports, list)
Amit Shahb353a6b2010-09-02 18:38:29 +05301934 unplug_port(port);
Amit Shah71778762010-02-12 10:32:16 +05301935
1936 unregister_chrdev(portdev->chr_major, "virtio-portsdev");
1937
Amit Shahe0620132010-09-02 18:38:30 +05301938 /*
1939 * When yanking out a device, we immediately lose the
1940 * (device-side) queues. So there's no point in keeping the
1941 * guest side around till we drop our final reference. This
1942 * also means that any ports which are in an open state will
1943 * have to just stop using the port, as the vqs are going
1944 * away.
1945 */
Amit Shaha0e2dbf2011-12-22 16:58:27 +05301946 remove_controlq_data(portdev);
1947 remove_vqs(portdev);
Amit Shah71778762010-02-12 10:32:16 +05301948 kfree(portdev);
1949}
1950
Rusty Russell31610432007-10-22 11:03:39 +10001951static struct virtio_device_id id_table[] = {
1952 { VIRTIO_ID_CONSOLE, VIRTIO_DEV_ANY_ID },
1953 { 0 },
1954};
1955
Christian Borntraegerc2983452008-11-25 13:36:26 +01001956static unsigned int features[] = {
1957 VIRTIO_CONSOLE_F_SIZE,
Amit Shahb99fa812010-05-19 22:15:46 -06001958 VIRTIO_CONSOLE_F_MULTIPORT,
Christian Borntraegerc2983452008-11-25 13:36:26 +01001959};
1960
Amit Shah2b8f41d2011-12-22 16:58:28 +05301961#ifdef CONFIG_PM
1962static int virtcons_freeze(struct virtio_device *vdev)
1963{
1964 struct ports_device *portdev;
1965 struct port *port;
1966
1967 portdev = vdev->priv;
1968
1969 vdev->config->reset(vdev);
1970
Amit Shahc743d092012-01-06 16:19:08 +05301971 virtqueue_disable_cb(portdev->c_ivq);
Amit Shah2b8f41d2011-12-22 16:58:28 +05301972 cancel_work_sync(&portdev->control_work);
Amit Shahc743d092012-01-06 16:19:08 +05301973 /*
1974 * Once more: if control_work_handler() was running, it would
1975 * enable the cb as the last step.
1976 */
1977 virtqueue_disable_cb(portdev->c_ivq);
Amit Shah2b8f41d2011-12-22 16:58:28 +05301978 remove_controlq_data(portdev);
1979
1980 list_for_each_entry(port, &portdev->ports, list) {
Amit Shahc743d092012-01-06 16:19:08 +05301981 virtqueue_disable_cb(port->in_vq);
1982 virtqueue_disable_cb(port->out_vq);
Amit Shah2b8f41d2011-12-22 16:58:28 +05301983 /*
1984 * We'll ask the host later if the new invocation has
1985 * the port opened or closed.
1986 */
1987 port->host_connected = false;
1988 remove_port_data(port);
1989 }
1990 remove_vqs(portdev);
1991
1992 return 0;
1993}
1994
1995static int virtcons_restore(struct virtio_device *vdev)
1996{
1997 struct ports_device *portdev;
1998 struct port *port;
1999 int ret;
2000
2001 portdev = vdev->priv;
2002
2003 ret = init_vqs(portdev);
2004 if (ret)
2005 return ret;
2006
2007 if (use_multiport(portdev))
2008 fill_queue(portdev->c_ivq, &portdev->cvq_lock);
2009
2010 list_for_each_entry(port, &portdev->ports, list) {
2011 port->in_vq = portdev->in_vqs[port->id];
2012 port->out_vq = portdev->out_vqs[port->id];
2013
2014 fill_queue(port->in_vq, &port->inbuf_lock);
2015
2016 /* Get port open/close status on the host */
2017 send_control_msg(port, VIRTIO_CONSOLE_PORT_READY, 1);
Amit Shahfa8b66c2012-04-25 14:40:39 +05302018
2019 /*
2020 * If a port was open at the time of suspending, we
2021 * have to let the host know that it's still open.
2022 */
2023 if (port->guest_connected)
2024 send_control_msg(port, VIRTIO_CONSOLE_PORT_OPEN, 1);
Amit Shah2b8f41d2011-12-22 16:58:28 +05302025 }
2026 return 0;
2027}
2028#endif
2029
Rusty Russell31610432007-10-22 11:03:39 +10002030static struct virtio_driver virtio_console = {
Christian Borntraegerc2983452008-11-25 13:36:26 +01002031 .feature_table = features,
2032 .feature_table_size = ARRAY_SIZE(features),
Rusty Russell31610432007-10-22 11:03:39 +10002033 .driver.name = KBUILD_MODNAME,
2034 .driver.owner = THIS_MODULE,
2035 .id_table = id_table,
2036 .probe = virtcons_probe,
Amit Shah71778762010-02-12 10:32:16 +05302037 .remove = virtcons_remove,
Amit Shah7f5d8102009-12-21 22:22:08 +05302038 .config_changed = config_intr,
Amit Shah2b8f41d2011-12-22 16:58:28 +05302039#ifdef CONFIG_PM
2040 .freeze = virtcons_freeze,
2041 .restore = virtcons_restore,
2042#endif
Rusty Russell31610432007-10-22 11:03:39 +10002043};
2044
2045static int __init init(void)
2046{
Amit Shahfb08bd22009-12-21 21:36:04 +05302047 int err;
2048
2049 pdrvdata.class = class_create(THIS_MODULE, "virtio-ports");
2050 if (IS_ERR(pdrvdata.class)) {
2051 err = PTR_ERR(pdrvdata.class);
2052 pr_err("Error %d creating virtio-ports class\n", err);
2053 return err;
2054 }
Amit Shahd99393e2009-12-21 22:36:21 +05302055
2056 pdrvdata.debugfs_dir = debugfs_create_dir("virtio-ports", NULL);
2057 if (!pdrvdata.debugfs_dir) {
2058 pr_warning("Error %ld creating debugfs dir for virtio-ports\n",
2059 PTR_ERR(pdrvdata.debugfs_dir));
2060 }
Amit Shah38edf582010-01-18 19:15:05 +05302061 INIT_LIST_HEAD(&pdrvdata.consoles);
Amit Shah6bdf2af2010-09-02 18:11:49 +05302062 INIT_LIST_HEAD(&pdrvdata.portdevs);
Amit Shah38edf582010-01-18 19:15:05 +05302063
Rusty Russell31610432007-10-22 11:03:39 +10002064 return register_virtio_driver(&virtio_console);
2065}
Amit Shah71778762010-02-12 10:32:16 +05302066
2067static void __exit fini(void)
2068{
2069 unregister_virtio_driver(&virtio_console);
2070
2071 class_destroy(pdrvdata.class);
2072 if (pdrvdata.debugfs_dir)
2073 debugfs_remove_recursive(pdrvdata.debugfs_dir);
2074}
Rusty Russell31610432007-10-22 11:03:39 +10002075module_init(init);
Amit Shah71778762010-02-12 10:32:16 +05302076module_exit(fini);
Rusty Russell31610432007-10-22 11:03:39 +10002077
2078MODULE_DEVICE_TABLE(virtio, id_table);
2079MODULE_DESCRIPTION("Virtio console driver");
2080MODULE_LICENSE("GPL");