xref: /openbmc/linux/drivers/tty/hvc/hvcs.c (revision 4f2c0a4acffbec01079c28f839422e64ddeff004)
1e3b3d0f5SGreg Kroah-Hartman // SPDX-License-Identifier: GPL-2.0+
2728674a7SGreg Kroah-Hartman /*
3728674a7SGreg Kroah-Hartman  * IBM eServer Hypervisor Virtual Console Server Device Driver
4728674a7SGreg Kroah-Hartman  * Copyright (C) 2003, 2004 IBM Corp.
5728674a7SGreg Kroah-Hartman  *  Ryan S. Arnold (rsa@us.ibm.com)
6728674a7SGreg Kroah-Hartman  *
7728674a7SGreg Kroah-Hartman  * Author(s) :  Ryan S. Arnold <rsa@us.ibm.com>
8728674a7SGreg Kroah-Hartman  *
9728674a7SGreg Kroah-Hartman  * This is the device driver for the IBM Hypervisor Virtual Console Server,
10728674a7SGreg Kroah-Hartman  * "hvcs".  The IBM hvcs provides a tty driver interface to allow Linux
11728674a7SGreg Kroah-Hartman  * user space applications access to the system consoles of logically
12728674a7SGreg Kroah-Hartman  * partitioned operating systems, e.g. Linux, running on the same partitioned
13728674a7SGreg Kroah-Hartman  * Power5 ppc64 system.  Physical hardware consoles per partition are not
14728674a7SGreg Kroah-Hartman  * practical on this hardware so system consoles are accessed by this driver
15728674a7SGreg Kroah-Hartman  * using inter-partition firmware interfaces to virtual terminal devices.
16728674a7SGreg Kroah-Hartman  *
17728674a7SGreg Kroah-Hartman  * A vty is known to the HMC as a "virtual serial server adapter".  It is a
18728674a7SGreg Kroah-Hartman  * virtual terminal device that is created by firmware upon partition creation
19728674a7SGreg Kroah-Hartman  * to act as a partitioned OS's console device.
20728674a7SGreg Kroah-Hartman  *
21728674a7SGreg Kroah-Hartman  * Firmware dynamically (via hotplug) exposes vty-servers to a running ppc64
22728674a7SGreg Kroah-Hartman  * Linux system upon their creation by the HMC or their exposure during boot.
23728674a7SGreg Kroah-Hartman  * The non-user interactive backend of this driver is implemented as a vio
24728674a7SGreg Kroah-Hartman  * device driver so that it can receive notification of vty-server lifetimes
25728674a7SGreg Kroah-Hartman  * after it registers with the vio bus to handle vty-server probe and remove
26728674a7SGreg Kroah-Hartman  * callbacks.
27728674a7SGreg Kroah-Hartman  *
28728674a7SGreg Kroah-Hartman  * Many vty-servers can be configured to connect to one vty, but a vty can
29728674a7SGreg Kroah-Hartman  * only be actively connected to by a single vty-server, in any manner, at one
30728674a7SGreg Kroah-Hartman  * time.  If the HMC is currently hosting the console for a target Linux
31728674a7SGreg Kroah-Hartman  * partition; attempts to open the tty device to the partition's console using
32728674a7SGreg Kroah-Hartman  * the hvcs on any partition will return -EBUSY with every open attempt until
33728674a7SGreg Kroah-Hartman  * the HMC frees the connection between its vty-server and the desired
34728674a7SGreg Kroah-Hartman  * partition's vty device.  Conversely, a vty-server may only be connected to
35728674a7SGreg Kroah-Hartman  * a single vty at one time even though it may have several configured vty
36728674a7SGreg Kroah-Hartman  * partner possibilities.
37728674a7SGreg Kroah-Hartman  *
38728674a7SGreg Kroah-Hartman  * Firmware does not provide notification of vty partner changes to this
39728674a7SGreg Kroah-Hartman  * driver.  This means that an HMC Super Admin may add or remove partner vtys
40728674a7SGreg Kroah-Hartman  * from a vty-server's partner list but the changes will not be signaled to
41728674a7SGreg Kroah-Hartman  * the vty-server.  Firmware only notifies the driver when a vty-server is
42728674a7SGreg Kroah-Hartman  * added or removed from the system.  To compensate for this deficiency, this
43728674a7SGreg Kroah-Hartman  * driver implements a sysfs update attribute which provides a method for
44728674a7SGreg Kroah-Hartman  * rescanning partner information upon a user's request.
45728674a7SGreg Kroah-Hartman  *
46728674a7SGreg Kroah-Hartman  * Each vty-server, prior to being exposed to this driver is reference counted
47728674a7SGreg Kroah-Hartman  * using the 2.6 Linux kernel kref construct.
48728674a7SGreg Kroah-Hartman  *
49728674a7SGreg Kroah-Hartman  * For direction on installation and usage of this driver please reference
504d2e26a3SMauro Carvalho Chehab  * Documentation/powerpc/hvcs.rst.
51728674a7SGreg Kroah-Hartman  */
52728674a7SGreg Kroah-Hartman 
53728674a7SGreg Kroah-Hartman #include <linux/device.h>
54728674a7SGreg Kroah-Hartman #include <linux/init.h>
55728674a7SGreg Kroah-Hartman #include <linux/completion.h>
56728674a7SGreg Kroah-Hartman #include <linux/interrupt.h>
57728674a7SGreg Kroah-Hartman #include <linux/kernel.h>
58728674a7SGreg Kroah-Hartman #include <linux/kref.h>
59728674a7SGreg Kroah-Hartman #include <linux/kthread.h>
60728674a7SGreg Kroah-Hartman #include <linux/list.h>
61728674a7SGreg Kroah-Hartman #include <linux/major.h>
62728674a7SGreg Kroah-Hartman #include <linux/module.h>
63728674a7SGreg Kroah-Hartman #include <linux/moduleparam.h>
64728674a7SGreg Kroah-Hartman #include <linux/sched.h>
65728674a7SGreg Kroah-Hartman #include <linux/slab.h>
66728674a7SGreg Kroah-Hartman #include <linux/spinlock.h>
67728674a7SGreg Kroah-Hartman #include <linux/stat.h>
68728674a7SGreg Kroah-Hartman #include <linux/tty.h>
69728674a7SGreg Kroah-Hartman #include <linux/tty_flip.h>
70728674a7SGreg Kroah-Hartman #include <asm/hvconsole.h>
717c0f6ba6SLinus Torvalds #include <asm/hvcserver.h>
72*c9874d3fSAl Viro #include <linux/uaccess.h>
73728674a7SGreg Kroah-Hartman #include <linux/termios_internal.h>
74728674a7SGreg Kroah-Hartman #include <asm/vio.h>
75728674a7SGreg Kroah-Hartman 
76728674a7SGreg Kroah-Hartman /*
77728674a7SGreg Kroah-Hartman  * 1.3.0 -> 1.3.1 In hvcs_open memset(..,0x00,..) instead of memset(..,0x3F,00).
78728674a7SGreg Kroah-Hartman  * Removed braces around single statements following conditionals.  Removed '=
79728674a7SGreg Kroah-Hartman  * 0' after static int declarations since these default to zero.  Removed
80728674a7SGreg Kroah-Hartman  * list_for_each_safe() and replaced with list_for_each_entry() in
81728674a7SGreg Kroah-Hartman  * hvcs_get_by_index().  The 'safe' version is un-needed now that the driver is
82728674a7SGreg Kroah-Hartman  * using spinlocks.  Changed spin_lock_irqsave() to spin_lock() when locking
83728674a7SGreg Kroah-Hartman  * hvcs_structs_lock and hvcs_pi_lock since these are not touched in an int
84728674a7SGreg Kroah-Hartman  * handler.  Initialized hvcs_structs_lock and hvcs_pi_lock to
85728674a7SGreg Kroah-Hartman  * SPIN_LOCK_UNLOCKED at declaration time rather than in hvcs_module_init().
86728674a7SGreg Kroah-Hartman  * Added spin_lock around list_del() in destroy_hvcs_struct() to protect the
87728674a7SGreg Kroah-Hartman  * list traversals from a deletion.  Removed '= NULL' from pointer declaration
88728674a7SGreg Kroah-Hartman  * statements since they are initialized NULL by default.  Removed wmb()
89728674a7SGreg Kroah-Hartman  * instances from hvcs_try_write().  They probably aren't needed with locking in
90728674a7SGreg Kroah-Hartman  * place.  Added check and cleanup for hvcs_pi_buff = kmalloc() in
91728674a7SGreg Kroah-Hartman  * hvcs_module_init().  Exposed hvcs_struct.index via a sysfs attribute so that
92728674a7SGreg Kroah-Hartman  * the coupling between /dev/hvcs* and a vty-server can be automatically
93728674a7SGreg Kroah-Hartman  * determined.  Moved kobject_put() in hvcs_open outside of the
94728674a7SGreg Kroah-Hartman  * spin_unlock_irqrestore().
95728674a7SGreg Kroah-Hartman  *
96728674a7SGreg Kroah-Hartman  * 1.3.1 -> 1.3.2 Changed method for determining hvcs_struct->index and had it
97728674a7SGreg Kroah-Hartman  * align with how the tty layer always assigns the lowest index available.  This
98728674a7SGreg Kroah-Hartman  * change resulted in a list of ints that denotes which indexes are available.
99728674a7SGreg Kroah-Hartman  * Device additions and removals use the new hvcs_get_index() and
100728674a7SGreg Kroah-Hartman  * hvcs_return_index() helper functions.  The list is created with
101728674a7SGreg Kroah-Hartman  * hvsc_alloc_index_list() and it is destroyed with hvcs_free_index_list().
102728674a7SGreg Kroah-Hartman  * Without these fixes hotplug vty-server adapter support goes crazy with this
103728674a7SGreg Kroah-Hartman  * driver if the user removes a vty-server adapter.  Moved free_irq() outside of
104728674a7SGreg Kroah-Hartman  * the hvcs_final_close() function in order to get it out of the spinlock.
105728674a7SGreg Kroah-Hartman  * Rearranged hvcs_close().  Cleaned up some printks and did some housekeeping
106728674a7SGreg Kroah-Hartman  * on the changelog.  Removed local CLC_LENGTH and used HVCS_CLC_LENGTH from
107728674a7SGreg Kroah-Hartman  * arch/powerepc/include/asm/hvcserver.h
108728674a7SGreg Kroah-Hartman  *
10925985edcSLucas De Marchi  * 1.3.2 -> 1.3.3 Replaced yield() in hvcs_close() with tty_wait_until_sent() to
110728674a7SGreg Kroah-Hartman  * prevent possible lockup with realtime scheduling as similarly pointed out by
111728674a7SGreg Kroah-Hartman  * akpm in hvc_console.  Changed resulted in the removal of hvcs_final_close()
112728674a7SGreg Kroah-Hartman  * to reorder cleanup operations and prevent discarding of pending data during
113728674a7SGreg Kroah-Hartman  * an hvcs_close().  Removed spinlock protection of hvcs_struct data members in
114728674a7SGreg Kroah-Hartman  * hvcs_write_room() and hvcs_chars_in_buffer() because they aren't needed.
115728674a7SGreg Kroah-Hartman  */
116728674a7SGreg Kroah-Hartman 
117728674a7SGreg Kroah-Hartman #define HVCS_DRIVER_VERSION "1.3.3"
118728674a7SGreg Kroah-Hartman 
119728674a7SGreg Kroah-Hartman MODULE_AUTHOR("Ryan S. Arnold <rsa@us.ibm.com>");
120728674a7SGreg Kroah-Hartman MODULE_DESCRIPTION("IBM hvcs (Hypervisor Virtual Console Server) Driver");
121728674a7SGreg Kroah-Hartman MODULE_LICENSE("GPL");
122728674a7SGreg Kroah-Hartman MODULE_VERSION(HVCS_DRIVER_VERSION);
123728674a7SGreg Kroah-Hartman 
124728674a7SGreg Kroah-Hartman /*
125728674a7SGreg Kroah-Hartman  * Wait this long per iteration while trying to push buffered data to the
126728674a7SGreg Kroah-Hartman  * hypervisor before allowing the tty to complete a close operation.
127728674a7SGreg Kroah-Hartman  */
128728674a7SGreg Kroah-Hartman #define HVCS_CLOSE_WAIT (HZ/100) /* 1/10 of a second */
129728674a7SGreg Kroah-Hartman 
130728674a7SGreg Kroah-Hartman /*
131728674a7SGreg Kroah-Hartman  * Since the Linux TTY code does not currently (2-04-2004) support dynamic
132728674a7SGreg Kroah-Hartman  * addition of tty derived devices and we shouldn't allocate thousands of
133728674a7SGreg Kroah-Hartman  * tty_device pointers when the number of vty-server & vty partner connections
134728674a7SGreg Kroah-Hartman  * will most often be much lower than this, we'll arbitrarily allocate
135728674a7SGreg Kroah-Hartman  * HVCS_DEFAULT_SERVER_ADAPTERS tty_structs and cdev's by default when we
136728674a7SGreg Kroah-Hartman  * register the tty_driver. This can be overridden using an insmod parameter.
137728674a7SGreg Kroah-Hartman  */
138728674a7SGreg Kroah-Hartman #define HVCS_DEFAULT_SERVER_ADAPTERS	64
139728674a7SGreg Kroah-Hartman 
140728674a7SGreg Kroah-Hartman /*
141728674a7SGreg Kroah-Hartman  * The user can't insmod with more than HVCS_MAX_SERVER_ADAPTERS hvcs device
142728674a7SGreg Kroah-Hartman  * nodes as a sanity check.  Theoretically there can be over 1 Billion
143728674a7SGreg Kroah-Hartman  * vty-server & vty partner connections.
144728674a7SGreg Kroah-Hartman  */
145728674a7SGreg Kroah-Hartman #define HVCS_MAX_SERVER_ADAPTERS	1024
146728674a7SGreg Kroah-Hartman 
147728674a7SGreg Kroah-Hartman /*
148728674a7SGreg Kroah-Hartman  * We let Linux assign us a major number and we start the minors at zero.  There
149728674a7SGreg Kroah-Hartman  * is no intuitive mapping between minor number and the target vty-server
150728674a7SGreg Kroah-Hartman  * adapter except that each new vty-server adapter is always assigned to the
151728674a7SGreg Kroah-Hartman  * smallest minor number available.
152728674a7SGreg Kroah-Hartman  */
153728674a7SGreg Kroah-Hartman #define HVCS_MINOR_START	0
154728674a7SGreg Kroah-Hartman 
155728674a7SGreg Kroah-Hartman /*
156728674a7SGreg Kroah-Hartman  * The hcall interface involves putting 8 chars into each of two registers.
157728674a7SGreg Kroah-Hartman  * We load up those 2 registers (in arch/powerpc/platforms/pseries/hvconsole.c)
158728674a7SGreg Kroah-Hartman  * by casting char[16] to long[2].  It would work without __ALIGNED__, but a
159728674a7SGreg Kroah-Hartman  * little (tiny) bit slower because an unaligned load is slower than aligned
160728674a7SGreg Kroah-Hartman  * load.
161728674a7SGreg Kroah-Hartman  */
162728674a7SGreg Kroah-Hartman #define __ALIGNED__	__attribute__((__aligned__(8)))
163728674a7SGreg Kroah-Hartman 
164728674a7SGreg Kroah-Hartman /*
165728674a7SGreg Kroah-Hartman  * How much data can firmware send with each hvc_put_chars()?  Maybe this
166728674a7SGreg Kroah-Hartman  * should be moved into an architecture specific area.
167728674a7SGreg Kroah-Hartman  */
168728674a7SGreg Kroah-Hartman #define HVCS_BUFF_LEN	16
169728674a7SGreg Kroah-Hartman 
170728674a7SGreg Kroah-Hartman /*
171728674a7SGreg Kroah-Hartman  * This is the maximum amount of data we'll let the user send us (hvcs_write) at
172728674a7SGreg Kroah-Hartman  * once in a chunk as a sanity check.
173728674a7SGreg Kroah-Hartman  */
174728674a7SGreg Kroah-Hartman #define HVCS_MAX_FROM_USER	4096
175728674a7SGreg Kroah-Hartman 
176728674a7SGreg Kroah-Hartman /*
177728674a7SGreg Kroah-Hartman  * Be careful when adding flags to this line discipline.  Don't add anything
178728674a7SGreg Kroah-Hartman  * that will cause echoing or we'll go into recursive loop echoing chars back
179728674a7SGreg Kroah-Hartman  * and forth with the console drivers.
1806ec6e80dSBhumika Goyal  */
181728674a7SGreg Kroah-Hartman static const struct ktermios hvcs_tty_termios = {
182728674a7SGreg Kroah-Hartman 	.c_iflag = IGNBRK | IGNPAR,
183728674a7SGreg Kroah-Hartman 	.c_oflag = OPOST,
184728674a7SGreg Kroah-Hartman 	.c_cflag = B38400 | CS8 | CREAD | HUPCL,
185728674a7SGreg Kroah-Hartman 	.c_cc = INIT_C_CC,
186728674a7SGreg Kroah-Hartman 	.c_ispeed = 38400,
187728674a7SGreg Kroah-Hartman 	.c_ospeed = 38400
188728674a7SGreg Kroah-Hartman };
189728674a7SGreg Kroah-Hartman 
190728674a7SGreg Kroah-Hartman /*
191728674a7SGreg Kroah-Hartman  * This value is used to take the place of a command line parameter when the
192728674a7SGreg Kroah-Hartman  * module is inserted.  It starts as -1 and stays as such if the user doesn't
193728674a7SGreg Kroah-Hartman  * specify a module insmod parameter.  If they DO specify one then it is set to
194728674a7SGreg Kroah-Hartman  * the value of the integer passed in.
195728674a7SGreg Kroah-Hartman  */
196728674a7SGreg Kroah-Hartman static int hvcs_parm_num_devs = -1;
197728674a7SGreg Kroah-Hartman module_param(hvcs_parm_num_devs, int, 0);
198728674a7SGreg Kroah-Hartman 
199728674a7SGreg Kroah-Hartman static const char hvcs_driver_name[] = "hvcs";
200728674a7SGreg Kroah-Hartman static const char hvcs_device_node[] = "hvcs";
201728674a7SGreg Kroah-Hartman 
202728674a7SGreg Kroah-Hartman /* Status of partner info rescan triggered via sysfs. */
203728674a7SGreg Kroah-Hartman static int hvcs_rescan_status;
204728674a7SGreg Kroah-Hartman 
205728674a7SGreg Kroah-Hartman static struct tty_driver *hvcs_tty_driver;
206728674a7SGreg Kroah-Hartman 
207728674a7SGreg Kroah-Hartman /*
208728674a7SGreg Kroah-Hartman  * In order to be somewhat sane this driver always associates the hvcs_struct
209728674a7SGreg Kroah-Hartman  * index element with the numerically equal tty->index.  This means that a
210728674a7SGreg Kroah-Hartman  * hotplugged vty-server adapter will always map to the lowest index valued
211728674a7SGreg Kroah-Hartman  * device node.  If vty-servers were hotplug removed from the system and then
212728674a7SGreg Kroah-Hartman  * new ones added the new vty-server may have the largest slot number of all
213728674a7SGreg Kroah-Hartman  * the vty-server adapters in the partition but it may have the lowest dev node
214728674a7SGreg Kroah-Hartman  * index of all the adapters due to the hole left by the hotplug removed
215728674a7SGreg Kroah-Hartman  * adapter.  There are a set of functions provided to get the lowest index for
216728674a7SGreg Kroah-Hartman  * a new device as well as return the index to the list.  This list is allocated
217728674a7SGreg Kroah-Hartman  * with a number of elements equal to the number of device nodes requested when
218728674a7SGreg Kroah-Hartman  * the module was inserted.
219728674a7SGreg Kroah-Hartman  */
220728674a7SGreg Kroah-Hartman static int *hvcs_index_list;
221728674a7SGreg Kroah-Hartman 
222728674a7SGreg Kroah-Hartman /*
223728674a7SGreg Kroah-Hartman  * How large is the list?  This is kept for traversal since the list is
224728674a7SGreg Kroah-Hartman  * dynamically created.
225728674a7SGreg Kroah-Hartman  */
226728674a7SGreg Kroah-Hartman static int hvcs_index_count;
227728674a7SGreg Kroah-Hartman 
228728674a7SGreg Kroah-Hartman /*
229728674a7SGreg Kroah-Hartman  * Used by the khvcsd to pick up I/O operations when the kernel_thread is
230728674a7SGreg Kroah-Hartman  * already awake but potentially shifted to TASK_INTERRUPTIBLE state.
231728674a7SGreg Kroah-Hartman  */
232728674a7SGreg Kroah-Hartman static int hvcs_kicked;
233728674a7SGreg Kroah-Hartman 
234728674a7SGreg Kroah-Hartman /*
235728674a7SGreg Kroah-Hartman  * Use by the kthread construct for task operations like waking the sleeping
236728674a7SGreg Kroah-Hartman  * thread and stopping the kthread.
237728674a7SGreg Kroah-Hartman  */
238728674a7SGreg Kroah-Hartman static struct task_struct *hvcs_task;
239728674a7SGreg Kroah-Hartman 
240728674a7SGreg Kroah-Hartman /*
241728674a7SGreg Kroah-Hartman  * We allocate this for the use of all of the hvcs_structs when they fetch
242728674a7SGreg Kroah-Hartman  * partner info.
243728674a7SGreg Kroah-Hartman  */
244728674a7SGreg Kroah-Hartman static unsigned long *hvcs_pi_buff;
245728674a7SGreg Kroah-Hartman 
246728674a7SGreg Kroah-Hartman /* Only allow one hvcs_struct to use the hvcs_pi_buff at a time. */
247728674a7SGreg Kroah-Hartman static DEFINE_SPINLOCK(hvcs_pi_lock);
248728674a7SGreg Kroah-Hartman 
249728674a7SGreg Kroah-Hartman /* One vty-server per hvcs_struct */
2501997cf04SJiri Slaby struct hvcs_struct {
251728674a7SGreg Kroah-Hartman 	struct tty_port port;
252728674a7SGreg Kroah-Hartman 	spinlock_t lock;
253728674a7SGreg Kroah-Hartman 
254728674a7SGreg Kroah-Hartman 	/*
255728674a7SGreg Kroah-Hartman 	 * This index identifies this hvcs device as the complement to a
256728674a7SGreg Kroah-Hartman 	 * specific tty index.
257728674a7SGreg Kroah-Hartman 	 */
258728674a7SGreg Kroah-Hartman 	unsigned int index;
259728674a7SGreg Kroah-Hartman 
260728674a7SGreg Kroah-Hartman 	/*
261728674a7SGreg Kroah-Hartman 	 * Used to tell the driver kernel_thread what operations need to take
262728674a7SGreg Kroah-Hartman 	 * place upon this hvcs_struct instance.
263728674a7SGreg Kroah-Hartman 	 */
264728674a7SGreg Kroah-Hartman 	int todo_mask;
265728674a7SGreg Kroah-Hartman 
266728674a7SGreg Kroah-Hartman 	/*
267728674a7SGreg Kroah-Hartman 	 * This buffer is required so that when hvcs_write_room() reports that
268728674a7SGreg Kroah-Hartman 	 * it can send HVCS_BUFF_LEN characters that it will buffer the full
269728674a7SGreg Kroah-Hartman 	 * HVCS_BUFF_LEN characters if need be.  This is essential for opost
270728674a7SGreg Kroah-Hartman 	 * writes since they do not do high level buffering and expect to be
271728674a7SGreg Kroah-Hartman 	 * able to send what the driver commits to sending buffering
272728674a7SGreg Kroah-Hartman 	 * [e.g. tab to space conversions in n_tty.c opost()].
273728674a7SGreg Kroah-Hartman 	 */
274728674a7SGreg Kroah-Hartman 	char buffer[HVCS_BUFF_LEN];
275728674a7SGreg Kroah-Hartman 	int chars_in_buffer;
276728674a7SGreg Kroah-Hartman 
2772cd9fa25SJiri Slaby 	/*
278728674a7SGreg Kroah-Hartman 	 * Any variable below is valid before a tty is connected and
27946a1ca70SPaul Bolle 	 * stays valid after the tty is disconnected.  These shouldn't be
280728674a7SGreg Kroah-Hartman 	 * whacked until the kobject refcount reaches zero though some entries
281728674a7SGreg Kroah-Hartman 	 * may be changed via sysfs initiatives.
282728674a7SGreg Kroah-Hartman 	 */
283728674a7SGreg Kroah-Hartman 	int connected; /* is the vty-server currently connected to a vty? */
284728674a7SGreg Kroah-Hartman 	uint32_t p_unit_address; /* partner unit address */
285728674a7SGreg Kroah-Hartman 	uint32_t p_partition_ID; /* partner partition ID */
286728674a7SGreg Kroah-Hartman 	char p_location_code[HVCS_CLC_LENGTH + 1]; /* CLC + Null Term */
287728674a7SGreg Kroah-Hartman 	struct list_head next; /* list management */
288728674a7SGreg Kroah-Hartman 	struct vio_dev *vdev;
289728674a7SGreg Kroah-Hartman 	struct completion *destroyed;
290728674a7SGreg Kroah-Hartman };
291728674a7SGreg Kroah-Hartman 
292c7704d35SBenjamin Herrenschmidt static LIST_HEAD(hvcs_structs);
293728674a7SGreg Kroah-Hartman static DEFINE_SPINLOCK(hvcs_structs_lock);
294728674a7SGreg Kroah-Hartman static DEFINE_MUTEX(hvcs_init_mutex);
295728674a7SGreg Kroah-Hartman 
296728674a7SGreg Kroah-Hartman static int hvcs_get_pi(struct hvcs_struct *hvcsd);
297728674a7SGreg Kroah-Hartman static int hvcs_rescan_devices_list(void);
298728674a7SGreg Kroah-Hartman 
2999671f099SBill Pemberton static void hvcs_partner_free(struct hvcs_struct *hvcsd);
300728674a7SGreg Kroah-Hartman 
301728674a7SGreg Kroah-Hartman static int hvcs_initialize(void);
302728674a7SGreg Kroah-Hartman 
303728674a7SGreg Kroah-Hartman #define HVCS_SCHED_READ	0x00000001
304728674a7SGreg Kroah-Hartman #define HVCS_QUICK_READ	0x00000002
305728674a7SGreg Kroah-Hartman #define HVCS_TRY_WRITE	0x00000004
306728674a7SGreg Kroah-Hartman #define HVCS_READ_MASK	(HVCS_SCHED_READ | HVCS_QUICK_READ)
307728674a7SGreg Kroah-Hartman 
from_vio_dev(struct vio_dev * viod)308728674a7SGreg Kroah-Hartman static inline struct hvcs_struct *from_vio_dev(struct vio_dev *viod)
309728674a7SGreg Kroah-Hartman {
310728674a7SGreg Kroah-Hartman 	return dev_get_drvdata(&viod->dev);
311728674a7SGreg Kroah-Hartman }
312728674a7SGreg Kroah-Hartman /* The sysfs interface for the driver and devices */
313728674a7SGreg Kroah-Hartman 
hvcs_partner_vtys_show(struct device * dev,struct device_attribute * attr,char * buf)314728674a7SGreg Kroah-Hartman static ssize_t hvcs_partner_vtys_show(struct device *dev, struct device_attribute *attr, char *buf)
315728674a7SGreg Kroah-Hartman {
316728674a7SGreg Kroah-Hartman 	struct vio_dev *viod = to_vio_dev(dev);
317728674a7SGreg Kroah-Hartman 	struct hvcs_struct *hvcsd = from_vio_dev(viod);
318728674a7SGreg Kroah-Hartman 	unsigned long flags;
319728674a7SGreg Kroah-Hartman 	int retval;
320728674a7SGreg Kroah-Hartman 
321728674a7SGreg Kroah-Hartman 	spin_lock_irqsave(&hvcsd->lock, flags);
322728674a7SGreg Kroah-Hartman 	retval = sprintf(buf, "%X\n", hvcsd->p_unit_address);
323728674a7SGreg Kroah-Hartman 	spin_unlock_irqrestore(&hvcsd->lock, flags);
324728674a7SGreg Kroah-Hartman 	return retval;
325728674a7SGreg Kroah-Hartman }
326728674a7SGreg Kroah-Hartman static DEVICE_ATTR(partner_vtys, S_IRUGO, hvcs_partner_vtys_show, NULL);
327728674a7SGreg Kroah-Hartman 
hvcs_partner_clcs_show(struct device * dev,struct device_attribute * attr,char * buf)328728674a7SGreg Kroah-Hartman static ssize_t hvcs_partner_clcs_show(struct device *dev, struct device_attribute *attr, char *buf)
329728674a7SGreg Kroah-Hartman {
330728674a7SGreg Kroah-Hartman 	struct vio_dev *viod = to_vio_dev(dev);
331728674a7SGreg Kroah-Hartman 	struct hvcs_struct *hvcsd = from_vio_dev(viod);
332728674a7SGreg Kroah-Hartman 	unsigned long flags;
333728674a7SGreg Kroah-Hartman 	int retval;
334728674a7SGreg Kroah-Hartman 
335728674a7SGreg Kroah-Hartman 	spin_lock_irqsave(&hvcsd->lock, flags);
336728674a7SGreg Kroah-Hartman 	retval = sprintf(buf, "%s\n", &hvcsd->p_location_code[0]);
337728674a7SGreg Kroah-Hartman 	spin_unlock_irqrestore(&hvcsd->lock, flags);
338728674a7SGreg Kroah-Hartman 	return retval;
339728674a7SGreg Kroah-Hartman }
340728674a7SGreg Kroah-Hartman static DEVICE_ATTR(partner_clcs, S_IRUGO, hvcs_partner_clcs_show, NULL);
341728674a7SGreg Kroah-Hartman 
hvcs_current_vty_store(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)342728674a7SGreg Kroah-Hartman static ssize_t hvcs_current_vty_store(struct device *dev, struct device_attribute *attr, const char * buf,
343728674a7SGreg Kroah-Hartman 		size_t count)
344728674a7SGreg Kroah-Hartman {
345728674a7SGreg Kroah-Hartman 	/*
346728674a7SGreg Kroah-Hartman 	 * Don't need this feature at the present time because firmware doesn't
347728674a7SGreg Kroah-Hartman 	 * yet support multiple partners.
348728674a7SGreg Kroah-Hartman 	 */
349728674a7SGreg Kroah-Hartman 	printk(KERN_INFO "HVCS: Denied current_vty change: -EPERM.\n");
350728674a7SGreg Kroah-Hartman 	return -EPERM;
351728674a7SGreg Kroah-Hartman }
352728674a7SGreg Kroah-Hartman 
hvcs_current_vty_show(struct device * dev,struct device_attribute * attr,char * buf)353728674a7SGreg Kroah-Hartman static ssize_t hvcs_current_vty_show(struct device *dev, struct device_attribute *attr, char *buf)
354728674a7SGreg Kroah-Hartman {
355728674a7SGreg Kroah-Hartman 	struct vio_dev *viod = to_vio_dev(dev);
356728674a7SGreg Kroah-Hartman 	struct hvcs_struct *hvcsd = from_vio_dev(viod);
357728674a7SGreg Kroah-Hartman 	unsigned long flags;
358728674a7SGreg Kroah-Hartman 	int retval;
359728674a7SGreg Kroah-Hartman 
360728674a7SGreg Kroah-Hartman 	spin_lock_irqsave(&hvcsd->lock, flags);
361728674a7SGreg Kroah-Hartman 	retval = sprintf(buf, "%s\n", &hvcsd->p_location_code[0]);
362728674a7SGreg Kroah-Hartman 	spin_unlock_irqrestore(&hvcsd->lock, flags);
363728674a7SGreg Kroah-Hartman 	return retval;
364728674a7SGreg Kroah-Hartman }
365728674a7SGreg Kroah-Hartman 
366728674a7SGreg Kroah-Hartman static DEVICE_ATTR(current_vty,
367728674a7SGreg Kroah-Hartman 	S_IRUGO | S_IWUSR, hvcs_current_vty_show, hvcs_current_vty_store);
368728674a7SGreg Kroah-Hartman 
hvcs_vterm_state_store(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)369728674a7SGreg Kroah-Hartman static ssize_t hvcs_vterm_state_store(struct device *dev, struct device_attribute *attr, const char *buf,
370728674a7SGreg Kroah-Hartman 		size_t count)
371728674a7SGreg Kroah-Hartman {
372728674a7SGreg Kroah-Hartman 	struct vio_dev *viod = to_vio_dev(dev);
373728674a7SGreg Kroah-Hartman 	struct hvcs_struct *hvcsd = from_vio_dev(viod);
374728674a7SGreg Kroah-Hartman 	unsigned long flags;
375728674a7SGreg Kroah-Hartman 
376728674a7SGreg Kroah-Hartman 	/* writing a '0' to this sysfs entry will result in the disconnect. */
377728674a7SGreg Kroah-Hartman 	if (simple_strtol(buf, NULL, 0) != 0)
378728674a7SGreg Kroah-Hartman 		return -EINVAL;
379728674a7SGreg Kroah-Hartman 
3801997cf04SJiri Slaby 	spin_lock_irqsave(&hvcsd->lock, flags);
381728674a7SGreg Kroah-Hartman 
382728674a7SGreg Kroah-Hartman 	if (hvcsd->port.count > 0) {
383728674a7SGreg Kroah-Hartman 		spin_unlock_irqrestore(&hvcsd->lock, flags);
384728674a7SGreg Kroah-Hartman 		printk(KERN_INFO "HVCS: vterm state unchanged.  "
385728674a7SGreg Kroah-Hartman 				"The hvcs device node is still in use.\n");
386728674a7SGreg Kroah-Hartman 		return -EPERM;
387728674a7SGreg Kroah-Hartman 	}
388728674a7SGreg Kroah-Hartman 
389728674a7SGreg Kroah-Hartman 	if (hvcsd->connected == 0) {
390728674a7SGreg Kroah-Hartman 		spin_unlock_irqrestore(&hvcsd->lock, flags);
391728674a7SGreg Kroah-Hartman 		printk(KERN_INFO "HVCS: vterm state unchanged. The"
392728674a7SGreg Kroah-Hartman 				" vty-server is not connected to a vty.\n");
393728674a7SGreg Kroah-Hartman 		return -EPERM;
394728674a7SGreg Kroah-Hartman 	}
395728674a7SGreg Kroah-Hartman 
396728674a7SGreg Kroah-Hartman 	hvcs_partner_free(hvcsd);
397728674a7SGreg Kroah-Hartman 	printk(KERN_INFO "HVCS: Closed vty-server@%X and"
398728674a7SGreg Kroah-Hartman 			" partner vty@%X:%d connection.\n",
399728674a7SGreg Kroah-Hartman 			hvcsd->vdev->unit_address,
400728674a7SGreg Kroah-Hartman 			hvcsd->p_unit_address,
401728674a7SGreg Kroah-Hartman 			(uint32_t)hvcsd->p_partition_ID);
402728674a7SGreg Kroah-Hartman 
403728674a7SGreg Kroah-Hartman 	spin_unlock_irqrestore(&hvcsd->lock, flags);
404728674a7SGreg Kroah-Hartman 	return count;
405728674a7SGreg Kroah-Hartman }
406728674a7SGreg Kroah-Hartman 
hvcs_vterm_state_show(struct device * dev,struct device_attribute * attr,char * buf)407728674a7SGreg Kroah-Hartman static ssize_t hvcs_vterm_state_show(struct device *dev, struct device_attribute *attr, char *buf)
408728674a7SGreg Kroah-Hartman {
409728674a7SGreg Kroah-Hartman 	struct vio_dev *viod = to_vio_dev(dev);
410728674a7SGreg Kroah-Hartman 	struct hvcs_struct *hvcsd = from_vio_dev(viod);
411728674a7SGreg Kroah-Hartman 	unsigned long flags;
412728674a7SGreg Kroah-Hartman 	int retval;
413728674a7SGreg Kroah-Hartman 
414728674a7SGreg Kroah-Hartman 	spin_lock_irqsave(&hvcsd->lock, flags);
415728674a7SGreg Kroah-Hartman 	retval = sprintf(buf, "%d\n", hvcsd->connected);
416728674a7SGreg Kroah-Hartman 	spin_unlock_irqrestore(&hvcsd->lock, flags);
417728674a7SGreg Kroah-Hartman 	return retval;
418728674a7SGreg Kroah-Hartman }
419728674a7SGreg Kroah-Hartman static DEVICE_ATTR(vterm_state, S_IRUGO | S_IWUSR,
420728674a7SGreg Kroah-Hartman 		hvcs_vterm_state_show, hvcs_vterm_state_store);
421728674a7SGreg Kroah-Hartman 
hvcs_index_show(struct device * dev,struct device_attribute * attr,char * buf)422728674a7SGreg Kroah-Hartman static ssize_t hvcs_index_show(struct device *dev, struct device_attribute *attr, char *buf)
423728674a7SGreg Kroah-Hartman {
424728674a7SGreg Kroah-Hartman 	struct vio_dev *viod = to_vio_dev(dev);
425728674a7SGreg Kroah-Hartman 	struct hvcs_struct *hvcsd = from_vio_dev(viod);
426728674a7SGreg Kroah-Hartman 	unsigned long flags;
427728674a7SGreg Kroah-Hartman 	int retval;
428728674a7SGreg Kroah-Hartman 
429728674a7SGreg Kroah-Hartman 	spin_lock_irqsave(&hvcsd->lock, flags);
430728674a7SGreg Kroah-Hartman 	retval = sprintf(buf, "%d\n", hvcsd->index);
431728674a7SGreg Kroah-Hartman 	spin_unlock_irqrestore(&hvcsd->lock, flags);
432728674a7SGreg Kroah-Hartman 	return retval;
433728674a7SGreg Kroah-Hartman }
434728674a7SGreg Kroah-Hartman 
435728674a7SGreg Kroah-Hartman static DEVICE_ATTR(index, S_IRUGO, hvcs_index_show, NULL);
436728674a7SGreg Kroah-Hartman 
437728674a7SGreg Kroah-Hartman static struct attribute *hvcs_dev_attrs[] = {
438728674a7SGreg Kroah-Hartman 	&dev_attr_partner_vtys.attr,
439728674a7SGreg Kroah-Hartman 	&dev_attr_partner_clcs.attr,
440728674a7SGreg Kroah-Hartman 	&dev_attr_current_vty.attr,
441728674a7SGreg Kroah-Hartman 	&dev_attr_vterm_state.attr,
442728674a7SGreg Kroah-Hartman 	&dev_attr_index.attr,
443728674a7SGreg Kroah-Hartman 	NULL,
444728674a7SGreg Kroah-Hartman };
445728674a7SGreg Kroah-Hartman 
446728674a7SGreg Kroah-Hartman ATTRIBUTE_GROUPS(hvcs_dev);
447728674a7SGreg Kroah-Hartman 
rescan_show(struct device_driver * ddp,char * buf)4484e70a6feSGreg Kroah-Hartman static ssize_t rescan_show(struct device_driver *ddp, char *buf)
449728674a7SGreg Kroah-Hartman {
450728674a7SGreg Kroah-Hartman 	/* A 1 means it is updating, a 0 means it is done updating */
451728674a7SGreg Kroah-Hartman 	return snprintf(buf, PAGE_SIZE, "%d\n", hvcs_rescan_status);
452728674a7SGreg Kroah-Hartman }
453728674a7SGreg Kroah-Hartman 
rescan_store(struct device_driver * ddp,const char * buf,size_t count)4544e70a6feSGreg Kroah-Hartman static ssize_t rescan_store(struct device_driver *ddp, const char * buf,
455728674a7SGreg Kroah-Hartman 		size_t count)
456728674a7SGreg Kroah-Hartman {
457728674a7SGreg Kroah-Hartman 	if ((simple_strtol(buf, NULL, 0) != 1)
458728674a7SGreg Kroah-Hartman 		&& (hvcs_rescan_status != 0))
459728674a7SGreg Kroah-Hartman 		return -EINVAL;
460728674a7SGreg Kroah-Hartman 
461728674a7SGreg Kroah-Hartman 	hvcs_rescan_status = 1;
462728674a7SGreg Kroah-Hartman 	printk(KERN_INFO "HVCS: rescanning partner info for all"
463728674a7SGreg Kroah-Hartman 		" vty-servers.\n");
464728674a7SGreg Kroah-Hartman 	hvcs_rescan_devices_list();
465728674a7SGreg Kroah-Hartman 	hvcs_rescan_status = 0;
466728674a7SGreg Kroah-Hartman 	return count;
467728674a7SGreg Kroah-Hartman }
468728674a7SGreg Kroah-Hartman 
4694e70a6feSGreg Kroah-Hartman static DRIVER_ATTR_RW(rescan);
470728674a7SGreg Kroah-Hartman 
471728674a7SGreg Kroah-Hartman static struct attribute *hvcs_attrs[] = {
472728674a7SGreg Kroah-Hartman 	&driver_attr_rescan.attr,
473728674a7SGreg Kroah-Hartman 	NULL,
474728674a7SGreg Kroah-Hartman };
475728674a7SGreg Kroah-Hartman 
476728674a7SGreg Kroah-Hartman ATTRIBUTE_GROUPS(hvcs);
477728674a7SGreg Kroah-Hartman 
hvcs_kick(void)478728674a7SGreg Kroah-Hartman static void hvcs_kick(void)
479728674a7SGreg Kroah-Hartman {
480728674a7SGreg Kroah-Hartman 	hvcs_kicked = 1;
481728674a7SGreg Kroah-Hartman 	wmb();
482728674a7SGreg Kroah-Hartman 	wake_up_process(hvcs_task);
483728674a7SGreg Kroah-Hartman }
484728674a7SGreg Kroah-Hartman 
hvcs_unthrottle(struct tty_struct * tty)485728674a7SGreg Kroah-Hartman static void hvcs_unthrottle(struct tty_struct *tty)
486728674a7SGreg Kroah-Hartman {
487728674a7SGreg Kroah-Hartman 	struct hvcs_struct *hvcsd = tty->driver_data;
488728674a7SGreg Kroah-Hartman 	unsigned long flags;
489728674a7SGreg Kroah-Hartman 
490728674a7SGreg Kroah-Hartman 	spin_lock_irqsave(&hvcsd->lock, flags);
491728674a7SGreg Kroah-Hartman 	hvcsd->todo_mask |= HVCS_SCHED_READ;
492728674a7SGreg Kroah-Hartman 	spin_unlock_irqrestore(&hvcsd->lock, flags);
493728674a7SGreg Kroah-Hartman 	hvcs_kick();
494728674a7SGreg Kroah-Hartman }
495728674a7SGreg Kroah-Hartman 
hvcs_throttle(struct tty_struct * tty)496728674a7SGreg Kroah-Hartman static void hvcs_throttle(struct tty_struct *tty)
497728674a7SGreg Kroah-Hartman {
498728674a7SGreg Kroah-Hartman 	struct hvcs_struct *hvcsd = tty->driver_data;
499728674a7SGreg Kroah-Hartman 	unsigned long flags;
500728674a7SGreg Kroah-Hartman 
501728674a7SGreg Kroah-Hartman 	spin_lock_irqsave(&hvcsd->lock, flags);
502728674a7SGreg Kroah-Hartman 	vio_disable_interrupts(hvcsd->vdev);
503728674a7SGreg Kroah-Hartman 	spin_unlock_irqrestore(&hvcsd->lock, flags);
504728674a7SGreg Kroah-Hartman }
505728674a7SGreg Kroah-Hartman 
506728674a7SGreg Kroah-Hartman /*
507728674a7SGreg Kroah-Hartman  * If the device is being removed we don't have to worry about this interrupt
508728674a7SGreg Kroah-Hartman  * handler taking any further interrupts because they are disabled which means
509728674a7SGreg Kroah-Hartman  * the hvcs_struct will always be valid in this handler.
510728674a7SGreg Kroah-Hartman  */
hvcs_handle_interrupt(int irq,void * dev_instance)511728674a7SGreg Kroah-Hartman static irqreturn_t hvcs_handle_interrupt(int irq, void *dev_instance)
512728674a7SGreg Kroah-Hartman {
513728674a7SGreg Kroah-Hartman 	struct hvcs_struct *hvcsd = dev_instance;
514728674a7SGreg Kroah-Hartman 
515728674a7SGreg Kroah-Hartman 	spin_lock(&hvcsd->lock);
516728674a7SGreg Kroah-Hartman 	vio_disable_interrupts(hvcsd->vdev);
517728674a7SGreg Kroah-Hartman 	hvcsd->todo_mask |= HVCS_SCHED_READ;
518728674a7SGreg Kroah-Hartman 	spin_unlock(&hvcsd->lock);
519728674a7SGreg Kroah-Hartman 	hvcs_kick();
520728674a7SGreg Kroah-Hartman 
5216968a759SJiri Slaby 	return IRQ_HANDLED;
522728674a7SGreg Kroah-Hartman }
523728674a7SGreg Kroah-Hartman 
524728674a7SGreg Kroah-Hartman /* This function must be called with the hvcsd->lock held */
hvcs_try_write(struct hvcs_struct * hvcsd)525728674a7SGreg Kroah-Hartman static void hvcs_try_write(struct hvcs_struct *hvcsd)
526728674a7SGreg Kroah-Hartman {
527728674a7SGreg Kroah-Hartman 	uint32_t unit_address = hvcsd->vdev->unit_address;
528728674a7SGreg Kroah-Hartman 	struct tty_struct *tty = hvcsd->port.tty;
529728674a7SGreg Kroah-Hartman 	int sent;
530728674a7SGreg Kroah-Hartman 
531728674a7SGreg Kroah-Hartman 	if (hvcsd->todo_mask & HVCS_TRY_WRITE) {
532728674a7SGreg Kroah-Hartman 		/* won't send partial writes */
533728674a7SGreg Kroah-Hartman 		sent = hvc_put_chars(unit_address,
534728674a7SGreg Kroah-Hartman 				&hvcsd->buffer[0],
535728674a7SGreg Kroah-Hartman 				hvcsd->chars_in_buffer );
536728674a7SGreg Kroah-Hartman 		if (sent > 0) {
537728674a7SGreg Kroah-Hartman 			hvcsd->chars_in_buffer = 0;
53825985edcSLucas De Marchi 			/* wmb(); */
539728674a7SGreg Kroah-Hartman 			hvcsd->todo_mask &= ~(HVCS_TRY_WRITE);
540728674a7SGreg Kroah-Hartman 			/* wmb(); */
541728674a7SGreg Kroah-Hartman 
542728674a7SGreg Kroah-Hartman 			/*
543728674a7SGreg Kroah-Hartman 			 * We are still obligated to deliver the data to the
544728674a7SGreg Kroah-Hartman 			 * hypervisor even if the tty has been closed because
545728674a7SGreg Kroah-Hartman 			 * we committed to delivering it.  But don't try to wake
546728674a7SGreg Kroah-Hartman 			 * a non-existent tty.
547728674a7SGreg Kroah-Hartman 			 */
548728674a7SGreg Kroah-Hartman 			if (tty) {
549728674a7SGreg Kroah-Hartman 				tty_wakeup(tty);
550728674a7SGreg Kroah-Hartman 			}
551728674a7SGreg Kroah-Hartman 		}
552728674a7SGreg Kroah-Hartman 	}
553728674a7SGreg Kroah-Hartman }
554728674a7SGreg Kroah-Hartman 
hvcs_io(struct hvcs_struct * hvcsd)555728674a7SGreg Kroah-Hartman static int hvcs_io(struct hvcs_struct *hvcsd)
556728674a7SGreg Kroah-Hartman {
557728674a7SGreg Kroah-Hartman 	uint32_t unit_address;
558728674a7SGreg Kroah-Hartman 	struct tty_struct *tty;
5596968a759SJiri Slaby 	char buf[HVCS_BUFF_LEN] __ALIGNED__;
560728674a7SGreg Kroah-Hartman 	unsigned long flags;
561728674a7SGreg Kroah-Hartman 	int got = 0;
562728674a7SGreg Kroah-Hartman 
56397ef38b8SPeter Hurley 	spin_lock_irqsave(&hvcsd->lock, flags);
564728674a7SGreg Kroah-Hartman 
565728674a7SGreg Kroah-Hartman 	unit_address = hvcsd->vdev->unit_address;
566728674a7SGreg Kroah-Hartman 	tty = hvcsd->port.tty;
567728674a7SGreg Kroah-Hartman 
568728674a7SGreg Kroah-Hartman 	hvcs_try_write(hvcsd);
569728674a7SGreg Kroah-Hartman 
570728674a7SGreg Kroah-Hartman 	if (!tty || tty_throttled(tty)) {
571728674a7SGreg Kroah-Hartman 		hvcsd->todo_mask &= ~(HVCS_READ_MASK);
572227434f8SJiri Slaby 		goto bail;
573728674a7SGreg Kroah-Hartman 	} else if (!(hvcsd->todo_mask & (HVCS_READ_MASK)))
574728674a7SGreg Kroah-Hartman 		goto bail;
575728674a7SGreg Kroah-Hartman 
57605c7cd39SJiri Slaby 	/* remove the read masks */
577728674a7SGreg Kroah-Hartman 	hvcsd->todo_mask &= ~(HVCS_READ_MASK);
578728674a7SGreg Kroah-Hartman 
579728674a7SGreg Kroah-Hartman 	if (tty_buffer_request_room(&hvcsd->port, HVCS_BUFF_LEN) >= HVCS_BUFF_LEN) {
580728674a7SGreg Kroah-Hartman 		got = hvc_get_chars(unit_address,
581728674a7SGreg Kroah-Hartman 				&buf[0],
582728674a7SGreg Kroah-Hartman 				HVCS_BUFF_LEN);
583728674a7SGreg Kroah-Hartman 		tty_insert_flip_string(&hvcsd->port, buf, got);
5840bc1bd09SJiri Slaby 	}
585728674a7SGreg Kroah-Hartman 
5862e124b4aSJiri Slaby 	/* Give the TTY time to process the data we just sent. */
5873d27b05eSWan Jiabing 	if (got)
588728674a7SGreg Kroah-Hartman 		hvcsd->todo_mask |= HVCS_QUICK_READ;
589728674a7SGreg Kroah-Hartman 
590728674a7SGreg Kroah-Hartman 	spin_unlock_irqrestore(&hvcsd->lock, flags);
591728674a7SGreg Kroah-Hartman 	/* This is synch -- FIXME :js: it is not! */
592728674a7SGreg Kroah-Hartman 	if (got)
593728674a7SGreg Kroah-Hartman 		tty_flip_buffer_push(&hvcsd->port);
594728674a7SGreg Kroah-Hartman 	else {
595728674a7SGreg Kroah-Hartman 		/* Do this _after_ the flip_buffer_push */
596728674a7SGreg Kroah-Hartman 		spin_lock_irqsave(&hvcsd->lock, flags);
597728674a7SGreg Kroah-Hartman 		vio_enable_interrupts(hvcsd->vdev);
598728674a7SGreg Kroah-Hartman 		spin_unlock_irqrestore(&hvcsd->lock, flags);
599728674a7SGreg Kroah-Hartman 	}
600728674a7SGreg Kroah-Hartman 
601728674a7SGreg Kroah-Hartman 	return hvcsd->todo_mask;
602728674a7SGreg Kroah-Hartman 
603728674a7SGreg Kroah-Hartman  bail:
604728674a7SGreg Kroah-Hartman 	spin_unlock_irqrestore(&hvcsd->lock, flags);
605728674a7SGreg Kroah-Hartman 	return hvcsd->todo_mask;
606728674a7SGreg Kroah-Hartman }
607728674a7SGreg Kroah-Hartman 
khvcsd(void * unused)608728674a7SGreg Kroah-Hartman static int khvcsd(void *unused)
609728674a7SGreg Kroah-Hartman {
610728674a7SGreg Kroah-Hartman 	struct hvcs_struct *hvcsd;
611728674a7SGreg Kroah-Hartman 	int hvcs_todo_mask;
612728674a7SGreg Kroah-Hartman 
613728674a7SGreg Kroah-Hartman 	__set_current_state(TASK_RUNNING);
614728674a7SGreg Kroah-Hartman 
615728674a7SGreg Kroah-Hartman 	do {
616728674a7SGreg Kroah-Hartman 		hvcs_todo_mask = 0;
617728674a7SGreg Kroah-Hartman 		hvcs_kicked = 0;
618728674a7SGreg Kroah-Hartman 		wmb();
619728674a7SGreg Kroah-Hartman 
620728674a7SGreg Kroah-Hartman 		spin_lock(&hvcs_structs_lock);
621728674a7SGreg Kroah-Hartman 		list_for_each_entry(hvcsd, &hvcs_structs, next) {
622728674a7SGreg Kroah-Hartman 			hvcs_todo_mask |= hvcs_io(hvcsd);
623728674a7SGreg Kroah-Hartman 		}
624728674a7SGreg Kroah-Hartman 		spin_unlock(&hvcs_structs_lock);
625728674a7SGreg Kroah-Hartman 
626728674a7SGreg Kroah-Hartman 		/*
627728674a7SGreg Kroah-Hartman 		 * If any of the hvcs adapters want to try a write or quick read
628728674a7SGreg Kroah-Hartman 		 * don't schedule(), yield a smidgen then execute the hvcs_io
629728674a7SGreg Kroah-Hartman 		 * thread again for those that want the write.
630728674a7SGreg Kroah-Hartman 		 */
631728674a7SGreg Kroah-Hartman 		 if (hvcs_todo_mask & (HVCS_TRY_WRITE | HVCS_QUICK_READ)) {
632728674a7SGreg Kroah-Hartman 			yield();
633728674a7SGreg Kroah-Hartman 			continue;
634728674a7SGreg Kroah-Hartman 		}
635728674a7SGreg Kroah-Hartman 
636728674a7SGreg Kroah-Hartman 		set_current_state(TASK_INTERRUPTIBLE);
637728674a7SGreg Kroah-Hartman 		if (!hvcs_kicked)
638a846c3e5SArvind Yadav 			schedule();
639728674a7SGreg Kroah-Hartman 		__set_current_state(TASK_RUNNING);
640728674a7SGreg Kroah-Hartman 	} while (!kthread_should_stop());
641728674a7SGreg Kroah-Hartman 
642728674a7SGreg Kroah-Hartman 	return 0;
643728674a7SGreg Kroah-Hartman }
644728674a7SGreg Kroah-Hartman 
645728674a7SGreg Kroah-Hartman static const struct vio_device_id hvcs_driver_table[] = {
646728674a7SGreg Kroah-Hartman 	{"serial-server", "hvterm2"},
647728674a7SGreg Kroah-Hartman 	{ "", "" }
648728674a7SGreg Kroah-Hartman };
649728674a7SGreg Kroah-Hartman MODULE_DEVICE_TABLE(vio, hvcs_driver_table);
650728674a7SGreg Kroah-Hartman 
hvcs_return_index(int index)651728674a7SGreg Kroah-Hartman static void hvcs_return_index(int index)
652728674a7SGreg Kroah-Hartman {
653728674a7SGreg Kroah-Hartman 	/* Paranoia check */
654728674a7SGreg Kroah-Hartman 	if (!hvcs_index_list)
655728674a7SGreg Kroah-Hartman 		return;
656728674a7SGreg Kroah-Hartman 	if (index < 0 || index >= hvcs_index_count)
6572cd9fa25SJiri Slaby 		return;
658728674a7SGreg Kroah-Hartman 	if (hvcs_index_list[index] == -1)
6592cd9fa25SJiri Slaby 		return;
660728674a7SGreg Kroah-Hartman 	else
661728674a7SGreg Kroah-Hartman 		hvcs_index_list[index] = -1;
662728674a7SGreg Kroah-Hartman }
663728674a7SGreg Kroah-Hartman 
hvcs_destruct_port(struct tty_port * p)664728674a7SGreg Kroah-Hartman static void hvcs_destruct_port(struct tty_port *p)
665728674a7SGreg Kroah-Hartman {
666728674a7SGreg Kroah-Hartman 	struct hvcs_struct *hvcsd = container_of(p, struct hvcs_struct, port);
667728674a7SGreg Kroah-Hartman 	struct vio_dev *vdev;
668728674a7SGreg Kroah-Hartman 	struct completion *comp;
669728674a7SGreg Kroah-Hartman 	unsigned long flags;
670728674a7SGreg Kroah-Hartman 
671728674a7SGreg Kroah-Hartman 	spin_lock(&hvcs_structs_lock);
672728674a7SGreg Kroah-Hartman 	spin_lock_irqsave(&hvcsd->lock, flags);
673728674a7SGreg Kroah-Hartman 
674728674a7SGreg Kroah-Hartman 	comp = hvcsd->destroyed;
675728674a7SGreg Kroah-Hartman 	/* the list_del poisons the pointers */
676728674a7SGreg Kroah-Hartman 	list_del(&(hvcsd->next));
677728674a7SGreg Kroah-Hartman 
678728674a7SGreg Kroah-Hartman 	if (hvcsd->connected == 1) {
679728674a7SGreg Kroah-Hartman 		hvcs_partner_free(hvcsd);
680728674a7SGreg Kroah-Hartman 		printk(KERN_INFO "HVCS: Closed vty-server@%X and"
681728674a7SGreg Kroah-Hartman 				" partner vty@%X:%d connection.\n",
682728674a7SGreg Kroah-Hartman 				hvcsd->vdev->unit_address,
683728674a7SGreg Kroah-Hartman 				hvcsd->p_unit_address,
684728674a7SGreg Kroah-Hartman 				(uint32_t)hvcsd->p_partition_ID);
685728674a7SGreg Kroah-Hartman 	}
686728674a7SGreg Kroah-Hartman 	printk(KERN_INFO "HVCS: Destroyed hvcs_struct for vty-server@%X.\n",
687728674a7SGreg Kroah-Hartman 			hvcsd->vdev->unit_address);
688728674a7SGreg Kroah-Hartman 
689728674a7SGreg Kroah-Hartman 	vdev = hvcsd->vdev;
690728674a7SGreg Kroah-Hartman 	hvcsd->vdev = NULL;
691728674a7SGreg Kroah-Hartman 
692728674a7SGreg Kroah-Hartman 	hvcsd->p_unit_address = 0;
693728674a7SGreg Kroah-Hartman 	hvcsd->p_partition_ID = 0;
694728674a7SGreg Kroah-Hartman 	hvcsd->destroyed = NULL;
695728674a7SGreg Kroah-Hartman 	hvcs_return_index(hvcsd->index);
6962cd9fa25SJiri Slaby 	memset(&hvcsd->p_location_code[0], 0x00, HVCS_CLC_LENGTH + 1);
6972cd9fa25SJiri Slaby 
6982cd9fa25SJiri Slaby 	spin_unlock_irqrestore(&hvcsd->lock, flags);
6992cd9fa25SJiri Slaby 	spin_unlock(&hvcs_structs_lock);
700728674a7SGreg Kroah-Hartman 
701728674a7SGreg Kroah-Hartman 	kfree(hvcsd);
702728674a7SGreg Kroah-Hartman 	if (comp)
703728674a7SGreg Kroah-Hartman 		complete(comp);
704728674a7SGreg Kroah-Hartman }
705728674a7SGreg Kroah-Hartman 
706728674a7SGreg Kroah-Hartman static const struct tty_port_operations hvcs_port_ops = {
707728674a7SGreg Kroah-Hartman 	.destruct = hvcs_destruct_port,
708728674a7SGreg Kroah-Hartman };
709728674a7SGreg Kroah-Hartman 
hvcs_get_index(void)710728674a7SGreg Kroah-Hartman static int hvcs_get_index(void)
711728674a7SGreg Kroah-Hartman {
712728674a7SGreg Kroah-Hartman 	int i;
713728674a7SGreg Kroah-Hartman 	/* Paranoia check */
714728674a7SGreg Kroah-Hartman 	if (!hvcs_index_list) {
715728674a7SGreg Kroah-Hartman 		printk(KERN_ERR "HVCS: hvcs_index_list NOT valid!.\n");
716728674a7SGreg Kroah-Hartman 		return -EFAULT;
717728674a7SGreg Kroah-Hartman 	}
7189671f099SBill Pemberton 	/* Find the numerically lowest first free index. */
719728674a7SGreg Kroah-Hartman 	for(i = 0; i < hvcs_index_count; i++) {
720728674a7SGreg Kroah-Hartman 		if (hvcs_index_list[i] == -1) {
721728674a7SGreg Kroah-Hartman 			hvcs_index_list[i] = 0;
722728674a7SGreg Kroah-Hartman 			return i;
723c7704d35SBenjamin Herrenschmidt 		}
724728674a7SGreg Kroah-Hartman 	}
725728674a7SGreg Kroah-Hartman 	return -1;
726728674a7SGreg Kroah-Hartman }
727728674a7SGreg Kroah-Hartman 
hvcs_probe(struct vio_dev * dev,const struct vio_device_id * id)728728674a7SGreg Kroah-Hartman static int hvcs_probe(
729728674a7SGreg Kroah-Hartman 	struct vio_dev *dev,
730728674a7SGreg Kroah-Hartman 	const struct vio_device_id *id)
731c7704d35SBenjamin Herrenschmidt {
732c7704d35SBenjamin Herrenschmidt 	struct hvcs_struct *hvcsd;
733c7704d35SBenjamin Herrenschmidt 	int index, rc;
734c7704d35SBenjamin Herrenschmidt 
735c7704d35SBenjamin Herrenschmidt 	if (!dev || !id) {
736c7704d35SBenjamin Herrenschmidt 		printk(KERN_ERR "HVCS: probed with invalid parameter.\n");
737c7704d35SBenjamin Herrenschmidt 		return -EPERM;
738728674a7SGreg Kroah-Hartman 	}
739728674a7SGreg Kroah-Hartman 
740728674a7SGreg Kroah-Hartman 	/* Make sure we are properly initialized */
741728674a7SGreg Kroah-Hartman 	rc = hvcs_initialize();
742728674a7SGreg Kroah-Hartman 	if (rc) {
743728674a7SGreg Kroah-Hartman 		pr_err("HVCS: Failed to initialize core driver.\n");
744728674a7SGreg Kroah-Hartman 		return rc;
745728674a7SGreg Kroah-Hartman 	}
746728674a7SGreg Kroah-Hartman 
747728674a7SGreg Kroah-Hartman 	/* early to avoid cleanup on failure */
7481997cf04SJiri Slaby 	index = hvcs_get_index();
7492cd9fa25SJiri Slaby 	if (index < 0) {
750728674a7SGreg Kroah-Hartman 		return -EFAULT;
751728674a7SGreg Kroah-Hartman 	}
752728674a7SGreg Kroah-Hartman 
753728674a7SGreg Kroah-Hartman 	hvcsd = kzalloc(sizeof(*hvcsd), GFP_KERNEL);
754728674a7SGreg Kroah-Hartman 	if (!hvcsd)
755728674a7SGreg Kroah-Hartman 		return -ENODEV;
756728674a7SGreg Kroah-Hartman 
757728674a7SGreg Kroah-Hartman 	tty_port_init(&hvcsd->port);
758728674a7SGreg Kroah-Hartman 	hvcsd->port.ops = &hvcs_port_ops;
759728674a7SGreg Kroah-Hartman 	spin_lock_init(&hvcsd->lock);
760728674a7SGreg Kroah-Hartman 
761728674a7SGreg Kroah-Hartman 	hvcsd->vdev = dev;
762728674a7SGreg Kroah-Hartman 	dev_set_drvdata(&dev->dev, hvcsd);
763728674a7SGreg Kroah-Hartman 
764728674a7SGreg Kroah-Hartman 	hvcsd->index = index;
765728674a7SGreg Kroah-Hartman 
766728674a7SGreg Kroah-Hartman 	/* hvcsd->index = ++hvcs_struct_count; */
767728674a7SGreg Kroah-Hartman 	hvcsd->chars_in_buffer = 0;
768728674a7SGreg Kroah-Hartman 	hvcsd->todo_mask = 0;
769728674a7SGreg Kroah-Hartman 	hvcsd->connected = 0;
770728674a7SGreg Kroah-Hartman 
771728674a7SGreg Kroah-Hartman 	/*
772728674a7SGreg Kroah-Hartman 	 * This will populate the hvcs_struct's partner info fields for the
773728674a7SGreg Kroah-Hartman 	 * first time.
774728674a7SGreg Kroah-Hartman 	 */
775728674a7SGreg Kroah-Hartman 	if (hvcs_get_pi(hvcsd)) {
776728674a7SGreg Kroah-Hartman 		printk(KERN_ERR "HVCS: Failed to fetch partner"
777728674a7SGreg Kroah-Hartman 			" info for vty-server@%X on device probe.\n",
778728674a7SGreg Kroah-Hartman 			hvcsd->vdev->unit_address);
779728674a7SGreg Kroah-Hartman 	}
780728674a7SGreg Kroah-Hartman 
781728674a7SGreg Kroah-Hartman 	/*
782728674a7SGreg Kroah-Hartman 	 * If a user app opens a tty that corresponds to this vty-server before
783728674a7SGreg Kroah-Hartman 	 * the hvcs_struct has been added to the devices list then the user app
784728674a7SGreg Kroah-Hartman 	 * will get -ENODEV.
785728674a7SGreg Kroah-Hartman 	 */
786728674a7SGreg Kroah-Hartman 	spin_lock(&hvcs_structs_lock);
787728674a7SGreg Kroah-Hartman 	list_add_tail(&(hvcsd->next), &hvcs_structs);
788728674a7SGreg Kroah-Hartman 	spin_unlock(&hvcs_structs_lock);
789728674a7SGreg Kroah-Hartman 
790728674a7SGreg Kroah-Hartman 	printk(KERN_INFO "HVCS: vty-server@%X added to the vio bus.\n", dev->unit_address);
791728674a7SGreg Kroah-Hartman 
792728674a7SGreg Kroah-Hartman 	/*
793728674a7SGreg Kroah-Hartman 	 * DON'T enable interrupts here because there is no user to receive the
794728674a7SGreg Kroah-Hartman 	 * data.
795728674a7SGreg Kroah-Hartman 	 */
796728674a7SGreg Kroah-Hartman 	return 0;
797386a966fSUwe Kleine-König }
798728674a7SGreg Kroah-Hartman 
hvcs_remove(struct vio_dev * dev)799728674a7SGreg Kroah-Hartman static void hvcs_remove(struct vio_dev *dev)
800728674a7SGreg Kroah-Hartman {
801728674a7SGreg Kroah-Hartman 	struct hvcs_struct *hvcsd = dev_get_drvdata(&dev->dev);
802728674a7SGreg Kroah-Hartman 	DECLARE_COMPLETION_ONSTACK(comp);
803728674a7SGreg Kroah-Hartman 	unsigned long flags;
804728674a7SGreg Kroah-Hartman 	struct tty_struct *tty;
805728674a7SGreg Kroah-Hartman 
806728674a7SGreg Kroah-Hartman 	/* By this time the vty-server won't be getting any more interrupts */
8076968a759SJiri Slaby 
808728674a7SGreg Kroah-Hartman 	spin_lock_irqsave(&hvcsd->lock, flags);
809728674a7SGreg Kroah-Hartman 
810728674a7SGreg Kroah-Hartman 	hvcsd->destroyed = &comp;
811728674a7SGreg Kroah-Hartman 	tty = tty_port_tty_get(&hvcsd->port);
812728674a7SGreg Kroah-Hartman 
813728674a7SGreg Kroah-Hartman 	spin_unlock_irqrestore(&hvcsd->lock, flags);
814728674a7SGreg Kroah-Hartman 
8152cd9fa25SJiri Slaby 	/*
816728674a7SGreg Kroah-Hartman 	 * The tty should always be valid at this time unless a
817728674a7SGreg Kroah-Hartman 	 * simultaneous tty close already cleaned up the hvcs_struct.
818728674a7SGreg Kroah-Hartman 	 */
819728674a7SGreg Kroah-Hartman 	if (tty) {
820728674a7SGreg Kroah-Hartman 		tty_vhangup(tty);
821728674a7SGreg Kroah-Hartman 		tty_kref_put(tty);
822728674a7SGreg Kroah-Hartman 	}
823728674a7SGreg Kroah-Hartman 
824728674a7SGreg Kroah-Hartman 	tty_port_put(&hvcsd->port);
825728674a7SGreg Kroah-Hartman 	wait_for_completion(&comp);
826728674a7SGreg Kroah-Hartman 	printk(KERN_INFO "HVCS: vty-server@%X removed from the"
827728674a7SGreg Kroah-Hartman 			" vio bus.\n", dev->unit_address);
828728674a7SGreg Kroah-Hartman };
829728674a7SGreg Kroah-Hartman 
830728674a7SGreg Kroah-Hartman static struct vio_driver hvcs_vio_driver = {
831728674a7SGreg Kroah-Hartman 	.id_table	= hvcs_driver_table,
83291116cbaSBill Pemberton 	.probe		= hvcs_probe,
833728674a7SGreg Kroah-Hartman 	.remove		= hvcs_remove,
834728674a7SGreg Kroah-Hartman 	.name		= hvcs_driver_name,
835728674a7SGreg Kroah-Hartman 	.driver = {
836728674a7SGreg Kroah-Hartman 		.groups = hvcs_groups,
837728674a7SGreg Kroah-Hartman 		.dev_groups = hvcs_dev_groups,
838728674a7SGreg Kroah-Hartman 	},
839728674a7SGreg Kroah-Hartman };
840728674a7SGreg Kroah-Hartman 
841728674a7SGreg Kroah-Hartman /* Only called from hvcs_get_pi please */
hvcs_set_pi(struct hvcs_partner_info * pi,struct hvcs_struct * hvcsd)842728674a7SGreg Kroah-Hartman static void hvcs_set_pi(struct hvcs_partner_info *pi, struct hvcs_struct *hvcsd)
843eb9e109dSWolfram Sang {
844acf01e66SJoe Perches 	hvcsd->p_unit_address = pi->unit_address;
845728674a7SGreg Kroah-Hartman 	hvcsd->p_partition_ID  = pi->partition_ID;
846728674a7SGreg Kroah-Hartman 
847728674a7SGreg Kroah-Hartman 	/* copy the null-term char too */
848728674a7SGreg Kroah-Hartman 	strscpy(hvcsd->p_location_code, pi->location_code,
849728674a7SGreg Kroah-Hartman 		sizeof(hvcsd->p_location_code));
850728674a7SGreg Kroah-Hartman }
851728674a7SGreg Kroah-Hartman 
852728674a7SGreg Kroah-Hartman /*
853728674a7SGreg Kroah-Hartman  * Traverse the list and add the partner info that is found to the hvcs_struct
854728674a7SGreg Kroah-Hartman  * struct entry. NOTE: At this time I know that partner info will return a
855728674a7SGreg Kroah-Hartman  * single entry but in the future there may be multiple partner info entries per
856728674a7SGreg Kroah-Hartman  * vty-server and you'll want to zero out that list and reset it.  If for some
857728674a7SGreg Kroah-Hartman  * reason you have an old version of this driver but there IS more than one
858728674a7SGreg Kroah-Hartman  * partner info then hvcsd->p_* will hold the last partner info data from the
859728674a7SGreg Kroah-Hartman  * firmware query.  A good way to update this code would be to replace the three
860728674a7SGreg Kroah-Hartman  * partner info fields in hvcs_struct with a list of hvcs_partner_info
861728674a7SGreg Kroah-Hartman  * instances.
862728674a7SGreg Kroah-Hartman  *
863728674a7SGreg Kroah-Hartman  * This function must be called with the hvcsd->lock held.
864728674a7SGreg Kroah-Hartman  */
hvcs_get_pi(struct hvcs_struct * hvcsd)865728674a7SGreg Kroah-Hartman static int hvcs_get_pi(struct hvcs_struct *hvcsd)
866728674a7SGreg Kroah-Hartman {
867728674a7SGreg Kroah-Hartman 	struct hvcs_partner_info *pi;
868728674a7SGreg Kroah-Hartman 	uint32_t unit_address = hvcsd->vdev->unit_address;
869728674a7SGreg Kroah-Hartman 	struct list_head head;
870728674a7SGreg Kroah-Hartman 	int retval;
871728674a7SGreg Kroah-Hartman 
872728674a7SGreg Kroah-Hartman 	spin_lock(&hvcs_pi_lock);
873728674a7SGreg Kroah-Hartman 	if (!hvcs_pi_buff) {
874728674a7SGreg Kroah-Hartman 		spin_unlock(&hvcs_pi_lock);
875728674a7SGreg Kroah-Hartman 		return -EFAULT;
876728674a7SGreg Kroah-Hartman 	}
877728674a7SGreg Kroah-Hartman 	retval = hvcs_get_partner_info(unit_address, &head, hvcs_pi_buff);
878728674a7SGreg Kroah-Hartman 	spin_unlock(&hvcs_pi_lock);
879728674a7SGreg Kroah-Hartman 	if (retval) {
880728674a7SGreg Kroah-Hartman 		printk(KERN_ERR "HVCS: Failed to fetch partner"
881728674a7SGreg Kroah-Hartman 			" info for vty-server@%x.\n", unit_address);
882728674a7SGreg Kroah-Hartman 		return retval;
883728674a7SGreg Kroah-Hartman 	}
884728674a7SGreg Kroah-Hartman 
885728674a7SGreg Kroah-Hartman 	/* nixes the values if the partner vty went away */
886728674a7SGreg Kroah-Hartman 	hvcsd->p_unit_address = 0;
887728674a7SGreg Kroah-Hartman 	hvcsd->p_partition_ID = 0;
888728674a7SGreg Kroah-Hartman 
889728674a7SGreg Kroah-Hartman 	list_for_each_entry(pi, &head, node)
890728674a7SGreg Kroah-Hartman 		hvcs_set_pi(pi, hvcsd);
891728674a7SGreg Kroah-Hartman 
892728674a7SGreg Kroah-Hartman 	hvcs_free_partner_info(&head);
893728674a7SGreg Kroah-Hartman 	return 0;
894728674a7SGreg Kroah-Hartman }
895728674a7SGreg Kroah-Hartman 
896728674a7SGreg Kroah-Hartman /*
897728674a7SGreg Kroah-Hartman  * This function is executed by the driver "rescan" sysfs entry.  It shouldn't
898728674a7SGreg Kroah-Hartman  * be executed elsewhere, in order to prevent deadlock issues.
899728674a7SGreg Kroah-Hartman  */
hvcs_rescan_devices_list(void)900728674a7SGreg Kroah-Hartman static int hvcs_rescan_devices_list(void)
901728674a7SGreg Kroah-Hartman {
902728674a7SGreg Kroah-Hartman 	struct hvcs_struct *hvcsd;
903728674a7SGreg Kroah-Hartman 	unsigned long flags;
904728674a7SGreg Kroah-Hartman 
905728674a7SGreg Kroah-Hartman 	spin_lock(&hvcs_structs_lock);
906728674a7SGreg Kroah-Hartman 
907728674a7SGreg Kroah-Hartman 	list_for_each_entry(hvcsd, &hvcs_structs, next) {
908728674a7SGreg Kroah-Hartman 		spin_lock_irqsave(&hvcsd->lock, flags);
909728674a7SGreg Kroah-Hartman 		hvcs_get_pi(hvcsd);
910728674a7SGreg Kroah-Hartman 		spin_unlock_irqrestore(&hvcsd->lock, flags);
911728674a7SGreg Kroah-Hartman 	}
912728674a7SGreg Kroah-Hartman 
913728674a7SGreg Kroah-Hartman 	spin_unlock(&hvcs_structs_lock);
914728674a7SGreg Kroah-Hartman 
915728674a7SGreg Kroah-Hartman 	return 0;
916728674a7SGreg Kroah-Hartman }
917728674a7SGreg Kroah-Hartman 
918728674a7SGreg Kroah-Hartman /*
919728674a7SGreg Kroah-Hartman  * Farm this off into its own function because it could be more complex once
920728674a7SGreg Kroah-Hartman  * multiple partners support is added. This function should be called with
921728674a7SGreg Kroah-Hartman  * the hvcsd->lock held.
922728674a7SGreg Kroah-Hartman  */
hvcs_has_pi(struct hvcs_struct * hvcsd)923728674a7SGreg Kroah-Hartman static int hvcs_has_pi(struct hvcs_struct *hvcsd)
924728674a7SGreg Kroah-Hartman {
925728674a7SGreg Kroah-Hartman 	if ((!hvcsd->p_unit_address) || (!hvcsd->p_partition_ID))
926728674a7SGreg Kroah-Hartman 		return 0;
927728674a7SGreg Kroah-Hartman 	return 1;
928728674a7SGreg Kroah-Hartman }
929728674a7SGreg Kroah-Hartman 
930728674a7SGreg Kroah-Hartman /*
931728674a7SGreg Kroah-Hartman  * NOTE: It is possible that the super admin removed a partner vty and then
932728674a7SGreg Kroah-Hartman  * added a different vty as the new partner.
933728674a7SGreg Kroah-Hartman  *
934728674a7SGreg Kroah-Hartman  * This function must be called with the hvcsd->lock held.
935728674a7SGreg Kroah-Hartman  */
hvcs_partner_connect(struct hvcs_struct * hvcsd)936728674a7SGreg Kroah-Hartman static int hvcs_partner_connect(struct hvcs_struct *hvcsd)
937728674a7SGreg Kroah-Hartman {
938728674a7SGreg Kroah-Hartman 	int retval;
939728674a7SGreg Kroah-Hartman 	unsigned int unit_address = hvcsd->vdev->unit_address;
940728674a7SGreg Kroah-Hartman 
941728674a7SGreg Kroah-Hartman 	/*
942728674a7SGreg Kroah-Hartman 	 * If there wasn't any pi when the device was added it doesn't meant
943728674a7SGreg Kroah-Hartman 	 * there isn't any now.  This driver isn't notified when a new partner
944728674a7SGreg Kroah-Hartman 	 * vty is added to a vty-server so we discover changes on our own.
945728674a7SGreg Kroah-Hartman 	 * Please see comments in hvcs_register_connection() for justification
946728674a7SGreg Kroah-Hartman 	 * of this bizarre code.
947728674a7SGreg Kroah-Hartman 	 */
948728674a7SGreg Kroah-Hartman 	retval = hvcs_register_connection(unit_address,
949728674a7SGreg Kroah-Hartman 			hvcsd->p_partition_ID,
950728674a7SGreg Kroah-Hartman 			hvcsd->p_unit_address);
951728674a7SGreg Kroah-Hartman 	if (!retval) {
952728674a7SGreg Kroah-Hartman 		hvcsd->connected = 1;
953728674a7SGreg Kroah-Hartman 		return 0;
954728674a7SGreg Kroah-Hartman 	} else if (retval != -EINVAL)
955728674a7SGreg Kroah-Hartman 		return retval;
956728674a7SGreg Kroah-Hartman 
957728674a7SGreg Kroah-Hartman 	/*
958728674a7SGreg Kroah-Hartman 	 * As per the spec re-get the pi and try again if -EINVAL after the
959728674a7SGreg Kroah-Hartman 	 * first connection attempt.
960728674a7SGreg Kroah-Hartman 	 */
961728674a7SGreg Kroah-Hartman 	if (hvcs_get_pi(hvcsd))
962728674a7SGreg Kroah-Hartman 		return -ENOMEM;
963728674a7SGreg Kroah-Hartman 
964728674a7SGreg Kroah-Hartman 	if (!hvcs_has_pi(hvcsd))
965728674a7SGreg Kroah-Hartman 		return -ENODEV;
966728674a7SGreg Kroah-Hartman 
967728674a7SGreg Kroah-Hartman 	retval = hvcs_register_connection(unit_address,
968728674a7SGreg Kroah-Hartman 			hvcsd->p_partition_ID,
969728674a7SGreg Kroah-Hartman 			hvcsd->p_unit_address);
970728674a7SGreg Kroah-Hartman 	if (retval != -EINVAL) {
971728674a7SGreg Kroah-Hartman 		hvcsd->connected = 1;
972728674a7SGreg Kroah-Hartman 		return retval;
973728674a7SGreg Kroah-Hartman 	}
974728674a7SGreg Kroah-Hartman 
975728674a7SGreg Kroah-Hartman 	/*
976728674a7SGreg Kroah-Hartman 	 * EBUSY is the most likely scenario though the vty could have been
977728674a7SGreg Kroah-Hartman 	 * removed or there really could be an hcall error due to the parameter
978728674a7SGreg Kroah-Hartman 	 * data but thanks to ambiguous firmware return codes we can't really
979728674a7SGreg Kroah-Hartman 	 * tell.
980728674a7SGreg Kroah-Hartman 	 */
981728674a7SGreg Kroah-Hartman 	printk(KERN_INFO "HVCS: vty-server or partner"
982728674a7SGreg Kroah-Hartman 			" vty is busy.  Try again later.\n");
983728674a7SGreg Kroah-Hartman 	return -EBUSY;
984728674a7SGreg Kroah-Hartman }
985728674a7SGreg Kroah-Hartman 
986728674a7SGreg Kroah-Hartman /* This function must be called with the hvcsd->lock held */
hvcs_partner_free(struct hvcs_struct * hvcsd)987728674a7SGreg Kroah-Hartman static void hvcs_partner_free(struct hvcs_struct *hvcsd)
988728674a7SGreg Kroah-Hartman {
989728674a7SGreg Kroah-Hartman 	int retval;
990728674a7SGreg Kroah-Hartman 	do {
991728674a7SGreg Kroah-Hartman 		retval = hvcs_free_connection(hvcsd->vdev->unit_address);
992728674a7SGreg Kroah-Hartman 	} while (retval == -EBUSY);
993728674a7SGreg Kroah-Hartman 	hvcsd->connected = 0;
994728674a7SGreg Kroah-Hartman }
995728674a7SGreg Kroah-Hartman 
996728674a7SGreg Kroah-Hartman /* This helper function must be called WITHOUT the hvcsd->lock held */
hvcs_enable_device(struct hvcs_struct * hvcsd,uint32_t unit_address,unsigned int irq,struct vio_dev * vdev)997728674a7SGreg Kroah-Hartman static int hvcs_enable_device(struct hvcs_struct *hvcsd, uint32_t unit_address,
998728674a7SGreg Kroah-Hartman 		unsigned int irq, struct vio_dev *vdev)
999728674a7SGreg Kroah-Hartman {
1000728674a7SGreg Kroah-Hartman 	unsigned long flags;
1001728674a7SGreg Kroah-Hartman 	int rc;
1002873b4f18SGreg Kroah-Hartman 
1003873b4f18SGreg Kroah-Hartman 	/*
1004728674a7SGreg Kroah-Hartman 	 * It is possible that the vty-server was removed between the time that
1005728674a7SGreg Kroah-Hartman 	 * the conn was registered and now.
1006728674a7SGreg Kroah-Hartman 	 */
1007728674a7SGreg Kroah-Hartman 	rc = request_irq(irq, &hvcs_handle_interrupt, 0, "ibmhvcs", hvcsd);
1008728674a7SGreg Kroah-Hartman 	if (!rc) {
1009728674a7SGreg Kroah-Hartman 		/*
1010728674a7SGreg Kroah-Hartman 		 * It is possible the vty-server was removed after the irq was
1011728674a7SGreg Kroah-Hartman 		 * requested but before we have time to enable interrupts.
1012728674a7SGreg Kroah-Hartman 		 */
1013728674a7SGreg Kroah-Hartman 		if (vio_enable_interrupts(vdev) == H_SUCCESS)
1014728674a7SGreg Kroah-Hartman 			return 0;
1015728674a7SGreg Kroah-Hartman 		else {
1016728674a7SGreg Kroah-Hartman 			printk(KERN_ERR "HVCS: int enable failed for"
1017728674a7SGreg Kroah-Hartman 					" vty-server@%X.\n", unit_address);
1018728674a7SGreg Kroah-Hartman 			free_irq(irq, hvcsd);
1019728674a7SGreg Kroah-Hartman 		}
1020728674a7SGreg Kroah-Hartman 	} else
1021728674a7SGreg Kroah-Hartman 		printk(KERN_ERR "HVCS: irq req failed for"
1022728674a7SGreg Kroah-Hartman 				" vty-server@%X.\n", unit_address);
1023728674a7SGreg Kroah-Hartman 
1024728674a7SGreg Kroah-Hartman 	spin_lock_irqsave(&hvcsd->lock, flags);
1025728674a7SGreg Kroah-Hartman 	hvcs_partner_free(hvcsd);
1026728674a7SGreg Kroah-Hartman 	spin_unlock_irqrestore(&hvcsd->lock, flags);
1027728674a7SGreg Kroah-Hartman 
1028728674a7SGreg Kroah-Hartman 	return rc;
1029728674a7SGreg Kroah-Hartman 
1030728674a7SGreg Kroah-Hartman }
1031728674a7SGreg Kroah-Hartman 
1032728674a7SGreg Kroah-Hartman /*
1033728674a7SGreg Kroah-Hartman  * This always increments the kref ref count if the call is successful.
1034728674a7SGreg Kroah-Hartman  * Please remember to dec when you are done with the instance.
1035728674a7SGreg Kroah-Hartman  *
1036410235fdSJiri Slaby  * NOTICE: Do NOT hold either the hvcs_struct.lock or hvcs_structs_lock when
1037728674a7SGreg Kroah-Hartman  * calling this function or you will get deadlock.
1038728674a7SGreg Kroah-Hartman  */
hvcs_get_by_index(int index)1039728674a7SGreg Kroah-Hartman static struct hvcs_struct *hvcs_get_by_index(int index)
1040728674a7SGreg Kroah-Hartman {
1041728674a7SGreg Kroah-Hartman 	struct hvcs_struct *hvcsd;
1042728674a7SGreg Kroah-Hartman 	unsigned long flags;
10432cd9fa25SJiri Slaby 
1044728674a7SGreg Kroah-Hartman 	spin_lock(&hvcs_structs_lock);
1045728674a7SGreg Kroah-Hartman 	list_for_each_entry(hvcsd, &hvcs_structs, next) {
1046728674a7SGreg Kroah-Hartman 		spin_lock_irqsave(&hvcsd->lock, flags);
1047728674a7SGreg Kroah-Hartman 		if (hvcsd->index == index) {
1048728674a7SGreg Kroah-Hartman 			tty_port_get(&hvcsd->port);
1049728674a7SGreg Kroah-Hartman 			spin_unlock_irqrestore(&hvcsd->lock, flags);
1050728674a7SGreg Kroah-Hartman 			spin_unlock(&hvcs_structs_lock);
1051410235fdSJiri Slaby 			return hvcsd;
1052410235fdSJiri Slaby 		}
1053728674a7SGreg Kroah-Hartman 		spin_unlock_irqrestore(&hvcsd->lock, flags);
1054728674a7SGreg Kroah-Hartman 	}
105527bf7c43SJiri Slaby 	spin_unlock(&hvcs_structs_lock);
1056728674a7SGreg Kroah-Hartman 
1057728674a7SGreg Kroah-Hartman 	return NULL;
1058728674a7SGreg Kroah-Hartman }
105997d15089SJiri Slaby 
hvcs_install(struct tty_driver * driver,struct tty_struct * tty)106097d15089SJiri Slaby static int hvcs_install(struct tty_driver *driver, struct tty_struct *tty)
106197d15089SJiri Slaby {
1062728674a7SGreg Kroah-Hartman 	struct hvcs_struct *hvcsd;
1063728674a7SGreg Kroah-Hartman 	struct vio_dev *vdev;
1064728674a7SGreg Kroah-Hartman 	unsigned long unit_address, flags;
1065728674a7SGreg Kroah-Hartman 	unsigned int irq;
1066728674a7SGreg Kroah-Hartman 	int retval;
106797d15089SJiri Slaby 
106897d15089SJiri Slaby 	/*
1069728674a7SGreg Kroah-Hartman 	 * Is there a vty-server that shares the same index?
1070728674a7SGreg Kroah-Hartman 	 * This function increments the kref index.
1071728674a7SGreg Kroah-Hartman 	 */
1072728674a7SGreg Kroah-Hartman 	hvcsd = hvcs_get_by_index(tty->index);
1073728674a7SGreg Kroah-Hartman 	if (!hvcsd) {
1074728674a7SGreg Kroah-Hartman 		printk(KERN_WARNING "HVCS: open failed, no device associated"
1075728674a7SGreg Kroah-Hartman 				" with tty->index %d.\n", tty->index);
107697d15089SJiri Slaby 		return -ENODEV;
107797d15089SJiri Slaby 	}
107897d15089SJiri Slaby 
107997d15089SJiri Slaby 	spin_lock_irqsave(&hvcsd->lock, flags);
108097d15089SJiri Slaby 
108197d15089SJiri Slaby 	if (hvcsd->connected == 0) {
108297d15089SJiri Slaby 		retval = hvcs_partner_connect(hvcsd);
108397d15089SJiri Slaby 		if (retval) {
1084728674a7SGreg Kroah-Hartman 			spin_unlock_irqrestore(&hvcsd->lock, flags);
108527bf7c43SJiri Slaby 			printk(KERN_WARNING "HVCS: partner connect failed.\n");
10866968a759SJiri Slaby 			goto err_put;
1087728674a7SGreg Kroah-Hartman 		}
1088728674a7SGreg Kroah-Hartman 	}
1089728674a7SGreg Kroah-Hartman 
1090728674a7SGreg Kroah-Hartman 	hvcsd->port.count = 0;
1091728674a7SGreg Kroah-Hartman 	hvcsd->port.tty = tty;
1092728674a7SGreg Kroah-Hartman 	tty->driver_data = hvcsd;
1093728674a7SGreg Kroah-Hartman 
1094728674a7SGreg Kroah-Hartman 	memset(&hvcsd->buffer[0], 0x00, HVCS_BUFF_LEN);
1095728674a7SGreg Kroah-Hartman 
1096728674a7SGreg Kroah-Hartman 	/*
1097728674a7SGreg Kroah-Hartman 	 * Save these in the spinlock for the enable operations that need them
1098728674a7SGreg Kroah-Hartman 	 * outside of the spinlock.
1099728674a7SGreg Kroah-Hartman 	 */
1100728674a7SGreg Kroah-Hartman 	irq = hvcsd->vdev->irq;
1101728674a7SGreg Kroah-Hartman 	vdev = hvcsd->vdev;
1102728674a7SGreg Kroah-Hartman 	unit_address = hvcsd->vdev->unit_address;
1103728674a7SGreg Kroah-Hartman 
1104728674a7SGreg Kroah-Hartman 	hvcsd->todo_mask |= HVCS_SCHED_READ;
1105728674a7SGreg Kroah-Hartman 	spin_unlock_irqrestore(&hvcsd->lock, flags);
110697d15089SJiri Slaby 
110797d15089SJiri Slaby 	/*
1108728674a7SGreg Kroah-Hartman 	 * This must be done outside of the spinlock because it requests irqs
110997d15089SJiri Slaby 	 * and will grab the spinlock and free the connection if it fails.
1110728674a7SGreg Kroah-Hartman 	 */
1111728674a7SGreg Kroah-Hartman 	retval = hvcs_enable_device(hvcsd, unit_address, irq, vdev);
111227bf7c43SJiri Slaby 	if (retval) {
111327bf7c43SJiri Slaby 		printk(KERN_WARNING "HVCS: enable device failed.\n");
111427bf7c43SJiri Slaby 		goto err_put;
1115728674a7SGreg Kroah-Hartman 	}
111627bf7c43SJiri Slaby 
111727bf7c43SJiri Slaby 	retval = tty_port_install(&hvcsd->port, driver, tty);
111827bf7c43SJiri Slaby 	if (retval)
111927bf7c43SJiri Slaby 		goto err_irq;
112027bf7c43SJiri Slaby 
112127bf7c43SJiri Slaby 	return 0;
112227bf7c43SJiri Slaby err_irq:
112327bf7c43SJiri Slaby 	spin_lock_irqsave(&hvcsd->lock, flags);
112427bf7c43SJiri Slaby 	vio_disable_interrupts(hvcsd->vdev);
112527bf7c43SJiri Slaby 	spin_unlock_irqrestore(&hvcsd->lock, flags);
112627bf7c43SJiri Slaby 	free_irq(irq, hvcsd);
112727bf7c43SJiri Slaby err_put:
112827bf7c43SJiri Slaby 	tty_port_put(&hvcsd->port);
112927bf7c43SJiri Slaby 
113027bf7c43SJiri Slaby 	return retval;
113127bf7c43SJiri Slaby }
113227bf7c43SJiri Slaby 
113327bf7c43SJiri Slaby /*
113427bf7c43SJiri Slaby  * This is invoked via the tty_open interface when a user app connects to the
113527bf7c43SJiri Slaby  * /dev node.
1136728674a7SGreg Kroah-Hartman  */
hvcs_open(struct tty_struct * tty,struct file * filp)1137728674a7SGreg Kroah-Hartman static int hvcs_open(struct tty_struct *tty, struct file *filp)
11381997cf04SJiri Slaby {
1139728674a7SGreg Kroah-Hartman 	struct hvcs_struct *hvcsd = tty->driver_data;
1140728674a7SGreg Kroah-Hartman 	unsigned long flags;
1141728674a7SGreg Kroah-Hartman 
1142728674a7SGreg Kroah-Hartman 	spin_lock_irqsave(&hvcsd->lock, flags);
1143728674a7SGreg Kroah-Hartman 	hvcsd->port.count++;
1144728674a7SGreg Kroah-Hartman 	hvcsd->todo_mask |= HVCS_SCHED_READ;
1145728674a7SGreg Kroah-Hartman 	spin_unlock_irqrestore(&hvcsd->lock, flags);
1146728674a7SGreg Kroah-Hartman 
1147728674a7SGreg Kroah-Hartman 	hvcs_kick();
1148728674a7SGreg Kroah-Hartman 
1149728674a7SGreg Kroah-Hartman 	printk(KERN_INFO "HVCS: vty-server@%X connection opened.\n",
1150728674a7SGreg Kroah-Hartman 		hvcsd->vdev->unit_address );
1151728674a7SGreg Kroah-Hartman 
1152728674a7SGreg Kroah-Hartman 	return 0;
1153728674a7SGreg Kroah-Hartman }
1154d4e33facSAlan Cox 
hvcs_close(struct tty_struct * tty,struct file * filp)1155728674a7SGreg Kroah-Hartman static void hvcs_close(struct tty_struct *tty, struct file *filp)
1156728674a7SGreg Kroah-Hartman {
1157728674a7SGreg Kroah-Hartman 	struct hvcs_struct *hvcsd;
1158728674a7SGreg Kroah-Hartman 	unsigned long flags;
1159728674a7SGreg Kroah-Hartman 	int irq;
1160728674a7SGreg Kroah-Hartman 
1161728674a7SGreg Kroah-Hartman 	/*
1162728674a7SGreg Kroah-Hartman 	 * Is someone trying to close the file associated with this device after
1163728674a7SGreg Kroah-Hartman 	 * we have hung up?  If so tty->driver_data wouldn't be valid.
1164728674a7SGreg Kroah-Hartman 	 */
1165728674a7SGreg Kroah-Hartman 	if (tty_hung_up_p(filp))
1166728674a7SGreg Kroah-Hartman 		return;
1167728674a7SGreg Kroah-Hartman 
1168728674a7SGreg Kroah-Hartman 	/*
1169728674a7SGreg Kroah-Hartman 	 * No driver_data means that this close was probably issued after a
1170728674a7SGreg Kroah-Hartman 	 * failed hvcs_open by the tty layer's release_dev() api and we can just
1171728674a7SGreg Kroah-Hartman 	 * exit cleanly.
1172728674a7SGreg Kroah-Hartman 	 */
1173728674a7SGreg Kroah-Hartman 	if (!tty->driver_data)
11741997cf04SJiri Slaby 		return;
1175728674a7SGreg Kroah-Hartman 
1176728674a7SGreg Kroah-Hartman 	hvcsd = tty->driver_data;
1177728674a7SGreg Kroah-Hartman 
1178728674a7SGreg Kroah-Hartman 	spin_lock_irqsave(&hvcsd->lock, flags);
1179728674a7SGreg Kroah-Hartman 	if (hvcsd->port.count == 0) {
1180728674a7SGreg Kroah-Hartman 		spin_unlock_irqrestore(&hvcsd->lock, flags);
1181728674a7SGreg Kroah-Hartman 		return;
1182728674a7SGreg Kroah-Hartman 	} else if (--hvcsd->port.count == 0) {
11836968a759SJiri Slaby 
1184728674a7SGreg Kroah-Hartman 		vio_disable_interrupts(hvcsd->vdev);
1185728674a7SGreg Kroah-Hartman 
1186728674a7SGreg Kroah-Hartman 		/*
1187728674a7SGreg Kroah-Hartman 		 * NULL this early so that the kernel_thread doesn't try to
118879c1faa4SPeter Hurley 		 * execute any operations on the TTY even though it is obligated
1189728674a7SGreg Kroah-Hartman 		 * to deliver any pending I/O to the hypervisor.
1190728674a7SGreg Kroah-Hartman 		 */
1191728674a7SGreg Kroah-Hartman 		hvcsd->port.tty = NULL;
11921997cf04SJiri Slaby 
1193228a9ff8SColin Ian King 		irq = hvcsd->vdev->irq;
11941997cf04SJiri Slaby 		spin_unlock_irqrestore(&hvcsd->lock, flags);
1195728674a7SGreg Kroah-Hartman 
1196728674a7SGreg Kroah-Hartman 		tty_wait_until_sent(tty, HVCS_CLOSE_WAIT);
1197728674a7SGreg Kroah-Hartman 
119827bf7c43SJiri Slaby 		free_irq(irq, hvcsd);
119927bf7c43SJiri Slaby 		return;
120027bf7c43SJiri Slaby 	} else if (hvcsd->port.count < 0) {
120127bf7c43SJiri Slaby 		printk(KERN_ERR "HVCS: vty-server@%X open_count: %d is mismanaged.\n",
120227bf7c43SJiri Slaby 		hvcsd->vdev->unit_address, hvcsd->port.count);
120327bf7c43SJiri Slaby 	}
120463ffcbdaSTyrel Datwyler 
120563ffcbdaSTyrel Datwyler 	spin_unlock_irqrestore(&hvcsd->lock, flags);
120663ffcbdaSTyrel Datwyler }
120763ffcbdaSTyrel Datwyler 
hvcs_cleanup(struct tty_struct * tty)120863ffcbdaSTyrel Datwyler static void hvcs_cleanup(struct tty_struct * tty)
120963ffcbdaSTyrel Datwyler {
121063ffcbdaSTyrel Datwyler 	struct hvcs_struct *hvcsd = tty->driver_data;
12112cd9fa25SJiri Slaby 
1212728674a7SGreg Kroah-Hartman 	/*
1213728674a7SGreg Kroah-Hartman 	 * This line is important because it tells hvcs_open that this
1214728674a7SGreg Kroah-Hartman 	 * device needs to be re-configured the next time hvcs_open is
1215728674a7SGreg Kroah-Hartman 	 * called.
1216728674a7SGreg Kroah-Hartman 	 */
1217728674a7SGreg Kroah-Hartman 	tty->driver_data = NULL;
1218728674a7SGreg Kroah-Hartman 
1219d4e33facSAlan Cox 	tty_port_put(&hvcsd->port);
1220728674a7SGreg Kroah-Hartman }
1221728674a7SGreg Kroah-Hartman 
hvcs_hangup(struct tty_struct * tty)1222728674a7SGreg Kroah-Hartman static void hvcs_hangup(struct tty_struct * tty)
12231997cf04SJiri Slaby {
1224728674a7SGreg Kroah-Hartman 	struct hvcs_struct *hvcsd = tty->driver_data;
1225728674a7SGreg Kroah-Hartman 	unsigned long flags;
1226728674a7SGreg Kroah-Hartman 	int irq;
1227728674a7SGreg Kroah-Hartman 
1228728674a7SGreg Kroah-Hartman 	spin_lock_irqsave(&hvcsd->lock, flags);
1229728674a7SGreg Kroah-Hartman 
1230728674a7SGreg Kroah-Hartman 	/*
1231728674a7SGreg Kroah-Hartman 	 * Don't kref put inside the spinlock because the destruction
1232728674a7SGreg Kroah-Hartman 	 * callback may use the spinlock and it may get called before the
1233728674a7SGreg Kroah-Hartman 	 * spinlock has been released.
1234728674a7SGreg Kroah-Hartman 	 */
12356968a759SJiri Slaby 	vio_disable_interrupts(hvcsd->vdev);
12366968a759SJiri Slaby 
1237728674a7SGreg Kroah-Hartman 	hvcsd->todo_mask = 0;
12381997cf04SJiri Slaby 	hvcsd->port.tty = NULL;
1239728674a7SGreg Kroah-Hartman 	hvcsd->port.count = 0;
1240728674a7SGreg Kroah-Hartman 
1241728674a7SGreg Kroah-Hartman 	/* This will drop any buffered data on the floor which is OK in a hangup
1242728674a7SGreg Kroah-Hartman 	 * scenario. */
1243728674a7SGreg Kroah-Hartman 	memset(&hvcsd->buffer[0], 0x00, HVCS_BUFF_LEN);
1244728674a7SGreg Kroah-Hartman 	hvcsd->chars_in_buffer = 0;
1245728674a7SGreg Kroah-Hartman 
1246728674a7SGreg Kroah-Hartman 	irq = hvcsd->vdev->irq;
1247728674a7SGreg Kroah-Hartman 
1248728674a7SGreg Kroah-Hartman 	spin_unlock_irqrestore(&hvcsd->lock, flags);
1249728674a7SGreg Kroah-Hartman 
1250728674a7SGreg Kroah-Hartman 	free_irq(irq, hvcsd);
1251728674a7SGreg Kroah-Hartman }
1252728674a7SGreg Kroah-Hartman 
1253728674a7SGreg Kroah-Hartman /*
1254728674a7SGreg Kroah-Hartman  * NOTE: This is almost always from_user since user level apps interact with the
1255728674a7SGreg Kroah-Hartman  * /dev nodes. I'm trusting that if hvcs_write gets called and interrupted by
1256728674a7SGreg Kroah-Hartman  * hvcs_remove (which removes the target device and executes tty_hangup()) that
1257728674a7SGreg Kroah-Hartman  * tty_hangup will allow hvcs_write time to complete execution before it
1258728674a7SGreg Kroah-Hartman  * terminates our device.
1259728674a7SGreg Kroah-Hartman  */
hvcs_write(struct tty_struct * tty,const u8 * buf,size_t count)1260728674a7SGreg Kroah-Hartman static ssize_t hvcs_write(struct tty_struct *tty, const u8 *buf, size_t count)
1261728674a7SGreg Kroah-Hartman {
1262728674a7SGreg Kroah-Hartman 	struct hvcs_struct *hvcsd = tty->driver_data;
12632cd9fa25SJiri Slaby 	unsigned int unit_address;
1264728674a7SGreg Kroah-Hartman 	const unsigned char *charbuf;
1265728674a7SGreg Kroah-Hartman 	unsigned long flags;
1266728674a7SGreg Kroah-Hartman 	size_t total_sent = 0;
1267728674a7SGreg Kroah-Hartman 	size_t tosend = 0;
1268728674a7SGreg Kroah-Hartman 	int result = 0;
1269728674a7SGreg Kroah-Hartman 
1270728674a7SGreg Kroah-Hartman 	/*
1271728674a7SGreg Kroah-Hartman 	 * If they don't check the return code off of their open they may
1272728674a7SGreg Kroah-Hartman 	 * attempt this even if there is no connected device.
1273728674a7SGreg Kroah-Hartman 	 */
1274728674a7SGreg Kroah-Hartman 	if (!hvcsd)
1275728674a7SGreg Kroah-Hartman 		return -ENODEV;
1276728674a7SGreg Kroah-Hartman 
1277728674a7SGreg Kroah-Hartman 	/* Reasonable size to prevent user level flooding */
1278728674a7SGreg Kroah-Hartman 	if (count > HVCS_MAX_FROM_USER) {
1279728674a7SGreg Kroah-Hartman 		printk(KERN_WARNING "HVCS write: count being truncated to"
1280728674a7SGreg Kroah-Hartman 				" HVCS_MAX_FROM_USER.\n");
1281728674a7SGreg Kroah-Hartman 		count = HVCS_MAX_FROM_USER;
1282728674a7SGreg Kroah-Hartman 	}
1283728674a7SGreg Kroah-Hartman 
1284728674a7SGreg Kroah-Hartman 	charbuf = buf;
1285728674a7SGreg Kroah-Hartman 
1286728674a7SGreg Kroah-Hartman 	spin_lock_irqsave(&hvcsd->lock, flags);
1287728674a7SGreg Kroah-Hartman 
1288728674a7SGreg Kroah-Hartman 	/*
1289728674a7SGreg Kroah-Hartman 	 * Somehow an open succeeded but the device was removed or the
1290728674a7SGreg Kroah-Hartman 	 * connection terminated between the vty-server and partner vty during
1291728674a7SGreg Kroah-Hartman 	 * the middle of a write operation?  This is a crummy place to do this
1292728674a7SGreg Kroah-Hartman 	 * but we want to keep it all in the spinlock.
1293728674a7SGreg Kroah-Hartman 	 */
1294728674a7SGreg Kroah-Hartman 	if (hvcsd->port.count <= 0) {
1295728674a7SGreg Kroah-Hartman 		spin_unlock_irqrestore(&hvcsd->lock, flags);
1296728674a7SGreg Kroah-Hartman 		return -ENODEV;
1297728674a7SGreg Kroah-Hartman 	}
1298728674a7SGreg Kroah-Hartman 
1299728674a7SGreg Kroah-Hartman 	unit_address = hvcsd->vdev->unit_address;
1300728674a7SGreg Kroah-Hartman 
1301728674a7SGreg Kroah-Hartman 	while (count > 0) {
1302728674a7SGreg Kroah-Hartman 		tosend = min_t(size_t, count,
1303728674a7SGreg Kroah-Hartman 			       (HVCS_BUFF_LEN - hvcsd->chars_in_buffer));
130425985edcSLucas De Marchi 		/*
1305728674a7SGreg Kroah-Hartman 		 * No more space, this probably means that the last call to
1306728674a7SGreg Kroah-Hartman 		 * hvcs_write() didn't succeed and the buffer was filled up.
1307728674a7SGreg Kroah-Hartman 		 */
1308728674a7SGreg Kroah-Hartman 		if (!tosend)
13091997cf04SJiri Slaby 			break;
1310728674a7SGreg Kroah-Hartman 
1311728674a7SGreg Kroah-Hartman 		memcpy(&hvcsd->buffer[hvcsd->chars_in_buffer],
1312728674a7SGreg Kroah-Hartman 				&charbuf[total_sent],
1313728674a7SGreg Kroah-Hartman 				tosend);
1314728674a7SGreg Kroah-Hartman 
1315728674a7SGreg Kroah-Hartman 		hvcsd->chars_in_buffer += tosend;
1316728674a7SGreg Kroah-Hartman 
1317728674a7SGreg Kroah-Hartman 		result = 0;
1318728674a7SGreg Kroah-Hartman 
1319728674a7SGreg Kroah-Hartman 		/*
1320728674a7SGreg Kroah-Hartman 		 * If this is true then we don't want to try writing to the
1321728674a7SGreg Kroah-Hartman 		 * hypervisor because that is the kernel_threads job now.  We'll
1322728674a7SGreg Kroah-Hartman 		 * just add to the buffer.
1323728674a7SGreg Kroah-Hartman 		 */
1324728674a7SGreg Kroah-Hartman 		if (!(hvcsd->todo_mask & HVCS_TRY_WRITE))
1325728674a7SGreg Kroah-Hartman 			/* won't send partial writes */
1326728674a7SGreg Kroah-Hartman 			result = hvc_put_chars(unit_address,
1327728674a7SGreg Kroah-Hartman 					&hvcsd->buffer[0],
1328728674a7SGreg Kroah-Hartman 					hvcsd->chars_in_buffer);
1329728674a7SGreg Kroah-Hartman 
1330728674a7SGreg Kroah-Hartman 		/*
1331728674a7SGreg Kroah-Hartman 		 * Since we know we have enough room in hvcsd->buffer for
1332728674a7SGreg Kroah-Hartman 		 * tosend we record that it was sent regardless of whether the
1333728674a7SGreg Kroah-Hartman 		 * hypervisor actually took it because we have it buffered.
1334728674a7SGreg Kroah-Hartman 		 */
1335728674a7SGreg Kroah-Hartman 		total_sent+=tosend;
1336728674a7SGreg Kroah-Hartman 		count-=tosend;
1337728674a7SGreg Kroah-Hartman 		if (result == 0) {
1338728674a7SGreg Kroah-Hartman 			hvcsd->todo_mask |= HVCS_TRY_WRITE;
1339728674a7SGreg Kroah-Hartman 			hvcs_kick();
1340728674a7SGreg Kroah-Hartman 			break;
1341728674a7SGreg Kroah-Hartman 		}
1342728674a7SGreg Kroah-Hartman 
1343728674a7SGreg Kroah-Hartman 		hvcsd->chars_in_buffer = 0;
1344728674a7SGreg Kroah-Hartman 		/*
1345728674a7SGreg Kroah-Hartman 		 * Test after the chars_in_buffer reset otherwise this could
1346728674a7SGreg Kroah-Hartman 		 * deadlock our writes if hvc_put_chars fails.
1347728674a7SGreg Kroah-Hartman 		 */
1348728674a7SGreg Kroah-Hartman 		if (result < 0)
1349728674a7SGreg Kroah-Hartman 			break;
1350728674a7SGreg Kroah-Hartman 	}
1351728674a7SGreg Kroah-Hartman 
1352728674a7SGreg Kroah-Hartman 	spin_unlock_irqrestore(&hvcsd->lock, flags);
1353728674a7SGreg Kroah-Hartman 
1354728674a7SGreg Kroah-Hartman 	if (result == -1)
1355728674a7SGreg Kroah-Hartman 		return -EIO;
1356728674a7SGreg Kroah-Hartman 	else
1357728674a7SGreg Kroah-Hartman 		return total_sent;
1358728674a7SGreg Kroah-Hartman }
1359728674a7SGreg Kroah-Hartman 
1360728674a7SGreg Kroah-Hartman /*
1361728674a7SGreg Kroah-Hartman  * This is really asking how much can we guarantee that we can send or that we
1362728674a7SGreg Kroah-Hartman  * absolutely WILL BUFFER if we can't send it.  This driver MUST honor the
1363728674a7SGreg Kroah-Hartman  * return value, hence the reason for hvcs_struct buffering.
1364728674a7SGreg Kroah-Hartman  */
hvcs_write_room(struct tty_struct * tty)1365728674a7SGreg Kroah-Hartman static unsigned int hvcs_write_room(struct tty_struct *tty)
1366728674a7SGreg Kroah-Hartman {
1367728674a7SGreg Kroah-Hartman 	struct hvcs_struct *hvcsd = tty->driver_data;
1368728674a7SGreg Kroah-Hartman 
1369728674a7SGreg Kroah-Hartman 	if (!hvcsd || hvcsd->port.count <= 0)
1370728674a7SGreg Kroah-Hartman 		return 0;
1371728674a7SGreg Kroah-Hartman 
1372728674a7SGreg Kroah-Hartman 	return HVCS_BUFF_LEN - hvcsd->chars_in_buffer;
1373728674a7SGreg Kroah-Hartman }
1374728674a7SGreg Kroah-Hartman 
hvcs_chars_in_buffer(struct tty_struct * tty)137525985edcSLucas De Marchi static unsigned int hvcs_chars_in_buffer(struct tty_struct *tty)
1376728674a7SGreg Kroah-Hartman {
1377728674a7SGreg Kroah-Hartman 	struct hvcs_struct *hvcsd = tty->driver_data;
1378728674a7SGreg Kroah-Hartman 
137903b3b1a2SJiri Slaby 	return hvcsd->chars_in_buffer;
1380728674a7SGreg Kroah-Hartman }
1381728674a7SGreg Kroah-Hartman 
1382728674a7SGreg Kroah-Hartman static const struct tty_operations hvcs_ops = {
13831997cf04SJiri Slaby 	.install = hvcs_install,
1384728674a7SGreg Kroah-Hartman 	.open = hvcs_open,
1385728674a7SGreg Kroah-Hartman 	.close = hvcs_close,
1386728674a7SGreg Kroah-Hartman 	.cleanup = hvcs_cleanup,
1387728674a7SGreg Kroah-Hartman 	.hangup = hvcs_hangup,
1388728674a7SGreg Kroah-Hartman 	.write = hvcs_write,
1389fff4ef17SJiri Slaby 	.write_room = hvcs_write_room,
1390728674a7SGreg Kroah-Hartman 	.chars_in_buffer = hvcs_chars_in_buffer,
1391728674a7SGreg Kroah-Hartman 	.unthrottle = hvcs_unthrottle,
1392728674a7SGreg Kroah-Hartman 	.throttle = hvcs_throttle,
1393728674a7SGreg Kroah-Hartman };
1394728674a7SGreg Kroah-Hartman 
hvcs_alloc_index_list(int n)1395728674a7SGreg Kroah-Hartman static int hvcs_alloc_index_list(int n)
1396728674a7SGreg Kroah-Hartman {
139727bf7c43SJiri Slaby 	int i;
1398728674a7SGreg Kroah-Hartman 
1399728674a7SGreg Kroah-Hartman 	hvcs_index_list = kmalloc_array(n, sizeof(hvcs_index_count),
140027bf7c43SJiri Slaby 					GFP_KERNEL);
1401728674a7SGreg Kroah-Hartman 	if (!hvcs_index_list)
1402728674a7SGreg Kroah-Hartman 		return -ENOMEM;
1403728674a7SGreg Kroah-Hartman 	hvcs_index_count = n;
1404728674a7SGreg Kroah-Hartman 	for (i = 0; i < hvcs_index_count; i++)
1405728674a7SGreg Kroah-Hartman 		hvcs_index_list[i] = -1;
1406728674a7SGreg Kroah-Hartman 	return 0;
1407728674a7SGreg Kroah-Hartman }
1408728674a7SGreg Kroah-Hartman 
hvcs_free_index_list(void)1409728674a7SGreg Kroah-Hartman static void hvcs_free_index_list(void)
1410728674a7SGreg Kroah-Hartman {
1411728674a7SGreg Kroah-Hartman 	/* Paranoia check to be thorough. */
1412728674a7SGreg Kroah-Hartman 	kfree(hvcs_index_list);
14136da2ec56SKees Cook 	hvcs_index_list = NULL;
14146da2ec56SKees Cook 	hvcs_index_count = 0;
1415728674a7SGreg Kroah-Hartman }
1416728674a7SGreg Kroah-Hartman 
hvcs_initialize(void)1417728674a7SGreg Kroah-Hartman static int hvcs_initialize(void)
1418728674a7SGreg Kroah-Hartman {
1419728674a7SGreg Kroah-Hartman 	int rc, num_ttys_to_alloc;
1420728674a7SGreg Kroah-Hartman 
1421728674a7SGreg Kroah-Hartman 	mutex_lock(&hvcs_init_mutex);
1422728674a7SGreg Kroah-Hartman 	if (hvcs_task) {
1423728674a7SGreg Kroah-Hartman 		mutex_unlock(&hvcs_init_mutex);
1424728674a7SGreg Kroah-Hartman 		return 0;
1425728674a7SGreg Kroah-Hartman 	}
1426728674a7SGreg Kroah-Hartman 
1427728674a7SGreg Kroah-Hartman 	/* Has the user specified an overload with an insmod param? */
1428728674a7SGreg Kroah-Hartman 	if (hvcs_parm_num_devs <= 0 ||
1429728674a7SGreg Kroah-Hartman 		(hvcs_parm_num_devs > HVCS_MAX_SERVER_ADAPTERS)) {
1430728674a7SGreg Kroah-Hartman 		num_ttys_to_alloc = HVCS_DEFAULT_SERVER_ADAPTERS;
14319671f099SBill Pemberton 	} else
1432728674a7SGreg Kroah-Hartman 		num_ttys_to_alloc = hvcs_parm_num_devs;
1433c7704d35SBenjamin Herrenschmidt 
1434728674a7SGreg Kroah-Hartman 	hvcs_tty_driver = tty_alloc_driver(num_ttys_to_alloc,
1435c7704d35SBenjamin Herrenschmidt 			TTY_DRIVER_REAL_RAW);
1436c7704d35SBenjamin Herrenschmidt 	if (IS_ERR(hvcs_tty_driver)) {
1437c7704d35SBenjamin Herrenschmidt 		mutex_unlock(&hvcs_init_mutex);
1438c7704d35SBenjamin Herrenschmidt 		return PTR_ERR(hvcs_tty_driver);
1439c7704d35SBenjamin Herrenschmidt 	}
1440728674a7SGreg Kroah-Hartman 
1441728674a7SGreg Kroah-Hartman 	if (hvcs_alloc_index_list(num_ttys_to_alloc)) {
1442728674a7SGreg Kroah-Hartman 		rc = -ENOMEM;
1443728674a7SGreg Kroah-Hartman 		goto index_fail;
1444728674a7SGreg Kroah-Hartman 	}
1445728674a7SGreg Kroah-Hartman 
1446728674a7SGreg Kroah-Hartman 	hvcs_tty_driver->driver_name = hvcs_driver_name;
1447728674a7SGreg Kroah-Hartman 	hvcs_tty_driver->name = hvcs_device_node;
144839b7b42bSJiri Slaby 
144939b7b42bSJiri Slaby 	/*
145039b7b42bSJiri Slaby 	 * We'll let the system assign us a major number, indicated by leaving
1451ad3d1e5fSWei Yongjun 	 * it blank.
145239b7b42bSJiri Slaby 	 */
1453ad3d1e5fSWei Yongjun 
1454728674a7SGreg Kroah-Hartman 	hvcs_tty_driver->minor_start = HVCS_MINOR_START;
1455728674a7SGreg Kroah-Hartman 	hvcs_tty_driver->type = TTY_DRIVER_TYPE_SYSTEM;
1456728674a7SGreg Kroah-Hartman 
1457728674a7SGreg Kroah-Hartman 	/*
1458728674a7SGreg Kroah-Hartman 	 * We role our own so that we DONT ECHO.  We can't echo because the
1459728674a7SGreg Kroah-Hartman 	 * device we are connecting to already echoes by default and this would
1460728674a7SGreg Kroah-Hartman 	 * throw us into a horrible recursive echo-echo-echo loop.
1461728674a7SGreg Kroah-Hartman 	 */
1462728674a7SGreg Kroah-Hartman 	hvcs_tty_driver->init_termios = hvcs_tty_termios;
1463728674a7SGreg Kroah-Hartman 
1464728674a7SGreg Kroah-Hartman 	tty_set_operations(hvcs_tty_driver, &hvcs_ops);
1465728674a7SGreg Kroah-Hartman 
1466728674a7SGreg Kroah-Hartman 	/*
1467728674a7SGreg Kroah-Hartman 	 * The following call will result in sysfs entries that denote the
1468728674a7SGreg Kroah-Hartman 	 * dynamically assigned major and minor numbers for our devices.
1469728674a7SGreg Kroah-Hartman 	 */
1470728674a7SGreg Kroah-Hartman 	if (tty_register_driver(hvcs_tty_driver)) {
1471728674a7SGreg Kroah-Hartman 		printk(KERN_ERR "HVCS: registration as a tty driver failed.\n");
1472728674a7SGreg Kroah-Hartman 		rc = -EIO;
1473728674a7SGreg Kroah-Hartman 		goto register_fail;
1474728674a7SGreg Kroah-Hartman 	}
1475728674a7SGreg Kroah-Hartman 
1476728674a7SGreg Kroah-Hartman 	hvcs_pi_buff = (unsigned long *) __get_free_page(GFP_KERNEL);
1477728674a7SGreg Kroah-Hartman 	if (!hvcs_pi_buff) {
1478728674a7SGreg Kroah-Hartman 		rc = -ENOMEM;
1479728674a7SGreg Kroah-Hartman 		goto buff_alloc_fail;
1480728674a7SGreg Kroah-Hartman 	}
1481728674a7SGreg Kroah-Hartman 
1482728674a7SGreg Kroah-Hartman 	hvcs_task = kthread_run(khvcsd, NULL, "khvcsd");
1483728674a7SGreg Kroah-Hartman 	if (IS_ERR(hvcs_task)) {
1484728674a7SGreg Kroah-Hartman 		printk(KERN_ERR "HVCS: khvcsd creation failed.\n");
1485728674a7SGreg Kroah-Hartman 		rc = -EIO;
1486728674a7SGreg Kroah-Hartman 		goto kthread_fail;
1487728674a7SGreg Kroah-Hartman 	}
1488728674a7SGreg Kroah-Hartman 	mutex_unlock(&hvcs_init_mutex);
1489728674a7SGreg Kroah-Hartman 	return 0;
1490ac07a4a5SBrian King 
1491728674a7SGreg Kroah-Hartman kthread_fail:
1492728674a7SGreg Kroah-Hartman 	free_page((unsigned long)hvcs_pi_buff);
1493728674a7SGreg Kroah-Hartman buff_alloc_fail:
1494728674a7SGreg Kroah-Hartman 	tty_unregister_driver(hvcs_tty_driver);
1495728674a7SGreg Kroah-Hartman register_fail:
1496728674a7SGreg Kroah-Hartman 	hvcs_free_index_list();
1497728674a7SGreg Kroah-Hartman index_fail:
1498c7704d35SBenjamin Herrenschmidt 	tty_driver_kref_put(hvcs_tty_driver);
1499728674a7SGreg Kroah-Hartman 	hvcs_tty_driver = NULL;
1500728674a7SGreg Kroah-Hartman 	mutex_unlock(&hvcs_init_mutex);
1501728674a7SGreg Kroah-Hartman 	return rc;
1502c7704d35SBenjamin Herrenschmidt }
1503728674a7SGreg Kroah-Hartman 
hvcs_module_init(void)1504728674a7SGreg Kroah-Hartman static int __init hvcs_module_init(void)
1505728674a7SGreg Kroah-Hartman {
1506ac07a4a5SBrian King 	int rc = vio_register_driver(&hvcs_vio_driver);
1507728674a7SGreg Kroah-Hartman 	if (rc) {
1508728674a7SGreg Kroah-Hartman 		printk(KERN_ERR "HVCS: can't register vio driver\n");
1509728674a7SGreg Kroah-Hartman 		return rc;
1510728674a7SGreg Kroah-Hartman 	}
1511728674a7SGreg Kroah-Hartman 
15129f90a4ddSJiri Slaby 	pr_info("HVCS: Driver registered.\n");
1513728674a7SGreg Kroah-Hartman 
1514c7704d35SBenjamin Herrenschmidt 	return 0;
1515728674a7SGreg Kroah-Hartman }
1516728674a7SGreg Kroah-Hartman 
hvcs_module_exit(void)1517728674a7SGreg Kroah-Hartman static void __exit hvcs_module_exit(void)
1518c7704d35SBenjamin Herrenschmidt {
1519c7704d35SBenjamin Herrenschmidt 	/*
1520c7704d35SBenjamin Herrenschmidt 	 * This driver receives hvcs_remove callbacks for each device upon
1521c7704d35SBenjamin Herrenschmidt 	 * module removal.
1522c7704d35SBenjamin Herrenschmidt 	 */
1523c7704d35SBenjamin Herrenschmidt 	vio_unregister_driver(&hvcs_vio_driver);
1524c7704d35SBenjamin Herrenschmidt 	if (!hvcs_task)
1525c7704d35SBenjamin Herrenschmidt 		return;
1526c7704d35SBenjamin Herrenschmidt 
1527c7704d35SBenjamin Herrenschmidt 	/*
1528c7704d35SBenjamin Herrenschmidt 	 * This synchronous operation  will wake the khvcsd kthread if it is
1529c7704d35SBenjamin Herrenschmidt 	 * asleep and will return when khvcsd has terminated.
1530c7704d35SBenjamin Herrenschmidt 	 */
1531c7704d35SBenjamin Herrenschmidt 	kthread_stop(hvcs_task);
1532c7704d35SBenjamin Herrenschmidt 
1533d437fa91SJoe Perches 	spin_lock(&hvcs_pi_lock);
1534c7704d35SBenjamin Herrenschmidt 	free_page((unsigned long)hvcs_pi_buff);
1535c7704d35SBenjamin Herrenschmidt 	hvcs_pi_buff = NULL;
1536c7704d35SBenjamin Herrenschmidt 	spin_unlock(&hvcs_pi_lock);
1537c7704d35SBenjamin Herrenschmidt 
1538728674a7SGreg Kroah-Hartman 	tty_unregister_driver(hvcs_tty_driver);
1539728674a7SGreg Kroah-Hartman 
1540728674a7SGreg Kroah-Hartman 	hvcs_free_index_list();
1541728674a7SGreg Kroah-Hartman 
1542728674a7SGreg Kroah-Hartman 	tty_driver_kref_put(hvcs_tty_driver);
1543728674a7SGreg Kroah-Hartman 
1544c7704d35SBenjamin Herrenschmidt 	printk(KERN_INFO "HVCS: driver module removed.\n");
1545c7704d35SBenjamin Herrenschmidt }
1546c7704d35SBenjamin Herrenschmidt 
1547728674a7SGreg Kroah-Hartman module_init(hvcs_module_init);
1548728674a7SGreg Kroah-Hartman module_exit(hvcs_module_exit);
1549728674a7SGreg Kroah-Hartman