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