virtio: console: don't rely on virtqueue_add_buf() returning capacity.
[cascardo/linux.git] / drivers / char / virtio_console.c
1 /*
2  * Copyright (C) 2006, 2007, 2009 Rusty Russell, IBM Corporation
3  * Copyright (C) 2009, 2010, 2011 Red Hat, Inc.
4  * Copyright (C) 2009, 2010, 2011 Amit Shah <amit.shah@redhat.com>
5  *
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  */
20 #include <linux/cdev.h>
21 #include <linux/debugfs.h>
22 #include <linux/completion.h>
23 #include <linux/device.h>
24 #include <linux/err.h>
25 #include <linux/freezer.h>
26 #include <linux/fs.h>
27 #include <linux/splice.h>
28 #include <linux/pagemap.h>
29 #include <linux/init.h>
30 #include <linux/list.h>
31 #include <linux/poll.h>
32 #include <linux/sched.h>
33 #include <linux/slab.h>
34 #include <linux/spinlock.h>
35 #include <linux/virtio.h>
36 #include <linux/virtio_console.h>
37 #include <linux/wait.h>
38 #include <linux/workqueue.h>
39 #include <linux/module.h>
40 #include "../tty/hvc/hvc_console.h"
41
42 /*
43  * This is a global struct for storing common data for all the devices
44  * this driver handles.
45  *
46  * Mainly, it has a linked list for all the consoles in one place so
47  * that callbacks from hvc for get_chars(), put_chars() work properly
48  * across multiple devices and multiple ports per device.
49  */
50 struct ports_driver_data {
51         /* Used for registering chardevs */
52         struct class *class;
53
54         /* Used for exporting per-port information to debugfs */
55         struct dentry *debugfs_dir;
56
57         /* List of all the devices we're handling */
58         struct list_head portdevs;
59
60         /* Number of devices this driver is handling */
61         unsigned int index;
62
63         /*
64          * This is used to keep track of the number of hvc consoles
65          * spawned by this driver.  This number is given as the first
66          * argument to hvc_alloc().  To correctly map an initial
67          * console spawned via hvc_instantiate to the console being
68          * hooked up via hvc_alloc, we need to pass the same vtermno.
69          *
70          * We also just assume the first console being initialised was
71          * the first one that got used as the initial console.
72          */
73         unsigned int next_vtermno;
74
75         /* All the console devices handled by this driver */
76         struct list_head consoles;
77 };
78 static struct ports_driver_data pdrvdata;
79
80 DEFINE_SPINLOCK(pdrvdata_lock);
81 DECLARE_COMPLETION(early_console_added);
82
83 /* This struct holds information that's relevant only for console ports */
84 struct console {
85         /* We'll place all consoles in a list in the pdrvdata struct */
86         struct list_head list;
87
88         /* The hvc device associated with this console port */
89         struct hvc_struct *hvc;
90
91         /* The size of the console */
92         struct winsize ws;
93
94         /*
95          * This number identifies the number that we used to register
96          * with hvc in hvc_instantiate() and hvc_alloc(); this is the
97          * number passed on by the hvc callbacks to us to
98          * differentiate between the other console ports handled by
99          * this driver
100          */
101         u32 vtermno;
102 };
103
104 struct port_buffer {
105         char *buf;
106
107         /* size of the buffer in *buf above */
108         size_t size;
109
110         /* used length of the buffer */
111         size_t len;
112         /* offset in the buf from which to consume data */
113         size_t offset;
114 };
115
116 /*
117  * This is a per-device struct that stores data common to all the
118  * ports for that device (vdev->priv).
119  */
120 struct ports_device {
121         /* Next portdev in the list, head is in the pdrvdata struct */
122         struct list_head list;
123
124         /*
125          * Workqueue handlers where we process deferred work after
126          * notification
127          */
128         struct work_struct control_work;
129
130         struct list_head ports;
131
132         /* To protect the list of ports */
133         spinlock_t ports_lock;
134
135         /* To protect the vq operations for the control channel */
136         spinlock_t cvq_lock;
137
138         /* The current config space is stored here */
139         struct virtio_console_config config;
140
141         /* The virtio device we're associated with */
142         struct virtio_device *vdev;
143
144         /*
145          * A couple of virtqueues for the control channel: one for
146          * guest->host transfers, one for host->guest transfers
147          */
148         struct virtqueue *c_ivq, *c_ovq;
149
150         /* Array of per-port IO virtqueues */
151         struct virtqueue **in_vqs, **out_vqs;
152
153         /* Used for numbering devices for sysfs and debugfs */
154         unsigned int drv_index;
155
156         /* Major number for this device.  Ports will be created as minors. */
157         int chr_major;
158 };
159
160 struct port_stats {
161         unsigned long bytes_sent, bytes_received, bytes_discarded;
162 };
163
164 /* This struct holds the per-port data */
165 struct port {
166         /* Next port in the list, head is in the ports_device */
167         struct list_head list;
168
169         /* Pointer to the parent virtio_console device */
170         struct ports_device *portdev;
171
172         /* The current buffer from which data has to be fed to readers */
173         struct port_buffer *inbuf;
174
175         /*
176          * To protect the operations on the in_vq associated with this
177          * port.  Has to be a spinlock because it can be called from
178          * interrupt context (get_char()).
179          */
180         spinlock_t inbuf_lock;
181
182         /* Protect the operations on the out_vq. */
183         spinlock_t outvq_lock;
184
185         /* The IO vqs for this port */
186         struct virtqueue *in_vq, *out_vq;
187
188         /* File in the debugfs directory that exposes this port's information */
189         struct dentry *debugfs_file;
190
191         /*
192          * Keep count of the bytes sent, received and discarded for
193          * this port for accounting and debugging purposes.  These
194          * counts are not reset across port open / close events.
195          */
196         struct port_stats stats;
197
198         /*
199          * The entries in this struct will be valid if this port is
200          * hooked up to an hvc console
201          */
202         struct console cons;
203
204         /* Each port associates with a separate char device */
205         struct cdev *cdev;
206         struct device *dev;
207
208         /* Reference-counting to handle port hot-unplugs and file operations */
209         struct kref kref;
210
211         /* A waitqueue for poll() or blocking read operations */
212         wait_queue_head_t waitqueue;
213
214         /* The 'name' of the port that we expose via sysfs properties */
215         char *name;
216
217         /* We can notify apps of host connect / disconnect events via SIGIO */
218         struct fasync_struct *async_queue;
219
220         /* The 'id' to identify the port with the Host */
221         u32 id;
222
223         bool outvq_full;
224
225         /* Is the host device open */
226         bool host_connected;
227
228         /* We should allow only one process to open a port */
229         bool guest_connected;
230 };
231
232 /* This is the very early arch-specified put chars function. */
233 static int (*early_put_chars)(u32, const char *, int);
234
235 static struct port *find_port_by_vtermno(u32 vtermno)
236 {
237         struct port *port;
238         struct console *cons;
239         unsigned long flags;
240
241         spin_lock_irqsave(&pdrvdata_lock, flags);
242         list_for_each_entry(cons, &pdrvdata.consoles, list) {
243                 if (cons->vtermno == vtermno) {
244                         port = container_of(cons, struct port, cons);
245                         goto out;
246                 }
247         }
248         port = NULL;
249 out:
250         spin_unlock_irqrestore(&pdrvdata_lock, flags);
251         return port;
252 }
253
254 static struct port *find_port_by_devt_in_portdev(struct ports_device *portdev,
255                                                  dev_t dev)
256 {
257         struct port *port;
258         unsigned long flags;
259
260         spin_lock_irqsave(&portdev->ports_lock, flags);
261         list_for_each_entry(port, &portdev->ports, list)
262                 if (port->cdev->dev == dev)
263                         goto out;
264         port = NULL;
265 out:
266         spin_unlock_irqrestore(&portdev->ports_lock, flags);
267
268         return port;
269 }
270
271 static struct port *find_port_by_devt(dev_t dev)
272 {
273         struct ports_device *portdev;
274         struct port *port;
275         unsigned long flags;
276
277         spin_lock_irqsave(&pdrvdata_lock, flags);
278         list_for_each_entry(portdev, &pdrvdata.portdevs, list) {
279                 port = find_port_by_devt_in_portdev(portdev, dev);
280                 if (port)
281                         goto out;
282         }
283         port = NULL;
284 out:
285         spin_unlock_irqrestore(&pdrvdata_lock, flags);
286         return port;
287 }
288
289 static struct port *find_port_by_id(struct ports_device *portdev, u32 id)
290 {
291         struct port *port;
292         unsigned long flags;
293
294         spin_lock_irqsave(&portdev->ports_lock, flags);
295         list_for_each_entry(port, &portdev->ports, list)
296                 if (port->id == id)
297                         goto out;
298         port = NULL;
299 out:
300         spin_unlock_irqrestore(&portdev->ports_lock, flags);
301
302         return port;
303 }
304
305 static struct port *find_port_by_vq(struct ports_device *portdev,
306                                     struct virtqueue *vq)
307 {
308         struct port *port;
309         unsigned long flags;
310
311         spin_lock_irqsave(&portdev->ports_lock, flags);
312         list_for_each_entry(port, &portdev->ports, list)
313                 if (port->in_vq == vq || port->out_vq == vq)
314                         goto out;
315         port = NULL;
316 out:
317         spin_unlock_irqrestore(&portdev->ports_lock, flags);
318         return port;
319 }
320
321 static bool is_console_port(struct port *port)
322 {
323         if (port->cons.hvc)
324                 return true;
325         return false;
326 }
327
328 static inline bool use_multiport(struct ports_device *portdev)
329 {
330         /*
331          * This condition can be true when put_chars is called from
332          * early_init
333          */
334         if (!portdev->vdev)
335                 return 0;
336         return portdev->vdev->features[0] & (1 << VIRTIO_CONSOLE_F_MULTIPORT);
337 }
338
339 static void free_buf(struct port_buffer *buf)
340 {
341         kfree(buf->buf);
342         kfree(buf);
343 }
344
345 static struct port_buffer *alloc_buf(size_t buf_size)
346 {
347         struct port_buffer *buf;
348
349         buf = kmalloc(sizeof(*buf), GFP_KERNEL);
350         if (!buf)
351                 goto fail;
352         buf->buf = kzalloc(buf_size, GFP_KERNEL);
353         if (!buf->buf)
354                 goto free_buf;
355         buf->len = 0;
356         buf->offset = 0;
357         buf->size = buf_size;
358         return buf;
359
360 free_buf:
361         kfree(buf);
362 fail:
363         return NULL;
364 }
365
366 /* Callers should take appropriate locks */
367 static struct port_buffer *get_inbuf(struct port *port)
368 {
369         struct port_buffer *buf;
370         unsigned int len;
371
372         if (port->inbuf)
373                 return port->inbuf;
374
375         buf = virtqueue_get_buf(port->in_vq, &len);
376         if (buf) {
377                 buf->len = len;
378                 buf->offset = 0;
379                 port->stats.bytes_received += len;
380         }
381         return buf;
382 }
383
384 /*
385  * Create a scatter-gather list representing our input buffer and put
386  * it in the queue.
387  *
388  * Callers should take appropriate locks.
389  */
390 static int add_inbuf(struct virtqueue *vq, struct port_buffer *buf)
391 {
392         struct scatterlist sg[1];
393         int ret;
394
395         sg_init_one(sg, buf->buf, buf->size);
396
397         ret = virtqueue_add_buf(vq, sg, 0, 1, buf, GFP_ATOMIC);
398         virtqueue_kick(vq);
399         if (!ret)
400                 ret = vq->num_free;
401         return ret;
402 }
403
404 /* Discard any unread data this port has. Callers lockers. */
405 static void discard_port_data(struct port *port)
406 {
407         struct port_buffer *buf;
408         unsigned int err;
409
410         if (!port->portdev) {
411                 /* Device has been unplugged.  vqs are already gone. */
412                 return;
413         }
414         buf = get_inbuf(port);
415
416         err = 0;
417         while (buf) {
418                 port->stats.bytes_discarded += buf->len - buf->offset;
419                 if (add_inbuf(port->in_vq, buf) < 0) {
420                         err++;
421                         free_buf(buf);
422                 }
423                 port->inbuf = NULL;
424                 buf = get_inbuf(port);
425         }
426         if (err)
427                 dev_warn(port->dev, "Errors adding %d buffers back to vq\n",
428                          err);
429 }
430
431 static bool port_has_data(struct port *port)
432 {
433         unsigned long flags;
434         bool ret;
435
436         ret = false;
437         spin_lock_irqsave(&port->inbuf_lock, flags);
438         port->inbuf = get_inbuf(port);
439         if (port->inbuf)
440                 ret = true;
441
442         spin_unlock_irqrestore(&port->inbuf_lock, flags);
443         return ret;
444 }
445
446 static ssize_t __send_control_msg(struct ports_device *portdev, u32 port_id,
447                                   unsigned int event, unsigned int value)
448 {
449         struct scatterlist sg[1];
450         struct virtio_console_control cpkt;
451         struct virtqueue *vq;
452         unsigned int len;
453
454         if (!use_multiport(portdev))
455                 return 0;
456
457         cpkt.id = port_id;
458         cpkt.event = event;
459         cpkt.value = value;
460
461         vq = portdev->c_ovq;
462
463         sg_init_one(sg, &cpkt, sizeof(cpkt));
464         if (virtqueue_add_buf(vq, sg, 1, 0, &cpkt, GFP_ATOMIC) >= 0) {
465                 virtqueue_kick(vq);
466                 while (!virtqueue_get_buf(vq, &len))
467                         cpu_relax();
468         }
469         return 0;
470 }
471
472 static ssize_t send_control_msg(struct port *port, unsigned int event,
473                                 unsigned int value)
474 {
475         /* Did the port get unplugged before userspace closed it? */
476         if (port->portdev)
477                 return __send_control_msg(port->portdev, port->id, event, value);
478         return 0;
479 }
480
481 struct buffer_token {
482         union {
483                 void *buf;
484                 struct scatterlist *sg;
485         } u;
486         /* If sgpages == 0 then buf is used, else sg is used */
487         unsigned int sgpages;
488 };
489
490 static void reclaim_sg_pages(struct scatterlist *sg, unsigned int nrpages)
491 {
492         int i;
493         struct page *page;
494
495         for (i = 0; i < nrpages; i++) {
496                 page = sg_page(&sg[i]);
497                 if (!page)
498                         break;
499                 put_page(page);
500         }
501         kfree(sg);
502 }
503
504 /* Callers must take the port->outvq_lock */
505 static void reclaim_consumed_buffers(struct port *port)
506 {
507         struct buffer_token *tok;
508         unsigned int len;
509
510         if (!port->portdev) {
511                 /* Device has been unplugged.  vqs are already gone. */
512                 return;
513         }
514         while ((tok = virtqueue_get_buf(port->out_vq, &len))) {
515                 if (tok->sgpages)
516                         reclaim_sg_pages(tok->u.sg, tok->sgpages);
517                 else
518                         kfree(tok->u.buf);
519                 kfree(tok);
520                 port->outvq_full = false;
521         }
522 }
523
524 static ssize_t __send_to_port(struct port *port, struct scatterlist *sg,
525                               int nents, size_t in_count,
526                               struct buffer_token *tok, bool nonblock)
527 {
528         struct virtqueue *out_vq;
529         ssize_t ret;
530         unsigned long flags;
531         unsigned int len;
532
533         out_vq = port->out_vq;
534
535         spin_lock_irqsave(&port->outvq_lock, flags);
536
537         reclaim_consumed_buffers(port);
538
539         ret = virtqueue_add_buf(out_vq, sg, nents, 0, tok, GFP_ATOMIC);
540
541         /* Tell Host to go! */
542         virtqueue_kick(out_vq);
543
544         if (ret < 0) {
545                 in_count = 0;
546                 goto done;
547         }
548
549         if (ret == 0)
550                 port->outvq_full = true;
551
552         if (nonblock)
553                 goto done;
554
555         /*
556          * Wait till the host acknowledges it pushed out the data we
557          * sent.  This is done for data from the hvc_console; the tty
558          * operations are performed with spinlocks held so we can't
559          * sleep here.  An alternative would be to copy the data to a
560          * buffer and relax the spinning requirement.  The downside is
561          * we need to kmalloc a GFP_ATOMIC buffer each time the
562          * console driver writes something out.
563          */
564         while (!virtqueue_get_buf(out_vq, &len))
565                 cpu_relax();
566 done:
567         spin_unlock_irqrestore(&port->outvq_lock, flags);
568
569         port->stats.bytes_sent += in_count;
570         /*
571          * We're expected to return the amount of data we wrote -- all
572          * of it
573          */
574         return in_count;
575 }
576
577 static ssize_t send_buf(struct port *port, void *in_buf, size_t in_count,
578                         bool nonblock)
579 {
580         struct scatterlist sg[1];
581         struct buffer_token *tok;
582
583         tok = kmalloc(sizeof(*tok), GFP_ATOMIC);
584         if (!tok)
585                 return -ENOMEM;
586         tok->sgpages = 0;
587         tok->u.buf = in_buf;
588
589         sg_init_one(sg, in_buf, in_count);
590
591         return __send_to_port(port, sg, 1, in_count, tok, nonblock);
592 }
593
594 static ssize_t send_pages(struct port *port, struct scatterlist *sg, int nents,
595                           size_t in_count, bool nonblock)
596 {
597         struct buffer_token *tok;
598
599         tok = kmalloc(sizeof(*tok), GFP_ATOMIC);
600         if (!tok)
601                 return -ENOMEM;
602         tok->sgpages = nents;
603         tok->u.sg = sg;
604
605         return __send_to_port(port, sg, nents, in_count, tok, nonblock);
606 }
607
608 /*
609  * Give out the data that's requested from the buffer that we have
610  * queued up.
611  */
612 static ssize_t fill_readbuf(struct port *port, char *out_buf, size_t out_count,
613                             bool to_user)
614 {
615         struct port_buffer *buf;
616         unsigned long flags;
617
618         if (!out_count || !port_has_data(port))
619                 return 0;
620
621         buf = port->inbuf;
622         out_count = min(out_count, buf->len - buf->offset);
623
624         if (to_user) {
625                 ssize_t ret;
626
627                 ret = copy_to_user(out_buf, buf->buf + buf->offset, out_count);
628                 if (ret)
629                         return -EFAULT;
630         } else {
631                 memcpy(out_buf, buf->buf + buf->offset, out_count);
632         }
633
634         buf->offset += out_count;
635
636         if (buf->offset == buf->len) {
637                 /*
638                  * We're done using all the data in this buffer.
639                  * Re-queue so that the Host can send us more data.
640                  */
641                 spin_lock_irqsave(&port->inbuf_lock, flags);
642                 port->inbuf = NULL;
643
644                 if (add_inbuf(port->in_vq, buf) < 0)
645                         dev_warn(port->dev, "failed add_buf\n");
646
647                 spin_unlock_irqrestore(&port->inbuf_lock, flags);
648         }
649         /* Return the number of bytes actually copied */
650         return out_count;
651 }
652
653 /* The condition that must be true for polling to end */
654 static bool will_read_block(struct port *port)
655 {
656         if (!port->guest_connected) {
657                 /* Port got hot-unplugged. Let's exit. */
658                 return false;
659         }
660         return !port_has_data(port) && port->host_connected;
661 }
662
663 static bool will_write_block(struct port *port)
664 {
665         bool ret;
666
667         if (!port->guest_connected) {
668                 /* Port got hot-unplugged. Let's exit. */
669                 return false;
670         }
671         if (!port->host_connected)
672                 return true;
673
674         spin_lock_irq(&port->outvq_lock);
675         /*
676          * Check if the Host has consumed any buffers since we last
677          * sent data (this is only applicable for nonblocking ports).
678          */
679         reclaim_consumed_buffers(port);
680         ret = port->outvq_full;
681         spin_unlock_irq(&port->outvq_lock);
682
683         return ret;
684 }
685
686 static ssize_t port_fops_read(struct file *filp, char __user *ubuf,
687                               size_t count, loff_t *offp)
688 {
689         struct port *port;
690         ssize_t ret;
691
692         port = filp->private_data;
693
694         if (!port_has_data(port)) {
695                 /*
696                  * If nothing's connected on the host just return 0 in
697                  * case of list_empty; this tells the userspace app
698                  * that there's no connection
699                  */
700                 if (!port->host_connected)
701                         return 0;
702                 if (filp->f_flags & O_NONBLOCK)
703                         return -EAGAIN;
704
705                 ret = wait_event_freezable(port->waitqueue,
706                                            !will_read_block(port));
707                 if (ret < 0)
708                         return ret;
709         }
710         /* Port got hot-unplugged. */
711         if (!port->guest_connected)
712                 return -ENODEV;
713         /*
714          * We could've received a disconnection message while we were
715          * waiting for more data.
716          *
717          * This check is not clubbed in the if() statement above as we
718          * might receive some data as well as the host could get
719          * disconnected after we got woken up from our wait.  So we
720          * really want to give off whatever data we have and only then
721          * check for host_connected.
722          */
723         if (!port_has_data(port) && !port->host_connected)
724                 return 0;
725
726         return fill_readbuf(port, ubuf, count, true);
727 }
728
729 static int wait_port_writable(struct port *port, bool nonblock)
730 {
731         int ret;
732
733         if (will_write_block(port)) {
734                 if (nonblock)
735                         return -EAGAIN;
736
737                 ret = wait_event_freezable(port->waitqueue,
738                                            !will_write_block(port));
739                 if (ret < 0)
740                         return ret;
741         }
742         /* Port got hot-unplugged. */
743         if (!port->guest_connected)
744                 return -ENODEV;
745
746         return 0;
747 }
748
749 static ssize_t port_fops_write(struct file *filp, const char __user *ubuf,
750                                size_t count, loff_t *offp)
751 {
752         struct port *port;
753         char *buf;
754         ssize_t ret;
755         bool nonblock;
756
757         /* Userspace could be out to fool us */
758         if (!count)
759                 return 0;
760
761         port = filp->private_data;
762
763         nonblock = filp->f_flags & O_NONBLOCK;
764
765         ret = wait_port_writable(port, nonblock);
766         if (ret < 0)
767                 return ret;
768
769         count = min((size_t)(32 * 1024), count);
770
771         buf = kmalloc(count, GFP_KERNEL);
772         if (!buf)
773                 return -ENOMEM;
774
775         ret = copy_from_user(buf, ubuf, count);
776         if (ret) {
777                 ret = -EFAULT;
778                 goto free_buf;
779         }
780
781         /*
782          * We now ask send_buf() to not spin for generic ports -- we
783          * can re-use the same code path that non-blocking file
784          * descriptors take for blocking file descriptors since the
785          * wait is already done and we're certain the write will go
786          * through to the host.
787          */
788         nonblock = true;
789         ret = send_buf(port, buf, count, nonblock);
790
791         if (nonblock && ret > 0)
792                 goto out;
793
794 free_buf:
795         kfree(buf);
796 out:
797         return ret;
798 }
799
800 struct sg_list {
801         unsigned int n;
802         unsigned int size;
803         size_t len;
804         struct scatterlist *sg;
805 };
806
807 static int pipe_to_sg(struct pipe_inode_info *pipe, struct pipe_buffer *buf,
808                         struct splice_desc *sd)
809 {
810         struct sg_list *sgl = sd->u.data;
811         unsigned int offset, len;
812
813         if (sgl->n == sgl->size)
814                 return 0;
815
816         /* Try lock this page */
817         if (buf->ops->steal(pipe, buf) == 0) {
818                 /* Get reference and unlock page for moving */
819                 get_page(buf->page);
820                 unlock_page(buf->page);
821
822                 len = min(buf->len, sd->len);
823                 sg_set_page(&(sgl->sg[sgl->n]), buf->page, len, buf->offset);
824         } else {
825                 /* Failback to copying a page */
826                 struct page *page = alloc_page(GFP_KERNEL);
827                 char *src = buf->ops->map(pipe, buf, 1);
828                 char *dst;
829
830                 if (!page)
831                         return -ENOMEM;
832                 dst = kmap(page);
833
834                 offset = sd->pos & ~PAGE_MASK;
835
836                 len = sd->len;
837                 if (len + offset > PAGE_SIZE)
838                         len = PAGE_SIZE - offset;
839
840                 memcpy(dst + offset, src + buf->offset, len);
841
842                 kunmap(page);
843                 buf->ops->unmap(pipe, buf, src);
844
845                 sg_set_page(&(sgl->sg[sgl->n]), page, len, offset);
846         }
847         sgl->n++;
848         sgl->len += len;
849
850         return len;
851 }
852
853 /* Faster zero-copy write by splicing */
854 static ssize_t port_fops_splice_write(struct pipe_inode_info *pipe,
855                                       struct file *filp, loff_t *ppos,
856                                       size_t len, unsigned int flags)
857 {
858         struct port *port = filp->private_data;
859         struct sg_list sgl;
860         ssize_t ret;
861         struct splice_desc sd = {
862                 .total_len = len,
863                 .flags = flags,
864                 .pos = *ppos,
865                 .u.data = &sgl,
866         };
867
868         ret = wait_port_writable(port, filp->f_flags & O_NONBLOCK);
869         if (ret < 0)
870                 return ret;
871
872         sgl.n = 0;
873         sgl.len = 0;
874         sgl.size = pipe->nrbufs;
875         sgl.sg = kmalloc(sizeof(struct scatterlist) * sgl.size, GFP_KERNEL);
876         if (unlikely(!sgl.sg))
877                 return -ENOMEM;
878
879         sg_init_table(sgl.sg, sgl.size);
880         ret = __splice_from_pipe(pipe, &sd, pipe_to_sg);
881         if (likely(ret > 0))
882                 ret = send_pages(port, sgl.sg, sgl.n, sgl.len, true);
883
884         return ret;
885 }
886
887 static unsigned int port_fops_poll(struct file *filp, poll_table *wait)
888 {
889         struct port *port;
890         unsigned int ret;
891
892         port = filp->private_data;
893         poll_wait(filp, &port->waitqueue, wait);
894
895         if (!port->guest_connected) {
896                 /* Port got unplugged */
897                 return POLLHUP;
898         }
899         ret = 0;
900         if (!will_read_block(port))
901                 ret |= POLLIN | POLLRDNORM;
902         if (!will_write_block(port))
903                 ret |= POLLOUT;
904         if (!port->host_connected)
905                 ret |= POLLHUP;
906
907         return ret;
908 }
909
910 static void remove_port(struct kref *kref);
911
912 static int port_fops_release(struct inode *inode, struct file *filp)
913 {
914         struct port *port;
915
916         port = filp->private_data;
917
918         /* Notify host of port being closed */
919         send_control_msg(port, VIRTIO_CONSOLE_PORT_OPEN, 0);
920
921         spin_lock_irq(&port->inbuf_lock);
922         port->guest_connected = false;
923
924         discard_port_data(port);
925
926         spin_unlock_irq(&port->inbuf_lock);
927
928         spin_lock_irq(&port->outvq_lock);
929         reclaim_consumed_buffers(port);
930         spin_unlock_irq(&port->outvq_lock);
931
932         /*
933          * Locks aren't necessary here as a port can't be opened after
934          * unplug, and if a port isn't unplugged, a kref would already
935          * exist for the port.  Plus, taking ports_lock here would
936          * create a dependency on other locks taken by functions
937          * inside remove_port if we're the last holder of the port,
938          * creating many problems.
939          */
940         kref_put(&port->kref, remove_port);
941
942         return 0;
943 }
944
945 static int port_fops_open(struct inode *inode, struct file *filp)
946 {
947         struct cdev *cdev = inode->i_cdev;
948         struct port *port;
949         int ret;
950
951         port = find_port_by_devt(cdev->dev);
952         filp->private_data = port;
953
954         /* Prevent against a port getting hot-unplugged at the same time */
955         spin_lock_irq(&port->portdev->ports_lock);
956         kref_get(&port->kref);
957         spin_unlock_irq(&port->portdev->ports_lock);
958
959         /*
960          * Don't allow opening of console port devices -- that's done
961          * via /dev/hvc
962          */
963         if (is_console_port(port)) {
964                 ret = -ENXIO;
965                 goto out;
966         }
967
968         /* Allow only one process to open a particular port at a time */
969         spin_lock_irq(&port->inbuf_lock);
970         if (port->guest_connected) {
971                 spin_unlock_irq(&port->inbuf_lock);
972                 ret = -EMFILE;
973                 goto out;
974         }
975
976         port->guest_connected = true;
977         spin_unlock_irq(&port->inbuf_lock);
978
979         spin_lock_irq(&port->outvq_lock);
980         /*
981          * There might be a chance that we missed reclaiming a few
982          * buffers in the window of the port getting previously closed
983          * and opening now.
984          */
985         reclaim_consumed_buffers(port);
986         spin_unlock_irq(&port->outvq_lock);
987
988         nonseekable_open(inode, filp);
989
990         /* Notify host of port being opened */
991         send_control_msg(filp->private_data, VIRTIO_CONSOLE_PORT_OPEN, 1);
992
993         return 0;
994 out:
995         kref_put(&port->kref, remove_port);
996         return ret;
997 }
998
999 static int port_fops_fasync(int fd, struct file *filp, int mode)
1000 {
1001         struct port *port;
1002
1003         port = filp->private_data;
1004         return fasync_helper(fd, filp, mode, &port->async_queue);
1005 }
1006
1007 /*
1008  * The file operations that we support: programs in the guest can open
1009  * a console device, read from it, write to it, poll for data and
1010  * close it.  The devices are at
1011  *   /dev/vport<device number>p<port number>
1012  */
1013 static const struct file_operations port_fops = {
1014         .owner = THIS_MODULE,
1015         .open  = port_fops_open,
1016         .read  = port_fops_read,
1017         .write = port_fops_write,
1018         .splice_write = port_fops_splice_write,
1019         .poll  = port_fops_poll,
1020         .release = port_fops_release,
1021         .fasync = port_fops_fasync,
1022         .llseek = no_llseek,
1023 };
1024
1025 /*
1026  * The put_chars() callback is pretty straightforward.
1027  *
1028  * We turn the characters into a scatter-gather list, add it to the
1029  * output queue and then kick the Host.  Then we sit here waiting for
1030  * it to finish: inefficient in theory, but in practice
1031  * implementations will do it immediately (lguest's Launcher does).
1032  */
1033 static int put_chars(u32 vtermno, const char *buf, int count)
1034 {
1035         struct port *port;
1036
1037         if (unlikely(early_put_chars))
1038                 return early_put_chars(vtermno, buf, count);
1039
1040         port = find_port_by_vtermno(vtermno);
1041         if (!port)
1042                 return -EPIPE;
1043
1044         return send_buf(port, (void *)buf, count, false);
1045 }
1046
1047 /*
1048  * get_chars() is the callback from the hvc_console infrastructure
1049  * when an interrupt is received.
1050  *
1051  * We call out to fill_readbuf that gets us the required data from the
1052  * buffers that are queued up.
1053  */
1054 static int get_chars(u32 vtermno, char *buf, int count)
1055 {
1056         struct port *port;
1057
1058         /* If we've not set up the port yet, we have no input to give. */
1059         if (unlikely(early_put_chars))
1060                 return 0;
1061
1062         port = find_port_by_vtermno(vtermno);
1063         if (!port)
1064                 return -EPIPE;
1065
1066         /* If we don't have an input queue yet, we can't get input. */
1067         BUG_ON(!port->in_vq);
1068
1069         return fill_readbuf(port, buf, count, false);
1070 }
1071
1072 static void resize_console(struct port *port)
1073 {
1074         struct virtio_device *vdev;
1075
1076         /* The port could have been hot-unplugged */
1077         if (!port || !is_console_port(port))
1078                 return;
1079
1080         vdev = port->portdev->vdev;
1081         if (virtio_has_feature(vdev, VIRTIO_CONSOLE_F_SIZE))
1082                 hvc_resize(port->cons.hvc, port->cons.ws);
1083 }
1084
1085 /* We set the configuration at this point, since we now have a tty */
1086 static int notifier_add_vio(struct hvc_struct *hp, int data)
1087 {
1088         struct port *port;
1089
1090         port = find_port_by_vtermno(hp->vtermno);
1091         if (!port)
1092                 return -EINVAL;
1093
1094         hp->irq_requested = 1;
1095         resize_console(port);
1096
1097         return 0;
1098 }
1099
1100 static void notifier_del_vio(struct hvc_struct *hp, int data)
1101 {
1102         hp->irq_requested = 0;
1103 }
1104
1105 /* The operations for console ports. */
1106 static const struct hv_ops hv_ops = {
1107         .get_chars = get_chars,
1108         .put_chars = put_chars,
1109         .notifier_add = notifier_add_vio,
1110         .notifier_del = notifier_del_vio,
1111         .notifier_hangup = notifier_del_vio,
1112 };
1113
1114 /*
1115  * Console drivers are initialized very early so boot messages can go
1116  * out, so we do things slightly differently from the generic virtio
1117  * initialization of the net and block drivers.
1118  *
1119  * At this stage, the console is output-only.  It's too early to set
1120  * up a virtqueue, so we let the drivers do some boutique early-output
1121  * thing.
1122  */
1123 int __init virtio_cons_early_init(int (*put_chars)(u32, const char *, int))
1124 {
1125         early_put_chars = put_chars;
1126         return hvc_instantiate(0, 0, &hv_ops);
1127 }
1128
1129 int init_port_console(struct port *port)
1130 {
1131         int ret;
1132
1133         /*
1134          * The Host's telling us this port is a console port.  Hook it
1135          * up with an hvc console.
1136          *
1137          * To set up and manage our virtual console, we call
1138          * hvc_alloc().
1139          *
1140          * The first argument of hvc_alloc() is the virtual console
1141          * number.  The second argument is the parameter for the
1142          * notification mechanism (like irq number).  We currently
1143          * leave this as zero, virtqueues have implicit notifications.
1144          *
1145          * The third argument is a "struct hv_ops" containing the
1146          * put_chars() get_chars(), notifier_add() and notifier_del()
1147          * pointers.  The final argument is the output buffer size: we
1148          * can do any size, so we put PAGE_SIZE here.
1149          */
1150         port->cons.vtermno = pdrvdata.next_vtermno;
1151
1152         port->cons.hvc = hvc_alloc(port->cons.vtermno, 0, &hv_ops, PAGE_SIZE);
1153         if (IS_ERR(port->cons.hvc)) {
1154                 ret = PTR_ERR(port->cons.hvc);
1155                 dev_err(port->dev,
1156                         "error %d allocating hvc for port\n", ret);
1157                 port->cons.hvc = NULL;
1158                 return ret;
1159         }
1160         spin_lock_irq(&pdrvdata_lock);
1161         pdrvdata.next_vtermno++;
1162         list_add_tail(&port->cons.list, &pdrvdata.consoles);
1163         spin_unlock_irq(&pdrvdata_lock);
1164         port->guest_connected = true;
1165
1166         /*
1167          * Start using the new console output if this is the first
1168          * console to come up.
1169          */
1170         if (early_put_chars)
1171                 early_put_chars = NULL;
1172
1173         /* Notify host of port being opened */
1174         send_control_msg(port, VIRTIO_CONSOLE_PORT_OPEN, 1);
1175
1176         return 0;
1177 }
1178
1179 static ssize_t show_port_name(struct device *dev,
1180                               struct device_attribute *attr, char *buffer)
1181 {
1182         struct port *port;
1183
1184         port = dev_get_drvdata(dev);
1185
1186         return sprintf(buffer, "%s\n", port->name);
1187 }
1188
1189 static DEVICE_ATTR(name, S_IRUGO, show_port_name, NULL);
1190
1191 static struct attribute *port_sysfs_entries[] = {
1192         &dev_attr_name.attr,
1193         NULL
1194 };
1195
1196 static struct attribute_group port_attribute_group = {
1197         .name = NULL,           /* put in device directory */
1198         .attrs = port_sysfs_entries,
1199 };
1200
1201 static ssize_t debugfs_read(struct file *filp, char __user *ubuf,
1202                             size_t count, loff_t *offp)
1203 {
1204         struct port *port;
1205         char *buf;
1206         ssize_t ret, out_offset, out_count;
1207
1208         out_count = 1024;
1209         buf = kmalloc(out_count, GFP_KERNEL);
1210         if (!buf)
1211                 return -ENOMEM;
1212
1213         port = filp->private_data;
1214         out_offset = 0;
1215         out_offset += snprintf(buf + out_offset, out_count,
1216                                "name: %s\n", port->name ? port->name : "");
1217         out_offset += snprintf(buf + out_offset, out_count - out_offset,
1218                                "guest_connected: %d\n", port->guest_connected);
1219         out_offset += snprintf(buf + out_offset, out_count - out_offset,
1220                                "host_connected: %d\n", port->host_connected);
1221         out_offset += snprintf(buf + out_offset, out_count - out_offset,
1222                                "outvq_full: %d\n", port->outvq_full);
1223         out_offset += snprintf(buf + out_offset, out_count - out_offset,
1224                                "bytes_sent: %lu\n", port->stats.bytes_sent);
1225         out_offset += snprintf(buf + out_offset, out_count - out_offset,
1226                                "bytes_received: %lu\n",
1227                                port->stats.bytes_received);
1228         out_offset += snprintf(buf + out_offset, out_count - out_offset,
1229                                "bytes_discarded: %lu\n",
1230                                port->stats.bytes_discarded);
1231         out_offset += snprintf(buf + out_offset, out_count - out_offset,
1232                                "is_console: %s\n",
1233                                is_console_port(port) ? "yes" : "no");
1234         out_offset += snprintf(buf + out_offset, out_count - out_offset,
1235                                "console_vtermno: %u\n", port->cons.vtermno);
1236
1237         ret = simple_read_from_buffer(ubuf, count, offp, buf, out_offset);
1238         kfree(buf);
1239         return ret;
1240 }
1241
1242 static const struct file_operations port_debugfs_ops = {
1243         .owner = THIS_MODULE,
1244         .open  = simple_open,
1245         .read  = debugfs_read,
1246 };
1247
1248 static void set_console_size(struct port *port, u16 rows, u16 cols)
1249 {
1250         if (!port || !is_console_port(port))
1251                 return;
1252
1253         port->cons.ws.ws_row = rows;
1254         port->cons.ws.ws_col = cols;
1255 }
1256
1257 static unsigned int fill_queue(struct virtqueue *vq, spinlock_t *lock)
1258 {
1259         struct port_buffer *buf;
1260         unsigned int nr_added_bufs;
1261         int ret;
1262
1263         nr_added_bufs = 0;
1264         do {
1265                 buf = alloc_buf(PAGE_SIZE);
1266                 if (!buf)
1267                         break;
1268
1269                 spin_lock_irq(lock);
1270                 ret = add_inbuf(vq, buf);
1271                 if (ret < 0) {
1272                         spin_unlock_irq(lock);
1273                         free_buf(buf);
1274                         break;
1275                 }
1276                 nr_added_bufs++;
1277                 spin_unlock_irq(lock);
1278         } while (ret > 0);
1279
1280         return nr_added_bufs;
1281 }
1282
1283 static void send_sigio_to_port(struct port *port)
1284 {
1285         if (port->async_queue && port->guest_connected)
1286                 kill_fasync(&port->async_queue, SIGIO, POLL_OUT);
1287 }
1288
1289 static int add_port(struct ports_device *portdev, u32 id)
1290 {
1291         char debugfs_name[16];
1292         struct port *port;
1293         struct port_buffer *buf;
1294         dev_t devt;
1295         unsigned int nr_added_bufs;
1296         int err;
1297
1298         port = kmalloc(sizeof(*port), GFP_KERNEL);
1299         if (!port) {
1300                 err = -ENOMEM;
1301                 goto fail;
1302         }
1303         kref_init(&port->kref);
1304
1305         port->portdev = portdev;
1306         port->id = id;
1307
1308         port->name = NULL;
1309         port->inbuf = NULL;
1310         port->cons.hvc = NULL;
1311         port->async_queue = NULL;
1312
1313         port->cons.ws.ws_row = port->cons.ws.ws_col = 0;
1314
1315         port->host_connected = port->guest_connected = false;
1316         port->stats = (struct port_stats) { 0 };
1317
1318         port->outvq_full = false;
1319
1320         port->in_vq = portdev->in_vqs[port->id];
1321         port->out_vq = portdev->out_vqs[port->id];
1322
1323         port->cdev = cdev_alloc();
1324         if (!port->cdev) {
1325                 dev_err(&port->portdev->vdev->dev, "Error allocating cdev\n");
1326                 err = -ENOMEM;
1327                 goto free_port;
1328         }
1329         port->cdev->ops = &port_fops;
1330
1331         devt = MKDEV(portdev->chr_major, id);
1332         err = cdev_add(port->cdev, devt, 1);
1333         if (err < 0) {
1334                 dev_err(&port->portdev->vdev->dev,
1335                         "Error %d adding cdev for port %u\n", err, id);
1336                 goto free_cdev;
1337         }
1338         port->dev = device_create(pdrvdata.class, &port->portdev->vdev->dev,
1339                                   devt, port, "vport%up%u",
1340                                   port->portdev->drv_index, id);
1341         if (IS_ERR(port->dev)) {
1342                 err = PTR_ERR(port->dev);
1343                 dev_err(&port->portdev->vdev->dev,
1344                         "Error %d creating device for port %u\n",
1345                         err, id);
1346                 goto free_cdev;
1347         }
1348
1349         spin_lock_init(&port->inbuf_lock);
1350         spin_lock_init(&port->outvq_lock);
1351         init_waitqueue_head(&port->waitqueue);
1352
1353         /* Fill the in_vq with buffers so the host can send us data. */
1354         nr_added_bufs = fill_queue(port->in_vq, &port->inbuf_lock);
1355         if (!nr_added_bufs) {
1356                 dev_err(port->dev, "Error allocating inbufs\n");
1357                 err = -ENOMEM;
1358                 goto free_device;
1359         }
1360
1361         /*
1362          * If we're not using multiport support, this has to be a console port
1363          */
1364         if (!use_multiport(port->portdev)) {
1365                 err = init_port_console(port);
1366                 if (err)
1367                         goto free_inbufs;
1368         }
1369
1370         spin_lock_irq(&portdev->ports_lock);
1371         list_add_tail(&port->list, &port->portdev->ports);
1372         spin_unlock_irq(&portdev->ports_lock);
1373
1374         /*
1375          * Tell the Host we're set so that it can send us various
1376          * configuration parameters for this port (eg, port name,
1377          * caching, whether this is a console port, etc.)
1378          */
1379         send_control_msg(port, VIRTIO_CONSOLE_PORT_READY, 1);
1380
1381         if (pdrvdata.debugfs_dir) {
1382                 /*
1383                  * Finally, create the debugfs file that we can use to
1384                  * inspect a port's state at any time
1385                  */
1386                 sprintf(debugfs_name, "vport%up%u",
1387                         port->portdev->drv_index, id);
1388                 port->debugfs_file = debugfs_create_file(debugfs_name, 0444,
1389                                                          pdrvdata.debugfs_dir,
1390                                                          port,
1391                                                          &port_debugfs_ops);
1392         }
1393         return 0;
1394
1395 free_inbufs:
1396         while ((buf = virtqueue_detach_unused_buf(port->in_vq)))
1397                 free_buf(buf);
1398 free_device:
1399         device_destroy(pdrvdata.class, port->dev->devt);
1400 free_cdev:
1401         cdev_del(port->cdev);
1402 free_port:
1403         kfree(port);
1404 fail:
1405         /* The host might want to notify management sw about port add failure */
1406         __send_control_msg(portdev, id, VIRTIO_CONSOLE_PORT_READY, 0);
1407         return err;
1408 }
1409
1410 /* No users remain, remove all port-specific data. */
1411 static void remove_port(struct kref *kref)
1412 {
1413         struct port *port;
1414
1415         port = container_of(kref, struct port, kref);
1416
1417         sysfs_remove_group(&port->dev->kobj, &port_attribute_group);
1418         device_destroy(pdrvdata.class, port->dev->devt);
1419         cdev_del(port->cdev);
1420
1421         kfree(port->name);
1422
1423         debugfs_remove(port->debugfs_file);
1424
1425         kfree(port);
1426 }
1427
1428 static void remove_port_data(struct port *port)
1429 {
1430         struct port_buffer *buf;
1431
1432         /* Remove unused data this port might have received. */
1433         discard_port_data(port);
1434
1435         reclaim_consumed_buffers(port);
1436
1437         /* Remove buffers we queued up for the Host to send us data in. */
1438         while ((buf = virtqueue_detach_unused_buf(port->in_vq)))
1439                 free_buf(buf);
1440 }
1441
1442 /*
1443  * Port got unplugged.  Remove port from portdev's list and drop the
1444  * kref reference.  If no userspace has this port opened, it will
1445  * result in immediate removal the port.
1446  */
1447 static void unplug_port(struct port *port)
1448 {
1449         spin_lock_irq(&port->portdev->ports_lock);
1450         list_del(&port->list);
1451         spin_unlock_irq(&port->portdev->ports_lock);
1452
1453         if (port->guest_connected) {
1454                 port->guest_connected = false;
1455                 port->host_connected = false;
1456                 wake_up_interruptible(&port->waitqueue);
1457
1458                 /* Let the app know the port is going down. */
1459                 send_sigio_to_port(port);
1460         }
1461
1462         if (is_console_port(port)) {
1463                 spin_lock_irq(&pdrvdata_lock);
1464                 list_del(&port->cons.list);
1465                 spin_unlock_irq(&pdrvdata_lock);
1466                 hvc_remove(port->cons.hvc);
1467         }
1468
1469         remove_port_data(port);
1470
1471         /*
1472          * We should just assume the device itself has gone off --
1473          * else a close on an open port later will try to send out a
1474          * control message.
1475          */
1476         port->portdev = NULL;
1477
1478         /*
1479          * Locks around here are not necessary - a port can't be
1480          * opened after we removed the port struct from ports_list
1481          * above.
1482          */
1483         kref_put(&port->kref, remove_port);
1484 }
1485
1486 /* Any private messages that the Host and Guest want to share */
1487 static void handle_control_message(struct ports_device *portdev,
1488                                    struct port_buffer *buf)
1489 {
1490         struct virtio_console_control *cpkt;
1491         struct port *port;
1492         size_t name_size;
1493         int err;
1494
1495         cpkt = (struct virtio_console_control *)(buf->buf + buf->offset);
1496
1497         port = find_port_by_id(portdev, cpkt->id);
1498         if (!port && cpkt->event != VIRTIO_CONSOLE_PORT_ADD) {
1499                 /* No valid header at start of buffer.  Drop it. */
1500                 dev_dbg(&portdev->vdev->dev,
1501                         "Invalid index %u in control packet\n", cpkt->id);
1502                 return;
1503         }
1504
1505         switch (cpkt->event) {
1506         case VIRTIO_CONSOLE_PORT_ADD:
1507                 if (port) {
1508                         dev_dbg(&portdev->vdev->dev,
1509                                 "Port %u already added\n", port->id);
1510                         send_control_msg(port, VIRTIO_CONSOLE_PORT_READY, 1);
1511                         break;
1512                 }
1513                 if (cpkt->id >= portdev->config.max_nr_ports) {
1514                         dev_warn(&portdev->vdev->dev,
1515                                 "Request for adding port with out-of-bound id %u, max. supported id: %u\n",
1516                                 cpkt->id, portdev->config.max_nr_ports - 1);
1517                         break;
1518                 }
1519                 add_port(portdev, cpkt->id);
1520                 break;
1521         case VIRTIO_CONSOLE_PORT_REMOVE:
1522                 unplug_port(port);
1523                 break;
1524         case VIRTIO_CONSOLE_CONSOLE_PORT:
1525                 if (!cpkt->value)
1526                         break;
1527                 if (is_console_port(port))
1528                         break;
1529
1530                 init_port_console(port);
1531                 complete(&early_console_added);
1532                 /*
1533                  * Could remove the port here in case init fails - but
1534                  * have to notify the host first.
1535                  */
1536                 break;
1537         case VIRTIO_CONSOLE_RESIZE: {
1538                 struct {
1539                         __u16 rows;
1540                         __u16 cols;
1541                 } size;
1542
1543                 if (!is_console_port(port))
1544                         break;
1545
1546                 memcpy(&size, buf->buf + buf->offset + sizeof(*cpkt),
1547                        sizeof(size));
1548                 set_console_size(port, size.rows, size.cols);
1549
1550                 port->cons.hvc->irq_requested = 1;
1551                 resize_console(port);
1552                 break;
1553         }
1554         case VIRTIO_CONSOLE_PORT_OPEN:
1555                 port->host_connected = cpkt->value;
1556                 wake_up_interruptible(&port->waitqueue);
1557                 /*
1558                  * If the host port got closed and the host had any
1559                  * unconsumed buffers, we'll be able to reclaim them
1560                  * now.
1561                  */
1562                 spin_lock_irq(&port->outvq_lock);
1563                 reclaim_consumed_buffers(port);
1564                 spin_unlock_irq(&port->outvq_lock);
1565
1566                 /*
1567                  * If the guest is connected, it'll be interested in
1568                  * knowing the host connection state changed.
1569                  */
1570                 send_sigio_to_port(port);
1571                 break;
1572         case VIRTIO_CONSOLE_PORT_NAME:
1573                 /*
1574                  * If we woke up after hibernation, we can get this
1575                  * again.  Skip it in that case.
1576                  */
1577                 if (port->name)
1578                         break;
1579
1580                 /*
1581                  * Skip the size of the header and the cpkt to get the size
1582                  * of the name that was sent
1583                  */
1584                 name_size = buf->len - buf->offset - sizeof(*cpkt) + 1;
1585
1586                 port->name = kmalloc(name_size, GFP_KERNEL);
1587                 if (!port->name) {
1588                         dev_err(port->dev,
1589                                 "Not enough space to store port name\n");
1590                         break;
1591                 }
1592                 strncpy(port->name, buf->buf + buf->offset + sizeof(*cpkt),
1593                         name_size - 1);
1594                 port->name[name_size - 1] = 0;
1595
1596                 /*
1597                  * Since we only have one sysfs attribute, 'name',
1598                  * create it only if we have a name for the port.
1599                  */
1600                 err = sysfs_create_group(&port->dev->kobj,
1601                                          &port_attribute_group);
1602                 if (err) {
1603                         dev_err(port->dev,
1604                                 "Error %d creating sysfs device attributes\n",
1605                                 err);
1606                 } else {
1607                         /*
1608                          * Generate a udev event so that appropriate
1609                          * symlinks can be created based on udev
1610                          * rules.
1611                          */
1612                         kobject_uevent(&port->dev->kobj, KOBJ_CHANGE);
1613                 }
1614                 break;
1615         }
1616 }
1617
1618 static void control_work_handler(struct work_struct *work)
1619 {
1620         struct ports_device *portdev;
1621         struct virtqueue *vq;
1622         struct port_buffer *buf;
1623         unsigned int len;
1624
1625         portdev = container_of(work, struct ports_device, control_work);
1626         vq = portdev->c_ivq;
1627
1628         spin_lock(&portdev->cvq_lock);
1629         while ((buf = virtqueue_get_buf(vq, &len))) {
1630                 spin_unlock(&portdev->cvq_lock);
1631
1632                 buf->len = len;
1633                 buf->offset = 0;
1634
1635                 handle_control_message(portdev, buf);
1636
1637                 spin_lock(&portdev->cvq_lock);
1638                 if (add_inbuf(portdev->c_ivq, buf) < 0) {
1639                         dev_warn(&portdev->vdev->dev,
1640                                  "Error adding buffer to queue\n");
1641                         free_buf(buf);
1642                 }
1643         }
1644         spin_unlock(&portdev->cvq_lock);
1645 }
1646
1647 static void out_intr(struct virtqueue *vq)
1648 {
1649         struct port *port;
1650
1651         port = find_port_by_vq(vq->vdev->priv, vq);
1652         if (!port)
1653                 return;
1654
1655         wake_up_interruptible(&port->waitqueue);
1656 }
1657
1658 static void in_intr(struct virtqueue *vq)
1659 {
1660         struct port *port;
1661         unsigned long flags;
1662
1663         port = find_port_by_vq(vq->vdev->priv, vq);
1664         if (!port)
1665                 return;
1666
1667         spin_lock_irqsave(&port->inbuf_lock, flags);
1668         port->inbuf = get_inbuf(port);
1669
1670         /*
1671          * Don't queue up data when port is closed.  This condition
1672          * can be reached when a console port is not yet connected (no
1673          * tty is spawned) and the host sends out data to console
1674          * ports.  For generic serial ports, the host won't
1675          * (shouldn't) send data till the guest is connected.
1676          */
1677         if (!port->guest_connected)
1678                 discard_port_data(port);
1679
1680         spin_unlock_irqrestore(&port->inbuf_lock, flags);
1681
1682         wake_up_interruptible(&port->waitqueue);
1683
1684         /* Send a SIGIO indicating new data in case the process asked for it */
1685         send_sigio_to_port(port);
1686
1687         if (is_console_port(port) && hvc_poll(port->cons.hvc))
1688                 hvc_kick();
1689 }
1690
1691 static void control_intr(struct virtqueue *vq)
1692 {
1693         struct ports_device *portdev;
1694
1695         portdev = vq->vdev->priv;
1696         schedule_work(&portdev->control_work);
1697 }
1698
1699 static void config_intr(struct virtio_device *vdev)
1700 {
1701         struct ports_device *portdev;
1702
1703         portdev = vdev->priv;
1704
1705         if (!use_multiport(portdev)) {
1706                 struct port *port;
1707                 u16 rows, cols;
1708
1709                 vdev->config->get(vdev,
1710                                   offsetof(struct virtio_console_config, cols),
1711                                   &cols, sizeof(u16));
1712                 vdev->config->get(vdev,
1713                                   offsetof(struct virtio_console_config, rows),
1714                                   &rows, sizeof(u16));
1715
1716                 port = find_port_by_id(portdev, 0);
1717                 set_console_size(port, rows, cols);
1718
1719                 /*
1720                  * We'll use this way of resizing only for legacy
1721                  * support.  For newer userspace
1722                  * (VIRTIO_CONSOLE_F_MULTPORT+), use control messages
1723                  * to indicate console size changes so that it can be
1724                  * done per-port.
1725                  */
1726                 resize_console(port);
1727         }
1728 }
1729
1730 static int init_vqs(struct ports_device *portdev)
1731 {
1732         vq_callback_t **io_callbacks;
1733         char **io_names;
1734         struct virtqueue **vqs;
1735         u32 i, j, nr_ports, nr_queues;
1736         int err;
1737
1738         nr_ports = portdev->config.max_nr_ports;
1739         nr_queues = use_multiport(portdev) ? (nr_ports + 1) * 2 : 2;
1740
1741         vqs = kmalloc(nr_queues * sizeof(struct virtqueue *), GFP_KERNEL);
1742         io_callbacks = kmalloc(nr_queues * sizeof(vq_callback_t *), GFP_KERNEL);
1743         io_names = kmalloc(nr_queues * sizeof(char *), GFP_KERNEL);
1744         portdev->in_vqs = kmalloc(nr_ports * sizeof(struct virtqueue *),
1745                                   GFP_KERNEL);
1746         portdev->out_vqs = kmalloc(nr_ports * sizeof(struct virtqueue *),
1747                                    GFP_KERNEL);
1748         if (!vqs || !io_callbacks || !io_names || !portdev->in_vqs ||
1749             !portdev->out_vqs) {
1750                 err = -ENOMEM;
1751                 goto free;
1752         }
1753
1754         /*
1755          * For backward compat (newer host but older guest), the host
1756          * spawns a console port first and also inits the vqs for port
1757          * 0 before others.
1758          */
1759         j = 0;
1760         io_callbacks[j] = in_intr;
1761         io_callbacks[j + 1] = out_intr;
1762         io_names[j] = "input";
1763         io_names[j + 1] = "output";
1764         j += 2;
1765
1766         if (use_multiport(portdev)) {
1767                 io_callbacks[j] = control_intr;
1768                 io_callbacks[j + 1] = NULL;
1769                 io_names[j] = "control-i";
1770                 io_names[j + 1] = "control-o";
1771
1772                 for (i = 1; i < nr_ports; i++) {
1773                         j += 2;
1774                         io_callbacks[j] = in_intr;
1775                         io_callbacks[j + 1] = out_intr;
1776                         io_names[j] = "input";
1777                         io_names[j + 1] = "output";
1778                 }
1779         }
1780         /* Find the queues. */
1781         err = portdev->vdev->config->find_vqs(portdev->vdev, nr_queues, vqs,
1782                                               io_callbacks,
1783                                               (const char **)io_names);
1784         if (err)
1785                 goto free;
1786
1787         j = 0;
1788         portdev->in_vqs[0] = vqs[0];
1789         portdev->out_vqs[0] = vqs[1];
1790         j += 2;
1791         if (use_multiport(portdev)) {
1792                 portdev->c_ivq = vqs[j];
1793                 portdev->c_ovq = vqs[j + 1];
1794
1795                 for (i = 1; i < nr_ports; i++) {
1796                         j += 2;
1797                         portdev->in_vqs[i] = vqs[j];
1798                         portdev->out_vqs[i] = vqs[j + 1];
1799                 }
1800         }
1801         kfree(io_names);
1802         kfree(io_callbacks);
1803         kfree(vqs);
1804
1805         return 0;
1806
1807 free:
1808         kfree(portdev->out_vqs);
1809         kfree(portdev->in_vqs);
1810         kfree(io_names);
1811         kfree(io_callbacks);
1812         kfree(vqs);
1813
1814         return err;
1815 }
1816
1817 static const struct file_operations portdev_fops = {
1818         .owner = THIS_MODULE,
1819 };
1820
1821 static void remove_vqs(struct ports_device *portdev)
1822 {
1823         portdev->vdev->config->del_vqs(portdev->vdev);
1824         kfree(portdev->in_vqs);
1825         kfree(portdev->out_vqs);
1826 }
1827
1828 static void remove_controlq_data(struct ports_device *portdev)
1829 {
1830         struct port_buffer *buf;
1831         unsigned int len;
1832
1833         if (!use_multiport(portdev))
1834                 return;
1835
1836         while ((buf = virtqueue_get_buf(portdev->c_ivq, &len)))
1837                 free_buf(buf);
1838
1839         while ((buf = virtqueue_detach_unused_buf(portdev->c_ivq)))
1840                 free_buf(buf);
1841 }
1842
1843 /*
1844  * Once we're further in boot, we get probed like any other virtio
1845  * device.
1846  *
1847  * If the host also supports multiple console ports, we check the
1848  * config space to see how many ports the host has spawned.  We
1849  * initialize each port found.
1850  */
1851 static int __devinit virtcons_probe(struct virtio_device *vdev)
1852 {
1853         struct ports_device *portdev;
1854         int err;
1855         bool multiport;
1856         bool early = early_put_chars != NULL;
1857
1858         /* Ensure to read early_put_chars now */
1859         barrier();
1860
1861         portdev = kmalloc(sizeof(*portdev), GFP_KERNEL);
1862         if (!portdev) {
1863                 err = -ENOMEM;
1864                 goto fail;
1865         }
1866
1867         /* Attach this portdev to this virtio_device, and vice-versa. */
1868         portdev->vdev = vdev;
1869         vdev->priv = portdev;
1870
1871         spin_lock_irq(&pdrvdata_lock);
1872         portdev->drv_index = pdrvdata.index++;
1873         spin_unlock_irq(&pdrvdata_lock);
1874
1875         portdev->chr_major = register_chrdev(0, "virtio-portsdev",
1876                                              &portdev_fops);
1877         if (portdev->chr_major < 0) {
1878                 dev_err(&vdev->dev,
1879                         "Error %d registering chrdev for device %u\n",
1880                         portdev->chr_major, portdev->drv_index);
1881                 err = portdev->chr_major;
1882                 goto free;
1883         }
1884
1885         multiport = false;
1886         portdev->config.max_nr_ports = 1;
1887         if (virtio_config_val(vdev, VIRTIO_CONSOLE_F_MULTIPORT,
1888                               offsetof(struct virtio_console_config,
1889                                        max_nr_ports),
1890                               &portdev->config.max_nr_ports) == 0)
1891                 multiport = true;
1892
1893         err = init_vqs(portdev);
1894         if (err < 0) {
1895                 dev_err(&vdev->dev, "Error %d initializing vqs\n", err);
1896                 goto free_chrdev;
1897         }
1898
1899         spin_lock_init(&portdev->ports_lock);
1900         INIT_LIST_HEAD(&portdev->ports);
1901
1902         if (multiport) {
1903                 unsigned int nr_added_bufs;
1904
1905                 spin_lock_init(&portdev->cvq_lock);
1906                 INIT_WORK(&portdev->control_work, &control_work_handler);
1907
1908                 nr_added_bufs = fill_queue(portdev->c_ivq, &portdev->cvq_lock);
1909                 if (!nr_added_bufs) {
1910                         dev_err(&vdev->dev,
1911                                 "Error allocating buffers for control queue\n");
1912                         err = -ENOMEM;
1913                         goto free_vqs;
1914                 }
1915         } else {
1916                 /*
1917                  * For backward compatibility: Create a console port
1918                  * if we're running on older host.
1919                  */
1920                 add_port(portdev, 0);
1921         }
1922
1923         spin_lock_irq(&pdrvdata_lock);
1924         list_add_tail(&portdev->list, &pdrvdata.portdevs);
1925         spin_unlock_irq(&pdrvdata_lock);
1926
1927         __send_control_msg(portdev, VIRTIO_CONSOLE_BAD_ID,
1928                            VIRTIO_CONSOLE_DEVICE_READY, 1);
1929
1930         /*
1931          * If there was an early virtio console, assume that there are no
1932          * other consoles. We need to wait until the hvc_alloc matches the
1933          * hvc_instantiate, otherwise tty_open will complain, resulting in
1934          * a "Warning: unable to open an initial console" boot failure.
1935          * Without multiport this is done in add_port above. With multiport
1936          * this might take some host<->guest communication - thus we have to
1937          * wait.
1938          */
1939         if (multiport && early)
1940                 wait_for_completion(&early_console_added);
1941
1942         return 0;
1943
1944 free_vqs:
1945         /* The host might want to notify mgmt sw about device add failure */
1946         __send_control_msg(portdev, VIRTIO_CONSOLE_BAD_ID,
1947                            VIRTIO_CONSOLE_DEVICE_READY, 0);
1948         remove_vqs(portdev);
1949 free_chrdev:
1950         unregister_chrdev(portdev->chr_major, "virtio-portsdev");
1951 free:
1952         kfree(portdev);
1953 fail:
1954         return err;
1955 }
1956
1957 static void virtcons_remove(struct virtio_device *vdev)
1958 {
1959         struct ports_device *portdev;
1960         struct port *port, *port2;
1961
1962         portdev = vdev->priv;
1963
1964         spin_lock_irq(&pdrvdata_lock);
1965         list_del(&portdev->list);
1966         spin_unlock_irq(&pdrvdata_lock);
1967
1968         /* Disable interrupts for vqs */
1969         vdev->config->reset(vdev);
1970         /* Finish up work that's lined up */
1971         cancel_work_sync(&portdev->control_work);
1972
1973         list_for_each_entry_safe(port, port2, &portdev->ports, list)
1974                 unplug_port(port);
1975
1976         unregister_chrdev(portdev->chr_major, "virtio-portsdev");
1977
1978         /*
1979          * When yanking out a device, we immediately lose the
1980          * (device-side) queues.  So there's no point in keeping the
1981          * guest side around till we drop our final reference.  This
1982          * also means that any ports which are in an open state will
1983          * have to just stop using the port, as the vqs are going
1984          * away.
1985          */
1986         remove_controlq_data(portdev);
1987         remove_vqs(portdev);
1988         kfree(portdev);
1989 }
1990
1991 static struct virtio_device_id id_table[] = {
1992         { VIRTIO_ID_CONSOLE, VIRTIO_DEV_ANY_ID },
1993         { 0 },
1994 };
1995
1996 static unsigned int features[] = {
1997         VIRTIO_CONSOLE_F_SIZE,
1998         VIRTIO_CONSOLE_F_MULTIPORT,
1999 };
2000
2001 #ifdef CONFIG_PM
2002 static int virtcons_freeze(struct virtio_device *vdev)
2003 {
2004         struct ports_device *portdev;
2005         struct port *port;
2006
2007         portdev = vdev->priv;
2008
2009         vdev->config->reset(vdev);
2010
2011         virtqueue_disable_cb(portdev->c_ivq);
2012         cancel_work_sync(&portdev->control_work);
2013         /*
2014          * Once more: if control_work_handler() was running, it would
2015          * enable the cb as the last step.
2016          */
2017         virtqueue_disable_cb(portdev->c_ivq);
2018         remove_controlq_data(portdev);
2019
2020         list_for_each_entry(port, &portdev->ports, list) {
2021                 virtqueue_disable_cb(port->in_vq);
2022                 virtqueue_disable_cb(port->out_vq);
2023                 /*
2024                  * We'll ask the host later if the new invocation has
2025                  * the port opened or closed.
2026                  */
2027                 port->host_connected = false;
2028                 remove_port_data(port);
2029         }
2030         remove_vqs(portdev);
2031
2032         return 0;
2033 }
2034
2035 static int virtcons_restore(struct virtio_device *vdev)
2036 {
2037         struct ports_device *portdev;
2038         struct port *port;
2039         int ret;
2040
2041         portdev = vdev->priv;
2042
2043         ret = init_vqs(portdev);
2044         if (ret)
2045                 return ret;
2046
2047         if (use_multiport(portdev))
2048                 fill_queue(portdev->c_ivq, &portdev->cvq_lock);
2049
2050         list_for_each_entry(port, &portdev->ports, list) {
2051                 port->in_vq = portdev->in_vqs[port->id];
2052                 port->out_vq = portdev->out_vqs[port->id];
2053
2054                 fill_queue(port->in_vq, &port->inbuf_lock);
2055
2056                 /* Get port open/close status on the host */
2057                 send_control_msg(port, VIRTIO_CONSOLE_PORT_READY, 1);
2058
2059                 /*
2060                  * If a port was open at the time of suspending, we
2061                  * have to let the host know that it's still open.
2062                  */
2063                 if (port->guest_connected)
2064                         send_control_msg(port, VIRTIO_CONSOLE_PORT_OPEN, 1);
2065         }
2066         return 0;
2067 }
2068 #endif
2069
2070 static struct virtio_driver virtio_console = {
2071         .feature_table = features,
2072         .feature_table_size = ARRAY_SIZE(features),
2073         .driver.name =  KBUILD_MODNAME,
2074         .driver.owner = THIS_MODULE,
2075         .id_table =     id_table,
2076         .probe =        virtcons_probe,
2077         .remove =       virtcons_remove,
2078         .config_changed = config_intr,
2079 #ifdef CONFIG_PM
2080         .freeze =       virtcons_freeze,
2081         .restore =      virtcons_restore,
2082 #endif
2083 };
2084
2085 static int __init init(void)
2086 {
2087         int err;
2088
2089         pdrvdata.class = class_create(THIS_MODULE, "virtio-ports");
2090         if (IS_ERR(pdrvdata.class)) {
2091                 err = PTR_ERR(pdrvdata.class);
2092                 pr_err("Error %d creating virtio-ports class\n", err);
2093                 return err;
2094         }
2095
2096         pdrvdata.debugfs_dir = debugfs_create_dir("virtio-ports", NULL);
2097         if (!pdrvdata.debugfs_dir) {
2098                 pr_warning("Error %ld creating debugfs dir for virtio-ports\n",
2099                            PTR_ERR(pdrvdata.debugfs_dir));
2100         }
2101         INIT_LIST_HEAD(&pdrvdata.consoles);
2102         INIT_LIST_HEAD(&pdrvdata.portdevs);
2103
2104         err = register_virtio_driver(&virtio_console);
2105         if (err < 0) {
2106                 pr_err("Error %d registering virtio driver\n", err);
2107                 goto free;
2108         }
2109         return 0;
2110 free:
2111         if (pdrvdata.debugfs_dir)
2112                 debugfs_remove_recursive(pdrvdata.debugfs_dir);
2113         class_destroy(pdrvdata.class);
2114         return err;
2115 }
2116
2117 static void __exit fini(void)
2118 {
2119         unregister_virtio_driver(&virtio_console);
2120
2121         class_destroy(pdrvdata.class);
2122         if (pdrvdata.debugfs_dir)
2123                 debugfs_remove_recursive(pdrvdata.debugfs_dir);
2124 }
2125 module_init(init);
2126 module_exit(fini);
2127
2128 MODULE_DEVICE_TABLE(virtio, id_table);
2129 MODULE_DESCRIPTION("Virtio console driver");
2130 MODULE_LICENSE("GPL");