[AVR32] Remove mii_phy_addr and eth_addr from eth_platform_data
[linux-2.6.git] / drivers / char / hvcs.c
blobd090622f1dea2ec599040fd6aa0d967941d775ce
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 kobject construct. This kobject is also used by
61 * the vio bus to provide a vio device sysfs entry that this driver attaches
62 * device specific attributes to, including partner information. The vio bus
63 * framework also provides a sysfs entry for each vio driver. The hvcs driver
64 * provides driver attributes in this entry.
66 * For direction on installation and usage of this driver please reference
67 * Documentation/powerpc/hvcs.txt.
70 #include <linux/device.h>
71 #include <linux/init.h>
72 #include <linux/interrupt.h>
73 #include <linux/kernel.h>
74 #include <linux/kobject.h>
75 #include <linux/kthread.h>
76 #include <linux/list.h>
77 #include <linux/major.h>
78 #include <linux/module.h>
79 #include <linux/moduleparam.h>
80 #include <linux/sched.h>
81 #include <linux/spinlock.h>
82 #include <linux/stat.h>
83 #include <linux/tty.h>
84 #include <linux/tty_flip.h>
85 #include <asm/hvconsole.h>
86 #include <asm/hvcserver.h>
87 #include <asm/uaccess.h>
88 #include <asm/vio.h>
91 * 1.3.0 -> 1.3.1 In hvcs_open memset(..,0x00,..) instead of memset(..,0x3F,00).
92 * Removed braces around single statements following conditionals. Removed '=
93 * 0' after static int declarations since these default to zero. Removed
94 * list_for_each_safe() and replaced with list_for_each_entry() in
95 * hvcs_get_by_index(). The 'safe' version is un-needed now that the driver is
96 * using spinlocks. Changed spin_lock_irqsave() to spin_lock() when locking
97 * hvcs_structs_lock and hvcs_pi_lock since these are not touched in an int
98 * handler. Initialized hvcs_structs_lock and hvcs_pi_lock to
99 * SPIN_LOCK_UNLOCKED at declaration time rather than in hvcs_module_init().
100 * Added spin_lock around list_del() in destroy_hvcs_struct() to protect the
101 * list traversals from a deletion. Removed '= NULL' from pointer declaration
102 * statements since they are initialized NULL by default. Removed wmb()
103 * instances from hvcs_try_write(). They probably aren't needed with locking in
104 * place. Added check and cleanup for hvcs_pi_buff = kmalloc() in
105 * hvcs_module_init(). Exposed hvcs_struct.index via a sysfs attribute so that
106 * the coupling between /dev/hvcs* and a vty-server can be automatically
107 * determined. Moved kobject_put() in hvcs_open outside of the
108 * spin_unlock_irqrestore().
110 * 1.3.1 -> 1.3.2 Changed method for determining hvcs_struct->index and had it
111 * align with how the tty layer always assigns the lowest index available. This
112 * change resulted in a list of ints that denotes which indexes are available.
113 * Device additions and removals use the new hvcs_get_index() and
114 * hvcs_return_index() helper functions. The list is created with
115 * hvsc_alloc_index_list() and it is destroyed with hvcs_free_index_list().
116 * Without these fixes hotplug vty-server adapter support goes crazy with this
117 * driver if the user removes a vty-server adapter. Moved free_irq() outside of
118 * the hvcs_final_close() function in order to get it out of the spinlock.
119 * Rearranged hvcs_close(). Cleaned up some printks and did some housekeeping
120 * on the changelog. Removed local CLC_LENGTH and used HVCS_CLC_LENGTH from
121 * include/asm-powerpc/hvcserver.h
123 * 1.3.2 -> 1.3.3 Replaced yield() in hvcs_close() with tty_wait_until_sent() to
124 * prevent possible lockup with realtime scheduling as similarily pointed out by
125 * akpm in hvc_console. Changed resulted in the removal of hvcs_final_close()
126 * to reorder cleanup operations and prevent discarding of pending data during
127 * an hvcs_close(). Removed spinlock protection of hvcs_struct data members in
128 * hvcs_write_room() and hvcs_chars_in_buffer() because they aren't needed.
131 #define HVCS_DRIVER_VERSION "1.3.3"
133 MODULE_AUTHOR("Ryan S. Arnold <rsa@us.ibm.com>");
134 MODULE_DESCRIPTION("IBM hvcs (Hypervisor Virtual Console Server) Driver");
135 MODULE_LICENSE("GPL");
136 MODULE_VERSION(HVCS_DRIVER_VERSION);
139 * Wait this long per iteration while trying to push buffered data to the
140 * hypervisor before allowing the tty to complete a close operation.
142 #define HVCS_CLOSE_WAIT (HZ/100) /* 1/10 of a second */
145 * Since the Linux TTY code does not currently (2-04-2004) support dynamic
146 * addition of tty derived devices and we shouldn't allocate thousands of
147 * tty_device pointers when the number of vty-server & vty partner connections
148 * will most often be much lower than this, we'll arbitrarily allocate
149 * HVCS_DEFAULT_SERVER_ADAPTERS tty_structs and cdev's by default when we
150 * register the tty_driver. This can be overridden using an insmod parameter.
152 #define HVCS_DEFAULT_SERVER_ADAPTERS 64
155 * The user can't insmod with more than HVCS_MAX_SERVER_ADAPTERS hvcs device
156 * nodes as a sanity check. Theoretically there can be over 1 Billion
157 * vty-server & vty partner connections.
159 #define HVCS_MAX_SERVER_ADAPTERS 1024
162 * We let Linux assign us a major number and we start the minors at zero. There
163 * is no intuitive mapping between minor number and the target vty-server
164 * adapter except that each new vty-server adapter is always assigned to the
165 * smallest minor number available.
167 #define HVCS_MINOR_START 0
170 * The hcall interface involves putting 8 chars into each of two registers.
171 * We load up those 2 registers (in arch/powerpc/platforms/pseries/hvconsole.c)
172 * by casting char[16] to long[2]. It would work without __ALIGNED__, but a
173 * little (tiny) bit slower because an unaligned load is slower than aligned
174 * load.
176 #define __ALIGNED__ __attribute__((__aligned__(8)))
179 * How much data can firmware send with each hvc_put_chars()? Maybe this
180 * should be moved into an architecture specific area.
182 #define HVCS_BUFF_LEN 16
185 * This is the maximum amount of data we'll let the user send us (hvcs_write) at
186 * once in a chunk as a sanity check.
188 #define HVCS_MAX_FROM_USER 4096
191 * Be careful when adding flags to this line discipline. Don't add anything
192 * that will cause echoing or we'll go into recursive loop echoing chars back
193 * and forth with the console drivers.
195 static struct termios hvcs_tty_termios = {
196 .c_iflag = IGNBRK | IGNPAR,
197 .c_oflag = OPOST,
198 .c_cflag = B38400 | CS8 | CREAD | HUPCL,
199 .c_cc = INIT_C_CC
203 * This value is used to take the place of a command line parameter when the
204 * module is inserted. It starts as -1 and stays as such if the user doesn't
205 * specify a module insmod parameter. If they DO specify one then it is set to
206 * the value of the integer passed in.
208 static int hvcs_parm_num_devs = -1;
209 module_param(hvcs_parm_num_devs, int, 0);
211 char hvcs_driver_name[] = "hvcs";
212 char hvcs_device_node[] = "hvcs";
213 char hvcs_driver_string[]
214 = "IBM hvcs (Hypervisor Virtual Console Server) Driver";
216 /* Status of partner info rescan triggered via sysfs. */
217 static int hvcs_rescan_status;
219 static struct tty_driver *hvcs_tty_driver;
222 * In order to be somewhat sane this driver always associates the hvcs_struct
223 * index element with the numerically equal tty->index. This means that a
224 * hotplugged vty-server adapter will always map to the lowest index valued
225 * device node. If vty-servers were hotplug removed from the system and then
226 * new ones added the new vty-server may have the largest slot number of all
227 * the vty-server adapters in the partition but it may have the lowest dev node
228 * index of all the adapters due to the hole left by the hotplug removed
229 * adapter. There are a set of functions provided to get the lowest index for
230 * a new device as well as return the index to the list. This list is allocated
231 * with a number of elements equal to the number of device nodes requested when
232 * the module was inserted.
234 static int *hvcs_index_list;
237 * How large is the list? This is kept for traversal since the list is
238 * dynamically created.
240 static int hvcs_index_count;
243 * Used by the khvcsd to pick up I/O operations when the kernel_thread is
244 * already awake but potentially shifted to TASK_INTERRUPTIBLE state.
246 static int hvcs_kicked;
249 * Use by the kthread construct for task operations like waking the sleeping
250 * thread and stopping the kthread.
252 static struct task_struct *hvcs_task;
255 * We allocate this for the use of all of the hvcs_structs when they fetch
256 * partner info.
258 static unsigned long *hvcs_pi_buff;
260 /* Only allow one hvcs_struct to use the hvcs_pi_buff at a time. */
261 static DEFINE_SPINLOCK(hvcs_pi_lock);
263 /* One vty-server per hvcs_struct */
264 struct hvcs_struct {
265 spinlock_t lock;
268 * This index identifies this hvcs device as the complement to a
269 * specific tty index.
271 unsigned int index;
273 struct tty_struct *tty;
274 unsigned int open_count;
277 * Used to tell the driver kernel_thread what operations need to take
278 * place upon this hvcs_struct instance.
280 int todo_mask;
283 * This buffer is required so that when hvcs_write_room() reports that
284 * it can send HVCS_BUFF_LEN characters that it will buffer the full
285 * HVCS_BUFF_LEN characters if need be. This is essential for opost
286 * writes since they do not do high level buffering and expect to be
287 * able to send what the driver commits to sending buffering
288 * [e.g. tab to space conversions in n_tty.c opost()].
290 char buffer[HVCS_BUFF_LEN];
291 int chars_in_buffer;
294 * Any variable below the kobject is valid before a tty is connected and
295 * stays valid after the tty is disconnected. These shouldn't be
296 * whacked until the koject refcount reaches zero though some entries
297 * may be changed via sysfs initiatives.
299 struct kobject kobj; /* ref count & hvcs_struct lifetime */
300 int connected; /* is the vty-server currently connected to a vty? */
301 uint32_t p_unit_address; /* partner unit address */
302 uint32_t p_partition_ID; /* partner partition ID */
303 char p_location_code[HVCS_CLC_LENGTH + 1]; /* CLC + Null Term */
304 struct list_head next; /* list management */
305 struct vio_dev *vdev;
308 /* Required to back map a kobject to its containing object */
309 #define from_kobj(kobj) container_of(kobj, struct hvcs_struct, kobj)
311 static struct list_head hvcs_structs = LIST_HEAD_INIT(hvcs_structs);
312 static DEFINE_SPINLOCK(hvcs_structs_lock);
314 static void hvcs_unthrottle(struct tty_struct *tty);
315 static void hvcs_throttle(struct tty_struct *tty);
316 static irqreturn_t hvcs_handle_interrupt(int irq, void *dev_instance);
318 static int hvcs_write(struct tty_struct *tty,
319 const unsigned char *buf, int count);
320 static int hvcs_write_room(struct tty_struct *tty);
321 static int hvcs_chars_in_buffer(struct tty_struct *tty);
323 static int hvcs_has_pi(struct hvcs_struct *hvcsd);
324 static void hvcs_set_pi(struct hvcs_partner_info *pi,
325 struct hvcs_struct *hvcsd);
326 static int hvcs_get_pi(struct hvcs_struct *hvcsd);
327 static int hvcs_rescan_devices_list(void);
329 static int hvcs_partner_connect(struct hvcs_struct *hvcsd);
330 static void hvcs_partner_free(struct hvcs_struct *hvcsd);
332 static int hvcs_enable_device(struct hvcs_struct *hvcsd,
333 uint32_t unit_address, unsigned int irq, struct vio_dev *dev);
335 static void destroy_hvcs_struct(struct kobject *kobj);
336 static int hvcs_open(struct tty_struct *tty, struct file *filp);
337 static void hvcs_close(struct tty_struct *tty, struct file *filp);
338 static void hvcs_hangup(struct tty_struct * tty);
340 static int __devinit hvcs_probe(struct vio_dev *dev,
341 const struct vio_device_id *id);
342 static int __devexit hvcs_remove(struct vio_dev *dev);
343 static int __init hvcs_module_init(void);
344 static void __exit hvcs_module_exit(void);
346 #define HVCS_SCHED_READ 0x00000001
347 #define HVCS_QUICK_READ 0x00000002
348 #define HVCS_TRY_WRITE 0x00000004
349 #define HVCS_READ_MASK (HVCS_SCHED_READ | HVCS_QUICK_READ)
351 static inline struct hvcs_struct *from_vio_dev(struct vio_dev *viod)
353 return viod->dev.driver_data;
355 /* The sysfs interface for the driver and devices */
357 static ssize_t hvcs_partner_vtys_show(struct device *dev, struct device_attribute *attr, char *buf)
359 struct vio_dev *viod = to_vio_dev(dev);
360 struct hvcs_struct *hvcsd = from_vio_dev(viod);
361 unsigned long flags;
362 int retval;
364 spin_lock_irqsave(&hvcsd->lock, flags);
365 retval = sprintf(buf, "%X\n", hvcsd->p_unit_address);
366 spin_unlock_irqrestore(&hvcsd->lock, flags);
367 return retval;
369 static DEVICE_ATTR(partner_vtys, S_IRUGO, hvcs_partner_vtys_show, NULL);
371 static ssize_t hvcs_partner_clcs_show(struct device *dev, struct device_attribute *attr, char *buf)
373 struct vio_dev *viod = to_vio_dev(dev);
374 struct hvcs_struct *hvcsd = from_vio_dev(viod);
375 unsigned long flags;
376 int retval;
378 spin_lock_irqsave(&hvcsd->lock, flags);
379 retval = sprintf(buf, "%s\n", &hvcsd->p_location_code[0]);
380 spin_unlock_irqrestore(&hvcsd->lock, flags);
381 return retval;
383 static DEVICE_ATTR(partner_clcs, S_IRUGO, hvcs_partner_clcs_show, NULL);
385 static ssize_t hvcs_current_vty_store(struct device *dev, struct device_attribute *attr, const char * buf,
386 size_t count)
389 * Don't need this feature at the present time because firmware doesn't
390 * yet support multiple partners.
392 printk(KERN_INFO "HVCS: Denied current_vty change: -EPERM.\n");
393 return -EPERM;
396 static ssize_t hvcs_current_vty_show(struct device *dev, struct device_attribute *attr, char *buf)
398 struct vio_dev *viod = to_vio_dev(dev);
399 struct hvcs_struct *hvcsd = from_vio_dev(viod);
400 unsigned long flags;
401 int retval;
403 spin_lock_irqsave(&hvcsd->lock, flags);
404 retval = sprintf(buf, "%s\n", &hvcsd->p_location_code[0]);
405 spin_unlock_irqrestore(&hvcsd->lock, flags);
406 return retval;
409 static DEVICE_ATTR(current_vty,
410 S_IRUGO | S_IWUSR, hvcs_current_vty_show, hvcs_current_vty_store);
412 static ssize_t hvcs_vterm_state_store(struct device *dev, struct device_attribute *attr, const char *buf,
413 size_t count)
415 struct vio_dev *viod = to_vio_dev(dev);
416 struct hvcs_struct *hvcsd = from_vio_dev(viod);
417 unsigned long flags;
419 /* writing a '0' to this sysfs entry will result in the disconnect. */
420 if (simple_strtol(buf, NULL, 0) != 0)
421 return -EINVAL;
423 spin_lock_irqsave(&hvcsd->lock, flags);
425 if (hvcsd->open_count > 0) {
426 spin_unlock_irqrestore(&hvcsd->lock, flags);
427 printk(KERN_INFO "HVCS: vterm state unchanged. "
428 "The hvcs device node is still in use.\n");
429 return -EPERM;
432 if (hvcsd->connected == 0) {
433 spin_unlock_irqrestore(&hvcsd->lock, flags);
434 printk(KERN_INFO "HVCS: vterm state unchanged. The"
435 " vty-server is not connected to a vty.\n");
436 return -EPERM;
439 hvcs_partner_free(hvcsd);
440 printk(KERN_INFO "HVCS: Closed vty-server@%X and"
441 " partner vty@%X:%d connection.\n",
442 hvcsd->vdev->unit_address,
443 hvcsd->p_unit_address,
444 (uint32_t)hvcsd->p_partition_ID);
446 spin_unlock_irqrestore(&hvcsd->lock, flags);
447 return count;
450 static ssize_t hvcs_vterm_state_show(struct device *dev, struct device_attribute *attr, char *buf)
452 struct vio_dev *viod = to_vio_dev(dev);
453 struct hvcs_struct *hvcsd = from_vio_dev(viod);
454 unsigned long flags;
455 int retval;
457 spin_lock_irqsave(&hvcsd->lock, flags);
458 retval = sprintf(buf, "%d\n", hvcsd->connected);
459 spin_unlock_irqrestore(&hvcsd->lock, flags);
460 return retval;
462 static DEVICE_ATTR(vterm_state, S_IRUGO | S_IWUSR,
463 hvcs_vterm_state_show, hvcs_vterm_state_store);
465 static ssize_t hvcs_index_show(struct device *dev, struct device_attribute *attr, char *buf)
467 struct vio_dev *viod = to_vio_dev(dev);
468 struct hvcs_struct *hvcsd = from_vio_dev(viod);
469 unsigned long flags;
470 int retval;
472 spin_lock_irqsave(&hvcsd->lock, flags);
473 retval = sprintf(buf, "%d\n", hvcsd->index);
474 spin_unlock_irqrestore(&hvcsd->lock, flags);
475 return retval;
478 static DEVICE_ATTR(index, S_IRUGO, hvcs_index_show, NULL);
480 static struct attribute *hvcs_attrs[] = {
481 &dev_attr_partner_vtys.attr,
482 &dev_attr_partner_clcs.attr,
483 &dev_attr_current_vty.attr,
484 &dev_attr_vterm_state.attr,
485 &dev_attr_index.attr,
486 NULL,
489 static struct attribute_group hvcs_attr_group = {
490 .attrs = hvcs_attrs,
493 static ssize_t hvcs_rescan_show(struct device_driver *ddp, char *buf)
495 /* A 1 means it is updating, a 0 means it is done updating */
496 return snprintf(buf, PAGE_SIZE, "%d\n", hvcs_rescan_status);
499 static ssize_t hvcs_rescan_store(struct device_driver *ddp, const char * buf,
500 size_t count)
502 if ((simple_strtol(buf, NULL, 0) != 1)
503 && (hvcs_rescan_status != 0))
504 return -EINVAL;
506 hvcs_rescan_status = 1;
507 printk(KERN_INFO "HVCS: rescanning partner info for all"
508 " vty-servers.\n");
509 hvcs_rescan_devices_list();
510 hvcs_rescan_status = 0;
511 return count;
514 static DRIVER_ATTR(rescan,
515 S_IRUGO | S_IWUSR, hvcs_rescan_show, hvcs_rescan_store);
517 static void hvcs_kick(void)
519 hvcs_kicked = 1;
520 wmb();
521 wake_up_process(hvcs_task);
524 static void hvcs_unthrottle(struct tty_struct *tty)
526 struct hvcs_struct *hvcsd = tty->driver_data;
527 unsigned long flags;
529 spin_lock_irqsave(&hvcsd->lock, flags);
530 hvcsd->todo_mask |= HVCS_SCHED_READ;
531 spin_unlock_irqrestore(&hvcsd->lock, flags);
532 hvcs_kick();
535 static void hvcs_throttle(struct tty_struct *tty)
537 struct hvcs_struct *hvcsd = tty->driver_data;
538 unsigned long flags;
540 spin_lock_irqsave(&hvcsd->lock, flags);
541 vio_disable_interrupts(hvcsd->vdev);
542 spin_unlock_irqrestore(&hvcsd->lock, flags);
546 * If the device is being removed we don't have to worry about this interrupt
547 * handler taking any further interrupts because they are disabled which means
548 * the hvcs_struct will always be valid in this handler.
550 static irqreturn_t hvcs_handle_interrupt(int irq, void *dev_instance)
552 struct hvcs_struct *hvcsd = dev_instance;
554 spin_lock(&hvcsd->lock);
555 vio_disable_interrupts(hvcsd->vdev);
556 hvcsd->todo_mask |= HVCS_SCHED_READ;
557 spin_unlock(&hvcsd->lock);
558 hvcs_kick();
560 return IRQ_HANDLED;
563 /* This function must be called with the hvcsd->lock held */
564 static void hvcs_try_write(struct hvcs_struct *hvcsd)
566 uint32_t unit_address = hvcsd->vdev->unit_address;
567 struct tty_struct *tty = hvcsd->tty;
568 int sent;
570 if (hvcsd->todo_mask & HVCS_TRY_WRITE) {
571 /* won't send partial writes */
572 sent = hvc_put_chars(unit_address,
573 &hvcsd->buffer[0],
574 hvcsd->chars_in_buffer );
575 if (sent > 0) {
576 hvcsd->chars_in_buffer = 0;
577 /* wmb(); */
578 hvcsd->todo_mask &= ~(HVCS_TRY_WRITE);
579 /* wmb(); */
582 * We are still obligated to deliver the data to the
583 * hypervisor even if the tty has been closed because
584 * we commited to delivering it. But don't try to wake
585 * a non-existent tty.
587 if (tty) {
588 tty_wakeup(tty);
594 static int hvcs_io(struct hvcs_struct *hvcsd)
596 uint32_t unit_address;
597 struct tty_struct *tty;
598 char buf[HVCS_BUFF_LEN] __ALIGNED__;
599 unsigned long flags;
600 int got = 0;
602 spin_lock_irqsave(&hvcsd->lock, flags);
604 unit_address = hvcsd->vdev->unit_address;
605 tty = hvcsd->tty;
607 hvcs_try_write(hvcsd);
609 if (!tty || test_bit(TTY_THROTTLED, &tty->flags)) {
610 hvcsd->todo_mask &= ~(HVCS_READ_MASK);
611 goto bail;
612 } else if (!(hvcsd->todo_mask & (HVCS_READ_MASK)))
613 goto bail;
615 /* remove the read masks */
616 hvcsd->todo_mask &= ~(HVCS_READ_MASK);
618 if (tty_buffer_request_room(tty, HVCS_BUFF_LEN) >= HVCS_BUFF_LEN) {
619 got = hvc_get_chars(unit_address,
620 &buf[0],
621 HVCS_BUFF_LEN);
622 tty_insert_flip_string(tty, buf, got);
625 /* Give the TTY time to process the data we just sent. */
626 if (got)
627 hvcsd->todo_mask |= HVCS_QUICK_READ;
629 spin_unlock_irqrestore(&hvcsd->lock, flags);
630 /* This is synch because tty->low_latency == 1 */
631 if(got)
632 tty_flip_buffer_push(tty);
634 if (!got) {
635 /* Do this _after_ the flip_buffer_push */
636 spin_lock_irqsave(&hvcsd->lock, flags);
637 vio_enable_interrupts(hvcsd->vdev);
638 spin_unlock_irqrestore(&hvcsd->lock, flags);
641 return hvcsd->todo_mask;
643 bail:
644 spin_unlock_irqrestore(&hvcsd->lock, flags);
645 return hvcsd->todo_mask;
648 static int khvcsd(void *unused)
650 struct hvcs_struct *hvcsd;
651 int hvcs_todo_mask;
653 __set_current_state(TASK_RUNNING);
655 do {
656 hvcs_todo_mask = 0;
657 hvcs_kicked = 0;
658 wmb();
660 spin_lock(&hvcs_structs_lock);
661 list_for_each_entry(hvcsd, &hvcs_structs, next) {
662 hvcs_todo_mask |= hvcs_io(hvcsd);
664 spin_unlock(&hvcs_structs_lock);
667 * If any of the hvcs adapters want to try a write or quick read
668 * don't schedule(), yield a smidgen then execute the hvcs_io
669 * thread again for those that want the write.
671 if (hvcs_todo_mask & (HVCS_TRY_WRITE | HVCS_QUICK_READ)) {
672 yield();
673 continue;
676 set_current_state(TASK_INTERRUPTIBLE);
677 if (!hvcs_kicked)
678 schedule();
679 __set_current_state(TASK_RUNNING);
680 } while (!kthread_should_stop());
682 return 0;
685 static struct vio_device_id hvcs_driver_table[] __devinitdata= {
686 {"serial-server", "hvterm2"},
687 { "", "" }
689 MODULE_DEVICE_TABLE(vio, hvcs_driver_table);
691 static void hvcs_return_index(int index)
693 /* Paranoia check */
694 if (!hvcs_index_list)
695 return;
696 if (index < 0 || index >= hvcs_index_count)
697 return;
698 if (hvcs_index_list[index] == -1)
699 return;
700 else
701 hvcs_index_list[index] = -1;
704 /* callback when the kboject ref count reaches zero */
705 static void destroy_hvcs_struct(struct kobject *kobj)
707 struct hvcs_struct *hvcsd = from_kobj(kobj);
708 struct vio_dev *vdev;
709 unsigned long flags;
711 spin_lock(&hvcs_structs_lock);
712 spin_lock_irqsave(&hvcsd->lock, flags);
714 /* the list_del poisons the pointers */
715 list_del(&(hvcsd->next));
717 if (hvcsd->connected == 1) {
718 hvcs_partner_free(hvcsd);
719 printk(KERN_INFO "HVCS: Closed vty-server@%X and"
720 " partner vty@%X:%d connection.\n",
721 hvcsd->vdev->unit_address,
722 hvcsd->p_unit_address,
723 (uint32_t)hvcsd->p_partition_ID);
725 printk(KERN_INFO "HVCS: Destroyed hvcs_struct for vty-server@%X.\n",
726 hvcsd->vdev->unit_address);
728 vdev = hvcsd->vdev;
729 hvcsd->vdev = NULL;
731 hvcsd->p_unit_address = 0;
732 hvcsd->p_partition_ID = 0;
733 hvcs_return_index(hvcsd->index);
734 memset(&hvcsd->p_location_code[0], 0x00, HVCS_CLC_LENGTH + 1);
736 spin_unlock_irqrestore(&hvcsd->lock, flags);
737 spin_unlock(&hvcs_structs_lock);
739 sysfs_remove_group(&vdev->dev.kobj, &hvcs_attr_group);
741 kfree(hvcsd);
744 static struct kobj_type hvcs_kobj_type = {
745 .release = destroy_hvcs_struct,
748 static int hvcs_get_index(void)
750 int i;
751 /* Paranoia check */
752 if (!hvcs_index_list) {
753 printk(KERN_ERR "HVCS: hvcs_index_list NOT valid!.\n");
754 return -EFAULT;
756 /* Find the numerically lowest first free index. */
757 for(i = 0; i < hvcs_index_count; i++) {
758 if (hvcs_index_list[i] == -1) {
759 hvcs_index_list[i] = 0;
760 return i;
763 return -1;
766 static int __devinit hvcs_probe(
767 struct vio_dev *dev,
768 const struct vio_device_id *id)
770 struct hvcs_struct *hvcsd;
771 int index;
772 int retval;
774 if (!dev || !id) {
775 printk(KERN_ERR "HVCS: probed with invalid parameter.\n");
776 return -EPERM;
779 /* early to avoid cleanup on failure */
780 index = hvcs_get_index();
781 if (index < 0) {
782 return -EFAULT;
785 hvcsd = kmalloc(sizeof(*hvcsd), GFP_KERNEL);
786 if (!hvcsd)
787 return -ENODEV;
789 /* hvcsd->tty is zeroed out with the memset */
790 memset(hvcsd, 0x00, sizeof(*hvcsd));
792 spin_lock_init(&hvcsd->lock);
793 /* Automatically incs the refcount the first time */
794 kobject_init(&hvcsd->kobj);
795 /* Set up the callback for terminating the hvcs_struct's life */
796 hvcsd->kobj.ktype = &hvcs_kobj_type;
798 hvcsd->vdev = dev;
799 dev->dev.driver_data = hvcsd;
801 hvcsd->index = index;
803 /* hvcsd->index = ++hvcs_struct_count; */
804 hvcsd->chars_in_buffer = 0;
805 hvcsd->todo_mask = 0;
806 hvcsd->connected = 0;
809 * This will populate the hvcs_struct's partner info fields for the
810 * first time.
812 if (hvcs_get_pi(hvcsd)) {
813 printk(KERN_ERR "HVCS: Failed to fetch partner"
814 " info for vty-server@%X on device probe.\n",
815 hvcsd->vdev->unit_address);
819 * If a user app opens a tty that corresponds to this vty-server before
820 * the hvcs_struct has been added to the devices list then the user app
821 * will get -ENODEV.
823 spin_lock(&hvcs_structs_lock);
824 list_add_tail(&(hvcsd->next), &hvcs_structs);
825 spin_unlock(&hvcs_structs_lock);
827 retval = sysfs_create_group(&dev->dev.kobj, &hvcs_attr_group);
828 if (retval) {
829 printk(KERN_ERR "HVCS: Can't create sysfs attrs for vty-server@%X\n",
830 hvcsd->vdev->unit_address);
831 return retval;
834 printk(KERN_INFO "HVCS: vty-server@%X added to the vio bus.\n", dev->unit_address);
837 * DON'T enable interrupts here because there is no user to receive the
838 * data.
840 return 0;
843 static int __devexit hvcs_remove(struct vio_dev *dev)
845 struct hvcs_struct *hvcsd = dev->dev.driver_data;
846 unsigned long flags;
847 struct kobject *kobjp;
848 struct tty_struct *tty;
850 if (!hvcsd)
851 return -ENODEV;
853 /* By this time the vty-server won't be getting any more interrups */
855 spin_lock_irqsave(&hvcsd->lock, flags);
857 tty = hvcsd->tty;
859 kobjp = &hvcsd->kobj;
861 spin_unlock_irqrestore(&hvcsd->lock, flags);
864 * Let the last holder of this object cause it to be removed, which
865 * would probably be tty_hangup below.
867 kobject_put (kobjp);
870 * The hangup is a scheduled function which will auto chain call
871 * hvcs_hangup. The tty should always be valid at this time unless a
872 * simultaneous tty close already cleaned up the hvcs_struct.
874 if (tty)
875 tty_hangup(tty);
877 printk(KERN_INFO "HVCS: vty-server@%X removed from the"
878 " vio bus.\n", dev->unit_address);
879 return 0;
882 static struct vio_driver hvcs_vio_driver = {
883 .id_table = hvcs_driver_table,
884 .probe = hvcs_probe,
885 .remove = hvcs_remove,
886 .driver = {
887 .name = hvcs_driver_name,
888 .owner = THIS_MODULE,
892 /* Only called from hvcs_get_pi please */
893 static void hvcs_set_pi(struct hvcs_partner_info *pi, struct hvcs_struct *hvcsd)
895 int clclength;
897 hvcsd->p_unit_address = pi->unit_address;
898 hvcsd->p_partition_ID = pi->partition_ID;
899 clclength = strlen(&pi->location_code[0]);
900 if (clclength > HVCS_CLC_LENGTH)
901 clclength = HVCS_CLC_LENGTH;
903 /* copy the null-term char too */
904 strncpy(&hvcsd->p_location_code[0],
905 &pi->location_code[0], clclength + 1);
909 * Traverse the list and add the partner info that is found to the hvcs_struct
910 * struct entry. NOTE: At this time I know that partner info will return a
911 * single entry but in the future there may be multiple partner info entries per
912 * vty-server and you'll want to zero out that list and reset it. If for some
913 * reason you have an old version of this driver but there IS more than one
914 * partner info then hvcsd->p_* will hold the last partner info data from the
915 * firmware query. A good way to update this code would be to replace the three
916 * partner info fields in hvcs_struct with a list of hvcs_partner_info
917 * instances.
919 * This function must be called with the hvcsd->lock held.
921 static int hvcs_get_pi(struct hvcs_struct *hvcsd)
923 struct hvcs_partner_info *pi;
924 uint32_t unit_address = hvcsd->vdev->unit_address;
925 struct list_head head;
926 int retval;
928 spin_lock(&hvcs_pi_lock);
929 if (!hvcs_pi_buff) {
930 spin_unlock(&hvcs_pi_lock);
931 return -EFAULT;
933 retval = hvcs_get_partner_info(unit_address, &head, hvcs_pi_buff);
934 spin_unlock(&hvcs_pi_lock);
935 if (retval) {
936 printk(KERN_ERR "HVCS: Failed to fetch partner"
937 " info for vty-server@%x.\n", unit_address);
938 return retval;
941 /* nixes the values if the partner vty went away */
942 hvcsd->p_unit_address = 0;
943 hvcsd->p_partition_ID = 0;
945 list_for_each_entry(pi, &head, node)
946 hvcs_set_pi(pi, hvcsd);
948 hvcs_free_partner_info(&head);
949 return 0;
953 * This function is executed by the driver "rescan" sysfs entry. It shouldn't
954 * be executed elsewhere, in order to prevent deadlock issues.
956 static int hvcs_rescan_devices_list(void)
958 struct hvcs_struct *hvcsd;
959 unsigned long flags;
961 spin_lock(&hvcs_structs_lock);
963 list_for_each_entry(hvcsd, &hvcs_structs, next) {
964 spin_lock_irqsave(&hvcsd->lock, flags);
965 hvcs_get_pi(hvcsd);
966 spin_unlock_irqrestore(&hvcsd->lock, flags);
969 spin_unlock(&hvcs_structs_lock);
971 return 0;
975 * Farm this off into its own function because it could be more complex once
976 * multiple partners support is added. This function should be called with
977 * the hvcsd->lock held.
979 static int hvcs_has_pi(struct hvcs_struct *hvcsd)
981 if ((!hvcsd->p_unit_address) || (!hvcsd->p_partition_ID))
982 return 0;
983 return 1;
987 * NOTE: It is possible that the super admin removed a partner vty and then
988 * added a different vty as the new partner.
990 * This function must be called with the hvcsd->lock held.
992 static int hvcs_partner_connect(struct hvcs_struct *hvcsd)
994 int retval;
995 unsigned int unit_address = hvcsd->vdev->unit_address;
998 * If there wasn't any pi when the device was added it doesn't meant
999 * there isn't any now. This driver isn't notified when a new partner
1000 * vty is added to a vty-server so we discover changes on our own.
1001 * Please see comments in hvcs_register_connection() for justification
1002 * of this bizarre code.
1004 retval = hvcs_register_connection(unit_address,
1005 hvcsd->p_partition_ID,
1006 hvcsd->p_unit_address);
1007 if (!retval) {
1008 hvcsd->connected = 1;
1009 return 0;
1010 } else if (retval != -EINVAL)
1011 return retval;
1014 * As per the spec re-get the pi and try again if -EINVAL after the
1015 * first connection attempt.
1017 if (hvcs_get_pi(hvcsd))
1018 return -ENOMEM;
1020 if (!hvcs_has_pi(hvcsd))
1021 return -ENODEV;
1023 retval = hvcs_register_connection(unit_address,
1024 hvcsd->p_partition_ID,
1025 hvcsd->p_unit_address);
1026 if (retval != -EINVAL) {
1027 hvcsd->connected = 1;
1028 return retval;
1032 * EBUSY is the most likely scenario though the vty could have been
1033 * removed or there really could be an hcall error due to the parameter
1034 * data but thanks to ambiguous firmware return codes we can't really
1035 * tell.
1037 printk(KERN_INFO "HVCS: vty-server or partner"
1038 " vty is busy. Try again later.\n");
1039 return -EBUSY;
1042 /* This function must be called with the hvcsd->lock held */
1043 static void hvcs_partner_free(struct hvcs_struct *hvcsd)
1045 int retval;
1046 do {
1047 retval = hvcs_free_connection(hvcsd->vdev->unit_address);
1048 } while (retval == -EBUSY);
1049 hvcsd->connected = 0;
1052 /* This helper function must be called WITHOUT the hvcsd->lock held */
1053 static int hvcs_enable_device(struct hvcs_struct *hvcsd, uint32_t unit_address,
1054 unsigned int irq, struct vio_dev *vdev)
1056 unsigned long flags;
1057 int rc;
1060 * It is possible that the vty-server was removed between the time that
1061 * the conn was registered and now.
1063 if (!(rc = request_irq(irq, &hvcs_handle_interrupt,
1064 IRQF_DISABLED, "ibmhvcs", hvcsd))) {
1066 * It is possible the vty-server was removed after the irq was
1067 * requested but before we have time to enable interrupts.
1069 if (vio_enable_interrupts(vdev) == H_SUCCESS)
1070 return 0;
1071 else {
1072 printk(KERN_ERR "HVCS: int enable failed for"
1073 " vty-server@%X.\n", unit_address);
1074 free_irq(irq, hvcsd);
1076 } else
1077 printk(KERN_ERR "HVCS: irq req failed for"
1078 " vty-server@%X.\n", unit_address);
1080 spin_lock_irqsave(&hvcsd->lock, flags);
1081 hvcs_partner_free(hvcsd);
1082 spin_unlock_irqrestore(&hvcsd->lock, flags);
1084 return rc;
1089 * This always increments the kobject ref count if the call is successful.
1090 * Please remember to dec when you are done with the instance.
1092 * NOTICE: Do NOT hold either the hvcs_struct.lock or hvcs_structs_lock when
1093 * calling this function or you will get deadlock.
1095 struct hvcs_struct *hvcs_get_by_index(int index)
1097 struct hvcs_struct *hvcsd = NULL;
1098 unsigned long flags;
1100 spin_lock(&hvcs_structs_lock);
1101 /* We can immediately discard OOB requests */
1102 if (index >= 0 && index < HVCS_MAX_SERVER_ADAPTERS) {
1103 list_for_each_entry(hvcsd, &hvcs_structs, next) {
1104 spin_lock_irqsave(&hvcsd->lock, flags);
1105 if (hvcsd->index == index) {
1106 kobject_get(&hvcsd->kobj);
1107 spin_unlock_irqrestore(&hvcsd->lock, flags);
1108 spin_unlock(&hvcs_structs_lock);
1109 return hvcsd;
1111 spin_unlock_irqrestore(&hvcsd->lock, flags);
1113 hvcsd = NULL;
1116 spin_unlock(&hvcs_structs_lock);
1117 return hvcsd;
1121 * This is invoked via the tty_open interface when a user app connects to the
1122 * /dev node.
1124 static int hvcs_open(struct tty_struct *tty, struct file *filp)
1126 struct hvcs_struct *hvcsd;
1127 int rc, retval = 0;
1128 unsigned long flags;
1129 unsigned int irq;
1130 struct vio_dev *vdev;
1131 unsigned long unit_address;
1132 struct kobject *kobjp;
1134 if (tty->driver_data)
1135 goto fast_open;
1138 * Is there a vty-server that shares the same index?
1139 * This function increments the kobject index.
1141 if (!(hvcsd = hvcs_get_by_index(tty->index))) {
1142 printk(KERN_WARNING "HVCS: open failed, no device associated"
1143 " with tty->index %d.\n", tty->index);
1144 return -ENODEV;
1147 spin_lock_irqsave(&hvcsd->lock, flags);
1149 if (hvcsd->connected == 0)
1150 if ((retval = hvcs_partner_connect(hvcsd)))
1151 goto error_release;
1153 hvcsd->open_count = 1;
1154 hvcsd->tty = tty;
1155 tty->driver_data = hvcsd;
1158 * Set this driver to low latency so that we actually have a chance at
1159 * catching a throttled TTY after we flip_buffer_push. Otherwise the
1160 * flush_to_async may not execute until after the kernel_thread has
1161 * yielded and resumed the next flip_buffer_push resulting in data
1162 * loss.
1164 tty->low_latency = 1;
1166 memset(&hvcsd->buffer[0], 0x00, HVCS_BUFF_LEN);
1169 * Save these in the spinlock for the enable operations that need them
1170 * outside of the spinlock.
1172 irq = hvcsd->vdev->irq;
1173 vdev = hvcsd->vdev;
1174 unit_address = hvcsd->vdev->unit_address;
1176 hvcsd->todo_mask |= HVCS_SCHED_READ;
1177 spin_unlock_irqrestore(&hvcsd->lock, flags);
1180 * This must be done outside of the spinlock because it requests irqs
1181 * and will grab the spinlock and free the connection if it fails.
1183 if (((rc = hvcs_enable_device(hvcsd, unit_address, irq, vdev)))) {
1184 kobject_put(&hvcsd->kobj);
1185 printk(KERN_WARNING "HVCS: enable device failed.\n");
1186 return rc;
1189 goto open_success;
1191 fast_open:
1192 hvcsd = tty->driver_data;
1194 spin_lock_irqsave(&hvcsd->lock, flags);
1195 if (!kobject_get(&hvcsd->kobj)) {
1196 spin_unlock_irqrestore(&hvcsd->lock, flags);
1197 printk(KERN_ERR "HVCS: Kobject of open"
1198 " hvcs doesn't exist.\n");
1199 return -EFAULT; /* Is this the right return value? */
1202 hvcsd->open_count++;
1204 hvcsd->todo_mask |= HVCS_SCHED_READ;
1205 spin_unlock_irqrestore(&hvcsd->lock, flags);
1206 open_success:
1207 hvcs_kick();
1209 printk(KERN_INFO "HVCS: vty-server@%X connection opened.\n",
1210 hvcsd->vdev->unit_address );
1212 return 0;
1214 error_release:
1215 kobjp = &hvcsd->kobj;
1216 spin_unlock_irqrestore(&hvcsd->lock, flags);
1217 kobject_put(&hvcsd->kobj);
1219 printk(KERN_WARNING "HVCS: partner connect failed.\n");
1220 return retval;
1223 static void hvcs_close(struct tty_struct *tty, struct file *filp)
1225 struct hvcs_struct *hvcsd;
1226 unsigned long flags;
1227 struct kobject *kobjp;
1228 int irq = NO_IRQ;
1231 * Is someone trying to close the file associated with this device after
1232 * we have hung up? If so tty->driver_data wouldn't be valid.
1234 if (tty_hung_up_p(filp))
1235 return;
1238 * No driver_data means that this close was probably issued after a
1239 * failed hvcs_open by the tty layer's release_dev() api and we can just
1240 * exit cleanly.
1242 if (!tty->driver_data)
1243 return;
1245 hvcsd = tty->driver_data;
1247 spin_lock_irqsave(&hvcsd->lock, flags);
1248 kobjp = &hvcsd->kobj;
1249 if (--hvcsd->open_count == 0) {
1251 vio_disable_interrupts(hvcsd->vdev);
1254 * NULL this early so that the kernel_thread doesn't try to
1255 * execute any operations on the TTY even though it is obligated
1256 * to deliver any pending I/O to the hypervisor.
1258 hvcsd->tty = NULL;
1260 irq = hvcsd->vdev->irq;
1261 spin_unlock_irqrestore(&hvcsd->lock, flags);
1263 tty_wait_until_sent(tty, HVCS_CLOSE_WAIT);
1266 * This line is important because it tells hvcs_open that this
1267 * device needs to be re-configured the next time hvcs_open is
1268 * called.
1270 tty->driver_data = NULL;
1272 free_irq(irq, hvcsd);
1273 kobject_put(kobjp);
1274 return;
1275 } else if (hvcsd->open_count < 0) {
1276 printk(KERN_ERR "HVCS: vty-server@%X open_count: %d"
1277 " is missmanaged.\n",
1278 hvcsd->vdev->unit_address, hvcsd->open_count);
1281 spin_unlock_irqrestore(&hvcsd->lock, flags);
1282 kobject_put(kobjp);
1285 static void hvcs_hangup(struct tty_struct * tty)
1287 struct hvcs_struct *hvcsd = tty->driver_data;
1288 unsigned long flags;
1289 int temp_open_count;
1290 struct kobject *kobjp;
1291 int irq = NO_IRQ;
1293 spin_lock_irqsave(&hvcsd->lock, flags);
1294 /* Preserve this so that we know how many kobject refs to put */
1295 temp_open_count = hvcsd->open_count;
1298 * Don't kobject put inside the spinlock because the destruction
1299 * callback may use the spinlock and it may get called before the
1300 * spinlock has been released. Get a pointer to the kobject and
1301 * kobject_put on that after releasing the spinlock.
1303 kobjp = &hvcsd->kobj;
1305 vio_disable_interrupts(hvcsd->vdev);
1307 hvcsd->todo_mask = 0;
1309 /* I don't think the tty needs the hvcs_struct pointer after a hangup */
1310 hvcsd->tty->driver_data = NULL;
1311 hvcsd->tty = NULL;
1313 hvcsd->open_count = 0;
1315 /* This will drop any buffered data on the floor which is OK in a hangup
1316 * scenario. */
1317 memset(&hvcsd->buffer[0], 0x00, HVCS_BUFF_LEN);
1318 hvcsd->chars_in_buffer = 0;
1320 irq = hvcsd->vdev->irq;
1322 spin_unlock_irqrestore(&hvcsd->lock, flags);
1324 free_irq(irq, hvcsd);
1327 * We need to kobject_put() for every open_count we have since the
1328 * tty_hangup() function doesn't invoke a close per open connection on a
1329 * non-console device.
1331 while(temp_open_count) {
1332 --temp_open_count;
1334 * The final put will trigger destruction of the hvcs_struct.
1335 * NOTE: If this hangup was signaled from user space then the
1336 * final put will never happen.
1338 kobject_put(kobjp);
1343 * NOTE: This is almost always from_user since user level apps interact with the
1344 * /dev nodes. I'm trusting that if hvcs_write gets called and interrupted by
1345 * hvcs_remove (which removes the target device and executes tty_hangup()) that
1346 * tty_hangup will allow hvcs_write time to complete execution before it
1347 * terminates our device.
1349 static int hvcs_write(struct tty_struct *tty,
1350 const unsigned char *buf, int count)
1352 struct hvcs_struct *hvcsd = tty->driver_data;
1353 unsigned int unit_address;
1354 const unsigned char *charbuf;
1355 unsigned long flags;
1356 int total_sent = 0;
1357 int tosend = 0;
1358 int result = 0;
1361 * If they don't check the return code off of their open they may
1362 * attempt this even if there is no connected device.
1364 if (!hvcsd)
1365 return -ENODEV;
1367 /* Reasonable size to prevent user level flooding */
1368 if (count > HVCS_MAX_FROM_USER) {
1369 printk(KERN_WARNING "HVCS write: count being truncated to"
1370 " HVCS_MAX_FROM_USER.\n");
1371 count = HVCS_MAX_FROM_USER;
1374 charbuf = buf;
1376 spin_lock_irqsave(&hvcsd->lock, flags);
1379 * Somehow an open succedded but the device was removed or the
1380 * connection terminated between the vty-server and partner vty during
1381 * the middle of a write operation? This is a crummy place to do this
1382 * but we want to keep it all in the spinlock.
1384 if (hvcsd->open_count <= 0) {
1385 spin_unlock_irqrestore(&hvcsd->lock, flags);
1386 return -ENODEV;
1389 unit_address = hvcsd->vdev->unit_address;
1391 while (count > 0) {
1392 tosend = min(count, (HVCS_BUFF_LEN - hvcsd->chars_in_buffer));
1394 * No more space, this probably means that the last call to
1395 * hvcs_write() didn't succeed and the buffer was filled up.
1397 if (!tosend)
1398 break;
1400 memcpy(&hvcsd->buffer[hvcsd->chars_in_buffer],
1401 &charbuf[total_sent],
1402 tosend);
1404 hvcsd->chars_in_buffer += tosend;
1406 result = 0;
1409 * If this is true then we don't want to try writing to the
1410 * hypervisor because that is the kernel_threads job now. We'll
1411 * just add to the buffer.
1413 if (!(hvcsd->todo_mask & HVCS_TRY_WRITE))
1414 /* won't send partial writes */
1415 result = hvc_put_chars(unit_address,
1416 &hvcsd->buffer[0],
1417 hvcsd->chars_in_buffer);
1420 * Since we know we have enough room in hvcsd->buffer for
1421 * tosend we record that it was sent regardless of whether the
1422 * hypervisor actually took it because we have it buffered.
1424 total_sent+=tosend;
1425 count-=tosend;
1426 if (result == 0) {
1427 hvcsd->todo_mask |= HVCS_TRY_WRITE;
1428 hvcs_kick();
1429 break;
1432 hvcsd->chars_in_buffer = 0;
1434 * Test after the chars_in_buffer reset otherwise this could
1435 * deadlock our writes if hvc_put_chars fails.
1437 if (result < 0)
1438 break;
1441 spin_unlock_irqrestore(&hvcsd->lock, flags);
1443 if (result == -1)
1444 return -EIO;
1445 else
1446 return total_sent;
1450 * This is really asking how much can we guarentee that we can send or that we
1451 * absolutely WILL BUFFER if we can't send it. This driver MUST honor the
1452 * return value, hence the reason for hvcs_struct buffering.
1454 static int hvcs_write_room(struct tty_struct *tty)
1456 struct hvcs_struct *hvcsd = tty->driver_data;
1458 if (!hvcsd || hvcsd->open_count <= 0)
1459 return 0;
1461 return HVCS_BUFF_LEN - hvcsd->chars_in_buffer;
1464 static int hvcs_chars_in_buffer(struct tty_struct *tty)
1466 struct hvcs_struct *hvcsd = tty->driver_data;
1468 return hvcsd->chars_in_buffer;
1471 static const struct tty_operations hvcs_ops = {
1472 .open = hvcs_open,
1473 .close = hvcs_close,
1474 .hangup = hvcs_hangup,
1475 .write = hvcs_write,
1476 .write_room = hvcs_write_room,
1477 .chars_in_buffer = hvcs_chars_in_buffer,
1478 .unthrottle = hvcs_unthrottle,
1479 .throttle = hvcs_throttle,
1482 static int hvcs_alloc_index_list(int n)
1484 int i;
1486 hvcs_index_list = kmalloc(n * sizeof(hvcs_index_count),GFP_KERNEL);
1487 if (!hvcs_index_list)
1488 return -ENOMEM;
1489 hvcs_index_count = n;
1490 for (i = 0; i < hvcs_index_count; i++)
1491 hvcs_index_list[i] = -1;
1492 return 0;
1495 static void hvcs_free_index_list(void)
1497 /* Paranoia check to be thorough. */
1498 kfree(hvcs_index_list);
1499 hvcs_index_list = NULL;
1500 hvcs_index_count = 0;
1503 static int __init hvcs_module_init(void)
1505 int rc;
1506 int num_ttys_to_alloc;
1508 printk(KERN_INFO "Initializing %s\n", hvcs_driver_string);
1510 /* Has the user specified an overload with an insmod param? */
1511 if (hvcs_parm_num_devs <= 0 ||
1512 (hvcs_parm_num_devs > HVCS_MAX_SERVER_ADAPTERS)) {
1513 num_ttys_to_alloc = HVCS_DEFAULT_SERVER_ADAPTERS;
1514 } else
1515 num_ttys_to_alloc = hvcs_parm_num_devs;
1517 hvcs_tty_driver = alloc_tty_driver(num_ttys_to_alloc);
1518 if (!hvcs_tty_driver)
1519 return -ENOMEM;
1521 if (hvcs_alloc_index_list(num_ttys_to_alloc)) {
1522 rc = -ENOMEM;
1523 goto index_fail;
1526 hvcs_tty_driver->owner = THIS_MODULE;
1528 hvcs_tty_driver->driver_name = hvcs_driver_name;
1529 hvcs_tty_driver->name = hvcs_device_node;
1532 * We'll let the system assign us a major number, indicated by leaving
1533 * it blank.
1536 hvcs_tty_driver->minor_start = HVCS_MINOR_START;
1537 hvcs_tty_driver->type = TTY_DRIVER_TYPE_SYSTEM;
1540 * We role our own so that we DONT ECHO. We can't echo because the
1541 * device we are connecting to already echoes by default and this would
1542 * throw us into a horrible recursive echo-echo-echo loop.
1544 hvcs_tty_driver->init_termios = hvcs_tty_termios;
1545 hvcs_tty_driver->flags = TTY_DRIVER_REAL_RAW;
1547 tty_set_operations(hvcs_tty_driver, &hvcs_ops);
1550 * The following call will result in sysfs entries that denote the
1551 * dynamically assigned major and minor numbers for our devices.
1553 if (tty_register_driver(hvcs_tty_driver)) {
1554 printk(KERN_ERR "HVCS: registration as a tty driver failed.\n");
1555 rc = -EIO;
1556 goto register_fail;
1559 hvcs_pi_buff = kmalloc(PAGE_SIZE, GFP_KERNEL);
1560 if (!hvcs_pi_buff) {
1561 rc = -ENOMEM;
1562 goto buff_alloc_fail;
1565 hvcs_task = kthread_run(khvcsd, NULL, "khvcsd");
1566 if (IS_ERR(hvcs_task)) {
1567 printk(KERN_ERR "HVCS: khvcsd creation failed. Driver not loaded.\n");
1568 rc = -EIO;
1569 goto kthread_fail;
1572 rc = vio_register_driver(&hvcs_vio_driver);
1573 if (rc) {
1574 printk(KERN_ERR "HVCS: can't register vio driver\n");
1575 goto vio_fail;
1579 * This needs to be done AFTER the vio_register_driver() call or else
1580 * the kobjects won't be initialized properly.
1582 rc = driver_create_file(&(hvcs_vio_driver.driver), &driver_attr_rescan);
1583 if (rc) {
1584 printk(KERN_ERR "HVCS: sysfs attr create failed\n");
1585 goto attr_fail;
1588 printk(KERN_INFO "HVCS: driver module inserted.\n");
1590 return 0;
1592 attr_fail:
1593 vio_unregister_driver(&hvcs_vio_driver);
1594 vio_fail:
1595 kthread_stop(hvcs_task);
1596 kthread_fail:
1597 kfree(hvcs_pi_buff);
1598 buff_alloc_fail:
1599 tty_unregister_driver(hvcs_tty_driver);
1600 register_fail:
1601 hvcs_free_index_list();
1602 index_fail:
1603 put_tty_driver(hvcs_tty_driver);
1604 hvcs_tty_driver = NULL;
1605 return rc;
1608 static void __exit hvcs_module_exit(void)
1611 * This driver receives hvcs_remove callbacks for each device upon
1612 * module removal.
1616 * This synchronous operation will wake the khvcsd kthread if it is
1617 * asleep and will return when khvcsd has terminated.
1619 kthread_stop(hvcs_task);
1621 spin_lock(&hvcs_pi_lock);
1622 kfree(hvcs_pi_buff);
1623 hvcs_pi_buff = NULL;
1624 spin_unlock(&hvcs_pi_lock);
1626 driver_remove_file(&hvcs_vio_driver.driver, &driver_attr_rescan);
1628 vio_unregister_driver(&hvcs_vio_driver);
1630 tty_unregister_driver(hvcs_tty_driver);
1632 hvcs_free_index_list();
1634 put_tty_driver(hvcs_tty_driver);
1636 printk(KERN_INFO "HVCS: driver module removed.\n");
1639 module_init(hvcs_module_init);
1640 module_exit(hvcs_module_exit);