]> err.no Git - linux-2.6/blob - drivers/char/hvc_iseries.c
[POWERPC] The two vio HVC backends clash
[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                 spin_lock_irqsave(&consoleloglock, flags);
158                 HvCall_writeLogBuffer(buf, count);
159                 spin_unlock_irqrestore(&consoleloglock, flags);
160                 sent = count;
161                 goto done;
162         }
163
164         viochar = vio_get_event_buffer(viomajorsubtype_chario);
165         if (viochar == NULL) {
166                 hvlog("\n\rviocons: Can't get viochar buffer.");
167                 goto done;
168         }
169
170         while ((count > 0) && ((pi->seq - pi->ack) < VIOCHAR_WINDOW)) {
171                 int len;
172
173                 len = (count > VIOCHAR_MAX_DATA) ? VIOCHAR_MAX_DATA : count;
174
175                 if (viochar_is_console(pi)) {
176                         spin_lock_irqsave(&consoleloglock, flags);
177                         HvCall_writeLogBuffer(buf, len);
178                         spin_unlock_irqrestore(&consoleloglock, flags);
179                 }
180
181                 init_data_event(viochar, pi->lp);
182
183                 viochar->len = len;
184                 viochar->event.xCorrelationToken = pi->seq++;
185                 viochar->event.xSizeMinus1 =
186                         offsetof(struct viocharlpevent, data) + len;
187
188                 memcpy(viochar->data, buf, len);
189
190                 hvrc = HvCallEvent_signalLpEvent(&viochar->event);
191                 if (hvrc)
192                         hvlog("\n\rerror sending event! return code %d\n\r",
193                                 (int)hvrc);
194                 sent += len;
195                 count -= len;
196                 buf += len;
197         }
198
199         vio_free_event_buffer(viomajorsubtype_chario, viochar);
200 done:
201         spin_unlock_irqrestore(&consolelock, flags);
202         return sent;
203 }
204
205 static struct hv_ops hvc_get_put_ops = {
206         .get_chars = get_chars,
207         .put_chars = put_chars,
208 };
209
210 static int __devinit hvc_vio_probe(struct vio_dev *vdev,
211                         const struct vio_device_id *id)
212 {
213         struct hvc_struct *hp;
214         struct port_info *pi;
215
216         /* probed with invalid parameters. */
217         if (!vdev || !id)
218                 return -EPERM;
219
220         if (vdev->unit_address >= VTTY_PORTS)
221                 return -ENODEV;
222
223         pi = &port_info[vdev->unit_address];
224
225         hp = hvc_alloc(vdev->unit_address, vdev->irq, &hvc_get_put_ops,
226                         VIOCHAR_MAX_DATA);
227         if (IS_ERR(hp))
228                 return PTR_ERR(hp);
229         pi->hp = hp;
230         dev_set_drvdata(&vdev->dev, pi);
231
232         return 0;
233 }
234
235 static int __devexit hvc_vio_remove(struct vio_dev *vdev)
236 {
237         struct port_info *pi = dev_get_drvdata(&vdev->dev);
238         struct hvc_struct *hp = pi->hp;
239
240         return hvc_remove(hp);
241 }
242
243 static struct vio_driver hvc_vio_driver = {
244         .id_table       = hvc_driver_table,
245         .probe          = hvc_vio_probe,
246         .remove         = hvc_vio_remove,
247         .driver         = {
248                 .name   = hvc_driver_name,
249                 .owner  = THIS_MODULE,
250         }
251 };
252
253 static void hvc_open_event(struct HvLpEvent *event)
254 {
255         unsigned long flags;
256         struct viocharlpevent *cevent = (struct viocharlpevent *)event;
257         u8 port = cevent->virtual_device;
258         struct port_info *pi;
259         int reject = 0;
260
261         if (hvlpevent_is_ack(event)) {
262                 if (port >= VTTY_PORTS)
263                         return;
264
265                 spin_lock_irqsave(&consolelock, flags);
266
267                 pi = &port_info[port];
268                 if (event->xRc == HvLpEvent_Rc_Good) {
269                         pi->seq = pi->ack = 0;
270                         /*
271                          * This line allows connections from the primary
272                          * partition but once one is connected from the
273                          * primary partition nothing short of a reboot
274                          * of linux will allow access from the hosting
275                          * partition again without a required iSeries fix.
276                          */
277                         pi->lp = event->xTargetLp;
278                 }
279
280                 spin_unlock_irqrestore(&consolelock, flags);
281                 if (event->xRc != HvLpEvent_Rc_Good)
282                         printk(KERN_WARNING
283                                "hvc: handle_open_event: event->xRc == (%d).\n",
284                                event->xRc);
285
286                 if (event->xCorrelationToken != 0) {
287                         atomic_t *aptr= (atomic_t *)event->xCorrelationToken;
288                         atomic_set(aptr, 1);
289                 } else
290                         printk(KERN_WARNING
291                                "hvc: weird...got open ack without atomic\n");
292                 return;
293         }
294
295         /* This had better require an ack, otherwise complain */
296         if (!hvlpevent_need_ack(event)) {
297                 printk(KERN_WARNING "hvc: viocharopen without ack bit!\n");
298                 return;
299         }
300
301         spin_lock_irqsave(&consolelock, flags);
302
303         /* Make sure this is a good virtual tty */
304         if (port >= VTTY_PORTS) {
305                 event->xRc = HvLpEvent_Rc_SubtypeError;
306                 cevent->subtype_result_code = viorc_openRejected;
307                 /*
308                  * Flag state here since we can't printk while holding
309                  * the consolelock spinlock.
310                  */
311                 reject = 1;
312         } else {
313                 pi = &port_info[port];
314                 if ((pi->lp != HvLpIndexInvalid) &&
315                                 (pi->lp != event->xSourceLp)) {
316                         /*
317                          * If this is tty is already connected to a different
318                          * partition, fail.
319                          */
320                         event->xRc = HvLpEvent_Rc_SubtypeError;
321                         cevent->subtype_result_code = viorc_openRejected;
322                         reject = 2;
323                 } else {
324                         pi->lp = event->xSourceLp;
325                         event->xRc = HvLpEvent_Rc_Good;
326                         cevent->subtype_result_code = viorc_good;
327                         pi->seq = pi->ack = 0;
328                 }
329         }
330
331         spin_unlock_irqrestore(&consolelock, flags);
332
333         if (reject == 1)
334                 printk(KERN_WARNING "hvc: open rejected: bad virtual tty.\n");
335         else if (reject == 2)
336                 printk(KERN_WARNING "hvc: open rejected: console in exclusive "
337                                 "use by another partition.\n");
338
339         /* Return the acknowledgement */
340         HvCallEvent_ackLpEvent(event);
341 }
342
343 /*
344  * Handle a close charLpEvent.  This should ONLY be an Interrupt because the
345  * virtual console should never actually issue a close event to the hypervisor
346  * because the virtual console never goes away.  A close event coming from the
347  * hypervisor simply means that there are no client consoles connected to the
348  * virtual console.
349  */
350 static void hvc_close_event(struct HvLpEvent *event)
351 {
352         unsigned long flags;
353         struct viocharlpevent *cevent = (struct viocharlpevent *)event;
354         u8 port = cevent->virtual_device;
355
356         if (!hvlpevent_is_int(event)) {
357                 printk(KERN_WARNING
358                         "hvc: got unexpected close acknowlegement\n");
359                 return;
360         }
361
362         if (port >= VTTY_PORTS) {
363                 printk(KERN_WARNING
364                         "hvc: close message from invalid virtual device.\n");
365                 return;
366         }
367
368         /* For closes, just mark the console partition invalid */
369         spin_lock_irqsave(&consolelock, flags);
370
371         if (port_info[port].lp == event->xSourceLp)
372                 port_info[port].lp = HvLpIndexInvalid;
373
374         spin_unlock_irqrestore(&consolelock, flags);
375 }
376
377 static void hvc_data_event(struct HvLpEvent *event)
378 {
379         unsigned long flags;
380         struct viocharlpevent *cevent = (struct viocharlpevent *)event;
381         struct port_info *pi;
382         int n;
383         u8 port = cevent->virtual_device;
384
385         if (port >= VTTY_PORTS) {
386                 printk(KERN_WARNING "hvc: data on invalid virtual device %d\n",
387                                 port);
388                 return;
389         }
390         if (cevent->len == 0)
391                 return;
392
393         /*
394          * Change 05/01/2003 - Ryan Arnold: If a partition other than
395          * the current exclusive partition tries to send us data
396          * events then just drop them on the floor because we don't
397          * want his stinking data.  He isn't authorized to receive
398          * data because he wasn't the first one to get the console,
399          * therefore he shouldn't be allowed to send data either.
400          * This will work without an iSeries fix.
401          */
402         pi = &port_info[port];
403         if (pi->lp != event->xSourceLp)
404                 return;
405
406         spin_lock_irqsave(&consolelock, flags);
407
408         n = IN_BUF_SIZE - pi->in_end;
409         if (n > cevent->len)
410                 n = cevent->len;
411         if (n > 0) {
412                 memcpy(&pi->in_buf[pi->in_end], cevent->data, n);
413                 pi->in_end += n;
414         }
415         spin_unlock_irqrestore(&consolelock, flags);
416         if (n == 0)
417                 printk(KERN_WARNING "hvc: input buffer overflow\n");
418 }
419
420 static void hvc_ack_event(struct HvLpEvent *event)
421 {
422         struct viocharlpevent *cevent = (struct viocharlpevent *)event;
423         unsigned long flags;
424         u8 port = cevent->virtual_device;
425
426         if (port >= VTTY_PORTS) {
427                 printk(KERN_WARNING "hvc: data on invalid virtual device\n");
428                 return;
429         }
430
431         spin_lock_irqsave(&consolelock, flags);
432         port_info[port].ack = event->xCorrelationToken;
433         spin_unlock_irqrestore(&consolelock, flags);
434 }
435
436 static void hvc_config_event(struct HvLpEvent *event)
437 {
438         struct viocharlpevent *cevent = (struct viocharlpevent *)event;
439
440         if (cevent->data[0] == 0x01)
441                 printk(KERN_INFO "hvc: window resized to %d: %d: %d: %d\n",
442                        cevent->data[1], cevent->data[2],
443                        cevent->data[3], cevent->data[4]);
444         else
445                 printk(KERN_WARNING "hvc: unknown config event\n");
446 }
447
448 static void hvc_handle_event(struct HvLpEvent *event)
449 {
450         int charminor;
451
452         if (event == NULL)
453                 return;
454
455         charminor = event->xSubtype & VIOMINOR_SUBTYPE_MASK;
456         switch (charminor) {
457         case viocharopen:
458                 hvc_open_event(event);
459                 break;
460         case viocharclose:
461                 hvc_close_event(event);
462                 break;
463         case viochardata:
464                 hvc_data_event(event);
465                 break;
466         case viocharack:
467                 hvc_ack_event(event);
468                 break;
469         case viocharconfig:
470                 hvc_config_event(event);
471                 break;
472         default:
473                 if (hvlpevent_is_int(event) && hvlpevent_need_ack(event)) {
474                         event->xRc = HvLpEvent_Rc_InvalidSubtype;
475                         HvCallEvent_ackLpEvent(event);
476                 }
477         }
478 }
479
480 static int send_open(HvLpIndex remoteLp, void *sem)
481 {
482         return HvCallEvent_signalLpEventFast(remoteLp,
483                         HvLpEvent_Type_VirtualIo,
484                         viomajorsubtype_chario | viocharopen,
485                         HvLpEvent_AckInd_DoAck, HvLpEvent_AckType_ImmediateAck,
486                         viopath_sourceinst(remoteLp),
487                         viopath_targetinst(remoteLp),
488                         (u64)(unsigned long)sem, VIOVERSION << 16,
489                         0, 0, 0, 0);
490 }
491
492 static int hvc_vio_init(void)
493 {
494         atomic_t wait_flag;
495         int rc;
496
497         if (!firmware_has_feature(FW_FEATURE_ISERIES))
498                 return -EIO;
499
500         /* +2 for fudge */
501         rc = viopath_open(HvLpConfig_getPrimaryLpIndex(),
502                         viomajorsubtype_chario, VIOCHAR_WINDOW + 2);
503         if (rc)
504                 printk(KERN_WARNING "hvc: error opening to primary %d\n", rc);
505
506         if (viopath_hostLp == HvLpIndexInvalid)
507                 vio_set_hostlp();
508
509         /*
510          * And if the primary is not the same as the hosting LP, open to the
511          * hosting lp
512          */
513         if ((viopath_hostLp != HvLpIndexInvalid) &&
514             (viopath_hostLp != HvLpConfig_getPrimaryLpIndex())) {
515                 printk(KERN_INFO "hvc: open path to hosting (%d)\n",
516                                 viopath_hostLp);
517                 rc = viopath_open(viopath_hostLp, viomajorsubtype_chario,
518                                 VIOCHAR_WINDOW + 2);    /* +2 for fudge */
519                 if (rc)
520                         printk(KERN_WARNING
521                                 "error opening to partition %d: %d\n",
522                                 viopath_hostLp, rc);
523         }
524
525         if (vio_setHandler(viomajorsubtype_chario, hvc_handle_event) < 0)
526                 printk(KERN_WARNING
527                         "hvc: error seting handler for console events!\n");
528
529         /*
530          * First, try to open the console to the hosting lp.
531          * Wait on a semaphore for the response.
532          */
533         atomic_set(&wait_flag, 0);
534         if ((viopath_isactive(viopath_hostLp)) &&
535             (send_open(viopath_hostLp, &wait_flag) == 0)) {
536                 printk(KERN_INFO "hvc: hosting partition %d\n", viopath_hostLp);
537                 while (atomic_read(&wait_flag) == 0)
538                         mb();
539                 atomic_set(&wait_flag, 0);
540         }
541
542         /*
543          * If we don't have an active console, try the primary
544          */
545         if ((!viopath_isactive(port_info[0].lp)) &&
546             (viopath_isactive(HvLpConfig_getPrimaryLpIndex())) &&
547             (send_open(HvLpConfig_getPrimaryLpIndex(), &wait_flag) == 0)) {
548                 printk(KERN_INFO "hvc: opening console to primary partition\n");
549                 while (atomic_read(&wait_flag) == 0)
550                         mb();
551         }
552
553         /* Register as a vio device to receive callbacks */
554         rc = vio_register_driver(&hvc_vio_driver);
555
556         return rc;
557 }
558 module_init(hvc_vio_init); /* after drivers/char/hvc_console.c */
559
560 static void hvc_vio_exit(void)
561 {
562         vio_unregister_driver(&hvc_vio_driver);
563 }
564 module_exit(hvc_vio_exit);
565
566 /* the device tree order defines our numbering */
567 static int hvc_find_vtys(void)
568 {
569         struct device_node *vty;
570         int num_found = 0;
571
572         for (vty = of_find_node_by_name(NULL, "vty"); vty != NULL;
573                         vty = of_find_node_by_name(vty, "vty")) {
574                 const uint32_t *vtermno;
575
576                 /* We have statically defined space for only a certain number
577                  * of console adapters.
578                  */
579                 if ((num_found >= MAX_NR_HVC_CONSOLES) ||
580                                 (num_found >= VTTY_PORTS))
581                         break;
582
583                 vtermno = get_property(vty, "reg", NULL);
584                 if (!vtermno)
585                         continue;
586
587                 if (!device_is_compatible(vty, "IBM,iSeries-vty"))
588                         continue;
589
590                 if (num_found == 0)
591                         add_preferred_console("hvc", 0, NULL);
592                 hvc_instantiate(*vtermno, num_found, &hvc_get_put_ops);
593                 ++num_found;
594         }
595
596         return num_found;
597 }
598 console_initcall(hvc_find_vtys);