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