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