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