ia64/linux-2.6.18-xen.hg

annotate drivers/char/hvcs.c @ 893:f994bfe9b93b

linux/blktap2: reduce TLB flush scope

c/s 885 added very coarse TLB flushing. Since these flushes always
follow single page updates, single page flushes (when available) are
sufficient.

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