Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0+
0002 /*
0003  * IBM eServer Hypervisor Virtual Console Server Device Driver
0004  * Copyright (C) 2003, 2004 IBM Corp.
0005  *  Ryan S. Arnold (rsa@us.ibm.com)
0006  *
0007  * Author(s) :  Ryan S. Arnold <rsa@us.ibm.com>
0008  *
0009  * This is the device driver for the IBM Hypervisor Virtual Console Server,
0010  * "hvcs".  The IBM hvcs provides a tty driver interface to allow Linux
0011  * user space applications access to the system consoles of logically
0012  * partitioned operating systems, e.g. Linux, running on the same partitioned
0013  * Power5 ppc64 system.  Physical hardware consoles per partition are not
0014  * practical on this hardware so system consoles are accessed by this driver
0015  * using inter-partition firmware interfaces to virtual terminal devices.
0016  *
0017  * A vty is known to the HMC as a "virtual serial server adapter".  It is a
0018  * virtual terminal device that is created by firmware upon partition creation
0019  * to act as a partitioned OS's console device.
0020  *
0021  * Firmware dynamically (via hotplug) exposes vty-servers to a running ppc64
0022  * Linux system upon their creation by the HMC or their exposure during boot.
0023  * The non-user interactive backend of this driver is implemented as a vio
0024  * device driver so that it can receive notification of vty-server lifetimes
0025  * after it registers with the vio bus to handle vty-server probe and remove
0026  * callbacks.
0027  *
0028  * Many vty-servers can be configured to connect to one vty, but a vty can
0029  * only be actively connected to by a single vty-server, in any manner, at one
0030  * time.  If the HMC is currently hosting the console for a target Linux
0031  * partition; attempts to open the tty device to the partition's console using
0032  * the hvcs on any partition will return -EBUSY with every open attempt until
0033  * the HMC frees the connection between its vty-server and the desired
0034  * partition's vty device.  Conversely, a vty-server may only be connected to
0035  * a single vty at one time even though it may have several configured vty
0036  * partner possibilities.
0037  *
0038  * Firmware does not provide notification of vty partner changes to this
0039  * driver.  This means that an HMC Super Admin may add or remove partner vtys
0040  * from a vty-server's partner list but the changes will not be signaled to
0041  * the vty-server.  Firmware only notifies the driver when a vty-server is
0042  * added or removed from the system.  To compensate for this deficiency, this
0043  * driver implements a sysfs update attribute which provides a method for
0044  * rescanning partner information upon a user's request.
0045  *
0046  * Each vty-server, prior to being exposed to this driver is reference counted
0047  * using the 2.6 Linux kernel kref construct.
0048  *
0049  * For direction on installation and usage of this driver please reference
0050  * Documentation/powerpc/hvcs.rst.
0051  */
0052 
0053 #include <linux/device.h>
0054 #include <linux/init.h>
0055 #include <linux/interrupt.h>
0056 #include <linux/kernel.h>
0057 #include <linux/kref.h>
0058 #include <linux/kthread.h>
0059 #include <linux/list.h>
0060 #include <linux/major.h>
0061 #include <linux/module.h>
0062 #include <linux/moduleparam.h>
0063 #include <linux/sched.h>
0064 #include <linux/slab.h>
0065 #include <linux/spinlock.h>
0066 #include <linux/stat.h>
0067 #include <linux/tty.h>
0068 #include <linux/tty_flip.h>
0069 #include <asm/hvconsole.h>
0070 #include <asm/hvcserver.h>
0071 #include <linux/uaccess.h>
0072 #include <asm/vio.h>
0073 
0074 /*
0075  * 1.3.0 -> 1.3.1 In hvcs_open memset(..,0x00,..) instead of memset(..,0x3F,00).
0076  * Removed braces around single statements following conditionals.  Removed '=
0077  * 0' after static int declarations since these default to zero.  Removed
0078  * list_for_each_safe() and replaced with list_for_each_entry() in
0079  * hvcs_get_by_index().  The 'safe' version is un-needed now that the driver is
0080  * using spinlocks.  Changed spin_lock_irqsave() to spin_lock() when locking
0081  * hvcs_structs_lock and hvcs_pi_lock since these are not touched in an int
0082  * handler.  Initialized hvcs_structs_lock and hvcs_pi_lock to
0083  * SPIN_LOCK_UNLOCKED at declaration time rather than in hvcs_module_init().
0084  * Added spin_lock around list_del() in destroy_hvcs_struct() to protect the
0085  * list traversals from a deletion.  Removed '= NULL' from pointer declaration
0086  * statements since they are initialized NULL by default.  Removed wmb()
0087  * instances from hvcs_try_write().  They probably aren't needed with locking in
0088  * place.  Added check and cleanup for hvcs_pi_buff = kmalloc() in
0089  * hvcs_module_init().  Exposed hvcs_struct.index via a sysfs attribute so that
0090  * the coupling between /dev/hvcs* and a vty-server can be automatically
0091  * determined.  Moved kobject_put() in hvcs_open outside of the
0092  * spin_unlock_irqrestore().
0093  *
0094  * 1.3.1 -> 1.3.2 Changed method for determining hvcs_struct->index and had it
0095  * align with how the tty layer always assigns the lowest index available.  This
0096  * change resulted in a list of ints that denotes which indexes are available.
0097  * Device additions and removals use the new hvcs_get_index() and
0098  * hvcs_return_index() helper functions.  The list is created with
0099  * hvsc_alloc_index_list() and it is destroyed with hvcs_free_index_list().
0100  * Without these fixes hotplug vty-server adapter support goes crazy with this
0101  * driver if the user removes a vty-server adapter.  Moved free_irq() outside of
0102  * the hvcs_final_close() function in order to get it out of the spinlock.
0103  * Rearranged hvcs_close().  Cleaned up some printks and did some housekeeping
0104  * on the changelog.  Removed local CLC_LENGTH and used HVCS_CLC_LENGTH from
0105  * arch/powerepc/include/asm/hvcserver.h
0106  *
0107  * 1.3.2 -> 1.3.3 Replaced yield() in hvcs_close() with tty_wait_until_sent() to
0108  * prevent possible lockup with realtime scheduling as similarly pointed out by
0109  * akpm in hvc_console.  Changed resulted in the removal of hvcs_final_close()
0110  * to reorder cleanup operations and prevent discarding of pending data during
0111  * an hvcs_close().  Removed spinlock protection of hvcs_struct data members in
0112  * hvcs_write_room() and hvcs_chars_in_buffer() because they aren't needed.
0113  */
0114 
0115 #define HVCS_DRIVER_VERSION "1.3.3"
0116 
0117 MODULE_AUTHOR("Ryan S. Arnold <rsa@us.ibm.com>");
0118 MODULE_DESCRIPTION("IBM hvcs (Hypervisor Virtual Console Server) Driver");
0119 MODULE_LICENSE("GPL");
0120 MODULE_VERSION(HVCS_DRIVER_VERSION);
0121 
0122 /*
0123  * Wait this long per iteration while trying to push buffered data to the
0124  * hypervisor before allowing the tty to complete a close operation.
0125  */
0126 #define HVCS_CLOSE_WAIT (HZ/100) /* 1/10 of a second */
0127 
0128 /*
0129  * Since the Linux TTY code does not currently (2-04-2004) support dynamic
0130  * addition of tty derived devices and we shouldn't allocate thousands of
0131  * tty_device pointers when the number of vty-server & vty partner connections
0132  * will most often be much lower than this, we'll arbitrarily allocate
0133  * HVCS_DEFAULT_SERVER_ADAPTERS tty_structs and cdev's by default when we
0134  * register the tty_driver. This can be overridden using an insmod parameter.
0135  */
0136 #define HVCS_DEFAULT_SERVER_ADAPTERS    64
0137 
0138 /*
0139  * The user can't insmod with more than HVCS_MAX_SERVER_ADAPTERS hvcs device
0140  * nodes as a sanity check.  Theoretically there can be over 1 Billion
0141  * vty-server & vty partner connections.
0142  */
0143 #define HVCS_MAX_SERVER_ADAPTERS    1024
0144 
0145 /*
0146  * We let Linux assign us a major number and we start the minors at zero.  There
0147  * is no intuitive mapping between minor number and the target vty-server
0148  * adapter except that each new vty-server adapter is always assigned to the
0149  * smallest minor number available.
0150  */
0151 #define HVCS_MINOR_START    0
0152 
0153 /*
0154  * The hcall interface involves putting 8 chars into each of two registers.
0155  * We load up those 2 registers (in arch/powerpc/platforms/pseries/hvconsole.c)
0156  * by casting char[16] to long[2].  It would work without __ALIGNED__, but a 
0157  * little (tiny) bit slower because an unaligned load is slower than aligned 
0158  * load.
0159  */
0160 #define __ALIGNED__ __attribute__((__aligned__(8)))
0161 
0162 /*
0163  * How much data can firmware send with each hvc_put_chars()?  Maybe this
0164  * should be moved into an architecture specific area.
0165  */
0166 #define HVCS_BUFF_LEN   16
0167 
0168 /*
0169  * This is the maximum amount of data we'll let the user send us (hvcs_write) at
0170  * once in a chunk as a sanity check.
0171  */
0172 #define HVCS_MAX_FROM_USER  4096
0173 
0174 /*
0175  * Be careful when adding flags to this line discipline.  Don't add anything
0176  * that will cause echoing or we'll go into recursive loop echoing chars back
0177  * and forth with the console drivers.
0178  */
0179 static const struct ktermios hvcs_tty_termios = {
0180     .c_iflag = IGNBRK | IGNPAR,
0181     .c_oflag = OPOST,
0182     .c_cflag = B38400 | CS8 | CREAD | HUPCL,
0183     .c_cc = INIT_C_CC,
0184     .c_ispeed = 38400,
0185     .c_ospeed = 38400
0186 };
0187 
0188 /*
0189  * This value is used to take the place of a command line parameter when the
0190  * module is inserted.  It starts as -1 and stays as such if the user doesn't
0191  * specify a module insmod parameter.  If they DO specify one then it is set to
0192  * the value of the integer passed in.
0193  */
0194 static int hvcs_parm_num_devs = -1;
0195 module_param(hvcs_parm_num_devs, int, 0);
0196 
0197 static const char hvcs_driver_name[] = "hvcs";
0198 static const char hvcs_device_node[] = "hvcs";
0199 
0200 /* Status of partner info rescan triggered via sysfs. */
0201 static int hvcs_rescan_status;
0202 
0203 static struct tty_driver *hvcs_tty_driver;
0204 
0205 /*
0206  * In order to be somewhat sane this driver always associates the hvcs_struct
0207  * index element with the numerically equal tty->index.  This means that a
0208  * hotplugged vty-server adapter will always map to the lowest index valued
0209  * device node.  If vty-servers were hotplug removed from the system and then
0210  * new ones added the new vty-server may have the largest slot number of all
0211  * the vty-server adapters in the partition but it may have the lowest dev node
0212  * index of all the adapters due to the hole left by the hotplug removed
0213  * adapter.  There are a set of functions provided to get the lowest index for
0214  * a new device as well as return the index to the list.  This list is allocated
0215  * with a number of elements equal to the number of device nodes requested when
0216  * the module was inserted.
0217  */
0218 static int *hvcs_index_list;
0219 
0220 /*
0221  * How large is the list?  This is kept for traversal since the list is
0222  * dynamically created.
0223  */
0224 static int hvcs_index_count;
0225 
0226 /*
0227  * Used by the khvcsd to pick up I/O operations when the kernel_thread is
0228  * already awake but potentially shifted to TASK_INTERRUPTIBLE state.
0229  */
0230 static int hvcs_kicked;
0231 
0232 /*
0233  * Use by the kthread construct for task operations like waking the sleeping
0234  * thread and stopping the kthread.
0235  */
0236 static struct task_struct *hvcs_task;
0237 
0238 /*
0239  * We allocate this for the use of all of the hvcs_structs when they fetch
0240  * partner info.
0241  */
0242 static unsigned long *hvcs_pi_buff;
0243 
0244 /* Only allow one hvcs_struct to use the hvcs_pi_buff at a time. */
0245 static DEFINE_SPINLOCK(hvcs_pi_lock);
0246 
0247 /* One vty-server per hvcs_struct */
0248 struct hvcs_struct {
0249     struct tty_port port;
0250     spinlock_t lock;
0251 
0252     /*
0253      * This index identifies this hvcs device as the complement to a
0254      * specific tty index.
0255      */
0256     unsigned int index;
0257 
0258     /*
0259      * Used to tell the driver kernel_thread what operations need to take
0260      * place upon this hvcs_struct instance.
0261      */
0262     int todo_mask;
0263 
0264     /*
0265      * This buffer is required so that when hvcs_write_room() reports that
0266      * it can send HVCS_BUFF_LEN characters that it will buffer the full
0267      * HVCS_BUFF_LEN characters if need be.  This is essential for opost
0268      * writes since they do not do high level buffering and expect to be
0269      * able to send what the driver commits to sending buffering
0270      * [e.g. tab to space conversions in n_tty.c opost()].
0271      */
0272     char buffer[HVCS_BUFF_LEN];
0273     int chars_in_buffer;
0274 
0275     /*
0276      * Any variable below is valid before a tty is connected and
0277      * stays valid after the tty is disconnected.  These shouldn't be
0278      * whacked until the kobject refcount reaches zero though some entries
0279      * may be changed via sysfs initiatives.
0280      */
0281     int connected; /* is the vty-server currently connected to a vty? */
0282     uint32_t p_unit_address; /* partner unit address */
0283     uint32_t p_partition_ID; /* partner partition ID */
0284     char p_location_code[HVCS_CLC_LENGTH + 1]; /* CLC + Null Term */
0285     struct list_head next; /* list management */
0286     struct vio_dev *vdev;
0287 };
0288 
0289 static LIST_HEAD(hvcs_structs);
0290 static DEFINE_SPINLOCK(hvcs_structs_lock);
0291 static DEFINE_MUTEX(hvcs_init_mutex);
0292 
0293 static int hvcs_get_pi(struct hvcs_struct *hvcsd);
0294 static int hvcs_rescan_devices_list(void);
0295 
0296 static void hvcs_partner_free(struct hvcs_struct *hvcsd);
0297 
0298 static int hvcs_initialize(void);
0299 
0300 #define HVCS_SCHED_READ 0x00000001
0301 #define HVCS_QUICK_READ 0x00000002
0302 #define HVCS_TRY_WRITE  0x00000004
0303 #define HVCS_READ_MASK  (HVCS_SCHED_READ | HVCS_QUICK_READ)
0304 
0305 static inline struct hvcs_struct *from_vio_dev(struct vio_dev *viod)
0306 {
0307     return dev_get_drvdata(&viod->dev);
0308 }
0309 /* The sysfs interface for the driver and devices */
0310 
0311 static ssize_t hvcs_partner_vtys_show(struct device *dev, struct device_attribute *attr, char *buf)
0312 {
0313     struct vio_dev *viod = to_vio_dev(dev);
0314     struct hvcs_struct *hvcsd = from_vio_dev(viod);
0315     unsigned long flags;
0316     int retval;
0317 
0318     spin_lock_irqsave(&hvcsd->lock, flags);
0319     retval = sprintf(buf, "%X\n", hvcsd->p_unit_address);
0320     spin_unlock_irqrestore(&hvcsd->lock, flags);
0321     return retval;
0322 }
0323 static DEVICE_ATTR(partner_vtys, S_IRUGO, hvcs_partner_vtys_show, NULL);
0324 
0325 static ssize_t hvcs_partner_clcs_show(struct device *dev, struct device_attribute *attr, char *buf)
0326 {
0327     struct vio_dev *viod = to_vio_dev(dev);
0328     struct hvcs_struct *hvcsd = from_vio_dev(viod);
0329     unsigned long flags;
0330     int retval;
0331 
0332     spin_lock_irqsave(&hvcsd->lock, flags);
0333     retval = sprintf(buf, "%s\n", &hvcsd->p_location_code[0]);
0334     spin_unlock_irqrestore(&hvcsd->lock, flags);
0335     return retval;
0336 }
0337 static DEVICE_ATTR(partner_clcs, S_IRUGO, hvcs_partner_clcs_show, NULL);
0338 
0339 static ssize_t hvcs_current_vty_store(struct device *dev, struct device_attribute *attr, const char * buf,
0340         size_t count)
0341 {
0342     /*
0343      * Don't need this feature at the present time because firmware doesn't
0344      * yet support multiple partners.
0345      */
0346     printk(KERN_INFO "HVCS: Denied current_vty change: -EPERM.\n");
0347     return -EPERM;
0348 }
0349 
0350 static ssize_t hvcs_current_vty_show(struct device *dev, struct device_attribute *attr, char *buf)
0351 {
0352     struct vio_dev *viod = to_vio_dev(dev);
0353     struct hvcs_struct *hvcsd = from_vio_dev(viod);
0354     unsigned long flags;
0355     int retval;
0356 
0357     spin_lock_irqsave(&hvcsd->lock, flags);
0358     retval = sprintf(buf, "%s\n", &hvcsd->p_location_code[0]);
0359     spin_unlock_irqrestore(&hvcsd->lock, flags);
0360     return retval;
0361 }
0362 
0363 static DEVICE_ATTR(current_vty,
0364     S_IRUGO | S_IWUSR, hvcs_current_vty_show, hvcs_current_vty_store);
0365 
0366 static ssize_t hvcs_vterm_state_store(struct device *dev, struct device_attribute *attr, const char *buf,
0367         size_t count)
0368 {
0369     struct vio_dev *viod = to_vio_dev(dev);
0370     struct hvcs_struct *hvcsd = from_vio_dev(viod);
0371     unsigned long flags;
0372 
0373     /* writing a '0' to this sysfs entry will result in the disconnect. */
0374     if (simple_strtol(buf, NULL, 0) != 0)
0375         return -EINVAL;
0376 
0377     spin_lock_irqsave(&hvcsd->lock, flags);
0378 
0379     if (hvcsd->port.count > 0) {
0380         spin_unlock_irqrestore(&hvcsd->lock, flags);
0381         printk(KERN_INFO "HVCS: vterm state unchanged.  "
0382                 "The hvcs device node is still in use.\n");
0383         return -EPERM;
0384     }
0385 
0386     if (hvcsd->connected == 0) {
0387         spin_unlock_irqrestore(&hvcsd->lock, flags);
0388         printk(KERN_INFO "HVCS: vterm state unchanged. The"
0389                 " vty-server is not connected to a vty.\n");
0390         return -EPERM;
0391     }
0392 
0393     hvcs_partner_free(hvcsd);
0394     printk(KERN_INFO "HVCS: Closed vty-server@%X and"
0395             " partner vty@%X:%d connection.\n",
0396             hvcsd->vdev->unit_address,
0397             hvcsd->p_unit_address,
0398             (uint32_t)hvcsd->p_partition_ID);
0399 
0400     spin_unlock_irqrestore(&hvcsd->lock, flags);
0401     return count;
0402 }
0403 
0404 static ssize_t hvcs_vterm_state_show(struct device *dev, struct device_attribute *attr, char *buf)
0405 {
0406     struct vio_dev *viod = to_vio_dev(dev);
0407     struct hvcs_struct *hvcsd = from_vio_dev(viod);
0408     unsigned long flags;
0409     int retval;
0410 
0411     spin_lock_irqsave(&hvcsd->lock, flags);
0412     retval = sprintf(buf, "%d\n", hvcsd->connected);
0413     spin_unlock_irqrestore(&hvcsd->lock, flags);
0414     return retval;
0415 }
0416 static DEVICE_ATTR(vterm_state, S_IRUGO | S_IWUSR,
0417         hvcs_vterm_state_show, hvcs_vterm_state_store);
0418 
0419 static ssize_t hvcs_index_show(struct device *dev, struct device_attribute *attr, char *buf)
0420 {
0421     struct vio_dev *viod = to_vio_dev(dev);
0422     struct hvcs_struct *hvcsd = from_vio_dev(viod);
0423     unsigned long flags;
0424     int retval;
0425 
0426     spin_lock_irqsave(&hvcsd->lock, flags);
0427     retval = sprintf(buf, "%d\n", hvcsd->index);
0428     spin_unlock_irqrestore(&hvcsd->lock, flags);
0429     return retval;
0430 }
0431 
0432 static DEVICE_ATTR(index, S_IRUGO, hvcs_index_show, NULL);
0433 
0434 static struct attribute *hvcs_attrs[] = {
0435     &dev_attr_partner_vtys.attr,
0436     &dev_attr_partner_clcs.attr,
0437     &dev_attr_current_vty.attr,
0438     &dev_attr_vterm_state.attr,
0439     &dev_attr_index.attr,
0440     NULL,
0441 };
0442 
0443 static struct attribute_group hvcs_attr_group = {
0444     .attrs = hvcs_attrs,
0445 };
0446 
0447 static ssize_t rescan_show(struct device_driver *ddp, char *buf)
0448 {
0449     /* A 1 means it is updating, a 0 means it is done updating */
0450     return snprintf(buf, PAGE_SIZE, "%d\n", hvcs_rescan_status);
0451 }
0452 
0453 static ssize_t rescan_store(struct device_driver *ddp, const char * buf,
0454         size_t count)
0455 {
0456     if ((simple_strtol(buf, NULL, 0) != 1)
0457         && (hvcs_rescan_status != 0))
0458         return -EINVAL;
0459 
0460     hvcs_rescan_status = 1;
0461     printk(KERN_INFO "HVCS: rescanning partner info for all"
0462         " vty-servers.\n");
0463     hvcs_rescan_devices_list();
0464     hvcs_rescan_status = 0;
0465     return count;
0466 }
0467 
0468 static DRIVER_ATTR_RW(rescan);
0469 
0470 static void hvcs_kick(void)
0471 {
0472     hvcs_kicked = 1;
0473     wmb();
0474     wake_up_process(hvcs_task);
0475 }
0476 
0477 static void hvcs_unthrottle(struct tty_struct *tty)
0478 {
0479     struct hvcs_struct *hvcsd = tty->driver_data;
0480     unsigned long flags;
0481 
0482     spin_lock_irqsave(&hvcsd->lock, flags);
0483     hvcsd->todo_mask |= HVCS_SCHED_READ;
0484     spin_unlock_irqrestore(&hvcsd->lock, flags);
0485     hvcs_kick();
0486 }
0487 
0488 static void hvcs_throttle(struct tty_struct *tty)
0489 {
0490     struct hvcs_struct *hvcsd = tty->driver_data;
0491     unsigned long flags;
0492 
0493     spin_lock_irqsave(&hvcsd->lock, flags);
0494     vio_disable_interrupts(hvcsd->vdev);
0495     spin_unlock_irqrestore(&hvcsd->lock, flags);
0496 }
0497 
0498 /*
0499  * If the device is being removed we don't have to worry about this interrupt
0500  * handler taking any further interrupts because they are disabled which means
0501  * the hvcs_struct will always be valid in this handler.
0502  */
0503 static irqreturn_t hvcs_handle_interrupt(int irq, void *dev_instance)
0504 {
0505     struct hvcs_struct *hvcsd = dev_instance;
0506 
0507     spin_lock(&hvcsd->lock);
0508     vio_disable_interrupts(hvcsd->vdev);
0509     hvcsd->todo_mask |= HVCS_SCHED_READ;
0510     spin_unlock(&hvcsd->lock);
0511     hvcs_kick();
0512 
0513     return IRQ_HANDLED;
0514 }
0515 
0516 /* This function must be called with the hvcsd->lock held */
0517 static void hvcs_try_write(struct hvcs_struct *hvcsd)
0518 {
0519     uint32_t unit_address = hvcsd->vdev->unit_address;
0520     struct tty_struct *tty = hvcsd->port.tty;
0521     int sent;
0522 
0523     if (hvcsd->todo_mask & HVCS_TRY_WRITE) {
0524         /* won't send partial writes */
0525         sent = hvc_put_chars(unit_address,
0526                 &hvcsd->buffer[0],
0527                 hvcsd->chars_in_buffer );
0528         if (sent > 0) {
0529             hvcsd->chars_in_buffer = 0;
0530             /* wmb(); */
0531             hvcsd->todo_mask &= ~(HVCS_TRY_WRITE);
0532             /* wmb(); */
0533 
0534             /*
0535              * We are still obligated to deliver the data to the
0536              * hypervisor even if the tty has been closed because
0537              * we committed to delivering it.  But don't try to wake
0538              * a non-existent tty.
0539              */
0540             if (tty) {
0541                 tty_wakeup(tty);
0542             }
0543         }
0544     }
0545 }
0546 
0547 static int hvcs_io(struct hvcs_struct *hvcsd)
0548 {
0549     uint32_t unit_address;
0550     struct tty_struct *tty;
0551     char buf[HVCS_BUFF_LEN] __ALIGNED__;
0552     unsigned long flags;
0553     int got = 0;
0554 
0555     spin_lock_irqsave(&hvcsd->lock, flags);
0556 
0557     unit_address = hvcsd->vdev->unit_address;
0558     tty = hvcsd->port.tty;
0559 
0560     hvcs_try_write(hvcsd);
0561 
0562     if (!tty || tty_throttled(tty)) {
0563         hvcsd->todo_mask &= ~(HVCS_READ_MASK);
0564         goto bail;
0565     } else if (!(hvcsd->todo_mask & (HVCS_READ_MASK)))
0566         goto bail;
0567 
0568     /* remove the read masks */
0569     hvcsd->todo_mask &= ~(HVCS_READ_MASK);
0570 
0571     if (tty_buffer_request_room(&hvcsd->port, HVCS_BUFF_LEN) >= HVCS_BUFF_LEN) {
0572         got = hvc_get_chars(unit_address,
0573                 &buf[0],
0574                 HVCS_BUFF_LEN);
0575         tty_insert_flip_string(&hvcsd->port, buf, got);
0576     }
0577 
0578     /* Give the TTY time to process the data we just sent. */
0579     if (got)
0580         hvcsd->todo_mask |= HVCS_QUICK_READ;
0581 
0582     spin_unlock_irqrestore(&hvcsd->lock, flags);
0583     /* This is synch -- FIXME :js: it is not! */
0584     if (got)
0585         tty_flip_buffer_push(&hvcsd->port);
0586     else {
0587         /* Do this _after_ the flip_buffer_push */
0588         spin_lock_irqsave(&hvcsd->lock, flags);
0589         vio_enable_interrupts(hvcsd->vdev);
0590         spin_unlock_irqrestore(&hvcsd->lock, flags);
0591     }
0592 
0593     return hvcsd->todo_mask;
0594 
0595  bail:
0596     spin_unlock_irqrestore(&hvcsd->lock, flags);
0597     return hvcsd->todo_mask;
0598 }
0599 
0600 static int khvcsd(void *unused)
0601 {
0602     struct hvcs_struct *hvcsd;
0603     int hvcs_todo_mask;
0604 
0605     __set_current_state(TASK_RUNNING);
0606 
0607     do {
0608         hvcs_todo_mask = 0;
0609         hvcs_kicked = 0;
0610         wmb();
0611 
0612         spin_lock(&hvcs_structs_lock);
0613         list_for_each_entry(hvcsd, &hvcs_structs, next) {
0614             hvcs_todo_mask |= hvcs_io(hvcsd);
0615         }
0616         spin_unlock(&hvcs_structs_lock);
0617 
0618         /*
0619          * If any of the hvcs adapters want to try a write or quick read
0620          * don't schedule(), yield a smidgen then execute the hvcs_io
0621          * thread again for those that want the write.
0622          */
0623          if (hvcs_todo_mask & (HVCS_TRY_WRITE | HVCS_QUICK_READ)) {
0624             yield();
0625             continue;
0626         }
0627 
0628         set_current_state(TASK_INTERRUPTIBLE);
0629         if (!hvcs_kicked)
0630             schedule();
0631         __set_current_state(TASK_RUNNING);
0632     } while (!kthread_should_stop());
0633 
0634     return 0;
0635 }
0636 
0637 static const struct vio_device_id hvcs_driver_table[] = {
0638     {"serial-server", "hvterm2"},
0639     { "", "" }
0640 };
0641 MODULE_DEVICE_TABLE(vio, hvcs_driver_table);
0642 
0643 static void hvcs_return_index(int index)
0644 {
0645     /* Paranoia check */
0646     if (!hvcs_index_list)
0647         return;
0648     if (index < 0 || index >= hvcs_index_count)
0649         return;
0650     if (hvcs_index_list[index] == -1)
0651         return;
0652     else
0653         hvcs_index_list[index] = -1;
0654 }
0655 
0656 static void hvcs_destruct_port(struct tty_port *p)
0657 {
0658     struct hvcs_struct *hvcsd = container_of(p, struct hvcs_struct, port);
0659     struct vio_dev *vdev;
0660     unsigned long flags;
0661 
0662     spin_lock(&hvcs_structs_lock);
0663     spin_lock_irqsave(&hvcsd->lock, flags);
0664 
0665     /* the list_del poisons the pointers */
0666     list_del(&(hvcsd->next));
0667 
0668     if (hvcsd->connected == 1) {
0669         hvcs_partner_free(hvcsd);
0670         printk(KERN_INFO "HVCS: Closed vty-server@%X and"
0671                 " partner vty@%X:%d connection.\n",
0672                 hvcsd->vdev->unit_address,
0673                 hvcsd->p_unit_address,
0674                 (uint32_t)hvcsd->p_partition_ID);
0675     }
0676     printk(KERN_INFO "HVCS: Destroyed hvcs_struct for vty-server@%X.\n",
0677             hvcsd->vdev->unit_address);
0678 
0679     vdev = hvcsd->vdev;
0680     hvcsd->vdev = NULL;
0681 
0682     hvcsd->p_unit_address = 0;
0683     hvcsd->p_partition_ID = 0;
0684     hvcs_return_index(hvcsd->index);
0685     memset(&hvcsd->p_location_code[0], 0x00, HVCS_CLC_LENGTH + 1);
0686 
0687     spin_unlock_irqrestore(&hvcsd->lock, flags);
0688     spin_unlock(&hvcs_structs_lock);
0689 
0690     sysfs_remove_group(&vdev->dev.kobj, &hvcs_attr_group);
0691 
0692     kfree(hvcsd);
0693 }
0694 
0695 static const struct tty_port_operations hvcs_port_ops = {
0696     .destruct = hvcs_destruct_port,
0697 };
0698 
0699 static int hvcs_get_index(void)
0700 {
0701     int i;
0702     /* Paranoia check */
0703     if (!hvcs_index_list) {
0704         printk(KERN_ERR "HVCS: hvcs_index_list NOT valid!.\n");
0705         return -EFAULT;
0706     }
0707     /* Find the numerically lowest first free index. */
0708     for(i = 0; i < hvcs_index_count; i++) {
0709         if (hvcs_index_list[i] == -1) {
0710             hvcs_index_list[i] = 0;
0711             return i;
0712         }
0713     }
0714     return -1;
0715 }
0716 
0717 static int hvcs_probe(
0718     struct vio_dev *dev,
0719     const struct vio_device_id *id)
0720 {
0721     struct hvcs_struct *hvcsd;
0722     int index, rc;
0723     int retval;
0724 
0725     if (!dev || !id) {
0726         printk(KERN_ERR "HVCS: probed with invalid parameter.\n");
0727         return -EPERM;
0728     }
0729 
0730     /* Make sure we are properly initialized */
0731     rc = hvcs_initialize();
0732     if (rc) {
0733         pr_err("HVCS: Failed to initialize core driver.\n");
0734         return rc;
0735     }
0736 
0737     /* early to avoid cleanup on failure */
0738     index = hvcs_get_index();
0739     if (index < 0) {
0740         return -EFAULT;
0741     }
0742 
0743     hvcsd = kzalloc(sizeof(*hvcsd), GFP_KERNEL);
0744     if (!hvcsd)
0745         return -ENODEV;
0746 
0747     tty_port_init(&hvcsd->port);
0748     hvcsd->port.ops = &hvcs_port_ops;
0749     spin_lock_init(&hvcsd->lock);
0750 
0751     hvcsd->vdev = dev;
0752     dev_set_drvdata(&dev->dev, hvcsd);
0753 
0754     hvcsd->index = index;
0755 
0756     /* hvcsd->index = ++hvcs_struct_count; */
0757     hvcsd->chars_in_buffer = 0;
0758     hvcsd->todo_mask = 0;
0759     hvcsd->connected = 0;
0760 
0761     /*
0762      * This will populate the hvcs_struct's partner info fields for the
0763      * first time.
0764      */
0765     if (hvcs_get_pi(hvcsd)) {
0766         printk(KERN_ERR "HVCS: Failed to fetch partner"
0767             " info for vty-server@%X on device probe.\n",
0768             hvcsd->vdev->unit_address);
0769     }
0770 
0771     /*
0772      * If a user app opens a tty that corresponds to this vty-server before
0773      * the hvcs_struct has been added to the devices list then the user app
0774      * will get -ENODEV.
0775      */
0776     spin_lock(&hvcs_structs_lock);
0777     list_add_tail(&(hvcsd->next), &hvcs_structs);
0778     spin_unlock(&hvcs_structs_lock);
0779 
0780     retval = sysfs_create_group(&dev->dev.kobj, &hvcs_attr_group);
0781     if (retval) {
0782         printk(KERN_ERR "HVCS: Can't create sysfs attrs for vty-server@%X\n",
0783                hvcsd->vdev->unit_address);
0784         return retval;
0785     }
0786 
0787     printk(KERN_INFO "HVCS: vty-server@%X added to the vio bus.\n", dev->unit_address);
0788 
0789     /*
0790      * DON'T enable interrupts here because there is no user to receive the
0791      * data.
0792      */
0793     return 0;
0794 }
0795 
0796 static void hvcs_remove(struct vio_dev *dev)
0797 {
0798     struct hvcs_struct *hvcsd = dev_get_drvdata(&dev->dev);
0799     unsigned long flags;
0800     struct tty_struct *tty;
0801 
0802     /* By this time the vty-server won't be getting any more interrupts */
0803 
0804     spin_lock_irqsave(&hvcsd->lock, flags);
0805 
0806     tty = hvcsd->port.tty;
0807 
0808     spin_unlock_irqrestore(&hvcsd->lock, flags);
0809 
0810     /*
0811      * Let the last holder of this object cause it to be removed, which
0812      * would probably be tty_hangup below.
0813      */
0814     tty_port_put(&hvcsd->port);
0815 
0816     /*
0817      * The hangup is a scheduled function which will auto chain call
0818      * hvcs_hangup.  The tty should always be valid at this time unless a
0819      * simultaneous tty close already cleaned up the hvcs_struct.
0820      */
0821     if (tty)
0822         tty_hangup(tty);
0823 
0824     printk(KERN_INFO "HVCS: vty-server@%X removed from the"
0825             " vio bus.\n", dev->unit_address);
0826 };
0827 
0828 static struct vio_driver hvcs_vio_driver = {
0829     .id_table   = hvcs_driver_table,
0830     .probe      = hvcs_probe,
0831     .remove     = hvcs_remove,
0832     .name       = hvcs_driver_name,
0833 };
0834 
0835 /* Only called from hvcs_get_pi please */
0836 static void hvcs_set_pi(struct hvcs_partner_info *pi, struct hvcs_struct *hvcsd)
0837 {
0838     hvcsd->p_unit_address = pi->unit_address;
0839     hvcsd->p_partition_ID  = pi->partition_ID;
0840 
0841     /* copy the null-term char too */
0842     strlcpy(hvcsd->p_location_code, pi->location_code,
0843         sizeof(hvcsd->p_location_code));
0844 }
0845 
0846 /*
0847  * Traverse the list and add the partner info that is found to the hvcs_struct
0848  * struct entry. NOTE: At this time I know that partner info will return a
0849  * single entry but in the future there may be multiple partner info entries per
0850  * vty-server and you'll want to zero out that list and reset it.  If for some
0851  * reason you have an old version of this driver but there IS more than one
0852  * partner info then hvcsd->p_* will hold the last partner info data from the
0853  * firmware query.  A good way to update this code would be to replace the three
0854  * partner info fields in hvcs_struct with a list of hvcs_partner_info
0855  * instances.
0856  *
0857  * This function must be called with the hvcsd->lock held.
0858  */
0859 static int hvcs_get_pi(struct hvcs_struct *hvcsd)
0860 {
0861     struct hvcs_partner_info *pi;
0862     uint32_t unit_address = hvcsd->vdev->unit_address;
0863     struct list_head head;
0864     int retval;
0865 
0866     spin_lock(&hvcs_pi_lock);
0867     if (!hvcs_pi_buff) {
0868         spin_unlock(&hvcs_pi_lock);
0869         return -EFAULT;
0870     }
0871     retval = hvcs_get_partner_info(unit_address, &head, hvcs_pi_buff);
0872     spin_unlock(&hvcs_pi_lock);
0873     if (retval) {
0874         printk(KERN_ERR "HVCS: Failed to fetch partner"
0875             " info for vty-server@%x.\n", unit_address);
0876         return retval;
0877     }
0878 
0879     /* nixes the values if the partner vty went away */
0880     hvcsd->p_unit_address = 0;
0881     hvcsd->p_partition_ID = 0;
0882 
0883     list_for_each_entry(pi, &head, node)
0884         hvcs_set_pi(pi, hvcsd);
0885 
0886     hvcs_free_partner_info(&head);
0887     return 0;
0888 }
0889 
0890 /*
0891  * This function is executed by the driver "rescan" sysfs entry.  It shouldn't
0892  * be executed elsewhere, in order to prevent deadlock issues.
0893  */
0894 static int hvcs_rescan_devices_list(void)
0895 {
0896     struct hvcs_struct *hvcsd;
0897     unsigned long flags;
0898 
0899     spin_lock(&hvcs_structs_lock);
0900 
0901     list_for_each_entry(hvcsd, &hvcs_structs, next) {
0902         spin_lock_irqsave(&hvcsd->lock, flags);
0903         hvcs_get_pi(hvcsd);
0904         spin_unlock_irqrestore(&hvcsd->lock, flags);
0905     }
0906 
0907     spin_unlock(&hvcs_structs_lock);
0908 
0909     return 0;
0910 }
0911 
0912 /*
0913  * Farm this off into its own function because it could be more complex once
0914  * multiple partners support is added. This function should be called with
0915  * the hvcsd->lock held.
0916  */
0917 static int hvcs_has_pi(struct hvcs_struct *hvcsd)
0918 {
0919     if ((!hvcsd->p_unit_address) || (!hvcsd->p_partition_ID))
0920         return 0;
0921     return 1;
0922 }
0923 
0924 /*
0925  * NOTE: It is possible that the super admin removed a partner vty and then
0926  * added a different vty as the new partner.
0927  *
0928  * This function must be called with the hvcsd->lock held.
0929  */
0930 static int hvcs_partner_connect(struct hvcs_struct *hvcsd)
0931 {
0932     int retval;
0933     unsigned int unit_address = hvcsd->vdev->unit_address;
0934 
0935     /*
0936      * If there wasn't any pi when the device was added it doesn't meant
0937      * there isn't any now.  This driver isn't notified when a new partner
0938      * vty is added to a vty-server so we discover changes on our own.
0939      * Please see comments in hvcs_register_connection() for justification
0940      * of this bizarre code.
0941      */
0942     retval = hvcs_register_connection(unit_address,
0943             hvcsd->p_partition_ID,
0944             hvcsd->p_unit_address);
0945     if (!retval) {
0946         hvcsd->connected = 1;
0947         return 0;
0948     } else if (retval != -EINVAL)
0949         return retval;
0950 
0951     /*
0952      * As per the spec re-get the pi and try again if -EINVAL after the
0953      * first connection attempt.
0954      */
0955     if (hvcs_get_pi(hvcsd))
0956         return -ENOMEM;
0957 
0958     if (!hvcs_has_pi(hvcsd))
0959         return -ENODEV;
0960 
0961     retval = hvcs_register_connection(unit_address,
0962             hvcsd->p_partition_ID,
0963             hvcsd->p_unit_address);
0964     if (retval != -EINVAL) {
0965         hvcsd->connected = 1;
0966         return retval;
0967     }
0968 
0969     /*
0970      * EBUSY is the most likely scenario though the vty could have been
0971      * removed or there really could be an hcall error due to the parameter
0972      * data but thanks to ambiguous firmware return codes we can't really
0973      * tell.
0974      */
0975     printk(KERN_INFO "HVCS: vty-server or partner"
0976             " vty is busy.  Try again later.\n");
0977     return -EBUSY;
0978 }
0979 
0980 /* This function must be called with the hvcsd->lock held */
0981 static void hvcs_partner_free(struct hvcs_struct *hvcsd)
0982 {
0983     int retval;
0984     do {
0985         retval = hvcs_free_connection(hvcsd->vdev->unit_address);
0986     } while (retval == -EBUSY);
0987     hvcsd->connected = 0;
0988 }
0989 
0990 /* This helper function must be called WITHOUT the hvcsd->lock held */
0991 static int hvcs_enable_device(struct hvcs_struct *hvcsd, uint32_t unit_address,
0992         unsigned int irq, struct vio_dev *vdev)
0993 {
0994     unsigned long flags;
0995     int rc;
0996 
0997     /*
0998      * It is possible that the vty-server was removed between the time that
0999      * the conn was registered and now.
1000      */
1001     rc = request_irq(irq, &hvcs_handle_interrupt, 0, "ibmhvcs", hvcsd);
1002     if (!rc) {
1003         /*
1004          * It is possible the vty-server was removed after the irq was
1005          * requested but before we have time to enable interrupts.
1006          */
1007         if (vio_enable_interrupts(vdev) == H_SUCCESS)
1008             return 0;
1009         else {
1010             printk(KERN_ERR "HVCS: int enable failed for"
1011                     " vty-server@%X.\n", unit_address);
1012             free_irq(irq, hvcsd);
1013         }
1014     } else
1015         printk(KERN_ERR "HVCS: irq req failed for"
1016                 " vty-server@%X.\n", unit_address);
1017 
1018     spin_lock_irqsave(&hvcsd->lock, flags);
1019     hvcs_partner_free(hvcsd);
1020     spin_unlock_irqrestore(&hvcsd->lock, flags);
1021 
1022     return rc;
1023 
1024 }
1025 
1026 /*
1027  * This always increments the kref ref count if the call is successful.
1028  * Please remember to dec when you are done with the instance.
1029  *
1030  * NOTICE: Do NOT hold either the hvcs_struct.lock or hvcs_structs_lock when
1031  * calling this function or you will get deadlock.
1032  */
1033 static struct hvcs_struct *hvcs_get_by_index(int index)
1034 {
1035     struct hvcs_struct *hvcsd;
1036     unsigned long flags;
1037 
1038     spin_lock(&hvcs_structs_lock);
1039     list_for_each_entry(hvcsd, &hvcs_structs, next) {
1040         spin_lock_irqsave(&hvcsd->lock, flags);
1041         if (hvcsd->index == index) {
1042             tty_port_get(&hvcsd->port);
1043             spin_unlock_irqrestore(&hvcsd->lock, flags);
1044             spin_unlock(&hvcs_structs_lock);
1045             return hvcsd;
1046         }
1047         spin_unlock_irqrestore(&hvcsd->lock, flags);
1048     }
1049     spin_unlock(&hvcs_structs_lock);
1050 
1051     return NULL;
1052 }
1053 
1054 static int hvcs_install(struct tty_driver *driver, struct tty_struct *tty)
1055 {
1056     struct hvcs_struct *hvcsd;
1057     struct vio_dev *vdev;
1058     unsigned long unit_address, flags;
1059     unsigned int irq;
1060     int retval;
1061 
1062     /*
1063      * Is there a vty-server that shares the same index?
1064      * This function increments the kref index.
1065      */
1066     hvcsd = hvcs_get_by_index(tty->index);
1067     if (!hvcsd) {
1068         printk(KERN_WARNING "HVCS: open failed, no device associated"
1069                 " with tty->index %d.\n", tty->index);
1070         return -ENODEV;
1071     }
1072 
1073     spin_lock_irqsave(&hvcsd->lock, flags);
1074 
1075     if (hvcsd->connected == 0) {
1076         retval = hvcs_partner_connect(hvcsd);
1077         if (retval) {
1078             spin_unlock_irqrestore(&hvcsd->lock, flags);
1079             printk(KERN_WARNING "HVCS: partner connect failed.\n");
1080             goto err_put;
1081         }
1082     }
1083 
1084     hvcsd->port.count = 0;
1085     hvcsd->port.tty = tty;
1086     tty->driver_data = hvcsd;
1087 
1088     memset(&hvcsd->buffer[0], 0x00, HVCS_BUFF_LEN);
1089 
1090     /*
1091      * Save these in the spinlock for the enable operations that need them
1092      * outside of the spinlock.
1093      */
1094     irq = hvcsd->vdev->irq;
1095     vdev = hvcsd->vdev;
1096     unit_address = hvcsd->vdev->unit_address;
1097 
1098     hvcsd->todo_mask |= HVCS_SCHED_READ;
1099     spin_unlock_irqrestore(&hvcsd->lock, flags);
1100 
1101     /*
1102      * This must be done outside of the spinlock because it requests irqs
1103      * and will grab the spinlock and free the connection if it fails.
1104      */
1105     retval = hvcs_enable_device(hvcsd, unit_address, irq, vdev);
1106     if (retval) {
1107         printk(KERN_WARNING "HVCS: enable device failed.\n");
1108         goto err_put;
1109     }
1110 
1111     retval = tty_port_install(&hvcsd->port, driver, tty);
1112     if (retval)
1113         goto err_irq;
1114 
1115     return 0;
1116 err_irq:
1117     spin_lock_irqsave(&hvcsd->lock, flags);
1118     vio_disable_interrupts(hvcsd->vdev);
1119     spin_unlock_irqrestore(&hvcsd->lock, flags);
1120     free_irq(irq, hvcsd);
1121 err_put:
1122     tty_port_put(&hvcsd->port);
1123 
1124     return retval;
1125 }
1126 
1127 /*
1128  * This is invoked via the tty_open interface when a user app connects to the
1129  * /dev node.
1130  */
1131 static int hvcs_open(struct tty_struct *tty, struct file *filp)
1132 {
1133     struct hvcs_struct *hvcsd = tty->driver_data;
1134     unsigned long flags;
1135 
1136     spin_lock_irqsave(&hvcsd->lock, flags);
1137     hvcsd->port.count++;
1138     hvcsd->todo_mask |= HVCS_SCHED_READ;
1139     spin_unlock_irqrestore(&hvcsd->lock, flags);
1140 
1141     hvcs_kick();
1142 
1143     printk(KERN_INFO "HVCS: vty-server@%X connection opened.\n",
1144         hvcsd->vdev->unit_address );
1145 
1146     return 0;
1147 }
1148 
1149 static void hvcs_close(struct tty_struct *tty, struct file *filp)
1150 {
1151     struct hvcs_struct *hvcsd;
1152     unsigned long flags;
1153     int irq;
1154 
1155     /*
1156      * Is someone trying to close the file associated with this device after
1157      * we have hung up?  If so tty->driver_data wouldn't be valid.
1158      */
1159     if (tty_hung_up_p(filp))
1160         return;
1161 
1162     /*
1163      * No driver_data means that this close was probably issued after a
1164      * failed hvcs_open by the tty layer's release_dev() api and we can just
1165      * exit cleanly.
1166      */
1167     if (!tty->driver_data)
1168         return;
1169 
1170     hvcsd = tty->driver_data;
1171 
1172     spin_lock_irqsave(&hvcsd->lock, flags);
1173     if (--hvcsd->port.count == 0) {
1174 
1175         vio_disable_interrupts(hvcsd->vdev);
1176 
1177         /*
1178          * NULL this early so that the kernel_thread doesn't try to
1179          * execute any operations on the TTY even though it is obligated
1180          * to deliver any pending I/O to the hypervisor.
1181          */
1182         hvcsd->port.tty = NULL;
1183 
1184         irq = hvcsd->vdev->irq;
1185         spin_unlock_irqrestore(&hvcsd->lock, flags);
1186 
1187         tty_wait_until_sent(tty, HVCS_CLOSE_WAIT);
1188 
1189         free_irq(irq, hvcsd);
1190         return;
1191     } else if (hvcsd->port.count < 0) {
1192         printk(KERN_ERR "HVCS: vty-server@%X open_count: %d is mismanaged.\n",
1193         hvcsd->vdev->unit_address, hvcsd->port.count);
1194     }
1195 
1196     spin_unlock_irqrestore(&hvcsd->lock, flags);
1197 }
1198 
1199 static void hvcs_cleanup(struct tty_struct * tty)
1200 {
1201     struct hvcs_struct *hvcsd = tty->driver_data;
1202 
1203     /*
1204      * This line is important because it tells hvcs_open that this
1205      * device needs to be re-configured the next time hvcs_open is
1206      * called.
1207      */
1208     tty->driver_data = NULL;
1209 
1210     tty_port_put(&hvcsd->port);
1211 }
1212 
1213 static void hvcs_hangup(struct tty_struct * tty)
1214 {
1215     struct hvcs_struct *hvcsd = tty->driver_data;
1216     unsigned long flags;
1217     int temp_open_count;
1218     int irq;
1219 
1220     spin_lock_irqsave(&hvcsd->lock, flags);
1221     /* Preserve this so that we know how many kref refs to put */
1222     temp_open_count = hvcsd->port.count;
1223 
1224     /*
1225      * Don't kref put inside the spinlock because the destruction
1226      * callback may use the spinlock and it may get called before the
1227      * spinlock has been released.
1228      */
1229     vio_disable_interrupts(hvcsd->vdev);
1230 
1231     hvcsd->todo_mask = 0;
1232 
1233     /* I don't think the tty needs the hvcs_struct pointer after a hangup */
1234     tty->driver_data = NULL;
1235     hvcsd->port.tty = NULL;
1236 
1237     hvcsd->port.count = 0;
1238 
1239     /* This will drop any buffered data on the floor which is OK in a hangup
1240      * scenario. */
1241     memset(&hvcsd->buffer[0], 0x00, HVCS_BUFF_LEN);
1242     hvcsd->chars_in_buffer = 0;
1243 
1244     irq = hvcsd->vdev->irq;
1245 
1246     spin_unlock_irqrestore(&hvcsd->lock, flags);
1247 
1248     free_irq(irq, hvcsd);
1249 
1250     /*
1251      * We need to kref_put() for every open_count we have since the
1252      * tty_hangup() function doesn't invoke a close per open connection on a
1253      * non-console device.
1254      */
1255     while(temp_open_count) {
1256         --temp_open_count;
1257         /*
1258          * The final put will trigger destruction of the hvcs_struct.
1259          * NOTE:  If this hangup was signaled from user space then the
1260          * final put will never happen.
1261          */
1262         tty_port_put(&hvcsd->port);
1263     }
1264 }
1265 
1266 /*
1267  * NOTE: This is almost always from_user since user level apps interact with the
1268  * /dev nodes. I'm trusting that if hvcs_write gets called and interrupted by
1269  * hvcs_remove (which removes the target device and executes tty_hangup()) that
1270  * tty_hangup will allow hvcs_write time to complete execution before it
1271  * terminates our device.
1272  */
1273 static int hvcs_write(struct tty_struct *tty,
1274         const unsigned char *buf, int count)
1275 {
1276     struct hvcs_struct *hvcsd = tty->driver_data;
1277     unsigned int unit_address;
1278     const unsigned char *charbuf;
1279     unsigned long flags;
1280     int total_sent = 0;
1281     int tosend = 0;
1282     int result = 0;
1283 
1284     /*
1285      * If they don't check the return code off of their open they may
1286      * attempt this even if there is no connected device.
1287      */
1288     if (!hvcsd)
1289         return -ENODEV;
1290 
1291     /* Reasonable size to prevent user level flooding */
1292     if (count > HVCS_MAX_FROM_USER) {
1293         printk(KERN_WARNING "HVCS write: count being truncated to"
1294                 " HVCS_MAX_FROM_USER.\n");
1295         count = HVCS_MAX_FROM_USER;
1296     }
1297 
1298     charbuf = buf;
1299 
1300     spin_lock_irqsave(&hvcsd->lock, flags);
1301 
1302     /*
1303      * Somehow an open succeeded but the device was removed or the
1304      * connection terminated between the vty-server and partner vty during
1305      * the middle of a write operation?  This is a crummy place to do this
1306      * but we want to keep it all in the spinlock.
1307      */
1308     if (hvcsd->port.count <= 0) {
1309         spin_unlock_irqrestore(&hvcsd->lock, flags);
1310         return -ENODEV;
1311     }
1312 
1313     unit_address = hvcsd->vdev->unit_address;
1314 
1315     while (count > 0) {
1316         tosend = min(count, (HVCS_BUFF_LEN - hvcsd->chars_in_buffer));
1317         /*
1318          * No more space, this probably means that the last call to
1319          * hvcs_write() didn't succeed and the buffer was filled up.
1320          */
1321         if (!tosend)
1322             break;
1323 
1324         memcpy(&hvcsd->buffer[hvcsd->chars_in_buffer],
1325                 &charbuf[total_sent],
1326                 tosend);
1327 
1328         hvcsd->chars_in_buffer += tosend;
1329 
1330         result = 0;
1331 
1332         /*
1333          * If this is true then we don't want to try writing to the
1334          * hypervisor because that is the kernel_threads job now.  We'll
1335          * just add to the buffer.
1336          */
1337         if (!(hvcsd->todo_mask & HVCS_TRY_WRITE))
1338             /* won't send partial writes */
1339             result = hvc_put_chars(unit_address,
1340                     &hvcsd->buffer[0],
1341                     hvcsd->chars_in_buffer);
1342 
1343         /*
1344          * Since we know we have enough room in hvcsd->buffer for
1345          * tosend we record that it was sent regardless of whether the
1346          * hypervisor actually took it because we have it buffered.
1347          */
1348         total_sent+=tosend;
1349         count-=tosend;
1350         if (result == 0) {
1351             hvcsd->todo_mask |= HVCS_TRY_WRITE;
1352             hvcs_kick();
1353             break;
1354         }
1355 
1356         hvcsd->chars_in_buffer = 0;
1357         /*
1358          * Test after the chars_in_buffer reset otherwise this could
1359          * deadlock our writes if hvc_put_chars fails.
1360          */
1361         if (result < 0)
1362             break;
1363     }
1364 
1365     spin_unlock_irqrestore(&hvcsd->lock, flags);
1366 
1367     if (result == -1)
1368         return -EIO;
1369     else
1370         return total_sent;
1371 }
1372 
1373 /*
1374  * This is really asking how much can we guarantee that we can send or that we
1375  * absolutely WILL BUFFER if we can't send it.  This driver MUST honor the
1376  * return value, hence the reason for hvcs_struct buffering.
1377  */
1378 static unsigned int hvcs_write_room(struct tty_struct *tty)
1379 {
1380     struct hvcs_struct *hvcsd = tty->driver_data;
1381 
1382     if (!hvcsd || hvcsd->port.count <= 0)
1383         return 0;
1384 
1385     return HVCS_BUFF_LEN - hvcsd->chars_in_buffer;
1386 }
1387 
1388 static unsigned int hvcs_chars_in_buffer(struct tty_struct *tty)
1389 {
1390     struct hvcs_struct *hvcsd = tty->driver_data;
1391 
1392     return hvcsd->chars_in_buffer;
1393 }
1394 
1395 static const struct tty_operations hvcs_ops = {
1396     .install = hvcs_install,
1397     .open = hvcs_open,
1398     .close = hvcs_close,
1399     .cleanup = hvcs_cleanup,
1400     .hangup = hvcs_hangup,
1401     .write = hvcs_write,
1402     .write_room = hvcs_write_room,
1403     .chars_in_buffer = hvcs_chars_in_buffer,
1404     .unthrottle = hvcs_unthrottle,
1405     .throttle = hvcs_throttle,
1406 };
1407 
1408 static int hvcs_alloc_index_list(int n)
1409 {
1410     int i;
1411 
1412     hvcs_index_list = kmalloc_array(n, sizeof(hvcs_index_count),
1413                     GFP_KERNEL);
1414     if (!hvcs_index_list)
1415         return -ENOMEM;
1416     hvcs_index_count = n;
1417     for (i = 0; i < hvcs_index_count; i++)
1418         hvcs_index_list[i] = -1;
1419     return 0;
1420 }
1421 
1422 static void hvcs_free_index_list(void)
1423 {
1424     /* Paranoia check to be thorough. */
1425     kfree(hvcs_index_list);
1426     hvcs_index_list = NULL;
1427     hvcs_index_count = 0;
1428 }
1429 
1430 static int hvcs_initialize(void)
1431 {
1432     int rc, num_ttys_to_alloc;
1433 
1434     mutex_lock(&hvcs_init_mutex);
1435     if (hvcs_task) {
1436         mutex_unlock(&hvcs_init_mutex);
1437         return 0;
1438     }
1439 
1440     /* Has the user specified an overload with an insmod param? */
1441     if (hvcs_parm_num_devs <= 0 ||
1442         (hvcs_parm_num_devs > HVCS_MAX_SERVER_ADAPTERS)) {
1443         num_ttys_to_alloc = HVCS_DEFAULT_SERVER_ADAPTERS;
1444     } else
1445         num_ttys_to_alloc = hvcs_parm_num_devs;
1446 
1447     hvcs_tty_driver = tty_alloc_driver(num_ttys_to_alloc,
1448             TTY_DRIVER_REAL_RAW);
1449     if (IS_ERR(hvcs_tty_driver)) {
1450         mutex_unlock(&hvcs_init_mutex);
1451         return PTR_ERR(hvcs_tty_driver);
1452     }
1453 
1454     if (hvcs_alloc_index_list(num_ttys_to_alloc)) {
1455         rc = -ENOMEM;
1456         goto index_fail;
1457     }
1458 
1459     hvcs_tty_driver->driver_name = hvcs_driver_name;
1460     hvcs_tty_driver->name = hvcs_device_node;
1461 
1462     /*
1463      * We'll let the system assign us a major number, indicated by leaving
1464      * it blank.
1465      */
1466 
1467     hvcs_tty_driver->minor_start = HVCS_MINOR_START;
1468     hvcs_tty_driver->type = TTY_DRIVER_TYPE_SYSTEM;
1469 
1470     /*
1471      * We role our own so that we DONT ECHO.  We can't echo because the
1472      * device we are connecting to already echoes by default and this would
1473      * throw us into a horrible recursive echo-echo-echo loop.
1474      */
1475     hvcs_tty_driver->init_termios = hvcs_tty_termios;
1476 
1477     tty_set_operations(hvcs_tty_driver, &hvcs_ops);
1478 
1479     /*
1480      * The following call will result in sysfs entries that denote the
1481      * dynamically assigned major and minor numbers for our devices.
1482      */
1483     if (tty_register_driver(hvcs_tty_driver)) {
1484         printk(KERN_ERR "HVCS: registration as a tty driver failed.\n");
1485         rc = -EIO;
1486         goto register_fail;
1487     }
1488 
1489     hvcs_pi_buff = (unsigned long *) __get_free_page(GFP_KERNEL);
1490     if (!hvcs_pi_buff) {
1491         rc = -ENOMEM;
1492         goto buff_alloc_fail;
1493     }
1494 
1495     hvcs_task = kthread_run(khvcsd, NULL, "khvcsd");
1496     if (IS_ERR(hvcs_task)) {
1497         printk(KERN_ERR "HVCS: khvcsd creation failed.\n");
1498         rc = -EIO;
1499         goto kthread_fail;
1500     }
1501     mutex_unlock(&hvcs_init_mutex);
1502     return 0;
1503 
1504 kthread_fail:
1505     free_page((unsigned long)hvcs_pi_buff);
1506 buff_alloc_fail:
1507     tty_unregister_driver(hvcs_tty_driver);
1508 register_fail:
1509     hvcs_free_index_list();
1510 index_fail:
1511     tty_driver_kref_put(hvcs_tty_driver);
1512     hvcs_tty_driver = NULL;
1513     mutex_unlock(&hvcs_init_mutex);
1514     return rc;
1515 }
1516 
1517 static int __init hvcs_module_init(void)
1518 {
1519     int rc = vio_register_driver(&hvcs_vio_driver);
1520     if (rc) {
1521         printk(KERN_ERR "HVCS: can't register vio driver\n");
1522         return rc;
1523     }
1524 
1525     pr_info("HVCS: Driver registered.\n");
1526 
1527     /* This needs to be done AFTER the vio_register_driver() call or else
1528      * the kobjects won't be initialized properly.
1529      */
1530     rc = driver_create_file(&(hvcs_vio_driver.driver), &driver_attr_rescan);
1531     if (rc)
1532         pr_warn("HVCS: Failed to create rescan file (err %d)\n", rc);
1533 
1534     return 0;
1535 }
1536 
1537 static void __exit hvcs_module_exit(void)
1538 {
1539     /*
1540      * This driver receives hvcs_remove callbacks for each device upon
1541      * module removal.
1542      */
1543     vio_unregister_driver(&hvcs_vio_driver);
1544     if (!hvcs_task)
1545         return;
1546 
1547     /*
1548      * This synchronous operation  will wake the khvcsd kthread if it is
1549      * asleep and will return when khvcsd has terminated.
1550      */
1551     kthread_stop(hvcs_task);
1552 
1553     spin_lock(&hvcs_pi_lock);
1554     free_page((unsigned long)hvcs_pi_buff);
1555     hvcs_pi_buff = NULL;
1556     spin_unlock(&hvcs_pi_lock);
1557 
1558     driver_remove_file(&hvcs_vio_driver.driver, &driver_attr_rescan);
1559 
1560     tty_unregister_driver(hvcs_tty_driver);
1561 
1562     hvcs_free_index_list();
1563 
1564     tty_driver_kref_put(hvcs_tty_driver);
1565 
1566     printk(KERN_INFO "HVCS: driver module removed.\n");
1567 }
1568 
1569 module_init(hvcs_module_init);
1570 module_exit(hvcs_module_exit);