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