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