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 0x44: return ("ZERO_EXT"); 114 case 0x45: 115 switch (cmd->features) { 116 case 0x55: return ("WRITE_UNCORRECTABLE48 PSEUDO"); 117 case 0xaa: return ("WRITE_UNCORRECTABLE48 FLAGGED"); 118 } 119 return "WRITE_UNCORRECTABLE48"; 120 case 0x47: return ("READ_LOG_DMA_EXT"); 121 case 0x4a: return ("ZAC_MANAGEMENT_IN"); 122 case 0x51: return ("CONFIGURE_STREAM"); 123 case 0x60: return ("READ_FPDMA_QUEUED"); 124 case 0x61: return ("WRITE_FPDMA_QUEUED"); 125 case 0x63: 126 switch (cmd->features & 0xf) { 127 case 0x00: return ("NCQ_NON_DATA ABORT NCQ QUEUE"); 128 case 0x01: return ("NCQ_NON_DATA DEADLINE HANDLING"); 129 case 0x05: return ("NCQ_NON_DATA SET FEATURES"); 130 /* 131 * XXX KDM need common decoding between NCQ and non-NCQ 132 * versions of SET FEATURES. 133 */ 134 case 0x06: return ("NCQ_NON_DATA ZERO EXT"); 135 case 0x07: return ("NCQ_NON_DATA ZAC MANAGEMENT OUT"); 136 } 137 return ("NCQ_NON_DATA"); 138 case 0x64: 139 switch (cmd->sector_count_exp & 0xf) { 140 case 0x00: return ("SEND_FPDMA_QUEUED DATA SET MANAGEMENT"); 141 case 0x02: return ("SEND_FPDMA_QUEUED WRITE LOG DMA EXT"); 142 case 0x03: return ("SEND_FPDMA_QUEUED ZAC MANAGEMENT OUT"); 143 case 0x04: return ("SEND_FPDMA_QUEUED DATA SET MANAGEMENT XL"); 144 } 145 return ("SEND_FPDMA_QUEUED"); 146 case 0x65: 147 switch (cmd->sector_count_exp & 0xf) { 148 case 0x01: return ("RECEIVE_FPDMA_QUEUED READ LOG DMA EXT"); 149 case 0x02: return ("RECEIVE_FPDMA_QUEUED ZAC MANAGEMENT IN"); 150 } 151 return ("RECEIVE_FPDMA_QUEUED"); 152 case 0x67: 153 if (cmd->features == 0xec) 154 return ("SEP_ATTN IDENTIFY"); 155 switch (cmd->lba_low) { 156 case 0x00: return ("SEP_ATTN READ BUFFER"); 157 case 0x02: return ("SEP_ATTN RECEIVE DIAGNOSTIC RESULTS"); 158 case 0x80: return ("SEP_ATTN WRITE BUFFER"); 159 case 0x82: return ("SEP_ATTN SEND DIAGNOSTIC"); 160 } 161 return ("SEP_ATTN"); 162 case 0x70: return ("SEEK"); 163 case 0x87: return ("CFA_TRANSLATE_SECTOR"); 164 case 0x90: return ("EXECUTE_DEVICE_DIAGNOSTIC"); 165 case 0x92: return ("DOWNLOAD_MICROCODE"); 166 case 0x9a: return ("ZAC_MANAGEMENT_OUT"); 167 case 0xa0: return ("PACKET"); 168 case 0xa1: return ("ATAPI_IDENTIFY"); 169 case 0xa2: return ("SERVICE"); 170 case 0xb0: 171 switch(cmd->features) { 172 case 0xd0: return ("SMART READ ATTR VALUES"); 173 case 0xd1: return ("SMART READ ATTR THRESHOLDS"); 174 case 0xd3: return ("SMART SAVE ATTR VALUES"); 175 case 0xd4: return ("SMART EXECUTE OFFLINE IMMEDIATE"); 176 case 0xd5: return ("SMART READ LOG DATA"); 177 case 0xd8: return ("SMART ENABLE OPERATION"); 178 case 0xd9: return ("SMART DISABLE OPERATION"); 179 case 0xda: return ("SMART RETURN STATUS"); 180 } 181 return ("SMART"); 182 case 0xb1: return ("DEVICE CONFIGURATION"); 183 case 0xc0: return ("CFA_ERASE"); 184 case 0xc4: return ("READ_MUL"); 185 case 0xc5: return ("WRITE_MUL"); 186 case 0xc6: return ("SET_MULTI"); 187 case 0xc7: return ("READ_DMA_QUEUED"); 188 case 0xc8: return ("READ_DMA"); 189 case 0xca: return ("WRITE_DMA"); 190 case 0xcc: return ("WRITE_DMA_QUEUED"); 191 case 0xcd: return ("CFA_WRITE_MULTIPLE_WITHOUT_ERASE"); 192 case 0xce: return ("WRITE_MUL_FUA48"); 193 case 0xd1: return ("CHECK_MEDIA_CARD_TYPE"); 194 case 0xda: return ("GET_MEDIA_STATUS"); 195 case 0xde: return ("MEDIA_LOCK"); 196 case 0xdf: return ("MEDIA_UNLOCK"); 197 case 0xe0: return ("STANDBY_IMMEDIATE"); 198 case 0xe1: return ("IDLE_IMMEDIATE"); 199 case 0xe2: return ("STANDBY"); 200 case 0xe3: return ("IDLE"); 201 case 0xe4: return ("READ_BUFFER/PM"); 202 case 0xe5: return ("CHECK_POWER_MODE"); 203 case 0xe6: return ("SLEEP"); 204 case 0xe7: return ("FLUSHCACHE"); 205 case 0xe8: return ("WRITE_PM"); 206 case 0xea: return ("FLUSHCACHE48"); 207 case 0xec: return ("ATA_IDENTIFY"); 208 case 0xed: return ("MEDIA_EJECT"); 209 case 0xef: 210 /* 211 * XXX KDM need common decoding between NCQ and non-NCQ 212 * versions of SET FEATURES. 213 */ 214 switch (cmd->features) { 215 case 0x02: return ("SETFEATURES ENABLE WCACHE"); 216 case 0x03: return ("SETFEATURES SET TRANSFER MODE"); 217 case 0x04: return ("SETFEATURES ENABLE APM"); 218 case 0x06: return ("SETFEATURES ENABLE PUIS"); 219 case 0x07: return ("SETFEATURES SPIN-UP"); 220 case 0x0b: return ("SETFEATURES ENABLE WRITE READ VERIFY"); 221 case 0x0c: return ("SETFEATURES ENABLE DEVICE LIFE CONTROL"); 222 case 0x10: return ("SETFEATURES ENABLE SATA FEATURE"); 223 case 0x41: return ("SETFEATURES ENABLE FREEFALL CONTROL"); 224 case 0x43: return ("SETFEATURES SET MAX HOST INT SECT TIMES"); 225 case 0x45: return ("SETFEATURES SET RATE BASIS"); 226 case 0x4a: return ("SETFEATURES EXTENDED POWER CONDITIONS"); 227 case 0x55: return ("SETFEATURES DISABLE RCACHE"); 228 case 0x5d: return ("SETFEATURES ENABLE RELIRQ"); 229 case 0x5e: return ("SETFEATURES ENABLE SRVIRQ"); 230 case 0x62: return ("SETFEATURES LONG PHYS SECT ALIGN ERC"); 231 case 0x63: return ("SETFEATURES DSN"); 232 case 0x66: return ("SETFEATURES DISABLE DEFAULTS"); 233 case 0x82: return ("SETFEATURES DISABLE WCACHE"); 234 case 0x85: return ("SETFEATURES DISABLE APM"); 235 case 0x86: return ("SETFEATURES DISABLE PUIS"); 236 case 0x8b: return ("SETFEATURES DISABLE WRITE READ VERIFY"); 237 case 0x8c: return ("SETFEATURES DISABLE DEVICE LIFE CONTROL"); 238 case 0x90: return ("SETFEATURES DISABLE SATA FEATURE"); 239 case 0xaa: return ("SETFEATURES ENABLE RCACHE"); 240 case 0xC1: return ("SETFEATURES DISABLE FREEFALL CONTROL"); 241 case 0xC3: return ("SETFEATURES SENSE DATA REPORTING"); 242 case 0xC4: return ("SETFEATURES NCQ SENSE DATA RETURN"); 243 case 0xCC: return ("SETFEATURES ENABLE DEFAULTS"); 244 case 0xdd: return ("SETFEATURES DISABLE RELIRQ"); 245 case 0xde: return ("SETFEATURES DISABLE SRVIRQ"); 246 } 247 return "SETFEATURES"; 248 case 0xf1: return ("SECURITY_SET_PASSWORD"); 249 case 0xf2: return ("SECURITY_UNLOCK"); 250 case 0xf3: return ("SECURITY_ERASE_PREPARE"); 251 case 0xf4: return ("SECURITY_ERASE_UNIT"); 252 case 0xf5: return ("SECURITY_FREEZE_LOCK"); 253 case 0xf6: return ("SECURITY_DISABLE_PASSWORD"); 254 case 0xf8: return ("READ_NATIVE_MAX_ADDRESS"); 255 case 0xf9: return ("SET_MAX_ADDRESS"); 256 } 257 return "UNKNOWN"; 258 } 259 260 char * 261 ata_cmd_string(struct ata_cmd *cmd, char *cmd_string, size_t len) 262 { 263 struct sbuf sb; 264 int error; 265 266 if (len == 0) 267 return (""); 268 269 sbuf_new(&sb, cmd_string, len, SBUF_FIXEDLEN); 270 ata_cmd_sbuf(cmd, &sb); 271 272 error = sbuf_finish(&sb); 273 if (error != 0 && error != ENOMEM) 274 return (""); 275 276 return(sbuf_data(&sb)); 277 } 278 279 void 280 ata_cmd_sbuf(struct ata_cmd *cmd, struct sbuf *sb) 281 { 282 sbuf_printf(sb, "%02x %02x %02x %02x " 283 "%02x %02x %02x %02x %02x %02x %02x %02x", 284 cmd->command, cmd->features, 285 cmd->lba_low, cmd->lba_mid, cmd->lba_high, cmd->device, 286 cmd->lba_low_exp, cmd->lba_mid_exp, cmd->lba_high_exp, 287 cmd->features_exp, cmd->sector_count, cmd->sector_count_exp); 288 } 289 290 char * 291 ata_res_string(struct ata_res *res, char *res_string, size_t len) 292 { 293 struct sbuf sb; 294 int error; 295 296 if (len == 0) 297 return (""); 298 299 sbuf_new(&sb, res_string, len, SBUF_FIXEDLEN); 300 ata_res_sbuf(res, &sb); 301 302 error = sbuf_finish(&sb); 303 if (error != 0 && error != ENOMEM) 304 return (""); 305 306 return(sbuf_data(&sb)); 307 } 308 309 int 310 ata_res_sbuf(struct ata_res *res, struct sbuf *sb) 311 { 312 313 sbuf_printf(sb, "%02x %02x %02x %02x " 314 "%02x %02x %02x %02x %02x %02x %02x", 315 res->status, res->error, 316 res->lba_low, res->lba_mid, res->lba_high, res->device, 317 res->lba_low_exp, res->lba_mid_exp, res->lba_high_exp, 318 res->sector_count, res->sector_count_exp); 319 320 return (0); 321 } 322 323 /* 324 * ata_command_sbuf() returns 0 for success and -1 for failure. 325 */ 326 int 327 ata_command_sbuf(struct ccb_ataio *ataio, struct sbuf *sb) 328 { 329 330 sbuf_printf(sb, "%s. ACB: ", 331 ata_op_string(&ataio->cmd)); 332 ata_cmd_sbuf(&ataio->cmd, sb); 333 334 return(0); 335 } 336 337 /* 338 * ata_status_abuf() returns 0 for success and -1 for failure. 339 */ 340 int 341 ata_status_sbuf(struct ccb_ataio *ataio, struct sbuf *sb) 342 { 343 344 sbuf_printf(sb, "ATA status: %02x (%s%s%s%s%s%s%s%s)", 345 ataio->res.status, 346 (ataio->res.status & 0x80) ? "BSY " : "", 347 (ataio->res.status & 0x40) ? "DRDY " : "", 348 (ataio->res.status & 0x20) ? "DF " : "", 349 (ataio->res.status & 0x10) ? "SERV " : "", 350 (ataio->res.status & 0x08) ? "DRQ " : "", 351 (ataio->res.status & 0x04) ? "CORR " : "", 352 (ataio->res.status & 0x02) ? "IDX " : "", 353 (ataio->res.status & 0x01) ? "ERR" : ""); 354 if (ataio->res.status & 1) { 355 sbuf_printf(sb, ", error: %02x (%s%s%s%s%s%s%s%s)", 356 ataio->res.error, 357 (ataio->res.error & 0x80) ? "ICRC " : "", 358 (ataio->res.error & 0x40) ? "UNC " : "", 359 (ataio->res.error & 0x20) ? "MC " : "", 360 (ataio->res.error & 0x10) ? "IDNF " : "", 361 (ataio->res.error & 0x08) ? "MCR " : "", 362 (ataio->res.error & 0x04) ? "ABRT " : "", 363 (ataio->res.error & 0x02) ? "NM " : "", 364 (ataio->res.error & 0x01) ? "ILI" : ""); 365 } 366 367 return(0); 368 } 369 370 void 371 ata_print_ident(struct ata_params *ident_data) 372 { 373 const char *proto; 374 char product[48], revision[16], ata[12], sata[12]; 375 376 cam_strvis(product, ident_data->model, sizeof(ident_data->model), 377 sizeof(product)); 378 cam_strvis(revision, ident_data->revision, sizeof(ident_data->revision), 379 sizeof(revision)); 380 proto = (ident_data->config == ATA_PROTO_CFA) ? "CFA" : 381 (ident_data->config & ATA_PROTO_ATAPI) ? "ATAPI" : "ATA"; 382 if (ata_version(ident_data->version_major) == 0) { 383 snprintf(ata, sizeof(ata), "%s", proto); 384 } else if (ata_version(ident_data->version_major) <= 7) { 385 snprintf(ata, sizeof(ata), "%s-%d", proto, 386 ata_version(ident_data->version_major)); 387 } else if (ata_version(ident_data->version_major) == 8) { 388 snprintf(ata, sizeof(ata), "%s8-ACS", proto); 389 } else { 390 snprintf(ata, sizeof(ata), "ACS-%d %s", 391 ata_version(ident_data->version_major) - 7, proto); 392 } 393 if (ident_data->satacapabilities && ident_data->satacapabilities != 0xffff) { 394 if (ident_data->satacapabilities & ATA_SATA_GEN3) 395 snprintf(sata, sizeof(sata), " SATA 3.x"); 396 else if (ident_data->satacapabilities & ATA_SATA_GEN2) 397 snprintf(sata, sizeof(sata), " SATA 2.x"); 398 else if (ident_data->satacapabilities & ATA_SATA_GEN1) 399 snprintf(sata, sizeof(sata), " SATA 1.x"); 400 else 401 snprintf(sata, sizeof(sata), " SATA"); 402 } else 403 sata[0] = 0; 404 printf("<%s %s> %s%s device\n", product, revision, ata, sata); 405 } 406 407 void 408 ata_print_ident_short(struct ata_params *ident_data) 409 { 410 char product[48], revision[16]; 411 412 cam_strvis(product, ident_data->model, sizeof(ident_data->model), 413 sizeof(product)); 414 cam_strvis(revision, ident_data->revision, sizeof(ident_data->revision), 415 sizeof(revision)); 416 printf("<%s %s>", product, revision); 417 } 418 419 void 420 semb_print_ident(struct sep_identify_data *ident_data) 421 { 422 char vendor[9], product[17], revision[5], fw[5], in[7], ins[5]; 423 424 cam_strvis(vendor, ident_data->vendor_id, 8, sizeof(vendor)); 425 cam_strvis(product, ident_data->product_id, 16, sizeof(product)); 426 cam_strvis(revision, ident_data->product_rev, 4, sizeof(revision)); 427 cam_strvis(fw, ident_data->firmware_rev, 4, sizeof(fw)); 428 cam_strvis(in, ident_data->interface_id, 6, sizeof(in)); 429 cam_strvis(ins, ident_data->interface_rev, 4, sizeof(ins)); 430 printf("<%s %s %s %s> SEMB %s %s device\n", 431 vendor, product, revision, fw, in, ins); 432 } 433 434 void 435 semb_print_ident_short(struct sep_identify_data *ident_data) 436 { 437 char vendor[9], product[17], revision[5], fw[5]; 438 439 cam_strvis(vendor, ident_data->vendor_id, 8, sizeof(vendor)); 440 cam_strvis(product, ident_data->product_id, 16, sizeof(product)); 441 cam_strvis(revision, ident_data->product_rev, 4, sizeof(revision)); 442 cam_strvis(fw, ident_data->firmware_rev, 4, sizeof(fw)); 443 printf("<%s %s %s %s>", vendor, product, revision, fw); 444 } 445 446 uint32_t 447 ata_logical_sector_size(struct ata_params *ident_data) 448 { 449 if ((ident_data->pss & ATA_PSS_VALID_MASK) == ATA_PSS_VALID_VALUE && 450 (ident_data->pss & ATA_PSS_LSSABOVE512)) { 451 return (((u_int32_t)ident_data->lss_1 | 452 ((u_int32_t)ident_data->lss_2 << 16)) * 2); 453 } 454 return (512); 455 } 456 457 uint64_t 458 ata_physical_sector_size(struct ata_params *ident_data) 459 { 460 if ((ident_data->pss & ATA_PSS_VALID_MASK) == ATA_PSS_VALID_VALUE) { 461 if (ident_data->pss & ATA_PSS_MULTLS) { 462 return ((uint64_t)ata_logical_sector_size(ident_data) * 463 (1 << (ident_data->pss & ATA_PSS_LSPPS))); 464 } else { 465 return (uint64_t)ata_logical_sector_size(ident_data); 466 } 467 } 468 return (512); 469 } 470 471 uint64_t 472 ata_logical_sector_offset(struct ata_params *ident_data) 473 { 474 if ((ident_data->lsalign & 0xc000) == 0x4000) { 475 return ((uint64_t)ata_logical_sector_size(ident_data) * 476 (ident_data->lsalign & 0x3fff)); 477 } 478 return (0); 479 } 480 481 void 482 ata_28bit_cmd(struct ccb_ataio *ataio, uint8_t cmd, uint8_t features, 483 uint32_t lba, uint8_t sector_count) 484 { 485 bzero(&ataio->cmd, sizeof(ataio->cmd)); 486 ataio->cmd.flags = 0; 487 if (cmd == ATA_READ_DMA || 488 cmd == ATA_READ_DMA_QUEUED || 489 cmd == ATA_WRITE_DMA || 490 cmd == ATA_WRITE_DMA_QUEUED) 491 ataio->cmd.flags |= CAM_ATAIO_DMA; 492 ataio->cmd.command = cmd; 493 ataio->cmd.features = features; 494 ataio->cmd.lba_low = lba; 495 ataio->cmd.lba_mid = lba >> 8; 496 ataio->cmd.lba_high = lba >> 16; 497 ataio->cmd.device = ATA_DEV_LBA | ((lba >> 24) & 0x0f); 498 ataio->cmd.sector_count = sector_count; 499 } 500 501 void 502 ata_48bit_cmd(struct ccb_ataio *ataio, uint8_t cmd, uint16_t features, 503 uint64_t lba, uint16_t sector_count) 504 { 505 506 ataio->cmd.flags = CAM_ATAIO_48BIT; 507 if (cmd == ATA_READ_DMA48 || 508 cmd == ATA_READ_DMA_QUEUED48 || 509 cmd == ATA_READ_STREAM_DMA48 || 510 cmd == ATA_WRITE_DMA48 || 511 cmd == ATA_WRITE_DMA_FUA48 || 512 cmd == ATA_WRITE_DMA_QUEUED48 || 513 cmd == ATA_WRITE_DMA_QUEUED_FUA48 || 514 cmd == ATA_WRITE_STREAM_DMA48 || 515 cmd == ATA_DATA_SET_MANAGEMENT || 516 cmd == ATA_READ_LOG_DMA_EXT) 517 ataio->cmd.flags |= CAM_ATAIO_DMA; 518 ataio->cmd.command = cmd; 519 ataio->cmd.features = features; 520 ataio->cmd.lba_low = lba; 521 ataio->cmd.lba_mid = lba >> 8; 522 ataio->cmd.lba_high = lba >> 16; 523 ataio->cmd.device = ATA_DEV_LBA; 524 ataio->cmd.lba_low_exp = lba >> 24; 525 ataio->cmd.lba_mid_exp = lba >> 32; 526 ataio->cmd.lba_high_exp = lba >> 40; 527 ataio->cmd.features_exp = features >> 8; 528 ataio->cmd.sector_count = sector_count; 529 ataio->cmd.sector_count_exp = sector_count >> 8; 530 ataio->cmd.control = 0; 531 } 532 533 void 534 ata_ncq_cmd(struct ccb_ataio *ataio, uint8_t cmd, 535 uint64_t lba, uint16_t sector_count) 536 { 537 538 ataio->cmd.flags = CAM_ATAIO_48BIT | CAM_ATAIO_FPDMA; 539 ataio->cmd.command = cmd; 540 ataio->cmd.features = sector_count; 541 ataio->cmd.lba_low = lba; 542 ataio->cmd.lba_mid = lba >> 8; 543 ataio->cmd.lba_high = lba >> 16; 544 ataio->cmd.device = ATA_DEV_LBA; 545 ataio->cmd.lba_low_exp = lba >> 24; 546 ataio->cmd.lba_mid_exp = lba >> 32; 547 ataio->cmd.lba_high_exp = lba >> 40; 548 ataio->cmd.features_exp = sector_count >> 8; 549 ataio->cmd.sector_count = 0; 550 ataio->cmd.sector_count_exp = 0; 551 ataio->cmd.control = 0; 552 } 553 554 void 555 ata_reset_cmd(struct ccb_ataio *ataio) 556 { 557 bzero(&ataio->cmd, sizeof(ataio->cmd)); 558 ataio->cmd.flags = CAM_ATAIO_CONTROL | CAM_ATAIO_NEEDRESULT; 559 ataio->cmd.control = 0x04; 560 } 561 562 void 563 ata_pm_read_cmd(struct ccb_ataio *ataio, int reg, int port) 564 { 565 bzero(&ataio->cmd, sizeof(ataio->cmd)); 566 ataio->cmd.flags = CAM_ATAIO_NEEDRESULT; 567 ataio->cmd.command = ATA_READ_PM; 568 ataio->cmd.features = reg; 569 ataio->cmd.device = port & 0x0f; 570 } 571 572 void 573 ata_pm_write_cmd(struct ccb_ataio *ataio, int reg, int port, uint32_t val) 574 { 575 bzero(&ataio->cmd, sizeof(ataio->cmd)); 576 ataio->cmd.flags = 0; 577 ataio->cmd.command = ATA_WRITE_PM; 578 ataio->cmd.features = reg; 579 ataio->cmd.sector_count = val; 580 ataio->cmd.lba_low = val >> 8; 581 ataio->cmd.lba_mid = val >> 16; 582 ataio->cmd.lba_high = val >> 24; 583 ataio->cmd.device = port & 0x0f; 584 } 585 586 void 587 ata_read_log(struct ccb_ataio *ataio, uint32_t retries, 588 void (*cbfcnp)(struct cam_periph *, union ccb *), 589 uint32_t log_address, uint32_t page_number, uint16_t block_count, 590 uint32_t protocol, uint8_t *data_ptr, uint32_t dxfer_len, 591 uint32_t timeout) 592 { 593 uint64_t lba; 594 595 cam_fill_ataio(ataio, 596 /*retries*/ 1, 597 /*cbfcnp*/ cbfcnp, 598 /*flags*/ CAM_DIR_IN, 599 /*tag_action*/ 0, 600 /*data_ptr*/ data_ptr, 601 /*dxfer_len*/ dxfer_len, 602 /*timeout*/ timeout); 603 604 lba = (((uint64_t)page_number & 0xff00) << 32) | 605 ((page_number & 0x00ff) << 8) | 606 (log_address & 0xff); 607 608 ata_48bit_cmd(ataio, 609 /*cmd*/ (protocol & CAM_ATAIO_DMA) ? ATA_READ_LOG_DMA_EXT : 610 ATA_READ_LOG_EXT, 611 /*features*/ 0, 612 /*lba*/ lba, 613 /*sector_count*/ block_count); 614 } 615 616 void 617 ata_bswap(int8_t *buf, int len) 618 { 619 u_int16_t *ptr = (u_int16_t*)(buf + len); 620 621 while (--ptr >= (u_int16_t*)buf) 622 *ptr = be16toh(*ptr); 623 } 624 625 void 626 ata_btrim(int8_t *buf, int len) 627 { 628 int8_t *ptr; 629 630 for (ptr = buf; ptr < buf+len; ++ptr) 631 if (!*ptr || *ptr == '_') 632 *ptr = ' '; 633 for (ptr = buf + len - 1; ptr >= buf && *ptr == ' '; --ptr) 634 *ptr = 0; 635 } 636 637 void 638 ata_bpack(int8_t *src, int8_t *dst, int len) 639 { 640 int i, j, blank; 641 642 for (i = j = blank = 0 ; i < len; i++) { 643 if (blank && src[i] == ' ') continue; 644 if (blank && src[i] != ' ') { 645 dst[j++] = src[i]; 646 blank = 0; 647 continue; 648 } 649 if (src[i] == ' ') { 650 blank = 1; 651 if (i == 0) 652 continue; 653 } 654 dst[j++] = src[i]; 655 } 656 while (j < len) 657 dst[j++] = 0x00; 658 } 659 660 int 661 ata_max_pmode(struct ata_params *ap) 662 { 663 if (ap->atavalid & ATA_FLAG_64_70) { 664 if (ap->apiomodes & 0x02) 665 return ATA_PIO4; 666 if (ap->apiomodes & 0x01) 667 return ATA_PIO3; 668 } 669 if (ap->mwdmamodes & 0x04) 670 return ATA_PIO4; 671 if (ap->mwdmamodes & 0x02) 672 return ATA_PIO3; 673 if (ap->mwdmamodes & 0x01) 674 return ATA_PIO2; 675 if ((ap->retired_piomode & ATA_RETIRED_PIO_MASK) == 0x200) 676 return ATA_PIO2; 677 if ((ap->retired_piomode & ATA_RETIRED_PIO_MASK) == 0x100) 678 return ATA_PIO1; 679 if ((ap->retired_piomode & ATA_RETIRED_PIO_MASK) == 0x000) 680 return ATA_PIO0; 681 return ATA_PIO0; 682 } 683 684 int 685 ata_max_wmode(struct ata_params *ap) 686 { 687 if (ap->mwdmamodes & 0x04) 688 return ATA_WDMA2; 689 if (ap->mwdmamodes & 0x02) 690 return ATA_WDMA1; 691 if (ap->mwdmamodes & 0x01) 692 return ATA_WDMA0; 693 return -1; 694 } 695 696 int 697 ata_max_umode(struct ata_params *ap) 698 { 699 if (ap->atavalid & ATA_FLAG_88) { 700 if (ap->udmamodes & 0x40) 701 return ATA_UDMA6; 702 if (ap->udmamodes & 0x20) 703 return ATA_UDMA5; 704 if (ap->udmamodes & 0x10) 705 return ATA_UDMA4; 706 if (ap->udmamodes & 0x08) 707 return ATA_UDMA3; 708 if (ap->udmamodes & 0x04) 709 return ATA_UDMA2; 710 if (ap->udmamodes & 0x02) 711 return ATA_UDMA1; 712 if (ap->udmamodes & 0x01) 713 return ATA_UDMA0; 714 } 715 return -1; 716 } 717 718 int 719 ata_max_mode(struct ata_params *ap, int maxmode) 720 { 721 722 if (maxmode == 0) 723 maxmode = ATA_DMA_MAX; 724 if (maxmode >= ATA_UDMA0 && ata_max_umode(ap) > 0) 725 return (min(maxmode, ata_max_umode(ap))); 726 if (maxmode >= ATA_WDMA0 && ata_max_wmode(ap) > 0) 727 return (min(maxmode, ata_max_wmode(ap))); 728 return (min(maxmode, ata_max_pmode(ap))); 729 } 730 731 char * 732 ata_mode2string(int mode) 733 { 734 switch (mode) { 735 case -1: return "UNSUPPORTED"; 736 case 0: return "NONE"; 737 case ATA_PIO0: return "PIO0"; 738 case ATA_PIO1: return "PIO1"; 739 case ATA_PIO2: return "PIO2"; 740 case ATA_PIO3: return "PIO3"; 741 case ATA_PIO4: return "PIO4"; 742 case ATA_WDMA0: return "WDMA0"; 743 case ATA_WDMA1: return "WDMA1"; 744 case ATA_WDMA2: return "WDMA2"; 745 case ATA_UDMA0: return "UDMA0"; 746 case ATA_UDMA1: return "UDMA1"; 747 case ATA_UDMA2: return "UDMA2"; 748 case ATA_UDMA3: return "UDMA3"; 749 case ATA_UDMA4: return "UDMA4"; 750 case ATA_UDMA5: return "UDMA5"; 751 case ATA_UDMA6: return "UDMA6"; 752 default: 753 if (mode & ATA_DMA_MASK) 754 return "BIOSDMA"; 755 else 756 return "BIOSPIO"; 757 } 758 } 759 760 int 761 ata_string2mode(char *str) 762 { 763 if (!strcasecmp(str, "PIO0")) return (ATA_PIO0); 764 if (!strcasecmp(str, "PIO1")) return (ATA_PIO1); 765 if (!strcasecmp(str, "PIO2")) return (ATA_PIO2); 766 if (!strcasecmp(str, "PIO3")) return (ATA_PIO3); 767 if (!strcasecmp(str, "PIO4")) return (ATA_PIO4); 768 if (!strcasecmp(str, "WDMA0")) return (ATA_WDMA0); 769 if (!strcasecmp(str, "WDMA1")) return (ATA_WDMA1); 770 if (!strcasecmp(str, "WDMA2")) return (ATA_WDMA2); 771 if (!strcasecmp(str, "UDMA0")) return (ATA_UDMA0); 772 if (!strcasecmp(str, "UDMA16")) return (ATA_UDMA0); 773 if (!strcasecmp(str, "UDMA1")) return (ATA_UDMA1); 774 if (!strcasecmp(str, "UDMA25")) return (ATA_UDMA1); 775 if (!strcasecmp(str, "UDMA2")) return (ATA_UDMA2); 776 if (!strcasecmp(str, "UDMA33")) return (ATA_UDMA2); 777 if (!strcasecmp(str, "UDMA3")) return (ATA_UDMA3); 778 if (!strcasecmp(str, "UDMA44")) return (ATA_UDMA3); 779 if (!strcasecmp(str, "UDMA4")) return (ATA_UDMA4); 780 if (!strcasecmp(str, "UDMA66")) return (ATA_UDMA4); 781 if (!strcasecmp(str, "UDMA5")) return (ATA_UDMA5); 782 if (!strcasecmp(str, "UDMA100")) return (ATA_UDMA5); 783 if (!strcasecmp(str, "UDMA6")) return (ATA_UDMA6); 784 if (!strcasecmp(str, "UDMA133")) return (ATA_UDMA6); 785 return (-1); 786 } 787 788 789 u_int 790 ata_mode2speed(int mode) 791 { 792 switch (mode) { 793 case ATA_PIO0: 794 default: 795 return (3300); 796 case ATA_PIO1: 797 return (5200); 798 case ATA_PIO2: 799 return (8300); 800 case ATA_PIO3: 801 return (11100); 802 case ATA_PIO4: 803 return (16700); 804 case ATA_WDMA0: 805 return (4200); 806 case ATA_WDMA1: 807 return (13300); 808 case ATA_WDMA2: 809 return (16700); 810 case ATA_UDMA0: 811 return (16700); 812 case ATA_UDMA1: 813 return (25000); 814 case ATA_UDMA2: 815 return (33300); 816 case ATA_UDMA3: 817 return (44400); 818 case ATA_UDMA4: 819 return (66700); 820 case ATA_UDMA5: 821 return (100000); 822 case ATA_UDMA6: 823 return (133000); 824 } 825 } 826 827 u_int 828 ata_revision2speed(int revision) 829 { 830 switch (revision) { 831 case 1: 832 default: 833 return (150000); 834 case 2: 835 return (300000); 836 case 3: 837 return (600000); 838 } 839 } 840 841 int 842 ata_speed2revision(u_int speed) 843 { 844 switch (speed) { 845 case 0: 846 return (0); 847 case 150000: 848 return (1); 849 case 300000: 850 return (2); 851 case 600000: 852 return (3); 853 default: 854 return (-1); 855 } 856 } 857 858 int 859 ata_identify_match(caddr_t identbuffer, caddr_t table_entry) 860 { 861 struct scsi_inquiry_pattern *entry; 862 struct ata_params *ident; 863 864 entry = (struct scsi_inquiry_pattern *)table_entry; 865 ident = (struct ata_params *)identbuffer; 866 867 if ((cam_strmatch(ident->model, entry->product, 868 sizeof(ident->model)) == 0) 869 && (cam_strmatch(ident->revision, entry->revision, 870 sizeof(ident->revision)) == 0)) { 871 return (0); 872 } 873 return (-1); 874 } 875 876 int 877 ata_static_identify_match(caddr_t identbuffer, caddr_t table_entry) 878 { 879 struct scsi_static_inquiry_pattern *entry; 880 struct ata_params *ident; 881 882 entry = (struct scsi_static_inquiry_pattern *)table_entry; 883 ident = (struct ata_params *)identbuffer; 884 885 if ((cam_strmatch(ident->model, entry->product, 886 sizeof(ident->model)) == 0) 887 && (cam_strmatch(ident->revision, entry->revision, 888 sizeof(ident->revision)) == 0)) { 889 return (0); 890 } 891 return (-1); 892 } 893 894 void 895 semb_receive_diagnostic_results(struct ccb_ataio *ataio, 896 u_int32_t retries, void (*cbfcnp)(struct cam_periph *, union ccb*), 897 uint8_t tag_action, int pcv, uint8_t page_code, 898 uint8_t *data_ptr, uint16_t length, uint32_t timeout) 899 { 900 901 length = min(length, 1020); 902 length = (length + 3) & ~3; 903 cam_fill_ataio(ataio, 904 retries, 905 cbfcnp, 906 /*flags*/CAM_DIR_IN, 907 tag_action, 908 data_ptr, 909 length, 910 timeout); 911 ata_28bit_cmd(ataio, ATA_SEP_ATTN, 912 pcv ? page_code : 0, 0x02, length / 4); 913 } 914 915 void 916 semb_send_diagnostic(struct ccb_ataio *ataio, 917 u_int32_t retries, void (*cbfcnp)(struct cam_periph *, union ccb *), 918 uint8_t tag_action, uint8_t *data_ptr, uint16_t length, uint32_t timeout) 919 { 920 921 length = min(length, 1020); 922 length = (length + 3) & ~3; 923 cam_fill_ataio(ataio, 924 retries, 925 cbfcnp, 926 /*flags*/length ? CAM_DIR_OUT : CAM_DIR_NONE, 927 tag_action, 928 data_ptr, 929 length, 930 timeout); 931 ata_28bit_cmd(ataio, ATA_SEP_ATTN, 932 length > 0 ? data_ptr[0] : 0, 0x82, length / 4); 933 } 934 935 void 936 semb_read_buffer(struct ccb_ataio *ataio, 937 u_int32_t retries, void (*cbfcnp)(struct cam_periph *, union ccb*), 938 uint8_t tag_action, uint8_t page_code, 939 uint8_t *data_ptr, uint16_t length, uint32_t timeout) 940 { 941 942 length = min(length, 1020); 943 length = (length + 3) & ~3; 944 cam_fill_ataio(ataio, 945 retries, 946 cbfcnp, 947 /*flags*/CAM_DIR_IN, 948 tag_action, 949 data_ptr, 950 length, 951 timeout); 952 ata_28bit_cmd(ataio, ATA_SEP_ATTN, 953 page_code, 0x00, length / 4); 954 } 955 956 void 957 semb_write_buffer(struct ccb_ataio *ataio, 958 u_int32_t retries, void (*cbfcnp)(struct cam_periph *, union ccb *), 959 uint8_t tag_action, uint8_t *data_ptr, uint16_t length, uint32_t timeout) 960 { 961 962 length = min(length, 1020); 963 length = (length + 3) & ~3; 964 cam_fill_ataio(ataio, 965 retries, 966 cbfcnp, 967 /*flags*/length ? CAM_DIR_OUT : CAM_DIR_NONE, 968 tag_action, 969 data_ptr, 970 length, 971 timeout); 972 ata_28bit_cmd(ataio, ATA_SEP_ATTN, 973 length > 0 ? data_ptr[0] : 0, 0x80, length / 4); 974 } 975 976 977 void 978 ata_zac_mgmt_out(struct ccb_ataio *ataio, uint32_t retries, 979 void (*cbfcnp)(struct cam_periph *, union ccb *), 980 int use_ncq, uint8_t zm_action, uint64_t zone_id, 981 uint8_t zone_flags, uint16_t sector_count, uint8_t *data_ptr, 982 uint32_t dxfer_len, uint32_t timeout) 983 { 984 uint8_t command_out, ata_flags; 985 uint16_t features_out, sectors_out; 986 uint32_t auxiliary; 987 988 if (use_ncq == 0) { 989 command_out = ATA_ZAC_MANAGEMENT_OUT; 990 features_out = (zm_action & 0xf) | (zone_flags << 8); 991 if (dxfer_len == 0) { 992 ata_flags = 0; 993 sectors_out = 0; 994 } else { 995 ata_flags = CAM_ATAIO_DMA; 996 /* XXX KDM use sector count? */ 997 sectors_out = ((dxfer_len >> 9) & 0xffff); 998 } 999 auxiliary = 0; 1000 } else { 1001 if (dxfer_len == 0) { 1002 command_out = ATA_NCQ_NON_DATA; 1003 features_out = ATA_NCQ_ZAC_MGMT_OUT; 1004 sectors_out = 0; 1005 } else { 1006 command_out = ATA_SEND_FPDMA_QUEUED; 1007 1008 /* Note that we're defaulting to normal priority */ 1009 sectors_out = ATA_SFPDMA_ZAC_MGMT_OUT << 8; 1010 1011 /* 1012 * For SEND FPDMA QUEUED, the transfer length is 1013 * encoded in the FEATURE register, and 0 means 1014 * that 65536 512 byte blocks are to be tranferred. 1015 * In practice, it seems unlikely that we'll see 1016 * a transfer that large. 1017 */ 1018 if (dxfer_len == (65536 * 512)) { 1019 features_out = 0; 1020 } else { 1021 /* 1022 * Yes, the caller can theoretically send a 1023 * transfer larger than we can handle. 1024 * Anyone using this function needs enough 1025 * knowledge to avoid doing that. 1026 */ 1027 features_out = ((dxfer_len >> 9) & 0xffff); 1028 } 1029 } 1030 auxiliary = (zm_action & 0xf) | (zone_flags << 8); 1031 1032 ata_flags = CAM_ATAIO_FPDMA; 1033 } 1034 1035 cam_fill_ataio(ataio, 1036 /*retries*/ retries, 1037 /*cbfcnp*/ cbfcnp, 1038 /*flags*/ (dxfer_len > 0) ? CAM_DIR_OUT : CAM_DIR_NONE, 1039 /*tag_action*/ 0, 1040 /*data_ptr*/ data_ptr, 1041 /*dxfer_len*/ dxfer_len, 1042 /*timeout*/ timeout); 1043 1044 ata_48bit_cmd(ataio, 1045 /*cmd*/ command_out, 1046 /*features*/ features_out, 1047 /*lba*/ zone_id, 1048 /*sector_count*/ sectors_out); 1049 1050 ataio->cmd.flags |= ata_flags; 1051 if (auxiliary != 0) { 1052 ataio->ata_flags |= ATA_FLAG_AUX; 1053 ataio->aux = auxiliary; 1054 } 1055 } 1056 1057 void 1058 ata_zac_mgmt_in(struct ccb_ataio *ataio, uint32_t retries, 1059 void (*cbfcnp)(struct cam_periph *, union ccb *), 1060 int use_ncq, uint8_t zm_action, uint64_t zone_id, 1061 uint8_t zone_flags, uint8_t *data_ptr, uint32_t dxfer_len, 1062 uint32_t timeout) 1063 { 1064 uint8_t command_out, ata_flags; 1065 uint16_t features_out, sectors_out; 1066 uint32_t auxiliary; 1067 1068 if (use_ncq == 0) { 1069 command_out = ATA_ZAC_MANAGEMENT_IN; 1070 /* XXX KDM put a macro here */ 1071 features_out = (zm_action & 0xf) | (zone_flags << 8); 1072 ata_flags = CAM_ATAIO_DMA; 1073 sectors_out = ((dxfer_len >> 9) & 0xffff); 1074 auxiliary = 0; 1075 } else { 1076 command_out = ATA_RECV_FPDMA_QUEUED; 1077 sectors_out = ATA_RFPDMA_ZAC_MGMT_IN << 8; 1078 auxiliary = (zm_action & 0xf) | (zone_flags << 8), 1079 ata_flags = CAM_ATAIO_FPDMA; 1080 /* 1081 * For RECEIVE FPDMA QUEUED, the transfer length is 1082 * encoded in the FEATURE register, and 0 means 1083 * that 65536 512 byte blocks are to be tranferred. 1084 * In practice, it is unlikely we will see a transfer that 1085 * large. 1086 */ 1087 if (dxfer_len == (65536 * 512)) { 1088 features_out = 0; 1089 } else { 1090 /* 1091 * Yes, the caller can theoretically request a 1092 * transfer larger than we can handle. 1093 * Anyone using this function needs enough 1094 * knowledge to avoid doing that. 1095 */ 1096 features_out = ((dxfer_len >> 9) & 0xffff); 1097 } 1098 } 1099 1100 cam_fill_ataio(ataio, 1101 /*retries*/ retries, 1102 /*cbfcnp*/ cbfcnp, 1103 /*flags*/ CAM_DIR_IN, 1104 /*tag_action*/ 0, 1105 /*data_ptr*/ data_ptr, 1106 /*dxfer_len*/ dxfer_len, 1107 /*timeout*/ timeout); 1108 1109 ata_48bit_cmd(ataio, 1110 /*cmd*/ command_out, 1111 /*features*/ features_out, 1112 /*lba*/ zone_id, 1113 /*sector_count*/ sectors_out); 1114 1115 ataio->cmd.flags |= ata_flags; 1116 if (auxiliary != 0) { 1117 ataio->ata_flags |= ATA_FLAG_AUX; 1118 ataio->aux = auxiliary; 1119 } 1120 } 1121