blob: c84486b3e43d87a9f3d664861b5ed47e968eb157 [file] [log] [blame]
Rusty Russella23ea922010-01-18 19:14:55 +05301/*
2 * Copyright (C) 2006, 2007, 2009 Rusty Russell, IBM Corporation
Amit Shah17634ba2009-12-21 21:03:25 +05303 * Copyright (C) 2009, 2010 Red Hat, Inc.
Rusty Russell31610432007-10-22 11:03:39 +10004 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 */
Amit Shahfb08bd22009-12-21 21:36:04 +053019#include <linux/cdev.h>
Amit Shahd99393e2009-12-21 22:36:21 +053020#include <linux/debugfs.h>
Amit Shahfb08bd22009-12-21 21:36:04 +053021#include <linux/device.h>
Rusty Russell31610432007-10-22 11:03:39 +100022#include <linux/err.h>
Amit Shah2030fa42009-12-21 21:49:30 +053023#include <linux/fs.h>
Rusty Russell31610432007-10-22 11:03:39 +100024#include <linux/init.h>
Amit Shah38edf582010-01-18 19:15:05 +053025#include <linux/list.h>
Amit Shah2030fa42009-12-21 21:49:30 +053026#include <linux/poll.h>
27#include <linux/sched.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090028#include <linux/slab.h>
Amit Shah38edf582010-01-18 19:15:05 +053029#include <linux/spinlock.h>
Rusty Russell31610432007-10-22 11:03:39 +100030#include <linux/virtio.h>
31#include <linux/virtio_console.h>
Amit Shah2030fa42009-12-21 21:49:30 +053032#include <linux/wait.h>
Amit Shah17634ba2009-12-21 21:03:25 +053033#include <linux/workqueue.h>
Rusty Russell31610432007-10-22 11:03:39 +100034#include "hvc_console.h"
35
Amit Shah38edf582010-01-18 19:15:05 +053036/*
37 * This is a global struct for storing common data for all the devices
38 * this driver handles.
39 *
40 * Mainly, it has a linked list for all the consoles in one place so
41 * that callbacks from hvc for get_chars(), put_chars() work properly
42 * across multiple devices and multiple ports per device.
43 */
44struct ports_driver_data {
Amit Shahfb08bd22009-12-21 21:36:04 +053045 /* Used for registering chardevs */
46 struct class *class;
47
Amit Shahd99393e2009-12-21 22:36:21 +053048 /* Used for exporting per-port information to debugfs */
49 struct dentry *debugfs_dir;
50
Amit Shah6bdf2af2010-09-02 18:11:49 +053051 /* List of all the devices we're handling */
52 struct list_head portdevs;
53
Amit Shahfb08bd22009-12-21 21:36:04 +053054 /* Number of devices this driver is handling */
55 unsigned int index;
56
Rusty Russelld8a02bd2010-01-18 19:15:06 +053057 /*
58 * This is used to keep track of the number of hvc consoles
59 * spawned by this driver. This number is given as the first
60 * argument to hvc_alloc(). To correctly map an initial
61 * console spawned via hvc_instantiate to the console being
62 * hooked up via hvc_alloc, we need to pass the same vtermno.
63 *
64 * We also just assume the first console being initialised was
65 * the first one that got used as the initial console.
66 */
67 unsigned int next_vtermno;
68
Amit Shah38edf582010-01-18 19:15:05 +053069 /* All the console devices handled by this driver */
70 struct list_head consoles;
71};
72static struct ports_driver_data pdrvdata;
73
74DEFINE_SPINLOCK(pdrvdata_lock);
75
Amit Shah4f23c572010-01-18 19:15:09 +053076/* This struct holds information that's relevant only for console ports */
77struct console {
78 /* We'll place all consoles in a list in the pdrvdata struct */
79 struct list_head list;
80
81 /* The hvc device associated with this console port */
82 struct hvc_struct *hvc;
83
Amit Shah97788292010-05-06 02:05:08 +053084 /* The size of the console */
85 struct winsize ws;
86
Amit Shah4f23c572010-01-18 19:15:09 +053087 /*
88 * This number identifies the number that we used to register
89 * with hvc in hvc_instantiate() and hvc_alloc(); this is the
90 * number passed on by the hvc callbacks to us to
91 * differentiate between the other console ports handled by
92 * this driver
93 */
94 u32 vtermno;
95};
96
Amit Shahfdb9a052010-01-18 19:15:01 +053097struct port_buffer {
98 char *buf;
99
100 /* size of the buffer in *buf above */
101 size_t size;
102
103 /* used length of the buffer */
104 size_t len;
105 /* offset in the buf from which to consume data */
106 size_t offset;
107};
108
Amit Shah17634ba2009-12-21 21:03:25 +0530109/*
110 * This is a per-device struct that stores data common to all the
111 * ports for that device (vdev->priv).
112 */
113struct ports_device {
Amit Shah6bdf2af2010-09-02 18:11:49 +0530114 /* Next portdev in the list, head is in the pdrvdata struct */
115 struct list_head list;
116
Amit Shah17634ba2009-12-21 21:03:25 +0530117 /*
118 * Workqueue handlers where we process deferred work after
119 * notification
120 */
121 struct work_struct control_work;
122
123 struct list_head ports;
124
125 /* To protect the list of ports */
126 spinlock_t ports_lock;
127
128 /* To protect the vq operations for the control channel */
129 spinlock_t cvq_lock;
130
131 /* The current config space is stored here */
Amit Shahb99fa812010-05-19 22:15:46 -0600132 struct virtio_console_config config;
Amit Shah17634ba2009-12-21 21:03:25 +0530133
134 /* The virtio device we're associated with */
135 struct virtio_device *vdev;
136
137 /*
138 * A couple of virtqueues for the control channel: one for
139 * guest->host transfers, one for host->guest transfers
140 */
141 struct virtqueue *c_ivq, *c_ovq;
142
143 /* Array of per-port IO virtqueues */
144 struct virtqueue **in_vqs, **out_vqs;
Amit Shahfb08bd22009-12-21 21:36:04 +0530145
146 /* Used for numbering devices for sysfs and debugfs */
147 unsigned int drv_index;
148
149 /* Major number for this device. Ports will be created as minors. */
150 int chr_major;
Amit Shah17634ba2009-12-21 21:03:25 +0530151};
152
Amit Shah1c85bf32010-01-18 19:15:07 +0530153/* This struct holds the per-port data */
Rusty Russell21206ed2010-01-18 19:15:00 +0530154struct port {
Amit Shah17634ba2009-12-21 21:03:25 +0530155 /* Next port in the list, head is in the ports_device */
156 struct list_head list;
157
Amit Shah1c85bf32010-01-18 19:15:07 +0530158 /* Pointer to the parent virtio_console device */
159 struct ports_device *portdev;
Amit Shahfdb9a052010-01-18 19:15:01 +0530160
161 /* The current buffer from which data has to be fed to readers */
162 struct port_buffer *inbuf;
Rusty Russell31610432007-10-22 11:03:39 +1000163
Amit Shah203baab2010-01-18 19:15:12 +0530164 /*
165 * To protect the operations on the in_vq associated with this
166 * port. Has to be a spinlock because it can be called from
167 * interrupt context (get_char()).
168 */
169 spinlock_t inbuf_lock;
170
Amit Shahcdfadfc2010-05-19 22:15:50 -0600171 /* Protect the operations on the out_vq. */
172 spinlock_t outvq_lock;
173
Amit Shah1c85bf32010-01-18 19:15:07 +0530174 /* The IO vqs for this port */
175 struct virtqueue *in_vq, *out_vq;
176
Amit Shahd99393e2009-12-21 22:36:21 +0530177 /* File in the debugfs directory that exposes this port's information */
178 struct dentry *debugfs_file;
179
Amit Shah4f23c572010-01-18 19:15:09 +0530180 /*
181 * The entries in this struct will be valid if this port is
182 * hooked up to an hvc console
183 */
184 struct console cons;
Amit Shah17634ba2009-12-21 21:03:25 +0530185
Amit Shahfb08bd22009-12-21 21:36:04 +0530186 /* Each port associates with a separate char device */
Amit Shahd22a6982010-09-02 18:20:59 +0530187 struct cdev *cdev;
Amit Shahfb08bd22009-12-21 21:36:04 +0530188 struct device *dev;
189
Amit Shahb353a6b2010-09-02 18:38:29 +0530190 /* Reference-counting to handle port hot-unplugs and file operations */
191 struct kref kref;
192
Amit Shah2030fa42009-12-21 21:49:30 +0530193 /* A waitqueue for poll() or blocking read operations */
194 wait_queue_head_t waitqueue;
195
Amit Shah431edb82009-12-21 21:57:40 +0530196 /* The 'name' of the port that we expose via sysfs properties */
197 char *name;
198
Amit Shah17634ba2009-12-21 21:03:25 +0530199 /* The 'id' to identify the port with the Host */
200 u32 id;
Amit Shah2030fa42009-12-21 21:49:30 +0530201
Amit Shahcdfadfc2010-05-19 22:15:50 -0600202 bool outvq_full;
203
Amit Shah2030fa42009-12-21 21:49:30 +0530204 /* Is the host device open */
205 bool host_connected;
Amit Shah3c7969c2009-11-26 11:25:38 +0530206
207 /* We should allow only one process to open a port */
208 bool guest_connected;
Rusty Russell21206ed2010-01-18 19:15:00 +0530209};
Rusty Russell31610432007-10-22 11:03:39 +1000210
Rusty Russell971f3392010-01-18 19:14:56 +0530211/* This is the very early arch-specified put chars function. */
212static int (*early_put_chars)(u32, const char *, int);
213
Amit Shah38edf582010-01-18 19:15:05 +0530214static struct port *find_port_by_vtermno(u32 vtermno)
215{
216 struct port *port;
Amit Shah4f23c572010-01-18 19:15:09 +0530217 struct console *cons;
Amit Shah38edf582010-01-18 19:15:05 +0530218 unsigned long flags;
219
220 spin_lock_irqsave(&pdrvdata_lock, flags);
Amit Shah4f23c572010-01-18 19:15:09 +0530221 list_for_each_entry(cons, &pdrvdata.consoles, list) {
222 if (cons->vtermno == vtermno) {
223 port = container_of(cons, struct port, cons);
Amit Shah38edf582010-01-18 19:15:05 +0530224 goto out;
Amit Shah4f23c572010-01-18 19:15:09 +0530225 }
Amit Shah38edf582010-01-18 19:15:05 +0530226 }
227 port = NULL;
228out:
229 spin_unlock_irqrestore(&pdrvdata_lock, flags);
230 return port;
231}
232
Amit Shah04950cd2010-09-02 18:20:58 +0530233static struct port *find_port_by_devt_in_portdev(struct ports_device *portdev,
234 dev_t dev)
235{
236 struct port *port;
237 unsigned long flags;
238
239 spin_lock_irqsave(&portdev->ports_lock, flags);
240 list_for_each_entry(port, &portdev->ports, list)
Amit Shahd22a6982010-09-02 18:20:59 +0530241 if (port->cdev->dev == dev)
Amit Shah04950cd2010-09-02 18:20:58 +0530242 goto out;
243 port = NULL;
244out:
245 spin_unlock_irqrestore(&portdev->ports_lock, flags);
246
247 return port;
248}
249
250static struct port *find_port_by_devt(dev_t dev)
251{
252 struct ports_device *portdev;
253 struct port *port;
254 unsigned long flags;
255
256 spin_lock_irqsave(&pdrvdata_lock, flags);
257 list_for_each_entry(portdev, &pdrvdata.portdevs, list) {
258 port = find_port_by_devt_in_portdev(portdev, dev);
259 if (port)
260 goto out;
261 }
262 port = NULL;
263out:
264 spin_unlock_irqrestore(&pdrvdata_lock, flags);
265 return port;
266}
267
Amit Shah17634ba2009-12-21 21:03:25 +0530268static struct port *find_port_by_id(struct ports_device *portdev, u32 id)
269{
270 struct port *port;
271 unsigned long flags;
272
273 spin_lock_irqsave(&portdev->ports_lock, flags);
274 list_for_each_entry(port, &portdev->ports, list)
275 if (port->id == id)
276 goto out;
277 port = NULL;
278out:
279 spin_unlock_irqrestore(&portdev->ports_lock, flags);
280
281 return port;
282}
283
Amit Shah203baab2010-01-18 19:15:12 +0530284static struct port *find_port_by_vq(struct ports_device *portdev,
285 struct virtqueue *vq)
286{
287 struct port *port;
Amit Shah203baab2010-01-18 19:15:12 +0530288 unsigned long flags;
289
Amit Shah17634ba2009-12-21 21:03:25 +0530290 spin_lock_irqsave(&portdev->ports_lock, flags);
291 list_for_each_entry(port, &portdev->ports, list)
Amit Shah203baab2010-01-18 19:15:12 +0530292 if (port->in_vq == vq || port->out_vq == vq)
293 goto out;
Amit Shah203baab2010-01-18 19:15:12 +0530294 port = NULL;
295out:
Amit Shah17634ba2009-12-21 21:03:25 +0530296 spin_unlock_irqrestore(&portdev->ports_lock, flags);
Amit Shah203baab2010-01-18 19:15:12 +0530297 return port;
298}
299
Amit Shah17634ba2009-12-21 21:03:25 +0530300static bool is_console_port(struct port *port)
301{
302 if (port->cons.hvc)
303 return true;
304 return false;
305}
306
307static inline bool use_multiport(struct ports_device *portdev)
308{
309 /*
310 * This condition can be true when put_chars is called from
311 * early_init
312 */
313 if (!portdev->vdev)
314 return 0;
315 return portdev->vdev->features[0] & (1 << VIRTIO_CONSOLE_F_MULTIPORT);
316}
317
Amit Shahfdb9a052010-01-18 19:15:01 +0530318static void free_buf(struct port_buffer *buf)
319{
320 kfree(buf->buf);
321 kfree(buf);
322}
323
324static struct port_buffer *alloc_buf(size_t buf_size)
325{
326 struct port_buffer *buf;
327
328 buf = kmalloc(sizeof(*buf), GFP_KERNEL);
329 if (!buf)
330 goto fail;
331 buf->buf = kzalloc(buf_size, GFP_KERNEL);
332 if (!buf->buf)
333 goto free_buf;
334 buf->len = 0;
335 buf->offset = 0;
336 buf->size = buf_size;
337 return buf;
338
339free_buf:
340 kfree(buf);
341fail:
342 return NULL;
343}
344
Amit Shaha3cde442010-01-18 19:15:03 +0530345/* Callers should take appropriate locks */
346static void *get_inbuf(struct port *port)
347{
348 struct port_buffer *buf;
349 struct virtqueue *vq;
350 unsigned int len;
351
352 vq = port->in_vq;
Michael S. Tsirkin505b0452010-04-12 16:18:32 +0300353 buf = virtqueue_get_buf(vq, &len);
Amit Shaha3cde442010-01-18 19:15:03 +0530354 if (buf) {
355 buf->len = len;
356 buf->offset = 0;
357 }
358 return buf;
359}
360
Rusty Russella23ea922010-01-18 19:14:55 +0530361/*
Amit Shahe27b5192010-01-18 19:15:02 +0530362 * Create a scatter-gather list representing our input buffer and put
363 * it in the queue.
364 *
365 * Callers should take appropriate locks.
366 */
Amit Shah203baab2010-01-18 19:15:12 +0530367static int add_inbuf(struct virtqueue *vq, struct port_buffer *buf)
Amit Shahe27b5192010-01-18 19:15:02 +0530368{
369 struct scatterlist sg[1];
Amit Shah203baab2010-01-18 19:15:12 +0530370 int ret;
Amit Shah1c85bf32010-01-18 19:15:07 +0530371
Amit Shahe27b5192010-01-18 19:15:02 +0530372 sg_init_one(sg, buf->buf, buf->size);
373
Michael S. Tsirkin505b0452010-04-12 16:18:32 +0300374 ret = virtqueue_add_buf(vq, sg, 0, 1, buf);
375 virtqueue_kick(vq);
Amit Shah203baab2010-01-18 19:15:12 +0530376 return ret;
377}
378
Amit Shah88f251a2009-12-21 22:15:30 +0530379/* Discard any unread data this port has. Callers lockers. */
380static void discard_port_data(struct port *port)
381{
382 struct port_buffer *buf;
383 struct virtqueue *vq;
384 unsigned int len;
Amit Shahd6933562010-02-12 10:32:18 +0530385 int ret;
Amit Shah88f251a2009-12-21 22:15:30 +0530386
387 vq = port->in_vq;
388 if (port->inbuf)
389 buf = port->inbuf;
390 else
Michael S. Tsirkin505b0452010-04-12 16:18:32 +0300391 buf = virtqueue_get_buf(vq, &len);
Amit Shah88f251a2009-12-21 22:15:30 +0530392
Amit Shahd6933562010-02-12 10:32:18 +0530393 ret = 0;
394 while (buf) {
395 if (add_inbuf(vq, buf) < 0) {
396 ret++;
397 free_buf(buf);
398 }
Michael S. Tsirkin505b0452010-04-12 16:18:32 +0300399 buf = virtqueue_get_buf(vq, &len);
Amit Shah88f251a2009-12-21 22:15:30 +0530400 }
Amit Shah88f251a2009-12-21 22:15:30 +0530401 port->inbuf = NULL;
Amit Shahd6933562010-02-12 10:32:18 +0530402 if (ret)
403 dev_warn(port->dev, "Errors adding %d buffers back to vq\n",
404 ret);
Amit Shah88f251a2009-12-21 22:15:30 +0530405}
406
Amit Shah203baab2010-01-18 19:15:12 +0530407static bool port_has_data(struct port *port)
408{
409 unsigned long flags;
410 bool ret;
411
Amit Shah203baab2010-01-18 19:15:12 +0530412 spin_lock_irqsave(&port->inbuf_lock, flags);
Amit Shahd6933562010-02-12 10:32:18 +0530413 if (port->inbuf) {
Amit Shah203baab2010-01-18 19:15:12 +0530414 ret = true;
Amit Shahd6933562010-02-12 10:32:18 +0530415 goto out;
416 }
417 port->inbuf = get_inbuf(port);
418 if (port->inbuf) {
419 ret = true;
420 goto out;
421 }
422 ret = false;
423out:
Amit Shah203baab2010-01-18 19:15:12 +0530424 spin_unlock_irqrestore(&port->inbuf_lock, flags);
Amit Shah203baab2010-01-18 19:15:12 +0530425 return ret;
426}
427
Amit Shah3425e702010-05-19 22:15:46 -0600428static ssize_t __send_control_msg(struct ports_device *portdev, u32 port_id,
429 unsigned int event, unsigned int value)
Amit Shah17634ba2009-12-21 21:03:25 +0530430{
431 struct scatterlist sg[1];
432 struct virtio_console_control cpkt;
433 struct virtqueue *vq;
Amit Shah604b2ad2010-02-24 10:36:51 +0530434 unsigned int len;
Amit Shah17634ba2009-12-21 21:03:25 +0530435
Amit Shah3425e702010-05-19 22:15:46 -0600436 if (!use_multiport(portdev))
Amit Shah17634ba2009-12-21 21:03:25 +0530437 return 0;
438
Amit Shah3425e702010-05-19 22:15:46 -0600439 cpkt.id = port_id;
Amit Shah17634ba2009-12-21 21:03:25 +0530440 cpkt.event = event;
441 cpkt.value = value;
442
Amit Shah3425e702010-05-19 22:15:46 -0600443 vq = portdev->c_ovq;
Amit Shah17634ba2009-12-21 21:03:25 +0530444
445 sg_init_one(sg, &cpkt, sizeof(cpkt));
Michael S. Tsirkin505b0452010-04-12 16:18:32 +0300446 if (virtqueue_add_buf(vq, sg, 1, 0, &cpkt) >= 0) {
447 virtqueue_kick(vq);
448 while (!virtqueue_get_buf(vq, &len))
Amit Shah17634ba2009-12-21 21:03:25 +0530449 cpu_relax();
450 }
451 return 0;
452}
453
Amit Shah3425e702010-05-19 22:15:46 -0600454static ssize_t send_control_msg(struct port *port, unsigned int event,
455 unsigned int value)
456{
Amit Shah84ec06c2010-09-02 18:11:42 +0530457 /* Did the port get unplugged before userspace closed it? */
458 if (port->portdev)
459 return __send_control_msg(port->portdev, port->id, event, value);
460 return 0;
Amit Shah3425e702010-05-19 22:15:46 -0600461}
462
Amit Shahcdfadfc2010-05-19 22:15:50 -0600463/* Callers must take the port->outvq_lock */
464static void reclaim_consumed_buffers(struct port *port)
465{
466 void *buf;
467 unsigned int len;
468
469 while ((buf = virtqueue_get_buf(port->out_vq, &len))) {
470 kfree(buf);
471 port->outvq_full = false;
472 }
473}
474
475static ssize_t send_buf(struct port *port, void *in_buf, size_t in_count,
476 bool nonblock)
Amit Shahf997f00b2009-12-21 17:28:51 +0530477{
478 struct scatterlist sg[1];
479 struct virtqueue *out_vq;
480 ssize_t ret;
Amit Shahcdfadfc2010-05-19 22:15:50 -0600481 unsigned long flags;
Amit Shahf997f00b2009-12-21 17:28:51 +0530482 unsigned int len;
483
484 out_vq = port->out_vq;
485
Amit Shahcdfadfc2010-05-19 22:15:50 -0600486 spin_lock_irqsave(&port->outvq_lock, flags);
487
488 reclaim_consumed_buffers(port);
489
Amit Shahf997f00b2009-12-21 17:28:51 +0530490 sg_init_one(sg, in_buf, in_count);
Michael S. Tsirkin505b0452010-04-12 16:18:32 +0300491 ret = virtqueue_add_buf(out_vq, sg, 1, 0, in_buf);
Amit Shahf997f00b2009-12-21 17:28:51 +0530492
493 /* Tell Host to go! */
Michael S. Tsirkin505b0452010-04-12 16:18:32 +0300494 virtqueue_kick(out_vq);
Amit Shahf997f00b2009-12-21 17:28:51 +0530495
496 if (ret < 0) {
Rusty Russell9ff4cfa2010-04-08 09:46:16 -0600497 in_count = 0;
Amit Shahcdfadfc2010-05-19 22:15:50 -0600498 goto done;
Amit Shahf997f00b2009-12-21 17:28:51 +0530499 }
500
Amit Shahcdfadfc2010-05-19 22:15:50 -0600501 if (ret == 0)
502 port->outvq_full = true;
503
504 if (nonblock)
505 goto done;
506
507 /*
508 * Wait till the host acknowledges it pushed out the data we
Amit Shah531295e2010-10-20 13:45:43 +1030509 * sent. This is done for data from the hvc_console; the tty
510 * operations are performed with spinlocks held so we can't
511 * sleep here. An alternative would be to copy the data to a
512 * buffer and relax the spinning requirement. The downside is
513 * we need to kmalloc a GFP_ATOMIC buffer each time the
514 * console driver writes something out.
Amit Shahcdfadfc2010-05-19 22:15:50 -0600515 */
Michael S. Tsirkin505b0452010-04-12 16:18:32 +0300516 while (!virtqueue_get_buf(out_vq, &len))
Amit Shahf997f00b2009-12-21 17:28:51 +0530517 cpu_relax();
Amit Shahcdfadfc2010-05-19 22:15:50 -0600518done:
519 spin_unlock_irqrestore(&port->outvq_lock, flags);
520 /*
521 * We're expected to return the amount of data we wrote -- all
522 * of it
523 */
Rusty Russell9ff4cfa2010-04-08 09:46:16 -0600524 return in_count;
Amit Shahf997f00b2009-12-21 17:28:51 +0530525}
526
Amit Shah203baab2010-01-18 19:15:12 +0530527/*
528 * Give out the data that's requested from the buffer that we have
529 * queued up.
530 */
Amit Shahb766cee2009-12-21 21:26:45 +0530531static ssize_t fill_readbuf(struct port *port, char *out_buf, size_t out_count,
532 bool to_user)
Amit Shah203baab2010-01-18 19:15:12 +0530533{
534 struct port_buffer *buf;
535 unsigned long flags;
536
537 if (!out_count || !port_has_data(port))
538 return 0;
539
540 buf = port->inbuf;
Amit Shahb766cee2009-12-21 21:26:45 +0530541 out_count = min(out_count, buf->len - buf->offset);
Amit Shah203baab2010-01-18 19:15:12 +0530542
Amit Shahb766cee2009-12-21 21:26:45 +0530543 if (to_user) {
544 ssize_t ret;
Amit Shah203baab2010-01-18 19:15:12 +0530545
Amit Shahb766cee2009-12-21 21:26:45 +0530546 ret = copy_to_user(out_buf, buf->buf + buf->offset, out_count);
547 if (ret)
548 return -EFAULT;
549 } else {
550 memcpy(out_buf, buf->buf + buf->offset, out_count);
551 }
552
Amit Shah203baab2010-01-18 19:15:12 +0530553 buf->offset += out_count;
554
555 if (buf->offset == buf->len) {
556 /*
557 * We're done using all the data in this buffer.
558 * Re-queue so that the Host can send us more data.
559 */
560 spin_lock_irqsave(&port->inbuf_lock, flags);
561 port->inbuf = NULL;
562
563 if (add_inbuf(port->in_vq, buf) < 0)
Amit Shahfb08bd22009-12-21 21:36:04 +0530564 dev_warn(port->dev, "failed add_buf\n");
Amit Shah203baab2010-01-18 19:15:12 +0530565
566 spin_unlock_irqrestore(&port->inbuf_lock, flags);
567 }
Amit Shahb766cee2009-12-21 21:26:45 +0530568 /* Return the number of bytes actually copied */
Amit Shah203baab2010-01-18 19:15:12 +0530569 return out_count;
Amit Shahe27b5192010-01-18 19:15:02 +0530570}
571
Amit Shah2030fa42009-12-21 21:49:30 +0530572/* The condition that must be true for polling to end */
Amit Shah60caacd2010-05-19 22:15:49 -0600573static bool will_read_block(struct port *port)
Amit Shah2030fa42009-12-21 21:49:30 +0530574{
Amit Shah3709ea72010-09-02 18:11:43 +0530575 if (!port->guest_connected) {
576 /* Port got hot-unplugged. Let's exit. */
577 return false;
578 }
Amit Shah60caacd2010-05-19 22:15:49 -0600579 return !port_has_data(port) && port->host_connected;
Amit Shah2030fa42009-12-21 21:49:30 +0530580}
581
Amit Shahcdfadfc2010-05-19 22:15:50 -0600582static bool will_write_block(struct port *port)
583{
584 bool ret;
585
Amit Shah60e5e0b2010-05-27 13:24:40 +0530586 if (!port->guest_connected) {
587 /* Port got hot-unplugged. Let's exit. */
588 return false;
589 }
Amit Shahcdfadfc2010-05-19 22:15:50 -0600590 if (!port->host_connected)
591 return true;
592
593 spin_lock_irq(&port->outvq_lock);
594 /*
595 * Check if the Host has consumed any buffers since we last
596 * sent data (this is only applicable for nonblocking ports).
597 */
598 reclaim_consumed_buffers(port);
599 ret = port->outvq_full;
600 spin_unlock_irq(&port->outvq_lock);
601
602 return ret;
603}
604
Amit Shah2030fa42009-12-21 21:49:30 +0530605static ssize_t port_fops_read(struct file *filp, char __user *ubuf,
606 size_t count, loff_t *offp)
607{
608 struct port *port;
609 ssize_t ret;
610
611 port = filp->private_data;
612
613 if (!port_has_data(port)) {
614 /*
615 * If nothing's connected on the host just return 0 in
616 * case of list_empty; this tells the userspace app
617 * that there's no connection
618 */
619 if (!port->host_connected)
620 return 0;
621 if (filp->f_flags & O_NONBLOCK)
622 return -EAGAIN;
623
624 ret = wait_event_interruptible(port->waitqueue,
Amit Shah60caacd2010-05-19 22:15:49 -0600625 !will_read_block(port));
Amit Shah2030fa42009-12-21 21:49:30 +0530626 if (ret < 0)
627 return ret;
628 }
Amit Shahb3dddb92010-09-02 18:11:45 +0530629 /* Port got hot-unplugged. */
630 if (!port->guest_connected)
631 return -ENODEV;
Amit Shah2030fa42009-12-21 21:49:30 +0530632 /*
633 * We could've received a disconnection message while we were
634 * waiting for more data.
635 *
636 * This check is not clubbed in the if() statement above as we
637 * might receive some data as well as the host could get
638 * disconnected after we got woken up from our wait. So we
639 * really want to give off whatever data we have and only then
640 * check for host_connected.
641 */
642 if (!port_has_data(port) && !port->host_connected)
643 return 0;
644
645 return fill_readbuf(port, ubuf, count, true);
646}
647
648static ssize_t port_fops_write(struct file *filp, const char __user *ubuf,
649 size_t count, loff_t *offp)
650{
651 struct port *port;
652 char *buf;
653 ssize_t ret;
Amit Shahcdfadfc2010-05-19 22:15:50 -0600654 bool nonblock;
Amit Shah2030fa42009-12-21 21:49:30 +0530655
Amit Shah65745422010-09-14 13:26:16 +0530656 /* Userspace could be out to fool us */
657 if (!count)
658 return 0;
659
Amit Shah2030fa42009-12-21 21:49:30 +0530660 port = filp->private_data;
661
Amit Shahcdfadfc2010-05-19 22:15:50 -0600662 nonblock = filp->f_flags & O_NONBLOCK;
663
664 if (will_write_block(port)) {
665 if (nonblock)
666 return -EAGAIN;
667
668 ret = wait_event_interruptible(port->waitqueue,
669 !will_write_block(port));
670 if (ret < 0)
671 return ret;
672 }
Amit Shahf4028112010-09-02 18:11:46 +0530673 /* Port got hot-unplugged. */
674 if (!port->guest_connected)
675 return -ENODEV;
Amit Shahcdfadfc2010-05-19 22:15:50 -0600676
Amit Shah2030fa42009-12-21 21:49:30 +0530677 count = min((size_t)(32 * 1024), count);
678
679 buf = kmalloc(count, GFP_KERNEL);
680 if (!buf)
681 return -ENOMEM;
682
683 ret = copy_from_user(buf, ubuf, count);
684 if (ret) {
685 ret = -EFAULT;
686 goto free_buf;
687 }
688
Amit Shah531295e2010-10-20 13:45:43 +1030689 /*
690 * We now ask send_buf() to not spin for generic ports -- we
691 * can re-use the same code path that non-blocking file
692 * descriptors take for blocking file descriptors since the
693 * wait is already done and we're certain the write will go
694 * through to the host.
695 */
696 nonblock = true;
Amit Shahcdfadfc2010-05-19 22:15:50 -0600697 ret = send_buf(port, buf, count, nonblock);
698
699 if (nonblock && ret > 0)
700 goto out;
701
Amit Shah2030fa42009-12-21 21:49:30 +0530702free_buf:
703 kfree(buf);
Amit Shahcdfadfc2010-05-19 22:15:50 -0600704out:
Amit Shah2030fa42009-12-21 21:49:30 +0530705 return ret;
706}
707
708static unsigned int port_fops_poll(struct file *filp, poll_table *wait)
709{
710 struct port *port;
711 unsigned int ret;
712
713 port = filp->private_data;
714 poll_wait(filp, &port->waitqueue, wait);
715
Amit Shah8529a502010-09-02 18:11:44 +0530716 if (!port->guest_connected) {
717 /* Port got unplugged */
718 return POLLHUP;
719 }
Amit Shah2030fa42009-12-21 21:49:30 +0530720 ret = 0;
Hans de Goede6df7aad2010-09-16 14:43:08 +0530721 if (!will_read_block(port))
Amit Shah2030fa42009-12-21 21:49:30 +0530722 ret |= POLLIN | POLLRDNORM;
Amit Shahcdfadfc2010-05-19 22:15:50 -0600723 if (!will_write_block(port))
Amit Shah2030fa42009-12-21 21:49:30 +0530724 ret |= POLLOUT;
725 if (!port->host_connected)
726 ret |= POLLHUP;
727
728 return ret;
729}
730
Amit Shahb353a6b2010-09-02 18:38:29 +0530731static void remove_port(struct kref *kref);
732
Amit Shah2030fa42009-12-21 21:49:30 +0530733static int port_fops_release(struct inode *inode, struct file *filp)
734{
735 struct port *port;
736
737 port = filp->private_data;
738
739 /* Notify host of port being closed */
740 send_control_msg(port, VIRTIO_CONSOLE_PORT_OPEN, 0);
741
Amit Shah88f251a2009-12-21 22:15:30 +0530742 spin_lock_irq(&port->inbuf_lock);
Amit Shah3c7969c2009-11-26 11:25:38 +0530743 port->guest_connected = false;
744
Amit Shah88f251a2009-12-21 22:15:30 +0530745 discard_port_data(port);
746
747 spin_unlock_irq(&port->inbuf_lock);
748
Amit Shahcdfadfc2010-05-19 22:15:50 -0600749 spin_lock_irq(&port->outvq_lock);
750 reclaim_consumed_buffers(port);
751 spin_unlock_irq(&port->outvq_lock);
752
Amit Shahb353a6b2010-09-02 18:38:29 +0530753 /*
754 * Locks aren't necessary here as a port can't be opened after
755 * unplug, and if a port isn't unplugged, a kref would already
756 * exist for the port. Plus, taking ports_lock here would
757 * create a dependency on other locks taken by functions
758 * inside remove_port if we're the last holder of the port,
759 * creating many problems.
760 */
761 kref_put(&port->kref, remove_port);
762
Amit Shah2030fa42009-12-21 21:49:30 +0530763 return 0;
764}
765
766static int port_fops_open(struct inode *inode, struct file *filp)
767{
768 struct cdev *cdev = inode->i_cdev;
769 struct port *port;
Amit Shah8ad37e82010-09-02 18:11:48 +0530770 int ret;
Amit Shah2030fa42009-12-21 21:49:30 +0530771
Amit Shah04950cd2010-09-02 18:20:58 +0530772 port = find_port_by_devt(cdev->dev);
Amit Shah2030fa42009-12-21 21:49:30 +0530773 filp->private_data = port;
774
Amit Shahb353a6b2010-09-02 18:38:29 +0530775 /* Prevent against a port getting hot-unplugged at the same time */
776 spin_lock_irq(&port->portdev->ports_lock);
777 kref_get(&port->kref);
778 spin_unlock_irq(&port->portdev->ports_lock);
779
Amit Shah2030fa42009-12-21 21:49:30 +0530780 /*
781 * Don't allow opening of console port devices -- that's done
782 * via /dev/hvc
783 */
Amit Shah8ad37e82010-09-02 18:11:48 +0530784 if (is_console_port(port)) {
785 ret = -ENXIO;
786 goto out;
787 }
Amit Shah2030fa42009-12-21 21:49:30 +0530788
Amit Shah3c7969c2009-11-26 11:25:38 +0530789 /* Allow only one process to open a particular port at a time */
790 spin_lock_irq(&port->inbuf_lock);
791 if (port->guest_connected) {
792 spin_unlock_irq(&port->inbuf_lock);
Amit Shah8ad37e82010-09-02 18:11:48 +0530793 ret = -EMFILE;
794 goto out;
Amit Shah3c7969c2009-11-26 11:25:38 +0530795 }
796
797 port->guest_connected = true;
798 spin_unlock_irq(&port->inbuf_lock);
799
Amit Shahcdfadfc2010-05-19 22:15:50 -0600800 spin_lock_irq(&port->outvq_lock);
801 /*
802 * There might be a chance that we missed reclaiming a few
803 * buffers in the window of the port getting previously closed
804 * and opening now.
805 */
806 reclaim_consumed_buffers(port);
807 spin_unlock_irq(&port->outvq_lock);
808
Amit Shah2030fa42009-12-21 21:49:30 +0530809 /* Notify host of port being opened */
810 send_control_msg(filp->private_data, VIRTIO_CONSOLE_PORT_OPEN, 1);
811
812 return 0;
Amit Shah8ad37e82010-09-02 18:11:48 +0530813out:
Amit Shahb353a6b2010-09-02 18:38:29 +0530814 kref_put(&port->kref, remove_port);
Amit Shah8ad37e82010-09-02 18:11:48 +0530815 return ret;
Amit Shah2030fa42009-12-21 21:49:30 +0530816}
817
818/*
819 * The file operations that we support: programs in the guest can open
820 * a console device, read from it, write to it, poll for data and
821 * close it. The devices are at
822 * /dev/vport<device number>p<port number>
823 */
824static const struct file_operations port_fops = {
825 .owner = THIS_MODULE,
826 .open = port_fops_open,
827 .read = port_fops_read,
828 .write = port_fops_write,
829 .poll = port_fops_poll,
830 .release = port_fops_release,
831};
832
Amit Shahe27b5192010-01-18 19:15:02 +0530833/*
Rusty Russella23ea922010-01-18 19:14:55 +0530834 * The put_chars() callback is pretty straightforward.
Rusty Russell31610432007-10-22 11:03:39 +1000835 *
Rusty Russella23ea922010-01-18 19:14:55 +0530836 * We turn the characters into a scatter-gather list, add it to the
837 * output queue and then kick the Host. Then we sit here waiting for
838 * it to finish: inefficient in theory, but in practice
839 * implementations will do it immediately (lguest's Launcher does).
840 */
Rusty Russell31610432007-10-22 11:03:39 +1000841static int put_chars(u32 vtermno, const char *buf, int count)
842{
Rusty Russell21206ed2010-01-18 19:15:00 +0530843 struct port *port;
Amit Shah38edf582010-01-18 19:15:05 +0530844
François Diakhaté162a6892010-03-23 18:23:15 +0530845 if (unlikely(early_put_chars))
846 return early_put_chars(vtermno, buf, count);
847
Amit Shah38edf582010-01-18 19:15:05 +0530848 port = find_port_by_vtermno(vtermno);
849 if (!port)
Amit Shah6dc69f972010-05-19 22:15:47 -0600850 return -EPIPE;
Rusty Russell31610432007-10-22 11:03:39 +1000851
Amit Shahcdfadfc2010-05-19 22:15:50 -0600852 return send_buf(port, (void *)buf, count, false);
Rusty Russell31610432007-10-22 11:03:39 +1000853}
854
Rusty Russella23ea922010-01-18 19:14:55 +0530855/*
Rusty Russella23ea922010-01-18 19:14:55 +0530856 * get_chars() is the callback from the hvc_console infrastructure
857 * when an interrupt is received.
Rusty Russell31610432007-10-22 11:03:39 +1000858 *
Amit Shah203baab2010-01-18 19:15:12 +0530859 * We call out to fill_readbuf that gets us the required data from the
860 * buffers that are queued up.
Rusty Russella23ea922010-01-18 19:14:55 +0530861 */
Rusty Russell31610432007-10-22 11:03:39 +1000862static int get_chars(u32 vtermno, char *buf, int count)
863{
Rusty Russell21206ed2010-01-18 19:15:00 +0530864 struct port *port;
Rusty Russell31610432007-10-22 11:03:39 +1000865
Amit Shah6dc69f972010-05-19 22:15:47 -0600866 /* If we've not set up the port yet, we have no input to give. */
867 if (unlikely(early_put_chars))
868 return 0;
869
Amit Shah38edf582010-01-18 19:15:05 +0530870 port = find_port_by_vtermno(vtermno);
871 if (!port)
Amit Shah6dc69f972010-05-19 22:15:47 -0600872 return -EPIPE;
Rusty Russell21206ed2010-01-18 19:15:00 +0530873
874 /* If we don't have an input queue yet, we can't get input. */
875 BUG_ON(!port->in_vq);
876
Amit Shahb766cee2009-12-21 21:26:45 +0530877 return fill_readbuf(port, buf, count, false);
Rusty Russell31610432007-10-22 11:03:39 +1000878}
Rusty Russell31610432007-10-22 11:03:39 +1000879
Amit Shahcb06e362010-01-18 19:15:08 +0530880static void resize_console(struct port *port)
Christian Borntraegerc2983452008-11-25 13:36:26 +0100881{
Amit Shahcb06e362010-01-18 19:15:08 +0530882 struct virtio_device *vdev;
Christian Borntraegerc2983452008-11-25 13:36:26 +0100883
Amit Shah2de16a42010-03-19 17:36:44 +0530884 /* The port could have been hot-unplugged */
Amit Shah97788292010-05-06 02:05:08 +0530885 if (!port || !is_console_port(port))
Amit Shah2de16a42010-03-19 17:36:44 +0530886 return;
887
Amit Shahcb06e362010-01-18 19:15:08 +0530888 vdev = port->portdev->vdev;
Amit Shah97788292010-05-06 02:05:08 +0530889 if (virtio_has_feature(vdev, VIRTIO_CONSOLE_F_SIZE))
890 hvc_resize(port->cons.hvc, port->cons.ws);
Christian Borntraegerc2983452008-11-25 13:36:26 +0100891}
892
Amit Shah38edf582010-01-18 19:15:05 +0530893/* We set the configuration at this point, since we now have a tty */
Christian Borntraeger91fcad12008-06-20 15:24:15 +0200894static int notifier_add_vio(struct hvc_struct *hp, int data)
895{
Amit Shah38edf582010-01-18 19:15:05 +0530896 struct port *port;
897
898 port = find_port_by_vtermno(hp->vtermno);
899 if (!port)
900 return -EINVAL;
901
Christian Borntraeger91fcad12008-06-20 15:24:15 +0200902 hp->irq_requested = 1;
Amit Shahcb06e362010-01-18 19:15:08 +0530903 resize_console(port);
Christian Borntraegerc2983452008-11-25 13:36:26 +0100904
Christian Borntraeger91fcad12008-06-20 15:24:15 +0200905 return 0;
906}
907
908static void notifier_del_vio(struct hvc_struct *hp, int data)
909{
910 hp->irq_requested = 0;
911}
912
Amit Shah17634ba2009-12-21 21:03:25 +0530913/* The operations for console ports. */
Rusty Russell1dff3992009-11-28 12:20:26 +0530914static const struct hv_ops hv_ops = {
Rusty Russell971f3392010-01-18 19:14:56 +0530915 .get_chars = get_chars,
916 .put_chars = put_chars,
917 .notifier_add = notifier_add_vio,
918 .notifier_del = notifier_del_vio,
919 .notifier_hangup = notifier_del_vio,
920};
921
922/*
923 * Console drivers are initialized very early so boot messages can go
924 * out, so we do things slightly differently from the generic virtio
925 * initialization of the net and block drivers.
926 *
927 * At this stage, the console is output-only. It's too early to set
928 * up a virtqueue, so we let the drivers do some boutique early-output
929 * thing.
930 */
931int __init virtio_cons_early_init(int (*put_chars)(u32, const char *, int))
932{
933 early_put_chars = put_chars;
934 return hvc_instantiate(0, 0, &hv_ops);
935}
936
Amit Shah17634ba2009-12-21 21:03:25 +0530937int init_port_console(struct port *port)
Amit Shahcfa6d372010-01-18 19:15:10 +0530938{
939 int ret;
940
941 /*
942 * The Host's telling us this port is a console port. Hook it
943 * up with an hvc console.
944 *
945 * To set up and manage our virtual console, we call
946 * hvc_alloc().
947 *
948 * The first argument of hvc_alloc() is the virtual console
949 * number. The second argument is the parameter for the
950 * notification mechanism (like irq number). We currently
951 * leave this as zero, virtqueues have implicit notifications.
952 *
953 * The third argument is a "struct hv_ops" containing the
954 * put_chars() get_chars(), notifier_add() and notifier_del()
955 * pointers. The final argument is the output buffer size: we
956 * can do any size, so we put PAGE_SIZE here.
957 */
958 port->cons.vtermno = pdrvdata.next_vtermno;
959
960 port->cons.hvc = hvc_alloc(port->cons.vtermno, 0, &hv_ops, PAGE_SIZE);
961 if (IS_ERR(port->cons.hvc)) {
962 ret = PTR_ERR(port->cons.hvc);
Amit Shah298add72010-01-18 16:35:23 +0530963 dev_err(port->dev,
964 "error %d allocating hvc for port\n", ret);
Amit Shahcfa6d372010-01-18 19:15:10 +0530965 port->cons.hvc = NULL;
966 return ret;
967 }
968 spin_lock_irq(&pdrvdata_lock);
969 pdrvdata.next_vtermno++;
970 list_add_tail(&port->cons.list, &pdrvdata.consoles);
971 spin_unlock_irq(&pdrvdata_lock);
Amit Shah3c7969c2009-11-26 11:25:38 +0530972 port->guest_connected = true;
Amit Shahcfa6d372010-01-18 19:15:10 +0530973
Amit Shah1d051602010-05-19 22:15:49 -0600974 /*
975 * Start using the new console output if this is the first
976 * console to come up.
977 */
978 if (early_put_chars)
979 early_put_chars = NULL;
980
Amit Shah2030fa42009-12-21 21:49:30 +0530981 /* Notify host of port being opened */
982 send_control_msg(port, VIRTIO_CONSOLE_PORT_OPEN, 1);
983
Amit Shahcfa6d372010-01-18 19:15:10 +0530984 return 0;
985}
986
Amit Shah431edb82009-12-21 21:57:40 +0530987static ssize_t show_port_name(struct device *dev,
988 struct device_attribute *attr, char *buffer)
989{
990 struct port *port;
991
992 port = dev_get_drvdata(dev);
993
994 return sprintf(buffer, "%s\n", port->name);
995}
996
997static DEVICE_ATTR(name, S_IRUGO, show_port_name, NULL);
998
999static struct attribute *port_sysfs_entries[] = {
1000 &dev_attr_name.attr,
1001 NULL
1002};
1003
1004static struct attribute_group port_attribute_group = {
1005 .name = NULL, /* put in device directory */
1006 .attrs = port_sysfs_entries,
1007};
1008
Amit Shahd99393e2009-12-21 22:36:21 +05301009static int debugfs_open(struct inode *inode, struct file *filp)
1010{
1011 filp->private_data = inode->i_private;
1012 return 0;
1013}
1014
1015static ssize_t debugfs_read(struct file *filp, char __user *ubuf,
1016 size_t count, loff_t *offp)
1017{
1018 struct port *port;
1019 char *buf;
1020 ssize_t ret, out_offset, out_count;
1021
1022 out_count = 1024;
1023 buf = kmalloc(out_count, GFP_KERNEL);
1024 if (!buf)
1025 return -ENOMEM;
1026
1027 port = filp->private_data;
1028 out_offset = 0;
1029 out_offset += snprintf(buf + out_offset, out_count,
1030 "name: %s\n", port->name ? port->name : "");
1031 out_offset += snprintf(buf + out_offset, out_count - out_offset,
1032 "guest_connected: %d\n", port->guest_connected);
1033 out_offset += snprintf(buf + out_offset, out_count - out_offset,
1034 "host_connected: %d\n", port->host_connected);
1035 out_offset += snprintf(buf + out_offset, out_count - out_offset,
Amit Shahcdfadfc2010-05-19 22:15:50 -06001036 "outvq_full: %d\n", port->outvq_full);
1037 out_offset += snprintf(buf + out_offset, out_count - out_offset,
Amit Shahd99393e2009-12-21 22:36:21 +05301038 "is_console: %s\n",
1039 is_console_port(port) ? "yes" : "no");
1040 out_offset += snprintf(buf + out_offset, out_count - out_offset,
1041 "console_vtermno: %u\n", port->cons.vtermno);
1042
1043 ret = simple_read_from_buffer(ubuf, count, offp, buf, out_offset);
1044 kfree(buf);
1045 return ret;
1046}
1047
1048static const struct file_operations port_debugfs_ops = {
1049 .owner = THIS_MODULE,
1050 .open = debugfs_open,
1051 .read = debugfs_read,
1052};
1053
Amit Shah97788292010-05-06 02:05:08 +05301054static void set_console_size(struct port *port, u16 rows, u16 cols)
1055{
1056 if (!port || !is_console_port(port))
1057 return;
1058
1059 port->cons.ws.ws_row = rows;
1060 port->cons.ws.ws_col = cols;
1061}
1062
Amit Shahc446f8f2010-05-19 22:15:48 -06001063static unsigned int fill_queue(struct virtqueue *vq, spinlock_t *lock)
1064{
1065 struct port_buffer *buf;
1066 unsigned int nr_added_bufs;
1067 int ret;
1068
1069 nr_added_bufs = 0;
1070 do {
1071 buf = alloc_buf(PAGE_SIZE);
1072 if (!buf)
1073 break;
1074
1075 spin_lock_irq(lock);
1076 ret = add_inbuf(vq, buf);
1077 if (ret < 0) {
1078 spin_unlock_irq(lock);
1079 free_buf(buf);
1080 break;
1081 }
1082 nr_added_bufs++;
1083 spin_unlock_irq(lock);
1084 } while (ret > 0);
1085
1086 return nr_added_bufs;
1087}
1088
1089static int add_port(struct ports_device *portdev, u32 id)
1090{
1091 char debugfs_name[16];
1092 struct port *port;
1093 struct port_buffer *buf;
1094 dev_t devt;
1095 unsigned int nr_added_bufs;
1096 int err;
1097
1098 port = kmalloc(sizeof(*port), GFP_KERNEL);
1099 if (!port) {
1100 err = -ENOMEM;
1101 goto fail;
1102 }
Amit Shahb353a6b2010-09-02 18:38:29 +05301103 kref_init(&port->kref);
Amit Shahc446f8f2010-05-19 22:15:48 -06001104
1105 port->portdev = portdev;
1106 port->id = id;
1107
1108 port->name = NULL;
1109 port->inbuf = NULL;
1110 port->cons.hvc = NULL;
1111
Amit Shah97788292010-05-06 02:05:08 +05301112 port->cons.ws.ws_row = port->cons.ws.ws_col = 0;
1113
Amit Shahc446f8f2010-05-19 22:15:48 -06001114 port->host_connected = port->guest_connected = false;
1115
Amit Shahcdfadfc2010-05-19 22:15:50 -06001116 port->outvq_full = false;
1117
Amit Shahc446f8f2010-05-19 22:15:48 -06001118 port->in_vq = portdev->in_vqs[port->id];
1119 port->out_vq = portdev->out_vqs[port->id];
1120
Amit Shahd22a6982010-09-02 18:20:59 +05301121 port->cdev = cdev_alloc();
1122 if (!port->cdev) {
1123 dev_err(&port->portdev->vdev->dev, "Error allocating cdev\n");
1124 err = -ENOMEM;
1125 goto free_port;
1126 }
1127 port->cdev->ops = &port_fops;
Amit Shahc446f8f2010-05-19 22:15:48 -06001128
1129 devt = MKDEV(portdev->chr_major, id);
Amit Shahd22a6982010-09-02 18:20:59 +05301130 err = cdev_add(port->cdev, devt, 1);
Amit Shahc446f8f2010-05-19 22:15:48 -06001131 if (err < 0) {
1132 dev_err(&port->portdev->vdev->dev,
1133 "Error %d adding cdev for port %u\n", err, id);
Amit Shahd22a6982010-09-02 18:20:59 +05301134 goto free_cdev;
Amit Shahc446f8f2010-05-19 22:15:48 -06001135 }
1136 port->dev = device_create(pdrvdata.class, &port->portdev->vdev->dev,
1137 devt, port, "vport%up%u",
1138 port->portdev->drv_index, id);
1139 if (IS_ERR(port->dev)) {
1140 err = PTR_ERR(port->dev);
1141 dev_err(&port->portdev->vdev->dev,
1142 "Error %d creating device for port %u\n",
1143 err, id);
1144 goto free_cdev;
1145 }
1146
1147 spin_lock_init(&port->inbuf_lock);
Amit Shahcdfadfc2010-05-19 22:15:50 -06001148 spin_lock_init(&port->outvq_lock);
Amit Shahc446f8f2010-05-19 22:15:48 -06001149 init_waitqueue_head(&port->waitqueue);
1150
1151 /* Fill the in_vq with buffers so the host can send us data. */
1152 nr_added_bufs = fill_queue(port->in_vq, &port->inbuf_lock);
1153 if (!nr_added_bufs) {
1154 dev_err(port->dev, "Error allocating inbufs\n");
1155 err = -ENOMEM;
1156 goto free_device;
1157 }
1158
1159 /*
1160 * If we're not using multiport support, this has to be a console port
1161 */
1162 if (!use_multiport(port->portdev)) {
1163 err = init_port_console(port);
1164 if (err)
1165 goto free_inbufs;
1166 }
1167
1168 spin_lock_irq(&portdev->ports_lock);
1169 list_add_tail(&port->list, &port->portdev->ports);
1170 spin_unlock_irq(&portdev->ports_lock);
1171
1172 /*
1173 * Tell the Host we're set so that it can send us various
1174 * configuration parameters for this port (eg, port name,
1175 * caching, whether this is a console port, etc.)
1176 */
1177 send_control_msg(port, VIRTIO_CONSOLE_PORT_READY, 1);
1178
1179 if (pdrvdata.debugfs_dir) {
1180 /*
1181 * Finally, create the debugfs file that we can use to
1182 * inspect a port's state at any time
1183 */
1184 sprintf(debugfs_name, "vport%up%u",
1185 port->portdev->drv_index, id);
1186 port->debugfs_file = debugfs_create_file(debugfs_name, 0444,
1187 pdrvdata.debugfs_dir,
1188 port,
1189 &port_debugfs_ops);
1190 }
1191 return 0;
1192
1193free_inbufs:
1194 while ((buf = virtqueue_detach_unused_buf(port->in_vq)))
1195 free_buf(buf);
1196free_device:
1197 device_destroy(pdrvdata.class, port->dev->devt);
1198free_cdev:
Amit Shahd22a6982010-09-02 18:20:59 +05301199 cdev_del(port->cdev);
Amit Shahc446f8f2010-05-19 22:15:48 -06001200free_port:
1201 kfree(port);
1202fail:
1203 /* The host might want to notify management sw about port add failure */
Julia Lawall0643e4c2010-05-15 11:45:53 +02001204 __send_control_msg(portdev, id, VIRTIO_CONSOLE_PORT_READY, 0);
Amit Shahc446f8f2010-05-19 22:15:48 -06001205 return err;
1206}
1207
Amit Shahb353a6b2010-09-02 18:38:29 +05301208/* No users remain, remove all port-specific data. */
1209static void remove_port(struct kref *kref)
1210{
1211 struct port *port;
1212
1213 port = container_of(kref, struct port, kref);
1214
1215 sysfs_remove_group(&port->dev->kobj, &port_attribute_group);
1216 device_destroy(pdrvdata.class, port->dev->devt);
1217 cdev_del(port->cdev);
1218
1219 kfree(port->name);
1220
1221 debugfs_remove(port->debugfs_file);
1222
1223 kfree(port);
1224}
1225
1226/*
1227 * Port got unplugged. Remove port from portdev's list and drop the
1228 * kref reference. If no userspace has this port opened, it will
1229 * result in immediate removal the port.
1230 */
1231static void unplug_port(struct port *port)
Amit Shah1f7aa422009-12-21 22:27:31 +05301232{
Amit Shaha9cdd482010-02-12 10:32:15 +05301233 struct port_buffer *buf;
1234
Amit Shahb353a6b2010-09-02 18:38:29 +05301235 spin_lock_irq(&port->portdev->ports_lock);
1236 list_del(&port->list);
1237 spin_unlock_irq(&port->portdev->ports_lock);
1238
Amit Shah00476342010-05-27 13:24:39 +05301239 if (port->guest_connected) {
1240 port->guest_connected = false;
1241 port->host_connected = false;
1242 wake_up_interruptible(&port->waitqueue);
Amit Shah00476342010-05-27 13:24:39 +05301243 }
1244
Amit Shah1f7aa422009-12-21 22:27:31 +05301245 if (is_console_port(port)) {
1246 spin_lock_irq(&pdrvdata_lock);
1247 list_del(&port->cons.list);
1248 spin_unlock_irq(&pdrvdata_lock);
Amit Shah69eb9a92010-05-19 22:15:47 -06001249#if 0
1250 /*
1251 * hvc_remove() not called as removing one hvc port
1252 * results in other hvc ports getting frozen.
1253 *
1254 * Once this is resolved in hvc, this functionality
1255 * will be enabled. Till that is done, the -EPIPE
1256 * return from get_chars() above will help
1257 * hvc_console.c to clean up on ports we remove here.
1258 */
Amit Shah1f7aa422009-12-21 22:27:31 +05301259 hvc_remove(port->cons.hvc);
Amit Shah69eb9a92010-05-19 22:15:47 -06001260#endif
Amit Shah1f7aa422009-12-21 22:27:31 +05301261 }
Amit Shah1f7aa422009-12-21 22:27:31 +05301262
Amit Shaha9cdd482010-02-12 10:32:15 +05301263 /* Remove unused data this port might have received. */
Amit Shah1f7aa422009-12-21 22:27:31 +05301264 discard_port_data(port);
Amit Shaha9cdd482010-02-12 10:32:15 +05301265
Amit Shahcdfadfc2010-05-19 22:15:50 -06001266 reclaim_consumed_buffers(port);
1267
Amit Shaha9cdd482010-02-12 10:32:15 +05301268 /* Remove buffers we queued up for the Host to send us data in. */
Michael S. Tsirkin505b0452010-04-12 16:18:32 +03001269 while ((buf = virtqueue_detach_unused_buf(port->in_vq)))
Amit Shaha9cdd482010-02-12 10:32:15 +05301270 free_buf(buf);
1271
Amit Shahb353a6b2010-09-02 18:38:29 +05301272 /*
1273 * We should just assume the device itself has gone off --
1274 * else a close on an open port later will try to send out a
1275 * control message.
1276 */
1277 port->portdev = NULL;
Amit Shah1f7aa422009-12-21 22:27:31 +05301278
Amit Shahb353a6b2010-09-02 18:38:29 +05301279 /*
1280 * Locks around here are not necessary - a port can't be
1281 * opened after we removed the port struct from ports_list
1282 * above.
1283 */
1284 kref_put(&port->kref, remove_port);
Amit Shah1f7aa422009-12-21 22:27:31 +05301285}
1286
Amit Shah17634ba2009-12-21 21:03:25 +05301287/* Any private messages that the Host and Guest want to share */
1288static void handle_control_message(struct ports_device *portdev,
1289 struct port_buffer *buf)
1290{
1291 struct virtio_console_control *cpkt;
1292 struct port *port;
Amit Shah431edb82009-12-21 21:57:40 +05301293 size_t name_size;
1294 int err;
Amit Shah17634ba2009-12-21 21:03:25 +05301295
1296 cpkt = (struct virtio_console_control *)(buf->buf + buf->offset);
1297
1298 port = find_port_by_id(portdev, cpkt->id);
Amit Shahf909f852010-05-19 22:15:48 -06001299 if (!port && cpkt->event != VIRTIO_CONSOLE_PORT_ADD) {
Amit Shah17634ba2009-12-21 21:03:25 +05301300 /* No valid header at start of buffer. Drop it. */
1301 dev_dbg(&portdev->vdev->dev,
1302 "Invalid index %u in control packet\n", cpkt->id);
1303 return;
1304 }
1305
1306 switch (cpkt->event) {
Amit Shahf909f852010-05-19 22:15:48 -06001307 case VIRTIO_CONSOLE_PORT_ADD:
1308 if (port) {
Amit Shah1d051602010-05-19 22:15:49 -06001309 dev_dbg(&portdev->vdev->dev,
1310 "Port %u already added\n", port->id);
Amit Shahf909f852010-05-19 22:15:48 -06001311 send_control_msg(port, VIRTIO_CONSOLE_PORT_READY, 1);
1312 break;
1313 }
1314 if (cpkt->id >= portdev->config.max_nr_ports) {
1315 dev_warn(&portdev->vdev->dev,
1316 "Request for adding port with out-of-bound id %u, max. supported id: %u\n",
1317 cpkt->id, portdev->config.max_nr_ports - 1);
1318 break;
1319 }
1320 add_port(portdev, cpkt->id);
1321 break;
1322 case VIRTIO_CONSOLE_PORT_REMOVE:
Amit Shahb353a6b2010-09-02 18:38:29 +05301323 unplug_port(port);
Amit Shahf909f852010-05-19 22:15:48 -06001324 break;
Amit Shah17634ba2009-12-21 21:03:25 +05301325 case VIRTIO_CONSOLE_CONSOLE_PORT:
1326 if (!cpkt->value)
1327 break;
1328 if (is_console_port(port))
1329 break;
1330
1331 init_port_console(port);
1332 /*
1333 * Could remove the port here in case init fails - but
1334 * have to notify the host first.
1335 */
1336 break;
Amit Shah8345adb2010-05-06 02:05:09 +05301337 case VIRTIO_CONSOLE_RESIZE: {
1338 struct {
1339 __u16 rows;
1340 __u16 cols;
1341 } size;
1342
Amit Shah17634ba2009-12-21 21:03:25 +05301343 if (!is_console_port(port))
1344 break;
Amit Shah8345adb2010-05-06 02:05:09 +05301345
1346 memcpy(&size, buf->buf + buf->offset + sizeof(*cpkt),
1347 sizeof(size));
1348 set_console_size(port, size.rows, size.cols);
1349
Amit Shah17634ba2009-12-21 21:03:25 +05301350 port->cons.hvc->irq_requested = 1;
1351 resize_console(port);
1352 break;
Amit Shah8345adb2010-05-06 02:05:09 +05301353 }
Amit Shah2030fa42009-12-21 21:49:30 +05301354 case VIRTIO_CONSOLE_PORT_OPEN:
1355 port->host_connected = cpkt->value;
1356 wake_up_interruptible(&port->waitqueue);
Amit Shahcdfadfc2010-05-19 22:15:50 -06001357 /*
1358 * If the host port got closed and the host had any
1359 * unconsumed buffers, we'll be able to reclaim them
1360 * now.
1361 */
1362 spin_lock_irq(&port->outvq_lock);
1363 reclaim_consumed_buffers(port);
1364 spin_unlock_irq(&port->outvq_lock);
Amit Shah2030fa42009-12-21 21:49:30 +05301365 break;
Amit Shah431edb82009-12-21 21:57:40 +05301366 case VIRTIO_CONSOLE_PORT_NAME:
1367 /*
1368 * Skip the size of the header and the cpkt to get the size
1369 * of the name that was sent
1370 */
1371 name_size = buf->len - buf->offset - sizeof(*cpkt) + 1;
1372
1373 port->name = kmalloc(name_size, GFP_KERNEL);
1374 if (!port->name) {
1375 dev_err(port->dev,
1376 "Not enough space to store port name\n");
1377 break;
1378 }
1379 strncpy(port->name, buf->buf + buf->offset + sizeof(*cpkt),
1380 name_size - 1);
1381 port->name[name_size - 1] = 0;
1382
1383 /*
1384 * Since we only have one sysfs attribute, 'name',
1385 * create it only if we have a name for the port.
1386 */
1387 err = sysfs_create_group(&port->dev->kobj,
1388 &port_attribute_group);
Amit Shahec642132010-03-19 17:36:43 +05301389 if (err) {
Amit Shah431edb82009-12-21 21:57:40 +05301390 dev_err(port->dev,
1391 "Error %d creating sysfs device attributes\n",
1392 err);
Amit Shahec642132010-03-19 17:36:43 +05301393 } else {
1394 /*
1395 * Generate a udev event so that appropriate
1396 * symlinks can be created based on udev
1397 * rules.
1398 */
1399 kobject_uevent(&port->dev->kobj, KOBJ_CHANGE);
1400 }
Amit Shah431edb82009-12-21 21:57:40 +05301401 break;
Amit Shah17634ba2009-12-21 21:03:25 +05301402 }
1403}
1404
1405static void control_work_handler(struct work_struct *work)
1406{
1407 struct ports_device *portdev;
1408 struct virtqueue *vq;
1409 struct port_buffer *buf;
1410 unsigned int len;
1411
1412 portdev = container_of(work, struct ports_device, control_work);
1413 vq = portdev->c_ivq;
1414
1415 spin_lock(&portdev->cvq_lock);
Michael S. Tsirkin505b0452010-04-12 16:18:32 +03001416 while ((buf = virtqueue_get_buf(vq, &len))) {
Amit Shah17634ba2009-12-21 21:03:25 +05301417 spin_unlock(&portdev->cvq_lock);
1418
1419 buf->len = len;
1420 buf->offset = 0;
1421
1422 handle_control_message(portdev, buf);
1423
1424 spin_lock(&portdev->cvq_lock);
1425 if (add_inbuf(portdev->c_ivq, buf) < 0) {
1426 dev_warn(&portdev->vdev->dev,
1427 "Error adding buffer to queue\n");
1428 free_buf(buf);
1429 }
1430 }
1431 spin_unlock(&portdev->cvq_lock);
1432}
1433
1434static void in_intr(struct virtqueue *vq)
1435{
1436 struct port *port;
1437 unsigned long flags;
1438
1439 port = find_port_by_vq(vq->vdev->priv, vq);
1440 if (!port)
1441 return;
1442
1443 spin_lock_irqsave(&port->inbuf_lock, flags);
Amit Shahd6933562010-02-12 10:32:18 +05301444 if (!port->inbuf)
1445 port->inbuf = get_inbuf(port);
Amit Shah17634ba2009-12-21 21:03:25 +05301446
Amit Shah88f251a2009-12-21 22:15:30 +05301447 /*
1448 * Don't queue up data when port is closed. This condition
1449 * can be reached when a console port is not yet connected (no
1450 * tty is spawned) and the host sends out data to console
1451 * ports. For generic serial ports, the host won't
1452 * (shouldn't) send data till the guest is connected.
1453 */
1454 if (!port->guest_connected)
1455 discard_port_data(port);
1456
Amit Shah17634ba2009-12-21 21:03:25 +05301457 spin_unlock_irqrestore(&port->inbuf_lock, flags);
1458
Amit Shah2030fa42009-12-21 21:49:30 +05301459 wake_up_interruptible(&port->waitqueue);
1460
Amit Shah17634ba2009-12-21 21:03:25 +05301461 if (is_console_port(port) && hvc_poll(port->cons.hvc))
1462 hvc_kick();
1463}
1464
1465static void control_intr(struct virtqueue *vq)
1466{
1467 struct ports_device *portdev;
1468
1469 portdev = vq->vdev->priv;
1470 schedule_work(&portdev->control_work);
1471}
1472
Amit Shah7f5d8102009-12-21 22:22:08 +05301473static void config_intr(struct virtio_device *vdev)
1474{
1475 struct ports_device *portdev;
1476
1477 portdev = vdev->priv;
Amit Shah99f905f2010-05-19 22:15:48 -06001478
Amit Shah4038f5b72010-05-06 02:05:07 +05301479 if (!use_multiport(portdev)) {
Amit Shah97788292010-05-06 02:05:08 +05301480 struct port *port;
1481 u16 rows, cols;
1482
1483 vdev->config->get(vdev,
1484 offsetof(struct virtio_console_config, cols),
1485 &cols, sizeof(u16));
1486 vdev->config->get(vdev,
1487 offsetof(struct virtio_console_config, rows),
1488 &rows, sizeof(u16));
1489
1490 port = find_port_by_id(portdev, 0);
1491 set_console_size(port, rows, cols);
1492
Amit Shah4038f5b72010-05-06 02:05:07 +05301493 /*
1494 * We'll use this way of resizing only for legacy
1495 * support. For newer userspace
1496 * (VIRTIO_CONSOLE_F_MULTPORT+), use control messages
1497 * to indicate console size changes so that it can be
1498 * done per-port.
1499 */
Amit Shah97788292010-05-06 02:05:08 +05301500 resize_console(port);
Amit Shah4038f5b72010-05-06 02:05:07 +05301501 }
Amit Shah7f5d8102009-12-21 22:22:08 +05301502}
1503
Amit Shah2658a79a2010-01-18 19:15:11 +05301504static int init_vqs(struct ports_device *portdev)
1505{
1506 vq_callback_t **io_callbacks;
1507 char **io_names;
1508 struct virtqueue **vqs;
Amit Shah17634ba2009-12-21 21:03:25 +05301509 u32 i, j, nr_ports, nr_queues;
Amit Shah2658a79a2010-01-18 19:15:11 +05301510 int err;
1511
Amit Shah17634ba2009-12-21 21:03:25 +05301512 nr_ports = portdev->config.max_nr_ports;
1513 nr_queues = use_multiport(portdev) ? (nr_ports + 1) * 2 : 2;
Amit Shah2658a79a2010-01-18 19:15:11 +05301514
1515 vqs = kmalloc(nr_queues * sizeof(struct virtqueue *), GFP_KERNEL);
1516 if (!vqs) {
1517 err = -ENOMEM;
1518 goto fail;
1519 }
1520 io_callbacks = kmalloc(nr_queues * sizeof(vq_callback_t *), GFP_KERNEL);
1521 if (!io_callbacks) {
1522 err = -ENOMEM;
1523 goto free_vqs;
1524 }
1525 io_names = kmalloc(nr_queues * sizeof(char *), GFP_KERNEL);
1526 if (!io_names) {
1527 err = -ENOMEM;
1528 goto free_callbacks;
1529 }
1530 portdev->in_vqs = kmalloc(nr_ports * sizeof(struct virtqueue *),
1531 GFP_KERNEL);
1532 if (!portdev->in_vqs) {
1533 err = -ENOMEM;
1534 goto free_names;
1535 }
1536 portdev->out_vqs = kmalloc(nr_ports * sizeof(struct virtqueue *),
1537 GFP_KERNEL);
1538 if (!portdev->out_vqs) {
1539 err = -ENOMEM;
1540 goto free_invqs;
1541 }
1542
Amit Shah17634ba2009-12-21 21:03:25 +05301543 /*
1544 * For backward compat (newer host but older guest), the host
1545 * spawns a console port first and also inits the vqs for port
1546 * 0 before others.
1547 */
1548 j = 0;
1549 io_callbacks[j] = in_intr;
1550 io_callbacks[j + 1] = NULL;
1551 io_names[j] = "input";
1552 io_names[j + 1] = "output";
1553 j += 2;
Amit Shah2658a79a2010-01-18 19:15:11 +05301554
Amit Shah17634ba2009-12-21 21:03:25 +05301555 if (use_multiport(portdev)) {
1556 io_callbacks[j] = control_intr;
1557 io_callbacks[j + 1] = NULL;
1558 io_names[j] = "control-i";
1559 io_names[j + 1] = "control-o";
1560
1561 for (i = 1; i < nr_ports; i++) {
1562 j += 2;
1563 io_callbacks[j] = in_intr;
1564 io_callbacks[j + 1] = NULL;
1565 io_names[j] = "input";
1566 io_names[j + 1] = "output";
1567 }
1568 }
Amit Shah2658a79a2010-01-18 19:15:11 +05301569 /* Find the queues. */
1570 err = portdev->vdev->config->find_vqs(portdev->vdev, nr_queues, vqs,
1571 io_callbacks,
1572 (const char **)io_names);
1573 if (err)
1574 goto free_outvqs;
1575
Amit Shah17634ba2009-12-21 21:03:25 +05301576 j = 0;
Amit Shah2658a79a2010-01-18 19:15:11 +05301577 portdev->in_vqs[0] = vqs[0];
1578 portdev->out_vqs[0] = vqs[1];
Amit Shah17634ba2009-12-21 21:03:25 +05301579 j += 2;
1580 if (use_multiport(portdev)) {
1581 portdev->c_ivq = vqs[j];
1582 portdev->c_ovq = vqs[j + 1];
Amit Shah2658a79a2010-01-18 19:15:11 +05301583
Amit Shah17634ba2009-12-21 21:03:25 +05301584 for (i = 1; i < nr_ports; i++) {
1585 j += 2;
1586 portdev->in_vqs[i] = vqs[j];
1587 portdev->out_vqs[i] = vqs[j + 1];
1588 }
1589 }
Amit Shah2658a79a2010-01-18 19:15:11 +05301590 kfree(io_callbacks);
1591 kfree(io_names);
1592 kfree(vqs);
1593
1594 return 0;
1595
1596free_names:
1597 kfree(io_names);
1598free_callbacks:
1599 kfree(io_callbacks);
1600free_outvqs:
1601 kfree(portdev->out_vqs);
1602free_invqs:
1603 kfree(portdev->in_vqs);
1604free_vqs:
1605 kfree(vqs);
1606fail:
1607 return err;
1608}
1609
Amit Shahfb08bd22009-12-21 21:36:04 +05301610static const struct file_operations portdev_fops = {
1611 .owner = THIS_MODULE,
1612};
1613
Amit Shah1c85bf32010-01-18 19:15:07 +05301614/*
1615 * Once we're further in boot, we get probed like any other virtio
1616 * device.
Amit Shah17634ba2009-12-21 21:03:25 +05301617 *
1618 * If the host also supports multiple console ports, we check the
1619 * config space to see how many ports the host has spawned. We
1620 * initialize each port found.
Amit Shah1c85bf32010-01-18 19:15:07 +05301621 */
1622static int __devinit virtcons_probe(struct virtio_device *vdev)
1623{
Amit Shah1c85bf32010-01-18 19:15:07 +05301624 struct ports_device *portdev;
1625 int err;
Amit Shah17634ba2009-12-21 21:03:25 +05301626 bool multiport;
Amit Shah1c85bf32010-01-18 19:15:07 +05301627
1628 portdev = kmalloc(sizeof(*portdev), GFP_KERNEL);
1629 if (!portdev) {
1630 err = -ENOMEM;
1631 goto fail;
1632 }
1633
1634 /* Attach this portdev to this virtio_device, and vice-versa. */
1635 portdev->vdev = vdev;
1636 vdev->priv = portdev;
1637
Amit Shahfb08bd22009-12-21 21:36:04 +05301638 spin_lock_irq(&pdrvdata_lock);
1639 portdev->drv_index = pdrvdata.index++;
1640 spin_unlock_irq(&pdrvdata_lock);
1641
1642 portdev->chr_major = register_chrdev(0, "virtio-portsdev",
1643 &portdev_fops);
1644 if (portdev->chr_major < 0) {
1645 dev_err(&vdev->dev,
1646 "Error %d registering chrdev for device %u\n",
1647 portdev->chr_major, portdev->drv_index);
1648 err = portdev->chr_major;
1649 goto free;
1650 }
1651
Amit Shah17634ba2009-12-21 21:03:25 +05301652 multiport = false;
Amit Shah17634ba2009-12-21 21:03:25 +05301653 portdev->config.max_nr_ports = 1;
1654 if (virtio_has_feature(vdev, VIRTIO_CONSOLE_F_MULTIPORT)) {
1655 multiport = true;
1656 vdev->features[0] |= 1 << VIRTIO_CONSOLE_F_MULTIPORT;
1657
Amit Shahb99fa812010-05-19 22:15:46 -06001658 vdev->config->get(vdev, offsetof(struct virtio_console_config,
Amit Shahb99fa812010-05-19 22:15:46 -06001659 max_nr_ports),
Amit Shah17634ba2009-12-21 21:03:25 +05301660 &portdev->config.max_nr_ports,
1661 sizeof(portdev->config.max_nr_ports));
Amit Shah17634ba2009-12-21 21:03:25 +05301662 }
1663
1664 /* Let the Host know we support multiple ports.*/
1665 vdev->config->finalize_features(vdev);
1666
Amit Shah2658a79a2010-01-18 19:15:11 +05301667 err = init_vqs(portdev);
1668 if (err < 0) {
1669 dev_err(&vdev->dev, "Error %d initializing vqs\n", err);
Amit Shahfb08bd22009-12-21 21:36:04 +05301670 goto free_chrdev;
Amit Shah2658a79a2010-01-18 19:15:11 +05301671 }
Amit Shah1c85bf32010-01-18 19:15:07 +05301672
Amit Shah17634ba2009-12-21 21:03:25 +05301673 spin_lock_init(&portdev->ports_lock);
1674 INIT_LIST_HEAD(&portdev->ports);
1675
1676 if (multiport) {
Amit Shah335a64a5c2010-02-24 10:37:44 +05301677 unsigned int nr_added_bufs;
1678
Amit Shah17634ba2009-12-21 21:03:25 +05301679 spin_lock_init(&portdev->cvq_lock);
1680 INIT_WORK(&portdev->control_work, &control_work_handler);
1681
Amit Shah335a64a5c2010-02-24 10:37:44 +05301682 nr_added_bufs = fill_queue(portdev->c_ivq, &portdev->cvq_lock);
1683 if (!nr_added_bufs) {
Amit Shah22a29ea2010-02-12 10:32:17 +05301684 dev_err(&vdev->dev,
1685 "Error allocating buffers for control queue\n");
1686 err = -ENOMEM;
1687 goto free_vqs;
1688 }
Amit Shah1d051602010-05-19 22:15:49 -06001689 } else {
1690 /*
1691 * For backward compatibility: Create a console port
1692 * if we're running on older host.
1693 */
1694 add_port(portdev, 0);
Amit Shah17634ba2009-12-21 21:03:25 +05301695 }
1696
Amit Shah6bdf2af2010-09-02 18:11:49 +05301697 spin_lock_irq(&pdrvdata_lock);
1698 list_add_tail(&portdev->list, &pdrvdata.portdevs);
1699 spin_unlock_irq(&pdrvdata_lock);
1700
Amit Shahf909f852010-05-19 22:15:48 -06001701 __send_control_msg(portdev, VIRTIO_CONSOLE_BAD_ID,
1702 VIRTIO_CONSOLE_DEVICE_READY, 1);
Rusty Russell31610432007-10-22 11:03:39 +10001703 return 0;
1704
Amit Shah22a29ea2010-02-12 10:32:17 +05301705free_vqs:
Julia Lawall0643e4c2010-05-15 11:45:53 +02001706 /* The host might want to notify mgmt sw about device add failure */
1707 __send_control_msg(portdev, VIRTIO_CONSOLE_BAD_ID,
1708 VIRTIO_CONSOLE_DEVICE_READY, 0);
Amit Shah22a29ea2010-02-12 10:32:17 +05301709 vdev->config->del_vqs(vdev);
1710 kfree(portdev->in_vqs);
1711 kfree(portdev->out_vqs);
Amit Shahfb08bd22009-12-21 21:36:04 +05301712free_chrdev:
1713 unregister_chrdev(portdev->chr_major, "virtio-portsdev");
Rusty Russell31610432007-10-22 11:03:39 +10001714free:
Amit Shah1c85bf32010-01-18 19:15:07 +05301715 kfree(portdev);
Rusty Russell31610432007-10-22 11:03:39 +10001716fail:
1717 return err;
1718}
1719
Amit Shah71778762010-02-12 10:32:16 +05301720static void virtcons_remove(struct virtio_device *vdev)
1721{
1722 struct ports_device *portdev;
1723 struct port *port, *port2;
Amit Shah71778762010-02-12 10:32:16 +05301724
1725 portdev = vdev->priv;
1726
Amit Shah6bdf2af2010-09-02 18:11:49 +05301727 spin_lock_irq(&pdrvdata_lock);
1728 list_del(&portdev->list);
1729 spin_unlock_irq(&pdrvdata_lock);
1730
Amit Shah02238952010-09-02 18:11:40 +05301731 /* Disable interrupts for vqs */
1732 vdev->config->reset(vdev);
1733 /* Finish up work that's lined up */
Amit Shah71778762010-02-12 10:32:16 +05301734 cancel_work_sync(&portdev->control_work);
Amit Shah71778762010-02-12 10:32:16 +05301735
1736 list_for_each_entry_safe(port, port2, &portdev->ports, list)
Amit Shahb353a6b2010-09-02 18:38:29 +05301737 unplug_port(port);
Amit Shah71778762010-02-12 10:32:16 +05301738
1739 unregister_chrdev(portdev->chr_major, "virtio-portsdev");
1740
Amit Shahe0620132010-09-02 18:38:30 +05301741 /*
1742 * When yanking out a device, we immediately lose the
1743 * (device-side) queues. So there's no point in keeping the
1744 * guest side around till we drop our final reference. This
1745 * also means that any ports which are in an open state will
1746 * have to just stop using the port, as the vqs are going
1747 * away.
1748 */
Amit Shah96eb8722010-09-02 18:11:41 +05301749 if (use_multiport(portdev)) {
1750 struct port_buffer *buf;
1751 unsigned int len;
Amit Shah71778762010-02-12 10:32:16 +05301752
Amit Shah96eb8722010-09-02 18:11:41 +05301753 while ((buf = virtqueue_get_buf(portdev->c_ivq, &len)))
1754 free_buf(buf);
1755
1756 while ((buf = virtqueue_detach_unused_buf(portdev->c_ivq)))
1757 free_buf(buf);
1758 }
Amit Shah71778762010-02-12 10:32:16 +05301759
1760 vdev->config->del_vqs(vdev);
1761 kfree(portdev->in_vqs);
1762 kfree(portdev->out_vqs);
1763
1764 kfree(portdev);
1765}
1766
Rusty Russell31610432007-10-22 11:03:39 +10001767static struct virtio_device_id id_table[] = {
1768 { VIRTIO_ID_CONSOLE, VIRTIO_DEV_ANY_ID },
1769 { 0 },
1770};
1771
Christian Borntraegerc2983452008-11-25 13:36:26 +01001772static unsigned int features[] = {
1773 VIRTIO_CONSOLE_F_SIZE,
Amit Shahb99fa812010-05-19 22:15:46 -06001774 VIRTIO_CONSOLE_F_MULTIPORT,
Christian Borntraegerc2983452008-11-25 13:36:26 +01001775};
1776
Rusty Russell31610432007-10-22 11:03:39 +10001777static struct virtio_driver virtio_console = {
Christian Borntraegerc2983452008-11-25 13:36:26 +01001778 .feature_table = features,
1779 .feature_table_size = ARRAY_SIZE(features),
Rusty Russell31610432007-10-22 11:03:39 +10001780 .driver.name = KBUILD_MODNAME,
1781 .driver.owner = THIS_MODULE,
1782 .id_table = id_table,
1783 .probe = virtcons_probe,
Amit Shah71778762010-02-12 10:32:16 +05301784 .remove = virtcons_remove,
Amit Shah7f5d8102009-12-21 22:22:08 +05301785 .config_changed = config_intr,
Rusty Russell31610432007-10-22 11:03:39 +10001786};
1787
1788static int __init init(void)
1789{
Amit Shahfb08bd22009-12-21 21:36:04 +05301790 int err;
1791
1792 pdrvdata.class = class_create(THIS_MODULE, "virtio-ports");
1793 if (IS_ERR(pdrvdata.class)) {
1794 err = PTR_ERR(pdrvdata.class);
1795 pr_err("Error %d creating virtio-ports class\n", err);
1796 return err;
1797 }
Amit Shahd99393e2009-12-21 22:36:21 +05301798
1799 pdrvdata.debugfs_dir = debugfs_create_dir("virtio-ports", NULL);
1800 if (!pdrvdata.debugfs_dir) {
1801 pr_warning("Error %ld creating debugfs dir for virtio-ports\n",
1802 PTR_ERR(pdrvdata.debugfs_dir));
1803 }
Amit Shah38edf582010-01-18 19:15:05 +05301804 INIT_LIST_HEAD(&pdrvdata.consoles);
Amit Shah6bdf2af2010-09-02 18:11:49 +05301805 INIT_LIST_HEAD(&pdrvdata.portdevs);
Amit Shah38edf582010-01-18 19:15:05 +05301806
Rusty Russell31610432007-10-22 11:03:39 +10001807 return register_virtio_driver(&virtio_console);
1808}
Amit Shah71778762010-02-12 10:32:16 +05301809
1810static void __exit fini(void)
1811{
1812 unregister_virtio_driver(&virtio_console);
1813
1814 class_destroy(pdrvdata.class);
1815 if (pdrvdata.debugfs_dir)
1816 debugfs_remove_recursive(pdrvdata.debugfs_dir);
1817}
Rusty Russell31610432007-10-22 11:03:39 +10001818module_init(init);
Amit Shah71778762010-02-12 10:32:16 +05301819module_exit(fini);
Rusty Russell31610432007-10-22 11:03:39 +10001820
1821MODULE_DEVICE_TABLE(virtio, id_table);
1822MODULE_DESCRIPTION("Virtio console driver");
1823MODULE_LICENSE("GPL");