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