Print this page
4630 clean stale references to ddi_iopb_alloc and ddi_iopb_free
4634 undocument scsi_hba_attach() and ddi_dma_lim(9s)
Split |
Close |
Expand all |
Collapse all |
--- old/usr/src/uts/intel/io/dktp/dcdev/dadk.c
+++ new/usr/src/uts/intel/io/dktp/dcdev/dadk.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 (c) 2006, 2010, Oracle and/or its affiliates. All rights reserved.
24 24 */
25 25
26 26 /*
27 27 * Direct Attached Disk
28 28 */
29 29
30 30 #include <sys/file.h>
31 31 #include <sys/scsi/scsi.h>
32 32 #include <sys/var.h>
33 33 #include <sys/proc.h>
34 34 #include <sys/dktp/cm.h>
35 35 #include <sys/vtoc.h>
36 36 #include <sys/dkio.h>
37 37 #include <sys/policy.h>
38 38 #include <sys/priv.h>
39 39
40 40 #include <sys/dktp/dadev.h>
41 41 #include <sys/dktp/fctypes.h>
42 42 #include <sys/dktp/flowctrl.h>
43 43 #include <sys/dktp/tgcom.h>
44 44 #include <sys/dktp/tgdk.h>
45 45 #include <sys/dktp/bbh.h>
46 46 #include <sys/dktp/dadkio.h>
47 47 #include <sys/dktp/dadk.h>
48 48 #include <sys/cdio.h>
49 49
50 50 /*
51 51 * Local Function Prototypes
52 52 */
53 53 static void dadk_restart(void *pktp);
54 54 static void dadk_pktcb(struct cmpkt *pktp);
55 55 static void dadk_iodone(struct buf *bp);
56 56 static void dadk_polldone(struct buf *bp);
57 57 static void dadk_setcap(struct dadk *dadkp);
58 58 static void dadk_create_errstats(struct dadk *dadkp, int instance);
59 59 static void dadk_destroy_errstats(struct dadk *dadkp);
60 60
61 61 static int dadk_chkerr(struct cmpkt *pktp);
62 62 static int dadk_ioprep(struct dadk *dadkp, struct cmpkt *pktp);
63 63 static int dadk_iosetup(struct dadk *dadkp, struct cmpkt *pktp);
64 64 static int dadk_ioretry(struct cmpkt *pktp, int action);
65 65
66 66 static struct cmpkt *dadk_pktprep(struct dadk *dadkp, struct cmpkt *in_pktp,
67 67 struct buf *bp, void (*cb_func)(struct buf *), int (*func)(caddr_t),
68 68 caddr_t arg);
69 69
70 70 static int dadk_pkt(opaque_t com_data, struct buf *bp, int (*func)(caddr_t),
71 71 caddr_t arg);
72 72 static void dadk_transport(opaque_t com_data, struct buf *bp);
73 73 static int dadk_ctl_ioctl(struct dadk *, uint32_t, uintptr_t, int);
74 74
75 75 struct tgcom_objops dadk_com_ops = {
76 76 nodev,
77 77 nodev,
78 78 dadk_pkt,
79 79 dadk_transport,
80 80 0, 0
81 81 };
82 82
83 83 /*
84 84 * architecture dependent allocation restrictions for dadk_iob_alloc(). For
85 85 * x86, we'll set dma_attr_addr_hi to dadk_max_phys_addr and dma_attr_sgllen
86 86 * to dadk_sgl_size during _init().
87 87 */
88 88 #if defined(__sparc)
89 89 static ddi_dma_attr_t dadk_alloc_attr = {
90 90 DMA_ATTR_V0, /* version number */
91 91 0x0, /* lowest usable address */
92 92 0xFFFFFFFFull, /* high DMA address range */
93 93 0xFFFFFFFFull, /* DMA counter register */
94 94 1, /* DMA address alignment */
95 95 1, /* DMA burstsizes */
96 96 1, /* min effective DMA size */
97 97 0xFFFFFFFFull, /* max DMA xfer size */
98 98 0xFFFFFFFFull, /* segment boundary */
99 99 1, /* s/g list length */
100 100 512, /* granularity of device */
101 101 0, /* DMA transfer flags */
102 102 };
103 103 #elif defined(__x86)
104 104 static ddi_dma_attr_t dadk_alloc_attr = {
105 105 DMA_ATTR_V0, /* version number */
106 106 0x0, /* lowest usable address */
107 107 0x0, /* high DMA address range [set in _init()] */
108 108 0xFFFFull, /* DMA counter register */
109 109 512, /* DMA address alignment */
110 110 1, /* DMA burstsizes */
111 111 1, /* min effective DMA size */
112 112 0xFFFFFFFFull, /* max DMA xfer size */
113 113 0xFFFFFFFFull, /* segment boundary */
114 114 0, /* s/g list length [set in _init()] */
115 115 512, /* granularity of device */
116 116 0, /* DMA transfer flags */
117 117 };
118 118
119 119 uint64_t dadk_max_phys_addr = 0xFFFFFFFFull;
120 120 int dadk_sgl_size = 0xFF;
121 121 #endif
122 122
123 123 static int dadk_rmb_ioctl(struct dadk *dadkp, int cmd, intptr_t arg, int flags,
124 124 int silent);
125 125 static void dadk_rmb_iodone(struct buf *bp);
126 126
127 127 static int dadk_dk_buf_setup(struct dadk *dadkp, opaque_t *cmdp,
128 128 dev_t dev, enum uio_seg dataspace, int rw);
129 129 static void dadk_dk(struct dadk *dadkp, struct dadkio_rwcmd *scmdp,
130 130 struct buf *bp);
131 131 static void dadkmin(struct buf *bp);
132 132 static int dadk_dk_strategy(struct buf *bp);
133 133 static void dadk_recorderr(struct cmpkt *pktp, struct dadkio_rwcmd *rwcmdp);
134 134
135 135 struct tgdk_objops dadk_ops = {
136 136 dadk_init,
137 137 dadk_free,
138 138 dadk_probe,
139 139 dadk_attach,
140 140 dadk_open,
141 141 dadk_close,
142 142 dadk_ioctl,
143 143 dadk_strategy,
144 144 dadk_setgeom,
145 145 dadk_getgeom,
146 146 dadk_iob_alloc,
147 147 dadk_iob_free,
148 148 dadk_iob_htoc,
149 149 dadk_iob_xfer,
150 150 dadk_dump,
151 151 dadk_getphygeom,
152 152 dadk_set_bbhobj,
153 153 dadk_check_media,
154 154 dadk_inquiry,
155 155 dadk_cleanup,
156 156 0
157 157 };
158 158
159 159 /*
160 160 * Local static data
161 161 */
162 162
163 163 #ifdef DADK_DEBUG
164 164 #define DENT 0x0001
165 165 #define DERR 0x0002
166 166 #define DIO 0x0004
167 167 #define DGEOM 0x0010
168 168 #define DSTATE 0x0020
169 169 static int dadk_debug = DGEOM;
170 170
171 171 #endif /* DADK_DEBUG */
172 172
173 173 static int dadk_check_media_time = 3000000; /* 3 Second State Check */
174 174 static int dadk_dk_maxphys = 0x80000;
175 175
176 176 static char *dadk_cmds[] = {
177 177 "\000Unknown", /* unknown */
178 178 "\001read sector", /* DCMD_READ 1 */
179 179 "\002write sector", /* DCMD_WRITE 2 */
180 180 "\003format track", /* DCMD_FMTTRK 3 */
181 181 "\004format whole drive", /* DCMD_FMTDRV 4 */
182 182 "\005recalibrate", /* DCMD_RECAL 5 */
183 183 "\006seek sector", /* DCMD_SEEK 6 */
184 184 "\007read verify", /* DCMD_RDVER 7 */
185 185 "\010read defect list", /* DCMD_GETDEF 8 */
186 186 "\011lock door", /* DCMD_LOCK 9 */
187 187 "\012unlock door", /* DCMD_UNLOCK 10 */
188 188 "\013start motor", /* DCMD_START_MOTOR 11 */
189 189 "\014stop motor", /* DCMD_STOP_MOTOR 12 */
190 190 "\015eject", /* DCMD_EJECT 13 */
191 191 "\016update geometry", /* DCMD_UPDATE_GEOM 14 */
192 192 "\017get state", /* DCMD_GET_STATE 15 */
193 193 "\020cdrom pause", /* DCMD_PAUSE 16 */
194 194 "\021cdrom resume", /* DCMD_RESUME 17 */
195 195 "\022cdrom play track index", /* DCMD_PLAYTRKIND 18 */
196 196 "\023cdrom play msf", /* DCMD_PLAYMSF 19 */
197 197 "\024cdrom sub channel", /* DCMD_SUBCHNL 20 */
198 198 "\025cdrom read mode 1", /* DCMD_READMODE1 21 */
199 199 "\026cdrom read toc header", /* DCMD_READTOCHDR 22 */
200 200 "\027cdrom read toc entry", /* DCMD_READTOCENT 23 */
201 201 "\030cdrom read offset", /* DCMD_READOFFSET 24 */
202 202 "\031cdrom read mode 2", /* DCMD_READMODE2 25 */
203 203 "\032cdrom volume control", /* DCMD_VOLCTRL 26 */
204 204 "\033flush cache", /* DCMD_FLUSH_CACHE 27 */
205 205 NULL
206 206 };
207 207
208 208 static char *dadk_sense[] = {
209 209 "\000Success", /* DERR_SUCCESS */
210 210 "\001address mark not found", /* DERR_AMNF */
211 211 "\002track 0 not found", /* DERR_TKONF */
212 212 "\003aborted command", /* DERR_ABORT */
213 213 "\004write fault", /* DERR_DWF */
214 214 "\005ID not found", /* DERR_IDNF */
215 215 "\006drive busy", /* DERR_BUSY */
216 216 "\007uncorrectable data error", /* DERR_UNC */
217 217 "\010bad block detected", /* DERR_BBK */
218 218 "\011invalid command", /* DERR_INVCDB */
219 219 "\012device hard error", /* DERR_HARD */
220 220 "\013illegal length indicated", /* DERR_ILI */
221 221 "\014end of media", /* DERR_EOM */
222 222 "\015media change requested", /* DERR_MCR */
223 223 "\016recovered from error", /* DERR_RECOVER */
224 224 "\017device not ready", /* DERR_NOTREADY */
225 225 "\020medium error", /* DERR_MEDIUM */
226 226 "\021hardware error", /* DERR_HW */
227 227 "\022illegal request", /* DERR_ILL */
228 228 "\023unit attention", /* DERR_UNIT_ATTN */
229 229 "\024data protection", /* DERR_DATA_PROT */
230 230 "\025miscompare", /* DERR_MISCOMPARE */
231 231 "\026ICRC error during UDMA", /* DERR_ICRC */
232 232 "\027reserved", /* DERR_RESV */
233 233 NULL
234 234 };
235 235
236 236 static char *dadk_name = "Disk";
237 237
238 238 /*
239 239 * This is the loadable module wrapper
240 240 */
241 241 #include <sys/modctl.h>
242 242
243 243 extern struct mod_ops mod_miscops;
244 244
245 245 static struct modlmisc modlmisc = {
246 246 &mod_miscops, /* Type of module */
247 247 "Direct Attached Disk"
248 248 };
249 249
250 250 static struct modlinkage modlinkage = {
251 251 MODREV_1, (void *)&modlmisc, NULL
252 252 };
253 253
254 254 int
255 255 _init(void)
256 256 {
257 257 #ifdef DADK_DEBUG
258 258 if (dadk_debug & DENT)
259 259 PRF("dadk_init: call\n");
260 260 #endif
261 261
262 262 #if defined(__x86)
263 263 /* set the max physical address for iob allocs on x86 */
264 264 dadk_alloc_attr.dma_attr_addr_hi = dadk_max_phys_addr;
265 265
266 266 /*
267 267 * set the sgllen for iob allocs on x86. If this is set less than
268 268 * the number of pages the buffer will take (taking into account
269 269 * alignment), it would force the allocator to try and allocate
270 270 * contiguous pages.
271 271 */
272 272 dadk_alloc_attr.dma_attr_sgllen = dadk_sgl_size;
273 273 #endif
274 274
275 275 return (mod_install(&modlinkage));
276 276 }
277 277
278 278 int
279 279 _fini(void)
280 280 {
281 281 #ifdef DADK_DEBUG
282 282 if (dadk_debug & DENT)
283 283 PRF("dadk_fini: call\n");
284 284 #endif
285 285
286 286 return (mod_remove(&modlinkage));
287 287 }
288 288
289 289 int
290 290 _info(struct modinfo *modinfop)
291 291 {
292 292 return (mod_info(&modlinkage, modinfop));
293 293 }
294 294
295 295 struct tgdk_obj *
296 296 dadk_create()
297 297 {
298 298 struct tgdk_obj *dkobjp;
299 299 struct dadk *dadkp;
300 300
301 301 dkobjp = kmem_zalloc((sizeof (*dkobjp) + sizeof (*dadkp)), KM_NOSLEEP);
302 302 if (!dkobjp)
303 303 return (NULL);
304 304 dadkp = (struct dadk *)(dkobjp+1);
305 305
306 306 dkobjp->tg_ops = (struct tgdk_objops *)&dadk_ops;
307 307 dkobjp->tg_data = (opaque_t)dadkp;
308 308 dkobjp->tg_ext = &(dkobjp->tg_extblk);
309 309 dadkp->dad_extp = &(dkobjp->tg_extblk);
310 310
311 311 #ifdef DADK_DEBUG
312 312 if (dadk_debug & DENT)
313 313 PRF("dadk_create: tgdkobjp= 0x%x dadkp= 0x%x\n", dkobjp, dadkp);
314 314 #endif
315 315 return (dkobjp);
316 316 }
317 317
318 318 int
319 319 dadk_init(opaque_t objp, opaque_t devp, opaque_t flcobjp, opaque_t queobjp,
320 320 opaque_t bbhobjp, void *lkarg)
321 321 {
322 322 struct dadk *dadkp = (struct dadk *)objp;
323 323 struct scsi_device *sdevp = (struct scsi_device *)devp;
324 324
325 325 dadkp->dad_sd = devp;
326 326 dadkp->dad_ctlobjp = (opaque_t)sdevp->sd_address.a_hba_tran;
327 327 sdevp->sd_private = (caddr_t)dadkp;
328 328
329 329 /* initialize the communication object */
330 330 dadkp->dad_com.com_data = (opaque_t)dadkp;
331 331 dadkp->dad_com.com_ops = &dadk_com_ops;
332 332
333 333 dadkp->dad_bbhobjp = bbhobjp;
334 334 BBH_INIT(bbhobjp);
335 335
336 336 dadkp->dad_flcobjp = flcobjp;
337 337 mutex_init(&dadkp->dad_cmd_mutex, NULL, MUTEX_DRIVER, NULL);
338 338 dadkp->dad_cmd_count = 0;
339 339 return (FLC_INIT(flcobjp, &(dadkp->dad_com), queobjp, lkarg));
340 340 }
341 341
342 342 int
343 343 dadk_free(struct tgdk_obj *dkobjp)
344 344 {
345 345 TGDK_CLEANUP(dkobjp);
346 346 kmem_free(dkobjp, (sizeof (*dkobjp) + sizeof (struct dadk)));
347 347
348 348 return (DDI_SUCCESS);
349 349 }
350 350
351 351 void
352 352 dadk_cleanup(struct tgdk_obj *dkobjp)
353 353 {
354 354 struct dadk *dadkp;
355 355
356 356 dadkp = (struct dadk *)(dkobjp->tg_data);
357 357 if (dadkp->dad_sd)
358 358 dadkp->dad_sd->sd_private = NULL;
359 359 if (dadkp->dad_bbhobjp) {
360 360 BBH_FREE(dadkp->dad_bbhobjp);
361 361 dadkp->dad_bbhobjp = NULL;
362 362 }
363 363 if (dadkp->dad_flcobjp) {
364 364 FLC_FREE(dadkp->dad_flcobjp);
365 365 dadkp->dad_flcobjp = NULL;
366 366 }
367 367 mutex_destroy(&dadkp->dad_cmd_mutex);
368 368 }
369 369
370 370 /* ARGSUSED */
371 371 int
372 372 dadk_probe(opaque_t objp, int kmsflg)
373 373 {
374 374 struct dadk *dadkp = (struct dadk *)objp;
375 375 struct scsi_device *devp;
376 376 char name[80];
377 377
378 378 devp = dadkp->dad_sd;
379 379 if (!devp->sd_inq || (devp->sd_inq->inq_dtype == DTYPE_NOTPRESENT) ||
380 380 (devp->sd_inq->inq_dtype == DTYPE_UNKNOWN)) {
381 381 return (DDI_PROBE_FAILURE);
382 382 }
383 383
384 384 switch (devp->sd_inq->inq_dtype) {
385 385 case DTYPE_DIRECT:
386 386 dadkp->dad_ctype = DKC_DIRECT;
387 387 dadkp->dad_extp->tg_nodetype = DDI_NT_BLOCK;
388 388 dadkp->dad_extp->tg_ctype = DKC_DIRECT;
389 389 break;
390 390 case DTYPE_RODIRECT: /* eg cdrom */
391 391 dadkp->dad_ctype = DKC_CDROM;
392 392 dadkp->dad_extp->tg_rdonly = 1;
393 393 dadkp->dad_rdonly = 1;
394 394 dadkp->dad_cdrom = 1;
395 395 dadkp->dad_extp->tg_nodetype = DDI_NT_CD;
396 396 dadkp->dad_extp->tg_ctype = DKC_CDROM;
397 397 break;
398 398 case DTYPE_WORM:
399 399 case DTYPE_OPTICAL:
400 400 default:
401 401 return (DDI_PROBE_FAILURE);
402 402 }
403 403
404 404 dadkp->dad_extp->tg_rmb = dadkp->dad_rmb = devp->sd_inq->inq_rmb;
405 405
406 406 dadkp->dad_secshf = SCTRSHFT;
407 407 dadkp->dad_blkshf = 0;
408 408
409 409 /* display the device name */
410 410 (void) strcpy(name, "Vendor '");
411 411 gda_inqfill((caddr_t)devp->sd_inq->inq_vid, 8, &name[strlen(name)]);
412 412 (void) strcat(name, "' Product '");
413 413 gda_inqfill((caddr_t)devp->sd_inq->inq_pid, 16, &name[strlen(name)]);
414 414 (void) strcat(name, "'");
415 415 gda_log(devp->sd_dev, dadk_name, CE_NOTE, "!<%s>\n", name);
416 416
417 417 return (DDI_PROBE_SUCCESS);
418 418 }
419 419
420 420
421 421 /* ARGSUSED */
422 422 int
423 423 dadk_attach(opaque_t objp)
424 424 {
425 425 return (DDI_SUCCESS);
426 426 }
427 427
428 428 int
429 429 dadk_set_bbhobj(opaque_t objp, opaque_t bbhobjp)
430 430 {
431 431 struct dadk *dadkp = (struct dadk *)objp;
432 432 /* free the old bbh object */
433 433 if (dadkp->dad_bbhobjp)
434 434 BBH_FREE(dadkp->dad_bbhobjp);
435 435
436 436 /* initialize the new bbh object */
437 437 dadkp->dad_bbhobjp = bbhobjp;
438 438 BBH_INIT(bbhobjp);
439 439
440 440 return (DDI_SUCCESS);
441 441 }
442 442
443 443 /* ARGSUSED */
444 444 int
445 445 dadk_open(opaque_t objp, int flag)
446 446 {
447 447 struct dadk *dadkp = (struct dadk *)objp;
448 448 int error;
449 449 int wce;
450 450
451 451 if (!dadkp->dad_rmb) {
452 452 if (dadkp->dad_phyg.g_cap) {
453 453 FLC_START_KSTAT(dadkp->dad_flcobjp, "disk",
454 454 ddi_get_instance(CTL_DIP_DEV(dadkp->dad_ctlobjp)));
455 455 return (DDI_SUCCESS);
456 456 }
457 457 } else {
458 458 mutex_enter(&dadkp->dad_mutex);
459 459 dadkp->dad_iostate = DKIO_NONE;
460 460 cv_broadcast(&dadkp->dad_state_cv);
461 461 mutex_exit(&dadkp->dad_mutex);
462 462
463 463 if (dadk_rmb_ioctl(dadkp, DCMD_START_MOTOR, 0, 0,
464 464 DADK_SILENT) ||
465 465 dadk_rmb_ioctl(dadkp, DCMD_LOCK, 0, 0, DADK_SILENT) ||
466 466 dadk_rmb_ioctl(dadkp, DCMD_UPDATE_GEOM, 0, 0,
467 467 DADK_SILENT)) {
468 468 return (DDI_FAILURE);
469 469 }
470 470
471 471 mutex_enter(&dadkp->dad_mutex);
472 472 dadkp->dad_iostate = DKIO_INSERTED;
473 473 cv_broadcast(&dadkp->dad_state_cv);
474 474 mutex_exit(&dadkp->dad_mutex);
475 475 }
476 476
477 477 /*
478 478 * get write cache enable state
479 479 * If there is an error, must assume that write cache
480 480 * is enabled.
481 481 * NOTE: Since there is currently no Solaris mechanism to
482 482 * change the state of the Write Cache Enable feature,
483 483 * this code just checks the value of the WCE bit
484 484 * obtained at device init time. If a mechanism
485 485 * is added to the driver to change WCE, dad_wce
486 486 * must be updated appropriately.
487 487 */
488 488 error = dadk_ctl_ioctl(dadkp, DIOCTL_GETWCE,
489 489 (uintptr_t)&wce, FKIOCTL | FNATIVE);
490 490 mutex_enter(&dadkp->dad_mutex);
491 491 dadkp->dad_wce = (error != 0) || (wce != 0);
492 492 mutex_exit(&dadkp->dad_mutex);
493 493
494 494 /* logical disk geometry */
495 495 (void) dadk_ctl_ioctl(dadkp, DIOCTL_GETGEOM,
496 496 (uintptr_t)&dadkp->dad_logg, FKIOCTL | FNATIVE);
497 497 if (dadkp->dad_logg.g_cap == 0)
498 498 return (DDI_FAILURE);
499 499
500 500 /* get physical disk geometry */
501 501 (void) dadk_ctl_ioctl(dadkp, DIOCTL_GETPHYGEOM,
502 502 (uintptr_t)&dadkp->dad_phyg, FKIOCTL | FNATIVE);
503 503 if (dadkp->dad_phyg.g_cap == 0)
504 504 return (DDI_FAILURE);
505 505
506 506 dadk_setcap(dadkp);
507 507
508 508 dadk_create_errstats(dadkp,
509 509 ddi_get_instance(CTL_DIP_DEV(dadkp->dad_ctlobjp)));
510 510
511 511 /* start profiling */
512 512 FLC_START_KSTAT(dadkp->dad_flcobjp, "disk",
513 513 ddi_get_instance(CTL_DIP_DEV(dadkp->dad_ctlobjp)));
514 514
515 515 return (DDI_SUCCESS);
516 516 }
517 517
518 518 static void
519 519 dadk_setcap(struct dadk *dadkp)
520 520 {
521 521 int totsize;
522 522 int i;
523 523
524 524 totsize = dadkp->dad_phyg.g_secsiz;
525 525
526 526 if (totsize == 0) {
527 527 if (dadkp->dad_cdrom) {
528 528 totsize = 2048;
529 529 } else {
530 530 totsize = NBPSCTR;
531 531 }
532 532 } else {
533 533 /* Round down sector size to multiple of 512B */
534 534 totsize &= ~(NBPSCTR-1);
535 535 }
536 536 dadkp->dad_phyg.g_secsiz = totsize;
537 537
538 538 /* set sec,block shift factor - (512->0, 1024->1, 2048->2, etc.) */
539 539 totsize >>= SCTRSHFT;
540 540 for (i = 0; totsize != 1; i++, totsize >>= 1)
541 541 ;
542 542 dadkp->dad_blkshf = i;
543 543 dadkp->dad_secshf = i + SCTRSHFT;
544 544 }
545 545
546 546
547 547 static void
548 548 dadk_create_errstats(struct dadk *dadkp, int instance)
549 549 {
550 550 dadk_errstats_t *dep;
551 551 char kstatname[KSTAT_STRLEN];
552 552 dadk_ioc_string_t dadk_ioc_string;
553 553
554 554 if (dadkp->dad_errstats)
555 555 return;
556 556
557 557 (void) sprintf(kstatname, "cmdk%d,error", instance);
558 558 dadkp->dad_errstats = kstat_create("cmdkerror", instance,
559 559 kstatname, "device_error", KSTAT_TYPE_NAMED,
560 560 sizeof (dadk_errstats_t) / sizeof (kstat_named_t),
561 561 KSTAT_FLAG_PERSISTENT);
562 562
563 563 if (!dadkp->dad_errstats)
564 564 return;
565 565
566 566 dep = (dadk_errstats_t *)dadkp->dad_errstats->ks_data;
567 567
568 568 kstat_named_init(&dep->dadk_softerrs,
569 569 "Soft Errors", KSTAT_DATA_UINT32);
570 570 kstat_named_init(&dep->dadk_harderrs,
571 571 "Hard Errors", KSTAT_DATA_UINT32);
572 572 kstat_named_init(&dep->dadk_transerrs,
573 573 "Transport Errors", KSTAT_DATA_UINT32);
574 574 kstat_named_init(&dep->dadk_model,
575 575 "Model", KSTAT_DATA_CHAR);
576 576 kstat_named_init(&dep->dadk_revision,
577 577 "Revision", KSTAT_DATA_CHAR);
578 578 kstat_named_init(&dep->dadk_serial,
579 579 "Serial No", KSTAT_DATA_CHAR);
580 580 kstat_named_init(&dep->dadk_capacity,
581 581 "Size", KSTAT_DATA_ULONGLONG);
582 582 kstat_named_init(&dep->dadk_rq_media_err,
583 583 "Media Error", KSTAT_DATA_UINT32);
584 584 kstat_named_init(&dep->dadk_rq_ntrdy_err,
585 585 "Device Not Ready", KSTAT_DATA_UINT32);
586 586 kstat_named_init(&dep->dadk_rq_nodev_err,
587 587 "No Device", KSTAT_DATA_UINT32);
588 588 kstat_named_init(&dep->dadk_rq_recov_err,
589 589 "Recoverable", KSTAT_DATA_UINT32);
590 590 kstat_named_init(&dep->dadk_rq_illrq_err,
591 591 "Illegal Request", KSTAT_DATA_UINT32);
592 592
593 593 dadkp->dad_errstats->ks_private = dep;
594 594 dadkp->dad_errstats->ks_update = nulldev;
595 595 kstat_install(dadkp->dad_errstats);
596 596
597 597 /* get model */
598 598 dep->dadk_model.value.c[0] = 0;
599 599 dadk_ioc_string.is_buf = &dep->dadk_model.value.c[0];
600 600 dadk_ioc_string.is_size = sizeof (dep->dadk_model.value.c);
601 601 (void) dadk_ctl_ioctl(dadkp, DIOCTL_GETMODEL,
602 602 (uintptr_t)&dadk_ioc_string, FKIOCTL | FNATIVE);
603 603
604 604 /* get serial */
605 605 dep->dadk_serial.value.c[0] = 0;
606 606 dadk_ioc_string.is_buf = &dep->dadk_serial.value.c[0];
607 607 dadk_ioc_string.is_size = sizeof (dep->dadk_serial.value.c);
608 608 (void) dadk_ctl_ioctl(dadkp, DIOCTL_GETSERIAL,
609 609 (uintptr_t)&dadk_ioc_string, FKIOCTL | FNATIVE);
610 610
611 611 /* Get revision */
612 612 dep->dadk_revision.value.c[0] = 0;
613 613
614 614 /* Get capacity */
615 615
616 616 dep->dadk_capacity.value.ui64 =
617 617 (uint64_t)dadkp->dad_logg.g_cap *
618 618 (uint64_t)dadkp->dad_logg.g_secsiz;
619 619 }
620 620
621 621
622 622 int
623 623 dadk_close(opaque_t objp)
624 624 {
625 625 struct dadk *dadkp = (struct dadk *)objp;
626 626
627 627 if (dadkp->dad_rmb) {
628 628 (void) dadk_rmb_ioctl(dadkp, DCMD_STOP_MOTOR, 0, 0,
629 629 DADK_SILENT);
630 630 (void) dadk_rmb_ioctl(dadkp, DCMD_UNLOCK, 0, 0, DADK_SILENT);
631 631 }
632 632 FLC_STOP_KSTAT(dadkp->dad_flcobjp);
633 633
634 634 dadk_destroy_errstats(dadkp);
635 635
636 636 return (DDI_SUCCESS);
637 637 }
638 638
639 639 static void
640 640 dadk_destroy_errstats(struct dadk *dadkp)
641 641 {
642 642 if (!dadkp->dad_errstats)
643 643 return;
644 644
645 645 kstat_delete(dadkp->dad_errstats);
646 646 dadkp->dad_errstats = NULL;
647 647 }
648 648
649 649
650 650 int
651 651 dadk_strategy(opaque_t objp, struct buf *bp)
652 652 {
653 653 struct dadk *dadkp = (struct dadk *)objp;
654 654
655 655 if (dadkp->dad_rdonly && !(bp->b_flags & B_READ)) {
656 656 bioerror(bp, EROFS);
657 657 return (DDI_FAILURE);
658 658 }
659 659
660 660 if (bp->b_bcount & (dadkp->DAD_SECSIZ-1)) {
661 661 bioerror(bp, ENXIO);
662 662 return (DDI_FAILURE);
663 663 }
664 664
665 665 SET_BP_SEC(bp, (LBLK2SEC(GET_BP_SEC(bp), dadkp->dad_blkshf)));
666 666 mutex_enter(&dadkp->dad_cmd_mutex);
667 667 dadkp->dad_cmd_count++;
668 668 mutex_exit(&dadkp->dad_cmd_mutex);
669 669 FLC_ENQUE(dadkp->dad_flcobjp, bp);
670 670
671 671 return (DDI_SUCCESS);
672 672 }
673 673
674 674 int
675 675 dadk_dump(opaque_t objp, struct buf *bp)
676 676 {
677 677 struct dadk *dadkp = (struct dadk *)objp;
678 678 struct cmpkt *pktp;
679 679
680 680 if (dadkp->dad_rdonly) {
681 681 bioerror(bp, EROFS);
682 682 return (DDI_FAILURE);
683 683 }
684 684
685 685 if (bp->b_bcount & (dadkp->DAD_SECSIZ-1)) {
686 686 bioerror(bp, ENXIO);
687 687 return (DDI_FAILURE);
688 688 }
689 689
690 690 SET_BP_SEC(bp, (LBLK2SEC(GET_BP_SEC(bp), dadkp->dad_blkshf)));
691 691
692 692 pktp = dadk_pktprep(dadkp, NULL, bp, dadk_polldone, NULL, NULL);
693 693 if (!pktp) {
694 694 cmn_err(CE_WARN, "no resources for dumping");
695 695 bioerror(bp, EIO);
696 696 return (DDI_FAILURE);
697 697 }
698 698 pktp->cp_flags |= CPF_NOINTR;
699 699
700 700 (void) dadk_ioprep(dadkp, pktp);
701 701 dadk_transport(dadkp, bp);
702 702 pktp->cp_byteleft -= pktp->cp_bytexfer;
703 703
704 704 while (geterror(bp) == 0 && pktp->cp_byteleft != 0) {
705 705 (void) dadk_iosetup(dadkp, pktp);
706 706 dadk_transport(dadkp, bp);
707 707 pktp->cp_byteleft -= pktp->cp_bytexfer;
708 708 }
709 709
710 710 if (pktp->cp_private)
711 711 BBH_FREEHANDLE(dadkp->dad_bbhobjp, pktp->cp_private);
712 712 gda_free(dadkp->dad_ctlobjp, pktp, NULL);
713 713 return (DDI_SUCCESS);
714 714 }
715 715
716 716 /* ARGSUSED */
717 717 int
718 718 dadk_ioctl(opaque_t objp, dev_t dev, int cmd, intptr_t arg, int flag,
719 719 cred_t *cred_p, int *rval_p)
720 720 {
721 721 struct dadk *dadkp = (struct dadk *)objp;
722 722
723 723 switch (cmd) {
724 724 case DKIOCGETDEF:
725 725 {
726 726 struct buf *bp;
727 727 int err, head;
728 728 unsigned char *secbuf;
729 729 STRUCT_DECL(defect_header, adh);
730 730
731 731 STRUCT_INIT(adh, flag & FMODELS);
732 732
733 733 /*
734 734 * copyin header ....
735 735 * yields head number and buffer address
736 736 */
737 737 if (ddi_copyin((caddr_t)arg, STRUCT_BUF(adh), STRUCT_SIZE(adh),
738 738 flag))
739 739 return (EFAULT);
740 740 head = STRUCT_FGET(adh, head);
741 741 if (head < 0 || head >= dadkp->dad_phyg.g_head)
742 742 return (ENXIO);
743 743 secbuf = kmem_zalloc(NBPSCTR, KM_SLEEP);
744 744 if (!secbuf)
745 745 return (ENOMEM);
746 746 bp = getrbuf(KM_SLEEP);
747 747 if (!bp) {
748 748 kmem_free(secbuf, NBPSCTR);
749 749 return (ENOMEM);
750 750 }
751 751
752 752 bp->b_edev = dev;
753 753 bp->b_dev = cmpdev(dev);
754 754 bp->b_flags = B_BUSY;
755 755 bp->b_resid = 0;
756 756 bp->b_bcount = NBPSCTR;
757 757 bp->b_un.b_addr = (caddr_t)secbuf;
758 758 bp->b_blkno = head; /* I had to put it somwhere! */
759 759 bp->b_forw = (struct buf *)dadkp;
760 760 bp->b_back = (struct buf *)DCMD_GETDEF;
761 761
762 762 mutex_enter(&dadkp->dad_cmd_mutex);
763 763 dadkp->dad_cmd_count++;
764 764 mutex_exit(&dadkp->dad_cmd_mutex);
765 765 FLC_ENQUE(dadkp->dad_flcobjp, bp);
766 766 err = biowait(bp);
767 767 if (!err) {
768 768 if (ddi_copyout((caddr_t)secbuf,
769 769 STRUCT_FGETP(adh, buffer), NBPSCTR, flag))
770 770 err = ENXIO;
771 771 }
772 772 kmem_free(secbuf, NBPSCTR);
773 773 freerbuf(bp);
774 774 return (err);
775 775 }
776 776 case DIOCTL_RWCMD:
777 777 {
778 778 struct dadkio_rwcmd *rwcmdp;
779 779 int status, rw;
780 780
781 781 /*
782 782 * copied in by cmdk and, if necessary, converted to the
783 783 * correct datamodel
784 784 */
785 785 rwcmdp = (struct dadkio_rwcmd *)(intptr_t)arg;
786 786
787 787 /*
788 788 * handle the complex cases here; we pass these
789 789 * through to the driver, which will queue them and
790 790 * handle the requests asynchronously. The simpler
791 791 * cases ,which can return immediately, fail here, and
792 792 * the request reverts to the dadk_ioctl routine, while
793 793 * will reroute them directly to the ata driver.
794 794 */
795 795 switch (rwcmdp->cmd) {
796 796 case DADKIO_RWCMD_READ :
797 797 /*FALLTHROUGH*/
798 798 case DADKIO_RWCMD_WRITE:
799 799 rw = ((rwcmdp->cmd == DADKIO_RWCMD_WRITE) ?
800 800 B_WRITE : B_READ);
801 801 status = dadk_dk_buf_setup(dadkp,
802 802 (opaque_t)rwcmdp, dev, ((flag &FKIOCTL) ?
803 803 UIO_SYSSPACE : UIO_USERSPACE), rw);
804 804 return (status);
805 805 default:
806 806 return (EINVAL);
807 807 }
808 808 }
809 809 case DKIOC_UPDATEFW:
810 810
811 811 /*
812 812 * Require PRIV_ALL privilege to invoke DKIOC_UPDATEFW
813 813 * to protect the firmware update from malicious use
814 814 */
815 815 if (PRIV_POLICY(cred_p, PRIV_ALL, B_FALSE, EPERM, NULL) != 0)
816 816 return (EPERM);
817 817 else
818 818 return (dadk_ctl_ioctl(dadkp, cmd, arg, flag));
819 819
820 820 case DKIOCFLUSHWRITECACHE:
821 821 {
822 822 struct buf *bp;
823 823 int err = 0;
824 824 struct dk_callback *dkc = (struct dk_callback *)arg;
825 825 struct cmpkt *pktp;
826 826 int is_sync = 1;
827 827
828 828 mutex_enter(&dadkp->dad_mutex);
829 829 if (dadkp->dad_noflush || ! dadkp->dad_wce) {
830 830 err = dadkp->dad_noflush ? ENOTSUP : 0;
831 831 mutex_exit(&dadkp->dad_mutex);
832 832 /*
833 833 * If a callback was requested: a
834 834 * callback will always be done if the
835 835 * caller saw the DKIOCFLUSHWRITECACHE
836 836 * ioctl return 0, and never done if the
837 837 * caller saw the ioctl return an error.
838 838 */
839 839 if ((flag & FKIOCTL) && dkc != NULL &&
840 840 dkc->dkc_callback != NULL) {
841 841 (*dkc->dkc_callback)(dkc->dkc_cookie,
842 842 err);
843 843 /*
844 844 * Did callback and reported error.
845 845 * Since we did a callback, ioctl
846 846 * should return 0.
847 847 */
848 848 err = 0;
849 849 }
850 850 return (err);
851 851 }
852 852 mutex_exit(&dadkp->dad_mutex);
853 853
854 854 bp = getrbuf(KM_SLEEP);
855 855
856 856 bp->b_edev = dev;
857 857 bp->b_dev = cmpdev(dev);
858 858 bp->b_flags = B_BUSY;
859 859 bp->b_resid = 0;
860 860 bp->b_bcount = 0;
861 861 SET_BP_SEC(bp, 0);
862 862
863 863 if ((flag & FKIOCTL) && dkc != NULL &&
864 864 dkc->dkc_callback != NULL) {
865 865 struct dk_callback *dkc2 =
866 866 (struct dk_callback *)kmem_zalloc(
867 867 sizeof (struct dk_callback), KM_SLEEP);
868 868
869 869 bcopy(dkc, dkc2, sizeof (*dkc2));
870 870 bp->b_private = dkc2;
871 871 bp->b_iodone = dadk_flushdone;
872 872 is_sync = 0;
873 873 }
874 874
875 875 /*
876 876 * Setup command pkt
877 877 * dadk_pktprep() can't fail since DDI_DMA_SLEEP set
878 878 */
879 879 pktp = dadk_pktprep(dadkp, NULL, bp,
880 880 dadk_iodone, DDI_DMA_SLEEP, NULL);
881 881
882 882 pktp->cp_time = DADK_FLUSH_CACHE_TIME;
883 883
884 884 *((char *)(pktp->cp_cdbp)) = DCMD_FLUSH_CACHE;
885 885 pktp->cp_byteleft = 0;
886 886 pktp->cp_private = NULL;
887 887 pktp->cp_secleft = 0;
888 888 pktp->cp_srtsec = -1;
889 889 pktp->cp_bytexfer = 0;
890 890
891 891 CTL_IOSETUP(dadkp->dad_ctlobjp, pktp);
892 892
893 893 mutex_enter(&dadkp->dad_cmd_mutex);
894 894 dadkp->dad_cmd_count++;
895 895 mutex_exit(&dadkp->dad_cmd_mutex);
896 896 FLC_ENQUE(dadkp->dad_flcobjp, bp);
897 897
898 898 if (is_sync) {
899 899 err = biowait(bp);
900 900 freerbuf(bp);
901 901 }
902 902 return (err);
903 903 }
904 904 default:
905 905 if (!dadkp->dad_rmb)
906 906 return (dadk_ctl_ioctl(dadkp, cmd, arg, flag));
907 907 }
908 908
909 909 switch (cmd) {
910 910 case CDROMSTOP:
911 911 return (dadk_rmb_ioctl(dadkp, DCMD_STOP_MOTOR, 0,
912 912 0, DADK_SILENT));
913 913 case CDROMSTART:
914 914 return (dadk_rmb_ioctl(dadkp, DCMD_START_MOTOR, 0,
915 915 0, DADK_SILENT));
916 916 case DKIOCLOCK:
917 917 return (dadk_rmb_ioctl(dadkp, DCMD_LOCK, 0, 0, DADK_SILENT));
918 918 case DKIOCUNLOCK:
919 919 return (dadk_rmb_ioctl(dadkp, DCMD_UNLOCK, 0, 0, DADK_SILENT));
920 920 case DKIOCEJECT:
921 921 case CDROMEJECT:
922 922 {
923 923 int ret;
924 924
925 925 if (ret = dadk_rmb_ioctl(dadkp, DCMD_UNLOCK, 0, 0,
926 926 DADK_SILENT)) {
927 927 return (ret);
928 928 }
929 929 if (ret = dadk_rmb_ioctl(dadkp, DCMD_EJECT, 0, 0,
930 930 DADK_SILENT)) {
931 931 return (ret);
932 932 }
933 933 mutex_enter(&dadkp->dad_mutex);
934 934 dadkp->dad_iostate = DKIO_EJECTED;
935 935 cv_broadcast(&dadkp->dad_state_cv);
936 936 mutex_exit(&dadkp->dad_mutex);
937 937
938 938 return (0);
939 939
940 940 }
941 941 default:
942 942 return (ENOTTY);
943 943 /*
944 944 * cdrom audio commands
945 945 */
946 946 case CDROMPAUSE:
947 947 cmd = DCMD_PAUSE;
948 948 break;
949 949 case CDROMRESUME:
950 950 cmd = DCMD_RESUME;
951 951 break;
952 952 case CDROMPLAYMSF:
953 953 cmd = DCMD_PLAYMSF;
954 954 break;
955 955 case CDROMPLAYTRKIND:
956 956 cmd = DCMD_PLAYTRKIND;
957 957 break;
958 958 case CDROMREADTOCHDR:
959 959 cmd = DCMD_READTOCHDR;
960 960 break;
961 961 case CDROMREADTOCENTRY:
962 962 cmd = DCMD_READTOCENT;
963 963 break;
964 964 case CDROMVOLCTRL:
965 965 cmd = DCMD_VOLCTRL;
966 966 break;
967 967 case CDROMSUBCHNL:
968 968 cmd = DCMD_SUBCHNL;
969 969 break;
970 970 case CDROMREADMODE2:
971 971 cmd = DCMD_READMODE2;
972 972 break;
973 973 case CDROMREADMODE1:
974 974 cmd = DCMD_READMODE1;
975 975 break;
976 976 case CDROMREADOFFSET:
977 977 cmd = DCMD_READOFFSET;
978 978 break;
979 979 }
980 980 return (dadk_rmb_ioctl(dadkp, cmd, arg, flag, 0));
981 981 }
982 982
983 983 int
984 984 dadk_flushdone(struct buf *bp)
985 985 {
986 986 struct dk_callback *dkc = bp->b_private;
987 987
988 988 ASSERT(dkc != NULL && dkc->dkc_callback != NULL);
989 989
990 990 (*dkc->dkc_callback)(dkc->dkc_cookie, geterror(bp));
991 991
992 992 kmem_free(dkc, sizeof (*dkc));
993 993 freerbuf(bp);
994 994 return (0);
995 995 }
996 996
997 997 int
998 998 dadk_getphygeom(opaque_t objp, struct tgdk_geom *dkgeom_p)
999 999 {
1000 1000 struct dadk *dadkp = (struct dadk *)objp;
1001 1001
1002 1002 bcopy((caddr_t)&dadkp->dad_phyg, (caddr_t)dkgeom_p,
1003 1003 sizeof (struct tgdk_geom));
1004 1004 return (DDI_SUCCESS);
1005 1005 }
1006 1006
1007 1007 int
1008 1008 dadk_getgeom(opaque_t objp, struct tgdk_geom *dkgeom_p)
1009 1009 {
1010 1010 struct dadk *dadkp = (struct dadk *)objp;
1011 1011 bcopy((caddr_t)&dadkp->dad_logg, (caddr_t)dkgeom_p,
1012 1012 sizeof (struct tgdk_geom));
1013 1013 return (DDI_SUCCESS);
1014 1014 }
1015 1015
1016 1016 int
1017 1017 dadk_setgeom(opaque_t objp, struct tgdk_geom *dkgeom_p)
1018 1018 {
1019 1019 struct dadk *dadkp = (struct dadk *)objp;
1020 1020
1021 1021 dadkp->dad_logg.g_cyl = dkgeom_p->g_cyl;
1022 1022 dadkp->dad_logg.g_head = dkgeom_p->g_head;
1023 1023 dadkp->dad_logg.g_sec = dkgeom_p->g_sec;
1024 1024 dadkp->dad_logg.g_cap = dkgeom_p->g_cap;
1025 1025 return (DDI_SUCCESS);
1026 1026 }
1027 1027
1028 1028
1029 1029 tgdk_iob_handle
1030 1030 dadk_iob_alloc(opaque_t objp, daddr_t blkno, ssize_t xfer, int kmsflg)
1031 1031 {
1032 1032 struct dadk *dadkp = (struct dadk *)objp;
1033 1033 struct buf *bp;
1034 1034 struct tgdk_iob *iobp;
1035 1035 size_t rlen;
1036 1036
1037 1037 iobp = kmem_zalloc(sizeof (*iobp), kmsflg);
1038 1038 if (iobp == NULL)
1039 1039 return (NULL);
1040 1040 if ((bp = getrbuf(kmsflg)) == NULL) {
1041 1041 kmem_free(iobp, sizeof (*iobp));
1042 1042 return (NULL);
↓ open down ↓ |
1042 lines elided |
↑ open up ↑ |
1043 1043 }
1044 1044
1045 1045 iobp->b_psec = LBLK2SEC(blkno, dadkp->dad_blkshf);
1046 1046 iobp->b_pbyteoff = (blkno & ((1<<dadkp->dad_blkshf) - 1)) << SCTRSHFT;
1047 1047 iobp->b_pbytecnt = ((iobp->b_pbyteoff + xfer + dadkp->DAD_SECSIZ - 1)
1048 1048 >> dadkp->dad_secshf) << dadkp->dad_secshf;
1049 1049
1050 1050 bp->b_un.b_addr = 0;
1051 1051 /*
1052 1052 * use i_ddi_mem_alloc() for now until we have an interface to allocate
1053 - * memory for DMA which doesn't require a DMA handle. ddi_iopb_alloc()
1054 - * is obsolete and we want more flexibility in controlling the DMA
1055 - * address constraints..
1053 + * memory for DMA which doesn't require a DMA handle.
1056 1054 */
1057 1055 if (i_ddi_mem_alloc((dadkp->dad_sd)->sd_dev, &dadk_alloc_attr,
1058 1056 (size_t)iobp->b_pbytecnt, ((kmsflg == KM_SLEEP) ? 1 : 0), 0, NULL,
1059 1057 &bp->b_un.b_addr, &rlen, NULL) != DDI_SUCCESS) {
1060 1058 freerbuf(bp);
1061 1059 kmem_free(iobp, sizeof (*iobp));
1062 1060 return (NULL);
1063 1061 }
1064 1062 iobp->b_flag |= IOB_BPALLOC | IOB_BPBUFALLOC;
1065 1063 iobp->b_bp = bp;
1066 1064 iobp->b_lblk = blkno;
1067 1065 iobp->b_xfer = xfer;
1068 1066 iobp->b_lblk = blkno;
1069 1067 iobp->b_xfer = xfer;
1070 1068 return (iobp);
1071 1069 }
1072 1070
1073 1071 /* ARGSUSED */
1074 1072 int
1075 1073 dadk_iob_free(opaque_t objp, struct tgdk_iob *iobp)
1076 1074 {
1077 1075 struct buf *bp;
1078 1076
1079 1077 if (iobp) {
1080 1078 if (iobp->b_bp && (iobp->b_flag & IOB_BPALLOC)) {
1081 1079 bp = iobp->b_bp;
1082 1080 if (bp->b_un.b_addr && (iobp->b_flag & IOB_BPBUFALLOC))
1083 1081 i_ddi_mem_free((caddr_t)bp->b_un.b_addr, NULL);
1084 1082 freerbuf(bp);
1085 1083 }
1086 1084 kmem_free(iobp, sizeof (*iobp));
1087 1085 }
1088 1086 return (DDI_SUCCESS);
1089 1087 }
1090 1088
1091 1089 /* ARGSUSED */
1092 1090 caddr_t
1093 1091 dadk_iob_htoc(opaque_t objp, struct tgdk_iob *iobp)
1094 1092 {
1095 1093 return (iobp->b_bp->b_un.b_addr+iobp->b_pbyteoff);
1096 1094 }
1097 1095
1098 1096
1099 1097 caddr_t
1100 1098 dadk_iob_xfer(opaque_t objp, struct tgdk_iob *iobp, int rw)
1101 1099 {
1102 1100 struct dadk *dadkp = (struct dadk *)objp;
1103 1101 struct buf *bp;
1104 1102 int err;
1105 1103
1106 1104 bp = iobp->b_bp;
1107 1105 if (dadkp->dad_rdonly && !(rw & B_READ)) {
1108 1106 bioerror(bp, EROFS);
1109 1107 return (NULL);
1110 1108 }
1111 1109
1112 1110 bp->b_flags |= (B_BUSY | rw);
1113 1111 bp->b_bcount = iobp->b_pbytecnt;
1114 1112 SET_BP_SEC(bp, iobp->b_psec);
1115 1113 bp->av_back = (struct buf *)0;
1116 1114 bp->b_resid = 0;
1117 1115
1118 1116 /* call flow control */
1119 1117 mutex_enter(&dadkp->dad_cmd_mutex);
1120 1118 dadkp->dad_cmd_count++;
1121 1119 mutex_exit(&dadkp->dad_cmd_mutex);
1122 1120 FLC_ENQUE(dadkp->dad_flcobjp, bp);
1123 1121 err = biowait(bp);
1124 1122
1125 1123 bp->b_bcount = iobp->b_xfer;
1126 1124 bp->b_flags &= ~(B_DONE|B_BUSY);
1127 1125
1128 1126 if (err)
1129 1127 return (NULL);
1130 1128
1131 1129 return (bp->b_un.b_addr+iobp->b_pbyteoff);
1132 1130 }
1133 1131
1134 1132 static void
1135 1133 dadk_transport(opaque_t com_data, struct buf *bp)
1136 1134 {
1137 1135 struct dadk *dadkp = (struct dadk *)com_data;
1138 1136
1139 1137 if (CTL_TRANSPORT(dadkp->dad_ctlobjp, GDA_BP_PKT(bp)) ==
1140 1138 CTL_SEND_SUCCESS)
1141 1139 return;
1142 1140 dadk_restart((void*)GDA_BP_PKT(bp));
1143 1141 }
1144 1142
1145 1143 static int
1146 1144 dadk_pkt(opaque_t com_data, struct buf *bp, int (*func)(caddr_t), caddr_t arg)
1147 1145 {
1148 1146 struct cmpkt *pktp;
1149 1147 struct dadk *dadkp = (struct dadk *)com_data;
1150 1148
1151 1149 if (GDA_BP_PKT(bp))
1152 1150 return (DDI_SUCCESS);
1153 1151
1154 1152 pktp = dadk_pktprep(dadkp, NULL, bp, dadk_iodone, func, arg);
1155 1153 if (!pktp)
1156 1154 return (DDI_FAILURE);
1157 1155
1158 1156 return (dadk_ioprep(dadkp, pktp));
1159 1157 }
1160 1158
1161 1159 /*
1162 1160 * Read, Write preparation
1163 1161 */
1164 1162 static int
1165 1163 dadk_ioprep(struct dadk *dadkp, struct cmpkt *pktp)
1166 1164 {
1167 1165 struct buf *bp;
1168 1166
1169 1167 bp = pktp->cp_bp;
1170 1168 if (bp->b_forw == (struct buf *)dadkp)
1171 1169 *((char *)(pktp->cp_cdbp)) = (char)(intptr_t)bp->b_back;
1172 1170
1173 1171 else if (bp->b_flags & B_READ)
1174 1172 *((char *)(pktp->cp_cdbp)) = DCMD_READ;
1175 1173 else
1176 1174 *((char *)(pktp->cp_cdbp)) = DCMD_WRITE;
1177 1175 pktp->cp_byteleft = bp->b_bcount;
1178 1176
1179 1177 /* setup the bad block list handle */
1180 1178 pktp->cp_private = BBH_GETHANDLE(dadkp->dad_bbhobjp, bp);
1181 1179 return (dadk_iosetup(dadkp, pktp));
1182 1180 }
1183 1181
1184 1182 static int
1185 1183 dadk_iosetup(struct dadk *dadkp, struct cmpkt *pktp)
1186 1184 {
1187 1185 struct buf *bp;
1188 1186 bbh_cookie_t bbhckp;
1189 1187 int seccnt;
1190 1188
1191 1189 seccnt = pktp->cp_bytexfer >> dadkp->dad_secshf;
1192 1190 pktp->cp_secleft -= seccnt;
1193 1191
1194 1192 if (pktp->cp_secleft) {
1195 1193 pktp->cp_srtsec += seccnt;
1196 1194 } else {
1197 1195 /* get the first cookie from the bad block list */
1198 1196 if (!pktp->cp_private) {
1199 1197 bp = pktp->cp_bp;
1200 1198 pktp->cp_srtsec = GET_BP_SEC(bp);
1201 1199 pktp->cp_secleft = (bp->b_bcount >> dadkp->dad_secshf);
1202 1200 } else {
1203 1201 bbhckp = BBH_HTOC(dadkp->dad_bbhobjp,
1204 1202 pktp->cp_private);
1205 1203 pktp->cp_srtsec = BBH_GETCK_SECTOR(dadkp->dad_bbhobjp,
1206 1204 bbhckp);
1207 1205 pktp->cp_secleft = BBH_GETCK_SECLEN(dadkp->dad_bbhobjp,
1208 1206 bbhckp);
1209 1207 }
1210 1208 }
1211 1209
1212 1210 pktp->cp_bytexfer = pktp->cp_secleft << dadkp->dad_secshf;
1213 1211
1214 1212 if (CTL_IOSETUP(dadkp->dad_ctlobjp, pktp)) {
1215 1213 return (DDI_SUCCESS);
1216 1214 } else {
1217 1215 return (DDI_FAILURE);
1218 1216 }
1219 1217
1220 1218
1221 1219
1222 1220
1223 1221 }
1224 1222
1225 1223 static struct cmpkt *
1226 1224 dadk_pktprep(struct dadk *dadkp, struct cmpkt *in_pktp, struct buf *bp,
1227 1225 void (*cb_func)(struct buf *), int (*func)(caddr_t), caddr_t arg)
1228 1226 {
1229 1227 struct cmpkt *pktp;
1230 1228
1231 1229 pktp = gda_pktprep(dadkp->dad_ctlobjp, in_pktp, (opaque_t)bp, func,
1232 1230 arg);
1233 1231
1234 1232 if (pktp) {
1235 1233 pktp->cp_callback = dadk_pktcb;
1236 1234 pktp->cp_time = DADK_IO_TIME;
1237 1235 pktp->cp_flags = 0;
1238 1236 pktp->cp_iodone = cb_func;
1239 1237 pktp->cp_dev_private = (opaque_t)dadkp;
1240 1238
1241 1239 }
1242 1240
1243 1241 return (pktp);
1244 1242 }
1245 1243
1246 1244
1247 1245 static void
1248 1246 dadk_restart(void *vpktp)
1249 1247 {
1250 1248 struct cmpkt *pktp = (struct cmpkt *)vpktp;
1251 1249
1252 1250 if (dadk_ioretry(pktp, QUE_COMMAND) == JUST_RETURN)
1253 1251 return;
1254 1252 pktp->cp_iodone(pktp->cp_bp);
1255 1253 }
1256 1254
1257 1255 static int
1258 1256 dadk_ioretry(struct cmpkt *pktp, int action)
1259 1257 {
1260 1258 struct buf *bp;
1261 1259 struct dadk *dadkp = PKT2DADK(pktp);
1262 1260
1263 1261 switch (action) {
1264 1262 case QUE_COMMAND:
1265 1263 if (pktp->cp_retry++ < DADK_RETRY_COUNT) {
1266 1264 CTL_IOSETUP(dadkp->dad_ctlobjp, pktp);
1267 1265 if (CTL_TRANSPORT(dadkp->dad_ctlobjp, pktp) ==
1268 1266 CTL_SEND_SUCCESS) {
1269 1267 return (JUST_RETURN);
1270 1268 }
1271 1269 gda_log(dadkp->dad_sd->sd_dev, dadk_name,
1272 1270 CE_WARN, "transport of command fails\n");
1273 1271 } else
1274 1272 gda_log(dadkp->dad_sd->sd_dev,
1275 1273 dadk_name, CE_WARN,
1276 1274 "exceeds maximum number of retries\n");
1277 1275 bioerror(pktp->cp_bp, ENXIO);
1278 1276 /*FALLTHROUGH*/
1279 1277 case COMMAND_DONE_ERROR:
1280 1278 bp = pktp->cp_bp;
1281 1279 bp->b_resid += pktp->cp_byteleft - pktp->cp_bytexfer +
1282 1280 pktp->cp_resid;
1283 1281 if (geterror(bp) == 0) {
1284 1282 if ((*((char *)(pktp->cp_cdbp)) == DCMD_FLUSH_CACHE) &&
1285 1283 (pktp->cp_dev_private == (opaque_t)dadkp) &&
1286 1284 ((int)(*(char *)pktp->cp_scbp) == DERR_ABORT)) {
1287 1285 /*
1288 1286 * Flag "unimplemented" responses for
1289 1287 * DCMD_FLUSH_CACHE as ENOTSUP
1290 1288 */
1291 1289 bioerror(bp, ENOTSUP);
1292 1290 mutex_enter(&dadkp->dad_mutex);
1293 1291 dadkp->dad_noflush = 1;
1294 1292 mutex_exit(&dadkp->dad_mutex);
1295 1293 } else {
1296 1294 bioerror(bp, EIO);
1297 1295 }
1298 1296 }
1299 1297 /*FALLTHROUGH*/
1300 1298 case COMMAND_DONE:
1301 1299 default:
1302 1300 return (COMMAND_DONE);
1303 1301 }
1304 1302 }
1305 1303
1306 1304
1307 1305 static void
1308 1306 dadk_pktcb(struct cmpkt *pktp)
1309 1307 {
1310 1308 int action;
1311 1309 struct dadkio_rwcmd *rwcmdp;
1312 1310
1313 1311 rwcmdp = (struct dadkio_rwcmd *)pktp->cp_passthru; /* ioctl packet */
1314 1312
1315 1313 if (pktp->cp_reason == CPS_SUCCESS) {
1316 1314 if (rwcmdp && (rwcmdp != (opaque_t)DADK_SILENT))
1317 1315 rwcmdp->status.status = DADKIO_STAT_NO_ERROR;
1318 1316 pktp->cp_iodone(pktp->cp_bp);
1319 1317 return;
1320 1318 }
1321 1319
1322 1320 if (rwcmdp && (rwcmdp != (opaque_t)DADK_SILENT)) {
1323 1321 if (pktp->cp_reason == CPS_CHKERR)
1324 1322 dadk_recorderr(pktp, rwcmdp);
1325 1323 dadk_iodone(pktp->cp_bp);
1326 1324 return;
1327 1325 }
1328 1326
1329 1327 if (pktp->cp_reason == CPS_CHKERR)
1330 1328 action = dadk_chkerr(pktp);
1331 1329 else
1332 1330 action = COMMAND_DONE_ERROR;
1333 1331
1334 1332 if (action == JUST_RETURN)
1335 1333 return;
1336 1334
1337 1335 /*
1338 1336 * If we are panicking don't retry the command
1339 1337 * just fail it so we can go down completing all
1340 1338 * of the buffers.
1341 1339 */
1342 1340 if (ddi_in_panic() && action == QUE_COMMAND)
1343 1341 action = COMMAND_DONE_ERROR;
1344 1342
1345 1343 if (action != COMMAND_DONE) {
1346 1344 if ((dadk_ioretry(pktp, action)) == JUST_RETURN)
1347 1345 return;
1348 1346 }
1349 1347 pktp->cp_iodone(pktp->cp_bp);
1350 1348 }
1351 1349
1352 1350
1353 1351
1354 1352 static struct dadkio_derr dadk_errtab[] = {
1355 1353 {COMMAND_DONE, GDA_INFORMATIONAL}, /* 0 DERR_SUCCESS */
1356 1354 {QUE_COMMAND, GDA_FATAL}, /* 1 DERR_AMNF */
1357 1355 {QUE_COMMAND, GDA_FATAL}, /* 2 DERR_TKONF */
1358 1356 {COMMAND_DONE_ERROR, GDA_INFORMATIONAL}, /* 3 DERR_ABORT */
1359 1357 {QUE_COMMAND, GDA_RETRYABLE}, /* 4 DERR_DWF */
1360 1358 {QUE_COMMAND, GDA_FATAL}, /* 5 DERR_IDNF */
1361 1359 {JUST_RETURN, GDA_INFORMATIONAL}, /* 6 DERR_BUSY */
1362 1360 {QUE_COMMAND, GDA_FATAL}, /* 7 DERR_UNC */
1363 1361 {QUE_COMMAND, GDA_RETRYABLE}, /* 8 DERR_BBK */
1364 1362 {COMMAND_DONE_ERROR, GDA_FATAL}, /* 9 DERR_INVCDB */
1365 1363 {COMMAND_DONE_ERROR, GDA_FATAL}, /* 10 DERR_HARD */
1366 1364 {COMMAND_DONE_ERROR, GDA_FATAL}, /* 11 DERR_ILI */
1367 1365 {COMMAND_DONE_ERROR, GDA_FATAL}, /* 12 DERR_EOM */
1368 1366 {COMMAND_DONE, GDA_INFORMATIONAL}, /* 13 DERR_MCR */
1369 1367 {COMMAND_DONE, GDA_INFORMATIONAL}, /* 14 DERR_RECOVER */
1370 1368 {COMMAND_DONE_ERROR, GDA_FATAL}, /* 15 DERR_NOTREADY */
1371 1369 {QUE_COMMAND, GDA_RETRYABLE}, /* 16 DERR_MEDIUM */
1372 1370 {COMMAND_DONE_ERROR, GDA_FATAL}, /* 17 DERR_HW */
1373 1371 {COMMAND_DONE, GDA_FATAL}, /* 18 DERR_ILL */
1374 1372 {COMMAND_DONE, GDA_FATAL}, /* 19 DERR_UNIT_ATTN */
1375 1373 {COMMAND_DONE_ERROR, GDA_FATAL}, /* 20 DERR_DATA_PROT */
1376 1374 {COMMAND_DONE_ERROR, GDA_FATAL}, /* 21 DERR_MISCOMPARE */
1377 1375 {QUE_COMMAND, GDA_RETRYABLE}, /* 22 DERR_ICRC */
1378 1376 {COMMAND_DONE_ERROR, GDA_FATAL}, /* 23 DERR_RESV */
1379 1377 };
1380 1378
1381 1379 static int
1382 1380 dadk_chkerr(struct cmpkt *pktp)
1383 1381 {
1384 1382 daddr_t err_blkno;
1385 1383 struct dadk *dadkp = PKT2DADK(pktp);
1386 1384 dadk_errstats_t *dep;
1387 1385 int scb = *(char *)pktp->cp_scbp;
1388 1386
1389 1387 if (scb == DERR_SUCCESS) {
1390 1388 if (pktp->cp_retry != 0 && dadkp->dad_errstats != NULL) {
1391 1389 dep = (dadk_errstats_t *)
1392 1390 dadkp->dad_errstats->ks_data;
1393 1391 dep->dadk_rq_recov_err.value.ui32++;
1394 1392 }
1395 1393 return (COMMAND_DONE);
1396 1394 }
1397 1395
1398 1396 if (pktp->cp_retry) {
1399 1397 err_blkno = pktp->cp_srtsec + ((pktp->cp_bytexfer -
1400 1398 pktp->cp_resid) >> dadkp->dad_secshf);
1401 1399 } else
1402 1400 err_blkno = -1;
1403 1401
1404 1402 if (dadkp->dad_errstats != NULL) {
1405 1403 dep = (dadk_errstats_t *)dadkp->dad_errstats->ks_data;
1406 1404
1407 1405 switch (dadk_errtab[scb].d_severity) {
1408 1406 case GDA_RETRYABLE:
1409 1407 dep->dadk_softerrs.value.ui32++;
1410 1408 break;
1411 1409
1412 1410 case GDA_FATAL:
1413 1411 dep->dadk_harderrs.value.ui32++;
1414 1412 break;
1415 1413
1416 1414 default:
1417 1415 break;
1418 1416 }
1419 1417
1420 1418 switch (scb) {
1421 1419 case DERR_INVCDB:
1422 1420 case DERR_ILI:
1423 1421 case DERR_EOM:
1424 1422 case DERR_HW:
1425 1423 case DERR_ICRC:
1426 1424 dep->dadk_transerrs.value.ui32++;
1427 1425 break;
1428 1426
1429 1427 case DERR_AMNF:
1430 1428 case DERR_TKONF:
1431 1429 case DERR_DWF:
1432 1430 case DERR_BBK:
1433 1431 case DERR_UNC:
1434 1432 case DERR_HARD:
1435 1433 case DERR_MEDIUM:
1436 1434 case DERR_DATA_PROT:
1437 1435 case DERR_MISCOMP:
1438 1436 dep->dadk_rq_media_err.value.ui32++;
1439 1437 break;
1440 1438
1441 1439 case DERR_NOTREADY:
1442 1440 dep->dadk_rq_ntrdy_err.value.ui32++;
1443 1441 break;
1444 1442
1445 1443 case DERR_IDNF:
1446 1444 case DERR_UNIT_ATTN:
1447 1445 dep->dadk_rq_nodev_err.value.ui32++;
1448 1446 break;
1449 1447
1450 1448 case DERR_ILL:
1451 1449 case DERR_RESV:
1452 1450 dep->dadk_rq_illrq_err.value.ui32++;
1453 1451 break;
1454 1452
1455 1453 default:
1456 1454 break;
1457 1455 }
1458 1456 }
1459 1457
1460 1458 /* if attempting to read a sector from a cdrom audio disk */
1461 1459 if ((dadkp->dad_cdrom) &&
1462 1460 (*((char *)(pktp->cp_cdbp)) == DCMD_READ) &&
1463 1461 (scb == DERR_ILL)) {
1464 1462 return (COMMAND_DONE);
1465 1463 }
1466 1464 if (pktp->cp_passthru == NULL) {
1467 1465 gda_errmsg(dadkp->dad_sd, pktp, dadk_name,
1468 1466 dadk_errtab[scb].d_severity, pktp->cp_srtsec,
1469 1467 err_blkno, dadk_cmds, dadk_sense);
1470 1468 }
1471 1469
1472 1470 if (scb == DERR_BUSY) {
1473 1471 (void) timeout(dadk_restart, (void *)pktp, DADK_BSY_TIMEOUT);
1474 1472 }
1475 1473
1476 1474 return (dadk_errtab[scb].d_action);
1477 1475 }
1478 1476
1479 1477 static void
1480 1478 dadk_recorderr(struct cmpkt *pktp, struct dadkio_rwcmd *rwcmdp)
1481 1479 {
1482 1480 struct dadk *dadkp;
1483 1481 int scb;
1484 1482
1485 1483 dadkp = PKT2DADK(pktp);
1486 1484 scb = (int)(*(char *)pktp->cp_scbp);
1487 1485
1488 1486
1489 1487 rwcmdp->status.failed_blk = rwcmdp->blkaddr +
1490 1488 ((pktp->cp_bytexfer - pktp->cp_resid) >> dadkp->dad_secshf);
1491 1489
1492 1490 rwcmdp->status.resid = pktp->cp_bp->b_resid +
1493 1491 pktp->cp_byteleft - pktp->cp_bytexfer + pktp->cp_resid;
1494 1492 switch ((int)(* (char *)pktp->cp_scbp)) {
1495 1493 case DERR_AMNF:
1496 1494 case DERR_ABORT:
1497 1495 rwcmdp->status.status = DADKIO_STAT_ILLEGAL_REQUEST;
1498 1496 break;
1499 1497 case DERR_DWF:
1500 1498 case DERR_IDNF:
1501 1499 rwcmdp->status.status = DADKIO_STAT_ILLEGAL_ADDRESS;
1502 1500 break;
1503 1501 case DERR_TKONF:
1504 1502 case DERR_UNC:
1505 1503 case DERR_BBK:
1506 1504 rwcmdp->status.status = DADKIO_STAT_MEDIUM_ERROR;
1507 1505 rwcmdp->status.failed_blk_is_valid = 1;
1508 1506 rwcmdp->status.resid = 0;
1509 1507 break;
1510 1508 case DERR_BUSY:
1511 1509 rwcmdp->status.status = DADKIO_STAT_NOT_READY;
1512 1510 break;
1513 1511 case DERR_INVCDB:
1514 1512 case DERR_HARD:
1515 1513 rwcmdp->status.status = DADKIO_STAT_HARDWARE_ERROR;
1516 1514 break;
1517 1515 case DERR_ICRC:
1518 1516 default:
1519 1517 rwcmdp->status.status = DADKIO_STAT_NOT_SUPPORTED;
1520 1518 }
1521 1519
1522 1520 if (rwcmdp->flags & DADKIO_FLAG_SILENT)
1523 1521 return;
1524 1522 gda_errmsg(dadkp->dad_sd, pktp, dadk_name, dadk_errtab[scb].d_severity,
1525 1523 rwcmdp->blkaddr, rwcmdp->status.failed_blk,
1526 1524 dadk_cmds, dadk_sense);
1527 1525 }
1528 1526
1529 1527 /*ARGSUSED*/
1530 1528 static void
1531 1529 dadk_polldone(struct buf *bp)
1532 1530 {
1533 1531 struct cmpkt *pktp;
1534 1532 struct dadk *dadkp;
1535 1533
1536 1534 pktp = GDA_BP_PKT(bp);
1537 1535 dadkp = PKT2DADK(pktp);
1538 1536 mutex_enter(&dadkp->dad_cmd_mutex);
1539 1537 dadkp->dad_cmd_count--;
1540 1538 mutex_exit(&dadkp->dad_cmd_mutex);
1541 1539 }
1542 1540
1543 1541 static void
1544 1542 dadk_iodone(struct buf *bp)
1545 1543 {
1546 1544 struct cmpkt *pktp;
1547 1545 struct dadk *dadkp;
1548 1546
1549 1547 pktp = GDA_BP_PKT(bp);
1550 1548 dadkp = PKT2DADK(pktp);
1551 1549
1552 1550 /* check for all iodone */
1553 1551 pktp->cp_byteleft -= pktp->cp_bytexfer;
1554 1552 if (geterror(bp) == 0 && pktp->cp_byteleft != 0) {
1555 1553 pktp->cp_retry = 0;
1556 1554 (void) dadk_iosetup(dadkp, pktp);
1557 1555
1558 1556
1559 1557 /* transport the next one */
1560 1558 if (CTL_TRANSPORT(dadkp->dad_ctlobjp, pktp) == CTL_SEND_SUCCESS)
1561 1559 return;
1562 1560 if ((dadk_ioretry(pktp, QUE_COMMAND)) == JUST_RETURN)
1563 1561 return;
1564 1562 }
1565 1563
1566 1564 /* start next one */
1567 1565 FLC_DEQUE(dadkp->dad_flcobjp, bp);
1568 1566
1569 1567 /* free pkt */
1570 1568 if (pktp->cp_private)
1571 1569 BBH_FREEHANDLE(dadkp->dad_bbhobjp, pktp->cp_private);
1572 1570 gda_free(dadkp->dad_ctlobjp, pktp, NULL);
1573 1571 mutex_enter(&dadkp->dad_cmd_mutex);
1574 1572 dadkp->dad_cmd_count--;
1575 1573 mutex_exit(&dadkp->dad_cmd_mutex);
1576 1574 biodone(bp);
1577 1575 }
1578 1576
1579 1577 int
1580 1578 dadk_check_media(opaque_t objp, int *state)
1581 1579 {
1582 1580 struct dadk *dadkp = (struct dadk *)objp;
1583 1581
1584 1582 if (!dadkp->dad_rmb) {
1585 1583 return (ENXIO);
1586 1584 }
1587 1585 #ifdef DADK_DEBUG
1588 1586 if (dadk_debug & DSTATE)
1589 1587 PRF("dadk_check_media: user state %x disk state %x\n",
1590 1588 *state, dadkp->dad_iostate);
1591 1589 #endif
1592 1590 /*
1593 1591 * If state already changed just return
1594 1592 */
1595 1593 if (*state != dadkp->dad_iostate) {
1596 1594 *state = dadkp->dad_iostate;
1597 1595 return (0);
1598 1596 }
1599 1597
1600 1598 /*
1601 1599 * Startup polling on thread state
1602 1600 */
1603 1601 mutex_enter(&dadkp->dad_mutex);
1604 1602 if (dadkp->dad_thread_cnt == 0) {
1605 1603 /*
1606 1604 * One thread per removable dadk device
1607 1605 */
1608 1606 (void) thread_create(NULL, 0, dadk_watch_thread, dadkp, 0, &p0,
1609 1607 TS_RUN, v.v_maxsyspri - 2);
1610 1608 }
1611 1609 dadkp->dad_thread_cnt++;
1612 1610
1613 1611 /*
1614 1612 * Wait for state to change
1615 1613 */
1616 1614 do {
1617 1615 if (cv_wait_sig(&dadkp->dad_state_cv, &dadkp->dad_mutex) == 0) {
1618 1616 dadkp->dad_thread_cnt--;
1619 1617 mutex_exit(&dadkp->dad_mutex);
1620 1618 return (EINTR);
1621 1619 }
1622 1620 } while (*state == dadkp->dad_iostate);
1623 1621 *state = dadkp->dad_iostate;
1624 1622 dadkp->dad_thread_cnt--;
1625 1623 mutex_exit(&dadkp->dad_mutex);
1626 1624 return (0);
1627 1625 }
1628 1626
1629 1627
1630 1628 #define MEDIA_ACCESS_DELAY 2000000
1631 1629
1632 1630 static void
1633 1631 dadk_watch_thread(struct dadk *dadkp)
1634 1632 {
1635 1633 enum dkio_state state;
1636 1634 int interval;
1637 1635
1638 1636 interval = drv_usectohz(dadk_check_media_time);
1639 1637
1640 1638 do {
1641 1639 if (dadk_rmb_ioctl(dadkp, DCMD_GET_STATE, (intptr_t)&state, 0,
1642 1640 DADK_SILENT)) {
1643 1641 /*
1644 1642 * Assume state remained the same
1645 1643 */
1646 1644 state = dadkp->dad_iostate;
1647 1645 }
1648 1646
1649 1647 /*
1650 1648 * now signal the waiting thread if this is *not* the
1651 1649 * specified state;
1652 1650 * delay the signal if the state is DKIO_INSERTED
1653 1651 * to allow the target to recover
1654 1652 */
1655 1653 if (state != dadkp->dad_iostate) {
1656 1654
1657 1655 dadkp->dad_iostate = state;
1658 1656 if (state == DKIO_INSERTED) {
1659 1657 /*
1660 1658 * delay the signal to give the drive a chance
1661 1659 * to do what it apparently needs to do
1662 1660 */
1663 1661 (void) timeout((void(*)(void *))cv_broadcast,
1664 1662 (void *)&dadkp->dad_state_cv,
1665 1663 drv_usectohz((clock_t)MEDIA_ACCESS_DELAY));
1666 1664 } else {
1667 1665 cv_broadcast(&dadkp->dad_state_cv);
1668 1666 }
1669 1667 }
1670 1668 delay(interval);
1671 1669 } while (dadkp->dad_thread_cnt);
1672 1670 }
1673 1671
1674 1672 int
1675 1673 dadk_inquiry(opaque_t objp, opaque_t *inqpp)
1676 1674 {
1677 1675 struct dadk *dadkp = (struct dadk *)objp;
1678 1676 struct scsi_inquiry **sinqpp = (struct scsi_inquiry **)inqpp;
1679 1677
1680 1678 if (dadkp && dadkp->dad_sd && dadkp->dad_sd->sd_inq) {
1681 1679 *sinqpp = dadkp->dad_sd->sd_inq;
1682 1680 return (DDI_SUCCESS);
1683 1681 }
1684 1682
1685 1683 return (DDI_FAILURE);
1686 1684 }
1687 1685
1688 1686 static int
1689 1687 dadk_rmb_ioctl(struct dadk *dadkp, int cmd, intptr_t arg, int flags, int silent)
1690 1688
1691 1689 {
1692 1690 struct buf *bp;
1693 1691 int err;
1694 1692 struct cmpkt *pktp;
1695 1693
1696 1694 if ((bp = getrbuf(KM_SLEEP)) == NULL) {
1697 1695 return (ENOMEM);
1698 1696 }
1699 1697 pktp = dadk_pktprep(dadkp, NULL, bp, dadk_rmb_iodone, NULL, NULL);
1700 1698 if (!pktp) {
1701 1699 freerbuf(bp);
1702 1700 return (ENOMEM);
1703 1701 }
1704 1702 bp->b_back = (struct buf *)arg;
1705 1703 bp->b_forw = (struct buf *)dadkp->dad_flcobjp;
1706 1704 pktp->cp_passthru = (opaque_t)(intptr_t)silent;
1707 1705
1708 1706 err = dadk_ctl_ioctl(dadkp, cmd, (uintptr_t)pktp, flags);
1709 1707 freerbuf(bp);
1710 1708 gda_free(dadkp->dad_ctlobjp, pktp, NULL);
1711 1709 return (err);
1712 1710
1713 1711
1714 1712 }
1715 1713
1716 1714 static void
1717 1715 dadk_rmb_iodone(struct buf *bp)
1718 1716 {
1719 1717 struct cmpkt *pktp;
1720 1718 struct dadk *dadkp;
1721 1719
1722 1720 pktp = GDA_BP_PKT(bp);
1723 1721 dadkp = PKT2DADK(pktp);
1724 1722
1725 1723 bp->b_flags &= ~(B_DONE|B_BUSY);
1726 1724
1727 1725 /* Start next one */
1728 1726 FLC_DEQUE(dadkp->dad_flcobjp, bp);
1729 1727
1730 1728 mutex_enter(&dadkp->dad_cmd_mutex);
1731 1729 dadkp->dad_cmd_count--;
1732 1730 mutex_exit(&dadkp->dad_cmd_mutex);
1733 1731 biodone(bp);
1734 1732 }
1735 1733
1736 1734 static int
1737 1735 dadk_dk_buf_setup(struct dadk *dadkp, opaque_t *cmdp, dev_t dev,
1738 1736 enum uio_seg dataspace, int rw)
1739 1737 {
1740 1738 struct dadkio_rwcmd *rwcmdp = (struct dadkio_rwcmd *)cmdp;
1741 1739 struct buf *bp;
1742 1740 struct iovec aiov;
1743 1741 struct uio auio;
1744 1742 struct uio *uio = &auio;
1745 1743 int status;
1746 1744
1747 1745 bp = getrbuf(KM_SLEEP);
1748 1746
1749 1747 bp->av_forw = bp->b_forw = (struct buf *)dadkp;
1750 1748 bp->b_back = (struct buf *)rwcmdp; /* ioctl packet */
1751 1749
1752 1750 bzero((caddr_t)&auio, sizeof (struct uio));
1753 1751 bzero((caddr_t)&aiov, sizeof (struct iovec));
1754 1752 aiov.iov_base = rwcmdp->bufaddr;
1755 1753 aiov.iov_len = rwcmdp->buflen;
1756 1754 uio->uio_iov = &aiov;
1757 1755
1758 1756 uio->uio_iovcnt = 1;
1759 1757 uio->uio_resid = rwcmdp->buflen;
1760 1758 uio->uio_segflg = dataspace;
1761 1759
1762 1760 /* Let physio do the rest... */
1763 1761 status = physio(dadk_dk_strategy, bp, dev, rw, dadkmin, uio);
1764 1762
1765 1763 freerbuf(bp);
1766 1764 return (status);
1767 1765
1768 1766 }
1769 1767
1770 1768 /* Do not let a user gendisk request get too big or */
1771 1769 /* else we could use to many resources. */
1772 1770
1773 1771 static void
1774 1772 dadkmin(struct buf *bp)
1775 1773 {
1776 1774 if (bp->b_bcount > dadk_dk_maxphys)
1777 1775 bp->b_bcount = dadk_dk_maxphys;
1778 1776 }
1779 1777
1780 1778 static int
1781 1779 dadk_dk_strategy(struct buf *bp)
1782 1780 {
1783 1781 dadk_dk((struct dadk *)bp->av_forw, (struct dadkio_rwcmd *)bp->b_back,
1784 1782 bp);
1785 1783 return (0);
1786 1784 }
1787 1785
1788 1786 static void
1789 1787 dadk_dk(struct dadk *dadkp, struct dadkio_rwcmd *rwcmdp, struct buf *bp)
1790 1788 {
1791 1789 struct cmpkt *pktp;
1792 1790
1793 1791 pktp = dadk_pktprep(dadkp, NULL, bp, dadk_iodone, NULL, NULL);
1794 1792 if (!pktp) {
1795 1793 bioerror(bp, ENOMEM);
1796 1794 biodone(bp);
1797 1795 return;
1798 1796 }
1799 1797
1800 1798 pktp->cp_passthru = rwcmdp;
1801 1799
1802 1800 (void) dadk_ioprep(dadkp, pktp);
1803 1801
1804 1802 mutex_enter(&dadkp->dad_cmd_mutex);
1805 1803 dadkp->dad_cmd_count++;
1806 1804 mutex_exit(&dadkp->dad_cmd_mutex);
1807 1805 FLC_ENQUE(dadkp->dad_flcobjp, bp);
1808 1806 }
1809 1807
1810 1808 /*
1811 1809 * There is no existing way to notify cmdk module
1812 1810 * when the command completed, so add this function
1813 1811 * to calculate how many on-going commands.
1814 1812 */
1815 1813 int
1816 1814 dadk_getcmds(opaque_t objp)
1817 1815 {
1818 1816 struct dadk *dadkp = (struct dadk *)objp;
1819 1817 int count;
1820 1818
1821 1819 mutex_enter(&dadkp->dad_cmd_mutex);
1822 1820 count = dadkp->dad_cmd_count;
1823 1821 mutex_exit(&dadkp->dad_cmd_mutex);
1824 1822 return (count);
1825 1823 }
1826 1824
1827 1825 /*
1828 1826 * this function was used to calc the cmd for CTL_IOCTL
1829 1827 */
1830 1828 static int
1831 1829 dadk_ctl_ioctl(struct dadk *dadkp, uint32_t cmd, uintptr_t arg, int flag)
1832 1830 {
1833 1831 int error;
1834 1832 mutex_enter(&dadkp->dad_cmd_mutex);
1835 1833 dadkp->dad_cmd_count++;
1836 1834 mutex_exit(&dadkp->dad_cmd_mutex);
1837 1835 error = CTL_IOCTL(dadkp->dad_ctlobjp, cmd, arg, flag);
1838 1836 mutex_enter(&dadkp->dad_cmd_mutex);
1839 1837 dadkp->dad_cmd_count--;
1840 1838 mutex_exit(&dadkp->dad_cmd_mutex);
1841 1839 return (error);
1842 1840 }
↓ open down ↓ |
777 lines elided |
↑ open up ↑ |
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX