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