ata_all.c revision 308075
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: stable/10/sys/cam/ata/ata_all.c 308075 2016-10-29 08:42:43Z mav $"); 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 58int 59ata_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 71char * 72ata_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 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 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 0x51: return ("CONFIGURE_STREAM"); 121 case 0x57: return ("WRITE_LOG_DMA_EXT"); 122 case 0x5b: return ("TRUSTED_NON_DATA"); 123 case 0x5c: return ("TRUSTED_RECEIVE"); 124 case 0x5d: return ("TRUSTED_RECEIVE_DMA"); 125 case 0x5e: return ("TRUSTED_SEND"); 126 case 0x5f: return ("TRUSTED_SEND_DMA"); 127 case 0x60: return ("READ_FPDMA_QUEUED"); 128 case 0x61: return ("WRITE_FPDMA_QUEUED"); 129 case 0x63: return ("NCQ_NON_DATA"); 130 case 0x64: return ("SEND_FPDMA_QUEUED"); 131 case 0x65: return ("RECEIVE_FPDMA_QUEUED"); 132 case 0x67: 133 if (cmd->features == 0xec) 134 return ("SEP_ATTN IDENTIFY"); 135 switch (cmd->lba_low) { 136 case 0x00: return ("SEP_ATTN READ BUFFER"); 137 case 0x02: return ("SEP_ATTN RECEIVE DIAGNOSTIC RESULTS"); 138 case 0x80: return ("SEP_ATTN WRITE BUFFER"); 139 case 0x82: return ("SEP_ATTN SEND DIAGNOSTIC"); 140 } 141 return ("SEP_ATTN"); 142 case 0x70: return ("SEEK"); 143 case 0x77: return ("SET_DATE_TIME_EXT"); 144 case 0x78: return ("ACCESSIBLE_MAX_ADDRESS_CONFIGURATION"); 145 case 0x87: return ("CFA_TRANSLATE_SECTOR"); 146 case 0x90: return ("EXECUTE_DEVICE_DIAGNOSTIC"); 147 case 0x92: return ("DOWNLOAD_MICROCODE"); 148 case 0x93: return ("DOWNLOAD_MICROCODE_DMA"); 149 case 0x9a: return ("ZAC_MANAGEMENT_OUT"); 150 case 0xa0: return ("PACKET"); 151 case 0xa1: return ("ATAPI_IDENTIFY"); 152 case 0xa2: return ("SERVICE"); 153 case 0xb0: 154 switch(cmd->features) { 155 case 0xd0: return ("SMART READ ATTR VALUES"); 156 case 0xd1: return ("SMART READ ATTR THRESHOLDS"); 157 case 0xd3: return ("SMART SAVE ATTR VALUES"); 158 case 0xd4: return ("SMART EXECUTE OFFLINE IMMEDIATE"); 159 case 0xd5: return ("SMART READ LOG DATA"); 160 case 0xd8: return ("SMART ENABLE OPERATION"); 161 case 0xd9: return ("SMART DISABLE OPERATION"); 162 case 0xda: return ("SMART RETURN STATUS"); 163 } 164 return ("SMART"); 165 case 0xb1: return ("DEVICE CONFIGURATION"); 166 case 0xb4: return ("SANITIZE_DEVICE"); 167 case 0xc0: return ("CFA_ERASE"); 168 case 0xc4: return ("READ_MUL"); 169 case 0xc5: return ("WRITE_MUL"); 170 case 0xc6: return ("SET_MULTI"); 171 case 0xc7: return ("READ_DMA_QUEUED"); 172 case 0xc8: return ("READ_DMA"); 173 case 0xca: return ("WRITE_DMA"); 174 case 0xcc: return ("WRITE_DMA_QUEUED"); 175 case 0xcd: return ("CFA_WRITE_MULTIPLE_WITHOUT_ERASE"); 176 case 0xce: return ("WRITE_MUL_FUA48"); 177 case 0xd1: return ("CHECK_MEDIA_CARD_TYPE"); 178 case 0xda: return ("GET_MEDIA_STATUS"); 179 case 0xde: return ("MEDIA_LOCK"); 180 case 0xdf: return ("MEDIA_UNLOCK"); 181 case 0xe0: return ("STANDBY_IMMEDIATE"); 182 case 0xe1: return ("IDLE_IMMEDIATE"); 183 case 0xe2: return ("STANDBY"); 184 case 0xe3: return ("IDLE"); 185 case 0xe4: return ("READ_BUFFER/PM"); 186 case 0xe5: return ("CHECK_POWER_MODE"); 187 case 0xe6: return ("SLEEP"); 188 case 0xe7: return ("FLUSHCACHE"); 189 case 0xe8: return ("WRITE_PM"); 190 case 0xea: return ("FLUSHCACHE48"); 191 case 0xec: return ("ATA_IDENTIFY"); 192 case 0xed: return ("MEDIA_EJECT"); 193 case 0xef: 194 switch (cmd->features) { 195 case 0x03: return ("SETFEATURES SET TRANSFER MODE"); 196 case 0x02: return ("SETFEATURES ENABLE WCACHE"); 197 case 0x82: return ("SETFEATURES DISABLE WCACHE"); 198 case 0x06: return ("SETFEATURES ENABLE PUIS"); 199 case 0x86: return ("SETFEATURES DISABLE PUIS"); 200 case 0x07: return ("SETFEATURES SPIN-UP"); 201 case 0x10: return ("SETFEATURES ENABLE SATA FEATURE"); 202 case 0x90: return ("SETFEATURES DISABLE SATA FEATURE"); 203 case 0xaa: return ("SETFEATURES ENABLE RCACHE"); 204 case 0x55: return ("SETFEATURES DISABLE RCACHE"); 205 case 0x5d: return ("SETFEATURES ENABLE RELIRQ"); 206 case 0xdd: return ("SETFEATURES DISABLE RELIRQ"); 207 case 0x5e: return ("SETFEATURES ENABLE SRVIRQ"); 208 case 0xde: return ("SETFEATURES DISABLE SRVIRQ"); 209 } 210 return "SETFEATURES"; 211 case 0xf1: return ("SECURITY_SET_PASSWORD"); 212 case 0xf2: return ("SECURITY_UNLOCK"); 213 case 0xf3: return ("SECURITY_ERASE_PREPARE"); 214 case 0xf4: return ("SECURITY_ERASE_UNIT"); 215 case 0xf5: return ("SECURITY_FREEZE_LOCK"); 216 case 0xf6: return ("SECURITY_DISABLE_PASSWORD"); 217 case 0xf8: return ("READ_NATIVE_MAX_ADDRESS"); 218 case 0xf9: return ("SET_MAX_ADDRESS"); 219 } 220 return "UNKNOWN"; 221} 222 223char * 224ata_cmd_string(struct ata_cmd *cmd, char *cmd_string, size_t len) 225{ 226 struct sbuf sb; 227 int error; 228 229 if (len == 0) 230 return (""); 231 232 sbuf_new(&sb, cmd_string, len, SBUF_FIXEDLEN); 233 ata_cmd_sbuf(cmd, &sb); 234 235 error = sbuf_finish(&sb); 236 if (error != 0 && error != ENOMEM) 237 return (""); 238 239 return(sbuf_data(&sb)); 240} 241 242void 243ata_cmd_sbuf(struct ata_cmd *cmd, struct sbuf *sb) 244{ 245 sbuf_printf(sb, "%02x %02x %02x %02x " 246 "%02x %02x %02x %02x %02x %02x %02x %02x", 247 cmd->command, cmd->features, 248 cmd->lba_low, cmd->lba_mid, cmd->lba_high, cmd->device, 249 cmd->lba_low_exp, cmd->lba_mid_exp, cmd->lba_high_exp, 250 cmd->features_exp, cmd->sector_count, cmd->sector_count_exp); 251} 252 253char * 254ata_res_string(struct ata_res *res, char *res_string, size_t len) 255{ 256 257 snprintf(res_string, len, "%02x %02x %02x %02x " 258 "%02x %02x %02x %02x %02x %02x %02x", 259 res->status, res->error, 260 res->lba_low, res->lba_mid, res->lba_high, res->device, 261 res->lba_low_exp, res->lba_mid_exp, res->lba_high_exp, 262 res->sector_count, res->sector_count_exp); 263 264 return (res_string); 265} 266 267/* 268 * ata_command_sbuf() returns 0 for success and -1 for failure. 269 */ 270int 271ata_command_sbuf(struct ccb_ataio *ataio, struct sbuf *sb) 272{ 273 274 sbuf_printf(sb, "%s. ACB: ", 275 ata_op_string(&ataio->cmd)); 276 ata_cmd_sbuf(&ataio->cmd, sb); 277 278 return(0); 279} 280 281/* 282 * ata_status_abuf() returns 0 for success and -1 for failure. 283 */ 284int 285ata_status_sbuf(struct ccb_ataio *ataio, struct sbuf *sb) 286{ 287 288 sbuf_printf(sb, "ATA status: %02x (%s%s%s%s%s%s%s%s)", 289 ataio->res.status, 290 (ataio->res.status & 0x80) ? "BSY " : "", 291 (ataio->res.status & 0x40) ? "DRDY " : "", 292 (ataio->res.status & 0x20) ? "DF " : "", 293 (ataio->res.status & 0x10) ? "SERV " : "", 294 (ataio->res.status & 0x08) ? "DRQ " : "", 295 (ataio->res.status & 0x04) ? "CORR " : "", 296 (ataio->res.status & 0x02) ? "IDX " : "", 297 (ataio->res.status & 0x01) ? "ERR" : ""); 298 if (ataio->res.status & 1) { 299 sbuf_printf(sb, ", error: %02x (%s%s%s%s%s%s%s%s)", 300 ataio->res.error, 301 (ataio->res.error & 0x80) ? "ICRC " : "", 302 (ataio->res.error & 0x40) ? "UNC " : "", 303 (ataio->res.error & 0x20) ? "MC " : "", 304 (ataio->res.error & 0x10) ? "IDNF " : "", 305 (ataio->res.error & 0x08) ? "MCR " : "", 306 (ataio->res.error & 0x04) ? "ABRT " : "", 307 (ataio->res.error & 0x02) ? "NM " : "", 308 (ataio->res.error & 0x01) ? "ILI" : ""); 309 } 310 311 return(0); 312} 313 314/* 315 * ata_res_sbuf() returns 0 for success and -1 for failure. 316 */ 317int 318ata_res_sbuf(struct ccb_ataio *ataio, struct sbuf *sb) 319{ 320 char res_str[(11 * 3) + 1]; 321 322 sbuf_printf(sb, "RES: %s", 323 ata_res_string(&ataio->res, res_str, sizeof(res_str))); 324 325 return(0); 326} 327 328void 329ata_print_ident(struct ata_params *ident_data) 330{ 331 const char *proto; 332 char product[48], revision[16], ata[12], sata[12]; 333 334 cam_strvis(product, ident_data->model, sizeof(ident_data->model), 335 sizeof(product)); 336 cam_strvis(revision, ident_data->revision, sizeof(ident_data->revision), 337 sizeof(revision)); 338 proto = (ident_data->config == ATA_PROTO_CFA) ? "CFA" : 339 (ident_data->config & ATA_PROTO_ATAPI) ? "ATAPI" : "ATA"; 340 if (ata_version(ident_data->version_major) == 0) { 341 snprintf(ata, sizeof(ata), "%s", proto); 342 } else if (ata_version(ident_data->version_major) <= 7) { 343 snprintf(ata, sizeof(ata), "%s-%d", proto, 344 ata_version(ident_data->version_major)); 345 } else if (ata_version(ident_data->version_major) == 8) { 346 snprintf(ata, sizeof(ata), "%s8-ACS", proto); 347 } else { 348 snprintf(ata, sizeof(ata), "ACS-%d %s", 349 ata_version(ident_data->version_major) - 7, proto); 350 } 351 if (ident_data->satacapabilities && ident_data->satacapabilities != 0xffff) { 352 if (ident_data->satacapabilities & ATA_SATA_GEN3) 353 snprintf(sata, sizeof(sata), " SATA 3.x"); 354 else if (ident_data->satacapabilities & ATA_SATA_GEN2) 355 snprintf(sata, sizeof(sata), " SATA 2.x"); 356 else if (ident_data->satacapabilities & ATA_SATA_GEN1) 357 snprintf(sata, sizeof(sata), " SATA 1.x"); 358 else 359 snprintf(sata, sizeof(sata), " SATA"); 360 } else 361 sata[0] = 0; 362 printf("<%s %s> %s%s device\n", product, revision, ata, sata); 363} 364 365void 366ata_print_ident_short(struct ata_params *ident_data) 367{ 368 char product[48], revision[16]; 369 370 cam_strvis(product, ident_data->model, sizeof(ident_data->model), 371 sizeof(product)); 372 cam_strvis(revision, ident_data->revision, sizeof(ident_data->revision), 373 sizeof(revision)); 374 printf("<%s %s>", product, revision); 375} 376 377void 378semb_print_ident(struct sep_identify_data *ident_data) 379{ 380 char vendor[9], product[17], revision[5], fw[5], in[7], ins[5]; 381 382 cam_strvis(vendor, ident_data->vendor_id, 8, sizeof(vendor)); 383 cam_strvis(product, ident_data->product_id, 16, sizeof(product)); 384 cam_strvis(revision, ident_data->product_rev, 4, sizeof(revision)); 385 cam_strvis(fw, ident_data->firmware_rev, 4, sizeof(fw)); 386 cam_strvis(in, ident_data->interface_id, 6, sizeof(in)); 387 cam_strvis(ins, ident_data->interface_rev, 4, sizeof(ins)); 388 printf("<%s %s %s %s> SEMB %s %s device\n", 389 vendor, product, revision, fw, in, ins); 390} 391 392void 393semb_print_ident_short(struct sep_identify_data *ident_data) 394{ 395 char vendor[9], product[17], revision[5], fw[5]; 396 397 cam_strvis(vendor, ident_data->vendor_id, 8, sizeof(vendor)); 398 cam_strvis(product, ident_data->product_id, 16, sizeof(product)); 399 cam_strvis(revision, ident_data->product_rev, 4, sizeof(revision)); 400 cam_strvis(fw, ident_data->firmware_rev, 4, sizeof(fw)); 401 printf("<%s %s %s %s>", vendor, product, revision, fw); 402} 403 404uint32_t 405ata_logical_sector_size(struct ata_params *ident_data) 406{ 407 if ((ident_data->pss & ATA_PSS_VALID_MASK) == ATA_PSS_VALID_VALUE && 408 (ident_data->pss & ATA_PSS_LSSABOVE512)) { 409 return (((u_int32_t)ident_data->lss_1 | 410 ((u_int32_t)ident_data->lss_2 << 16)) * 2); 411 } 412 return (512); 413} 414 415uint64_t 416ata_physical_sector_size(struct ata_params *ident_data) 417{ 418 if ((ident_data->pss & ATA_PSS_VALID_MASK) == ATA_PSS_VALID_VALUE) { 419 if (ident_data->pss & ATA_PSS_MULTLS) { 420 return ((uint64_t)ata_logical_sector_size(ident_data) * 421 (1 << (ident_data->pss & ATA_PSS_LSPPS))); 422 } else { 423 return (uint64_t)ata_logical_sector_size(ident_data); 424 } 425 } 426 return (512); 427} 428 429uint64_t 430ata_logical_sector_offset(struct ata_params *ident_data) 431{ 432 if ((ident_data->lsalign & 0xc000) == 0x4000) { 433 return ((uint64_t)ata_logical_sector_size(ident_data) * 434 (ident_data->lsalign & 0x3fff)); 435 } 436 return (0); 437} 438 439void 440ata_28bit_cmd(struct ccb_ataio *ataio, uint8_t cmd, uint8_t features, 441 uint32_t lba, uint8_t sector_count) 442{ 443 bzero(&ataio->cmd, sizeof(ataio->cmd)); 444 ataio->cmd.flags = 0; 445 if (cmd == ATA_READ_DMA || 446 cmd == ATA_READ_DMA_QUEUED || 447 cmd == ATA_WRITE_DMA || 448 cmd == ATA_WRITE_DMA_QUEUED) 449 ataio->cmd.flags |= CAM_ATAIO_DMA; 450 ataio->cmd.command = cmd; 451 ataio->cmd.features = features; 452 ataio->cmd.lba_low = lba; 453 ataio->cmd.lba_mid = lba >> 8; 454 ataio->cmd.lba_high = lba >> 16; 455 ataio->cmd.device = ATA_DEV_LBA | ((lba >> 24) & 0x0f); 456 ataio->cmd.sector_count = sector_count; 457} 458 459void 460ata_48bit_cmd(struct ccb_ataio *ataio, uint8_t cmd, uint16_t features, 461 uint64_t lba, uint16_t sector_count) 462{ 463 464 ataio->cmd.flags = CAM_ATAIO_48BIT; 465 if (cmd == ATA_READ_DMA48 || 466 cmd == ATA_READ_DMA_QUEUED48 || 467 cmd == ATA_READ_STREAM_DMA48 || 468 cmd == ATA_WRITE_DMA48 || 469 cmd == ATA_WRITE_DMA_FUA48 || 470 cmd == ATA_WRITE_DMA_QUEUED48 || 471 cmd == ATA_WRITE_DMA_QUEUED_FUA48 || 472 cmd == ATA_WRITE_STREAM_DMA48 || 473 cmd == ATA_DATA_SET_MANAGEMENT) 474 ataio->cmd.flags |= CAM_ATAIO_DMA; 475 ataio->cmd.command = cmd; 476 ataio->cmd.features = features; 477 ataio->cmd.lba_low = lba; 478 ataio->cmd.lba_mid = lba >> 8; 479 ataio->cmd.lba_high = lba >> 16; 480 ataio->cmd.device = ATA_DEV_LBA; 481 ataio->cmd.lba_low_exp = lba >> 24; 482 ataio->cmd.lba_mid_exp = lba >> 32; 483 ataio->cmd.lba_high_exp = lba >> 40; 484 ataio->cmd.features_exp = features >> 8; 485 ataio->cmd.sector_count = sector_count; 486 ataio->cmd.sector_count_exp = sector_count >> 8; 487 ataio->cmd.control = 0; 488} 489 490void 491ata_ncq_cmd(struct ccb_ataio *ataio, uint8_t cmd, 492 uint64_t lba, uint16_t sector_count) 493{ 494 495 ataio->cmd.flags = CAM_ATAIO_48BIT | CAM_ATAIO_FPDMA; 496 ataio->cmd.command = cmd; 497 ataio->cmd.features = sector_count; 498 ataio->cmd.lba_low = lba; 499 ataio->cmd.lba_mid = lba >> 8; 500 ataio->cmd.lba_high = lba >> 16; 501 ataio->cmd.device = ATA_DEV_LBA; 502 ataio->cmd.lba_low_exp = lba >> 24; 503 ataio->cmd.lba_mid_exp = lba >> 32; 504 ataio->cmd.lba_high_exp = lba >> 40; 505 ataio->cmd.features_exp = sector_count >> 8; 506 ataio->cmd.sector_count = 0; 507 ataio->cmd.sector_count_exp = 0; 508 ataio->cmd.control = 0; 509} 510 511void 512ata_reset_cmd(struct ccb_ataio *ataio) 513{ 514 bzero(&ataio->cmd, sizeof(ataio->cmd)); 515 ataio->cmd.flags = CAM_ATAIO_CONTROL | CAM_ATAIO_NEEDRESULT; 516 ataio->cmd.control = 0x04; 517} 518 519void 520ata_pm_read_cmd(struct ccb_ataio *ataio, int reg, int port) 521{ 522 bzero(&ataio->cmd, sizeof(ataio->cmd)); 523 ataio->cmd.flags = CAM_ATAIO_NEEDRESULT; 524 ataio->cmd.command = ATA_READ_PM; 525 ataio->cmd.features = reg; 526 ataio->cmd.device = port & 0x0f; 527} 528 529void 530ata_pm_write_cmd(struct ccb_ataio *ataio, int reg, int port, uint32_t val) 531{ 532 bzero(&ataio->cmd, sizeof(ataio->cmd)); 533 ataio->cmd.flags = 0; 534 ataio->cmd.command = ATA_WRITE_PM; 535 ataio->cmd.features = reg; 536 ataio->cmd.sector_count = val; 537 ataio->cmd.lba_low = val >> 8; 538 ataio->cmd.lba_mid = val >> 16; 539 ataio->cmd.lba_high = val >> 24; 540 ataio->cmd.device = port & 0x0f; 541} 542 543void 544ata_bswap(int8_t *buf, int len) 545{ 546 u_int16_t *ptr = (u_int16_t*)(buf + len); 547 548 while (--ptr >= (u_int16_t*)buf) 549 *ptr = be16toh(*ptr); 550} 551 552void 553ata_btrim(int8_t *buf, int len) 554{ 555 int8_t *ptr; 556 557 for (ptr = buf; ptr < buf+len; ++ptr) 558 if (!*ptr || *ptr == '_') 559 *ptr = ' '; 560 for (ptr = buf + len - 1; ptr >= buf && *ptr == ' '; --ptr) 561 *ptr = 0; 562} 563 564void 565ata_bpack(int8_t *src, int8_t *dst, int len) 566{ 567 int i, j, blank; 568 569 for (i = j = blank = 0 ; i < len; i++) { 570 if (blank && src[i] == ' ') continue; 571 if (blank && src[i] != ' ') { 572 dst[j++] = src[i]; 573 blank = 0; 574 continue; 575 } 576 if (src[i] == ' ') { 577 blank = 1; 578 if (i == 0) 579 continue; 580 } 581 dst[j++] = src[i]; 582 } 583 while (j < len) 584 dst[j++] = 0x00; 585} 586 587int 588ata_max_pmode(struct ata_params *ap) 589{ 590 if (ap->atavalid & ATA_FLAG_64_70) { 591 if (ap->apiomodes & 0x02) 592 return ATA_PIO4; 593 if (ap->apiomodes & 0x01) 594 return ATA_PIO3; 595 } 596 if (ap->mwdmamodes & 0x04) 597 return ATA_PIO4; 598 if (ap->mwdmamodes & 0x02) 599 return ATA_PIO3; 600 if (ap->mwdmamodes & 0x01) 601 return ATA_PIO2; 602 if ((ap->retired_piomode & ATA_RETIRED_PIO_MASK) == 0x200) 603 return ATA_PIO2; 604 if ((ap->retired_piomode & ATA_RETIRED_PIO_MASK) == 0x100) 605 return ATA_PIO1; 606 if ((ap->retired_piomode & ATA_RETIRED_PIO_MASK) == 0x000) 607 return ATA_PIO0; 608 return ATA_PIO0; 609} 610 611int 612ata_max_wmode(struct ata_params *ap) 613{ 614 if (ap->mwdmamodes & 0x04) 615 return ATA_WDMA2; 616 if (ap->mwdmamodes & 0x02) 617 return ATA_WDMA1; 618 if (ap->mwdmamodes & 0x01) 619 return ATA_WDMA0; 620 return -1; 621} 622 623int 624ata_max_umode(struct ata_params *ap) 625{ 626 if (ap->atavalid & ATA_FLAG_88) { 627 if (ap->udmamodes & 0x40) 628 return ATA_UDMA6; 629 if (ap->udmamodes & 0x20) 630 return ATA_UDMA5; 631 if (ap->udmamodes & 0x10) 632 return ATA_UDMA4; 633 if (ap->udmamodes & 0x08) 634 return ATA_UDMA3; 635 if (ap->udmamodes & 0x04) 636 return ATA_UDMA2; 637 if (ap->udmamodes & 0x02) 638 return ATA_UDMA1; 639 if (ap->udmamodes & 0x01) 640 return ATA_UDMA0; 641 } 642 return -1; 643} 644 645int 646ata_max_mode(struct ata_params *ap, int maxmode) 647{ 648 649 if (maxmode == 0) 650 maxmode = ATA_DMA_MAX; 651 if (maxmode >= ATA_UDMA0 && ata_max_umode(ap) > 0) 652 return (min(maxmode, ata_max_umode(ap))); 653 if (maxmode >= ATA_WDMA0 && ata_max_wmode(ap) > 0) 654 return (min(maxmode, ata_max_wmode(ap))); 655 return (min(maxmode, ata_max_pmode(ap))); 656} 657 658char * 659ata_mode2string(int mode) 660{ 661 switch (mode) { 662 case -1: return "UNSUPPORTED"; 663 case 0: return "NONE"; 664 case ATA_PIO0: return "PIO0"; 665 case ATA_PIO1: return "PIO1"; 666 case ATA_PIO2: return "PIO2"; 667 case ATA_PIO3: return "PIO3"; 668 case ATA_PIO4: return "PIO4"; 669 case ATA_WDMA0: return "WDMA0"; 670 case ATA_WDMA1: return "WDMA1"; 671 case ATA_WDMA2: return "WDMA2"; 672 case ATA_UDMA0: return "UDMA0"; 673 case ATA_UDMA1: return "UDMA1"; 674 case ATA_UDMA2: return "UDMA2"; 675 case ATA_UDMA3: return "UDMA3"; 676 case ATA_UDMA4: return "UDMA4"; 677 case ATA_UDMA5: return "UDMA5"; 678 case ATA_UDMA6: return "UDMA6"; 679 default: 680 if (mode & ATA_DMA_MASK) 681 return "BIOSDMA"; 682 else 683 return "BIOSPIO"; 684 } 685} 686 687int 688ata_string2mode(char *str) 689{ 690 if (!strcasecmp(str, "PIO0")) return (ATA_PIO0); 691 if (!strcasecmp(str, "PIO1")) return (ATA_PIO1); 692 if (!strcasecmp(str, "PIO2")) return (ATA_PIO2); 693 if (!strcasecmp(str, "PIO3")) return (ATA_PIO3); 694 if (!strcasecmp(str, "PIO4")) return (ATA_PIO4); 695 if (!strcasecmp(str, "WDMA0")) return (ATA_WDMA0); 696 if (!strcasecmp(str, "WDMA1")) return (ATA_WDMA1); 697 if (!strcasecmp(str, "WDMA2")) return (ATA_WDMA2); 698 if (!strcasecmp(str, "UDMA0")) return (ATA_UDMA0); 699 if (!strcasecmp(str, "UDMA16")) return (ATA_UDMA0); 700 if (!strcasecmp(str, "UDMA1")) return (ATA_UDMA1); 701 if (!strcasecmp(str, "UDMA25")) return (ATA_UDMA1); 702 if (!strcasecmp(str, "UDMA2")) return (ATA_UDMA2); 703 if (!strcasecmp(str, "UDMA33")) return (ATA_UDMA2); 704 if (!strcasecmp(str, "UDMA3")) return (ATA_UDMA3); 705 if (!strcasecmp(str, "UDMA44")) return (ATA_UDMA3); 706 if (!strcasecmp(str, "UDMA4")) return (ATA_UDMA4); 707 if (!strcasecmp(str, "UDMA66")) return (ATA_UDMA4); 708 if (!strcasecmp(str, "UDMA5")) return (ATA_UDMA5); 709 if (!strcasecmp(str, "UDMA100")) return (ATA_UDMA5); 710 if (!strcasecmp(str, "UDMA6")) return (ATA_UDMA6); 711 if (!strcasecmp(str, "UDMA133")) return (ATA_UDMA6); 712 return (-1); 713} 714 715 716u_int 717ata_mode2speed(int mode) 718{ 719 switch (mode) { 720 case ATA_PIO0: 721 default: 722 return (3300); 723 case ATA_PIO1: 724 return (5200); 725 case ATA_PIO2: 726 return (8300); 727 case ATA_PIO3: 728 return (11100); 729 case ATA_PIO4: 730 return (16700); 731 case ATA_WDMA0: 732 return (4200); 733 case ATA_WDMA1: 734 return (13300); 735 case ATA_WDMA2: 736 return (16700); 737 case ATA_UDMA0: 738 return (16700); 739 case ATA_UDMA1: 740 return (25000); 741 case ATA_UDMA2: 742 return (33300); 743 case ATA_UDMA3: 744 return (44400); 745 case ATA_UDMA4: 746 return (66700); 747 case ATA_UDMA5: 748 return (100000); 749 case ATA_UDMA6: 750 return (133000); 751 } 752} 753 754u_int 755ata_revision2speed(int revision) 756{ 757 switch (revision) { 758 case 1: 759 default: 760 return (150000); 761 case 2: 762 return (300000); 763 case 3: 764 return (600000); 765 } 766} 767 768int 769ata_speed2revision(u_int speed) 770{ 771 switch (speed) { 772 case 0: 773 return (0); 774 case 150000: 775 return (1); 776 case 300000: 777 return (2); 778 case 600000: 779 return (3); 780 default: 781 return (-1); 782 } 783} 784 785int 786ata_identify_match(caddr_t identbuffer, caddr_t table_entry) 787{ 788 struct scsi_inquiry_pattern *entry; 789 struct ata_params *ident; 790 791 entry = (struct scsi_inquiry_pattern *)table_entry; 792 ident = (struct ata_params *)identbuffer; 793 794 if ((cam_strmatch(ident->model, entry->product, 795 sizeof(ident->model)) == 0) 796 && (cam_strmatch(ident->revision, entry->revision, 797 sizeof(ident->revision)) == 0)) { 798 return (0); 799 } 800 return (-1); 801} 802 803int 804ata_static_identify_match(caddr_t identbuffer, caddr_t table_entry) 805{ 806 struct scsi_static_inquiry_pattern *entry; 807 struct ata_params *ident; 808 809 entry = (struct scsi_static_inquiry_pattern *)table_entry; 810 ident = (struct ata_params *)identbuffer; 811 812 if ((cam_strmatch(ident->model, entry->product, 813 sizeof(ident->model)) == 0) 814 && (cam_strmatch(ident->revision, entry->revision, 815 sizeof(ident->revision)) == 0)) { 816 return (0); 817 } 818 return (-1); 819} 820 821void 822semb_receive_diagnostic_results(struct ccb_ataio *ataio, 823 u_int32_t retries, void (*cbfcnp)(struct cam_periph *, union ccb*), 824 uint8_t tag_action, int pcv, uint8_t page_code, 825 uint8_t *data_ptr, uint16_t length, uint32_t timeout) 826{ 827 828 length = min(length, 1020); 829 length = (length + 3) & ~3; 830 cam_fill_ataio(ataio, 831 retries, 832 cbfcnp, 833 /*flags*/CAM_DIR_IN, 834 tag_action, 835 data_ptr, 836 length, 837 timeout); 838 ata_28bit_cmd(ataio, ATA_SEP_ATTN, 839 pcv ? page_code : 0, 0x02, length / 4); 840} 841 842void 843semb_send_diagnostic(struct ccb_ataio *ataio, 844 u_int32_t retries, void (*cbfcnp)(struct cam_periph *, union ccb *), 845 uint8_t tag_action, uint8_t *data_ptr, uint16_t length, uint32_t timeout) 846{ 847 848 length = min(length, 1020); 849 length = (length + 3) & ~3; 850 cam_fill_ataio(ataio, 851 retries, 852 cbfcnp, 853 /*flags*/length ? CAM_DIR_OUT : CAM_DIR_NONE, 854 tag_action, 855 data_ptr, 856 length, 857 timeout); 858 ata_28bit_cmd(ataio, ATA_SEP_ATTN, 859 length > 0 ? data_ptr[0] : 0, 0x82, length / 4); 860} 861 862void 863semb_read_buffer(struct ccb_ataio *ataio, 864 u_int32_t retries, void (*cbfcnp)(struct cam_periph *, union ccb*), 865 uint8_t tag_action, uint8_t page_code, 866 uint8_t *data_ptr, uint16_t length, uint32_t timeout) 867{ 868 869 length = min(length, 1020); 870 length = (length + 3) & ~3; 871 cam_fill_ataio(ataio, 872 retries, 873 cbfcnp, 874 /*flags*/CAM_DIR_IN, 875 tag_action, 876 data_ptr, 877 length, 878 timeout); 879 ata_28bit_cmd(ataio, ATA_SEP_ATTN, 880 page_code, 0x00, length / 4); 881} 882 883void 884semb_write_buffer(struct ccb_ataio *ataio, 885 u_int32_t retries, void (*cbfcnp)(struct cam_periph *, union ccb *), 886 uint8_t tag_action, uint8_t *data_ptr, uint16_t length, uint32_t timeout) 887{ 888 889 length = min(length, 1020); 890 length = (length + 3) & ~3; 891 cam_fill_ataio(ataio, 892 retries, 893 cbfcnp, 894 /*flags*/length ? CAM_DIR_OUT : CAM_DIR_NONE, 895 tag_action, 896 data_ptr, 897 length, 898 timeout); 899 ata_28bit_cmd(ataio, ATA_SEP_ATTN, 900 length > 0 ? data_ptr[0] : 0, 0x80, length / 4); 901} 902 903