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