blob: 8a9c140d19be8ab58b9bb13de130afab06d3d87d [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 Shah2030fa42009-12-21 21:49:30 +0530190 /* A waitqueue for poll() or blocking read operations */
191 wait_queue_head_t waitqueue;
192
Amit Shah431edb82009-12-21 21:57:40 +0530193 /* The 'name' of the port that we expose via sysfs properties */
194 char *name;
195
Amit Shah17634ba2009-12-21 21:03:25 +0530196 /* The 'id' to identify the port with the Host */
197 u32 id;
Amit Shah2030fa42009-12-21 21:49:30 +0530198
Amit Shahcdfadfc2010-05-19 22:15:50 -0600199 bool outvq_full;
200
Amit Shah2030fa42009-12-21 21:49:30 +0530201 /* Is the host device open */
202 bool host_connected;
Amit Shah3c7969c2009-11-26 11:25:38 +0530203
204 /* We should allow only one process to open a port */
205 bool guest_connected;
Rusty Russell21206ed2010-01-18 19:15:00 +0530206};
Rusty Russell31610432007-10-22 11:03:39 +1000207
Rusty Russell971f3392010-01-18 19:14:56 +0530208/* This is the very early arch-specified put chars function. */
209static int (*early_put_chars)(u32, const char *, int);
210
Amit Shah38edf582010-01-18 19:15:05 +0530211static struct port *find_port_by_vtermno(u32 vtermno)
212{
213 struct port *port;
Amit Shah4f23c572010-01-18 19:15:09 +0530214 struct console *cons;
Amit Shah38edf582010-01-18 19:15:05 +0530215 unsigned long flags;
216
217 spin_lock_irqsave(&pdrvdata_lock, flags);
Amit Shah4f23c572010-01-18 19:15:09 +0530218 list_for_each_entry(cons, &pdrvdata.consoles, list) {
219 if (cons->vtermno == vtermno) {
220 port = container_of(cons, struct port, cons);
Amit Shah38edf582010-01-18 19:15:05 +0530221 goto out;
Amit Shah4f23c572010-01-18 19:15:09 +0530222 }
Amit Shah38edf582010-01-18 19:15:05 +0530223 }
224 port = NULL;
225out:
226 spin_unlock_irqrestore(&pdrvdata_lock, flags);
227 return port;
228}
229
Amit Shah04950cd2010-09-02 18:20:58 +0530230static struct port *find_port_by_devt_in_portdev(struct ports_device *portdev,
231 dev_t dev)
232{
233 struct port *port;
234 unsigned long flags;
235
236 spin_lock_irqsave(&portdev->ports_lock, flags);
237 list_for_each_entry(port, &portdev->ports, list)
Amit Shahd22a6982010-09-02 18:20:59 +0530238 if (port->cdev->dev == dev)
Amit Shah04950cd2010-09-02 18:20:58 +0530239 goto out;
240 port = NULL;
241out:
242 spin_unlock_irqrestore(&portdev->ports_lock, flags);
243
244 return port;
245}
246
247static struct port *find_port_by_devt(dev_t dev)
248{
249 struct ports_device *portdev;
250 struct port *port;
251 unsigned long flags;
252
253 spin_lock_irqsave(&pdrvdata_lock, flags);
254 list_for_each_entry(portdev, &pdrvdata.portdevs, list) {
255 port = find_port_by_devt_in_portdev(portdev, dev);
256 if (port)
257 goto out;
258 }
259 port = NULL;
260out:
261 spin_unlock_irqrestore(&pdrvdata_lock, flags);
262 return port;
263}
264
Amit Shah17634ba2009-12-21 21:03:25 +0530265static struct port *find_port_by_id(struct ports_device *portdev, u32 id)
266{
267 struct port *port;
268 unsigned long flags;
269
270 spin_lock_irqsave(&portdev->ports_lock, flags);
271 list_for_each_entry(port, &portdev->ports, list)
272 if (port->id == id)
273 goto out;
274 port = NULL;
275out:
276 spin_unlock_irqrestore(&portdev->ports_lock, flags);
277
278 return port;
279}
280
Amit Shah203baab2010-01-18 19:15:12 +0530281static struct port *find_port_by_vq(struct ports_device *portdev,
282 struct virtqueue *vq)
283{
284 struct port *port;
Amit Shah203baab2010-01-18 19:15:12 +0530285 unsigned long flags;
286
Amit Shah17634ba2009-12-21 21:03:25 +0530287 spin_lock_irqsave(&portdev->ports_lock, flags);
288 list_for_each_entry(port, &portdev->ports, list)
Amit Shah203baab2010-01-18 19:15:12 +0530289 if (port->in_vq == vq || port->out_vq == vq)
290 goto out;
Amit Shah203baab2010-01-18 19:15:12 +0530291 port = NULL;
292out:
Amit Shah17634ba2009-12-21 21:03:25 +0530293 spin_unlock_irqrestore(&portdev->ports_lock, flags);
Amit Shah203baab2010-01-18 19:15:12 +0530294 return port;
295}
296
Amit Shah17634ba2009-12-21 21:03:25 +0530297static bool is_console_port(struct port *port)
298{
299 if (port->cons.hvc)
300 return true;
301 return false;
302}
303
304static inline bool use_multiport(struct ports_device *portdev)
305{
306 /*
307 * This condition can be true when put_chars is called from
308 * early_init
309 */
310 if (!portdev->vdev)
311 return 0;
312 return portdev->vdev->features[0] & (1 << VIRTIO_CONSOLE_F_MULTIPORT);
313}
314
Amit Shahfdb9a052010-01-18 19:15:01 +0530315static void free_buf(struct port_buffer *buf)
316{
317 kfree(buf->buf);
318 kfree(buf);
319}
320
321static struct port_buffer *alloc_buf(size_t buf_size)
322{
323 struct port_buffer *buf;
324
325 buf = kmalloc(sizeof(*buf), GFP_KERNEL);
326 if (!buf)
327 goto fail;
328 buf->buf = kzalloc(buf_size, GFP_KERNEL);
329 if (!buf->buf)
330 goto free_buf;
331 buf->len = 0;
332 buf->offset = 0;
333 buf->size = buf_size;
334 return buf;
335
336free_buf:
337 kfree(buf);
338fail:
339 return NULL;
340}
341
Amit Shaha3cde442010-01-18 19:15:03 +0530342/* Callers should take appropriate locks */
343static void *get_inbuf(struct port *port)
344{
345 struct port_buffer *buf;
346 struct virtqueue *vq;
347 unsigned int len;
348
349 vq = port->in_vq;
Michael S. Tsirkin505b0452010-04-12 16:18:32 +0300350 buf = virtqueue_get_buf(vq, &len);
Amit Shaha3cde442010-01-18 19:15:03 +0530351 if (buf) {
352 buf->len = len;
353 buf->offset = 0;
354 }
355 return buf;
356}
357
Rusty Russella23ea922010-01-18 19:14:55 +0530358/*
Amit Shahe27b5192010-01-18 19:15:02 +0530359 * Create a scatter-gather list representing our input buffer and put
360 * it in the queue.
361 *
362 * Callers should take appropriate locks.
363 */
Amit Shah203baab2010-01-18 19:15:12 +0530364static int add_inbuf(struct virtqueue *vq, struct port_buffer *buf)
Amit Shahe27b5192010-01-18 19:15:02 +0530365{
366 struct scatterlist sg[1];
Amit Shah203baab2010-01-18 19:15:12 +0530367 int ret;
Amit Shah1c85bf32010-01-18 19:15:07 +0530368
Amit Shahe27b5192010-01-18 19:15:02 +0530369 sg_init_one(sg, buf->buf, buf->size);
370
Michael S. Tsirkin505b0452010-04-12 16:18:32 +0300371 ret = virtqueue_add_buf(vq, sg, 0, 1, buf);
372 virtqueue_kick(vq);
Amit Shah203baab2010-01-18 19:15:12 +0530373 return ret;
374}
375
Amit Shah88f251a2009-12-21 22:15:30 +0530376/* Discard any unread data this port has. Callers lockers. */
377static void discard_port_data(struct port *port)
378{
379 struct port_buffer *buf;
380 struct virtqueue *vq;
381 unsigned int len;
Amit Shahd6933562010-02-12 10:32:18 +0530382 int ret;
Amit Shah88f251a2009-12-21 22:15:30 +0530383
384 vq = port->in_vq;
385 if (port->inbuf)
386 buf = port->inbuf;
387 else
Michael S. Tsirkin505b0452010-04-12 16:18:32 +0300388 buf = virtqueue_get_buf(vq, &len);
Amit Shah88f251a2009-12-21 22:15:30 +0530389
Amit Shahd6933562010-02-12 10:32:18 +0530390 ret = 0;
391 while (buf) {
392 if (add_inbuf(vq, buf) < 0) {
393 ret++;
394 free_buf(buf);
395 }
Michael S. Tsirkin505b0452010-04-12 16:18:32 +0300396 buf = virtqueue_get_buf(vq, &len);
Amit Shah88f251a2009-12-21 22:15:30 +0530397 }
Amit Shah88f251a2009-12-21 22:15:30 +0530398 port->inbuf = NULL;
Amit Shahd6933562010-02-12 10:32:18 +0530399 if (ret)
400 dev_warn(port->dev, "Errors adding %d buffers back to vq\n",
401 ret);
Amit Shah88f251a2009-12-21 22:15:30 +0530402}
403
Amit Shah203baab2010-01-18 19:15:12 +0530404static bool port_has_data(struct port *port)
405{
406 unsigned long flags;
407 bool ret;
408
Amit Shah203baab2010-01-18 19:15:12 +0530409 spin_lock_irqsave(&port->inbuf_lock, flags);
Amit Shahd6933562010-02-12 10:32:18 +0530410 if (port->inbuf) {
Amit Shah203baab2010-01-18 19:15:12 +0530411 ret = true;
Amit Shahd6933562010-02-12 10:32:18 +0530412 goto out;
413 }
414 port->inbuf = get_inbuf(port);
415 if (port->inbuf) {
416 ret = true;
417 goto out;
418 }
419 ret = false;
420out:
Amit Shah203baab2010-01-18 19:15:12 +0530421 spin_unlock_irqrestore(&port->inbuf_lock, flags);
Amit Shah203baab2010-01-18 19:15:12 +0530422 return ret;
423}
424
Amit Shah3425e702010-05-19 22:15:46 -0600425static ssize_t __send_control_msg(struct ports_device *portdev, u32 port_id,
426 unsigned int event, unsigned int value)
Amit Shah17634ba2009-12-21 21:03:25 +0530427{
428 struct scatterlist sg[1];
429 struct virtio_console_control cpkt;
430 struct virtqueue *vq;
Amit Shah604b2ad2010-02-24 10:36:51 +0530431 unsigned int len;
Amit Shah17634ba2009-12-21 21:03:25 +0530432
Amit Shah3425e702010-05-19 22:15:46 -0600433 if (!use_multiport(portdev))
Amit Shah17634ba2009-12-21 21:03:25 +0530434 return 0;
435
Amit Shah3425e702010-05-19 22:15:46 -0600436 cpkt.id = port_id;
Amit Shah17634ba2009-12-21 21:03:25 +0530437 cpkt.event = event;
438 cpkt.value = value;
439
Amit Shah3425e702010-05-19 22:15:46 -0600440 vq = portdev->c_ovq;
Amit Shah17634ba2009-12-21 21:03:25 +0530441
442 sg_init_one(sg, &cpkt, sizeof(cpkt));
Michael S. Tsirkin505b0452010-04-12 16:18:32 +0300443 if (virtqueue_add_buf(vq, sg, 1, 0, &cpkt) >= 0) {
444 virtqueue_kick(vq);
445 while (!virtqueue_get_buf(vq, &len))
Amit Shah17634ba2009-12-21 21:03:25 +0530446 cpu_relax();
447 }
448 return 0;
449}
450
Amit Shah3425e702010-05-19 22:15:46 -0600451static ssize_t send_control_msg(struct port *port, unsigned int event,
452 unsigned int value)
453{
Amit Shah84ec06c2010-09-02 18:11:42 +0530454 /* Did the port get unplugged before userspace closed it? */
455 if (port->portdev)
456 return __send_control_msg(port->portdev, port->id, event, value);
457 return 0;
Amit Shah3425e702010-05-19 22:15:46 -0600458}
459
Amit Shahcdfadfc2010-05-19 22:15:50 -0600460/* Callers must take the port->outvq_lock */
461static void reclaim_consumed_buffers(struct port *port)
462{
463 void *buf;
464 unsigned int len;
465
466 while ((buf = virtqueue_get_buf(port->out_vq, &len))) {
467 kfree(buf);
468 port->outvq_full = false;
469 }
470}
471
472static ssize_t send_buf(struct port *port, void *in_buf, size_t in_count,
473 bool nonblock)
Amit Shahf997f00b2009-12-21 17:28:51 +0530474{
475 struct scatterlist sg[1];
476 struct virtqueue *out_vq;
477 ssize_t ret;
Amit Shahcdfadfc2010-05-19 22:15:50 -0600478 unsigned long flags;
Amit Shahf997f00b2009-12-21 17:28:51 +0530479 unsigned int len;
480
481 out_vq = port->out_vq;
482
Amit Shahcdfadfc2010-05-19 22:15:50 -0600483 spin_lock_irqsave(&port->outvq_lock, flags);
484
485 reclaim_consumed_buffers(port);
486
Amit Shahf997f00b2009-12-21 17:28:51 +0530487 sg_init_one(sg, in_buf, in_count);
Michael S. Tsirkin505b0452010-04-12 16:18:32 +0300488 ret = virtqueue_add_buf(out_vq, sg, 1, 0, in_buf);
Amit Shahf997f00b2009-12-21 17:28:51 +0530489
490 /* Tell Host to go! */
Michael S. Tsirkin505b0452010-04-12 16:18:32 +0300491 virtqueue_kick(out_vq);
Amit Shahf997f00b2009-12-21 17:28:51 +0530492
493 if (ret < 0) {
Rusty Russell9ff4cfa2010-04-08 09:46:16 -0600494 in_count = 0;
Amit Shahcdfadfc2010-05-19 22:15:50 -0600495 goto done;
Amit Shahf997f00b2009-12-21 17:28:51 +0530496 }
497
Amit Shahcdfadfc2010-05-19 22:15:50 -0600498 if (ret == 0)
499 port->outvq_full = true;
500
501 if (nonblock)
502 goto done;
503
504 /*
505 * Wait till the host acknowledges it pushed out the data we
Amit Shah531295e2010-10-20 13:45:43 +1030506 * sent. This is done for data from the hvc_console; the tty
507 * operations are performed with spinlocks held so we can't
508 * sleep here. An alternative would be to copy the data to a
509 * buffer and relax the spinning requirement. The downside is
510 * we need to kmalloc a GFP_ATOMIC buffer each time the
511 * console driver writes something out.
Amit Shahcdfadfc2010-05-19 22:15:50 -0600512 */
Michael S. Tsirkin505b0452010-04-12 16:18:32 +0300513 while (!virtqueue_get_buf(out_vq, &len))
Amit Shahf997f00b2009-12-21 17:28:51 +0530514 cpu_relax();
Amit Shahcdfadfc2010-05-19 22:15:50 -0600515done:
516 spin_unlock_irqrestore(&port->outvq_lock, flags);
517 /*
518 * We're expected to return the amount of data we wrote -- all
519 * of it
520 */
Rusty Russell9ff4cfa2010-04-08 09:46:16 -0600521 return in_count;
Amit Shahf997f00b2009-12-21 17:28:51 +0530522}
523
Amit Shah203baab2010-01-18 19:15:12 +0530524/*
525 * Give out the data that's requested from the buffer that we have
526 * queued up.
527 */
Amit Shahb766cee2009-12-21 21:26:45 +0530528static ssize_t fill_readbuf(struct port *port, char *out_buf, size_t out_count,
529 bool to_user)
Amit Shah203baab2010-01-18 19:15:12 +0530530{
531 struct port_buffer *buf;
532 unsigned long flags;
533
534 if (!out_count || !port_has_data(port))
535 return 0;
536
537 buf = port->inbuf;
Amit Shahb766cee2009-12-21 21:26:45 +0530538 out_count = min(out_count, buf->len - buf->offset);
Amit Shah203baab2010-01-18 19:15:12 +0530539
Amit Shahb766cee2009-12-21 21:26:45 +0530540 if (to_user) {
541 ssize_t ret;
Amit Shah203baab2010-01-18 19:15:12 +0530542
Amit Shahb766cee2009-12-21 21:26:45 +0530543 ret = copy_to_user(out_buf, buf->buf + buf->offset, out_count);
544 if (ret)
545 return -EFAULT;
546 } else {
547 memcpy(out_buf, buf->buf + buf->offset, out_count);
548 }
549
Amit Shah203baab2010-01-18 19:15:12 +0530550 buf->offset += out_count;
551
552 if (buf->offset == buf->len) {
553 /*
554 * We're done using all the data in this buffer.
555 * Re-queue so that the Host can send us more data.
556 */
557 spin_lock_irqsave(&port->inbuf_lock, flags);
558 port->inbuf = NULL;
559
560 if (add_inbuf(port->in_vq, buf) < 0)
Amit Shahfb08bd22009-12-21 21:36:04 +0530561 dev_warn(port->dev, "failed add_buf\n");
Amit Shah203baab2010-01-18 19:15:12 +0530562
563 spin_unlock_irqrestore(&port->inbuf_lock, flags);
564 }
Amit Shahb766cee2009-12-21 21:26:45 +0530565 /* Return the number of bytes actually copied */
Amit Shah203baab2010-01-18 19:15:12 +0530566 return out_count;
Amit Shahe27b5192010-01-18 19:15:02 +0530567}
568
Amit Shah2030fa42009-12-21 21:49:30 +0530569/* The condition that must be true for polling to end */
Amit Shah60caacd2010-05-19 22:15:49 -0600570static bool will_read_block(struct port *port)
Amit Shah2030fa42009-12-21 21:49:30 +0530571{
Amit Shah3709ea72010-09-02 18:11:43 +0530572 if (!port->guest_connected) {
573 /* Port got hot-unplugged. Let's exit. */
574 return false;
575 }
Amit Shah60caacd2010-05-19 22:15:49 -0600576 return !port_has_data(port) && port->host_connected;
Amit Shah2030fa42009-12-21 21:49:30 +0530577}
578
Amit Shahcdfadfc2010-05-19 22:15:50 -0600579static bool will_write_block(struct port *port)
580{
581 bool ret;
582
Amit Shah60e5e0b2010-05-27 13:24:40 +0530583 if (!port->guest_connected) {
584 /* Port got hot-unplugged. Let's exit. */
585 return false;
586 }
Amit Shahcdfadfc2010-05-19 22:15:50 -0600587 if (!port->host_connected)
588 return true;
589
590 spin_lock_irq(&port->outvq_lock);
591 /*
592 * Check if the Host has consumed any buffers since we last
593 * sent data (this is only applicable for nonblocking ports).
594 */
595 reclaim_consumed_buffers(port);
596 ret = port->outvq_full;
597 spin_unlock_irq(&port->outvq_lock);
598
599 return ret;
600}
601
Amit Shah2030fa42009-12-21 21:49:30 +0530602static ssize_t port_fops_read(struct file *filp, char __user *ubuf,
603 size_t count, loff_t *offp)
604{
605 struct port *port;
606 ssize_t ret;
607
608 port = filp->private_data;
609
610 if (!port_has_data(port)) {
611 /*
612 * If nothing's connected on the host just return 0 in
613 * case of list_empty; this tells the userspace app
614 * that there's no connection
615 */
616 if (!port->host_connected)
617 return 0;
618 if (filp->f_flags & O_NONBLOCK)
619 return -EAGAIN;
620
621 ret = wait_event_interruptible(port->waitqueue,
Amit Shah60caacd2010-05-19 22:15:49 -0600622 !will_read_block(port));
Amit Shah2030fa42009-12-21 21:49:30 +0530623 if (ret < 0)
624 return ret;
625 }
Amit Shahb3dddb92010-09-02 18:11:45 +0530626 /* Port got hot-unplugged. */
627 if (!port->guest_connected)
628 return -ENODEV;
Amit Shah2030fa42009-12-21 21:49:30 +0530629 /*
630 * We could've received a disconnection message while we were
631 * waiting for more data.
632 *
633 * This check is not clubbed in the if() statement above as we
634 * might receive some data as well as the host could get
635 * disconnected after we got woken up from our wait. So we
636 * really want to give off whatever data we have and only then
637 * check for host_connected.
638 */
639 if (!port_has_data(port) && !port->host_connected)
640 return 0;
641
642 return fill_readbuf(port, ubuf, count, true);
643}
644
645static ssize_t port_fops_write(struct file *filp, const char __user *ubuf,
646 size_t count, loff_t *offp)
647{
648 struct port *port;
649 char *buf;
650 ssize_t ret;
Amit Shahcdfadfc2010-05-19 22:15:50 -0600651 bool nonblock;
Amit Shah2030fa42009-12-21 21:49:30 +0530652
Amit Shah65745422010-09-14 13:26:16 +0530653 /* Userspace could be out to fool us */
654 if (!count)
655 return 0;
656
Amit Shah2030fa42009-12-21 21:49:30 +0530657 port = filp->private_data;
658
Amit Shahcdfadfc2010-05-19 22:15:50 -0600659 nonblock = filp->f_flags & O_NONBLOCK;
660
661 if (will_write_block(port)) {
662 if (nonblock)
663 return -EAGAIN;
664
665 ret = wait_event_interruptible(port->waitqueue,
666 !will_write_block(port));
667 if (ret < 0)
668 return ret;
669 }
Amit Shahf4028112010-09-02 18:11:46 +0530670 /* Port got hot-unplugged. */
671 if (!port->guest_connected)
672 return -ENODEV;
Amit Shahcdfadfc2010-05-19 22:15:50 -0600673
Amit Shah2030fa42009-12-21 21:49:30 +0530674 count = min((size_t)(32 * 1024), count);
675
676 buf = kmalloc(count, GFP_KERNEL);
677 if (!buf)
678 return -ENOMEM;
679
680 ret = copy_from_user(buf, ubuf, count);
681 if (ret) {
682 ret = -EFAULT;
683 goto free_buf;
684 }
685
Amit Shah531295e2010-10-20 13:45:43 +1030686 /*
687 * We now ask send_buf() to not spin for generic ports -- we
688 * can re-use the same code path that non-blocking file
689 * descriptors take for blocking file descriptors since the
690 * wait is already done and we're certain the write will go
691 * through to the host.
692 */
693 nonblock = true;
Amit Shahcdfadfc2010-05-19 22:15:50 -0600694 ret = send_buf(port, buf, count, nonblock);
695
696 if (nonblock && ret > 0)
697 goto out;
698
Amit Shah2030fa42009-12-21 21:49:30 +0530699free_buf:
700 kfree(buf);
Amit Shahcdfadfc2010-05-19 22:15:50 -0600701out:
Amit Shah2030fa42009-12-21 21:49:30 +0530702 return ret;
703}
704
705static unsigned int port_fops_poll(struct file *filp, poll_table *wait)
706{
707 struct port *port;
708 unsigned int ret;
709
710 port = filp->private_data;
711 poll_wait(filp, &port->waitqueue, wait);
712
Amit Shah8529a502010-09-02 18:11:44 +0530713 if (!port->guest_connected) {
714 /* Port got unplugged */
715 return POLLHUP;
716 }
Amit Shah2030fa42009-12-21 21:49:30 +0530717 ret = 0;
Hans de Goede6df7aad2010-09-16 14:43:08 +0530718 if (!will_read_block(port))
Amit Shah2030fa42009-12-21 21:49:30 +0530719 ret |= POLLIN | POLLRDNORM;
Amit Shahcdfadfc2010-05-19 22:15:50 -0600720 if (!will_write_block(port))
Amit Shah2030fa42009-12-21 21:49:30 +0530721 ret |= POLLOUT;
722 if (!port->host_connected)
723 ret |= POLLHUP;
724
725 return ret;
726}
727
728static int port_fops_release(struct inode *inode, struct file *filp)
729{
730 struct port *port;
731
732 port = filp->private_data;
733
734 /* Notify host of port being closed */
735 send_control_msg(port, VIRTIO_CONSOLE_PORT_OPEN, 0);
736
Amit Shah88f251a2009-12-21 22:15:30 +0530737 spin_lock_irq(&port->inbuf_lock);
Amit Shah3c7969c2009-11-26 11:25:38 +0530738 port->guest_connected = false;
739
Amit Shah88f251a2009-12-21 22:15:30 +0530740 discard_port_data(port);
741
742 spin_unlock_irq(&port->inbuf_lock);
743
Amit Shahcdfadfc2010-05-19 22:15:50 -0600744 spin_lock_irq(&port->outvq_lock);
745 reclaim_consumed_buffers(port);
746 spin_unlock_irq(&port->outvq_lock);
747
Amit Shah2030fa42009-12-21 21:49:30 +0530748 return 0;
749}
750
751static int port_fops_open(struct inode *inode, struct file *filp)
752{
753 struct cdev *cdev = inode->i_cdev;
754 struct port *port;
Amit Shah8ad37e82010-09-02 18:11:48 +0530755 int ret;
Amit Shah2030fa42009-12-21 21:49:30 +0530756
Amit Shah04950cd2010-09-02 18:20:58 +0530757 port = find_port_by_devt(cdev->dev);
Amit Shah2030fa42009-12-21 21:49:30 +0530758 filp->private_data = port;
759
760 /*
761 * Don't allow opening of console port devices -- that's done
762 * via /dev/hvc
763 */
Amit Shah8ad37e82010-09-02 18:11:48 +0530764 if (is_console_port(port)) {
765 ret = -ENXIO;
766 goto out;
767 }
Amit Shah2030fa42009-12-21 21:49:30 +0530768
Amit Shah3c7969c2009-11-26 11:25:38 +0530769 /* Allow only one process to open a particular port at a time */
770 spin_lock_irq(&port->inbuf_lock);
771 if (port->guest_connected) {
772 spin_unlock_irq(&port->inbuf_lock);
Amit Shah8ad37e82010-09-02 18:11:48 +0530773 ret = -EMFILE;
774 goto out;
Amit Shah3c7969c2009-11-26 11:25:38 +0530775 }
776
777 port->guest_connected = true;
778 spin_unlock_irq(&port->inbuf_lock);
779
Amit Shahcdfadfc2010-05-19 22:15:50 -0600780 spin_lock_irq(&port->outvq_lock);
781 /*
782 * There might be a chance that we missed reclaiming a few
783 * buffers in the window of the port getting previously closed
784 * and opening now.
785 */
786 reclaim_consumed_buffers(port);
787 spin_unlock_irq(&port->outvq_lock);
788
Amit Shah2030fa42009-12-21 21:49:30 +0530789 /* Notify host of port being opened */
790 send_control_msg(filp->private_data, VIRTIO_CONSOLE_PORT_OPEN, 1);
791
792 return 0;
Amit Shah8ad37e82010-09-02 18:11:48 +0530793out:
794 return ret;
Amit Shah2030fa42009-12-21 21:49:30 +0530795}
796
797/*
798 * The file operations that we support: programs in the guest can open
799 * a console device, read from it, write to it, poll for data and
800 * close it. The devices are at
801 * /dev/vport<device number>p<port number>
802 */
803static const struct file_operations port_fops = {
804 .owner = THIS_MODULE,
805 .open = port_fops_open,
806 .read = port_fops_read,
807 .write = port_fops_write,
808 .poll = port_fops_poll,
809 .release = port_fops_release,
810};
811
Amit Shahe27b5192010-01-18 19:15:02 +0530812/*
Rusty Russella23ea922010-01-18 19:14:55 +0530813 * The put_chars() callback is pretty straightforward.
Rusty Russell31610432007-10-22 11:03:39 +1000814 *
Rusty Russella23ea922010-01-18 19:14:55 +0530815 * We turn the characters into a scatter-gather list, add it to the
816 * output queue and then kick the Host. Then we sit here waiting for
817 * it to finish: inefficient in theory, but in practice
818 * implementations will do it immediately (lguest's Launcher does).
819 */
Rusty Russell31610432007-10-22 11:03:39 +1000820static int put_chars(u32 vtermno, const char *buf, int count)
821{
Rusty Russell21206ed2010-01-18 19:15:00 +0530822 struct port *port;
Amit Shah38edf582010-01-18 19:15:05 +0530823
François Diakhaté162a6892010-03-23 18:23:15 +0530824 if (unlikely(early_put_chars))
825 return early_put_chars(vtermno, buf, count);
826
Amit Shah38edf582010-01-18 19:15:05 +0530827 port = find_port_by_vtermno(vtermno);
828 if (!port)
Amit Shah6dc69f972010-05-19 22:15:47 -0600829 return -EPIPE;
Rusty Russell31610432007-10-22 11:03:39 +1000830
Amit Shahcdfadfc2010-05-19 22:15:50 -0600831 return send_buf(port, (void *)buf, count, false);
Rusty Russell31610432007-10-22 11:03:39 +1000832}
833
Rusty Russella23ea922010-01-18 19:14:55 +0530834/*
Rusty Russella23ea922010-01-18 19:14:55 +0530835 * get_chars() is the callback from the hvc_console infrastructure
836 * when an interrupt is received.
Rusty Russell31610432007-10-22 11:03:39 +1000837 *
Amit Shah203baab2010-01-18 19:15:12 +0530838 * We call out to fill_readbuf that gets us the required data from the
839 * buffers that are queued up.
Rusty Russella23ea922010-01-18 19:14:55 +0530840 */
Rusty Russell31610432007-10-22 11:03:39 +1000841static int get_chars(u32 vtermno, char *buf, int count)
842{
Rusty Russell21206ed2010-01-18 19:15:00 +0530843 struct port *port;
Rusty Russell31610432007-10-22 11:03:39 +1000844
Amit Shah6dc69f972010-05-19 22:15:47 -0600845 /* If we've not set up the port yet, we have no input to give. */
846 if (unlikely(early_put_chars))
847 return 0;
848
Amit Shah38edf582010-01-18 19:15:05 +0530849 port = find_port_by_vtermno(vtermno);
850 if (!port)
Amit Shah6dc69f972010-05-19 22:15:47 -0600851 return -EPIPE;
Rusty Russell21206ed2010-01-18 19:15:00 +0530852
853 /* If we don't have an input queue yet, we can't get input. */
854 BUG_ON(!port->in_vq);
855
Amit Shahb766cee2009-12-21 21:26:45 +0530856 return fill_readbuf(port, buf, count, false);
Rusty Russell31610432007-10-22 11:03:39 +1000857}
Rusty Russell31610432007-10-22 11:03:39 +1000858
Amit Shahcb06e362010-01-18 19:15:08 +0530859static void resize_console(struct port *port)
Christian Borntraegerc2983452008-11-25 13:36:26 +0100860{
Amit Shahcb06e362010-01-18 19:15:08 +0530861 struct virtio_device *vdev;
Christian Borntraegerc2983452008-11-25 13:36:26 +0100862
Amit Shah2de16a42010-03-19 17:36:44 +0530863 /* The port could have been hot-unplugged */
Amit Shah97788292010-05-06 02:05:08 +0530864 if (!port || !is_console_port(port))
Amit Shah2de16a42010-03-19 17:36:44 +0530865 return;
866
Amit Shahcb06e362010-01-18 19:15:08 +0530867 vdev = port->portdev->vdev;
Amit Shah97788292010-05-06 02:05:08 +0530868 if (virtio_has_feature(vdev, VIRTIO_CONSOLE_F_SIZE))
869 hvc_resize(port->cons.hvc, port->cons.ws);
Christian Borntraegerc2983452008-11-25 13:36:26 +0100870}
871
Amit Shah38edf582010-01-18 19:15:05 +0530872/* We set the configuration at this point, since we now have a tty */
Christian Borntraeger91fcad12008-06-20 15:24:15 +0200873static int notifier_add_vio(struct hvc_struct *hp, int data)
874{
Amit Shah38edf582010-01-18 19:15:05 +0530875 struct port *port;
876
877 port = find_port_by_vtermno(hp->vtermno);
878 if (!port)
879 return -EINVAL;
880
Christian Borntraeger91fcad12008-06-20 15:24:15 +0200881 hp->irq_requested = 1;
Amit Shahcb06e362010-01-18 19:15:08 +0530882 resize_console(port);
Christian Borntraegerc2983452008-11-25 13:36:26 +0100883
Christian Borntraeger91fcad12008-06-20 15:24:15 +0200884 return 0;
885}
886
887static void notifier_del_vio(struct hvc_struct *hp, int data)
888{
889 hp->irq_requested = 0;
890}
891
Amit Shah17634ba2009-12-21 21:03:25 +0530892/* The operations for console ports. */
Rusty Russell1dff3992009-11-28 12:20:26 +0530893static const struct hv_ops hv_ops = {
Rusty Russell971f3392010-01-18 19:14:56 +0530894 .get_chars = get_chars,
895 .put_chars = put_chars,
896 .notifier_add = notifier_add_vio,
897 .notifier_del = notifier_del_vio,
898 .notifier_hangup = notifier_del_vio,
899};
900
901/*
902 * Console drivers are initialized very early so boot messages can go
903 * out, so we do things slightly differently from the generic virtio
904 * initialization of the net and block drivers.
905 *
906 * At this stage, the console is output-only. It's too early to set
907 * up a virtqueue, so we let the drivers do some boutique early-output
908 * thing.
909 */
910int __init virtio_cons_early_init(int (*put_chars)(u32, const char *, int))
911{
912 early_put_chars = put_chars;
913 return hvc_instantiate(0, 0, &hv_ops);
914}
915
Amit Shah17634ba2009-12-21 21:03:25 +0530916int init_port_console(struct port *port)
Amit Shahcfa6d372010-01-18 19:15:10 +0530917{
918 int ret;
919
920 /*
921 * The Host's telling us this port is a console port. Hook it
922 * up with an hvc console.
923 *
924 * To set up and manage our virtual console, we call
925 * hvc_alloc().
926 *
927 * The first argument of hvc_alloc() is the virtual console
928 * number. The second argument is the parameter for the
929 * notification mechanism (like irq number). We currently
930 * leave this as zero, virtqueues have implicit notifications.
931 *
932 * The third argument is a "struct hv_ops" containing the
933 * put_chars() get_chars(), notifier_add() and notifier_del()
934 * pointers. The final argument is the output buffer size: we
935 * can do any size, so we put PAGE_SIZE here.
936 */
937 port->cons.vtermno = pdrvdata.next_vtermno;
938
939 port->cons.hvc = hvc_alloc(port->cons.vtermno, 0, &hv_ops, PAGE_SIZE);
940 if (IS_ERR(port->cons.hvc)) {
941 ret = PTR_ERR(port->cons.hvc);
Amit Shah298add72010-01-18 16:35:23 +0530942 dev_err(port->dev,
943 "error %d allocating hvc for port\n", ret);
Amit Shahcfa6d372010-01-18 19:15:10 +0530944 port->cons.hvc = NULL;
945 return ret;
946 }
947 spin_lock_irq(&pdrvdata_lock);
948 pdrvdata.next_vtermno++;
949 list_add_tail(&port->cons.list, &pdrvdata.consoles);
950 spin_unlock_irq(&pdrvdata_lock);
Amit Shah3c7969c2009-11-26 11:25:38 +0530951 port->guest_connected = true;
Amit Shahcfa6d372010-01-18 19:15:10 +0530952
Amit Shah1d051602010-05-19 22:15:49 -0600953 /*
954 * Start using the new console output if this is the first
955 * console to come up.
956 */
957 if (early_put_chars)
958 early_put_chars = NULL;
959
Amit Shah2030fa42009-12-21 21:49:30 +0530960 /* Notify host of port being opened */
961 send_control_msg(port, VIRTIO_CONSOLE_PORT_OPEN, 1);
962
Amit Shahcfa6d372010-01-18 19:15:10 +0530963 return 0;
964}
965
Amit Shah431edb82009-12-21 21:57:40 +0530966static ssize_t show_port_name(struct device *dev,
967 struct device_attribute *attr, char *buffer)
968{
969 struct port *port;
970
971 port = dev_get_drvdata(dev);
972
973 return sprintf(buffer, "%s\n", port->name);
974}
975
976static DEVICE_ATTR(name, S_IRUGO, show_port_name, NULL);
977
978static struct attribute *port_sysfs_entries[] = {
979 &dev_attr_name.attr,
980 NULL
981};
982
983static struct attribute_group port_attribute_group = {
984 .name = NULL, /* put in device directory */
985 .attrs = port_sysfs_entries,
986};
987
Amit Shahd99393e2009-12-21 22:36:21 +0530988static int debugfs_open(struct inode *inode, struct file *filp)
989{
990 filp->private_data = inode->i_private;
991 return 0;
992}
993
994static ssize_t debugfs_read(struct file *filp, char __user *ubuf,
995 size_t count, loff_t *offp)
996{
997 struct port *port;
998 char *buf;
999 ssize_t ret, out_offset, out_count;
1000
1001 out_count = 1024;
1002 buf = kmalloc(out_count, GFP_KERNEL);
1003 if (!buf)
1004 return -ENOMEM;
1005
1006 port = filp->private_data;
1007 out_offset = 0;
1008 out_offset += snprintf(buf + out_offset, out_count,
1009 "name: %s\n", port->name ? port->name : "");
1010 out_offset += snprintf(buf + out_offset, out_count - out_offset,
1011 "guest_connected: %d\n", port->guest_connected);
1012 out_offset += snprintf(buf + out_offset, out_count - out_offset,
1013 "host_connected: %d\n", port->host_connected);
1014 out_offset += snprintf(buf + out_offset, out_count - out_offset,
Amit Shahcdfadfc2010-05-19 22:15:50 -06001015 "outvq_full: %d\n", port->outvq_full);
1016 out_offset += snprintf(buf + out_offset, out_count - out_offset,
Amit Shahd99393e2009-12-21 22:36:21 +05301017 "is_console: %s\n",
1018 is_console_port(port) ? "yes" : "no");
1019 out_offset += snprintf(buf + out_offset, out_count - out_offset,
1020 "console_vtermno: %u\n", port->cons.vtermno);
1021
1022 ret = simple_read_from_buffer(ubuf, count, offp, buf, out_offset);
1023 kfree(buf);
1024 return ret;
1025}
1026
1027static const struct file_operations port_debugfs_ops = {
1028 .owner = THIS_MODULE,
1029 .open = debugfs_open,
1030 .read = debugfs_read,
1031};
1032
Amit Shah97788292010-05-06 02:05:08 +05301033static void set_console_size(struct port *port, u16 rows, u16 cols)
1034{
1035 if (!port || !is_console_port(port))
1036 return;
1037
1038 port->cons.ws.ws_row = rows;
1039 port->cons.ws.ws_col = cols;
1040}
1041
Amit Shahc446f8f2010-05-19 22:15:48 -06001042static unsigned int fill_queue(struct virtqueue *vq, spinlock_t *lock)
1043{
1044 struct port_buffer *buf;
1045 unsigned int nr_added_bufs;
1046 int ret;
1047
1048 nr_added_bufs = 0;
1049 do {
1050 buf = alloc_buf(PAGE_SIZE);
1051 if (!buf)
1052 break;
1053
1054 spin_lock_irq(lock);
1055 ret = add_inbuf(vq, buf);
1056 if (ret < 0) {
1057 spin_unlock_irq(lock);
1058 free_buf(buf);
1059 break;
1060 }
1061 nr_added_bufs++;
1062 spin_unlock_irq(lock);
1063 } while (ret > 0);
1064
1065 return nr_added_bufs;
1066}
1067
1068static int add_port(struct ports_device *portdev, u32 id)
1069{
1070 char debugfs_name[16];
1071 struct port *port;
1072 struct port_buffer *buf;
1073 dev_t devt;
1074 unsigned int nr_added_bufs;
1075 int err;
1076
1077 port = kmalloc(sizeof(*port), GFP_KERNEL);
1078 if (!port) {
1079 err = -ENOMEM;
1080 goto fail;
1081 }
1082
1083 port->portdev = portdev;
1084 port->id = id;
1085
1086 port->name = NULL;
1087 port->inbuf = NULL;
1088 port->cons.hvc = NULL;
1089
Amit Shah97788292010-05-06 02:05:08 +05301090 port->cons.ws.ws_row = port->cons.ws.ws_col = 0;
1091
Amit Shahc446f8f2010-05-19 22:15:48 -06001092 port->host_connected = port->guest_connected = false;
1093
Amit Shahcdfadfc2010-05-19 22:15:50 -06001094 port->outvq_full = false;
1095
Amit Shahc446f8f2010-05-19 22:15:48 -06001096 port->in_vq = portdev->in_vqs[port->id];
1097 port->out_vq = portdev->out_vqs[port->id];
1098
Amit Shahd22a6982010-09-02 18:20:59 +05301099 port->cdev = cdev_alloc();
1100 if (!port->cdev) {
1101 dev_err(&port->portdev->vdev->dev, "Error allocating cdev\n");
1102 err = -ENOMEM;
1103 goto free_port;
1104 }
1105 port->cdev->ops = &port_fops;
Amit Shahc446f8f2010-05-19 22:15:48 -06001106
1107 devt = MKDEV(portdev->chr_major, id);
Amit Shahd22a6982010-09-02 18:20:59 +05301108 err = cdev_add(port->cdev, devt, 1);
Amit Shahc446f8f2010-05-19 22:15:48 -06001109 if (err < 0) {
1110 dev_err(&port->portdev->vdev->dev,
1111 "Error %d adding cdev for port %u\n", err, id);
Amit Shahd22a6982010-09-02 18:20:59 +05301112 goto free_cdev;
Amit Shahc446f8f2010-05-19 22:15:48 -06001113 }
1114 port->dev = device_create(pdrvdata.class, &port->portdev->vdev->dev,
1115 devt, port, "vport%up%u",
1116 port->portdev->drv_index, id);
1117 if (IS_ERR(port->dev)) {
1118 err = PTR_ERR(port->dev);
1119 dev_err(&port->portdev->vdev->dev,
1120 "Error %d creating device for port %u\n",
1121 err, id);
1122 goto free_cdev;
1123 }
1124
1125 spin_lock_init(&port->inbuf_lock);
Amit Shahcdfadfc2010-05-19 22:15:50 -06001126 spin_lock_init(&port->outvq_lock);
Amit Shahc446f8f2010-05-19 22:15:48 -06001127 init_waitqueue_head(&port->waitqueue);
1128
1129 /* Fill the in_vq with buffers so the host can send us data. */
1130 nr_added_bufs = fill_queue(port->in_vq, &port->inbuf_lock);
1131 if (!nr_added_bufs) {
1132 dev_err(port->dev, "Error allocating inbufs\n");
1133 err = -ENOMEM;
1134 goto free_device;
1135 }
1136
1137 /*
1138 * If we're not using multiport support, this has to be a console port
1139 */
1140 if (!use_multiport(port->portdev)) {
1141 err = init_port_console(port);
1142 if (err)
1143 goto free_inbufs;
1144 }
1145
1146 spin_lock_irq(&portdev->ports_lock);
1147 list_add_tail(&port->list, &port->portdev->ports);
1148 spin_unlock_irq(&portdev->ports_lock);
1149
1150 /*
1151 * Tell the Host we're set so that it can send us various
1152 * configuration parameters for this port (eg, port name,
1153 * caching, whether this is a console port, etc.)
1154 */
1155 send_control_msg(port, VIRTIO_CONSOLE_PORT_READY, 1);
1156
1157 if (pdrvdata.debugfs_dir) {
1158 /*
1159 * Finally, create the debugfs file that we can use to
1160 * inspect a port's state at any time
1161 */
1162 sprintf(debugfs_name, "vport%up%u",
1163 port->portdev->drv_index, id);
1164 port->debugfs_file = debugfs_create_file(debugfs_name, 0444,
1165 pdrvdata.debugfs_dir,
1166 port,
1167 &port_debugfs_ops);
1168 }
1169 return 0;
1170
1171free_inbufs:
1172 while ((buf = virtqueue_detach_unused_buf(port->in_vq)))
1173 free_buf(buf);
1174free_device:
1175 device_destroy(pdrvdata.class, port->dev->devt);
1176free_cdev:
Amit Shahd22a6982010-09-02 18:20:59 +05301177 cdev_del(port->cdev);
Amit Shahc446f8f2010-05-19 22:15:48 -06001178free_port:
1179 kfree(port);
1180fail:
1181 /* The host might want to notify management sw about port add failure */
Julia Lawall0643e4c2010-05-15 11:45:53 +02001182 __send_control_msg(portdev, id, VIRTIO_CONSOLE_PORT_READY, 0);
Amit Shahc446f8f2010-05-19 22:15:48 -06001183 return err;
1184}
1185
Amit Shah1f7aa422009-12-21 22:27:31 +05301186/* Remove all port-specific data. */
Amit Shah7a285312010-09-02 18:11:47 +05301187static void remove_port(struct port *port)
Amit Shah1f7aa422009-12-21 22:27:31 +05301188{
Amit Shaha9cdd482010-02-12 10:32:15 +05301189 struct port_buffer *buf;
1190
Amit Shah00476342010-05-27 13:24:39 +05301191 if (port->guest_connected) {
1192 port->guest_connected = false;
1193 port->host_connected = false;
1194 wake_up_interruptible(&port->waitqueue);
1195 send_control_msg(port, VIRTIO_CONSOLE_PORT_OPEN, 0);
1196 }
1197
Amit Shah1f7aa422009-12-21 22:27:31 +05301198 spin_lock_irq(&port->portdev->ports_lock);
1199 list_del(&port->list);
1200 spin_unlock_irq(&port->portdev->ports_lock);
1201
1202 if (is_console_port(port)) {
1203 spin_lock_irq(&pdrvdata_lock);
1204 list_del(&port->cons.list);
1205 spin_unlock_irq(&pdrvdata_lock);
Amit Shah69eb9a92010-05-19 22:15:47 -06001206#if 0
1207 /*
1208 * hvc_remove() not called as removing one hvc port
1209 * results in other hvc ports getting frozen.
1210 *
1211 * Once this is resolved in hvc, this functionality
1212 * will be enabled. Till that is done, the -EPIPE
1213 * return from get_chars() above will help
1214 * hvc_console.c to clean up on ports we remove here.
1215 */
Amit Shah1f7aa422009-12-21 22:27:31 +05301216 hvc_remove(port->cons.hvc);
Amit Shah69eb9a92010-05-19 22:15:47 -06001217#endif
Amit Shah1f7aa422009-12-21 22:27:31 +05301218 }
Amit Shah1f7aa422009-12-21 22:27:31 +05301219 sysfs_remove_group(&port->dev->kobj, &port_attribute_group);
1220 device_destroy(pdrvdata.class, port->dev->devt);
Amit Shahd22a6982010-09-02 18:20:59 +05301221 cdev_del(port->cdev);
Amit Shah1f7aa422009-12-21 22:27:31 +05301222
Amit Shaha9cdd482010-02-12 10:32:15 +05301223 /* Remove unused data this port might have received. */
Amit Shah1f7aa422009-12-21 22:27:31 +05301224 discard_port_data(port);
Amit Shaha9cdd482010-02-12 10:32:15 +05301225
Amit Shahcdfadfc2010-05-19 22:15:50 -06001226 reclaim_consumed_buffers(port);
1227
Amit Shaha9cdd482010-02-12 10:32:15 +05301228 /* Remove buffers we queued up for the Host to send us data in. */
Michael S. Tsirkin505b0452010-04-12 16:18:32 +03001229 while ((buf = virtqueue_detach_unused_buf(port->in_vq)))
Amit Shaha9cdd482010-02-12 10:32:15 +05301230 free_buf(buf);
1231
Amit Shah1f7aa422009-12-21 22:27:31 +05301232 kfree(port->name);
1233
Amit Shahd99393e2009-12-21 22:36:21 +05301234 debugfs_remove(port->debugfs_file);
1235
Amit Shah1f7aa422009-12-21 22:27:31 +05301236 kfree(port);
Amit Shah1f7aa422009-12-21 22:27:31 +05301237}
1238
Amit Shah17634ba2009-12-21 21:03:25 +05301239/* Any private messages that the Host and Guest want to share */
1240static void handle_control_message(struct ports_device *portdev,
1241 struct port_buffer *buf)
1242{
1243 struct virtio_console_control *cpkt;
1244 struct port *port;
Amit Shah431edb82009-12-21 21:57:40 +05301245 size_t name_size;
1246 int err;
Amit Shah17634ba2009-12-21 21:03:25 +05301247
1248 cpkt = (struct virtio_console_control *)(buf->buf + buf->offset);
1249
1250 port = find_port_by_id(portdev, cpkt->id);
Amit Shahf909f852010-05-19 22:15:48 -06001251 if (!port && cpkt->event != VIRTIO_CONSOLE_PORT_ADD) {
Amit Shah17634ba2009-12-21 21:03:25 +05301252 /* No valid header at start of buffer. Drop it. */
1253 dev_dbg(&portdev->vdev->dev,
1254 "Invalid index %u in control packet\n", cpkt->id);
1255 return;
1256 }
1257
1258 switch (cpkt->event) {
Amit Shahf909f852010-05-19 22:15:48 -06001259 case VIRTIO_CONSOLE_PORT_ADD:
1260 if (port) {
Amit Shah1d051602010-05-19 22:15:49 -06001261 dev_dbg(&portdev->vdev->dev,
1262 "Port %u already added\n", port->id);
Amit Shahf909f852010-05-19 22:15:48 -06001263 send_control_msg(port, VIRTIO_CONSOLE_PORT_READY, 1);
1264 break;
1265 }
1266 if (cpkt->id >= portdev->config.max_nr_ports) {
1267 dev_warn(&portdev->vdev->dev,
1268 "Request for adding port with out-of-bound id %u, max. supported id: %u\n",
1269 cpkt->id, portdev->config.max_nr_ports - 1);
1270 break;
1271 }
1272 add_port(portdev, cpkt->id);
1273 break;
1274 case VIRTIO_CONSOLE_PORT_REMOVE:
1275 remove_port(port);
1276 break;
Amit Shah17634ba2009-12-21 21:03:25 +05301277 case VIRTIO_CONSOLE_CONSOLE_PORT:
1278 if (!cpkt->value)
1279 break;
1280 if (is_console_port(port))
1281 break;
1282
1283 init_port_console(port);
1284 /*
1285 * Could remove the port here in case init fails - but
1286 * have to notify the host first.
1287 */
1288 break;
Amit Shah8345adb2010-05-06 02:05:09 +05301289 case VIRTIO_CONSOLE_RESIZE: {
1290 struct {
1291 __u16 rows;
1292 __u16 cols;
1293 } size;
1294
Amit Shah17634ba2009-12-21 21:03:25 +05301295 if (!is_console_port(port))
1296 break;
Amit Shah8345adb2010-05-06 02:05:09 +05301297
1298 memcpy(&size, buf->buf + buf->offset + sizeof(*cpkt),
1299 sizeof(size));
1300 set_console_size(port, size.rows, size.cols);
1301
Amit Shah17634ba2009-12-21 21:03:25 +05301302 port->cons.hvc->irq_requested = 1;
1303 resize_console(port);
1304 break;
Amit Shah8345adb2010-05-06 02:05:09 +05301305 }
Amit Shah2030fa42009-12-21 21:49:30 +05301306 case VIRTIO_CONSOLE_PORT_OPEN:
1307 port->host_connected = cpkt->value;
1308 wake_up_interruptible(&port->waitqueue);
Amit Shahcdfadfc2010-05-19 22:15:50 -06001309 /*
1310 * If the host port got closed and the host had any
1311 * unconsumed buffers, we'll be able to reclaim them
1312 * now.
1313 */
1314 spin_lock_irq(&port->outvq_lock);
1315 reclaim_consumed_buffers(port);
1316 spin_unlock_irq(&port->outvq_lock);
Amit Shah2030fa42009-12-21 21:49:30 +05301317 break;
Amit Shah431edb82009-12-21 21:57:40 +05301318 case VIRTIO_CONSOLE_PORT_NAME:
1319 /*
1320 * Skip the size of the header and the cpkt to get the size
1321 * of the name that was sent
1322 */
1323 name_size = buf->len - buf->offset - sizeof(*cpkt) + 1;
1324
1325 port->name = kmalloc(name_size, GFP_KERNEL);
1326 if (!port->name) {
1327 dev_err(port->dev,
1328 "Not enough space to store port name\n");
1329 break;
1330 }
1331 strncpy(port->name, buf->buf + buf->offset + sizeof(*cpkt),
1332 name_size - 1);
1333 port->name[name_size - 1] = 0;
1334
1335 /*
1336 * Since we only have one sysfs attribute, 'name',
1337 * create it only if we have a name for the port.
1338 */
1339 err = sysfs_create_group(&port->dev->kobj,
1340 &port_attribute_group);
Amit Shahec642132010-03-19 17:36:43 +05301341 if (err) {
Amit Shah431edb82009-12-21 21:57:40 +05301342 dev_err(port->dev,
1343 "Error %d creating sysfs device attributes\n",
1344 err);
Amit Shahec642132010-03-19 17:36:43 +05301345 } else {
1346 /*
1347 * Generate a udev event so that appropriate
1348 * symlinks can be created based on udev
1349 * rules.
1350 */
1351 kobject_uevent(&port->dev->kobj, KOBJ_CHANGE);
1352 }
Amit Shah431edb82009-12-21 21:57:40 +05301353 break;
Amit Shah17634ba2009-12-21 21:03:25 +05301354 }
1355}
1356
1357static void control_work_handler(struct work_struct *work)
1358{
1359 struct ports_device *portdev;
1360 struct virtqueue *vq;
1361 struct port_buffer *buf;
1362 unsigned int len;
1363
1364 portdev = container_of(work, struct ports_device, control_work);
1365 vq = portdev->c_ivq;
1366
1367 spin_lock(&portdev->cvq_lock);
Michael S. Tsirkin505b0452010-04-12 16:18:32 +03001368 while ((buf = virtqueue_get_buf(vq, &len))) {
Amit Shah17634ba2009-12-21 21:03:25 +05301369 spin_unlock(&portdev->cvq_lock);
1370
1371 buf->len = len;
1372 buf->offset = 0;
1373
1374 handle_control_message(portdev, buf);
1375
1376 spin_lock(&portdev->cvq_lock);
1377 if (add_inbuf(portdev->c_ivq, buf) < 0) {
1378 dev_warn(&portdev->vdev->dev,
1379 "Error adding buffer to queue\n");
1380 free_buf(buf);
1381 }
1382 }
1383 spin_unlock(&portdev->cvq_lock);
1384}
1385
1386static void in_intr(struct virtqueue *vq)
1387{
1388 struct port *port;
1389 unsigned long flags;
1390
1391 port = find_port_by_vq(vq->vdev->priv, vq);
1392 if (!port)
1393 return;
1394
1395 spin_lock_irqsave(&port->inbuf_lock, flags);
Amit Shahd6933562010-02-12 10:32:18 +05301396 if (!port->inbuf)
1397 port->inbuf = get_inbuf(port);
Amit Shah17634ba2009-12-21 21:03:25 +05301398
Amit Shah88f251a2009-12-21 22:15:30 +05301399 /*
1400 * Don't queue up data when port is closed. This condition
1401 * can be reached when a console port is not yet connected (no
1402 * tty is spawned) and the host sends out data to console
1403 * ports. For generic serial ports, the host won't
1404 * (shouldn't) send data till the guest is connected.
1405 */
1406 if (!port->guest_connected)
1407 discard_port_data(port);
1408
Amit Shah17634ba2009-12-21 21:03:25 +05301409 spin_unlock_irqrestore(&port->inbuf_lock, flags);
1410
Amit Shah2030fa42009-12-21 21:49:30 +05301411 wake_up_interruptible(&port->waitqueue);
1412
Amit Shah17634ba2009-12-21 21:03:25 +05301413 if (is_console_port(port) && hvc_poll(port->cons.hvc))
1414 hvc_kick();
1415}
1416
1417static void control_intr(struct virtqueue *vq)
1418{
1419 struct ports_device *portdev;
1420
1421 portdev = vq->vdev->priv;
1422 schedule_work(&portdev->control_work);
1423}
1424
Amit Shah7f5d8102009-12-21 22:22:08 +05301425static void config_intr(struct virtio_device *vdev)
1426{
1427 struct ports_device *portdev;
1428
1429 portdev = vdev->priv;
Amit Shah99f905f2010-05-19 22:15:48 -06001430
Amit Shah4038f5b72010-05-06 02:05:07 +05301431 if (!use_multiport(portdev)) {
Amit Shah97788292010-05-06 02:05:08 +05301432 struct port *port;
1433 u16 rows, cols;
1434
1435 vdev->config->get(vdev,
1436 offsetof(struct virtio_console_config, cols),
1437 &cols, sizeof(u16));
1438 vdev->config->get(vdev,
1439 offsetof(struct virtio_console_config, rows),
1440 &rows, sizeof(u16));
1441
1442 port = find_port_by_id(portdev, 0);
1443 set_console_size(port, rows, cols);
1444
Amit Shah4038f5b72010-05-06 02:05:07 +05301445 /*
1446 * We'll use this way of resizing only for legacy
1447 * support. For newer userspace
1448 * (VIRTIO_CONSOLE_F_MULTPORT+), use control messages
1449 * to indicate console size changes so that it can be
1450 * done per-port.
1451 */
Amit Shah97788292010-05-06 02:05:08 +05301452 resize_console(port);
Amit Shah4038f5b72010-05-06 02:05:07 +05301453 }
Amit Shah7f5d8102009-12-21 22:22:08 +05301454}
1455
Amit Shah2658a79a2010-01-18 19:15:11 +05301456static int init_vqs(struct ports_device *portdev)
1457{
1458 vq_callback_t **io_callbacks;
1459 char **io_names;
1460 struct virtqueue **vqs;
Amit Shah17634ba2009-12-21 21:03:25 +05301461 u32 i, j, nr_ports, nr_queues;
Amit Shah2658a79a2010-01-18 19:15:11 +05301462 int err;
1463
Amit Shah17634ba2009-12-21 21:03:25 +05301464 nr_ports = portdev->config.max_nr_ports;
1465 nr_queues = use_multiport(portdev) ? (nr_ports + 1) * 2 : 2;
Amit Shah2658a79a2010-01-18 19:15:11 +05301466
1467 vqs = kmalloc(nr_queues * sizeof(struct virtqueue *), GFP_KERNEL);
1468 if (!vqs) {
1469 err = -ENOMEM;
1470 goto fail;
1471 }
1472 io_callbacks = kmalloc(nr_queues * sizeof(vq_callback_t *), GFP_KERNEL);
1473 if (!io_callbacks) {
1474 err = -ENOMEM;
1475 goto free_vqs;
1476 }
1477 io_names = kmalloc(nr_queues * sizeof(char *), GFP_KERNEL);
1478 if (!io_names) {
1479 err = -ENOMEM;
1480 goto free_callbacks;
1481 }
1482 portdev->in_vqs = kmalloc(nr_ports * sizeof(struct virtqueue *),
1483 GFP_KERNEL);
1484 if (!portdev->in_vqs) {
1485 err = -ENOMEM;
1486 goto free_names;
1487 }
1488 portdev->out_vqs = kmalloc(nr_ports * sizeof(struct virtqueue *),
1489 GFP_KERNEL);
1490 if (!portdev->out_vqs) {
1491 err = -ENOMEM;
1492 goto free_invqs;
1493 }
1494
Amit Shah17634ba2009-12-21 21:03:25 +05301495 /*
1496 * For backward compat (newer host but older guest), the host
1497 * spawns a console port first and also inits the vqs for port
1498 * 0 before others.
1499 */
1500 j = 0;
1501 io_callbacks[j] = in_intr;
1502 io_callbacks[j + 1] = NULL;
1503 io_names[j] = "input";
1504 io_names[j + 1] = "output";
1505 j += 2;
Amit Shah2658a79a2010-01-18 19:15:11 +05301506
Amit Shah17634ba2009-12-21 21:03:25 +05301507 if (use_multiport(portdev)) {
1508 io_callbacks[j] = control_intr;
1509 io_callbacks[j + 1] = NULL;
1510 io_names[j] = "control-i";
1511 io_names[j + 1] = "control-o";
1512
1513 for (i = 1; i < nr_ports; i++) {
1514 j += 2;
1515 io_callbacks[j] = in_intr;
1516 io_callbacks[j + 1] = NULL;
1517 io_names[j] = "input";
1518 io_names[j + 1] = "output";
1519 }
1520 }
Amit Shah2658a79a2010-01-18 19:15:11 +05301521 /* Find the queues. */
1522 err = portdev->vdev->config->find_vqs(portdev->vdev, nr_queues, vqs,
1523 io_callbacks,
1524 (const char **)io_names);
1525 if (err)
1526 goto free_outvqs;
1527
Amit Shah17634ba2009-12-21 21:03:25 +05301528 j = 0;
Amit Shah2658a79a2010-01-18 19:15:11 +05301529 portdev->in_vqs[0] = vqs[0];
1530 portdev->out_vqs[0] = vqs[1];
Amit Shah17634ba2009-12-21 21:03:25 +05301531 j += 2;
1532 if (use_multiport(portdev)) {
1533 portdev->c_ivq = vqs[j];
1534 portdev->c_ovq = vqs[j + 1];
Amit Shah2658a79a2010-01-18 19:15:11 +05301535
Amit Shah17634ba2009-12-21 21:03:25 +05301536 for (i = 1; i < nr_ports; i++) {
1537 j += 2;
1538 portdev->in_vqs[i] = vqs[j];
1539 portdev->out_vqs[i] = vqs[j + 1];
1540 }
1541 }
Amit Shah2658a79a2010-01-18 19:15:11 +05301542 kfree(io_callbacks);
1543 kfree(io_names);
1544 kfree(vqs);
1545
1546 return 0;
1547
1548free_names:
1549 kfree(io_names);
1550free_callbacks:
1551 kfree(io_callbacks);
1552free_outvqs:
1553 kfree(portdev->out_vqs);
1554free_invqs:
1555 kfree(portdev->in_vqs);
1556free_vqs:
1557 kfree(vqs);
1558fail:
1559 return err;
1560}
1561
Amit Shahfb08bd22009-12-21 21:36:04 +05301562static const struct file_operations portdev_fops = {
1563 .owner = THIS_MODULE,
1564};
1565
Amit Shah1c85bf32010-01-18 19:15:07 +05301566/*
1567 * Once we're further in boot, we get probed like any other virtio
1568 * device.
Amit Shah17634ba2009-12-21 21:03:25 +05301569 *
1570 * If the host also supports multiple console ports, we check the
1571 * config space to see how many ports the host has spawned. We
1572 * initialize each port found.
Amit Shah1c85bf32010-01-18 19:15:07 +05301573 */
1574static int __devinit virtcons_probe(struct virtio_device *vdev)
1575{
Amit Shah1c85bf32010-01-18 19:15:07 +05301576 struct ports_device *portdev;
1577 int err;
Amit Shah17634ba2009-12-21 21:03:25 +05301578 bool multiport;
Amit Shah1c85bf32010-01-18 19:15:07 +05301579
1580 portdev = kmalloc(sizeof(*portdev), GFP_KERNEL);
1581 if (!portdev) {
1582 err = -ENOMEM;
1583 goto fail;
1584 }
1585
1586 /* Attach this portdev to this virtio_device, and vice-versa. */
1587 portdev->vdev = vdev;
1588 vdev->priv = portdev;
1589
Amit Shahfb08bd22009-12-21 21:36:04 +05301590 spin_lock_irq(&pdrvdata_lock);
1591 portdev->drv_index = pdrvdata.index++;
1592 spin_unlock_irq(&pdrvdata_lock);
1593
1594 portdev->chr_major = register_chrdev(0, "virtio-portsdev",
1595 &portdev_fops);
1596 if (portdev->chr_major < 0) {
1597 dev_err(&vdev->dev,
1598 "Error %d registering chrdev for device %u\n",
1599 portdev->chr_major, portdev->drv_index);
1600 err = portdev->chr_major;
1601 goto free;
1602 }
1603
Amit Shah17634ba2009-12-21 21:03:25 +05301604 multiport = false;
Amit Shah17634ba2009-12-21 21:03:25 +05301605 portdev->config.max_nr_ports = 1;
1606 if (virtio_has_feature(vdev, VIRTIO_CONSOLE_F_MULTIPORT)) {
1607 multiport = true;
1608 vdev->features[0] |= 1 << VIRTIO_CONSOLE_F_MULTIPORT;
1609
Amit Shahb99fa812010-05-19 22:15:46 -06001610 vdev->config->get(vdev, offsetof(struct virtio_console_config,
Amit Shahb99fa812010-05-19 22:15:46 -06001611 max_nr_ports),
Amit Shah17634ba2009-12-21 21:03:25 +05301612 &portdev->config.max_nr_ports,
1613 sizeof(portdev->config.max_nr_ports));
Amit Shah17634ba2009-12-21 21:03:25 +05301614 }
1615
1616 /* Let the Host know we support multiple ports.*/
1617 vdev->config->finalize_features(vdev);
1618
Amit Shah2658a79a2010-01-18 19:15:11 +05301619 err = init_vqs(portdev);
1620 if (err < 0) {
1621 dev_err(&vdev->dev, "Error %d initializing vqs\n", err);
Amit Shahfb08bd22009-12-21 21:36:04 +05301622 goto free_chrdev;
Amit Shah2658a79a2010-01-18 19:15:11 +05301623 }
Amit Shah1c85bf32010-01-18 19:15:07 +05301624
Amit Shah17634ba2009-12-21 21:03:25 +05301625 spin_lock_init(&portdev->ports_lock);
1626 INIT_LIST_HEAD(&portdev->ports);
1627
1628 if (multiport) {
Amit Shah335a64a5c2010-02-24 10:37:44 +05301629 unsigned int nr_added_bufs;
1630
Amit Shah17634ba2009-12-21 21:03:25 +05301631 spin_lock_init(&portdev->cvq_lock);
1632 INIT_WORK(&portdev->control_work, &control_work_handler);
1633
Amit Shah335a64a5c2010-02-24 10:37:44 +05301634 nr_added_bufs = fill_queue(portdev->c_ivq, &portdev->cvq_lock);
1635 if (!nr_added_bufs) {
Amit Shah22a29ea2010-02-12 10:32:17 +05301636 dev_err(&vdev->dev,
1637 "Error allocating buffers for control queue\n");
1638 err = -ENOMEM;
1639 goto free_vqs;
1640 }
Amit Shah1d051602010-05-19 22:15:49 -06001641 } else {
1642 /*
1643 * For backward compatibility: Create a console port
1644 * if we're running on older host.
1645 */
1646 add_port(portdev, 0);
Amit Shah17634ba2009-12-21 21:03:25 +05301647 }
1648
Amit Shah6bdf2af2010-09-02 18:11:49 +05301649 spin_lock_irq(&pdrvdata_lock);
1650 list_add_tail(&portdev->list, &pdrvdata.portdevs);
1651 spin_unlock_irq(&pdrvdata_lock);
1652
Amit Shahf909f852010-05-19 22:15:48 -06001653 __send_control_msg(portdev, VIRTIO_CONSOLE_BAD_ID,
1654 VIRTIO_CONSOLE_DEVICE_READY, 1);
Rusty Russell31610432007-10-22 11:03:39 +10001655 return 0;
1656
Amit Shah22a29ea2010-02-12 10:32:17 +05301657free_vqs:
Julia Lawall0643e4c2010-05-15 11:45:53 +02001658 /* The host might want to notify mgmt sw about device add failure */
1659 __send_control_msg(portdev, VIRTIO_CONSOLE_BAD_ID,
1660 VIRTIO_CONSOLE_DEVICE_READY, 0);
Amit Shah22a29ea2010-02-12 10:32:17 +05301661 vdev->config->del_vqs(vdev);
1662 kfree(portdev->in_vqs);
1663 kfree(portdev->out_vqs);
Amit Shahfb08bd22009-12-21 21:36:04 +05301664free_chrdev:
1665 unregister_chrdev(portdev->chr_major, "virtio-portsdev");
Rusty Russell31610432007-10-22 11:03:39 +10001666free:
Amit Shah1c85bf32010-01-18 19:15:07 +05301667 kfree(portdev);
Rusty Russell31610432007-10-22 11:03:39 +10001668fail:
1669 return err;
1670}
1671
Amit Shah71778762010-02-12 10:32:16 +05301672static void virtcons_remove(struct virtio_device *vdev)
1673{
1674 struct ports_device *portdev;
1675 struct port *port, *port2;
Amit Shah71778762010-02-12 10:32:16 +05301676
1677 portdev = vdev->priv;
1678
Amit Shah6bdf2af2010-09-02 18:11:49 +05301679 spin_lock_irq(&pdrvdata_lock);
1680 list_del(&portdev->list);
1681 spin_unlock_irq(&pdrvdata_lock);
1682
Amit Shah02238952010-09-02 18:11:40 +05301683 /* Disable interrupts for vqs */
1684 vdev->config->reset(vdev);
1685 /* Finish up work that's lined up */
Amit Shah71778762010-02-12 10:32:16 +05301686 cancel_work_sync(&portdev->control_work);
Amit Shah71778762010-02-12 10:32:16 +05301687
1688 list_for_each_entry_safe(port, port2, &portdev->ports, list)
1689 remove_port(port);
1690
1691 unregister_chrdev(portdev->chr_major, "virtio-portsdev");
1692
Amit Shah96eb8722010-09-02 18:11:41 +05301693 if (use_multiport(portdev)) {
1694 struct port_buffer *buf;
1695 unsigned int len;
Amit Shah71778762010-02-12 10:32:16 +05301696
Amit Shah96eb8722010-09-02 18:11:41 +05301697 while ((buf = virtqueue_get_buf(portdev->c_ivq, &len)))
1698 free_buf(buf);
1699
1700 while ((buf = virtqueue_detach_unused_buf(portdev->c_ivq)))
1701 free_buf(buf);
1702 }
Amit Shah71778762010-02-12 10:32:16 +05301703
1704 vdev->config->del_vqs(vdev);
1705 kfree(portdev->in_vqs);
1706 kfree(portdev->out_vqs);
1707
1708 kfree(portdev);
1709}
1710
Rusty Russell31610432007-10-22 11:03:39 +10001711static struct virtio_device_id id_table[] = {
1712 { VIRTIO_ID_CONSOLE, VIRTIO_DEV_ANY_ID },
1713 { 0 },
1714};
1715
Christian Borntraegerc2983452008-11-25 13:36:26 +01001716static unsigned int features[] = {
1717 VIRTIO_CONSOLE_F_SIZE,
Amit Shahb99fa812010-05-19 22:15:46 -06001718 VIRTIO_CONSOLE_F_MULTIPORT,
Christian Borntraegerc2983452008-11-25 13:36:26 +01001719};
1720
Rusty Russell31610432007-10-22 11:03:39 +10001721static struct virtio_driver virtio_console = {
Christian Borntraegerc2983452008-11-25 13:36:26 +01001722 .feature_table = features,
1723 .feature_table_size = ARRAY_SIZE(features),
Rusty Russell31610432007-10-22 11:03:39 +10001724 .driver.name = KBUILD_MODNAME,
1725 .driver.owner = THIS_MODULE,
1726 .id_table = id_table,
1727 .probe = virtcons_probe,
Amit Shah71778762010-02-12 10:32:16 +05301728 .remove = virtcons_remove,
Amit Shah7f5d8102009-12-21 22:22:08 +05301729 .config_changed = config_intr,
Rusty Russell31610432007-10-22 11:03:39 +10001730};
1731
1732static int __init init(void)
1733{
Amit Shahfb08bd22009-12-21 21:36:04 +05301734 int err;
1735
1736 pdrvdata.class = class_create(THIS_MODULE, "virtio-ports");
1737 if (IS_ERR(pdrvdata.class)) {
1738 err = PTR_ERR(pdrvdata.class);
1739 pr_err("Error %d creating virtio-ports class\n", err);
1740 return err;
1741 }
Amit Shahd99393e2009-12-21 22:36:21 +05301742
1743 pdrvdata.debugfs_dir = debugfs_create_dir("virtio-ports", NULL);
1744 if (!pdrvdata.debugfs_dir) {
1745 pr_warning("Error %ld creating debugfs dir for virtio-ports\n",
1746 PTR_ERR(pdrvdata.debugfs_dir));
1747 }
Amit Shah38edf582010-01-18 19:15:05 +05301748 INIT_LIST_HEAD(&pdrvdata.consoles);
Amit Shah6bdf2af2010-09-02 18:11:49 +05301749 INIT_LIST_HEAD(&pdrvdata.portdevs);
Amit Shah38edf582010-01-18 19:15:05 +05301750
Rusty Russell31610432007-10-22 11:03:39 +10001751 return register_virtio_driver(&virtio_console);
1752}
Amit Shah71778762010-02-12 10:32:16 +05301753
1754static void __exit fini(void)
1755{
1756 unregister_virtio_driver(&virtio_console);
1757
1758 class_destroy(pdrvdata.class);
1759 if (pdrvdata.debugfs_dir)
1760 debugfs_remove_recursive(pdrvdata.debugfs_dir);
1761}
Rusty Russell31610432007-10-22 11:03:39 +10001762module_init(init);
Amit Shah71778762010-02-12 10:32:16 +05301763module_exit(fini);
Rusty Russell31610432007-10-22 11:03:39 +10001764
1765MODULE_DEVICE_TABLE(virtio, id_table);
1766MODULE_DESCRIPTION("Virtio console driver");
1767MODULE_LICENSE("GPL");