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