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