xref: /freebsd/sys/cam/scsi/scsi_targ_bh.c (revision b601c69bdbe8755d26570261d7fd4c02ee4eff74)
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 #include <stddef.h>	/* For offsetof */
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_extend.h>
46 #include <cam/cam_periph.h>
47 #include <cam/cam_queue.h>
48 #include <cam/cam_xpt_periph.h>
49 #include <cam/cam_debug.h>
50 
51 #include <cam/scsi/scsi_all.h>
52 #include <cam/scsi/scsi_message.h>
53 
54 typedef enum {
55 	TARGBH_STATE_NORMAL,
56 	TARGBH_STATE_EXCEPTION,
57 	TARGBH_STATE_TEARDOWN
58 } targbh_state;
59 
60 typedef enum {
61 	TARGBH_FLAG_NONE	 = 0x00,
62 	TARGBH_FLAG_LUN_ENABLED	 = 0x01
63 } targbh_flags;
64 
65 typedef enum {
66 	TARGBH_CCB_WORKQ,
67 	TARGBH_CCB_WAITING
68 } targbh_ccb_types;
69 
70 #define MAX_ACCEPT	8
71 #define MAX_IMMEDIATE	16
72 #define MAX_BUF_SIZE	256	/* Max inquiry/sense/mode page transfer */
73 
74 #define MIN(a, b) ((a > b) ? b : a)
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 
83 TAILQ_HEAD(ccb_queue, ccb_hdr);
84 
85 struct 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 
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 */
105 	u_int32_t timeout;
106 	u_int8_t  status;	/* Status to return to initiator */
107 };
108 
109 static 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 
115 static 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 
128 static const int request_sense_size = offsetof(struct scsi_sense_data, fru);
129 
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);
140 #ifdef NOTYET
141 static  int		targbherror(union ccb *ccb, u_int32_t cam_flags,
142 				    u_int32_t sense_flags);
143 #endif
144 static struct targbh_cmd_desc*	targbhallocdescr(void);
145 static void		targbhfreedescr(struct targbh_cmd_desc *buf);
146 
147 static struct periph_driver targbhdriver =
148 {
149 	targbhinit, "targbh",
150 	TAILQ_HEAD_INITIALIZER(targbhdriver.units), /* generation */ 0
151 };
152 
153 DATA_SET(periphdriver_set, targbhdriver);
154 
155 static void
156 targbhinit(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;
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 
187 static void
188 targbhasync(void *callback_arg, u_int32_t code,
189 	    struct cam_path *path, void *arg)
190 {
191 	struct cam_periph *periph;
192 
193 	periph = (struct cam_periph *)callback_arg;
194 	switch (code) {
195 	case AC_PATH_REGISTERED:
196 	{
197 		struct ccb_pathinq *cpi;
198 		struct cam_path *new_path;
199 		cam_status status;
200 
201 		cpi = (struct ccb_pathinq *)arg;
202 
203 		/* Only attach to controllers that support target mode */
204 		if ((cpi->target_sprt & PIT_PROCESSOR) == 0)
205 			break;
206 
207 		/*
208 		 * Allocate a peripheral instance for
209 		 * this target instance.
210 		 */
211 		status = xpt_create_path(&new_path, NULL,
212 					 xpt_path_path_id(path),
213 					 CAM_TARGET_WILDCARD, CAM_LUN_WILDCARD);
214 		if (status != CAM_REQ_CMP) {
215 			printf("targbhasync: Unable to create path "
216 				"due to status 0x%x\n", status);
217 			break;
218 		}
219 		status = cam_periph_alloc(targbhctor, NULL, targbhdtor,
220 					  targbhstart,
221 					  "targbh", CAM_PERIPH_BIO,
222 					  new_path, targbhasync,
223 					  AC_PATH_REGISTERED,
224 					  cpi);
225 		xpt_free_path(new_path);
226 		if (status != CAM_REQ_CMP
227 		 && status != CAM_REQ_INPROG)
228 			printf("targbhasync: Unable to allocate new periph "
229 			       "due to status 0x%x\n", status);
230 		break;
231 	}
232 	case AC_PATH_DEREGISTERED:
233 	{
234 		targbhdislun(periph);
235 		break;
236 	}
237 	default:
238 		break;
239 	}
240 }
241 
242 /* Attempt to enable our lun */
243 static cam_status
244 targbhenlun(struct cam_periph *periph)
245 {
246 	union ccb immed_ccb;
247 	struct targbh_softc *softc;
248 	cam_status status;
249 	int i;
250 
251 	softc = (struct targbh_softc *)periph->softc;
252 
253 	if ((softc->flags & TARGBH_FLAG_LUN_ENABLED) != 0)
254 		return (CAM_REQ_CMP);
255 
256 	xpt_setup_ccb(&immed_ccb.ccb_h, periph->path, /*priority*/1);
257 	immed_ccb.ccb_h.func_code = XPT_EN_LUN;
258 
259 	/* Don't need support for any vendor specific commands */
260 	immed_ccb.cel.grp6_len = 0;
261 	immed_ccb.cel.grp7_len = 0;
262 	immed_ccb.cel.enable = 1;
263 	xpt_action(&immed_ccb);
264 	status = immed_ccb.ccb_h.status;
265 	if (status != CAM_REQ_CMP) {
266 		xpt_print_path(periph->path);
267 		printf("targbhenlun - Enable Lun Rejected for status 0x%x\n",
268 		       status);
269 		return (status);
270 	}
271 
272 	softc->flags |= TARGBH_FLAG_LUN_ENABLED;
273 
274 	/*
275 	 * Build up a buffer of accept target I/O
276 	 * operations for incoming selections.
277 	 */
278 	for (i = 0; i < MAX_ACCEPT; i++) {
279 		struct ccb_accept_tio *atio;
280 
281 		atio = (struct ccb_accept_tio*)malloc(sizeof(*atio), M_DEVBUF,
282 						      M_NOWAIT);
283 		if (atio == NULL) {
284 			status = CAM_RESRC_UNAVAIL;
285 			break;
286 		}
287 
288 		atio->ccb_h.ccb_descr = targbhallocdescr();
289 
290 		if (atio->ccb_h.ccb_descr == NULL) {
291 			free(atio, M_DEVBUF);
292 			status = CAM_RESRC_UNAVAIL;
293 			break;
294 		}
295 
296 		xpt_setup_ccb(&atio->ccb_h, periph->path, /*priority*/1);
297 		atio->ccb_h.func_code = XPT_ACCEPT_TARGET_IO;
298 		atio->ccb_h.cbfcnp = targbhdone;
299 		xpt_action((union ccb *)atio);
300 		status = atio->ccb_h.status;
301 		if (status != CAM_REQ_INPROG) {
302 			targbhfreedescr(atio->ccb_h.ccb_descr);
303 			free(atio, M_DEVBUF);
304 			break;
305 		}
306 		((struct targbh_cmd_desc*)atio->ccb_h.ccb_descr)->atio_link =
307 		    softc->accept_tio_list;
308 		softc->accept_tio_list = atio;
309 	}
310 
311 	if (i == 0) {
312 		xpt_print_path(periph->path);
313 		printf("targbhenlun - Could not allocate accept tio CCBs: "
314 		       "status = 0x%x\n", status);
315 		targbhdislun(periph);
316 		return (CAM_REQ_CMP_ERR);
317 	}
318 
319 	/*
320 	 * Build up a buffer of immediate notify CCBs
321 	 * so the SIM can tell us of asynchronous target mode events.
322 	 */
323 	for (i = 0; i < MAX_ACCEPT; i++) {
324 		struct ccb_immed_notify *inot;
325 
326 		inot = (struct ccb_immed_notify*)malloc(sizeof(*inot), M_DEVBUF,
327 						        M_NOWAIT);
328 
329 		if (inot == NULL) {
330 			status = CAM_RESRC_UNAVAIL;
331 			break;
332 		}
333 
334 		xpt_setup_ccb(&inot->ccb_h, periph->path, /*priority*/1);
335 		inot->ccb_h.func_code = XPT_IMMED_NOTIFY;
336 		inot->ccb_h.cbfcnp = targbhdone;
337 		xpt_action((union ccb *)inot);
338 		status = inot->ccb_h.status;
339 		if (status != CAM_REQ_INPROG) {
340 			free(inot, M_DEVBUF);
341 			break;
342 		}
343 		SLIST_INSERT_HEAD(&softc->immed_notify_slist, &inot->ccb_h,
344 				  periph_links.sle);
345 	}
346 
347 	if (i == 0) {
348 		xpt_print_path(periph->path);
349 		printf("targbhenlun - Could not allocate immediate notify "
350 		       "CCBs: status = 0x%x\n", status);
351 		targbhdislun(periph);
352 		return (CAM_REQ_CMP_ERR);
353 	}
354 
355 	return (CAM_REQ_CMP);
356 }
357 
358 static cam_status
359 targbhdislun(struct cam_periph *periph)
360 {
361 	union ccb ccb;
362 	struct targbh_softc *softc;
363 	struct ccb_accept_tio* atio;
364 	struct ccb_hdr *ccb_h;
365 
366 	softc = (struct targbh_softc *)periph->softc;
367 	if ((softc->flags & TARGBH_FLAG_LUN_ENABLED) == 0)
368 		return CAM_REQ_CMP;
369 
370 	/* XXX Block for Continue I/O completion */
371 
372 	/* Kill off all ACCECPT and IMMEDIATE CCBs */
373 	while ((atio = softc->accept_tio_list) != NULL) {
374 
375 		softc->accept_tio_list =
376 		    ((struct targbh_cmd_desc*)atio->ccb_h.ccb_descr)->atio_link;
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 *)atio;
380 		xpt_action(&ccb);
381 	}
382 
383 	while ((ccb_h = SLIST_FIRST(&softc->immed_notify_slist)) != NULL) {
384 		SLIST_REMOVE_HEAD(&softc->immed_notify_slist, periph_links.sle);
385 		xpt_setup_ccb(&ccb.cab.ccb_h, periph->path, /*priority*/1);
386 		ccb.cab.ccb_h.func_code = XPT_ABORT;
387 		ccb.cab.abort_ccb = (union ccb *)ccb_h;
388 		xpt_action(&ccb);
389 	}
390 
391 	/*
392 	 * Dissable this lun.
393 	 */
394 	xpt_setup_ccb(&ccb.cel.ccb_h, periph->path, /*priority*/1);
395 	ccb.cel.ccb_h.func_code = XPT_EN_LUN;
396 	ccb.cel.enable = 0;
397 	xpt_action(&ccb);
398 
399 	if (ccb.cel.ccb_h.status != CAM_REQ_CMP)
400 		printf("targbhdislun - Disabling lun on controller failed "
401 		       "with status 0x%x\n", ccb.cel.ccb_h.status);
402 	else
403 		softc->flags &= ~TARGBH_FLAG_LUN_ENABLED;
404 	return (ccb.cel.ccb_h.status);
405 }
406 
407 static cam_status
408 targbhctor(struct cam_periph *periph, void *arg)
409 {
410 	struct ccb_pathinq *cpi;
411 	struct targbh_softc *softc;
412 
413 	cpi = (struct ccb_pathinq *)arg;
414 
415 	/* Allocate our per-instance private storage */
416 	softc = (struct targbh_softc *)malloc(sizeof(*softc),
417 					      M_DEVBUF, M_NOWAIT);
418 	if (softc == NULL) {
419 		printf("targctor: unable to malloc softc\n");
420 		return (CAM_REQ_CMP_ERR);
421 	}
422 
423 	bzero(softc, sizeof(*softc));
424 	TAILQ_INIT(&softc->pending_queue);
425 	TAILQ_INIT(&softc->work_queue);
426 	softc->accept_tio_list = NULL;
427 	SLIST_INIT(&softc->immed_notify_slist);
428 	softc->state = TARGBH_STATE_NORMAL;
429 	periph->softc = softc;
430 	softc->init_level++;
431 
432 	return (targbhenlun(periph));
433 }
434 
435 static void
436 targbhdtor(struct cam_periph *periph)
437 {
438 	struct targbh_softc *softc;
439 
440 	softc = (struct targbh_softc *)periph->softc;
441 
442 	softc->state = TARGBH_STATE_TEARDOWN;
443 
444 	targbhdislun(periph);
445 
446 	switch (softc->init_level) {
447 	default:
448 		/* FALLTHROUGH */
449 	case 1:
450 		free(softc, M_DEVBUF);
451 		break;
452 	case 0:
453 		panic("targdtor - impossible init level");;
454 	}
455 }
456 
457 static void
458 targbhstart(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 	int    s;
467 
468 	softc = (struct targbh_softc *)periph->softc;
469 
470 	s = splbio();
471 	ccbh = TAILQ_FIRST(&softc->work_queue);
472 	if (periph->immediate_priority <= periph->pinfo.priority) {
473 		start_ccb->ccb_h.ccb_type = TARGBH_CCB_WAITING;
474 		SLIST_INSERT_HEAD(&periph->ccb_list, &start_ccb->ccb_h,
475 				  periph_links.sle);
476 		periph->immediate_priority = CAM_PRIORITY_NONE;
477 		splx(s);
478 		wakeup(&periph->ccb_list);
479 	} else if (ccbh == NULL) {
480 		splx(s);
481 		xpt_release_ccb(start_ccb);
482 	} else {
483 		TAILQ_REMOVE(&softc->work_queue, ccbh, periph_links.tqe);
484 		TAILQ_INSERT_HEAD(&softc->pending_queue, ccbh,
485 				  periph_links.tqe);
486 		splx(s);
487 		atio = (struct ccb_accept_tio*)ccbh;
488 		desc = (struct targbh_cmd_desc *)atio->ccb_h.ccb_descr;
489 
490 		/* Is this a tagged request? */
491 		flags = atio->ccb_h.flags & (CAM_TAG_ACTION_VALID|CAM_DIR_MASK);
492 
493 		csio = &start_ccb->csio;
494 		/*
495 		 * If we are done with the transaction, tell the
496 		 * controller to send status and perform a CMD_CMPLT.
497 		 * If we have associated sense data, see if we can
498 		 * send that too.
499 		 */
500 		if (desc->data_resid == desc->data_increment) {
501 			flags |= CAM_SEND_STATUS;
502 			if (atio->sense_len) {
503 				csio->sense_len = atio->sense_len;
504 				csio->sense_data = atio->sense_data;
505 				flags |= CAM_SEND_SENSE;
506 			}
507 
508 		}
509 
510 		cam_fill_ctio(csio,
511 			      /*retries*/2,
512 			      targbhdone,
513 			      flags,
514 			      (flags & CAM_TAG_ACTION_VALID)?
515 				MSG_SIMPLE_Q_TAG : 0,
516 			      atio->tag_id,
517 			      atio->init_id,
518 			      desc->status,
519 			      /*data_ptr*/desc->data_increment == 0
520 					  ? NULL : desc->data,
521 			      /*dxfer_len*/desc->data_increment,
522 			      /*timeout*/desc->timeout);
523 
524 		/* Override our wildcard attachment */
525 		start_ccb->ccb_h.target_id = atio->ccb_h.target_id;
526 		start_ccb->ccb_h.target_lun = atio->ccb_h.target_lun;
527 
528 		start_ccb->ccb_h.ccb_type = TARGBH_CCB_WORKQ;
529 		start_ccb->ccb_h.ccb_atio = atio;
530 		CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE,
531 			  ("Sending a CTIO\n"));
532 		xpt_action(start_ccb);
533 		s = splbio();
534 		ccbh = TAILQ_FIRST(&softc->work_queue);
535 		splx(s);
536 	}
537 	if (ccbh != NULL)
538 		xpt_schedule(periph, /*priority*/1);
539 }
540 
541 static void
542 targbhdone(struct cam_periph *periph, union ccb *done_ccb)
543 {
544 	struct targbh_softc *softc;
545 
546 	softc = (struct targbh_softc *)periph->softc;
547 
548 	if (done_ccb->ccb_h.ccb_type == TARGBH_CCB_WAITING) {
549 		/* Caller will release the CCB */
550 		wakeup(&done_ccb->ccb_h.cbfcnp);
551 		return;
552 	}
553 
554 	switch (done_ccb->ccb_h.func_code) {
555 	case XPT_ACCEPT_TARGET_IO:
556 	{
557 		struct ccb_accept_tio *atio;
558 		struct targbh_cmd_desc *descr;
559 		u_int8_t *cdb;
560 
561 		atio = &done_ccb->atio;
562 		descr = (struct targbh_cmd_desc*)atio->ccb_h.ccb_descr;
563 		cdb = atio->cdb_io.cdb_bytes;
564 		if (softc->state == TARGBH_STATE_TEARDOWN
565 		 || atio->ccb_h.status == CAM_REQ_ABORTED) {
566 			targbhfreedescr(descr);
567 			free(done_ccb, M_DEVBUF);
568 			return;
569 		}
570 
571 		/*
572 		 * Determine the type of incoming command and
573 		 * setup our buffer for a response.
574 		 */
575 		switch (cdb[0]) {
576 		case INQUIRY:
577 		{
578 			struct scsi_inquiry *inq;
579 
580 			inq = (struct scsi_inquiry *)cdb;
581 			CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE,
582 				  ("Saw an inquiry!\n"));
583 			/*
584 			 * Validate the command.  We don't
585 			 * support any VPD pages, so complain
586 			 * if EVPD is set.
587 			 */
588 			if ((inq->byte2 & SI_EVPD) != 0
589 			 || inq->page_code != 0) {
590 				atio->ccb_h.flags &= ~CAM_DIR_MASK;
591 				atio->ccb_h.flags |= CAM_DIR_NONE;
592 				/*
593 				 * This needs to have other than a
594 				 * no_lun_sense_data response.
595 				 */
596 				atio->sense_data = no_lun_sense_data;
597 				atio->sense_len = sizeof(no_lun_sense_data);
598 				descr->data_resid = 0;
599 				descr->data_increment = 0;
600 				descr->status = SCSI_STATUS_CHECK_COND;
601 				break;
602 			}
603 			/*
604 			 * Direction is always relative
605 			 * to the initator.
606 			 */
607 			atio->ccb_h.flags &= ~CAM_DIR_MASK;
608 			atio->ccb_h.flags |= CAM_DIR_IN;
609 			descr->data = &no_lun_inq_data;
610 			descr->data_resid = MIN(sizeof(no_lun_inq_data),
611 						SCSI_CDB6_LEN(inq->length));
612 			descr->data_increment = descr->data_resid;
613 			descr->timeout = 5 * 1000;
614 			descr->status = SCSI_STATUS_OK;
615 			break;
616 		}
617 		case REQUEST_SENSE:
618 		{
619 			struct scsi_request_sense *rsense;
620 
621 			rsense = (struct scsi_request_sense *)cdb;
622 			/* Refer to static sense data */
623 			atio->ccb_h.flags &= ~CAM_DIR_MASK;
624 			atio->ccb_h.flags |= CAM_DIR_IN;
625 			descr->data = &no_lun_sense_data;
626 			descr->data_resid = request_sense_size;
627 			descr->data_resid = MIN(descr->data_resid,
628 						SCSI_CDB6_LEN(rsense->length));
629 			descr->data_increment = descr->data_resid;
630 			descr->timeout = 5 * 1000;
631 			descr->status = SCSI_STATUS_OK;
632 			break;
633 		}
634 		default:
635 			/* Constant CA, tell initiator */
636 			/* Direction is always relative to the initator */
637 			atio->ccb_h.flags &= ~CAM_DIR_MASK;
638 			atio->ccb_h.flags |= CAM_DIR_NONE;
639 			atio->sense_data = no_lun_sense_data;
640 			atio->sense_len = sizeof (no_lun_sense_data);
641 			descr->data_resid = 0;
642 			descr->data_increment = 0;
643 			descr->timeout = 5 * 1000;
644 			descr->status = SCSI_STATUS_CHECK_COND;
645 			break;
646 		}
647 
648 		/* Queue us up to receive a Continue Target I/O ccb. */
649 		TAILQ_INSERT_TAIL(&softc->work_queue, &atio->ccb_h,
650 				  periph_links.tqe);
651 		xpt_schedule(periph, /*priority*/1);
652 		break;
653 	}
654 	case XPT_CONT_TARGET_IO:
655 	{
656 		struct ccb_accept_tio *atio;
657 		struct targbh_cmd_desc *desc;
658 
659 		CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE,
660 			  ("Received completed CTIO\n"));
661 		atio = (struct ccb_accept_tio*)done_ccb->ccb_h.ccb_atio;
662 		desc = (struct targbh_cmd_desc *)atio->ccb_h.ccb_descr;
663 
664 		TAILQ_REMOVE(&softc->pending_queue, &atio->ccb_h,
665 			     periph_links.tqe);
666 
667 		/*
668 		 * We could check for CAM_SENT_SENSE bein set here,
669 		 * but since we're not maintaining any CA/UA state,
670 		 * there's no point.
671 		 */
672 		atio->sense_len = 0;
673 		done_ccb->ccb_h.flags &= ~CAM_SEND_SENSE;
674 		done_ccb->ccb_h.status &= ~CAM_SENT_SENSE;
675 
676 		/* XXX Check for errors */
677 		desc->data_resid -= desc->data_increment;
678 		xpt_release_ccb(done_ccb);
679 		if (softc->state != TARGBH_STATE_TEARDOWN) {
680 
681 			/*
682 			 * Send the original accept TIO back to the
683 			 * controller to handle more work.
684 			 */
685 			CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE,
686 				  ("Returning ATIO to target\n"));
687 			/* Restore wildcards */
688 			atio->ccb_h.target_id = CAM_TARGET_WILDCARD;
689 			atio->ccb_h.target_lun = CAM_LUN_WILDCARD;
690 			xpt_action((union ccb *)atio);
691 			break;
692 		} else {
693 			targbhfreedescr(desc);
694 			free(atio, M_DEVBUF);
695 		}
696 		break;
697 	}
698 	case XPT_IMMED_NOTIFY:
699 	{
700 		if (softc->state == TARGBH_STATE_TEARDOWN
701 		 || done_ccb->ccb_h.status == CAM_REQ_ABORTED) {
702 			printf("Freed an immediate notify\n");
703 			free(done_ccb, M_DEVBUF);
704 		}
705 		break;
706 	}
707 	default:
708 		panic("targbhdone: Unexpected ccb opcode");
709 		break;
710 	}
711 }
712 
713 #ifdef NOTYET
714 static int
715 targbherror(union ccb *ccb, u_int32_t cam_flags, u_int32_t sense_flags)
716 {
717 	return 0;
718 }
719 #endif
720 
721 static struct targbh_cmd_desc*
722 targbhallocdescr()
723 {
724 	struct targbh_cmd_desc* descr;
725 
726 	/* Allocate the targbh_descr structure */
727 	descr = (struct targbh_cmd_desc *)malloc(sizeof(*descr),
728 					       M_DEVBUF, M_NOWAIT);
729 	if (descr == NULL)
730 		return (NULL);
731 
732 	bzero(descr, sizeof(*descr));
733 
734 	/* Allocate buffer backing store */
735 	descr->backing_store = malloc(MAX_BUF_SIZE, M_DEVBUF, M_NOWAIT);
736 	if (descr->backing_store == NULL) {
737 		free(descr, M_DEVBUF);
738 		return (NULL);
739 	}
740 	descr->max_size = MAX_BUF_SIZE;
741 	return (descr);
742 }
743 
744 static void
745 targbhfreedescr(struct targbh_cmd_desc *descr)
746 {
747 	free(descr->backing_store, M_DEVBUF);
748 	free(descr, M_DEVBUF);
749 }
750