blob: 105181c1e6beb848c1fe73f1c6a60a69959ac687 [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>
Amit Shahfb08bd22009-12-21 21:36:04 +053022#include <linux/device.h>
Rusty Russell31610432007-10-22 11:03:39 +100023#include <linux/err.h>
Amit Shaha08fa922011-09-14 13:06:41 +053024#include <linux/freezer.h>
Amit Shah2030fa42009-12-21 21:49:30 +053025#include <linux/fs.h>
Rusty Russell31610432007-10-22 11:03:39 +100026#include <linux/init.h>
Amit Shah38edf582010-01-18 19:15:05 +053027#include <linux/list.h>
Amit Shah2030fa42009-12-21 21:49:30 +053028#include <linux/poll.h>
29#include <linux/sched.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090030#include <linux/slab.h>
Amit Shah38edf582010-01-18 19:15:05 +053031#include <linux/spinlock.h>
Rusty Russell31610432007-10-22 11:03:39 +100032#include <linux/virtio.h>
33#include <linux/virtio_console.h>
Amit Shah2030fa42009-12-21 21:49:30 +053034#include <linux/wait.h>
Amit Shah17634ba2009-12-21 21:03:25 +053035#include <linux/workqueue.h>
Amit Shah51df0ac2011-02-01 09:31:25 +053036#include "../tty/hvc/hvc_console.h"
Rusty Russell31610432007-10-22 11:03:39 +100037
Amit Shah38edf582010-01-18 19:15:05 +053038/*
39 * This is a global struct for storing common data for all the devices
40 * this driver handles.
41 *
42 * Mainly, it has a linked list for all the consoles in one place so
43 * that callbacks from hvc for get_chars(), put_chars() work properly
44 * across multiple devices and multiple ports per device.
45 */
46struct ports_driver_data {
Amit Shahfb08bd22009-12-21 21:36:04 +053047 /* Used for registering chardevs */
48 struct class *class;
49
Amit Shahd99393e2009-12-21 22:36:21 +053050 /* Used for exporting per-port information to debugfs */
51 struct dentry *debugfs_dir;
52
Amit Shah6bdf2af2010-09-02 18:11:49 +053053 /* List of all the devices we're handling */
54 struct list_head portdevs;
55
Amit Shahfb08bd22009-12-21 21:36:04 +053056 /* Number of devices this driver is handling */
57 unsigned int index;
58
Rusty Russelld8a02bd2010-01-18 19:15:06 +053059 /*
60 * This is used to keep track of the number of hvc consoles
61 * spawned by this driver. This number is given as the first
62 * argument to hvc_alloc(). To correctly map an initial
63 * console spawned via hvc_instantiate to the console being
64 * hooked up via hvc_alloc, we need to pass the same vtermno.
65 *
66 * We also just assume the first console being initialised was
67 * the first one that got used as the initial console.
68 */
69 unsigned int next_vtermno;
70
Amit Shah38edf582010-01-18 19:15:05 +053071 /* All the console devices handled by this driver */
72 struct list_head consoles;
73};
74static struct ports_driver_data pdrvdata;
75
76DEFINE_SPINLOCK(pdrvdata_lock);
77
Amit Shah4f23c572010-01-18 19:15:09 +053078/* This struct holds information that's relevant only for console ports */
79struct console {
80 /* We'll place all consoles in a list in the pdrvdata struct */
81 struct list_head list;
82
83 /* The hvc device associated with this console port */
84 struct hvc_struct *hvc;
85
Amit Shah97788292010-05-06 02:05:08 +053086 /* The size of the console */
87 struct winsize ws;
88
Amit Shah4f23c572010-01-18 19:15:09 +053089 /*
90 * This number identifies the number that we used to register
91 * with hvc in hvc_instantiate() and hvc_alloc(); this is the
92 * number passed on by the hvc callbacks to us to
93 * differentiate between the other console ports handled by
94 * this driver
95 */
96 u32 vtermno;
97};
98
Amit Shahfdb9a052010-01-18 19:15:01 +053099struct port_buffer {
100 char *buf;
101
102 /* size of the buffer in *buf above */
103 size_t size;
104
105 /* used length of the buffer */
106 size_t len;
107 /* offset in the buf from which to consume data */
108 size_t offset;
109};
110
Amit Shah17634ba2009-12-21 21:03:25 +0530111/*
112 * This is a per-device struct that stores data common to all the
113 * ports for that device (vdev->priv).
114 */
115struct ports_device {
Amit Shah6bdf2af2010-09-02 18:11:49 +0530116 /* Next portdev in the list, head is in the pdrvdata struct */
117 struct list_head list;
118
Amit Shah17634ba2009-12-21 21:03:25 +0530119 /*
120 * Workqueue handlers where we process deferred work after
121 * notification
122 */
123 struct work_struct control_work;
124
125 struct list_head ports;
126
127 /* To protect the list of ports */
128 spinlock_t ports_lock;
129
130 /* To protect the vq operations for the control channel */
131 spinlock_t cvq_lock;
132
133 /* The current config space is stored here */
Amit Shahb99fa812010-05-19 22:15:46 -0600134 struct virtio_console_config config;
Amit Shah17634ba2009-12-21 21:03:25 +0530135
136 /* The virtio device we're associated with */
137 struct virtio_device *vdev;
138
139 /*
140 * A couple of virtqueues for the control channel: one for
141 * guest->host transfers, one for host->guest transfers
142 */
143 struct virtqueue *c_ivq, *c_ovq;
144
145 /* Array of per-port IO virtqueues */
146 struct virtqueue **in_vqs, **out_vqs;
Amit Shahfb08bd22009-12-21 21:36:04 +0530147
148 /* Used for numbering devices for sysfs and debugfs */
149 unsigned int drv_index;
150
151 /* Major number for this device. Ports will be created as minors. */
152 int chr_major;
Amit Shah17634ba2009-12-21 21:03:25 +0530153};
154
Amit Shah1c85bf32010-01-18 19:15:07 +0530155/* This struct holds the per-port data */
Rusty Russell21206ed2010-01-18 19:15:00 +0530156struct port {
Amit Shah17634ba2009-12-21 21:03:25 +0530157 /* Next port in the list, head is in the ports_device */
158 struct list_head list;
159
Amit Shah1c85bf32010-01-18 19:15:07 +0530160 /* Pointer to the parent virtio_console device */
161 struct ports_device *portdev;
Amit Shahfdb9a052010-01-18 19:15:01 +0530162
163 /* The current buffer from which data has to be fed to readers */
164 struct port_buffer *inbuf;
Rusty Russell31610432007-10-22 11:03:39 +1000165
Amit Shah203baab2010-01-18 19:15:12 +0530166 /*
167 * To protect the operations on the in_vq associated with this
168 * port. Has to be a spinlock because it can be called from
169 * interrupt context (get_char()).
170 */
171 spinlock_t inbuf_lock;
172
Amit Shahcdfadfc2010-05-19 22:15:50 -0600173 /* Protect the operations on the out_vq. */
174 spinlock_t outvq_lock;
175
Amit Shah1c85bf32010-01-18 19:15:07 +0530176 /* The IO vqs for this port */
177 struct virtqueue *in_vq, *out_vq;
178
Amit Shahd99393e2009-12-21 22:36:21 +0530179 /* File in the debugfs directory that exposes this port's information */
180 struct dentry *debugfs_file;
181
Amit Shah4f23c572010-01-18 19:15:09 +0530182 /*
183 * The entries in this struct will be valid if this port is
184 * hooked up to an hvc console
185 */
186 struct console cons;
Amit Shah17634ba2009-12-21 21:03:25 +0530187
Amit Shahfb08bd22009-12-21 21:36:04 +0530188 /* Each port associates with a separate char device */
Amit Shahd22a6982010-09-02 18:20:59 +0530189 struct cdev *cdev;
Amit Shahfb08bd22009-12-21 21:36:04 +0530190 struct device *dev;
191
Amit Shahb353a6b2010-09-02 18:38:29 +0530192 /* Reference-counting to handle port hot-unplugs and file operations */
193 struct kref kref;
194
Amit Shah2030fa42009-12-21 21:49:30 +0530195 /* A waitqueue for poll() or blocking read operations */
196 wait_queue_head_t waitqueue;
197
Amit Shah431edb82009-12-21 21:57:40 +0530198 /* The 'name' of the port that we expose via sysfs properties */
199 char *name;
200
Amit Shah3eae0ad2010-09-02 18:47:52 +0530201 /* We can notify apps of host connect / disconnect events via SIGIO */
202 struct fasync_struct *async_queue;
203
Amit Shah17634ba2009-12-21 21:03:25 +0530204 /* The 'id' to identify the port with the Host */
205 u32 id;
Amit Shah2030fa42009-12-21 21:49:30 +0530206
Amit Shahcdfadfc2010-05-19 22:15:50 -0600207 bool outvq_full;
208
Amit Shah2030fa42009-12-21 21:49:30 +0530209 /* Is the host device open */
210 bool host_connected;
Amit Shah3c7969c2009-11-26 11:25:38 +0530211
212 /* We should allow only one process to open a port */
213 bool guest_connected;
Rusty Russell21206ed2010-01-18 19:15:00 +0530214};
Rusty Russell31610432007-10-22 11:03:39 +1000215
Rusty Russell971f3392010-01-18 19:14:56 +0530216/* This is the very early arch-specified put chars function. */
217static int (*early_put_chars)(u32, const char *, int);
218
Amit Shah38edf582010-01-18 19:15:05 +0530219static struct port *find_port_by_vtermno(u32 vtermno)
220{
221 struct port *port;
Amit Shah4f23c572010-01-18 19:15:09 +0530222 struct console *cons;
Amit Shah38edf582010-01-18 19:15:05 +0530223 unsigned long flags;
224
225 spin_lock_irqsave(&pdrvdata_lock, flags);
Amit Shah4f23c572010-01-18 19:15:09 +0530226 list_for_each_entry(cons, &pdrvdata.consoles, list) {
227 if (cons->vtermno == vtermno) {
228 port = container_of(cons, struct port, cons);
Amit Shah38edf582010-01-18 19:15:05 +0530229 goto out;
Amit Shah4f23c572010-01-18 19:15:09 +0530230 }
Amit Shah38edf582010-01-18 19:15:05 +0530231 }
232 port = NULL;
233out:
234 spin_unlock_irqrestore(&pdrvdata_lock, flags);
235 return port;
236}
237
Amit Shah04950cd2010-09-02 18:20:58 +0530238static struct port *find_port_by_devt_in_portdev(struct ports_device *portdev,
239 dev_t dev)
240{
241 struct port *port;
242 unsigned long flags;
243
244 spin_lock_irqsave(&portdev->ports_lock, flags);
245 list_for_each_entry(port, &portdev->ports, list)
Amit Shahd22a6982010-09-02 18:20:59 +0530246 if (port->cdev->dev == dev)
Amit Shah04950cd2010-09-02 18:20:58 +0530247 goto out;
248 port = NULL;
249out:
250 spin_unlock_irqrestore(&portdev->ports_lock, flags);
251
252 return port;
253}
254
255static struct port *find_port_by_devt(dev_t dev)
256{
257 struct ports_device *portdev;
258 struct port *port;
259 unsigned long flags;
260
261 spin_lock_irqsave(&pdrvdata_lock, flags);
262 list_for_each_entry(portdev, &pdrvdata.portdevs, list) {
263 port = find_port_by_devt_in_portdev(portdev, dev);
264 if (port)
265 goto out;
266 }
267 port = NULL;
268out:
269 spin_unlock_irqrestore(&pdrvdata_lock, flags);
270 return port;
271}
272
Amit Shah17634ba2009-12-21 21:03:25 +0530273static struct port *find_port_by_id(struct ports_device *portdev, u32 id)
274{
275 struct port *port;
276 unsigned long flags;
277
278 spin_lock_irqsave(&portdev->ports_lock, flags);
279 list_for_each_entry(port, &portdev->ports, list)
280 if (port->id == id)
281 goto out;
282 port = NULL;
283out:
284 spin_unlock_irqrestore(&portdev->ports_lock, flags);
285
286 return port;
287}
288
Amit Shah203baab2010-01-18 19:15:12 +0530289static struct port *find_port_by_vq(struct ports_device *portdev,
290 struct virtqueue *vq)
291{
292 struct port *port;
Amit Shah203baab2010-01-18 19:15:12 +0530293 unsigned long flags;
294
Amit Shah17634ba2009-12-21 21:03:25 +0530295 spin_lock_irqsave(&portdev->ports_lock, flags);
296 list_for_each_entry(port, &portdev->ports, list)
Amit Shah203baab2010-01-18 19:15:12 +0530297 if (port->in_vq == vq || port->out_vq == vq)
298 goto out;
Amit Shah203baab2010-01-18 19:15:12 +0530299 port = NULL;
300out:
Amit Shah17634ba2009-12-21 21:03:25 +0530301 spin_unlock_irqrestore(&portdev->ports_lock, flags);
Amit Shah203baab2010-01-18 19:15:12 +0530302 return port;
303}
304
Amit Shah17634ba2009-12-21 21:03:25 +0530305static bool is_console_port(struct port *port)
306{
307 if (port->cons.hvc)
308 return true;
309 return false;
310}
311
312static inline bool use_multiport(struct ports_device *portdev)
313{
314 /*
315 * This condition can be true when put_chars is called from
316 * early_init
317 */
318 if (!portdev->vdev)
319 return 0;
320 return portdev->vdev->features[0] & (1 << VIRTIO_CONSOLE_F_MULTIPORT);
321}
322
Amit Shahfdb9a052010-01-18 19:15:01 +0530323static void free_buf(struct port_buffer *buf)
324{
325 kfree(buf->buf);
326 kfree(buf);
327}
328
329static struct port_buffer *alloc_buf(size_t buf_size)
330{
331 struct port_buffer *buf;
332
333 buf = kmalloc(sizeof(*buf), GFP_KERNEL);
334 if (!buf)
335 goto fail;
336 buf->buf = kzalloc(buf_size, GFP_KERNEL);
337 if (!buf->buf)
338 goto free_buf;
339 buf->len = 0;
340 buf->offset = 0;
341 buf->size = buf_size;
342 return buf;
343
344free_buf:
345 kfree(buf);
346fail:
347 return NULL;
348}
349
Amit Shaha3cde442010-01-18 19:15:03 +0530350/* Callers should take appropriate locks */
Amit Shahdefde662011-09-14 13:06:42 +0530351static struct port_buffer *get_inbuf(struct port *port)
Amit Shaha3cde442010-01-18 19:15:03 +0530352{
353 struct port_buffer *buf;
Amit Shaha3cde442010-01-18 19:15:03 +0530354 unsigned int len;
355
Amit Shahd25a9dd2011-09-14 13:06:43 +0530356 if (port->inbuf)
357 return port->inbuf;
358
359 buf = virtqueue_get_buf(port->in_vq, &len);
Amit Shaha3cde442010-01-18 19:15:03 +0530360 if (buf) {
361 buf->len = len;
362 buf->offset = 0;
363 }
364 return buf;
365}
366
Rusty Russella23ea922010-01-18 19:14:55 +0530367/*
Amit Shahe27b5192010-01-18 19:15:02 +0530368 * Create a scatter-gather list representing our input buffer and put
369 * it in the queue.
370 *
371 * Callers should take appropriate locks.
372 */
Amit Shah203baab2010-01-18 19:15:12 +0530373static int add_inbuf(struct virtqueue *vq, struct port_buffer *buf)
Amit Shahe27b5192010-01-18 19:15:02 +0530374{
375 struct scatterlist sg[1];
Amit Shah203baab2010-01-18 19:15:12 +0530376 int ret;
Amit Shah1c85bf32010-01-18 19:15:07 +0530377
Amit Shahe27b5192010-01-18 19:15:02 +0530378 sg_init_one(sg, buf->buf, buf->size);
379
Michael S. Tsirkin505b0452010-04-12 16:18:32 +0300380 ret = virtqueue_add_buf(vq, sg, 0, 1, buf);
381 virtqueue_kick(vq);
Amit Shah203baab2010-01-18 19:15:12 +0530382 return ret;
383}
384
Amit Shah88f251a2009-12-21 22:15:30 +0530385/* Discard any unread data this port has. Callers lockers. */
386static void discard_port_data(struct port *port)
387{
388 struct port_buffer *buf;
Amit Shah2d24cda2011-09-14 13:06:45 +0530389 unsigned int err;
Amit Shah88f251a2009-12-21 22:15:30 +0530390
Amit Shahd7a62cd2011-03-04 14:04:33 +1030391 if (!port->portdev) {
392 /* Device has been unplugged. vqs are already gone. */
393 return;
394 }
Amit Shah2d24cda2011-09-14 13:06:45 +0530395 buf = get_inbuf(port);
Amit Shah88f251a2009-12-21 22:15:30 +0530396
Amit Shahce072a02011-09-14 13:06:44 +0530397 err = 0;
Amit Shahd6933562010-02-12 10:32:18 +0530398 while (buf) {
Amit Shah2d24cda2011-09-14 13:06:45 +0530399 if (add_inbuf(port->in_vq, buf) < 0) {
Amit Shahce072a02011-09-14 13:06:44 +0530400 err++;
Amit Shahd6933562010-02-12 10:32:18 +0530401 free_buf(buf);
402 }
Amit Shah2d24cda2011-09-14 13:06:45 +0530403 port->inbuf = NULL;
404 buf = get_inbuf(port);
Amit Shah88f251a2009-12-21 22:15:30 +0530405 }
Amit Shahce072a02011-09-14 13:06:44 +0530406 if (err)
Amit Shahd6933562010-02-12 10:32:18 +0530407 dev_warn(port->dev, "Errors adding %d buffers back to vq\n",
Amit Shahce072a02011-09-14 13:06:44 +0530408 err);
Amit Shah88f251a2009-12-21 22:15:30 +0530409}
410
Amit Shah203baab2010-01-18 19:15:12 +0530411static bool port_has_data(struct port *port)
412{
413 unsigned long flags;
414 bool ret;
415
Amit Shahd6933562010-02-12 10:32:18 +0530416 ret = false;
Amit Shahd25a9dd2011-09-14 13:06:43 +0530417 spin_lock_irqsave(&port->inbuf_lock, flags);
418 port->inbuf = get_inbuf(port);
419 if (port->inbuf)
420 ret = true;
421
Amit Shah203baab2010-01-18 19:15:12 +0530422 spin_unlock_irqrestore(&port->inbuf_lock, flags);
Amit Shah203baab2010-01-18 19:15:12 +0530423 return ret;
424}
425
Amit Shah3425e702010-05-19 22:15:46 -0600426static ssize_t __send_control_msg(struct ports_device *portdev, u32 port_id,
427 unsigned int event, unsigned int value)
Amit Shah17634ba2009-12-21 21:03:25 +0530428{
429 struct scatterlist sg[1];
430 struct virtio_console_control cpkt;
431 struct virtqueue *vq;
Amit Shah604b2ad2010-02-24 10:36:51 +0530432 unsigned int len;
Amit Shah17634ba2009-12-21 21:03:25 +0530433
Amit Shah3425e702010-05-19 22:15:46 -0600434 if (!use_multiport(portdev))
Amit Shah17634ba2009-12-21 21:03:25 +0530435 return 0;
436
Amit Shah3425e702010-05-19 22:15:46 -0600437 cpkt.id = port_id;
Amit Shah17634ba2009-12-21 21:03:25 +0530438 cpkt.event = event;
439 cpkt.value = value;
440
Amit Shah3425e702010-05-19 22:15:46 -0600441 vq = portdev->c_ovq;
Amit Shah17634ba2009-12-21 21:03:25 +0530442
443 sg_init_one(sg, &cpkt, sizeof(cpkt));
Michael S. Tsirkin505b0452010-04-12 16:18:32 +0300444 if (virtqueue_add_buf(vq, sg, 1, 0, &cpkt) >= 0) {
445 virtqueue_kick(vq);
446 while (!virtqueue_get_buf(vq, &len))
Amit Shah17634ba2009-12-21 21:03:25 +0530447 cpu_relax();
448 }
449 return 0;
450}
451
Amit Shah3425e702010-05-19 22:15:46 -0600452static ssize_t send_control_msg(struct port *port, unsigned int event,
453 unsigned int value)
454{
Amit Shah84ec06c2010-09-02 18:11:42 +0530455 /* Did the port get unplugged before userspace closed it? */
456 if (port->portdev)
457 return __send_control_msg(port->portdev, port->id, event, value);
458 return 0;
Amit Shah3425e702010-05-19 22:15:46 -0600459}
460
Amit Shahcdfadfc2010-05-19 22:15:50 -0600461/* Callers must take the port->outvq_lock */
462static void reclaim_consumed_buffers(struct port *port)
463{
464 void *buf;
465 unsigned int len;
466
Amit Shahd7a62cd2011-03-04 14:04:33 +1030467 if (!port->portdev) {
468 /* Device has been unplugged. vqs are already gone. */
469 return;
470 }
Amit Shahcdfadfc2010-05-19 22:15:50 -0600471 while ((buf = virtqueue_get_buf(port->out_vq, &len))) {
472 kfree(buf);
473 port->outvq_full = false;
474 }
475}
476
477static ssize_t send_buf(struct port *port, void *in_buf, size_t in_count,
478 bool nonblock)
Amit Shahf997f00b2009-12-21 17:28:51 +0530479{
480 struct scatterlist sg[1];
481 struct virtqueue *out_vq;
482 ssize_t ret;
Amit Shahcdfadfc2010-05-19 22:15:50 -0600483 unsigned long flags;
Amit Shahf997f00b2009-12-21 17:28:51 +0530484 unsigned int len;
485
486 out_vq = port->out_vq;
487
Amit Shahcdfadfc2010-05-19 22:15:50 -0600488 spin_lock_irqsave(&port->outvq_lock, flags);
489
490 reclaim_consumed_buffers(port);
491
Amit Shahf997f00b2009-12-21 17:28:51 +0530492 sg_init_one(sg, in_buf, in_count);
Michael S. Tsirkin505b0452010-04-12 16:18:32 +0300493 ret = virtqueue_add_buf(out_vq, sg, 1, 0, in_buf);
Amit Shahf997f00b2009-12-21 17:28:51 +0530494
495 /* Tell Host to go! */
Michael S. Tsirkin505b0452010-04-12 16:18:32 +0300496 virtqueue_kick(out_vq);
Amit Shahf997f00b2009-12-21 17:28:51 +0530497
498 if (ret < 0) {
Rusty Russell9ff4cfa2010-04-08 09:46:16 -0600499 in_count = 0;
Amit Shahcdfadfc2010-05-19 22:15:50 -0600500 goto done;
Amit Shahf997f00b2009-12-21 17:28:51 +0530501 }
502
Amit Shahcdfadfc2010-05-19 22:15:50 -0600503 if (ret == 0)
504 port->outvq_full = true;
505
506 if (nonblock)
507 goto done;
508
509 /*
510 * Wait till the host acknowledges it pushed out the data we
Amit Shah531295e2010-10-20 13:45:43 +1030511 * sent. This is done for data from the hvc_console; the tty
512 * operations are performed with spinlocks held so we can't
513 * sleep here. An alternative would be to copy the data to a
514 * buffer and relax the spinning requirement. The downside is
515 * we need to kmalloc a GFP_ATOMIC buffer each time the
516 * console driver writes something out.
Amit Shahcdfadfc2010-05-19 22:15:50 -0600517 */
Michael S. Tsirkin505b0452010-04-12 16:18:32 +0300518 while (!virtqueue_get_buf(out_vq, &len))
Amit Shahf997f00b2009-12-21 17:28:51 +0530519 cpu_relax();
Amit Shahcdfadfc2010-05-19 22:15:50 -0600520done:
521 spin_unlock_irqrestore(&port->outvq_lock, flags);
522 /*
523 * We're expected to return the amount of data we wrote -- all
524 * of it
525 */
Rusty Russell9ff4cfa2010-04-08 09:46:16 -0600526 return in_count;
Amit Shahf997f00b2009-12-21 17:28:51 +0530527}
528
Amit Shah203baab2010-01-18 19:15:12 +0530529/*
530 * Give out the data that's requested from the buffer that we have
531 * queued up.
532 */
Amit Shahb766cee2009-12-21 21:26:45 +0530533static ssize_t fill_readbuf(struct port *port, char *out_buf, size_t out_count,
534 bool to_user)
Amit Shah203baab2010-01-18 19:15:12 +0530535{
536 struct port_buffer *buf;
537 unsigned long flags;
538
539 if (!out_count || !port_has_data(port))
540 return 0;
541
542 buf = port->inbuf;
Amit Shahb766cee2009-12-21 21:26:45 +0530543 out_count = min(out_count, buf->len - buf->offset);
Amit Shah203baab2010-01-18 19:15:12 +0530544
Amit Shahb766cee2009-12-21 21:26:45 +0530545 if (to_user) {
546 ssize_t ret;
Amit Shah203baab2010-01-18 19:15:12 +0530547
Amit Shahb766cee2009-12-21 21:26:45 +0530548 ret = copy_to_user(out_buf, buf->buf + buf->offset, out_count);
549 if (ret)
550 return -EFAULT;
551 } else {
552 memcpy(out_buf, buf->buf + buf->offset, out_count);
553 }
554
Amit Shah203baab2010-01-18 19:15:12 +0530555 buf->offset += out_count;
556
557 if (buf->offset == buf->len) {
558 /*
559 * We're done using all the data in this buffer.
560 * Re-queue so that the Host can send us more data.
561 */
562 spin_lock_irqsave(&port->inbuf_lock, flags);
563 port->inbuf = NULL;
564
565 if (add_inbuf(port->in_vq, buf) < 0)
Amit Shahfb08bd22009-12-21 21:36:04 +0530566 dev_warn(port->dev, "failed add_buf\n");
Amit Shah203baab2010-01-18 19:15:12 +0530567
568 spin_unlock_irqrestore(&port->inbuf_lock, flags);
569 }
Amit Shahb766cee2009-12-21 21:26:45 +0530570 /* Return the number of bytes actually copied */
Amit Shah203baab2010-01-18 19:15:12 +0530571 return out_count;
Amit Shahe27b5192010-01-18 19:15:02 +0530572}
573
Amit Shah2030fa42009-12-21 21:49:30 +0530574/* The condition that must be true for polling to end */
Amit Shah60caacd2010-05-19 22:15:49 -0600575static bool will_read_block(struct port *port)
Amit Shah2030fa42009-12-21 21:49:30 +0530576{
Amit Shah3709ea72010-09-02 18:11:43 +0530577 if (!port->guest_connected) {
578 /* Port got hot-unplugged. Let's exit. */
579 return false;
580 }
Amit Shah60caacd2010-05-19 22:15:49 -0600581 return !port_has_data(port) && port->host_connected;
Amit Shah2030fa42009-12-21 21:49:30 +0530582}
583
Amit Shahcdfadfc2010-05-19 22:15:50 -0600584static bool will_write_block(struct port *port)
585{
586 bool ret;
587
Amit Shah60e5e0b2010-05-27 13:24:40 +0530588 if (!port->guest_connected) {
589 /* Port got hot-unplugged. Let's exit. */
590 return false;
591 }
Amit Shahcdfadfc2010-05-19 22:15:50 -0600592 if (!port->host_connected)
593 return true;
594
595 spin_lock_irq(&port->outvq_lock);
596 /*
597 * Check if the Host has consumed any buffers since we last
598 * sent data (this is only applicable for nonblocking ports).
599 */
600 reclaim_consumed_buffers(port);
601 ret = port->outvq_full;
602 spin_unlock_irq(&port->outvq_lock);
603
604 return ret;
605}
606
Amit Shah2030fa42009-12-21 21:49:30 +0530607static ssize_t port_fops_read(struct file *filp, char __user *ubuf,
608 size_t count, loff_t *offp)
609{
610 struct port *port;
611 ssize_t ret;
612
613 port = filp->private_data;
614
615 if (!port_has_data(port)) {
616 /*
617 * If nothing's connected on the host just return 0 in
618 * case of list_empty; this tells the userspace app
619 * that there's no connection
620 */
621 if (!port->host_connected)
622 return 0;
623 if (filp->f_flags & O_NONBLOCK)
624 return -EAGAIN;
625
Amit Shaha08fa922011-09-14 13:06:41 +0530626 ret = wait_event_freezable(port->waitqueue,
627 !will_read_block(port));
Amit Shah2030fa42009-12-21 21:49:30 +0530628 if (ret < 0)
629 return ret;
630 }
Amit Shahb3dddb92010-09-02 18:11:45 +0530631 /* Port got hot-unplugged. */
632 if (!port->guest_connected)
633 return -ENODEV;
Amit Shah2030fa42009-12-21 21:49:30 +0530634 /*
635 * We could've received a disconnection message while we were
636 * waiting for more data.
637 *
638 * This check is not clubbed in the if() statement above as we
639 * might receive some data as well as the host could get
640 * disconnected after we got woken up from our wait. So we
641 * really want to give off whatever data we have and only then
642 * check for host_connected.
643 */
644 if (!port_has_data(port) && !port->host_connected)
645 return 0;
646
647 return fill_readbuf(port, ubuf, count, true);
648}
649
650static ssize_t port_fops_write(struct file *filp, const char __user *ubuf,
651 size_t count, loff_t *offp)
652{
653 struct port *port;
654 char *buf;
655 ssize_t ret;
Amit Shahcdfadfc2010-05-19 22:15:50 -0600656 bool nonblock;
Amit Shah2030fa42009-12-21 21:49:30 +0530657
Amit Shah65745422010-09-14 13:26:16 +0530658 /* Userspace could be out to fool us */
659 if (!count)
660 return 0;
661
Amit Shah2030fa42009-12-21 21:49:30 +0530662 port = filp->private_data;
663
Amit Shahcdfadfc2010-05-19 22:15:50 -0600664 nonblock = filp->f_flags & O_NONBLOCK;
665
666 if (will_write_block(port)) {
667 if (nonblock)
668 return -EAGAIN;
669
Amit Shaha08fa922011-09-14 13:06:41 +0530670 ret = wait_event_freezable(port->waitqueue,
671 !will_write_block(port));
Amit Shahcdfadfc2010-05-19 22:15:50 -0600672 if (ret < 0)
673 return ret;
674 }
Amit Shahf4028112010-09-02 18:11:46 +0530675 /* Port got hot-unplugged. */
676 if (!port->guest_connected)
677 return -ENODEV;
Amit Shahcdfadfc2010-05-19 22:15:50 -0600678
Amit Shah2030fa42009-12-21 21:49:30 +0530679 count = min((size_t)(32 * 1024), count);
680
681 buf = kmalloc(count, GFP_KERNEL);
682 if (!buf)
683 return -ENOMEM;
684
685 ret = copy_from_user(buf, ubuf, count);
686 if (ret) {
687 ret = -EFAULT;
688 goto free_buf;
689 }
690
Amit Shah531295e2010-10-20 13:45:43 +1030691 /*
692 * We now ask send_buf() to not spin for generic ports -- we
693 * can re-use the same code path that non-blocking file
694 * descriptors take for blocking file descriptors since the
695 * wait is already done and we're certain the write will go
696 * through to the host.
697 */
698 nonblock = true;
Amit Shahcdfadfc2010-05-19 22:15:50 -0600699 ret = send_buf(port, buf, count, nonblock);
700
701 if (nonblock && ret > 0)
702 goto out;
703
Amit Shah2030fa42009-12-21 21:49:30 +0530704free_buf:
705 kfree(buf);
Amit Shahcdfadfc2010-05-19 22:15:50 -0600706out:
Amit Shah2030fa42009-12-21 21:49:30 +0530707 return ret;
708}
709
710static unsigned int port_fops_poll(struct file *filp, poll_table *wait)
711{
712 struct port *port;
713 unsigned int ret;
714
715 port = filp->private_data;
716 poll_wait(filp, &port->waitqueue, wait);
717
Amit Shah8529a502010-09-02 18:11:44 +0530718 if (!port->guest_connected) {
719 /* Port got unplugged */
720 return POLLHUP;
721 }
Amit Shah2030fa42009-12-21 21:49:30 +0530722 ret = 0;
Hans de Goede6df7aad2010-09-16 14:43:08 +0530723 if (!will_read_block(port))
Amit Shah2030fa42009-12-21 21:49:30 +0530724 ret |= POLLIN | POLLRDNORM;
Amit Shahcdfadfc2010-05-19 22:15:50 -0600725 if (!will_write_block(port))
Amit Shah2030fa42009-12-21 21:49:30 +0530726 ret |= POLLOUT;
727 if (!port->host_connected)
728 ret |= POLLHUP;
729
730 return ret;
731}
732
Amit Shahb353a6b2010-09-02 18:38:29 +0530733static void remove_port(struct kref *kref);
734
Amit Shah2030fa42009-12-21 21:49:30 +0530735static int port_fops_release(struct inode *inode, struct file *filp)
736{
737 struct port *port;
738
739 port = filp->private_data;
740
741 /* Notify host of port being closed */
742 send_control_msg(port, VIRTIO_CONSOLE_PORT_OPEN, 0);
743
Amit Shah88f251a2009-12-21 22:15:30 +0530744 spin_lock_irq(&port->inbuf_lock);
Amit Shah3c7969c2009-11-26 11:25:38 +0530745 port->guest_connected = false;
746
Amit Shah88f251a2009-12-21 22:15:30 +0530747 discard_port_data(port);
748
749 spin_unlock_irq(&port->inbuf_lock);
750
Amit Shahcdfadfc2010-05-19 22:15:50 -0600751 spin_lock_irq(&port->outvq_lock);
752 reclaim_consumed_buffers(port);
753 spin_unlock_irq(&port->outvq_lock);
754
Amit Shahb353a6b2010-09-02 18:38:29 +0530755 /*
756 * Locks aren't necessary here as a port can't be opened after
757 * unplug, and if a port isn't unplugged, a kref would already
758 * exist for the port. Plus, taking ports_lock here would
759 * create a dependency on other locks taken by functions
760 * inside remove_port if we're the last holder of the port,
761 * creating many problems.
762 */
763 kref_put(&port->kref, remove_port);
764
Amit Shah2030fa42009-12-21 21:49:30 +0530765 return 0;
766}
767
768static int port_fops_open(struct inode *inode, struct file *filp)
769{
770 struct cdev *cdev = inode->i_cdev;
771 struct port *port;
Amit Shah8ad37e82010-09-02 18:11:48 +0530772 int ret;
Amit Shah2030fa42009-12-21 21:49:30 +0530773
Amit Shah04950cd2010-09-02 18:20:58 +0530774 port = find_port_by_devt(cdev->dev);
Amit Shah2030fa42009-12-21 21:49:30 +0530775 filp->private_data = port;
776
Amit Shahb353a6b2010-09-02 18:38:29 +0530777 /* Prevent against a port getting hot-unplugged at the same time */
778 spin_lock_irq(&port->portdev->ports_lock);
779 kref_get(&port->kref);
780 spin_unlock_irq(&port->portdev->ports_lock);
781
Amit Shah2030fa42009-12-21 21:49:30 +0530782 /*
783 * Don't allow opening of console port devices -- that's done
784 * via /dev/hvc
785 */
Amit Shah8ad37e82010-09-02 18:11:48 +0530786 if (is_console_port(port)) {
787 ret = -ENXIO;
788 goto out;
789 }
Amit Shah2030fa42009-12-21 21:49:30 +0530790
Amit Shah3c7969c2009-11-26 11:25:38 +0530791 /* Allow only one process to open a particular port at a time */
792 spin_lock_irq(&port->inbuf_lock);
793 if (port->guest_connected) {
794 spin_unlock_irq(&port->inbuf_lock);
Amit Shah8ad37e82010-09-02 18:11:48 +0530795 ret = -EMFILE;
796 goto out;
Amit Shah3c7969c2009-11-26 11:25:38 +0530797 }
798
799 port->guest_connected = true;
800 spin_unlock_irq(&port->inbuf_lock);
801
Amit Shahcdfadfc2010-05-19 22:15:50 -0600802 spin_lock_irq(&port->outvq_lock);
803 /*
804 * There might be a chance that we missed reclaiming a few
805 * buffers in the window of the port getting previously closed
806 * and opening now.
807 */
808 reclaim_consumed_buffers(port);
809 spin_unlock_irq(&port->outvq_lock);
810
Amit Shah299fb612010-09-16 14:43:09 +0530811 nonseekable_open(inode, filp);
812
Amit Shah2030fa42009-12-21 21:49:30 +0530813 /* Notify host of port being opened */
814 send_control_msg(filp->private_data, VIRTIO_CONSOLE_PORT_OPEN, 1);
815
816 return 0;
Amit Shah8ad37e82010-09-02 18:11:48 +0530817out:
Amit Shahb353a6b2010-09-02 18:38:29 +0530818 kref_put(&port->kref, remove_port);
Amit Shah8ad37e82010-09-02 18:11:48 +0530819 return ret;
Amit Shah2030fa42009-12-21 21:49:30 +0530820}
821
Amit Shah3eae0ad2010-09-02 18:47:52 +0530822static int port_fops_fasync(int fd, struct file *filp, int mode)
823{
824 struct port *port;
825
826 port = filp->private_data;
827 return fasync_helper(fd, filp, mode, &port->async_queue);
828}
829
Amit Shah2030fa42009-12-21 21:49:30 +0530830/*
831 * The file operations that we support: programs in the guest can open
832 * a console device, read from it, write to it, poll for data and
833 * close it. The devices are at
834 * /dev/vport<device number>p<port number>
835 */
836static const struct file_operations port_fops = {
837 .owner = THIS_MODULE,
838 .open = port_fops_open,
839 .read = port_fops_read,
840 .write = port_fops_write,
841 .poll = port_fops_poll,
842 .release = port_fops_release,
Amit Shah3eae0ad2010-09-02 18:47:52 +0530843 .fasync = port_fops_fasync,
Amit Shah299fb612010-09-16 14:43:09 +0530844 .llseek = no_llseek,
Amit Shah2030fa42009-12-21 21:49:30 +0530845};
846
Amit Shahe27b5192010-01-18 19:15:02 +0530847/*
Rusty Russella23ea922010-01-18 19:14:55 +0530848 * The put_chars() callback is pretty straightforward.
Rusty Russell31610432007-10-22 11:03:39 +1000849 *
Rusty Russella23ea922010-01-18 19:14:55 +0530850 * We turn the characters into a scatter-gather list, add it to the
851 * output queue and then kick the Host. Then we sit here waiting for
852 * it to finish: inefficient in theory, but in practice
853 * implementations will do it immediately (lguest's Launcher does).
854 */
Rusty Russell31610432007-10-22 11:03:39 +1000855static int put_chars(u32 vtermno, const char *buf, int count)
856{
Rusty Russell21206ed2010-01-18 19:15:00 +0530857 struct port *port;
Amit Shah38edf582010-01-18 19:15:05 +0530858
François Diakhaté162a6892010-03-23 18:23:15 +0530859 if (unlikely(early_put_chars))
860 return early_put_chars(vtermno, buf, count);
861
Amit Shah38edf582010-01-18 19:15:05 +0530862 port = find_port_by_vtermno(vtermno);
863 if (!port)
Amit Shah6dc69f972010-05-19 22:15:47 -0600864 return -EPIPE;
Rusty Russell31610432007-10-22 11:03:39 +1000865
Amit Shahcdfadfc2010-05-19 22:15:50 -0600866 return send_buf(port, (void *)buf, count, false);
Rusty Russell31610432007-10-22 11:03:39 +1000867}
868
Rusty Russella23ea922010-01-18 19:14:55 +0530869/*
Rusty Russella23ea922010-01-18 19:14:55 +0530870 * get_chars() is the callback from the hvc_console infrastructure
871 * when an interrupt is received.
Rusty Russell31610432007-10-22 11:03:39 +1000872 *
Amit Shah203baab2010-01-18 19:15:12 +0530873 * We call out to fill_readbuf that gets us the required data from the
874 * buffers that are queued up.
Rusty Russella23ea922010-01-18 19:14:55 +0530875 */
Rusty Russell31610432007-10-22 11:03:39 +1000876static int get_chars(u32 vtermno, char *buf, int count)
877{
Rusty Russell21206ed2010-01-18 19:15:00 +0530878 struct port *port;
Rusty Russell31610432007-10-22 11:03:39 +1000879
Amit Shah6dc69f972010-05-19 22:15:47 -0600880 /* If we've not set up the port yet, we have no input to give. */
881 if (unlikely(early_put_chars))
882 return 0;
883
Amit Shah38edf582010-01-18 19:15:05 +0530884 port = find_port_by_vtermno(vtermno);
885 if (!port)
Amit Shah6dc69f972010-05-19 22:15:47 -0600886 return -EPIPE;
Rusty Russell21206ed2010-01-18 19:15:00 +0530887
888 /* If we don't have an input queue yet, we can't get input. */
889 BUG_ON(!port->in_vq);
890
Amit Shahb766cee2009-12-21 21:26:45 +0530891 return fill_readbuf(port, buf, count, false);
Rusty Russell31610432007-10-22 11:03:39 +1000892}
Rusty Russell31610432007-10-22 11:03:39 +1000893
Amit Shahcb06e362010-01-18 19:15:08 +0530894static void resize_console(struct port *port)
Christian Borntraegerc2983452008-11-25 13:36:26 +0100895{
Amit Shahcb06e362010-01-18 19:15:08 +0530896 struct virtio_device *vdev;
Christian Borntraegerc2983452008-11-25 13:36:26 +0100897
Amit Shah2de16a42010-03-19 17:36:44 +0530898 /* The port could have been hot-unplugged */
Amit Shah97788292010-05-06 02:05:08 +0530899 if (!port || !is_console_port(port))
Amit Shah2de16a42010-03-19 17:36:44 +0530900 return;
901
Amit Shahcb06e362010-01-18 19:15:08 +0530902 vdev = port->portdev->vdev;
Amit Shah97788292010-05-06 02:05:08 +0530903 if (virtio_has_feature(vdev, VIRTIO_CONSOLE_F_SIZE))
904 hvc_resize(port->cons.hvc, port->cons.ws);
Christian Borntraegerc2983452008-11-25 13:36:26 +0100905}
906
Amit Shah38edf582010-01-18 19:15:05 +0530907/* We set the configuration at this point, since we now have a tty */
Christian Borntraeger91fcad12008-06-20 15:24:15 +0200908static int notifier_add_vio(struct hvc_struct *hp, int data)
909{
Amit Shah38edf582010-01-18 19:15:05 +0530910 struct port *port;
911
912 port = find_port_by_vtermno(hp->vtermno);
913 if (!port)
914 return -EINVAL;
915
Christian Borntraeger91fcad12008-06-20 15:24:15 +0200916 hp->irq_requested = 1;
Amit Shahcb06e362010-01-18 19:15:08 +0530917 resize_console(port);
Christian Borntraegerc2983452008-11-25 13:36:26 +0100918
Christian Borntraeger91fcad12008-06-20 15:24:15 +0200919 return 0;
920}
921
922static void notifier_del_vio(struct hvc_struct *hp, int data)
923{
924 hp->irq_requested = 0;
925}
926
Amit Shah17634ba2009-12-21 21:03:25 +0530927/* The operations for console ports. */
Rusty Russell1dff3992009-11-28 12:20:26 +0530928static const struct hv_ops hv_ops = {
Rusty Russell971f3392010-01-18 19:14:56 +0530929 .get_chars = get_chars,
930 .put_chars = put_chars,
931 .notifier_add = notifier_add_vio,
932 .notifier_del = notifier_del_vio,
933 .notifier_hangup = notifier_del_vio,
934};
935
936/*
937 * Console drivers are initialized very early so boot messages can go
938 * out, so we do things slightly differently from the generic virtio
939 * initialization of the net and block drivers.
940 *
941 * At this stage, the console is output-only. It's too early to set
942 * up a virtqueue, so we let the drivers do some boutique early-output
943 * thing.
944 */
945int __init virtio_cons_early_init(int (*put_chars)(u32, const char *, int))
946{
947 early_put_chars = put_chars;
948 return hvc_instantiate(0, 0, &hv_ops);
949}
950
Amit Shah17634ba2009-12-21 21:03:25 +0530951int init_port_console(struct port *port)
Amit Shahcfa6d372010-01-18 19:15:10 +0530952{
953 int ret;
954
955 /*
956 * The Host's telling us this port is a console port. Hook it
957 * up with an hvc console.
958 *
959 * To set up and manage our virtual console, we call
960 * hvc_alloc().
961 *
962 * The first argument of hvc_alloc() is the virtual console
963 * number. The second argument is the parameter for the
964 * notification mechanism (like irq number). We currently
965 * leave this as zero, virtqueues have implicit notifications.
966 *
967 * The third argument is a "struct hv_ops" containing the
968 * put_chars() get_chars(), notifier_add() and notifier_del()
969 * pointers. The final argument is the output buffer size: we
970 * can do any size, so we put PAGE_SIZE here.
971 */
972 port->cons.vtermno = pdrvdata.next_vtermno;
973
974 port->cons.hvc = hvc_alloc(port->cons.vtermno, 0, &hv_ops, PAGE_SIZE);
975 if (IS_ERR(port->cons.hvc)) {
976 ret = PTR_ERR(port->cons.hvc);
Amit Shah298add72010-01-18 16:35:23 +0530977 dev_err(port->dev,
978 "error %d allocating hvc for port\n", ret);
Amit Shahcfa6d372010-01-18 19:15:10 +0530979 port->cons.hvc = NULL;
980 return ret;
981 }
982 spin_lock_irq(&pdrvdata_lock);
983 pdrvdata.next_vtermno++;
984 list_add_tail(&port->cons.list, &pdrvdata.consoles);
985 spin_unlock_irq(&pdrvdata_lock);
Amit Shah3c7969c2009-11-26 11:25:38 +0530986 port->guest_connected = true;
Amit Shahcfa6d372010-01-18 19:15:10 +0530987
Amit Shah1d051602010-05-19 22:15:49 -0600988 /*
989 * Start using the new console output if this is the first
990 * console to come up.
991 */
992 if (early_put_chars)
993 early_put_chars = NULL;
994
Amit Shah2030fa42009-12-21 21:49:30 +0530995 /* Notify host of port being opened */
996 send_control_msg(port, VIRTIO_CONSOLE_PORT_OPEN, 1);
997
Amit Shahcfa6d372010-01-18 19:15:10 +0530998 return 0;
999}
1000
Amit Shah431edb82009-12-21 21:57:40 +05301001static ssize_t show_port_name(struct device *dev,
1002 struct device_attribute *attr, char *buffer)
1003{
1004 struct port *port;
1005
1006 port = dev_get_drvdata(dev);
1007
1008 return sprintf(buffer, "%s\n", port->name);
1009}
1010
1011static DEVICE_ATTR(name, S_IRUGO, show_port_name, NULL);
1012
1013static struct attribute *port_sysfs_entries[] = {
1014 &dev_attr_name.attr,
1015 NULL
1016};
1017
1018static struct attribute_group port_attribute_group = {
1019 .name = NULL, /* put in device directory */
1020 .attrs = port_sysfs_entries,
1021};
1022
Amit Shahd99393e2009-12-21 22:36:21 +05301023static int debugfs_open(struct inode *inode, struct file *filp)
1024{
1025 filp->private_data = inode->i_private;
1026 return 0;
1027}
1028
1029static ssize_t debugfs_read(struct file *filp, char __user *ubuf,
1030 size_t count, loff_t *offp)
1031{
1032 struct port *port;
1033 char *buf;
1034 ssize_t ret, out_offset, out_count;
1035
1036 out_count = 1024;
1037 buf = kmalloc(out_count, GFP_KERNEL);
1038 if (!buf)
1039 return -ENOMEM;
1040
1041 port = filp->private_data;
1042 out_offset = 0;
1043 out_offset += snprintf(buf + out_offset, out_count,
1044 "name: %s\n", port->name ? port->name : "");
1045 out_offset += snprintf(buf + out_offset, out_count - out_offset,
1046 "guest_connected: %d\n", port->guest_connected);
1047 out_offset += snprintf(buf + out_offset, out_count - out_offset,
1048 "host_connected: %d\n", port->host_connected);
1049 out_offset += snprintf(buf + out_offset, out_count - out_offset,
Amit Shahcdfadfc2010-05-19 22:15:50 -06001050 "outvq_full: %d\n", port->outvq_full);
1051 out_offset += snprintf(buf + out_offset, out_count - out_offset,
Amit Shahd99393e2009-12-21 22:36:21 +05301052 "is_console: %s\n",
1053 is_console_port(port) ? "yes" : "no");
1054 out_offset += snprintf(buf + out_offset, out_count - out_offset,
1055 "console_vtermno: %u\n", port->cons.vtermno);
1056
1057 ret = simple_read_from_buffer(ubuf, count, offp, buf, out_offset);
1058 kfree(buf);
1059 return ret;
1060}
1061
1062static const struct file_operations port_debugfs_ops = {
1063 .owner = THIS_MODULE,
1064 .open = debugfs_open,
1065 .read = debugfs_read,
1066};
1067
Amit Shah97788292010-05-06 02:05:08 +05301068static void set_console_size(struct port *port, u16 rows, u16 cols)
1069{
1070 if (!port || !is_console_port(port))
1071 return;
1072
1073 port->cons.ws.ws_row = rows;
1074 port->cons.ws.ws_col = cols;
1075}
1076
Amit Shahc446f8f2010-05-19 22:15:48 -06001077static unsigned int fill_queue(struct virtqueue *vq, spinlock_t *lock)
1078{
1079 struct port_buffer *buf;
1080 unsigned int nr_added_bufs;
1081 int ret;
1082
1083 nr_added_bufs = 0;
1084 do {
1085 buf = alloc_buf(PAGE_SIZE);
1086 if (!buf)
1087 break;
1088
1089 spin_lock_irq(lock);
1090 ret = add_inbuf(vq, buf);
1091 if (ret < 0) {
1092 spin_unlock_irq(lock);
1093 free_buf(buf);
1094 break;
1095 }
1096 nr_added_bufs++;
1097 spin_unlock_irq(lock);
1098 } while (ret > 0);
1099
1100 return nr_added_bufs;
1101}
1102
Amit Shah3eae0ad2010-09-02 18:47:52 +05301103static void send_sigio_to_port(struct port *port)
1104{
1105 if (port->async_queue && port->guest_connected)
1106 kill_fasync(&port->async_queue, SIGIO, POLL_OUT);
1107}
1108
Amit Shahc446f8f2010-05-19 22:15:48 -06001109static int add_port(struct ports_device *portdev, u32 id)
1110{
1111 char debugfs_name[16];
1112 struct port *port;
1113 struct port_buffer *buf;
1114 dev_t devt;
1115 unsigned int nr_added_bufs;
1116 int err;
1117
1118 port = kmalloc(sizeof(*port), GFP_KERNEL);
1119 if (!port) {
1120 err = -ENOMEM;
1121 goto fail;
1122 }
Amit Shahb353a6b2010-09-02 18:38:29 +05301123 kref_init(&port->kref);
Amit Shahc446f8f2010-05-19 22:15:48 -06001124
1125 port->portdev = portdev;
1126 port->id = id;
1127
1128 port->name = NULL;
1129 port->inbuf = NULL;
1130 port->cons.hvc = NULL;
Amit Shah3eae0ad2010-09-02 18:47:52 +05301131 port->async_queue = NULL;
Amit Shahc446f8f2010-05-19 22:15:48 -06001132
Amit Shah97788292010-05-06 02:05:08 +05301133 port->cons.ws.ws_row = port->cons.ws.ws_col = 0;
1134
Amit Shahc446f8f2010-05-19 22:15:48 -06001135 port->host_connected = port->guest_connected = false;
1136
Amit Shahcdfadfc2010-05-19 22:15:50 -06001137 port->outvq_full = false;
1138
Amit Shahc446f8f2010-05-19 22:15:48 -06001139 port->in_vq = portdev->in_vqs[port->id];
1140 port->out_vq = portdev->out_vqs[port->id];
1141
Amit Shahd22a6982010-09-02 18:20:59 +05301142 port->cdev = cdev_alloc();
1143 if (!port->cdev) {
1144 dev_err(&port->portdev->vdev->dev, "Error allocating cdev\n");
1145 err = -ENOMEM;
1146 goto free_port;
1147 }
1148 port->cdev->ops = &port_fops;
Amit Shahc446f8f2010-05-19 22:15:48 -06001149
1150 devt = MKDEV(portdev->chr_major, id);
Amit Shahd22a6982010-09-02 18:20:59 +05301151 err = cdev_add(port->cdev, devt, 1);
Amit Shahc446f8f2010-05-19 22:15:48 -06001152 if (err < 0) {
1153 dev_err(&port->portdev->vdev->dev,
1154 "Error %d adding cdev for port %u\n", err, id);
Amit Shahd22a6982010-09-02 18:20:59 +05301155 goto free_cdev;
Amit Shahc446f8f2010-05-19 22:15:48 -06001156 }
1157 port->dev = device_create(pdrvdata.class, &port->portdev->vdev->dev,
1158 devt, port, "vport%up%u",
1159 port->portdev->drv_index, id);
1160 if (IS_ERR(port->dev)) {
1161 err = PTR_ERR(port->dev);
1162 dev_err(&port->portdev->vdev->dev,
1163 "Error %d creating device for port %u\n",
1164 err, id);
1165 goto free_cdev;
1166 }
1167
1168 spin_lock_init(&port->inbuf_lock);
Amit Shahcdfadfc2010-05-19 22:15:50 -06001169 spin_lock_init(&port->outvq_lock);
Amit Shahc446f8f2010-05-19 22:15:48 -06001170 init_waitqueue_head(&port->waitqueue);
1171
1172 /* Fill the in_vq with buffers so the host can send us data. */
1173 nr_added_bufs = fill_queue(port->in_vq, &port->inbuf_lock);
1174 if (!nr_added_bufs) {
1175 dev_err(port->dev, "Error allocating inbufs\n");
1176 err = -ENOMEM;
1177 goto free_device;
1178 }
1179
1180 /*
1181 * If we're not using multiport support, this has to be a console port
1182 */
1183 if (!use_multiport(port->portdev)) {
1184 err = init_port_console(port);
1185 if (err)
1186 goto free_inbufs;
1187 }
1188
1189 spin_lock_irq(&portdev->ports_lock);
1190 list_add_tail(&port->list, &port->portdev->ports);
1191 spin_unlock_irq(&portdev->ports_lock);
1192
1193 /*
1194 * Tell the Host we're set so that it can send us various
1195 * configuration parameters for this port (eg, port name,
1196 * caching, whether this is a console port, etc.)
1197 */
1198 send_control_msg(port, VIRTIO_CONSOLE_PORT_READY, 1);
1199
1200 if (pdrvdata.debugfs_dir) {
1201 /*
1202 * Finally, create the debugfs file that we can use to
1203 * inspect a port's state at any time
1204 */
1205 sprintf(debugfs_name, "vport%up%u",
1206 port->portdev->drv_index, id);
1207 port->debugfs_file = debugfs_create_file(debugfs_name, 0444,
1208 pdrvdata.debugfs_dir,
1209 port,
1210 &port_debugfs_ops);
1211 }
1212 return 0;
1213
1214free_inbufs:
1215 while ((buf = virtqueue_detach_unused_buf(port->in_vq)))
1216 free_buf(buf);
1217free_device:
1218 device_destroy(pdrvdata.class, port->dev->devt);
1219free_cdev:
Amit Shahd22a6982010-09-02 18:20:59 +05301220 cdev_del(port->cdev);
Amit Shahc446f8f2010-05-19 22:15:48 -06001221free_port:
1222 kfree(port);
1223fail:
1224 /* The host might want to notify management sw about port add failure */
Julia Lawall0643e4c2010-05-15 11:45:53 +02001225 __send_control_msg(portdev, id, VIRTIO_CONSOLE_PORT_READY, 0);
Amit Shahc446f8f2010-05-19 22:15:48 -06001226 return err;
1227}
1228
Amit Shahb353a6b2010-09-02 18:38:29 +05301229/* No users remain, remove all port-specific data. */
1230static void remove_port(struct kref *kref)
1231{
1232 struct port *port;
1233
1234 port = container_of(kref, struct port, kref);
1235
1236 sysfs_remove_group(&port->dev->kobj, &port_attribute_group);
1237 device_destroy(pdrvdata.class, port->dev->devt);
1238 cdev_del(port->cdev);
1239
1240 kfree(port->name);
1241
1242 debugfs_remove(port->debugfs_file);
1243
1244 kfree(port);
1245}
1246
1247/*
1248 * Port got unplugged. Remove port from portdev's list and drop the
1249 * kref reference. If no userspace has this port opened, it will
1250 * result in immediate removal the port.
1251 */
1252static void unplug_port(struct port *port)
Amit Shah1f7aa422009-12-21 22:27:31 +05301253{
Amit Shaha9cdd482010-02-12 10:32:15 +05301254 struct port_buffer *buf;
1255
Amit Shahb353a6b2010-09-02 18:38:29 +05301256 spin_lock_irq(&port->portdev->ports_lock);
1257 list_del(&port->list);
1258 spin_unlock_irq(&port->portdev->ports_lock);
1259
Amit Shah00476342010-05-27 13:24:39 +05301260 if (port->guest_connected) {
1261 port->guest_connected = false;
1262 port->host_connected = false;
1263 wake_up_interruptible(&port->waitqueue);
Amit Shaha461e112010-09-02 18:47:54 +05301264
1265 /* Let the app know the port is going down. */
1266 send_sigio_to_port(port);
Amit Shah00476342010-05-27 13:24:39 +05301267 }
1268
Amit Shah1f7aa422009-12-21 22:27:31 +05301269 if (is_console_port(port)) {
1270 spin_lock_irq(&pdrvdata_lock);
1271 list_del(&port->cons.list);
1272 spin_unlock_irq(&pdrvdata_lock);
1273 hvc_remove(port->cons.hvc);
1274 }
Amit Shah1f7aa422009-12-21 22:27:31 +05301275
Amit Shaha9cdd482010-02-12 10:32:15 +05301276 /* Remove unused data this port might have received. */
Amit Shah1f7aa422009-12-21 22:27:31 +05301277 discard_port_data(port);
Amit Shaha9cdd482010-02-12 10:32:15 +05301278
Amit Shahcdfadfc2010-05-19 22:15:50 -06001279 reclaim_consumed_buffers(port);
1280
Amit Shaha9cdd482010-02-12 10:32:15 +05301281 /* Remove buffers we queued up for the Host to send us data in. */
Michael S. Tsirkin505b0452010-04-12 16:18:32 +03001282 while ((buf = virtqueue_detach_unused_buf(port->in_vq)))
Amit Shaha9cdd482010-02-12 10:32:15 +05301283 free_buf(buf);
1284
Amit Shahb353a6b2010-09-02 18:38:29 +05301285 /*
1286 * We should just assume the device itself has gone off --
1287 * else a close on an open port later will try to send out a
1288 * control message.
1289 */
1290 port->portdev = NULL;
Amit Shah1f7aa422009-12-21 22:27:31 +05301291
Amit Shahb353a6b2010-09-02 18:38:29 +05301292 /*
1293 * Locks around here are not necessary - a port can't be
1294 * opened after we removed the port struct from ports_list
1295 * above.
1296 */
1297 kref_put(&port->kref, remove_port);
Amit Shah1f7aa422009-12-21 22:27:31 +05301298}
1299
Amit Shah17634ba2009-12-21 21:03:25 +05301300/* Any private messages that the Host and Guest want to share */
1301static void handle_control_message(struct ports_device *portdev,
1302 struct port_buffer *buf)
1303{
1304 struct virtio_console_control *cpkt;
1305 struct port *port;
Amit Shah431edb82009-12-21 21:57:40 +05301306 size_t name_size;
1307 int err;
Amit Shah17634ba2009-12-21 21:03:25 +05301308
1309 cpkt = (struct virtio_console_control *)(buf->buf + buf->offset);
1310
1311 port = find_port_by_id(portdev, cpkt->id);
Amit Shahf909f852010-05-19 22:15:48 -06001312 if (!port && cpkt->event != VIRTIO_CONSOLE_PORT_ADD) {
Amit Shah17634ba2009-12-21 21:03:25 +05301313 /* No valid header at start of buffer. Drop it. */
1314 dev_dbg(&portdev->vdev->dev,
1315 "Invalid index %u in control packet\n", cpkt->id);
1316 return;
1317 }
1318
1319 switch (cpkt->event) {
Amit Shahf909f852010-05-19 22:15:48 -06001320 case VIRTIO_CONSOLE_PORT_ADD:
1321 if (port) {
Amit Shah1d051602010-05-19 22:15:49 -06001322 dev_dbg(&portdev->vdev->dev,
1323 "Port %u already added\n", port->id);
Amit Shahf909f852010-05-19 22:15:48 -06001324 send_control_msg(port, VIRTIO_CONSOLE_PORT_READY, 1);
1325 break;
1326 }
1327 if (cpkt->id >= portdev->config.max_nr_ports) {
1328 dev_warn(&portdev->vdev->dev,
1329 "Request for adding port with out-of-bound id %u, max. supported id: %u\n",
1330 cpkt->id, portdev->config.max_nr_ports - 1);
1331 break;
1332 }
1333 add_port(portdev, cpkt->id);
1334 break;
1335 case VIRTIO_CONSOLE_PORT_REMOVE:
Amit Shahb353a6b2010-09-02 18:38:29 +05301336 unplug_port(port);
Amit Shahf909f852010-05-19 22:15:48 -06001337 break;
Amit Shah17634ba2009-12-21 21:03:25 +05301338 case VIRTIO_CONSOLE_CONSOLE_PORT:
1339 if (!cpkt->value)
1340 break;
1341 if (is_console_port(port))
1342 break;
1343
1344 init_port_console(port);
1345 /*
1346 * Could remove the port here in case init fails - but
1347 * have to notify the host first.
1348 */
1349 break;
Amit Shah8345adb2010-05-06 02:05:09 +05301350 case VIRTIO_CONSOLE_RESIZE: {
1351 struct {
1352 __u16 rows;
1353 __u16 cols;
1354 } size;
1355
Amit Shah17634ba2009-12-21 21:03:25 +05301356 if (!is_console_port(port))
1357 break;
Amit Shah8345adb2010-05-06 02:05:09 +05301358
1359 memcpy(&size, buf->buf + buf->offset + sizeof(*cpkt),
1360 sizeof(size));
1361 set_console_size(port, size.rows, size.cols);
1362
Amit Shah17634ba2009-12-21 21:03:25 +05301363 port->cons.hvc->irq_requested = 1;
1364 resize_console(port);
1365 break;
Amit Shah8345adb2010-05-06 02:05:09 +05301366 }
Amit Shah2030fa42009-12-21 21:49:30 +05301367 case VIRTIO_CONSOLE_PORT_OPEN:
1368 port->host_connected = cpkt->value;
1369 wake_up_interruptible(&port->waitqueue);
Amit Shahcdfadfc2010-05-19 22:15:50 -06001370 /*
1371 * If the host port got closed and the host had any
1372 * unconsumed buffers, we'll be able to reclaim them
1373 * now.
1374 */
1375 spin_lock_irq(&port->outvq_lock);
1376 reclaim_consumed_buffers(port);
1377 spin_unlock_irq(&port->outvq_lock);
Amit Shah3eae0ad2010-09-02 18:47:52 +05301378
1379 /*
1380 * If the guest is connected, it'll be interested in
1381 * knowing the host connection state changed.
1382 */
1383 send_sigio_to_port(port);
Amit Shah2030fa42009-12-21 21:49:30 +05301384 break;
Amit Shah431edb82009-12-21 21:57:40 +05301385 case VIRTIO_CONSOLE_PORT_NAME:
1386 /*
Amit Shah291024e2011-09-14 13:06:40 +05301387 * If we woke up after hibernation, we can get this
1388 * again. Skip it in that case.
1389 */
1390 if (port->name)
1391 break;
1392
1393 /*
Amit Shah431edb82009-12-21 21:57:40 +05301394 * Skip the size of the header and the cpkt to get the size
1395 * of the name that was sent
1396 */
1397 name_size = buf->len - buf->offset - sizeof(*cpkt) + 1;
1398
1399 port->name = kmalloc(name_size, GFP_KERNEL);
1400 if (!port->name) {
1401 dev_err(port->dev,
1402 "Not enough space to store port name\n");
1403 break;
1404 }
1405 strncpy(port->name, buf->buf + buf->offset + sizeof(*cpkt),
1406 name_size - 1);
1407 port->name[name_size - 1] = 0;
1408
1409 /*
1410 * Since we only have one sysfs attribute, 'name',
1411 * create it only if we have a name for the port.
1412 */
1413 err = sysfs_create_group(&port->dev->kobj,
1414 &port_attribute_group);
Amit Shahec642132010-03-19 17:36:43 +05301415 if (err) {
Amit Shah431edb82009-12-21 21:57:40 +05301416 dev_err(port->dev,
1417 "Error %d creating sysfs device attributes\n",
1418 err);
Amit Shahec642132010-03-19 17:36:43 +05301419 } else {
1420 /*
1421 * Generate a udev event so that appropriate
1422 * symlinks can be created based on udev
1423 * rules.
1424 */
1425 kobject_uevent(&port->dev->kobj, KOBJ_CHANGE);
1426 }
Amit Shah431edb82009-12-21 21:57:40 +05301427 break;
Amit Shah17634ba2009-12-21 21:03:25 +05301428 }
1429}
1430
1431static void control_work_handler(struct work_struct *work)
1432{
1433 struct ports_device *portdev;
1434 struct virtqueue *vq;
1435 struct port_buffer *buf;
1436 unsigned int len;
1437
1438 portdev = container_of(work, struct ports_device, control_work);
1439 vq = portdev->c_ivq;
1440
1441 spin_lock(&portdev->cvq_lock);
Michael S. Tsirkin505b0452010-04-12 16:18:32 +03001442 while ((buf = virtqueue_get_buf(vq, &len))) {
Amit Shah17634ba2009-12-21 21:03:25 +05301443 spin_unlock(&portdev->cvq_lock);
1444
1445 buf->len = len;
1446 buf->offset = 0;
1447
1448 handle_control_message(portdev, buf);
1449
1450 spin_lock(&portdev->cvq_lock);
1451 if (add_inbuf(portdev->c_ivq, buf) < 0) {
1452 dev_warn(&portdev->vdev->dev,
1453 "Error adding buffer to queue\n");
1454 free_buf(buf);
1455 }
1456 }
1457 spin_unlock(&portdev->cvq_lock);
1458}
1459
Amit Shah2770c5e2011-01-31 13:06:36 +05301460static void out_intr(struct virtqueue *vq)
1461{
1462 struct port *port;
1463
1464 port = find_port_by_vq(vq->vdev->priv, vq);
1465 if (!port)
1466 return;
1467
1468 wake_up_interruptible(&port->waitqueue);
1469}
1470
Amit Shah17634ba2009-12-21 21:03:25 +05301471static void in_intr(struct virtqueue *vq)
1472{
1473 struct port *port;
1474 unsigned long flags;
1475
1476 port = find_port_by_vq(vq->vdev->priv, vq);
1477 if (!port)
1478 return;
1479
1480 spin_lock_irqsave(&port->inbuf_lock, flags);
Amit Shahd25a9dd2011-09-14 13:06:43 +05301481 port->inbuf = get_inbuf(port);
Amit Shah17634ba2009-12-21 21:03:25 +05301482
Amit Shah88f251a2009-12-21 22:15:30 +05301483 /*
1484 * Don't queue up data when port is closed. This condition
1485 * can be reached when a console port is not yet connected (no
1486 * tty is spawned) and the host sends out data to console
1487 * ports. For generic serial ports, the host won't
1488 * (shouldn't) send data till the guest is connected.
1489 */
1490 if (!port->guest_connected)
1491 discard_port_data(port);
1492
Amit Shah17634ba2009-12-21 21:03:25 +05301493 spin_unlock_irqrestore(&port->inbuf_lock, flags);
1494
Amit Shah2030fa42009-12-21 21:49:30 +05301495 wake_up_interruptible(&port->waitqueue);
1496
Amit Shah55f6bcc2010-09-02 18:47:53 +05301497 /* Send a SIGIO indicating new data in case the process asked for it */
1498 send_sigio_to_port(port);
1499
Amit Shah17634ba2009-12-21 21:03:25 +05301500 if (is_console_port(port) && hvc_poll(port->cons.hvc))
1501 hvc_kick();
1502}
1503
1504static void control_intr(struct virtqueue *vq)
1505{
1506 struct ports_device *portdev;
1507
1508 portdev = vq->vdev->priv;
1509 schedule_work(&portdev->control_work);
1510}
1511
Amit Shah7f5d8102009-12-21 22:22:08 +05301512static void config_intr(struct virtio_device *vdev)
1513{
1514 struct ports_device *portdev;
1515
1516 portdev = vdev->priv;
Amit Shah99f905f2010-05-19 22:15:48 -06001517
Amit Shah4038f5b72010-05-06 02:05:07 +05301518 if (!use_multiport(portdev)) {
Amit Shah97788292010-05-06 02:05:08 +05301519 struct port *port;
1520 u16 rows, cols;
1521
1522 vdev->config->get(vdev,
1523 offsetof(struct virtio_console_config, cols),
1524 &cols, sizeof(u16));
1525 vdev->config->get(vdev,
1526 offsetof(struct virtio_console_config, rows),
1527 &rows, sizeof(u16));
1528
1529 port = find_port_by_id(portdev, 0);
1530 set_console_size(port, rows, cols);
1531
Amit Shah4038f5b72010-05-06 02:05:07 +05301532 /*
1533 * We'll use this way of resizing only for legacy
1534 * support. For newer userspace
1535 * (VIRTIO_CONSOLE_F_MULTPORT+), use control messages
1536 * to indicate console size changes so that it can be
1537 * done per-port.
1538 */
Amit Shah97788292010-05-06 02:05:08 +05301539 resize_console(port);
Amit Shah4038f5b72010-05-06 02:05:07 +05301540 }
Amit Shah7f5d8102009-12-21 22:22:08 +05301541}
1542
Amit Shah2658a792010-01-18 19:15:11 +05301543static int init_vqs(struct ports_device *portdev)
1544{
1545 vq_callback_t **io_callbacks;
1546 char **io_names;
1547 struct virtqueue **vqs;
Amit Shah17634ba2009-12-21 21:03:25 +05301548 u32 i, j, nr_ports, nr_queues;
Amit Shah2658a792010-01-18 19:15:11 +05301549 int err;
1550
Amit Shah17634ba2009-12-21 21:03:25 +05301551 nr_ports = portdev->config.max_nr_ports;
1552 nr_queues = use_multiport(portdev) ? (nr_ports + 1) * 2 : 2;
Amit Shah2658a792010-01-18 19:15:11 +05301553
1554 vqs = kmalloc(nr_queues * sizeof(struct virtqueue *), GFP_KERNEL);
Amit Shah2658a792010-01-18 19:15:11 +05301555 io_callbacks = kmalloc(nr_queues * sizeof(vq_callback_t *), GFP_KERNEL);
Amit Shah2658a792010-01-18 19:15:11 +05301556 io_names = kmalloc(nr_queues * sizeof(char *), GFP_KERNEL);
Amit Shah2658a792010-01-18 19:15:11 +05301557 portdev->in_vqs = kmalloc(nr_ports * sizeof(struct virtqueue *),
1558 GFP_KERNEL);
Amit Shah2658a792010-01-18 19:15:11 +05301559 portdev->out_vqs = kmalloc(nr_ports * sizeof(struct virtqueue *),
1560 GFP_KERNEL);
Jiri Slaby22e132f2010-11-06 10:06:50 +01001561 if (!vqs || !io_callbacks || !io_names || !portdev->in_vqs ||
Amit Shah286f9a22011-09-14 13:06:39 +05301562 !portdev->out_vqs) {
Amit Shah2658a792010-01-18 19:15:11 +05301563 err = -ENOMEM;
Jiri Slaby22e132f2010-11-06 10:06:50 +01001564 goto free;
Amit Shah2658a792010-01-18 19:15:11 +05301565 }
1566
Amit Shah17634ba2009-12-21 21:03:25 +05301567 /*
1568 * For backward compat (newer host but older guest), the host
1569 * spawns a console port first and also inits the vqs for port
1570 * 0 before others.
1571 */
1572 j = 0;
1573 io_callbacks[j] = in_intr;
Amit Shah2770c5e2011-01-31 13:06:36 +05301574 io_callbacks[j + 1] = out_intr;
Amit Shah17634ba2009-12-21 21:03:25 +05301575 io_names[j] = "input";
1576 io_names[j + 1] = "output";
1577 j += 2;
Amit Shah2658a792010-01-18 19:15:11 +05301578
Amit Shah17634ba2009-12-21 21:03:25 +05301579 if (use_multiport(portdev)) {
1580 io_callbacks[j] = control_intr;
1581 io_callbacks[j + 1] = NULL;
1582 io_names[j] = "control-i";
1583 io_names[j + 1] = "control-o";
1584
1585 for (i = 1; i < nr_ports; i++) {
1586 j += 2;
1587 io_callbacks[j] = in_intr;
Amit Shah2770c5e2011-01-31 13:06:36 +05301588 io_callbacks[j + 1] = out_intr;
Amit Shah17634ba2009-12-21 21:03:25 +05301589 io_names[j] = "input";
1590 io_names[j + 1] = "output";
1591 }
1592 }
Amit Shah2658a792010-01-18 19:15:11 +05301593 /* Find the queues. */
1594 err = portdev->vdev->config->find_vqs(portdev->vdev, nr_queues, vqs,
1595 io_callbacks,
1596 (const char **)io_names);
1597 if (err)
Jiri Slaby22e132f2010-11-06 10:06:50 +01001598 goto free;
Amit Shah2658a792010-01-18 19:15:11 +05301599
Amit Shah17634ba2009-12-21 21:03:25 +05301600 j = 0;
Amit Shah2658a792010-01-18 19:15:11 +05301601 portdev->in_vqs[0] = vqs[0];
1602 portdev->out_vqs[0] = vqs[1];
Amit Shah17634ba2009-12-21 21:03:25 +05301603 j += 2;
1604 if (use_multiport(portdev)) {
1605 portdev->c_ivq = vqs[j];
1606 portdev->c_ovq = vqs[j + 1];
Amit Shah2658a792010-01-18 19:15:11 +05301607
Amit Shah17634ba2009-12-21 21:03:25 +05301608 for (i = 1; i < nr_ports; i++) {
1609 j += 2;
1610 portdev->in_vqs[i] = vqs[j];
1611 portdev->out_vqs[i] = vqs[j + 1];
1612 }
1613 }
Amit Shah2658a792010-01-18 19:15:11 +05301614 kfree(io_names);
Jiri Slaby22e132f2010-11-06 10:06:50 +01001615 kfree(io_callbacks);
Amit Shah2658a792010-01-18 19:15:11 +05301616 kfree(vqs);
1617
1618 return 0;
1619
Jiri Slaby22e132f2010-11-06 10:06:50 +01001620free:
Amit Shah2658a792010-01-18 19:15:11 +05301621 kfree(portdev->out_vqs);
Amit Shah2658a792010-01-18 19:15:11 +05301622 kfree(portdev->in_vqs);
Jiri Slaby22e132f2010-11-06 10:06:50 +01001623 kfree(io_names);
1624 kfree(io_callbacks);
Amit Shah2658a792010-01-18 19:15:11 +05301625 kfree(vqs);
Jiri Slaby22e132f2010-11-06 10:06:50 +01001626
Amit Shah2658a792010-01-18 19:15:11 +05301627 return err;
1628}
1629
Amit Shahfb08bd22009-12-21 21:36:04 +05301630static const struct file_operations portdev_fops = {
1631 .owner = THIS_MODULE,
1632};
1633
Amit Shah1c85bf32010-01-18 19:15:07 +05301634/*
1635 * Once we're further in boot, we get probed like any other virtio
1636 * device.
Amit Shah17634ba2009-12-21 21:03:25 +05301637 *
1638 * If the host also supports multiple console ports, we check the
1639 * config space to see how many ports the host has spawned. We
1640 * initialize each port found.
Amit Shah1c85bf32010-01-18 19:15:07 +05301641 */
1642static int __devinit virtcons_probe(struct virtio_device *vdev)
1643{
Amit Shah1c85bf32010-01-18 19:15:07 +05301644 struct ports_device *portdev;
1645 int err;
Amit Shah17634ba2009-12-21 21:03:25 +05301646 bool multiport;
Amit Shah1c85bf32010-01-18 19:15:07 +05301647
1648 portdev = kmalloc(sizeof(*portdev), GFP_KERNEL);
1649 if (!portdev) {
1650 err = -ENOMEM;
1651 goto fail;
1652 }
1653
1654 /* Attach this portdev to this virtio_device, and vice-versa. */
1655 portdev->vdev = vdev;
1656 vdev->priv = portdev;
1657
Amit Shahfb08bd22009-12-21 21:36:04 +05301658 spin_lock_irq(&pdrvdata_lock);
1659 portdev->drv_index = pdrvdata.index++;
1660 spin_unlock_irq(&pdrvdata_lock);
1661
1662 portdev->chr_major = register_chrdev(0, "virtio-portsdev",
1663 &portdev_fops);
1664 if (portdev->chr_major < 0) {
1665 dev_err(&vdev->dev,
1666 "Error %d registering chrdev for device %u\n",
1667 portdev->chr_major, portdev->drv_index);
1668 err = portdev->chr_major;
1669 goto free;
1670 }
1671
Amit Shah17634ba2009-12-21 21:03:25 +05301672 multiport = false;
Amit Shah17634ba2009-12-21 21:03:25 +05301673 portdev->config.max_nr_ports = 1;
Sasha Levin51c6d612011-08-14 17:52:31 +03001674 if (virtio_config_val(vdev, VIRTIO_CONSOLE_F_MULTIPORT,
1675 offsetof(struct virtio_console_config,
1676 max_nr_ports),
1677 &portdev->config.max_nr_ports) == 0)
Amit Shah17634ba2009-12-21 21:03:25 +05301678 multiport = true;
Amit Shah17634ba2009-12-21 21:03:25 +05301679
Amit Shah2658a792010-01-18 19:15:11 +05301680 err = init_vqs(portdev);
1681 if (err < 0) {
1682 dev_err(&vdev->dev, "Error %d initializing vqs\n", err);
Amit Shahfb08bd22009-12-21 21:36:04 +05301683 goto free_chrdev;
Amit Shah2658a792010-01-18 19:15:11 +05301684 }
Amit Shah1c85bf32010-01-18 19:15:07 +05301685
Amit Shah17634ba2009-12-21 21:03:25 +05301686 spin_lock_init(&portdev->ports_lock);
1687 INIT_LIST_HEAD(&portdev->ports);
1688
1689 if (multiport) {
Amit Shah335a64a5c2010-02-24 10:37:44 +05301690 unsigned int nr_added_bufs;
1691
Amit Shah17634ba2009-12-21 21:03:25 +05301692 spin_lock_init(&portdev->cvq_lock);
1693 INIT_WORK(&portdev->control_work, &control_work_handler);
1694
Amit Shah335a64a5c2010-02-24 10:37:44 +05301695 nr_added_bufs = fill_queue(portdev->c_ivq, &portdev->cvq_lock);
1696 if (!nr_added_bufs) {
Amit Shah22a29ea2010-02-12 10:32:17 +05301697 dev_err(&vdev->dev,
1698 "Error allocating buffers for control queue\n");
1699 err = -ENOMEM;
1700 goto free_vqs;
1701 }
Amit Shah1d051602010-05-19 22:15:49 -06001702 } else {
1703 /*
1704 * For backward compatibility: Create a console port
1705 * if we're running on older host.
1706 */
1707 add_port(portdev, 0);
Amit Shah17634ba2009-12-21 21:03:25 +05301708 }
1709
Amit Shah6bdf2af2010-09-02 18:11:49 +05301710 spin_lock_irq(&pdrvdata_lock);
1711 list_add_tail(&portdev->list, &pdrvdata.portdevs);
1712 spin_unlock_irq(&pdrvdata_lock);
1713
Amit Shahf909f852010-05-19 22:15:48 -06001714 __send_control_msg(portdev, VIRTIO_CONSOLE_BAD_ID,
1715 VIRTIO_CONSOLE_DEVICE_READY, 1);
Rusty Russell31610432007-10-22 11:03:39 +10001716 return 0;
1717
Amit Shah22a29ea2010-02-12 10:32:17 +05301718free_vqs:
Julia Lawall0643e4c2010-05-15 11:45:53 +02001719 /* The host might want to notify mgmt sw about device add failure */
1720 __send_control_msg(portdev, VIRTIO_CONSOLE_BAD_ID,
1721 VIRTIO_CONSOLE_DEVICE_READY, 0);
Amit Shah22a29ea2010-02-12 10:32:17 +05301722 vdev->config->del_vqs(vdev);
1723 kfree(portdev->in_vqs);
1724 kfree(portdev->out_vqs);
Amit Shahfb08bd22009-12-21 21:36:04 +05301725free_chrdev:
1726 unregister_chrdev(portdev->chr_major, "virtio-portsdev");
Rusty Russell31610432007-10-22 11:03:39 +10001727free:
Amit Shah1c85bf32010-01-18 19:15:07 +05301728 kfree(portdev);
Rusty Russell31610432007-10-22 11:03:39 +10001729fail:
1730 return err;
1731}
1732
Amit Shah71778762010-02-12 10:32:16 +05301733static void virtcons_remove(struct virtio_device *vdev)
1734{
1735 struct ports_device *portdev;
1736 struct port *port, *port2;
Amit Shah71778762010-02-12 10:32:16 +05301737
1738 portdev = vdev->priv;
1739
Amit Shah6bdf2af2010-09-02 18:11:49 +05301740 spin_lock_irq(&pdrvdata_lock);
1741 list_del(&portdev->list);
1742 spin_unlock_irq(&pdrvdata_lock);
1743
Amit Shah02238952010-09-02 18:11:40 +05301744 /* Disable interrupts for vqs */
1745 vdev->config->reset(vdev);
1746 /* Finish up work that's lined up */
Amit Shah71778762010-02-12 10:32:16 +05301747 cancel_work_sync(&portdev->control_work);
Amit Shah71778762010-02-12 10:32:16 +05301748
1749 list_for_each_entry_safe(port, port2, &portdev->ports, list)
Amit Shahb353a6b2010-09-02 18:38:29 +05301750 unplug_port(port);
Amit Shah71778762010-02-12 10:32:16 +05301751
1752 unregister_chrdev(portdev->chr_major, "virtio-portsdev");
1753
Amit Shahe0620132010-09-02 18:38:30 +05301754 /*
1755 * When yanking out a device, we immediately lose the
1756 * (device-side) queues. So there's no point in keeping the
1757 * guest side around till we drop our final reference. This
1758 * also means that any ports which are in an open state will
1759 * have to just stop using the port, as the vqs are going
1760 * away.
1761 */
Amit Shah96eb8722010-09-02 18:11:41 +05301762 if (use_multiport(portdev)) {
1763 struct port_buffer *buf;
1764 unsigned int len;
Amit Shah71778762010-02-12 10:32:16 +05301765
Amit Shah96eb8722010-09-02 18:11:41 +05301766 while ((buf = virtqueue_get_buf(portdev->c_ivq, &len)))
1767 free_buf(buf);
1768
1769 while ((buf = virtqueue_detach_unused_buf(portdev->c_ivq)))
1770 free_buf(buf);
1771 }
Amit Shah71778762010-02-12 10:32:16 +05301772
1773 vdev->config->del_vqs(vdev);
1774 kfree(portdev->in_vqs);
1775 kfree(portdev->out_vqs);
1776
1777 kfree(portdev);
1778}
1779
Rusty Russell31610432007-10-22 11:03:39 +10001780static struct virtio_device_id id_table[] = {
1781 { VIRTIO_ID_CONSOLE, VIRTIO_DEV_ANY_ID },
1782 { 0 },
1783};
1784
Christian Borntraegerc2983452008-11-25 13:36:26 +01001785static unsigned int features[] = {
1786 VIRTIO_CONSOLE_F_SIZE,
Amit Shahb99fa812010-05-19 22:15:46 -06001787 VIRTIO_CONSOLE_F_MULTIPORT,
Christian Borntraegerc2983452008-11-25 13:36:26 +01001788};
1789
Rusty Russell31610432007-10-22 11:03:39 +10001790static struct virtio_driver virtio_console = {
Christian Borntraegerc2983452008-11-25 13:36:26 +01001791 .feature_table = features,
1792 .feature_table_size = ARRAY_SIZE(features),
Rusty Russell31610432007-10-22 11:03:39 +10001793 .driver.name = KBUILD_MODNAME,
1794 .driver.owner = THIS_MODULE,
1795 .id_table = id_table,
1796 .probe = virtcons_probe,
Amit Shah71778762010-02-12 10:32:16 +05301797 .remove = virtcons_remove,
Amit Shah7f5d8102009-12-21 22:22:08 +05301798 .config_changed = config_intr,
Rusty Russell31610432007-10-22 11:03:39 +10001799};
1800
1801static int __init init(void)
1802{
Amit Shahfb08bd22009-12-21 21:36:04 +05301803 int err;
1804
1805 pdrvdata.class = class_create(THIS_MODULE, "virtio-ports");
1806 if (IS_ERR(pdrvdata.class)) {
1807 err = PTR_ERR(pdrvdata.class);
1808 pr_err("Error %d creating virtio-ports class\n", err);
1809 return err;
1810 }
Amit Shahd99393e2009-12-21 22:36:21 +05301811
1812 pdrvdata.debugfs_dir = debugfs_create_dir("virtio-ports", NULL);
1813 if (!pdrvdata.debugfs_dir) {
1814 pr_warning("Error %ld creating debugfs dir for virtio-ports\n",
1815 PTR_ERR(pdrvdata.debugfs_dir));
1816 }
Amit Shah38edf582010-01-18 19:15:05 +05301817 INIT_LIST_HEAD(&pdrvdata.consoles);
Amit Shah6bdf2af2010-09-02 18:11:49 +05301818 INIT_LIST_HEAD(&pdrvdata.portdevs);
Amit Shah38edf582010-01-18 19:15:05 +05301819
Rusty Russell31610432007-10-22 11:03:39 +10001820 return register_virtio_driver(&virtio_console);
1821}
Amit Shah71778762010-02-12 10:32:16 +05301822
1823static void __exit fini(void)
1824{
1825 unregister_virtio_driver(&virtio_console);
1826
1827 class_destroy(pdrvdata.class);
1828 if (pdrvdata.debugfs_dir)
1829 debugfs_remove_recursive(pdrvdata.debugfs_dir);
1830}
Rusty Russell31610432007-10-22 11:03:39 +10001831module_init(init);
Amit Shah71778762010-02-12 10:32:16 +05301832module_exit(fini);
Rusty Russell31610432007-10-22 11:03:39 +10001833
1834MODULE_DEVICE_TABLE(virtio, id_table);
1835MODULE_DESCRIPTION("Virtio console driver");
1836MODULE_LICENSE("GPL");