2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2009 Alexander Motin <mav@FreeBSD.org>
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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.
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.
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
32 #include <sys/param.h>
37 #include <sys/systm.h>
38 #include <sys/libkern.h>
39 #include <sys/kernel.h>
40 #include <sys/sysctl.h>
47 #define min(a,b) (((a)<(b))?(a):(b))
52 #include <cam/cam_ccb.h>
53 #include <cam/cam_queue.h>
54 #include <cam/cam_xpt.h>
56 #include <cam/ata/ata_all.h>
58 #include <sys/endian.h>
67 for (bit = 15; bit >= 0; bit--)
74 ata_op_string(struct ata_cmd *cmd)
77 if (cmd->control & 0x04)
78 return ("SOFT_RESET");
79 switch (cmd->command) {
81 switch (cmd->features) {
82 case 0x00: return ("NOP FLUSHQUEUE");
83 case 0x01: return ("NOP AUTOPOLL");
86 case 0x03: return ("CFA_REQUEST_EXTENDED_ERROR");
88 switch (cmd->features) {
89 case 0x01: return ("DSM TRIM");
92 case 0x08: return ("DEVICE_RESET");
93 case 0x0b: return ("REQUEST_SENSE_DATA_EXT");
94 case 0x20: return ("READ");
95 case 0x24: return ("READ48");
96 case 0x25: return ("READ_DMA48");
97 case 0x26: return ("READ_DMA_QUEUED48");
98 case 0x27: return ("READ_NATIVE_MAX_ADDRESS48");
99 case 0x29: return ("READ_MUL48");
100 case 0x2a: return ("READ_STREAM_DMA48");
101 case 0x2b: return ("READ_STREAM48");
102 case 0x2f: return ("READ_LOG_EXT");
103 case 0x30: return ("WRITE");
104 case 0x34: return ("WRITE48");
105 case 0x35: return ("WRITE_DMA48");
106 case 0x36: return ("WRITE_DMA_QUEUED48");
107 case 0x37: return ("SET_MAX_ADDRESS48");
108 case 0x39: return ("WRITE_MUL48");
109 case 0x3a: return ("WRITE_STREAM_DMA48");
110 case 0x3b: return ("WRITE_STREAM48");
111 case 0x3d: return ("WRITE_DMA_FUA48");
112 case 0x3e: return ("WRITE_DMA_QUEUED_FUA48");
113 case 0x3f: return ("WRITE_LOG_EXT");
114 case 0x40: return ("READ_VERIFY");
115 case 0x42: return ("READ_VERIFY48");
116 case 0x44: return ("ZERO_EXT");
118 switch (cmd->features) {
119 case 0x55: return ("WRITE_UNCORRECTABLE48 PSEUDO");
120 case 0xaa: return ("WRITE_UNCORRECTABLE48 FLAGGED");
122 return "WRITE_UNCORRECTABLE48";
123 case 0x47: return ("READ_LOG_DMA_EXT");
124 case 0x4a: return ("ZAC_MANAGEMENT_IN");
125 case 0x51: return ("CONFIGURE_STREAM");
126 case 0x57: return ("WRITE_LOG_DMA_EXT");
127 case 0x5b: return ("TRUSTED_NON_DATA");
128 case 0x5c: return ("TRUSTED_RECEIVE");
129 case 0x5d: return ("TRUSTED_RECEIVE_DMA");
130 case 0x5e: return ("TRUSTED_SEND");
131 case 0x5f: return ("TRUSTED_SEND_DMA");
132 case 0x60: return ("READ_FPDMA_QUEUED");
133 case 0x61: return ("WRITE_FPDMA_QUEUED");
135 switch (cmd->features & 0xf) {
136 case 0x00: return ("NCQ_NON_DATA ABORT NCQ QUEUE");
137 case 0x01: return ("NCQ_NON_DATA DEADLINE HANDLING");
138 case 0x05: return ("NCQ_NON_DATA SET FEATURES");
140 * XXX KDM need common decoding between NCQ and non-NCQ
141 * versions of SET FEATURES.
143 case 0x06: return ("NCQ_NON_DATA ZERO EXT");
144 case 0x07: return ("NCQ_NON_DATA ZAC MANAGEMENT OUT");
146 return ("NCQ_NON_DATA");
148 switch (cmd->sector_count_exp & 0xf) {
149 case 0x00: return ("SEND_FPDMA_QUEUED DATA SET MANAGEMENT");
150 case 0x02: return ("SEND_FPDMA_QUEUED WRITE LOG DMA EXT");
151 case 0x03: return ("SEND_FPDMA_QUEUED ZAC MANAGEMENT OUT");
152 case 0x04: return ("SEND_FPDMA_QUEUED DATA SET MANAGEMENT XL");
154 return ("SEND_FPDMA_QUEUED");
156 switch (cmd->sector_count_exp & 0xf) {
157 case 0x01: return ("RECEIVE_FPDMA_QUEUED READ LOG DMA EXT");
158 case 0x02: return ("RECEIVE_FPDMA_QUEUED ZAC MANAGEMENT IN");
160 return ("RECEIVE_FPDMA_QUEUED");
162 if (cmd->features == 0xec)
163 return ("SEP_ATTN IDENTIFY");
164 switch (cmd->lba_low) {
165 case 0x00: return ("SEP_ATTN READ BUFFER");
166 case 0x02: return ("SEP_ATTN RECEIVE DIAGNOSTIC RESULTS");
167 case 0x80: return ("SEP_ATTN WRITE BUFFER");
168 case 0x82: return ("SEP_ATTN SEND DIAGNOSTIC");
171 case 0x70: return ("SEEK");
172 case 0x77: return ("SET_DATE_TIME_EXT");
173 case 0x78: return ("ACCESSIBLE_MAX_ADDRESS_CONFIGURATION");
174 case 0x87: return ("CFA_TRANSLATE_SECTOR");
175 case 0x90: return ("EXECUTE_DEVICE_DIAGNOSTIC");
176 case 0x92: return ("DOWNLOAD_MICROCODE");
177 case 0x93: return ("DOWNLOAD_MICROCODE_DMA");
178 case 0x9a: return ("ZAC_MANAGEMENT_OUT");
179 case 0xa0: return ("PACKET");
180 case 0xa1: return ("ATAPI_IDENTIFY");
181 case 0xa2: return ("SERVICE");
183 switch(cmd->features) {
184 case 0xd0: return ("SMART READ ATTR VALUES");
185 case 0xd1: return ("SMART READ ATTR THRESHOLDS");
186 case 0xd3: return ("SMART SAVE ATTR VALUES");
187 case 0xd4: return ("SMART EXECUTE OFFLINE IMMEDIATE");
188 case 0xd5: return ("SMART READ LOG DATA");
189 case 0xd8: return ("SMART ENABLE OPERATION");
190 case 0xd9: return ("SMART DISABLE OPERATION");
191 case 0xda: return ("SMART RETURN STATUS");
194 case 0xb1: return ("DEVICE CONFIGURATION");
195 case 0xb4: return ("SANITIZE_DEVICE");
196 case 0xc0: return ("CFA_ERASE");
197 case 0xc4: return ("READ_MUL");
198 case 0xc5: return ("WRITE_MUL");
199 case 0xc6: return ("SET_MULTI");
200 case 0xc7: return ("READ_DMA_QUEUED");
201 case 0xc8: return ("READ_DMA");
202 case 0xca: return ("WRITE_DMA");
203 case 0xcc: return ("WRITE_DMA_QUEUED");
204 case 0xcd: return ("CFA_WRITE_MULTIPLE_WITHOUT_ERASE");
205 case 0xce: return ("WRITE_MUL_FUA48");
206 case 0xd1: return ("CHECK_MEDIA_CARD_TYPE");
207 case 0xda: return ("GET_MEDIA_STATUS");
208 case 0xde: return ("MEDIA_LOCK");
209 case 0xdf: return ("MEDIA_UNLOCK");
210 case 0xe0: return ("STANDBY_IMMEDIATE");
211 case 0xe1: return ("IDLE_IMMEDIATE");
212 case 0xe2: return ("STANDBY");
213 case 0xe3: return ("IDLE");
214 case 0xe4: return ("READ_BUFFER/PM");
215 case 0xe5: return ("CHECK_POWER_MODE");
216 case 0xe6: return ("SLEEP");
217 case 0xe7: return ("FLUSHCACHE");
218 case 0xe8: return ("WRITE_BUFFER/PM");
219 case 0xe9: return ("READ_BUFFER_DMA");
220 case 0xea: return ("FLUSHCACHE48");
221 case 0xeb: return ("WRITE_BUFFER_DMA");
222 case 0xec: return ("ATA_IDENTIFY");
223 case 0xed: return ("MEDIA_EJECT");
226 * XXX KDM need common decoding between NCQ and non-NCQ
227 * versions of SET FEATURES.
229 switch (cmd->features) {
230 case 0x02: return ("SETFEATURES ENABLE WCACHE");
231 case 0x03: return ("SETFEATURES SET TRANSFER MODE");
232 case 0x04: return ("SETFEATURES ENABLE APM");
233 case 0x06: return ("SETFEATURES ENABLE PUIS");
234 case 0x07: return ("SETFEATURES SPIN-UP");
235 case 0x0b: return ("SETFEATURES ENABLE WRITE READ VERIFY");
236 case 0x0c: return ("SETFEATURES ENABLE DEVICE LIFE CONTROL");
237 case 0x10: return ("SETFEATURES ENABLE SATA FEATURE");
238 case 0x41: return ("SETFEATURES ENABLE FREEFALL CONTROL");
239 case 0x43: return ("SETFEATURES SET MAX HOST INT SECT TIMES");
240 case 0x45: return ("SETFEATURES SET RATE BASIS");
241 case 0x4a: return ("SETFEATURES EXTENDED POWER CONDITIONS");
242 case 0x55: return ("SETFEATURES DISABLE RCACHE");
243 case 0x5d: return ("SETFEATURES ENABLE RELIRQ");
244 case 0x5e: return ("SETFEATURES ENABLE SRVIRQ");
245 case 0x62: return ("SETFEATURES LONG PHYS SECT ALIGN ERC");
246 case 0x63: return ("SETFEATURES DSN");
247 case 0x66: return ("SETFEATURES DISABLE DEFAULTS");
248 case 0x82: return ("SETFEATURES DISABLE WCACHE");
249 case 0x85: return ("SETFEATURES DISABLE APM");
250 case 0x86: return ("SETFEATURES DISABLE PUIS");
251 case 0x8b: return ("SETFEATURES DISABLE WRITE READ VERIFY");
252 case 0x8c: return ("SETFEATURES DISABLE DEVICE LIFE CONTROL");
253 case 0x90: return ("SETFEATURES DISABLE SATA FEATURE");
254 case 0xaa: return ("SETFEATURES ENABLE RCACHE");
255 case 0xC1: return ("SETFEATURES DISABLE FREEFALL CONTROL");
256 case 0xC3: return ("SETFEATURES SENSE DATA REPORTING");
257 case 0xC4: return ("SETFEATURES NCQ SENSE DATA RETURN");
258 case 0xCC: return ("SETFEATURES ENABLE DEFAULTS");
259 case 0xdd: return ("SETFEATURES DISABLE RELIRQ");
260 case 0xde: return ("SETFEATURES DISABLE SRVIRQ");
262 return "SETFEATURES";
263 case 0xf1: return ("SECURITY_SET_PASSWORD");
264 case 0xf2: return ("SECURITY_UNLOCK");
265 case 0xf3: return ("SECURITY_ERASE_PREPARE");
266 case 0xf4: return ("SECURITY_ERASE_UNIT");
267 case 0xf5: return ("SECURITY_FREEZE_LOCK");
268 case 0xf6: return ("SECURITY_DISABLE_PASSWORD");
269 case 0xf8: return ("READ_NATIVE_MAX_ADDRESS");
270 case 0xf9: return ("SET_MAX_ADDRESS");
276 ata_cmd_string(struct ata_cmd *cmd, char *cmd_string, size_t len)
284 sbuf_new(&sb, cmd_string, len, SBUF_FIXEDLEN);
285 ata_cmd_sbuf(cmd, &sb);
287 error = sbuf_finish(&sb);
288 if (error != 0 && error != ENOMEM)
291 return(sbuf_data(&sb));
295 ata_cmd_sbuf(struct ata_cmd *cmd, struct sbuf *sb)
297 sbuf_printf(sb, "%02x %02x %02x %02x "
298 "%02x %02x %02x %02x %02x %02x %02x %02x",
299 cmd->command, cmd->features,
300 cmd->lba_low, cmd->lba_mid, cmd->lba_high, cmd->device,
301 cmd->lba_low_exp, cmd->lba_mid_exp, cmd->lba_high_exp,
302 cmd->features_exp, cmd->sector_count, cmd->sector_count_exp);
306 ata_res_string(struct ata_res *res, char *res_string, size_t len)
314 sbuf_new(&sb, res_string, len, SBUF_FIXEDLEN);
315 ata_res_sbuf(res, &sb);
317 error = sbuf_finish(&sb);
318 if (error != 0 && error != ENOMEM)
321 return(sbuf_data(&sb));
325 ata_res_sbuf(struct ata_res *res, struct sbuf *sb)
328 sbuf_printf(sb, "%02x %02x %02x %02x "
329 "%02x %02x %02x %02x %02x %02x %02x",
330 res->status, res->error,
331 res->lba_low, res->lba_mid, res->lba_high, res->device,
332 res->lba_low_exp, res->lba_mid_exp, res->lba_high_exp,
333 res->sector_count, res->sector_count_exp);
339 * ata_command_sbuf() returns 0 for success and -1 for failure.
342 ata_command_sbuf(struct ccb_ataio *ataio, struct sbuf *sb)
345 sbuf_printf(sb, "%s. ACB: ",
346 ata_op_string(&ataio->cmd));
347 ata_cmd_sbuf(&ataio->cmd, sb);
353 * ata_status_abuf() returns 0 for success and -1 for failure.
356 ata_status_sbuf(struct ccb_ataio *ataio, struct sbuf *sb)
359 sbuf_printf(sb, "ATA status: %02x (%s%s%s%s%s%s%s%s)",
361 (ataio->res.status & 0x80) ? "BSY " : "",
362 (ataio->res.status & 0x40) ? "DRDY " : "",
363 (ataio->res.status & 0x20) ? "DF " : "",
364 (ataio->res.status & 0x10) ? "SERV " : "",
365 (ataio->res.status & 0x08) ? "DRQ " : "",
366 (ataio->res.status & 0x04) ? "CORR " : "",
367 (ataio->res.status & 0x02) ? "IDX " : "",
368 (ataio->res.status & 0x01) ? "ERR" : "");
369 if (ataio->res.status & 1) {
370 sbuf_printf(sb, ", error: %02x (%s%s%s%s%s%s%s%s)",
372 (ataio->res.error & 0x80) ? "ICRC " : "",
373 (ataio->res.error & 0x40) ? "UNC " : "",
374 (ataio->res.error & 0x20) ? "MC " : "",
375 (ataio->res.error & 0x10) ? "IDNF " : "",
376 (ataio->res.error & 0x08) ? "MCR " : "",
377 (ataio->res.error & 0x04) ? "ABRT " : "",
378 (ataio->res.error & 0x02) ? "NM " : "",
379 (ataio->res.error & 0x01) ? "ILI" : "");
386 ata_print_ident(struct ata_params *ident_data)
389 char ata[12], sata[12];
391 ata_print_ident_short(ident_data);
393 proto = (ident_data->config == ATA_PROTO_CFA) ? "CFA" :
394 (ident_data->config & ATA_PROTO_ATAPI) ? "ATAPI" : "ATA";
395 if (ata_version(ident_data->version_major) == 0) {
396 snprintf(ata, sizeof(ata), "%s", proto);
397 } else if (ata_version(ident_data->version_major) <= 7) {
398 snprintf(ata, sizeof(ata), "%s-%d", proto,
399 ata_version(ident_data->version_major));
400 } else if (ata_version(ident_data->version_major) == 8) {
401 snprintf(ata, sizeof(ata), "%s8-ACS", proto);
403 snprintf(ata, sizeof(ata), "ACS-%d %s",
404 ata_version(ident_data->version_major) - 7, proto);
406 if (ident_data->satacapabilities && ident_data->satacapabilities != 0xffff) {
407 if (ident_data->satacapabilities & ATA_SATA_GEN3)
408 snprintf(sata, sizeof(sata), " SATA 3.x");
409 else if (ident_data->satacapabilities & ATA_SATA_GEN2)
410 snprintf(sata, sizeof(sata), " SATA 2.x");
411 else if (ident_data->satacapabilities & ATA_SATA_GEN1)
412 snprintf(sata, sizeof(sata), " SATA 1.x");
414 snprintf(sata, sizeof(sata), " SATA");
417 printf(" %s%s device\n", ata, sata);
421 ata_print_ident_sbuf(struct ata_params *ident_data, struct sbuf *sb)
423 const char *proto, *sata;
426 ata_print_ident_short_sbuf(ident_data, sb);
427 sbuf_printf(sb, " ");
429 proto = (ident_data->config == ATA_PROTO_CFA) ? "CFA" :
430 (ident_data->config & ATA_PROTO_ATAPI) ? "ATAPI" : "ATA";
431 version = ata_version(ident_data->version_major);
435 sbuf_printf(sb, "%s", proto);
444 sbuf_printf(sb, "%s-%d", proto, version);
447 sbuf_printf(sb, "%s8-ACS", proto);
450 sbuf_printf(sb, "ACS-%d %s", version - 7, proto);
454 if (ident_data->satacapabilities && ident_data->satacapabilities != 0xffff) {
455 if (ident_data->satacapabilities & ATA_SATA_GEN3)
457 else if (ident_data->satacapabilities & ATA_SATA_GEN2)
459 else if (ident_data->satacapabilities & ATA_SATA_GEN1)
465 sbuf_printf(sb, "%s device\n", sata);
469 ata_print_ident_short(struct ata_params *ident_data)
471 char product[48], revision[16];
473 cam_strvis(product, ident_data->model, sizeof(ident_data->model),
475 cam_strvis(revision, ident_data->revision, sizeof(ident_data->revision),
477 printf("<%s %s>", product, revision);
481 ata_print_ident_short_sbuf(struct ata_params *ident_data, struct sbuf *sb)
484 sbuf_printf(sb, "<");
485 cam_strvis_sbuf(sb, ident_data->model, sizeof(ident_data->model), 0);
486 sbuf_printf(sb, " ");
487 cam_strvis_sbuf(sb, ident_data->revision, sizeof(ident_data->revision), 0);
488 sbuf_printf(sb, ">");
492 semb_print_ident(struct sep_identify_data *ident_data)
496 semb_print_ident_short(ident_data);
497 cam_strvis(in, ident_data->interface_id, 6, sizeof(in));
498 cam_strvis(ins, ident_data->interface_rev, 4, sizeof(ins));
499 printf(" SEMB %s %s device\n", in, ins);
503 semb_print_ident_sbuf(struct sep_identify_data *ident_data, struct sbuf *sb)
506 semb_print_ident_short_sbuf(ident_data, sb);
508 sbuf_printf(sb, " SEMB ");
509 cam_strvis_sbuf(sb, ident_data->interface_id, 6, 0);
510 sbuf_printf(sb, " ");
511 cam_strvis_sbuf(sb, ident_data->interface_rev, 4, 0);
512 sbuf_printf(sb, " device\n");
516 semb_print_ident_short(struct sep_identify_data *ident_data)
518 char vendor[9], product[17], revision[5], fw[5];
520 cam_strvis(vendor, ident_data->vendor_id, 8, sizeof(vendor));
521 cam_strvis(product, ident_data->product_id, 16, sizeof(product));
522 cam_strvis(revision, ident_data->product_rev, 4, sizeof(revision));
523 cam_strvis(fw, ident_data->firmware_rev, 4, sizeof(fw));
524 printf("<%s %s %s %s>", vendor, product, revision, fw);
528 semb_print_ident_short_sbuf(struct sep_identify_data *ident_data, struct sbuf *sb)
531 sbuf_printf(sb, "<");
532 cam_strvis_sbuf(sb, ident_data->vendor_id, 8, 0);
533 sbuf_printf(sb, " ");
534 cam_strvis_sbuf(sb, ident_data->product_id, 16, 0);
535 sbuf_printf(sb, " ");
536 cam_strvis_sbuf(sb, ident_data->product_rev, 4, 0);
537 sbuf_printf(sb, " ");
538 cam_strvis_sbuf(sb, ident_data->firmware_rev, 4, 0);
539 sbuf_printf(sb, ">");
543 ata_logical_sector_size(struct ata_params *ident_data)
545 if ((ident_data->pss & ATA_PSS_VALID_MASK) == ATA_PSS_VALID_VALUE &&
546 (ident_data->pss & ATA_PSS_LSSABOVE512)) {
547 return (((u_int32_t)ident_data->lss_1 |
548 ((u_int32_t)ident_data->lss_2 << 16)) * 2);
554 ata_physical_sector_size(struct ata_params *ident_data)
556 if ((ident_data->pss & ATA_PSS_VALID_MASK) == ATA_PSS_VALID_VALUE) {
557 if (ident_data->pss & ATA_PSS_MULTLS) {
558 return ((uint64_t)ata_logical_sector_size(ident_data) *
559 (1 << (ident_data->pss & ATA_PSS_LSPPS)));
561 return (uint64_t)ata_logical_sector_size(ident_data);
568 ata_logical_sector_offset(struct ata_params *ident_data)
570 if ((ident_data->lsalign & 0xc000) == 0x4000) {
571 return ((uint64_t)ata_logical_sector_size(ident_data) *
572 (ident_data->lsalign & 0x3fff));
578 ata_28bit_cmd(struct ccb_ataio *ataio, uint8_t cmd, uint8_t features,
579 uint32_t lba, uint8_t sector_count)
581 bzero(&ataio->cmd, sizeof(ataio->cmd));
582 ataio->cmd.flags = 0;
583 if (cmd == ATA_READ_DMA ||
584 cmd == ATA_READ_DMA_QUEUED ||
585 cmd == ATA_WRITE_DMA ||
586 cmd == ATA_WRITE_DMA_QUEUED ||
587 cmd == ATA_TRUSTED_RECEIVE_DMA ||
588 cmd == ATA_TRUSTED_SEND_DMA ||
589 cmd == ATA_DOWNLOAD_MICROCODE_DMA ||
590 cmd == ATA_READ_BUFFER_DMA ||
591 cmd == ATA_WRITE_BUFFER_DMA)
592 ataio->cmd.flags |= CAM_ATAIO_DMA;
593 ataio->cmd.command = cmd;
594 ataio->cmd.features = features;
595 ataio->cmd.lba_low = lba;
596 ataio->cmd.lba_mid = lba >> 8;
597 ataio->cmd.lba_high = lba >> 16;
598 ataio->cmd.device = ATA_DEV_LBA | ((lba >> 24) & 0x0f);
599 ataio->cmd.sector_count = sector_count;
603 ata_48bit_cmd(struct ccb_ataio *ataio, uint8_t cmd, uint16_t features,
604 uint64_t lba, uint16_t sector_count)
607 ataio->cmd.flags = CAM_ATAIO_48BIT;
608 if (cmd == ATA_READ_DMA48 ||
609 cmd == ATA_READ_DMA_QUEUED48 ||
610 cmd == ATA_READ_STREAM_DMA48 ||
611 cmd == ATA_WRITE_DMA48 ||
612 cmd == ATA_WRITE_DMA_FUA48 ||
613 cmd == ATA_WRITE_DMA_QUEUED48 ||
614 cmd == ATA_WRITE_DMA_QUEUED_FUA48 ||
615 cmd == ATA_WRITE_STREAM_DMA48 ||
616 cmd == ATA_DATA_SET_MANAGEMENT ||
617 cmd == ATA_READ_LOG_DMA_EXT ||
618 cmd == ATA_WRITE_LOG_DMA_EXT)
619 ataio->cmd.flags |= CAM_ATAIO_DMA;
620 ataio->cmd.command = cmd;
621 ataio->cmd.features = features;
622 ataio->cmd.lba_low = lba;
623 ataio->cmd.lba_mid = lba >> 8;
624 ataio->cmd.lba_high = lba >> 16;
625 ataio->cmd.device = ATA_DEV_LBA;
626 ataio->cmd.lba_low_exp = lba >> 24;
627 ataio->cmd.lba_mid_exp = lba >> 32;
628 ataio->cmd.lba_high_exp = lba >> 40;
629 ataio->cmd.features_exp = features >> 8;
630 ataio->cmd.sector_count = sector_count;
631 ataio->cmd.sector_count_exp = sector_count >> 8;
632 ataio->cmd.control = 0;
636 ata_ncq_cmd(struct ccb_ataio *ataio, uint8_t cmd,
637 uint64_t lba, uint16_t sector_count)
640 ataio->cmd.flags = CAM_ATAIO_48BIT | CAM_ATAIO_FPDMA;
641 ataio->cmd.command = cmd;
642 ataio->cmd.features = sector_count;
643 ataio->cmd.lba_low = lba;
644 ataio->cmd.lba_mid = lba >> 8;
645 ataio->cmd.lba_high = lba >> 16;
646 ataio->cmd.device = ATA_DEV_LBA;
647 ataio->cmd.lba_low_exp = lba >> 24;
648 ataio->cmd.lba_mid_exp = lba >> 32;
649 ataio->cmd.lba_high_exp = lba >> 40;
650 ataio->cmd.features_exp = sector_count >> 8;
651 ataio->cmd.sector_count = 0;
652 ataio->cmd.sector_count_exp = 0;
653 ataio->cmd.control = 0;
657 ata_reset_cmd(struct ccb_ataio *ataio)
659 bzero(&ataio->cmd, sizeof(ataio->cmd));
660 ataio->cmd.flags = CAM_ATAIO_CONTROL | CAM_ATAIO_NEEDRESULT;
661 ataio->cmd.control = 0x04;
665 ata_pm_read_cmd(struct ccb_ataio *ataio, int reg, int port)
667 bzero(&ataio->cmd, sizeof(ataio->cmd));
668 ataio->cmd.flags = CAM_ATAIO_NEEDRESULT;
669 ataio->cmd.command = ATA_READ_PM;
670 ataio->cmd.features = reg;
671 ataio->cmd.device = port & 0x0f;
675 ata_pm_write_cmd(struct ccb_ataio *ataio, int reg, int port, uint32_t val)
677 bzero(&ataio->cmd, sizeof(ataio->cmd));
678 ataio->cmd.flags = 0;
679 ataio->cmd.command = ATA_WRITE_PM;
680 ataio->cmd.features = reg;
681 ataio->cmd.sector_count = val;
682 ataio->cmd.lba_low = val >> 8;
683 ataio->cmd.lba_mid = val >> 16;
684 ataio->cmd.lba_high = val >> 24;
685 ataio->cmd.device = port & 0x0f;
689 ata_read_log(struct ccb_ataio *ataio, uint32_t retries,
690 void (*cbfcnp)(struct cam_periph *, union ccb *),
691 uint32_t log_address, uint32_t page_number, uint16_t block_count,
692 uint32_t protocol, uint8_t *data_ptr, uint32_t dxfer_len,
697 cam_fill_ataio(ataio,
700 /*flags*/ CAM_DIR_IN,
702 /*data_ptr*/ data_ptr,
703 /*dxfer_len*/ dxfer_len,
704 /*timeout*/ timeout);
706 lba = (((uint64_t)page_number & 0xff00) << 32) |
707 ((page_number & 0x00ff) << 8) |
708 (log_address & 0xff);
711 /*cmd*/ (protocol & CAM_ATAIO_DMA) ? ATA_READ_LOG_DMA_EXT :
715 /*sector_count*/ block_count);
719 ata_bswap(int8_t *buf, int len)
721 u_int16_t *ptr = (u_int16_t*)(buf + len);
723 while (--ptr >= (u_int16_t*)buf)
724 *ptr = be16toh(*ptr);
728 ata_btrim(int8_t *buf, int len)
732 for (ptr = buf; ptr < buf+len; ++ptr)
733 if (!*ptr || *ptr == '_')
735 for (ptr = buf + len - 1; ptr >= buf && *ptr == ' '; --ptr)
740 ata_bpack(int8_t *src, int8_t *dst, int len)
744 for (i = j = blank = 0 ; i < len; i++) {
745 if (blank && src[i] == ' ') continue;
746 if (blank && src[i] != ' ') {
763 ata_max_pmode(struct ata_params *ap)
765 if (ap->atavalid & ATA_FLAG_64_70) {
766 if (ap->apiomodes & 0x02)
768 if (ap->apiomodes & 0x01)
771 if (ap->mwdmamodes & 0x04)
773 if (ap->mwdmamodes & 0x02)
775 if (ap->mwdmamodes & 0x01)
777 if ((ap->retired_piomode & ATA_RETIRED_PIO_MASK) == 0x200)
779 if ((ap->retired_piomode & ATA_RETIRED_PIO_MASK) == 0x100)
781 if ((ap->retired_piomode & ATA_RETIRED_PIO_MASK) == 0x000)
787 ata_max_wmode(struct ata_params *ap)
789 if (ap->mwdmamodes & 0x04)
791 if (ap->mwdmamodes & 0x02)
793 if (ap->mwdmamodes & 0x01)
799 ata_max_umode(struct ata_params *ap)
801 if (ap->atavalid & ATA_FLAG_88) {
802 if (ap->udmamodes & 0x40)
804 if (ap->udmamodes & 0x20)
806 if (ap->udmamodes & 0x10)
808 if (ap->udmamodes & 0x08)
810 if (ap->udmamodes & 0x04)
812 if (ap->udmamodes & 0x02)
814 if (ap->udmamodes & 0x01)
821 ata_max_mode(struct ata_params *ap, int maxmode)
825 maxmode = ATA_DMA_MAX;
826 if (maxmode >= ATA_UDMA0 && ata_max_umode(ap) > 0)
827 return (min(maxmode, ata_max_umode(ap)));
828 if (maxmode >= ATA_WDMA0 && ata_max_wmode(ap) > 0)
829 return (min(maxmode, ata_max_wmode(ap)));
830 return (min(maxmode, ata_max_pmode(ap)));
834 ata_mode2string(int mode)
837 case -1: return "UNSUPPORTED";
838 case 0: return "NONE";
839 case ATA_PIO0: return "PIO0";
840 case ATA_PIO1: return "PIO1";
841 case ATA_PIO2: return "PIO2";
842 case ATA_PIO3: return "PIO3";
843 case ATA_PIO4: return "PIO4";
844 case ATA_WDMA0: return "WDMA0";
845 case ATA_WDMA1: return "WDMA1";
846 case ATA_WDMA2: return "WDMA2";
847 case ATA_UDMA0: return "UDMA0";
848 case ATA_UDMA1: return "UDMA1";
849 case ATA_UDMA2: return "UDMA2";
850 case ATA_UDMA3: return "UDMA3";
851 case ATA_UDMA4: return "UDMA4";
852 case ATA_UDMA5: return "UDMA5";
853 case ATA_UDMA6: return "UDMA6";
855 if (mode & ATA_DMA_MASK)
863 ata_string2mode(char *str)
865 if (!strcasecmp(str, "PIO0")) return (ATA_PIO0);
866 if (!strcasecmp(str, "PIO1")) return (ATA_PIO1);
867 if (!strcasecmp(str, "PIO2")) return (ATA_PIO2);
868 if (!strcasecmp(str, "PIO3")) return (ATA_PIO3);
869 if (!strcasecmp(str, "PIO4")) return (ATA_PIO4);
870 if (!strcasecmp(str, "WDMA0")) return (ATA_WDMA0);
871 if (!strcasecmp(str, "WDMA1")) return (ATA_WDMA1);
872 if (!strcasecmp(str, "WDMA2")) return (ATA_WDMA2);
873 if (!strcasecmp(str, "UDMA0")) return (ATA_UDMA0);
874 if (!strcasecmp(str, "UDMA16")) return (ATA_UDMA0);
875 if (!strcasecmp(str, "UDMA1")) return (ATA_UDMA1);
876 if (!strcasecmp(str, "UDMA25")) return (ATA_UDMA1);
877 if (!strcasecmp(str, "UDMA2")) return (ATA_UDMA2);
878 if (!strcasecmp(str, "UDMA33")) return (ATA_UDMA2);
879 if (!strcasecmp(str, "UDMA3")) return (ATA_UDMA3);
880 if (!strcasecmp(str, "UDMA44")) return (ATA_UDMA3);
881 if (!strcasecmp(str, "UDMA4")) return (ATA_UDMA4);
882 if (!strcasecmp(str, "UDMA66")) return (ATA_UDMA4);
883 if (!strcasecmp(str, "UDMA5")) return (ATA_UDMA5);
884 if (!strcasecmp(str, "UDMA100")) return (ATA_UDMA5);
885 if (!strcasecmp(str, "UDMA6")) return (ATA_UDMA6);
886 if (!strcasecmp(str, "UDMA133")) return (ATA_UDMA6);
892 ata_mode2speed(int mode)
930 ata_revision2speed(int revision)
944 ata_speed2revision(u_int speed)
961 ata_identify_match(caddr_t identbuffer, caddr_t table_entry)
963 struct scsi_inquiry_pattern *entry;
964 struct ata_params *ident;
966 entry = (struct scsi_inquiry_pattern *)table_entry;
967 ident = (struct ata_params *)identbuffer;
969 if ((cam_strmatch(ident->model, entry->product,
970 sizeof(ident->model)) == 0)
971 && (cam_strmatch(ident->revision, entry->revision,
972 sizeof(ident->revision)) == 0)) {
979 ata_static_identify_match(caddr_t identbuffer, caddr_t table_entry)
981 struct scsi_static_inquiry_pattern *entry;
982 struct ata_params *ident;
984 entry = (struct scsi_static_inquiry_pattern *)table_entry;
985 ident = (struct ata_params *)identbuffer;
987 if ((cam_strmatch(ident->model, entry->product,
988 sizeof(ident->model)) == 0)
989 && (cam_strmatch(ident->revision, entry->revision,
990 sizeof(ident->revision)) == 0)) {
997 semb_receive_diagnostic_results(struct ccb_ataio *ataio,
998 u_int32_t retries, void (*cbfcnp)(struct cam_periph *, union ccb*),
999 uint8_t tag_action, int pcv, uint8_t page_code,
1000 uint8_t *data_ptr, uint16_t length, uint32_t timeout)
1003 length = min(length, 1020);
1004 length = (length + 3) & ~3;
1005 cam_fill_ataio(ataio,
1008 /*flags*/CAM_DIR_IN,
1013 ata_28bit_cmd(ataio, ATA_SEP_ATTN,
1014 pcv ? page_code : 0, 0x02, length / 4);
1018 semb_send_diagnostic(struct ccb_ataio *ataio,
1019 u_int32_t retries, void (*cbfcnp)(struct cam_periph *, union ccb *),
1020 uint8_t tag_action, uint8_t *data_ptr, uint16_t length, uint32_t timeout)
1023 length = min(length, 1020);
1024 length = (length + 3) & ~3;
1025 cam_fill_ataio(ataio,
1028 /*flags*/length ? CAM_DIR_OUT : CAM_DIR_NONE,
1033 ata_28bit_cmd(ataio, ATA_SEP_ATTN,
1034 length > 0 ? data_ptr[0] : 0, 0x82, length / 4);
1038 semb_read_buffer(struct ccb_ataio *ataio,
1039 u_int32_t retries, void (*cbfcnp)(struct cam_periph *, union ccb*),
1040 uint8_t tag_action, uint8_t page_code,
1041 uint8_t *data_ptr, uint16_t length, uint32_t timeout)
1044 length = min(length, 1020);
1045 length = (length + 3) & ~3;
1046 cam_fill_ataio(ataio,
1049 /*flags*/CAM_DIR_IN,
1054 ata_28bit_cmd(ataio, ATA_SEP_ATTN,
1055 page_code, 0x00, length / 4);
1059 semb_write_buffer(struct ccb_ataio *ataio,
1060 u_int32_t retries, void (*cbfcnp)(struct cam_periph *, union ccb *),
1061 uint8_t tag_action, uint8_t *data_ptr, uint16_t length, uint32_t timeout)
1064 length = min(length, 1020);
1065 length = (length + 3) & ~3;
1066 cam_fill_ataio(ataio,
1069 /*flags*/length ? CAM_DIR_OUT : CAM_DIR_NONE,
1074 ata_28bit_cmd(ataio, ATA_SEP_ATTN,
1075 length > 0 ? data_ptr[0] : 0, 0x80, length / 4);
1080 ata_zac_mgmt_out(struct ccb_ataio *ataio, uint32_t retries,
1081 void (*cbfcnp)(struct cam_periph *, union ccb *),
1082 int use_ncq, uint8_t zm_action, uint64_t zone_id,
1083 uint8_t zone_flags, uint16_t sector_count, uint8_t *data_ptr,
1084 uint32_t dxfer_len, uint32_t timeout)
1086 uint8_t command_out, ata_flags;
1087 uint16_t features_out, sectors_out;
1091 command_out = ATA_ZAC_MANAGEMENT_OUT;
1092 features_out = (zm_action & 0xf) | (zone_flags << 8);
1093 if (dxfer_len == 0) {
1097 ata_flags = CAM_ATAIO_DMA;
1098 /* XXX KDM use sector count? */
1099 sectors_out = ((dxfer_len >> 9) & 0xffff);
1103 if (dxfer_len == 0) {
1104 command_out = ATA_NCQ_NON_DATA;
1105 features_out = ATA_NCQ_ZAC_MGMT_OUT;
1108 command_out = ATA_SEND_FPDMA_QUEUED;
1110 /* Note that we're defaulting to normal priority */
1111 sectors_out = ATA_SFPDMA_ZAC_MGMT_OUT << 8;
1114 * For SEND FPDMA QUEUED, the transfer length is
1115 * encoded in the FEATURE register, and 0 means
1116 * that 65536 512 byte blocks are to be tranferred.
1117 * In practice, it seems unlikely that we'll see
1118 * a transfer that large.
1120 if (dxfer_len == (65536 * 512)) {
1124 * Yes, the caller can theoretically send a
1125 * transfer larger than we can handle.
1126 * Anyone using this function needs enough
1127 * knowledge to avoid doing that.
1129 features_out = ((dxfer_len >> 9) & 0xffff);
1132 auxiliary = (zm_action & 0xf) | (zone_flags << 8);
1134 ata_flags = CAM_ATAIO_FPDMA;
1137 cam_fill_ataio(ataio,
1138 /*retries*/ retries,
1140 /*flags*/ (dxfer_len > 0) ? CAM_DIR_OUT : CAM_DIR_NONE,
1142 /*data_ptr*/ data_ptr,
1143 /*dxfer_len*/ dxfer_len,
1144 /*timeout*/ timeout);
1146 ata_48bit_cmd(ataio,
1147 /*cmd*/ command_out,
1148 /*features*/ features_out,
1150 /*sector_count*/ sectors_out);
1152 ataio->cmd.flags |= ata_flags;
1153 if (auxiliary != 0) {
1154 ataio->ata_flags |= ATA_FLAG_AUX;
1155 ataio->aux = auxiliary;
1160 ata_zac_mgmt_in(struct ccb_ataio *ataio, uint32_t retries,
1161 void (*cbfcnp)(struct cam_periph *, union ccb *),
1162 int use_ncq, uint8_t zm_action, uint64_t zone_id,
1163 uint8_t zone_flags, uint8_t *data_ptr, uint32_t dxfer_len,
1166 uint8_t command_out, ata_flags;
1167 uint16_t features_out, sectors_out;
1171 command_out = ATA_ZAC_MANAGEMENT_IN;
1172 /* XXX KDM put a macro here */
1173 features_out = (zm_action & 0xf) | (zone_flags << 8);
1174 ata_flags = CAM_ATAIO_DMA;
1175 sectors_out = ((dxfer_len >> 9) & 0xffff);
1178 command_out = ATA_RECV_FPDMA_QUEUED;
1179 sectors_out = ATA_RFPDMA_ZAC_MGMT_IN << 8;
1180 auxiliary = (zm_action & 0xf) | (zone_flags << 8);
1181 ata_flags = CAM_ATAIO_FPDMA;
1183 * For RECEIVE FPDMA QUEUED, the transfer length is
1184 * encoded in the FEATURE register, and 0 means
1185 * that 65536 512 byte blocks are to be tranferred.
1186 * In practice, it is unlikely we will see a transfer that
1189 if (dxfer_len == (65536 * 512)) {
1193 * Yes, the caller can theoretically request a
1194 * transfer larger than we can handle.
1195 * Anyone using this function needs enough
1196 * knowledge to avoid doing that.
1198 features_out = ((dxfer_len >> 9) & 0xffff);
1202 cam_fill_ataio(ataio,
1203 /*retries*/ retries,
1205 /*flags*/ CAM_DIR_IN,
1207 /*data_ptr*/ data_ptr,
1208 /*dxfer_len*/ dxfer_len,
1209 /*timeout*/ timeout);
1211 ata_48bit_cmd(ataio,
1212 /*cmd*/ command_out,
1213 /*features*/ features_out,
1215 /*sector_count*/ sectors_out);
1217 ataio->cmd.flags |= ata_flags;
1218 if (auxiliary != 0) {
1219 ataio->ata_flags |= ATA_FLAG_AUX;
1220 ataio->aux = auxiliary;