1230557Sjimharris/*-
2230557Sjimharris * This file is provided under a dual BSD/GPLv2 license.  When using or
3230557Sjimharris * redistributing this file, you may do so under either license.
4230557Sjimharris *
5230557Sjimharris * GPL LICENSE SUMMARY
6230557Sjimharris *
7230557Sjimharris * Copyright(c) 2008 - 2011 Intel Corporation. All rights reserved.
8230557Sjimharris *
9230557Sjimharris * This program is free software; you can redistribute it and/or modify
10230557Sjimharris * it under the terms of version 2 of the GNU General Public License as
11230557Sjimharris * published by the Free Software Foundation.
12230557Sjimharris *
13230557Sjimharris * This program is distributed in the hope that it will be useful, but
14230557Sjimharris * WITHOUT ANY WARRANTY; without even the implied warranty of
15230557Sjimharris * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16230557Sjimharris * General Public License for more details.
17230557Sjimharris *
18230557Sjimharris * You should have received a copy of the GNU General Public License
19230557Sjimharris * along with this program; if not, write to the Free Software
20230557Sjimharris * Foundation, Inc., 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
21230557Sjimharris * The full GNU General Public License is included in this distribution
22230557Sjimharris * in the file called LICENSE.GPL.
23230557Sjimharris *
24230557Sjimharris * BSD LICENSE
25230557Sjimharris *
26230557Sjimharris * Copyright(c) 2008 - 2011 Intel Corporation. All rights reserved.
27230557Sjimharris * All rights reserved.
28230557Sjimharris *
29230557Sjimharris * Redistribution and use in source and binary forms, with or without
30230557Sjimharris * modification, are permitted provided that the following conditions
31230557Sjimharris * are met:
32230557Sjimharris *
33230557Sjimharris *   * Redistributions of source code must retain the above copyright
34230557Sjimharris *     notice, this list of conditions and the following disclaimer.
35230557Sjimharris *   * Redistributions in binary form must reproduce the above copyright
36230557Sjimharris *     notice, this list of conditions and the following disclaimer in
37230557Sjimharris *     the documentation and/or other materials provided with the
38230557Sjimharris *     distribution.
39230557Sjimharris *
40230557Sjimharris * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
41230557Sjimharris * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
42230557Sjimharris * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
43230557Sjimharris * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
44230557Sjimharris * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
45230557Sjimharris * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
46230557Sjimharris * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
47230557Sjimharris * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
48230557Sjimharris * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
49230557Sjimharris * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
50230557Sjimharris * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
51230557Sjimharris *
52230557Sjimharris * $FreeBSD: releng/10.3/sys/dev/isci/scil/sci_util.h 265571 2014-05-07 16:57:33Z jimharris $
53230557Sjimharris */
54230557Sjimharris#ifndef _SCI_UTIL_H_
55230557Sjimharris#define _SCI_UTIL_H_
56230557Sjimharris
57265571Sjimharris#include <sys/param.h>
58265571Sjimharris
59230557Sjimharris#include <dev/isci/scil/sci_types.h>
60230557Sjimharris
61230557Sjimharris#ifndef ARRAY_SIZE
62230557Sjimharris#define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
63230557Sjimharris#endif
64230557Sjimharris
65230557Sjimharris/**
66230557Sjimharris * Normal byte swap macro
67230557Sjimharris */
68230557Sjimharris#define SCIC_SWAP_DWORD(x) \
69230557Sjimharris   ( \
70230557Sjimharris       (((x) >> 24) & 0x000000FF) \
71230557Sjimharris     | (((x) >>  8) & 0x0000FF00) \
72230557Sjimharris     | (((x) <<  8) & 0x00FF0000) \
73230557Sjimharris     | (((x) << 24) & 0xFF000000) \
74230557Sjimharris   )
75230557Sjimharris
76230557Sjimharris#define SCIC_BUILD_DWORD(char_buffer) \
77230557Sjimharris   ( \
78230557Sjimharris     ((char_buffer)[0] << 24) \
79230557Sjimharris   | ((char_buffer)[1] << 16) \
80230557Sjimharris   | ((char_buffer)[2] <<  8) \
81230557Sjimharris   | ((char_buffer)[3]) \
82230557Sjimharris   )
83230557Sjimharris
84230557Sjimharris#define SCI_FIELD_OFFSET(type, field)   ((POINTER_UINT)&(((type *)0)->field))
85230557Sjimharris
86230557Sjimharris//This macro counts how many bits being set in a mask.
87230557Sjimharris#define SCI_GET_BITS_SET_COUNT(mask, set_bit_count)     \
88230557Sjimharris{                                                  \
89230557Sjimharris   U8 index;                                       \
90230557Sjimharris   set_bit_count = 0;                              \
91230557Sjimharris   for (index = 0; index < sizeof(mask)*8; index++)            \
92230557Sjimharris   {                                               \
93230557Sjimharris      if( mask & (1<<index) )                      \
94230557Sjimharris         set_bit_count++;                          \
95230557Sjimharris   }                                               \
96230557Sjimharris}
97230557Sjimharris
98230557Sjimharris/**
99230557Sjimharris * This macro simply performs addition on an SCI_PHYSICAL_ADDRESS
100230557Sjimharris * type.  The lower U32 value is "clipped" or "wrapped" back through
101230557Sjimharris * 0.  When this occurs the upper 32-bits are incremented by 1.
102230557Sjimharris */
103230557Sjimharris#define sci_physical_address_add(physical_address, value) \
104230557Sjimharris{ \
105230557Sjimharris   U32 lower = sci_cb_physical_address_lower((physical_address)); \
106230557Sjimharris   U32 upper = sci_cb_physical_address_upper((physical_address)); \
107230557Sjimharris \
108230557Sjimharris   if (lower + (value) < lower) \
109230557Sjimharris      upper += 1; \
110230557Sjimharris \
111230557Sjimharris   lower += (value); \
112230557Sjimharris   sci_cb_make_physical_address(physical_address, upper, lower); \
113230557Sjimharris}
114230557Sjimharris
115230557Sjimharris/**
116230557Sjimharris * This macro simply performs subtraction on an SCI_PHYSICAL_ADDRESS
117230557Sjimharris * type.  The lower U32 value is "clipped" or "wrapped" back through
118230557Sjimharris * 0.  When this occurs the upper 32-bits are decremented by 1.
119230557Sjimharris */
120230557Sjimharris#define sci_physical_address_subtract(physical_address, value) \
121230557Sjimharris{ \
122230557Sjimharris   U32 lower = sci_cb_physical_address_lower((physical_address)); \
123230557Sjimharris   U32 upper = sci_cb_physical_address_upper((physical_address)); \
124230557Sjimharris \
125230557Sjimharris   if (lower - (value) > lower) \
126230557Sjimharris      upper -= 1; \
127230557Sjimharris \
128230557Sjimharris   lower -= (value); \
129230557Sjimharris   sci_cb_make_physical_address(physical_address, upper, lower); \
130230557Sjimharris}
131230557Sjimharris
132230557Sjimharris/**
133230557Sjimharris * @brief Copy the data from source to destination and swap the
134230557Sjimharris *        bytes during the copy.
135230557Sjimharris *
136230557Sjimharris * @param[in] destination This parameter specifies the destination address
137230557Sjimharris *            to which the data is to be copied.
138230557Sjimharris * @param[in] source This parameter specifies the source address from
139230557Sjimharris *            which data is to be copied.
140230557Sjimharris * @param[in] word_count This parameter specifies the number of 32-bit words
141230557Sjimharris *            to copy and byte swap.
142230557Sjimharris *
143230557Sjimharris * @return none
144230557Sjimharris */
145230557Sjimharrisvoid scic_word_copy_with_swap(
146230557Sjimharris   U32 *destination,
147230557Sjimharris   U32 *source,
148230557Sjimharris   U32 word_count
149230557Sjimharris);
150230557Sjimharris
151230557Sjimharris
152230557Sjimharris#define sci_ssp_get_sense_data_length(sense_data_length_buffer) \
153230557Sjimharris   SCIC_BUILD_DWORD(sense_data_length_buffer)
154230557Sjimharris
155230557Sjimharris#define sci_ssp_get_response_data_length(response_data_length_buffer) \
156230557Sjimharris   SCIC_BUILD_DWORD(response_data_length_buffer)
157230557Sjimharris
158230557Sjimharris#endif // _SCI_UTIL_H_
159