[SCSI] libsas: Provide a transport-level facility to request SAS addrs
[linux-2.6] / arch / powerpc / platforms / iseries / viopath.c
1 /* -*- linux-c -*-
2  *
3  *  iSeries Virtual I/O Message Path code
4  *
5  *  Authors: Dave Boutcher <boutcher@us.ibm.com>
6  *           Ryan Arnold <ryanarn@us.ibm.com>
7  *           Colin Devilbiss <devilbis@us.ibm.com>
8  *
9  * (C) Copyright 2000-2005 IBM Corporation
10  *
11  * This code is used by the iSeries virtual disk, cd,
12  * tape, and console to communicate with OS/400 in another
13  * partition.
14  *
15  * This program is free software;  you can redistribute it and/or
16  * modify it under the terms of the GNU General Public License as
17  * published by the Free Software Foundation; either version 2 of the
18  * License, or (at your option) anyu later version.
19  *
20  * This program is distributed in the hope that it will be useful, but
21  * WITHOUT ANY WARRANTY; without even the implied warranty of
22  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
23  * General Public License for more details.
24  *
25  * You should have received a copy of the GNU General Public License
26  * along with this program; if not, write to the Free Software Foundation,
27  * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
28  *
29  */
30 #include <linux/module.h>
31 #include <linux/kernel.h>
32 #include <linux/errno.h>
33 #include <linux/vmalloc.h>
34 #include <linux/string.h>
35 #include <linux/proc_fs.h>
36 #include <linux/dma-mapping.h>
37 #include <linux/wait.h>
38 #include <linux/seq_file.h>
39 #include <linux/interrupt.h>
40 #include <linux/completion.h>
41
42 #include <asm/system.h>
43 #include <asm/uaccess.h>
44 #include <asm/prom.h>
45 #include <asm/firmware.h>
46 #include <asm/iseries/hv_types.h>
47 #include <asm/iseries/hv_lp_event.h>
48 #include <asm/iseries/hv_lp_config.h>
49 #include <asm/iseries/mf.h>
50 #include <asm/iseries/vio.h>
51
52 /* Status of the path to each other partition in the system.
53  * This is overkill, since we will only ever establish connections
54  * to our hosting partition and the primary partition on the system.
55  * But this allows for other support in the future.
56  */
57 static struct viopathStatus {
58         int isOpen;             /* Did we open the path?            */
59         int isActive;           /* Do we have a mon msg outstanding */
60         int users[VIO_MAX_SUBTYPES];
61         HvLpInstanceId mSourceInst;
62         HvLpInstanceId mTargetInst;
63         int numberAllocated;
64 } viopathStatus[HVMAXARCHITECTEDLPS];
65
66 static DEFINE_SPINLOCK(statuslock);
67
68 /*
69  * For each kind of event we allocate a buffer that is
70  * guaranteed not to cross a page boundary
71  */
72 static unsigned char event_buffer[VIO_MAX_SUBTYPES * 256]
73         __attribute__((__aligned__(4096)));
74 static atomic_t event_buffer_available[VIO_MAX_SUBTYPES];
75 static int event_buffer_initialised;
76
77 static void handleMonitorEvent(struct HvLpEvent *event);
78
79 /*
80  * We use this structure to handle asynchronous responses.  The caller
81  * blocks on the semaphore and the handler posts the semaphore.  However,
82  * if system_state is not SYSTEM_RUNNING, then wait_atomic is used ...
83  */
84 struct alloc_parms {
85         struct completion done;
86         int number;
87         atomic_t wait_atomic;
88         int used_wait_atomic;
89 };
90
91 /* Put a sequence number in each mon msg.  The value is not
92  * important.  Start at something other than 0 just for
93  * readability.  wrapping this is ok.
94  */
95 static u8 viomonseq = 22;
96
97 /* Our hosting logical partition.  We get this at startup
98  * time, and different modules access this variable directly.
99  */
100 HvLpIndex viopath_hostLp = HvLpIndexInvalid;
101 EXPORT_SYMBOL(viopath_hostLp);
102 HvLpIndex viopath_ourLp = HvLpIndexInvalid;
103 EXPORT_SYMBOL(viopath_ourLp);
104
105 /* For each kind of incoming event we set a pointer to a
106  * routine to call.
107  */
108 static vio_event_handler_t *vio_handler[VIO_MAX_SUBTYPES];
109
110 #define VIOPATH_KERN_WARN       KERN_WARNING "viopath: "
111 #define VIOPATH_KERN_INFO       KERN_INFO "viopath: "
112
113 static int proc_viopath_show(struct seq_file *m, void *v)
114 {
115         char *buf;
116         u16 vlanMap;
117         dma_addr_t handle;
118         HvLpEvent_Rc hvrc;
119         DECLARE_COMPLETION(done);
120         struct device_node *node;
121         const char *sysid;
122
123         buf = kzalloc(HW_PAGE_SIZE, GFP_KERNEL);
124         if (!buf)
125                 return 0;
126
127         handle = iseries_hv_map(buf, HW_PAGE_SIZE, DMA_FROM_DEVICE);
128
129         hvrc = HvCallEvent_signalLpEventFast(viopath_hostLp,
130                         HvLpEvent_Type_VirtualIo,
131                         viomajorsubtype_config | vioconfigget,
132                         HvLpEvent_AckInd_DoAck, HvLpEvent_AckType_ImmediateAck,
133                         viopath_sourceinst(viopath_hostLp),
134                         viopath_targetinst(viopath_hostLp),
135                         (u64)(unsigned long)&done, VIOVERSION << 16,
136                         ((u64)handle) << 32, HW_PAGE_SIZE, 0, 0);
137
138         if (hvrc != HvLpEvent_Rc_Good)
139                 printk(VIOPATH_KERN_WARN "hv error on op %d\n", (int)hvrc);
140
141         wait_for_completion(&done);
142
143         vlanMap = HvLpConfig_getVirtualLanIndexMap();
144
145         buf[HW_PAGE_SIZE-1] = '\0';
146         seq_printf(m, "%s", buf);
147
148         iseries_hv_unmap(handle, HW_PAGE_SIZE, DMA_FROM_DEVICE);
149         kfree(buf);
150
151         seq_printf(m, "AVAILABLE_VETH=%x\n", vlanMap);
152
153         node = of_find_node_by_path("/");
154         sysid = NULL;
155         if (node != NULL)
156                 sysid = of_get_property(node, "system-id", NULL);
157
158         if (sysid == NULL)
159                 seq_printf(m, "SRLNBR=<UNKNOWN>\n");
160         else
161                 /* Skip "IBM," on front of serial number, see dt.c */
162                 seq_printf(m, "SRLNBR=%s\n", sysid + 4);
163
164         of_node_put(node);
165
166         return 0;
167 }
168
169 static int proc_viopath_open(struct inode *inode, struct file *file)
170 {
171         return single_open(file, proc_viopath_show, NULL);
172 }
173
174 static const struct file_operations proc_viopath_operations = {
175         .open           = proc_viopath_open,
176         .read           = seq_read,
177         .llseek         = seq_lseek,
178         .release        = single_release,
179 };
180
181 static int __init vio_proc_init(void)
182 {
183         struct proc_dir_entry *e;
184
185         if (!firmware_has_feature(FW_FEATURE_ISERIES))
186                 return 0;
187
188         e = create_proc_entry("iSeries/config", 0, NULL);
189         if (e)
190                 e->proc_fops = &proc_viopath_operations;
191
192         return 0;
193 }
194 __initcall(vio_proc_init);
195
196 /* See if a given LP is active.  Allow for invalid lps to be passed in
197  * and just return invalid
198  */
199 int viopath_isactive(HvLpIndex lp)
200 {
201         if (lp == HvLpIndexInvalid)
202                 return 0;
203         if (lp < HVMAXARCHITECTEDLPS)
204                 return viopathStatus[lp].isActive;
205         else
206                 return 0;
207 }
208 EXPORT_SYMBOL(viopath_isactive);
209
210 /*
211  * We cache the source and target instance ids for each
212  * partition.
213  */
214 HvLpInstanceId viopath_sourceinst(HvLpIndex lp)
215 {
216         return viopathStatus[lp].mSourceInst;
217 }
218 EXPORT_SYMBOL(viopath_sourceinst);
219
220 HvLpInstanceId viopath_targetinst(HvLpIndex lp)
221 {
222         return viopathStatus[lp].mTargetInst;
223 }
224 EXPORT_SYMBOL(viopath_targetinst);
225
226 /*
227  * Send a monitor message.  This is a message with the acknowledge
228  * bit on that the other side will NOT explicitly acknowledge.  When
229  * the other side goes down, the hypervisor will acknowledge any
230  * outstanding messages....so we will know when the other side dies.
231  */
232 static void sendMonMsg(HvLpIndex remoteLp)
233 {
234         HvLpEvent_Rc hvrc;
235
236         viopathStatus[remoteLp].mSourceInst =
237                 HvCallEvent_getSourceLpInstanceId(remoteLp,
238                                 HvLpEvent_Type_VirtualIo);
239         viopathStatus[remoteLp].mTargetInst =
240                 HvCallEvent_getTargetLpInstanceId(remoteLp,
241                                 HvLpEvent_Type_VirtualIo);
242
243         /*
244          * Deliberately ignore the return code here.  if we call this
245          * more than once, we don't care.
246          */
247         vio_setHandler(viomajorsubtype_monitor, handleMonitorEvent);
248
249         hvrc = HvCallEvent_signalLpEventFast(remoteLp, HvLpEvent_Type_VirtualIo,
250                         viomajorsubtype_monitor, HvLpEvent_AckInd_DoAck,
251                         HvLpEvent_AckType_DeferredAck,
252                         viopathStatus[remoteLp].mSourceInst,
253                         viopathStatus[remoteLp].mTargetInst,
254                         viomonseq++, 0, 0, 0, 0, 0);
255
256         if (hvrc == HvLpEvent_Rc_Good)
257                 viopathStatus[remoteLp].isActive = 1;
258         else {
259                 printk(VIOPATH_KERN_WARN "could not connect to partition %d\n",
260                                 remoteLp);
261                 viopathStatus[remoteLp].isActive = 0;
262         }
263 }
264
265 static void handleMonitorEvent(struct HvLpEvent *event)
266 {
267         HvLpIndex remoteLp;
268         int i;
269
270         /*
271          * This handler is _also_ called as part of the loop
272          * at the end of this routine, so it must be able to
273          * ignore NULL events...
274          */
275         if (!event)
276                 return;
277
278         /*
279          * First see if this is just a normal monitor message from the
280          * other partition
281          */
282         if (hvlpevent_is_int(event)) {
283                 remoteLp = event->xSourceLp;
284                 if (!viopathStatus[remoteLp].isActive)
285                         sendMonMsg(remoteLp);
286                 return;
287         }
288
289         /*
290          * This path is for an acknowledgement; the other partition
291          * died
292          */
293         remoteLp = event->xTargetLp;
294         if ((event->xSourceInstanceId != viopathStatus[remoteLp].mSourceInst) ||
295             (event->xTargetInstanceId != viopathStatus[remoteLp].mTargetInst)) {
296                 printk(VIOPATH_KERN_WARN "ignoring ack....mismatched instances\n");
297                 return;
298         }
299
300         printk(VIOPATH_KERN_WARN "partition %d ended\n", remoteLp);
301
302         viopathStatus[remoteLp].isActive = 0;
303
304         /*
305          * For each active handler, pass them a NULL
306          * message to indicate that the other partition
307          * died
308          */
309         for (i = 0; i < VIO_MAX_SUBTYPES; i++) {
310                 if (vio_handler[i] != NULL)
311                         (*vio_handler[i])(NULL);
312         }
313 }
314
315 int vio_setHandler(int subtype, vio_event_handler_t *beh)
316 {
317         subtype = subtype >> VIOMAJOR_SUBTYPE_SHIFT;
318         if ((subtype < 0) || (subtype >= VIO_MAX_SUBTYPES))
319                 return -EINVAL;
320         if (vio_handler[subtype] != NULL)
321                 return -EBUSY;
322         vio_handler[subtype] = beh;
323         return 0;
324 }
325 EXPORT_SYMBOL(vio_setHandler);
326
327 int vio_clearHandler(int subtype)
328 {
329         subtype = subtype >> VIOMAJOR_SUBTYPE_SHIFT;
330         if ((subtype < 0) || (subtype >= VIO_MAX_SUBTYPES))
331                 return -EINVAL;
332         if (vio_handler[subtype] == NULL)
333                 return -EAGAIN;
334         vio_handler[subtype] = NULL;
335         return 0;
336 }
337 EXPORT_SYMBOL(vio_clearHandler);
338
339 static void handleConfig(struct HvLpEvent *event)
340 {
341         if (!event)
342                 return;
343         if (hvlpevent_is_int(event)) {
344                 printk(VIOPATH_KERN_WARN
345                        "unexpected config request from partition %d",
346                        event->xSourceLp);
347
348                 if (hvlpevent_need_ack(event)) {
349                         event->xRc = HvLpEvent_Rc_InvalidSubtype;
350                         HvCallEvent_ackLpEvent(event);
351                 }
352                 return;
353         }
354
355         complete((struct completion *)event->xCorrelationToken);
356 }
357
358 /*
359  * Initialization of the hosting partition
360  */
361 void vio_set_hostlp(void)
362 {
363         /*
364          * If this has already been set then we DON'T want to either change
365          * it or re-register the proc file system
366          */
367         if (viopath_hostLp != HvLpIndexInvalid)
368                 return;
369
370         /*
371          * Figure out our hosting partition.  This isn't allowed to change
372          * while we're active
373          */
374         viopath_ourLp = HvLpConfig_getLpIndex();
375         viopath_hostLp = HvLpConfig_getHostingLpIndex(viopath_ourLp);
376
377         if (viopath_hostLp != HvLpIndexInvalid)
378                 vio_setHandler(viomajorsubtype_config, handleConfig);
379 }
380 EXPORT_SYMBOL(vio_set_hostlp);
381
382 static void vio_handleEvent(struct HvLpEvent *event)
383 {
384         HvLpIndex remoteLp;
385         int subtype = (event->xSubtype & VIOMAJOR_SUBTYPE_MASK)
386                 >> VIOMAJOR_SUBTYPE_SHIFT;
387
388         if (hvlpevent_is_int(event)) {
389                 remoteLp = event->xSourceLp;
390                 /*
391                  * The isActive is checked because if the hosting partition
392                  * went down and came back up it would not be active but it
393                  * would have different source and target instances, in which
394                  * case we'd want to reset them.  This case really protects
395                  * against an unauthorized active partition sending interrupts
396                  * or acks to this linux partition.
397                  */
398                 if (viopathStatus[remoteLp].isActive
399                     && (event->xSourceInstanceId !=
400                         viopathStatus[remoteLp].mTargetInst)) {
401                         printk(VIOPATH_KERN_WARN
402                                "message from invalid partition. "
403                                "int msg rcvd, source inst (%d) doesnt match (%d)\n",
404                                viopathStatus[remoteLp].mTargetInst,
405                                event->xSourceInstanceId);
406                         return;
407                 }
408
409                 if (viopathStatus[remoteLp].isActive
410                     && (event->xTargetInstanceId !=
411                         viopathStatus[remoteLp].mSourceInst)) {
412                         printk(VIOPATH_KERN_WARN
413                                "message from invalid partition. "
414                                "int msg rcvd, target inst (%d) doesnt match (%d)\n",
415                                viopathStatus[remoteLp].mSourceInst,
416                                event->xTargetInstanceId);
417                         return;
418                 }
419         } else {
420                 remoteLp = event->xTargetLp;
421                 if (event->xSourceInstanceId !=
422                     viopathStatus[remoteLp].mSourceInst) {
423                         printk(VIOPATH_KERN_WARN
424                                "message from invalid partition. "
425                                "ack msg rcvd, source inst (%d) doesnt match (%d)\n",
426                                viopathStatus[remoteLp].mSourceInst,
427                                event->xSourceInstanceId);
428                         return;
429                 }
430
431                 if (event->xTargetInstanceId !=
432                     viopathStatus[remoteLp].mTargetInst) {
433                         printk(VIOPATH_KERN_WARN
434                                "message from invalid partition. "
435                                "viopath: ack msg rcvd, target inst (%d) doesnt match (%d)\n",
436                                viopathStatus[remoteLp].mTargetInst,
437                                event->xTargetInstanceId);
438                         return;
439                 }
440         }
441
442         if (vio_handler[subtype] == NULL) {
443                 printk(VIOPATH_KERN_WARN
444                        "unexpected virtual io event subtype %d from partition %d\n",
445                        event->xSubtype, remoteLp);
446                 /* No handler.  Ack if necessary */
447                 if (hvlpevent_is_int(event) && hvlpevent_need_ack(event)) {
448                         event->xRc = HvLpEvent_Rc_InvalidSubtype;
449                         HvCallEvent_ackLpEvent(event);
450                 }
451                 return;
452         }
453
454         /* This innocuous little line is where all the real work happens */
455         (*vio_handler[subtype])(event);
456 }
457
458 static void viopath_donealloc(void *parm, int number)
459 {
460         struct alloc_parms *parmsp = parm;
461
462         parmsp->number = number;
463         if (parmsp->used_wait_atomic)
464                 atomic_set(&parmsp->wait_atomic, 0);
465         else
466                 complete(&parmsp->done);
467 }
468
469 static int allocateEvents(HvLpIndex remoteLp, int numEvents)
470 {
471         struct alloc_parms parms;
472
473         if (system_state != SYSTEM_RUNNING) {
474                 parms.used_wait_atomic = 1;
475                 atomic_set(&parms.wait_atomic, 1);
476         } else {
477                 parms.used_wait_atomic = 0;
478                 init_completion(&parms.done);
479         }
480         mf_allocate_lp_events(remoteLp, HvLpEvent_Type_VirtualIo, 250,  /* It would be nice to put a real number here! */
481                             numEvents, &viopath_donealloc, &parms);
482         if (system_state != SYSTEM_RUNNING) {
483                 while (atomic_read(&parms.wait_atomic))
484                         mb();
485         } else
486                 wait_for_completion(&parms.done);
487         return parms.number;
488 }
489
490 int viopath_open(HvLpIndex remoteLp, int subtype, int numReq)
491 {
492         int i;
493         unsigned long flags;
494         int tempNumAllocated;
495
496         if ((remoteLp >= HVMAXARCHITECTEDLPS) || (remoteLp == HvLpIndexInvalid))
497                 return -EINVAL;
498
499         subtype = subtype >> VIOMAJOR_SUBTYPE_SHIFT;
500         if ((subtype < 0) || (subtype >= VIO_MAX_SUBTYPES))
501                 return -EINVAL;
502
503         spin_lock_irqsave(&statuslock, flags);
504
505         if (!event_buffer_initialised) {
506                 for (i = 0; i < VIO_MAX_SUBTYPES; i++)
507                         atomic_set(&event_buffer_available[i], 1);
508                 event_buffer_initialised = 1;
509         }
510
511         viopathStatus[remoteLp].users[subtype]++;
512
513         if (!viopathStatus[remoteLp].isOpen) {
514                 viopathStatus[remoteLp].isOpen = 1;
515                 HvCallEvent_openLpEventPath(remoteLp, HvLpEvent_Type_VirtualIo);
516
517                 /*
518                  * Don't hold the spinlock during an operation that
519                  * can sleep.
520                  */
521                 spin_unlock_irqrestore(&statuslock, flags);
522                 tempNumAllocated = allocateEvents(remoteLp, 1);
523                 spin_lock_irqsave(&statuslock, flags);
524
525                 viopathStatus[remoteLp].numberAllocated += tempNumAllocated;
526
527                 if (viopathStatus[remoteLp].numberAllocated == 0) {
528                         HvCallEvent_closeLpEventPath(remoteLp,
529                                         HvLpEvent_Type_VirtualIo);
530
531                         spin_unlock_irqrestore(&statuslock, flags);
532                         return -ENOMEM;
533                 }
534
535                 viopathStatus[remoteLp].mSourceInst =
536                         HvCallEvent_getSourceLpInstanceId(remoteLp,
537                                         HvLpEvent_Type_VirtualIo);
538                 viopathStatus[remoteLp].mTargetInst =
539                         HvCallEvent_getTargetLpInstanceId(remoteLp,
540                                         HvLpEvent_Type_VirtualIo);
541                 HvLpEvent_registerHandler(HvLpEvent_Type_VirtualIo,
542                                           &vio_handleEvent);
543                 sendMonMsg(remoteLp);
544                 printk(VIOPATH_KERN_INFO "opening connection to partition %d, "
545                                 "setting sinst %d, tinst %d\n",
546                                 remoteLp, viopathStatus[remoteLp].mSourceInst,
547                                 viopathStatus[remoteLp].mTargetInst);
548         }
549
550         spin_unlock_irqrestore(&statuslock, flags);
551         tempNumAllocated = allocateEvents(remoteLp, numReq);
552         spin_lock_irqsave(&statuslock, flags);
553         viopathStatus[remoteLp].numberAllocated += tempNumAllocated;
554         spin_unlock_irqrestore(&statuslock, flags);
555
556         return 0;
557 }
558 EXPORT_SYMBOL(viopath_open);
559
560 int viopath_close(HvLpIndex remoteLp, int subtype, int numReq)
561 {
562         unsigned long flags;
563         int i;
564         int numOpen;
565         struct alloc_parms parms;
566
567         if ((remoteLp >= HVMAXARCHITECTEDLPS) || (remoteLp == HvLpIndexInvalid))
568                 return -EINVAL;
569
570         subtype = subtype >> VIOMAJOR_SUBTYPE_SHIFT;
571         if ((subtype < 0) || (subtype >= VIO_MAX_SUBTYPES))
572                 return -EINVAL;
573
574         spin_lock_irqsave(&statuslock, flags);
575         /*
576          * If the viopath_close somehow gets called before a
577          * viopath_open it could decrement to -1 which is a non
578          * recoverable state so we'll prevent this from
579          * happening.
580          */
581         if (viopathStatus[remoteLp].users[subtype] > 0)
582                 viopathStatus[remoteLp].users[subtype]--;
583
584         spin_unlock_irqrestore(&statuslock, flags);
585
586         parms.used_wait_atomic = 0;
587         init_completion(&parms.done);
588         mf_deallocate_lp_events(remoteLp, HvLpEvent_Type_VirtualIo,
589                               numReq, &viopath_donealloc, &parms);
590         wait_for_completion(&parms.done);
591
592         spin_lock_irqsave(&statuslock, flags);
593         for (i = 0, numOpen = 0; i < VIO_MAX_SUBTYPES; i++)
594                 numOpen += viopathStatus[remoteLp].users[i];
595
596         if ((viopathStatus[remoteLp].isOpen) && (numOpen == 0)) {
597                 printk(VIOPATH_KERN_INFO "closing connection to partition %d\n",
598                                 remoteLp);
599
600                 HvCallEvent_closeLpEventPath(remoteLp,
601                                              HvLpEvent_Type_VirtualIo);
602                 viopathStatus[remoteLp].isOpen = 0;
603                 viopathStatus[remoteLp].isActive = 0;
604
605                 for (i = 0; i < VIO_MAX_SUBTYPES; i++)
606                         atomic_set(&event_buffer_available[i], 0);
607                 event_buffer_initialised = 0;
608         }
609         spin_unlock_irqrestore(&statuslock, flags);
610         return 0;
611 }
612 EXPORT_SYMBOL(viopath_close);
613
614 void *vio_get_event_buffer(int subtype)
615 {
616         subtype = subtype >> VIOMAJOR_SUBTYPE_SHIFT;
617         if ((subtype < 0) || (subtype >= VIO_MAX_SUBTYPES))
618                 return NULL;
619
620         if (atomic_dec_if_positive(&event_buffer_available[subtype]) == 0)
621                 return &event_buffer[subtype * 256];
622         else
623                 return NULL;
624 }
625 EXPORT_SYMBOL(vio_get_event_buffer);
626
627 void vio_free_event_buffer(int subtype, void *buffer)
628 {
629         subtype = subtype >> VIOMAJOR_SUBTYPE_SHIFT;
630         if ((subtype < 0) || (subtype >= VIO_MAX_SUBTYPES)) {
631                 printk(VIOPATH_KERN_WARN
632                        "unexpected subtype %d freeing event buffer\n", subtype);
633                 return;
634         }
635
636         if (atomic_read(&event_buffer_available[subtype]) != 0) {
637                 printk(VIOPATH_KERN_WARN
638                        "freeing unallocated event buffer, subtype %d\n",
639                        subtype);
640                 return;
641         }
642
643         if (buffer != &event_buffer[subtype * 256]) {
644                 printk(VIOPATH_KERN_WARN
645                        "freeing invalid event buffer, subtype %d\n", subtype);
646         }
647
648         atomic_set(&event_buffer_available[subtype], 1);
649 }
650 EXPORT_SYMBOL(vio_free_event_buffer);
651
652 static const struct vio_error_entry vio_no_error =
653     { 0, 0, "Non-VIO Error" };
654 static const struct vio_error_entry vio_unknown_error =
655     { 0, EIO, "Unknown Error" };
656
657 static const struct vio_error_entry vio_default_errors[] = {
658         {0x0001, EIO, "No Connection"},
659         {0x0002, EIO, "No Receiver"},
660         {0x0003, EIO, "No Buffer Available"},
661         {0x0004, EBADRQC, "Invalid Message Type"},
662         {0x0000, 0, NULL},
663 };
664
665 const struct vio_error_entry *vio_lookup_rc(
666                 const struct vio_error_entry *local_table, u16 rc)
667 {
668         const struct vio_error_entry *cur;
669
670         if (!rc)
671                 return &vio_no_error;
672         if (local_table)
673                 for (cur = local_table; cur->rc; ++cur)
674                         if (cur->rc == rc)
675                                 return cur;
676         for (cur = vio_default_errors; cur->rc; ++cur)
677                 if (cur->rc == rc)
678                         return cur;
679         return &vio_unknown_error;
680 }
681 EXPORT_SYMBOL(vio_lookup_rc);