1 // SPDX-License-Identifier: GPL-2.0+
3 * IBM eServer Hypervisor Virtual Console Server Device Driver
4 * Copyright (C) 2003, 2004 IBM Corp.
9 * This is the device driver for the IBM Hypervisor Virtual Console Server,
10 * "hvcs". The IBM hvcs provides a tty driver interface to allow Linux
11 * user space applications access to the system consoles of logically
12 * partitioned operating systems, e.g. Linux, running on the same partitioned
13 * Power5 ppc64 system. Physical hardware consoles per partition are not
14 * practical on this hardware so system consoles are accessed by this driver
15 * using inter-partition firmware interfaces to virtual terminal devices.
17 * A vty is known to the HMC as a "virtual serial server adapter". It is a
18 * virtual terminal device that is created by firmware upon partition creation
19 * to act as a partitioned OS's console device.
21 * Firmware dynamically (via hotplug) exposes vty-servers to a running ppc64
22 * Linux system upon their creation by the HMC or their exposure during boot.
23 * The non-user interactive backend of this driver is implemented as a vio
24 * device driver so that it can receive notification of vty-server lifetimes
25 * after it registers with the vio bus to handle vty-server probe and remove
28 * Many vty-servers can be configured to connect to one vty, but a vty can
29 * only be actively connected to by a single vty-server, in any manner, at one
30 * time. If the HMC is currently hosting the console for a target Linux
31 * partition; attempts to open the tty device to the partition's console using
32 * the hvcs on any partition will return -EBUSY with every open attempt until
33 * the HMC frees the connection between its vty-server and the desired
34 * partition's vty device. Conversely, a vty-server may only be connected to
35 * a single vty at one time even though it may have several configured vty
36 * partner possibilities.
38 * Firmware does not provide notification of vty partner changes to this
39 * driver. This means that an HMC Super Admin may add or remove partner vtys
40 * from a vty-server's partner list but the changes will not be signaled to
41 * the vty-server. Firmware only notifies the driver when a vty-server is
42 * added or removed from the system. To compensate for this deficiency, this
43 * driver implements a sysfs update attribute which provides a method for
44 * rescanning partner information upon a user's request.
46 * Each vty-server, prior to being exposed to this driver is reference counted
47 * using the 2.6 Linux kernel kref construct.
49 * For direction on installation and usage of this driver please reference
50 * Documentation/powerpc/hvcs.rst.
53 #include <linux/device.h>
54 #include <linux/init.h>
55 #include <linux/interrupt.h>
56 #include <linux/kernel.h>
57 #include <linux/kref.h>
58 #include <linux/kthread.h>
59 #include <linux/list.h>
60 #include <linux/major.h>
61 #include <linux/module.h>
62 #include <linux/moduleparam.h>
63 #include <linux/sched.h>
64 #include <linux/slab.h>
65 #include <linux/spinlock.h>
66 #include <linux/stat.h>
67 #include <linux/tty.h>
68 #include <linux/tty_flip.h>
69 #include <asm/hvconsole.h>
70 #include <asm/hvcserver.h>
71 #include <linux/uaccess.h>
75 * 1.3.0 -> 1.3.1 In hvcs_open memset(..,0x00,..) instead of memset(..,0x3F,00).
76 * Removed braces around single statements following conditionals. Removed '=
77 * 0' after static int declarations since these default to zero. Removed
78 * list_for_each_safe() and replaced with list_for_each_entry() in
79 * hvcs_get_by_index(). The 'safe' version is un-needed now that the driver is
80 * using spinlocks. Changed spin_lock_irqsave() to spin_lock() when locking
81 * hvcs_structs_lock and hvcs_pi_lock since these are not touched in an int
82 * handler. Initialized hvcs_structs_lock and hvcs_pi_lock to
83 * SPIN_LOCK_UNLOCKED at declaration time rather than in hvcs_module_init().
84 * Added spin_lock around list_del() in destroy_hvcs_struct() to protect the
85 * list traversals from a deletion. Removed '= NULL' from pointer declaration
86 * statements since they are initialized NULL by default. Removed wmb()
87 * instances from hvcs_try_write(). They probably aren't needed with locking in
88 * place. Added check and cleanup for hvcs_pi_buff = kmalloc() in
89 * hvcs_module_init(). Exposed hvcs_struct.index via a sysfs attribute so that
90 * the coupling between /dev/hvcs* and a vty-server can be automatically
91 * determined. Moved kobject_put() in hvcs_open outside of the
92 * spin_unlock_irqrestore().
94 * 1.3.1 -> 1.3.2 Changed method for determining hvcs_struct->index and had it
95 * align with how the tty layer always assigns the lowest index available. This
96 * change resulted in a list of ints that denotes which indexes are available.
97 * Device additions and removals use the new hvcs_get_index() and
98 * hvcs_return_index() helper functions. The list is created with
99 * hvsc_alloc_index_list() and it is destroyed with hvcs_free_index_list().
100 * Without these fixes hotplug vty-server adapter support goes crazy with this
101 * driver if the user removes a vty-server adapter. Moved free_irq() outside of
102 * the hvcs_final_close() function in order to get it out of the spinlock.
103 * Rearranged hvcs_close(). Cleaned up some printks and did some housekeeping
104 * on the changelog. Removed local CLC_LENGTH and used HVCS_CLC_LENGTH from
105 * arch/powerepc/include/asm/hvcserver.h
107 * 1.3.2 -> 1.3.3 Replaced yield() in hvcs_close() with tty_wait_until_sent() to
108 * prevent possible lockup with realtime scheduling as similarly pointed out by
109 * akpm in hvc_console. Changed resulted in the removal of hvcs_final_close()
110 * to reorder cleanup operations and prevent discarding of pending data during
111 * an hvcs_close(). Removed spinlock protection of hvcs_struct data members in
112 * hvcs_write_room() and hvcs_chars_in_buffer() because they aren't needed.
115 #define HVCS_DRIVER_VERSION "1.3.3"
118 MODULE_DESCRIPTION("IBM hvcs (Hypervisor Virtual Console Server) Driver");
119 MODULE_LICENSE("GPL");
120 MODULE_VERSION(HVCS_DRIVER_VERSION);
123 * Wait this long per iteration while trying to push buffered data to the
124 * hypervisor before allowing the tty to complete a close operation.
126 #define HVCS_CLOSE_WAIT (HZ/100) /* 1/10 of a second */
129 * Since the Linux TTY code does not currently (2-04-2004) support dynamic
130 * addition of tty derived devices and we shouldn't allocate thousands of
131 * tty_device pointers when the number of vty-server & vty partner connections
132 * will most often be much lower than this, we'll arbitrarily allocate
133 * HVCS_DEFAULT_SERVER_ADAPTERS tty_structs and cdev's by default when we
134 * register the tty_driver. This can be overridden using an insmod parameter.
136 #define HVCS_DEFAULT_SERVER_ADAPTERS 64
139 * The user can't insmod with more than HVCS_MAX_SERVER_ADAPTERS hvcs device
140 * nodes as a sanity check. Theoretically there can be over 1 Billion
141 * vty-server & vty partner connections.
143 #define HVCS_MAX_SERVER_ADAPTERS 1024
146 * We let Linux assign us a major number and we start the minors at zero. There
147 * is no intuitive mapping between minor number and the target vty-server
148 * adapter except that each new vty-server adapter is always assigned to the
149 * smallest minor number available.
151 #define HVCS_MINOR_START 0
154 * The hcall interface involves putting 8 chars into each of two registers.
155 * We load up those 2 registers (in arch/powerpc/platforms/pseries/hvconsole.c)
156 * by casting char[16] to long[2]. It would work without __ALIGNED__, but a
157 * little (tiny) bit slower because an unaligned load is slower than aligned
160 #define __ALIGNED__ __attribute__((__aligned__(8)))
163 * How much data can firmware send with each hvc_put_chars()? Maybe this
164 * should be moved into an architecture specific area.
166 #define HVCS_BUFF_LEN 16
169 * This is the maximum amount of data we'll let the user send us (hvcs_write) at
170 * once in a chunk as a sanity check.
172 #define HVCS_MAX_FROM_USER 4096
175 * Be careful when adding flags to this line discipline. Don't add anything
176 * that will cause echoing or we'll go into recursive loop echoing chars back
177 * and forth with the console drivers.
179 static const struct ktermios hvcs_tty_termios = {
180 .c_iflag = IGNBRK | IGNPAR,
182 .c_cflag = B38400 | CS8 | CREAD | HUPCL,
189 * This value is used to take the place of a command line parameter when the
190 * module is inserted. It starts as -1 and stays as such if the user doesn't
191 * specify a module insmod parameter. If they DO specify one then it is set to
192 * the value of the integer passed in.
194 static int hvcs_parm_num_devs = -1;
195 module_param(hvcs_parm_num_devs, int, 0);
197 static const char hvcs_driver_name[] = "hvcs";
198 static const char hvcs_device_node[] = "hvcs";
200 /* Status of partner info rescan triggered via sysfs. */
201 static int hvcs_rescan_status;
203 static struct tty_driver *hvcs_tty_driver;
206 * In order to be somewhat sane this driver always associates the hvcs_struct
207 * index element with the numerically equal tty->index. This means that a
208 * hotplugged vty-server adapter will always map to the lowest index valued
209 * device node. If vty-servers were hotplug removed from the system and then
210 * new ones added the new vty-server may have the largest slot number of all
211 * the vty-server adapters in the partition but it may have the lowest dev node
212 * index of all the adapters due to the hole left by the hotplug removed
213 * adapter. There are a set of functions provided to get the lowest index for
214 * a new device as well as return the index to the list. This list is allocated
215 * with a number of elements equal to the number of device nodes requested when
216 * the module was inserted.
218 static int *hvcs_index_list;
221 * How large is the list? This is kept for traversal since the list is
222 * dynamically created.
224 static int hvcs_index_count;
227 * Used by the khvcsd to pick up I/O operations when the kernel_thread is
228 * already awake but potentially shifted to TASK_INTERRUPTIBLE state.
230 static int hvcs_kicked;
233 * Use by the kthread construct for task operations like waking the sleeping
234 * thread and stopping the kthread.
236 static struct task_struct *hvcs_task;
239 * We allocate this for the use of all of the hvcs_structs when they fetch
242 static unsigned long *hvcs_pi_buff;
244 /* Only allow one hvcs_struct to use the hvcs_pi_buff at a time. */
245 static DEFINE_SPINLOCK(hvcs_pi_lock);
247 /* One vty-server per hvcs_struct */
249 struct tty_port port;
253 * This index identifies this hvcs device as the complement to a
254 * specific tty index.
259 * Used to tell the driver kernel_thread what operations need to take
260 * place upon this hvcs_struct instance.
265 * This buffer is required so that when hvcs_write_room() reports that
266 * it can send HVCS_BUFF_LEN characters that it will buffer the full
267 * HVCS_BUFF_LEN characters if need be. This is essential for opost
268 * writes since they do not do high level buffering and expect to be
269 * able to send what the driver commits to sending buffering
270 * [e.g. tab to space conversions in n_tty.c opost()].
272 char buffer[HVCS_BUFF_LEN];
276 * Any variable below is valid before a tty is connected and
277 * stays valid after the tty is disconnected. These shouldn't be
278 * whacked until the kobject refcount reaches zero though some entries
279 * may be changed via sysfs initiatives.
281 int connected; /* is the vty-server currently connected to a vty? */
282 uint32_t p_unit_address; /* partner unit address */
283 uint32_t p_partition_ID; /* partner partition ID */
284 char p_location_code[HVCS_CLC_LENGTH + 1]; /* CLC + Null Term */
285 struct list_head next; /* list management */
286 struct vio_dev *vdev;
289 static LIST_HEAD(hvcs_structs);
290 static DEFINE_SPINLOCK(hvcs_structs_lock);
291 static DEFINE_MUTEX(hvcs_init_mutex);
293 static int hvcs_get_pi(struct hvcs_struct *hvcsd);
294 static int hvcs_rescan_devices_list(void);
296 static void hvcs_partner_free(struct hvcs_struct *hvcsd);
298 static int hvcs_initialize(void);
300 #define HVCS_SCHED_READ 0x00000001
301 #define HVCS_QUICK_READ 0x00000002
302 #define HVCS_TRY_WRITE 0x00000004
303 #define HVCS_READ_MASK (HVCS_SCHED_READ | HVCS_QUICK_READ)
305 static inline struct hvcs_struct *from_vio_dev(struct vio_dev *viod)
307 return dev_get_drvdata(&viod->dev);
309 /* The sysfs interface for the driver and devices */
311 static ssize_t hvcs_partner_vtys_show(struct device *dev, struct device_attribute *attr, char *buf)
313 struct vio_dev *viod = to_vio_dev(dev);
314 struct hvcs_struct *hvcsd = from_vio_dev(viod);
318 spin_lock_irqsave(&hvcsd->lock, flags);
319 retval = sprintf(buf, "%X\n", hvcsd->p_unit_address);
320 spin_unlock_irqrestore(&hvcsd->lock, flags);
323 static DEVICE_ATTR(partner_vtys, S_IRUGO, hvcs_partner_vtys_show, NULL);
325 static ssize_t hvcs_partner_clcs_show(struct device *dev, struct device_attribute *attr, char *buf)
327 struct vio_dev *viod = to_vio_dev(dev);
328 struct hvcs_struct *hvcsd = from_vio_dev(viod);
332 spin_lock_irqsave(&hvcsd->lock, flags);
333 retval = sprintf(buf, "%s\n", &hvcsd->p_location_code[0]);
334 spin_unlock_irqrestore(&hvcsd->lock, flags);
337 static DEVICE_ATTR(partner_clcs, S_IRUGO, hvcs_partner_clcs_show, NULL);
339 static ssize_t hvcs_current_vty_store(struct device *dev, struct device_attribute *attr, const char * buf,
343 * Don't need this feature at the present time because firmware doesn't
344 * yet support multiple partners.
346 printk(KERN_INFO "HVCS: Denied current_vty change: -EPERM.\n");
350 static ssize_t hvcs_current_vty_show(struct device *dev, struct device_attribute *attr, char *buf)
352 struct vio_dev *viod = to_vio_dev(dev);
353 struct hvcs_struct *hvcsd = from_vio_dev(viod);
357 spin_lock_irqsave(&hvcsd->lock, flags);
358 retval = sprintf(buf, "%s\n", &hvcsd->p_location_code[0]);
359 spin_unlock_irqrestore(&hvcsd->lock, flags);
363 static DEVICE_ATTR(current_vty,
364 S_IRUGO | S_IWUSR, hvcs_current_vty_show, hvcs_current_vty_store);
366 static ssize_t hvcs_vterm_state_store(struct device *dev, struct device_attribute *attr, const char *buf,
369 struct vio_dev *viod = to_vio_dev(dev);
370 struct hvcs_struct *hvcsd = from_vio_dev(viod);
373 /* writing a '0' to this sysfs entry will result in the disconnect. */
374 if (simple_strtol(buf, NULL, 0) != 0)
377 spin_lock_irqsave(&hvcsd->lock, flags);
379 if (hvcsd->port.count > 0) {
380 spin_unlock_irqrestore(&hvcsd->lock, flags);
381 printk(KERN_INFO "HVCS: vterm state unchanged. "
382 "The hvcs device node is still in use.\n");
386 if (hvcsd->connected == 0) {
387 spin_unlock_irqrestore(&hvcsd->lock, flags);
388 printk(KERN_INFO "HVCS: vterm state unchanged. The"
389 " vty-server is not connected to a vty.\n");
393 hvcs_partner_free(hvcsd);
394 printk(KERN_INFO "HVCS: Closed vty-server@%X and"
395 " partner vty@%X:%d connection.\n",
396 hvcsd->vdev->unit_address,
397 hvcsd->p_unit_address,
398 (uint32_t)hvcsd->p_partition_ID);
400 spin_unlock_irqrestore(&hvcsd->lock, flags);
404 static ssize_t hvcs_vterm_state_show(struct device *dev, struct device_attribute *attr, char *buf)
406 struct vio_dev *viod = to_vio_dev(dev);
407 struct hvcs_struct *hvcsd = from_vio_dev(viod);
411 spin_lock_irqsave(&hvcsd->lock, flags);
412 retval = sprintf(buf, "%d\n", hvcsd->connected);
413 spin_unlock_irqrestore(&hvcsd->lock, flags);
416 static DEVICE_ATTR(vterm_state, S_IRUGO | S_IWUSR,
417 hvcs_vterm_state_show, hvcs_vterm_state_store);
419 static ssize_t hvcs_index_show(struct device *dev, struct device_attribute *attr, char *buf)
421 struct vio_dev *viod = to_vio_dev(dev);
422 struct hvcs_struct *hvcsd = from_vio_dev(viod);
426 spin_lock_irqsave(&hvcsd->lock, flags);
427 retval = sprintf(buf, "%d\n", hvcsd->index);
428 spin_unlock_irqrestore(&hvcsd->lock, flags);
432 static DEVICE_ATTR(index, S_IRUGO, hvcs_index_show, NULL);
434 static struct attribute *hvcs_attrs[] = {
435 &dev_attr_partner_vtys.attr,
436 &dev_attr_partner_clcs.attr,
437 &dev_attr_current_vty.attr,
438 &dev_attr_vterm_state.attr,
439 &dev_attr_index.attr,
443 static struct attribute_group hvcs_attr_group = {
447 static ssize_t rescan_show(struct device_driver *ddp, char *buf)
449 /* A 1 means it is updating, a 0 means it is done updating */
450 return snprintf(buf, PAGE_SIZE, "%d\n", hvcs_rescan_status);
453 static ssize_t rescan_store(struct device_driver *ddp, const char * buf,
456 if ((simple_strtol(buf, NULL, 0) != 1)
457 && (hvcs_rescan_status != 0))
460 hvcs_rescan_status = 1;
461 printk(KERN_INFO "HVCS: rescanning partner info for all"
463 hvcs_rescan_devices_list();
464 hvcs_rescan_status = 0;
468 static DRIVER_ATTR_RW(rescan);
470 static void hvcs_kick(void)
474 wake_up_process(hvcs_task);
477 static void hvcs_unthrottle(struct tty_struct *tty)
479 struct hvcs_struct *hvcsd = tty->driver_data;
482 spin_lock_irqsave(&hvcsd->lock, flags);
483 hvcsd->todo_mask |= HVCS_SCHED_READ;
484 spin_unlock_irqrestore(&hvcsd->lock, flags);
488 static void hvcs_throttle(struct tty_struct *tty)
490 struct hvcs_struct *hvcsd = tty->driver_data;
493 spin_lock_irqsave(&hvcsd->lock, flags);
494 vio_disable_interrupts(hvcsd->vdev);
495 spin_unlock_irqrestore(&hvcsd->lock, flags);
499 * If the device is being removed we don't have to worry about this interrupt
500 * handler taking any further interrupts because they are disabled which means
501 * the hvcs_struct will always be valid in this handler.
503 static irqreturn_t hvcs_handle_interrupt(int irq, void *dev_instance)
505 struct hvcs_struct *hvcsd = dev_instance;
507 spin_lock(&hvcsd->lock);
508 vio_disable_interrupts(hvcsd->vdev);
509 hvcsd->todo_mask |= HVCS_SCHED_READ;
510 spin_unlock(&hvcsd->lock);
516 /* This function must be called with the hvcsd->lock held */
517 static void hvcs_try_write(struct hvcs_struct *hvcsd)
519 uint32_t unit_address = hvcsd->vdev->unit_address;
520 struct tty_struct *tty = hvcsd->port.tty;
523 if (hvcsd->todo_mask & HVCS_TRY_WRITE) {
524 /* won't send partial writes */
525 sent = hvc_put_chars(unit_address,
527 hvcsd->chars_in_buffer );
529 hvcsd->chars_in_buffer = 0;
531 hvcsd->todo_mask &= ~(HVCS_TRY_WRITE);
535 * We are still obligated to deliver the data to the
536 * hypervisor even if the tty has been closed because
537 * we committed to delivering it. But don't try to wake
538 * a non-existent tty.
547 static int hvcs_io(struct hvcs_struct *hvcsd)
549 uint32_t unit_address;
550 struct tty_struct *tty;
551 char buf[HVCS_BUFF_LEN] __ALIGNED__;
555 spin_lock_irqsave(&hvcsd->lock, flags);
557 unit_address = hvcsd->vdev->unit_address;
558 tty = hvcsd->port.tty;
560 hvcs_try_write(hvcsd);
562 if (!tty || tty_throttled(tty)) {
563 hvcsd->todo_mask &= ~(HVCS_READ_MASK);
565 } else if (!(hvcsd->todo_mask & (HVCS_READ_MASK)))
568 /* remove the read masks */
569 hvcsd->todo_mask &= ~(HVCS_READ_MASK);
571 if (tty_buffer_request_room(&hvcsd->port, HVCS_BUFF_LEN) >= HVCS_BUFF_LEN) {
572 got = hvc_get_chars(unit_address,
575 tty_insert_flip_string(&hvcsd->port, buf, got);
578 /* Give the TTY time to process the data we just sent. */
580 hvcsd->todo_mask |= HVCS_QUICK_READ;
582 spin_unlock_irqrestore(&hvcsd->lock, flags);
583 /* This is synch -- FIXME :js: it is not! */
585 tty_flip_buffer_push(&hvcsd->port);
588 /* Do this _after_ the flip_buffer_push */
589 spin_lock_irqsave(&hvcsd->lock, flags);
590 vio_enable_interrupts(hvcsd->vdev);
591 spin_unlock_irqrestore(&hvcsd->lock, flags);
594 return hvcsd->todo_mask;
597 spin_unlock_irqrestore(&hvcsd->lock, flags);
598 return hvcsd->todo_mask;
601 static int khvcsd(void *unused)
603 struct hvcs_struct *hvcsd;
606 __set_current_state(TASK_RUNNING);
613 spin_lock(&hvcs_structs_lock);
614 list_for_each_entry(hvcsd, &hvcs_structs, next) {
615 hvcs_todo_mask |= hvcs_io(hvcsd);
617 spin_unlock(&hvcs_structs_lock);
620 * If any of the hvcs adapters want to try a write or quick read
621 * don't schedule(), yield a smidgen then execute the hvcs_io
622 * thread again for those that want the write.
624 if (hvcs_todo_mask & (HVCS_TRY_WRITE | HVCS_QUICK_READ)) {
629 set_current_state(TASK_INTERRUPTIBLE);
632 __set_current_state(TASK_RUNNING);
633 } while (!kthread_should_stop());
638 static const struct vio_device_id hvcs_driver_table[] = {
639 {"serial-server", "hvterm2"},
642 MODULE_DEVICE_TABLE(vio, hvcs_driver_table);
644 static void hvcs_return_index(int index)
647 if (!hvcs_index_list)
649 if (index < 0 || index >= hvcs_index_count)
651 if (hvcs_index_list[index] == -1)
654 hvcs_index_list[index] = -1;
657 static void hvcs_destruct_port(struct tty_port *p)
659 struct hvcs_struct *hvcsd = container_of(p, struct hvcs_struct, port);
660 struct vio_dev *vdev;
663 spin_lock(&hvcs_structs_lock);
664 spin_lock_irqsave(&hvcsd->lock, flags);
666 /* the list_del poisons the pointers */
667 list_del(&(hvcsd->next));
669 if (hvcsd->connected == 1) {
670 hvcs_partner_free(hvcsd);
671 printk(KERN_INFO "HVCS: Closed vty-server@%X and"
672 " partner vty@%X:%d connection.\n",
673 hvcsd->vdev->unit_address,
674 hvcsd->p_unit_address,
675 (uint32_t)hvcsd->p_partition_ID);
677 printk(KERN_INFO "HVCS: Destroyed hvcs_struct for vty-server@%X.\n",
678 hvcsd->vdev->unit_address);
683 hvcsd->p_unit_address = 0;
684 hvcsd->p_partition_ID = 0;
685 hvcs_return_index(hvcsd->index);
686 memset(&hvcsd->p_location_code[0], 0x00, HVCS_CLC_LENGTH + 1);
688 spin_unlock_irqrestore(&hvcsd->lock, flags);
689 spin_unlock(&hvcs_structs_lock);
691 sysfs_remove_group(&vdev->dev.kobj, &hvcs_attr_group);
696 static const struct tty_port_operations hvcs_port_ops = {
697 .destruct = hvcs_destruct_port,
700 static int hvcs_get_index(void)
704 if (!hvcs_index_list) {
705 printk(KERN_ERR "HVCS: hvcs_index_list NOT valid!.\n");
708 /* Find the numerically lowest first free index. */
709 for(i = 0; i < hvcs_index_count; i++) {
710 if (hvcs_index_list[i] == -1) {
711 hvcs_index_list[i] = 0;
718 static int hvcs_probe(
720 const struct vio_device_id *id)
722 struct hvcs_struct *hvcsd;
727 printk(KERN_ERR "HVCS: probed with invalid parameter.\n");
731 /* Make sure we are properly initialized */
732 rc = hvcs_initialize();
734 pr_err("HVCS: Failed to initialize core driver.\n");
738 /* early to avoid cleanup on failure */
739 index = hvcs_get_index();
744 hvcsd = kzalloc(sizeof(*hvcsd), GFP_KERNEL);
748 tty_port_init(&hvcsd->port);
749 hvcsd->port.ops = &hvcs_port_ops;
750 spin_lock_init(&hvcsd->lock);
753 dev_set_drvdata(&dev->dev, hvcsd);
755 hvcsd->index = index;
757 /* hvcsd->index = ++hvcs_struct_count; */
758 hvcsd->chars_in_buffer = 0;
759 hvcsd->todo_mask = 0;
760 hvcsd->connected = 0;
763 * This will populate the hvcs_struct's partner info fields for the
766 if (hvcs_get_pi(hvcsd)) {
767 printk(KERN_ERR "HVCS: Failed to fetch partner"
768 " info for vty-server@%X on device probe.\n",
769 hvcsd->vdev->unit_address);
773 * If a user app opens a tty that corresponds to this vty-server before
774 * the hvcs_struct has been added to the devices list then the user app
777 spin_lock(&hvcs_structs_lock);
778 list_add_tail(&(hvcsd->next), &hvcs_structs);
779 spin_unlock(&hvcs_structs_lock);
781 retval = sysfs_create_group(&dev->dev.kobj, &hvcs_attr_group);
783 printk(KERN_ERR "HVCS: Can't create sysfs attrs for vty-server@%X\n",
784 hvcsd->vdev->unit_address);
788 printk(KERN_INFO "HVCS: vty-server@%X added to the vio bus.\n", dev->unit_address);
791 * DON'T enable interrupts here because there is no user to receive the
797 static void hvcs_remove(struct vio_dev *dev)
799 struct hvcs_struct *hvcsd = dev_get_drvdata(&dev->dev);
801 struct tty_struct *tty;
803 /* By this time the vty-server won't be getting any more interrupts */
805 spin_lock_irqsave(&hvcsd->lock, flags);
807 tty = hvcsd->port.tty;
809 spin_unlock_irqrestore(&hvcsd->lock, flags);
812 * Let the last holder of this object cause it to be removed, which
813 * would probably be tty_hangup below.
815 tty_port_put(&hvcsd->port);
818 * The hangup is a scheduled function which will auto chain call
819 * hvcs_hangup. The tty should always be valid at this time unless a
820 * simultaneous tty close already cleaned up the hvcs_struct.
825 printk(KERN_INFO "HVCS: vty-server@%X removed from the"
826 " vio bus.\n", dev->unit_address);
829 static struct vio_driver hvcs_vio_driver = {
830 .id_table = hvcs_driver_table,
832 .remove = hvcs_remove,
833 .name = hvcs_driver_name,
836 /* Only called from hvcs_get_pi please */
837 static void hvcs_set_pi(struct hvcs_partner_info *pi, struct hvcs_struct *hvcsd)
839 hvcsd->p_unit_address = pi->unit_address;
840 hvcsd->p_partition_ID = pi->partition_ID;
842 /* copy the null-term char too */
843 strlcpy(hvcsd->p_location_code, pi->location_code,
844 sizeof(hvcsd->p_location_code));
848 * Traverse the list and add the partner info that is found to the hvcs_struct
849 * struct entry. NOTE: At this time I know that partner info will return a
850 * single entry but in the future there may be multiple partner info entries per
851 * vty-server and you'll want to zero out that list and reset it. If for some
852 * reason you have an old version of this driver but there IS more than one
853 * partner info then hvcsd->p_* will hold the last partner info data from the
854 * firmware query. A good way to update this code would be to replace the three
855 * partner info fields in hvcs_struct with a list of hvcs_partner_info
858 * This function must be called with the hvcsd->lock held.
860 static int hvcs_get_pi(struct hvcs_struct *hvcsd)
862 struct hvcs_partner_info *pi;
863 uint32_t unit_address = hvcsd->vdev->unit_address;
864 struct list_head head;
867 spin_lock(&hvcs_pi_lock);
869 spin_unlock(&hvcs_pi_lock);
872 retval = hvcs_get_partner_info(unit_address, &head, hvcs_pi_buff);
873 spin_unlock(&hvcs_pi_lock);
875 printk(KERN_ERR "HVCS: Failed to fetch partner"
876 " info for vty-server@%x.\n", unit_address);
880 /* nixes the values if the partner vty went away */
881 hvcsd->p_unit_address = 0;
882 hvcsd->p_partition_ID = 0;
884 list_for_each_entry(pi, &head, node)
885 hvcs_set_pi(pi, hvcsd);
887 hvcs_free_partner_info(&head);
892 * This function is executed by the driver "rescan" sysfs entry. It shouldn't
893 * be executed elsewhere, in order to prevent deadlock issues.
895 static int hvcs_rescan_devices_list(void)
897 struct hvcs_struct *hvcsd;
900 spin_lock(&hvcs_structs_lock);
902 list_for_each_entry(hvcsd, &hvcs_structs, next) {
903 spin_lock_irqsave(&hvcsd->lock, flags);
905 spin_unlock_irqrestore(&hvcsd->lock, flags);
908 spin_unlock(&hvcs_structs_lock);
914 * Farm this off into its own function because it could be more complex once
915 * multiple partners support is added. This function should be called with
916 * the hvcsd->lock held.
918 static int hvcs_has_pi(struct hvcs_struct *hvcsd)
920 if ((!hvcsd->p_unit_address) || (!hvcsd->p_partition_ID))
926 * NOTE: It is possible that the super admin removed a partner vty and then
927 * added a different vty as the new partner.
929 * This function must be called with the hvcsd->lock held.
931 static int hvcs_partner_connect(struct hvcs_struct *hvcsd)
934 unsigned int unit_address = hvcsd->vdev->unit_address;
937 * If there wasn't any pi when the device was added it doesn't meant
938 * there isn't any now. This driver isn't notified when a new partner
939 * vty is added to a vty-server so we discover changes on our own.
940 * Please see comments in hvcs_register_connection() for justification
941 * of this bizarre code.
943 retval = hvcs_register_connection(unit_address,
944 hvcsd->p_partition_ID,
945 hvcsd->p_unit_address);
947 hvcsd->connected = 1;
949 } else if (retval != -EINVAL)
953 * As per the spec re-get the pi and try again if -EINVAL after the
954 * first connection attempt.
956 if (hvcs_get_pi(hvcsd))
959 if (!hvcs_has_pi(hvcsd))
962 retval = hvcs_register_connection(unit_address,
963 hvcsd->p_partition_ID,
964 hvcsd->p_unit_address);
965 if (retval != -EINVAL) {
966 hvcsd->connected = 1;
971 * EBUSY is the most likely scenario though the vty could have been
972 * removed or there really could be an hcall error due to the parameter
973 * data but thanks to ambiguous firmware return codes we can't really
976 printk(KERN_INFO "HVCS: vty-server or partner"
977 " vty is busy. Try again later.\n");
981 /* This function must be called with the hvcsd->lock held */
982 static void hvcs_partner_free(struct hvcs_struct *hvcsd)
986 retval = hvcs_free_connection(hvcsd->vdev->unit_address);
987 } while (retval == -EBUSY);
988 hvcsd->connected = 0;
991 /* This helper function must be called WITHOUT the hvcsd->lock held */
992 static int hvcs_enable_device(struct hvcs_struct *hvcsd, uint32_t unit_address,
993 unsigned int irq, struct vio_dev *vdev)
999 * It is possible that the vty-server was removed between the time that
1000 * the conn was registered and now.
1002 rc = request_irq(irq, &hvcs_handle_interrupt, 0, "ibmhvcs", hvcsd);
1005 * It is possible the vty-server was removed after the irq was
1006 * requested but before we have time to enable interrupts.
1008 if (vio_enable_interrupts(vdev) == H_SUCCESS)
1011 printk(KERN_ERR "HVCS: int enable failed for"
1012 " vty-server@%X.\n", unit_address);
1013 free_irq(irq, hvcsd);
1016 printk(KERN_ERR "HVCS: irq req failed for"
1017 " vty-server@%X.\n", unit_address);
1019 spin_lock_irqsave(&hvcsd->lock, flags);
1020 hvcs_partner_free(hvcsd);
1021 spin_unlock_irqrestore(&hvcsd->lock, flags);
1028 * This always increments the kref ref count if the call is successful.
1029 * Please remember to dec when you are done with the instance.
1031 * NOTICE: Do NOT hold either the hvcs_struct.lock or hvcs_structs_lock when
1032 * calling this function or you will get deadlock.
1034 static struct hvcs_struct *hvcs_get_by_index(int index)
1036 struct hvcs_struct *hvcsd;
1037 unsigned long flags;
1039 spin_lock(&hvcs_structs_lock);
1040 list_for_each_entry(hvcsd, &hvcs_structs, next) {
1041 spin_lock_irqsave(&hvcsd->lock, flags);
1042 if (hvcsd->index == index) {
1043 tty_port_get(&hvcsd->port);
1044 spin_unlock_irqrestore(&hvcsd->lock, flags);
1045 spin_unlock(&hvcs_structs_lock);
1048 spin_unlock_irqrestore(&hvcsd->lock, flags);
1050 spin_unlock(&hvcs_structs_lock);
1055 static int hvcs_install(struct tty_driver *driver, struct tty_struct *tty)
1057 struct hvcs_struct *hvcsd;
1058 struct vio_dev *vdev;
1059 unsigned long unit_address, flags;
1064 * Is there a vty-server that shares the same index?
1065 * This function increments the kref index.
1067 hvcsd = hvcs_get_by_index(tty->index);
1069 printk(KERN_WARNING "HVCS: open failed, no device associated"
1070 " with tty->index %d.\n", tty->index);
1074 spin_lock_irqsave(&hvcsd->lock, flags);
1076 if (hvcsd->connected == 0) {
1077 retval = hvcs_partner_connect(hvcsd);
1079 spin_unlock_irqrestore(&hvcsd->lock, flags);
1080 printk(KERN_WARNING "HVCS: partner connect failed.\n");
1085 hvcsd->port.count = 0;
1086 hvcsd->port.tty = tty;
1087 tty->driver_data = hvcsd;
1089 memset(&hvcsd->buffer[0], 0x00, HVCS_BUFF_LEN);
1092 * Save these in the spinlock for the enable operations that need them
1093 * outside of the spinlock.
1095 irq = hvcsd->vdev->irq;
1097 unit_address = hvcsd->vdev->unit_address;
1099 hvcsd->todo_mask |= HVCS_SCHED_READ;
1100 spin_unlock_irqrestore(&hvcsd->lock, flags);
1103 * This must be done outside of the spinlock because it requests irqs
1104 * and will grab the spinlock and free the connection if it fails.
1106 retval = hvcs_enable_device(hvcsd, unit_address, irq, vdev);
1108 printk(KERN_WARNING "HVCS: enable device failed.\n");
1112 retval = tty_port_install(&hvcsd->port, driver, tty);
1118 spin_lock_irqsave(&hvcsd->lock, flags);
1119 vio_disable_interrupts(hvcsd->vdev);
1120 spin_unlock_irqrestore(&hvcsd->lock, flags);
1121 free_irq(irq, hvcsd);
1123 tty_port_put(&hvcsd->port);
1129 * This is invoked via the tty_open interface when a user app connects to the
1132 static int hvcs_open(struct tty_struct *tty, struct file *filp)
1134 struct hvcs_struct *hvcsd = tty->driver_data;
1135 unsigned long flags;
1137 spin_lock_irqsave(&hvcsd->lock, flags);
1138 hvcsd->port.count++;
1139 hvcsd->todo_mask |= HVCS_SCHED_READ;
1140 spin_unlock_irqrestore(&hvcsd->lock, flags);
1144 printk(KERN_INFO "HVCS: vty-server@%X connection opened.\n",
1145 hvcsd->vdev->unit_address );
1150 static void hvcs_close(struct tty_struct *tty, struct file *filp)
1152 struct hvcs_struct *hvcsd;
1153 unsigned long flags;
1157 * Is someone trying to close the file associated with this device after
1158 * we have hung up? If so tty->driver_data wouldn't be valid.
1160 if (tty_hung_up_p(filp))
1164 * No driver_data means that this close was probably issued after a
1165 * failed hvcs_open by the tty layer's release_dev() api and we can just
1168 if (!tty->driver_data)
1171 hvcsd = tty->driver_data;
1173 spin_lock_irqsave(&hvcsd->lock, flags);
1174 if (--hvcsd->port.count == 0) {
1176 vio_disable_interrupts(hvcsd->vdev);
1179 * NULL this early so that the kernel_thread doesn't try to
1180 * execute any operations on the TTY even though it is obligated
1181 * to deliver any pending I/O to the hypervisor.
1183 hvcsd->port.tty = NULL;
1185 irq = hvcsd->vdev->irq;
1186 spin_unlock_irqrestore(&hvcsd->lock, flags);
1188 tty_wait_until_sent(tty, HVCS_CLOSE_WAIT);
1190 free_irq(irq, hvcsd);
1192 } else if (hvcsd->port.count < 0) {
1193 printk(KERN_ERR "HVCS: vty-server@%X open_count: %d is mismanaged.\n",
1194 hvcsd->vdev->unit_address, hvcsd->port.count);
1197 spin_unlock_irqrestore(&hvcsd->lock, flags);
1200 static void hvcs_cleanup(struct tty_struct * tty)
1202 struct hvcs_struct *hvcsd = tty->driver_data;
1205 * This line is important because it tells hvcs_open that this
1206 * device needs to be re-configured the next time hvcs_open is
1209 tty->driver_data = NULL;
1211 tty_port_put(&hvcsd->port);
1214 static void hvcs_hangup(struct tty_struct * tty)
1216 struct hvcs_struct *hvcsd = tty->driver_data;
1217 unsigned long flags;
1218 int temp_open_count;
1221 spin_lock_irqsave(&hvcsd->lock, flags);
1222 /* Preserve this so that we know how many kref refs to put */
1223 temp_open_count = hvcsd->port.count;
1226 * Don't kref put inside the spinlock because the destruction
1227 * callback may use the spinlock and it may get called before the
1228 * spinlock has been released.
1230 vio_disable_interrupts(hvcsd->vdev);
1232 hvcsd->todo_mask = 0;
1234 /* I don't think the tty needs the hvcs_struct pointer after a hangup */
1235 tty->driver_data = NULL;
1236 hvcsd->port.tty = NULL;
1238 hvcsd->port.count = 0;
1240 /* This will drop any buffered data on the floor which is OK in a hangup
1242 memset(&hvcsd->buffer[0], 0x00, HVCS_BUFF_LEN);
1243 hvcsd->chars_in_buffer = 0;
1245 irq = hvcsd->vdev->irq;
1247 spin_unlock_irqrestore(&hvcsd->lock, flags);
1249 free_irq(irq, hvcsd);
1252 * We need to kref_put() for every open_count we have since the
1253 * tty_hangup() function doesn't invoke a close per open connection on a
1254 * non-console device.
1256 while(temp_open_count) {
1259 * The final put will trigger destruction of the hvcs_struct.
1260 * NOTE: If this hangup was signaled from user space then the
1261 * final put will never happen.
1263 tty_port_put(&hvcsd->port);
1268 * NOTE: This is almost always from_user since user level apps interact with the
1269 * /dev nodes. I'm trusting that if hvcs_write gets called and interrupted by
1270 * hvcs_remove (which removes the target device and executes tty_hangup()) that
1271 * tty_hangup will allow hvcs_write time to complete execution before it
1272 * terminates our device.
1274 static int hvcs_write(struct tty_struct *tty,
1275 const unsigned char *buf, int count)
1277 struct hvcs_struct *hvcsd = tty->driver_data;
1278 unsigned int unit_address;
1279 const unsigned char *charbuf;
1280 unsigned long flags;
1286 * If they don't check the return code off of their open they may
1287 * attempt this even if there is no connected device.
1292 /* Reasonable size to prevent user level flooding */
1293 if (count > HVCS_MAX_FROM_USER) {
1294 printk(KERN_WARNING "HVCS write: count being truncated to"
1295 " HVCS_MAX_FROM_USER.\n");
1296 count = HVCS_MAX_FROM_USER;
1301 spin_lock_irqsave(&hvcsd->lock, flags);
1304 * Somehow an open succeeded but the device was removed or the
1305 * connection terminated between the vty-server and partner vty during
1306 * the middle of a write operation? This is a crummy place to do this
1307 * but we want to keep it all in the spinlock.
1309 if (hvcsd->port.count <= 0) {
1310 spin_unlock_irqrestore(&hvcsd->lock, flags);
1314 unit_address = hvcsd->vdev->unit_address;
1317 tosend = min(count, (HVCS_BUFF_LEN - hvcsd->chars_in_buffer));
1319 * No more space, this probably means that the last call to
1320 * hvcs_write() didn't succeed and the buffer was filled up.
1325 memcpy(&hvcsd->buffer[hvcsd->chars_in_buffer],
1326 &charbuf[total_sent],
1329 hvcsd->chars_in_buffer += tosend;
1334 * If this is true then we don't want to try writing to the
1335 * hypervisor because that is the kernel_threads job now. We'll
1336 * just add to the buffer.
1338 if (!(hvcsd->todo_mask & HVCS_TRY_WRITE))
1339 /* won't send partial writes */
1340 result = hvc_put_chars(unit_address,
1342 hvcsd->chars_in_buffer);
1345 * Since we know we have enough room in hvcsd->buffer for
1346 * tosend we record that it was sent regardless of whether the
1347 * hypervisor actually took it because we have it buffered.
1352 hvcsd->todo_mask |= HVCS_TRY_WRITE;
1357 hvcsd->chars_in_buffer = 0;
1359 * Test after the chars_in_buffer reset otherwise this could
1360 * deadlock our writes if hvc_put_chars fails.
1366 spin_unlock_irqrestore(&hvcsd->lock, flags);
1375 * This is really asking how much can we guarantee that we can send or that we
1376 * absolutely WILL BUFFER if we can't send it. This driver MUST honor the
1377 * return value, hence the reason for hvcs_struct buffering.
1379 static unsigned int hvcs_write_room(struct tty_struct *tty)
1381 struct hvcs_struct *hvcsd = tty->driver_data;
1383 if (!hvcsd || hvcsd->port.count <= 0)
1386 return HVCS_BUFF_LEN - hvcsd->chars_in_buffer;
1389 static unsigned int hvcs_chars_in_buffer(struct tty_struct *tty)
1391 struct hvcs_struct *hvcsd = tty->driver_data;
1393 return hvcsd->chars_in_buffer;
1396 static const struct tty_operations hvcs_ops = {
1397 .install = hvcs_install,
1399 .close = hvcs_close,
1400 .cleanup = hvcs_cleanup,
1401 .hangup = hvcs_hangup,
1402 .write = hvcs_write,
1403 .write_room = hvcs_write_room,
1404 .chars_in_buffer = hvcs_chars_in_buffer,
1405 .unthrottle = hvcs_unthrottle,
1406 .throttle = hvcs_throttle,
1409 static int hvcs_alloc_index_list(int n)
1413 hvcs_index_list = kmalloc_array(n, sizeof(hvcs_index_count),
1415 if (!hvcs_index_list)
1417 hvcs_index_count = n;
1418 for (i = 0; i < hvcs_index_count; i++)
1419 hvcs_index_list[i] = -1;
1423 static void hvcs_free_index_list(void)
1425 /* Paranoia check to be thorough. */
1426 kfree(hvcs_index_list);
1427 hvcs_index_list = NULL;
1428 hvcs_index_count = 0;
1431 static int hvcs_initialize(void)
1433 int rc, num_ttys_to_alloc;
1435 mutex_lock(&hvcs_init_mutex);
1437 mutex_unlock(&hvcs_init_mutex);
1441 /* Has the user specified an overload with an insmod param? */
1442 if (hvcs_parm_num_devs <= 0 ||
1443 (hvcs_parm_num_devs > HVCS_MAX_SERVER_ADAPTERS)) {
1444 num_ttys_to_alloc = HVCS_DEFAULT_SERVER_ADAPTERS;
1446 num_ttys_to_alloc = hvcs_parm_num_devs;
1448 hvcs_tty_driver = tty_alloc_driver(num_ttys_to_alloc,
1449 TTY_DRIVER_REAL_RAW);
1450 if (IS_ERR(hvcs_tty_driver)) {
1451 mutex_unlock(&hvcs_init_mutex);
1452 return PTR_ERR(hvcs_tty_driver);
1455 if (hvcs_alloc_index_list(num_ttys_to_alloc)) {
1460 hvcs_tty_driver->driver_name = hvcs_driver_name;
1461 hvcs_tty_driver->name = hvcs_device_node;
1464 * We'll let the system assign us a major number, indicated by leaving
1468 hvcs_tty_driver->minor_start = HVCS_MINOR_START;
1469 hvcs_tty_driver->type = TTY_DRIVER_TYPE_SYSTEM;
1472 * We role our own so that we DONT ECHO. We can't echo because the
1473 * device we are connecting to already echoes by default and this would
1474 * throw us into a horrible recursive echo-echo-echo loop.
1476 hvcs_tty_driver->init_termios = hvcs_tty_termios;
1478 tty_set_operations(hvcs_tty_driver, &hvcs_ops);
1481 * The following call will result in sysfs entries that denote the
1482 * dynamically assigned major and minor numbers for our devices.
1484 if (tty_register_driver(hvcs_tty_driver)) {
1485 printk(KERN_ERR "HVCS: registration as a tty driver failed.\n");
1490 hvcs_pi_buff = (unsigned long *) __get_free_page(GFP_KERNEL);
1491 if (!hvcs_pi_buff) {
1493 goto buff_alloc_fail;
1496 hvcs_task = kthread_run(khvcsd, NULL, "khvcsd");
1497 if (IS_ERR(hvcs_task)) {
1498 printk(KERN_ERR "HVCS: khvcsd creation failed.\n");
1502 mutex_unlock(&hvcs_init_mutex);
1506 free_page((unsigned long)hvcs_pi_buff);
1508 tty_unregister_driver(hvcs_tty_driver);
1510 hvcs_free_index_list();
1512 tty_driver_kref_put(hvcs_tty_driver);
1513 hvcs_tty_driver = NULL;
1514 mutex_unlock(&hvcs_init_mutex);
1518 static int __init hvcs_module_init(void)
1520 int rc = vio_register_driver(&hvcs_vio_driver);
1522 printk(KERN_ERR "HVCS: can't register vio driver\n");
1526 pr_info("HVCS: Driver registered.\n");
1528 /* This needs to be done AFTER the vio_register_driver() call or else
1529 * the kobjects won't be initialized properly.
1531 rc = driver_create_file(&(hvcs_vio_driver.driver), &driver_attr_rescan);
1533 pr_warn("HVCS: Failed to create rescan file (err %d)\n", rc);
1538 static void __exit hvcs_module_exit(void)
1541 * This driver receives hvcs_remove callbacks for each device upon
1544 vio_unregister_driver(&hvcs_vio_driver);
1549 * This synchronous operation will wake the khvcsd kthread if it is
1550 * asleep and will return when khvcsd has terminated.
1552 kthread_stop(hvcs_task);
1554 spin_lock(&hvcs_pi_lock);
1555 free_page((unsigned long)hvcs_pi_buff);
1556 hvcs_pi_buff = NULL;
1557 spin_unlock(&hvcs_pi_lock);
1559 driver_remove_file(&hvcs_vio_driver.driver, &driver_attr_rescan);
1561 tty_unregister_driver(hvcs_tty_driver);
1563 hvcs_free_index_list();
1565 tty_driver_kref_put(hvcs_tty_driver);
1567 printk(KERN_INFO "HVCS: driver module removed.\n");
1570 module_init(hvcs_module_init);
1571 module_exit(hvcs_module_exit);