1 /* 2 * CDDL HEADER START 3 * 4 * The contents of this file are subject to the terms of the 5 * Common Development and Distribution License (the "License"). 6 * You may not use this file except in compliance with the License. 7 * 8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 9 * or http://www.opensolaris.org/os/licensing. 10 * See the License for the specific language governing permissions 11 * and limitations under the License. 12 * 13 * When distributing Covered Code, include this CDDL HEADER in each 14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 15 * If applicable, add the following below this CDDL HEADER, with the 16 * fields enclosed by brackets "[]" replaced with your own identifying 17 * information: Portions Copyright [yyyy] [name of copyright owner] 18 * 19 * CDDL HEADER END 20 */ 21 22 /* 23 * Copyright 2008 Sun Microsystems, Inc. All rights reserved. 24 * Use is subject to license terms. 25 */ 26 27 #pragma ident "%Z%%M% %I% %E% SMI" 28 29 #include <sys/types.h> 30 #include <sys/dkio.h> 31 #include <sys/cdio.h> 32 #include <sys/file.h> 33 34 #include "ata_common.h" 35 #include "ata_disk.h" 36 37 /* 38 * this typedef really should be in dktp/cmpkt.h 39 */ 40 typedef struct cmpkt cmpkt_t; 41 42 43 /* 44 * DADA entry points 45 */ 46 47 static int ata_disk_abort(opaque_t ctl_data, cmpkt_t *pktp); 48 static int ata_disk_reset(opaque_t ctl_data, int level); 49 static int ata_disk_ioctl(opaque_t ctl_data, int cmd, intptr_t a, int flag); 50 static cmpkt_t *ata_disk_pktalloc(opaque_t ctl_data, int (*callback)(caddr_t), 51 caddr_t arg); 52 static void ata_disk_pktfree(opaque_t ctl_data, cmpkt_t *pktp); 53 static cmpkt_t *ata_disk_memsetup(opaque_t ctl_data, cmpkt_t *pktp, 54 struct buf *bp, int (*callback)(caddr_t), caddr_t arg); 55 static void ata_disk_memfree(opaque_t ctl_data, cmpkt_t *pktp); 56 static cmpkt_t *ata_disk_iosetup(opaque_t ctl_data, cmpkt_t *pktp); 57 static int ata_disk_transport(opaque_t ctl_data, cmpkt_t *pktp); 58 59 /* 60 * DADA packet callbacks 61 */ 62 63 static void ata_disk_complete(ata_drv_t *ata_drvp, ata_pkt_t *ata_pktp, 64 int do_callback); 65 static int ata_disk_intr(ata_ctl_t *ata_ctlp, ata_drv_t *ata_drvp, 66 ata_pkt_t *ata_pktp); 67 static int ata_disk_intr_dma(ata_ctl_t *ata_ctlp, ata_drv_t *ata_drvp, 68 ata_pkt_t *ata_pktp); 69 static int ata_disk_intr_pio_in(ata_ctl_t *ata_ctlp, ata_drv_t *ata_drvp, 70 ata_pkt_t *ata_pktp); 71 static int ata_disk_intr_pio_out(ata_ctl_t *ata_ctlp, ata_drv_t *ata_drvp, 72 ata_pkt_t *ata_pktp); 73 static int ata_disk_start(ata_ctl_t *ata_ctlp, ata_drv_t *ata_drvp, 74 ata_pkt_t *ata_pktp); 75 static int ata_disk_start_dma_in(ata_ctl_t *ata_ctlp, ata_drv_t *ata_drvp, 76 ata_pkt_t *ata_pktp); 77 static int ata_disk_start_dma_out(ata_ctl_t *ata_ctlp, ata_drv_t *ata_drvp, 78 ata_pkt_t *ata_pktp); 79 static int ata_disk_start_pio_in(ata_ctl_t *ata_ctlp, ata_drv_t *ata_drvp, 80 ata_pkt_t *ata_pktp); 81 static int ata_disk_start_pio_out(ata_ctl_t *ata_ctlp, ata_drv_t *ata_drvp, 82 ata_pkt_t *ata_pktp); 83 84 /* 85 * Local Function prototypes 86 */ 87 88 static int ata_disk_eject(ata_ctl_t *ata_ctlp, ata_drv_t *ata_drvp, 89 ata_pkt_t *ata_pktp); 90 static void ata_disk_fake_inquiry(ata_drv_t *ata_drvp); 91 static void ata_disk_get_resid(ata_ctl_t *ata_ctlp, ata_drv_t *ata_drvp, 92 ata_pkt_t *ata_pktp); 93 static int ata_disk_initialize_device_parameters(ata_ctl_t *ata_ctlp, 94 ata_drv_t *ata_drvp); 95 static int ata_disk_lock(ata_ctl_t *ata_ctlp, ata_drv_t *ata_drvp, 96 ata_pkt_t *ata_pktp); 97 static int ata_disk_set_multiple(ata_ctl_t *ata_ctlp, ata_drv_t *ata_drvp); 98 static void ata_disk_pio_xfer_data_in(ata_ctl_t *ata_ctlp, ata_pkt_t *ata_pktp); 99 static void ata_disk_pio_xfer_data_out(ata_ctl_t *ata_ctlp, 100 ata_pkt_t *ata_pktp); 101 static void ata_disk_set_standby_timer(ata_ctl_t *ata_ctlp, 102 ata_drv_t *ata_drvp); 103 static int ata_disk_recalibrate(ata_ctl_t *ata_ctlp, ata_drv_t *ata_drvp, 104 ata_pkt_t *ata_pktp); 105 static int ata_disk_standby(ata_ctl_t *ata_ctlp, ata_drv_t *ata_drvp, 106 ata_pkt_t *ata_pktp); 107 static int ata_disk_start_common(ata_ctl_t *ata_ctlp, ata_drv_t *ata_drvp, 108 ata_pkt_t *ata_pktp); 109 static int ata_disk_state(ata_ctl_t *ata_ctlp, ata_drv_t *ata_drvp, 110 ata_pkt_t *ata_pktp); 111 static int ata_disk_unlock(ata_ctl_t *ata_ctlp, ata_drv_t *ata_drvp, 112 ata_pkt_t *ata_pktp); 113 static int ata_get_capacity(ata_drv_t *ata_drvp, uint64_t *capacity); 114 static void ata_fix_large_disk_geometry(ata_drv_t *ata_drvp); 115 static uint64_t ata_calculate_28bits_capacity(ata_drv_t *ata_drvp); 116 static uint64_t ata_calculate_48bits_capacity(ata_drv_t *ata_drvp); 117 static int ata_copy_dk_ioc_string(intptr_t arg, char *source, int length, 118 int flag); 119 static void ata_set_write_cache(ata_ctl_t *ata_ctlp, ata_drv_t *ata_drvp); 120 static int ata_disk_update_fw(gtgt_t *gtgtp, ata_ctl_t *ata_ctlp, 121 ata_drv_t *ata_drvp, caddr_t fwfile, uint_t size, 122 uint8_t type, int flag); 123 static int ata_disk_set_feature_spinup(ata_ctl_t *ata_ctlp, 124 ata_drv_t *ata_drvp, ata_pkt_t *ata_pktp); 125 static int ata_disk_id_update(ata_ctl_t *ata_ctlp, 126 ata_drv_t *ata_drvp, ata_pkt_t *ata_pktp); 127 128 129 /* 130 * Local static data 131 */ 132 133 uint_t ata_disk_init_dev_parm_wait = 4 * 1000000; 134 uint_t ata_disk_set_mult_wait = 4 * 1000000; 135 int ata_disk_do_standby_timer = TRUE; 136 137 /* timeout value for device update firmware */ 138 int ata_disk_updatefw_time = 60; 139 140 /* 141 * ata_write_cache == 1 force write cache on. 142 * ata_write_cache == 0 do not modify write cache. firmware defaults kept. 143 * ata_write_cache == -1 force write cache off. 144 */ 145 int ata_write_cache = 1; 146 147 148 static struct ctl_objops ata_disk_objops = { 149 ata_disk_pktalloc, 150 ata_disk_pktfree, 151 ata_disk_memsetup, 152 ata_disk_memfree, 153 ata_disk_iosetup, 154 ata_disk_transport, 155 ata_disk_reset, 156 ata_disk_abort, 157 nulldev, 158 nulldev, 159 ata_disk_ioctl, 160 0, 0 161 }; 162 163 164 165 /* 166 * 167 * initialize the ata_disk sub-system 168 * 169 */ 170 171 /*ARGSUSED*/ 172 int 173 ata_disk_attach( 174 ata_ctl_t *ata_ctlp) 175 { 176 ADBG_TRACE(("ata_disk_init entered\n")); 177 return (TRUE); 178 } 179 180 181 182 /* 183 * 184 * destroy the ata_disk sub-system 185 * 186 */ 187 188 /*ARGSUSED*/ 189 void 190 ata_disk_detach( 191 ata_ctl_t *ata_ctlp) 192 { 193 ADBG_TRACE(("ata_disk_destroy entered\n")); 194 } 195 196 197 /* 198 * Test whether the disk can support Logical Block Addressing 199 */ 200 201 int 202 ata_test_lba_support(struct ata_id *aidp) 203 { 204 #ifdef __old_version__ 205 /* 206 * determine if the drive supports LBA mode 207 */ 208 if (aidp->ai_cap & ATAC_LBA_SUPPORT) 209 return (TRUE); 210 #else 211 /* 212 * Determine if the drive supports LBA mode 213 * LBA mode is mandatory on ATA-3 (or newer) drives but is 214 * optional on ATA-2 (or older) drives. On ATA-2 drives 215 * the ai_majorversion word should be 0xffff or 0x0000 216 * (version not reported). 217 */ 218 if (aidp->ai_majorversion != 0xffff && 219 aidp->ai_majorversion >= (1 << 3)) { 220 /* ATA-3 or better */ 221 return (TRUE); 222 } else if (aidp->ai_cap & ATAC_LBA_SUPPORT) { 223 /* ATA-2 LBA capability bit set */ 224 return (TRUE); 225 } else { 226 return (FALSE); 227 } 228 #endif 229 } 230 231 /* 232 * ATA-6 drives do not provide geometry information, so words 233 * ai_heads, ai_sectors and ai_fixcyls may not be valid 234 */ 235 static void 236 ata_fixup_ata6_geometry(struct ata_id *aidp) 237 { 238 /* check cylinders, heads, and sectors for valid values */ 239 if (aidp->ai_heads != 0 && aidp->ai_heads != 0xffff && 240 aidp->ai_sectors != 0 && aidp->ai_sectors != 0xffff && 241 aidp->ai_fixcyls != 0) 242 return; /* assume valid geometry - do nothing */ 243 244 /* 245 * Pre-set standard geometry values - they are not necessarily 246 * optimal for a given capacity 247 */ 248 aidp->ai_heads = 0x10; 249 aidp->ai_sectors = 0x3f; 250 aidp->ai_fixcyls = 1; 251 /* 252 * The fixcyls value will get fixed up later in 253 * ata_fix_large_disk_geometry. 254 */ 255 } 256 257 /* 258 * 259 * initialize the soft-structure for an ATA (non-PACKET) drive and 260 * then configure the drive with the correct modes and options. 261 * 262 */ 263 264 int 265 ata_disk_init_drive( 266 ata_drv_t *ata_drvp) 267 { 268 ata_ctl_t *ata_ctlp = ata_drvp->ad_ctlp; 269 struct ata_id *aidp = &ata_drvp->ad_id; 270 struct ctl_obj *ctlobjp; 271 struct scsi_device *devp; 272 int len; 273 int val; 274 int mode; 275 short *chs; 276 char buf[80]; 277 278 ADBG_TRACE(("ata_disk_init_drive entered\n")); 279 280 /* ATA disks don't support LUNs */ 281 282 if (ata_drvp->ad_lun != 0) 283 return (FALSE); 284 285 /* 286 * set up drive structure 287 * ATA-6 drives do not provide geometry information, so words 288 * ai_heads, ai_sectors and ai_fixcyls may not be valid - they 289 * will be fixed later 290 */ 291 292 ata_drvp->ad_phhd = aidp->ai_heads; 293 ata_drvp->ad_phsec = aidp->ai_sectors; 294 ata_drvp->ad_drvrhd = aidp->ai_heads; 295 ata_drvp->ad_drvrsec = aidp->ai_sectors; 296 ata_drvp->ad_drvrcyl = aidp->ai_fixcyls; 297 ata_drvp->ad_acyl = 0; 298 299 if (ata_test_lba_support(&ata_drvp->ad_id)) 300 ata_drvp->ad_drive_bits |= ATDH_LBA; 301 302 /* Get capacity and check for 48-bit mode */ 303 mode = ata_get_capacity(ata_drvp, &ata_drvp->ad_capacity); 304 if (mode == AD_EXT48) { 305 ata_drvp->ad_flags |= AD_EXT48; 306 } 307 308 /* straighten out the geometry */ 309 (void) sprintf(buf, "SUNW-ata-%p-d%d-chs", (void *) ata_ctlp->ac_data, 310 ata_drvp->ad_targ+1); 311 if (ddi_getlongprop(DDI_DEV_T_ANY, ddi_root_node(), 0, 312 buf, (caddr_t)&chs, &len) == DDI_PROP_SUCCESS) { 313 /* 314 * if the number of sectors and heads in bios matches the 315 * physical geometry, then so should the number of cylinders 316 * this is to prevent the 1023 limit in the older bios's 317 * causing loss of space. 318 */ 319 if (chs[1] == (ata_drvp->ad_drvrhd - 1) && 320 chs[2] == ata_drvp->ad_drvrsec) 321 /* Set chs[0] to zero-based number of cylinders. */ 322 chs[0] = aidp->ai_fixcyls - 1; 323 else if (!(ata_drvp->ad_drive_bits & ATDH_LBA)) { 324 /* 325 * if the the sector/heads do not match that of the 326 * bios and the drive does not support LBA. We go ahead 327 * and advertise the bios geometry but use the physical 328 * geometry for sector translation. 329 */ 330 cmn_err(CE_WARN, "!Disk 0x%p,%d: BIOS geometry " 331 "different from physical, and no LBA support.", 332 (void *)ata_ctlp->ac_data, ata_drvp->ad_targ); 333 } 334 335 /* 336 * chs[0,1] are zero-based; make them one-based. 337 */ 338 ata_drvp->ad_drvrcyl = chs[0] + 1; 339 ata_drvp->ad_drvrhd = chs[1] + 1; 340 ata_drvp->ad_drvrsec = chs[2]; 341 kmem_free(chs, len); 342 } else { 343 /* 344 * Property not present; this means that boot.bin has 345 * determined that the drive supports Int13 LBA. Note 346 * this, but just return a geometry with a large 347 * cylinder count; this will be the signal for dadk to 348 * fail DKIOCG_VIRTGEOM. 349 * ad_drvr* are already set; just recalculate ad_drvrcyl 350 * from capacity. 351 */ 352 353 ata_drvp->ad_flags |= AD_INT13LBA; 354 if (ata_drvp->ad_capacity != 0) { 355 ata_drvp->ad_drvrcyl = ata_drvp->ad_capacity / 356 (ata_drvp->ad_drvrhd * ata_drvp->ad_drvrsec); 357 } else { 358 /* 359 * Something's wrong; return something sure to 360 * fail the "cyls < 1024" test. This will 361 * never make it out of the DKIOCG_VIRTGEOM 362 * call, so its total bogosity won't matter. 363 */ 364 ata_drvp->ad_drvrcyl = 1025; 365 ata_drvp->ad_drvrhd = 1; 366 ata_drvp->ad_drvrsec = 1; 367 } 368 } 369 370 /* fix geometry for disks > 31GB, if needed */ 371 ata_fix_large_disk_geometry(ata_drvp); 372 373 /* 374 * set up the scsi_device and ctl_obj structures 375 */ 376 devp = kmem_zalloc(scsi_device_size(), KM_SLEEP); 377 ata_drvp->ad_device = devp; 378 ctlobjp = &ata_drvp->ad_ctl_obj; 379 380 devp->sd_inq = &ata_drvp->ad_inquiry; 381 devp->sd_address.a_hba_tran = (scsi_hba_tran_t *)ctlobjp; 382 devp->sd_address.a_target = (ushort_t)ata_drvp->ad_targ; 383 devp->sd_address.a_lun = (uchar_t)ata_drvp->ad_lun; 384 mutex_init(&devp->sd_mutex, NULL, MUTEX_DRIVER, NULL); 385 ata_drvp->ad_flags |= AD_MUTEX_INIT; 386 387 /* 388 * DADA ops vectors and cookie 389 */ 390 ctlobjp->c_ops = (struct ctl_objops *)&ata_disk_objops; 391 392 /* 393 * this is filled in with gtgtp by ata_disk_bus_ctl(INITCHILD) 394 */ 395 ctlobjp->c_data = NULL; 396 397 ctlobjp->c_ext = &(ctlobjp->c_extblk); 398 ctlobjp->c_extblk.c_ctldip = ata_ctlp->ac_dip; 399 ctlobjp->c_extblk.c_targ = ata_drvp->ad_targ; 400 ctlobjp->c_extblk.c_blksz = NBPSCTR; 401 402 /* 403 * Get highest block factor supported by the drive. 404 * Some drives report 0 if read/write multiple not supported, 405 * adjust their blocking factor to 1. 406 */ 407 ata_drvp->ad_block_factor = aidp->ai_mult1 & 0xff; 408 409 /* 410 * If a block factor property exists, use the smaller of the 411 * property value and the highest value the drive can support. 412 */ 413 (void) sprintf(buf, "drive%d_block_factor", ata_drvp->ad_targ); 414 val = ddi_prop_get_int(DDI_DEV_T_ANY, ata_ctlp->ac_dip, 0, buf, 415 ata_drvp->ad_block_factor); 416 417 ata_drvp->ad_block_factor = (short)min(val, ata_drvp->ad_block_factor); 418 419 if (ata_drvp->ad_block_factor == 0) 420 ata_drvp->ad_block_factor = 1; 421 422 if (!ata_disk_setup_parms(ata_ctlp, ata_drvp)) { 423 ata_drvp->ad_device = NULL; 424 kmem_free(devp, scsi_device_size()); 425 return (FALSE); 426 } 427 428 ata_disk_fake_inquiry(ata_drvp); 429 430 return (TRUE); 431 } 432 433 /* 434 * Test if a disk supports 48-bit (extended mode) addressing and 435 * get disk capacity. 436 * Return value: 437 * AD_EXT48 if 48-bit mode is available, 0 otherwise, 438 * capacity in sectors. 439 * There are several indicators for 48-bit addressing. If any of 440 * them is missing, assume 28-bit (non-extended) addressing. 441 */ 442 443 static int 444 ata_get_capacity(ata_drv_t *ata_drvp, uint64_t *capacity) 445 { 446 struct ata_id *aidp = &ata_drvp->ad_id; 447 uint64_t cap28; /* capacity in 28-bit mode */ 448 uint64_t cap48; /* capacity in 48-bit mode */ 449 450 /* 451 * First compute capacity in 28-bit mode, using 28-bit capacity 452 * words in IDENTIFY DEVICE response words 453 */ 454 cap28 = ata_calculate_28bits_capacity(ata_drvp); 455 *capacity = cap28; 456 457 /* No 48-bit mode before ATA 6 */ 458 if (!IS_ATA_VERSION_SUPPORTED(aidp, 6)) 459 return (0); 460 461 /* Check that 48 bit addressing is supported & enabled */ 462 /* words 83 and 86 */ 463 if (!(aidp->ai_cmdset83 & ATACS_EXT48)) 464 return (0); 465 if (!(aidp->ai_features86 & ATACS_EXT48)) 466 return (0); 467 468 /* 469 * Drive supports ATA-6. Since ATA-6 drives may not provide 470 * geometry info, pre-set standard geometry values 471 */ 472 ata_fixup_ata6_geometry(aidp); 473 474 /* Compute 48-bit capacity */ 475 cap48 = ata_calculate_48bits_capacity(ata_drvp); 476 477 /* 478 * If capacity is smaller then the maximum capacity addressable 479 * in 28-bit mode, just use 28-bit capacity value. 480 * We will use 28-bit addressing read/write commands. 481 */ 482 if (cap48 <= MAX_28BIT_CAPACITY) 483 return (0); 484 485 /* 486 * Capacity is too big for 28-bits addressing. But, to make 487 * sure that the drive implements ATA-6 correctly, the 488 * final check: cap28 should be MAX for 28-bit addressing. 489 * If it's not, we shouldn't use 48-bit mode, so return 490 * the capacity reported in 28-bit capacity words. 491 */ 492 if (cap28 != MAX_28BIT_CAPACITY) 493 return (0); /* not max, use 28-bit value */ 494 495 /* 496 * All is well so return 48-bit capacity indicator 497 */ 498 ADBG_INIT(("ATA: using 48-bit mode for capacity %llx blocks\n", 499 (unsigned long long)cap48)); 500 501 *capacity = cap48; 502 return (AD_EXT48); 503 } 504 505 /* 506 * With the advent of disks that hold more than 31 GB, we run into a 507 * limitation in the sizes of the fields that describe the geometry. 508 * The cylinders, heads, and sectors-per-track are each described by a 509 * 16-bit number -- both in the structure returned from IDENTIFY 510 * DEVICE and in the structure returned from the DIOCTL_GETGEOM or 511 * DIOCTL_GETPHYGEOM ioctl. 512 * 513 * The typical disk has 32 heads per cylinder and 63 sectors per 514 * track. A 16 bit field can contain up to 65535. So the largest 515 * disk that can be described in these fields is 65535 * 32 * 63 * 512 516 * (bytes/sector), or about 31.5 GB. The cylinder count gets truncated 517 * when stored in a narrow field, so a 40GB disk appears to have only 518 * 8 GB! 519 * 520 * The solution (for the time being at least) is to lie about the 521 * geometry. If the number of cylinders is too large to fit in 16 522 * bits, we will halve the cylinders and double the heads, repeating 523 * until we can fit the geometry into 3 shorts. 524 * FUTURE ENHANCEMENT: If this ever isn't enough, we could 525 * add another step to double sectors/track as well. 526 */ 527 528 static void 529 ata_fix_large_disk_geometry( 530 ata_drv_t *ata_drvp) 531 { 532 struct ata_id *aidp = &ata_drvp->ad_id; 533 534 /* no hope for large disks if LBA not supported */ 535 if (!(ata_drvp->ad_drive_bits & ATDH_LBA)) 536 return; 537 538 /* 539 * Fix up the geometry to be returned by DIOCTL_GETGEOM. 540 * If number of cylinders > USHRT_MAX, double heads and 541 * halve cylinders until everything fits. 542 */ 543 while (ata_drvp->ad_drvrcyl > USHRT_MAX) { 544 int tempheads; 545 546 /* is there room in 16 bits to double the heads? */ 547 tempheads = 2 * ata_drvp->ad_drvrhd; 548 if (tempheads > USHRT_MAX) { 549 /* 550 * No room to double the heads. 551 * I give up, there's no way to represent this. 552 * Limit disk size. 553 */ 554 cmn_err(CE_WARN, 555 "Disk is too large: " 556 "Model %s, Serial# %s " 557 "Approximating...\n", 558 aidp->ai_model, aidp->ai_drvser); 559 ata_drvp->ad_drvrcyl = USHRT_MAX; 560 break; 561 } 562 563 /* OK, so double the heads and halve the cylinders */ 564 ata_drvp->ad_drvrcyl /= 2; 565 ata_drvp->ad_drvrhd *= 2; 566 } 567 } 568 569 /* 570 * Calculate capacity using 28-bit capacity words from IDENTIFY DEVICE 571 * return words 572 */ 573 uint64_t 574 ata_calculate_28bits_capacity(ata_drv_t *ata_drvp) 575 { 576 /* 577 * Asked x3t13 for advice; this implements Hale Landis' 578 * response, minus the "use ATA_INIT_DEVPARMS". 579 * See "capacity.notes". 580 */ 581 582 /* some local shorthand/renaming to clarify the meaning */ 583 584 ushort_t curcyls_w54, curhds_w55, cursect_w56; 585 uint32_t curcap_w57_58; 586 587 if ((ata_drvp->ad_drive_bits & ATDH_LBA) != 0) { 588 return ((uint64_t)(ata_drvp->ad_id.ai_addrsec[0] + 589 ata_drvp->ad_id.ai_addrsec[1] * 0x10000)); 590 } 591 592 /* 593 * If we're not LBA, then first try to validate "current" values. 594 */ 595 596 curcyls_w54 = ata_drvp->ad_id.ai_curcyls; 597 curhds_w55 = ata_drvp->ad_id.ai_curheads; 598 cursect_w56 = ata_drvp->ad_id.ai_cursectrk; 599 curcap_w57_58 = ata_drvp->ad_id.ai_cursccp[0] + 600 ata_drvp->ad_id.ai_cursccp[1] * 0x10000; 601 602 if (((ata_drvp->ad_id.ai_validinfo & 1) == 1) && 603 (curhds_w55 >= 1) && (curhds_w55 <= 16) && 604 (cursect_w56 >= 1) && (cursect_w56 <= 63) && 605 (curcap_w57_58 == curcyls_w54 * curhds_w55 * cursect_w56)) { 606 return ((uint64_t)curcap_w57_58); 607 } 608 609 /* 610 * At this point, Hale recommends ATA_INIT_DEVPARMS. 611 * I don't want to do that, so simply use 1/3/6 as 612 * a final fallback, and continue to assume the BIOS 613 * has done whatever INIT_DEVPARMS are necessary. 614 */ 615 616 return ((uint64_t)(ata_drvp->ad_id.ai_fixcyls * 617 ata_drvp->ad_id.ai_heads * ata_drvp->ad_id.ai_sectors)); 618 } 619 620 /* 621 * Calculate capacity using 48-bits capacity words from IDENTIFY DEVICE 622 * return words 623 */ 624 uint64_t 625 ata_calculate_48bits_capacity(ata_drv_t *ata_drvp) 626 { 627 uint64_t cap48 = 0; 628 int i; 629 630 for (i = 3; i >= 0; --i) { 631 cap48 <<= 16; 632 cap48 += ata_drvp->ad_id.ai_addrsecxt[i]; 633 } 634 return (cap48); 635 } 636 637 638 /* 639 * 640 * Setup the drives Read/Write Multiple Blocking factor and the 641 * current translation geometry. Necessary during attach and after 642 * Software Resets. 643 * 644 */ 645 646 int 647 ata_disk_setup_parms( 648 ata_ctl_t *ata_ctlp, 649 ata_drv_t *ata_drvp) 650 { 651 652 /* 653 * program geometry info back to the drive 654 */ 655 if (!ata_disk_initialize_device_parameters(ata_ctlp, ata_drvp)) { 656 return (FALSE); 657 } 658 659 /* 660 * Determine the blocking factor 661 */ 662 if (ata_drvp->ad_block_factor > 1) { 663 /* 664 * Program the block factor into the drive. If this 665 * fails, then go back to using a block size of 1. 666 */ 667 if (!ata_disk_set_multiple(ata_ctlp, ata_drvp)) 668 ata_drvp->ad_block_factor = 1; 669 } 670 671 672 if (ata_drvp->ad_block_factor > 1) { 673 ata_drvp->ad_rd_cmd = ATC_RDMULT; 674 ata_drvp->ad_wr_cmd = ATC_WRMULT; 675 } else { 676 ata_drvp->ad_rd_cmd = ATC_RDSEC; 677 ata_drvp->ad_wr_cmd = ATC_WRSEC; 678 } 679 680 ata_drvp->ad_bytes_per_block = ata_drvp->ad_block_factor << SCTRSHFT; 681 682 ADBG_INIT(("set block factor for drive %d to %d\n", 683 ata_drvp->ad_targ, ata_drvp->ad_block_factor)); 684 685 if (ata_disk_do_standby_timer) 686 ata_disk_set_standby_timer(ata_ctlp, ata_drvp); 687 688 ata_set_write_cache(ata_ctlp, ata_drvp); 689 690 return (TRUE); 691 } 692 693 694 /* 695 * Take the timeout value specified in the "standby" property 696 * and convert from seconds to the magic parm expected by the 697 * the drive. Then issue the IDLE command to set the drive's 698 * internal standby timer. 699 */ 700 701 static void 702 ata_disk_set_standby_timer( 703 ata_ctl_t *ata_ctlp, 704 ata_drv_t *ata_drvp) 705 { 706 uchar_t parm; 707 int timeout = ata_ctlp->ac_standby_time; 708 709 /* 710 * take the timeout value, specificed in seconds, and 711 * encode it into the proper command parm 712 */ 713 714 /* 715 * don't change it if no property specified or if 716 * the specified value is out of range 717 */ 718 if (timeout < 0 || timeout > (12 * 60 * 60)) 719 return; 720 721 /* 1 to 1200 seconds (20 minutes) == N * 5 seconds */ 722 if (timeout <= (240 * 5)) 723 parm = (timeout + 4) / 5; 724 725 /* 20 to 21 minutes == 21 minutes */ 726 else if (timeout <= (21 * 60)) 727 parm = 252; 728 729 /* 21 minutes to 21 minutes 15 seconds == 21:15 */ 730 else if (timeout <= ((21 * 60) + 15)) 731 parm = 255; 732 733 /* 21:15 to 330 minutes == N * 30 minutes */ 734 else if (timeout <= (11 * 30 * 60)) 735 parm = 240 + ((timeout + (30 * 60) - 1)/ (30 * 60)); 736 737 /* > 330 minutes == 8 to 12 hours */ 738 else 739 parm = 253; 740 741 (void) ata_command(ata_ctlp, ata_drvp, TRUE, FALSE, 5 * 1000000, 742 ATC_IDLE, 0, parm, 0, 0, 0, 0); 743 } 744 745 746 747 /* 748 * 749 * destroy an ata disk drive 750 * 751 */ 752 753 void 754 ata_disk_uninit_drive( 755 ata_drv_t *ata_drvp) 756 { 757 struct scsi_device *devp = ata_drvp->ad_device; 758 759 ADBG_TRACE(("ata_disk_uninit_drive entered\n")); 760 761 if (devp) { 762 if (ata_drvp->ad_flags & AD_MUTEX_INIT) 763 mutex_destroy(&devp->sd_mutex); 764 ata_drvp->ad_device = NULL; 765 kmem_free(devp, scsi_device_size()); 766 } 767 } 768 769 770 771 772 /* 773 * 774 * DADA compliant bus_ctl entry point 775 * 776 */ 777 778 /*ARGSUSED*/ 779 int 780 ata_disk_bus_ctl( 781 dev_info_t *d, 782 dev_info_t *r, 783 ddi_ctl_enum_t o, 784 void *a, 785 void *v) 786 { 787 ADBG_TRACE(("ata_disk_bus_ctl entered\n")); 788 789 switch (o) { 790 791 case DDI_CTLOPS_REPORTDEV: 792 { 793 int targ; 794 795 targ = ddi_prop_get_int(DDI_DEV_T_ANY, r, DDI_PROP_DONTPASS, 796 "target", 0); 797 cmn_err(CE_CONT, "?%s%d at %s%d target %d lun %d\n", 798 ddi_driver_name(r), ddi_get_instance(r), 799 ddi_driver_name(d), ddi_get_instance(d), targ, 0); 800 return (DDI_SUCCESS); 801 } 802 case DDI_CTLOPS_INITCHILD: 803 { 804 dev_info_t *cdip = (dev_info_t *)a; 805 ata_drv_t *ata_drvp; 806 ata_ctl_t *ata_ctlp; 807 ata_tgt_t *ata_tgtp; 808 struct scsi_device *devp; 809 struct ctl_obj *ctlobjp; 810 gtgt_t *gtgtp; 811 char name[MAXNAMELEN]; 812 813 /* 814 * save time by picking up ptr to drive struct left 815 * by ata_bus_ctl - isn't that convenient. 816 */ 817 ata_drvp = ddi_get_driver_private(cdip); 818 ata_ctlp = ata_drvp->ad_ctlp; 819 820 /* set up pointers to child dip */ 821 822 devp = ata_drvp->ad_device; 823 /* 824 * If sd_dev is set, it means that the target has already 825 * being initialized. The cdip is a duplicate node from 826 * reexpansion of driver.conf. Fail INITCHILD here. 827 */ 828 if ((devp == NULL) || (devp->sd_dev != NULL)) { 829 return (DDI_FAILURE); 830 } 831 devp->sd_dev = cdip; 832 833 ctlobjp = &ata_drvp->ad_ctl_obj; 834 ctlobjp->c_extblk.c_devdip = cdip; 835 836 /* 837 * Create the "ata" property for use by the target driver 838 */ 839 if (!ata_prop_create(cdip, ata_drvp, "ata")) { 840 return (DDI_FAILURE); 841 } 842 843 gtgtp = ghd_target_init(d, cdip, &ata_ctlp->ac_ccc, 844 sizeof (ata_tgt_t), ata_ctlp, 845 ata_drvp->ad_targ, 846 ata_drvp->ad_lun); 847 848 /* gt_tgt_private points to ata_tgt_t */ 849 ata_tgtp = GTGTP2ATATGTP(gtgtp); 850 ata_tgtp->at_drvp = ata_drvp; 851 ata_tgtp->at_dma_attr = ata_pciide_dma_attr; 852 ata_tgtp->at_dma_attr.dma_attr_maxxfer = 853 ata_ctlp->ac_max_transfer << SCTRSHFT; 854 855 /* gtgtp is the opaque arg to all my entry points */ 856 ctlobjp->c_data = gtgtp; 857 858 /* create device name */ 859 860 (void) sprintf(name, "%x,%x", ata_drvp->ad_targ, 861 ata_drvp->ad_lun); 862 ddi_set_name_addr(cdip, name); 863 ddi_set_driver_private(cdip, devp); 864 865 return (DDI_SUCCESS); 866 } 867 868 case DDI_CTLOPS_UNINITCHILD: 869 { 870 dev_info_t *cdip = (dev_info_t *)a; 871 struct scsi_device *devp; 872 struct ctl_obj *ctlobjp; 873 gtgt_t *gtgtp; 874 875 devp = ddi_get_driver_private(cdip); 876 ctlobjp = (struct ctl_obj *)devp->sd_address.a_hba_tran; 877 gtgtp = ctlobjp->c_data; 878 879 ghd_target_free(d, cdip, >GTP2ATAP(gtgtp)->ac_ccc, gtgtp); 880 881 ddi_set_driver_private(cdip, NULL); 882 ddi_set_name_addr(cdip, NULL); 883 return (DDI_SUCCESS); 884 } 885 886 default: 887 return (DDI_FAILURE); 888 } 889 } 890 891 892 /* 893 * 894 * DADA abort entry point - not currently used by dadk 895 * 896 */ 897 898 /* ARGSUSED */ 899 static int 900 ata_disk_abort(opaque_t ctl_data, cmpkt_t *pktp) 901 { 902 ADBG_TRACE(("ata_disk_abort entered\n")); 903 904 /* XXX - Note that this interface is currently not used by dadk */ 905 906 /* 907 * GHD abort functions take a pointer to a scsi_address 908 * and so they're unusable here. The ata driver used to 909 * return DDI_SUCCESS here without doing anything. Its 910 * seems that DDI_FAILURE is more appropriate. 911 */ 912 913 return (DDI_FAILURE); 914 } 915 916 917 918 /* 919 * 920 * DADA reset entry point - not currently used by dadk 921 * (except in debug versions of driver) 922 * 923 */ 924 925 /* ARGSUSED */ 926 static int 927 ata_disk_reset(opaque_t ctl_data, int level) 928 { 929 gtgt_t *gtgtp = (gtgt_t *)ctl_data; 930 ata_drv_t *ata_drvp = GTGTP2ATADRVP(gtgtp); 931 int rc; 932 933 ADBG_TRACE(("ata_disk_reset entered\n")); 934 935 /* XXX - Note that this interface is currently not used by dadk */ 936 937 if (level == RESET_TARGET) { 938 rc = ghd_tran_reset_target(&ata_drvp->ad_ctlp->ac_ccc, gtgtp, 939 NULL); 940 } else if (level == RESET_ALL) { 941 rc = ghd_tran_reset_bus(&ata_drvp->ad_ctlp->ac_ccc, gtgtp, 942 NULL); 943 } 944 945 return (rc ? DDI_SUCCESS : DDI_FAILURE); 946 } 947 948 949 950 /* 951 * 952 * DADA ioctl entry point 953 * 954 */ 955 956 /* ARGSUSED */ 957 static int 958 ata_disk_ioctl(opaque_t ctl_data, int cmd, intptr_t arg, int flag) 959 { 960 gtgt_t *gtgtp = (gtgt_t *)ctl_data; 961 ata_ctl_t *ata_ctlp = GTGTP2ATAP(gtgtp); 962 ata_drv_t *ata_drvp = GTGTP2ATADRVP(gtgtp); 963 int rc, rc2; 964 struct tgdk_geom tgdk; 965 int wce; 966 struct ata_id *aidp = &ata_drvp->ad_id; 967 dk_updatefw_t updatefw; 968 #ifdef _MULTI_DATAMODEL 969 dk_updatefw_32_t updatefw32; 970 #endif 971 dk_disk_id_t dk_disk_id; 972 char buf[80]; 973 int i; 974 975 976 ADBG_TRACE(("ata_disk_ioctl entered, cmd = %d\n", cmd)); 977 978 switch (cmd) { 979 980 case DIOCTL_GETGEOM: 981 case DIOCTL_GETPHYGEOM: 982 tgdk.g_cyl = ata_drvp->ad_drvrcyl; 983 tgdk.g_head = ata_drvp->ad_drvrhd; 984 tgdk.g_sec = ata_drvp->ad_drvrsec; 985 tgdk.g_acyl = ata_drvp->ad_acyl; 986 tgdk.g_secsiz = 512; 987 tgdk.g_cap = tgdk.g_cyl * tgdk.g_head * tgdk.g_sec; 988 if (ddi_copyout(&tgdk, (caddr_t)arg, sizeof (tgdk), flag)) 989 return (EFAULT); 990 return (0); 991 992 case DCMD_UPDATE_GEOM: 993 /* ??? fix this to issue IDENTIFY DEVICE ??? */ 994 /* might not be necessary since I don't know of any ATA/IDE that */ 995 /* can change its geometry. On the other hand, ATAPI devices like the */ 996 /* LS-120 or PD/CD can change their geometry when new media is inserted */ 997 return (0); 998 999 /* copy the model number into the caller's buffer */ 1000 case DIOCTL_GETMODEL: 1001 rc = ata_copy_dk_ioc_string(arg, aidp->ai_model, 1002 sizeof (aidp->ai_model), flag); 1003 return (rc); 1004 1005 /* copy the serial number into the caller's buffer */ 1006 case DIOCTL_GETSERIAL: 1007 rc = ata_copy_dk_ioc_string(arg, aidp->ai_drvser, 1008 sizeof (aidp->ai_drvser), 1009 flag); 1010 return (rc); 1011 1012 case DIOCTL_GETWCE: 1013 /* 1014 * WCE is only supported in ATAPI-4 or higher, for 1015 * lower rev devices, must assume write cache is 1016 * enabled. 1017 * NOTE: Since there is currently no Solaris mechanism 1018 * to change the state of the Write Cache Enable feature, 1019 * this code just checks the value of the WCE bit 1020 * obtained at device init time. If a mechanism 1021 * is added to the driver to change WCE, this code 1022 * must be updated appropriately. 1023 */ 1024 wce = (aidp->ai_majorversion == 0xffff) || 1025 ((aidp->ai_majorversion & ATAC_MAJVER_4) == 0) || 1026 (aidp->ai_features85 & ATAC_FEATURES85_WCE) != 0; 1027 1028 if (ddi_copyout(&wce, (caddr_t)arg, sizeof (wce), flag) != 0) 1029 return (EFAULT); 1030 1031 return (0); 1032 1033 case DCMD_GET_STATE: 1034 rc = ata_queue_cmd(ata_disk_state, NULL, ata_ctlp, ata_drvp, 1035 gtgtp); 1036 break; 1037 1038 case DCMD_LOCK: 1039 case DKIOCLOCK: 1040 rc = ata_queue_cmd(ata_disk_lock, NULL, ata_ctlp, ata_drvp, 1041 gtgtp); 1042 break; 1043 1044 case DCMD_UNLOCK: 1045 case DKIOCUNLOCK: 1046 rc = ata_queue_cmd(ata_disk_unlock, NULL, ata_ctlp, ata_drvp, 1047 gtgtp); 1048 break; 1049 1050 case DCMD_START_MOTOR: 1051 case CDROMSTART: 1052 rc = ata_queue_cmd(ata_disk_recalibrate, NULL, ata_ctlp, 1053 ata_drvp, gtgtp); 1054 break; 1055 1056 case DCMD_STOP_MOTOR: 1057 case CDROMSTOP: 1058 rc = ata_queue_cmd(ata_disk_standby, NULL, ata_ctlp, ata_drvp, 1059 gtgtp); 1060 break; 1061 1062 case DKIOCEJECT: 1063 case CDROMEJECT: 1064 rc = ata_queue_cmd(ata_disk_eject, NULL, ata_ctlp, ata_drvp, 1065 gtgtp); 1066 break; 1067 1068 case DKIOC_UPDATEFW: 1069 1070 /* 1071 * Call DOWNLOAD MICROCODE command to update device 1072 * firmware. 1073 * 1074 * return value: 1075 * normal 0 Download microcode success 1076 * error EFAULT Bad address 1077 * ENXIO No such device or address 1078 * EINVAL Invalid argument 1079 * ENOMEM Not enough core 1080 * ENOTSUP Operation not supported 1081 * EIO I/O error 1082 * EPERM Not owner 1083 */ 1084 1085 /* 1086 * The following code deals with handling 32-bit request 1087 * in 64-bit kernel. 1088 */ 1089 #ifdef _MULTI_DATAMODEL 1090 if (ddi_model_convert_from(flag & FMODELS) == 1091 DDI_MODEL_ILP32) { 1092 if (ddi_copyin((void *)arg, &updatefw32, 1093 sizeof (dk_updatefw_32_t), flag)) 1094 return (EFAULT); 1095 1096 updatefw.dku_ptrbuf = 1097 (caddr_t)(uintptr_t)updatefw32.dku_ptrbuf; 1098 updatefw.dku_size = updatefw32.dku_size; 1099 updatefw.dku_type = updatefw32.dku_type; 1100 } else { 1101 if (ddi_copyin((void *)arg, &updatefw, 1102 sizeof (dk_updatefw_t), flag)) 1103 return (EFAULT); 1104 } 1105 #else 1106 if (ddi_copyin((void *)arg, &updatefw, 1107 sizeof (dk_updatefw_t), flag)) 1108 return (EFAULT); 1109 #endif 1110 rc = ata_disk_update_fw(gtgtp, ata_ctlp, ata_drvp, 1111 updatefw.dku_ptrbuf, updatefw.dku_size, 1112 updatefw.dku_type, flag); 1113 1114 /* 1115 * According to ATA8-ACS spec, the new microcode should 1116 * become effective immediately after the transfer of the 1117 * last data segment has completed, so here we will call 1118 * IDENTIFY DEVICE command immediately to update 1119 * ata_id content when success. 1120 */ 1121 if (rc == 0) { 1122 rc2 = ata_queue_cmd(ata_disk_id_update, NULL, 1123 ata_ctlp, ata_drvp, gtgtp); 1124 if (rc2 != TRUE) { 1125 return (ENXIO); 1126 } else { 1127 /* 1128 * Check whether the content of the IDENTIFY 1129 * DEVICE data is incomplete, if yes, it's 1130 * because the device supports the Power-up 1131 * in Standby feature set, and we will first 1132 * check word 2, and then decide whether need 1133 * to call set feature to spin-up the device, 1134 * and then call IDENTIFY DEVICE command again. 1135 */ 1136 aidp = &ata_drvp->ad_id; 1137 if (aidp->ai_config & ATA_ID_INCMPT) { 1138 if (aidp->ai_resv0 == 0x37c8 || 1139 aidp->ai_resv0 == 0x738c) { 1140 /* Spin-up the device */ 1141 (void) ata_queue_cmd( 1142 ata_disk_set_feature_spinup, 1143 NULL, 1144 ata_ctlp, 1145 ata_drvp, 1146 gtgtp); 1147 } 1148 1149 /* Try to update ata_id again */ 1150 rc2 = ata_queue_cmd( 1151 ata_disk_id_update, 1152 NULL, 1153 ata_ctlp, 1154 ata_drvp, 1155 gtgtp); 1156 if (rc2 != TRUE) { 1157 return (ENXIO); 1158 } else { 1159 aidp = &ata_drvp->ad_id; 1160 if (aidp->ai_config & 1161 ATA_ID_INCMPT) 1162 return (ENXIO); 1163 } 1164 } 1165 1166 /* 1167 * Dump the drive information. 1168 */ 1169 ATAPRT(("?\tUpdate firmware of %s device at " 1170 "targ %d, lun %d lastlun 0x%x\n", 1171 (ATAPIDRV(ata_drvp) ? "ATAPI":"IDE"), 1172 ata_drvp->ad_targ, ata_drvp->ad_lun, 1173 aidp->ai_lastlun)); 1174 1175 (void) strncpy(buf, aidp->ai_model, 1176 sizeof (aidp->ai_model)); 1177 buf[sizeof (aidp->ai_model)] = '\0'; 1178 for (i = sizeof (aidp->ai_model) - 1; 1179 buf[i] == ' '; i--) 1180 buf[i] = '\0'; 1181 ATAPRT(("?\tmodel %s\n", buf)); 1182 1183 (void) strncpy(buf, aidp->ai_fw, 1184 sizeof (aidp->ai_fw)); 1185 buf[sizeof (aidp->ai_fw)] = '\0'; 1186 for (i = sizeof (aidp->ai_fw) - 1; 1187 buf[i] == ' '; i--) 1188 buf[i] = '\0'; 1189 ATAPRT(("?\tfw %s\n", buf)); 1190 } 1191 } 1192 return (rc); 1193 1194 case DKIOC_GETDISKID: 1195 bzero(&dk_disk_id, sizeof (dk_disk_id_t)); 1196 1197 dk_disk_id.dkd_dtype = DKD_ATA_TYPE; 1198 1199 /* Get the model number */ 1200 (void) strncpy(dk_disk_id.disk_id.ata_disk_id.dkd_amodel, 1201 aidp->ai_model, sizeof (aidp->ai_model)); 1202 1203 /* Get the firmware revision */ 1204 (void) strncpy(dk_disk_id.disk_id.ata_disk_id.dkd_afwver, 1205 aidp->ai_fw, sizeof (aidp->ai_fw)); 1206 1207 /* Get the serial number */ 1208 (void) strncpy(dk_disk_id.disk_id.ata_disk_id.dkd_aserial, 1209 aidp->ai_drvser, sizeof (aidp->ai_drvser)); 1210 1211 if (ddi_copyout(&dk_disk_id, (void *)arg, 1212 sizeof (dk_disk_id_t), flag)) 1213 return (EFAULT); 1214 else 1215 return (0); 1216 1217 default: 1218 ADBG_WARN(("ata_disk_ioctl: unsupported cmd 0x%x\n", cmd)); 1219 return (ENOTTY); 1220 } 1221 1222 if (rc) 1223 return (0); 1224 return (ENXIO); 1225 1226 } 1227 1228 1229 #ifdef ___not___used___ 1230 /* 1231 * Issue an ATA command to the drive using the packet already 1232 * allocated by the target driver 1233 */ 1234 1235 int 1236 ata_disk_do_ioctl( 1237 int (*func)(ata_ctl_t *, ata_drv_t *, ata_pkt_t *), 1238 void *arg, 1239 ata_ctl_t *ata_ctlp, 1240 gtgt_t *gtgtp, 1241 cmpkt_t *pktp) 1242 { 1243 gcmd_t *gcmdp = CPKT2GCMD(pktp); 1244 ata_pkt_t *ata_pktp = GCMD2APKT(gcmdp); 1245 int rc; 1246 1247 ata_pktp->ap_start = func; 1248 ata_pktp->ap_intr = NULL; 1249 ata_pktp->ap_complete = NULL; 1250 ata_pktp->ap_v_addr = (caddr_t)arg; 1251 1252 /* 1253 * add it to the queue, when it gets to the front the 1254 * ap_start function is called. 1255 */ 1256 rc = ghd_transport(&ata_ctlp->ac_ccc, gcmdp, gcmdp->cmd_gtgtp, 1257 0, TRUE, NULL); 1258 1259 if (rc != TRAN_ACCEPT) { 1260 /* this should never, ever happen */ 1261 return (ENXIO); 1262 } 1263 1264 if (ata_pktp->ap_flags & AP_ERROR) 1265 return (ENXIO); 1266 return (0); 1267 } 1268 #endif 1269 1270 1271 1272 /* 1273 * 1274 * DADA pktalloc entry point 1275 * 1276 */ 1277 1278 /* ARGSUSED */ 1279 static cmpkt_t * 1280 ata_disk_pktalloc(opaque_t ctl_data, int (*callback)(caddr_t), caddr_t arg) 1281 { 1282 gtgt_t *gtgtp = (gtgt_t *)ctl_data; 1283 ata_drv_t *ata_drvp = GTGTP2ATADRVP(gtgtp); 1284 cmpkt_t *pktp; 1285 ata_pkt_t *ata_pktp; 1286 gcmd_t *gcmdp; 1287 1288 ADBG_TRACE(("ata_disk_pktalloc entered\n")); 1289 1290 /* 1291 * Allocate and init the GHD gcmd_t structure and the 1292 * DADA cmpkt and the ata_pkt 1293 */ 1294 if ((gcmdp = ghd_gcmd_alloc(gtgtp, 1295 (sizeof (cmpkt_t) + sizeof (ata_pkt_t)), 1296 (callback == DDI_DMA_SLEEP))) == NULL) { 1297 return ((cmpkt_t *)NULL); 1298 } 1299 ASSERT(gcmdp != NULL); 1300 1301 ata_pktp = GCMD2APKT(gcmdp); 1302 ASSERT(ata_pktp != NULL); 1303 1304 pktp = (cmpkt_t *)(ata_pktp + 1); 1305 1306 pktp->cp_ctl_private = (void *)gcmdp; 1307 ata_pktp->ap_gcmdp = gcmdp; 1308 gcmdp->cmd_pktp = (void *)pktp; 1309 1310 /* 1311 * At this point the structures are linked like this: 1312 * 1313 * (struct cmpkt) <--> (struct gcmd) <--> (struct ata_pkt) 1314 */ 1315 1316 /* callback functions */ 1317 1318 ata_pktp->ap_start = ata_disk_start; 1319 ata_pktp->ap_intr = ata_disk_intr; 1320 ata_pktp->ap_complete = ata_disk_complete; 1321 1322 /* other ata_pkt setup */ 1323 1324 ata_pktp->ap_bytes_per_block = ata_drvp->ad_bytes_per_block; 1325 1326 /* cmpkt setup */ 1327 1328 pktp->cp_cdblen = 1; 1329 pktp->cp_cdbp = (opaque_t)&ata_pktp->ap_cdb; 1330 pktp->cp_scbp = (opaque_t)&ata_pktp->ap_scb; 1331 pktp->cp_scblen = 1; 1332 1333 return (pktp); 1334 } 1335 1336 1337 1338 /* 1339 * 1340 * DADA pktfree entry point 1341 * 1342 */ 1343 1344 /* ARGSUSED */ 1345 static void 1346 ata_disk_pktfree(opaque_t ctl_data, cmpkt_t *pktp) 1347 { 1348 ata_pkt_t *ata_pktp = CPKT2APKT(pktp); 1349 1350 ADBG_TRACE(("ata_disk_pktfree entered\n")); 1351 1352 /* check not free already */ 1353 1354 ASSERT(!(ata_pktp->ap_flags & AP_FREE)); 1355 ata_pktp->ap_flags = AP_FREE; 1356 1357 ghd_gcmd_free(CPKT2GCMD(pktp)); 1358 } 1359 1360 1361 /* 1362 * 1363 * DADA memsetup entry point 1364 * 1365 */ 1366 1367 /* ARGSUSED */ 1368 static cmpkt_t * 1369 ata_disk_memsetup( 1370 opaque_t ctl_data, 1371 cmpkt_t *pktp, 1372 struct buf *bp, 1373 int (*callback)(caddr_t), 1374 caddr_t arg) 1375 { 1376 gtgt_t *gtgtp = (gtgt_t *)ctl_data; 1377 ata_pkt_t *ata_pktp = CPKT2APKT(pktp); 1378 gcmd_t *gcmdp = APKT2GCMD(ata_pktp); 1379 int flags; 1380 1381 ADBG_TRACE(("ata_disk_memsetup entered\n")); 1382 1383 ata_pktp->ap_sg_cnt = 0; 1384 1385 if (bp->b_bcount == 0) { 1386 ata_pktp->ap_v_addr = NULL; 1387 return (pktp); 1388 } 1389 1390 if (GTGTP2ATADRVP(gtgtp)->ad_pciide_dma != ATA_DMA_ON) 1391 goto skip_dma_setup; 1392 1393 if (ata_dma_disabled) 1394 goto skip_dma_setup; 1395 1396 /* 1397 * The PCI-IDE DMA engine is brain-damaged and can't 1398 * DMA non-aligned buffers. 1399 */ 1400 if (!(bp->b_flags & B_PAGEIO) && 1401 ((uintptr_t)bp->b_un.b_addr) & PCIIDE_PRDE_ADDR_MASK) { 1402 goto skip_dma_setup; 1403 } 1404 1405 /* 1406 * It also insists that the byte count must be even. 1407 */ 1408 if (bp->b_bcount & 1) 1409 goto skip_dma_setup; 1410 1411 /* check direction for data transfer */ 1412 if (bp->b_flags & B_READ) { 1413 flags = DDI_DMA_READ | DDI_DMA_PARTIAL; 1414 } else { 1415 flags = DDI_DMA_WRITE | DDI_DMA_PARTIAL; 1416 } 1417 1418 /* 1419 * Bind the DMA handle to the buf 1420 */ 1421 if (ghd_dma_buf_bind_attr(>GTP2ATAP(gtgtp)->ac_ccc, gcmdp, bp, flags, 1422 callback, arg, >GTP2ATATGTP(gtgtp)->at_dma_attr)) { 1423 ata_pktp->ap_v_addr = 0; 1424 return (pktp); 1425 } 1426 1427 skip_dma_setup: 1428 bp_mapin(bp); 1429 ata_pktp->ap_v_addr = bp->b_un.b_addr; 1430 return (pktp); 1431 } 1432 1433 1434 1435 /* 1436 * 1437 * DADA memfree entry point 1438 * 1439 */ 1440 1441 /* 1442 * 1157317 sez that drivers shouldn't call bp_mapout(), as either 1443 * biodone() or biowait() will end up doing it, but after they 1444 * call bp->b_iodone(), which is a necessary sequence for 1445 * Online Disk Suite. However, the DDI group wants to rethink 1446 * bp_mapin()/bp_mapout() and how they should behave in the 1447 * presence of layered drivers, etc. For the moment, fix 1448 * the OLDS problem by removing the bp_mapout() call. 1449 */ 1450 1451 #define BUG_1157317 1452 1453 /* ARGSUSED */ 1454 static void 1455 ata_disk_memfree(opaque_t ctl_data, cmpkt_t *pktp) 1456 { 1457 gcmd_t *gcmdp = CPKT2GCMD(pktp); 1458 1459 ADBG_TRACE(("ata_disk_memfree entered\n")); 1460 1461 if (gcmdp->cmd_dma_handle) 1462 ghd_dmafree_attr(gcmdp); 1463 #if !defined(BUG_1157317) 1464 else 1465 bp_mapout(pktp->cp_bp); 1466 #endif 1467 } 1468 1469 1470 1471 /* 1472 * 1473 * DADA iosetup entry point 1474 * 1475 */ 1476 1477 static cmpkt_t * 1478 ata_disk_iosetup(opaque_t ctl_data, cmpkt_t *pktp) 1479 { 1480 gtgt_t *gtgtp = (gtgt_t *)ctl_data; 1481 ata_drv_t *ata_drvp = GTGTP2ATADRVP(gtgtp); 1482 ata_pkt_t *ata_pktp = CPKT2APKT(pktp); 1483 gcmd_t *gcmdp = APKT2GCMD(ata_pktp); 1484 uint_t sec_count; 1485 daddr_t start_sec; 1486 uint_t byte_count; 1487 1488 ADBG_TRACE(("ata_disk_iosetup entered\n")); 1489 1490 /* 1491 * Check for DCMD_FLUSH_CACHE (which does no I/O) and 1492 * just do basic setup. 1493 */ 1494 if (pktp->cp_passthru == NULL && 1495 ata_pktp->ap_cdb == DCMD_FLUSH_CACHE) { 1496 ata_pktp->ap_cmd = ATC_FLUSH_CACHE; 1497 ata_pktp->ap_flags = 0; 1498 ata_pktp->ap_count = 0; 1499 ata_pktp->ap_startsec = 0; 1500 ata_pktp->ap_sg_cnt = 0; 1501 ata_pktp->ap_pciide_dma = FALSE; 1502 return (pktp); 1503 } 1504 1505 /* check for error retry */ 1506 if (ata_pktp->ap_flags & AP_ERROR) { 1507 /* 1508 * this is a temporary work-around for dadk calling 1509 * iosetup for retry. The correct 1510 * solution is changing dadk to not to call iosetup 1511 * for a retry. 1512 * We do not apply the work-around for pio mode since 1513 * that does not involve moving dma windows and reducing the 1514 * sector count would work for pio mode on a retry 1515 * for now. 1516 */ 1517 if (gcmdp->cmd_dma_handle != NULL) { 1518 ata_pktp->ap_flags = 0; 1519 return (NULL); 1520 } 1521 1522 ata_pktp->ap_bytes_per_block = NBPSCTR; 1523 sec_count = 1; 1524 1525 /* 1526 * Since we are retrying the last read or write operation, 1527 * restore the old values of the ap_v_addr and ap_resid. 1528 * This assumes CTL_IOSETUP is called again on retry; if not, 1529 * this needs to be done in CTL_TRANSPORT. 1530 */ 1531 if (ata_pktp->ap_flags & (AP_READ | AP_WRITE)) { 1532 ata_pktp->ap_v_addr = ata_pktp->ap_v_addr_sav; 1533 ata_pktp->ap_resid = ata_pktp->ap_resid_sav; 1534 } 1535 } else { 1536 /* 1537 * Limit request to ac_max_transfer sectors. 1538 * The value is specified by the user in the 1539 * max_transfer property. It must be in the range 1 to 256. 1540 * When max_transfer is 0x100 it is bigger than 8 bits. 1541 * The spec says 0 represents 256 so it should be OK. 1542 */ 1543 sec_count = min((pktp->cp_bytexfer >> SCTRSHFT), 1544 ata_drvp->ad_ctlp->ac_max_transfer); 1545 /* 1546 * Save the current values of ap_v_addr and ap_resid 1547 * in case a retry operation happens. During a retry 1548 * operation we need to restore these values. 1549 */ 1550 ata_pktp->ap_v_addr_sav = ata_pktp->ap_v_addr; 1551 ata_pktp->ap_resid_sav = ata_pktp->ap_resid; 1552 } 1553 1554 /* reset flags */ 1555 ata_pktp->ap_flags = 0; 1556 1557 #ifdef DADKIO_RWCMD_READ 1558 start_sec = pktp->cp_passthru ? RWCMDP(pktp)->blkaddr : pktp->cp_srtsec; 1559 #else 1560 start_sec = pktp->cp_srtsec; 1561 #endif 1562 1563 /* 1564 * Setup the PCIDE Bus Master Scatter/Gather list 1565 */ 1566 ata_pktp->ap_sg_cnt = 0; 1567 ata_pktp->ap_pciide_dma = FALSE; 1568 if (gcmdp->cmd_dma_handle != NULL && sec_count != 0) { 1569 byte_count = sec_count << SCTRSHFT; 1570 if ((ghd_dmaget_attr(>GTP2ATAP(gtgtp)->ac_ccc, gcmdp, 1571 byte_count, ATA_DMA_NSEGS, &byte_count) == FALSE) || 1572 (byte_count == 0)) { 1573 ADBG_ERROR(("ata_disk_iosetup: byte count zero\n")); 1574 return (NULL); 1575 } 1576 sec_count = byte_count >> SCTRSHFT; 1577 } 1578 1579 /* 1580 * In the non-48-bit mode addressing (CHS and LBA28) the sector 1581 * count is a 8-bit value and the sector count 0 represents 256 1582 * sectors. 1583 * In the extended addressing (LBA48) the sector count is a 16-bit 1584 * value, so max_transfer 0x100 cannot be truncated to 8-bits 1585 * because this would represent a zero sector count. 1586 */ 1587 ata_pktp->ap_count = sec_count; 1588 if (!(ata_drvp->ad_flags & AD_EXT48)) { 1589 ata_pktp->ap_count &= 0xff; 1590 } 1591 ata_pktp->ap_startsec = start_sec; 1592 1593 #ifdef DADKIO_RWCMD_READ 1594 if (pktp->cp_passthru) { 1595 switch (RWCMDP(pktp)->cmd) { 1596 case DADKIO_RWCMD_READ: 1597 if (ata_pktp->ap_sg_cnt) { 1598 ata_pktp->ap_cmd = ATC_READ_DMA; 1599 ata_pktp->ap_pciide_dma = TRUE; 1600 ata_pktp->ap_start = ata_disk_start_dma_in; 1601 ata_pktp->ap_intr = ata_disk_intr_dma; 1602 } else { 1603 ata_pktp->ap_cmd = ATC_RDSEC; 1604 ata_pktp->ap_start = ata_disk_start_pio_in; 1605 ata_pktp->ap_intr = ata_disk_intr_pio_in; 1606 } 1607 ata_pktp->ap_flags |= AP_READ; 1608 break; 1609 case DADKIO_RWCMD_WRITE: 1610 if (ata_pktp->ap_sg_cnt) { 1611 ata_pktp->ap_cmd = ATC_WRITE_DMA; 1612 ata_pktp->ap_pciide_dma = TRUE; 1613 ata_pktp->ap_start = ata_disk_start_dma_out; 1614 ata_pktp->ap_intr = ata_disk_intr_dma; 1615 } else { 1616 ata_pktp->ap_cmd = ATC_WRSEC; 1617 ata_pktp->ap_start = ata_disk_start_pio_out; 1618 ata_pktp->ap_intr = ata_disk_intr_pio_out; 1619 } 1620 ata_pktp->ap_flags |= AP_WRITE; 1621 break; 1622 } 1623 1624 byte_count = RWCMDP(pktp)->buflen; 1625 pktp->cp_bytexfer = byte_count; 1626 pktp->cp_resid = byte_count; 1627 ata_pktp->ap_resid = byte_count; 1628 1629 /* 1630 * since we're not using READ/WRITE MULTIPLE, we 1631 * should set bytes_per_block to one sector 1632 * XXX- why wasn't this in the old driver?? 1633 */ 1634 ata_pktp->ap_bytes_per_block = NBPSCTR; 1635 } else 1636 #endif 1637 { 1638 byte_count = sec_count << SCTRSHFT; 1639 pktp->cp_bytexfer = byte_count; 1640 pktp->cp_resid = byte_count; 1641 ata_pktp->ap_resid = byte_count; 1642 1643 /* setup the task file registers */ 1644 1645 switch (ata_pktp->ap_cdb) { 1646 case DCMD_READ: 1647 if (ata_pktp->ap_sg_cnt) { 1648 ata_pktp->ap_cmd = ATC_READ_DMA; 1649 ata_pktp->ap_pciide_dma = TRUE; 1650 ata_pktp->ap_start = ata_disk_start_dma_in; 1651 ata_pktp->ap_intr = ata_disk_intr_dma; 1652 } else { 1653 ata_pktp->ap_cmd = ata_drvp->ad_rd_cmd; 1654 ata_pktp->ap_start = ata_disk_start_pio_in; 1655 ata_pktp->ap_intr = ata_disk_intr_pio_in; 1656 } 1657 ata_pktp->ap_flags |= AP_READ; 1658 break; 1659 1660 case DCMD_WRITE: 1661 if (ata_pktp->ap_sg_cnt) { 1662 ata_pktp->ap_cmd = ATC_WRITE_DMA; 1663 ata_pktp->ap_pciide_dma = TRUE; 1664 ata_pktp->ap_start = ata_disk_start_dma_out; 1665 ata_pktp->ap_intr = ata_disk_intr_dma; 1666 } else { 1667 ata_pktp->ap_cmd = ata_drvp->ad_wr_cmd; 1668 ata_pktp->ap_start = ata_disk_start_pio_out; 1669 ata_pktp->ap_intr = ata_disk_intr_pio_out; 1670 } 1671 ata_pktp->ap_flags |= AP_WRITE; 1672 break; 1673 1674 default: 1675 ADBG_WARN(("ata_disk_iosetup: unknown command 0x%x\n", 1676 ata_pktp->ap_cdb)); 1677 pktp = NULL; 1678 break; 1679 } 1680 } 1681 1682 /* If 48-bit mode is used, convert command to 48-bit mode cmd */ 1683 if (pktp != NULL && ata_drvp->ad_flags & AD_EXT48) { 1684 switch (ata_pktp->ap_cmd) { 1685 case ATC_RDSEC: 1686 ata_pktp->ap_cmd = ATC_RDSEC_EXT; 1687 break; 1688 case ATC_WRSEC: 1689 ata_pktp->ap_cmd = ATC_WRSEC_EXT; 1690 break; 1691 case ATC_RDMULT: 1692 ata_pktp->ap_cmd = ATC_RDMULT_EXT; 1693 break; 1694 case ATC_WRMULT: 1695 ata_pktp->ap_cmd = ATC_WRMULT_EXT; 1696 break; 1697 case ATC_READ_DMA: 1698 ata_pktp->ap_cmd = ATC_RDDMA_EXT; 1699 break; 1700 case ATC_WRITE_DMA: 1701 ata_pktp->ap_cmd = ATC_WRDMA_EXT; 1702 break; 1703 } 1704 } 1705 1706 return (pktp); 1707 } 1708 1709 1710 1711 /* 1712 * 1713 * DADA transport entry point 1714 * 1715 */ 1716 1717 static int 1718 ata_disk_transport(opaque_t ctl_data, cmpkt_t *pktp) 1719 { 1720 gtgt_t *gtgtp = (gtgt_t *)ctl_data; 1721 ata_drv_t *ata_drvp = GTGTP2ATADRVP(gtgtp); 1722 ata_ctl_t *ata_ctlp = ata_drvp->ad_ctlp; 1723 ata_pkt_t *ata_pktp = CPKT2APKT(pktp); 1724 int rc; 1725 int polled = FALSE; 1726 1727 ADBG_TRACE(("ata_disk_transport entered\n")); 1728 1729 /* check for polling pkt */ 1730 1731 if (pktp->cp_flags & CPF_NOINTR) { 1732 polled = TRUE; 1733 } 1734 1735 /* call ghd transport routine */ 1736 1737 rc = ghd_transport(&ata_ctlp->ac_ccc, APKT2GCMD(ata_pktp), 1738 gtgtp, pktp->cp_time, polled, NULL); 1739 1740 /* see if pkt was not accepted */ 1741 1742 if (rc == TRAN_BUSY) 1743 return (CTL_SEND_BUSY); 1744 1745 if (rc == TRAN_ACCEPT) 1746 return (CTL_SEND_SUCCESS); 1747 1748 return (CTL_SEND_FAILURE); 1749 } 1750 1751 1752 /* 1753 * 1754 * routines to load the cylinder/head/sector/count 1755 * task file registers. 1756 * 1757 */ 1758 static void 1759 ata_disk_load_regs_lba28(ata_pkt_t *ata_pktp, ata_drv_t *ata_drvp) 1760 { 1761 ata_ctl_t *ata_ctlp = ata_drvp->ad_ctlp; 1762 ddi_acc_handle_t io_hdl1 = ata_ctlp->ac_iohandle1; 1763 uint_t lba; /* LBA of first sector */ 1764 1765 lba = ata_pktp->ap_startsec; 1766 1767 ddi_put8(io_hdl1, ata_ctlp->ac_count, 1768 ata_pktp->ap_count); 1769 ddi_put8(io_hdl1, ata_ctlp->ac_sect, lba); 1770 lba >>= 8; 1771 ddi_put8(io_hdl1, ata_ctlp->ac_lcyl, lba); 1772 lba >>= 8; 1773 ddi_put8(io_hdl1, ata_ctlp->ac_hcyl, lba); 1774 lba >>= 8; 1775 /* 1776 * dev/head register can use only 4 bits 1777 * must also include drive selector. 1778 */ 1779 lba = (lba & 0xf) | ata_drvp->ad_drive_bits; 1780 ddi_put8(io_hdl1, ata_ctlp->ac_drvhd, lba); 1781 } 1782 1783 /* 1784 * In 48-bit extended mode, the sector count is 16 bits wide, and the 1785 * LBA is 48 bits wide, as follows: 1786 * register most recent previous 1787 * name value value 1788 * -------- ---------- --------- 1789 * sector cnt count(7:0) count(15:8) 1790 * sector num lba(7:0) lba(31:24) 1791 * cyl low lba(15:8) lba(39:32) 1792 * cyl hi lba(23:16) lba(47:40) 1793 * device/head 111D0000 N/A 1794 * ^ ^ 1795 * | | 1796 * | +-- drive number 1797 * | 1798 * +-- indicates LBA 1799 * The other two 1 bits are historical and are not used in 48bit 1800 * extended mode. 1801 */ 1802 /* 1803 * WARNING: 1804 * dada framework passes starting sector as daddr_t type, thus 1805 * limiting reachable disk space in 32-bit x86 architecture to 1 terabyte. 1806 * Therefore high 16 bits of the 48-bits address can be and 1807 * are currently ignored. 1808 */ 1809 static void 1810 ata_disk_load_regs_lba48(ata_pkt_t *ata_pktp, ata_drv_t *ata_drvp) 1811 { 1812 ata_ctl_t *ata_ctlp = ata_drvp->ad_ctlp; 1813 ddi_acc_handle_t io_hdl1 = ata_ctlp->ac_iohandle1; 1814 uint16_t seccnt; /* 16-bit sector count */ 1815 uint_t lbalow; /* low-order 24 bits of LBA */ 1816 uint_t lbahi; /* high-order 24 bits of LBA */ 1817 1818 seccnt = ata_pktp->ap_count; 1819 /* high-order 8 bits of lbalow never get used */ 1820 lbalow = ata_pktp->ap_startsec; 1821 lbahi = ata_pktp->ap_startsec >> 24; 1822 1823 ddi_put8(io_hdl1, ata_ctlp->ac_count, seccnt >> 8); 1824 ddi_put8(io_hdl1, ata_ctlp->ac_count, seccnt); 1825 /* Send the high-order half first */ 1826 ddi_put8(io_hdl1, ata_ctlp->ac_sect, lbahi); 1827 lbahi >>= 8; 1828 ddi_put8(io_hdl1, ata_ctlp->ac_lcyl, lbahi); 1829 lbahi >>= 8; 1830 ddi_put8(io_hdl1, ata_ctlp->ac_hcyl, lbahi); 1831 /* Send the low-order half */ 1832 ddi_put8(io_hdl1, ata_ctlp->ac_sect, lbalow); 1833 lbalow >>= 8; 1834 ddi_put8(io_hdl1, ata_ctlp->ac_lcyl, lbalow); 1835 lbalow >>= 8; 1836 ddi_put8(io_hdl1, ata_ctlp->ac_hcyl, lbalow); 1837 ddi_put8(io_hdl1, ata_ctlp->ac_drvhd, 1838 ata_drvp->ad_drive_bits); 1839 } 1840 1841 static void 1842 ata_disk_load_regs_chs(ata_pkt_t *ata_pktp, ata_drv_t *ata_drvp) 1843 { 1844 ata_ctl_t *ata_ctlp = ata_drvp->ad_ctlp; 1845 ddi_acc_handle_t io_hdl1 = ata_ctlp->ac_iohandle1; 1846 uint_t resid; 1847 uint_t cyl; 1848 uchar_t head; 1849 uchar_t drvheads; 1850 uchar_t drvsectors; 1851 1852 drvheads = ata_drvp->ad_phhd; 1853 drvsectors = ata_drvp->ad_phsec; 1854 1855 resid = ata_pktp->ap_startsec / drvsectors; 1856 head = (resid % drvheads) & 0xf; 1857 cyl = resid / drvheads; 1858 /* automatically truncate to char */ 1859 ddi_put8(io_hdl1, ata_ctlp->ac_sect, 1860 (ata_pktp->ap_startsec % drvsectors) + 1); 1861 ddi_put8(io_hdl1, ata_ctlp->ac_count, ata_pktp->ap_count); 1862 ddi_put8(io_hdl1, ata_ctlp->ac_hcyl, (cyl >> 8)); 1863 /* lcyl gets truncated to 8 bits */ 1864 ddi_put8(io_hdl1, ata_ctlp->ac_lcyl, cyl); 1865 ddi_put8(io_hdl1, ata_ctlp->ac_drvhd, 1866 ata_drvp->ad_drive_bits | head); 1867 } 1868 1869 1870 /* 1871 * 1872 * packet start callback routines 1873 * 1874 */ 1875 1876 /* ARGSUSED */ 1877 static int 1878 ata_disk_start_common( 1879 ata_ctl_t *ata_ctlp, 1880 ata_drv_t *ata_drvp, 1881 ata_pkt_t *ata_pktp) 1882 { 1883 ddi_acc_handle_t io_hdl1 = ata_ctlp->ac_iohandle1; 1884 ddi_acc_handle_t io_hdl2 = ata_ctlp->ac_iohandle2; 1885 1886 ADBG_TRACE(("ata_disk_start_common entered\n")); 1887 1888 ADBG_TRANSPORT(("ata_disk_start:\tpkt = 0x%p, pkt flags = 0x%x\n", 1889 ata_pktp, ata_pktp->ap_flags)); 1890 ADBG_TRANSPORT(("\tcommand=0x%x, sect=0x%lx\n", 1891 ata_pktp->ap_cmd, ata_pktp->ap_startsec)); 1892 ADBG_TRANSPORT(("\tcount=0x%x, drvhd = 0x%x\n", 1893 ata_pktp->ap_count, ata_drvp->ad_drive_bits)); 1894 1895 /* 1896 * If AC_BSY_WAIT is set, wait for controller to not be busy, 1897 * before issuing a command. If AC_BSY_WAIT is not set, 1898 * skip the wait. This is important for laptops that do 1899 * suspend/resume but do not correctly wait for the busy bit to 1900 * drop after a resume. 1901 * 1902 * NOTE: this test for ATS_BSY is also needed if/when we 1903 * implement the overlapped/queued command protocols. Currently, 1904 * the overlap/queued feature is not supported so the test is 1905 * conditional. 1906 */ 1907 if (ata_ctlp->ac_timing_flags & AC_BSY_WAIT) { 1908 if (!ata_wait(io_hdl2, ata_ctlp->ac_ioaddr2, 1909 0, ATS_BSY, 5000000)) { 1910 ADBG_ERROR(("ata_disk_start: BUSY\n")); 1911 return (FALSE); 1912 } 1913 } 1914 1915 ddi_put8(io_hdl1, ata_ctlp->ac_drvhd, ata_drvp->ad_drive_bits); 1916 ata_nsecwait(400); 1917 1918 /* 1919 * make certain the drive selected 1920 */ 1921 if (!ata_wait(io_hdl2, ata_ctlp->ac_ioaddr2, 1922 ATS_DRDY, ATS_BSY, 5 * 1000000)) { 1923 ADBG_ERROR(("ata_disk_start: select failed\n")); 1924 return (FALSE); 1925 } 1926 1927 if (ata_pktp->ap_cmd == ATC_LOAD_FW) { 1928 1929 /* the sector count is 16 bits wide */ 1930 ddi_put8(io_hdl1, ata_ctlp->ac_count, ata_pktp->ap_count); 1931 ddi_put8(io_hdl1, ata_ctlp->ac_sect, ata_pktp->ap_count >> 8); 1932 ddi_put8(io_hdl1, ata_ctlp->ac_lcyl, ata_pktp->ap_startsec); 1933 ddi_put8(io_hdl1, ata_ctlp->ac_hcyl, 1934 ata_pktp->ap_startsec >> 8); 1935 1936 /* put subcommand for DOWNLOAD MICROCODE */ 1937 ddi_put8(io_hdl1, ata_ctlp->ac_feature, ata_pktp->ap_bcount); 1938 } else { 1939 1940 /* 1941 * We use different methods for loading the task file 1942 * registers, depending on whether the disk 1943 * uses LBA or CHS addressing and whether 48-bit 1944 * extended addressing is to be used. 1945 */ 1946 if (!(ata_drvp->ad_drive_bits & ATDH_LBA)) 1947 ata_disk_load_regs_chs(ata_pktp, ata_drvp); 1948 else if (ata_drvp->ad_flags & AD_EXT48) 1949 ata_disk_load_regs_lba48(ata_pktp, ata_drvp); 1950 else 1951 ata_disk_load_regs_lba28(ata_pktp, ata_drvp); 1952 ddi_put8(io_hdl1, ata_ctlp->ac_feature, 0); 1953 } 1954 1955 /* 1956 * Always make certain interrupts are enabled. It's been reported 1957 * (but not confirmed) that some notebook computers don't 1958 * clear the interrupt disable bit after being resumed. The 1959 * easiest way to fix this is to always clear the disable bit 1960 * before every command. 1961 */ 1962 ddi_put8(io_hdl2, ata_ctlp->ac_devctl, ATDC_D3); 1963 return (TRUE); 1964 } 1965 1966 1967 /* 1968 * 1969 * Start a non-data ATA command (not DMA and not PIO): 1970 * 1971 */ 1972 1973 static int 1974 ata_disk_start( 1975 ata_ctl_t *ata_ctlp, 1976 ata_drv_t *ata_drvp, 1977 ata_pkt_t *ata_pktp) 1978 { 1979 ddi_acc_handle_t io_hdl1 = ata_ctlp->ac_iohandle1; 1980 int rc; 1981 1982 rc = ata_disk_start_common(ata_ctlp, ata_drvp, ata_pktp); 1983 1984 if (!rc) 1985 return (ATA_FSM_RC_BUSY); 1986 1987 /* 1988 * This next one sets the controller in motion 1989 */ 1990 ddi_put8(io_hdl1, ata_ctlp->ac_cmd, ata_pktp->ap_cmd); 1991 1992 /* wait for the busy bit to settle */ 1993 ata_nsecwait(400); 1994 1995 return (ATA_FSM_RC_OKAY); 1996 } 1997 1998 1999 2000 static int 2001 ata_disk_start_dma_in( 2002 ata_ctl_t *ata_ctlp, 2003 ata_drv_t *ata_drvp, 2004 ata_pkt_t *ata_pktp) 2005 { 2006 ddi_acc_handle_t io_hdl1 = ata_ctlp->ac_iohandle1; 2007 int rc; 2008 2009 rc = ata_disk_start_common(ata_ctlp, ata_drvp, ata_pktp); 2010 2011 if (!rc) 2012 return (ATA_FSM_RC_BUSY); 2013 2014 /* 2015 * Copy the Scatter/Gather list to the controller's 2016 * Physical Region Descriptor Table 2017 */ 2018 ata_pciide_dma_setup(ata_ctlp, ata_pktp->ap_sg_list, 2019 ata_pktp->ap_sg_cnt); 2020 2021 /* 2022 * reset the PCIIDE Controller's interrupt and error status bits 2023 */ 2024 (void) ata_pciide_status_clear(ata_ctlp); 2025 2026 /* 2027 * This next one sets the drive in motion 2028 */ 2029 ddi_put8(io_hdl1, ata_ctlp->ac_cmd, ata_pktp->ap_cmd); 2030 2031 /* wait for the drive's busy bit to settle */ 2032 ata_nsecwait(400); 2033 2034 ata_pciide_dma_start(ata_ctlp, PCIIDE_BMICX_RWCON_WRITE_TO_MEMORY); 2035 2036 return (ATA_FSM_RC_OKAY); 2037 } 2038 2039 2040 2041 static int 2042 ata_disk_start_dma_out( 2043 ata_ctl_t *ata_ctlp, 2044 ata_drv_t *ata_drvp, 2045 ata_pkt_t *ata_pktp) 2046 { 2047 ddi_acc_handle_t io_hdl1 = ata_ctlp->ac_iohandle1; 2048 int rc; 2049 2050 rc = ata_disk_start_common(ata_ctlp, ata_drvp, ata_pktp); 2051 2052 if (!rc) 2053 return (ATA_FSM_RC_BUSY); 2054 2055 /* 2056 * Copy the Scatter/Gather list to the controller's 2057 * Physical Region Descriptor Table 2058 */ 2059 ata_pciide_dma_setup(ata_ctlp, ata_pktp->ap_sg_list, 2060 ata_pktp->ap_sg_cnt); 2061 2062 /* 2063 * reset the PCIIDE Controller's interrupt and error status bits 2064 */ 2065 (void) ata_pciide_status_clear(ata_ctlp); 2066 2067 /* 2068 * This next one sets the drive in motion 2069 */ 2070 ddi_put8(io_hdl1, ata_ctlp->ac_cmd, ata_pktp->ap_cmd); 2071 2072 /* wait for the drive's busy bit to settle */ 2073 ata_nsecwait(400); 2074 2075 ata_pciide_dma_start(ata_ctlp, PCIIDE_BMICX_RWCON_READ_FROM_MEMORY); 2076 2077 return (ATA_FSM_RC_OKAY); 2078 } 2079 2080 2081 2082 2083 2084 /* 2085 * 2086 * Start a PIO data-in ATA command: 2087 * 2088 */ 2089 2090 static int 2091 ata_disk_start_pio_in( 2092 ata_ctl_t *ata_ctlp, 2093 ata_drv_t *ata_drvp, 2094 ata_pkt_t *ata_pktp) 2095 { 2096 ddi_acc_handle_t io_hdl1 = ata_ctlp->ac_iohandle1; 2097 int rc; 2098 2099 rc = ata_disk_start_common(ata_ctlp, ata_drvp, ata_pktp); 2100 2101 if (!rc) 2102 return (ATA_FSM_RC_BUSY); 2103 /* 2104 * This next one sets the controller in motion 2105 */ 2106 ddi_put8(io_hdl1, ata_ctlp->ac_cmd, ata_pktp->ap_cmd); 2107 2108 /* wait for the busy bit to settle */ 2109 ata_nsecwait(400); 2110 2111 return (ATA_FSM_RC_OKAY); 2112 } 2113 2114 2115 2116 2117 /* 2118 * 2119 * Start a PIO data-out ATA command: 2120 * 2121 */ 2122 2123 static int 2124 ata_disk_start_pio_out( 2125 ata_ctl_t *ata_ctlp, 2126 ata_drv_t *ata_drvp, 2127 ata_pkt_t *ata_pktp) 2128 { 2129 ddi_acc_handle_t io_hdl1 = ata_ctlp->ac_iohandle1; 2130 ddi_acc_handle_t io_hdl2 = ata_ctlp->ac_iohandle2; 2131 int rc; 2132 2133 ata_pktp->ap_wrt_count = 0; 2134 2135 rc = ata_disk_start_common(ata_ctlp, ata_drvp, ata_pktp); 2136 2137 if (!rc) 2138 return (ATA_FSM_RC_BUSY); 2139 /* 2140 * This next one sets the controller in motion 2141 */ 2142 ddi_put8(io_hdl1, ata_ctlp->ac_cmd, ata_pktp->ap_cmd); 2143 2144 /* wait for the busy bit to settle */ 2145 ata_nsecwait(400); 2146 2147 /* 2148 * Wait for the drive to assert DRQ to send the first chunk 2149 * of data. Have to busy wait because there's no interrupt for 2150 * the first chunk. This sucks (a lot of cycles) if the 2151 * drive responds too slowly or if the wait loop granularity 2152 * is too large. It's really bad if the drive is defective and 2153 * the loop times out. 2154 */ 2155 2156 if (!ata_wait3(io_hdl2, ata_ctlp->ac_ioaddr2, 2157 ATS_DRQ, ATS_BSY, /* okay */ 2158 ATS_ERR, ATS_BSY, /* cmd failed */ 2159 ATS_DF, ATS_BSY, /* drive failed */ 2160 4000000)) { 2161 ADBG_WARN(("ata_disk_start_pio_out: no DRQ\n")); 2162 ata_pktp->ap_flags |= AP_ERROR; 2163 return (ATA_FSM_RC_INTR); 2164 } 2165 2166 /* 2167 * Tell the upper layer to fake a hardware interrupt which 2168 * actually causes the first segment to be written to the drive. 2169 */ 2170 return (ATA_FSM_RC_INTR); 2171 } 2172 2173 2174 2175 /* 2176 * 2177 * packet complete callback routine 2178 * 2179 */ 2180 2181 static void 2182 ata_disk_complete( 2183 ata_drv_t *ata_drvp, 2184 ata_pkt_t *ata_pktp, 2185 int do_callback) 2186 { 2187 struct ata_id *aidp = &ata_drvp->ad_id; 2188 cmpkt_t *pktp; 2189 2190 ADBG_TRACE(("ata_disk_complete entered\n")); 2191 ADBG_TRANSPORT(("ata_disk_complete: pkt = 0x%p\n", ata_pktp)); 2192 2193 pktp = APKT2CPKT(ata_pktp); 2194 2195 /* update resid */ 2196 2197 pktp->cp_resid = ata_pktp->ap_resid; 2198 2199 if (ata_pktp->ap_flags & AP_ERROR) { 2200 2201 pktp->cp_reason = CPS_CHKERR; 2202 2203 if (ata_pktp->ap_error & ATE_BBK_ICRC) { 2204 if (IS_ATA_VERSION_GE(aidp, 4)) 2205 ata_pktp->ap_scb = DERR_ICRC; 2206 else 2207 ata_pktp->ap_scb = DERR_BBK; 2208 } else if (ata_pktp->ap_error & ATE_UNC) 2209 ata_pktp->ap_scb = DERR_UNC; 2210 else if (ata_pktp->ap_error & ATE_IDNF) 2211 ata_pktp->ap_scb = DERR_IDNF; 2212 else if (ata_pktp->ap_error & ATE_TKONF) 2213 ata_pktp->ap_scb = DERR_TKONF; 2214 else if (ata_pktp->ap_error & ATE_AMNF) 2215 ata_pktp->ap_scb = DERR_AMNF; 2216 else if (ata_pktp->ap_status & ATS_BSY) 2217 ata_pktp->ap_scb = DERR_BUSY; 2218 else if (ata_pktp->ap_status & ATS_DF) 2219 ata_pktp->ap_scb = DERR_DWF; 2220 else /* any unknown error */ 2221 ata_pktp->ap_scb = DERR_ABORT; 2222 } else if (ata_pktp->ap_flags & 2223 (AP_ABORT|AP_TIMEOUT|AP_BUS_RESET)) { 2224 2225 pktp->cp_reason = CPS_CHKERR; 2226 ata_pktp->ap_scb = DERR_ABORT; 2227 } else { 2228 pktp->cp_reason = CPS_SUCCESS; 2229 ata_pktp->ap_scb = DERR_SUCCESS; 2230 } 2231 2232 /* callback */ 2233 if (do_callback) 2234 (*pktp->cp_callback)(pktp); 2235 } 2236 2237 2238 /* 2239 * 2240 * Interrupt callbacks 2241 * 2242 */ 2243 2244 2245 /* 2246 * 2247 * ATA command, no data 2248 * 2249 */ 2250 2251 /* ARGSUSED */ 2252 static int 2253 ata_disk_intr( 2254 ata_ctl_t *ata_ctlp, 2255 ata_drv_t *ata_drvp, 2256 ata_pkt_t *ata_pktp) 2257 { 2258 uchar_t status; 2259 2260 ADBG_TRACE(("ata_disk_intr entered\n")); 2261 ADBG_TRANSPORT(("ata_disk_intr: pkt = 0x%p\n", ata_pktp)); 2262 2263 status = ata_get_status_clear_intr(ata_ctlp, ata_pktp); 2264 2265 ASSERT((status & (ATS_BSY | ATS_DRQ)) == 0); 2266 2267 /* 2268 * check for errors 2269 */ 2270 2271 if (status & (ATS_DF | ATS_ERR)) { 2272 ADBG_WARN(("ata_disk_intr: status 0x%x error 0x%x\n", status, 2273 ddi_get8(ata_ctlp->ac_iohandle1, ata_ctlp->ac_error))); 2274 ata_pktp->ap_flags |= AP_ERROR; 2275 } 2276 2277 if (ata_pktp->ap_flags & AP_ERROR) { 2278 ata_pktp->ap_status = ddi_get8(ata_ctlp->ac_iohandle2, 2279 ata_ctlp->ac_altstatus); 2280 ata_pktp->ap_error = ddi_get8(ata_ctlp->ac_iohandle1, 2281 ata_ctlp->ac_error); 2282 } 2283 2284 /* tell the upper layer this request is complete */ 2285 return (ATA_FSM_RC_FINI); 2286 } 2287 2288 2289 /* 2290 * 2291 * ATA command, PIO data in 2292 * 2293 */ 2294 2295 /* ARGSUSED */ 2296 static int 2297 ata_disk_intr_pio_in( 2298 ata_ctl_t *ata_ctlp, 2299 ata_drv_t *ata_drvp, 2300 ata_pkt_t *ata_pktp) 2301 { 2302 ddi_acc_handle_t io_hdl1 = ata_ctlp->ac_iohandle1; 2303 ddi_acc_handle_t io_hdl2 = ata_ctlp->ac_iohandle2; 2304 uchar_t status; 2305 2306 ADBG_TRACE(("ata_disk_pio_in entered\n")); 2307 ADBG_TRANSPORT(("ata_disk_pio_in: pkt = 0x%p\n", ata_pktp)); 2308 2309 /* 2310 * first make certain DRQ is asserted (and no errors) 2311 */ 2312 (void) ata_wait3(io_hdl2, ata_ctlp->ac_ioaddr2, 2313 ATS_DRQ, ATS_BSY, ATS_ERR, ATS_BSY, ATS_DF, ATS_BSY, 2314 4000000); 2315 2316 status = ata_get_status_clear_intr(ata_ctlp, ata_pktp); 2317 2318 if (status & ATS_BSY) { 2319 ADBG_WARN(("ata_disk_pio_in: BUSY\n")); 2320 ata_pktp->ap_flags |= AP_ERROR; 2321 ata_pktp->ap_status = ddi_get8(io_hdl2, ata_ctlp->ac_altstatus); 2322 ata_pktp->ap_error = ddi_get8(io_hdl1, ata_ctlp->ac_error); 2323 return (ATA_FSM_RC_BUSY); 2324 } 2325 2326 /* 2327 * record any errors 2328 */ 2329 if ((status & (ATS_DRQ | ATS_DF | ATS_ERR)) != ATS_DRQ) { 2330 ADBG_WARN(("ata_disk_pio_in: status 0x%x error 0x%x\n", 2331 status, ddi_get8(io_hdl1, ata_ctlp->ac_error))); 2332 ata_pktp->ap_flags |= AP_ERROR; 2333 ata_pktp->ap_status = ddi_get8(io_hdl2, ata_ctlp->ac_altstatus); 2334 ata_pktp->ap_error = ddi_get8(io_hdl1, ata_ctlp->ac_error); 2335 } 2336 2337 /* 2338 * read the next chunk of data (if any) 2339 */ 2340 if (status & ATS_DRQ) { 2341 ata_disk_pio_xfer_data_in(ata_ctlp, ata_pktp); 2342 } 2343 2344 /* 2345 * If that was the last chunk, wait for the device to clear DRQ 2346 */ 2347 if (ata_pktp->ap_resid == 0) { 2348 if (ata_wait(io_hdl2, ata_ctlp->ac_ioaddr2, 2349 0, (ATS_DRQ | ATS_BSY), 4000000)) { 2350 /* tell the upper layer this request is complete */ 2351 return (ATA_FSM_RC_FINI); 2352 } 2353 2354 ADBG_WARN(("ata_disk_pio_in: DRQ stuck\n")); 2355 ata_pktp->ap_flags |= AP_ERROR; 2356 ata_pktp->ap_status = ddi_get8(io_hdl2, ata_ctlp->ac_altstatus); 2357 ata_pktp->ap_error = ddi_get8(io_hdl1, ata_ctlp->ac_error); 2358 } 2359 2360 /* 2361 * check for errors 2362 */ 2363 if (ata_pktp->ap_flags & AP_ERROR) { 2364 return (ATA_FSM_RC_FINI); 2365 } 2366 2367 /* 2368 * If the read command isn't done yet, 2369 * wait for the next interrupt. 2370 */ 2371 ADBG_TRACE(("ata_disk_pio_in: partial\n")); 2372 return (ATA_FSM_RC_OKAY); 2373 } 2374 2375 2376 2377 /* 2378 * 2379 * ATA command, PIO data out 2380 * 2381 */ 2382 2383 /* ARGSUSED */ 2384 static int 2385 ata_disk_intr_pio_out( 2386 ata_ctl_t *ata_ctlp, 2387 ata_drv_t *ata_drvp, 2388 ata_pkt_t *ata_pktp) 2389 { 2390 ddi_acc_handle_t io_hdl1 = ata_ctlp->ac_iohandle1; 2391 ddi_acc_handle_t io_hdl2 = ata_ctlp->ac_iohandle2; 2392 int tmp_count = ata_pktp->ap_wrt_count; 2393 uchar_t status; 2394 2395 /* 2396 * clear the IRQ 2397 */ 2398 status = ata_get_status_clear_intr(ata_ctlp, ata_pktp); 2399 2400 ADBG_TRACE(("ata_disk_intr_pio_out entered\n")); 2401 ADBG_TRANSPORT(("ata_disk_intr_pio_out: pkt = 0x%p\n", ata_pktp)); 2402 2403 ASSERT(!(status & ATS_BSY)); 2404 2405 2406 /* 2407 * check for errors 2408 */ 2409 2410 if (status & (ATS_DF | ATS_ERR)) { 2411 ADBG_WARN(("ata_disk_intr_pio_out: status 0x%x error 0x%x\n", 2412 status, ddi_get8(io_hdl1, ata_ctlp->ac_error))); 2413 ata_pktp->ap_flags |= AP_ERROR; 2414 ata_pktp->ap_status = ddi_get8(io_hdl2, ata_ctlp->ac_altstatus); 2415 ata_pktp->ap_error = ddi_get8(io_hdl1, ata_ctlp->ac_error); 2416 /* tell the upper layer this request is complete */ 2417 return (ATA_FSM_RC_FINI); 2418 } 2419 2420 2421 /* 2422 * last write was okay, bump the ptr and 2423 * decr the resid count 2424 */ 2425 ata_pktp->ap_v_addr += tmp_count; 2426 ata_pktp->ap_resid -= tmp_count; 2427 2428 /* 2429 * check for final interrupt on write command 2430 */ 2431 if (ata_pktp->ap_resid <= 0) { 2432 /* tell the upper layer this request is complete */ 2433 return (ATA_FSM_RC_FINI); 2434 } 2435 2436 /* 2437 * Perform the next data transfer 2438 * 2439 * First make certain DRQ is asserted and no error status. 2440 * (I'm not certain but I think some drives might deassert BSY 2441 * before asserting DRQ. This extra ata_wait3() will 2442 * compensate for such drives). 2443 * 2444 */ 2445 (void) ata_wait3(io_hdl2, ata_ctlp->ac_ioaddr2, 2446 ATS_DRQ, ATS_BSY, ATS_ERR, ATS_BSY, ATS_DF, ATS_BSY, 4000000); 2447 2448 status = ddi_get8(io_hdl2, ata_ctlp->ac_altstatus); 2449 2450 if (status & ATS_BSY) { 2451 /* this should never happen */ 2452 ADBG_WARN(("ata_disk_intr_pio_out: BUSY\n")); 2453 ata_pktp->ap_flags |= AP_ERROR; 2454 ata_pktp->ap_status = ddi_get8(io_hdl2, ata_ctlp->ac_altstatus); 2455 ata_pktp->ap_error = ddi_get8(io_hdl1, ata_ctlp->ac_error); 2456 return (ATA_FSM_RC_BUSY); 2457 } 2458 2459 /* 2460 * bailout if any errors 2461 */ 2462 if ((status & (ATS_DRQ | ATS_DF | ATS_ERR)) != ATS_DRQ) { 2463 ADBG_WARN(("ata_disk_pio_out: status 0x%x error 0x%x\n", 2464 status, ddi_get8(io_hdl1, ata_ctlp->ac_error))); 2465 ata_pktp->ap_flags |= AP_ERROR; 2466 ata_pktp->ap_status = ddi_get8(io_hdl2, ata_ctlp->ac_altstatus); 2467 ata_pktp->ap_error = ddi_get8(io_hdl1, ata_ctlp->ac_error); 2468 return (ATA_FSM_RC_FINI); 2469 } 2470 2471 /* 2472 * write the next chunk of data 2473 */ 2474 ADBG_TRACE(("ata_disk_intr_pio_out: write xfer\n")); 2475 ata_disk_pio_xfer_data_out(ata_ctlp, ata_pktp); 2476 2477 /* 2478 * Wait for the next interrupt before checking the transfer 2479 * status and adjusting the transfer count. 2480 * 2481 */ 2482 return (ATA_FSM_RC_OKAY); 2483 } 2484 2485 2486 /* 2487 * 2488 * ATA command, DMA data in/out 2489 * 2490 */ 2491 2492 static int 2493 ata_disk_intr_dma( 2494 ata_ctl_t *ata_ctlp, 2495 ata_drv_t *ata_drvp, 2496 ata_pkt_t *ata_pktp) 2497 { 2498 ddi_acc_handle_t io_hdl1 = ata_ctlp->ac_iohandle1; 2499 ddi_acc_handle_t io_hdl2 = ata_ctlp->ac_iohandle2; 2500 uchar_t status; 2501 2502 ADBG_TRACE(("ata_disk_intr_dma entered\n")); 2503 ADBG_TRANSPORT(("ata_disk_intr_dma: pkt = 0x%p\n", ata_pktp)); 2504 2505 /* 2506 * halt the DMA engine 2507 */ 2508 ata_pciide_dma_stop(ata_ctlp); 2509 2510 /* 2511 * wait for the device to clear DRQ 2512 */ 2513 if (!ata_wait(io_hdl2, ata_ctlp->ac_ioaddr2, 2514 0, (ATS_DRQ | ATS_BSY), 4000000)) { 2515 ADBG_WARN(("ata_disk_intr_dma: DRQ stuck\n")); 2516 ata_pktp->ap_flags |= AP_ERROR; 2517 ata_pktp->ap_status = ddi_get8(io_hdl2, ata_ctlp->ac_altstatus); 2518 ata_pktp->ap_error = ddi_get8(io_hdl1, ata_ctlp->ac_error); 2519 return (ATA_FSM_RC_BUSY); 2520 } 2521 2522 /* 2523 * get the status and clear the IRQ, and check for DMA error 2524 */ 2525 status = ata_get_status_clear_intr(ata_ctlp, ata_pktp); 2526 2527 /* 2528 * check for drive errors 2529 */ 2530 2531 if (status & (ATS_DF | ATS_ERR)) { 2532 ADBG_WARN(("ata_disk_intr_dma: status 0x%x error 0x%x\n", 2533 status, ddi_get8(io_hdl1, ata_ctlp->ac_error))); 2534 ata_pktp->ap_flags |= AP_ERROR; 2535 ata_pktp->ap_status = ddi_get8(io_hdl2, ata_ctlp->ac_altstatus); 2536 ata_pktp->ap_error = ddi_get8(io_hdl1, ata_ctlp->ac_error); 2537 } 2538 2539 /* 2540 * If there was a drive or DMA error, compute a resid count 2541 */ 2542 if (ata_pktp->ap_flags & AP_ERROR) { 2543 /* 2544 * grab the last sector address from the drive regs 2545 * and use that to compute the resid 2546 */ 2547 ata_disk_get_resid(ata_ctlp, ata_drvp, ata_pktp); 2548 } else { 2549 ata_pktp->ap_resid = 0; 2550 } 2551 2552 /* tell the upper layer this request is complete */ 2553 return (ATA_FSM_RC_FINI); 2554 } 2555 2556 2557 /* 2558 * 2559 * Low level PIO routine that transfers data from the drive 2560 * 2561 */ 2562 2563 static void 2564 ata_disk_pio_xfer_data_in( 2565 ata_ctl_t *ata_ctlp, 2566 ata_pkt_t *ata_pktp) 2567 { 2568 ddi_acc_handle_t io_hdl1 = ata_ctlp->ac_iohandle1; 2569 int count; 2570 2571 count = min(ata_pktp->ap_resid, 2572 ata_pktp->ap_bytes_per_block); 2573 2574 ADBG_TRANSPORT(("ata_disk_pio_xfer_data_in: 0x%x bytes, addr = 0x%p\n", 2575 count, ata_pktp->ap_v_addr)); 2576 2577 /* 2578 * read count bytes 2579 */ 2580 2581 ASSERT(count != 0); 2582 2583 ddi_rep_get16(io_hdl1, (ushort_t *)ata_pktp->ap_v_addr, 2584 ata_ctlp->ac_data, (count >> 1), DDI_DEV_NO_AUTOINCR); 2585 2586 /* wait for the busy bit to settle */ 2587 ata_nsecwait(400); 2588 2589 /* 2590 * this read command completed okay, bump the ptr and 2591 * decr the resid count now. 2592 */ 2593 ata_pktp->ap_v_addr += count; 2594 ata_pktp->ap_resid -= count; 2595 } 2596 2597 2598 /* 2599 * 2600 * Low level PIO routine that transfers data to the drive 2601 * 2602 */ 2603 2604 static void 2605 ata_disk_pio_xfer_data_out( 2606 ata_ctl_t *ata_ctlp, 2607 ata_pkt_t *ata_pktp) 2608 { 2609 ddi_acc_handle_t io_hdl1 = ata_ctlp->ac_iohandle1; 2610 int count; 2611 2612 count = min(ata_pktp->ap_resid, 2613 ata_pktp->ap_bytes_per_block); 2614 2615 ADBG_TRANSPORT(("ata_disk_pio_xfer_data_out: 0x%x bytes, addr = 0x%p\n", 2616 count, ata_pktp->ap_v_addr)); 2617 2618 /* 2619 * read or write count bytes 2620 */ 2621 2622 ASSERT(count != 0); 2623 2624 ddi_rep_put16(io_hdl1, (ushort_t *)ata_pktp->ap_v_addr, 2625 ata_ctlp->ac_data, (count >> 1), DDI_DEV_NO_AUTOINCR); 2626 2627 /* wait for the busy bit to settle */ 2628 ata_nsecwait(400); 2629 2630 /* 2631 * save the count here so I can correctly adjust 2632 * the ap_v_addr and ap_resid values at the next 2633 * interrupt. 2634 */ 2635 ata_pktp->ap_wrt_count = count; 2636 } 2637 2638 2639 /* 2640 * 2641 * ATA Initialize Device Parameters (aka Set Params) command 2642 * 2643 * If the drive was put in some sort of CHS extended/logical geometry 2644 * mode by the BIOS, this function will reset it to its "native" 2645 * CHS geometry. This ensures that we don't run into any sort of 2646 * 1024 cylinder (or 65535 cylinder) limitation that may have been 2647 * created by a BIOS (or users) that chooses a bogus translated geometry. 2648 */ 2649 2650 static int 2651 ata_disk_initialize_device_parameters( 2652 ata_ctl_t *ata_ctlp, 2653 ata_drv_t *ata_drvp) 2654 { 2655 int rc; 2656 2657 rc = ata_command(ata_ctlp, ata_drvp, FALSE, FALSE, 2658 ata_disk_init_dev_parm_wait, 2659 ATC_SETPARAM, 2660 0, /* feature n/a */ 2661 ata_drvp->ad_phsec, /* max sector (1-based) */ 2662 0, /* sector n/a */ 2663 (ata_drvp->ad_phhd -1), /* max head (0-based) */ 2664 0, /* cyl_low n/a */ 2665 0); /* cyl_hi n/a */ 2666 2667 if (rc) 2668 return (TRUE); 2669 2670 ADBG_ERROR(("ata_init_dev_parms: failed\n")); 2671 return (FALSE); 2672 } 2673 2674 2675 2676 /* 2677 * 2678 * create fake inquiry data for DADA interface 2679 * 2680 */ 2681 2682 static void 2683 ata_disk_fake_inquiry( 2684 ata_drv_t *ata_drvp) 2685 { 2686 struct ata_id *ata_idp = &ata_drvp->ad_id; 2687 struct scsi_inquiry *inqp = &ata_drvp->ad_inquiry; 2688 2689 ADBG_TRACE(("ata_disk_fake_inquiry entered\n")); 2690 2691 if (ata_idp->ai_config & ATA_ID_REM_DRV) /* ide removable bit */ 2692 inqp->inq_rmb = 1; /* scsi removable bit */ 2693 2694 (void) strncpy(inqp->inq_vid, "Gen-ATA ", sizeof (inqp->inq_vid)); 2695 inqp->inq_dtype = DTYPE_DIRECT; 2696 inqp->inq_qual = DPQ_POSSIBLE; 2697 2698 (void) strncpy(inqp->inq_pid, ata_idp->ai_model, 2699 sizeof (inqp->inq_pid)); 2700 (void) strncpy(inqp->inq_revision, ata_idp->ai_fw, 2701 sizeof (inqp->inq_revision)); 2702 } 2703 2704 #define LOOP_COUNT 10000 2705 2706 2707 /* 2708 * 2709 * ATA Set Multiple Mode 2710 * 2711 */ 2712 2713 static int 2714 ata_disk_set_multiple( 2715 ata_ctl_t *ata_ctlp, 2716 ata_drv_t *ata_drvp) 2717 { 2718 int rc; 2719 2720 rc = ata_command(ata_ctlp, ata_drvp, TRUE, FALSE, 2721 ata_disk_set_mult_wait, 2722 ATC_SETMULT, 2723 0, /* feature n/a */ 2724 ata_drvp->ad_block_factor, /* count */ 2725 0, /* sector n/a */ 2726 0, /* head n/a */ 2727 0, /* cyl_low n/a */ 2728 0); /* cyl_hi n/a */ 2729 2730 if (rc) { 2731 return (TRUE); 2732 } 2733 2734 ADBG_ERROR(("ata_disk_set_multiple: failed\n")); 2735 return (FALSE); 2736 } 2737 2738 2739 /* 2740 * 2741 * ATA Identify Device command 2742 * 2743 */ 2744 2745 int 2746 ata_disk_id( 2747 ddi_acc_handle_t io_hdl1, 2748 caddr_t ioaddr1, 2749 ddi_acc_handle_t io_hdl2, 2750 caddr_t ioaddr2, 2751 struct ata_id *ata_idp) 2752 { 2753 int rc; 2754 2755 ADBG_TRACE(("ata_disk_id entered\n")); 2756 2757 rc = ata_id_common(ATC_ID_DEVICE, TRUE, io_hdl1, ioaddr1, io_hdl2, 2758 ioaddr2, ata_idp); 2759 2760 if (!rc) 2761 return (FALSE); 2762 2763 /* 2764 * If the disk is a CF/Microdrive that works under ATA mode 2765 * through CF<->ATA adapters, identify it as an ATA device 2766 * and a non removable media. 2767 */ 2768 if (ata_idp->ai_config == ATA_ID_COMPACT_FLASH) { 2769 ata_idp->ai_config = ATA_ID_CF_TO_ATA; 2770 } 2771 2772 if ((ata_idp->ai_config & ATAC_ATA_TYPE_MASK) != ATAC_ATA_TYPE) 2773 return (FALSE); 2774 2775 if (ata_idp->ai_heads == 0 || ata_idp->ai_sectors == 0) { 2776 return (FALSE); 2777 } 2778 2779 return (TRUE); 2780 } 2781 2782 static daddr_t 2783 ata_last_block_xferred_chs(ata_drv_t *ata_drvp) 2784 { 2785 ata_ctl_t *ata_ctlp = ata_drvp->ad_ctlp; 2786 ddi_acc_handle_t io_hdl1 = ata_ctlp->ac_iohandle1; 2787 uchar_t drvheads = ata_drvp->ad_phhd; 2788 uchar_t drvsectors = ata_drvp->ad_phsec; 2789 uchar_t sector; 2790 uchar_t head; 2791 uchar_t low_cyl; 2792 uchar_t hi_cyl; 2793 daddr_t lbastop; 2794 2795 sector = ddi_get8(io_hdl1, ata_ctlp->ac_sect); 2796 head = ddi_get8(io_hdl1, ata_ctlp->ac_drvhd) & 0xf; 2797 low_cyl = ddi_get8(io_hdl1, ata_ctlp->ac_lcyl); 2798 hi_cyl = ddi_get8(io_hdl1, ata_ctlp->ac_hcyl); 2799 2800 lbastop = low_cyl; 2801 lbastop |= (uint_t)hi_cyl << 8; 2802 lbastop *= (uint_t)drvheads; 2803 lbastop += (uint_t)head; 2804 lbastop *= (uint_t)drvsectors; 2805 lbastop += (uint_t)sector - 1; 2806 return (lbastop); 2807 } 2808 2809 static daddr_t 2810 ata_last_block_xferred_lba28(ata_ctl_t *ata_ctlp) 2811 { 2812 ddi_acc_handle_t io_hdl1 = ata_ctlp->ac_iohandle1; 2813 daddr_t lbastop; 2814 2815 lbastop = ddi_get8(io_hdl1, ata_ctlp->ac_drvhd) & 0xf; 2816 lbastop <<= 8; 2817 lbastop += ddi_get8(io_hdl1, ata_ctlp->ac_hcyl); 2818 lbastop <<= 8; 2819 lbastop += ddi_get8(io_hdl1, ata_ctlp->ac_lcyl); 2820 lbastop <<= 8; 2821 lbastop += ddi_get8(io_hdl1, ata_ctlp->ac_sect); 2822 return (lbastop); 2823 } 2824 2825 static daddr_t 2826 ata_last_block_xferred_lba48(ata_ctl_t *ata_ctlp) 2827 { 2828 ddi_acc_handle_t io_hdl1 = ata_ctlp->ac_iohandle1; 2829 ddi_acc_handle_t io_hdl2 = ata_ctlp->ac_iohandle2; 2830 daddr_t lbastop; 2831 2832 /* turn on HOB and read the high-order 24 bits */ 2833 ddi_put8(io_hdl2, ata_ctlp->ac_devctl, (ATDC_D3 | ATDC_HOB)); 2834 lbastop = ddi_get8(io_hdl1, ata_ctlp->ac_hcyl); 2835 lbastop <<= 8; 2836 lbastop += ddi_get8(io_hdl1, ata_ctlp->ac_lcyl); 2837 lbastop <<= 8; 2838 lbastop += ddi_get8(io_hdl1, ata_ctlp->ac_sect); 2839 lbastop <<= 8; 2840 2841 /* Turn off HOB and read the low-order 24-bits */ 2842 ddi_put8(io_hdl2, ata_ctlp->ac_devctl, (ATDC_D3)); 2843 lbastop += ddi_get8(io_hdl1, ata_ctlp->ac_hcyl); 2844 lbastop <<= 8; 2845 lbastop += ddi_get8(io_hdl1, ata_ctlp->ac_lcyl); 2846 lbastop <<= 8; 2847 lbastop += ddi_get8(io_hdl1, ata_ctlp->ac_sect); 2848 return (lbastop); 2849 } 2850 2851 2852 /* 2853 * 2854 * Need to compute a value for ap_resid so that cp_resid can 2855 * be set by ata_disk_complete(). The cp_resid var is actually 2856 * misnamed. It's actually the offset to the block in which the 2857 * error occurred not the number of bytes transferred to the device. 2858 * At least that's how dadk actually uses the cp_resid when reporting 2859 * an error. In other words the sector that had the error and the 2860 * number of bytes transferred don't always indicate the same offset. 2861 * On top of that, when doing DMA transfers there's actually no 2862 * way to determine how many bytes have been transferred by the DMA 2863 * engine. On the other hand, the drive will report which sector 2864 * it faulted on. Using that address this routine computes the 2865 * number of residual bytes beyond that point which probably weren't 2866 * written to the drive (the drive is allowed to re-order sector 2867 * writes but on an ATA disk there's no way to deal with that 2868 * complication; in other words, the resid value calculated by 2869 * this routine is as good as we can manage). 2870 */ 2871 2872 static void 2873 ata_disk_get_resid( 2874 ata_ctl_t *ata_ctlp, 2875 ata_drv_t *ata_drvp, 2876 ata_pkt_t *ata_pktp) 2877 { 2878 uint_t lba_start; 2879 uint_t lba_stop; 2880 uint_t resid_bytes; 2881 uint_t resid_sectors; 2882 2883 lba_start = ata_pktp->ap_startsec; 2884 2885 if (ata_drvp->ad_flags & AD_EXT48) 2886 lba_stop = ata_last_block_xferred_lba48(ata_ctlp); 2887 else if (ata_drvp->ad_drive_bits & ATDH_LBA) 2888 lba_stop = ata_last_block_xferred_lba28(ata_ctlp); 2889 else /* CHS mode */ 2890 lba_stop = ata_last_block_xferred_chs(ata_drvp); 2891 2892 resid_sectors = lba_start + ata_pktp->ap_count - lba_stop; 2893 resid_bytes = resid_sectors << SCTRSHFT; 2894 2895 ADBG_TRACE(("ata_disk_get_resid start 0x%x cnt 0x%x stop 0x%x\n", 2896 lba_start, ata_pktp->ap_count, lba_stop)); 2897 ata_pktp->ap_resid = resid_bytes; 2898 } 2899 2900 2901 2902 /* 2903 * Removable media commands * 2904 */ 2905 2906 2907 2908 /* 2909 * get the media status 2910 * 2911 * NOTE: the error handling case probably isn't correct but it 2912 * will have to do until someone gives me a drive to test this on. 2913 */ 2914 static int 2915 ata_disk_state( 2916 ata_ctl_t *ata_ctlp, 2917 ata_drv_t *ata_drvp, 2918 ata_pkt_t *ata_pktp) 2919 { 2920 int *statep = (int *)ata_pktp->ap_v_addr; 2921 uchar_t err; 2922 2923 ADBG_TRACE(("ata_disk_state\n")); 2924 if (ata_command(ata_ctlp, ata_drvp, TRUE, TRUE, 5 * 1000000, 2925 ATC_DOOR_LOCK, 0, 0, 0, 0, 0, 0)) { 2926 *statep = DKIO_INSERTED; 2927 return (ATA_FSM_RC_FINI); 2928 } 2929 2930 err = ddi_get8(ata_ctlp->ac_iohandle1, ata_ctlp->ac_error); 2931 if (err & ATE_NM) 2932 *statep = DKIO_EJECTED; 2933 else 2934 *statep = DKIO_NONE; 2935 2936 return (ATA_FSM_RC_FINI); 2937 } 2938 2939 /* 2940 * eject the media 2941 */ 2942 2943 static int 2944 ata_disk_eject( 2945 ata_ctl_t *ata_ctlp, 2946 ata_drv_t *ata_drvp, 2947 ata_pkt_t *ata_pktp) 2948 { 2949 ADBG_TRACE(("ata_disk_eject\n")); 2950 if (ata_command(ata_ctlp, ata_drvp, TRUE, TRUE, 5 * 1000000, 2951 ATC_EJECT, 0, 0, 0, 0, 0, 0)) { 2952 return (ATA_FSM_RC_FINI); 2953 } 2954 ata_pktp->ap_flags |= AP_ERROR; 2955 return (ATA_FSM_RC_FINI); 2956 } 2957 2958 /* 2959 * lock the drive 2960 * 2961 */ 2962 static int 2963 ata_disk_lock( 2964 ata_ctl_t *ata_ctlp, 2965 ata_drv_t *ata_drvp, 2966 ata_pkt_t *ata_pktp) 2967 { 2968 ADBG_TRACE(("ata_disk_lock\n")); 2969 if (ata_command(ata_ctlp, ata_drvp, TRUE, TRUE, 5 * 1000000, 2970 ATC_DOOR_LOCK, 0, 0, 0, 0, 0, 0)) { 2971 return (ATA_FSM_RC_FINI); 2972 } 2973 ata_pktp->ap_flags |= AP_ERROR; 2974 return (ATA_FSM_RC_FINI); 2975 } 2976 2977 2978 /* 2979 * unlock the drive 2980 * 2981 */ 2982 static int 2983 ata_disk_unlock( 2984 ata_ctl_t *ata_ctlp, 2985 ata_drv_t *ata_drvp, 2986 ata_pkt_t *ata_pktp) 2987 { 2988 ADBG_TRACE(("ata_disk_unlock\n")); 2989 if (ata_command(ata_ctlp, ata_drvp, TRUE, TRUE, 5 * 1000000, 2990 ATC_DOOR_UNLOCK, 0, 0, 0, 0, 0, 0)) { 2991 return (ATA_FSM_RC_FINI); 2992 } 2993 ata_pktp->ap_flags |= AP_ERROR; 2994 return (ATA_FSM_RC_FINI); 2995 } 2996 2997 2998 /* 2999 * put the drive into standby mode 3000 */ 3001 static int 3002 ata_disk_standby( 3003 ata_ctl_t *ata_ctlp, 3004 ata_drv_t *ata_drvp, 3005 ata_pkt_t *ata_pktp) 3006 { 3007 ADBG_TRACE(("ata_disk_standby\n")); 3008 if (ata_command(ata_ctlp, ata_drvp, TRUE, TRUE, 5 * 1000000, 3009 ATC_STANDBY_IM, 0, 0, 0, 0, 0, 0)) { 3010 return (ATA_FSM_RC_FINI); 3011 } 3012 ata_pktp->ap_flags |= AP_ERROR; 3013 return (ATA_FSM_RC_FINI); 3014 } 3015 3016 3017 /* 3018 * Recalibrate 3019 * 3020 * Note the extra long timeout value. This is necessary in case 3021 * the drive was in standby mode and needs to spin up the media. 3022 * 3023 */ 3024 static int 3025 ata_disk_recalibrate( 3026 ata_ctl_t *ata_ctlp, 3027 ata_drv_t *ata_drvp, 3028 ata_pkt_t *ata_pktp) 3029 { 3030 ADBG_TRACE(("ata_disk_recalibrate\n")); 3031 if (ata_command(ata_ctlp, ata_drvp, TRUE, TRUE, 31 * 1000000, 3032 ATC_RECAL, 0, 0, 0, 0, 0, 0)) { 3033 return (ATA_FSM_RC_FINI); 3034 } 3035 ata_pktp->ap_flags |= AP_ERROR; 3036 return (ATA_FSM_RC_FINI); 3037 } 3038 3039 /* 3040 * Copy a string of bytes that were obtained by Identify Device into a 3041 * string buffer provided by the caller. 3042 * 3043 * 1. Determine the amount to copy. This is the lesser of the 3044 * length of the source string or the space available in the user's 3045 * buffer. 3046 * 2. The true length of the source string is always returned to the 3047 * caller in the size field of the argument. 3048 * 3. Copy the string, add a terminating NUL character at the end. 3049 */ 3050 3051 static int 3052 ata_copy_dk_ioc_string(intptr_t arg, char *source, int length, int flag) 3053 { 3054 STRUCT_DECL(dadk_ioc_string, ds_arg); 3055 int destsize; 3056 char nulchar; 3057 caddr_t outp; 3058 3059 /* 3060 * The ioctls that use this routine are only available to 3061 * the kernel. 3062 */ 3063 if ((flag & FKIOCTL) == 0) 3064 return (EFAULT); 3065 3066 STRUCT_INIT(ds_arg, flag & FMODELS); 3067 3068 /* 1. determine size of user's buffer */ 3069 if (ddi_copyin((caddr_t)arg, STRUCT_BUF(ds_arg), STRUCT_SIZE(ds_arg), 3070 flag)) 3071 return (EFAULT); 3072 destsize = STRUCT_FGET(ds_arg, is_size); 3073 if (destsize > length + 1) 3074 destsize = length + 1; 3075 3076 /* 3077 * 2. Return the copied length to the caller. Note: for 3078 * convenience, we actually copy the entire structure back out, not 3079 * just the length. We don't change the is_buf field, so this 3080 * shouldn't break anything. 3081 */ 3082 STRUCT_FSET(ds_arg, is_size, length); 3083 if (ddi_copyout(STRUCT_BUF(ds_arg), (caddr_t)arg, STRUCT_SIZE(ds_arg), 3084 flag)) 3085 return (EFAULT); 3086 3087 /* 3. copy the string and add a NULL terminator */ 3088 outp = STRUCT_FGETP(ds_arg, is_buf); 3089 if (ddi_copyout(source, outp, destsize - 1, flag)) 3090 return (EFAULT); 3091 nulchar = '\0'; 3092 if (ddi_copyout(&nulchar, outp + (destsize - 1), 1, flag)) 3093 return (EFAULT); 3094 return (0); 3095 } 3096 3097 /* 3098 * Sun branded drives are shipped write cache disabled. The default is to 3099 * force write write caching on. 3100 */ 3101 static void 3102 ata_set_write_cache(ata_ctl_t *ata_ctlp, ata_drv_t *ata_drvp) 3103 { 3104 char *path; 3105 3106 if (ata_write_cache == 1) { 3107 if (ata_set_feature(ata_ctlp, ata_drvp, FC_WRITE_CACHE_ON, 0) 3108 == FALSE) { 3109 path = kmem_alloc(MAXPATHLEN + 1, KM_NOSLEEP); 3110 if (path != NULL) { 3111 cmn_err(CE_WARN, 3112 "%s unable to enable write cache targ=%d", 3113 ddi_pathname(ata_ctlp->ac_dip, path), 3114 ata_drvp->ad_targ); 3115 kmem_free(path, MAXPATHLEN + 1); 3116 } 3117 } 3118 } else if (ata_write_cache == -1) { 3119 if (ata_set_feature(ata_ctlp, ata_drvp, FC_WRITE_CACHE_OFF, 0) 3120 == FALSE) { 3121 path = kmem_alloc(MAXPATHLEN + 1, KM_NOSLEEP); 3122 if (path != NULL) { 3123 cmn_err(CE_WARN, 3124 "%s unable to disable write cache targ=%d", 3125 ddi_pathname(ata_ctlp->ac_dip, path), 3126 ata_drvp->ad_targ); 3127 kmem_free(path, MAXPATHLEN + 1); 3128 } 3129 } 3130 } 3131 } 3132 3133 /* 3134 * Call set feature to spin-up the device. 3135 */ 3136 static int 3137 ata_disk_set_feature_spinup( 3138 ata_ctl_t *ata_ctlp, 3139 ata_drv_t *ata_drvp, 3140 ata_pkt_t *ata_pktp) 3141 { 3142 int rc; 3143 3144 ADBG_TRACE(("ata_disk_set_feature_spinup entered\n")); 3145 3146 rc = ata_set_feature(ata_ctlp, ata_drvp, 0x07, 0); 3147 if (!rc) 3148 ata_pktp->ap_flags |= AP_ERROR; 3149 3150 return (ATA_FSM_RC_FINI); 3151 } 3152 3153 /* 3154 * Update device ata_id content - IDENTIFY DEVICE command. 3155 */ 3156 static int 3157 ata_disk_id_update( 3158 ata_ctl_t *ata_ctlp, 3159 ata_drv_t *ata_drvp, 3160 ata_pkt_t *ata_pktp) 3161 { 3162 ddi_acc_handle_t io_hdl1 = ata_ctlp->ac_iohandle1; 3163 caddr_t ioaddr1 = ata_ctlp->ac_ioaddr1; 3164 ddi_acc_handle_t io_hdl2 = ata_ctlp->ac_iohandle2; 3165 caddr_t ioaddr2 = ata_ctlp->ac_ioaddr2; 3166 struct ata_id *aidp = &ata_drvp->ad_id; 3167 int rc; 3168 3169 ADBG_TRACE(("ata_disk_id_update entered\n")); 3170 3171 /* 3172 * select the appropriate drive and LUN 3173 */ 3174 ddi_put8(io_hdl1, (uchar_t *)ioaddr1 + AT_DRVHD, 3175 ata_drvp->ad_drive_bits); 3176 ata_nsecwait(400); 3177 3178 /* 3179 * make certain the drive is selected, and wait for not busy 3180 */ 3181 if (!ata_wait(io_hdl2, ioaddr2, ATS_DRDY, ATS_BSY, 5 * 1000000)) { 3182 ADBG_ERROR(("ata_disk_id_update: select failed\n")); 3183 ata_pktp->ap_flags |= AP_ERROR; 3184 return (ATA_FSM_RC_FINI); 3185 } 3186 3187 rc = ata_disk_id(io_hdl1, ioaddr1, io_hdl2, ioaddr2, aidp); 3188 3189 if (!rc) { 3190 ata_pktp->ap_flags |= AP_ERROR; 3191 } else { 3192 swab(aidp->ai_drvser, aidp->ai_drvser, 3193 sizeof (aidp->ai_drvser)); 3194 swab(aidp->ai_fw, aidp->ai_fw, 3195 sizeof (aidp->ai_fw)); 3196 swab(aidp->ai_model, aidp->ai_model, 3197 sizeof (aidp->ai_model)); 3198 } 3199 3200 return (ATA_FSM_RC_FINI); 3201 } 3202 3203 /* 3204 * Update device firmware. 3205 */ 3206 static int 3207 ata_disk_update_fw(gtgt_t *gtgtp, ata_ctl_t *ata_ctlp, 3208 ata_drv_t *ata_drvp, caddr_t fwfile, 3209 uint_t size, uint8_t type, int flag) 3210 { 3211 ata_pkt_t *ata_pktp; 3212 gcmd_t *gcmdp = NULL; 3213 caddr_t fwfile_memp = NULL, tmp_fwfile_memp; 3214 uint_t total_sec_count, sec_count, start_sec = 0; 3215 uint8_t cmd_type; 3216 int rc; 3217 3218 /* 3219 * First check whether DOWNLOAD MICROCODE command is supported 3220 */ 3221 if (!(ata_drvp->ad_id.ai_cmdset83 & 0x1)) { 3222 ADBG_ERROR(("drive doesn't support download " 3223 "microcode command\n")); 3224 return (ENOTSUP); 3225 } 3226 3227 switch (type) { 3228 case FW_TYPE_TEMP: 3229 cmd_type = ATCM_FW_TEMP; 3230 break; 3231 3232 case FW_TYPE_PERM: 3233 cmd_type = ATCM_FW_PERM; 3234 break; 3235 3236 default: 3237 return (EINVAL); 3238 } 3239 3240 /* Temporary subcommand is obsolete in ATA/ATAPI-8 version */ 3241 if (cmd_type == ATCM_FW_TEMP) { 3242 if (ata_drvp->ad_id.ai_majorversion & ATAC_MAJVER_8) { 3243 ADBG_ERROR(("Temporary use is obsolete in " 3244 "ATA/ATAPI-8 version\n")); 3245 return (ENOTSUP); 3246 } 3247 } 3248 3249 total_sec_count = size >> SCTRSHFT; 3250 if (total_sec_count > MAX_FWFILE_SIZE_ONECMD) { 3251 if (cmd_type == ATCM_FW_TEMP) { 3252 ADBG_ERROR(("firmware size: %x sectors is too large\n", 3253 total_sec_count)); 3254 return (EINVAL); 3255 } else { 3256 ADBG_WARN(("firmware size: %x sectors is larger than" 3257 " one command, need to use the multicommand" 3258 " subcommand\n", total_sec_count)); 3259 3260 cmd_type = ATCM_FW_MULTICMD; 3261 if (!(ata_drvp->ad_id.ai_padding2[15] & 0x10)) { 3262 ADBG_ERROR(("This drive doesn't support " 3263 "the multicommand subcommand\n")); 3264 return (ENOTSUP); 3265 } 3266 } 3267 } 3268 3269 fwfile_memp = kmem_zalloc(size, KM_SLEEP); 3270 3271 if (ddi_copyin(fwfile, fwfile_memp, size, flag)) { 3272 ADBG_ERROR(("ata_disk_update_fw copyin failed\n")); 3273 rc = EFAULT; 3274 goto done; 3275 } 3276 3277 tmp_fwfile_memp = fwfile_memp; 3278 3279 for (; total_sec_count > 0; ) { 3280 if ((gcmdp == NULL) && !(gcmdp = 3281 ghd_gcmd_alloc(gtgtp, sizeof (*ata_pktp), TRUE))) { 3282 ADBG_ERROR(("ata_disk_update_fw alloc failed\n")); 3283 rc = ENOMEM; 3284 goto done; 3285 } 3286 3287 /* set the back ptr from the ata_pkt to the gcmd_t */ 3288 ata_pktp = GCMD2APKT(gcmdp); 3289 ata_pktp->ap_gcmdp = gcmdp; 3290 ata_pktp->ap_hd = ata_drvp->ad_drive_bits; 3291 ata_pktp->ap_bytes_per_block = ata_drvp->ad_bytes_per_block; 3292 3293 /* use PIO mode to update disk firmware */ 3294 ata_pktp->ap_start = ata_disk_start_pio_out; 3295 ata_pktp->ap_intr = ata_disk_intr_pio_out; 3296 ata_pktp->ap_complete = NULL; 3297 3298 ata_pktp->ap_cmd = ATC_LOAD_FW; 3299 /* use ap_bcount to set subcommand code */ 3300 ata_pktp->ap_bcount = (size_t)cmd_type; 3301 ata_pktp->ap_pciide_dma = FALSE; 3302 ata_pktp->ap_sg_cnt = 0; 3303 3304 sec_count = min(total_sec_count, MAX_FWFILE_SIZE_ONECMD); 3305 ata_pktp->ap_flags = 0; 3306 3307 ata_pktp->ap_count = sec_count; 3308 ata_pktp->ap_startsec = start_sec; 3309 ata_pktp->ap_v_addr = tmp_fwfile_memp; 3310 ata_pktp->ap_resid = sec_count << SCTRSHFT; 3311 3312 /* add it to the queue, and use POLL mode */ 3313 rc = ghd_transport(&ata_ctlp->ac_ccc, gcmdp, gcmdp->cmd_gtgtp, 3314 ata_disk_updatefw_time, TRUE, NULL); 3315 3316 if (rc != TRAN_ACCEPT) { 3317 /* this should never, ever happen */ 3318 rc = ENOTSUP; 3319 goto done; 3320 } 3321 3322 if (ata_pktp->ap_flags & AP_ERROR) { 3323 if (ata_pktp->ap_error & ATE_ABORT) { 3324 rc = ENOTSUP; 3325 } else 3326 rc = EIO; 3327 goto done; 3328 3329 } else { 3330 total_sec_count -= sec_count; 3331 tmp_fwfile_memp += sec_count << SCTRSHFT; 3332 start_sec += sec_count; 3333 } 3334 } 3335 3336 rc = 0; 3337 done: 3338 if (gcmdp != NULL) 3339 ghd_gcmd_free(gcmdp); 3340 3341 kmem_free(fwfile_memp, size); 3342 3343 return (rc); 3344 } 3345