1 /*-
2 * SPDX-License-Identifier: BSD-2-Clause
3 *
4 * Copyright (c) 2019 Leandro Lupori
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25 * SUCH DAMAGE.
26 */
27
28 #include <sys/cdefs.h>
29 #include <sys/param.h>
30 #include <sys/types.h>
31
32 #include <dev/aacraid/aacraid_reg.h>
33 #include <dev/aacraid/aacraid_endian.h>
34
35 #if _BYTE_ORDER != _LITTLE_ENDIAN
36
37 #define TOH2(field, bits) field = le##bits##toh(field)
38 #define TOH(field, bits) TOH2(field, bits)
39
40 #define TOLE2(field, bits) field = htole##bits(field)
41 #define TOLE(field, bits) TOLE2(field, bits)
42
43 /* Convert from Little-Endian to host order (TOH) */
44
45 void
aac_fib_header_toh(struct aac_fib_header * ptr)46 aac_fib_header_toh(struct aac_fib_header *ptr)
47 {
48 TOH(ptr->XferState, 32);
49 TOH(ptr->Command, 16);
50 TOH(ptr->Size, 16);
51 TOH(ptr->SenderSize, 16);
52 TOH(ptr->SenderFibAddress, 32);
53 TOH(ptr->u.ReceiverFibAddress, 32);
54 TOH(ptr->Handle, 32);
55 TOH(ptr->Previous, 32);
56 TOH(ptr->Next, 32);
57 }
58
59 void
aac_adapter_info_toh(struct aac_adapter_info * ptr)60 aac_adapter_info_toh(struct aac_adapter_info *ptr)
61 {
62 TOH(ptr->PlatformBase, 32);
63 TOH(ptr->CpuArchitecture, 32);
64 TOH(ptr->CpuVariant, 32);
65 TOH(ptr->ClockSpeed, 32);
66 TOH(ptr->ExecutionMem, 32);
67 TOH(ptr->BufferMem, 32);
68 TOH(ptr->TotalMem, 32);
69
70 TOH(ptr->KernelRevision.buildNumber, 32);
71 TOH(ptr->MonitorRevision.buildNumber, 32);
72 TOH(ptr->HardwareRevision.buildNumber, 32);
73 TOH(ptr->BIOSRevision.buildNumber, 32);
74
75 TOH(ptr->ClusteringEnabled, 32);
76 TOH(ptr->ClusterChannelMask, 32);
77 TOH(ptr->SerialNumber, 64);
78 TOH(ptr->batteryPlatform, 32);
79 TOH(ptr->SupportedOptions, 32);
80 TOH(ptr->OemVariant, 32);
81 }
82
83 void
aac_container_creation_toh(struct aac_container_creation * ptr)84 aac_container_creation_toh(struct aac_container_creation *ptr)
85 {
86 u_int32_t *date = (u_int32_t *)ptr + 1;
87
88 *date = le32toh(*date);
89 TOH(ptr->ViaAdapterSerialNumber, 64);
90 }
91
92 void
aac_mntobj_toh(struct aac_mntobj * ptr)93 aac_mntobj_toh(struct aac_mntobj *ptr)
94 {
95 TOH(ptr->ObjectId, 32);
96 aac_container_creation_toh(&ptr->CreateInfo);
97 TOH(ptr->Capacity, 32);
98 TOH(ptr->VolType, 32);
99 TOH(ptr->ObjType, 32);
100 TOH(ptr->ContentState, 32);
101 TOH(ptr->ObjExtension.BlockDevice.BlockSize, 32);
102 TOH(ptr->ObjExtension.BlockDevice.bdLgclPhysMap, 32);
103 TOH(ptr->AlterEgoId, 32);
104 TOH(ptr->CapacityHigh, 32);
105 }
106
107 void
aac_mntinforesp_toh(struct aac_mntinforesp * ptr)108 aac_mntinforesp_toh(struct aac_mntinforesp *ptr)
109 {
110 TOH(ptr->Status, 32);
111 TOH(ptr->MntType, 32);
112 TOH(ptr->MntRespCount, 32);
113 aac_mntobj_toh(&ptr->MntTable[0]);
114 }
115
116 void
aac_fsa_ctm_toh(struct aac_fsa_ctm * ptr)117 aac_fsa_ctm_toh(struct aac_fsa_ctm *ptr)
118 {
119 int i;
120
121 TOH(ptr->command, 32);
122 for (i = 0; i < CT_FIB_PARAMS; i++)
123 TOH(ptr->param[i], 32);
124 }
125
126 void
aac_cnt_config_toh(struct aac_cnt_config * ptr)127 aac_cnt_config_toh(struct aac_cnt_config *ptr)
128 {
129 TOH(ptr->Command, 32);
130 aac_fsa_ctm_toh(&ptr->CTCommand);
131 }
132
133 void
aac_ctcfg_resp_toh(struct aac_ctcfg_resp * ptr)134 aac_ctcfg_resp_toh(struct aac_ctcfg_resp *ptr)
135 {
136 TOH(ptr->Status, 32);
137 TOH(ptr->resp, 32);
138 TOH(ptr->param, 32);
139 }
140
141 void
aac_getbusinf_toh(struct aac_getbusinf * ptr)142 aac_getbusinf_toh(struct aac_getbusinf *ptr)
143 {
144 TOH(ptr->ProbeComplete, 32);
145 TOH(ptr->BusCount, 32);
146 TOH(ptr->TargetsPerBus, 32);
147 }
148
149 void
aac_vmi_businf_resp_toh(struct aac_vmi_businf_resp * ptr)150 aac_vmi_businf_resp_toh(struct aac_vmi_businf_resp *ptr)
151 {
152 TOH(ptr->Status, 32);
153 TOH(ptr->ObjType, 32);
154 TOH(ptr->MethId, 32);
155 TOH(ptr->ObjId, 32);
156 TOH(ptr->IoctlCmd, 32);
157 aac_getbusinf_toh(&ptr->BusInf);
158 }
159
160 void
aac_srb_response_toh(struct aac_srb_response * ptr)161 aac_srb_response_toh(struct aac_srb_response *ptr)
162 {
163 TOH(ptr->fib_status, 32);
164 TOH(ptr->srb_status, 32);
165 TOH(ptr->scsi_status, 32);
166 TOH(ptr->data_len, 32);
167 TOH(ptr->sense_len, 32);
168 }
169
170 /* Convert from host order to Little-Endian (TOLE) */
171
172 void
aac_adapter_init_tole(struct aac_adapter_init * ptr)173 aac_adapter_init_tole(struct aac_adapter_init *ptr)
174 {
175 TOLE(ptr->InitStructRevision, 32);
176 TOLE(ptr->NoOfMSIXVectors, 32);
177 TOLE(ptr->FilesystemRevision, 32);
178 TOLE(ptr->CommHeaderAddress, 32);
179 TOLE(ptr->FastIoCommAreaAddress, 32);
180 TOLE(ptr->AdapterFibsPhysicalAddress, 32);
181 TOLE(ptr->AdapterFibsVirtualAddress, 32);
182 TOLE(ptr->AdapterFibsSize, 32);
183 TOLE(ptr->AdapterFibAlign, 32);
184 TOLE(ptr->PrintfBufferAddress, 32);
185 TOLE(ptr->PrintfBufferSize, 32);
186 TOLE(ptr->HostPhysMemPages, 32);
187 TOLE(ptr->HostElapsedSeconds, 32);
188 TOLE(ptr->InitFlags, 32);
189 TOLE(ptr->MaxIoCommands, 32);
190 TOLE(ptr->MaxIoSize, 32);
191 TOLE(ptr->MaxFibSize, 32);
192 TOLE(ptr->MaxNumAif, 32);
193 TOLE(ptr->HostRRQ_AddrLow, 32);
194 TOLE(ptr->HostRRQ_AddrHigh, 32);
195 }
196
197 void
aac_fib_header_tole(struct aac_fib_header * ptr)198 aac_fib_header_tole(struct aac_fib_header *ptr)
199 {
200 TOLE(ptr->XferState, 32);
201 TOLE(ptr->Command, 16);
202 TOLE(ptr->Size, 16);
203 TOLE(ptr->SenderSize, 16);
204 TOLE(ptr->SenderFibAddress, 32);
205 TOLE(ptr->u.ReceiverFibAddress, 32);
206 TOLE(ptr->Handle, 32);
207 TOLE(ptr->Previous, 32);
208 TOLE(ptr->Next, 32);
209 }
210
211 void
aac_mntinfo_tole(struct aac_mntinfo * ptr)212 aac_mntinfo_tole(struct aac_mntinfo *ptr)
213 {
214 TOLE(ptr->Command, 32);
215 TOLE(ptr->MntType, 32);
216 TOLE(ptr->MntCount, 32);
217 }
218
219 void
aac_fsa_ctm_tole(struct aac_fsa_ctm * ptr)220 aac_fsa_ctm_tole(struct aac_fsa_ctm *ptr)
221 {
222 int i;
223
224 TOLE(ptr->command, 32);
225 for (i = 0; i < CT_FIB_PARAMS; i++)
226 TOLE(ptr->param[i], 32);
227 }
228
229 void
aac_cnt_config_tole(struct aac_cnt_config * ptr)230 aac_cnt_config_tole(struct aac_cnt_config *ptr)
231 {
232 TOLE(ptr->Command, 32);
233 aac_fsa_ctm_tole(&ptr->CTCommand);
234 }
235
236 void
aac_raw_io_tole(struct aac_raw_io * ptr)237 aac_raw_io_tole(struct aac_raw_io *ptr)
238 {
239 TOLE(ptr->BlockNumber, 64);
240 TOLE(ptr->ByteCount, 32);
241 TOLE(ptr->ContainerId, 16);
242 TOLE(ptr->Flags, 16);
243 TOLE(ptr->BpTotal, 16);
244 TOLE(ptr->BpComplete, 16);
245 }
246
247 void
aac_raw_io2_tole(struct aac_raw_io2 * ptr)248 aac_raw_io2_tole(struct aac_raw_io2 *ptr)
249 {
250 TOLE(ptr->strtBlkLow, 32);
251 TOLE(ptr->strtBlkHigh, 32);
252 TOLE(ptr->byteCnt, 32);
253 TOLE(ptr->ldNum, 16);
254 TOLE(ptr->flags, 16);
255 TOLE(ptr->sgeFirstSize, 32);
256 TOLE(ptr->sgeNominalSize, 32);
257 }
258
259 void
aac_fib_xporthdr_tole(struct aac_fib_xporthdr * ptr)260 aac_fib_xporthdr_tole(struct aac_fib_xporthdr *ptr)
261 {
262 TOLE(ptr->HostAddress, 64);
263 TOLE(ptr->Size, 32);
264 TOLE(ptr->Handle, 32);
265 }
266
267 void
aac_ctcfg_tole(struct aac_ctcfg * ptr)268 aac_ctcfg_tole(struct aac_ctcfg *ptr)
269 {
270 TOLE(ptr->Command, 32);
271 TOLE(ptr->cmd, 32);
272 TOLE(ptr->param, 32);
273 }
274
275 void
aac_vmioctl_tole(struct aac_vmioctl * ptr)276 aac_vmioctl_tole(struct aac_vmioctl *ptr)
277 {
278 TOLE(ptr->Command, 32);
279 TOLE(ptr->ObjType, 32);
280 TOLE(ptr->MethId, 32);
281 TOLE(ptr->ObjId, 32);
282 TOLE(ptr->IoctlCmd, 32);
283 TOLE(ptr->IoctlBuf[0], 32);
284 }
285
286 void
aac_pause_command_tole(struct aac_pause_command * ptr)287 aac_pause_command_tole(struct aac_pause_command *ptr)
288 {
289 TOLE(ptr->Command, 32);
290 TOLE(ptr->Type, 32);
291 TOLE(ptr->Timeout, 32);
292 TOLE(ptr->Min, 32);
293 TOLE(ptr->NoRescan, 32);
294 TOLE(ptr->Parm3, 32);
295 TOLE(ptr->Parm4, 32);
296 TOLE(ptr->Count, 32);
297 }
298
299 void
aac_srb_tole(struct aac_srb * ptr)300 aac_srb_tole(struct aac_srb *ptr)
301 {
302 TOLE(ptr->function, 32);
303 TOLE(ptr->bus, 32);
304 TOLE(ptr->target, 32);
305 TOLE(ptr->lun, 32);
306 TOLE(ptr->timeout, 32);
307 TOLE(ptr->flags, 32);
308 TOLE(ptr->data_len, 32);
309 TOLE(ptr->retry_limit, 32);
310 TOLE(ptr->cdb_len, 32);
311 }
312
313 void
aac_sge_ieee1212_tole(struct aac_sge_ieee1212 * ptr)314 aac_sge_ieee1212_tole(struct aac_sge_ieee1212 *ptr)
315 {
316 TOLE(ptr->addrLow, 32);
317 TOLE(ptr->addrHigh, 32);
318 TOLE(ptr->length, 32);
319 TOLE(ptr->flags, 32);
320 }
321
322 void
aac_sg_entryraw_tole(struct aac_sg_entryraw * ptr)323 aac_sg_entryraw_tole(struct aac_sg_entryraw *ptr)
324 {
325 TOLE(ptr->Next, 32);
326 TOLE(ptr->Prev, 32);
327 TOLE(ptr->SgAddress, 64);
328 TOLE(ptr->SgByteCount, 32);
329 TOLE(ptr->Flags, 32);
330 }
331
332 void
aac_sg_entry_tole(struct aac_sg_entry * ptr)333 aac_sg_entry_tole(struct aac_sg_entry *ptr)
334 {
335 TOLE(ptr->SgAddress, 32);
336 TOLE(ptr->SgByteCount, 32);
337 }
338
339 void
aac_sg_entry64_tole(struct aac_sg_entry64 * ptr)340 aac_sg_entry64_tole(struct aac_sg_entry64 *ptr)
341 {
342 TOLE(ptr->SgAddress, 64);
343 TOLE(ptr->SgByteCount, 32);
344 }
345
346 void
aac_blockread_tole(struct aac_blockread * ptr)347 aac_blockread_tole(struct aac_blockread *ptr)
348 {
349 TOLE(ptr->Command, 32);
350 TOLE(ptr->ContainerId, 32);
351 TOLE(ptr->BlockNumber, 32);
352 TOLE(ptr->ByteCount, 32);
353 }
354
355 void
aac_blockwrite_tole(struct aac_blockwrite * ptr)356 aac_blockwrite_tole(struct aac_blockwrite *ptr)
357 {
358 TOLE(ptr->Command, 32);
359 TOLE(ptr->ContainerId, 32);
360 TOLE(ptr->BlockNumber, 32);
361 TOLE(ptr->ByteCount, 32);
362 TOLE(ptr->Stable, 32);
363 }
364
365 void
aac_blockread64_tole(struct aac_blockread64 * ptr)366 aac_blockread64_tole(struct aac_blockread64 *ptr)
367 {
368 TOLE(ptr->Command, 32);
369 TOLE(ptr->ContainerId, 16);
370 TOLE(ptr->SectorCount, 16);
371 TOLE(ptr->BlockNumber, 32);
372 TOLE(ptr->Pad, 16);
373 TOLE(ptr->Flags, 16);
374 }
375
376 void
aac_blockwrite64_tole(struct aac_blockwrite64 * ptr)377 aac_blockwrite64_tole(struct aac_blockwrite64 *ptr)
378 {
379 TOLE(ptr->Command, 32);
380 TOLE(ptr->ContainerId, 16);
381 TOLE(ptr->SectorCount, 16);
382 TOLE(ptr->BlockNumber, 32);
383 TOLE(ptr->Pad, 16);
384 TOLE(ptr->Flags, 16);
385 }
386
387 #endif
388