Documentation cleanup: trivial misspelling, punctuation, and grammar corrections.
[linux-2.6] / drivers / char / hvc_iseries.c
1 /*
2  * iSeries vio driver interface to hvc_console.c
3  *
4  * This code is based heavily on hvc_vio.c and viocons.c
5  *
6  * Copyright (C) 2006 Stephen Rothwell, IBM Corporation
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2 of the License, or
11  * (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the Free Software
20  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
21  */
22 #include <stdarg.h>
23 #include <linux/types.h>
24 #include <linux/init.h>
25 #include <linux/kernel.h>
26 #include <linux/spinlock.h>
27 #include <linux/console.h>
28
29 #include <asm/hvconsole.h>
30 #include <asm/vio.h>
31 #include <asm/prom.h>
32 #include <asm/firmware.h>
33 #include <asm/iseries/vio.h>
34 #include <asm/iseries/hv_call.h>
35 #include <asm/iseries/hv_lp_config.h>
36 #include <asm/iseries/hv_lp_event.h>
37
38 #include "hvc_console.h"
39
40 #define VTTY_PORTS 10
41
42 static DEFINE_SPINLOCK(consolelock);
43 static DEFINE_SPINLOCK(consoleloglock);
44
45 static const char hvc_driver_name[] = "hvc_console";
46
47 #define IN_BUF_SIZE     200
48
49 /*
50  * Our port information.
51  */
52 static struct port_info {
53         HvLpIndex lp;
54         u64 seq;        /* sequence number of last HV send */
55         u64 ack;        /* last ack from HV */
56         struct hvc_struct *hp;
57         int in_start;
58         int in_end;
59         unsigned char in_buf[IN_BUF_SIZE];
60 } port_info[VTTY_PORTS] = {
61         [ 0 ... VTTY_PORTS - 1 ] = {
62                 .lp = HvLpIndexInvalid
63         }
64 };
65
66 #define viochar_is_console(pi)  ((pi) == &port_info[0])
67
68 static struct vio_device_id hvc_driver_table[] __devinitdata = {
69         {"serial", "IBM,iSeries-vty"},
70         { "", "" }
71 };
72 MODULE_DEVICE_TABLE(vio, hvc_driver_table);
73
74 static void hvlog(char *fmt, ...)
75 {
76         int i;
77         unsigned long flags;
78         va_list args;
79         static char buf[256];
80
81         spin_lock_irqsave(&consoleloglock, flags);
82         va_start(args, fmt);
83         i = vscnprintf(buf, sizeof(buf) - 1, fmt, args);
84         va_end(args);
85         buf[i++] = '\r';
86         HvCall_writeLogBuffer(buf, i);
87         spin_unlock_irqrestore(&consoleloglock, flags);
88 }
89
90 /*
91  * Initialize the common fields in a charLpEvent
92  */
93 static void init_data_event(struct viocharlpevent *viochar, HvLpIndex lp)
94 {
95         struct HvLpEvent *hev = &viochar->event;
96
97         memset(viochar, 0, sizeof(struct viocharlpevent));
98
99         hev->flags = HV_LP_EVENT_VALID | HV_LP_EVENT_DEFERRED_ACK |
100                 HV_LP_EVENT_INT;
101         hev->xType = HvLpEvent_Type_VirtualIo;
102         hev->xSubtype = viomajorsubtype_chario | viochardata;
103         hev->xSourceLp = HvLpConfig_getLpIndex();
104         hev->xTargetLp = lp;
105         hev->xSizeMinus1 = sizeof(struct viocharlpevent);
106         hev->xSourceInstanceId = viopath_sourceinst(lp);
107         hev->xTargetInstanceId = viopath_targetinst(lp);
108 }
109
110 static int get_chars(uint32_t vtermno, char *buf, int count)
111 {
112         struct port_info *pi;
113         int n = 0;
114         unsigned long flags;
115
116         if (vtermno >= VTTY_PORTS)
117                 return -EINVAL;
118         if (count == 0)
119                 return 0;
120
121         pi = &port_info[vtermno];
122         spin_lock_irqsave(&consolelock, flags);
123
124         if (pi->in_end == 0)
125                 goto done;
126
127         n = pi->in_end - pi->in_start;
128         if (n > count)
129                 n = count;
130         memcpy(buf, &pi->in_buf[pi->in_start], n);
131         pi->in_start += n;
132         if (pi->in_start == pi->in_end) {
133                 pi->in_start = 0;
134                 pi->in_end = 0;
135         }
136 done:
137         spin_unlock_irqrestore(&consolelock, flags);
138         return n;
139 }
140
141 static int put_chars(uint32_t vtermno, const char *buf, int count)
142 {
143         struct viocharlpevent *viochar;
144         struct port_info *pi;
145         HvLpEvent_Rc hvrc;
146         unsigned long flags;
147         int sent = 0;
148
149         if (vtermno >= VTTY_PORTS)
150                 return -EINVAL;
151
152         pi = &port_info[vtermno];
153
154         spin_lock_irqsave(&consolelock, flags);
155
156         if (viochar_is_console(pi) && !viopath_isactive(pi->lp)) {
157                 HvCall_writeLogBuffer(buf, count);
158                 sent = count;
159                 goto done;
160         }
161
162         viochar = vio_get_event_buffer(viomajorsubtype_chario);
163         if (viochar == NULL) {
164                 hvlog("\n\rviocons: Can't get viochar buffer.");
165                 goto done;
166         }
167
168         while ((count > 0) && ((pi->seq - pi->ack) < VIOCHAR_WINDOW)) {
169                 int len;
170
171                 len = (count > VIOCHAR_MAX_DATA) ? VIOCHAR_MAX_DATA : count;
172
173                 if (viochar_is_console(pi))
174                         HvCall_writeLogBuffer(buf, len);
175
176                 init_data_event(viochar, pi->lp);
177
178                 viochar->len = len;
179                 viochar->event.xCorrelationToken = pi->seq++;
180                 viochar->event.xSizeMinus1 =
181                         offsetof(struct viocharlpevent, data) + len;
182
183                 memcpy(viochar->data, buf, len);
184
185                 hvrc = HvCallEvent_signalLpEvent(&viochar->event);
186                 if (hvrc)
187                         hvlog("\n\rerror sending event! return code %d\n\r",
188                                 (int)hvrc);
189                 sent += len;
190                 count -= len;
191                 buf += len;
192         }
193
194         vio_free_event_buffer(viomajorsubtype_chario, viochar);
195 done:
196         spin_unlock_irqrestore(&consolelock, flags);
197         return sent;
198 }
199
200 static struct hv_ops hvc_get_put_ops = {
201         .get_chars = get_chars,
202         .put_chars = put_chars,
203         .notifier_add = notifier_add_irq,
204         .notifier_del = notifier_del_irq,
205 };
206
207 static int __devinit hvc_vio_probe(struct vio_dev *vdev,
208                         const struct vio_device_id *id)
209 {
210         struct hvc_struct *hp;
211         struct port_info *pi;
212
213         /* probed with invalid parameters. */
214         if (!vdev || !id)
215                 return -EPERM;
216
217         if (vdev->unit_address >= VTTY_PORTS)
218                 return -ENODEV;
219
220         pi = &port_info[vdev->unit_address];
221
222         hp = hvc_alloc(vdev->unit_address, vdev->irq, &hvc_get_put_ops,
223                         VIOCHAR_MAX_DATA);
224         if (IS_ERR(hp))
225                 return PTR_ERR(hp);
226         pi->hp = hp;
227         dev_set_drvdata(&vdev->dev, pi);
228
229         return 0;
230 }
231
232 static int __devexit hvc_vio_remove(struct vio_dev *vdev)
233 {
234         struct port_info *pi = dev_get_drvdata(&vdev->dev);
235         struct hvc_struct *hp = pi->hp;
236
237         return hvc_remove(hp);
238 }
239
240 static struct vio_driver hvc_vio_driver = {
241         .id_table       = hvc_driver_table,
242         .probe          = hvc_vio_probe,
243         .remove         = hvc_vio_remove,
244         .driver         = {
245                 .name   = hvc_driver_name,
246                 .owner  = THIS_MODULE,
247         }
248 };
249
250 static void hvc_open_event(struct HvLpEvent *event)
251 {
252         unsigned long flags;
253         struct viocharlpevent *cevent = (struct viocharlpevent *)event;
254         u8 port = cevent->virtual_device;
255         struct port_info *pi;
256         int reject = 0;
257
258         if (hvlpevent_is_ack(event)) {
259                 if (port >= VTTY_PORTS)
260                         return;
261
262                 spin_lock_irqsave(&consolelock, flags);
263
264                 pi = &port_info[port];
265                 if (event->xRc == HvLpEvent_Rc_Good) {
266                         pi->seq = pi->ack = 0;
267                         /*
268                          * This line allows connections from the primary
269                          * partition but once one is connected from the
270                          * primary partition nothing short of a reboot
271                          * of linux will allow access from the hosting
272                          * partition again without a required iSeries fix.
273                          */
274                         pi->lp = event->xTargetLp;
275                 }
276
277                 spin_unlock_irqrestore(&consolelock, flags);
278                 if (event->xRc != HvLpEvent_Rc_Good)
279                         printk(KERN_WARNING
280                                "hvc: handle_open_event: event->xRc == (%d).\n",
281                                event->xRc);
282
283                 if (event->xCorrelationToken != 0) {
284                         atomic_t *aptr= (atomic_t *)event->xCorrelationToken;
285                         atomic_set(aptr, 1);
286                 } else
287                         printk(KERN_WARNING
288                                "hvc: weird...got open ack without atomic\n");
289                 return;
290         }
291
292         /* This had better require an ack, otherwise complain */
293         if (!hvlpevent_need_ack(event)) {
294                 printk(KERN_WARNING "hvc: viocharopen without ack bit!\n");
295                 return;
296         }
297
298         spin_lock_irqsave(&consolelock, flags);
299
300         /* Make sure this is a good virtual tty */
301         if (port >= VTTY_PORTS) {
302                 event->xRc = HvLpEvent_Rc_SubtypeError;
303                 cevent->subtype_result_code = viorc_openRejected;
304                 /*
305                  * Flag state here since we can't printk while holding
306                  * the consolelock spinlock.
307                  */
308                 reject = 1;
309         } else {
310                 pi = &port_info[port];
311                 if ((pi->lp != HvLpIndexInvalid) &&
312                                 (pi->lp != event->xSourceLp)) {
313                         /*
314                          * If this is tty is already connected to a different
315                          * partition, fail.
316                          */
317                         event->xRc = HvLpEvent_Rc_SubtypeError;
318                         cevent->subtype_result_code = viorc_openRejected;
319                         reject = 2;
320                 } else {
321                         pi->lp = event->xSourceLp;
322                         event->xRc = HvLpEvent_Rc_Good;
323                         cevent->subtype_result_code = viorc_good;
324                         pi->seq = pi->ack = 0;
325                 }
326         }
327
328         spin_unlock_irqrestore(&consolelock, flags);
329
330         if (reject == 1)
331                 printk(KERN_WARNING "hvc: open rejected: bad virtual tty.\n");
332         else if (reject == 2)
333                 printk(KERN_WARNING "hvc: open rejected: console in exclusive "
334                                 "use by another partition.\n");
335
336         /* Return the acknowledgement */
337         HvCallEvent_ackLpEvent(event);
338 }
339
340 /*
341  * Handle a close charLpEvent.  This should ONLY be an Interrupt because the
342  * virtual console should never actually issue a close event to the hypervisor
343  * because the virtual console never goes away.  A close event coming from the
344  * hypervisor simply means that there are no client consoles connected to the
345  * virtual console.
346  */
347 static void hvc_close_event(struct HvLpEvent *event)
348 {
349         unsigned long flags;
350         struct viocharlpevent *cevent = (struct viocharlpevent *)event;
351         u8 port = cevent->virtual_device;
352
353         if (!hvlpevent_is_int(event)) {
354                 printk(KERN_WARNING
355                         "hvc: got unexpected close acknowlegement\n");
356                 return;
357         }
358
359         if (port >= VTTY_PORTS) {
360                 printk(KERN_WARNING
361                         "hvc: close message from invalid virtual device.\n");
362                 return;
363         }
364
365         /* For closes, just mark the console partition invalid */
366         spin_lock_irqsave(&consolelock, flags);
367
368         if (port_info[port].lp == event->xSourceLp)
369                 port_info[port].lp = HvLpIndexInvalid;
370
371         spin_unlock_irqrestore(&consolelock, flags);
372 }
373
374 static void hvc_data_event(struct HvLpEvent *event)
375 {
376         unsigned long flags;
377         struct viocharlpevent *cevent = (struct viocharlpevent *)event;
378         struct port_info *pi;
379         int n;
380         u8 port = cevent->virtual_device;
381
382         if (port >= VTTY_PORTS) {
383                 printk(KERN_WARNING "hvc: data on invalid virtual device %d\n",
384                                 port);
385                 return;
386         }
387         if (cevent->len == 0)
388                 return;
389
390         /*
391          * Change 05/01/2003 - Ryan Arnold: If a partition other than
392          * the current exclusive partition tries to send us data
393          * events then just drop them on the floor because we don't
394          * want his stinking data.  He isn't authorized to receive
395          * data because he wasn't the first one to get the console,
396          * therefore he shouldn't be allowed to send data either.
397          * This will work without an iSeries fix.
398          */
399         pi = &port_info[port];
400         if (pi->lp != event->xSourceLp)
401                 return;
402
403         spin_lock_irqsave(&consolelock, flags);
404
405         n = IN_BUF_SIZE - pi->in_end;
406         if (n > cevent->len)
407                 n = cevent->len;
408         if (n > 0) {
409                 memcpy(&pi->in_buf[pi->in_end], cevent->data, n);
410                 pi->in_end += n;
411         }
412         spin_unlock_irqrestore(&consolelock, flags);
413         if (n == 0)
414                 printk(KERN_WARNING "hvc: input buffer overflow\n");
415 }
416
417 static void hvc_ack_event(struct HvLpEvent *event)
418 {
419         struct viocharlpevent *cevent = (struct viocharlpevent *)event;
420         unsigned long flags;
421         u8 port = cevent->virtual_device;
422
423         if (port >= VTTY_PORTS) {
424                 printk(KERN_WARNING "hvc: data on invalid virtual device\n");
425                 return;
426         }
427
428         spin_lock_irqsave(&consolelock, flags);
429         port_info[port].ack = event->xCorrelationToken;
430         spin_unlock_irqrestore(&consolelock, flags);
431 }
432
433 static void hvc_config_event(struct HvLpEvent *event)
434 {
435         struct viocharlpevent *cevent = (struct viocharlpevent *)event;
436
437         if (cevent->data[0] == 0x01)
438                 printk(KERN_INFO "hvc: window resized to %d: %d: %d: %d\n",
439                        cevent->data[1], cevent->data[2],
440                        cevent->data[3], cevent->data[4]);
441         else
442                 printk(KERN_WARNING "hvc: unknown config event\n");
443 }
444
445 static void hvc_handle_event(struct HvLpEvent *event)
446 {
447         int charminor;
448
449         if (event == NULL)
450                 return;
451
452         charminor = event->xSubtype & VIOMINOR_SUBTYPE_MASK;
453         switch (charminor) {
454         case viocharopen:
455                 hvc_open_event(event);
456                 break;
457         case viocharclose:
458                 hvc_close_event(event);
459                 break;
460         case viochardata:
461                 hvc_data_event(event);
462                 break;
463         case viocharack:
464                 hvc_ack_event(event);
465                 break;
466         case viocharconfig:
467                 hvc_config_event(event);
468                 break;
469         default:
470                 if (hvlpevent_is_int(event) && hvlpevent_need_ack(event)) {
471                         event->xRc = HvLpEvent_Rc_InvalidSubtype;
472                         HvCallEvent_ackLpEvent(event);
473                 }
474         }
475 }
476
477 static int __init send_open(HvLpIndex remoteLp, void *sem)
478 {
479         return HvCallEvent_signalLpEventFast(remoteLp,
480                         HvLpEvent_Type_VirtualIo,
481                         viomajorsubtype_chario | viocharopen,
482                         HvLpEvent_AckInd_DoAck, HvLpEvent_AckType_ImmediateAck,
483                         viopath_sourceinst(remoteLp),
484                         viopath_targetinst(remoteLp),
485                         (u64)(unsigned long)sem, VIOVERSION << 16,
486                         0, 0, 0, 0);
487 }
488
489 static int __init hvc_vio_init(void)
490 {
491         atomic_t wait_flag;
492         int rc;
493
494         if (!firmware_has_feature(FW_FEATURE_ISERIES))
495                 return -EIO;
496
497         /* +2 for fudge */
498         rc = viopath_open(HvLpConfig_getPrimaryLpIndex(),
499                         viomajorsubtype_chario, VIOCHAR_WINDOW + 2);
500         if (rc)
501                 printk(KERN_WARNING "hvc: error opening to primary %d\n", rc);
502
503         if (viopath_hostLp == HvLpIndexInvalid)
504                 vio_set_hostlp();
505
506         /*
507          * And if the primary is not the same as the hosting LP, open to the
508          * hosting lp
509          */
510         if ((viopath_hostLp != HvLpIndexInvalid) &&
511             (viopath_hostLp != HvLpConfig_getPrimaryLpIndex())) {
512                 printk(KERN_INFO "hvc: open path to hosting (%d)\n",
513                                 viopath_hostLp);
514                 rc = viopath_open(viopath_hostLp, viomajorsubtype_chario,
515                                 VIOCHAR_WINDOW + 2);    /* +2 for fudge */
516                 if (rc)
517                         printk(KERN_WARNING
518                                 "error opening to partition %d: %d\n",
519                                 viopath_hostLp, rc);
520         }
521
522         if (vio_setHandler(viomajorsubtype_chario, hvc_handle_event) < 0)
523                 printk(KERN_WARNING
524                         "hvc: error seting handler for console events!\n");
525
526         /*
527          * First, try to open the console to the hosting lp.
528          * Wait on a semaphore for the response.
529          */
530         atomic_set(&wait_flag, 0);
531         if ((viopath_isactive(viopath_hostLp)) &&
532             (send_open(viopath_hostLp, &wait_flag) == 0)) {
533                 printk(KERN_INFO "hvc: hosting partition %d\n", viopath_hostLp);
534                 while (atomic_read(&wait_flag) == 0)
535                         mb();
536                 atomic_set(&wait_flag, 0);
537         }
538
539         /*
540          * If we don't have an active console, try the primary
541          */
542         if ((!viopath_isactive(port_info[0].lp)) &&
543             (viopath_isactive(HvLpConfig_getPrimaryLpIndex())) &&
544             (send_open(HvLpConfig_getPrimaryLpIndex(), &wait_flag) == 0)) {
545                 printk(KERN_INFO "hvc: opening console to primary partition\n");
546                 while (atomic_read(&wait_flag) == 0)
547                         mb();
548         }
549
550         /* Register as a vio device to receive callbacks */
551         rc = vio_register_driver(&hvc_vio_driver);
552
553         return rc;
554 }
555 module_init(hvc_vio_init); /* after drivers/char/hvc_console.c */
556
557 static void __exit hvc_vio_exit(void)
558 {
559         vio_unregister_driver(&hvc_vio_driver);
560 }
561 module_exit(hvc_vio_exit);
562
563 /* the device tree order defines our numbering */
564 static int __init hvc_find_vtys(void)
565 {
566         struct device_node *vty;
567         int num_found = 0;
568
569         for (vty = of_find_node_by_name(NULL, "vty"); vty != NULL;
570                         vty = of_find_node_by_name(vty, "vty")) {
571                 const uint32_t *vtermno;
572
573                 /* We have statically defined space for only a certain number
574                  * of console adapters.
575                  */
576                 if ((num_found >= MAX_NR_HVC_CONSOLES) ||
577                                 (num_found >= VTTY_PORTS))
578                         break;
579
580                 vtermno = of_get_property(vty, "reg", NULL);
581                 if (!vtermno)
582                         continue;
583
584                 if (!of_device_is_compatible(vty, "IBM,iSeries-vty"))
585                         continue;
586
587                 if (num_found == 0)
588                         add_preferred_console("hvc", 0, NULL);
589                 hvc_instantiate(*vtermno, num_found, &hvc_get_put_ops);
590                 ++num_found;
591         }
592
593         return num_found;
594 }
595 console_initcall(hvc_find_vtys);