IB: Fix information leak in marshalling code
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / char / hvcs.c
blobbedc6c1b6fa5ef290db126aa9bef863f9b68fe22
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)
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.
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.
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
20 * Author(s) : Ryan S. Arnold <rsa@us.ibm.com>
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.
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.
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.
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.
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.
59 * Each vty-server, prior to being exposed to this driver is reference counted
60 * using the 2.6 Linux kernel kref construct.
62 * For direction on installation and usage of this driver please reference
63 * Documentation/powerpc/hvcs.txt.
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>
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().
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
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 similarily 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.
128 #define HVCS_DRIVER_VERSION "1.3.3"
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);
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.
139 #define HVCS_CLOSE_WAIT (HZ/100) /* 1/10 of a second */
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.
149 #define HVCS_DEFAULT_SERVER_ADAPTERS 64
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.
156 #define HVCS_MAX_SERVER_ADAPTERS 1024
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.
164 #define HVCS_MINOR_START 0
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.
173 #define __ALIGNED__ __attribute__((__aligned__(8)))
176 * How much data can firmware send with each hvc_put_chars()? Maybe this
177 * should be moved into an architecture specific area.
179 #define HVCS_BUFF_LEN 16
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.
185 #define HVCS_MAX_FROM_USER 4096
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.
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
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.
207 static int hvcs_parm_num_devs = -1;
208 module_param(hvcs_parm_num_devs, int, 0);
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";
215 /* Status of partner info rescan triggered via sysfs. */
216 static int hvcs_rescan_status;
218 static struct tty_driver *hvcs_tty_driver;
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.
233 static int *hvcs_index_list;
236 * How large is the list? This is kept for traversal since the list is
237 * dynamically created.
239 static int hvcs_index_count;
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.
245 static int hvcs_kicked;
248 * Use by the kthread construct for task operations like waking the sleeping
249 * thread and stopping the kthread.
251 static struct task_struct *hvcs_task;
254 * We allocate this for the use of all of the hvcs_structs when they fetch
255 * partner info.
257 static unsigned long *hvcs_pi_buff;
259 /* Only allow one hvcs_struct to use the hvcs_pi_buff at a time. */
260 static DEFINE_SPINLOCK(hvcs_pi_lock);
262 /* One vty-server per hvcs_struct */
263 struct hvcs_struct {
264 spinlock_t lock;
267 * This index identifies this hvcs device as the complement to a
268 * specific tty index.
270 unsigned int index;
272 struct tty_struct *tty;
273 int open_count;
276 * Used to tell the driver kernel_thread what operations need to take
277 * place upon this hvcs_struct instance.
279 int todo_mask;
282 * This buffer is required so that when hvcs_write_room() reports that
283 * it can send HVCS_BUFF_LEN characters that it will buffer the full
284 * HVCS_BUFF_LEN characters if need be. This is essential for opost
285 * writes since they do not do high level buffering and expect to be
286 * able to send what the driver commits to sending buffering
287 * [e.g. tab to space conversions in n_tty.c opost()].
289 char buffer[HVCS_BUFF_LEN];
290 int chars_in_buffer;
293 * Any variable below the kref is valid before a tty is connected and
294 * stays valid after the tty is disconnected. These shouldn't be
295 * whacked until the koject refcount reaches zero though some entries
296 * may be changed via sysfs initiatives.
298 struct kref kref; /* ref count & hvcs_struct lifetime */
299 int connected; /* is the vty-server currently connected to a vty? */
300 uint32_t p_unit_address; /* partner unit address */
301 uint32_t p_partition_ID; /* partner partition ID */
302 char p_location_code[HVCS_CLC_LENGTH + 1]; /* CLC + Null Term */
303 struct list_head next; /* list management */
304 struct vio_dev *vdev;
307 /* Required to back map a kref to its containing object */
308 #define from_kref(k) container_of(k, struct hvcs_struct, kref)
310 static LIST_HEAD(hvcs_structs);
311 static DEFINE_SPINLOCK(hvcs_structs_lock);
313 static void hvcs_unthrottle(struct tty_struct *tty);
314 static void hvcs_throttle(struct tty_struct *tty);
315 static irqreturn_t hvcs_handle_interrupt(int irq, void *dev_instance);
317 static int hvcs_write(struct tty_struct *tty,
318 const unsigned char *buf, int count);
319 static int hvcs_write_room(struct tty_struct *tty);
320 static int hvcs_chars_in_buffer(struct tty_struct *tty);
322 static int hvcs_has_pi(struct hvcs_struct *hvcsd);
323 static void hvcs_set_pi(struct hvcs_partner_info *pi,
324 struct hvcs_struct *hvcsd);
325 static int hvcs_get_pi(struct hvcs_struct *hvcsd);
326 static int hvcs_rescan_devices_list(void);
328 static int hvcs_partner_connect(struct hvcs_struct *hvcsd);
329 static void hvcs_partner_free(struct hvcs_struct *hvcsd);
331 static int hvcs_enable_device(struct hvcs_struct *hvcsd,
332 uint32_t unit_address, unsigned int irq, struct vio_dev *dev);
334 static int hvcs_open(struct tty_struct *tty, struct file *filp);
335 static void hvcs_close(struct tty_struct *tty, struct file *filp);
336 static void hvcs_hangup(struct tty_struct * tty);
338 static int __devinit hvcs_probe(struct vio_dev *dev,
339 const struct vio_device_id *id);
340 static int __devexit hvcs_remove(struct vio_dev *dev);
341 static int __init hvcs_module_init(void);
342 static void __exit hvcs_module_exit(void);
344 #define HVCS_SCHED_READ 0x00000001
345 #define HVCS_QUICK_READ 0x00000002
346 #define HVCS_TRY_WRITE 0x00000004
347 #define HVCS_READ_MASK (HVCS_SCHED_READ | HVCS_QUICK_READ)
349 static inline struct hvcs_struct *from_vio_dev(struct vio_dev *viod)
351 return dev_get_drvdata(&viod->dev);
353 /* The sysfs interface for the driver and devices */
355 static ssize_t hvcs_partner_vtys_show(struct device *dev, struct device_attribute *attr, char *buf)
357 struct vio_dev *viod = to_vio_dev(dev);
358 struct hvcs_struct *hvcsd = from_vio_dev(viod);
359 unsigned long flags;
360 int retval;
362 spin_lock_irqsave(&hvcsd->lock, flags);
363 retval = sprintf(buf, "%X\n", hvcsd->p_unit_address);
364 spin_unlock_irqrestore(&hvcsd->lock, flags);
365 return retval;
367 static DEVICE_ATTR(partner_vtys, S_IRUGO, hvcs_partner_vtys_show, NULL);
369 static ssize_t hvcs_partner_clcs_show(struct device *dev, struct device_attribute *attr, char *buf)
371 struct vio_dev *viod = to_vio_dev(dev);
372 struct hvcs_struct *hvcsd = from_vio_dev(viod);
373 unsigned long flags;
374 int retval;
376 spin_lock_irqsave(&hvcsd->lock, flags);
377 retval = sprintf(buf, "%s\n", &hvcsd->p_location_code[0]);
378 spin_unlock_irqrestore(&hvcsd->lock, flags);
379 return retval;
381 static DEVICE_ATTR(partner_clcs, S_IRUGO, hvcs_partner_clcs_show, NULL);
383 static ssize_t hvcs_current_vty_store(struct device *dev, struct device_attribute *attr, const char * buf,
384 size_t count)
387 * Don't need this feature at the present time because firmware doesn't
388 * yet support multiple partners.
390 printk(KERN_INFO "HVCS: Denied current_vty change: -EPERM.\n");
391 return -EPERM;
394 static ssize_t hvcs_current_vty_show(struct device *dev, struct device_attribute *attr, char *buf)
396 struct vio_dev *viod = to_vio_dev(dev);
397 struct hvcs_struct *hvcsd = from_vio_dev(viod);
398 unsigned long flags;
399 int retval;
401 spin_lock_irqsave(&hvcsd->lock, flags);
402 retval = sprintf(buf, "%s\n", &hvcsd->p_location_code[0]);
403 spin_unlock_irqrestore(&hvcsd->lock, flags);
404 return retval;
407 static DEVICE_ATTR(current_vty,
408 S_IRUGO | S_IWUSR, hvcs_current_vty_show, hvcs_current_vty_store);
410 static ssize_t hvcs_vterm_state_store(struct device *dev, struct device_attribute *attr, const char *buf,
411 size_t count)
413 struct vio_dev *viod = to_vio_dev(dev);
414 struct hvcs_struct *hvcsd = from_vio_dev(viod);
415 unsigned long flags;
417 /* writing a '0' to this sysfs entry will result in the disconnect. */
418 if (simple_strtol(buf, NULL, 0) != 0)
419 return -EINVAL;
421 spin_lock_irqsave(&hvcsd->lock, flags);
423 if (hvcsd->open_count > 0) {
424 spin_unlock_irqrestore(&hvcsd->lock, flags);
425 printk(KERN_INFO "HVCS: vterm state unchanged. "
426 "The hvcs device node is still in use.\n");
427 return -EPERM;
430 if (hvcsd->connected == 0) {
431 spin_unlock_irqrestore(&hvcsd->lock, flags);
432 printk(KERN_INFO "HVCS: vterm state unchanged. The"
433 " vty-server is not connected to a vty.\n");
434 return -EPERM;
437 hvcs_partner_free(hvcsd);
438 printk(KERN_INFO "HVCS: Closed vty-server@%X and"
439 " partner vty@%X:%d connection.\n",
440 hvcsd->vdev->unit_address,
441 hvcsd->p_unit_address,
442 (uint32_t)hvcsd->p_partition_ID);
444 spin_unlock_irqrestore(&hvcsd->lock, flags);
445 return count;
448 static ssize_t hvcs_vterm_state_show(struct device *dev, struct device_attribute *attr, char *buf)
450 struct vio_dev *viod = to_vio_dev(dev);
451 struct hvcs_struct *hvcsd = from_vio_dev(viod);
452 unsigned long flags;
453 int retval;
455 spin_lock_irqsave(&hvcsd->lock, flags);
456 retval = sprintf(buf, "%d\n", hvcsd->connected);
457 spin_unlock_irqrestore(&hvcsd->lock, flags);
458 return retval;
460 static DEVICE_ATTR(vterm_state, S_IRUGO | S_IWUSR,
461 hvcs_vterm_state_show, hvcs_vterm_state_store);
463 static ssize_t hvcs_index_show(struct device *dev, struct device_attribute *attr, char *buf)
465 struct vio_dev *viod = to_vio_dev(dev);
466 struct hvcs_struct *hvcsd = from_vio_dev(viod);
467 unsigned long flags;
468 int retval;
470 spin_lock_irqsave(&hvcsd->lock, flags);
471 retval = sprintf(buf, "%d\n", hvcsd->index);
472 spin_unlock_irqrestore(&hvcsd->lock, flags);
473 return retval;
476 static DEVICE_ATTR(index, S_IRUGO, hvcs_index_show, NULL);
478 static struct attribute *hvcs_attrs[] = {
479 &dev_attr_partner_vtys.attr,
480 &dev_attr_partner_clcs.attr,
481 &dev_attr_current_vty.attr,
482 &dev_attr_vterm_state.attr,
483 &dev_attr_index.attr,
484 NULL,
487 static struct attribute_group hvcs_attr_group = {
488 .attrs = hvcs_attrs,
491 static ssize_t hvcs_rescan_show(struct device_driver *ddp, char *buf)
493 /* A 1 means it is updating, a 0 means it is done updating */
494 return snprintf(buf, PAGE_SIZE, "%d\n", hvcs_rescan_status);
497 static ssize_t hvcs_rescan_store(struct device_driver *ddp, const char * buf,
498 size_t count)
500 if ((simple_strtol(buf, NULL, 0) != 1)
501 && (hvcs_rescan_status != 0))
502 return -EINVAL;
504 hvcs_rescan_status = 1;
505 printk(KERN_INFO "HVCS: rescanning partner info for all"
506 " vty-servers.\n");
507 hvcs_rescan_devices_list();
508 hvcs_rescan_status = 0;
509 return count;
512 static DRIVER_ATTR(rescan,
513 S_IRUGO | S_IWUSR, hvcs_rescan_show, hvcs_rescan_store);
515 static void hvcs_kick(void)
517 hvcs_kicked = 1;
518 wmb();
519 wake_up_process(hvcs_task);
522 static void hvcs_unthrottle(struct tty_struct *tty)
524 struct hvcs_struct *hvcsd = tty->driver_data;
525 unsigned long flags;
527 spin_lock_irqsave(&hvcsd->lock, flags);
528 hvcsd->todo_mask |= HVCS_SCHED_READ;
529 spin_unlock_irqrestore(&hvcsd->lock, flags);
530 hvcs_kick();
533 static void hvcs_throttle(struct tty_struct *tty)
535 struct hvcs_struct *hvcsd = tty->driver_data;
536 unsigned long flags;
538 spin_lock_irqsave(&hvcsd->lock, flags);
539 vio_disable_interrupts(hvcsd->vdev);
540 spin_unlock_irqrestore(&hvcsd->lock, flags);
544 * If the device is being removed we don't have to worry about this interrupt
545 * handler taking any further interrupts because they are disabled which means
546 * the hvcs_struct will always be valid in this handler.
548 static irqreturn_t hvcs_handle_interrupt(int irq, void *dev_instance)
550 struct hvcs_struct *hvcsd = dev_instance;
552 spin_lock(&hvcsd->lock);
553 vio_disable_interrupts(hvcsd->vdev);
554 hvcsd->todo_mask |= HVCS_SCHED_READ;
555 spin_unlock(&hvcsd->lock);
556 hvcs_kick();
558 return IRQ_HANDLED;
561 /* This function must be called with the hvcsd->lock held */
562 static void hvcs_try_write(struct hvcs_struct *hvcsd)
564 uint32_t unit_address = hvcsd->vdev->unit_address;
565 struct tty_struct *tty = hvcsd->tty;
566 int sent;
568 if (hvcsd->todo_mask & HVCS_TRY_WRITE) {
569 /* won't send partial writes */
570 sent = hvc_put_chars(unit_address,
571 &hvcsd->buffer[0],
572 hvcsd->chars_in_buffer );
573 if (sent > 0) {
574 hvcsd->chars_in_buffer = 0;
575 /* wmb(); */
576 hvcsd->todo_mask &= ~(HVCS_TRY_WRITE);
577 /* wmb(); */
580 * We are still obligated to deliver the data to the
581 * hypervisor even if the tty has been closed because
582 * we commited to delivering it. But don't try to wake
583 * a non-existent tty.
585 if (tty) {
586 tty_wakeup(tty);
592 static int hvcs_io(struct hvcs_struct *hvcsd)
594 uint32_t unit_address;
595 struct tty_struct *tty;
596 char buf[HVCS_BUFF_LEN] __ALIGNED__;
597 unsigned long flags;
598 int got = 0;
600 spin_lock_irqsave(&hvcsd->lock, flags);
602 unit_address = hvcsd->vdev->unit_address;
603 tty = hvcsd->tty;
605 hvcs_try_write(hvcsd);
607 if (!tty || test_bit(TTY_THROTTLED, &tty->flags)) {
608 hvcsd->todo_mask &= ~(HVCS_READ_MASK);
609 goto bail;
610 } else if (!(hvcsd->todo_mask & (HVCS_READ_MASK)))
611 goto bail;
613 /* remove the read masks */
614 hvcsd->todo_mask &= ~(HVCS_READ_MASK);
616 if (tty_buffer_request_room(tty, HVCS_BUFF_LEN) >= HVCS_BUFF_LEN) {
617 got = hvc_get_chars(unit_address,
618 &buf[0],
619 HVCS_BUFF_LEN);
620 tty_insert_flip_string(tty, buf, got);
623 /* Give the TTY time to process the data we just sent. */
624 if (got)
625 hvcsd->todo_mask |= HVCS_QUICK_READ;
627 spin_unlock_irqrestore(&hvcsd->lock, flags);
628 /* This is synch because tty->low_latency == 1 */
629 if(got)
630 tty_flip_buffer_push(tty);
632 if (!got) {
633 /* Do this _after_ the flip_buffer_push */
634 spin_lock_irqsave(&hvcsd->lock, flags);
635 vio_enable_interrupts(hvcsd->vdev);
636 spin_unlock_irqrestore(&hvcsd->lock, flags);
639 return hvcsd->todo_mask;
641 bail:
642 spin_unlock_irqrestore(&hvcsd->lock, flags);
643 return hvcsd->todo_mask;
646 static int khvcsd(void *unused)
648 struct hvcs_struct *hvcsd;
649 int hvcs_todo_mask;
651 __set_current_state(TASK_RUNNING);
653 do {
654 hvcs_todo_mask = 0;
655 hvcs_kicked = 0;
656 wmb();
658 spin_lock(&hvcs_structs_lock);
659 list_for_each_entry(hvcsd, &hvcs_structs, next) {
660 hvcs_todo_mask |= hvcs_io(hvcsd);
662 spin_unlock(&hvcs_structs_lock);
665 * If any of the hvcs adapters want to try a write or quick read
666 * don't schedule(), yield a smidgen then execute the hvcs_io
667 * thread again for those that want the write.
669 if (hvcs_todo_mask & (HVCS_TRY_WRITE | HVCS_QUICK_READ)) {
670 yield();
671 continue;
674 set_current_state(TASK_INTERRUPTIBLE);
675 if (!hvcs_kicked)
676 schedule();
677 __set_current_state(TASK_RUNNING);
678 } while (!kthread_should_stop());
680 return 0;
683 static struct vio_device_id hvcs_driver_table[] __devinitdata= {
684 {"serial-server", "hvterm2"},
685 { "", "" }
687 MODULE_DEVICE_TABLE(vio, hvcs_driver_table);
689 static void hvcs_return_index(int index)
691 /* Paranoia check */
692 if (!hvcs_index_list)
693 return;
694 if (index < 0 || index >= hvcs_index_count)
695 return;
696 if (hvcs_index_list[index] == -1)
697 return;
698 else
699 hvcs_index_list[index] = -1;
702 /* callback when the kref ref count reaches zero */
703 static void destroy_hvcs_struct(struct kref *kref)
705 struct hvcs_struct *hvcsd = from_kref(kref);
706 struct vio_dev *vdev;
707 unsigned long flags;
709 spin_lock(&hvcs_structs_lock);
710 spin_lock_irqsave(&hvcsd->lock, flags);
712 /* the list_del poisons the pointers */
713 list_del(&(hvcsd->next));
715 if (hvcsd->connected == 1) {
716 hvcs_partner_free(hvcsd);
717 printk(KERN_INFO "HVCS: Closed vty-server@%X and"
718 " partner vty@%X:%d connection.\n",
719 hvcsd->vdev->unit_address,
720 hvcsd->p_unit_address,
721 (uint32_t)hvcsd->p_partition_ID);
723 printk(KERN_INFO "HVCS: Destroyed hvcs_struct for vty-server@%X.\n",
724 hvcsd->vdev->unit_address);
726 vdev = hvcsd->vdev;
727 hvcsd->vdev = NULL;
729 hvcsd->p_unit_address = 0;
730 hvcsd->p_partition_ID = 0;
731 hvcs_return_index(hvcsd->index);
732 memset(&hvcsd->p_location_code[0], 0x00, HVCS_CLC_LENGTH + 1);
734 spin_unlock_irqrestore(&hvcsd->lock, flags);
735 spin_unlock(&hvcs_structs_lock);
737 sysfs_remove_group(&vdev->dev.kobj, &hvcs_attr_group);
739 kfree(hvcsd);
742 static int hvcs_get_index(void)
744 int i;
745 /* Paranoia check */
746 if (!hvcs_index_list) {
747 printk(KERN_ERR "HVCS: hvcs_index_list NOT valid!.\n");
748 return -EFAULT;
750 /* Find the numerically lowest first free index. */
751 for(i = 0; i < hvcs_index_count; i++) {
752 if (hvcs_index_list[i] == -1) {
753 hvcs_index_list[i] = 0;
754 return i;
757 return -1;
760 static int __devinit hvcs_probe(
761 struct vio_dev *dev,
762 const struct vio_device_id *id)
764 struct hvcs_struct *hvcsd;
765 int index;
766 int retval;
768 if (!dev || !id) {
769 printk(KERN_ERR "HVCS: probed with invalid parameter.\n");
770 return -EPERM;
773 /* early to avoid cleanup on failure */
774 index = hvcs_get_index();
775 if (index < 0) {
776 return -EFAULT;
779 hvcsd = kzalloc(sizeof(*hvcsd), GFP_KERNEL);
780 if (!hvcsd)
781 return -ENODEV;
784 spin_lock_init(&hvcsd->lock);
785 /* Automatically incs the refcount the first time */
786 kref_init(&hvcsd->kref);
788 hvcsd->vdev = dev;
789 dev_set_drvdata(&dev->dev, hvcsd);
791 hvcsd->index = index;
793 /* hvcsd->index = ++hvcs_struct_count; */
794 hvcsd->chars_in_buffer = 0;
795 hvcsd->todo_mask = 0;
796 hvcsd->connected = 0;
799 * This will populate the hvcs_struct's partner info fields for the
800 * first time.
802 if (hvcs_get_pi(hvcsd)) {
803 printk(KERN_ERR "HVCS: Failed to fetch partner"
804 " info for vty-server@%X on device probe.\n",
805 hvcsd->vdev->unit_address);
809 * If a user app opens a tty that corresponds to this vty-server before
810 * the hvcs_struct has been added to the devices list then the user app
811 * will get -ENODEV.
813 spin_lock(&hvcs_structs_lock);
814 list_add_tail(&(hvcsd->next), &hvcs_structs);
815 spin_unlock(&hvcs_structs_lock);
817 retval = sysfs_create_group(&dev->dev.kobj, &hvcs_attr_group);
818 if (retval) {
819 printk(KERN_ERR "HVCS: Can't create sysfs attrs for vty-server@%X\n",
820 hvcsd->vdev->unit_address);
821 return retval;
824 printk(KERN_INFO "HVCS: vty-server@%X added to the vio bus.\n", dev->unit_address);
827 * DON'T enable interrupts here because there is no user to receive the
828 * data.
830 return 0;
833 static int __devexit hvcs_remove(struct vio_dev *dev)
835 struct hvcs_struct *hvcsd = dev_get_drvdata(&dev->dev);
836 unsigned long flags;
837 struct tty_struct *tty;
839 if (!hvcsd)
840 return -ENODEV;
842 /* By this time the vty-server won't be getting any more interrupts */
844 spin_lock_irqsave(&hvcsd->lock, flags);
846 tty = hvcsd->tty;
848 spin_unlock_irqrestore(&hvcsd->lock, flags);
851 * Let the last holder of this object cause it to be removed, which
852 * would probably be tty_hangup below.
854 kref_put(&hvcsd->kref, destroy_hvcs_struct);
857 * The hangup is a scheduled function which will auto chain call
858 * hvcs_hangup. The tty should always be valid at this time unless a
859 * simultaneous tty close already cleaned up the hvcs_struct.
861 if (tty)
862 tty_hangup(tty);
864 printk(KERN_INFO "HVCS: vty-server@%X removed from the"
865 " vio bus.\n", dev->unit_address);
866 return 0;
869 static struct vio_driver hvcs_vio_driver = {
870 .id_table = hvcs_driver_table,
871 .probe = hvcs_probe,
872 .remove = __devexit_p(hvcs_remove),
873 .driver = {
874 .name = hvcs_driver_name,
875 .owner = THIS_MODULE,
879 /* Only called from hvcs_get_pi please */
880 static void hvcs_set_pi(struct hvcs_partner_info *pi, struct hvcs_struct *hvcsd)
882 int clclength;
884 hvcsd->p_unit_address = pi->unit_address;
885 hvcsd->p_partition_ID = pi->partition_ID;
886 clclength = strlen(&pi->location_code[0]);
887 if (clclength > HVCS_CLC_LENGTH)
888 clclength = HVCS_CLC_LENGTH;
890 /* copy the null-term char too */
891 strncpy(&hvcsd->p_location_code[0],
892 &pi->location_code[0], clclength + 1);
896 * Traverse the list and add the partner info that is found to the hvcs_struct
897 * struct entry. NOTE: At this time I know that partner info will return a
898 * single entry but in the future there may be multiple partner info entries per
899 * vty-server and you'll want to zero out that list and reset it. If for some
900 * reason you have an old version of this driver but there IS more than one
901 * partner info then hvcsd->p_* will hold the last partner info data from the
902 * firmware query. A good way to update this code would be to replace the three
903 * partner info fields in hvcs_struct with a list of hvcs_partner_info
904 * instances.
906 * This function must be called with the hvcsd->lock held.
908 static int hvcs_get_pi(struct hvcs_struct *hvcsd)
910 struct hvcs_partner_info *pi;
911 uint32_t unit_address = hvcsd->vdev->unit_address;
912 struct list_head head;
913 int retval;
915 spin_lock(&hvcs_pi_lock);
916 if (!hvcs_pi_buff) {
917 spin_unlock(&hvcs_pi_lock);
918 return -EFAULT;
920 retval = hvcs_get_partner_info(unit_address, &head, hvcs_pi_buff);
921 spin_unlock(&hvcs_pi_lock);
922 if (retval) {
923 printk(KERN_ERR "HVCS: Failed to fetch partner"
924 " info for vty-server@%x.\n", unit_address);
925 return retval;
928 /* nixes the values if the partner vty went away */
929 hvcsd->p_unit_address = 0;
930 hvcsd->p_partition_ID = 0;
932 list_for_each_entry(pi, &head, node)
933 hvcs_set_pi(pi, hvcsd);
935 hvcs_free_partner_info(&head);
936 return 0;
940 * This function is executed by the driver "rescan" sysfs entry. It shouldn't
941 * be executed elsewhere, in order to prevent deadlock issues.
943 static int hvcs_rescan_devices_list(void)
945 struct hvcs_struct *hvcsd;
946 unsigned long flags;
948 spin_lock(&hvcs_structs_lock);
950 list_for_each_entry(hvcsd, &hvcs_structs, next) {
951 spin_lock_irqsave(&hvcsd->lock, flags);
952 hvcs_get_pi(hvcsd);
953 spin_unlock_irqrestore(&hvcsd->lock, flags);
956 spin_unlock(&hvcs_structs_lock);
958 return 0;
962 * Farm this off into its own function because it could be more complex once
963 * multiple partners support is added. This function should be called with
964 * the hvcsd->lock held.
966 static int hvcs_has_pi(struct hvcs_struct *hvcsd)
968 if ((!hvcsd->p_unit_address) || (!hvcsd->p_partition_ID))
969 return 0;
970 return 1;
974 * NOTE: It is possible that the super admin removed a partner vty and then
975 * added a different vty as the new partner.
977 * This function must be called with the hvcsd->lock held.
979 static int hvcs_partner_connect(struct hvcs_struct *hvcsd)
981 int retval;
982 unsigned int unit_address = hvcsd->vdev->unit_address;
985 * If there wasn't any pi when the device was added it doesn't meant
986 * there isn't any now. This driver isn't notified when a new partner
987 * vty is added to a vty-server so we discover changes on our own.
988 * Please see comments in hvcs_register_connection() for justification
989 * of this bizarre code.
991 retval = hvcs_register_connection(unit_address,
992 hvcsd->p_partition_ID,
993 hvcsd->p_unit_address);
994 if (!retval) {
995 hvcsd->connected = 1;
996 return 0;
997 } else if (retval != -EINVAL)
998 return retval;
1001 * As per the spec re-get the pi and try again if -EINVAL after the
1002 * first connection attempt.
1004 if (hvcs_get_pi(hvcsd))
1005 return -ENOMEM;
1007 if (!hvcs_has_pi(hvcsd))
1008 return -ENODEV;
1010 retval = hvcs_register_connection(unit_address,
1011 hvcsd->p_partition_ID,
1012 hvcsd->p_unit_address);
1013 if (retval != -EINVAL) {
1014 hvcsd->connected = 1;
1015 return retval;
1019 * EBUSY is the most likely scenario though the vty could have been
1020 * removed or there really could be an hcall error due to the parameter
1021 * data but thanks to ambiguous firmware return codes we can't really
1022 * tell.
1024 printk(KERN_INFO "HVCS: vty-server or partner"
1025 " vty is busy. Try again later.\n");
1026 return -EBUSY;
1029 /* This function must be called with the hvcsd->lock held */
1030 static void hvcs_partner_free(struct hvcs_struct *hvcsd)
1032 int retval;
1033 do {
1034 retval = hvcs_free_connection(hvcsd->vdev->unit_address);
1035 } while (retval == -EBUSY);
1036 hvcsd->connected = 0;
1039 /* This helper function must be called WITHOUT the hvcsd->lock held */
1040 static int hvcs_enable_device(struct hvcs_struct *hvcsd, uint32_t unit_address,
1041 unsigned int irq, struct vio_dev *vdev)
1043 unsigned long flags;
1044 int rc;
1047 * It is possible that the vty-server was removed between the time that
1048 * the conn was registered and now.
1050 if (!(rc = request_irq(irq, &hvcs_handle_interrupt,
1051 IRQF_DISABLED, "ibmhvcs", hvcsd))) {
1053 * It is possible the vty-server was removed after the irq was
1054 * requested but before we have time to enable interrupts.
1056 if (vio_enable_interrupts(vdev) == H_SUCCESS)
1057 return 0;
1058 else {
1059 printk(KERN_ERR "HVCS: int enable failed for"
1060 " vty-server@%X.\n", unit_address);
1061 free_irq(irq, hvcsd);
1063 } else
1064 printk(KERN_ERR "HVCS: irq req failed for"
1065 " vty-server@%X.\n", unit_address);
1067 spin_lock_irqsave(&hvcsd->lock, flags);
1068 hvcs_partner_free(hvcsd);
1069 spin_unlock_irqrestore(&hvcsd->lock, flags);
1071 return rc;
1076 * This always increments the kref ref count if the call is successful.
1077 * Please remember to dec when you are done with the instance.
1079 * NOTICE: Do NOT hold either the hvcs_struct.lock or hvcs_structs_lock when
1080 * calling this function or you will get deadlock.
1082 static struct hvcs_struct *hvcs_get_by_index(int index)
1084 struct hvcs_struct *hvcsd = NULL;
1085 unsigned long flags;
1087 spin_lock(&hvcs_structs_lock);
1088 /* We can immediately discard OOB requests */
1089 if (index >= 0 && index < HVCS_MAX_SERVER_ADAPTERS) {
1090 list_for_each_entry(hvcsd, &hvcs_structs, next) {
1091 spin_lock_irqsave(&hvcsd->lock, flags);
1092 if (hvcsd->index == index) {
1093 kref_get(&hvcsd->kref);
1094 spin_unlock_irqrestore(&hvcsd->lock, flags);
1095 spin_unlock(&hvcs_structs_lock);
1096 return hvcsd;
1098 spin_unlock_irqrestore(&hvcsd->lock, flags);
1100 hvcsd = NULL;
1103 spin_unlock(&hvcs_structs_lock);
1104 return hvcsd;
1108 * This is invoked via the tty_open interface when a user app connects to the
1109 * /dev node.
1111 static int hvcs_open(struct tty_struct *tty, struct file *filp)
1113 struct hvcs_struct *hvcsd;
1114 int rc, retval = 0;
1115 unsigned long flags;
1116 unsigned int irq;
1117 struct vio_dev *vdev;
1118 unsigned long unit_address;
1120 if (tty->driver_data)
1121 goto fast_open;
1124 * Is there a vty-server that shares the same index?
1125 * This function increments the kref index.
1127 if (!(hvcsd = hvcs_get_by_index(tty->index))) {
1128 printk(KERN_WARNING "HVCS: open failed, no device associated"
1129 " with tty->index %d.\n", tty->index);
1130 return -ENODEV;
1133 spin_lock_irqsave(&hvcsd->lock, flags);
1135 if (hvcsd->connected == 0)
1136 if ((retval = hvcs_partner_connect(hvcsd)))
1137 goto error_release;
1139 hvcsd->open_count = 1;
1140 hvcsd->tty = tty;
1141 tty->driver_data = hvcsd;
1143 memset(&hvcsd->buffer[0], 0x00, HVCS_BUFF_LEN);
1146 * Save these in the spinlock for the enable operations that need them
1147 * outside of the spinlock.
1149 irq = hvcsd->vdev->irq;
1150 vdev = hvcsd->vdev;
1151 unit_address = hvcsd->vdev->unit_address;
1153 hvcsd->todo_mask |= HVCS_SCHED_READ;
1154 spin_unlock_irqrestore(&hvcsd->lock, flags);
1157 * This must be done outside of the spinlock because it requests irqs
1158 * and will grab the spinlock and free the connection if it fails.
1160 if (((rc = hvcs_enable_device(hvcsd, unit_address, irq, vdev)))) {
1161 kref_put(&hvcsd->kref, destroy_hvcs_struct);
1162 printk(KERN_WARNING "HVCS: enable device failed.\n");
1163 return rc;
1166 goto open_success;
1168 fast_open:
1169 hvcsd = tty->driver_data;
1171 spin_lock_irqsave(&hvcsd->lock, flags);
1172 kref_get(&hvcsd->kref);
1173 hvcsd->open_count++;
1174 hvcsd->todo_mask |= HVCS_SCHED_READ;
1175 spin_unlock_irqrestore(&hvcsd->lock, flags);
1177 open_success:
1178 hvcs_kick();
1180 printk(KERN_INFO "HVCS: vty-server@%X connection opened.\n",
1181 hvcsd->vdev->unit_address );
1183 return 0;
1185 error_release:
1186 spin_unlock_irqrestore(&hvcsd->lock, flags);
1187 kref_put(&hvcsd->kref, destroy_hvcs_struct);
1189 printk(KERN_WARNING "HVCS: partner connect failed.\n");
1190 return retval;
1193 static void hvcs_close(struct tty_struct *tty, struct file *filp)
1195 struct hvcs_struct *hvcsd;
1196 unsigned long flags;
1197 int irq = NO_IRQ;
1200 * Is someone trying to close the file associated with this device after
1201 * we have hung up? If so tty->driver_data wouldn't be valid.
1203 if (tty_hung_up_p(filp))
1204 return;
1207 * No driver_data means that this close was probably issued after a
1208 * failed hvcs_open by the tty layer's release_dev() api and we can just
1209 * exit cleanly.
1211 if (!tty->driver_data)
1212 return;
1214 hvcsd = tty->driver_data;
1216 spin_lock_irqsave(&hvcsd->lock, flags);
1217 if (--hvcsd->open_count == 0) {
1219 vio_disable_interrupts(hvcsd->vdev);
1222 * NULL this early so that the kernel_thread doesn't try to
1223 * execute any operations on the TTY even though it is obligated
1224 * to deliver any pending I/O to the hypervisor.
1226 hvcsd->tty = NULL;
1228 irq = hvcsd->vdev->irq;
1229 spin_unlock_irqrestore(&hvcsd->lock, flags);
1231 tty_wait_until_sent(tty, HVCS_CLOSE_WAIT);
1234 * This line is important because it tells hvcs_open that this
1235 * device needs to be re-configured the next time hvcs_open is
1236 * called.
1238 tty->driver_data = NULL;
1240 free_irq(irq, hvcsd);
1241 kref_put(&hvcsd->kref, destroy_hvcs_struct);
1242 return;
1243 } else if (hvcsd->open_count < 0) {
1244 printk(KERN_ERR "HVCS: vty-server@%X open_count: %d"
1245 " is missmanaged.\n",
1246 hvcsd->vdev->unit_address, hvcsd->open_count);
1249 spin_unlock_irqrestore(&hvcsd->lock, flags);
1250 kref_put(&hvcsd->kref, destroy_hvcs_struct);
1253 static void hvcs_hangup(struct tty_struct * tty)
1255 struct hvcs_struct *hvcsd = tty->driver_data;
1256 unsigned long flags;
1257 int temp_open_count;
1258 int irq = NO_IRQ;
1260 spin_lock_irqsave(&hvcsd->lock, flags);
1261 /* Preserve this so that we know how many kref refs to put */
1262 temp_open_count = hvcsd->open_count;
1265 * Don't kref put inside the spinlock because the destruction
1266 * callback may use the spinlock and it may get called before the
1267 * spinlock has been released.
1269 vio_disable_interrupts(hvcsd->vdev);
1271 hvcsd->todo_mask = 0;
1273 /* I don't think the tty needs the hvcs_struct pointer after a hangup */
1274 hvcsd->tty->driver_data = NULL;
1275 hvcsd->tty = NULL;
1277 hvcsd->open_count = 0;
1279 /* This will drop any buffered data on the floor which is OK in a hangup
1280 * scenario. */
1281 memset(&hvcsd->buffer[0], 0x00, HVCS_BUFF_LEN);
1282 hvcsd->chars_in_buffer = 0;
1284 irq = hvcsd->vdev->irq;
1286 spin_unlock_irqrestore(&hvcsd->lock, flags);
1288 free_irq(irq, hvcsd);
1291 * We need to kref_put() for every open_count we have since the
1292 * tty_hangup() function doesn't invoke a close per open connection on a
1293 * non-console device.
1295 while(temp_open_count) {
1296 --temp_open_count;
1298 * The final put will trigger destruction of the hvcs_struct.
1299 * NOTE: If this hangup was signaled from user space then the
1300 * final put will never happen.
1302 kref_put(&hvcsd->kref, destroy_hvcs_struct);
1307 * NOTE: This is almost always from_user since user level apps interact with the
1308 * /dev nodes. I'm trusting that if hvcs_write gets called and interrupted by
1309 * hvcs_remove (which removes the target device and executes tty_hangup()) that
1310 * tty_hangup will allow hvcs_write time to complete execution before it
1311 * terminates our device.
1313 static int hvcs_write(struct tty_struct *tty,
1314 const unsigned char *buf, int count)
1316 struct hvcs_struct *hvcsd = tty->driver_data;
1317 unsigned int unit_address;
1318 const unsigned char *charbuf;
1319 unsigned long flags;
1320 int total_sent = 0;
1321 int tosend = 0;
1322 int result = 0;
1325 * If they don't check the return code off of their open they may
1326 * attempt this even if there is no connected device.
1328 if (!hvcsd)
1329 return -ENODEV;
1331 /* Reasonable size to prevent user level flooding */
1332 if (count > HVCS_MAX_FROM_USER) {
1333 printk(KERN_WARNING "HVCS write: count being truncated to"
1334 " HVCS_MAX_FROM_USER.\n");
1335 count = HVCS_MAX_FROM_USER;
1338 charbuf = buf;
1340 spin_lock_irqsave(&hvcsd->lock, flags);
1343 * Somehow an open succedded but the device was removed or the
1344 * connection terminated between the vty-server and partner vty during
1345 * the middle of a write operation? This is a crummy place to do this
1346 * but we want to keep it all in the spinlock.
1348 if (hvcsd->open_count <= 0) {
1349 spin_unlock_irqrestore(&hvcsd->lock, flags);
1350 return -ENODEV;
1353 unit_address = hvcsd->vdev->unit_address;
1355 while (count > 0) {
1356 tosend = min(count, (HVCS_BUFF_LEN - hvcsd->chars_in_buffer));
1358 * No more space, this probably means that the last call to
1359 * hvcs_write() didn't succeed and the buffer was filled up.
1361 if (!tosend)
1362 break;
1364 memcpy(&hvcsd->buffer[hvcsd->chars_in_buffer],
1365 &charbuf[total_sent],
1366 tosend);
1368 hvcsd->chars_in_buffer += tosend;
1370 result = 0;
1373 * If this is true then we don't want to try writing to the
1374 * hypervisor because that is the kernel_threads job now. We'll
1375 * just add to the buffer.
1377 if (!(hvcsd->todo_mask & HVCS_TRY_WRITE))
1378 /* won't send partial writes */
1379 result = hvc_put_chars(unit_address,
1380 &hvcsd->buffer[0],
1381 hvcsd->chars_in_buffer);
1384 * Since we know we have enough room in hvcsd->buffer for
1385 * tosend we record that it was sent regardless of whether the
1386 * hypervisor actually took it because we have it buffered.
1388 total_sent+=tosend;
1389 count-=tosend;
1390 if (result == 0) {
1391 hvcsd->todo_mask |= HVCS_TRY_WRITE;
1392 hvcs_kick();
1393 break;
1396 hvcsd->chars_in_buffer = 0;
1398 * Test after the chars_in_buffer reset otherwise this could
1399 * deadlock our writes if hvc_put_chars fails.
1401 if (result < 0)
1402 break;
1405 spin_unlock_irqrestore(&hvcsd->lock, flags);
1407 if (result == -1)
1408 return -EIO;
1409 else
1410 return total_sent;
1414 * This is really asking how much can we guarentee that we can send or that we
1415 * absolutely WILL BUFFER if we can't send it. This driver MUST honor the
1416 * return value, hence the reason for hvcs_struct buffering.
1418 static int hvcs_write_room(struct tty_struct *tty)
1420 struct hvcs_struct *hvcsd = tty->driver_data;
1422 if (!hvcsd || hvcsd->open_count <= 0)
1423 return 0;
1425 return HVCS_BUFF_LEN - hvcsd->chars_in_buffer;
1428 static int hvcs_chars_in_buffer(struct tty_struct *tty)
1430 struct hvcs_struct *hvcsd = tty->driver_data;
1432 return hvcsd->chars_in_buffer;
1435 static const struct tty_operations hvcs_ops = {
1436 .open = hvcs_open,
1437 .close = hvcs_close,
1438 .hangup = hvcs_hangup,
1439 .write = hvcs_write,
1440 .write_room = hvcs_write_room,
1441 .chars_in_buffer = hvcs_chars_in_buffer,
1442 .unthrottle = hvcs_unthrottle,
1443 .throttle = hvcs_throttle,
1446 static int hvcs_alloc_index_list(int n)
1448 int i;
1450 hvcs_index_list = kmalloc(n * sizeof(hvcs_index_count),GFP_KERNEL);
1451 if (!hvcs_index_list)
1452 return -ENOMEM;
1453 hvcs_index_count = n;
1454 for (i = 0; i < hvcs_index_count; i++)
1455 hvcs_index_list[i] = -1;
1456 return 0;
1459 static void hvcs_free_index_list(void)
1461 /* Paranoia check to be thorough. */
1462 kfree(hvcs_index_list);
1463 hvcs_index_list = NULL;
1464 hvcs_index_count = 0;
1467 static int __init hvcs_module_init(void)
1469 int rc;
1470 int num_ttys_to_alloc;
1472 printk(KERN_INFO "Initializing %s\n", hvcs_driver_string);
1474 /* Has the user specified an overload with an insmod param? */
1475 if (hvcs_parm_num_devs <= 0 ||
1476 (hvcs_parm_num_devs > HVCS_MAX_SERVER_ADAPTERS)) {
1477 num_ttys_to_alloc = HVCS_DEFAULT_SERVER_ADAPTERS;
1478 } else
1479 num_ttys_to_alloc = hvcs_parm_num_devs;
1481 hvcs_tty_driver = alloc_tty_driver(num_ttys_to_alloc);
1482 if (!hvcs_tty_driver)
1483 return -ENOMEM;
1485 if (hvcs_alloc_index_list(num_ttys_to_alloc)) {
1486 rc = -ENOMEM;
1487 goto index_fail;
1490 hvcs_tty_driver->owner = THIS_MODULE;
1492 hvcs_tty_driver->driver_name = hvcs_driver_name;
1493 hvcs_tty_driver->name = hvcs_device_node;
1496 * We'll let the system assign us a major number, indicated by leaving
1497 * it blank.
1500 hvcs_tty_driver->minor_start = HVCS_MINOR_START;
1501 hvcs_tty_driver->type = TTY_DRIVER_TYPE_SYSTEM;
1504 * We role our own so that we DONT ECHO. We can't echo because the
1505 * device we are connecting to already echoes by default and this would
1506 * throw us into a horrible recursive echo-echo-echo loop.
1508 hvcs_tty_driver->init_termios = hvcs_tty_termios;
1509 hvcs_tty_driver->flags = TTY_DRIVER_REAL_RAW;
1511 tty_set_operations(hvcs_tty_driver, &hvcs_ops);
1514 * The following call will result in sysfs entries that denote the
1515 * dynamically assigned major and minor numbers for our devices.
1517 if (tty_register_driver(hvcs_tty_driver)) {
1518 printk(KERN_ERR "HVCS: registration as a tty driver failed.\n");
1519 rc = -EIO;
1520 goto register_fail;
1523 hvcs_pi_buff = kmalloc(PAGE_SIZE, GFP_KERNEL);
1524 if (!hvcs_pi_buff) {
1525 rc = -ENOMEM;
1526 goto buff_alloc_fail;
1529 hvcs_task = kthread_run(khvcsd, NULL, "khvcsd");
1530 if (IS_ERR(hvcs_task)) {
1531 printk(KERN_ERR "HVCS: khvcsd creation failed. Driver not loaded.\n");
1532 rc = -EIO;
1533 goto kthread_fail;
1536 rc = vio_register_driver(&hvcs_vio_driver);
1537 if (rc) {
1538 printk(KERN_ERR "HVCS: can't register vio driver\n");
1539 goto vio_fail;
1543 * This needs to be done AFTER the vio_register_driver() call or else
1544 * the kobjects won't be initialized properly.
1546 rc = driver_create_file(&(hvcs_vio_driver.driver), &driver_attr_rescan);
1547 if (rc) {
1548 printk(KERN_ERR "HVCS: sysfs attr create failed\n");
1549 goto attr_fail;
1552 printk(KERN_INFO "HVCS: driver module inserted.\n");
1554 return 0;
1556 attr_fail:
1557 vio_unregister_driver(&hvcs_vio_driver);
1558 vio_fail:
1559 kthread_stop(hvcs_task);
1560 kthread_fail:
1561 kfree(hvcs_pi_buff);
1562 buff_alloc_fail:
1563 tty_unregister_driver(hvcs_tty_driver);
1564 register_fail:
1565 hvcs_free_index_list();
1566 index_fail:
1567 put_tty_driver(hvcs_tty_driver);
1568 hvcs_tty_driver = NULL;
1569 return rc;
1572 static void __exit hvcs_module_exit(void)
1575 * This driver receives hvcs_remove callbacks for each device upon
1576 * module removal.
1580 * This synchronous operation will wake the khvcsd kthread if it is
1581 * asleep and will return when khvcsd has terminated.
1583 kthread_stop(hvcs_task);
1585 spin_lock(&hvcs_pi_lock);
1586 kfree(hvcs_pi_buff);
1587 hvcs_pi_buff = NULL;
1588 spin_unlock(&hvcs_pi_lock);
1590 driver_remove_file(&hvcs_vio_driver.driver, &driver_attr_rescan);
1592 vio_unregister_driver(&hvcs_vio_driver);
1594 tty_unregister_driver(hvcs_tty_driver);
1596 hvcs_free_index_list();
1598 put_tty_driver(hvcs_tty_driver);
1600 printk(KERN_INFO "HVCS: driver module removed.\n");
1603 module_init(hvcs_module_init);
1604 module_exit(hvcs_module_exit);