Loading...
1// SPDX-License-Identifier: GPL-2.0
2/* vcc.c: sun4v virtual channel concentrator
3 *
4 * Copyright (C) 2017 Oracle. All rights reserved.
5 */
6
7#include <linux/delay.h>
8#include <linux/interrupt.h>
9#include <linux/module.h>
10#include <linux/slab.h>
11#include <linux/sysfs.h>
12#include <linux/tty.h>
13#include <linux/tty_flip.h>
14#include <asm/vio.h>
15#include <asm/ldc.h>
16
17#define DRV_MODULE_NAME "vcc"
18#define DRV_MODULE_VERSION "1.1"
19#define DRV_MODULE_RELDATE "July 1, 2017"
20
21static char version[] =
22 DRV_MODULE_NAME ".c:v" DRV_MODULE_VERSION " (" DRV_MODULE_RELDATE ")";
23
24MODULE_DESCRIPTION("Sun LDOM virtual console concentrator driver");
25MODULE_LICENSE("GPL");
26MODULE_VERSION(DRV_MODULE_VERSION);
27
28struct vcc_port {
29 struct vio_driver_state vio;
30
31 spinlock_t lock;
32 char *domain;
33 struct tty_struct *tty; /* only populated while dev is open */
34 unsigned long index; /* index into the vcc_table */
35
36 u64 refcnt;
37 bool excl_locked;
38
39 bool removed;
40
41 /* This buffer is required to support the tty write_room interface
42 * and guarantee that any characters that the driver accepts will
43 * be eventually sent, either immediately or later.
44 */
45 int chars_in_buffer;
46 struct vio_vcc buffer;
47
48 struct timer_list rx_timer;
49 struct timer_list tx_timer;
50};
51
52/* Microseconds that thread will delay waiting for a vcc port ref */
53#define VCC_REF_DELAY 100
54
55#define VCC_MAX_PORTS 1024
56#define VCC_MINOR_START 0 /* must be zero */
57#define VCC_BUFF_LEN VIO_VCC_MTU_SIZE
58
59#define VCC_CTL_BREAK -1
60#define VCC_CTL_HUP -2
61
62static const char vcc_driver_name[] = "vcc";
63static const char vcc_device_node[] = "vcc";
64static struct tty_driver *vcc_tty_driver;
65
66static struct vcc_port *vcc_table[VCC_MAX_PORTS];
67static DEFINE_SPINLOCK(vcc_table_lock);
68
69int vcc_dbg;
70int vcc_dbg_ldc;
71int vcc_dbg_vio;
72
73module_param(vcc_dbg, uint, 0664);
74module_param(vcc_dbg_ldc, uint, 0664);
75module_param(vcc_dbg_vio, uint, 0664);
76
77#define VCC_DBG_DRV 0x1
78#define VCC_DBG_LDC 0x2
79#define VCC_DBG_PKT 0x4
80
81#define vccdbg(f, a...) \
82 do { \
83 if (vcc_dbg & VCC_DBG_DRV) \
84 pr_info(f, ## a); \
85 } while (0) \
86
87#define vccdbgl(l) \
88 do { \
89 if (vcc_dbg & VCC_DBG_LDC) \
90 ldc_print(l); \
91 } while (0) \
92
93#define vccdbgp(pkt) \
94 do { \
95 if (vcc_dbg & VCC_DBG_PKT) { \
96 int i; \
97 for (i = 0; i < pkt.tag.stype; i++) \
98 pr_info("[%c]", pkt.data[i]); \
99 } \
100 } while (0) \
101
102/* Note: Be careful when adding flags to this line discipline. Don't
103 * add anything that will cause echoing or we'll go into recursive
104 * loop echoing chars back and forth with the console drivers.
105 */
106static const struct ktermios vcc_tty_termios = {
107 .c_iflag = IGNBRK | IGNPAR,
108 .c_oflag = OPOST,
109 .c_cflag = B38400 | CS8 | CREAD | HUPCL,
110 .c_cc = INIT_C_CC,
111 .c_ispeed = 38400,
112 .c_ospeed = 38400
113};
114
115/**
116 * vcc_table_add() - Add VCC port to the VCC table
117 * @port: pointer to the VCC port
118 *
119 * Return: index of the port in the VCC table on success,
120 * -1 on failure
121 */
122static int vcc_table_add(struct vcc_port *port)
123{
124 unsigned long flags;
125 int i;
126
127 spin_lock_irqsave(&vcc_table_lock, flags);
128 for (i = VCC_MINOR_START; i < VCC_MAX_PORTS; i++) {
129 if (!vcc_table[i]) {
130 vcc_table[i] = port;
131 break;
132 }
133 }
134 spin_unlock_irqrestore(&vcc_table_lock, flags);
135
136 if (i < VCC_MAX_PORTS)
137 return i;
138 else
139 return -1;
140}
141
142/**
143 * vcc_table_remove() - Removes a VCC port from the VCC table
144 * @index: Index into the VCC table
145 */
146static void vcc_table_remove(unsigned long index)
147{
148 unsigned long flags;
149
150 if (WARN_ON(index >= VCC_MAX_PORTS))
151 return;
152
153 spin_lock_irqsave(&vcc_table_lock, flags);
154 vcc_table[index] = NULL;
155 spin_unlock_irqrestore(&vcc_table_lock, flags);
156}
157
158/**
159 * vcc_get() - Gets a reference to VCC port
160 * @index: Index into the VCC table
161 * @excl: Indicates if an exclusive access is requested
162 *
163 * Return: reference to the VCC port, if found
164 * NULL, if port not found
165 */
166static struct vcc_port *vcc_get(unsigned long index, bool excl)
167{
168 struct vcc_port *port;
169 unsigned long flags;
170
171try_again:
172 spin_lock_irqsave(&vcc_table_lock, flags);
173
174 port = vcc_table[index];
175 if (!port) {
176 spin_unlock_irqrestore(&vcc_table_lock, flags);
177 return NULL;
178 }
179
180 if (!excl) {
181 if (port->excl_locked) {
182 spin_unlock_irqrestore(&vcc_table_lock, flags);
183 udelay(VCC_REF_DELAY);
184 goto try_again;
185 }
186 port->refcnt++;
187 spin_unlock_irqrestore(&vcc_table_lock, flags);
188 return port;
189 }
190
191 if (port->refcnt) {
192 spin_unlock_irqrestore(&vcc_table_lock, flags);
193 /* Threads wanting exclusive access will wait half the time,
194 * probably giving them higher priority in the case of
195 * multiple waiters.
196 */
197 udelay(VCC_REF_DELAY/2);
198 goto try_again;
199 }
200
201 port->refcnt++;
202 port->excl_locked = true;
203 spin_unlock_irqrestore(&vcc_table_lock, flags);
204
205 return port;
206}
207
208/**
209 * vcc_put() - Returns a reference to VCC port
210 * @port: pointer to VCC port
211 * @excl: Indicates if the returned reference is an exclusive reference
212 *
213 * Note: It's the caller's responsibility to ensure the correct value
214 * for the excl flag
215 */
216static void vcc_put(struct vcc_port *port, bool excl)
217{
218 unsigned long flags;
219
220 if (!port)
221 return;
222
223 spin_lock_irqsave(&vcc_table_lock, flags);
224
225 /* check if caller attempted to put with the wrong flags */
226 if (WARN_ON((excl && !port->excl_locked) ||
227 (!excl && port->excl_locked)))
228 goto done;
229
230 port->refcnt--;
231
232 if (excl)
233 port->excl_locked = false;
234
235done:
236 spin_unlock_irqrestore(&vcc_table_lock, flags);
237}
238
239/**
240 * vcc_get_ne() - Get a non-exclusive reference to VCC port
241 * @index: Index into the VCC table
242 *
243 * Gets a non-exclusive reference to VCC port, if it's not removed
244 *
245 * Return: pointer to the VCC port, if found
246 * NULL, if port not found
247 */
248static struct vcc_port *vcc_get_ne(unsigned long index)
249{
250 struct vcc_port *port;
251
252 port = vcc_get(index, false);
253
254 if (port && port->removed) {
255 vcc_put(port, false);
256 return NULL;
257 }
258
259 return port;
260}
261
262static void vcc_kick_rx(struct vcc_port *port)
263{
264 struct vio_driver_state *vio = &port->vio;
265
266 assert_spin_locked(&port->lock);
267
268 if (!timer_pending(&port->rx_timer) && !port->removed) {
269 disable_irq_nosync(vio->vdev->rx_irq);
270 port->rx_timer.expires = (jiffies + 1);
271 add_timer(&port->rx_timer);
272 }
273}
274
275static void vcc_kick_tx(struct vcc_port *port)
276{
277 assert_spin_locked(&port->lock);
278
279 if (!timer_pending(&port->tx_timer) && !port->removed) {
280 port->tx_timer.expires = (jiffies + 1);
281 add_timer(&port->tx_timer);
282 }
283}
284
285static int vcc_rx_check(struct tty_struct *tty, int size)
286{
287 if (WARN_ON(!tty || !tty->port))
288 return 1;
289
290 /* tty_buffer_request_room won't sleep because it uses
291 * GFP_ATOMIC flag to allocate buffer
292 */
293 if (test_bit(TTY_THROTTLED, &tty->flags) ||
294 (tty_buffer_request_room(tty->port, VCC_BUFF_LEN) < VCC_BUFF_LEN))
295 return 0;
296
297 return 1;
298}
299
300static int vcc_rx(struct tty_struct *tty, char *buf, int size)
301{
302 int len = 0;
303
304 if (WARN_ON(!tty || !tty->port))
305 return len;
306
307 len = tty_insert_flip_string(tty->port, buf, size);
308 if (len)
309 tty_flip_buffer_push(tty->port);
310
311 return len;
312}
313
314static int vcc_ldc_read(struct vcc_port *port)
315{
316 struct vio_driver_state *vio = &port->vio;
317 struct tty_struct *tty;
318 struct vio_vcc pkt;
319 int rv = 0;
320
321 tty = port->tty;
322 if (!tty) {
323 rv = ldc_rx_reset(vio->lp);
324 vccdbg("VCC: reset rx q: rv=%d\n", rv);
325 goto done;
326 }
327
328 /* Read as long as LDC has incoming data. */
329 while (1) {
330 if (!vcc_rx_check(tty, VIO_VCC_MTU_SIZE)) {
331 vcc_kick_rx(port);
332 break;
333 }
334
335 vccdbgl(vio->lp);
336
337 rv = ldc_read(vio->lp, &pkt, sizeof(pkt));
338 if (rv <= 0)
339 break;
340
341 vccdbg("VCC: ldc_read()=%d\n", rv);
342 vccdbg("TAG [%02x:%02x:%04x:%08x]\n",
343 pkt.tag.type, pkt.tag.stype,
344 pkt.tag.stype_env, pkt.tag.sid);
345
346 if (pkt.tag.type == VIO_TYPE_DATA) {
347 vccdbgp(pkt);
348 /* vcc_rx_check ensures memory availability */
349 vcc_rx(tty, pkt.data, pkt.tag.stype);
350 } else {
351 pr_err("VCC: unknown msg [%02x:%02x:%04x:%08x]\n",
352 pkt.tag.type, pkt.tag.stype,
353 pkt.tag.stype_env, pkt.tag.sid);
354 rv = -ECONNRESET;
355 break;
356 }
357
358 WARN_ON(rv != LDC_PACKET_SIZE);
359 }
360
361done:
362 return rv;
363}
364
365static void vcc_rx_timer(struct timer_list *t)
366{
367 struct vcc_port *port = from_timer(port, t, rx_timer);
368 struct vio_driver_state *vio;
369 unsigned long flags;
370 int rv;
371
372 spin_lock_irqsave(&port->lock, flags);
373 port->rx_timer.expires = 0;
374
375 vio = &port->vio;
376
377 enable_irq(vio->vdev->rx_irq);
378
379 if (!port->tty || port->removed)
380 goto done;
381
382 rv = vcc_ldc_read(port);
383 if (rv == -ECONNRESET)
384 vio_conn_reset(vio);
385
386done:
387 spin_unlock_irqrestore(&port->lock, flags);
388 vcc_put(port, false);
389}
390
391static void vcc_tx_timer(struct timer_list *t)
392{
393 struct vcc_port *port = from_timer(port, t, tx_timer);
394 struct vio_vcc *pkt;
395 unsigned long flags;
396 int tosend = 0;
397 int rv;
398
399 spin_lock_irqsave(&port->lock, flags);
400 port->tx_timer.expires = 0;
401
402 if (!port->tty || port->removed)
403 goto done;
404
405 tosend = min(VCC_BUFF_LEN, port->chars_in_buffer);
406 if (!tosend)
407 goto done;
408
409 pkt = &port->buffer;
410 pkt->tag.type = VIO_TYPE_DATA;
411 pkt->tag.stype = tosend;
412 vccdbgl(port->vio.lp);
413
414 rv = ldc_write(port->vio.lp, pkt, (VIO_TAG_SIZE + tosend));
415 WARN_ON(!rv);
416
417 if (rv < 0) {
418 vccdbg("VCC: ldc_write()=%d\n", rv);
419 vcc_kick_tx(port);
420 } else {
421 struct tty_struct *tty = port->tty;
422
423 port->chars_in_buffer = 0;
424 if (tty)
425 tty_wakeup(tty);
426 }
427
428done:
429 spin_unlock_irqrestore(&port->lock, flags);
430 vcc_put(port, false);
431}
432
433/**
434 * vcc_event() - LDC event processing engine
435 * @arg: VCC private data
436 * @event: LDC event
437 *
438 * Handles LDC events for VCC
439 */
440static void vcc_event(void *arg, int event)
441{
442 struct vio_driver_state *vio;
443 struct vcc_port *port;
444 unsigned long flags;
445 int rv;
446
447 port = arg;
448 vio = &port->vio;
449
450 spin_lock_irqsave(&port->lock, flags);
451
452 switch (event) {
453 case LDC_EVENT_RESET:
454 case LDC_EVENT_UP:
455 vio_link_state_change(vio, event);
456 break;
457
458 case LDC_EVENT_DATA_READY:
459 rv = vcc_ldc_read(port);
460 if (rv == -ECONNRESET)
461 vio_conn_reset(vio);
462 break;
463
464 default:
465 pr_err("VCC: unexpected LDC event(%d)\n", event);
466 }
467
468 spin_unlock_irqrestore(&port->lock, flags);
469}
470
471static struct ldc_channel_config vcc_ldc_cfg = {
472 .event = vcc_event,
473 .mtu = VIO_VCC_MTU_SIZE,
474 .mode = LDC_MODE_RAW,
475 .debug = 0,
476};
477
478/* Ordered from largest major to lowest */
479static struct vio_version vcc_versions[] = {
480 { .major = 1, .minor = 0 },
481};
482
483static struct tty_port_operations vcc_port_ops = { 0 };
484
485static ssize_t vcc_sysfs_domain_show(struct device *dev,
486 struct device_attribute *attr,
487 char *buf)
488{
489 struct vcc_port *port;
490 int rv;
491
492 port = dev_get_drvdata(dev);
493 if (!port)
494 return -ENODEV;
495
496 rv = scnprintf(buf, PAGE_SIZE, "%s\n", port->domain);
497
498 return rv;
499}
500
501static int vcc_send_ctl(struct vcc_port *port, int ctl)
502{
503 struct vio_vcc pkt;
504 int rv;
505
506 pkt.tag.type = VIO_TYPE_CTRL;
507 pkt.tag.sid = ctl;
508 pkt.tag.stype = 0;
509
510 rv = ldc_write(port->vio.lp, &pkt, sizeof(pkt.tag));
511 WARN_ON(!rv);
512 vccdbg("VCC: ldc_write(%ld)=%d\n", sizeof(pkt.tag), rv);
513
514 return rv;
515}
516
517static ssize_t vcc_sysfs_break_store(struct device *dev,
518 struct device_attribute *attr,
519 const char *buf, size_t count)
520{
521 struct vcc_port *port;
522 unsigned long flags;
523 int rv = count;
524 int brk;
525
526 port = dev_get_drvdata(dev);
527 if (!port)
528 return -ENODEV;
529
530 spin_lock_irqsave(&port->lock, flags);
531
532 if (sscanf(buf, "%ud", &brk) != 1 || brk != 1)
533 rv = -EINVAL;
534 else if (vcc_send_ctl(port, VCC_CTL_BREAK) < 0)
535 vcc_kick_tx(port);
536
537 spin_unlock_irqrestore(&port->lock, flags);
538
539 return rv;
540}
541
542static DEVICE_ATTR(domain, 0400, vcc_sysfs_domain_show, NULL);
543static DEVICE_ATTR(break, 0200, NULL, vcc_sysfs_break_store);
544
545static struct attribute *vcc_sysfs_entries[] = {
546 &dev_attr_domain.attr,
547 &dev_attr_break.attr,
548 NULL
549};
550
551static struct attribute_group vcc_attribute_group = {
552 .name = NULL,
553 .attrs = vcc_sysfs_entries,
554};
555
556/**
557 * vcc_probe() - Initialize VCC port
558 * @vdev: Pointer to VIO device of the new VCC port
559 * @id: VIO device ID
560 *
561 * Initializes a VCC port to receive serial console data from
562 * the guest domain. Sets up a TTY end point on the control
563 * domain. Sets up VIO/LDC link between the guest & control
564 * domain endpoints.
565 *
566 * Return: status of the probe
567 */
568static int vcc_probe(struct vio_dev *vdev, const struct vio_device_id *id)
569{
570 struct mdesc_handle *hp;
571 struct vcc_port *port;
572 struct device *dev;
573 const char *domain;
574 char *name;
575 u64 node;
576 int rv;
577
578 vccdbg("VCC: name=%s\n", dev_name(&vdev->dev));
579
580 if (!vcc_tty_driver) {
581 pr_err("VCC: TTY driver not registered\n");
582 return -ENODEV;
583 }
584
585 port = kzalloc(sizeof(struct vcc_port), GFP_KERNEL);
586 if (!port)
587 return -ENOMEM;
588
589 name = kstrdup(dev_name(&vdev->dev), GFP_KERNEL);
590
591 rv = vio_driver_init(&port->vio, vdev, VDEV_CONSOLE_CON, vcc_versions,
592 ARRAY_SIZE(vcc_versions), NULL, name);
593 if (rv)
594 goto free_port;
595
596 port->vio.debug = vcc_dbg_vio;
597 vcc_ldc_cfg.debug = vcc_dbg_ldc;
598
599 rv = vio_ldc_alloc(&port->vio, &vcc_ldc_cfg, port);
600 if (rv)
601 goto free_port;
602
603 spin_lock_init(&port->lock);
604
605 port->index = vcc_table_add(port);
606 if (port->index == -1) {
607 pr_err("VCC: no more TTY indices left for allocation\n");
608 rv = -ENOMEM;
609 goto free_ldc;
610 }
611
612 /* Register the device using VCC table index as TTY index */
613 dev = tty_register_device(vcc_tty_driver, port->index, &vdev->dev);
614 if (IS_ERR(dev)) {
615 rv = PTR_ERR(dev);
616 goto free_table;
617 }
618
619 hp = mdesc_grab();
620
621 node = vio_vdev_node(hp, vdev);
622 if (node == MDESC_NODE_NULL) {
623 rv = -ENXIO;
624 mdesc_release(hp);
625 goto unreg_tty;
626 }
627
628 domain = mdesc_get_property(hp, node, "vcc-domain-name", NULL);
629 if (!domain) {
630 rv = -ENXIO;
631 mdesc_release(hp);
632 goto unreg_tty;
633 }
634 port->domain = kstrdup(domain, GFP_KERNEL);
635
636 mdesc_release(hp);
637
638 rv = sysfs_create_group(&vdev->dev.kobj, &vcc_attribute_group);
639 if (rv)
640 goto free_domain;
641
642 timer_setup(&port->rx_timer, vcc_rx_timer, 0);
643 timer_setup(&port->tx_timer, vcc_tx_timer, 0);
644
645 dev_set_drvdata(&vdev->dev, port);
646
647 /* It's possible to receive IRQs in the middle of vio_port_up. Disable
648 * IRQs until the port is up.
649 */
650 disable_irq_nosync(vdev->rx_irq);
651 vio_port_up(&port->vio);
652 enable_irq(vdev->rx_irq);
653
654 return 0;
655
656free_domain:
657 kfree(port->domain);
658unreg_tty:
659 tty_unregister_device(vcc_tty_driver, port->index);
660free_table:
661 vcc_table_remove(port->index);
662free_ldc:
663 vio_ldc_free(&port->vio);
664free_port:
665 kfree(name);
666 kfree(port);
667
668 return rv;
669}
670
671/**
672 * vcc_remove() - Terminate a VCC port
673 * @vdev: Pointer to VIO device of the VCC port
674 *
675 * Terminates a VCC port. Sets up the teardown of TTY and
676 * VIO/LDC link between guest and primary domains.
677 *
678 * Return: status of removal
679 */
680static int vcc_remove(struct vio_dev *vdev)
681{
682 struct vcc_port *port = dev_get_drvdata(&vdev->dev);
683
684 if (!port)
685 return -ENODEV;
686
687 del_timer_sync(&port->rx_timer);
688 del_timer_sync(&port->tx_timer);
689
690 /* If there's a process with the device open, do a synchronous
691 * hangup of the TTY. This *may* cause the process to call close
692 * asynchronously, but it's not guaranteed.
693 */
694 if (port->tty)
695 tty_vhangup(port->tty);
696
697 /* Get exclusive reference to VCC, ensures that there are no other
698 * clients to this port
699 */
700 port = vcc_get(port->index, true);
701
702 if (WARN_ON(!port))
703 return -ENODEV;
704
705 tty_unregister_device(vcc_tty_driver, port->index);
706
707 del_timer_sync(&port->vio.timer);
708 vio_ldc_free(&port->vio);
709 sysfs_remove_group(&vdev->dev.kobj, &vcc_attribute_group);
710 dev_set_drvdata(&vdev->dev, NULL);
711 if (port->tty) {
712 port->removed = true;
713 vcc_put(port, true);
714 } else {
715 vcc_table_remove(port->index);
716
717 kfree(port->vio.name);
718 kfree(port->domain);
719 kfree(port);
720 }
721
722 return 0;
723}
724
725static const struct vio_device_id vcc_match[] = {
726 {
727 .type = "vcc-port",
728 },
729 {},
730};
731MODULE_DEVICE_TABLE(vio, vcc_match);
732
733static struct vio_driver vcc_driver = {
734 .id_table = vcc_match,
735 .probe = vcc_probe,
736 .remove = vcc_remove,
737 .name = "vcc",
738};
739
740static int vcc_open(struct tty_struct *tty, struct file *vcc_file)
741{
742 struct vcc_port *port;
743
744 if (unlikely(!tty)) {
745 pr_err("VCC: open: Invalid TTY handle\n");
746 return -ENXIO;
747 }
748
749 if (tty->count > 1)
750 return -EBUSY;
751
752 port = vcc_get_ne(tty->index);
753 if (unlikely(!port)) {
754 pr_err("VCC: open: Failed to find VCC port\n");
755 return -ENODEV;
756 }
757
758 if (unlikely(!port->vio.lp)) {
759 pr_err("VCC: open: LDC channel not configured\n");
760 vcc_put(port, false);
761 return -EPIPE;
762 }
763 vccdbgl(port->vio.lp);
764
765 vcc_put(port, false);
766
767 if (unlikely(!tty->port)) {
768 pr_err("VCC: open: TTY port not found\n");
769 return -ENXIO;
770 }
771
772 if (unlikely(!tty->port->ops)) {
773 pr_err("VCC: open: TTY ops not defined\n");
774 return -ENXIO;
775 }
776
777 return tty_port_open(tty->port, tty, vcc_file);
778}
779
780static void vcc_close(struct tty_struct *tty, struct file *vcc_file)
781{
782 if (unlikely(!tty)) {
783 pr_err("VCC: close: Invalid TTY handle\n");
784 return;
785 }
786
787 if (unlikely(tty->count > 1))
788 return;
789
790 if (unlikely(!tty->port)) {
791 pr_err("VCC: close: TTY port not found\n");
792 return;
793 }
794
795 tty_port_close(tty->port, tty, vcc_file);
796}
797
798static void vcc_ldc_hup(struct vcc_port *port)
799{
800 unsigned long flags;
801
802 spin_lock_irqsave(&port->lock, flags);
803
804 if (vcc_send_ctl(port, VCC_CTL_HUP) < 0)
805 vcc_kick_tx(port);
806
807 spin_unlock_irqrestore(&port->lock, flags);
808}
809
810static void vcc_hangup(struct tty_struct *tty)
811{
812 struct vcc_port *port;
813
814 if (unlikely(!tty)) {
815 pr_err("VCC: hangup: Invalid TTY handle\n");
816 return;
817 }
818
819 port = vcc_get_ne(tty->index);
820 if (unlikely(!port)) {
821 pr_err("VCC: hangup: Failed to find VCC port\n");
822 return;
823 }
824
825 if (unlikely(!tty->port)) {
826 pr_err("VCC: hangup: TTY port not found\n");
827 vcc_put(port, false);
828 return;
829 }
830
831 vcc_ldc_hup(port);
832
833 vcc_put(port, false);
834
835 tty_port_hangup(tty->port);
836}
837
838static int vcc_write(struct tty_struct *tty, const unsigned char *buf,
839 int count)
840{
841 struct vcc_port *port;
842 struct vio_vcc *pkt;
843 unsigned long flags;
844 int total_sent = 0;
845 int tosend = 0;
846 int rv = -EINVAL;
847
848 if (unlikely(!tty)) {
849 pr_err("VCC: write: Invalid TTY handle\n");
850 return -ENXIO;
851 }
852
853 port = vcc_get_ne(tty->index);
854 if (unlikely(!port)) {
855 pr_err("VCC: write: Failed to find VCC port");
856 return -ENODEV;
857 }
858
859 spin_lock_irqsave(&port->lock, flags);
860
861 pkt = &port->buffer;
862 pkt->tag.type = VIO_TYPE_DATA;
863
864 while (count > 0) {
865 /* Minimum of data to write and space available */
866 tosend = min(count, (VCC_BUFF_LEN - port->chars_in_buffer));
867
868 if (!tosend)
869 break;
870
871 memcpy(&pkt->data[port->chars_in_buffer], &buf[total_sent],
872 tosend);
873 port->chars_in_buffer += tosend;
874 pkt->tag.stype = tosend;
875
876 vccdbg("TAG [%02x:%02x:%04x:%08x]\n", pkt->tag.type,
877 pkt->tag.stype, pkt->tag.stype_env, pkt->tag.sid);
878 vccdbg("DATA [%s]\n", pkt->data);
879 vccdbgl(port->vio.lp);
880
881 /* Since we know we have enough room in VCC buffer for tosend
882 * we record that it was sent regardless of whether the
883 * hypervisor actually took it because we have it buffered.
884 */
885 rv = ldc_write(port->vio.lp, pkt, (VIO_TAG_SIZE + tosend));
886 vccdbg("VCC: write: ldc_write(%d)=%d\n",
887 (VIO_TAG_SIZE + tosend), rv);
888
889 total_sent += tosend;
890 count -= tosend;
891 if (rv < 0) {
892 vcc_kick_tx(port);
893 break;
894 }
895
896 port->chars_in_buffer = 0;
897 }
898
899 spin_unlock_irqrestore(&port->lock, flags);
900
901 vcc_put(port, false);
902
903 vccdbg("VCC: write: total=%d rv=%d", total_sent, rv);
904
905 return total_sent ? total_sent : rv;
906}
907
908static int vcc_write_room(struct tty_struct *tty)
909{
910 struct vcc_port *port;
911 u64 num;
912
913 if (unlikely(!tty)) {
914 pr_err("VCC: write_room: Invalid TTY handle\n");
915 return -ENXIO;
916 }
917
918 port = vcc_get_ne(tty->index);
919 if (unlikely(!port)) {
920 pr_err("VCC: write_room: Failed to find VCC port\n");
921 return -ENODEV;
922 }
923
924 num = VCC_BUFF_LEN - port->chars_in_buffer;
925
926 vcc_put(port, false);
927
928 return num;
929}
930
931static int vcc_chars_in_buffer(struct tty_struct *tty)
932{
933 struct vcc_port *port;
934 u64 num;
935
936 if (unlikely(!tty)) {
937 pr_err("VCC: chars_in_buffer: Invalid TTY handle\n");
938 return -ENXIO;
939 }
940
941 port = vcc_get_ne(tty->index);
942 if (unlikely(!port)) {
943 pr_err("VCC: chars_in_buffer: Failed to find VCC port\n");
944 return -ENODEV;
945 }
946
947 num = port->chars_in_buffer;
948
949 vcc_put(port, false);
950
951 return num;
952}
953
954static int vcc_break_ctl(struct tty_struct *tty, int state)
955{
956 struct vcc_port *port;
957 unsigned long flags;
958
959 if (unlikely(!tty)) {
960 pr_err("VCC: break_ctl: Invalid TTY handle\n");
961 return -ENXIO;
962 }
963
964 port = vcc_get_ne(tty->index);
965 if (unlikely(!port)) {
966 pr_err("VCC: break_ctl: Failed to find VCC port\n");
967 return -ENODEV;
968 }
969
970 /* Turn off break */
971 if (state == 0) {
972 vcc_put(port, false);
973 return 0;
974 }
975
976 spin_lock_irqsave(&port->lock, flags);
977
978 if (vcc_send_ctl(port, VCC_CTL_BREAK) < 0)
979 vcc_kick_tx(port);
980
981 spin_unlock_irqrestore(&port->lock, flags);
982
983 vcc_put(port, false);
984
985 return 0;
986}
987
988static int vcc_install(struct tty_driver *driver, struct tty_struct *tty)
989{
990 struct vcc_port *port_vcc;
991 struct tty_port *port_tty;
992 int ret;
993
994 if (unlikely(!tty)) {
995 pr_err("VCC: install: Invalid TTY handle\n");
996 return -ENXIO;
997 }
998
999 if (tty->index >= VCC_MAX_PORTS)
1000 return -EINVAL;
1001
1002 ret = tty_standard_install(driver, tty);
1003 if (ret)
1004 return ret;
1005
1006 port_tty = kzalloc(sizeof(struct tty_port), GFP_KERNEL);
1007 if (!port_tty)
1008 return -ENOMEM;
1009
1010 port_vcc = vcc_get(tty->index, true);
1011 if (!port_vcc) {
1012 pr_err("VCC: install: Failed to find VCC port\n");
1013 tty->port = NULL;
1014 kfree(port_tty);
1015 return -ENODEV;
1016 }
1017
1018 tty_port_init(port_tty);
1019 port_tty->ops = &vcc_port_ops;
1020 tty->port = port_tty;
1021
1022 port_vcc->tty = tty;
1023
1024 vcc_put(port_vcc, true);
1025
1026 return 0;
1027}
1028
1029static void vcc_cleanup(struct tty_struct *tty)
1030{
1031 struct vcc_port *port;
1032
1033 if (unlikely(!tty)) {
1034 pr_err("VCC: cleanup: Invalid TTY handle\n");
1035 return;
1036 }
1037
1038 port = vcc_get(tty->index, true);
1039 if (port) {
1040 port->tty = NULL;
1041
1042 if (port->removed) {
1043 vcc_table_remove(tty->index);
1044 kfree(port->vio.name);
1045 kfree(port->domain);
1046 kfree(port);
1047 } else {
1048 vcc_put(port, true);
1049 }
1050 }
1051
1052 tty_port_destroy(tty->port);
1053 kfree(tty->port);
1054 tty->port = NULL;
1055}
1056
1057static const struct tty_operations vcc_ops = {
1058 .open = vcc_open,
1059 .close = vcc_close,
1060 .hangup = vcc_hangup,
1061 .write = vcc_write,
1062 .write_room = vcc_write_room,
1063 .chars_in_buffer = vcc_chars_in_buffer,
1064 .break_ctl = vcc_break_ctl,
1065 .install = vcc_install,
1066 .cleanup = vcc_cleanup,
1067};
1068
1069#define VCC_TTY_FLAGS (TTY_DRIVER_DYNAMIC_DEV | TTY_DRIVER_REAL_RAW)
1070
1071static int vcc_tty_init(void)
1072{
1073 int rv;
1074
1075 pr_info("VCC: %s\n", version);
1076
1077 vcc_tty_driver = tty_alloc_driver(VCC_MAX_PORTS, VCC_TTY_FLAGS);
1078 if (IS_ERR(vcc_tty_driver)) {
1079 pr_err("VCC: TTY driver alloc failed\n");
1080 return PTR_ERR(vcc_tty_driver);
1081 }
1082
1083 vcc_tty_driver->driver_name = vcc_driver_name;
1084 vcc_tty_driver->name = vcc_device_node;
1085
1086 vcc_tty_driver->minor_start = VCC_MINOR_START;
1087 vcc_tty_driver->type = TTY_DRIVER_TYPE_SYSTEM;
1088 vcc_tty_driver->init_termios = vcc_tty_termios;
1089
1090 tty_set_operations(vcc_tty_driver, &vcc_ops);
1091
1092 rv = tty_register_driver(vcc_tty_driver);
1093 if (rv) {
1094 pr_err("VCC: TTY driver registration failed\n");
1095 put_tty_driver(vcc_tty_driver);
1096 vcc_tty_driver = NULL;
1097 return rv;
1098 }
1099
1100 vccdbg("VCC: TTY driver registered\n");
1101
1102 return 0;
1103}
1104
1105static void vcc_tty_exit(void)
1106{
1107 tty_unregister_driver(vcc_tty_driver);
1108 put_tty_driver(vcc_tty_driver);
1109 vccdbg("VCC: TTY driver unregistered\n");
1110
1111 vcc_tty_driver = NULL;
1112}
1113
1114static int __init vcc_init(void)
1115{
1116 int rv;
1117
1118 rv = vcc_tty_init();
1119 if (rv) {
1120 pr_err("VCC: TTY init failed\n");
1121 return rv;
1122 }
1123
1124 rv = vio_register_driver(&vcc_driver);
1125 if (rv) {
1126 pr_err("VCC: VIO driver registration failed\n");
1127 vcc_tty_exit();
1128 } else {
1129 vccdbg("VCC: VIO driver registered successfully\n");
1130 }
1131
1132 return rv;
1133}
1134
1135static void __exit vcc_exit(void)
1136{
1137 vio_unregister_driver(&vcc_driver);
1138 vccdbg("VCC: VIO driver unregistered\n");
1139 vcc_tty_exit();
1140 vccdbg("VCC: TTY driver unregistered\n");
1141}
1142
1143module_init(vcc_init);
1144module_exit(vcc_exit);
1// SPDX-License-Identifier: GPL-2.0
2/* vcc.c: sun4v virtual channel concentrator
3 *
4 * Copyright (C) 2017 Oracle. All rights reserved.
5 */
6
7#include <linux/delay.h>
8#include <linux/interrupt.h>
9#include <linux/module.h>
10#include <linux/slab.h>
11#include <linux/sysfs.h>
12#include <linux/tty.h>
13#include <linux/tty_flip.h>
14#include <linux/termios_internal.h>
15#include <asm/vio.h>
16#include <asm/ldc.h>
17
18MODULE_DESCRIPTION("Sun LDOM virtual console concentrator driver");
19MODULE_LICENSE("GPL");
20MODULE_VERSION("1.1");
21
22struct vcc_port {
23 struct vio_driver_state vio;
24
25 spinlock_t lock;
26 char *domain;
27 struct tty_struct *tty; /* only populated while dev is open */
28 unsigned long index; /* index into the vcc_table */
29
30 u64 refcnt;
31 bool excl_locked;
32
33 bool removed;
34
35 /* This buffer is required to support the tty write_room interface
36 * and guarantee that any characters that the driver accepts will
37 * be eventually sent, either immediately or later.
38 */
39 int chars_in_buffer;
40 struct vio_vcc buffer;
41
42 struct timer_list rx_timer;
43 struct timer_list tx_timer;
44};
45
46/* Microseconds that thread will delay waiting for a vcc port ref */
47#define VCC_REF_DELAY 100
48
49#define VCC_MAX_PORTS 1024
50#define VCC_MINOR_START 0 /* must be zero */
51#define VCC_BUFF_LEN VIO_VCC_MTU_SIZE
52
53#define VCC_CTL_BREAK -1
54#define VCC_CTL_HUP -2
55
56static struct tty_driver *vcc_tty_driver;
57
58static struct vcc_port *vcc_table[VCC_MAX_PORTS];
59static DEFINE_SPINLOCK(vcc_table_lock);
60
61static unsigned int vcc_dbg;
62static unsigned int vcc_dbg_ldc;
63static unsigned int vcc_dbg_vio;
64
65module_param(vcc_dbg, uint, 0664);
66module_param(vcc_dbg_ldc, uint, 0664);
67module_param(vcc_dbg_vio, uint, 0664);
68
69#define VCC_DBG_DRV 0x1
70#define VCC_DBG_LDC 0x2
71#define VCC_DBG_PKT 0x4
72
73#define vccdbg(f, a...) \
74 do { \
75 if (vcc_dbg & VCC_DBG_DRV) \
76 pr_info(f, ## a); \
77 } while (0) \
78
79#define vccdbgl(l) \
80 do { \
81 if (vcc_dbg & VCC_DBG_LDC) \
82 ldc_print(l); \
83 } while (0) \
84
85#define vccdbgp(pkt) \
86 do { \
87 if (vcc_dbg & VCC_DBG_PKT) { \
88 int i; \
89 for (i = 0; i < pkt.tag.stype; i++) \
90 pr_info("[%c]", pkt.data[i]); \
91 } \
92 } while (0) \
93
94/* Note: Be careful when adding flags to this line discipline. Don't
95 * add anything that will cause echoing or we'll go into recursive
96 * loop echoing chars back and forth with the console drivers.
97 */
98static const struct ktermios vcc_tty_termios = {
99 .c_iflag = IGNBRK | IGNPAR,
100 .c_oflag = OPOST,
101 .c_cflag = B38400 | CS8 | CREAD | HUPCL,
102 .c_cc = INIT_C_CC,
103 .c_ispeed = 38400,
104 .c_ospeed = 38400
105};
106
107/**
108 * vcc_table_add() - Add VCC port to the VCC table
109 * @port: pointer to the VCC port
110 *
111 * Return: index of the port in the VCC table on success,
112 * -1 on failure
113 */
114static int vcc_table_add(struct vcc_port *port)
115{
116 unsigned long flags;
117 int i;
118
119 spin_lock_irqsave(&vcc_table_lock, flags);
120 for (i = VCC_MINOR_START; i < VCC_MAX_PORTS; i++) {
121 if (!vcc_table[i]) {
122 vcc_table[i] = port;
123 break;
124 }
125 }
126 spin_unlock_irqrestore(&vcc_table_lock, flags);
127
128 if (i < VCC_MAX_PORTS)
129 return i;
130 else
131 return -1;
132}
133
134/**
135 * vcc_table_remove() - Removes a VCC port from the VCC table
136 * @index: Index into the VCC table
137 */
138static void vcc_table_remove(unsigned long index)
139{
140 unsigned long flags;
141
142 if (WARN_ON(index >= VCC_MAX_PORTS))
143 return;
144
145 spin_lock_irqsave(&vcc_table_lock, flags);
146 vcc_table[index] = NULL;
147 spin_unlock_irqrestore(&vcc_table_lock, flags);
148}
149
150/**
151 * vcc_get() - Gets a reference to VCC port
152 * @index: Index into the VCC table
153 * @excl: Indicates if an exclusive access is requested
154 *
155 * Return: reference to the VCC port, if found
156 * NULL, if port not found
157 */
158static struct vcc_port *vcc_get(unsigned long index, bool excl)
159{
160 struct vcc_port *port;
161 unsigned long flags;
162
163try_again:
164 spin_lock_irqsave(&vcc_table_lock, flags);
165
166 port = vcc_table[index];
167 if (!port) {
168 spin_unlock_irqrestore(&vcc_table_lock, flags);
169 return NULL;
170 }
171
172 if (!excl) {
173 if (port->excl_locked) {
174 spin_unlock_irqrestore(&vcc_table_lock, flags);
175 udelay(VCC_REF_DELAY);
176 goto try_again;
177 }
178 port->refcnt++;
179 spin_unlock_irqrestore(&vcc_table_lock, flags);
180 return port;
181 }
182
183 if (port->refcnt) {
184 spin_unlock_irqrestore(&vcc_table_lock, flags);
185 /* Threads wanting exclusive access will wait half the time,
186 * probably giving them higher priority in the case of
187 * multiple waiters.
188 */
189 udelay(VCC_REF_DELAY/2);
190 goto try_again;
191 }
192
193 port->refcnt++;
194 port->excl_locked = true;
195 spin_unlock_irqrestore(&vcc_table_lock, flags);
196
197 return port;
198}
199
200/**
201 * vcc_put() - Returns a reference to VCC port
202 * @port: pointer to VCC port
203 * @excl: Indicates if the returned reference is an exclusive reference
204 *
205 * Note: It's the caller's responsibility to ensure the correct value
206 * for the excl flag
207 */
208static void vcc_put(struct vcc_port *port, bool excl)
209{
210 unsigned long flags;
211
212 if (!port)
213 return;
214
215 spin_lock_irqsave(&vcc_table_lock, flags);
216
217 /* check if caller attempted to put with the wrong flags */
218 if (WARN_ON((excl && !port->excl_locked) ||
219 (!excl && port->excl_locked)))
220 goto done;
221
222 port->refcnt--;
223
224 if (excl)
225 port->excl_locked = false;
226
227done:
228 spin_unlock_irqrestore(&vcc_table_lock, flags);
229}
230
231/**
232 * vcc_get_ne() - Get a non-exclusive reference to VCC port
233 * @index: Index into the VCC table
234 *
235 * Gets a non-exclusive reference to VCC port, if it's not removed
236 *
237 * Return: pointer to the VCC port, if found
238 * NULL, if port not found
239 */
240static struct vcc_port *vcc_get_ne(unsigned long index)
241{
242 struct vcc_port *port;
243
244 port = vcc_get(index, false);
245
246 if (port && port->removed) {
247 vcc_put(port, false);
248 return NULL;
249 }
250
251 return port;
252}
253
254static void vcc_kick_rx(struct vcc_port *port)
255{
256 struct vio_driver_state *vio = &port->vio;
257
258 assert_spin_locked(&port->lock);
259
260 if (!timer_pending(&port->rx_timer) && !port->removed) {
261 disable_irq_nosync(vio->vdev->rx_irq);
262 port->rx_timer.expires = (jiffies + 1);
263 add_timer(&port->rx_timer);
264 }
265}
266
267static void vcc_kick_tx(struct vcc_port *port)
268{
269 assert_spin_locked(&port->lock);
270
271 if (!timer_pending(&port->tx_timer) && !port->removed) {
272 port->tx_timer.expires = (jiffies + 1);
273 add_timer(&port->tx_timer);
274 }
275}
276
277static int vcc_rx_check(struct tty_struct *tty, int size)
278{
279 if (WARN_ON(!tty || !tty->port))
280 return 1;
281
282 /* tty_buffer_request_room won't sleep because it uses
283 * GFP_ATOMIC flag to allocate buffer
284 */
285 if (test_bit(TTY_THROTTLED, &tty->flags) ||
286 (tty_buffer_request_room(tty->port, VCC_BUFF_LEN) < VCC_BUFF_LEN))
287 return 0;
288
289 return 1;
290}
291
292static int vcc_rx(struct tty_struct *tty, char *buf, int size)
293{
294 int len = 0;
295
296 if (WARN_ON(!tty || !tty->port))
297 return len;
298
299 len = tty_insert_flip_string(tty->port, buf, size);
300 if (len)
301 tty_flip_buffer_push(tty->port);
302
303 return len;
304}
305
306static int vcc_ldc_read(struct vcc_port *port)
307{
308 struct vio_driver_state *vio = &port->vio;
309 struct tty_struct *tty;
310 struct vio_vcc pkt;
311 int rv = 0;
312
313 tty = port->tty;
314 if (!tty) {
315 rv = ldc_rx_reset(vio->lp);
316 vccdbg("VCC: reset rx q: rv=%d\n", rv);
317 goto done;
318 }
319
320 /* Read as long as LDC has incoming data. */
321 while (1) {
322 if (!vcc_rx_check(tty, VIO_VCC_MTU_SIZE)) {
323 vcc_kick_rx(port);
324 break;
325 }
326
327 vccdbgl(vio->lp);
328
329 rv = ldc_read(vio->lp, &pkt, sizeof(pkt));
330 if (rv <= 0)
331 break;
332
333 vccdbg("VCC: ldc_read()=%d\n", rv);
334 vccdbg("TAG [%02x:%02x:%04x:%08x]\n",
335 pkt.tag.type, pkt.tag.stype,
336 pkt.tag.stype_env, pkt.tag.sid);
337
338 if (pkt.tag.type == VIO_TYPE_DATA) {
339 vccdbgp(pkt);
340 /* vcc_rx_check ensures memory availability */
341 vcc_rx(tty, pkt.data, pkt.tag.stype);
342 } else {
343 pr_err("VCC: unknown msg [%02x:%02x:%04x:%08x]\n",
344 pkt.tag.type, pkt.tag.stype,
345 pkt.tag.stype_env, pkt.tag.sid);
346 rv = -ECONNRESET;
347 break;
348 }
349
350 WARN_ON(rv != LDC_PACKET_SIZE);
351 }
352
353done:
354 return rv;
355}
356
357static void vcc_rx_timer(struct timer_list *t)
358{
359 struct vcc_port *port = from_timer(port, t, rx_timer);
360 struct vio_driver_state *vio;
361 unsigned long flags;
362 int rv;
363
364 spin_lock_irqsave(&port->lock, flags);
365 port->rx_timer.expires = 0;
366
367 vio = &port->vio;
368
369 enable_irq(vio->vdev->rx_irq);
370
371 if (!port->tty || port->removed)
372 goto done;
373
374 rv = vcc_ldc_read(port);
375 if (rv == -ECONNRESET)
376 vio_conn_reset(vio);
377
378done:
379 spin_unlock_irqrestore(&port->lock, flags);
380 vcc_put(port, false);
381}
382
383static void vcc_tx_timer(struct timer_list *t)
384{
385 struct vcc_port *port = from_timer(port, t, tx_timer);
386 struct vio_vcc *pkt;
387 unsigned long flags;
388 int tosend = 0;
389 int rv;
390
391 spin_lock_irqsave(&port->lock, flags);
392 port->tx_timer.expires = 0;
393
394 if (!port->tty || port->removed)
395 goto done;
396
397 tosend = min(VCC_BUFF_LEN, port->chars_in_buffer);
398 if (!tosend)
399 goto done;
400
401 pkt = &port->buffer;
402 pkt->tag.type = VIO_TYPE_DATA;
403 pkt->tag.stype = tosend;
404 vccdbgl(port->vio.lp);
405
406 rv = ldc_write(port->vio.lp, pkt, (VIO_TAG_SIZE + tosend));
407 WARN_ON(!rv);
408
409 if (rv < 0) {
410 vccdbg("VCC: ldc_write()=%d\n", rv);
411 vcc_kick_tx(port);
412 } else {
413 struct tty_struct *tty = port->tty;
414
415 port->chars_in_buffer = 0;
416 if (tty)
417 tty_wakeup(tty);
418 }
419
420done:
421 spin_unlock_irqrestore(&port->lock, flags);
422 vcc_put(port, false);
423}
424
425/**
426 * vcc_event() - LDC event processing engine
427 * @arg: VCC private data
428 * @event: LDC event
429 *
430 * Handles LDC events for VCC
431 */
432static void vcc_event(void *arg, int event)
433{
434 struct vio_driver_state *vio;
435 struct vcc_port *port;
436 unsigned long flags;
437 int rv;
438
439 port = arg;
440 vio = &port->vio;
441
442 spin_lock_irqsave(&port->lock, flags);
443
444 switch (event) {
445 case LDC_EVENT_RESET:
446 case LDC_EVENT_UP:
447 vio_link_state_change(vio, event);
448 break;
449
450 case LDC_EVENT_DATA_READY:
451 rv = vcc_ldc_read(port);
452 if (rv == -ECONNRESET)
453 vio_conn_reset(vio);
454 break;
455
456 default:
457 pr_err("VCC: unexpected LDC event(%d)\n", event);
458 }
459
460 spin_unlock_irqrestore(&port->lock, flags);
461}
462
463static struct ldc_channel_config vcc_ldc_cfg = {
464 .event = vcc_event,
465 .mtu = VIO_VCC_MTU_SIZE,
466 .mode = LDC_MODE_RAW,
467 .debug = 0,
468};
469
470/* Ordered from largest major to lowest */
471static struct vio_version vcc_versions[] = {
472 { .major = 1, .minor = 0 },
473};
474
475static struct tty_port_operations vcc_port_ops = { 0 };
476
477static ssize_t domain_show(struct device *dev,
478 struct device_attribute *attr,
479 char *buf)
480{
481 struct vcc_port *port;
482 int rv;
483
484 port = dev_get_drvdata(dev);
485 if (!port)
486 return -ENODEV;
487
488 rv = scnprintf(buf, PAGE_SIZE, "%s\n", port->domain);
489
490 return rv;
491}
492
493static int vcc_send_ctl(struct vcc_port *port, int ctl)
494{
495 struct vio_vcc pkt;
496 int rv;
497
498 pkt.tag.type = VIO_TYPE_CTRL;
499 pkt.tag.sid = ctl;
500 pkt.tag.stype = 0;
501
502 rv = ldc_write(port->vio.lp, &pkt, sizeof(pkt.tag));
503 WARN_ON(!rv);
504 vccdbg("VCC: ldc_write(%ld)=%d\n", sizeof(pkt.tag), rv);
505
506 return rv;
507}
508
509static ssize_t break_store(struct device *dev,
510 struct device_attribute *attr,
511 const char *buf, size_t count)
512{
513 struct vcc_port *port;
514 unsigned long flags;
515 int rv = count;
516 int brk;
517
518 port = dev_get_drvdata(dev);
519 if (!port)
520 return -ENODEV;
521
522 spin_lock_irqsave(&port->lock, flags);
523
524 if (sscanf(buf, "%ud", &brk) != 1 || brk != 1)
525 rv = -EINVAL;
526 else if (vcc_send_ctl(port, VCC_CTL_BREAK) < 0)
527 vcc_kick_tx(port);
528
529 spin_unlock_irqrestore(&port->lock, flags);
530
531 return rv;
532}
533
534static DEVICE_ATTR_ADMIN_RO(domain);
535static DEVICE_ATTR_WO(break);
536
537static struct attribute *vcc_sysfs_entries[] = {
538 &dev_attr_domain.attr,
539 &dev_attr_break.attr,
540 NULL
541};
542
543static struct attribute_group vcc_attribute_group = {
544 .name = NULL,
545 .attrs = vcc_sysfs_entries,
546};
547
548/**
549 * vcc_probe() - Initialize VCC port
550 * @vdev: Pointer to VIO device of the new VCC port
551 * @id: VIO device ID
552 *
553 * Initializes a VCC port to receive serial console data from
554 * the guest domain. Sets up a TTY end point on the control
555 * domain. Sets up VIO/LDC link between the guest & control
556 * domain endpoints.
557 *
558 * Return: status of the probe
559 */
560static int vcc_probe(struct vio_dev *vdev, const struct vio_device_id *id)
561{
562 struct mdesc_handle *hp;
563 struct vcc_port *port;
564 struct device *dev;
565 const char *domain;
566 char *name;
567 u64 node;
568 int rv;
569
570 vccdbg("VCC: name=%s\n", dev_name(&vdev->dev));
571
572 if (!vcc_tty_driver) {
573 pr_err("VCC: TTY driver not registered\n");
574 return -ENODEV;
575 }
576
577 port = kzalloc(sizeof(struct vcc_port), GFP_KERNEL);
578 if (!port)
579 return -ENOMEM;
580
581 name = kstrdup(dev_name(&vdev->dev), GFP_KERNEL);
582
583 rv = vio_driver_init(&port->vio, vdev, VDEV_CONSOLE_CON, vcc_versions,
584 ARRAY_SIZE(vcc_versions), NULL, name);
585 if (rv)
586 goto free_port;
587
588 port->vio.debug = vcc_dbg_vio;
589 vcc_ldc_cfg.debug = vcc_dbg_ldc;
590
591 rv = vio_ldc_alloc(&port->vio, &vcc_ldc_cfg, port);
592 if (rv)
593 goto free_port;
594
595 spin_lock_init(&port->lock);
596
597 port->index = vcc_table_add(port);
598 if (port->index == -1) {
599 pr_err("VCC: no more TTY indices left for allocation\n");
600 rv = -ENOMEM;
601 goto free_ldc;
602 }
603
604 /* Register the device using VCC table index as TTY index */
605 dev = tty_register_device(vcc_tty_driver, port->index, &vdev->dev);
606 if (IS_ERR(dev)) {
607 rv = PTR_ERR(dev);
608 goto free_table;
609 }
610
611 hp = mdesc_grab();
612
613 node = vio_vdev_node(hp, vdev);
614 if (node == MDESC_NODE_NULL) {
615 rv = -ENXIO;
616 mdesc_release(hp);
617 goto unreg_tty;
618 }
619
620 domain = mdesc_get_property(hp, node, "vcc-domain-name", NULL);
621 if (!domain) {
622 rv = -ENXIO;
623 mdesc_release(hp);
624 goto unreg_tty;
625 }
626 port->domain = kstrdup(domain, GFP_KERNEL);
627
628 mdesc_release(hp);
629
630 rv = sysfs_create_group(&vdev->dev.kobj, &vcc_attribute_group);
631 if (rv)
632 goto free_domain;
633
634 timer_setup(&port->rx_timer, vcc_rx_timer, 0);
635 timer_setup(&port->tx_timer, vcc_tx_timer, 0);
636
637 dev_set_drvdata(&vdev->dev, port);
638
639 /* It's possible to receive IRQs in the middle of vio_port_up. Disable
640 * IRQs until the port is up.
641 */
642 disable_irq_nosync(vdev->rx_irq);
643 vio_port_up(&port->vio);
644 enable_irq(vdev->rx_irq);
645
646 return 0;
647
648free_domain:
649 kfree(port->domain);
650unreg_tty:
651 tty_unregister_device(vcc_tty_driver, port->index);
652free_table:
653 vcc_table_remove(port->index);
654free_ldc:
655 vio_ldc_free(&port->vio);
656free_port:
657 kfree(name);
658 kfree(port);
659
660 return rv;
661}
662
663/**
664 * vcc_remove() - Terminate a VCC port
665 * @vdev: Pointer to VIO device of the VCC port
666 *
667 * Terminates a VCC port. Sets up the teardown of TTY and
668 * VIO/LDC link between guest and primary domains.
669 *
670 * Return: status of removal
671 */
672static void vcc_remove(struct vio_dev *vdev)
673{
674 struct vcc_port *port = dev_get_drvdata(&vdev->dev);
675
676 del_timer_sync(&port->rx_timer);
677 del_timer_sync(&port->tx_timer);
678
679 /* If there's a process with the device open, do a synchronous
680 * hangup of the TTY. This *may* cause the process to call close
681 * asynchronously, but it's not guaranteed.
682 */
683 if (port->tty)
684 tty_vhangup(port->tty);
685
686 /* Get exclusive reference to VCC, ensures that there are no other
687 * clients to this port. This cannot fail.
688 */
689 vcc_get(port->index, true);
690
691 tty_unregister_device(vcc_tty_driver, port->index);
692
693 del_timer_sync(&port->vio.timer);
694 vio_ldc_free(&port->vio);
695 sysfs_remove_group(&vdev->dev.kobj, &vcc_attribute_group);
696 dev_set_drvdata(&vdev->dev, NULL);
697 if (port->tty) {
698 port->removed = true;
699 vcc_put(port, true);
700 } else {
701 vcc_table_remove(port->index);
702
703 kfree(port->vio.name);
704 kfree(port->domain);
705 kfree(port);
706 }
707}
708
709static const struct vio_device_id vcc_match[] = {
710 {
711 .type = "vcc-port",
712 },
713 {},
714};
715MODULE_DEVICE_TABLE(vio, vcc_match);
716
717static struct vio_driver vcc_driver = {
718 .id_table = vcc_match,
719 .probe = vcc_probe,
720 .remove = vcc_remove,
721 .name = "vcc",
722};
723
724static int vcc_open(struct tty_struct *tty, struct file *vcc_file)
725{
726 struct vcc_port *port;
727
728 if (tty->count > 1)
729 return -EBUSY;
730
731 port = vcc_get_ne(tty->index);
732 if (unlikely(!port)) {
733 pr_err("VCC: open: Failed to find VCC port\n");
734 return -ENODEV;
735 }
736
737 if (unlikely(!port->vio.lp)) {
738 pr_err("VCC: open: LDC channel not configured\n");
739 vcc_put(port, false);
740 return -EPIPE;
741 }
742 vccdbgl(port->vio.lp);
743
744 vcc_put(port, false);
745
746 if (unlikely(!tty->port)) {
747 pr_err("VCC: open: TTY port not found\n");
748 return -ENXIO;
749 }
750
751 if (unlikely(!tty->port->ops)) {
752 pr_err("VCC: open: TTY ops not defined\n");
753 return -ENXIO;
754 }
755
756 return tty_port_open(tty->port, tty, vcc_file);
757}
758
759static void vcc_close(struct tty_struct *tty, struct file *vcc_file)
760{
761 if (unlikely(tty->count > 1))
762 return;
763
764 if (unlikely(!tty->port)) {
765 pr_err("VCC: close: TTY port not found\n");
766 return;
767 }
768
769 tty_port_close(tty->port, tty, vcc_file);
770}
771
772static void vcc_ldc_hup(struct vcc_port *port)
773{
774 unsigned long flags;
775
776 spin_lock_irqsave(&port->lock, flags);
777
778 if (vcc_send_ctl(port, VCC_CTL_HUP) < 0)
779 vcc_kick_tx(port);
780
781 spin_unlock_irqrestore(&port->lock, flags);
782}
783
784static void vcc_hangup(struct tty_struct *tty)
785{
786 struct vcc_port *port;
787
788 port = vcc_get_ne(tty->index);
789 if (unlikely(!port)) {
790 pr_err("VCC: hangup: Failed to find VCC port\n");
791 return;
792 }
793
794 if (unlikely(!tty->port)) {
795 pr_err("VCC: hangup: TTY port not found\n");
796 vcc_put(port, false);
797 return;
798 }
799
800 vcc_ldc_hup(port);
801
802 vcc_put(port, false);
803
804 tty_port_hangup(tty->port);
805}
806
807static int vcc_write(struct tty_struct *tty, const unsigned char *buf,
808 int count)
809{
810 struct vcc_port *port;
811 struct vio_vcc *pkt;
812 unsigned long flags;
813 int total_sent = 0;
814 int tosend = 0;
815 int rv = -EINVAL;
816
817 port = vcc_get_ne(tty->index);
818 if (unlikely(!port)) {
819 pr_err("VCC: write: Failed to find VCC port");
820 return -ENODEV;
821 }
822
823 spin_lock_irqsave(&port->lock, flags);
824
825 pkt = &port->buffer;
826 pkt->tag.type = VIO_TYPE_DATA;
827
828 while (count > 0) {
829 /* Minimum of data to write and space available */
830 tosend = min(count, (VCC_BUFF_LEN - port->chars_in_buffer));
831
832 if (!tosend)
833 break;
834
835 memcpy(&pkt->data[port->chars_in_buffer], &buf[total_sent],
836 tosend);
837 port->chars_in_buffer += tosend;
838 pkt->tag.stype = tosend;
839
840 vccdbg("TAG [%02x:%02x:%04x:%08x]\n", pkt->tag.type,
841 pkt->tag.stype, pkt->tag.stype_env, pkt->tag.sid);
842 vccdbg("DATA [%s]\n", pkt->data);
843 vccdbgl(port->vio.lp);
844
845 /* Since we know we have enough room in VCC buffer for tosend
846 * we record that it was sent regardless of whether the
847 * hypervisor actually took it because we have it buffered.
848 */
849 rv = ldc_write(port->vio.lp, pkt, (VIO_TAG_SIZE + tosend));
850 vccdbg("VCC: write: ldc_write(%d)=%d\n",
851 (VIO_TAG_SIZE + tosend), rv);
852
853 total_sent += tosend;
854 count -= tosend;
855 if (rv < 0) {
856 vcc_kick_tx(port);
857 break;
858 }
859
860 port->chars_in_buffer = 0;
861 }
862
863 spin_unlock_irqrestore(&port->lock, flags);
864
865 vcc_put(port, false);
866
867 vccdbg("VCC: write: total=%d rv=%d", total_sent, rv);
868
869 return total_sent ? total_sent : rv;
870}
871
872static unsigned int vcc_write_room(struct tty_struct *tty)
873{
874 struct vcc_port *port;
875 unsigned int num;
876
877 port = vcc_get_ne(tty->index);
878 if (unlikely(!port)) {
879 pr_err("VCC: write_room: Failed to find VCC port\n");
880 return 0;
881 }
882
883 num = VCC_BUFF_LEN - port->chars_in_buffer;
884
885 vcc_put(port, false);
886
887 return num;
888}
889
890static unsigned int vcc_chars_in_buffer(struct tty_struct *tty)
891{
892 struct vcc_port *port;
893 unsigned int num;
894
895 port = vcc_get_ne(tty->index);
896 if (unlikely(!port)) {
897 pr_err("VCC: chars_in_buffer: Failed to find VCC port\n");
898 return 0;
899 }
900
901 num = port->chars_in_buffer;
902
903 vcc_put(port, false);
904
905 return num;
906}
907
908static int vcc_break_ctl(struct tty_struct *tty, int state)
909{
910 struct vcc_port *port;
911 unsigned long flags;
912
913 port = vcc_get_ne(tty->index);
914 if (unlikely(!port)) {
915 pr_err("VCC: break_ctl: Failed to find VCC port\n");
916 return -ENODEV;
917 }
918
919 /* Turn off break */
920 if (state == 0) {
921 vcc_put(port, false);
922 return 0;
923 }
924
925 spin_lock_irqsave(&port->lock, flags);
926
927 if (vcc_send_ctl(port, VCC_CTL_BREAK) < 0)
928 vcc_kick_tx(port);
929
930 spin_unlock_irqrestore(&port->lock, flags);
931
932 vcc_put(port, false);
933
934 return 0;
935}
936
937static int vcc_install(struct tty_driver *driver, struct tty_struct *tty)
938{
939 struct vcc_port *port_vcc;
940 struct tty_port *port_tty;
941 int ret;
942
943 if (tty->index >= VCC_MAX_PORTS)
944 return -EINVAL;
945
946 ret = tty_standard_install(driver, tty);
947 if (ret)
948 return ret;
949
950 port_tty = kzalloc(sizeof(struct tty_port), GFP_KERNEL);
951 if (!port_tty)
952 return -ENOMEM;
953
954 port_vcc = vcc_get(tty->index, true);
955 if (!port_vcc) {
956 pr_err("VCC: install: Failed to find VCC port\n");
957 tty->port = NULL;
958 kfree(port_tty);
959 return -ENODEV;
960 }
961
962 tty_port_init(port_tty);
963 port_tty->ops = &vcc_port_ops;
964 tty->port = port_tty;
965
966 port_vcc->tty = tty;
967
968 vcc_put(port_vcc, true);
969
970 return 0;
971}
972
973static void vcc_cleanup(struct tty_struct *tty)
974{
975 struct vcc_port *port;
976
977 port = vcc_get(tty->index, true);
978 if (port) {
979 port->tty = NULL;
980
981 if (port->removed) {
982 vcc_table_remove(tty->index);
983 kfree(port->vio.name);
984 kfree(port->domain);
985 kfree(port);
986 } else {
987 vcc_put(port, true);
988 }
989 }
990
991 tty_port_destroy(tty->port);
992 kfree(tty->port);
993 tty->port = NULL;
994}
995
996static const struct tty_operations vcc_ops = {
997 .open = vcc_open,
998 .close = vcc_close,
999 .hangup = vcc_hangup,
1000 .write = vcc_write,
1001 .write_room = vcc_write_room,
1002 .chars_in_buffer = vcc_chars_in_buffer,
1003 .break_ctl = vcc_break_ctl,
1004 .install = vcc_install,
1005 .cleanup = vcc_cleanup,
1006};
1007
1008#define VCC_TTY_FLAGS (TTY_DRIVER_DYNAMIC_DEV | TTY_DRIVER_REAL_RAW)
1009
1010static int vcc_tty_init(void)
1011{
1012 int rv;
1013
1014 vcc_tty_driver = tty_alloc_driver(VCC_MAX_PORTS, VCC_TTY_FLAGS);
1015 if (IS_ERR(vcc_tty_driver)) {
1016 pr_err("VCC: TTY driver alloc failed\n");
1017 return PTR_ERR(vcc_tty_driver);
1018 }
1019
1020 vcc_tty_driver->driver_name = "vcc";
1021 vcc_tty_driver->name = "vcc";
1022
1023 vcc_tty_driver->minor_start = VCC_MINOR_START;
1024 vcc_tty_driver->type = TTY_DRIVER_TYPE_SYSTEM;
1025 vcc_tty_driver->init_termios = vcc_tty_termios;
1026
1027 tty_set_operations(vcc_tty_driver, &vcc_ops);
1028
1029 rv = tty_register_driver(vcc_tty_driver);
1030 if (rv) {
1031 pr_err("VCC: TTY driver registration failed\n");
1032 tty_driver_kref_put(vcc_tty_driver);
1033 vcc_tty_driver = NULL;
1034 return rv;
1035 }
1036
1037 vccdbg("VCC: TTY driver registered\n");
1038
1039 return 0;
1040}
1041
1042static void vcc_tty_exit(void)
1043{
1044 tty_unregister_driver(vcc_tty_driver);
1045 tty_driver_kref_put(vcc_tty_driver);
1046 vccdbg("VCC: TTY driver unregistered\n");
1047
1048 vcc_tty_driver = NULL;
1049}
1050
1051static int __init vcc_init(void)
1052{
1053 int rv;
1054
1055 rv = vcc_tty_init();
1056 if (rv) {
1057 pr_err("VCC: TTY init failed\n");
1058 return rv;
1059 }
1060
1061 rv = vio_register_driver(&vcc_driver);
1062 if (rv) {
1063 pr_err("VCC: VIO driver registration failed\n");
1064 vcc_tty_exit();
1065 } else {
1066 vccdbg("VCC: VIO driver registered successfully\n");
1067 }
1068
1069 return rv;
1070}
1071
1072static void __exit vcc_exit(void)
1073{
1074 vio_unregister_driver(&vcc_driver);
1075 vccdbg("VCC: VIO driver unregistered\n");
1076 vcc_tty_exit();
1077 vccdbg("VCC: TTY driver unregistered\n");
1078}
1079
1080module_init(vcc_init);
1081module_exit(vcc_exit);