1 // SPDX-License-Identifier: GPL-2.0
2 /* vcc.c: sun4v virtual channel concentrator
4 * Copyright (C) 2017 Oracle. All rights reserved.
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>
18 MODULE_DESCRIPTION("Sun LDOM virtual console concentrator driver");
19 MODULE_LICENSE("GPL");
20 MODULE_VERSION("1.1");
23 struct vio_driver_state vio;
27 struct tty_struct *tty; /* only populated while dev is open */
28 unsigned long index; /* index into the vcc_table */
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.
40 struct vio_vcc buffer;
42 struct timer_list rx_timer;
43 struct timer_list tx_timer;
46 /* Microseconds that thread will delay waiting for a vcc port ref */
47 #define VCC_REF_DELAY 100
49 #define VCC_MAX_PORTS 1024
50 #define VCC_MINOR_START 0 /* must be zero */
51 #define VCC_BUFF_LEN VIO_VCC_MTU_SIZE
53 #define VCC_CTL_BREAK -1
54 #define VCC_CTL_HUP -2
56 static struct tty_driver *vcc_tty_driver;
58 static struct vcc_port *vcc_table[VCC_MAX_PORTS];
59 static DEFINE_SPINLOCK(vcc_table_lock);
61 static unsigned int vcc_dbg;
62 static unsigned int vcc_dbg_ldc;
63 static unsigned int vcc_dbg_vio;
65 module_param(vcc_dbg, uint, 0664);
66 module_param(vcc_dbg_ldc, uint, 0664);
67 module_param(vcc_dbg_vio, uint, 0664);
69 #define VCC_DBG_DRV 0x1
70 #define VCC_DBG_LDC 0x2
71 #define VCC_DBG_PKT 0x4
73 #define vccdbg(f, a...) \
75 if (vcc_dbg & VCC_DBG_DRV) \
81 if (vcc_dbg & VCC_DBG_LDC) \
85 #define vccdbgp(pkt) \
87 if (vcc_dbg & VCC_DBG_PKT) { \
89 for (i = 0; i < pkt.tag.stype; i++) \
90 pr_info("[%c]", pkt.data[i]); \
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.
98 static const struct ktermios vcc_tty_termios = {
99 .c_iflag = IGNBRK | IGNPAR,
101 .c_cflag = B38400 | CS8 | CREAD | HUPCL,
108 * vcc_table_add() - Add VCC port to the VCC table
109 * @port: pointer to the VCC port
111 * Return: index of the port in the VCC table on success,
114 static int vcc_table_add(struct vcc_port *port)
119 spin_lock_irqsave(&vcc_table_lock, flags);
120 for (i = VCC_MINOR_START; i < VCC_MAX_PORTS; i++) {
126 spin_unlock_irqrestore(&vcc_table_lock, flags);
128 if (i < VCC_MAX_PORTS)
135 * vcc_table_remove() - Removes a VCC port from the VCC table
136 * @index: Index into the VCC table
138 static void vcc_table_remove(unsigned long index)
142 if (WARN_ON(index >= VCC_MAX_PORTS))
145 spin_lock_irqsave(&vcc_table_lock, flags);
146 vcc_table[index] = NULL;
147 spin_unlock_irqrestore(&vcc_table_lock, flags);
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
155 * Return: reference to the VCC port, if found
156 * NULL, if port not found
158 static struct vcc_port *vcc_get(unsigned long index, bool excl)
160 struct vcc_port *port;
164 spin_lock_irqsave(&vcc_table_lock, flags);
166 port = vcc_table[index];
168 spin_unlock_irqrestore(&vcc_table_lock, flags);
173 if (port->excl_locked) {
174 spin_unlock_irqrestore(&vcc_table_lock, flags);
175 udelay(VCC_REF_DELAY);
179 spin_unlock_irqrestore(&vcc_table_lock, flags);
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
189 udelay(VCC_REF_DELAY/2);
194 port->excl_locked = true;
195 spin_unlock_irqrestore(&vcc_table_lock, flags);
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
205 * Note: It's the caller's responsibility to ensure the correct value
208 static void vcc_put(struct vcc_port *port, bool excl)
215 spin_lock_irqsave(&vcc_table_lock, flags);
217 /* check if caller attempted to put with the wrong flags */
218 if (WARN_ON((excl && !port->excl_locked) ||
219 (!excl && port->excl_locked)))
225 port->excl_locked = false;
228 spin_unlock_irqrestore(&vcc_table_lock, flags);
232 * vcc_get_ne() - Get a non-exclusive reference to VCC port
233 * @index: Index into the VCC table
235 * Gets a non-exclusive reference to VCC port, if it's not removed
237 * Return: pointer to the VCC port, if found
238 * NULL, if port not found
240 static struct vcc_port *vcc_get_ne(unsigned long index)
242 struct vcc_port *port;
244 port = vcc_get(index, false);
246 if (port && port->removed) {
247 vcc_put(port, false);
254 static void vcc_kick_rx(struct vcc_port *port)
256 struct vio_driver_state *vio = &port->vio;
258 assert_spin_locked(&port->lock);
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);
267 static void vcc_kick_tx(struct vcc_port *port)
269 assert_spin_locked(&port->lock);
271 if (!timer_pending(&port->tx_timer) && !port->removed) {
272 port->tx_timer.expires = (jiffies + 1);
273 add_timer(&port->tx_timer);
277 static int vcc_rx_check(struct tty_struct *tty, int size)
279 if (WARN_ON(!tty || !tty->port))
282 /* tty_buffer_request_room won't sleep because it uses
283 * GFP_ATOMIC flag to allocate buffer
285 if (test_bit(TTY_THROTTLED, &tty->flags) ||
286 (tty_buffer_request_room(tty->port, VCC_BUFF_LEN) < VCC_BUFF_LEN))
292 static int vcc_rx(struct tty_struct *tty, char *buf, int size)
296 if (WARN_ON(!tty || !tty->port))
299 len = tty_insert_flip_string(tty->port, buf, size);
301 tty_flip_buffer_push(tty->port);
306 static int vcc_ldc_read(struct vcc_port *port)
308 struct vio_driver_state *vio = &port->vio;
309 struct tty_struct *tty;
315 rv = ldc_rx_reset(vio->lp);
316 vccdbg("VCC: reset rx q: rv=%d\n", rv);
320 /* Read as long as LDC has incoming data. */
322 if (!vcc_rx_check(tty, VIO_VCC_MTU_SIZE)) {
329 rv = ldc_read(vio->lp, &pkt, sizeof(pkt));
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);
338 if (pkt.tag.type == VIO_TYPE_DATA) {
340 /* vcc_rx_check ensures memory availability */
341 vcc_rx(tty, pkt.data, pkt.tag.stype);
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);
350 WARN_ON(rv != LDC_PACKET_SIZE);
357 static void vcc_rx_timer(struct timer_list *t)
359 struct vcc_port *port = from_timer(port, t, rx_timer);
360 struct vio_driver_state *vio;
364 spin_lock_irqsave(&port->lock, flags);
365 port->rx_timer.expires = 0;
369 enable_irq(vio->vdev->rx_irq);
371 if (!port->tty || port->removed)
374 rv = vcc_ldc_read(port);
375 if (rv == -ECONNRESET)
379 spin_unlock_irqrestore(&port->lock, flags);
380 vcc_put(port, false);
383 static void vcc_tx_timer(struct timer_list *t)
385 struct vcc_port *port = from_timer(port, t, tx_timer);
391 spin_lock_irqsave(&port->lock, flags);
392 port->tx_timer.expires = 0;
394 if (!port->tty || port->removed)
397 tosend = min(VCC_BUFF_LEN, port->chars_in_buffer);
402 pkt->tag.type = VIO_TYPE_DATA;
403 pkt->tag.stype = tosend;
404 vccdbgl(port->vio.lp);
406 rv = ldc_write(port->vio.lp, pkt, (VIO_TAG_SIZE + tosend));
410 vccdbg("VCC: ldc_write()=%d\n", rv);
413 struct tty_struct *tty = port->tty;
415 port->chars_in_buffer = 0;
421 spin_unlock_irqrestore(&port->lock, flags);
422 vcc_put(port, false);
426 * vcc_event() - LDC event processing engine
427 * @arg: VCC private data
430 * Handles LDC events for VCC
432 static void vcc_event(void *arg, int event)
434 struct vio_driver_state *vio;
435 struct vcc_port *port;
442 spin_lock_irqsave(&port->lock, flags);
445 case LDC_EVENT_RESET:
447 vio_link_state_change(vio, event);
450 case LDC_EVENT_DATA_READY:
451 rv = vcc_ldc_read(port);
452 if (rv == -ECONNRESET)
457 pr_err("VCC: unexpected LDC event(%d)\n", event);
460 spin_unlock_irqrestore(&port->lock, flags);
463 static struct ldc_channel_config vcc_ldc_cfg = {
465 .mtu = VIO_VCC_MTU_SIZE,
466 .mode = LDC_MODE_RAW,
470 /* Ordered from largest major to lowest */
471 static struct vio_version vcc_versions[] = {
472 { .major = 1, .minor = 0 },
475 static struct tty_port_operations vcc_port_ops = { 0 };
477 static ssize_t domain_show(struct device *dev,
478 struct device_attribute *attr,
481 struct vcc_port *port;
484 port = dev_get_drvdata(dev);
488 rv = scnprintf(buf, PAGE_SIZE, "%s\n", port->domain);
493 static int vcc_send_ctl(struct vcc_port *port, int ctl)
498 pkt.tag.type = VIO_TYPE_CTRL;
502 rv = ldc_write(port->vio.lp, &pkt, sizeof(pkt.tag));
504 vccdbg("VCC: ldc_write(%ld)=%d\n", sizeof(pkt.tag), rv);
509 static ssize_t break_store(struct device *dev,
510 struct device_attribute *attr,
511 const char *buf, size_t count)
513 struct vcc_port *port;
518 port = dev_get_drvdata(dev);
522 spin_lock_irqsave(&port->lock, flags);
524 if (sscanf(buf, "%ud", &brk) != 1 || brk != 1)
526 else if (vcc_send_ctl(port, VCC_CTL_BREAK) < 0)
529 spin_unlock_irqrestore(&port->lock, flags);
534 static DEVICE_ATTR_ADMIN_RO(domain);
535 static DEVICE_ATTR_WO(break);
537 static struct attribute *vcc_sysfs_entries[] = {
538 &dev_attr_domain.attr,
539 &dev_attr_break.attr,
543 static struct attribute_group vcc_attribute_group = {
545 .attrs = vcc_sysfs_entries,
549 * vcc_probe() - Initialize VCC port
550 * @vdev: Pointer to VIO device of the new VCC port
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
558 * Return: status of the probe
560 static int vcc_probe(struct vio_dev *vdev, const struct vio_device_id *id)
562 struct mdesc_handle *hp;
563 struct vcc_port *port;
570 vccdbg("VCC: name=%s\n", dev_name(&vdev->dev));
572 if (!vcc_tty_driver) {
573 pr_err("VCC: TTY driver not registered\n");
577 port = kzalloc(sizeof(struct vcc_port), GFP_KERNEL);
581 name = kstrdup(dev_name(&vdev->dev), GFP_KERNEL);
583 rv = vio_driver_init(&port->vio, vdev, VDEV_CONSOLE_CON, vcc_versions,
584 ARRAY_SIZE(vcc_versions), NULL, name);
588 port->vio.debug = vcc_dbg_vio;
589 vcc_ldc_cfg.debug = vcc_dbg_ldc;
591 rv = vio_ldc_alloc(&port->vio, &vcc_ldc_cfg, port);
595 spin_lock_init(&port->lock);
597 port->index = vcc_table_add(port);
598 if (port->index == -1) {
599 pr_err("VCC: no more TTY indices left for allocation\n");
604 /* Register the device using VCC table index as TTY index */
605 dev = tty_register_device(vcc_tty_driver, port->index, &vdev->dev);
613 node = vio_vdev_node(hp, vdev);
614 if (node == MDESC_NODE_NULL) {
620 domain = mdesc_get_property(hp, node, "vcc-domain-name", NULL);
626 port->domain = kstrdup(domain, GFP_KERNEL);
630 rv = sysfs_create_group(&vdev->dev.kobj, &vcc_attribute_group);
634 timer_setup(&port->rx_timer, vcc_rx_timer, 0);
635 timer_setup(&port->tx_timer, vcc_tx_timer, 0);
637 dev_set_drvdata(&vdev->dev, port);
639 /* It's possible to receive IRQs in the middle of vio_port_up. Disable
640 * IRQs until the port is up.
642 disable_irq_nosync(vdev->rx_irq);
643 vio_port_up(&port->vio);
644 enable_irq(vdev->rx_irq);
651 tty_unregister_device(vcc_tty_driver, port->index);
653 vcc_table_remove(port->index);
655 vio_ldc_free(&port->vio);
664 * vcc_remove() - Terminate a VCC port
665 * @vdev: Pointer to VIO device of the VCC port
667 * Terminates a VCC port. Sets up the teardown of TTY and
668 * VIO/LDC link between guest and primary domains.
670 * Return: status of removal
672 static void vcc_remove(struct vio_dev *vdev)
674 struct vcc_port *port = dev_get_drvdata(&vdev->dev);
676 del_timer_sync(&port->rx_timer);
677 del_timer_sync(&port->tx_timer);
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.
684 tty_vhangup(port->tty);
686 /* Get exclusive reference to VCC, ensures that there are no other
687 * clients to this port. This cannot fail.
689 vcc_get(port->index, true);
691 tty_unregister_device(vcc_tty_driver, port->index);
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);
698 port->removed = true;
701 vcc_table_remove(port->index);
703 kfree(port->vio.name);
709 static const struct vio_device_id vcc_match[] = {
715 MODULE_DEVICE_TABLE(vio, vcc_match);
717 static struct vio_driver vcc_driver = {
718 .id_table = vcc_match,
720 .remove = vcc_remove,
724 static int vcc_open(struct tty_struct *tty, struct file *vcc_file)
726 struct vcc_port *port;
731 port = vcc_get_ne(tty->index);
732 if (unlikely(!port)) {
733 pr_err("VCC: open: Failed to find VCC port\n");
737 if (unlikely(!port->vio.lp)) {
738 pr_err("VCC: open: LDC channel not configured\n");
739 vcc_put(port, false);
742 vccdbgl(port->vio.lp);
744 vcc_put(port, false);
746 if (unlikely(!tty->port)) {
747 pr_err("VCC: open: TTY port not found\n");
751 if (unlikely(!tty->port->ops)) {
752 pr_err("VCC: open: TTY ops not defined\n");
756 return tty_port_open(tty->port, tty, vcc_file);
759 static void vcc_close(struct tty_struct *tty, struct file *vcc_file)
761 if (unlikely(tty->count > 1))
764 if (unlikely(!tty->port)) {
765 pr_err("VCC: close: TTY port not found\n");
769 tty_port_close(tty->port, tty, vcc_file);
772 static void vcc_ldc_hup(struct vcc_port *port)
776 spin_lock_irqsave(&port->lock, flags);
778 if (vcc_send_ctl(port, VCC_CTL_HUP) < 0)
781 spin_unlock_irqrestore(&port->lock, flags);
784 static void vcc_hangup(struct tty_struct *tty)
786 struct vcc_port *port;
788 port = vcc_get_ne(tty->index);
789 if (unlikely(!port)) {
790 pr_err("VCC: hangup: Failed to find VCC port\n");
794 if (unlikely(!tty->port)) {
795 pr_err("VCC: hangup: TTY port not found\n");
796 vcc_put(port, false);
802 vcc_put(port, false);
804 tty_port_hangup(tty->port);
807 static int vcc_write(struct tty_struct *tty, const unsigned char *buf,
810 struct vcc_port *port;
817 port = vcc_get_ne(tty->index);
818 if (unlikely(!port)) {
819 pr_err("VCC: write: Failed to find VCC port");
823 spin_lock_irqsave(&port->lock, flags);
826 pkt->tag.type = VIO_TYPE_DATA;
829 /* Minimum of data to write and space available */
830 tosend = min(count, (VCC_BUFF_LEN - port->chars_in_buffer));
835 memcpy(&pkt->data[port->chars_in_buffer], &buf[total_sent],
837 port->chars_in_buffer += tosend;
838 pkt->tag.stype = tosend;
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);
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.
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);
853 total_sent += tosend;
860 port->chars_in_buffer = 0;
863 spin_unlock_irqrestore(&port->lock, flags);
865 vcc_put(port, false);
867 vccdbg("VCC: write: total=%d rv=%d", total_sent, rv);
869 return total_sent ? total_sent : rv;
872 static unsigned int vcc_write_room(struct tty_struct *tty)
874 struct vcc_port *port;
877 port = vcc_get_ne(tty->index);
878 if (unlikely(!port)) {
879 pr_err("VCC: write_room: Failed to find VCC port\n");
883 num = VCC_BUFF_LEN - port->chars_in_buffer;
885 vcc_put(port, false);
890 static unsigned int vcc_chars_in_buffer(struct tty_struct *tty)
892 struct vcc_port *port;
895 port = vcc_get_ne(tty->index);
896 if (unlikely(!port)) {
897 pr_err("VCC: chars_in_buffer: Failed to find VCC port\n");
901 num = port->chars_in_buffer;
903 vcc_put(port, false);
908 static int vcc_break_ctl(struct tty_struct *tty, int state)
910 struct vcc_port *port;
913 port = vcc_get_ne(tty->index);
914 if (unlikely(!port)) {
915 pr_err("VCC: break_ctl: Failed to find VCC port\n");
921 vcc_put(port, false);
925 spin_lock_irqsave(&port->lock, flags);
927 if (vcc_send_ctl(port, VCC_CTL_BREAK) < 0)
930 spin_unlock_irqrestore(&port->lock, flags);
932 vcc_put(port, false);
937 static int vcc_install(struct tty_driver *driver, struct tty_struct *tty)
939 struct vcc_port *port_vcc;
940 struct tty_port *port_tty;
943 if (tty->index >= VCC_MAX_PORTS)
946 ret = tty_standard_install(driver, tty);
950 port_tty = kzalloc(sizeof(struct tty_port), GFP_KERNEL);
954 port_vcc = vcc_get(tty->index, true);
956 pr_err("VCC: install: Failed to find VCC port\n");
962 tty_port_init(port_tty);
963 port_tty->ops = &vcc_port_ops;
964 tty->port = port_tty;
968 vcc_put(port_vcc, true);
973 static void vcc_cleanup(struct tty_struct *tty)
975 struct vcc_port *port;
977 port = vcc_get(tty->index, true);
982 vcc_table_remove(tty->index);
983 kfree(port->vio.name);
991 tty_port_destroy(tty->port);
996 static const struct tty_operations vcc_ops = {
999 .hangup = vcc_hangup,
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,
1008 #define VCC_TTY_FLAGS (TTY_DRIVER_DYNAMIC_DEV | TTY_DRIVER_REAL_RAW)
1010 static int vcc_tty_init(void)
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);
1020 vcc_tty_driver->driver_name = "vcc";
1021 vcc_tty_driver->name = "vcc";
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;
1027 tty_set_operations(vcc_tty_driver, &vcc_ops);
1029 rv = tty_register_driver(vcc_tty_driver);
1031 pr_err("VCC: TTY driver registration failed\n");
1032 tty_driver_kref_put(vcc_tty_driver);
1033 vcc_tty_driver = NULL;
1037 vccdbg("VCC: TTY driver registered\n");
1042 static void vcc_tty_exit(void)
1044 tty_unregister_driver(vcc_tty_driver);
1045 tty_driver_kref_put(vcc_tty_driver);
1046 vccdbg("VCC: TTY driver unregistered\n");
1048 vcc_tty_driver = NULL;
1051 static int __init vcc_init(void)
1055 rv = vcc_tty_init();
1057 pr_err("VCC: TTY init failed\n");
1061 rv = vio_register_driver(&vcc_driver);
1063 pr_err("VCC: VIO driver registration failed\n");
1066 vccdbg("VCC: VIO driver registered successfully\n");
1072 static void __exit vcc_exit(void)
1074 vio_unregister_driver(&vcc_driver);
1075 vccdbg("VCC: VIO driver unregistered\n");
1077 vccdbg("VCC: TTY driver unregistered\n");
1080 module_init(vcc_init);
1081 module_exit(vcc_exit);