scsi_targ_bh.c revision 231772
174778Sbrian/*-
226026Sbrian * Implementation of the Target Mode 'Black Hole device' for CAM.
374778Sbrian *
485964Sbrian * Copyright (c) 1999 Justin T. Gibbs.
577701Sbrian * All rights reserved.
677701Sbrian *
777701Sbrian * Redistribution and use in source and binary forms, with or without
877701Sbrian * modification, are permitted provided that the following conditions
977701Sbrian * are met:
1077701Sbrian * 1. Redistributions of source code must retain the above copyright
1177701Sbrian *    notice, this list of conditions, and the following disclaimer,
1277701Sbrian *    without modification, immediately at the beginning of the file.
1377701Sbrian * 2. The name of the author may not be used to endorse or promote products
1477701Sbrian *    derived from this software without specific prior written permission.
1577701Sbrian *
1677701Sbrian * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
1777701Sbrian * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1877701Sbrian * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
1977701Sbrian * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
2077701Sbrian * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2177701Sbrian * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2277701Sbrian * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2377701Sbrian * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2477701Sbrian * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2577701Sbrian * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2677701Sbrian * SUCH DAMAGE.
2777701Sbrian */
2877701Sbrian
2977701Sbrian#include <sys/cdefs.h>
3077701Sbrian__FBSDID("$FreeBSD: stable/9/sys/cam/scsi/scsi_targ_bh.c 231772 2012-02-15 17:28:09Z ken $");
31162674Spiso
3274778Sbrian#include <sys/param.h>
3374778Sbrian#include <sys/queue.h>
3499207Sbrian#include <sys/systm.h>
3574778Sbrian#include <sys/kernel.h>
3674778Sbrian#include <sys/types.h>
3774778Sbrian#include <sys/bio.h>
3826026Sbrian#include <sys/conf.h>
3926026Sbrian#include <sys/devicestat.h>
4074778Sbrian#include <sys/malloc.h>
4126026Sbrian#include <sys/uio.h>
42162674Spiso
43162674Spiso#include <cam/cam.h>
44162674Spiso#include <cam/cam_ccb.h>
45162674Spiso#include <cam/cam_periph.h>
46162674Spiso#include <cam/cam_queue.h>
47145931Sglebius#include <cam/cam_xpt_periph.h>
48124621Sphk#include <cam/cam_debug.h>
49145931Sglebius#include <cam/cam_sim.h>
50145931Sglebius
51145931Sglebius#include <cam/scsi/scsi_all.h>
52145931Sglebius#include <cam/scsi/scsi_message.h>
53145931Sglebius
54145931SglebiusMALLOC_DEFINE(M_SCSIBH, "SCSI bh", "SCSI blackhole buffers");
55145931Sglebius
56124621Sphktypedef enum {
57124621Sphk	TARGBH_STATE_NORMAL,
58124621Sphk	TARGBH_STATE_EXCEPTION,
59124621Sphk	TARGBH_STATE_TEARDOWN
60124621Sphk} targbh_state;
61124621Sphk
62124621Sphktypedef enum {
63127094Sdes	TARGBH_FLAG_NONE	 = 0x00,
64124621Sphk	TARGBH_FLAG_LUN_ENABLED	 = 0x01
65124621Sphk} targbh_flags;
66124621Sphk
67127094Sdestypedef enum {
68124621Sphk	TARGBH_CCB_WORKQ,
69124621Sphk	TARGBH_CCB_WAITING
7026026Sbrian} targbh_ccb_types;
71124621Sphk
72124621Sphk#define MAX_ACCEPT	8
73124621Sphk#define MAX_IMMEDIATE	16
74124621Sphk#define MAX_BUF_SIZE	256	/* Max inquiry/sense/mode page transfer */
75124621Sphk
76124621Sphk/* Offsets into our private CCB area for storing accept information */
77124621Sphk#define ccb_type	ppriv_field0
78124621Sphk#define ccb_descr	ppriv_ptr1
79124621Sphk
80124621Sphk/* We stick a pointer to the originating accept TIO in each continue I/O CCB */
81124621Sphk#define ccb_atio	ppriv_ptr1
82127094Sdes
83124621SphkTAILQ_HEAD(ccb_queue, ccb_hdr);
8474778Sbrian
85127094Sdesstruct targbh_softc {
86127094Sdes	struct		ccb_queue pending_queue;
87127094Sdes	struct		ccb_queue work_queue;
88127094Sdes	struct		ccb_queue unknown_atio_queue;
89124621Sphk	struct		devstat device_stats;
90127094Sdes	targbh_state	state;
91127094Sdes	targbh_flags	flags;
92124621Sphk	u_int		init_level;
93124621Sphk	u_int		inq_data_len;
94127094Sdes	struct		ccb_accept_tio *accept_tio_list;
95127094Sdes	struct		ccb_hdr_slist immed_notify_slist;
96131566Sphk};
97127094Sdes
98124621Sphkstruct targbh_cmd_desc {
99124621Sphk	struct	  ccb_accept_tio* atio_link;
100124621Sphk	u_int	  data_resid;	/* How much left to transfer */
101127094Sdes	u_int	  data_increment;/* Amount to send before next disconnect */
102131614Sdes	void*	  data;		/* The data. Can be from backing_store or not */
103127094Sdes	void*	  backing_store;/* Backing store allocated for this descriptor*/
104124621Sphk	u_int	  max_size;	/* Size of backing_store */
105127094Sdes	u_int32_t timeout;
106127094Sdes	u_int8_t  status;	/* Status to return to initiator */
107131614Sdes};
108131614Sdes
109124621Sphkstatic struct scsi_inquiry_data no_lun_inq_data =
110127094Sdes{
111127094Sdes	T_NODEVICE | (SID_QUAL_BAD_LU << 5), 0,
112127094Sdes	/* version */2, /* format version */2
113127094Sdes};
114124621Sphk
115127094Sdesstatic struct scsi_sense_data_fixed no_lun_sense_data =
116127094Sdes{
117127094Sdes	SSD_CURRENT_ERROR|SSD_ERRCODE_VALID,
118124621Sphk	0,
119124621Sphk	SSD_KEY_NOT_READY,
120127094Sdes	{ 0, 0, 0, 0 },
121127094Sdes	/*extra_len*/offsetof(struct scsi_sense_data_fixed, fru)
122127094Sdes                   - offsetof(struct scsi_sense_data_fixed, extra_len),
123124621Sphk	{ 0, 0, 0, 0 },
124124621Sphk	/* Logical Unit Not Supported */
125127094Sdes	/*ASC*/0x25, /*ASCQ*/0
126124621Sphk};
127127094Sdes
128127094Sdesstatic const int request_sense_size = offsetof(struct scsi_sense_data_fixed, fru);
129124621Sphk
130124621Sphkstatic periph_init_t	targbhinit;
131127094Sdesstatic void		targbhasync(void *callback_arg, u_int32_t code,
132124621Sphk				    struct cam_path *path, void *arg);
133162674Spisostatic cam_status	targbhenlun(struct cam_periph *periph);
134162674Spisostatic cam_status	targbhdislun(struct cam_periph *periph);
135162674Spisostatic periph_ctor_t	targbhctor;
136162674Spisostatic periph_dtor_t	targbhdtor;
137124621Sphkstatic periph_start_t	targbhstart;
138164797Spisostatic void		targbhdone(struct cam_periph *periph,
139164797Spiso				   union ccb *done_ccb);
140164797Spiso#ifdef NOTYET
141124621Sphkstatic  int		targbherror(union ccb *ccb, u_int32_t cam_flags,
142124621Sphk				    u_int32_t sense_flags);
143124621Sphk#endif
144124621Sphkstatic struct targbh_cmd_desc*	targbhallocdescr(void);
145124621Sphkstatic void		targbhfreedescr(struct targbh_cmd_desc *buf);
14674778Sbrian
14763899Sarchiestatic struct periph_driver targbhdriver =
14874778Sbrian{
14974778Sbrian	targbhinit, "targbh",
15074778Sbrian	TAILQ_HEAD_INITIALIZER(targbhdriver.units), /* generation */ 0
15174778Sbrian};
15274778Sbrian
15327864SbrianPERIPHDRIVER_DECLARE(targbh, targbhdriver);
15474778Sbrian
15574778Sbrianstatic void
15674778Sbriantargbhinit(void)
15774778Sbrian{
15874778Sbrian	cam_status status;
15959702Sru
16074778Sbrian	/*
16174778Sbrian	 * Install a global async callback.  This callback will
16274778Sbrian	 * receive async callbacks like "new path registered".
16374778Sbrian	 */
16474778Sbrian	status = xpt_register_async(AC_PATH_REGISTERED | AC_PATH_DEREGISTERED,
16574778Sbrian				    targbhasync, NULL, NULL);
16674778Sbrian
16774778Sbrian	if (status != CAM_REQ_CMP) {
16844307Sbrian		printf("targbh: Failed to attach master async callback "
16974778Sbrian		       "due to status 0x%x!\n", status);
17074778Sbrian	}
17174778Sbrian}
17274778Sbrian
17374778Sbrianstatic void
17474778Sbriantargbhasync(void *callback_arg, u_int32_t code,
17574778Sbrian	    struct cam_path *path, void *arg)
17674778Sbrian{
177145926Sglebius	struct cam_path *new_path;
17874778Sbrian	struct ccb_pathinq *cpi;
179145926Sglebius	path_id_t bus_path_id;
18074778Sbrian	cam_status status;
18174778Sbrian
18274778Sbrian	cpi = (struct ccb_pathinq *)arg;
18374778Sbrian	if (code == AC_PATH_REGISTERED)
18474778Sbrian		bus_path_id = cpi->ccb_h.path_id;
18574778Sbrian	else
18674778Sbrian		bus_path_id = xpt_path_path_id(path);
18774778Sbrian	/*
18874778Sbrian	 * Allocate a peripheral instance for
18974778Sbrian	 * this target instance.
19027864Sbrian	 */
19174778Sbrian	status = xpt_create_path(&new_path, NULL,
19274778Sbrian				 bus_path_id,
19374778Sbrian				 CAM_TARGET_WILDCARD, CAM_LUN_WILDCARD);
19474778Sbrian	if (status != CAM_REQ_CMP) {
19574778Sbrian		printf("targbhasync: Unable to create path "
19674778Sbrian			"due to status 0x%x\n", status);
19774778Sbrian		return;
19874778Sbrian	}
19927864Sbrian
20074778Sbrian	switch (code) {
20174778Sbrian	case AC_PATH_REGISTERED:
20274778Sbrian	{
20374778Sbrian		/* Only attach to controllers that support target mode */
20474778Sbrian		if ((cpi->target_sprt & PIT_PROCESSOR) == 0)
20544307Sbrian			break;
20674778Sbrian
20774778Sbrian		status = cam_periph_alloc(targbhctor, NULL, targbhdtor,
20874778Sbrian					  targbhstart,
20974778Sbrian					  "targbh", CAM_PERIPH_BIO,
21074778Sbrian					  new_path, targbhasync,
21144307Sbrian					  AC_PATH_REGISTERED,
212223077Sae					  cpi);
213223077Sae		break;
214223077Sae	}
215223077Sae	case AC_PATH_DEREGISTERED:
216223077Sae	{
217223077Sae		struct cam_periph *periph;
218223077Sae
219223077Sae		if ((periph = cam_periph_find(new_path, "targbh")) != NULL)
220223077Sae			cam_periph_invalidate(periph);
221223077Sae		break;
222223077Sae	}
223223080Sae	default:
224223080Sae		break;
225223080Sae	}
226223080Sae	xpt_free_path(new_path);
227223080Sae}
228223080Sae
22974778Sbrian/* Attempt to enable our lun */
23074778Sbrianstatic cam_status
23174778Sbriantargbhenlun(struct cam_periph *periph)
23274778Sbrian{
23374778Sbrian	union ccb immed_ccb;
23474778Sbrian	struct targbh_softc *softc;
23526026Sbrian	cam_status status;
236127094Sdes	int i;
23774778Sbrian
23874778Sbrian	softc = (struct targbh_softc *)periph->softc;
239
240	if ((softc->flags & TARGBH_FLAG_LUN_ENABLED) != 0)
241		return (CAM_REQ_CMP);
242
243	xpt_setup_ccb(&immed_ccb.ccb_h, periph->path, CAM_PRIORITY_NORMAL);
244	immed_ccb.ccb_h.func_code = XPT_EN_LUN;
245
246	/* Don't need support for any vendor specific commands */
247	immed_ccb.cel.grp6_len = 0;
248	immed_ccb.cel.grp7_len = 0;
249	immed_ccb.cel.enable = 1;
250	xpt_action(&immed_ccb);
251	status = immed_ccb.ccb_h.status;
252	if (status != CAM_REQ_CMP) {
253		xpt_print(periph->path,
254		    "targbhenlun - Enable Lun Rejected with status 0x%x\n",
255		    status);
256		return (status);
257	}
258
259	softc->flags |= TARGBH_FLAG_LUN_ENABLED;
260
261	/*
262	 * Build up a buffer of accept target I/O
263	 * operations for incoming selections.
264	 */
265	for (i = 0; i < MAX_ACCEPT; i++) {
266		struct ccb_accept_tio *atio;
267
268		atio = (struct ccb_accept_tio*)malloc(sizeof(*atio), M_SCSIBH,
269						      M_NOWAIT);
270		if (atio == NULL) {
271			status = CAM_RESRC_UNAVAIL;
272			break;
273		}
274
275		atio->ccb_h.ccb_descr = targbhallocdescr();
276
277		if (atio->ccb_h.ccb_descr == NULL) {
278			free(atio, M_SCSIBH);
279			status = CAM_RESRC_UNAVAIL;
280			break;
281		}
282
283		xpt_setup_ccb(&atio->ccb_h, periph->path, CAM_PRIORITY_NORMAL);
284		atio->ccb_h.func_code = XPT_ACCEPT_TARGET_IO;
285		atio->ccb_h.cbfcnp = targbhdone;
286		xpt_action((union ccb *)atio);
287		status = atio->ccb_h.status;
288		if (status != CAM_REQ_INPROG) {
289			targbhfreedescr(atio->ccb_h.ccb_descr);
290			free(atio, M_SCSIBH);
291			break;
292		}
293		((struct targbh_cmd_desc*)atio->ccb_h.ccb_descr)->atio_link =
294		    softc->accept_tio_list;
295		softc->accept_tio_list = atio;
296	}
297
298	if (i == 0) {
299		xpt_print(periph->path,
300		    "targbhenlun - Could not allocate accept tio CCBs: status "
301		    "= 0x%x\n", status);
302		targbhdislun(periph);
303		return (CAM_REQ_CMP_ERR);
304	}
305
306	/*
307	 * Build up a buffer of immediate notify CCBs
308	 * so the SIM can tell us of asynchronous target mode events.
309	 */
310	for (i = 0; i < MAX_ACCEPT; i++) {
311		struct ccb_immed_notify *inot;
312
313		inot = (struct ccb_immed_notify*)malloc(sizeof(*inot), M_SCSIBH,
314						        M_NOWAIT);
315
316		if (inot == NULL) {
317			status = CAM_RESRC_UNAVAIL;
318			break;
319		}
320
321		xpt_setup_ccb(&inot->ccb_h, periph->path, CAM_PRIORITY_NORMAL);
322		inot->ccb_h.func_code = XPT_IMMED_NOTIFY;
323		inot->ccb_h.cbfcnp = targbhdone;
324		xpt_action((union ccb *)inot);
325		status = inot->ccb_h.status;
326		if (status != CAM_REQ_INPROG) {
327			free(inot, M_SCSIBH);
328			break;
329		}
330		SLIST_INSERT_HEAD(&softc->immed_notify_slist, &inot->ccb_h,
331				  periph_links.sle);
332	}
333
334	if (i == 0) {
335		xpt_print(periph->path,
336		    "targbhenlun - Could not allocate immediate notify "
337		    "CCBs: status = 0x%x\n", status);
338		targbhdislun(periph);
339		return (CAM_REQ_CMP_ERR);
340	}
341
342	return (CAM_REQ_CMP);
343}
344
345static cam_status
346targbhdislun(struct cam_periph *periph)
347{
348	union ccb ccb;
349	struct targbh_softc *softc;
350	struct ccb_accept_tio* atio;
351	struct ccb_hdr *ccb_h;
352
353	softc = (struct targbh_softc *)periph->softc;
354	if ((softc->flags & TARGBH_FLAG_LUN_ENABLED) == 0)
355		return CAM_REQ_CMP;
356
357	/* XXX Block for Continue I/O completion */
358
359	/* Kill off all ACCECPT and IMMEDIATE CCBs */
360	while ((atio = softc->accept_tio_list) != NULL) {
361
362		softc->accept_tio_list =
363		    ((struct targbh_cmd_desc*)atio->ccb_h.ccb_descr)->atio_link;
364		xpt_setup_ccb(&ccb.cab.ccb_h, periph->path, CAM_PRIORITY_NORMAL);
365		ccb.cab.ccb_h.func_code = XPT_ABORT;
366		ccb.cab.abort_ccb = (union ccb *)atio;
367		xpt_action(&ccb);
368	}
369
370	while ((ccb_h = SLIST_FIRST(&softc->immed_notify_slist)) != NULL) {
371		SLIST_REMOVE_HEAD(&softc->immed_notify_slist, periph_links.sle);
372		xpt_setup_ccb(&ccb.cab.ccb_h, periph->path, CAM_PRIORITY_NORMAL);
373		ccb.cab.ccb_h.func_code = XPT_ABORT;
374		ccb.cab.abort_ccb = (union ccb *)ccb_h;
375		xpt_action(&ccb);
376	}
377
378	/*
379	 * Dissable this lun.
380	 */
381	xpt_setup_ccb(&ccb.cel.ccb_h, periph->path, CAM_PRIORITY_NORMAL);
382	ccb.cel.ccb_h.func_code = XPT_EN_LUN;
383	ccb.cel.enable = 0;
384	xpt_action(&ccb);
385
386	if (ccb.cel.ccb_h.status != CAM_REQ_CMP)
387		printf("targbhdislun - Disabling lun on controller failed "
388		       "with status 0x%x\n", ccb.cel.ccb_h.status);
389	else
390		softc->flags &= ~TARGBH_FLAG_LUN_ENABLED;
391	return (ccb.cel.ccb_h.status);
392}
393
394static cam_status
395targbhctor(struct cam_periph *periph, void *arg)
396{
397	struct targbh_softc *softc;
398
399	/* Allocate our per-instance private storage */
400	softc = (struct targbh_softc *)malloc(sizeof(*softc),
401					      M_SCSIBH, M_NOWAIT);
402	if (softc == NULL) {
403		printf("targctor: unable to malloc softc\n");
404		return (CAM_REQ_CMP_ERR);
405	}
406
407	bzero(softc, sizeof(*softc));
408	TAILQ_INIT(&softc->pending_queue);
409	TAILQ_INIT(&softc->work_queue);
410	softc->accept_tio_list = NULL;
411	SLIST_INIT(&softc->immed_notify_slist);
412	softc->state = TARGBH_STATE_NORMAL;
413	periph->softc = softc;
414	softc->init_level++;
415
416	return (targbhenlun(periph));
417}
418
419static void
420targbhdtor(struct cam_periph *periph)
421{
422	struct targbh_softc *softc;
423
424	softc = (struct targbh_softc *)periph->softc;
425
426	softc->state = TARGBH_STATE_TEARDOWN;
427
428	targbhdislun(periph);
429
430	switch (softc->init_level) {
431	case 0:
432		panic("targdtor - impossible init level");
433	case 1:
434		/* FALLTHROUGH */
435	default:
436		/* XXX Wait for callback of targbhdislun() */
437		msleep(softc, periph->sim->mtx, PRIBIO, "targbh", hz/2);
438		free(softc, M_SCSIBH);
439		break;
440	}
441}
442
443static void
444targbhstart(struct cam_periph *periph, union ccb *start_ccb)
445{
446	struct targbh_softc *softc;
447	struct ccb_hdr *ccbh;
448	struct ccb_accept_tio *atio;
449	struct targbh_cmd_desc *desc;
450	struct ccb_scsiio *csio;
451	ccb_flags flags;
452
453	softc = (struct targbh_softc *)periph->softc;
454
455	ccbh = TAILQ_FIRST(&softc->work_queue);
456	if (periph->immediate_priority <= periph->pinfo.priority) {
457		start_ccb->ccb_h.ccb_type = TARGBH_CCB_WAITING;
458		SLIST_INSERT_HEAD(&periph->ccb_list, &start_ccb->ccb_h,
459				  periph_links.sle);
460		periph->immediate_priority = CAM_PRIORITY_NONE;
461		wakeup(&periph->ccb_list);
462	} else if (ccbh == NULL) {
463		xpt_release_ccb(start_ccb);
464	} else {
465		TAILQ_REMOVE(&softc->work_queue, ccbh, periph_links.tqe);
466		TAILQ_INSERT_HEAD(&softc->pending_queue, ccbh,
467				  periph_links.tqe);
468		atio = (struct ccb_accept_tio*)ccbh;
469		desc = (struct targbh_cmd_desc *)atio->ccb_h.ccb_descr;
470
471		/* Is this a tagged request? */
472		flags = atio->ccb_h.flags &
473		    (CAM_DIS_DISCONNECT|CAM_TAG_ACTION_VALID|CAM_DIR_MASK);
474
475		csio = &start_ccb->csio;
476		/*
477		 * If we are done with the transaction, tell the
478		 * controller to send status and perform a CMD_CMPLT.
479		 * If we have associated sense data, see if we can
480		 * send that too.
481		 */
482		if (desc->data_resid == desc->data_increment) {
483			flags |= CAM_SEND_STATUS;
484			if (atio->sense_len) {
485				csio->sense_len = atio->sense_len;
486				csio->sense_data = atio->sense_data;
487				flags |= CAM_SEND_SENSE;
488			}
489
490		}
491
492		cam_fill_ctio(csio,
493			      /*retries*/2,
494			      targbhdone,
495			      flags,
496			      (flags & CAM_TAG_ACTION_VALID)?
497				MSG_SIMPLE_Q_TAG : 0,
498			      atio->tag_id,
499			      atio->init_id,
500			      desc->status,
501			      /*data_ptr*/desc->data_increment == 0
502					  ? NULL : desc->data,
503			      /*dxfer_len*/desc->data_increment,
504			      /*timeout*/desc->timeout);
505
506		/* Override our wildcard attachment */
507		start_ccb->ccb_h.target_id = atio->ccb_h.target_id;
508		start_ccb->ccb_h.target_lun = atio->ccb_h.target_lun;
509
510		start_ccb->ccb_h.ccb_type = TARGBH_CCB_WORKQ;
511		start_ccb->ccb_h.ccb_atio = atio;
512		CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE,
513			  ("Sending a CTIO\n"));
514		xpt_action(start_ccb);
515		/*
516		 * If the queue was frozen waiting for the response
517		 * to this ATIO (for instance disconnection was disallowed),
518		 * then release it now that our response has been queued.
519		 */
520		if ((atio->ccb_h.status & CAM_DEV_QFRZN) != 0) {
521			cam_release_devq(periph->path,
522					 /*relsim_flags*/0,
523					 /*reduction*/0,
524					 /*timeout*/0,
525					 /*getcount_only*/0);
526			atio->ccb_h.status &= ~CAM_DEV_QFRZN;
527		}
528		ccbh = TAILQ_FIRST(&softc->work_queue);
529	}
530	if (ccbh != NULL)
531		xpt_schedule(periph, CAM_PRIORITY_NORMAL);
532}
533
534static void
535targbhdone(struct cam_periph *periph, union ccb *done_ccb)
536{
537	struct targbh_softc *softc;
538
539	softc = (struct targbh_softc *)periph->softc;
540
541	if (done_ccb->ccb_h.ccb_type == TARGBH_CCB_WAITING) {
542		/* Caller will release the CCB */
543		wakeup(&done_ccb->ccb_h.cbfcnp);
544		return;
545	}
546
547	switch (done_ccb->ccb_h.func_code) {
548	case XPT_ACCEPT_TARGET_IO:
549	{
550		struct ccb_accept_tio *atio;
551		struct targbh_cmd_desc *descr;
552		u_int8_t *cdb;
553		int priority;
554
555		atio = &done_ccb->atio;
556		descr = (struct targbh_cmd_desc*)atio->ccb_h.ccb_descr;
557		cdb = atio->cdb_io.cdb_bytes;
558		if (softc->state == TARGBH_STATE_TEARDOWN
559		 || atio->ccb_h.status == CAM_REQ_ABORTED) {
560			targbhfreedescr(descr);
561			xpt_free_ccb(done_ccb);
562			return;
563		}
564
565		/*
566		 * Determine the type of incoming command and
567		 * setup our buffer for a response.
568		 */
569		switch (cdb[0]) {
570		case INQUIRY:
571		{
572			struct scsi_inquiry *inq;
573
574			inq = (struct scsi_inquiry *)cdb;
575			CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE,
576				  ("Saw an inquiry!\n"));
577			/*
578			 * Validate the command.  We don't
579			 * support any VPD pages, so complain
580			 * if EVPD is set.
581			 */
582			if ((inq->byte2 & SI_EVPD) != 0
583			 || inq->page_code != 0) {
584				atio->ccb_h.flags &= ~CAM_DIR_MASK;
585				atio->ccb_h.flags |= CAM_DIR_NONE;
586				/*
587				 * This needs to have other than a
588				 * no_lun_sense_data response.
589				 */
590				bcopy(&no_lun_sense_data, &atio->sense_data,
591				      min(sizeof(no_lun_sense_data),
592					  sizeof(atio->sense_data)));
593				atio->sense_len = sizeof(no_lun_sense_data);
594				descr->data_resid = 0;
595				descr->data_increment = 0;
596				descr->status = SCSI_STATUS_CHECK_COND;
597				break;
598			}
599			/*
600			 * Direction is always relative
601			 * to the initator.
602			 */
603			atio->ccb_h.flags &= ~CAM_DIR_MASK;
604			atio->ccb_h.flags |= CAM_DIR_IN;
605			descr->data = &no_lun_inq_data;
606			descr->data_resid = MIN(sizeof(no_lun_inq_data),
607						scsi_2btoul(inq->length));
608			descr->data_increment = descr->data_resid;
609			descr->timeout = 5 * 1000;
610			descr->status = SCSI_STATUS_OK;
611			break;
612		}
613		case REQUEST_SENSE:
614		{
615			struct scsi_request_sense *rsense;
616
617			rsense = (struct scsi_request_sense *)cdb;
618			/* Refer to static sense data */
619			atio->ccb_h.flags &= ~CAM_DIR_MASK;
620			atio->ccb_h.flags |= CAM_DIR_IN;
621			descr->data = &no_lun_sense_data;
622			descr->data_resid = request_sense_size;
623			descr->data_resid = MIN(descr->data_resid,
624						SCSI_CDB6_LEN(rsense->length));
625			descr->data_increment = descr->data_resid;
626			descr->timeout = 5 * 1000;
627			descr->status = SCSI_STATUS_OK;
628			break;
629		}
630		default:
631			/* Constant CA, tell initiator */
632			/* Direction is always relative to the initator */
633			atio->ccb_h.flags &= ~CAM_DIR_MASK;
634			atio->ccb_h.flags |= CAM_DIR_NONE;
635			bcopy(&no_lun_sense_data, &atio->sense_data,
636			      min(sizeof(no_lun_sense_data),
637				  sizeof(atio->sense_data)));
638			atio->sense_len = sizeof (no_lun_sense_data);
639			descr->data_resid = 0;
640			descr->data_increment = 0;
641			descr->timeout = 5 * 1000;
642			descr->status = SCSI_STATUS_CHECK_COND;
643			break;
644		}
645
646		/* Queue us up to receive a Continue Target I/O ccb. */
647		if ((atio->ccb_h.flags & CAM_DIS_DISCONNECT) != 0) {
648			TAILQ_INSERT_HEAD(&softc->work_queue, &atio->ccb_h,
649					  periph_links.tqe);
650			priority = 0;
651		} else {
652			TAILQ_INSERT_TAIL(&softc->work_queue, &atio->ccb_h,
653					  periph_links.tqe);
654			priority = CAM_PRIORITY_NORMAL;
655		}
656		xpt_schedule(periph, priority);
657		break;
658	}
659	case XPT_CONT_TARGET_IO:
660	{
661		struct ccb_accept_tio *atio;
662		struct targbh_cmd_desc *desc;
663
664		CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE,
665			  ("Received completed CTIO\n"));
666		atio = (struct ccb_accept_tio*)done_ccb->ccb_h.ccb_atio;
667		desc = (struct targbh_cmd_desc *)atio->ccb_h.ccb_descr;
668
669		TAILQ_REMOVE(&softc->pending_queue, &atio->ccb_h,
670			     periph_links.tqe);
671
672		/*
673		 * We could check for CAM_SENT_SENSE bein set here,
674		 * but since we're not maintaining any CA/UA state,
675		 * there's no point.
676		 */
677		atio->sense_len = 0;
678		done_ccb->ccb_h.flags &= ~CAM_SEND_SENSE;
679		done_ccb->ccb_h.status &= ~CAM_SENT_SENSE;
680
681		/*
682		 * Any errors will not change the data we return,
683		 * so make sure the queue is not left frozen.
684		 * XXX - At some point there may be errors that
685		 *       leave us in a connected state with the
686		 *       initiator...
687		 */
688		if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0) {
689			printf("Releasing Queue\n");
690			cam_release_devq(done_ccb->ccb_h.path,
691					 /*relsim_flags*/0,
692					 /*reduction*/0,
693					 /*timeout*/0,
694					 /*getcount_only*/0);
695			done_ccb->ccb_h.status &= ~CAM_DEV_QFRZN;
696		}
697		desc->data_resid -= desc->data_increment;
698		xpt_release_ccb(done_ccb);
699		if (softc->state != TARGBH_STATE_TEARDOWN) {
700
701			/*
702			 * Send the original accept TIO back to the
703			 * controller to handle more work.
704			 */
705			CAM_DEBUG(periph->path, CAM_DEBUG_SUBTRACE,
706				  ("Returning ATIO to target\n"));
707			/* Restore wildcards */
708			atio->ccb_h.target_id = CAM_TARGET_WILDCARD;
709			atio->ccb_h.target_lun = CAM_LUN_WILDCARD;
710			xpt_action((union ccb *)atio);
711			break;
712		} else {
713			targbhfreedescr(desc);
714			free(atio, M_SCSIBH);
715		}
716		break;
717	}
718	case XPT_IMMED_NOTIFY:
719	{
720		int frozen;
721
722		frozen = (done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0;
723		if (softc->state == TARGBH_STATE_TEARDOWN
724		 || done_ccb->ccb_h.status == CAM_REQ_ABORTED) {
725			printf("Freed an immediate notify\n");
726			xpt_free_ccb(done_ccb);
727		} else {
728			/* Requeue for another immediate event */
729			xpt_action(done_ccb);
730		}
731		if (frozen != 0)
732			cam_release_devq(periph->path,
733					 /*relsim_flags*/0,
734					 /*opening reduction*/0,
735					 /*timeout*/0,
736					 /*getcount_only*/0);
737		break;
738	}
739	default:
740		panic("targbhdone: Unexpected ccb opcode");
741		break;
742	}
743}
744
745#ifdef NOTYET
746static int
747targbherror(union ccb *ccb, u_int32_t cam_flags, u_int32_t sense_flags)
748{
749	return 0;
750}
751#endif
752
753static struct targbh_cmd_desc*
754targbhallocdescr()
755{
756	struct targbh_cmd_desc* descr;
757
758	/* Allocate the targbh_descr structure */
759	descr = (struct targbh_cmd_desc *)malloc(sizeof(*descr),
760					       M_SCSIBH, M_NOWAIT);
761	if (descr == NULL)
762		return (NULL);
763
764	bzero(descr, sizeof(*descr));
765
766	/* Allocate buffer backing store */
767	descr->backing_store = malloc(MAX_BUF_SIZE, M_SCSIBH, M_NOWAIT);
768	if (descr->backing_store == NULL) {
769		free(descr, M_SCSIBH);
770		return (NULL);
771	}
772	descr->max_size = MAX_BUF_SIZE;
773	return (descr);
774}
775
776static void
777targbhfreedescr(struct targbh_cmd_desc *descr)
778{
779	free(descr->backing_store, M_SCSIBH);
780	free(descr, M_SCSIBH);
781}
782