1 /*- 2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD 3 * 4 * Copyright (c) 2000 Matthew Jacob 5 * All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions, and the following disclaimer, 12 * without modification, immediately at the beginning of the file. 13 * 2. The name of the author may not be used to endorse or promote products 14 * derived from this software without specific prior written permission. 15 * 16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR 20 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 26 * SUCH DAMAGE. 27 */ 28 29 #include <sys/cdefs.h> 30 __FBSDID("$FreeBSD$"); 31 32 #include <sys/param.h> 33 34 #include <sys/conf.h> 35 #include <sys/errno.h> 36 #include <sys/fcntl.h> 37 #include <sys/kernel.h> 38 #include <sys/kthread.h> 39 #include <sys/lock.h> 40 #include <sys/malloc.h> 41 #include <sys/mutex.h> 42 #include <sys/proc.h> 43 #include <sys/queue.h> 44 #include <sys/sbuf.h> 45 #include <sys/sx.h> 46 #include <sys/sysent.h> 47 #include <sys/systm.h> 48 #include <sys/sysctl.h> 49 #include <sys/types.h> 50 51 #include <machine/stdarg.h> 52 53 #include <cam/cam.h> 54 #include <cam/cam_ccb.h> 55 #include <cam/cam_debug.h> 56 #include <cam/cam_periph.h> 57 #include <cam/cam_xpt_periph.h> 58 59 #include <cam/scsi/scsi_all.h> 60 #include <cam/scsi/scsi_message.h> 61 #include <cam/scsi/scsi_enc.h> 62 #include <cam/scsi/scsi_enc_internal.h> 63 64 #include "opt_ses.h" 65 66 MALLOC_DEFINE(M_SCSIENC, "SCSI ENC", "SCSI ENC buffers"); 67 68 /* Enclosure type independent driver */ 69 70 static d_open_t enc_open; 71 static d_close_t enc_close; 72 static d_ioctl_t enc_ioctl; 73 static periph_init_t enc_init; 74 static periph_ctor_t enc_ctor; 75 static periph_oninv_t enc_oninvalidate; 76 static periph_dtor_t enc_dtor; 77 78 static void enc_async(void *, uint32_t, struct cam_path *, void *); 79 static enctyp enc_type(struct ccb_getdev *); 80 81 SYSCTL_NODE(_kern_cam, OID_AUTO, enc, CTLFLAG_RD | CTLFLAG_MPSAFE, 0, 82 "CAM Enclosure Services driver"); 83 84 #if defined(DEBUG) || defined(ENC_DEBUG) 85 int enc_verbose = 1; 86 #else 87 int enc_verbose = 0; 88 #endif 89 SYSCTL_INT(_kern_cam_enc, OID_AUTO, verbose, CTLFLAG_RWTUN, 90 &enc_verbose, 0, "Enable verbose logging"); 91 92 const char *elm_type_names[] = ELM_TYPE_NAMES; 93 CTASSERT(nitems(elm_type_names) - 1 == ELMTYP_LAST); 94 95 static struct periph_driver encdriver = { 96 enc_init, "ses", 97 TAILQ_HEAD_INITIALIZER(encdriver.units), /* generation */ 0 98 }; 99 100 PERIPHDRIVER_DECLARE(enc, encdriver); 101 102 static struct cdevsw enc_cdevsw = { 103 .d_version = D_VERSION, 104 .d_open = enc_open, 105 .d_close = enc_close, 106 .d_ioctl = enc_ioctl, 107 .d_name = "ses", 108 .d_flags = D_TRACKCLOSE, 109 }; 110 111 static void 112 enc_init(void) 113 { 114 cam_status status; 115 116 /* 117 * Install a global async callback. This callback will 118 * receive async callbacks like "new device found". 119 */ 120 status = xpt_register_async(AC_FOUND_DEVICE, enc_async, NULL, NULL); 121 122 if (status != CAM_REQ_CMP) { 123 printf("enc: Failed to attach master async callback " 124 "due to status 0x%x!\n", status); 125 } 126 } 127 128 static void 129 enc_devgonecb(void *arg) 130 { 131 struct cam_periph *periph; 132 struct enc_softc *enc; 133 struct mtx *mtx; 134 int i; 135 136 periph = (struct cam_periph *)arg; 137 mtx = cam_periph_mtx(periph); 138 mtx_lock(mtx); 139 enc = (struct enc_softc *)periph->softc; 140 141 /* 142 * When we get this callback, we will get no more close calls from 143 * devfs. So if we have any dangling opens, we need to release the 144 * reference held for that particular context. 145 */ 146 for (i = 0; i < enc->open_count; i++) 147 cam_periph_release_locked(periph); 148 149 enc->open_count = 0; 150 151 /* 152 * Release the reference held for the device node, it is gone now. 153 */ 154 cam_periph_release_locked(periph); 155 156 /* 157 * We reference the lock directly here, instead of using 158 * cam_periph_unlock(). The reason is that the final call to 159 * cam_periph_release_locked() above could result in the periph 160 * getting freed. If that is the case, dereferencing the periph 161 * with a cam_periph_unlock() call would cause a page fault. 162 */ 163 mtx_unlock(mtx); 164 } 165 166 static void 167 enc_oninvalidate(struct cam_periph *periph) 168 { 169 struct enc_softc *enc; 170 171 enc = periph->softc; 172 173 enc->enc_flags |= ENC_FLAG_INVALID; 174 175 /* If the sub-driver has an invalidate routine, call it */ 176 if (enc->enc_vec.softc_invalidate != NULL) 177 enc->enc_vec.softc_invalidate(enc); 178 179 /* 180 * Unregister any async callbacks. 181 */ 182 xpt_register_async(0, enc_async, periph, periph->path); 183 184 /* 185 * Shutdown our daemon. 186 */ 187 enc->enc_flags |= ENC_FLAG_SHUTDOWN; 188 if (enc->enc_daemon != NULL) { 189 /* Signal the ses daemon to terminate. */ 190 wakeup(enc->enc_daemon); 191 } 192 callout_drain(&enc->status_updater); 193 194 destroy_dev_sched_cb(enc->enc_dev, enc_devgonecb, periph); 195 } 196 197 static void 198 enc_dtor(struct cam_periph *periph) 199 { 200 struct enc_softc *enc; 201 202 enc = periph->softc; 203 204 /* If the sub-driver has a cleanup routine, call it */ 205 if (enc->enc_vec.softc_cleanup != NULL) 206 enc->enc_vec.softc_cleanup(enc); 207 208 root_mount_rel(&enc->enc_rootmount); 209 210 ENC_FREE(enc); 211 } 212 213 static void 214 enc_async(void *callback_arg, uint32_t code, struct cam_path *path, void *arg) 215 { 216 struct cam_periph *periph; 217 218 periph = (struct cam_periph *)callback_arg; 219 220 switch(code) { 221 case AC_FOUND_DEVICE: 222 { 223 struct ccb_getdev *cgd; 224 cam_status status; 225 path_id_t path_id; 226 227 cgd = (struct ccb_getdev *)arg; 228 if (arg == NULL) { 229 break; 230 } 231 232 if (enc_type(cgd) == ENC_NONE) { 233 /* 234 * Schedule announcement of the ENC bindings for 235 * this device if it is managed by a SEP. 236 */ 237 path_id = xpt_path_path_id(path); 238 xpt_lock_buses(); 239 TAILQ_FOREACH(periph, &encdriver.units, unit_links) { 240 struct enc_softc *softc; 241 242 softc = (struct enc_softc *)periph->softc; 243 244 /* Check this SEP is ready. */ 245 if (softc == NULL || (softc->enc_flags & 246 ENC_FLAG_INITIALIZED) == 0 || 247 softc->enc_vec.device_found == NULL) 248 continue; 249 250 /* Check this SEP may manage this device. */ 251 if (xpt_path_path_id(periph->path) != path_id && 252 (softc->enc_type != ENC_SEMB_SES || 253 cgd->protocol != PROTO_ATA)) 254 continue; 255 256 softc->enc_vec.device_found(softc); 257 } 258 xpt_unlock_buses(); 259 return; 260 } 261 262 status = cam_periph_alloc(enc_ctor, enc_oninvalidate, 263 enc_dtor, NULL, "ses", CAM_PERIPH_BIO, 264 path, enc_async, AC_FOUND_DEVICE, cgd); 265 266 if (status != CAM_REQ_CMP && status != CAM_REQ_INPROG) { 267 printf("enc_async: Unable to probe new device due to " 268 "status 0x%x\n", status); 269 } 270 break; 271 } 272 default: 273 cam_periph_async(periph, code, path, arg); 274 break; 275 } 276 } 277 278 static int 279 enc_open(struct cdev *dev, int flags, int fmt, struct thread *td) 280 { 281 struct cam_periph *periph; 282 struct enc_softc *softc; 283 int error = 0; 284 285 periph = (struct cam_periph *)dev->si_drv1; 286 if (cam_periph_acquire(periph) != 0) 287 return (ENXIO); 288 289 cam_periph_lock(periph); 290 291 softc = (struct enc_softc *)periph->softc; 292 293 if ((softc->enc_flags & ENC_FLAG_INITIALIZED) == 0) { 294 error = ENXIO; 295 goto out; 296 } 297 if (softc->enc_flags & ENC_FLAG_INVALID) { 298 error = ENXIO; 299 goto out; 300 } 301 out: 302 if (error != 0) 303 cam_periph_release_locked(periph); 304 else 305 softc->open_count++; 306 307 cam_periph_unlock(periph); 308 309 return (error); 310 } 311 312 static int 313 enc_close(struct cdev *dev, int flag, int fmt, struct thread *td) 314 { 315 struct cam_periph *periph; 316 struct enc_softc *enc; 317 struct mtx *mtx; 318 319 periph = (struct cam_periph *)dev->si_drv1; 320 mtx = cam_periph_mtx(periph); 321 mtx_lock(mtx); 322 323 enc = periph->softc; 324 enc->open_count--; 325 326 cam_periph_release_locked(periph); 327 328 /* 329 * We reference the lock directly here, instead of using 330 * cam_periph_unlock(). The reason is that the call to 331 * cam_periph_release_locked() above could result in the periph 332 * getting freed. If that is the case, dereferencing the periph 333 * with a cam_periph_unlock() call would cause a page fault. 334 * 335 * cam_periph_release() avoids this problem using the same method, 336 * but we're manually acquiring and dropping the lock here to 337 * protect the open count and avoid another lock acquisition and 338 * release. 339 */ 340 mtx_unlock(mtx); 341 342 return (0); 343 } 344 345 int 346 enc_error(union ccb *ccb, uint32_t cflags, uint32_t sflags) 347 { 348 struct enc_softc *softc; 349 struct cam_periph *periph; 350 351 periph = xpt_path_periph(ccb->ccb_h.path); 352 softc = (struct enc_softc *)periph->softc; 353 354 return (cam_periph_error(ccb, cflags, sflags)); 355 } 356 357 static int 358 enc_ioctl(struct cdev *dev, u_long cmd, caddr_t arg_addr, int flag, 359 struct thread *td) 360 { 361 struct cam_periph *periph; 362 encioc_enc_status_t tmp; 363 encioc_string_t sstr; 364 encioc_elm_status_t elms; 365 encioc_elm_desc_t elmd; 366 encioc_elm_devnames_t elmdn; 367 encioc_element_t *uelm; 368 enc_softc_t *enc; 369 enc_cache_t *cache; 370 void *addr; 371 int error, i; 372 373 #ifdef COMPAT_FREEBSD32 374 if (SV_PROC_FLAG(td->td_proc, SV_ILP32)) 375 return (ENOTTY); 376 #endif 377 378 if (arg_addr) 379 addr = *((caddr_t *) arg_addr); 380 else 381 addr = NULL; 382 383 periph = (struct cam_periph *)dev->si_drv1; 384 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("entering encioctl\n")); 385 386 cam_periph_lock(periph); 387 enc = (struct enc_softc *)periph->softc; 388 cache = &enc->enc_cache; 389 390 /* 391 * Now check to see whether we're initialized or not. 392 * This actually should never fail as we're not supposed 393 * to get past enc_open w/o successfully initializing 394 * things. 395 */ 396 if ((enc->enc_flags & ENC_FLAG_INITIALIZED) == 0) { 397 cam_periph_unlock(periph); 398 return (ENXIO); 399 } 400 cam_periph_unlock(periph); 401 402 error = 0; 403 404 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, 405 ("trying to do ioctl %#lx\n", cmd)); 406 407 /* 408 * If this command can change the device's state, 409 * we must have the device open for writing. 410 * 411 * For commands that get information about the 412 * device- we don't need to lock the peripheral 413 * if we aren't running a command. The periph 414 * also can't go away while a user process has 415 * it open. 416 */ 417 switch (cmd) { 418 case ENCIOC_GETNELM: 419 case ENCIOC_GETELMMAP: 420 case ENCIOC_GETENCSTAT: 421 case ENCIOC_GETELMSTAT: 422 case ENCIOC_GETELMDESC: 423 case ENCIOC_GETELMDEVNAMES: 424 case ENCIOC_GETENCNAME: 425 case ENCIOC_GETENCID: 426 break; 427 default: 428 if ((flag & FWRITE) == 0) { 429 return (EBADF); 430 } 431 } 432 433 /* 434 * XXX The values read here are only valid for the current 435 * configuration generation. We need these ioctls 436 * to also pass in/out a generation number. 437 */ 438 sx_slock(&enc->enc_cache_lock); 439 switch (cmd) { 440 case ENCIOC_GETNELM: 441 error = copyout(&cache->nelms, addr, sizeof (cache->nelms)); 442 break; 443 444 case ENCIOC_GETELMMAP: 445 for (uelm = addr, i = 0; i != cache->nelms; i++) { 446 encioc_element_t kelm; 447 kelm.elm_idx = i; 448 kelm.elm_subenc_id = cache->elm_map[i].subenclosure; 449 kelm.elm_type = cache->elm_map[i].elm_type; 450 error = copyout(&kelm, &uelm[i], sizeof(kelm)); 451 if (error) 452 break; 453 } 454 break; 455 456 case ENCIOC_GETENCSTAT: 457 cam_periph_lock(periph); 458 error = enc->enc_vec.get_enc_status(enc, 1); 459 if (error) { 460 cam_periph_unlock(periph); 461 break; 462 } 463 tmp = cache->enc_status; 464 cam_periph_unlock(periph); 465 error = copyout(&tmp, addr, sizeof(tmp)); 466 cache->enc_status = tmp; 467 break; 468 469 case ENCIOC_SETENCSTAT: 470 error = copyin(addr, &tmp, sizeof(tmp)); 471 if (error) 472 break; 473 cam_periph_lock(periph); 474 error = enc->enc_vec.set_enc_status(enc, tmp, 1); 475 cam_periph_unlock(periph); 476 break; 477 478 case ENCIOC_GETSTRING: 479 case ENCIOC_SETSTRING: 480 case ENCIOC_GETENCNAME: 481 case ENCIOC_GETENCID: 482 if (enc->enc_vec.handle_string == NULL) { 483 error = EINVAL; 484 break; 485 } 486 error = copyin(addr, &sstr, sizeof(sstr)); 487 if (error) 488 break; 489 cam_periph_lock(periph); 490 error = enc->enc_vec.handle_string(enc, &sstr, cmd); 491 cam_periph_unlock(periph); 492 if (error == 0 || error == ENOMEM) 493 (void)copyout(&sstr.bufsiz, 494 &((encioc_string_t *)addr)->bufsiz, 495 sizeof(sstr.bufsiz)); 496 break; 497 498 case ENCIOC_GETELMSTAT: 499 error = copyin(addr, &elms, sizeof(elms)); 500 if (error) 501 break; 502 if (elms.elm_idx >= cache->nelms) { 503 error = EINVAL; 504 break; 505 } 506 cam_periph_lock(periph); 507 error = enc->enc_vec.get_elm_status(enc, &elms, 1); 508 cam_periph_unlock(periph); 509 if (error) 510 break; 511 error = copyout(&elms, addr, sizeof(elms)); 512 break; 513 514 case ENCIOC_GETELMDESC: 515 error = copyin(addr, &elmd, sizeof(elmd)); 516 if (error) 517 break; 518 if (elmd.elm_idx >= cache->nelms) { 519 error = EINVAL; 520 break; 521 } 522 if (enc->enc_vec.get_elm_desc != NULL) { 523 error = enc->enc_vec.get_elm_desc(enc, &elmd); 524 if (error) 525 break; 526 } else 527 elmd.elm_desc_len = 0; 528 error = copyout(&elmd, addr, sizeof(elmd)); 529 break; 530 531 case ENCIOC_GETELMDEVNAMES: 532 if (enc->enc_vec.get_elm_devnames == NULL) { 533 error = EINVAL; 534 break; 535 } 536 error = copyin(addr, &elmdn, sizeof(elmdn)); 537 if (error) 538 break; 539 if (elmdn.elm_idx >= cache->nelms) { 540 error = EINVAL; 541 break; 542 } 543 cam_periph_lock(periph); 544 error = (*enc->enc_vec.get_elm_devnames)(enc, &elmdn); 545 cam_periph_unlock(periph); 546 if (error) 547 break; 548 error = copyout(&elmdn, addr, sizeof(elmdn)); 549 break; 550 551 case ENCIOC_SETELMSTAT: 552 error = copyin(addr, &elms, sizeof(elms)); 553 if (error) 554 break; 555 556 if (elms.elm_idx >= cache->nelms) { 557 error = EINVAL; 558 break; 559 } 560 cam_periph_lock(periph); 561 error = enc->enc_vec.set_elm_status(enc, &elms, 1); 562 cam_periph_unlock(periph); 563 564 break; 565 566 case ENCIOC_INIT: 567 568 cam_periph_lock(periph); 569 error = enc->enc_vec.init_enc(enc); 570 cam_periph_unlock(periph); 571 break; 572 573 default: 574 cam_periph_lock(periph); 575 error = cam_periph_ioctl(periph, cmd, arg_addr, enc_error); 576 cam_periph_unlock(periph); 577 break; 578 } 579 sx_sunlock(&enc->enc_cache_lock); 580 return (error); 581 } 582 583 int 584 enc_runcmd(struct enc_softc *enc, char *cdb, int cdbl, char *dptr, int *dlenp) 585 { 586 int error, dlen, tdlen; 587 ccb_flags ddf; 588 union ccb *ccb; 589 590 CAM_DEBUG(enc->periph->path, CAM_DEBUG_TRACE, 591 ("entering enc_runcmd\n")); 592 if (dptr) { 593 if ((dlen = *dlenp) < 0) { 594 dlen = -dlen; 595 ddf = CAM_DIR_OUT; 596 } else { 597 ddf = CAM_DIR_IN; 598 } 599 } else { 600 dlen = 0; 601 ddf = CAM_DIR_NONE; 602 } 603 604 if (cdbl > IOCDBLEN) { 605 cdbl = IOCDBLEN; 606 } 607 608 ccb = cam_periph_getccb(enc->periph, CAM_PRIORITY_NORMAL); 609 if (enc->enc_type == ENC_SEMB_SES || enc->enc_type == ENC_SEMB_SAFT) { 610 tdlen = min(dlen, 1020); 611 tdlen = (tdlen + 3) & ~3; 612 cam_fill_ataio(&ccb->ataio, 0, NULL, ddf, 0, dptr, tdlen, 613 30 * 1000); 614 if (cdb[0] == RECEIVE_DIAGNOSTIC) 615 ata_28bit_cmd(&ccb->ataio, 616 ATA_SEP_ATTN, cdb[2], 0x02, tdlen / 4); 617 else if (cdb[0] == SEND_DIAGNOSTIC) 618 ata_28bit_cmd(&ccb->ataio, 619 ATA_SEP_ATTN, dlen > 0 ? dptr[0] : 0, 620 0x82, tdlen / 4); 621 else if (cdb[0] == READ_BUFFER) 622 ata_28bit_cmd(&ccb->ataio, 623 ATA_SEP_ATTN, cdb[2], 0x00, tdlen / 4); 624 else 625 ata_28bit_cmd(&ccb->ataio, 626 ATA_SEP_ATTN, dlen > 0 ? dptr[0] : 0, 627 0x80, tdlen / 4); 628 } else { 629 tdlen = dlen; 630 cam_fill_csio(&ccb->csio, 0, NULL, ddf, MSG_SIMPLE_Q_TAG, 631 dptr, dlen, sizeof (struct scsi_sense_data), cdbl, 632 60 * 1000); 633 bcopy(cdb, ccb->csio.cdb_io.cdb_bytes, cdbl); 634 } 635 636 error = cam_periph_runccb(ccb, enc_error, ENC_CFLAGS, ENC_FLAGS, NULL); 637 if (error) { 638 if (dptr) { 639 *dlenp = dlen; 640 } 641 } else { 642 if (dptr) { 643 if (ccb->ccb_h.func_code == XPT_ATA_IO) 644 *dlenp = ccb->ataio.resid; 645 else 646 *dlenp = ccb->csio.resid; 647 *dlenp += tdlen - dlen; 648 } 649 } 650 xpt_release_ccb(ccb); 651 CAM_DEBUG(enc->periph->path, CAM_DEBUG_SUBTRACE, 652 ("exiting enc_runcmd: *dlenp = %d\n", *dlenp)); 653 return (error); 654 } 655 656 void 657 enc_log(struct enc_softc *enc, const char *fmt, ...) 658 { 659 va_list ap; 660 661 printf("%s%d: ", enc->periph->periph_name, enc->periph->unit_number); 662 va_start(ap, fmt); 663 vprintf(fmt, ap); 664 va_end(ap); 665 } 666 667 /* 668 * The code after this point runs on many platforms, 669 * so forgive the slightly awkward and nonconforming 670 * appearance. 671 */ 672 673 /* 674 * Is this a device that supports enclosure services? 675 * 676 * It's a pretty simple ruleset- if it is device type 677 * 0x0D (13), it's an ENCLOSURE device. 678 */ 679 680 #define SAFTE_START 44 681 #define SAFTE_END 50 682 #define SAFTE_LEN SAFTE_END-SAFTE_START 683 684 static enctyp 685 enc_type(struct ccb_getdev *cgd) 686 { 687 int buflen; 688 unsigned char *iqd; 689 690 if (cgd->protocol == PROTO_SEMB) { 691 iqd = (unsigned char *)&cgd->ident_data; 692 if (STRNCMP(iqd + 43, "S-E-S", 5) == 0) 693 return (ENC_SEMB_SES); 694 else if (STRNCMP(iqd + 43, "SAF-TE", 6) == 0) 695 return (ENC_SEMB_SAFT); 696 return (ENC_NONE); 697 698 } else if (cgd->protocol != PROTO_SCSI) 699 return (ENC_NONE); 700 701 iqd = (unsigned char *)&cgd->inq_data; 702 buflen = min(sizeof(cgd->inq_data), 703 SID_ADDITIONAL_LENGTH(&cgd->inq_data)); 704 705 if ((iqd[0] & 0x1f) == T_ENCLOSURE) 706 return (ENC_SES); 707 708 #ifdef SES_ENABLE_PASSTHROUGH 709 if ((iqd[6] & 0x40) && (iqd[2] & 0x7) >= 2) { 710 /* 711 * PassThrough Device. 712 */ 713 return (ENC_SES_PASSTHROUGH); 714 } 715 #endif 716 717 /* 718 * The comparison is short for a reason- 719 * some vendors were chopping it short. 720 */ 721 722 if (buflen < SAFTE_END - 2) { 723 return (ENC_NONE); 724 } 725 726 if (STRNCMP((char *)&iqd[SAFTE_START], "SAF-TE", SAFTE_LEN - 2) == 0) { 727 return (ENC_SAFT); 728 } 729 return (ENC_NONE); 730 } 731 732 /*================== Enclosure Monitoring/Processing Daemon ==================*/ 733 /** 734 * \brief Queue an update request for a given action, if needed. 735 * 736 * \param enc SES softc to queue the request for. 737 * \param action Action requested. 738 */ 739 void 740 enc_update_request(enc_softc_t *enc, uint32_t action) 741 { 742 if ((enc->pending_actions & (0x1 << action)) == 0) { 743 enc->pending_actions |= (0x1 << action); 744 ENC_DLOG(enc, "%s: queing requested action %d\n", 745 __func__, action); 746 if (enc->current_action == ENC_UPDATE_NONE) 747 wakeup(enc->enc_daemon); 748 } else { 749 ENC_DLOG(enc, "%s: ignoring requested action %d - " 750 "Already queued\n", __func__, action); 751 } 752 } 753 754 /** 755 * \brief Invoke the handler of the highest priority pending 756 * state in the SES state machine. 757 * 758 * \param enc The SES instance invoking the state machine. 759 */ 760 static void 761 enc_fsm_step(enc_softc_t *enc) 762 { 763 union ccb *ccb; 764 uint8_t *buf; 765 struct enc_fsm_state *cur_state; 766 int error; 767 uint32_t xfer_len; 768 769 ENC_DLOG(enc, "%s enter %p\n", __func__, enc); 770 771 enc->current_action = ffs(enc->pending_actions) - 1; 772 enc->pending_actions &= ~(0x1 << enc->current_action); 773 774 cur_state = &enc->enc_fsm_states[enc->current_action]; 775 776 buf = NULL; 777 if (cur_state->buf_size != 0) { 778 cam_periph_unlock(enc->periph); 779 buf = malloc(cur_state->buf_size, M_SCSIENC, M_WAITOK|M_ZERO); 780 cam_periph_lock(enc->periph); 781 } 782 783 error = 0; 784 ccb = NULL; 785 if (cur_state->fill != NULL) { 786 ccb = cam_periph_getccb(enc->periph, CAM_PRIORITY_NORMAL); 787 788 error = cur_state->fill(enc, cur_state, ccb, buf); 789 if (error != 0) 790 goto done; 791 792 error = cam_periph_runccb(ccb, cur_state->error, 793 ENC_CFLAGS, 794 ENC_FLAGS|SF_QUIET_IR, NULL); 795 } 796 797 if (ccb != NULL) { 798 if (ccb->ccb_h.func_code == XPT_ATA_IO) 799 xfer_len = ccb->ataio.dxfer_len - ccb->ataio.resid; 800 else 801 xfer_len = ccb->csio.dxfer_len - ccb->csio.resid; 802 } else 803 xfer_len = 0; 804 805 cam_periph_unlock(enc->periph); 806 cur_state->done(enc, cur_state, ccb, &buf, error, xfer_len); 807 cam_periph_lock(enc->periph); 808 809 done: 810 ENC_DLOG(enc, "%s exit - result %d\n", __func__, error); 811 ENC_FREE_AND_NULL(buf); 812 if (ccb != NULL) 813 xpt_release_ccb(ccb); 814 } 815 816 /** 817 * \invariant Called with cam_periph mutex held. 818 */ 819 static void 820 enc_status_updater(void *arg) 821 { 822 enc_softc_t *enc; 823 824 enc = arg; 825 if (enc->enc_vec.poll_status != NULL) 826 enc->enc_vec.poll_status(enc); 827 } 828 829 static void 830 enc_daemon(void *arg) 831 { 832 enc_softc_t *enc; 833 834 enc = arg; 835 836 cam_periph_lock(enc->periph); 837 while ((enc->enc_flags & ENC_FLAG_SHUTDOWN) == 0) { 838 if (enc->pending_actions == 0) { 839 /* 840 * Reset callout and msleep, or 841 * issue timed task completion 842 * status command. 843 */ 844 enc->current_action = ENC_UPDATE_NONE; 845 846 /* 847 * We've been through our state machine at least 848 * once. Allow the transition to userland. 849 */ 850 root_mount_rel(&enc->enc_rootmount); 851 852 callout_reset(&enc->status_updater, 60*hz, 853 enc_status_updater, enc); 854 855 cam_periph_sleep(enc->periph, enc->enc_daemon, 856 PUSER, "idle", 0); 857 } else { 858 enc_fsm_step(enc); 859 } 860 } 861 enc->enc_daemon = NULL; 862 cam_periph_unlock(enc->periph); 863 cam_periph_release(enc->periph); 864 kproc_exit(0); 865 } 866 867 static int 868 enc_kproc_init(enc_softc_t *enc) 869 { 870 int result; 871 872 callout_init_mtx(&enc->status_updater, cam_periph_mtx(enc->periph), 0); 873 874 if (cam_periph_acquire(enc->periph) != 0) 875 return (ENXIO); 876 877 result = kproc_create(enc_daemon, enc, &enc->enc_daemon, /*flags*/0, 878 /*stackpgs*/0, "enc_daemon%d", 879 enc->periph->unit_number); 880 if (result == 0) { 881 /* Do an initial load of all page data. */ 882 cam_periph_lock(enc->periph); 883 enc->enc_vec.poll_status(enc); 884 cam_periph_unlock(enc->periph); 885 } else 886 cam_periph_release(enc->periph); 887 return (result); 888 } 889 890 static cam_status 891 enc_ctor(struct cam_periph *periph, void *arg) 892 { 893 cam_status status = CAM_REQ_CMP_ERR; 894 int err; 895 enc_softc_t *enc; 896 struct ccb_getdev *cgd; 897 char *tname; 898 struct make_dev_args args; 899 struct sbuf sb; 900 901 cgd = (struct ccb_getdev *)arg; 902 if (cgd == NULL) { 903 printf("enc_ctor: no getdev CCB, can't register device\n"); 904 goto out; 905 } 906 907 enc = ENC_MALLOCZ(sizeof(*enc)); 908 if (enc == NULL) { 909 printf("enc_ctor: Unable to probe new device. " 910 "Unable to allocate enc\n"); 911 goto out; 912 } 913 enc->periph = periph; 914 enc->current_action = ENC_UPDATE_INVALID; 915 916 enc->enc_type = enc_type(cgd); 917 sx_init(&enc->enc_cache_lock, "enccache"); 918 919 switch (enc->enc_type) { 920 case ENC_SES: 921 case ENC_SES_PASSTHROUGH: 922 case ENC_SEMB_SES: 923 err = ses_softc_init(enc); 924 break; 925 case ENC_SAFT: 926 case ENC_SEMB_SAFT: 927 err = safte_softc_init(enc); 928 break; 929 case ENC_NONE: 930 default: 931 ENC_FREE(enc); 932 return (CAM_REQ_CMP_ERR); 933 } 934 935 if (err) { 936 xpt_print(periph->path, "error %d initializing\n", err); 937 goto out; 938 } 939 940 /* 941 * Hold off userland until we have made at least one pass 942 * through our state machine so that physical path data is 943 * present. 944 */ 945 if (enc->enc_vec.poll_status != NULL) { 946 root_mount_hold_token(periph->periph_name, &enc->enc_rootmount); 947 } 948 949 /* 950 * The softc field is set only once the enc is fully initialized 951 * so that we can rely on this field to detect partially 952 * initialized periph objects in the AC_FOUND_DEVICE handler. 953 */ 954 periph->softc = enc; 955 956 cam_periph_unlock(periph); 957 if (enc->enc_vec.poll_status != NULL) { 958 err = enc_kproc_init(enc); 959 if (err) { 960 xpt_print(periph->path, 961 "error %d starting enc_daemon\n", err); 962 goto out; 963 } 964 } 965 966 /* 967 * Acquire a reference to the periph before we create the devfs 968 * instance for it. We'll release this reference once the devfs 969 * instance has been freed. 970 */ 971 if (cam_periph_acquire(periph) != 0) { 972 xpt_print(periph->path, "%s: lost periph during " 973 "registration!\n", __func__); 974 cam_periph_lock(periph); 975 976 return (CAM_REQ_CMP_ERR); 977 } 978 979 make_dev_args_init(&args); 980 args.mda_devsw = &enc_cdevsw; 981 args.mda_unit = periph->unit_number; 982 args.mda_uid = UID_ROOT; 983 args.mda_gid = GID_OPERATOR; 984 args.mda_mode = 0600; 985 args.mda_si_drv1 = periph; 986 err = make_dev_s(&args, &enc->enc_dev, "%s%d", periph->periph_name, 987 periph->unit_number); 988 cam_periph_lock(periph); 989 if (err != 0) { 990 cam_periph_release_locked(periph); 991 return (CAM_REQ_CMP_ERR); 992 } 993 994 enc->enc_flags |= ENC_FLAG_INITIALIZED; 995 996 /* 997 * Add an async callback so that we get notified if this 998 * device goes away. 999 */ 1000 xpt_register_async(AC_LOST_DEVICE, enc_async, periph, periph->path); 1001 1002 switch (enc->enc_type) { 1003 default: 1004 case ENC_NONE: 1005 tname = "No ENC device"; 1006 break; 1007 case ENC_SES: 1008 tname = "SES Device"; 1009 break; 1010 case ENC_SES_PASSTHROUGH: 1011 tname = "SES Passthrough Device"; 1012 break; 1013 case ENC_SAFT: 1014 tname = "SAF-TE Device"; 1015 break; 1016 case ENC_SEMB_SES: 1017 tname = "SEMB SES Device"; 1018 break; 1019 case ENC_SEMB_SAFT: 1020 tname = "SEMB SAF-TE Device"; 1021 break; 1022 } 1023 1024 sbuf_new(&sb, enc->announce_buf, ENC_ANNOUNCE_SZ, SBUF_FIXEDLEN); 1025 xpt_announce_periph_sbuf(periph, &sb, tname); 1026 sbuf_finish(&sb); 1027 sbuf_putbuf(&sb); 1028 1029 status = CAM_REQ_CMP; 1030 1031 out: 1032 if (status != CAM_REQ_CMP) 1033 enc_dtor(periph); 1034 return (status); 1035 } 1036