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