Merge git://git.kernel.org/pub/scm/linux/kernel/git/mason/btrfs-unstable
[linux-2.6/mini2440.git] / drivers / char / hvcs.c
blobc76bccf5354dc77f1b97bdb3ea22266d8154faa5
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/spinlock.h>
78 #include <linux/stat.h>
79 #include <linux/tty.h>
80 #include <linux/tty_flip.h>
81 #include <asm/hvconsole.h>
82 #include <asm/hvcserver.h>
83 #include <asm/uaccess.h>
84 #include <asm/vio.h>
87 * 1.3.0 -> 1.3.1 In hvcs_open memset(..,0x00,..) instead of memset(..,0x3F,00).
88 * Removed braces around single statements following conditionals. Removed '=
89 * 0' after static int declarations since these default to zero. Removed
90 * list_for_each_safe() and replaced with list_for_each_entry() in
91 * hvcs_get_by_index(). The 'safe' version is un-needed now that the driver is
92 * using spinlocks. Changed spin_lock_irqsave() to spin_lock() when locking
93 * hvcs_structs_lock and hvcs_pi_lock since these are not touched in an int
94 * handler. Initialized hvcs_structs_lock and hvcs_pi_lock to
95 * SPIN_LOCK_UNLOCKED at declaration time rather than in hvcs_module_init().
96 * Added spin_lock around list_del() in destroy_hvcs_struct() to protect the
97 * list traversals from a deletion. Removed '= NULL' from pointer declaration
98 * statements since they are initialized NULL by default. Removed wmb()
99 * instances from hvcs_try_write(). They probably aren't needed with locking in
100 * place. Added check and cleanup for hvcs_pi_buff = kmalloc() in
101 * hvcs_module_init(). Exposed hvcs_struct.index via a sysfs attribute so that
102 * the coupling between /dev/hvcs* and a vty-server can be automatically
103 * determined. Moved kobject_put() in hvcs_open outside of the
104 * spin_unlock_irqrestore().
106 * 1.3.1 -> 1.3.2 Changed method for determining hvcs_struct->index and had it
107 * align with how the tty layer always assigns the lowest index available. This
108 * change resulted in a list of ints that denotes which indexes are available.
109 * Device additions and removals use the new hvcs_get_index() and
110 * hvcs_return_index() helper functions. The list is created with
111 * hvsc_alloc_index_list() and it is destroyed with hvcs_free_index_list().
112 * Without these fixes hotplug vty-server adapter support goes crazy with this
113 * driver if the user removes a vty-server adapter. Moved free_irq() outside of
114 * the hvcs_final_close() function in order to get it out of the spinlock.
115 * Rearranged hvcs_close(). Cleaned up some printks and did some housekeeping
116 * on the changelog. Removed local CLC_LENGTH and used HVCS_CLC_LENGTH from
117 * arch/powerepc/include/asm/hvcserver.h
119 * 1.3.2 -> 1.3.3 Replaced yield() in hvcs_close() with tty_wait_until_sent() to
120 * prevent possible lockup with realtime scheduling as similarily pointed out by
121 * akpm in hvc_console. Changed resulted in the removal of hvcs_final_close()
122 * to reorder cleanup operations and prevent discarding of pending data during
123 * an hvcs_close(). Removed spinlock protection of hvcs_struct data members in
124 * hvcs_write_room() and hvcs_chars_in_buffer() because they aren't needed.
127 #define HVCS_DRIVER_VERSION "1.3.3"
129 MODULE_AUTHOR("Ryan S. Arnold <rsa@us.ibm.com>");
130 MODULE_DESCRIPTION("IBM hvcs (Hypervisor Virtual Console Server) Driver");
131 MODULE_LICENSE("GPL");
132 MODULE_VERSION(HVCS_DRIVER_VERSION);
135 * Wait this long per iteration while trying to push buffered data to the
136 * hypervisor before allowing the tty to complete a close operation.
138 #define HVCS_CLOSE_WAIT (HZ/100) /* 1/10 of a second */
141 * Since the Linux TTY code does not currently (2-04-2004) support dynamic
142 * addition of tty derived devices and we shouldn't allocate thousands of
143 * tty_device pointers when the number of vty-server & vty partner connections
144 * will most often be much lower than this, we'll arbitrarily allocate
145 * HVCS_DEFAULT_SERVER_ADAPTERS tty_structs and cdev's by default when we
146 * register the tty_driver. This can be overridden using an insmod parameter.
148 #define HVCS_DEFAULT_SERVER_ADAPTERS 64
151 * The user can't insmod with more than HVCS_MAX_SERVER_ADAPTERS hvcs device
152 * nodes as a sanity check. Theoretically there can be over 1 Billion
153 * vty-server & vty partner connections.
155 #define HVCS_MAX_SERVER_ADAPTERS 1024
158 * We let Linux assign us a major number and we start the minors at zero. There
159 * is no intuitive mapping between minor number and the target vty-server
160 * adapter except that each new vty-server adapter is always assigned to the
161 * smallest minor number available.
163 #define HVCS_MINOR_START 0
166 * The hcall interface involves putting 8 chars into each of two registers.
167 * We load up those 2 registers (in arch/powerpc/platforms/pseries/hvconsole.c)
168 * by casting char[16] to long[2]. It would work without __ALIGNED__, but a
169 * little (tiny) bit slower because an unaligned load is slower than aligned
170 * load.
172 #define __ALIGNED__ __attribute__((__aligned__(8)))
175 * How much data can firmware send with each hvc_put_chars()? Maybe this
176 * should be moved into an architecture specific area.
178 #define HVCS_BUFF_LEN 16
181 * This is the maximum amount of data we'll let the user send us (hvcs_write) at
182 * once in a chunk as a sanity check.
184 #define HVCS_MAX_FROM_USER 4096
187 * Be careful when adding flags to this line discipline. Don't add anything
188 * that will cause echoing or we'll go into recursive loop echoing chars back
189 * and forth with the console drivers.
191 static struct ktermios hvcs_tty_termios = {
192 .c_iflag = IGNBRK | IGNPAR,
193 .c_oflag = OPOST,
194 .c_cflag = B38400 | CS8 | CREAD | HUPCL,
195 .c_cc = INIT_C_CC,
196 .c_ispeed = 38400,
197 .c_ospeed = 38400
201 * This value is used to take the place of a command line parameter when the
202 * module is inserted. It starts as -1 and stays as such if the user doesn't
203 * specify a module insmod parameter. If they DO specify one then it is set to
204 * the value of the integer passed in.
206 static int hvcs_parm_num_devs = -1;
207 module_param(hvcs_parm_num_devs, int, 0);
209 static const char hvcs_driver_name[] = "hvcs";
210 static const char hvcs_device_node[] = "hvcs";
211 static const char hvcs_driver_string[]
212 = "IBM hvcs (Hypervisor Virtual Console Server) Driver";
214 /* Status of partner info rescan triggered via sysfs. */
215 static int hvcs_rescan_status;
217 static struct tty_driver *hvcs_tty_driver;
220 * In order to be somewhat sane this driver always associates the hvcs_struct
221 * index element with the numerically equal tty->index. This means that a
222 * hotplugged vty-server adapter will always map to the lowest index valued
223 * device node. If vty-servers were hotplug removed from the system and then
224 * new ones added the new vty-server may have the largest slot number of all
225 * the vty-server adapters in the partition but it may have the lowest dev node
226 * index of all the adapters due to the hole left by the hotplug removed
227 * adapter. There are a set of functions provided to get the lowest index for
228 * a new device as well as return the index to the list. This list is allocated
229 * with a number of elements equal to the number of device nodes requested when
230 * the module was inserted.
232 static int *hvcs_index_list;
235 * How large is the list? This is kept for traversal since the list is
236 * dynamically created.
238 static int hvcs_index_count;
241 * Used by the khvcsd to pick up I/O operations when the kernel_thread is
242 * already awake but potentially shifted to TASK_INTERRUPTIBLE state.
244 static int hvcs_kicked;
247 * Use by the kthread construct for task operations like waking the sleeping
248 * thread and stopping the kthread.
250 static struct task_struct *hvcs_task;
253 * We allocate this for the use of all of the hvcs_structs when they fetch
254 * partner info.
256 static unsigned long *hvcs_pi_buff;
258 /* Only allow one hvcs_struct to use the hvcs_pi_buff at a time. */
259 static DEFINE_SPINLOCK(hvcs_pi_lock);
261 /* One vty-server per hvcs_struct */
262 struct hvcs_struct {
263 spinlock_t lock;
266 * This index identifies this hvcs device as the complement to a
267 * specific tty index.
269 unsigned int index;
271 struct tty_struct *tty;
272 int open_count;
275 * Used to tell the driver kernel_thread what operations need to take
276 * place upon this hvcs_struct instance.
278 int todo_mask;
281 * This buffer is required so that when hvcs_write_room() reports that
282 * it can send HVCS_BUFF_LEN characters that it will buffer the full
283 * HVCS_BUFF_LEN characters if need be. This is essential for opost
284 * writes since they do not do high level buffering and expect to be
285 * able to send what the driver commits to sending buffering
286 * [e.g. tab to space conversions in n_tty.c opost()].
288 char buffer[HVCS_BUFF_LEN];
289 int chars_in_buffer;
292 * Any variable below the kref is valid before a tty is connected and
293 * stays valid after the tty is disconnected. These shouldn't be
294 * whacked until the koject refcount reaches zero though some entries
295 * may be changed via sysfs initiatives.
297 struct kref kref; /* ref count & hvcs_struct lifetime */
298 int connected; /* is the vty-server currently connected to a vty? */
299 uint32_t p_unit_address; /* partner unit address */
300 uint32_t p_partition_ID; /* partner partition ID */
301 char p_location_code[HVCS_CLC_LENGTH + 1]; /* CLC + Null Term */
302 struct list_head next; /* list management */
303 struct vio_dev *vdev;
306 /* Required to back map a kref to its containing object */
307 #define from_kref(k) container_of(k, struct hvcs_struct, kref)
309 static LIST_HEAD(hvcs_structs);
310 static DEFINE_SPINLOCK(hvcs_structs_lock);
312 static void hvcs_unthrottle(struct tty_struct *tty);
313 static void hvcs_throttle(struct tty_struct *tty);
314 static irqreturn_t hvcs_handle_interrupt(int irq, void *dev_instance);
316 static int hvcs_write(struct tty_struct *tty,
317 const unsigned char *buf, int count);
318 static int hvcs_write_room(struct tty_struct *tty);
319 static int hvcs_chars_in_buffer(struct tty_struct *tty);
321 static int hvcs_has_pi(struct hvcs_struct *hvcsd);
322 static void hvcs_set_pi(struct hvcs_partner_info *pi,
323 struct hvcs_struct *hvcsd);
324 static int hvcs_get_pi(struct hvcs_struct *hvcsd);
325 static int hvcs_rescan_devices_list(void);
327 static int hvcs_partner_connect(struct hvcs_struct *hvcsd);
328 static void hvcs_partner_free(struct hvcs_struct *hvcsd);
330 static int hvcs_enable_device(struct hvcs_struct *hvcsd,
331 uint32_t unit_address, unsigned int irq, struct vio_dev *dev);
333 static int hvcs_open(struct tty_struct *tty, struct file *filp);
334 static void hvcs_close(struct tty_struct *tty, struct file *filp);
335 static void hvcs_hangup(struct tty_struct * tty);
337 static int __devinit hvcs_probe(struct vio_dev *dev,
338 const struct vio_device_id *id);
339 static int __devexit hvcs_remove(struct vio_dev *dev);
340 static int __init hvcs_module_init(void);
341 static void __exit hvcs_module_exit(void);
343 #define HVCS_SCHED_READ 0x00000001
344 #define HVCS_QUICK_READ 0x00000002
345 #define HVCS_TRY_WRITE 0x00000004
346 #define HVCS_READ_MASK (HVCS_SCHED_READ | HVCS_QUICK_READ)
348 static inline struct hvcs_struct *from_vio_dev(struct vio_dev *viod)
350 return viod->dev.driver_data;
352 /* The sysfs interface for the driver and devices */
354 static ssize_t hvcs_partner_vtys_show(struct device *dev, struct device_attribute *attr, char *buf)
356 struct vio_dev *viod = to_vio_dev(dev);
357 struct hvcs_struct *hvcsd = from_vio_dev(viod);
358 unsigned long flags;
359 int retval;
361 spin_lock_irqsave(&hvcsd->lock, flags);
362 retval = sprintf(buf, "%X\n", hvcsd->p_unit_address);
363 spin_unlock_irqrestore(&hvcsd->lock, flags);
364 return retval;
366 static DEVICE_ATTR(partner_vtys, S_IRUGO, hvcs_partner_vtys_show, NULL);
368 static ssize_t hvcs_partner_clcs_show(struct device *dev, struct device_attribute *attr, char *buf)
370 struct vio_dev *viod = to_vio_dev(dev);
371 struct hvcs_struct *hvcsd = from_vio_dev(viod);
372 unsigned long flags;
373 int retval;
375 spin_lock_irqsave(&hvcsd->lock, flags);
376 retval = sprintf(buf, "%s\n", &hvcsd->p_location_code[0]);
377 spin_unlock_irqrestore(&hvcsd->lock, flags);
378 return retval;
380 static DEVICE_ATTR(partner_clcs, S_IRUGO, hvcs_partner_clcs_show, NULL);
382 static ssize_t hvcs_current_vty_store(struct device *dev, struct device_attribute *attr, const char * buf,
383 size_t count)
386 * Don't need this feature at the present time because firmware doesn't
387 * yet support multiple partners.
389 printk(KERN_INFO "HVCS: Denied current_vty change: -EPERM.\n");
390 return -EPERM;
393 static ssize_t hvcs_current_vty_show(struct device *dev, struct device_attribute *attr, char *buf)
395 struct vio_dev *viod = to_vio_dev(dev);
396 struct hvcs_struct *hvcsd = from_vio_dev(viod);
397 unsigned long flags;
398 int retval;
400 spin_lock_irqsave(&hvcsd->lock, flags);
401 retval = sprintf(buf, "%s\n", &hvcsd->p_location_code[0]);
402 spin_unlock_irqrestore(&hvcsd->lock, flags);
403 return retval;
406 static DEVICE_ATTR(current_vty,
407 S_IRUGO | S_IWUSR, hvcs_current_vty_show, hvcs_current_vty_store);
409 static ssize_t hvcs_vterm_state_store(struct device *dev, struct device_attribute *attr, const char *buf,
410 size_t count)
412 struct vio_dev *viod = to_vio_dev(dev);
413 struct hvcs_struct *hvcsd = from_vio_dev(viod);
414 unsigned long flags;
416 /* writing a '0' to this sysfs entry will result in the disconnect. */
417 if (simple_strtol(buf, NULL, 0) != 0)
418 return -EINVAL;
420 spin_lock_irqsave(&hvcsd->lock, flags);
422 if (hvcsd->open_count > 0) {
423 spin_unlock_irqrestore(&hvcsd->lock, flags);
424 printk(KERN_INFO "HVCS: vterm state unchanged. "
425 "The hvcs device node is still in use.\n");
426 return -EPERM;
429 if (hvcsd->connected == 0) {
430 spin_unlock_irqrestore(&hvcsd->lock, flags);
431 printk(KERN_INFO "HVCS: vterm state unchanged. The"
432 " vty-server is not connected to a vty.\n");
433 return -EPERM;
436 hvcs_partner_free(hvcsd);
437 printk(KERN_INFO "HVCS: Closed vty-server@%X and"
438 " partner vty@%X:%d connection.\n",
439 hvcsd->vdev->unit_address,
440 hvcsd->p_unit_address,
441 (uint32_t)hvcsd->p_partition_ID);
443 spin_unlock_irqrestore(&hvcsd->lock, flags);
444 return count;
447 static ssize_t hvcs_vterm_state_show(struct device *dev, struct device_attribute *attr, char *buf)
449 struct vio_dev *viod = to_vio_dev(dev);
450 struct hvcs_struct *hvcsd = from_vio_dev(viod);
451 unsigned long flags;
452 int retval;
454 spin_lock_irqsave(&hvcsd->lock, flags);
455 retval = sprintf(buf, "%d\n", hvcsd->connected);
456 spin_unlock_irqrestore(&hvcsd->lock, flags);
457 return retval;
459 static DEVICE_ATTR(vterm_state, S_IRUGO | S_IWUSR,
460 hvcs_vterm_state_show, hvcs_vterm_state_store);
462 static ssize_t hvcs_index_show(struct device *dev, struct device_attribute *attr, char *buf)
464 struct vio_dev *viod = to_vio_dev(dev);
465 struct hvcs_struct *hvcsd = from_vio_dev(viod);
466 unsigned long flags;
467 int retval;
469 spin_lock_irqsave(&hvcsd->lock, flags);
470 retval = sprintf(buf, "%d\n", hvcsd->index);
471 spin_unlock_irqrestore(&hvcsd->lock, flags);
472 return retval;
475 static DEVICE_ATTR(index, S_IRUGO, hvcs_index_show, NULL);
477 static struct attribute *hvcs_attrs[] = {
478 &dev_attr_partner_vtys.attr,
479 &dev_attr_partner_clcs.attr,
480 &dev_attr_current_vty.attr,
481 &dev_attr_vterm_state.attr,
482 &dev_attr_index.attr,
483 NULL,
486 static struct attribute_group hvcs_attr_group = {
487 .attrs = hvcs_attrs,
490 static ssize_t hvcs_rescan_show(struct device_driver *ddp, char *buf)
492 /* A 1 means it is updating, a 0 means it is done updating */
493 return snprintf(buf, PAGE_SIZE, "%d\n", hvcs_rescan_status);
496 static ssize_t hvcs_rescan_store(struct device_driver *ddp, const char * buf,
497 size_t count)
499 if ((simple_strtol(buf, NULL, 0) != 1)
500 && (hvcs_rescan_status != 0))
501 return -EINVAL;
503 hvcs_rescan_status = 1;
504 printk(KERN_INFO "HVCS: rescanning partner info for all"
505 " vty-servers.\n");
506 hvcs_rescan_devices_list();
507 hvcs_rescan_status = 0;
508 return count;
511 static DRIVER_ATTR(rescan,
512 S_IRUGO | S_IWUSR, hvcs_rescan_show, hvcs_rescan_store);
514 static void hvcs_kick(void)
516 hvcs_kicked = 1;
517 wmb();
518 wake_up_process(hvcs_task);
521 static void hvcs_unthrottle(struct tty_struct *tty)
523 struct hvcs_struct *hvcsd = tty->driver_data;
524 unsigned long flags;
526 spin_lock_irqsave(&hvcsd->lock, flags);
527 hvcsd->todo_mask |= HVCS_SCHED_READ;
528 spin_unlock_irqrestore(&hvcsd->lock, flags);
529 hvcs_kick();
532 static void hvcs_throttle(struct tty_struct *tty)
534 struct hvcs_struct *hvcsd = tty->driver_data;
535 unsigned long flags;
537 spin_lock_irqsave(&hvcsd->lock, flags);
538 vio_disable_interrupts(hvcsd->vdev);
539 spin_unlock_irqrestore(&hvcsd->lock, flags);
543 * If the device is being removed we don't have to worry about this interrupt
544 * handler taking any further interrupts because they are disabled which means
545 * the hvcs_struct will always be valid in this handler.
547 static irqreturn_t hvcs_handle_interrupt(int irq, void *dev_instance)
549 struct hvcs_struct *hvcsd = dev_instance;
551 spin_lock(&hvcsd->lock);
552 vio_disable_interrupts(hvcsd->vdev);
553 hvcsd->todo_mask |= HVCS_SCHED_READ;
554 spin_unlock(&hvcsd->lock);
555 hvcs_kick();
557 return IRQ_HANDLED;
560 /* This function must be called with the hvcsd->lock held */
561 static void hvcs_try_write(struct hvcs_struct *hvcsd)
563 uint32_t unit_address = hvcsd->vdev->unit_address;
564 struct tty_struct *tty = hvcsd->tty;
565 int sent;
567 if (hvcsd->todo_mask & HVCS_TRY_WRITE) {
568 /* won't send partial writes */
569 sent = hvc_put_chars(unit_address,
570 &hvcsd->buffer[0],
571 hvcsd->chars_in_buffer );
572 if (sent > 0) {
573 hvcsd->chars_in_buffer = 0;
574 /* wmb(); */
575 hvcsd->todo_mask &= ~(HVCS_TRY_WRITE);
576 /* wmb(); */
579 * We are still obligated to deliver the data to the
580 * hypervisor even if the tty has been closed because
581 * we commited to delivering it. But don't try to wake
582 * a non-existent tty.
584 if (tty) {
585 tty_wakeup(tty);
591 static int hvcs_io(struct hvcs_struct *hvcsd)
593 uint32_t unit_address;
594 struct tty_struct *tty;
595 char buf[HVCS_BUFF_LEN] __ALIGNED__;
596 unsigned long flags;
597 int got = 0;
599 spin_lock_irqsave(&hvcsd->lock, flags);
601 unit_address = hvcsd->vdev->unit_address;
602 tty = hvcsd->tty;
604 hvcs_try_write(hvcsd);
606 if (!tty || test_bit(TTY_THROTTLED, &tty->flags)) {
607 hvcsd->todo_mask &= ~(HVCS_READ_MASK);
608 goto bail;
609 } else if (!(hvcsd->todo_mask & (HVCS_READ_MASK)))
610 goto bail;
612 /* remove the read masks */
613 hvcsd->todo_mask &= ~(HVCS_READ_MASK);
615 if (tty_buffer_request_room(tty, HVCS_BUFF_LEN) >= HVCS_BUFF_LEN) {
616 got = hvc_get_chars(unit_address,
617 &buf[0],
618 HVCS_BUFF_LEN);
619 tty_insert_flip_string(tty, buf, got);
622 /* Give the TTY time to process the data we just sent. */
623 if (got)
624 hvcsd->todo_mask |= HVCS_QUICK_READ;
626 spin_unlock_irqrestore(&hvcsd->lock, flags);
627 /* This is synch because tty->low_latency == 1 */
628 if(got)
629 tty_flip_buffer_push(tty);
631 if (!got) {
632 /* Do this _after_ the flip_buffer_push */
633 spin_lock_irqsave(&hvcsd->lock, flags);
634 vio_enable_interrupts(hvcsd->vdev);
635 spin_unlock_irqrestore(&hvcsd->lock, flags);
638 return hvcsd->todo_mask;
640 bail:
641 spin_unlock_irqrestore(&hvcsd->lock, flags);
642 return hvcsd->todo_mask;
645 static int khvcsd(void *unused)
647 struct hvcs_struct *hvcsd;
648 int hvcs_todo_mask;
650 __set_current_state(TASK_RUNNING);
652 do {
653 hvcs_todo_mask = 0;
654 hvcs_kicked = 0;
655 wmb();
657 spin_lock(&hvcs_structs_lock);
658 list_for_each_entry(hvcsd, &hvcs_structs, next) {
659 hvcs_todo_mask |= hvcs_io(hvcsd);
661 spin_unlock(&hvcs_structs_lock);
664 * If any of the hvcs adapters want to try a write or quick read
665 * don't schedule(), yield a smidgen then execute the hvcs_io
666 * thread again for those that want the write.
668 if (hvcs_todo_mask & (HVCS_TRY_WRITE | HVCS_QUICK_READ)) {
669 yield();
670 continue;
673 set_current_state(TASK_INTERRUPTIBLE);
674 if (!hvcs_kicked)
675 schedule();
676 __set_current_state(TASK_RUNNING);
677 } while (!kthread_should_stop());
679 return 0;
682 static struct vio_device_id hvcs_driver_table[] __devinitdata= {
683 {"serial-server", "hvterm2"},
684 { "", "" }
686 MODULE_DEVICE_TABLE(vio, hvcs_driver_table);
688 static void hvcs_return_index(int index)
690 /* Paranoia check */
691 if (!hvcs_index_list)
692 return;
693 if (index < 0 || index >= hvcs_index_count)
694 return;
695 if (hvcs_index_list[index] == -1)
696 return;
697 else
698 hvcs_index_list[index] = -1;
701 /* callback when the kref ref count reaches zero */
702 static void destroy_hvcs_struct(struct kref *kref)
704 struct hvcs_struct *hvcsd = from_kref(kref);
705 struct vio_dev *vdev;
706 unsigned long flags;
708 spin_lock(&hvcs_structs_lock);
709 spin_lock_irqsave(&hvcsd->lock, flags);
711 /* the list_del poisons the pointers */
712 list_del(&(hvcsd->next));
714 if (hvcsd->connected == 1) {
715 hvcs_partner_free(hvcsd);
716 printk(KERN_INFO "HVCS: Closed vty-server@%X and"
717 " partner vty@%X:%d connection.\n",
718 hvcsd->vdev->unit_address,
719 hvcsd->p_unit_address,
720 (uint32_t)hvcsd->p_partition_ID);
722 printk(KERN_INFO "HVCS: Destroyed hvcs_struct for vty-server@%X.\n",
723 hvcsd->vdev->unit_address);
725 vdev = hvcsd->vdev;
726 hvcsd->vdev = NULL;
728 hvcsd->p_unit_address = 0;
729 hvcsd->p_partition_ID = 0;
730 hvcs_return_index(hvcsd->index);
731 memset(&hvcsd->p_location_code[0], 0x00, HVCS_CLC_LENGTH + 1);
733 spin_unlock_irqrestore(&hvcsd->lock, flags);
734 spin_unlock(&hvcs_structs_lock);
736 sysfs_remove_group(&vdev->dev.kobj, &hvcs_attr_group);
738 kfree(hvcsd);
741 static int hvcs_get_index(void)
743 int i;
744 /* Paranoia check */
745 if (!hvcs_index_list) {
746 printk(KERN_ERR "HVCS: hvcs_index_list NOT valid!.\n");
747 return -EFAULT;
749 /* Find the numerically lowest first free index. */
750 for(i = 0; i < hvcs_index_count; i++) {
751 if (hvcs_index_list[i] == -1) {
752 hvcs_index_list[i] = 0;
753 return i;
756 return -1;
759 static int __devinit hvcs_probe(
760 struct vio_dev *dev,
761 const struct vio_device_id *id)
763 struct hvcs_struct *hvcsd;
764 int index;
765 int retval;
767 if (!dev || !id) {
768 printk(KERN_ERR "HVCS: probed with invalid parameter.\n");
769 return -EPERM;
772 /* early to avoid cleanup on failure */
773 index = hvcs_get_index();
774 if (index < 0) {
775 return -EFAULT;
778 hvcsd = kzalloc(sizeof(*hvcsd), GFP_KERNEL);
779 if (!hvcsd)
780 return -ENODEV;
783 spin_lock_init(&hvcsd->lock);
784 /* Automatically incs the refcount the first time */
785 kref_init(&hvcsd->kref);
787 hvcsd->vdev = dev;
788 dev->dev.driver_data = hvcsd;
790 hvcsd->index = index;
792 /* hvcsd->index = ++hvcs_struct_count; */
793 hvcsd->chars_in_buffer = 0;
794 hvcsd->todo_mask = 0;
795 hvcsd->connected = 0;
798 * This will populate the hvcs_struct's partner info fields for the
799 * first time.
801 if (hvcs_get_pi(hvcsd)) {
802 printk(KERN_ERR "HVCS: Failed to fetch partner"
803 " info for vty-server@%X on device probe.\n",
804 hvcsd->vdev->unit_address);
808 * If a user app opens a tty that corresponds to this vty-server before
809 * the hvcs_struct has been added to the devices list then the user app
810 * will get -ENODEV.
812 spin_lock(&hvcs_structs_lock);
813 list_add_tail(&(hvcsd->next), &hvcs_structs);
814 spin_unlock(&hvcs_structs_lock);
816 retval = sysfs_create_group(&dev->dev.kobj, &hvcs_attr_group);
817 if (retval) {
818 printk(KERN_ERR "HVCS: Can't create sysfs attrs for vty-server@%X\n",
819 hvcsd->vdev->unit_address);
820 return retval;
823 printk(KERN_INFO "HVCS: vty-server@%X added to the vio bus.\n", dev->unit_address);
826 * DON'T enable interrupts here because there is no user to receive the
827 * data.
829 return 0;
832 static int __devexit hvcs_remove(struct vio_dev *dev)
834 struct hvcs_struct *hvcsd = dev->dev.driver_data;
835 unsigned long flags;
836 struct tty_struct *tty;
838 if (!hvcsd)
839 return -ENODEV;
841 /* By this time the vty-server won't be getting any more interrupts */
843 spin_lock_irqsave(&hvcsd->lock, flags);
845 tty = hvcsd->tty;
847 spin_unlock_irqrestore(&hvcsd->lock, flags);
850 * Let the last holder of this object cause it to be removed, which
851 * would probably be tty_hangup below.
853 kref_put(&hvcsd->kref, destroy_hvcs_struct);
856 * The hangup is a scheduled function which will auto chain call
857 * hvcs_hangup. The tty should always be valid at this time unless a
858 * simultaneous tty close already cleaned up the hvcs_struct.
860 if (tty)
861 tty_hangup(tty);
863 printk(KERN_INFO "HVCS: vty-server@%X removed from the"
864 " vio bus.\n", dev->unit_address);
865 return 0;
868 static struct vio_driver hvcs_vio_driver = {
869 .id_table = hvcs_driver_table,
870 .probe = hvcs_probe,
871 .remove = hvcs_remove,
872 .driver = {
873 .name = hvcs_driver_name,
874 .owner = THIS_MODULE,
878 /* Only called from hvcs_get_pi please */
879 static void hvcs_set_pi(struct hvcs_partner_info *pi, struct hvcs_struct *hvcsd)
881 int clclength;
883 hvcsd->p_unit_address = pi->unit_address;
884 hvcsd->p_partition_ID = pi->partition_ID;
885 clclength = strlen(&pi->location_code[0]);
886 if (clclength > HVCS_CLC_LENGTH)
887 clclength = HVCS_CLC_LENGTH;
889 /* copy the null-term char too */
890 strncpy(&hvcsd->p_location_code[0],
891 &pi->location_code[0], clclength + 1);
895 * Traverse the list and add the partner info that is found to the hvcs_struct
896 * struct entry. NOTE: At this time I know that partner info will return a
897 * single entry but in the future there may be multiple partner info entries per
898 * vty-server and you'll want to zero out that list and reset it. If for some
899 * reason you have an old version of this driver but there IS more than one
900 * partner info then hvcsd->p_* will hold the last partner info data from the
901 * firmware query. A good way to update this code would be to replace the three
902 * partner info fields in hvcs_struct with a list of hvcs_partner_info
903 * instances.
905 * This function must be called with the hvcsd->lock held.
907 static int hvcs_get_pi(struct hvcs_struct *hvcsd)
909 struct hvcs_partner_info *pi;
910 uint32_t unit_address = hvcsd->vdev->unit_address;
911 struct list_head head;
912 int retval;
914 spin_lock(&hvcs_pi_lock);
915 if (!hvcs_pi_buff) {
916 spin_unlock(&hvcs_pi_lock);
917 return -EFAULT;
919 retval = hvcs_get_partner_info(unit_address, &head, hvcs_pi_buff);
920 spin_unlock(&hvcs_pi_lock);
921 if (retval) {
922 printk(KERN_ERR "HVCS: Failed to fetch partner"
923 " info for vty-server@%x.\n", unit_address);
924 return retval;
927 /* nixes the values if the partner vty went away */
928 hvcsd->p_unit_address = 0;
929 hvcsd->p_partition_ID = 0;
931 list_for_each_entry(pi, &head, node)
932 hvcs_set_pi(pi, hvcsd);
934 hvcs_free_partner_info(&head);
935 return 0;
939 * This function is executed by the driver "rescan" sysfs entry. It shouldn't
940 * be executed elsewhere, in order to prevent deadlock issues.
942 static int hvcs_rescan_devices_list(void)
944 struct hvcs_struct *hvcsd;
945 unsigned long flags;
947 spin_lock(&hvcs_structs_lock);
949 list_for_each_entry(hvcsd, &hvcs_structs, next) {
950 spin_lock_irqsave(&hvcsd->lock, flags);
951 hvcs_get_pi(hvcsd);
952 spin_unlock_irqrestore(&hvcsd->lock, flags);
955 spin_unlock(&hvcs_structs_lock);
957 return 0;
961 * Farm this off into its own function because it could be more complex once
962 * multiple partners support is added. This function should be called with
963 * the hvcsd->lock held.
965 static int hvcs_has_pi(struct hvcs_struct *hvcsd)
967 if ((!hvcsd->p_unit_address) || (!hvcsd->p_partition_ID))
968 return 0;
969 return 1;
973 * NOTE: It is possible that the super admin removed a partner vty and then
974 * added a different vty as the new partner.
976 * This function must be called with the hvcsd->lock held.
978 static int hvcs_partner_connect(struct hvcs_struct *hvcsd)
980 int retval;
981 unsigned int unit_address = hvcsd->vdev->unit_address;
984 * If there wasn't any pi when the device was added it doesn't meant
985 * there isn't any now. This driver isn't notified when a new partner
986 * vty is added to a vty-server so we discover changes on our own.
987 * Please see comments in hvcs_register_connection() for justification
988 * of this bizarre code.
990 retval = hvcs_register_connection(unit_address,
991 hvcsd->p_partition_ID,
992 hvcsd->p_unit_address);
993 if (!retval) {
994 hvcsd->connected = 1;
995 return 0;
996 } else if (retval != -EINVAL)
997 return retval;
1000 * As per the spec re-get the pi and try again if -EINVAL after the
1001 * first connection attempt.
1003 if (hvcs_get_pi(hvcsd))
1004 return -ENOMEM;
1006 if (!hvcs_has_pi(hvcsd))
1007 return -ENODEV;
1009 retval = hvcs_register_connection(unit_address,
1010 hvcsd->p_partition_ID,
1011 hvcsd->p_unit_address);
1012 if (retval != -EINVAL) {
1013 hvcsd->connected = 1;
1014 return retval;
1018 * EBUSY is the most likely scenario though the vty could have been
1019 * removed or there really could be an hcall error due to the parameter
1020 * data but thanks to ambiguous firmware return codes we can't really
1021 * tell.
1023 printk(KERN_INFO "HVCS: vty-server or partner"
1024 " vty is busy. Try again later.\n");
1025 return -EBUSY;
1028 /* This function must be called with the hvcsd->lock held */
1029 static void hvcs_partner_free(struct hvcs_struct *hvcsd)
1031 int retval;
1032 do {
1033 retval = hvcs_free_connection(hvcsd->vdev->unit_address);
1034 } while (retval == -EBUSY);
1035 hvcsd->connected = 0;
1038 /* This helper function must be called WITHOUT the hvcsd->lock held */
1039 static int hvcs_enable_device(struct hvcs_struct *hvcsd, uint32_t unit_address,
1040 unsigned int irq, struct vio_dev *vdev)
1042 unsigned long flags;
1043 int rc;
1046 * It is possible that the vty-server was removed between the time that
1047 * the conn was registered and now.
1049 if (!(rc = request_irq(irq, &hvcs_handle_interrupt,
1050 IRQF_DISABLED, "ibmhvcs", hvcsd))) {
1052 * It is possible the vty-server was removed after the irq was
1053 * requested but before we have time to enable interrupts.
1055 if (vio_enable_interrupts(vdev) == H_SUCCESS)
1056 return 0;
1057 else {
1058 printk(KERN_ERR "HVCS: int enable failed for"
1059 " vty-server@%X.\n", unit_address);
1060 free_irq(irq, hvcsd);
1062 } else
1063 printk(KERN_ERR "HVCS: irq req failed for"
1064 " vty-server@%X.\n", unit_address);
1066 spin_lock_irqsave(&hvcsd->lock, flags);
1067 hvcs_partner_free(hvcsd);
1068 spin_unlock_irqrestore(&hvcsd->lock, flags);
1070 return rc;
1075 * This always increments the kref ref count if the call is successful.
1076 * Please remember to dec when you are done with the instance.
1078 * NOTICE: Do NOT hold either the hvcs_struct.lock or hvcs_structs_lock when
1079 * calling this function or you will get deadlock.
1081 static struct hvcs_struct *hvcs_get_by_index(int index)
1083 struct hvcs_struct *hvcsd = NULL;
1084 unsigned long flags;
1086 spin_lock(&hvcs_structs_lock);
1087 /* We can immediately discard OOB requests */
1088 if (index >= 0 && index < HVCS_MAX_SERVER_ADAPTERS) {
1089 list_for_each_entry(hvcsd, &hvcs_structs, next) {
1090 spin_lock_irqsave(&hvcsd->lock, flags);
1091 if (hvcsd->index == index) {
1092 kref_get(&hvcsd->kref);
1093 spin_unlock_irqrestore(&hvcsd->lock, flags);
1094 spin_unlock(&hvcs_structs_lock);
1095 return hvcsd;
1097 spin_unlock_irqrestore(&hvcsd->lock, flags);
1099 hvcsd = NULL;
1102 spin_unlock(&hvcs_structs_lock);
1103 return hvcsd;
1107 * This is invoked via the tty_open interface when a user app connects to the
1108 * /dev node.
1110 static int hvcs_open(struct tty_struct *tty, struct file *filp)
1112 struct hvcs_struct *hvcsd;
1113 int rc, retval = 0;
1114 unsigned long flags;
1115 unsigned int irq;
1116 struct vio_dev *vdev;
1117 unsigned long unit_address;
1119 if (tty->driver_data)
1120 goto fast_open;
1123 * Is there a vty-server that shares the same index?
1124 * This function increments the kref index.
1126 if (!(hvcsd = hvcs_get_by_index(tty->index))) {
1127 printk(KERN_WARNING "HVCS: open failed, no device associated"
1128 " with tty->index %d.\n", tty->index);
1129 return -ENODEV;
1132 spin_lock_irqsave(&hvcsd->lock, flags);
1134 if (hvcsd->connected == 0)
1135 if ((retval = hvcs_partner_connect(hvcsd)))
1136 goto error_release;
1138 hvcsd->open_count = 1;
1139 hvcsd->tty = tty;
1140 tty->driver_data = hvcsd;
1142 memset(&hvcsd->buffer[0], 0x00, HVCS_BUFF_LEN);
1145 * Save these in the spinlock for the enable operations that need them
1146 * outside of the spinlock.
1148 irq = hvcsd->vdev->irq;
1149 vdev = hvcsd->vdev;
1150 unit_address = hvcsd->vdev->unit_address;
1152 hvcsd->todo_mask |= HVCS_SCHED_READ;
1153 spin_unlock_irqrestore(&hvcsd->lock, flags);
1156 * This must be done outside of the spinlock because it requests irqs
1157 * and will grab the spinlock and free the connection if it fails.
1159 if (((rc = hvcs_enable_device(hvcsd, unit_address, irq, vdev)))) {
1160 kref_put(&hvcsd->kref, destroy_hvcs_struct);
1161 printk(KERN_WARNING "HVCS: enable device failed.\n");
1162 return rc;
1165 goto open_success;
1167 fast_open:
1168 hvcsd = tty->driver_data;
1170 spin_lock_irqsave(&hvcsd->lock, flags);
1171 kref_get(&hvcsd->kref);
1172 hvcsd->open_count++;
1173 hvcsd->todo_mask |= HVCS_SCHED_READ;
1174 spin_unlock_irqrestore(&hvcsd->lock, flags);
1176 open_success:
1177 hvcs_kick();
1179 printk(KERN_INFO "HVCS: vty-server@%X connection opened.\n",
1180 hvcsd->vdev->unit_address );
1182 return 0;
1184 error_release:
1185 spin_unlock_irqrestore(&hvcsd->lock, flags);
1186 kref_put(&hvcsd->kref, destroy_hvcs_struct);
1188 printk(KERN_WARNING "HVCS: partner connect failed.\n");
1189 return retval;
1192 static void hvcs_close(struct tty_struct *tty, struct file *filp)
1194 struct hvcs_struct *hvcsd;
1195 unsigned long flags;
1196 int irq = NO_IRQ;
1199 * Is someone trying to close the file associated with this device after
1200 * we have hung up? If so tty->driver_data wouldn't be valid.
1202 if (tty_hung_up_p(filp))
1203 return;
1206 * No driver_data means that this close was probably issued after a
1207 * failed hvcs_open by the tty layer's release_dev() api and we can just
1208 * exit cleanly.
1210 if (!tty->driver_data)
1211 return;
1213 hvcsd = tty->driver_data;
1215 spin_lock_irqsave(&hvcsd->lock, flags);
1216 if (--hvcsd->open_count == 0) {
1218 vio_disable_interrupts(hvcsd->vdev);
1221 * NULL this early so that the kernel_thread doesn't try to
1222 * execute any operations on the TTY even though it is obligated
1223 * to deliver any pending I/O to the hypervisor.
1225 hvcsd->tty = NULL;
1227 irq = hvcsd->vdev->irq;
1228 spin_unlock_irqrestore(&hvcsd->lock, flags);
1230 tty_wait_until_sent(tty, HVCS_CLOSE_WAIT);
1233 * This line is important because it tells hvcs_open that this
1234 * device needs to be re-configured the next time hvcs_open is
1235 * called.
1237 tty->driver_data = NULL;
1239 free_irq(irq, hvcsd);
1240 kref_put(&hvcsd->kref, destroy_hvcs_struct);
1241 return;
1242 } else if (hvcsd->open_count < 0) {
1243 printk(KERN_ERR "HVCS: vty-server@%X open_count: %d"
1244 " is missmanaged.\n",
1245 hvcsd->vdev->unit_address, hvcsd->open_count);
1248 spin_unlock_irqrestore(&hvcsd->lock, flags);
1249 kref_put(&hvcsd->kref, destroy_hvcs_struct);
1252 static void hvcs_hangup(struct tty_struct * tty)
1254 struct hvcs_struct *hvcsd = tty->driver_data;
1255 unsigned long flags;
1256 int temp_open_count;
1257 int irq = NO_IRQ;
1259 spin_lock_irqsave(&hvcsd->lock, flags);
1260 /* Preserve this so that we know how many kref refs to put */
1261 temp_open_count = hvcsd->open_count;
1264 * Don't kref put inside the spinlock because the destruction
1265 * callback may use the spinlock and it may get called before the
1266 * spinlock has been released.
1268 vio_disable_interrupts(hvcsd->vdev);
1270 hvcsd->todo_mask = 0;
1272 /* I don't think the tty needs the hvcs_struct pointer after a hangup */
1273 hvcsd->tty->driver_data = NULL;
1274 hvcsd->tty = NULL;
1276 hvcsd->open_count = 0;
1278 /* This will drop any buffered data on the floor which is OK in a hangup
1279 * scenario. */
1280 memset(&hvcsd->buffer[0], 0x00, HVCS_BUFF_LEN);
1281 hvcsd->chars_in_buffer = 0;
1283 irq = hvcsd->vdev->irq;
1285 spin_unlock_irqrestore(&hvcsd->lock, flags);
1287 free_irq(irq, hvcsd);
1290 * We need to kref_put() for every open_count we have since the
1291 * tty_hangup() function doesn't invoke a close per open connection on a
1292 * non-console device.
1294 while(temp_open_count) {
1295 --temp_open_count;
1297 * The final put will trigger destruction of the hvcs_struct.
1298 * NOTE: If this hangup was signaled from user space then the
1299 * final put will never happen.
1301 kref_put(&hvcsd->kref, destroy_hvcs_struct);
1306 * NOTE: This is almost always from_user since user level apps interact with the
1307 * /dev nodes. I'm trusting that if hvcs_write gets called and interrupted by
1308 * hvcs_remove (which removes the target device and executes tty_hangup()) that
1309 * tty_hangup will allow hvcs_write time to complete execution before it
1310 * terminates our device.
1312 static int hvcs_write(struct tty_struct *tty,
1313 const unsigned char *buf, int count)
1315 struct hvcs_struct *hvcsd = tty->driver_data;
1316 unsigned int unit_address;
1317 const unsigned char *charbuf;
1318 unsigned long flags;
1319 int total_sent = 0;
1320 int tosend = 0;
1321 int result = 0;
1324 * If they don't check the return code off of their open they may
1325 * attempt this even if there is no connected device.
1327 if (!hvcsd)
1328 return -ENODEV;
1330 /* Reasonable size to prevent user level flooding */
1331 if (count > HVCS_MAX_FROM_USER) {
1332 printk(KERN_WARNING "HVCS write: count being truncated to"
1333 " HVCS_MAX_FROM_USER.\n");
1334 count = HVCS_MAX_FROM_USER;
1337 charbuf = buf;
1339 spin_lock_irqsave(&hvcsd->lock, flags);
1342 * Somehow an open succedded but the device was removed or the
1343 * connection terminated between the vty-server and partner vty during
1344 * the middle of a write operation? This is a crummy place to do this
1345 * but we want to keep it all in the spinlock.
1347 if (hvcsd->open_count <= 0) {
1348 spin_unlock_irqrestore(&hvcsd->lock, flags);
1349 return -ENODEV;
1352 unit_address = hvcsd->vdev->unit_address;
1354 while (count > 0) {
1355 tosend = min(count, (HVCS_BUFF_LEN - hvcsd->chars_in_buffer));
1357 * No more space, this probably means that the last call to
1358 * hvcs_write() didn't succeed and the buffer was filled up.
1360 if (!tosend)
1361 break;
1363 memcpy(&hvcsd->buffer[hvcsd->chars_in_buffer],
1364 &charbuf[total_sent],
1365 tosend);
1367 hvcsd->chars_in_buffer += tosend;
1369 result = 0;
1372 * If this is true then we don't want to try writing to the
1373 * hypervisor because that is the kernel_threads job now. We'll
1374 * just add to the buffer.
1376 if (!(hvcsd->todo_mask & HVCS_TRY_WRITE))
1377 /* won't send partial writes */
1378 result = hvc_put_chars(unit_address,
1379 &hvcsd->buffer[0],
1380 hvcsd->chars_in_buffer);
1383 * Since we know we have enough room in hvcsd->buffer for
1384 * tosend we record that it was sent regardless of whether the
1385 * hypervisor actually took it because we have it buffered.
1387 total_sent+=tosend;
1388 count-=tosend;
1389 if (result == 0) {
1390 hvcsd->todo_mask |= HVCS_TRY_WRITE;
1391 hvcs_kick();
1392 break;
1395 hvcsd->chars_in_buffer = 0;
1397 * Test after the chars_in_buffer reset otherwise this could
1398 * deadlock our writes if hvc_put_chars fails.
1400 if (result < 0)
1401 break;
1404 spin_unlock_irqrestore(&hvcsd->lock, flags);
1406 if (result == -1)
1407 return -EIO;
1408 else
1409 return total_sent;
1413 * This is really asking how much can we guarentee that we can send or that we
1414 * absolutely WILL BUFFER if we can't send it. This driver MUST honor the
1415 * return value, hence the reason for hvcs_struct buffering.
1417 static int hvcs_write_room(struct tty_struct *tty)
1419 struct hvcs_struct *hvcsd = tty->driver_data;
1421 if (!hvcsd || hvcsd->open_count <= 0)
1422 return 0;
1424 return HVCS_BUFF_LEN - hvcsd->chars_in_buffer;
1427 static int hvcs_chars_in_buffer(struct tty_struct *tty)
1429 struct hvcs_struct *hvcsd = tty->driver_data;
1431 return hvcsd->chars_in_buffer;
1434 static const struct tty_operations hvcs_ops = {
1435 .open = hvcs_open,
1436 .close = hvcs_close,
1437 .hangup = hvcs_hangup,
1438 .write = hvcs_write,
1439 .write_room = hvcs_write_room,
1440 .chars_in_buffer = hvcs_chars_in_buffer,
1441 .unthrottle = hvcs_unthrottle,
1442 .throttle = hvcs_throttle,
1445 static int hvcs_alloc_index_list(int n)
1447 int i;
1449 hvcs_index_list = kmalloc(n * sizeof(hvcs_index_count),GFP_KERNEL);
1450 if (!hvcs_index_list)
1451 return -ENOMEM;
1452 hvcs_index_count = n;
1453 for (i = 0; i < hvcs_index_count; i++)
1454 hvcs_index_list[i] = -1;
1455 return 0;
1458 static void hvcs_free_index_list(void)
1460 /* Paranoia check to be thorough. */
1461 kfree(hvcs_index_list);
1462 hvcs_index_list = NULL;
1463 hvcs_index_count = 0;
1466 static int __init hvcs_module_init(void)
1468 int rc;
1469 int num_ttys_to_alloc;
1471 printk(KERN_INFO "Initializing %s\n", hvcs_driver_string);
1473 /* Has the user specified an overload with an insmod param? */
1474 if (hvcs_parm_num_devs <= 0 ||
1475 (hvcs_parm_num_devs > HVCS_MAX_SERVER_ADAPTERS)) {
1476 num_ttys_to_alloc = HVCS_DEFAULT_SERVER_ADAPTERS;
1477 } else
1478 num_ttys_to_alloc = hvcs_parm_num_devs;
1480 hvcs_tty_driver = alloc_tty_driver(num_ttys_to_alloc);
1481 if (!hvcs_tty_driver)
1482 return -ENOMEM;
1484 if (hvcs_alloc_index_list(num_ttys_to_alloc)) {
1485 rc = -ENOMEM;
1486 goto index_fail;
1489 hvcs_tty_driver->owner = THIS_MODULE;
1491 hvcs_tty_driver->driver_name = hvcs_driver_name;
1492 hvcs_tty_driver->name = hvcs_device_node;
1495 * We'll let the system assign us a major number, indicated by leaving
1496 * it blank.
1499 hvcs_tty_driver->minor_start = HVCS_MINOR_START;
1500 hvcs_tty_driver->type = TTY_DRIVER_TYPE_SYSTEM;
1503 * We role our own so that we DONT ECHO. We can't echo because the
1504 * device we are connecting to already echoes by default and this would
1505 * throw us into a horrible recursive echo-echo-echo loop.
1507 hvcs_tty_driver->init_termios = hvcs_tty_termios;
1508 hvcs_tty_driver->flags = TTY_DRIVER_REAL_RAW;
1510 tty_set_operations(hvcs_tty_driver, &hvcs_ops);
1513 * The following call will result in sysfs entries that denote the
1514 * dynamically assigned major and minor numbers for our devices.
1516 if (tty_register_driver(hvcs_tty_driver)) {
1517 printk(KERN_ERR "HVCS: registration as a tty driver failed.\n");
1518 rc = -EIO;
1519 goto register_fail;
1522 hvcs_pi_buff = kmalloc(PAGE_SIZE, GFP_KERNEL);
1523 if (!hvcs_pi_buff) {
1524 rc = -ENOMEM;
1525 goto buff_alloc_fail;
1528 hvcs_task = kthread_run(khvcsd, NULL, "khvcsd");
1529 if (IS_ERR(hvcs_task)) {
1530 printk(KERN_ERR "HVCS: khvcsd creation failed. Driver not loaded.\n");
1531 rc = -EIO;
1532 goto kthread_fail;
1535 rc = vio_register_driver(&hvcs_vio_driver);
1536 if (rc) {
1537 printk(KERN_ERR "HVCS: can't register vio driver\n");
1538 goto vio_fail;
1542 * This needs to be done AFTER the vio_register_driver() call or else
1543 * the kobjects won't be initialized properly.
1545 rc = driver_create_file(&(hvcs_vio_driver.driver), &driver_attr_rescan);
1546 if (rc) {
1547 printk(KERN_ERR "HVCS: sysfs attr create failed\n");
1548 goto attr_fail;
1551 printk(KERN_INFO "HVCS: driver module inserted.\n");
1553 return 0;
1555 attr_fail:
1556 vio_unregister_driver(&hvcs_vio_driver);
1557 vio_fail:
1558 kthread_stop(hvcs_task);
1559 kthread_fail:
1560 kfree(hvcs_pi_buff);
1561 buff_alloc_fail:
1562 tty_unregister_driver(hvcs_tty_driver);
1563 register_fail:
1564 hvcs_free_index_list();
1565 index_fail:
1566 put_tty_driver(hvcs_tty_driver);
1567 hvcs_tty_driver = NULL;
1568 return rc;
1571 static void __exit hvcs_module_exit(void)
1574 * This driver receives hvcs_remove callbacks for each device upon
1575 * module removal.
1579 * This synchronous operation will wake the khvcsd kthread if it is
1580 * asleep and will return when khvcsd has terminated.
1582 kthread_stop(hvcs_task);
1584 spin_lock(&hvcs_pi_lock);
1585 kfree(hvcs_pi_buff);
1586 hvcs_pi_buff = NULL;
1587 spin_unlock(&hvcs_pi_lock);
1589 driver_remove_file(&hvcs_vio_driver.driver, &driver_attr_rescan);
1591 vio_unregister_driver(&hvcs_vio_driver);
1593 tty_unregister_driver(hvcs_tty_driver);
1595 hvcs_free_index_list();
1597 put_tty_driver(hvcs_tty_driver);
1599 printk(KERN_INFO "HVCS: driver module removed.\n");
1602 module_init(hvcs_module_init);
1603 module_exit(hvcs_module_exit);