Linux Audio

Check our new training course

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