xref: /freebsd/sys/cam/ata/ata_all.c (revision 882f88ff77194ef2eea005232780468404438788)
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:
219 		switch(cmd->features) {
220 		case 0x00: return ("SANITIZE_STATUS_EXT");
221 		case 0x11: return ("CRYPTO_SCRAMBLE_EXT");
222 		case 0x12: return ("BLOCK_ERASE_EXT");
223 		case 0x14: return ("OVERWRITE_EXT");
224 		case 0x20: return ("SANITIZE_FREEZE_LOCK_EXT");
225 		case 0x40: return ("SANITIZE_ANTIFREEZE_LOCK_EXT");
226 		}
227 		return ("SANITIZE_DEVICE");
228 	case 0xc0: return ("CFA_ERASE");
229 	case 0xc4: return ("READ_MUL");
230 	case 0xc5: return ("WRITE_MUL");
231 	case 0xc6: return ("SET_MULTI");
232 	case 0xc7: return ("READ_DMA_QUEUED");
233 	case 0xc8: return ("READ_DMA");
234 	case 0xca: return ("WRITE_DMA");
235 	case 0xcc: return ("WRITE_DMA_QUEUED");
236 	case 0xcd: return ("CFA_WRITE_MULTIPLE_WITHOUT_ERASE");
237 	case 0xce: return ("WRITE_MUL_FUA48");
238 	case 0xd1: return ("CHECK_MEDIA_CARD_TYPE");
239 	case 0xda: return ("GET_MEDIA_STATUS");
240 	case 0xde: return ("MEDIA_LOCK");
241 	case 0xdf: return ("MEDIA_UNLOCK");
242 	case 0xe0: return ("STANDBY_IMMEDIATE");
243 	case 0xe1: return ("IDLE_IMMEDIATE");
244 	case 0xe2: return ("STANDBY");
245 	case 0xe3: return ("IDLE");
246 	case 0xe4: return ("READ_BUFFER/PM");
247 	case 0xe5: return ("CHECK_POWER_MODE");
248 	case 0xe6: return ("SLEEP");
249 	case 0xe7: return ("FLUSHCACHE");
250 	case 0xe8: return ("WRITE_BUFFER/PM");
251 	case 0xe9: return ("READ_BUFFER_DMA");
252 	case 0xea: return ("FLUSHCACHE48");
253 	case 0xeb: return ("WRITE_BUFFER_DMA");
254 	case 0xec: return ("ATA_IDENTIFY");
255 	case 0xed: return ("MEDIA_EJECT");
256 	case 0xef:
257 		/*
258 		 * XXX KDM need common decoding between NCQ and non-NCQ
259 		 * versions of SET FEATURES.
260 		 */
261 		switch (cmd->features) {
262 	        case 0x02: return ("SETFEATURES ENABLE WCACHE");
263 	        case 0x03: return ("SETFEATURES SET TRANSFER MODE");
264 		case 0x05: return ("SETFEATURES ENABLE APM");
265 	        case 0x06: return ("SETFEATURES ENABLE PUIS");
266 	        case 0x07: return ("SETFEATURES SPIN-UP");
267 		case 0x0b: return ("SETFEATURES ENABLE WRITE READ VERIFY");
268 		case 0x0c: return ("SETFEATURES ENABLE DEVICE LIFE CONTROL");
269 	        case 0x10: return ("SETFEATURES ENABLE SATA FEATURE");
270 		case 0x41: return ("SETFEATURES ENABLE FREEFALL CONTROL");
271 		case 0x43: return ("SETFEATURES SET MAX HOST INT SECT TIMES");
272 		case 0x45: return ("SETFEATURES SET RATE BASIS");
273 		case 0x4a: return ("SETFEATURES EXTENDED POWER CONDITIONS");
274 		case 0x50: return ("SETFEATURES ADVANCED BACKGROUD OPERATION");
275 	        case 0x55: return ("SETFEATURES DISABLE RCACHE");
276 		case 0x5d: return ("SETFEATURES ENABLE RELIRQ");
277 		case 0x5e: return ("SETFEATURES ENABLE SRVIRQ");
278 		case 0x62: return ("SETFEATURES LONG PHYS SECT ALIGN ERC");
279 		case 0x63: return ("SETFEATURES DSN");
280 		case 0x66: return ("SETFEATURES DISABLE DEFAULTS");
281 	        case 0x82: return ("SETFEATURES DISABLE WCACHE");
282 	        case 0x85: return ("SETFEATURES DISABLE APM");
283 	        case 0x86: return ("SETFEATURES DISABLE PUIS");
284 		case 0x8b: return ("SETFEATURES DISABLE WRITE READ VERIFY");
285 		case 0x8c: return ("SETFEATURES DISABLE DEVICE LIFE CONTROL");
286 	        case 0x90: return ("SETFEATURES DISABLE SATA FEATURE");
287 	        case 0xaa: return ("SETFEATURES ENABLE RCACHE");
288 		case 0xC1: return ("SETFEATURES DISABLE FREEFALL CONTROL");
289 		case 0xC3: return ("SETFEATURES SENSE DATA REPORTING");
290 		case 0xC4: return ("SETFEATURES NCQ SENSE DATA RETURN");
291 		case 0xCC: return ("SETFEATURES ENABLE DEFAULTS");
292 		case 0xdd: return ("SETFEATURES DISABLE RELIRQ");
293 		case 0xde: return ("SETFEATURES DISABLE SRVIRQ");
294 	        }
295 	        return "SETFEATURES";
296 	case 0xf1: return ("SECURITY_SET_PASSWORD");
297 	case 0xf2: return ("SECURITY_UNLOCK");
298 	case 0xf3: return ("SECURITY_ERASE_PREPARE");
299 	case 0xf4: return ("SECURITY_ERASE_UNIT");
300 	case 0xf5: return ("SECURITY_FREEZE_LOCK");
301 	case 0xf6: return ("SECURITY_DISABLE_PASSWORD");
302 	case 0xf8: return ("READ_NATIVE_MAX_ADDRESS");
303 	case 0xf9: return ("SET_MAX_ADDRESS");
304 	}
305 	return "UNKNOWN";
306 }
307 
308 char *
309 ata_cmd_string(struct ata_cmd *cmd, char *cmd_string, size_t len)
310 {
311 	struct sbuf sb;
312 	int error;
313 
314 	if (len == 0)
315 		return ("");
316 
317 	sbuf_new(&sb, cmd_string, len, SBUF_FIXEDLEN);
318 	ata_cmd_sbuf(cmd, &sb);
319 
320 	error = sbuf_finish(&sb);
321 	if (error != 0 && error != ENOMEM)
322 		return ("");
323 
324 	return(sbuf_data(&sb));
325 }
326 
327 void
328 ata_cmd_sbuf(struct ata_cmd *cmd, struct sbuf *sb)
329 {
330 	sbuf_printf(sb, "%02x %02x %02x %02x "
331 	    "%02x %02x %02x %02x %02x %02x %02x %02x",
332 	    cmd->command, cmd->features,
333 	    cmd->lba_low, cmd->lba_mid, cmd->lba_high, cmd->device,
334 	    cmd->lba_low_exp, cmd->lba_mid_exp, cmd->lba_high_exp,
335 	    cmd->features_exp, cmd->sector_count, cmd->sector_count_exp);
336 }
337 
338 char *
339 ata_res_string(struct ata_res *res, char *res_string, size_t len)
340 {
341 	struct sbuf sb;
342 	int error;
343 
344 	if (len == 0)
345 		return ("");
346 
347 	sbuf_new(&sb, res_string, len, SBUF_FIXEDLEN);
348 	ata_res_sbuf(res, &sb);
349 
350 	error = sbuf_finish(&sb);
351 	if (error != 0 && error != ENOMEM)
352 		return ("");
353 
354 	return(sbuf_data(&sb));
355 }
356 
357 int
358 ata_res_sbuf(struct ata_res *res, struct sbuf *sb)
359 {
360 
361 	sbuf_printf(sb, "%02x %02x %02x %02x "
362 	    "%02x %02x %02x %02x %02x %02x %02x",
363 	    res->status, res->error,
364 	    res->lba_low, res->lba_mid, res->lba_high, res->device,
365 	    res->lba_low_exp, res->lba_mid_exp, res->lba_high_exp,
366 	    res->sector_count, res->sector_count_exp);
367 
368 	return (0);
369 }
370 
371 /*
372  * ata_command_sbuf() returns 0 for success and -1 for failure.
373  */
374 int
375 ata_command_sbuf(struct ccb_ataio *ataio, struct sbuf *sb)
376 {
377 
378 	sbuf_printf(sb, "%s. ACB: ",
379 	    ata_op_string(&ataio->cmd));
380 	ata_cmd_sbuf(&ataio->cmd, sb);
381 
382 	return(0);
383 }
384 
385 /*
386  * ata_status_abuf() returns 0 for success and -1 for failure.
387  */
388 int
389 ata_status_sbuf(struct ccb_ataio *ataio, struct sbuf *sb)
390 {
391 
392 	sbuf_printf(sb, "ATA status: %02x (%s%s%s%s%s%s%s%s)",
393 	    ataio->res.status,
394 	    (ataio->res.status & 0x80) ? "BSY " : "",
395 	    (ataio->res.status & 0x40) ? "DRDY " : "",
396 	    (ataio->res.status & 0x20) ? "DF " : "",
397 	    (ataio->res.status & 0x10) ? "SERV " : "",
398 	    (ataio->res.status & 0x08) ? "DRQ " : "",
399 	    (ataio->res.status & 0x04) ? "CORR " : "",
400 	    (ataio->res.status & 0x02) ? "IDX " : "",
401 	    (ataio->res.status & 0x01) ? "ERR" : "");
402 	if (ataio->res.status & 1) {
403 	    sbuf_printf(sb, ", error: %02x (%s%s%s%s%s%s%s%s)",
404 		ataio->res.error,
405 		(ataio->res.error & 0x80) ? "ICRC " : "",
406 		(ataio->res.error & 0x40) ? "UNC " : "",
407 		(ataio->res.error & 0x20) ? "MC " : "",
408 		(ataio->res.error & 0x10) ? "IDNF " : "",
409 		(ataio->res.error & 0x08) ? "MCR " : "",
410 		(ataio->res.error & 0x04) ? "ABRT " : "",
411 		(ataio->res.error & 0x02) ? "NM " : "",
412 		(ataio->res.error & 0x01) ? "ILI" : "");
413 	}
414 
415 	return(0);
416 }
417 
418 void
419 ata_print_ident(struct ata_params *ident_data)
420 {
421 	const char *proto;
422 	char ata[12], sata[12];
423 
424 	ata_print_ident_short(ident_data);
425 
426 	proto = (ident_data->config == ATA_PROTO_CFA) ? "CFA" :
427 		(ident_data->config & ATA_PROTO_ATAPI) ? "ATAPI" : "ATA";
428 	if (ata_version(ident_data->version_major) == 0) {
429 		snprintf(ata, sizeof(ata), "%s", proto);
430 	} else if (ata_version(ident_data->version_major) <= 7) {
431 		snprintf(ata, sizeof(ata), "%s-%d", proto,
432 		    ata_version(ident_data->version_major));
433 	} else if (ata_version(ident_data->version_major) == 8) {
434 		snprintf(ata, sizeof(ata), "%s8-ACS", proto);
435 	} else {
436 		snprintf(ata, sizeof(ata), "ACS-%d %s",
437 		    ata_version(ident_data->version_major) - 7, proto);
438 	}
439 	if (ident_data->satacapabilities && ident_data->satacapabilities != 0xffff) {
440 		if (ident_data->satacapabilities & ATA_SATA_GEN3)
441 			snprintf(sata, sizeof(sata), " SATA 3.x");
442 		else if (ident_data->satacapabilities & ATA_SATA_GEN2)
443 			snprintf(sata, sizeof(sata), " SATA 2.x");
444 		else if (ident_data->satacapabilities & ATA_SATA_GEN1)
445 			snprintf(sata, sizeof(sata), " SATA 1.x");
446 		else
447 			snprintf(sata, sizeof(sata), " SATA");
448 	} else
449 		sata[0] = 0;
450 	printf(" %s%s device\n", ata, sata);
451 }
452 
453 void
454 ata_print_ident_sbuf(struct ata_params *ident_data, struct sbuf *sb)
455 {
456 	const char *proto, *sata;
457 	int version;
458 
459 	ata_print_ident_short_sbuf(ident_data, sb);
460 	sbuf_printf(sb, " ");
461 
462 	proto = (ident_data->config == ATA_PROTO_CFA) ? "CFA" :
463 		(ident_data->config & ATA_PROTO_ATAPI) ? "ATAPI" : "ATA";
464 	version = ata_version(ident_data->version_major);
465 
466 	switch (version) {
467 	case 0:
468 		sbuf_printf(sb, "%s", proto);
469 		break;
470 	case 1:
471 	case 2:
472 	case 3:
473 	case 4:
474 	case 5:
475 	case 6:
476 	case 7:
477 		sbuf_printf(sb, "%s-%d", proto, version);
478 		break;
479 	case 8:
480 		sbuf_printf(sb, "%s8-ACS", proto);
481 		break;
482 	default:
483 		sbuf_printf(sb, "ACS-%d %s", version - 7, proto);
484 		break;
485 	}
486 
487 	if (ident_data->satacapabilities && ident_data->satacapabilities != 0xffff) {
488 		if (ident_data->satacapabilities & ATA_SATA_GEN3)
489 			sata = " SATA 3.x";
490 		else if (ident_data->satacapabilities & ATA_SATA_GEN2)
491 			sata = " SATA 2.x";
492 		else if (ident_data->satacapabilities & ATA_SATA_GEN1)
493 			sata = " SATA 1.x";
494 		else
495 			sata = " SATA";
496 	} else
497 		sata = "";
498 	sbuf_printf(sb, "%s device\n", sata);
499 }
500 
501 void
502 ata_print_ident_short(struct ata_params *ident_data)
503 {
504 	char product[48], revision[16];
505 
506 	cam_strvis(product, ident_data->model, sizeof(ident_data->model),
507 		   sizeof(product));
508 	cam_strvis(revision, ident_data->revision, sizeof(ident_data->revision),
509 		   sizeof(revision));
510 	printf("<%s %s>", product, revision);
511 }
512 
513 void
514 ata_print_ident_short_sbuf(struct ata_params *ident_data, struct sbuf *sb)
515 {
516 
517 	sbuf_printf(sb, "<");
518 	cam_strvis_sbuf(sb, ident_data->model, sizeof(ident_data->model), 0);
519 	sbuf_printf(sb, " ");
520 	cam_strvis_sbuf(sb, ident_data->revision, sizeof(ident_data->revision), 0);
521 	sbuf_printf(sb, ">");
522 }
523 
524 void
525 semb_print_ident(struct sep_identify_data *ident_data)
526 {
527 	char in[7], ins[5];
528 
529 	semb_print_ident_short(ident_data);
530 	cam_strvis(in, ident_data->interface_id, 6, sizeof(in));
531 	cam_strvis(ins, ident_data->interface_rev, 4, sizeof(ins));
532 	printf(" SEMB %s %s device\n", in, ins);
533 }
534 
535 void
536 semb_print_ident_sbuf(struct sep_identify_data *ident_data, struct sbuf *sb)
537 {
538 
539 	semb_print_ident_short_sbuf(ident_data, sb);
540 
541 	sbuf_printf(sb, " SEMB ");
542 	cam_strvis_sbuf(sb, ident_data->interface_id, 6, 0);
543 	sbuf_printf(sb, " ");
544 	cam_strvis_sbuf(sb, ident_data->interface_rev, 4, 0);
545 	sbuf_printf(sb, " device\n");
546 }
547 
548 void
549 semb_print_ident_short(struct sep_identify_data *ident_data)
550 {
551 	char vendor[9], product[17], revision[5], fw[5];
552 
553 	cam_strvis(vendor, ident_data->vendor_id, 8, sizeof(vendor));
554 	cam_strvis(product, ident_data->product_id, 16, sizeof(product));
555 	cam_strvis(revision, ident_data->product_rev, 4, sizeof(revision));
556 	cam_strvis(fw, ident_data->firmware_rev, 4, sizeof(fw));
557 	printf("<%s %s %s %s>", vendor, product, revision, fw);
558 }
559 
560 void
561 semb_print_ident_short_sbuf(struct sep_identify_data *ident_data, struct sbuf *sb)
562 {
563 
564 	sbuf_printf(sb, "<");
565 	cam_strvis_sbuf(sb, ident_data->vendor_id, 8, 0);
566 	sbuf_printf(sb, " ");
567 	cam_strvis_sbuf(sb, ident_data->product_id, 16, 0);
568 	sbuf_printf(sb, " ");
569 	cam_strvis_sbuf(sb, ident_data->product_rev, 4, 0);
570 	sbuf_printf(sb, " ");
571 	cam_strvis_sbuf(sb, ident_data->firmware_rev, 4, 0);
572 	sbuf_printf(sb, ">");
573 }
574 
575 uint32_t
576 ata_logical_sector_size(struct ata_params *ident_data)
577 {
578 	if ((ident_data->pss & ATA_PSS_VALID_MASK) == ATA_PSS_VALID_VALUE &&
579 	    (ident_data->pss & ATA_PSS_LSSABOVE512)) {
580 		return (((u_int32_t)ident_data->lss_1 |
581 		    ((u_int32_t)ident_data->lss_2 << 16)) * 2);
582 	}
583 	return (512);
584 }
585 
586 uint64_t
587 ata_physical_sector_size(struct ata_params *ident_data)
588 {
589 	if ((ident_data->pss & ATA_PSS_VALID_MASK) == ATA_PSS_VALID_VALUE) {
590 		if (ident_data->pss & ATA_PSS_MULTLS) {
591 			return ((uint64_t)ata_logical_sector_size(ident_data) *
592 			    (1 << (ident_data->pss & ATA_PSS_LSPPS)));
593 		} else {
594 			return (uint64_t)ata_logical_sector_size(ident_data);
595 		}
596 	}
597 	return (512);
598 }
599 
600 uint64_t
601 ata_logical_sector_offset(struct ata_params *ident_data)
602 {
603 	if ((ident_data->lsalign & 0xc000) == 0x4000) {
604 		return ((uint64_t)ata_logical_sector_size(ident_data) *
605 		    (ident_data->lsalign & 0x3fff));
606 	}
607 	return (0);
608 }
609 
610 void
611 ata_28bit_cmd(struct ccb_ataio *ataio, uint8_t cmd, uint8_t features,
612     uint32_t lba, uint8_t sector_count)
613 {
614 	bzero(&ataio->cmd, sizeof(ataio->cmd));
615 	ataio->cmd.flags = 0;
616 	if (cmd == ATA_READ_DMA ||
617 	    cmd == ATA_READ_DMA_QUEUED ||
618 	    cmd == ATA_WRITE_DMA ||
619 	    cmd == ATA_WRITE_DMA_QUEUED ||
620 	    cmd == ATA_TRUSTED_RECEIVE_DMA ||
621 	    cmd == ATA_TRUSTED_SEND_DMA ||
622 	    cmd == ATA_DOWNLOAD_MICROCODE_DMA ||
623 	    cmd == ATA_READ_BUFFER_DMA ||
624 	    cmd == ATA_WRITE_BUFFER_DMA)
625 		ataio->cmd.flags |= CAM_ATAIO_DMA;
626 	ataio->cmd.command = cmd;
627 	ataio->cmd.features = features;
628 	ataio->cmd.lba_low = lba;
629 	ataio->cmd.lba_mid = lba >> 8;
630 	ataio->cmd.lba_high = lba >> 16;
631 	ataio->cmd.device = ATA_DEV_LBA | ((lba >> 24) & 0x0f);
632 	ataio->cmd.sector_count = sector_count;
633 }
634 
635 void
636 ata_48bit_cmd(struct ccb_ataio *ataio, uint8_t cmd, uint16_t features,
637     uint64_t lba, uint16_t sector_count)
638 {
639 
640 	ataio->cmd.flags = CAM_ATAIO_48BIT;
641 	if (cmd == ATA_READ_DMA48 ||
642 	    cmd == ATA_READ_DMA_QUEUED48 ||
643 	    cmd == ATA_READ_STREAM_DMA48 ||
644 	    cmd == ATA_WRITE_DMA48 ||
645 	    cmd == ATA_WRITE_DMA_FUA48 ||
646 	    cmd == ATA_WRITE_DMA_QUEUED48 ||
647 	    cmd == ATA_WRITE_DMA_QUEUED_FUA48 ||
648 	    cmd == ATA_WRITE_STREAM_DMA48 ||
649 	    cmd == ATA_DATA_SET_MANAGEMENT ||
650 	    cmd == ATA_READ_LOG_DMA_EXT ||
651 	    cmd == ATA_WRITE_LOG_DMA_EXT)
652 		ataio->cmd.flags |= CAM_ATAIO_DMA;
653 	ataio->cmd.command = cmd;
654 	ataio->cmd.features = features;
655 	ataio->cmd.lba_low = lba;
656 	ataio->cmd.lba_mid = lba >> 8;
657 	ataio->cmd.lba_high = lba >> 16;
658 	ataio->cmd.device = ATA_DEV_LBA;
659 	ataio->cmd.lba_low_exp = lba >> 24;
660 	ataio->cmd.lba_mid_exp = lba >> 32;
661 	ataio->cmd.lba_high_exp = lba >> 40;
662 	ataio->cmd.features_exp = features >> 8;
663 	ataio->cmd.sector_count = sector_count;
664 	ataio->cmd.sector_count_exp = sector_count >> 8;
665 	ataio->cmd.control = 0;
666 }
667 
668 void
669 ata_ncq_cmd(struct ccb_ataio *ataio, uint8_t cmd,
670     uint64_t lba, uint16_t sector_count)
671 {
672 
673 	ataio->cmd.flags = CAM_ATAIO_48BIT | CAM_ATAIO_FPDMA;
674 	ataio->cmd.command = cmd;
675 	ataio->cmd.features = sector_count;
676 	ataio->cmd.lba_low = lba;
677 	ataio->cmd.lba_mid = lba >> 8;
678 	ataio->cmd.lba_high = lba >> 16;
679 	ataio->cmd.device = ATA_DEV_LBA;
680 	ataio->cmd.lba_low_exp = lba >> 24;
681 	ataio->cmd.lba_mid_exp = lba >> 32;
682 	ataio->cmd.lba_high_exp = lba >> 40;
683 	ataio->cmd.features_exp = sector_count >> 8;
684 	ataio->cmd.sector_count = 0;
685 	ataio->cmd.sector_count_exp = 0;
686 	ataio->cmd.control = 0;
687 }
688 
689 void
690 ata_reset_cmd(struct ccb_ataio *ataio)
691 {
692 	bzero(&ataio->cmd, sizeof(ataio->cmd));
693 	ataio->cmd.flags = CAM_ATAIO_CONTROL | CAM_ATAIO_NEEDRESULT;
694 	ataio->cmd.control = 0x04;
695 }
696 
697 void
698 ata_pm_read_cmd(struct ccb_ataio *ataio, int reg, int port)
699 {
700 	bzero(&ataio->cmd, sizeof(ataio->cmd));
701 	ataio->cmd.flags = CAM_ATAIO_NEEDRESULT;
702 	ataio->cmd.command = ATA_READ_PM;
703 	ataio->cmd.features = reg;
704 	ataio->cmd.device = port & 0x0f;
705 }
706 
707 void
708 ata_pm_write_cmd(struct ccb_ataio *ataio, int reg, int port, uint32_t val)
709 {
710 	bzero(&ataio->cmd, sizeof(ataio->cmd));
711 	ataio->cmd.flags = 0;
712 	ataio->cmd.command = ATA_WRITE_PM;
713 	ataio->cmd.features = reg;
714 	ataio->cmd.sector_count = val;
715 	ataio->cmd.lba_low = val >> 8;
716 	ataio->cmd.lba_mid = val >> 16;
717 	ataio->cmd.lba_high = val >> 24;
718 	ataio->cmd.device = port & 0x0f;
719 }
720 
721 void
722 ata_read_log(struct ccb_ataio *ataio, uint32_t retries,
723 	     void (*cbfcnp)(struct cam_periph *, union ccb *),
724 	     uint32_t log_address, uint32_t page_number, uint16_t block_count,
725 	     uint32_t protocol, uint8_t *data_ptr, uint32_t dxfer_len,
726 	     uint32_t timeout)
727 {
728 	uint64_t lba;
729 
730 	cam_fill_ataio(ataio,
731 	    /*retries*/ 1,
732 	    /*cbfcnp*/ cbfcnp,
733 	    /*flags*/ CAM_DIR_IN,
734 	    /*tag_action*/ 0,
735 	    /*data_ptr*/ data_ptr,
736 	    /*dxfer_len*/ dxfer_len,
737 	    /*timeout*/ timeout);
738 
739 	lba = (((uint64_t)page_number & 0xff00) << 32) |
740 	      ((page_number & 0x00ff) << 8) |
741 	      (log_address & 0xff);
742 
743 	ata_48bit_cmd(ataio,
744 	    /*cmd*/ (protocol & CAM_ATAIO_DMA) ? ATA_READ_LOG_DMA_EXT :
745 		     ATA_READ_LOG_EXT,
746 	    /*features*/ 0,
747 	    /*lba*/ lba,
748 	    /*sector_count*/ block_count);
749 }
750 
751 void
752 ata_bswap(int8_t *buf, int len)
753 {
754 	u_int16_t *ptr = (u_int16_t*)(buf + len);
755 
756 	while (--ptr >= (u_int16_t*)buf)
757 		*ptr = be16toh(*ptr);
758 }
759 
760 void
761 ata_btrim(int8_t *buf, int len)
762 {
763 	int8_t *ptr;
764 
765 	for (ptr = buf; ptr < buf+len; ++ptr)
766 		if (!*ptr || *ptr == '_')
767 			*ptr = ' ';
768 	for (ptr = buf + len - 1; ptr >= buf && *ptr == ' '; --ptr)
769 		*ptr = 0;
770 }
771 
772 void
773 ata_bpack(int8_t *src, int8_t *dst, int len)
774 {
775 	int i, j, blank;
776 
777 	for (i = j = blank = 0 ; i < len; i++) {
778 		if (blank && src[i] == ' ') continue;
779 		if (blank && src[i] != ' ') {
780 			dst[j++] = src[i];
781 			blank = 0;
782 			continue;
783 		}
784 		if (src[i] == ' ') {
785 			blank = 1;
786 			if (i == 0)
787 			continue;
788 		}
789 		dst[j++] = src[i];
790 	}
791 	while (j < len)
792 		dst[j++] = 0x00;
793 }
794 
795 int
796 ata_max_pmode(struct ata_params *ap)
797 {
798     if (ap->atavalid & ATA_FLAG_64_70) {
799 	if (ap->apiomodes & 0x02)
800 	    return ATA_PIO4;
801 	if (ap->apiomodes & 0x01)
802 	    return ATA_PIO3;
803     }
804     if (ap->mwdmamodes & 0x04)
805 	return ATA_PIO4;
806     if (ap->mwdmamodes & 0x02)
807 	return ATA_PIO3;
808     if (ap->mwdmamodes & 0x01)
809 	return ATA_PIO2;
810     if ((ap->retired_piomode & ATA_RETIRED_PIO_MASK) == 0x200)
811 	return ATA_PIO2;
812     if ((ap->retired_piomode & ATA_RETIRED_PIO_MASK) == 0x100)
813 	return ATA_PIO1;
814     if ((ap->retired_piomode & ATA_RETIRED_PIO_MASK) == 0x000)
815 	return ATA_PIO0;
816     return ATA_PIO0;
817 }
818 
819 int
820 ata_max_wmode(struct ata_params *ap)
821 {
822     if (ap->mwdmamodes & 0x04)
823 	return ATA_WDMA2;
824     if (ap->mwdmamodes & 0x02)
825 	return ATA_WDMA1;
826     if (ap->mwdmamodes & 0x01)
827 	return ATA_WDMA0;
828     return -1;
829 }
830 
831 int
832 ata_max_umode(struct ata_params *ap)
833 {
834     if (ap->atavalid & ATA_FLAG_88) {
835 	if (ap->udmamodes & 0x40)
836 	    return ATA_UDMA6;
837 	if (ap->udmamodes & 0x20)
838 	    return ATA_UDMA5;
839 	if (ap->udmamodes & 0x10)
840 	    return ATA_UDMA4;
841 	if (ap->udmamodes & 0x08)
842 	    return ATA_UDMA3;
843 	if (ap->udmamodes & 0x04)
844 	    return ATA_UDMA2;
845 	if (ap->udmamodes & 0x02)
846 	    return ATA_UDMA1;
847 	if (ap->udmamodes & 0x01)
848 	    return ATA_UDMA0;
849     }
850     return -1;
851 }
852 
853 int
854 ata_max_mode(struct ata_params *ap, int maxmode)
855 {
856 
857 	if (maxmode == 0)
858 		maxmode = ATA_DMA_MAX;
859 	if (maxmode >= ATA_UDMA0 && ata_max_umode(ap) > 0)
860 		return (min(maxmode, ata_max_umode(ap)));
861 	if (maxmode >= ATA_WDMA0 && ata_max_wmode(ap) > 0)
862 		return (min(maxmode, ata_max_wmode(ap)));
863 	return (min(maxmode, ata_max_pmode(ap)));
864 }
865 
866 char *
867 ata_mode2string(int mode)
868 {
869     switch (mode) {
870     case -1: return "UNSUPPORTED";
871     case 0: return "NONE";
872     case ATA_PIO0: return "PIO0";
873     case ATA_PIO1: return "PIO1";
874     case ATA_PIO2: return "PIO2";
875     case ATA_PIO3: return "PIO3";
876     case ATA_PIO4: return "PIO4";
877     case ATA_WDMA0: return "WDMA0";
878     case ATA_WDMA1: return "WDMA1";
879     case ATA_WDMA2: return "WDMA2";
880     case ATA_UDMA0: return "UDMA0";
881     case ATA_UDMA1: return "UDMA1";
882     case ATA_UDMA2: return "UDMA2";
883     case ATA_UDMA3: return "UDMA3";
884     case ATA_UDMA4: return "UDMA4";
885     case ATA_UDMA5: return "UDMA5";
886     case ATA_UDMA6: return "UDMA6";
887     default:
888 	if (mode & ATA_DMA_MASK)
889 	    return "BIOSDMA";
890 	else
891 	    return "BIOSPIO";
892     }
893 }
894 
895 int
896 ata_string2mode(char *str)
897 {
898 	if (!strcasecmp(str, "PIO0")) return (ATA_PIO0);
899 	if (!strcasecmp(str, "PIO1")) return (ATA_PIO1);
900 	if (!strcasecmp(str, "PIO2")) return (ATA_PIO2);
901 	if (!strcasecmp(str, "PIO3")) return (ATA_PIO3);
902 	if (!strcasecmp(str, "PIO4")) return (ATA_PIO4);
903 	if (!strcasecmp(str, "WDMA0")) return (ATA_WDMA0);
904 	if (!strcasecmp(str, "WDMA1")) return (ATA_WDMA1);
905 	if (!strcasecmp(str, "WDMA2")) return (ATA_WDMA2);
906 	if (!strcasecmp(str, "UDMA0")) return (ATA_UDMA0);
907 	if (!strcasecmp(str, "UDMA16")) return (ATA_UDMA0);
908 	if (!strcasecmp(str, "UDMA1")) return (ATA_UDMA1);
909 	if (!strcasecmp(str, "UDMA25")) return (ATA_UDMA1);
910 	if (!strcasecmp(str, "UDMA2")) return (ATA_UDMA2);
911 	if (!strcasecmp(str, "UDMA33")) return (ATA_UDMA2);
912 	if (!strcasecmp(str, "UDMA3")) return (ATA_UDMA3);
913 	if (!strcasecmp(str, "UDMA44")) return (ATA_UDMA3);
914 	if (!strcasecmp(str, "UDMA4")) return (ATA_UDMA4);
915 	if (!strcasecmp(str, "UDMA66")) return (ATA_UDMA4);
916 	if (!strcasecmp(str, "UDMA5")) return (ATA_UDMA5);
917 	if (!strcasecmp(str, "UDMA100")) return (ATA_UDMA5);
918 	if (!strcasecmp(str, "UDMA6")) return (ATA_UDMA6);
919 	if (!strcasecmp(str, "UDMA133")) return (ATA_UDMA6);
920 	return (-1);
921 }
922 
923 
924 u_int
925 ata_mode2speed(int mode)
926 {
927 	switch (mode) {
928 	case ATA_PIO0:
929 	default:
930 		return (3300);
931 	case ATA_PIO1:
932 		return (5200);
933 	case ATA_PIO2:
934 		return (8300);
935 	case ATA_PIO3:
936 		return (11100);
937 	case ATA_PIO4:
938 		return (16700);
939 	case ATA_WDMA0:
940 		return (4200);
941 	case ATA_WDMA1:
942 		return (13300);
943 	case ATA_WDMA2:
944 		return (16700);
945 	case ATA_UDMA0:
946 		return (16700);
947 	case ATA_UDMA1:
948 		return (25000);
949 	case ATA_UDMA2:
950 		return (33300);
951 	case ATA_UDMA3:
952 		return (44400);
953 	case ATA_UDMA4:
954 		return (66700);
955 	case ATA_UDMA5:
956 		return (100000);
957 	case ATA_UDMA6:
958 		return (133000);
959 	}
960 }
961 
962 u_int
963 ata_revision2speed(int revision)
964 {
965 	switch (revision) {
966 	case 1:
967 	default:
968 		return (150000);
969 	case 2:
970 		return (300000);
971 	case 3:
972 		return (600000);
973 	}
974 }
975 
976 int
977 ata_speed2revision(u_int speed)
978 {
979 	switch (speed) {
980 	case 0:
981 		return (0);
982 	case 150000:
983 		return (1);
984 	case 300000:
985 		return (2);
986 	case 600000:
987 		return (3);
988 	default:
989 		return (-1);
990 	}
991 }
992 
993 int
994 ata_identify_match(caddr_t identbuffer, caddr_t table_entry)
995 {
996 	struct scsi_inquiry_pattern *entry;
997 	struct ata_params *ident;
998 
999 	entry = (struct scsi_inquiry_pattern *)table_entry;
1000 	ident = (struct ata_params *)identbuffer;
1001 
1002 	if ((cam_strmatch(ident->model, entry->product,
1003 			  sizeof(ident->model)) == 0)
1004 	 && (cam_strmatch(ident->revision, entry->revision,
1005 			  sizeof(ident->revision)) == 0)) {
1006 		return (0);
1007 	}
1008         return (-1);
1009 }
1010 
1011 int
1012 ata_static_identify_match(caddr_t identbuffer, caddr_t table_entry)
1013 {
1014 	struct scsi_static_inquiry_pattern *entry;
1015 	struct ata_params *ident;
1016 
1017 	entry = (struct scsi_static_inquiry_pattern *)table_entry;
1018 	ident = (struct ata_params *)identbuffer;
1019 
1020 	if ((cam_strmatch(ident->model, entry->product,
1021 			  sizeof(ident->model)) == 0)
1022 	 && (cam_strmatch(ident->revision, entry->revision,
1023 			  sizeof(ident->revision)) == 0)) {
1024 		return (0);
1025 	}
1026         return (-1);
1027 }
1028 
1029 void
1030 semb_receive_diagnostic_results(struct ccb_ataio *ataio,
1031     u_int32_t retries, void (*cbfcnp)(struct cam_periph *, union ccb*),
1032     uint8_t tag_action, int pcv, uint8_t page_code,
1033     uint8_t *data_ptr, uint16_t length, uint32_t timeout)
1034 {
1035 
1036 	length = min(length, 1020);
1037 	length = (length + 3) & ~3;
1038 	cam_fill_ataio(ataio,
1039 		      retries,
1040 		      cbfcnp,
1041 		      /*flags*/CAM_DIR_IN,
1042 		      tag_action,
1043 		      data_ptr,
1044 		      length,
1045 		      timeout);
1046 	ata_28bit_cmd(ataio, ATA_SEP_ATTN,
1047 	    pcv ? page_code : 0, 0x02, length / 4);
1048 }
1049 
1050 void
1051 semb_send_diagnostic(struct ccb_ataio *ataio,
1052     u_int32_t retries, void (*cbfcnp)(struct cam_periph *, union ccb *),
1053     uint8_t tag_action, uint8_t *data_ptr, uint16_t length, uint32_t timeout)
1054 {
1055 
1056 	length = min(length, 1020);
1057 	length = (length + 3) & ~3;
1058 	cam_fill_ataio(ataio,
1059 		      retries,
1060 		      cbfcnp,
1061 		      /*flags*/length ? CAM_DIR_OUT : CAM_DIR_NONE,
1062 		      tag_action,
1063 		      data_ptr,
1064 		      length,
1065 		      timeout);
1066 	ata_28bit_cmd(ataio, ATA_SEP_ATTN,
1067 	    length > 0 ? data_ptr[0] : 0, 0x82, length / 4);
1068 }
1069 
1070 void
1071 semb_read_buffer(struct ccb_ataio *ataio,
1072     u_int32_t retries, void (*cbfcnp)(struct cam_periph *, union ccb*),
1073     uint8_t tag_action, uint8_t page_code,
1074     uint8_t *data_ptr, uint16_t length, uint32_t timeout)
1075 {
1076 
1077 	length = min(length, 1020);
1078 	length = (length + 3) & ~3;
1079 	cam_fill_ataio(ataio,
1080 		      retries,
1081 		      cbfcnp,
1082 		      /*flags*/CAM_DIR_IN,
1083 		      tag_action,
1084 		      data_ptr,
1085 		      length,
1086 		      timeout);
1087 	ata_28bit_cmd(ataio, ATA_SEP_ATTN,
1088 	    page_code, 0x00, length / 4);
1089 }
1090 
1091 void
1092 semb_write_buffer(struct ccb_ataio *ataio,
1093     u_int32_t retries, void (*cbfcnp)(struct cam_periph *, union ccb *),
1094     uint8_t tag_action, uint8_t *data_ptr, uint16_t length, uint32_t timeout)
1095 {
1096 
1097 	length = min(length, 1020);
1098 	length = (length + 3) & ~3;
1099 	cam_fill_ataio(ataio,
1100 		      retries,
1101 		      cbfcnp,
1102 		      /*flags*/length ? CAM_DIR_OUT : CAM_DIR_NONE,
1103 		      tag_action,
1104 		      data_ptr,
1105 		      length,
1106 		      timeout);
1107 	ata_28bit_cmd(ataio, ATA_SEP_ATTN,
1108 	    length > 0 ? data_ptr[0] : 0, 0x80, length / 4);
1109 }
1110 
1111 
1112 void
1113 ata_zac_mgmt_out(struct ccb_ataio *ataio, uint32_t retries,
1114 		 void (*cbfcnp)(struct cam_periph *, union ccb *),
1115 		 int use_ncq, uint8_t zm_action, uint64_t zone_id,
1116 		 uint8_t zone_flags, uint16_t sector_count, uint8_t *data_ptr,
1117 		 uint32_t dxfer_len, uint32_t timeout)
1118 {
1119 	uint8_t command_out, ata_flags;
1120 	uint16_t features_out, sectors_out;
1121 	uint32_t auxiliary;
1122 
1123 	if (use_ncq == 0) {
1124 		command_out = ATA_ZAC_MANAGEMENT_OUT;
1125 		features_out = (zm_action & 0xf) | (zone_flags << 8);
1126 		if (dxfer_len == 0) {
1127 			ata_flags = 0;
1128 			sectors_out = 0;
1129 		} else {
1130 			ata_flags = CAM_ATAIO_DMA;
1131 			/* XXX KDM use sector count? */
1132 			sectors_out = ((dxfer_len >> 9) & 0xffff);
1133 		}
1134 		auxiliary = 0;
1135 	} else {
1136 		if (dxfer_len == 0) {
1137 			command_out = ATA_NCQ_NON_DATA;
1138 			features_out = ATA_NCQ_ZAC_MGMT_OUT;
1139 			sectors_out = 0;
1140 		} else {
1141 			command_out = ATA_SEND_FPDMA_QUEUED;
1142 
1143 			/* Note that we're defaulting to normal priority */
1144 			sectors_out = ATA_SFPDMA_ZAC_MGMT_OUT << 8;
1145 
1146 			/*
1147 			 * For SEND FPDMA QUEUED, the transfer length is
1148 			 * encoded in the FEATURE register, and 0 means
1149 			 * that 65536 512 byte blocks are to be tranferred.
1150 			 * In practice, it seems unlikely that we'll see
1151 			 * a transfer that large.
1152 			 */
1153 			if (dxfer_len == (65536 * 512)) {
1154 				features_out = 0;
1155 			} else {
1156 				/*
1157 				 * Yes, the caller can theoretically send a
1158 				 * transfer larger than we can handle.
1159 				 * Anyone using this function needs enough
1160 				 * knowledge to avoid doing that.
1161 				 */
1162 				features_out = ((dxfer_len >> 9) & 0xffff);
1163 			}
1164 		}
1165 		auxiliary = (zm_action & 0xf) | (zone_flags << 8);
1166 
1167 		ata_flags = CAM_ATAIO_FPDMA;
1168 	}
1169 
1170 	cam_fill_ataio(ataio,
1171 	    /*retries*/ retries,
1172 	    /*cbfcnp*/ cbfcnp,
1173 	    /*flags*/ (dxfer_len > 0) ? CAM_DIR_OUT : CAM_DIR_NONE,
1174 	    /*tag_action*/ 0,
1175 	    /*data_ptr*/ data_ptr,
1176 	    /*dxfer_len*/ dxfer_len,
1177 	    /*timeout*/ timeout);
1178 
1179 	ata_48bit_cmd(ataio,
1180 	    /*cmd*/ command_out,
1181 	    /*features*/ features_out,
1182 	    /*lba*/ zone_id,
1183 	    /*sector_count*/ sectors_out);
1184 
1185 	ataio->cmd.flags |= ata_flags;
1186 	if (auxiliary != 0) {
1187 		ataio->ata_flags |= ATA_FLAG_AUX;
1188 		ataio->aux = auxiliary;
1189 	}
1190 }
1191 
1192 void
1193 ata_zac_mgmt_in(struct ccb_ataio *ataio, uint32_t retries,
1194 		void (*cbfcnp)(struct cam_periph *, union ccb *),
1195 		int use_ncq, uint8_t zm_action, uint64_t zone_id,
1196 		uint8_t zone_flags, uint8_t *data_ptr, uint32_t dxfer_len,
1197 		uint32_t timeout)
1198 {
1199 	uint8_t command_out, ata_flags;
1200 	uint16_t features_out, sectors_out;
1201 	uint32_t auxiliary;
1202 
1203 	if (use_ncq == 0) {
1204 		command_out = ATA_ZAC_MANAGEMENT_IN;
1205 		/* XXX KDM put a macro here */
1206 		features_out = (zm_action & 0xf) | (zone_flags << 8);
1207 		ata_flags = CAM_ATAIO_DMA;
1208 		sectors_out = ((dxfer_len >> 9) & 0xffff);
1209 		auxiliary = 0;
1210 	} else {
1211 		command_out = ATA_RECV_FPDMA_QUEUED;
1212 		sectors_out = ATA_RFPDMA_ZAC_MGMT_IN << 8;
1213 		auxiliary = (zm_action & 0xf) | (zone_flags << 8);
1214 		ata_flags = CAM_ATAIO_FPDMA;
1215 		/*
1216 		 * For RECEIVE FPDMA QUEUED, the transfer length is
1217 		 * encoded in the FEATURE register, and 0 means
1218 		 * that 65536 512 byte blocks are to be tranferred.
1219 		 * In practice, it is unlikely we will see a transfer that
1220 		 * large.
1221 		 */
1222 		if (dxfer_len == (65536 * 512)) {
1223 			features_out = 0;
1224 		} else {
1225 			/*
1226 			 * Yes, the caller can theoretically request a
1227 			 * transfer larger than we can handle.
1228 			 * Anyone using this function needs enough
1229 			 * knowledge to avoid doing that.
1230 			 */
1231 			features_out = ((dxfer_len >> 9) & 0xffff);
1232 		}
1233 	}
1234 
1235 	cam_fill_ataio(ataio,
1236 	    /*retries*/ retries,
1237 	    /*cbfcnp*/ cbfcnp,
1238 	    /*flags*/ CAM_DIR_IN,
1239 	    /*tag_action*/ 0,
1240 	    /*data_ptr*/ data_ptr,
1241 	    /*dxfer_len*/ dxfer_len,
1242 	    /*timeout*/ timeout);
1243 
1244 	ata_48bit_cmd(ataio,
1245 	    /*cmd*/ command_out,
1246 	    /*features*/ features_out,
1247 	    /*lba*/ zone_id,
1248 	    /*sector_count*/ sectors_out);
1249 
1250 	ataio->cmd.flags |= ata_flags;
1251 	if (auxiliary != 0) {
1252 		ataio->ata_flags |= ATA_FLAG_AUX;
1253 		ataio->aux = auxiliary;
1254 	}
1255 }
1256 
1257 void
1258 ata_param_fixup(struct ata_params *ident_buf)
1259 {
1260 	int16_t *ptr;
1261 
1262 	for (ptr = (int16_t *)ident_buf;
1263 	     ptr < (int16_t *)ident_buf + sizeof(struct ata_params)/2; ptr++) {
1264 		*ptr = le16toh(*ptr);
1265 	}
1266 	if (strncmp(ident_buf->model, "FX", 2) &&
1267 	    strncmp(ident_buf->model, "NEC", 3) &&
1268 	    strncmp(ident_buf->model, "Pioneer", 7) &&
1269 	    strncmp(ident_buf->model, "SHARP", 5)) {
1270 		ata_bswap(ident_buf->model, sizeof(ident_buf->model));
1271 		ata_bswap(ident_buf->revision, sizeof(ident_buf->revision));
1272 		ata_bswap(ident_buf->serial, sizeof(ident_buf->serial));
1273 	}
1274 	ata_btrim(ident_buf->model, sizeof(ident_buf->model));
1275 	ata_bpack(ident_buf->model, ident_buf->model, sizeof(ident_buf->model));
1276 	ata_btrim(ident_buf->revision, sizeof(ident_buf->revision));
1277 	ata_bpack(ident_buf->revision, ident_buf->revision, sizeof(ident_buf->revision));
1278 	ata_btrim(ident_buf->serial, sizeof(ident_buf->serial));
1279 	ata_bpack(ident_buf->serial, ident_buf->serial, sizeof(ident_buf->serial));
1280 }
1281