1*4882a593Smuzhiyun // SPDX-License-Identifier: BSD-3-Clause OR GPL-2.0
2*4882a593Smuzhiyun /******************************************************************************
3*4882a593Smuzhiyun *
4*4882a593Smuzhiyun * Module Name: nswalk - Functions for walking the ACPI namespace
5*4882a593Smuzhiyun *
6*4882a593Smuzhiyun * Copyright (C) 2000 - 2020, Intel Corp.
7*4882a593Smuzhiyun *
8*4882a593Smuzhiyun *****************************************************************************/
9*4882a593Smuzhiyun
10*4882a593Smuzhiyun #include <acpi/acpi.h>
11*4882a593Smuzhiyun #include "accommon.h"
12*4882a593Smuzhiyun #include "acnamesp.h"
13*4882a593Smuzhiyun
14*4882a593Smuzhiyun #define _COMPONENT ACPI_NAMESPACE
15*4882a593Smuzhiyun ACPI_MODULE_NAME("nswalk")
16*4882a593Smuzhiyun
17*4882a593Smuzhiyun /*******************************************************************************
18*4882a593Smuzhiyun *
19*4882a593Smuzhiyun * FUNCTION: acpi_ns_get_next_node
20*4882a593Smuzhiyun *
21*4882a593Smuzhiyun * PARAMETERS: parent_node - Parent node whose children we are
22*4882a593Smuzhiyun * getting
23*4882a593Smuzhiyun * child_node - Previous child that was found.
24*4882a593Smuzhiyun * The NEXT child will be returned
25*4882a593Smuzhiyun *
26*4882a593Smuzhiyun * RETURN: struct acpi_namespace_node - Pointer to the NEXT child or NULL if
27*4882a593Smuzhiyun * none is found.
28*4882a593Smuzhiyun *
29*4882a593Smuzhiyun * DESCRIPTION: Return the next peer node within the namespace. If Handle
30*4882a593Smuzhiyun * is valid, Scope is ignored. Otherwise, the first node
31*4882a593Smuzhiyun * within Scope is returned.
32*4882a593Smuzhiyun *
33*4882a593Smuzhiyun ******************************************************************************/
acpi_ns_get_next_node(struct acpi_namespace_node * parent_node,struct acpi_namespace_node * child_node)34*4882a593Smuzhiyun struct acpi_namespace_node *acpi_ns_get_next_node(struct acpi_namespace_node
35*4882a593Smuzhiyun *parent_node,
36*4882a593Smuzhiyun struct acpi_namespace_node
37*4882a593Smuzhiyun *child_node)
38*4882a593Smuzhiyun {
39*4882a593Smuzhiyun ACPI_FUNCTION_ENTRY();
40*4882a593Smuzhiyun
41*4882a593Smuzhiyun if (!child_node) {
42*4882a593Smuzhiyun
43*4882a593Smuzhiyun /* It's really the parent's _scope_ that we want */
44*4882a593Smuzhiyun
45*4882a593Smuzhiyun return (parent_node->child);
46*4882a593Smuzhiyun }
47*4882a593Smuzhiyun
48*4882a593Smuzhiyun /* Otherwise just return the next peer */
49*4882a593Smuzhiyun
50*4882a593Smuzhiyun return (child_node->peer);
51*4882a593Smuzhiyun }
52*4882a593Smuzhiyun
53*4882a593Smuzhiyun /*******************************************************************************
54*4882a593Smuzhiyun *
55*4882a593Smuzhiyun * FUNCTION: acpi_ns_get_next_node_typed
56*4882a593Smuzhiyun *
57*4882a593Smuzhiyun * PARAMETERS: type - Type of node to be searched for
58*4882a593Smuzhiyun * parent_node - Parent node whose children we are
59*4882a593Smuzhiyun * getting
60*4882a593Smuzhiyun * child_node - Previous child that was found.
61*4882a593Smuzhiyun * The NEXT child will be returned
62*4882a593Smuzhiyun *
63*4882a593Smuzhiyun * RETURN: struct acpi_namespace_node - Pointer to the NEXT child or NULL if
64*4882a593Smuzhiyun * none is found.
65*4882a593Smuzhiyun *
66*4882a593Smuzhiyun * DESCRIPTION: Return the next peer node within the namespace. If Handle
67*4882a593Smuzhiyun * is valid, Scope is ignored. Otherwise, the first node
68*4882a593Smuzhiyun * within Scope is returned.
69*4882a593Smuzhiyun *
70*4882a593Smuzhiyun ******************************************************************************/
71*4882a593Smuzhiyun
acpi_ns_get_next_node_typed(acpi_object_type type,struct acpi_namespace_node * parent_node,struct acpi_namespace_node * child_node)72*4882a593Smuzhiyun struct acpi_namespace_node *acpi_ns_get_next_node_typed(acpi_object_type type,
73*4882a593Smuzhiyun struct
74*4882a593Smuzhiyun acpi_namespace_node
75*4882a593Smuzhiyun *parent_node,
76*4882a593Smuzhiyun struct
77*4882a593Smuzhiyun acpi_namespace_node
78*4882a593Smuzhiyun *child_node)
79*4882a593Smuzhiyun {
80*4882a593Smuzhiyun struct acpi_namespace_node *next_node = NULL;
81*4882a593Smuzhiyun
82*4882a593Smuzhiyun ACPI_FUNCTION_ENTRY();
83*4882a593Smuzhiyun
84*4882a593Smuzhiyun next_node = acpi_ns_get_next_node(parent_node, child_node);
85*4882a593Smuzhiyun
86*4882a593Smuzhiyun
87*4882a593Smuzhiyun /* If any type is OK, we are done */
88*4882a593Smuzhiyun
89*4882a593Smuzhiyun if (type == ACPI_TYPE_ANY) {
90*4882a593Smuzhiyun
91*4882a593Smuzhiyun /* next_node is NULL if we are at the end-of-list */
92*4882a593Smuzhiyun
93*4882a593Smuzhiyun return (next_node);
94*4882a593Smuzhiyun }
95*4882a593Smuzhiyun
96*4882a593Smuzhiyun /* Must search for the node -- but within this scope only */
97*4882a593Smuzhiyun
98*4882a593Smuzhiyun while (next_node) {
99*4882a593Smuzhiyun
100*4882a593Smuzhiyun /* If type matches, we are done */
101*4882a593Smuzhiyun
102*4882a593Smuzhiyun if (next_node->type == type) {
103*4882a593Smuzhiyun return (next_node);
104*4882a593Smuzhiyun }
105*4882a593Smuzhiyun
106*4882a593Smuzhiyun /* Otherwise, move on to the next peer node */
107*4882a593Smuzhiyun
108*4882a593Smuzhiyun next_node = next_node->peer;
109*4882a593Smuzhiyun }
110*4882a593Smuzhiyun
111*4882a593Smuzhiyun /* Not found */
112*4882a593Smuzhiyun
113*4882a593Smuzhiyun return (NULL);
114*4882a593Smuzhiyun }
115*4882a593Smuzhiyun
116*4882a593Smuzhiyun /*******************************************************************************
117*4882a593Smuzhiyun *
118*4882a593Smuzhiyun * FUNCTION: acpi_ns_walk_namespace
119*4882a593Smuzhiyun *
120*4882a593Smuzhiyun * PARAMETERS: type - acpi_object_type to search for
121*4882a593Smuzhiyun * start_node - Handle in namespace where search begins
122*4882a593Smuzhiyun * max_depth - Depth to which search is to reach
123*4882a593Smuzhiyun * flags - Whether to unlock the NS before invoking
124*4882a593Smuzhiyun * the callback routine
125*4882a593Smuzhiyun * descending_callback - Called during tree descent
126*4882a593Smuzhiyun * when an object of "Type" is found
127*4882a593Smuzhiyun * ascending_callback - Called during tree ascent
128*4882a593Smuzhiyun * when an object of "Type" is found
129*4882a593Smuzhiyun * context - Passed to user function(s) above
130*4882a593Smuzhiyun * return_value - from the user_function if terminated
131*4882a593Smuzhiyun * early. Otherwise, returns NULL.
132*4882a593Smuzhiyun * RETURNS: Status
133*4882a593Smuzhiyun *
134*4882a593Smuzhiyun * DESCRIPTION: Performs a modified depth-first walk of the namespace tree,
135*4882a593Smuzhiyun * starting (and ending) at the node specified by start_handle.
136*4882a593Smuzhiyun * The callback function is called whenever a node that matches
137*4882a593Smuzhiyun * the type parameter is found. If the callback function returns
138*4882a593Smuzhiyun * a non-zero value, the search is terminated immediately and
139*4882a593Smuzhiyun * this value is returned to the caller.
140*4882a593Smuzhiyun *
141*4882a593Smuzhiyun * The point of this procedure is to provide a generic namespace
142*4882a593Smuzhiyun * walk routine that can be called from multiple places to
143*4882a593Smuzhiyun * provide multiple services; the callback function(s) can be
144*4882a593Smuzhiyun * tailored to each task, whether it is a print function,
145*4882a593Smuzhiyun * a compare function, etc.
146*4882a593Smuzhiyun *
147*4882a593Smuzhiyun ******************************************************************************/
148*4882a593Smuzhiyun
149*4882a593Smuzhiyun acpi_status
acpi_ns_walk_namespace(acpi_object_type type,acpi_handle start_node,u32 max_depth,u32 flags,acpi_walk_callback descending_callback,acpi_walk_callback ascending_callback,void * context,void ** return_value)150*4882a593Smuzhiyun acpi_ns_walk_namespace(acpi_object_type type,
151*4882a593Smuzhiyun acpi_handle start_node,
152*4882a593Smuzhiyun u32 max_depth,
153*4882a593Smuzhiyun u32 flags,
154*4882a593Smuzhiyun acpi_walk_callback descending_callback,
155*4882a593Smuzhiyun acpi_walk_callback ascending_callback,
156*4882a593Smuzhiyun void *context, void **return_value)
157*4882a593Smuzhiyun {
158*4882a593Smuzhiyun acpi_status status;
159*4882a593Smuzhiyun acpi_status mutex_status;
160*4882a593Smuzhiyun struct acpi_namespace_node *child_node;
161*4882a593Smuzhiyun struct acpi_namespace_node *parent_node;
162*4882a593Smuzhiyun acpi_object_type child_type;
163*4882a593Smuzhiyun u32 level;
164*4882a593Smuzhiyun u8 node_previously_visited = FALSE;
165*4882a593Smuzhiyun
166*4882a593Smuzhiyun ACPI_FUNCTION_TRACE(ns_walk_namespace);
167*4882a593Smuzhiyun
168*4882a593Smuzhiyun /* Special case for the namespace Root Node */
169*4882a593Smuzhiyun
170*4882a593Smuzhiyun if (start_node == ACPI_ROOT_OBJECT) {
171*4882a593Smuzhiyun start_node = acpi_gbl_root_node;
172*4882a593Smuzhiyun if (!start_node) {
173*4882a593Smuzhiyun return_ACPI_STATUS(AE_NO_NAMESPACE);
174*4882a593Smuzhiyun }
175*4882a593Smuzhiyun }
176*4882a593Smuzhiyun
177*4882a593Smuzhiyun /* Null child means "get first node" */
178*4882a593Smuzhiyun
179*4882a593Smuzhiyun parent_node = start_node;
180*4882a593Smuzhiyun child_node = acpi_ns_get_next_node(parent_node, NULL);
181*4882a593Smuzhiyun child_type = ACPI_TYPE_ANY;
182*4882a593Smuzhiyun level = 1;
183*4882a593Smuzhiyun
184*4882a593Smuzhiyun /*
185*4882a593Smuzhiyun * Traverse the tree of nodes until we bubble back up to where we
186*4882a593Smuzhiyun * started. When Level is zero, the loop is done because we have
187*4882a593Smuzhiyun * bubbled up to (and passed) the original parent handle (start_entry)
188*4882a593Smuzhiyun */
189*4882a593Smuzhiyun while (level > 0 && child_node) {
190*4882a593Smuzhiyun status = AE_OK;
191*4882a593Smuzhiyun
192*4882a593Smuzhiyun /* Found next child, get the type if we are not searching for ANY */
193*4882a593Smuzhiyun
194*4882a593Smuzhiyun if (type != ACPI_TYPE_ANY) {
195*4882a593Smuzhiyun child_type = child_node->type;
196*4882a593Smuzhiyun }
197*4882a593Smuzhiyun
198*4882a593Smuzhiyun /*
199*4882a593Smuzhiyun * Ignore all temporary namespace nodes (created during control
200*4882a593Smuzhiyun * method execution) unless told otherwise. These temporary nodes
201*4882a593Smuzhiyun * can cause a race condition because they can be deleted during
202*4882a593Smuzhiyun * the execution of the user function (if the namespace is
203*4882a593Smuzhiyun * unlocked before invocation of the user function.) Only the
204*4882a593Smuzhiyun * debugger namespace dump will examine the temporary nodes.
205*4882a593Smuzhiyun */
206*4882a593Smuzhiyun if ((child_node->flags & ANOBJ_TEMPORARY) &&
207*4882a593Smuzhiyun !(flags & ACPI_NS_WALK_TEMP_NODES)) {
208*4882a593Smuzhiyun status = AE_CTRL_DEPTH;
209*4882a593Smuzhiyun }
210*4882a593Smuzhiyun
211*4882a593Smuzhiyun /* Type must match requested type */
212*4882a593Smuzhiyun
213*4882a593Smuzhiyun else if (child_type == type) {
214*4882a593Smuzhiyun /*
215*4882a593Smuzhiyun * Found a matching node, invoke the user callback function.
216*4882a593Smuzhiyun * Unlock the namespace if flag is set.
217*4882a593Smuzhiyun */
218*4882a593Smuzhiyun if (flags & ACPI_NS_WALK_UNLOCK) {
219*4882a593Smuzhiyun mutex_status =
220*4882a593Smuzhiyun acpi_ut_release_mutex(ACPI_MTX_NAMESPACE);
221*4882a593Smuzhiyun if (ACPI_FAILURE(mutex_status)) {
222*4882a593Smuzhiyun return_ACPI_STATUS(mutex_status);
223*4882a593Smuzhiyun }
224*4882a593Smuzhiyun }
225*4882a593Smuzhiyun
226*4882a593Smuzhiyun /*
227*4882a593Smuzhiyun * Invoke the user function, either descending, ascending,
228*4882a593Smuzhiyun * or both.
229*4882a593Smuzhiyun */
230*4882a593Smuzhiyun if (!node_previously_visited) {
231*4882a593Smuzhiyun if (descending_callback) {
232*4882a593Smuzhiyun status =
233*4882a593Smuzhiyun descending_callback(child_node,
234*4882a593Smuzhiyun level, context,
235*4882a593Smuzhiyun return_value);
236*4882a593Smuzhiyun }
237*4882a593Smuzhiyun } else {
238*4882a593Smuzhiyun if (ascending_callback) {
239*4882a593Smuzhiyun status =
240*4882a593Smuzhiyun ascending_callback(child_node,
241*4882a593Smuzhiyun level, context,
242*4882a593Smuzhiyun return_value);
243*4882a593Smuzhiyun }
244*4882a593Smuzhiyun }
245*4882a593Smuzhiyun
246*4882a593Smuzhiyun if (flags & ACPI_NS_WALK_UNLOCK) {
247*4882a593Smuzhiyun mutex_status =
248*4882a593Smuzhiyun acpi_ut_acquire_mutex(ACPI_MTX_NAMESPACE);
249*4882a593Smuzhiyun if (ACPI_FAILURE(mutex_status)) {
250*4882a593Smuzhiyun return_ACPI_STATUS(mutex_status);
251*4882a593Smuzhiyun }
252*4882a593Smuzhiyun }
253*4882a593Smuzhiyun
254*4882a593Smuzhiyun switch (status) {
255*4882a593Smuzhiyun case AE_OK:
256*4882a593Smuzhiyun case AE_CTRL_DEPTH:
257*4882a593Smuzhiyun
258*4882a593Smuzhiyun /* Just keep going */
259*4882a593Smuzhiyun break;
260*4882a593Smuzhiyun
261*4882a593Smuzhiyun case AE_CTRL_TERMINATE:
262*4882a593Smuzhiyun
263*4882a593Smuzhiyun /* Exit now, with OK status */
264*4882a593Smuzhiyun
265*4882a593Smuzhiyun return_ACPI_STATUS(AE_OK);
266*4882a593Smuzhiyun
267*4882a593Smuzhiyun default:
268*4882a593Smuzhiyun
269*4882a593Smuzhiyun /* All others are valid exceptions */
270*4882a593Smuzhiyun
271*4882a593Smuzhiyun return_ACPI_STATUS(status);
272*4882a593Smuzhiyun }
273*4882a593Smuzhiyun }
274*4882a593Smuzhiyun
275*4882a593Smuzhiyun /*
276*4882a593Smuzhiyun * Depth first search: Attempt to go down another level in the
277*4882a593Smuzhiyun * namespace if we are allowed to. Don't go any further if we have
278*4882a593Smuzhiyun * reached the caller specified maximum depth or if the user
279*4882a593Smuzhiyun * function has specified that the maximum depth has been reached.
280*4882a593Smuzhiyun */
281*4882a593Smuzhiyun if (!node_previously_visited &&
282*4882a593Smuzhiyun (level < max_depth) && (status != AE_CTRL_DEPTH)) {
283*4882a593Smuzhiyun if (child_node->child) {
284*4882a593Smuzhiyun
285*4882a593Smuzhiyun /* There is at least one child of this node, visit it */
286*4882a593Smuzhiyun
287*4882a593Smuzhiyun level++;
288*4882a593Smuzhiyun parent_node = child_node;
289*4882a593Smuzhiyun child_node =
290*4882a593Smuzhiyun acpi_ns_get_next_node(parent_node, NULL);
291*4882a593Smuzhiyun continue;
292*4882a593Smuzhiyun }
293*4882a593Smuzhiyun }
294*4882a593Smuzhiyun
295*4882a593Smuzhiyun /* No more children, re-visit this node */
296*4882a593Smuzhiyun
297*4882a593Smuzhiyun if (!node_previously_visited) {
298*4882a593Smuzhiyun node_previously_visited = TRUE;
299*4882a593Smuzhiyun continue;
300*4882a593Smuzhiyun }
301*4882a593Smuzhiyun
302*4882a593Smuzhiyun /* No more children, visit peers */
303*4882a593Smuzhiyun
304*4882a593Smuzhiyun child_node = acpi_ns_get_next_node(parent_node, child_node);
305*4882a593Smuzhiyun if (child_node) {
306*4882a593Smuzhiyun node_previously_visited = FALSE;
307*4882a593Smuzhiyun }
308*4882a593Smuzhiyun
309*4882a593Smuzhiyun /* No peers, re-visit parent */
310*4882a593Smuzhiyun
311*4882a593Smuzhiyun else {
312*4882a593Smuzhiyun /*
313*4882a593Smuzhiyun * No more children of this node (acpi_ns_get_next_node failed), go
314*4882a593Smuzhiyun * back upwards in the namespace tree to the node's parent.
315*4882a593Smuzhiyun */
316*4882a593Smuzhiyun level--;
317*4882a593Smuzhiyun child_node = parent_node;
318*4882a593Smuzhiyun parent_node = parent_node->parent;
319*4882a593Smuzhiyun
320*4882a593Smuzhiyun node_previously_visited = TRUE;
321*4882a593Smuzhiyun }
322*4882a593Smuzhiyun }
323*4882a593Smuzhiyun
324*4882a593Smuzhiyun /* Complete walk, not terminated by user function */
325*4882a593Smuzhiyun
326*4882a593Smuzhiyun return_ACPI_STATUS(AE_OK);
327*4882a593Smuzhiyun }
328