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