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 |
Stephen Rothwell | b8b572e | 2008-08-01 15:20:30 +1000 | [diff] [blame] | 117 | * arch/powerepc/include/asm/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; |
roel kluin | 3d26825 | 2008-12-02 11:21:43 +0000 | [diff] [blame] | 272 | int open_count; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1142 | memset(&hvcsd->buffer[0], 0x00, HVCS_BUFF_LEN); |
| 1143 | |
| 1144 | /* |
| 1145 | * Save these in the spinlock for the enable operations that need them |
| 1146 | * outside of the spinlock. |
| 1147 | */ |
| 1148 | irq = hvcsd->vdev->irq; |
| 1149 | vdev = hvcsd->vdev; |
| 1150 | unit_address = hvcsd->vdev->unit_address; |
| 1151 | |
| 1152 | hvcsd->todo_mask |= HVCS_SCHED_READ; |
| 1153 | spin_unlock_irqrestore(&hvcsd->lock, flags); |
| 1154 | |
| 1155 | /* |
| 1156 | * This must be done outside of the spinlock because it requests irqs |
| 1157 | * and will grab the spinlock and free the connection if it fails. |
| 1158 | */ |
| 1159 | if (((rc = hvcs_enable_device(hvcsd, unit_address, irq, vdev)))) { |
Greg Kroah-Hartman | f7eb12c | 2007-11-28 10:46:22 -0800 | [diff] [blame] | 1160 | kref_put(&hvcsd->kref, destroy_hvcs_struct); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1161 | printk(KERN_WARNING "HVCS: enable device failed.\n"); |
| 1162 | return rc; |
| 1163 | } |
| 1164 | |
| 1165 | goto open_success; |
| 1166 | |
| 1167 | fast_open: |
| 1168 | hvcsd = tty->driver_data; |
| 1169 | |
| 1170 | spin_lock_irqsave(&hvcsd->lock, flags); |
Greg Kroah-Hartman | f7eb12c | 2007-11-28 10:46:22 -0800 | [diff] [blame] | 1171 | kref_get(&hvcsd->kref); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1172 | hvcsd->open_count++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1173 | hvcsd->todo_mask |= HVCS_SCHED_READ; |
| 1174 | spin_unlock_irqrestore(&hvcsd->lock, flags); |
Greg Kroah-Hartman | f7eb12c | 2007-11-28 10:46:22 -0800 | [diff] [blame] | 1175 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1176 | open_success: |
| 1177 | hvcs_kick(); |
| 1178 | |
| 1179 | printk(KERN_INFO "HVCS: vty-server@%X connection opened.\n", |
| 1180 | hvcsd->vdev->unit_address ); |
| 1181 | |
| 1182 | return 0; |
| 1183 | |
| 1184 | error_release: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1185 | spin_unlock_irqrestore(&hvcsd->lock, flags); |
Greg Kroah-Hartman | f7eb12c | 2007-11-28 10:46:22 -0800 | [diff] [blame] | 1186 | kref_put(&hvcsd->kref, destroy_hvcs_struct); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1187 | |
| 1188 | printk(KERN_WARNING "HVCS: partner connect failed.\n"); |
| 1189 | return retval; |
| 1190 | } |
| 1191 | |
| 1192 | static void hvcs_close(struct tty_struct *tty, struct file *filp) |
| 1193 | { |
| 1194 | struct hvcs_struct *hvcsd; |
| 1195 | unsigned long flags; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1196 | int irq = NO_IRQ; |
| 1197 | |
| 1198 | /* |
| 1199 | * Is someone trying to close the file associated with this device after |
| 1200 | * we have hung up? If so tty->driver_data wouldn't be valid. |
| 1201 | */ |
| 1202 | if (tty_hung_up_p(filp)) |
| 1203 | return; |
| 1204 | |
| 1205 | /* |
| 1206 | * No driver_data means that this close was probably issued after a |
| 1207 | * failed hvcs_open by the tty layer's release_dev() api and we can just |
| 1208 | * exit cleanly. |
| 1209 | */ |
| 1210 | if (!tty->driver_data) |
| 1211 | return; |
| 1212 | |
| 1213 | hvcsd = tty->driver_data; |
| 1214 | |
| 1215 | spin_lock_irqsave(&hvcsd->lock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1216 | if (--hvcsd->open_count == 0) { |
| 1217 | |
| 1218 | vio_disable_interrupts(hvcsd->vdev); |
| 1219 | |
| 1220 | /* |
| 1221 | * NULL this early so that the kernel_thread doesn't try to |
| 1222 | * execute any operations on the TTY even though it is obligated |
| 1223 | * to deliver any pending I/O to the hypervisor. |
| 1224 | */ |
| 1225 | hvcsd->tty = NULL; |
| 1226 | |
| 1227 | irq = hvcsd->vdev->irq; |
| 1228 | spin_unlock_irqrestore(&hvcsd->lock, flags); |
| 1229 | |
| 1230 | tty_wait_until_sent(tty, HVCS_CLOSE_WAIT); |
| 1231 | |
| 1232 | /* |
| 1233 | * This line is important because it tells hvcs_open that this |
| 1234 | * device needs to be re-configured the next time hvcs_open is |
| 1235 | * called. |
| 1236 | */ |
| 1237 | tty->driver_data = NULL; |
| 1238 | |
| 1239 | free_irq(irq, hvcsd); |
Greg Kroah-Hartman | f7eb12c | 2007-11-28 10:46:22 -0800 | [diff] [blame] | 1240 | kref_put(&hvcsd->kref, destroy_hvcs_struct); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1241 | return; |
| 1242 | } else if (hvcsd->open_count < 0) { |
| 1243 | printk(KERN_ERR "HVCS: vty-server@%X open_count: %d" |
| 1244 | " is missmanaged.\n", |
| 1245 | hvcsd->vdev->unit_address, hvcsd->open_count); |
| 1246 | } |
| 1247 | |
| 1248 | spin_unlock_irqrestore(&hvcsd->lock, flags); |
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 | } |
| 1251 | |
| 1252 | static void hvcs_hangup(struct tty_struct * tty) |
| 1253 | { |
| 1254 | struct hvcs_struct *hvcsd = tty->driver_data; |
| 1255 | unsigned long flags; |
| 1256 | int temp_open_count; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1257 | int irq = NO_IRQ; |
| 1258 | |
| 1259 | spin_lock_irqsave(&hvcsd->lock, flags); |
Greg Kroah-Hartman | f7eb12c | 2007-11-28 10:46:22 -0800 | [diff] [blame] | 1260 | /* Preserve this so that we know how many kref refs to put */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1261 | temp_open_count = hvcsd->open_count; |
| 1262 | |
| 1263 | /* |
Greg Kroah-Hartman | f7eb12c | 2007-11-28 10:46:22 -0800 | [diff] [blame] | 1264 | * Don't kref put inside the spinlock because the destruction |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1265 | * 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] | 1266 | * spinlock has been released. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1267 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1268 | vio_disable_interrupts(hvcsd->vdev); |
| 1269 | |
| 1270 | hvcsd->todo_mask = 0; |
| 1271 | |
| 1272 | /* I don't think the tty needs the hvcs_struct pointer after a hangup */ |
| 1273 | hvcsd->tty->driver_data = NULL; |
| 1274 | hvcsd->tty = NULL; |
| 1275 | |
| 1276 | hvcsd->open_count = 0; |
| 1277 | |
| 1278 | /* This will drop any buffered data on the floor which is OK in a hangup |
| 1279 | * scenario. */ |
| 1280 | memset(&hvcsd->buffer[0], 0x00, HVCS_BUFF_LEN); |
| 1281 | hvcsd->chars_in_buffer = 0; |
| 1282 | |
| 1283 | irq = hvcsd->vdev->irq; |
| 1284 | |
| 1285 | spin_unlock_irqrestore(&hvcsd->lock, flags); |
| 1286 | |
| 1287 | free_irq(irq, hvcsd); |
| 1288 | |
| 1289 | /* |
Greg Kroah-Hartman | f7eb12c | 2007-11-28 10:46:22 -0800 | [diff] [blame] | 1290 | * 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] | 1291 | * tty_hangup() function doesn't invoke a close per open connection on a |
| 1292 | * non-console device. |
| 1293 | */ |
| 1294 | while(temp_open_count) { |
| 1295 | --temp_open_count; |
| 1296 | /* |
| 1297 | * The final put will trigger destruction of the hvcs_struct. |
| 1298 | * NOTE: If this hangup was signaled from user space then the |
| 1299 | * final put will never happen. |
| 1300 | */ |
Greg Kroah-Hartman | f7eb12c | 2007-11-28 10:46:22 -0800 | [diff] [blame] | 1301 | kref_put(&hvcsd->kref, destroy_hvcs_struct); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1302 | } |
| 1303 | } |
| 1304 | |
| 1305 | /* |
| 1306 | * NOTE: This is almost always from_user since user level apps interact with the |
| 1307 | * /dev nodes. I'm trusting that if hvcs_write gets called and interrupted by |
| 1308 | * hvcs_remove (which removes the target device and executes tty_hangup()) that |
| 1309 | * tty_hangup will allow hvcs_write time to complete execution before it |
| 1310 | * terminates our device. |
| 1311 | */ |
| 1312 | static int hvcs_write(struct tty_struct *tty, |
| 1313 | const unsigned char *buf, int count) |
| 1314 | { |
| 1315 | struct hvcs_struct *hvcsd = tty->driver_data; |
| 1316 | unsigned int unit_address; |
| 1317 | const unsigned char *charbuf; |
| 1318 | unsigned long flags; |
| 1319 | int total_sent = 0; |
| 1320 | int tosend = 0; |
| 1321 | int result = 0; |
| 1322 | |
| 1323 | /* |
| 1324 | * If they don't check the return code off of their open they may |
| 1325 | * attempt this even if there is no connected device. |
| 1326 | */ |
| 1327 | if (!hvcsd) |
| 1328 | return -ENODEV; |
| 1329 | |
| 1330 | /* Reasonable size to prevent user level flooding */ |
| 1331 | if (count > HVCS_MAX_FROM_USER) { |
| 1332 | printk(KERN_WARNING "HVCS write: count being truncated to" |
| 1333 | " HVCS_MAX_FROM_USER.\n"); |
| 1334 | count = HVCS_MAX_FROM_USER; |
| 1335 | } |
| 1336 | |
| 1337 | charbuf = buf; |
| 1338 | |
| 1339 | spin_lock_irqsave(&hvcsd->lock, flags); |
| 1340 | |
| 1341 | /* |
| 1342 | * Somehow an open succedded but the device was removed or the |
| 1343 | * connection terminated between the vty-server and partner vty during |
| 1344 | * the middle of a write operation? This is a crummy place to do this |
| 1345 | * but we want to keep it all in the spinlock. |
| 1346 | */ |
| 1347 | if (hvcsd->open_count <= 0) { |
| 1348 | spin_unlock_irqrestore(&hvcsd->lock, flags); |
| 1349 | return -ENODEV; |
| 1350 | } |
| 1351 | |
| 1352 | unit_address = hvcsd->vdev->unit_address; |
| 1353 | |
| 1354 | while (count > 0) { |
| 1355 | tosend = min(count, (HVCS_BUFF_LEN - hvcsd->chars_in_buffer)); |
| 1356 | /* |
| 1357 | * No more space, this probably means that the last call to |
| 1358 | * hvcs_write() didn't succeed and the buffer was filled up. |
| 1359 | */ |
| 1360 | if (!tosend) |
| 1361 | break; |
| 1362 | |
| 1363 | memcpy(&hvcsd->buffer[hvcsd->chars_in_buffer], |
| 1364 | &charbuf[total_sent], |
| 1365 | tosend); |
| 1366 | |
| 1367 | hvcsd->chars_in_buffer += tosend; |
| 1368 | |
| 1369 | result = 0; |
| 1370 | |
| 1371 | /* |
| 1372 | * If this is true then we don't want to try writing to the |
| 1373 | * hypervisor because that is the kernel_threads job now. We'll |
| 1374 | * just add to the buffer. |
| 1375 | */ |
| 1376 | if (!(hvcsd->todo_mask & HVCS_TRY_WRITE)) |
| 1377 | /* won't send partial writes */ |
| 1378 | result = hvc_put_chars(unit_address, |
| 1379 | &hvcsd->buffer[0], |
| 1380 | hvcsd->chars_in_buffer); |
| 1381 | |
| 1382 | /* |
| 1383 | * Since we know we have enough room in hvcsd->buffer for |
| 1384 | * tosend we record that it was sent regardless of whether the |
| 1385 | * hypervisor actually took it because we have it buffered. |
| 1386 | */ |
| 1387 | total_sent+=tosend; |
| 1388 | count-=tosend; |
| 1389 | if (result == 0) { |
| 1390 | hvcsd->todo_mask |= HVCS_TRY_WRITE; |
| 1391 | hvcs_kick(); |
| 1392 | break; |
| 1393 | } |
| 1394 | |
| 1395 | hvcsd->chars_in_buffer = 0; |
| 1396 | /* |
| 1397 | * Test after the chars_in_buffer reset otherwise this could |
| 1398 | * deadlock our writes if hvc_put_chars fails. |
| 1399 | */ |
| 1400 | if (result < 0) |
| 1401 | break; |
| 1402 | } |
| 1403 | |
| 1404 | spin_unlock_irqrestore(&hvcsd->lock, flags); |
| 1405 | |
| 1406 | if (result == -1) |
| 1407 | return -EIO; |
| 1408 | else |
| 1409 | return total_sent; |
| 1410 | } |
| 1411 | |
| 1412 | /* |
| 1413 | * This is really asking how much can we guarentee that we can send or that we |
| 1414 | * absolutely WILL BUFFER if we can't send it. This driver MUST honor the |
| 1415 | * return value, hence the reason for hvcs_struct buffering. |
| 1416 | */ |
| 1417 | static int hvcs_write_room(struct tty_struct *tty) |
| 1418 | { |
| 1419 | struct hvcs_struct *hvcsd = tty->driver_data; |
| 1420 | |
| 1421 | if (!hvcsd || hvcsd->open_count <= 0) |
| 1422 | return 0; |
| 1423 | |
| 1424 | return HVCS_BUFF_LEN - hvcsd->chars_in_buffer; |
| 1425 | } |
| 1426 | |
| 1427 | static int hvcs_chars_in_buffer(struct tty_struct *tty) |
| 1428 | { |
| 1429 | struct hvcs_struct *hvcsd = tty->driver_data; |
| 1430 | |
| 1431 | return hvcsd->chars_in_buffer; |
| 1432 | } |
| 1433 | |
Jeff Dike | b68e31d | 2006-10-02 02:17:18 -0700 | [diff] [blame] | 1434 | static const struct tty_operations hvcs_ops = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1435 | .open = hvcs_open, |
| 1436 | .close = hvcs_close, |
| 1437 | .hangup = hvcs_hangup, |
| 1438 | .write = hvcs_write, |
| 1439 | .write_room = hvcs_write_room, |
| 1440 | .chars_in_buffer = hvcs_chars_in_buffer, |
| 1441 | .unthrottle = hvcs_unthrottle, |
| 1442 | .throttle = hvcs_throttle, |
| 1443 | }; |
| 1444 | |
| 1445 | static int hvcs_alloc_index_list(int n) |
| 1446 | { |
| 1447 | int i; |
Jesper Juhl | 8f76078 | 2006-06-27 02:55:06 -0700 | [diff] [blame] | 1448 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1449 | hvcs_index_list = kmalloc(n * sizeof(hvcs_index_count),GFP_KERNEL); |
| 1450 | if (!hvcs_index_list) |
| 1451 | return -ENOMEM; |
| 1452 | hvcs_index_count = n; |
Jesper Juhl | 8f76078 | 2006-06-27 02:55:06 -0700 | [diff] [blame] | 1453 | for (i = 0; i < hvcs_index_count; i++) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1454 | hvcs_index_list[i] = -1; |
| 1455 | return 0; |
| 1456 | } |
| 1457 | |
| 1458 | static void hvcs_free_index_list(void) |
| 1459 | { |
| 1460 | /* Paranoia check to be thorough. */ |
Jesper Juhl | 8f76078 | 2006-06-27 02:55:06 -0700 | [diff] [blame] | 1461 | kfree(hvcs_index_list); |
| 1462 | hvcs_index_list = NULL; |
| 1463 | hvcs_index_count = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1464 | } |
| 1465 | |
| 1466 | static int __init hvcs_module_init(void) |
| 1467 | { |
| 1468 | int rc; |
| 1469 | int num_ttys_to_alloc; |
| 1470 | |
| 1471 | printk(KERN_INFO "Initializing %s\n", hvcs_driver_string); |
| 1472 | |
| 1473 | /* Has the user specified an overload with an insmod param? */ |
| 1474 | if (hvcs_parm_num_devs <= 0 || |
| 1475 | (hvcs_parm_num_devs > HVCS_MAX_SERVER_ADAPTERS)) { |
| 1476 | num_ttys_to_alloc = HVCS_DEFAULT_SERVER_ADAPTERS; |
| 1477 | } else |
| 1478 | num_ttys_to_alloc = hvcs_parm_num_devs; |
| 1479 | |
| 1480 | hvcs_tty_driver = alloc_tty_driver(num_ttys_to_alloc); |
| 1481 | if (!hvcs_tty_driver) |
| 1482 | return -ENOMEM; |
| 1483 | |
Linas Vepstas | c15bb29 | 2006-12-06 20:38:07 -0800 | [diff] [blame] | 1484 | if (hvcs_alloc_index_list(num_ttys_to_alloc)) { |
| 1485 | rc = -ENOMEM; |
| 1486 | goto index_fail; |
| 1487 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1488 | |
| 1489 | hvcs_tty_driver->owner = THIS_MODULE; |
| 1490 | |
| 1491 | hvcs_tty_driver->driver_name = hvcs_driver_name; |
| 1492 | hvcs_tty_driver->name = hvcs_device_node; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1493 | |
| 1494 | /* |
| 1495 | * We'll let the system assign us a major number, indicated by leaving |
| 1496 | * it blank. |
| 1497 | */ |
| 1498 | |
| 1499 | hvcs_tty_driver->minor_start = HVCS_MINOR_START; |
| 1500 | hvcs_tty_driver->type = TTY_DRIVER_TYPE_SYSTEM; |
| 1501 | |
| 1502 | /* |
| 1503 | * We role our own so that we DONT ECHO. We can't echo because the |
| 1504 | * device we are connecting to already echoes by default and this would |
| 1505 | * throw us into a horrible recursive echo-echo-echo loop. |
| 1506 | */ |
| 1507 | hvcs_tty_driver->init_termios = hvcs_tty_termios; |
| 1508 | hvcs_tty_driver->flags = TTY_DRIVER_REAL_RAW; |
| 1509 | |
| 1510 | tty_set_operations(hvcs_tty_driver, &hvcs_ops); |
| 1511 | |
| 1512 | /* |
| 1513 | * The following call will result in sysfs entries that denote the |
| 1514 | * dynamically assigned major and minor numbers for our devices. |
| 1515 | */ |
| 1516 | if (tty_register_driver(hvcs_tty_driver)) { |
Linas Vepstas | c15bb29 | 2006-12-06 20:38:07 -0800 | [diff] [blame] | 1517 | printk(KERN_ERR "HVCS: registration as a tty driver failed.\n"); |
| 1518 | rc = -EIO; |
| 1519 | goto register_fail; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1520 | } |
| 1521 | |
| 1522 | hvcs_pi_buff = kmalloc(PAGE_SIZE, GFP_KERNEL); |
| 1523 | if (!hvcs_pi_buff) { |
Linas Vepstas | c15bb29 | 2006-12-06 20:38:07 -0800 | [diff] [blame] | 1524 | rc = -ENOMEM; |
| 1525 | goto buff_alloc_fail; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1526 | } |
| 1527 | |
| 1528 | hvcs_task = kthread_run(khvcsd, NULL, "khvcsd"); |
| 1529 | if (IS_ERR(hvcs_task)) { |
| 1530 | printk(KERN_ERR "HVCS: khvcsd creation failed. Driver not loaded.\n"); |
Linas Vepstas | c15bb29 | 2006-12-06 20:38:07 -0800 | [diff] [blame] | 1531 | rc = -EIO; |
| 1532 | goto kthread_fail; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1533 | } |
| 1534 | |
| 1535 | rc = vio_register_driver(&hvcs_vio_driver); |
Linas Vepstas | c15bb29 | 2006-12-06 20:38:07 -0800 | [diff] [blame] | 1536 | if (rc) { |
| 1537 | printk(KERN_ERR "HVCS: can't register vio driver\n"); |
| 1538 | goto vio_fail; |
| 1539 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1540 | |
| 1541 | /* |
| 1542 | * This needs to be done AFTER the vio_register_driver() call or else |
| 1543 | * the kobjects won't be initialized properly. |
| 1544 | */ |
Linas Vepstas | c15bb29 | 2006-12-06 20:38:07 -0800 | [diff] [blame] | 1545 | rc = driver_create_file(&(hvcs_vio_driver.driver), &driver_attr_rescan); |
| 1546 | if (rc) { |
| 1547 | printk(KERN_ERR "HVCS: sysfs attr create failed\n"); |
| 1548 | goto attr_fail; |
| 1549 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1550 | |
| 1551 | printk(KERN_INFO "HVCS: driver module inserted.\n"); |
| 1552 | |
Linas Vepstas | c15bb29 | 2006-12-06 20:38:07 -0800 | [diff] [blame] | 1553 | return 0; |
| 1554 | |
| 1555 | attr_fail: |
| 1556 | vio_unregister_driver(&hvcs_vio_driver); |
| 1557 | vio_fail: |
| 1558 | kthread_stop(hvcs_task); |
| 1559 | kthread_fail: |
| 1560 | kfree(hvcs_pi_buff); |
| 1561 | buff_alloc_fail: |
| 1562 | tty_unregister_driver(hvcs_tty_driver); |
| 1563 | register_fail: |
| 1564 | hvcs_free_index_list(); |
| 1565 | index_fail: |
| 1566 | put_tty_driver(hvcs_tty_driver); |
| 1567 | hvcs_tty_driver = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1568 | return rc; |
| 1569 | } |
| 1570 | |
| 1571 | static void __exit hvcs_module_exit(void) |
| 1572 | { |
| 1573 | /* |
| 1574 | * This driver receives hvcs_remove callbacks for each device upon |
| 1575 | * module removal. |
| 1576 | */ |
| 1577 | |
| 1578 | /* |
| 1579 | * This synchronous operation will wake the khvcsd kthread if it is |
| 1580 | * asleep and will return when khvcsd has terminated. |
| 1581 | */ |
| 1582 | kthread_stop(hvcs_task); |
| 1583 | |
| 1584 | spin_lock(&hvcs_pi_lock); |
| 1585 | kfree(hvcs_pi_buff); |
| 1586 | hvcs_pi_buff = NULL; |
| 1587 | spin_unlock(&hvcs_pi_lock); |
| 1588 | |
Linas Vepstas | c15bb29 | 2006-12-06 20:38:07 -0800 | [diff] [blame] | 1589 | driver_remove_file(&hvcs_vio_driver.driver, &driver_attr_rescan); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1590 | |
| 1591 | vio_unregister_driver(&hvcs_vio_driver); |
| 1592 | |
| 1593 | tty_unregister_driver(hvcs_tty_driver); |
| 1594 | |
| 1595 | hvcs_free_index_list(); |
| 1596 | |
| 1597 | put_tty_driver(hvcs_tty_driver); |
| 1598 | |
| 1599 | printk(KERN_INFO "HVCS: driver module removed.\n"); |
| 1600 | } |
| 1601 | |
| 1602 | module_init(hvcs_module_init); |
| 1603 | module_exit(hvcs_module_exit); |