linux/drivers/tty/hvc/hvcs.c
<<
>>
Prefs
   1// SPDX-License-Identifier: GPL-2.0+
   2/*
   3 * IBM eServer Hypervisor Virtual Console Server Device Driver
   4 * Copyright (C) 2003, 2004 IBM Corp.
   5 *  Ryan S. Arnold (rsa@us.ibm.com)
   6 *
   7 * Author(s) :  Ryan S. Arnold <rsa@us.ibm.com>
   8 *
   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.
  16 *
  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.
  20 *
  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
  26 * callbacks.
  27 *
  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.
  37 *
  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.
  45 *
  46 * Each vty-server, prior to being exposed to this driver is reference counted
  47 * using the 2.6 Linux kernel kref construct.
  48 *
  49 * For direction on installation and usage of this driver please reference
  50 * Documentation/powerpc/hvcs.txt.
  51 */
  52
  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>
  72#include <asm/vio.h>
  73
  74/*
  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().
  93 *
  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
 106 *
 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.
 113 */
 114
 115#define HVCS_DRIVER_VERSION "1.3.3"
 116
 117MODULE_AUTHOR("Ryan S. Arnold <rsa@us.ibm.com>");
 118MODULE_DESCRIPTION("IBM hvcs (Hypervisor Virtual Console Server) Driver");
 119MODULE_LICENSE("GPL");
 120MODULE_VERSION(HVCS_DRIVER_VERSION);
 121
 122/*
 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.
 125 */
 126#define HVCS_CLOSE_WAIT (HZ/100) /* 1/10 of a second */
 127
 128/*
 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.
 135 */
 136#define HVCS_DEFAULT_SERVER_ADAPTERS    64
 137
 138/*
 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.
 142 */
 143#define HVCS_MAX_SERVER_ADAPTERS        1024
 144
 145/*
 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.
 150 */
 151#define HVCS_MINOR_START        0
 152
 153/*
 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 
 158 * load.
 159 */
 160#define __ALIGNED__     __attribute__((__aligned__(8)))
 161
 162/*
 163 * How much data can firmware send with each hvc_put_chars()?  Maybe this
 164 * should be moved into an architecture specific area.
 165 */
 166#define HVCS_BUFF_LEN   16
 167
 168/*
 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.
 171 */
 172#define HVCS_MAX_FROM_USER      4096
 173
 174/*
 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.
 178 */
 179static const struct ktermios hvcs_tty_termios = {
 180        .c_iflag = IGNBRK | IGNPAR,
 181        .c_oflag = OPOST,
 182        .c_cflag = B38400 | CS8 | CREAD | HUPCL,
 183        .c_cc = INIT_C_CC,
 184        .c_ispeed = 38400,
 185        .c_ospeed = 38400
 186};
 187
 188/*
 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.
 193 */
 194static int hvcs_parm_num_devs = -1;
 195module_param(hvcs_parm_num_devs, int, 0);
 196
 197static const char hvcs_driver_name[] = "hvcs";
 198static const char hvcs_device_node[] = "hvcs";
 199static const char hvcs_driver_string[]
 200        = "IBM hvcs (Hypervisor Virtual Console Server) Driver";
 201
 202/* Status of partner info rescan triggered via sysfs. */
 203static int hvcs_rescan_status;
 204
 205static struct tty_driver *hvcs_tty_driver;
 206
 207/*
 208 * In order to be somewhat sane this driver always associates the hvcs_struct
 209 * index element with the numerically equal tty->index.  This means that a
 210 * hotplugged vty-server adapter will always map to the lowest index valued
 211 * device node.  If vty-servers were hotplug removed from the system and then
 212 * new ones added the new vty-server may have the largest slot number of all
 213 * the vty-server adapters in the partition but it may have the lowest dev node
 214 * index of all the adapters due to the hole left by the hotplug removed
 215 * adapter.  There are a set of functions provided to get the lowest index for
 216 * a new device as well as return the index to the list.  This list is allocated
 217 * with a number of elements equal to the number of device nodes requested when
 218 * the module was inserted.
 219 */
 220static int *hvcs_index_list;
 221
 222/*
 223 * How large is the list?  This is kept for traversal since the list is
 224 * dynamically created.
 225 */
 226static int hvcs_index_count;
 227
 228/*
 229 * Used by the khvcsd to pick up I/O operations when the kernel_thread is
 230 * already awake but potentially shifted to TASK_INTERRUPTIBLE state.
 231 */
 232static int hvcs_kicked;
 233
 234/*
 235 * Use by the kthread construct for task operations like waking the sleeping
 236 * thread and stopping the kthread.
 237 */
 238static struct task_struct *hvcs_task;
 239
 240/*
 241 * We allocate this for the use of all of the hvcs_structs when they fetch
 242 * partner info.
 243 */
 244static unsigned long *hvcs_pi_buff;
 245
 246/* Only allow one hvcs_struct to use the hvcs_pi_buff at a time. */
 247static DEFINE_SPINLOCK(hvcs_pi_lock);
 248
 249/* One vty-server per hvcs_struct */
 250struct hvcs_struct {
 251        struct tty_port port;
 252        spinlock_t lock;
 253
 254        /*
 255         * This index identifies this hvcs device as the complement to a
 256         * specific tty index.
 257         */
 258        unsigned int index;
 259
 260        /*
 261         * Used to tell the driver kernel_thread what operations need to take
 262         * place upon this hvcs_struct instance.
 263         */
 264        int todo_mask;
 265
 266        /*
 267         * This buffer is required so that when hvcs_write_room() reports that
 268         * it can send HVCS_BUFF_LEN characters that it will buffer the full
 269         * HVCS_BUFF_LEN characters if need be.  This is essential for opost
 270         * writes since they do not do high level buffering and expect to be
 271         * able to send what the driver commits to sending buffering
 272         * [e.g. tab to space conversions in n_tty.c opost()].
 273         */
 274        char buffer[HVCS_BUFF_LEN];
 275        int chars_in_buffer;
 276
 277        /*
 278         * Any variable below is valid before a tty is connected and
 279         * stays valid after the tty is disconnected.  These shouldn't be
 280         * whacked until the kobject refcount reaches zero though some entries
 281         * may be changed via sysfs initiatives.
 282         */
 283        int connected; /* is the vty-server currently connected to a vty? */
 284        uint32_t p_unit_address; /* partner unit address */
 285        uint32_t p_partition_ID; /* partner partition ID */
 286        char p_location_code[HVCS_CLC_LENGTH + 1]; /* CLC + Null Term */
 287        struct list_head next; /* list management */
 288        struct vio_dev *vdev;
 289};
 290
 291static LIST_HEAD(hvcs_structs);
 292static DEFINE_SPINLOCK(hvcs_structs_lock);
 293static DEFINE_MUTEX(hvcs_init_mutex);
 294
 295static void hvcs_unthrottle(struct tty_struct *tty);
 296static void hvcs_throttle(struct tty_struct *tty);
 297static irqreturn_t hvcs_handle_interrupt(int irq, void *dev_instance);
 298
 299static int hvcs_write(struct tty_struct *tty,
 300                const unsigned char *buf, int count);
 301static int hvcs_write_room(struct tty_struct *tty);
 302static int hvcs_chars_in_buffer(struct tty_struct *tty);
 303
 304static int hvcs_has_pi(struct hvcs_struct *hvcsd);
 305static void hvcs_set_pi(struct hvcs_partner_info *pi,
 306                struct hvcs_struct *hvcsd);
 307static int hvcs_get_pi(struct hvcs_struct *hvcsd);
 308static int hvcs_rescan_devices_list(void);
 309
 310static int hvcs_partner_connect(struct hvcs_struct *hvcsd);
 311static void hvcs_partner_free(struct hvcs_struct *hvcsd);
 312
 313static int hvcs_enable_device(struct hvcs_struct *hvcsd,
 314                uint32_t unit_address, unsigned int irq, struct vio_dev *dev);
 315
 316static int hvcs_open(struct tty_struct *tty, struct file *filp);
 317static void hvcs_close(struct tty_struct *tty, struct file *filp);
 318static void hvcs_hangup(struct tty_struct * tty);
 319
 320static int hvcs_probe(struct vio_dev *dev,
 321                const struct vio_device_id *id);
 322static int __init hvcs_module_init(void);
 323static void __exit hvcs_module_exit(void);
 324static int hvcs_initialize(void);
 325
 326#define HVCS_SCHED_READ 0x00000001
 327#define HVCS_QUICK_READ 0x00000002
 328#define HVCS_TRY_WRITE  0x00000004
 329#define HVCS_READ_MASK  (HVCS_SCHED_READ | HVCS_QUICK_READ)
 330
 331static inline struct hvcs_struct *from_vio_dev(struct vio_dev *viod)
 332{
 333        return dev_get_drvdata(&viod->dev);
 334}
 335/* The sysfs interface for the driver and devices */
 336
 337static ssize_t hvcs_partner_vtys_show(struct device *dev, struct device_attribute *attr, char *buf)
 338{
 339        struct vio_dev *viod = to_vio_dev(dev);
 340        struct hvcs_struct *hvcsd = from_vio_dev(viod);
 341        unsigned long flags;
 342        int retval;
 343
 344        spin_lock_irqsave(&hvcsd->lock, flags);
 345        retval = sprintf(buf, "%X\n", hvcsd->p_unit_address);
 346        spin_unlock_irqrestore(&hvcsd->lock, flags);
 347        return retval;
 348}
 349static DEVICE_ATTR(partner_vtys, S_IRUGO, hvcs_partner_vtys_show, NULL);
 350
 351static ssize_t hvcs_partner_clcs_show(struct device *dev, struct device_attribute *attr, char *buf)
 352{
 353        struct vio_dev *viod = to_vio_dev(dev);
 354        struct hvcs_struct *hvcsd = from_vio_dev(viod);
 355        unsigned long flags;
 356        int retval;
 357
 358        spin_lock_irqsave(&hvcsd->lock, flags);
 359        retval = sprintf(buf, "%s\n", &hvcsd->p_location_code[0]);
 360        spin_unlock_irqrestore(&hvcsd->lock, flags);
 361        return retval;
 362}
 363static DEVICE_ATTR(partner_clcs, S_IRUGO, hvcs_partner_clcs_show, NULL);
 364
 365static ssize_t hvcs_current_vty_store(struct device *dev, struct device_attribute *attr, const char * buf,
 366                size_t count)
 367{
 368        /*
 369         * Don't need this feature at the present time because firmware doesn't
 370         * yet support multiple partners.
 371         */
 372        printk(KERN_INFO "HVCS: Denied current_vty change: -EPERM.\n");
 373        return -EPERM;
 374}
 375
 376static ssize_t hvcs_current_vty_show(struct device *dev, struct device_attribute *attr, char *buf)
 377{
 378        struct vio_dev *viod = to_vio_dev(dev);
 379        struct hvcs_struct *hvcsd = from_vio_dev(viod);
 380        unsigned long flags;
 381        int retval;
 382
 383        spin_lock_irqsave(&hvcsd->lock, flags);
 384        retval = sprintf(buf, "%s\n", &hvcsd->p_location_code[0]);
 385        spin_unlock_irqrestore(&hvcsd->lock, flags);
 386        return retval;
 387}
 388
 389static DEVICE_ATTR(current_vty,
 390        S_IRUGO | S_IWUSR, hvcs_current_vty_show, hvcs_current_vty_store);
 391
 392static ssize_t hvcs_vterm_state_store(struct device *dev, struct device_attribute *attr, const char *buf,
 393                size_t count)
 394{
 395        struct vio_dev *viod = to_vio_dev(dev);
 396        struct hvcs_struct *hvcsd = from_vio_dev(viod);
 397        unsigned long flags;
 398
 399        /* writing a '0' to this sysfs entry will result in the disconnect. */
 400        if (simple_strtol(buf, NULL, 0) != 0)
 401                return -EINVAL;
 402
 403        spin_lock_irqsave(&hvcsd->lock, flags);
 404
 405        if (hvcsd->port.count > 0) {
 406                spin_unlock_irqrestore(&hvcsd->lock, flags);
 407                printk(KERN_INFO "HVCS: vterm state unchanged.  "
 408                                "The hvcs device node is still in use.\n");
 409                return -EPERM;
 410        }
 411
 412        if (hvcsd->connected == 0) {
 413                spin_unlock_irqrestore(&hvcsd->lock, flags);
 414                printk(KERN_INFO "HVCS: vterm state unchanged. The"
 415                                " vty-server is not connected to a vty.\n");
 416                return -EPERM;
 417        }
 418
 419        hvcs_partner_free(hvcsd);
 420        printk(KERN_INFO "HVCS: Closed vty-server@%X and"
 421                        " partner vty@%X:%d connection.\n",
 422                        hvcsd->vdev->unit_address,
 423                        hvcsd->p_unit_address,
 424                        (uint32_t)hvcsd->p_partition_ID);
 425
 426        spin_unlock_irqrestore(&hvcsd->lock, flags);
 427        return count;
 428}
 429
 430static ssize_t hvcs_vterm_state_show(struct device *dev, struct device_attribute *attr, char *buf)
 431{
 432        struct vio_dev *viod = to_vio_dev(dev);
 433        struct hvcs_struct *hvcsd = from_vio_dev(viod);
 434        unsigned long flags;
 435        int retval;
 436
 437        spin_lock_irqsave(&hvcsd->lock, flags);
 438        retval = sprintf(buf, "%d\n", hvcsd->connected);
 439        spin_unlock_irqrestore(&hvcsd->lock, flags);
 440        return retval;
 441}
 442static DEVICE_ATTR(vterm_state, S_IRUGO | S_IWUSR,
 443                hvcs_vterm_state_show, hvcs_vterm_state_store);
 444
 445static ssize_t hvcs_index_show(struct device *dev, struct device_attribute *attr, char *buf)
 446{
 447        struct vio_dev *viod = to_vio_dev(dev);
 448        struct hvcs_struct *hvcsd = from_vio_dev(viod);
 449        unsigned long flags;
 450        int retval;
 451
 452        spin_lock_irqsave(&hvcsd->lock, flags);
 453        retval = sprintf(buf, "%d\n", hvcsd->index);
 454        spin_unlock_irqrestore(&hvcsd->lock, flags);
 455        return retval;
 456}
 457
 458static DEVICE_ATTR(index, S_IRUGO, hvcs_index_show, NULL);
 459
 460static struct attribute *hvcs_attrs[] = {
 461        &dev_attr_partner_vtys.attr,
 462        &dev_attr_partner_clcs.attr,
 463        &dev_attr_current_vty.attr,
 464        &dev_attr_vterm_state.attr,
 465        &dev_attr_index.attr,
 466        NULL,
 467};
 468
 469static struct attribute_group hvcs_attr_group = {
 470        .attrs = hvcs_attrs,
 471};
 472
 473static ssize_t rescan_show(struct device_driver *ddp, char *buf)
 474{
 475        /* A 1 means it is updating, a 0 means it is done updating */
 476        return snprintf(buf, PAGE_SIZE, "%d\n", hvcs_rescan_status);
 477}
 478
 479static ssize_t rescan_store(struct device_driver *ddp, const char * buf,
 480                size_t count)
 481{
 482        if ((simple_strtol(buf, NULL, 0) != 1)
 483                && (hvcs_rescan_status != 0))
 484                return -EINVAL;
 485
 486        hvcs_rescan_status = 1;
 487        printk(KERN_INFO "HVCS: rescanning partner info for all"
 488                " vty-servers.\n");
 489        hvcs_rescan_devices_list();
 490        hvcs_rescan_status = 0;
 491        return count;
 492}
 493
 494static DRIVER_ATTR_RW(rescan);
 495
 496static void hvcs_kick(void)
 497{
 498        hvcs_kicked = 1;
 499        wmb();
 500        wake_up_process(hvcs_task);
 501}
 502
 503static void hvcs_unthrottle(struct tty_struct *tty)
 504{
 505        struct hvcs_struct *hvcsd = tty->driver_data;
 506        unsigned long flags;
 507
 508        spin_lock_irqsave(&hvcsd->lock, flags);
 509        hvcsd->todo_mask |= HVCS_SCHED_READ;
 510        spin_unlock_irqrestore(&hvcsd->lock, flags);
 511        hvcs_kick();
 512}
 513
 514static void hvcs_throttle(struct tty_struct *tty)
 515{
 516        struct hvcs_struct *hvcsd = tty->driver_data;
 517        unsigned long flags;
 518
 519        spin_lock_irqsave(&hvcsd->lock, flags);
 520        vio_disable_interrupts(hvcsd->vdev);
 521        spin_unlock_irqrestore(&hvcsd->lock, flags);
 522}
 523
 524/*
 525 * If the device is being removed we don't have to worry about this interrupt
 526 * handler taking any further interrupts because they are disabled which means
 527 * the hvcs_struct will always be valid in this handler.
 528 */
 529static irqreturn_t hvcs_handle_interrupt(int irq, void *dev_instance)
 530{
 531        struct hvcs_struct *hvcsd = dev_instance;
 532
 533        spin_lock(&hvcsd->lock);
 534        vio_disable_interrupts(hvcsd->vdev);
 535        hvcsd->todo_mask |= HVCS_SCHED_READ;
 536        spin_unlock(&hvcsd->lock);
 537        hvcs_kick();
 538
 539        return IRQ_HANDLED;
 540}
 541
 542/* This function must be called with the hvcsd->lock held */
 543static void hvcs_try_write(struct hvcs_struct *hvcsd)
 544{
 545        uint32_t unit_address = hvcsd->vdev->unit_address;
 546        struct tty_struct *tty = hvcsd->port.tty;
 547        int sent;
 548
 549        if (hvcsd->todo_mask & HVCS_TRY_WRITE) {
 550                /* won't send partial writes */
 551                sent = hvc_put_chars(unit_address,
 552                                &hvcsd->buffer[0],
 553                                hvcsd->chars_in_buffer );
 554                if (sent > 0) {
 555                        hvcsd->chars_in_buffer = 0;
 556                        /* wmb(); */
 557                        hvcsd->todo_mask &= ~(HVCS_TRY_WRITE);
 558                        /* wmb(); */
 559
 560                        /*
 561                         * We are still obligated to deliver the data to the
 562                         * hypervisor even if the tty has been closed because
 563                         * we committed to delivering it.  But don't try to wake
 564                         * a non-existent tty.
 565                         */
 566                        if (tty) {
 567                                tty_wakeup(tty);
 568                        }
 569                }
 570        }
 571}
 572
 573static int hvcs_io(struct hvcs_struct *hvcsd)
 574{
 575        uint32_t unit_address;
 576        struct tty_struct *tty;
 577        char buf[HVCS_BUFF_LEN] __ALIGNED__;
 578        unsigned long flags;
 579        int got = 0;
 580
 581        spin_lock_irqsave(&hvcsd->lock, flags);
 582
 583        unit_address = hvcsd->vdev->unit_address;
 584        tty = hvcsd->port.tty;
 585
 586        hvcs_try_write(hvcsd);
 587
 588        if (!tty || tty_throttled(tty)) {
 589                hvcsd->todo_mask &= ~(HVCS_READ_MASK);
 590                goto bail;
 591        } else if (!(hvcsd->todo_mask & (HVCS_READ_MASK)))
 592                goto bail;
 593
 594        /* remove the read masks */
 595        hvcsd->todo_mask &= ~(HVCS_READ_MASK);
 596
 597        if (tty_buffer_request_room(&hvcsd->port, HVCS_BUFF_LEN) >= HVCS_BUFF_LEN) {
 598                got = hvc_get_chars(unit_address,
 599                                &buf[0],
 600                                HVCS_BUFF_LEN);
 601                tty_insert_flip_string(&hvcsd->port, buf, got);
 602        }
 603
 604        /* Give the TTY time to process the data we just sent. */
 605        if (got)
 606                hvcsd->todo_mask |= HVCS_QUICK_READ;
 607
 608        spin_unlock_irqrestore(&hvcsd->lock, flags);
 609        /* This is synch because tty->low_latency == 1 */
 610        if(got)
 611                tty_flip_buffer_push(&hvcsd->port);
 612
 613        if (!got) {
 614                /* Do this _after_ the flip_buffer_push */
 615                spin_lock_irqsave(&hvcsd->lock, flags);
 616                vio_enable_interrupts(hvcsd->vdev);
 617                spin_unlock_irqrestore(&hvcsd->lock, flags);
 618        }
 619
 620        return hvcsd->todo_mask;
 621
 622 bail:
 623        spin_unlock_irqrestore(&hvcsd->lock, flags);
 624        return hvcsd->todo_mask;
 625}
 626
 627static int khvcsd(void *unused)
 628{
 629        struct hvcs_struct *hvcsd;
 630        int hvcs_todo_mask;
 631
 632        __set_current_state(TASK_RUNNING);
 633
 634        do {
 635                hvcs_todo_mask = 0;
 636                hvcs_kicked = 0;
 637                wmb();
 638
 639                spin_lock(&hvcs_structs_lock);
 640                list_for_each_entry(hvcsd, &hvcs_structs, next) {
 641                        hvcs_todo_mask |= hvcs_io(hvcsd);
 642                }
 643                spin_unlock(&hvcs_structs_lock);
 644
 645                /*
 646                 * If any of the hvcs adapters want to try a write or quick read
 647                 * don't schedule(), yield a smidgen then execute the hvcs_io
 648                 * thread again for those that want the write.
 649                 */
 650                 if (hvcs_todo_mask & (HVCS_TRY_WRITE | HVCS_QUICK_READ)) {
 651                        yield();
 652                        continue;
 653                }
 654
 655                set_current_state(TASK_INTERRUPTIBLE);
 656                if (!hvcs_kicked)
 657                        schedule();
 658                __set_current_state(TASK_RUNNING);
 659        } while (!kthread_should_stop());
 660
 661        return 0;
 662}
 663
 664static const struct vio_device_id hvcs_driver_table[] = {
 665        {"serial-server", "hvterm2"},
 666        { "", "" }
 667};
 668MODULE_DEVICE_TABLE(vio, hvcs_driver_table);
 669
 670static void hvcs_return_index(int index)
 671{
 672        /* Paranoia check */
 673        if (!hvcs_index_list)
 674                return;
 675        if (index < 0 || index >= hvcs_index_count)
 676                return;
 677        if (hvcs_index_list[index] == -1)
 678                return;
 679        else
 680                hvcs_index_list[index] = -1;
 681}
 682
 683static void hvcs_destruct_port(struct tty_port *p)
 684{
 685        struct hvcs_struct *hvcsd = container_of(p, struct hvcs_struct, port);
 686        struct vio_dev *vdev;
 687        unsigned long flags;
 688
 689        spin_lock(&hvcs_structs_lock);
 690        spin_lock_irqsave(&hvcsd->lock, flags);
 691
 692        /* the list_del poisons the pointers */
 693        list_del(&(hvcsd->next));
 694
 695        if (hvcsd->connected == 1) {
 696                hvcs_partner_free(hvcsd);
 697                printk(KERN_INFO "HVCS: Closed vty-server@%X and"
 698                                " partner vty@%X:%d connection.\n",
 699                                hvcsd->vdev->unit_address,
 700                                hvcsd->p_unit_address,
 701                                (uint32_t)hvcsd->p_partition_ID);
 702        }
 703        printk(KERN_INFO "HVCS: Destroyed hvcs_struct for vty-server@%X.\n",
 704                        hvcsd->vdev->unit_address);
 705
 706        vdev = hvcsd->vdev;
 707        hvcsd->vdev = NULL;
 708
 709        hvcsd->p_unit_address = 0;
 710        hvcsd->p_partition_ID = 0;
 711        hvcs_return_index(hvcsd->index);
 712        memset(&hvcsd->p_location_code[0], 0x00, HVCS_CLC_LENGTH + 1);
 713
 714        spin_unlock_irqrestore(&hvcsd->lock, flags);
 715        spin_unlock(&hvcs_structs_lock);
 716
 717        sysfs_remove_group(&vdev->dev.kobj, &hvcs_attr_group);
 718
 719        kfree(hvcsd);
 720}
 721
 722static const struct tty_port_operations hvcs_port_ops = {
 723        .destruct = hvcs_destruct_port,
 724};
 725
 726static int hvcs_get_index(void)
 727{
 728        int i;
 729        /* Paranoia check */
 730        if (!hvcs_index_list) {
 731                printk(KERN_ERR "HVCS: hvcs_index_list NOT valid!.\n");
 732                return -EFAULT;
 733        }
 734        /* Find the numerically lowest first free index. */
 735        for(i = 0; i < hvcs_index_count; i++) {
 736                if (hvcs_index_list[i] == -1) {
 737                        hvcs_index_list[i] = 0;
 738                        return i;
 739                }
 740        }
 741        return -1;
 742}
 743
 744static int hvcs_probe(
 745        struct vio_dev *dev,
 746        const struct vio_device_id *id)
 747{
 748        struct hvcs_struct *hvcsd;
 749        int index, rc;
 750        int retval;
 751
 752        if (!dev || !id) {
 753                printk(KERN_ERR "HVCS: probed with invalid parameter.\n");
 754                return -EPERM;
 755        }
 756
 757        /* Make sure we are properly initialized */
 758        rc = hvcs_initialize();
 759        if (rc) {
 760                pr_err("HVCS: Failed to initialize core driver.\n");
 761                return rc;
 762        }
 763
 764        /* early to avoid cleanup on failure */
 765        index = hvcs_get_index();
 766        if (index < 0) {
 767                return -EFAULT;
 768        }
 769
 770        hvcsd = kzalloc(sizeof(*hvcsd), GFP_KERNEL);
 771        if (!hvcsd)
 772                return -ENODEV;
 773
 774        tty_port_init(&hvcsd->port);
 775        hvcsd->port.ops = &hvcs_port_ops;
 776        spin_lock_init(&hvcsd->lock);
 777
 778        hvcsd->vdev = dev;
 779        dev_set_drvdata(&dev->dev, hvcsd);
 780
 781        hvcsd->index = index;
 782
 783        /* hvcsd->index = ++hvcs_struct_count; */
 784        hvcsd->chars_in_buffer = 0;
 785        hvcsd->todo_mask = 0;
 786        hvcsd->connected = 0;
 787
 788        /*
 789         * This will populate the hvcs_struct's partner info fields for the
 790         * first time.
 791         */
 792        if (hvcs_get_pi(hvcsd)) {
 793                printk(KERN_ERR "HVCS: Failed to fetch partner"
 794                        " info for vty-server@%X on device probe.\n",
 795                        hvcsd->vdev->unit_address);
 796        }
 797
 798        /*
 799         * If a user app opens a tty that corresponds to this vty-server before
 800         * the hvcs_struct has been added to the devices list then the user app
 801         * will get -ENODEV.
 802         */
 803        spin_lock(&hvcs_structs_lock);
 804        list_add_tail(&(hvcsd->next), &hvcs_structs);
 805        spin_unlock(&hvcs_structs_lock);
 806
 807        retval = sysfs_create_group(&dev->dev.kobj, &hvcs_attr_group);
 808        if (retval) {
 809                printk(KERN_ERR "HVCS: Can't create sysfs attrs for vty-server@%X\n",
 810                       hvcsd->vdev->unit_address);
 811                return retval;
 812        }
 813
 814        printk(KERN_INFO "HVCS: vty-server@%X added to the vio bus.\n", dev->unit_address);
 815
 816        /*
 817         * DON'T enable interrupts here because there is no user to receive the
 818         * data.
 819         */
 820        return 0;
 821}
 822
 823static void hvcs_remove(struct vio_dev *dev)
 824{
 825        struct hvcs_struct *hvcsd = dev_get_drvdata(&dev->dev);
 826        unsigned long flags;
 827        struct tty_struct *tty;
 828
 829        /* By this time the vty-server won't be getting any more interrupts */
 830
 831        spin_lock_irqsave(&hvcsd->lock, flags);
 832
 833        tty = hvcsd->port.tty;
 834
 835        spin_unlock_irqrestore(&hvcsd->lock, flags);
 836
 837        /*
 838         * Let the last holder of this object cause it to be removed, which
 839         * would probably be tty_hangup below.
 840         */
 841        tty_port_put(&hvcsd->port);
 842
 843        /*
 844         * The hangup is a scheduled function which will auto chain call
 845         * hvcs_hangup.  The tty should always be valid at this time unless a
 846         * simultaneous tty close already cleaned up the hvcs_struct.
 847         */
 848        if (tty)
 849                tty_hangup(tty);
 850
 851        printk(KERN_INFO "HVCS: vty-server@%X removed from the"
 852                        " vio bus.\n", dev->unit_address);
 853};
 854
 855static struct vio_driver hvcs_vio_driver = {
 856        .id_table       = hvcs_driver_table,
 857        .probe          = hvcs_probe,
 858        .remove         = hvcs_remove,
 859        .name           = hvcs_driver_name,
 860};
 861
 862/* Only called from hvcs_get_pi please */
 863static void hvcs_set_pi(struct hvcs_partner_info *pi, struct hvcs_struct *hvcsd)
 864{
 865        hvcsd->p_unit_address = pi->unit_address;
 866        hvcsd->p_partition_ID  = pi->partition_ID;
 867
 868        /* copy the null-term char too */
 869        strlcpy(&hvcsd->p_location_code[0],
 870                        &pi->location_code[0], sizeof(hvcsd->p_location_code));
 871}
 872
 873/*
 874 * Traverse the list and add the partner info that is found to the hvcs_struct
 875 * struct entry. NOTE: At this time I know that partner info will return a
 876 * single entry but in the future there may be multiple partner info entries per
 877 * vty-server and you'll want to zero out that list and reset it.  If for some
 878 * reason you have an old version of this driver but there IS more than one
 879 * partner info then hvcsd->p_* will hold the last partner info data from the
 880 * firmware query.  A good way to update this code would be to replace the three
 881 * partner info fields in hvcs_struct with a list of hvcs_partner_info
 882 * instances.
 883 *
 884 * This function must be called with the hvcsd->lock held.
 885 */
 886static int hvcs_get_pi(struct hvcs_struct *hvcsd)
 887{
 888        struct hvcs_partner_info *pi;
 889        uint32_t unit_address = hvcsd->vdev->unit_address;
 890        struct list_head head;
 891        int retval;
 892
 893        spin_lock(&hvcs_pi_lock);
 894        if (!hvcs_pi_buff) {
 895                spin_unlock(&hvcs_pi_lock);
 896                return -EFAULT;
 897        }
 898        retval = hvcs_get_partner_info(unit_address, &head, hvcs_pi_buff);
 899        spin_unlock(&hvcs_pi_lock);
 900        if (retval) {
 901                printk(KERN_ERR "HVCS: Failed to fetch partner"
 902                        " info for vty-server@%x.\n", unit_address);
 903                return retval;
 904        }
 905
 906        /* nixes the values if the partner vty went away */
 907        hvcsd->p_unit_address = 0;
 908        hvcsd->p_partition_ID = 0;
 909
 910        list_for_each_entry(pi, &head, node)
 911                hvcs_set_pi(pi, hvcsd);
 912
 913        hvcs_free_partner_info(&head);
 914        return 0;
 915}
 916
 917/*
 918 * This function is executed by the driver "rescan" sysfs entry.  It shouldn't
 919 * be executed elsewhere, in order to prevent deadlock issues.
 920 */
 921static int hvcs_rescan_devices_list(void)
 922{
 923        struct hvcs_struct *hvcsd;
 924        unsigned long flags;
 925
 926        spin_lock(&hvcs_structs_lock);
 927
 928        list_for_each_entry(hvcsd, &hvcs_structs, next) {
 929                spin_lock_irqsave(&hvcsd->lock, flags);
 930                hvcs_get_pi(hvcsd);
 931                spin_unlock_irqrestore(&hvcsd->lock, flags);
 932        }
 933
 934        spin_unlock(&hvcs_structs_lock);
 935
 936        return 0;
 937}
 938
 939/*
 940 * Farm this off into its own function because it could be more complex once
 941 * multiple partners support is added. This function should be called with
 942 * the hvcsd->lock held.
 943 */
 944static int hvcs_has_pi(struct hvcs_struct *hvcsd)
 945{
 946        if ((!hvcsd->p_unit_address) || (!hvcsd->p_partition_ID))
 947                return 0;
 948        return 1;
 949}
 950
 951/*
 952 * NOTE: It is possible that the super admin removed a partner vty and then
 953 * added a different vty as the new partner.
 954 *
 955 * This function must be called with the hvcsd->lock held.
 956 */
 957static int hvcs_partner_connect(struct hvcs_struct *hvcsd)
 958{
 959        int retval;
 960        unsigned int unit_address = hvcsd->vdev->unit_address;
 961
 962        /*
 963         * If there wasn't any pi when the device was added it doesn't meant
 964         * there isn't any now.  This driver isn't notified when a new partner
 965         * vty is added to a vty-server so we discover changes on our own.
 966         * Please see comments in hvcs_register_connection() for justification
 967         * of this bizarre code.
 968         */
 969        retval = hvcs_register_connection(unit_address,
 970                        hvcsd->p_partition_ID,
 971                        hvcsd->p_unit_address);
 972        if (!retval) {
 973                hvcsd->connected = 1;
 974                return 0;
 975        } else if (retval != -EINVAL)
 976                return retval;
 977
 978        /*
 979         * As per the spec re-get the pi and try again if -EINVAL after the
 980         * first connection attempt.
 981         */
 982        if (hvcs_get_pi(hvcsd))
 983                return -ENOMEM;
 984
 985        if (!hvcs_has_pi(hvcsd))
 986                return -ENODEV;
 987
 988        retval = hvcs_register_connection(unit_address,
 989                        hvcsd->p_partition_ID,
 990                        hvcsd->p_unit_address);
 991        if (retval != -EINVAL) {
 992                hvcsd->connected = 1;
 993                return retval;
 994        }
 995
 996        /*
 997         * EBUSY is the most likely scenario though the vty could have been
 998         * removed or there really could be an hcall error due to the parameter
 999         * data but thanks to ambiguous firmware return codes we can't really
1000         * tell.
1001         */
1002        printk(KERN_INFO "HVCS: vty-server or partner"
1003                        " vty is busy.  Try again later.\n");
1004        return -EBUSY;
1005}
1006
1007/* This function must be called with the hvcsd->lock held */
1008static void hvcs_partner_free(struct hvcs_struct *hvcsd)
1009{
1010        int retval;
1011        do {
1012                retval = hvcs_free_connection(hvcsd->vdev->unit_address);
1013        } while (retval == -EBUSY);
1014        hvcsd->connected = 0;
1015}
1016
1017/* This helper function must be called WITHOUT the hvcsd->lock held */
1018static int hvcs_enable_device(struct hvcs_struct *hvcsd, uint32_t unit_address,
1019                unsigned int irq, struct vio_dev *vdev)
1020{
1021        unsigned long flags;
1022        int rc;
1023
1024        /*
1025         * It is possible that the vty-server was removed between the time that
1026         * the conn was registered and now.
1027         */
1028        rc = request_irq(irq, &hvcs_handle_interrupt, 0, "ibmhvcs", hvcsd);
1029        if (!rc) {
1030                /*
1031                 * It is possible the vty-server was removed after the irq was
1032                 * requested but before we have time to enable interrupts.
1033                 */
1034                if (vio_enable_interrupts(vdev) == H_SUCCESS)
1035                        return 0;
1036                else {
1037                        printk(KERN_ERR "HVCS: int enable failed for"
1038                                        " vty-server@%X.\n", unit_address);
1039                        free_irq(irq, hvcsd);
1040                }
1041        } else
1042                printk(KERN_ERR "HVCS: irq req failed for"
1043                                " vty-server@%X.\n", unit_address);
1044
1045        spin_lock_irqsave(&hvcsd->lock, flags);
1046        hvcs_partner_free(hvcsd);
1047        spin_unlock_irqrestore(&hvcsd->lock, flags);
1048
1049        return rc;
1050
1051}
1052
1053/*
1054 * This always increments the kref ref count if the call is successful.
1055 * Please remember to dec when you are done with the instance.
1056 *
1057 * NOTICE: Do NOT hold either the hvcs_struct.lock or hvcs_structs_lock when
1058 * calling this function or you will get deadlock.
1059 */
1060static struct hvcs_struct *hvcs_get_by_index(int index)
1061{
1062        struct hvcs_struct *hvcsd;
1063        unsigned long flags;
1064
1065        spin_lock(&hvcs_structs_lock);
1066        list_for_each_entry(hvcsd, &hvcs_structs, next) {
1067                spin_lock_irqsave(&hvcsd->lock, flags);
1068                if (hvcsd->index == index) {
1069                        tty_port_get(&hvcsd->port);
1070                        spin_unlock_irqrestore(&hvcsd->lock, flags);
1071                        spin_unlock(&hvcs_structs_lock);
1072                        return hvcsd;
1073                }
1074                spin_unlock_irqrestore(&hvcsd->lock, flags);
1075        }
1076        spin_unlock(&hvcs_structs_lock);
1077
1078        return NULL;
1079}
1080
1081static int hvcs_install(struct tty_driver *driver, struct tty_struct *tty)
1082{
1083        struct hvcs_struct *hvcsd;
1084        struct vio_dev *vdev;
1085        unsigned long unit_address, flags;
1086        unsigned int irq;
1087        int retval;
1088
1089        /*
1090         * Is there a vty-server that shares the same index?
1091         * This function increments the kref index.
1092         */
1093        hvcsd = hvcs_get_by_index(tty->index);
1094        if (!hvcsd) {
1095                printk(KERN_WARNING "HVCS: open failed, no device associated"
1096                                " with tty->index %d.\n", tty->index);
1097                return -ENODEV;
1098        }
1099
1100        spin_lock_irqsave(&hvcsd->lock, flags);
1101
1102        if (hvcsd->connected == 0) {
1103                retval = hvcs_partner_connect(hvcsd);
1104                if (retval) {
1105                        spin_unlock_irqrestore(&hvcsd->lock, flags);
1106                        printk(KERN_WARNING "HVCS: partner connect failed.\n");
1107                        goto err_put;
1108                }
1109        }
1110
1111        hvcsd->port.count = 0;
1112        hvcsd->port.tty = tty;
1113        tty->driver_data = hvcsd;
1114
1115        memset(&hvcsd->buffer[0], 0x00, HVCS_BUFF_LEN);
1116
1117        /*
1118         * Save these in the spinlock for the enable operations that need them
1119         * outside of the spinlock.
1120         */
1121        irq = hvcsd->vdev->irq;
1122        vdev = hvcsd->vdev;
1123        unit_address = hvcsd->vdev->unit_address;
1124
1125        hvcsd->todo_mask |= HVCS_SCHED_READ;
1126        spin_unlock_irqrestore(&hvcsd->lock, flags);
1127
1128        /*
1129         * This must be done outside of the spinlock because it requests irqs
1130         * and will grab the spinlock and free the connection if it fails.
1131         */
1132        retval = hvcs_enable_device(hvcsd, unit_address, irq, vdev);
1133        if (retval) {
1134                printk(KERN_WARNING "HVCS: enable device failed.\n");
1135                goto err_put;
1136        }
1137
1138        retval = tty_port_install(&hvcsd->port, driver, tty);
1139        if (retval)
1140                goto err_irq;
1141
1142        return 0;
1143err_irq:
1144        spin_lock_irqsave(&hvcsd->lock, flags);
1145        vio_disable_interrupts(hvcsd->vdev);
1146        spin_unlock_irqrestore(&hvcsd->lock, flags);
1147        free_irq(irq, hvcsd);
1148err_put:
1149        tty_port_put(&hvcsd->port);
1150
1151        return retval;
1152}
1153
1154/*
1155 * This is invoked via the tty_open interface when a user app connects to the
1156 * /dev node.
1157 */
1158static int hvcs_open(struct tty_struct *tty, struct file *filp)
1159{
1160        struct hvcs_struct *hvcsd = tty->driver_data;
1161        unsigned long flags;
1162
1163        spin_lock_irqsave(&hvcsd->lock, flags);
1164        hvcsd->port.count++;
1165        hvcsd->todo_mask |= HVCS_SCHED_READ;
1166        spin_unlock_irqrestore(&hvcsd->lock, flags);
1167
1168        hvcs_kick();
1169
1170        printk(KERN_INFO "HVCS: vty-server@%X connection opened.\n",
1171                hvcsd->vdev->unit_address );
1172
1173        return 0;
1174}
1175
1176static void hvcs_close(struct tty_struct *tty, struct file *filp)
1177{
1178        struct hvcs_struct *hvcsd;
1179        unsigned long flags;
1180        int irq;
1181
1182        /*
1183         * Is someone trying to close the file associated with this device after
1184         * we have hung up?  If so tty->driver_data wouldn't be valid.
1185         */
1186        if (tty_hung_up_p(filp))
1187                return;
1188
1189        /*
1190         * No driver_data means that this close was probably issued after a
1191         * failed hvcs_open by the tty layer's release_dev() api and we can just
1192         * exit cleanly.
1193         */
1194        if (!tty->driver_data)
1195                return;
1196
1197        hvcsd = tty->driver_data;
1198
1199        spin_lock_irqsave(&hvcsd->lock, flags);
1200        if (--hvcsd->port.count == 0) {
1201
1202                vio_disable_interrupts(hvcsd->vdev);
1203
1204                /*
1205                 * NULL this early so that the kernel_thread doesn't try to
1206                 * execute any operations on the TTY even though it is obligated
1207                 * to deliver any pending I/O to the hypervisor.
1208                 */
1209                hvcsd->port.tty = NULL;
1210
1211                irq = hvcsd->vdev->irq;
1212                spin_unlock_irqrestore(&hvcsd->lock, flags);
1213
1214                tty_wait_until_sent(tty, HVCS_CLOSE_WAIT);
1215
1216                /*
1217                 * This line is important because it tells hvcs_open that this
1218                 * device needs to be re-configured the next time hvcs_open is
1219                 * called.
1220                 */
1221                tty->driver_data = NULL;
1222
1223                free_irq(irq, hvcsd);
1224                return;
1225        } else if (hvcsd->port.count < 0) {
1226                printk(KERN_ERR "HVCS: vty-server@%X open_count: %d is mismanaged.\n",
1227                hvcsd->vdev->unit_address, hvcsd->port.count);
1228        }
1229
1230        spin_unlock_irqrestore(&hvcsd->lock, flags);
1231}
1232
1233static void hvcs_cleanup(struct tty_struct * tty)
1234{
1235        struct hvcs_struct *hvcsd = tty->driver_data;
1236
1237        tty_port_put(&hvcsd->port);
1238}
1239
1240static void hvcs_hangup(struct tty_struct * tty)
1241{
1242        struct hvcs_struct *hvcsd = tty->driver_data;
1243        unsigned long flags;
1244        int temp_open_count;
1245        int irq;
1246
1247        spin_lock_irqsave(&hvcsd->lock, flags);
1248        /* Preserve this so that we know how many kref refs to put */
1249        temp_open_count = hvcsd->port.count;
1250
1251        /*
1252         * Don't kref put inside the spinlock because the destruction
1253         * callback may use the spinlock and it may get called before the
1254         * spinlock has been released.
1255         */
1256        vio_disable_interrupts(hvcsd->vdev);
1257
1258        hvcsd->todo_mask = 0;
1259
1260        /* I don't think the tty needs the hvcs_struct pointer after a hangup */
1261        tty->driver_data = NULL;
1262        hvcsd->port.tty = NULL;
1263
1264        hvcsd->port.count = 0;
1265
1266        /* This will drop any buffered data on the floor which is OK in a hangup
1267         * scenario. */
1268        memset(&hvcsd->buffer[0], 0x00, HVCS_BUFF_LEN);
1269        hvcsd->chars_in_buffer = 0;
1270
1271        irq = hvcsd->vdev->irq;
1272
1273        spin_unlock_irqrestore(&hvcsd->lock, flags);
1274
1275        free_irq(irq, hvcsd);
1276
1277        /*
1278         * We need to kref_put() for every open_count we have since the
1279         * tty_hangup() function doesn't invoke a close per open connection on a
1280         * non-console device.
1281         */
1282        while(temp_open_count) {
1283                --temp_open_count;
1284                /*
1285                 * The final put will trigger destruction of the hvcs_struct.
1286                 * NOTE:  If this hangup was signaled from user space then the
1287                 * final put will never happen.
1288                 */
1289                tty_port_put(&hvcsd->port);
1290        }
1291}
1292
1293/*
1294 * NOTE: This is almost always from_user since user level apps interact with the
1295 * /dev nodes. I'm trusting that if hvcs_write gets called and interrupted by
1296 * hvcs_remove (which removes the target device and executes tty_hangup()) that
1297 * tty_hangup will allow hvcs_write time to complete execution before it
1298 * terminates our device.
1299 */
1300static int hvcs_write(struct tty_struct *tty,
1301                const unsigned char *buf, int count)
1302{
1303        struct hvcs_struct *hvcsd = tty->driver_data;
1304        unsigned int unit_address;
1305        const unsigned char *charbuf;
1306        unsigned long flags;
1307        int total_sent = 0;
1308        int tosend = 0;
1309        int result = 0;
1310
1311        /*
1312         * If they don't check the return code off of their open they may
1313         * attempt this even if there is no connected device.
1314         */
1315        if (!hvcsd)
1316                return -ENODEV;
1317
1318        /* Reasonable size to prevent user level flooding */
1319        if (count > HVCS_MAX_FROM_USER) {
1320                printk(KERN_WARNING "HVCS write: count being truncated to"
1321                                " HVCS_MAX_FROM_USER.\n");
1322                count = HVCS_MAX_FROM_USER;
1323        }
1324
1325        charbuf = buf;
1326
1327        spin_lock_irqsave(&hvcsd->lock, flags);
1328
1329        /*
1330         * Somehow an open succeeded but the device was removed or the
1331         * connection terminated between the vty-server and partner vty during
1332         * the middle of a write operation?  This is a crummy place to do this
1333         * but we want to keep it all in the spinlock.
1334         */
1335        if (hvcsd->port.count <= 0) {
1336                spin_unlock_irqrestore(&hvcsd->lock, flags);
1337                return -ENODEV;
1338        }
1339
1340        unit_address = hvcsd->vdev->unit_address;
1341
1342        while (count > 0) {
1343                tosend = min(count, (HVCS_BUFF_LEN - hvcsd->chars_in_buffer));
1344                /*
1345                 * No more space, this probably means that the last call to
1346                 * hvcs_write() didn't succeed and the buffer was filled up.
1347                 */
1348                if (!tosend)
1349                        break;
1350
1351                memcpy(&hvcsd->buffer[hvcsd->chars_in_buffer],
1352                                &charbuf[total_sent],
1353                                tosend);
1354
1355                hvcsd->chars_in_buffer += tosend;
1356
1357                result = 0;
1358
1359                /*
1360                 * If this is true then we don't want to try writing to the
1361                 * hypervisor because that is the kernel_threads job now.  We'll
1362                 * just add to the buffer.
1363                 */
1364                if (!(hvcsd->todo_mask & HVCS_TRY_WRITE))
1365                        /* won't send partial writes */
1366                        result = hvc_put_chars(unit_address,
1367                                        &hvcsd->buffer[0],
1368                                        hvcsd->chars_in_buffer);
1369
1370                /*
1371                 * Since we know we have enough room in hvcsd->buffer for
1372                 * tosend we record that it was sent regardless of whether the
1373                 * hypervisor actually took it because we have it buffered.
1374                 */
1375                total_sent+=tosend;
1376                count-=tosend;
1377                if (result == 0) {
1378                        hvcsd->todo_mask |= HVCS_TRY_WRITE;
1379                        hvcs_kick();
1380                        break;
1381                }
1382
1383                hvcsd->chars_in_buffer = 0;
1384                /*
1385                 * Test after the chars_in_buffer reset otherwise this could
1386                 * deadlock our writes if hvc_put_chars fails.
1387                 */
1388                if (result < 0)
1389                        break;
1390        }
1391
1392        spin_unlock_irqrestore(&hvcsd->lock, flags);
1393
1394        if (result == -1)
1395                return -EIO;
1396        else
1397                return total_sent;
1398}
1399
1400/*
1401 * This is really asking how much can we guarantee that we can send or that we
1402 * absolutely WILL BUFFER if we can't send it.  This driver MUST honor the
1403 * return value, hence the reason for hvcs_struct buffering.
1404 */
1405static int hvcs_write_room(struct tty_struct *tty)
1406{
1407        struct hvcs_struct *hvcsd = tty->driver_data;
1408
1409        if (!hvcsd || hvcsd->port.count <= 0)
1410                return 0;
1411
1412        return HVCS_BUFF_LEN - hvcsd->chars_in_buffer;
1413}
1414
1415static int hvcs_chars_in_buffer(struct tty_struct *tty)
1416{
1417        struct hvcs_struct *hvcsd = tty->driver_data;
1418
1419        return hvcsd->chars_in_buffer;
1420}
1421
1422static const struct tty_operations hvcs_ops = {
1423        .install = hvcs_install,
1424        .open = hvcs_open,
1425        .close = hvcs_close,
1426        .cleanup = hvcs_cleanup,
1427        .hangup = hvcs_hangup,
1428        .write = hvcs_write,
1429        .write_room = hvcs_write_room,
1430        .chars_in_buffer = hvcs_chars_in_buffer,
1431        .unthrottle = hvcs_unthrottle,
1432        .throttle = hvcs_throttle,
1433};
1434
1435static int hvcs_alloc_index_list(int n)
1436{
1437        int i;
1438
1439        hvcs_index_list = kmalloc_array(n, sizeof(hvcs_index_count),
1440                                        GFP_KERNEL);
1441        if (!hvcs_index_list)
1442                return -ENOMEM;
1443        hvcs_index_count = n;
1444        for (i = 0; i < hvcs_index_count; i++)
1445                hvcs_index_list[i] = -1;
1446        return 0;
1447}
1448
1449static void hvcs_free_index_list(void)
1450{
1451        /* Paranoia check to be thorough. */
1452        kfree(hvcs_index_list);
1453        hvcs_index_list = NULL;
1454        hvcs_index_count = 0;
1455}
1456
1457static int hvcs_initialize(void)
1458{
1459        int rc, num_ttys_to_alloc;
1460
1461        mutex_lock(&hvcs_init_mutex);
1462        if (hvcs_task) {
1463                mutex_unlock(&hvcs_init_mutex);
1464                return 0;
1465        }
1466
1467        /* Has the user specified an overload with an insmod param? */
1468        if (hvcs_parm_num_devs <= 0 ||
1469                (hvcs_parm_num_devs > HVCS_MAX_SERVER_ADAPTERS)) {
1470                num_ttys_to_alloc = HVCS_DEFAULT_SERVER_ADAPTERS;
1471        } else
1472                num_ttys_to_alloc = hvcs_parm_num_devs;
1473
1474        hvcs_tty_driver = alloc_tty_driver(num_ttys_to_alloc);
1475        if (!hvcs_tty_driver) {
1476                mutex_unlock(&hvcs_init_mutex);
1477                return -ENOMEM;
1478        }
1479
1480        if (hvcs_alloc_index_list(num_ttys_to_alloc)) {
1481                rc = -ENOMEM;
1482                goto index_fail;
1483        }
1484
1485        hvcs_tty_driver->driver_name = hvcs_driver_name;
1486        hvcs_tty_driver->name = hvcs_device_node;
1487
1488        /*
1489         * We'll let the system assign us a major number, indicated by leaving
1490         * it blank.
1491         */
1492
1493        hvcs_tty_driver->minor_start = HVCS_MINOR_START;
1494        hvcs_tty_driver->type = TTY_DRIVER_TYPE_SYSTEM;
1495
1496        /*
1497         * We role our own so that we DONT ECHO.  We can't echo because the
1498         * device we are connecting to already echoes by default and this would
1499         * throw us into a horrible recursive echo-echo-echo loop.
1500         */
1501        hvcs_tty_driver->init_termios = hvcs_tty_termios;
1502        hvcs_tty_driver->flags = TTY_DRIVER_REAL_RAW;
1503
1504        tty_set_operations(hvcs_tty_driver, &hvcs_ops);
1505
1506        /*
1507         * The following call will result in sysfs entries that denote the
1508         * dynamically assigned major and minor numbers for our devices.
1509         */
1510        if (tty_register_driver(hvcs_tty_driver)) {
1511                printk(KERN_ERR "HVCS: registration as a tty driver failed.\n");
1512                rc = -EIO;
1513                goto register_fail;
1514        }
1515
1516        hvcs_pi_buff = (unsigned long *) __get_free_page(GFP_KERNEL);
1517        if (!hvcs_pi_buff) {
1518                rc = -ENOMEM;
1519                goto buff_alloc_fail;
1520        }
1521
1522        hvcs_task = kthread_run(khvcsd, NULL, "khvcsd");
1523        if (IS_ERR(hvcs_task)) {
1524                printk(KERN_ERR "HVCS: khvcsd creation failed.\n");
1525                rc = -EIO;
1526                goto kthread_fail;
1527        }
1528        mutex_unlock(&hvcs_init_mutex);
1529        return 0;
1530
1531kthread_fail:
1532        free_page((unsigned long)hvcs_pi_buff);
1533buff_alloc_fail:
1534        tty_unregister_driver(hvcs_tty_driver);
1535register_fail:
1536        hvcs_free_index_list();
1537index_fail:
1538        put_tty_driver(hvcs_tty_driver);
1539        hvcs_tty_driver = NULL;
1540        mutex_unlock(&hvcs_init_mutex);
1541        return rc;
1542}
1543
1544static int __init hvcs_module_init(void)
1545{
1546        int rc = vio_register_driver(&hvcs_vio_driver);
1547        if (rc) {
1548                printk(KERN_ERR "HVCS: can't register vio driver\n");
1549                return rc;
1550        }
1551
1552        pr_info("HVCS: Driver registered.\n");
1553
1554        /* This needs to be done AFTER the vio_register_driver() call or else
1555         * the kobjects won't be initialized properly.
1556         */
1557        rc = driver_create_file(&(hvcs_vio_driver.driver), &driver_attr_rescan);
1558        if (rc)
1559                pr_warn("HVCS: Failed to create rescan file (err %d)\n", rc);
1560
1561        return 0;
1562}
1563
1564static void __exit hvcs_module_exit(void)
1565{
1566        /*
1567         * This driver receives hvcs_remove callbacks for each device upon
1568         * module removal.
1569         */
1570        vio_unregister_driver(&hvcs_vio_driver);
1571        if (!hvcs_task)
1572                return;
1573
1574        /*
1575         * This synchronous operation  will wake the khvcsd kthread if it is
1576         * asleep and will return when khvcsd has terminated.
1577         */
1578        kthread_stop(hvcs_task);
1579
1580        spin_lock(&hvcs_pi_lock);
1581        free_page((unsigned long)hvcs_pi_buff);
1582        hvcs_pi_buff = NULL;
1583        spin_unlock(&hvcs_pi_lock);
1584
1585        driver_remove_file(&hvcs_vio_driver.driver, &driver_attr_rescan);
1586
1587        tty_unregister_driver(hvcs_tty_driver);
1588
1589        hvcs_free_index_list();
1590
1591        put_tty_driver(hvcs_tty_driver);
1592
1593        printk(KERN_INFO "HVCS: driver module removed.\n");
1594}
1595
1596module_init(hvcs_module_init);
1597module_exit(hvcs_module_exit);
1598