xref: /freebsd/sys/cam/ata/ata_all.c (revision 39ee7a7a6bdd1557b1c3532abf60d139798ac88b)
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 	const char *proto;
279 	char product[48], revision[16], ata[12], sata[12];
280 
281 	cam_strvis(product, ident_data->model, sizeof(ident_data->model),
282 		   sizeof(product));
283 	cam_strvis(revision, ident_data->revision, sizeof(ident_data->revision),
284 		   sizeof(revision));
285 	proto = (ident_data->config == ATA_PROTO_CFA) ? "CFA" :
286 		(ident_data->config & ATA_PROTO_ATAPI) ? "ATAPI" : "ATA";
287 	if (ata_version(ident_data->version_major) == 0) {
288 		snprintf(ata, sizeof(ata), "%s", proto);
289 	} else if (ata_version(ident_data->version_major) <= 7) {
290 		snprintf(ata, sizeof(ata), "%s-%d", proto,
291 		    ata_version(ident_data->version_major));
292 	} else if (ata_version(ident_data->version_major) == 8) {
293 		snprintf(ata, sizeof(ata), "%s8-ACS", proto);
294 	} else {
295 		snprintf(ata, sizeof(ata), "ACS-%d %s",
296 		    ata_version(ident_data->version_major) - 7, proto);
297 	}
298 	if (ident_data->satacapabilities && ident_data->satacapabilities != 0xffff) {
299 		if (ident_data->satacapabilities & ATA_SATA_GEN3)
300 			snprintf(sata, sizeof(sata), " SATA 3.x");
301 		else if (ident_data->satacapabilities & ATA_SATA_GEN2)
302 			snprintf(sata, sizeof(sata), " SATA 2.x");
303 		else if (ident_data->satacapabilities & ATA_SATA_GEN1)
304 			snprintf(sata, sizeof(sata), " SATA 1.x");
305 		else
306 			snprintf(sata, sizeof(sata), " SATA");
307 	} else
308 		sata[0] = 0;
309 	printf("<%s %s> %s%s device\n", product, revision, ata, sata);
310 }
311 
312 void
313 ata_print_ident_short(struct ata_params *ident_data)
314 {
315 	char product[48], revision[16];
316 
317 	cam_strvis(product, ident_data->model, sizeof(ident_data->model),
318 		   sizeof(product));
319 	cam_strvis(revision, ident_data->revision, sizeof(ident_data->revision),
320 		   sizeof(revision));
321 	printf("<%s %s>", product, revision);
322 }
323 
324 void
325 semb_print_ident(struct sep_identify_data *ident_data)
326 {
327 	char vendor[9], product[17], revision[5], fw[5], in[7], ins[5];
328 
329 	cam_strvis(vendor, ident_data->vendor_id, 8, sizeof(vendor));
330 	cam_strvis(product, ident_data->product_id, 16, sizeof(product));
331 	cam_strvis(revision, ident_data->product_rev, 4, sizeof(revision));
332 	cam_strvis(fw, ident_data->firmware_rev, 4, sizeof(fw));
333 	cam_strvis(in, ident_data->interface_id, 6, sizeof(in));
334 	cam_strvis(ins, ident_data->interface_rev, 4, sizeof(ins));
335 	printf("<%s %s %s %s> SEMB %s %s device\n",
336 	    vendor, product, revision, fw, in, ins);
337 }
338 
339 void
340 semb_print_ident_short(struct sep_identify_data *ident_data)
341 {
342 	char vendor[9], product[17], revision[5], fw[5];
343 
344 	cam_strvis(vendor, ident_data->vendor_id, 8, sizeof(vendor));
345 	cam_strvis(product, ident_data->product_id, 16, sizeof(product));
346 	cam_strvis(revision, ident_data->product_rev, 4, sizeof(revision));
347 	cam_strvis(fw, ident_data->firmware_rev, 4, sizeof(fw));
348 	printf("<%s %s %s %s>", vendor, product, revision, fw);
349 }
350 
351 uint32_t
352 ata_logical_sector_size(struct ata_params *ident_data)
353 {
354 	if ((ident_data->pss & ATA_PSS_VALID_MASK) == ATA_PSS_VALID_VALUE &&
355 	    (ident_data->pss & ATA_PSS_LSSABOVE512)) {
356 		return (((u_int32_t)ident_data->lss_1 |
357 		    ((u_int32_t)ident_data->lss_2 << 16)) * 2);
358 	}
359 	return (512);
360 }
361 
362 uint64_t
363 ata_physical_sector_size(struct ata_params *ident_data)
364 {
365 	if ((ident_data->pss & ATA_PSS_VALID_MASK) == ATA_PSS_VALID_VALUE) {
366 		if (ident_data->pss & ATA_PSS_MULTLS) {
367 			return ((uint64_t)ata_logical_sector_size(ident_data) *
368 			    (1 << (ident_data->pss & ATA_PSS_LSPPS)));
369 		} else {
370 			return (uint64_t)ata_logical_sector_size(ident_data);
371 		}
372 	}
373 	return (512);
374 }
375 
376 uint64_t
377 ata_logical_sector_offset(struct ata_params *ident_data)
378 {
379 	if ((ident_data->lsalign & 0xc000) == 0x4000) {
380 		return ((uint64_t)ata_logical_sector_size(ident_data) *
381 		    (ident_data->lsalign & 0x3fff));
382 	}
383 	return (0);
384 }
385 
386 void
387 ata_28bit_cmd(struct ccb_ataio *ataio, uint8_t cmd, uint8_t features,
388     uint32_t lba, uint8_t sector_count)
389 {
390 	bzero(&ataio->cmd, sizeof(ataio->cmd));
391 	ataio->cmd.flags = 0;
392 	if (cmd == ATA_READ_DMA ||
393 	    cmd == ATA_READ_DMA_QUEUED ||
394 	    cmd == ATA_WRITE_DMA ||
395 	    cmd == ATA_WRITE_DMA_QUEUED)
396 		ataio->cmd.flags |= CAM_ATAIO_DMA;
397 	ataio->cmd.command = cmd;
398 	ataio->cmd.features = features;
399 	ataio->cmd.lba_low = lba;
400 	ataio->cmd.lba_mid = lba >> 8;
401 	ataio->cmd.lba_high = lba >> 16;
402 	ataio->cmd.device = ATA_DEV_LBA | ((lba >> 24) & 0x0f);
403 	ataio->cmd.sector_count = sector_count;
404 }
405 
406 void
407 ata_48bit_cmd(struct ccb_ataio *ataio, uint8_t cmd, uint16_t features,
408     uint64_t lba, uint16_t sector_count)
409 {
410 
411 	ataio->cmd.flags = CAM_ATAIO_48BIT;
412 	if (cmd == ATA_READ_DMA48 ||
413 	    cmd == ATA_READ_DMA_QUEUED48 ||
414 	    cmd == ATA_READ_STREAM_DMA48 ||
415 	    cmd == ATA_WRITE_DMA48 ||
416 	    cmd == ATA_WRITE_DMA_FUA48 ||
417 	    cmd == ATA_WRITE_DMA_QUEUED48 ||
418 	    cmd == ATA_WRITE_DMA_QUEUED_FUA48 ||
419 	    cmd == ATA_WRITE_STREAM_DMA48 ||
420 	    cmd == ATA_DATA_SET_MANAGEMENT)
421 		ataio->cmd.flags |= CAM_ATAIO_DMA;
422 	ataio->cmd.command = cmd;
423 	ataio->cmd.features = features;
424 	ataio->cmd.lba_low = lba;
425 	ataio->cmd.lba_mid = lba >> 8;
426 	ataio->cmd.lba_high = lba >> 16;
427 	ataio->cmd.device = ATA_DEV_LBA;
428 	ataio->cmd.lba_low_exp = lba >> 24;
429 	ataio->cmd.lba_mid_exp = lba >> 32;
430 	ataio->cmd.lba_high_exp = lba >> 40;
431 	ataio->cmd.features_exp = features >> 8;
432 	ataio->cmd.sector_count = sector_count;
433 	ataio->cmd.sector_count_exp = sector_count >> 8;
434 	ataio->cmd.control = 0;
435 }
436 
437 void
438 ata_ncq_cmd(struct ccb_ataio *ataio, uint8_t cmd,
439     uint64_t lba, uint16_t sector_count)
440 {
441 
442 	ataio->cmd.flags = CAM_ATAIO_48BIT | CAM_ATAIO_FPDMA;
443 	ataio->cmd.command = cmd;
444 	ataio->cmd.features = sector_count;
445 	ataio->cmd.lba_low = lba;
446 	ataio->cmd.lba_mid = lba >> 8;
447 	ataio->cmd.lba_high = lba >> 16;
448 	ataio->cmd.device = ATA_DEV_LBA;
449 	ataio->cmd.lba_low_exp = lba >> 24;
450 	ataio->cmd.lba_mid_exp = lba >> 32;
451 	ataio->cmd.lba_high_exp = lba >> 40;
452 	ataio->cmd.features_exp = sector_count >> 8;
453 	ataio->cmd.sector_count = 0;
454 	ataio->cmd.sector_count_exp = 0;
455 	ataio->cmd.control = 0;
456 }
457 
458 void
459 ata_reset_cmd(struct ccb_ataio *ataio)
460 {
461 	bzero(&ataio->cmd, sizeof(ataio->cmd));
462 	ataio->cmd.flags = CAM_ATAIO_CONTROL | CAM_ATAIO_NEEDRESULT;
463 	ataio->cmd.control = 0x04;
464 }
465 
466 void
467 ata_pm_read_cmd(struct ccb_ataio *ataio, int reg, int port)
468 {
469 	bzero(&ataio->cmd, sizeof(ataio->cmd));
470 	ataio->cmd.flags = CAM_ATAIO_NEEDRESULT;
471 	ataio->cmd.command = ATA_READ_PM;
472 	ataio->cmd.features = reg;
473 	ataio->cmd.device = port & 0x0f;
474 }
475 
476 void
477 ata_pm_write_cmd(struct ccb_ataio *ataio, int reg, int port, uint32_t val)
478 {
479 	bzero(&ataio->cmd, sizeof(ataio->cmd));
480 	ataio->cmd.flags = 0;
481 	ataio->cmd.command = ATA_WRITE_PM;
482 	ataio->cmd.features = reg;
483 	ataio->cmd.sector_count = val;
484 	ataio->cmd.lba_low = val >> 8;
485 	ataio->cmd.lba_mid = val >> 16;
486 	ataio->cmd.lba_high = val >> 24;
487 	ataio->cmd.device = port & 0x0f;
488 }
489 
490 void
491 ata_bswap(int8_t *buf, int len)
492 {
493 	u_int16_t *ptr = (u_int16_t*)(buf + len);
494 
495 	while (--ptr >= (u_int16_t*)buf)
496 		*ptr = be16toh(*ptr);
497 }
498 
499 void
500 ata_btrim(int8_t *buf, int len)
501 {
502 	int8_t *ptr;
503 
504 	for (ptr = buf; ptr < buf+len; ++ptr)
505 		if (!*ptr || *ptr == '_')
506 			*ptr = ' ';
507 	for (ptr = buf + len - 1; ptr >= buf && *ptr == ' '; --ptr)
508 		*ptr = 0;
509 }
510 
511 void
512 ata_bpack(int8_t *src, int8_t *dst, int len)
513 {
514 	int i, j, blank;
515 
516 	for (i = j = blank = 0 ; i < len; i++) {
517 		if (blank && src[i] == ' ') continue;
518 		if (blank && src[i] != ' ') {
519 			dst[j++] = src[i];
520 			blank = 0;
521 			continue;
522 		}
523 		if (src[i] == ' ') {
524 			blank = 1;
525 			if (i == 0)
526 			continue;
527 		}
528 		dst[j++] = src[i];
529 	}
530 	while (j < len)
531 		dst[j++] = 0x00;
532 }
533 
534 int
535 ata_max_pmode(struct ata_params *ap)
536 {
537     if (ap->atavalid & ATA_FLAG_64_70) {
538 	if (ap->apiomodes & 0x02)
539 	    return ATA_PIO4;
540 	if (ap->apiomodes & 0x01)
541 	    return ATA_PIO3;
542     }
543     if (ap->mwdmamodes & 0x04)
544 	return ATA_PIO4;
545     if (ap->mwdmamodes & 0x02)
546 	return ATA_PIO3;
547     if (ap->mwdmamodes & 0x01)
548 	return ATA_PIO2;
549     if ((ap->retired_piomode & ATA_RETIRED_PIO_MASK) == 0x200)
550 	return ATA_PIO2;
551     if ((ap->retired_piomode & ATA_RETIRED_PIO_MASK) == 0x100)
552 	return ATA_PIO1;
553     if ((ap->retired_piomode & ATA_RETIRED_PIO_MASK) == 0x000)
554 	return ATA_PIO0;
555     return ATA_PIO0;
556 }
557 
558 int
559 ata_max_wmode(struct ata_params *ap)
560 {
561     if (ap->mwdmamodes & 0x04)
562 	return ATA_WDMA2;
563     if (ap->mwdmamodes & 0x02)
564 	return ATA_WDMA1;
565     if (ap->mwdmamodes & 0x01)
566 	return ATA_WDMA0;
567     return -1;
568 }
569 
570 int
571 ata_max_umode(struct ata_params *ap)
572 {
573     if (ap->atavalid & ATA_FLAG_88) {
574 	if (ap->udmamodes & 0x40)
575 	    return ATA_UDMA6;
576 	if (ap->udmamodes & 0x20)
577 	    return ATA_UDMA5;
578 	if (ap->udmamodes & 0x10)
579 	    return ATA_UDMA4;
580 	if (ap->udmamodes & 0x08)
581 	    return ATA_UDMA3;
582 	if (ap->udmamodes & 0x04)
583 	    return ATA_UDMA2;
584 	if (ap->udmamodes & 0x02)
585 	    return ATA_UDMA1;
586 	if (ap->udmamodes & 0x01)
587 	    return ATA_UDMA0;
588     }
589     return -1;
590 }
591 
592 int
593 ata_max_mode(struct ata_params *ap, int maxmode)
594 {
595 
596 	if (maxmode == 0)
597 		maxmode = ATA_DMA_MAX;
598 	if (maxmode >= ATA_UDMA0 && ata_max_umode(ap) > 0)
599 		return (min(maxmode, ata_max_umode(ap)));
600 	if (maxmode >= ATA_WDMA0 && ata_max_wmode(ap) > 0)
601 		return (min(maxmode, ata_max_wmode(ap)));
602 	return (min(maxmode, ata_max_pmode(ap)));
603 }
604 
605 char *
606 ata_mode2string(int mode)
607 {
608     switch (mode) {
609     case -1: return "UNSUPPORTED";
610     case 0: return "NONE";
611     case ATA_PIO0: return "PIO0";
612     case ATA_PIO1: return "PIO1";
613     case ATA_PIO2: return "PIO2";
614     case ATA_PIO3: return "PIO3";
615     case ATA_PIO4: return "PIO4";
616     case ATA_WDMA0: return "WDMA0";
617     case ATA_WDMA1: return "WDMA1";
618     case ATA_WDMA2: return "WDMA2";
619     case ATA_UDMA0: return "UDMA0";
620     case ATA_UDMA1: return "UDMA1";
621     case ATA_UDMA2: return "UDMA2";
622     case ATA_UDMA3: return "UDMA3";
623     case ATA_UDMA4: return "UDMA4";
624     case ATA_UDMA5: return "UDMA5";
625     case ATA_UDMA6: return "UDMA6";
626     default:
627 	if (mode & ATA_DMA_MASK)
628 	    return "BIOSDMA";
629 	else
630 	    return "BIOSPIO";
631     }
632 }
633 
634 int
635 ata_string2mode(char *str)
636 {
637 	if (!strcasecmp(str, "PIO0")) return (ATA_PIO0);
638 	if (!strcasecmp(str, "PIO1")) return (ATA_PIO1);
639 	if (!strcasecmp(str, "PIO2")) return (ATA_PIO2);
640 	if (!strcasecmp(str, "PIO3")) return (ATA_PIO3);
641 	if (!strcasecmp(str, "PIO4")) return (ATA_PIO4);
642 	if (!strcasecmp(str, "WDMA0")) return (ATA_WDMA0);
643 	if (!strcasecmp(str, "WDMA1")) return (ATA_WDMA1);
644 	if (!strcasecmp(str, "WDMA2")) return (ATA_WDMA2);
645 	if (!strcasecmp(str, "UDMA0")) return (ATA_UDMA0);
646 	if (!strcasecmp(str, "UDMA16")) return (ATA_UDMA0);
647 	if (!strcasecmp(str, "UDMA1")) return (ATA_UDMA1);
648 	if (!strcasecmp(str, "UDMA25")) return (ATA_UDMA1);
649 	if (!strcasecmp(str, "UDMA2")) return (ATA_UDMA2);
650 	if (!strcasecmp(str, "UDMA33")) return (ATA_UDMA2);
651 	if (!strcasecmp(str, "UDMA3")) return (ATA_UDMA3);
652 	if (!strcasecmp(str, "UDMA44")) return (ATA_UDMA3);
653 	if (!strcasecmp(str, "UDMA4")) return (ATA_UDMA4);
654 	if (!strcasecmp(str, "UDMA66")) return (ATA_UDMA4);
655 	if (!strcasecmp(str, "UDMA5")) return (ATA_UDMA5);
656 	if (!strcasecmp(str, "UDMA100")) return (ATA_UDMA5);
657 	if (!strcasecmp(str, "UDMA6")) return (ATA_UDMA6);
658 	if (!strcasecmp(str, "UDMA133")) return (ATA_UDMA6);
659 	return (-1);
660 }
661 
662 
663 u_int
664 ata_mode2speed(int mode)
665 {
666 	switch (mode) {
667 	case ATA_PIO0:
668 	default:
669 		return (3300);
670 	case ATA_PIO1:
671 		return (5200);
672 	case ATA_PIO2:
673 		return (8300);
674 	case ATA_PIO3:
675 		return (11100);
676 	case ATA_PIO4:
677 		return (16700);
678 	case ATA_WDMA0:
679 		return (4200);
680 	case ATA_WDMA1:
681 		return (13300);
682 	case ATA_WDMA2:
683 		return (16700);
684 	case ATA_UDMA0:
685 		return (16700);
686 	case ATA_UDMA1:
687 		return (25000);
688 	case ATA_UDMA2:
689 		return (33300);
690 	case ATA_UDMA3:
691 		return (44400);
692 	case ATA_UDMA4:
693 		return (66700);
694 	case ATA_UDMA5:
695 		return (100000);
696 	case ATA_UDMA6:
697 		return (133000);
698 	}
699 }
700 
701 u_int
702 ata_revision2speed(int revision)
703 {
704 	switch (revision) {
705 	case 1:
706 	default:
707 		return (150000);
708 	case 2:
709 		return (300000);
710 	case 3:
711 		return (600000);
712 	}
713 }
714 
715 int
716 ata_speed2revision(u_int speed)
717 {
718 	switch (speed) {
719 	case 0:
720 		return (0);
721 	case 150000:
722 		return (1);
723 	case 300000:
724 		return (2);
725 	case 600000:
726 		return (3);
727 	default:
728 		return (-1);
729 	}
730 }
731 
732 int
733 ata_identify_match(caddr_t identbuffer, caddr_t table_entry)
734 {
735 	struct scsi_inquiry_pattern *entry;
736 	struct ata_params *ident;
737 
738 	entry = (struct scsi_inquiry_pattern *)table_entry;
739 	ident = (struct ata_params *)identbuffer;
740 
741 	if ((cam_strmatch(ident->model, entry->product,
742 			  sizeof(ident->model)) == 0)
743 	 && (cam_strmatch(ident->revision, entry->revision,
744 			  sizeof(ident->revision)) == 0)) {
745 		return (0);
746 	}
747         return (-1);
748 }
749 
750 int
751 ata_static_identify_match(caddr_t identbuffer, caddr_t table_entry)
752 {
753 	struct scsi_static_inquiry_pattern *entry;
754 	struct ata_params *ident;
755 
756 	entry = (struct scsi_static_inquiry_pattern *)table_entry;
757 	ident = (struct ata_params *)identbuffer;
758 
759 	if ((cam_strmatch(ident->model, entry->product,
760 			  sizeof(ident->model)) == 0)
761 	 && (cam_strmatch(ident->revision, entry->revision,
762 			  sizeof(ident->revision)) == 0)) {
763 		return (0);
764 	}
765         return (-1);
766 }
767 
768 void
769 semb_receive_diagnostic_results(struct ccb_ataio *ataio,
770     u_int32_t retries, void (*cbfcnp)(struct cam_periph *, union ccb*),
771     uint8_t tag_action, int pcv, uint8_t page_code,
772     uint8_t *data_ptr, uint16_t length, uint32_t timeout)
773 {
774 
775 	length = min(length, 1020);
776 	length = (length + 3) & ~3;
777 	cam_fill_ataio(ataio,
778 		      retries,
779 		      cbfcnp,
780 		      /*flags*/CAM_DIR_IN,
781 		      tag_action,
782 		      data_ptr,
783 		      length,
784 		      timeout);
785 	ata_28bit_cmd(ataio, ATA_SEP_ATTN,
786 	    pcv ? page_code : 0, 0x02, length / 4);
787 }
788 
789 void
790 semb_send_diagnostic(struct ccb_ataio *ataio,
791     u_int32_t retries, void (*cbfcnp)(struct cam_periph *, union ccb *),
792     uint8_t tag_action, uint8_t *data_ptr, uint16_t length, uint32_t timeout)
793 {
794 
795 	length = min(length, 1020);
796 	length = (length + 3) & ~3;
797 	cam_fill_ataio(ataio,
798 		      retries,
799 		      cbfcnp,
800 		      /*flags*/length ? CAM_DIR_OUT : CAM_DIR_NONE,
801 		      tag_action,
802 		      data_ptr,
803 		      length,
804 		      timeout);
805 	ata_28bit_cmd(ataio, ATA_SEP_ATTN,
806 	    length > 0 ? data_ptr[0] : 0, 0x82, length / 4);
807 }
808 
809 void
810 semb_read_buffer(struct ccb_ataio *ataio,
811     u_int32_t retries, void (*cbfcnp)(struct cam_periph *, union ccb*),
812     uint8_t tag_action, uint8_t page_code,
813     uint8_t *data_ptr, uint16_t length, uint32_t timeout)
814 {
815 
816 	length = min(length, 1020);
817 	length = (length + 3) & ~3;
818 	cam_fill_ataio(ataio,
819 		      retries,
820 		      cbfcnp,
821 		      /*flags*/CAM_DIR_IN,
822 		      tag_action,
823 		      data_ptr,
824 		      length,
825 		      timeout);
826 	ata_28bit_cmd(ataio, ATA_SEP_ATTN,
827 	    page_code, 0x00, length / 4);
828 }
829 
830 void
831 semb_write_buffer(struct ccb_ataio *ataio,
832     u_int32_t retries, void (*cbfcnp)(struct cam_periph *, union ccb *),
833     uint8_t tag_action, uint8_t *data_ptr, uint16_t length, uint32_t timeout)
834 {
835 
836 	length = min(length, 1020);
837 	length = (length + 3) & ~3;
838 	cam_fill_ataio(ataio,
839 		      retries,
840 		      cbfcnp,
841 		      /*flags*/length ? CAM_DIR_OUT : CAM_DIR_NONE,
842 		      tag_action,
843 		      data_ptr,
844 		      length,
845 		      timeout);
846 	ata_28bit_cmd(ataio, ATA_SEP_ATTN,
847 	    length > 0 ? data_ptr[0] : 0, 0x80, length / 4);
848 }
849 
850