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