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