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.19 2008/05/18 20:30:20 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"
50 #include "../cam_sim.h"
53 #include "scsi_message.h"
55 MALLOC_DEFINE(M_SCSIBH, "SCSI bh", "SCSI blackhole buffers");
59 TARGBH_STATE_EXCEPTION,
64 TARGBH_FLAG_NONE = 0x00,
65 TARGBH_FLAG_LUN_ENABLED = 0x01
74 #define MAX_IMMEDIATE 16
75 #define MAX_BUF_SIZE 256 /* Max inquiry/sense/mode page transfer */
77 /* Offsets into our private CCB area for storing accept information */
78 #define ccb_type ppriv_field0
79 #define ccb_descr ppriv_ptr1
81 /* We stick a pointer to the originating accept TIO in each continue I/O CCB */
82 #define ccb_atio ppriv_ptr1
84 TAILQ_HEAD(ccb_queue, ccb_hdr);
87 struct ccb_queue pending_queue;
88 struct ccb_queue work_queue;
89 struct ccb_queue unknown_atio_queue;
90 struct devstat device_stats;
95 struct ccb_accept_tio *accept_tio_list;
96 struct ccb_hdr_slist immed_notify_slist;
99 struct targbh_cmd_desc {
100 struct ccb_accept_tio* atio_link;
101 u_int data_resid; /* How much left to transfer */
102 u_int data_increment;/* Amount to send before next disconnect */
103 void* data; /* The data. Can be from backing_store or not */
104 void* backing_store;/* Backing store allocated for this descriptor*/
105 u_int max_size; /* Size of backing_store */
107 u_int8_t status; /* Status to return to initiator */
110 static struct scsi_inquiry_data no_lun_inq_data =
112 T_NODEVICE | (SID_QUAL_BAD_LU << 5), 0,
113 /* version */2, /* format version */2
116 static struct scsi_sense_data no_lun_sense_data =
118 SSD_CURRENT_ERROR|SSD_ERRCODE_VALID,
122 /*extra_len*/offsetof(struct scsi_sense_data, fru)
123 - offsetof(struct scsi_sense_data, extra_len),
125 /* Logical Unit Not Supported */
126 /*ASC*/0x25, /*ASCQ*/0
129 static const int request_sense_size = offsetof(struct scsi_sense_data, fru);
131 static periph_init_t targbhinit;
132 static void targbhasync(void *callback_arg, u_int32_t code,
133 struct cam_path *path, void *arg);
134 static cam_status targbhenlun(struct cam_periph *periph);
135 static cam_status targbhdislun(struct cam_periph *periph);
136 static periph_ctor_t targbhctor;
137 static periph_dtor_t targbhdtor;
138 static periph_start_t targbhstart;
139 static void targbhdone(struct cam_periph *periph,
140 union ccb *done_ccb);
142 static int targbherror(union ccb *ccb, u_int32_t cam_flags,
143 u_int32_t sense_flags);
145 static struct targbh_cmd_desc* targbhallocdescr(void);
146 static void targbhfreedescr(struct targbh_cmd_desc *buf);
148 static struct periph_driver targbhdriver =
150 targbhinit, "targbh",
151 TAILQ_HEAD_INITIALIZER(targbhdriver.units), /* generation */ 0
154 PERIPHDRIVER_DECLARE(targbh, targbhdriver);
162 * Install a global async callback. This callback will
163 * receive async callbacks like "new path registered".
165 status = xpt_register_async(AC_PATH_REGISTERED | AC_PATH_DEREGISTERED,
166 targbhasync, NULL, NULL);
168 if (status != CAM_REQ_CMP) {
169 kprintf("targbh: Failed to attach master async callback "
170 "due to status 0x%x!\n", status);
175 targbhasync(void *callback_arg, u_int32_t code,
176 struct cam_path *path, void *arg)
178 struct cam_path *new_path;
179 struct ccb_pathinq *cpi;
180 path_id_t bus_path_id;
183 cpi = (struct ccb_pathinq *)arg;
184 if (code == AC_PATH_REGISTERED)
185 bus_path_id = cpi->ccb_h.path_id;
187 bus_path_id = xpt_path_path_id(path);
189 * Allocate a peripheral instance for
190 * this target instance.
192 status = xpt_create_path(&new_path, NULL,
194 CAM_TARGET_WILDCARD, CAM_LUN_WILDCARD);
195 if (status != CAM_REQ_CMP) {
196 kprintf("targbhasync: Unable to create path "
197 "due to status 0x%x\n", status);
202 case AC_PATH_REGISTERED:
204 /* Only attach to controllers that support target mode */
205 if ((cpi->target_sprt & PIT_PROCESSOR) == 0)
208 status = cam_periph_alloc(targbhctor, NULL, targbhdtor,
210 "targbh", CAM_PERIPH_BIO,
211 new_path, targbhasync,
216 case AC_PATH_DEREGISTERED:
218 struct cam_periph *periph;
220 if ((periph = cam_periph_find(new_path, "targbh")) != NULL)
221 cam_periph_invalidate(periph);
227 xpt_free_path(new_path);
230 /* Attempt to enable our lun */
232 targbhenlun(struct cam_periph *periph)
235 struct targbh_softc *softc;
239 softc = (struct targbh_softc *)periph->softc;
241 if ((softc->flags & TARGBH_FLAG_LUN_ENABLED) != 0)
242 return (CAM_REQ_CMP);
244 xpt_setup_ccb(&immed_ccb.ccb_h, periph->path, /*priority*/1);
245 immed_ccb.ccb_h.func_code = XPT_EN_LUN;
247 /* Don't need support for any vendor specific commands */
248 immed_ccb.cel.grp6_len = 0;
249 immed_ccb.cel.grp7_len = 0;
250 immed_ccb.cel.enable = 1;
251 xpt_action(&immed_ccb);
252 status = immed_ccb.ccb_h.status;
253 if (status != CAM_REQ_CMP) {
254 xpt_print(periph->path,
255 "targbhenlun - Enable Lun Rejected with status 0x%x\n",
260 softc->flags |= TARGBH_FLAG_LUN_ENABLED;
263 * Build up a buffer of accept target I/O
264 * operations for incoming selections.
266 for (i = 0; i < MAX_ACCEPT; i++) {
267 struct ccb_accept_tio *atio;
269 atio = kmalloc(sizeof(*atio), M_SCSIBH, M_INTWAIT);
271 atio->ccb_h.ccb_descr = targbhallocdescr();
273 xpt_setup_ccb(&atio->ccb_h, periph->path, /*priority*/1);
274 atio->ccb_h.func_code = XPT_ACCEPT_TARGET_IO;
275 atio->ccb_h.cbfcnp = targbhdone;
276 xpt_action((union ccb *)atio);
277 status = atio->ccb_h.status;
278 if (status != CAM_REQ_INPROG) {
279 targbhfreedescr(atio->ccb_h.ccb_descr);
280 kfree(atio, M_SCSIBH);
283 ((struct targbh_cmd_desc*)atio->ccb_h.ccb_descr)->atio_link =
284 softc->accept_tio_list;
285 softc->accept_tio_list = atio;
289 xpt_print(periph->path,
290 "targbhenlun - Could not allocate accept tio CCBs: status "
292 targbhdislun(periph);
293 return (CAM_REQ_CMP_ERR);
297 * Build up a buffer of immediate notify CCBs
298 * so the SIM can tell us of asynchronous target mode events.
300 for (i = 0; i < MAX_ACCEPT; i++) {
301 struct ccb_immed_notify *inot;
303 inot = kmalloc(sizeof(*inot), M_SCSIBH, M_INTWAIT);
305 xpt_setup_ccb(&inot->ccb_h, periph->path, /*priority*/1);
306 inot->ccb_h.func_code = XPT_IMMED_NOTIFY;
307 inot->ccb_h.cbfcnp = targbhdone;
308 xpt_action((union ccb *)inot);
309 status = inot->ccb_h.status;
310 if (status != CAM_REQ_INPROG) {
311 kfree(inot, M_SCSIBH);
314 SLIST_INSERT_HEAD(&softc->immed_notify_slist, &inot->ccb_h,
319 xpt_print(periph->path,
320 "targbhenlun - Could not allocate immediate notify "
321 "CCBs: status = 0x%x\n", status);
322 targbhdislun(periph);
323 return (CAM_REQ_CMP_ERR);
326 return (CAM_REQ_CMP);
330 targbhdislun(struct cam_periph *periph)
333 struct targbh_softc *softc;
334 struct ccb_accept_tio* atio;
335 struct ccb_hdr *ccb_h;
337 softc = (struct targbh_softc *)periph->softc;
338 if ((softc->flags & TARGBH_FLAG_LUN_ENABLED) == 0)
341 /* XXX Block for Continue I/O completion */
343 /* Kill off all ACCECPT and IMMEDIATE CCBs */
344 while ((atio = softc->accept_tio_list) != NULL) {
346 softc->accept_tio_list =
347 ((struct targbh_cmd_desc*)atio->ccb_h.ccb_descr)->atio_link;
348 xpt_setup_ccb(&ccb.cab.ccb_h, periph->path, /*priority*/1);
349 ccb.cab.ccb_h.func_code = XPT_ABORT;
350 ccb.cab.abort_ccb = (union ccb *)atio;
354 while ((ccb_h = SLIST_FIRST(&softc->immed_notify_slist)) != NULL) {
355 SLIST_REMOVE_HEAD(&softc->immed_notify_slist, periph_links.sle);
356 xpt_setup_ccb(&ccb.cab.ccb_h, periph->path, /*priority*/1);
357 ccb.cab.ccb_h.func_code = XPT_ABORT;
358 ccb.cab.abort_ccb = (union ccb *)ccb_h;
365 xpt_setup_ccb(&ccb.cel.ccb_h, periph->path, /*priority*/1);
366 ccb.cel.ccb_h.func_code = XPT_EN_LUN;
370 if (ccb.cel.ccb_h.status != CAM_REQ_CMP)
371 kprintf("targbhdislun - Disabling lun on controller failed "
372 "with status 0x%x\n", ccb.cel.ccb_h.status);
374 softc->flags &= ~TARGBH_FLAG_LUN_ENABLED;
375 return (ccb.cel.ccb_h.status);
379 targbhctor(struct cam_periph *periph, void *arg)
381 struct targbh_softc *softc;
383 /* Allocate our per-instance private storage */
384 softc = kmalloc(sizeof(*softc), M_SCSIBH, M_INTWAIT | M_ZERO);
385 TAILQ_INIT(&softc->pending_queue);
386 TAILQ_INIT(&softc->work_queue);
387 softc->accept_tio_list = NULL;
388 SLIST_INIT(&softc->immed_notify_slist);
389 softc->state = TARGBH_STATE_NORMAL;
390 periph->softc = softc;
393 return (targbhenlun(periph));
397 targbhdtor(struct cam_periph *periph)
399 struct targbh_softc *softc;
401 softc = (struct targbh_softc *)periph->softc;
403 softc->state = TARGBH_STATE_TEARDOWN;
405 targbhdislun(periph);
407 switch (softc->init_level) {
409 panic("targdtor - impossible init level");
413 /* XXX Wait for callback of targbhdislun() */
414 sim_lock_sleep(softc, 0, "targbh", hz/2, periph->sim->lock);
415 kfree(softc, M_SCSIBH);
421 targbhstart(struct cam_periph *periph, union ccb *start_ccb)
423 struct targbh_softc *softc;
424 struct ccb_hdr *ccbh;
425 struct ccb_accept_tio *atio;
426 struct targbh_cmd_desc *desc;
427 struct ccb_scsiio *csio;
430 softc = (struct targbh_softc *)periph->softc;
432 ccbh = TAILQ_FIRST(&softc->work_queue);
433 if (periph->immediate_priority <= periph->pinfo.priority) {
434 start_ccb->ccb_h.ccb_type = TARGBH_CCB_WAITING;
435 SLIST_INSERT_HEAD(&periph->ccb_list, &start_ccb->ccb_h,
437 periph->immediate_priority = CAM_PRIORITY_NONE;
438 wakeup(&periph->ccb_list);
439 } else if (ccbh == NULL) {
440 xpt_release_ccb(start_ccb);
442 TAILQ_REMOVE(&softc->work_queue, ccbh, periph_links.tqe);
443 TAILQ_INSERT_HEAD(&softc->pending_queue, ccbh,
445 atio = (struct ccb_accept_tio*)ccbh;
446 desc = (struct targbh_cmd_desc *)atio->ccb_h.ccb_descr;
448 /* Is this a tagged request? */
449 flags = atio->ccb_h.flags &
450 (CAM_DIS_DISCONNECT|CAM_TAG_ACTION_VALID|CAM_DIR_MASK);
452 csio = &start_ccb->csio;
454 * If we are done with the transaction, tell the
455 * controller to send status and perform a CMD_CMPLT.
456 * If we have associated sense data, see if we can
459 if (desc->data_resid == desc->data_increment) {
460 flags |= CAM_SEND_STATUS;
461 if (atio->sense_len) {
462 csio->sense_len = atio->sense_len;
463 csio->sense_data = atio->sense_data;
464 flags |= CAM_SEND_SENSE;
473 (flags & CAM_TAG_ACTION_VALID)?
474 MSG_SIMPLE_Q_TAG : 0,
478 /*data_ptr*/desc->data_increment == 0
480 /*dxfer_len*/desc->data_increment,
481 /*timeout*/desc->timeout);
483 /* Override our wildcard attachment */
484 start_ccb->ccb_h.target_id = atio->ccb_h.target_id;
485 start_ccb->ccb_h.target_lun = atio->ccb_h.target_lun;
487 start_ccb->ccb_h.ccb_type = TARGBH_CCB_WORKQ;
488 start_ccb->ccb_h.ccb_atio = atio;
489 CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE,
490 ("Sending a CTIO\n"));
491 xpt_action(start_ccb);
493 * If the queue was frozen waiting for the response
494 * to this ATIO (for instance disconnection was disallowed),
495 * then release it now that our response has been queued.
497 if ((atio->ccb_h.status & CAM_DEV_QFRZN) != 0) {
498 cam_release_devq(periph->path,
503 atio->ccb_h.status &= ~CAM_DEV_QFRZN;
505 ccbh = TAILQ_FIRST(&softc->work_queue);
508 xpt_schedule(periph, /*priority*/1);
512 targbhdone(struct cam_periph *periph, union ccb *done_ccb)
514 struct targbh_softc *softc;
516 softc = (struct targbh_softc *)periph->softc;
518 if (done_ccb->ccb_h.ccb_type == TARGBH_CCB_WAITING) {
519 /* Caller will release the CCB */
520 wakeup(&done_ccb->ccb_h.cbfcnp);
524 switch (done_ccb->ccb_h.func_code) {
525 case XPT_ACCEPT_TARGET_IO:
527 struct ccb_accept_tio *atio;
528 struct targbh_cmd_desc *descr;
532 atio = &done_ccb->atio;
533 descr = (struct targbh_cmd_desc*)atio->ccb_h.ccb_descr;
534 cdb = atio->cdb_io.cdb_bytes;
535 if (softc->state == TARGBH_STATE_TEARDOWN
536 || atio->ccb_h.status == CAM_REQ_ABORTED) {
537 targbhfreedescr(descr);
538 xpt_free_ccb(done_ccb);
543 * Determine the type of incoming command and
544 * setup our buffer for a response.
549 struct scsi_inquiry *inq;
551 inq = (struct scsi_inquiry *)cdb;
552 CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE,
553 ("Saw an inquiry!\n"));
555 * Validate the command. We don't
556 * support any VPD pages, so complain
559 if ((inq->byte2 & SI_EVPD) != 0
560 || inq->page_code != 0) {
561 atio->ccb_h.flags &= ~CAM_DIR_MASK;
562 atio->ccb_h.flags |= CAM_DIR_NONE;
564 * This needs to have other than a
565 * no_lun_sense_data response.
567 atio->sense_data = no_lun_sense_data;
568 atio->sense_len = sizeof(no_lun_sense_data);
569 descr->data_resid = 0;
570 descr->data_increment = 0;
571 descr->status = SCSI_STATUS_CHECK_COND;
575 * Direction is always relative
578 atio->ccb_h.flags &= ~CAM_DIR_MASK;
579 atio->ccb_h.flags |= CAM_DIR_IN;
580 descr->data = &no_lun_inq_data;
581 descr->data_resid = MIN(sizeof(no_lun_inq_data),
582 SCSI_CDB6_LEN(inq->length));
583 descr->data_increment = descr->data_resid;
584 descr->timeout = 5 * 1000;
585 descr->status = SCSI_STATUS_OK;
590 struct scsi_request_sense *rsense;
592 rsense = (struct scsi_request_sense *)cdb;
593 /* Refer to static sense data */
594 atio->ccb_h.flags &= ~CAM_DIR_MASK;
595 atio->ccb_h.flags |= CAM_DIR_IN;
596 descr->data = &no_lun_sense_data;
597 descr->data_resid = request_sense_size;
598 descr->data_resid = MIN(descr->data_resid,
599 SCSI_CDB6_LEN(rsense->length));
600 descr->data_increment = descr->data_resid;
601 descr->timeout = 5 * 1000;
602 descr->status = SCSI_STATUS_OK;
606 /* Constant CA, tell initiator */
607 /* Direction is always relative to the initator */
608 atio->ccb_h.flags &= ~CAM_DIR_MASK;
609 atio->ccb_h.flags |= CAM_DIR_NONE;
610 atio->sense_data = no_lun_sense_data;
611 atio->sense_len = sizeof (no_lun_sense_data);
612 descr->data_resid = 0;
613 descr->data_increment = 0;
614 descr->timeout = 5 * 1000;
615 descr->status = SCSI_STATUS_CHECK_COND;
619 /* Queue us up to receive a Continue Target I/O ccb. */
620 if ((atio->ccb_h.flags & CAM_DIS_DISCONNECT) != 0) {
621 TAILQ_INSERT_HEAD(&softc->work_queue, &atio->ccb_h,
625 TAILQ_INSERT_TAIL(&softc->work_queue, &atio->ccb_h,
629 xpt_schedule(periph, priority);
632 case XPT_CONT_TARGET_IO:
634 struct ccb_accept_tio *atio;
635 struct targbh_cmd_desc *desc;
637 CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE,
638 ("Received completed CTIO\n"));
639 atio = (struct ccb_accept_tio*)done_ccb->ccb_h.ccb_atio;
640 desc = (struct targbh_cmd_desc *)atio->ccb_h.ccb_descr;
642 TAILQ_REMOVE(&softc->pending_queue, &atio->ccb_h,
646 * We could check for CAM_SENT_SENSE bein set here,
647 * but since we're not maintaining any CA/UA state,
651 done_ccb->ccb_h.flags &= ~CAM_SEND_SENSE;
652 done_ccb->ccb_h.status &= ~CAM_SENT_SENSE;
655 * Any errors will not change the data we return,
656 * so make sure the queue is not left frozen.
657 * XXX - At some point there may be errors that
658 * leave us in a connected state with the
661 if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0) {
662 kprintf("Releasing Queue\n");
663 cam_release_devq(done_ccb->ccb_h.path,
668 done_ccb->ccb_h.status &= ~CAM_DEV_QFRZN;
670 desc->data_resid -= desc->data_increment;
671 xpt_release_ccb(done_ccb);
672 if (softc->state != TARGBH_STATE_TEARDOWN) {
675 * Send the original accept TIO back to the
676 * controller to handle more work.
678 CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE,
679 ("Returning ATIO to target\n"));
680 /* Restore wildcards */
681 atio->ccb_h.target_id = CAM_TARGET_WILDCARD;
682 atio->ccb_h.target_lun = CAM_LUN_WILDCARD;
683 xpt_action((union ccb *)atio);
686 targbhfreedescr(desc);
687 kfree(atio, M_SCSIBH);
691 case XPT_IMMED_NOTIFY:
695 frozen = (done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0;
696 if (softc->state == TARGBH_STATE_TEARDOWN
697 || done_ccb->ccb_h.status == CAM_REQ_ABORTED) {
698 kprintf("Freed an immediate notify\n");
699 xpt_free_ccb(done_ccb);
701 /* Requeue for another immediate event */
702 xpt_action(done_ccb);
705 cam_release_devq(periph->path,
707 /*opening reduction*/0,
713 panic("targbhdone: Unexpected ccb opcode");
720 targbherror(union ccb *ccb, u_int32_t cam_flags, u_int32_t sense_flags)
726 static struct targbh_cmd_desc *
727 targbhallocdescr(void)
729 struct targbh_cmd_desc* descr;
731 /* Allocate the targbh_descr structure */
732 descr = kmalloc(sizeof(*descr), M_SCSIBH, M_INTWAIT | M_ZERO);
734 /* Allocate buffer backing store */
735 descr->backing_store = kmalloc(MAX_BUF_SIZE, M_SCSIBH, M_INTWAIT);
736 descr->max_size = MAX_BUF_SIZE;
741 targbhfreedescr(struct targbh_cmd_desc *descr)
743 kfree(descr->backing_store, M_SCSIBH);
744 kfree(descr, M_SCSIBH);