blob: 7320190a5886ee1498f9f38c9f2141156db42738 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * IBM eServer Hypervisor Virtual Console Server Device Driver
3 * Copyright (C) 2003, 2004 IBM Corp.
4 * Ryan S. Arnold (rsa@us.ibm.com)
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 *
20 * Author(s) : Ryan S. Arnold <rsa@us.ibm.com>
21 *
22 * This is the device driver for the IBM Hypervisor Virtual Console Server,
23 * "hvcs". The IBM hvcs provides a tty driver interface to allow Linux
24 * user space applications access to the system consoles of logically
25 * partitioned operating systems, e.g. Linux, running on the same partitioned
26 * Power5 ppc64 system. Physical hardware consoles per partition are not
27 * practical on this hardware so system consoles are accessed by this driver
28 * using inter-partition firmware interfaces to virtual terminal devices.
29 *
30 * A vty is known to the HMC as a "virtual serial server adapter". It is a
31 * virtual terminal device that is created by firmware upon partition creation
32 * to act as a partitioned OS's console device.
33 *
34 * Firmware dynamically (via hotplug) exposes vty-servers to a running ppc64
35 * Linux system upon their creation by the HMC or their exposure during boot.
36 * The non-user interactive backend of this driver is implemented as a vio
37 * device driver so that it can receive notification of vty-server lifetimes
38 * after it registers with the vio bus to handle vty-server probe and remove
39 * callbacks.
40 *
41 * Many vty-servers can be configured to connect to one vty, but a vty can
42 * only be actively connected to by a single vty-server, in any manner, at one
43 * time. If the HMC is currently hosting the console for a target Linux
44 * partition; attempts to open the tty device to the partition's console using
45 * the hvcs on any partition will return -EBUSY with every open attempt until
46 * the HMC frees the connection between its vty-server and the desired
47 * partition's vty device. Conversely, a vty-server may only be connected to
48 * a single vty at one time even though it may have several configured vty
49 * partner possibilities.
50 *
51 * Firmware does not provide notification of vty partner changes to this
52 * driver. This means that an HMC Super Admin may add or remove partner vtys
53 * from a vty-server's partner list but the changes will not be signaled to
54 * the vty-server. Firmware only notifies the driver when a vty-server is
55 * added or removed from the system. To compensate for this deficiency, this
56 * driver implements a sysfs update attribute which provides a method for
57 * rescanning partner information upon a user's request.
58 *
59 * Each vty-server, prior to being exposed to this driver is reference counted
Greg Kroah-Hartmanf7eb12c2007-11-28 10:46:22 -080060 * using the 2.6 Linux kernel kref construct.
Linus Torvalds1da177e2005-04-16 15:20:36 -070061 *
62 * For direction on installation and usage of this driver please reference
63 * Documentation/powerpc/hvcs.txt.
64 */
65
66#include <linux/device.h>
67#include <linux/init.h>
68#include <linux/interrupt.h>
69#include <linux/kernel.h>
Greg Kroah-Hartmanf7eb12c2007-11-28 10:46:22 -080070#include <linux/kref.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070071#include <linux/kthread.h>
72#include <linux/list.h>
73#include <linux/major.h>
74#include <linux/module.h>
75#include <linux/moduleparam.h>
76#include <linux/sched.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090077#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070078#include <linux/spinlock.h>
79#include <linux/stat.h>
80#include <linux/tty.h>
81#include <linux/tty_flip.h>
82#include <asm/hvconsole.h>
83#include <asm/hvcserver.h>
Linus Torvalds7c0f6ba2016-12-24 11:46:01 -080084#include <linux/uaccess.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070085#include <asm/vio.h>
86
87/*
88 * 1.3.0 -> 1.3.1 In hvcs_open memset(..,0x00,..) instead of memset(..,0x3F,00).
89 * Removed braces around single statements following conditionals. Removed '=
90 * 0' after static int declarations since these default to zero. Removed
91 * list_for_each_safe() and replaced with list_for_each_entry() in
92 * hvcs_get_by_index(). The 'safe' version is un-needed now that the driver is
93 * using spinlocks. Changed spin_lock_irqsave() to spin_lock() when locking
94 * hvcs_structs_lock and hvcs_pi_lock since these are not touched in an int
95 * handler. Initialized hvcs_structs_lock and hvcs_pi_lock to
96 * SPIN_LOCK_UNLOCKED at declaration time rather than in hvcs_module_init().
97 * Added spin_lock around list_del() in destroy_hvcs_struct() to protect the
98 * list traversals from a deletion. Removed '= NULL' from pointer declaration
99 * statements since they are initialized NULL by default. Removed wmb()
100 * instances from hvcs_try_write(). They probably aren't needed with locking in
101 * place. Added check and cleanup for hvcs_pi_buff = kmalloc() in
102 * hvcs_module_init(). Exposed hvcs_struct.index via a sysfs attribute so that
103 * the coupling between /dev/hvcs* and a vty-server can be automatically
104 * determined. Moved kobject_put() in hvcs_open outside of the
105 * spin_unlock_irqrestore().
106 *
107 * 1.3.1 -> 1.3.2 Changed method for determining hvcs_struct->index and had it
108 * align with how the tty layer always assigns the lowest index available. This
109 * change resulted in a list of ints that denotes which indexes are available.
110 * Device additions and removals use the new hvcs_get_index() and
111 * hvcs_return_index() helper functions. The list is created with
112 * hvsc_alloc_index_list() and it is destroyed with hvcs_free_index_list().
113 * Without these fixes hotplug vty-server adapter support goes crazy with this
114 * driver if the user removes a vty-server adapter. Moved free_irq() outside of
115 * the hvcs_final_close() function in order to get it out of the spinlock.
116 * Rearranged hvcs_close(). Cleaned up some printks and did some housekeeping
117 * on the changelog. Removed local CLC_LENGTH and used HVCS_CLC_LENGTH from
Stephen Rothwellb8b572e2008-08-01 15:20:30 +1000118 * arch/powerepc/include/asm/hvcserver.h
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119 *
120 * 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 -0300121 * prevent possible lockup with realtime scheduling as similarly pointed out by
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122 * akpm in hvc_console. Changed resulted in the removal of hvcs_final_close()
123 * to reorder cleanup operations and prevent discarding of pending data during
124 * an hvcs_close(). Removed spinlock protection of hvcs_struct data members in
125 * hvcs_write_room() and hvcs_chars_in_buffer() because they aren't needed.
126 */
127
128#define HVCS_DRIVER_VERSION "1.3.3"
129
130MODULE_AUTHOR("Ryan S. Arnold <rsa@us.ibm.com>");
131MODULE_DESCRIPTION("IBM hvcs (Hypervisor Virtual Console Server) Driver");
132MODULE_LICENSE("GPL");
133MODULE_VERSION(HVCS_DRIVER_VERSION);
134
135/*
136 * Wait this long per iteration while trying to push buffered data to the
137 * hypervisor before allowing the tty to complete a close operation.
138 */
139#define HVCS_CLOSE_WAIT (HZ/100) /* 1/10 of a second */
140
141/*
142 * Since the Linux TTY code does not currently (2-04-2004) support dynamic
143 * addition of tty derived devices and we shouldn't allocate thousands of
144 * tty_device pointers when the number of vty-server & vty partner connections
145 * will most often be much lower than this, we'll arbitrarily allocate
146 * HVCS_DEFAULT_SERVER_ADAPTERS tty_structs and cdev's by default when we
147 * register the tty_driver. This can be overridden using an insmod parameter.
148 */
149#define HVCS_DEFAULT_SERVER_ADAPTERS 64
150
151/*
152 * The user can't insmod with more than HVCS_MAX_SERVER_ADAPTERS hvcs device
153 * nodes as a sanity check. Theoretically there can be over 1 Billion
154 * vty-server & vty partner connections.
155 */
156#define HVCS_MAX_SERVER_ADAPTERS 1024
157
158/*
159 * We let Linux assign us a major number and we start the minors at zero. There
160 * is no intuitive mapping between minor number and the target vty-server
161 * adapter except that each new vty-server adapter is always assigned to the
162 * smallest minor number available.
163 */
164#define HVCS_MINOR_START 0
165
166/*
167 * The hcall interface involves putting 8 chars into each of two registers.
Jon Mason2ef94812006-01-23 10:58:20 -0600168 * We load up those 2 registers (in arch/powerpc/platforms/pseries/hvconsole.c)
169 * by casting char[16] to long[2]. It would work without __ALIGNED__, but a
170 * little (tiny) bit slower because an unaligned load is slower than aligned
171 * load.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172 */
173#define __ALIGNED__ __attribute__((__aligned__(8)))
174
175/*
176 * How much data can firmware send with each hvc_put_chars()? Maybe this
177 * should be moved into an architecture specific area.
178 */
179#define HVCS_BUFF_LEN 16
180
181/*
182 * This is the maximum amount of data we'll let the user send us (hvcs_write) at
183 * once in a chunk as a sanity check.
184 */
185#define HVCS_MAX_FROM_USER 4096
186
187/*
188 * Be careful when adding flags to this line discipline. Don't add anything
189 * that will cause echoing or we'll go into recursive loop echoing chars back
190 * and forth with the console drivers.
191 */
Alan Cox606d0992006-12-08 02:38:45 -0800192static struct ktermios hvcs_tty_termios = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193 .c_iflag = IGNBRK | IGNPAR,
194 .c_oflag = OPOST,
195 .c_cflag = B38400 | CS8 | CREAD | HUPCL,
Alan Cox606d0992006-12-08 02:38:45 -0800196 .c_cc = INIT_C_CC,
197 .c_ispeed = 38400,
198 .c_ospeed = 38400
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199};
200
201/*
202 * This value is used to take the place of a command line parameter when the
203 * module is inserted. It starts as -1 and stays as such if the user doesn't
204 * specify a module insmod parameter. If they DO specify one then it is set to
205 * the value of the integer passed in.
206 */
207static int hvcs_parm_num_devs = -1;
208module_param(hvcs_parm_num_devs, int, 0);
209
Stephen Rothwellb1c30112007-07-22 00:25:00 +1000210static const char hvcs_driver_name[] = "hvcs";
211static const char hvcs_device_node[] = "hvcs";
212static const char hvcs_driver_string[]
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213 = "IBM hvcs (Hypervisor Virtual Console Server) Driver";
214
215/* Status of partner info rescan triggered via sysfs. */
216static int hvcs_rescan_status;
217
218static struct tty_driver *hvcs_tty_driver;
219
220/*
221 * In order to be somewhat sane this driver always associates the hvcs_struct
222 * index element with the numerically equal tty->index. This means that a
223 * hotplugged vty-server adapter will always map to the lowest index valued
224 * device node. If vty-servers were hotplug removed from the system and then
225 * new ones added the new vty-server may have the largest slot number of all
226 * the vty-server adapters in the partition but it may have the lowest dev node
227 * index of all the adapters due to the hole left by the hotplug removed
228 * adapter. There are a set of functions provided to get the lowest index for
229 * a new device as well as return the index to the list. This list is allocated
230 * with a number of elements equal to the number of device nodes requested when
231 * the module was inserted.
232 */
233static int *hvcs_index_list;
234
235/*
236 * How large is the list? This is kept for traversal since the list is
237 * dynamically created.
238 */
239static int hvcs_index_count;
240
241/*
242 * Used by the khvcsd to pick up I/O operations when the kernel_thread is
243 * already awake but potentially shifted to TASK_INTERRUPTIBLE state.
244 */
245static int hvcs_kicked;
246
247/*
248 * Use by the kthread construct for task operations like waking the sleeping
249 * thread and stopping the kthread.
250 */
251static struct task_struct *hvcs_task;
252
253/*
254 * We allocate this for the use of all of the hvcs_structs when they fetch
255 * partner info.
256 */
257static unsigned long *hvcs_pi_buff;
258
259/* Only allow one hvcs_struct to use the hvcs_pi_buff at a time. */
260static DEFINE_SPINLOCK(hvcs_pi_lock);
261
262/* One vty-server per hvcs_struct */
263struct hvcs_struct {
Jiri Slaby1997cf02012-04-02 13:54:24 +0200264 struct tty_port port;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265 spinlock_t lock;
266
267 /*
268 * This index identifies this hvcs device as the complement to a
269 * specific tty index.
270 */
271 unsigned int index;
272
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273 /*
274 * Used to tell the driver kernel_thread what operations need to take
275 * place upon this hvcs_struct instance.
276 */
277 int todo_mask;
278
279 /*
280 * This buffer is required so that when hvcs_write_room() reports that
281 * it can send HVCS_BUFF_LEN characters that it will buffer the full
282 * HVCS_BUFF_LEN characters if need be. This is essential for opost
283 * writes since they do not do high level buffering and expect to be
284 * able to send what the driver commits to sending buffering
285 * [e.g. tab to space conversions in n_tty.c opost()].
286 */
287 char buffer[HVCS_BUFF_LEN];
288 int chars_in_buffer;
289
290 /*
Jiri Slaby2cd9fa22012-04-02 13:54:25 +0200291 * Any variable below is valid before a tty is connected and
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292 * stays valid after the tty is disconnected. These shouldn't be
Paul Bolle46a1ca72011-02-15 00:05:01 +0100293 * whacked until the kobject refcount reaches zero though some entries
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294 * may be changed via sysfs initiatives.
295 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296 int connected; /* is the vty-server currently connected to a vty? */
297 uint32_t p_unit_address; /* partner unit address */
298 uint32_t p_partition_ID; /* partner partition ID */
299 char p_location_code[HVCS_CLC_LENGTH + 1]; /* CLC + Null Term */
300 struct list_head next; /* list management */
301 struct vio_dev *vdev;
302};
303
Denis Chengbed97592008-02-06 01:37:35 -0800304static LIST_HEAD(hvcs_structs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305static DEFINE_SPINLOCK(hvcs_structs_lock);
Benjamin Herrenschmidtc7704d32011-02-06 18:26:25 +0000306static DEFINE_MUTEX(hvcs_init_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307
308static void hvcs_unthrottle(struct tty_struct *tty);
309static void hvcs_throttle(struct tty_struct *tty);
David Howells7d12e782006-10-05 14:55:46 +0100310static irqreturn_t hvcs_handle_interrupt(int irq, void *dev_instance);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311
312static int hvcs_write(struct tty_struct *tty,
313 const unsigned char *buf, int count);
314static int hvcs_write_room(struct tty_struct *tty);
315static int hvcs_chars_in_buffer(struct tty_struct *tty);
316
317static int hvcs_has_pi(struct hvcs_struct *hvcsd);
318static void hvcs_set_pi(struct hvcs_partner_info *pi,
319 struct hvcs_struct *hvcsd);
320static int hvcs_get_pi(struct hvcs_struct *hvcsd);
321static int hvcs_rescan_devices_list(void);
322
323static int hvcs_partner_connect(struct hvcs_struct *hvcsd);
324static void hvcs_partner_free(struct hvcs_struct *hvcsd);
325
326static int hvcs_enable_device(struct hvcs_struct *hvcsd,
327 uint32_t unit_address, unsigned int irq, struct vio_dev *dev);
328
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329static int hvcs_open(struct tty_struct *tty, struct file *filp);
330static void hvcs_close(struct tty_struct *tty, struct file *filp);
331static void hvcs_hangup(struct tty_struct * tty);
332
Bill Pemberton9671f092012-11-19 13:21:50 -0500333static int hvcs_probe(struct vio_dev *dev,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334 const struct vio_device_id *id);
Bill Pembertonae8d8a12012-11-19 13:26:18 -0500335static int hvcs_remove(struct vio_dev *dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336static int __init hvcs_module_init(void);
337static void __exit hvcs_module_exit(void);
Bill Pemberton9671f092012-11-19 13:21:50 -0500338static int hvcs_initialize(void);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339
340#define HVCS_SCHED_READ 0x00000001
341#define HVCS_QUICK_READ 0x00000002
342#define HVCS_TRY_WRITE 0x00000004
343#define HVCS_READ_MASK (HVCS_SCHED_READ | HVCS_QUICK_READ)
344
Linas Vepstasc15bb292006-12-06 20:38:07 -0800345static inline struct hvcs_struct *from_vio_dev(struct vio_dev *viod)
346{
Greg Kroah-Hartman317ccc42009-05-04 12:40:54 -0700347 return dev_get_drvdata(&viod->dev);
Linas Vepstasc15bb292006-12-06 20:38:07 -0800348}
349/* The sysfs interface for the driver and devices */
350
351static ssize_t hvcs_partner_vtys_show(struct device *dev, struct device_attribute *attr, char *buf)
352{
353 struct vio_dev *viod = to_vio_dev(dev);
354 struct hvcs_struct *hvcsd = from_vio_dev(viod);
355 unsigned long flags;
356 int retval;
357
358 spin_lock_irqsave(&hvcsd->lock, flags);
359 retval = sprintf(buf, "%X\n", hvcsd->p_unit_address);
360 spin_unlock_irqrestore(&hvcsd->lock, flags);
361 return retval;
362}
363static DEVICE_ATTR(partner_vtys, S_IRUGO, hvcs_partner_vtys_show, NULL);
364
365static ssize_t hvcs_partner_clcs_show(struct device *dev, struct device_attribute *attr, char *buf)
366{
367 struct vio_dev *viod = to_vio_dev(dev);
368 struct hvcs_struct *hvcsd = from_vio_dev(viod);
369 unsigned long flags;
370 int retval;
371
372 spin_lock_irqsave(&hvcsd->lock, flags);
373 retval = sprintf(buf, "%s\n", &hvcsd->p_location_code[0]);
374 spin_unlock_irqrestore(&hvcsd->lock, flags);
375 return retval;
376}
377static DEVICE_ATTR(partner_clcs, S_IRUGO, hvcs_partner_clcs_show, NULL);
378
379static ssize_t hvcs_current_vty_store(struct device *dev, struct device_attribute *attr, const char * buf,
380 size_t count)
381{
382 /*
383 * Don't need this feature at the present time because firmware doesn't
384 * yet support multiple partners.
385 */
386 printk(KERN_INFO "HVCS: Denied current_vty change: -EPERM.\n");
387 return -EPERM;
388}
389
390static ssize_t hvcs_current_vty_show(struct device *dev, struct device_attribute *attr, char *buf)
391{
392 struct vio_dev *viod = to_vio_dev(dev);
393 struct hvcs_struct *hvcsd = from_vio_dev(viod);
394 unsigned long flags;
395 int retval;
396
397 spin_lock_irqsave(&hvcsd->lock, flags);
398 retval = sprintf(buf, "%s\n", &hvcsd->p_location_code[0]);
399 spin_unlock_irqrestore(&hvcsd->lock, flags);
400 return retval;
401}
402
403static DEVICE_ATTR(current_vty,
404 S_IRUGO | S_IWUSR, hvcs_current_vty_show, hvcs_current_vty_store);
405
406static ssize_t hvcs_vterm_state_store(struct device *dev, struct device_attribute *attr, const char *buf,
407 size_t count)
408{
409 struct vio_dev *viod = to_vio_dev(dev);
410 struct hvcs_struct *hvcsd = from_vio_dev(viod);
411 unsigned long flags;
412
413 /* writing a '0' to this sysfs entry will result in the disconnect. */
414 if (simple_strtol(buf, NULL, 0) != 0)
415 return -EINVAL;
416
417 spin_lock_irqsave(&hvcsd->lock, flags);
418
Jiri Slaby1997cf02012-04-02 13:54:24 +0200419 if (hvcsd->port.count > 0) {
Linas Vepstasc15bb292006-12-06 20:38:07 -0800420 spin_unlock_irqrestore(&hvcsd->lock, flags);
421 printk(KERN_INFO "HVCS: vterm state unchanged. "
422 "The hvcs device node is still in use.\n");
423 return -EPERM;
424 }
425
426 if (hvcsd->connected == 0) {
427 spin_unlock_irqrestore(&hvcsd->lock, flags);
428 printk(KERN_INFO "HVCS: vterm state unchanged. The"
429 " vty-server is not connected to a vty.\n");
430 return -EPERM;
431 }
432
433 hvcs_partner_free(hvcsd);
434 printk(KERN_INFO "HVCS: Closed vty-server@%X and"
435 " partner vty@%X:%d connection.\n",
436 hvcsd->vdev->unit_address,
437 hvcsd->p_unit_address,
438 (uint32_t)hvcsd->p_partition_ID);
439
440 spin_unlock_irqrestore(&hvcsd->lock, flags);
441 return count;
442}
443
444static ssize_t hvcs_vterm_state_show(struct device *dev, struct device_attribute *attr, char *buf)
445{
446 struct vio_dev *viod = to_vio_dev(dev);
447 struct hvcs_struct *hvcsd = from_vio_dev(viod);
448 unsigned long flags;
449 int retval;
450
451 spin_lock_irqsave(&hvcsd->lock, flags);
452 retval = sprintf(buf, "%d\n", hvcsd->connected);
453 spin_unlock_irqrestore(&hvcsd->lock, flags);
454 return retval;
455}
456static DEVICE_ATTR(vterm_state, S_IRUGO | S_IWUSR,
457 hvcs_vterm_state_show, hvcs_vterm_state_store);
458
459static ssize_t hvcs_index_show(struct device *dev, struct device_attribute *attr, char *buf)
460{
461 struct vio_dev *viod = to_vio_dev(dev);
462 struct hvcs_struct *hvcsd = from_vio_dev(viod);
463 unsigned long flags;
464 int retval;
465
466 spin_lock_irqsave(&hvcsd->lock, flags);
467 retval = sprintf(buf, "%d\n", hvcsd->index);
468 spin_unlock_irqrestore(&hvcsd->lock, flags);
469 return retval;
470}
471
472static DEVICE_ATTR(index, S_IRUGO, hvcs_index_show, NULL);
473
474static struct attribute *hvcs_attrs[] = {
475 &dev_attr_partner_vtys.attr,
476 &dev_attr_partner_clcs.attr,
477 &dev_attr_current_vty.attr,
478 &dev_attr_vterm_state.attr,
479 &dev_attr_index.attr,
480 NULL,
481};
482
483static struct attribute_group hvcs_attr_group = {
484 .attrs = hvcs_attrs,
485};
486
Greg Kroah-Hartman4e70a6f2017-06-09 11:03:07 +0200487static ssize_t rescan_show(struct device_driver *ddp, char *buf)
Linas Vepstasc15bb292006-12-06 20:38:07 -0800488{
489 /* A 1 means it is updating, a 0 means it is done updating */
490 return snprintf(buf, PAGE_SIZE, "%d\n", hvcs_rescan_status);
491}
492
Greg Kroah-Hartman4e70a6f2017-06-09 11:03:07 +0200493static ssize_t rescan_store(struct device_driver *ddp, const char * buf,
Linas Vepstasc15bb292006-12-06 20:38:07 -0800494 size_t count)
495{
496 if ((simple_strtol(buf, NULL, 0) != 1)
497 && (hvcs_rescan_status != 0))
498 return -EINVAL;
499
500 hvcs_rescan_status = 1;
501 printk(KERN_INFO "HVCS: rescanning partner info for all"
502 " vty-servers.\n");
503 hvcs_rescan_devices_list();
504 hvcs_rescan_status = 0;
505 return count;
506}
507
Greg Kroah-Hartman4e70a6f2017-06-09 11:03:07 +0200508static DRIVER_ATTR_RW(rescan);
Linas Vepstasc15bb292006-12-06 20:38:07 -0800509
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510static void hvcs_kick(void)
511{
512 hvcs_kicked = 1;
513 wmb();
514 wake_up_process(hvcs_task);
515}
516
517static void hvcs_unthrottle(struct tty_struct *tty)
518{
519 struct hvcs_struct *hvcsd = tty->driver_data;
520 unsigned long flags;
521
522 spin_lock_irqsave(&hvcsd->lock, flags);
523 hvcsd->todo_mask |= HVCS_SCHED_READ;
524 spin_unlock_irqrestore(&hvcsd->lock, flags);
525 hvcs_kick();
526}
527
528static void hvcs_throttle(struct tty_struct *tty)
529{
530 struct hvcs_struct *hvcsd = tty->driver_data;
531 unsigned long flags;
532
533 spin_lock_irqsave(&hvcsd->lock, flags);
534 vio_disable_interrupts(hvcsd->vdev);
535 spin_unlock_irqrestore(&hvcsd->lock, flags);
536}
537
538/*
539 * If the device is being removed we don't have to worry about this interrupt
540 * handler taking any further interrupts because they are disabled which means
541 * the hvcs_struct will always be valid in this handler.
542 */
David Howells7d12e782006-10-05 14:55:46 +0100543static irqreturn_t hvcs_handle_interrupt(int irq, void *dev_instance)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544{
545 struct hvcs_struct *hvcsd = dev_instance;
546
547 spin_lock(&hvcsd->lock);
548 vio_disable_interrupts(hvcsd->vdev);
549 hvcsd->todo_mask |= HVCS_SCHED_READ;
550 spin_unlock(&hvcsd->lock);
551 hvcs_kick();
552
553 return IRQ_HANDLED;
554}
555
556/* This function must be called with the hvcsd->lock held */
557static void hvcs_try_write(struct hvcs_struct *hvcsd)
558{
559 uint32_t unit_address = hvcsd->vdev->unit_address;
Jiri Slaby6968a752012-04-02 13:54:26 +0200560 struct tty_struct *tty = hvcsd->port.tty;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561 int sent;
562
563 if (hvcsd->todo_mask & HVCS_TRY_WRITE) {
564 /* won't send partial writes */
565 sent = hvc_put_chars(unit_address,
566 &hvcsd->buffer[0],
567 hvcsd->chars_in_buffer );
568 if (sent > 0) {
569 hvcsd->chars_in_buffer = 0;
570 /* wmb(); */
571 hvcsd->todo_mask &= ~(HVCS_TRY_WRITE);
572 /* wmb(); */
573
574 /*
575 * We are still obligated to deliver the data to the
576 * hypervisor even if the tty has been closed because
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300577 * we committed to delivering it. But don't try to wake
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578 * a non-existent tty.
579 */
580 if (tty) {
581 tty_wakeup(tty);
582 }
583 }
584 }
585}
586
587static int hvcs_io(struct hvcs_struct *hvcsd)
588{
589 uint32_t unit_address;
590 struct tty_struct *tty;
591 char buf[HVCS_BUFF_LEN] __ALIGNED__;
592 unsigned long flags;
593 int got = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594
595 spin_lock_irqsave(&hvcsd->lock, flags);
596
597 unit_address = hvcsd->vdev->unit_address;
Jiri Slaby6968a752012-04-02 13:54:26 +0200598 tty = hvcsd->port.tty;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599
600 hvcs_try_write(hvcsd);
601
Peter Hurley97ef38b2016-04-09 17:11:36 -0700602 if (!tty || tty_throttled(tty)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603 hvcsd->todo_mask &= ~(HVCS_READ_MASK);
604 goto bail;
605 } else if (!(hvcsd->todo_mask & (HVCS_READ_MASK)))
606 goto bail;
607
608 /* remove the read masks */
609 hvcsd->todo_mask &= ~(HVCS_READ_MASK);
610
Jiri Slaby227434f2013-01-03 15:53:01 +0100611 if (tty_buffer_request_room(&hvcsd->port, HVCS_BUFF_LEN) >= HVCS_BUFF_LEN) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612 got = hvc_get_chars(unit_address,
613 &buf[0],
614 HVCS_BUFF_LEN);
Jiri Slaby05c7cd32013-01-03 15:53:04 +0100615 tty_insert_flip_string(&hvcsd->port, buf, got);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616 }
617
618 /* Give the TTY time to process the data we just sent. */
619 if (got)
620 hvcsd->todo_mask |= HVCS_QUICK_READ;
621
622 spin_unlock_irqrestore(&hvcsd->lock, flags);
Alan Cox33f0f882006-01-09 20:54:13 -0800623 /* This is synch because tty->low_latency == 1 */
624 if(got)
Jiri Slaby2e124b42013-01-03 15:53:06 +0100625 tty_flip_buffer_push(&hvcsd->port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626
627 if (!got) {
628 /* Do this _after_ the flip_buffer_push */
629 spin_lock_irqsave(&hvcsd->lock, flags);
630 vio_enable_interrupts(hvcsd->vdev);
631 spin_unlock_irqrestore(&hvcsd->lock, flags);
632 }
633
634 return hvcsd->todo_mask;
635
636 bail:
637 spin_unlock_irqrestore(&hvcsd->lock, flags);
638 return hvcsd->todo_mask;
639}
640
641static int khvcsd(void *unused)
642{
643 struct hvcs_struct *hvcsd;
644 int hvcs_todo_mask;
645
646 __set_current_state(TASK_RUNNING);
647
648 do {
649 hvcs_todo_mask = 0;
650 hvcs_kicked = 0;
651 wmb();
652
653 spin_lock(&hvcs_structs_lock);
654 list_for_each_entry(hvcsd, &hvcs_structs, next) {
655 hvcs_todo_mask |= hvcs_io(hvcsd);
656 }
657 spin_unlock(&hvcs_structs_lock);
658
659 /*
660 * If any of the hvcs adapters want to try a write or quick read
661 * don't schedule(), yield a smidgen then execute the hvcs_io
662 * thread again for those that want the write.
663 */
664 if (hvcs_todo_mask & (HVCS_TRY_WRITE | HVCS_QUICK_READ)) {
665 yield();
666 continue;
667 }
668
669 set_current_state(TASK_INTERRUPTIBLE);
670 if (!hvcs_kicked)
671 schedule();
672 __set_current_state(TASK_RUNNING);
673 } while (!kthread_should_stop());
674
675 return 0;
676}
677
Arvind Yadava846c3e2017-08-17 19:04:23 +0530678static const struct vio_device_id hvcs_driver_table[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679 {"serial-server", "hvterm2"},
Stephen Rothwellfb120da2005-08-17 16:42:59 +1000680 { "", "" }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681};
682MODULE_DEVICE_TABLE(vio, hvcs_driver_table);
683
684static void hvcs_return_index(int index)
685{
686 /* Paranoia check */
687 if (!hvcs_index_list)
688 return;
689 if (index < 0 || index >= hvcs_index_count)
690 return;
691 if (hvcs_index_list[index] == -1)
692 return;
693 else
694 hvcs_index_list[index] = -1;
695}
696
Jiri Slaby2cd9fa22012-04-02 13:54:25 +0200697static void hvcs_destruct_port(struct tty_port *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698{
Jiri Slaby2cd9fa22012-04-02 13:54:25 +0200699 struct hvcs_struct *hvcsd = container_of(p, struct hvcs_struct, port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700 struct vio_dev *vdev;
701 unsigned long flags;
702
703 spin_lock(&hvcs_structs_lock);
704 spin_lock_irqsave(&hvcsd->lock, flags);
705
706 /* the list_del poisons the pointers */
707 list_del(&(hvcsd->next));
708
709 if (hvcsd->connected == 1) {
710 hvcs_partner_free(hvcsd);
711 printk(KERN_INFO "HVCS: Closed vty-server@%X and"
712 " partner vty@%X:%d connection.\n",
713 hvcsd->vdev->unit_address,
714 hvcsd->p_unit_address,
715 (uint32_t)hvcsd->p_partition_ID);
716 }
717 printk(KERN_INFO "HVCS: Destroyed hvcs_struct for vty-server@%X.\n",
718 hvcsd->vdev->unit_address);
719
720 vdev = hvcsd->vdev;
721 hvcsd->vdev = NULL;
722
723 hvcsd->p_unit_address = 0;
724 hvcsd->p_partition_ID = 0;
725 hvcs_return_index(hvcsd->index);
726 memset(&hvcsd->p_location_code[0], 0x00, HVCS_CLC_LENGTH + 1);
727
728 spin_unlock_irqrestore(&hvcsd->lock, flags);
729 spin_unlock(&hvcs_structs_lock);
730
Linas Vepstasc15bb292006-12-06 20:38:07 -0800731 sysfs_remove_group(&vdev->dev.kobj, &hvcs_attr_group);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732
733 kfree(hvcsd);
734}
735
Jiri Slaby2cd9fa22012-04-02 13:54:25 +0200736static const struct tty_port_operations hvcs_port_ops = {
737 .destruct = hvcs_destruct_port,
738};
739
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740static int hvcs_get_index(void)
741{
742 int i;
743 /* Paranoia check */
744 if (!hvcs_index_list) {
745 printk(KERN_ERR "HVCS: hvcs_index_list NOT valid!.\n");
746 return -EFAULT;
747 }
748 /* Find the numerically lowest first free index. */
749 for(i = 0; i < hvcs_index_count; i++) {
750 if (hvcs_index_list[i] == -1) {
751 hvcs_index_list[i] = 0;
752 return i;
753 }
754 }
755 return -1;
756}
757
Bill Pemberton9671f092012-11-19 13:21:50 -0500758static int hvcs_probe(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759 struct vio_dev *dev,
760 const struct vio_device_id *id)
761{
762 struct hvcs_struct *hvcsd;
Benjamin Herrenschmidtc7704d32011-02-06 18:26:25 +0000763 int index, rc;
Linas Vepstasc15bb292006-12-06 20:38:07 -0800764 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765
766 if (!dev || !id) {
767 printk(KERN_ERR "HVCS: probed with invalid parameter.\n");
768 return -EPERM;
769 }
770
Benjamin Herrenschmidtc7704d32011-02-06 18:26:25 +0000771 /* Make sure we are properly initialized */
772 rc = hvcs_initialize();
773 if (rc) {
774 pr_err("HVCS: Failed to initialize core driver.\n");
775 return rc;
776 }
777
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778 /* early to avoid cleanup on failure */
779 index = hvcs_get_index();
780 if (index < 0) {
781 return -EFAULT;
782 }
783
Yoann Padioleaudd00cc42007-07-19 01:49:03 -0700784 hvcsd = kzalloc(sizeof(*hvcsd), GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785 if (!hvcsd)
786 return -ENODEV;
787
Jiri Slaby1997cf02012-04-02 13:54:24 +0200788 tty_port_init(&hvcsd->port);
Jiri Slaby2cd9fa22012-04-02 13:54:25 +0200789 hvcsd->port.ops = &hvcs_port_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790 spin_lock_init(&hvcsd->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791
792 hvcsd->vdev = dev;
Greg Kroah-Hartman317ccc42009-05-04 12:40:54 -0700793 dev_set_drvdata(&dev->dev, hvcsd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794
795 hvcsd->index = index;
796
797 /* hvcsd->index = ++hvcs_struct_count; */
798 hvcsd->chars_in_buffer = 0;
799 hvcsd->todo_mask = 0;
800 hvcsd->connected = 0;
801
802 /*
803 * This will populate the hvcs_struct's partner info fields for the
804 * first time.
805 */
806 if (hvcs_get_pi(hvcsd)) {
807 printk(KERN_ERR "HVCS: Failed to fetch partner"
808 " info for vty-server@%X on device probe.\n",
809 hvcsd->vdev->unit_address);
810 }
811
812 /*
813 * If a user app opens a tty that corresponds to this vty-server before
814 * the hvcs_struct has been added to the devices list then the user app
815 * will get -ENODEV.
816 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817 spin_lock(&hvcs_structs_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818 list_add_tail(&(hvcsd->next), &hvcs_structs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819 spin_unlock(&hvcs_structs_lock);
820
Linas Vepstasc15bb292006-12-06 20:38:07 -0800821 retval = sysfs_create_group(&dev->dev.kobj, &hvcs_attr_group);
822 if (retval) {
823 printk(KERN_ERR "HVCS: Can't create sysfs attrs for vty-server@%X\n",
824 hvcsd->vdev->unit_address);
825 return retval;
826 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827
828 printk(KERN_INFO "HVCS: vty-server@%X added to the vio bus.\n", dev->unit_address);
829
830 /*
831 * DON'T enable interrupts here because there is no user to receive the
832 * data.
833 */
834 return 0;
835}
836
Bill Pembertonae8d8a12012-11-19 13:26:18 -0500837static int hvcs_remove(struct vio_dev *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838{
Greg Kroah-Hartman317ccc42009-05-04 12:40:54 -0700839 struct hvcs_struct *hvcsd = dev_get_drvdata(&dev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841 struct tty_struct *tty;
842
843 if (!hvcsd)
844 return -ENODEV;
845
Joe Perches8dfba4d2008-02-03 17:11:42 +0200846 /* By this time the vty-server won't be getting any more interrupts */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847
848 spin_lock_irqsave(&hvcsd->lock, flags);
849
Jiri Slaby6968a752012-04-02 13:54:26 +0200850 tty = hvcsd->port.tty;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852 spin_unlock_irqrestore(&hvcsd->lock, flags);
853
854 /*
855 * Let the last holder of this object cause it to be removed, which
856 * would probably be tty_hangup below.
857 */
Jiri Slaby2cd9fa22012-04-02 13:54:25 +0200858 tty_port_put(&hvcsd->port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700859
860 /*
861 * The hangup is a scheduled function which will auto chain call
862 * hvcs_hangup. The tty should always be valid at this time unless a
863 * simultaneous tty close already cleaned up the hvcs_struct.
864 */
865 if (tty)
866 tty_hangup(tty);
867
868 printk(KERN_INFO "HVCS: vty-server@%X removed from the"
869 " vio bus.\n", dev->unit_address);
870 return 0;
871};
872
873static struct vio_driver hvcs_vio_driver = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874 .id_table = hvcs_driver_table,
875 .probe = hvcs_probe,
Bill Pemberton91116cb2012-11-19 13:21:06 -0500876 .remove = hvcs_remove,
Benjamin Herrenschmidtcb52d892012-03-26 19:06:30 +0000877 .name = hvcs_driver_name,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878};
879
880/* Only called from hvcs_get_pi please */
881static void hvcs_set_pi(struct hvcs_partner_info *pi, struct hvcs_struct *hvcsd)
882{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883 hvcsd->p_unit_address = pi->unit_address;
884 hvcsd->p_partition_ID = pi->partition_ID;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700885
886 /* copy the null-term char too */
Chen Gang9276dfd2013-02-25 17:43:25 +0000887 strlcpy(&hvcsd->p_location_code[0],
888 &pi->location_code[0], sizeof(hvcsd->p_location_code));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889}
890
891/*
892 * Traverse the list and add the partner info that is found to the hvcs_struct
893 * struct entry. NOTE: At this time I know that partner info will return a
894 * single entry but in the future there may be multiple partner info entries per
895 * vty-server and you'll want to zero out that list and reset it. If for some
896 * reason you have an old version of this driver but there IS more than one
897 * partner info then hvcsd->p_* will hold the last partner info data from the
898 * firmware query. A good way to update this code would be to replace the three
899 * partner info fields in hvcs_struct with a list of hvcs_partner_info
900 * instances.
901 *
902 * This function must be called with the hvcsd->lock held.
903 */
904static int hvcs_get_pi(struct hvcs_struct *hvcsd)
905{
906 struct hvcs_partner_info *pi;
907 uint32_t unit_address = hvcsd->vdev->unit_address;
908 struct list_head head;
909 int retval;
910
911 spin_lock(&hvcs_pi_lock);
912 if (!hvcs_pi_buff) {
913 spin_unlock(&hvcs_pi_lock);
914 return -EFAULT;
915 }
916 retval = hvcs_get_partner_info(unit_address, &head, hvcs_pi_buff);
917 spin_unlock(&hvcs_pi_lock);
918 if (retval) {
919 printk(KERN_ERR "HVCS: Failed to fetch partner"
920 " info for vty-server@%x.\n", unit_address);
921 return retval;
922 }
923
924 /* nixes the values if the partner vty went away */
925 hvcsd->p_unit_address = 0;
926 hvcsd->p_partition_ID = 0;
927
928 list_for_each_entry(pi, &head, node)
929 hvcs_set_pi(pi, hvcsd);
930
931 hvcs_free_partner_info(&head);
932 return 0;
933}
934
935/*
936 * This function is executed by the driver "rescan" sysfs entry. It shouldn't
937 * be executed elsewhere, in order to prevent deadlock issues.
938 */
939static int hvcs_rescan_devices_list(void)
940{
941 struct hvcs_struct *hvcsd;
942 unsigned long flags;
943
944 spin_lock(&hvcs_structs_lock);
945
946 list_for_each_entry(hvcsd, &hvcs_structs, next) {
947 spin_lock_irqsave(&hvcsd->lock, flags);
948 hvcs_get_pi(hvcsd);
949 spin_unlock_irqrestore(&hvcsd->lock, flags);
950 }
951
952 spin_unlock(&hvcs_structs_lock);
953
954 return 0;
955}
956
957/*
958 * Farm this off into its own function because it could be more complex once
959 * multiple partners support is added. This function should be called with
960 * the hvcsd->lock held.
961 */
962static int hvcs_has_pi(struct hvcs_struct *hvcsd)
963{
964 if ((!hvcsd->p_unit_address) || (!hvcsd->p_partition_ID))
965 return 0;
966 return 1;
967}
968
969/*
970 * NOTE: It is possible that the super admin removed a partner vty and then
971 * added a different vty as the new partner.
972 *
973 * This function must be called with the hvcsd->lock held.
974 */
975static int hvcs_partner_connect(struct hvcs_struct *hvcsd)
976{
977 int retval;
978 unsigned int unit_address = hvcsd->vdev->unit_address;
979
980 /*
981 * If there wasn't any pi when the device was added it doesn't meant
982 * there isn't any now. This driver isn't notified when a new partner
983 * vty is added to a vty-server so we discover changes on our own.
984 * Please see comments in hvcs_register_connection() for justification
985 * of this bizarre code.
986 */
987 retval = hvcs_register_connection(unit_address,
988 hvcsd->p_partition_ID,
989 hvcsd->p_unit_address);
990 if (!retval) {
991 hvcsd->connected = 1;
992 return 0;
993 } else if (retval != -EINVAL)
994 return retval;
995
996 /*
997 * As per the spec re-get the pi and try again if -EINVAL after the
998 * first connection attempt.
999 */
1000 if (hvcs_get_pi(hvcsd))
1001 return -ENOMEM;
1002
1003 if (!hvcs_has_pi(hvcsd))
1004 return -ENODEV;
1005
1006 retval = hvcs_register_connection(unit_address,
1007 hvcsd->p_partition_ID,
1008 hvcsd->p_unit_address);
1009 if (retval != -EINVAL) {
1010 hvcsd->connected = 1;
1011 return retval;
1012 }
1013
1014 /*
1015 * EBUSY is the most likely scenario though the vty could have been
1016 * removed or there really could be an hcall error due to the parameter
1017 * data but thanks to ambiguous firmware return codes we can't really
1018 * tell.
1019 */
1020 printk(KERN_INFO "HVCS: vty-server or partner"
1021 " vty is busy. Try again later.\n");
1022 return -EBUSY;
1023}
1024
1025/* This function must be called with the hvcsd->lock held */
1026static void hvcs_partner_free(struct hvcs_struct *hvcsd)
1027{
1028 int retval;
1029 do {
1030 retval = hvcs_free_connection(hvcsd->vdev->unit_address);
1031 } while (retval == -EBUSY);
1032 hvcsd->connected = 0;
1033}
1034
1035/* This helper function must be called WITHOUT the hvcsd->lock held */
1036static int hvcs_enable_device(struct hvcs_struct *hvcsd, uint32_t unit_address,
1037 unsigned int irq, struct vio_dev *vdev)
1038{
1039 unsigned long flags;
1040 int rc;
1041
1042 /*
1043 * It is possible that the vty-server was removed between the time that
1044 * the conn was registered and now.
1045 */
Greg Kroah-Hartman873b4f12015-04-30 11:22:09 +02001046 rc = request_irq(irq, &hvcs_handle_interrupt, 0, "ibmhvcs", hvcsd);
1047 if (!rc) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001048 /*
1049 * It is possible the vty-server was removed after the irq was
1050 * requested but before we have time to enable interrupts.
1051 */
Segher Boessenkool706c8c92006-03-30 14:49:40 +02001052 if (vio_enable_interrupts(vdev) == H_SUCCESS)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001053 return 0;
1054 else {
1055 printk(KERN_ERR "HVCS: int enable failed for"
1056 " vty-server@%X.\n", unit_address);
1057 free_irq(irq, hvcsd);
1058 }
1059 } else
1060 printk(KERN_ERR "HVCS: irq req failed for"
1061 " vty-server@%X.\n", unit_address);
1062
1063 spin_lock_irqsave(&hvcsd->lock, flags);
1064 hvcs_partner_free(hvcsd);
1065 spin_unlock_irqrestore(&hvcsd->lock, flags);
1066
1067 return rc;
1068
1069}
1070
1071/*
Greg Kroah-Hartmanf7eb12c2007-11-28 10:46:22 -08001072 * This always increments the kref ref count if the call is successful.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001073 * Please remember to dec when you are done with the instance.
1074 *
1075 * NOTICE: Do NOT hold either the hvcs_struct.lock or hvcs_structs_lock when
1076 * calling this function or you will get deadlock.
1077 */
Stephen Rothwellb1c30112007-07-22 00:25:00 +10001078static struct hvcs_struct *hvcs_get_by_index(int index)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001079{
Jiri Slaby410235f2012-03-05 14:52:01 +01001080 struct hvcs_struct *hvcsd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001081 unsigned long flags;
1082
1083 spin_lock(&hvcs_structs_lock);
Jiri Slaby410235f2012-03-05 14:52:01 +01001084 list_for_each_entry(hvcsd, &hvcs_structs, next) {
1085 spin_lock_irqsave(&hvcsd->lock, flags);
1086 if (hvcsd->index == index) {
Jiri Slaby2cd9fa22012-04-02 13:54:25 +02001087 tty_port_get(&hvcsd->port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001088 spin_unlock_irqrestore(&hvcsd->lock, flags);
Jiri Slaby410235f2012-03-05 14:52:01 +01001089 spin_unlock(&hvcs_structs_lock);
1090 return hvcsd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001091 }
Jiri Slaby410235f2012-03-05 14:52:01 +01001092 spin_unlock_irqrestore(&hvcsd->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001093 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001094 spin_unlock(&hvcs_structs_lock);
Jiri Slaby410235f2012-03-05 14:52:01 +01001095
1096 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001097}
1098
Jiri Slaby27bf7c42012-08-07 21:48:06 +02001099static int hvcs_install(struct tty_driver *driver, struct tty_struct *tty)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001100{
1101 struct hvcs_struct *hvcsd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001102 struct vio_dev *vdev;
Jiri Slaby97d15082012-08-07 21:48:05 +02001103 unsigned long unit_address, flags;
1104 unsigned int irq;
1105 int retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001106
Linus Torvalds1da177e2005-04-16 15:20:36 -07001107 /*
1108 * Is there a vty-server that shares the same index?
Greg Kroah-Hartmanf7eb12c2007-11-28 10:46:22 -08001109 * This function increments the kref index.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001110 */
Jiri Slaby97d15082012-08-07 21:48:05 +02001111 hvcsd = hvcs_get_by_index(tty->index);
1112 if (!hvcsd) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001113 printk(KERN_WARNING "HVCS: open failed, no device associated"
1114 " with tty->index %d.\n", tty->index);
1115 return -ENODEV;
1116 }
1117
1118 spin_lock_irqsave(&hvcsd->lock, flags);
1119
Jiri Slaby97d15082012-08-07 21:48:05 +02001120 if (hvcsd->connected == 0) {
1121 retval = hvcs_partner_connect(hvcsd);
1122 if (retval) {
1123 spin_unlock_irqrestore(&hvcsd->lock, flags);
1124 printk(KERN_WARNING "HVCS: partner connect failed.\n");
1125 goto err_put;
1126 }
1127 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001128
Jiri Slaby27bf7c42012-08-07 21:48:06 +02001129 hvcsd->port.count = 0;
Jiri Slaby6968a752012-04-02 13:54:26 +02001130 hvcsd->port.tty = tty;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001131 tty->driver_data = hvcsd;
1132
Linus Torvalds1da177e2005-04-16 15:20:36 -07001133 memset(&hvcsd->buffer[0], 0x00, HVCS_BUFF_LEN);
1134
1135 /*
1136 * Save these in the spinlock for the enable operations that need them
1137 * outside of the spinlock.
1138 */
1139 irq = hvcsd->vdev->irq;
1140 vdev = hvcsd->vdev;
1141 unit_address = hvcsd->vdev->unit_address;
1142
1143 hvcsd->todo_mask |= HVCS_SCHED_READ;
1144 spin_unlock_irqrestore(&hvcsd->lock, flags);
1145
1146 /*
1147 * This must be done outside of the spinlock because it requests irqs
1148 * and will grab the spinlock and free the connection if it fails.
1149 */
Jiri Slaby97d15082012-08-07 21:48:05 +02001150 retval = hvcs_enable_device(hvcsd, unit_address, irq, vdev);
1151 if (retval) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001152 printk(KERN_WARNING "HVCS: enable device failed.\n");
Jiri Slaby97d15082012-08-07 21:48:05 +02001153 goto err_put;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001154 }
1155
Jiri Slaby27bf7c42012-08-07 21:48:06 +02001156 retval = tty_port_install(&hvcsd->port, driver, tty);
1157 if (retval)
1158 goto err_irq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001159
Jiri Slaby27bf7c42012-08-07 21:48:06 +02001160 return 0;
1161err_irq:
1162 spin_lock_irqsave(&hvcsd->lock, flags);
1163 vio_disable_interrupts(hvcsd->vdev);
1164 spin_unlock_irqrestore(&hvcsd->lock, flags);
1165 free_irq(irq, hvcsd);
1166err_put:
1167 tty_port_put(&hvcsd->port);
1168
1169 return retval;
1170}
1171
1172/*
1173 * This is invoked via the tty_open interface when a user app connects to the
1174 * /dev node.
1175 */
1176static int hvcs_open(struct tty_struct *tty, struct file *filp)
1177{
1178 struct hvcs_struct *hvcsd = tty->driver_data;
1179 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001180
1181 spin_lock_irqsave(&hvcsd->lock, flags);
Jiri Slaby1997cf02012-04-02 13:54:24 +02001182 hvcsd->port.count++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001183 hvcsd->todo_mask |= HVCS_SCHED_READ;
1184 spin_unlock_irqrestore(&hvcsd->lock, flags);
Greg Kroah-Hartmanf7eb12c2007-11-28 10:46:22 -08001185
Linus Torvalds1da177e2005-04-16 15:20:36 -07001186 hvcs_kick();
1187
1188 printk(KERN_INFO "HVCS: vty-server@%X connection opened.\n",
1189 hvcsd->vdev->unit_address );
1190
1191 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001192}
1193
1194static void hvcs_close(struct tty_struct *tty, struct file *filp)
1195{
1196 struct hvcs_struct *hvcsd;
1197 unsigned long flags;
Alan Coxd4e33fa2012-01-26 17:44:09 +00001198 int irq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001199
1200 /*
1201 * Is someone trying to close the file associated with this device after
1202 * we have hung up? If so tty->driver_data wouldn't be valid.
1203 */
1204 if (tty_hung_up_p(filp))
1205 return;
1206
1207 /*
1208 * No driver_data means that this close was probably issued after a
1209 * failed hvcs_open by the tty layer's release_dev() api and we can just
1210 * exit cleanly.
1211 */
1212 if (!tty->driver_data)
1213 return;
1214
1215 hvcsd = tty->driver_data;
1216
1217 spin_lock_irqsave(&hvcsd->lock, flags);
Jiri Slaby1997cf02012-04-02 13:54:24 +02001218 if (--hvcsd->port.count == 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001219
1220 vio_disable_interrupts(hvcsd->vdev);
1221
1222 /*
1223 * NULL this early so that the kernel_thread doesn't try to
1224 * execute any operations on the TTY even though it is obligated
1225 * to deliver any pending I/O to the hypervisor.
1226 */
Jiri Slaby6968a752012-04-02 13:54:26 +02001227 hvcsd->port.tty = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001228
1229 irq = hvcsd->vdev->irq;
1230 spin_unlock_irqrestore(&hvcsd->lock, flags);
1231
Peter Hurley79c1faa2015-10-10 16:00:51 -04001232 tty_wait_until_sent(tty, HVCS_CLOSE_WAIT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001233
1234 /*
1235 * This line is important because it tells hvcs_open that this
1236 * device needs to be re-configured the next time hvcs_open is
1237 * called.
1238 */
1239 tty->driver_data = NULL;
1240
1241 free_irq(irq, hvcsd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001242 return;
Jiri Slaby1997cf02012-04-02 13:54:24 +02001243 } else if (hvcsd->port.count < 0) {
Colin Ian King228a9ff2017-04-22 13:35:33 +01001244 printk(KERN_ERR "HVCS: vty-server@%X open_count: %d is mismanaged.\n",
Jiri Slaby1997cf02012-04-02 13:54:24 +02001245 hvcsd->vdev->unit_address, hvcsd->port.count);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001246 }
1247
1248 spin_unlock_irqrestore(&hvcsd->lock, flags);
Jiri Slaby27bf7c42012-08-07 21:48:06 +02001249}
1250
1251static void hvcs_cleanup(struct tty_struct * tty)
1252{
1253 struct hvcs_struct *hvcsd = tty->driver_data;
1254
Jiri Slaby2cd9fa22012-04-02 13:54:25 +02001255 tty_port_put(&hvcsd->port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001256}
1257
1258static void hvcs_hangup(struct tty_struct * tty)
1259{
1260 struct hvcs_struct *hvcsd = tty->driver_data;
1261 unsigned long flags;
1262 int temp_open_count;
Alan Coxd4e33fa2012-01-26 17:44:09 +00001263 int irq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001264
1265 spin_lock_irqsave(&hvcsd->lock, flags);
Greg Kroah-Hartmanf7eb12c2007-11-28 10:46:22 -08001266 /* Preserve this so that we know how many kref refs to put */
Jiri Slaby1997cf02012-04-02 13:54:24 +02001267 temp_open_count = hvcsd->port.count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001268
1269 /*
Greg Kroah-Hartmanf7eb12c2007-11-28 10:46:22 -08001270 * Don't kref put inside the spinlock because the destruction
Linus Torvalds1da177e2005-04-16 15:20:36 -07001271 * callback may use the spinlock and it may get called before the
Greg Kroah-Hartmanf7eb12c2007-11-28 10:46:22 -08001272 * spinlock has been released.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001273 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001274 vio_disable_interrupts(hvcsd->vdev);
1275
1276 hvcsd->todo_mask = 0;
1277
1278 /* I don't think the tty needs the hvcs_struct pointer after a hangup */
Jiri Slaby6968a752012-04-02 13:54:26 +02001279 tty->driver_data = NULL;
1280 hvcsd->port.tty = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001281
Jiri Slaby1997cf02012-04-02 13:54:24 +02001282 hvcsd->port.count = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001283
1284 /* This will drop any buffered data on the floor which is OK in a hangup
1285 * scenario. */
1286 memset(&hvcsd->buffer[0], 0x00, HVCS_BUFF_LEN);
1287 hvcsd->chars_in_buffer = 0;
1288
1289 irq = hvcsd->vdev->irq;
1290
1291 spin_unlock_irqrestore(&hvcsd->lock, flags);
1292
1293 free_irq(irq, hvcsd);
1294
1295 /*
Greg Kroah-Hartmanf7eb12c2007-11-28 10:46:22 -08001296 * We need to kref_put() for every open_count we have since the
Linus Torvalds1da177e2005-04-16 15:20:36 -07001297 * tty_hangup() function doesn't invoke a close per open connection on a
1298 * non-console device.
1299 */
1300 while(temp_open_count) {
1301 --temp_open_count;
1302 /*
1303 * The final put will trigger destruction of the hvcs_struct.
1304 * NOTE: If this hangup was signaled from user space then the
1305 * final put will never happen.
1306 */
Jiri Slaby2cd9fa22012-04-02 13:54:25 +02001307 tty_port_put(&hvcsd->port);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001308 }
1309}
1310
1311/*
1312 * NOTE: This is almost always from_user since user level apps interact with the
1313 * /dev nodes. I'm trusting that if hvcs_write gets called and interrupted by
1314 * hvcs_remove (which removes the target device and executes tty_hangup()) that
1315 * tty_hangup will allow hvcs_write time to complete execution before it
1316 * terminates our device.
1317 */
1318static int hvcs_write(struct tty_struct *tty,
1319 const unsigned char *buf, int count)
1320{
1321 struct hvcs_struct *hvcsd = tty->driver_data;
1322 unsigned int unit_address;
1323 const unsigned char *charbuf;
1324 unsigned long flags;
1325 int total_sent = 0;
1326 int tosend = 0;
1327 int result = 0;
1328
1329 /*
1330 * If they don't check the return code off of their open they may
1331 * attempt this even if there is no connected device.
1332 */
1333 if (!hvcsd)
1334 return -ENODEV;
1335
1336 /* Reasonable size to prevent user level flooding */
1337 if (count > HVCS_MAX_FROM_USER) {
1338 printk(KERN_WARNING "HVCS write: count being truncated to"
1339 " HVCS_MAX_FROM_USER.\n");
1340 count = HVCS_MAX_FROM_USER;
1341 }
1342
1343 charbuf = buf;
1344
1345 spin_lock_irqsave(&hvcsd->lock, flags);
1346
1347 /*
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001348 * Somehow an open succeeded but the device was removed or the
Linus Torvalds1da177e2005-04-16 15:20:36 -07001349 * connection terminated between the vty-server and partner vty during
1350 * the middle of a write operation? This is a crummy place to do this
1351 * but we want to keep it all in the spinlock.
1352 */
Jiri Slaby1997cf02012-04-02 13:54:24 +02001353 if (hvcsd->port.count <= 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001354 spin_unlock_irqrestore(&hvcsd->lock, flags);
1355 return -ENODEV;
1356 }
1357
1358 unit_address = hvcsd->vdev->unit_address;
1359
1360 while (count > 0) {
1361 tosend = min(count, (HVCS_BUFF_LEN - hvcsd->chars_in_buffer));
1362 /*
1363 * No more space, this probably means that the last call to
1364 * hvcs_write() didn't succeed and the buffer was filled up.
1365 */
1366 if (!tosend)
1367 break;
1368
1369 memcpy(&hvcsd->buffer[hvcsd->chars_in_buffer],
1370 &charbuf[total_sent],
1371 tosend);
1372
1373 hvcsd->chars_in_buffer += tosend;
1374
1375 result = 0;
1376
1377 /*
1378 * If this is true then we don't want to try writing to the
1379 * hypervisor because that is the kernel_threads job now. We'll
1380 * just add to the buffer.
1381 */
1382 if (!(hvcsd->todo_mask & HVCS_TRY_WRITE))
1383 /* won't send partial writes */
1384 result = hvc_put_chars(unit_address,
1385 &hvcsd->buffer[0],
1386 hvcsd->chars_in_buffer);
1387
1388 /*
1389 * Since we know we have enough room in hvcsd->buffer for
1390 * tosend we record that it was sent regardless of whether the
1391 * hypervisor actually took it because we have it buffered.
1392 */
1393 total_sent+=tosend;
1394 count-=tosend;
1395 if (result == 0) {
1396 hvcsd->todo_mask |= HVCS_TRY_WRITE;
1397 hvcs_kick();
1398 break;
1399 }
1400
1401 hvcsd->chars_in_buffer = 0;
1402 /*
1403 * Test after the chars_in_buffer reset otherwise this could
1404 * deadlock our writes if hvc_put_chars fails.
1405 */
1406 if (result < 0)
1407 break;
1408 }
1409
1410 spin_unlock_irqrestore(&hvcsd->lock, flags);
1411
1412 if (result == -1)
1413 return -EIO;
1414 else
1415 return total_sent;
1416}
1417
1418/*
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001419 * This is really asking how much can we guarantee that we can send or that we
Linus Torvalds1da177e2005-04-16 15:20:36 -07001420 * absolutely WILL BUFFER if we can't send it. This driver MUST honor the
1421 * return value, hence the reason for hvcs_struct buffering.
1422 */
1423static int hvcs_write_room(struct tty_struct *tty)
1424{
1425 struct hvcs_struct *hvcsd = tty->driver_data;
1426
Jiri Slaby1997cf02012-04-02 13:54:24 +02001427 if (!hvcsd || hvcsd->port.count <= 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001428 return 0;
1429
1430 return HVCS_BUFF_LEN - hvcsd->chars_in_buffer;
1431}
1432
1433static int hvcs_chars_in_buffer(struct tty_struct *tty)
1434{
1435 struct hvcs_struct *hvcsd = tty->driver_data;
1436
1437 return hvcsd->chars_in_buffer;
1438}
1439
Jeff Dikeb68e31d2006-10-02 02:17:18 -07001440static const struct tty_operations hvcs_ops = {
Jiri Slaby27bf7c42012-08-07 21:48:06 +02001441 .install = hvcs_install,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001442 .open = hvcs_open,
1443 .close = hvcs_close,
Jiri Slaby27bf7c42012-08-07 21:48:06 +02001444 .cleanup = hvcs_cleanup,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001445 .hangup = hvcs_hangup,
1446 .write = hvcs_write,
1447 .write_room = hvcs_write_room,
1448 .chars_in_buffer = hvcs_chars_in_buffer,
1449 .unthrottle = hvcs_unthrottle,
1450 .throttle = hvcs_throttle,
1451};
1452
1453static int hvcs_alloc_index_list(int n)
1454{
1455 int i;
Jesper Juhl8f760782006-06-27 02:55:06 -07001456
Linus Torvalds1da177e2005-04-16 15:20:36 -07001457 hvcs_index_list = kmalloc(n * sizeof(hvcs_index_count),GFP_KERNEL);
1458 if (!hvcs_index_list)
1459 return -ENOMEM;
1460 hvcs_index_count = n;
Jesper Juhl8f760782006-06-27 02:55:06 -07001461 for (i = 0; i < hvcs_index_count; i++)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001462 hvcs_index_list[i] = -1;
1463 return 0;
1464}
1465
1466static void hvcs_free_index_list(void)
1467{
1468 /* Paranoia check to be thorough. */
Jesper Juhl8f760782006-06-27 02:55:06 -07001469 kfree(hvcs_index_list);
1470 hvcs_index_list = NULL;
1471 hvcs_index_count = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001472}
1473
Bill Pemberton9671f092012-11-19 13:21:50 -05001474static int hvcs_initialize(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001475{
Benjamin Herrenschmidtc7704d32011-02-06 18:26:25 +00001476 int rc, num_ttys_to_alloc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001477
Benjamin Herrenschmidtc7704d32011-02-06 18:26:25 +00001478 mutex_lock(&hvcs_init_mutex);
1479 if (hvcs_task) {
1480 mutex_unlock(&hvcs_init_mutex);
1481 return 0;
1482 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001483
1484 /* Has the user specified an overload with an insmod param? */
1485 if (hvcs_parm_num_devs <= 0 ||
1486 (hvcs_parm_num_devs > HVCS_MAX_SERVER_ADAPTERS)) {
1487 num_ttys_to_alloc = HVCS_DEFAULT_SERVER_ADAPTERS;
1488 } else
1489 num_ttys_to_alloc = hvcs_parm_num_devs;
1490
1491 hvcs_tty_driver = alloc_tty_driver(num_ttys_to_alloc);
Wei Yongjunad3d1e52012-10-22 12:42:59 +08001492 if (!hvcs_tty_driver) {
1493 mutex_unlock(&hvcs_init_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001494 return -ENOMEM;
Wei Yongjunad3d1e52012-10-22 12:42:59 +08001495 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001496
Linas Vepstasc15bb292006-12-06 20:38:07 -08001497 if (hvcs_alloc_index_list(num_ttys_to_alloc)) {
1498 rc = -ENOMEM;
1499 goto index_fail;
1500 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001501
Linus Torvalds1da177e2005-04-16 15:20:36 -07001502 hvcs_tty_driver->driver_name = hvcs_driver_name;
1503 hvcs_tty_driver->name = hvcs_device_node;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001504
1505 /*
1506 * We'll let the system assign us a major number, indicated by leaving
1507 * it blank.
1508 */
1509
1510 hvcs_tty_driver->minor_start = HVCS_MINOR_START;
1511 hvcs_tty_driver->type = TTY_DRIVER_TYPE_SYSTEM;
1512
1513 /*
1514 * We role our own so that we DONT ECHO. We can't echo because the
1515 * device we are connecting to already echoes by default and this would
1516 * throw us into a horrible recursive echo-echo-echo loop.
1517 */
1518 hvcs_tty_driver->init_termios = hvcs_tty_termios;
1519 hvcs_tty_driver->flags = TTY_DRIVER_REAL_RAW;
1520
1521 tty_set_operations(hvcs_tty_driver, &hvcs_ops);
1522
1523 /*
1524 * The following call will result in sysfs entries that denote the
1525 * dynamically assigned major and minor numbers for our devices.
1526 */
1527 if (tty_register_driver(hvcs_tty_driver)) {
Linas Vepstasc15bb292006-12-06 20:38:07 -08001528 printk(KERN_ERR "HVCS: registration as a tty driver failed.\n");
1529 rc = -EIO;
1530 goto register_fail;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001531 }
1532
Brian Kingac07a4a2011-09-13 11:22:51 +00001533 hvcs_pi_buff = (unsigned long *) __get_free_page(GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001534 if (!hvcs_pi_buff) {
Linas Vepstasc15bb292006-12-06 20:38:07 -08001535 rc = -ENOMEM;
1536 goto buff_alloc_fail;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001537 }
1538
1539 hvcs_task = kthread_run(khvcsd, NULL, "khvcsd");
1540 if (IS_ERR(hvcs_task)) {
Benjamin Herrenschmidtc7704d32011-02-06 18:26:25 +00001541 printk(KERN_ERR "HVCS: khvcsd creation failed.\n");
Linas Vepstasc15bb292006-12-06 20:38:07 -08001542 rc = -EIO;
1543 goto kthread_fail;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001544 }
Benjamin Herrenschmidtc7704d32011-02-06 18:26:25 +00001545 mutex_unlock(&hvcs_init_mutex);
Linas Vepstasc15bb292006-12-06 20:38:07 -08001546 return 0;
1547
Linas Vepstasc15bb292006-12-06 20:38:07 -08001548kthread_fail:
Brian Kingac07a4a2011-09-13 11:22:51 +00001549 free_page((unsigned long)hvcs_pi_buff);
Linas Vepstasc15bb292006-12-06 20:38:07 -08001550buff_alloc_fail:
1551 tty_unregister_driver(hvcs_tty_driver);
1552register_fail:
1553 hvcs_free_index_list();
1554index_fail:
1555 put_tty_driver(hvcs_tty_driver);
1556 hvcs_tty_driver = NULL;
Benjamin Herrenschmidtc7704d32011-02-06 18:26:25 +00001557 mutex_unlock(&hvcs_init_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001558 return rc;
1559}
1560
Benjamin Herrenschmidtc7704d32011-02-06 18:26:25 +00001561static int __init hvcs_module_init(void)
1562{
1563 int rc = vio_register_driver(&hvcs_vio_driver);
1564 if (rc) {
1565 printk(KERN_ERR "HVCS: can't register vio driver\n");
1566 return rc;
1567 }
1568
1569 pr_info("HVCS: Driver registered.\n");
1570
1571 /* This needs to be done AFTER the vio_register_driver() call or else
1572 * the kobjects won't be initialized properly.
1573 */
1574 rc = driver_create_file(&(hvcs_vio_driver.driver), &driver_attr_rescan);
1575 if (rc)
Joe Perchesd437fa92017-02-16 23:11:44 -08001576 pr_warn("HVCS: Failed to create rescan file (err %d)\n", rc);
Benjamin Herrenschmidtc7704d32011-02-06 18:26:25 +00001577
1578 return 0;
1579}
1580
Linus Torvalds1da177e2005-04-16 15:20:36 -07001581static void __exit hvcs_module_exit(void)
1582{
1583 /*
1584 * This driver receives hvcs_remove callbacks for each device upon
1585 * module removal.
1586 */
Benjamin Herrenschmidtc7704d32011-02-06 18:26:25 +00001587 vio_unregister_driver(&hvcs_vio_driver);
1588 if (!hvcs_task)
1589 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001590
1591 /*
1592 * This synchronous operation will wake the khvcsd kthread if it is
1593 * asleep and will return when khvcsd has terminated.
1594 */
1595 kthread_stop(hvcs_task);
1596
1597 spin_lock(&hvcs_pi_lock);
Brian Kingac07a4a2011-09-13 11:22:51 +00001598 free_page((unsigned long)hvcs_pi_buff);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001599 hvcs_pi_buff = NULL;
1600 spin_unlock(&hvcs_pi_lock);
1601
Linas Vepstasc15bb292006-12-06 20:38:07 -08001602 driver_remove_file(&hvcs_vio_driver.driver, &driver_attr_rescan);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001603
Linus Torvalds1da177e2005-04-16 15:20:36 -07001604 tty_unregister_driver(hvcs_tty_driver);
1605
1606 hvcs_free_index_list();
1607
1608 put_tty_driver(hvcs_tty_driver);
1609
1610 printk(KERN_INFO "HVCS: driver module removed.\n");
1611}
1612
1613module_init(hvcs_module_init);
1614module_exit(hvcs_module_exit);