scsi_targ_bh.c revision 120426
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
29#include <sys/cdefs.h>
30__FBSDID("$FreeBSD: head/sys/cam/scsi/scsi_targ_bh.c 120426 2003-09-25 05:02:19Z simokawa $");
31
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>
37#include <sys/bio.h>
38#include <sys/conf.h>
39#include <sys/devicestat.h>
40#include <sys/malloc.h>
41#include <sys/uio.h>
42
43#include <cam/cam.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>
49
50#include <cam/scsi/scsi_all.h>
51#include <cam/scsi/scsi_message.h>
52
53typedef enum {
54	TARGBH_STATE_NORMAL,
55	TARGBH_STATE_EXCEPTION,
56	TARGBH_STATE_TEARDOWN
57} targbh_state;
58
59typedef enum {
60	TARGBH_FLAG_NONE	 = 0x00,
61	TARGBH_FLAG_LUN_ENABLED	 = 0x01
62} targbh_flags;
63
64typedef enum {
65	TARGBH_CCB_WORKQ,
66	TARGBH_CCB_WAITING
67} targbh_ccb_types;
68
69#define MAX_ACCEPT	8
70#define MAX_IMMEDIATE	16
71#define MAX_BUF_SIZE	256	/* Max inquiry/sense/mode page transfer */
72
73/* Offsets into our private CCB area for storing accept information */
74#define ccb_type	ppriv_field0
75#define ccb_descr	ppriv_ptr1
76
77/* We stick a pointer to the originating accept TIO in each continue I/O CCB */
78#define ccb_atio	ppriv_ptr1
79
80TAILQ_HEAD(ccb_queue, ccb_hdr);
81
82struct targbh_softc {
83	struct		ccb_queue pending_queue;
84	struct		ccb_queue work_queue;
85	struct		ccb_queue unknown_atio_queue;
86	struct		devstat device_stats;
87	targbh_state	state;
88	targbh_flags	flags;
89	u_int		init_level;
90	u_int		inq_data_len;
91	struct		ccb_accept_tio *accept_tio_list;
92	struct		ccb_hdr_slist immed_notify_slist;
93};
94
95struct 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 */
102	u_int32_t timeout;
103	u_int8_t  status;	/* Status to return to initiator */
104};
105
106static struct scsi_inquiry_data no_lun_inq_data =
107{
108	T_NODEVICE | (SID_QUAL_BAD_LU << 5), 0,
109	/* version */2, /* format version */2
110};
111
112static struct scsi_sense_data no_lun_sense_data =
113{
114	SSD_CURRENT_ERROR|SSD_ERRCODE_VALID,
115	0,
116	SSD_KEY_NOT_READY,
117	{ 0, 0, 0, 0 },
118	/*extra_len*/offsetof(struct scsi_sense_data, fru)
119                   - offsetof(struct scsi_sense_data, extra_len),
120	{ 0, 0, 0, 0 },
121	/* Logical Unit Not Supported */
122	/*ASC*/0x25, /*ASCQ*/0
123};
124
125static const int request_sense_size = offsetof(struct scsi_sense_data, fru);
126
127static periph_init_t	targbhinit;
128static void		targbhasync(void *callback_arg, u_int32_t code,
129				    struct cam_path *path, void *arg);
130static cam_status	targbhenlun(struct cam_periph *periph);
131static cam_status	targbhdislun(struct cam_periph *periph);
132static periph_ctor_t	targbhctor;
133static periph_dtor_t	targbhdtor;
134static periph_start_t	targbhstart;
135static void		targbhdone(struct cam_periph *periph,
136				   union ccb *done_ccb);
137#ifdef NOTYET
138static  int		targbherror(union ccb *ccb, u_int32_t cam_flags,
139				    u_int32_t sense_flags);
140#endif
141static struct targbh_cmd_desc*	targbhallocdescr(void);
142static void		targbhfreedescr(struct targbh_cmd_desc *buf);
143
144static struct periph_driver targbhdriver =
145{
146	targbhinit, "targbh",
147	TAILQ_HEAD_INITIALIZER(targbhdriver.units), /* generation */ 0
148};
149
150PERIPHDRIVER_DECLARE(targbh, targbhdriver);
151
152static void
153targbhinit(void)
154{
155	cam_status status;
156	struct cam_path *path;
157
158	/*
159	 * Install a global async callback.  This callback will
160	 * receive async callbacks like "new path registered".
161	 */
162	status = xpt_create_path(&path, /*periph*/NULL, CAM_XPT_PATH_ID,
163				 CAM_TARGET_WILDCARD, CAM_LUN_WILDCARD);
164
165	if (status == CAM_REQ_CMP) {
166		struct ccb_setasync csa;
167
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;
175		xpt_free_path(path);
176        }
177
178	if (status != CAM_REQ_CMP) {
179		printf("targbh: Failed to attach master async callback "
180		       "due to status 0x%x!\n", status);
181	}
182}
183
184static void
185targbhasync(void *callback_arg, u_int32_t code,
186	    struct cam_path *path, void *arg)
187{
188	struct cam_path *new_path;
189	cam_status status;
190
191	/*
192	 * Allocate a peripheral instance for
193	 * this target instance.
194	 */
195	status = xpt_create_path(&new_path, NULL,
196				 xpt_path_path_id(path),
197				 CAM_TARGET_WILDCARD, CAM_LUN_WILDCARD);
198	if (status != CAM_REQ_CMP) {
199		printf("targbhasync: Unable to create path "
200			"due to status 0x%x\n", status);
201		return;
202	}
203
204	switch (code) {
205	case AC_PATH_REGISTERED:
206	{
207		struct ccb_pathinq *cpi;
208
209		cpi = (struct ccb_pathinq *)arg;
210
211		/* Only attach to controllers that support target mode */
212		if ((cpi->target_sprt & PIT_PROCESSOR) == 0)
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		break;
222	}
223	case AC_PATH_DEREGISTERED:
224	{
225		cam_periph_invalidate(cam_periph_find(new_path, "targbh"));
226		break;
227	}
228	default:
229		break;
230	}
231	xpt_free_path(new_path);
232}
233
234/* Attempt to enable our lun */
235static cam_status
236targbhenlun(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
350static cam_status
351targbhdislun(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
399static cam_status
400targbhctor(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
424static void
425targbhdtor(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		/* XXX Wait for callback of targbhdislun() */
442		tsleep(softc, PRIBIO, "targbh", hz/2);
443		free(softc, M_DEVBUF);
444		break;
445	}
446}
447
448static void
449targbhstart(struct cam_periph *periph, union ccb *start_ccb)
450{
451	struct targbh_softc *softc;
452	struct ccb_hdr *ccbh;
453	struct ccb_accept_tio *atio;
454	struct targbh_cmd_desc *desc;
455	struct ccb_scsiio *csio;
456	ccb_flags flags;
457	int    s;
458
459	softc = (struct targbh_softc *)periph->softc;
460
461	s = splbio();
462	ccbh = TAILQ_FIRST(&softc->work_queue);
463	if (periph->immediate_priority <= periph->pinfo.priority) {
464		start_ccb->ccb_h.ccb_type = TARGBH_CCB_WAITING;
465		SLIST_INSERT_HEAD(&periph->ccb_list, &start_ccb->ccb_h,
466				  periph_links.sle);
467		periph->immediate_priority = CAM_PRIORITY_NONE;
468		splx(s);
469		wakeup(&periph->ccb_list);
470	} else if (ccbh == NULL) {
471		splx(s);
472		xpt_release_ccb(start_ccb);
473	} else {
474		TAILQ_REMOVE(&softc->work_queue, ccbh, periph_links.tqe);
475		TAILQ_INSERT_HEAD(&softc->pending_queue, ccbh,
476				  periph_links.tqe);
477		splx(s);
478		atio = (struct ccb_accept_tio*)ccbh;
479		desc = (struct targbh_cmd_desc *)atio->ccb_h.ccb_descr;
480
481		/* Is this a tagged request? */
482		flags = atio->ccb_h.flags &
483		    (CAM_DIS_DISCONNECT|CAM_TAG_ACTION_VALID|CAM_DIR_MASK);
484
485		csio = &start_ccb->csio;
486		/*
487		 * If we are done with the transaction, tell the
488		 * controller to send status and perform a CMD_CMPLT.
489		 * If we have associated sense data, see if we can
490		 * send that too.
491		 */
492		if (desc->data_resid == desc->data_increment) {
493			flags |= CAM_SEND_STATUS;
494			if (atio->sense_len) {
495				csio->sense_len = atio->sense_len;
496				csio->sense_data = atio->sense_data;
497				flags |= CAM_SEND_SENSE;
498			}
499
500		}
501
502		cam_fill_ctio(csio,
503			      /*retries*/2,
504			      targbhdone,
505			      flags,
506			      (flags & CAM_TAG_ACTION_VALID)?
507				MSG_SIMPLE_Q_TAG : 0,
508			      atio->tag_id,
509			      atio->init_id,
510			      desc->status,
511			      /*data_ptr*/desc->data_increment == 0
512					  ? NULL : desc->data,
513			      /*dxfer_len*/desc->data_increment,
514			      /*timeout*/desc->timeout);
515
516		/* Override our wildcard attachment */
517		start_ccb->ccb_h.target_id = atio->ccb_h.target_id;
518		start_ccb->ccb_h.target_lun = atio->ccb_h.target_lun;
519
520		start_ccb->ccb_h.ccb_type = TARGBH_CCB_WORKQ;
521		start_ccb->ccb_h.ccb_atio = atio;
522		CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE,
523			  ("Sending a CTIO\n"));
524		xpt_action(start_ccb);
525		/*
526		 * If the queue was frozen waiting for the response
527		 * to this ATIO (for instance disconnection was disallowed),
528		 * then release it now that our response has been queued.
529		 */
530		if ((atio->ccb_h.status & CAM_DEV_QFRZN) != 0) {
531			cam_release_devq(periph->path,
532					 /*relsim_flags*/0,
533					 /*reduction*/0,
534					 /*timeout*/0,
535					 /*getcount_only*/0);
536			atio->ccb_h.status &= ~CAM_DEV_QFRZN;
537		}
538		s = splbio();
539		ccbh = TAILQ_FIRST(&softc->work_queue);
540		splx(s);
541	}
542	if (ccbh != NULL)
543		xpt_schedule(periph, /*priority*/1);
544}
545
546static void
547targbhdone(struct cam_periph *periph, union ccb *done_ccb)
548{
549	struct targbh_softc *softc;
550
551	softc = (struct targbh_softc *)periph->softc;
552
553	if (done_ccb->ccb_h.ccb_type == TARGBH_CCB_WAITING) {
554		/* Caller will release the CCB */
555		wakeup(&done_ccb->ccb_h.cbfcnp);
556		return;
557	}
558
559	switch (done_ccb->ccb_h.func_code) {
560	case XPT_ACCEPT_TARGET_IO:
561	{
562		struct ccb_accept_tio *atio;
563		struct targbh_cmd_desc *descr;
564		u_int8_t *cdb;
565		int priority;
566
567		atio = &done_ccb->atio;
568		descr = (struct targbh_cmd_desc*)atio->ccb_h.ccb_descr;
569		cdb = atio->cdb_io.cdb_bytes;
570		if (softc->state == TARGBH_STATE_TEARDOWN
571		 || atio->ccb_h.status == CAM_REQ_ABORTED) {
572			targbhfreedescr(descr);
573			free(done_ccb, M_DEVBUF);
574			return;
575		}
576
577		/*
578		 * Determine the type of incoming command and
579		 * setup our buffer for a response.
580		 */
581		switch (cdb[0]) {
582		case INQUIRY:
583		{
584			struct scsi_inquiry *inq;
585
586			inq = (struct scsi_inquiry *)cdb;
587			CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE,
588				  ("Saw an inquiry!\n"));
589			/*
590			 * Validate the command.  We don't
591			 * support any VPD pages, so complain
592			 * if EVPD is set.
593			 */
594			if ((inq->byte2 & SI_EVPD) != 0
595			 || inq->page_code != 0) {
596				atio->ccb_h.flags &= ~CAM_DIR_MASK;
597				atio->ccb_h.flags |= CAM_DIR_NONE;
598				/*
599				 * This needs to have other than a
600				 * no_lun_sense_data response.
601				 */
602				atio->sense_data = no_lun_sense_data;
603				atio->sense_len = sizeof(no_lun_sense_data);
604				descr->data_resid = 0;
605				descr->data_increment = 0;
606				descr->status = SCSI_STATUS_CHECK_COND;
607				break;
608			}
609			/*
610			 * Direction is always relative
611			 * to the initator.
612			 */
613			atio->ccb_h.flags &= ~CAM_DIR_MASK;
614			atio->ccb_h.flags |= CAM_DIR_IN;
615			descr->data = &no_lun_inq_data;
616			descr->data_resid = MIN(sizeof(no_lun_inq_data),
617						SCSI_CDB6_LEN(inq->length));
618			descr->data_increment = descr->data_resid;
619			descr->timeout = 5 * 1000;
620			descr->status = SCSI_STATUS_OK;
621			break;
622		}
623		case REQUEST_SENSE:
624		{
625			struct scsi_request_sense *rsense;
626
627			rsense = (struct scsi_request_sense *)cdb;
628			/* Refer to static sense data */
629			atio->ccb_h.flags &= ~CAM_DIR_MASK;
630			atio->ccb_h.flags |= CAM_DIR_IN;
631			descr->data = &no_lun_sense_data;
632			descr->data_resid = request_sense_size;
633			descr->data_resid = MIN(descr->data_resid,
634						SCSI_CDB6_LEN(rsense->length));
635			descr->data_increment = descr->data_resid;
636			descr->timeout = 5 * 1000;
637			descr->status = SCSI_STATUS_OK;
638			break;
639		}
640		default:
641			/* Constant CA, tell initiator */
642			/* Direction is always relative to the initator */
643			atio->ccb_h.flags &= ~CAM_DIR_MASK;
644			atio->ccb_h.flags |= CAM_DIR_NONE;
645			atio->sense_data = no_lun_sense_data;
646			atio->sense_len = sizeof (no_lun_sense_data);
647			descr->data_resid = 0;
648			descr->data_increment = 0;
649			descr->timeout = 5 * 1000;
650			descr->status = SCSI_STATUS_CHECK_COND;
651			break;
652		}
653
654		/* Queue us up to receive a Continue Target I/O ccb. */
655		if ((atio->ccb_h.flags & CAM_DIS_DISCONNECT) != 0) {
656			TAILQ_INSERT_HEAD(&softc->work_queue, &atio->ccb_h,
657					  periph_links.tqe);
658			priority = 0;
659		} else {
660			TAILQ_INSERT_TAIL(&softc->work_queue, &atio->ccb_h,
661					  periph_links.tqe);
662			priority = 1;
663		}
664		xpt_schedule(periph, priority);
665		break;
666	}
667	case XPT_CONT_TARGET_IO:
668	{
669		struct ccb_accept_tio *atio;
670		struct targbh_cmd_desc *desc;
671
672		CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE,
673			  ("Received completed CTIO\n"));
674		atio = (struct ccb_accept_tio*)done_ccb->ccb_h.ccb_atio;
675		desc = (struct targbh_cmd_desc *)atio->ccb_h.ccb_descr;
676
677		TAILQ_REMOVE(&softc->pending_queue, &atio->ccb_h,
678			     periph_links.tqe);
679
680		/*
681		 * We could check for CAM_SENT_SENSE bein set here,
682		 * but since we're not maintaining any CA/UA state,
683		 * there's no point.
684		 */
685		atio->sense_len = 0;
686		done_ccb->ccb_h.flags &= ~CAM_SEND_SENSE;
687		done_ccb->ccb_h.status &= ~CAM_SENT_SENSE;
688
689		/*
690		 * Any errors will not change the data we return,
691		 * so make sure the queue is not left frozen.
692		 * XXX - At some point there may be errors that
693		 *       leave us in a connected state with the
694		 *       initiator...
695		 */
696		if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0) {
697			printf("Releasing Queue\n");
698			cam_release_devq(done_ccb->ccb_h.path,
699					 /*relsim_flags*/0,
700					 /*reduction*/0,
701					 /*timeout*/0,
702					 /*getcount_only*/0);
703			done_ccb->ccb_h.status &= ~CAM_DEV_QFRZN;
704		}
705		desc->data_resid -= desc->data_increment;
706		xpt_release_ccb(done_ccb);
707		if (softc->state != TARGBH_STATE_TEARDOWN) {
708
709			/*
710			 * Send the original accept TIO back to the
711			 * controller to handle more work.
712			 */
713			CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE,
714				  ("Returning ATIO to target\n"));
715			/* Restore wildcards */
716			atio->ccb_h.target_id = CAM_TARGET_WILDCARD;
717			atio->ccb_h.target_lun = CAM_LUN_WILDCARD;
718			xpt_action((union ccb *)atio);
719			break;
720		} else {
721			targbhfreedescr(desc);
722			free(atio, M_DEVBUF);
723		}
724		break;
725	}
726	case XPT_IMMED_NOTIFY:
727	{
728		int frozen;
729
730		frozen = (done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0;
731		if (softc->state == TARGBH_STATE_TEARDOWN
732		 || done_ccb->ccb_h.status == CAM_REQ_ABORTED) {
733			printf("Freed an immediate notify\n");
734			free(done_ccb, M_DEVBUF);
735		} else {
736			/* Requeue for another immediate event */
737			xpt_action(done_ccb);
738		}
739		if (frozen != 0)
740			cam_release_devq(periph->path,
741					 /*relsim_flags*/0,
742					 /*opening reduction*/0,
743					 /*timeout*/0,
744					 /*getcount_only*/0);
745		break;
746	}
747	default:
748		panic("targbhdone: Unexpected ccb opcode");
749		break;
750	}
751}
752
753#ifdef NOTYET
754static int
755targbherror(union ccb *ccb, u_int32_t cam_flags, u_int32_t sense_flags)
756{
757	return 0;
758}
759#endif
760
761static struct targbh_cmd_desc*
762targbhallocdescr()
763{
764	struct targbh_cmd_desc* descr;
765
766	/* Allocate the targbh_descr structure */
767	descr = (struct targbh_cmd_desc *)malloc(sizeof(*descr),
768					       M_DEVBUF, M_NOWAIT);
769	if (descr == NULL)
770		return (NULL);
771
772	bzero(descr, sizeof(*descr));
773
774	/* Allocate buffer backing store */
775	descr->backing_store = malloc(MAX_BUF_SIZE, M_DEVBUF, M_NOWAIT);
776	if (descr->backing_store == NULL) {
777		free(descr, M_DEVBUF);
778		return (NULL);
779	}
780	descr->max_size = MAX_BUF_SIZE;
781	return (descr);
782}
783
784static void
785targbhfreedescr(struct targbh_cmd_desc *descr)
786{
787	free(descr->backing_store, M_DEVBUF);
788	free(descr, M_DEVBUF);
789}
790