blob: fc5a12e5627625e4473f2190cd9aba47814d9472 [file] [log] [blame]
Greg Kroah-Hartmane3b3d0f2017-11-06 18:11:51 +01001// SPDX-License-Identifier: GPL-2.0+
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/*
3 * IBM eServer Hypervisor Virtual Console Server Device Driver
4 * Copyright (C) 2003, 2004 IBM Corp.
5 * Ryan S. Arnold (rsa@us.ibm.com)
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 *
21 * Author(s) : Ryan S. Arnold <rsa@us.ibm.com>
22 *
23 * This is the device driver for the IBM Hypervisor Virtual Console Server,
24 * "hvcs". The IBM hvcs provides a tty driver interface to allow Linux
25 * user space applications access to the system consoles of logically
26 * partitioned operating systems, e.g. Linux, running on the same partitioned
27 * Power5 ppc64 system. Physical hardware consoles per partition are not
28 * practical on this hardware so system consoles are accessed by this driver
29 * using inter-partition firmware interfaces to virtual terminal devices.
30 *
31 * A vty is known to the HMC as a "virtual serial server adapter". It is a
32 * virtual terminal device that is created by firmware upon partition creation
33 * to act as a partitioned OS's console device.
34 *
35 * Firmware dynamically (via hotplug) exposes vty-servers to a running ppc64
36 * Linux system upon their creation by the HMC or their exposure during boot.
37 * The non-user interactive backend of this driver is implemented as a vio
38 * device driver so that it can receive notification of vty-server lifetimes
39 * after it registers with the vio bus to handle vty-server probe and remove
40 * callbacks.
41 *
42 * Many vty-servers can be configured to connect to one vty, but a vty can
43 * only be actively connected to by a single vty-server, in any manner, at one
44 * time. If the HMC is currently hosting the console for a target Linux
45 * partition; attempts to open the tty device to the partition's console using
46 * the hvcs on any partition will return -EBUSY with every open attempt until
47 * the HMC frees the connection between its vty-server and the desired
48 * partition's vty device. Conversely, a vty-server may only be connected to
49 * a single vty at one time even though it may have several configured vty
50 * partner possibilities.
51 *
52 * Firmware does not provide notification of vty partner changes to this
53 * driver. This means that an HMC Super Admin may add or remove partner vtys
54 * from a vty-server's partner list but the changes will not be signaled to
55 * the vty-server. Firmware only notifies the driver when a vty-server is
56 * added or removed from the system. To compensate for this deficiency, this
57 * driver implements a sysfs update attribute which provides a method for
58 * rescanning partner information upon a user's request.
59 *
60 * Each vty-server, prior to being exposed to this driver is reference counted
Greg Kroah-Hartmanf7eb12c2007-11-28 10:46:22 -080061 * using the 2.6 Linux kernel kref construct.
Linus Torvalds1da177e2005-04-16 15:20:36 -070062 *
63 * For direction on installation and usage of this driver please reference
64 * Documentation/powerpc/hvcs.txt.
65 */
66
67#include <linux/device.h>
68#include <linux/init.h>
69#include <linux/interrupt.h>
70#include <linux/kernel.h>
Greg Kroah-Hartmanf7eb12c2007-11-28 10:46:22 -080071#include <linux/kref.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070072#include <linux/kthread.h>
73#include <linux/list.h>
74#include <linux/major.h>
75#include <linux/module.h>
76#include <linux/moduleparam.h>
77#include <linux/sched.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090078#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070079#include <linux/spinlock.h>
80#include <linux/stat.h>
81#include <linux/tty.h>
82#include <linux/tty_flip.h>
83#include <asm/hvconsole.h>
84#include <asm/hvcserver.h>
Linus Torvalds7c0f6ba2016-12-24 11:46:01 -080085#include <linux/uaccess.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070086#include <asm/vio.h>
87
88/*
89 * 1.3.0 -> 1.3.1 In hvcs_open memset(..,0x00,..) instead of memset(..,0x3F,00).
90 * Removed braces around single statements following conditionals. Removed '=
91 * 0' after static int declarations since these default to zero. Removed
92 * list_for_each_safe() and replaced with list_for_each_entry() in
93 * hvcs_get_by_index(). The 'safe' version is un-needed now that the driver is
94 * using spinlocks. Changed spin_lock_irqsave() to spin_lock() when locking
95 * hvcs_structs_lock and hvcs_pi_lock since these are not touched in an int
96 * handler. Initialized hvcs_structs_lock and hvcs_pi_lock to
97 * SPIN_LOCK_UNLOCKED at declaration time rather than in hvcs_module_init().
98 * Added spin_lock around list_del() in destroy_hvcs_struct() to protect the
99 * list traversals from a deletion. Removed '= NULL' from pointer declaration
100 * statements since they are initialized NULL by default. Removed wmb()
101 * instances from hvcs_try_write(). They probably aren't needed with locking in
102 * place. Added check and cleanup for hvcs_pi_buff = kmalloc() in
103 * hvcs_module_init(). Exposed hvcs_struct.index via a sysfs attribute so that
104 * the coupling between /dev/hvcs* and a vty-server can be automatically
105 * determined. Moved kobject_put() in hvcs_open outside of the
106 * spin_unlock_irqrestore().
107 *
108 * 1.3.1 -> 1.3.2 Changed method for determining hvcs_struct->index and had it
109 * align with how the tty layer always assigns the lowest index available. This
110 * change resulted in a list of ints that denotes which indexes are available.
111 * Device additions and removals use the new hvcs_get_index() and
112 * hvcs_return_index() helper functions. The list is created with
113 * hvsc_alloc_index_list() and it is destroyed with hvcs_free_index_list().
114 * Without these fixes hotplug vty-server adapter support goes crazy with this
115 * driver if the user removes a vty-server adapter. Moved free_irq() outside of
116 * the hvcs_final_close() function in order to get it out of the spinlock.
117 * Rearranged hvcs_close(). Cleaned up some printks and did some housekeeping
118 * on the changelog. Removed local CLC_LENGTH and used HVCS_CLC_LENGTH from
Stephen Rothwellb8b572e2008-08-01 15:20:30 +1000119 * arch/powerepc/include/asm/hvcserver.h
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120 *
121 * 1.3.2 -> 1.3.3 Replaced yield() in hvcs_close() with tty_wait_until_sent() to
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300122 * prevent possible lockup with realtime scheduling as similarly pointed out by
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123 * akpm in hvc_console. Changed resulted in the removal of hvcs_final_close()
124 * to reorder cleanup operations and prevent discarding of pending data during
125 * an hvcs_close(). Removed spinlock protection of hvcs_struct data members in
126 * hvcs_write_room() and hvcs_chars_in_buffer() because they aren't needed.
127 */
128
129#define HVCS_DRIVER_VERSION "1.3.3"
130
131MODULE_AUTHOR("Ryan S. Arnold <rsa@us.ibm.com>");
132MODULE_DESCRIPTION("IBM hvcs (Hypervisor Virtual Console Server) Driver");
133MODULE_LICENSE("GPL");
134MODULE_VERSION(HVCS_DRIVER_VERSION);
135
136/*
137 * Wait this long per iteration while trying to push buffered data to the
138 * hypervisor before allowing the tty to complete a close operation.
139 */
140#define HVCS_CLOSE_WAIT (HZ/100) /* 1/10 of a second */
141
142/*
143 * Since the Linux TTY code does not currently (2-04-2004) support dynamic
144 * addition of tty derived devices and we shouldn't allocate thousands of
145 * tty_device pointers when the number of vty-server & vty partner connections
146 * will most often be much lower than this, we'll arbitrarily allocate
147 * HVCS_DEFAULT_SERVER_ADAPTERS tty_structs and cdev's by default when we
148 * register the tty_driver. This can be overridden using an insmod parameter.
149 */
150#define HVCS_DEFAULT_SERVER_ADAPTERS 64
151
152/*
153 * The user can't insmod with more than HVCS_MAX_SERVER_ADAPTERS hvcs device
154 * nodes as a sanity check. Theoretically there can be over 1 Billion
155 * vty-server & vty partner connections.
156 */
157#define HVCS_MAX_SERVER_ADAPTERS 1024
158
159/*
160 * We let Linux assign us a major number and we start the minors at zero. There
161 * is no intuitive mapping between minor number and the target vty-server
162 * adapter except that each new vty-server adapter is always assigned to the
163 * smallest minor number available.
164 */
165#define HVCS_MINOR_START 0
166
167/*
168 * The hcall interface involves putting 8 chars into each of two registers.
Jon Mason2ef94812006-01-23 10:58:20 -0600169 * We load up those 2 registers (in arch/powerpc/platforms/pseries/hvconsole.c)
170 * by casting char[16] to long[2]. It would work without __ALIGNED__, but a
171 * little (tiny) bit slower because an unaligned load is slower than aligned
172 * load.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173 */
174#define __ALIGNED__ __attribute__((__aligned__(8)))
175
176/*
177 * How much data can firmware send with each hvc_put_chars()? Maybe this
178 * should be moved into an architecture specific area.
179 */
180#define HVCS_BUFF_LEN 16
181
182/*
183 * This is the maximum amount of data we'll let the user send us (hvcs_write) at
184 * once in a chunk as a sanity check.
185 */
186#define HVCS_MAX_FROM_USER 4096
187
188/*
189 * Be careful when adding flags to this line discipline. Don't add anything
190 * that will cause echoing or we'll go into recursive loop echoing chars back
191 * and forth with the console drivers.
192 */
Bhumika Goyal6ec6e802017-08-29 00:00:31 +0530193static const struct ktermios hvcs_tty_termios = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194 .c_iflag = IGNBRK | IGNPAR,
195 .c_oflag = OPOST,
196 .c_cflag = B38400 | CS8 | CREAD | HUPCL,
Alan Cox606d0992006-12-08 02:38:45 -0800197 .c_cc = INIT_C_CC,
198 .c_ispeed = 38400,
199 .c_ospeed = 38400
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200};
201
202/*
203 * This value is used to take the place of a command line parameter when the
204 * module is inserted. It starts as -1 and stays as such if the user doesn't
205 * specify a module insmod parameter. If they DO specify one then it is set to
206 * the value of the integer passed in.
207 */
208static int hvcs_parm_num_devs = -1;
209module_param(hvcs_parm_num_devs, int, 0);
210
Stephen Rothwellb1c30112007-07-22 00:25:00 +1000211static const char hvcs_driver_name[] = "hvcs";
212static const char hvcs_device_node[] = "hvcs";
213static const char hvcs_driver_string[]
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214 = "IBM hvcs (Hypervisor Virtual Console Server) Driver";
215
216/* Status of partner info rescan triggered via sysfs. */
217static int hvcs_rescan_status;
218
219static struct tty_driver *hvcs_tty_driver;
220
221/*
222 * In order to be somewhat sane this driver always associates the hvcs_struct
223 * index element with the numerically equal tty->index. This means that a
224 * hotplugged vty-server adapter will always map to the lowest index valued
225 * device node. If vty-servers were hotplug removed from the system and then
226 * new ones added the new vty-server may have the largest slot number of all
227 * the vty-server adapters in the partition but it may have the lowest dev node
228 * index of all the adapters due to the hole left by the hotplug removed
229 * adapter. There are a set of functions provided to get the lowest index for
230 * a new device as well as return the index to the list. This list is allocated
231 * with a number of elements equal to the number of device nodes requested when
232 * the module was inserted.
233 */
234static int *hvcs_index_list;
235
236/*
237 * How large is the list? This is kept for traversal since the list is
238 * dynamically created.
239 */
240static int hvcs_index_count;
241
242/*
243 * Used by the khvcsd to pick up I/O operations when the kernel_thread is
244 * already awake but potentially shifted to TASK_INTERRUPTIBLE state.
245 */
246static int hvcs_kicked;
247
248/*
249 * Use by the kthread construct for task operations like waking the sleeping
250 * thread and stopping the kthread.
251 */
252static struct task_struct *hvcs_task;
253
254/*
255 * We allocate this for the use of all of the hvcs_structs when they fetch
256 * partner info.
257 */
258static unsigned long *hvcs_pi_buff;
259
260/* Only allow one hvcs_struct to use the hvcs_pi_buff at a time. */
261static DEFINE_SPINLOCK(hvcs_pi_lock);
262
263/* One vty-server per hvcs_struct */
264struct hvcs_struct {
Jiri Slaby1997cf02012-04-02 13:54:24 +0200265 struct tty_port port;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266 spinlock_t lock;
267
268 /*
269 * This index identifies this hvcs device as the complement to a
270 * specific tty index.
271 */
272 unsigned int index;
273
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274 /*
275 * Used to tell the driver kernel_thread what operations need to take
276 * place upon this hvcs_struct instance.
277 */
278 int todo_mask;
279
280 /*
281 * This buffer is required so that when hvcs_write_room() reports that
282 * it can send HVCS_BUFF_LEN characters that it will buffer the full
283 * HVCS_BUFF_LEN characters if need be. This is essential for opost
284 * writes since they do not do high level buffering and expect to be
285 * able to send what the driver commits to sending buffering
286 * [e.g. tab to space conversions in n_tty.c opost()].
287 */
288 char buffer[HVCS_BUFF_LEN];
289 int chars_in_buffer;
290
291 /*
Jiri Slaby2cd9fa22012-04-02 13:54:25 +0200292 * Any variable below is valid before a tty is connected and
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293 * stays valid after the tty is disconnected. These shouldn't be
Paul Bolle46a1ca72011-02-15 00:05:01 +0100294 * whacked until the kobject refcount reaches zero though some entries
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295 * may be changed via sysfs initiatives.
296 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297 int connected; /* is the vty-server currently connected to a vty? */
298 uint32_t p_unit_address; /* partner unit address */
299 uint32_t p_partition_ID; /* partner partition ID */
300 char p_location_code[HVCS_CLC_LENGTH + 1]; /* CLC + Null Term */
301 struct list_head next; /* list management */
302 struct vio_dev *vdev;
303};
304
Denis Chengbed97592008-02-06 01:37:35 -0800305static LIST_HEAD(hvcs_structs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306static DEFINE_SPINLOCK(hvcs_structs_lock);
Benjamin Herrenschmidtc7704d32011-02-06 18:26:25 +0000307static DEFINE_MUTEX(hvcs_init_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308
309static void hvcs_unthrottle(struct tty_struct *tty);
310static void hvcs_throttle(struct tty_struct *tty);
David Howells7d12e782006-10-05 14:55:46 +0100311static irqreturn_t hvcs_handle_interrupt(int irq, void *dev_instance);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312
313static int hvcs_write(struct tty_struct *tty,
314 const unsigned char *buf, int count);
315static int hvcs_write_room(struct tty_struct *tty);
316static int hvcs_chars_in_buffer(struct tty_struct *tty);
317
318static int hvcs_has_pi(struct hvcs_struct *hvcsd);
319static void hvcs_set_pi(struct hvcs_partner_info *pi,
320 struct hvcs_struct *hvcsd);
321static int hvcs_get_pi(struct hvcs_struct *hvcsd);
322static int hvcs_rescan_devices_list(void);
323
324static int hvcs_partner_connect(struct hvcs_struct *hvcsd);
325static void hvcs_partner_free(struct hvcs_struct *hvcsd);
326
327static int hvcs_enable_device(struct hvcs_struct *hvcsd,
328 uint32_t unit_address, unsigned int irq, struct vio_dev *dev);
329
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330static int hvcs_open(struct tty_struct *tty, struct file *filp);
331static void hvcs_close(struct tty_struct *tty, struct file *filp);
332static void hvcs_hangup(struct tty_struct * tty);
333
Bill Pemberton9671f092012-11-19 13:21:50 -0500334static int hvcs_probe(struct vio_dev *dev,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335 const struct vio_device_id *id);
Bill Pembertonae8d8a12012-11-19 13:26:18 -0500336static int hvcs_remove(struct vio_dev *dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337static int __init hvcs_module_init(void);
338static void __exit hvcs_module_exit(void);
Bill Pemberton9671f092012-11-19 13:21:50 -0500339static int hvcs_initialize(void);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340
341#define HVCS_SCHED_READ 0x00000001
342#define HVCS_QUICK_READ 0x00000002
343#define HVCS_TRY_WRITE 0x00000004
344#define HVCS_READ_MASK (HVCS_SCHED_READ | HVCS_QUICK_READ)
345
Linas Vepstasc15bb292006-12-06 20:38:07 -0800346static inline struct hvcs_struct *from_vio_dev(struct vio_dev *viod)
347{
Greg Kroah-Hartman317ccc42009-05-04 12:40:54 -0700348 return dev_get_drvdata(&viod->dev);
Linas Vepstasc15bb292006-12-06 20:38:07 -0800349}
350/* The sysfs interface for the driver and devices */
351
352static ssize_t hvcs_partner_vtys_show(struct device *dev, struct device_attribute *attr, char *buf)
353{
354 struct vio_dev *viod = to_vio_dev(dev);
355 struct hvcs_struct *hvcsd = from_vio_dev(viod);
356 unsigned long flags;
357 int retval;
358
359 spin_lock_irqsave(&hvcsd->lock, flags);
360 retval = sprintf(buf, "%X\n", hvcsd->p_unit_address);
361 spin_unlock_irqrestore(&hvcsd->lock, flags);
362 return retval;
363}
364static DEVICE_ATTR(partner_vtys, S_IRUGO, hvcs_partner_vtys_show, NULL);
365
366static ssize_t hvcs_partner_clcs_show(struct device *dev, struct device_attribute *attr, char *buf)
367{
368 struct vio_dev *viod = to_vio_dev(dev);
369 struct hvcs_struct *hvcsd = from_vio_dev(viod);
370 unsigned long flags;
371 int retval;
372
373 spin_lock_irqsave(&hvcsd->lock, flags);
374 retval = sprintf(buf, "%s\n", &hvcsd->p_location_code[0]);
375 spin_unlock_irqrestore(&hvcsd->lock, flags);
376 return retval;
377}
378static DEVICE_ATTR(partner_clcs, S_IRUGO, hvcs_partner_clcs_show, NULL);
379
380static ssize_t hvcs_current_vty_store(struct device *dev, struct device_attribute *attr, const char * buf,
381 size_t count)
382{
383 /*
384 * Don't need this feature at the present time because firmware doesn't
385 * yet support multiple partners.
386 */
387 printk(KERN_INFO "HVCS: Denied current_vty change: -EPERM.\n");
388 return -EPERM;
389}
390
391static ssize_t hvcs_current_vty_show(struct device *dev, struct device_attribute *attr, char *buf)
392{
393 struct vio_dev *viod = to_vio_dev(dev);
394 struct hvcs_struct *hvcsd = from_vio_dev(viod);
395 unsigned long flags;
396 int retval;
397
398 spin_lock_irqsave(&hvcsd->lock, flags);
399 retval = sprintf(buf, "%s\n", &hvcsd->p_location_code[0]);
400 spin_unlock_irqrestore(&hvcsd->lock, flags);
401 return retval;
402}
403
404static DEVICE_ATTR(current_vty,
405 S_IRUGO | S_IWUSR, hvcs_current_vty_show, hvcs_current_vty_store);
406
407static ssize_t hvcs_vterm_state_store(struct device *dev, struct device_attribute *attr, const char *buf,
408 size_t count)
409{
410 struct vio_dev *viod = to_vio_dev(dev);
411 struct hvcs_struct *hvcsd = from_vio_dev(viod);
412 unsigned long flags;
413
414 /* writing a '0' to this sysfs entry will result in the disconnect. */
415 if (simple_strtol(buf, NULL, 0) != 0)
416 return -EINVAL;
417
418 spin_lock_irqsave(&hvcsd->lock, flags);
419
Jiri Slaby1997cf02012-04-02 13:54:24 +0200420 if (hvcsd->port.count > 0) {
Linas Vepstasc15bb292006-12-06 20:38:07 -0800421 spin_unlock_irqrestore(&hvcsd->lock, flags);
422 printk(KERN_INFO "HVCS: vterm state unchanged. "
423 "The hvcs device node is still in use.\n");
424 return -EPERM;
425 }
426
427 if (hvcsd->connected == 0) {
428 spin_unlock_irqrestore(&hvcsd->lock, flags);
429 printk(KERN_INFO "HVCS: vterm state unchanged. The"
430 " vty-server is not connected to a vty.\n");
431 return -EPERM;
432 }
433
434 hvcs_partner_free(hvcsd);
435 printk(KERN_INFO "HVCS: Closed vty-server@%X and"
436 " partner vty@%X:%d connection.\n",
437 hvcsd->vdev->unit_address,
438 hvcsd->p_unit_address,
439 (uint32_t)hvcsd->p_partition_ID);
440
441 spin_unlock_irqrestore(&hvcsd->lock, flags);
442 return count;
443}
444
445static ssize_t hvcs_vterm_state_show(struct device *dev, struct device_attribute *attr, char *buf)
446{
447 struct vio_dev *viod = to_vio_dev(dev);
448 struct hvcs_struct *hvcsd = from_vio_dev(viod);
449 unsigned long flags;
450 int retval;
451
452 spin_lock_irqsave(&hvcsd->lock, flags);
453 retval = sprintf(buf, "%d\n", hvcsd->connected);
454 spin_unlock_irqrestore(&hvcsd->lock, flags);
455 return retval;
456}
457static DEVICE_ATTR(vterm_state, S_IRUGO | S_IWUSR,
458 hvcs_vterm_state_show, hvcs_vterm_state_store);
459
460static ssize_t hvcs_index_show(struct device *dev, struct device_attribute *attr, char *buf)
461{
462 struct vio_dev *viod = to_vio_dev(dev);
463 struct hvcs_struct *hvcsd = from_vio_dev(viod);
464 unsigned long flags;
465 int retval;
466
467 spin_lock_irqsave(&hvcsd->lock, flags);
468 retval = sprintf(buf, "%d\n", hvcsd->index);
469 spin_unlock_irqrestore(&hvcsd->lock, flags);
470 return retval;
471}
472
473static DEVICE_ATTR(index, S_IRUGO, hvcs_index_show, NULL);
474
475static struct attribute *hvcs_attrs[] = {
476 &dev_attr_partner_vtys.attr,
477 &dev_attr_partner_clcs.attr,
478 &dev_attr_current_vty.attr,
479 &dev_attr_vterm_state.attr,
480 &dev_attr_index.attr,
481 NULL,
482};
483
484static struct attribute_group hvcs_attr_group = {
485 .attrs = hvcs_attrs,
486};
487
Greg Kroah-Hartman4e70a6f2017-06-09 11:03:07 +0200488static ssize_t rescan_show(struct device_driver *ddp, char *buf)
Linas Vepstasc15bb292006-12-06 20:38:07 -0800489{
490 /* A 1 means it is updating, a 0 means it is done updating */
491 return snprintf(buf, PAGE_SIZE, "%d\n", hvcs_rescan_status);
492}
493
Greg Kroah-Hartman4e70a6f2017-06-09 11:03:07 +0200494static ssize_t rescan_store(struct device_driver *ddp, const char * buf,
Linas Vepstasc15bb292006-12-06 20:38:07 -0800495 size_t count)
496{
497 if ((simple_strtol(buf, NULL, 0) != 1)
498 && (hvcs_rescan_status != 0))
499 return -EINVAL;
500
501 hvcs_rescan_status = 1;
502 printk(KERN_INFO "HVCS: rescanning partner info for all"
503 " vty-servers.\n");
504 hvcs_rescan_devices_list();
505 hvcs_rescan_status = 0;
506 return count;
507}
508
Greg Kroah-Hartman4e70a6f2017-06-09 11:03:07 +0200509static DRIVER_ATTR_RW(rescan);
Linas Vepstasc15bb292006-12-06 20:38:07 -0800510
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511static void hvcs_kick(void)
512{
513 hvcs_kicked = 1;
514 wmb();
515 wake_up_process(hvcs_task);
516}
517
518static void hvcs_unthrottle(struct tty_struct *tty)
519{
520 struct hvcs_struct *hvcsd = tty->driver_data;
521 unsigned long flags;
522
523 spin_lock_irqsave(&hvcsd->lock, flags);
524 hvcsd->todo_mask |= HVCS_SCHED_READ;
525 spin_unlock_irqrestore(&hvcsd->lock, flags);
526 hvcs_kick();
527}
528
529static void hvcs_throttle(struct tty_struct *tty)
530{
531 struct hvcs_struct *hvcsd = tty->driver_data;
532 unsigned long flags;
533
534 spin_lock_irqsave(&hvcsd->lock, flags);
535 vio_disable_interrupts(hvcsd->vdev);
536 spin_unlock_irqrestore(&hvcsd->lock, flags);
537}
538
539/*
540 * If the device is being removed we don't have to worry about this interrupt
541 * handler taking any further interrupts because they are disabled which means
542 * the hvcs_struct will always be valid in this handler.
543 */
David Howells7d12e782006-10-05 14:55:46 +0100544static irqreturn_t hvcs_handle_interrupt(int irq, void *dev_instance)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545{
546 struct hvcs_struct *hvcsd = dev_instance;
547
548 spin_lock(&hvcsd->lock);
549 vio_disable_interrupts(hvcsd->vdev);
550 hvcsd->todo_mask |= HVCS_SCHED_READ;
551 spin_unlock(&hvcsd->lock);
552 hvcs_kick();
553
554 return IRQ_HANDLED;
555}
556
557/* This function must be called with the hvcsd->lock held */
558static void hvcs_try_write(struct hvcs_struct *hvcsd)
559{
560 uint32_t unit_address = hvcsd->vdev->unit_address;
Jiri Slaby6968a752012-04-02 13:54:26 +0200561 struct tty_struct *tty = hvcsd->port.tty;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562 int sent;
563
564 if (hvcsd->todo_mask & HVCS_TRY_WRITE) {
565 /* won't send partial writes */
566 sent = hvc_put_chars(unit_address,
567 &hvcsd->buffer[0],
568 hvcsd->chars_in_buffer );
569 if (sent > 0) {
570 hvcsd->chars_in_buffer = 0;
571 /* wmb(); */
572 hvcsd->todo_mask &= ~(HVCS_TRY_WRITE);
573 /* wmb(); */
574
575 /*
576 * We are still obligated to deliver the data to the
577 * hypervisor even if the tty has been closed because
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300578 * we committed to delivering it. But don't try to wake
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579 * a non-existent tty.
580 */
581 if (tty) {
582 tty_wakeup(tty);
583 }
584 }
585 }
586}
587
588static int hvcs_io(struct hvcs_struct *hvcsd)
589{
590 uint32_t unit_address;
591 struct tty_struct *tty;
592 char buf[HVCS_BUFF_LEN] __ALIGNED__;
593 unsigned long flags;
594 int got = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595
596 spin_lock_irqsave(&hvcsd->lock, flags);
597
598 unit_address = hvcsd->vdev->unit_address;
Jiri Slaby6968a752012-04-02 13:54:26 +0200599 tty = hvcsd->port.tty;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600
601 hvcs_try_write(hvcsd);
602
Peter Hurley97ef38b2016-04-09 17:11:36 -0700603 if (!tty || tty_throttled(tty)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604 hvcsd->todo_mask &= ~(HVCS_READ_MASK);
605 goto bail;
606 } else if (!(hvcsd->todo_mask & (HVCS_READ_MASK)))
607 goto bail;
608
609 /* remove the read masks */
610 hvcsd->todo_mask &= ~(HVCS_READ_MASK);
611
Jiri Slaby227434f2013-01-03 15:53:01 +0100612 if (tty_buffer_request_room(&hvcsd->port, HVCS_BUFF_LEN) >= HVCS_BUFF_LEN) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613 got = hvc_get_chars(unit_address,
614 &buf[0],
615 HVCS_BUFF_LEN);
Jiri Slaby05c7cd32013-01-03 15:53:04 +0100616 tty_insert_flip_string(&hvcsd->port, buf, got);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617 }
618
619 /* Give the TTY time to process the data we just sent. */
620 if (got)
621 hvcsd->todo_mask |= HVCS_QUICK_READ;
622
623 spin_unlock_irqrestore(&hvcsd->lock, flags);
Alan Cox33f0f882006-01-09 20:54:13 -0800624 /* This is synch because tty->low_latency == 1 */
625 if(got)
Jiri Slaby2e124b42013-01-03 15:53:06 +0100626 tty_flip_buffer_push(&hvcsd->port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627
628 if (!got) {
629 /* Do this _after_ the flip_buffer_push */
630 spin_lock_irqsave(&hvcsd->lock, flags);
631 vio_enable_interrupts(hvcsd->vdev);
632 spin_unlock_irqrestore(&hvcsd->lock, flags);
633 }
634
635 return hvcsd->todo_mask;
636
637 bail:
638 spin_unlock_irqrestore(&hvcsd->lock, flags);
639 return hvcsd->todo_mask;
640}
641
642static int khvcsd(void *unused)
643{
644 struct hvcs_struct *hvcsd;
645 int hvcs_todo_mask;
646
647 __set_current_state(TASK_RUNNING);
648
649 do {
650 hvcs_todo_mask = 0;
651 hvcs_kicked = 0;
652 wmb();
653
654 spin_lock(&hvcs_structs_lock);
655 list_for_each_entry(hvcsd, &hvcs_structs, next) {
656 hvcs_todo_mask |= hvcs_io(hvcsd);
657 }
658 spin_unlock(&hvcs_structs_lock);
659
660 /*
661 * If any of the hvcs adapters want to try a write or quick read
662 * don't schedule(), yield a smidgen then execute the hvcs_io
663 * thread again for those that want the write.
664 */
665 if (hvcs_todo_mask & (HVCS_TRY_WRITE | HVCS_QUICK_READ)) {
666 yield();
667 continue;
668 }
669
670 set_current_state(TASK_INTERRUPTIBLE);
671 if (!hvcs_kicked)
672 schedule();
673 __set_current_state(TASK_RUNNING);
674 } while (!kthread_should_stop());
675
676 return 0;
677}
678
Arvind Yadava846c3e2017-08-17 19:04:23 +0530679static const struct vio_device_id hvcs_driver_table[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680 {"serial-server", "hvterm2"},
Stephen Rothwellfb120da2005-08-17 16:42:59 +1000681 { "", "" }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682};
683MODULE_DEVICE_TABLE(vio, hvcs_driver_table);
684
685static void hvcs_return_index(int index)
686{
687 /* Paranoia check */
688 if (!hvcs_index_list)
689 return;
690 if (index < 0 || index >= hvcs_index_count)
691 return;
692 if (hvcs_index_list[index] == -1)
693 return;
694 else
695 hvcs_index_list[index] = -1;
696}
697
Jiri Slaby2cd9fa22012-04-02 13:54:25 +0200698static void hvcs_destruct_port(struct tty_port *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699{
Jiri Slaby2cd9fa22012-04-02 13:54:25 +0200700 struct hvcs_struct *hvcsd = container_of(p, struct hvcs_struct, port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701 struct vio_dev *vdev;
702 unsigned long flags;
703
704 spin_lock(&hvcs_structs_lock);
705 spin_lock_irqsave(&hvcsd->lock, flags);
706
707 /* the list_del poisons the pointers */
708 list_del(&(hvcsd->next));
709
710 if (hvcsd->connected == 1) {
711 hvcs_partner_free(hvcsd);
712 printk(KERN_INFO "HVCS: Closed vty-server@%X and"
713 " partner vty@%X:%d connection.\n",
714 hvcsd->vdev->unit_address,
715 hvcsd->p_unit_address,
716 (uint32_t)hvcsd->p_partition_ID);
717 }
718 printk(KERN_INFO "HVCS: Destroyed hvcs_struct for vty-server@%X.\n",
719 hvcsd->vdev->unit_address);
720
721 vdev = hvcsd->vdev;
722 hvcsd->vdev = NULL;
723
724 hvcsd->p_unit_address = 0;
725 hvcsd->p_partition_ID = 0;
726 hvcs_return_index(hvcsd->index);
727 memset(&hvcsd->p_location_code[0], 0x00, HVCS_CLC_LENGTH + 1);
728
729 spin_unlock_irqrestore(&hvcsd->lock, flags);
730 spin_unlock(&hvcs_structs_lock);
731
Linas Vepstasc15bb292006-12-06 20:38:07 -0800732 sysfs_remove_group(&vdev->dev.kobj, &hvcs_attr_group);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733
734 kfree(hvcsd);
735}
736
Jiri Slaby2cd9fa22012-04-02 13:54:25 +0200737static const struct tty_port_operations hvcs_port_ops = {
738 .destruct = hvcs_destruct_port,
739};
740
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741static int hvcs_get_index(void)
742{
743 int i;
744 /* Paranoia check */
745 if (!hvcs_index_list) {
746 printk(KERN_ERR "HVCS: hvcs_index_list NOT valid!.\n");
747 return -EFAULT;
748 }
749 /* Find the numerically lowest first free index. */
750 for(i = 0; i < hvcs_index_count; i++) {
751 if (hvcs_index_list[i] == -1) {
752 hvcs_index_list[i] = 0;
753 return i;
754 }
755 }
756 return -1;
757}
758
Bill Pemberton9671f092012-11-19 13:21:50 -0500759static int hvcs_probe(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760 struct vio_dev *dev,
761 const struct vio_device_id *id)
762{
763 struct hvcs_struct *hvcsd;
Benjamin Herrenschmidtc7704d32011-02-06 18:26:25 +0000764 int index, rc;
Linas Vepstasc15bb292006-12-06 20:38:07 -0800765 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766
767 if (!dev || !id) {
768 printk(KERN_ERR "HVCS: probed with invalid parameter.\n");
769 return -EPERM;
770 }
771
Benjamin Herrenschmidtc7704d32011-02-06 18:26:25 +0000772 /* Make sure we are properly initialized */
773 rc = hvcs_initialize();
774 if (rc) {
775 pr_err("HVCS: Failed to initialize core driver.\n");
776 return rc;
777 }
778
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779 /* early to avoid cleanup on failure */
780 index = hvcs_get_index();
781 if (index < 0) {
782 return -EFAULT;
783 }
784
Yoann Padioleaudd00cc42007-07-19 01:49:03 -0700785 hvcsd = kzalloc(sizeof(*hvcsd), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786 if (!hvcsd)
787 return -ENODEV;
788
Jiri Slaby1997cf02012-04-02 13:54:24 +0200789 tty_port_init(&hvcsd->port);
Jiri Slaby2cd9fa22012-04-02 13:54:25 +0200790 hvcsd->port.ops = &hvcs_port_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791 spin_lock_init(&hvcsd->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792
793 hvcsd->vdev = dev;
Greg Kroah-Hartman317ccc42009-05-04 12:40:54 -0700794 dev_set_drvdata(&dev->dev, hvcsd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795
796 hvcsd->index = index;
797
798 /* hvcsd->index = ++hvcs_struct_count; */
799 hvcsd->chars_in_buffer = 0;
800 hvcsd->todo_mask = 0;
801 hvcsd->connected = 0;
802
803 /*
804 * This will populate the hvcs_struct's partner info fields for the
805 * first time.
806 */
807 if (hvcs_get_pi(hvcsd)) {
808 printk(KERN_ERR "HVCS: Failed to fetch partner"
809 " info for vty-server@%X on device probe.\n",
810 hvcsd->vdev->unit_address);
811 }
812
813 /*
814 * If a user app opens a tty that corresponds to this vty-server before
815 * the hvcs_struct has been added to the devices list then the user app
816 * will get -ENODEV.
817 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818 spin_lock(&hvcs_structs_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819 list_add_tail(&(hvcsd->next), &hvcs_structs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820 spin_unlock(&hvcs_structs_lock);
821
Linas Vepstasc15bb292006-12-06 20:38:07 -0800822 retval = sysfs_create_group(&dev->dev.kobj, &hvcs_attr_group);
823 if (retval) {
824 printk(KERN_ERR "HVCS: Can't create sysfs attrs for vty-server@%X\n",
825 hvcsd->vdev->unit_address);
826 return retval;
827 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828
829 printk(KERN_INFO "HVCS: vty-server@%X added to the vio bus.\n", dev->unit_address);
830
831 /*
832 * DON'T enable interrupts here because there is no user to receive the
833 * data.
834 */
835 return 0;
836}
837
Bill Pembertonae8d8a12012-11-19 13:26:18 -0500838static int hvcs_remove(struct vio_dev *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839{
Greg Kroah-Hartman317ccc42009-05-04 12:40:54 -0700840 struct hvcs_struct *hvcsd = dev_get_drvdata(&dev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842 struct tty_struct *tty;
843
844 if (!hvcsd)
845 return -ENODEV;
846
Joe Perches8dfba4d2008-02-03 17:11:42 +0200847 /* By this time the vty-server won't be getting any more interrupts */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848
849 spin_lock_irqsave(&hvcsd->lock, flags);
850
Jiri Slaby6968a752012-04-02 13:54:26 +0200851 tty = hvcsd->port.tty;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853 spin_unlock_irqrestore(&hvcsd->lock, flags);
854
855 /*
856 * Let the last holder of this object cause it to be removed, which
857 * would probably be tty_hangup below.
858 */
Jiri Slaby2cd9fa22012-04-02 13:54:25 +0200859 tty_port_put(&hvcsd->port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860
861 /*
862 * The hangup is a scheduled function which will auto chain call
863 * hvcs_hangup. The tty should always be valid at this time unless a
864 * simultaneous tty close already cleaned up the hvcs_struct.
865 */
866 if (tty)
867 tty_hangup(tty);
868
869 printk(KERN_INFO "HVCS: vty-server@%X removed from the"
870 " vio bus.\n", dev->unit_address);
871 return 0;
872};
873
874static struct vio_driver hvcs_vio_driver = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875 .id_table = hvcs_driver_table,
876 .probe = hvcs_probe,
Bill Pemberton91116cb2012-11-19 13:21:06 -0500877 .remove = hvcs_remove,
Benjamin Herrenschmidtcb52d892012-03-26 19:06:30 +0000878 .name = hvcs_driver_name,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879};
880
881/* Only called from hvcs_get_pi please */
882static void hvcs_set_pi(struct hvcs_partner_info *pi, struct hvcs_struct *hvcsd)
883{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884 hvcsd->p_unit_address = pi->unit_address;
885 hvcsd->p_partition_ID = pi->partition_ID;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886
887 /* copy the null-term char too */
Chen Gang9276dfd2013-02-25 17:43:25 +0000888 strlcpy(&hvcsd->p_location_code[0],
889 &pi->location_code[0], sizeof(hvcsd->p_location_code));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890}
891
892/*
893 * Traverse the list and add the partner info that is found to the hvcs_struct
894 * struct entry. NOTE: At this time I know that partner info will return a
895 * single entry but in the future there may be multiple partner info entries per
896 * vty-server and you'll want to zero out that list and reset it. If for some
897 * reason you have an old version of this driver but there IS more than one
898 * partner info then hvcsd->p_* will hold the last partner info data from the
899 * firmware query. A good way to update this code would be to replace the three
900 * partner info fields in hvcs_struct with a list of hvcs_partner_info
901 * instances.
902 *
903 * This function must be called with the hvcsd->lock held.
904 */
905static int hvcs_get_pi(struct hvcs_struct *hvcsd)
906{
907 struct hvcs_partner_info *pi;
908 uint32_t unit_address = hvcsd->vdev->unit_address;
909 struct list_head head;
910 int retval;
911
912 spin_lock(&hvcs_pi_lock);
913 if (!hvcs_pi_buff) {
914 spin_unlock(&hvcs_pi_lock);
915 return -EFAULT;
916 }
917 retval = hvcs_get_partner_info(unit_address, &head, hvcs_pi_buff);
918 spin_unlock(&hvcs_pi_lock);
919 if (retval) {
920 printk(KERN_ERR "HVCS: Failed to fetch partner"
921 " info for vty-server@%x.\n", unit_address);
922 return retval;
923 }
924
925 /* nixes the values if the partner vty went away */
926 hvcsd->p_unit_address = 0;
927 hvcsd->p_partition_ID = 0;
928
929 list_for_each_entry(pi, &head, node)
930 hvcs_set_pi(pi, hvcsd);
931
932 hvcs_free_partner_info(&head);
933 return 0;
934}
935
936/*
937 * This function is executed by the driver "rescan" sysfs entry. It shouldn't
938 * be executed elsewhere, in order to prevent deadlock issues.
939 */
940static int hvcs_rescan_devices_list(void)
941{
942 struct hvcs_struct *hvcsd;
943 unsigned long flags;
944
945 spin_lock(&hvcs_structs_lock);
946
947 list_for_each_entry(hvcsd, &hvcs_structs, next) {
948 spin_lock_irqsave(&hvcsd->lock, flags);
949 hvcs_get_pi(hvcsd);
950 spin_unlock_irqrestore(&hvcsd->lock, flags);
951 }
952
953 spin_unlock(&hvcs_structs_lock);
954
955 return 0;
956}
957
958/*
959 * Farm this off into its own function because it could be more complex once
960 * multiple partners support is added. This function should be called with
961 * the hvcsd->lock held.
962 */
963static int hvcs_has_pi(struct hvcs_struct *hvcsd)
964{
965 if ((!hvcsd->p_unit_address) || (!hvcsd->p_partition_ID))
966 return 0;
967 return 1;
968}
969
970/*
971 * NOTE: It is possible that the super admin removed a partner vty and then
972 * added a different vty as the new partner.
973 *
974 * This function must be called with the hvcsd->lock held.
975 */
976static int hvcs_partner_connect(struct hvcs_struct *hvcsd)
977{
978 int retval;
979 unsigned int unit_address = hvcsd->vdev->unit_address;
980
981 /*
982 * If there wasn't any pi when the device was added it doesn't meant
983 * there isn't any now. This driver isn't notified when a new partner
984 * vty is added to a vty-server so we discover changes on our own.
985 * Please see comments in hvcs_register_connection() for justification
986 * of this bizarre code.
987 */
988 retval = hvcs_register_connection(unit_address,
989 hvcsd->p_partition_ID,
990 hvcsd->p_unit_address);
991 if (!retval) {
992 hvcsd->connected = 1;
993 return 0;
994 } else if (retval != -EINVAL)
995 return retval;
996
997 /*
998 * As per the spec re-get the pi and try again if -EINVAL after the
999 * first connection attempt.
1000 */
1001 if (hvcs_get_pi(hvcsd))
1002 return -ENOMEM;
1003
1004 if (!hvcs_has_pi(hvcsd))
1005 return -ENODEV;
1006
1007 retval = hvcs_register_connection(unit_address,
1008 hvcsd->p_partition_ID,
1009 hvcsd->p_unit_address);
1010 if (retval != -EINVAL) {
1011 hvcsd->connected = 1;
1012 return retval;
1013 }
1014
1015 /*
1016 * EBUSY is the most likely scenario though the vty could have been
1017 * removed or there really could be an hcall error due to the parameter
1018 * data but thanks to ambiguous firmware return codes we can't really
1019 * tell.
1020 */
1021 printk(KERN_INFO "HVCS: vty-server or partner"
1022 " vty is busy. Try again later.\n");
1023 return -EBUSY;
1024}
1025
1026/* This function must be called with the hvcsd->lock held */
1027static void hvcs_partner_free(struct hvcs_struct *hvcsd)
1028{
1029 int retval;
1030 do {
1031 retval = hvcs_free_connection(hvcsd->vdev->unit_address);
1032 } while (retval == -EBUSY);
1033 hvcsd->connected = 0;
1034}
1035
1036/* This helper function must be called WITHOUT the hvcsd->lock held */
1037static int hvcs_enable_device(struct hvcs_struct *hvcsd, uint32_t unit_address,
1038 unsigned int irq, struct vio_dev *vdev)
1039{
1040 unsigned long flags;
1041 int rc;
1042
1043 /*
1044 * It is possible that the vty-server was removed between the time that
1045 * the conn was registered and now.
1046 */
Greg Kroah-Hartman873b4f12015-04-30 11:22:09 +02001047 rc = request_irq(irq, &hvcs_handle_interrupt, 0, "ibmhvcs", hvcsd);
1048 if (!rc) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049 /*
1050 * It is possible the vty-server was removed after the irq was
1051 * requested but before we have time to enable interrupts.
1052 */
Segher Boessenkool706c8c92006-03-30 14:49:40 +02001053 if (vio_enable_interrupts(vdev) == H_SUCCESS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001054 return 0;
1055 else {
1056 printk(KERN_ERR "HVCS: int enable failed for"
1057 " vty-server@%X.\n", unit_address);
1058 free_irq(irq, hvcsd);
1059 }
1060 } else
1061 printk(KERN_ERR "HVCS: irq req failed for"
1062 " vty-server@%X.\n", unit_address);
1063
1064 spin_lock_irqsave(&hvcsd->lock, flags);
1065 hvcs_partner_free(hvcsd);
1066 spin_unlock_irqrestore(&hvcsd->lock, flags);
1067
1068 return rc;
1069
1070}
1071
1072/*
Greg Kroah-Hartmanf7eb12c2007-11-28 10:46:22 -08001073 * This always increments the kref ref count if the call is successful.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001074 * Please remember to dec when you are done with the instance.
1075 *
1076 * NOTICE: Do NOT hold either the hvcs_struct.lock or hvcs_structs_lock when
1077 * calling this function or you will get deadlock.
1078 */
Stephen Rothwellb1c30112007-07-22 00:25:00 +10001079static struct hvcs_struct *hvcs_get_by_index(int index)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001080{
Jiri Slaby410235f2012-03-05 14:52:01 +01001081 struct hvcs_struct *hvcsd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001082 unsigned long flags;
1083
1084 spin_lock(&hvcs_structs_lock);
Jiri Slaby410235f2012-03-05 14:52:01 +01001085 list_for_each_entry(hvcsd, &hvcs_structs, next) {
1086 spin_lock_irqsave(&hvcsd->lock, flags);
1087 if (hvcsd->index == index) {
Jiri Slaby2cd9fa22012-04-02 13:54:25 +02001088 tty_port_get(&hvcsd->port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001089 spin_unlock_irqrestore(&hvcsd->lock, flags);
Jiri Slaby410235f2012-03-05 14:52:01 +01001090 spin_unlock(&hvcs_structs_lock);
1091 return hvcsd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001092 }
Jiri Slaby410235f2012-03-05 14:52:01 +01001093 spin_unlock_irqrestore(&hvcsd->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001094 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001095 spin_unlock(&hvcs_structs_lock);
Jiri Slaby410235f2012-03-05 14:52:01 +01001096
1097 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001098}
1099
Jiri Slaby27bf7c42012-08-07 21:48:06 +02001100static int hvcs_install(struct tty_driver *driver, struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001101{
1102 struct hvcs_struct *hvcsd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001103 struct vio_dev *vdev;
Jiri Slaby97d15082012-08-07 21:48:05 +02001104 unsigned long unit_address, flags;
1105 unsigned int irq;
1106 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001107
Linus Torvalds1da177e2005-04-16 15:20:36 -07001108 /*
1109 * Is there a vty-server that shares the same index?
Greg Kroah-Hartmanf7eb12c2007-11-28 10:46:22 -08001110 * This function increments the kref index.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001111 */
Jiri Slaby97d15082012-08-07 21:48:05 +02001112 hvcsd = hvcs_get_by_index(tty->index);
1113 if (!hvcsd) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001114 printk(KERN_WARNING "HVCS: open failed, no device associated"
1115 " with tty->index %d.\n", tty->index);
1116 return -ENODEV;
1117 }
1118
1119 spin_lock_irqsave(&hvcsd->lock, flags);
1120
Jiri Slaby97d15082012-08-07 21:48:05 +02001121 if (hvcsd->connected == 0) {
1122 retval = hvcs_partner_connect(hvcsd);
1123 if (retval) {
1124 spin_unlock_irqrestore(&hvcsd->lock, flags);
1125 printk(KERN_WARNING "HVCS: partner connect failed.\n");
1126 goto err_put;
1127 }
1128 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001129
Jiri Slaby27bf7c42012-08-07 21:48:06 +02001130 hvcsd->port.count = 0;
Jiri Slaby6968a752012-04-02 13:54:26 +02001131 hvcsd->port.tty = tty;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001132 tty->driver_data = hvcsd;
1133
Linus Torvalds1da177e2005-04-16 15:20:36 -07001134 memset(&hvcsd->buffer[0], 0x00, HVCS_BUFF_LEN);
1135
1136 /*
1137 * Save these in the spinlock for the enable operations that need them
1138 * outside of the spinlock.
1139 */
1140 irq = hvcsd->vdev->irq;
1141 vdev = hvcsd->vdev;
1142 unit_address = hvcsd->vdev->unit_address;
1143
1144 hvcsd->todo_mask |= HVCS_SCHED_READ;
1145 spin_unlock_irqrestore(&hvcsd->lock, flags);
1146
1147 /*
1148 * This must be done outside of the spinlock because it requests irqs
1149 * and will grab the spinlock and free the connection if it fails.
1150 */
Jiri Slaby97d15082012-08-07 21:48:05 +02001151 retval = hvcs_enable_device(hvcsd, unit_address, irq, vdev);
1152 if (retval) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001153 printk(KERN_WARNING "HVCS: enable device failed.\n");
Jiri Slaby97d15082012-08-07 21:48:05 +02001154 goto err_put;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001155 }
1156
Jiri Slaby27bf7c42012-08-07 21:48:06 +02001157 retval = tty_port_install(&hvcsd->port, driver, tty);
1158 if (retval)
1159 goto err_irq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001160
Jiri Slaby27bf7c42012-08-07 21:48:06 +02001161 return 0;
1162err_irq:
1163 spin_lock_irqsave(&hvcsd->lock, flags);
1164 vio_disable_interrupts(hvcsd->vdev);
1165 spin_unlock_irqrestore(&hvcsd->lock, flags);
1166 free_irq(irq, hvcsd);
1167err_put:
1168 tty_port_put(&hvcsd->port);
1169
1170 return retval;
1171}
1172
1173/*
1174 * This is invoked via the tty_open interface when a user app connects to the
1175 * /dev node.
1176 */
1177static int hvcs_open(struct tty_struct *tty, struct file *filp)
1178{
1179 struct hvcs_struct *hvcsd = tty->driver_data;
1180 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001181
1182 spin_lock_irqsave(&hvcsd->lock, flags);
Jiri Slaby1997cf02012-04-02 13:54:24 +02001183 hvcsd->port.count++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001184 hvcsd->todo_mask |= HVCS_SCHED_READ;
1185 spin_unlock_irqrestore(&hvcsd->lock, flags);
Greg Kroah-Hartmanf7eb12c2007-11-28 10:46:22 -08001186
Linus Torvalds1da177e2005-04-16 15:20:36 -07001187 hvcs_kick();
1188
1189 printk(KERN_INFO "HVCS: vty-server@%X connection opened.\n",
1190 hvcsd->vdev->unit_address );
1191
1192 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001193}
1194
1195static void hvcs_close(struct tty_struct *tty, struct file *filp)
1196{
1197 struct hvcs_struct *hvcsd;
1198 unsigned long flags;
Alan Coxd4e33fa2012-01-26 17:44:09 +00001199 int irq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001200
1201 /*
1202 * Is someone trying to close the file associated with this device after
1203 * we have hung up? If so tty->driver_data wouldn't be valid.
1204 */
1205 if (tty_hung_up_p(filp))
1206 return;
1207
1208 /*
1209 * No driver_data means that this close was probably issued after a
1210 * failed hvcs_open by the tty layer's release_dev() api and we can just
1211 * exit cleanly.
1212 */
1213 if (!tty->driver_data)
1214 return;
1215
1216 hvcsd = tty->driver_data;
1217
1218 spin_lock_irqsave(&hvcsd->lock, flags);
Jiri Slaby1997cf02012-04-02 13:54:24 +02001219 if (--hvcsd->port.count == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001220
1221 vio_disable_interrupts(hvcsd->vdev);
1222
1223 /*
1224 * NULL this early so that the kernel_thread doesn't try to
1225 * execute any operations on the TTY even though it is obligated
1226 * to deliver any pending I/O to the hypervisor.
1227 */
Jiri Slaby6968a752012-04-02 13:54:26 +02001228 hvcsd->port.tty = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001229
1230 irq = hvcsd->vdev->irq;
1231 spin_unlock_irqrestore(&hvcsd->lock, flags);
1232
Peter Hurley79c1faa2015-10-10 16:00:51 -04001233 tty_wait_until_sent(tty, HVCS_CLOSE_WAIT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001234
1235 /*
1236 * This line is important because it tells hvcs_open that this
1237 * device needs to be re-configured the next time hvcs_open is
1238 * called.
1239 */
1240 tty->driver_data = NULL;
1241
1242 free_irq(irq, hvcsd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001243 return;
Jiri Slaby1997cf02012-04-02 13:54:24 +02001244 } else if (hvcsd->port.count < 0) {
Colin Ian King228a9ff2017-04-22 13:35:33 +01001245 printk(KERN_ERR "HVCS: vty-server@%X open_count: %d is mismanaged.\n",
Jiri Slaby1997cf02012-04-02 13:54:24 +02001246 hvcsd->vdev->unit_address, hvcsd->port.count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001247 }
1248
1249 spin_unlock_irqrestore(&hvcsd->lock, flags);
Jiri Slaby27bf7c42012-08-07 21:48:06 +02001250}
1251
1252static void hvcs_cleanup(struct tty_struct * tty)
1253{
1254 struct hvcs_struct *hvcsd = tty->driver_data;
1255
Jiri Slaby2cd9fa22012-04-02 13:54:25 +02001256 tty_port_put(&hvcsd->port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001257}
1258
1259static void hvcs_hangup(struct tty_struct * tty)
1260{
1261 struct hvcs_struct *hvcsd = tty->driver_data;
1262 unsigned long flags;
1263 int temp_open_count;
Alan Coxd4e33fa2012-01-26 17:44:09 +00001264 int irq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001265
1266 spin_lock_irqsave(&hvcsd->lock, flags);
Greg Kroah-Hartmanf7eb12c2007-11-28 10:46:22 -08001267 /* Preserve this so that we know how many kref refs to put */
Jiri Slaby1997cf02012-04-02 13:54:24 +02001268 temp_open_count = hvcsd->port.count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001269
1270 /*
Greg Kroah-Hartmanf7eb12c2007-11-28 10:46:22 -08001271 * Don't kref put inside the spinlock because the destruction
Linus Torvalds1da177e2005-04-16 15:20:36 -07001272 * callback may use the spinlock and it may get called before the
Greg Kroah-Hartmanf7eb12c2007-11-28 10:46:22 -08001273 * spinlock has been released.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001274 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001275 vio_disable_interrupts(hvcsd->vdev);
1276
1277 hvcsd->todo_mask = 0;
1278
1279 /* I don't think the tty needs the hvcs_struct pointer after a hangup */
Jiri Slaby6968a752012-04-02 13:54:26 +02001280 tty->driver_data = NULL;
1281 hvcsd->port.tty = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001282
Jiri Slaby1997cf02012-04-02 13:54:24 +02001283 hvcsd->port.count = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001284
1285 /* This will drop any buffered data on the floor which is OK in a hangup
1286 * scenario. */
1287 memset(&hvcsd->buffer[0], 0x00, HVCS_BUFF_LEN);
1288 hvcsd->chars_in_buffer = 0;
1289
1290 irq = hvcsd->vdev->irq;
1291
1292 spin_unlock_irqrestore(&hvcsd->lock, flags);
1293
1294 free_irq(irq, hvcsd);
1295
1296 /*
Greg Kroah-Hartmanf7eb12c2007-11-28 10:46:22 -08001297 * We need to kref_put() for every open_count we have since the
Linus Torvalds1da177e2005-04-16 15:20:36 -07001298 * tty_hangup() function doesn't invoke a close per open connection on a
1299 * non-console device.
1300 */
1301 while(temp_open_count) {
1302 --temp_open_count;
1303 /*
1304 * The final put will trigger destruction of the hvcs_struct.
1305 * NOTE: If this hangup was signaled from user space then the
1306 * final put will never happen.
1307 */
Jiri Slaby2cd9fa22012-04-02 13:54:25 +02001308 tty_port_put(&hvcsd->port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001309 }
1310}
1311
1312/*
1313 * NOTE: This is almost always from_user since user level apps interact with the
1314 * /dev nodes. I'm trusting that if hvcs_write gets called and interrupted by
1315 * hvcs_remove (which removes the target device and executes tty_hangup()) that
1316 * tty_hangup will allow hvcs_write time to complete execution before it
1317 * terminates our device.
1318 */
1319static int hvcs_write(struct tty_struct *tty,
1320 const unsigned char *buf, int count)
1321{
1322 struct hvcs_struct *hvcsd = tty->driver_data;
1323 unsigned int unit_address;
1324 const unsigned char *charbuf;
1325 unsigned long flags;
1326 int total_sent = 0;
1327 int tosend = 0;
1328 int result = 0;
1329
1330 /*
1331 * If they don't check the return code off of their open they may
1332 * attempt this even if there is no connected device.
1333 */
1334 if (!hvcsd)
1335 return -ENODEV;
1336
1337 /* Reasonable size to prevent user level flooding */
1338 if (count > HVCS_MAX_FROM_USER) {
1339 printk(KERN_WARNING "HVCS write: count being truncated to"
1340 " HVCS_MAX_FROM_USER.\n");
1341 count = HVCS_MAX_FROM_USER;
1342 }
1343
1344 charbuf = buf;
1345
1346 spin_lock_irqsave(&hvcsd->lock, flags);
1347
1348 /*
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001349 * Somehow an open succeeded but the device was removed or the
Linus Torvalds1da177e2005-04-16 15:20:36 -07001350 * connection terminated between the vty-server and partner vty during
1351 * the middle of a write operation? This is a crummy place to do this
1352 * but we want to keep it all in the spinlock.
1353 */
Jiri Slaby1997cf02012-04-02 13:54:24 +02001354 if (hvcsd->port.count <= 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001355 spin_unlock_irqrestore(&hvcsd->lock, flags);
1356 return -ENODEV;
1357 }
1358
1359 unit_address = hvcsd->vdev->unit_address;
1360
1361 while (count > 0) {
1362 tosend = min(count, (HVCS_BUFF_LEN - hvcsd->chars_in_buffer));
1363 /*
1364 * No more space, this probably means that the last call to
1365 * hvcs_write() didn't succeed and the buffer was filled up.
1366 */
1367 if (!tosend)
1368 break;
1369
1370 memcpy(&hvcsd->buffer[hvcsd->chars_in_buffer],
1371 &charbuf[total_sent],
1372 tosend);
1373
1374 hvcsd->chars_in_buffer += tosend;
1375
1376 result = 0;
1377
1378 /*
1379 * If this is true then we don't want to try writing to the
1380 * hypervisor because that is the kernel_threads job now. We'll
1381 * just add to the buffer.
1382 */
1383 if (!(hvcsd->todo_mask & HVCS_TRY_WRITE))
1384 /* won't send partial writes */
1385 result = hvc_put_chars(unit_address,
1386 &hvcsd->buffer[0],
1387 hvcsd->chars_in_buffer);
1388
1389 /*
1390 * Since we know we have enough room in hvcsd->buffer for
1391 * tosend we record that it was sent regardless of whether the
1392 * hypervisor actually took it because we have it buffered.
1393 */
1394 total_sent+=tosend;
1395 count-=tosend;
1396 if (result == 0) {
1397 hvcsd->todo_mask |= HVCS_TRY_WRITE;
1398 hvcs_kick();
1399 break;
1400 }
1401
1402 hvcsd->chars_in_buffer = 0;
1403 /*
1404 * Test after the chars_in_buffer reset otherwise this could
1405 * deadlock our writes if hvc_put_chars fails.
1406 */
1407 if (result < 0)
1408 break;
1409 }
1410
1411 spin_unlock_irqrestore(&hvcsd->lock, flags);
1412
1413 if (result == -1)
1414 return -EIO;
1415 else
1416 return total_sent;
1417}
1418
1419/*
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001420 * This is really asking how much can we guarantee that we can send or that we
Linus Torvalds1da177e2005-04-16 15:20:36 -07001421 * absolutely WILL BUFFER if we can't send it. This driver MUST honor the
1422 * return value, hence the reason for hvcs_struct buffering.
1423 */
1424static int hvcs_write_room(struct tty_struct *tty)
1425{
1426 struct hvcs_struct *hvcsd = tty->driver_data;
1427
Jiri Slaby1997cf02012-04-02 13:54:24 +02001428 if (!hvcsd || hvcsd->port.count <= 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001429 return 0;
1430
1431 return HVCS_BUFF_LEN - hvcsd->chars_in_buffer;
1432}
1433
1434static int hvcs_chars_in_buffer(struct tty_struct *tty)
1435{
1436 struct hvcs_struct *hvcsd = tty->driver_data;
1437
1438 return hvcsd->chars_in_buffer;
1439}
1440
Jeff Dikeb68e31d2006-10-02 02:17:18 -07001441static const struct tty_operations hvcs_ops = {
Jiri Slaby27bf7c42012-08-07 21:48:06 +02001442 .install = hvcs_install,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001443 .open = hvcs_open,
1444 .close = hvcs_close,
Jiri Slaby27bf7c42012-08-07 21:48:06 +02001445 .cleanup = hvcs_cleanup,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001446 .hangup = hvcs_hangup,
1447 .write = hvcs_write,
1448 .write_room = hvcs_write_room,
1449 .chars_in_buffer = hvcs_chars_in_buffer,
1450 .unthrottle = hvcs_unthrottle,
1451 .throttle = hvcs_throttle,
1452};
1453
1454static int hvcs_alloc_index_list(int n)
1455{
1456 int i;
Jesper Juhl8f760782006-06-27 02:55:06 -07001457
Linus Torvalds1da177e2005-04-16 15:20:36 -07001458 hvcs_index_list = kmalloc(n * sizeof(hvcs_index_count),GFP_KERNEL);
1459 if (!hvcs_index_list)
1460 return -ENOMEM;
1461 hvcs_index_count = n;
Jesper Juhl8f760782006-06-27 02:55:06 -07001462 for (i = 0; i < hvcs_index_count; i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001463 hvcs_index_list[i] = -1;
1464 return 0;
1465}
1466
1467static void hvcs_free_index_list(void)
1468{
1469 /* Paranoia check to be thorough. */
Jesper Juhl8f760782006-06-27 02:55:06 -07001470 kfree(hvcs_index_list);
1471 hvcs_index_list = NULL;
1472 hvcs_index_count = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001473}
1474
Bill Pemberton9671f092012-11-19 13:21:50 -05001475static int hvcs_initialize(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001476{
Benjamin Herrenschmidtc7704d32011-02-06 18:26:25 +00001477 int rc, num_ttys_to_alloc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001478
Benjamin Herrenschmidtc7704d32011-02-06 18:26:25 +00001479 mutex_lock(&hvcs_init_mutex);
1480 if (hvcs_task) {
1481 mutex_unlock(&hvcs_init_mutex);
1482 return 0;
1483 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001484
1485 /* Has the user specified an overload with an insmod param? */
1486 if (hvcs_parm_num_devs <= 0 ||
1487 (hvcs_parm_num_devs > HVCS_MAX_SERVER_ADAPTERS)) {
1488 num_ttys_to_alloc = HVCS_DEFAULT_SERVER_ADAPTERS;
1489 } else
1490 num_ttys_to_alloc = hvcs_parm_num_devs;
1491
1492 hvcs_tty_driver = alloc_tty_driver(num_ttys_to_alloc);
Wei Yongjunad3d1e52012-10-22 12:42:59 +08001493 if (!hvcs_tty_driver) {
1494 mutex_unlock(&hvcs_init_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001495 return -ENOMEM;
Wei Yongjunad3d1e52012-10-22 12:42:59 +08001496 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001497
Linas Vepstasc15bb292006-12-06 20:38:07 -08001498 if (hvcs_alloc_index_list(num_ttys_to_alloc)) {
1499 rc = -ENOMEM;
1500 goto index_fail;
1501 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001502
Linus Torvalds1da177e2005-04-16 15:20:36 -07001503 hvcs_tty_driver->driver_name = hvcs_driver_name;
1504 hvcs_tty_driver->name = hvcs_device_node;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001505
1506 /*
1507 * We'll let the system assign us a major number, indicated by leaving
1508 * it blank.
1509 */
1510
1511 hvcs_tty_driver->minor_start = HVCS_MINOR_START;
1512 hvcs_tty_driver->type = TTY_DRIVER_TYPE_SYSTEM;
1513
1514 /*
1515 * We role our own so that we DONT ECHO. We can't echo because the
1516 * device we are connecting to already echoes by default and this would
1517 * throw us into a horrible recursive echo-echo-echo loop.
1518 */
1519 hvcs_tty_driver->init_termios = hvcs_tty_termios;
1520 hvcs_tty_driver->flags = TTY_DRIVER_REAL_RAW;
1521
1522 tty_set_operations(hvcs_tty_driver, &hvcs_ops);
1523
1524 /*
1525 * The following call will result in sysfs entries that denote the
1526 * dynamically assigned major and minor numbers for our devices.
1527 */
1528 if (tty_register_driver(hvcs_tty_driver)) {
Linas Vepstasc15bb292006-12-06 20:38:07 -08001529 printk(KERN_ERR "HVCS: registration as a tty driver failed.\n");
1530 rc = -EIO;
1531 goto register_fail;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001532 }
1533
Brian Kingac07a4a2011-09-13 11:22:51 +00001534 hvcs_pi_buff = (unsigned long *) __get_free_page(GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001535 if (!hvcs_pi_buff) {
Linas Vepstasc15bb292006-12-06 20:38:07 -08001536 rc = -ENOMEM;
1537 goto buff_alloc_fail;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001538 }
1539
1540 hvcs_task = kthread_run(khvcsd, NULL, "khvcsd");
1541 if (IS_ERR(hvcs_task)) {
Benjamin Herrenschmidtc7704d32011-02-06 18:26:25 +00001542 printk(KERN_ERR "HVCS: khvcsd creation failed.\n");
Linas Vepstasc15bb292006-12-06 20:38:07 -08001543 rc = -EIO;
1544 goto kthread_fail;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001545 }
Benjamin Herrenschmidtc7704d32011-02-06 18:26:25 +00001546 mutex_unlock(&hvcs_init_mutex);
Linas Vepstasc15bb292006-12-06 20:38:07 -08001547 return 0;
1548
Linas Vepstasc15bb292006-12-06 20:38:07 -08001549kthread_fail:
Brian Kingac07a4a2011-09-13 11:22:51 +00001550 free_page((unsigned long)hvcs_pi_buff);
Linas Vepstasc15bb292006-12-06 20:38:07 -08001551buff_alloc_fail:
1552 tty_unregister_driver(hvcs_tty_driver);
1553register_fail:
1554 hvcs_free_index_list();
1555index_fail:
1556 put_tty_driver(hvcs_tty_driver);
1557 hvcs_tty_driver = NULL;
Benjamin Herrenschmidtc7704d32011-02-06 18:26:25 +00001558 mutex_unlock(&hvcs_init_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001559 return rc;
1560}
1561
Benjamin Herrenschmidtc7704d32011-02-06 18:26:25 +00001562static int __init hvcs_module_init(void)
1563{
1564 int rc = vio_register_driver(&hvcs_vio_driver);
1565 if (rc) {
1566 printk(KERN_ERR "HVCS: can't register vio driver\n");
1567 return rc;
1568 }
1569
1570 pr_info("HVCS: Driver registered.\n");
1571
1572 /* This needs to be done AFTER the vio_register_driver() call or else
1573 * the kobjects won't be initialized properly.
1574 */
1575 rc = driver_create_file(&(hvcs_vio_driver.driver), &driver_attr_rescan);
1576 if (rc)
Joe Perchesd437fa92017-02-16 23:11:44 -08001577 pr_warn("HVCS: Failed to create rescan file (err %d)\n", rc);
Benjamin Herrenschmidtc7704d32011-02-06 18:26:25 +00001578
1579 return 0;
1580}
1581
Linus Torvalds1da177e2005-04-16 15:20:36 -07001582static void __exit hvcs_module_exit(void)
1583{
1584 /*
1585 * This driver receives hvcs_remove callbacks for each device upon
1586 * module removal.
1587 */
Benjamin Herrenschmidtc7704d32011-02-06 18:26:25 +00001588 vio_unregister_driver(&hvcs_vio_driver);
1589 if (!hvcs_task)
1590 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001591
1592 /*
1593 * This synchronous operation will wake the khvcsd kthread if it is
1594 * asleep and will return when khvcsd has terminated.
1595 */
1596 kthread_stop(hvcs_task);
1597
1598 spin_lock(&hvcs_pi_lock);
Brian Kingac07a4a2011-09-13 11:22:51 +00001599 free_page((unsigned long)hvcs_pi_buff);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001600 hvcs_pi_buff = NULL;
1601 spin_unlock(&hvcs_pi_lock);
1602
Linas Vepstasc15bb292006-12-06 20:38:07 -08001603 driver_remove_file(&hvcs_vio_driver.driver, &driver_attr_rescan);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001604
Linus Torvalds1da177e2005-04-16 15:20:36 -07001605 tty_unregister_driver(hvcs_tty_driver);
1606
1607 hvcs_free_index_list();
1608
1609 put_tty_driver(hvcs_tty_driver);
1610
1611 printk(KERN_INFO "HVCS: driver module removed.\n");
1612}
1613
1614module_init(hvcs_module_init);
1615module_exit(hvcs_module_exit);