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