1c2c66affSColin Finck /******************************************************************************
2c2c66affSColin Finck  *
3c2c66affSColin Finck  * Module Name: nswalk - Functions for walking the ACPI namespace
4c2c66affSColin Finck  *
5c2c66affSColin Finck  *****************************************************************************/
6c2c66affSColin Finck 
7c2c66affSColin Finck /*
8*03b24380SThomas Faber  * Copyright (C) 2000 - 2022, Intel Corp.
9c2c66affSColin Finck  * All rights reserved.
10c2c66affSColin Finck  *
11c2c66affSColin Finck  * Redistribution and use in source and binary forms, with or without
12c2c66affSColin Finck  * modification, are permitted provided that the following conditions
13c2c66affSColin Finck  * are met:
14c2c66affSColin Finck  * 1. Redistributions of source code must retain the above copyright
15c2c66affSColin Finck  *    notice, this list of conditions, and the following disclaimer,
16c2c66affSColin Finck  *    without modification.
17c2c66affSColin Finck  * 2. Redistributions in binary form must reproduce at minimum a disclaimer
18c2c66affSColin Finck  *    substantially similar to the "NO WARRANTY" disclaimer below
19c2c66affSColin Finck  *    ("Disclaimer") and any redistribution must be conditioned upon
20c2c66affSColin Finck  *    including a substantially similar Disclaimer requirement for further
21c2c66affSColin Finck  *    binary redistribution.
22c2c66affSColin Finck  * 3. Neither the names of the above-listed copyright holders nor the names
23c2c66affSColin Finck  *    of any contributors may be used to endorse or promote products derived
24c2c66affSColin Finck  *    from this software without specific prior written permission.
25c2c66affSColin Finck  *
26c2c66affSColin Finck  * Alternatively, this software may be distributed under the terms of the
27c2c66affSColin Finck  * GNU General Public License ("GPL") version 2 as published by the Free
28c2c66affSColin Finck  * Software Foundation.
29c2c66affSColin Finck  *
30c2c66affSColin Finck  * NO WARRANTY
31c2c66affSColin Finck  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
32c2c66affSColin Finck  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
336eb8cc49SThomas Faber  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
34c2c66affSColin Finck  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
35c2c66affSColin Finck  * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
36c2c66affSColin Finck  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
37c2c66affSColin Finck  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
38c2c66affSColin Finck  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
39c2c66affSColin Finck  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
40c2c66affSColin Finck  * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
41c2c66affSColin Finck  * POSSIBILITY OF SUCH DAMAGES.
42c2c66affSColin Finck  */
43c2c66affSColin Finck 
44c2c66affSColin Finck #include "acpi.h"
45c2c66affSColin Finck #include "accommon.h"
46c2c66affSColin Finck #include "acnamesp.h"
47c2c66affSColin Finck 
48c2c66affSColin Finck 
49c2c66affSColin Finck #define _COMPONENT          ACPI_NAMESPACE
50c2c66affSColin Finck         ACPI_MODULE_NAME    ("nswalk")
51c2c66affSColin Finck 
52c2c66affSColin Finck 
53c2c66affSColin Finck /*******************************************************************************
54c2c66affSColin Finck  *
55c2c66affSColin Finck  * FUNCTION:    AcpiNsGetNextNode
56c2c66affSColin Finck  *
57c2c66affSColin Finck  * PARAMETERS:  ParentNode          - Parent node whose children we are
58c2c66affSColin Finck  *                                    getting
59c2c66affSColin Finck  *              ChildNode           - Previous child that was found.
60c2c66affSColin Finck  *                                    The NEXT child will be returned
61c2c66affSColin Finck  *
62c2c66affSColin Finck  * RETURN:      ACPI_NAMESPACE_NODE - Pointer to the NEXT child or NULL if
63c2c66affSColin Finck  *                                    none is found.
64c2c66affSColin Finck  *
65c2c66affSColin Finck  * DESCRIPTION: Return the next peer node within the namespace. If Handle
66c2c66affSColin Finck  *              is valid, Scope is ignored. Otherwise, the first node
67c2c66affSColin Finck  *              within Scope is returned.
68c2c66affSColin Finck  *
69c2c66affSColin Finck  ******************************************************************************/
70c2c66affSColin Finck 
71c2c66affSColin Finck ACPI_NAMESPACE_NODE *
AcpiNsGetNextNode(ACPI_NAMESPACE_NODE * ParentNode,ACPI_NAMESPACE_NODE * ChildNode)72c2c66affSColin Finck AcpiNsGetNextNode (
73c2c66affSColin Finck     ACPI_NAMESPACE_NODE     *ParentNode,
74c2c66affSColin Finck     ACPI_NAMESPACE_NODE     *ChildNode)
75c2c66affSColin Finck {
76c2c66affSColin Finck     ACPI_FUNCTION_ENTRY ();
77c2c66affSColin Finck 
78c2c66affSColin Finck 
79c2c66affSColin Finck     if (!ChildNode)
80c2c66affSColin Finck     {
81c2c66affSColin Finck         /* It's really the parent's _scope_ that we want */
82c2c66affSColin Finck 
83c2c66affSColin Finck         return (ParentNode->Child);
84c2c66affSColin Finck     }
85c2c66affSColin Finck 
86c2c66affSColin Finck     /* Otherwise just return the next peer */
87c2c66affSColin Finck 
88c2c66affSColin Finck     return (ChildNode->Peer);
89c2c66affSColin Finck }
90c2c66affSColin Finck 
91c2c66affSColin Finck 
92c2c66affSColin Finck /*******************************************************************************
93c2c66affSColin Finck  *
94c2c66affSColin Finck  * FUNCTION:    AcpiNsGetNextNodeTyped
95c2c66affSColin Finck  *
96c2c66affSColin Finck  * PARAMETERS:  Type                - Type of node to be searched for
97c2c66affSColin Finck  *              ParentNode          - Parent node whose children we are
98c2c66affSColin Finck  *                                    getting
99c2c66affSColin Finck  *              ChildNode           - Previous child that was found.
100c2c66affSColin Finck  *                                    The NEXT child will be returned
101c2c66affSColin Finck  *
102c2c66affSColin Finck  * RETURN:      ACPI_NAMESPACE_NODE - Pointer to the NEXT child or NULL if
103c2c66affSColin Finck  *                                    none is found.
104c2c66affSColin Finck  *
105c2c66affSColin Finck  * DESCRIPTION: Return the next peer node within the namespace. If Handle
106c2c66affSColin Finck  *              is valid, Scope is ignored. Otherwise, the first node
107c2c66affSColin Finck  *              within Scope is returned.
108c2c66affSColin Finck  *
109c2c66affSColin Finck  ******************************************************************************/
110c2c66affSColin Finck 
111c2c66affSColin Finck ACPI_NAMESPACE_NODE *
AcpiNsGetNextNodeTyped(ACPI_OBJECT_TYPE Type,ACPI_NAMESPACE_NODE * ParentNode,ACPI_NAMESPACE_NODE * ChildNode)112c2c66affSColin Finck AcpiNsGetNextNodeTyped (
113c2c66affSColin Finck     ACPI_OBJECT_TYPE        Type,
114c2c66affSColin Finck     ACPI_NAMESPACE_NODE     *ParentNode,
115c2c66affSColin Finck     ACPI_NAMESPACE_NODE     *ChildNode)
116c2c66affSColin Finck {
117c2c66affSColin Finck     ACPI_NAMESPACE_NODE     *NextNode = NULL;
118c2c66affSColin Finck 
119c2c66affSColin Finck 
120c2c66affSColin Finck     ACPI_FUNCTION_ENTRY ();
121c2c66affSColin Finck 
122c2c66affSColin Finck 
123c2c66affSColin Finck     NextNode = AcpiNsGetNextNode (ParentNode, ChildNode);
124c2c66affSColin Finck 
125c2c66affSColin Finck     /* If any type is OK, we are done */
126c2c66affSColin Finck 
127c2c66affSColin Finck     if (Type == ACPI_TYPE_ANY)
128c2c66affSColin Finck     {
129c2c66affSColin Finck         /* NextNode is NULL if we are at the end-of-list */
130c2c66affSColin Finck 
131c2c66affSColin Finck         return (NextNode);
132c2c66affSColin Finck     }
133c2c66affSColin Finck 
134c2c66affSColin Finck     /* Must search for the node -- but within this scope only */
135c2c66affSColin Finck 
136c2c66affSColin Finck     while (NextNode)
137c2c66affSColin Finck     {
138c2c66affSColin Finck         /* If type matches, we are done */
139c2c66affSColin Finck 
140c2c66affSColin Finck         if (NextNode->Type == Type)
141c2c66affSColin Finck         {
142c2c66affSColin Finck             return (NextNode);
143c2c66affSColin Finck         }
144c2c66affSColin Finck 
145c2c66affSColin Finck         /* Otherwise, move on to the next peer node */
146c2c66affSColin Finck 
147c2c66affSColin Finck         NextNode = NextNode->Peer;
148c2c66affSColin Finck     }
149c2c66affSColin Finck 
150c2c66affSColin Finck     /* Not found */
151c2c66affSColin Finck 
152c2c66affSColin Finck     return (NULL);
153c2c66affSColin Finck }
154c2c66affSColin Finck 
155c2c66affSColin Finck 
156c2c66affSColin Finck /*******************************************************************************
157c2c66affSColin Finck  *
158c2c66affSColin Finck  * FUNCTION:    AcpiNsWalkNamespace
159c2c66affSColin Finck  *
160c2c66affSColin Finck  * PARAMETERS:  Type                - ACPI_OBJECT_TYPE to search for
161c2c66affSColin Finck  *              StartNode           - Handle in namespace where search begins
162c2c66affSColin Finck  *              MaxDepth            - Depth to which search is to reach
163c2c66affSColin Finck  *              Flags               - Whether to unlock the NS before invoking
164c2c66affSColin Finck  *                                    the callback routine
165c2c66affSColin Finck  *              DescendingCallback  - Called during tree descent
166c2c66affSColin Finck  *                                    when an object of "Type" is found
167c2c66affSColin Finck  *              AscendingCallback   - Called during tree ascent
168c2c66affSColin Finck  *                                    when an object of "Type" is found
169c2c66affSColin Finck  *              Context             - Passed to user function(s) above
170c2c66affSColin Finck  *              ReturnValue         - from the UserFunction if terminated
171c2c66affSColin Finck  *                                    early. Otherwise, returns NULL.
172c2c66affSColin Finck  * RETURNS:     Status
173c2c66affSColin Finck  *
174c2c66affSColin Finck  * DESCRIPTION: Performs a modified depth-first walk of the namespace tree,
175c2c66affSColin Finck  *              starting (and ending) at the node specified by StartHandle.
176c2c66affSColin Finck  *              The callback function is called whenever a node that matches
177c2c66affSColin Finck  *              the type parameter is found. If the callback function returns
178c2c66affSColin Finck  *              a non-zero value, the search is terminated immediately and
179c2c66affSColin Finck  *              this value is returned to the caller.
180c2c66affSColin Finck  *
181c2c66affSColin Finck  *              The point of this procedure is to provide a generic namespace
182c2c66affSColin Finck  *              walk routine that can be called from multiple places to
183c2c66affSColin Finck  *              provide multiple services; the callback function(s) can be
184c2c66affSColin Finck  *              tailored to each task, whether it is a print function,
185c2c66affSColin Finck  *              a compare function, etc.
186c2c66affSColin Finck  *
187c2c66affSColin Finck  ******************************************************************************/
188c2c66affSColin Finck 
189c2c66affSColin Finck ACPI_STATUS
AcpiNsWalkNamespace(ACPI_OBJECT_TYPE Type,ACPI_HANDLE StartNode,UINT32 MaxDepth,UINT32 Flags,ACPI_WALK_CALLBACK DescendingCallback,ACPI_WALK_CALLBACK AscendingCallback,void * Context,void ** ReturnValue)190c2c66affSColin Finck AcpiNsWalkNamespace (
191c2c66affSColin Finck     ACPI_OBJECT_TYPE        Type,
192c2c66affSColin Finck     ACPI_HANDLE             StartNode,
193c2c66affSColin Finck     UINT32                  MaxDepth,
194c2c66affSColin Finck     UINT32                  Flags,
195c2c66affSColin Finck     ACPI_WALK_CALLBACK      DescendingCallback,
196c2c66affSColin Finck     ACPI_WALK_CALLBACK      AscendingCallback,
197c2c66affSColin Finck     void                    *Context,
198c2c66affSColin Finck     void                    **ReturnValue)
199c2c66affSColin Finck {
200c2c66affSColin Finck     ACPI_STATUS             Status;
201c2c66affSColin Finck     ACPI_STATUS             MutexStatus;
202c2c66affSColin Finck     ACPI_NAMESPACE_NODE     *ChildNode;
203c2c66affSColin Finck     ACPI_NAMESPACE_NODE     *ParentNode;
204c2c66affSColin Finck     ACPI_OBJECT_TYPE        ChildType;
205c2c66affSColin Finck     UINT32                  Level;
206c2c66affSColin Finck     BOOLEAN                 NodePreviouslyVisited = FALSE;
207c2c66affSColin Finck 
208c2c66affSColin Finck 
209c2c66affSColin Finck     ACPI_FUNCTION_TRACE (NsWalkNamespace);
210c2c66affSColin Finck 
211c2c66affSColin Finck 
212c2c66affSColin Finck     /* Special case for the namespace Root Node */
213c2c66affSColin Finck 
214c2c66affSColin Finck     if (StartNode == ACPI_ROOT_OBJECT)
215c2c66affSColin Finck     {
216c2c66affSColin Finck         StartNode = AcpiGbl_RootNode;
217*03b24380SThomas Faber         if (!StartNode)
218*03b24380SThomas Faber         {
219*03b24380SThomas Faber             return_ACPI_STATUS (AE_NO_NAMESPACE);
220*03b24380SThomas Faber         }
221c2c66affSColin Finck     }
222c2c66affSColin Finck 
223c2c66affSColin Finck     /* Null child means "get first node" */
224c2c66affSColin Finck 
225c2c66affSColin Finck     ParentNode = StartNode;
226c2c66affSColin Finck     ChildNode = AcpiNsGetNextNode (ParentNode, NULL);
227c2c66affSColin Finck     ChildType = ACPI_TYPE_ANY;
228c2c66affSColin Finck     Level = 1;
229c2c66affSColin Finck 
230c2c66affSColin Finck     /*
231c2c66affSColin Finck      * Traverse the tree of nodes until we bubble back up to where we
232c2c66affSColin Finck      * started. When Level is zero, the loop is done because we have
233c2c66affSColin Finck      * bubbled up to (and passed) the original parent handle (StartEntry)
234c2c66affSColin Finck      */
235c2c66affSColin Finck     while (Level > 0 && ChildNode)
236c2c66affSColin Finck     {
237c2c66affSColin Finck         Status = AE_OK;
238c2c66affSColin Finck 
239c2c66affSColin Finck         /* Found next child, get the type if we are not searching for ANY */
240c2c66affSColin Finck 
241c2c66affSColin Finck         if (Type != ACPI_TYPE_ANY)
242c2c66affSColin Finck         {
243c2c66affSColin Finck             ChildType = ChildNode->Type;
244c2c66affSColin Finck         }
245c2c66affSColin Finck 
246c2c66affSColin Finck         /*
247c2c66affSColin Finck          * Ignore all temporary namespace nodes (created during control
248c2c66affSColin Finck          * method execution) unless told otherwise. These temporary nodes
249c2c66affSColin Finck          * can cause a race condition because they can be deleted during
250c2c66affSColin Finck          * the execution of the user function (if the namespace is
251c2c66affSColin Finck          * unlocked before invocation of the user function.) Only the
252c2c66affSColin Finck          * debugger namespace dump will examine the temporary nodes.
253c2c66affSColin Finck          */
254c2c66affSColin Finck         if ((ChildNode->Flags & ANOBJ_TEMPORARY) &&
255c2c66affSColin Finck             !(Flags & ACPI_NS_WALK_TEMP_NODES))
256c2c66affSColin Finck         {
257c2c66affSColin Finck             Status = AE_CTRL_DEPTH;
258c2c66affSColin Finck         }
259c2c66affSColin Finck 
260c2c66affSColin Finck         /* Type must match requested type */
261c2c66affSColin Finck 
262c2c66affSColin Finck         else if (ChildType == Type)
263c2c66affSColin Finck         {
264c2c66affSColin Finck             /*
265c2c66affSColin Finck              * Found a matching node, invoke the user callback function.
266c2c66affSColin Finck              * Unlock the namespace if flag is set.
267c2c66affSColin Finck              */
268c2c66affSColin Finck             if (Flags & ACPI_NS_WALK_UNLOCK)
269c2c66affSColin Finck             {
270c2c66affSColin Finck                 MutexStatus = AcpiUtReleaseMutex (ACPI_MTX_NAMESPACE);
271c2c66affSColin Finck                 if (ACPI_FAILURE (MutexStatus))
272c2c66affSColin Finck                 {
273c2c66affSColin Finck                     return_ACPI_STATUS (MutexStatus);
274c2c66affSColin Finck                 }
275c2c66affSColin Finck             }
276c2c66affSColin Finck 
277c2c66affSColin Finck             /*
278c2c66affSColin Finck              * Invoke the user function, either descending, ascending,
279c2c66affSColin Finck              * or both.
280c2c66affSColin Finck              */
281c2c66affSColin Finck             if (!NodePreviouslyVisited)
282c2c66affSColin Finck             {
283c2c66affSColin Finck                 if (DescendingCallback)
284c2c66affSColin Finck                 {
285c2c66affSColin Finck                     Status = DescendingCallback (ChildNode, Level,
286c2c66affSColin Finck                         Context, ReturnValue);
287c2c66affSColin Finck                 }
288c2c66affSColin Finck             }
289c2c66affSColin Finck             else
290c2c66affSColin Finck             {
291c2c66affSColin Finck                 if (AscendingCallback)
292c2c66affSColin Finck                 {
293c2c66affSColin Finck                     Status = AscendingCallback (ChildNode, Level,
294c2c66affSColin Finck                         Context, ReturnValue);
295c2c66affSColin Finck                 }
296c2c66affSColin Finck             }
297c2c66affSColin Finck 
298c2c66affSColin Finck             if (Flags & ACPI_NS_WALK_UNLOCK)
299c2c66affSColin Finck             {
300c2c66affSColin Finck                 MutexStatus = AcpiUtAcquireMutex (ACPI_MTX_NAMESPACE);
301c2c66affSColin Finck                 if (ACPI_FAILURE (MutexStatus))
302c2c66affSColin Finck                 {
303c2c66affSColin Finck                     return_ACPI_STATUS (MutexStatus);
304c2c66affSColin Finck                 }
305c2c66affSColin Finck             }
306c2c66affSColin Finck 
307c2c66affSColin Finck             switch (Status)
308c2c66affSColin Finck             {
309c2c66affSColin Finck             case AE_OK:
310c2c66affSColin Finck             case AE_CTRL_DEPTH:
311c2c66affSColin Finck 
312c2c66affSColin Finck                 /* Just keep going */
313c2c66affSColin Finck                 break;
314c2c66affSColin Finck 
315c2c66affSColin Finck             case AE_CTRL_TERMINATE:
316c2c66affSColin Finck 
317c2c66affSColin Finck                 /* Exit now, with OK status */
318c2c66affSColin Finck 
319c2c66affSColin Finck                 return_ACPI_STATUS (AE_OK);
320c2c66affSColin Finck 
321c2c66affSColin Finck             default:
322c2c66affSColin Finck 
323c2c66affSColin Finck                 /* All others are valid exceptions */
324c2c66affSColin Finck 
325c2c66affSColin Finck                 return_ACPI_STATUS (Status);
326c2c66affSColin Finck             }
327c2c66affSColin Finck         }
328c2c66affSColin Finck 
329c2c66affSColin Finck         /*
330c2c66affSColin Finck          * Depth first search: Attempt to go down another level in the
331c2c66affSColin Finck          * namespace if we are allowed to. Don't go any further if we have
332c2c66affSColin Finck          * reached the caller specified maximum depth or if the user
333c2c66affSColin Finck          * function has specified that the maximum depth has been reached.
334c2c66affSColin Finck          */
335c2c66affSColin Finck         if (!NodePreviouslyVisited &&
336c2c66affSColin Finck             (Level < MaxDepth) &&
337c2c66affSColin Finck             (Status != AE_CTRL_DEPTH))
338c2c66affSColin Finck         {
339c2c66affSColin Finck             if (ChildNode->Child)
340c2c66affSColin Finck             {
341c2c66affSColin Finck                 /* There is at least one child of this node, visit it */
342c2c66affSColin Finck 
343c2c66affSColin Finck                 Level++;
344c2c66affSColin Finck                 ParentNode = ChildNode;
345c2c66affSColin Finck                 ChildNode = AcpiNsGetNextNode (ParentNode, NULL);
346c2c66affSColin Finck                 continue;
347c2c66affSColin Finck             }
348c2c66affSColin Finck         }
349c2c66affSColin Finck 
350c2c66affSColin Finck         /* No more children, re-visit this node */
351c2c66affSColin Finck 
352c2c66affSColin Finck         if (!NodePreviouslyVisited)
353c2c66affSColin Finck         {
354c2c66affSColin Finck             NodePreviouslyVisited = TRUE;
355c2c66affSColin Finck             continue;
356c2c66affSColin Finck         }
357c2c66affSColin Finck 
358c2c66affSColin Finck         /* No more children, visit peers */
359c2c66affSColin Finck 
360c2c66affSColin Finck         ChildNode = AcpiNsGetNextNode (ParentNode, ChildNode);
361c2c66affSColin Finck         if (ChildNode)
362c2c66affSColin Finck         {
363c2c66affSColin Finck             NodePreviouslyVisited = FALSE;
364c2c66affSColin Finck         }
365c2c66affSColin Finck 
366c2c66affSColin Finck         /* No peers, re-visit parent */
367c2c66affSColin Finck 
368c2c66affSColin Finck         else
369c2c66affSColin Finck         {
370c2c66affSColin Finck             /*
371c2c66affSColin Finck              * No more children of this node (AcpiNsGetNextNode failed), go
372c2c66affSColin Finck              * back upwards in the namespace tree to the node's parent.
373c2c66affSColin Finck              */
374c2c66affSColin Finck             Level--;
375c2c66affSColin Finck             ChildNode = ParentNode;
376c2c66affSColin Finck             ParentNode = ParentNode->Parent;
377c2c66affSColin Finck 
378c2c66affSColin Finck             NodePreviouslyVisited = TRUE;
379c2c66affSColin Finck         }
380c2c66affSColin Finck     }
381c2c66affSColin Finck 
382c2c66affSColin Finck     /* Complete walk, not terminated by user function */
383c2c66affSColin Finck 
384c2c66affSColin Finck     return_ACPI_STATUS (AE_OK);
385c2c66affSColin Finck }
386