1195534Sscottl/*- 2195534Sscottl * Copyright (c) 2009 Alexander Motin <mav@FreeBSD.org> 3195534Sscottl * All rights reserved. 4195534Sscottl * 5195534Sscottl * Redistribution and use in source and binary forms, with or without 6195534Sscottl * modification, are permitted provided that the following conditions 7195534Sscottl * are met: 8195534Sscottl * 1. Redistributions of source code must retain the above copyright 9195534Sscottl * notice, this list of conditions and the following disclaimer, 10195534Sscottl * without modification, immediately at the beginning of the file. 11195534Sscottl * 2. Redistributions in binary form must reproduce the above copyright 12195534Sscottl * notice, this list of conditions and the following disclaimer in the 13195534Sscottl * documentation and/or other materials provided with the distribution. 14195534Sscottl * 15195534Sscottl * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 16195534Sscottl * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 17195534Sscottl * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 18195534Sscottl * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 19195534Sscottl * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 20195534Sscottl * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 21195534Sscottl * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 22195534Sscottl * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 23195534Sscottl * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 24195534Sscottl * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 25195534Sscottl */ 26195534Sscottl 27195534Sscottl#include <sys/cdefs.h> 28195534Sscottl__FBSDID("$FreeBSD: releng/10.3/sys/cam/ata/ata_all.c 280898 2015-03-31 07:26:39Z mav $"); 29195534Sscottl 30195534Sscottl#include <sys/param.h> 31195534Sscottl 32195534Sscottl#ifdef _KERNEL 33195534Sscottl#include <opt_scsi.h> 34195534Sscottl 35195534Sscottl#include <sys/systm.h> 36195534Sscottl#include <sys/libkern.h> 37195534Sscottl#include <sys/kernel.h> 38195534Sscottl#include <sys/sysctl.h> 39195534Sscottl#else 40195534Sscottl#include <errno.h> 41195534Sscottl#include <stdio.h> 42195534Sscottl#include <stdlib.h> 43195534Sscottl#include <string.h> 44195534Sscottl#ifndef min 45195534Sscottl#define min(a,b) (((a)<(b))?(a):(b)) 46195534Sscottl#endif 47195534Sscottl#endif 48195534Sscottl 49195534Sscottl#include <cam/cam.h> 50195534Sscottl#include <cam/cam_ccb.h> 51195534Sscottl#include <cam/cam_queue.h> 52195534Sscottl#include <cam/cam_xpt.h> 53195534Sscottl#include <sys/ata.h> 54195534Sscottl#include <cam/ata/ata_all.h> 55195534Sscottl#include <sys/sbuf.h> 56195534Sscottl#include <sys/endian.h> 57195534Sscottl 58195534Sscottlint 59195534Sscottlata_version(int ver) 60195534Sscottl{ 61195534Sscottl int bit; 62195534Sscottl 63195534Sscottl if (ver == 0xffff) 64195534Sscottl return 0; 65195534Sscottl for (bit = 15; bit >= 0; bit--) 66195534Sscottl if (ver & (1<<bit)) 67195534Sscottl return bit; 68195534Sscottl return 0; 69195534Sscottl} 70195534Sscottl 71198849Smavchar * 72198849Smavata_op_string(struct ata_cmd *cmd) 73198849Smav{ 74198849Smav 75220616Smav if (cmd->control & 0x04) 76220616Smav return ("SOFT_RESET"); 77198849Smav switch (cmd->command) { 78198849Smav case 0x00: return ("NOP"); 79198849Smav case 0x03: return ("CFA_REQUEST_EXTENDED_ERROR"); 80201139Smav case 0x06: 81201139Smav switch (cmd->features) { 82201139Smav case 0x01: return ("DSM TRIM"); 83201139Smav } 84201139Smav return "DSM"; 85198849Smav case 0x08: return ("DEVICE_RESET"); 86198849Smav case 0x20: return ("READ"); 87198849Smav case 0x24: return ("READ48"); 88198849Smav case 0x25: return ("READ_DMA48"); 89198849Smav case 0x26: return ("READ_DMA_QUEUED48"); 90198849Smav case 0x27: return ("READ_NATIVE_MAX_ADDRESS48"); 91198849Smav case 0x29: return ("READ_MUL48"); 92198849Smav case 0x2a: return ("READ_STREAM_DMA48"); 93198849Smav case 0x2b: return ("READ_STREAM48"); 94198849Smav case 0x2f: return ("READ_LOG_EXT"); 95198849Smav case 0x30: return ("WRITE"); 96198849Smav case 0x34: return ("WRITE48"); 97198849Smav case 0x35: return ("WRITE_DMA48"); 98198849Smav case 0x36: return ("WRITE_DMA_QUEUED48"); 99198849Smav case 0x37: return ("SET_MAX_ADDRESS48"); 100198849Smav case 0x39: return ("WRITE_MUL48"); 101198849Smav case 0x3a: return ("WRITE_STREAM_DMA48"); 102198849Smav case 0x3b: return ("WRITE_STREAM48"); 103200008Smav case 0x3d: return ("WRITE_DMA_FUA48"); 104200008Smav case 0x3e: return ("WRITE_DMA_QUEUED_FUA48"); 105198849Smav case 0x3f: return ("WRITE_LOG_EXT"); 106198849Smav case 0x40: return ("READ_VERIFY"); 107198849Smav case 0x42: return ("READ_VERIFY48"); 108198849Smav case 0x51: return ("CONFIGURE_STREAM"); 109198849Smav case 0x60: return ("READ_FPDMA_QUEUED"); 110198849Smav case 0x61: return ("WRITE_FPDMA_QUEUED"); 111280754Smav case 0x63: return ("NCQ_NON_DATA"); 112280754Smav case 0x64: return ("SEND_FPDMA_QUEUED"); 113280754Smav case 0x65: return ("RECEIVE_FPDMA_QUEUED"); 114235897Smav case 0x67: 115235897Smav if (cmd->features == 0xec) 116235897Smav return ("SEP_ATTN IDENTIFY"); 117235897Smav switch (cmd->lba_low) { 118235897Smav case 0x00: return ("SEP_ATTN READ BUFFER"); 119235897Smav case 0x02: return ("SEP_ATTN RECEIVE DIAGNOSTIC RESULTS"); 120235897Smav case 0x80: return ("SEP_ATTN WRITE BUFFER"); 121235897Smav case 0x82: return ("SEP_ATTN SEND DIAGNOSTIC"); 122235897Smav } 123235897Smav return ("SEP_ATTN"); 124198849Smav case 0x70: return ("SEEK"); 125198849Smav case 0x87: return ("CFA_TRANSLATE_SECTOR"); 126198849Smav case 0x90: return ("EXECUTE_DEVICE_DIAGNOSTIC"); 127198849Smav case 0x92: return ("DOWNLOAD_MICROCODE"); 128198849Smav case 0xa0: return ("PACKET"); 129198849Smav case 0xa1: return ("ATAPI_IDENTIFY"); 130198849Smav case 0xa2: return ("SERVICE"); 131198849Smav case 0xb0: return ("SMART"); 132198849Smav case 0xb1: return ("DEVICE CONFIGURATION"); 133198849Smav case 0xc0: return ("CFA_ERASE"); 134198849Smav case 0xc4: return ("READ_MUL"); 135198849Smav case 0xc5: return ("WRITE_MUL"); 136198849Smav case 0xc6: return ("SET_MULTI"); 137198849Smav case 0xc7: return ("READ_DMA_QUEUED"); 138198849Smav case 0xc8: return ("READ_DMA"); 139198849Smav case 0xca: return ("WRITE_DMA"); 140198849Smav case 0xcc: return ("WRITE_DMA_QUEUED"); 141198849Smav case 0xcd: return ("CFA_WRITE_MULTIPLE_WITHOUT_ERASE"); 142200008Smav case 0xce: return ("WRITE_MUL_FUA48"); 143198849Smav case 0xd1: return ("CHECK_MEDIA_CARD_TYPE"); 144198849Smav case 0xda: return ("GET_MEDIA_STATUS"); 145198849Smav case 0xde: return ("MEDIA_LOCK"); 146198849Smav case 0xdf: return ("MEDIA_UNLOCK"); 147198849Smav case 0xe0: return ("STANDBY_IMMEDIATE"); 148198849Smav case 0xe1: return ("IDLE_IMMEDIATE"); 149198849Smav case 0xe2: return ("STANDBY"); 150198849Smav case 0xe3: return ("IDLE"); 151198849Smav case 0xe4: return ("READ_BUFFER/PM"); 152198849Smav case 0xe5: return ("CHECK_POWER_MODE"); 153198849Smav case 0xe6: return ("SLEEP"); 154198849Smav case 0xe7: return ("FLUSHCACHE"); 155198849Smav case 0xe8: return ("WRITE_PM"); 156198849Smav case 0xea: return ("FLUSHCACHE48"); 157198849Smav case 0xec: return ("ATA_IDENTIFY"); 158198849Smav case 0xed: return ("MEDIA_EJECT"); 159198849Smav case 0xef: 160198849Smav switch (cmd->features) { 161198849Smav case 0x03: return ("SETFEATURES SET TRANSFER MODE"); 162198849Smav case 0x02: return ("SETFEATURES ENABLE WCACHE"); 163198849Smav case 0x82: return ("SETFEATURES DISABLE WCACHE"); 164203421Smav case 0x06: return ("SETFEATURES ENABLE PUIS"); 165203421Smav case 0x86: return ("SETFEATURES DISABLE PUIS"); 166203421Smav case 0x07: return ("SETFEATURES SPIN-UP"); 167220616Smav case 0x10: return ("SETFEATURES ENABLE SATA FEATURE"); 168220616Smav case 0x90: return ("SETFEATURES DISABLE SATA FEATURE"); 169198849Smav case 0xaa: return ("SETFEATURES ENABLE RCACHE"); 170198849Smav case 0x55: return ("SETFEATURES DISABLE RCACHE"); 171198849Smav } 172198849Smav return "SETFEATURES"; 173198849Smav case 0xf1: return ("SECURITY_SET_PASSWORD"); 174198849Smav case 0xf2: return ("SECURITY_UNLOCK"); 175198849Smav case 0xf3: return ("SECURITY_ERASE_PREPARE"); 176198849Smav case 0xf4: return ("SECURITY_ERASE_UNIT"); 177238363Sbrueffer case 0xf5: return ("SECURITY_FREEZE_LOCK"); 178238363Sbrueffer case 0xf6: return ("SECURITY_DISABLE_PASSWORD"); 179198849Smav case 0xf8: return ("READ_NATIVE_MAX_ADDRESS"); 180198849Smav case 0xf9: return ("SET_MAX_ADDRESS"); 181198849Smav } 182198849Smav return "UNKNOWN"; 183198849Smav} 184198849Smav 185198849Smavchar * 186198849Smavata_cmd_string(struct ata_cmd *cmd, char *cmd_string, size_t len) 187198849Smav{ 188198849Smav 189198849Smav snprintf(cmd_string, len, "%02x %02x %02x %02x " 190198849Smav "%02x %02x %02x %02x %02x %02x %02x %02x", 191198849Smav cmd->command, cmd->features, 192198849Smav cmd->lba_low, cmd->lba_mid, cmd->lba_high, cmd->device, 193198849Smav cmd->lba_low_exp, cmd->lba_mid_exp, cmd->lba_high_exp, 194198849Smav cmd->features_exp, cmd->sector_count, cmd->sector_count_exp); 195198849Smav 196198849Smav return(cmd_string); 197198849Smav} 198198849Smav 199198849Smavchar * 200198849Smavata_res_string(struct ata_res *res, char *res_string, size_t len) 201198849Smav{ 202198849Smav 203198849Smav snprintf(res_string, len, "%02x %02x %02x %02x " 204198849Smav "%02x %02x %02x %02x %02x %02x %02x", 205198849Smav res->status, res->error, 206198849Smav res->lba_low, res->lba_mid, res->lba_high, res->device, 207198849Smav res->lba_low_exp, res->lba_mid_exp, res->lba_high_exp, 208198849Smav res->sector_count, res->sector_count_exp); 209198849Smav 210198849Smav return(res_string); 211198849Smav} 212198849Smav 213198849Smav/* 214198849Smav * ata_command_sbuf() returns 0 for success and -1 for failure. 215198849Smav */ 216198849Smavint 217198849Smavata_command_sbuf(struct ccb_ataio *ataio, struct sbuf *sb) 218198849Smav{ 219198849Smav char cmd_str[(12 * 3) + 1]; 220198849Smav 221203108Smav sbuf_printf(sb, "%s. ACB: %s", 222198849Smav ata_op_string(&ataio->cmd), 223198849Smav ata_cmd_string(&ataio->cmd, cmd_str, sizeof(cmd_str))); 224198849Smav 225198849Smav return(0); 226198849Smav} 227198849Smav 228198849Smav/* 229198849Smav * ata_status_abuf() returns 0 for success and -1 for failure. 230198849Smav */ 231198849Smavint 232198849Smavata_status_sbuf(struct ccb_ataio *ataio, struct sbuf *sb) 233198849Smav{ 234198849Smav 235203108Smav sbuf_printf(sb, "ATA status: %02x (%s%s%s%s%s%s%s%s)", 236198849Smav ataio->res.status, 237198849Smav (ataio->res.status & 0x80) ? "BSY " : "", 238198849Smav (ataio->res.status & 0x40) ? "DRDY " : "", 239198849Smav (ataio->res.status & 0x20) ? "DF " : "", 240198849Smav (ataio->res.status & 0x10) ? "SERV " : "", 241198849Smav (ataio->res.status & 0x08) ? "DRQ " : "", 242198849Smav (ataio->res.status & 0x04) ? "CORR " : "", 243198849Smav (ataio->res.status & 0x02) ? "IDX " : "", 244198849Smav (ataio->res.status & 0x01) ? "ERR" : ""); 245198849Smav if (ataio->res.status & 1) { 246203108Smav sbuf_printf(sb, ", error: %02x (%s%s%s%s%s%s%s%s)", 247198849Smav ataio->res.error, 248198849Smav (ataio->res.error & 0x80) ? "ICRC " : "", 249198849Smav (ataio->res.error & 0x40) ? "UNC " : "", 250198849Smav (ataio->res.error & 0x20) ? "MC " : "", 251198849Smav (ataio->res.error & 0x10) ? "IDNF " : "", 252198849Smav (ataio->res.error & 0x08) ? "MCR " : "", 253198849Smav (ataio->res.error & 0x04) ? "ABRT " : "", 254198849Smav (ataio->res.error & 0x02) ? "NM " : "", 255198849Smav (ataio->res.error & 0x01) ? "ILI" : ""); 256198849Smav } 257198849Smav 258198849Smav return(0); 259198849Smav} 260198849Smav 261198849Smav/* 262198849Smav * ata_res_sbuf() returns 0 for success and -1 for failure. 263198849Smav */ 264198849Smavint 265198849Smavata_res_sbuf(struct ccb_ataio *ataio, struct sbuf *sb) 266198849Smav{ 267198849Smav char res_str[(11 * 3) + 1]; 268198849Smav 269198849Smav sbuf_printf(sb, "RES: %s", 270198849Smav ata_res_string(&ataio->res, res_str, sizeof(res_str))); 271198849Smav 272198849Smav return(0); 273198849Smav} 274198849Smav 275195534Sscottlvoid 276195534Sscottlata_print_ident(struct ata_params *ident_data) 277195534Sscottl{ 278280898Smav const char *proto; 279280898Smav char product[48], revision[16], ata[12], sata[12]; 280195534Sscottl 281195534Sscottl cam_strvis(product, ident_data->model, sizeof(ident_data->model), 282195534Sscottl sizeof(product)); 283195534Sscottl cam_strvis(revision, ident_data->revision, sizeof(ident_data->revision), 284195534Sscottl sizeof(revision)); 285280898Smav proto = (ident_data->config == ATA_PROTO_CFA) ? "CFA" : 286280898Smav (ident_data->config & ATA_PROTO_ATAPI) ? "ATAPI" : "ATA"; 287280898Smav if (ata_version(ident_data->version_major) == 0) { 288280898Smav snprintf(ata, sizeof(ata), "%s", proto); 289280898Smav } else if (ata_version(ident_data->version_major) <= 7) { 290280898Smav snprintf(ata, sizeof(ata), "%s-%d", proto, 291280898Smav ata_version(ident_data->version_major)); 292280898Smav } else if (ata_version(ident_data->version_major) == 8) { 293280898Smav snprintf(ata, sizeof(ata), "%s8-ACS", proto); 294280898Smav } else { 295280898Smav snprintf(ata, sizeof(ata), "ACS-%d %s", 296280898Smav ata_version(ident_data->version_major) - 7, proto); 297280898Smav } 298195534Sscottl if (ident_data->satacapabilities && ident_data->satacapabilities != 0xffff) { 299197541Smav if (ident_data->satacapabilities & ATA_SATA_GEN3) 300280898Smav snprintf(sata, sizeof(sata), " SATA 3.x"); 301197541Smav else if (ident_data->satacapabilities & ATA_SATA_GEN2) 302280898Smav snprintf(sata, sizeof(sata), " SATA 2.x"); 303195534Sscottl else if (ident_data->satacapabilities & ATA_SATA_GEN1) 304280898Smav snprintf(sata, sizeof(sata), " SATA 1.x"); 305195534Sscottl else 306280898Smav snprintf(sata, sizeof(sata), " SATA"); 307280898Smav } else 308280898Smav sata[0] = 0; 309280898Smav printf("<%s %s> %s%s device\n", product, revision, ata, sata); 310195534Sscottl} 311195534Sscottl 312235897Smavvoid 313257049Smavata_print_ident_short(struct ata_params *ident_data) 314257049Smav{ 315257049Smav char product[48], revision[16]; 316257049Smav 317257049Smav cam_strvis(product, ident_data->model, sizeof(ident_data->model), 318257049Smav sizeof(product)); 319257049Smav cam_strvis(revision, ident_data->revision, sizeof(ident_data->revision), 320257049Smav sizeof(revision)); 321257049Smav printf("<%s %s>", product, revision); 322257049Smav} 323257049Smav 324257049Smavvoid 325235897Smavsemb_print_ident(struct sep_identify_data *ident_data) 326235897Smav{ 327235897Smav char vendor[9], product[17], revision[5], fw[5], in[7], ins[5]; 328235897Smav 329235897Smav cam_strvis(vendor, ident_data->vendor_id, 8, sizeof(vendor)); 330235897Smav cam_strvis(product, ident_data->product_id, 16, sizeof(product)); 331235897Smav cam_strvis(revision, ident_data->product_rev, 4, sizeof(revision)); 332235897Smav cam_strvis(fw, ident_data->firmware_rev, 4, sizeof(fw)); 333235897Smav cam_strvis(in, ident_data->interface_id, 6, sizeof(in)); 334235897Smav cam_strvis(ins, ident_data->interface_rev, 4, sizeof(ins)); 335235897Smav printf("<%s %s %s %s> SEMB %s %s device\n", 336235897Smav vendor, product, revision, fw, in, ins); 337235897Smav} 338235897Smav 339257049Smavvoid 340257049Smavsemb_print_ident_short(struct sep_identify_data *ident_data) 341257049Smav{ 342257049Smav char vendor[9], product[17], revision[5], fw[5]; 343257049Smav 344257049Smav cam_strvis(vendor, ident_data->vendor_id, 8, sizeof(vendor)); 345257049Smav cam_strvis(product, ident_data->product_id, 16, sizeof(product)); 346257049Smav cam_strvis(revision, ident_data->product_rev, 4, sizeof(revision)); 347257049Smav cam_strvis(fw, ident_data->firmware_rev, 4, sizeof(fw)); 348257049Smav printf("<%s %s %s %s>", vendor, product, revision, fw); 349257049Smav} 350257049Smav 351198897Smavuint32_t 352198897Smavata_logical_sector_size(struct ata_params *ident_data) 353198897Smav{ 354263156Smav if ((ident_data->pss & ATA_PSS_VALID_MASK) == ATA_PSS_VALID_VALUE && 355198897Smav (ident_data->pss & ATA_PSS_LSSABOVE512)) { 356263156Smav return (((u_int32_t)ident_data->lss_1 | 357263156Smav ((u_int32_t)ident_data->lss_2 << 16)) * 2); 358198897Smav } 359198897Smav return (512); 360198897Smav} 361198897Smav 362198897Smavuint64_t 363198897Smavata_physical_sector_size(struct ata_params *ident_data) 364198897Smav{ 365263156Smav if ((ident_data->pss & ATA_PSS_VALID_MASK) == ATA_PSS_VALID_VALUE) { 366263156Smav if (ident_data->pss & ATA_PSS_MULTLS) { 367263156Smav return ((uint64_t)ata_logical_sector_size(ident_data) * 368263156Smav (1 << (ident_data->pss & ATA_PSS_LSPPS))); 369263156Smav } else { 370263156Smav return (uint64_t)ata_logical_sector_size(ident_data); 371263156Smav } 372198897Smav } 373198897Smav return (512); 374198897Smav} 375198897Smav 376198897Smavuint64_t 377198897Smavata_logical_sector_offset(struct ata_params *ident_data) 378198897Smav{ 379198897Smav if ((ident_data->lsalign & 0xc000) == 0x4000) { 380198897Smav return ((uint64_t)ata_logical_sector_size(ident_data) * 381198897Smav (ident_data->lsalign & 0x3fff)); 382198897Smav } 383198897Smav return (0); 384198897Smav} 385198897Smav 386195534Sscottlvoid 387196659Smavata_28bit_cmd(struct ccb_ataio *ataio, uint8_t cmd, uint8_t features, 388195534Sscottl uint32_t lba, uint8_t sector_count) 389195534Sscottl{ 390195534Sscottl bzero(&ataio->cmd, sizeof(ataio->cmd)); 391195534Sscottl ataio->cmd.flags = 0; 392200008Smav if (cmd == ATA_READ_DMA || 393200008Smav cmd == ATA_READ_DMA_QUEUED || 394200008Smav cmd == ATA_WRITE_DMA || 395200008Smav cmd == ATA_WRITE_DMA_QUEUED) 396200008Smav ataio->cmd.flags |= CAM_ATAIO_DMA; 397195534Sscottl ataio->cmd.command = cmd; 398195534Sscottl ataio->cmd.features = features; 399195534Sscottl ataio->cmd.lba_low = lba; 400195534Sscottl ataio->cmd.lba_mid = lba >> 8; 401195534Sscottl ataio->cmd.lba_high = lba >> 16; 402238393Sbrueffer ataio->cmd.device = ATA_DEV_LBA | ((lba >> 24) & 0x0f); 403195534Sscottl ataio->cmd.sector_count = sector_count; 404195534Sscottl} 405195534Sscottl 406195534Sscottlvoid 407195534Sscottlata_48bit_cmd(struct ccb_ataio *ataio, uint8_t cmd, uint16_t features, 408195534Sscottl uint64_t lba, uint16_t sector_count) 409195534Sscottl{ 410248695Smav 411195534Sscottl ataio->cmd.flags = CAM_ATAIO_48BIT; 412200008Smav if (cmd == ATA_READ_DMA48 || 413200008Smav cmd == ATA_READ_DMA_QUEUED48 || 414200008Smav cmd == ATA_READ_STREAM_DMA48 || 415200008Smav cmd == ATA_WRITE_DMA48 || 416200008Smav cmd == ATA_WRITE_DMA_FUA48 || 417200008Smav cmd == ATA_WRITE_DMA_QUEUED48 || 418200008Smav cmd == ATA_WRITE_DMA_QUEUED_FUA48 || 419201139Smav cmd == ATA_WRITE_STREAM_DMA48 || 420201139Smav cmd == ATA_DATA_SET_MANAGEMENT) 421200008Smav ataio->cmd.flags |= CAM_ATAIO_DMA; 422195534Sscottl ataio->cmd.command = cmd; 423195534Sscottl ataio->cmd.features = features; 424195534Sscottl ataio->cmd.lba_low = lba; 425195534Sscottl ataio->cmd.lba_mid = lba >> 8; 426195534Sscottl ataio->cmd.lba_high = lba >> 16; 427238393Sbrueffer ataio->cmd.device = ATA_DEV_LBA; 428195534Sscottl ataio->cmd.lba_low_exp = lba >> 24; 429195534Sscottl ataio->cmd.lba_mid_exp = lba >> 32; 430195534Sscottl ataio->cmd.lba_high_exp = lba >> 40; 431195534Sscottl ataio->cmd.features_exp = features >> 8; 432195534Sscottl ataio->cmd.sector_count = sector_count; 433195534Sscottl ataio->cmd.sector_count_exp = sector_count >> 8; 434248695Smav ataio->cmd.control = 0; 435195534Sscottl} 436195534Sscottl 437195534Sscottlvoid 438195534Sscottlata_ncq_cmd(struct ccb_ataio *ataio, uint8_t cmd, 439195534Sscottl uint64_t lba, uint16_t sector_count) 440195534Sscottl{ 441248695Smav 442195534Sscottl ataio->cmd.flags = CAM_ATAIO_48BIT | CAM_ATAIO_FPDMA; 443195534Sscottl ataio->cmd.command = cmd; 444195534Sscottl ataio->cmd.features = sector_count; 445195534Sscottl ataio->cmd.lba_low = lba; 446195534Sscottl ataio->cmd.lba_mid = lba >> 8; 447195534Sscottl ataio->cmd.lba_high = lba >> 16; 448238393Sbrueffer ataio->cmd.device = ATA_DEV_LBA; 449195534Sscottl ataio->cmd.lba_low_exp = lba >> 24; 450195534Sscottl ataio->cmd.lba_mid_exp = lba >> 32; 451195534Sscottl ataio->cmd.lba_high_exp = lba >> 40; 452195534Sscottl ataio->cmd.features_exp = sector_count >> 8; 453248695Smav ataio->cmd.sector_count = 0; 454248695Smav ataio->cmd.sector_count_exp = 0; 455248695Smav ataio->cmd.control = 0; 456195534Sscottl} 457195534Sscottl 458195534Sscottlvoid 459195534Sscottlata_reset_cmd(struct ccb_ataio *ataio) 460195534Sscottl{ 461195534Sscottl bzero(&ataio->cmd, sizeof(ataio->cmd)); 462195534Sscottl ataio->cmd.flags = CAM_ATAIO_CONTROL | CAM_ATAIO_NEEDRESULT; 463195534Sscottl ataio->cmd.control = 0x04; 464195534Sscottl} 465195534Sscottl 466195534Sscottlvoid 467195534Sscottlata_pm_read_cmd(struct ccb_ataio *ataio, int reg, int port) 468195534Sscottl{ 469195534Sscottl bzero(&ataio->cmd, sizeof(ataio->cmd)); 470198904Smav ataio->cmd.flags = CAM_ATAIO_NEEDRESULT; 471195534Sscottl ataio->cmd.command = ATA_READ_PM; 472195534Sscottl ataio->cmd.features = reg; 473195534Sscottl ataio->cmd.device = port & 0x0f; 474195534Sscottl} 475195534Sscottl 476195534Sscottlvoid 477198904Smavata_pm_write_cmd(struct ccb_ataio *ataio, int reg, int port, uint32_t val) 478195534Sscottl{ 479195534Sscottl bzero(&ataio->cmd, sizeof(ataio->cmd)); 480198904Smav ataio->cmd.flags = 0; 481195534Sscottl ataio->cmd.command = ATA_WRITE_PM; 482195534Sscottl ataio->cmd.features = reg; 483198904Smav ataio->cmd.sector_count = val; 484195534Sscottl ataio->cmd.lba_low = val >> 8; 485195534Sscottl ataio->cmd.lba_mid = val >> 16; 486195534Sscottl ataio->cmd.lba_high = val >> 24; 487195534Sscottl ataio->cmd.device = port & 0x0f; 488195534Sscottl} 489195534Sscottl 490195534Sscottlvoid 491195534Sscottlata_bswap(int8_t *buf, int len) 492195534Sscottl{ 493195534Sscottl u_int16_t *ptr = (u_int16_t*)(buf + len); 494195534Sscottl 495195534Sscottl while (--ptr >= (u_int16_t*)buf) 496195534Sscottl *ptr = be16toh(*ptr); 497195534Sscottl} 498195534Sscottl 499195534Sscottlvoid 500195534Sscottlata_btrim(int8_t *buf, int len) 501195534Sscottl{ 502195534Sscottl int8_t *ptr; 503195534Sscottl 504195534Sscottl for (ptr = buf; ptr < buf+len; ++ptr) 505195534Sscottl if (!*ptr || *ptr == '_') 506195534Sscottl *ptr = ' '; 507195534Sscottl for (ptr = buf + len - 1; ptr >= buf && *ptr == ' '; --ptr) 508195534Sscottl *ptr = 0; 509195534Sscottl} 510195534Sscottl 511195534Sscottlvoid 512195534Sscottlata_bpack(int8_t *src, int8_t *dst, int len) 513195534Sscottl{ 514195534Sscottl int i, j, blank; 515195534Sscottl 516195534Sscottl for (i = j = blank = 0 ; i < len; i++) { 517195534Sscottl if (blank && src[i] == ' ') continue; 518195534Sscottl if (blank && src[i] != ' ') { 519195534Sscottl dst[j++] = src[i]; 520195534Sscottl blank = 0; 521195534Sscottl continue; 522195534Sscottl } 523195534Sscottl if (src[i] == ' ') { 524195534Sscottl blank = 1; 525195534Sscottl if (i == 0) 526195534Sscottl continue; 527195534Sscottl } 528195534Sscottl dst[j++] = src[i]; 529195534Sscottl } 530195534Sscottl while (j < len) 531195534Sscottl dst[j++] = 0x00; 532195534Sscottl} 533195534Sscottl 534195534Sscottlint 535195534Sscottlata_max_pmode(struct ata_params *ap) 536195534Sscottl{ 537195534Sscottl if (ap->atavalid & ATA_FLAG_64_70) { 538195534Sscottl if (ap->apiomodes & 0x02) 539195534Sscottl return ATA_PIO4; 540195534Sscottl if (ap->apiomodes & 0x01) 541195534Sscottl return ATA_PIO3; 542195534Sscottl } 543195534Sscottl if (ap->mwdmamodes & 0x04) 544195534Sscottl return ATA_PIO4; 545195534Sscottl if (ap->mwdmamodes & 0x02) 546195534Sscottl return ATA_PIO3; 547195534Sscottl if (ap->mwdmamodes & 0x01) 548195534Sscottl return ATA_PIO2; 549195534Sscottl if ((ap->retired_piomode & ATA_RETIRED_PIO_MASK) == 0x200) 550195534Sscottl return ATA_PIO2; 551195534Sscottl if ((ap->retired_piomode & ATA_RETIRED_PIO_MASK) == 0x100) 552195534Sscottl return ATA_PIO1; 553195534Sscottl if ((ap->retired_piomode & ATA_RETIRED_PIO_MASK) == 0x000) 554195534Sscottl return ATA_PIO0; 555195534Sscottl return ATA_PIO0; 556195534Sscottl} 557195534Sscottl 558195534Sscottlint 559195534Sscottlata_max_wmode(struct ata_params *ap) 560195534Sscottl{ 561195534Sscottl if (ap->mwdmamodes & 0x04) 562195534Sscottl return ATA_WDMA2; 563195534Sscottl if (ap->mwdmamodes & 0x02) 564195534Sscottl return ATA_WDMA1; 565195534Sscottl if (ap->mwdmamodes & 0x01) 566195534Sscottl return ATA_WDMA0; 567195534Sscottl return -1; 568195534Sscottl} 569195534Sscottl 570195534Sscottlint 571195534Sscottlata_max_umode(struct ata_params *ap) 572195534Sscottl{ 573195534Sscottl if (ap->atavalid & ATA_FLAG_88) { 574195534Sscottl if (ap->udmamodes & 0x40) 575195534Sscottl return ATA_UDMA6; 576195534Sscottl if (ap->udmamodes & 0x20) 577195534Sscottl return ATA_UDMA5; 578195534Sscottl if (ap->udmamodes & 0x10) 579195534Sscottl return ATA_UDMA4; 580195534Sscottl if (ap->udmamodes & 0x08) 581195534Sscottl return ATA_UDMA3; 582195534Sscottl if (ap->udmamodes & 0x04) 583195534Sscottl return ATA_UDMA2; 584195534Sscottl if (ap->udmamodes & 0x02) 585195534Sscottl return ATA_UDMA1; 586195534Sscottl if (ap->udmamodes & 0x01) 587195534Sscottl return ATA_UDMA0; 588195534Sscottl } 589195534Sscottl return -1; 590195534Sscottl} 591195534Sscottl 592195534Sscottlint 593199747Smavata_max_mode(struct ata_params *ap, int maxmode) 594195534Sscottl{ 595195534Sscottl 596199747Smav if (maxmode == 0) 597199747Smav maxmode = ATA_DMA_MAX; 598199747Smav if (maxmode >= ATA_UDMA0 && ata_max_umode(ap) > 0) 599199747Smav return (min(maxmode, ata_max_umode(ap))); 600199747Smav if (maxmode >= ATA_WDMA0 && ata_max_wmode(ap) > 0) 601199747Smav return (min(maxmode, ata_max_wmode(ap))); 602199747Smav return (min(maxmode, ata_max_pmode(ap))); 603199747Smav} 604195534Sscottl 605199747Smavchar * 606199747Smavata_mode2string(int mode) 607199747Smav{ 608199747Smav switch (mode) { 609199747Smav case -1: return "UNSUPPORTED"; 610199747Smav case 0: return "NONE"; 611199747Smav case ATA_PIO0: return "PIO0"; 612199747Smav case ATA_PIO1: return "PIO1"; 613199747Smav case ATA_PIO2: return "PIO2"; 614199747Smav case ATA_PIO3: return "PIO3"; 615199747Smav case ATA_PIO4: return "PIO4"; 616199747Smav case ATA_WDMA0: return "WDMA0"; 617199747Smav case ATA_WDMA1: return "WDMA1"; 618199747Smav case ATA_WDMA2: return "WDMA2"; 619199747Smav case ATA_UDMA0: return "UDMA0"; 620199747Smav case ATA_UDMA1: return "UDMA1"; 621199747Smav case ATA_UDMA2: return "UDMA2"; 622199747Smav case ATA_UDMA3: return "UDMA3"; 623199747Smav case ATA_UDMA4: return "UDMA4"; 624199747Smav case ATA_UDMA5: return "UDMA5"; 625199747Smav case ATA_UDMA6: return "UDMA6"; 626199747Smav default: 627199747Smav if (mode & ATA_DMA_MASK) 628199747Smav return "BIOSDMA"; 629199747Smav else 630199747Smav return "BIOSPIO"; 631199747Smav } 632199747Smav} 633195534Sscottl 634199821Smavint 635199821Smavata_string2mode(char *str) 636199821Smav{ 637199821Smav if (!strcasecmp(str, "PIO0")) return (ATA_PIO0); 638199821Smav if (!strcasecmp(str, "PIO1")) return (ATA_PIO1); 639199821Smav if (!strcasecmp(str, "PIO2")) return (ATA_PIO2); 640199821Smav if (!strcasecmp(str, "PIO3")) return (ATA_PIO3); 641199821Smav if (!strcasecmp(str, "PIO4")) return (ATA_PIO4); 642199821Smav if (!strcasecmp(str, "WDMA0")) return (ATA_WDMA0); 643199821Smav if (!strcasecmp(str, "WDMA1")) return (ATA_WDMA1); 644199821Smav if (!strcasecmp(str, "WDMA2")) return (ATA_WDMA2); 645199821Smav if (!strcasecmp(str, "UDMA0")) return (ATA_UDMA0); 646199821Smav if (!strcasecmp(str, "UDMA16")) return (ATA_UDMA0); 647199821Smav if (!strcasecmp(str, "UDMA1")) return (ATA_UDMA1); 648199821Smav if (!strcasecmp(str, "UDMA25")) return (ATA_UDMA1); 649199821Smav if (!strcasecmp(str, "UDMA2")) return (ATA_UDMA2); 650199821Smav if (!strcasecmp(str, "UDMA33")) return (ATA_UDMA2); 651199821Smav if (!strcasecmp(str, "UDMA3")) return (ATA_UDMA3); 652199821Smav if (!strcasecmp(str, "UDMA44")) return (ATA_UDMA3); 653199821Smav if (!strcasecmp(str, "UDMA4")) return (ATA_UDMA4); 654199821Smav if (!strcasecmp(str, "UDMA66")) return (ATA_UDMA4); 655199821Smav if (!strcasecmp(str, "UDMA5")) return (ATA_UDMA5); 656199821Smav if (!strcasecmp(str, "UDMA100")) return (ATA_UDMA5); 657199821Smav if (!strcasecmp(str, "UDMA6")) return (ATA_UDMA6); 658199821Smav if (!strcasecmp(str, "UDMA133")) return (ATA_UDMA6); 659199821Smav return (-1); 660199821Smav} 661199821Smav 662199821Smav 663199747Smavu_int 664199747Smavata_mode2speed(int mode) 665199747Smav{ 666199747Smav switch (mode) { 667199747Smav case ATA_PIO0: 668199747Smav default: 669199747Smav return (3300); 670199747Smav case ATA_PIO1: 671199747Smav return (5200); 672199747Smav case ATA_PIO2: 673199747Smav return (8300); 674199747Smav case ATA_PIO3: 675199747Smav return (11100); 676199747Smav case ATA_PIO4: 677199747Smav return (16700); 678199747Smav case ATA_WDMA0: 679199747Smav return (4200); 680199747Smav case ATA_WDMA1: 681199747Smav return (13300); 682199747Smav case ATA_WDMA2: 683199747Smav return (16700); 684199747Smav case ATA_UDMA0: 685199747Smav return (16700); 686199747Smav case ATA_UDMA1: 687199747Smav return (25000); 688199747Smav case ATA_UDMA2: 689199747Smav return (33300); 690199747Smav case ATA_UDMA3: 691199747Smav return (44400); 692199747Smav case ATA_UDMA4: 693199747Smav return (66700); 694199747Smav case ATA_UDMA5: 695199747Smav return (100000); 696199747Smav case ATA_UDMA6: 697199747Smav return (133000); 698199747Smav } 699199747Smav} 700195534Sscottl 701199747Smavu_int 702199747Smavata_revision2speed(int revision) 703199747Smav{ 704199747Smav switch (revision) { 705199747Smav case 1: 706199747Smav default: 707199747Smav return (150000); 708199747Smav case 2: 709199747Smav return (300000); 710199747Smav case 3: 711199747Smav return (600000); 712199747Smav } 713199747Smav} 714195534Sscottl 715199747Smavint 716199747Smavata_speed2revision(u_int speed) 717199747Smav{ 718199747Smav switch (speed) { 719199821Smav case 0: 720199821Smav return (0); 721199747Smav case 150000: 722199747Smav return (1); 723199747Smav case 300000: 724199747Smav return (2); 725199747Smav case 600000: 726199747Smav return (3); 727199821Smav default: 728199821Smav return (-1); 729199747Smav } 730195534Sscottl} 731195534Sscottl 732199178Smavint 733199178Smavata_identify_match(caddr_t identbuffer, caddr_t table_entry) 734199178Smav{ 735199178Smav struct scsi_inquiry_pattern *entry; 736199178Smav struct ata_params *ident; 737199178Smav 738199178Smav entry = (struct scsi_inquiry_pattern *)table_entry; 739199178Smav ident = (struct ata_params *)identbuffer; 740199178Smav 741199178Smav if ((cam_strmatch(ident->model, entry->product, 742199178Smav sizeof(ident->model)) == 0) 743199178Smav && (cam_strmatch(ident->revision, entry->revision, 744199178Smav sizeof(ident->revision)) == 0)) { 745199178Smav return (0); 746199178Smav } 747199178Smav return (-1); 748199178Smav} 749199178Smav 750199178Smavint 751199178Smavata_static_identify_match(caddr_t identbuffer, caddr_t table_entry) 752199178Smav{ 753199178Smav struct scsi_static_inquiry_pattern *entry; 754199178Smav struct ata_params *ident; 755199178Smav 756199178Smav entry = (struct scsi_static_inquiry_pattern *)table_entry; 757199178Smav ident = (struct ata_params *)identbuffer; 758199178Smav 759199178Smav if ((cam_strmatch(ident->model, entry->product, 760199178Smav sizeof(ident->model)) == 0) 761199178Smav && (cam_strmatch(ident->revision, entry->revision, 762199178Smav sizeof(ident->revision)) == 0)) { 763199178Smav return (0); 764199178Smav } 765199178Smav return (-1); 766199178Smav} 767235897Smav 768235897Smavvoid 769235897Smavsemb_receive_diagnostic_results(struct ccb_ataio *ataio, 770235897Smav u_int32_t retries, void (*cbfcnp)(struct cam_periph *, union ccb*), 771235897Smav uint8_t tag_action, int pcv, uint8_t page_code, 772235897Smav uint8_t *data_ptr, uint16_t length, uint32_t timeout) 773235897Smav{ 774235897Smav 775235897Smav length = min(length, 1020); 776235897Smav length = (length + 3) & ~3; 777235897Smav cam_fill_ataio(ataio, 778235897Smav retries, 779235897Smav cbfcnp, 780235897Smav /*flags*/CAM_DIR_IN, 781235897Smav tag_action, 782235897Smav data_ptr, 783235897Smav length, 784235897Smav timeout); 785235897Smav ata_28bit_cmd(ataio, ATA_SEP_ATTN, 786235897Smav pcv ? page_code : 0, 0x02, length / 4); 787235897Smav} 788235897Smav 789235897Smavvoid 790235897Smavsemb_send_diagnostic(struct ccb_ataio *ataio, 791235897Smav u_int32_t retries, void (*cbfcnp)(struct cam_periph *, union ccb *), 792235897Smav uint8_t tag_action, uint8_t *data_ptr, uint16_t length, uint32_t timeout) 793235897Smav{ 794235897Smav 795235897Smav length = min(length, 1020); 796235897Smav length = (length + 3) & ~3; 797235897Smav cam_fill_ataio(ataio, 798235897Smav retries, 799235897Smav cbfcnp, 800235897Smav /*flags*/length ? CAM_DIR_OUT : CAM_DIR_NONE, 801235897Smav tag_action, 802235897Smav data_ptr, 803235897Smav length, 804235897Smav timeout); 805235897Smav ata_28bit_cmd(ataio, ATA_SEP_ATTN, 806235897Smav length > 0 ? data_ptr[0] : 0, 0x82, length / 4); 807235897Smav} 808235897Smav 809235897Smavvoid 810235897Smavsemb_read_buffer(struct ccb_ataio *ataio, 811235897Smav u_int32_t retries, void (*cbfcnp)(struct cam_periph *, union ccb*), 812235897Smav uint8_t tag_action, uint8_t page_code, 813235897Smav uint8_t *data_ptr, uint16_t length, uint32_t timeout) 814235897Smav{ 815235897Smav 816235897Smav length = min(length, 1020); 817235897Smav length = (length + 3) & ~3; 818235897Smav cam_fill_ataio(ataio, 819235897Smav retries, 820235897Smav cbfcnp, 821235897Smav /*flags*/CAM_DIR_IN, 822235897Smav tag_action, 823235897Smav data_ptr, 824235897Smav length, 825235897Smav timeout); 826235897Smav ata_28bit_cmd(ataio, ATA_SEP_ATTN, 827235897Smav page_code, 0x00, length / 4); 828235897Smav} 829235897Smav 830235897Smavvoid 831235897Smavsemb_write_buffer(struct ccb_ataio *ataio, 832235897Smav u_int32_t retries, void (*cbfcnp)(struct cam_periph *, union ccb *), 833235897Smav uint8_t tag_action, uint8_t *data_ptr, uint16_t length, uint32_t timeout) 834235897Smav{ 835235897Smav 836235897Smav length = min(length, 1020); 837235897Smav length = (length + 3) & ~3; 838235897Smav cam_fill_ataio(ataio, 839235897Smav retries, 840235897Smav cbfcnp, 841235897Smav /*flags*/length ? CAM_DIR_OUT : CAM_DIR_NONE, 842235897Smav tag_action, 843235897Smav data_ptr, 844235897Smav length, 845235897Smav timeout); 846235897Smav ata_28bit_cmd(ataio, ATA_SEP_ATTN, 847235897Smav length > 0 ? data_ptr[0] : 0, 0x80, length / 4); 848235897Smav} 849235897Smav 850