1 /* 2 * CDDL HEADER START 3 * 4 * The contents of this file are subject to the terms of the 5 * Common Development and Distribution License (the "License"). 6 * You may not use this file except in compliance with the License. 7 * 8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 9 * or http://www.opensolaris.org/os/licensing. 10 * See the License for the specific language governing permissions 11 * and limitations under the License. 12 * 13 * When distributing Covered Code, include this CDDL HEADER in each 14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 15 * If applicable, add the following below this CDDL HEADER, with the 16 * fields enclosed by brackets "[]" replaced with your own identifying 17 * information: Portions Copyright [yyyy] [name of copyright owner] 18 * 19 * CDDL HEADER END 20 */ 21 /* 22 * Enclosure Services Device target driver 23 * 24 * Copyright 2009 Sun Microsystems, Inc. All rights reserved. 25 * Use is subject to license terms. 26 */ 27 28 #include <sys/modctl.h> 29 #include <sys/file.h> 30 #include <sys/scsi/scsi.h> 31 #include <sys/scsi/generic/status.h> 32 #include <sys/scsi/targets/sesio.h> 33 #include <sys/scsi/targets/ses.h> 34 35 36 37 /* 38 * Power management defines (should be in a common include file?) 39 */ 40 #define PM_HARDWARE_STATE_PROP "pm-hardware-state" 41 #define PM_NEEDS_SUSPEND_RESUME "needs-suspend-resume" 42 43 44 /* 45 * Global Driver Data 46 */ 47 int ses_io_time = SES_IO_TIME; 48 49 static int ses_retry_count = SES_RETRY_COUNT * SES_RETRY_MULTIPLIER; 50 51 #ifdef DEBUG 52 int ses_debug = 0; 53 #else /* DEBUG */ 54 #define ses_debug 0 55 #endif /* DEBUG */ 56 57 58 /* 59 * External Enclosure Functions 60 */ 61 extern int ses_softc_init(ses_softc_t *, int); 62 extern int ses_init_enc(ses_softc_t *); 63 extern int ses_get_encstat(ses_softc_t *, int); 64 extern int ses_set_encstat(ses_softc_t *, uchar_t, int); 65 extern int ses_get_objstat(ses_softc_t *, ses_objarg *, int); 66 extern int ses_set_objstat(ses_softc_t *, ses_objarg *, int); 67 68 extern int safte_softc_init(ses_softc_t *, int); 69 extern int safte_init_enc(ses_softc_t *); 70 extern int safte_get_encstat(ses_softc_t *, int); 71 extern int safte_set_encstat(ses_softc_t *, uchar_t, int); 72 extern int safte_get_objstat(ses_softc_t *, ses_objarg *, int); 73 extern int safte_set_objstat(ses_softc_t *, ses_objarg *, int); 74 75 extern int sen_softc_init(ses_softc_t *, int); 76 extern int sen_init_enc(ses_softc_t *); 77 extern int sen_get_encstat(ses_softc_t *, int); 78 extern int sen_set_encstat(ses_softc_t *, uchar_t, int); 79 extern int sen_get_objstat(ses_softc_t *, ses_objarg *, int); 80 extern int sen_set_objstat(ses_softc_t *, ses_objarg *, int); 81 82 /* 83 * Local Function prototypes 84 */ 85 static int ses_info(dev_info_t *, ddi_info_cmd_t, void *, void **); 86 static int ses_probe(dev_info_t *); 87 static int ses_attach(dev_info_t *, ddi_attach_cmd_t); 88 static int ses_detach(dev_info_t *, ddi_detach_cmd_t); 89 90 static int is_enc_dev(ses_softc_t *, struct scsi_inquiry *, int, enctyp *); 91 static int ses_doattach(dev_info_t *dip); 92 93 static int ses_open(dev_t *, int, int, cred_t *); 94 static int ses_close(dev_t, int, int, cred_t *); 95 static int ses_ioctl(dev_t, int, intptr_t, int, cred_t *, int *); 96 97 static encvec vecs[3] = { 98 { 99 ses_softc_init, ses_init_enc, ses_get_encstat, 100 ses_set_encstat, ses_get_objstat, ses_set_objstat 101 }, 102 { 103 safte_softc_init, safte_init_enc, safte_get_encstat, 104 safte_set_encstat, safte_get_objstat, safte_set_objstat, 105 }, 106 { 107 sen_softc_init, sen_init_enc, sen_get_encstat, 108 sen_set_encstat, sen_get_objstat, sen_set_objstat 109 } 110 }; 111 112 113 /* 114 * Local Functions 115 */ 116 static int ses_start(struct buf *bp); 117 static int ses_decode_sense(struct scsi_pkt *pkt, int *err); 118 119 static void ses_get_pkt(struct buf *bp, int (*func)(opaque_t)); 120 static void ses_callback(struct scsi_pkt *pkt); 121 static void ses_restart(void *arg); 122 123 124 /* 125 * Local Static Data 126 */ 127 #ifndef D_HOTPLUG 128 #define D_HOTPLUG 0 129 #endif /* D_HOTPLUG */ 130 131 static struct cb_ops ses_cb_ops = { 132 ses_open, /* open */ 133 ses_close, /* close */ 134 nodev, /* strategy */ 135 nodev, /* print */ 136 nodev, /* dump */ 137 nodev, /* read */ 138 nodev, /* write */ 139 ses_ioctl, /* ioctl */ 140 nodev, /* devmap */ 141 nodev, /* mmap */ 142 nodev, /* segmap */ 143 nochpoll, /* poll */ 144 ddi_prop_op, /* cb_prop_op */ 145 0, /* streamtab */ 146 #if !defined(CB_REV) 147 D_MP | D_NEW | D_HOTPLUG /* Driver compatibility flag */ 148 #else /* !defined(CB_REV) */ 149 D_MP | D_NEW | D_HOTPLUG, /* Driver compatibility flag */ 150 CB_REV, /* cb_ops version number */ 151 nodev, /* aread */ 152 nodev /* awrite */ 153 #endif /* !defined(CB_REV) */ 154 }; 155 156 static struct dev_ops ses_dev_ops = { 157 DEVO_REV, /* devo_rev, */ 158 0, /* refcnt */ 159 ses_info, /* info */ 160 nulldev, /* identify */ 161 ses_probe, /* probe */ 162 ses_attach, /* attach */ 163 ses_detach, /* detach */ 164 nodev, /* reset */ 165 &ses_cb_ops, /* driver operations */ 166 (struct bus_ops *)NULL, /* bus operations */ 167 NULL, /* power */ 168 ddi_quiesce_not_needed, /* quiesce */ 169 }; 170 171 static void *estate = NULL; 172 static const char *Snm = "ses"; 173 static const char *Str = "%s\n"; 174 static const char *efl = "copyin/copyout EFAULT @ line %d"; 175 static const char *fail_msg = "%stransport failed: reason '%s': %s"; 176 177 178 179 /* 180 * autoconfiguration routines. 181 */ 182 char _depends_on[] = "misc/scsi"; 183 184 static struct modldrv modldrv = { 185 &mod_driverops, 186 "SCSI Enclosure Services", 187 &ses_dev_ops 188 }; 189 190 static struct modlinkage modlinkage = { 191 MODREV_1, &modldrv, NULL 192 }; 193 194 195 int 196 _init(void) 197 { 198 int status; 199 status = ddi_soft_state_init(&estate, sizeof (ses_softc_t), 0); 200 if (status == 0) { 201 if ((status = mod_install(&modlinkage)) != 0) { 202 ddi_soft_state_fini(&estate); 203 } 204 } 205 return (status); 206 } 207 208 int 209 _fini(void) 210 { 211 int status; 212 if ((status = mod_remove(&modlinkage)) != 0) { 213 return (status); 214 } 215 ddi_soft_state_fini(&estate); 216 return (status); 217 } 218 219 int 220 _info(struct modinfo *modinfop) 221 { 222 return (mod_info(&modlinkage, modinfop)); 223 } 224 225 static int 226 ses_probe(dev_info_t *dip) 227 { 228 int err; 229 struct scsi_device *devp; 230 enctyp ep; 231 232 /* 233 * I finally figured out why we return success 234 * on every probe. The devices that we attach to 235 * don't all report as being the same "device type" 236 * 237 * 1) A5x00 -- report as Enclosure Services (0xD) SES 238 * 2) A1000 -- report as Direct Access (0x0) SES 239 * uses the same target as raid controler. 240 * 3) D1000 -- report as processor (0x3) SAFTE 241 * 3) D240 -- report as processor (0x3) SAFTE 242 * 243 * We also reportedly attach to SEN devices which I 244 * believe reside in a Tobasco tray. I have never 245 * been able to get one to attach. 246 * 247 */ 248 if (dip == NULL) 249 return (DDI_PROBE_FAILURE); 250 /* SES_LOG(NULL, SES_CE_DEBUG1, "ses_probe: OK"); */ 251 if (ddi_dev_is_sid(dip) == DDI_SUCCESS) { 252 return (DDI_PROBE_DONTCARE); 253 } 254 255 devp = ddi_get_driver_private(dip); 256 257 /* Legacy: prevent driver.conf specified ses nodes on atapi. */ 258 if (scsi_ifgetcap(&devp->sd_address, "interconnect-type", -1) == 259 INTERCONNECT_ATAPI) 260 return (DDI_PROBE_FAILURE); 261 262 /* 263 * XXX: Breakage from the x86 folks. 264 */ 265 if (strcmp(ddi_get_name(ddi_get_parent(dip)), "ata") == 0) { 266 return (DDI_PROBE_FAILURE); 267 } 268 269 switch (err = scsi_probe(devp, SLEEP_FUNC)) { 270 case SCSIPROBE_EXISTS: 271 if (is_enc_dev(NULL, devp->sd_inq, SUN_INQSIZE, &ep)) { 272 break; 273 } 274 /* FALLTHROUGH */ 275 case SCSIPROBE_NORESP: 276 scsi_unprobe(devp); 277 return (DDI_PROBE_FAILURE); 278 default: 279 SES_LOG(NULL, SES_CE_DEBUG9, 280 "ses_probe: probe error %d", err); 281 scsi_unprobe(devp); 282 return (DDI_PROBE_FAILURE); 283 } 284 scsi_unprobe(devp); 285 return (DDI_PROBE_SUCCESS); 286 } 287 288 static int 289 ses_attach(dev_info_t *dip, ddi_attach_cmd_t cmd) 290 { 291 int inst, err; 292 ses_softc_t *ssc; 293 294 inst = ddi_get_instance(dip); 295 switch (cmd) { 296 case DDI_ATTACH: 297 SES_LOG(NULL, SES_CE_DEBUG9, "ses_attach: DDI_ATTACH ses%d", 298 inst); 299 300 err = ses_doattach(dip); 301 302 if (err == DDI_FAILURE) { 303 return (DDI_FAILURE); 304 } 305 SES_LOG(NULL, SES_CE_DEBUG4, 306 "ses_attach: DDI_ATTACH OK ses%d", inst); 307 break; 308 309 case DDI_RESUME: 310 if ((ssc = ddi_get_soft_state(estate, inst)) == NULL) { 311 return (DDI_FAILURE); 312 } 313 SES_LOG(ssc, SES_CE_DEBUG1, "ses_attach: DDI_ATTACH ses%d", 314 inst); 315 ssc->ses_suspended = 0; 316 break; 317 318 default: 319 return (DDI_FAILURE); 320 } 321 return (DDI_SUCCESS); 322 } 323 324 static int 325 is_enc_dev(ses_softc_t *ssc, struct scsi_inquiry *inqp, int iqlen, enctyp *ep) 326 { 327 uchar_t dt = (inqp->inq_dtype & DTYPE_MASK); 328 uchar_t *iqd = (uchar_t *)inqp; 329 330 if (dt == DTYPE_ESI) { 331 if (strncmp(inqp->inq_vid, SEN_ID, SEN_ID_LEN) == 0) { 332 SES_LOG(ssc, SES_CE_DEBUG3, "SEN device found"); 333 *ep = SEN_TYPE; 334 } else if (inqp->inq_rdf == RDF_SCSI2) { 335 /* 336 * Per SPC4 #6.4.2 Standard Inquiry Data, response 337 * data format (RDF) values of 0 and 1 are Obsolete, 338 * whereas values greater than 2 are Reserved 339 */ 340 SES_LOG(ssc, SES_CE_DEBUG3, "SES device found"); 341 *ep = SES_TYPE; 342 } else { 343 SES_LOG(ssc, SES_CE_DEBUG3, "Pre-SCSI3 SES device"); 344 *ep = SES_TYPE; 345 } 346 return (1); 347 } 348 if ((iqd[6] & 0x40) && inqp->inq_rdf >= RDF_SCSI2) { 349 /* 350 * PassThrough Device. 351 */ 352 *ep = SES_TYPE; 353 SES_LOG(ssc, SES_CE_DEBUG3, "Passthru SES device"); 354 return (1); 355 } 356 357 if (iqlen < 47) { 358 SES_LOG(ssc, CE_NOTE, 359 "INQUIRY data too short to determine SAF-TE"); 360 return (0); 361 } 362 if (strncmp((char *)&iqd[44], "SAF-TE", 4) == 0) { 363 *ep = SAFT_TYPE; 364 SES_LOG(ssc, SES_CE_DEBUG3, "SAF-TE device found"); 365 return (1); 366 } 367 return (0); 368 } 369 370 371 /* 372 * Attach ses device. 373 * 374 * XXX: Power management is NOT supported. A token framework 375 * is provided that will need to be extended assuming we have 376 * ses devices we can power down. Currently, we don't have any. 377 */ 378 static int 379 ses_doattach(dev_info_t *dip) 380 { 381 int inst, err; 382 Scsidevp devp; 383 ses_softc_t *ssc; 384 enctyp etyp; 385 386 inst = ddi_get_instance(dip); 387 /* 388 * Workaround for bug #4154979- for some reason we can 389 * be called with identical instance numbers but for 390 * different dev_info_t-s- all but one are bogus. 391 * 392 * Bad Dog! No Biscuit! 393 * 394 * A quick workaround might be to call ddi_soft_state_zalloc 395 * unconditionally, as the implementation fails these calls 396 * if there's an item already allocated. A more reasonable 397 * and longer term change is to move the allocation past 398 * the probe for the device's existence as most of these 399 * 'bogus' calls are for nonexistent devices. 400 */ 401 402 devp = ddi_get_driver_private(dip); 403 devp->sd_dev = dip; 404 405 /* 406 * Determine whether the { i, t, l } we're called 407 * to start is an enclosure services device. 408 */ 409 410 /* 411 * Call the scsi_probe routine to see whether 412 * we actually have an Enclosure Services device at 413 * this address. 414 */ 415 err = scsi_probe(devp, SLEEP_FUNC); 416 if (err != SCSIPROBE_EXISTS) { 417 SES_LOG(NULL, SES_CE_DEBUG9, 418 "ses_doattach: probe error %d", err); 419 scsi_unprobe(devp); 420 return (DDI_FAILURE); 421 } 422 /* Call is_enc_dev() to get the etyp */ 423 if (!(is_enc_dev(NULL, devp->sd_inq, SUN_INQSIZE, &etyp))) { 424 SES_LOG(NULL, CE_WARN, 425 "ses_doattach: ses%d: is_enc_dev failure", inst); 426 scsi_unprobe(devp); 427 return (DDI_FAILURE); 428 } 429 430 if (ddi_soft_state_zalloc(estate, inst) != DDI_SUCCESS) { 431 scsi_unprobe(devp); 432 SES_LOG(NULL, CE_NOTE, "ses%d: softalloc fails", inst); 433 return (DDI_FAILURE); 434 } 435 ssc = ddi_get_soft_state(estate, inst); 436 if (ssc == NULL) { 437 scsi_unprobe(devp); 438 SES_LOG(NULL, CE_NOTE, "ses%d: get_soft_state fails", inst); 439 return (DDI_FAILURE); 440 } 441 devp->sd_private = (opaque_t)ssc; 442 ssc->ses_devp = devp; 443 err = ddi_create_minor_node(dip, "0", S_IFCHR, inst, 444 DDI_NT_SCSI_ENCLOSURE, NULL); 445 if (err == DDI_FAILURE) { 446 ddi_remove_minor_node(dip, NULL); 447 SES_LOG(ssc, CE_NOTE, "minor node creation failed"); 448 ddi_soft_state_free(estate, inst); 449 scsi_unprobe(devp); 450 return (DDI_FAILURE); 451 } 452 453 ssc->ses_type = etyp; 454 ssc->ses_vec = vecs[etyp]; 455 456 /* Call SoftC Init Routine A bit later... */ 457 458 ssc->ses_rqbp = scsi_alloc_consistent_buf(SES_ROUTE(ssc), 459 NULL, MAX_SENSE_LENGTH, B_READ, SLEEP_FUNC, NULL); 460 if (ssc->ses_rqbp != NULL) { 461 ssc->ses_rqpkt = scsi_init_pkt(SES_ROUTE(ssc), NULL, 462 ssc->ses_rqbp, CDB_GROUP0, 1, 0, PKT_CONSISTENT, 463 SLEEP_FUNC, NULL); 464 } 465 if (ssc->ses_rqbp == NULL || ssc->ses_rqpkt == NULL) { 466 ddi_remove_minor_node(dip, NULL); 467 SES_LOG(ssc, CE_NOTE, "scsi_init_pkt of rqbuf failed"); 468 if (ssc->ses_rqbp != NULL) { 469 scsi_free_consistent_buf(ssc->ses_rqbp); 470 ssc->ses_rqbp = NULL; 471 } 472 ddi_soft_state_free(estate, inst); 473 scsi_unprobe(devp); 474 return (DDI_FAILURE); 475 } 476 ssc->ses_rqpkt->pkt_private = (opaque_t)ssc; 477 ssc->ses_rqpkt->pkt_address = *(SES_ROUTE(ssc)); 478 ssc->ses_rqpkt->pkt_comp = ses_callback; 479 ssc->ses_rqpkt->pkt_time = ses_io_time; 480 ssc->ses_rqpkt->pkt_flags = FLAG_NOPARITY|FLAG_NODISCON|FLAG_SENSING; 481 ssc->ses_rqpkt->pkt_cdbp[0] = SCMD_REQUEST_SENSE; 482 ssc->ses_rqpkt->pkt_cdbp[1] = 0; 483 ssc->ses_rqpkt->pkt_cdbp[2] = 0; 484 ssc->ses_rqpkt->pkt_cdbp[3] = 0; 485 ssc->ses_rqpkt->pkt_cdbp[4] = MAX_SENSE_LENGTH; 486 ssc->ses_rqpkt->pkt_cdbp[5] = 0; 487 488 switch (scsi_ifgetcap(SES_ROUTE(ssc), "auto-rqsense", 1)) { 489 case 1: 490 /* if already set, don't reset it */ 491 ssc->ses_arq = 1; 492 break; 493 case 0: 494 /* try and set it */ 495 ssc->ses_arq = ((scsi_ifsetcap(SES_ROUTE(ssc), 496 "auto-rqsense", 1, 1) == 1) ? 1 : 0); 497 break; 498 default: 499 /* probably undefined, so zero it out */ 500 ssc->ses_arq = 0; 501 break; 502 } 503 504 ssc->ses_sbufp = getrbuf(KM_SLEEP); 505 cv_init(&ssc->ses_sbufcv, NULL, CV_DRIVER, NULL); 506 507 /* 508 * If the HBA supports wide, tell it to use wide. 509 */ 510 if (scsi_ifgetcap(SES_ROUTE(ssc), "wide-xfer", 1) != -1) { 511 int wd = ((devp->sd_inq->inq_rdf == RDF_SCSI2) && 512 (devp->sd_inq->inq_wbus16 || devp->sd_inq->inq_wbus32)) 513 ? 1 : 0; 514 (void) scsi_ifsetcap(SES_ROUTE(ssc), "wide-xfer", wd, 1); 515 } 516 517 /* 518 * Now do ssc init of enclosure specifics. 519 * At the same time, check to make sure getrbuf 520 * actually succeeded. 521 */ 522 if ((*ssc->ses_vec.softc_init)(ssc, 1)) { 523 SES_LOG(ssc, SES_CE_DEBUG3, "failed softc init"); 524 (void) (*ssc->ses_vec.softc_init)(ssc, 0); 525 ddi_remove_minor_node(dip, NULL); 526 scsi_destroy_pkt(ssc->ses_rqpkt); 527 scsi_free_consistent_buf(ssc->ses_rqbp); 528 if (ssc->ses_sbufp) { 529 freerbuf(ssc->ses_sbufp); 530 } 531 cv_destroy(&ssc->ses_sbufcv); 532 ddi_soft_state_free(estate, inst); 533 scsi_unprobe(devp); 534 return (DDI_FAILURE); 535 } 536 537 /* 538 * create this property so that PM code knows we want 539 * to be suspended at PM time 540 */ 541 (void) ddi_prop_update_string(DDI_DEV_T_NONE, dip, 542 PM_HARDWARE_STATE_PROP, PM_NEEDS_SUSPEND_RESUME); 543 544 /* announce the existence of this device */ 545 ddi_report_dev(dip); 546 return (DDI_SUCCESS); 547 } 548 549 550 /* 551 * Detach ses device. 552 * 553 * XXX: Power management is NOT supported. A token framework 554 * is provided that will need to be extended assuming we have 555 * ses devices we can power down. Currently, we don't have any. 556 */ 557 static int 558 ses_detach(dev_info_t *dip, ddi_detach_cmd_t cmd) 559 { 560 ses_softc_t *ssc; 561 int inst; 562 563 switch (cmd) { 564 case DDI_DETACH: 565 inst = ddi_get_instance(dip); 566 ssc = ddi_get_soft_state(estate, inst); 567 if (ssc == NULL) { 568 cmn_err(CE_NOTE, 569 "ses%d: DDI_DETACH, no softstate found", inst); 570 return (DDI_FAILURE); 571 } 572 if (ISOPEN(ssc)) { 573 return (DDI_FAILURE); 574 } 575 576 #if !defined(lint) 577 /* LINTED */ 578 _NOTE(COMPETING_THREADS_NOW); 579 #endif /* !defined(lint) */ 580 581 if (ssc->ses_vec.softc_init) 582 (void) (*ssc->ses_vec.softc_init)(ssc, 0); 583 584 #if !defined(lint) 585 _NOTE(NO_COMPETING_THREADS_NOW); 586 #endif /* !defined(lint) */ 587 588 (void) scsi_ifsetcap(SES_ROUTE(ssc), "auto-rqsense", 1, 0); 589 scsi_destroy_pkt(ssc->ses_rqpkt); 590 scsi_free_consistent_buf(ssc->ses_rqbp); 591 freerbuf(ssc->ses_sbufp); 592 cv_destroy(&ssc->ses_sbufcv); 593 ddi_soft_state_free(estate, inst); 594 ddi_prop_remove_all(dip); 595 ddi_remove_minor_node(dip, NULL); 596 scsi_unprobe(ddi_get_driver_private(dip)); 597 break; 598 599 case DDI_SUSPEND: 600 inst = ddi_get_instance(dip); 601 if ((ssc = ddi_get_soft_state(estate, inst)) == NULL) { 602 cmn_err(CE_NOTE, 603 "ses%d: DDI_SUSPEND, no softstate found", inst); 604 return (DDI_FAILURE); 605 } 606 607 /* 608 * If driver idle, accept suspend request. 609 * If it's busy, reject it. This keeps things simple! 610 */ 611 mutex_enter(SES_MUTEX); 612 if (ssc->ses_sbufbsy) { 613 mutex_exit(SES_MUTEX); 614 return (DDI_FAILURE); 615 } 616 ssc->ses_suspended = 1; 617 mutex_exit(SES_MUTEX); 618 break; 619 620 default: 621 return (DDI_FAILURE); 622 } 623 return (DDI_SUCCESS); 624 } 625 626 /* ARGSUSED */ 627 static int 628 ses_info(dev_info_t *dip, ddi_info_cmd_t infocmd, void *arg, void **result) 629 { 630 dev_t dev; 631 ses_softc_t *ssc; 632 int inst, error; 633 634 switch (infocmd) { 635 case DDI_INFO_DEVT2DEVINFO: 636 dev = (dev_t)arg; 637 inst = getminor(dev); 638 if ((ssc = ddi_get_soft_state(estate, inst)) == NULL) { 639 return (DDI_FAILURE); 640 } 641 *result = (void *) ssc->ses_devp->sd_dev; 642 error = DDI_SUCCESS; 643 break; 644 case DDI_INFO_DEVT2INSTANCE: 645 dev = (dev_t)arg; 646 inst = getminor(dev); 647 *result = (void *)(uintptr_t)inst; 648 error = DDI_SUCCESS; 649 break; 650 default: 651 error = DDI_FAILURE; 652 } 653 return (error); 654 } 655 656 657 /* 658 * Unix Entry Points 659 */ 660 661 /* ARGSUSED */ 662 static int 663 ses_open(dev_t *dev_p, int flag, int otyp, cred_t *cred_p) 664 { 665 ses_softc_t *ssc; 666 667 if ((ssc = ddi_get_soft_state(estate, getminor(*dev_p))) == NULL) { 668 return (ENXIO); 669 } 670 671 /* 672 * If the device is powered down, request it's activation. 673 * If it can't be activated, fail open. 674 */ 675 if (ssc->ses_suspended && 676 ddi_dev_is_needed(SES_DEVINFO(ssc), 0, 1) != DDI_SUCCESS) { 677 return (EIO); 678 } 679 680 mutex_enter(SES_MUTEX); 681 if (otyp == OTYP_LYR) 682 ssc->ses_lyropen++; 683 else 684 ssc->ses_oflag = 1; 685 686 ssc->ses_present = (ssc->ses_present)? ssc->ses_present: SES_OPENING; 687 mutex_exit(SES_MUTEX); 688 return (EOK); 689 } 690 691 /*ARGSUSED*/ 692 static int 693 ses_close(dev_t dev, int flag, int otyp, cred_t *cred_p) 694 { 695 ses_softc_t *ssc; 696 if ((ssc = ddi_get_soft_state(estate, getminor(dev))) == NULL) { 697 return (ENXIO); 698 } 699 700 if (ssc->ses_suspended) { 701 (void) ddi_dev_is_needed(SES_DEVINFO(ssc), 0, 1); 702 } 703 704 mutex_enter(SES_MUTEX); 705 if (otyp == OTYP_LYR) 706 ssc->ses_lyropen -= (ssc->ses_lyropen)? 1: 0; 707 else 708 ssc->ses_oflag = 0; 709 mutex_exit(SES_MUTEX); 710 return (0); 711 } 712 713 714 /*ARGSUSED3*/ 715 static int 716 ses_ioctl(dev_t dev, int cmd, intptr_t arg, int flg, cred_t *cred_p, int *rvalp) 717 { 718 ses_softc_t *ssc; 719 ses_object k, *up; 720 ses_objarg x; 721 uchar_t t; 722 uchar_t i; 723 int rv = 0; 724 725 if ((ssc = ddi_get_soft_state(estate, getminor(dev))) == NULL || 726 ssc->ses_present == SES_CLOSED) { 727 return (ENXIO); 728 } 729 730 731 switch (cmd) { 732 case SESIOC_GETNOBJ: 733 if (ddi_copyout(&ssc->ses_nobjects, (void *)arg, 734 sizeof (int), flg)) { 735 rv = EFAULT; 736 break; 737 } 738 break; 739 740 case SESIOC_GETOBJMAP: 741 up = (ses_object *) arg; 742 mutex_enter(SES_MUTEX); 743 for (i = 0; i != ssc->ses_nobjects; i++) { 744 k.obj_id = i; 745 k.subencid = ssc->ses_objmap[i].subenclosure; 746 k.elem_type = ssc->ses_objmap[i].enctype; 747 if (ddi_copyout(&k, up, sizeof (k), flg)) { 748 rv = EFAULT; 749 break; 750 } 751 up++; 752 } 753 mutex_exit(SES_MUTEX); 754 break; 755 756 case SESIOC_INIT: 757 rv = (*ssc->ses_vec.init_enc)(ssc); 758 break; 759 760 case SESIOC_GETENCSTAT: 761 if ((ssc->ses_encstat & ENCI_SVALID) == 0) { 762 rv = (*ssc->ses_vec.get_encstat)(ssc, KM_SLEEP); 763 if (rv) { 764 break; 765 } 766 } 767 t = ssc->ses_encstat & 0xf; 768 if (ddi_copyout(&t, (void *)arg, sizeof (t), flg)) 769 rv = EFAULT; 770 /* 771 * And always invalidate enclosure status on the way out. 772 */ 773 mutex_enter(SES_MUTEX); 774 ssc->ses_encstat &= ~ENCI_SVALID; 775 mutex_exit(SES_MUTEX); 776 break; 777 778 case SESIOC_SETENCSTAT: 779 if (ddi_copyin((void *)arg, &t, sizeof (t), flg)) 780 rv = EFAULT; 781 else 782 rv = (*ssc->ses_vec.set_encstat)(ssc, t, KM_SLEEP); 783 mutex_enter(SES_MUTEX); 784 ssc->ses_encstat &= ~ENCI_SVALID; 785 mutex_exit(SES_MUTEX); 786 break; 787 788 case SESIOC_GETOBJSTAT: 789 if (ddi_copyin((void *)arg, &x, sizeof (x), flg)) { 790 rv = EFAULT; 791 break; 792 } 793 if (x.obj_id >= ssc->ses_nobjects) { 794 rv = EINVAL; 795 break; 796 } 797 if ((rv = (*ssc->ses_vec.get_objstat)(ssc, &x, KM_SLEEP)) != 0) 798 break; 799 if (ddi_copyout(&x, (void *)arg, sizeof (x), flg)) 800 rv = EFAULT; 801 else { 802 /* 803 * Now that we no longer poll, svalid never stays true. 804 */ 805 mutex_enter(SES_MUTEX); 806 ssc->ses_objmap[x.obj_id].svalid = 0; 807 mutex_exit(SES_MUTEX); 808 } 809 break; 810 811 case SESIOC_SETOBJSTAT: 812 if (ddi_copyin((void *)arg, &x, sizeof (x), flg)) { 813 rv = EFAULT; 814 break; 815 } 816 if (x.obj_id >= ssc->ses_nobjects) { 817 rv = EINVAL; 818 break; 819 } 820 rv = (*ssc->ses_vec.set_objstat)(ssc, &x, KM_SLEEP); 821 if (rv == 0) { 822 mutex_enter(SES_MUTEX); 823 ssc->ses_objmap[x.obj_id].svalid = 0; 824 mutex_exit(SES_MUTEX); 825 } 826 break; 827 828 case USCSICMD: 829 rv = ses_uscsi_cmd(ssc, (Uscmd *)arg, flg); 830 break; 831 832 default: 833 rv = ENOTTY; 834 break; 835 } 836 return (rv); 837 } 838 839 840 /* 841 * Loop on running a kernel based command 842 * 843 * FIXME: This routine is not really needed. 844 */ 845 int 846 ses_runcmd(ses_softc_t *ssc, Uscmd *lp) 847 { 848 int e; 849 850 lp->uscsi_status = 0; 851 e = ses_uscsi_cmd(ssc, lp, FKIOCTL); 852 853 #ifdef not 854 /* 855 * Debug: Nice cross-check code for verifying consistent status. 856 */ 857 if (lp->uscsi_status) { 858 if (lp->uscsi_status == STATUS_CHECK) { 859 SES_LOG(ssc, CE_NOTE, "runcmd<cdb[0]=" 860 "0x%x->%s ASC/ASCQ=0x%x/0x%x>", 861 lp->uscsi_cdb[0], 862 scsi_sname(lp->uscsi_rqbuf[2] & 0xf), 863 lp->uscsi_rqbuf[12] & 0xff, 864 lp->uscsi_rqbuf[13] & 0xff); 865 } else { 866 SES_LOG(ssc, CE_NOTE, "runcmd<cdb[0]=" 867 "0x%x -> Status 0x%x", lp->uscsi_cdb[0], 868 lp->uscsi_status); 869 } 870 } 871 #endif /* not */ 872 return (e); 873 } 874 875 876 /* 877 * Run a scsi command. 878 */ 879 int 880 ses_uscsi_cmd(ses_softc_t *ssc, Uscmd *Uc, int Uf) 881 { 882 Uscmd *uscmd; 883 struct buf *bp; 884 enum uio_seg uioseg; 885 int err; 886 887 /* 888 * Grab local 'special' buffer 889 */ 890 mutex_enter(SES_MUTEX); 891 while (ssc->ses_sbufbsy) { 892 cv_wait(&ssc->ses_sbufcv, &ssc->ses_devp->sd_mutex); 893 } 894 ssc->ses_sbufbsy = 1; 895 mutex_exit(SES_MUTEX); 896 897 /* 898 * If the device is powered down, request it's activation. 899 * This check must be done after setting ses_sbufbsy! 900 */ 901 if (ssc->ses_suspended && 902 ddi_dev_is_needed(SES_DEVINFO(ssc), 0, 1) != DDI_SUCCESS) { 903 mutex_enter(SES_MUTEX); 904 ssc->ses_sbufbsy = 0; 905 mutex_exit(SES_MUTEX); 906 return (EIO); 907 } 908 909 err = scsi_uscsi_alloc_and_copyin((intptr_t)Uc, Uf, 910 SES_ROUTE(ssc), &uscmd); 911 if (err != 0) { 912 SES_LOG(ssc, SES_CE_DEBUG1, "ses_uscsi_cmd: " 913 "scsi_uscsi_alloc_and_copyin failed\n"); 914 mutex_enter(SES_MUTEX); 915 ssc->ses_sbufbsy = 0; 916 cv_signal(&ssc->ses_sbufcv); 917 mutex_exit(SES_MUTEX); 918 SES_LOG(ssc, SES_CE_DEBUG2, efl, __LINE__); 919 return (err); 920 } 921 922 /* 923 * Copy the uscsi command related infos to ssc for use in ses_start() 924 * and ses_callback(). 925 */ 926 bcopy(uscmd, &ssc->ses_uscsicmd, sizeof (Uscmd)); 927 if (uscmd->uscsi_cdb != NULL) { 928 bcopy(uscmd->uscsi_cdb, &ssc->ses_srqcdb, 929 (size_t)(uscmd->uscsi_cdblen)); 930 } 931 ssc->ses_uscsicmd.uscsi_status = 0; 932 933 bp = ssc->ses_sbufp; 934 bp->av_back = (struct buf *)NULL; 935 bp->av_forw = (struct buf *)NULL; 936 bp->b_back = (struct buf *)ssc; 937 bp->b_edev = NODEV; 938 939 if (uscmd->uscsi_cdb != NULL) { 940 if (uscmd->uscsi_cdblen == CDB_GROUP0) { 941 SES_LOG(ssc, SES_CE_DEBUG7, 942 "scsi_cmd: %x %x %x %x %x %x", 943 ((char *)uscmd->uscsi_cdb)[0], 944 ((char *)uscmd->uscsi_cdb)[1], 945 ((char *)uscmd->uscsi_cdb)[2], 946 ((char *)uscmd->uscsi_cdb)[3], 947 ((char *)uscmd->uscsi_cdb)[4], 948 ((char *)uscmd->uscsi_cdb)[5]); 949 } else { 950 SES_LOG(ssc, SES_CE_DEBUG7, 951 "scsi cmd: %x %x %x %x %x %x %x %x %x %x", 952 ((char *)uscmd->uscsi_cdb)[0], 953 ((char *)uscmd->uscsi_cdb)[1], 954 ((char *)uscmd->uscsi_cdb)[2], 955 ((char *)uscmd->uscsi_cdb)[3], 956 ((char *)uscmd->uscsi_cdb)[4], 957 ((char *)uscmd->uscsi_cdb)[5], 958 ((char *)uscmd->uscsi_cdb)[6], 959 ((char *)uscmd->uscsi_cdb)[7], 960 ((char *)uscmd->uscsi_cdb)[8], 961 ((char *)uscmd->uscsi_cdb)[9]); 962 } 963 } 964 965 uioseg = (Uf & FKIOCTL) ? UIO_SYSSPACE : UIO_USERSPACE; 966 err = scsi_uscsi_handle_cmd(NODEV, uioseg, uscmd, 967 ses_start, bp, NULL); 968 969 /* 970 * ses_callback() may set values for ssc->ses_uscsicmd or 971 * ssc->ses_srqsbuf, so copy them back to uscmd. 972 */ 973 if (uscmd->uscsi_rqbuf != NULL) { 974 bcopy(&ssc->ses_srqsbuf, uscmd->uscsi_rqbuf, 975 (size_t)(uscmd->uscsi_rqlen)); 976 uscmd->uscsi_rqresid = ssc->ses_uscsicmd.uscsi_rqresid; 977 } 978 uscmd->uscsi_status = ssc->ses_uscsicmd.uscsi_status; 979 980 (void) scsi_uscsi_copyout_and_free((intptr_t)Uc, uscmd); 981 mutex_enter(SES_MUTEX); 982 ssc->ses_sbufbsy = 0; 983 cv_signal(&ssc->ses_sbufcv); 984 mutex_exit(SES_MUTEX); 985 986 return (err); 987 } 988 989 990 991 /* 992 * Command start and done functions. 993 */ 994 static int 995 ses_start(struct buf *bp) 996 { 997 ses_softc_t *ssc = (ses_softc_t *)bp->b_back; 998 999 SES_LOG(ssc, SES_CE_DEBUG9, "ses_start"); 1000 if (!BP_PKT(bp)) { 1001 /* 1002 * Allocate a packet. 1003 */ 1004 ses_get_pkt(bp, SLEEP_FUNC); 1005 if (!BP_PKT(bp)) { 1006 int err; 1007 bp->b_resid = bp->b_bcount; 1008 if (geterror(bp) == 0) 1009 SET_BP_ERROR(bp, EIO); 1010 err = geterror(bp); 1011 biodone(bp); 1012 return (err); 1013 } 1014 } 1015 1016 /* 1017 * Initialize the transfer residue, error code, and retry count. 1018 */ 1019 bp->b_resid = 0; 1020 SET_BP_ERROR(bp, 0); 1021 1022 #if !defined(lint) 1023 _NOTE(NO_COMPETING_THREADS_NOW); 1024 #endif /* !defined(lint) */ 1025 ssc->ses_retries = ses_retry_count; 1026 1027 #if !defined(lint) 1028 /* LINTED */ 1029 _NOTE(COMPETING_THREADS_NOW); 1030 #endif /* !defined(lint) */ 1031 1032 SES_LOG(ssc, SES_CE_DEBUG9, "ses_start -> scsi_transport"); 1033 switch (scsi_transport(BP_PKT(bp))) { 1034 case TRAN_ACCEPT: 1035 return (0); 1036 /* break; */ 1037 1038 case TRAN_BUSY: 1039 SES_LOG(ssc, SES_CE_DEBUG2, 1040 "ses_start: TRANSPORT BUSY"); 1041 SES_ENABLE_RESTART(SES_RESTART_TIME, BP_PKT(bp)); 1042 return (0); 1043 /* break; */ 1044 1045 default: 1046 SES_LOG(ssc, SES_CE_DEBUG2, "TRANSPORT ERROR\n"); 1047 SET_BP_ERROR(bp, EIO); 1048 scsi_destroy_pkt(BP_PKT(bp)); 1049 SET_BP_PKT(bp, NULL); 1050 biodone(bp); 1051 return (EIO); 1052 /* break; */ 1053 } 1054 } 1055 1056 1057 static void 1058 ses_get_pkt(struct buf *bp, int (*func)()) 1059 { 1060 ses_softc_t *ssc = (ses_softc_t *)bp->b_back; 1061 Uscmd *scmd = &ssc->ses_uscsicmd; 1062 struct scsi_pkt *pkt; 1063 int stat_size = 1; 1064 int flags = 0; 1065 1066 if ((scmd->uscsi_flags & USCSI_RQENABLE) && ssc->ses_arq) { 1067 if (scmd->uscsi_rqlen > SENSE_LENGTH) { 1068 stat_size = (int)(scmd->uscsi_rqlen) + 1069 sizeof (struct scsi_arq_status) - 1070 sizeof (struct scsi_extended_sense); 1071 flags = PKT_XARQ; 1072 } else { 1073 stat_size = sizeof (struct scsi_arq_status); 1074 } 1075 } 1076 1077 if (bp->b_bcount) { 1078 pkt = scsi_init_pkt(SES_ROUTE(ssc), NULL, bp, 1079 scmd->uscsi_cdblen, stat_size, 0, flags, 1080 func, (caddr_t)ssc); 1081 } else { 1082 pkt = scsi_init_pkt(SES_ROUTE(ssc), NULL, NULL, 1083 scmd->uscsi_cdblen, stat_size, 0, flags, 1084 func, (caddr_t)ssc); 1085 } 1086 SET_BP_PKT(bp, pkt); 1087 if (pkt == (struct scsi_pkt *)NULL) 1088 return; 1089 bcopy(scmd->uscsi_cdb, pkt->pkt_cdbp, (size_t)scmd->uscsi_cdblen); 1090 pkt->pkt_time = scmd->uscsi_timeout; 1091 1092 pkt->pkt_comp = ses_callback; 1093 pkt->pkt_private = (opaque_t)ssc; 1094 } 1095 1096 1097 /* 1098 * Restart ses command. 1099 */ 1100 static void 1101 ses_restart(void *arg) 1102 { 1103 struct scsi_pkt *pkt = (struct scsi_pkt *)arg; 1104 ses_softc_t *ssc = (ses_softc_t *)pkt->pkt_private; 1105 struct buf *bp = ssc->ses_sbufp; 1106 SES_LOG(ssc, SES_CE_DEBUG9, "ses_restart"); 1107 1108 ssc->ses_restart_id = NULL; 1109 1110 switch (scsi_transport(pkt)) { 1111 case TRAN_ACCEPT: 1112 SES_LOG(ssc, SES_CE_DEBUG9, 1113 "RESTART %d ok", ssc->ses_retries); 1114 return; 1115 /* break; */ 1116 case TRAN_BUSY: 1117 SES_LOG(ssc, SES_CE_DEBUG1, 1118 "RESTART %d TRANSPORT BUSY\n", ssc->ses_retries); 1119 if (ssc->ses_retries > SES_NO_RETRY) { 1120 ssc->ses_retries -= SES_BUSY_RETRY; 1121 SES_ENABLE_RESTART(SES_RESTART_TIME, pkt); 1122 return; 1123 } 1124 SET_BP_ERROR(bp, EBUSY); 1125 break; 1126 default: 1127 SET_BP_ERROR(bp, EIO); 1128 break; 1129 } 1130 SES_LOG(ssc, SES_CE_DEBUG1, 1131 "RESTART %d TRANSPORT FAILED\n", ssc->ses_retries); 1132 1133 pkt = (struct scsi_pkt *)bp->av_back; 1134 scsi_destroy_pkt(pkt); 1135 bp->b_resid = bp->b_bcount; 1136 SET_BP_PKT(bp, NULL); 1137 biodone(bp); 1138 } 1139 1140 1141 /* 1142 * Command completion processing 1143 */ 1144 #define HBA_RESET (STAT_BUS_RESET|STAT_DEV_RESET|STAT_ABORTED) 1145 static void 1146 ses_callback(struct scsi_pkt *pkt) 1147 { 1148 ses_softc_t *ssc = (ses_softc_t *)pkt->pkt_private; 1149 struct buf *bp; 1150 Uscmd *scmd; 1151 int err; 1152 char action; 1153 1154 bp = ssc->ses_sbufp; 1155 scmd = &ssc->ses_uscsicmd; 1156 /* SES_LOG(ssc, SES_CE_DEBUG9, "ses_callback"); */ 1157 1158 /* 1159 * Optimization: Normal completion. 1160 */ 1161 if (pkt->pkt_reason == CMD_CMPLT && 1162 !SCBP_C(pkt) && 1163 !(pkt->pkt_flags & FLAG_SENSING) && 1164 !pkt->pkt_resid) { 1165 scsi_destroy_pkt(pkt); 1166 SET_BP_PKT(bp, NULL); 1167 biodone(bp); 1168 return; 1169 } 1170 1171 1172 /* 1173 * Abnormal completion. 1174 * 1175 * Assume most common error initially. 1176 */ 1177 err = EIO; 1178 action = COMMAND_DONE; 1179 if (scmd->uscsi_flags & USCSI_DIAGNOSE) { 1180 ssc->ses_retries = SES_NO_RETRY; 1181 } 1182 1183 CHECK_PKT: 1184 if (pkt->pkt_reason != CMD_CMPLT) { 1185 /* Process transport errors. */ 1186 switch (pkt->pkt_reason) { 1187 case CMD_TIMEOUT: 1188 /* 1189 * If the transport layer didn't clear the problem, 1190 * reset the target. 1191 */ 1192 if (! (pkt->pkt_statistics & HBA_RESET)) { 1193 (void) scsi_reset(&pkt->pkt_address, 1194 RESET_TARGET); 1195 } 1196 err = ETIMEDOUT; 1197 break; 1198 1199 case CMD_INCOMPLETE: 1200 case CMD_UNX_BUS_FREE: 1201 /* 1202 * No response? If probing, give up. 1203 * Otherwise, keep trying until retries exhausted. 1204 * Then lockdown the driver as the device is 1205 * unplugged. 1206 */ 1207 if (ssc->ses_retries <= SES_NO_RETRY && 1208 !(scmd->uscsi_flags & USCSI_DIAGNOSE)) { 1209 ssc->ses_present = SES_CLOSED; 1210 } 1211 /* Inhibit retries to speed probe/attach. */ 1212 if (ssc->ses_present < SES_OPEN) { 1213 ssc->ses_retries = SES_NO_RETRY; 1214 } 1215 /* SES_CMD_RETRY4(ssc->ses_retries); */ 1216 err = ENXIO; 1217 break; 1218 1219 case CMD_DATA_OVR: 1220 /* 1221 * XXX: Some HBA's (e.g. Adaptec 1740 and 1222 * earlier ISP revs) report a DATA OVERRUN 1223 * error instead of a transfer residue. So, 1224 * we convert the error and restart. 1225 */ 1226 if ((bp->b_bcount - pkt->pkt_resid) > 0) { 1227 SES_LOG(ssc, SES_CE_DEBUG6, 1228 "ignoring overrun"); 1229 pkt->pkt_reason = CMD_CMPLT; 1230 err = EOK; 1231 goto CHECK_PKT; 1232 } 1233 ssc->ses_retries = SES_NO_RETRY; 1234 /* err = EIO; */ 1235 break; 1236 1237 case CMD_DMA_DERR: 1238 ssc->ses_retries = SES_NO_RETRY; 1239 err = EFAULT; 1240 break; 1241 1242 default: 1243 /* err = EIO; */ 1244 break; 1245 } 1246 if (pkt == ssc->ses_rqpkt) { 1247 SES_LOG(ssc, CE_WARN, fail_msg, 1248 "Request Sense ", 1249 scsi_rname(pkt->pkt_reason), 1250 (ssc->ses_retries > 0)? 1251 "retrying": "giving up"); 1252 pkt = (struct scsi_pkt *)bp->av_back; 1253 action = QUE_SENSE; 1254 } else { 1255 SES_LOG(ssc, CE_WARN, fail_msg, 1256 "", scsi_rname(pkt->pkt_reason), 1257 (ssc->ses_retries > 0)? 1258 "retrying": "giving up"); 1259 action = QUE_COMMAND; 1260 } 1261 /* Device exists, allow full error recovery. */ 1262 if (ssc->ses_retries > SES_NO_RETRY) { 1263 ssc->ses_present = SES_OPEN; 1264 } 1265 1266 1267 /* 1268 * Process status and sense data errors. 1269 */ 1270 } else { 1271 ssc->ses_present = SES_OPEN; 1272 action = ses_decode_sense(pkt, &err); 1273 } 1274 1275 1276 /* 1277 * Initiate error recovery action, as needed. 1278 */ 1279 switch (action) { 1280 case QUE_COMMAND_NOW: 1281 /* SES_LOG(ssc, SES_CE_DEBUG1, "retrying cmd now"); */ 1282 if (ssc->ses_retries > SES_NO_RETRY) { 1283 ssc->ses_retries -= SES_CMD_RETRY; 1284 scmd->uscsi_status = 0; 1285 if (ssc->ses_arq) 1286 bzero(pkt->pkt_scbp, 1287 sizeof (struct scsi_arq_status)); 1288 1289 if (scsi_transport((struct scsi_pkt *)bp->av_back) 1290 != TRAN_ACCEPT) { 1291 SES_ENABLE_RESTART(SES_RESTART_TIME, 1292 (struct scsi_pkt *)bp->av_back); 1293 } 1294 return; 1295 } 1296 break; 1297 1298 case QUE_COMMAND: 1299 SES_LOG(ssc, SES_CE_DEBUG1, "retrying cmd"); 1300 if (ssc->ses_retries > SES_NO_RETRY) { 1301 ssc->ses_retries -= 1302 (err == EBUSY)? SES_BUSY_RETRY: SES_CMD_RETRY; 1303 scmd->uscsi_status = 0; 1304 if (ssc->ses_arq) 1305 bzero(pkt->pkt_scbp, 1306 sizeof (struct scsi_arq_status)); 1307 1308 SES_ENABLE_RESTART( 1309 (err == EBUSY)? SES_BUSY_TIME: SES_RESTART_TIME, 1310 (struct scsi_pkt *)bp->av_back); 1311 return; 1312 } 1313 break; 1314 1315 case QUE_SENSE: 1316 SES_LOG(ssc, SES_CE_DEBUG1, "retrying sense"); 1317 if (ssc->ses_retries > SES_NO_RETRY) { 1318 ssc->ses_retries -= SES_SENSE_RETRY; 1319 scmd->uscsi_status = 0; 1320 bzero(&ssc->ses_srqsbuf, MAX_SENSE_LENGTH); 1321 1322 if (scsi_transport(ssc->ses_rqpkt) != TRAN_ACCEPT) { 1323 SES_ENABLE_RESTART(SES_RESTART_TIME, 1324 ssc->ses_rqpkt); 1325 } 1326 return; 1327 } 1328 break; 1329 1330 case COMMAND_DONE: 1331 SES_LOG(ssc, SES_CE_DEBUG4, "cmd done"); 1332 pkt = (struct scsi_pkt *)bp->av_back; 1333 bp->b_resid = pkt->pkt_resid; 1334 if (bp->b_resid) { 1335 SES_LOG(ssc, SES_CE_DEBUG6, 1336 "transfer residue %ld(%ld)", 1337 bp->b_bcount - bp->b_resid, bp->b_bcount); 1338 } 1339 break; 1340 } 1341 pkt = (struct scsi_pkt *)bp->av_back; 1342 if (err) { 1343 SES_LOG(ssc, SES_CE_DEBUG1, "SES: ERROR %d\n", err); 1344 SET_BP_ERROR(bp, err); 1345 bp->b_resid = bp->b_bcount; 1346 } 1347 scsi_destroy_pkt(pkt); 1348 SET_BP_PKT(bp, NULL); 1349 biodone(bp); 1350 } 1351 1352 1353 /* 1354 * Check status and sense data and determine recovery. 1355 */ 1356 static int 1357 ses_decode_sense(struct scsi_pkt *pkt, int *err) 1358 { 1359 ses_softc_t *ssc = (ses_softc_t *)pkt->pkt_private; 1360 struct scsi_extended_sense *sense = 1361 (struct scsi_extended_sense *)&ssc->ses_srqsbuf; 1362 Uscmd *scmd = &ssc->ses_uscsicmd; 1363 char sense_flag = 0; 1364 uchar_t status = SCBP_C(pkt) & STATUS_MASK; 1365 char *err_action; 1366 char action; 1367 uchar_t rqlen; 1368 int amt; 1369 1370 /* 1371 * Process manual request sense. 1372 * Copy manual request sense to sense buffer. 1373 * 1374 * This is done if auto request sense is not enabled. 1375 * Or the auto request sense failed and the request 1376 * sense needs to be retried. 1377 */ 1378 if (pkt->pkt_flags & FLAG_SENSING) { 1379 struct buf *sbp = ssc->ses_rqbp; 1380 amt = min(MAX_SENSE_LENGTH, 1381 sbp->b_bcount - sbp->b_resid); 1382 rqlen = min((uchar_t)amt, scmd->uscsi_rqlen); 1383 bcopy(sbp->b_un.b_addr, sense, rqlen); 1384 scmd->uscsi_rqresid = scmd->uscsi_rqlen - rqlen; 1385 sense_flag = 1; 1386 /* 1387 * Process auto request sense. 1388 * Copy auto request sense to sense buffer. 1389 * 1390 * If auto request sense failed due to transport error, 1391 * retry the command. Otherwise process the status and 1392 * sense data. 1393 */ 1394 } else if (ssc->ses_arq && pkt->pkt_state & STATE_ARQ_DONE) { 1395 struct scsi_arq_status *arq = 1396 (struct scsi_arq_status *)(pkt->pkt_scbp); 1397 uchar_t *arq_status = (uchar_t *)&arq->sts_rqpkt_status; 1398 if (pkt->pkt_state & STATE_XARQ_DONE) { 1399 amt = MAX_SENSE_LENGTH - arq->sts_rqpkt_resid; 1400 } else { 1401 if (arq->sts_rqpkt_resid > SENSE_LENGTH) { 1402 amt = MAX_SENSE_LENGTH - arq->sts_rqpkt_resid; 1403 } else { 1404 amt = SENSE_LENGTH - arq->sts_rqpkt_resid; 1405 } 1406 } 1407 1408 if (arq->sts_rqpkt_reason != CMD_CMPLT) { 1409 return (QUE_COMMAND); 1410 } 1411 1412 rqlen = min((uchar_t)amt, scmd->uscsi_rqlen); 1413 bcopy(&arq->sts_sensedata, sense, rqlen); 1414 scmd->uscsi_status = status; 1415 scmd->uscsi_rqresid = scmd->uscsi_rqlen - rqlen; 1416 status = *arq_status & STATUS_MASK; 1417 pkt->pkt_state &= ~STATE_ARQ_DONE; 1418 sense_flag = 1; 1419 } 1420 1421 1422 /* 1423 * Check status of REQUEST SENSE or command. 1424 * 1425 * If it's not successful, try retrying the original command 1426 * and hope that it goes away. If not, we'll eventually run 1427 * out of retries and die. 1428 */ 1429 switch (status) { 1430 case STATUS_GOOD: 1431 case STATUS_INTERMEDIATE: 1432 case STATUS_MET: 1433 /* 1434 * If the command status is ok, we're done. 1435 * Otherwise, examine the request sense data. 1436 */ 1437 if (! sense_flag) { 1438 *err = EOK; 1439 return (COMMAND_DONE); 1440 } 1441 break; 1442 1443 case STATUS_CHECK: 1444 SES_LOG(ssc, SES_CE_DEBUG3, "status decode: check"); 1445 *err = EIO; 1446 return (QUE_SENSE); 1447 /* break; */ 1448 1449 case STATUS_BUSY: 1450 SES_LOG(ssc, SES_CE_DEBUG1, "status decode: busy"); 1451 /* SES_CMD_RETRY2(ssc->ses_retries); */ 1452 *err = EBUSY; 1453 return (QUE_COMMAND); 1454 /* break; */ 1455 1456 case STATUS_RESERVATION_CONFLICT: 1457 SES_LOG(ssc, SES_CE_DEBUG1, "status decode: reserved"); 1458 *err = EACCES; 1459 return (COMMAND_DONE_ERROR); 1460 /* break; */ 1461 1462 case STATUS_TERMINATED: 1463 SES_LOG(ssc, SES_CE_DEBUG1, "status decode: terminated"); 1464 *err = ECANCELED; 1465 return (COMMAND_DONE_ERROR); 1466 /* break; */ 1467 1468 default: 1469 SES_LOG(ssc, SES_CE_DEBUG1, "status 0x%x", status); 1470 *err = EIO; 1471 return (QUE_COMMAND); 1472 /* break; */ 1473 } 1474 1475 1476 /* 1477 * Check REQUEST SENSE error code. 1478 * 1479 * Either there's no error, a retryable error, 1480 * or it's dead. SES devices aren't very complex. 1481 */ 1482 err_action = "retrying"; 1483 switch (sense->es_key) { 1484 case KEY_RECOVERABLE_ERROR: 1485 *err = EOK; 1486 err_action = "recovered"; 1487 action = COMMAND_DONE; 1488 break; 1489 1490 case KEY_UNIT_ATTENTION: 1491 /* 1492 * This is common for RAID! 1493 */ 1494 /* *err = EIO; */ 1495 SES_CMD_RETRY1(ssc->ses_retries); 1496 action = QUE_COMMAND_NOW; 1497 break; 1498 1499 case KEY_NOT_READY: 1500 case KEY_NO_SENSE: 1501 /* *err = EIO; */ 1502 action = QUE_COMMAND; 1503 break; 1504 1505 default: 1506 /* *err = EIO; */ 1507 err_action = "fatal"; 1508 action = COMMAND_DONE_ERROR; 1509 break; 1510 } 1511 SES_LOG(ssc, SES_CE_DEBUG1, 1512 "cdb[0]= 0x%x %s, key=0x%x, ASC/ASCQ=0x%x/0x%x", 1513 scmd->uscsi_cdb[0], err_action, 1514 sense->es_key, sense->es_add_code, sense->es_qual_code); 1515 1516 #ifdef not 1517 /* 1518 * Dump cdb and sense data stat's for manufacturing. 1519 */ 1520 if (DEBUGGING_ERR || sd_error_level == SDERR_ALL) { 1521 auto buf[128]; 1522 1523 p = pkt->pkt_cdbp; 1524 if ((j = scsi_cdb_size[CDB_GROUPID(*p)]) == 0) 1525 j = CDB_SIZE; 1526 1527 /* Print cdb */ 1528 (void) sprintf(buf, "cmd:"); 1529 for (i = 0; i < j; i++) { 1530 (void) sprintf(&buf[strlen(buf)], 1531 hex, (uchar_t)*p++); 1532 } 1533 SES_LOG(ssc, SES_CE_DEBUG3, "%s", buf); 1534 1535 /* Suppress trailing zero's in sense data */ 1536 if (amt > 3) { 1537 p = (char *)devp->sd_sense + amt; 1538 for (j = amt; j > 3; j--) { 1539 if (*(--p)) break; 1540 } 1541 } else { 1542 j = amt; 1543 } 1544 1545 /* Print sense data. */ 1546 (void) sprintf(buf, "sense:"); 1547 p = (char *)devp->sd_sense; 1548 for (i = 0; i < j; i++) { 1549 (void) sprintf(&buf[strlen(buf)], 1550 hex, (uchar_t)*p++); 1551 } 1552 SES_LOG(ssc, SES_CE_DEBUG3, "%s", buf); 1553 } 1554 #endif /* not */ 1555 return (action); 1556 } 1557 1558 1559 /*PRINTFLIKE3*/ 1560 void 1561 ses_log(ses_softc_t *ssc, int level, const char *fmt, ...) 1562 { 1563 va_list ap; 1564 char buf[256]; 1565 1566 va_start(ap, fmt); 1567 (void) vsprintf(buf, fmt, ap); 1568 va_end(ap); 1569 1570 if (ssc == (ses_softc_t *)NULL) { 1571 switch (level) { 1572 case SES_CE_DEBUG1: 1573 if (ses_debug > 1) 1574 cmn_err(CE_NOTE, "%s", buf); 1575 break; 1576 case SES_CE_DEBUG2: 1577 if (ses_debug > 2) 1578 cmn_err(CE_NOTE, "%s", buf); 1579 break; 1580 case SES_CE_DEBUG3: 1581 if (ses_debug > 3) 1582 cmn_err(CE_NOTE, "%s", buf); 1583 break; 1584 case SES_CE_DEBUG4: 1585 if (ses_debug > 4) 1586 cmn_err(CE_NOTE, "%s", buf); 1587 break; 1588 case SES_CE_DEBUG5: 1589 if (ses_debug > 5) 1590 cmn_err(CE_NOTE, "%s", buf); 1591 break; 1592 case SES_CE_DEBUG6: 1593 if (ses_debug > 6) 1594 cmn_err(CE_NOTE, "%s", buf); 1595 break; 1596 case SES_CE_DEBUG7: 1597 if (ses_debug > 7) 1598 cmn_err(CE_NOTE, "%s", buf); 1599 break; 1600 case SES_CE_DEBUG8: 1601 if (ses_debug > 8) 1602 cmn_err(CE_NOTE, "%s", buf); 1603 break; 1604 case SES_CE_DEBUG9: 1605 if (ses_debug > 9) 1606 cmn_err(CE_NOTE, "%s", buf); 1607 break; 1608 case CE_NOTE: 1609 case CE_WARN: 1610 case CE_PANIC: 1611 cmn_err(level, "%s", buf); 1612 break; 1613 case SES_CE_DEBUG: 1614 default: 1615 cmn_err(CE_NOTE, "%s", buf); 1616 break; 1617 } 1618 return; 1619 } 1620 1621 switch (level) { 1622 case CE_CONT: 1623 case CE_NOTE: 1624 case CE_WARN: 1625 case CE_PANIC: 1626 scsi_log(SES_DEVINFO(ssc), (char *)Snm, level, Str, buf); 1627 break; 1628 case SES_CE_DEBUG1: 1629 if (ses_debug > 1) 1630 scsi_log(SES_DEVINFO(ssc), (char *)Snm, SCSI_DEBUG, 1631 Str, buf); 1632 break; 1633 case SES_CE_DEBUG2: 1634 if (ses_debug > 2) 1635 scsi_log(SES_DEVINFO(ssc), (char *)Snm, SCSI_DEBUG, 1636 Str, buf); 1637 break; 1638 case SES_CE_DEBUG3: 1639 if (ses_debug > 3) 1640 scsi_log(SES_DEVINFO(ssc), (char *)Snm, SCSI_DEBUG, 1641 Str, buf); 1642 break; 1643 case SES_CE_DEBUG4: 1644 if (ses_debug > 4) 1645 scsi_log(SES_DEVINFO(ssc), (char *)Snm, SCSI_DEBUG, 1646 Str, buf); 1647 break; 1648 case SES_CE_DEBUG5: 1649 if (ses_debug > 5) 1650 scsi_log(SES_DEVINFO(ssc), (char *)Snm, SCSI_DEBUG, 1651 Str, buf); 1652 break; 1653 case SES_CE_DEBUG6: 1654 if (ses_debug > 6) 1655 scsi_log(SES_DEVINFO(ssc), (char *)Snm, SCSI_DEBUG, 1656 Str, buf); 1657 break; 1658 case SES_CE_DEBUG7: 1659 if (ses_debug > 7) 1660 scsi_log(SES_DEVINFO(ssc), (char *)Snm, SCSI_DEBUG, 1661 Str, buf); 1662 break; 1663 case SES_CE_DEBUG8: 1664 if (ses_debug > 8) 1665 scsi_log(SES_DEVINFO(ssc), (char *)Snm, SCSI_DEBUG, 1666 Str, buf); 1667 break; 1668 case SES_CE_DEBUG9: 1669 if (ses_debug > 9) 1670 scsi_log(SES_DEVINFO(ssc), (char *)Snm, SCSI_DEBUG, 1671 Str, buf); 1672 break; 1673 case SES_CE_DEBUG: 1674 default: 1675 scsi_log(SES_DEVINFO(ssc), (char *)Snm, SCSI_DEBUG, Str, buf); 1676 break; 1677 } 1678 } 1679 /* 1680 * mode: c 1681 * Local variables: 1682 * c-indent-level: 8 1683 * c-brace-imaginary-offset: 0 1684 * c-brace-offset: -8 1685 * c-argdecl-indent: 8 1686 * c-label-offset: -8 1687 * c-continued-statement-offset: 8 1688 * c-continued-brace-offset: 0 1689 * End: 1690 */ 1691