2 * Copyright (c) 2009 Alexander Motin <mav@FreeBSD.org>
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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.
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
30 #include <sys/param.h>
35 #include <sys/systm.h>
36 #include <sys/libkern.h>
37 #include <sys/kernel.h>
38 #include <sys/sysctl.h>
45 #define min(a,b) (((a)<(b))?(a):(b))
50 #include <cam/cam_ccb.h>
51 #include <cam/cam_queue.h>
52 #include <cam/cam_xpt.h>
54 #include <cam/ata/ata_all.h>
56 #include <sys/endian.h>
65 for (bit = 15; bit >= 0; bit--)
72 ata_op_string(struct ata_cmd *cmd)
75 if (cmd->control & 0x04)
76 return ("SOFT_RESET");
77 switch (cmd->command) {
79 switch (cmd->features) {
80 case 0x00: return ("NOP FLUSHQUEUE");
81 case 0x01: return ("NOP AUTOPOLL");
84 case 0x03: return ("CFA_REQUEST_EXTENDED_ERROR");
86 switch (cmd->features) {
87 case 0x01: return ("DSM TRIM");
90 case 0x08: return ("DEVICE_RESET");
91 case 0x0b: return ("REQUEST_SENSE_DATA_EXT");
92 case 0x20: return ("READ");
93 case 0x24: return ("READ48");
94 case 0x25: return ("READ_DMA48");
95 case 0x26: return ("READ_DMA_QUEUED48");
96 case 0x27: return ("READ_NATIVE_MAX_ADDRESS48");
97 case 0x29: return ("READ_MUL48");
98 case 0x2a: return ("READ_STREAM_DMA48");
99 case 0x2b: return ("READ_STREAM48");
100 case 0x2f: return ("READ_LOG_EXT");
101 case 0x30: return ("WRITE");
102 case 0x34: return ("WRITE48");
103 case 0x35: return ("WRITE_DMA48");
104 case 0x36: return ("WRITE_DMA_QUEUED48");
105 case 0x37: return ("SET_MAX_ADDRESS48");
106 case 0x39: return ("WRITE_MUL48");
107 case 0x3a: return ("WRITE_STREAM_DMA48");
108 case 0x3b: return ("WRITE_STREAM48");
109 case 0x3d: return ("WRITE_DMA_FUA48");
110 case 0x3e: return ("WRITE_DMA_QUEUED_FUA48");
111 case 0x3f: return ("WRITE_LOG_EXT");
112 case 0x40: return ("READ_VERIFY");
113 case 0x42: return ("READ_VERIFY48");
114 case 0x44: return ("ZERO_EXT");
116 switch (cmd->features) {
117 case 0x55: return ("WRITE_UNCORRECTABLE48 PSEUDO");
118 case 0xaa: return ("WRITE_UNCORRECTABLE48 FLAGGED");
120 return "WRITE_UNCORRECTABLE48";
121 case 0x47: return ("READ_LOG_DMA_EXT");
122 case 0x4a: return ("ZAC_MANAGEMENT_IN");
123 case 0x51: return ("CONFIGURE_STREAM");
124 case 0x57: return ("WRITE_LOG_DMA_EXT");
125 case 0x5b: return ("TRUSTED_NON_DATA");
126 case 0x5c: return ("TRUSTED_RECEIVE");
127 case 0x5d: return ("TRUSTED_RECEIVE_DMA");
128 case 0x5e: return ("TRUSTED_SEND");
129 case 0x5f: return ("TRUSTED_SEND_DMA");
130 case 0x60: return ("READ_FPDMA_QUEUED");
131 case 0x61: return ("WRITE_FPDMA_QUEUED");
133 switch (cmd->features & 0xf) {
134 case 0x00: return ("NCQ_NON_DATA ABORT NCQ QUEUE");
135 case 0x01: return ("NCQ_NON_DATA DEADLINE HANDLING");
136 case 0x05: return ("NCQ_NON_DATA SET FEATURES");
138 * XXX KDM need common decoding between NCQ and non-NCQ
139 * versions of SET FEATURES.
141 case 0x06: return ("NCQ_NON_DATA ZERO EXT");
142 case 0x07: return ("NCQ_NON_DATA ZAC MANAGEMENT OUT");
144 return ("NCQ_NON_DATA");
146 switch (cmd->sector_count_exp & 0xf) {
147 case 0x00: return ("SEND_FPDMA_QUEUED DATA SET MANAGEMENT");
148 case 0x02: return ("SEND_FPDMA_QUEUED WRITE LOG DMA EXT");
149 case 0x03: return ("SEND_FPDMA_QUEUED ZAC MANAGEMENT OUT");
150 case 0x04: return ("SEND_FPDMA_QUEUED DATA SET MANAGEMENT XL");
152 return ("SEND_FPDMA_QUEUED");
154 switch (cmd->sector_count_exp & 0xf) {
155 case 0x01: return ("RECEIVE_FPDMA_QUEUED READ LOG DMA EXT");
156 case 0x02: return ("RECEIVE_FPDMA_QUEUED ZAC MANAGEMENT IN");
158 return ("RECEIVE_FPDMA_QUEUED");
160 if (cmd->features == 0xec)
161 return ("SEP_ATTN IDENTIFY");
162 switch (cmd->lba_low) {
163 case 0x00: return ("SEP_ATTN READ BUFFER");
164 case 0x02: return ("SEP_ATTN RECEIVE DIAGNOSTIC RESULTS");
165 case 0x80: return ("SEP_ATTN WRITE BUFFER");
166 case 0x82: return ("SEP_ATTN SEND DIAGNOSTIC");
169 case 0x70: return ("SEEK");
170 case 0x77: return ("SET_DATE_TIME_EXT");
171 case 0x78: return ("ACCESSIBLE_MAX_ADDRESS_CONFIGURATION");
172 case 0x87: return ("CFA_TRANSLATE_SECTOR");
173 case 0x90: return ("EXECUTE_DEVICE_DIAGNOSTIC");
174 case 0x92: return ("DOWNLOAD_MICROCODE");
175 case 0x93: return ("DOWNLOAD_MICROCODE_DMA");
176 case 0x9a: return ("ZAC_MANAGEMENT_OUT");
177 case 0xa0: return ("PACKET");
178 case 0xa1: return ("ATAPI_IDENTIFY");
179 case 0xa2: return ("SERVICE");
181 switch(cmd->features) {
182 case 0xd0: return ("SMART READ ATTR VALUES");
183 case 0xd1: return ("SMART READ ATTR THRESHOLDS");
184 case 0xd3: return ("SMART SAVE ATTR VALUES");
185 case 0xd4: return ("SMART EXECUTE OFFLINE IMMEDIATE");
186 case 0xd5: return ("SMART READ LOG DATA");
187 case 0xd8: return ("SMART ENABLE OPERATION");
188 case 0xd9: return ("SMART DISABLE OPERATION");
189 case 0xda: return ("SMART RETURN STATUS");
192 case 0xb1: return ("DEVICE CONFIGURATION");
193 case 0xb4: return ("SANITIZE_DEVICE");
194 case 0xc0: return ("CFA_ERASE");
195 case 0xc4: return ("READ_MUL");
196 case 0xc5: return ("WRITE_MUL");
197 case 0xc6: return ("SET_MULTI");
198 case 0xc7: return ("READ_DMA_QUEUED");
199 case 0xc8: return ("READ_DMA");
200 case 0xca: return ("WRITE_DMA");
201 case 0xcc: return ("WRITE_DMA_QUEUED");
202 case 0xcd: return ("CFA_WRITE_MULTIPLE_WITHOUT_ERASE");
203 case 0xce: return ("WRITE_MUL_FUA48");
204 case 0xd1: return ("CHECK_MEDIA_CARD_TYPE");
205 case 0xda: return ("GET_MEDIA_STATUS");
206 case 0xde: return ("MEDIA_LOCK");
207 case 0xdf: return ("MEDIA_UNLOCK");
208 case 0xe0: return ("STANDBY_IMMEDIATE");
209 case 0xe1: return ("IDLE_IMMEDIATE");
210 case 0xe2: return ("STANDBY");
211 case 0xe3: return ("IDLE");
212 case 0xe4: return ("READ_BUFFER/PM");
213 case 0xe5: return ("CHECK_POWER_MODE");
214 case 0xe6: return ("SLEEP");
215 case 0xe7: return ("FLUSHCACHE");
216 case 0xe8: return ("WRITE_PM");
217 case 0xea: return ("FLUSHCACHE48");
218 case 0xec: return ("ATA_IDENTIFY");
219 case 0xed: return ("MEDIA_EJECT");
222 * XXX KDM need common decoding between NCQ and non-NCQ
223 * versions of SET FEATURES.
225 switch (cmd->features) {
226 case 0x02: return ("SETFEATURES ENABLE WCACHE");
227 case 0x03: return ("SETFEATURES SET TRANSFER MODE");
228 case 0x04: return ("SETFEATURES ENABLE APM");
229 case 0x06: return ("SETFEATURES ENABLE PUIS");
230 case 0x07: return ("SETFEATURES SPIN-UP");
231 case 0x0b: return ("SETFEATURES ENABLE WRITE READ VERIFY");
232 case 0x0c: return ("SETFEATURES ENABLE DEVICE LIFE CONTROL");
233 case 0x10: return ("SETFEATURES ENABLE SATA FEATURE");
234 case 0x41: return ("SETFEATURES ENABLE FREEFALL CONTROL");
235 case 0x43: return ("SETFEATURES SET MAX HOST INT SECT TIMES");
236 case 0x45: return ("SETFEATURES SET RATE BASIS");
237 case 0x4a: return ("SETFEATURES EXTENDED POWER CONDITIONS");
238 case 0x55: return ("SETFEATURES DISABLE RCACHE");
239 case 0x5d: return ("SETFEATURES ENABLE RELIRQ");
240 case 0x5e: return ("SETFEATURES ENABLE SRVIRQ");
241 case 0x62: return ("SETFEATURES LONG PHYS SECT ALIGN ERC");
242 case 0x63: return ("SETFEATURES DSN");
243 case 0x66: return ("SETFEATURES DISABLE DEFAULTS");
244 case 0x82: return ("SETFEATURES DISABLE WCACHE");
245 case 0x85: return ("SETFEATURES DISABLE APM");
246 case 0x86: return ("SETFEATURES DISABLE PUIS");
247 case 0x8b: return ("SETFEATURES DISABLE WRITE READ VERIFY");
248 case 0x8c: return ("SETFEATURES DISABLE DEVICE LIFE CONTROL");
249 case 0x90: return ("SETFEATURES DISABLE SATA FEATURE");
250 case 0xaa: return ("SETFEATURES ENABLE RCACHE");
251 case 0xC1: return ("SETFEATURES DISABLE FREEFALL CONTROL");
252 case 0xC3: return ("SETFEATURES SENSE DATA REPORTING");
253 case 0xC4: return ("SETFEATURES NCQ SENSE DATA RETURN");
254 case 0xCC: return ("SETFEATURES ENABLE DEFAULTS");
255 case 0xdd: return ("SETFEATURES DISABLE RELIRQ");
256 case 0xde: return ("SETFEATURES DISABLE SRVIRQ");
258 return "SETFEATURES";
259 case 0xf1: return ("SECURITY_SET_PASSWORD");
260 case 0xf2: return ("SECURITY_UNLOCK");
261 case 0xf3: return ("SECURITY_ERASE_PREPARE");
262 case 0xf4: return ("SECURITY_ERASE_UNIT");
263 case 0xf5: return ("SECURITY_FREEZE_LOCK");
264 case 0xf6: return ("SECURITY_DISABLE_PASSWORD");
265 case 0xf8: return ("READ_NATIVE_MAX_ADDRESS");
266 case 0xf9: return ("SET_MAX_ADDRESS");
272 ata_cmd_string(struct ata_cmd *cmd, char *cmd_string, size_t len)
280 sbuf_new(&sb, cmd_string, len, SBUF_FIXEDLEN);
281 ata_cmd_sbuf(cmd, &sb);
283 error = sbuf_finish(&sb);
284 if (error != 0 && error != ENOMEM)
287 return(sbuf_data(&sb));
291 ata_cmd_sbuf(struct ata_cmd *cmd, struct sbuf *sb)
293 sbuf_printf(sb, "%02x %02x %02x %02x "
294 "%02x %02x %02x %02x %02x %02x %02x %02x",
295 cmd->command, cmd->features,
296 cmd->lba_low, cmd->lba_mid, cmd->lba_high, cmd->device,
297 cmd->lba_low_exp, cmd->lba_mid_exp, cmd->lba_high_exp,
298 cmd->features_exp, cmd->sector_count, cmd->sector_count_exp);
302 ata_res_string(struct ata_res *res, char *res_string, size_t len)
310 sbuf_new(&sb, res_string, len, SBUF_FIXEDLEN);
311 ata_res_sbuf(res, &sb);
313 error = sbuf_finish(&sb);
314 if (error != 0 && error != ENOMEM)
317 return(sbuf_data(&sb));
321 ata_res_sbuf(struct ata_res *res, struct sbuf *sb)
324 sbuf_printf(sb, "%02x %02x %02x %02x "
325 "%02x %02x %02x %02x %02x %02x %02x",
326 res->status, res->error,
327 res->lba_low, res->lba_mid, res->lba_high, res->device,
328 res->lba_low_exp, res->lba_mid_exp, res->lba_high_exp,
329 res->sector_count, res->sector_count_exp);
335 * ata_command_sbuf() returns 0 for success and -1 for failure.
338 ata_command_sbuf(struct ccb_ataio *ataio, struct sbuf *sb)
341 sbuf_printf(sb, "%s. ACB: ",
342 ata_op_string(&ataio->cmd));
343 ata_cmd_sbuf(&ataio->cmd, sb);
349 * ata_status_abuf() returns 0 for success and -1 for failure.
352 ata_status_sbuf(struct ccb_ataio *ataio, struct sbuf *sb)
355 sbuf_printf(sb, "ATA status: %02x (%s%s%s%s%s%s%s%s)",
357 (ataio->res.status & 0x80) ? "BSY " : "",
358 (ataio->res.status & 0x40) ? "DRDY " : "",
359 (ataio->res.status & 0x20) ? "DF " : "",
360 (ataio->res.status & 0x10) ? "SERV " : "",
361 (ataio->res.status & 0x08) ? "DRQ " : "",
362 (ataio->res.status & 0x04) ? "CORR " : "",
363 (ataio->res.status & 0x02) ? "IDX " : "",
364 (ataio->res.status & 0x01) ? "ERR" : "");
365 if (ataio->res.status & 1) {
366 sbuf_printf(sb, ", error: %02x (%s%s%s%s%s%s%s%s)",
368 (ataio->res.error & 0x80) ? "ICRC " : "",
369 (ataio->res.error & 0x40) ? "UNC " : "",
370 (ataio->res.error & 0x20) ? "MC " : "",
371 (ataio->res.error & 0x10) ? "IDNF " : "",
372 (ataio->res.error & 0x08) ? "MCR " : "",
373 (ataio->res.error & 0x04) ? "ABRT " : "",
374 (ataio->res.error & 0x02) ? "NM " : "",
375 (ataio->res.error & 0x01) ? "ILI" : "");
382 ata_print_ident(struct ata_params *ident_data)
385 char product[48], revision[16], ata[12], sata[12];
387 cam_strvis(product, ident_data->model, sizeof(ident_data->model),
389 cam_strvis(revision, ident_data->revision, sizeof(ident_data->revision),
391 proto = (ident_data->config == ATA_PROTO_CFA) ? "CFA" :
392 (ident_data->config & ATA_PROTO_ATAPI) ? "ATAPI" : "ATA";
393 if (ata_version(ident_data->version_major) == 0) {
394 snprintf(ata, sizeof(ata), "%s", proto);
395 } else if (ata_version(ident_data->version_major) <= 7) {
396 snprintf(ata, sizeof(ata), "%s-%d", proto,
397 ata_version(ident_data->version_major));
398 } else if (ata_version(ident_data->version_major) == 8) {
399 snprintf(ata, sizeof(ata), "%s8-ACS", proto);
401 snprintf(ata, sizeof(ata), "ACS-%d %s",
402 ata_version(ident_data->version_major) - 7, proto);
404 if (ident_data->satacapabilities && ident_data->satacapabilities != 0xffff) {
405 if (ident_data->satacapabilities & ATA_SATA_GEN3)
406 snprintf(sata, sizeof(sata), " SATA 3.x");
407 else if (ident_data->satacapabilities & ATA_SATA_GEN2)
408 snprintf(sata, sizeof(sata), " SATA 2.x");
409 else if (ident_data->satacapabilities & ATA_SATA_GEN1)
410 snprintf(sata, sizeof(sata), " SATA 1.x");
412 snprintf(sata, sizeof(sata), " SATA");
415 printf("<%s %s> %s%s device\n", product, revision, ata, sata);
419 ata_print_ident_short(struct ata_params *ident_data)
421 char product[48], revision[16];
423 cam_strvis(product, ident_data->model, sizeof(ident_data->model),
425 cam_strvis(revision, ident_data->revision, sizeof(ident_data->revision),
427 printf("<%s %s>", product, revision);
431 semb_print_ident(struct sep_identify_data *ident_data)
433 char vendor[9], product[17], revision[5], fw[5], in[7], ins[5];
435 cam_strvis(vendor, ident_data->vendor_id, 8, sizeof(vendor));
436 cam_strvis(product, ident_data->product_id, 16, sizeof(product));
437 cam_strvis(revision, ident_data->product_rev, 4, sizeof(revision));
438 cam_strvis(fw, ident_data->firmware_rev, 4, sizeof(fw));
439 cam_strvis(in, ident_data->interface_id, 6, sizeof(in));
440 cam_strvis(ins, ident_data->interface_rev, 4, sizeof(ins));
441 printf("<%s %s %s %s> SEMB %s %s device\n",
442 vendor, product, revision, fw, in, ins);
446 semb_print_ident_short(struct sep_identify_data *ident_data)
448 char vendor[9], product[17], revision[5], fw[5];
450 cam_strvis(vendor, ident_data->vendor_id, 8, sizeof(vendor));
451 cam_strvis(product, ident_data->product_id, 16, sizeof(product));
452 cam_strvis(revision, ident_data->product_rev, 4, sizeof(revision));
453 cam_strvis(fw, ident_data->firmware_rev, 4, sizeof(fw));
454 printf("<%s %s %s %s>", vendor, product, revision, fw);
458 ata_logical_sector_size(struct ata_params *ident_data)
460 if ((ident_data->pss & ATA_PSS_VALID_MASK) == ATA_PSS_VALID_VALUE &&
461 (ident_data->pss & ATA_PSS_LSSABOVE512)) {
462 return (((u_int32_t)ident_data->lss_1 |
463 ((u_int32_t)ident_data->lss_2 << 16)) * 2);
469 ata_physical_sector_size(struct ata_params *ident_data)
471 if ((ident_data->pss & ATA_PSS_VALID_MASK) == ATA_PSS_VALID_VALUE) {
472 if (ident_data->pss & ATA_PSS_MULTLS) {
473 return ((uint64_t)ata_logical_sector_size(ident_data) *
474 (1 << (ident_data->pss & ATA_PSS_LSPPS)));
476 return (uint64_t)ata_logical_sector_size(ident_data);
483 ata_logical_sector_offset(struct ata_params *ident_data)
485 if ((ident_data->lsalign & 0xc000) == 0x4000) {
486 return ((uint64_t)ata_logical_sector_size(ident_data) *
487 (ident_data->lsalign & 0x3fff));
493 ata_28bit_cmd(struct ccb_ataio *ataio, uint8_t cmd, uint8_t features,
494 uint32_t lba, uint8_t sector_count)
496 bzero(&ataio->cmd, sizeof(ataio->cmd));
497 ataio->cmd.flags = 0;
498 if (cmd == ATA_READ_DMA ||
499 cmd == ATA_READ_DMA_QUEUED ||
500 cmd == ATA_WRITE_DMA ||
501 cmd == ATA_WRITE_DMA_QUEUED)
502 ataio->cmd.flags |= CAM_ATAIO_DMA;
503 ataio->cmd.command = cmd;
504 ataio->cmd.features = features;
505 ataio->cmd.lba_low = lba;
506 ataio->cmd.lba_mid = lba >> 8;
507 ataio->cmd.lba_high = lba >> 16;
508 ataio->cmd.device = ATA_DEV_LBA | ((lba >> 24) & 0x0f);
509 ataio->cmd.sector_count = sector_count;
513 ata_48bit_cmd(struct ccb_ataio *ataio, uint8_t cmd, uint16_t features,
514 uint64_t lba, uint16_t sector_count)
517 ataio->cmd.flags = CAM_ATAIO_48BIT;
518 if (cmd == ATA_READ_DMA48 ||
519 cmd == ATA_READ_DMA_QUEUED48 ||
520 cmd == ATA_READ_STREAM_DMA48 ||
521 cmd == ATA_WRITE_DMA48 ||
522 cmd == ATA_WRITE_DMA_FUA48 ||
523 cmd == ATA_WRITE_DMA_QUEUED48 ||
524 cmd == ATA_WRITE_DMA_QUEUED_FUA48 ||
525 cmd == ATA_WRITE_STREAM_DMA48 ||
526 cmd == ATA_DATA_SET_MANAGEMENT ||
527 cmd == ATA_READ_LOG_DMA_EXT)
528 ataio->cmd.flags |= CAM_ATAIO_DMA;
529 ataio->cmd.command = cmd;
530 ataio->cmd.features = features;
531 ataio->cmd.lba_low = lba;
532 ataio->cmd.lba_mid = lba >> 8;
533 ataio->cmd.lba_high = lba >> 16;
534 ataio->cmd.device = ATA_DEV_LBA;
535 ataio->cmd.lba_low_exp = lba >> 24;
536 ataio->cmd.lba_mid_exp = lba >> 32;
537 ataio->cmd.lba_high_exp = lba >> 40;
538 ataio->cmd.features_exp = features >> 8;
539 ataio->cmd.sector_count = sector_count;
540 ataio->cmd.sector_count_exp = sector_count >> 8;
541 ataio->cmd.control = 0;
545 ata_ncq_cmd(struct ccb_ataio *ataio, uint8_t cmd,
546 uint64_t lba, uint16_t sector_count)
549 ataio->cmd.flags = CAM_ATAIO_48BIT | CAM_ATAIO_FPDMA;
550 ataio->cmd.command = cmd;
551 ataio->cmd.features = sector_count;
552 ataio->cmd.lba_low = lba;
553 ataio->cmd.lba_mid = lba >> 8;
554 ataio->cmd.lba_high = lba >> 16;
555 ataio->cmd.device = ATA_DEV_LBA;
556 ataio->cmd.lba_low_exp = lba >> 24;
557 ataio->cmd.lba_mid_exp = lba >> 32;
558 ataio->cmd.lba_high_exp = lba >> 40;
559 ataio->cmd.features_exp = sector_count >> 8;
560 ataio->cmd.sector_count = 0;
561 ataio->cmd.sector_count_exp = 0;
562 ataio->cmd.control = 0;
566 ata_reset_cmd(struct ccb_ataio *ataio)
568 bzero(&ataio->cmd, sizeof(ataio->cmd));
569 ataio->cmd.flags = CAM_ATAIO_CONTROL | CAM_ATAIO_NEEDRESULT;
570 ataio->cmd.control = 0x04;
574 ata_pm_read_cmd(struct ccb_ataio *ataio, int reg, int port)
576 bzero(&ataio->cmd, sizeof(ataio->cmd));
577 ataio->cmd.flags = CAM_ATAIO_NEEDRESULT;
578 ataio->cmd.command = ATA_READ_PM;
579 ataio->cmd.features = reg;
580 ataio->cmd.device = port & 0x0f;
584 ata_pm_write_cmd(struct ccb_ataio *ataio, int reg, int port, uint32_t val)
586 bzero(&ataio->cmd, sizeof(ataio->cmd));
587 ataio->cmd.flags = 0;
588 ataio->cmd.command = ATA_WRITE_PM;
589 ataio->cmd.features = reg;
590 ataio->cmd.sector_count = val;
591 ataio->cmd.lba_low = val >> 8;
592 ataio->cmd.lba_mid = val >> 16;
593 ataio->cmd.lba_high = val >> 24;
594 ataio->cmd.device = port & 0x0f;
598 ata_read_log(struct ccb_ataio *ataio, uint32_t retries,
599 void (*cbfcnp)(struct cam_periph *, union ccb *),
600 uint32_t log_address, uint32_t page_number, uint16_t block_count,
601 uint32_t protocol, uint8_t *data_ptr, uint32_t dxfer_len,
606 cam_fill_ataio(ataio,
609 /*flags*/ CAM_DIR_IN,
611 /*data_ptr*/ data_ptr,
612 /*dxfer_len*/ dxfer_len,
613 /*timeout*/ timeout);
615 lba = (((uint64_t)page_number & 0xff00) << 32) |
616 ((page_number & 0x00ff) << 8) |
617 (log_address & 0xff);
620 /*cmd*/ (protocol & CAM_ATAIO_DMA) ? ATA_READ_LOG_DMA_EXT :
624 /*sector_count*/ block_count);
628 ata_bswap(int8_t *buf, int len)
630 u_int16_t *ptr = (u_int16_t*)(buf + len);
632 while (--ptr >= (u_int16_t*)buf)
633 *ptr = be16toh(*ptr);
637 ata_btrim(int8_t *buf, int len)
641 for (ptr = buf; ptr < buf+len; ++ptr)
642 if (!*ptr || *ptr == '_')
644 for (ptr = buf + len - 1; ptr >= buf && *ptr == ' '; --ptr)
649 ata_bpack(int8_t *src, int8_t *dst, int len)
653 for (i = j = blank = 0 ; i < len; i++) {
654 if (blank && src[i] == ' ') continue;
655 if (blank && src[i] != ' ') {
672 ata_max_pmode(struct ata_params *ap)
674 if (ap->atavalid & ATA_FLAG_64_70) {
675 if (ap->apiomodes & 0x02)
677 if (ap->apiomodes & 0x01)
680 if (ap->mwdmamodes & 0x04)
682 if (ap->mwdmamodes & 0x02)
684 if (ap->mwdmamodes & 0x01)
686 if ((ap->retired_piomode & ATA_RETIRED_PIO_MASK) == 0x200)
688 if ((ap->retired_piomode & ATA_RETIRED_PIO_MASK) == 0x100)
690 if ((ap->retired_piomode & ATA_RETIRED_PIO_MASK) == 0x000)
696 ata_max_wmode(struct ata_params *ap)
698 if (ap->mwdmamodes & 0x04)
700 if (ap->mwdmamodes & 0x02)
702 if (ap->mwdmamodes & 0x01)
708 ata_max_umode(struct ata_params *ap)
710 if (ap->atavalid & ATA_FLAG_88) {
711 if (ap->udmamodes & 0x40)
713 if (ap->udmamodes & 0x20)
715 if (ap->udmamodes & 0x10)
717 if (ap->udmamodes & 0x08)
719 if (ap->udmamodes & 0x04)
721 if (ap->udmamodes & 0x02)
723 if (ap->udmamodes & 0x01)
730 ata_max_mode(struct ata_params *ap, int maxmode)
734 maxmode = ATA_DMA_MAX;
735 if (maxmode >= ATA_UDMA0 && ata_max_umode(ap) > 0)
736 return (min(maxmode, ata_max_umode(ap)));
737 if (maxmode >= ATA_WDMA0 && ata_max_wmode(ap) > 0)
738 return (min(maxmode, ata_max_wmode(ap)));
739 return (min(maxmode, ata_max_pmode(ap)));
743 ata_mode2string(int mode)
746 case -1: return "UNSUPPORTED";
747 case 0: return "NONE";
748 case ATA_PIO0: return "PIO0";
749 case ATA_PIO1: return "PIO1";
750 case ATA_PIO2: return "PIO2";
751 case ATA_PIO3: return "PIO3";
752 case ATA_PIO4: return "PIO4";
753 case ATA_WDMA0: return "WDMA0";
754 case ATA_WDMA1: return "WDMA1";
755 case ATA_WDMA2: return "WDMA2";
756 case ATA_UDMA0: return "UDMA0";
757 case ATA_UDMA1: return "UDMA1";
758 case ATA_UDMA2: return "UDMA2";
759 case ATA_UDMA3: return "UDMA3";
760 case ATA_UDMA4: return "UDMA4";
761 case ATA_UDMA5: return "UDMA5";
762 case ATA_UDMA6: return "UDMA6";
764 if (mode & ATA_DMA_MASK)
772 ata_string2mode(char *str)
774 if (!strcasecmp(str, "PIO0")) return (ATA_PIO0);
775 if (!strcasecmp(str, "PIO1")) return (ATA_PIO1);
776 if (!strcasecmp(str, "PIO2")) return (ATA_PIO2);
777 if (!strcasecmp(str, "PIO3")) return (ATA_PIO3);
778 if (!strcasecmp(str, "PIO4")) return (ATA_PIO4);
779 if (!strcasecmp(str, "WDMA0")) return (ATA_WDMA0);
780 if (!strcasecmp(str, "WDMA1")) return (ATA_WDMA1);
781 if (!strcasecmp(str, "WDMA2")) return (ATA_WDMA2);
782 if (!strcasecmp(str, "UDMA0")) return (ATA_UDMA0);
783 if (!strcasecmp(str, "UDMA16")) return (ATA_UDMA0);
784 if (!strcasecmp(str, "UDMA1")) return (ATA_UDMA1);
785 if (!strcasecmp(str, "UDMA25")) return (ATA_UDMA1);
786 if (!strcasecmp(str, "UDMA2")) return (ATA_UDMA2);
787 if (!strcasecmp(str, "UDMA33")) return (ATA_UDMA2);
788 if (!strcasecmp(str, "UDMA3")) return (ATA_UDMA3);
789 if (!strcasecmp(str, "UDMA44")) return (ATA_UDMA3);
790 if (!strcasecmp(str, "UDMA4")) return (ATA_UDMA4);
791 if (!strcasecmp(str, "UDMA66")) return (ATA_UDMA4);
792 if (!strcasecmp(str, "UDMA5")) return (ATA_UDMA5);
793 if (!strcasecmp(str, "UDMA100")) return (ATA_UDMA5);
794 if (!strcasecmp(str, "UDMA6")) return (ATA_UDMA6);
795 if (!strcasecmp(str, "UDMA133")) return (ATA_UDMA6);
801 ata_mode2speed(int mode)
839 ata_revision2speed(int revision)
853 ata_speed2revision(u_int speed)
870 ata_identify_match(caddr_t identbuffer, caddr_t table_entry)
872 struct scsi_inquiry_pattern *entry;
873 struct ata_params *ident;
875 entry = (struct scsi_inquiry_pattern *)table_entry;
876 ident = (struct ata_params *)identbuffer;
878 if ((cam_strmatch(ident->model, entry->product,
879 sizeof(ident->model)) == 0)
880 && (cam_strmatch(ident->revision, entry->revision,
881 sizeof(ident->revision)) == 0)) {
888 ata_static_identify_match(caddr_t identbuffer, caddr_t table_entry)
890 struct scsi_static_inquiry_pattern *entry;
891 struct ata_params *ident;
893 entry = (struct scsi_static_inquiry_pattern *)table_entry;
894 ident = (struct ata_params *)identbuffer;
896 if ((cam_strmatch(ident->model, entry->product,
897 sizeof(ident->model)) == 0)
898 && (cam_strmatch(ident->revision, entry->revision,
899 sizeof(ident->revision)) == 0)) {
906 semb_receive_diagnostic_results(struct ccb_ataio *ataio,
907 u_int32_t retries, void (*cbfcnp)(struct cam_periph *, union ccb*),
908 uint8_t tag_action, int pcv, uint8_t page_code,
909 uint8_t *data_ptr, uint16_t length, uint32_t timeout)
912 length = min(length, 1020);
913 length = (length + 3) & ~3;
914 cam_fill_ataio(ataio,
922 ata_28bit_cmd(ataio, ATA_SEP_ATTN,
923 pcv ? page_code : 0, 0x02, length / 4);
927 semb_send_diagnostic(struct ccb_ataio *ataio,
928 u_int32_t retries, void (*cbfcnp)(struct cam_periph *, union ccb *),
929 uint8_t tag_action, uint8_t *data_ptr, uint16_t length, uint32_t timeout)
932 length = min(length, 1020);
933 length = (length + 3) & ~3;
934 cam_fill_ataio(ataio,
937 /*flags*/length ? CAM_DIR_OUT : CAM_DIR_NONE,
942 ata_28bit_cmd(ataio, ATA_SEP_ATTN,
943 length > 0 ? data_ptr[0] : 0, 0x82, length / 4);
947 semb_read_buffer(struct ccb_ataio *ataio,
948 u_int32_t retries, void (*cbfcnp)(struct cam_periph *, union ccb*),
949 uint8_t tag_action, uint8_t page_code,
950 uint8_t *data_ptr, uint16_t length, uint32_t timeout)
953 length = min(length, 1020);
954 length = (length + 3) & ~3;
955 cam_fill_ataio(ataio,
963 ata_28bit_cmd(ataio, ATA_SEP_ATTN,
964 page_code, 0x00, length / 4);
968 semb_write_buffer(struct ccb_ataio *ataio,
969 u_int32_t retries, void (*cbfcnp)(struct cam_periph *, union ccb *),
970 uint8_t tag_action, uint8_t *data_ptr, uint16_t length, uint32_t timeout)
973 length = min(length, 1020);
974 length = (length + 3) & ~3;
975 cam_fill_ataio(ataio,
978 /*flags*/length ? CAM_DIR_OUT : CAM_DIR_NONE,
983 ata_28bit_cmd(ataio, ATA_SEP_ATTN,
984 length > 0 ? data_ptr[0] : 0, 0x80, length / 4);
989 ata_zac_mgmt_out(struct ccb_ataio *ataio, uint32_t retries,
990 void (*cbfcnp)(struct cam_periph *, union ccb *),
991 int use_ncq, uint8_t zm_action, uint64_t zone_id,
992 uint8_t zone_flags, uint16_t sector_count, uint8_t *data_ptr,
993 uint32_t dxfer_len, uint32_t timeout)
995 uint8_t command_out, ata_flags;
996 uint16_t features_out, sectors_out;
1000 command_out = ATA_ZAC_MANAGEMENT_OUT;
1001 features_out = (zm_action & 0xf) | (zone_flags << 8);
1002 if (dxfer_len == 0) {
1006 ata_flags = CAM_ATAIO_DMA;
1007 /* XXX KDM use sector count? */
1008 sectors_out = ((dxfer_len >> 9) & 0xffff);
1012 if (dxfer_len == 0) {
1013 command_out = ATA_NCQ_NON_DATA;
1014 features_out = ATA_NCQ_ZAC_MGMT_OUT;
1017 command_out = ATA_SEND_FPDMA_QUEUED;
1019 /* Note that we're defaulting to normal priority */
1020 sectors_out = ATA_SFPDMA_ZAC_MGMT_OUT << 8;
1023 * For SEND FPDMA QUEUED, the transfer length is
1024 * encoded in the FEATURE register, and 0 means
1025 * that 65536 512 byte blocks are to be tranferred.
1026 * In practice, it seems unlikely that we'll see
1027 * a transfer that large.
1029 if (dxfer_len == (65536 * 512)) {
1033 * Yes, the caller can theoretically send a
1034 * transfer larger than we can handle.
1035 * Anyone using this function needs enough
1036 * knowledge to avoid doing that.
1038 features_out = ((dxfer_len >> 9) & 0xffff);
1041 auxiliary = (zm_action & 0xf) | (zone_flags << 8);
1043 ata_flags = CAM_ATAIO_FPDMA;
1046 cam_fill_ataio(ataio,
1047 /*retries*/ retries,
1049 /*flags*/ (dxfer_len > 0) ? CAM_DIR_OUT : CAM_DIR_NONE,
1051 /*data_ptr*/ data_ptr,
1052 /*dxfer_len*/ dxfer_len,
1053 /*timeout*/ timeout);
1055 ata_48bit_cmd(ataio,
1056 /*cmd*/ command_out,
1057 /*features*/ features_out,
1059 /*sector_count*/ sectors_out);
1061 ataio->cmd.flags |= ata_flags;
1062 if (auxiliary != 0) {
1063 ataio->ata_flags |= ATA_FLAG_AUX;
1064 ataio->aux = auxiliary;
1069 ata_zac_mgmt_in(struct ccb_ataio *ataio, uint32_t retries,
1070 void (*cbfcnp)(struct cam_periph *, union ccb *),
1071 int use_ncq, uint8_t zm_action, uint64_t zone_id,
1072 uint8_t zone_flags, uint8_t *data_ptr, uint32_t dxfer_len,
1075 uint8_t command_out, ata_flags;
1076 uint16_t features_out, sectors_out;
1080 command_out = ATA_ZAC_MANAGEMENT_IN;
1081 /* XXX KDM put a macro here */
1082 features_out = (zm_action & 0xf) | (zone_flags << 8);
1083 ata_flags = CAM_ATAIO_DMA;
1084 sectors_out = ((dxfer_len >> 9) & 0xffff);
1087 command_out = ATA_RECV_FPDMA_QUEUED;
1088 sectors_out = ATA_RFPDMA_ZAC_MGMT_IN << 8;
1089 auxiliary = (zm_action & 0xf) | (zone_flags << 8);
1090 ata_flags = CAM_ATAIO_FPDMA;
1092 * For RECEIVE FPDMA QUEUED, the transfer length is
1093 * encoded in the FEATURE register, and 0 means
1094 * that 65536 512 byte blocks are to be tranferred.
1095 * In practice, it is unlikely we will see a transfer that
1098 if (dxfer_len == (65536 * 512)) {
1102 * Yes, the caller can theoretically request a
1103 * transfer larger than we can handle.
1104 * Anyone using this function needs enough
1105 * knowledge to avoid doing that.
1107 features_out = ((dxfer_len >> 9) & 0xffff);
1111 cam_fill_ataio(ataio,
1112 /*retries*/ retries,
1114 /*flags*/ CAM_DIR_IN,
1116 /*data_ptr*/ data_ptr,
1117 /*dxfer_len*/ dxfer_len,
1118 /*timeout*/ timeout);
1120 ata_48bit_cmd(ataio,
1121 /*cmd*/ command_out,
1122 /*features*/ features_out,
1124 /*sector_count*/ sectors_out);
1126 ataio->cmd.flags |= ata_flags;
1127 if (auxiliary != 0) {
1128 ataio->ata_flags |= ATA_FLAG_AUX;
1129 ataio->aux = auxiliary;