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