2 * iSeries vio driver interface to hvc_console.c
4 * This code is based heavily on hvc_vio.c and viocons.c
6 * Copyright (C) 2006 Stephen Rothwell, IBM Corporation
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 #include <linux/types.h>
24 #include <linux/init.h>
25 #include <linux/kernel.h>
26 #include <linux/spinlock.h>
27 #include <linux/console.h>
29 #include <asm/hvconsole.h>
32 #include <asm/firmware.h>
33 #include <asm/iseries/vio.h>
34 #include <asm/iseries/hv_call.h>
35 #include <asm/iseries/hv_lp_config.h>
36 #include <asm/iseries/hv_lp_event.h>
38 #include "hvc_console.h"
42 static DEFINE_SPINLOCK(consolelock
);
43 static DEFINE_SPINLOCK(consoleloglock
);
45 static const char hvc_driver_name
[] = "hvc_console";
47 #define IN_BUF_SIZE 200
50 * Our port information.
52 static struct port_info
{
54 u64 seq
; /* sequence number of last HV send */
55 u64 ack
; /* last ack from HV */
56 struct hvc_struct
*hp
;
59 unsigned char in_buf
[IN_BUF_SIZE
];
60 } port_info
[VTTY_PORTS
] = {
61 [ 0 ... VTTY_PORTS
- 1 ] = {
62 .lp
= HvLpIndexInvalid
66 #define viochar_is_console(pi) ((pi) == &port_info[0])
68 static struct vio_device_id hvc_driver_table
[] __devinitdata
= {
69 {"serial", "IBM,iSeries-vty"},
72 MODULE_DEVICE_TABLE(vio
, hvc_driver_table
);
74 static void hvlog(char *fmt
, ...)
81 spin_lock_irqsave(&consoleloglock
, flags
);
83 i
= vscnprintf(buf
, sizeof(buf
) - 1, fmt
, args
);
86 HvCall_writeLogBuffer(buf
, i
);
87 spin_unlock_irqrestore(&consoleloglock
, flags
);
91 * Initialize the common fields in a charLpEvent
93 static void init_data_event(struct viocharlpevent
*viochar
, HvLpIndex lp
)
95 struct HvLpEvent
*hev
= &viochar
->event
;
97 memset(viochar
, 0, sizeof(struct viocharlpevent
));
99 hev
->flags
= HV_LP_EVENT_VALID
| HV_LP_EVENT_DEFERRED_ACK
|
101 hev
->xType
= HvLpEvent_Type_VirtualIo
;
102 hev
->xSubtype
= viomajorsubtype_chario
| viochardata
;
103 hev
->xSourceLp
= HvLpConfig_getLpIndex();
105 hev
->xSizeMinus1
= sizeof(struct viocharlpevent
);
106 hev
->xSourceInstanceId
= viopath_sourceinst(lp
);
107 hev
->xTargetInstanceId
= viopath_targetinst(lp
);
110 static int get_chars(uint32_t vtermno
, char *buf
, int count
)
112 struct port_info
*pi
;
116 if (vtermno
>= VTTY_PORTS
)
121 pi
= &port_info
[vtermno
];
122 spin_lock_irqsave(&consolelock
, flags
);
127 n
= pi
->in_end
- pi
->in_start
;
130 memcpy(buf
, &pi
->in_buf
[pi
->in_start
], n
);
132 if (pi
->in_start
== pi
->in_end
) {
137 spin_unlock_irqrestore(&consolelock
, flags
);
141 static int put_chars(uint32_t vtermno
, const char *buf
, int count
)
143 struct viocharlpevent
*viochar
;
144 struct port_info
*pi
;
149 if (vtermno
>= VTTY_PORTS
)
152 pi
= &port_info
[vtermno
];
154 spin_lock_irqsave(&consolelock
, flags
);
156 if (viochar_is_console(pi
) && !viopath_isactive(pi
->lp
)) {
157 HvCall_writeLogBuffer(buf
, count
);
162 viochar
= vio_get_event_buffer(viomajorsubtype_chario
);
163 if (viochar
== NULL
) {
164 hvlog("\n\rviocons: Can't get viochar buffer.");
168 while ((count
> 0) && ((pi
->seq
- pi
->ack
) < VIOCHAR_WINDOW
)) {
171 len
= (count
> VIOCHAR_MAX_DATA
) ? VIOCHAR_MAX_DATA
: count
;
173 if (viochar_is_console(pi
))
174 HvCall_writeLogBuffer(buf
, len
);
176 init_data_event(viochar
, pi
->lp
);
179 viochar
->event
.xCorrelationToken
= pi
->seq
++;
180 viochar
->event
.xSizeMinus1
=
181 offsetof(struct viocharlpevent
, data
) + len
;
183 memcpy(viochar
->data
, buf
, len
);
185 hvrc
= HvCallEvent_signalLpEvent(&viochar
->event
);
187 hvlog("\n\rerror sending event! return code %d\n\r",
194 vio_free_event_buffer(viomajorsubtype_chario
, viochar
);
196 spin_unlock_irqrestore(&consolelock
, flags
);
200 static struct hv_ops hvc_get_put_ops
= {
201 .get_chars
= get_chars
,
202 .put_chars
= put_chars
,
203 .notifier_add
= notifier_add_irq
,
204 .notifier_del
= notifier_del_irq
,
205 .notifier_hangup
= notifier_hangup_irq
,
208 static int __devinit
hvc_vio_probe(struct vio_dev
*vdev
,
209 const struct vio_device_id
*id
)
211 struct hvc_struct
*hp
;
212 struct port_info
*pi
;
214 /* probed with invalid parameters. */
218 if (vdev
->unit_address
>= VTTY_PORTS
)
221 pi
= &port_info
[vdev
->unit_address
];
223 hp
= hvc_alloc(vdev
->unit_address
, vdev
->irq
, &hvc_get_put_ops
,
228 dev_set_drvdata(&vdev
->dev
, pi
);
233 static int __devexit
hvc_vio_remove(struct vio_dev
*vdev
)
235 struct port_info
*pi
= dev_get_drvdata(&vdev
->dev
);
236 struct hvc_struct
*hp
= pi
->hp
;
238 return hvc_remove(hp
);
241 static struct vio_driver hvc_vio_driver
= {
242 .id_table
= hvc_driver_table
,
243 .probe
= hvc_vio_probe
,
244 .remove
= hvc_vio_remove
,
246 .name
= hvc_driver_name
,
247 .owner
= THIS_MODULE
,
251 static void hvc_open_event(struct HvLpEvent
*event
)
254 struct viocharlpevent
*cevent
= (struct viocharlpevent
*)event
;
255 u8 port
= cevent
->virtual_device
;
256 struct port_info
*pi
;
259 if (hvlpevent_is_ack(event
)) {
260 if (port
>= VTTY_PORTS
)
263 spin_lock_irqsave(&consolelock
, flags
);
265 pi
= &port_info
[port
];
266 if (event
->xRc
== HvLpEvent_Rc_Good
) {
267 pi
->seq
= pi
->ack
= 0;
269 * This line allows connections from the primary
270 * partition but once one is connected from the
271 * primary partition nothing short of a reboot
272 * of linux will allow access from the hosting
273 * partition again without a required iSeries fix.
275 pi
->lp
= event
->xTargetLp
;
278 spin_unlock_irqrestore(&consolelock
, flags
);
279 if (event
->xRc
!= HvLpEvent_Rc_Good
)
281 "hvc: handle_open_event: event->xRc == (%d).\n",
284 if (event
->xCorrelationToken
!= 0) {
285 atomic_t
*aptr
= (atomic_t
*)event
->xCorrelationToken
;
289 "hvc: weird...got open ack without atomic\n");
293 /* This had better require an ack, otherwise complain */
294 if (!hvlpevent_need_ack(event
)) {
295 printk(KERN_WARNING
"hvc: viocharopen without ack bit!\n");
299 spin_lock_irqsave(&consolelock
, flags
);
301 /* Make sure this is a good virtual tty */
302 if (port
>= VTTY_PORTS
) {
303 event
->xRc
= HvLpEvent_Rc_SubtypeError
;
304 cevent
->subtype_result_code
= viorc_openRejected
;
306 * Flag state here since we can't printk while holding
307 * the consolelock spinlock.
311 pi
= &port_info
[port
];
312 if ((pi
->lp
!= HvLpIndexInvalid
) &&
313 (pi
->lp
!= event
->xSourceLp
)) {
315 * If this is tty is already connected to a different
318 event
->xRc
= HvLpEvent_Rc_SubtypeError
;
319 cevent
->subtype_result_code
= viorc_openRejected
;
322 pi
->lp
= event
->xSourceLp
;
323 event
->xRc
= HvLpEvent_Rc_Good
;
324 cevent
->subtype_result_code
= viorc_good
;
325 pi
->seq
= pi
->ack
= 0;
329 spin_unlock_irqrestore(&consolelock
, flags
);
332 printk(KERN_WARNING
"hvc: open rejected: bad virtual tty.\n");
333 else if (reject
== 2)
334 printk(KERN_WARNING
"hvc: open rejected: console in exclusive "
335 "use by another partition.\n");
337 /* Return the acknowledgement */
338 HvCallEvent_ackLpEvent(event
);
342 * Handle a close charLpEvent. This should ONLY be an Interrupt because the
343 * virtual console should never actually issue a close event to the hypervisor
344 * because the virtual console never goes away. A close event coming from the
345 * hypervisor simply means that there are no client consoles connected to the
348 static void hvc_close_event(struct HvLpEvent
*event
)
351 struct viocharlpevent
*cevent
= (struct viocharlpevent
*)event
;
352 u8 port
= cevent
->virtual_device
;
354 if (!hvlpevent_is_int(event
)) {
356 "hvc: got unexpected close acknowlegement\n");
360 if (port
>= VTTY_PORTS
) {
362 "hvc: close message from invalid virtual device.\n");
366 /* For closes, just mark the console partition invalid */
367 spin_lock_irqsave(&consolelock
, flags
);
369 if (port_info
[port
].lp
== event
->xSourceLp
)
370 port_info
[port
].lp
= HvLpIndexInvalid
;
372 spin_unlock_irqrestore(&consolelock
, flags
);
375 static void hvc_data_event(struct HvLpEvent
*event
)
378 struct viocharlpevent
*cevent
= (struct viocharlpevent
*)event
;
379 struct port_info
*pi
;
381 u8 port
= cevent
->virtual_device
;
383 if (port
>= VTTY_PORTS
) {
384 printk(KERN_WARNING
"hvc: data on invalid virtual device %d\n",
388 if (cevent
->len
== 0)
392 * Change 05/01/2003 - Ryan Arnold: If a partition other than
393 * the current exclusive partition tries to send us data
394 * events then just drop them on the floor because we don't
395 * want his stinking data. He isn't authorized to receive
396 * data because he wasn't the first one to get the console,
397 * therefore he shouldn't be allowed to send data either.
398 * This will work without an iSeries fix.
400 pi
= &port_info
[port
];
401 if (pi
->lp
!= event
->xSourceLp
)
404 spin_lock_irqsave(&consolelock
, flags
);
406 n
= IN_BUF_SIZE
- pi
->in_end
;
410 memcpy(&pi
->in_buf
[pi
->in_end
], cevent
->data
, n
);
413 spin_unlock_irqrestore(&consolelock
, flags
);
415 printk(KERN_WARNING
"hvc: input buffer overflow\n");
418 static void hvc_ack_event(struct HvLpEvent
*event
)
420 struct viocharlpevent
*cevent
= (struct viocharlpevent
*)event
;
422 u8 port
= cevent
->virtual_device
;
424 if (port
>= VTTY_PORTS
) {
425 printk(KERN_WARNING
"hvc: data on invalid virtual device\n");
429 spin_lock_irqsave(&consolelock
, flags
);
430 port_info
[port
].ack
= event
->xCorrelationToken
;
431 spin_unlock_irqrestore(&consolelock
, flags
);
434 static void hvc_config_event(struct HvLpEvent
*event
)
436 struct viocharlpevent
*cevent
= (struct viocharlpevent
*)event
;
438 if (cevent
->data
[0] == 0x01)
439 printk(KERN_INFO
"hvc: window resized to %d: %d: %d: %d\n",
440 cevent
->data
[1], cevent
->data
[2],
441 cevent
->data
[3], cevent
->data
[4]);
443 printk(KERN_WARNING
"hvc: unknown config event\n");
446 static void hvc_handle_event(struct HvLpEvent
*event
)
453 charminor
= event
->xSubtype
& VIOMINOR_SUBTYPE_MASK
;
456 hvc_open_event(event
);
459 hvc_close_event(event
);
462 hvc_data_event(event
);
465 hvc_ack_event(event
);
468 hvc_config_event(event
);
471 if (hvlpevent_is_int(event
) && hvlpevent_need_ack(event
)) {
472 event
->xRc
= HvLpEvent_Rc_InvalidSubtype
;
473 HvCallEvent_ackLpEvent(event
);
478 static int __init
send_open(HvLpIndex remoteLp
, void *sem
)
480 return HvCallEvent_signalLpEventFast(remoteLp
,
481 HvLpEvent_Type_VirtualIo
,
482 viomajorsubtype_chario
| viocharopen
,
483 HvLpEvent_AckInd_DoAck
, HvLpEvent_AckType_ImmediateAck
,
484 viopath_sourceinst(remoteLp
),
485 viopath_targetinst(remoteLp
),
486 (u64
)(unsigned long)sem
, VIOVERSION
<< 16,
490 static int __init
hvc_vio_init(void)
495 if (!firmware_has_feature(FW_FEATURE_ISERIES
))
499 rc
= viopath_open(HvLpConfig_getPrimaryLpIndex(),
500 viomajorsubtype_chario
, VIOCHAR_WINDOW
+ 2);
502 printk(KERN_WARNING
"hvc: error opening to primary %d\n", rc
);
504 if (viopath_hostLp
== HvLpIndexInvalid
)
508 * And if the primary is not the same as the hosting LP, open to the
511 if ((viopath_hostLp
!= HvLpIndexInvalid
) &&
512 (viopath_hostLp
!= HvLpConfig_getPrimaryLpIndex())) {
513 printk(KERN_INFO
"hvc: open path to hosting (%d)\n",
515 rc
= viopath_open(viopath_hostLp
, viomajorsubtype_chario
,
516 VIOCHAR_WINDOW
+ 2); /* +2 for fudge */
519 "error opening to partition %d: %d\n",
523 if (vio_setHandler(viomajorsubtype_chario
, hvc_handle_event
) < 0)
525 "hvc: error seting handler for console events!\n");
528 * First, try to open the console to the hosting lp.
529 * Wait on a semaphore for the response.
531 atomic_set(&wait_flag
, 0);
532 if ((viopath_isactive(viopath_hostLp
)) &&
533 (send_open(viopath_hostLp
, &wait_flag
) == 0)) {
534 printk(KERN_INFO
"hvc: hosting partition %d\n", viopath_hostLp
);
535 while (atomic_read(&wait_flag
) == 0)
537 atomic_set(&wait_flag
, 0);
541 * If we don't have an active console, try the primary
543 if ((!viopath_isactive(port_info
[0].lp
)) &&
544 (viopath_isactive(HvLpConfig_getPrimaryLpIndex())) &&
545 (send_open(HvLpConfig_getPrimaryLpIndex(), &wait_flag
) == 0)) {
546 printk(KERN_INFO
"hvc: opening console to primary partition\n");
547 while (atomic_read(&wait_flag
) == 0)
551 /* Register as a vio device to receive callbacks */
552 rc
= vio_register_driver(&hvc_vio_driver
);
556 module_init(hvc_vio_init
); /* after drivers/char/hvc_console.c */
558 static void __exit
hvc_vio_exit(void)
560 vio_unregister_driver(&hvc_vio_driver
);
562 module_exit(hvc_vio_exit
);
564 /* the device tree order defines our numbering */
565 static int __init
hvc_find_vtys(void)
567 struct device_node
*vty
;
570 for (vty
= of_find_node_by_name(NULL
, "vty"); vty
!= NULL
;
571 vty
= of_find_node_by_name(vty
, "vty")) {
572 const uint32_t *vtermno
;
574 /* We have statically defined space for only a certain number
575 * of console adapters.
577 if ((num_found
>= MAX_NR_HVC_CONSOLES
) ||
578 (num_found
>= VTTY_PORTS
)) {
583 vtermno
= of_get_property(vty
, "reg", NULL
);
587 if (!of_device_is_compatible(vty
, "IBM,iSeries-vty"))
591 add_preferred_console("hvc", 0, NULL
);
592 hvc_instantiate(*vtermno
, num_found
, &hvc_get_put_ops
);
598 console_initcall(hvc_find_vtys
);