ata_all.c revision 223019
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: head/sys/cam/ata/ata_all.c 223019 2011-06-12 18:52: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"); 111198849Smav case 0x70: return ("SEEK"); 112198849Smav case 0x87: return ("CFA_TRANSLATE_SECTOR"); 113198849Smav case 0x90: return ("EXECUTE_DEVICE_DIAGNOSTIC"); 114198849Smav case 0x92: return ("DOWNLOAD_MICROCODE"); 115198849Smav case 0xa0: return ("PACKET"); 116198849Smav case 0xa1: return ("ATAPI_IDENTIFY"); 117198849Smav case 0xa2: return ("SERVICE"); 118198849Smav case 0xb0: return ("SMART"); 119198849Smav case 0xb1: return ("DEVICE CONFIGURATION"); 120198849Smav case 0xc0: return ("CFA_ERASE"); 121198849Smav case 0xc4: return ("READ_MUL"); 122198849Smav case 0xc5: return ("WRITE_MUL"); 123198849Smav case 0xc6: return ("SET_MULTI"); 124198849Smav case 0xc7: return ("READ_DMA_QUEUED"); 125198849Smav case 0xc8: return ("READ_DMA"); 126198849Smav case 0xca: return ("WRITE_DMA"); 127198849Smav case 0xcc: return ("WRITE_DMA_QUEUED"); 128198849Smav case 0xcd: return ("CFA_WRITE_MULTIPLE_WITHOUT_ERASE"); 129200008Smav case 0xce: return ("WRITE_MUL_FUA48"); 130198849Smav case 0xd1: return ("CHECK_MEDIA_CARD_TYPE"); 131198849Smav case 0xda: return ("GET_MEDIA_STATUS"); 132198849Smav case 0xde: return ("MEDIA_LOCK"); 133198849Smav case 0xdf: return ("MEDIA_UNLOCK"); 134198849Smav case 0xe0: return ("STANDBY_IMMEDIATE"); 135198849Smav case 0xe1: return ("IDLE_IMMEDIATE"); 136198849Smav case 0xe2: return ("STANDBY"); 137198849Smav case 0xe3: return ("IDLE"); 138198849Smav case 0xe4: return ("READ_BUFFER/PM"); 139198849Smav case 0xe5: return ("CHECK_POWER_MODE"); 140198849Smav case 0xe6: return ("SLEEP"); 141198849Smav case 0xe7: return ("FLUSHCACHE"); 142198849Smav case 0xe8: return ("WRITE_PM"); 143198849Smav case 0xea: return ("FLUSHCACHE48"); 144198849Smav case 0xec: return ("ATA_IDENTIFY"); 145198849Smav case 0xed: return ("MEDIA_EJECT"); 146198849Smav case 0xef: 147198849Smav switch (cmd->features) { 148198849Smav case 0x03: return ("SETFEATURES SET TRANSFER MODE"); 149198849Smav case 0x02: return ("SETFEATURES ENABLE WCACHE"); 150198849Smav case 0x82: return ("SETFEATURES DISABLE WCACHE"); 151203421Smav case 0x06: return ("SETFEATURES ENABLE PUIS"); 152203421Smav case 0x86: return ("SETFEATURES DISABLE PUIS"); 153203421Smav case 0x07: return ("SETFEATURES SPIN-UP"); 154220616Smav case 0x10: return ("SETFEATURES ENABLE SATA FEATURE"); 155220616Smav case 0x90: return ("SETFEATURES DISABLE SATA FEATURE"); 156198849Smav case 0xaa: return ("SETFEATURES ENABLE RCACHE"); 157198849Smav case 0x55: return ("SETFEATURES DISABLE RCACHE"); 158198849Smav } 159198849Smav return "SETFEATURES"; 160198849Smav case 0xf1: return ("SECURITY_SET_PASSWORD"); 161198849Smav case 0xf2: return ("SECURITY_UNLOCK"); 162198849Smav case 0xf3: return ("SECURITY_ERASE_PREPARE"); 163198849Smav case 0xf4: return ("SECURITY_ERASE_UNIT"); 164198849Smav case 0xf5: return ("SECURITY_FREE_LOCK"); 165198849Smav case 0xf6: return ("SECURITY DISABLE PASSWORD"); 166198849Smav case 0xf8: return ("READ_NATIVE_MAX_ADDRESS"); 167198849Smav case 0xf9: return ("SET_MAX_ADDRESS"); 168198849Smav } 169198849Smav return "UNKNOWN"; 170198849Smav} 171198849Smav 172198849Smavchar * 173198849Smavata_cmd_string(struct ata_cmd *cmd, char *cmd_string, size_t len) 174198849Smav{ 175198849Smav 176198849Smav snprintf(cmd_string, len, "%02x %02x %02x %02x " 177198849Smav "%02x %02x %02x %02x %02x %02x %02x %02x", 178198849Smav cmd->command, cmd->features, 179198849Smav cmd->lba_low, cmd->lba_mid, cmd->lba_high, cmd->device, 180198849Smav cmd->lba_low_exp, cmd->lba_mid_exp, cmd->lba_high_exp, 181198849Smav cmd->features_exp, cmd->sector_count, cmd->sector_count_exp); 182198849Smav 183198849Smav return(cmd_string); 184198849Smav} 185198849Smav 186198849Smavchar * 187198849Smavata_res_string(struct ata_res *res, char *res_string, size_t len) 188198849Smav{ 189198849Smav 190198849Smav snprintf(res_string, len, "%02x %02x %02x %02x " 191198849Smav "%02x %02x %02x %02x %02x %02x %02x", 192198849Smav res->status, res->error, 193198849Smav res->lba_low, res->lba_mid, res->lba_high, res->device, 194198849Smav res->lba_low_exp, res->lba_mid_exp, res->lba_high_exp, 195198849Smav res->sector_count, res->sector_count_exp); 196198849Smav 197198849Smav return(res_string); 198198849Smav} 199198849Smav 200198849Smav/* 201198849Smav * ata_command_sbuf() returns 0 for success and -1 for failure. 202198849Smav */ 203198849Smavint 204198849Smavata_command_sbuf(struct ccb_ataio *ataio, struct sbuf *sb) 205198849Smav{ 206198849Smav char cmd_str[(12 * 3) + 1]; 207198849Smav 208203108Smav sbuf_printf(sb, "%s. ACB: %s", 209198849Smav ata_op_string(&ataio->cmd), 210198849Smav ata_cmd_string(&ataio->cmd, cmd_str, sizeof(cmd_str))); 211198849Smav 212198849Smav return(0); 213198849Smav} 214198849Smav 215198849Smav/* 216198849Smav * ata_status_abuf() returns 0 for success and -1 for failure. 217198849Smav */ 218198849Smavint 219198849Smavata_status_sbuf(struct ccb_ataio *ataio, struct sbuf *sb) 220198849Smav{ 221198849Smav 222203108Smav sbuf_printf(sb, "ATA status: %02x (%s%s%s%s%s%s%s%s)", 223198849Smav ataio->res.status, 224198849Smav (ataio->res.status & 0x80) ? "BSY " : "", 225198849Smav (ataio->res.status & 0x40) ? "DRDY " : "", 226198849Smav (ataio->res.status & 0x20) ? "DF " : "", 227198849Smav (ataio->res.status & 0x10) ? "SERV " : "", 228198849Smav (ataio->res.status & 0x08) ? "DRQ " : "", 229198849Smav (ataio->res.status & 0x04) ? "CORR " : "", 230198849Smav (ataio->res.status & 0x02) ? "IDX " : "", 231198849Smav (ataio->res.status & 0x01) ? "ERR" : ""); 232198849Smav if (ataio->res.status & 1) { 233203108Smav sbuf_printf(sb, ", error: %02x (%s%s%s%s%s%s%s%s)", 234198849Smav ataio->res.error, 235198849Smav (ataio->res.error & 0x80) ? "ICRC " : "", 236198849Smav (ataio->res.error & 0x40) ? "UNC " : "", 237198849Smav (ataio->res.error & 0x20) ? "MC " : "", 238198849Smav (ataio->res.error & 0x10) ? "IDNF " : "", 239198849Smav (ataio->res.error & 0x08) ? "MCR " : "", 240198849Smav (ataio->res.error & 0x04) ? "ABRT " : "", 241198849Smav (ataio->res.error & 0x02) ? "NM " : "", 242198849Smav (ataio->res.error & 0x01) ? "ILI" : ""); 243198849Smav } 244198849Smav 245198849Smav return(0); 246198849Smav} 247198849Smav 248198849Smav/* 249198849Smav * ata_res_sbuf() returns 0 for success and -1 for failure. 250198849Smav */ 251198849Smavint 252198849Smavata_res_sbuf(struct ccb_ataio *ataio, struct sbuf *sb) 253198849Smav{ 254198849Smav char res_str[(11 * 3) + 1]; 255198849Smav 256198849Smav sbuf_printf(sb, "RES: %s", 257198849Smav ata_res_string(&ataio->res, res_str, sizeof(res_str))); 258198849Smav 259198849Smav return(0); 260198849Smav} 261198849Smav 262195534Sscottlvoid 263195534Sscottlata_print_ident(struct ata_params *ident_data) 264195534Sscottl{ 265195534Sscottl char product[48], revision[16]; 266195534Sscottl 267195534Sscottl cam_strvis(product, ident_data->model, sizeof(ident_data->model), 268195534Sscottl sizeof(product)); 269195534Sscottl cam_strvis(revision, ident_data->revision, sizeof(ident_data->revision), 270195534Sscottl sizeof(revision)); 271200218Smav printf("<%s %s> %s-%d", 272200218Smav product, revision, 273223019Smav (ident_data->config == ATA_PROTO_CFA) ? "CFA" : 274200218Smav (ident_data->config & ATA_PROTO_ATAPI) ? "ATAPI" : "ATA", 275200218Smav ata_version(ident_data->version_major)); 276195534Sscottl if (ident_data->satacapabilities && ident_data->satacapabilities != 0xffff) { 277197541Smav if (ident_data->satacapabilities & ATA_SATA_GEN3) 278197541Smav printf(" SATA 3.x"); 279197541Smav else if (ident_data->satacapabilities & ATA_SATA_GEN2) 280195534Sscottl printf(" SATA 2.x"); 281195534Sscottl else if (ident_data->satacapabilities & ATA_SATA_GEN1) 282195534Sscottl printf(" SATA 1.x"); 283195534Sscottl else 284195534Sscottl printf(" SATA"); 285195534Sscottl } 286195534Sscottl printf(" device\n"); 287195534Sscottl} 288195534Sscottl 289198897Smavuint32_t 290198897Smavata_logical_sector_size(struct ata_params *ident_data) 291198897Smav{ 292198897Smav if ((ident_data->pss & 0xc000) == 0x4000 && 293198897Smav (ident_data->pss & ATA_PSS_LSSABOVE512)) { 294198897Smav return ((u_int32_t)ident_data->lss_1 | 295198897Smav ((u_int32_t)ident_data->lss_2 << 16)); 296198897Smav } 297198897Smav return (512); 298198897Smav} 299198897Smav 300198897Smavuint64_t 301198897Smavata_physical_sector_size(struct ata_params *ident_data) 302198897Smav{ 303198897Smav if ((ident_data->pss & 0xc000) == 0x4000 && 304198897Smav (ident_data->pss & ATA_PSS_MULTLS)) { 305198897Smav return ((uint64_t)ata_logical_sector_size(ident_data) * 306198897Smav (1 << (ident_data->pss & ATA_PSS_LSPPS))); 307198897Smav } 308198897Smav return (512); 309198897Smav} 310198897Smav 311198897Smavuint64_t 312198897Smavata_logical_sector_offset(struct ata_params *ident_data) 313198897Smav{ 314198897Smav if ((ident_data->lsalign & 0xc000) == 0x4000) { 315198897Smav return ((uint64_t)ata_logical_sector_size(ident_data) * 316198897Smav (ident_data->lsalign & 0x3fff)); 317198897Smav } 318198897Smav return (0); 319198897Smav} 320198897Smav 321195534Sscottlvoid 322196659Smavata_28bit_cmd(struct ccb_ataio *ataio, uint8_t cmd, uint8_t features, 323195534Sscottl uint32_t lba, uint8_t sector_count) 324195534Sscottl{ 325195534Sscottl bzero(&ataio->cmd, sizeof(ataio->cmd)); 326195534Sscottl ataio->cmd.flags = 0; 327200008Smav if (cmd == ATA_READ_DMA || 328200008Smav cmd == ATA_READ_DMA_QUEUED || 329200008Smav cmd == ATA_WRITE_DMA || 330200008Smav cmd == ATA_WRITE_DMA_QUEUED) 331200008Smav ataio->cmd.flags |= CAM_ATAIO_DMA; 332195534Sscottl ataio->cmd.command = cmd; 333195534Sscottl ataio->cmd.features = features; 334195534Sscottl ataio->cmd.lba_low = lba; 335195534Sscottl ataio->cmd.lba_mid = lba >> 8; 336195534Sscottl ataio->cmd.lba_high = lba >> 16; 337195534Sscottl ataio->cmd.device = 0x40 | ((lba >> 24) & 0x0f); 338195534Sscottl ataio->cmd.sector_count = sector_count; 339195534Sscottl} 340195534Sscottl 341195534Sscottlvoid 342195534Sscottlata_48bit_cmd(struct ccb_ataio *ataio, uint8_t cmd, uint16_t features, 343195534Sscottl uint64_t lba, uint16_t sector_count) 344195534Sscottl{ 345195534Sscottl bzero(&ataio->cmd, sizeof(ataio->cmd)); 346195534Sscottl ataio->cmd.flags = CAM_ATAIO_48BIT; 347200008Smav if (cmd == ATA_READ_DMA48 || 348200008Smav cmd == ATA_READ_DMA_QUEUED48 || 349200008Smav cmd == ATA_READ_STREAM_DMA48 || 350200008Smav cmd == ATA_WRITE_DMA48 || 351200008Smav cmd == ATA_WRITE_DMA_FUA48 || 352200008Smav cmd == ATA_WRITE_DMA_QUEUED48 || 353200008Smav cmd == ATA_WRITE_DMA_QUEUED_FUA48 || 354201139Smav cmd == ATA_WRITE_STREAM_DMA48 || 355201139Smav cmd == ATA_DATA_SET_MANAGEMENT) 356200008Smav ataio->cmd.flags |= CAM_ATAIO_DMA; 357195534Sscottl ataio->cmd.command = cmd; 358195534Sscottl ataio->cmd.features = features; 359195534Sscottl ataio->cmd.lba_low = lba; 360195534Sscottl ataio->cmd.lba_mid = lba >> 8; 361195534Sscottl ataio->cmd.lba_high = lba >> 16; 362195534Sscottl ataio->cmd.device = 0x40; 363195534Sscottl ataio->cmd.lba_low_exp = lba >> 24; 364195534Sscottl ataio->cmd.lba_mid_exp = lba >> 32; 365195534Sscottl ataio->cmd.lba_high_exp = lba >> 40; 366195534Sscottl ataio->cmd.features_exp = features >> 8; 367195534Sscottl ataio->cmd.sector_count = sector_count; 368195534Sscottl ataio->cmd.sector_count_exp = sector_count >> 8; 369195534Sscottl} 370195534Sscottl 371195534Sscottlvoid 372195534Sscottlata_ncq_cmd(struct ccb_ataio *ataio, uint8_t cmd, 373195534Sscottl uint64_t lba, uint16_t sector_count) 374195534Sscottl{ 375195534Sscottl bzero(&ataio->cmd, sizeof(ataio->cmd)); 376195534Sscottl ataio->cmd.flags = CAM_ATAIO_48BIT | CAM_ATAIO_FPDMA; 377195534Sscottl ataio->cmd.command = cmd; 378195534Sscottl ataio->cmd.features = sector_count; 379195534Sscottl ataio->cmd.lba_low = lba; 380195534Sscottl ataio->cmd.lba_mid = lba >> 8; 381195534Sscottl ataio->cmd.lba_high = lba >> 16; 382195534Sscottl ataio->cmd.device = 0x40; 383195534Sscottl ataio->cmd.lba_low_exp = lba >> 24; 384195534Sscottl ataio->cmd.lba_mid_exp = lba >> 32; 385195534Sscottl ataio->cmd.lba_high_exp = lba >> 40; 386195534Sscottl ataio->cmd.features_exp = sector_count >> 8; 387195534Sscottl} 388195534Sscottl 389195534Sscottlvoid 390195534Sscottlata_reset_cmd(struct ccb_ataio *ataio) 391195534Sscottl{ 392195534Sscottl bzero(&ataio->cmd, sizeof(ataio->cmd)); 393195534Sscottl ataio->cmd.flags = CAM_ATAIO_CONTROL | CAM_ATAIO_NEEDRESULT; 394195534Sscottl ataio->cmd.control = 0x04; 395195534Sscottl} 396195534Sscottl 397195534Sscottlvoid 398195534Sscottlata_pm_read_cmd(struct ccb_ataio *ataio, int reg, int port) 399195534Sscottl{ 400195534Sscottl bzero(&ataio->cmd, sizeof(ataio->cmd)); 401198904Smav ataio->cmd.flags = CAM_ATAIO_NEEDRESULT; 402195534Sscottl ataio->cmd.command = ATA_READ_PM; 403195534Sscottl ataio->cmd.features = reg; 404195534Sscottl ataio->cmd.device = port & 0x0f; 405195534Sscottl} 406195534Sscottl 407195534Sscottlvoid 408198904Smavata_pm_write_cmd(struct ccb_ataio *ataio, int reg, int port, uint32_t val) 409195534Sscottl{ 410195534Sscottl bzero(&ataio->cmd, sizeof(ataio->cmd)); 411198904Smav ataio->cmd.flags = 0; 412195534Sscottl ataio->cmd.command = ATA_WRITE_PM; 413195534Sscottl ataio->cmd.features = reg; 414198904Smav ataio->cmd.sector_count = val; 415195534Sscottl ataio->cmd.lba_low = val >> 8; 416195534Sscottl ataio->cmd.lba_mid = val >> 16; 417195534Sscottl ataio->cmd.lba_high = val >> 24; 418195534Sscottl ataio->cmd.device = port & 0x0f; 419195534Sscottl} 420195534Sscottl 421195534Sscottlvoid 422195534Sscottlata_bswap(int8_t *buf, int len) 423195534Sscottl{ 424195534Sscottl u_int16_t *ptr = (u_int16_t*)(buf + len); 425195534Sscottl 426195534Sscottl while (--ptr >= (u_int16_t*)buf) 427195534Sscottl *ptr = be16toh(*ptr); 428195534Sscottl} 429195534Sscottl 430195534Sscottlvoid 431195534Sscottlata_btrim(int8_t *buf, int len) 432195534Sscottl{ 433195534Sscottl int8_t *ptr; 434195534Sscottl 435195534Sscottl for (ptr = buf; ptr < buf+len; ++ptr) 436195534Sscottl if (!*ptr || *ptr == '_') 437195534Sscottl *ptr = ' '; 438195534Sscottl for (ptr = buf + len - 1; ptr >= buf && *ptr == ' '; --ptr) 439195534Sscottl *ptr = 0; 440195534Sscottl} 441195534Sscottl 442195534Sscottlvoid 443195534Sscottlata_bpack(int8_t *src, int8_t *dst, int len) 444195534Sscottl{ 445195534Sscottl int i, j, blank; 446195534Sscottl 447195534Sscottl for (i = j = blank = 0 ; i < len; i++) { 448195534Sscottl if (blank && src[i] == ' ') continue; 449195534Sscottl if (blank && src[i] != ' ') { 450195534Sscottl dst[j++] = src[i]; 451195534Sscottl blank = 0; 452195534Sscottl continue; 453195534Sscottl } 454195534Sscottl if (src[i] == ' ') { 455195534Sscottl blank = 1; 456195534Sscottl if (i == 0) 457195534Sscottl continue; 458195534Sscottl } 459195534Sscottl dst[j++] = src[i]; 460195534Sscottl } 461195534Sscottl while (j < len) 462195534Sscottl dst[j++] = 0x00; 463195534Sscottl} 464195534Sscottl 465195534Sscottlint 466195534Sscottlata_max_pmode(struct ata_params *ap) 467195534Sscottl{ 468195534Sscottl if (ap->atavalid & ATA_FLAG_64_70) { 469195534Sscottl if (ap->apiomodes & 0x02) 470195534Sscottl return ATA_PIO4; 471195534Sscottl if (ap->apiomodes & 0x01) 472195534Sscottl return ATA_PIO3; 473195534Sscottl } 474195534Sscottl if (ap->mwdmamodes & 0x04) 475195534Sscottl return ATA_PIO4; 476195534Sscottl if (ap->mwdmamodes & 0x02) 477195534Sscottl return ATA_PIO3; 478195534Sscottl if (ap->mwdmamodes & 0x01) 479195534Sscottl return ATA_PIO2; 480195534Sscottl if ((ap->retired_piomode & ATA_RETIRED_PIO_MASK) == 0x200) 481195534Sscottl return ATA_PIO2; 482195534Sscottl if ((ap->retired_piomode & ATA_RETIRED_PIO_MASK) == 0x100) 483195534Sscottl return ATA_PIO1; 484195534Sscottl if ((ap->retired_piomode & ATA_RETIRED_PIO_MASK) == 0x000) 485195534Sscottl return ATA_PIO0; 486195534Sscottl return ATA_PIO0; 487195534Sscottl} 488195534Sscottl 489195534Sscottlint 490195534Sscottlata_max_wmode(struct ata_params *ap) 491195534Sscottl{ 492195534Sscottl if (ap->mwdmamodes & 0x04) 493195534Sscottl return ATA_WDMA2; 494195534Sscottl if (ap->mwdmamodes & 0x02) 495195534Sscottl return ATA_WDMA1; 496195534Sscottl if (ap->mwdmamodes & 0x01) 497195534Sscottl return ATA_WDMA0; 498195534Sscottl return -1; 499195534Sscottl} 500195534Sscottl 501195534Sscottlint 502195534Sscottlata_max_umode(struct ata_params *ap) 503195534Sscottl{ 504195534Sscottl if (ap->atavalid & ATA_FLAG_88) { 505195534Sscottl if (ap->udmamodes & 0x40) 506195534Sscottl return ATA_UDMA6; 507195534Sscottl if (ap->udmamodes & 0x20) 508195534Sscottl return ATA_UDMA5; 509195534Sscottl if (ap->udmamodes & 0x10) 510195534Sscottl return ATA_UDMA4; 511195534Sscottl if (ap->udmamodes & 0x08) 512195534Sscottl return ATA_UDMA3; 513195534Sscottl if (ap->udmamodes & 0x04) 514195534Sscottl return ATA_UDMA2; 515195534Sscottl if (ap->udmamodes & 0x02) 516195534Sscottl return ATA_UDMA1; 517195534Sscottl if (ap->udmamodes & 0x01) 518195534Sscottl return ATA_UDMA0; 519195534Sscottl } 520195534Sscottl return -1; 521195534Sscottl} 522195534Sscottl 523195534Sscottlint 524199747Smavata_max_mode(struct ata_params *ap, int maxmode) 525195534Sscottl{ 526195534Sscottl 527199747Smav if (maxmode == 0) 528199747Smav maxmode = ATA_DMA_MAX; 529199747Smav if (maxmode >= ATA_UDMA0 && ata_max_umode(ap) > 0) 530199747Smav return (min(maxmode, ata_max_umode(ap))); 531199747Smav if (maxmode >= ATA_WDMA0 && ata_max_wmode(ap) > 0) 532199747Smav return (min(maxmode, ata_max_wmode(ap))); 533199747Smav return (min(maxmode, ata_max_pmode(ap))); 534199747Smav} 535195534Sscottl 536199747Smavchar * 537199747Smavata_mode2string(int mode) 538199747Smav{ 539199747Smav switch (mode) { 540199747Smav case -1: return "UNSUPPORTED"; 541199747Smav case 0: return "NONE"; 542199747Smav case ATA_PIO0: return "PIO0"; 543199747Smav case ATA_PIO1: return "PIO1"; 544199747Smav case ATA_PIO2: return "PIO2"; 545199747Smav case ATA_PIO3: return "PIO3"; 546199747Smav case ATA_PIO4: return "PIO4"; 547199747Smav case ATA_WDMA0: return "WDMA0"; 548199747Smav case ATA_WDMA1: return "WDMA1"; 549199747Smav case ATA_WDMA2: return "WDMA2"; 550199747Smav case ATA_UDMA0: return "UDMA0"; 551199747Smav case ATA_UDMA1: return "UDMA1"; 552199747Smav case ATA_UDMA2: return "UDMA2"; 553199747Smav case ATA_UDMA3: return "UDMA3"; 554199747Smav case ATA_UDMA4: return "UDMA4"; 555199747Smav case ATA_UDMA5: return "UDMA5"; 556199747Smav case ATA_UDMA6: return "UDMA6"; 557199747Smav default: 558199747Smav if (mode & ATA_DMA_MASK) 559199747Smav return "BIOSDMA"; 560199747Smav else 561199747Smav return "BIOSPIO"; 562199747Smav } 563199747Smav} 564195534Sscottl 565199821Smavint 566199821Smavata_string2mode(char *str) 567199821Smav{ 568199821Smav if (!strcasecmp(str, "PIO0")) return (ATA_PIO0); 569199821Smav if (!strcasecmp(str, "PIO1")) return (ATA_PIO1); 570199821Smav if (!strcasecmp(str, "PIO2")) return (ATA_PIO2); 571199821Smav if (!strcasecmp(str, "PIO3")) return (ATA_PIO3); 572199821Smav if (!strcasecmp(str, "PIO4")) return (ATA_PIO4); 573199821Smav if (!strcasecmp(str, "WDMA0")) return (ATA_WDMA0); 574199821Smav if (!strcasecmp(str, "WDMA1")) return (ATA_WDMA1); 575199821Smav if (!strcasecmp(str, "WDMA2")) return (ATA_WDMA2); 576199821Smav if (!strcasecmp(str, "UDMA0")) return (ATA_UDMA0); 577199821Smav if (!strcasecmp(str, "UDMA16")) return (ATA_UDMA0); 578199821Smav if (!strcasecmp(str, "UDMA1")) return (ATA_UDMA1); 579199821Smav if (!strcasecmp(str, "UDMA25")) return (ATA_UDMA1); 580199821Smav if (!strcasecmp(str, "UDMA2")) return (ATA_UDMA2); 581199821Smav if (!strcasecmp(str, "UDMA33")) return (ATA_UDMA2); 582199821Smav if (!strcasecmp(str, "UDMA3")) return (ATA_UDMA3); 583199821Smav if (!strcasecmp(str, "UDMA44")) return (ATA_UDMA3); 584199821Smav if (!strcasecmp(str, "UDMA4")) return (ATA_UDMA4); 585199821Smav if (!strcasecmp(str, "UDMA66")) return (ATA_UDMA4); 586199821Smav if (!strcasecmp(str, "UDMA5")) return (ATA_UDMA5); 587199821Smav if (!strcasecmp(str, "UDMA100")) return (ATA_UDMA5); 588199821Smav if (!strcasecmp(str, "UDMA6")) return (ATA_UDMA6); 589199821Smav if (!strcasecmp(str, "UDMA133")) return (ATA_UDMA6); 590199821Smav return (-1); 591199821Smav} 592199821Smav 593199821Smav 594199747Smavu_int 595199747Smavata_mode2speed(int mode) 596199747Smav{ 597199747Smav switch (mode) { 598199747Smav case ATA_PIO0: 599199747Smav default: 600199747Smav return (3300); 601199747Smav case ATA_PIO1: 602199747Smav return (5200); 603199747Smav case ATA_PIO2: 604199747Smav return (8300); 605199747Smav case ATA_PIO3: 606199747Smav return (11100); 607199747Smav case ATA_PIO4: 608199747Smav return (16700); 609199747Smav case ATA_WDMA0: 610199747Smav return (4200); 611199747Smav case ATA_WDMA1: 612199747Smav return (13300); 613199747Smav case ATA_WDMA2: 614199747Smav return (16700); 615199747Smav case ATA_UDMA0: 616199747Smav return (16700); 617199747Smav case ATA_UDMA1: 618199747Smav return (25000); 619199747Smav case ATA_UDMA2: 620199747Smav return (33300); 621199747Smav case ATA_UDMA3: 622199747Smav return (44400); 623199747Smav case ATA_UDMA4: 624199747Smav return (66700); 625199747Smav case ATA_UDMA5: 626199747Smav return (100000); 627199747Smav case ATA_UDMA6: 628199747Smav return (133000); 629199747Smav } 630199747Smav} 631195534Sscottl 632199747Smavu_int 633199747Smavata_revision2speed(int revision) 634199747Smav{ 635199747Smav switch (revision) { 636199747Smav case 1: 637199747Smav default: 638199747Smav return (150000); 639199747Smav case 2: 640199747Smav return (300000); 641199747Smav case 3: 642199747Smav return (600000); 643199747Smav } 644199747Smav} 645195534Sscottl 646199747Smavint 647199747Smavata_speed2revision(u_int speed) 648199747Smav{ 649199747Smav switch (speed) { 650199821Smav case 0: 651199821Smav return (0); 652199747Smav case 150000: 653199747Smav return (1); 654199747Smav case 300000: 655199747Smav return (2); 656199747Smav case 600000: 657199747Smav return (3); 658199821Smav default: 659199821Smav return (-1); 660199747Smav } 661195534Sscottl} 662195534Sscottl 663199178Smavint 664199178Smavata_identify_match(caddr_t identbuffer, caddr_t table_entry) 665199178Smav{ 666199178Smav struct scsi_inquiry_pattern *entry; 667199178Smav struct ata_params *ident; 668199178Smav 669199178Smav entry = (struct scsi_inquiry_pattern *)table_entry; 670199178Smav ident = (struct ata_params *)identbuffer; 671199178Smav 672199178Smav if ((cam_strmatch(ident->model, entry->product, 673199178Smav sizeof(ident->model)) == 0) 674199178Smav && (cam_strmatch(ident->revision, entry->revision, 675199178Smav sizeof(ident->revision)) == 0)) { 676199178Smav return (0); 677199178Smav } 678199178Smav return (-1); 679199178Smav} 680199178Smav 681199178Smavint 682199178Smavata_static_identify_match(caddr_t identbuffer, caddr_t table_entry) 683199178Smav{ 684199178Smav struct scsi_static_inquiry_pattern *entry; 685199178Smav struct ata_params *ident; 686199178Smav 687199178Smav entry = (struct scsi_static_inquiry_pattern *)table_entry; 688199178Smav ident = (struct ata_params *)identbuffer; 689199178Smav 690199178Smav if ((cam_strmatch(ident->model, entry->product, 691199178Smav sizeof(ident->model)) == 0) 692199178Smav && (cam_strmatch(ident->revision, entry->revision, 693199178Smav sizeof(ident->revision)) == 0)) { 694199178Smav return (0); 695199178Smav } 696199178Smav return (-1); 697199178Smav} 698