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