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