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