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