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");
93 switch (cmd->features) {
94 case 0x01: return ("DSM_XL TRIM");
97 case 0x08: return ("DEVICE_RESET");
98 case 0x0b: return ("REQUEST_SENSE_DATA_EXT");
99 case 0x12: return ("GET_PHYSICAL_ELEMENT_STATUS");
100 case 0x20: return ("READ");
101 case 0x24: return ("READ48");
102 case 0x25: return ("READ_DMA48");
103 case 0x26: return ("READ_DMA_QUEUED48");
104 case 0x27: return ("READ_NATIVE_MAX_ADDRESS48");
105 case 0x29: return ("READ_MUL48");
106 case 0x2a: return ("READ_STREAM_DMA48");
107 case 0x2b: return ("READ_STREAM48");
108 case 0x2f: return ("READ_LOG_EXT");
109 case 0x30: return ("WRITE");
110 case 0x34: return ("WRITE48");
111 case 0x35: return ("WRITE_DMA48");
112 case 0x36: return ("WRITE_DMA_QUEUED48");
113 case 0x37: return ("SET_MAX_ADDRESS48");
114 case 0x39: return ("WRITE_MUL48");
115 case 0x3a: return ("WRITE_STREAM_DMA48");
116 case 0x3b: return ("WRITE_STREAM48");
117 case 0x3d: return ("WRITE_DMA_FUA48");
118 case 0x3e: return ("WRITE_DMA_QUEUED_FUA48");
119 case 0x3f: return ("WRITE_LOG_EXT");
120 case 0x40: return ("READ_VERIFY");
121 case 0x42: return ("READ_VERIFY48");
123 switch (cmd->features) {
124 case 0x01: return ("ZERO_EXT TRIM");
128 switch (cmd->features) {
129 case 0x55: return ("WRITE_UNCORRECTABLE48 PSEUDO");
130 case 0xaa: return ("WRITE_UNCORRECTABLE48 FLAGGED");
132 return "WRITE_UNCORRECTABLE48";
133 case 0x47: return ("READ_LOG_DMA_EXT");
134 case 0x4a: return ("ZAC_MANAGEMENT_IN");
135 case 0x51: return ("CONFIGURE_STREAM");
136 case 0x57: return ("WRITE_LOG_DMA_EXT");
137 case 0x5b: return ("TRUSTED_NON_DATA");
138 case 0x5c: return ("TRUSTED_RECEIVE");
139 case 0x5d: return ("TRUSTED_RECEIVE_DMA");
140 case 0x5e: return ("TRUSTED_SEND");
141 case 0x5f: return ("TRUSTED_SEND_DMA");
142 case 0x60: return ("READ_FPDMA_QUEUED");
143 case 0x61: return ("WRITE_FPDMA_QUEUED");
145 switch (cmd->features & 0xf) {
146 case 0x00: return ("NCQ_NON_DATA ABORT NCQ QUEUE");
147 case 0x01: return ("NCQ_NON_DATA DEADLINE HANDLING");
148 case 0x02: return ("NCQ_NON_DATA HYBRID DEMOTE BY SIZE");
149 case 0x03: return ("NCQ_NON_DATA HYBRID CHANGE BY LBA RANGE");
150 case 0x04: return ("NCQ_NON_DATA HYBRID CONTROL");
151 case 0x05: return ("NCQ_NON_DATA SET FEATURES");
153 * XXX KDM need common decoding between NCQ and non-NCQ
154 * versions of SET FEATURES.
156 case 0x06: return ("NCQ_NON_DATA ZERO EXT");
157 case 0x07: return ("NCQ_NON_DATA ZAC MANAGEMENT OUT");
159 return ("NCQ_NON_DATA");
161 switch (cmd->sector_count_exp & 0xf) {
162 case 0x00: return ("SEND_FPDMA_QUEUED DATA SET MANAGEMENT");
163 case 0x01: return ("SEND_FPDMA_QUEUED HYBRID EVICT");
164 case 0x02: return ("SEND_FPDMA_QUEUED WRITE LOG DMA EXT");
165 case 0x03: return ("SEND_FPDMA_QUEUED ZAC MANAGEMENT OUT");
166 case 0x04: return ("SEND_FPDMA_QUEUED DATA SET MANAGEMENT XL");
168 return ("SEND_FPDMA_QUEUED");
170 switch (cmd->sector_count_exp & 0xf) {
171 case 0x01: return ("RECEIVE_FPDMA_QUEUED READ LOG DMA EXT");
172 case 0x02: return ("RECEIVE_FPDMA_QUEUED ZAC MANAGEMENT IN");
174 return ("RECEIVE_FPDMA_QUEUED");
176 if (cmd->features == 0xec)
177 return ("SEP_ATTN IDENTIFY");
178 switch (cmd->lba_low) {
179 case 0x00: return ("SEP_ATTN READ BUFFER");
180 case 0x02: return ("SEP_ATTN RECEIVE DIAGNOSTIC RESULTS");
181 case 0x80: return ("SEP_ATTN WRITE BUFFER");
182 case 0x82: return ("SEP_ATTN SEND DIAGNOSTIC");
185 case 0x70: return ("SEEK");
186 case 0x77: return ("SET_DATE_TIME_EXT");
187 case 0x78: return ("ACCESSIBLE_MAX_ADDRESS_CONFIGURATION");
188 case 0x7C: return ("REMOVE_ELEMENT_AND_TRUNCATE");
189 case 0x87: return ("CFA_TRANSLATE_SECTOR");
190 case 0x90: return ("EXECUTE_DEVICE_DIAGNOSTIC");
191 case 0x92: return ("DOWNLOAD_MICROCODE");
192 case 0x93: return ("DOWNLOAD_MICROCODE_DMA");
193 case 0x9a: return ("ZAC_MANAGEMENT_OUT");
194 case 0xa0: return ("PACKET");
195 case 0xa1: return ("ATAPI_IDENTIFY");
196 case 0xa2: return ("SERVICE");
198 switch(cmd->features) {
199 case 0xd0: return ("SMART READ ATTR VALUES");
200 case 0xd1: return ("SMART READ ATTR THRESHOLDS");
201 case 0xd3: return ("SMART SAVE ATTR VALUES");
202 case 0xd4: return ("SMART EXECUTE OFFLINE IMMEDIATE");
203 case 0xd5: return ("SMART READ LOG");
204 case 0xd6: return ("SMART WRITE LOG");
205 case 0xd8: return ("SMART ENABLE OPERATION");
206 case 0xd9: return ("SMART DISABLE OPERATION");
207 case 0xda: return ("SMART RETURN STATUS");
210 case 0xb1: return ("DEVICE CONFIGURATION");
211 case 0xb2: return ("SET_SECTOR_CONFIGURATION_EXT");
212 case 0xb4: return ("SANITIZE_DEVICE");
213 case 0xc0: return ("CFA_ERASE");
214 case 0xc4: return ("READ_MUL");
215 case 0xc5: return ("WRITE_MUL");
216 case 0xc6: return ("SET_MULTI");
217 case 0xc7: return ("READ_DMA_QUEUED");
218 case 0xc8: return ("READ_DMA");
219 case 0xca: return ("WRITE_DMA");
220 case 0xcc: return ("WRITE_DMA_QUEUED");
221 case 0xcd: return ("CFA_WRITE_MULTIPLE_WITHOUT_ERASE");
222 case 0xce: return ("WRITE_MUL_FUA48");
223 case 0xd1: return ("CHECK_MEDIA_CARD_TYPE");
224 case 0xda: return ("GET_MEDIA_STATUS");
225 case 0xde: return ("MEDIA_LOCK");
226 case 0xdf: return ("MEDIA_UNLOCK");
227 case 0xe0: return ("STANDBY_IMMEDIATE");
228 case 0xe1: return ("IDLE_IMMEDIATE");
229 case 0xe2: return ("STANDBY");
230 case 0xe3: return ("IDLE");
231 case 0xe4: return ("READ_BUFFER/PM");
232 case 0xe5: return ("CHECK_POWER_MODE");
233 case 0xe6: return ("SLEEP");
234 case 0xe7: return ("FLUSHCACHE");
235 case 0xe8: return ("WRITE_BUFFER/PM");
236 case 0xe9: return ("READ_BUFFER_DMA");
237 case 0xea: return ("FLUSHCACHE48");
238 case 0xeb: return ("WRITE_BUFFER_DMA");
239 case 0xec: return ("ATA_IDENTIFY");
240 case 0xed: return ("MEDIA_EJECT");
243 * XXX KDM need common decoding between NCQ and non-NCQ
244 * versions of SET FEATURES.
246 switch (cmd->features) {
247 case 0x02: return ("SETFEATURES ENABLE WCACHE");
248 case 0x03: return ("SETFEATURES SET TRANSFER MODE");
249 case 0x05: return ("SETFEATURES ENABLE APM");
250 case 0x06: return ("SETFEATURES ENABLE PUIS");
251 case 0x07: return ("SETFEATURES SPIN-UP");
252 case 0x0b: return ("SETFEATURES ENABLE WRITE READ VERIFY");
253 case 0x0c: return ("SETFEATURES ENABLE DEVICE LIFE CONTROL");
254 case 0x10: return ("SETFEATURES ENABLE SATA FEATURE");
255 case 0x41: return ("SETFEATURES ENABLE FREEFALL CONTROL");
256 case 0x43: return ("SETFEATURES SET MAX HOST INT SECT TIMES");
257 case 0x45: return ("SETFEATURES SET RATE BASIS");
258 case 0x4a: return ("SETFEATURES EXTENDED POWER CONDITIONS");
259 case 0x50: return ("SETFEATURES ADVANCED BACKGROUD OPERATION");
260 case 0x55: return ("SETFEATURES DISABLE RCACHE");
261 case 0x5d: return ("SETFEATURES ENABLE RELIRQ");
262 case 0x5e: return ("SETFEATURES ENABLE SRVIRQ");
263 case 0x62: return ("SETFEATURES LONG PHYS SECT ALIGN ERC");
264 case 0x63: return ("SETFEATURES DSN");
265 case 0x66: return ("SETFEATURES DISABLE DEFAULTS");
266 case 0x82: return ("SETFEATURES DISABLE WCACHE");
267 case 0x85: return ("SETFEATURES DISABLE APM");
268 case 0x86: return ("SETFEATURES DISABLE PUIS");
269 case 0x8b: return ("SETFEATURES DISABLE WRITE READ VERIFY");
270 case 0x8c: return ("SETFEATURES DISABLE DEVICE LIFE CONTROL");
271 case 0x90: return ("SETFEATURES DISABLE SATA FEATURE");
272 case 0xaa: return ("SETFEATURES ENABLE RCACHE");
273 case 0xC1: return ("SETFEATURES DISABLE FREEFALL CONTROL");
274 case 0xC3: return ("SETFEATURES SENSE DATA REPORTING");
275 case 0xC4: return ("SETFEATURES NCQ SENSE DATA RETURN");
276 case 0xCC: return ("SETFEATURES ENABLE DEFAULTS");
277 case 0xdd: return ("SETFEATURES DISABLE RELIRQ");
278 case 0xde: return ("SETFEATURES DISABLE SRVIRQ");
280 return "SETFEATURES";
281 case 0xf1: return ("SECURITY_SET_PASSWORD");
282 case 0xf2: return ("SECURITY_UNLOCK");
283 case 0xf3: return ("SECURITY_ERASE_PREPARE");
284 case 0xf4: return ("SECURITY_ERASE_UNIT");
285 case 0xf5: return ("SECURITY_FREEZE_LOCK");
286 case 0xf6: return ("SECURITY_DISABLE_PASSWORD");
287 case 0xf8: return ("READ_NATIVE_MAX_ADDRESS");
288 case 0xf9: return ("SET_MAX_ADDRESS");
294 ata_cmd_string(struct ata_cmd *cmd, char *cmd_string, size_t len)
302 sbuf_new(&sb, cmd_string, len, SBUF_FIXEDLEN);
303 ata_cmd_sbuf(cmd, &sb);
305 error = sbuf_finish(&sb);
306 if (error != 0 && error != ENOMEM)
309 return(sbuf_data(&sb));
313 ata_cmd_sbuf(struct ata_cmd *cmd, struct sbuf *sb)
315 sbuf_printf(sb, "%02x %02x %02x %02x "
316 "%02x %02x %02x %02x %02x %02x %02x %02x",
317 cmd->command, cmd->features,
318 cmd->lba_low, cmd->lba_mid, cmd->lba_high, cmd->device,
319 cmd->lba_low_exp, cmd->lba_mid_exp, cmd->lba_high_exp,
320 cmd->features_exp, cmd->sector_count, cmd->sector_count_exp);
324 ata_res_string(struct ata_res *res, char *res_string, size_t len)
332 sbuf_new(&sb, res_string, len, SBUF_FIXEDLEN);
333 ata_res_sbuf(res, &sb);
335 error = sbuf_finish(&sb);
336 if (error != 0 && error != ENOMEM)
339 return(sbuf_data(&sb));
343 ata_res_sbuf(struct ata_res *res, struct sbuf *sb)
346 sbuf_printf(sb, "%02x %02x %02x %02x "
347 "%02x %02x %02x %02x %02x %02x %02x",
348 res->status, res->error,
349 res->lba_low, res->lba_mid, res->lba_high, res->device,
350 res->lba_low_exp, res->lba_mid_exp, res->lba_high_exp,
351 res->sector_count, res->sector_count_exp);
357 * ata_command_sbuf() returns 0 for success and -1 for failure.
360 ata_command_sbuf(struct ccb_ataio *ataio, struct sbuf *sb)
363 sbuf_printf(sb, "%s. ACB: ",
364 ata_op_string(&ataio->cmd));
365 ata_cmd_sbuf(&ataio->cmd, sb);
371 * ata_status_abuf() returns 0 for success and -1 for failure.
374 ata_status_sbuf(struct ccb_ataio *ataio, struct sbuf *sb)
377 sbuf_printf(sb, "ATA status: %02x (%s%s%s%s%s%s%s%s)",
379 (ataio->res.status & 0x80) ? "BSY " : "",
380 (ataio->res.status & 0x40) ? "DRDY " : "",
381 (ataio->res.status & 0x20) ? "DF " : "",
382 (ataio->res.status & 0x10) ? "SERV " : "",
383 (ataio->res.status & 0x08) ? "DRQ " : "",
384 (ataio->res.status & 0x04) ? "CORR " : "",
385 (ataio->res.status & 0x02) ? "IDX " : "",
386 (ataio->res.status & 0x01) ? "ERR" : "");
387 if (ataio->res.status & 1) {
388 sbuf_printf(sb, ", error: %02x (%s%s%s%s%s%s%s%s)",
390 (ataio->res.error & 0x80) ? "ICRC " : "",
391 (ataio->res.error & 0x40) ? "UNC " : "",
392 (ataio->res.error & 0x20) ? "MC " : "",
393 (ataio->res.error & 0x10) ? "IDNF " : "",
394 (ataio->res.error & 0x08) ? "MCR " : "",
395 (ataio->res.error & 0x04) ? "ABRT " : "",
396 (ataio->res.error & 0x02) ? "NM " : "",
397 (ataio->res.error & 0x01) ? "ILI" : "");
404 ata_print_ident(struct ata_params *ident_data)
407 char ata[12], sata[12];
409 ata_print_ident_short(ident_data);
411 proto = (ident_data->config == ATA_PROTO_CFA) ? "CFA" :
412 (ident_data->config & ATA_PROTO_ATAPI) ? "ATAPI" : "ATA";
413 if (ata_version(ident_data->version_major) == 0) {
414 snprintf(ata, sizeof(ata), "%s", proto);
415 } else if (ata_version(ident_data->version_major) <= 7) {
416 snprintf(ata, sizeof(ata), "%s-%d", proto,
417 ata_version(ident_data->version_major));
418 } else if (ata_version(ident_data->version_major) == 8) {
419 snprintf(ata, sizeof(ata), "%s8-ACS", proto);
421 snprintf(ata, sizeof(ata), "ACS-%d %s",
422 ata_version(ident_data->version_major) - 7, proto);
424 if (ident_data->satacapabilities && ident_data->satacapabilities != 0xffff) {
425 if (ident_data->satacapabilities & ATA_SATA_GEN3)
426 snprintf(sata, sizeof(sata), " SATA 3.x");
427 else if (ident_data->satacapabilities & ATA_SATA_GEN2)
428 snprintf(sata, sizeof(sata), " SATA 2.x");
429 else if (ident_data->satacapabilities & ATA_SATA_GEN1)
430 snprintf(sata, sizeof(sata), " SATA 1.x");
432 snprintf(sata, sizeof(sata), " SATA");
435 printf(" %s%s device\n", ata, sata);
439 ata_print_ident_sbuf(struct ata_params *ident_data, struct sbuf *sb)
441 const char *proto, *sata;
444 ata_print_ident_short_sbuf(ident_data, sb);
445 sbuf_printf(sb, " ");
447 proto = (ident_data->config == ATA_PROTO_CFA) ? "CFA" :
448 (ident_data->config & ATA_PROTO_ATAPI) ? "ATAPI" : "ATA";
449 version = ata_version(ident_data->version_major);
453 sbuf_printf(sb, "%s", proto);
462 sbuf_printf(sb, "%s-%d", proto, version);
465 sbuf_printf(sb, "%s8-ACS", proto);
468 sbuf_printf(sb, "ACS-%d %s", version - 7, proto);
472 if (ident_data->satacapabilities && ident_data->satacapabilities != 0xffff) {
473 if (ident_data->satacapabilities & ATA_SATA_GEN3)
475 else if (ident_data->satacapabilities & ATA_SATA_GEN2)
477 else if (ident_data->satacapabilities & ATA_SATA_GEN1)
483 sbuf_printf(sb, "%s device\n", sata);
487 ata_print_ident_short(struct ata_params *ident_data)
489 char product[48], revision[16];
491 cam_strvis(product, ident_data->model, sizeof(ident_data->model),
493 cam_strvis(revision, ident_data->revision, sizeof(ident_data->revision),
495 printf("<%s %s>", product, revision);
499 ata_print_ident_short_sbuf(struct ata_params *ident_data, struct sbuf *sb)
502 sbuf_printf(sb, "<");
503 cam_strvis_sbuf(sb, ident_data->model, sizeof(ident_data->model), 0);
504 sbuf_printf(sb, " ");
505 cam_strvis_sbuf(sb, ident_data->revision, sizeof(ident_data->revision), 0);
506 sbuf_printf(sb, ">");
510 semb_print_ident(struct sep_identify_data *ident_data)
514 semb_print_ident_short(ident_data);
515 cam_strvis(in, ident_data->interface_id, 6, sizeof(in));
516 cam_strvis(ins, ident_data->interface_rev, 4, sizeof(ins));
517 printf(" SEMB %s %s device\n", in, ins);
521 semb_print_ident_sbuf(struct sep_identify_data *ident_data, struct sbuf *sb)
524 semb_print_ident_short_sbuf(ident_data, sb);
526 sbuf_printf(sb, " SEMB ");
527 cam_strvis_sbuf(sb, ident_data->interface_id, 6, 0);
528 sbuf_printf(sb, " ");
529 cam_strvis_sbuf(sb, ident_data->interface_rev, 4, 0);
530 sbuf_printf(sb, " device\n");
534 semb_print_ident_short(struct sep_identify_data *ident_data)
536 char vendor[9], product[17], revision[5], fw[5];
538 cam_strvis(vendor, ident_data->vendor_id, 8, sizeof(vendor));
539 cam_strvis(product, ident_data->product_id, 16, sizeof(product));
540 cam_strvis(revision, ident_data->product_rev, 4, sizeof(revision));
541 cam_strvis(fw, ident_data->firmware_rev, 4, sizeof(fw));
542 printf("<%s %s %s %s>", vendor, product, revision, fw);
546 semb_print_ident_short_sbuf(struct sep_identify_data *ident_data, struct sbuf *sb)
549 sbuf_printf(sb, "<");
550 cam_strvis_sbuf(sb, ident_data->vendor_id, 8, 0);
551 sbuf_printf(sb, " ");
552 cam_strvis_sbuf(sb, ident_data->product_id, 16, 0);
553 sbuf_printf(sb, " ");
554 cam_strvis_sbuf(sb, ident_data->product_rev, 4, 0);
555 sbuf_printf(sb, " ");
556 cam_strvis_sbuf(sb, ident_data->firmware_rev, 4, 0);
557 sbuf_printf(sb, ">");
561 ata_logical_sector_size(struct ata_params *ident_data)
563 if ((ident_data->pss & ATA_PSS_VALID_MASK) == ATA_PSS_VALID_VALUE &&
564 (ident_data->pss & ATA_PSS_LSSABOVE512)) {
565 return (((u_int32_t)ident_data->lss_1 |
566 ((u_int32_t)ident_data->lss_2 << 16)) * 2);
572 ata_physical_sector_size(struct ata_params *ident_data)
574 if ((ident_data->pss & ATA_PSS_VALID_MASK) == ATA_PSS_VALID_VALUE) {
575 if (ident_data->pss & ATA_PSS_MULTLS) {
576 return ((uint64_t)ata_logical_sector_size(ident_data) *
577 (1 << (ident_data->pss & ATA_PSS_LSPPS)));
579 return (uint64_t)ata_logical_sector_size(ident_data);
586 ata_logical_sector_offset(struct ata_params *ident_data)
588 if ((ident_data->lsalign & 0xc000) == 0x4000) {
589 return ((uint64_t)ata_logical_sector_size(ident_data) *
590 (ident_data->lsalign & 0x3fff));
596 ata_28bit_cmd(struct ccb_ataio *ataio, uint8_t cmd, uint8_t features,
597 uint32_t lba, uint8_t sector_count)
599 bzero(&ataio->cmd, sizeof(ataio->cmd));
600 ataio->cmd.flags = 0;
601 if (cmd == ATA_READ_DMA ||
602 cmd == ATA_READ_DMA_QUEUED ||
603 cmd == ATA_WRITE_DMA ||
604 cmd == ATA_WRITE_DMA_QUEUED ||
605 cmd == ATA_TRUSTED_RECEIVE_DMA ||
606 cmd == ATA_TRUSTED_SEND_DMA ||
607 cmd == ATA_DOWNLOAD_MICROCODE_DMA ||
608 cmd == ATA_READ_BUFFER_DMA ||
609 cmd == ATA_WRITE_BUFFER_DMA)
610 ataio->cmd.flags |= CAM_ATAIO_DMA;
611 ataio->cmd.command = cmd;
612 ataio->cmd.features = features;
613 ataio->cmd.lba_low = lba;
614 ataio->cmd.lba_mid = lba >> 8;
615 ataio->cmd.lba_high = lba >> 16;
616 ataio->cmd.device = ATA_DEV_LBA | ((lba >> 24) & 0x0f);
617 ataio->cmd.sector_count = sector_count;
621 ata_48bit_cmd(struct ccb_ataio *ataio, uint8_t cmd, uint16_t features,
622 uint64_t lba, uint16_t sector_count)
625 ataio->cmd.flags = CAM_ATAIO_48BIT;
626 if (cmd == ATA_READ_DMA48 ||
627 cmd == ATA_READ_DMA_QUEUED48 ||
628 cmd == ATA_READ_STREAM_DMA48 ||
629 cmd == ATA_WRITE_DMA48 ||
630 cmd == ATA_WRITE_DMA_FUA48 ||
631 cmd == ATA_WRITE_DMA_QUEUED48 ||
632 cmd == ATA_WRITE_DMA_QUEUED_FUA48 ||
633 cmd == ATA_WRITE_STREAM_DMA48 ||
634 cmd == ATA_DATA_SET_MANAGEMENT ||
635 cmd == ATA_READ_LOG_DMA_EXT ||
636 cmd == ATA_WRITE_LOG_DMA_EXT)
637 ataio->cmd.flags |= CAM_ATAIO_DMA;
638 ataio->cmd.command = cmd;
639 ataio->cmd.features = features;
640 ataio->cmd.lba_low = lba;
641 ataio->cmd.lba_mid = lba >> 8;
642 ataio->cmd.lba_high = lba >> 16;
643 ataio->cmd.device = ATA_DEV_LBA;
644 ataio->cmd.lba_low_exp = lba >> 24;
645 ataio->cmd.lba_mid_exp = lba >> 32;
646 ataio->cmd.lba_high_exp = lba >> 40;
647 ataio->cmd.features_exp = features >> 8;
648 ataio->cmd.sector_count = sector_count;
649 ataio->cmd.sector_count_exp = sector_count >> 8;
650 ataio->cmd.control = 0;
654 ata_ncq_cmd(struct ccb_ataio *ataio, uint8_t cmd,
655 uint64_t lba, uint16_t sector_count)
658 ataio->cmd.flags = CAM_ATAIO_48BIT | CAM_ATAIO_FPDMA;
659 ataio->cmd.command = cmd;
660 ataio->cmd.features = sector_count;
661 ataio->cmd.lba_low = lba;
662 ataio->cmd.lba_mid = lba >> 8;
663 ataio->cmd.lba_high = lba >> 16;
664 ataio->cmd.device = ATA_DEV_LBA;
665 ataio->cmd.lba_low_exp = lba >> 24;
666 ataio->cmd.lba_mid_exp = lba >> 32;
667 ataio->cmd.lba_high_exp = lba >> 40;
668 ataio->cmd.features_exp = sector_count >> 8;
669 ataio->cmd.sector_count = 0;
670 ataio->cmd.sector_count_exp = 0;
671 ataio->cmd.control = 0;
675 ata_reset_cmd(struct ccb_ataio *ataio)
677 bzero(&ataio->cmd, sizeof(ataio->cmd));
678 ataio->cmd.flags = CAM_ATAIO_CONTROL | CAM_ATAIO_NEEDRESULT;
679 ataio->cmd.control = 0x04;
683 ata_pm_read_cmd(struct ccb_ataio *ataio, int reg, int port)
685 bzero(&ataio->cmd, sizeof(ataio->cmd));
686 ataio->cmd.flags = CAM_ATAIO_NEEDRESULT;
687 ataio->cmd.command = ATA_READ_PM;
688 ataio->cmd.features = reg;
689 ataio->cmd.device = port & 0x0f;
693 ata_pm_write_cmd(struct ccb_ataio *ataio, int reg, int port, uint32_t val)
695 bzero(&ataio->cmd, sizeof(ataio->cmd));
696 ataio->cmd.flags = 0;
697 ataio->cmd.command = ATA_WRITE_PM;
698 ataio->cmd.features = reg;
699 ataio->cmd.sector_count = val;
700 ataio->cmd.lba_low = val >> 8;
701 ataio->cmd.lba_mid = val >> 16;
702 ataio->cmd.lba_high = val >> 24;
703 ataio->cmd.device = port & 0x0f;
707 ata_read_log(struct ccb_ataio *ataio, uint32_t retries,
708 void (*cbfcnp)(struct cam_periph *, union ccb *),
709 uint32_t log_address, uint32_t page_number, uint16_t block_count,
710 uint32_t protocol, uint8_t *data_ptr, uint32_t dxfer_len,
715 cam_fill_ataio(ataio,
718 /*flags*/ CAM_DIR_IN,
720 /*data_ptr*/ data_ptr,
721 /*dxfer_len*/ dxfer_len,
722 /*timeout*/ timeout);
724 lba = (((uint64_t)page_number & 0xff00) << 32) |
725 ((page_number & 0x00ff) << 8) |
726 (log_address & 0xff);
729 /*cmd*/ (protocol & CAM_ATAIO_DMA) ? ATA_READ_LOG_DMA_EXT :
733 /*sector_count*/ block_count);
737 ata_bswap(int8_t *buf, int len)
739 u_int16_t *ptr = (u_int16_t*)(buf + len);
741 while (--ptr >= (u_int16_t*)buf)
742 *ptr = be16toh(*ptr);
746 ata_btrim(int8_t *buf, int len)
750 for (ptr = buf; ptr < buf+len; ++ptr)
751 if (!*ptr || *ptr == '_')
753 for (ptr = buf + len - 1; ptr >= buf && *ptr == ' '; --ptr)
758 ata_bpack(int8_t *src, int8_t *dst, int len)
762 for (i = j = blank = 0 ; i < len; i++) {
763 if (blank && src[i] == ' ') continue;
764 if (blank && src[i] != ' ') {
781 ata_max_pmode(struct ata_params *ap)
783 if (ap->atavalid & ATA_FLAG_64_70) {
784 if (ap->apiomodes & 0x02)
786 if (ap->apiomodes & 0x01)
789 if (ap->mwdmamodes & 0x04)
791 if (ap->mwdmamodes & 0x02)
793 if (ap->mwdmamodes & 0x01)
795 if ((ap->retired_piomode & ATA_RETIRED_PIO_MASK) == 0x200)
797 if ((ap->retired_piomode & ATA_RETIRED_PIO_MASK) == 0x100)
799 if ((ap->retired_piomode & ATA_RETIRED_PIO_MASK) == 0x000)
805 ata_max_wmode(struct ata_params *ap)
807 if (ap->mwdmamodes & 0x04)
809 if (ap->mwdmamodes & 0x02)
811 if (ap->mwdmamodes & 0x01)
817 ata_max_umode(struct ata_params *ap)
819 if (ap->atavalid & ATA_FLAG_88) {
820 if (ap->udmamodes & 0x40)
822 if (ap->udmamodes & 0x20)
824 if (ap->udmamodes & 0x10)
826 if (ap->udmamodes & 0x08)
828 if (ap->udmamodes & 0x04)
830 if (ap->udmamodes & 0x02)
832 if (ap->udmamodes & 0x01)
839 ata_max_mode(struct ata_params *ap, int maxmode)
843 maxmode = ATA_DMA_MAX;
844 if (maxmode >= ATA_UDMA0 && ata_max_umode(ap) > 0)
845 return (min(maxmode, ata_max_umode(ap)));
846 if (maxmode >= ATA_WDMA0 && ata_max_wmode(ap) > 0)
847 return (min(maxmode, ata_max_wmode(ap)));
848 return (min(maxmode, ata_max_pmode(ap)));
852 ata_mode2string(int mode)
855 case -1: return "UNSUPPORTED";
856 case 0: return "NONE";
857 case ATA_PIO0: return "PIO0";
858 case ATA_PIO1: return "PIO1";
859 case ATA_PIO2: return "PIO2";
860 case ATA_PIO3: return "PIO3";
861 case ATA_PIO4: return "PIO4";
862 case ATA_WDMA0: return "WDMA0";
863 case ATA_WDMA1: return "WDMA1";
864 case ATA_WDMA2: return "WDMA2";
865 case ATA_UDMA0: return "UDMA0";
866 case ATA_UDMA1: return "UDMA1";
867 case ATA_UDMA2: return "UDMA2";
868 case ATA_UDMA3: return "UDMA3";
869 case ATA_UDMA4: return "UDMA4";
870 case ATA_UDMA5: return "UDMA5";
871 case ATA_UDMA6: return "UDMA6";
873 if (mode & ATA_DMA_MASK)
881 ata_string2mode(char *str)
883 if (!strcasecmp(str, "PIO0")) return (ATA_PIO0);
884 if (!strcasecmp(str, "PIO1")) return (ATA_PIO1);
885 if (!strcasecmp(str, "PIO2")) return (ATA_PIO2);
886 if (!strcasecmp(str, "PIO3")) return (ATA_PIO3);
887 if (!strcasecmp(str, "PIO4")) return (ATA_PIO4);
888 if (!strcasecmp(str, "WDMA0")) return (ATA_WDMA0);
889 if (!strcasecmp(str, "WDMA1")) return (ATA_WDMA1);
890 if (!strcasecmp(str, "WDMA2")) return (ATA_WDMA2);
891 if (!strcasecmp(str, "UDMA0")) return (ATA_UDMA0);
892 if (!strcasecmp(str, "UDMA16")) return (ATA_UDMA0);
893 if (!strcasecmp(str, "UDMA1")) return (ATA_UDMA1);
894 if (!strcasecmp(str, "UDMA25")) return (ATA_UDMA1);
895 if (!strcasecmp(str, "UDMA2")) return (ATA_UDMA2);
896 if (!strcasecmp(str, "UDMA33")) return (ATA_UDMA2);
897 if (!strcasecmp(str, "UDMA3")) return (ATA_UDMA3);
898 if (!strcasecmp(str, "UDMA44")) return (ATA_UDMA3);
899 if (!strcasecmp(str, "UDMA4")) return (ATA_UDMA4);
900 if (!strcasecmp(str, "UDMA66")) return (ATA_UDMA4);
901 if (!strcasecmp(str, "UDMA5")) return (ATA_UDMA5);
902 if (!strcasecmp(str, "UDMA100")) return (ATA_UDMA5);
903 if (!strcasecmp(str, "UDMA6")) return (ATA_UDMA6);
904 if (!strcasecmp(str, "UDMA133")) return (ATA_UDMA6);
910 ata_mode2speed(int mode)
948 ata_revision2speed(int revision)
962 ata_speed2revision(u_int speed)
979 ata_identify_match(caddr_t identbuffer, caddr_t table_entry)
981 struct scsi_inquiry_pattern *entry;
982 struct ata_params *ident;
984 entry = (struct scsi_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 ata_static_identify_match(caddr_t identbuffer, caddr_t table_entry)
999 struct scsi_static_inquiry_pattern *entry;
1000 struct ata_params *ident;
1002 entry = (struct scsi_static_inquiry_pattern *)table_entry;
1003 ident = (struct ata_params *)identbuffer;
1005 if ((cam_strmatch(ident->model, entry->product,
1006 sizeof(ident->model)) == 0)
1007 && (cam_strmatch(ident->revision, entry->revision,
1008 sizeof(ident->revision)) == 0)) {
1015 semb_receive_diagnostic_results(struct ccb_ataio *ataio,
1016 u_int32_t retries, void (*cbfcnp)(struct cam_periph *, union ccb*),
1017 uint8_t tag_action, int pcv, uint8_t page_code,
1018 uint8_t *data_ptr, uint16_t length, uint32_t timeout)
1021 length = min(length, 1020);
1022 length = (length + 3) & ~3;
1023 cam_fill_ataio(ataio,
1026 /*flags*/CAM_DIR_IN,
1031 ata_28bit_cmd(ataio, ATA_SEP_ATTN,
1032 pcv ? page_code : 0, 0x02, length / 4);
1036 semb_send_diagnostic(struct ccb_ataio *ataio,
1037 u_int32_t retries, void (*cbfcnp)(struct cam_periph *, union ccb *),
1038 uint8_t tag_action, uint8_t *data_ptr, uint16_t length, uint32_t timeout)
1041 length = min(length, 1020);
1042 length = (length + 3) & ~3;
1043 cam_fill_ataio(ataio,
1046 /*flags*/length ? CAM_DIR_OUT : CAM_DIR_NONE,
1051 ata_28bit_cmd(ataio, ATA_SEP_ATTN,
1052 length > 0 ? data_ptr[0] : 0, 0x82, length / 4);
1056 semb_read_buffer(struct ccb_ataio *ataio,
1057 u_int32_t retries, void (*cbfcnp)(struct cam_periph *, union ccb*),
1058 uint8_t tag_action, uint8_t page_code,
1059 uint8_t *data_ptr, uint16_t length, uint32_t timeout)
1062 length = min(length, 1020);
1063 length = (length + 3) & ~3;
1064 cam_fill_ataio(ataio,
1067 /*flags*/CAM_DIR_IN,
1072 ata_28bit_cmd(ataio, ATA_SEP_ATTN,
1073 page_code, 0x00, length / 4);
1077 semb_write_buffer(struct ccb_ataio *ataio,
1078 u_int32_t retries, void (*cbfcnp)(struct cam_periph *, union ccb *),
1079 uint8_t tag_action, uint8_t *data_ptr, uint16_t length, uint32_t timeout)
1082 length = min(length, 1020);
1083 length = (length + 3) & ~3;
1084 cam_fill_ataio(ataio,
1087 /*flags*/length ? CAM_DIR_OUT : CAM_DIR_NONE,
1092 ata_28bit_cmd(ataio, ATA_SEP_ATTN,
1093 length > 0 ? data_ptr[0] : 0, 0x80, length / 4);
1098 ata_zac_mgmt_out(struct ccb_ataio *ataio, uint32_t retries,
1099 void (*cbfcnp)(struct cam_periph *, union ccb *),
1100 int use_ncq, uint8_t zm_action, uint64_t zone_id,
1101 uint8_t zone_flags, uint16_t sector_count, uint8_t *data_ptr,
1102 uint32_t dxfer_len, uint32_t timeout)
1104 uint8_t command_out, ata_flags;
1105 uint16_t features_out, sectors_out;
1109 command_out = ATA_ZAC_MANAGEMENT_OUT;
1110 features_out = (zm_action & 0xf) | (zone_flags << 8);
1111 if (dxfer_len == 0) {
1115 ata_flags = CAM_ATAIO_DMA;
1116 /* XXX KDM use sector count? */
1117 sectors_out = ((dxfer_len >> 9) & 0xffff);
1121 if (dxfer_len == 0) {
1122 command_out = ATA_NCQ_NON_DATA;
1123 features_out = ATA_NCQ_ZAC_MGMT_OUT;
1126 command_out = ATA_SEND_FPDMA_QUEUED;
1128 /* Note that we're defaulting to normal priority */
1129 sectors_out = ATA_SFPDMA_ZAC_MGMT_OUT << 8;
1132 * For SEND FPDMA QUEUED, the transfer length is
1133 * encoded in the FEATURE register, and 0 means
1134 * that 65536 512 byte blocks are to be tranferred.
1135 * In practice, it seems unlikely that we'll see
1136 * a transfer that large.
1138 if (dxfer_len == (65536 * 512)) {
1142 * Yes, the caller can theoretically send a
1143 * transfer larger than we can handle.
1144 * Anyone using this function needs enough
1145 * knowledge to avoid doing that.
1147 features_out = ((dxfer_len >> 9) & 0xffff);
1150 auxiliary = (zm_action & 0xf) | (zone_flags << 8);
1152 ata_flags = CAM_ATAIO_FPDMA;
1155 cam_fill_ataio(ataio,
1156 /*retries*/ retries,
1158 /*flags*/ (dxfer_len > 0) ? CAM_DIR_OUT : CAM_DIR_NONE,
1160 /*data_ptr*/ data_ptr,
1161 /*dxfer_len*/ dxfer_len,
1162 /*timeout*/ timeout);
1164 ata_48bit_cmd(ataio,
1165 /*cmd*/ command_out,
1166 /*features*/ features_out,
1168 /*sector_count*/ sectors_out);
1170 ataio->cmd.flags |= ata_flags;
1171 if (auxiliary != 0) {
1172 ataio->ata_flags |= ATA_FLAG_AUX;
1173 ataio->aux = auxiliary;
1178 ata_zac_mgmt_in(struct ccb_ataio *ataio, uint32_t retries,
1179 void (*cbfcnp)(struct cam_periph *, union ccb *),
1180 int use_ncq, uint8_t zm_action, uint64_t zone_id,
1181 uint8_t zone_flags, uint8_t *data_ptr, uint32_t dxfer_len,
1184 uint8_t command_out, ata_flags;
1185 uint16_t features_out, sectors_out;
1189 command_out = ATA_ZAC_MANAGEMENT_IN;
1190 /* XXX KDM put a macro here */
1191 features_out = (zm_action & 0xf) | (zone_flags << 8);
1192 ata_flags = CAM_ATAIO_DMA;
1193 sectors_out = ((dxfer_len >> 9) & 0xffff);
1196 command_out = ATA_RECV_FPDMA_QUEUED;
1197 sectors_out = ATA_RFPDMA_ZAC_MGMT_IN << 8;
1198 auxiliary = (zm_action & 0xf) | (zone_flags << 8);
1199 ata_flags = CAM_ATAIO_FPDMA;
1201 * For RECEIVE FPDMA QUEUED, the transfer length is
1202 * encoded in the FEATURE register, and 0 means
1203 * that 65536 512 byte blocks are to be tranferred.
1204 * In practice, it is unlikely we will see a transfer that
1207 if (dxfer_len == (65536 * 512)) {
1211 * Yes, the caller can theoretically request a
1212 * transfer larger than we can handle.
1213 * Anyone using this function needs enough
1214 * knowledge to avoid doing that.
1216 features_out = ((dxfer_len >> 9) & 0xffff);
1220 cam_fill_ataio(ataio,
1221 /*retries*/ retries,
1223 /*flags*/ CAM_DIR_IN,
1225 /*data_ptr*/ data_ptr,
1226 /*dxfer_len*/ dxfer_len,
1227 /*timeout*/ timeout);
1229 ata_48bit_cmd(ataio,
1230 /*cmd*/ command_out,
1231 /*features*/ features_out,
1233 /*sector_count*/ sectors_out);
1235 ataio->cmd.flags |= ata_flags;
1236 if (auxiliary != 0) {
1237 ataio->ata_flags |= ATA_FLAG_AUX;
1238 ataio->aux = auxiliary;