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