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