xref: /freebsd/sys/cam/ata/ata_all.c (revision 7e00348e7605b9906601438008341ffc37c00e2c)
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: return ("NOP");
79 	case 0x03: return ("CFA_REQUEST_EXTENDED_ERROR");
80 	case 0x06:
81 		switch (cmd->features) {
82 	        case 0x01: return ("DSM TRIM");
83 	        }
84 	        return "DSM";
85 	case 0x08: return ("DEVICE_RESET");
86 	case 0x20: return ("READ");
87 	case 0x24: return ("READ48");
88 	case 0x25: return ("READ_DMA48");
89 	case 0x26: return ("READ_DMA_QUEUED48");
90 	case 0x27: return ("READ_NATIVE_MAX_ADDRESS48");
91 	case 0x29: return ("READ_MUL48");
92 	case 0x2a: return ("READ_STREAM_DMA48");
93 	case 0x2b: return ("READ_STREAM48");
94 	case 0x2f: return ("READ_LOG_EXT");
95 	case 0x30: return ("WRITE");
96 	case 0x34: return ("WRITE48");
97 	case 0x35: return ("WRITE_DMA48");
98 	case 0x36: return ("WRITE_DMA_QUEUED48");
99 	case 0x37: return ("SET_MAX_ADDRESS48");
100 	case 0x39: return ("WRITE_MUL48");
101 	case 0x3a: return ("WRITE_STREAM_DMA48");
102 	case 0x3b: return ("WRITE_STREAM48");
103 	case 0x3d: return ("WRITE_DMA_FUA48");
104 	case 0x3e: return ("WRITE_DMA_QUEUED_FUA48");
105 	case 0x3f: return ("WRITE_LOG_EXT");
106 	case 0x40: return ("READ_VERIFY");
107 	case 0x42: return ("READ_VERIFY48");
108 	case 0x51: return ("CONFIGURE_STREAM");
109 	case 0x60: return ("READ_FPDMA_QUEUED");
110 	case 0x61: return ("WRITE_FPDMA_QUEUED");
111 	case 0x63: return ("NCQ_NON_DATA");
112 	case 0x64: return ("SEND_FPDMA_QUEUED");
113 	case 0x65: return ("RECEIVE_FPDMA_QUEUED");
114 	case 0x67:
115 		if (cmd->features == 0xec)
116 			return ("SEP_ATTN IDENTIFY");
117 		switch (cmd->lba_low) {
118 		case 0x00: return ("SEP_ATTN READ BUFFER");
119 		case 0x02: return ("SEP_ATTN RECEIVE DIAGNOSTIC RESULTS");
120 		case 0x80: return ("SEP_ATTN WRITE BUFFER");
121 		case 0x82: return ("SEP_ATTN SEND DIAGNOSTIC");
122 		}
123 		return ("SEP_ATTN");
124 	case 0x70: return ("SEEK");
125 	case 0x87: return ("CFA_TRANSLATE_SECTOR");
126 	case 0x90: return ("EXECUTE_DEVICE_DIAGNOSTIC");
127 	case 0x92: return ("DOWNLOAD_MICROCODE");
128 	case 0xa0: return ("PACKET");
129 	case 0xa1: return ("ATAPI_IDENTIFY");
130 	case 0xa2: return ("SERVICE");
131 	case 0xb0: return ("SMART");
132 	case 0xb1: return ("DEVICE CONFIGURATION");
133 	case 0xc0: return ("CFA_ERASE");
134 	case 0xc4: return ("READ_MUL");
135 	case 0xc5: return ("WRITE_MUL");
136 	case 0xc6: return ("SET_MULTI");
137 	case 0xc7: return ("READ_DMA_QUEUED");
138 	case 0xc8: return ("READ_DMA");
139 	case 0xca: return ("WRITE_DMA");
140 	case 0xcc: return ("WRITE_DMA_QUEUED");
141 	case 0xcd: return ("CFA_WRITE_MULTIPLE_WITHOUT_ERASE");
142 	case 0xce: return ("WRITE_MUL_FUA48");
143 	case 0xd1: return ("CHECK_MEDIA_CARD_TYPE");
144 	case 0xda: return ("GET_MEDIA_STATUS");
145 	case 0xde: return ("MEDIA_LOCK");
146 	case 0xdf: return ("MEDIA_UNLOCK");
147 	case 0xe0: return ("STANDBY_IMMEDIATE");
148 	case 0xe1: return ("IDLE_IMMEDIATE");
149 	case 0xe2: return ("STANDBY");
150 	case 0xe3: return ("IDLE");
151 	case 0xe4: return ("READ_BUFFER/PM");
152 	case 0xe5: return ("CHECK_POWER_MODE");
153 	case 0xe6: return ("SLEEP");
154 	case 0xe7: return ("FLUSHCACHE");
155 	case 0xe8: return ("WRITE_PM");
156 	case 0xea: return ("FLUSHCACHE48");
157 	case 0xec: return ("ATA_IDENTIFY");
158 	case 0xed: return ("MEDIA_EJECT");
159 	case 0xef:
160 		switch (cmd->features) {
161 	        case 0x03: return ("SETFEATURES SET TRANSFER MODE");
162 	        case 0x02: return ("SETFEATURES ENABLE WCACHE");
163 	        case 0x82: return ("SETFEATURES DISABLE WCACHE");
164 	        case 0x06: return ("SETFEATURES ENABLE PUIS");
165 	        case 0x86: return ("SETFEATURES DISABLE PUIS");
166 	        case 0x07: return ("SETFEATURES SPIN-UP");
167 	        case 0x10: return ("SETFEATURES ENABLE SATA FEATURE");
168 	        case 0x90: return ("SETFEATURES DISABLE SATA FEATURE");
169 	        case 0xaa: return ("SETFEATURES ENABLE RCACHE");
170 	        case 0x55: return ("SETFEATURES DISABLE RCACHE");
171 	        }
172 	        return "SETFEATURES";
173 	case 0xf1: return ("SECURITY_SET_PASSWORD");
174 	case 0xf2: return ("SECURITY_UNLOCK");
175 	case 0xf3: return ("SECURITY_ERASE_PREPARE");
176 	case 0xf4: return ("SECURITY_ERASE_UNIT");
177 	case 0xf5: return ("SECURITY_FREEZE_LOCK");
178 	case 0xf6: return ("SECURITY_DISABLE_PASSWORD");
179 	case 0xf8: return ("READ_NATIVE_MAX_ADDRESS");
180 	case 0xf9: return ("SET_MAX_ADDRESS");
181 	}
182 	return "UNKNOWN";
183 }
184 
185 char *
186 ata_cmd_string(struct ata_cmd *cmd, char *cmd_string, size_t len)
187 {
188 
189 	snprintf(cmd_string, len, "%02x %02x %02x %02x "
190 	    "%02x %02x %02x %02x %02x %02x %02x %02x",
191 	    cmd->command, cmd->features,
192 	    cmd->lba_low, cmd->lba_mid, cmd->lba_high, cmd->device,
193 	    cmd->lba_low_exp, cmd->lba_mid_exp, cmd->lba_high_exp,
194 	    cmd->features_exp, cmd->sector_count, cmd->sector_count_exp);
195 
196 	return(cmd_string);
197 }
198 
199 char *
200 ata_res_string(struct ata_res *res, char *res_string, size_t len)
201 {
202 
203 	snprintf(res_string, len, "%02x %02x %02x %02x "
204 	    "%02x %02x %02x %02x %02x %02x %02x",
205 	    res->status, res->error,
206 	    res->lba_low, res->lba_mid, res->lba_high, res->device,
207 	    res->lba_low_exp, res->lba_mid_exp, res->lba_high_exp,
208 	    res->sector_count, res->sector_count_exp);
209 
210 	return(res_string);
211 }
212 
213 /*
214  * ata_command_sbuf() returns 0 for success and -1 for failure.
215  */
216 int
217 ata_command_sbuf(struct ccb_ataio *ataio, struct sbuf *sb)
218 {
219 	char cmd_str[(12 * 3) + 1];
220 
221 	sbuf_printf(sb, "%s. ACB: %s",
222 	    ata_op_string(&ataio->cmd),
223 	    ata_cmd_string(&ataio->cmd, cmd_str, sizeof(cmd_str)));
224 
225 	return(0);
226 }
227 
228 /*
229  * ata_status_abuf() returns 0 for success and -1 for failure.
230  */
231 int
232 ata_status_sbuf(struct ccb_ataio *ataio, struct sbuf *sb)
233 {
234 
235 	sbuf_printf(sb, "ATA status: %02x (%s%s%s%s%s%s%s%s)",
236 	    ataio->res.status,
237 	    (ataio->res.status & 0x80) ? "BSY " : "",
238 	    (ataio->res.status & 0x40) ? "DRDY " : "",
239 	    (ataio->res.status & 0x20) ? "DF " : "",
240 	    (ataio->res.status & 0x10) ? "SERV " : "",
241 	    (ataio->res.status & 0x08) ? "DRQ " : "",
242 	    (ataio->res.status & 0x04) ? "CORR " : "",
243 	    (ataio->res.status & 0x02) ? "IDX " : "",
244 	    (ataio->res.status & 0x01) ? "ERR" : "");
245 	if (ataio->res.status & 1) {
246 	    sbuf_printf(sb, ", error: %02x (%s%s%s%s%s%s%s%s)",
247 		ataio->res.error,
248 		(ataio->res.error & 0x80) ? "ICRC " : "",
249 		(ataio->res.error & 0x40) ? "UNC " : "",
250 		(ataio->res.error & 0x20) ? "MC " : "",
251 		(ataio->res.error & 0x10) ? "IDNF " : "",
252 		(ataio->res.error & 0x08) ? "MCR " : "",
253 		(ataio->res.error & 0x04) ? "ABRT " : "",
254 		(ataio->res.error & 0x02) ? "NM " : "",
255 		(ataio->res.error & 0x01) ? "ILI" : "");
256 	}
257 
258 	return(0);
259 }
260 
261 /*
262  * ata_res_sbuf() returns 0 for success and -1 for failure.
263  */
264 int
265 ata_res_sbuf(struct ccb_ataio *ataio, struct sbuf *sb)
266 {
267 	char res_str[(11 * 3) + 1];
268 
269 	sbuf_printf(sb, "RES: %s",
270 	    ata_res_string(&ataio->res, res_str, sizeof(res_str)));
271 
272 	return(0);
273 }
274 
275 void
276 ata_print_ident(struct ata_params *ident_data)
277 {
278 	char product[48], revision[16];
279 
280 	cam_strvis(product, ident_data->model, sizeof(ident_data->model),
281 		   sizeof(product));
282 	cam_strvis(revision, ident_data->revision, sizeof(ident_data->revision),
283 		   sizeof(revision));
284 	printf("<%s %s> %s-%d",
285 	    product, revision,
286 	    (ident_data->config == ATA_PROTO_CFA) ? "CFA" :
287 	    (ident_data->config & ATA_PROTO_ATAPI) ? "ATAPI" : "ATA",
288 	    ata_version(ident_data->version_major));
289 	if (ident_data->satacapabilities && ident_data->satacapabilities != 0xffff) {
290 		if (ident_data->satacapabilities & ATA_SATA_GEN3)
291 			printf(" SATA 3.x");
292 		else if (ident_data->satacapabilities & ATA_SATA_GEN2)
293 			printf(" SATA 2.x");
294 		else if (ident_data->satacapabilities & ATA_SATA_GEN1)
295 			printf(" SATA 1.x");
296 		else
297 			printf(" SATA");
298 	}
299 	printf(" device\n");
300 }
301 
302 void
303 ata_print_ident_short(struct ata_params *ident_data)
304 {
305 	char product[48], revision[16];
306 
307 	cam_strvis(product, ident_data->model, sizeof(ident_data->model),
308 		   sizeof(product));
309 	cam_strvis(revision, ident_data->revision, sizeof(ident_data->revision),
310 		   sizeof(revision));
311 	printf("<%s %s>", product, revision);
312 }
313 
314 void
315 semb_print_ident(struct sep_identify_data *ident_data)
316 {
317 	char vendor[9], product[17], revision[5], fw[5], in[7], ins[5];
318 
319 	cam_strvis(vendor, ident_data->vendor_id, 8, sizeof(vendor));
320 	cam_strvis(product, ident_data->product_id, 16, sizeof(product));
321 	cam_strvis(revision, ident_data->product_rev, 4, sizeof(revision));
322 	cam_strvis(fw, ident_data->firmware_rev, 4, sizeof(fw));
323 	cam_strvis(in, ident_data->interface_id, 6, sizeof(in));
324 	cam_strvis(ins, ident_data->interface_rev, 4, sizeof(ins));
325 	printf("<%s %s %s %s> SEMB %s %s device\n",
326 	    vendor, product, revision, fw, in, ins);
327 }
328 
329 void
330 semb_print_ident_short(struct sep_identify_data *ident_data)
331 {
332 	char vendor[9], product[17], revision[5], fw[5];
333 
334 	cam_strvis(vendor, ident_data->vendor_id, 8, sizeof(vendor));
335 	cam_strvis(product, ident_data->product_id, 16, sizeof(product));
336 	cam_strvis(revision, ident_data->product_rev, 4, sizeof(revision));
337 	cam_strvis(fw, ident_data->firmware_rev, 4, sizeof(fw));
338 	printf("<%s %s %s %s>", vendor, product, revision, fw);
339 }
340 
341 uint32_t
342 ata_logical_sector_size(struct ata_params *ident_data)
343 {
344 	if ((ident_data->pss & ATA_PSS_VALID_MASK) == ATA_PSS_VALID_VALUE &&
345 	    (ident_data->pss & ATA_PSS_LSSABOVE512)) {
346 		return (((u_int32_t)ident_data->lss_1 |
347 		    ((u_int32_t)ident_data->lss_2 << 16)) * 2);
348 	}
349 	return (512);
350 }
351 
352 uint64_t
353 ata_physical_sector_size(struct ata_params *ident_data)
354 {
355 	if ((ident_data->pss & ATA_PSS_VALID_MASK) == ATA_PSS_VALID_VALUE) {
356 		if (ident_data->pss & ATA_PSS_MULTLS) {
357 			return ((uint64_t)ata_logical_sector_size(ident_data) *
358 			    (1 << (ident_data->pss & ATA_PSS_LSPPS)));
359 		} else {
360 			return (uint64_t)ata_logical_sector_size(ident_data);
361 		}
362 	}
363 	return (512);
364 }
365 
366 uint64_t
367 ata_logical_sector_offset(struct ata_params *ident_data)
368 {
369 	if ((ident_data->lsalign & 0xc000) == 0x4000) {
370 		return ((uint64_t)ata_logical_sector_size(ident_data) *
371 		    (ident_data->lsalign & 0x3fff));
372 	}
373 	return (0);
374 }
375 
376 void
377 ata_28bit_cmd(struct ccb_ataio *ataio, uint8_t cmd, uint8_t features,
378     uint32_t lba, uint8_t sector_count)
379 {
380 	bzero(&ataio->cmd, sizeof(ataio->cmd));
381 	ataio->cmd.flags = 0;
382 	if (cmd == ATA_READ_DMA ||
383 	    cmd == ATA_READ_DMA_QUEUED ||
384 	    cmd == ATA_WRITE_DMA ||
385 	    cmd == ATA_WRITE_DMA_QUEUED)
386 		ataio->cmd.flags |= CAM_ATAIO_DMA;
387 	ataio->cmd.command = cmd;
388 	ataio->cmd.features = features;
389 	ataio->cmd.lba_low = lba;
390 	ataio->cmd.lba_mid = lba >> 8;
391 	ataio->cmd.lba_high = lba >> 16;
392 	ataio->cmd.device = ATA_DEV_LBA | ((lba >> 24) & 0x0f);
393 	ataio->cmd.sector_count = sector_count;
394 }
395 
396 void
397 ata_48bit_cmd(struct ccb_ataio *ataio, uint8_t cmd, uint16_t features,
398     uint64_t lba, uint16_t sector_count)
399 {
400 
401 	ataio->cmd.flags = CAM_ATAIO_48BIT;
402 	if (cmd == ATA_READ_DMA48 ||
403 	    cmd == ATA_READ_DMA_QUEUED48 ||
404 	    cmd == ATA_READ_STREAM_DMA48 ||
405 	    cmd == ATA_WRITE_DMA48 ||
406 	    cmd == ATA_WRITE_DMA_FUA48 ||
407 	    cmd == ATA_WRITE_DMA_QUEUED48 ||
408 	    cmd == ATA_WRITE_DMA_QUEUED_FUA48 ||
409 	    cmd == ATA_WRITE_STREAM_DMA48 ||
410 	    cmd == ATA_DATA_SET_MANAGEMENT)
411 		ataio->cmd.flags |= CAM_ATAIO_DMA;
412 	ataio->cmd.command = cmd;
413 	ataio->cmd.features = features;
414 	ataio->cmd.lba_low = lba;
415 	ataio->cmd.lba_mid = lba >> 8;
416 	ataio->cmd.lba_high = lba >> 16;
417 	ataio->cmd.device = ATA_DEV_LBA;
418 	ataio->cmd.lba_low_exp = lba >> 24;
419 	ataio->cmd.lba_mid_exp = lba >> 32;
420 	ataio->cmd.lba_high_exp = lba >> 40;
421 	ataio->cmd.features_exp = features >> 8;
422 	ataio->cmd.sector_count = sector_count;
423 	ataio->cmd.sector_count_exp = sector_count >> 8;
424 	ataio->cmd.control = 0;
425 }
426 
427 void
428 ata_ncq_cmd(struct ccb_ataio *ataio, uint8_t cmd,
429     uint64_t lba, uint16_t sector_count)
430 {
431 
432 	ataio->cmd.flags = CAM_ATAIO_48BIT | CAM_ATAIO_FPDMA;
433 	ataio->cmd.command = cmd;
434 	ataio->cmd.features = sector_count;
435 	ataio->cmd.lba_low = lba;
436 	ataio->cmd.lba_mid = lba >> 8;
437 	ataio->cmd.lba_high = lba >> 16;
438 	ataio->cmd.device = ATA_DEV_LBA;
439 	ataio->cmd.lba_low_exp = lba >> 24;
440 	ataio->cmd.lba_mid_exp = lba >> 32;
441 	ataio->cmd.lba_high_exp = lba >> 40;
442 	ataio->cmd.features_exp = sector_count >> 8;
443 	ataio->cmd.sector_count = 0;
444 	ataio->cmd.sector_count_exp = 0;
445 	ataio->cmd.control = 0;
446 }
447 
448 void
449 ata_reset_cmd(struct ccb_ataio *ataio)
450 {
451 	bzero(&ataio->cmd, sizeof(ataio->cmd));
452 	ataio->cmd.flags = CAM_ATAIO_CONTROL | CAM_ATAIO_NEEDRESULT;
453 	ataio->cmd.control = 0x04;
454 }
455 
456 void
457 ata_pm_read_cmd(struct ccb_ataio *ataio, int reg, int port)
458 {
459 	bzero(&ataio->cmd, sizeof(ataio->cmd));
460 	ataio->cmd.flags = CAM_ATAIO_NEEDRESULT;
461 	ataio->cmd.command = ATA_READ_PM;
462 	ataio->cmd.features = reg;
463 	ataio->cmd.device = port & 0x0f;
464 }
465 
466 void
467 ata_pm_write_cmd(struct ccb_ataio *ataio, int reg, int port, uint32_t val)
468 {
469 	bzero(&ataio->cmd, sizeof(ataio->cmd));
470 	ataio->cmd.flags = 0;
471 	ataio->cmd.command = ATA_WRITE_PM;
472 	ataio->cmd.features = reg;
473 	ataio->cmd.sector_count = val;
474 	ataio->cmd.lba_low = val >> 8;
475 	ataio->cmd.lba_mid = val >> 16;
476 	ataio->cmd.lba_high = val >> 24;
477 	ataio->cmd.device = port & 0x0f;
478 }
479 
480 void
481 ata_bswap(int8_t *buf, int len)
482 {
483 	u_int16_t *ptr = (u_int16_t*)(buf + len);
484 
485 	while (--ptr >= (u_int16_t*)buf)
486 		*ptr = be16toh(*ptr);
487 }
488 
489 void
490 ata_btrim(int8_t *buf, int len)
491 {
492 	int8_t *ptr;
493 
494 	for (ptr = buf; ptr < buf+len; ++ptr)
495 		if (!*ptr || *ptr == '_')
496 			*ptr = ' ';
497 	for (ptr = buf + len - 1; ptr >= buf && *ptr == ' '; --ptr)
498 		*ptr = 0;
499 }
500 
501 void
502 ata_bpack(int8_t *src, int8_t *dst, int len)
503 {
504 	int i, j, blank;
505 
506 	for (i = j = blank = 0 ; i < len; i++) {
507 		if (blank && src[i] == ' ') continue;
508 		if (blank && src[i] != ' ') {
509 			dst[j++] = src[i];
510 			blank = 0;
511 			continue;
512 		}
513 		if (src[i] == ' ') {
514 			blank = 1;
515 			if (i == 0)
516 			continue;
517 		}
518 		dst[j++] = src[i];
519 	}
520 	while (j < len)
521 		dst[j++] = 0x00;
522 }
523 
524 int
525 ata_max_pmode(struct ata_params *ap)
526 {
527     if (ap->atavalid & ATA_FLAG_64_70) {
528 	if (ap->apiomodes & 0x02)
529 	    return ATA_PIO4;
530 	if (ap->apiomodes & 0x01)
531 	    return ATA_PIO3;
532     }
533     if (ap->mwdmamodes & 0x04)
534 	return ATA_PIO4;
535     if (ap->mwdmamodes & 0x02)
536 	return ATA_PIO3;
537     if (ap->mwdmamodes & 0x01)
538 	return ATA_PIO2;
539     if ((ap->retired_piomode & ATA_RETIRED_PIO_MASK) == 0x200)
540 	return ATA_PIO2;
541     if ((ap->retired_piomode & ATA_RETIRED_PIO_MASK) == 0x100)
542 	return ATA_PIO1;
543     if ((ap->retired_piomode & ATA_RETIRED_PIO_MASK) == 0x000)
544 	return ATA_PIO0;
545     return ATA_PIO0;
546 }
547 
548 int
549 ata_max_wmode(struct ata_params *ap)
550 {
551     if (ap->mwdmamodes & 0x04)
552 	return ATA_WDMA2;
553     if (ap->mwdmamodes & 0x02)
554 	return ATA_WDMA1;
555     if (ap->mwdmamodes & 0x01)
556 	return ATA_WDMA0;
557     return -1;
558 }
559 
560 int
561 ata_max_umode(struct ata_params *ap)
562 {
563     if (ap->atavalid & ATA_FLAG_88) {
564 	if (ap->udmamodes & 0x40)
565 	    return ATA_UDMA6;
566 	if (ap->udmamodes & 0x20)
567 	    return ATA_UDMA5;
568 	if (ap->udmamodes & 0x10)
569 	    return ATA_UDMA4;
570 	if (ap->udmamodes & 0x08)
571 	    return ATA_UDMA3;
572 	if (ap->udmamodes & 0x04)
573 	    return ATA_UDMA2;
574 	if (ap->udmamodes & 0x02)
575 	    return ATA_UDMA1;
576 	if (ap->udmamodes & 0x01)
577 	    return ATA_UDMA0;
578     }
579     return -1;
580 }
581 
582 int
583 ata_max_mode(struct ata_params *ap, int maxmode)
584 {
585 
586 	if (maxmode == 0)
587 		maxmode = ATA_DMA_MAX;
588 	if (maxmode >= ATA_UDMA0 && ata_max_umode(ap) > 0)
589 		return (min(maxmode, ata_max_umode(ap)));
590 	if (maxmode >= ATA_WDMA0 && ata_max_wmode(ap) > 0)
591 		return (min(maxmode, ata_max_wmode(ap)));
592 	return (min(maxmode, ata_max_pmode(ap)));
593 }
594 
595 char *
596 ata_mode2string(int mode)
597 {
598     switch (mode) {
599     case -1: return "UNSUPPORTED";
600     case 0: return "NONE";
601     case ATA_PIO0: return "PIO0";
602     case ATA_PIO1: return "PIO1";
603     case ATA_PIO2: return "PIO2";
604     case ATA_PIO3: return "PIO3";
605     case ATA_PIO4: return "PIO4";
606     case ATA_WDMA0: return "WDMA0";
607     case ATA_WDMA1: return "WDMA1";
608     case ATA_WDMA2: return "WDMA2";
609     case ATA_UDMA0: return "UDMA0";
610     case ATA_UDMA1: return "UDMA1";
611     case ATA_UDMA2: return "UDMA2";
612     case ATA_UDMA3: return "UDMA3";
613     case ATA_UDMA4: return "UDMA4";
614     case ATA_UDMA5: return "UDMA5";
615     case ATA_UDMA6: return "UDMA6";
616     default:
617 	if (mode & ATA_DMA_MASK)
618 	    return "BIOSDMA";
619 	else
620 	    return "BIOSPIO";
621     }
622 }
623 
624 int
625 ata_string2mode(char *str)
626 {
627 	if (!strcasecmp(str, "PIO0")) return (ATA_PIO0);
628 	if (!strcasecmp(str, "PIO1")) return (ATA_PIO1);
629 	if (!strcasecmp(str, "PIO2")) return (ATA_PIO2);
630 	if (!strcasecmp(str, "PIO3")) return (ATA_PIO3);
631 	if (!strcasecmp(str, "PIO4")) return (ATA_PIO4);
632 	if (!strcasecmp(str, "WDMA0")) return (ATA_WDMA0);
633 	if (!strcasecmp(str, "WDMA1")) return (ATA_WDMA1);
634 	if (!strcasecmp(str, "WDMA2")) return (ATA_WDMA2);
635 	if (!strcasecmp(str, "UDMA0")) return (ATA_UDMA0);
636 	if (!strcasecmp(str, "UDMA16")) return (ATA_UDMA0);
637 	if (!strcasecmp(str, "UDMA1")) return (ATA_UDMA1);
638 	if (!strcasecmp(str, "UDMA25")) return (ATA_UDMA1);
639 	if (!strcasecmp(str, "UDMA2")) return (ATA_UDMA2);
640 	if (!strcasecmp(str, "UDMA33")) return (ATA_UDMA2);
641 	if (!strcasecmp(str, "UDMA3")) return (ATA_UDMA3);
642 	if (!strcasecmp(str, "UDMA44")) return (ATA_UDMA3);
643 	if (!strcasecmp(str, "UDMA4")) return (ATA_UDMA4);
644 	if (!strcasecmp(str, "UDMA66")) return (ATA_UDMA4);
645 	if (!strcasecmp(str, "UDMA5")) return (ATA_UDMA5);
646 	if (!strcasecmp(str, "UDMA100")) return (ATA_UDMA5);
647 	if (!strcasecmp(str, "UDMA6")) return (ATA_UDMA6);
648 	if (!strcasecmp(str, "UDMA133")) return (ATA_UDMA6);
649 	return (-1);
650 }
651 
652 
653 u_int
654 ata_mode2speed(int mode)
655 {
656 	switch (mode) {
657 	case ATA_PIO0:
658 	default:
659 		return (3300);
660 	case ATA_PIO1:
661 		return (5200);
662 	case ATA_PIO2:
663 		return (8300);
664 	case ATA_PIO3:
665 		return (11100);
666 	case ATA_PIO4:
667 		return (16700);
668 	case ATA_WDMA0:
669 		return (4200);
670 	case ATA_WDMA1:
671 		return (13300);
672 	case ATA_WDMA2:
673 		return (16700);
674 	case ATA_UDMA0:
675 		return (16700);
676 	case ATA_UDMA1:
677 		return (25000);
678 	case ATA_UDMA2:
679 		return (33300);
680 	case ATA_UDMA3:
681 		return (44400);
682 	case ATA_UDMA4:
683 		return (66700);
684 	case ATA_UDMA5:
685 		return (100000);
686 	case ATA_UDMA6:
687 		return (133000);
688 	}
689 }
690 
691 u_int
692 ata_revision2speed(int revision)
693 {
694 	switch (revision) {
695 	case 1:
696 	default:
697 		return (150000);
698 	case 2:
699 		return (300000);
700 	case 3:
701 		return (600000);
702 	}
703 }
704 
705 int
706 ata_speed2revision(u_int speed)
707 {
708 	switch (speed) {
709 	case 0:
710 		return (0);
711 	case 150000:
712 		return (1);
713 	case 300000:
714 		return (2);
715 	case 600000:
716 		return (3);
717 	default:
718 		return (-1);
719 	}
720 }
721 
722 int
723 ata_identify_match(caddr_t identbuffer, caddr_t table_entry)
724 {
725 	struct scsi_inquiry_pattern *entry;
726 	struct ata_params *ident;
727 
728 	entry = (struct scsi_inquiry_pattern *)table_entry;
729 	ident = (struct ata_params *)identbuffer;
730 
731 	if ((cam_strmatch(ident->model, entry->product,
732 			  sizeof(ident->model)) == 0)
733 	 && (cam_strmatch(ident->revision, entry->revision,
734 			  sizeof(ident->revision)) == 0)) {
735 		return (0);
736 	}
737         return (-1);
738 }
739 
740 int
741 ata_static_identify_match(caddr_t identbuffer, caddr_t table_entry)
742 {
743 	struct scsi_static_inquiry_pattern *entry;
744 	struct ata_params *ident;
745 
746 	entry = (struct scsi_static_inquiry_pattern *)table_entry;
747 	ident = (struct ata_params *)identbuffer;
748 
749 	if ((cam_strmatch(ident->model, entry->product,
750 			  sizeof(ident->model)) == 0)
751 	 && (cam_strmatch(ident->revision, entry->revision,
752 			  sizeof(ident->revision)) == 0)) {
753 		return (0);
754 	}
755         return (-1);
756 }
757 
758 void
759 semb_receive_diagnostic_results(struct ccb_ataio *ataio,
760     u_int32_t retries, void (*cbfcnp)(struct cam_periph *, union ccb*),
761     uint8_t tag_action, int pcv, uint8_t page_code,
762     uint8_t *data_ptr, uint16_t length, uint32_t timeout)
763 {
764 
765 	length = min(length, 1020);
766 	length = (length + 3) & ~3;
767 	cam_fill_ataio(ataio,
768 		      retries,
769 		      cbfcnp,
770 		      /*flags*/CAM_DIR_IN,
771 		      tag_action,
772 		      data_ptr,
773 		      length,
774 		      timeout);
775 	ata_28bit_cmd(ataio, ATA_SEP_ATTN,
776 	    pcv ? page_code : 0, 0x02, length / 4);
777 }
778 
779 void
780 semb_send_diagnostic(struct ccb_ataio *ataio,
781     u_int32_t retries, void (*cbfcnp)(struct cam_periph *, union ccb *),
782     uint8_t tag_action, uint8_t *data_ptr, uint16_t length, uint32_t timeout)
783 {
784 
785 	length = min(length, 1020);
786 	length = (length + 3) & ~3;
787 	cam_fill_ataio(ataio,
788 		      retries,
789 		      cbfcnp,
790 		      /*flags*/length ? CAM_DIR_OUT : CAM_DIR_NONE,
791 		      tag_action,
792 		      data_ptr,
793 		      length,
794 		      timeout);
795 	ata_28bit_cmd(ataio, ATA_SEP_ATTN,
796 	    length > 0 ? data_ptr[0] : 0, 0x82, length / 4);
797 }
798 
799 void
800 semb_read_buffer(struct ccb_ataio *ataio,
801     u_int32_t retries, void (*cbfcnp)(struct cam_periph *, union ccb*),
802     uint8_t tag_action, uint8_t page_code,
803     uint8_t *data_ptr, uint16_t length, uint32_t timeout)
804 {
805 
806 	length = min(length, 1020);
807 	length = (length + 3) & ~3;
808 	cam_fill_ataio(ataio,
809 		      retries,
810 		      cbfcnp,
811 		      /*flags*/CAM_DIR_IN,
812 		      tag_action,
813 		      data_ptr,
814 		      length,
815 		      timeout);
816 	ata_28bit_cmd(ataio, ATA_SEP_ATTN,
817 	    page_code, 0x00, length / 4);
818 }
819 
820 void
821 semb_write_buffer(struct ccb_ataio *ataio,
822     u_int32_t retries, void (*cbfcnp)(struct cam_periph *, union ccb *),
823     uint8_t tag_action, uint8_t *data_ptr, uint16_t length, uint32_t timeout)
824 {
825 
826 	length = min(length, 1020);
827 	length = (length + 3) & ~3;
828 	cam_fill_ataio(ataio,
829 		      retries,
830 		      cbfcnp,
831 		      /*flags*/length ? CAM_DIR_OUT : CAM_DIR_NONE,
832 		      tag_action,
833 		      data_ptr,
834 		      length,
835 		      timeout);
836 	ata_28bit_cmd(ataio, ATA_SEP_ATTN,
837 	    length > 0 ? data_ptr[0] : 0, 0x80, length / 4);
838 }
839 
840