Pull altix-ce1.0-asic into release branch
[linux-2.6] / drivers / acpi / parser / psloop.c
1 /******************************************************************************
2  *
3  * Module Name: psloop - Main AML parse loop
4  *
5  *****************************************************************************/
6
7 /*
8  * Copyright (C) 2000 - 2006, R. Byron Moore
9  * All rights reserved.
10  *
11  * Redistribution and use in source and binary forms, with or without
12  * modification, are permitted provided that the following conditions
13  * are met:
14  * 1. Redistributions of source code must retain the above copyright
15  *    notice, this list of conditions, and the following disclaimer,
16  *    without modification.
17  * 2. Redistributions in binary form must reproduce at minimum a disclaimer
18  *    substantially similar to the "NO WARRANTY" disclaimer below
19  *    ("Disclaimer") and any redistribution must be conditioned upon
20  *    including a substantially similar Disclaimer requirement for further
21  *    binary redistribution.
22  * 3. Neither the names of the above-listed copyright holders nor the names
23  *    of any contributors may be used to endorse or promote products derived
24  *    from this software without specific prior written permission.
25  *
26  * Alternatively, this software may be distributed under the terms of the
27  * GNU General Public License ("GPL") version 2 as published by the Free
28  * Software Foundation.
29  *
30  * NO WARRANTY
31  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
32  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
33  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR
34  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
35  * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
36  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
37  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
38  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
39  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
40  * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
41  * POSSIBILITY OF SUCH DAMAGES.
42  */
43
44 /*
45  * Parse the AML and build an operation tree as most interpreters,
46  * like Perl, do.  Parsing is done by hand rather than with a YACC
47  * generated parser to tightly constrain stack and dynamic memory
48  * usage.  At the same time, parsing is kept flexible and the code
49  * fairly compact by parsing based on a list of AML opcode
50  * templates in aml_op_info[]
51  */
52
53 #include <acpi/acpi.h>
54 #include <acpi/acparser.h>
55 #include <acpi/acdispat.h>
56 #include <acpi/amlcode.h>
57
58 #define _COMPONENT          ACPI_PARSER
59 ACPI_MODULE_NAME("psloop")
60
61 static u32 acpi_gbl_depth = 0;
62
63 /*******************************************************************************
64  *
65  * FUNCTION:    acpi_ps_parse_loop
66  *
67  * PARAMETERS:  walk_state          - Current state
68  *
69  * RETURN:      Status
70  *
71  * DESCRIPTION: Parse AML (pointed to by the current parser state) and return
72  *              a tree of ops.
73  *
74  ******************************************************************************/
75
76 acpi_status acpi_ps_parse_loop(struct acpi_walk_state *walk_state)
77 {
78         acpi_status status = AE_OK;
79         acpi_status status2;
80         union acpi_parse_object *op = NULL;     /* current op */
81         union acpi_parse_object *arg = NULL;
82         union acpi_parse_object *pre_op = NULL;
83         struct acpi_parse_state *parser_state;
84         u8 *aml_op_start = NULL;
85
86         ACPI_FUNCTION_TRACE_PTR("ps_parse_loop", walk_state);
87
88         if (walk_state->descending_callback == NULL) {
89                 return_ACPI_STATUS(AE_BAD_PARAMETER);
90         }
91
92         parser_state = &walk_state->parser_state;
93         walk_state->arg_types = 0;
94
95 #if (!defined (ACPI_NO_METHOD_EXECUTION) && !defined (ACPI_CONSTANT_EVAL_ONLY))
96
97         if (walk_state->walk_type & ACPI_WALK_METHOD_RESTART) {
98                 /* We are restarting a preempted control method */
99
100                 if (acpi_ps_has_completed_scope(parser_state)) {
101                         /*
102                          * We must check if a predicate to an IF or WHILE statement
103                          * was just completed
104                          */
105                         if ((parser_state->scope->parse_scope.op) &&
106                             ((parser_state->scope->parse_scope.op->common.
107                               aml_opcode == AML_IF_OP)
108                              || (parser_state->scope->parse_scope.op->common.
109                                  aml_opcode == AML_WHILE_OP))
110                             && (walk_state->control_state)
111                             && (walk_state->control_state->common.state ==
112                                 ACPI_CONTROL_PREDICATE_EXECUTING)) {
113                                 /*
114                                  * A predicate was just completed, get the value of the
115                                  * predicate and branch based on that value
116                                  */
117                                 walk_state->op = NULL;
118                                 status =
119                                     acpi_ds_get_predicate_value(walk_state,
120                                                                 ACPI_TO_POINTER
121                                                                 (TRUE));
122                                 if (ACPI_FAILURE(status)
123                                     && ((status & AE_CODE_MASK) !=
124                                         AE_CODE_CONTROL)) {
125                                         if (status == AE_AML_NO_RETURN_VALUE) {
126                                                 ACPI_EXCEPTION((AE_INFO, status,
127                                                                 "Invoked method did not return a value"));
128
129                                         }
130                                         ACPI_EXCEPTION((AE_INFO, status,
131                                                         "get_predicate Failed"));
132                                         return_ACPI_STATUS(status);
133                                 }
134
135                                 status =
136                                     acpi_ps_next_parse_state(walk_state, op,
137                                                              status);
138                         }
139
140                         acpi_ps_pop_scope(parser_state, &op,
141                                           &walk_state->arg_types,
142                                           &walk_state->arg_count);
143                         ACPI_DEBUG_PRINT((ACPI_DB_PARSE,
144                                           "Popped scope, Op=%p\n", op));
145                 } else if (walk_state->prev_op) {
146                         /* We were in the middle of an op */
147
148                         op = walk_state->prev_op;
149                         walk_state->arg_types = walk_state->prev_arg_types;
150                 }
151         }
152 #endif
153
154         /* Iterative parsing loop, while there is more AML to process: */
155
156         while ((parser_state->aml < parser_state->aml_end) || (op)) {
157                 aml_op_start = parser_state->aml;
158                 if (!op) {
159                         /* Get the next opcode from the AML stream */
160
161                         walk_state->aml_offset =
162                             (u32) ACPI_PTR_DIFF(parser_state->aml,
163                                                 parser_state->aml_start);
164                         walk_state->opcode = acpi_ps_peek_opcode(parser_state);
165
166                         /*
167                          * First cut to determine what we have found:
168                          * 1) A valid AML opcode
169                          * 2) A name string
170                          * 3) An unknown/invalid opcode
171                          */
172                         walk_state->op_info =
173                             acpi_ps_get_opcode_info(walk_state->opcode);
174                         switch (walk_state->op_info->class) {
175                         case AML_CLASS_ASCII:
176                         case AML_CLASS_PREFIX:
177                                 /*
178                                  * Starts with a valid prefix or ASCII char, this is a name
179                                  * string.  Convert the bare name string to a namepath.
180                                  */
181                                 walk_state->opcode = AML_INT_NAMEPATH_OP;
182                                 walk_state->arg_types = ARGP_NAMESTRING;
183                                 break;
184
185                         case AML_CLASS_UNKNOWN:
186
187                                 /* The opcode is unrecognized.  Just skip unknown opcodes */
188
189                                 ACPI_ERROR((AE_INFO,
190                                             "Found unknown opcode %X at AML address %p offset %X, ignoring",
191                                             walk_state->opcode,
192                                             parser_state->aml,
193                                             walk_state->aml_offset));
194
195                                 ACPI_DUMP_BUFFER(parser_state->aml, 128);
196
197                                 /* Assume one-byte bad opcode */
198
199                                 parser_state->aml++;
200                                 continue;
201
202                         default:
203
204                                 /* Found opcode info, this is a normal opcode */
205
206                                 parser_state->aml +=
207                                     acpi_ps_get_opcode_size(walk_state->opcode);
208                                 walk_state->arg_types =
209                                     walk_state->op_info->parse_args;
210                                 break;
211                         }
212
213                         /* Create Op structure and append to parent's argument list */
214
215                         if (walk_state->op_info->flags & AML_NAMED) {
216                                 /* Allocate a new pre_op if necessary */
217
218                                 if (!pre_op) {
219                                         pre_op =
220                                             acpi_ps_alloc_op(walk_state->
221                                                              opcode);
222                                         if (!pre_op) {
223                                                 status = AE_NO_MEMORY;
224                                                 goto close_this_op;
225                                         }
226                                 }
227
228                                 pre_op->common.value.arg = NULL;
229                                 pre_op->common.aml_opcode = walk_state->opcode;
230
231                                 /*
232                                  * Get and append arguments until we find the node that contains
233                                  * the name (the type ARGP_NAME).
234                                  */
235                                 while (GET_CURRENT_ARG_TYPE
236                                        (walk_state->arg_types)
237                                        &&
238                                        (GET_CURRENT_ARG_TYPE
239                                         (walk_state->arg_types) != ARGP_NAME)) {
240                                         status =
241                                             acpi_ps_get_next_arg(walk_state,
242                                                                  parser_state,
243                                                                  GET_CURRENT_ARG_TYPE
244                                                                  (walk_state->
245                                                                   arg_types),
246                                                                  &arg);
247                                         if (ACPI_FAILURE(status)) {
248                                                 goto close_this_op;
249                                         }
250
251                                         acpi_ps_append_arg(pre_op, arg);
252                                         INCREMENT_ARG_LIST(walk_state->
253                                                            arg_types);
254                                 }
255
256                                 /*
257                                  * Make sure that we found a NAME and didn't run out of
258                                  * arguments
259                                  */
260                                 if (!GET_CURRENT_ARG_TYPE
261                                     (walk_state->arg_types)) {
262                                         status = AE_AML_NO_OPERAND;
263                                         goto close_this_op;
264                                 }
265
266                                 /* We know that this arg is a name, move to next arg */
267
268                                 INCREMENT_ARG_LIST(walk_state->arg_types);
269
270                                 /*
271                                  * Find the object.  This will either insert the object into
272                                  * the namespace or simply look it up
273                                  */
274                                 walk_state->op = NULL;
275
276                                 status =
277                                     walk_state->descending_callback(walk_state,
278                                                                     &op);
279                                 if (ACPI_FAILURE(status)) {
280                                         ACPI_EXCEPTION((AE_INFO, status,
281                                                         "During name lookup/catalog"));
282                                         goto close_this_op;
283                                 }
284
285                                 if (!op) {
286                                         continue;
287                                 }
288
289                                 status =
290                                     acpi_ps_next_parse_state(walk_state, op,
291                                                              status);
292                                 if (status == AE_CTRL_PENDING) {
293                                         status = AE_OK;
294                                         goto close_this_op;
295                                 }
296
297                                 if (ACPI_FAILURE(status)) {
298                                         goto close_this_op;
299                                 }
300
301                                 acpi_ps_append_arg(op,
302                                                    pre_op->common.value.arg);
303                                 acpi_gbl_depth++;
304
305                                 if (op->common.aml_opcode == AML_REGION_OP) {
306                                         /*
307                                          * Defer final parsing of an operation_region body,
308                                          * because we don't have enough info in the first pass
309                                          * to parse it correctly (i.e., there may be method
310                                          * calls within the term_arg elements of the body.)
311                                          *
312                                          * However, we must continue parsing because
313                                          * the opregion is not a standalone package --
314                                          * we don't know where the end is at this point.
315                                          *
316                                          * (Length is unknown until parse of the body complete)
317                                          */
318                                         op->named.data = aml_op_start;
319                                         op->named.length = 0;
320                                 }
321                         } else {
322                                 /* Not a named opcode, just allocate Op and append to parent */
323
324                                 walk_state->op_info =
325                                     acpi_ps_get_opcode_info(walk_state->opcode);
326                                 op = acpi_ps_alloc_op(walk_state->opcode);
327                                 if (!op) {
328                                         status = AE_NO_MEMORY;
329                                         goto close_this_op;
330                                 }
331
332                                 if (walk_state->op_info->flags & AML_CREATE) {
333                                         /*
334                                          * Backup to beginning of create_xXXfield declaration
335                                          * body_length is unknown until we parse the body
336                                          */
337                                         op->named.data = aml_op_start;
338                                         op->named.length = 0;
339                                 }
340
341                                 acpi_ps_append_arg(acpi_ps_get_parent_scope
342                                                    (parser_state), op);
343
344                                 if ((walk_state->descending_callback != NULL)) {
345                                         /*
346                                          * Find the object. This will either insert the object into
347                                          * the namespace or simply look it up
348                                          */
349                                         walk_state->op = op;
350
351                                         status =
352                                             walk_state->
353                                             descending_callback(walk_state,
354                                                                 &op);
355                                         status =
356                                             acpi_ps_next_parse_state(walk_state,
357                                                                      op,
358                                                                      status);
359                                         if (status == AE_CTRL_PENDING) {
360                                                 status = AE_OK;
361                                                 goto close_this_op;
362                                         }
363
364                                         if (ACPI_FAILURE(status)) {
365                                                 goto close_this_op;
366                                         }
367                                 }
368                         }
369
370                         op->common.aml_offset = walk_state->aml_offset;
371
372                         if (walk_state->op_info) {
373                                 ACPI_DEBUG_PRINT((ACPI_DB_PARSE,
374                                                   "Opcode %4.4X [%s] Op %p Aml %p aml_offset %5.5X\n",
375                                                   (u32) op->common.aml_opcode,
376                                                   walk_state->op_info->name, op,
377                                                   parser_state->aml,
378                                                   op->common.aml_offset));
379                         }
380                 }
381
382                 /*
383                  * Start arg_count at zero because we don't know if there are
384                  * any args yet
385                  */
386                 walk_state->arg_count = 0;
387
388                 /* Are there any arguments that must be processed? */
389
390                 if (walk_state->arg_types) {
391                         /* Get arguments */
392
393                         switch (op->common.aml_opcode) {
394                         case AML_BYTE_OP:       /* AML_BYTEDATA_ARG */
395                         case AML_WORD_OP:       /* AML_WORDDATA_ARG */
396                         case AML_DWORD_OP:      /* AML_DWORDATA_ARG */
397                         case AML_QWORD_OP:      /* AML_QWORDATA_ARG */
398                         case AML_STRING_OP:     /* AML_ASCIICHARLIST_ARG */
399
400                                 /* Fill in constant or string argument directly */
401
402                                 acpi_ps_get_next_simple_arg(parser_state,
403                                                             GET_CURRENT_ARG_TYPE
404                                                             (walk_state->
405                                                              arg_types), op);
406                                 break;
407
408                         case AML_INT_NAMEPATH_OP:       /* AML_NAMESTRING_ARG */
409
410                                 status =
411                                     acpi_ps_get_next_namepath(walk_state,
412                                                               parser_state, op,
413                                                               1);
414                                 if (ACPI_FAILURE(status)) {
415                                         goto close_this_op;
416                                 }
417
418                                 walk_state->arg_types = 0;
419                                 break;
420
421                         default:
422                                 /*
423                                  * Op is not a constant or string, append each argument
424                                  * to the Op
425                                  */
426                                 while (GET_CURRENT_ARG_TYPE
427                                        (walk_state->arg_types)
428                                        && !walk_state->arg_count) {
429                                         walk_state->aml_offset = (u32)
430                                             ACPI_PTR_DIFF(parser_state->aml,
431                                                           parser_state->
432                                                           aml_start);
433
434                                         status =
435                                             acpi_ps_get_next_arg(walk_state,
436                                                                  parser_state,
437                                                                  GET_CURRENT_ARG_TYPE
438                                                                  (walk_state->
439                                                                   arg_types),
440                                                                  &arg);
441                                         if (ACPI_FAILURE(status)) {
442                                                 goto close_this_op;
443                                         }
444
445                                         if (arg) {
446                                                 arg->common.aml_offset =
447                                                     walk_state->aml_offset;
448                                                 acpi_ps_append_arg(op, arg);
449                                         }
450                                         INCREMENT_ARG_LIST(walk_state->
451                                                            arg_types);
452                                 }
453
454                                 /* Special processing for certain opcodes */
455
456                                 /* TBD (remove): Temporary mechanism to disable this code if needed */
457
458 #ifdef ACPI_ENABLE_MODULE_LEVEL_CODE
459
460                                 if ((walk_state->pass_number <=
461                                      ACPI_IMODE_LOAD_PASS1)
462                                     &&
463                                     ((walk_state->
464                                       parse_flags & ACPI_PARSE_DISASSEMBLE) ==
465                                      0)) {
466                                         /*
467                                          * We want to skip If/Else/While constructs during Pass1
468                                          * because we want to actually conditionally execute the
469                                          * code during Pass2.
470                                          *
471                                          * Except for disassembly, where we always want to
472                                          * walk the If/Else/While packages
473                                          */
474                                         switch (op->common.aml_opcode) {
475                                         case AML_IF_OP:
476                                         case AML_ELSE_OP:
477                                         case AML_WHILE_OP:
478
479                                                 ACPI_DEBUG_PRINT((ACPI_DB_PARSE,
480                                                                   "Pass1: Skipping an If/Else/While body\n"));
481
482                                                 /* Skip body of if/else/while in pass 1 */
483
484                                                 parser_state->aml =
485                                                     parser_state->pkg_end;
486                                                 walk_state->arg_count = 0;
487                                                 break;
488
489                                         default:
490                                                 break;
491                                         }
492                                 }
493 #endif
494                                 switch (op->common.aml_opcode) {
495                                 case AML_METHOD_OP:
496
497                                         /*
498                                          * Skip parsing of control method
499                                          * because we don't have enough info in the first pass
500                                          * to parse it correctly.
501                                          *
502                                          * Save the length and address of the body
503                                          */
504                                         op->named.data = parser_state->aml;
505                                         op->named.length =
506                                             (u32) (parser_state->pkg_end -
507                                                    parser_state->aml);
508
509                                         /* Skip body of method */
510
511                                         parser_state->aml =
512                                             parser_state->pkg_end;
513                                         walk_state->arg_count = 0;
514                                         break;
515
516                                 case AML_BUFFER_OP:
517                                 case AML_PACKAGE_OP:
518                                 case AML_VAR_PACKAGE_OP:
519
520                                         if ((op->common.parent) &&
521                                             (op->common.parent->common.
522                                              aml_opcode == AML_NAME_OP)
523                                             && (walk_state->pass_number <=
524                                                 ACPI_IMODE_LOAD_PASS2)) {
525                                                 /*
526                                                  * Skip parsing of Buffers and Packages
527                                                  * because we don't have enough info in the first pass
528                                                  * to parse them correctly.
529                                                  */
530                                                 op->named.data = aml_op_start;
531                                                 op->named.length =
532                                                     (u32) (parser_state->
533                                                            pkg_end -
534                                                            aml_op_start);
535
536                                                 /* Skip body */
537
538                                                 parser_state->aml =
539                                                     parser_state->pkg_end;
540                                                 walk_state->arg_count = 0;
541                                         }
542                                         break;
543
544                                 case AML_WHILE_OP:
545
546                                         if (walk_state->control_state) {
547                                                 walk_state->control_state->
548                                                     control.package_end =
549                                                     parser_state->pkg_end;
550                                         }
551                                         break;
552
553                                 default:
554
555                                         /* No action for all other opcodes */
556                                         break;
557                                 }
558                                 break;
559                         }
560                 }
561
562                 /* Check for arguments that need to be processed */
563
564                 if (walk_state->arg_count) {
565                         /*
566                          * There are arguments (complex ones), push Op and
567                          * prepare for argument
568                          */
569                         status = acpi_ps_push_scope(parser_state, op,
570                                                     walk_state->arg_types,
571                                                     walk_state->arg_count);
572                         if (ACPI_FAILURE(status)) {
573                                 goto close_this_op;
574                         }
575                         op = NULL;
576                         continue;
577                 }
578
579                 /*
580                  * All arguments have been processed -- Op is complete,
581                  * prepare for next
582                  */
583                 walk_state->op_info =
584                     acpi_ps_get_opcode_info(op->common.aml_opcode);
585                 if (walk_state->op_info->flags & AML_NAMED) {
586                         if (acpi_gbl_depth) {
587                                 acpi_gbl_depth--;
588                         }
589
590                         if (op->common.aml_opcode == AML_REGION_OP) {
591                                 /*
592                                  * Skip parsing of control method or opregion body,
593                                  * because we don't have enough info in the first pass
594                                  * to parse them correctly.
595                                  *
596                                  * Completed parsing an op_region declaration, we now
597                                  * know the length.
598                                  */
599                                 op->named.length =
600                                     (u32) (parser_state->aml - op->named.data);
601                         }
602                 }
603
604                 if (walk_state->op_info->flags & AML_CREATE) {
605                         /*
606                          * Backup to beginning of create_xXXfield declaration (1 for
607                          * Opcode)
608                          *
609                          * body_length is unknown until we parse the body
610                          */
611                         op->named.length =
612                             (u32) (parser_state->aml - op->named.data);
613                 }
614
615                 /* This op complete, notify the dispatcher */
616
617                 if (walk_state->ascending_callback != NULL) {
618                         walk_state->op = op;
619                         walk_state->opcode = op->common.aml_opcode;
620
621                         status = walk_state->ascending_callback(walk_state);
622                         status =
623                             acpi_ps_next_parse_state(walk_state, op, status);
624                         if (status == AE_CTRL_PENDING) {
625                                 status = AE_OK;
626                                 goto close_this_op;
627                         }
628                 }
629
630               close_this_op:
631                 /*
632                  * Finished one argument of the containing scope
633                  */
634                 parser_state->scope->parse_scope.arg_count--;
635
636                 /* Finished with pre_op */
637
638                 if (pre_op) {
639                         acpi_ps_free_op(pre_op);
640                         pre_op = NULL;
641                 }
642
643                 /* Close this Op (will result in parse subtree deletion) */
644
645                 status2 = acpi_ps_complete_this_op(walk_state, op);
646                 if (ACPI_FAILURE(status2)) {
647                         return_ACPI_STATUS(status2);
648                 }
649                 op = NULL;
650
651                 switch (status) {
652                 case AE_OK:
653                         break;
654
655                 case AE_CTRL_TRANSFER:
656
657                         /* We are about to transfer to a called method. */
658
659                         walk_state->prev_op = op;
660                         walk_state->prev_arg_types = walk_state->arg_types;
661                         return_ACPI_STATUS(status);
662
663                 case AE_CTRL_END:
664
665                         acpi_ps_pop_scope(parser_state, &op,
666                                           &walk_state->arg_types,
667                                           &walk_state->arg_count);
668
669                         if (op) {
670                                 walk_state->op = op;
671                                 walk_state->op_info =
672                                     acpi_ps_get_opcode_info(op->common.
673                                                             aml_opcode);
674                                 walk_state->opcode = op->common.aml_opcode;
675
676                                 status =
677                                     walk_state->ascending_callback(walk_state);
678                                 status =
679                                     acpi_ps_next_parse_state(walk_state, op,
680                                                              status);
681
682                                 status2 =
683                                     acpi_ps_complete_this_op(walk_state, op);
684                                 if (ACPI_FAILURE(status2)) {
685                                         return_ACPI_STATUS(status2);
686                                 }
687                                 op = NULL;
688                         }
689                         status = AE_OK;
690                         break;
691
692                 case AE_CTRL_BREAK:
693                 case AE_CTRL_CONTINUE:
694
695                         /* Pop off scopes until we find the While */
696
697                         while (!op || (op->common.aml_opcode != AML_WHILE_OP)) {
698                                 acpi_ps_pop_scope(parser_state, &op,
699                                                   &walk_state->arg_types,
700                                                   &walk_state->arg_count);
701
702                                 if (op->common.aml_opcode != AML_WHILE_OP) {
703                                         status2 =
704                                             acpi_ds_result_stack_pop
705                                             (walk_state);
706                                         if (ACPI_FAILURE(status2)) {
707                                                 return_ACPI_STATUS(status2);
708                                         }
709                                 }
710                         }
711
712                         /* Close this iteration of the While loop */
713
714                         walk_state->op = op;
715                         walk_state->op_info =
716                             acpi_ps_get_opcode_info(op->common.aml_opcode);
717                         walk_state->opcode = op->common.aml_opcode;
718
719                         status = walk_state->ascending_callback(walk_state);
720                         status =
721                             acpi_ps_next_parse_state(walk_state, op, status);
722
723                         status2 = acpi_ps_complete_this_op(walk_state, op);
724                         if (ACPI_FAILURE(status2)) {
725                                 return_ACPI_STATUS(status2);
726                         }
727                         op = NULL;
728
729                         status = AE_OK;
730                         break;
731
732                 case AE_CTRL_TERMINATE:
733
734                         status = AE_OK;
735
736                         /* Clean up */
737                         do {
738                                 if (op) {
739                                         status2 =
740                                             acpi_ps_complete_this_op(walk_state,
741                                                                      op);
742                                         if (ACPI_FAILURE(status2)) {
743                                                 return_ACPI_STATUS(status2);
744                                         }
745                                 }
746                                 acpi_ps_pop_scope(parser_state, &op,
747                                                   &walk_state->arg_types,
748                                                   &walk_state->arg_count);
749
750                         } while (op);
751
752                         return_ACPI_STATUS(status);
753
754                 default:        /* All other non-AE_OK status */
755
756                         do {
757                                 if (op) {
758                                         status2 =
759                                             acpi_ps_complete_this_op(walk_state,
760                                                                      op);
761                                         if (ACPI_FAILURE(status2)) {
762                                                 return_ACPI_STATUS(status2);
763                                         }
764                                 }
765                                 acpi_ps_pop_scope(parser_state, &op,
766                                                   &walk_state->arg_types,
767                                                   &walk_state->arg_count);
768
769                         } while (op);
770
771                         /*
772                          * TBD: Cleanup parse ops on error
773                          */
774 #if 0
775                         if (op == NULL) {
776                                 acpi_ps_pop_scope(parser_state, &op,
777                                                   &walk_state->arg_types,
778                                                   &walk_state->arg_count);
779                         }
780 #endif
781                         walk_state->prev_op = op;
782                         walk_state->prev_arg_types = walk_state->arg_types;
783                         return_ACPI_STATUS(status);
784                 }
785
786                 /* This scope complete? */
787
788                 if (acpi_ps_has_completed_scope(parser_state)) {
789                         acpi_ps_pop_scope(parser_state, &op,
790                                           &walk_state->arg_types,
791                                           &walk_state->arg_count);
792                         ACPI_DEBUG_PRINT((ACPI_DB_PARSE,
793                                           "Popped scope, Op=%p\n", op));
794                 } else {
795                         op = NULL;
796                 }
797
798         }                       /* while parser_state->Aml */
799
800         /*
801          * Complete the last Op (if not completed), and clear the scope stack.
802          * It is easily possible to end an AML "package" with an unbounded number
803          * of open scopes (such as when several ASL blocks are closed with
804          * sequential closing braces).  We want to terminate each one cleanly.
805          */
806         ACPI_DEBUG_PRINT((ACPI_DB_PARSE, "AML package complete at Op %p\n",
807                           op));
808         do {
809                 if (op) {
810                         if (walk_state->ascending_callback != NULL) {
811                                 walk_state->op = op;
812                                 walk_state->op_info =
813                                     acpi_ps_get_opcode_info(op->common.
814                                                             aml_opcode);
815                                 walk_state->opcode = op->common.aml_opcode;
816
817                                 status =
818                                     walk_state->ascending_callback(walk_state);
819                                 status =
820                                     acpi_ps_next_parse_state(walk_state, op,
821                                                              status);
822                                 if (status == AE_CTRL_PENDING) {
823                                         status = AE_OK;
824                                         goto close_this_op;
825                                 }
826
827                                 if (status == AE_CTRL_TERMINATE) {
828                                         status = AE_OK;
829
830                                         /* Clean up */
831                                         do {
832                                                 if (op) {
833                                                         status2 =
834                                                             acpi_ps_complete_this_op
835                                                             (walk_state, op);
836                                                         if (ACPI_FAILURE
837                                                             (status2)) {
838                                                                 return_ACPI_STATUS
839                                                                     (status2);
840                                                         }
841                                                 }
842
843                                                 acpi_ps_pop_scope(parser_state,
844                                                                   &op,
845                                                                   &walk_state->
846                                                                   arg_types,
847                                                                   &walk_state->
848                                                                   arg_count);
849
850                                         } while (op);
851
852                                         return_ACPI_STATUS(status);
853                                 }
854
855                                 else if (ACPI_FAILURE(status)) {
856                                         /* First error is most important */
857
858                                         (void)
859                                             acpi_ps_complete_this_op(walk_state,
860                                                                      op);
861                                         return_ACPI_STATUS(status);
862                                 }
863                         }
864
865                         status2 = acpi_ps_complete_this_op(walk_state, op);
866                         if (ACPI_FAILURE(status2)) {
867                                 return_ACPI_STATUS(status2);
868                         }
869                 }
870
871                 acpi_ps_pop_scope(parser_state, &op, &walk_state->arg_types,
872                                   &walk_state->arg_count);
873
874         } while (op);
875
876         return_ACPI_STATUS(status);
877 }