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