Linux Audio

Check our new training course

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