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