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