xref: /freebsd/sys/cam/ata/ata_all.c (revision a76f78dc3f43dbc25021f17255fee87cc0eafc73)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3  *
4  * Copyright (c) 2009 Alexander Motin <mav@FreeBSD.org>
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer,
12  *    without modification, immediately at the beginning of the file.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27  */
28 
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
31 
32 #include <sys/param.h>
33 
34 #ifdef _KERNEL
35 #include "opt_scsi.h"
36 
37 #include <sys/systm.h>
38 #include <sys/libkern.h>
39 #include <sys/kernel.h>
40 #include <sys/sysctl.h>
41 #else
42 #include <errno.h>
43 #include <stdio.h>
44 #include <stdlib.h>
45 #include <string.h>
46 #ifndef min
47 #define min(a,b) (((a)<(b))?(a):(b))
48 #endif
49 #endif
50 
51 #include <cam/cam.h>
52 #include <cam/cam_ccb.h>
53 #include <cam/cam_queue.h>
54 #include <cam/cam_xpt.h>
55 #include <sys/ata.h>
56 #include <cam/ata/ata_all.h>
57 #include <sys/sbuf.h>
58 #include <sys/endian.h>
59 
60 int
61 ata_version(int ver)
62 {
63 	int bit;
64 
65 	if (ver == 0xffff)
66 		return 0;
67 	for (bit = 15; bit >= 0; bit--)
68 		if (ver & (1<<bit))
69 			return bit;
70 	return 0;
71 }
72 
73 char *
74 ata_op_string(struct ata_cmd *cmd)
75 {
76 
77 	if (cmd->control & 0x04)
78 		return ("SOFT_RESET");
79 	switch (cmd->command) {
80 	case 0x00:
81 		switch (cmd->features) {
82 		case 0x00: return ("NOP FLUSHQUEUE");
83 		case 0x01: return ("NOP AUTOPOLL");
84 		}
85 		return ("NOP");
86 	case 0x03: return ("CFA_REQUEST_EXTENDED_ERROR");
87 	case 0x06:
88 		switch (cmd->features) {
89 		case 0x01: return ("DSM TRIM");
90 		}
91 		return "DSM";
92 	case 0x07:
93 		switch (cmd->features) {
94 		case 0x01: return ("DSM_XL TRIM");
95 		}
96 		return "DSM_XL";
97 	case 0x08: return ("DEVICE_RESET");
98 	case 0x0b: return ("REQUEST_SENSE_DATA_EXT");
99 	case 0x12: return ("GET_PHYSICAL_ELEMENT_STATUS");
100 	case 0x20: return ("READ");
101 	case 0x24: return ("READ48");
102 	case 0x25: return ("READ_DMA48");
103 	case 0x26: return ("READ_DMA_QUEUED48");
104 	case 0x27: return ("READ_NATIVE_MAX_ADDRESS48");
105 	case 0x29: return ("READ_MUL48");
106 	case 0x2a: return ("READ_STREAM_DMA48");
107 	case 0x2b: return ("READ_STREAM48");
108 	case 0x2f: return ("READ_LOG_EXT");
109 	case 0x30: return ("WRITE");
110 	case 0x34: return ("WRITE48");
111 	case 0x35: return ("WRITE_DMA48");
112 	case 0x36: return ("WRITE_DMA_QUEUED48");
113 	case 0x37: return ("SET_MAX_ADDRESS48");
114 	case 0x39: return ("WRITE_MUL48");
115 	case 0x3a: return ("WRITE_STREAM_DMA48");
116 	case 0x3b: return ("WRITE_STREAM48");
117 	case 0x3d: return ("WRITE_DMA_FUA48");
118 	case 0x3e: return ("WRITE_DMA_QUEUED_FUA48");
119 	case 0x3f: return ("WRITE_LOG_EXT");
120 	case 0x40: return ("READ_VERIFY");
121 	case 0x42: return ("READ_VERIFY48");
122 	case 0x44:
123 		switch (cmd->features) {
124 		case 0x01: return ("ZERO_EXT TRIM");
125 		}
126 		return "ZERO_EXT";
127 	case 0x45:
128 		switch (cmd->features) {
129 		case 0x55: return ("WRITE_UNCORRECTABLE48 PSEUDO");
130 		case 0xaa: return ("WRITE_UNCORRECTABLE48 FLAGGED");
131 		}
132 		return "WRITE_UNCORRECTABLE48";
133 	case 0x47: return ("READ_LOG_DMA_EXT");
134 	case 0x4a: return ("ZAC_MANAGEMENT_IN");
135 	case 0x51: return ("CONFIGURE_STREAM");
136 	case 0x57: return ("WRITE_LOG_DMA_EXT");
137 	case 0x5b: return ("TRUSTED_NON_DATA");
138 	case 0x5c: return ("TRUSTED_RECEIVE");
139 	case 0x5d: return ("TRUSTED_RECEIVE_DMA");
140 	case 0x5e: return ("TRUSTED_SEND");
141 	case 0x5f: return ("TRUSTED_SEND_DMA");
142 	case 0x60: return ("READ_FPDMA_QUEUED");
143 	case 0x61: return ("WRITE_FPDMA_QUEUED");
144 	case 0x63:
145 		switch (cmd->features & 0xf) {
146 		case 0x00: return ("NCQ_NON_DATA ABORT NCQ QUEUE");
147 		case 0x01: return ("NCQ_NON_DATA DEADLINE HANDLING");
148 		case 0x02: return ("NCQ_NON_DATA HYBRID DEMOTE BY SIZE");
149 		case 0x03: return ("NCQ_NON_DATA HYBRID CHANGE BY LBA RANGE");
150 		case 0x04: return ("NCQ_NON_DATA HYBRID CONTROL");
151 		case 0x05: return ("NCQ_NON_DATA SET FEATURES");
152 		/*
153 		 * XXX KDM need common decoding between NCQ and non-NCQ
154 		 * versions of SET FEATURES.
155 		 */
156 		case 0x06: return ("NCQ_NON_DATA ZERO EXT");
157 		case 0x07: return ("NCQ_NON_DATA ZAC MANAGEMENT OUT");
158 		}
159 		return ("NCQ_NON_DATA");
160 	case 0x64:
161 		switch (cmd->sector_count_exp & 0xf) {
162 		case 0x00: return ("SEND_FPDMA_QUEUED DATA SET MANAGEMENT");
163 		case 0x01: return ("SEND_FPDMA_QUEUED HYBRID EVICT");
164 		case 0x02: return ("SEND_FPDMA_QUEUED WRITE LOG DMA EXT");
165 		case 0x03: return ("SEND_FPDMA_QUEUED ZAC MANAGEMENT OUT");
166 		case 0x04: return ("SEND_FPDMA_QUEUED DATA SET MANAGEMENT XL");
167 		}
168 		return ("SEND_FPDMA_QUEUED");
169 	case 0x65:
170 		switch (cmd->sector_count_exp & 0xf) {
171 		case 0x01: return ("RECEIVE_FPDMA_QUEUED READ LOG DMA EXT");
172 		case 0x02: return ("RECEIVE_FPDMA_QUEUED ZAC MANAGEMENT IN");
173 		}
174 		return ("RECEIVE_FPDMA_QUEUED");
175 	case 0x67:
176 		if (cmd->features == 0xec)
177 			return ("SEP_ATTN IDENTIFY");
178 		switch (cmd->lba_low) {
179 		case 0x00: return ("SEP_ATTN READ BUFFER");
180 		case 0x02: return ("SEP_ATTN RECEIVE DIAGNOSTIC RESULTS");
181 		case 0x80: return ("SEP_ATTN WRITE BUFFER");
182 		case 0x82: return ("SEP_ATTN SEND DIAGNOSTIC");
183 		}
184 		return ("SEP_ATTN");
185 	case 0x70: return ("SEEK");
186 	case 0x77: return ("SET_DATE_TIME_EXT");
187 	case 0x78:
188 		switch (cmd->features) {
189 		case 0x00: return ("GET_NATIVE_MAX_ADDRESS_EXT");
190 		case 0x01: return ("SET_ACCESSIBLE_MAX_ADDRESS_EXT");
191 		case 0x02: return ("FREEZE_ACCESSIBLE_MAX_ADDRESS_EXT");
192 		}
193 		return ("ACCESSIBLE_MAX_ADDRESS_CONFIGURATION");
194 	case 0x7C: return ("REMOVE_ELEMENT_AND_TRUNCATE");
195 	case 0x87: return ("CFA_TRANSLATE_SECTOR");
196 	case 0x90: return ("EXECUTE_DEVICE_DIAGNOSTIC");
197 	case 0x92: return ("DOWNLOAD_MICROCODE");
198 	case 0x93: return ("DOWNLOAD_MICROCODE_DMA");
199 	case 0x9a: return ("ZAC_MANAGEMENT_OUT");
200 	case 0xa0: return ("PACKET");
201 	case 0xa1: return ("ATAPI_IDENTIFY");
202 	case 0xa2: return ("SERVICE");
203 	case 0xb0:
204 		switch(cmd->features) {
205 		case 0xd0: return ("SMART READ ATTR VALUES");
206 		case 0xd1: return ("SMART READ ATTR THRESHOLDS");
207 		case 0xd3: return ("SMART SAVE ATTR VALUES");
208 		case 0xd4: return ("SMART EXECUTE OFFLINE IMMEDIATE");
209 		case 0xd5: return ("SMART READ LOG");
210 		case 0xd6: return ("SMART WRITE LOG");
211 		case 0xd8: return ("SMART ENABLE OPERATION");
212 		case 0xd9: return ("SMART DISABLE OPERATION");
213 		case 0xda: return ("SMART RETURN STATUS");
214 		}
215 		return ("SMART");
216 	case 0xb1: return ("DEVICE CONFIGURATION");
217 	case 0xb2: return ("SET_SECTOR_CONFIGURATION_EXT");
218 	case 0xb4: return ("SANITIZE_DEVICE");
219 	case 0xc0: return ("CFA_ERASE");
220 	case 0xc4: return ("READ_MUL");
221 	case 0xc5: return ("WRITE_MUL");
222 	case 0xc6: return ("SET_MULTI");
223 	case 0xc7: return ("READ_DMA_QUEUED");
224 	case 0xc8: return ("READ_DMA");
225 	case 0xca: return ("WRITE_DMA");
226 	case 0xcc: return ("WRITE_DMA_QUEUED");
227 	case 0xcd: return ("CFA_WRITE_MULTIPLE_WITHOUT_ERASE");
228 	case 0xce: return ("WRITE_MUL_FUA48");
229 	case 0xd1: return ("CHECK_MEDIA_CARD_TYPE");
230 	case 0xda: return ("GET_MEDIA_STATUS");
231 	case 0xde: return ("MEDIA_LOCK");
232 	case 0xdf: return ("MEDIA_UNLOCK");
233 	case 0xe0: return ("STANDBY_IMMEDIATE");
234 	case 0xe1: return ("IDLE_IMMEDIATE");
235 	case 0xe2: return ("STANDBY");
236 	case 0xe3: return ("IDLE");
237 	case 0xe4: return ("READ_BUFFER/PM");
238 	case 0xe5: return ("CHECK_POWER_MODE");
239 	case 0xe6: return ("SLEEP");
240 	case 0xe7: return ("FLUSHCACHE");
241 	case 0xe8: return ("WRITE_BUFFER/PM");
242 	case 0xe9: return ("READ_BUFFER_DMA");
243 	case 0xea: return ("FLUSHCACHE48");
244 	case 0xeb: return ("WRITE_BUFFER_DMA");
245 	case 0xec: return ("ATA_IDENTIFY");
246 	case 0xed: return ("MEDIA_EJECT");
247 	case 0xef:
248 		/*
249 		 * XXX KDM need common decoding between NCQ and non-NCQ
250 		 * versions of SET FEATURES.
251 		 */
252 		switch (cmd->features) {
253 	        case 0x02: return ("SETFEATURES ENABLE WCACHE");
254 	        case 0x03: return ("SETFEATURES SET TRANSFER MODE");
255 		case 0x05: return ("SETFEATURES ENABLE APM");
256 	        case 0x06: return ("SETFEATURES ENABLE PUIS");
257 	        case 0x07: return ("SETFEATURES SPIN-UP");
258 		case 0x0b: return ("SETFEATURES ENABLE WRITE READ VERIFY");
259 		case 0x0c: return ("SETFEATURES ENABLE DEVICE LIFE CONTROL");
260 	        case 0x10: return ("SETFEATURES ENABLE SATA FEATURE");
261 		case 0x41: return ("SETFEATURES ENABLE FREEFALL CONTROL");
262 		case 0x43: return ("SETFEATURES SET MAX HOST INT SECT TIMES");
263 		case 0x45: return ("SETFEATURES SET RATE BASIS");
264 		case 0x4a: return ("SETFEATURES EXTENDED POWER CONDITIONS");
265 		case 0x50: return ("SETFEATURES ADVANCED BACKGROUD OPERATION");
266 	        case 0x55: return ("SETFEATURES DISABLE RCACHE");
267 		case 0x5d: return ("SETFEATURES ENABLE RELIRQ");
268 		case 0x5e: return ("SETFEATURES ENABLE SRVIRQ");
269 		case 0x62: return ("SETFEATURES LONG PHYS SECT ALIGN ERC");
270 		case 0x63: return ("SETFEATURES DSN");
271 		case 0x66: return ("SETFEATURES DISABLE DEFAULTS");
272 	        case 0x82: return ("SETFEATURES DISABLE WCACHE");
273 	        case 0x85: return ("SETFEATURES DISABLE APM");
274 	        case 0x86: return ("SETFEATURES DISABLE PUIS");
275 		case 0x8b: return ("SETFEATURES DISABLE WRITE READ VERIFY");
276 		case 0x8c: return ("SETFEATURES DISABLE DEVICE LIFE CONTROL");
277 	        case 0x90: return ("SETFEATURES DISABLE SATA FEATURE");
278 	        case 0xaa: return ("SETFEATURES ENABLE RCACHE");
279 		case 0xC1: return ("SETFEATURES DISABLE FREEFALL CONTROL");
280 		case 0xC3: return ("SETFEATURES SENSE DATA REPORTING");
281 		case 0xC4: return ("SETFEATURES NCQ SENSE DATA RETURN");
282 		case 0xCC: return ("SETFEATURES ENABLE DEFAULTS");
283 		case 0xdd: return ("SETFEATURES DISABLE RELIRQ");
284 		case 0xde: return ("SETFEATURES DISABLE SRVIRQ");
285 	        }
286 	        return "SETFEATURES";
287 	case 0xf1: return ("SECURITY_SET_PASSWORD");
288 	case 0xf2: return ("SECURITY_UNLOCK");
289 	case 0xf3: return ("SECURITY_ERASE_PREPARE");
290 	case 0xf4: return ("SECURITY_ERASE_UNIT");
291 	case 0xf5: return ("SECURITY_FREEZE_LOCK");
292 	case 0xf6: return ("SECURITY_DISABLE_PASSWORD");
293 	case 0xf8: return ("READ_NATIVE_MAX_ADDRESS");
294 	case 0xf9: return ("SET_MAX_ADDRESS");
295 	}
296 	return "UNKNOWN";
297 }
298 
299 char *
300 ata_cmd_string(struct ata_cmd *cmd, char *cmd_string, size_t len)
301 {
302 	struct sbuf sb;
303 	int error;
304 
305 	if (len == 0)
306 		return ("");
307 
308 	sbuf_new(&sb, cmd_string, len, SBUF_FIXEDLEN);
309 	ata_cmd_sbuf(cmd, &sb);
310 
311 	error = sbuf_finish(&sb);
312 	if (error != 0 && error != ENOMEM)
313 		return ("");
314 
315 	return(sbuf_data(&sb));
316 }
317 
318 void
319 ata_cmd_sbuf(struct ata_cmd *cmd, struct sbuf *sb)
320 {
321 	sbuf_printf(sb, "%02x %02x %02x %02x "
322 	    "%02x %02x %02x %02x %02x %02x %02x %02x",
323 	    cmd->command, cmd->features,
324 	    cmd->lba_low, cmd->lba_mid, cmd->lba_high, cmd->device,
325 	    cmd->lba_low_exp, cmd->lba_mid_exp, cmd->lba_high_exp,
326 	    cmd->features_exp, cmd->sector_count, cmd->sector_count_exp);
327 }
328 
329 char *
330 ata_res_string(struct ata_res *res, char *res_string, size_t len)
331 {
332 	struct sbuf sb;
333 	int error;
334 
335 	if (len == 0)
336 		return ("");
337 
338 	sbuf_new(&sb, res_string, len, SBUF_FIXEDLEN);
339 	ata_res_sbuf(res, &sb);
340 
341 	error = sbuf_finish(&sb);
342 	if (error != 0 && error != ENOMEM)
343 		return ("");
344 
345 	return(sbuf_data(&sb));
346 }
347 
348 int
349 ata_res_sbuf(struct ata_res *res, struct sbuf *sb)
350 {
351 
352 	sbuf_printf(sb, "%02x %02x %02x %02x "
353 	    "%02x %02x %02x %02x %02x %02x %02x",
354 	    res->status, res->error,
355 	    res->lba_low, res->lba_mid, res->lba_high, res->device,
356 	    res->lba_low_exp, res->lba_mid_exp, res->lba_high_exp,
357 	    res->sector_count, res->sector_count_exp);
358 
359 	return (0);
360 }
361 
362 /*
363  * ata_command_sbuf() returns 0 for success and -1 for failure.
364  */
365 int
366 ata_command_sbuf(struct ccb_ataio *ataio, struct sbuf *sb)
367 {
368 
369 	sbuf_printf(sb, "%s. ACB: ",
370 	    ata_op_string(&ataio->cmd));
371 	ata_cmd_sbuf(&ataio->cmd, sb);
372 
373 	return(0);
374 }
375 
376 /*
377  * ata_status_abuf() returns 0 for success and -1 for failure.
378  */
379 int
380 ata_status_sbuf(struct ccb_ataio *ataio, struct sbuf *sb)
381 {
382 
383 	sbuf_printf(sb, "ATA status: %02x (%s%s%s%s%s%s%s%s)",
384 	    ataio->res.status,
385 	    (ataio->res.status & 0x80) ? "BSY " : "",
386 	    (ataio->res.status & 0x40) ? "DRDY " : "",
387 	    (ataio->res.status & 0x20) ? "DF " : "",
388 	    (ataio->res.status & 0x10) ? "SERV " : "",
389 	    (ataio->res.status & 0x08) ? "DRQ " : "",
390 	    (ataio->res.status & 0x04) ? "CORR " : "",
391 	    (ataio->res.status & 0x02) ? "IDX " : "",
392 	    (ataio->res.status & 0x01) ? "ERR" : "");
393 	if (ataio->res.status & 1) {
394 	    sbuf_printf(sb, ", error: %02x (%s%s%s%s%s%s%s%s)",
395 		ataio->res.error,
396 		(ataio->res.error & 0x80) ? "ICRC " : "",
397 		(ataio->res.error & 0x40) ? "UNC " : "",
398 		(ataio->res.error & 0x20) ? "MC " : "",
399 		(ataio->res.error & 0x10) ? "IDNF " : "",
400 		(ataio->res.error & 0x08) ? "MCR " : "",
401 		(ataio->res.error & 0x04) ? "ABRT " : "",
402 		(ataio->res.error & 0x02) ? "NM " : "",
403 		(ataio->res.error & 0x01) ? "ILI" : "");
404 	}
405 
406 	return(0);
407 }
408 
409 void
410 ata_print_ident(struct ata_params *ident_data)
411 {
412 	const char *proto;
413 	char ata[12], sata[12];
414 
415 	ata_print_ident_short(ident_data);
416 
417 	proto = (ident_data->config == ATA_PROTO_CFA) ? "CFA" :
418 		(ident_data->config & ATA_PROTO_ATAPI) ? "ATAPI" : "ATA";
419 	if (ata_version(ident_data->version_major) == 0) {
420 		snprintf(ata, sizeof(ata), "%s", proto);
421 	} else if (ata_version(ident_data->version_major) <= 7) {
422 		snprintf(ata, sizeof(ata), "%s-%d", proto,
423 		    ata_version(ident_data->version_major));
424 	} else if (ata_version(ident_data->version_major) == 8) {
425 		snprintf(ata, sizeof(ata), "%s8-ACS", proto);
426 	} else {
427 		snprintf(ata, sizeof(ata), "ACS-%d %s",
428 		    ata_version(ident_data->version_major) - 7, proto);
429 	}
430 	if (ident_data->satacapabilities && ident_data->satacapabilities != 0xffff) {
431 		if (ident_data->satacapabilities & ATA_SATA_GEN3)
432 			snprintf(sata, sizeof(sata), " SATA 3.x");
433 		else if (ident_data->satacapabilities & ATA_SATA_GEN2)
434 			snprintf(sata, sizeof(sata), " SATA 2.x");
435 		else if (ident_data->satacapabilities & ATA_SATA_GEN1)
436 			snprintf(sata, sizeof(sata), " SATA 1.x");
437 		else
438 			snprintf(sata, sizeof(sata), " SATA");
439 	} else
440 		sata[0] = 0;
441 	printf(" %s%s device\n", ata, sata);
442 }
443 
444 void
445 ata_print_ident_sbuf(struct ata_params *ident_data, struct sbuf *sb)
446 {
447 	const char *proto, *sata;
448 	int version;
449 
450 	ata_print_ident_short_sbuf(ident_data, sb);
451 	sbuf_printf(sb, " ");
452 
453 	proto = (ident_data->config == ATA_PROTO_CFA) ? "CFA" :
454 		(ident_data->config & ATA_PROTO_ATAPI) ? "ATAPI" : "ATA";
455 	version = ata_version(ident_data->version_major);
456 
457 	switch (version) {
458 	case 0:
459 		sbuf_printf(sb, "%s", proto);
460 		break;
461 	case 1:
462 	case 2:
463 	case 3:
464 	case 4:
465 	case 5:
466 	case 6:
467 	case 7:
468 		sbuf_printf(sb, "%s-%d", proto, version);
469 		break;
470 	case 8:
471 		sbuf_printf(sb, "%s8-ACS", proto);
472 		break;
473 	default:
474 		sbuf_printf(sb, "ACS-%d %s", version - 7, proto);
475 		break;
476 	}
477 
478 	if (ident_data->satacapabilities && ident_data->satacapabilities != 0xffff) {
479 		if (ident_data->satacapabilities & ATA_SATA_GEN3)
480 			sata = " SATA 3.x";
481 		else if (ident_data->satacapabilities & ATA_SATA_GEN2)
482 			sata = " SATA 2.x";
483 		else if (ident_data->satacapabilities & ATA_SATA_GEN1)
484 			sata = " SATA 1.x";
485 		else
486 			sata = " SATA";
487 	} else
488 		sata = "";
489 	sbuf_printf(sb, "%s device\n", sata);
490 }
491 
492 void
493 ata_print_ident_short(struct ata_params *ident_data)
494 {
495 	char product[48], revision[16];
496 
497 	cam_strvis(product, ident_data->model, sizeof(ident_data->model),
498 		   sizeof(product));
499 	cam_strvis(revision, ident_data->revision, sizeof(ident_data->revision),
500 		   sizeof(revision));
501 	printf("<%s %s>", product, revision);
502 }
503 
504 void
505 ata_print_ident_short_sbuf(struct ata_params *ident_data, struct sbuf *sb)
506 {
507 
508 	sbuf_printf(sb, "<");
509 	cam_strvis_sbuf(sb, ident_data->model, sizeof(ident_data->model), 0);
510 	sbuf_printf(sb, " ");
511 	cam_strvis_sbuf(sb, ident_data->revision, sizeof(ident_data->revision), 0);
512 	sbuf_printf(sb, ">");
513 }
514 
515 void
516 semb_print_ident(struct sep_identify_data *ident_data)
517 {
518 	char in[7], ins[5];
519 
520 	semb_print_ident_short(ident_data);
521 	cam_strvis(in, ident_data->interface_id, 6, sizeof(in));
522 	cam_strvis(ins, ident_data->interface_rev, 4, sizeof(ins));
523 	printf(" SEMB %s %s device\n", in, ins);
524 }
525 
526 void
527 semb_print_ident_sbuf(struct sep_identify_data *ident_data, struct sbuf *sb)
528 {
529 
530 	semb_print_ident_short_sbuf(ident_data, sb);
531 
532 	sbuf_printf(sb, " SEMB ");
533 	cam_strvis_sbuf(sb, ident_data->interface_id, 6, 0);
534 	sbuf_printf(sb, " ");
535 	cam_strvis_sbuf(sb, ident_data->interface_rev, 4, 0);
536 	sbuf_printf(sb, " device\n");
537 }
538 
539 void
540 semb_print_ident_short(struct sep_identify_data *ident_data)
541 {
542 	char vendor[9], product[17], revision[5], fw[5];
543 
544 	cam_strvis(vendor, ident_data->vendor_id, 8, sizeof(vendor));
545 	cam_strvis(product, ident_data->product_id, 16, sizeof(product));
546 	cam_strvis(revision, ident_data->product_rev, 4, sizeof(revision));
547 	cam_strvis(fw, ident_data->firmware_rev, 4, sizeof(fw));
548 	printf("<%s %s %s %s>", vendor, product, revision, fw);
549 }
550 
551 void
552 semb_print_ident_short_sbuf(struct sep_identify_data *ident_data, struct sbuf *sb)
553 {
554 
555 	sbuf_printf(sb, "<");
556 	cam_strvis_sbuf(sb, ident_data->vendor_id, 8, 0);
557 	sbuf_printf(sb, " ");
558 	cam_strvis_sbuf(sb, ident_data->product_id, 16, 0);
559 	sbuf_printf(sb, " ");
560 	cam_strvis_sbuf(sb, ident_data->product_rev, 4, 0);
561 	sbuf_printf(sb, " ");
562 	cam_strvis_sbuf(sb, ident_data->firmware_rev, 4, 0);
563 	sbuf_printf(sb, ">");
564 }
565 
566 uint32_t
567 ata_logical_sector_size(struct ata_params *ident_data)
568 {
569 	if ((ident_data->pss & ATA_PSS_VALID_MASK) == ATA_PSS_VALID_VALUE &&
570 	    (ident_data->pss & ATA_PSS_LSSABOVE512)) {
571 		return (((u_int32_t)ident_data->lss_1 |
572 		    ((u_int32_t)ident_data->lss_2 << 16)) * 2);
573 	}
574 	return (512);
575 }
576 
577 uint64_t
578 ata_physical_sector_size(struct ata_params *ident_data)
579 {
580 	if ((ident_data->pss & ATA_PSS_VALID_MASK) == ATA_PSS_VALID_VALUE) {
581 		if (ident_data->pss & ATA_PSS_MULTLS) {
582 			return ((uint64_t)ata_logical_sector_size(ident_data) *
583 			    (1 << (ident_data->pss & ATA_PSS_LSPPS)));
584 		} else {
585 			return (uint64_t)ata_logical_sector_size(ident_data);
586 		}
587 	}
588 	return (512);
589 }
590 
591 uint64_t
592 ata_logical_sector_offset(struct ata_params *ident_data)
593 {
594 	if ((ident_data->lsalign & 0xc000) == 0x4000) {
595 		return ((uint64_t)ata_logical_sector_size(ident_data) *
596 		    (ident_data->lsalign & 0x3fff));
597 	}
598 	return (0);
599 }
600 
601 void
602 ata_28bit_cmd(struct ccb_ataio *ataio, uint8_t cmd, uint8_t features,
603     uint32_t lba, uint8_t sector_count)
604 {
605 	bzero(&ataio->cmd, sizeof(ataio->cmd));
606 	ataio->cmd.flags = 0;
607 	if (cmd == ATA_READ_DMA ||
608 	    cmd == ATA_READ_DMA_QUEUED ||
609 	    cmd == ATA_WRITE_DMA ||
610 	    cmd == ATA_WRITE_DMA_QUEUED ||
611 	    cmd == ATA_TRUSTED_RECEIVE_DMA ||
612 	    cmd == ATA_TRUSTED_SEND_DMA ||
613 	    cmd == ATA_DOWNLOAD_MICROCODE_DMA ||
614 	    cmd == ATA_READ_BUFFER_DMA ||
615 	    cmd == ATA_WRITE_BUFFER_DMA)
616 		ataio->cmd.flags |= CAM_ATAIO_DMA;
617 	ataio->cmd.command = cmd;
618 	ataio->cmd.features = features;
619 	ataio->cmd.lba_low = lba;
620 	ataio->cmd.lba_mid = lba >> 8;
621 	ataio->cmd.lba_high = lba >> 16;
622 	ataio->cmd.device = ATA_DEV_LBA | ((lba >> 24) & 0x0f);
623 	ataio->cmd.sector_count = sector_count;
624 }
625 
626 void
627 ata_48bit_cmd(struct ccb_ataio *ataio, uint8_t cmd, uint16_t features,
628     uint64_t lba, uint16_t sector_count)
629 {
630 
631 	ataio->cmd.flags = CAM_ATAIO_48BIT;
632 	if (cmd == ATA_READ_DMA48 ||
633 	    cmd == ATA_READ_DMA_QUEUED48 ||
634 	    cmd == ATA_READ_STREAM_DMA48 ||
635 	    cmd == ATA_WRITE_DMA48 ||
636 	    cmd == ATA_WRITE_DMA_FUA48 ||
637 	    cmd == ATA_WRITE_DMA_QUEUED48 ||
638 	    cmd == ATA_WRITE_DMA_QUEUED_FUA48 ||
639 	    cmd == ATA_WRITE_STREAM_DMA48 ||
640 	    cmd == ATA_DATA_SET_MANAGEMENT ||
641 	    cmd == ATA_READ_LOG_DMA_EXT ||
642 	    cmd == ATA_WRITE_LOG_DMA_EXT)
643 		ataio->cmd.flags |= CAM_ATAIO_DMA;
644 	ataio->cmd.command = cmd;
645 	ataio->cmd.features = features;
646 	ataio->cmd.lba_low = lba;
647 	ataio->cmd.lba_mid = lba >> 8;
648 	ataio->cmd.lba_high = lba >> 16;
649 	ataio->cmd.device = ATA_DEV_LBA;
650 	ataio->cmd.lba_low_exp = lba >> 24;
651 	ataio->cmd.lba_mid_exp = lba >> 32;
652 	ataio->cmd.lba_high_exp = lba >> 40;
653 	ataio->cmd.features_exp = features >> 8;
654 	ataio->cmd.sector_count = sector_count;
655 	ataio->cmd.sector_count_exp = sector_count >> 8;
656 	ataio->cmd.control = 0;
657 }
658 
659 void
660 ata_ncq_cmd(struct ccb_ataio *ataio, uint8_t cmd,
661     uint64_t lba, uint16_t sector_count)
662 {
663 
664 	ataio->cmd.flags = CAM_ATAIO_48BIT | CAM_ATAIO_FPDMA;
665 	ataio->cmd.command = cmd;
666 	ataio->cmd.features = sector_count;
667 	ataio->cmd.lba_low = lba;
668 	ataio->cmd.lba_mid = lba >> 8;
669 	ataio->cmd.lba_high = lba >> 16;
670 	ataio->cmd.device = ATA_DEV_LBA;
671 	ataio->cmd.lba_low_exp = lba >> 24;
672 	ataio->cmd.lba_mid_exp = lba >> 32;
673 	ataio->cmd.lba_high_exp = lba >> 40;
674 	ataio->cmd.features_exp = sector_count >> 8;
675 	ataio->cmd.sector_count = 0;
676 	ataio->cmd.sector_count_exp = 0;
677 	ataio->cmd.control = 0;
678 }
679 
680 void
681 ata_reset_cmd(struct ccb_ataio *ataio)
682 {
683 	bzero(&ataio->cmd, sizeof(ataio->cmd));
684 	ataio->cmd.flags = CAM_ATAIO_CONTROL | CAM_ATAIO_NEEDRESULT;
685 	ataio->cmd.control = 0x04;
686 }
687 
688 void
689 ata_pm_read_cmd(struct ccb_ataio *ataio, int reg, int port)
690 {
691 	bzero(&ataio->cmd, sizeof(ataio->cmd));
692 	ataio->cmd.flags = CAM_ATAIO_NEEDRESULT;
693 	ataio->cmd.command = ATA_READ_PM;
694 	ataio->cmd.features = reg;
695 	ataio->cmd.device = port & 0x0f;
696 }
697 
698 void
699 ata_pm_write_cmd(struct ccb_ataio *ataio, int reg, int port, uint32_t val)
700 {
701 	bzero(&ataio->cmd, sizeof(ataio->cmd));
702 	ataio->cmd.flags = 0;
703 	ataio->cmd.command = ATA_WRITE_PM;
704 	ataio->cmd.features = reg;
705 	ataio->cmd.sector_count = val;
706 	ataio->cmd.lba_low = val >> 8;
707 	ataio->cmd.lba_mid = val >> 16;
708 	ataio->cmd.lba_high = val >> 24;
709 	ataio->cmd.device = port & 0x0f;
710 }
711 
712 void
713 ata_read_log(struct ccb_ataio *ataio, uint32_t retries,
714 	     void (*cbfcnp)(struct cam_periph *, union ccb *),
715 	     uint32_t log_address, uint32_t page_number, uint16_t block_count,
716 	     uint32_t protocol, uint8_t *data_ptr, uint32_t dxfer_len,
717 	     uint32_t timeout)
718 {
719 	uint64_t lba;
720 
721 	cam_fill_ataio(ataio,
722 	    /*retries*/ 1,
723 	    /*cbfcnp*/ cbfcnp,
724 	    /*flags*/ CAM_DIR_IN,
725 	    /*tag_action*/ 0,
726 	    /*data_ptr*/ data_ptr,
727 	    /*dxfer_len*/ dxfer_len,
728 	    /*timeout*/ timeout);
729 
730 	lba = (((uint64_t)page_number & 0xff00) << 32) |
731 	      ((page_number & 0x00ff) << 8) |
732 	      (log_address & 0xff);
733 
734 	ata_48bit_cmd(ataio,
735 	    /*cmd*/ (protocol & CAM_ATAIO_DMA) ? ATA_READ_LOG_DMA_EXT :
736 		     ATA_READ_LOG_EXT,
737 	    /*features*/ 0,
738 	    /*lba*/ lba,
739 	    /*sector_count*/ block_count);
740 }
741 
742 void
743 ata_bswap(int8_t *buf, int len)
744 {
745 	u_int16_t *ptr = (u_int16_t*)(buf + len);
746 
747 	while (--ptr >= (u_int16_t*)buf)
748 		*ptr = be16toh(*ptr);
749 }
750 
751 void
752 ata_btrim(int8_t *buf, int len)
753 {
754 	int8_t *ptr;
755 
756 	for (ptr = buf; ptr < buf+len; ++ptr)
757 		if (!*ptr || *ptr == '_')
758 			*ptr = ' ';
759 	for (ptr = buf + len - 1; ptr >= buf && *ptr == ' '; --ptr)
760 		*ptr = 0;
761 }
762 
763 void
764 ata_bpack(int8_t *src, int8_t *dst, int len)
765 {
766 	int i, j, blank;
767 
768 	for (i = j = blank = 0 ; i < len; i++) {
769 		if (blank && src[i] == ' ') continue;
770 		if (blank && src[i] != ' ') {
771 			dst[j++] = src[i];
772 			blank = 0;
773 			continue;
774 		}
775 		if (src[i] == ' ') {
776 			blank = 1;
777 			if (i == 0)
778 			continue;
779 		}
780 		dst[j++] = src[i];
781 	}
782 	while (j < len)
783 		dst[j++] = 0x00;
784 }
785 
786 int
787 ata_max_pmode(struct ata_params *ap)
788 {
789     if (ap->atavalid & ATA_FLAG_64_70) {
790 	if (ap->apiomodes & 0x02)
791 	    return ATA_PIO4;
792 	if (ap->apiomodes & 0x01)
793 	    return ATA_PIO3;
794     }
795     if (ap->mwdmamodes & 0x04)
796 	return ATA_PIO4;
797     if (ap->mwdmamodes & 0x02)
798 	return ATA_PIO3;
799     if (ap->mwdmamodes & 0x01)
800 	return ATA_PIO2;
801     if ((ap->retired_piomode & ATA_RETIRED_PIO_MASK) == 0x200)
802 	return ATA_PIO2;
803     if ((ap->retired_piomode & ATA_RETIRED_PIO_MASK) == 0x100)
804 	return ATA_PIO1;
805     if ((ap->retired_piomode & ATA_RETIRED_PIO_MASK) == 0x000)
806 	return ATA_PIO0;
807     return ATA_PIO0;
808 }
809 
810 int
811 ata_max_wmode(struct ata_params *ap)
812 {
813     if (ap->mwdmamodes & 0x04)
814 	return ATA_WDMA2;
815     if (ap->mwdmamodes & 0x02)
816 	return ATA_WDMA1;
817     if (ap->mwdmamodes & 0x01)
818 	return ATA_WDMA0;
819     return -1;
820 }
821 
822 int
823 ata_max_umode(struct ata_params *ap)
824 {
825     if (ap->atavalid & ATA_FLAG_88) {
826 	if (ap->udmamodes & 0x40)
827 	    return ATA_UDMA6;
828 	if (ap->udmamodes & 0x20)
829 	    return ATA_UDMA5;
830 	if (ap->udmamodes & 0x10)
831 	    return ATA_UDMA4;
832 	if (ap->udmamodes & 0x08)
833 	    return ATA_UDMA3;
834 	if (ap->udmamodes & 0x04)
835 	    return ATA_UDMA2;
836 	if (ap->udmamodes & 0x02)
837 	    return ATA_UDMA1;
838 	if (ap->udmamodes & 0x01)
839 	    return ATA_UDMA0;
840     }
841     return -1;
842 }
843 
844 int
845 ata_max_mode(struct ata_params *ap, int maxmode)
846 {
847 
848 	if (maxmode == 0)
849 		maxmode = ATA_DMA_MAX;
850 	if (maxmode >= ATA_UDMA0 && ata_max_umode(ap) > 0)
851 		return (min(maxmode, ata_max_umode(ap)));
852 	if (maxmode >= ATA_WDMA0 && ata_max_wmode(ap) > 0)
853 		return (min(maxmode, ata_max_wmode(ap)));
854 	return (min(maxmode, ata_max_pmode(ap)));
855 }
856 
857 char *
858 ata_mode2string(int mode)
859 {
860     switch (mode) {
861     case -1: return "UNSUPPORTED";
862     case 0: return "NONE";
863     case ATA_PIO0: return "PIO0";
864     case ATA_PIO1: return "PIO1";
865     case ATA_PIO2: return "PIO2";
866     case ATA_PIO3: return "PIO3";
867     case ATA_PIO4: return "PIO4";
868     case ATA_WDMA0: return "WDMA0";
869     case ATA_WDMA1: return "WDMA1";
870     case ATA_WDMA2: return "WDMA2";
871     case ATA_UDMA0: return "UDMA0";
872     case ATA_UDMA1: return "UDMA1";
873     case ATA_UDMA2: return "UDMA2";
874     case ATA_UDMA3: return "UDMA3";
875     case ATA_UDMA4: return "UDMA4";
876     case ATA_UDMA5: return "UDMA5";
877     case ATA_UDMA6: return "UDMA6";
878     default:
879 	if (mode & ATA_DMA_MASK)
880 	    return "BIOSDMA";
881 	else
882 	    return "BIOSPIO";
883     }
884 }
885 
886 int
887 ata_string2mode(char *str)
888 {
889 	if (!strcasecmp(str, "PIO0")) return (ATA_PIO0);
890 	if (!strcasecmp(str, "PIO1")) return (ATA_PIO1);
891 	if (!strcasecmp(str, "PIO2")) return (ATA_PIO2);
892 	if (!strcasecmp(str, "PIO3")) return (ATA_PIO3);
893 	if (!strcasecmp(str, "PIO4")) return (ATA_PIO4);
894 	if (!strcasecmp(str, "WDMA0")) return (ATA_WDMA0);
895 	if (!strcasecmp(str, "WDMA1")) return (ATA_WDMA1);
896 	if (!strcasecmp(str, "WDMA2")) return (ATA_WDMA2);
897 	if (!strcasecmp(str, "UDMA0")) return (ATA_UDMA0);
898 	if (!strcasecmp(str, "UDMA16")) return (ATA_UDMA0);
899 	if (!strcasecmp(str, "UDMA1")) return (ATA_UDMA1);
900 	if (!strcasecmp(str, "UDMA25")) return (ATA_UDMA1);
901 	if (!strcasecmp(str, "UDMA2")) return (ATA_UDMA2);
902 	if (!strcasecmp(str, "UDMA33")) return (ATA_UDMA2);
903 	if (!strcasecmp(str, "UDMA3")) return (ATA_UDMA3);
904 	if (!strcasecmp(str, "UDMA44")) return (ATA_UDMA3);
905 	if (!strcasecmp(str, "UDMA4")) return (ATA_UDMA4);
906 	if (!strcasecmp(str, "UDMA66")) return (ATA_UDMA4);
907 	if (!strcasecmp(str, "UDMA5")) return (ATA_UDMA5);
908 	if (!strcasecmp(str, "UDMA100")) return (ATA_UDMA5);
909 	if (!strcasecmp(str, "UDMA6")) return (ATA_UDMA6);
910 	if (!strcasecmp(str, "UDMA133")) return (ATA_UDMA6);
911 	return (-1);
912 }
913 
914 
915 u_int
916 ata_mode2speed(int mode)
917 {
918 	switch (mode) {
919 	case ATA_PIO0:
920 	default:
921 		return (3300);
922 	case ATA_PIO1:
923 		return (5200);
924 	case ATA_PIO2:
925 		return (8300);
926 	case ATA_PIO3:
927 		return (11100);
928 	case ATA_PIO4:
929 		return (16700);
930 	case ATA_WDMA0:
931 		return (4200);
932 	case ATA_WDMA1:
933 		return (13300);
934 	case ATA_WDMA2:
935 		return (16700);
936 	case ATA_UDMA0:
937 		return (16700);
938 	case ATA_UDMA1:
939 		return (25000);
940 	case ATA_UDMA2:
941 		return (33300);
942 	case ATA_UDMA3:
943 		return (44400);
944 	case ATA_UDMA4:
945 		return (66700);
946 	case ATA_UDMA5:
947 		return (100000);
948 	case ATA_UDMA6:
949 		return (133000);
950 	}
951 }
952 
953 u_int
954 ata_revision2speed(int revision)
955 {
956 	switch (revision) {
957 	case 1:
958 	default:
959 		return (150000);
960 	case 2:
961 		return (300000);
962 	case 3:
963 		return (600000);
964 	}
965 }
966 
967 int
968 ata_speed2revision(u_int speed)
969 {
970 	switch (speed) {
971 	case 0:
972 		return (0);
973 	case 150000:
974 		return (1);
975 	case 300000:
976 		return (2);
977 	case 600000:
978 		return (3);
979 	default:
980 		return (-1);
981 	}
982 }
983 
984 int
985 ata_identify_match(caddr_t identbuffer, caddr_t table_entry)
986 {
987 	struct scsi_inquiry_pattern *entry;
988 	struct ata_params *ident;
989 
990 	entry = (struct scsi_inquiry_pattern *)table_entry;
991 	ident = (struct ata_params *)identbuffer;
992 
993 	if ((cam_strmatch(ident->model, entry->product,
994 			  sizeof(ident->model)) == 0)
995 	 && (cam_strmatch(ident->revision, entry->revision,
996 			  sizeof(ident->revision)) == 0)) {
997 		return (0);
998 	}
999         return (-1);
1000 }
1001 
1002 int
1003 ata_static_identify_match(caddr_t identbuffer, caddr_t table_entry)
1004 {
1005 	struct scsi_static_inquiry_pattern *entry;
1006 	struct ata_params *ident;
1007 
1008 	entry = (struct scsi_static_inquiry_pattern *)table_entry;
1009 	ident = (struct ata_params *)identbuffer;
1010 
1011 	if ((cam_strmatch(ident->model, entry->product,
1012 			  sizeof(ident->model)) == 0)
1013 	 && (cam_strmatch(ident->revision, entry->revision,
1014 			  sizeof(ident->revision)) == 0)) {
1015 		return (0);
1016 	}
1017         return (-1);
1018 }
1019 
1020 void
1021 semb_receive_diagnostic_results(struct ccb_ataio *ataio,
1022     u_int32_t retries, void (*cbfcnp)(struct cam_periph *, union ccb*),
1023     uint8_t tag_action, int pcv, uint8_t page_code,
1024     uint8_t *data_ptr, uint16_t length, uint32_t timeout)
1025 {
1026 
1027 	length = min(length, 1020);
1028 	length = (length + 3) & ~3;
1029 	cam_fill_ataio(ataio,
1030 		      retries,
1031 		      cbfcnp,
1032 		      /*flags*/CAM_DIR_IN,
1033 		      tag_action,
1034 		      data_ptr,
1035 		      length,
1036 		      timeout);
1037 	ata_28bit_cmd(ataio, ATA_SEP_ATTN,
1038 	    pcv ? page_code : 0, 0x02, length / 4);
1039 }
1040 
1041 void
1042 semb_send_diagnostic(struct ccb_ataio *ataio,
1043     u_int32_t retries, void (*cbfcnp)(struct cam_periph *, union ccb *),
1044     uint8_t tag_action, uint8_t *data_ptr, uint16_t length, uint32_t timeout)
1045 {
1046 
1047 	length = min(length, 1020);
1048 	length = (length + 3) & ~3;
1049 	cam_fill_ataio(ataio,
1050 		      retries,
1051 		      cbfcnp,
1052 		      /*flags*/length ? CAM_DIR_OUT : CAM_DIR_NONE,
1053 		      tag_action,
1054 		      data_ptr,
1055 		      length,
1056 		      timeout);
1057 	ata_28bit_cmd(ataio, ATA_SEP_ATTN,
1058 	    length > 0 ? data_ptr[0] : 0, 0x82, length / 4);
1059 }
1060 
1061 void
1062 semb_read_buffer(struct ccb_ataio *ataio,
1063     u_int32_t retries, void (*cbfcnp)(struct cam_periph *, union ccb*),
1064     uint8_t tag_action, uint8_t page_code,
1065     uint8_t *data_ptr, uint16_t length, uint32_t timeout)
1066 {
1067 
1068 	length = min(length, 1020);
1069 	length = (length + 3) & ~3;
1070 	cam_fill_ataio(ataio,
1071 		      retries,
1072 		      cbfcnp,
1073 		      /*flags*/CAM_DIR_IN,
1074 		      tag_action,
1075 		      data_ptr,
1076 		      length,
1077 		      timeout);
1078 	ata_28bit_cmd(ataio, ATA_SEP_ATTN,
1079 	    page_code, 0x00, length / 4);
1080 }
1081 
1082 void
1083 semb_write_buffer(struct ccb_ataio *ataio,
1084     u_int32_t retries, void (*cbfcnp)(struct cam_periph *, union ccb *),
1085     uint8_t tag_action, uint8_t *data_ptr, uint16_t length, uint32_t timeout)
1086 {
1087 
1088 	length = min(length, 1020);
1089 	length = (length + 3) & ~3;
1090 	cam_fill_ataio(ataio,
1091 		      retries,
1092 		      cbfcnp,
1093 		      /*flags*/length ? CAM_DIR_OUT : CAM_DIR_NONE,
1094 		      tag_action,
1095 		      data_ptr,
1096 		      length,
1097 		      timeout);
1098 	ata_28bit_cmd(ataio, ATA_SEP_ATTN,
1099 	    length > 0 ? data_ptr[0] : 0, 0x80, length / 4);
1100 }
1101 
1102 
1103 void
1104 ata_zac_mgmt_out(struct ccb_ataio *ataio, uint32_t retries,
1105 		 void (*cbfcnp)(struct cam_periph *, union ccb *),
1106 		 int use_ncq, uint8_t zm_action, uint64_t zone_id,
1107 		 uint8_t zone_flags, uint16_t sector_count, uint8_t *data_ptr,
1108 		 uint32_t dxfer_len, uint32_t timeout)
1109 {
1110 	uint8_t command_out, ata_flags;
1111 	uint16_t features_out, sectors_out;
1112 	uint32_t auxiliary;
1113 
1114 	if (use_ncq == 0) {
1115 		command_out = ATA_ZAC_MANAGEMENT_OUT;
1116 		features_out = (zm_action & 0xf) | (zone_flags << 8);
1117 		if (dxfer_len == 0) {
1118 			ata_flags = 0;
1119 			sectors_out = 0;
1120 		} else {
1121 			ata_flags = CAM_ATAIO_DMA;
1122 			/* XXX KDM use sector count? */
1123 			sectors_out = ((dxfer_len >> 9) & 0xffff);
1124 		}
1125 		auxiliary = 0;
1126 	} else {
1127 		if (dxfer_len == 0) {
1128 			command_out = ATA_NCQ_NON_DATA;
1129 			features_out = ATA_NCQ_ZAC_MGMT_OUT;
1130 			sectors_out = 0;
1131 		} else {
1132 			command_out = ATA_SEND_FPDMA_QUEUED;
1133 
1134 			/* Note that we're defaulting to normal priority */
1135 			sectors_out = ATA_SFPDMA_ZAC_MGMT_OUT << 8;
1136 
1137 			/*
1138 			 * For SEND FPDMA QUEUED, the transfer length is
1139 			 * encoded in the FEATURE register, and 0 means
1140 			 * that 65536 512 byte blocks are to be tranferred.
1141 			 * In practice, it seems unlikely that we'll see
1142 			 * a transfer that large.
1143 			 */
1144 			if (dxfer_len == (65536 * 512)) {
1145 				features_out = 0;
1146 			} else {
1147 				/*
1148 				 * Yes, the caller can theoretically send a
1149 				 * transfer larger than we can handle.
1150 				 * Anyone using this function needs enough
1151 				 * knowledge to avoid doing that.
1152 				 */
1153 				features_out = ((dxfer_len >> 9) & 0xffff);
1154 			}
1155 		}
1156 		auxiliary = (zm_action & 0xf) | (zone_flags << 8);
1157 
1158 		ata_flags = CAM_ATAIO_FPDMA;
1159 	}
1160 
1161 	cam_fill_ataio(ataio,
1162 	    /*retries*/ retries,
1163 	    /*cbfcnp*/ cbfcnp,
1164 	    /*flags*/ (dxfer_len > 0) ? CAM_DIR_OUT : CAM_DIR_NONE,
1165 	    /*tag_action*/ 0,
1166 	    /*data_ptr*/ data_ptr,
1167 	    /*dxfer_len*/ dxfer_len,
1168 	    /*timeout*/ timeout);
1169 
1170 	ata_48bit_cmd(ataio,
1171 	    /*cmd*/ command_out,
1172 	    /*features*/ features_out,
1173 	    /*lba*/ zone_id,
1174 	    /*sector_count*/ sectors_out);
1175 
1176 	ataio->cmd.flags |= ata_flags;
1177 	if (auxiliary != 0) {
1178 		ataio->ata_flags |= ATA_FLAG_AUX;
1179 		ataio->aux = auxiliary;
1180 	}
1181 }
1182 
1183 void
1184 ata_zac_mgmt_in(struct ccb_ataio *ataio, uint32_t retries,
1185 		void (*cbfcnp)(struct cam_periph *, union ccb *),
1186 		int use_ncq, uint8_t zm_action, uint64_t zone_id,
1187 		uint8_t zone_flags, uint8_t *data_ptr, uint32_t dxfer_len,
1188 		uint32_t timeout)
1189 {
1190 	uint8_t command_out, ata_flags;
1191 	uint16_t features_out, sectors_out;
1192 	uint32_t auxiliary;
1193 
1194 	if (use_ncq == 0) {
1195 		command_out = ATA_ZAC_MANAGEMENT_IN;
1196 		/* XXX KDM put a macro here */
1197 		features_out = (zm_action & 0xf) | (zone_flags << 8);
1198 		ata_flags = CAM_ATAIO_DMA;
1199 		sectors_out = ((dxfer_len >> 9) & 0xffff);
1200 		auxiliary = 0;
1201 	} else {
1202 		command_out = ATA_RECV_FPDMA_QUEUED;
1203 		sectors_out = ATA_RFPDMA_ZAC_MGMT_IN << 8;
1204 		auxiliary = (zm_action & 0xf) | (zone_flags << 8);
1205 		ata_flags = CAM_ATAIO_FPDMA;
1206 		/*
1207 		 * For RECEIVE FPDMA QUEUED, the transfer length is
1208 		 * encoded in the FEATURE register, and 0 means
1209 		 * that 65536 512 byte blocks are to be tranferred.
1210 		 * In practice, it is unlikely we will see a transfer that
1211 		 * large.
1212 		 */
1213 		if (dxfer_len == (65536 * 512)) {
1214 			features_out = 0;
1215 		} else {
1216 			/*
1217 			 * Yes, the caller can theoretically request a
1218 			 * transfer larger than we can handle.
1219 			 * Anyone using this function needs enough
1220 			 * knowledge to avoid doing that.
1221 			 */
1222 			features_out = ((dxfer_len >> 9) & 0xffff);
1223 		}
1224 	}
1225 
1226 	cam_fill_ataio(ataio,
1227 	    /*retries*/ retries,
1228 	    /*cbfcnp*/ cbfcnp,
1229 	    /*flags*/ CAM_DIR_IN,
1230 	    /*tag_action*/ 0,
1231 	    /*data_ptr*/ data_ptr,
1232 	    /*dxfer_len*/ dxfer_len,
1233 	    /*timeout*/ timeout);
1234 
1235 	ata_48bit_cmd(ataio,
1236 	    /*cmd*/ command_out,
1237 	    /*features*/ features_out,
1238 	    /*lba*/ zone_id,
1239 	    /*sector_count*/ sectors_out);
1240 
1241 	ataio->cmd.flags |= ata_flags;
1242 	if (auxiliary != 0) {
1243 		ataio->ata_flags |= ATA_FLAG_AUX;
1244 		ataio->aux = auxiliary;
1245 	}
1246 }
1247 
1248 void
1249 ata_param_fixup(struct ata_params *ident_buf)
1250 {
1251 	int16_t *ptr;
1252 
1253 	for (ptr = (int16_t *)ident_buf;
1254 	     ptr < (int16_t *)ident_buf + sizeof(struct ata_params)/2; ptr++) {
1255 		*ptr = le16toh(*ptr);
1256 	}
1257 	if (strncmp(ident_buf->model, "FX", 2) &&
1258 	    strncmp(ident_buf->model, "NEC", 3) &&
1259 	    strncmp(ident_buf->model, "Pioneer", 7) &&
1260 	    strncmp(ident_buf->model, "SHARP", 5)) {
1261 		ata_bswap(ident_buf->model, sizeof(ident_buf->model));
1262 		ata_bswap(ident_buf->revision, sizeof(ident_buf->revision));
1263 		ata_bswap(ident_buf->serial, sizeof(ident_buf->serial));
1264 	}
1265 	ata_btrim(ident_buf->model, sizeof(ident_buf->model));
1266 	ata_bpack(ident_buf->model, ident_buf->model, sizeof(ident_buf->model));
1267 	ata_btrim(ident_buf->revision, sizeof(ident_buf->revision));
1268 	ata_bpack(ident_buf->revision, ident_buf->revision, sizeof(ident_buf->revision));
1269 	ata_btrim(ident_buf->serial, sizeof(ident_buf->serial));
1270 	ata_bpack(ident_buf->serial, ident_buf->serial, sizeof(ident_buf->serial));
1271 }
1272