xref: /freebsd/sys/dev/isci/scil/sci_util.h (revision 2f02600abfddfc4e9f20dd384a2e729b451e16bd)
1 /*-
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.
4  *
5  * GPL LICENSE SUMMARY
6  *
7  * Copyright(c) 2008 - 2011 Intel Corporation. All rights reserved.
8  *
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.
12  *
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.
17  *
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.
23  *
24  * BSD LICENSE
25  *
26  * Copyright(c) 2008 - 2011 Intel Corporation. All rights reserved.
27  * All rights reserved.
28  *
29  * Redistribution and use in source and binary forms, with or without
30  * modification, are permitted provided that the following conditions
31  * are met:
32  *
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
38  *     distribution.
39  *
40  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
41  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
42  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
43  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
44  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
45  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
46  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
47  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
48  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
49  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
50  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
51  *
52  * $FreeBSD$
53  */
54 #ifndef _SCI_UTIL_H_
55 #define _SCI_UTIL_H_
56 
57 #include <dev/isci/scil/sci_types.h>
58 
59 #ifndef ARRAY_SIZE
60 #define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
61 #endif
62 
63 #ifndef MIN
64 #define MIN(x,y) ((x) < (y) ? (x) : (y))
65 #endif
66 
67 #ifndef MAX
68 #define MAX(x,y) ((x) > (y) ? (x) : (y))
69 #endif
70 
71 /**
72  * Normal byte swap macro
73  */
74 #define SCIC_SWAP_DWORD(x) \
75    ( \
76        (((x) >> 24) & 0x000000FF) \
77      | (((x) >>  8) & 0x0000FF00) \
78      | (((x) <<  8) & 0x00FF0000) \
79      | (((x) << 24) & 0xFF000000) \
80    )
81 
82 #define SCIC_BUILD_DWORD(char_buffer) \
83    ( \
84      ((char_buffer)[0] << 24) \
85    | ((char_buffer)[1] << 16) \
86    | ((char_buffer)[2] <<  8) \
87    | ((char_buffer)[3]) \
88    )
89 
90 #define SCI_FIELD_OFFSET(type, field)   ((POINTER_UINT)&(((type *)0)->field))
91 
92 //This macro counts how many bits being set in a mask.
93 #define SCI_GET_BITS_SET_COUNT(mask, set_bit_count)     \
94 {                                                  \
95    U8 index;                                       \
96    set_bit_count = 0;                              \
97    for (index = 0; index < sizeof(mask)*8; index++)            \
98    {                                               \
99       if( mask & (1<<index) )                      \
100          set_bit_count++;                          \
101    }                                               \
102 }
103 
104 /**
105  * This macro simply performs addition on an SCI_PHYSICAL_ADDRESS
106  * type.  The lower U32 value is "clipped" or "wrapped" back through
107  * 0.  When this occurs the upper 32-bits are incremented by 1.
108  */
109 #define sci_physical_address_add(physical_address, value) \
110 { \
111    U32 lower = sci_cb_physical_address_lower((physical_address)); \
112    U32 upper = sci_cb_physical_address_upper((physical_address)); \
113  \
114    if (lower + (value) < lower) \
115       upper += 1; \
116  \
117    lower += (value); \
118    sci_cb_make_physical_address(physical_address, upper, lower); \
119 }
120 
121 /**
122  * This macro simply performs subtraction on an SCI_PHYSICAL_ADDRESS
123  * type.  The lower U32 value is "clipped" or "wrapped" back through
124  * 0.  When this occurs the upper 32-bits are decremented by 1.
125  */
126 #define sci_physical_address_subtract(physical_address, value) \
127 { \
128    U32 lower = sci_cb_physical_address_lower((physical_address)); \
129    U32 upper = sci_cb_physical_address_upper((physical_address)); \
130  \
131    if (lower - (value) > lower) \
132       upper -= 1; \
133  \
134    lower -= (value); \
135    sci_cb_make_physical_address(physical_address, upper, lower); \
136 }
137 
138 /**
139  * @brief Copy the data from source to destination and swap the
140  *        bytes during the copy.
141  *
142  * @param[in] destination This parameter specifies the destination address
143  *            to which the data is to be copied.
144  * @param[in] source This parameter specifies the source address from
145  *            which data is to be copied.
146  * @param[in] word_count This parameter specifies the number of 32-bit words
147  *            to copy and byte swap.
148  *
149  * @return none
150  */
151 void scic_word_copy_with_swap(
152    U32 *destination,
153    U32 *source,
154    U32 word_count
155 );
156 
157 
158 #define sci_ssp_get_sense_data_length(sense_data_length_buffer) \
159    SCIC_BUILD_DWORD(sense_data_length_buffer)
160 
161 #define sci_ssp_get_response_data_length(response_data_length_buffer) \
162    SCIC_BUILD_DWORD(response_data_length_buffer)
163 
164 #endif // _SCI_UTIL_H_
165