2 * Implementation of the Target Mode 'Black Hole device' for CAM.
4 * Copyright (c) 1999 Justin T. Gibbs.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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.
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
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
32 #include <sys/param.h>
33 #include <sys/queue.h>
34 #include <sys/systm.h>
35 #include <sys/kernel.h>
36 #include <sys/types.h>
39 #include <sys/devicestat.h>
40 #include <sys/malloc.h>
44 #include <cam/cam_ccb.h>
45 #include <cam/cam_periph.h>
46 #include <cam/cam_queue.h>
47 #include <cam/cam_xpt_periph.h>
48 #include <cam/cam_debug.h>
50 #include <cam/scsi/scsi_all.h>
51 #include <cam/scsi/scsi_message.h>
55 TARGBH_STATE_EXCEPTION,
60 TARGBH_FLAG_NONE = 0x00,
61 TARGBH_FLAG_LUN_ENABLED = 0x01
70 #define MAX_IMMEDIATE 16
71 #define MAX_BUF_SIZE 256 /* Max inquiry/sense/mode page transfer */
73 /* Offsets into our private CCB area for storing accept information */
74 #define ccb_type ppriv_field0
75 #define ccb_descr ppriv_ptr1
77 /* We stick a pointer to the originating accept TIO in each continue I/O CCB */
78 #define ccb_atio ppriv_ptr1
80 TAILQ_HEAD(ccb_queue, ccb_hdr);
83 struct ccb_queue pending_queue;
84 struct ccb_queue work_queue;
85 struct ccb_queue unknown_atio_queue;
86 struct devstat device_stats;
91 struct ccb_accept_tio *accept_tio_list;
92 struct ccb_hdr_slist immed_notify_slist;
95 struct targbh_cmd_desc {
96 struct ccb_accept_tio* atio_link;
97 u_int data_resid; /* How much left to transfer */
98 u_int data_increment;/* Amount to send before next disconnect */
99 void* data; /* The data. Can be from backing_store or not */
100 void* backing_store;/* Backing store allocated for this descriptor*/
101 u_int max_size; /* Size of backing_store */
103 u_int8_t status; /* Status to return to initiator */
106 static struct scsi_inquiry_data no_lun_inq_data =
108 T_NODEVICE | (SID_QUAL_BAD_LU << 5), 0,
109 /* version */2, /* format version */2
112 static struct scsi_sense_data no_lun_sense_data =
114 SSD_CURRENT_ERROR|SSD_ERRCODE_VALID,
118 /*extra_len*/offsetof(struct scsi_sense_data, fru)
119 - offsetof(struct scsi_sense_data, extra_len),
121 /* Logical Unit Not Supported */
122 /*ASC*/0x25, /*ASCQ*/0
125 static const int request_sense_size = offsetof(struct scsi_sense_data, fru);
127 static periph_init_t targbhinit;
128 static void targbhasync(void *callback_arg, u_int32_t code,
129 struct cam_path *path, void *arg);
130 static cam_status targbhenlun(struct cam_periph *periph);
131 static cam_status targbhdislun(struct cam_periph *periph);
132 static periph_ctor_t targbhctor;
133 static periph_dtor_t targbhdtor;
134 static periph_start_t targbhstart;
135 static void targbhdone(struct cam_periph *periph,
136 union ccb *done_ccb);
138 static int targbherror(union ccb *ccb, u_int32_t cam_flags,
139 u_int32_t sense_flags);
141 static struct targbh_cmd_desc* targbhallocdescr(void);
142 static void targbhfreedescr(struct targbh_cmd_desc *buf);
144 static struct periph_driver targbhdriver =
146 targbhinit, "targbh",
147 TAILQ_HEAD_INITIALIZER(targbhdriver.units), /* generation */ 0
150 PERIPHDRIVER_DECLARE(targbh, targbhdriver);
156 struct cam_path *path;
159 * Install a global async callback. This callback will
160 * receive async callbacks like "new path registered".
162 status = xpt_create_path(&path, /*periph*/NULL, CAM_XPT_PATH_ID,
163 CAM_TARGET_WILDCARD, CAM_LUN_WILDCARD);
165 if (status == CAM_REQ_CMP) {
166 struct ccb_setasync csa;
168 xpt_setup_ccb(&csa.ccb_h, path, /*priority*/5);
169 csa.ccb_h.func_code = XPT_SASYNC_CB;
170 csa.event_enable = AC_PATH_REGISTERED | AC_PATH_DEREGISTERED;
171 csa.callback = targbhasync;
172 csa.callback_arg = NULL;
173 xpt_action((union ccb *)&csa);
174 status = csa.ccb_h.status;
178 if (status != CAM_REQ_CMP) {
179 printf("targbh: Failed to attach master async callback "
180 "due to status 0x%x!\n", status);
185 targbhasync(void *callback_arg, u_int32_t code,
186 struct cam_path *path, void *arg)
188 struct cam_path *new_path;
189 struct ccb_pathinq *cpi;
190 path_id_t bus_path_id;
193 cpi = (struct ccb_pathinq *)arg;
194 if (code == AC_PATH_REGISTERED)
195 bus_path_id = cpi->ccb_h.path_id;
197 bus_path_id = xpt_path_path_id(path);
199 * Allocate a peripheral instance for
200 * this target instance.
202 status = xpt_create_path(&new_path, NULL,
204 CAM_TARGET_WILDCARD, CAM_LUN_WILDCARD);
205 if (status != CAM_REQ_CMP) {
206 printf("targbhasync: Unable to create path "
207 "due to status 0x%x\n", status);
212 case AC_PATH_REGISTERED:
214 /* Only attach to controllers that support target mode */
215 if ((cpi->target_sprt & PIT_PROCESSOR) == 0)
218 status = cam_periph_alloc(targbhctor, NULL, targbhdtor,
220 "targbh", CAM_PERIPH_BIO,
221 new_path, targbhasync,
226 case AC_PATH_DEREGISTERED:
228 struct cam_periph *periph;
230 if ((periph = cam_periph_find(new_path, "targbh")) != NULL)
231 cam_periph_invalidate(periph);
237 xpt_free_path(new_path);
240 /* Attempt to enable our lun */
242 targbhenlun(struct cam_periph *periph)
245 struct targbh_softc *softc;
249 softc = (struct targbh_softc *)periph->softc;
251 if ((softc->flags & TARGBH_FLAG_LUN_ENABLED) != 0)
252 return (CAM_REQ_CMP);
254 xpt_setup_ccb(&immed_ccb.ccb_h, periph->path, /*priority*/1);
255 immed_ccb.ccb_h.func_code = XPT_EN_LUN;
257 /* Don't need support for any vendor specific commands */
258 immed_ccb.cel.grp6_len = 0;
259 immed_ccb.cel.grp7_len = 0;
260 immed_ccb.cel.enable = 1;
261 xpt_action(&immed_ccb);
262 status = immed_ccb.ccb_h.status;
263 if (status != CAM_REQ_CMP) {
264 xpt_print_path(periph->path);
265 printf("targbhenlun - Enable Lun Rejected with status 0x%x\n",
270 softc->flags |= TARGBH_FLAG_LUN_ENABLED;
273 * Build up a buffer of accept target I/O
274 * operations for incoming selections.
276 for (i = 0; i < MAX_ACCEPT; i++) {
277 struct ccb_accept_tio *atio;
279 atio = (struct ccb_accept_tio*)malloc(sizeof(*atio), M_DEVBUF,
282 status = CAM_RESRC_UNAVAIL;
286 atio->ccb_h.ccb_descr = targbhallocdescr();
288 if (atio->ccb_h.ccb_descr == NULL) {
289 free(atio, M_DEVBUF);
290 status = CAM_RESRC_UNAVAIL;
294 xpt_setup_ccb(&atio->ccb_h, periph->path, /*priority*/1);
295 atio->ccb_h.func_code = XPT_ACCEPT_TARGET_IO;
296 atio->ccb_h.cbfcnp = targbhdone;
297 xpt_action((union ccb *)atio);
298 status = atio->ccb_h.status;
299 if (status != CAM_REQ_INPROG) {
300 targbhfreedescr(atio->ccb_h.ccb_descr);
301 free(atio, M_DEVBUF);
304 ((struct targbh_cmd_desc*)atio->ccb_h.ccb_descr)->atio_link =
305 softc->accept_tio_list;
306 softc->accept_tio_list = atio;
310 xpt_print_path(periph->path);
311 printf("targbhenlun - Could not allocate accept tio CCBs: "
312 "status = 0x%x\n", status);
313 targbhdislun(periph);
314 return (CAM_REQ_CMP_ERR);
318 * Build up a buffer of immediate notify CCBs
319 * so the SIM can tell us of asynchronous target mode events.
321 for (i = 0; i < MAX_ACCEPT; i++) {
322 struct ccb_immed_notify *inot;
324 inot = (struct ccb_immed_notify*)malloc(sizeof(*inot), M_DEVBUF,
328 status = CAM_RESRC_UNAVAIL;
332 xpt_setup_ccb(&inot->ccb_h, periph->path, /*priority*/1);
333 inot->ccb_h.func_code = XPT_IMMED_NOTIFY;
334 inot->ccb_h.cbfcnp = targbhdone;
335 xpt_action((union ccb *)inot);
336 status = inot->ccb_h.status;
337 if (status != CAM_REQ_INPROG) {
338 free(inot, M_DEVBUF);
341 SLIST_INSERT_HEAD(&softc->immed_notify_slist, &inot->ccb_h,
346 xpt_print_path(periph->path);
347 printf("targbhenlun - Could not allocate immediate notify "
348 "CCBs: status = 0x%x\n", status);
349 targbhdislun(periph);
350 return (CAM_REQ_CMP_ERR);
353 return (CAM_REQ_CMP);
357 targbhdislun(struct cam_periph *periph)
360 struct targbh_softc *softc;
361 struct ccb_accept_tio* atio;
362 struct ccb_hdr *ccb_h;
364 softc = (struct targbh_softc *)periph->softc;
365 if ((softc->flags & TARGBH_FLAG_LUN_ENABLED) == 0)
368 /* XXX Block for Continue I/O completion */
370 /* Kill off all ACCECPT and IMMEDIATE CCBs */
371 while ((atio = softc->accept_tio_list) != NULL) {
373 softc->accept_tio_list =
374 ((struct targbh_cmd_desc*)atio->ccb_h.ccb_descr)->atio_link;
375 xpt_setup_ccb(&ccb.cab.ccb_h, periph->path, /*priority*/1);
376 ccb.cab.ccb_h.func_code = XPT_ABORT;
377 ccb.cab.abort_ccb = (union ccb *)atio;
381 while ((ccb_h = SLIST_FIRST(&softc->immed_notify_slist)) != NULL) {
382 SLIST_REMOVE_HEAD(&softc->immed_notify_slist, periph_links.sle);
383 xpt_setup_ccb(&ccb.cab.ccb_h, periph->path, /*priority*/1);
384 ccb.cab.ccb_h.func_code = XPT_ABORT;
385 ccb.cab.abort_ccb = (union ccb *)ccb_h;
392 xpt_setup_ccb(&ccb.cel.ccb_h, periph->path, /*priority*/1);
393 ccb.cel.ccb_h.func_code = XPT_EN_LUN;
397 if (ccb.cel.ccb_h.status != CAM_REQ_CMP)
398 printf("targbhdislun - Disabling lun on controller failed "
399 "with status 0x%x\n", ccb.cel.ccb_h.status);
401 softc->flags &= ~TARGBH_FLAG_LUN_ENABLED;
402 return (ccb.cel.ccb_h.status);
406 targbhctor(struct cam_periph *periph, void *arg)
408 struct targbh_softc *softc;
410 /* Allocate our per-instance private storage */
411 softc = (struct targbh_softc *)malloc(sizeof(*softc),
414 printf("targctor: unable to malloc softc\n");
415 return (CAM_REQ_CMP_ERR);
418 bzero(softc, sizeof(*softc));
419 TAILQ_INIT(&softc->pending_queue);
420 TAILQ_INIT(&softc->work_queue);
421 softc->accept_tio_list = NULL;
422 SLIST_INIT(&softc->immed_notify_slist);
423 softc->state = TARGBH_STATE_NORMAL;
424 periph->softc = softc;
427 return (targbhenlun(periph));
431 targbhdtor(struct cam_periph *periph)
433 struct targbh_softc *softc;
435 softc = (struct targbh_softc *)periph->softc;
437 softc->state = TARGBH_STATE_TEARDOWN;
439 targbhdislun(periph);
441 switch (softc->init_level) {
443 panic("targdtor - impossible init level");;
447 /* XXX Wait for callback of targbhdislun() */
448 tsleep(softc, PRIBIO, "targbh", hz/2);
449 free(softc, M_DEVBUF);
455 targbhstart(struct cam_periph *periph, union ccb *start_ccb)
457 struct targbh_softc *softc;
458 struct ccb_hdr *ccbh;
459 struct ccb_accept_tio *atio;
460 struct targbh_cmd_desc *desc;
461 struct ccb_scsiio *csio;
465 softc = (struct targbh_softc *)periph->softc;
468 ccbh = TAILQ_FIRST(&softc->work_queue);
469 if (periph->immediate_priority <= periph->pinfo.priority) {
470 start_ccb->ccb_h.ccb_type = TARGBH_CCB_WAITING;
471 SLIST_INSERT_HEAD(&periph->ccb_list, &start_ccb->ccb_h,
473 periph->immediate_priority = CAM_PRIORITY_NONE;
475 wakeup(&periph->ccb_list);
476 } else if (ccbh == NULL) {
478 xpt_release_ccb(start_ccb);
480 TAILQ_REMOVE(&softc->work_queue, ccbh, periph_links.tqe);
481 TAILQ_INSERT_HEAD(&softc->pending_queue, ccbh,
484 atio = (struct ccb_accept_tio*)ccbh;
485 desc = (struct targbh_cmd_desc *)atio->ccb_h.ccb_descr;
487 /* Is this a tagged request? */
488 flags = atio->ccb_h.flags &
489 (CAM_DIS_DISCONNECT|CAM_TAG_ACTION_VALID|CAM_DIR_MASK);
491 csio = &start_ccb->csio;
493 * If we are done with the transaction, tell the
494 * controller to send status and perform a CMD_CMPLT.
495 * If we have associated sense data, see if we can
498 if (desc->data_resid == desc->data_increment) {
499 flags |= CAM_SEND_STATUS;
500 if (atio->sense_len) {
501 csio->sense_len = atio->sense_len;
502 csio->sense_data = atio->sense_data;
503 flags |= CAM_SEND_SENSE;
512 (flags & CAM_TAG_ACTION_VALID)?
513 MSG_SIMPLE_Q_TAG : 0,
517 /*data_ptr*/desc->data_increment == 0
519 /*dxfer_len*/desc->data_increment,
520 /*timeout*/desc->timeout);
522 /* Override our wildcard attachment */
523 start_ccb->ccb_h.target_id = atio->ccb_h.target_id;
524 start_ccb->ccb_h.target_lun = atio->ccb_h.target_lun;
526 start_ccb->ccb_h.ccb_type = TARGBH_CCB_WORKQ;
527 start_ccb->ccb_h.ccb_atio = atio;
528 CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE,
529 ("Sending a CTIO\n"));
530 xpt_action(start_ccb);
532 * If the queue was frozen waiting for the response
533 * to this ATIO (for instance disconnection was disallowed),
534 * then release it now that our response has been queued.
536 if ((atio->ccb_h.status & CAM_DEV_QFRZN) != 0) {
537 cam_release_devq(periph->path,
542 atio->ccb_h.status &= ~CAM_DEV_QFRZN;
545 ccbh = TAILQ_FIRST(&softc->work_queue);
549 xpt_schedule(periph, /*priority*/1);
553 targbhdone(struct cam_periph *periph, union ccb *done_ccb)
555 struct targbh_softc *softc;
557 softc = (struct targbh_softc *)periph->softc;
559 if (done_ccb->ccb_h.ccb_type == TARGBH_CCB_WAITING) {
560 /* Caller will release the CCB */
561 wakeup(&done_ccb->ccb_h.cbfcnp);
565 switch (done_ccb->ccb_h.func_code) {
566 case XPT_ACCEPT_TARGET_IO:
568 struct ccb_accept_tio *atio;
569 struct targbh_cmd_desc *descr;
573 atio = &done_ccb->atio;
574 descr = (struct targbh_cmd_desc*)atio->ccb_h.ccb_descr;
575 cdb = atio->cdb_io.cdb_bytes;
576 if (softc->state == TARGBH_STATE_TEARDOWN
577 || atio->ccb_h.status == CAM_REQ_ABORTED) {
578 targbhfreedescr(descr);
579 free(done_ccb, M_DEVBUF);
584 * Determine the type of incoming command and
585 * setup our buffer for a response.
590 struct scsi_inquiry *inq;
592 inq = (struct scsi_inquiry *)cdb;
593 CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE,
594 ("Saw an inquiry!\n"));
596 * Validate the command. We don't
597 * support any VPD pages, so complain
600 if ((inq->byte2 & SI_EVPD) != 0
601 || inq->page_code != 0) {
602 atio->ccb_h.flags &= ~CAM_DIR_MASK;
603 atio->ccb_h.flags |= CAM_DIR_NONE;
605 * This needs to have other than a
606 * no_lun_sense_data response.
608 atio->sense_data = no_lun_sense_data;
609 atio->sense_len = sizeof(no_lun_sense_data);
610 descr->data_resid = 0;
611 descr->data_increment = 0;
612 descr->status = SCSI_STATUS_CHECK_COND;
616 * Direction is always relative
619 atio->ccb_h.flags &= ~CAM_DIR_MASK;
620 atio->ccb_h.flags |= CAM_DIR_IN;
621 descr->data = &no_lun_inq_data;
622 descr->data_resid = MIN(sizeof(no_lun_inq_data),
623 SCSI_CDB6_LEN(inq->length));
624 descr->data_increment = descr->data_resid;
625 descr->timeout = 5 * 1000;
626 descr->status = SCSI_STATUS_OK;
631 struct scsi_request_sense *rsense;
633 rsense = (struct scsi_request_sense *)cdb;
634 /* Refer to static sense data */
635 atio->ccb_h.flags &= ~CAM_DIR_MASK;
636 atio->ccb_h.flags |= CAM_DIR_IN;
637 descr->data = &no_lun_sense_data;
638 descr->data_resid = request_sense_size;
639 descr->data_resid = MIN(descr->data_resid,
640 SCSI_CDB6_LEN(rsense->length));
641 descr->data_increment = descr->data_resid;
642 descr->timeout = 5 * 1000;
643 descr->status = SCSI_STATUS_OK;
647 /* Constant CA, tell initiator */
648 /* Direction is always relative to the initator */
649 atio->ccb_h.flags &= ~CAM_DIR_MASK;
650 atio->ccb_h.flags |= CAM_DIR_NONE;
651 atio->sense_data = no_lun_sense_data;
652 atio->sense_len = sizeof (no_lun_sense_data);
653 descr->data_resid = 0;
654 descr->data_increment = 0;
655 descr->timeout = 5 * 1000;
656 descr->status = SCSI_STATUS_CHECK_COND;
660 /* Queue us up to receive a Continue Target I/O ccb. */
661 if ((atio->ccb_h.flags & CAM_DIS_DISCONNECT) != 0) {
662 TAILQ_INSERT_HEAD(&softc->work_queue, &atio->ccb_h,
666 TAILQ_INSERT_TAIL(&softc->work_queue, &atio->ccb_h,
670 xpt_schedule(periph, priority);
673 case XPT_CONT_TARGET_IO:
675 struct ccb_accept_tio *atio;
676 struct targbh_cmd_desc *desc;
678 CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE,
679 ("Received completed CTIO\n"));
680 atio = (struct ccb_accept_tio*)done_ccb->ccb_h.ccb_atio;
681 desc = (struct targbh_cmd_desc *)atio->ccb_h.ccb_descr;
683 TAILQ_REMOVE(&softc->pending_queue, &atio->ccb_h,
687 * We could check for CAM_SENT_SENSE bein set here,
688 * but since we're not maintaining any CA/UA state,
692 done_ccb->ccb_h.flags &= ~CAM_SEND_SENSE;
693 done_ccb->ccb_h.status &= ~CAM_SENT_SENSE;
696 * Any errors will not change the data we return,
697 * so make sure the queue is not left frozen.
698 * XXX - At some point there may be errors that
699 * leave us in a connected state with the
702 if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0) {
703 printf("Releasing Queue\n");
704 cam_release_devq(done_ccb->ccb_h.path,
709 done_ccb->ccb_h.status &= ~CAM_DEV_QFRZN;
711 desc->data_resid -= desc->data_increment;
712 xpt_release_ccb(done_ccb);
713 if (softc->state != TARGBH_STATE_TEARDOWN) {
716 * Send the original accept TIO back to the
717 * controller to handle more work.
719 CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE,
720 ("Returning ATIO to target\n"));
721 /* Restore wildcards */
722 atio->ccb_h.target_id = CAM_TARGET_WILDCARD;
723 atio->ccb_h.target_lun = CAM_LUN_WILDCARD;
724 xpt_action((union ccb *)atio);
727 targbhfreedescr(desc);
728 free(atio, M_DEVBUF);
732 case XPT_IMMED_NOTIFY:
736 frozen = (done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0;
737 if (softc->state == TARGBH_STATE_TEARDOWN
738 || done_ccb->ccb_h.status == CAM_REQ_ABORTED) {
739 printf("Freed an immediate notify\n");
740 free(done_ccb, M_DEVBUF);
742 /* Requeue for another immediate event */
743 xpt_action(done_ccb);
746 cam_release_devq(periph->path,
748 /*opening reduction*/0,
754 panic("targbhdone: Unexpected ccb opcode");
761 targbherror(union ccb *ccb, u_int32_t cam_flags, u_int32_t sense_flags)
767 static struct targbh_cmd_desc*
770 struct targbh_cmd_desc* descr;
772 /* Allocate the targbh_descr structure */
773 descr = (struct targbh_cmd_desc *)malloc(sizeof(*descr),
778 bzero(descr, sizeof(*descr));
780 /* Allocate buffer backing store */
781 descr->backing_store = malloc(MAX_BUF_SIZE, M_DEVBUF, M_NOWAIT);
782 if (descr->backing_store == NULL) {
783 free(descr, M_DEVBUF);
786 descr->max_size = MAX_BUF_SIZE;
791 targbhfreedescr(struct targbh_cmd_desc *descr)
793 free(descr->backing_store, M_DEVBUF);
794 free(descr, M_DEVBUF);