2 * This file is provided under a dual BSD/GPLv2 license. When using or
3 * redistributing this file, you may do so under either license.
7 * Copyright(c) 2008 - 2011 Intel Corporation. All rights reserved.
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of version 2 of the GNU General Public License as
11 * published by the Free Software Foundation.
13 * This program is distributed in the hope that it will be useful, but
14 * WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
21 * The full GNU General Public License is included in this distribution
22 * in the file called LICENSE.GPL.
26 * Copyright(c) 2008 - 2011 Intel Corporation. All rights reserved.
27 * All rights reserved.
29 * Redistribution and use in source and binary forms, with or without
30 * modification, are permitted provided that the following conditions
33 * * Redistributions of source code must retain the above copyright
34 * notice, this list of conditions and the following disclaimer.
35 * * Redistributions in binary form must reproduce the above copyright
36 * notice, this list of conditions and the following disclaimer in
37 * the documentation and/or other materials provided with the
39 * * Neither the name of Intel Corporation nor the names of its
40 * contributors may be used to endorse or promote products derived
41 * from this software without specific prior written permission.
43 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
44 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
45 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
46 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
47 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
48 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
49 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
50 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
51 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
52 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
53 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
57 * This file contains the implementation of the SCIC_SDS_REMOTE_NODE_TABLE
58 * public, protected, and private methods.
63 #include "remote_node_table.h"
64 #include "remote_node_context.h"
68 * @remote_node_table: This is the remote node index table from which the
69 * selection will be made.
70 * @group_table_index: This is the index to the group table from which to
71 * search for an available selection.
73 * This routine will find the bit position in absolute bit terms of the next 32
74 * + bit position. If there are available bits in the first u32 then it is
75 * just bit position. u32 This is the absolute bit position for an available
78 static u32
scic_sds_remote_node_table_get_group_index(
79 struct scic_remote_node_table
*remote_node_table
,
80 u32 group_table_index
)
86 group_table
= remote_node_table
->remote_node_groups
[group_table_index
];
88 for (dword_index
= 0; dword_index
< remote_node_table
->group_array_size
; dword_index
++) {
89 if (group_table
[dword_index
] != 0) {
90 for (bit_index
= 0; bit_index
< 32; bit_index
++) {
91 if ((group_table
[dword_index
] & (1 << bit_index
)) != 0) {
92 return (dword_index
* 32) + bit_index
;
98 return SCIC_SDS_REMOTE_NODE_TABLE_INVALID_INDEX
;
103 * @out]: remote_node_table This the remote node table in which to clear the
105 * @set_index: This is the remote node selector in which the change will be
107 * @group_index: This is the bit index in the table to be modified.
109 * This method will clear the group index entry in the specified group index
112 static void scic_sds_remote_node_table_clear_group_index(
113 struct scic_remote_node_table
*remote_node_table
,
114 u32 group_table_index
,
121 BUG_ON(group_table_index
>= SCU_STP_REMOTE_NODE_COUNT
);
122 BUG_ON(group_index
>= (u32
)(remote_node_table
->group_array_size
* 32));
124 dword_index
= group_index
/ 32;
125 bit_index
= group_index
% 32;
126 group_table
= remote_node_table
->remote_node_groups
[group_table_index
];
128 group_table
[dword_index
] = group_table
[dword_index
] & ~(1 << bit_index
);
133 * @out]: remote_node_table This the remote node table in which to set the
135 * @group_table_index: This is the remote node selector in which the change
137 * @group_index: This is the bit position in the table to be modified.
139 * This method will set the group index bit entry in the specified gropu index
142 static void scic_sds_remote_node_table_set_group_index(
143 struct scic_remote_node_table
*remote_node_table
,
144 u32 group_table_index
,
151 BUG_ON(group_table_index
>= SCU_STP_REMOTE_NODE_COUNT
);
152 BUG_ON(group_index
>= (u32
)(remote_node_table
->group_array_size
* 32));
154 dword_index
= group_index
/ 32;
155 bit_index
= group_index
% 32;
156 group_table
= remote_node_table
->remote_node_groups
[group_table_index
];
158 group_table
[dword_index
] = group_table
[dword_index
] | (1 << bit_index
);
163 * @out]: remote_node_table This is the remote node table in which to modify
164 * the remote node availability.
165 * @remote_node_index: This is the remote node index that is being returned to
168 * This method will set the remote to available in the remote node allocation
171 static void scic_sds_remote_node_table_set_node_index(
172 struct scic_remote_node_table
*remote_node_table
,
173 u32 remote_node_index
)
181 (remote_node_table
->available_nodes_array_size
* SCIC_SDS_REMOTE_NODE_SETS_PER_DWORD
)
182 <= (remote_node_index
/ SCU_STP_REMOTE_NODE_COUNT
)
185 dword_location
= remote_node_index
/ SCIC_SDS_REMOTE_NODES_PER_DWORD
;
186 dword_remainder
= remote_node_index
% SCIC_SDS_REMOTE_NODES_PER_DWORD
;
187 slot_normalized
= (dword_remainder
/ SCU_STP_REMOTE_NODE_COUNT
) * sizeof(u32
);
188 slot_position
= remote_node_index
% SCU_STP_REMOTE_NODE_COUNT
;
190 remote_node_table
->available_remote_nodes
[dword_location
] |=
191 1 << (slot_normalized
+ slot_position
);
196 * @out]: remote_node_table This is the remote node table from which to clear
197 * the available remote node bit.
198 * @remote_node_index: This is the remote node index which is to be cleared
201 * This method clears the remote node index from the table of available remote
204 static void scic_sds_remote_node_table_clear_node_index(
205 struct scic_remote_node_table
*remote_node_table
,
206 u32 remote_node_index
)
214 (remote_node_table
->available_nodes_array_size
* SCIC_SDS_REMOTE_NODE_SETS_PER_DWORD
)
215 <= (remote_node_index
/ SCU_STP_REMOTE_NODE_COUNT
)
218 dword_location
= remote_node_index
/ SCIC_SDS_REMOTE_NODES_PER_DWORD
;
219 dword_remainder
= remote_node_index
% SCIC_SDS_REMOTE_NODES_PER_DWORD
;
220 slot_normalized
= (dword_remainder
/ SCU_STP_REMOTE_NODE_COUNT
) * sizeof(u32
);
221 slot_position
= remote_node_index
% SCU_STP_REMOTE_NODE_COUNT
;
223 remote_node_table
->available_remote_nodes
[dword_location
] &=
224 ~(1 << (slot_normalized
+ slot_position
));
229 * @out]: remote_node_table The remote node table from which the slot will be
231 * @group_index: The index for the slot that is to be cleared.
233 * This method clears the entire table slot at the specified slot index. none
235 static void scic_sds_remote_node_table_clear_group(
236 struct scic_remote_node_table
*remote_node_table
,
244 (remote_node_table
->available_nodes_array_size
* SCIC_SDS_REMOTE_NODE_SETS_PER_DWORD
)
245 <= (group_index
/ SCU_STP_REMOTE_NODE_COUNT
)
248 dword_location
= group_index
/ SCIC_SDS_REMOTE_NODE_SETS_PER_DWORD
;
249 dword_remainder
= group_index
% SCIC_SDS_REMOTE_NODE_SETS_PER_DWORD
;
251 dword_value
= remote_node_table
->available_remote_nodes
[dword_location
];
252 dword_value
&= ~(SCIC_SDS_REMOTE_NODE_TABLE_FULL_SLOT_VALUE
<< (dword_remainder
* 4));
253 remote_node_table
->available_remote_nodes
[dword_location
] = dword_value
;
258 * @remote_node_table:
260 * THis method sets an entire remote node group in the remote node table.
262 static void scic_sds_remote_node_table_set_group(
263 struct scic_remote_node_table
*remote_node_table
,
271 (remote_node_table
->available_nodes_array_size
* SCIC_SDS_REMOTE_NODE_SETS_PER_DWORD
)
272 <= (group_index
/ SCU_STP_REMOTE_NODE_COUNT
)
275 dword_location
= group_index
/ SCIC_SDS_REMOTE_NODE_SETS_PER_DWORD
;
276 dword_remainder
= group_index
% SCIC_SDS_REMOTE_NODE_SETS_PER_DWORD
;
278 dword_value
= remote_node_table
->available_remote_nodes
[dword_location
];
279 dword_value
|= (SCIC_SDS_REMOTE_NODE_TABLE_FULL_SLOT_VALUE
<< (dword_remainder
* 4));
280 remote_node_table
->available_remote_nodes
[dword_location
] = dword_value
;
285 * @remote_node_table: This is the remote node table that for which the group
286 * value is to be returned.
287 * @group_index: This is the group index to use to find the group value.
289 * This method will return the group value for the specified group index. The
290 * bit values at the specified remote node group index.
292 static u8
scic_sds_remote_node_table_get_group_value(
293 struct scic_remote_node_table
*remote_node_table
,
300 dword_location
= group_index
/ SCIC_SDS_REMOTE_NODE_SETS_PER_DWORD
;
301 dword_remainder
= group_index
% SCIC_SDS_REMOTE_NODE_SETS_PER_DWORD
;
303 dword_value
= remote_node_table
->available_remote_nodes
[dword_location
];
304 dword_value
&= (SCIC_SDS_REMOTE_NODE_TABLE_FULL_SLOT_VALUE
<< (dword_remainder
* 4));
305 dword_value
= dword_value
>> (dword_remainder
* 4);
307 return (u8
)dword_value
;
312 * @out]: remote_node_table The remote that which is to be initialized.
313 * @remote_node_entries: The number of entries to put in the table.
315 * This method will initialize the remote node table for use. none
317 void scic_sds_remote_node_table_initialize(
318 struct scic_remote_node_table
*remote_node_table
,
319 u32 remote_node_entries
)
324 * Initialize the raw data we could improve the speed by only initializing
325 * those entries that we are actually going to be used */
327 remote_node_table
->available_remote_nodes
,
329 sizeof(remote_node_table
->available_remote_nodes
)
333 remote_node_table
->remote_node_groups
,
335 sizeof(remote_node_table
->remote_node_groups
)
338 /* Initialize the available remote node sets */
339 remote_node_table
->available_nodes_array_size
= (u16
)
340 (remote_node_entries
/ SCIC_SDS_REMOTE_NODES_PER_DWORD
)
341 + ((remote_node_entries
% SCIC_SDS_REMOTE_NODES_PER_DWORD
) != 0);
344 /* Initialize each full DWORD to a FULL SET of remote nodes */
345 for (index
= 0; index
< remote_node_entries
; index
++) {
346 scic_sds_remote_node_table_set_node_index(remote_node_table
, index
);
349 remote_node_table
->group_array_size
= (u16
)
350 (remote_node_entries
/ (SCU_STP_REMOTE_NODE_COUNT
* 32))
351 + ((remote_node_entries
% (SCU_STP_REMOTE_NODE_COUNT
* 32)) != 0);
353 for (index
= 0; index
< (remote_node_entries
/ SCU_STP_REMOTE_NODE_COUNT
); index
++) {
355 * These are all guaranteed to be full slot values so fill them in the
356 * available sets of 3 remote nodes */
357 scic_sds_remote_node_table_set_group_index(remote_node_table
, 2, index
);
360 /* Now fill in any remainders that we may find */
361 if ((remote_node_entries
% SCU_STP_REMOTE_NODE_COUNT
) == 2) {
362 scic_sds_remote_node_table_set_group_index(remote_node_table
, 1, index
);
363 } else if ((remote_node_entries
% SCU_STP_REMOTE_NODE_COUNT
) == 1) {
364 scic_sds_remote_node_table_set_group_index(remote_node_table
, 0, index
);
370 * @out]: remote_node_table The remote node table from which to allocate a
372 * @table_index: The group index that is to be used for the search.
374 * This method will allocate a single RNi from the remote node table. The
375 * table index will determine from which remote node group table to search.
376 * This search may fail and another group node table can be specified. The
377 * function is designed to allow a serach of the available single remote node
378 * group up to the triple remote node group. If an entry is found in the
379 * specified table the remote node is removed and the remote node groups are
380 * updated. The RNi value or an invalid remote node context if an RNi can not
383 static u16
scic_sds_remote_node_table_allocate_single_remote_node(
384 struct scic_remote_node_table
*remote_node_table
,
385 u32 group_table_index
)
390 u16 remote_node_index
= SCIC_SDS_REMOTE_NODE_CONTEXT_INVALID_INDEX
;
392 group_index
= scic_sds_remote_node_table_get_group_index(
393 remote_node_table
, group_table_index
);
395 /* We could not find an available slot in the table selector 0 */
396 if (group_index
!= SCIC_SDS_REMOTE_NODE_TABLE_INVALID_INDEX
) {
397 group_value
= scic_sds_remote_node_table_get_group_value(
398 remote_node_table
, group_index
);
400 for (index
= 0; index
< SCU_STP_REMOTE_NODE_COUNT
; index
++) {
401 if (((1 << index
) & group_value
) != 0) {
402 /* We have selected a bit now clear it */
403 remote_node_index
= (u16
)(group_index
* SCU_STP_REMOTE_NODE_COUNT
406 scic_sds_remote_node_table_clear_group_index(
407 remote_node_table
, group_table_index
, group_index
410 scic_sds_remote_node_table_clear_node_index(
411 remote_node_table
, remote_node_index
414 if (group_table_index
> 0) {
415 scic_sds_remote_node_table_set_group_index(
416 remote_node_table
, group_table_index
- 1, group_index
425 return remote_node_index
;
430 * @remote_node_table: This is the remote node table from which to allocate the
431 * remote node entries.
432 * @group_table_index: THis is the group table index which must equal two (2)
433 * for this operation.
435 * This method will allocate three consecutive remote node context entries. If
436 * there are no remaining triple entries the function will return a failure.
437 * The remote node index that represents three consecutive remote node entries
438 * or an invalid remote node context if none can be found.
440 static u16
scic_sds_remote_node_table_allocate_triple_remote_node(
441 struct scic_remote_node_table
*remote_node_table
,
442 u32 group_table_index
)
445 u16 remote_node_index
= SCIC_SDS_REMOTE_NODE_CONTEXT_INVALID_INDEX
;
447 group_index
= scic_sds_remote_node_table_get_group_index(
448 remote_node_table
, group_table_index
);
450 if (group_index
!= SCIC_SDS_REMOTE_NODE_TABLE_INVALID_INDEX
) {
451 remote_node_index
= (u16
)group_index
* SCU_STP_REMOTE_NODE_COUNT
;
453 scic_sds_remote_node_table_clear_group_index(
454 remote_node_table
, group_table_index
, group_index
457 scic_sds_remote_node_table_clear_group(
458 remote_node_table
, group_index
462 return remote_node_index
;
467 * @remote_node_table: This is the remote node table from which the remote node
468 * allocation is to take place.
469 * @remote_node_count: This is ther remote node count which is one of
470 * SCU_SSP_REMOTE_NODE_COUNT(1) or SCU_STP_REMOTE_NODE_COUNT(3).
472 * This method will allocate a remote node that mataches the remote node count
473 * specified by the caller. Valid values for remote node count is
474 * SCU_SSP_REMOTE_NODE_COUNT(1) or SCU_STP_REMOTE_NODE_COUNT(3). u16 This is
475 * the remote node index that is returned or an invalid remote node context.
477 u16
scic_sds_remote_node_table_allocate_remote_node(
478 struct scic_remote_node_table
*remote_node_table
,
479 u32 remote_node_count
)
481 u16 remote_node_index
= SCIC_SDS_REMOTE_NODE_CONTEXT_INVALID_INDEX
;
483 if (remote_node_count
== SCU_SSP_REMOTE_NODE_COUNT
) {
485 scic_sds_remote_node_table_allocate_single_remote_node(
486 remote_node_table
, 0);
488 if (remote_node_index
== SCIC_SDS_REMOTE_NODE_CONTEXT_INVALID_INDEX
) {
490 scic_sds_remote_node_table_allocate_single_remote_node(
491 remote_node_table
, 1);
494 if (remote_node_index
== SCIC_SDS_REMOTE_NODE_CONTEXT_INVALID_INDEX
) {
496 scic_sds_remote_node_table_allocate_single_remote_node(
497 remote_node_table
, 2);
499 } else if (remote_node_count
== SCU_STP_REMOTE_NODE_COUNT
) {
501 scic_sds_remote_node_table_allocate_triple_remote_node(
502 remote_node_table
, 2);
505 return remote_node_index
;
510 * @remote_node_table:
512 * This method will free a single remote node index back to the remote node
513 * table. This routine will update the remote node groups
515 static void scic_sds_remote_node_table_release_single_remote_node(
516 struct scic_remote_node_table
*remote_node_table
,
517 u16 remote_node_index
)
522 group_index
= remote_node_index
/ SCU_STP_REMOTE_NODE_COUNT
;
524 group_value
= scic_sds_remote_node_table_get_group_value(remote_node_table
, group_index
);
527 * Assert that we are not trying to add an entry to a slot that is already
529 BUG_ON(group_value
== SCIC_SDS_REMOTE_NODE_TABLE_FULL_SLOT_VALUE
);
531 if (group_value
== 0x00) {
533 * There are no entries in this slot so it must be added to the single
535 scic_sds_remote_node_table_set_group_index(remote_node_table
, 0, group_index
);
536 } else if ((group_value
& (group_value
- 1)) == 0) {
538 * There is only one entry in this slot so it must be moved from the
539 * single slot table to the dual slot table */
540 scic_sds_remote_node_table_clear_group_index(remote_node_table
, 0, group_index
);
541 scic_sds_remote_node_table_set_group_index(remote_node_table
, 1, group_index
);
544 * There are two entries in the slot so it must be moved from the dual
545 * slot table to the tripple slot table. */
546 scic_sds_remote_node_table_clear_group_index(remote_node_table
, 1, group_index
);
547 scic_sds_remote_node_table_set_group_index(remote_node_table
, 2, group_index
);
550 scic_sds_remote_node_table_set_node_index(remote_node_table
, remote_node_index
);
555 * @remote_node_table: This is the remote node table to which the remote node
556 * index is to be freed.
558 * This method will release a group of three consecutive remote nodes back to
559 * the free remote nodes.
561 static void scic_sds_remote_node_table_release_triple_remote_node(
562 struct scic_remote_node_table
*remote_node_table
,
563 u16 remote_node_index
)
567 group_index
= remote_node_index
/ SCU_STP_REMOTE_NODE_COUNT
;
569 scic_sds_remote_node_table_set_group_index(
570 remote_node_table
, 2, group_index
573 scic_sds_remote_node_table_set_group(remote_node_table
, group_index
);
578 * @remote_node_table: The remote node table to which the remote node index is
580 * @remote_node_count: This is the count of consecutive remote nodes that are
583 * This method will release the remote node index back into the remote node
586 void scic_sds_remote_node_table_release_remote_node_index(
587 struct scic_remote_node_table
*remote_node_table
,
588 u32 remote_node_count
,
589 u16 remote_node_index
)
591 if (remote_node_count
== SCU_SSP_REMOTE_NODE_COUNT
) {
592 scic_sds_remote_node_table_release_single_remote_node(
593 remote_node_table
, remote_node_index
);
594 } else if (remote_node_count
== SCU_STP_REMOTE_NODE_COUNT
) {
595 scic_sds_remote_node_table_release_triple_remote_node(
596 remote_node_table
, remote_node_index
);