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
28 * $FreeBSD: src/sys/cam/scsi/scsi_targ_bh.c,v 1.4.2.6 2003/11/14 11:31:25 simokawa Exp $
29 * $DragonFly: src/sys/bus/cam/scsi/scsi_targ_bh.c,v 1.18 2007/12/01 22:21:18 pavalos Exp $
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>
38 #include <sys/devicestat.h>
39 #include <sys/malloc.h>
40 #include <sys/thread2.h>
44 #include "../cam_ccb.h"
45 #include "../cam_extend.h"
46 #include "../cam_periph.h"
47 #include "../cam_queue.h"
48 #include "../cam_xpt_periph.h"
49 #include "../cam_debug.h"
52 #include "scsi_message.h"
54 MALLOC_DEFINE(M_SCSIBH, "SCSI bh", "SCSI blackhole buffers");
58 TARGBH_STATE_EXCEPTION,
63 TARGBH_FLAG_NONE = 0x00,
64 TARGBH_FLAG_LUN_ENABLED = 0x01
73 #define MAX_IMMEDIATE 16
74 #define MAX_BUF_SIZE 256 /* Max inquiry/sense/mode page transfer */
76 /* Offsets into our private CCB area for storing accept information */
77 #define ccb_type ppriv_field0
78 #define ccb_descr ppriv_ptr1
80 /* We stick a pointer to the originating accept TIO in each continue I/O CCB */
81 #define ccb_atio ppriv_ptr1
83 TAILQ_HEAD(ccb_queue, ccb_hdr);
86 struct ccb_queue pending_queue;
87 struct ccb_queue work_queue;
88 struct ccb_queue unknown_atio_queue;
89 struct devstat device_stats;
94 struct ccb_accept_tio *accept_tio_list;
95 struct ccb_hdr_slist immed_notify_slist;
98 struct targbh_cmd_desc {
99 struct ccb_accept_tio* atio_link;
100 u_int data_resid; /* How much left to transfer */
101 u_int data_increment;/* Amount to send before next disconnect */
102 void* data; /* The data. Can be from backing_store or not */
103 void* backing_store;/* Backing store allocated for this descriptor*/
104 u_int max_size; /* Size of backing_store */
106 u_int8_t status; /* Status to return to initiator */
109 static struct scsi_inquiry_data no_lun_inq_data =
111 T_NODEVICE | (SID_QUAL_BAD_LU << 5), 0,
112 /* version */2, /* format version */2
115 static struct scsi_sense_data no_lun_sense_data =
117 SSD_CURRENT_ERROR|SSD_ERRCODE_VALID,
121 /*extra_len*/offsetof(struct scsi_sense_data, fru)
122 - offsetof(struct scsi_sense_data, extra_len),
124 /* Logical Unit Not Supported */
125 /*ASC*/0x25, /*ASCQ*/0
128 static const int request_sense_size = offsetof(struct scsi_sense_data, fru);
130 static periph_init_t targbhinit;
131 static void targbhasync(void *callback_arg, u_int32_t code,
132 struct cam_path *path, void *arg);
133 static cam_status targbhenlun(struct cam_periph *periph);
134 static cam_status targbhdislun(struct cam_periph *periph);
135 static periph_ctor_t targbhctor;
136 static periph_dtor_t targbhdtor;
137 static periph_start_t targbhstart;
138 static void targbhdone(struct cam_periph *periph,
139 union ccb *done_ccb);
141 static int targbherror(union ccb *ccb, u_int32_t cam_flags,
142 u_int32_t sense_flags);
144 static struct targbh_cmd_desc* targbhallocdescr(void);
145 static void targbhfreedescr(struct targbh_cmd_desc *buf);
147 static struct periph_driver targbhdriver =
149 targbhinit, "targbh",
150 TAILQ_HEAD_INITIALIZER(targbhdriver.units), /* generation */ 0
153 PERIPHDRIVER_DECLARE(targbh, targbhdriver);
159 struct cam_path *path;
162 * Install a global async callback. This callback will
163 * receive async callbacks like "new path registered".
165 status = xpt_create_path(&path, /*periph*/NULL, CAM_XPT_PATH_ID,
166 CAM_TARGET_WILDCARD, CAM_LUN_WILDCARD);
168 if (status == CAM_REQ_CMP) {
169 struct ccb_setasync csa;
171 xpt_setup_ccb(&csa.ccb_h, path, /*priority*/5);
172 csa.ccb_h.func_code = XPT_SASYNC_CB;
173 csa.event_enable = AC_PATH_REGISTERED | AC_PATH_DEREGISTERED;
174 csa.callback = targbhasync;
175 csa.callback_arg = NULL;
176 xpt_action((union ccb *)&csa);
177 status = csa.ccb_h.status;
181 if (status != CAM_REQ_CMP) {
182 kprintf("targbh: Failed to attach master async callback "
183 "due to status 0x%x!\n", status);
188 targbhasync(void *callback_arg, u_int32_t code,
189 struct cam_path *path, void *arg)
191 struct cam_path *new_path;
192 struct ccb_pathinq *cpi;
193 path_id_t bus_path_id;
196 cpi = (struct ccb_pathinq *)arg;
197 if (code == AC_PATH_REGISTERED)
198 bus_path_id = cpi->ccb_h.path_id;
200 bus_path_id = xpt_path_path_id(path);
202 * Allocate a peripheral instance for
203 * this target instance.
205 status = xpt_create_path(&new_path, NULL,
207 CAM_TARGET_WILDCARD, CAM_LUN_WILDCARD);
208 if (status != CAM_REQ_CMP) {
209 kprintf("targbhasync: Unable to create path "
210 "due to status 0x%x\n", status);
215 case AC_PATH_REGISTERED:
217 /* Only attach to controllers that support target mode */
218 if ((cpi->target_sprt & PIT_PROCESSOR) == 0)
221 status = cam_periph_alloc(targbhctor, NULL, targbhdtor,
223 "targbh", CAM_PERIPH_BIO,
224 new_path, targbhasync,
229 case AC_PATH_DEREGISTERED:
231 struct cam_periph *periph;
233 if ((periph = cam_periph_find(new_path, "targbh")) != NULL)
234 cam_periph_invalidate(periph);
240 xpt_free_path(new_path);
243 /* Attempt to enable our lun */
245 targbhenlun(struct cam_periph *periph)
248 struct targbh_softc *softc;
252 softc = (struct targbh_softc *)periph->softc;
254 if ((softc->flags & TARGBH_FLAG_LUN_ENABLED) != 0)
255 return (CAM_REQ_CMP);
257 xpt_setup_ccb(&immed_ccb.ccb_h, periph->path, /*priority*/1);
258 immed_ccb.ccb_h.func_code = XPT_EN_LUN;
260 /* Don't need support for any vendor specific commands */
261 immed_ccb.cel.grp6_len = 0;
262 immed_ccb.cel.grp7_len = 0;
263 immed_ccb.cel.enable = 1;
264 xpt_action(&immed_ccb);
265 status = immed_ccb.ccb_h.status;
266 if (status != CAM_REQ_CMP) {
267 xpt_print_path(periph->path);
268 kprintf("targbhenlun - Enable Lun Rejected with status 0x%x\n",
273 softc->flags |= TARGBH_FLAG_LUN_ENABLED;
276 * Build up a buffer of accept target I/O
277 * operations for incoming selections.
279 for (i = 0; i < MAX_ACCEPT; i++) {
280 struct ccb_accept_tio *atio;
282 atio = kmalloc(sizeof(*atio), M_SCSIBH, M_INTWAIT);
284 atio->ccb_h.ccb_descr = targbhallocdescr();
286 xpt_setup_ccb(&atio->ccb_h, periph->path, /*priority*/1);
287 atio->ccb_h.func_code = XPT_ACCEPT_TARGET_IO;
288 atio->ccb_h.cbfcnp = targbhdone;
289 xpt_action((union ccb *)atio);
290 status = atio->ccb_h.status;
291 if (status != CAM_REQ_INPROG) {
292 targbhfreedescr(atio->ccb_h.ccb_descr);
293 kfree(atio, M_SCSIBH);
296 ((struct targbh_cmd_desc*)atio->ccb_h.ccb_descr)->atio_link =
297 softc->accept_tio_list;
298 softc->accept_tio_list = atio;
302 xpt_print_path(periph->path);
303 kprintf("targbhenlun - Could not allocate accept tio CCBs: "
304 "status = 0x%x\n", status);
305 targbhdislun(periph);
306 return (CAM_REQ_CMP_ERR);
310 * Build up a buffer of immediate notify CCBs
311 * so the SIM can tell us of asynchronous target mode events.
313 for (i = 0; i < MAX_ACCEPT; i++) {
314 struct ccb_immed_notify *inot;
316 inot = kmalloc(sizeof(*inot), M_SCSIBH, M_INTWAIT);
318 xpt_setup_ccb(&inot->ccb_h, periph->path, /*priority*/1);
319 inot->ccb_h.func_code = XPT_IMMED_NOTIFY;
320 inot->ccb_h.cbfcnp = targbhdone;
321 xpt_action((union ccb *)inot);
322 status = inot->ccb_h.status;
323 if (status != CAM_REQ_INPROG) {
324 kfree(inot, M_SCSIBH);
327 SLIST_INSERT_HEAD(&softc->immed_notify_slist, &inot->ccb_h,
332 xpt_print_path(periph->path);
333 kprintf("targbhenlun - Could not allocate immediate notify "
334 "CCBs: status = 0x%x\n", status);
335 targbhdislun(periph);
336 return (CAM_REQ_CMP_ERR);
339 return (CAM_REQ_CMP);
343 targbhdislun(struct cam_periph *periph)
346 struct targbh_softc *softc;
347 struct ccb_accept_tio* atio;
348 struct ccb_hdr *ccb_h;
350 softc = (struct targbh_softc *)periph->softc;
351 if ((softc->flags & TARGBH_FLAG_LUN_ENABLED) == 0)
354 /* XXX Block for Continue I/O completion */
356 /* Kill off all ACCECPT and IMMEDIATE CCBs */
357 while ((atio = softc->accept_tio_list) != NULL) {
359 softc->accept_tio_list =
360 ((struct targbh_cmd_desc*)atio->ccb_h.ccb_descr)->atio_link;
361 xpt_setup_ccb(&ccb.cab.ccb_h, periph->path, /*priority*/1);
362 ccb.cab.ccb_h.func_code = XPT_ABORT;
363 ccb.cab.abort_ccb = (union ccb *)atio;
367 while ((ccb_h = SLIST_FIRST(&softc->immed_notify_slist)) != NULL) {
368 SLIST_REMOVE_HEAD(&softc->immed_notify_slist, periph_links.sle);
369 xpt_setup_ccb(&ccb.cab.ccb_h, periph->path, /*priority*/1);
370 ccb.cab.ccb_h.func_code = XPT_ABORT;
371 ccb.cab.abort_ccb = (union ccb *)ccb_h;
378 xpt_setup_ccb(&ccb.cel.ccb_h, periph->path, /*priority*/1);
379 ccb.cel.ccb_h.func_code = XPT_EN_LUN;
383 if (ccb.cel.ccb_h.status != CAM_REQ_CMP)
384 kprintf("targbhdislun - Disabling lun on controller failed "
385 "with status 0x%x\n", ccb.cel.ccb_h.status);
387 softc->flags &= ~TARGBH_FLAG_LUN_ENABLED;
388 return (ccb.cel.ccb_h.status);
392 targbhctor(struct cam_periph *periph, void *arg)
394 struct targbh_softc *softc;
396 /* Allocate our per-instance private storage */
397 softc = kmalloc(sizeof(*softc), M_SCSIBH, M_INTWAIT | M_ZERO);
398 TAILQ_INIT(&softc->pending_queue);
399 TAILQ_INIT(&softc->work_queue);
400 softc->accept_tio_list = NULL;
401 SLIST_INIT(&softc->immed_notify_slist);
402 softc->state = TARGBH_STATE_NORMAL;
403 periph->softc = softc;
406 return (targbhenlun(periph));
410 targbhdtor(struct cam_periph *periph)
412 struct targbh_softc *softc;
414 softc = (struct targbh_softc *)periph->softc;
416 softc->state = TARGBH_STATE_TEARDOWN;
418 targbhdislun(periph);
420 switch (softc->init_level) {
422 panic("targdtor - impossible init level");
426 /* XXX Wait for callback of targbhdislun() */
427 tsleep(softc, 0, "targbh", hz/2);
428 kfree(softc, M_SCSIBH);
434 targbhstart(struct cam_periph *periph, union ccb *start_ccb)
436 struct targbh_softc *softc;
437 struct ccb_hdr *ccbh;
438 struct ccb_accept_tio *atio;
439 struct targbh_cmd_desc *desc;
440 struct ccb_scsiio *csio;
443 softc = (struct targbh_softc *)periph->softc;
446 ccbh = TAILQ_FIRST(&softc->work_queue);
447 if (periph->immediate_priority <= periph->pinfo.priority) {
448 start_ccb->ccb_h.ccb_type = TARGBH_CCB_WAITING;
449 SLIST_INSERT_HEAD(&periph->ccb_list, &start_ccb->ccb_h,
451 periph->immediate_priority = CAM_PRIORITY_NONE;
453 wakeup(&periph->ccb_list);
454 } else if (ccbh == NULL) {
456 xpt_release_ccb(start_ccb);
458 TAILQ_REMOVE(&softc->work_queue, ccbh, periph_links.tqe);
459 TAILQ_INSERT_HEAD(&softc->pending_queue, ccbh,
462 atio = (struct ccb_accept_tio*)ccbh;
463 desc = (struct targbh_cmd_desc *)atio->ccb_h.ccb_descr;
465 /* Is this a tagged request? */
466 flags = atio->ccb_h.flags &
467 (CAM_DIS_DISCONNECT|CAM_TAG_ACTION_VALID|CAM_DIR_MASK);
469 csio = &start_ccb->csio;
471 * If we are done with the transaction, tell the
472 * controller to send status and perform a CMD_CMPLT.
473 * If we have associated sense data, see if we can
476 if (desc->data_resid == desc->data_increment) {
477 flags |= CAM_SEND_STATUS;
478 if (atio->sense_len) {
479 csio->sense_len = atio->sense_len;
480 csio->sense_data = atio->sense_data;
481 flags |= CAM_SEND_SENSE;
490 (flags & CAM_TAG_ACTION_VALID)?
491 MSG_SIMPLE_Q_TAG : 0,
495 /*data_ptr*/desc->data_increment == 0
497 /*dxfer_len*/desc->data_increment,
498 /*timeout*/desc->timeout);
500 /* Override our wildcard attachment */
501 start_ccb->ccb_h.target_id = atio->ccb_h.target_id;
502 start_ccb->ccb_h.target_lun = atio->ccb_h.target_lun;
504 start_ccb->ccb_h.ccb_type = TARGBH_CCB_WORKQ;
505 start_ccb->ccb_h.ccb_atio = atio;
506 CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE,
507 ("Sending a CTIO\n"));
508 xpt_action(start_ccb);
510 * If the queue was frozen waiting for the response
511 * to this ATIO (for instance disconnection was disallowed),
512 * then release it now that our response has been queued.
514 if ((atio->ccb_h.status & CAM_DEV_QFRZN) != 0) {
515 cam_release_devq(periph->path,
520 atio->ccb_h.status &= ~CAM_DEV_QFRZN;
523 ccbh = TAILQ_FIRST(&softc->work_queue);
527 xpt_schedule(periph, /*priority*/1);
531 targbhdone(struct cam_periph *periph, union ccb *done_ccb)
533 struct targbh_softc *softc;
535 softc = (struct targbh_softc *)periph->softc;
537 if (done_ccb->ccb_h.ccb_type == TARGBH_CCB_WAITING) {
538 /* Caller will release the CCB */
539 wakeup(&done_ccb->ccb_h.cbfcnp);
543 switch (done_ccb->ccb_h.func_code) {
544 case XPT_ACCEPT_TARGET_IO:
546 struct ccb_accept_tio *atio;
547 struct targbh_cmd_desc *descr;
551 atio = &done_ccb->atio;
552 descr = (struct targbh_cmd_desc*)atio->ccb_h.ccb_descr;
553 cdb = atio->cdb_io.cdb_bytes;
554 if (softc->state == TARGBH_STATE_TEARDOWN
555 || atio->ccb_h.status == CAM_REQ_ABORTED) {
556 targbhfreedescr(descr);
557 xpt_free_ccb(done_ccb);
562 * Determine the type of incoming command and
563 * setup our buffer for a response.
568 struct scsi_inquiry *inq;
570 inq = (struct scsi_inquiry *)cdb;
571 CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE,
572 ("Saw an inquiry!\n"));
574 * Validate the command. We don't
575 * support any VPD pages, so complain
578 if ((inq->byte2 & SI_EVPD) != 0
579 || inq->page_code != 0) {
580 atio->ccb_h.flags &= ~CAM_DIR_MASK;
581 atio->ccb_h.flags |= CAM_DIR_NONE;
583 * This needs to have other than a
584 * no_lun_sense_data response.
586 atio->sense_data = no_lun_sense_data;
587 atio->sense_len = sizeof(no_lun_sense_data);
588 descr->data_resid = 0;
589 descr->data_increment = 0;
590 descr->status = SCSI_STATUS_CHECK_COND;
594 * Direction is always relative
597 atio->ccb_h.flags &= ~CAM_DIR_MASK;
598 atio->ccb_h.flags |= CAM_DIR_IN;
599 descr->data = &no_lun_inq_data;
600 descr->data_resid = MIN(sizeof(no_lun_inq_data),
601 SCSI_CDB6_LEN(inq->length));
602 descr->data_increment = descr->data_resid;
603 descr->timeout = 5 * 1000;
604 descr->status = SCSI_STATUS_OK;
609 struct scsi_request_sense *rsense;
611 rsense = (struct scsi_request_sense *)cdb;
612 /* Refer to static sense data */
613 atio->ccb_h.flags &= ~CAM_DIR_MASK;
614 atio->ccb_h.flags |= CAM_DIR_IN;
615 descr->data = &no_lun_sense_data;
616 descr->data_resid = request_sense_size;
617 descr->data_resid = MIN(descr->data_resid,
618 SCSI_CDB6_LEN(rsense->length));
619 descr->data_increment = descr->data_resid;
620 descr->timeout = 5 * 1000;
621 descr->status = SCSI_STATUS_OK;
625 /* Constant CA, tell initiator */
626 /* Direction is always relative to the initator */
627 atio->ccb_h.flags &= ~CAM_DIR_MASK;
628 atio->ccb_h.flags |= CAM_DIR_NONE;
629 atio->sense_data = no_lun_sense_data;
630 atio->sense_len = sizeof (no_lun_sense_data);
631 descr->data_resid = 0;
632 descr->data_increment = 0;
633 descr->timeout = 5 * 1000;
634 descr->status = SCSI_STATUS_CHECK_COND;
638 /* Queue us up to receive a Continue Target I/O ccb. */
639 if ((atio->ccb_h.flags & CAM_DIS_DISCONNECT) != 0) {
640 TAILQ_INSERT_HEAD(&softc->work_queue, &atio->ccb_h,
644 TAILQ_INSERT_TAIL(&softc->work_queue, &atio->ccb_h,
648 xpt_schedule(periph, priority);
651 case XPT_CONT_TARGET_IO:
653 struct ccb_accept_tio *atio;
654 struct targbh_cmd_desc *desc;
656 CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE,
657 ("Received completed CTIO\n"));
658 atio = (struct ccb_accept_tio*)done_ccb->ccb_h.ccb_atio;
659 desc = (struct targbh_cmd_desc *)atio->ccb_h.ccb_descr;
661 TAILQ_REMOVE(&softc->pending_queue, &atio->ccb_h,
665 * We could check for CAM_SENT_SENSE bein set here,
666 * but since we're not maintaining any CA/UA state,
670 done_ccb->ccb_h.flags &= ~CAM_SEND_SENSE;
671 done_ccb->ccb_h.status &= ~CAM_SENT_SENSE;
674 * Any errors will not change the data we return,
675 * so make sure the queue is not left frozen.
676 * XXX - At some point there may be errors that
677 * leave us in a connected state with the
680 if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0) {
681 kprintf("Releasing Queue\n");
682 cam_release_devq(done_ccb->ccb_h.path,
687 done_ccb->ccb_h.status &= ~CAM_DEV_QFRZN;
689 desc->data_resid -= desc->data_increment;
690 xpt_release_ccb(done_ccb);
691 if (softc->state != TARGBH_STATE_TEARDOWN) {
694 * Send the original accept TIO back to the
695 * controller to handle more work.
697 CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE,
698 ("Returning ATIO to target\n"));
699 /* Restore wildcards */
700 atio->ccb_h.target_id = CAM_TARGET_WILDCARD;
701 atio->ccb_h.target_lun = CAM_LUN_WILDCARD;
702 xpt_action((union ccb *)atio);
705 targbhfreedescr(desc);
706 kfree(atio, M_SCSIBH);
710 case XPT_IMMED_NOTIFY:
714 frozen = (done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0;
715 if (softc->state == TARGBH_STATE_TEARDOWN
716 || done_ccb->ccb_h.status == CAM_REQ_ABORTED) {
717 kprintf("Freed an immediate notify\n");
718 xpt_free_ccb(done_ccb);
720 /* Requeue for another immediate event */
721 xpt_action(done_ccb);
724 cam_release_devq(periph->path,
726 /*opening reduction*/0,
732 panic("targbhdone: Unexpected ccb opcode");
739 targbherror(union ccb *ccb, u_int32_t cam_flags, u_int32_t sense_flags)
745 static struct targbh_cmd_desc *
746 targbhallocdescr(void)
748 struct targbh_cmd_desc* descr;
750 /* Allocate the targbh_descr structure */
751 descr = kmalloc(sizeof(*descr), M_SCSIBH, M_INTWAIT | M_ZERO);
753 /* Allocate buffer backing store */
754 descr->backing_store = kmalloc(MAX_BUF_SIZE, M_SCSIBH, M_INTWAIT);
755 descr->max_size = MAX_BUF_SIZE;
760 targbhfreedescr(struct targbh_cmd_desc *descr)
762 kfree(descr->backing_store, M_SCSIBH);
763 kfree(descr, M_SCSIBH);