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