1 /* 2 * Implementation of the Target Mode 'Black Hole device' for CAM. 3 * 4 * Copyright (c) 1999 Justin T. Gibbs. 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 * $FreeBSD$ 29 */ 30 31 #include <sys/param.h> 32 #include <sys/queue.h> 33 #include <sys/systm.h> 34 #include <sys/kernel.h> 35 #include <sys/types.h> 36 #include <sys/bio.h> 37 #include <sys/conf.h> 38 #include <sys/devicestat.h> 39 #include <sys/malloc.h> 40 #include <sys/uio.h> 41 42 #include <cam/cam.h> 43 #include <cam/cam_ccb.h> 44 #include <cam/cam_periph.h> 45 #include <cam/cam_queue.h> 46 #include <cam/cam_xpt_periph.h> 47 #include <cam/cam_debug.h> 48 49 #include <cam/scsi/scsi_all.h> 50 #include <cam/scsi/scsi_message.h> 51 52 typedef enum { 53 TARGBH_STATE_NORMAL, 54 TARGBH_STATE_EXCEPTION, 55 TARGBH_STATE_TEARDOWN 56 } targbh_state; 57 58 typedef enum { 59 TARGBH_FLAG_NONE = 0x00, 60 TARGBH_FLAG_LUN_ENABLED = 0x01 61 } targbh_flags; 62 63 typedef enum { 64 TARGBH_CCB_WORKQ, 65 TARGBH_CCB_WAITING 66 } targbh_ccb_types; 67 68 #define MAX_ACCEPT 8 69 #define MAX_IMMEDIATE 16 70 #define MAX_BUF_SIZE 256 /* Max inquiry/sense/mode page transfer */ 71 72 #define MIN(a, b) ((a > b) ? b : a) 73 74 /* Offsets into our private CCB area for storing accept information */ 75 #define ccb_type ppriv_field0 76 #define ccb_descr ppriv_ptr1 77 78 /* We stick a pointer to the originating accept TIO in each continue I/O CCB */ 79 #define ccb_atio ppriv_ptr1 80 81 TAILQ_HEAD(ccb_queue, ccb_hdr); 82 83 struct targbh_softc { 84 struct ccb_queue pending_queue; 85 struct ccb_queue work_queue; 86 struct ccb_queue unknown_atio_queue; 87 struct devstat device_stats; 88 targbh_state state; 89 targbh_flags flags; 90 u_int init_level; 91 u_int inq_data_len; 92 struct ccb_accept_tio *accept_tio_list; 93 struct ccb_hdr_slist immed_notify_slist; 94 }; 95 96 struct targbh_cmd_desc { 97 struct ccb_accept_tio* atio_link; 98 u_int data_resid; /* How much left to transfer */ 99 u_int data_increment;/* Amount to send before next disconnect */ 100 void* data; /* The data. Can be from backing_store or not */ 101 void* backing_store;/* Backing store allocated for this descriptor*/ 102 u_int max_size; /* Size of backing_store */ 103 u_int32_t timeout; 104 u_int8_t status; /* Status to return to initiator */ 105 }; 106 107 static struct scsi_inquiry_data no_lun_inq_data = 108 { 109 T_NODEVICE | (SID_QUAL_BAD_LU << 5), 0, 110 /* version */2, /* format version */2 111 }; 112 113 static struct scsi_sense_data no_lun_sense_data = 114 { 115 SSD_CURRENT_ERROR|SSD_ERRCODE_VALID, 116 0, 117 SSD_KEY_NOT_READY, 118 { 0, 0, 0, 0 }, 119 /*extra_len*/offsetof(struct scsi_sense_data, fru) 120 - offsetof(struct scsi_sense_data, extra_len), 121 { 0, 0, 0, 0 }, 122 /* Logical Unit Not Supported */ 123 /*ASC*/0x25, /*ASCQ*/0 124 }; 125 126 static const int request_sense_size = offsetof(struct scsi_sense_data, fru); 127 128 static periph_init_t targbhinit; 129 static void targbhasync(void *callback_arg, u_int32_t code, 130 struct cam_path *path, void *arg); 131 static cam_status targbhenlun(struct cam_periph *periph); 132 static cam_status targbhdislun(struct cam_periph *periph); 133 static periph_ctor_t targbhctor; 134 static periph_dtor_t targbhdtor; 135 static periph_start_t targbhstart; 136 static void targbhdone(struct cam_periph *periph, 137 union ccb *done_ccb); 138 #ifdef NOTYET 139 static int targbherror(union ccb *ccb, u_int32_t cam_flags, 140 u_int32_t sense_flags); 141 #endif 142 static struct targbh_cmd_desc* targbhallocdescr(void); 143 static void targbhfreedescr(struct targbh_cmd_desc *buf); 144 145 static struct periph_driver targbhdriver = 146 { 147 targbhinit, "targbh", 148 TAILQ_HEAD_INITIALIZER(targbhdriver.units), /* generation */ 0 149 }; 150 151 PERIPHDRIVER_DECLARE(targbh, targbhdriver); 152 153 static void 154 targbhinit(void) 155 { 156 cam_status status; 157 struct cam_path *path; 158 159 /* 160 * Install a global async callback. This callback will 161 * receive async callbacks like "new path registered". 162 */ 163 status = xpt_create_path(&path, /*periph*/NULL, CAM_XPT_PATH_ID, 164 CAM_TARGET_WILDCARD, CAM_LUN_WILDCARD); 165 166 if (status == CAM_REQ_CMP) { 167 struct ccb_setasync csa; 168 169 xpt_setup_ccb(&csa.ccb_h, path, /*priority*/5); 170 csa.ccb_h.func_code = XPT_SASYNC_CB; 171 csa.event_enable = AC_PATH_REGISTERED; 172 csa.callback = targbhasync; 173 csa.callback_arg = NULL; 174 xpt_action((union ccb *)&csa); 175 status = csa.ccb_h.status; 176 xpt_free_path(path); 177 } 178 179 if (status != CAM_REQ_CMP) { 180 printf("targbh: Failed to attach master async callback " 181 "due to status 0x%x!\n", status); 182 } 183 } 184 185 static void 186 targbhasync(void *callback_arg, u_int32_t code, 187 struct cam_path *path, void *arg) 188 { 189 struct cam_periph *periph; 190 191 periph = (struct cam_periph *)callback_arg; 192 switch (code) { 193 case AC_PATH_REGISTERED: 194 { 195 struct ccb_pathinq *cpi; 196 struct cam_path *new_path; 197 cam_status status; 198 199 cpi = (struct ccb_pathinq *)arg; 200 201 /* Only attach to controllers that support target mode */ 202 if ((cpi->target_sprt & PIT_PROCESSOR) == 0) 203 break; 204 205 /* 206 * Allocate a peripheral instance for 207 * this target instance. 208 */ 209 status = xpt_create_path(&new_path, NULL, 210 xpt_path_path_id(path), 211 CAM_TARGET_WILDCARD, CAM_LUN_WILDCARD); 212 if (status != CAM_REQ_CMP) { 213 printf("targbhasync: Unable to create path " 214 "due to status 0x%x\n", status); 215 break; 216 } 217 status = cam_periph_alloc(targbhctor, NULL, targbhdtor, 218 targbhstart, 219 "targbh", CAM_PERIPH_BIO, 220 new_path, targbhasync, 221 AC_PATH_REGISTERED, 222 cpi); 223 xpt_free_path(new_path); 224 break; 225 } 226 case AC_PATH_DEREGISTERED: 227 { 228 targbhdislun(periph); 229 break; 230 } 231 default: 232 break; 233 } 234 } 235 236 /* Attempt to enable our lun */ 237 static cam_status 238 targbhenlun(struct cam_periph *periph) 239 { 240 union ccb immed_ccb; 241 struct targbh_softc *softc; 242 cam_status status; 243 int i; 244 245 softc = (struct targbh_softc *)periph->softc; 246 247 if ((softc->flags & TARGBH_FLAG_LUN_ENABLED) != 0) 248 return (CAM_REQ_CMP); 249 250 xpt_setup_ccb(&immed_ccb.ccb_h, periph->path, /*priority*/1); 251 immed_ccb.ccb_h.func_code = XPT_EN_LUN; 252 253 /* Don't need support for any vendor specific commands */ 254 immed_ccb.cel.grp6_len = 0; 255 immed_ccb.cel.grp7_len = 0; 256 immed_ccb.cel.enable = 1; 257 xpt_action(&immed_ccb); 258 status = immed_ccb.ccb_h.status; 259 if (status != CAM_REQ_CMP) { 260 xpt_print_path(periph->path); 261 printf("targbhenlun - Enable Lun Rejected with status 0x%x\n", 262 status); 263 return (status); 264 } 265 266 softc->flags |= TARGBH_FLAG_LUN_ENABLED; 267 268 /* 269 * Build up a buffer of accept target I/O 270 * operations for incoming selections. 271 */ 272 for (i = 0; i < MAX_ACCEPT; i++) { 273 struct ccb_accept_tio *atio; 274 275 atio = (struct ccb_accept_tio*)malloc(sizeof(*atio), M_DEVBUF, 276 M_NOWAIT); 277 if (atio == NULL) { 278 status = CAM_RESRC_UNAVAIL; 279 break; 280 } 281 282 atio->ccb_h.ccb_descr = targbhallocdescr(); 283 284 if (atio->ccb_h.ccb_descr == NULL) { 285 free(atio, M_DEVBUF); 286 status = CAM_RESRC_UNAVAIL; 287 break; 288 } 289 290 xpt_setup_ccb(&atio->ccb_h, periph->path, /*priority*/1); 291 atio->ccb_h.func_code = XPT_ACCEPT_TARGET_IO; 292 atio->ccb_h.cbfcnp = targbhdone; 293 xpt_action((union ccb *)atio); 294 status = atio->ccb_h.status; 295 if (status != CAM_REQ_INPROG) { 296 targbhfreedescr(atio->ccb_h.ccb_descr); 297 free(atio, M_DEVBUF); 298 break; 299 } 300 ((struct targbh_cmd_desc*)atio->ccb_h.ccb_descr)->atio_link = 301 softc->accept_tio_list; 302 softc->accept_tio_list = atio; 303 } 304 305 if (i == 0) { 306 xpt_print_path(periph->path); 307 printf("targbhenlun - Could not allocate accept tio CCBs: " 308 "status = 0x%x\n", status); 309 targbhdislun(periph); 310 return (CAM_REQ_CMP_ERR); 311 } 312 313 /* 314 * Build up a buffer of immediate notify CCBs 315 * so the SIM can tell us of asynchronous target mode events. 316 */ 317 for (i = 0; i < MAX_ACCEPT; i++) { 318 struct ccb_immed_notify *inot; 319 320 inot = (struct ccb_immed_notify*)malloc(sizeof(*inot), M_DEVBUF, 321 M_NOWAIT); 322 323 if (inot == NULL) { 324 status = CAM_RESRC_UNAVAIL; 325 break; 326 } 327 328 xpt_setup_ccb(&inot->ccb_h, periph->path, /*priority*/1); 329 inot->ccb_h.func_code = XPT_IMMED_NOTIFY; 330 inot->ccb_h.cbfcnp = targbhdone; 331 xpt_action((union ccb *)inot); 332 status = inot->ccb_h.status; 333 if (status != CAM_REQ_INPROG) { 334 free(inot, M_DEVBUF); 335 break; 336 } 337 SLIST_INSERT_HEAD(&softc->immed_notify_slist, &inot->ccb_h, 338 periph_links.sle); 339 } 340 341 if (i == 0) { 342 xpt_print_path(periph->path); 343 printf("targbhenlun - Could not allocate immediate notify " 344 "CCBs: status = 0x%x\n", status); 345 targbhdislun(periph); 346 return (CAM_REQ_CMP_ERR); 347 } 348 349 return (CAM_REQ_CMP); 350 } 351 352 static cam_status 353 targbhdislun(struct cam_periph *periph) 354 { 355 union ccb ccb; 356 struct targbh_softc *softc; 357 struct ccb_accept_tio* atio; 358 struct ccb_hdr *ccb_h; 359 360 softc = (struct targbh_softc *)periph->softc; 361 if ((softc->flags & TARGBH_FLAG_LUN_ENABLED) == 0) 362 return CAM_REQ_CMP; 363 364 /* XXX Block for Continue I/O completion */ 365 366 /* Kill off all ACCECPT and IMMEDIATE CCBs */ 367 while ((atio = softc->accept_tio_list) != NULL) { 368 369 softc->accept_tio_list = 370 ((struct targbh_cmd_desc*)atio->ccb_h.ccb_descr)->atio_link; 371 xpt_setup_ccb(&ccb.cab.ccb_h, periph->path, /*priority*/1); 372 ccb.cab.ccb_h.func_code = XPT_ABORT; 373 ccb.cab.abort_ccb = (union ccb *)atio; 374 xpt_action(&ccb); 375 } 376 377 while ((ccb_h = SLIST_FIRST(&softc->immed_notify_slist)) != NULL) { 378 SLIST_REMOVE_HEAD(&softc->immed_notify_slist, periph_links.sle); 379 xpt_setup_ccb(&ccb.cab.ccb_h, periph->path, /*priority*/1); 380 ccb.cab.ccb_h.func_code = XPT_ABORT; 381 ccb.cab.abort_ccb = (union ccb *)ccb_h; 382 xpt_action(&ccb); 383 } 384 385 /* 386 * Dissable this lun. 387 */ 388 xpt_setup_ccb(&ccb.cel.ccb_h, periph->path, /*priority*/1); 389 ccb.cel.ccb_h.func_code = XPT_EN_LUN; 390 ccb.cel.enable = 0; 391 xpt_action(&ccb); 392 393 if (ccb.cel.ccb_h.status != CAM_REQ_CMP) 394 printf("targbhdislun - Disabling lun on controller failed " 395 "with status 0x%x\n", ccb.cel.ccb_h.status); 396 else 397 softc->flags &= ~TARGBH_FLAG_LUN_ENABLED; 398 return (ccb.cel.ccb_h.status); 399 } 400 401 static cam_status 402 targbhctor(struct cam_periph *periph, void *arg) 403 { 404 struct ccb_pathinq *cpi; 405 struct targbh_softc *softc; 406 407 cpi = (struct ccb_pathinq *)arg; 408 409 /* Allocate our per-instance private storage */ 410 softc = (struct targbh_softc *)malloc(sizeof(*softc), 411 M_DEVBUF, M_NOWAIT); 412 if (softc == NULL) { 413 printf("targctor: unable to malloc softc\n"); 414 return (CAM_REQ_CMP_ERR); 415 } 416 417 bzero(softc, sizeof(*softc)); 418 TAILQ_INIT(&softc->pending_queue); 419 TAILQ_INIT(&softc->work_queue); 420 softc->accept_tio_list = NULL; 421 SLIST_INIT(&softc->immed_notify_slist); 422 softc->state = TARGBH_STATE_NORMAL; 423 periph->softc = softc; 424 softc->init_level++; 425 426 return (targbhenlun(periph)); 427 } 428 429 static void 430 targbhdtor(struct cam_periph *periph) 431 { 432 struct targbh_softc *softc; 433 434 softc = (struct targbh_softc *)periph->softc; 435 436 softc->state = TARGBH_STATE_TEARDOWN; 437 438 targbhdislun(periph); 439 440 switch (softc->init_level) { 441 default: 442 /* FALLTHROUGH */ 443 case 1: 444 free(softc, M_DEVBUF); 445 break; 446 case 0: 447 panic("targdtor - impossible init level");; 448 } 449 } 450 451 static void 452 targbhstart(struct cam_periph *periph, union ccb *start_ccb) 453 { 454 struct targbh_softc *softc; 455 struct ccb_hdr *ccbh; 456 struct ccb_accept_tio *atio; 457 struct targbh_cmd_desc *desc; 458 struct ccb_scsiio *csio; 459 ccb_flags flags; 460 int s; 461 462 softc = (struct targbh_softc *)periph->softc; 463 464 s = splbio(); 465 ccbh = TAILQ_FIRST(&softc->work_queue); 466 if (periph->immediate_priority <= periph->pinfo.priority) { 467 start_ccb->ccb_h.ccb_type = TARGBH_CCB_WAITING; 468 SLIST_INSERT_HEAD(&periph->ccb_list, &start_ccb->ccb_h, 469 periph_links.sle); 470 periph->immediate_priority = CAM_PRIORITY_NONE; 471 splx(s); 472 wakeup(&periph->ccb_list); 473 } else if (ccbh == NULL) { 474 splx(s); 475 xpt_release_ccb(start_ccb); 476 } else { 477 TAILQ_REMOVE(&softc->work_queue, ccbh, periph_links.tqe); 478 TAILQ_INSERT_HEAD(&softc->pending_queue, ccbh, 479 periph_links.tqe); 480 splx(s); 481 atio = (struct ccb_accept_tio*)ccbh; 482 desc = (struct targbh_cmd_desc *)atio->ccb_h.ccb_descr; 483 484 /* Is this a tagged request? */ 485 flags = atio->ccb_h.flags & 486 (CAM_DIS_DISCONNECT|CAM_TAG_ACTION_VALID|CAM_DIR_MASK); 487 488 csio = &start_ccb->csio; 489 /* 490 * If we are done with the transaction, tell the 491 * controller to send status and perform a CMD_CMPLT. 492 * If we have associated sense data, see if we can 493 * send that too. 494 */ 495 if (desc->data_resid == desc->data_increment) { 496 flags |= CAM_SEND_STATUS; 497 if (atio->sense_len) { 498 csio->sense_len = atio->sense_len; 499 csio->sense_data = atio->sense_data; 500 flags |= CAM_SEND_SENSE; 501 } 502 503 } 504 505 cam_fill_ctio(csio, 506 /*retries*/2, 507 targbhdone, 508 flags, 509 (flags & CAM_TAG_ACTION_VALID)? 510 MSG_SIMPLE_Q_TAG : 0, 511 atio->tag_id, 512 atio->init_id, 513 desc->status, 514 /*data_ptr*/desc->data_increment == 0 515 ? NULL : desc->data, 516 /*dxfer_len*/desc->data_increment, 517 /*timeout*/desc->timeout); 518 519 /* Override our wildcard attachment */ 520 start_ccb->ccb_h.target_id = atio->ccb_h.target_id; 521 start_ccb->ccb_h.target_lun = atio->ccb_h.target_lun; 522 523 start_ccb->ccb_h.ccb_type = TARGBH_CCB_WORKQ; 524 start_ccb->ccb_h.ccb_atio = atio; 525 CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE, 526 ("Sending a CTIO\n")); 527 xpt_action(start_ccb); 528 /* 529 * If the queue was frozen waiting for the response 530 * to this ATIO (for instance disconnection was disallowed), 531 * then release it now that our response has been queued. 532 */ 533 if ((atio->ccb_h.status & CAM_DEV_QFRZN) != 0) { 534 cam_release_devq(periph->path, 535 /*relsim_flags*/0, 536 /*reduction*/0, 537 /*timeout*/0, 538 /*getcount_only*/0); 539 atio->ccb_h.status &= ~CAM_DEV_QFRZN; 540 } 541 s = splbio(); 542 ccbh = TAILQ_FIRST(&softc->work_queue); 543 splx(s); 544 } 545 if (ccbh != NULL) 546 xpt_schedule(periph, /*priority*/1); 547 } 548 549 static void 550 targbhdone(struct cam_periph *periph, union ccb *done_ccb) 551 { 552 struct targbh_softc *softc; 553 554 softc = (struct targbh_softc *)periph->softc; 555 556 if (done_ccb->ccb_h.ccb_type == TARGBH_CCB_WAITING) { 557 /* Caller will release the CCB */ 558 wakeup(&done_ccb->ccb_h.cbfcnp); 559 return; 560 } 561 562 switch (done_ccb->ccb_h.func_code) { 563 case XPT_ACCEPT_TARGET_IO: 564 { 565 struct ccb_accept_tio *atio; 566 struct targbh_cmd_desc *descr; 567 u_int8_t *cdb; 568 int priority; 569 570 atio = &done_ccb->atio; 571 descr = (struct targbh_cmd_desc*)atio->ccb_h.ccb_descr; 572 cdb = atio->cdb_io.cdb_bytes; 573 if (softc->state == TARGBH_STATE_TEARDOWN 574 || atio->ccb_h.status == CAM_REQ_ABORTED) { 575 targbhfreedescr(descr); 576 free(done_ccb, M_DEVBUF); 577 return; 578 } 579 580 /* 581 * Determine the type of incoming command and 582 * setup our buffer for a response. 583 */ 584 switch (cdb[0]) { 585 case INQUIRY: 586 { 587 struct scsi_inquiry *inq; 588 589 inq = (struct scsi_inquiry *)cdb; 590 CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE, 591 ("Saw an inquiry!\n")); 592 /* 593 * Validate the command. We don't 594 * support any VPD pages, so complain 595 * if EVPD is set. 596 */ 597 if ((inq->byte2 & SI_EVPD) != 0 598 || inq->page_code != 0) { 599 atio->ccb_h.flags &= ~CAM_DIR_MASK; 600 atio->ccb_h.flags |= CAM_DIR_NONE; 601 /* 602 * This needs to have other than a 603 * no_lun_sense_data response. 604 */ 605 atio->sense_data = no_lun_sense_data; 606 atio->sense_len = sizeof(no_lun_sense_data); 607 descr->data_resid = 0; 608 descr->data_increment = 0; 609 descr->status = SCSI_STATUS_CHECK_COND; 610 break; 611 } 612 /* 613 * Direction is always relative 614 * to the initator. 615 */ 616 atio->ccb_h.flags &= ~CAM_DIR_MASK; 617 atio->ccb_h.flags |= CAM_DIR_IN; 618 descr->data = &no_lun_inq_data; 619 descr->data_resid = MIN(sizeof(no_lun_inq_data), 620 SCSI_CDB6_LEN(inq->length)); 621 descr->data_increment = descr->data_resid; 622 descr->timeout = 5 * 1000; 623 descr->status = SCSI_STATUS_OK; 624 break; 625 } 626 case REQUEST_SENSE: 627 { 628 struct scsi_request_sense *rsense; 629 630 rsense = (struct scsi_request_sense *)cdb; 631 /* Refer to static sense data */ 632 atio->ccb_h.flags &= ~CAM_DIR_MASK; 633 atio->ccb_h.flags |= CAM_DIR_IN; 634 descr->data = &no_lun_sense_data; 635 descr->data_resid = request_sense_size; 636 descr->data_resid = MIN(descr->data_resid, 637 SCSI_CDB6_LEN(rsense->length)); 638 descr->data_increment = descr->data_resid; 639 descr->timeout = 5 * 1000; 640 descr->status = SCSI_STATUS_OK; 641 break; 642 } 643 default: 644 /* Constant CA, tell initiator */ 645 /* Direction is always relative to the initator */ 646 atio->ccb_h.flags &= ~CAM_DIR_MASK; 647 atio->ccb_h.flags |= CAM_DIR_NONE; 648 atio->sense_data = no_lun_sense_data; 649 atio->sense_len = sizeof (no_lun_sense_data); 650 descr->data_resid = 0; 651 descr->data_increment = 0; 652 descr->timeout = 5 * 1000; 653 descr->status = SCSI_STATUS_CHECK_COND; 654 break; 655 } 656 657 /* Queue us up to receive a Continue Target I/O ccb. */ 658 if ((atio->ccb_h.flags & CAM_DIS_DISCONNECT) != 0) { 659 TAILQ_INSERT_HEAD(&softc->work_queue, &atio->ccb_h, 660 periph_links.tqe); 661 priority = 0; 662 } else { 663 TAILQ_INSERT_TAIL(&softc->work_queue, &atio->ccb_h, 664 periph_links.tqe); 665 priority = 1; 666 } 667 xpt_schedule(periph, priority); 668 break; 669 } 670 case XPT_CONT_TARGET_IO: 671 { 672 struct ccb_accept_tio *atio; 673 struct targbh_cmd_desc *desc; 674 675 CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE, 676 ("Received completed CTIO\n")); 677 atio = (struct ccb_accept_tio*)done_ccb->ccb_h.ccb_atio; 678 desc = (struct targbh_cmd_desc *)atio->ccb_h.ccb_descr; 679 680 TAILQ_REMOVE(&softc->pending_queue, &atio->ccb_h, 681 periph_links.tqe); 682 683 /* 684 * We could check for CAM_SENT_SENSE bein set here, 685 * but since we're not maintaining any CA/UA state, 686 * there's no point. 687 */ 688 atio->sense_len = 0; 689 done_ccb->ccb_h.flags &= ~CAM_SEND_SENSE; 690 done_ccb->ccb_h.status &= ~CAM_SENT_SENSE; 691 692 /* 693 * Any errors will not change the data we return, 694 * so make sure the queue is not left frozen. 695 * XXX - At some point there may be errors that 696 * leave us in a connected state with the 697 * initiator... 698 */ 699 if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0) { 700 printf("Releasing Queue\n"); 701 cam_release_devq(done_ccb->ccb_h.path, 702 /*relsim_flags*/0, 703 /*reduction*/0, 704 /*timeout*/0, 705 /*getcount_only*/0); 706 done_ccb->ccb_h.status &= ~CAM_DEV_QFRZN; 707 } 708 desc->data_resid -= desc->data_increment; 709 xpt_release_ccb(done_ccb); 710 if (softc->state != TARGBH_STATE_TEARDOWN) { 711 712 /* 713 * Send the original accept TIO back to the 714 * controller to handle more work. 715 */ 716 CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE, 717 ("Returning ATIO to target\n")); 718 /* Restore wildcards */ 719 atio->ccb_h.target_id = CAM_TARGET_WILDCARD; 720 atio->ccb_h.target_lun = CAM_LUN_WILDCARD; 721 xpt_action((union ccb *)atio); 722 break; 723 } else { 724 targbhfreedescr(desc); 725 free(atio, M_DEVBUF); 726 } 727 break; 728 } 729 case XPT_IMMED_NOTIFY: 730 { 731 int frozen; 732 733 frozen = (done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0; 734 if (softc->state == TARGBH_STATE_TEARDOWN 735 || done_ccb->ccb_h.status == CAM_REQ_ABORTED) { 736 printf("Freed an immediate notify\n"); 737 free(done_ccb, M_DEVBUF); 738 } else { 739 /* Requeue for another immediate event */ 740 xpt_action(done_ccb); 741 } 742 if (frozen != 0) 743 cam_release_devq(periph->path, 744 /*relsim_flags*/0, 745 /*opening reduction*/0, 746 /*timeout*/0, 747 /*getcount_only*/0); 748 break; 749 } 750 default: 751 panic("targbhdone: Unexpected ccb opcode"); 752 break; 753 } 754 } 755 756 #ifdef NOTYET 757 static int 758 targbherror(union ccb *ccb, u_int32_t cam_flags, u_int32_t sense_flags) 759 { 760 return 0; 761 } 762 #endif 763 764 static struct targbh_cmd_desc* 765 targbhallocdescr() 766 { 767 struct targbh_cmd_desc* descr; 768 769 /* Allocate the targbh_descr structure */ 770 descr = (struct targbh_cmd_desc *)malloc(sizeof(*descr), 771 M_DEVBUF, M_NOWAIT); 772 if (descr == NULL) 773 return (NULL); 774 775 bzero(descr, sizeof(*descr)); 776 777 /* Allocate buffer backing store */ 778 descr->backing_store = malloc(MAX_BUF_SIZE, M_DEVBUF, M_NOWAIT); 779 if (descr->backing_store == NULL) { 780 free(descr, M_DEVBUF); 781 return (NULL); 782 } 783 descr->max_size = MAX_BUF_SIZE; 784 return (descr); 785 } 786 787 static void 788 targbhfreedescr(struct targbh_cmd_desc *descr) 789 { 790 free(descr->backing_store, M_DEVBUF); 791 free(descr, M_DEVBUF); 792 } 793