1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53#include <linux/device.h>
54#include <linux/init.h>
55#include <linux/interrupt.h>
56#include <linux/kernel.h>
57#include <linux/kref.h>
58#include <linux/kthread.h>
59#include <linux/list.h>
60#include <linux/major.h>
61#include <linux/module.h>
62#include <linux/moduleparam.h>
63#include <linux/sched.h>
64#include <linux/slab.h>
65#include <linux/spinlock.h>
66#include <linux/stat.h>
67#include <linux/tty.h>
68#include <linux/tty_flip.h>
69#include <asm/hvconsole.h>
70#include <asm/hvcserver.h>
71#include <linux/uaccess.h>
72#include <asm/vio.h>
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115#define HVCS_DRIVER_VERSION "1.3.3"
116
117MODULE_AUTHOR("Ryan S. Arnold <rsa@us.ibm.com>");
118MODULE_DESCRIPTION("IBM hvcs (Hypervisor Virtual Console Server) Driver");
119MODULE_LICENSE("GPL");
120MODULE_VERSION(HVCS_DRIVER_VERSION);
121
122
123
124
125
126#define HVCS_CLOSE_WAIT (HZ/100)
127
128
129
130
131
132
133
134
135
136#define HVCS_DEFAULT_SERVER_ADAPTERS 64
137
138
139
140
141
142
143#define HVCS_MAX_SERVER_ADAPTERS 1024
144
145
146
147
148
149
150
151#define HVCS_MINOR_START 0
152
153
154
155
156
157
158
159
160#define __ALIGNED__ __attribute__((__aligned__(8)))
161
162
163
164
165
166#define HVCS_BUFF_LEN 16
167
168
169
170
171
172#define HVCS_MAX_FROM_USER 4096
173
174
175
176
177
178
179static const struct ktermios hvcs_tty_termios = {
180 .c_iflag = IGNBRK | IGNPAR,
181 .c_oflag = OPOST,
182 .c_cflag = B38400 | CS8 | CREAD | HUPCL,
183 .c_cc = INIT_C_CC,
184 .c_ispeed = 38400,
185 .c_ospeed = 38400
186};
187
188
189
190
191
192
193
194static int hvcs_parm_num_devs = -1;
195module_param(hvcs_parm_num_devs, int, 0);
196
197static const char hvcs_driver_name[] = "hvcs";
198static const char hvcs_device_node[] = "hvcs";
199static const char hvcs_driver_string[]
200 = "IBM hvcs (Hypervisor Virtual Console Server) Driver";
201
202
203static int hvcs_rescan_status;
204
205static struct tty_driver *hvcs_tty_driver;
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220static int *hvcs_index_list;
221
222
223
224
225
226static int hvcs_index_count;
227
228
229
230
231
232static int hvcs_kicked;
233
234
235
236
237
238static struct task_struct *hvcs_task;
239
240
241
242
243
244static unsigned long *hvcs_pi_buff;
245
246
247static DEFINE_SPINLOCK(hvcs_pi_lock);
248
249
250struct hvcs_struct {
251 struct tty_port port;
252 spinlock_t lock;
253
254
255
256
257
258 unsigned int index;
259
260
261
262
263
264 int todo_mask;
265
266
267
268
269
270
271
272
273
274 char buffer[HVCS_BUFF_LEN];
275 int chars_in_buffer;
276
277
278
279
280
281
282
283 int connected;
284 uint32_t p_unit_address;
285 uint32_t p_partition_ID;
286 char p_location_code[HVCS_CLC_LENGTH + 1];
287 struct list_head next;
288 struct vio_dev *vdev;
289};
290
291static LIST_HEAD(hvcs_structs);
292static DEFINE_SPINLOCK(hvcs_structs_lock);
293static DEFINE_MUTEX(hvcs_init_mutex);
294
295static void hvcs_unthrottle(struct tty_struct *tty);
296static void hvcs_throttle(struct tty_struct *tty);
297static irqreturn_t hvcs_handle_interrupt(int irq, void *dev_instance);
298
299static int hvcs_write(struct tty_struct *tty,
300 const unsigned char *buf, int count);
301static int hvcs_write_room(struct tty_struct *tty);
302static int hvcs_chars_in_buffer(struct tty_struct *tty);
303
304static int hvcs_has_pi(struct hvcs_struct *hvcsd);
305static void hvcs_set_pi(struct hvcs_partner_info *pi,
306 struct hvcs_struct *hvcsd);
307static int hvcs_get_pi(struct hvcs_struct *hvcsd);
308static int hvcs_rescan_devices_list(void);
309
310static int hvcs_partner_connect(struct hvcs_struct *hvcsd);
311static void hvcs_partner_free(struct hvcs_struct *hvcsd);
312
313static int hvcs_enable_device(struct hvcs_struct *hvcsd,
314 uint32_t unit_address, unsigned int irq, struct vio_dev *dev);
315
316static int hvcs_open(struct tty_struct *tty, struct file *filp);
317static void hvcs_close(struct tty_struct *tty, struct file *filp);
318static void hvcs_hangup(struct tty_struct * tty);
319
320static int hvcs_probe(struct vio_dev *dev,
321 const struct vio_device_id *id);
322static int hvcs_remove(struct vio_dev *dev);
323static int __init hvcs_module_init(void);
324static void __exit hvcs_module_exit(void);
325static int hvcs_initialize(void);
326
327#define HVCS_SCHED_READ 0x00000001
328#define HVCS_QUICK_READ 0x00000002
329#define HVCS_TRY_WRITE 0x00000004
330#define HVCS_READ_MASK (HVCS_SCHED_READ | HVCS_QUICK_READ)
331
332static inline struct hvcs_struct *from_vio_dev(struct vio_dev *viod)
333{
334 return dev_get_drvdata(&viod->dev);
335}
336
337
338static ssize_t hvcs_partner_vtys_show(struct device *dev, struct device_attribute *attr, char *buf)
339{
340 struct vio_dev *viod = to_vio_dev(dev);
341 struct hvcs_struct *hvcsd = from_vio_dev(viod);
342 unsigned long flags;
343 int retval;
344
345 spin_lock_irqsave(&hvcsd->lock, flags);
346 retval = sprintf(buf, "%X\n", hvcsd->p_unit_address);
347 spin_unlock_irqrestore(&hvcsd->lock, flags);
348 return retval;
349}
350static DEVICE_ATTR(partner_vtys, S_IRUGO, hvcs_partner_vtys_show, NULL);
351
352static ssize_t hvcs_partner_clcs_show(struct device *dev, struct device_attribute *attr, char *buf)
353{
354 struct vio_dev *viod = to_vio_dev(dev);
355 struct hvcs_struct *hvcsd = from_vio_dev(viod);
356 unsigned long flags;
357 int retval;
358
359 spin_lock_irqsave(&hvcsd->lock, flags);
360 retval = sprintf(buf, "%s\n", &hvcsd->p_location_code[0]);
361 spin_unlock_irqrestore(&hvcsd->lock, flags);
362 return retval;
363}
364static DEVICE_ATTR(partner_clcs, S_IRUGO, hvcs_partner_clcs_show, NULL);
365
366static ssize_t hvcs_current_vty_store(struct device *dev, struct device_attribute *attr, const char * buf,
367 size_t count)
368{
369
370
371
372
373 printk(KERN_INFO "HVCS: Denied current_vty change: -EPERM.\n");
374 return -EPERM;
375}
376
377static ssize_t hvcs_current_vty_show(struct device *dev, struct device_attribute *attr, char *buf)
378{
379 struct vio_dev *viod = to_vio_dev(dev);
380 struct hvcs_struct *hvcsd = from_vio_dev(viod);
381 unsigned long flags;
382 int retval;
383
384 spin_lock_irqsave(&hvcsd->lock, flags);
385 retval = sprintf(buf, "%s\n", &hvcsd->p_location_code[0]);
386 spin_unlock_irqrestore(&hvcsd->lock, flags);
387 return retval;
388}
389
390static DEVICE_ATTR(current_vty,
391 S_IRUGO | S_IWUSR, hvcs_current_vty_show, hvcs_current_vty_store);
392
393static ssize_t hvcs_vterm_state_store(struct device *dev, struct device_attribute *attr, const char *buf,
394 size_t count)
395{
396 struct vio_dev *viod = to_vio_dev(dev);
397 struct hvcs_struct *hvcsd = from_vio_dev(viod);
398 unsigned long flags;
399
400
401 if (simple_strtol(buf, NULL, 0) != 0)
402 return -EINVAL;
403
404 spin_lock_irqsave(&hvcsd->lock, flags);
405
406 if (hvcsd->port.count > 0) {
407 spin_unlock_irqrestore(&hvcsd->lock, flags);
408 printk(KERN_INFO "HVCS: vterm state unchanged. "
409 "The hvcs device node is still in use.\n");
410 return -EPERM;
411 }
412
413 if (hvcsd->connected == 0) {
414 spin_unlock_irqrestore(&hvcsd->lock, flags);
415 printk(KERN_INFO "HVCS: vterm state unchanged. The"
416 " vty-server is not connected to a vty.\n");
417 return -EPERM;
418 }
419
420 hvcs_partner_free(hvcsd);
421 printk(KERN_INFO "HVCS: Closed vty-server@%X and"
422 " partner vty@%X:%d connection.\n",
423 hvcsd->vdev->unit_address,
424 hvcsd->p_unit_address,
425 (uint32_t)hvcsd->p_partition_ID);
426
427 spin_unlock_irqrestore(&hvcsd->lock, flags);
428 return count;
429}
430
431static ssize_t hvcs_vterm_state_show(struct device *dev, struct device_attribute *attr, char *buf)
432{
433 struct vio_dev *viod = to_vio_dev(dev);
434 struct hvcs_struct *hvcsd = from_vio_dev(viod);
435 unsigned long flags;
436 int retval;
437
438 spin_lock_irqsave(&hvcsd->lock, flags);
439 retval = sprintf(buf, "%d\n", hvcsd->connected);
440 spin_unlock_irqrestore(&hvcsd->lock, flags);
441 return retval;
442}
443static DEVICE_ATTR(vterm_state, S_IRUGO | S_IWUSR,
444 hvcs_vterm_state_show, hvcs_vterm_state_store);
445
446static ssize_t hvcs_index_show(struct device *dev, struct device_attribute *attr, char *buf)
447{
448 struct vio_dev *viod = to_vio_dev(dev);
449 struct hvcs_struct *hvcsd = from_vio_dev(viod);
450 unsigned long flags;
451 int retval;
452
453 spin_lock_irqsave(&hvcsd->lock, flags);
454 retval = sprintf(buf, "%d\n", hvcsd->index);
455 spin_unlock_irqrestore(&hvcsd->lock, flags);
456 return retval;
457}
458
459static DEVICE_ATTR(index, S_IRUGO, hvcs_index_show, NULL);
460
461static struct attribute *hvcs_attrs[] = {
462 &dev_attr_partner_vtys.attr,
463 &dev_attr_partner_clcs.attr,
464 &dev_attr_current_vty.attr,
465 &dev_attr_vterm_state.attr,
466 &dev_attr_index.attr,
467 NULL,
468};
469
470static struct attribute_group hvcs_attr_group = {
471 .attrs = hvcs_attrs,
472};
473
474static ssize_t rescan_show(struct device_driver *ddp, char *buf)
475{
476
477 return snprintf(buf, PAGE_SIZE, "%d\n", hvcs_rescan_status);
478}
479
480static ssize_t rescan_store(struct device_driver *ddp, const char * buf,
481 size_t count)
482{
483 if ((simple_strtol(buf, NULL, 0) != 1)
484 && (hvcs_rescan_status != 0))
485 return -EINVAL;
486
487 hvcs_rescan_status = 1;
488 printk(KERN_INFO "HVCS: rescanning partner info for all"
489 " vty-servers.\n");
490 hvcs_rescan_devices_list();
491 hvcs_rescan_status = 0;
492 return count;
493}
494
495static DRIVER_ATTR_RW(rescan);
496
497static void hvcs_kick(void)
498{
499 hvcs_kicked = 1;
500 wmb();
501 wake_up_process(hvcs_task);
502}
503
504static void hvcs_unthrottle(struct tty_struct *tty)
505{
506 struct hvcs_struct *hvcsd = tty->driver_data;
507 unsigned long flags;
508
509 spin_lock_irqsave(&hvcsd->lock, flags);
510 hvcsd->todo_mask |= HVCS_SCHED_READ;
511 spin_unlock_irqrestore(&hvcsd->lock, flags);
512 hvcs_kick();
513}
514
515static void hvcs_throttle(struct tty_struct *tty)
516{
517 struct hvcs_struct *hvcsd = tty->driver_data;
518 unsigned long flags;
519
520 spin_lock_irqsave(&hvcsd->lock, flags);
521 vio_disable_interrupts(hvcsd->vdev);
522 spin_unlock_irqrestore(&hvcsd->lock, flags);
523}
524
525
526
527
528
529
530static irqreturn_t hvcs_handle_interrupt(int irq, void *dev_instance)
531{
532 struct hvcs_struct *hvcsd = dev_instance;
533
534 spin_lock(&hvcsd->lock);
535 vio_disable_interrupts(hvcsd->vdev);
536 hvcsd->todo_mask |= HVCS_SCHED_READ;
537 spin_unlock(&hvcsd->lock);
538 hvcs_kick();
539
540 return IRQ_HANDLED;
541}
542
543
544static void hvcs_try_write(struct hvcs_struct *hvcsd)
545{
546 uint32_t unit_address = hvcsd->vdev->unit_address;
547 struct tty_struct *tty = hvcsd->port.tty;
548 int sent;
549
550 if (hvcsd->todo_mask & HVCS_TRY_WRITE) {
551
552 sent = hvc_put_chars(unit_address,
553 &hvcsd->buffer[0],
554 hvcsd->chars_in_buffer );
555 if (sent > 0) {
556 hvcsd->chars_in_buffer = 0;
557
558 hvcsd->todo_mask &= ~(HVCS_TRY_WRITE);
559
560
561
562
563
564
565
566
567 if (tty) {
568 tty_wakeup(tty);
569 }
570 }
571 }
572}
573
574static int hvcs_io(struct hvcs_struct *hvcsd)
575{
576 uint32_t unit_address;
577 struct tty_struct *tty;
578 char buf[HVCS_BUFF_LEN] __ALIGNED__;
579 unsigned long flags;
580 int got = 0;
581
582 spin_lock_irqsave(&hvcsd->lock, flags);
583
584 unit_address = hvcsd->vdev->unit_address;
585 tty = hvcsd->port.tty;
586
587 hvcs_try_write(hvcsd);
588
589 if (!tty || tty_throttled(tty)) {
590 hvcsd->todo_mask &= ~(HVCS_READ_MASK);
591 goto bail;
592 } else if (!(hvcsd->todo_mask & (HVCS_READ_MASK)))
593 goto bail;
594
595
596 hvcsd->todo_mask &= ~(HVCS_READ_MASK);
597
598 if (tty_buffer_request_room(&hvcsd->port, HVCS_BUFF_LEN) >= HVCS_BUFF_LEN) {
599 got = hvc_get_chars(unit_address,
600 &buf[0],
601 HVCS_BUFF_LEN);
602 tty_insert_flip_string(&hvcsd->port, buf, got);
603 }
604
605
606 if (got)
607 hvcsd->todo_mask |= HVCS_QUICK_READ;
608
609 spin_unlock_irqrestore(&hvcsd->lock, flags);
610
611 if(got)
612 tty_flip_buffer_push(&hvcsd->port);
613
614 if (!got) {
615
616 spin_lock_irqsave(&hvcsd->lock, flags);
617 vio_enable_interrupts(hvcsd->vdev);
618 spin_unlock_irqrestore(&hvcsd->lock, flags);
619 }
620
621 return hvcsd->todo_mask;
622
623 bail:
624 spin_unlock_irqrestore(&hvcsd->lock, flags);
625 return hvcsd->todo_mask;
626}
627
628static int khvcsd(void *unused)
629{
630 struct hvcs_struct *hvcsd;
631 int hvcs_todo_mask;
632
633 __set_current_state(TASK_RUNNING);
634
635 do {
636 hvcs_todo_mask = 0;
637 hvcs_kicked = 0;
638 wmb();
639
640 spin_lock(&hvcs_structs_lock);
641 list_for_each_entry(hvcsd, &hvcs_structs, next) {
642 hvcs_todo_mask |= hvcs_io(hvcsd);
643 }
644 spin_unlock(&hvcs_structs_lock);
645
646
647
648
649
650
651 if (hvcs_todo_mask & (HVCS_TRY_WRITE | HVCS_QUICK_READ)) {
652 yield();
653 continue;
654 }
655
656 set_current_state(TASK_INTERRUPTIBLE);
657 if (!hvcs_kicked)
658 schedule();
659 __set_current_state(TASK_RUNNING);
660 } while (!kthread_should_stop());
661
662 return 0;
663}
664
665static const struct vio_device_id hvcs_driver_table[] = {
666 {"serial-server", "hvterm2"},
667 { "", "" }
668};
669MODULE_DEVICE_TABLE(vio, hvcs_driver_table);
670
671static void hvcs_return_index(int index)
672{
673
674 if (!hvcs_index_list)
675 return;
676 if (index < 0 || index >= hvcs_index_count)
677 return;
678 if (hvcs_index_list[index] == -1)
679 return;
680 else
681 hvcs_index_list[index] = -1;
682}
683
684static void hvcs_destruct_port(struct tty_port *p)
685{
686 struct hvcs_struct *hvcsd = container_of(p, struct hvcs_struct, port);
687 struct vio_dev *vdev;
688 unsigned long flags;
689
690 spin_lock(&hvcs_structs_lock);
691 spin_lock_irqsave(&hvcsd->lock, flags);
692
693
694 list_del(&(hvcsd->next));
695
696 if (hvcsd->connected == 1) {
697 hvcs_partner_free(hvcsd);
698 printk(KERN_INFO "HVCS: Closed vty-server@%X and"
699 " partner vty@%X:%d connection.\n",
700 hvcsd->vdev->unit_address,
701 hvcsd->p_unit_address,
702 (uint32_t)hvcsd->p_partition_ID);
703 }
704 printk(KERN_INFO "HVCS: Destroyed hvcs_struct for vty-server@%X.\n",
705 hvcsd->vdev->unit_address);
706
707 vdev = hvcsd->vdev;
708 hvcsd->vdev = NULL;
709
710 hvcsd->p_unit_address = 0;
711 hvcsd->p_partition_ID = 0;
712 hvcs_return_index(hvcsd->index);
713 memset(&hvcsd->p_location_code[0], 0x00, HVCS_CLC_LENGTH + 1);
714
715 spin_unlock_irqrestore(&hvcsd->lock, flags);
716 spin_unlock(&hvcs_structs_lock);
717
718 sysfs_remove_group(&vdev->dev.kobj, &hvcs_attr_group);
719
720 kfree(hvcsd);
721}
722
723static const struct tty_port_operations hvcs_port_ops = {
724 .destruct = hvcs_destruct_port,
725};
726
727static int hvcs_get_index(void)
728{
729 int i;
730
731 if (!hvcs_index_list) {
732 printk(KERN_ERR "HVCS: hvcs_index_list NOT valid!.\n");
733 return -EFAULT;
734 }
735
736 for(i = 0; i < hvcs_index_count; i++) {
737 if (hvcs_index_list[i] == -1) {
738 hvcs_index_list[i] = 0;
739 return i;
740 }
741 }
742 return -1;
743}
744
745static int hvcs_probe(
746 struct vio_dev *dev,
747 const struct vio_device_id *id)
748{
749 struct hvcs_struct *hvcsd;
750 int index, rc;
751 int retval;
752
753 if (!dev || !id) {
754 printk(KERN_ERR "HVCS: probed with invalid parameter.\n");
755 return -EPERM;
756 }
757
758
759 rc = hvcs_initialize();
760 if (rc) {
761 pr_err("HVCS: Failed to initialize core driver.\n");
762 return rc;
763 }
764
765
766 index = hvcs_get_index();
767 if (index < 0) {
768 return -EFAULT;
769 }
770
771 hvcsd = kzalloc(sizeof(*hvcsd), GFP_KERNEL);
772 if (!hvcsd)
773 return -ENODEV;
774
775 tty_port_init(&hvcsd->port);
776 hvcsd->port.ops = &hvcs_port_ops;
777 spin_lock_init(&hvcsd->lock);
778
779 hvcsd->vdev = dev;
780 dev_set_drvdata(&dev->dev, hvcsd);
781
782 hvcsd->index = index;
783
784
785 hvcsd->chars_in_buffer = 0;
786 hvcsd->todo_mask = 0;
787 hvcsd->connected = 0;
788
789
790
791
792
793 if (hvcs_get_pi(hvcsd)) {
794 printk(KERN_ERR "HVCS: Failed to fetch partner"
795 " info for vty-server@%X on device probe.\n",
796 hvcsd->vdev->unit_address);
797 }
798
799
800
801
802
803
804 spin_lock(&hvcs_structs_lock);
805 list_add_tail(&(hvcsd->next), &hvcs_structs);
806 spin_unlock(&hvcs_structs_lock);
807
808 retval = sysfs_create_group(&dev->dev.kobj, &hvcs_attr_group);
809 if (retval) {
810 printk(KERN_ERR "HVCS: Can't create sysfs attrs for vty-server@%X\n",
811 hvcsd->vdev->unit_address);
812 return retval;
813 }
814
815 printk(KERN_INFO "HVCS: vty-server@%X added to the vio bus.\n", dev->unit_address);
816
817
818
819
820
821 return 0;
822}
823
824static int hvcs_remove(struct vio_dev *dev)
825{
826 struct hvcs_struct *hvcsd = dev_get_drvdata(&dev->dev);
827 unsigned long flags;
828 struct tty_struct *tty;
829
830 if (!hvcsd)
831 return -ENODEV;
832
833
834
835 spin_lock_irqsave(&hvcsd->lock, flags);
836
837 tty = hvcsd->port.tty;
838
839 spin_unlock_irqrestore(&hvcsd->lock, flags);
840
841
842
843
844
845 tty_port_put(&hvcsd->port);
846
847
848
849
850
851
852 if (tty)
853 tty_hangup(tty);
854
855 printk(KERN_INFO "HVCS: vty-server@%X removed from the"
856 " vio bus.\n", dev->unit_address);
857 return 0;
858};
859
860static struct vio_driver hvcs_vio_driver = {
861 .id_table = hvcs_driver_table,
862 .probe = hvcs_probe,
863 .remove = hvcs_remove,
864 .name = hvcs_driver_name,
865};
866
867
868static void hvcs_set_pi(struct hvcs_partner_info *pi, struct hvcs_struct *hvcsd)
869{
870 hvcsd->p_unit_address = pi->unit_address;
871 hvcsd->p_partition_ID = pi->partition_ID;
872
873
874 strlcpy(&hvcsd->p_location_code[0],
875 &pi->location_code[0], sizeof(hvcsd->p_location_code));
876}
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891static int hvcs_get_pi(struct hvcs_struct *hvcsd)
892{
893 struct hvcs_partner_info *pi;
894 uint32_t unit_address = hvcsd->vdev->unit_address;
895 struct list_head head;
896 int retval;
897
898 spin_lock(&hvcs_pi_lock);
899 if (!hvcs_pi_buff) {
900 spin_unlock(&hvcs_pi_lock);
901 return -EFAULT;
902 }
903 retval = hvcs_get_partner_info(unit_address, &head, hvcs_pi_buff);
904 spin_unlock(&hvcs_pi_lock);
905 if (retval) {
906 printk(KERN_ERR "HVCS: Failed to fetch partner"
907 " info for vty-server@%x.\n", unit_address);
908 return retval;
909 }
910
911
912 hvcsd->p_unit_address = 0;
913 hvcsd->p_partition_ID = 0;
914
915 list_for_each_entry(pi, &head, node)
916 hvcs_set_pi(pi, hvcsd);
917
918 hvcs_free_partner_info(&head);
919 return 0;
920}
921
922
923
924
925
926static int hvcs_rescan_devices_list(void)
927{
928 struct hvcs_struct *hvcsd;
929 unsigned long flags;
930
931 spin_lock(&hvcs_structs_lock);
932
933 list_for_each_entry(hvcsd, &hvcs_structs, next) {
934 spin_lock_irqsave(&hvcsd->lock, flags);
935 hvcs_get_pi(hvcsd);
936 spin_unlock_irqrestore(&hvcsd->lock, flags);
937 }
938
939 spin_unlock(&hvcs_structs_lock);
940
941 return 0;
942}
943
944
945
946
947
948
949static int hvcs_has_pi(struct hvcs_struct *hvcsd)
950{
951 if ((!hvcsd->p_unit_address) || (!hvcsd->p_partition_ID))
952 return 0;
953 return 1;
954}
955
956
957
958
959
960
961
962static int hvcs_partner_connect(struct hvcs_struct *hvcsd)
963{
964 int retval;
965 unsigned int unit_address = hvcsd->vdev->unit_address;
966
967
968
969
970
971
972
973
974 retval = hvcs_register_connection(unit_address,
975 hvcsd->p_partition_ID,
976 hvcsd->p_unit_address);
977 if (!retval) {
978 hvcsd->connected = 1;
979 return 0;
980 } else if (retval != -EINVAL)
981 return retval;
982
983
984
985
986
987 if (hvcs_get_pi(hvcsd))
988 return -ENOMEM;
989
990 if (!hvcs_has_pi(hvcsd))
991 return -ENODEV;
992
993 retval = hvcs_register_connection(unit_address,
994 hvcsd->p_partition_ID,
995 hvcsd->p_unit_address);
996 if (retval != -EINVAL) {
997 hvcsd->connected = 1;
998 return retval;
999 }
1000
1001
1002
1003
1004
1005
1006
1007 printk(KERN_INFO "HVCS: vty-server or partner"
1008 " vty is busy. Try again later.\n");
1009 return -EBUSY;
1010}
1011
1012
1013static void hvcs_partner_free(struct hvcs_struct *hvcsd)
1014{
1015 int retval;
1016 do {
1017 retval = hvcs_free_connection(hvcsd->vdev->unit_address);
1018 } while (retval == -EBUSY);
1019 hvcsd->connected = 0;
1020}
1021
1022
1023static int hvcs_enable_device(struct hvcs_struct *hvcsd, uint32_t unit_address,
1024 unsigned int irq, struct vio_dev *vdev)
1025{
1026 unsigned long flags;
1027 int rc;
1028
1029
1030
1031
1032
1033 rc = request_irq(irq, &hvcs_handle_interrupt, 0, "ibmhvcs", hvcsd);
1034 if (!rc) {
1035
1036
1037
1038
1039 if (vio_enable_interrupts(vdev) == H_SUCCESS)
1040 return 0;
1041 else {
1042 printk(KERN_ERR "HVCS: int enable failed for"
1043 " vty-server@%X.\n", unit_address);
1044 free_irq(irq, hvcsd);
1045 }
1046 } else
1047 printk(KERN_ERR "HVCS: irq req failed for"
1048 " vty-server@%X.\n", unit_address);
1049
1050 spin_lock_irqsave(&hvcsd->lock, flags);
1051 hvcs_partner_free(hvcsd);
1052 spin_unlock_irqrestore(&hvcsd->lock, flags);
1053
1054 return rc;
1055
1056}
1057
1058
1059
1060
1061
1062
1063
1064
1065static struct hvcs_struct *hvcs_get_by_index(int index)
1066{
1067 struct hvcs_struct *hvcsd;
1068 unsigned long flags;
1069
1070 spin_lock(&hvcs_structs_lock);
1071 list_for_each_entry(hvcsd, &hvcs_structs, next) {
1072 spin_lock_irqsave(&hvcsd->lock, flags);
1073 if (hvcsd->index == index) {
1074 tty_port_get(&hvcsd->port);
1075 spin_unlock_irqrestore(&hvcsd->lock, flags);
1076 spin_unlock(&hvcs_structs_lock);
1077 return hvcsd;
1078 }
1079 spin_unlock_irqrestore(&hvcsd->lock, flags);
1080 }
1081 spin_unlock(&hvcs_structs_lock);
1082
1083 return NULL;
1084}
1085
1086static int hvcs_install(struct tty_driver *driver, struct tty_struct *tty)
1087{
1088 struct hvcs_struct *hvcsd;
1089 struct vio_dev *vdev;
1090 unsigned long unit_address, flags;
1091 unsigned int irq;
1092 int retval;
1093
1094
1095
1096
1097
1098 hvcsd = hvcs_get_by_index(tty->index);
1099 if (!hvcsd) {
1100 printk(KERN_WARNING "HVCS: open failed, no device associated"
1101 " with tty->index %d.\n", tty->index);
1102 return -ENODEV;
1103 }
1104
1105 spin_lock_irqsave(&hvcsd->lock, flags);
1106
1107 if (hvcsd->connected == 0) {
1108 retval = hvcs_partner_connect(hvcsd);
1109 if (retval) {
1110 spin_unlock_irqrestore(&hvcsd->lock, flags);
1111 printk(KERN_WARNING "HVCS: partner connect failed.\n");
1112 goto err_put;
1113 }
1114 }
1115
1116 hvcsd->port.count = 0;
1117 hvcsd->port.tty = tty;
1118 tty->driver_data = hvcsd;
1119
1120 memset(&hvcsd->buffer[0], 0x00, HVCS_BUFF_LEN);
1121
1122
1123
1124
1125
1126 irq = hvcsd->vdev->irq;
1127 vdev = hvcsd->vdev;
1128 unit_address = hvcsd->vdev->unit_address;
1129
1130 hvcsd->todo_mask |= HVCS_SCHED_READ;
1131 spin_unlock_irqrestore(&hvcsd->lock, flags);
1132
1133
1134
1135
1136
1137 retval = hvcs_enable_device(hvcsd, unit_address, irq, vdev);
1138 if (retval) {
1139 printk(KERN_WARNING "HVCS: enable device failed.\n");
1140 goto err_put;
1141 }
1142
1143 retval = tty_port_install(&hvcsd->port, driver, tty);
1144 if (retval)
1145 goto err_irq;
1146
1147 return 0;
1148err_irq:
1149 spin_lock_irqsave(&hvcsd->lock, flags);
1150 vio_disable_interrupts(hvcsd->vdev);
1151 spin_unlock_irqrestore(&hvcsd->lock, flags);
1152 free_irq(irq, hvcsd);
1153err_put:
1154 tty_port_put(&hvcsd->port);
1155
1156 return retval;
1157}
1158
1159
1160
1161
1162
1163static int hvcs_open(struct tty_struct *tty, struct file *filp)
1164{
1165 struct hvcs_struct *hvcsd = tty->driver_data;
1166 unsigned long flags;
1167
1168 spin_lock_irqsave(&hvcsd->lock, flags);
1169 hvcsd->port.count++;
1170 hvcsd->todo_mask |= HVCS_SCHED_READ;
1171 spin_unlock_irqrestore(&hvcsd->lock, flags);
1172
1173 hvcs_kick();
1174
1175 printk(KERN_INFO "HVCS: vty-server@%X connection opened.\n",
1176 hvcsd->vdev->unit_address );
1177
1178 return 0;
1179}
1180
1181static void hvcs_close(struct tty_struct *tty, struct file *filp)
1182{
1183 struct hvcs_struct *hvcsd;
1184 unsigned long flags;
1185 int irq;
1186
1187
1188
1189
1190
1191 if (tty_hung_up_p(filp))
1192 return;
1193
1194
1195
1196
1197
1198
1199 if (!tty->driver_data)
1200 return;
1201
1202 hvcsd = tty->driver_data;
1203
1204 spin_lock_irqsave(&hvcsd->lock, flags);
1205 if (--hvcsd->port.count == 0) {
1206
1207 vio_disable_interrupts(hvcsd->vdev);
1208
1209
1210
1211
1212
1213
1214 hvcsd->port.tty = NULL;
1215
1216 irq = hvcsd->vdev->irq;
1217 spin_unlock_irqrestore(&hvcsd->lock, flags);
1218
1219 tty_wait_until_sent(tty, HVCS_CLOSE_WAIT);
1220
1221
1222
1223
1224
1225
1226 tty->driver_data = NULL;
1227
1228 free_irq(irq, hvcsd);
1229 return;
1230 } else if (hvcsd->port.count < 0) {
1231 printk(KERN_ERR "HVCS: vty-server@%X open_count: %d is mismanaged.\n",
1232 hvcsd->vdev->unit_address, hvcsd->port.count);
1233 }
1234
1235 spin_unlock_irqrestore(&hvcsd->lock, flags);
1236}
1237
1238static void hvcs_cleanup(struct tty_struct * tty)
1239{
1240 struct hvcs_struct *hvcsd = tty->driver_data;
1241
1242 tty_port_put(&hvcsd->port);
1243}
1244
1245static void hvcs_hangup(struct tty_struct * tty)
1246{
1247 struct hvcs_struct *hvcsd = tty->driver_data;
1248 unsigned long flags;
1249 int temp_open_count;
1250 int irq;
1251
1252 spin_lock_irqsave(&hvcsd->lock, flags);
1253
1254 temp_open_count = hvcsd->port.count;
1255
1256
1257
1258
1259
1260
1261 vio_disable_interrupts(hvcsd->vdev);
1262
1263 hvcsd->todo_mask = 0;
1264
1265
1266 tty->driver_data = NULL;
1267 hvcsd->port.tty = NULL;
1268
1269 hvcsd->port.count = 0;
1270
1271
1272
1273 memset(&hvcsd->buffer[0], 0x00, HVCS_BUFF_LEN);
1274 hvcsd->chars_in_buffer = 0;
1275
1276 irq = hvcsd->vdev->irq;
1277
1278 spin_unlock_irqrestore(&hvcsd->lock, flags);
1279
1280 free_irq(irq, hvcsd);
1281
1282
1283
1284
1285
1286
1287 while(temp_open_count) {
1288 --temp_open_count;
1289
1290
1291
1292
1293
1294 tty_port_put(&hvcsd->port);
1295 }
1296}
1297
1298
1299
1300
1301
1302
1303
1304
1305static int hvcs_write(struct tty_struct *tty,
1306 const unsigned char *buf, int count)
1307{
1308 struct hvcs_struct *hvcsd = tty->driver_data;
1309 unsigned int unit_address;
1310 const unsigned char *charbuf;
1311 unsigned long flags;
1312 int total_sent = 0;
1313 int tosend = 0;
1314 int result = 0;
1315
1316
1317
1318
1319
1320 if (!hvcsd)
1321 return -ENODEV;
1322
1323
1324 if (count > HVCS_MAX_FROM_USER) {
1325 printk(KERN_WARNING "HVCS write: count being truncated to"
1326 " HVCS_MAX_FROM_USER.\n");
1327 count = HVCS_MAX_FROM_USER;
1328 }
1329
1330 charbuf = buf;
1331
1332 spin_lock_irqsave(&hvcsd->lock, flags);
1333
1334
1335
1336
1337
1338
1339
1340 if (hvcsd->port.count <= 0) {
1341 spin_unlock_irqrestore(&hvcsd->lock, flags);
1342 return -ENODEV;
1343 }
1344
1345 unit_address = hvcsd->vdev->unit_address;
1346
1347 while (count > 0) {
1348 tosend = min(count, (HVCS_BUFF_LEN - hvcsd->chars_in_buffer));
1349
1350
1351
1352
1353 if (!tosend)
1354 break;
1355
1356 memcpy(&hvcsd->buffer[hvcsd->chars_in_buffer],
1357 &charbuf[total_sent],
1358 tosend);
1359
1360 hvcsd->chars_in_buffer += tosend;
1361
1362 result = 0;
1363
1364
1365
1366
1367
1368
1369 if (!(hvcsd->todo_mask & HVCS_TRY_WRITE))
1370
1371 result = hvc_put_chars(unit_address,
1372 &hvcsd->buffer[0],
1373 hvcsd->chars_in_buffer);
1374
1375
1376
1377
1378
1379
1380 total_sent+=tosend;
1381 count-=tosend;
1382 if (result == 0) {
1383 hvcsd->todo_mask |= HVCS_TRY_WRITE;
1384 hvcs_kick();
1385 break;
1386 }
1387
1388 hvcsd->chars_in_buffer = 0;
1389
1390
1391
1392
1393 if (result < 0)
1394 break;
1395 }
1396
1397 spin_unlock_irqrestore(&hvcsd->lock, flags);
1398
1399 if (result == -1)
1400 return -EIO;
1401 else
1402 return total_sent;
1403}
1404
1405
1406
1407
1408
1409
1410static int hvcs_write_room(struct tty_struct *tty)
1411{
1412 struct hvcs_struct *hvcsd = tty->driver_data;
1413
1414 if (!hvcsd || hvcsd->port.count <= 0)
1415 return 0;
1416
1417 return HVCS_BUFF_LEN - hvcsd->chars_in_buffer;
1418}
1419
1420static int hvcs_chars_in_buffer(struct tty_struct *tty)
1421{
1422 struct hvcs_struct *hvcsd = tty->driver_data;
1423
1424 return hvcsd->chars_in_buffer;
1425}
1426
1427static const struct tty_operations hvcs_ops = {
1428 .install = hvcs_install,
1429 .open = hvcs_open,
1430 .close = hvcs_close,
1431 .cleanup = hvcs_cleanup,
1432 .hangup = hvcs_hangup,
1433 .write = hvcs_write,
1434 .write_room = hvcs_write_room,
1435 .chars_in_buffer = hvcs_chars_in_buffer,
1436 .unthrottle = hvcs_unthrottle,
1437 .throttle = hvcs_throttle,
1438};
1439
1440static int hvcs_alloc_index_list(int n)
1441{
1442 int i;
1443
1444 hvcs_index_list = kmalloc_array(n, sizeof(hvcs_index_count),
1445 GFP_KERNEL);
1446 if (!hvcs_index_list)
1447 return -ENOMEM;
1448 hvcs_index_count = n;
1449 for (i = 0; i < hvcs_index_count; i++)
1450 hvcs_index_list[i] = -1;
1451 return 0;
1452}
1453
1454static void hvcs_free_index_list(void)
1455{
1456
1457 kfree(hvcs_index_list);
1458 hvcs_index_list = NULL;
1459 hvcs_index_count = 0;
1460}
1461
1462static int hvcs_initialize(void)
1463{
1464 int rc, num_ttys_to_alloc;
1465
1466 mutex_lock(&hvcs_init_mutex);
1467 if (hvcs_task) {
1468 mutex_unlock(&hvcs_init_mutex);
1469 return 0;
1470 }
1471
1472
1473 if (hvcs_parm_num_devs <= 0 ||
1474 (hvcs_parm_num_devs > HVCS_MAX_SERVER_ADAPTERS)) {
1475 num_ttys_to_alloc = HVCS_DEFAULT_SERVER_ADAPTERS;
1476 } else
1477 num_ttys_to_alloc = hvcs_parm_num_devs;
1478
1479 hvcs_tty_driver = alloc_tty_driver(num_ttys_to_alloc);
1480 if (!hvcs_tty_driver) {
1481 mutex_unlock(&hvcs_init_mutex);
1482 return -ENOMEM;
1483 }
1484
1485 if (hvcs_alloc_index_list(num_ttys_to_alloc)) {
1486 rc = -ENOMEM;
1487 goto index_fail;
1488 }
1489
1490 hvcs_tty_driver->driver_name = hvcs_driver_name;
1491 hvcs_tty_driver->name = hvcs_device_node;
1492
1493
1494
1495
1496
1497
1498 hvcs_tty_driver->minor_start = HVCS_MINOR_START;
1499 hvcs_tty_driver->type = TTY_DRIVER_TYPE_SYSTEM;
1500
1501
1502
1503
1504
1505
1506 hvcs_tty_driver->init_termios = hvcs_tty_termios;
1507 hvcs_tty_driver->flags = TTY_DRIVER_REAL_RAW;
1508
1509 tty_set_operations(hvcs_tty_driver, &hvcs_ops);
1510
1511
1512
1513
1514
1515 if (tty_register_driver(hvcs_tty_driver)) {
1516 printk(KERN_ERR "HVCS: registration as a tty driver failed.\n");
1517 rc = -EIO;
1518 goto register_fail;
1519 }
1520
1521 hvcs_pi_buff = (unsigned long *) __get_free_page(GFP_KERNEL);
1522 if (!hvcs_pi_buff) {
1523 rc = -ENOMEM;
1524 goto buff_alloc_fail;
1525 }
1526
1527 hvcs_task = kthread_run(khvcsd, NULL, "khvcsd");
1528 if (IS_ERR(hvcs_task)) {
1529 printk(KERN_ERR "HVCS: khvcsd creation failed.\n");
1530 rc = -EIO;
1531 goto kthread_fail;
1532 }
1533 mutex_unlock(&hvcs_init_mutex);
1534 return 0;
1535
1536kthread_fail:
1537 free_page((unsigned long)hvcs_pi_buff);
1538buff_alloc_fail:
1539 tty_unregister_driver(hvcs_tty_driver);
1540register_fail:
1541 hvcs_free_index_list();
1542index_fail:
1543 put_tty_driver(hvcs_tty_driver);
1544 hvcs_tty_driver = NULL;
1545 mutex_unlock(&hvcs_init_mutex);
1546 return rc;
1547}
1548
1549static int __init hvcs_module_init(void)
1550{
1551 int rc = vio_register_driver(&hvcs_vio_driver);
1552 if (rc) {
1553 printk(KERN_ERR "HVCS: can't register vio driver\n");
1554 return rc;
1555 }
1556
1557 pr_info("HVCS: Driver registered.\n");
1558
1559
1560
1561
1562 rc = driver_create_file(&(hvcs_vio_driver.driver), &driver_attr_rescan);
1563 if (rc)
1564 pr_warn("HVCS: Failed to create rescan file (err %d)\n", rc);
1565
1566 return 0;
1567}
1568
1569static void __exit hvcs_module_exit(void)
1570{
1571
1572
1573
1574
1575 vio_unregister_driver(&hvcs_vio_driver);
1576 if (!hvcs_task)
1577 return;
1578
1579
1580
1581
1582
1583 kthread_stop(hvcs_task);
1584
1585 spin_lock(&hvcs_pi_lock);
1586 free_page((unsigned long)hvcs_pi_buff);
1587 hvcs_pi_buff = NULL;
1588 spin_unlock(&hvcs_pi_lock);
1589
1590 driver_remove_file(&hvcs_vio_driver.driver, &driver_attr_rescan);
1591
1592 tty_unregister_driver(hvcs_tty_driver);
1593
1594 hvcs_free_index_list();
1595
1596 put_tty_driver(hvcs_tty_driver);
1597
1598 printk(KERN_INFO "HVCS: driver module removed.\n");
1599}
1600
1601module_init(hvcs_module_init);
1602module_exit(hvcs_module_exit);
1603