xref: /freebsd/sys/dev/isci/scil/scic_sds_remote_node_table.c (revision 685dc743dc3b5645e34836464128e1c0558b404b)
1f11c7f63SJim Harris /*-
2*718cf2ccSPedro F. Giffuni  * SPDX-License-Identifier: BSD-2-Clause OR GPL-2.0
3*718cf2ccSPedro F. Giffuni  *
4f11c7f63SJim Harris  * This file is provided under a dual BSD/GPLv2 license.  When using or
5f11c7f63SJim Harris  * redistributing this file, you may do so under either license.
6f11c7f63SJim Harris  *
7f11c7f63SJim Harris  * GPL LICENSE SUMMARY
8f11c7f63SJim Harris  *
9f11c7f63SJim Harris  * Copyright(c) 2008 - 2011 Intel Corporation. All rights reserved.
10f11c7f63SJim Harris  *
11f11c7f63SJim Harris  * This program is free software; you can redistribute it and/or modify
12f11c7f63SJim Harris  * it under the terms of version 2 of the GNU General Public License as
13f11c7f63SJim Harris  * published by the Free Software Foundation.
14f11c7f63SJim Harris  *
15f11c7f63SJim Harris  * This program is distributed in the hope that it will be useful, but
16f11c7f63SJim Harris  * WITHOUT ANY WARRANTY; without even the implied warranty of
17f11c7f63SJim Harris  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
18f11c7f63SJim Harris  * General Public License for more details.
19f11c7f63SJim Harris  *
20f11c7f63SJim Harris  * You should have received a copy of the GNU General Public License
21f11c7f63SJim Harris  * along with this program; if not, write to the Free Software
22f11c7f63SJim Harris  * Foundation, Inc., 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
23f11c7f63SJim Harris  * The full GNU General Public License is included in this distribution
24f11c7f63SJim Harris  * in the file called LICENSE.GPL.
25f11c7f63SJim Harris  *
26f11c7f63SJim Harris  * BSD LICENSE
27f11c7f63SJim Harris  *
28f11c7f63SJim Harris  * Copyright(c) 2008 - 2011 Intel Corporation. All rights reserved.
29f11c7f63SJim Harris  * All rights reserved.
30f11c7f63SJim Harris  *
31f11c7f63SJim Harris  * Redistribution and use in source and binary forms, with or without
32f11c7f63SJim Harris  * modification, are permitted provided that the following conditions
33f11c7f63SJim Harris  * are met:
34f11c7f63SJim Harris  *
35f11c7f63SJim Harris  *   * Redistributions of source code must retain the above copyright
36f11c7f63SJim Harris  *     notice, this list of conditions and the following disclaimer.
37f11c7f63SJim Harris  *   * Redistributions in binary form must reproduce the above copyright
38f11c7f63SJim Harris  *     notice, this list of conditions and the following disclaimer in
39f11c7f63SJim Harris  *     the documentation and/or other materials provided with the
40f11c7f63SJim Harris  *     distribution.
41f11c7f63SJim Harris  *
42f11c7f63SJim Harris  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
43f11c7f63SJim Harris  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
44f11c7f63SJim Harris  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
45f11c7f63SJim Harris  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
46f11c7f63SJim Harris  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
47f11c7f63SJim Harris  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
48f11c7f63SJim Harris  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
49f11c7f63SJim Harris  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
50f11c7f63SJim Harris  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
51f11c7f63SJim Harris  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
52f11c7f63SJim Harris  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
53f11c7f63SJim Harris  */
54f11c7f63SJim Harris 
55f11c7f63SJim Harris #include <sys/cdefs.h>
56f11c7f63SJim Harris /**
57f11c7f63SJim Harris  * @file
58f11c7f63SJim Harris  *
59f11c7f63SJim Harris  * @brief This file contains the implementation of the
60f11c7f63SJim Harris  *        SCIC_SDS_REMOTE_NODE_TABLE public, protected, and private methods.
61f11c7f63SJim Harris  */
62f11c7f63SJim Harris 
63f11c7f63SJim Harris #include <dev/isci/scil/scic_sds_remote_node_table.h>
64f11c7f63SJim Harris #include <dev/isci/scil/scic_sds_remote_node_context.h>
65f11c7f63SJim Harris 
66f11c7f63SJim Harris /**
67f11c7f63SJim Harris  * This routine will find the bit position in absolute bit terms of the next
68f11c7f63SJim Harris  * available bit for selection.  The absolute bit is index * 32 + bit
69f11c7f63SJim Harris  * position.  If there are available bits in the first U32 then it is just bit
70f11c7f63SJim Harris  * position.
71f11c7f63SJim Harris  *  @param[in] remote_node_table This is the remote node index table from
72f11c7f63SJim Harris  *       which the selection will be made.
73f11c7f63SJim Harris  * @param[in] group_table_index This is the index to the group table from
74f11c7f63SJim Harris  *       which to search for an available selection.
75f11c7f63SJim Harris  *
76f11c7f63SJim Harris  * @return U32 This is the absolute bit position for an available group.
77f11c7f63SJim Harris  */
78f11c7f63SJim Harris static
scic_sds_remote_node_table_get_group_index(SCIC_REMOTE_NODE_TABLE_T * remote_node_table,U32 group_table_index)79f11c7f63SJim Harris U32 scic_sds_remote_node_table_get_group_index(
80f11c7f63SJim Harris    SCIC_REMOTE_NODE_TABLE_T * remote_node_table,
81f11c7f63SJim Harris    U32                        group_table_index
82f11c7f63SJim Harris )
83f11c7f63SJim Harris {
84f11c7f63SJim Harris    U32   dword_index;
85f11c7f63SJim Harris    U32 * group_table;
86f11c7f63SJim Harris    U32   bit_index;
87f11c7f63SJim Harris 
88f11c7f63SJim Harris    group_table = remote_node_table->remote_node_groups[group_table_index];
89f11c7f63SJim Harris 
90f11c7f63SJim Harris    for (dword_index = 0; dword_index < remote_node_table->group_array_size; dword_index++)
91f11c7f63SJim Harris    {
92f11c7f63SJim Harris       if (group_table[dword_index] != 0)
93f11c7f63SJim Harris       {
94f11c7f63SJim Harris          for (bit_index = 0; bit_index < 32; bit_index++)
95f11c7f63SJim Harris          {
96f11c7f63SJim Harris             if ((group_table[dword_index] & (1 << bit_index)) != 0)
97f11c7f63SJim Harris             {
98f11c7f63SJim Harris                return (dword_index * 32) + bit_index;
99f11c7f63SJim Harris             }
100f11c7f63SJim Harris          }
101f11c7f63SJim Harris       }
102f11c7f63SJim Harris    }
103f11c7f63SJim Harris 
104f11c7f63SJim Harris    return SCIC_SDS_REMOTE_NODE_TABLE_INVALID_INDEX;
105f11c7f63SJim Harris }
106f11c7f63SJim Harris 
107f11c7f63SJim Harris /**
108f11c7f63SJim Harris  * This method will clear the group index entry in the specified group index
109f11c7f63SJim Harris  * table.
110f11c7f63SJim Harris  *
111f11c7f63SJim Harris  * @param[in out] remote_node_table This the remote node table in which to
112f11c7f63SJim Harris  *       clear the selector.
113f11c7f63SJim Harris  * @param[in] set_index This is the remote node selector in which the change
114f11c7f63SJim Harris  *       will be made.
115f11c7f63SJim Harris  * @param[in] group_index This is the bit index in the table to be modified.
116f11c7f63SJim Harris  *
117f11c7f63SJim Harris  * @return none
118f11c7f63SJim Harris  */
119f11c7f63SJim Harris static
scic_sds_remote_node_table_clear_group_index(SCIC_REMOTE_NODE_TABLE_T * remote_node_table,U32 group_table_index,U32 group_index)120f11c7f63SJim Harris void scic_sds_remote_node_table_clear_group_index(
121f11c7f63SJim Harris    SCIC_REMOTE_NODE_TABLE_T * remote_node_table,
122f11c7f63SJim Harris    U32                        group_table_index,
123f11c7f63SJim Harris    U32                        group_index
124f11c7f63SJim Harris )
125f11c7f63SJim Harris {
126f11c7f63SJim Harris    U32   dword_index;
127f11c7f63SJim Harris    U32   bit_index;
128f11c7f63SJim Harris    U32 * group_table;
129f11c7f63SJim Harris 
130f11c7f63SJim Harris    ASSERT(group_table_index < SCU_STP_REMOTE_NODE_COUNT);
131f11c7f63SJim Harris    ASSERT(group_index < (U32)(remote_node_table->group_array_size * 32));
132f11c7f63SJim Harris 
133f11c7f63SJim Harris    dword_index = group_index / 32;
134f11c7f63SJim Harris    bit_index   = group_index % 32;
135f11c7f63SJim Harris    group_table = remote_node_table->remote_node_groups[group_table_index];
136f11c7f63SJim Harris 
137f11c7f63SJim Harris    group_table[dword_index] = group_table[dword_index] & ~(1 << bit_index);
138f11c7f63SJim Harris }
139f11c7f63SJim Harris 
140f11c7f63SJim Harris /**
141453130d9SPedro F. Giffuni  * This method will set the group index bit entry in the specified group index
142f11c7f63SJim Harris  * table.
143f11c7f63SJim Harris  *
144f11c7f63SJim Harris  * @param[in out] remote_node_table This the remote node table in which to set
145f11c7f63SJim Harris  *       the selector.
146f11c7f63SJim Harris  * @param[in] group_table_index This is the remote node selector in which the
147f11c7f63SJim Harris  *       change will be made.
148f11c7f63SJim Harris  * @param[in] group_index This is the bit position in the table to be
149f11c7f63SJim Harris  *       modified.
150f11c7f63SJim Harris  *
151f11c7f63SJim Harris  * @return none
152f11c7f63SJim Harris  */
153f11c7f63SJim Harris static
scic_sds_remote_node_table_set_group_index(SCIC_REMOTE_NODE_TABLE_T * remote_node_table,U32 group_table_index,U32 group_index)154f11c7f63SJim Harris void scic_sds_remote_node_table_set_group_index(
155f11c7f63SJim Harris    SCIC_REMOTE_NODE_TABLE_T * remote_node_table,
156f11c7f63SJim Harris    U32                        group_table_index,
157f11c7f63SJim Harris    U32                        group_index
158f11c7f63SJim Harris )
159f11c7f63SJim Harris {
160f11c7f63SJim Harris    U32   dword_index;
161f11c7f63SJim Harris    U32   bit_index;
162f11c7f63SJim Harris    U32 * group_table;
163f11c7f63SJim Harris 
164f11c7f63SJim Harris    ASSERT(group_table_index < SCU_STP_REMOTE_NODE_COUNT);
165f11c7f63SJim Harris    ASSERT(group_index < (U32)(remote_node_table->group_array_size * 32));
166f11c7f63SJim Harris 
167f11c7f63SJim Harris    dword_index = group_index / 32;
168f11c7f63SJim Harris    bit_index   = group_index % 32;
169f11c7f63SJim Harris    group_table = remote_node_table->remote_node_groups[group_table_index];
170f11c7f63SJim Harris 
171f11c7f63SJim Harris    group_table[dword_index] = group_table[dword_index] | (1 << bit_index);
172f11c7f63SJim Harris }
173f11c7f63SJim Harris 
174f11c7f63SJim Harris /**
175f11c7f63SJim Harris  * This method will set the remote to available in the remote node allocation
176f11c7f63SJim Harris  * table.
177f11c7f63SJim Harris  *
178f11c7f63SJim Harris  * @param[in out] remote_node_table This is the remote node table in which to
179f11c7f63SJim Harris  *       modify the remote node availability.
180f11c7f63SJim Harris  * @param[in] remote_node_index This is the remote node index that is being
181f11c7f63SJim Harris  *       returned to the table.
182f11c7f63SJim Harris  *
183f11c7f63SJim Harris  * @return none
184f11c7f63SJim Harris  */
185f11c7f63SJim Harris static
scic_sds_remote_node_table_set_node_index(SCIC_REMOTE_NODE_TABLE_T * remote_node_table,U32 remote_node_index)186f11c7f63SJim Harris void scic_sds_remote_node_table_set_node_index(
187f11c7f63SJim Harris    SCIC_REMOTE_NODE_TABLE_T * remote_node_table,
188f11c7f63SJim Harris    U32                        remote_node_index
189f11c7f63SJim Harris )
190f11c7f63SJim Harris {
191f11c7f63SJim Harris    U32 dword_location;
192f11c7f63SJim Harris    U32 dword_remainder;
193f11c7f63SJim Harris    U32 slot_normalized;
194f11c7f63SJim Harris    U32 slot_position;
195f11c7f63SJim Harris 
196f11c7f63SJim Harris    ASSERT(
197f11c7f63SJim Harris         (remote_node_table->available_nodes_array_size * SCIC_SDS_REMOTE_NODE_SETS_PER_DWORD)
198f11c7f63SJim Harris       > (remote_node_index / SCU_STP_REMOTE_NODE_COUNT)
199f11c7f63SJim Harris    );
200f11c7f63SJim Harris 
201f11c7f63SJim Harris    dword_location  = remote_node_index / SCIC_SDS_REMOTE_NODES_PER_DWORD;
202f11c7f63SJim Harris    dword_remainder = remote_node_index % SCIC_SDS_REMOTE_NODES_PER_DWORD;
203f11c7f63SJim Harris    slot_normalized = (dword_remainder / SCU_STP_REMOTE_NODE_COUNT) * sizeof(U32);
204f11c7f63SJim Harris    slot_position   = remote_node_index % SCU_STP_REMOTE_NODE_COUNT;
205f11c7f63SJim Harris 
206f11c7f63SJim Harris    remote_node_table->available_remote_nodes[dword_location] |=
207f11c7f63SJim Harris       1 << (slot_normalized + slot_position);
208f11c7f63SJim Harris }
209f11c7f63SJim Harris 
210f11c7f63SJim Harris /**
211f11c7f63SJim Harris  * This method clears the remote node index from the table of available remote
212f11c7f63SJim Harris  * nodes.
213f11c7f63SJim Harris  *
214f11c7f63SJim Harris  * @param[in out] remote_node_table This is the remote node table from which
215f11c7f63SJim Harris  *       to clear the available remote node bit.
216f11c7f63SJim Harris  * @param[in] remote_node_index This is the remote node index which is to be
217f11c7f63SJim Harris  *       cleared from the table.
218f11c7f63SJim Harris  *
219f11c7f63SJim Harris  * @return none
220f11c7f63SJim Harris  */
221f11c7f63SJim Harris static
scic_sds_remote_node_table_clear_node_index(SCIC_REMOTE_NODE_TABLE_T * remote_node_table,U32 remote_node_index)222f11c7f63SJim Harris void scic_sds_remote_node_table_clear_node_index(
223f11c7f63SJim Harris    SCIC_REMOTE_NODE_TABLE_T * remote_node_table,
224f11c7f63SJim Harris    U32                        remote_node_index
225f11c7f63SJim Harris )
226f11c7f63SJim Harris {
227f11c7f63SJim Harris    U32 dword_location;
228f11c7f63SJim Harris    U32 dword_remainder;
229f11c7f63SJim Harris    U32 slot_position;
230f11c7f63SJim Harris    U32 slot_normalized;
231f11c7f63SJim Harris 
232f11c7f63SJim Harris    ASSERT(
233f11c7f63SJim Harris         (remote_node_table->available_nodes_array_size * SCIC_SDS_REMOTE_NODE_SETS_PER_DWORD)
234f11c7f63SJim Harris       > (remote_node_index / SCU_STP_REMOTE_NODE_COUNT)
235f11c7f63SJim Harris    );
236f11c7f63SJim Harris 
237f11c7f63SJim Harris    dword_location  = remote_node_index / SCIC_SDS_REMOTE_NODES_PER_DWORD;
238f11c7f63SJim Harris    dword_remainder = remote_node_index % SCIC_SDS_REMOTE_NODES_PER_DWORD;
239f11c7f63SJim Harris    slot_normalized = (dword_remainder / SCU_STP_REMOTE_NODE_COUNT) * sizeof(U32);
240f11c7f63SJim Harris    slot_position   = remote_node_index % SCU_STP_REMOTE_NODE_COUNT;
241f11c7f63SJim Harris 
242f11c7f63SJim Harris    remote_node_table->available_remote_nodes[dword_location] &=
243f11c7f63SJim Harris       ~(1 << (slot_normalized + slot_position));
244f11c7f63SJim Harris }
245f11c7f63SJim Harris 
246f11c7f63SJim Harris /**
247f11c7f63SJim Harris  * This method clears the entire table slot at the specified slot index.
248f11c7f63SJim Harris  *
249f11c7f63SJim Harris  * @param[in out] remote_node_table The remote node table from which the slot
250f11c7f63SJim Harris  *       will be cleared.
251f11c7f63SJim Harris  * @param[in] group_index The index for the slot that is to be cleared.
252f11c7f63SJim Harris  *
253f11c7f63SJim Harris  * @return none
254f11c7f63SJim Harris  */
255f11c7f63SJim Harris static
scic_sds_remote_node_table_clear_group(SCIC_REMOTE_NODE_TABLE_T * remote_node_table,U32 group_index)256f11c7f63SJim Harris void scic_sds_remote_node_table_clear_group(
257f11c7f63SJim Harris    SCIC_REMOTE_NODE_TABLE_T * remote_node_table,
258f11c7f63SJim Harris    U32                        group_index
259f11c7f63SJim Harris )
260f11c7f63SJim Harris {
261f11c7f63SJim Harris    U32 dword_location;
262f11c7f63SJim Harris    U32 dword_remainder;
263f11c7f63SJim Harris    U32 dword_value;
264f11c7f63SJim Harris 
265f11c7f63SJim Harris    ASSERT(
266f11c7f63SJim Harris         (remote_node_table->available_nodes_array_size * SCIC_SDS_REMOTE_NODE_SETS_PER_DWORD)
267f11c7f63SJim Harris       > (group_index / SCU_STP_REMOTE_NODE_COUNT)
268f11c7f63SJim Harris    );
269f11c7f63SJim Harris 
270f11c7f63SJim Harris    dword_location  = group_index / SCIC_SDS_REMOTE_NODE_SETS_PER_DWORD;
271f11c7f63SJim Harris    dword_remainder = group_index % SCIC_SDS_REMOTE_NODE_SETS_PER_DWORD;
272f11c7f63SJim Harris 
273f11c7f63SJim Harris    dword_value = remote_node_table->available_remote_nodes[dword_location];
274f11c7f63SJim Harris    dword_value &= ~(SCIC_SDS_REMOTE_NODE_TABLE_FULL_SLOT_VALUE << (dword_remainder * 4));
275f11c7f63SJim Harris    remote_node_table->available_remote_nodes[dword_location] = dword_value;
276f11c7f63SJim Harris }
277f11c7f63SJim Harris 
278f11c7f63SJim Harris /**
279f11c7f63SJim Harris  * THis method sets an entire remote node group in the remote node table.
280f11c7f63SJim Harris  *
281f11c7f63SJim Harris  * @param[in] remote_node_table
282f11c7f63SJim Harris  * @param[in] group_index
283f11c7f63SJim Harris  */
284f11c7f63SJim Harris static
scic_sds_remote_node_table_set_group(SCIC_REMOTE_NODE_TABLE_T * remote_node_table,U32 group_index)285f11c7f63SJim Harris void scic_sds_remote_node_table_set_group(
286f11c7f63SJim Harris    SCIC_REMOTE_NODE_TABLE_T * remote_node_table,
287f11c7f63SJim Harris    U32                        group_index
288f11c7f63SJim Harris )
289f11c7f63SJim Harris {
290f11c7f63SJim Harris    U32 dword_location;
291f11c7f63SJim Harris    U32 dword_remainder;
292f11c7f63SJim Harris    U32 dword_value;
293f11c7f63SJim Harris 
294f11c7f63SJim Harris    ASSERT(
295f11c7f63SJim Harris         (remote_node_table->available_nodes_array_size * SCIC_SDS_REMOTE_NODE_SETS_PER_DWORD)
296f11c7f63SJim Harris       > (group_index / SCU_STP_REMOTE_NODE_COUNT)
297f11c7f63SJim Harris    );
298f11c7f63SJim Harris 
299f11c7f63SJim Harris    dword_location  = group_index / SCIC_SDS_REMOTE_NODE_SETS_PER_DWORD;
300f11c7f63SJim Harris    dword_remainder = group_index % SCIC_SDS_REMOTE_NODE_SETS_PER_DWORD;
301f11c7f63SJim Harris 
302f11c7f63SJim Harris    dword_value = remote_node_table->available_remote_nodes[dword_location];
303f11c7f63SJim Harris    dword_value |= (SCIC_SDS_REMOTE_NODE_TABLE_FULL_SLOT_VALUE << (dword_remainder * 4));
304f11c7f63SJim Harris    remote_node_table->available_remote_nodes[dword_location] = dword_value;
305f11c7f63SJim Harris }
306f11c7f63SJim Harris 
307f11c7f63SJim Harris /**
308f11c7f63SJim Harris  * This method will return the group value for the specified group index.
309f11c7f63SJim Harris  *
310f11c7f63SJim Harris  * @param[in] remote_node_table This is the remote node table that for which
311f11c7f63SJim Harris  *       the group value is to be returned.
312f11c7f63SJim Harris  * @param[in] group_index This is the group index to use to find the group
313f11c7f63SJim Harris  *       value.
314f11c7f63SJim Harris  *
315f11c7f63SJim Harris  * @return The bit values at the specified remote node group index.
316f11c7f63SJim Harris  */
317f11c7f63SJim Harris static
scic_sds_remote_node_table_get_group_value(SCIC_REMOTE_NODE_TABLE_T * remote_node_table,U32 group_index)318f11c7f63SJim Harris U8 scic_sds_remote_node_table_get_group_value(
319f11c7f63SJim Harris    SCIC_REMOTE_NODE_TABLE_T * remote_node_table,
320f11c7f63SJim Harris    U32                        group_index
321f11c7f63SJim Harris )
322f11c7f63SJim Harris {
323f11c7f63SJim Harris    U32 dword_location;
324f11c7f63SJim Harris    U32 dword_remainder;
325f11c7f63SJim Harris    U32 dword_value;
326f11c7f63SJim Harris 
327f11c7f63SJim Harris    dword_location  = group_index / SCIC_SDS_REMOTE_NODE_SETS_PER_DWORD;
328f11c7f63SJim Harris    dword_remainder = group_index % SCIC_SDS_REMOTE_NODE_SETS_PER_DWORD;
329f11c7f63SJim Harris 
330f11c7f63SJim Harris    dword_value = remote_node_table->available_remote_nodes[dword_location];
331f11c7f63SJim Harris    dword_value &= (SCIC_SDS_REMOTE_NODE_TABLE_FULL_SLOT_VALUE << (dword_remainder * 4));
332f11c7f63SJim Harris    dword_value = dword_value >> (dword_remainder * 4);
333f11c7f63SJim Harris 
334f11c7f63SJim Harris    return (U8)dword_value;
335f11c7f63SJim Harris }
336f11c7f63SJim Harris 
337f11c7f63SJim Harris /**
338f11c7f63SJim Harris  * This method will initialize the remote node table for use.
339f11c7f63SJim Harris  *
340f11c7f63SJim Harris  * @param[in out] remote_node_table The remote that which is to be
341f11c7f63SJim Harris  *       initialized.
342f11c7f63SJim Harris  * @param[in] remote_node_entries The number of entries to put in the table.
343f11c7f63SJim Harris  *
344f11c7f63SJim Harris  * @return none
345f11c7f63SJim Harris  */
scic_sds_remote_node_table_initialize(SCIC_REMOTE_NODE_TABLE_T * remote_node_table,U32 remote_node_entries)346f11c7f63SJim Harris void scic_sds_remote_node_table_initialize(
347f11c7f63SJim Harris    SCIC_REMOTE_NODE_TABLE_T * remote_node_table,
348f11c7f63SJim Harris    U32                        remote_node_entries
349f11c7f63SJim Harris )
350f11c7f63SJim Harris {
351f11c7f63SJim Harris    U32 index;
352f11c7f63SJim Harris 
353f11c7f63SJim Harris    // Initialize the raw data we could improve the speed by only initializing
354f11c7f63SJim Harris    // those entries that we are actually going to be used
355f11c7f63SJim Harris    memset(
356f11c7f63SJim Harris       remote_node_table->available_remote_nodes,
357f11c7f63SJim Harris       0x00,
358f11c7f63SJim Harris       sizeof(remote_node_table->available_remote_nodes)
359f11c7f63SJim Harris    );
360f11c7f63SJim Harris 
361f11c7f63SJim Harris    memset(
362f11c7f63SJim Harris       remote_node_table->remote_node_groups,
363f11c7f63SJim Harris       0x00,
364f11c7f63SJim Harris       sizeof(remote_node_table->remote_node_groups)
365f11c7f63SJim Harris    );
366f11c7f63SJim Harris 
367f11c7f63SJim Harris    // Initialize the available remote node sets
368f11c7f63SJim Harris    remote_node_table->available_nodes_array_size = (U16)
369f11c7f63SJim Harris         (remote_node_entries / SCIC_SDS_REMOTE_NODES_PER_DWORD)
370f11c7f63SJim Harris       + ((remote_node_entries % SCIC_SDS_REMOTE_NODES_PER_DWORD) != 0);
371f11c7f63SJim Harris 
372f11c7f63SJim Harris 
373f11c7f63SJim Harris    // Initialize each full DWORD to a FULL SET of remote nodes
374f11c7f63SJim Harris    for (index = 0; index < remote_node_entries; index++)
375f11c7f63SJim Harris    {
376f11c7f63SJim Harris       scic_sds_remote_node_table_set_node_index(remote_node_table, index);
377f11c7f63SJim Harris    }
378f11c7f63SJim Harris 
379f11c7f63SJim Harris    remote_node_table->group_array_size = (U16)
380f11c7f63SJim Harris         (remote_node_entries / (SCU_STP_REMOTE_NODE_COUNT * 32))
381f11c7f63SJim Harris       + ((remote_node_entries % (SCU_STP_REMOTE_NODE_COUNT * 32)) != 0);
382f11c7f63SJim Harris 
383f11c7f63SJim Harris    for (index = 0; index < (remote_node_entries / SCU_STP_REMOTE_NODE_COUNT); index++)
384f11c7f63SJim Harris    {
385f11c7f63SJim Harris       // These are all guaranteed to be full slot values so fill them in the
386f11c7f63SJim Harris       // available sets of 3 remote nodes
387f11c7f63SJim Harris       scic_sds_remote_node_table_set_group_index(remote_node_table, 2, index);
388f11c7f63SJim Harris    }
389f11c7f63SJim Harris 
390f11c7f63SJim Harris    // Now fill in any remainders that we may find
391f11c7f63SJim Harris    if ((remote_node_entries % SCU_STP_REMOTE_NODE_COUNT) == 2)
392f11c7f63SJim Harris    {
393f11c7f63SJim Harris       scic_sds_remote_node_table_set_group_index(remote_node_table, 1, index);
394f11c7f63SJim Harris    }
395f11c7f63SJim Harris    else if ((remote_node_entries % SCU_STP_REMOTE_NODE_COUNT) == 1)
396f11c7f63SJim Harris    {
397f11c7f63SJim Harris       scic_sds_remote_node_table_set_group_index(remote_node_table, 0, index);
398f11c7f63SJim Harris    }
399f11c7f63SJim Harris }
400f11c7f63SJim Harris 
401f11c7f63SJim Harris /**
402f11c7f63SJim Harris  * This method will allocate a single RNi from the remote node table.  The
403f11c7f63SJim Harris  * table index will determine from which remote node group table to search.
404f11c7f63SJim Harris  * This search may fail and another group node table can be specified.  The
405f11c7f63SJim Harris  * function is designed to allow a serach of the available single remote node
406f11c7f63SJim Harris  * group up to the triple remote node group.  If an entry is found in the
407f11c7f63SJim Harris  * specified table the remote node is removed and the remote node groups are
408f11c7f63SJim Harris  * updated.
409f11c7f63SJim Harris  *
410f11c7f63SJim Harris  * @param[in out] remote_node_table The remote node table from which to
411f11c7f63SJim Harris  *       allocate a remote node.
412f11c7f63SJim Harris  * @param[in] table_index The group index that is to be used for the search.
413f11c7f63SJim Harris  *
414f11c7f63SJim Harris  * @return The RNi value or an invalid remote node context if an RNi can not
415f11c7f63SJim Harris  *         be found.
416f11c7f63SJim Harris  */
417f11c7f63SJim Harris static
scic_sds_remote_node_table_allocate_single_remote_node(SCIC_REMOTE_NODE_TABLE_T * remote_node_table,U32 group_table_index)418f11c7f63SJim Harris U16 scic_sds_remote_node_table_allocate_single_remote_node(
419f11c7f63SJim Harris    SCIC_REMOTE_NODE_TABLE_T * remote_node_table,
420f11c7f63SJim Harris    U32                        group_table_index
421f11c7f63SJim Harris )
422f11c7f63SJim Harris {
423f11c7f63SJim Harris    U8  index;
424f11c7f63SJim Harris    U8  group_value;
425f11c7f63SJim Harris    U32 group_index;
426f11c7f63SJim Harris    U16 remote_node_index = SCIC_SDS_REMOTE_NODE_CONTEXT_INVALID_INDEX;
427f11c7f63SJim Harris 
428f11c7f63SJim Harris    group_index = scic_sds_remote_node_table_get_group_index(
429f11c7f63SJim Harris                                              remote_node_table, group_table_index);
430f11c7f63SJim Harris 
431f11c7f63SJim Harris    // We could not find an available slot in the table selector 0
432f11c7f63SJim Harris    if (group_index != SCIC_SDS_REMOTE_NODE_TABLE_INVALID_INDEX)
433f11c7f63SJim Harris    {
434f11c7f63SJim Harris       group_value = scic_sds_remote_node_table_get_group_value(
435f11c7f63SJim Harris                                     remote_node_table, group_index);
436f11c7f63SJim Harris 
437f11c7f63SJim Harris       for (index = 0; index < SCU_STP_REMOTE_NODE_COUNT; index++)
438f11c7f63SJim Harris       {
439f11c7f63SJim Harris          if (((1 << index) & group_value) != 0)
440f11c7f63SJim Harris          {
441f11c7f63SJim Harris             // We have selected a bit now clear it
442f11c7f63SJim Harris             remote_node_index = (U16) (group_index * SCU_STP_REMOTE_NODE_COUNT
443f11c7f63SJim Harris                                        + index);
444f11c7f63SJim Harris 
445f11c7f63SJim Harris             scic_sds_remote_node_table_clear_group_index(
446f11c7f63SJim Harris                remote_node_table, group_table_index, group_index
447f11c7f63SJim Harris             );
448f11c7f63SJim Harris 
449f11c7f63SJim Harris             scic_sds_remote_node_table_clear_node_index(
450f11c7f63SJim Harris                remote_node_table, remote_node_index
451f11c7f63SJim Harris             );
452f11c7f63SJim Harris 
453f11c7f63SJim Harris             if (group_table_index > 0)
454f11c7f63SJim Harris             {
455f11c7f63SJim Harris                scic_sds_remote_node_table_set_group_index(
456f11c7f63SJim Harris                   remote_node_table, group_table_index - 1, group_index
457f11c7f63SJim Harris                );
458f11c7f63SJim Harris             }
459f11c7f63SJim Harris 
460f11c7f63SJim Harris             break;
461f11c7f63SJim Harris          }
462f11c7f63SJim Harris       }
463f11c7f63SJim Harris    }
464f11c7f63SJim Harris 
465f11c7f63SJim Harris    return remote_node_index;
466f11c7f63SJim Harris }
467f11c7f63SJim Harris 
468f11c7f63SJim Harris /**
469f11c7f63SJim Harris  * This method will allocate three consecutive remote node context entries. If
470f11c7f63SJim Harris  * there are no remaining triple entries the function will return a failure.
471f11c7f63SJim Harris  *
472f11c7f63SJim Harris  * @param[in] remote_node_table This is the remote node table from which to
473f11c7f63SJim Harris  *       allocate the remote node entries.
474f11c7f63SJim Harris  * @param[in] group_table_index THis is the group table index which must equal
475f11c7f63SJim Harris  *       two (2) for this operation.
476f11c7f63SJim Harris  *
477f11c7f63SJim Harris  * @return The remote node index that represents three consecutive remote node
478f11c7f63SJim Harris  *         entries or an invalid remote node context if none can be found.
479f11c7f63SJim Harris  */
480f11c7f63SJim Harris static
scic_sds_remote_node_table_allocate_triple_remote_node(SCIC_REMOTE_NODE_TABLE_T * remote_node_table,U32 group_table_index)481f11c7f63SJim Harris U16 scic_sds_remote_node_table_allocate_triple_remote_node(
482f11c7f63SJim Harris    SCIC_REMOTE_NODE_TABLE_T * remote_node_table,
483f11c7f63SJim Harris    U32                        group_table_index
484f11c7f63SJim Harris )
485f11c7f63SJim Harris {
486f11c7f63SJim Harris    U32 group_index;
487f11c7f63SJim Harris    U16 remote_node_index = SCIC_SDS_REMOTE_NODE_CONTEXT_INVALID_INDEX;
488f11c7f63SJim Harris 
489f11c7f63SJim Harris    group_index = scic_sds_remote_node_table_get_group_index(
490f11c7f63SJim Harris                                              remote_node_table, group_table_index);
491f11c7f63SJim Harris 
492f11c7f63SJim Harris    if (group_index != SCIC_SDS_REMOTE_NODE_TABLE_INVALID_INDEX)
493f11c7f63SJim Harris    {
494f11c7f63SJim Harris       remote_node_index = (U16) group_index * SCU_STP_REMOTE_NODE_COUNT;
495f11c7f63SJim Harris 
496f11c7f63SJim Harris       scic_sds_remote_node_table_clear_group_index(
497f11c7f63SJim Harris          remote_node_table, group_table_index, group_index
498f11c7f63SJim Harris       );
499f11c7f63SJim Harris 
500f11c7f63SJim Harris       scic_sds_remote_node_table_clear_group(
501f11c7f63SJim Harris          remote_node_table, group_index
502f11c7f63SJim Harris       );
503f11c7f63SJim Harris    }
504f11c7f63SJim Harris 
505f11c7f63SJim Harris    return remote_node_index;
506f11c7f63SJim Harris }
507f11c7f63SJim Harris 
508f11c7f63SJim Harris /**
509f11c7f63SJim Harris  * This method will allocate a remote node that mataches the remote node count
510f11c7f63SJim Harris  * specified by the caller.  Valid values for remote node count is
511f11c7f63SJim Harris  * SCU_SSP_REMOTE_NODE_COUNT(1) or SCU_STP_REMOTE_NODE_COUNT(3).
512f11c7f63SJim Harris  *
513f11c7f63SJim Harris  * @param[in] remote_node_table This is the remote node table from which the
514f11c7f63SJim Harris  *       remote node allocation is to take place.
515f11c7f63SJim Harris  * @param[in] remote_node_count This is ther remote node count which is one of
516f11c7f63SJim Harris  *       SCU_SSP_REMOTE_NODE_COUNT(1) or SCU_STP_REMOTE_NODE_COUNT(3).
517f11c7f63SJim Harris  *
518f11c7f63SJim Harris  * @return U16 This is the remote node index that is returned or an invalid
519f11c7f63SJim Harris  *         remote node context.
520f11c7f63SJim Harris  */
scic_sds_remote_node_table_allocate_remote_node(SCIC_REMOTE_NODE_TABLE_T * remote_node_table,U32 remote_node_count)521f11c7f63SJim Harris U16 scic_sds_remote_node_table_allocate_remote_node(
522f11c7f63SJim Harris    SCIC_REMOTE_NODE_TABLE_T * remote_node_table,
523f11c7f63SJim Harris    U32                        remote_node_count
524f11c7f63SJim Harris )
525f11c7f63SJim Harris {
526f11c7f63SJim Harris    U16 remote_node_index = SCIC_SDS_REMOTE_NODE_CONTEXT_INVALID_INDEX;
527f11c7f63SJim Harris 
528f11c7f63SJim Harris    if (remote_node_count == SCU_SSP_REMOTE_NODE_COUNT)
529f11c7f63SJim Harris    {
530f11c7f63SJim Harris       remote_node_index =
531f11c7f63SJim Harris          scic_sds_remote_node_table_allocate_single_remote_node(
532f11c7f63SJim Harris                                                          remote_node_table, 0);
533f11c7f63SJim Harris 
534f11c7f63SJim Harris       if (remote_node_index == SCIC_SDS_REMOTE_NODE_CONTEXT_INVALID_INDEX)
535f11c7f63SJim Harris       {
536f11c7f63SJim Harris          remote_node_index =
537f11c7f63SJim Harris             scic_sds_remote_node_table_allocate_single_remote_node(
538f11c7f63SJim Harris                                                          remote_node_table, 1);
539f11c7f63SJim Harris       }
540f11c7f63SJim Harris 
541f11c7f63SJim Harris       if (remote_node_index == SCIC_SDS_REMOTE_NODE_CONTEXT_INVALID_INDEX)
542f11c7f63SJim Harris       {
543f11c7f63SJim Harris          remote_node_index =
544f11c7f63SJim Harris             scic_sds_remote_node_table_allocate_single_remote_node(
545f11c7f63SJim Harris                                                          remote_node_table, 2);
546f11c7f63SJim Harris       }
547f11c7f63SJim Harris    }
548f11c7f63SJim Harris    else if (remote_node_count == SCU_STP_REMOTE_NODE_COUNT)
549f11c7f63SJim Harris    {
550f11c7f63SJim Harris       remote_node_index =
551f11c7f63SJim Harris          scic_sds_remote_node_table_allocate_triple_remote_node(
552f11c7f63SJim Harris                                                          remote_node_table, 2);
553f11c7f63SJim Harris    }
554f11c7f63SJim Harris 
555f11c7f63SJim Harris    return remote_node_index;
556f11c7f63SJim Harris }
557f11c7f63SJim Harris 
558f11c7f63SJim Harris /**
559f11c7f63SJim Harris  * This method will free a single remote node index back to the remote node
560f11c7f63SJim Harris  * table.  This routine will update the remote node groups
561f11c7f63SJim Harris  *
562f11c7f63SJim Harris  * @param[in] remote_node_table
563f11c7f63SJim Harris  * @param[in] remote_node_index
564f11c7f63SJim Harris  */
565f11c7f63SJim Harris static
scic_sds_remote_node_table_release_single_remote_node(SCIC_REMOTE_NODE_TABLE_T * remote_node_table,U16 remote_node_index)566f11c7f63SJim Harris void scic_sds_remote_node_table_release_single_remote_node(
567f11c7f63SJim Harris    SCIC_REMOTE_NODE_TABLE_T * remote_node_table,
568f11c7f63SJim Harris    U16                        remote_node_index
569f11c7f63SJim Harris )
570f11c7f63SJim Harris {
571f11c7f63SJim Harris    U32 group_index;
572f11c7f63SJim Harris    U8  group_value;
573f11c7f63SJim Harris 
574f11c7f63SJim Harris    group_index = remote_node_index / SCU_STP_REMOTE_NODE_COUNT;
575f11c7f63SJim Harris 
576f11c7f63SJim Harris    group_value = scic_sds_remote_node_table_get_group_value(remote_node_table, group_index);
577f11c7f63SJim Harris 
578f11c7f63SJim Harris    // Assert that we are not trying to add an entry to a slot that is already
579f11c7f63SJim Harris    // full.
580f11c7f63SJim Harris    ASSERT(group_value != SCIC_SDS_REMOTE_NODE_TABLE_FULL_SLOT_VALUE);
581f11c7f63SJim Harris 
582f11c7f63SJim Harris    if (group_value == 0x00)
583f11c7f63SJim Harris    {
584f11c7f63SJim Harris       // There are no entries in this slot so it must be added to the single
585f11c7f63SJim Harris       // slot table.
586f11c7f63SJim Harris       scic_sds_remote_node_table_set_group_index(remote_node_table, 0, group_index);
587f11c7f63SJim Harris    }
588f11c7f63SJim Harris    else if ((group_value & (group_value -1)) == 0)
589f11c7f63SJim Harris    {
590f11c7f63SJim Harris       // There is only one entry in this slot so it must be moved from the
591f11c7f63SJim Harris       // single slot table to the dual slot table
592f11c7f63SJim Harris       scic_sds_remote_node_table_clear_group_index(remote_node_table, 0, group_index);
593f11c7f63SJim Harris       scic_sds_remote_node_table_set_group_index(remote_node_table, 1, group_index);
594f11c7f63SJim Harris    }
595f11c7f63SJim Harris    else
596f11c7f63SJim Harris    {
597f11c7f63SJim Harris       // There are two entries in the slot so it must be moved from the dual
598f11c7f63SJim Harris       // slot table to the tripple slot table.
599f11c7f63SJim Harris       scic_sds_remote_node_table_clear_group_index(remote_node_table, 1, group_index);
600f11c7f63SJim Harris       scic_sds_remote_node_table_set_group_index(remote_node_table, 2, group_index);
601f11c7f63SJim Harris    }
602f11c7f63SJim Harris 
603f11c7f63SJim Harris    scic_sds_remote_node_table_set_node_index(remote_node_table, remote_node_index);
604f11c7f63SJim Harris }
605f11c7f63SJim Harris 
606f11c7f63SJim Harris /**
607f11c7f63SJim Harris  * This method will release a group of three consecutive remote nodes back to
608f11c7f63SJim Harris  * the free remote nodes.
609f11c7f63SJim Harris  *
610f11c7f63SJim Harris  * @param[in] remote_node_table This is the remote node table to which the
611f11c7f63SJim Harris  *       remote node index is to be freed.
612f11c7f63SJim Harris  * @param[in] remote_node_index This is the remote node index which is being
613f11c7f63SJim Harris  *       freed.
614f11c7f63SJim Harris  */
615f11c7f63SJim Harris static
scic_sds_remote_node_table_release_triple_remote_node(SCIC_REMOTE_NODE_TABLE_T * remote_node_table,U16 remote_node_index)616f11c7f63SJim Harris void scic_sds_remote_node_table_release_triple_remote_node(
617f11c7f63SJim Harris    SCIC_REMOTE_NODE_TABLE_T * remote_node_table,
618f11c7f63SJim Harris    U16                        remote_node_index
619f11c7f63SJim Harris )
620f11c7f63SJim Harris {
621f11c7f63SJim Harris    U32 group_index;
622f11c7f63SJim Harris 
623f11c7f63SJim Harris    group_index = remote_node_index / SCU_STP_REMOTE_NODE_COUNT;
624f11c7f63SJim Harris 
625f11c7f63SJim Harris    scic_sds_remote_node_table_set_group_index(
626f11c7f63SJim Harris       remote_node_table, 2, group_index
627f11c7f63SJim Harris    );
628f11c7f63SJim Harris 
629f11c7f63SJim Harris    scic_sds_remote_node_table_set_group(remote_node_table, group_index);
630f11c7f63SJim Harris }
631f11c7f63SJim Harris 
632f11c7f63SJim Harris /**
633f11c7f63SJim Harris  * This method will release the remote node index back into the remote node
634f11c7f63SJim Harris  * table free pool.
635f11c7f63SJim Harris  *
636f11c7f63SJim Harris  * @param[in] remote_node_table The remote node table to which the remote node
637f11c7f63SJim Harris  *       index is to be freed.
638f11c7f63SJim Harris  * @param[in] remote_node_count This is the count of consecutive remote nodes
639f11c7f63SJim Harris  *       that are to be freed.
640f11c7f63SJim Harris  * @param[in] remote_node_index This is the remote node index of the start of
641f11c7f63SJim Harris  *       the number of remote nodes to be freed.
642f11c7f63SJim Harris  */
scic_sds_remote_node_table_release_remote_node_index(SCIC_REMOTE_NODE_TABLE_T * remote_node_table,U32 remote_node_count,U16 remote_node_index)643f11c7f63SJim Harris void scic_sds_remote_node_table_release_remote_node_index(
644f11c7f63SJim Harris    SCIC_REMOTE_NODE_TABLE_T * remote_node_table,
645f11c7f63SJim Harris    U32                        remote_node_count,
646f11c7f63SJim Harris    U16                        remote_node_index
647f11c7f63SJim Harris )
648f11c7f63SJim Harris {
649f11c7f63SJim Harris    if (remote_node_count == SCU_SSP_REMOTE_NODE_COUNT)
650f11c7f63SJim Harris    {
651f11c7f63SJim Harris       scic_sds_remote_node_table_release_single_remote_node(
652f11c7f63SJim Harris                                        remote_node_table, remote_node_index);
653f11c7f63SJim Harris    }
654f11c7f63SJim Harris    else if (remote_node_count == SCU_STP_REMOTE_NODE_COUNT)
655f11c7f63SJim Harris    {
656f11c7f63SJim Harris       scic_sds_remote_node_table_release_triple_remote_node(
657f11c7f63SJim Harris                                        remote_node_table, remote_node_index);
658f11c7f63SJim Harris    }
659f11c7f63SJim Harris }
660f11c7f63SJim Harris 
661