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