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