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