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