xref: /freebsd/sys/dev/isci/scil/sci_util.h (revision c697fb7f)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause OR GPL-2.0
3  *
4  * This file is provided under a dual BSD/GPLv2 license.  When using or
5  * redistributing this file, you may do so under either license.
6  *
7  * GPL LICENSE SUMMARY
8  *
9  * Copyright(c) 2008 - 2011 Intel Corporation. All rights reserved.
10  *
11  * This program is free software; you can redistribute it and/or modify
12  * it under the terms of version 2 of the GNU General Public License as
13  * published by the Free Software Foundation.
14  *
15  * This program is distributed in the hope that it will be useful, but
16  * WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
18  * General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, write to the Free Software
22  * Foundation, Inc., 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
23  * The full GNU General Public License is included in this distribution
24  * in the file called LICENSE.GPL.
25  *
26  * BSD LICENSE
27  *
28  * Copyright(c) 2008 - 2011 Intel Corporation. All rights reserved.
29  * All rights reserved.
30  *
31  * Redistribution and use in source and binary forms, with or without
32  * modification, are permitted provided that the following conditions
33  * are met:
34  *
35  *   * Redistributions of source code must retain the above copyright
36  *     notice, this list of conditions and the following disclaimer.
37  *   * Redistributions in binary form must reproduce the above copyright
38  *     notice, this list of conditions and the following disclaimer in
39  *     the documentation and/or other materials provided with the
40  *     distribution.
41  *
42  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
43  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
44  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
45  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
46  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
47  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
48  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
49  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
50  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
51  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
52  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
53  *
54  * $FreeBSD$
55  */
56 #ifndef _SCI_UTIL_H_
57 #define _SCI_UTIL_H_
58 
59 #include <sys/param.h>
60 
61 #include <dev/isci/scil/sci_types.h>
62 
63 #ifndef ARRAY_SIZE
64 #define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
65 #endif
66 
67 /**
68  * Normal byte swap macro
69  */
70 #define SCIC_SWAP_DWORD(x) \
71    ( \
72        (((x) >> 24) & 0x000000FF) \
73      | (((x) >>  8) & 0x0000FF00) \
74      | (((x) <<  8) & 0x00FF0000) \
75      | (((x) << 24) & 0xFF000000) \
76    )
77 
78 #define SCIC_BUILD_DWORD(char_buffer) \
79    ( \
80      ((char_buffer)[0] << 24) \
81    | ((char_buffer)[1] << 16) \
82    | ((char_buffer)[2] <<  8) \
83    | ((char_buffer)[3]) \
84    )
85 
86 #define SCI_FIELD_OFFSET(type, field)   ((POINTER_UINT)&(((type *)0)->field))
87 
88 //This macro counts how many bits being set in a mask.
89 #define SCI_GET_BITS_SET_COUNT(mask, set_bit_count)     \
90 {                                                  \
91    U8 index;                                       \
92    set_bit_count = 0;                              \
93    for (index = 0; index < sizeof(mask)*8; index++)            \
94    {                                               \
95       if( mask & (1<<index) )                      \
96          set_bit_count++;                          \
97    }                                               \
98 }
99 
100 /**
101  * This macro simply performs addition on an SCI_PHYSICAL_ADDRESS
102  * type.  The lower U32 value is "clipped" or "wrapped" back through
103  * 0.  When this occurs the upper 32-bits are incremented by 1.
104  */
105 #define sci_physical_address_add(physical_address, value) \
106 { \
107    U32 lower = sci_cb_physical_address_lower((physical_address)); \
108    U32 upper = sci_cb_physical_address_upper((physical_address)); \
109  \
110    if (lower + (value) < lower) \
111       upper += 1; \
112  \
113    lower += (value); \
114    sci_cb_make_physical_address(physical_address, upper, lower); \
115 }
116 
117 /**
118  * This macro simply performs subtraction on an SCI_PHYSICAL_ADDRESS
119  * type.  The lower U32 value is "clipped" or "wrapped" back through
120  * 0.  When this occurs the upper 32-bits are decremented by 1.
121  */
122 #define sci_physical_address_subtract(physical_address, value) \
123 { \
124    U32 lower = sci_cb_physical_address_lower((physical_address)); \
125    U32 upper = sci_cb_physical_address_upper((physical_address)); \
126  \
127    if (lower - (value) > lower) \
128       upper -= 1; \
129  \
130    lower -= (value); \
131    sci_cb_make_physical_address(physical_address, upper, lower); \
132 }
133 
134 /**
135  * @brief Copy the data from source to destination and swap the
136  *        bytes during the copy.
137  *
138  * @param[in] destination This parameter specifies the destination address
139  *            to which the data is to be copied.
140  * @param[in] source This parameter specifies the source address from
141  *            which data is to be copied.
142  * @param[in] word_count This parameter specifies the number of 32-bit words
143  *            to copy and byte swap.
144  *
145  * @return none
146  */
147 void scic_word_copy_with_swap(
148    U32 *destination,
149    U32 *source,
150    U32 word_count
151 );
152 
153 
154 #define sci_ssp_get_sense_data_length(sense_data_length_buffer) \
155    SCIC_BUILD_DWORD(sense_data_length_buffer)
156 
157 #define sci_ssp_get_response_data_length(response_data_length_buffer) \
158    SCIC_BUILD_DWORD(response_data_length_buffer)
159 
160 #endif // _SCI_UTIL_H_
161