3 * drivers/char/viocons.c
5 * iSeries Virtual Terminal
7 * Authors: Dave Boutcher <boutcher@us.ibm.com>
8 * Ryan Arnold <ryanarn@us.ibm.com>
9 * Colin Devilbiss <devilbis@us.ibm.com>
10 * Stephen Rothwell <sfr@au1.ibm.com>
12 * (C) Copyright 2000, 2001, 2002, 2003, 2004 IBM Corporation
14 * This program is free software; you can redistribute it and/or
15 * modify it under the terms of the GNU General Public License as
16 * published by the Free Software Foundation; either version 2 of the
17 * License, or (at your option) anyu later version.
19 * This program is distributed in the hope that it will be useful, but
20 * WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
22 * General Public License for more details.
24 * You should have received a copy of the GNU General Public License
25 * along with this program; if not, write to the Free Software Foundation,
26 * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
28 #include <linux/config.h>
29 #include <linux/version.h>
30 #include <linux/kernel.h>
31 #include <linux/proc_fs.h>
32 #include <linux/errno.h>
33 #include <linux/vmalloc.h>
35 #include <linux/console.h>
36 #include <linux/module.h>
37 #include <asm/uaccess.h>
38 #include <linux/init.h>
39 #include <linux/wait.h>
40 #include <linux/spinlock.h>
41 #include <asm/ioctls.h>
43 #include <linux/tty.h>
44 #include <linux/tty_flip.h>
45 #include <linux/sysrq.h>
47 #include <asm/iSeries/vio.h>
49 #include <asm/iSeries/HvLpEvent.h>
50 #include <asm/iSeries/HvCallEvent.h>
51 #include <asm/iSeries/HvLpConfig.h>
52 #include <asm/iSeries/HvCall.h>
55 #error You must turn off CONFIG_VT to use CONFIG_VIOCONS
58 #define VIOTTY_MAGIC (0x0DCB)
61 #define VIOCONS_KERN_WARN KERN_WARNING "viocons: "
62 #define VIOCONS_KERN_INFO KERN_INFO "viocons: "
64 static spinlock_t consolelock = SPIN_LOCK_UNLOCKED;
65 static spinlock_t consoleloglock = SPIN_LOCK_UNLOCKED;
67 #ifdef CONFIG_MAGIC_SYSRQ
68 static int vio_sysrq_pressed;
69 extern int sysrq_enabled;
73 * The structure of the events that flow between us and OS/400. You can't
74 * mess with this unless the OS/400 side changes too
76 struct viocharlpevent {
77 struct HvLpEvent event;
80 u16 subtype_result_code;
83 u8 data[VIOCHAR_MAX_DATA];
87 * This is a place where we handle the distribution of memory
88 * for copy_from_user() calls. The buffer_available array is to
89 * help us determine which buffer to use.
91 #define VIOCHAR_NUM_CFU_BUFFERS 7
92 static struct viocharlpevent viocons_cfu_buffer[VIOCHAR_NUM_CFU_BUFFERS];
93 static atomic_t viocons_cfu_buffer_available[VIOCHAR_NUM_CFU_BUFFERS];
95 #define VIOCHAR_WINDOW 10
96 #define VIOCHAR_HIGHWATERMARK 3
100 viocharclose = 0x0002,
101 viochardata = 0x0003,
103 viocharconfig = 0x0005
110 #define VIOCHAR_NUM_BUF 16
113 * Our port information. We store a pointer to one entry in the
116 static struct port_info {
118 struct tty_struct *tty;
121 u64 seq; /* sequence number of last HV send */
122 u64 ack; /* last ack from HV */
124 * When we get writes faster than we can send it to the partition,
125 * buffer the data here. Note that used is a bit map of used buffers.
126 * It had better have enough bits to hold VIOCHAR_NUM_BUF the bitops assume
127 * it is a multiple of unsigned long
130 u8 *buffer[VIOCHAR_NUM_BUF];
131 int bufferBytes[VIOCHAR_NUM_BUF];
135 } port_info[VTTY_PORTS];
137 #define viochar_is_console(pi) ((pi) == &port_info[0])
138 #define viochar_port(pi) ((pi) - &port_info[0])
140 static void initDataEvent(struct viocharlpevent *viochar, HvLpIndex lp);
142 static struct tty_driver *viotty_driver;
144 void hvlog(char *fmt, ...)
149 static char buf[256];
151 spin_lock_irqsave(&consoleloglock, flags);
153 i = vscnprintf(buf, sizeof(buf) - 1, fmt, args);
156 HvCall_writeLogBuffer(buf, i);
157 spin_unlock_irqrestore(&consoleloglock, flags);
160 void hvlogOutput(const char *buf, int count)
165 static const char cr = '\r';
168 spin_lock_irqsave(&consoleloglock, flags);
169 for (index = 0; index < count; index++) {
170 if (buf[index] == '\n') {
172 * Start right after the last '\n' or at the zeroth
173 * array position and output the number of characters
174 * including the newline.
176 HvCall_writeLogBuffer(&buf[begin], index - begin + 1);
178 HvCall_writeLogBuffer(&cr, 1);
181 if ((index - begin) > 0)
182 HvCall_writeLogBuffer(&buf[begin], index - begin);
183 spin_unlock_irqrestore(&consoleloglock, flags);
187 * Make sure we're pointing to a valid port_info structure. Shamelessly
188 * plagerized from serial.c
190 static inline int viotty_paranoia_check(struct port_info *pi,
191 char *name, const char *routine)
193 static const char *bad_pi_addr = VIOCONS_KERN_WARN
194 "warning: bad address for port_info struct (%s) in %s\n";
195 static const char *badmagic = VIOCONS_KERN_WARN
196 "warning: bad magic number for port_info struct (%s) in %s\n";
198 if ((pi < &port_info[0]) || (viochar_port(pi) > VTTY_PORTS)) {
199 printk(bad_pi_addr, name, routine);
202 if (pi->magic != VIOTTY_MAGIC) {
203 printk(badmagic, name, routine);
210 * This function should ONLY be called once from viocons_init2
212 static void viocons_init_cfu_buffer(void)
216 for (i = 1; i < VIOCHAR_NUM_CFU_BUFFERS; i++)
217 atomic_set(&viocons_cfu_buffer_available[i], 1);
220 static struct viocharlpevent *viocons_get_cfu_buffer(void)
225 * Grab the first available buffer. It doesn't matter if we
226 * are interrupted during this array traversal as long as we
227 * get an available space.
229 for (i = 0; i < VIOCHAR_NUM_CFU_BUFFERS; i++)
230 if (atomic_dec_if_positive(&viocons_cfu_buffer_available[i])
232 return &viocons_cfu_buffer[i];
233 hvlog("\n\rviocons: viocons_get_cfu_buffer : no free buffers found");
237 static void viocons_free_cfu_buffer(struct viocharlpevent *buffer)
241 i = buffer - &viocons_cfu_buffer[0];
242 if (i >= (sizeof(viocons_cfu_buffer) / sizeof(viocons_cfu_buffer[0]))) {
243 hvlog("\n\rviocons: viocons_free_cfu_buffer : buffer pointer not found in list.");
246 if (atomic_read(&viocons_cfu_buffer_available[i]) != 0) {
247 hvlog("\n\rviocons: WARNING : returning unallocated cfu buffer.");
250 atomic_set(&viocons_cfu_buffer_available[i], 1);
254 * Add data to our pending-send buffers.
256 * NOTE: Don't use printk in here because it gets nastily recursive.
257 * hvlog can be used to log to the hypervisor buffer
259 static int buffer_add(struct port_info *pi, const char *buf, size_t len)
270 * If there is no space left in the current buffer, we have
271 * filled everything up, so return. If we filled the previous
272 * buffer we would already have moved to the next one.
274 if (pi->bufferBytes[pi->curbuf] == VIOCHAR_MAX_DATA) {
275 hvlog ("\n\rviocons: No overflow buffer available for memcpy().\n");
276 pi->bufferOverflow++;
277 pi->overflowMessage = 1;
282 * Turn on the "used" bit for this buffer. If it's already on,
285 set_bit(pi->curbuf, &pi->used);
288 * See if this buffer has been allocated. If not, allocate it.
290 if (pi->buffer[pi->curbuf] == NULL) {
291 pi->buffer[pi->curbuf] =
292 kmalloc(VIOCHAR_MAX_DATA, GFP_ATOMIC);
293 if (pi->buffer[pi->curbuf] == NULL) {
294 hvlog("\n\rviocons: kmalloc failed allocating spaces for buffer %d.",
300 /* Figure out how much we can copy into this buffer. */
301 if (bleft < (VIOCHAR_MAX_DATA - pi->bufferBytes[pi->curbuf]))
304 curlen = VIOCHAR_MAX_DATA - pi->bufferBytes[pi->curbuf];
306 /* Copy the data into the buffer. */
307 memcpy(pi->buffer[pi->curbuf] + pi->bufferBytes[pi->curbuf],
310 pi->bufferBytes[pi->curbuf] += curlen;
315 * Now see if we've filled this buffer. If not then
316 * we'll try to use it again later. If we've filled it
317 * up then we'll advance the curbuf to the next in the
320 if (pi->bufferBytes[pi->curbuf] == VIOCHAR_MAX_DATA) {
321 nextbuf = (pi->curbuf + 1) % VIOCHAR_NUM_BUF;
323 * Move to the next buffer if it hasn't been used yet
325 if (test_bit(nextbuf, &pi->used) == 0)
326 pi->curbuf = nextbuf;
335 * NOTE: Don't use printk in here because it gets nastily recursive.
336 * hvlog can be used to log to the hypervisor buffer
338 static void send_buffers(struct port_info *pi)
342 struct viocharlpevent *viochar;
345 spin_lock_irqsave(&consolelock, flags);
347 viochar = (struct viocharlpevent *)
348 vio_get_event_buffer(viomajorsubtype_chario);
350 /* Make sure we got a buffer */
351 if (viochar == NULL) {
352 hvlog("\n\rviocons: Can't get viochar buffer in sendBuffers().");
353 spin_unlock_irqrestore(&consolelock, flags);
358 hvlog("\n\rviocons: in sendbuffers(), but no buffers used.\n");
359 vio_free_event_buffer(viomajorsubtype_chario, viochar);
360 spin_unlock_irqrestore(&consolelock, flags);
365 * curbuf points to the buffer we're filling. We want to
366 * start sending AFTER this one.
368 nextbuf = (pi->curbuf + 1) % VIOCHAR_NUM_BUF;
371 * Loop until we find a buffer with the used bit on
373 while (test_bit(nextbuf, &pi->used) == 0)
374 nextbuf = (nextbuf + 1) % VIOCHAR_NUM_BUF;
376 initDataEvent(viochar, pi->lp);
379 * While we have buffers with data, and our send window
382 while ((test_bit(nextbuf, &pi->used)) &&
383 ((pi->seq - pi->ack) < VIOCHAR_WINDOW)) {
384 viochar->len = pi->bufferBytes[nextbuf];
385 viochar->event.xCorrelationToken = pi->seq++;
386 viochar->event.xSizeMinus1 =
387 offsetof(struct viocharlpevent, data) + viochar->len;
389 memcpy(viochar->data, pi->buffer[nextbuf], viochar->len);
391 hvrc = HvCallEvent_signalLpEvent(&viochar->event);
394 * MUST unlock the spinlock before doing a printk
396 vio_free_event_buffer(viomajorsubtype_chario, viochar);
397 spin_unlock_irqrestore(&consolelock, flags);
399 printk(VIOCONS_KERN_WARN
400 "error sending event! return code %d\n",
406 * clear the used bit, zero the number of bytes in
407 * this buffer, and move to the next buffer
409 clear_bit(nextbuf, &pi->used);
410 pi->bufferBytes[nextbuf] = 0;
411 nextbuf = (nextbuf + 1) % VIOCHAR_NUM_BUF;
415 * If we have emptied all the buffers, start at 0 again.
416 * this will re-use any allocated buffers
421 if (pi->overflowMessage)
422 pi->overflowMessage = 0;
425 if ((pi->tty->flags & (1 << TTY_DO_WRITE_WAKEUP)) &&
426 (pi->tty->ldisc.write_wakeup))
427 (pi->tty->ldisc.write_wakeup)(pi->tty);
428 wake_up_interruptible(&pi->tty->write_wait);
432 vio_free_event_buffer(viomajorsubtype_chario, viochar);
433 spin_unlock_irqrestore(&consolelock, flags);
437 * Our internal writer. Gets called both from the console device and
438 * the tty device. the tty pointer will be NULL if called from the console.
439 * Return total number of bytes "written".
441 * NOTE: Don't use printk in here because it gets nastily recursive. hvlog
442 * can be used to log to the hypervisor buffer
444 static int internal_write(struct port_info *pi, const char *buf,
445 size_t len, struct viocharlpevent *viochar)
452 int copy_needed = (viochar == NULL);
455 * Write to the hvlog of inbound data are now done prior to
456 * calling internal_write() since internal_write() is only called in
457 * the event that an lp event path is active, which isn't the case for
458 * logging attempts prior to console initialization.
460 * If there is already data queued for this port, send it prior to
461 * attempting to send any new data.
466 spin_lock_irqsave(&consolelock, flags);
469 * If the internal_write() was passed a pointer to a
470 * viocharlpevent then we don't need to allocate a new one
471 * (this is the case where we are internal_writing user space
472 * data). If we aren't writing user space data then we need
473 * to get an event from viopath.
476 /* This one is fetched from the viopath data structure */
477 viochar = (struct viocharlpevent *)
478 vio_get_event_buffer(viomajorsubtype_chario);
479 /* Make sure we got a buffer */
480 if (viochar == NULL) {
481 spin_unlock_irqrestore(&consolelock, flags);
482 hvlog("\n\rviocons: Can't get viochar buffer in internal_write().");
485 initDataEvent(viochar, pi->lp);
491 while ((bleft > 0) && (pi->used == 0) &&
492 ((pi->seq - pi->ack) < VIOCHAR_WINDOW)) {
493 if (bleft > VIOCHAR_MAX_DATA)
494 curlen = VIOCHAR_MAX_DATA;
498 viochar->event.xCorrelationToken = pi->seq++;
501 memcpy(viochar->data, curbuf, curlen);
502 viochar->len = curlen;
505 viochar->event.xSizeMinus1 =
506 offsetof(struct viocharlpevent, data) + curlen;
508 hvrc = HvCallEvent_signalLpEvent(&viochar->event);
510 spin_unlock_irqrestore(&consolelock, flags);
512 vio_free_event_buffer(viomajorsubtype_chario, viochar);
514 hvlog("viocons: error sending event! %d\n", (int)hvrc);
522 /* If we didn't send it all, buffer as much of it as we can. */
524 bleft -= buffer_add(pi, curbuf, bleft);
526 * Since we grabbed it from the viopath data structure, return
527 * it to the data structure.
530 vio_free_event_buffer(viomajorsubtype_chario, viochar);
531 spin_unlock_irqrestore(&consolelock, flags);
536 static struct port_info *get_port_data(struct tty_struct *tty)
539 struct port_info *pi;
541 spin_lock_irqsave(&consolelock, flags);
543 pi = (struct port_info *)tty->driver_data;
544 if (!pi || viotty_paranoia_check(pi, tty->name,
550 * If this is the console device, use the lp from
551 * the first port entry
554 spin_unlock_irqrestore(&consolelock, flags);
559 * Initialize the common fields in a charLpEvent
561 static void initDataEvent(struct viocharlpevent *viochar, HvLpIndex lp)
563 memset(viochar, 0, sizeof(struct viocharlpevent));
565 viochar->event.xFlags.xValid = 1;
566 viochar->event.xFlags.xFunction = HvLpEvent_Function_Int;
567 viochar->event.xFlags.xAckInd = HvLpEvent_AckInd_NoAck;
568 viochar->event.xFlags.xAckType = HvLpEvent_AckType_DeferredAck;
569 viochar->event.xType = HvLpEvent_Type_VirtualIo;
570 viochar->event.xSubtype = viomajorsubtype_chario | viochardata;
571 viochar->event.xSourceLp = HvLpConfig_getLpIndex();
572 viochar->event.xTargetLp = lp;
573 viochar->event.xSizeMinus1 = sizeof(struct viocharlpevent);
574 viochar->event.xSourceInstanceId = viopath_sourceinst(lp);
575 viochar->event.xTargetInstanceId = viopath_targetinst(lp);
579 * early console device write
581 static void viocons_write_early(struct console *co, const char *s, unsigned count)
583 hvlogOutput(s, count);
587 * console device write
589 static void viocons_write(struct console *co, const char *s, unsigned count)
593 struct port_info *pi;
595 static const char cr = '\r';
598 * Check port data first because the target LP might be valid but
599 * simply not active, in which case we want to hvlog the output.
601 pi = get_port_data(NULL);
603 hvlog("\n\rviocons_write: unable to get port data.");
607 hvlogOutput(s, count);
609 if (!viopath_isactive(pi->lp)) {
611 * This is a VERY noisy trace message in the case where the
612 * path manager is not active or in the case where this
613 * function is called prior to viocons initialization. It is
614 * being commented out for the sake of a clear trace buffer.
617 hvlog("\n\rviocons_write: path not active to lp %d", pi->lp);
623 * Any newline character found will cause a
624 * carriage return character to be emitted as well.
627 for (index = 0; index < count; index++) {
628 if (s[index] == '\n') {
630 * Newline found. Print everything up to and
631 * including the newline
633 internal_write(pi, &s[begin], index - begin + 1,
636 /* Emit a carriage return as well */
637 internal_write(pi, &cr, 1, NULL);
641 /* If any characters left to write, write them now */
642 if ((index - begin) > 0)
643 internal_write(pi, &s[begin], index - begin, NULL);
647 * Work out the device associate with this console
649 static struct tty_driver *viocons_device(struct console *c, int *index)
652 return viotty_driver;
656 * console device I/O methods
658 static struct console viocons_early = {
660 .write = viocons_write_early,
661 .flags = CON_PRINTBUFFER,
665 static struct console viocons = {
667 .write = viocons_write,
668 .device = viocons_device,
669 .flags = CON_PRINTBUFFER,
676 static int viotty_open(struct tty_struct *tty, struct file *filp)
680 struct port_info *pi;
684 if ((port < 0) || (port >= VTTY_PORTS))
687 spin_lock_irqsave(&consolelock, flags);
689 pi = &port_info[port];
690 /* If some other TTY is already connected here, reject the open */
691 if ((pi->tty) && (pi->tty != tty)) {
692 spin_unlock_irqrestore(&consolelock, flags);
693 printk(VIOCONS_KERN_WARN
694 "attempt to open device twice from different ttys\n");
697 tty->driver_data = pi;
699 spin_unlock_irqrestore(&consolelock, flags);
707 static void viotty_close(struct tty_struct *tty, struct file *filp)
710 struct port_info *pi;
712 spin_lock_irqsave(&consolelock, flags);
713 pi = (struct port_info *)tty->driver_data;
715 if (!pi || viotty_paranoia_check(pi, tty->name, "viotty_close")) {
716 spin_unlock_irqrestore(&consolelock, flags);
721 spin_unlock_irqrestore(&consolelock, flags);
727 static int viotty_write(struct tty_struct *tty, int from_user,
728 const unsigned char *buf, int count)
732 struct port_info *pi;
734 pi = get_port_data(tty);
736 hvlog("\n\rviotty_write: no port data.");
740 if (viochar_is_console(pi))
741 hvlogOutput(buf, count);
744 * If the path to this LP is closed, don't bother doing anything more.
745 * just dump the data on the floor and return count. For some reason
746 * some user level programs will attempt to probe available tty's and
747 * they'll attempt a viotty_write on an invalid port which maps to an
748 * invalid target lp. If this is the case then ignore the
749 * viotty_write call and, since the viopath isn't active to this
750 * partition, return count.
752 if (!viopath_isactive(pi->lp)) {
753 /* Noisy trace. Commented unless needed. */
755 hvlog("\n\rviotty_write: viopath NOT active for lp %d.",pi->lp);
761 * If the viotty_write is invoked from user space we want to do the
762 * copy_from_user() into an event buffer from the cfu buffer before
763 * internal_write() is called because internal_write may need to buffer
764 * data which will need to grab a spin_lock and we shouldn't
765 * copy_from_user() while holding a spin_lock. Should internal_write()
766 * not need to buffer data then it'll just use the event we created here
767 * rather than checking one out from vio_get_event_buffer().
770 struct viocharlpevent *viochar;
772 const char *curbuf = buf;
774 viochar = viocons_get_cfu_buffer();
777 initDataEvent(viochar, pi->lp);
779 if (count > VIOCHAR_MAX_DATA)
780 curlen = VIOCHAR_MAX_DATA;
783 viochar->len = curlen;
784 ret = copy_from_user(viochar->data, curbuf, curlen);
787 ret = internal_write(pi, viochar->data,
788 viochar->len, viochar);
795 viocons_free_cfu_buffer(viochar);
797 total = internal_write(pi, buf, count, NULL);
802 * TTY put_char method
804 static void viotty_put_char(struct tty_struct *tty, unsigned char ch)
806 struct port_info *pi;
808 pi = get_port_data(tty);
812 /* This will append '\r' as well if the char is '\n' */
813 if (viochar_is_console(pi))
816 if (viopath_isactive(pi->lp))
817 internal_write(pi, &ch, 1, NULL);
821 * TTY write_room method
823 static int viotty_write_room(struct tty_struct *tty)
827 struct port_info *pi;
830 spin_lock_irqsave(&consolelock, flags);
831 pi = (struct port_info *)tty->driver_data;
832 if (!pi || viotty_paranoia_check(pi, tty->name, "viotty_write_room")) {
833 spin_unlock_irqrestore(&consolelock, flags);
837 /* If no buffers are used, return the max size. */
839 spin_unlock_irqrestore(&consolelock, flags);
840 return VIOCHAR_MAX_DATA * VIOCHAR_NUM_BUF;
844 * We retain the spinlock because we want to get an accurate
845 * count and it can change on us between each operation if we
846 * don't hold the spinlock.
848 for (i = 0; ((i < VIOCHAR_NUM_BUF) && (room < VIOCHAR_MAX_DATA)); i++)
849 room += (VIOCHAR_MAX_DATA - pi->bufferBytes[i]);
850 spin_unlock_irqrestore(&consolelock, flags);
852 if (room > VIOCHAR_MAX_DATA)
853 room = VIOCHAR_MAX_DATA;
858 * TTY chars_in_buffer method
860 static int viotty_chars_in_buffer(struct tty_struct *tty)
865 static int viotty_ioctl(struct tty_struct *tty, struct file *file,
866 unsigned int cmd, unsigned long arg)
870 * the ioctls below read/set the flags usually shown in the leds
871 * don't use them - they will go away without warning
875 return put_user(0, (char *)arg);
881 return n_tty_ioctl(tty, file, cmd, arg);
885 * Handle an open charLpEvent. Could be either interrupt or ack
887 static void vioHandleOpenEvent(struct HvLpEvent *event)
890 struct viocharlpevent *cevent = (struct viocharlpevent *)event;
891 u8 port = cevent->virtual_device;
892 struct port_info *pi;
895 if (event->xFlags.xFunction == HvLpEvent_Function_Ack) {
896 if (port >= VTTY_PORTS)
899 spin_lock_irqsave(&consolelock, flags);
900 /* Got the lock, don't cause console output */
902 pi = &port_info[port];
903 if (event->xRc == HvLpEvent_Rc_Good) {
904 pi->seq = pi->ack = 0;
906 * This line allows connections from the primary
907 * partition but once one is connected from the
908 * primary partition nothing short of a reboot
909 * of linux will allow access from the hosting
910 * partition again without a required iSeries fix.
912 pi->lp = event->xTargetLp;
915 spin_unlock_irqrestore(&consolelock, flags);
916 if (event->xRc != HvLpEvent_Rc_Good)
917 printk(VIOCONS_KERN_WARN
918 "handle_open_event: event->xRc == (%d).\n",
921 if (event->xCorrelationToken != 0) {
922 atomic_t *aptr= (atomic_t *)event->xCorrelationToken;
925 printk(VIOCONS_KERN_WARN
926 "wierd...got open ack without atomic\n");
930 /* This had better require an ack, otherwise complain */
931 if (event->xFlags.xAckInd != HvLpEvent_AckInd_DoAck) {
932 printk(VIOCONS_KERN_WARN "viocharopen without ack bit!\n");
936 spin_lock_irqsave(&consolelock, flags);
937 /* Got the lock, don't cause console output */
939 /* Make sure this is a good virtual tty */
940 if (port >= VTTY_PORTS) {
941 event->xRc = HvLpEvent_Rc_SubtypeError;
942 cevent->subtype_result_code = viorc_openRejected;
944 * Flag state here since we can't printk while holding
949 pi = &port_info[port];
950 if ((pi->lp != HvLpIndexInvalid) &&
951 (pi->lp != event->xSourceLp)) {
953 * If this is tty is already connected to a different
956 event->xRc = HvLpEvent_Rc_SubtypeError;
957 cevent->subtype_result_code = viorc_openRejected;
960 pi->lp = event->xSourceLp;
961 event->xRc = HvLpEvent_Rc_Good;
962 cevent->subtype_result_code = viorc_good;
963 pi->seq = pi->ack = 0;
968 spin_unlock_irqrestore(&consolelock, flags);
971 printk(VIOCONS_KERN_WARN "open rejected: bad virtual tty.\n");
972 else if (reject == 2)
973 printk(VIOCONS_KERN_WARN
974 "open rejected: console in exclusive use by another partition.\n");
976 /* Return the acknowledgement */
977 HvCallEvent_ackLpEvent(event);
981 * Handle a close charLpEvent. This should ONLY be an Interrupt because the
982 * virtual console should never actually issue a close event to the hypervisor
983 * because the virtual console never goes away. A close event coming from the
984 * hypervisor simply means that there are no client consoles connected to the
987 * Regardless of the number of connections masqueraded on the other side of
988 * the hypervisor ONLY ONE close event should be called to accompany the ONE
989 * open event that is called. The close event should ONLY be called when NO
990 * MORE connections (masqueraded or not) exist on the other side of the
993 static void vioHandleCloseEvent(struct HvLpEvent *event)
996 struct viocharlpevent *cevent = (struct viocharlpevent *)event;
997 u8 port = cevent->virtual_device;
999 if (event->xFlags.xFunction == HvLpEvent_Function_Int) {
1000 if (port >= VTTY_PORTS) {
1001 printk(VIOCONS_KERN_WARN
1002 "close message from invalid virtual device.\n");
1006 /* For closes, just mark the console partition invalid */
1007 spin_lock_irqsave(&consolelock, flags);
1008 /* Got the lock, don't cause console output */
1010 if (port_info[port].lp == event->xSourceLp)
1011 port_info[port].lp = HvLpIndexInvalid;
1013 spin_unlock_irqrestore(&consolelock, flags);
1014 printk(VIOCONS_KERN_INFO "close from %d\n", event->xSourceLp);
1016 printk(VIOCONS_KERN_WARN
1017 "got unexpected close acknowlegement\n");
1021 * Handle a config charLpEvent. Could be either interrupt or ack
1023 static void vioHandleConfig(struct HvLpEvent *event)
1025 struct viocharlpevent *cevent = (struct viocharlpevent *)event;
1027 HvCall_writeLogBuffer(cevent->data, cevent->len);
1029 if (cevent->data[0] == 0x01)
1030 printk(VIOCONS_KERN_INFO "window resized to %d: %d: %d: %d\n",
1031 cevent->data[1], cevent->data[2],
1032 cevent->data[3], cevent->data[4]);
1034 printk(VIOCONS_KERN_WARN "unknown config event\n");
1038 * Handle a data charLpEvent.
1040 static void vioHandleData(struct HvLpEvent *event)
1042 struct tty_struct *tty;
1043 unsigned long flags;
1044 struct viocharlpevent *cevent = (struct viocharlpevent *)event;
1045 struct port_info *pi;
1047 u8 port = cevent->virtual_device;
1049 if (port >= VTTY_PORTS) {
1050 printk(VIOCONS_KERN_WARN "data on invalid virtual device %d\n",
1056 * Hold the spinlock so that we don't take an interrupt that
1057 * changes tty between the time we fetch the port_info
1058 * pointer and the time we paranoia check.
1060 spin_lock_irqsave(&consolelock, flags);
1061 pi = &port_info[port];
1064 * Change 05/01/2003 - Ryan Arnold: If a partition other than
1065 * the current exclusive partition tries to send us data
1066 * events then just drop them on the floor because we don't
1067 * want his stinking data. He isn't authorized to receive
1068 * data because he wasn't the first one to get the console,
1069 * therefore he shouldn't be allowed to send data either.
1070 * This will work without an iSeries fix.
1072 if (pi->lp != event->xSourceLp) {
1073 spin_unlock_irqrestore(&consolelock, flags);
1079 spin_unlock_irqrestore(&consolelock, flags);
1080 printk(VIOCONS_KERN_WARN "no tty for virtual device %d\n",
1085 if (tty->magic != TTY_MAGIC) {
1086 spin_unlock_irqrestore(&consolelock, flags);
1087 printk(VIOCONS_KERN_WARN "tty bad magic\n");
1092 * Just to be paranoid, make sure the tty points back to this port
1094 pi = (struct port_info *)tty->driver_data;
1095 if (!pi || viotty_paranoia_check(pi, tty->name, "vioHandleData")) {
1096 spin_unlock_irqrestore(&consolelock, flags);
1099 spin_unlock_irqrestore(&consolelock, flags);
1102 * Change 07/21/2003 - Ryan Arnold: functionality added to
1103 * support sysrq utilizing ^O as the sysrq key. The sysrq
1104 * functionality will only work if built into the kernel and
1105 * then only if sysrq is enabled through the proc filesystem.
1107 for (index = 0; index < cevent->len; index++) {
1108 #ifdef CONFIG_MAGIC_SYSRQ
1109 if (sysrq_enabled) {
1110 /* 0x0f is the ascii character for ^O */
1111 if (cevent->data[index] == '\x0f') {
1112 vio_sysrq_pressed = 1;
1114 * continue because we don't want to add
1115 * the sysrq key into the data string.
1118 } else if (vio_sysrq_pressed) {
1119 handle_sysrq(cevent->data[index], NULL, tty);
1120 vio_sysrq_pressed = 0;
1122 * continue because we don't want to add
1123 * the sysrq sequence into the data string.
1130 * The sysrq sequence isn't included in this check if
1131 * sysrq is enabled and compiled into the kernel because
1132 * the sequence will never get inserted into the buffer.
1133 * Don't attempt to copy more data into the buffer than we
1134 * have room for because it would fail without indication.
1136 if ((tty->flip.count + 1) > TTY_FLIPBUF_SIZE) {
1137 printk(VIOCONS_KERN_WARN "input buffer overflow!\n");
1140 tty_insert_flip_char(tty, cevent->data[index], TTY_NORMAL);
1143 /* if cevent->len == 0 then no data was added to the buffer and flip.count == 0 */
1144 if (tty->flip.count)
1145 /* The next call resets flip.count when the data is flushed. */
1146 tty_flip_buffer_push(tty);
1150 * Handle an ack charLpEvent.
1152 static void vioHandleAck(struct HvLpEvent *event)
1154 struct viocharlpevent *cevent = (struct viocharlpevent *)event;
1155 unsigned long flags;
1156 u8 port = cevent->virtual_device;
1158 if (port >= VTTY_PORTS) {
1159 printk(VIOCONS_KERN_WARN "data on invalid virtual device\n");
1163 spin_lock_irqsave(&consolelock, flags);
1164 port_info[port].ack = event->xCorrelationToken;
1165 spin_unlock_irqrestore(&consolelock, flags);
1167 if (port_info[port].used)
1168 send_buffers(&port_info[port]);
1172 * Handle charLpEvents and route to the appropriate routine
1174 static void vioHandleCharEvent(struct HvLpEvent *event)
1181 charminor = event->xSubtype & VIOMINOR_SUBTYPE_MASK;
1182 switch (charminor) {
1184 vioHandleOpenEvent(event);
1187 vioHandleCloseEvent(event);
1190 vioHandleData(event);
1193 vioHandleAck(event);
1196 vioHandleConfig(event);
1199 if ((event->xFlags.xFunction == HvLpEvent_Function_Int) &&
1200 (event->xFlags.xAckInd == HvLpEvent_AckInd_DoAck)) {
1201 event->xRc = HvLpEvent_Rc_InvalidSubtype;
1202 HvCallEvent_ackLpEvent(event);
1208 * Send an open event
1210 static int send_open(HvLpIndex remoteLp, void *sem)
1212 return HvCallEvent_signalLpEventFast(remoteLp,
1213 HvLpEvent_Type_VirtualIo,
1214 viomajorsubtype_chario | viocharopen,
1215 HvLpEvent_AckInd_DoAck, HvLpEvent_AckType_ImmediateAck,
1216 viopath_sourceinst(remoteLp),
1217 viopath_targetinst(remoteLp),
1218 (u64)(unsigned long)sem, VIOVERSION << 16,
1222 static struct tty_operations serial_ops = {
1223 .open = viotty_open,
1224 .close = viotty_close,
1225 .write = viotty_write,
1226 .put_char = viotty_put_char,
1227 .write_room = viotty_write_room,
1228 .chars_in_buffer = viotty_chars_in_buffer,
1229 .ioctl = viotty_ioctl,
1232 static int __init viocons_init2(void)
1238 rc = viopath_open(HvLpConfig_getPrimaryLpIndex(),
1239 viomajorsubtype_chario, VIOCHAR_WINDOW + 2);
1241 printk(VIOCONS_KERN_WARN "error opening to primary %d\n", rc);
1243 if (viopath_hostLp == HvLpIndexInvalid)
1247 * And if the primary is not the same as the hosting LP, open to the
1250 if ((viopath_hostLp != HvLpIndexInvalid) &&
1251 (viopath_hostLp != HvLpConfig_getPrimaryLpIndex())) {
1252 printk(VIOCONS_KERN_INFO "open path to hosting (%d)\n",
1254 rc = viopath_open(viopath_hostLp, viomajorsubtype_chario,
1255 VIOCHAR_WINDOW + 2); /* +2 for fudge */
1257 printk(VIOCONS_KERN_WARN
1258 "error opening to partition %d: %d\n",
1259 viopath_hostLp, rc);
1262 if (vio_setHandler(viomajorsubtype_chario, vioHandleCharEvent) < 0)
1263 printk(VIOCONS_KERN_WARN
1264 "error seting handler for console events!\n");
1267 * First, try to open the console to the hosting lp.
1268 * Wait on a semaphore for the response.
1270 atomic_set(&wait_flag, 0);
1271 if ((viopath_isactive(viopath_hostLp)) &&
1272 (send_open(viopath_hostLp, (void *)&wait_flag) == 0)) {
1273 printk(VIOCONS_KERN_INFO "hosting partition %d\n",
1275 while (atomic_read(&wait_flag) == 0)
1277 atomic_set(&wait_flag, 0);
1281 * If we don't have an active console, try the primary
1283 if ((!viopath_isactive(port_info[0].lp)) &&
1284 (viopath_isactive(HvLpConfig_getPrimaryLpIndex())) &&
1285 (send_open(HvLpConfig_getPrimaryLpIndex(), (void *)&wait_flag)
1287 printk(VIOCONS_KERN_INFO "opening console to primary partition\n");
1288 while (atomic_read(&wait_flag) == 0)
1292 /* Initialize the tty_driver structure */
1293 viotty_driver = alloc_tty_driver(VTTY_PORTS);
1294 viotty_driver->owner = THIS_MODULE;
1295 viotty_driver->driver_name = "vioconsole";
1296 viotty_driver->devfs_name = "vcs/";
1297 viotty_driver->name = "tty";
1298 viotty_driver->name_base = 1;
1299 viotty_driver->major = TTY_MAJOR;
1300 viotty_driver->minor_start = 1;
1301 viotty_driver->type = TTY_DRIVER_TYPE_CONSOLE;
1302 viotty_driver->subtype = 1;
1303 viotty_driver->init_termios = tty_std_termios;
1304 viotty_driver->flags = TTY_DRIVER_REAL_RAW | TTY_DRIVER_RESET_TERMIOS;
1305 tty_set_operations(viotty_driver, &serial_ops);
1307 if (tty_register_driver(viotty_driver)) {
1308 printk(VIOCONS_KERN_WARN "couldn't register console driver\n");
1309 put_tty_driver(viotty_driver);
1310 viotty_driver = NULL;
1313 viocons_init_cfu_buffer();
1315 unregister_console(&viocons_early);
1316 register_console(&viocons);
1321 static int __init viocons_init(void)
1325 printk(VIOCONS_KERN_INFO "registering console\n");
1326 for (i = 0; i < VTTY_PORTS; i++) {
1327 port_info[i].lp = HvLpIndexInvalid;
1328 port_info[i].magic = VIOTTY_MAGIC;
1330 HvCall_setLogBufferFormatAndCodepage(HvCall_LogBuffer_ASCII, 437);
1331 register_console(&viocons_early);
1335 console_initcall(viocons_init);
1336 module_init(viocons_init2);