xref: /freebsd/sys/dev/isci/scil/sci_util.h (revision 95ee2897e98f5d444f26ed2334cc7c439f9c16c6)
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 #ifndef _SCI_UTIL_H_
55f11c7f63SJim Harris #define _SCI_UTIL_H_
56f11c7f63SJim Harris 
575d16b897SJim Harris #include <sys/param.h>
585d16b897SJim Harris 
59f11c7f63SJim Harris #include <dev/isci/scil/sci_types.h>
60f11c7f63SJim Harris 
61f11c7f63SJim Harris #ifndef ARRAY_SIZE
62f11c7f63SJim Harris #define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
63f11c7f63SJim Harris #endif
64f11c7f63SJim Harris 
65f11c7f63SJim Harris /**
66f11c7f63SJim Harris  * Normal byte swap macro
67f11c7f63SJim Harris  */
68f11c7f63SJim Harris #define SCIC_SWAP_DWORD(x) \
69f11c7f63SJim Harris    ( \
70f11c7f63SJim Harris        (((x) >> 24) & 0x000000FF) \
71f11c7f63SJim Harris      | (((x) >>  8) & 0x0000FF00) \
72f11c7f63SJim Harris      | (((x) <<  8) & 0x00FF0000) \
73f11c7f63SJim Harris      | (((x) << 24) & 0xFF000000) \
74f11c7f63SJim Harris    )
75f11c7f63SJim Harris 
76f11c7f63SJim Harris #define SCIC_BUILD_DWORD(char_buffer) \
77f11c7f63SJim Harris    ( \
78f11c7f63SJim Harris      ((char_buffer)[0] << 24) \
79f11c7f63SJim Harris    | ((char_buffer)[1] << 16) \
80f11c7f63SJim Harris    | ((char_buffer)[2] <<  8) \
81f11c7f63SJim Harris    | ((char_buffer)[3]) \
82f11c7f63SJim Harris    )
83f11c7f63SJim Harris 
84f11c7f63SJim Harris #define SCI_FIELD_OFFSET(type, field)   ((POINTER_UINT)&(((type *)0)->field))
85f11c7f63SJim Harris 
86f11c7f63SJim Harris //This macro counts how many bits being set in a mask.
87f11c7f63SJim Harris #define SCI_GET_BITS_SET_COUNT(mask, set_bit_count)     \
88f11c7f63SJim Harris {                                                  \
89f11c7f63SJim Harris    U8 index;                                       \
90f11c7f63SJim Harris    set_bit_count = 0;                              \
91f11c7f63SJim Harris    for (index = 0; index < sizeof(mask)*8; index++)            \
92f11c7f63SJim Harris    {                                               \
93f11c7f63SJim Harris       if( mask & (1<<index) )                      \
94f11c7f63SJim Harris          set_bit_count++;                          \
95f11c7f63SJim Harris    }                                               \
96f11c7f63SJim Harris }
97f11c7f63SJim Harris 
98f11c7f63SJim Harris /**
99f11c7f63SJim Harris  * This macro simply performs addition on an SCI_PHYSICAL_ADDRESS
100f11c7f63SJim Harris  * type.  The lower U32 value is "clipped" or "wrapped" back through
101f11c7f63SJim Harris  * 0.  When this occurs the upper 32-bits are incremented by 1.
102f11c7f63SJim Harris  */
103f11c7f63SJim Harris #define sci_physical_address_add(physical_address, value) \
104f11c7f63SJim Harris { \
105f11c7f63SJim Harris    U32 lower = sci_cb_physical_address_lower((physical_address)); \
106f11c7f63SJim Harris    U32 upper = sci_cb_physical_address_upper((physical_address)); \
107f11c7f63SJim Harris  \
108f11c7f63SJim Harris    if (lower + (value) < lower) \
109f11c7f63SJim Harris       upper += 1; \
110f11c7f63SJim Harris  \
111f11c7f63SJim Harris    lower += (value); \
112f11c7f63SJim Harris    sci_cb_make_physical_address(physical_address, upper, lower); \
113f11c7f63SJim Harris }
114f11c7f63SJim Harris 
115f11c7f63SJim Harris /**
116f11c7f63SJim Harris  * This macro simply performs subtraction on an SCI_PHYSICAL_ADDRESS
117f11c7f63SJim Harris  * type.  The lower U32 value is "clipped" or "wrapped" back through
118f11c7f63SJim Harris  * 0.  When this occurs the upper 32-bits are decremented by 1.
119f11c7f63SJim Harris  */
120f11c7f63SJim Harris #define sci_physical_address_subtract(physical_address, value) \
121f11c7f63SJim Harris { \
122f11c7f63SJim Harris    U32 lower = sci_cb_physical_address_lower((physical_address)); \
123f11c7f63SJim Harris    U32 upper = sci_cb_physical_address_upper((physical_address)); \
124f11c7f63SJim Harris  \
125f11c7f63SJim Harris    if (lower - (value) > lower) \
126f11c7f63SJim Harris       upper -= 1; \
127f11c7f63SJim Harris  \
128f11c7f63SJim Harris    lower -= (value); \
129f11c7f63SJim Harris    sci_cb_make_physical_address(physical_address, upper, lower); \
130f11c7f63SJim Harris }
131f11c7f63SJim Harris 
132f11c7f63SJim Harris /**
133f11c7f63SJim Harris  * @brief Copy the data from source to destination and swap the
134f11c7f63SJim Harris  *        bytes during the copy.
135f11c7f63SJim Harris  *
136f11c7f63SJim Harris  * @param[in] destination This parameter specifies the destination address
137f11c7f63SJim Harris  *            to which the data is to be copied.
138f11c7f63SJim Harris  * @param[in] source This parameter specifies the source address from
139f11c7f63SJim Harris  *            which data is to be copied.
140f11c7f63SJim Harris  * @param[in] word_count This parameter specifies the number of 32-bit words
141f11c7f63SJim Harris  *            to copy and byte swap.
142f11c7f63SJim Harris  *
143f11c7f63SJim Harris  * @return none
144f11c7f63SJim Harris  */
145f11c7f63SJim Harris void scic_word_copy_with_swap(
146f11c7f63SJim Harris    U32 *destination,
147f11c7f63SJim Harris    U32 *source,
148f11c7f63SJim Harris    U32 word_count
149f11c7f63SJim Harris );
150f11c7f63SJim Harris 
151f11c7f63SJim Harris 
152f11c7f63SJim Harris #define sci_ssp_get_sense_data_length(sense_data_length_buffer) \
153f11c7f63SJim Harris    SCIC_BUILD_DWORD(sense_data_length_buffer)
154f11c7f63SJim Harris 
155f11c7f63SJim Harris #define sci_ssp_get_response_data_length(response_data_length_buffer) \
156f11c7f63SJim Harris    SCIC_BUILD_DWORD(response_data_length_buffer)
157f11c7f63SJim Harris 
158f11c7f63SJim Harris #endif // _SCI_UTIL_H_
159