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