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