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