blob: db244b5b6c8ad23dce053503b6883735a1487cfa [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
Rusty Russell971f3392010-01-18 19:14:56 +0530232/* This is the very early arch-specified put chars function. */
233static int (*early_put_chars)(u32, const char *, int);
234
Amit Shah38edf582010-01-18 19:15:05 +0530235static struct port *find_port_by_vtermno(u32 vtermno)
236{
237 struct port *port;
Amit Shah4f23c572010-01-18 19:15:09 +0530238 struct console *cons;
Amit Shah38edf582010-01-18 19:15:05 +0530239 unsigned long flags;
240
241 spin_lock_irqsave(&pdrvdata_lock, flags);
Amit Shah4f23c572010-01-18 19:15:09 +0530242 list_for_each_entry(cons, &pdrvdata.consoles, list) {
243 if (cons->vtermno == vtermno) {
244 port = container_of(cons, struct port, cons);
Amit Shah38edf582010-01-18 19:15:05 +0530245 goto out;
Amit Shah4f23c572010-01-18 19:15:09 +0530246 }
Amit Shah38edf582010-01-18 19:15:05 +0530247 }
248 port = NULL;
249out:
250 spin_unlock_irqrestore(&pdrvdata_lock, flags);
251 return port;
252}
253
Amit Shah04950cd2010-09-02 18:20:58 +0530254static struct port *find_port_by_devt_in_portdev(struct ports_device *portdev,
255 dev_t dev)
256{
257 struct port *port;
258 unsigned long flags;
259
260 spin_lock_irqsave(&portdev->ports_lock, flags);
261 list_for_each_entry(port, &portdev->ports, list)
Amit Shahd22a6982010-09-02 18:20:59 +0530262 if (port->cdev->dev == dev)
Amit Shah04950cd2010-09-02 18:20:58 +0530263 goto out;
264 port = NULL;
265out:
266 spin_unlock_irqrestore(&portdev->ports_lock, flags);
267
268 return port;
269}
270
271static struct port *find_port_by_devt(dev_t dev)
272{
273 struct ports_device *portdev;
274 struct port *port;
275 unsigned long flags;
276
277 spin_lock_irqsave(&pdrvdata_lock, flags);
278 list_for_each_entry(portdev, &pdrvdata.portdevs, list) {
279 port = find_port_by_devt_in_portdev(portdev, dev);
280 if (port)
281 goto out;
282 }
283 port = NULL;
284out:
285 spin_unlock_irqrestore(&pdrvdata_lock, flags);
286 return port;
287}
288
Amit Shah17634ba2009-12-21 21:03:25 +0530289static struct port *find_port_by_id(struct ports_device *portdev, u32 id)
290{
291 struct port *port;
292 unsigned long flags;
293
294 spin_lock_irqsave(&portdev->ports_lock, flags);
295 list_for_each_entry(port, &portdev->ports, list)
296 if (port->id == id)
297 goto out;
298 port = NULL;
299out:
300 spin_unlock_irqrestore(&portdev->ports_lock, flags);
301
302 return port;
303}
304
Amit Shah203baab2010-01-18 19:15:12 +0530305static struct port *find_port_by_vq(struct ports_device *portdev,
306 struct virtqueue *vq)
307{
308 struct port *port;
Amit Shah203baab2010-01-18 19:15:12 +0530309 unsigned long flags;
310
Amit Shah17634ba2009-12-21 21:03:25 +0530311 spin_lock_irqsave(&portdev->ports_lock, flags);
312 list_for_each_entry(port, &portdev->ports, list)
Amit Shah203baab2010-01-18 19:15:12 +0530313 if (port->in_vq == vq || port->out_vq == vq)
314 goto out;
Amit Shah203baab2010-01-18 19:15:12 +0530315 port = NULL;
316out:
Amit Shah17634ba2009-12-21 21:03:25 +0530317 spin_unlock_irqrestore(&portdev->ports_lock, flags);
Amit Shah203baab2010-01-18 19:15:12 +0530318 return port;
319}
320
Amit Shah17634ba2009-12-21 21:03:25 +0530321static bool is_console_port(struct port *port)
322{
323 if (port->cons.hvc)
324 return true;
325 return false;
326}
327
328static inline bool use_multiport(struct ports_device *portdev)
329{
330 /*
331 * This condition can be true when put_chars is called from
332 * early_init
333 */
334 if (!portdev->vdev)
335 return 0;
336 return portdev->vdev->features[0] & (1 << VIRTIO_CONSOLE_F_MULTIPORT);
337}
338
Amit Shahfdb9a052010-01-18 19:15:01 +0530339static void free_buf(struct port_buffer *buf)
340{
341 kfree(buf->buf);
342 kfree(buf);
343}
344
345static struct port_buffer *alloc_buf(size_t buf_size)
346{
347 struct port_buffer *buf;
348
349 buf = kmalloc(sizeof(*buf), GFP_KERNEL);
350 if (!buf)
351 goto fail;
Sjur Brændeland0127f682012-10-15 09:57:34 +0200352 buf->buf = kmalloc(buf_size, GFP_KERNEL);
Amit Shahfdb9a052010-01-18 19:15:01 +0530353 if (!buf->buf)
354 goto free_buf;
355 buf->len = 0;
356 buf->offset = 0;
357 buf->size = buf_size;
358 return buf;
359
360free_buf:
361 kfree(buf);
362fail:
363 return NULL;
364}
365
Amit Shaha3cde442010-01-18 19:15:03 +0530366/* Callers should take appropriate locks */
Amit Shahdefde662011-09-14 13:06:42 +0530367static struct port_buffer *get_inbuf(struct port *port)
Amit Shaha3cde442010-01-18 19:15:03 +0530368{
369 struct port_buffer *buf;
Amit Shaha3cde442010-01-18 19:15:03 +0530370 unsigned int len;
371
Amit Shahd25a9dd2011-09-14 13:06:43 +0530372 if (port->inbuf)
373 return port->inbuf;
374
375 buf = virtqueue_get_buf(port->in_vq, &len);
Amit Shaha3cde442010-01-18 19:15:03 +0530376 if (buf) {
377 buf->len = len;
378 buf->offset = 0;
Amit Shah17e5b4f2011-09-14 13:06:46 +0530379 port->stats.bytes_received += len;
Amit Shaha3cde442010-01-18 19:15:03 +0530380 }
381 return buf;
382}
383
Rusty Russella23ea922010-01-18 19:14:55 +0530384/*
Amit Shahe27b5192010-01-18 19:15:02 +0530385 * Create a scatter-gather list representing our input buffer and put
386 * it in the queue.
387 *
388 * Callers should take appropriate locks.
389 */
Amit Shah203baab2010-01-18 19:15:12 +0530390static int add_inbuf(struct virtqueue *vq, struct port_buffer *buf)
Amit Shahe27b5192010-01-18 19:15:02 +0530391{
392 struct scatterlist sg[1];
Amit Shah203baab2010-01-18 19:15:12 +0530393 int ret;
Amit Shah1c85bf32010-01-18 19:15:07 +0530394
Amit Shahe27b5192010-01-18 19:15:02 +0530395 sg_init_one(sg, buf->buf, buf->size);
396
Rusty Russellf96fde42012-01-12 15:44:42 +1030397 ret = virtqueue_add_buf(vq, sg, 0, 1, buf, GFP_ATOMIC);
Michael S. Tsirkin505b0452010-04-12 16:18:32 +0300398 virtqueue_kick(vq);
Amit Shah49e86f12012-12-10 09:45:12 +1030399 if (!ret)
400 ret = vq->num_free;
Amit Shah203baab2010-01-18 19:15:12 +0530401 return ret;
402}
403
Amit Shah88f251a2009-12-21 22:15:30 +0530404/* Discard any unread data this port has. Callers lockers. */
405static void discard_port_data(struct port *port)
406{
407 struct port_buffer *buf;
Amit Shah2d24cda2011-09-14 13:06:45 +0530408 unsigned int err;
Amit Shah88f251a2009-12-21 22:15:30 +0530409
Amit Shahd7a62cd2011-03-04 14:04:33 +1030410 if (!port->portdev) {
411 /* Device has been unplugged. vqs are already gone. */
412 return;
413 }
Amit Shah2d24cda2011-09-14 13:06:45 +0530414 buf = get_inbuf(port);
Amit Shah88f251a2009-12-21 22:15:30 +0530415
Amit Shahce072a02011-09-14 13:06:44 +0530416 err = 0;
Amit Shahd6933562010-02-12 10:32:18 +0530417 while (buf) {
Amit Shah17e5b4f2011-09-14 13:06:46 +0530418 port->stats.bytes_discarded += buf->len - buf->offset;
Amit Shah2d24cda2011-09-14 13:06:45 +0530419 if (add_inbuf(port->in_vq, buf) < 0) {
Amit Shahce072a02011-09-14 13:06:44 +0530420 err++;
Amit Shahd6933562010-02-12 10:32:18 +0530421 free_buf(buf);
422 }
Amit Shah2d24cda2011-09-14 13:06:45 +0530423 port->inbuf = NULL;
424 buf = get_inbuf(port);
Amit Shah88f251a2009-12-21 22:15:30 +0530425 }
Amit Shahce072a02011-09-14 13:06:44 +0530426 if (err)
Amit Shahd6933562010-02-12 10:32:18 +0530427 dev_warn(port->dev, "Errors adding %d buffers back to vq\n",
Amit Shahce072a02011-09-14 13:06:44 +0530428 err);
Amit Shah88f251a2009-12-21 22:15:30 +0530429}
430
Amit Shah203baab2010-01-18 19:15:12 +0530431static bool port_has_data(struct port *port)
432{
433 unsigned long flags;
434 bool ret;
435
Amit Shahd6933562010-02-12 10:32:18 +0530436 ret = false;
Amit Shahd25a9dd2011-09-14 13:06:43 +0530437 spin_lock_irqsave(&port->inbuf_lock, flags);
438 port->inbuf = get_inbuf(port);
439 if (port->inbuf)
440 ret = true;
441
Amit Shah203baab2010-01-18 19:15:12 +0530442 spin_unlock_irqrestore(&port->inbuf_lock, flags);
Amit Shah203baab2010-01-18 19:15:12 +0530443 return ret;
444}
445
Amit Shah3425e702010-05-19 22:15:46 -0600446static ssize_t __send_control_msg(struct ports_device *portdev, u32 port_id,
447 unsigned int event, unsigned int value)
Amit Shah17634ba2009-12-21 21:03:25 +0530448{
449 struct scatterlist sg[1];
450 struct virtio_console_control cpkt;
451 struct virtqueue *vq;
Amit Shah604b2ad2010-02-24 10:36:51 +0530452 unsigned int len;
Amit Shah17634ba2009-12-21 21:03:25 +0530453
Amit Shah3425e702010-05-19 22:15:46 -0600454 if (!use_multiport(portdev))
Amit Shah17634ba2009-12-21 21:03:25 +0530455 return 0;
456
Amit Shah3425e702010-05-19 22:15:46 -0600457 cpkt.id = port_id;
Amit Shah17634ba2009-12-21 21:03:25 +0530458 cpkt.event = event;
459 cpkt.value = value;
460
Amit Shah3425e702010-05-19 22:15:46 -0600461 vq = portdev->c_ovq;
Amit Shah17634ba2009-12-21 21:03:25 +0530462
463 sg_init_one(sg, &cpkt, sizeof(cpkt));
Rusty Russell589575a2012-10-16 23:56:15 +1030464 if (virtqueue_add_buf(vq, sg, 1, 0, &cpkt, GFP_ATOMIC) == 0) {
Michael S. Tsirkin505b0452010-04-12 16:18:32 +0300465 virtqueue_kick(vq);
466 while (!virtqueue_get_buf(vq, &len))
Amit Shah17634ba2009-12-21 21:03:25 +0530467 cpu_relax();
468 }
469 return 0;
470}
471
Amit Shah3425e702010-05-19 22:15:46 -0600472static ssize_t send_control_msg(struct port *port, unsigned int event,
473 unsigned int value)
474{
Amit Shah84ec06c2010-09-02 18:11:42 +0530475 /* Did the port get unplugged before userspace closed it? */
476 if (port->portdev)
477 return __send_control_msg(port->portdev, port->id, event, value);
478 return 0;
Amit Shah3425e702010-05-19 22:15:46 -0600479}
480
Masami Hiramatsueb5e89f2012-08-09 21:30:39 +0900481struct buffer_token {
482 union {
483 void *buf;
484 struct scatterlist *sg;
485 } u;
Masami Hiramatsu8ca84a52012-08-09 21:31:20 +0900486 /* If sgpages == 0 then buf is used, else sg is used */
487 unsigned int sgpages;
Masami Hiramatsueb5e89f2012-08-09 21:30:39 +0900488};
489
Masami Hiramatsu8ca84a52012-08-09 21:31:20 +0900490static void reclaim_sg_pages(struct scatterlist *sg, unsigned int nrpages)
Masami Hiramatsueb5e89f2012-08-09 21:30:39 +0900491{
492 int i;
493 struct page *page;
494
Masami Hiramatsu8ca84a52012-08-09 21:31:20 +0900495 for (i = 0; i < nrpages; i++) {
Masami Hiramatsueb5e89f2012-08-09 21:30:39 +0900496 page = sg_page(&sg[i]);
497 if (!page)
498 break;
499 put_page(page);
500 }
501 kfree(sg);
502}
503
Amit Shahcdfadfc2010-05-19 22:15:50 -0600504/* Callers must take the port->outvq_lock */
505static void reclaim_consumed_buffers(struct port *port)
506{
Masami Hiramatsueb5e89f2012-08-09 21:30:39 +0900507 struct buffer_token *tok;
Amit Shahcdfadfc2010-05-19 22:15:50 -0600508 unsigned int len;
509
Amit Shahd7a62cd2011-03-04 14:04:33 +1030510 if (!port->portdev) {
511 /* Device has been unplugged. vqs are already gone. */
512 return;
513 }
Masami Hiramatsueb5e89f2012-08-09 21:30:39 +0900514 while ((tok = virtqueue_get_buf(port->out_vq, &len))) {
515 if (tok->sgpages)
Masami Hiramatsu8ca84a52012-08-09 21:31:20 +0900516 reclaim_sg_pages(tok->u.sg, tok->sgpages);
Masami Hiramatsueb5e89f2012-08-09 21:30:39 +0900517 else
518 kfree(tok->u.buf);
519 kfree(tok);
Amit Shahcdfadfc2010-05-19 22:15:50 -0600520 port->outvq_full = false;
521 }
522}
523
Masami Hiramatsueb5e89f2012-08-09 21:30:39 +0900524static ssize_t __send_to_port(struct port *port, struct scatterlist *sg,
525 int nents, size_t in_count,
526 struct buffer_token *tok, bool nonblock)
Amit Shahf997f00b2009-12-21 17:28:51 +0530527{
Amit Shahf997f00b2009-12-21 17:28:51 +0530528 struct virtqueue *out_vq;
Rusty Russell589575a2012-10-16 23:56:15 +1030529 int err;
Amit Shahcdfadfc2010-05-19 22:15:50 -0600530 unsigned long flags;
Amit Shahf997f00b2009-12-21 17:28:51 +0530531 unsigned int len;
532
533 out_vq = port->out_vq;
534
Amit Shahcdfadfc2010-05-19 22:15:50 -0600535 spin_lock_irqsave(&port->outvq_lock, flags);
536
537 reclaim_consumed_buffers(port);
538
Rusty Russell589575a2012-10-16 23:56:15 +1030539 err = virtqueue_add_buf(out_vq, sg, nents, 0, tok, GFP_ATOMIC);
Amit Shahf997f00b2009-12-21 17:28:51 +0530540
541 /* Tell Host to go! */
Michael S. Tsirkin505b0452010-04-12 16:18:32 +0300542 virtqueue_kick(out_vq);
Amit Shahf997f00b2009-12-21 17:28:51 +0530543
Rusty Russell589575a2012-10-16 23:56:15 +1030544 if (err) {
Rusty Russell9ff4cfa2010-04-08 09:46:16 -0600545 in_count = 0;
Amit Shahcdfadfc2010-05-19 22:15:50 -0600546 goto done;
Amit Shahf997f00b2009-12-21 17:28:51 +0530547 }
548
Rusty Russell589575a2012-10-16 23:56:15 +1030549 if (out_vq->num_free == 0)
Amit Shahcdfadfc2010-05-19 22:15:50 -0600550 port->outvq_full = true;
551
552 if (nonblock)
553 goto done;
554
555 /*
556 * Wait till the host acknowledges it pushed out the data we
Amit Shah531295e2010-10-20 13:45:43 +1030557 * sent. This is done for data from the hvc_console; the tty
558 * operations are performed with spinlocks held so we can't
559 * sleep here. An alternative would be to copy the data to a
560 * buffer and relax the spinning requirement. The downside is
561 * we need to kmalloc a GFP_ATOMIC buffer each time the
562 * console driver writes something out.
Amit Shahcdfadfc2010-05-19 22:15:50 -0600563 */
Michael S. Tsirkin505b0452010-04-12 16:18:32 +0300564 while (!virtqueue_get_buf(out_vq, &len))
Amit Shahf997f00b2009-12-21 17:28:51 +0530565 cpu_relax();
Amit Shahcdfadfc2010-05-19 22:15:50 -0600566done:
567 spin_unlock_irqrestore(&port->outvq_lock, flags);
Amit Shah17e5b4f2011-09-14 13:06:46 +0530568
569 port->stats.bytes_sent += in_count;
Amit Shahcdfadfc2010-05-19 22:15:50 -0600570 /*
571 * We're expected to return the amount of data we wrote -- all
572 * of it
573 */
Rusty Russell9ff4cfa2010-04-08 09:46:16 -0600574 return in_count;
Amit Shahf997f00b2009-12-21 17:28:51 +0530575}
576
Masami Hiramatsueb5e89f2012-08-09 21:30:39 +0900577static ssize_t send_buf(struct port *port, void *in_buf, size_t in_count,
578 bool nonblock)
579{
580 struct scatterlist sg[1];
581 struct buffer_token *tok;
582
583 tok = kmalloc(sizeof(*tok), GFP_ATOMIC);
584 if (!tok)
585 return -ENOMEM;
Masami Hiramatsu8ca84a52012-08-09 21:31:20 +0900586 tok->sgpages = 0;
Masami Hiramatsueb5e89f2012-08-09 21:30:39 +0900587 tok->u.buf = in_buf;
588
589 sg_init_one(sg, in_buf, in_count);
590
591 return __send_to_port(port, sg, 1, in_count, tok, nonblock);
592}
593
594static ssize_t send_pages(struct port *port, struct scatterlist *sg, int nents,
595 size_t in_count, bool nonblock)
596{
597 struct buffer_token *tok;
598
599 tok = kmalloc(sizeof(*tok), GFP_ATOMIC);
600 if (!tok)
601 return -ENOMEM;
Masami Hiramatsu8ca84a52012-08-09 21:31:20 +0900602 tok->sgpages = nents;
Masami Hiramatsueb5e89f2012-08-09 21:30:39 +0900603 tok->u.sg = sg;
604
605 return __send_to_port(port, sg, nents, in_count, tok, nonblock);
606}
607
Amit Shah203baab2010-01-18 19:15:12 +0530608/*
609 * Give out the data that's requested from the buffer that we have
610 * queued up.
611 */
Amit Shahb766cee2009-12-21 21:26:45 +0530612static ssize_t fill_readbuf(struct port *port, char *out_buf, size_t out_count,
613 bool to_user)
Amit Shah203baab2010-01-18 19:15:12 +0530614{
615 struct port_buffer *buf;
616 unsigned long flags;
617
618 if (!out_count || !port_has_data(port))
619 return 0;
620
621 buf = port->inbuf;
Amit Shahb766cee2009-12-21 21:26:45 +0530622 out_count = min(out_count, buf->len - buf->offset);
Amit Shah203baab2010-01-18 19:15:12 +0530623
Amit Shahb766cee2009-12-21 21:26:45 +0530624 if (to_user) {
625 ssize_t ret;
Amit Shah203baab2010-01-18 19:15:12 +0530626
Amit Shahb766cee2009-12-21 21:26:45 +0530627 ret = copy_to_user(out_buf, buf->buf + buf->offset, out_count);
628 if (ret)
629 return -EFAULT;
630 } else {
631 memcpy(out_buf, buf->buf + buf->offset, out_count);
632 }
633
Amit Shah203baab2010-01-18 19:15:12 +0530634 buf->offset += out_count;
635
636 if (buf->offset == buf->len) {
637 /*
638 * We're done using all the data in this buffer.
639 * Re-queue so that the Host can send us more data.
640 */
641 spin_lock_irqsave(&port->inbuf_lock, flags);
642 port->inbuf = NULL;
643
644 if (add_inbuf(port->in_vq, buf) < 0)
Amit Shahfb08bd22009-12-21 21:36:04 +0530645 dev_warn(port->dev, "failed add_buf\n");
Amit Shah203baab2010-01-18 19:15:12 +0530646
647 spin_unlock_irqrestore(&port->inbuf_lock, flags);
648 }
Amit Shahb766cee2009-12-21 21:26:45 +0530649 /* Return the number of bytes actually copied */
Amit Shah203baab2010-01-18 19:15:12 +0530650 return out_count;
Amit Shahe27b5192010-01-18 19:15:02 +0530651}
652
Amit Shah2030fa42009-12-21 21:49:30 +0530653/* The condition that must be true for polling to end */
Amit Shah60caacd2010-05-19 22:15:49 -0600654static bool will_read_block(struct port *port)
Amit Shah2030fa42009-12-21 21:49:30 +0530655{
Amit Shah3709ea72010-09-02 18:11:43 +0530656 if (!port->guest_connected) {
657 /* Port got hot-unplugged. Let's exit. */
658 return false;
659 }
Amit Shah60caacd2010-05-19 22:15:49 -0600660 return !port_has_data(port) && port->host_connected;
Amit Shah2030fa42009-12-21 21:49:30 +0530661}
662
Amit Shahcdfadfc2010-05-19 22:15:50 -0600663static bool will_write_block(struct port *port)
664{
665 bool ret;
666
Amit Shah60e5e0b2010-05-27 13:24:40 +0530667 if (!port->guest_connected) {
668 /* Port got hot-unplugged. Let's exit. */
669 return false;
670 }
Amit Shahcdfadfc2010-05-19 22:15:50 -0600671 if (!port->host_connected)
672 return true;
673
674 spin_lock_irq(&port->outvq_lock);
675 /*
676 * Check if the Host has consumed any buffers since we last
677 * sent data (this is only applicable for nonblocking ports).
678 */
679 reclaim_consumed_buffers(port);
680 ret = port->outvq_full;
681 spin_unlock_irq(&port->outvq_lock);
682
683 return ret;
684}
685
Amit Shah2030fa42009-12-21 21:49:30 +0530686static ssize_t port_fops_read(struct file *filp, char __user *ubuf,
687 size_t count, loff_t *offp)
688{
689 struct port *port;
690 ssize_t ret;
691
692 port = filp->private_data;
693
694 if (!port_has_data(port)) {
695 /*
696 * If nothing's connected on the host just return 0 in
697 * case of list_empty; this tells the userspace app
698 * that there's no connection
699 */
700 if (!port->host_connected)
701 return 0;
702 if (filp->f_flags & O_NONBLOCK)
703 return -EAGAIN;
704
Amit Shaha08fa922011-09-14 13:06:41 +0530705 ret = wait_event_freezable(port->waitqueue,
706 !will_read_block(port));
Amit Shah2030fa42009-12-21 21:49:30 +0530707 if (ret < 0)
708 return ret;
709 }
Amit Shahb3dddb92010-09-02 18:11:45 +0530710 /* Port got hot-unplugged. */
711 if (!port->guest_connected)
712 return -ENODEV;
Amit Shah2030fa42009-12-21 21:49:30 +0530713 /*
714 * We could've received a disconnection message while we were
715 * waiting for more data.
716 *
717 * This check is not clubbed in the if() statement above as we
718 * might receive some data as well as the host could get
719 * disconnected after we got woken up from our wait. So we
720 * really want to give off whatever data we have and only then
721 * check for host_connected.
722 */
723 if (!port_has_data(port) && !port->host_connected)
724 return 0;
725
726 return fill_readbuf(port, ubuf, count, true);
727}
728
Masami Hiramatsuefe75d22012-08-09 21:31:00 +0900729static int wait_port_writable(struct port *port, bool nonblock)
730{
731 int ret;
732
733 if (will_write_block(port)) {
734 if (nonblock)
735 return -EAGAIN;
736
737 ret = wait_event_freezable(port->waitqueue,
738 !will_write_block(port));
739 if (ret < 0)
740 return ret;
741 }
742 /* Port got hot-unplugged. */
743 if (!port->guest_connected)
744 return -ENODEV;
745
746 return 0;
747}
748
Amit Shah2030fa42009-12-21 21:49:30 +0530749static ssize_t port_fops_write(struct file *filp, const char __user *ubuf,
750 size_t count, loff_t *offp)
751{
752 struct port *port;
753 char *buf;
754 ssize_t ret;
Amit Shahcdfadfc2010-05-19 22:15:50 -0600755 bool nonblock;
Amit Shah2030fa42009-12-21 21:49:30 +0530756
Amit Shah65745422010-09-14 13:26:16 +0530757 /* Userspace could be out to fool us */
758 if (!count)
759 return 0;
760
Amit Shah2030fa42009-12-21 21:49:30 +0530761 port = filp->private_data;
762
Amit Shahcdfadfc2010-05-19 22:15:50 -0600763 nonblock = filp->f_flags & O_NONBLOCK;
764
Masami Hiramatsuefe75d22012-08-09 21:31:00 +0900765 ret = wait_port_writable(port, nonblock);
766 if (ret < 0)
767 return ret;
Amit Shahcdfadfc2010-05-19 22:15:50 -0600768
Amit Shah2030fa42009-12-21 21:49:30 +0530769 count = min((size_t)(32 * 1024), count);
770
771 buf = kmalloc(count, GFP_KERNEL);
772 if (!buf)
773 return -ENOMEM;
774
775 ret = copy_from_user(buf, ubuf, count);
776 if (ret) {
777 ret = -EFAULT;
778 goto free_buf;
779 }
780
Amit Shah531295e2010-10-20 13:45:43 +1030781 /*
782 * We now ask send_buf() to not spin for generic ports -- we
783 * can re-use the same code path that non-blocking file
784 * descriptors take for blocking file descriptors since the
785 * wait is already done and we're certain the write will go
786 * through to the host.
787 */
788 nonblock = true;
Amit Shahcdfadfc2010-05-19 22:15:50 -0600789 ret = send_buf(port, buf, count, nonblock);
790
791 if (nonblock && ret > 0)
792 goto out;
793
Amit Shah2030fa42009-12-21 21:49:30 +0530794free_buf:
795 kfree(buf);
Amit Shahcdfadfc2010-05-19 22:15:50 -0600796out:
Amit Shah2030fa42009-12-21 21:49:30 +0530797 return ret;
798}
799
Masami Hiramatsueb5e89f2012-08-09 21:30:39 +0900800struct sg_list {
801 unsigned int n;
Masami Hiramatsu8ca84a52012-08-09 21:31:20 +0900802 unsigned int size;
Masami Hiramatsueb5e89f2012-08-09 21:30:39 +0900803 size_t len;
804 struct scatterlist *sg;
805};
806
807static int pipe_to_sg(struct pipe_inode_info *pipe, struct pipe_buffer *buf,
808 struct splice_desc *sd)
809{
810 struct sg_list *sgl = sd->u.data;
Masami Hiramatsuec8fc872012-08-09 21:30:50 +0900811 unsigned int offset, len;
Masami Hiramatsueb5e89f2012-08-09 21:30:39 +0900812
Masami Hiramatsu8ca84a52012-08-09 21:31:20 +0900813 if (sgl->n == sgl->size)
Masami Hiramatsueb5e89f2012-08-09 21:30:39 +0900814 return 0;
815
816 /* Try lock this page */
817 if (buf->ops->steal(pipe, buf) == 0) {
818 /* Get reference and unlock page for moving */
819 get_page(buf->page);
820 unlock_page(buf->page);
821
822 len = min(buf->len, sd->len);
823 sg_set_page(&(sgl->sg[sgl->n]), buf->page, len, buf->offset);
Masami Hiramatsuec8fc872012-08-09 21:30:50 +0900824 } else {
825 /* Failback to copying a page */
826 struct page *page = alloc_page(GFP_KERNEL);
827 char *src = buf->ops->map(pipe, buf, 1);
828 char *dst;
829
830 if (!page)
831 return -ENOMEM;
832 dst = kmap(page);
833
834 offset = sd->pos & ~PAGE_MASK;
835
836 len = sd->len;
837 if (len + offset > PAGE_SIZE)
838 len = PAGE_SIZE - offset;
839
840 memcpy(dst + offset, src + buf->offset, len);
841
842 kunmap(page);
843 buf->ops->unmap(pipe, buf, src);
844
845 sg_set_page(&(sgl->sg[sgl->n]), page, len, offset);
Masami Hiramatsueb5e89f2012-08-09 21:30:39 +0900846 }
Masami Hiramatsuec8fc872012-08-09 21:30:50 +0900847 sgl->n++;
848 sgl->len += len;
Masami Hiramatsueb5e89f2012-08-09 21:30:39 +0900849
850 return len;
851}
852
853/* Faster zero-copy write by splicing */
854static ssize_t port_fops_splice_write(struct pipe_inode_info *pipe,
855 struct file *filp, loff_t *ppos,
856 size_t len, unsigned int flags)
857{
858 struct port *port = filp->private_data;
859 struct sg_list sgl;
860 ssize_t ret;
861 struct splice_desc sd = {
862 .total_len = len,
863 .flags = flags,
864 .pos = *ppos,
865 .u.data = &sgl,
866 };
867
Masami Hiramatsuefe75d22012-08-09 21:31:00 +0900868 ret = wait_port_writable(port, filp->f_flags & O_NONBLOCK);
869 if (ret < 0)
870 return ret;
871
Masami Hiramatsueb5e89f2012-08-09 21:30:39 +0900872 sgl.n = 0;
873 sgl.len = 0;
Masami Hiramatsu8ca84a52012-08-09 21:31:20 +0900874 sgl.size = pipe->nrbufs;
875 sgl.sg = kmalloc(sizeof(struct scatterlist) * sgl.size, GFP_KERNEL);
Masami Hiramatsueb5e89f2012-08-09 21:30:39 +0900876 if (unlikely(!sgl.sg))
877 return -ENOMEM;
878
Masami Hiramatsu8ca84a52012-08-09 21:31:20 +0900879 sg_init_table(sgl.sg, sgl.size);
Masami Hiramatsueb5e89f2012-08-09 21:30:39 +0900880 ret = __splice_from_pipe(pipe, &sd, pipe_to_sg);
881 if (likely(ret > 0))
882 ret = send_pages(port, sgl.sg, sgl.n, sgl.len, true);
883
Sjur Brændelandfe529532012-10-15 09:57:33 +0200884 if (unlikely(ret <= 0))
885 kfree(sgl.sg);
Masami Hiramatsueb5e89f2012-08-09 21:30:39 +0900886 return ret;
887}
888
Amit Shah2030fa42009-12-21 21:49:30 +0530889static unsigned int port_fops_poll(struct file *filp, poll_table *wait)
890{
891 struct port *port;
892 unsigned int ret;
893
894 port = filp->private_data;
895 poll_wait(filp, &port->waitqueue, wait);
896
Amit Shah8529a502010-09-02 18:11:44 +0530897 if (!port->guest_connected) {
898 /* Port got unplugged */
899 return POLLHUP;
900 }
Amit Shah2030fa42009-12-21 21:49:30 +0530901 ret = 0;
Hans de Goede6df7aad2010-09-16 14:43:08 +0530902 if (!will_read_block(port))
Amit Shah2030fa42009-12-21 21:49:30 +0530903 ret |= POLLIN | POLLRDNORM;
Amit Shahcdfadfc2010-05-19 22:15:50 -0600904 if (!will_write_block(port))
Amit Shah2030fa42009-12-21 21:49:30 +0530905 ret |= POLLOUT;
906 if (!port->host_connected)
907 ret |= POLLHUP;
908
909 return ret;
910}
911
Amit Shahb353a6b2010-09-02 18:38:29 +0530912static void remove_port(struct kref *kref);
913
Amit Shah2030fa42009-12-21 21:49:30 +0530914static int port_fops_release(struct inode *inode, struct file *filp)
915{
916 struct port *port;
917
918 port = filp->private_data;
919
920 /* Notify host of port being closed */
921 send_control_msg(port, VIRTIO_CONSOLE_PORT_OPEN, 0);
922
Amit Shah88f251a2009-12-21 22:15:30 +0530923 spin_lock_irq(&port->inbuf_lock);
Amit Shah3c7969c2009-11-26 11:25:38 +0530924 port->guest_connected = false;
925
Amit Shah88f251a2009-12-21 22:15:30 +0530926 discard_port_data(port);
927
928 spin_unlock_irq(&port->inbuf_lock);
929
Amit Shahcdfadfc2010-05-19 22:15:50 -0600930 spin_lock_irq(&port->outvq_lock);
931 reclaim_consumed_buffers(port);
932 spin_unlock_irq(&port->outvq_lock);
933
Amit Shahb353a6b2010-09-02 18:38:29 +0530934 /*
935 * Locks aren't necessary here as a port can't be opened after
936 * unplug, and if a port isn't unplugged, a kref would already
937 * exist for the port. Plus, taking ports_lock here would
938 * create a dependency on other locks taken by functions
939 * inside remove_port if we're the last holder of the port,
940 * creating many problems.
941 */
942 kref_put(&port->kref, remove_port);
943
Amit Shah2030fa42009-12-21 21:49:30 +0530944 return 0;
945}
946
947static int port_fops_open(struct inode *inode, struct file *filp)
948{
949 struct cdev *cdev = inode->i_cdev;
950 struct port *port;
Amit Shah8ad37e82010-09-02 18:11:48 +0530951 int ret;
Amit Shah2030fa42009-12-21 21:49:30 +0530952
Amit Shah04950cd2010-09-02 18:20:58 +0530953 port = find_port_by_devt(cdev->dev);
Amit Shah2030fa42009-12-21 21:49:30 +0530954 filp->private_data = port;
955
Amit Shahb353a6b2010-09-02 18:38:29 +0530956 /* Prevent against a port getting hot-unplugged at the same time */
957 spin_lock_irq(&port->portdev->ports_lock);
958 kref_get(&port->kref);
959 spin_unlock_irq(&port->portdev->ports_lock);
960
Amit Shah2030fa42009-12-21 21:49:30 +0530961 /*
962 * Don't allow opening of console port devices -- that's done
963 * via /dev/hvc
964 */
Amit Shah8ad37e82010-09-02 18:11:48 +0530965 if (is_console_port(port)) {
966 ret = -ENXIO;
967 goto out;
968 }
Amit Shah2030fa42009-12-21 21:49:30 +0530969
Amit Shah3c7969c2009-11-26 11:25:38 +0530970 /* Allow only one process to open a particular port at a time */
971 spin_lock_irq(&port->inbuf_lock);
972 if (port->guest_connected) {
973 spin_unlock_irq(&port->inbuf_lock);
Amit Shah8ad37e82010-09-02 18:11:48 +0530974 ret = -EMFILE;
975 goto out;
Amit Shah3c7969c2009-11-26 11:25:38 +0530976 }
977
978 port->guest_connected = true;
979 spin_unlock_irq(&port->inbuf_lock);
980
Amit Shahcdfadfc2010-05-19 22:15:50 -0600981 spin_lock_irq(&port->outvq_lock);
982 /*
983 * There might be a chance that we missed reclaiming a few
984 * buffers in the window of the port getting previously closed
985 * and opening now.
986 */
987 reclaim_consumed_buffers(port);
988 spin_unlock_irq(&port->outvq_lock);
989
Amit Shah299fb612010-09-16 14:43:09 +0530990 nonseekable_open(inode, filp);
991
Amit Shah2030fa42009-12-21 21:49:30 +0530992 /* Notify host of port being opened */
993 send_control_msg(filp->private_data, VIRTIO_CONSOLE_PORT_OPEN, 1);
994
995 return 0;
Amit Shah8ad37e82010-09-02 18:11:48 +0530996out:
Amit Shahb353a6b2010-09-02 18:38:29 +0530997 kref_put(&port->kref, remove_port);
Amit Shah8ad37e82010-09-02 18:11:48 +0530998 return ret;
Amit Shah2030fa42009-12-21 21:49:30 +0530999}
1000
Amit Shah3eae0ad2010-09-02 18:47:52 +05301001static int port_fops_fasync(int fd, struct file *filp, int mode)
1002{
1003 struct port *port;
1004
1005 port = filp->private_data;
1006 return fasync_helper(fd, filp, mode, &port->async_queue);
1007}
1008
Amit Shah2030fa42009-12-21 21:49:30 +05301009/*
1010 * The file operations that we support: programs in the guest can open
1011 * a console device, read from it, write to it, poll for data and
1012 * close it. The devices are at
1013 * /dev/vport<device number>p<port number>
1014 */
1015static const struct file_operations port_fops = {
1016 .owner = THIS_MODULE,
1017 .open = port_fops_open,
1018 .read = port_fops_read,
1019 .write = port_fops_write,
Masami Hiramatsueb5e89f2012-08-09 21:30:39 +09001020 .splice_write = port_fops_splice_write,
Amit Shah2030fa42009-12-21 21:49:30 +05301021 .poll = port_fops_poll,
1022 .release = port_fops_release,
Amit Shah3eae0ad2010-09-02 18:47:52 +05301023 .fasync = port_fops_fasync,
Amit Shah299fb612010-09-16 14:43:09 +05301024 .llseek = no_llseek,
Amit Shah2030fa42009-12-21 21:49:30 +05301025};
1026
Amit Shahe27b5192010-01-18 19:15:02 +05301027/*
Rusty Russella23ea922010-01-18 19:14:55 +05301028 * The put_chars() callback is pretty straightforward.
Rusty Russell31610432007-10-22 11:03:39 +10001029 *
Rusty Russella23ea922010-01-18 19:14:55 +05301030 * We turn the characters into a scatter-gather list, add it to the
1031 * output queue and then kick the Host. Then we sit here waiting for
1032 * it to finish: inefficient in theory, but in practice
1033 * implementations will do it immediately (lguest's Launcher does).
1034 */
Rusty Russell31610432007-10-22 11:03:39 +10001035static int put_chars(u32 vtermno, const char *buf, int count)
1036{
Rusty Russell21206ed2010-01-18 19:15:00 +05301037 struct port *port;
Amit Shah38edf582010-01-18 19:15:05 +05301038
François Diakhaté162a6892010-03-23 18:23:15 +05301039 if (unlikely(early_put_chars))
1040 return early_put_chars(vtermno, buf, count);
1041
Amit Shah38edf582010-01-18 19:15:05 +05301042 port = find_port_by_vtermno(vtermno);
1043 if (!port)
Amit Shah6dc69f972010-05-19 22:15:47 -06001044 return -EPIPE;
Rusty Russell31610432007-10-22 11:03:39 +10001045
Amit Shahcdfadfc2010-05-19 22:15:50 -06001046 return send_buf(port, (void *)buf, count, false);
Rusty Russell31610432007-10-22 11:03:39 +10001047}
1048
Rusty Russella23ea922010-01-18 19:14:55 +05301049/*
Rusty Russella23ea922010-01-18 19:14:55 +05301050 * get_chars() is the callback from the hvc_console infrastructure
1051 * when an interrupt is received.
Rusty Russell31610432007-10-22 11:03:39 +10001052 *
Amit Shah203baab2010-01-18 19:15:12 +05301053 * We call out to fill_readbuf that gets us the required data from the
1054 * buffers that are queued up.
Rusty Russella23ea922010-01-18 19:14:55 +05301055 */
Rusty Russell31610432007-10-22 11:03:39 +10001056static int get_chars(u32 vtermno, char *buf, int count)
1057{
Rusty Russell21206ed2010-01-18 19:15:00 +05301058 struct port *port;
Rusty Russell31610432007-10-22 11:03:39 +10001059
Amit Shah6dc69f972010-05-19 22:15:47 -06001060 /* If we've not set up the port yet, we have no input to give. */
1061 if (unlikely(early_put_chars))
1062 return 0;
1063
Amit Shah38edf582010-01-18 19:15:05 +05301064 port = find_port_by_vtermno(vtermno);
1065 if (!port)
Amit Shah6dc69f972010-05-19 22:15:47 -06001066 return -EPIPE;
Rusty Russell21206ed2010-01-18 19:15:00 +05301067
1068 /* If we don't have an input queue yet, we can't get input. */
1069 BUG_ON(!port->in_vq);
1070
Amit Shahb766cee2009-12-21 21:26:45 +05301071 return fill_readbuf(port, buf, count, false);
Rusty Russell31610432007-10-22 11:03:39 +10001072}
Rusty Russell31610432007-10-22 11:03:39 +10001073
Amit Shahcb06e362010-01-18 19:15:08 +05301074static void resize_console(struct port *port)
Christian Borntraegerc2983452008-11-25 13:36:26 +01001075{
Amit Shahcb06e362010-01-18 19:15:08 +05301076 struct virtio_device *vdev;
Christian Borntraegerc2983452008-11-25 13:36:26 +01001077
Amit Shah2de16a42010-03-19 17:36:44 +05301078 /* The port could have been hot-unplugged */
Amit Shah97788292010-05-06 02:05:08 +05301079 if (!port || !is_console_port(port))
Amit Shah2de16a42010-03-19 17:36:44 +05301080 return;
1081
Amit Shahcb06e362010-01-18 19:15:08 +05301082 vdev = port->portdev->vdev;
Amit Shah97788292010-05-06 02:05:08 +05301083 if (virtio_has_feature(vdev, VIRTIO_CONSOLE_F_SIZE))
1084 hvc_resize(port->cons.hvc, port->cons.ws);
Christian Borntraegerc2983452008-11-25 13:36:26 +01001085}
1086
Amit Shah38edf582010-01-18 19:15:05 +05301087/* We set the configuration at this point, since we now have a tty */
Christian Borntraeger91fcad12008-06-20 15:24:15 +02001088static int notifier_add_vio(struct hvc_struct *hp, int data)
1089{
Amit Shah38edf582010-01-18 19:15:05 +05301090 struct port *port;
1091
1092 port = find_port_by_vtermno(hp->vtermno);
1093 if (!port)
1094 return -EINVAL;
1095
Christian Borntraeger91fcad12008-06-20 15:24:15 +02001096 hp->irq_requested = 1;
Amit Shahcb06e362010-01-18 19:15:08 +05301097 resize_console(port);
Christian Borntraegerc2983452008-11-25 13:36:26 +01001098
Christian Borntraeger91fcad12008-06-20 15:24:15 +02001099 return 0;
1100}
1101
1102static void notifier_del_vio(struct hvc_struct *hp, int data)
1103{
1104 hp->irq_requested = 0;
1105}
1106
Amit Shah17634ba2009-12-21 21:03:25 +05301107/* The operations for console ports. */
Rusty Russell1dff3992009-11-28 12:20:26 +05301108static const struct hv_ops hv_ops = {
Rusty Russell971f3392010-01-18 19:14:56 +05301109 .get_chars = get_chars,
1110 .put_chars = put_chars,
1111 .notifier_add = notifier_add_vio,
1112 .notifier_del = notifier_del_vio,
1113 .notifier_hangup = notifier_del_vio,
1114};
1115
1116/*
1117 * Console drivers are initialized very early so boot messages can go
1118 * out, so we do things slightly differently from the generic virtio
1119 * initialization of the net and block drivers.
1120 *
1121 * At this stage, the console is output-only. It's too early to set
1122 * up a virtqueue, so we let the drivers do some boutique early-output
1123 * thing.
1124 */
1125int __init virtio_cons_early_init(int (*put_chars)(u32, const char *, int))
1126{
1127 early_put_chars = put_chars;
1128 return hvc_instantiate(0, 0, &hv_ops);
1129}
1130
Amit Shah17634ba2009-12-21 21:03:25 +05301131int init_port_console(struct port *port)
Amit Shahcfa6d372010-01-18 19:15:10 +05301132{
1133 int ret;
1134
1135 /*
1136 * The Host's telling us this port is a console port. Hook it
1137 * up with an hvc console.
1138 *
1139 * To set up and manage our virtual console, we call
1140 * hvc_alloc().
1141 *
1142 * The first argument of hvc_alloc() is the virtual console
1143 * number. The second argument is the parameter for the
1144 * notification mechanism (like irq number). We currently
1145 * leave this as zero, virtqueues have implicit notifications.
1146 *
1147 * The third argument is a "struct hv_ops" containing the
1148 * put_chars() get_chars(), notifier_add() and notifier_del()
1149 * pointers. The final argument is the output buffer size: we
1150 * can do any size, so we put PAGE_SIZE here.
1151 */
1152 port->cons.vtermno = pdrvdata.next_vtermno;
1153
1154 port->cons.hvc = hvc_alloc(port->cons.vtermno, 0, &hv_ops, PAGE_SIZE);
1155 if (IS_ERR(port->cons.hvc)) {
1156 ret = PTR_ERR(port->cons.hvc);
Amit Shah298add72010-01-18 16:35:23 +05301157 dev_err(port->dev,
1158 "error %d allocating hvc for port\n", ret);
Amit Shahcfa6d372010-01-18 19:15:10 +05301159 port->cons.hvc = NULL;
1160 return ret;
1161 }
1162 spin_lock_irq(&pdrvdata_lock);
1163 pdrvdata.next_vtermno++;
1164 list_add_tail(&port->cons.list, &pdrvdata.consoles);
1165 spin_unlock_irq(&pdrvdata_lock);
Amit Shah3c7969c2009-11-26 11:25:38 +05301166 port->guest_connected = true;
Amit Shahcfa6d372010-01-18 19:15:10 +05301167
Amit Shah1d051602010-05-19 22:15:49 -06001168 /*
1169 * Start using the new console output if this is the first
1170 * console to come up.
1171 */
1172 if (early_put_chars)
1173 early_put_chars = NULL;
1174
Amit Shah2030fa42009-12-21 21:49:30 +05301175 /* Notify host of port being opened */
1176 send_control_msg(port, VIRTIO_CONSOLE_PORT_OPEN, 1);
1177
Amit Shahcfa6d372010-01-18 19:15:10 +05301178 return 0;
1179}
1180
Amit Shah431edb82009-12-21 21:57:40 +05301181static ssize_t show_port_name(struct device *dev,
1182 struct device_attribute *attr, char *buffer)
1183{
1184 struct port *port;
1185
1186 port = dev_get_drvdata(dev);
1187
1188 return sprintf(buffer, "%s\n", port->name);
1189}
1190
1191static DEVICE_ATTR(name, S_IRUGO, show_port_name, NULL);
1192
1193static struct attribute *port_sysfs_entries[] = {
1194 &dev_attr_name.attr,
1195 NULL
1196};
1197
1198static struct attribute_group port_attribute_group = {
1199 .name = NULL, /* put in device directory */
1200 .attrs = port_sysfs_entries,
1201};
1202
Amit Shahd99393e2009-12-21 22:36:21 +05301203static ssize_t debugfs_read(struct file *filp, char __user *ubuf,
1204 size_t count, loff_t *offp)
1205{
1206 struct port *port;
1207 char *buf;
1208 ssize_t ret, out_offset, out_count;
1209
1210 out_count = 1024;
1211 buf = kmalloc(out_count, GFP_KERNEL);
1212 if (!buf)
1213 return -ENOMEM;
1214
1215 port = filp->private_data;
1216 out_offset = 0;
1217 out_offset += snprintf(buf + out_offset, out_count,
1218 "name: %s\n", port->name ? port->name : "");
1219 out_offset += snprintf(buf + out_offset, out_count - out_offset,
1220 "guest_connected: %d\n", port->guest_connected);
1221 out_offset += snprintf(buf + out_offset, out_count - out_offset,
1222 "host_connected: %d\n", port->host_connected);
1223 out_offset += snprintf(buf + out_offset, out_count - out_offset,
Amit Shahcdfadfc2010-05-19 22:15:50 -06001224 "outvq_full: %d\n", port->outvq_full);
1225 out_offset += snprintf(buf + out_offset, out_count - out_offset,
Amit Shah17e5b4f2011-09-14 13:06:46 +05301226 "bytes_sent: %lu\n", port->stats.bytes_sent);
1227 out_offset += snprintf(buf + out_offset, out_count - out_offset,
1228 "bytes_received: %lu\n",
1229 port->stats.bytes_received);
1230 out_offset += snprintf(buf + out_offset, out_count - out_offset,
1231 "bytes_discarded: %lu\n",
1232 port->stats.bytes_discarded);
1233 out_offset += snprintf(buf + out_offset, out_count - out_offset,
Amit Shahd99393e2009-12-21 22:36:21 +05301234 "is_console: %s\n",
1235 is_console_port(port) ? "yes" : "no");
1236 out_offset += snprintf(buf + out_offset, out_count - out_offset,
1237 "console_vtermno: %u\n", port->cons.vtermno);
1238
1239 ret = simple_read_from_buffer(ubuf, count, offp, buf, out_offset);
1240 kfree(buf);
1241 return ret;
1242}
1243
1244static const struct file_operations port_debugfs_ops = {
1245 .owner = THIS_MODULE,
Stephen Boyd234e3402012-04-05 14:25:11 -07001246 .open = simple_open,
Amit Shahd99393e2009-12-21 22:36:21 +05301247 .read = debugfs_read,
1248};
1249
Amit Shah97788292010-05-06 02:05:08 +05301250static void set_console_size(struct port *port, u16 rows, u16 cols)
1251{
1252 if (!port || !is_console_port(port))
1253 return;
1254
1255 port->cons.ws.ws_row = rows;
1256 port->cons.ws.ws_col = cols;
1257}
1258
Amit Shahc446f8f2010-05-19 22:15:48 -06001259static unsigned int fill_queue(struct virtqueue *vq, spinlock_t *lock)
1260{
1261 struct port_buffer *buf;
1262 unsigned int nr_added_bufs;
1263 int ret;
1264
1265 nr_added_bufs = 0;
1266 do {
1267 buf = alloc_buf(PAGE_SIZE);
1268 if (!buf)
1269 break;
1270
1271 spin_lock_irq(lock);
1272 ret = add_inbuf(vq, buf);
1273 if (ret < 0) {
1274 spin_unlock_irq(lock);
1275 free_buf(buf);
1276 break;
1277 }
1278 nr_added_bufs++;
1279 spin_unlock_irq(lock);
1280 } while (ret > 0);
1281
1282 return nr_added_bufs;
1283}
1284
Amit Shah3eae0ad2010-09-02 18:47:52 +05301285static void send_sigio_to_port(struct port *port)
1286{
1287 if (port->async_queue && port->guest_connected)
1288 kill_fasync(&port->async_queue, SIGIO, POLL_OUT);
1289}
1290
Amit Shahc446f8f2010-05-19 22:15:48 -06001291static int add_port(struct ports_device *portdev, u32 id)
1292{
1293 char debugfs_name[16];
1294 struct port *port;
1295 struct port_buffer *buf;
1296 dev_t devt;
1297 unsigned int nr_added_bufs;
1298 int err;
1299
1300 port = kmalloc(sizeof(*port), GFP_KERNEL);
1301 if (!port) {
1302 err = -ENOMEM;
1303 goto fail;
1304 }
Amit Shahb353a6b2010-09-02 18:38:29 +05301305 kref_init(&port->kref);
Amit Shahc446f8f2010-05-19 22:15:48 -06001306
1307 port->portdev = portdev;
1308 port->id = id;
1309
1310 port->name = NULL;
1311 port->inbuf = NULL;
1312 port->cons.hvc = NULL;
Amit Shah3eae0ad2010-09-02 18:47:52 +05301313 port->async_queue = NULL;
Amit Shahc446f8f2010-05-19 22:15:48 -06001314
Amit Shah97788292010-05-06 02:05:08 +05301315 port->cons.ws.ws_row = port->cons.ws.ws_col = 0;
1316
Amit Shahc446f8f2010-05-19 22:15:48 -06001317 port->host_connected = port->guest_connected = false;
Amit Shah17e5b4f2011-09-14 13:06:46 +05301318 port->stats = (struct port_stats) { 0 };
Amit Shahc446f8f2010-05-19 22:15:48 -06001319
Amit Shahcdfadfc2010-05-19 22:15:50 -06001320 port->outvq_full = false;
1321
Amit Shahc446f8f2010-05-19 22:15:48 -06001322 port->in_vq = portdev->in_vqs[port->id];
1323 port->out_vq = portdev->out_vqs[port->id];
1324
Amit Shahd22a6982010-09-02 18:20:59 +05301325 port->cdev = cdev_alloc();
1326 if (!port->cdev) {
1327 dev_err(&port->portdev->vdev->dev, "Error allocating cdev\n");
1328 err = -ENOMEM;
1329 goto free_port;
1330 }
1331 port->cdev->ops = &port_fops;
Amit Shahc446f8f2010-05-19 22:15:48 -06001332
1333 devt = MKDEV(portdev->chr_major, id);
Amit Shahd22a6982010-09-02 18:20:59 +05301334 err = cdev_add(port->cdev, devt, 1);
Amit Shahc446f8f2010-05-19 22:15:48 -06001335 if (err < 0) {
1336 dev_err(&port->portdev->vdev->dev,
1337 "Error %d adding cdev for port %u\n", err, id);
Amit Shahd22a6982010-09-02 18:20:59 +05301338 goto free_cdev;
Amit Shahc446f8f2010-05-19 22:15:48 -06001339 }
1340 port->dev = device_create(pdrvdata.class, &port->portdev->vdev->dev,
1341 devt, port, "vport%up%u",
1342 port->portdev->drv_index, id);
1343 if (IS_ERR(port->dev)) {
1344 err = PTR_ERR(port->dev);
1345 dev_err(&port->portdev->vdev->dev,
1346 "Error %d creating device for port %u\n",
1347 err, id);
1348 goto free_cdev;
1349 }
1350
1351 spin_lock_init(&port->inbuf_lock);
Amit Shahcdfadfc2010-05-19 22:15:50 -06001352 spin_lock_init(&port->outvq_lock);
Amit Shahc446f8f2010-05-19 22:15:48 -06001353 init_waitqueue_head(&port->waitqueue);
1354
1355 /* Fill the in_vq with buffers so the host can send us data. */
1356 nr_added_bufs = fill_queue(port->in_vq, &port->inbuf_lock);
1357 if (!nr_added_bufs) {
1358 dev_err(port->dev, "Error allocating inbufs\n");
1359 err = -ENOMEM;
1360 goto free_device;
1361 }
1362
1363 /*
1364 * If we're not using multiport support, this has to be a console port
1365 */
1366 if (!use_multiport(port->portdev)) {
1367 err = init_port_console(port);
1368 if (err)
1369 goto free_inbufs;
1370 }
1371
1372 spin_lock_irq(&portdev->ports_lock);
1373 list_add_tail(&port->list, &port->portdev->ports);
1374 spin_unlock_irq(&portdev->ports_lock);
1375
1376 /*
1377 * Tell the Host we're set so that it can send us various
1378 * configuration parameters for this port (eg, port name,
1379 * caching, whether this is a console port, etc.)
1380 */
1381 send_control_msg(port, VIRTIO_CONSOLE_PORT_READY, 1);
1382
1383 if (pdrvdata.debugfs_dir) {
1384 /*
1385 * Finally, create the debugfs file that we can use to
1386 * inspect a port's state at any time
1387 */
1388 sprintf(debugfs_name, "vport%up%u",
1389 port->portdev->drv_index, id);
1390 port->debugfs_file = debugfs_create_file(debugfs_name, 0444,
1391 pdrvdata.debugfs_dir,
1392 port,
1393 &port_debugfs_ops);
1394 }
1395 return 0;
1396
1397free_inbufs:
1398 while ((buf = virtqueue_detach_unused_buf(port->in_vq)))
1399 free_buf(buf);
1400free_device:
1401 device_destroy(pdrvdata.class, port->dev->devt);
1402free_cdev:
Amit Shahd22a6982010-09-02 18:20:59 +05301403 cdev_del(port->cdev);
Amit Shahc446f8f2010-05-19 22:15:48 -06001404free_port:
1405 kfree(port);
1406fail:
1407 /* The host might want to notify management sw about port add failure */
Julia Lawall0643e4c2010-05-15 11:45:53 +02001408 __send_control_msg(portdev, id, VIRTIO_CONSOLE_PORT_READY, 0);
Amit Shahc446f8f2010-05-19 22:15:48 -06001409 return err;
1410}
1411
Amit Shahb353a6b2010-09-02 18:38:29 +05301412/* No users remain, remove all port-specific data. */
1413static void remove_port(struct kref *kref)
1414{
1415 struct port *port;
1416
1417 port = container_of(kref, struct port, kref);
1418
1419 sysfs_remove_group(&port->dev->kobj, &port_attribute_group);
1420 device_destroy(pdrvdata.class, port->dev->devt);
1421 cdev_del(port->cdev);
1422
1423 kfree(port->name);
1424
1425 debugfs_remove(port->debugfs_file);
1426
1427 kfree(port);
1428}
1429
Amit Shaha0e2dbf2011-12-22 16:58:27 +05301430static void remove_port_data(struct port *port)
1431{
1432 struct port_buffer *buf;
1433
1434 /* Remove unused data this port might have received. */
1435 discard_port_data(port);
1436
1437 reclaim_consumed_buffers(port);
1438
1439 /* Remove buffers we queued up for the Host to send us data in. */
1440 while ((buf = virtqueue_detach_unused_buf(port->in_vq)))
1441 free_buf(buf);
sjur.brandeland@stericsson.comeb34f122012-11-16 11:20:19 +10301442
1443 /* Free pending buffers from the out-queue. */
1444 while ((buf = virtqueue_detach_unused_buf(port->out_vq)))
1445 free_buf(buf);
Amit Shaha0e2dbf2011-12-22 16:58:27 +05301446}
1447
Amit Shahb353a6b2010-09-02 18:38:29 +05301448/*
1449 * Port got unplugged. Remove port from portdev's list and drop the
1450 * kref reference. If no userspace has this port opened, it will
1451 * result in immediate removal the port.
1452 */
1453static void unplug_port(struct port *port)
Amit Shah1f7aa422009-12-21 22:27:31 +05301454{
Amit Shahb353a6b2010-09-02 18:38:29 +05301455 spin_lock_irq(&port->portdev->ports_lock);
1456 list_del(&port->list);
1457 spin_unlock_irq(&port->portdev->ports_lock);
1458
Amit Shah00476342010-05-27 13:24:39 +05301459 if (port->guest_connected) {
1460 port->guest_connected = false;
1461 port->host_connected = false;
1462 wake_up_interruptible(&port->waitqueue);
Amit Shaha461e112010-09-02 18:47:54 +05301463
1464 /* Let the app know the port is going down. */
1465 send_sigio_to_port(port);
Amit Shah00476342010-05-27 13:24:39 +05301466 }
1467
Amit Shah1f7aa422009-12-21 22:27:31 +05301468 if (is_console_port(port)) {
1469 spin_lock_irq(&pdrvdata_lock);
1470 list_del(&port->cons.list);
1471 spin_unlock_irq(&pdrvdata_lock);
1472 hvc_remove(port->cons.hvc);
1473 }
Amit Shah1f7aa422009-12-21 22:27:31 +05301474
Amit Shaha0e2dbf2011-12-22 16:58:27 +05301475 remove_port_data(port);
Amit Shaha9cdd482010-02-12 10:32:15 +05301476
Amit Shahb353a6b2010-09-02 18:38:29 +05301477 /*
1478 * We should just assume the device itself has gone off --
1479 * else a close on an open port later will try to send out a
1480 * control message.
1481 */
1482 port->portdev = NULL;
Amit Shah1f7aa422009-12-21 22:27:31 +05301483
Amit Shahb353a6b2010-09-02 18:38:29 +05301484 /*
1485 * Locks around here are not necessary - a port can't be
1486 * opened after we removed the port struct from ports_list
1487 * above.
1488 */
1489 kref_put(&port->kref, remove_port);
Amit Shah1f7aa422009-12-21 22:27:31 +05301490}
1491
Amit Shah17634ba2009-12-21 21:03:25 +05301492/* Any private messages that the Host and Guest want to share */
1493static void handle_control_message(struct ports_device *portdev,
1494 struct port_buffer *buf)
1495{
1496 struct virtio_console_control *cpkt;
1497 struct port *port;
Amit Shah431edb82009-12-21 21:57:40 +05301498 size_t name_size;
1499 int err;
Amit Shah17634ba2009-12-21 21:03:25 +05301500
1501 cpkt = (struct virtio_console_control *)(buf->buf + buf->offset);
1502
1503 port = find_port_by_id(portdev, cpkt->id);
Amit Shahf909f852010-05-19 22:15:48 -06001504 if (!port && cpkt->event != VIRTIO_CONSOLE_PORT_ADD) {
Amit Shah17634ba2009-12-21 21:03:25 +05301505 /* No valid header at start of buffer. Drop it. */
1506 dev_dbg(&portdev->vdev->dev,
1507 "Invalid index %u in control packet\n", cpkt->id);
1508 return;
1509 }
1510
1511 switch (cpkt->event) {
Amit Shahf909f852010-05-19 22:15:48 -06001512 case VIRTIO_CONSOLE_PORT_ADD:
1513 if (port) {
Amit Shah1d051602010-05-19 22:15:49 -06001514 dev_dbg(&portdev->vdev->dev,
1515 "Port %u already added\n", port->id);
Amit Shahf909f852010-05-19 22:15:48 -06001516 send_control_msg(port, VIRTIO_CONSOLE_PORT_READY, 1);
1517 break;
1518 }
1519 if (cpkt->id >= portdev->config.max_nr_ports) {
1520 dev_warn(&portdev->vdev->dev,
1521 "Request for adding port with out-of-bound id %u, max. supported id: %u\n",
1522 cpkt->id, portdev->config.max_nr_ports - 1);
1523 break;
1524 }
1525 add_port(portdev, cpkt->id);
1526 break;
1527 case VIRTIO_CONSOLE_PORT_REMOVE:
Amit Shahb353a6b2010-09-02 18:38:29 +05301528 unplug_port(port);
Amit Shahf909f852010-05-19 22:15:48 -06001529 break;
Amit Shah17634ba2009-12-21 21:03:25 +05301530 case VIRTIO_CONSOLE_CONSOLE_PORT:
1531 if (!cpkt->value)
1532 break;
1533 if (is_console_port(port))
1534 break;
1535
1536 init_port_console(port);
Christian Borntraeger5e384832011-09-22 23:44:23 +05301537 complete(&early_console_added);
Amit Shah17634ba2009-12-21 21:03:25 +05301538 /*
1539 * Could remove the port here in case init fails - but
1540 * have to notify the host first.
1541 */
1542 break;
Amit Shah8345adb2010-05-06 02:05:09 +05301543 case VIRTIO_CONSOLE_RESIZE: {
1544 struct {
1545 __u16 rows;
1546 __u16 cols;
1547 } size;
1548
Amit Shah17634ba2009-12-21 21:03:25 +05301549 if (!is_console_port(port))
1550 break;
Amit Shah8345adb2010-05-06 02:05:09 +05301551
1552 memcpy(&size, buf->buf + buf->offset + sizeof(*cpkt),
1553 sizeof(size));
1554 set_console_size(port, size.rows, size.cols);
1555
Amit Shah17634ba2009-12-21 21:03:25 +05301556 port->cons.hvc->irq_requested = 1;
1557 resize_console(port);
1558 break;
Amit Shah8345adb2010-05-06 02:05:09 +05301559 }
Amit Shah2030fa42009-12-21 21:49:30 +05301560 case VIRTIO_CONSOLE_PORT_OPEN:
1561 port->host_connected = cpkt->value;
1562 wake_up_interruptible(&port->waitqueue);
Amit Shahcdfadfc2010-05-19 22:15:50 -06001563 /*
1564 * If the host port got closed and the host had any
1565 * unconsumed buffers, we'll be able to reclaim them
1566 * now.
1567 */
1568 spin_lock_irq(&port->outvq_lock);
1569 reclaim_consumed_buffers(port);
1570 spin_unlock_irq(&port->outvq_lock);
Amit Shah3eae0ad2010-09-02 18:47:52 +05301571
1572 /*
1573 * If the guest is connected, it'll be interested in
1574 * knowing the host connection state changed.
1575 */
1576 send_sigio_to_port(port);
Amit Shah2030fa42009-12-21 21:49:30 +05301577 break;
Amit Shah431edb82009-12-21 21:57:40 +05301578 case VIRTIO_CONSOLE_PORT_NAME:
1579 /*
Amit Shah291024e2011-09-14 13:06:40 +05301580 * If we woke up after hibernation, we can get this
1581 * again. Skip it in that case.
1582 */
1583 if (port->name)
1584 break;
1585
1586 /*
Amit Shah431edb82009-12-21 21:57:40 +05301587 * Skip the size of the header and the cpkt to get the size
1588 * of the name that was sent
1589 */
1590 name_size = buf->len - buf->offset - sizeof(*cpkt) + 1;
1591
1592 port->name = kmalloc(name_size, GFP_KERNEL);
1593 if (!port->name) {
1594 dev_err(port->dev,
1595 "Not enough space to store port name\n");
1596 break;
1597 }
1598 strncpy(port->name, buf->buf + buf->offset + sizeof(*cpkt),
1599 name_size - 1);
1600 port->name[name_size - 1] = 0;
1601
1602 /*
1603 * Since we only have one sysfs attribute, 'name',
1604 * create it only if we have a name for the port.
1605 */
1606 err = sysfs_create_group(&port->dev->kobj,
1607 &port_attribute_group);
Amit Shahec642132010-03-19 17:36:43 +05301608 if (err) {
Amit Shah431edb82009-12-21 21:57:40 +05301609 dev_err(port->dev,
1610 "Error %d creating sysfs device attributes\n",
1611 err);
Amit Shahec642132010-03-19 17:36:43 +05301612 } else {
1613 /*
1614 * Generate a udev event so that appropriate
1615 * symlinks can be created based on udev
1616 * rules.
1617 */
1618 kobject_uevent(&port->dev->kobj, KOBJ_CHANGE);
1619 }
Amit Shah431edb82009-12-21 21:57:40 +05301620 break;
Amit Shah17634ba2009-12-21 21:03:25 +05301621 }
1622}
1623
1624static void control_work_handler(struct work_struct *work)
1625{
1626 struct ports_device *portdev;
1627 struct virtqueue *vq;
1628 struct port_buffer *buf;
1629 unsigned int len;
1630
1631 portdev = container_of(work, struct ports_device, control_work);
1632 vq = portdev->c_ivq;
1633
1634 spin_lock(&portdev->cvq_lock);
Michael S. Tsirkin505b0452010-04-12 16:18:32 +03001635 while ((buf = virtqueue_get_buf(vq, &len))) {
Amit Shah17634ba2009-12-21 21:03:25 +05301636 spin_unlock(&portdev->cvq_lock);
1637
1638 buf->len = len;
1639 buf->offset = 0;
1640
1641 handle_control_message(portdev, buf);
1642
1643 spin_lock(&portdev->cvq_lock);
1644 if (add_inbuf(portdev->c_ivq, buf) < 0) {
1645 dev_warn(&portdev->vdev->dev,
1646 "Error adding buffer to queue\n");
1647 free_buf(buf);
1648 }
1649 }
1650 spin_unlock(&portdev->cvq_lock);
1651}
1652
Amit Shah2770c5e2011-01-31 13:06:36 +05301653static void out_intr(struct virtqueue *vq)
1654{
1655 struct port *port;
1656
1657 port = find_port_by_vq(vq->vdev->priv, vq);
1658 if (!port)
1659 return;
1660
1661 wake_up_interruptible(&port->waitqueue);
1662}
1663
Amit Shah17634ba2009-12-21 21:03:25 +05301664static void in_intr(struct virtqueue *vq)
1665{
1666 struct port *port;
1667 unsigned long flags;
1668
1669 port = find_port_by_vq(vq->vdev->priv, vq);
1670 if (!port)
1671 return;
1672
1673 spin_lock_irqsave(&port->inbuf_lock, flags);
Amit Shahd25a9dd2011-09-14 13:06:43 +05301674 port->inbuf = get_inbuf(port);
Amit Shah17634ba2009-12-21 21:03:25 +05301675
Amit Shah88f251a2009-12-21 22:15:30 +05301676 /*
1677 * Don't queue up data when port is closed. This condition
1678 * can be reached when a console port is not yet connected (no
1679 * tty is spawned) and the host sends out data to console
1680 * ports. For generic serial ports, the host won't
1681 * (shouldn't) send data till the guest is connected.
1682 */
1683 if (!port->guest_connected)
1684 discard_port_data(port);
1685
Amit Shah17634ba2009-12-21 21:03:25 +05301686 spin_unlock_irqrestore(&port->inbuf_lock, flags);
1687
Amit Shah2030fa42009-12-21 21:49:30 +05301688 wake_up_interruptible(&port->waitqueue);
1689
Amit Shah55f6bcc2010-09-02 18:47:53 +05301690 /* Send a SIGIO indicating new data in case the process asked for it */
1691 send_sigio_to_port(port);
1692
Amit Shah17634ba2009-12-21 21:03:25 +05301693 if (is_console_port(port) && hvc_poll(port->cons.hvc))
1694 hvc_kick();
1695}
1696
1697static void control_intr(struct virtqueue *vq)
1698{
1699 struct ports_device *portdev;
1700
1701 portdev = vq->vdev->priv;
1702 schedule_work(&portdev->control_work);
1703}
1704
Amit Shah7f5d8102009-12-21 22:22:08 +05301705static void config_intr(struct virtio_device *vdev)
1706{
1707 struct ports_device *portdev;
1708
1709 portdev = vdev->priv;
Amit Shah99f905f2010-05-19 22:15:48 -06001710
Amit Shah4038f5b72010-05-06 02:05:07 +05301711 if (!use_multiport(portdev)) {
Amit Shah97788292010-05-06 02:05:08 +05301712 struct port *port;
1713 u16 rows, cols;
1714
1715 vdev->config->get(vdev,
1716 offsetof(struct virtio_console_config, cols),
1717 &cols, sizeof(u16));
1718 vdev->config->get(vdev,
1719 offsetof(struct virtio_console_config, rows),
1720 &rows, sizeof(u16));
1721
1722 port = find_port_by_id(portdev, 0);
1723 set_console_size(port, rows, cols);
1724
Amit Shah4038f5b72010-05-06 02:05:07 +05301725 /*
1726 * We'll use this way of resizing only for legacy
1727 * support. For newer userspace
1728 * (VIRTIO_CONSOLE_F_MULTPORT+), use control messages
1729 * to indicate console size changes so that it can be
1730 * done per-port.
1731 */
Amit Shah97788292010-05-06 02:05:08 +05301732 resize_console(port);
Amit Shah4038f5b72010-05-06 02:05:07 +05301733 }
Amit Shah7f5d8102009-12-21 22:22:08 +05301734}
1735
Amit Shah2658a79a2010-01-18 19:15:11 +05301736static int init_vqs(struct ports_device *portdev)
1737{
1738 vq_callback_t **io_callbacks;
1739 char **io_names;
1740 struct virtqueue **vqs;
Amit Shah17634ba2009-12-21 21:03:25 +05301741 u32 i, j, nr_ports, nr_queues;
Amit Shah2658a79a2010-01-18 19:15:11 +05301742 int err;
1743
Amit Shah17634ba2009-12-21 21:03:25 +05301744 nr_ports = portdev->config.max_nr_ports;
1745 nr_queues = use_multiport(portdev) ? (nr_ports + 1) * 2 : 2;
Amit Shah2658a79a2010-01-18 19:15:11 +05301746
1747 vqs = kmalloc(nr_queues * sizeof(struct virtqueue *), GFP_KERNEL);
Amit Shah2658a79a2010-01-18 19:15:11 +05301748 io_callbacks = kmalloc(nr_queues * sizeof(vq_callback_t *), GFP_KERNEL);
Amit Shah2658a79a2010-01-18 19:15:11 +05301749 io_names = kmalloc(nr_queues * sizeof(char *), GFP_KERNEL);
Amit Shah2658a79a2010-01-18 19:15:11 +05301750 portdev->in_vqs = kmalloc(nr_ports * sizeof(struct virtqueue *),
1751 GFP_KERNEL);
Amit Shah2658a79a2010-01-18 19:15:11 +05301752 portdev->out_vqs = kmalloc(nr_ports * sizeof(struct virtqueue *),
1753 GFP_KERNEL);
Jiri Slaby22e132f2010-11-06 10:06:50 +01001754 if (!vqs || !io_callbacks || !io_names || !portdev->in_vqs ||
Amit Shah286f9a22011-09-14 13:06:39 +05301755 !portdev->out_vqs) {
Amit Shah2658a79a2010-01-18 19:15:11 +05301756 err = -ENOMEM;
Jiri Slaby22e132f2010-11-06 10:06:50 +01001757 goto free;
Amit Shah2658a79a2010-01-18 19:15:11 +05301758 }
1759
Amit Shah17634ba2009-12-21 21:03:25 +05301760 /*
1761 * For backward compat (newer host but older guest), the host
1762 * spawns a console port first and also inits the vqs for port
1763 * 0 before others.
1764 */
1765 j = 0;
1766 io_callbacks[j] = in_intr;
Amit Shah2770c5e2011-01-31 13:06:36 +05301767 io_callbacks[j + 1] = out_intr;
Amit Shah17634ba2009-12-21 21:03:25 +05301768 io_names[j] = "input";
1769 io_names[j + 1] = "output";
1770 j += 2;
Amit Shah2658a79a2010-01-18 19:15:11 +05301771
Amit Shah17634ba2009-12-21 21:03:25 +05301772 if (use_multiport(portdev)) {
1773 io_callbacks[j] = control_intr;
1774 io_callbacks[j + 1] = NULL;
1775 io_names[j] = "control-i";
1776 io_names[j + 1] = "control-o";
1777
1778 for (i = 1; i < nr_ports; i++) {
1779 j += 2;
1780 io_callbacks[j] = in_intr;
Amit Shah2770c5e2011-01-31 13:06:36 +05301781 io_callbacks[j + 1] = out_intr;
Amit Shah17634ba2009-12-21 21:03:25 +05301782 io_names[j] = "input";
1783 io_names[j + 1] = "output";
1784 }
1785 }
Amit Shah2658a79a2010-01-18 19:15:11 +05301786 /* Find the queues. */
1787 err = portdev->vdev->config->find_vqs(portdev->vdev, nr_queues, vqs,
1788 io_callbacks,
1789 (const char **)io_names);
1790 if (err)
Jiri Slaby22e132f2010-11-06 10:06:50 +01001791 goto free;
Amit Shah2658a79a2010-01-18 19:15:11 +05301792
Amit Shah17634ba2009-12-21 21:03:25 +05301793 j = 0;
Amit Shah2658a79a2010-01-18 19:15:11 +05301794 portdev->in_vqs[0] = vqs[0];
1795 portdev->out_vqs[0] = vqs[1];
Amit Shah17634ba2009-12-21 21:03:25 +05301796 j += 2;
1797 if (use_multiport(portdev)) {
1798 portdev->c_ivq = vqs[j];
1799 portdev->c_ovq = vqs[j + 1];
Amit Shah2658a79a2010-01-18 19:15:11 +05301800
Amit Shah17634ba2009-12-21 21:03:25 +05301801 for (i = 1; i < nr_ports; i++) {
1802 j += 2;
1803 portdev->in_vqs[i] = vqs[j];
1804 portdev->out_vqs[i] = vqs[j + 1];
1805 }
1806 }
Amit Shah2658a79a2010-01-18 19:15:11 +05301807 kfree(io_names);
Jiri Slaby22e132f2010-11-06 10:06:50 +01001808 kfree(io_callbacks);
Amit Shah2658a79a2010-01-18 19:15:11 +05301809 kfree(vqs);
1810
1811 return 0;
1812
Jiri Slaby22e132f2010-11-06 10:06:50 +01001813free:
Amit Shah2658a79a2010-01-18 19:15:11 +05301814 kfree(portdev->out_vqs);
Amit Shah2658a79a2010-01-18 19:15:11 +05301815 kfree(portdev->in_vqs);
Jiri Slaby22e132f2010-11-06 10:06:50 +01001816 kfree(io_names);
1817 kfree(io_callbacks);
Amit Shah2658a79a2010-01-18 19:15:11 +05301818 kfree(vqs);
Jiri Slaby22e132f2010-11-06 10:06:50 +01001819
Amit Shah2658a79a2010-01-18 19:15:11 +05301820 return err;
1821}
1822
Amit Shahfb08bd22009-12-21 21:36:04 +05301823static const struct file_operations portdev_fops = {
1824 .owner = THIS_MODULE,
1825};
1826
Amit Shaha0e2dbf2011-12-22 16:58:27 +05301827static void remove_vqs(struct ports_device *portdev)
1828{
1829 portdev->vdev->config->del_vqs(portdev->vdev);
1830 kfree(portdev->in_vqs);
1831 kfree(portdev->out_vqs);
1832}
1833
1834static void remove_controlq_data(struct ports_device *portdev)
1835{
1836 struct port_buffer *buf;
1837 unsigned int len;
1838
1839 if (!use_multiport(portdev))
1840 return;
1841
1842 while ((buf = virtqueue_get_buf(portdev->c_ivq, &len)))
1843 free_buf(buf);
1844
1845 while ((buf = virtqueue_detach_unused_buf(portdev->c_ivq)))
1846 free_buf(buf);
1847}
1848
Amit Shah1c85bf32010-01-18 19:15:07 +05301849/*
1850 * Once we're further in boot, we get probed like any other virtio
1851 * device.
Amit Shah17634ba2009-12-21 21:03:25 +05301852 *
1853 * If the host also supports multiple console ports, we check the
1854 * config space to see how many ports the host has spawned. We
1855 * initialize each port found.
Amit Shah1c85bf32010-01-18 19:15:07 +05301856 */
1857static int __devinit virtcons_probe(struct virtio_device *vdev)
1858{
Amit Shah1c85bf32010-01-18 19:15:07 +05301859 struct ports_device *portdev;
1860 int err;
Amit Shah17634ba2009-12-21 21:03:25 +05301861 bool multiport;
Christian Borntraeger5e384832011-09-22 23:44:23 +05301862 bool early = early_put_chars != NULL;
1863
1864 /* Ensure to read early_put_chars now */
1865 barrier();
Amit Shah1c85bf32010-01-18 19:15:07 +05301866
1867 portdev = kmalloc(sizeof(*portdev), GFP_KERNEL);
1868 if (!portdev) {
1869 err = -ENOMEM;
1870 goto fail;
1871 }
1872
1873 /* Attach this portdev to this virtio_device, and vice-versa. */
1874 portdev->vdev = vdev;
1875 vdev->priv = portdev;
1876
Amit Shahfb08bd22009-12-21 21:36:04 +05301877 spin_lock_irq(&pdrvdata_lock);
1878 portdev->drv_index = pdrvdata.index++;
1879 spin_unlock_irq(&pdrvdata_lock);
1880
1881 portdev->chr_major = register_chrdev(0, "virtio-portsdev",
1882 &portdev_fops);
1883 if (portdev->chr_major < 0) {
1884 dev_err(&vdev->dev,
1885 "Error %d registering chrdev for device %u\n",
1886 portdev->chr_major, portdev->drv_index);
1887 err = portdev->chr_major;
1888 goto free;
1889 }
1890
Amit Shah17634ba2009-12-21 21:03:25 +05301891 multiport = false;
Amit Shah17634ba2009-12-21 21:03:25 +05301892 portdev->config.max_nr_ports = 1;
Sasha Levin51c6d612011-08-14 17:52:31 +03001893 if (virtio_config_val(vdev, VIRTIO_CONSOLE_F_MULTIPORT,
1894 offsetof(struct virtio_console_config,
1895 max_nr_ports),
1896 &portdev->config.max_nr_ports) == 0)
Amit Shah17634ba2009-12-21 21:03:25 +05301897 multiport = true;
Amit Shah17634ba2009-12-21 21:03:25 +05301898
Amit Shah2658a79a2010-01-18 19:15:11 +05301899 err = init_vqs(portdev);
1900 if (err < 0) {
1901 dev_err(&vdev->dev, "Error %d initializing vqs\n", err);
Amit Shahfb08bd22009-12-21 21:36:04 +05301902 goto free_chrdev;
Amit Shah2658a79a2010-01-18 19:15:11 +05301903 }
Amit Shah1c85bf32010-01-18 19:15:07 +05301904
Amit Shah17634ba2009-12-21 21:03:25 +05301905 spin_lock_init(&portdev->ports_lock);
1906 INIT_LIST_HEAD(&portdev->ports);
1907
1908 if (multiport) {
Amit Shah335a64a5c2010-02-24 10:37:44 +05301909 unsigned int nr_added_bufs;
1910
Amit Shah17634ba2009-12-21 21:03:25 +05301911 spin_lock_init(&portdev->cvq_lock);
1912 INIT_WORK(&portdev->control_work, &control_work_handler);
1913
Amit Shah335a64a5c2010-02-24 10:37:44 +05301914 nr_added_bufs = fill_queue(portdev->c_ivq, &portdev->cvq_lock);
1915 if (!nr_added_bufs) {
Amit Shah22a29ea2010-02-12 10:32:17 +05301916 dev_err(&vdev->dev,
1917 "Error allocating buffers for control queue\n");
1918 err = -ENOMEM;
1919 goto free_vqs;
1920 }
Amit Shah1d051602010-05-19 22:15:49 -06001921 } else {
1922 /*
1923 * For backward compatibility: Create a console port
1924 * if we're running on older host.
1925 */
1926 add_port(portdev, 0);
Amit Shah17634ba2009-12-21 21:03:25 +05301927 }
1928
Amit Shah6bdf2af2010-09-02 18:11:49 +05301929 spin_lock_irq(&pdrvdata_lock);
1930 list_add_tail(&portdev->list, &pdrvdata.portdevs);
1931 spin_unlock_irq(&pdrvdata_lock);
1932
Amit Shahf909f852010-05-19 22:15:48 -06001933 __send_control_msg(portdev, VIRTIO_CONSOLE_BAD_ID,
1934 VIRTIO_CONSOLE_DEVICE_READY, 1);
Christian Borntraeger5e384832011-09-22 23:44:23 +05301935
1936 /*
1937 * If there was an early virtio console, assume that there are no
1938 * other consoles. We need to wait until the hvc_alloc matches the
1939 * hvc_instantiate, otherwise tty_open will complain, resulting in
1940 * a "Warning: unable to open an initial console" boot failure.
1941 * Without multiport this is done in add_port above. With multiport
1942 * this might take some host<->guest communication - thus we have to
1943 * wait.
1944 */
1945 if (multiport && early)
1946 wait_for_completion(&early_console_added);
1947
Rusty Russell31610432007-10-22 11:03:39 +10001948 return 0;
1949
Amit Shah22a29ea2010-02-12 10:32:17 +05301950free_vqs:
Julia Lawall0643e4c2010-05-15 11:45:53 +02001951 /* The host might want to notify mgmt sw about device add failure */
1952 __send_control_msg(portdev, VIRTIO_CONSOLE_BAD_ID,
1953 VIRTIO_CONSOLE_DEVICE_READY, 0);
Amit Shaha0e2dbf2011-12-22 16:58:27 +05301954 remove_vqs(portdev);
Amit Shahfb08bd22009-12-21 21:36:04 +05301955free_chrdev:
1956 unregister_chrdev(portdev->chr_major, "virtio-portsdev");
Rusty Russell31610432007-10-22 11:03:39 +10001957free:
Amit Shah1c85bf32010-01-18 19:15:07 +05301958 kfree(portdev);
Rusty Russell31610432007-10-22 11:03:39 +10001959fail:
1960 return err;
1961}
1962
Amit Shah71778762010-02-12 10:32:16 +05301963static void virtcons_remove(struct virtio_device *vdev)
1964{
1965 struct ports_device *portdev;
1966 struct port *port, *port2;
Amit Shah71778762010-02-12 10:32:16 +05301967
1968 portdev = vdev->priv;
1969
Amit Shah6bdf2af2010-09-02 18:11:49 +05301970 spin_lock_irq(&pdrvdata_lock);
1971 list_del(&portdev->list);
1972 spin_unlock_irq(&pdrvdata_lock);
1973
Amit Shah02238952010-09-02 18:11:40 +05301974 /* Disable interrupts for vqs */
1975 vdev->config->reset(vdev);
1976 /* Finish up work that's lined up */
Amit Shah71778762010-02-12 10:32:16 +05301977 cancel_work_sync(&portdev->control_work);
Amit Shah71778762010-02-12 10:32:16 +05301978
1979 list_for_each_entry_safe(port, port2, &portdev->ports, list)
Amit Shahb353a6b2010-09-02 18:38:29 +05301980 unplug_port(port);
Amit Shah71778762010-02-12 10:32:16 +05301981
1982 unregister_chrdev(portdev->chr_major, "virtio-portsdev");
1983
Amit Shahe0620132010-09-02 18:38:30 +05301984 /*
1985 * When yanking out a device, we immediately lose the
1986 * (device-side) queues. So there's no point in keeping the
1987 * guest side around till we drop our final reference. This
1988 * also means that any ports which are in an open state will
1989 * have to just stop using the port, as the vqs are going
1990 * away.
1991 */
Amit Shaha0e2dbf2011-12-22 16:58:27 +05301992 remove_controlq_data(portdev);
1993 remove_vqs(portdev);
Amit Shah71778762010-02-12 10:32:16 +05301994 kfree(portdev);
1995}
1996
Rusty Russell31610432007-10-22 11:03:39 +10001997static struct virtio_device_id id_table[] = {
1998 { VIRTIO_ID_CONSOLE, VIRTIO_DEV_ANY_ID },
1999 { 0 },
2000};
2001
Christian Borntraegerc2983452008-11-25 13:36:26 +01002002static unsigned int features[] = {
2003 VIRTIO_CONSOLE_F_SIZE,
Amit Shahb99fa812010-05-19 22:15:46 -06002004 VIRTIO_CONSOLE_F_MULTIPORT,
Christian Borntraegerc2983452008-11-25 13:36:26 +01002005};
2006
Amit Shah2b8f41d2011-12-22 16:58:28 +05302007#ifdef CONFIG_PM
2008static int virtcons_freeze(struct virtio_device *vdev)
2009{
2010 struct ports_device *portdev;
2011 struct port *port;
2012
2013 portdev = vdev->priv;
2014
2015 vdev->config->reset(vdev);
2016
Amit Shahc743d092012-01-06 16:19:08 +05302017 virtqueue_disable_cb(portdev->c_ivq);
Amit Shah2b8f41d2011-12-22 16:58:28 +05302018 cancel_work_sync(&portdev->control_work);
Amit Shahc743d092012-01-06 16:19:08 +05302019 /*
2020 * Once more: if control_work_handler() was running, it would
2021 * enable the cb as the last step.
2022 */
2023 virtqueue_disable_cb(portdev->c_ivq);
Amit Shah2b8f41d2011-12-22 16:58:28 +05302024 remove_controlq_data(portdev);
2025
2026 list_for_each_entry(port, &portdev->ports, list) {
Amit Shahc743d092012-01-06 16:19:08 +05302027 virtqueue_disable_cb(port->in_vq);
2028 virtqueue_disable_cb(port->out_vq);
Amit Shah2b8f41d2011-12-22 16:58:28 +05302029 /*
2030 * We'll ask the host later if the new invocation has
2031 * the port opened or closed.
2032 */
2033 port->host_connected = false;
2034 remove_port_data(port);
2035 }
2036 remove_vqs(portdev);
2037
2038 return 0;
2039}
2040
2041static int virtcons_restore(struct virtio_device *vdev)
2042{
2043 struct ports_device *portdev;
2044 struct port *port;
2045 int ret;
2046
2047 portdev = vdev->priv;
2048
2049 ret = init_vqs(portdev);
2050 if (ret)
2051 return ret;
2052
2053 if (use_multiport(portdev))
2054 fill_queue(portdev->c_ivq, &portdev->cvq_lock);
2055
2056 list_for_each_entry(port, &portdev->ports, list) {
2057 port->in_vq = portdev->in_vqs[port->id];
2058 port->out_vq = portdev->out_vqs[port->id];
2059
2060 fill_queue(port->in_vq, &port->inbuf_lock);
2061
2062 /* Get port open/close status on the host */
2063 send_control_msg(port, VIRTIO_CONSOLE_PORT_READY, 1);
Amit Shahfa8b66c2012-04-25 14:40:39 +05302064
2065 /*
2066 * If a port was open at the time of suspending, we
2067 * have to let the host know that it's still open.
2068 */
2069 if (port->guest_connected)
2070 send_control_msg(port, VIRTIO_CONSOLE_PORT_OPEN, 1);
Amit Shah2b8f41d2011-12-22 16:58:28 +05302071 }
2072 return 0;
2073}
2074#endif
2075
Rusty Russell31610432007-10-22 11:03:39 +10002076static struct virtio_driver virtio_console = {
Christian Borntraegerc2983452008-11-25 13:36:26 +01002077 .feature_table = features,
2078 .feature_table_size = ARRAY_SIZE(features),
Rusty Russell31610432007-10-22 11:03:39 +10002079 .driver.name = KBUILD_MODNAME,
2080 .driver.owner = THIS_MODULE,
2081 .id_table = id_table,
2082 .probe = virtcons_probe,
Amit Shah71778762010-02-12 10:32:16 +05302083 .remove = virtcons_remove,
Amit Shah7f5d8102009-12-21 22:22:08 +05302084 .config_changed = config_intr,
Amit Shah2b8f41d2011-12-22 16:58:28 +05302085#ifdef CONFIG_PM
2086 .freeze = virtcons_freeze,
2087 .restore = virtcons_restore,
2088#endif
Rusty Russell31610432007-10-22 11:03:39 +10002089};
2090
2091static int __init init(void)
2092{
Amit Shahfb08bd22009-12-21 21:36:04 +05302093 int err;
2094
2095 pdrvdata.class = class_create(THIS_MODULE, "virtio-ports");
2096 if (IS_ERR(pdrvdata.class)) {
2097 err = PTR_ERR(pdrvdata.class);
2098 pr_err("Error %d creating virtio-ports class\n", err);
2099 return err;
2100 }
Amit Shahd99393e2009-12-21 22:36:21 +05302101
2102 pdrvdata.debugfs_dir = debugfs_create_dir("virtio-ports", NULL);
2103 if (!pdrvdata.debugfs_dir) {
2104 pr_warning("Error %ld creating debugfs dir for virtio-ports\n",
2105 PTR_ERR(pdrvdata.debugfs_dir));
2106 }
Amit Shah38edf582010-01-18 19:15:05 +05302107 INIT_LIST_HEAD(&pdrvdata.consoles);
Amit Shah6bdf2af2010-09-02 18:11:49 +05302108 INIT_LIST_HEAD(&pdrvdata.portdevs);
Amit Shah38edf582010-01-18 19:15:05 +05302109
Alexey Khoroshilov33e1afc2012-09-01 23:49:37 +04002110 err = register_virtio_driver(&virtio_console);
2111 if (err < 0) {
2112 pr_err("Error %d registering virtio driver\n", err);
2113 goto free;
2114 }
2115 return 0;
2116free:
2117 if (pdrvdata.debugfs_dir)
2118 debugfs_remove_recursive(pdrvdata.debugfs_dir);
2119 class_destroy(pdrvdata.class);
2120 return err;
Rusty Russell31610432007-10-22 11:03:39 +10002121}
Amit Shah71778762010-02-12 10:32:16 +05302122
2123static void __exit fini(void)
2124{
2125 unregister_virtio_driver(&virtio_console);
2126
2127 class_destroy(pdrvdata.class);
2128 if (pdrvdata.debugfs_dir)
2129 debugfs_remove_recursive(pdrvdata.debugfs_dir);
2130}
Rusty Russell31610432007-10-22 11:03:39 +10002131module_init(init);
Amit Shah71778762010-02-12 10:32:16 +05302132module_exit(fini);
Rusty Russell31610432007-10-22 11:03:39 +10002133
2134MODULE_DEVICE_TABLE(virtio, id_table);
2135MODULE_DESCRIPTION("Virtio console driver");
2136MODULE_LICENSE("GPL");